elantech.c 55 KB

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