elantech.c 60 KB

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