elantech.c 54 KB

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