elantech.c 34 KB

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