elantech.c 18 KB

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