elantech.c 45 KB

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