elantech.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431
  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_mt_report_pointer_emulation(dev, true);
  432. input_sync(dev);
  433. }
  434. static void process_packet_status_v4(struct psmouse *psmouse)
  435. {
  436. struct input_dev *dev = psmouse->dev;
  437. unsigned char *packet = psmouse->packet;
  438. unsigned fingers;
  439. int i;
  440. /* notify finger state change */
  441. fingers = packet[1] & 0x1f;
  442. for (i = 0; i < ETP_MAX_FINGERS; i++) {
  443. if ((fingers & (1 << i)) == 0) {
  444. input_mt_slot(dev, i);
  445. input_mt_report_slot_state(dev, MT_TOOL_FINGER, false);
  446. }
  447. }
  448. elantech_input_sync_v4(psmouse);
  449. }
  450. static void process_packet_head_v4(struct psmouse *psmouse)
  451. {
  452. struct input_dev *dev = psmouse->dev;
  453. struct elantech_data *etd = psmouse->private;
  454. unsigned char *packet = psmouse->packet;
  455. int id = ((packet[3] & 0xe0) >> 5) - 1;
  456. int pres, traces;
  457. if (id < 0)
  458. return;
  459. etd->mt[id].x = ((packet[1] & 0x0f) << 8) | packet[2];
  460. etd->mt[id].y = etd->y_max - (((packet[4] & 0x0f) << 8) | packet[5]);
  461. pres = (packet[1] & 0xf0) | ((packet[4] & 0xf0) >> 4);
  462. traces = (packet[0] & 0xf0) >> 4;
  463. input_mt_slot(dev, id);
  464. input_mt_report_slot_state(dev, MT_TOOL_FINGER, true);
  465. input_report_abs(dev, ABS_MT_POSITION_X, etd->mt[id].x);
  466. input_report_abs(dev, ABS_MT_POSITION_Y, etd->mt[id].y);
  467. input_report_abs(dev, ABS_MT_PRESSURE, pres);
  468. input_report_abs(dev, ABS_MT_TOUCH_MAJOR, traces * etd->width);
  469. /* report this for backwards compatibility */
  470. input_report_abs(dev, ABS_TOOL_WIDTH, traces);
  471. elantech_input_sync_v4(psmouse);
  472. }
  473. static void process_packet_motion_v4(struct psmouse *psmouse)
  474. {
  475. struct input_dev *dev = psmouse->dev;
  476. struct elantech_data *etd = psmouse->private;
  477. unsigned char *packet = psmouse->packet;
  478. int weight, delta_x1 = 0, delta_y1 = 0, delta_x2 = 0, delta_y2 = 0;
  479. int id, sid;
  480. id = ((packet[0] & 0xe0) >> 5) - 1;
  481. if (id < 0)
  482. return;
  483. sid = ((packet[3] & 0xe0) >> 5) - 1;
  484. weight = (packet[0] & 0x10) ? ETP_WEIGHT_VALUE : 1;
  485. /*
  486. * Motion packets give us the delta of x, y values of specific fingers,
  487. * but in two's complement. Let the compiler do the conversion for us.
  488. * Also _enlarge_ the numbers to int, in case of overflow.
  489. */
  490. delta_x1 = (signed char)packet[1];
  491. delta_y1 = (signed char)packet[2];
  492. delta_x2 = (signed char)packet[4];
  493. delta_y2 = (signed char)packet[5];
  494. etd->mt[id].x += delta_x1 * weight;
  495. etd->mt[id].y -= delta_y1 * weight;
  496. input_mt_slot(dev, id);
  497. input_report_abs(dev, ABS_MT_POSITION_X, etd->mt[id].x);
  498. input_report_abs(dev, ABS_MT_POSITION_Y, etd->mt[id].y);
  499. if (sid >= 0) {
  500. etd->mt[sid].x += delta_x2 * weight;
  501. etd->mt[sid].y -= delta_y2 * weight;
  502. input_mt_slot(dev, sid);
  503. input_report_abs(dev, ABS_MT_POSITION_X, etd->mt[sid].x);
  504. input_report_abs(dev, ABS_MT_POSITION_Y, etd->mt[sid].y);
  505. }
  506. elantech_input_sync_v4(psmouse);
  507. }
  508. static void elantech_report_absolute_v4(struct psmouse *psmouse,
  509. int packet_type)
  510. {
  511. switch (packet_type) {
  512. case PACKET_V4_STATUS:
  513. process_packet_status_v4(psmouse);
  514. break;
  515. case PACKET_V4_HEAD:
  516. process_packet_head_v4(psmouse);
  517. break;
  518. case PACKET_V4_MOTION:
  519. process_packet_motion_v4(psmouse);
  520. break;
  521. case PACKET_UNKNOWN:
  522. default:
  523. /* impossible to get here */
  524. break;
  525. }
  526. }
  527. static int elantech_packet_check_v1(struct psmouse *psmouse)
  528. {
  529. struct elantech_data *etd = psmouse->private;
  530. unsigned char *packet = psmouse->packet;
  531. unsigned char p1, p2, p3;
  532. /* Parity bits are placed differently */
  533. if (etd->fw_version < 0x020000) {
  534. /* byte 0: D U p1 p2 1 p3 R L */
  535. p1 = (packet[0] & 0x20) >> 5;
  536. p2 = (packet[0] & 0x10) >> 4;
  537. } else {
  538. /* byte 0: n1 n0 p2 p1 1 p3 R L */
  539. p1 = (packet[0] & 0x10) >> 4;
  540. p2 = (packet[0] & 0x20) >> 5;
  541. }
  542. p3 = (packet[0] & 0x04) >> 2;
  543. return etd->parity[packet[1]] == p1 &&
  544. etd->parity[packet[2]] == p2 &&
  545. etd->parity[packet[3]] == p3;
  546. }
  547. static int elantech_debounce_check_v2(struct psmouse *psmouse)
  548. {
  549. /*
  550. * When we encounter packet that matches this exactly, it means the
  551. * hardware is in debounce status. Just ignore the whole packet.
  552. */
  553. const u8 debounce_packet[] = { 0x84, 0xff, 0xff, 0x02, 0xff, 0xff };
  554. unsigned char *packet = psmouse->packet;
  555. return !memcmp(packet, debounce_packet, sizeof(debounce_packet));
  556. }
  557. static int elantech_packet_check_v2(struct psmouse *psmouse)
  558. {
  559. struct elantech_data *etd = psmouse->private;
  560. unsigned char *packet = psmouse->packet;
  561. /*
  562. * V2 hardware has two flavors. Older ones that do not report pressure,
  563. * and newer ones that reports pressure and width. With newer ones, all
  564. * packets (1, 2, 3 finger touch) have the same constant bits. With
  565. * older ones, 1/3 finger touch packets and 2 finger touch packets
  566. * have different constant bits.
  567. * With all three cases, if the constant bits are not exactly what I
  568. * expected, I consider them invalid.
  569. */
  570. if (etd->reports_pressure)
  571. return (packet[0] & 0x0c) == 0x04 &&
  572. (packet[3] & 0x0f) == 0x02;
  573. if ((packet[0] & 0xc0) == 0x80)
  574. return (packet[0] & 0x0c) == 0x0c &&
  575. (packet[3] & 0x0e) == 0x08;
  576. return (packet[0] & 0x3c) == 0x3c &&
  577. (packet[1] & 0xf0) == 0x00 &&
  578. (packet[3] & 0x3e) == 0x38 &&
  579. (packet[4] & 0xf0) == 0x00;
  580. }
  581. /*
  582. * We check the constant bits to determine what packet type we get,
  583. * so packet checking is mandatory for v3 and later hardware.
  584. */
  585. static int elantech_packet_check_v3(struct psmouse *psmouse)
  586. {
  587. struct elantech_data *etd = psmouse->private;
  588. const u8 debounce_packet[] = { 0xc4, 0xff, 0xff, 0x02, 0xff, 0xff };
  589. unsigned char *packet = psmouse->packet;
  590. /*
  591. * check debounce first, it has the same signature in byte 0
  592. * and byte 3 as PACKET_V3_HEAD.
  593. */
  594. if (!memcmp(packet, debounce_packet, sizeof(debounce_packet)))
  595. return PACKET_DEBOUNCE;
  596. /*
  597. * If the hardware flag 'crc_enabled' is set the packets have
  598. * different signatures.
  599. */
  600. if (etd->crc_enabled) {
  601. if ((packet[3] & 0x09) == 0x08)
  602. return PACKET_V3_HEAD;
  603. if ((packet[3] & 0x09) == 0x09)
  604. return PACKET_V3_TAIL;
  605. } else {
  606. if ((packet[0] & 0x0c) == 0x04 && (packet[3] & 0xcf) == 0x02)
  607. return PACKET_V3_HEAD;
  608. if ((packet[0] & 0x0c) == 0x0c && (packet[3] & 0xce) == 0x0c)
  609. return PACKET_V3_TAIL;
  610. }
  611. return PACKET_UNKNOWN;
  612. }
  613. static int elantech_packet_check_v4(struct psmouse *psmouse)
  614. {
  615. struct elantech_data *etd = psmouse->private;
  616. unsigned char *packet = psmouse->packet;
  617. unsigned char packet_type = packet[3] & 0x03;
  618. bool sanity_check;
  619. /*
  620. * Sanity check based on the constant bits of a packet.
  621. * The constant bits change depending on the value of
  622. * the hardware flag 'crc_enabled' but are the same for
  623. * every packet, regardless of the type.
  624. */
  625. if (etd->crc_enabled)
  626. sanity_check = ((packet[3] & 0x08) == 0x00);
  627. else
  628. sanity_check = ((packet[0] & 0x0c) == 0x04 &&
  629. (packet[3] & 0x1c) == 0x10);
  630. if (!sanity_check)
  631. return PACKET_UNKNOWN;
  632. switch (packet_type) {
  633. case 0:
  634. return PACKET_V4_STATUS;
  635. case 1:
  636. return PACKET_V4_HEAD;
  637. case 2:
  638. return PACKET_V4_MOTION;
  639. }
  640. return PACKET_UNKNOWN;
  641. }
  642. /*
  643. * Process byte stream from mouse and handle complete packets
  644. */
  645. static psmouse_ret_t elantech_process_byte(struct psmouse *psmouse)
  646. {
  647. struct elantech_data *etd = psmouse->private;
  648. int packet_type;
  649. if (psmouse->pktcnt < psmouse->pktsize)
  650. return PSMOUSE_GOOD_DATA;
  651. if (etd->debug > 1)
  652. elantech_packet_dump(psmouse);
  653. switch (etd->hw_version) {
  654. case 1:
  655. if (etd->paritycheck && !elantech_packet_check_v1(psmouse))
  656. return PSMOUSE_BAD_DATA;
  657. elantech_report_absolute_v1(psmouse);
  658. break;
  659. case 2:
  660. /* ignore debounce */
  661. if (elantech_debounce_check_v2(psmouse))
  662. return PSMOUSE_FULL_PACKET;
  663. if (etd->paritycheck && !elantech_packet_check_v2(psmouse))
  664. return PSMOUSE_BAD_DATA;
  665. elantech_report_absolute_v2(psmouse);
  666. break;
  667. case 3:
  668. packet_type = elantech_packet_check_v3(psmouse);
  669. /* ignore debounce */
  670. if (packet_type == PACKET_DEBOUNCE)
  671. return PSMOUSE_FULL_PACKET;
  672. if (packet_type == PACKET_UNKNOWN)
  673. return PSMOUSE_BAD_DATA;
  674. elantech_report_absolute_v3(psmouse, packet_type);
  675. break;
  676. case 4:
  677. packet_type = elantech_packet_check_v4(psmouse);
  678. if (packet_type == PACKET_UNKNOWN)
  679. return PSMOUSE_BAD_DATA;
  680. elantech_report_absolute_v4(psmouse, packet_type);
  681. break;
  682. }
  683. return PSMOUSE_FULL_PACKET;
  684. }
  685. /*
  686. * Put the touchpad into absolute mode
  687. */
  688. static int elantech_set_absolute_mode(struct psmouse *psmouse)
  689. {
  690. struct elantech_data *etd = psmouse->private;
  691. unsigned char val;
  692. int tries = ETP_READ_BACK_TRIES;
  693. int rc = 0;
  694. switch (etd->hw_version) {
  695. case 1:
  696. etd->reg_10 = 0x16;
  697. etd->reg_11 = 0x8f;
  698. if (elantech_write_reg(psmouse, 0x10, etd->reg_10) ||
  699. elantech_write_reg(psmouse, 0x11, etd->reg_11)) {
  700. rc = -1;
  701. }
  702. break;
  703. case 2:
  704. /* Windows driver values */
  705. etd->reg_10 = 0x54;
  706. etd->reg_11 = 0x88; /* 0x8a */
  707. etd->reg_21 = 0x60; /* 0x00 */
  708. if (elantech_write_reg(psmouse, 0x10, etd->reg_10) ||
  709. elantech_write_reg(psmouse, 0x11, etd->reg_11) ||
  710. elantech_write_reg(psmouse, 0x21, etd->reg_21)) {
  711. rc = -1;
  712. }
  713. break;
  714. case 3:
  715. etd->reg_10 = 0x0b;
  716. if (elantech_write_reg(psmouse, 0x10, etd->reg_10))
  717. rc = -1;
  718. break;
  719. case 4:
  720. etd->reg_07 = 0x01;
  721. if (elantech_write_reg(psmouse, 0x07, etd->reg_07))
  722. rc = -1;
  723. goto skip_readback_reg_10; /* v4 has no reg 0x10 to read */
  724. }
  725. if (rc == 0) {
  726. /*
  727. * Read back reg 0x10. For hardware version 1 we must make
  728. * sure the absolute mode bit is set. For hardware version 2
  729. * the touchpad is probably initializing and not ready until
  730. * we read back the value we just wrote.
  731. */
  732. do {
  733. rc = elantech_read_reg(psmouse, 0x10, &val);
  734. if (rc == 0)
  735. break;
  736. tries--;
  737. elantech_debug("retrying read (%d).\n", tries);
  738. msleep(ETP_READ_BACK_DELAY);
  739. } while (tries > 0);
  740. if (rc) {
  741. psmouse_err(psmouse,
  742. "failed to read back register 0x10.\n");
  743. } else if (etd->hw_version == 1 &&
  744. !(val & ETP_R10_ABSOLUTE_MODE)) {
  745. psmouse_err(psmouse,
  746. "touchpad refuses to switch to absolute mode.\n");
  747. rc = -1;
  748. }
  749. }
  750. skip_readback_reg_10:
  751. if (rc)
  752. psmouse_err(psmouse, "failed to initialise registers.\n");
  753. return rc;
  754. }
  755. static int elantech_set_range(struct psmouse *psmouse,
  756. unsigned int *x_min, unsigned int *y_min,
  757. unsigned int *x_max, unsigned int *y_max,
  758. unsigned int *width)
  759. {
  760. struct elantech_data *etd = psmouse->private;
  761. unsigned char param[3];
  762. unsigned char traces;
  763. switch (etd->hw_version) {
  764. case 1:
  765. *x_min = ETP_XMIN_V1;
  766. *y_min = ETP_YMIN_V1;
  767. *x_max = ETP_XMAX_V1;
  768. *y_max = ETP_YMAX_V1;
  769. break;
  770. case 2:
  771. if (etd->fw_version == 0x020800 ||
  772. etd->fw_version == 0x020b00 ||
  773. etd->fw_version == 0x020030) {
  774. *x_min = ETP_XMIN_V2;
  775. *y_min = ETP_YMIN_V2;
  776. *x_max = ETP_XMAX_V2;
  777. *y_max = ETP_YMAX_V2;
  778. } else {
  779. int i;
  780. int fixed_dpi;
  781. i = (etd->fw_version > 0x020800 &&
  782. etd->fw_version < 0x020900) ? 1 : 2;
  783. if (etd->send_cmd(psmouse, ETP_FW_ID_QUERY, param))
  784. return -1;
  785. fixed_dpi = param[1] & 0x10;
  786. if (((etd->fw_version >> 16) == 0x14) && fixed_dpi) {
  787. if (etd->send_cmd(psmouse, ETP_SAMPLE_QUERY, param))
  788. return -1;
  789. *x_max = (etd->capabilities[1] - i) * param[1] / 2;
  790. *y_max = (etd->capabilities[2] - i) * param[2] / 2;
  791. } else if (etd->fw_version == 0x040216) {
  792. *x_max = 819;
  793. *y_max = 405;
  794. } else if (etd->fw_version == 0x040219 || etd->fw_version == 0x040215) {
  795. *x_max = 900;
  796. *y_max = 500;
  797. } else {
  798. *x_max = (etd->capabilities[1] - i) * 64;
  799. *y_max = (etd->capabilities[2] - i) * 64;
  800. }
  801. }
  802. break;
  803. case 3:
  804. if (etd->send_cmd(psmouse, ETP_FW_ID_QUERY, param))
  805. return -1;
  806. *x_max = (0x0f & param[0]) << 8 | param[1];
  807. *y_max = (0xf0 & param[0]) << 4 | param[2];
  808. break;
  809. case 4:
  810. if (etd->send_cmd(psmouse, ETP_FW_ID_QUERY, param))
  811. return -1;
  812. *x_max = (0x0f & param[0]) << 8 | param[1];
  813. *y_max = (0xf0 & param[0]) << 4 | param[2];
  814. traces = etd->capabilities[1];
  815. if ((traces < 2) || (traces > *x_max))
  816. return -1;
  817. *width = *x_max / (traces - 1);
  818. break;
  819. }
  820. return 0;
  821. }
  822. /*
  823. * (value from firmware) * 10 + 790 = dpi
  824. * we also have to convert dpi to dots/mm (*10/254 to avoid floating point)
  825. */
  826. static unsigned int elantech_convert_res(unsigned int val)
  827. {
  828. return (val * 10 + 790) * 10 / 254;
  829. }
  830. static int elantech_get_resolution_v4(struct psmouse *psmouse,
  831. unsigned int *x_res,
  832. unsigned int *y_res)
  833. {
  834. unsigned char param[3];
  835. if (elantech_send_cmd(psmouse, ETP_RESOLUTION_QUERY, param))
  836. return -1;
  837. *x_res = elantech_convert_res(param[1] & 0x0f);
  838. *y_res = elantech_convert_res((param[1] & 0xf0) >> 4);
  839. return 0;
  840. }
  841. /*
  842. * Set the appropriate event bits for the input subsystem
  843. */
  844. static int elantech_set_input_params(struct psmouse *psmouse)
  845. {
  846. struct input_dev *dev = psmouse->dev;
  847. struct elantech_data *etd = psmouse->private;
  848. unsigned int x_min = 0, y_min = 0, x_max = 0, y_max = 0, width = 0;
  849. unsigned int x_res = 0, y_res = 0;
  850. if (elantech_set_range(psmouse, &x_min, &y_min, &x_max, &y_max, &width))
  851. return -1;
  852. __set_bit(INPUT_PROP_POINTER, dev->propbit);
  853. __set_bit(EV_KEY, dev->evbit);
  854. __set_bit(EV_ABS, dev->evbit);
  855. __clear_bit(EV_REL, dev->evbit);
  856. __set_bit(BTN_LEFT, dev->keybit);
  857. __set_bit(BTN_RIGHT, dev->keybit);
  858. __set_bit(BTN_TOUCH, dev->keybit);
  859. __set_bit(BTN_TOOL_FINGER, dev->keybit);
  860. __set_bit(BTN_TOOL_DOUBLETAP, dev->keybit);
  861. __set_bit(BTN_TOOL_TRIPLETAP, dev->keybit);
  862. switch (etd->hw_version) {
  863. case 1:
  864. /* Rocker button */
  865. if (etd->fw_version < 0x020000 &&
  866. (etd->capabilities[0] & ETP_CAP_HAS_ROCKER)) {
  867. __set_bit(BTN_FORWARD, dev->keybit);
  868. __set_bit(BTN_BACK, dev->keybit);
  869. }
  870. input_set_abs_params(dev, ABS_X, x_min, x_max, 0, 0);
  871. input_set_abs_params(dev, ABS_Y, y_min, y_max, 0, 0);
  872. break;
  873. case 2:
  874. __set_bit(BTN_TOOL_QUADTAP, dev->keybit);
  875. __set_bit(INPUT_PROP_SEMI_MT, dev->propbit);
  876. /* fall through */
  877. case 3:
  878. input_set_abs_params(dev, ABS_X, x_min, x_max, 0, 0);
  879. input_set_abs_params(dev, ABS_Y, y_min, y_max, 0, 0);
  880. if (etd->reports_pressure) {
  881. input_set_abs_params(dev, ABS_PRESSURE, ETP_PMIN_V2,
  882. ETP_PMAX_V2, 0, 0);
  883. input_set_abs_params(dev, ABS_TOOL_WIDTH, ETP_WMIN_V2,
  884. ETP_WMAX_V2, 0, 0);
  885. }
  886. input_mt_init_slots(dev, 2, 0);
  887. input_set_abs_params(dev, ABS_MT_POSITION_X, x_min, x_max, 0, 0);
  888. input_set_abs_params(dev, ABS_MT_POSITION_Y, y_min, y_max, 0, 0);
  889. break;
  890. case 4:
  891. if (elantech_get_resolution_v4(psmouse, &x_res, &y_res)) {
  892. /*
  893. * if query failed, print a warning and leave the values
  894. * zero to resemble synaptics.c behavior.
  895. */
  896. psmouse_warn(psmouse, "couldn't query resolution data.\n");
  897. }
  898. /* v4 is clickpad, with only one button. */
  899. __set_bit(INPUT_PROP_BUTTONPAD, dev->propbit);
  900. __clear_bit(BTN_RIGHT, dev->keybit);
  901. __set_bit(BTN_TOOL_QUADTAP, dev->keybit);
  902. /* For X to recognize me as touchpad. */
  903. input_set_abs_params(dev, ABS_X, x_min, x_max, 0, 0);
  904. input_set_abs_params(dev, ABS_Y, y_min, y_max, 0, 0);
  905. input_abs_set_res(dev, ABS_X, x_res);
  906. input_abs_set_res(dev, ABS_Y, y_res);
  907. /*
  908. * range of pressure and width is the same as v2,
  909. * report ABS_PRESSURE, ABS_TOOL_WIDTH for compatibility.
  910. */
  911. input_set_abs_params(dev, ABS_PRESSURE, ETP_PMIN_V2,
  912. ETP_PMAX_V2, 0, 0);
  913. input_set_abs_params(dev, ABS_TOOL_WIDTH, ETP_WMIN_V2,
  914. ETP_WMAX_V2, 0, 0);
  915. /* Multitouch capable pad, up to 5 fingers. */
  916. input_mt_init_slots(dev, ETP_MAX_FINGERS, 0);
  917. input_set_abs_params(dev, ABS_MT_POSITION_X, x_min, x_max, 0, 0);
  918. input_set_abs_params(dev, ABS_MT_POSITION_Y, y_min, y_max, 0, 0);
  919. input_abs_set_res(dev, ABS_MT_POSITION_X, x_res);
  920. input_abs_set_res(dev, ABS_MT_POSITION_Y, y_res);
  921. input_set_abs_params(dev, ABS_MT_PRESSURE, ETP_PMIN_V2,
  922. ETP_PMAX_V2, 0, 0);
  923. /*
  924. * The firmware reports how many trace lines the finger spans,
  925. * convert to surface unit as Protocol-B requires.
  926. */
  927. input_set_abs_params(dev, ABS_MT_TOUCH_MAJOR, 0,
  928. ETP_WMAX_V2 * width, 0, 0);
  929. break;
  930. }
  931. etd->y_max = y_max;
  932. etd->width = width;
  933. return 0;
  934. }
  935. struct elantech_attr_data {
  936. size_t field_offset;
  937. unsigned char reg;
  938. };
  939. /*
  940. * Display a register value by reading a sysfs entry
  941. */
  942. static ssize_t elantech_show_int_attr(struct psmouse *psmouse, void *data,
  943. char *buf)
  944. {
  945. struct elantech_data *etd = psmouse->private;
  946. struct elantech_attr_data *attr = data;
  947. unsigned char *reg = (unsigned char *) etd + attr->field_offset;
  948. int rc = 0;
  949. if (attr->reg)
  950. rc = elantech_read_reg(psmouse, attr->reg, reg);
  951. return sprintf(buf, "0x%02x\n", (attr->reg && rc) ? -1 : *reg);
  952. }
  953. /*
  954. * Write a register value by writing a sysfs entry
  955. */
  956. static ssize_t elantech_set_int_attr(struct psmouse *psmouse,
  957. void *data, const char *buf, size_t count)
  958. {
  959. struct elantech_data *etd = psmouse->private;
  960. struct elantech_attr_data *attr = data;
  961. unsigned char *reg = (unsigned char *) etd + attr->field_offset;
  962. unsigned char value;
  963. int err;
  964. err = kstrtou8(buf, 16, &value);
  965. if (err)
  966. return err;
  967. /* Do we need to preserve some bits for version 2 hardware too? */
  968. if (etd->hw_version == 1) {
  969. if (attr->reg == 0x10)
  970. /* Force absolute mode always on */
  971. value |= ETP_R10_ABSOLUTE_MODE;
  972. else if (attr->reg == 0x11)
  973. /* Force 4 byte mode always on */
  974. value |= ETP_R11_4_BYTE_MODE;
  975. }
  976. if (!attr->reg || elantech_write_reg(psmouse, attr->reg, value) == 0)
  977. *reg = value;
  978. return count;
  979. }
  980. #define ELANTECH_INT_ATTR(_name, _register) \
  981. static struct elantech_attr_data elantech_attr_##_name = { \
  982. .field_offset = offsetof(struct elantech_data, _name), \
  983. .reg = _register, \
  984. }; \
  985. PSMOUSE_DEFINE_ATTR(_name, S_IWUSR | S_IRUGO, \
  986. &elantech_attr_##_name, \
  987. elantech_show_int_attr, \
  988. elantech_set_int_attr)
  989. ELANTECH_INT_ATTR(reg_07, 0x07);
  990. ELANTECH_INT_ATTR(reg_10, 0x10);
  991. ELANTECH_INT_ATTR(reg_11, 0x11);
  992. ELANTECH_INT_ATTR(reg_20, 0x20);
  993. ELANTECH_INT_ATTR(reg_21, 0x21);
  994. ELANTECH_INT_ATTR(reg_22, 0x22);
  995. ELANTECH_INT_ATTR(reg_23, 0x23);
  996. ELANTECH_INT_ATTR(reg_24, 0x24);
  997. ELANTECH_INT_ATTR(reg_25, 0x25);
  998. ELANTECH_INT_ATTR(reg_26, 0x26);
  999. ELANTECH_INT_ATTR(debug, 0);
  1000. ELANTECH_INT_ATTR(paritycheck, 0);
  1001. static struct attribute *elantech_attrs[] = {
  1002. &psmouse_attr_reg_07.dattr.attr,
  1003. &psmouse_attr_reg_10.dattr.attr,
  1004. &psmouse_attr_reg_11.dattr.attr,
  1005. &psmouse_attr_reg_20.dattr.attr,
  1006. &psmouse_attr_reg_21.dattr.attr,
  1007. &psmouse_attr_reg_22.dattr.attr,
  1008. &psmouse_attr_reg_23.dattr.attr,
  1009. &psmouse_attr_reg_24.dattr.attr,
  1010. &psmouse_attr_reg_25.dattr.attr,
  1011. &psmouse_attr_reg_26.dattr.attr,
  1012. &psmouse_attr_debug.dattr.attr,
  1013. &psmouse_attr_paritycheck.dattr.attr,
  1014. NULL
  1015. };
  1016. static struct attribute_group elantech_attr_group = {
  1017. .attrs = elantech_attrs,
  1018. };
  1019. static bool elantech_is_signature_valid(const unsigned char *param)
  1020. {
  1021. static const unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10 };
  1022. int i;
  1023. if (param[0] == 0)
  1024. return false;
  1025. if (param[1] == 0)
  1026. return true;
  1027. for (i = 0; i < ARRAY_SIZE(rates); i++)
  1028. if (param[2] == rates[i])
  1029. return false;
  1030. return true;
  1031. }
  1032. /*
  1033. * Use magic knock to detect Elantech touchpad
  1034. */
  1035. int elantech_detect(struct psmouse *psmouse, bool set_properties)
  1036. {
  1037. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1038. unsigned char param[3];
  1039. ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
  1040. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  1041. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  1042. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  1043. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  1044. ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) {
  1045. psmouse_dbg(psmouse, "sending Elantech magic knock failed.\n");
  1046. return -1;
  1047. }
  1048. /*
  1049. * Report this in case there are Elantech models that use a different
  1050. * set of magic numbers
  1051. */
  1052. if (param[0] != 0x3c || param[1] != 0x03 ||
  1053. (param[2] != 0xc8 && param[2] != 0x00)) {
  1054. psmouse_dbg(psmouse,
  1055. "unexpected magic knock result 0x%02x, 0x%02x, 0x%02x.\n",
  1056. param[0], param[1], param[2]);
  1057. return -1;
  1058. }
  1059. /*
  1060. * Query touchpad's firmware version and see if it reports known
  1061. * value to avoid mis-detection. Logitech mice are known to respond
  1062. * to Elantech magic knock and there might be more.
  1063. */
  1064. if (synaptics_send_cmd(psmouse, ETP_FW_VERSION_QUERY, param)) {
  1065. psmouse_dbg(psmouse, "failed to query firmware version.\n");
  1066. return -1;
  1067. }
  1068. psmouse_dbg(psmouse,
  1069. "Elantech version query result 0x%02x, 0x%02x, 0x%02x.\n",
  1070. param[0], param[1], param[2]);
  1071. if (!elantech_is_signature_valid(param)) {
  1072. psmouse_dbg(psmouse,
  1073. "Probably not a real Elantech touchpad. Aborting.\n");
  1074. return -1;
  1075. }
  1076. if (set_properties) {
  1077. psmouse->vendor = "Elantech";
  1078. psmouse->name = "Touchpad";
  1079. }
  1080. return 0;
  1081. }
  1082. /*
  1083. * Clean up sysfs entries when disconnecting
  1084. */
  1085. static void elantech_disconnect(struct psmouse *psmouse)
  1086. {
  1087. sysfs_remove_group(&psmouse->ps2dev.serio->dev.kobj,
  1088. &elantech_attr_group);
  1089. kfree(psmouse->private);
  1090. psmouse->private = NULL;
  1091. }
  1092. /*
  1093. * Put the touchpad back into absolute mode when reconnecting
  1094. */
  1095. static int elantech_reconnect(struct psmouse *psmouse)
  1096. {
  1097. psmouse_reset(psmouse);
  1098. if (elantech_detect(psmouse, 0))
  1099. return -1;
  1100. if (elantech_set_absolute_mode(psmouse)) {
  1101. psmouse_err(psmouse,
  1102. "failed to put touchpad back into absolute mode.\n");
  1103. return -1;
  1104. }
  1105. return 0;
  1106. }
  1107. /*
  1108. * determine hardware version and set some properties according to it.
  1109. */
  1110. static int elantech_set_properties(struct elantech_data *etd)
  1111. {
  1112. /* This represents the version of IC body. */
  1113. int ver = (etd->fw_version & 0x0f0000) >> 16;
  1114. /* Early version of Elan touchpads doesn't obey the rule. */
  1115. if (etd->fw_version < 0x020030 || etd->fw_version == 0x020600)
  1116. etd->hw_version = 1;
  1117. else {
  1118. switch (ver) {
  1119. case 2:
  1120. case 4:
  1121. etd->hw_version = 2;
  1122. break;
  1123. case 5:
  1124. etd->hw_version = 3;
  1125. break;
  1126. case 6:
  1127. case 7:
  1128. etd->hw_version = 4;
  1129. break;
  1130. default:
  1131. return -1;
  1132. }
  1133. }
  1134. /* decide which send_cmd we're gonna use early */
  1135. etd->send_cmd = etd->hw_version >= 3 ? elantech_send_cmd :
  1136. synaptics_send_cmd;
  1137. /* Turn on packet checking by default */
  1138. etd->paritycheck = 1;
  1139. /*
  1140. * This firmware suffers from misreporting coordinates when
  1141. * a touch action starts causing the mouse cursor or scrolled page
  1142. * to jump. Enable a workaround.
  1143. */
  1144. etd->jumpy_cursor =
  1145. (etd->fw_version == 0x020022 || etd->fw_version == 0x020600);
  1146. if (etd->hw_version > 1) {
  1147. /* For now show extra debug information */
  1148. etd->debug = 1;
  1149. if (etd->fw_version >= 0x020800)
  1150. etd->reports_pressure = true;
  1151. }
  1152. /*
  1153. * The signatures of v3 and v4 packets change depending on the
  1154. * value of this hardware flag.
  1155. */
  1156. etd->crc_enabled = ((etd->fw_version & 0x4000) == 0x4000);
  1157. return 0;
  1158. }
  1159. /*
  1160. * Initialize the touchpad and create sysfs entries
  1161. */
  1162. int elantech_init(struct psmouse *psmouse)
  1163. {
  1164. struct elantech_data *etd;
  1165. int i, error;
  1166. unsigned char param[3];
  1167. psmouse->private = etd = kzalloc(sizeof(struct elantech_data), GFP_KERNEL);
  1168. if (!etd)
  1169. return -ENOMEM;
  1170. psmouse_reset(psmouse);
  1171. etd->parity[0] = 1;
  1172. for (i = 1; i < 256; i++)
  1173. etd->parity[i] = etd->parity[i & (i - 1)] ^ 1;
  1174. /*
  1175. * Do the version query again so we can store the result
  1176. */
  1177. if (synaptics_send_cmd(psmouse, ETP_FW_VERSION_QUERY, param)) {
  1178. psmouse_err(psmouse, "failed to query firmware version.\n");
  1179. goto init_fail;
  1180. }
  1181. etd->fw_version = (param[0] << 16) | (param[1] << 8) | param[2];
  1182. if (elantech_set_properties(etd)) {
  1183. psmouse_err(psmouse, "unknown hardware version, aborting...\n");
  1184. goto init_fail;
  1185. }
  1186. psmouse_info(psmouse,
  1187. "assuming hardware version %d (with firmware version 0x%02x%02x%02x)\n",
  1188. etd->hw_version, param[0], param[1], param[2]);
  1189. if (etd->send_cmd(psmouse, ETP_CAPABILITIES_QUERY,
  1190. etd->capabilities)) {
  1191. psmouse_err(psmouse, "failed to query capabilities.\n");
  1192. goto init_fail;
  1193. }
  1194. psmouse_info(psmouse,
  1195. "Synaptics capabilities query result 0x%02x, 0x%02x, 0x%02x.\n",
  1196. etd->capabilities[0], etd->capabilities[1],
  1197. etd->capabilities[2]);
  1198. if (elantech_set_absolute_mode(psmouse)) {
  1199. psmouse_err(psmouse,
  1200. "failed to put touchpad into absolute mode.\n");
  1201. goto init_fail;
  1202. }
  1203. if (elantech_set_input_params(psmouse)) {
  1204. psmouse_err(psmouse, "failed to query touchpad range.\n");
  1205. goto init_fail;
  1206. }
  1207. error = sysfs_create_group(&psmouse->ps2dev.serio->dev.kobj,
  1208. &elantech_attr_group);
  1209. if (error) {
  1210. psmouse_err(psmouse,
  1211. "failed to create sysfs attributes, error: %d.\n",
  1212. error);
  1213. goto init_fail;
  1214. }
  1215. psmouse->protocol_handler = elantech_process_byte;
  1216. psmouse->disconnect = elantech_disconnect;
  1217. psmouse->reconnect = elantech_reconnect;
  1218. psmouse->pktsize = etd->hw_version > 1 ? 6 : 4;
  1219. return 0;
  1220. init_fail:
  1221. kfree(etd);
  1222. return -1;
  1223. }