elantech.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  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. #define pr_fmt(fmt) KBUILD_BASENAME ": " fmt
  13. #include <linux/delay.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 "psmouse.h"
  21. #include "elantech.h"
  22. #define elantech_debug(fmt, ...) \
  23. do { \
  24. if (etd->debug) \
  25. printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); \
  26. } while (0)
  27. static bool force_elantech;
  28. module_param_named(force_elantech, force_elantech, bool, 0644);
  29. MODULE_PARM_DESC(force_elantech, "Force the Elantech PS/2 protocol extension to be used, 1 = enabled, 0 = disabled (default).");
  30. /*
  31. * Send a Synaptics style sliced query command
  32. */
  33. static int synaptics_send_cmd(struct psmouse *psmouse, unsigned char c,
  34. unsigned char *param)
  35. {
  36. if (psmouse_sliced_command(psmouse, c) ||
  37. ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_GETINFO)) {
  38. pr_err("synaptics_send_cmd query 0x%02x failed.\n", c);
  39. return -1;
  40. }
  41. return 0;
  42. }
  43. /*
  44. * A retrying version of ps2_command
  45. */
  46. static int elantech_ps2_command(struct psmouse *psmouse,
  47. unsigned char *param, int command)
  48. {
  49. struct ps2dev *ps2dev = &psmouse->ps2dev;
  50. struct elantech_data *etd = psmouse->private;
  51. int rc;
  52. int tries = ETP_PS2_COMMAND_TRIES;
  53. do {
  54. rc = ps2_command(ps2dev, param, command);
  55. if (rc == 0)
  56. break;
  57. tries--;
  58. elantech_debug("retrying ps2 command 0x%02x (%d).\n",
  59. command, tries);
  60. msleep(ETP_PS2_COMMAND_DELAY);
  61. } while (tries > 0);
  62. if (rc)
  63. pr_err("ps2 command 0x%02x failed.\n", command);
  64. return rc;
  65. }
  66. /*
  67. * Send an Elantech style special command to read a value from a register
  68. */
  69. static int elantech_read_reg(struct psmouse *psmouse, unsigned char reg,
  70. unsigned char *val)
  71. {
  72. struct elantech_data *etd = psmouse->private;
  73. unsigned char param[3];
  74. int rc = 0;
  75. if (reg < 0x10 || reg > 0x26)
  76. return -1;
  77. if (reg > 0x11 && reg < 0x20)
  78. return -1;
  79. switch (etd->hw_version) {
  80. case 1:
  81. if (psmouse_sliced_command(psmouse, ETP_REGISTER_READ) ||
  82. psmouse_sliced_command(psmouse, reg) ||
  83. ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_GETINFO)) {
  84. rc = -1;
  85. }
  86. break;
  87. case 2:
  88. if (elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
  89. elantech_ps2_command(psmouse, NULL, ETP_REGISTER_READ) ||
  90. elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
  91. elantech_ps2_command(psmouse, NULL, reg) ||
  92. elantech_ps2_command(psmouse, param, PSMOUSE_CMD_GETINFO)) {
  93. rc = -1;
  94. }
  95. break;
  96. }
  97. if (rc)
  98. pr_err("failed to read register 0x%02x.\n", reg);
  99. else
  100. *val = param[0];
  101. return rc;
  102. }
  103. /*
  104. * Send an Elantech style special command to write a register with a value
  105. */
  106. static int elantech_write_reg(struct psmouse *psmouse, unsigned char reg,
  107. unsigned char val)
  108. {
  109. struct elantech_data *etd = psmouse->private;
  110. int rc = 0;
  111. if (reg < 0x10 || reg > 0x26)
  112. return -1;
  113. if (reg > 0x11 && reg < 0x20)
  114. return -1;
  115. switch (etd->hw_version) {
  116. case 1:
  117. if (psmouse_sliced_command(psmouse, ETP_REGISTER_WRITE) ||
  118. psmouse_sliced_command(psmouse, reg) ||
  119. psmouse_sliced_command(psmouse, val) ||
  120. ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11)) {
  121. rc = -1;
  122. }
  123. break;
  124. case 2:
  125. if (elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
  126. elantech_ps2_command(psmouse, NULL, ETP_REGISTER_WRITE) ||
  127. elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
  128. elantech_ps2_command(psmouse, NULL, reg) ||
  129. elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
  130. elantech_ps2_command(psmouse, NULL, val) ||
  131. elantech_ps2_command(psmouse, NULL, PSMOUSE_CMD_SETSCALE11)) {
  132. rc = -1;
  133. }
  134. break;
  135. }
  136. if (rc)
  137. pr_err("failed to write register 0x%02x with value 0x%02x.\n",
  138. reg, val);
  139. return rc;
  140. }
  141. /*
  142. * Dump a complete mouse movement packet to the syslog
  143. */
  144. static void elantech_packet_dump(unsigned char *packet, int size)
  145. {
  146. int i;
  147. printk(KERN_DEBUG pr_fmt("PS/2 packet ["));
  148. for (i = 0; i < size; i++)
  149. printk("%s0x%02x ", (i) ? ", " : " ", packet[i]);
  150. printk("]\n");
  151. }
  152. /*
  153. * Interpret complete data packets and report absolute mode input events for
  154. * hardware version 1. (4 byte packets)
  155. */
  156. static void elantech_report_absolute_v1(struct psmouse *psmouse)
  157. {
  158. struct input_dev *dev = psmouse->dev;
  159. struct elantech_data *etd = psmouse->private;
  160. unsigned char *packet = psmouse->packet;
  161. int fingers;
  162. if (etd->fw_version < 0x020000) {
  163. /*
  164. * byte 0: D U p1 p2 1 p3 R L
  165. * byte 1: f 0 th tw x9 x8 y9 y8
  166. */
  167. fingers = ((packet[1] & 0x80) >> 7) +
  168. ((packet[1] & 0x30) >> 4);
  169. } else {
  170. /*
  171. * byte 0: n1 n0 p2 p1 1 p3 R L
  172. * byte 1: 0 0 0 0 x9 x8 y9 y8
  173. */
  174. fingers = (packet[0] & 0xc0) >> 6;
  175. }
  176. if (etd->jumpy_cursor) {
  177. if (fingers != 1) {
  178. etd->single_finger_reports = 0;
  179. } else if (etd->single_finger_reports < 2) {
  180. /* Discard first 2 reports of one finger, bogus */
  181. etd->single_finger_reports++;
  182. elantech_debug("discarding packet\n");
  183. return;
  184. }
  185. }
  186. input_report_key(dev, BTN_TOUCH, fingers != 0);
  187. /*
  188. * byte 2: x7 x6 x5 x4 x3 x2 x1 x0
  189. * byte 3: y7 y6 y5 y4 y3 y2 y1 y0
  190. */
  191. if (fingers) {
  192. input_report_abs(dev, ABS_X,
  193. ((packet[1] & 0x0c) << 6) | packet[2]);
  194. input_report_abs(dev, ABS_Y,
  195. ETP_YMAX_V1 - (((packet[1] & 0x03) << 8) | packet[3]));
  196. }
  197. input_report_key(dev, BTN_TOOL_FINGER, fingers == 1);
  198. input_report_key(dev, BTN_TOOL_DOUBLETAP, fingers == 2);
  199. input_report_key(dev, BTN_TOOL_TRIPLETAP, fingers == 3);
  200. input_report_key(dev, BTN_LEFT, packet[0] & 0x01);
  201. input_report_key(dev, BTN_RIGHT, packet[0] & 0x02);
  202. if (etd->fw_version < 0x020000 &&
  203. (etd->capabilities & ETP_CAP_HAS_ROCKER)) {
  204. /* rocker up */
  205. input_report_key(dev, BTN_FORWARD, packet[0] & 0x40);
  206. /* rocker down */
  207. input_report_key(dev, BTN_BACK, packet[0] & 0x80);
  208. }
  209. input_sync(dev);
  210. }
  211. static void elantech_set_slot(struct input_dev *dev, int slot, bool active,
  212. unsigned int x, unsigned int y)
  213. {
  214. input_mt_slot(dev, slot);
  215. input_mt_report_slot_state(dev, MT_TOOL_FINGER, active);
  216. if (active) {
  217. input_report_abs(dev, ABS_MT_POSITION_X, x);
  218. input_report_abs(dev, ABS_MT_POSITION_Y, y);
  219. }
  220. }
  221. /* x1 < x2 and y1 < y2 when two fingers, x = y = 0 when not pressed */
  222. static void elantech_report_semi_mt_data(struct input_dev *dev,
  223. unsigned int num_fingers,
  224. unsigned int x1, unsigned int y1,
  225. unsigned int x2, unsigned int y2)
  226. {
  227. elantech_set_slot(dev, 0, num_fingers != 0, x1, y1);
  228. elantech_set_slot(dev, 1, num_fingers == 2, x2, y2);
  229. }
  230. /*
  231. * Interpret complete data packets and report absolute mode input events for
  232. * hardware version 2. (6 byte packets)
  233. */
  234. static void elantech_report_absolute_v2(struct psmouse *psmouse)
  235. {
  236. struct elantech_data *etd = psmouse->private;
  237. struct input_dev *dev = psmouse->dev;
  238. unsigned char *packet = psmouse->packet;
  239. unsigned int fingers, x1 = 0, y1 = 0, x2 = 0, y2 = 0, width = 0, pres = 0;
  240. /* byte 0: n1 n0 . . . . R L */
  241. fingers = (packet[0] & 0xc0) >> 6;
  242. input_report_key(dev, BTN_TOUCH, fingers != 0);
  243. switch (fingers) {
  244. case 3:
  245. /*
  246. * Same as one finger, except report of more than 3 fingers:
  247. * byte 3: n4 . w1 w0 . . . .
  248. */
  249. if (packet[3] & 0x80)
  250. fingers = 4;
  251. /* pass through... */
  252. case 1:
  253. /*
  254. * byte 1: . . . . . x10 x9 x8
  255. * byte 2: x7 x6 x5 x4 x4 x2 x1 x0
  256. */
  257. x1 = ((packet[1] & 0x07) << 8) | packet[2];
  258. /*
  259. * byte 4: . . . . . . y9 y8
  260. * byte 5: y7 y6 y5 y4 y3 y2 y1 y0
  261. */
  262. y1 = ETP_YMAX_V2 - (((packet[4] & 0x03) << 8) | packet[5]);
  263. input_report_abs(dev, ABS_X, x1);
  264. input_report_abs(dev, ABS_Y, y1);
  265. pres = (packet[1] & 0xf0) | ((packet[4] & 0xf0) >> 4);
  266. width = ((packet[0] & 0x30) >> 2) | ((packet[3] & 0x30) >> 4);
  267. break;
  268. case 2:
  269. /*
  270. * The coordinate of each finger is reported separately
  271. * with a lower resolution for two finger touches:
  272. * byte 0: . . ay8 ax8 . . . .
  273. * byte 1: ax7 ax6 ax5 ax4 ax3 ax2 ax1 ax0
  274. */
  275. x1 = ((packet[0] & 0x10) << 4) | packet[1];
  276. /* byte 2: ay7 ay6 ay5 ay4 ay3 ay2 ay1 ay0 */
  277. y1 = ETP_2FT_YMAX - (((packet[0] & 0x20) << 3) | packet[2]);
  278. /*
  279. * byte 3: . . by8 bx8 . . . .
  280. * byte 4: bx7 bx6 bx5 bx4 bx3 bx2 bx1 bx0
  281. */
  282. x2 = ((packet[3] & 0x10) << 4) | packet[4];
  283. /* byte 5: by7 by8 by5 by4 by3 by2 by1 by0 */
  284. y2 = ETP_2FT_YMAX - (((packet[3] & 0x20) << 3) | packet[5]);
  285. /*
  286. * For compatibility with the X Synaptics driver scale up
  287. * one coordinate and report as ordinary mouse movent
  288. */
  289. input_report_abs(dev, ABS_X, x1 << 2);
  290. input_report_abs(dev, ABS_Y, y1 << 2);
  291. /* Unknown so just report sensible values */
  292. pres = 127;
  293. width = 7;
  294. break;
  295. }
  296. elantech_report_semi_mt_data(dev, fingers, x1, y1, x2, y2);
  297. input_report_key(dev, BTN_TOOL_FINGER, fingers == 1);
  298. input_report_key(dev, BTN_TOOL_DOUBLETAP, fingers == 2);
  299. input_report_key(dev, BTN_TOOL_TRIPLETAP, fingers == 3);
  300. input_report_key(dev, BTN_TOOL_QUADTAP, fingers == 4);
  301. input_report_key(dev, BTN_LEFT, packet[0] & 0x01);
  302. input_report_key(dev, BTN_RIGHT, packet[0] & 0x02);
  303. if (etd->reports_pressure) {
  304. input_report_abs(dev, ABS_PRESSURE, pres);
  305. input_report_abs(dev, ABS_TOOL_WIDTH, width);
  306. }
  307. input_sync(dev);
  308. }
  309. static int elantech_check_parity_v1(struct psmouse *psmouse)
  310. {
  311. struct elantech_data *etd = psmouse->private;
  312. unsigned char *packet = psmouse->packet;
  313. unsigned char p1, p2, p3;
  314. /* Parity bits are placed differently */
  315. if (etd->fw_version < 0x020000) {
  316. /* byte 0: D U p1 p2 1 p3 R L */
  317. p1 = (packet[0] & 0x20) >> 5;
  318. p2 = (packet[0] & 0x10) >> 4;
  319. } else {
  320. /* byte 0: n1 n0 p2 p1 1 p3 R L */
  321. p1 = (packet[0] & 0x10) >> 4;
  322. p2 = (packet[0] & 0x20) >> 5;
  323. }
  324. p3 = (packet[0] & 0x04) >> 2;
  325. return etd->parity[packet[1]] == p1 &&
  326. etd->parity[packet[2]] == p2 &&
  327. etd->parity[packet[3]] == p3;
  328. }
  329. /*
  330. * Process byte stream from mouse and handle complete packets
  331. */
  332. static psmouse_ret_t elantech_process_byte(struct psmouse *psmouse)
  333. {
  334. struct elantech_data *etd = psmouse->private;
  335. if (psmouse->pktcnt < psmouse->pktsize)
  336. return PSMOUSE_GOOD_DATA;
  337. if (etd->debug > 1)
  338. elantech_packet_dump(psmouse->packet, psmouse->pktsize);
  339. switch (etd->hw_version) {
  340. case 1:
  341. if (etd->paritycheck && !elantech_check_parity_v1(psmouse))
  342. return PSMOUSE_BAD_DATA;
  343. elantech_report_absolute_v1(psmouse);
  344. break;
  345. case 2:
  346. /* We don't know how to check parity in protocol v2 */
  347. elantech_report_absolute_v2(psmouse);
  348. break;
  349. }
  350. return PSMOUSE_FULL_PACKET;
  351. }
  352. /*
  353. * Put the touchpad into absolute mode
  354. */
  355. static int elantech_set_absolute_mode(struct psmouse *psmouse)
  356. {
  357. struct elantech_data *etd = psmouse->private;
  358. unsigned char val;
  359. int tries = ETP_READ_BACK_TRIES;
  360. int rc = 0;
  361. switch (etd->hw_version) {
  362. case 1:
  363. etd->reg_10 = 0x16;
  364. etd->reg_11 = 0x8f;
  365. if (elantech_write_reg(psmouse, 0x10, etd->reg_10) ||
  366. elantech_write_reg(psmouse, 0x11, etd->reg_11)) {
  367. rc = -1;
  368. }
  369. break;
  370. case 2:
  371. /* Windows driver values */
  372. etd->reg_10 = 0x54;
  373. etd->reg_11 = 0x88; /* 0x8a */
  374. etd->reg_21 = 0x60; /* 0x00 */
  375. if (elantech_write_reg(psmouse, 0x10, etd->reg_10) ||
  376. elantech_write_reg(psmouse, 0x11, etd->reg_11) ||
  377. elantech_write_reg(psmouse, 0x21, etd->reg_21)) {
  378. rc = -1;
  379. break;
  380. }
  381. }
  382. if (rc == 0) {
  383. /*
  384. * Read back reg 0x10. For hardware version 1 we must make
  385. * sure the absolute mode bit is set. For hardware version 2
  386. * the touchpad is probably initalising and not ready until
  387. * we read back the value we just wrote.
  388. */
  389. do {
  390. rc = elantech_read_reg(psmouse, 0x10, &val);
  391. if (rc == 0)
  392. break;
  393. tries--;
  394. elantech_debug("retrying read (%d).\n", tries);
  395. msleep(ETP_READ_BACK_DELAY);
  396. } while (tries > 0);
  397. if (rc) {
  398. pr_err("failed to read back register 0x10.\n");
  399. } else if (etd->hw_version == 1 &&
  400. !(val & ETP_R10_ABSOLUTE_MODE)) {
  401. pr_err("touchpad refuses to switch to absolute mode.\n");
  402. rc = -1;
  403. }
  404. }
  405. if (rc)
  406. pr_err("failed to initialise registers.\n");
  407. return rc;
  408. }
  409. /*
  410. * Set the appropriate event bits for the input subsystem
  411. */
  412. static void elantech_set_input_params(struct psmouse *psmouse)
  413. {
  414. struct input_dev *dev = psmouse->dev;
  415. struct elantech_data *etd = psmouse->private;
  416. __set_bit(EV_KEY, dev->evbit);
  417. __set_bit(EV_ABS, dev->evbit);
  418. __clear_bit(EV_REL, dev->evbit);
  419. __set_bit(BTN_LEFT, dev->keybit);
  420. __set_bit(BTN_RIGHT, dev->keybit);
  421. __set_bit(BTN_TOUCH, dev->keybit);
  422. __set_bit(BTN_TOOL_FINGER, dev->keybit);
  423. __set_bit(BTN_TOOL_DOUBLETAP, dev->keybit);
  424. __set_bit(BTN_TOOL_TRIPLETAP, dev->keybit);
  425. switch (etd->hw_version) {
  426. case 1:
  427. /* Rocker button */
  428. if (etd->fw_version < 0x020000 &&
  429. (etd->capabilities & ETP_CAP_HAS_ROCKER)) {
  430. __set_bit(BTN_FORWARD, dev->keybit);
  431. __set_bit(BTN_BACK, dev->keybit);
  432. }
  433. input_set_abs_params(dev, ABS_X, ETP_XMIN_V1, ETP_XMAX_V1, 0, 0);
  434. input_set_abs_params(dev, ABS_Y, ETP_YMIN_V1, ETP_YMAX_V1, 0, 0);
  435. break;
  436. case 2:
  437. __set_bit(BTN_TOOL_QUADTAP, dev->keybit);
  438. input_set_abs_params(dev, ABS_X, ETP_XMIN_V2, ETP_XMAX_V2, 0, 0);
  439. input_set_abs_params(dev, ABS_Y, ETP_YMIN_V2, ETP_YMAX_V2, 0, 0);
  440. if (etd->reports_pressure) {
  441. input_set_abs_params(dev, ABS_PRESSURE, ETP_PMIN_V2,
  442. ETP_PMAX_V2, 0, 0);
  443. input_set_abs_params(dev, ABS_TOOL_WIDTH, ETP_WMIN_V2,
  444. ETP_WMAX_V2, 0, 0);
  445. }
  446. __set_bit(INPUT_PROP_SEMI_MT, dev->propbit);
  447. input_mt_init_slots(dev, 2);
  448. input_set_abs_params(dev, ABS_MT_POSITION_X, ETP_XMIN_V2, ETP_XMAX_V2, 0, 0);
  449. input_set_abs_params(dev, ABS_MT_POSITION_Y, ETP_YMIN_V2, ETP_YMAX_V2, 0, 0);
  450. break;
  451. }
  452. }
  453. struct elantech_attr_data {
  454. size_t field_offset;
  455. unsigned char reg;
  456. };
  457. /*
  458. * Display a register value by reading a sysfs entry
  459. */
  460. static ssize_t elantech_show_int_attr(struct psmouse *psmouse, void *data,
  461. char *buf)
  462. {
  463. struct elantech_data *etd = psmouse->private;
  464. struct elantech_attr_data *attr = data;
  465. unsigned char *reg = (unsigned char *) etd + attr->field_offset;
  466. int rc = 0;
  467. if (attr->reg)
  468. rc = elantech_read_reg(psmouse, attr->reg, reg);
  469. return sprintf(buf, "0x%02x\n", (attr->reg && rc) ? -1 : *reg);
  470. }
  471. /*
  472. * Write a register value by writing a sysfs entry
  473. */
  474. static ssize_t elantech_set_int_attr(struct psmouse *psmouse,
  475. void *data, const char *buf, size_t count)
  476. {
  477. struct elantech_data *etd = psmouse->private;
  478. struct elantech_attr_data *attr = data;
  479. unsigned char *reg = (unsigned char *) etd + attr->field_offset;
  480. unsigned long value;
  481. int err;
  482. err = strict_strtoul(buf, 16, &value);
  483. if (err)
  484. return err;
  485. if (value > 0xff)
  486. return -EINVAL;
  487. /* Do we need to preserve some bits for version 2 hardware too? */
  488. if (etd->hw_version == 1) {
  489. if (attr->reg == 0x10)
  490. /* Force absolute mode always on */
  491. value |= ETP_R10_ABSOLUTE_MODE;
  492. else if (attr->reg == 0x11)
  493. /* Force 4 byte mode always on */
  494. value |= ETP_R11_4_BYTE_MODE;
  495. }
  496. if (!attr->reg || elantech_write_reg(psmouse, attr->reg, value) == 0)
  497. *reg = value;
  498. return count;
  499. }
  500. #define ELANTECH_INT_ATTR(_name, _register) \
  501. static struct elantech_attr_data elantech_attr_##_name = { \
  502. .field_offset = offsetof(struct elantech_data, _name), \
  503. .reg = _register, \
  504. }; \
  505. PSMOUSE_DEFINE_ATTR(_name, S_IWUSR | S_IRUGO, \
  506. &elantech_attr_##_name, \
  507. elantech_show_int_attr, \
  508. elantech_set_int_attr)
  509. ELANTECH_INT_ATTR(reg_10, 0x10);
  510. ELANTECH_INT_ATTR(reg_11, 0x11);
  511. ELANTECH_INT_ATTR(reg_20, 0x20);
  512. ELANTECH_INT_ATTR(reg_21, 0x21);
  513. ELANTECH_INT_ATTR(reg_22, 0x22);
  514. ELANTECH_INT_ATTR(reg_23, 0x23);
  515. ELANTECH_INT_ATTR(reg_24, 0x24);
  516. ELANTECH_INT_ATTR(reg_25, 0x25);
  517. ELANTECH_INT_ATTR(reg_26, 0x26);
  518. ELANTECH_INT_ATTR(debug, 0);
  519. ELANTECH_INT_ATTR(paritycheck, 0);
  520. static struct attribute *elantech_attrs[] = {
  521. &psmouse_attr_reg_10.dattr.attr,
  522. &psmouse_attr_reg_11.dattr.attr,
  523. &psmouse_attr_reg_20.dattr.attr,
  524. &psmouse_attr_reg_21.dattr.attr,
  525. &psmouse_attr_reg_22.dattr.attr,
  526. &psmouse_attr_reg_23.dattr.attr,
  527. &psmouse_attr_reg_24.dattr.attr,
  528. &psmouse_attr_reg_25.dattr.attr,
  529. &psmouse_attr_reg_26.dattr.attr,
  530. &psmouse_attr_debug.dattr.attr,
  531. &psmouse_attr_paritycheck.dattr.attr,
  532. NULL
  533. };
  534. static struct attribute_group elantech_attr_group = {
  535. .attrs = elantech_attrs,
  536. };
  537. static bool elantech_is_signature_valid(const unsigned char *param)
  538. {
  539. static const unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10 };
  540. int i;
  541. if (param[0] == 0)
  542. return false;
  543. if (param[1] == 0)
  544. return true;
  545. for (i = 0; i < ARRAY_SIZE(rates); i++)
  546. if (param[2] == rates[i])
  547. return false;
  548. return true;
  549. }
  550. /*
  551. * Use magic knock to detect Elantech touchpad
  552. */
  553. int elantech_detect(struct psmouse *psmouse, bool set_properties)
  554. {
  555. struct ps2dev *ps2dev = &psmouse->ps2dev;
  556. unsigned char param[3];
  557. ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
  558. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  559. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  560. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  561. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  562. ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) {
  563. pr_debug("sending Elantech magic knock failed.\n");
  564. return -1;
  565. }
  566. /*
  567. * Report this in case there are Elantech models that use a different
  568. * set of magic numbers
  569. */
  570. if (param[0] != 0x3c || param[1] != 0x03 || param[2] != 0xc8) {
  571. pr_debug("unexpected magic knock result 0x%02x, 0x%02x, 0x%02x.\n",
  572. param[0], param[1], param[2]);
  573. return -1;
  574. }
  575. /*
  576. * Query touchpad's firmware version and see if it reports known
  577. * value to avoid mis-detection. Logitech mice are known to respond
  578. * to Elantech magic knock and there might be more.
  579. */
  580. if (synaptics_send_cmd(psmouse, ETP_FW_VERSION_QUERY, param)) {
  581. pr_debug("failed to query firmware version.\n");
  582. return -1;
  583. }
  584. pr_debug("Elantech version query result 0x%02x, 0x%02x, 0x%02x.\n",
  585. param[0], param[1], param[2]);
  586. if (!elantech_is_signature_valid(param)) {
  587. if (!force_elantech) {
  588. pr_debug("Probably not a real Elantech touchpad. Aborting.\n");
  589. return -1;
  590. }
  591. pr_debug("Probably not a real Elantech touchpad. Enabling anyway due to force_elantech.\n");
  592. }
  593. if (set_properties) {
  594. psmouse->vendor = "Elantech";
  595. psmouse->name = "Touchpad";
  596. }
  597. return 0;
  598. }
  599. /*
  600. * Clean up sysfs entries when disconnecting
  601. */
  602. static void elantech_disconnect(struct psmouse *psmouse)
  603. {
  604. sysfs_remove_group(&psmouse->ps2dev.serio->dev.kobj,
  605. &elantech_attr_group);
  606. kfree(psmouse->private);
  607. psmouse->private = NULL;
  608. }
  609. /*
  610. * Put the touchpad back into absolute mode when reconnecting
  611. */
  612. static int elantech_reconnect(struct psmouse *psmouse)
  613. {
  614. if (elantech_detect(psmouse, 0))
  615. return -1;
  616. if (elantech_set_absolute_mode(psmouse)) {
  617. pr_err("failed to put touchpad back into absolute mode.\n");
  618. return -1;
  619. }
  620. return 0;
  621. }
  622. /*
  623. * Initialize the touchpad and create sysfs entries
  624. */
  625. int elantech_init(struct psmouse *psmouse)
  626. {
  627. struct elantech_data *etd;
  628. int i, error;
  629. unsigned char param[3];
  630. psmouse->private = etd = kzalloc(sizeof(struct elantech_data), GFP_KERNEL);
  631. if (!etd)
  632. return -ENOMEM;
  633. etd->parity[0] = 1;
  634. for (i = 1; i < 256; i++)
  635. etd->parity[i] = etd->parity[i & (i - 1)] ^ 1;
  636. /*
  637. * Do the version query again so we can store the result
  638. */
  639. if (synaptics_send_cmd(psmouse, ETP_FW_VERSION_QUERY, param)) {
  640. pr_err("failed to query firmware version.\n");
  641. goto init_fail;
  642. }
  643. etd->fw_version = (param[0] << 16) | (param[1] << 8) | param[2];
  644. /*
  645. * Assume every version greater than this is new EeePC style
  646. * hardware with 6 byte packets
  647. */
  648. if (etd->fw_version >= 0x020030) {
  649. etd->hw_version = 2;
  650. /* For now show extra debug information */
  651. etd->debug = 1;
  652. /* Don't know how to do parity checking for version 2 */
  653. etd->paritycheck = 0;
  654. if (etd->fw_version >= 0x020800)
  655. etd->reports_pressure = true;
  656. } else {
  657. etd->hw_version = 1;
  658. etd->paritycheck = 1;
  659. }
  660. pr_info("assuming hardware version %d, firmware version %d.%d.%d\n",
  661. etd->hw_version, param[0], param[1], param[2]);
  662. if (synaptics_send_cmd(psmouse, ETP_CAPABILITIES_QUERY, param)) {
  663. pr_err("failed to query capabilities.\n");
  664. goto init_fail;
  665. }
  666. pr_info("Synaptics capabilities query result 0x%02x, 0x%02x, 0x%02x.\n",
  667. param[0], param[1], param[2]);
  668. etd->capabilities = param[0];
  669. /*
  670. * This firmware suffers from misreporting coordinates when
  671. * a touch action starts causing the mouse cursor or scrolled page
  672. * to jump. Enable a workaround.
  673. */
  674. if (etd->fw_version == 0x020022 || etd->fw_version == 0x020600) {
  675. pr_info("firmware version 2.0.34/2.6.0 detected, enabling jumpy cursor workaround\n");
  676. etd->jumpy_cursor = true;
  677. }
  678. if (elantech_set_absolute_mode(psmouse)) {
  679. pr_err("failed to put touchpad into absolute mode.\n");
  680. goto init_fail;
  681. }
  682. elantech_set_input_params(psmouse);
  683. error = sysfs_create_group(&psmouse->ps2dev.serio->dev.kobj,
  684. &elantech_attr_group);
  685. if (error) {
  686. pr_err("failed to create sysfs attributes, error: %d.\n", error);
  687. goto init_fail;
  688. }
  689. psmouse->protocol_handler = elantech_process_byte;
  690. psmouse->disconnect = elantech_disconnect;
  691. psmouse->reconnect = elantech_reconnect;
  692. psmouse->pktsize = etd->hw_version == 2 ? 6 : 4;
  693. return 0;
  694. init_fail:
  695. kfree(etd);
  696. return -1;
  697. }