elantech.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Elantech Touchpad driver (v6)
  4. *
  5. * Copyright (C) 2007-2009 Arjan Opmeer <arjan@opmeer.net>
  6. *
  7. * Trademarks are the property of their respective owners.
  8. */
  9. #include <linux/delay.h>
  10. #include <linux/dmi.h>
  11. #include <linux/slab.h>
  12. #include <linux/module.h>
  13. #include <linux/i2c.h>
  14. #include <linux/input.h>
  15. #include <linux/input/mt.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/serio.h>
  18. #include <linux/libps2.h>
  19. #include <asm/unaligned.h>
  20. #include "psmouse.h"
  21. #include "elantech.h"
  22. #include "elan_i2c.h"
  23. #define elantech_debug(fmt, ...) \
  24. do { \
  25. if (etd->info.debug) \
  26. psmouse_printk(KERN_DEBUG, psmouse, \
  27. fmt, ##__VA_ARGS__); \
  28. } while (0)
  29. /*
  30. * Send a Synaptics style sliced query command
  31. */
  32. static int synaptics_send_cmd(struct psmouse *psmouse, unsigned char c,
  33. unsigned char *param)
  34. {
  35. if (ps2_sliced_command(&psmouse->ps2dev, c) ||
  36. ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_GETINFO)) {
  37. psmouse_err(psmouse, "%s query 0x%02x failed.\n", __func__, c);
  38. return -1;
  39. }
  40. return 0;
  41. }
  42. /*
  43. * V3 and later support this fast command
  44. */
  45. static int elantech_send_cmd(struct psmouse *psmouse, unsigned char c,
  46. unsigned char *param)
  47. {
  48. struct ps2dev *ps2dev = &psmouse->ps2dev;
  49. if (ps2_command(ps2dev, NULL, ETP_PS2_CUSTOM_COMMAND) ||
  50. ps2_command(ps2dev, NULL, c) ||
  51. ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) {
  52. psmouse_err(psmouse, "%s query 0x%02x failed.\n", __func__, c);
  53. return -1;
  54. }
  55. return 0;
  56. }
  57. /*
  58. * A retrying version of ps2_command
  59. */
  60. static int elantech_ps2_command(struct psmouse *psmouse,
  61. unsigned char *param, int command)
  62. {
  63. struct ps2dev *ps2dev = &psmouse->ps2dev;
  64. struct elantech_data *etd = psmouse->private;
  65. int rc;
  66. int tries = ETP_PS2_COMMAND_TRIES;
  67. do {
  68. rc = ps2_command(ps2dev, param, command);
  69. if (rc == 0)
  70. break;
  71. tries--;
  72. elantech_debug("retrying ps2 command 0x%02x (%d).\n",
  73. command, tries);
  74. msleep(ETP_PS2_COMMAND_DELAY);
  75. } while (tries > 0);
  76. if (rc)
  77. psmouse_err(psmouse, "ps2 command 0x%02x failed.\n", command);
  78. return rc;
  79. }
  80. /*
  81. * Send an Elantech style special command to read 3 bytes from a register
  82. */
  83. static int elantech_read_reg_params(struct psmouse *psmouse, u8 reg, u8 *param)
  84. {
  85. if (elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
  86. elantech_ps2_command(psmouse, NULL, ETP_REGISTER_READWRITE) ||
  87. elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
  88. elantech_ps2_command(psmouse, NULL, reg) ||
  89. elantech_ps2_command(psmouse, param, PSMOUSE_CMD_GETINFO)) {
  90. psmouse_err(psmouse,
  91. "failed to read register %#02x\n", reg);
  92. return -EIO;
  93. }
  94. return 0;
  95. }
  96. /*
  97. * Send an Elantech style special command to write a register with a parameter
  98. */
  99. static int elantech_write_reg_params(struct psmouse *psmouse, u8 reg, u8 *param)
  100. {
  101. if (elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
  102. elantech_ps2_command(psmouse, NULL, ETP_REGISTER_READWRITE) ||
  103. elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
  104. elantech_ps2_command(psmouse, NULL, reg) ||
  105. elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
  106. elantech_ps2_command(psmouse, NULL, param[0]) ||
  107. elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
  108. elantech_ps2_command(psmouse, NULL, param[1]) ||
  109. elantech_ps2_command(psmouse, NULL, PSMOUSE_CMD_SETSCALE11)) {
  110. psmouse_err(psmouse,
  111. "failed to write register %#02x with value %#02x%#02x\n",
  112. reg, param[0], param[1]);
  113. return -EIO;
  114. }
  115. return 0;
  116. }
  117. /*
  118. * Send an Elantech style special command to read a value from a register
  119. */
  120. static int elantech_read_reg(struct psmouse *psmouse, unsigned char reg,
  121. unsigned char *val)
  122. {
  123. struct elantech_data *etd = psmouse->private;
  124. unsigned char param[3];
  125. int rc = 0;
  126. if (reg < 0x07 || reg > 0x26)
  127. return -1;
  128. if (reg > 0x11 && reg < 0x20)
  129. return -1;
  130. switch (etd->info.hw_version) {
  131. case 1:
  132. if (ps2_sliced_command(&psmouse->ps2dev, ETP_REGISTER_READ) ||
  133. ps2_sliced_command(&psmouse->ps2dev, reg) ||
  134. ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_GETINFO)) {
  135. rc = -1;
  136. }
  137. break;
  138. case 2:
  139. if (elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
  140. elantech_ps2_command(psmouse, NULL, ETP_REGISTER_READ) ||
  141. elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
  142. elantech_ps2_command(psmouse, NULL, reg) ||
  143. elantech_ps2_command(psmouse, param, PSMOUSE_CMD_GETINFO)) {
  144. rc = -1;
  145. }
  146. break;
  147. case 3 ... 4:
  148. if (elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
  149. elantech_ps2_command(psmouse, NULL, ETP_REGISTER_READWRITE) ||
  150. elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
  151. elantech_ps2_command(psmouse, NULL, reg) ||
  152. elantech_ps2_command(psmouse, param, PSMOUSE_CMD_GETINFO)) {
  153. rc = -1;
  154. }
  155. break;
  156. }
  157. if (rc)
  158. psmouse_err(psmouse, "failed to read register 0x%02x.\n", reg);
  159. else if (etd->info.hw_version != 4)
  160. *val = param[0];
  161. else
  162. *val = param[1];
  163. return rc;
  164. }
  165. /*
  166. * Send an Elantech style special command to write a register with a value
  167. */
  168. static int elantech_write_reg(struct psmouse *psmouse, unsigned char reg,
  169. unsigned char val)
  170. {
  171. struct elantech_data *etd = psmouse->private;
  172. int rc = 0;
  173. if (reg < 0x07 || reg > 0x26)
  174. return -1;
  175. if (reg > 0x11 && reg < 0x20)
  176. return -1;
  177. switch (etd->info.hw_version) {
  178. case 1:
  179. if (ps2_sliced_command(&psmouse->ps2dev, ETP_REGISTER_WRITE) ||
  180. ps2_sliced_command(&psmouse->ps2dev, reg) ||
  181. ps2_sliced_command(&psmouse->ps2dev, val) ||
  182. ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11)) {
  183. rc = -1;
  184. }
  185. break;
  186. case 2:
  187. if (elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
  188. elantech_ps2_command(psmouse, NULL, ETP_REGISTER_WRITE) ||
  189. elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
  190. elantech_ps2_command(psmouse, NULL, reg) ||
  191. elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
  192. elantech_ps2_command(psmouse, NULL, val) ||
  193. elantech_ps2_command(psmouse, NULL, PSMOUSE_CMD_SETSCALE11)) {
  194. rc = -1;
  195. }
  196. break;
  197. case 3:
  198. if (elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
  199. elantech_ps2_command(psmouse, NULL, ETP_REGISTER_READWRITE) ||
  200. elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
  201. elantech_ps2_command(psmouse, NULL, reg) ||
  202. elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
  203. elantech_ps2_command(psmouse, NULL, val) ||
  204. elantech_ps2_command(psmouse, NULL, PSMOUSE_CMD_SETSCALE11)) {
  205. rc = -1;
  206. }
  207. break;
  208. case 4:
  209. if (elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
  210. elantech_ps2_command(psmouse, NULL, ETP_REGISTER_READWRITE) ||
  211. elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
  212. elantech_ps2_command(psmouse, NULL, reg) ||
  213. elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
  214. elantech_ps2_command(psmouse, NULL, ETP_REGISTER_READWRITE) ||
  215. elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
  216. elantech_ps2_command(psmouse, NULL, val) ||
  217. elantech_ps2_command(psmouse, NULL, PSMOUSE_CMD_SETSCALE11)) {
  218. rc = -1;
  219. }
  220. break;
  221. }
  222. if (rc)
  223. psmouse_err(psmouse,
  224. "failed to write register 0x%02x with value 0x%02x.\n",
  225. reg, val);
  226. return rc;
  227. }
  228. /*
  229. * Dump a complete mouse movement packet to the syslog
  230. */
  231. static void elantech_packet_dump(struct psmouse *psmouse)
  232. {
  233. psmouse_printk(KERN_DEBUG, psmouse, "PS/2 packet [%*ph]\n",
  234. psmouse->pktsize, psmouse->packet);
  235. }
  236. /*
  237. * Advertise INPUT_PROP_BUTTONPAD for clickpads. The testing of bit 12 in
  238. * fw_version for this is based on the following fw_version & caps table:
  239. *
  240. * Laptop-model: fw_version: caps: buttons:
  241. * Acer S3 0x461f00 10, 13, 0e clickpad
  242. * Acer S7-392 0x581f01 50, 17, 0d clickpad
  243. * Acer V5-131 0x461f02 01, 16, 0c clickpad
  244. * Acer V5-551 0x461f00 ? clickpad
  245. * Asus K53SV 0x450f01 78, 15, 0c 2 hw buttons
  246. * Asus G46VW 0x460f02 00, 18, 0c 2 hw buttons
  247. * Asus G750JX 0x360f00 00, 16, 0c 2 hw buttons
  248. * Asus TP500LN 0x381f17 10, 14, 0e clickpad
  249. * Asus X750JN 0x381f17 10, 14, 0e clickpad
  250. * Asus UX31 0x361f00 20, 15, 0e clickpad
  251. * Asus UX32VD 0x361f02 00, 15, 0e clickpad
  252. * Avatar AVIU-145A2 0x361f00 ? clickpad
  253. * Fujitsu CELSIUS H760 0x570f02 40, 14, 0c 3 hw buttons (**)
  254. * Fujitsu CELSIUS H780 0x5d0f02 41, 16, 0d 3 hw buttons (**)
  255. * Fujitsu LIFEBOOK E544 0x470f00 d0, 12, 09 2 hw buttons
  256. * Fujitsu LIFEBOOK E546 0x470f00 50, 12, 09 2 hw buttons
  257. * Fujitsu LIFEBOOK E547 0x470f00 50, 12, 09 2 hw buttons
  258. * Fujitsu LIFEBOOK E554 0x570f01 40, 14, 0c 2 hw buttons
  259. * Fujitsu LIFEBOOK E557 0x570f01 40, 14, 0c 2 hw buttons
  260. * Fujitsu T725 0x470f01 05, 12, 09 2 hw buttons
  261. * Fujitsu H730 0x570f00 c0, 14, 0c 3 hw buttons (**)
  262. * Gigabyte U2442 0x450f01 58, 17, 0c 2 hw buttons
  263. * Lenovo L430 0x350f02 b9, 15, 0c 2 hw buttons (*)
  264. * Lenovo L530 0x350f02 b9, 15, 0c 2 hw buttons (*)
  265. * Samsung NF210 0x150b00 78, 14, 0a 2 hw buttons
  266. * Samsung NP770Z5E 0x575f01 10, 15, 0f clickpad
  267. * Samsung NP700Z5B 0x361f06 21, 15, 0f clickpad
  268. * Samsung NP900X3E-A02 0x575f03 ? clickpad
  269. * Samsung NP-QX410 0x851b00 19, 14, 0c clickpad
  270. * Samsung RC512 0x450f00 08, 15, 0c 2 hw buttons
  271. * Samsung RF710 0x450f00 ? 2 hw buttons
  272. * System76 Pangolin 0x250f01 ? 2 hw buttons
  273. * (*) + 3 trackpoint buttons
  274. * (**) + 0 trackpoint buttons
  275. * Note: Lenovo L430 and Lenovo L530 have the same fw_version/caps
  276. */
  277. static inline int elantech_is_buttonpad(struct elantech_device_info *info)
  278. {
  279. return info->fw_version & 0x001000;
  280. }
  281. /*
  282. * Interpret complete data packets and report absolute mode input events for
  283. * hardware version 1. (4 byte packets)
  284. */
  285. static void elantech_report_absolute_v1(struct psmouse *psmouse)
  286. {
  287. struct input_dev *dev = psmouse->dev;
  288. struct elantech_data *etd = psmouse->private;
  289. unsigned char *packet = psmouse->packet;
  290. int fingers;
  291. if (etd->info.fw_version < 0x020000) {
  292. /*
  293. * byte 0: D U p1 p2 1 p3 R L
  294. * byte 1: f 0 th tw x9 x8 y9 y8
  295. */
  296. fingers = ((packet[1] & 0x80) >> 7) +
  297. ((packet[1] & 0x30) >> 4);
  298. } else {
  299. /*
  300. * byte 0: n1 n0 p2 p1 1 p3 R L
  301. * byte 1: 0 0 0 0 x9 x8 y9 y8
  302. */
  303. fingers = (packet[0] & 0xc0) >> 6;
  304. }
  305. if (etd->info.jumpy_cursor) {
  306. if (fingers != 1) {
  307. etd->single_finger_reports = 0;
  308. } else if (etd->single_finger_reports < 2) {
  309. /* Discard first 2 reports of one finger, bogus */
  310. etd->single_finger_reports++;
  311. elantech_debug("discarding packet\n");
  312. return;
  313. }
  314. }
  315. input_report_key(dev, BTN_TOUCH, fingers != 0);
  316. /*
  317. * byte 2: x7 x6 x5 x4 x3 x2 x1 x0
  318. * byte 3: y7 y6 y5 y4 y3 y2 y1 y0
  319. */
  320. if (fingers) {
  321. input_report_abs(dev, ABS_X,
  322. ((packet[1] & 0x0c) << 6) | packet[2]);
  323. input_report_abs(dev, ABS_Y,
  324. etd->y_max - (((packet[1] & 0x03) << 8) | packet[3]));
  325. }
  326. input_report_key(dev, BTN_TOOL_FINGER, fingers == 1);
  327. input_report_key(dev, BTN_TOOL_DOUBLETAP, fingers == 2);
  328. input_report_key(dev, BTN_TOOL_TRIPLETAP, fingers == 3);
  329. psmouse_report_standard_buttons(dev, packet[0]);
  330. if (etd->info.fw_version < 0x020000 &&
  331. (etd->info.capabilities[0] & ETP_CAP_HAS_ROCKER)) {
  332. /* rocker up */
  333. input_report_key(dev, BTN_FORWARD, packet[0] & 0x40);
  334. /* rocker down */
  335. input_report_key(dev, BTN_BACK, packet[0] & 0x80);
  336. }
  337. input_sync(dev);
  338. }
  339. static void elantech_set_slot(struct input_dev *dev, int slot, bool active,
  340. unsigned int x, unsigned int y)
  341. {
  342. input_mt_slot(dev, slot);
  343. input_mt_report_slot_state(dev, MT_TOOL_FINGER, active);
  344. if (active) {
  345. input_report_abs(dev, ABS_MT_POSITION_X, x);
  346. input_report_abs(dev, ABS_MT_POSITION_Y, y);
  347. }
  348. }
  349. /* x1 < x2 and y1 < y2 when two fingers, x = y = 0 when not pressed */
  350. static void elantech_report_semi_mt_data(struct input_dev *dev,
  351. unsigned int num_fingers,
  352. unsigned int x1, unsigned int y1,
  353. unsigned int x2, unsigned int y2)
  354. {
  355. elantech_set_slot(dev, 0, num_fingers != 0, x1, y1);
  356. elantech_set_slot(dev, 1, num_fingers >= 2, x2, y2);
  357. }
  358. /*
  359. * Interpret complete data packets and report absolute mode input events for
  360. * hardware version 2. (6 byte packets)
  361. */
  362. static void elantech_report_absolute_v2(struct psmouse *psmouse)
  363. {
  364. struct elantech_data *etd = psmouse->private;
  365. struct input_dev *dev = psmouse->dev;
  366. unsigned char *packet = psmouse->packet;
  367. unsigned int fingers, x1 = 0, y1 = 0, x2 = 0, y2 = 0;
  368. unsigned int width = 0, pres = 0;
  369. /* byte 0: n1 n0 . . . . R L */
  370. fingers = (packet[0] & 0xc0) >> 6;
  371. switch (fingers) {
  372. case 3:
  373. /*
  374. * Same as one finger, except report of more than 3 fingers:
  375. * byte 3: n4 . w1 w0 . . . .
  376. */
  377. if (packet[3] & 0x80)
  378. fingers = 4;
  379. fallthrough;
  380. case 1:
  381. /*
  382. * byte 1: . . . . x11 x10 x9 x8
  383. * byte 2: x7 x6 x5 x4 x4 x2 x1 x0
  384. */
  385. x1 = ((packet[1] & 0x0f) << 8) | packet[2];
  386. /*
  387. * byte 4: . . . . y11 y10 y9 y8
  388. * byte 5: y7 y6 y5 y4 y3 y2 y1 y0
  389. */
  390. y1 = etd->y_max - (((packet[4] & 0x0f) << 8) | packet[5]);
  391. pres = (packet[1] & 0xf0) | ((packet[4] & 0xf0) >> 4);
  392. width = ((packet[0] & 0x30) >> 2) | ((packet[3] & 0x30) >> 4);
  393. break;
  394. case 2:
  395. /*
  396. * The coordinate of each finger is reported separately
  397. * with a lower resolution for two finger touches:
  398. * byte 0: . . ay8 ax8 . . . .
  399. * byte 1: ax7 ax6 ax5 ax4 ax3 ax2 ax1 ax0
  400. */
  401. x1 = (((packet[0] & 0x10) << 4) | packet[1]) << 2;
  402. /* byte 2: ay7 ay6 ay5 ay4 ay3 ay2 ay1 ay0 */
  403. y1 = etd->y_max -
  404. ((((packet[0] & 0x20) << 3) | packet[2]) << 2);
  405. /*
  406. * byte 3: . . by8 bx8 . . . .
  407. * byte 4: bx7 bx6 bx5 bx4 bx3 bx2 bx1 bx0
  408. */
  409. x2 = (((packet[3] & 0x10) << 4) | packet[4]) << 2;
  410. /* byte 5: by7 by8 by5 by4 by3 by2 by1 by0 */
  411. y2 = etd->y_max -
  412. ((((packet[3] & 0x20) << 3) | packet[5]) << 2);
  413. /* Unknown so just report sensible values */
  414. pres = 127;
  415. width = 7;
  416. break;
  417. }
  418. input_report_key(dev, BTN_TOUCH, fingers != 0);
  419. if (fingers != 0) {
  420. input_report_abs(dev, ABS_X, x1);
  421. input_report_abs(dev, ABS_Y, y1);
  422. }
  423. elantech_report_semi_mt_data(dev, fingers, x1, y1, x2, y2);
  424. input_report_key(dev, BTN_TOOL_FINGER, fingers == 1);
  425. input_report_key(dev, BTN_TOOL_DOUBLETAP, fingers == 2);
  426. input_report_key(dev, BTN_TOOL_TRIPLETAP, fingers == 3);
  427. input_report_key(dev, BTN_TOOL_QUADTAP, fingers == 4);
  428. psmouse_report_standard_buttons(dev, packet[0]);
  429. if (etd->info.reports_pressure) {
  430. input_report_abs(dev, ABS_PRESSURE, pres);
  431. input_report_abs(dev, ABS_TOOL_WIDTH, width);
  432. }
  433. input_sync(dev);
  434. }
  435. static void elantech_report_trackpoint(struct psmouse *psmouse,
  436. int packet_type)
  437. {
  438. /*
  439. * byte 0: 0 0 sx sy 0 M R L
  440. * byte 1:~sx 0 0 0 0 0 0 0
  441. * byte 2:~sy 0 0 0 0 0 0 0
  442. * byte 3: 0 0 ~sy ~sx 0 1 1 0
  443. * byte 4: x7 x6 x5 x4 x3 x2 x1 x0
  444. * byte 5: y7 y6 y5 y4 y3 y2 y1 y0
  445. *
  446. * x and y are written in two's complement spread
  447. * over 9 bits with sx/sy the relative top bit and
  448. * x7..x0 and y7..y0 the lower bits.
  449. * The sign of y is opposite to what the input driver
  450. * expects for a relative movement
  451. */
  452. struct elantech_data *etd = psmouse->private;
  453. struct input_dev *tp_dev = etd->tp_dev;
  454. unsigned char *packet = psmouse->packet;
  455. int x, y;
  456. u32 t;
  457. t = get_unaligned_le32(&packet[0]);
  458. switch (t & ~7U) {
  459. case 0x06000030U:
  460. case 0x16008020U:
  461. case 0x26800010U:
  462. case 0x36808000U:
  463. /*
  464. * This firmware misreport coordinates for trackpoint
  465. * occasionally. Discard packets outside of [-127, 127] range
  466. * to prevent cursor jumps.
  467. */
  468. if (packet[4] == 0x80 || packet[5] == 0x80 ||
  469. packet[1] >> 7 == packet[4] >> 7 ||
  470. packet[2] >> 7 == packet[5] >> 7) {
  471. elantech_debug("discarding packet [%6ph]\n", packet);
  472. break;
  473. }
  474. x = packet[4] - (int)((packet[1]^0x80) << 1);
  475. y = (int)((packet[2]^0x80) << 1) - packet[5];
  476. psmouse_report_standard_buttons(tp_dev, packet[0]);
  477. input_report_rel(tp_dev, REL_X, x);
  478. input_report_rel(tp_dev, REL_Y, y);
  479. input_sync(tp_dev);
  480. break;
  481. default:
  482. /* Dump unexpected packet sequences if debug=1 (default) */
  483. if (etd->info.debug == 1)
  484. elantech_packet_dump(psmouse);
  485. break;
  486. }
  487. }
  488. /*
  489. * Interpret complete data packets and report absolute mode input events for
  490. * hardware version 3. (12 byte packets for two fingers)
  491. */
  492. static void elantech_report_absolute_v3(struct psmouse *psmouse,
  493. int packet_type)
  494. {
  495. struct input_dev *dev = psmouse->dev;
  496. struct elantech_data *etd = psmouse->private;
  497. unsigned char *packet = psmouse->packet;
  498. unsigned int fingers = 0, x1 = 0, y1 = 0, x2 = 0, y2 = 0;
  499. unsigned int width = 0, pres = 0;
  500. /* byte 0: n1 n0 . . . . R L */
  501. fingers = (packet[0] & 0xc0) >> 6;
  502. switch (fingers) {
  503. case 3:
  504. case 1:
  505. /*
  506. * byte 1: . . . . x11 x10 x9 x8
  507. * byte 2: x7 x6 x5 x4 x4 x2 x1 x0
  508. */
  509. x1 = ((packet[1] & 0x0f) << 8) | packet[2];
  510. /*
  511. * byte 4: . . . . y11 y10 y9 y8
  512. * byte 5: y7 y6 y5 y4 y3 y2 y1 y0
  513. */
  514. y1 = etd->y_max - (((packet[4] & 0x0f) << 8) | packet[5]);
  515. break;
  516. case 2:
  517. if (packet_type == PACKET_V3_HEAD) {
  518. /*
  519. * byte 1: . . . . ax11 ax10 ax9 ax8
  520. * byte 2: ax7 ax6 ax5 ax4 ax3 ax2 ax1 ax0
  521. */
  522. etd->mt[0].x = ((packet[1] & 0x0f) << 8) | packet[2];
  523. /*
  524. * byte 4: . . . . ay11 ay10 ay9 ay8
  525. * byte 5: ay7 ay6 ay5 ay4 ay3 ay2 ay1 ay0
  526. */
  527. etd->mt[0].y = etd->y_max -
  528. (((packet[4] & 0x0f) << 8) | packet[5]);
  529. /*
  530. * wait for next packet
  531. */
  532. return;
  533. }
  534. /* packet_type == PACKET_V3_TAIL */
  535. x1 = etd->mt[0].x;
  536. y1 = etd->mt[0].y;
  537. x2 = ((packet[1] & 0x0f) << 8) | packet[2];
  538. y2 = etd->y_max - (((packet[4] & 0x0f) << 8) | packet[5]);
  539. break;
  540. }
  541. pres = (packet[1] & 0xf0) | ((packet[4] & 0xf0) >> 4);
  542. width = ((packet[0] & 0x30) >> 2) | ((packet[3] & 0x30) >> 4);
  543. input_report_key(dev, BTN_TOUCH, fingers != 0);
  544. if (fingers != 0) {
  545. input_report_abs(dev, ABS_X, x1);
  546. input_report_abs(dev, ABS_Y, y1);
  547. }
  548. elantech_report_semi_mt_data(dev, fingers, x1, y1, x2, y2);
  549. input_report_key(dev, BTN_TOOL_FINGER, fingers == 1);
  550. input_report_key(dev, BTN_TOOL_DOUBLETAP, fingers == 2);
  551. input_report_key(dev, BTN_TOOL_TRIPLETAP, fingers == 3);
  552. /* For clickpads map both buttons to BTN_LEFT */
  553. if (elantech_is_buttonpad(&etd->info))
  554. input_report_key(dev, BTN_LEFT, packet[0] & 0x03);
  555. else
  556. psmouse_report_standard_buttons(dev, packet[0]);
  557. input_report_abs(dev, ABS_PRESSURE, pres);
  558. input_report_abs(dev, ABS_TOOL_WIDTH, width);
  559. input_sync(dev);
  560. }
  561. static void elantech_input_sync_v4(struct psmouse *psmouse)
  562. {
  563. struct input_dev *dev = psmouse->dev;
  564. struct elantech_data *etd = psmouse->private;
  565. unsigned char *packet = psmouse->packet;
  566. /* For clickpads map both buttons to BTN_LEFT */
  567. if (elantech_is_buttonpad(&etd->info))
  568. input_report_key(dev, BTN_LEFT, packet[0] & 0x03);
  569. else
  570. psmouse_report_standard_buttons(dev, packet[0]);
  571. input_mt_report_pointer_emulation(dev, true);
  572. input_sync(dev);
  573. }
  574. static void process_packet_status_v4(struct psmouse *psmouse)
  575. {
  576. struct input_dev *dev = psmouse->dev;
  577. unsigned char *packet = psmouse->packet;
  578. unsigned fingers;
  579. int i;
  580. /* notify finger state change */
  581. fingers = packet[1] & 0x1f;
  582. for (i = 0; i < ETP_MAX_FINGERS; i++) {
  583. if ((fingers & (1 << i)) == 0) {
  584. input_mt_slot(dev, i);
  585. input_mt_report_slot_state(dev, MT_TOOL_FINGER, false);
  586. }
  587. }
  588. elantech_input_sync_v4(psmouse);
  589. }
  590. static void process_packet_head_v4(struct psmouse *psmouse)
  591. {
  592. struct input_dev *dev = psmouse->dev;
  593. struct elantech_data *etd = psmouse->private;
  594. unsigned char *packet = psmouse->packet;
  595. int id = ((packet[3] & 0xe0) >> 5) - 1;
  596. int pres, traces;
  597. if (id < 0)
  598. return;
  599. etd->mt[id].x = ((packet[1] & 0x0f) << 8) | packet[2];
  600. etd->mt[id].y = etd->y_max - (((packet[4] & 0x0f) << 8) | packet[5]);
  601. pres = (packet[1] & 0xf0) | ((packet[4] & 0xf0) >> 4);
  602. traces = (packet[0] & 0xf0) >> 4;
  603. input_mt_slot(dev, id);
  604. input_mt_report_slot_state(dev, MT_TOOL_FINGER, true);
  605. input_report_abs(dev, ABS_MT_POSITION_X, etd->mt[id].x);
  606. input_report_abs(dev, ABS_MT_POSITION_Y, etd->mt[id].y);
  607. input_report_abs(dev, ABS_MT_PRESSURE, pres);
  608. input_report_abs(dev, ABS_MT_TOUCH_MAJOR, traces * etd->width);
  609. /* report this for backwards compatibility */
  610. input_report_abs(dev, ABS_TOOL_WIDTH, traces);
  611. elantech_input_sync_v4(psmouse);
  612. }
  613. static void process_packet_motion_v4(struct psmouse *psmouse)
  614. {
  615. struct input_dev *dev = psmouse->dev;
  616. struct elantech_data *etd = psmouse->private;
  617. unsigned char *packet = psmouse->packet;
  618. int weight, delta_x1 = 0, delta_y1 = 0, delta_x2 = 0, delta_y2 = 0;
  619. int id, sid;
  620. id = ((packet[0] & 0xe0) >> 5) - 1;
  621. if (id < 0)
  622. return;
  623. sid = ((packet[3] & 0xe0) >> 5) - 1;
  624. weight = (packet[0] & 0x10) ? ETP_WEIGHT_VALUE : 1;
  625. /*
  626. * Motion packets give us the delta of x, y values of specific fingers,
  627. * but in two's complement. Let the compiler do the conversion for us.
  628. * Also _enlarge_ the numbers to int, in case of overflow.
  629. */
  630. delta_x1 = (signed char)packet[1];
  631. delta_y1 = (signed char)packet[2];
  632. delta_x2 = (signed char)packet[4];
  633. delta_y2 = (signed char)packet[5];
  634. etd->mt[id].x += delta_x1 * weight;
  635. etd->mt[id].y -= delta_y1 * weight;
  636. input_mt_slot(dev, id);
  637. input_report_abs(dev, ABS_MT_POSITION_X, etd->mt[id].x);
  638. input_report_abs(dev, ABS_MT_POSITION_Y, etd->mt[id].y);
  639. if (sid >= 0) {
  640. etd->mt[sid].x += delta_x2 * weight;
  641. etd->mt[sid].y -= delta_y2 * weight;
  642. input_mt_slot(dev, sid);
  643. input_report_abs(dev, ABS_MT_POSITION_X, etd->mt[sid].x);
  644. input_report_abs(dev, ABS_MT_POSITION_Y, etd->mt[sid].y);
  645. }
  646. elantech_input_sync_v4(psmouse);
  647. }
  648. static void elantech_report_absolute_v4(struct psmouse *psmouse,
  649. int packet_type)
  650. {
  651. switch (packet_type) {
  652. case PACKET_V4_STATUS:
  653. process_packet_status_v4(psmouse);
  654. break;
  655. case PACKET_V4_HEAD:
  656. process_packet_head_v4(psmouse);
  657. break;
  658. case PACKET_V4_MOTION:
  659. process_packet_motion_v4(psmouse);
  660. break;
  661. case PACKET_UNKNOWN:
  662. default:
  663. /* impossible to get here */
  664. break;
  665. }
  666. }
  667. static int elantech_packet_check_v1(struct psmouse *psmouse)
  668. {
  669. struct elantech_data *etd = psmouse->private;
  670. unsigned char *packet = psmouse->packet;
  671. unsigned char p1, p2, p3;
  672. /* Parity bits are placed differently */
  673. if (etd->info.fw_version < 0x020000) {
  674. /* byte 0: D U p1 p2 1 p3 R L */
  675. p1 = (packet[0] & 0x20) >> 5;
  676. p2 = (packet[0] & 0x10) >> 4;
  677. } else {
  678. /* byte 0: n1 n0 p2 p1 1 p3 R L */
  679. p1 = (packet[0] & 0x10) >> 4;
  680. p2 = (packet[0] & 0x20) >> 5;
  681. }
  682. p3 = (packet[0] & 0x04) >> 2;
  683. return etd->parity[packet[1]] == p1 &&
  684. etd->parity[packet[2]] == p2 &&
  685. etd->parity[packet[3]] == p3;
  686. }
  687. static int elantech_debounce_check_v2(struct psmouse *psmouse)
  688. {
  689. /*
  690. * When we encounter packet that matches this exactly, it means the
  691. * hardware is in debounce status. Just ignore the whole packet.
  692. */
  693. static const u8 debounce_packet[] = {
  694. 0x84, 0xff, 0xff, 0x02, 0xff, 0xff
  695. };
  696. unsigned char *packet = psmouse->packet;
  697. return !memcmp(packet, debounce_packet, sizeof(debounce_packet));
  698. }
  699. static int elantech_packet_check_v2(struct psmouse *psmouse)
  700. {
  701. struct elantech_data *etd = psmouse->private;
  702. unsigned char *packet = psmouse->packet;
  703. /*
  704. * V2 hardware has two flavors. Older ones that do not report pressure,
  705. * and newer ones that reports pressure and width. With newer ones, all
  706. * packets (1, 2, 3 finger touch) have the same constant bits. With
  707. * older ones, 1/3 finger touch packets and 2 finger touch packets
  708. * have different constant bits.
  709. * With all three cases, if the constant bits are not exactly what I
  710. * expected, I consider them invalid.
  711. */
  712. if (etd->info.reports_pressure)
  713. return (packet[0] & 0x0c) == 0x04 &&
  714. (packet[3] & 0x0f) == 0x02;
  715. if ((packet[0] & 0xc0) == 0x80)
  716. return (packet[0] & 0x0c) == 0x0c &&
  717. (packet[3] & 0x0e) == 0x08;
  718. return (packet[0] & 0x3c) == 0x3c &&
  719. (packet[1] & 0xf0) == 0x00 &&
  720. (packet[3] & 0x3e) == 0x38 &&
  721. (packet[4] & 0xf0) == 0x00;
  722. }
  723. /*
  724. * We check the constant bits to determine what packet type we get,
  725. * so packet checking is mandatory for v3 and later hardware.
  726. */
  727. static int elantech_packet_check_v3(struct psmouse *psmouse)
  728. {
  729. struct elantech_data *etd = psmouse->private;
  730. static const u8 debounce_packet[] = {
  731. 0xc4, 0xff, 0xff, 0x02, 0xff, 0xff
  732. };
  733. unsigned char *packet = psmouse->packet;
  734. /*
  735. * check debounce first, it has the same signature in byte 0
  736. * and byte 3 as PACKET_V3_HEAD.
  737. */
  738. if (!memcmp(packet, debounce_packet, sizeof(debounce_packet)))
  739. return PACKET_DEBOUNCE;
  740. /*
  741. * If the hardware flag 'crc_enabled' is set the packets have
  742. * different signatures.
  743. */
  744. if (etd->info.crc_enabled) {
  745. if ((packet[3] & 0x09) == 0x08)
  746. return PACKET_V3_HEAD;
  747. if ((packet[3] & 0x09) == 0x09)
  748. return PACKET_V3_TAIL;
  749. } else {
  750. if ((packet[0] & 0x0c) == 0x04 && (packet[3] & 0xcf) == 0x02)
  751. return PACKET_V3_HEAD;
  752. if ((packet[0] & 0x0c) == 0x0c && (packet[3] & 0xce) == 0x0c)
  753. return PACKET_V3_TAIL;
  754. if ((packet[3] & 0x0f) == 0x06)
  755. return PACKET_TRACKPOINT;
  756. }
  757. return PACKET_UNKNOWN;
  758. }
  759. static int elantech_packet_check_v4(struct psmouse *psmouse)
  760. {
  761. struct elantech_data *etd = psmouse->private;
  762. unsigned char *packet = psmouse->packet;
  763. unsigned char packet_type = packet[3] & 0x03;
  764. unsigned int ic_version;
  765. bool sanity_check;
  766. if (etd->tp_dev && (packet[3] & 0x0f) == 0x06)
  767. return PACKET_TRACKPOINT;
  768. /* This represents the version of IC body. */
  769. ic_version = (etd->info.fw_version & 0x0f0000) >> 16;
  770. /*
  771. * Sanity check based on the constant bits of a packet.
  772. * The constant bits change depending on the value of
  773. * the hardware flag 'crc_enabled' and the version of
  774. * the IC body, but are the same for every packet,
  775. * regardless of the type.
  776. */
  777. if (etd->info.crc_enabled)
  778. sanity_check = ((packet[3] & 0x08) == 0x00);
  779. else if (ic_version == 7 && etd->info.samples[1] == 0x2A)
  780. sanity_check = ((packet[3] & 0x1c) == 0x10);
  781. else
  782. sanity_check = ((packet[0] & 0x08) == 0x00 &&
  783. (packet[3] & 0x1c) == 0x10);
  784. if (!sanity_check)
  785. return PACKET_UNKNOWN;
  786. switch (packet_type) {
  787. case 0:
  788. return PACKET_V4_STATUS;
  789. case 1:
  790. return PACKET_V4_HEAD;
  791. case 2:
  792. return PACKET_V4_MOTION;
  793. }
  794. return PACKET_UNKNOWN;
  795. }
  796. /*
  797. * Process byte stream from mouse and handle complete packets
  798. */
  799. static psmouse_ret_t elantech_process_byte(struct psmouse *psmouse)
  800. {
  801. struct elantech_data *etd = psmouse->private;
  802. int packet_type;
  803. if (psmouse->pktcnt < psmouse->pktsize)
  804. return PSMOUSE_GOOD_DATA;
  805. if (etd->info.debug > 1)
  806. elantech_packet_dump(psmouse);
  807. switch (etd->info.hw_version) {
  808. case 1:
  809. if (etd->info.paritycheck && !elantech_packet_check_v1(psmouse))
  810. return PSMOUSE_BAD_DATA;
  811. elantech_report_absolute_v1(psmouse);
  812. break;
  813. case 2:
  814. /* ignore debounce */
  815. if (elantech_debounce_check_v2(psmouse))
  816. return PSMOUSE_FULL_PACKET;
  817. if (etd->info.paritycheck && !elantech_packet_check_v2(psmouse))
  818. return PSMOUSE_BAD_DATA;
  819. elantech_report_absolute_v2(psmouse);
  820. break;
  821. case 3:
  822. packet_type = elantech_packet_check_v3(psmouse);
  823. switch (packet_type) {
  824. case PACKET_UNKNOWN:
  825. return PSMOUSE_BAD_DATA;
  826. case PACKET_DEBOUNCE:
  827. /* ignore debounce */
  828. break;
  829. case PACKET_TRACKPOINT:
  830. elantech_report_trackpoint(psmouse, packet_type);
  831. break;
  832. default:
  833. elantech_report_absolute_v3(psmouse, packet_type);
  834. break;
  835. }
  836. break;
  837. case 4:
  838. packet_type = elantech_packet_check_v4(psmouse);
  839. switch (packet_type) {
  840. case PACKET_UNKNOWN:
  841. return PSMOUSE_BAD_DATA;
  842. case PACKET_TRACKPOINT:
  843. elantech_report_trackpoint(psmouse, packet_type);
  844. break;
  845. default:
  846. elantech_report_absolute_v4(psmouse, packet_type);
  847. break;
  848. }
  849. break;
  850. }
  851. return PSMOUSE_FULL_PACKET;
  852. }
  853. /*
  854. * This writes the reg_07 value again to the hardware at the end of every
  855. * set_rate call because the register loses its value. reg_07 allows setting
  856. * absolute mode on v4 hardware
  857. */
  858. static void elantech_set_rate_restore_reg_07(struct psmouse *psmouse,
  859. unsigned int rate)
  860. {
  861. struct elantech_data *etd = psmouse->private;
  862. etd->original_set_rate(psmouse, rate);
  863. if (elantech_write_reg(psmouse, 0x07, etd->reg_07))
  864. psmouse_err(psmouse, "restoring reg_07 failed\n");
  865. }
  866. /*
  867. * Put the touchpad into absolute mode
  868. */
  869. static int elantech_set_absolute_mode(struct psmouse *psmouse)
  870. {
  871. struct elantech_data *etd = psmouse->private;
  872. unsigned char val;
  873. int tries = ETP_READ_BACK_TRIES;
  874. int rc = 0;
  875. switch (etd->info.hw_version) {
  876. case 1:
  877. etd->reg_10 = 0x16;
  878. etd->reg_11 = 0x8f;
  879. if (elantech_write_reg(psmouse, 0x10, etd->reg_10) ||
  880. elantech_write_reg(psmouse, 0x11, etd->reg_11)) {
  881. rc = -1;
  882. }
  883. break;
  884. case 2:
  885. /* Windows driver values */
  886. etd->reg_10 = 0x54;
  887. etd->reg_11 = 0x88; /* 0x8a */
  888. etd->reg_21 = 0x60; /* 0x00 */
  889. if (elantech_write_reg(psmouse, 0x10, etd->reg_10) ||
  890. elantech_write_reg(psmouse, 0x11, etd->reg_11) ||
  891. elantech_write_reg(psmouse, 0x21, etd->reg_21)) {
  892. rc = -1;
  893. }
  894. break;
  895. case 3:
  896. if (etd->info.set_hw_resolution)
  897. etd->reg_10 = 0x0b;
  898. else
  899. etd->reg_10 = 0x01;
  900. if (elantech_write_reg(psmouse, 0x10, etd->reg_10))
  901. rc = -1;
  902. break;
  903. case 4:
  904. etd->reg_07 = 0x01;
  905. if (elantech_write_reg(psmouse, 0x07, etd->reg_07))
  906. rc = -1;
  907. goto skip_readback_reg_10; /* v4 has no reg 0x10 to read */
  908. }
  909. if (rc == 0) {
  910. /*
  911. * Read back reg 0x10. For hardware version 1 we must make
  912. * sure the absolute mode bit is set. For hardware version 2
  913. * the touchpad is probably initializing and not ready until
  914. * we read back the value we just wrote.
  915. */
  916. do {
  917. rc = elantech_read_reg(psmouse, 0x10, &val);
  918. if (rc == 0)
  919. break;
  920. tries--;
  921. elantech_debug("retrying read (%d).\n", tries);
  922. msleep(ETP_READ_BACK_DELAY);
  923. } while (tries > 0);
  924. if (rc) {
  925. psmouse_err(psmouse,
  926. "failed to read back register 0x10.\n");
  927. } else if (etd->info.hw_version == 1 &&
  928. !(val & ETP_R10_ABSOLUTE_MODE)) {
  929. psmouse_err(psmouse,
  930. "touchpad refuses to switch to absolute mode.\n");
  931. rc = -1;
  932. }
  933. }
  934. skip_readback_reg_10:
  935. if (rc)
  936. psmouse_err(psmouse, "failed to initialise registers.\n");
  937. return rc;
  938. }
  939. /*
  940. * (value from firmware) * 10 + 790 = dpi
  941. * we also have to convert dpi to dots/mm (*10/254 to avoid floating point)
  942. */
  943. static unsigned int elantech_convert_res(unsigned int val)
  944. {
  945. return (val * 10 + 790) * 10 / 254;
  946. }
  947. static int elantech_get_resolution_v4(struct psmouse *psmouse,
  948. unsigned int *x_res,
  949. unsigned int *y_res,
  950. unsigned int *bus)
  951. {
  952. unsigned char param[3];
  953. if (elantech_send_cmd(psmouse, ETP_RESOLUTION_QUERY, param))
  954. return -1;
  955. *x_res = elantech_convert_res(param[1] & 0x0f);
  956. *y_res = elantech_convert_res((param[1] & 0xf0) >> 4);
  957. *bus = param[2];
  958. return 0;
  959. }
  960. static void elantech_set_buttonpad_prop(struct psmouse *psmouse)
  961. {
  962. struct input_dev *dev = psmouse->dev;
  963. struct elantech_data *etd = psmouse->private;
  964. if (elantech_is_buttonpad(&etd->info)) {
  965. __set_bit(INPUT_PROP_BUTTONPAD, dev->propbit);
  966. __clear_bit(BTN_RIGHT, dev->keybit);
  967. }
  968. }
  969. /*
  970. * Some hw_version 4 models do have a middle button
  971. */
  972. static const struct dmi_system_id elantech_dmi_has_middle_button[] = {
  973. #if defined(CONFIG_DMI) && defined(CONFIG_X86)
  974. {
  975. /* Fujitsu H730 has a middle button */
  976. .matches = {
  977. DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"),
  978. DMI_MATCH(DMI_PRODUCT_NAME, "CELSIUS H730"),
  979. },
  980. },
  981. {
  982. /* Fujitsu H760 also has a middle button */
  983. .matches = {
  984. DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"),
  985. DMI_MATCH(DMI_PRODUCT_NAME, "CELSIUS H760"),
  986. },
  987. },
  988. {
  989. /* Fujitsu H780 also has a middle button */
  990. .matches = {
  991. DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"),
  992. DMI_MATCH(DMI_PRODUCT_NAME, "CELSIUS H780"),
  993. },
  994. },
  995. #endif
  996. { }
  997. };
  998. /*
  999. * Set the appropriate event bits for the input subsystem
  1000. */
  1001. static int elantech_set_input_params(struct psmouse *psmouse)
  1002. {
  1003. struct input_dev *dev = psmouse->dev;
  1004. struct elantech_data *etd = psmouse->private;
  1005. struct elantech_device_info *info = &etd->info;
  1006. unsigned int x_min = info->x_min, y_min = info->y_min,
  1007. x_max = info->x_max, y_max = info->y_max,
  1008. width = info->width;
  1009. __set_bit(INPUT_PROP_POINTER, dev->propbit);
  1010. __set_bit(EV_KEY, dev->evbit);
  1011. __set_bit(EV_ABS, dev->evbit);
  1012. __clear_bit(EV_REL, dev->evbit);
  1013. __set_bit(BTN_LEFT, dev->keybit);
  1014. if (info->has_middle_button)
  1015. __set_bit(BTN_MIDDLE, dev->keybit);
  1016. __set_bit(BTN_RIGHT, dev->keybit);
  1017. __set_bit(BTN_TOUCH, dev->keybit);
  1018. __set_bit(BTN_TOOL_FINGER, dev->keybit);
  1019. __set_bit(BTN_TOOL_DOUBLETAP, dev->keybit);
  1020. __set_bit(BTN_TOOL_TRIPLETAP, dev->keybit);
  1021. switch (info->hw_version) {
  1022. case 1:
  1023. /* Rocker button */
  1024. if (info->fw_version < 0x020000 &&
  1025. (info->capabilities[0] & ETP_CAP_HAS_ROCKER)) {
  1026. __set_bit(BTN_FORWARD, dev->keybit);
  1027. __set_bit(BTN_BACK, dev->keybit);
  1028. }
  1029. input_set_abs_params(dev, ABS_X, x_min, x_max, 0, 0);
  1030. input_set_abs_params(dev, ABS_Y, y_min, y_max, 0, 0);
  1031. break;
  1032. case 2:
  1033. __set_bit(BTN_TOOL_QUADTAP, dev->keybit);
  1034. __set_bit(INPUT_PROP_SEMI_MT, dev->propbit);
  1035. fallthrough;
  1036. case 3:
  1037. if (info->hw_version == 3)
  1038. elantech_set_buttonpad_prop(psmouse);
  1039. input_set_abs_params(dev, ABS_X, x_min, x_max, 0, 0);
  1040. input_set_abs_params(dev, ABS_Y, y_min, y_max, 0, 0);
  1041. if (info->reports_pressure) {
  1042. input_set_abs_params(dev, ABS_PRESSURE, ETP_PMIN_V2,
  1043. ETP_PMAX_V2, 0, 0);
  1044. input_set_abs_params(dev, ABS_TOOL_WIDTH, ETP_WMIN_V2,
  1045. ETP_WMAX_V2, 0, 0);
  1046. }
  1047. input_mt_init_slots(dev, 2, INPUT_MT_SEMI_MT);
  1048. input_set_abs_params(dev, ABS_MT_POSITION_X, x_min, x_max, 0, 0);
  1049. input_set_abs_params(dev, ABS_MT_POSITION_Y, y_min, y_max, 0, 0);
  1050. break;
  1051. case 4:
  1052. elantech_set_buttonpad_prop(psmouse);
  1053. __set_bit(BTN_TOOL_QUADTAP, dev->keybit);
  1054. /* For X to recognize me as touchpad. */
  1055. input_set_abs_params(dev, ABS_X, x_min, x_max, 0, 0);
  1056. input_set_abs_params(dev, ABS_Y, y_min, y_max, 0, 0);
  1057. /*
  1058. * range of pressure and width is the same as v2,
  1059. * report ABS_PRESSURE, ABS_TOOL_WIDTH for compatibility.
  1060. */
  1061. input_set_abs_params(dev, ABS_PRESSURE, ETP_PMIN_V2,
  1062. ETP_PMAX_V2, 0, 0);
  1063. input_set_abs_params(dev, ABS_TOOL_WIDTH, ETP_WMIN_V2,
  1064. ETP_WMAX_V2, 0, 0);
  1065. /* Multitouch capable pad, up to 5 fingers. */
  1066. input_mt_init_slots(dev, ETP_MAX_FINGERS, 0);
  1067. input_set_abs_params(dev, ABS_MT_POSITION_X, x_min, x_max, 0, 0);
  1068. input_set_abs_params(dev, ABS_MT_POSITION_Y, y_min, y_max, 0, 0);
  1069. input_set_abs_params(dev, ABS_MT_PRESSURE, ETP_PMIN_V2,
  1070. ETP_PMAX_V2, 0, 0);
  1071. /*
  1072. * The firmware reports how many trace lines the finger spans,
  1073. * convert to surface unit as Protocol-B requires.
  1074. */
  1075. input_set_abs_params(dev, ABS_MT_TOUCH_MAJOR, 0,
  1076. ETP_WMAX_V2 * width, 0, 0);
  1077. break;
  1078. }
  1079. input_abs_set_res(dev, ABS_X, info->x_res);
  1080. input_abs_set_res(dev, ABS_Y, info->y_res);
  1081. if (info->hw_version > 1) {
  1082. input_abs_set_res(dev, ABS_MT_POSITION_X, info->x_res);
  1083. input_abs_set_res(dev, ABS_MT_POSITION_Y, info->y_res);
  1084. }
  1085. etd->y_max = y_max;
  1086. etd->width = width;
  1087. return 0;
  1088. }
  1089. struct elantech_attr_data {
  1090. size_t field_offset;
  1091. unsigned char reg;
  1092. };
  1093. /*
  1094. * Display a register value by reading a sysfs entry
  1095. */
  1096. static ssize_t elantech_show_int_attr(struct psmouse *psmouse, void *data,
  1097. char *buf)
  1098. {
  1099. struct elantech_data *etd = psmouse->private;
  1100. struct elantech_attr_data *attr = data;
  1101. unsigned char *reg = (unsigned char *) etd + attr->field_offset;
  1102. int rc = 0;
  1103. if (attr->reg)
  1104. rc = elantech_read_reg(psmouse, attr->reg, reg);
  1105. return sprintf(buf, "0x%02x\n", (attr->reg && rc) ? -1 : *reg);
  1106. }
  1107. /*
  1108. * Write a register value by writing a sysfs entry
  1109. */
  1110. static ssize_t elantech_set_int_attr(struct psmouse *psmouse,
  1111. void *data, const char *buf, size_t count)
  1112. {
  1113. struct elantech_data *etd = psmouse->private;
  1114. struct elantech_attr_data *attr = data;
  1115. unsigned char *reg = (unsigned char *) etd + attr->field_offset;
  1116. unsigned char value;
  1117. int err;
  1118. err = kstrtou8(buf, 16, &value);
  1119. if (err)
  1120. return err;
  1121. /* Do we need to preserve some bits for version 2 hardware too? */
  1122. if (etd->info.hw_version == 1) {
  1123. if (attr->reg == 0x10)
  1124. /* Force absolute mode always on */
  1125. value |= ETP_R10_ABSOLUTE_MODE;
  1126. else if (attr->reg == 0x11)
  1127. /* Force 4 byte mode always on */
  1128. value |= ETP_R11_4_BYTE_MODE;
  1129. }
  1130. if (!attr->reg || elantech_write_reg(psmouse, attr->reg, value) == 0)
  1131. *reg = value;
  1132. return count;
  1133. }
  1134. #define ELANTECH_INT_ATTR(_name, _register) \
  1135. static struct elantech_attr_data elantech_attr_##_name = { \
  1136. .field_offset = offsetof(struct elantech_data, _name), \
  1137. .reg = _register, \
  1138. }; \
  1139. PSMOUSE_DEFINE_ATTR(_name, 0644, \
  1140. &elantech_attr_##_name, \
  1141. elantech_show_int_attr, \
  1142. elantech_set_int_attr)
  1143. #define ELANTECH_INFO_ATTR(_name) \
  1144. static struct elantech_attr_data elantech_attr_##_name = { \
  1145. .field_offset = offsetof(struct elantech_data, info) + \
  1146. offsetof(struct elantech_device_info, _name), \
  1147. .reg = 0, \
  1148. }; \
  1149. PSMOUSE_DEFINE_ATTR(_name, 0644, \
  1150. &elantech_attr_##_name, \
  1151. elantech_show_int_attr, \
  1152. elantech_set_int_attr)
  1153. ELANTECH_INT_ATTR(reg_07, 0x07);
  1154. ELANTECH_INT_ATTR(reg_10, 0x10);
  1155. ELANTECH_INT_ATTR(reg_11, 0x11);
  1156. ELANTECH_INT_ATTR(reg_20, 0x20);
  1157. ELANTECH_INT_ATTR(reg_21, 0x21);
  1158. ELANTECH_INT_ATTR(reg_22, 0x22);
  1159. ELANTECH_INT_ATTR(reg_23, 0x23);
  1160. ELANTECH_INT_ATTR(reg_24, 0x24);
  1161. ELANTECH_INT_ATTR(reg_25, 0x25);
  1162. ELANTECH_INT_ATTR(reg_26, 0x26);
  1163. ELANTECH_INFO_ATTR(debug);
  1164. ELANTECH_INFO_ATTR(paritycheck);
  1165. ELANTECH_INFO_ATTR(crc_enabled);
  1166. static struct attribute *elantech_attrs[] = {
  1167. &psmouse_attr_reg_07.dattr.attr,
  1168. &psmouse_attr_reg_10.dattr.attr,
  1169. &psmouse_attr_reg_11.dattr.attr,
  1170. &psmouse_attr_reg_20.dattr.attr,
  1171. &psmouse_attr_reg_21.dattr.attr,
  1172. &psmouse_attr_reg_22.dattr.attr,
  1173. &psmouse_attr_reg_23.dattr.attr,
  1174. &psmouse_attr_reg_24.dattr.attr,
  1175. &psmouse_attr_reg_25.dattr.attr,
  1176. &psmouse_attr_reg_26.dattr.attr,
  1177. &psmouse_attr_debug.dattr.attr,
  1178. &psmouse_attr_paritycheck.dattr.attr,
  1179. &psmouse_attr_crc_enabled.dattr.attr,
  1180. NULL
  1181. };
  1182. static const struct attribute_group elantech_attr_group = {
  1183. .attrs = elantech_attrs,
  1184. };
  1185. static bool elantech_is_signature_valid(const unsigned char *param)
  1186. {
  1187. static const unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10 };
  1188. int i;
  1189. if (param[0] == 0)
  1190. return false;
  1191. if (param[1] == 0)
  1192. return true;
  1193. /*
  1194. * Some hw_version >= 4 models have a revision higher then 20. Meaning
  1195. * that param[2] may be 10 or 20, skip the rates check for these.
  1196. */
  1197. if ((param[0] & 0x0f) >= 0x06 && (param[1] & 0xaf) == 0x0f &&
  1198. param[2] < 40)
  1199. return true;
  1200. for (i = 0; i < ARRAY_SIZE(rates); i++)
  1201. if (param[2] == rates[i])
  1202. return false;
  1203. return true;
  1204. }
  1205. /*
  1206. * Use magic knock to detect Elantech touchpad
  1207. */
  1208. int elantech_detect(struct psmouse *psmouse, bool set_properties)
  1209. {
  1210. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1211. unsigned char param[3];
  1212. ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
  1213. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  1214. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  1215. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  1216. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  1217. ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) {
  1218. psmouse_dbg(psmouse, "sending Elantech magic knock failed.\n");
  1219. return -1;
  1220. }
  1221. /*
  1222. * Report this in case there are Elantech models that use a different
  1223. * set of magic numbers
  1224. */
  1225. if (param[0] != 0x3c || param[1] != 0x03 ||
  1226. (param[2] != 0xc8 && param[2] != 0x00)) {
  1227. psmouse_dbg(psmouse,
  1228. "unexpected magic knock result 0x%02x, 0x%02x, 0x%02x.\n",
  1229. param[0], param[1], param[2]);
  1230. return -1;
  1231. }
  1232. /*
  1233. * Query touchpad's firmware version and see if it reports known
  1234. * value to avoid mis-detection. Logitech mice are known to respond
  1235. * to Elantech magic knock and there might be more.
  1236. */
  1237. if (synaptics_send_cmd(psmouse, ETP_FW_VERSION_QUERY, param)) {
  1238. psmouse_dbg(psmouse, "failed to query firmware version.\n");
  1239. return -1;
  1240. }
  1241. psmouse_dbg(psmouse,
  1242. "Elantech version query result 0x%02x, 0x%02x, 0x%02x.\n",
  1243. param[0], param[1], param[2]);
  1244. if (!elantech_is_signature_valid(param)) {
  1245. psmouse_dbg(psmouse,
  1246. "Probably not a real Elantech touchpad. Aborting.\n");
  1247. return -1;
  1248. }
  1249. if (set_properties) {
  1250. psmouse->vendor = "Elantech";
  1251. psmouse->name = "Touchpad";
  1252. }
  1253. return 0;
  1254. }
  1255. /*
  1256. * Clean up sysfs entries when disconnecting
  1257. */
  1258. static void elantech_disconnect(struct psmouse *psmouse)
  1259. {
  1260. struct elantech_data *etd = psmouse->private;
  1261. /*
  1262. * We might have left a breadcrumb when trying to
  1263. * set up SMbus companion.
  1264. */
  1265. psmouse_smbus_cleanup(psmouse);
  1266. if (etd->tp_dev)
  1267. input_unregister_device(etd->tp_dev);
  1268. sysfs_remove_group(&psmouse->ps2dev.serio->dev.kobj,
  1269. &elantech_attr_group);
  1270. kfree(psmouse->private);
  1271. psmouse->private = NULL;
  1272. }
  1273. /*
  1274. * Put the touchpad back into absolute mode when reconnecting
  1275. */
  1276. static int elantech_reconnect(struct psmouse *psmouse)
  1277. {
  1278. psmouse_reset(psmouse);
  1279. if (elantech_detect(psmouse, 0))
  1280. return -1;
  1281. if (elantech_set_absolute_mode(psmouse)) {
  1282. psmouse_err(psmouse,
  1283. "failed to put touchpad back into absolute mode.\n");
  1284. return -1;
  1285. }
  1286. return 0;
  1287. }
  1288. /*
  1289. * Some hw_version 4 models do not work with crc_disabled
  1290. */
  1291. static const struct dmi_system_id elantech_dmi_force_crc_enabled[] = {
  1292. #if defined(CONFIG_DMI) && defined(CONFIG_X86)
  1293. {
  1294. /* Fujitsu H730 does not work with crc_enabled == 0 */
  1295. .matches = {
  1296. DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"),
  1297. DMI_MATCH(DMI_PRODUCT_NAME, "CELSIUS H730"),
  1298. },
  1299. },
  1300. {
  1301. /* Fujitsu H760 does not work with crc_enabled == 0 */
  1302. .matches = {
  1303. DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"),
  1304. DMI_MATCH(DMI_PRODUCT_NAME, "CELSIUS H760"),
  1305. },
  1306. },
  1307. {
  1308. /* Fujitsu LIFEBOOK E544 does not work with crc_enabled == 0 */
  1309. .matches = {
  1310. DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"),
  1311. DMI_MATCH(DMI_PRODUCT_NAME, "LIFEBOOK E544"),
  1312. },
  1313. },
  1314. {
  1315. /* Fujitsu LIFEBOOK E546 does not work with crc_enabled == 0 */
  1316. .matches = {
  1317. DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"),
  1318. DMI_MATCH(DMI_PRODUCT_NAME, "LIFEBOOK E546"),
  1319. },
  1320. },
  1321. {
  1322. /* Fujitsu LIFEBOOK E547 does not work with crc_enabled == 0 */
  1323. .matches = {
  1324. DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"),
  1325. DMI_MATCH(DMI_PRODUCT_NAME, "LIFEBOOK E547"),
  1326. },
  1327. },
  1328. {
  1329. /* Fujitsu LIFEBOOK E554 does not work with crc_enabled == 0 */
  1330. .matches = {
  1331. DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"),
  1332. DMI_MATCH(DMI_PRODUCT_NAME, "LIFEBOOK E554"),
  1333. },
  1334. },
  1335. {
  1336. /* Fujitsu LIFEBOOK E556 does not work with crc_enabled == 0 */
  1337. .matches = {
  1338. DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"),
  1339. DMI_MATCH(DMI_PRODUCT_NAME, "LIFEBOOK E556"),
  1340. },
  1341. },
  1342. {
  1343. /* Fujitsu LIFEBOOK E557 does not work with crc_enabled == 0 */
  1344. .matches = {
  1345. DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"),
  1346. DMI_MATCH(DMI_PRODUCT_NAME, "LIFEBOOK E557"),
  1347. },
  1348. },
  1349. {
  1350. /* Fujitsu LIFEBOOK U745 does not work with crc_enabled == 0 */
  1351. .matches = {
  1352. DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"),
  1353. DMI_MATCH(DMI_PRODUCT_NAME, "LIFEBOOK U745"),
  1354. },
  1355. },
  1356. #endif
  1357. { }
  1358. };
  1359. /*
  1360. * Some hw_version 3 models go into error state when we try to set
  1361. * bit 3 and/or bit 1 of r10.
  1362. */
  1363. static const struct dmi_system_id no_hw_res_dmi_table[] = {
  1364. #if defined(CONFIG_DMI) && defined(CONFIG_X86)
  1365. {
  1366. /* Gigabyte U2442 */
  1367. .matches = {
  1368. DMI_MATCH(DMI_SYS_VENDOR, "GIGABYTE"),
  1369. DMI_MATCH(DMI_PRODUCT_NAME, "U2442"),
  1370. },
  1371. },
  1372. #endif
  1373. { }
  1374. };
  1375. /*
  1376. * Change Report id 0x5E to 0x5F.
  1377. */
  1378. static int elantech_change_report_id(struct psmouse *psmouse)
  1379. {
  1380. /*
  1381. * NOTE: the code is expecting to receive param[] as an array of 3
  1382. * items (see __ps2_command()), even if in this case only 2 are
  1383. * actually needed. Make sure the array size is 3 to avoid potential
  1384. * stack out-of-bound accesses.
  1385. */
  1386. unsigned char param[3] = { 0x10, 0x03 };
  1387. if (elantech_write_reg_params(psmouse, 0x7, param) ||
  1388. elantech_read_reg_params(psmouse, 0x7, param) ||
  1389. param[0] != 0x10 || param[1] != 0x03) {
  1390. psmouse_err(psmouse, "Unable to change report ID to 0x5f.\n");
  1391. return -EIO;
  1392. }
  1393. return 0;
  1394. }
  1395. /*
  1396. * determine hardware version and set some properties according to it.
  1397. */
  1398. static int elantech_set_properties(struct elantech_device_info *info)
  1399. {
  1400. /* This represents the version of IC body. */
  1401. info->ic_version = (info->fw_version & 0x0f0000) >> 16;
  1402. /* Early version of Elan touchpads doesn't obey the rule. */
  1403. if (info->fw_version < 0x020030 || info->fw_version == 0x020600)
  1404. info->hw_version = 1;
  1405. else {
  1406. switch (info->ic_version) {
  1407. case 2:
  1408. case 4:
  1409. info->hw_version = 2;
  1410. break;
  1411. case 5:
  1412. info->hw_version = 3;
  1413. break;
  1414. case 6 ... 15:
  1415. info->hw_version = 4;
  1416. break;
  1417. default:
  1418. return -1;
  1419. }
  1420. }
  1421. /* Get information pattern for hw_version 4 */
  1422. info->pattern = 0x00;
  1423. if (info->ic_version == 0x0f && (info->fw_version & 0xff) <= 0x02)
  1424. info->pattern = info->fw_version & 0xff;
  1425. /* decide which send_cmd we're gonna use early */
  1426. info->send_cmd = info->hw_version >= 3 ? elantech_send_cmd :
  1427. synaptics_send_cmd;
  1428. /* Turn on packet checking by default */
  1429. info->paritycheck = 1;
  1430. /*
  1431. * This firmware suffers from misreporting coordinates when
  1432. * a touch action starts causing the mouse cursor or scrolled page
  1433. * to jump. Enable a workaround.
  1434. */
  1435. info->jumpy_cursor =
  1436. (info->fw_version == 0x020022 || info->fw_version == 0x020600);
  1437. if (info->hw_version > 1) {
  1438. /* For now show extra debug information */
  1439. info->debug = 1;
  1440. if (info->fw_version >= 0x020800)
  1441. info->reports_pressure = true;
  1442. }
  1443. /*
  1444. * The signatures of v3 and v4 packets change depending on the
  1445. * value of this hardware flag.
  1446. */
  1447. info->crc_enabled = (info->fw_version & 0x4000) == 0x4000 ||
  1448. dmi_check_system(elantech_dmi_force_crc_enabled);
  1449. /* Enable real hardware resolution on hw_version 3 ? */
  1450. info->set_hw_resolution = !dmi_check_system(no_hw_res_dmi_table);
  1451. return 0;
  1452. }
  1453. static int elantech_query_info(struct psmouse *psmouse,
  1454. struct elantech_device_info *info)
  1455. {
  1456. unsigned char param[3];
  1457. unsigned char traces;
  1458. unsigned char ic_body[3];
  1459. memset(info, 0, sizeof(*info));
  1460. /*
  1461. * Do the version query again so we can store the result
  1462. */
  1463. if (synaptics_send_cmd(psmouse, ETP_FW_VERSION_QUERY, param)) {
  1464. psmouse_err(psmouse, "failed to query firmware version.\n");
  1465. return -EINVAL;
  1466. }
  1467. info->fw_version = (param[0] << 16) | (param[1] << 8) | param[2];
  1468. if (elantech_set_properties(info)) {
  1469. psmouse_err(psmouse, "unknown hardware version, aborting...\n");
  1470. return -EINVAL;
  1471. }
  1472. psmouse_info(psmouse,
  1473. "assuming hardware version %d (with firmware version 0x%02x%02x%02x)\n",
  1474. info->hw_version, param[0], param[1], param[2]);
  1475. if (info->send_cmd(psmouse, ETP_CAPABILITIES_QUERY,
  1476. info->capabilities)) {
  1477. psmouse_err(psmouse, "failed to query capabilities.\n");
  1478. return -EINVAL;
  1479. }
  1480. psmouse_info(psmouse,
  1481. "Synaptics capabilities query result 0x%02x, 0x%02x, 0x%02x.\n",
  1482. info->capabilities[0], info->capabilities[1],
  1483. info->capabilities[2]);
  1484. if (info->hw_version != 1) {
  1485. if (info->send_cmd(psmouse, ETP_SAMPLE_QUERY, info->samples)) {
  1486. psmouse_err(psmouse, "failed to query sample data\n");
  1487. return -EINVAL;
  1488. }
  1489. psmouse_info(psmouse,
  1490. "Elan sample query result %02x, %02x, %02x\n",
  1491. info->samples[0],
  1492. info->samples[1],
  1493. info->samples[2]);
  1494. }
  1495. if (info->pattern > 0x00 && info->ic_version == 0xf) {
  1496. if (info->send_cmd(psmouse, ETP_ICBODY_QUERY, ic_body)) {
  1497. psmouse_err(psmouse, "failed to query ic body\n");
  1498. return -EINVAL;
  1499. }
  1500. info->ic_version = be16_to_cpup((__be16 *)ic_body);
  1501. psmouse_info(psmouse,
  1502. "Elan ic body: %#04x, current fw version: %#02x\n",
  1503. info->ic_version, ic_body[2]);
  1504. }
  1505. info->product_id = be16_to_cpup((__be16 *)info->samples);
  1506. if (info->pattern == 0x00)
  1507. info->product_id &= 0xff;
  1508. if (info->samples[1] == 0x74 && info->hw_version == 0x03) {
  1509. /*
  1510. * This module has a bug which makes absolute mode
  1511. * unusable, so let's abort so we'll be using standard
  1512. * PS/2 protocol.
  1513. */
  1514. psmouse_info(psmouse,
  1515. "absolute mode broken, forcing standard PS/2 protocol\n");
  1516. return -ENODEV;
  1517. }
  1518. /* The MSB indicates the presence of the trackpoint */
  1519. info->has_trackpoint = (info->capabilities[0] & 0x80) == 0x80;
  1520. if (info->has_trackpoint && info->ic_version == 0x0011 &&
  1521. (info->product_id == 0x08 || info->product_id == 0x09 ||
  1522. info->product_id == 0x0d || info->product_id == 0x0e)) {
  1523. /*
  1524. * This module has a bug which makes trackpoint in SMBus
  1525. * mode return invalid data unless trackpoint is switched
  1526. * from using 0x5e reports to 0x5f. If we are not able to
  1527. * make the switch, let's abort initialization so we'll be
  1528. * using standard PS/2 protocol.
  1529. */
  1530. if (elantech_change_report_id(psmouse)) {
  1531. psmouse_info(psmouse,
  1532. "Trackpoint report is broken, forcing standard PS/2 protocol\n");
  1533. return -ENODEV;
  1534. }
  1535. }
  1536. info->x_res = 31;
  1537. info->y_res = 31;
  1538. if (info->hw_version == 4) {
  1539. if (elantech_get_resolution_v4(psmouse,
  1540. &info->x_res,
  1541. &info->y_res,
  1542. &info->bus)) {
  1543. psmouse_warn(psmouse,
  1544. "failed to query resolution data.\n");
  1545. }
  1546. }
  1547. /* query range information */
  1548. switch (info->hw_version) {
  1549. case 1:
  1550. info->x_min = ETP_XMIN_V1;
  1551. info->y_min = ETP_YMIN_V1;
  1552. info->x_max = ETP_XMAX_V1;
  1553. info->y_max = ETP_YMAX_V1;
  1554. break;
  1555. case 2:
  1556. if (info->fw_version == 0x020800 ||
  1557. info->fw_version == 0x020b00 ||
  1558. info->fw_version == 0x020030) {
  1559. info->x_min = ETP_XMIN_V2;
  1560. info->y_min = ETP_YMIN_V2;
  1561. info->x_max = ETP_XMAX_V2;
  1562. info->y_max = ETP_YMAX_V2;
  1563. } else {
  1564. int i;
  1565. int fixed_dpi;
  1566. i = (info->fw_version > 0x020800 &&
  1567. info->fw_version < 0x020900) ? 1 : 2;
  1568. if (info->send_cmd(psmouse, ETP_FW_ID_QUERY, param))
  1569. return -EINVAL;
  1570. fixed_dpi = param[1] & 0x10;
  1571. if (((info->fw_version >> 16) == 0x14) && fixed_dpi) {
  1572. if (info->send_cmd(psmouse, ETP_SAMPLE_QUERY, param))
  1573. return -EINVAL;
  1574. info->x_max = (info->capabilities[1] - i) * param[1] / 2;
  1575. info->y_max = (info->capabilities[2] - i) * param[2] / 2;
  1576. } else if (info->fw_version == 0x040216) {
  1577. info->x_max = 819;
  1578. info->y_max = 405;
  1579. } else if (info->fw_version == 0x040219 || info->fw_version == 0x040215) {
  1580. info->x_max = 900;
  1581. info->y_max = 500;
  1582. } else {
  1583. info->x_max = (info->capabilities[1] - i) * 64;
  1584. info->y_max = (info->capabilities[2] - i) * 64;
  1585. }
  1586. }
  1587. break;
  1588. case 3:
  1589. if (info->send_cmd(psmouse, ETP_FW_ID_QUERY, param))
  1590. return -EINVAL;
  1591. info->x_max = (0x0f & param[0]) << 8 | param[1];
  1592. info->y_max = (0xf0 & param[0]) << 4 | param[2];
  1593. break;
  1594. case 4:
  1595. if (info->send_cmd(psmouse, ETP_FW_ID_QUERY, param))
  1596. return -EINVAL;
  1597. info->x_max = (0x0f & param[0]) << 8 | param[1];
  1598. info->y_max = (0xf0 & param[0]) << 4 | param[2];
  1599. traces = info->capabilities[1];
  1600. if ((traces < 2) || (traces > info->x_max))
  1601. return -EINVAL;
  1602. info->width = info->x_max / (traces - 1);
  1603. /* column number of traces */
  1604. info->x_traces = traces;
  1605. /* row number of traces */
  1606. traces = info->capabilities[2];
  1607. if ((traces >= 2) && (traces <= info->y_max))
  1608. info->y_traces = traces;
  1609. break;
  1610. }
  1611. /* check for the middle button: DMI matching or new v4 firmwares */
  1612. info->has_middle_button = dmi_check_system(elantech_dmi_has_middle_button) ||
  1613. (ETP_NEW_IC_SMBUS_HOST_NOTIFY(info->fw_version) &&
  1614. !elantech_is_buttonpad(info));
  1615. return 0;
  1616. }
  1617. #if defined(CONFIG_MOUSE_PS2_ELANTECH_SMBUS)
  1618. /*
  1619. * The newest Elantech device can use a secondary bus (over SMBus) which
  1620. * provides a better bandwidth and allow a better control of the touchpads.
  1621. * This is used to decide if we need to use this bus or not.
  1622. */
  1623. enum {
  1624. ELANTECH_SMBUS_NOT_SET = -1,
  1625. ELANTECH_SMBUS_OFF,
  1626. ELANTECH_SMBUS_ON,
  1627. };
  1628. static int elantech_smbus = IS_ENABLED(CONFIG_MOUSE_ELAN_I2C_SMBUS) ?
  1629. ELANTECH_SMBUS_NOT_SET : ELANTECH_SMBUS_OFF;
  1630. module_param_named(elantech_smbus, elantech_smbus, int, 0644);
  1631. MODULE_PARM_DESC(elantech_smbus, "Use a secondary bus for the Elantech device.");
  1632. static const char * const i2c_blacklist_pnp_ids[] = {
  1633. /*
  1634. * These are known to not be working properly as bits are missing
  1635. * in elan_i2c.
  1636. */
  1637. NULL
  1638. };
  1639. static int elantech_create_smbus(struct psmouse *psmouse,
  1640. struct elantech_device_info *info,
  1641. bool leave_breadcrumbs)
  1642. {
  1643. struct property_entry i2c_props[11] = {};
  1644. struct i2c_board_info smbus_board = {
  1645. I2C_BOARD_INFO("elan_i2c", 0x15),
  1646. .flags = I2C_CLIENT_HOST_NOTIFY,
  1647. };
  1648. unsigned int idx = 0;
  1649. smbus_board.properties = i2c_props;
  1650. i2c_props[idx++] = PROPERTY_ENTRY_U32("touchscreen-size-x",
  1651. info->x_max + 1);
  1652. i2c_props[idx++] = PROPERTY_ENTRY_U32("touchscreen-size-y",
  1653. info->y_max + 1);
  1654. i2c_props[idx++] = PROPERTY_ENTRY_U32("touchscreen-min-x",
  1655. info->x_min);
  1656. i2c_props[idx++] = PROPERTY_ENTRY_U32("touchscreen-min-y",
  1657. info->y_min);
  1658. if (info->x_res)
  1659. i2c_props[idx++] = PROPERTY_ENTRY_U32("touchscreen-x-mm",
  1660. (info->x_max + 1) / info->x_res);
  1661. if (info->y_res)
  1662. i2c_props[idx++] = PROPERTY_ENTRY_U32("touchscreen-y-mm",
  1663. (info->y_max + 1) / info->y_res);
  1664. if (info->has_trackpoint)
  1665. i2c_props[idx++] = PROPERTY_ENTRY_BOOL("elan,trackpoint");
  1666. if (info->has_middle_button)
  1667. i2c_props[idx++] = PROPERTY_ENTRY_BOOL("elan,middle-button");
  1668. if (info->x_traces)
  1669. i2c_props[idx++] = PROPERTY_ENTRY_U32("elan,x_traces",
  1670. info->x_traces);
  1671. if (info->y_traces)
  1672. i2c_props[idx++] = PROPERTY_ENTRY_U32("elan,y_traces",
  1673. info->y_traces);
  1674. if (elantech_is_buttonpad(info))
  1675. i2c_props[idx++] = PROPERTY_ENTRY_BOOL("elan,clickpad");
  1676. return psmouse_smbus_init(psmouse, &smbus_board, NULL, 0, false,
  1677. leave_breadcrumbs);
  1678. }
  1679. /**
  1680. * elantech_setup_smbus - called once the PS/2 devices are enumerated
  1681. * and decides to instantiate a SMBus InterTouch device.
  1682. */
  1683. static int elantech_setup_smbus(struct psmouse *psmouse,
  1684. struct elantech_device_info *info,
  1685. bool leave_breadcrumbs)
  1686. {
  1687. int error;
  1688. if (elantech_smbus == ELANTECH_SMBUS_OFF)
  1689. return -ENXIO;
  1690. if (elantech_smbus == ELANTECH_SMBUS_NOT_SET) {
  1691. /*
  1692. * New ICs are enabled by default, unless mentioned in
  1693. * i2c_blacklist_pnp_ids.
  1694. * Old ICs are up to the user to decide.
  1695. */
  1696. if (!ETP_NEW_IC_SMBUS_HOST_NOTIFY(info->fw_version) ||
  1697. psmouse_matches_pnp_id(psmouse, i2c_blacklist_pnp_ids))
  1698. return -ENXIO;
  1699. }
  1700. psmouse_info(psmouse, "Trying to set up SMBus access\n");
  1701. error = elantech_create_smbus(psmouse, info, leave_breadcrumbs);
  1702. if (error) {
  1703. if (error == -EAGAIN)
  1704. psmouse_info(psmouse, "SMbus companion is not ready yet\n");
  1705. else
  1706. psmouse_err(psmouse, "unable to create intertouch device\n");
  1707. return error;
  1708. }
  1709. return 0;
  1710. }
  1711. static bool elantech_use_host_notify(struct psmouse *psmouse,
  1712. struct elantech_device_info *info)
  1713. {
  1714. if (ETP_NEW_IC_SMBUS_HOST_NOTIFY(info->fw_version))
  1715. return true;
  1716. switch (info->bus) {
  1717. case ETP_BUS_PS2_ONLY:
  1718. /* expected case */
  1719. break;
  1720. case ETP_BUS_SMB_ALERT_ONLY:
  1721. case ETP_BUS_PS2_SMB_ALERT:
  1722. psmouse_dbg(psmouse, "Ignoring SMBus provider through alert protocol.\n");
  1723. break;
  1724. case ETP_BUS_SMB_HST_NTFY_ONLY:
  1725. case ETP_BUS_PS2_SMB_HST_NTFY:
  1726. return true;
  1727. default:
  1728. psmouse_dbg(psmouse,
  1729. "Ignoring SMBus bus provider %d.\n",
  1730. info->bus);
  1731. }
  1732. return false;
  1733. }
  1734. int elantech_init_smbus(struct psmouse *psmouse)
  1735. {
  1736. struct elantech_device_info info;
  1737. int error;
  1738. psmouse_reset(psmouse);
  1739. error = elantech_query_info(psmouse, &info);
  1740. if (error)
  1741. goto init_fail;
  1742. if (info.hw_version < 4) {
  1743. error = -ENXIO;
  1744. goto init_fail;
  1745. }
  1746. return elantech_create_smbus(psmouse, &info, false);
  1747. init_fail:
  1748. psmouse_reset(psmouse);
  1749. return error;
  1750. }
  1751. #endif /* CONFIG_MOUSE_PS2_ELANTECH_SMBUS */
  1752. /*
  1753. * Initialize the touchpad and create sysfs entries
  1754. */
  1755. static int elantech_setup_ps2(struct psmouse *psmouse,
  1756. struct elantech_device_info *info)
  1757. {
  1758. struct elantech_data *etd;
  1759. int i;
  1760. int error = -EINVAL;
  1761. struct input_dev *tp_dev;
  1762. psmouse->private = etd = kzalloc(sizeof(*etd), GFP_KERNEL);
  1763. if (!etd)
  1764. return -ENOMEM;
  1765. etd->info = *info;
  1766. etd->parity[0] = 1;
  1767. for (i = 1; i < 256; i++)
  1768. etd->parity[i] = etd->parity[i & (i - 1)] ^ 1;
  1769. if (elantech_set_absolute_mode(psmouse)) {
  1770. psmouse_err(psmouse,
  1771. "failed to put touchpad into absolute mode.\n");
  1772. goto init_fail;
  1773. }
  1774. if (info->fw_version == 0x381f17) {
  1775. etd->original_set_rate = psmouse->set_rate;
  1776. psmouse->set_rate = elantech_set_rate_restore_reg_07;
  1777. }
  1778. if (elantech_set_input_params(psmouse)) {
  1779. psmouse_err(psmouse, "failed to query touchpad range.\n");
  1780. goto init_fail;
  1781. }
  1782. error = sysfs_create_group(&psmouse->ps2dev.serio->dev.kobj,
  1783. &elantech_attr_group);
  1784. if (error) {
  1785. psmouse_err(psmouse,
  1786. "failed to create sysfs attributes, error: %d.\n",
  1787. error);
  1788. goto init_fail;
  1789. }
  1790. if (info->has_trackpoint) {
  1791. tp_dev = input_allocate_device();
  1792. if (!tp_dev) {
  1793. error = -ENOMEM;
  1794. goto init_fail_tp_alloc;
  1795. }
  1796. etd->tp_dev = tp_dev;
  1797. snprintf(etd->tp_phys, sizeof(etd->tp_phys), "%s/input1",
  1798. psmouse->ps2dev.serio->phys);
  1799. tp_dev->phys = etd->tp_phys;
  1800. tp_dev->name = "ETPS/2 Elantech TrackPoint";
  1801. tp_dev->id.bustype = BUS_I8042;
  1802. tp_dev->id.vendor = 0x0002;
  1803. tp_dev->id.product = PSMOUSE_ELANTECH;
  1804. tp_dev->id.version = 0x0000;
  1805. tp_dev->dev.parent = &psmouse->ps2dev.serio->dev;
  1806. tp_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
  1807. tp_dev->relbit[BIT_WORD(REL_X)] =
  1808. BIT_MASK(REL_X) | BIT_MASK(REL_Y);
  1809. tp_dev->keybit[BIT_WORD(BTN_LEFT)] =
  1810. BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_MIDDLE) |
  1811. BIT_MASK(BTN_RIGHT);
  1812. __set_bit(INPUT_PROP_POINTER, tp_dev->propbit);
  1813. __set_bit(INPUT_PROP_POINTING_STICK, tp_dev->propbit);
  1814. error = input_register_device(etd->tp_dev);
  1815. if (error < 0)
  1816. goto init_fail_tp_reg;
  1817. }
  1818. psmouse->protocol_handler = elantech_process_byte;
  1819. psmouse->disconnect = elantech_disconnect;
  1820. psmouse->reconnect = elantech_reconnect;
  1821. psmouse->pktsize = info->hw_version > 1 ? 6 : 4;
  1822. return 0;
  1823. init_fail_tp_reg:
  1824. input_free_device(tp_dev);
  1825. init_fail_tp_alloc:
  1826. sysfs_remove_group(&psmouse->ps2dev.serio->dev.kobj,
  1827. &elantech_attr_group);
  1828. init_fail:
  1829. kfree(etd);
  1830. return error;
  1831. }
  1832. int elantech_init_ps2(struct psmouse *psmouse)
  1833. {
  1834. struct elantech_device_info info;
  1835. int error;
  1836. psmouse_reset(psmouse);
  1837. error = elantech_query_info(psmouse, &info);
  1838. if (error)
  1839. goto init_fail;
  1840. error = elantech_setup_ps2(psmouse, &info);
  1841. if (error)
  1842. goto init_fail;
  1843. return 0;
  1844. init_fail:
  1845. psmouse_reset(psmouse);
  1846. return error;
  1847. }
  1848. int elantech_init(struct psmouse *psmouse)
  1849. {
  1850. struct elantech_device_info info;
  1851. int error;
  1852. psmouse_reset(psmouse);
  1853. error = elantech_query_info(psmouse, &info);
  1854. if (error)
  1855. goto init_fail;
  1856. #if defined(CONFIG_MOUSE_PS2_ELANTECH_SMBUS)
  1857. if (elantech_use_host_notify(psmouse, &info)) {
  1858. if (!IS_ENABLED(CONFIG_MOUSE_ELAN_I2C_SMBUS) ||
  1859. !IS_ENABLED(CONFIG_MOUSE_PS2_ELANTECH_SMBUS)) {
  1860. psmouse_warn(psmouse,
  1861. "The touchpad can support a better bus than the too old PS/2 protocol. "
  1862. "Make sure MOUSE_PS2_ELANTECH_SMBUS and MOUSE_ELAN_I2C_SMBUS are enabled to get a better touchpad experience.\n");
  1863. }
  1864. error = elantech_setup_smbus(psmouse, &info, true);
  1865. if (!error)
  1866. return PSMOUSE_ELANTECH_SMBUS;
  1867. }
  1868. #endif /* CONFIG_MOUSE_PS2_ELANTECH_SMBUS */
  1869. error = elantech_setup_ps2(psmouse, &info);
  1870. if (error < 0) {
  1871. /*
  1872. * Not using any flavor of Elantech support, so clean up
  1873. * SMbus breadcrumbs, if any.
  1874. */
  1875. psmouse_smbus_cleanup(psmouse);
  1876. goto init_fail;
  1877. }
  1878. return PSMOUSE_ELANTECH;
  1879. init_fail:
  1880. psmouse_reset(psmouse);
  1881. return error;
  1882. }