applespi.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * MacBook (Pro) SPI keyboard and touchpad driver
  4. *
  5. * Copyright (c) 2015-2018 Federico Lorenzi
  6. * Copyright (c) 2017-2018 Ronald Tschalär
  7. */
  8. /*
  9. * The keyboard and touchpad controller on the MacBookAir6, MacBookPro12,
  10. * MacBook8 and newer can be driven either by USB or SPI. However the USB
  11. * pins are only connected on the MacBookAir6 and 7 and the MacBookPro12.
  12. * All others need this driver. The interface is selected using ACPI methods:
  13. *
  14. * * UIEN ("USB Interface Enable"): If invoked with argument 1, disables SPI
  15. * and enables USB. If invoked with argument 0, disables USB.
  16. * * UIST ("USB Interface Status"): Returns 1 if USB is enabled, 0 otherwise.
  17. * * SIEN ("SPI Interface Enable"): If invoked with argument 1, disables USB
  18. * and enables SPI. If invoked with argument 0, disables SPI.
  19. * * SIST ("SPI Interface Status"): Returns 1 if SPI is enabled, 0 otherwise.
  20. * * ISOL: Resets the four GPIO pins used for SPI. Intended to be invoked with
  21. * argument 1, then once more with argument 0.
  22. *
  23. * UIEN and UIST are only provided on models where the USB pins are connected.
  24. *
  25. * SPI-based Protocol
  26. * ------------------
  27. *
  28. * The device and driver exchange messages (struct message); each message is
  29. * encapsulated in one or more packets (struct spi_packet). There are two types
  30. * of exchanges: reads, and writes. A read is signaled by a GPE, upon which one
  31. * message can be read from the device. A write exchange consists of writing a
  32. * command message, immediately reading a short status packet, and then, upon
  33. * receiving a GPE, reading the response message. Write exchanges cannot be
  34. * interleaved, i.e. a new write exchange must not be started till the previous
  35. * write exchange is complete. Whether a received message is part of a read or
  36. * write exchange is indicated in the encapsulating packet's flags field.
  37. *
  38. * A single message may be too large to fit in a single packet (which has a
  39. * fixed, 256-byte size). In that case it will be split over multiple,
  40. * consecutive packets.
  41. */
  42. #include <linux/acpi.h>
  43. #include <linux/crc16.h>
  44. #include <linux/debugfs.h>
  45. #include <linux/delay.h>
  46. #include <linux/efi.h>
  47. #include <linux/input.h>
  48. #include <linux/input/mt.h>
  49. #include <linux/ktime.h>
  50. #include <linux/leds.h>
  51. #include <linux/module.h>
  52. #include <linux/spinlock.h>
  53. #include <linux/spi/spi.h>
  54. #include <linux/wait.h>
  55. #include <linux/workqueue.h>
  56. #include <asm/barrier.h>
  57. #include <linux/unaligned.h>
  58. #define CREATE_TRACE_POINTS
  59. #include "applespi.h"
  60. #include "applespi_trace.h"
  61. #define APPLESPI_PACKET_SIZE 256
  62. #define APPLESPI_STATUS_SIZE 4
  63. #define PACKET_TYPE_READ 0x20
  64. #define PACKET_TYPE_WRITE 0x40
  65. #define PACKET_DEV_KEYB 0x01
  66. #define PACKET_DEV_TPAD 0x02
  67. #define PACKET_DEV_INFO 0xd0
  68. #define MAX_ROLLOVER 6
  69. #define MAX_FINGERS 11
  70. #define MAX_FINGER_ORIENTATION 16384
  71. #define MAX_PKTS_PER_MSG 2
  72. #define KBD_BL_LEVEL_MIN 32U
  73. #define KBD_BL_LEVEL_MAX 255U
  74. #define KBD_BL_LEVEL_SCALE 1000000U
  75. #define KBD_BL_LEVEL_ADJ \
  76. ((KBD_BL_LEVEL_MAX - KBD_BL_LEVEL_MIN) * KBD_BL_LEVEL_SCALE / 255U)
  77. #define EFI_BL_LEVEL_NAME L"KeyboardBacklightLevel"
  78. #define EFI_BL_LEVEL_GUID EFI_GUID(0xa076d2af, 0x9678, 0x4386, 0x8b, 0x58, 0x1f, 0xc8, 0xef, 0x04, 0x16, 0x19)
  79. #define APPLE_FLAG_FKEY 0x01
  80. #define SPI_RW_CHG_DELAY_US 100 /* from experimentation, in µs */
  81. #define SYNAPTICS_VENDOR_ID 0x06cb
  82. static unsigned int fnmode = 1;
  83. module_param(fnmode, uint, 0644);
  84. MODULE_PARM_DESC(fnmode, "Mode of Fn key on Apple keyboards (0 = disabled, [1] = fkeyslast, 2 = fkeysfirst)");
  85. static unsigned int fnremap;
  86. module_param(fnremap, uint, 0644);
  87. MODULE_PARM_DESC(fnremap, "Remap Fn key ([0] = no-remap; 1 = left-ctrl, 2 = left-shift, 3 = left-alt, 4 = left-meta, 6 = right-shift, 7 = right-alt, 8 = right-meta)");
  88. static bool iso_layout;
  89. module_param(iso_layout, bool, 0644);
  90. MODULE_PARM_DESC(iso_layout, "Enable/Disable hardcoded ISO-layout of the keyboard. ([0] = disabled, 1 = enabled)");
  91. static char touchpad_dimensions[40];
  92. module_param_string(touchpad_dimensions, touchpad_dimensions,
  93. sizeof(touchpad_dimensions), 0444);
  94. MODULE_PARM_DESC(touchpad_dimensions, "The pixel dimensions of the touchpad, as XxY+W+H .");
  95. /**
  96. * struct keyboard_protocol - keyboard message.
  97. * message.type = 0x0110, message.length = 0x000a
  98. *
  99. * @unknown1: unknown
  100. * @modifiers: bit-set of modifier/control keys pressed
  101. * @unknown2: unknown
  102. * @keys_pressed: the (non-modifier) keys currently pressed
  103. * @fn_pressed: whether the fn key is currently pressed
  104. * @crc16: crc over the whole message struct (message header +
  105. * this struct) minus this @crc16 field
  106. */
  107. struct keyboard_protocol {
  108. u8 unknown1;
  109. u8 modifiers;
  110. u8 unknown2;
  111. u8 keys_pressed[MAX_ROLLOVER];
  112. u8 fn_pressed;
  113. __le16 crc16;
  114. };
  115. /**
  116. * struct tp_finger - single trackpad finger structure, le16-aligned
  117. *
  118. * @origin: zero when switching track finger
  119. * @abs_x: absolute x coordinate
  120. * @abs_y: absolute y coordinate
  121. * @rel_x: relative x coordinate
  122. * @rel_y: relative y coordinate
  123. * @tool_major: tool area, major axis
  124. * @tool_minor: tool area, minor axis
  125. * @orientation: 16384 when point, else 15 bit angle
  126. * @touch_major: touch area, major axis
  127. * @touch_minor: touch area, minor axis
  128. * @unused: zeros
  129. * @pressure: pressure on forcetouch touchpad
  130. * @multi: one finger: varies, more fingers: constant
  131. * @crc16: on last finger: crc over the whole message struct
  132. * (i.e. message header + this struct) minus the last
  133. * @crc16 field; unknown on all other fingers.
  134. */
  135. struct tp_finger {
  136. __le16 origin;
  137. __le16 abs_x;
  138. __le16 abs_y;
  139. __le16 rel_x;
  140. __le16 rel_y;
  141. __le16 tool_major;
  142. __le16 tool_minor;
  143. __le16 orientation;
  144. __le16 touch_major;
  145. __le16 touch_minor;
  146. __le16 unused[2];
  147. __le16 pressure;
  148. __le16 multi;
  149. __le16 crc16;
  150. };
  151. /**
  152. * struct touchpad_protocol - touchpad message.
  153. * message.type = 0x0210
  154. *
  155. * @unknown1: unknown
  156. * @clicked: 1 if a button-click was detected, 0 otherwise
  157. * @unknown2: unknown
  158. * @number_of_fingers: the number of fingers being reported in @fingers
  159. * @clicked2: same as @clicked
  160. * @unknown3: unknown
  161. * @fingers: the data for each finger
  162. */
  163. struct touchpad_protocol {
  164. u8 unknown1[1];
  165. u8 clicked;
  166. u8 unknown2[28];
  167. u8 number_of_fingers;
  168. u8 clicked2;
  169. u8 unknown3[16];
  170. struct tp_finger fingers[];
  171. };
  172. /**
  173. * struct command_protocol_tp_info - get touchpad info.
  174. * message.type = 0x1020, message.length = 0x0000
  175. *
  176. * @crc16: crc over the whole message struct (message header +
  177. * this struct) minus this @crc16 field
  178. */
  179. struct command_protocol_tp_info {
  180. __le16 crc16;
  181. };
  182. /**
  183. * struct touchpad_info_protocol - touchpad info response.
  184. * message.type = 0x1020, message.length = 0x006e
  185. *
  186. * @unknown1: unknown
  187. * @model_flags: flags (vary by model number, but significance otherwise
  188. * unknown)
  189. * @model_no: the touchpad model number
  190. * @unknown2: unknown
  191. * @crc16: crc over the whole message struct (message header +
  192. * this struct) minus this @crc16 field
  193. */
  194. struct touchpad_info_protocol {
  195. u8 unknown1[105];
  196. u8 model_flags;
  197. u8 model_no;
  198. u8 unknown2[3];
  199. __le16 crc16;
  200. };
  201. /**
  202. * struct command_protocol_mt_init - initialize multitouch.
  203. * message.type = 0x0252, message.length = 0x0002
  204. *
  205. * @cmd: value: 0x0102
  206. * @crc16: crc over the whole message struct (message header +
  207. * this struct) minus this @crc16 field
  208. */
  209. struct command_protocol_mt_init {
  210. __le16 cmd;
  211. __le16 crc16;
  212. };
  213. /**
  214. * struct command_protocol_capsl - toggle caps-lock led
  215. * message.type = 0x0151, message.length = 0x0002
  216. *
  217. * @unknown: value: 0x01 (length?)
  218. * @led: 0 off, 2 on
  219. * @crc16: crc over the whole message struct (message header +
  220. * this struct) minus this @crc16 field
  221. */
  222. struct command_protocol_capsl {
  223. u8 unknown;
  224. u8 led;
  225. __le16 crc16;
  226. };
  227. /**
  228. * struct command_protocol_bl - set keyboard backlight brightness
  229. * message.type = 0xB051, message.length = 0x0006
  230. *
  231. * @const1: value: 0x01B0
  232. * @level: the brightness level to set
  233. * @const2: value: 0x0001 (backlight off), 0x01F4 (backlight on)
  234. * @crc16: crc over the whole message struct (message header +
  235. * this struct) minus this @crc16 field
  236. */
  237. struct command_protocol_bl {
  238. __le16 const1;
  239. __le16 level;
  240. __le16 const2;
  241. __le16 crc16;
  242. };
  243. /**
  244. * struct message - a complete spi message.
  245. *
  246. * Each message begins with fixed header, followed by a message-type specific
  247. * payload, and ends with a 16-bit crc. Because of the varying lengths of the
  248. * payload, the crc is defined at the end of each payload struct, rather than
  249. * in this struct.
  250. *
  251. * @type: the message type
  252. * @zero: always 0
  253. * @counter: incremented on each message, rolls over after 255; there is a
  254. * separate counter for each message type.
  255. * @rsp_buf_len:response buffer length (the exact nature of this field is quite
  256. * speculative). On a request/write this is often the same as
  257. * @length, though in some cases it has been seen to be much larger
  258. * (e.g. 0x400); on a response/read this the same as on the
  259. * request; for reads that are not responses it is 0.
  260. * @length: length of the remainder of the data in the whole message
  261. * structure (after re-assembly in case of being split over
  262. * multiple spi-packets), minus the trailing crc. The total size
  263. * of the message struct is therefore @length + 10.
  264. *
  265. * @keyboard: Keyboard message
  266. * @touchpad: Touchpad message
  267. * @tp_info: Touchpad info (response)
  268. * @tp_info_command: Touchpad info (CRC)
  269. * @init_mt_command: Initialise Multitouch
  270. * @capsl_command: Toggle caps-lock LED
  271. * @bl_command: Keyboard brightness
  272. * @data: Buffer data
  273. */
  274. struct message {
  275. __le16 type;
  276. u8 zero;
  277. u8 counter;
  278. __le16 rsp_buf_len;
  279. __le16 length;
  280. union {
  281. struct keyboard_protocol keyboard;
  282. struct touchpad_protocol touchpad;
  283. struct touchpad_info_protocol tp_info;
  284. struct command_protocol_tp_info tp_info_command;
  285. struct command_protocol_mt_init init_mt_command;
  286. struct command_protocol_capsl capsl_command;
  287. struct command_protocol_bl bl_command;
  288. DECLARE_FLEX_ARRAY(u8, data);
  289. };
  290. };
  291. /* type + zero + counter + rsp_buf_len + length */
  292. #define MSG_HEADER_SIZE 8
  293. /**
  294. * struct spi_packet - a complete spi packet; always 256 bytes. This carries
  295. * the (parts of the) message in the data. But note that this does not
  296. * necessarily contain a complete message, as in some cases (e.g. many
  297. * fingers pressed) the message is split over multiple packets (see the
  298. * @offset, @remaining, and @length fields). In general the data parts in
  299. * spi_packet's are concatenated until @remaining is 0, and the result is an
  300. * message.
  301. *
  302. * @flags: 0x40 = write (to device), 0x20 = read (from device); note that
  303. * the response to a write still has 0x40.
  304. * @device: 1 = keyboard, 2 = touchpad
  305. * @offset: specifies the offset of this packet's data in the complete
  306. * message; i.e. > 0 indicates this is a continuation packet (in
  307. * the second packet for a message split over multiple packets
  308. * this would then be the same as the @length in the first packet)
  309. * @remaining: number of message bytes remaining in subsequents packets (in
  310. * the first packet of a message split over two packets this would
  311. * then be the same as the @length in the second packet)
  312. * @length: length of the valid data in the @data in this packet
  313. * @data: all or part of a message
  314. * @crc16: crc over this whole structure minus this @crc16 field. This
  315. * covers just this packet, even on multi-packet messages (in
  316. * contrast to the crc in the message).
  317. */
  318. struct spi_packet {
  319. u8 flags;
  320. u8 device;
  321. __le16 offset;
  322. __le16 remaining;
  323. __le16 length;
  324. u8 data[246];
  325. __le16 crc16;
  326. };
  327. struct spi_settings {
  328. u64 spi_cs_delay; /* cs-to-clk delay in us */
  329. u64 reset_a2r_usec; /* active-to-receive delay? */
  330. u64 reset_rec_usec; /* ? (cur val: 10) */
  331. };
  332. /* this mimics struct drm_rect */
  333. struct applespi_tp_info {
  334. int x_min;
  335. int y_min;
  336. int x_max;
  337. int y_max;
  338. };
  339. struct applespi_data {
  340. struct spi_device *spi;
  341. struct spi_settings spi_settings;
  342. struct input_dev *keyboard_input_dev;
  343. struct input_dev *touchpad_input_dev;
  344. u8 *tx_buffer;
  345. u8 *tx_status;
  346. u8 *rx_buffer;
  347. u8 *msg_buf;
  348. unsigned int saved_msg_len;
  349. struct applespi_tp_info tp_info;
  350. u8 last_keys_pressed[MAX_ROLLOVER];
  351. u8 last_keys_fn_pressed[MAX_ROLLOVER];
  352. u8 last_fn_pressed;
  353. struct input_mt_pos pos[MAX_FINGERS];
  354. int slots[MAX_FINGERS];
  355. int gpe;
  356. acpi_handle sien;
  357. acpi_handle sist;
  358. struct spi_transfer dl_t;
  359. struct spi_transfer rd_t;
  360. struct spi_message rd_m;
  361. struct spi_transfer ww_t;
  362. struct spi_transfer wd_t;
  363. struct spi_transfer wr_t;
  364. struct spi_transfer st_t;
  365. struct spi_message wr_m;
  366. bool want_tp_info_cmd;
  367. bool want_mt_init_cmd;
  368. bool want_cl_led_on;
  369. bool have_cl_led_on;
  370. unsigned int want_bl_level;
  371. unsigned int have_bl_level;
  372. unsigned int cmd_msg_cntr;
  373. /* lock to protect the above parameters and flags below */
  374. spinlock_t cmd_msg_lock;
  375. ktime_t cmd_msg_queued;
  376. enum applespi_evt_type cmd_evt_type;
  377. struct led_classdev backlight_info;
  378. bool suspended;
  379. bool drain;
  380. wait_queue_head_t drain_complete;
  381. bool read_active;
  382. bool write_active;
  383. struct work_struct work;
  384. struct touchpad_info_protocol rcvd_tp_info;
  385. struct dentry *debugfs_root;
  386. bool debug_tp_dim;
  387. char tp_dim_val[40];
  388. int tp_dim_min_x;
  389. int tp_dim_max_x;
  390. int tp_dim_min_y;
  391. int tp_dim_max_y;
  392. };
  393. static const unsigned char applespi_scancodes[] = {
  394. 0, 0, 0, 0,
  395. KEY_A, KEY_B, KEY_C, KEY_D, KEY_E, KEY_F, KEY_G, KEY_H, KEY_I, KEY_J,
  396. KEY_K, KEY_L, KEY_M, KEY_N, KEY_O, KEY_P, KEY_Q, KEY_R, KEY_S, KEY_T,
  397. KEY_U, KEY_V, KEY_W, KEY_X, KEY_Y, KEY_Z,
  398. KEY_1, KEY_2, KEY_3, KEY_4, KEY_5, KEY_6, KEY_7, KEY_8, KEY_9, KEY_0,
  399. KEY_ENTER, KEY_ESC, KEY_BACKSPACE, KEY_TAB, KEY_SPACE, KEY_MINUS,
  400. KEY_EQUAL, KEY_LEFTBRACE, KEY_RIGHTBRACE, KEY_BACKSLASH, 0,
  401. KEY_SEMICOLON, KEY_APOSTROPHE, KEY_GRAVE, KEY_COMMA, KEY_DOT, KEY_SLASH,
  402. KEY_CAPSLOCK,
  403. KEY_F1, KEY_F2, KEY_F3, KEY_F4, KEY_F5, KEY_F6, KEY_F7, KEY_F8, KEY_F9,
  404. KEY_F10, KEY_F11, KEY_F12, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  405. KEY_RIGHT, KEY_LEFT, KEY_DOWN, KEY_UP,
  406. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, KEY_102ND,
  407. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  408. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, KEY_RO, 0, KEY_YEN, 0, 0, 0, 0, 0,
  409. 0, KEY_KATAKANAHIRAGANA, KEY_MUHENKAN
  410. };
  411. /*
  412. * This must have exactly as many entries as there are bits in
  413. * struct keyboard_protocol.modifiers .
  414. */
  415. static const unsigned char applespi_controlcodes[] = {
  416. KEY_LEFTCTRL,
  417. KEY_LEFTSHIFT,
  418. KEY_LEFTALT,
  419. KEY_LEFTMETA,
  420. 0,
  421. KEY_RIGHTSHIFT,
  422. KEY_RIGHTALT,
  423. KEY_RIGHTMETA
  424. };
  425. struct applespi_key_translation {
  426. u16 from;
  427. u16 to;
  428. u8 flags;
  429. };
  430. static const struct applespi_key_translation applespi_fn_codes[] = {
  431. { KEY_BACKSPACE, KEY_DELETE },
  432. { KEY_ENTER, KEY_INSERT },
  433. { KEY_F1, KEY_BRIGHTNESSDOWN, APPLE_FLAG_FKEY },
  434. { KEY_F2, KEY_BRIGHTNESSUP, APPLE_FLAG_FKEY },
  435. { KEY_F3, KEY_SCALE, APPLE_FLAG_FKEY },
  436. { KEY_F4, KEY_DASHBOARD, APPLE_FLAG_FKEY },
  437. { KEY_F5, KEY_KBDILLUMDOWN, APPLE_FLAG_FKEY },
  438. { KEY_F6, KEY_KBDILLUMUP, APPLE_FLAG_FKEY },
  439. { KEY_F7, KEY_PREVIOUSSONG, APPLE_FLAG_FKEY },
  440. { KEY_F8, KEY_PLAYPAUSE, APPLE_FLAG_FKEY },
  441. { KEY_F9, KEY_NEXTSONG, APPLE_FLAG_FKEY },
  442. { KEY_F10, KEY_MUTE, APPLE_FLAG_FKEY },
  443. { KEY_F11, KEY_VOLUMEDOWN, APPLE_FLAG_FKEY },
  444. { KEY_F12, KEY_VOLUMEUP, APPLE_FLAG_FKEY },
  445. { KEY_RIGHT, KEY_END },
  446. { KEY_LEFT, KEY_HOME },
  447. { KEY_DOWN, KEY_PAGEDOWN },
  448. { KEY_UP, KEY_PAGEUP },
  449. { }
  450. };
  451. static const struct applespi_key_translation apple_iso_keyboard[] = {
  452. { KEY_GRAVE, KEY_102ND },
  453. { KEY_102ND, KEY_GRAVE },
  454. { }
  455. };
  456. struct applespi_tp_model_info {
  457. u16 model;
  458. struct applespi_tp_info tp_info;
  459. };
  460. static const struct applespi_tp_model_info applespi_tp_models[] = {
  461. {
  462. .model = 0x04, /* MB8 MB9 MB10 */
  463. .tp_info = { -5087, -182, 5579, 6089 },
  464. },
  465. {
  466. .model = 0x05, /* MBP13,1 MBP13,2 MBP14,1 MBP14,2 */
  467. .tp_info = { -6243, -170, 6749, 7685 },
  468. },
  469. {
  470. .model = 0x06, /* MBP13,3 MBP14,3 */
  471. .tp_info = { -7456, -163, 7976, 9283 },
  472. },
  473. {}
  474. };
  475. typedef void (*applespi_trace_fun)(enum applespi_evt_type,
  476. enum applespi_pkt_type, u8 *, size_t);
  477. static applespi_trace_fun applespi_get_trace_fun(enum applespi_evt_type type)
  478. {
  479. switch (type) {
  480. case ET_CMD_TP_INI:
  481. return trace_applespi_tp_ini_cmd;
  482. case ET_CMD_BL:
  483. return trace_applespi_backlight_cmd;
  484. case ET_CMD_CL:
  485. return trace_applespi_caps_lock_cmd;
  486. case ET_RD_KEYB:
  487. return trace_applespi_keyboard_data;
  488. case ET_RD_TPAD:
  489. return trace_applespi_touchpad_data;
  490. case ET_RD_UNKN:
  491. return trace_applespi_unknown_data;
  492. default:
  493. WARN_ONCE(1, "Unknown msg type %d", type);
  494. return trace_applespi_unknown_data;
  495. }
  496. }
  497. static void applespi_setup_read_txfrs(struct applespi_data *applespi)
  498. {
  499. struct spi_message *msg = &applespi->rd_m;
  500. struct spi_transfer *dl_t = &applespi->dl_t;
  501. struct spi_transfer *rd_t = &applespi->rd_t;
  502. memset(dl_t, 0, sizeof(*dl_t));
  503. memset(rd_t, 0, sizeof(*rd_t));
  504. dl_t->delay.value = applespi->spi_settings.spi_cs_delay;
  505. dl_t->delay.unit = SPI_DELAY_UNIT_USECS;
  506. rd_t->rx_buf = applespi->rx_buffer;
  507. rd_t->len = APPLESPI_PACKET_SIZE;
  508. spi_message_init(msg);
  509. spi_message_add_tail(dl_t, msg);
  510. spi_message_add_tail(rd_t, msg);
  511. }
  512. static void applespi_setup_write_txfrs(struct applespi_data *applespi)
  513. {
  514. struct spi_message *msg = &applespi->wr_m;
  515. struct spi_transfer *wt_t = &applespi->ww_t;
  516. struct spi_transfer *dl_t = &applespi->wd_t;
  517. struct spi_transfer *wr_t = &applespi->wr_t;
  518. struct spi_transfer *st_t = &applespi->st_t;
  519. memset(wt_t, 0, sizeof(*wt_t));
  520. memset(dl_t, 0, sizeof(*dl_t));
  521. memset(wr_t, 0, sizeof(*wr_t));
  522. memset(st_t, 0, sizeof(*st_t));
  523. /*
  524. * All we need here is a delay at the beginning of the message before
  525. * asserting cs. But the current spi API doesn't support this, so we
  526. * end up with an extra unnecessary (but harmless) cs assertion and
  527. * deassertion.
  528. */
  529. wt_t->delay.value = SPI_RW_CHG_DELAY_US;
  530. wt_t->delay.unit = SPI_DELAY_UNIT_USECS;
  531. wt_t->cs_change = 1;
  532. dl_t->delay.value = applespi->spi_settings.spi_cs_delay;
  533. dl_t->delay.unit = SPI_DELAY_UNIT_USECS;
  534. wr_t->tx_buf = applespi->tx_buffer;
  535. wr_t->len = APPLESPI_PACKET_SIZE;
  536. wr_t->delay.value = SPI_RW_CHG_DELAY_US;
  537. wr_t->delay.unit = SPI_DELAY_UNIT_USECS;
  538. st_t->rx_buf = applespi->tx_status;
  539. st_t->len = APPLESPI_STATUS_SIZE;
  540. spi_message_init(msg);
  541. spi_message_add_tail(wt_t, msg);
  542. spi_message_add_tail(dl_t, msg);
  543. spi_message_add_tail(wr_t, msg);
  544. spi_message_add_tail(st_t, msg);
  545. }
  546. static int applespi_async(struct applespi_data *applespi,
  547. struct spi_message *message, void (*complete)(void *))
  548. {
  549. message->complete = complete;
  550. message->context = applespi;
  551. return spi_async(applespi->spi, message);
  552. }
  553. static inline bool applespi_check_write_status(struct applespi_data *applespi,
  554. int sts)
  555. {
  556. static u8 status_ok[] = { 0xac, 0x27, 0x68, 0xd5 };
  557. if (sts < 0) {
  558. dev_warn(&applespi->spi->dev, "Error writing to device: %d\n",
  559. sts);
  560. return false;
  561. }
  562. if (memcmp(applespi->tx_status, status_ok, APPLESPI_STATUS_SIZE)) {
  563. dev_warn(&applespi->spi->dev, "Error writing to device: %*ph\n",
  564. APPLESPI_STATUS_SIZE, applespi->tx_status);
  565. return false;
  566. }
  567. return true;
  568. }
  569. static int applespi_get_spi_settings(struct applespi_data *applespi)
  570. {
  571. struct acpi_device *adev = ACPI_COMPANION(&applespi->spi->dev);
  572. const union acpi_object *o;
  573. struct spi_settings *settings = &applespi->spi_settings;
  574. if (!acpi_dev_get_property(adev, "spiCSDelay", ACPI_TYPE_BUFFER, &o))
  575. settings->spi_cs_delay = *(u64 *)o->buffer.pointer;
  576. else
  577. dev_warn(&applespi->spi->dev,
  578. "Property spiCSDelay not found\n");
  579. if (!acpi_dev_get_property(adev, "resetA2RUsec", ACPI_TYPE_BUFFER, &o))
  580. settings->reset_a2r_usec = *(u64 *)o->buffer.pointer;
  581. else
  582. dev_warn(&applespi->spi->dev,
  583. "Property resetA2RUsec not found\n");
  584. if (!acpi_dev_get_property(adev, "resetRecUsec", ACPI_TYPE_BUFFER, &o))
  585. settings->reset_rec_usec = *(u64 *)o->buffer.pointer;
  586. else
  587. dev_warn(&applespi->spi->dev,
  588. "Property resetRecUsec not found\n");
  589. dev_dbg(&applespi->spi->dev,
  590. "SPI settings: spi_cs_delay=%llu reset_a2r_usec=%llu reset_rec_usec=%llu\n",
  591. settings->spi_cs_delay, settings->reset_a2r_usec,
  592. settings->reset_rec_usec);
  593. return 0;
  594. }
  595. static int applespi_setup_spi(struct applespi_data *applespi)
  596. {
  597. int sts;
  598. sts = applespi_get_spi_settings(applespi);
  599. if (sts)
  600. return sts;
  601. spin_lock_init(&applespi->cmd_msg_lock);
  602. init_waitqueue_head(&applespi->drain_complete);
  603. return 0;
  604. }
  605. static int applespi_enable_spi(struct applespi_data *applespi)
  606. {
  607. acpi_status acpi_sts;
  608. unsigned long long spi_status;
  609. /* check if SPI is already enabled, so we can skip the delay below */
  610. acpi_sts = acpi_evaluate_integer(applespi->sist, NULL, NULL,
  611. &spi_status);
  612. if (ACPI_SUCCESS(acpi_sts) && spi_status)
  613. return 0;
  614. /* SIEN(1) will enable SPI communication */
  615. acpi_sts = acpi_execute_simple_method(applespi->sien, NULL, 1);
  616. if (ACPI_FAILURE(acpi_sts)) {
  617. dev_err(&applespi->spi->dev, "SIEN failed: %s\n",
  618. acpi_format_exception(acpi_sts));
  619. return -ENODEV;
  620. }
  621. /*
  622. * Allow the SPI interface to come up before returning. Without this
  623. * delay, the SPI commands to enable multitouch mode may not reach
  624. * the trackpad controller, causing pointer movement to break upon
  625. * resume from sleep.
  626. */
  627. msleep(50);
  628. return 0;
  629. }
  630. static int applespi_send_cmd_msg(struct applespi_data *applespi);
  631. static void applespi_msg_complete(struct applespi_data *applespi,
  632. bool is_write_msg, bool is_read_compl)
  633. {
  634. unsigned long flags;
  635. spin_lock_irqsave(&applespi->cmd_msg_lock, flags);
  636. if (is_read_compl)
  637. applespi->read_active = false;
  638. if (is_write_msg)
  639. applespi->write_active = false;
  640. if (applespi->drain && !applespi->write_active)
  641. wake_up_all(&applespi->drain_complete);
  642. if (is_write_msg) {
  643. applespi->cmd_msg_queued = 0;
  644. applespi_send_cmd_msg(applespi);
  645. }
  646. spin_unlock_irqrestore(&applespi->cmd_msg_lock, flags);
  647. }
  648. static void applespi_async_write_complete(void *context)
  649. {
  650. struct applespi_data *applespi = context;
  651. enum applespi_evt_type evt_type = applespi->cmd_evt_type;
  652. applespi_get_trace_fun(evt_type)(evt_type, PT_WRITE,
  653. applespi->tx_buffer,
  654. APPLESPI_PACKET_SIZE);
  655. applespi_get_trace_fun(evt_type)(evt_type, PT_STATUS,
  656. applespi->tx_status,
  657. APPLESPI_STATUS_SIZE);
  658. udelay(SPI_RW_CHG_DELAY_US);
  659. if (!applespi_check_write_status(applespi, applespi->wr_m.status)) {
  660. /*
  661. * If we got an error, we presumably won't get the expected
  662. * response message either.
  663. */
  664. applespi_msg_complete(applespi, true, false);
  665. }
  666. }
  667. static int applespi_send_cmd_msg(struct applespi_data *applespi)
  668. {
  669. u16 crc;
  670. int sts;
  671. struct spi_packet *packet = (struct spi_packet *)applespi->tx_buffer;
  672. struct message *message = (struct message *)packet->data;
  673. u16 msg_len;
  674. u8 device;
  675. /* check if draining */
  676. if (applespi->drain)
  677. return 0;
  678. /* check whether send is in progress */
  679. if (applespi->cmd_msg_queued) {
  680. if (ktime_ms_delta(ktime_get(), applespi->cmd_msg_queued) < 1000)
  681. return 0;
  682. dev_warn(&applespi->spi->dev, "Command %d timed out\n",
  683. applespi->cmd_evt_type);
  684. applespi->cmd_msg_queued = 0;
  685. applespi->write_active = false;
  686. }
  687. /* set up packet */
  688. memset(packet, 0, APPLESPI_PACKET_SIZE);
  689. /* are we processing init commands? */
  690. if (applespi->want_tp_info_cmd) {
  691. applespi->want_tp_info_cmd = false;
  692. applespi->want_mt_init_cmd = true;
  693. applespi->cmd_evt_type = ET_CMD_TP_INI;
  694. /* build init command */
  695. device = PACKET_DEV_INFO;
  696. message->type = cpu_to_le16(0x1020);
  697. msg_len = sizeof(message->tp_info_command);
  698. message->zero = 0x02;
  699. message->rsp_buf_len = cpu_to_le16(0x0200);
  700. } else if (applespi->want_mt_init_cmd) {
  701. applespi->want_mt_init_cmd = false;
  702. applespi->cmd_evt_type = ET_CMD_TP_INI;
  703. /* build init command */
  704. device = PACKET_DEV_TPAD;
  705. message->type = cpu_to_le16(0x0252);
  706. msg_len = sizeof(message->init_mt_command);
  707. message->init_mt_command.cmd = cpu_to_le16(0x0102);
  708. /* do we need caps-lock command? */
  709. } else if (applespi->want_cl_led_on != applespi->have_cl_led_on) {
  710. applespi->have_cl_led_on = applespi->want_cl_led_on;
  711. applespi->cmd_evt_type = ET_CMD_CL;
  712. /* build led command */
  713. device = PACKET_DEV_KEYB;
  714. message->type = cpu_to_le16(0x0151);
  715. msg_len = sizeof(message->capsl_command);
  716. message->capsl_command.unknown = 0x01;
  717. message->capsl_command.led = applespi->have_cl_led_on ? 2 : 0;
  718. /* do we need backlight command? */
  719. } else if (applespi->want_bl_level != applespi->have_bl_level) {
  720. applespi->have_bl_level = applespi->want_bl_level;
  721. applespi->cmd_evt_type = ET_CMD_BL;
  722. /* build command buffer */
  723. device = PACKET_DEV_KEYB;
  724. message->type = cpu_to_le16(0xB051);
  725. msg_len = sizeof(message->bl_command);
  726. message->bl_command.const1 = cpu_to_le16(0x01B0);
  727. message->bl_command.level =
  728. cpu_to_le16(applespi->have_bl_level);
  729. if (applespi->have_bl_level > 0)
  730. message->bl_command.const2 = cpu_to_le16(0x01F4);
  731. else
  732. message->bl_command.const2 = cpu_to_le16(0x0001);
  733. /* everything's up-to-date */
  734. } else {
  735. return 0;
  736. }
  737. /* finalize packet */
  738. packet->flags = PACKET_TYPE_WRITE;
  739. packet->device = device;
  740. packet->length = cpu_to_le16(MSG_HEADER_SIZE + msg_len);
  741. message->counter = applespi->cmd_msg_cntr++ % (U8_MAX + 1);
  742. message->length = cpu_to_le16(msg_len - 2);
  743. if (!message->rsp_buf_len)
  744. message->rsp_buf_len = message->length;
  745. crc = crc16(0, (u8 *)message, le16_to_cpu(packet->length) - 2);
  746. put_unaligned_le16(crc, &message->data[msg_len - 2]);
  747. crc = crc16(0, (u8 *)packet, sizeof(*packet) - 2);
  748. packet->crc16 = cpu_to_le16(crc);
  749. /* send command */
  750. sts = applespi_async(applespi, &applespi->wr_m,
  751. applespi_async_write_complete);
  752. if (sts) {
  753. dev_warn(&applespi->spi->dev,
  754. "Error queueing async write to device: %d\n", sts);
  755. return sts;
  756. }
  757. applespi->cmd_msg_queued = ktime_get_coarse();
  758. applespi->write_active = true;
  759. return 0;
  760. }
  761. static void applespi_init(struct applespi_data *applespi, bool is_resume)
  762. {
  763. unsigned long flags;
  764. spin_lock_irqsave(&applespi->cmd_msg_lock, flags);
  765. if (is_resume)
  766. applespi->want_mt_init_cmd = true;
  767. else
  768. applespi->want_tp_info_cmd = true;
  769. applespi_send_cmd_msg(applespi);
  770. spin_unlock_irqrestore(&applespi->cmd_msg_lock, flags);
  771. }
  772. static int applespi_set_capsl_led(struct applespi_data *applespi,
  773. bool capslock_on)
  774. {
  775. unsigned long flags;
  776. int sts;
  777. spin_lock_irqsave(&applespi->cmd_msg_lock, flags);
  778. applespi->want_cl_led_on = capslock_on;
  779. sts = applespi_send_cmd_msg(applespi);
  780. spin_unlock_irqrestore(&applespi->cmd_msg_lock, flags);
  781. return sts;
  782. }
  783. static void applespi_set_bl_level(struct led_classdev *led_cdev,
  784. enum led_brightness value)
  785. {
  786. struct applespi_data *applespi =
  787. container_of(led_cdev, struct applespi_data, backlight_info);
  788. unsigned long flags;
  789. spin_lock_irqsave(&applespi->cmd_msg_lock, flags);
  790. if (value == 0) {
  791. applespi->want_bl_level = value;
  792. } else {
  793. /*
  794. * The backlight does not turn on till level 32, so we scale
  795. * the range here so that from a user's perspective it turns
  796. * on at 1.
  797. */
  798. applespi->want_bl_level =
  799. ((value * KBD_BL_LEVEL_ADJ) / KBD_BL_LEVEL_SCALE +
  800. KBD_BL_LEVEL_MIN);
  801. }
  802. applespi_send_cmd_msg(applespi);
  803. spin_unlock_irqrestore(&applespi->cmd_msg_lock, flags);
  804. }
  805. static int applespi_event(struct input_dev *dev, unsigned int type,
  806. unsigned int code, int value)
  807. {
  808. struct applespi_data *applespi = input_get_drvdata(dev);
  809. switch (type) {
  810. case EV_LED:
  811. applespi_set_capsl_led(applespi, !!test_bit(LED_CAPSL, dev->led));
  812. return 0;
  813. }
  814. return -EINVAL;
  815. }
  816. /* lifted from the BCM5974 driver and renamed from raw2int */
  817. /* convert 16-bit little endian to signed integer */
  818. static inline int le16_to_int(__le16 x)
  819. {
  820. return (signed short)le16_to_cpu(x);
  821. }
  822. static void applespi_debug_update_dimensions(struct applespi_data *applespi,
  823. const struct tp_finger *f)
  824. {
  825. applespi->tp_dim_min_x = min(applespi->tp_dim_min_x,
  826. le16_to_int(f->abs_x));
  827. applespi->tp_dim_max_x = max(applespi->tp_dim_max_x,
  828. le16_to_int(f->abs_x));
  829. applespi->tp_dim_min_y = min(applespi->tp_dim_min_y,
  830. le16_to_int(f->abs_y));
  831. applespi->tp_dim_max_y = max(applespi->tp_dim_max_y,
  832. le16_to_int(f->abs_y));
  833. }
  834. static int applespi_tp_dim_open(struct inode *inode, struct file *file)
  835. {
  836. struct applespi_data *applespi = inode->i_private;
  837. file->private_data = applespi;
  838. snprintf(applespi->tp_dim_val, sizeof(applespi->tp_dim_val),
  839. "0x%.4x %dx%d+%u+%u\n",
  840. applespi->touchpad_input_dev->id.product,
  841. applespi->tp_dim_min_x, applespi->tp_dim_min_y,
  842. applespi->tp_dim_max_x - applespi->tp_dim_min_x,
  843. applespi->tp_dim_max_y - applespi->tp_dim_min_y);
  844. return nonseekable_open(inode, file);
  845. }
  846. static ssize_t applespi_tp_dim_read(struct file *file, char __user *buf,
  847. size_t len, loff_t *off)
  848. {
  849. struct applespi_data *applespi = file->private_data;
  850. return simple_read_from_buffer(buf, len, off, applespi->tp_dim_val,
  851. strlen(applespi->tp_dim_val));
  852. }
  853. static const struct file_operations applespi_tp_dim_fops = {
  854. .owner = THIS_MODULE,
  855. .open = applespi_tp_dim_open,
  856. .read = applespi_tp_dim_read,
  857. };
  858. static void report_finger_data(struct input_dev *input, int slot,
  859. const struct input_mt_pos *pos,
  860. const struct tp_finger *f)
  861. {
  862. input_mt_slot(input, slot);
  863. input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
  864. input_report_abs(input, ABS_MT_TOUCH_MAJOR,
  865. le16_to_int(f->touch_major) << 1);
  866. input_report_abs(input, ABS_MT_TOUCH_MINOR,
  867. le16_to_int(f->touch_minor) << 1);
  868. input_report_abs(input, ABS_MT_WIDTH_MAJOR,
  869. le16_to_int(f->tool_major) << 1);
  870. input_report_abs(input, ABS_MT_WIDTH_MINOR,
  871. le16_to_int(f->tool_minor) << 1);
  872. input_report_abs(input, ABS_MT_ORIENTATION,
  873. MAX_FINGER_ORIENTATION - le16_to_int(f->orientation));
  874. input_report_abs(input, ABS_MT_POSITION_X, pos->x);
  875. input_report_abs(input, ABS_MT_POSITION_Y, pos->y);
  876. }
  877. static void report_tp_state(struct applespi_data *applespi,
  878. struct touchpad_protocol *t)
  879. {
  880. const struct tp_finger *f;
  881. struct input_dev *input;
  882. const struct applespi_tp_info *tp_info = &applespi->tp_info;
  883. int i, n;
  884. /* touchpad_input_dev is set async in worker */
  885. input = smp_load_acquire(&applespi->touchpad_input_dev);
  886. if (!input)
  887. return; /* touchpad isn't initialized yet */
  888. n = 0;
  889. for (i = 0; i < t->number_of_fingers; i++) {
  890. f = &t->fingers[i];
  891. if (le16_to_int(f->touch_major) == 0)
  892. continue;
  893. applespi->pos[n].x = le16_to_int(f->abs_x);
  894. applespi->pos[n].y = tp_info->y_min + tp_info->y_max -
  895. le16_to_int(f->abs_y);
  896. n++;
  897. if (applespi->debug_tp_dim)
  898. applespi_debug_update_dimensions(applespi, f);
  899. }
  900. input_mt_assign_slots(input, applespi->slots, applespi->pos, n, 0);
  901. for (i = 0; i < n; i++)
  902. report_finger_data(input, applespi->slots[i],
  903. &applespi->pos[i], &t->fingers[i]);
  904. input_mt_sync_frame(input);
  905. input_report_key(input, BTN_LEFT, t->clicked);
  906. input_sync(input);
  907. }
  908. static const struct applespi_key_translation *
  909. applespi_find_translation(const struct applespi_key_translation *table, u16 key)
  910. {
  911. const struct applespi_key_translation *trans;
  912. for (trans = table; trans->from; trans++)
  913. if (trans->from == key)
  914. return trans;
  915. return NULL;
  916. }
  917. static unsigned int applespi_translate_fn_key(unsigned int key, int fn_pressed)
  918. {
  919. const struct applespi_key_translation *trans;
  920. int do_translate;
  921. trans = applespi_find_translation(applespi_fn_codes, key);
  922. if (trans) {
  923. if (trans->flags & APPLE_FLAG_FKEY)
  924. do_translate = (fnmode == 2 && fn_pressed) ||
  925. (fnmode == 1 && !fn_pressed);
  926. else
  927. do_translate = fn_pressed;
  928. if (do_translate)
  929. key = trans->to;
  930. }
  931. return key;
  932. }
  933. static unsigned int applespi_translate_iso_layout(unsigned int key)
  934. {
  935. const struct applespi_key_translation *trans;
  936. trans = applespi_find_translation(apple_iso_keyboard, key);
  937. if (trans)
  938. key = trans->to;
  939. return key;
  940. }
  941. static unsigned int applespi_code_to_key(u8 code, int fn_pressed)
  942. {
  943. unsigned int key = applespi_scancodes[code];
  944. if (fnmode)
  945. key = applespi_translate_fn_key(key, fn_pressed);
  946. if (iso_layout)
  947. key = applespi_translate_iso_layout(key);
  948. return key;
  949. }
  950. static void
  951. applespi_remap_fn_key(struct keyboard_protocol *keyboard_protocol)
  952. {
  953. unsigned char tmp;
  954. u8 bit = BIT((fnremap - 1) & 0x07);
  955. if (!fnremap || fnremap > ARRAY_SIZE(applespi_controlcodes) ||
  956. !applespi_controlcodes[fnremap - 1])
  957. return;
  958. tmp = keyboard_protocol->fn_pressed;
  959. keyboard_protocol->fn_pressed = !!(keyboard_protocol->modifiers & bit);
  960. if (tmp)
  961. keyboard_protocol->modifiers |= bit;
  962. else
  963. keyboard_protocol->modifiers &= ~bit;
  964. }
  965. static void
  966. applespi_handle_keyboard_event(struct applespi_data *applespi,
  967. struct keyboard_protocol *keyboard_protocol)
  968. {
  969. unsigned int key;
  970. int i;
  971. compiletime_assert(ARRAY_SIZE(applespi_controlcodes) ==
  972. sizeof_field(struct keyboard_protocol, modifiers) * 8,
  973. "applespi_controlcodes has wrong number of entries");
  974. /* check for rollover overflow, which is signalled by all keys == 1 */
  975. if (!memchr_inv(keyboard_protocol->keys_pressed, 1, MAX_ROLLOVER))
  976. return;
  977. /* remap fn key if desired */
  978. applespi_remap_fn_key(keyboard_protocol);
  979. /* check released keys */
  980. for (i = 0; i < MAX_ROLLOVER; i++) {
  981. if (memchr(keyboard_protocol->keys_pressed,
  982. applespi->last_keys_pressed[i], MAX_ROLLOVER))
  983. continue; /* key is still pressed */
  984. key = applespi_code_to_key(applespi->last_keys_pressed[i],
  985. applespi->last_keys_fn_pressed[i]);
  986. input_report_key(applespi->keyboard_input_dev, key, 0);
  987. applespi->last_keys_fn_pressed[i] = 0;
  988. }
  989. /* check pressed keys */
  990. for (i = 0; i < MAX_ROLLOVER; i++) {
  991. if (keyboard_protocol->keys_pressed[i] <
  992. ARRAY_SIZE(applespi_scancodes) &&
  993. keyboard_protocol->keys_pressed[i] > 0) {
  994. key = applespi_code_to_key(
  995. keyboard_protocol->keys_pressed[i],
  996. keyboard_protocol->fn_pressed);
  997. input_report_key(applespi->keyboard_input_dev, key, 1);
  998. applespi->last_keys_fn_pressed[i] =
  999. keyboard_protocol->fn_pressed;
  1000. }
  1001. }
  1002. /* check control keys */
  1003. for (i = 0; i < ARRAY_SIZE(applespi_controlcodes); i++) {
  1004. if (keyboard_protocol->modifiers & BIT(i))
  1005. input_report_key(applespi->keyboard_input_dev,
  1006. applespi_controlcodes[i], 1);
  1007. else
  1008. input_report_key(applespi->keyboard_input_dev,
  1009. applespi_controlcodes[i], 0);
  1010. }
  1011. /* check function key */
  1012. if (keyboard_protocol->fn_pressed && !applespi->last_fn_pressed)
  1013. input_report_key(applespi->keyboard_input_dev, KEY_FN, 1);
  1014. else if (!keyboard_protocol->fn_pressed && applespi->last_fn_pressed)
  1015. input_report_key(applespi->keyboard_input_dev, KEY_FN, 0);
  1016. applespi->last_fn_pressed = keyboard_protocol->fn_pressed;
  1017. /* done */
  1018. input_sync(applespi->keyboard_input_dev);
  1019. memcpy(&applespi->last_keys_pressed, keyboard_protocol->keys_pressed,
  1020. sizeof(applespi->last_keys_pressed));
  1021. }
  1022. static const struct applespi_tp_info *applespi_find_touchpad_info(u8 model)
  1023. {
  1024. const struct applespi_tp_model_info *info;
  1025. for (info = applespi_tp_models; info->model; info++) {
  1026. if (info->model == model)
  1027. return &info->tp_info;
  1028. }
  1029. return NULL;
  1030. }
  1031. static int
  1032. applespi_register_touchpad_device(struct applespi_data *applespi,
  1033. struct touchpad_info_protocol *rcvd_tp_info)
  1034. {
  1035. const struct applespi_tp_info *tp_info;
  1036. struct input_dev *touchpad_input_dev;
  1037. int sts;
  1038. /* set up touchpad dimensions */
  1039. tp_info = applespi_find_touchpad_info(rcvd_tp_info->model_no);
  1040. if (!tp_info) {
  1041. dev_warn(&applespi->spi->dev,
  1042. "Unknown touchpad model %x - falling back to MB8 touchpad\n",
  1043. rcvd_tp_info->model_no);
  1044. tp_info = &applespi_tp_models[0].tp_info;
  1045. }
  1046. applespi->tp_info = *tp_info;
  1047. if (touchpad_dimensions[0]) {
  1048. int x, y, w, h;
  1049. sts = sscanf(touchpad_dimensions, "%dx%d+%u+%u", &x, &y, &w, &h);
  1050. if (sts == 4) {
  1051. dev_info(&applespi->spi->dev,
  1052. "Overriding touchpad dimensions from module param\n");
  1053. applespi->tp_info.x_min = x;
  1054. applespi->tp_info.y_min = y;
  1055. applespi->tp_info.x_max = x + w;
  1056. applespi->tp_info.y_max = y + h;
  1057. } else {
  1058. dev_warn(&applespi->spi->dev,
  1059. "Invalid touchpad dimensions '%s': must be in the form XxY+W+H\n",
  1060. touchpad_dimensions);
  1061. touchpad_dimensions[0] = '\0';
  1062. }
  1063. }
  1064. if (!touchpad_dimensions[0]) {
  1065. snprintf(touchpad_dimensions, sizeof(touchpad_dimensions),
  1066. "%dx%d+%u+%u",
  1067. applespi->tp_info.x_min,
  1068. applespi->tp_info.y_min,
  1069. applespi->tp_info.x_max - applespi->tp_info.x_min,
  1070. applespi->tp_info.y_max - applespi->tp_info.y_min);
  1071. }
  1072. /* create touchpad input device */
  1073. touchpad_input_dev = devm_input_allocate_device(&applespi->spi->dev);
  1074. if (!touchpad_input_dev) {
  1075. dev_err(&applespi->spi->dev,
  1076. "Failed to allocate touchpad input device\n");
  1077. return -ENOMEM;
  1078. }
  1079. touchpad_input_dev->name = "Apple SPI Touchpad";
  1080. touchpad_input_dev->phys = "applespi/input1";
  1081. touchpad_input_dev->dev.parent = &applespi->spi->dev;
  1082. touchpad_input_dev->id.bustype = BUS_SPI;
  1083. touchpad_input_dev->id.vendor = SYNAPTICS_VENDOR_ID;
  1084. touchpad_input_dev->id.product =
  1085. rcvd_tp_info->model_no << 8 | rcvd_tp_info->model_flags;
  1086. /* basic properties */
  1087. input_set_capability(touchpad_input_dev, EV_REL, REL_X);
  1088. input_set_capability(touchpad_input_dev, EV_REL, REL_Y);
  1089. __set_bit(INPUT_PROP_POINTER, touchpad_input_dev->propbit);
  1090. __set_bit(INPUT_PROP_BUTTONPAD, touchpad_input_dev->propbit);
  1091. /* finger touch area */
  1092. input_set_abs_params(touchpad_input_dev, ABS_MT_TOUCH_MAJOR,
  1093. 0, 5000, 0, 0);
  1094. input_set_abs_params(touchpad_input_dev, ABS_MT_TOUCH_MINOR,
  1095. 0, 5000, 0, 0);
  1096. /* finger approach area */
  1097. input_set_abs_params(touchpad_input_dev, ABS_MT_WIDTH_MAJOR,
  1098. 0, 5000, 0, 0);
  1099. input_set_abs_params(touchpad_input_dev, ABS_MT_WIDTH_MINOR,
  1100. 0, 5000, 0, 0);
  1101. /* finger orientation */
  1102. input_set_abs_params(touchpad_input_dev, ABS_MT_ORIENTATION,
  1103. -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION,
  1104. 0, 0);
  1105. /* finger position */
  1106. input_set_abs_params(touchpad_input_dev, ABS_MT_POSITION_X,
  1107. applespi->tp_info.x_min, applespi->tp_info.x_max,
  1108. 0, 0);
  1109. input_set_abs_params(touchpad_input_dev, ABS_MT_POSITION_Y,
  1110. applespi->tp_info.y_min, applespi->tp_info.y_max,
  1111. 0, 0);
  1112. /* touchpad button */
  1113. input_set_capability(touchpad_input_dev, EV_KEY, BTN_LEFT);
  1114. /* multitouch */
  1115. sts = input_mt_init_slots(touchpad_input_dev, MAX_FINGERS,
  1116. INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
  1117. INPUT_MT_TRACK);
  1118. if (sts) {
  1119. dev_err(&applespi->spi->dev,
  1120. "failed to initialize slots: %d", sts);
  1121. return sts;
  1122. }
  1123. /* register input device */
  1124. sts = input_register_device(touchpad_input_dev);
  1125. if (sts) {
  1126. dev_err(&applespi->spi->dev,
  1127. "Unable to register touchpad input device (%d)\n", sts);
  1128. return sts;
  1129. }
  1130. /* touchpad_input_dev is read async in spi callback */
  1131. smp_store_release(&applespi->touchpad_input_dev, touchpad_input_dev);
  1132. return 0;
  1133. }
  1134. static void applespi_worker(struct work_struct *work)
  1135. {
  1136. struct applespi_data *applespi =
  1137. container_of(work, struct applespi_data, work);
  1138. applespi_register_touchpad_device(applespi, &applespi->rcvd_tp_info);
  1139. }
  1140. static void applespi_handle_cmd_response(struct applespi_data *applespi,
  1141. struct spi_packet *packet,
  1142. struct message *message)
  1143. {
  1144. if (packet->device == PACKET_DEV_INFO &&
  1145. le16_to_cpu(message->type) == 0x1020) {
  1146. /*
  1147. * We're not allowed to sleep here, but registering an input
  1148. * device can sleep.
  1149. */
  1150. applespi->rcvd_tp_info = message->tp_info;
  1151. schedule_work(&applespi->work);
  1152. return;
  1153. }
  1154. if (le16_to_cpu(message->length) != 0x0000) {
  1155. dev_warn_ratelimited(&applespi->spi->dev,
  1156. "Received unexpected write response: length=%x\n",
  1157. le16_to_cpu(message->length));
  1158. return;
  1159. }
  1160. if (packet->device == PACKET_DEV_TPAD &&
  1161. le16_to_cpu(message->type) == 0x0252 &&
  1162. le16_to_cpu(message->rsp_buf_len) == 0x0002)
  1163. dev_info(&applespi->spi->dev, "modeswitch done.\n");
  1164. }
  1165. static bool applespi_verify_crc(struct applespi_data *applespi, u8 *buffer,
  1166. size_t buflen)
  1167. {
  1168. u16 crc;
  1169. crc = crc16(0, buffer, buflen);
  1170. if (crc) {
  1171. dev_warn_ratelimited(&applespi->spi->dev,
  1172. "Received corrupted packet (crc mismatch)\n");
  1173. trace_applespi_bad_crc(ET_RD_CRC, READ, buffer, buflen);
  1174. return false;
  1175. }
  1176. return true;
  1177. }
  1178. static void applespi_debug_print_read_packet(struct applespi_data *applespi,
  1179. struct spi_packet *packet)
  1180. {
  1181. unsigned int evt_type;
  1182. if (packet->flags == PACKET_TYPE_READ &&
  1183. packet->device == PACKET_DEV_KEYB)
  1184. evt_type = ET_RD_KEYB;
  1185. else if (packet->flags == PACKET_TYPE_READ &&
  1186. packet->device == PACKET_DEV_TPAD)
  1187. evt_type = ET_RD_TPAD;
  1188. else if (packet->flags == PACKET_TYPE_WRITE)
  1189. evt_type = applespi->cmd_evt_type;
  1190. else
  1191. evt_type = ET_RD_UNKN;
  1192. applespi_get_trace_fun(evt_type)(evt_type, PT_READ, applespi->rx_buffer,
  1193. APPLESPI_PACKET_SIZE);
  1194. }
  1195. static void applespi_got_data(struct applespi_data *applespi)
  1196. {
  1197. struct spi_packet *packet;
  1198. struct message *message;
  1199. unsigned int msg_len;
  1200. unsigned int off;
  1201. unsigned int rem;
  1202. unsigned int len;
  1203. /* process packet header */
  1204. if (!applespi_verify_crc(applespi, applespi->rx_buffer,
  1205. APPLESPI_PACKET_SIZE)) {
  1206. unsigned long flags;
  1207. spin_lock_irqsave(&applespi->cmd_msg_lock, flags);
  1208. if (applespi->drain) {
  1209. applespi->read_active = false;
  1210. applespi->write_active = false;
  1211. wake_up_all(&applespi->drain_complete);
  1212. }
  1213. spin_unlock_irqrestore(&applespi->cmd_msg_lock, flags);
  1214. return;
  1215. }
  1216. packet = (struct spi_packet *)applespi->rx_buffer;
  1217. applespi_debug_print_read_packet(applespi, packet);
  1218. off = le16_to_cpu(packet->offset);
  1219. rem = le16_to_cpu(packet->remaining);
  1220. len = le16_to_cpu(packet->length);
  1221. if (len > sizeof(packet->data)) {
  1222. dev_warn_ratelimited(&applespi->spi->dev,
  1223. "Received corrupted packet (invalid packet length %u)\n",
  1224. len);
  1225. goto msg_complete;
  1226. }
  1227. /* handle multi-packet messages */
  1228. if (rem > 0 || off > 0) {
  1229. if (off != applespi->saved_msg_len) {
  1230. dev_warn_ratelimited(&applespi->spi->dev,
  1231. "Received unexpected offset (got %u, expected %u)\n",
  1232. off, applespi->saved_msg_len);
  1233. goto msg_complete;
  1234. }
  1235. if (off + rem > MAX_PKTS_PER_MSG * APPLESPI_PACKET_SIZE) {
  1236. dev_warn_ratelimited(&applespi->spi->dev,
  1237. "Received message too large (size %u)\n",
  1238. off + rem);
  1239. goto msg_complete;
  1240. }
  1241. if (off + len > MAX_PKTS_PER_MSG * APPLESPI_PACKET_SIZE) {
  1242. dev_warn_ratelimited(&applespi->spi->dev,
  1243. "Received message too large (size %u)\n",
  1244. off + len);
  1245. goto msg_complete;
  1246. }
  1247. memcpy(applespi->msg_buf + off, &packet->data, len);
  1248. applespi->saved_msg_len += len;
  1249. if (rem > 0)
  1250. return;
  1251. message = (struct message *)applespi->msg_buf;
  1252. msg_len = applespi->saved_msg_len;
  1253. } else {
  1254. message = (struct message *)&packet->data;
  1255. msg_len = len;
  1256. }
  1257. /* got complete message - verify */
  1258. if (!applespi_verify_crc(applespi, (u8 *)message, msg_len))
  1259. goto msg_complete;
  1260. if (le16_to_cpu(message->length) != msg_len - MSG_HEADER_SIZE - 2) {
  1261. dev_warn_ratelimited(&applespi->spi->dev,
  1262. "Received corrupted packet (invalid message length %u - expected %u)\n",
  1263. le16_to_cpu(message->length),
  1264. msg_len - MSG_HEADER_SIZE - 2);
  1265. goto msg_complete;
  1266. }
  1267. /* handle message */
  1268. if (packet->flags == PACKET_TYPE_READ &&
  1269. packet->device == PACKET_DEV_KEYB) {
  1270. applespi_handle_keyboard_event(applespi, &message->keyboard);
  1271. } else if (packet->flags == PACKET_TYPE_READ &&
  1272. packet->device == PACKET_DEV_TPAD) {
  1273. struct touchpad_protocol *tp;
  1274. size_t tp_len;
  1275. tp = &message->touchpad;
  1276. tp_len = struct_size(tp, fingers, tp->number_of_fingers);
  1277. if (le16_to_cpu(message->length) + 2 != tp_len) {
  1278. dev_warn_ratelimited(&applespi->spi->dev,
  1279. "Received corrupted packet (invalid message length %u - num-fingers %u, tp-len %zu)\n",
  1280. le16_to_cpu(message->length),
  1281. tp->number_of_fingers, tp_len);
  1282. goto msg_complete;
  1283. }
  1284. if (tp->number_of_fingers > MAX_FINGERS) {
  1285. dev_warn_ratelimited(&applespi->spi->dev,
  1286. "Number of reported fingers (%u) exceeds max (%u))\n",
  1287. tp->number_of_fingers,
  1288. MAX_FINGERS);
  1289. tp->number_of_fingers = MAX_FINGERS;
  1290. }
  1291. report_tp_state(applespi, tp);
  1292. } else if (packet->flags == PACKET_TYPE_WRITE) {
  1293. applespi_handle_cmd_response(applespi, packet, message);
  1294. }
  1295. msg_complete:
  1296. applespi->saved_msg_len = 0;
  1297. applespi_msg_complete(applespi, packet->flags == PACKET_TYPE_WRITE,
  1298. true);
  1299. }
  1300. static void applespi_async_read_complete(void *context)
  1301. {
  1302. struct applespi_data *applespi = context;
  1303. if (applespi->rd_m.status < 0) {
  1304. dev_warn(&applespi->spi->dev, "Error reading from device: %d\n",
  1305. applespi->rd_m.status);
  1306. /*
  1307. * We don't actually know if this was a pure read, or a response
  1308. * to a write. But this is a rare error condition that should
  1309. * never occur, so clearing both flags to avoid deadlock.
  1310. */
  1311. applespi_msg_complete(applespi, true, true);
  1312. } else {
  1313. applespi_got_data(applespi);
  1314. }
  1315. acpi_finish_gpe(NULL, applespi->gpe);
  1316. }
  1317. static u32 applespi_notify(acpi_handle gpe_device, u32 gpe, void *context)
  1318. {
  1319. struct applespi_data *applespi = context;
  1320. int sts;
  1321. unsigned long flags;
  1322. trace_applespi_irq_received(ET_RD_IRQ, PT_READ);
  1323. spin_lock_irqsave(&applespi->cmd_msg_lock, flags);
  1324. if (!applespi->suspended) {
  1325. sts = applespi_async(applespi, &applespi->rd_m,
  1326. applespi_async_read_complete);
  1327. if (sts)
  1328. dev_warn(&applespi->spi->dev,
  1329. "Error queueing async read to device: %d\n",
  1330. sts);
  1331. else
  1332. applespi->read_active = true;
  1333. }
  1334. spin_unlock_irqrestore(&applespi->cmd_msg_lock, flags);
  1335. return ACPI_INTERRUPT_HANDLED;
  1336. }
  1337. static int applespi_get_saved_bl_level(struct applespi_data *applespi)
  1338. {
  1339. efi_status_t sts = EFI_NOT_FOUND;
  1340. u16 efi_data = 0;
  1341. unsigned long efi_data_len = sizeof(efi_data);
  1342. if (efi_rt_services_supported(EFI_RT_SUPPORTED_GET_VARIABLE))
  1343. sts = efi.get_variable(EFI_BL_LEVEL_NAME, &EFI_BL_LEVEL_GUID,
  1344. NULL, &efi_data_len, &efi_data);
  1345. if (sts != EFI_SUCCESS && sts != EFI_NOT_FOUND)
  1346. dev_warn(&applespi->spi->dev,
  1347. "Error getting backlight level from EFI vars: 0x%lx\n",
  1348. sts);
  1349. return sts != EFI_SUCCESS ? -ENODEV : efi_data;
  1350. }
  1351. static void applespi_save_bl_level(struct applespi_data *applespi,
  1352. unsigned int level)
  1353. {
  1354. efi_status_t sts = EFI_UNSUPPORTED;
  1355. u32 efi_attr;
  1356. u16 efi_data;
  1357. efi_data = (u16)level;
  1358. efi_attr = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS |
  1359. EFI_VARIABLE_RUNTIME_ACCESS;
  1360. if (efi_rt_services_supported(EFI_RT_SUPPORTED_SET_VARIABLE))
  1361. sts = efi.set_variable(EFI_BL_LEVEL_NAME, &EFI_BL_LEVEL_GUID,
  1362. efi_attr, sizeof(efi_data), &efi_data);
  1363. if (sts != EFI_SUCCESS)
  1364. dev_warn(&applespi->spi->dev,
  1365. "Error saving backlight level to EFI vars: 0x%lx\n", sts);
  1366. }
  1367. static int applespi_probe(struct spi_device *spi)
  1368. {
  1369. struct applespi_data *applespi;
  1370. acpi_handle spi_handle = ACPI_HANDLE(&spi->dev);
  1371. acpi_status acpi_sts;
  1372. int sts, i;
  1373. unsigned long long gpe, usb_status;
  1374. /* check if the USB interface is present and enabled already */
  1375. acpi_sts = acpi_evaluate_integer(spi_handle, "UIST", NULL, &usb_status);
  1376. if (ACPI_SUCCESS(acpi_sts) && usb_status) {
  1377. /* let the USB driver take over instead */
  1378. dev_info(&spi->dev, "USB interface already enabled\n");
  1379. return -ENODEV;
  1380. }
  1381. /* allocate driver data */
  1382. applespi = devm_kzalloc(&spi->dev, sizeof(*applespi), GFP_KERNEL);
  1383. if (!applespi)
  1384. return -ENOMEM;
  1385. applespi->spi = spi;
  1386. INIT_WORK(&applespi->work, applespi_worker);
  1387. /* store the driver data */
  1388. spi_set_drvdata(spi, applespi);
  1389. /* create our buffers */
  1390. applespi->tx_buffer = devm_kmalloc(&spi->dev, APPLESPI_PACKET_SIZE,
  1391. GFP_KERNEL);
  1392. applespi->tx_status = devm_kmalloc(&spi->dev, APPLESPI_STATUS_SIZE,
  1393. GFP_KERNEL);
  1394. applespi->rx_buffer = devm_kmalloc(&spi->dev, APPLESPI_PACKET_SIZE,
  1395. GFP_KERNEL);
  1396. applespi->msg_buf = devm_kmalloc_array(&spi->dev, MAX_PKTS_PER_MSG,
  1397. APPLESPI_PACKET_SIZE,
  1398. GFP_KERNEL);
  1399. if (!applespi->tx_buffer || !applespi->tx_status ||
  1400. !applespi->rx_buffer || !applespi->msg_buf)
  1401. return -ENOMEM;
  1402. /* set up our spi messages */
  1403. applespi_setup_read_txfrs(applespi);
  1404. applespi_setup_write_txfrs(applespi);
  1405. /* cache ACPI method handles */
  1406. acpi_sts = acpi_get_handle(spi_handle, "SIEN", &applespi->sien);
  1407. if (ACPI_FAILURE(acpi_sts)) {
  1408. dev_err(&applespi->spi->dev,
  1409. "Failed to get SIEN ACPI method handle: %s\n",
  1410. acpi_format_exception(acpi_sts));
  1411. return -ENODEV;
  1412. }
  1413. acpi_sts = acpi_get_handle(spi_handle, "SIST", &applespi->sist);
  1414. if (ACPI_FAILURE(acpi_sts)) {
  1415. dev_err(&applespi->spi->dev,
  1416. "Failed to get SIST ACPI method handle: %s\n",
  1417. acpi_format_exception(acpi_sts));
  1418. return -ENODEV;
  1419. }
  1420. /* switch on the SPI interface */
  1421. sts = applespi_setup_spi(applespi);
  1422. if (sts)
  1423. return sts;
  1424. sts = applespi_enable_spi(applespi);
  1425. if (sts)
  1426. return sts;
  1427. /* setup the keyboard input dev */
  1428. applespi->keyboard_input_dev = devm_input_allocate_device(&spi->dev);
  1429. if (!applespi->keyboard_input_dev)
  1430. return -ENOMEM;
  1431. applespi->keyboard_input_dev->name = "Apple SPI Keyboard";
  1432. applespi->keyboard_input_dev->phys = "applespi/input0";
  1433. applespi->keyboard_input_dev->dev.parent = &spi->dev;
  1434. applespi->keyboard_input_dev->id.bustype = BUS_SPI;
  1435. applespi->keyboard_input_dev->evbit[0] =
  1436. BIT_MASK(EV_KEY) | BIT_MASK(EV_LED) | BIT_MASK(EV_REP);
  1437. applespi->keyboard_input_dev->ledbit[0] = BIT_MASK(LED_CAPSL);
  1438. input_set_drvdata(applespi->keyboard_input_dev, applespi);
  1439. applespi->keyboard_input_dev->event = applespi_event;
  1440. for (i = 0; i < ARRAY_SIZE(applespi_scancodes); i++)
  1441. if (applespi_scancodes[i])
  1442. input_set_capability(applespi->keyboard_input_dev,
  1443. EV_KEY, applespi_scancodes[i]);
  1444. for (i = 0; i < ARRAY_SIZE(applespi_controlcodes); i++)
  1445. if (applespi_controlcodes[i])
  1446. input_set_capability(applespi->keyboard_input_dev,
  1447. EV_KEY, applespi_controlcodes[i]);
  1448. for (i = 0; i < ARRAY_SIZE(applespi_fn_codes); i++)
  1449. if (applespi_fn_codes[i].to)
  1450. input_set_capability(applespi->keyboard_input_dev,
  1451. EV_KEY, applespi_fn_codes[i].to);
  1452. input_set_capability(applespi->keyboard_input_dev, EV_KEY, KEY_FN);
  1453. sts = input_register_device(applespi->keyboard_input_dev);
  1454. if (sts) {
  1455. dev_err(&applespi->spi->dev,
  1456. "Unable to register keyboard input device (%d)\n", sts);
  1457. return -ENODEV;
  1458. }
  1459. /*
  1460. * The applespi device doesn't send interrupts normally (as is described
  1461. * in its DSDT), but rather seems to use ACPI GPEs.
  1462. */
  1463. acpi_sts = acpi_evaluate_integer(spi_handle, "_GPE", NULL, &gpe);
  1464. if (ACPI_FAILURE(acpi_sts)) {
  1465. dev_err(&applespi->spi->dev,
  1466. "Failed to obtain GPE for SPI slave device: %s\n",
  1467. acpi_format_exception(acpi_sts));
  1468. return -ENODEV;
  1469. }
  1470. applespi->gpe = (int)gpe;
  1471. acpi_sts = acpi_install_gpe_handler(NULL, applespi->gpe,
  1472. ACPI_GPE_LEVEL_TRIGGERED,
  1473. applespi_notify, applespi);
  1474. if (ACPI_FAILURE(acpi_sts)) {
  1475. dev_err(&applespi->spi->dev,
  1476. "Failed to install GPE handler for GPE %d: %s\n",
  1477. applespi->gpe, acpi_format_exception(acpi_sts));
  1478. return -ENODEV;
  1479. }
  1480. applespi->suspended = false;
  1481. acpi_sts = acpi_enable_gpe(NULL, applespi->gpe);
  1482. if (ACPI_FAILURE(acpi_sts)) {
  1483. dev_err(&applespi->spi->dev,
  1484. "Failed to enable GPE handler for GPE %d: %s\n",
  1485. applespi->gpe, acpi_format_exception(acpi_sts));
  1486. acpi_remove_gpe_handler(NULL, applespi->gpe, applespi_notify);
  1487. return -ENODEV;
  1488. }
  1489. /* trigger touchpad setup */
  1490. applespi_init(applespi, false);
  1491. /*
  1492. * By default this device is not enabled for wakeup; but USB keyboards
  1493. * generally are, so the expectation is that by default the keyboard
  1494. * will wake the system.
  1495. */
  1496. device_wakeup_enable(&spi->dev);
  1497. /* set up keyboard-backlight */
  1498. sts = applespi_get_saved_bl_level(applespi);
  1499. if (sts >= 0)
  1500. applespi_set_bl_level(&applespi->backlight_info, sts);
  1501. applespi->backlight_info.name = "spi::kbd_backlight";
  1502. applespi->backlight_info.default_trigger = "kbd-backlight";
  1503. applespi->backlight_info.brightness_set = applespi_set_bl_level;
  1504. sts = devm_led_classdev_register(&spi->dev, &applespi->backlight_info);
  1505. if (sts)
  1506. dev_warn(&applespi->spi->dev,
  1507. "Unable to register keyboard backlight class dev (%d)\n",
  1508. sts);
  1509. /* set up debugfs entries for touchpad dimensions logging */
  1510. applespi->debugfs_root = debugfs_create_dir("applespi", NULL);
  1511. debugfs_create_bool("enable_tp_dim", 0600, applespi->debugfs_root,
  1512. &applespi->debug_tp_dim);
  1513. debugfs_create_file("tp_dim", 0400, applespi->debugfs_root, applespi,
  1514. &applespi_tp_dim_fops);
  1515. return 0;
  1516. }
  1517. static void applespi_drain_writes(struct applespi_data *applespi)
  1518. {
  1519. unsigned long flags;
  1520. spin_lock_irqsave(&applespi->cmd_msg_lock, flags);
  1521. applespi->drain = true;
  1522. wait_event_lock_irq(applespi->drain_complete, !applespi->write_active,
  1523. applespi->cmd_msg_lock);
  1524. spin_unlock_irqrestore(&applespi->cmd_msg_lock, flags);
  1525. }
  1526. static void applespi_drain_reads(struct applespi_data *applespi)
  1527. {
  1528. unsigned long flags;
  1529. spin_lock_irqsave(&applespi->cmd_msg_lock, flags);
  1530. wait_event_lock_irq(applespi->drain_complete, !applespi->read_active,
  1531. applespi->cmd_msg_lock);
  1532. applespi->suspended = true;
  1533. spin_unlock_irqrestore(&applespi->cmd_msg_lock, flags);
  1534. }
  1535. static void applespi_remove(struct spi_device *spi)
  1536. {
  1537. struct applespi_data *applespi = spi_get_drvdata(spi);
  1538. applespi_drain_writes(applespi);
  1539. acpi_disable_gpe(NULL, applespi->gpe);
  1540. acpi_remove_gpe_handler(NULL, applespi->gpe, applespi_notify);
  1541. device_wakeup_disable(&spi->dev);
  1542. applespi_drain_reads(applespi);
  1543. debugfs_remove_recursive(applespi->debugfs_root);
  1544. }
  1545. static void applespi_shutdown(struct spi_device *spi)
  1546. {
  1547. struct applespi_data *applespi = spi_get_drvdata(spi);
  1548. applespi_save_bl_level(applespi, applespi->have_bl_level);
  1549. }
  1550. static int applespi_poweroff_late(struct device *dev)
  1551. {
  1552. struct spi_device *spi = to_spi_device(dev);
  1553. struct applespi_data *applespi = spi_get_drvdata(spi);
  1554. applespi_save_bl_level(applespi, applespi->have_bl_level);
  1555. return 0;
  1556. }
  1557. static int applespi_suspend(struct device *dev)
  1558. {
  1559. struct spi_device *spi = to_spi_device(dev);
  1560. struct applespi_data *applespi = spi_get_drvdata(spi);
  1561. acpi_status acpi_sts;
  1562. int sts;
  1563. /* turn off caps-lock - it'll stay on otherwise */
  1564. sts = applespi_set_capsl_led(applespi, false);
  1565. if (sts)
  1566. dev_warn(&applespi->spi->dev,
  1567. "Failed to turn off caps-lock led (%d)\n", sts);
  1568. applespi_drain_writes(applespi);
  1569. /* disable the interrupt */
  1570. acpi_sts = acpi_disable_gpe(NULL, applespi->gpe);
  1571. if (ACPI_FAILURE(acpi_sts))
  1572. dev_err(&applespi->spi->dev,
  1573. "Failed to disable GPE handler for GPE %d: %s\n",
  1574. applespi->gpe, acpi_format_exception(acpi_sts));
  1575. applespi_drain_reads(applespi);
  1576. return 0;
  1577. }
  1578. static int applespi_resume(struct device *dev)
  1579. {
  1580. struct spi_device *spi = to_spi_device(dev);
  1581. struct applespi_data *applespi = spi_get_drvdata(spi);
  1582. acpi_status acpi_sts;
  1583. unsigned long flags;
  1584. /* ensure our flags and state reflect a newly resumed device */
  1585. spin_lock_irqsave(&applespi->cmd_msg_lock, flags);
  1586. applespi->drain = false;
  1587. applespi->have_cl_led_on = false;
  1588. applespi->have_bl_level = 0;
  1589. applespi->cmd_msg_queued = 0;
  1590. applespi->read_active = false;
  1591. applespi->write_active = false;
  1592. applespi->suspended = false;
  1593. spin_unlock_irqrestore(&applespi->cmd_msg_lock, flags);
  1594. /* switch on the SPI interface */
  1595. applespi_enable_spi(applespi);
  1596. /* re-enable the interrupt */
  1597. acpi_sts = acpi_enable_gpe(NULL, applespi->gpe);
  1598. if (ACPI_FAILURE(acpi_sts))
  1599. dev_err(&applespi->spi->dev,
  1600. "Failed to re-enable GPE handler for GPE %d: %s\n",
  1601. applespi->gpe, acpi_format_exception(acpi_sts));
  1602. /* switch the touchpad into multitouch mode */
  1603. applespi_init(applespi, true);
  1604. return 0;
  1605. }
  1606. static const struct acpi_device_id applespi_acpi_match[] = {
  1607. { "APP000D", 0 },
  1608. { }
  1609. };
  1610. MODULE_DEVICE_TABLE(acpi, applespi_acpi_match);
  1611. static const struct dev_pm_ops applespi_pm_ops = {
  1612. SYSTEM_SLEEP_PM_OPS(applespi_suspend, applespi_resume)
  1613. .poweroff_late = pm_sleep_ptr(applespi_poweroff_late),
  1614. };
  1615. static struct spi_driver applespi_driver = {
  1616. .driver = {
  1617. .name = "applespi",
  1618. .acpi_match_table = applespi_acpi_match,
  1619. .pm = pm_sleep_ptr(&applespi_pm_ops),
  1620. },
  1621. .probe = applespi_probe,
  1622. .remove = applespi_remove,
  1623. .shutdown = applespi_shutdown,
  1624. };
  1625. module_spi_driver(applespi_driver)
  1626. MODULE_LICENSE("GPL v2");
  1627. MODULE_DESCRIPTION("MacBook(Pro) SPI Keyboard/Touchpad driver");
  1628. MODULE_AUTHOR("Federico Lorenzi");
  1629. MODULE_AUTHOR("Ronald Tschalär");