elantech.c 20 KB

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