elantech.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641
  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/dmi.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 <asm/unaligned.h>
  21. #include "psmouse.h"
  22. #include "elantech.h"
  23. #define elantech_debug(fmt, ...) \
  24. do { \
  25. if (etd->debug) \
  26. psmouse_printk(KERN_DEBUG, psmouse, \
  27. fmt, ##__VA_ARGS__); \
  28. } while (0)
  29. /*
  30. * Send a Synaptics style sliced query command
  31. */
  32. static int synaptics_send_cmd(struct psmouse *psmouse, unsigned char c,
  33. unsigned char *param)
  34. {
  35. if (psmouse_sliced_command(psmouse, c) ||
  36. ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_GETINFO)) {
  37. psmouse_err(psmouse, "%s query 0x%02x failed.\n", __func__, c);
  38. return -1;
  39. }
  40. return 0;
  41. }
  42. /*
  43. * V3 and later support this fast command
  44. */
  45. static int elantech_send_cmd(struct psmouse *psmouse, unsigned char c,
  46. unsigned char *param)
  47. {
  48. struct ps2dev *ps2dev = &psmouse->ps2dev;
  49. if (ps2_command(ps2dev, NULL, ETP_PS2_CUSTOM_COMMAND) ||
  50. ps2_command(ps2dev, NULL, c) ||
  51. ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) {
  52. psmouse_err(psmouse, "%s query 0x%02x failed.\n", __func__, c);
  53. return -1;
  54. }
  55. return 0;
  56. }
  57. /*
  58. * A retrying version of ps2_command
  59. */
  60. static int elantech_ps2_command(struct psmouse *psmouse,
  61. unsigned char *param, int command)
  62. {
  63. struct ps2dev *ps2dev = &psmouse->ps2dev;
  64. struct elantech_data *etd = psmouse->private;
  65. int rc;
  66. int tries = ETP_PS2_COMMAND_TRIES;
  67. do {
  68. rc = ps2_command(ps2dev, param, command);
  69. if (rc == 0)
  70. break;
  71. tries--;
  72. elantech_debug("retrying ps2 command 0x%02x (%d).\n",
  73. command, tries);
  74. msleep(ETP_PS2_COMMAND_DELAY);
  75. } while (tries > 0);
  76. if (rc)
  77. psmouse_err(psmouse, "ps2 command 0x%02x failed.\n", command);
  78. return rc;
  79. }
  80. /*
  81. * Send an Elantech style special command to read a value from a register
  82. */
  83. static int elantech_read_reg(struct psmouse *psmouse, unsigned char reg,
  84. unsigned char *val)
  85. {
  86. struct elantech_data *etd = psmouse->private;
  87. unsigned char param[3];
  88. int rc = 0;
  89. if (reg < 0x07 || reg > 0x26)
  90. return -1;
  91. if (reg > 0x11 && reg < 0x20)
  92. return -1;
  93. switch (etd->hw_version) {
  94. case 1:
  95. if (psmouse_sliced_command(psmouse, ETP_REGISTER_READ) ||
  96. psmouse_sliced_command(psmouse, reg) ||
  97. ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_GETINFO)) {
  98. rc = -1;
  99. }
  100. break;
  101. case 2:
  102. if (elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
  103. elantech_ps2_command(psmouse, NULL, ETP_REGISTER_READ) ||
  104. elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
  105. elantech_ps2_command(psmouse, NULL, reg) ||
  106. elantech_ps2_command(psmouse, param, PSMOUSE_CMD_GETINFO)) {
  107. rc = -1;
  108. }
  109. break;
  110. case 3 ... 4:
  111. if (elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
  112. elantech_ps2_command(psmouse, NULL, ETP_REGISTER_READWRITE) ||
  113. elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
  114. elantech_ps2_command(psmouse, NULL, reg) ||
  115. elantech_ps2_command(psmouse, param, PSMOUSE_CMD_GETINFO)) {
  116. rc = -1;
  117. }
  118. break;
  119. }
  120. if (rc)
  121. psmouse_err(psmouse, "failed to read register 0x%02x.\n", reg);
  122. else if (etd->hw_version != 4)
  123. *val = param[0];
  124. else
  125. *val = param[1];
  126. return rc;
  127. }
  128. /*
  129. * Send an Elantech style special command to write a register with a value
  130. */
  131. static int elantech_write_reg(struct psmouse *psmouse, unsigned char reg,
  132. unsigned char val)
  133. {
  134. struct elantech_data *etd = psmouse->private;
  135. int rc = 0;
  136. if (reg < 0x07 || reg > 0x26)
  137. return -1;
  138. if (reg > 0x11 && reg < 0x20)
  139. return -1;
  140. switch (etd->hw_version) {
  141. case 1:
  142. if (psmouse_sliced_command(psmouse, ETP_REGISTER_WRITE) ||
  143. psmouse_sliced_command(psmouse, reg) ||
  144. psmouse_sliced_command(psmouse, val) ||
  145. ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11)) {
  146. rc = -1;
  147. }
  148. break;
  149. case 2:
  150. if (elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
  151. elantech_ps2_command(psmouse, NULL, ETP_REGISTER_WRITE) ||
  152. elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
  153. elantech_ps2_command(psmouse, NULL, reg) ||
  154. elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
  155. elantech_ps2_command(psmouse, NULL, val) ||
  156. elantech_ps2_command(psmouse, NULL, PSMOUSE_CMD_SETSCALE11)) {
  157. rc = -1;
  158. }
  159. break;
  160. case 3:
  161. if (elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
  162. elantech_ps2_command(psmouse, NULL, ETP_REGISTER_READWRITE) ||
  163. elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
  164. elantech_ps2_command(psmouse, NULL, reg) ||
  165. elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
  166. elantech_ps2_command(psmouse, NULL, val) ||
  167. elantech_ps2_command(psmouse, NULL, PSMOUSE_CMD_SETSCALE11)) {
  168. rc = -1;
  169. }
  170. break;
  171. case 4:
  172. if (elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
  173. elantech_ps2_command(psmouse, NULL, ETP_REGISTER_READWRITE) ||
  174. elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
  175. elantech_ps2_command(psmouse, NULL, reg) ||
  176. elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
  177. elantech_ps2_command(psmouse, NULL, ETP_REGISTER_READWRITE) ||
  178. elantech_ps2_command(psmouse, NULL, ETP_PS2_CUSTOM_COMMAND) ||
  179. elantech_ps2_command(psmouse, NULL, val) ||
  180. elantech_ps2_command(psmouse, NULL, PSMOUSE_CMD_SETSCALE11)) {
  181. rc = -1;
  182. }
  183. break;
  184. }
  185. if (rc)
  186. psmouse_err(psmouse,
  187. "failed to write register 0x%02x with value 0x%02x.\n",
  188. reg, val);
  189. return rc;
  190. }
  191. /*
  192. * Dump a complete mouse movement packet to the syslog
  193. */
  194. static void elantech_packet_dump(struct psmouse *psmouse)
  195. {
  196. int i;
  197. psmouse_printk(KERN_DEBUG, psmouse, "PS/2 packet [");
  198. for (i = 0; i < psmouse->pktsize; i++)
  199. printk("%s0x%02x ", i ? ", " : " ", psmouse->packet[i]);
  200. printk("]\n");
  201. }
  202. /*
  203. * Interpret complete data packets and report absolute mode input events for
  204. * hardware version 1. (4 byte packets)
  205. */
  206. static void elantech_report_absolute_v1(struct psmouse *psmouse)
  207. {
  208. struct input_dev *dev = psmouse->dev;
  209. struct elantech_data *etd = psmouse->private;
  210. unsigned char *packet = psmouse->packet;
  211. int fingers;
  212. if (etd->fw_version < 0x020000) {
  213. /*
  214. * byte 0: D U p1 p2 1 p3 R L
  215. * byte 1: f 0 th tw x9 x8 y9 y8
  216. */
  217. fingers = ((packet[1] & 0x80) >> 7) +
  218. ((packet[1] & 0x30) >> 4);
  219. } else {
  220. /*
  221. * byte 0: n1 n0 p2 p1 1 p3 R L
  222. * byte 1: 0 0 0 0 x9 x8 y9 y8
  223. */
  224. fingers = (packet[0] & 0xc0) >> 6;
  225. }
  226. if (etd->jumpy_cursor) {
  227. if (fingers != 1) {
  228. etd->single_finger_reports = 0;
  229. } else if (etd->single_finger_reports < 2) {
  230. /* Discard first 2 reports of one finger, bogus */
  231. etd->single_finger_reports++;
  232. elantech_debug("discarding packet\n");
  233. return;
  234. }
  235. }
  236. input_report_key(dev, BTN_TOUCH, fingers != 0);
  237. /*
  238. * byte 2: x7 x6 x5 x4 x3 x2 x1 x0
  239. * byte 3: y7 y6 y5 y4 y3 y2 y1 y0
  240. */
  241. if (fingers) {
  242. input_report_abs(dev, ABS_X,
  243. ((packet[1] & 0x0c) << 6) | packet[2]);
  244. input_report_abs(dev, ABS_Y,
  245. etd->y_max - (((packet[1] & 0x03) << 8) | packet[3]));
  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. if (etd->fw_version < 0x020000 &&
  253. (etd->capabilities[0] & ETP_CAP_HAS_ROCKER)) {
  254. /* rocker up */
  255. input_report_key(dev, BTN_FORWARD, packet[0] & 0x40);
  256. /* rocker down */
  257. input_report_key(dev, BTN_BACK, packet[0] & 0x80);
  258. }
  259. input_sync(dev);
  260. }
  261. static void elantech_set_slot(struct input_dev *dev, int slot, bool active,
  262. unsigned int x, unsigned int y)
  263. {
  264. input_mt_slot(dev, slot);
  265. input_mt_report_slot_state(dev, MT_TOOL_FINGER, active);
  266. if (active) {
  267. input_report_abs(dev, ABS_MT_POSITION_X, x);
  268. input_report_abs(dev, ABS_MT_POSITION_Y, y);
  269. }
  270. }
  271. /* x1 < x2 and y1 < y2 when two fingers, x = y = 0 when not pressed */
  272. static void elantech_report_semi_mt_data(struct input_dev *dev,
  273. unsigned int num_fingers,
  274. unsigned int x1, unsigned int y1,
  275. unsigned int x2, unsigned int y2)
  276. {
  277. elantech_set_slot(dev, 0, num_fingers != 0, x1, y1);
  278. elantech_set_slot(dev, 1, num_fingers == 2, x2, y2);
  279. }
  280. /*
  281. * Interpret complete data packets and report absolute mode input events for
  282. * hardware version 2. (6 byte packets)
  283. */
  284. static void elantech_report_absolute_v2(struct psmouse *psmouse)
  285. {
  286. struct elantech_data *etd = psmouse->private;
  287. struct input_dev *dev = psmouse->dev;
  288. unsigned char *packet = psmouse->packet;
  289. unsigned int fingers, x1 = 0, y1 = 0, x2 = 0, y2 = 0;
  290. unsigned int width = 0, pres = 0;
  291. /* byte 0: n1 n0 . . . . R L */
  292. fingers = (packet[0] & 0xc0) >> 6;
  293. switch (fingers) {
  294. case 3:
  295. /*
  296. * Same as one finger, except report of more than 3 fingers:
  297. * byte 3: n4 . w1 w0 . . . .
  298. */
  299. if (packet[3] & 0x80)
  300. fingers = 4;
  301. /* pass through... */
  302. case 1:
  303. /*
  304. * byte 1: . . . . x11 x10 x9 x8
  305. * byte 2: x7 x6 x5 x4 x4 x2 x1 x0
  306. */
  307. x1 = ((packet[1] & 0x0f) << 8) | packet[2];
  308. /*
  309. * byte 4: . . . . y11 y10 y9 y8
  310. * byte 5: y7 y6 y5 y4 y3 y2 y1 y0
  311. */
  312. y1 = etd->y_max - (((packet[4] & 0x0f) << 8) | packet[5]);
  313. pres = (packet[1] & 0xf0) | ((packet[4] & 0xf0) >> 4);
  314. width = ((packet[0] & 0x30) >> 2) | ((packet[3] & 0x30) >> 4);
  315. break;
  316. case 2:
  317. /*
  318. * The coordinate of each finger is reported separately
  319. * with a lower resolution for two finger touches:
  320. * byte 0: . . ay8 ax8 . . . .
  321. * byte 1: ax7 ax6 ax5 ax4 ax3 ax2 ax1 ax0
  322. */
  323. x1 = (((packet[0] & 0x10) << 4) | packet[1]) << 2;
  324. /* byte 2: ay7 ay6 ay5 ay4 ay3 ay2 ay1 ay0 */
  325. y1 = etd->y_max -
  326. ((((packet[0] & 0x20) << 3) | packet[2]) << 2);
  327. /*
  328. * byte 3: . . by8 bx8 . . . .
  329. * byte 4: bx7 bx6 bx5 bx4 bx3 bx2 bx1 bx0
  330. */
  331. x2 = (((packet[3] & 0x10) << 4) | packet[4]) << 2;
  332. /* byte 5: by7 by8 by5 by4 by3 by2 by1 by0 */
  333. y2 = etd->y_max -
  334. ((((packet[3] & 0x20) << 3) | packet[5]) << 2);
  335. /* Unknown so just report sensible values */
  336. pres = 127;
  337. width = 7;
  338. break;
  339. }
  340. input_report_key(dev, BTN_TOUCH, fingers != 0);
  341. if (fingers != 0) {
  342. input_report_abs(dev, ABS_X, x1);
  343. input_report_abs(dev, ABS_Y, y1);
  344. }
  345. elantech_report_semi_mt_data(dev, fingers, x1, y1, x2, y2);
  346. input_report_key(dev, BTN_TOOL_FINGER, fingers == 1);
  347. input_report_key(dev, BTN_TOOL_DOUBLETAP, fingers == 2);
  348. input_report_key(dev, BTN_TOOL_TRIPLETAP, fingers == 3);
  349. input_report_key(dev, BTN_TOOL_QUADTAP, fingers == 4);
  350. input_report_key(dev, BTN_LEFT, packet[0] & 0x01);
  351. input_report_key(dev, BTN_RIGHT, packet[0] & 0x02);
  352. if (etd->reports_pressure) {
  353. input_report_abs(dev, ABS_PRESSURE, pres);
  354. input_report_abs(dev, ABS_TOOL_WIDTH, width);
  355. }
  356. input_sync(dev);
  357. }
  358. static void elantech_report_trackpoint(struct psmouse *psmouse,
  359. int packet_type)
  360. {
  361. /*
  362. * byte 0: 0 0 sx sy 0 M R L
  363. * byte 1:~sx 0 0 0 0 0 0 0
  364. * byte 2:~sy 0 0 0 0 0 0 0
  365. * byte 3: 0 0 ~sy ~sx 0 1 1 0
  366. * byte 4: x7 x6 x5 x4 x3 x2 x1 x0
  367. * byte 5: y7 y6 y5 y4 y3 y2 y1 y0
  368. *
  369. * x and y are written in two's complement spread
  370. * over 9 bits with sx/sy the relative top bit and
  371. * x7..x0 and y7..y0 the lower bits.
  372. * The sign of y is opposite to what the input driver
  373. * expects for a relative movement
  374. */
  375. struct elantech_data *etd = psmouse->private;
  376. struct input_dev *tp_dev = etd->tp_dev;
  377. unsigned char *packet = psmouse->packet;
  378. int x, y;
  379. u32 t;
  380. if (dev_WARN_ONCE(&psmouse->ps2dev.serio->dev,
  381. !tp_dev,
  382. psmouse_fmt("Unexpected trackpoint message\n"))) {
  383. if (etd->debug == 1)
  384. elantech_packet_dump(psmouse);
  385. return;
  386. }
  387. t = get_unaligned_le32(&packet[0]);
  388. switch (t & ~7U) {
  389. case 0x06000030U:
  390. case 0x16008020U:
  391. case 0x26800010U:
  392. case 0x36808000U:
  393. x = packet[4] - (int)((packet[1]^0x80) << 1);
  394. y = (int)((packet[2]^0x80) << 1) - packet[5];
  395. input_report_key(tp_dev, BTN_LEFT, packet[0] & 0x01);
  396. input_report_key(tp_dev, BTN_RIGHT, packet[0] & 0x02);
  397. input_report_key(tp_dev, BTN_MIDDLE, packet[0] & 0x04);
  398. input_report_rel(tp_dev, REL_X, x);
  399. input_report_rel(tp_dev, REL_Y, y);
  400. input_sync(tp_dev);
  401. break;
  402. default:
  403. /* Dump unexpected packet sequences if debug=1 (default) */
  404. if (etd->debug == 1)
  405. elantech_packet_dump(psmouse);
  406. break;
  407. }
  408. }
  409. /*
  410. * Interpret complete data packets and report absolute mode input events for
  411. * hardware version 3. (12 byte packets for two fingers)
  412. */
  413. static void elantech_report_absolute_v3(struct psmouse *psmouse,
  414. int packet_type)
  415. {
  416. struct input_dev *dev = psmouse->dev;
  417. struct elantech_data *etd = psmouse->private;
  418. unsigned char *packet = psmouse->packet;
  419. unsigned int fingers = 0, x1 = 0, y1 = 0, x2 = 0, y2 = 0;
  420. unsigned int width = 0, pres = 0;
  421. /* byte 0: n1 n0 . . . . R L */
  422. fingers = (packet[0] & 0xc0) >> 6;
  423. switch (fingers) {
  424. case 3:
  425. case 1:
  426. /*
  427. * byte 1: . . . . x11 x10 x9 x8
  428. * byte 2: x7 x6 x5 x4 x4 x2 x1 x0
  429. */
  430. x1 = ((packet[1] & 0x0f) << 8) | packet[2];
  431. /*
  432. * byte 4: . . . . y11 y10 y9 y8
  433. * byte 5: y7 y6 y5 y4 y3 y2 y1 y0
  434. */
  435. y1 = etd->y_max - (((packet[4] & 0x0f) << 8) | packet[5]);
  436. break;
  437. case 2:
  438. if (packet_type == PACKET_V3_HEAD) {
  439. /*
  440. * byte 1: . . . . ax11 ax10 ax9 ax8
  441. * byte 2: ax7 ax6 ax5 ax4 ax3 ax2 ax1 ax0
  442. */
  443. etd->mt[0].x = ((packet[1] & 0x0f) << 8) | packet[2];
  444. /*
  445. * byte 4: . . . . ay11 ay10 ay9 ay8
  446. * byte 5: ay7 ay6 ay5 ay4 ay3 ay2 ay1 ay0
  447. */
  448. etd->mt[0].y = etd->y_max -
  449. (((packet[4] & 0x0f) << 8) | packet[5]);
  450. /*
  451. * wait for next packet
  452. */
  453. return;
  454. }
  455. /* packet_type == PACKET_V3_TAIL */
  456. x1 = etd->mt[0].x;
  457. y1 = etd->mt[0].y;
  458. x2 = ((packet[1] & 0x0f) << 8) | packet[2];
  459. y2 = etd->y_max - (((packet[4] & 0x0f) << 8) | packet[5]);
  460. break;
  461. }
  462. pres = (packet[1] & 0xf0) | ((packet[4] & 0xf0) >> 4);
  463. width = ((packet[0] & 0x30) >> 2) | ((packet[3] & 0x30) >> 4);
  464. input_report_key(dev, BTN_TOUCH, fingers != 0);
  465. if (fingers != 0) {
  466. input_report_abs(dev, ABS_X, x1);
  467. input_report_abs(dev, ABS_Y, y1);
  468. }
  469. elantech_report_semi_mt_data(dev, fingers, x1, y1, x2, y2);
  470. input_report_key(dev, BTN_TOOL_FINGER, fingers == 1);
  471. input_report_key(dev, BTN_TOOL_DOUBLETAP, fingers == 2);
  472. input_report_key(dev, BTN_TOOL_TRIPLETAP, fingers == 3);
  473. /* For clickpads map both buttons to BTN_LEFT */
  474. if (etd->fw_version & 0x001000) {
  475. input_report_key(dev, BTN_LEFT, packet[0] & 0x03);
  476. } else {
  477. input_report_key(dev, BTN_LEFT, packet[0] & 0x01);
  478. input_report_key(dev, BTN_RIGHT, packet[0] & 0x02);
  479. }
  480. input_report_abs(dev, ABS_PRESSURE, pres);
  481. input_report_abs(dev, ABS_TOOL_WIDTH, width);
  482. input_sync(dev);
  483. }
  484. static void elantech_input_sync_v4(struct psmouse *psmouse)
  485. {
  486. struct input_dev *dev = psmouse->dev;
  487. struct elantech_data *etd = psmouse->private;
  488. unsigned char *packet = psmouse->packet;
  489. /* For clickpads map both buttons to BTN_LEFT */
  490. if (etd->fw_version & 0x001000) {
  491. input_report_key(dev, BTN_LEFT, packet[0] & 0x03);
  492. } else {
  493. input_report_key(dev, BTN_LEFT, packet[0] & 0x01);
  494. input_report_key(dev, BTN_RIGHT, packet[0] & 0x02);
  495. }
  496. input_mt_report_pointer_emulation(dev, true);
  497. input_sync(dev);
  498. }
  499. static void process_packet_status_v4(struct psmouse *psmouse)
  500. {
  501. struct input_dev *dev = psmouse->dev;
  502. unsigned char *packet = psmouse->packet;
  503. unsigned fingers;
  504. int i;
  505. /* notify finger state change */
  506. fingers = packet[1] & 0x1f;
  507. for (i = 0; i < ETP_MAX_FINGERS; i++) {
  508. if ((fingers & (1 << i)) == 0) {
  509. input_mt_slot(dev, i);
  510. input_mt_report_slot_state(dev, MT_TOOL_FINGER, false);
  511. }
  512. }
  513. elantech_input_sync_v4(psmouse);
  514. }
  515. static void process_packet_head_v4(struct psmouse *psmouse)
  516. {
  517. struct input_dev *dev = psmouse->dev;
  518. struct elantech_data *etd = psmouse->private;
  519. unsigned char *packet = psmouse->packet;
  520. int id = ((packet[3] & 0xe0) >> 5) - 1;
  521. int pres, traces;
  522. if (id < 0)
  523. return;
  524. etd->mt[id].x = ((packet[1] & 0x0f) << 8) | packet[2];
  525. etd->mt[id].y = etd->y_max - (((packet[4] & 0x0f) << 8) | packet[5]);
  526. pres = (packet[1] & 0xf0) | ((packet[4] & 0xf0) >> 4);
  527. traces = (packet[0] & 0xf0) >> 4;
  528. input_mt_slot(dev, id);
  529. input_mt_report_slot_state(dev, MT_TOOL_FINGER, true);
  530. input_report_abs(dev, ABS_MT_POSITION_X, etd->mt[id].x);
  531. input_report_abs(dev, ABS_MT_POSITION_Y, etd->mt[id].y);
  532. input_report_abs(dev, ABS_MT_PRESSURE, pres);
  533. input_report_abs(dev, ABS_MT_TOUCH_MAJOR, traces * etd->width);
  534. /* report this for backwards compatibility */
  535. input_report_abs(dev, ABS_TOOL_WIDTH, traces);
  536. elantech_input_sync_v4(psmouse);
  537. }
  538. static void process_packet_motion_v4(struct psmouse *psmouse)
  539. {
  540. struct input_dev *dev = psmouse->dev;
  541. struct elantech_data *etd = psmouse->private;
  542. unsigned char *packet = psmouse->packet;
  543. int weight, delta_x1 = 0, delta_y1 = 0, delta_x2 = 0, delta_y2 = 0;
  544. int id, sid;
  545. id = ((packet[0] & 0xe0) >> 5) - 1;
  546. if (id < 0)
  547. return;
  548. sid = ((packet[3] & 0xe0) >> 5) - 1;
  549. weight = (packet[0] & 0x10) ? ETP_WEIGHT_VALUE : 1;
  550. /*
  551. * Motion packets give us the delta of x, y values of specific fingers,
  552. * but in two's complement. Let the compiler do the conversion for us.
  553. * Also _enlarge_ the numbers to int, in case of overflow.
  554. */
  555. delta_x1 = (signed char)packet[1];
  556. delta_y1 = (signed char)packet[2];
  557. delta_x2 = (signed char)packet[4];
  558. delta_y2 = (signed char)packet[5];
  559. etd->mt[id].x += delta_x1 * weight;
  560. etd->mt[id].y -= delta_y1 * weight;
  561. input_mt_slot(dev, id);
  562. input_report_abs(dev, ABS_MT_POSITION_X, etd->mt[id].x);
  563. input_report_abs(dev, ABS_MT_POSITION_Y, etd->mt[id].y);
  564. if (sid >= 0) {
  565. etd->mt[sid].x += delta_x2 * weight;
  566. etd->mt[sid].y -= delta_y2 * weight;
  567. input_mt_slot(dev, sid);
  568. input_report_abs(dev, ABS_MT_POSITION_X, etd->mt[sid].x);
  569. input_report_abs(dev, ABS_MT_POSITION_Y, etd->mt[sid].y);
  570. }
  571. elantech_input_sync_v4(psmouse);
  572. }
  573. static void elantech_report_absolute_v4(struct psmouse *psmouse,
  574. int packet_type)
  575. {
  576. switch (packet_type) {
  577. case PACKET_V4_STATUS:
  578. process_packet_status_v4(psmouse);
  579. break;
  580. case PACKET_V4_HEAD:
  581. process_packet_head_v4(psmouse);
  582. break;
  583. case PACKET_V4_MOTION:
  584. process_packet_motion_v4(psmouse);
  585. break;
  586. case PACKET_UNKNOWN:
  587. default:
  588. /* impossible to get here */
  589. break;
  590. }
  591. }
  592. static int elantech_packet_check_v1(struct psmouse *psmouse)
  593. {
  594. struct elantech_data *etd = psmouse->private;
  595. unsigned char *packet = psmouse->packet;
  596. unsigned char p1, p2, p3;
  597. /* Parity bits are placed differently */
  598. if (etd->fw_version < 0x020000) {
  599. /* byte 0: D U p1 p2 1 p3 R L */
  600. p1 = (packet[0] & 0x20) >> 5;
  601. p2 = (packet[0] & 0x10) >> 4;
  602. } else {
  603. /* byte 0: n1 n0 p2 p1 1 p3 R L */
  604. p1 = (packet[0] & 0x10) >> 4;
  605. p2 = (packet[0] & 0x20) >> 5;
  606. }
  607. p3 = (packet[0] & 0x04) >> 2;
  608. return etd->parity[packet[1]] == p1 &&
  609. etd->parity[packet[2]] == p2 &&
  610. etd->parity[packet[3]] == p3;
  611. }
  612. static int elantech_debounce_check_v2(struct psmouse *psmouse)
  613. {
  614. /*
  615. * When we encounter packet that matches this exactly, it means the
  616. * hardware is in debounce status. Just ignore the whole packet.
  617. */
  618. const u8 debounce_packet[] = { 0x84, 0xff, 0xff, 0x02, 0xff, 0xff };
  619. unsigned char *packet = psmouse->packet;
  620. return !memcmp(packet, debounce_packet, sizeof(debounce_packet));
  621. }
  622. static int elantech_packet_check_v2(struct psmouse *psmouse)
  623. {
  624. struct elantech_data *etd = psmouse->private;
  625. unsigned char *packet = psmouse->packet;
  626. /*
  627. * V2 hardware has two flavors. Older ones that do not report pressure,
  628. * and newer ones that reports pressure and width. With newer ones, all
  629. * packets (1, 2, 3 finger touch) have the same constant bits. With
  630. * older ones, 1/3 finger touch packets and 2 finger touch packets
  631. * have different constant bits.
  632. * With all three cases, if the constant bits are not exactly what I
  633. * expected, I consider them invalid.
  634. */
  635. if (etd->reports_pressure)
  636. return (packet[0] & 0x0c) == 0x04 &&
  637. (packet[3] & 0x0f) == 0x02;
  638. if ((packet[0] & 0xc0) == 0x80)
  639. return (packet[0] & 0x0c) == 0x0c &&
  640. (packet[3] & 0x0e) == 0x08;
  641. return (packet[0] & 0x3c) == 0x3c &&
  642. (packet[1] & 0xf0) == 0x00 &&
  643. (packet[3] & 0x3e) == 0x38 &&
  644. (packet[4] & 0xf0) == 0x00;
  645. }
  646. /*
  647. * We check the constant bits to determine what packet type we get,
  648. * so packet checking is mandatory for v3 and later hardware.
  649. */
  650. static int elantech_packet_check_v3(struct psmouse *psmouse)
  651. {
  652. struct elantech_data *etd = psmouse->private;
  653. const u8 debounce_packet[] = { 0xc4, 0xff, 0xff, 0x02, 0xff, 0xff };
  654. unsigned char *packet = psmouse->packet;
  655. /*
  656. * check debounce first, it has the same signature in byte 0
  657. * and byte 3 as PACKET_V3_HEAD.
  658. */
  659. if (!memcmp(packet, debounce_packet, sizeof(debounce_packet)))
  660. return PACKET_DEBOUNCE;
  661. /*
  662. * If the hardware flag 'crc_enabled' is set the packets have
  663. * different signatures.
  664. */
  665. if (etd->crc_enabled) {
  666. if ((packet[3] & 0x09) == 0x08)
  667. return PACKET_V3_HEAD;
  668. if ((packet[3] & 0x09) == 0x09)
  669. return PACKET_V3_TAIL;
  670. } else {
  671. if ((packet[0] & 0x0c) == 0x04 && (packet[3] & 0xcf) == 0x02)
  672. return PACKET_V3_HEAD;
  673. if ((packet[0] & 0x0c) == 0x0c && (packet[3] & 0xce) == 0x0c)
  674. return PACKET_V3_TAIL;
  675. if ((packet[3] & 0x0f) == 0x06)
  676. return PACKET_TRACKPOINT;
  677. }
  678. return PACKET_UNKNOWN;
  679. }
  680. static int elantech_packet_check_v4(struct psmouse *psmouse)
  681. {
  682. struct elantech_data *etd = psmouse->private;
  683. unsigned char *packet = psmouse->packet;
  684. unsigned char packet_type = packet[3] & 0x03;
  685. bool sanity_check;
  686. /*
  687. * Sanity check based on the constant bits of a packet.
  688. * The constant bits change depending on the value of
  689. * the hardware flag 'crc_enabled' but are the same for
  690. * every packet, regardless of the type.
  691. */
  692. if (etd->crc_enabled)
  693. sanity_check = ((packet[3] & 0x08) == 0x00);
  694. else
  695. sanity_check = ((packet[0] & 0x0c) == 0x04 &&
  696. (packet[3] & 0x1c) == 0x10);
  697. if (!sanity_check)
  698. return PACKET_UNKNOWN;
  699. switch (packet_type) {
  700. case 0:
  701. return PACKET_V4_STATUS;
  702. case 1:
  703. return PACKET_V4_HEAD;
  704. case 2:
  705. return PACKET_V4_MOTION;
  706. }
  707. return PACKET_UNKNOWN;
  708. }
  709. /*
  710. * Process byte stream from mouse and handle complete packets
  711. */
  712. static psmouse_ret_t elantech_process_byte(struct psmouse *psmouse)
  713. {
  714. struct elantech_data *etd = psmouse->private;
  715. int packet_type;
  716. if (psmouse->pktcnt < psmouse->pktsize)
  717. return PSMOUSE_GOOD_DATA;
  718. if (etd->debug > 1)
  719. elantech_packet_dump(psmouse);
  720. switch (etd->hw_version) {
  721. case 1:
  722. if (etd->paritycheck && !elantech_packet_check_v1(psmouse))
  723. return PSMOUSE_BAD_DATA;
  724. elantech_report_absolute_v1(psmouse);
  725. break;
  726. case 2:
  727. /* ignore debounce */
  728. if (elantech_debounce_check_v2(psmouse))
  729. return PSMOUSE_FULL_PACKET;
  730. if (etd->paritycheck && !elantech_packet_check_v2(psmouse))
  731. return PSMOUSE_BAD_DATA;
  732. elantech_report_absolute_v2(psmouse);
  733. break;
  734. case 3:
  735. packet_type = elantech_packet_check_v3(psmouse);
  736. switch (packet_type) {
  737. case PACKET_UNKNOWN:
  738. return PSMOUSE_BAD_DATA;
  739. case PACKET_DEBOUNCE:
  740. /* ignore debounce */
  741. break;
  742. case PACKET_TRACKPOINT:
  743. elantech_report_trackpoint(psmouse, packet_type);
  744. break;
  745. default:
  746. elantech_report_absolute_v3(psmouse, packet_type);
  747. break;
  748. }
  749. break;
  750. case 4:
  751. packet_type = elantech_packet_check_v4(psmouse);
  752. if (packet_type == PACKET_UNKNOWN)
  753. return PSMOUSE_BAD_DATA;
  754. elantech_report_absolute_v4(psmouse, packet_type);
  755. break;
  756. }
  757. return PSMOUSE_FULL_PACKET;
  758. }
  759. /*
  760. * Put the touchpad into absolute mode
  761. */
  762. static int elantech_set_absolute_mode(struct psmouse *psmouse)
  763. {
  764. struct elantech_data *etd = psmouse->private;
  765. unsigned char val;
  766. int tries = ETP_READ_BACK_TRIES;
  767. int rc = 0;
  768. switch (etd->hw_version) {
  769. case 1:
  770. etd->reg_10 = 0x16;
  771. etd->reg_11 = 0x8f;
  772. if (elantech_write_reg(psmouse, 0x10, etd->reg_10) ||
  773. elantech_write_reg(psmouse, 0x11, etd->reg_11)) {
  774. rc = -1;
  775. }
  776. break;
  777. case 2:
  778. /* Windows driver values */
  779. etd->reg_10 = 0x54;
  780. etd->reg_11 = 0x88; /* 0x8a */
  781. etd->reg_21 = 0x60; /* 0x00 */
  782. if (elantech_write_reg(psmouse, 0x10, etd->reg_10) ||
  783. elantech_write_reg(psmouse, 0x11, etd->reg_11) ||
  784. elantech_write_reg(psmouse, 0x21, etd->reg_21)) {
  785. rc = -1;
  786. }
  787. break;
  788. case 3:
  789. if (etd->set_hw_resolution)
  790. etd->reg_10 = 0x0b;
  791. else
  792. etd->reg_10 = 0x01;
  793. if (elantech_write_reg(psmouse, 0x10, etd->reg_10))
  794. rc = -1;
  795. break;
  796. case 4:
  797. etd->reg_07 = 0x01;
  798. if (elantech_write_reg(psmouse, 0x07, etd->reg_07))
  799. rc = -1;
  800. goto skip_readback_reg_10; /* v4 has no reg 0x10 to read */
  801. }
  802. if (rc == 0) {
  803. /*
  804. * Read back reg 0x10. For hardware version 1 we must make
  805. * sure the absolute mode bit is set. For hardware version 2
  806. * the touchpad is probably initializing and not ready until
  807. * we read back the value we just wrote.
  808. */
  809. do {
  810. rc = elantech_read_reg(psmouse, 0x10, &val);
  811. if (rc == 0)
  812. break;
  813. tries--;
  814. elantech_debug("retrying read (%d).\n", tries);
  815. msleep(ETP_READ_BACK_DELAY);
  816. } while (tries > 0);
  817. if (rc) {
  818. psmouse_err(psmouse,
  819. "failed to read back register 0x10.\n");
  820. } else if (etd->hw_version == 1 &&
  821. !(val & ETP_R10_ABSOLUTE_MODE)) {
  822. psmouse_err(psmouse,
  823. "touchpad refuses to switch to absolute mode.\n");
  824. rc = -1;
  825. }
  826. }
  827. skip_readback_reg_10:
  828. if (rc)
  829. psmouse_err(psmouse, "failed to initialise registers.\n");
  830. return rc;
  831. }
  832. static int elantech_set_range(struct psmouse *psmouse,
  833. unsigned int *x_min, unsigned int *y_min,
  834. unsigned int *x_max, unsigned int *y_max,
  835. unsigned int *width)
  836. {
  837. struct elantech_data *etd = psmouse->private;
  838. unsigned char param[3];
  839. unsigned char traces;
  840. switch (etd->hw_version) {
  841. case 1:
  842. *x_min = ETP_XMIN_V1;
  843. *y_min = ETP_YMIN_V1;
  844. *x_max = ETP_XMAX_V1;
  845. *y_max = ETP_YMAX_V1;
  846. break;
  847. case 2:
  848. if (etd->fw_version == 0x020800 ||
  849. etd->fw_version == 0x020b00 ||
  850. etd->fw_version == 0x020030) {
  851. *x_min = ETP_XMIN_V2;
  852. *y_min = ETP_YMIN_V2;
  853. *x_max = ETP_XMAX_V2;
  854. *y_max = ETP_YMAX_V2;
  855. } else {
  856. int i;
  857. int fixed_dpi;
  858. i = (etd->fw_version > 0x020800 &&
  859. etd->fw_version < 0x020900) ? 1 : 2;
  860. if (etd->send_cmd(psmouse, ETP_FW_ID_QUERY, param))
  861. return -1;
  862. fixed_dpi = param[1] & 0x10;
  863. if (((etd->fw_version >> 16) == 0x14) && fixed_dpi) {
  864. if (etd->send_cmd(psmouse, ETP_SAMPLE_QUERY, param))
  865. return -1;
  866. *x_max = (etd->capabilities[1] - i) * param[1] / 2;
  867. *y_max = (etd->capabilities[2] - i) * param[2] / 2;
  868. } else if (etd->fw_version == 0x040216) {
  869. *x_max = 819;
  870. *y_max = 405;
  871. } else if (etd->fw_version == 0x040219 || etd->fw_version == 0x040215) {
  872. *x_max = 900;
  873. *y_max = 500;
  874. } else {
  875. *x_max = (etd->capabilities[1] - i) * 64;
  876. *y_max = (etd->capabilities[2] - i) * 64;
  877. }
  878. }
  879. break;
  880. case 3:
  881. if (etd->send_cmd(psmouse, ETP_FW_ID_QUERY, param))
  882. return -1;
  883. *x_max = (0x0f & param[0]) << 8 | param[1];
  884. *y_max = (0xf0 & param[0]) << 4 | param[2];
  885. break;
  886. case 4:
  887. if (etd->send_cmd(psmouse, ETP_FW_ID_QUERY, param))
  888. return -1;
  889. *x_max = (0x0f & param[0]) << 8 | param[1];
  890. *y_max = (0xf0 & param[0]) << 4 | param[2];
  891. traces = etd->capabilities[1];
  892. if ((traces < 2) || (traces > *x_max))
  893. return -1;
  894. *width = *x_max / (traces - 1);
  895. break;
  896. }
  897. return 0;
  898. }
  899. /*
  900. * (value from firmware) * 10 + 790 = dpi
  901. * we also have to convert dpi to dots/mm (*10/254 to avoid floating point)
  902. */
  903. static unsigned int elantech_convert_res(unsigned int val)
  904. {
  905. return (val * 10 + 790) * 10 / 254;
  906. }
  907. static int elantech_get_resolution_v4(struct psmouse *psmouse,
  908. unsigned int *x_res,
  909. unsigned int *y_res)
  910. {
  911. unsigned char param[3];
  912. if (elantech_send_cmd(psmouse, ETP_RESOLUTION_QUERY, param))
  913. return -1;
  914. *x_res = elantech_convert_res(param[1] & 0x0f);
  915. *y_res = elantech_convert_res((param[1] & 0xf0) >> 4);
  916. return 0;
  917. }
  918. /*
  919. * Advertise INPUT_PROP_BUTTONPAD for clickpads. The testing of bit 12 in
  920. * fw_version for this is based on the following fw_version & caps table:
  921. *
  922. * Laptop-model: fw_version: caps: buttons:
  923. * Acer S3 0x461f00 10, 13, 0e clickpad
  924. * Acer S7-392 0x581f01 50, 17, 0d clickpad
  925. * Acer V5-131 0x461f02 01, 16, 0c clickpad
  926. * Acer V5-551 0x461f00 ? clickpad
  927. * Asus K53SV 0x450f01 78, 15, 0c 2 hw buttons
  928. * Asus G46VW 0x460f02 00, 18, 0c 2 hw buttons
  929. * Asus G750JX 0x360f00 00, 16, 0c 2 hw buttons
  930. * Asus UX31 0x361f00 20, 15, 0e clickpad
  931. * Asus UX32VD 0x361f02 00, 15, 0e clickpad
  932. * Avatar AVIU-145A2 0x361f00 ? clickpad
  933. * Fujitsu H730 0x570f00 c0, 14, 0c 3 hw buttons (**)
  934. * Gigabyte U2442 0x450f01 58, 17, 0c 2 hw buttons
  935. * Lenovo L430 0x350f02 b9, 15, 0c 2 hw buttons (*)
  936. * Lenovo L530 0x350f02 b9, 15, 0c 2 hw buttons (*)
  937. * Samsung NF210 0x150b00 78, 14, 0a 2 hw buttons
  938. * Samsung NP770Z5E 0x575f01 10, 15, 0f clickpad
  939. * Samsung NP700Z5B 0x361f06 21, 15, 0f clickpad
  940. * Samsung NP900X3E-A02 0x575f03 ? clickpad
  941. * Samsung NP-QX410 0x851b00 19, 14, 0c clickpad
  942. * Samsung RC512 0x450f00 08, 15, 0c 2 hw buttons
  943. * Samsung RF710 0x450f00 ? 2 hw buttons
  944. * System76 Pangolin 0x250f01 ? 2 hw buttons
  945. * (*) + 3 trackpoint buttons
  946. * (**) + 0 trackpoint buttons
  947. * Note: Lenovo L430 and Lenovo L430 have the same fw_version/caps
  948. */
  949. static void elantech_set_buttonpad_prop(struct psmouse *psmouse)
  950. {
  951. struct input_dev *dev = psmouse->dev;
  952. struct elantech_data *etd = psmouse->private;
  953. if (etd->fw_version & 0x001000) {
  954. __set_bit(INPUT_PROP_BUTTONPAD, dev->propbit);
  955. __clear_bit(BTN_RIGHT, dev->keybit);
  956. }
  957. }
  958. /*
  959. * Set the appropriate event bits for the input subsystem
  960. */
  961. static int elantech_set_input_params(struct psmouse *psmouse)
  962. {
  963. struct input_dev *dev = psmouse->dev;
  964. struct elantech_data *etd = psmouse->private;
  965. unsigned int x_min = 0, y_min = 0, x_max = 0, y_max = 0, width = 0;
  966. unsigned int x_res = 0, y_res = 0;
  967. if (elantech_set_range(psmouse, &x_min, &y_min, &x_max, &y_max, &width))
  968. return -1;
  969. __set_bit(INPUT_PROP_POINTER, dev->propbit);
  970. __set_bit(EV_KEY, dev->evbit);
  971. __set_bit(EV_ABS, dev->evbit);
  972. __clear_bit(EV_REL, dev->evbit);
  973. __set_bit(BTN_LEFT, dev->keybit);
  974. __set_bit(BTN_RIGHT, dev->keybit);
  975. __set_bit(BTN_TOUCH, dev->keybit);
  976. __set_bit(BTN_TOOL_FINGER, dev->keybit);
  977. __set_bit(BTN_TOOL_DOUBLETAP, dev->keybit);
  978. __set_bit(BTN_TOOL_TRIPLETAP, dev->keybit);
  979. switch (etd->hw_version) {
  980. case 1:
  981. /* Rocker button */
  982. if (etd->fw_version < 0x020000 &&
  983. (etd->capabilities[0] & ETP_CAP_HAS_ROCKER)) {
  984. __set_bit(BTN_FORWARD, dev->keybit);
  985. __set_bit(BTN_BACK, dev->keybit);
  986. }
  987. input_set_abs_params(dev, ABS_X, x_min, x_max, 0, 0);
  988. input_set_abs_params(dev, ABS_Y, y_min, y_max, 0, 0);
  989. break;
  990. case 2:
  991. __set_bit(BTN_TOOL_QUADTAP, dev->keybit);
  992. __set_bit(INPUT_PROP_SEMI_MT, dev->propbit);
  993. /* fall through */
  994. case 3:
  995. if (etd->hw_version == 3)
  996. elantech_set_buttonpad_prop(psmouse);
  997. input_set_abs_params(dev, ABS_X, x_min, x_max, 0, 0);
  998. input_set_abs_params(dev, ABS_Y, y_min, y_max, 0, 0);
  999. if (etd->reports_pressure) {
  1000. input_set_abs_params(dev, ABS_PRESSURE, ETP_PMIN_V2,
  1001. ETP_PMAX_V2, 0, 0);
  1002. input_set_abs_params(dev, ABS_TOOL_WIDTH, ETP_WMIN_V2,
  1003. ETP_WMAX_V2, 0, 0);
  1004. }
  1005. input_mt_init_slots(dev, 2, 0);
  1006. input_set_abs_params(dev, ABS_MT_POSITION_X, x_min, x_max, 0, 0);
  1007. input_set_abs_params(dev, ABS_MT_POSITION_Y, y_min, y_max, 0, 0);
  1008. break;
  1009. case 4:
  1010. if (elantech_get_resolution_v4(psmouse, &x_res, &y_res)) {
  1011. /*
  1012. * if query failed, print a warning and leave the values
  1013. * zero to resemble synaptics.c behavior.
  1014. */
  1015. psmouse_warn(psmouse, "couldn't query resolution data.\n");
  1016. }
  1017. elantech_set_buttonpad_prop(psmouse);
  1018. __set_bit(BTN_TOOL_QUADTAP, dev->keybit);
  1019. /* For X to recognize me as touchpad. */
  1020. input_set_abs_params(dev, ABS_X, x_min, x_max, 0, 0);
  1021. input_set_abs_params(dev, ABS_Y, y_min, y_max, 0, 0);
  1022. input_abs_set_res(dev, ABS_X, x_res);
  1023. input_abs_set_res(dev, ABS_Y, y_res);
  1024. /*
  1025. * range of pressure and width is the same as v2,
  1026. * report ABS_PRESSURE, ABS_TOOL_WIDTH for compatibility.
  1027. */
  1028. input_set_abs_params(dev, ABS_PRESSURE, ETP_PMIN_V2,
  1029. ETP_PMAX_V2, 0, 0);
  1030. input_set_abs_params(dev, ABS_TOOL_WIDTH, ETP_WMIN_V2,
  1031. ETP_WMAX_V2, 0, 0);
  1032. /* Multitouch capable pad, up to 5 fingers. */
  1033. input_mt_init_slots(dev, ETP_MAX_FINGERS, 0);
  1034. input_set_abs_params(dev, ABS_MT_POSITION_X, x_min, x_max, 0, 0);
  1035. input_set_abs_params(dev, ABS_MT_POSITION_Y, y_min, y_max, 0, 0);
  1036. input_abs_set_res(dev, ABS_MT_POSITION_X, x_res);
  1037. input_abs_set_res(dev, ABS_MT_POSITION_Y, y_res);
  1038. input_set_abs_params(dev, ABS_MT_PRESSURE, ETP_PMIN_V2,
  1039. ETP_PMAX_V2, 0, 0);
  1040. /*
  1041. * The firmware reports how many trace lines the finger spans,
  1042. * convert to surface unit as Protocol-B requires.
  1043. */
  1044. input_set_abs_params(dev, ABS_MT_TOUCH_MAJOR, 0,
  1045. ETP_WMAX_V2 * width, 0, 0);
  1046. break;
  1047. }
  1048. etd->y_max = y_max;
  1049. etd->width = width;
  1050. return 0;
  1051. }
  1052. struct elantech_attr_data {
  1053. size_t field_offset;
  1054. unsigned char reg;
  1055. };
  1056. /*
  1057. * Display a register value by reading a sysfs entry
  1058. */
  1059. static ssize_t elantech_show_int_attr(struct psmouse *psmouse, void *data,
  1060. char *buf)
  1061. {
  1062. struct elantech_data *etd = psmouse->private;
  1063. struct elantech_attr_data *attr = data;
  1064. unsigned char *reg = (unsigned char *) etd + attr->field_offset;
  1065. int rc = 0;
  1066. if (attr->reg)
  1067. rc = elantech_read_reg(psmouse, attr->reg, reg);
  1068. return sprintf(buf, "0x%02x\n", (attr->reg && rc) ? -1 : *reg);
  1069. }
  1070. /*
  1071. * Write a register value by writing a sysfs entry
  1072. */
  1073. static ssize_t elantech_set_int_attr(struct psmouse *psmouse,
  1074. void *data, const char *buf, size_t count)
  1075. {
  1076. struct elantech_data *etd = psmouse->private;
  1077. struct elantech_attr_data *attr = data;
  1078. unsigned char *reg = (unsigned char *) etd + attr->field_offset;
  1079. unsigned char value;
  1080. int err;
  1081. err = kstrtou8(buf, 16, &value);
  1082. if (err)
  1083. return err;
  1084. /* Do we need to preserve some bits for version 2 hardware too? */
  1085. if (etd->hw_version == 1) {
  1086. if (attr->reg == 0x10)
  1087. /* Force absolute mode always on */
  1088. value |= ETP_R10_ABSOLUTE_MODE;
  1089. else if (attr->reg == 0x11)
  1090. /* Force 4 byte mode always on */
  1091. value |= ETP_R11_4_BYTE_MODE;
  1092. }
  1093. if (!attr->reg || elantech_write_reg(psmouse, attr->reg, value) == 0)
  1094. *reg = value;
  1095. return count;
  1096. }
  1097. #define ELANTECH_INT_ATTR(_name, _register) \
  1098. static struct elantech_attr_data elantech_attr_##_name = { \
  1099. .field_offset = offsetof(struct elantech_data, _name), \
  1100. .reg = _register, \
  1101. }; \
  1102. PSMOUSE_DEFINE_ATTR(_name, S_IWUSR | S_IRUGO, \
  1103. &elantech_attr_##_name, \
  1104. elantech_show_int_attr, \
  1105. elantech_set_int_attr)
  1106. ELANTECH_INT_ATTR(reg_07, 0x07);
  1107. ELANTECH_INT_ATTR(reg_10, 0x10);
  1108. ELANTECH_INT_ATTR(reg_11, 0x11);
  1109. ELANTECH_INT_ATTR(reg_20, 0x20);
  1110. ELANTECH_INT_ATTR(reg_21, 0x21);
  1111. ELANTECH_INT_ATTR(reg_22, 0x22);
  1112. ELANTECH_INT_ATTR(reg_23, 0x23);
  1113. ELANTECH_INT_ATTR(reg_24, 0x24);
  1114. ELANTECH_INT_ATTR(reg_25, 0x25);
  1115. ELANTECH_INT_ATTR(reg_26, 0x26);
  1116. ELANTECH_INT_ATTR(debug, 0);
  1117. ELANTECH_INT_ATTR(paritycheck, 0);
  1118. static struct attribute *elantech_attrs[] = {
  1119. &psmouse_attr_reg_07.dattr.attr,
  1120. &psmouse_attr_reg_10.dattr.attr,
  1121. &psmouse_attr_reg_11.dattr.attr,
  1122. &psmouse_attr_reg_20.dattr.attr,
  1123. &psmouse_attr_reg_21.dattr.attr,
  1124. &psmouse_attr_reg_22.dattr.attr,
  1125. &psmouse_attr_reg_23.dattr.attr,
  1126. &psmouse_attr_reg_24.dattr.attr,
  1127. &psmouse_attr_reg_25.dattr.attr,
  1128. &psmouse_attr_reg_26.dattr.attr,
  1129. &psmouse_attr_debug.dattr.attr,
  1130. &psmouse_attr_paritycheck.dattr.attr,
  1131. NULL
  1132. };
  1133. static struct attribute_group elantech_attr_group = {
  1134. .attrs = elantech_attrs,
  1135. };
  1136. static bool elantech_is_signature_valid(const unsigned char *param)
  1137. {
  1138. static const unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10 };
  1139. int i;
  1140. if (param[0] == 0)
  1141. return false;
  1142. if (param[1] == 0)
  1143. return true;
  1144. /*
  1145. * Some models have a revision higher then 20. Meaning param[2] may
  1146. * be 10 or 20, skip the rates check for these.
  1147. */
  1148. if (param[0] == 0x46 && (param[1] & 0xef) == 0x0f && param[2] < 40)
  1149. return true;
  1150. for (i = 0; i < ARRAY_SIZE(rates); i++)
  1151. if (param[2] == rates[i])
  1152. return false;
  1153. return true;
  1154. }
  1155. /*
  1156. * Use magic knock to detect Elantech touchpad
  1157. */
  1158. int elantech_detect(struct psmouse *psmouse, bool set_properties)
  1159. {
  1160. struct ps2dev *ps2dev = &psmouse->ps2dev;
  1161. unsigned char param[3];
  1162. ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
  1163. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  1164. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  1165. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  1166. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  1167. ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) {
  1168. psmouse_dbg(psmouse, "sending Elantech magic knock failed.\n");
  1169. return -1;
  1170. }
  1171. /*
  1172. * Report this in case there are Elantech models that use a different
  1173. * set of magic numbers
  1174. */
  1175. if (param[0] != 0x3c || param[1] != 0x03 ||
  1176. (param[2] != 0xc8 && param[2] != 0x00)) {
  1177. psmouse_dbg(psmouse,
  1178. "unexpected magic knock result 0x%02x, 0x%02x, 0x%02x.\n",
  1179. param[0], param[1], param[2]);
  1180. return -1;
  1181. }
  1182. /*
  1183. * Query touchpad's firmware version and see if it reports known
  1184. * value to avoid mis-detection. Logitech mice are known to respond
  1185. * to Elantech magic knock and there might be more.
  1186. */
  1187. if (synaptics_send_cmd(psmouse, ETP_FW_VERSION_QUERY, param)) {
  1188. psmouse_dbg(psmouse, "failed to query firmware version.\n");
  1189. return -1;
  1190. }
  1191. psmouse_dbg(psmouse,
  1192. "Elantech version query result 0x%02x, 0x%02x, 0x%02x.\n",
  1193. param[0], param[1], param[2]);
  1194. if (!elantech_is_signature_valid(param)) {
  1195. psmouse_dbg(psmouse,
  1196. "Probably not a real Elantech touchpad. Aborting.\n");
  1197. return -1;
  1198. }
  1199. if (set_properties) {
  1200. psmouse->vendor = "Elantech";
  1201. psmouse->name = "Touchpad";
  1202. }
  1203. return 0;
  1204. }
  1205. /*
  1206. * Clean up sysfs entries when disconnecting
  1207. */
  1208. static void elantech_disconnect(struct psmouse *psmouse)
  1209. {
  1210. struct elantech_data *etd = psmouse->private;
  1211. if (etd->tp_dev)
  1212. input_unregister_device(etd->tp_dev);
  1213. sysfs_remove_group(&psmouse->ps2dev.serio->dev.kobj,
  1214. &elantech_attr_group);
  1215. kfree(psmouse->private);
  1216. psmouse->private = NULL;
  1217. }
  1218. /*
  1219. * Put the touchpad back into absolute mode when reconnecting
  1220. */
  1221. static int elantech_reconnect(struct psmouse *psmouse)
  1222. {
  1223. psmouse_reset(psmouse);
  1224. if (elantech_detect(psmouse, 0))
  1225. return -1;
  1226. if (elantech_set_absolute_mode(psmouse)) {
  1227. psmouse_err(psmouse,
  1228. "failed to put touchpad back into absolute mode.\n");
  1229. return -1;
  1230. }
  1231. return 0;
  1232. }
  1233. /*
  1234. * Some hw_version 3 models go into error state when we try to set
  1235. * bit 3 and/or bit 1 of r10.
  1236. */
  1237. static const struct dmi_system_id no_hw_res_dmi_table[] = {
  1238. #if defined(CONFIG_DMI) && defined(CONFIG_X86)
  1239. {
  1240. /* Gigabyte U2442 */
  1241. .matches = {
  1242. DMI_MATCH(DMI_SYS_VENDOR, "GIGABYTE"),
  1243. DMI_MATCH(DMI_PRODUCT_NAME, "U2442"),
  1244. },
  1245. },
  1246. #endif
  1247. { }
  1248. };
  1249. /*
  1250. * determine hardware version and set some properties according to it.
  1251. */
  1252. static int elantech_set_properties(struct elantech_data *etd)
  1253. {
  1254. /* This represents the version of IC body. */
  1255. int ver = (etd->fw_version & 0x0f0000) >> 16;
  1256. /* Early version of Elan touchpads doesn't obey the rule. */
  1257. if (etd->fw_version < 0x020030 || etd->fw_version == 0x020600)
  1258. etd->hw_version = 1;
  1259. else {
  1260. switch (ver) {
  1261. case 2:
  1262. case 4:
  1263. etd->hw_version = 2;
  1264. break;
  1265. case 5:
  1266. etd->hw_version = 3;
  1267. break;
  1268. case 6:
  1269. case 7:
  1270. case 8:
  1271. case 9:
  1272. etd->hw_version = 4;
  1273. break;
  1274. default:
  1275. return -1;
  1276. }
  1277. }
  1278. /* decide which send_cmd we're gonna use early */
  1279. etd->send_cmd = etd->hw_version >= 3 ? elantech_send_cmd :
  1280. synaptics_send_cmd;
  1281. /* Turn on packet checking by default */
  1282. etd->paritycheck = 1;
  1283. /*
  1284. * This firmware suffers from misreporting coordinates when
  1285. * a touch action starts causing the mouse cursor or scrolled page
  1286. * to jump. Enable a workaround.
  1287. */
  1288. etd->jumpy_cursor =
  1289. (etd->fw_version == 0x020022 || etd->fw_version == 0x020600);
  1290. if (etd->hw_version > 1) {
  1291. /* For now show extra debug information */
  1292. etd->debug = 1;
  1293. if (etd->fw_version >= 0x020800)
  1294. etd->reports_pressure = true;
  1295. }
  1296. /*
  1297. * The signatures of v3 and v4 packets change depending on the
  1298. * value of this hardware flag.
  1299. */
  1300. etd->crc_enabled = ((etd->fw_version & 0x4000) == 0x4000);
  1301. /* Enable real hardware resolution on hw_version 3 ? */
  1302. etd->set_hw_resolution = !dmi_check_system(no_hw_res_dmi_table);
  1303. return 0;
  1304. }
  1305. /*
  1306. * Initialize the touchpad and create sysfs entries
  1307. */
  1308. int elantech_init(struct psmouse *psmouse)
  1309. {
  1310. struct elantech_data *etd;
  1311. int i;
  1312. int error = -EINVAL;
  1313. unsigned char param[3];
  1314. struct input_dev *tp_dev;
  1315. psmouse->private = etd = kzalloc(sizeof(struct elantech_data), GFP_KERNEL);
  1316. if (!etd)
  1317. return -ENOMEM;
  1318. psmouse_reset(psmouse);
  1319. etd->parity[0] = 1;
  1320. for (i = 1; i < 256; i++)
  1321. etd->parity[i] = etd->parity[i & (i - 1)] ^ 1;
  1322. /*
  1323. * Do the version query again so we can store the result
  1324. */
  1325. if (synaptics_send_cmd(psmouse, ETP_FW_VERSION_QUERY, param)) {
  1326. psmouse_err(psmouse, "failed to query firmware version.\n");
  1327. goto init_fail;
  1328. }
  1329. etd->fw_version = (param[0] << 16) | (param[1] << 8) | param[2];
  1330. if (elantech_set_properties(etd)) {
  1331. psmouse_err(psmouse, "unknown hardware version, aborting...\n");
  1332. goto init_fail;
  1333. }
  1334. psmouse_info(psmouse,
  1335. "assuming hardware version %d (with firmware version 0x%02x%02x%02x)\n",
  1336. etd->hw_version, param[0], param[1], param[2]);
  1337. if (etd->send_cmd(psmouse, ETP_CAPABILITIES_QUERY,
  1338. etd->capabilities)) {
  1339. psmouse_err(psmouse, "failed to query capabilities.\n");
  1340. goto init_fail;
  1341. }
  1342. psmouse_info(psmouse,
  1343. "Synaptics capabilities query result 0x%02x, 0x%02x, 0x%02x.\n",
  1344. etd->capabilities[0], etd->capabilities[1],
  1345. etd->capabilities[2]);
  1346. if (elantech_set_absolute_mode(psmouse)) {
  1347. psmouse_err(psmouse,
  1348. "failed to put touchpad into absolute mode.\n");
  1349. goto init_fail;
  1350. }
  1351. if (elantech_set_input_params(psmouse)) {
  1352. psmouse_err(psmouse, "failed to query touchpad range.\n");
  1353. goto init_fail;
  1354. }
  1355. error = sysfs_create_group(&psmouse->ps2dev.serio->dev.kobj,
  1356. &elantech_attr_group);
  1357. if (error) {
  1358. psmouse_err(psmouse,
  1359. "failed to create sysfs attributes, error: %d.\n",
  1360. error);
  1361. goto init_fail;
  1362. }
  1363. /* The MSB indicates the presence of the trackpoint */
  1364. if ((etd->capabilities[0] & 0x80) == 0x80) {
  1365. tp_dev = input_allocate_device();
  1366. if (!tp_dev) {
  1367. error = -ENOMEM;
  1368. goto init_fail_tp_alloc;
  1369. }
  1370. etd->tp_dev = tp_dev;
  1371. snprintf(etd->tp_phys, sizeof(etd->tp_phys), "%s/input1",
  1372. psmouse->ps2dev.serio->phys);
  1373. tp_dev->phys = etd->tp_phys;
  1374. tp_dev->name = "Elantech PS/2 TrackPoint";
  1375. tp_dev->id.bustype = BUS_I8042;
  1376. tp_dev->id.vendor = 0x0002;
  1377. tp_dev->id.product = PSMOUSE_ELANTECH;
  1378. tp_dev->id.version = 0x0000;
  1379. tp_dev->dev.parent = &psmouse->ps2dev.serio->dev;
  1380. tp_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
  1381. tp_dev->relbit[BIT_WORD(REL_X)] =
  1382. BIT_MASK(REL_X) | BIT_MASK(REL_Y);
  1383. tp_dev->keybit[BIT_WORD(BTN_LEFT)] =
  1384. BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_MIDDLE) |
  1385. BIT_MASK(BTN_RIGHT);
  1386. __set_bit(INPUT_PROP_POINTER, tp_dev->propbit);
  1387. __set_bit(INPUT_PROP_POINTING_STICK, tp_dev->propbit);
  1388. error = input_register_device(etd->tp_dev);
  1389. if (error < 0)
  1390. goto init_fail_tp_reg;
  1391. }
  1392. psmouse->protocol_handler = elantech_process_byte;
  1393. psmouse->disconnect = elantech_disconnect;
  1394. psmouse->reconnect = elantech_reconnect;
  1395. psmouse->pktsize = etd->hw_version > 1 ? 6 : 4;
  1396. return 0;
  1397. init_fail_tp_reg:
  1398. input_free_device(tp_dev);
  1399. init_fail_tp_alloc:
  1400. sysfs_remove_group(&psmouse->ps2dev.serio->dev.kobj,
  1401. &elantech_attr_group);
  1402. init_fail:
  1403. psmouse_reset(psmouse);
  1404. kfree(etd);
  1405. return error;
  1406. }