elantech.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  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/module.h>
  14. #include <linux/input.h>
  15. #include <linux/serio.h>
  16. #include <linux/libps2.h>
  17. #include "psmouse.h"
  18. #include "elantech.h"
  19. #define elantech_debug(format, arg...) \
  20. do { \
  21. if (etd->debug) \
  22. printk(KERN_DEBUG format, ##arg); \
  23. } while (0)
  24. static bool force_elantech;
  25. module_param_named(force_elantech, force_elantech, bool, 0644);
  26. MODULE_PARM_DESC(force_elantech, "Force the Elantech PS/2 protocol extension to be used, 1 = enabled, 0 = disabled (default).");
  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. pr_err("elantech.c: synaptics_send_cmd query 0x%02x failed.\n", c);
  36. return -1;
  37. }
  38. return 0;
  39. }
  40. /*
  41. * A retrying version of ps2_command
  42. */
  43. static int elantech_ps2_command(struct psmouse *psmouse,
  44. unsigned char *param, int command)
  45. {
  46. struct ps2dev *ps2dev = &psmouse->ps2dev;
  47. struct elantech_data *etd = psmouse->private;
  48. int rc;
  49. int tries = ETP_PS2_COMMAND_TRIES;
  50. do {
  51. rc = ps2_command(ps2dev, param, command);
  52. if (rc == 0)
  53. break;
  54. tries--;
  55. elantech_debug("elantech.c: retrying ps2 command 0x%02x (%d).\n",
  56. command, tries);
  57. msleep(ETP_PS2_COMMAND_DELAY);
  58. } while (tries > 0);
  59. if (rc)
  60. pr_err("elantech.c: ps2 command 0x%02x failed.\n", command);
  61. return rc;
  62. }
  63. /*
  64. * Send an Elantech style special command to read a value from a register
  65. */
  66. static int elantech_read_reg(struct psmouse *psmouse, unsigned char reg,
  67. unsigned char *val)
  68. {
  69. struct elantech_data *etd = psmouse->private;
  70. unsigned char param[3];
  71. int rc = 0;
  72. if (reg < 0x10 || reg > 0x26)
  73. return -1;
  74. if (reg > 0x11 && reg < 0x20)
  75. return -1;
  76. switch (etd->hw_version) {
  77. case 1:
  78. if (psmouse_sliced_command(psmouse, ETP_REGISTER_READ) ||
  79. psmouse_sliced_command(psmouse, reg) ||
  80. ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_GETINFO)) {
  81. rc = -1;
  82. }
  83. break;
  84. case 2:
  85. if (elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
  86. elantech_ps2_command(psmouse, NULL, ETP_REGISTER_READ) ||
  87. elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
  88. elantech_ps2_command(psmouse, NULL, reg) ||
  89. elantech_ps2_command(psmouse, param, PSMOUSE_CMD_GETINFO)) {
  90. rc = -1;
  91. }
  92. break;
  93. }
  94. if (rc)
  95. pr_err("elantech.c: failed to read register 0x%02x.\n", reg);
  96. else
  97. *val = param[0];
  98. return rc;
  99. }
  100. /*
  101. * Send an Elantech style special command to write a register with a value
  102. */
  103. static int elantech_write_reg(struct psmouse *psmouse, unsigned char reg,
  104. unsigned char val)
  105. {
  106. struct elantech_data *etd = psmouse->private;
  107. int rc = 0;
  108. if (reg < 0x10 || reg > 0x26)
  109. return -1;
  110. if (reg > 0x11 && reg < 0x20)
  111. return -1;
  112. switch (etd->hw_version) {
  113. case 1:
  114. if (psmouse_sliced_command(psmouse, ETP_REGISTER_WRITE) ||
  115. psmouse_sliced_command(psmouse, reg) ||
  116. psmouse_sliced_command(psmouse, val) ||
  117. ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11)) {
  118. rc = -1;
  119. }
  120. break;
  121. case 2:
  122. if (elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
  123. elantech_ps2_command(psmouse, NULL, ETP_REGISTER_WRITE) ||
  124. elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
  125. elantech_ps2_command(psmouse, NULL, reg) ||
  126. elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
  127. elantech_ps2_command(psmouse, NULL, val) ||
  128. elantech_ps2_command(psmouse, NULL, PSMOUSE_CMD_SETSCALE11)) {
  129. rc = -1;
  130. }
  131. break;
  132. }
  133. if (rc)
  134. pr_err("elantech.c: failed to write register 0x%02x with value 0x%02x.\n",
  135. reg, val);
  136. return rc;
  137. }
  138. /*
  139. * Dump a complete mouse movement packet to the syslog
  140. */
  141. static void elantech_packet_dump(unsigned char *packet, int size)
  142. {
  143. int i;
  144. printk(KERN_DEBUG "elantech.c: PS/2 packet [");
  145. for (i = 0; i < size; i++)
  146. printk("%s0x%02x ", (i) ? ", " : " ", packet[i]);
  147. printk("]\n");
  148. }
  149. /*
  150. * Interpret complete data packets and report absolute mode input events for
  151. * hardware version 1. (4 byte packets)
  152. */
  153. static void elantech_report_absolute_v1(struct psmouse *psmouse)
  154. {
  155. struct input_dev *dev = psmouse->dev;
  156. struct elantech_data *etd = psmouse->private;
  157. unsigned char *packet = psmouse->packet;
  158. int fingers;
  159. static int old_fingers;
  160. if (etd->fw_version_maj == 0x01) {
  161. /*
  162. * byte 0: D U p1 p2 1 p3 R L
  163. * byte 1: f 0 th tw x9 x8 y9 y8
  164. */
  165. fingers = ((packet[1] & 0x80) >> 7) +
  166. ((packet[1] & 0x30) >> 4);
  167. } else {
  168. /*
  169. * byte 0: n1 n0 p2 p1 1 p3 R L
  170. * byte 1: 0 0 0 0 x9 x8 y9 y8
  171. */
  172. fingers = (packet[0] & 0xc0) >> 6;
  173. }
  174. if (etd->jumpy_cursor) {
  175. /* Discard packets that are likely to have bogus coordinates */
  176. if (fingers > old_fingers) {
  177. elantech_debug("elantech.c: discarding packet\n");
  178. goto discard_packet_v1;
  179. }
  180. }
  181. input_report_key(dev, BTN_TOUCH, fingers != 0);
  182. /*
  183. * byte 2: x7 x6 x5 x4 x3 x2 x1 x0
  184. * byte 3: y7 y6 y5 y4 y3 y2 y1 y0
  185. */
  186. if (fingers) {
  187. input_report_abs(dev, ABS_X,
  188. ((packet[1] & 0x0c) << 6) | packet[2]);
  189. input_report_abs(dev, ABS_Y,
  190. ETP_YMAX_V1 - (((packet[1] & 0x03) << 8) | packet[3]));
  191. }
  192. input_report_key(dev, BTN_TOOL_FINGER, fingers == 1);
  193. input_report_key(dev, BTN_TOOL_DOUBLETAP, fingers == 2);
  194. input_report_key(dev, BTN_TOOL_TRIPLETAP, fingers == 3);
  195. input_report_key(dev, BTN_LEFT, packet[0] & 0x01);
  196. input_report_key(dev, BTN_RIGHT, packet[0] & 0x02);
  197. if ((etd->fw_version_maj == 0x01) &&
  198. (etd->capabilities & ETP_CAP_HAS_ROCKER)) {
  199. /* rocker up */
  200. input_report_key(dev, BTN_FORWARD, packet[0] & 0x40);
  201. /* rocker down */
  202. input_report_key(dev, BTN_BACK, packet[0] & 0x80);
  203. }
  204. input_sync(dev);
  205. discard_packet_v1:
  206. old_fingers = fingers;
  207. }
  208. /*
  209. * Interpret complete data packets and report absolute mode input events for
  210. * hardware version 2. (6 byte packets)
  211. */
  212. static void elantech_report_absolute_v2(struct psmouse *psmouse)
  213. {
  214. struct input_dev *dev = psmouse->dev;
  215. unsigned char *packet = psmouse->packet;
  216. int fingers, x1, y1, x2, y2;
  217. /* byte 0: n1 n0 . . . . R L */
  218. fingers = (packet[0] & 0xc0) >> 6;
  219. input_report_key(dev, BTN_TOUCH, fingers != 0);
  220. switch (fingers) {
  221. case 1:
  222. /*
  223. * byte 1: . . . . . x10 x9 x8
  224. * byte 2: x7 x6 x5 x4 x4 x2 x1 x0
  225. */
  226. input_report_abs(dev, ABS_X,
  227. ((packet[1] & 0x07) << 8) | packet[2]);
  228. /*
  229. * byte 4: . . . . . . y9 y8
  230. * byte 5: y7 y6 y5 y4 y3 y2 y1 y0
  231. */
  232. input_report_abs(dev, ABS_Y,
  233. ETP_YMAX_V2 - (((packet[4] & 0x03) << 8) | packet[5]));
  234. break;
  235. case 2:
  236. /*
  237. * The coordinate of each finger is reported separately
  238. * with a lower resolution for two finger touches:
  239. * byte 0: . . ay8 ax8 . . . .
  240. * byte 1: ax7 ax6 ax5 ax4 ax3 ax2 ax1 ax0
  241. */
  242. x1 = ((packet[0] & 0x10) << 4) | packet[1];
  243. /* byte 2: ay7 ay6 ay5 ay4 ay3 ay2 ay1 ay0 */
  244. y1 = ETP_2FT_YMAX - (((packet[0] & 0x20) << 3) | packet[2]);
  245. /*
  246. * byte 3: . . by8 bx8 . . . .
  247. * byte 4: bx7 bx6 bx5 bx4 bx3 bx2 bx1 bx0
  248. */
  249. x2 = ((packet[3] & 0x10) << 4) | packet[4];
  250. /* byte 5: by7 by8 by5 by4 by3 by2 by1 by0 */
  251. y2 = ETP_2FT_YMAX - (((packet[3] & 0x20) << 3) | packet[5]);
  252. /*
  253. * For compatibility with the X Synaptics driver scale up
  254. * one coordinate and report as ordinary mouse movent
  255. */
  256. input_report_abs(dev, ABS_X, x1 << 2);
  257. input_report_abs(dev, ABS_Y, y1 << 2);
  258. /*
  259. * For compatibility with the proprietary X Elantech driver
  260. * report both coordinates as hat coordinates
  261. */
  262. input_report_abs(dev, ABS_HAT0X, x1);
  263. input_report_abs(dev, ABS_HAT0Y, y1);
  264. input_report_abs(dev, ABS_HAT1X, x2);
  265. input_report_abs(dev, ABS_HAT1Y, y2);
  266. break;
  267. }
  268. input_report_key(dev, BTN_TOOL_FINGER, fingers == 1);
  269. input_report_key(dev, BTN_TOOL_DOUBLETAP, fingers == 2);
  270. input_report_key(dev, BTN_TOOL_TRIPLETAP, fingers == 3);
  271. input_report_key(dev, BTN_LEFT, packet[0] & 0x01);
  272. input_report_key(dev, BTN_RIGHT, packet[0] & 0x02);
  273. input_sync(dev);
  274. }
  275. static int elantech_check_parity_v1(struct psmouse *psmouse)
  276. {
  277. struct elantech_data *etd = psmouse->private;
  278. unsigned char *packet = psmouse->packet;
  279. unsigned char p1, p2, p3;
  280. /* Parity bits are placed differently */
  281. if (etd->fw_version_maj == 0x01) {
  282. /* byte 0: D U p1 p2 1 p3 R L */
  283. p1 = (packet[0] & 0x20) >> 5;
  284. p2 = (packet[0] & 0x10) >> 4;
  285. } else {
  286. /* byte 0: n1 n0 p2 p1 1 p3 R L */
  287. p1 = (packet[0] & 0x10) >> 4;
  288. p2 = (packet[0] & 0x20) >> 5;
  289. }
  290. p3 = (packet[0] & 0x04) >> 2;
  291. return etd->parity[packet[1]] == p1 &&
  292. etd->parity[packet[2]] == p2 &&
  293. etd->parity[packet[3]] == p3;
  294. }
  295. /*
  296. * Process byte stream from mouse and handle complete packets
  297. */
  298. static psmouse_ret_t elantech_process_byte(struct psmouse *psmouse)
  299. {
  300. struct elantech_data *etd = psmouse->private;
  301. if (psmouse->pktcnt < psmouse->pktsize)
  302. return PSMOUSE_GOOD_DATA;
  303. if (etd->debug > 1)
  304. elantech_packet_dump(psmouse->packet, psmouse->pktsize);
  305. switch (etd->hw_version) {
  306. case 1:
  307. if (etd->paritycheck && !elantech_check_parity_v1(psmouse))
  308. return PSMOUSE_BAD_DATA;
  309. elantech_report_absolute_v1(psmouse);
  310. break;
  311. case 2:
  312. /* We don't know how to check parity in protocol v2 */
  313. elantech_report_absolute_v2(psmouse);
  314. break;
  315. }
  316. return PSMOUSE_FULL_PACKET;
  317. }
  318. /*
  319. * Put the touchpad into absolute mode
  320. */
  321. static int elantech_set_absolute_mode(struct psmouse *psmouse)
  322. {
  323. struct elantech_data *etd = psmouse->private;
  324. unsigned char val;
  325. int tries = ETP_READ_BACK_TRIES;
  326. int rc = 0;
  327. switch (etd->hw_version) {
  328. case 1:
  329. etd->reg_10 = 0x16;
  330. etd->reg_11 = 0x8f;
  331. if (elantech_write_reg(psmouse, 0x10, etd->reg_10) ||
  332. elantech_write_reg(psmouse, 0x11, etd->reg_11)) {
  333. rc = -1;
  334. }
  335. break;
  336. case 2:
  337. /* Windows driver values */
  338. etd->reg_10 = 0x54;
  339. etd->reg_11 = 0x88; /* 0x8a */
  340. etd->reg_21 = 0x60; /* 0x00 */
  341. if (elantech_write_reg(psmouse, 0x10, etd->reg_10) ||
  342. elantech_write_reg(psmouse, 0x11, etd->reg_11) ||
  343. elantech_write_reg(psmouse, 0x21, etd->reg_21)) {
  344. rc = -1;
  345. break;
  346. }
  347. }
  348. if (rc == 0) {
  349. /*
  350. * Read back reg 0x10. For hardware version 1 we must make
  351. * sure the absolute mode bit is set. For hardware version 2
  352. * the touchpad is probably initalising and not ready until
  353. * we read back the value we just wrote.
  354. */
  355. do {
  356. rc = elantech_read_reg(psmouse, 0x10, &val);
  357. if (rc == 0)
  358. break;
  359. tries--;
  360. elantech_debug("elantech.c: retrying read (%d).\n",
  361. tries);
  362. msleep(ETP_READ_BACK_DELAY);
  363. } while (tries > 0);
  364. if (rc) {
  365. pr_err("elantech.c: failed to read back register 0x10.\n");
  366. } else if (etd->hw_version == 1 &&
  367. !(val & ETP_R10_ABSOLUTE_MODE)) {
  368. pr_err("elantech.c: touchpad refuses "
  369. "to switch to absolute mode.\n");
  370. rc = -1;
  371. }
  372. }
  373. if (rc)
  374. pr_err("elantech.c: 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_maj == 0x01) &&
  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. /*
  499. * Use magic knock to detect Elantech touchpad
  500. */
  501. int elantech_detect(struct psmouse *psmouse, bool set_properties)
  502. {
  503. struct ps2dev *ps2dev = &psmouse->ps2dev;
  504. unsigned char param[3];
  505. ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
  506. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  507. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  508. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  509. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  510. ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) {
  511. pr_debug("elantech.c: sending Elantech magic knock failed.\n");
  512. return -1;
  513. }
  514. /*
  515. * Report this in case there are Elantech models that use a different
  516. * set of magic numbers
  517. */
  518. if (param[0] != 0x3c || param[1] != 0x03 || param[2] != 0xc8) {
  519. pr_debug("elantech.c: "
  520. "unexpected magic knock result 0x%02x, 0x%02x, 0x%02x.\n",
  521. param[0], param[1], param[2]);
  522. return -1;
  523. }
  524. /*
  525. * Query touchpad's firmware version and see if it reports known
  526. * value to avoid mis-detection. Logitech mice are known to respond
  527. * to Elantech magic knock and there might be more.
  528. */
  529. if (synaptics_send_cmd(psmouse, ETP_FW_VERSION_QUERY, param)) {
  530. pr_debug("elantech.c: failed to query firmware version.\n");
  531. return -1;
  532. }
  533. pr_debug("elantech.c: Elantech version query result 0x%02x, 0x%02x, 0x%02x.\n",
  534. param[0], param[1], param[2]);
  535. if (param[0] == 0 || param[1] != 0) {
  536. if (!force_elantech) {
  537. pr_debug("elantech.c: Probably not a real Elantech touchpad. Aborting.\n");
  538. return -1;
  539. }
  540. pr_debug("elantech.c: Probably not a real Elantech touchpad. Enabling anyway due to force_elantech.\n");
  541. }
  542. if (set_properties) {
  543. psmouse->vendor = "Elantech";
  544. psmouse->name = "Touchpad";
  545. }
  546. return 0;
  547. }
  548. /*
  549. * Clean up sysfs entries when disconnecting
  550. */
  551. static void elantech_disconnect(struct psmouse *psmouse)
  552. {
  553. sysfs_remove_group(&psmouse->ps2dev.serio->dev.kobj,
  554. &elantech_attr_group);
  555. kfree(psmouse->private);
  556. psmouse->private = NULL;
  557. }
  558. /*
  559. * Put the touchpad back into absolute mode when reconnecting
  560. */
  561. static int elantech_reconnect(struct psmouse *psmouse)
  562. {
  563. if (elantech_detect(psmouse, 0))
  564. return -1;
  565. if (elantech_set_absolute_mode(psmouse)) {
  566. pr_err("elantech.c: failed to put touchpad back into absolute mode.\n");
  567. return -1;
  568. }
  569. return 0;
  570. }
  571. /*
  572. * Initialize the touchpad and create sysfs entries
  573. */
  574. int elantech_init(struct psmouse *psmouse)
  575. {
  576. struct elantech_data *etd;
  577. int i, error;
  578. unsigned char param[3];
  579. psmouse->private = etd = kzalloc(sizeof(struct elantech_data), GFP_KERNEL);
  580. if (!etd)
  581. return -1;
  582. etd->parity[0] = 1;
  583. for (i = 1; i < 256; i++)
  584. etd->parity[i] = etd->parity[i & (i - 1)] ^ 1;
  585. /*
  586. * Do the version query again so we can store the result
  587. */
  588. if (synaptics_send_cmd(psmouse, ETP_FW_VERSION_QUERY, param)) {
  589. pr_err("elantech.c: failed to query firmware version.\n");
  590. goto init_fail;
  591. }
  592. etd->fw_version_maj = param[0];
  593. etd->fw_version_min = param[2];
  594. /*
  595. * Assume every version greater than this is new EeePC style
  596. * hardware with 6 byte packets
  597. */
  598. if ((etd->fw_version_maj == 0x02 && etd->fw_version_min >= 0x30) ||
  599. etd->fw_version_maj > 0x02) {
  600. etd->hw_version = 2;
  601. /* For now show extra debug information */
  602. etd->debug = 1;
  603. /* Don't know how to do parity checking for version 2 */
  604. etd->paritycheck = 0;
  605. } else {
  606. etd->hw_version = 1;
  607. etd->paritycheck = 1;
  608. }
  609. pr_info("elantech.c: assuming hardware version %d, firmware version %d.%d\n",
  610. etd->hw_version, etd->fw_version_maj, etd->fw_version_min);
  611. if (synaptics_send_cmd(psmouse, ETP_CAPABILITIES_QUERY, param)) {
  612. pr_err("elantech.c: failed to query capabilities.\n");
  613. goto init_fail;
  614. }
  615. pr_info("elantech.c: Synaptics capabilities query result 0x%02x, 0x%02x, 0x%02x.\n",
  616. param[0], param[1], param[2]);
  617. etd->capabilities = param[0];
  618. /*
  619. * This firmware seems to suffer from misreporting coordinates when
  620. * a touch action starts causing the mouse cursor or scrolled page
  621. * to jump. Enable a workaround.
  622. */
  623. if (etd->fw_version_maj == 0x02 && etd->fw_version_min == 0x22) {
  624. pr_info("elantech.c: firmware version 2.34 detected, "
  625. "enabling jumpy cursor workaround\n");
  626. etd->jumpy_cursor = 1;
  627. }
  628. if (elantech_set_absolute_mode(psmouse)) {
  629. pr_err("elantech.c: failed to put touchpad into absolute mode.\n");
  630. goto init_fail;
  631. }
  632. elantech_set_input_params(psmouse);
  633. error = sysfs_create_group(&psmouse->ps2dev.serio->dev.kobj,
  634. &elantech_attr_group);
  635. if (error) {
  636. pr_err("elantech.c: failed to create sysfs attributes, error: %d.\n",
  637. error);
  638. goto init_fail;
  639. }
  640. psmouse->protocol_handler = elantech_process_byte;
  641. psmouse->disconnect = elantech_disconnect;
  642. psmouse->reconnect = elantech_reconnect;
  643. psmouse->pktsize = etd->hw_version == 2 ? 6 : 4;
  644. return 0;
  645. init_fail:
  646. kfree(etd);
  647. return -1;
  648. }