elantech.c 40 KB

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