elantech.c 20 KB

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