applespi.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970
  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 <asm/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. .llseek = no_llseek,
  858. };
  859. static void report_finger_data(struct input_dev *input, int slot,
  860. const struct input_mt_pos *pos,
  861. const struct tp_finger *f)
  862. {
  863. input_mt_slot(input, slot);
  864. input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
  865. input_report_abs(input, ABS_MT_TOUCH_MAJOR,
  866. le16_to_int(f->touch_major) << 1);
  867. input_report_abs(input, ABS_MT_TOUCH_MINOR,
  868. le16_to_int(f->touch_minor) << 1);
  869. input_report_abs(input, ABS_MT_WIDTH_MAJOR,
  870. le16_to_int(f->tool_major) << 1);
  871. input_report_abs(input, ABS_MT_WIDTH_MINOR,
  872. le16_to_int(f->tool_minor) << 1);
  873. input_report_abs(input, ABS_MT_ORIENTATION,
  874. MAX_FINGER_ORIENTATION - le16_to_int(f->orientation));
  875. input_report_abs(input, ABS_MT_POSITION_X, pos->x);
  876. input_report_abs(input, ABS_MT_POSITION_Y, pos->y);
  877. }
  878. static void report_tp_state(struct applespi_data *applespi,
  879. struct touchpad_protocol *t)
  880. {
  881. const struct tp_finger *f;
  882. struct input_dev *input;
  883. const struct applespi_tp_info *tp_info = &applespi->tp_info;
  884. int i, n;
  885. /* touchpad_input_dev is set async in worker */
  886. input = smp_load_acquire(&applespi->touchpad_input_dev);
  887. if (!input)
  888. return; /* touchpad isn't initialized yet */
  889. n = 0;
  890. for (i = 0; i < t->number_of_fingers; i++) {
  891. f = &t->fingers[i];
  892. if (le16_to_int(f->touch_major) == 0)
  893. continue;
  894. applespi->pos[n].x = le16_to_int(f->abs_x);
  895. applespi->pos[n].y = tp_info->y_min + tp_info->y_max -
  896. le16_to_int(f->abs_y);
  897. n++;
  898. if (applespi->debug_tp_dim)
  899. applespi_debug_update_dimensions(applespi, f);
  900. }
  901. input_mt_assign_slots(input, applespi->slots, applespi->pos, n, 0);
  902. for (i = 0; i < n; i++)
  903. report_finger_data(input, applespi->slots[i],
  904. &applespi->pos[i], &t->fingers[i]);
  905. input_mt_sync_frame(input);
  906. input_report_key(input, BTN_LEFT, t->clicked);
  907. input_sync(input);
  908. }
  909. static const struct applespi_key_translation *
  910. applespi_find_translation(const struct applespi_key_translation *table, u16 key)
  911. {
  912. const struct applespi_key_translation *trans;
  913. for (trans = table; trans->from; trans++)
  914. if (trans->from == key)
  915. return trans;
  916. return NULL;
  917. }
  918. static unsigned int applespi_translate_fn_key(unsigned int key, int fn_pressed)
  919. {
  920. const struct applespi_key_translation *trans;
  921. int do_translate;
  922. trans = applespi_find_translation(applespi_fn_codes, key);
  923. if (trans) {
  924. if (trans->flags & APPLE_FLAG_FKEY)
  925. do_translate = (fnmode == 2 && fn_pressed) ||
  926. (fnmode == 1 && !fn_pressed);
  927. else
  928. do_translate = fn_pressed;
  929. if (do_translate)
  930. key = trans->to;
  931. }
  932. return key;
  933. }
  934. static unsigned int applespi_translate_iso_layout(unsigned int key)
  935. {
  936. const struct applespi_key_translation *trans;
  937. trans = applespi_find_translation(apple_iso_keyboard, key);
  938. if (trans)
  939. key = trans->to;
  940. return key;
  941. }
  942. static unsigned int applespi_code_to_key(u8 code, int fn_pressed)
  943. {
  944. unsigned int key = applespi_scancodes[code];
  945. if (fnmode)
  946. key = applespi_translate_fn_key(key, fn_pressed);
  947. if (iso_layout)
  948. key = applespi_translate_iso_layout(key);
  949. return key;
  950. }
  951. static void
  952. applespi_remap_fn_key(struct keyboard_protocol *keyboard_protocol)
  953. {
  954. unsigned char tmp;
  955. u8 bit = BIT((fnremap - 1) & 0x07);
  956. if (!fnremap || fnremap > ARRAY_SIZE(applespi_controlcodes) ||
  957. !applespi_controlcodes[fnremap - 1])
  958. return;
  959. tmp = keyboard_protocol->fn_pressed;
  960. keyboard_protocol->fn_pressed = !!(keyboard_protocol->modifiers & bit);
  961. if (tmp)
  962. keyboard_protocol->modifiers |= bit;
  963. else
  964. keyboard_protocol->modifiers &= ~bit;
  965. }
  966. static void
  967. applespi_handle_keyboard_event(struct applespi_data *applespi,
  968. struct keyboard_protocol *keyboard_protocol)
  969. {
  970. unsigned int key;
  971. int i;
  972. compiletime_assert(ARRAY_SIZE(applespi_controlcodes) ==
  973. sizeof_field(struct keyboard_protocol, modifiers) * 8,
  974. "applespi_controlcodes has wrong number of entries");
  975. /* check for rollover overflow, which is signalled by all keys == 1 */
  976. if (!memchr_inv(keyboard_protocol->keys_pressed, 1, MAX_ROLLOVER))
  977. return;
  978. /* remap fn key if desired */
  979. applespi_remap_fn_key(keyboard_protocol);
  980. /* check released keys */
  981. for (i = 0; i < MAX_ROLLOVER; i++) {
  982. if (memchr(keyboard_protocol->keys_pressed,
  983. applespi->last_keys_pressed[i], MAX_ROLLOVER))
  984. continue; /* key is still pressed */
  985. key = applespi_code_to_key(applespi->last_keys_pressed[i],
  986. applespi->last_keys_fn_pressed[i]);
  987. input_report_key(applespi->keyboard_input_dev, key, 0);
  988. applespi->last_keys_fn_pressed[i] = 0;
  989. }
  990. /* check pressed keys */
  991. for (i = 0; i < MAX_ROLLOVER; i++) {
  992. if (keyboard_protocol->keys_pressed[i] <
  993. ARRAY_SIZE(applespi_scancodes) &&
  994. keyboard_protocol->keys_pressed[i] > 0) {
  995. key = applespi_code_to_key(
  996. keyboard_protocol->keys_pressed[i],
  997. keyboard_protocol->fn_pressed);
  998. input_report_key(applespi->keyboard_input_dev, key, 1);
  999. applespi->last_keys_fn_pressed[i] =
  1000. keyboard_protocol->fn_pressed;
  1001. }
  1002. }
  1003. /* check control keys */
  1004. for (i = 0; i < ARRAY_SIZE(applespi_controlcodes); i++) {
  1005. if (keyboard_protocol->modifiers & BIT(i))
  1006. input_report_key(applespi->keyboard_input_dev,
  1007. applespi_controlcodes[i], 1);
  1008. else
  1009. input_report_key(applespi->keyboard_input_dev,
  1010. applespi_controlcodes[i], 0);
  1011. }
  1012. /* check function key */
  1013. if (keyboard_protocol->fn_pressed && !applespi->last_fn_pressed)
  1014. input_report_key(applespi->keyboard_input_dev, KEY_FN, 1);
  1015. else if (!keyboard_protocol->fn_pressed && applespi->last_fn_pressed)
  1016. input_report_key(applespi->keyboard_input_dev, KEY_FN, 0);
  1017. applespi->last_fn_pressed = keyboard_protocol->fn_pressed;
  1018. /* done */
  1019. input_sync(applespi->keyboard_input_dev);
  1020. memcpy(&applespi->last_keys_pressed, keyboard_protocol->keys_pressed,
  1021. sizeof(applespi->last_keys_pressed));
  1022. }
  1023. static const struct applespi_tp_info *applespi_find_touchpad_info(u8 model)
  1024. {
  1025. const struct applespi_tp_model_info *info;
  1026. for (info = applespi_tp_models; info->model; info++) {
  1027. if (info->model == model)
  1028. return &info->tp_info;
  1029. }
  1030. return NULL;
  1031. }
  1032. static int
  1033. applespi_register_touchpad_device(struct applespi_data *applespi,
  1034. struct touchpad_info_protocol *rcvd_tp_info)
  1035. {
  1036. const struct applespi_tp_info *tp_info;
  1037. struct input_dev *touchpad_input_dev;
  1038. int sts;
  1039. /* set up touchpad dimensions */
  1040. tp_info = applespi_find_touchpad_info(rcvd_tp_info->model_no);
  1041. if (!tp_info) {
  1042. dev_warn(&applespi->spi->dev,
  1043. "Unknown touchpad model %x - falling back to MB8 touchpad\n",
  1044. rcvd_tp_info->model_no);
  1045. tp_info = &applespi_tp_models[0].tp_info;
  1046. }
  1047. applespi->tp_info = *tp_info;
  1048. if (touchpad_dimensions[0]) {
  1049. int x, y, w, h;
  1050. sts = sscanf(touchpad_dimensions, "%dx%d+%u+%u", &x, &y, &w, &h);
  1051. if (sts == 4) {
  1052. dev_info(&applespi->spi->dev,
  1053. "Overriding touchpad dimensions from module param\n");
  1054. applespi->tp_info.x_min = x;
  1055. applespi->tp_info.y_min = y;
  1056. applespi->tp_info.x_max = x + w;
  1057. applespi->tp_info.y_max = y + h;
  1058. } else {
  1059. dev_warn(&applespi->spi->dev,
  1060. "Invalid touchpad dimensions '%s': must be in the form XxY+W+H\n",
  1061. touchpad_dimensions);
  1062. touchpad_dimensions[0] = '\0';
  1063. }
  1064. }
  1065. if (!touchpad_dimensions[0]) {
  1066. snprintf(touchpad_dimensions, sizeof(touchpad_dimensions),
  1067. "%dx%d+%u+%u",
  1068. applespi->tp_info.x_min,
  1069. applespi->tp_info.y_min,
  1070. applespi->tp_info.x_max - applespi->tp_info.x_min,
  1071. applespi->tp_info.y_max - applespi->tp_info.y_min);
  1072. }
  1073. /* create touchpad input device */
  1074. touchpad_input_dev = devm_input_allocate_device(&applespi->spi->dev);
  1075. if (!touchpad_input_dev) {
  1076. dev_err(&applespi->spi->dev,
  1077. "Failed to allocate touchpad input device\n");
  1078. return -ENOMEM;
  1079. }
  1080. touchpad_input_dev->name = "Apple SPI Touchpad";
  1081. touchpad_input_dev->phys = "applespi/input1";
  1082. touchpad_input_dev->dev.parent = &applespi->spi->dev;
  1083. touchpad_input_dev->id.bustype = BUS_SPI;
  1084. touchpad_input_dev->id.vendor = SYNAPTICS_VENDOR_ID;
  1085. touchpad_input_dev->id.product =
  1086. rcvd_tp_info->model_no << 8 | rcvd_tp_info->model_flags;
  1087. /* basic properties */
  1088. input_set_capability(touchpad_input_dev, EV_REL, REL_X);
  1089. input_set_capability(touchpad_input_dev, EV_REL, REL_Y);
  1090. __set_bit(INPUT_PROP_POINTER, touchpad_input_dev->propbit);
  1091. __set_bit(INPUT_PROP_BUTTONPAD, touchpad_input_dev->propbit);
  1092. /* finger touch area */
  1093. input_set_abs_params(touchpad_input_dev, ABS_MT_TOUCH_MAJOR,
  1094. 0, 5000, 0, 0);
  1095. input_set_abs_params(touchpad_input_dev, ABS_MT_TOUCH_MINOR,
  1096. 0, 5000, 0, 0);
  1097. /* finger approach area */
  1098. input_set_abs_params(touchpad_input_dev, ABS_MT_WIDTH_MAJOR,
  1099. 0, 5000, 0, 0);
  1100. input_set_abs_params(touchpad_input_dev, ABS_MT_WIDTH_MINOR,
  1101. 0, 5000, 0, 0);
  1102. /* finger orientation */
  1103. input_set_abs_params(touchpad_input_dev, ABS_MT_ORIENTATION,
  1104. -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION,
  1105. 0, 0);
  1106. /* finger position */
  1107. input_set_abs_params(touchpad_input_dev, ABS_MT_POSITION_X,
  1108. applespi->tp_info.x_min, applespi->tp_info.x_max,
  1109. 0, 0);
  1110. input_set_abs_params(touchpad_input_dev, ABS_MT_POSITION_Y,
  1111. applespi->tp_info.y_min, applespi->tp_info.y_max,
  1112. 0, 0);
  1113. /* touchpad button */
  1114. input_set_capability(touchpad_input_dev, EV_KEY, BTN_LEFT);
  1115. /* multitouch */
  1116. sts = input_mt_init_slots(touchpad_input_dev, MAX_FINGERS,
  1117. INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
  1118. INPUT_MT_TRACK);
  1119. if (sts) {
  1120. dev_err(&applespi->spi->dev,
  1121. "failed to initialize slots: %d", sts);
  1122. return sts;
  1123. }
  1124. /* register input device */
  1125. sts = input_register_device(touchpad_input_dev);
  1126. if (sts) {
  1127. dev_err(&applespi->spi->dev,
  1128. "Unable to register touchpad input device (%d)\n", sts);
  1129. return sts;
  1130. }
  1131. /* touchpad_input_dev is read async in spi callback */
  1132. smp_store_release(&applespi->touchpad_input_dev, touchpad_input_dev);
  1133. return 0;
  1134. }
  1135. static void applespi_worker(struct work_struct *work)
  1136. {
  1137. struct applespi_data *applespi =
  1138. container_of(work, struct applespi_data, work);
  1139. applespi_register_touchpad_device(applespi, &applespi->rcvd_tp_info);
  1140. }
  1141. static void applespi_handle_cmd_response(struct applespi_data *applespi,
  1142. struct spi_packet *packet,
  1143. struct message *message)
  1144. {
  1145. if (packet->device == PACKET_DEV_INFO &&
  1146. le16_to_cpu(message->type) == 0x1020) {
  1147. /*
  1148. * We're not allowed to sleep here, but registering an input
  1149. * device can sleep.
  1150. */
  1151. applespi->rcvd_tp_info = message->tp_info;
  1152. schedule_work(&applespi->work);
  1153. return;
  1154. }
  1155. if (le16_to_cpu(message->length) != 0x0000) {
  1156. dev_warn_ratelimited(&applespi->spi->dev,
  1157. "Received unexpected write response: length=%x\n",
  1158. le16_to_cpu(message->length));
  1159. return;
  1160. }
  1161. if (packet->device == PACKET_DEV_TPAD &&
  1162. le16_to_cpu(message->type) == 0x0252 &&
  1163. le16_to_cpu(message->rsp_buf_len) == 0x0002)
  1164. dev_info(&applespi->spi->dev, "modeswitch done.\n");
  1165. }
  1166. static bool applespi_verify_crc(struct applespi_data *applespi, u8 *buffer,
  1167. size_t buflen)
  1168. {
  1169. u16 crc;
  1170. crc = crc16(0, buffer, buflen);
  1171. if (crc) {
  1172. dev_warn_ratelimited(&applespi->spi->dev,
  1173. "Received corrupted packet (crc mismatch)\n");
  1174. trace_applespi_bad_crc(ET_RD_CRC, READ, buffer, buflen);
  1175. return false;
  1176. }
  1177. return true;
  1178. }
  1179. static void applespi_debug_print_read_packet(struct applespi_data *applespi,
  1180. struct spi_packet *packet)
  1181. {
  1182. unsigned int evt_type;
  1183. if (packet->flags == PACKET_TYPE_READ &&
  1184. packet->device == PACKET_DEV_KEYB)
  1185. evt_type = ET_RD_KEYB;
  1186. else if (packet->flags == PACKET_TYPE_READ &&
  1187. packet->device == PACKET_DEV_TPAD)
  1188. evt_type = ET_RD_TPAD;
  1189. else if (packet->flags == PACKET_TYPE_WRITE)
  1190. evt_type = applespi->cmd_evt_type;
  1191. else
  1192. evt_type = ET_RD_UNKN;
  1193. applespi_get_trace_fun(evt_type)(evt_type, PT_READ, applespi->rx_buffer,
  1194. APPLESPI_PACKET_SIZE);
  1195. }
  1196. static void applespi_got_data(struct applespi_data *applespi)
  1197. {
  1198. struct spi_packet *packet;
  1199. struct message *message;
  1200. unsigned int msg_len;
  1201. unsigned int off;
  1202. unsigned int rem;
  1203. unsigned int len;
  1204. /* process packet header */
  1205. if (!applespi_verify_crc(applespi, applespi->rx_buffer,
  1206. APPLESPI_PACKET_SIZE)) {
  1207. unsigned long flags;
  1208. spin_lock_irqsave(&applespi->cmd_msg_lock, flags);
  1209. if (applespi->drain) {
  1210. applespi->read_active = false;
  1211. applespi->write_active = false;
  1212. wake_up_all(&applespi->drain_complete);
  1213. }
  1214. spin_unlock_irqrestore(&applespi->cmd_msg_lock, flags);
  1215. return;
  1216. }
  1217. packet = (struct spi_packet *)applespi->rx_buffer;
  1218. applespi_debug_print_read_packet(applespi, packet);
  1219. off = le16_to_cpu(packet->offset);
  1220. rem = le16_to_cpu(packet->remaining);
  1221. len = le16_to_cpu(packet->length);
  1222. if (len > sizeof(packet->data)) {
  1223. dev_warn_ratelimited(&applespi->spi->dev,
  1224. "Received corrupted packet (invalid packet length %u)\n",
  1225. len);
  1226. goto msg_complete;
  1227. }
  1228. /* handle multi-packet messages */
  1229. if (rem > 0 || off > 0) {
  1230. if (off != applespi->saved_msg_len) {
  1231. dev_warn_ratelimited(&applespi->spi->dev,
  1232. "Received unexpected offset (got %u, expected %u)\n",
  1233. off, applespi->saved_msg_len);
  1234. goto msg_complete;
  1235. }
  1236. if (off + rem > MAX_PKTS_PER_MSG * APPLESPI_PACKET_SIZE) {
  1237. dev_warn_ratelimited(&applespi->spi->dev,
  1238. "Received message too large (size %u)\n",
  1239. off + rem);
  1240. goto msg_complete;
  1241. }
  1242. if (off + len > MAX_PKTS_PER_MSG * APPLESPI_PACKET_SIZE) {
  1243. dev_warn_ratelimited(&applespi->spi->dev,
  1244. "Received message too large (size %u)\n",
  1245. off + len);
  1246. goto msg_complete;
  1247. }
  1248. memcpy(applespi->msg_buf + off, &packet->data, len);
  1249. applespi->saved_msg_len += len;
  1250. if (rem > 0)
  1251. return;
  1252. message = (struct message *)applespi->msg_buf;
  1253. msg_len = applespi->saved_msg_len;
  1254. } else {
  1255. message = (struct message *)&packet->data;
  1256. msg_len = len;
  1257. }
  1258. /* got complete message - verify */
  1259. if (!applespi_verify_crc(applespi, (u8 *)message, msg_len))
  1260. goto msg_complete;
  1261. if (le16_to_cpu(message->length) != msg_len - MSG_HEADER_SIZE - 2) {
  1262. dev_warn_ratelimited(&applespi->spi->dev,
  1263. "Received corrupted packet (invalid message length %u - expected %u)\n",
  1264. le16_to_cpu(message->length),
  1265. msg_len - MSG_HEADER_SIZE - 2);
  1266. goto msg_complete;
  1267. }
  1268. /* handle message */
  1269. if (packet->flags == PACKET_TYPE_READ &&
  1270. packet->device == PACKET_DEV_KEYB) {
  1271. applespi_handle_keyboard_event(applespi, &message->keyboard);
  1272. } else if (packet->flags == PACKET_TYPE_READ &&
  1273. packet->device == PACKET_DEV_TPAD) {
  1274. struct touchpad_protocol *tp;
  1275. size_t tp_len;
  1276. tp = &message->touchpad;
  1277. tp_len = struct_size(tp, fingers, tp->number_of_fingers);
  1278. if (le16_to_cpu(message->length) + 2 != tp_len) {
  1279. dev_warn_ratelimited(&applespi->spi->dev,
  1280. "Received corrupted packet (invalid message length %u - num-fingers %u, tp-len %zu)\n",
  1281. le16_to_cpu(message->length),
  1282. tp->number_of_fingers, tp_len);
  1283. goto msg_complete;
  1284. }
  1285. if (tp->number_of_fingers > MAX_FINGERS) {
  1286. dev_warn_ratelimited(&applespi->spi->dev,
  1287. "Number of reported fingers (%u) exceeds max (%u))\n",
  1288. tp->number_of_fingers,
  1289. MAX_FINGERS);
  1290. tp->number_of_fingers = MAX_FINGERS;
  1291. }
  1292. report_tp_state(applespi, tp);
  1293. } else if (packet->flags == PACKET_TYPE_WRITE) {
  1294. applespi_handle_cmd_response(applespi, packet, message);
  1295. }
  1296. msg_complete:
  1297. applespi->saved_msg_len = 0;
  1298. applespi_msg_complete(applespi, packet->flags == PACKET_TYPE_WRITE,
  1299. true);
  1300. }
  1301. static void applespi_async_read_complete(void *context)
  1302. {
  1303. struct applespi_data *applespi = context;
  1304. if (applespi->rd_m.status < 0) {
  1305. dev_warn(&applespi->spi->dev, "Error reading from device: %d\n",
  1306. applespi->rd_m.status);
  1307. /*
  1308. * We don't actually know if this was a pure read, or a response
  1309. * to a write. But this is a rare error condition that should
  1310. * never occur, so clearing both flags to avoid deadlock.
  1311. */
  1312. applespi_msg_complete(applespi, true, true);
  1313. } else {
  1314. applespi_got_data(applespi);
  1315. }
  1316. acpi_finish_gpe(NULL, applespi->gpe);
  1317. }
  1318. static u32 applespi_notify(acpi_handle gpe_device, u32 gpe, void *context)
  1319. {
  1320. struct applespi_data *applespi = context;
  1321. int sts;
  1322. unsigned long flags;
  1323. trace_applespi_irq_received(ET_RD_IRQ, PT_READ);
  1324. spin_lock_irqsave(&applespi->cmd_msg_lock, flags);
  1325. if (!applespi->suspended) {
  1326. sts = applespi_async(applespi, &applespi->rd_m,
  1327. applespi_async_read_complete);
  1328. if (sts)
  1329. dev_warn(&applespi->spi->dev,
  1330. "Error queueing async read to device: %d\n",
  1331. sts);
  1332. else
  1333. applespi->read_active = true;
  1334. }
  1335. spin_unlock_irqrestore(&applespi->cmd_msg_lock, flags);
  1336. return ACPI_INTERRUPT_HANDLED;
  1337. }
  1338. static int applespi_get_saved_bl_level(struct applespi_data *applespi)
  1339. {
  1340. efi_status_t sts = EFI_NOT_FOUND;
  1341. u16 efi_data = 0;
  1342. unsigned long efi_data_len = sizeof(efi_data);
  1343. if (efi_rt_services_supported(EFI_RT_SUPPORTED_GET_VARIABLE))
  1344. sts = efi.get_variable(EFI_BL_LEVEL_NAME, &EFI_BL_LEVEL_GUID,
  1345. NULL, &efi_data_len, &efi_data);
  1346. if (sts != EFI_SUCCESS && sts != EFI_NOT_FOUND)
  1347. dev_warn(&applespi->spi->dev,
  1348. "Error getting backlight level from EFI vars: 0x%lx\n",
  1349. sts);
  1350. return sts != EFI_SUCCESS ? -ENODEV : efi_data;
  1351. }
  1352. static void applespi_save_bl_level(struct applespi_data *applespi,
  1353. unsigned int level)
  1354. {
  1355. efi_status_t sts = EFI_UNSUPPORTED;
  1356. u32 efi_attr;
  1357. u16 efi_data;
  1358. efi_data = (u16)level;
  1359. efi_attr = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS |
  1360. EFI_VARIABLE_RUNTIME_ACCESS;
  1361. if (efi_rt_services_supported(EFI_RT_SUPPORTED_SET_VARIABLE))
  1362. sts = efi.set_variable(EFI_BL_LEVEL_NAME, &EFI_BL_LEVEL_GUID,
  1363. efi_attr, sizeof(efi_data), &efi_data);
  1364. if (sts != EFI_SUCCESS)
  1365. dev_warn(&applespi->spi->dev,
  1366. "Error saving backlight level to EFI vars: 0x%lx\n", sts);
  1367. }
  1368. static int applespi_probe(struct spi_device *spi)
  1369. {
  1370. struct applespi_data *applespi;
  1371. acpi_handle spi_handle = ACPI_HANDLE(&spi->dev);
  1372. acpi_status acpi_sts;
  1373. int sts, i;
  1374. unsigned long long gpe, usb_status;
  1375. /* check if the USB interface is present and enabled already */
  1376. acpi_sts = acpi_evaluate_integer(spi_handle, "UIST", NULL, &usb_status);
  1377. if (ACPI_SUCCESS(acpi_sts) && usb_status) {
  1378. /* let the USB driver take over instead */
  1379. dev_info(&spi->dev, "USB interface already enabled\n");
  1380. return -ENODEV;
  1381. }
  1382. /* allocate driver data */
  1383. applespi = devm_kzalloc(&spi->dev, sizeof(*applespi), GFP_KERNEL);
  1384. if (!applespi)
  1385. return -ENOMEM;
  1386. applespi->spi = spi;
  1387. INIT_WORK(&applespi->work, applespi_worker);
  1388. /* store the driver data */
  1389. spi_set_drvdata(spi, applespi);
  1390. /* create our buffers */
  1391. applespi->tx_buffer = devm_kmalloc(&spi->dev, APPLESPI_PACKET_SIZE,
  1392. GFP_KERNEL);
  1393. applespi->tx_status = devm_kmalloc(&spi->dev, APPLESPI_STATUS_SIZE,
  1394. GFP_KERNEL);
  1395. applespi->rx_buffer = devm_kmalloc(&spi->dev, APPLESPI_PACKET_SIZE,
  1396. GFP_KERNEL);
  1397. applespi->msg_buf = devm_kmalloc_array(&spi->dev, MAX_PKTS_PER_MSG,
  1398. APPLESPI_PACKET_SIZE,
  1399. GFP_KERNEL);
  1400. if (!applespi->tx_buffer || !applespi->tx_status ||
  1401. !applespi->rx_buffer || !applespi->msg_buf)
  1402. return -ENOMEM;
  1403. /* set up our spi messages */
  1404. applespi_setup_read_txfrs(applespi);
  1405. applespi_setup_write_txfrs(applespi);
  1406. /* cache ACPI method handles */
  1407. acpi_sts = acpi_get_handle(spi_handle, "SIEN", &applespi->sien);
  1408. if (ACPI_FAILURE(acpi_sts)) {
  1409. dev_err(&applespi->spi->dev,
  1410. "Failed to get SIEN ACPI method handle: %s\n",
  1411. acpi_format_exception(acpi_sts));
  1412. return -ENODEV;
  1413. }
  1414. acpi_sts = acpi_get_handle(spi_handle, "SIST", &applespi->sist);
  1415. if (ACPI_FAILURE(acpi_sts)) {
  1416. dev_err(&applespi->spi->dev,
  1417. "Failed to get SIST ACPI method handle: %s\n",
  1418. acpi_format_exception(acpi_sts));
  1419. return -ENODEV;
  1420. }
  1421. /* switch on the SPI interface */
  1422. sts = applespi_setup_spi(applespi);
  1423. if (sts)
  1424. return sts;
  1425. sts = applespi_enable_spi(applespi);
  1426. if (sts)
  1427. return sts;
  1428. /* setup the keyboard input dev */
  1429. applespi->keyboard_input_dev = devm_input_allocate_device(&spi->dev);
  1430. if (!applespi->keyboard_input_dev)
  1431. return -ENOMEM;
  1432. applespi->keyboard_input_dev->name = "Apple SPI Keyboard";
  1433. applespi->keyboard_input_dev->phys = "applespi/input0";
  1434. applespi->keyboard_input_dev->dev.parent = &spi->dev;
  1435. applespi->keyboard_input_dev->id.bustype = BUS_SPI;
  1436. applespi->keyboard_input_dev->evbit[0] =
  1437. BIT_MASK(EV_KEY) | BIT_MASK(EV_LED) | BIT_MASK(EV_REP);
  1438. applespi->keyboard_input_dev->ledbit[0] = BIT_MASK(LED_CAPSL);
  1439. input_set_drvdata(applespi->keyboard_input_dev, applespi);
  1440. applespi->keyboard_input_dev->event = applespi_event;
  1441. for (i = 0; i < ARRAY_SIZE(applespi_scancodes); i++)
  1442. if (applespi_scancodes[i])
  1443. input_set_capability(applespi->keyboard_input_dev,
  1444. EV_KEY, applespi_scancodes[i]);
  1445. for (i = 0; i < ARRAY_SIZE(applespi_controlcodes); i++)
  1446. if (applespi_controlcodes[i])
  1447. input_set_capability(applespi->keyboard_input_dev,
  1448. EV_KEY, applespi_controlcodes[i]);
  1449. for (i = 0; i < ARRAY_SIZE(applespi_fn_codes); i++)
  1450. if (applespi_fn_codes[i].to)
  1451. input_set_capability(applespi->keyboard_input_dev,
  1452. EV_KEY, applespi_fn_codes[i].to);
  1453. input_set_capability(applespi->keyboard_input_dev, EV_KEY, KEY_FN);
  1454. sts = input_register_device(applespi->keyboard_input_dev);
  1455. if (sts) {
  1456. dev_err(&applespi->spi->dev,
  1457. "Unable to register keyboard input device (%d)\n", sts);
  1458. return -ENODEV;
  1459. }
  1460. /*
  1461. * The applespi device doesn't send interrupts normally (as is described
  1462. * in its DSDT), but rather seems to use ACPI GPEs.
  1463. */
  1464. acpi_sts = acpi_evaluate_integer(spi_handle, "_GPE", NULL, &gpe);
  1465. if (ACPI_FAILURE(acpi_sts)) {
  1466. dev_err(&applespi->spi->dev,
  1467. "Failed to obtain GPE for SPI slave device: %s\n",
  1468. acpi_format_exception(acpi_sts));
  1469. return -ENODEV;
  1470. }
  1471. applespi->gpe = (int)gpe;
  1472. acpi_sts = acpi_install_gpe_handler(NULL, applespi->gpe,
  1473. ACPI_GPE_LEVEL_TRIGGERED,
  1474. applespi_notify, applespi);
  1475. if (ACPI_FAILURE(acpi_sts)) {
  1476. dev_err(&applespi->spi->dev,
  1477. "Failed to install GPE handler for GPE %d: %s\n",
  1478. applespi->gpe, acpi_format_exception(acpi_sts));
  1479. return -ENODEV;
  1480. }
  1481. applespi->suspended = false;
  1482. acpi_sts = acpi_enable_gpe(NULL, applespi->gpe);
  1483. if (ACPI_FAILURE(acpi_sts)) {
  1484. dev_err(&applespi->spi->dev,
  1485. "Failed to enable GPE handler for GPE %d: %s\n",
  1486. applespi->gpe, acpi_format_exception(acpi_sts));
  1487. acpi_remove_gpe_handler(NULL, applespi->gpe, applespi_notify);
  1488. return -ENODEV;
  1489. }
  1490. /* trigger touchpad setup */
  1491. applespi_init(applespi, false);
  1492. /*
  1493. * By default this device is not enabled for wakeup; but USB keyboards
  1494. * generally are, so the expectation is that by default the keyboard
  1495. * will wake the system.
  1496. */
  1497. device_wakeup_enable(&spi->dev);
  1498. /* set up keyboard-backlight */
  1499. sts = applespi_get_saved_bl_level(applespi);
  1500. if (sts >= 0)
  1501. applespi_set_bl_level(&applespi->backlight_info, sts);
  1502. applespi->backlight_info.name = "spi::kbd_backlight";
  1503. applespi->backlight_info.default_trigger = "kbd-backlight";
  1504. applespi->backlight_info.brightness_set = applespi_set_bl_level;
  1505. sts = devm_led_classdev_register(&spi->dev, &applespi->backlight_info);
  1506. if (sts)
  1507. dev_warn(&applespi->spi->dev,
  1508. "Unable to register keyboard backlight class dev (%d)\n",
  1509. sts);
  1510. /* set up debugfs entries for touchpad dimensions logging */
  1511. applespi->debugfs_root = debugfs_create_dir("applespi", NULL);
  1512. debugfs_create_bool("enable_tp_dim", 0600, applespi->debugfs_root,
  1513. &applespi->debug_tp_dim);
  1514. debugfs_create_file("tp_dim", 0400, applespi->debugfs_root, applespi,
  1515. &applespi_tp_dim_fops);
  1516. return 0;
  1517. }
  1518. static void applespi_drain_writes(struct applespi_data *applespi)
  1519. {
  1520. unsigned long flags;
  1521. spin_lock_irqsave(&applespi->cmd_msg_lock, flags);
  1522. applespi->drain = true;
  1523. wait_event_lock_irq(applespi->drain_complete, !applespi->write_active,
  1524. applespi->cmd_msg_lock);
  1525. spin_unlock_irqrestore(&applespi->cmd_msg_lock, flags);
  1526. }
  1527. static void applespi_drain_reads(struct applespi_data *applespi)
  1528. {
  1529. unsigned long flags;
  1530. spin_lock_irqsave(&applespi->cmd_msg_lock, flags);
  1531. wait_event_lock_irq(applespi->drain_complete, !applespi->read_active,
  1532. applespi->cmd_msg_lock);
  1533. applespi->suspended = true;
  1534. spin_unlock_irqrestore(&applespi->cmd_msg_lock, flags);
  1535. }
  1536. static void applespi_remove(struct spi_device *spi)
  1537. {
  1538. struct applespi_data *applespi = spi_get_drvdata(spi);
  1539. applespi_drain_writes(applespi);
  1540. acpi_disable_gpe(NULL, applespi->gpe);
  1541. acpi_remove_gpe_handler(NULL, applespi->gpe, applespi_notify);
  1542. device_wakeup_disable(&spi->dev);
  1543. applespi_drain_reads(applespi);
  1544. debugfs_remove_recursive(applespi->debugfs_root);
  1545. }
  1546. static void applespi_shutdown(struct spi_device *spi)
  1547. {
  1548. struct applespi_data *applespi = spi_get_drvdata(spi);
  1549. applespi_save_bl_level(applespi, applespi->have_bl_level);
  1550. }
  1551. static int applespi_poweroff_late(struct device *dev)
  1552. {
  1553. struct spi_device *spi = to_spi_device(dev);
  1554. struct applespi_data *applespi = spi_get_drvdata(spi);
  1555. applespi_save_bl_level(applespi, applespi->have_bl_level);
  1556. return 0;
  1557. }
  1558. static int __maybe_unused applespi_suspend(struct device *dev)
  1559. {
  1560. struct spi_device *spi = to_spi_device(dev);
  1561. struct applespi_data *applespi = spi_get_drvdata(spi);
  1562. acpi_status acpi_sts;
  1563. int sts;
  1564. /* turn off caps-lock - it'll stay on otherwise */
  1565. sts = applespi_set_capsl_led(applespi, false);
  1566. if (sts)
  1567. dev_warn(&applespi->spi->dev,
  1568. "Failed to turn off caps-lock led (%d)\n", sts);
  1569. applespi_drain_writes(applespi);
  1570. /* disable the interrupt */
  1571. acpi_sts = acpi_disable_gpe(NULL, applespi->gpe);
  1572. if (ACPI_FAILURE(acpi_sts))
  1573. dev_err(&applespi->spi->dev,
  1574. "Failed to disable GPE handler for GPE %d: %s\n",
  1575. applespi->gpe, acpi_format_exception(acpi_sts));
  1576. applespi_drain_reads(applespi);
  1577. return 0;
  1578. }
  1579. static int __maybe_unused applespi_resume(struct device *dev)
  1580. {
  1581. struct spi_device *spi = to_spi_device(dev);
  1582. struct applespi_data *applespi = spi_get_drvdata(spi);
  1583. acpi_status acpi_sts;
  1584. unsigned long flags;
  1585. /* ensure our flags and state reflect a newly resumed device */
  1586. spin_lock_irqsave(&applespi->cmd_msg_lock, flags);
  1587. applespi->drain = false;
  1588. applespi->have_cl_led_on = false;
  1589. applespi->have_bl_level = 0;
  1590. applespi->cmd_msg_queued = 0;
  1591. applespi->read_active = false;
  1592. applespi->write_active = false;
  1593. applespi->suspended = false;
  1594. spin_unlock_irqrestore(&applespi->cmd_msg_lock, flags);
  1595. /* switch on the SPI interface */
  1596. applespi_enable_spi(applespi);
  1597. /* re-enable the interrupt */
  1598. acpi_sts = acpi_enable_gpe(NULL, applespi->gpe);
  1599. if (ACPI_FAILURE(acpi_sts))
  1600. dev_err(&applespi->spi->dev,
  1601. "Failed to re-enable GPE handler for GPE %d: %s\n",
  1602. applespi->gpe, acpi_format_exception(acpi_sts));
  1603. /* switch the touchpad into multitouch mode */
  1604. applespi_init(applespi, true);
  1605. return 0;
  1606. }
  1607. static const struct acpi_device_id applespi_acpi_match[] = {
  1608. { "APP000D", 0 },
  1609. { }
  1610. };
  1611. MODULE_DEVICE_TABLE(acpi, applespi_acpi_match);
  1612. static const struct dev_pm_ops applespi_pm_ops = {
  1613. SET_SYSTEM_SLEEP_PM_OPS(applespi_suspend, applespi_resume)
  1614. .poweroff_late = applespi_poweroff_late,
  1615. };
  1616. static struct spi_driver applespi_driver = {
  1617. .driver = {
  1618. .name = "applespi",
  1619. .acpi_match_table = applespi_acpi_match,
  1620. .pm = &applespi_pm_ops,
  1621. },
  1622. .probe = applespi_probe,
  1623. .remove = applespi_remove,
  1624. .shutdown = applespi_shutdown,
  1625. };
  1626. module_spi_driver(applespi_driver)
  1627. MODULE_LICENSE("GPL v2");
  1628. MODULE_DESCRIPTION("MacBook(Pro) SPI Keyboard/Touchpad driver");
  1629. MODULE_AUTHOR("Federico Lorenzi");
  1630. MODULE_AUTHOR("Ronald Tschalär");