elantech.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324
  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/slab.h>
  14. #include <linux/module.h>
  15. #include <linux/input.h>
  16. #include <linux/input/mt.h>
  17. #include <linux/serio.h>
  18. #include <linux/libps2.h>
  19. #include "psmouse.h"
  20. #include "elantech.h"
  21. #define elantech_debug(fmt, ...) \
  22. do { \
  23. if (etd->debug) \
  24. psmouse_printk(KERN_DEBUG, psmouse, \
  25. fmt, ##__VA_ARGS__); \
  26. } while (0)
  27. /*
  28. * Send a Synaptics style sliced query command
  29. */
  30. static int synaptics_send_cmd(struct psmouse *psmouse, unsigned char c,
  31. unsigned char *param)
  32. {
  33. if (psmouse_sliced_command(psmouse, c) ||
  34. ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_GETINFO)) {
  35. psmouse_err(psmouse, "%s query 0x%02x failed.\n", __func__, c);
  36. return -1;
  37. }
  38. return 0;
  39. }
  40. /*
  41. * A retrying version of ps2_command
  42. */
  43. static int elantech_ps2_command(struct psmouse *psmouse,
  44. unsigned char *param, int command)
  45. {
  46. struct ps2dev *ps2dev = &psmouse->ps2dev;
  47. struct elantech_data *etd = psmouse->private;
  48. int rc;
  49. int tries = ETP_PS2_COMMAND_TRIES;
  50. do {
  51. rc = ps2_command(ps2dev, param, command);
  52. if (rc == 0)
  53. break;
  54. tries--;
  55. elantech_debug("retrying ps2 command 0x%02x (%d).\n",
  56. command, tries);
  57. msleep(ETP_PS2_COMMAND_DELAY);
  58. } while (tries > 0);
  59. if (rc)
  60. psmouse_err(psmouse, "ps2 command 0x%02x failed.\n", command);
  61. return rc;
  62. }
  63. /*
  64. * Send an Elantech style special command to read a value from a register
  65. */
  66. static int elantech_read_reg(struct psmouse *psmouse, unsigned char reg,
  67. unsigned char *val)
  68. {
  69. struct elantech_data *etd = psmouse->private;
  70. unsigned char param[3];
  71. int rc = 0;
  72. if (reg < 0x07 || reg > 0x26)
  73. return -1;
  74. if (reg > 0x11 && reg < 0x20)
  75. return -1;
  76. switch (etd->hw_version) {
  77. case 1:
  78. if (psmouse_sliced_command(psmouse, ETP_REGISTER_READ) ||
  79. psmouse_sliced_command(psmouse, reg) ||
  80. ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_GETINFO)) {
  81. rc = -1;
  82. }
  83. break;
  84. case 2:
  85. if (elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
  86. elantech_ps2_command(psmouse, NULL, ETP_REGISTER_READ) ||
  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. rc = -1;
  91. }
  92. break;
  93. case 3 ... 4:
  94. if (elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
  95. elantech_ps2_command(psmouse, NULL, ETP_REGISTER_READWRITE) ||
  96. elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
  97. elantech_ps2_command(psmouse, NULL, reg) ||
  98. elantech_ps2_command(psmouse, param, PSMOUSE_CMD_GETINFO)) {
  99. rc = -1;
  100. }
  101. break;
  102. }
  103. if (rc)
  104. psmouse_err(psmouse, "failed to read register 0x%02x.\n", reg);
  105. else if (etd->hw_version != 4)
  106. *val = param[0];
  107. else
  108. *val = param[1];
  109. return rc;
  110. }
  111. /*
  112. * Send an Elantech style special command to write a register with a value
  113. */
  114. static int elantech_write_reg(struct psmouse *psmouse, unsigned char reg,
  115. unsigned char val)
  116. {
  117. struct elantech_data *etd = psmouse->private;
  118. int rc = 0;
  119. if (reg < 0x07 || reg > 0x26)
  120. return -1;
  121. if (reg > 0x11 && reg < 0x20)
  122. return -1;
  123. switch (etd->hw_version) {
  124. case 1:
  125. if (psmouse_sliced_command(psmouse, ETP_REGISTER_WRITE) ||
  126. psmouse_sliced_command(psmouse, reg) ||
  127. psmouse_sliced_command(psmouse, val) ||
  128. ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11)) {
  129. rc = -1;
  130. }
  131. break;
  132. case 2:
  133. if (elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
  134. elantech_ps2_command(psmouse, NULL, ETP_REGISTER_WRITE) ||
  135. elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
  136. elantech_ps2_command(psmouse, NULL, reg) ||
  137. elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
  138. elantech_ps2_command(psmouse, NULL, val) ||
  139. elantech_ps2_command(psmouse, NULL, PSMOUSE_CMD_SETSCALE11)) {
  140. rc = -1;
  141. }
  142. break;
  143. case 3:
  144. if (elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
  145. elantech_ps2_command(psmouse, NULL, ETP_REGISTER_READWRITE) ||
  146. elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
  147. elantech_ps2_command(psmouse, NULL, reg) ||
  148. elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
  149. elantech_ps2_command(psmouse, NULL, val) ||
  150. elantech_ps2_command(psmouse, NULL, PSMOUSE_CMD_SETSCALE11)) {
  151. rc = -1;
  152. }
  153. break;
  154. case 4:
  155. if (elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
  156. elantech_ps2_command(psmouse, NULL, ETP_REGISTER_READWRITE) ||
  157. elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
  158. elantech_ps2_command(psmouse, NULL, reg) ||
  159. elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
  160. elantech_ps2_command(psmouse, NULL, ETP_REGISTER_READWRITE) ||
  161. elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
  162. elantech_ps2_command(psmouse, NULL, val) ||
  163. elantech_ps2_command(psmouse, NULL, PSMOUSE_CMD_SETSCALE11)) {
  164. rc = -1;
  165. }
  166. break;
  167. }
  168. if (rc)
  169. psmouse_err(psmouse,
  170. "failed to write register 0x%02x with value 0x%02x.\n",
  171. reg, val);
  172. return rc;
  173. }
  174. /*
  175. * Dump a complete mouse movement packet to the syslog
  176. */
  177. static void elantech_packet_dump(struct psmouse *psmouse)
  178. {
  179. int i;
  180. psmouse_printk(KERN_DEBUG, psmouse, "PS/2 packet [");
  181. for (i = 0; i < psmouse->pktsize; i++)
  182. printk("%s0x%02x ", i ? ", " : " ", psmouse->packet[i]);
  183. printk("]\n");
  184. }
  185. /*
  186. * Interpret complete data packets and report absolute mode input events for
  187. * hardware version 1. (4 byte packets)
  188. */
  189. static void elantech_report_absolute_v1(struct psmouse *psmouse)
  190. {
  191. struct input_dev *dev = psmouse->dev;
  192. struct elantech_data *etd = psmouse->private;
  193. unsigned char *packet = psmouse->packet;
  194. int fingers;
  195. if (etd->fw_version < 0x020000) {
  196. /*
  197. * byte 0: D U p1 p2 1 p3 R L
  198. * byte 1: f 0 th tw x9 x8 y9 y8
  199. */
  200. fingers = ((packet[1] & 0x80) >> 7) +
  201. ((packet[1] & 0x30) >> 4);
  202. } else {
  203. /*
  204. * byte 0: n1 n0 p2 p1 1 p3 R L
  205. * byte 1: 0 0 0 0 x9 x8 y9 y8
  206. */
  207. fingers = (packet[0] & 0xc0) >> 6;
  208. }
  209. if (etd->jumpy_cursor) {
  210. if (fingers != 1) {
  211. etd->single_finger_reports = 0;
  212. } else if (etd->single_finger_reports < 2) {
  213. /* Discard first 2 reports of one finger, bogus */
  214. etd->single_finger_reports++;
  215. elantech_debug("discarding packet\n");
  216. return;
  217. }
  218. }
  219. input_report_key(dev, BTN_TOUCH, fingers != 0);
  220. /*
  221. * byte 2: x7 x6 x5 x4 x3 x2 x1 x0
  222. * byte 3: y7 y6 y5 y4 y3 y2 y1 y0
  223. */
  224. if (fingers) {
  225. input_report_abs(dev, ABS_X,
  226. ((packet[1] & 0x0c) << 6) | packet[2]);
  227. input_report_abs(dev, ABS_Y,
  228. etd->y_max - (((packet[1] & 0x03) << 8) | packet[3]));
  229. }
  230. input_report_key(dev, BTN_TOOL_FINGER, fingers == 1);
  231. input_report_key(dev, BTN_TOOL_DOUBLETAP, fingers == 2);
  232. input_report_key(dev, BTN_TOOL_TRIPLETAP, fingers == 3);
  233. input_report_key(dev, BTN_LEFT, packet[0] & 0x01);
  234. input_report_key(dev, BTN_RIGHT, packet[0] & 0x02);
  235. if (etd->fw_version < 0x020000 &&
  236. (etd->capabilities[0] & ETP_CAP_HAS_ROCKER)) {
  237. /* rocker up */
  238. input_report_key(dev, BTN_FORWARD, packet[0] & 0x40);
  239. /* rocker down */
  240. input_report_key(dev, BTN_BACK, packet[0] & 0x80);
  241. }
  242. input_sync(dev);
  243. }
  244. static void elantech_set_slot(struct input_dev *dev, int slot, bool active,
  245. unsigned int x, unsigned int y)
  246. {
  247. input_mt_slot(dev, slot);
  248. input_mt_report_slot_state(dev, MT_TOOL_FINGER, active);
  249. if (active) {
  250. input_report_abs(dev, ABS_MT_POSITION_X, x);
  251. input_report_abs(dev, ABS_MT_POSITION_Y, y);
  252. }
  253. }
  254. /* x1 < x2 and y1 < y2 when two fingers, x = y = 0 when not pressed */
  255. static void elantech_report_semi_mt_data(struct input_dev *dev,
  256. unsigned int num_fingers,
  257. unsigned int x1, unsigned int y1,
  258. unsigned int x2, unsigned int y2)
  259. {
  260. elantech_set_slot(dev, 0, num_fingers != 0, x1, y1);
  261. elantech_set_slot(dev, 1, num_fingers == 2, x2, y2);
  262. }
  263. /*
  264. * Interpret complete data packets and report absolute mode input events for
  265. * hardware version 2. (6 byte packets)
  266. */
  267. static void elantech_report_absolute_v2(struct psmouse *psmouse)
  268. {
  269. struct elantech_data *etd = psmouse->private;
  270. struct input_dev *dev = psmouse->dev;
  271. unsigned char *packet = psmouse->packet;
  272. unsigned int fingers, x1 = 0, y1 = 0, x2 = 0, y2 = 0;
  273. unsigned int width = 0, pres = 0;
  274. /* byte 0: n1 n0 . . . . R L */
  275. fingers = (packet[0] & 0xc0) >> 6;
  276. switch (fingers) {
  277. case 3:
  278. /*
  279. * Same as one finger, except report of more than 3 fingers:
  280. * byte 3: n4 . w1 w0 . . . .
  281. */
  282. if (packet[3] & 0x80)
  283. fingers = 4;
  284. /* pass through... */
  285. case 1:
  286. /*
  287. * byte 1: . . . . x11 x10 x9 x8
  288. * byte 2: x7 x6 x5 x4 x4 x2 x1 x0
  289. */
  290. x1 = ((packet[1] & 0x0f) << 8) | packet[2];
  291. /*
  292. * byte 4: . . . . y11 y10 y9 y8
  293. * byte 5: y7 y6 y5 y4 y3 y2 y1 y0
  294. */
  295. y1 = etd->y_max - (((packet[4] & 0x0f) << 8) | packet[5]);
  296. pres = (packet[1] & 0xf0) | ((packet[4] & 0xf0) >> 4);
  297. width = ((packet[0] & 0x30) >> 2) | ((packet[3] & 0x30) >> 4);
  298. break;
  299. case 2:
  300. /*
  301. * The coordinate of each finger is reported separately
  302. * with a lower resolution for two finger touches:
  303. * byte 0: . . ay8 ax8 . . . .
  304. * byte 1: ax7 ax6 ax5 ax4 ax3 ax2 ax1 ax0
  305. */
  306. x1 = (((packet[0] & 0x10) << 4) | packet[1]) << 2;
  307. /* byte 2: ay7 ay6 ay5 ay4 ay3 ay2 ay1 ay0 */
  308. y1 = etd->y_max -
  309. ((((packet[0] & 0x20) << 3) | packet[2]) << 2);
  310. /*
  311. * byte 3: . . by8 bx8 . . . .
  312. * byte 4: bx7 bx6 bx5 bx4 bx3 bx2 bx1 bx0
  313. */
  314. x2 = (((packet[3] & 0x10) << 4) | packet[4]) << 2;
  315. /* byte 5: by7 by8 by5 by4 by3 by2 by1 by0 */
  316. y2 = etd->y_max -
  317. ((((packet[3] & 0x20) << 3) | packet[5]) << 2);
  318. /* Unknown so just report sensible values */
  319. pres = 127;
  320. width = 7;
  321. break;
  322. }
  323. input_report_key(dev, BTN_TOUCH, fingers != 0);
  324. if (fingers != 0) {
  325. input_report_abs(dev, ABS_X, x1);
  326. input_report_abs(dev, ABS_Y, y1);
  327. }
  328. elantech_report_semi_mt_data(dev, fingers, x1, y1, x2, y2);
  329. input_report_key(dev, BTN_TOOL_FINGER, fingers == 1);
  330. input_report_key(dev, BTN_TOOL_DOUBLETAP, fingers == 2);
  331. input_report_key(dev, BTN_TOOL_TRIPLETAP, fingers == 3);
  332. input_report_key(dev, BTN_TOOL_QUADTAP, fingers == 4);
  333. input_report_key(dev, BTN_LEFT, packet[0] & 0x01);
  334. input_report_key(dev, BTN_RIGHT, packet[0] & 0x02);
  335. if (etd->reports_pressure) {
  336. input_report_abs(dev, ABS_PRESSURE, pres);
  337. input_report_abs(dev, ABS_TOOL_WIDTH, width);
  338. }
  339. input_sync(dev);
  340. }
  341. /*
  342. * Interpret complete data packets and report absolute mode input events for
  343. * hardware version 3. (12 byte packets for two fingers)
  344. */
  345. static void elantech_report_absolute_v3(struct psmouse *psmouse,
  346. int packet_type)
  347. {
  348. struct input_dev *dev = psmouse->dev;
  349. struct elantech_data *etd = psmouse->private;
  350. unsigned char *packet = psmouse->packet;
  351. unsigned int fingers = 0, x1 = 0, y1 = 0, x2 = 0, y2 = 0;
  352. unsigned int width = 0, pres = 0;
  353. /* byte 0: n1 n0 . . . . R L */
  354. fingers = (packet[0] & 0xc0) >> 6;
  355. switch (fingers) {
  356. case 3:
  357. case 1:
  358. /*
  359. * byte 1: . . . . x11 x10 x9 x8
  360. * byte 2: x7 x6 x5 x4 x4 x2 x1 x0
  361. */
  362. x1 = ((packet[1] & 0x0f) << 8) | packet[2];
  363. /*
  364. * byte 4: . . . . y11 y10 y9 y8
  365. * byte 5: y7 y6 y5 y4 y3 y2 y1 y0
  366. */
  367. y1 = etd->y_max - (((packet[4] & 0x0f) << 8) | packet[5]);
  368. break;
  369. case 2:
  370. if (packet_type == PACKET_V3_HEAD) {
  371. /*
  372. * byte 1: . . . . ax11 ax10 ax9 ax8
  373. * byte 2: ax7 ax6 ax5 ax4 ax3 ax2 ax1 ax0
  374. */
  375. etd->mt[0].x = ((packet[1] & 0x0f) << 8) | packet[2];
  376. /*
  377. * byte 4: . . . . ay11 ay10 ay9 ay8
  378. * byte 5: ay7 ay6 ay5 ay4 ay3 ay2 ay1 ay0
  379. */
  380. etd->mt[0].y = etd->y_max -
  381. (((packet[4] & 0x0f) << 8) | packet[5]);
  382. /*
  383. * wait for next packet
  384. */
  385. return;
  386. }
  387. /* packet_type == PACKET_V3_TAIL */
  388. x1 = etd->mt[0].x;
  389. y1 = etd->mt[0].y;
  390. x2 = ((packet[1] & 0x0f) << 8) | packet[2];
  391. y2 = etd->y_max - (((packet[4] & 0x0f) << 8) | packet[5]);
  392. break;
  393. }
  394. pres = (packet[1] & 0xf0) | ((packet[4] & 0xf0) >> 4);
  395. width = ((packet[0] & 0x30) >> 2) | ((packet[3] & 0x30) >> 4);
  396. input_report_key(dev, BTN_TOUCH, fingers != 0);
  397. if (fingers != 0) {
  398. input_report_abs(dev, ABS_X, x1);
  399. input_report_abs(dev, ABS_Y, y1);
  400. }
  401. elantech_report_semi_mt_data(dev, fingers, x1, y1, x2, y2);
  402. input_report_key(dev, BTN_TOOL_FINGER, fingers == 1);
  403. input_report_key(dev, BTN_TOOL_DOUBLETAP, fingers == 2);
  404. input_report_key(dev, BTN_TOOL_TRIPLETAP, fingers == 3);
  405. input_report_key(dev, BTN_LEFT, packet[0] & 0x01);
  406. input_report_key(dev, BTN_RIGHT, packet[0] & 0x02);
  407. input_report_abs(dev, ABS_PRESSURE, pres);
  408. input_report_abs(dev, ABS_TOOL_WIDTH, width);
  409. input_sync(dev);
  410. }
  411. static void elantech_input_sync_v4(struct psmouse *psmouse)
  412. {
  413. struct input_dev *dev = psmouse->dev;
  414. unsigned char *packet = psmouse->packet;
  415. input_report_key(dev, BTN_LEFT, packet[0] & 0x01);
  416. input_report_key(dev, BTN_RIGHT, packet[0] & 0x02);
  417. input_mt_report_pointer_emulation(dev, true);
  418. input_sync(dev);
  419. }
  420. static void process_packet_status_v4(struct psmouse *psmouse)
  421. {
  422. struct input_dev *dev = psmouse->dev;
  423. unsigned char *packet = psmouse->packet;
  424. unsigned fingers;
  425. int i;
  426. /* notify finger state change */
  427. fingers = packet[1] & 0x1f;
  428. for (i = 0; i < ETP_MAX_FINGERS; i++) {
  429. if ((fingers & (1 << i)) == 0) {
  430. input_mt_slot(dev, i);
  431. input_mt_report_slot_state(dev, MT_TOOL_FINGER, false);
  432. }
  433. }
  434. elantech_input_sync_v4(psmouse);
  435. }
  436. static void process_packet_head_v4(struct psmouse *psmouse)
  437. {
  438. struct input_dev *dev = psmouse->dev;
  439. struct elantech_data *etd = psmouse->private;
  440. unsigned char *packet = psmouse->packet;
  441. int id = ((packet[3] & 0xe0) >> 5) - 1;
  442. int pres, traces;
  443. if (id < 0)
  444. return;
  445. etd->mt[id].x = ((packet[1] & 0x0f) << 8) | packet[2];
  446. etd->mt[id].y = etd->y_max - (((packet[4] & 0x0f) << 8) | packet[5]);
  447. pres = (packet[1] & 0xf0) | ((packet[4] & 0xf0) >> 4);
  448. traces = (packet[0] & 0xf0) >> 4;
  449. input_mt_slot(dev, id);
  450. input_mt_report_slot_state(dev, MT_TOOL_FINGER, true);
  451. input_report_abs(dev, ABS_MT_POSITION_X, etd->mt[id].x);
  452. input_report_abs(dev, ABS_MT_POSITION_Y, etd->mt[id].y);
  453. input_report_abs(dev, ABS_MT_PRESSURE, pres);
  454. input_report_abs(dev, ABS_MT_TOUCH_MAJOR, traces * etd->width);
  455. /* report this for backwards compatibility */
  456. input_report_abs(dev, ABS_TOOL_WIDTH, traces);
  457. elantech_input_sync_v4(psmouse);
  458. }
  459. static void process_packet_motion_v4(struct psmouse *psmouse)
  460. {
  461. struct input_dev *dev = psmouse->dev;
  462. struct elantech_data *etd = psmouse->private;
  463. unsigned char *packet = psmouse->packet;
  464. int weight, delta_x1 = 0, delta_y1 = 0, delta_x2 = 0, delta_y2 = 0;
  465. int id, sid;
  466. id = ((packet[0] & 0xe0) >> 5) - 1;
  467. if (id < 0)
  468. return;
  469. sid = ((packet[3] & 0xe0) >> 5) - 1;
  470. weight = (packet[0] & 0x10) ? ETP_WEIGHT_VALUE : 1;
  471. /*
  472. * Motion packets give us the delta of x, y values of specific fingers,
  473. * but in two's complement. Let the compiler do the conversion for us.
  474. * Also _enlarge_ the numbers to int, in case of overflow.
  475. */
  476. delta_x1 = (signed char)packet[1];
  477. delta_y1 = (signed char)packet[2];
  478. delta_x2 = (signed char)packet[4];
  479. delta_y2 = (signed char)packet[5];
  480. etd->mt[id].x += delta_x1 * weight;
  481. etd->mt[id].y -= delta_y1 * weight;
  482. input_mt_slot(dev, id);
  483. input_report_abs(dev, ABS_MT_POSITION_X, etd->mt[id].x);
  484. input_report_abs(dev, ABS_MT_POSITION_Y, etd->mt[id].y);
  485. if (sid >= 0) {
  486. etd->mt[sid].x += delta_x2 * weight;
  487. etd->mt[sid].y -= delta_y2 * weight;
  488. input_mt_slot(dev, sid);
  489. input_report_abs(dev, ABS_MT_POSITION_X, etd->mt[sid].x);
  490. input_report_abs(dev, ABS_MT_POSITION_Y, etd->mt[sid].y);
  491. }
  492. elantech_input_sync_v4(psmouse);
  493. }
  494. static void elantech_report_absolute_v4(struct psmouse *psmouse,
  495. int packet_type)
  496. {
  497. switch (packet_type) {
  498. case PACKET_V4_STATUS:
  499. process_packet_status_v4(psmouse);
  500. break;
  501. case PACKET_V4_HEAD:
  502. process_packet_head_v4(psmouse);
  503. break;
  504. case PACKET_V4_MOTION:
  505. process_packet_motion_v4(psmouse);
  506. break;
  507. case PACKET_UNKNOWN:
  508. default:
  509. /* impossible to get here */
  510. break;
  511. }
  512. }
  513. static int elantech_packet_check_v1(struct psmouse *psmouse)
  514. {
  515. struct elantech_data *etd = psmouse->private;
  516. unsigned char *packet = psmouse->packet;
  517. unsigned char p1, p2, p3;
  518. /* Parity bits are placed differently */
  519. if (etd->fw_version < 0x020000) {
  520. /* byte 0: D U p1 p2 1 p3 R L */
  521. p1 = (packet[0] & 0x20) >> 5;
  522. p2 = (packet[0] & 0x10) >> 4;
  523. } else {
  524. /* byte 0: n1 n0 p2 p1 1 p3 R L */
  525. p1 = (packet[0] & 0x10) >> 4;
  526. p2 = (packet[0] & 0x20) >> 5;
  527. }
  528. p3 = (packet[0] & 0x04) >> 2;
  529. return etd->parity[packet[1]] == p1 &&
  530. etd->parity[packet[2]] == p2 &&
  531. etd->parity[packet[3]] == p3;
  532. }
  533. static int elantech_debounce_check_v2(struct psmouse *psmouse)
  534. {
  535. /*
  536. * When we encounter packet that matches this exactly, it means the
  537. * hardware is in debounce status. Just ignore the whole packet.
  538. */
  539. const u8 debounce_packet[] = { 0x84, 0xff, 0xff, 0x02, 0xff, 0xff };
  540. unsigned char *packet = psmouse->packet;
  541. return !memcmp(packet, debounce_packet, sizeof(debounce_packet));
  542. }
  543. static int elantech_packet_check_v2(struct psmouse *psmouse)
  544. {
  545. struct elantech_data *etd = psmouse->private;
  546. unsigned char *packet = psmouse->packet;
  547. /*
  548. * V2 hardware has two flavors. Older ones that do not report pressure,
  549. * and newer ones that reports pressure and width. With newer ones, all
  550. * packets (1, 2, 3 finger touch) have the same constant bits. With
  551. * older ones, 1/3 finger touch packets and 2 finger touch packets
  552. * have different constant bits.
  553. * With all three cases, if the constant bits are not exactly what I
  554. * expected, I consider them invalid.
  555. */
  556. if (etd->reports_pressure)
  557. return (packet[0] & 0x0c) == 0x04 &&
  558. (packet[3] & 0x0f) == 0x02;
  559. if ((packet[0] & 0xc0) == 0x80)
  560. return (packet[0] & 0x0c) == 0x0c &&
  561. (packet[3] & 0x0e) == 0x08;
  562. return (packet[0] & 0x3c) == 0x3c &&
  563. (packet[1] & 0xf0) == 0x00 &&
  564. (packet[3] & 0x3e) == 0x38 &&
  565. (packet[4] & 0xf0) == 0x00;
  566. }
  567. /*
  568. * We check the constant bits to determine what packet type we get,
  569. * so packet checking is mandatory for v3 and later hardware.
  570. */
  571. static int elantech_packet_check_v3(struct psmouse *psmouse)
  572. {
  573. const u8 debounce_packet[] = { 0xc4, 0xff, 0xff, 0x02, 0xff, 0xff };
  574. unsigned char *packet = psmouse->packet;
  575. /*
  576. * check debounce first, it has the same signature in byte 0
  577. * and byte 3 as PACKET_V3_HEAD.
  578. */
  579. if (!memcmp(packet, debounce_packet, sizeof(debounce_packet)))
  580. return PACKET_DEBOUNCE;
  581. if ((packet[0] & 0x0c) == 0x04 && (packet[3] & 0xcf) == 0x02)
  582. return PACKET_V3_HEAD;
  583. if ((packet[0] & 0x0c) == 0x0c && (packet[3] & 0xce) == 0x0c)
  584. return PACKET_V3_TAIL;
  585. return PACKET_UNKNOWN;
  586. }
  587. static int elantech_packet_check_v4(struct psmouse *psmouse)
  588. {
  589. unsigned char *packet = psmouse->packet;
  590. if ((packet[0] & 0x0c) == 0x04 &&
  591. (packet[3] & 0x1f) == 0x11)
  592. return PACKET_V4_HEAD;
  593. if ((packet[0] & 0x0c) == 0x04 &&
  594. (packet[3] & 0x1f) == 0x12)
  595. return PACKET_V4_MOTION;
  596. if ((packet[0] & 0x0c) == 0x04 &&
  597. (packet[3] & 0x1f) == 0x10)
  598. return PACKET_V4_STATUS;
  599. return PACKET_UNKNOWN;
  600. }
  601. /*
  602. * Process byte stream from mouse and handle complete packets
  603. */
  604. static psmouse_ret_t elantech_process_byte(struct psmouse *psmouse)
  605. {
  606. struct elantech_data *etd = psmouse->private;
  607. int packet_type;
  608. if (psmouse->pktcnt < psmouse->pktsize)
  609. return PSMOUSE_GOOD_DATA;
  610. if (etd->debug > 1)
  611. elantech_packet_dump(psmouse);
  612. switch (etd->hw_version) {
  613. case 1:
  614. if (etd->paritycheck && !elantech_packet_check_v1(psmouse))
  615. return PSMOUSE_BAD_DATA;
  616. elantech_report_absolute_v1(psmouse);
  617. break;
  618. case 2:
  619. /* ignore debounce */
  620. if (elantech_debounce_check_v2(psmouse))
  621. return PSMOUSE_FULL_PACKET;
  622. if (etd->paritycheck && !elantech_packet_check_v2(psmouse))
  623. return PSMOUSE_BAD_DATA;
  624. elantech_report_absolute_v2(psmouse);
  625. break;
  626. case 3:
  627. packet_type = elantech_packet_check_v3(psmouse);
  628. /* ignore debounce */
  629. if (packet_type == PACKET_DEBOUNCE)
  630. return PSMOUSE_FULL_PACKET;
  631. if (packet_type == PACKET_UNKNOWN)
  632. return PSMOUSE_BAD_DATA;
  633. elantech_report_absolute_v3(psmouse, packet_type);
  634. break;
  635. case 4:
  636. packet_type = elantech_packet_check_v4(psmouse);
  637. if (packet_type == PACKET_UNKNOWN)
  638. return PSMOUSE_BAD_DATA;
  639. elantech_report_absolute_v4(psmouse, packet_type);
  640. break;
  641. }
  642. return PSMOUSE_FULL_PACKET;
  643. }
  644. /*
  645. * Put the touchpad into absolute mode
  646. */
  647. static int elantech_set_absolute_mode(struct psmouse *psmouse)
  648. {
  649. struct elantech_data *etd = psmouse->private;
  650. unsigned char val;
  651. int tries = ETP_READ_BACK_TRIES;
  652. int rc = 0;
  653. switch (etd->hw_version) {
  654. case 1:
  655. etd->reg_10 = 0x16;
  656. etd->reg_11 = 0x8f;
  657. if (elantech_write_reg(psmouse, 0x10, etd->reg_10) ||
  658. elantech_write_reg(psmouse, 0x11, etd->reg_11)) {
  659. rc = -1;
  660. }
  661. break;
  662. case 2:
  663. /* Windows driver values */
  664. etd->reg_10 = 0x54;
  665. etd->reg_11 = 0x88; /* 0x8a */
  666. etd->reg_21 = 0x60; /* 0x00 */
  667. if (elantech_write_reg(psmouse, 0x10, etd->reg_10) ||
  668. elantech_write_reg(psmouse, 0x11, etd->reg_11) ||
  669. elantech_write_reg(psmouse, 0x21, etd->reg_21)) {
  670. rc = -1;
  671. }
  672. break;
  673. case 3:
  674. etd->reg_10 = 0x0b;
  675. if (elantech_write_reg(psmouse, 0x10, etd->reg_10))
  676. rc = -1;
  677. break;
  678. case 4:
  679. etd->reg_07 = 0x01;
  680. if (elantech_write_reg(psmouse, 0x07, etd->reg_07))
  681. rc = -1;
  682. goto skip_readback_reg_10; /* v4 has no reg 0x10 to read */
  683. }
  684. if (rc == 0) {
  685. /*
  686. * Read back reg 0x10. For hardware version 1 we must make
  687. * sure the absolute mode bit is set. For hardware version 2
  688. * the touchpad is probably initializing and not ready until
  689. * we read back the value we just wrote.
  690. */
  691. do {
  692. rc = elantech_read_reg(psmouse, 0x10, &val);
  693. if (rc == 0)
  694. break;
  695. tries--;
  696. elantech_debug("retrying read (%d).\n", tries);
  697. msleep(ETP_READ_BACK_DELAY);
  698. } while (tries > 0);
  699. if (rc) {
  700. psmouse_err(psmouse,
  701. "failed to read back register 0x10.\n");
  702. } else if (etd->hw_version == 1 &&
  703. !(val & ETP_R10_ABSOLUTE_MODE)) {
  704. psmouse_err(psmouse,
  705. "touchpad refuses to switch to absolute mode.\n");
  706. rc = -1;
  707. }
  708. }
  709. skip_readback_reg_10:
  710. if (rc)
  711. psmouse_err(psmouse, "failed to initialise registers.\n");
  712. return rc;
  713. }
  714. static int elantech_set_range(struct psmouse *psmouse,
  715. unsigned int *x_min, unsigned int *y_min,
  716. unsigned int *x_max, unsigned int *y_max,
  717. unsigned int *width)
  718. {
  719. struct elantech_data *etd = psmouse->private;
  720. unsigned char param[3];
  721. unsigned char traces;
  722. switch (etd->hw_version) {
  723. case 1:
  724. *x_min = ETP_XMIN_V1;
  725. *y_min = ETP_YMIN_V1;
  726. *x_max = ETP_XMAX_V1;
  727. *y_max = ETP_YMAX_V1;
  728. break;
  729. case 2:
  730. if (etd->fw_version == 0x020800 ||
  731. etd->fw_version == 0x020b00 ||
  732. etd->fw_version == 0x020030) {
  733. *x_min = ETP_XMIN_V2;
  734. *y_min = ETP_YMIN_V2;
  735. *x_max = ETP_XMAX_V2;
  736. *y_max = ETP_YMAX_V2;
  737. } else {
  738. int i;
  739. int fixed_dpi;
  740. i = (etd->fw_version > 0x020800 &&
  741. etd->fw_version < 0x020900) ? 1 : 2;
  742. if (synaptics_send_cmd(psmouse, ETP_FW_ID_QUERY, param))
  743. return -1;
  744. fixed_dpi = param[1] & 0x10;
  745. if (((etd->fw_version >> 16) == 0x14) && fixed_dpi) {
  746. if (synaptics_send_cmd(psmouse, ETP_SAMPLE_QUERY, param))
  747. return -1;
  748. *x_max = (etd->capabilities[1] - i) * param[1] / 2;
  749. *y_max = (etd->capabilities[2] - i) * param[2] / 2;
  750. } else if (etd->fw_version == 0x040216) {
  751. *x_max = 819;
  752. *y_max = 405;
  753. } else if (etd->fw_version == 0x040219 || etd->fw_version == 0x040215) {
  754. *x_max = 900;
  755. *y_max = 500;
  756. } else {
  757. *x_max = (etd->capabilities[1] - i) * 64;
  758. *y_max = (etd->capabilities[2] - i) * 64;
  759. }
  760. }
  761. break;
  762. case 3:
  763. if (synaptics_send_cmd(psmouse, ETP_FW_ID_QUERY, param))
  764. return -1;
  765. *x_max = (0x0f & param[0]) << 8 | param[1];
  766. *y_max = (0xf0 & param[0]) << 4 | param[2];
  767. break;
  768. case 4:
  769. if (synaptics_send_cmd(psmouse, ETP_FW_ID_QUERY, param))
  770. return -1;
  771. *x_max = (0x0f & param[0]) << 8 | param[1];
  772. *y_max = (0xf0 & param[0]) << 4 | param[2];
  773. traces = etd->capabilities[1];
  774. if ((traces < 2) || (traces > *x_max))
  775. return -1;
  776. *width = *x_max / (traces - 1);
  777. break;
  778. }
  779. return 0;
  780. }
  781. /*
  782. * Set the appropriate event bits for the input subsystem
  783. */
  784. static int elantech_set_input_params(struct psmouse *psmouse)
  785. {
  786. struct input_dev *dev = psmouse->dev;
  787. struct elantech_data *etd = psmouse->private;
  788. unsigned int x_min = 0, y_min = 0, x_max = 0, y_max = 0, width = 0;
  789. if (elantech_set_range(psmouse, &x_min, &y_min, &x_max, &y_max, &width))
  790. return -1;
  791. __set_bit(EV_KEY, dev->evbit);
  792. __set_bit(EV_ABS, dev->evbit);
  793. __clear_bit(EV_REL, dev->evbit);
  794. __set_bit(BTN_LEFT, dev->keybit);
  795. __set_bit(BTN_RIGHT, dev->keybit);
  796. __set_bit(BTN_TOUCH, dev->keybit);
  797. __set_bit(BTN_TOOL_FINGER, dev->keybit);
  798. __set_bit(BTN_TOOL_DOUBLETAP, dev->keybit);
  799. __set_bit(BTN_TOOL_TRIPLETAP, dev->keybit);
  800. switch (etd->hw_version) {
  801. case 1:
  802. /* Rocker button */
  803. if (etd->fw_version < 0x020000 &&
  804. (etd->capabilities[0] & ETP_CAP_HAS_ROCKER)) {
  805. __set_bit(BTN_FORWARD, dev->keybit);
  806. __set_bit(BTN_BACK, dev->keybit);
  807. }
  808. input_set_abs_params(dev, ABS_X, x_min, x_max, 0, 0);
  809. input_set_abs_params(dev, ABS_Y, y_min, y_max, 0, 0);
  810. break;
  811. case 2:
  812. __set_bit(BTN_TOOL_QUADTAP, dev->keybit);
  813. __set_bit(INPUT_PROP_SEMI_MT, dev->propbit);
  814. /* fall through */
  815. case 3:
  816. input_set_abs_params(dev, ABS_X, x_min, x_max, 0, 0);
  817. input_set_abs_params(dev, ABS_Y, y_min, y_max, 0, 0);
  818. if (etd->reports_pressure) {
  819. input_set_abs_params(dev, ABS_PRESSURE, ETP_PMIN_V2,
  820. ETP_PMAX_V2, 0, 0);
  821. input_set_abs_params(dev, ABS_TOOL_WIDTH, ETP_WMIN_V2,
  822. ETP_WMAX_V2, 0, 0);
  823. }
  824. input_mt_init_slots(dev, 2);
  825. input_set_abs_params(dev, ABS_MT_POSITION_X, x_min, x_max, 0, 0);
  826. input_set_abs_params(dev, ABS_MT_POSITION_Y, y_min, y_max, 0, 0);
  827. break;
  828. case 4:
  829. __set_bit(BTN_TOOL_QUADTAP, dev->keybit);
  830. /* For X to recognize me as touchpad. */
  831. input_set_abs_params(dev, ABS_X, x_min, x_max, 0, 0);
  832. input_set_abs_params(dev, ABS_Y, y_min, y_max, 0, 0);
  833. /*
  834. * range of pressure and width is the same as v2,
  835. * report ABS_PRESSURE, ABS_TOOL_WIDTH for compatibility.
  836. */
  837. input_set_abs_params(dev, ABS_PRESSURE, ETP_PMIN_V2,
  838. ETP_PMAX_V2, 0, 0);
  839. input_set_abs_params(dev, ABS_TOOL_WIDTH, ETP_WMIN_V2,
  840. ETP_WMAX_V2, 0, 0);
  841. /* Multitouch capable pad, up to 5 fingers. */
  842. input_mt_init_slots(dev, ETP_MAX_FINGERS);
  843. input_set_abs_params(dev, ABS_MT_POSITION_X, x_min, x_max, 0, 0);
  844. input_set_abs_params(dev, ABS_MT_POSITION_Y, y_min, y_max, 0, 0);
  845. input_set_abs_params(dev, ABS_MT_PRESSURE, ETP_PMIN_V2,
  846. ETP_PMAX_V2, 0, 0);
  847. /*
  848. * The firmware reports how many trace lines the finger spans,
  849. * convert to surface unit as Protocol-B requires.
  850. */
  851. input_set_abs_params(dev, ABS_MT_TOUCH_MAJOR, 0,
  852. ETP_WMAX_V2 * width, 0, 0);
  853. break;
  854. }
  855. etd->y_max = y_max;
  856. etd->width = width;
  857. return 0;
  858. }
  859. struct elantech_attr_data {
  860. size_t field_offset;
  861. unsigned char reg;
  862. };
  863. /*
  864. * Display a register value by reading a sysfs entry
  865. */
  866. static ssize_t elantech_show_int_attr(struct psmouse *psmouse, void *data,
  867. char *buf)
  868. {
  869. struct elantech_data *etd = psmouse->private;
  870. struct elantech_attr_data *attr = data;
  871. unsigned char *reg = (unsigned char *) etd + attr->field_offset;
  872. int rc = 0;
  873. if (attr->reg)
  874. rc = elantech_read_reg(psmouse, attr->reg, reg);
  875. return sprintf(buf, "0x%02x\n", (attr->reg && rc) ? -1 : *reg);
  876. }
  877. /*
  878. * Write a register value by writing a sysfs entry
  879. */
  880. static ssize_t elantech_set_int_attr(struct psmouse *psmouse,
  881. void *data, const char *buf, size_t count)
  882. {
  883. struct elantech_data *etd = psmouse->private;
  884. struct elantech_attr_data *attr = data;
  885. unsigned char *reg = (unsigned char *) etd + attr->field_offset;
  886. unsigned long value;
  887. int err;
  888. err = strict_strtoul(buf, 16, &value);
  889. if (err)
  890. return err;
  891. if (value > 0xff)
  892. return -EINVAL;
  893. /* Do we need to preserve some bits for version 2 hardware too? */
  894. if (etd->hw_version == 1) {
  895. if (attr->reg == 0x10)
  896. /* Force absolute mode always on */
  897. value |= ETP_R10_ABSOLUTE_MODE;
  898. else if (attr->reg == 0x11)
  899. /* Force 4 byte mode always on */
  900. value |= ETP_R11_4_BYTE_MODE;
  901. }
  902. if (!attr->reg || elantech_write_reg(psmouse, attr->reg, value) == 0)
  903. *reg = value;
  904. return count;
  905. }
  906. #define ELANTECH_INT_ATTR(_name, _register) \
  907. static struct elantech_attr_data elantech_attr_##_name = { \
  908. .field_offset = offsetof(struct elantech_data, _name), \
  909. .reg = _register, \
  910. }; \
  911. PSMOUSE_DEFINE_ATTR(_name, S_IWUSR | S_IRUGO, \
  912. &elantech_attr_##_name, \
  913. elantech_show_int_attr, \
  914. elantech_set_int_attr)
  915. ELANTECH_INT_ATTR(reg_07, 0x07);
  916. ELANTECH_INT_ATTR(reg_10, 0x10);
  917. ELANTECH_INT_ATTR(reg_11, 0x11);
  918. ELANTECH_INT_ATTR(reg_20, 0x20);
  919. ELANTECH_INT_ATTR(reg_21, 0x21);
  920. ELANTECH_INT_ATTR(reg_22, 0x22);
  921. ELANTECH_INT_ATTR(reg_23, 0x23);
  922. ELANTECH_INT_ATTR(reg_24, 0x24);
  923. ELANTECH_INT_ATTR(reg_25, 0x25);
  924. ELANTECH_INT_ATTR(reg_26, 0x26);
  925. ELANTECH_INT_ATTR(debug, 0);
  926. ELANTECH_INT_ATTR(paritycheck, 0);
  927. static struct attribute *elantech_attrs[] = {
  928. &psmouse_attr_reg_07.dattr.attr,
  929. &psmouse_attr_reg_10.dattr.attr,
  930. &psmouse_attr_reg_11.dattr.attr,
  931. &psmouse_attr_reg_20.dattr.attr,
  932. &psmouse_attr_reg_21.dattr.attr,
  933. &psmouse_attr_reg_22.dattr.attr,
  934. &psmouse_attr_reg_23.dattr.attr,
  935. &psmouse_attr_reg_24.dattr.attr,
  936. &psmouse_attr_reg_25.dattr.attr,
  937. &psmouse_attr_reg_26.dattr.attr,
  938. &psmouse_attr_debug.dattr.attr,
  939. &psmouse_attr_paritycheck.dattr.attr,
  940. NULL
  941. };
  942. static struct attribute_group elantech_attr_group = {
  943. .attrs = elantech_attrs,
  944. };
  945. static bool elantech_is_signature_valid(const unsigned char *param)
  946. {
  947. static const unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10 };
  948. int i;
  949. if (param[0] == 0)
  950. return false;
  951. if (param[1] == 0)
  952. return true;
  953. for (i = 0; i < ARRAY_SIZE(rates); i++)
  954. if (param[2] == rates[i])
  955. return false;
  956. return true;
  957. }
  958. /*
  959. * Use magic knock to detect Elantech touchpad
  960. */
  961. int elantech_detect(struct psmouse *psmouse, bool set_properties)
  962. {
  963. struct ps2dev *ps2dev = &psmouse->ps2dev;
  964. unsigned char param[3];
  965. ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
  966. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  967. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  968. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  969. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  970. ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) {
  971. psmouse_dbg(psmouse, "sending Elantech magic knock failed.\n");
  972. return -1;
  973. }
  974. /*
  975. * Report this in case there are Elantech models that use a different
  976. * set of magic numbers
  977. */
  978. if (param[0] != 0x3c || param[1] != 0x03 ||
  979. (param[2] != 0xc8 && param[2] != 0x00)) {
  980. psmouse_dbg(psmouse,
  981. "unexpected magic knock result 0x%02x, 0x%02x, 0x%02x.\n",
  982. param[0], param[1], param[2]);
  983. return -1;
  984. }
  985. /*
  986. * Query touchpad's firmware version and see if it reports known
  987. * value to avoid mis-detection. Logitech mice are known to respond
  988. * to Elantech magic knock and there might be more.
  989. */
  990. if (synaptics_send_cmd(psmouse, ETP_FW_VERSION_QUERY, param)) {
  991. psmouse_dbg(psmouse, "failed to query firmware version.\n");
  992. return -1;
  993. }
  994. psmouse_dbg(psmouse,
  995. "Elantech version query result 0x%02x, 0x%02x, 0x%02x.\n",
  996. param[0], param[1], param[2]);
  997. if (!elantech_is_signature_valid(param)) {
  998. psmouse_dbg(psmouse,
  999. "Probably not a real Elantech touchpad. Aborting.\n");
  1000. return -1;
  1001. }
  1002. if (set_properties) {
  1003. psmouse->vendor = "Elantech";
  1004. psmouse->name = "Touchpad";
  1005. }
  1006. return 0;
  1007. }
  1008. /*
  1009. * Clean up sysfs entries when disconnecting
  1010. */
  1011. static void elantech_disconnect(struct psmouse *psmouse)
  1012. {
  1013. sysfs_remove_group(&psmouse->ps2dev.serio->dev.kobj,
  1014. &elantech_attr_group);
  1015. kfree(psmouse->private);
  1016. psmouse->private = NULL;
  1017. }
  1018. /*
  1019. * Put the touchpad back into absolute mode when reconnecting
  1020. */
  1021. static int elantech_reconnect(struct psmouse *psmouse)
  1022. {
  1023. if (elantech_detect(psmouse, 0))
  1024. return -1;
  1025. if (elantech_set_absolute_mode(psmouse)) {
  1026. psmouse_err(psmouse,
  1027. "failed to put touchpad back into absolute mode.\n");
  1028. return -1;
  1029. }
  1030. return 0;
  1031. }
  1032. /*
  1033. * determine hardware version and set some properties according to it.
  1034. */
  1035. static int elantech_set_properties(struct elantech_data *etd)
  1036. {
  1037. int ver = (etd->fw_version & 0x0f0000) >> 16;
  1038. if (etd->fw_version < 0x020030 || etd->fw_version == 0x020600)
  1039. etd->hw_version = 1;
  1040. else if (etd->fw_version < 0x150600)
  1041. etd->hw_version = 2;
  1042. else if (ver == 5)
  1043. etd->hw_version = 3;
  1044. else if (ver == 6)
  1045. etd->hw_version = 4;
  1046. else
  1047. return -1;
  1048. /*
  1049. * Turn on packet checking by default.
  1050. */
  1051. etd->paritycheck = 1;
  1052. /*
  1053. * This firmware suffers from misreporting coordinates when
  1054. * a touch action starts causing the mouse cursor or scrolled page
  1055. * to jump. Enable a workaround.
  1056. */
  1057. etd->jumpy_cursor =
  1058. (etd->fw_version == 0x020022 || etd->fw_version == 0x020600);
  1059. if (etd->hw_version > 1) {
  1060. /* For now show extra debug information */
  1061. etd->debug = 1;
  1062. if (etd->fw_version >= 0x020800)
  1063. etd->reports_pressure = true;
  1064. }
  1065. return 0;
  1066. }
  1067. /*
  1068. * Initialize the touchpad and create sysfs entries
  1069. */
  1070. int elantech_init(struct psmouse *psmouse)
  1071. {
  1072. struct elantech_data *etd;
  1073. int i, error;
  1074. unsigned char param[3];
  1075. psmouse->private = etd = kzalloc(sizeof(struct elantech_data), GFP_KERNEL);
  1076. if (!etd)
  1077. return -ENOMEM;
  1078. etd->parity[0] = 1;
  1079. for (i = 1; i < 256; i++)
  1080. etd->parity[i] = etd->parity[i & (i - 1)] ^ 1;
  1081. /*
  1082. * Do the version query again so we can store the result
  1083. */
  1084. if (synaptics_send_cmd(psmouse, ETP_FW_VERSION_QUERY, param)) {
  1085. psmouse_err(psmouse, "failed to query firmware version.\n");
  1086. goto init_fail;
  1087. }
  1088. etd->fw_version = (param[0] << 16) | (param[1] << 8) | param[2];
  1089. if (elantech_set_properties(etd)) {
  1090. psmouse_err(psmouse, "unknown hardware version, aborting...\n");
  1091. goto init_fail;
  1092. }
  1093. psmouse_info(psmouse,
  1094. "assuming hardware version %d (with firmware version 0x%02x%02x%02x)\n",
  1095. etd->hw_version, param[0], param[1], param[2]);
  1096. if (synaptics_send_cmd(psmouse, ETP_CAPABILITIES_QUERY,
  1097. etd->capabilities)) {
  1098. psmouse_err(psmouse, "failed to query capabilities.\n");
  1099. goto init_fail;
  1100. }
  1101. psmouse_info(psmouse,
  1102. "Synaptics capabilities query result 0x%02x, 0x%02x, 0x%02x.\n",
  1103. etd->capabilities[0], etd->capabilities[1],
  1104. etd->capabilities[2]);
  1105. if (elantech_set_absolute_mode(psmouse)) {
  1106. psmouse_err(psmouse,
  1107. "failed to put touchpad into absolute mode.\n");
  1108. goto init_fail;
  1109. }
  1110. if (elantech_set_input_params(psmouse)) {
  1111. psmouse_err(psmouse, "failed to query touchpad range.\n");
  1112. goto init_fail;
  1113. }
  1114. error = sysfs_create_group(&psmouse->ps2dev.serio->dev.kobj,
  1115. &elantech_attr_group);
  1116. if (error) {
  1117. psmouse_err(psmouse,
  1118. "failed to create sysfs attributes, error: %d.\n",
  1119. error);
  1120. goto init_fail;
  1121. }
  1122. psmouse->protocol_handler = elantech_process_byte;
  1123. psmouse->disconnect = elantech_disconnect;
  1124. psmouse->reconnect = elantech_reconnect;
  1125. psmouse->pktsize = etd->hw_version > 1 ? 6 : 4;
  1126. return 0;
  1127. init_fail:
  1128. kfree(etd);
  1129. return -1;
  1130. }