elantech.c 40 KB

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