elantech.c 20 KB

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