elantech.c 22 KB

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