elantech.c 19 KB

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