elantech.c 41 KB

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