elantech.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Elantech Touchpad driver (v6)
  4. *
  5. * Copyright (C) 2007-2009 Arjan Opmeer <[email protected]>
  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 <asm/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. * Put the touchpad back into absolute mode when reconnecting
  1276. */
  1277. static int elantech_reconnect(struct psmouse *psmouse)
  1278. {
  1279. psmouse_reset(psmouse);
  1280. if (elantech_detect(psmouse, 0))
  1281. return -1;
  1282. if (elantech_set_absolute_mode(psmouse)) {
  1283. psmouse_err(psmouse,
  1284. "failed to put touchpad back into absolute mode.\n");
  1285. return -1;
  1286. }
  1287. return 0;
  1288. }
  1289. /*
  1290. * Some hw_version 4 models do not work with crc_disabled
  1291. */
  1292. static const struct dmi_system_id elantech_dmi_force_crc_enabled[] = {
  1293. #if defined(CONFIG_DMI) && defined(CONFIG_X86)
  1294. {
  1295. /* Fujitsu H730 does not work with crc_enabled == 0 */
  1296. .matches = {
  1297. DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"),
  1298. DMI_MATCH(DMI_PRODUCT_NAME, "CELSIUS H730"),
  1299. },
  1300. },
  1301. {
  1302. /* Fujitsu H760 does not work with crc_enabled == 0 */
  1303. .matches = {
  1304. DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"),
  1305. DMI_MATCH(DMI_PRODUCT_NAME, "CELSIUS H760"),
  1306. },
  1307. },
  1308. {
  1309. /* Fujitsu LIFEBOOK E544 does not work with crc_enabled == 0 */
  1310. .matches = {
  1311. DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"),
  1312. DMI_MATCH(DMI_PRODUCT_NAME, "LIFEBOOK E544"),
  1313. },
  1314. },
  1315. {
  1316. /* Fujitsu LIFEBOOK E546 does not work with crc_enabled == 0 */
  1317. .matches = {
  1318. DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"),
  1319. DMI_MATCH(DMI_PRODUCT_NAME, "LIFEBOOK E546"),
  1320. },
  1321. },
  1322. {
  1323. /* Fujitsu LIFEBOOK E547 does not work with crc_enabled == 0 */
  1324. .matches = {
  1325. DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"),
  1326. DMI_MATCH(DMI_PRODUCT_NAME, "LIFEBOOK E547"),
  1327. },
  1328. },
  1329. {
  1330. /* Fujitsu LIFEBOOK E554 does not work with crc_enabled == 0 */
  1331. .matches = {
  1332. DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"),
  1333. DMI_MATCH(DMI_PRODUCT_NAME, "LIFEBOOK E554"),
  1334. },
  1335. },
  1336. {
  1337. /* Fujitsu LIFEBOOK E556 does not work with crc_enabled == 0 */
  1338. .matches = {
  1339. DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"),
  1340. DMI_MATCH(DMI_PRODUCT_NAME, "LIFEBOOK E556"),
  1341. },
  1342. },
  1343. {
  1344. /* Fujitsu LIFEBOOK E557 does not work with crc_enabled == 0 */
  1345. .matches = {
  1346. DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"),
  1347. DMI_MATCH(DMI_PRODUCT_NAME, "LIFEBOOK E557"),
  1348. },
  1349. },
  1350. {
  1351. /* Fujitsu LIFEBOOK U745 does not work with crc_enabled == 0 */
  1352. .matches = {
  1353. DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"),
  1354. DMI_MATCH(DMI_PRODUCT_NAME, "LIFEBOOK U745"),
  1355. },
  1356. },
  1357. #endif
  1358. { }
  1359. };
  1360. /*
  1361. * Some hw_version 3 models go into error state when we try to set
  1362. * bit 3 and/or bit 1 of r10.
  1363. */
  1364. static const struct dmi_system_id no_hw_res_dmi_table[] = {
  1365. #if defined(CONFIG_DMI) && defined(CONFIG_X86)
  1366. {
  1367. /* Gigabyte U2442 */
  1368. .matches = {
  1369. DMI_MATCH(DMI_SYS_VENDOR, "GIGABYTE"),
  1370. DMI_MATCH(DMI_PRODUCT_NAME, "U2442"),
  1371. },
  1372. },
  1373. #endif
  1374. { }
  1375. };
  1376. /*
  1377. * Change Report id 0x5E to 0x5F.
  1378. */
  1379. static int elantech_change_report_id(struct psmouse *psmouse)
  1380. {
  1381. /*
  1382. * NOTE: the code is expecting to receive param[] as an array of 3
  1383. * items (see __ps2_command()), even if in this case only 2 are
  1384. * actually needed. Make sure the array size is 3 to avoid potential
  1385. * stack out-of-bound accesses.
  1386. */
  1387. unsigned char param[3] = { 0x10, 0x03 };
  1388. if (elantech_write_reg_params(psmouse, 0x7, param) ||
  1389. elantech_read_reg_params(psmouse, 0x7, param) ||
  1390. param[0] != 0x10 || param[1] != 0x03) {
  1391. psmouse_err(psmouse, "Unable to change report ID to 0x5f.\n");
  1392. return -EIO;
  1393. }
  1394. return 0;
  1395. }
  1396. /*
  1397. * determine hardware version and set some properties according to it.
  1398. */
  1399. static int elantech_set_properties(struct elantech_device_info *info)
  1400. {
  1401. /* This represents the version of IC body. */
  1402. info->ic_version = (info->fw_version & 0x0f0000) >> 16;
  1403. /* Early version of Elan touchpads doesn't obey the rule. */
  1404. if (info->fw_version < 0x020030 || info->fw_version == 0x020600)
  1405. info->hw_version = 1;
  1406. else {
  1407. switch (info->ic_version) {
  1408. case 2:
  1409. case 4:
  1410. info->hw_version = 2;
  1411. break;
  1412. case 5:
  1413. info->hw_version = 3;
  1414. break;
  1415. case 6 ... 15:
  1416. info->hw_version = 4;
  1417. break;
  1418. default:
  1419. return -1;
  1420. }
  1421. }
  1422. /* Get information pattern for hw_version 4 */
  1423. info->pattern = 0x00;
  1424. if (info->ic_version == 0x0f && (info->fw_version & 0xff) <= 0x02)
  1425. info->pattern = info->fw_version & 0xff;
  1426. /* decide which send_cmd we're gonna use early */
  1427. info->send_cmd = info->hw_version >= 3 ? elantech_send_cmd :
  1428. synaptics_send_cmd;
  1429. /* Turn on packet checking by default */
  1430. info->paritycheck = 1;
  1431. /*
  1432. * This firmware suffers from misreporting coordinates when
  1433. * a touch action starts causing the mouse cursor or scrolled page
  1434. * to jump. Enable a workaround.
  1435. */
  1436. info->jumpy_cursor =
  1437. (info->fw_version == 0x020022 || info->fw_version == 0x020600);
  1438. if (info->hw_version > 1) {
  1439. /* For now show extra debug information */
  1440. info->debug = 1;
  1441. if (info->fw_version >= 0x020800)
  1442. info->reports_pressure = true;
  1443. }
  1444. /*
  1445. * The signatures of v3 and v4 packets change depending on the
  1446. * value of this hardware flag.
  1447. */
  1448. info->crc_enabled = (info->fw_version & 0x4000) == 0x4000 ||
  1449. dmi_check_system(elantech_dmi_force_crc_enabled);
  1450. /* Enable real hardware resolution on hw_version 3 ? */
  1451. info->set_hw_resolution = !dmi_check_system(no_hw_res_dmi_table);
  1452. return 0;
  1453. }
  1454. static int elantech_query_info(struct psmouse *psmouse,
  1455. struct elantech_device_info *info)
  1456. {
  1457. unsigned char param[3];
  1458. unsigned char traces;
  1459. unsigned char ic_body[3];
  1460. memset(info, 0, sizeof(*info));
  1461. /*
  1462. * Do the version query again so we can store the result
  1463. */
  1464. if (synaptics_send_cmd(psmouse, ETP_FW_VERSION_QUERY, param)) {
  1465. psmouse_err(psmouse, "failed to query firmware version.\n");
  1466. return -EINVAL;
  1467. }
  1468. info->fw_version = (param[0] << 16) | (param[1] << 8) | param[2];
  1469. if (elantech_set_properties(info)) {
  1470. psmouse_err(psmouse, "unknown hardware version, aborting...\n");
  1471. return -EINVAL;
  1472. }
  1473. psmouse_info(psmouse,
  1474. "assuming hardware version %d (with firmware version 0x%02x%02x%02x)\n",
  1475. info->hw_version, param[0], param[1], param[2]);
  1476. if (info->send_cmd(psmouse, ETP_CAPABILITIES_QUERY,
  1477. info->capabilities)) {
  1478. psmouse_err(psmouse, "failed to query capabilities.\n");
  1479. return -EINVAL;
  1480. }
  1481. psmouse_info(psmouse,
  1482. "Synaptics capabilities query result 0x%02x, 0x%02x, 0x%02x.\n",
  1483. info->capabilities[0], info->capabilities[1],
  1484. info->capabilities[2]);
  1485. if (info->hw_version != 1) {
  1486. if (info->send_cmd(psmouse, ETP_SAMPLE_QUERY, info->samples)) {
  1487. psmouse_err(psmouse, "failed to query sample data\n");
  1488. return -EINVAL;
  1489. }
  1490. psmouse_info(psmouse,
  1491. "Elan sample query result %02x, %02x, %02x\n",
  1492. info->samples[0],
  1493. info->samples[1],
  1494. info->samples[2]);
  1495. }
  1496. if (info->pattern > 0x00 && info->ic_version == 0xf) {
  1497. if (info->send_cmd(psmouse, ETP_ICBODY_QUERY, ic_body)) {
  1498. psmouse_err(psmouse, "failed to query ic body\n");
  1499. return -EINVAL;
  1500. }
  1501. info->ic_version = be16_to_cpup((__be16 *)ic_body);
  1502. psmouse_info(psmouse,
  1503. "Elan ic body: %#04x, current fw version: %#02x\n",
  1504. info->ic_version, ic_body[2]);
  1505. }
  1506. info->product_id = be16_to_cpup((__be16 *)info->samples);
  1507. if (info->pattern == 0x00)
  1508. info->product_id &= 0xff;
  1509. if (info->samples[1] == 0x74 && info->hw_version == 0x03) {
  1510. /*
  1511. * This module has a bug which makes absolute mode
  1512. * unusable, so let's abort so we'll be using standard
  1513. * PS/2 protocol.
  1514. */
  1515. psmouse_info(psmouse,
  1516. "absolute mode broken, forcing standard PS/2 protocol\n");
  1517. return -ENODEV;
  1518. }
  1519. /* The MSB indicates the presence of the trackpoint */
  1520. info->has_trackpoint = (info->capabilities[0] & 0x80) == 0x80;
  1521. if (info->has_trackpoint && info->ic_version == 0x0011 &&
  1522. (info->product_id == 0x08 || info->product_id == 0x09 ||
  1523. info->product_id == 0x0d || info->product_id == 0x0e)) {
  1524. /*
  1525. * This module has a bug which makes trackpoint in SMBus
  1526. * mode return invalid data unless trackpoint is switched
  1527. * from using 0x5e reports to 0x5f. If we are not able to
  1528. * make the switch, let's abort initialization so we'll be
  1529. * using standard PS/2 protocol.
  1530. */
  1531. if (elantech_change_report_id(psmouse)) {
  1532. psmouse_info(psmouse,
  1533. "Trackpoint report is broken, forcing standard PS/2 protocol\n");
  1534. return -ENODEV;
  1535. }
  1536. }
  1537. info->x_res = 31;
  1538. info->y_res = 31;
  1539. if (info->hw_version == 4) {
  1540. if (elantech_get_resolution_v4(psmouse,
  1541. &info->x_res,
  1542. &info->y_res,
  1543. &info->bus)) {
  1544. psmouse_warn(psmouse,
  1545. "failed to query resolution data.\n");
  1546. }
  1547. }
  1548. /* query range information */
  1549. switch (info->hw_version) {
  1550. case 1:
  1551. info->x_min = ETP_XMIN_V1;
  1552. info->y_min = ETP_YMIN_V1;
  1553. info->x_max = ETP_XMAX_V1;
  1554. info->y_max = ETP_YMAX_V1;
  1555. break;
  1556. case 2:
  1557. if (info->fw_version == 0x020800 ||
  1558. info->fw_version == 0x020b00 ||
  1559. info->fw_version == 0x020030) {
  1560. info->x_min = ETP_XMIN_V2;
  1561. info->y_min = ETP_YMIN_V2;
  1562. info->x_max = ETP_XMAX_V2;
  1563. info->y_max = ETP_YMAX_V2;
  1564. } else {
  1565. int i;
  1566. int fixed_dpi;
  1567. i = (info->fw_version > 0x020800 &&
  1568. info->fw_version < 0x020900) ? 1 : 2;
  1569. if (info->send_cmd(psmouse, ETP_FW_ID_QUERY, param))
  1570. return -EINVAL;
  1571. fixed_dpi = param[1] & 0x10;
  1572. if (((info->fw_version >> 16) == 0x14) && fixed_dpi) {
  1573. if (info->send_cmd(psmouse, ETP_SAMPLE_QUERY, param))
  1574. return -EINVAL;
  1575. info->x_max = (info->capabilities[1] - i) * param[1] / 2;
  1576. info->y_max = (info->capabilities[2] - i) * param[2] / 2;
  1577. } else if (info->fw_version == 0x040216) {
  1578. info->x_max = 819;
  1579. info->y_max = 405;
  1580. } else if (info->fw_version == 0x040219 || info->fw_version == 0x040215) {
  1581. info->x_max = 900;
  1582. info->y_max = 500;
  1583. } else {
  1584. info->x_max = (info->capabilities[1] - i) * 64;
  1585. info->y_max = (info->capabilities[2] - i) * 64;
  1586. }
  1587. }
  1588. break;
  1589. case 3:
  1590. if (info->send_cmd(psmouse, ETP_FW_ID_QUERY, param))
  1591. return -EINVAL;
  1592. info->x_max = (0x0f & param[0]) << 8 | param[1];
  1593. info->y_max = (0xf0 & param[0]) << 4 | param[2];
  1594. break;
  1595. case 4:
  1596. if (info->send_cmd(psmouse, ETP_FW_ID_QUERY, param))
  1597. return -EINVAL;
  1598. info->x_max = (0x0f & param[0]) << 8 | param[1];
  1599. info->y_max = (0xf0 & param[0]) << 4 | param[2];
  1600. traces = info->capabilities[1];
  1601. if ((traces < 2) || (traces > info->x_max))
  1602. return -EINVAL;
  1603. info->width = info->x_max / (traces - 1);
  1604. /* column number of traces */
  1605. info->x_traces = traces;
  1606. /* row number of traces */
  1607. traces = info->capabilities[2];
  1608. if ((traces >= 2) && (traces <= info->y_max))
  1609. info->y_traces = traces;
  1610. break;
  1611. }
  1612. /* check for the middle button: DMI matching or new v4 firmwares */
  1613. info->has_middle_button = dmi_check_system(elantech_dmi_has_middle_button) ||
  1614. (ETP_NEW_IC_SMBUS_HOST_NOTIFY(info->fw_version) &&
  1615. !elantech_is_buttonpad(info));
  1616. return 0;
  1617. }
  1618. #if defined(CONFIG_MOUSE_PS2_ELANTECH_SMBUS)
  1619. /*
  1620. * The newest Elantech device can use a secondary bus (over SMBus) which
  1621. * provides a better bandwidth and allow a better control of the touchpads.
  1622. * This is used to decide if we need to use this bus or not.
  1623. */
  1624. enum {
  1625. ELANTECH_SMBUS_NOT_SET = -1,
  1626. ELANTECH_SMBUS_OFF,
  1627. ELANTECH_SMBUS_ON,
  1628. };
  1629. static int elantech_smbus = IS_ENABLED(CONFIG_MOUSE_ELAN_I2C_SMBUS) ?
  1630. ELANTECH_SMBUS_NOT_SET : ELANTECH_SMBUS_OFF;
  1631. module_param_named(elantech_smbus, elantech_smbus, int, 0644);
  1632. MODULE_PARM_DESC(elantech_smbus, "Use a secondary bus for the Elantech device.");
  1633. static const char * const i2c_blacklist_pnp_ids[] = {
  1634. /*
  1635. * These are known to not be working properly as bits are missing
  1636. * in elan_i2c.
  1637. */
  1638. NULL
  1639. };
  1640. static int elantech_create_smbus(struct psmouse *psmouse,
  1641. struct elantech_device_info *info,
  1642. bool leave_breadcrumbs)
  1643. {
  1644. struct property_entry i2c_props[11] = {};
  1645. struct i2c_board_info smbus_board = {
  1646. I2C_BOARD_INFO("elan_i2c", 0x15),
  1647. .flags = I2C_CLIENT_HOST_NOTIFY,
  1648. };
  1649. unsigned int idx = 0;
  1650. i2c_props[idx++] = PROPERTY_ENTRY_U32("touchscreen-size-x",
  1651. info->x_max + 1);
  1652. i2c_props[idx++] = PROPERTY_ENTRY_U32("touchscreen-size-y",
  1653. info->y_max + 1);
  1654. i2c_props[idx++] = PROPERTY_ENTRY_U32("touchscreen-min-x",
  1655. info->x_min);
  1656. i2c_props[idx++] = PROPERTY_ENTRY_U32("touchscreen-min-y",
  1657. info->y_min);
  1658. if (info->x_res)
  1659. i2c_props[idx++] = PROPERTY_ENTRY_U32("touchscreen-x-mm",
  1660. (info->x_max + 1) / info->x_res);
  1661. if (info->y_res)
  1662. i2c_props[idx++] = PROPERTY_ENTRY_U32("touchscreen-y-mm",
  1663. (info->y_max + 1) / info->y_res);
  1664. if (info->has_trackpoint)
  1665. i2c_props[idx++] = PROPERTY_ENTRY_BOOL("elan,trackpoint");
  1666. if (info->has_middle_button)
  1667. i2c_props[idx++] = PROPERTY_ENTRY_BOOL("elan,middle-button");
  1668. if (info->x_traces)
  1669. i2c_props[idx++] = PROPERTY_ENTRY_U32("elan,x_traces",
  1670. info->x_traces);
  1671. if (info->y_traces)
  1672. i2c_props[idx++] = PROPERTY_ENTRY_U32("elan,y_traces",
  1673. info->y_traces);
  1674. if (elantech_is_buttonpad(info))
  1675. i2c_props[idx++] = PROPERTY_ENTRY_BOOL("elan,clickpad");
  1676. smbus_board.fwnode = fwnode_create_software_node(i2c_props, NULL);
  1677. if (IS_ERR(smbus_board.fwnode))
  1678. return PTR_ERR(smbus_board.fwnode);
  1679. return psmouse_smbus_init(psmouse, &smbus_board, NULL, 0, false,
  1680. leave_breadcrumbs);
  1681. }
  1682. /*
  1683. * elantech_setup_smbus - called once the PS/2 devices are enumerated
  1684. * and decides to instantiate a SMBus InterTouch device.
  1685. */
  1686. static int elantech_setup_smbus(struct psmouse *psmouse,
  1687. struct elantech_device_info *info,
  1688. bool leave_breadcrumbs)
  1689. {
  1690. int error;
  1691. if (elantech_smbus == ELANTECH_SMBUS_OFF)
  1692. return -ENXIO;
  1693. if (elantech_smbus == ELANTECH_SMBUS_NOT_SET) {
  1694. /*
  1695. * New ICs are enabled by default, unless mentioned in
  1696. * i2c_blacklist_pnp_ids.
  1697. * Old ICs are up to the user to decide.
  1698. */
  1699. if (!ETP_NEW_IC_SMBUS_HOST_NOTIFY(info->fw_version) ||
  1700. psmouse_matches_pnp_id(psmouse, i2c_blacklist_pnp_ids))
  1701. return -ENXIO;
  1702. }
  1703. psmouse_info(psmouse, "Trying to set up SMBus access\n");
  1704. error = elantech_create_smbus(psmouse, info, leave_breadcrumbs);
  1705. if (error) {
  1706. if (error == -EAGAIN)
  1707. psmouse_info(psmouse, "SMbus companion is not ready yet\n");
  1708. else
  1709. psmouse_err(psmouse, "unable to create intertouch device\n");
  1710. return error;
  1711. }
  1712. return 0;
  1713. }
  1714. static bool elantech_use_host_notify(struct psmouse *psmouse,
  1715. struct elantech_device_info *info)
  1716. {
  1717. if (ETP_NEW_IC_SMBUS_HOST_NOTIFY(info->fw_version))
  1718. return true;
  1719. switch (info->bus) {
  1720. case ETP_BUS_PS2_ONLY:
  1721. /* expected case */
  1722. break;
  1723. case ETP_BUS_SMB_ALERT_ONLY:
  1724. case ETP_BUS_PS2_SMB_ALERT:
  1725. psmouse_dbg(psmouse, "Ignoring SMBus provider through alert protocol.\n");
  1726. break;
  1727. case ETP_BUS_SMB_HST_NTFY_ONLY:
  1728. case ETP_BUS_PS2_SMB_HST_NTFY:
  1729. return true;
  1730. default:
  1731. psmouse_dbg(psmouse,
  1732. "Ignoring SMBus bus provider %d.\n",
  1733. info->bus);
  1734. }
  1735. return false;
  1736. }
  1737. int elantech_init_smbus(struct psmouse *psmouse)
  1738. {
  1739. struct elantech_device_info info;
  1740. int error;
  1741. psmouse_reset(psmouse);
  1742. error = elantech_query_info(psmouse, &info);
  1743. if (error)
  1744. goto init_fail;
  1745. if (info.hw_version < 4) {
  1746. error = -ENXIO;
  1747. goto init_fail;
  1748. }
  1749. return elantech_create_smbus(psmouse, &info, false);
  1750. init_fail:
  1751. psmouse_reset(psmouse);
  1752. return error;
  1753. }
  1754. #endif /* CONFIG_MOUSE_PS2_ELANTECH_SMBUS */
  1755. /*
  1756. * Initialize the touchpad and create sysfs entries
  1757. */
  1758. static int elantech_setup_ps2(struct psmouse *psmouse,
  1759. struct elantech_device_info *info)
  1760. {
  1761. struct elantech_data *etd;
  1762. int i;
  1763. int error = -EINVAL;
  1764. struct input_dev *tp_dev;
  1765. psmouse->private = etd = kzalloc(sizeof(*etd), GFP_KERNEL);
  1766. if (!etd)
  1767. return -ENOMEM;
  1768. etd->info = *info;
  1769. etd->parity[0] = 1;
  1770. for (i = 1; i < 256; i++)
  1771. etd->parity[i] = etd->parity[i & (i - 1)] ^ 1;
  1772. if (elantech_set_absolute_mode(psmouse)) {
  1773. psmouse_err(psmouse,
  1774. "failed to put touchpad into absolute mode.\n");
  1775. goto init_fail;
  1776. }
  1777. if (info->fw_version == 0x381f17) {
  1778. etd->original_set_rate = psmouse->set_rate;
  1779. psmouse->set_rate = elantech_set_rate_restore_reg_07;
  1780. }
  1781. if (elantech_set_input_params(psmouse)) {
  1782. psmouse_err(psmouse, "failed to query touchpad range.\n");
  1783. goto init_fail;
  1784. }
  1785. error = sysfs_create_group(&psmouse->ps2dev.serio->dev.kobj,
  1786. &elantech_attr_group);
  1787. if (error) {
  1788. psmouse_err(psmouse,
  1789. "failed to create sysfs attributes, error: %d.\n",
  1790. error);
  1791. goto init_fail;
  1792. }
  1793. if (info->has_trackpoint) {
  1794. tp_dev = input_allocate_device();
  1795. if (!tp_dev) {
  1796. error = -ENOMEM;
  1797. goto init_fail_tp_alloc;
  1798. }
  1799. etd->tp_dev = tp_dev;
  1800. snprintf(etd->tp_phys, sizeof(etd->tp_phys), "%s/input1",
  1801. psmouse->ps2dev.serio->phys);
  1802. tp_dev->phys = etd->tp_phys;
  1803. tp_dev->name = "ETPS/2 Elantech TrackPoint";
  1804. tp_dev->id.bustype = BUS_I8042;
  1805. tp_dev->id.vendor = 0x0002;
  1806. tp_dev->id.product = PSMOUSE_ELANTECH;
  1807. tp_dev->id.version = 0x0000;
  1808. tp_dev->dev.parent = &psmouse->ps2dev.serio->dev;
  1809. tp_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
  1810. tp_dev->relbit[BIT_WORD(REL_X)] =
  1811. BIT_MASK(REL_X) | BIT_MASK(REL_Y);
  1812. tp_dev->keybit[BIT_WORD(BTN_LEFT)] =
  1813. BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_MIDDLE) |
  1814. BIT_MASK(BTN_RIGHT);
  1815. __set_bit(INPUT_PROP_POINTER, tp_dev->propbit);
  1816. __set_bit(INPUT_PROP_POINTING_STICK, tp_dev->propbit);
  1817. error = input_register_device(etd->tp_dev);
  1818. if (error < 0)
  1819. goto init_fail_tp_reg;
  1820. }
  1821. psmouse->protocol_handler = elantech_process_byte;
  1822. psmouse->disconnect = elantech_disconnect;
  1823. psmouse->reconnect = elantech_reconnect;
  1824. psmouse->fast_reconnect = NULL;
  1825. psmouse->pktsize = info->hw_version > 1 ? 6 : 4;
  1826. return 0;
  1827. init_fail_tp_reg:
  1828. input_free_device(tp_dev);
  1829. init_fail_tp_alloc:
  1830. sysfs_remove_group(&psmouse->ps2dev.serio->dev.kobj,
  1831. &elantech_attr_group);
  1832. init_fail:
  1833. kfree(etd);
  1834. return error;
  1835. }
  1836. int elantech_init_ps2(struct psmouse *psmouse)
  1837. {
  1838. struct elantech_device_info info;
  1839. int error;
  1840. psmouse_reset(psmouse);
  1841. error = elantech_query_info(psmouse, &info);
  1842. if (error)
  1843. goto init_fail;
  1844. error = elantech_setup_ps2(psmouse, &info);
  1845. if (error)
  1846. goto init_fail;
  1847. return 0;
  1848. init_fail:
  1849. psmouse_reset(psmouse);
  1850. return error;
  1851. }
  1852. int elantech_init(struct psmouse *psmouse)
  1853. {
  1854. struct elantech_device_info info;
  1855. int error;
  1856. psmouse_reset(psmouse);
  1857. error = elantech_query_info(psmouse, &info);
  1858. if (error)
  1859. goto init_fail;
  1860. #if defined(CONFIG_MOUSE_PS2_ELANTECH_SMBUS)
  1861. if (elantech_use_host_notify(psmouse, &info)) {
  1862. if (!IS_ENABLED(CONFIG_MOUSE_ELAN_I2C_SMBUS) ||
  1863. !IS_ENABLED(CONFIG_MOUSE_PS2_ELANTECH_SMBUS)) {
  1864. psmouse_warn(psmouse,
  1865. "The touchpad can support a better bus than the too old PS/2 protocol. "
  1866. "Make sure MOUSE_PS2_ELANTECH_SMBUS and MOUSE_ELAN_I2C_SMBUS are enabled to get a better touchpad experience.\n");
  1867. }
  1868. error = elantech_setup_smbus(psmouse, &info, true);
  1869. if (!error)
  1870. return PSMOUSE_ELANTECH_SMBUS;
  1871. }
  1872. #endif /* CONFIG_MOUSE_PS2_ELANTECH_SMBUS */
  1873. error = elantech_setup_ps2(psmouse, &info);
  1874. if (error < 0) {
  1875. /*
  1876. * Not using any flavor of Elantech support, so clean up
  1877. * SMbus breadcrumbs, if any.
  1878. */
  1879. psmouse_smbus_cleanup(psmouse);
  1880. goto init_fail;
  1881. }
  1882. return PSMOUSE_ELANTECH;
  1883. init_fail:
  1884. psmouse_reset(psmouse);
  1885. return error;
  1886. }