elantech.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  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. etd->y_max - (((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[0] & 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;
  240. unsigned int width = 0, pres = 0;
  241. /* byte 0: n1 n0 . . . . R L */
  242. fingers = (packet[0] & 0xc0) >> 6;
  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: . . . . x11 x10 x9 x8
  255. * byte 2: x7 x6 x5 x4 x4 x2 x1 x0
  256. */
  257. x1 = ((packet[1] & 0x0f) << 8) | packet[2];
  258. /*
  259. * byte 4: . . . . y11 y10 y9 y8
  260. * byte 5: y7 y6 y5 y4 y3 y2 y1 y0
  261. */
  262. y1 = etd->y_max - (((packet[4] & 0x0f) << 8) | packet[5]);
  263. pres = (packet[1] & 0xf0) | ((packet[4] & 0xf0) >> 4);
  264. width = ((packet[0] & 0x30) >> 2) | ((packet[3] & 0x30) >> 4);
  265. break;
  266. case 2:
  267. /*
  268. * The coordinate of each finger is reported separately
  269. * with a lower resolution for two finger touches:
  270. * byte 0: . . ay8 ax8 . . . .
  271. * byte 1: ax7 ax6 ax5 ax4 ax3 ax2 ax1 ax0
  272. */
  273. x1 = (((packet[0] & 0x10) << 4) | packet[1]) << 2;
  274. /* byte 2: ay7 ay6 ay5 ay4 ay3 ay2 ay1 ay0 */
  275. y1 = etd->y_max -
  276. ((((packet[0] & 0x20) << 3) | packet[2]) << 2);
  277. /*
  278. * byte 3: . . by8 bx8 . . . .
  279. * byte 4: bx7 bx6 bx5 bx4 bx3 bx2 bx1 bx0
  280. */
  281. x2 = (((packet[3] & 0x10) << 4) | packet[4]) << 2;
  282. /* byte 5: by7 by8 by5 by4 by3 by2 by1 by0 */
  283. y2 = etd->y_max -
  284. ((((packet[3] & 0x20) << 3) | packet[5]) << 2);
  285. /* Unknown so just report sensible values */
  286. pres = 127;
  287. width = 7;
  288. break;
  289. }
  290. input_report_key(dev, BTN_TOUCH, fingers != 0);
  291. if (fingers != 0) {
  292. input_report_abs(dev, ABS_X, x1);
  293. input_report_abs(dev, ABS_Y, y1);
  294. }
  295. elantech_report_semi_mt_data(dev, fingers, x1, y1, x2, y2);
  296. input_report_key(dev, BTN_TOOL_FINGER, fingers == 1);
  297. input_report_key(dev, BTN_TOOL_DOUBLETAP, fingers == 2);
  298. input_report_key(dev, BTN_TOOL_TRIPLETAP, fingers == 3);
  299. input_report_key(dev, BTN_TOOL_QUADTAP, fingers == 4);
  300. input_report_key(dev, BTN_LEFT, packet[0] & 0x01);
  301. input_report_key(dev, BTN_RIGHT, packet[0] & 0x02);
  302. if (etd->reports_pressure) {
  303. input_report_abs(dev, ABS_PRESSURE, pres);
  304. input_report_abs(dev, ABS_TOOL_WIDTH, width);
  305. }
  306. input_sync(dev);
  307. }
  308. static int elantech_packet_check_v1(struct psmouse *psmouse)
  309. {
  310. struct elantech_data *etd = psmouse->private;
  311. unsigned char *packet = psmouse->packet;
  312. unsigned char p1, p2, p3;
  313. /* Parity bits are placed differently */
  314. if (etd->fw_version < 0x020000) {
  315. /* byte 0: D U p1 p2 1 p3 R L */
  316. p1 = (packet[0] & 0x20) >> 5;
  317. p2 = (packet[0] & 0x10) >> 4;
  318. } else {
  319. /* byte 0: n1 n0 p2 p1 1 p3 R L */
  320. p1 = (packet[0] & 0x10) >> 4;
  321. p2 = (packet[0] & 0x20) >> 5;
  322. }
  323. p3 = (packet[0] & 0x04) >> 2;
  324. return etd->parity[packet[1]] == p1 &&
  325. etd->parity[packet[2]] == p2 &&
  326. etd->parity[packet[3]] == p3;
  327. }
  328. static int elantech_packet_check_v2(struct psmouse *psmouse)
  329. {
  330. struct elantech_data *etd = psmouse->private;
  331. unsigned char *packet = psmouse->packet;
  332. /*
  333. * V2 hardware has two flavors. Older ones that do not report pressure,
  334. * and newer ones that reports pressure and width. With newer ones, all
  335. * packets (1, 2, 3 finger touch) have the same constant bits. With
  336. * older ones, 1/3 finger touch packets and 2 finger touch packets
  337. * have different constant bits.
  338. * With all three cases, if the constant bits are not exactly what I
  339. * expected, I consider them invalid.
  340. */
  341. if (etd->reports_pressure)
  342. return (packet[0] & 0x0c) == 0x04 &&
  343. (packet[3] & 0x0f) == 0x02;
  344. if ((packet[0] & 0xc0) == 0x80)
  345. return (packet[0] & 0x0c) == 0x0c &&
  346. (packet[3] & 0x0e) == 0x08;
  347. return (packet[0] & 0x3c) == 0x3c &&
  348. (packet[1] & 0xf0) == 0x00 &&
  349. (packet[3] & 0x3e) == 0x38 &&
  350. (packet[4] & 0xf0) == 0x00;
  351. }
  352. /*
  353. * Process byte stream from mouse and handle complete packets
  354. */
  355. static psmouse_ret_t elantech_process_byte(struct psmouse *psmouse)
  356. {
  357. struct elantech_data *etd = psmouse->private;
  358. if (psmouse->pktcnt < psmouse->pktsize)
  359. return PSMOUSE_GOOD_DATA;
  360. if (etd->debug > 1)
  361. elantech_packet_dump(psmouse->packet, psmouse->pktsize);
  362. switch (etd->hw_version) {
  363. case 1:
  364. if (etd->paritycheck && !elantech_packet_check_v1(psmouse))
  365. return PSMOUSE_BAD_DATA;
  366. elantech_report_absolute_v1(psmouse);
  367. break;
  368. case 2:
  369. if (etd->paritycheck && !elantech_packet_check_v2(psmouse))
  370. return PSMOUSE_BAD_DATA;
  371. elantech_report_absolute_v2(psmouse);
  372. break;
  373. }
  374. return PSMOUSE_FULL_PACKET;
  375. }
  376. /*
  377. * Put the touchpad into absolute mode
  378. */
  379. static int elantech_set_absolute_mode(struct psmouse *psmouse)
  380. {
  381. struct elantech_data *etd = psmouse->private;
  382. unsigned char val;
  383. int tries = ETP_READ_BACK_TRIES;
  384. int rc = 0;
  385. switch (etd->hw_version) {
  386. case 1:
  387. etd->reg_10 = 0x16;
  388. etd->reg_11 = 0x8f;
  389. if (elantech_write_reg(psmouse, 0x10, etd->reg_10) ||
  390. elantech_write_reg(psmouse, 0x11, etd->reg_11)) {
  391. rc = -1;
  392. }
  393. break;
  394. case 2:
  395. /* Windows driver values */
  396. etd->reg_10 = 0x54;
  397. etd->reg_11 = 0x88; /* 0x8a */
  398. etd->reg_21 = 0x60; /* 0x00 */
  399. if (elantech_write_reg(psmouse, 0x10, etd->reg_10) ||
  400. elantech_write_reg(psmouse, 0x11, etd->reg_11) ||
  401. elantech_write_reg(psmouse, 0x21, etd->reg_21)) {
  402. rc = -1;
  403. break;
  404. }
  405. }
  406. if (rc == 0) {
  407. /*
  408. * Read back reg 0x10. For hardware version 1 we must make
  409. * sure the absolute mode bit is set. For hardware version 2
  410. * the touchpad is probably initalising and not ready until
  411. * we read back the value we just wrote.
  412. */
  413. do {
  414. rc = elantech_read_reg(psmouse, 0x10, &val);
  415. if (rc == 0)
  416. break;
  417. tries--;
  418. elantech_debug("retrying read (%d).\n", tries);
  419. msleep(ETP_READ_BACK_DELAY);
  420. } while (tries > 0);
  421. if (rc) {
  422. pr_err("failed to read back register 0x10.\n");
  423. } else if (etd->hw_version == 1 &&
  424. !(val & ETP_R10_ABSOLUTE_MODE)) {
  425. pr_err("touchpad refuses to switch to absolute mode.\n");
  426. rc = -1;
  427. }
  428. }
  429. if (rc)
  430. pr_err("failed to initialise registers.\n");
  431. return rc;
  432. }
  433. static void elantech_set_range(struct psmouse *psmouse,
  434. unsigned int *x_min, unsigned int *y_min,
  435. unsigned int *x_max, unsigned int *y_max)
  436. {
  437. struct elantech_data *etd = psmouse->private;
  438. int i;
  439. switch (etd->hw_version) {
  440. case 1:
  441. *x_min = ETP_XMIN_V1;
  442. *y_min = ETP_YMIN_V1;
  443. *x_max = ETP_XMAX_V1;
  444. *y_max = ETP_YMAX_V1;
  445. break;
  446. case 2:
  447. if (etd->fw_version == 0x020800 ||
  448. etd->fw_version == 0x020b00 ||
  449. etd->fw_version == 0x020030) {
  450. *x_min = ETP_XMIN_V2;
  451. *y_min = ETP_YMIN_V2;
  452. *x_max = ETP_XMAX_V2;
  453. *y_max = ETP_YMAX_V2;
  454. } else {
  455. i = (etd->fw_version > 0x020800 &&
  456. etd->fw_version < 0x020900) ? 1 : 2;
  457. *x_min = 0;
  458. *y_min = 0;
  459. *x_max = (etd->capabilities[1] - i) * 64;
  460. *y_max = (etd->capabilities[2] - i) * 64;
  461. }
  462. break;
  463. }
  464. }
  465. /*
  466. * Set the appropriate event bits for the input subsystem
  467. */
  468. static void elantech_set_input_params(struct psmouse *psmouse)
  469. {
  470. struct input_dev *dev = psmouse->dev;
  471. struct elantech_data *etd = psmouse->private;
  472. unsigned int x_min = 0, y_min = 0, x_max = 0, y_max = 0;
  473. elantech_set_range(psmouse, &x_min, &y_min, &x_max, &y_max);
  474. __set_bit(EV_KEY, dev->evbit);
  475. __set_bit(EV_ABS, dev->evbit);
  476. __clear_bit(EV_REL, dev->evbit);
  477. __set_bit(BTN_LEFT, dev->keybit);
  478. __set_bit(BTN_RIGHT, dev->keybit);
  479. __set_bit(BTN_TOUCH, dev->keybit);
  480. __set_bit(BTN_TOOL_FINGER, dev->keybit);
  481. __set_bit(BTN_TOOL_DOUBLETAP, dev->keybit);
  482. __set_bit(BTN_TOOL_TRIPLETAP, dev->keybit);
  483. switch (etd->hw_version) {
  484. case 1:
  485. /* Rocker button */
  486. if (etd->fw_version < 0x020000 &&
  487. (etd->capabilities[0] & ETP_CAP_HAS_ROCKER)) {
  488. __set_bit(BTN_FORWARD, dev->keybit);
  489. __set_bit(BTN_BACK, dev->keybit);
  490. }
  491. input_set_abs_params(dev, ABS_X, x_min, x_max, 0, 0);
  492. input_set_abs_params(dev, ABS_Y, y_min, y_max, 0, 0);
  493. break;
  494. case 2:
  495. __set_bit(BTN_TOOL_QUADTAP, dev->keybit);
  496. input_set_abs_params(dev, ABS_X, x_min, x_max, 0, 0);
  497. input_set_abs_params(dev, ABS_Y, y_min, y_max, 0, 0);
  498. if (etd->reports_pressure) {
  499. input_set_abs_params(dev, ABS_PRESSURE, ETP_PMIN_V2,
  500. ETP_PMAX_V2, 0, 0);
  501. input_set_abs_params(dev, ABS_TOOL_WIDTH, ETP_WMIN_V2,
  502. ETP_WMAX_V2, 0, 0);
  503. }
  504. __set_bit(INPUT_PROP_SEMI_MT, dev->propbit);
  505. input_mt_init_slots(dev, 2);
  506. input_set_abs_params(dev, ABS_MT_POSITION_X, x_min, x_max, 0, 0);
  507. input_set_abs_params(dev, ABS_MT_POSITION_Y, y_min, y_max, 0, 0);
  508. break;
  509. }
  510. etd->y_max = y_max;
  511. }
  512. struct elantech_attr_data {
  513. size_t field_offset;
  514. unsigned char reg;
  515. };
  516. /*
  517. * Display a register value by reading a sysfs entry
  518. */
  519. static ssize_t elantech_show_int_attr(struct psmouse *psmouse, void *data,
  520. char *buf)
  521. {
  522. struct elantech_data *etd = psmouse->private;
  523. struct elantech_attr_data *attr = data;
  524. unsigned char *reg = (unsigned char *) etd + attr->field_offset;
  525. int rc = 0;
  526. if (attr->reg)
  527. rc = elantech_read_reg(psmouse, attr->reg, reg);
  528. return sprintf(buf, "0x%02x\n", (attr->reg && rc) ? -1 : *reg);
  529. }
  530. /*
  531. * Write a register value by writing a sysfs entry
  532. */
  533. static ssize_t elantech_set_int_attr(struct psmouse *psmouse,
  534. void *data, const char *buf, size_t count)
  535. {
  536. struct elantech_data *etd = psmouse->private;
  537. struct elantech_attr_data *attr = data;
  538. unsigned char *reg = (unsigned char *) etd + attr->field_offset;
  539. unsigned long value;
  540. int err;
  541. err = strict_strtoul(buf, 16, &value);
  542. if (err)
  543. return err;
  544. if (value > 0xff)
  545. return -EINVAL;
  546. /* Do we need to preserve some bits for version 2 hardware too? */
  547. if (etd->hw_version == 1) {
  548. if (attr->reg == 0x10)
  549. /* Force absolute mode always on */
  550. value |= ETP_R10_ABSOLUTE_MODE;
  551. else if (attr->reg == 0x11)
  552. /* Force 4 byte mode always on */
  553. value |= ETP_R11_4_BYTE_MODE;
  554. }
  555. if (!attr->reg || elantech_write_reg(psmouse, attr->reg, value) == 0)
  556. *reg = value;
  557. return count;
  558. }
  559. #define ELANTECH_INT_ATTR(_name, _register) \
  560. static struct elantech_attr_data elantech_attr_##_name = { \
  561. .field_offset = offsetof(struct elantech_data, _name), \
  562. .reg = _register, \
  563. }; \
  564. PSMOUSE_DEFINE_ATTR(_name, S_IWUSR | S_IRUGO, \
  565. &elantech_attr_##_name, \
  566. elantech_show_int_attr, \
  567. elantech_set_int_attr)
  568. ELANTECH_INT_ATTR(reg_10, 0x10);
  569. ELANTECH_INT_ATTR(reg_11, 0x11);
  570. ELANTECH_INT_ATTR(reg_20, 0x20);
  571. ELANTECH_INT_ATTR(reg_21, 0x21);
  572. ELANTECH_INT_ATTR(reg_22, 0x22);
  573. ELANTECH_INT_ATTR(reg_23, 0x23);
  574. ELANTECH_INT_ATTR(reg_24, 0x24);
  575. ELANTECH_INT_ATTR(reg_25, 0x25);
  576. ELANTECH_INT_ATTR(reg_26, 0x26);
  577. ELANTECH_INT_ATTR(debug, 0);
  578. ELANTECH_INT_ATTR(paritycheck, 0);
  579. static struct attribute *elantech_attrs[] = {
  580. &psmouse_attr_reg_10.dattr.attr,
  581. &psmouse_attr_reg_11.dattr.attr,
  582. &psmouse_attr_reg_20.dattr.attr,
  583. &psmouse_attr_reg_21.dattr.attr,
  584. &psmouse_attr_reg_22.dattr.attr,
  585. &psmouse_attr_reg_23.dattr.attr,
  586. &psmouse_attr_reg_24.dattr.attr,
  587. &psmouse_attr_reg_25.dattr.attr,
  588. &psmouse_attr_reg_26.dattr.attr,
  589. &psmouse_attr_debug.dattr.attr,
  590. &psmouse_attr_paritycheck.dattr.attr,
  591. NULL
  592. };
  593. static struct attribute_group elantech_attr_group = {
  594. .attrs = elantech_attrs,
  595. };
  596. static bool elantech_is_signature_valid(const unsigned char *param)
  597. {
  598. static const unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10 };
  599. int i;
  600. if (param[0] == 0)
  601. return false;
  602. if (param[1] == 0)
  603. return true;
  604. for (i = 0; i < ARRAY_SIZE(rates); i++)
  605. if (param[2] == rates[i])
  606. return false;
  607. return true;
  608. }
  609. /*
  610. * Use magic knock to detect Elantech touchpad
  611. */
  612. int elantech_detect(struct psmouse *psmouse, bool set_properties)
  613. {
  614. struct ps2dev *ps2dev = &psmouse->ps2dev;
  615. unsigned char param[3];
  616. ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
  617. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  618. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  619. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  620. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  621. ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) {
  622. pr_debug("sending Elantech magic knock failed.\n");
  623. return -1;
  624. }
  625. /*
  626. * Report this in case there are Elantech models that use a different
  627. * set of magic numbers
  628. */
  629. if (param[0] != 0x3c || param[1] != 0x03 || param[2] != 0xc8) {
  630. pr_debug("unexpected magic knock result 0x%02x, 0x%02x, 0x%02x.\n",
  631. param[0], param[1], param[2]);
  632. return -1;
  633. }
  634. /*
  635. * Query touchpad's firmware version and see if it reports known
  636. * value to avoid mis-detection. Logitech mice are known to respond
  637. * to Elantech magic knock and there might be more.
  638. */
  639. if (synaptics_send_cmd(psmouse, ETP_FW_VERSION_QUERY, param)) {
  640. pr_debug("failed to query firmware version.\n");
  641. return -1;
  642. }
  643. pr_debug("Elantech version query result 0x%02x, 0x%02x, 0x%02x.\n",
  644. param[0], param[1], param[2]);
  645. if (!elantech_is_signature_valid(param)) {
  646. if (!force_elantech) {
  647. pr_debug("Probably not a real Elantech touchpad. Aborting.\n");
  648. return -1;
  649. }
  650. pr_debug("Probably not a real Elantech touchpad. Enabling anyway due to force_elantech.\n");
  651. }
  652. if (set_properties) {
  653. psmouse->vendor = "Elantech";
  654. psmouse->name = "Touchpad";
  655. }
  656. return 0;
  657. }
  658. /*
  659. * Clean up sysfs entries when disconnecting
  660. */
  661. static void elantech_disconnect(struct psmouse *psmouse)
  662. {
  663. sysfs_remove_group(&psmouse->ps2dev.serio->dev.kobj,
  664. &elantech_attr_group);
  665. kfree(psmouse->private);
  666. psmouse->private = NULL;
  667. }
  668. /*
  669. * Put the touchpad back into absolute mode when reconnecting
  670. */
  671. static int elantech_reconnect(struct psmouse *psmouse)
  672. {
  673. if (elantech_detect(psmouse, 0))
  674. return -1;
  675. if (elantech_set_absolute_mode(psmouse)) {
  676. pr_err("failed to put touchpad back into absolute mode.\n");
  677. return -1;
  678. }
  679. return 0;
  680. }
  681. /*
  682. * Initialize the touchpad and create sysfs entries
  683. */
  684. int elantech_init(struct psmouse *psmouse)
  685. {
  686. struct elantech_data *etd;
  687. int i, error;
  688. unsigned char param[3];
  689. psmouse->private = etd = kzalloc(sizeof(struct elantech_data), GFP_KERNEL);
  690. if (!etd)
  691. return -ENOMEM;
  692. etd->parity[0] = 1;
  693. for (i = 1; i < 256; i++)
  694. etd->parity[i] = etd->parity[i & (i - 1)] ^ 1;
  695. /*
  696. * Do the version query again so we can store the result
  697. */
  698. if (synaptics_send_cmd(psmouse, ETP_FW_VERSION_QUERY, param)) {
  699. pr_err("failed to query firmware version.\n");
  700. goto init_fail;
  701. }
  702. etd->fw_version = (param[0] << 16) | (param[1] << 8) | param[2];
  703. /*
  704. * Assume every version greater than this is new EeePC style
  705. * hardware with 6 byte packets
  706. */
  707. if (etd->fw_version >= 0x020030) {
  708. etd->hw_version = 2;
  709. /* For now show extra debug information */
  710. etd->debug = 1;
  711. etd->paritycheck = 1;
  712. if (etd->fw_version >= 0x020800)
  713. etd->reports_pressure = true;
  714. } else {
  715. etd->hw_version = 1;
  716. etd->paritycheck = 1;
  717. }
  718. pr_info("assuming hardware version %d, firmware version %d.%d.%d\n",
  719. etd->hw_version, param[0], param[1], param[2]);
  720. if (synaptics_send_cmd(psmouse, ETP_CAPABILITIES_QUERY,
  721. etd->capabilities)) {
  722. pr_err("failed to query capabilities.\n");
  723. goto init_fail;
  724. }
  725. pr_info("Synaptics capabilities query result 0x%02x, 0x%02x, 0x%02x.\n",
  726. etd->capabilities[0], etd->capabilities[1],
  727. etd->capabilities[2]);
  728. /*
  729. * This firmware suffers from misreporting coordinates when
  730. * a touch action starts causing the mouse cursor or scrolled page
  731. * to jump. Enable a workaround.
  732. */
  733. if (etd->fw_version == 0x020022 || etd->fw_version == 0x020600) {
  734. pr_info("firmware version 2.0.34/2.6.0 detected, enabling jumpy cursor workaround\n");
  735. etd->jumpy_cursor = true;
  736. }
  737. if (elantech_set_absolute_mode(psmouse)) {
  738. pr_err("failed to put touchpad into absolute mode.\n");
  739. goto init_fail;
  740. }
  741. elantech_set_input_params(psmouse);
  742. error = sysfs_create_group(&psmouse->ps2dev.serio->dev.kobj,
  743. &elantech_attr_group);
  744. if (error) {
  745. pr_err("failed to create sysfs attributes, error: %d.\n", error);
  746. goto init_fail;
  747. }
  748. psmouse->protocol_handler = elantech_process_byte;
  749. psmouse->disconnect = elantech_disconnect;
  750. psmouse->reconnect = elantech_reconnect;
  751. psmouse->pktsize = etd->hw_version == 2 ? 6 : 4;
  752. return 0;
  753. init_fail:
  754. kfree(etd);
  755. return -1;
  756. }