elantech.c 49 KB

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