hgpk.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * OLPC HGPK (XO-1) touchpad PS/2 mouse driver
  4. *
  5. * Copyright (c) 2006-2008 One Laptop Per Child
  6. * Authors:
  7. * Zephaniah E. Hull
  8. * Andres Salomon <dilinger@debian.org>
  9. *
  10. * This driver is partly based on the ALPS driver, which is:
  11. *
  12. * Copyright (c) 2003 Neil Brown <neilb@cse.unsw.edu.au>
  13. * Copyright (c) 2003-2005 Peter Osterlund <petero2@telia.com>
  14. * Copyright (c) 2004 Dmitry Torokhov <dtor@mail.ru>
  15. * Copyright (c) 2005 Vojtech Pavlik <vojtech@suse.cz>
  16. */
  17. /*
  18. * The spec from ALPS is available from
  19. * <http://wiki.laptop.org/go/Touch_Pad/Tablet>. It refers to this
  20. * device as HGPK (Hybrid GS, PT, and Keymatrix).
  21. *
  22. * The earliest versions of the device had simultaneous reporting; that
  23. * was removed. After that, the device used the Advanced Mode GS/PT streaming
  24. * stuff. That turned out to be too buggy to support, so we've finally
  25. * switched to Mouse Mode (which utilizes only the center 1/3 of the touchpad).
  26. */
  27. #define DEBUG
  28. #include <linux/slab.h>
  29. #include <linux/input.h>
  30. #include <linux/module.h>
  31. #include <linux/serio.h>
  32. #include <linux/libps2.h>
  33. #include <linux/delay.h>
  34. #include <asm/olpc.h>
  35. #include "psmouse.h"
  36. #include "hgpk.h"
  37. #define ILLEGAL_XY 999999
  38. static bool tpdebug;
  39. module_param(tpdebug, bool, 0644);
  40. MODULE_PARM_DESC(tpdebug, "enable debugging, dumping packets to KERN_DEBUG.");
  41. static int recalib_delta = 100;
  42. module_param(recalib_delta, int, 0644);
  43. MODULE_PARM_DESC(recalib_delta,
  44. "packets containing a delta this large will be discarded, and a "
  45. "recalibration may be scheduled.");
  46. static int jumpy_delay = 20;
  47. module_param(jumpy_delay, int, 0644);
  48. MODULE_PARM_DESC(jumpy_delay,
  49. "delay (ms) before recal after jumpiness detected");
  50. static int spew_delay = 1;
  51. module_param(spew_delay, int, 0644);
  52. MODULE_PARM_DESC(spew_delay,
  53. "delay (ms) before recal after packet spew detected");
  54. static int recal_guard_time;
  55. module_param(recal_guard_time, int, 0644);
  56. MODULE_PARM_DESC(recal_guard_time,
  57. "interval (ms) during which recal will be restarted if packet received");
  58. static int post_interrupt_delay = 40;
  59. module_param(post_interrupt_delay, int, 0644);
  60. MODULE_PARM_DESC(post_interrupt_delay,
  61. "delay (ms) before recal after recal interrupt detected");
  62. static bool autorecal = true;
  63. module_param(autorecal, bool, 0644);
  64. MODULE_PARM_DESC(autorecal, "enable recalibration in the driver");
  65. static char hgpk_mode_name[16];
  66. module_param_string(hgpk_mode, hgpk_mode_name, sizeof(hgpk_mode_name), 0644);
  67. MODULE_PARM_DESC(hgpk_mode,
  68. "default hgpk mode: mouse, glidesensor or pentablet");
  69. static int hgpk_default_mode = HGPK_MODE_MOUSE;
  70. static const char * const hgpk_mode_names[] = {
  71. [HGPK_MODE_MOUSE] = "Mouse",
  72. [HGPK_MODE_GLIDESENSOR] = "GlideSensor",
  73. [HGPK_MODE_PENTABLET] = "PenTablet",
  74. };
  75. static int hgpk_mode_from_name(const char *buf, int len)
  76. {
  77. int i;
  78. for (i = 0; i < ARRAY_SIZE(hgpk_mode_names); i++) {
  79. const char *name = hgpk_mode_names[i];
  80. if (strlen(name) == len && !strncasecmp(name, buf, len))
  81. return i;
  82. }
  83. return HGPK_MODE_INVALID;
  84. }
  85. /*
  86. * see if new value is within 20% of half of old value
  87. */
  88. static int approx_half(int curr, int prev)
  89. {
  90. int belowhalf, abovehalf;
  91. if (curr < 5 || prev < 5)
  92. return 0;
  93. belowhalf = (prev * 8) / 20;
  94. abovehalf = (prev * 12) / 20;
  95. return belowhalf < curr && curr <= abovehalf;
  96. }
  97. /*
  98. * Throw out oddly large delta packets, and any that immediately follow whose
  99. * values are each approximately half of the previous. It seems that the ALPS
  100. * firmware emits errant packets, and they get averaged out slowly.
  101. */
  102. static int hgpk_discard_decay_hack(struct psmouse *psmouse, int x, int y)
  103. {
  104. struct hgpk_data *priv = psmouse->private;
  105. int avx, avy;
  106. bool do_recal = false;
  107. avx = abs(x);
  108. avy = abs(y);
  109. /* discard if too big, or half that but > 4 times the prev delta */
  110. if (avx > recalib_delta ||
  111. (avx > recalib_delta / 2 && ((avx / 4) > priv->xlast))) {
  112. psmouse_warn(psmouse, "detected %dpx jump in x\n", x);
  113. priv->xbigj = avx;
  114. } else if (approx_half(avx, priv->xbigj)) {
  115. psmouse_warn(psmouse, "detected secondary %dpx jump in x\n", x);
  116. priv->xbigj = avx;
  117. priv->xsaw_secondary++;
  118. } else {
  119. if (priv->xbigj && priv->xsaw_secondary > 1)
  120. do_recal = true;
  121. priv->xbigj = 0;
  122. priv->xsaw_secondary = 0;
  123. }
  124. if (avy > recalib_delta ||
  125. (avy > recalib_delta / 2 && ((avy / 4) > priv->ylast))) {
  126. psmouse_warn(psmouse, "detected %dpx jump in y\n", y);
  127. priv->ybigj = avy;
  128. } else if (approx_half(avy, priv->ybigj)) {
  129. psmouse_warn(psmouse, "detected secondary %dpx jump in y\n", y);
  130. priv->ybigj = avy;
  131. priv->ysaw_secondary++;
  132. } else {
  133. if (priv->ybigj && priv->ysaw_secondary > 1)
  134. do_recal = true;
  135. priv->ybigj = 0;
  136. priv->ysaw_secondary = 0;
  137. }
  138. priv->xlast = avx;
  139. priv->ylast = avy;
  140. if (do_recal && jumpy_delay) {
  141. psmouse_warn(psmouse, "scheduling recalibration\n");
  142. psmouse_queue_work(psmouse, &priv->recalib_wq,
  143. msecs_to_jiffies(jumpy_delay));
  144. }
  145. return priv->xbigj || priv->ybigj;
  146. }
  147. static void hgpk_reset_spew_detection(struct hgpk_data *priv)
  148. {
  149. priv->spew_count = 0;
  150. priv->dupe_count = 0;
  151. priv->x_tally = 0;
  152. priv->y_tally = 0;
  153. priv->spew_flag = NO_SPEW;
  154. }
  155. static void hgpk_reset_hack_state(struct psmouse *psmouse)
  156. {
  157. struct hgpk_data *priv = psmouse->private;
  158. priv->abs_x = priv->abs_y = -1;
  159. priv->xlast = priv->ylast = ILLEGAL_XY;
  160. priv->xbigj = priv->ybigj = 0;
  161. priv->xsaw_secondary = priv->ysaw_secondary = 0;
  162. hgpk_reset_spew_detection(priv);
  163. }
  164. /*
  165. * We have no idea why this particular hardware bug occurs. The touchpad
  166. * will randomly start spewing packets without anything touching the
  167. * pad. This wouldn't necessarily be bad, but it's indicative of a
  168. * severely miscalibrated pad; attempting to use the touchpad while it's
  169. * spewing means the cursor will jump all over the place, and act "drunk".
  170. *
  171. * The packets that are spewed tend to all have deltas between -2 and 2, and
  172. * the cursor will move around without really going very far. It will
  173. * tend to end up in the same location; if we tally up the changes over
  174. * 100 packets, we end up w/ a final delta of close to 0. This happens
  175. * pretty regularly when the touchpad is spewing, and is pretty hard to
  176. * manually trigger (at least for *my* fingers). So, it makes a perfect
  177. * scheme for detecting spews.
  178. */
  179. static void hgpk_spewing_hack(struct psmouse *psmouse,
  180. int l, int r, int x, int y)
  181. {
  182. struct hgpk_data *priv = psmouse->private;
  183. /* ignore button press packets; many in a row could trigger
  184. * a false-positive! */
  185. if (l || r)
  186. return;
  187. /* don't track spew if the workaround feature has been turned off */
  188. if (!spew_delay)
  189. return;
  190. if (abs(x) > 3 || abs(y) > 3) {
  191. /* no spew, or spew ended */
  192. hgpk_reset_spew_detection(priv);
  193. return;
  194. }
  195. /* Keep a tally of the overall delta to the cursor position caused by
  196. * the spew */
  197. priv->x_tally += x;
  198. priv->y_tally += y;
  199. switch (priv->spew_flag) {
  200. case NO_SPEW:
  201. /* we're not spewing, but this packet might be the start */
  202. priv->spew_flag = MAYBE_SPEWING;
  203. fallthrough;
  204. case MAYBE_SPEWING:
  205. priv->spew_count++;
  206. if (priv->spew_count < SPEW_WATCH_COUNT)
  207. break;
  208. /* excessive spew detected, request recalibration */
  209. priv->spew_flag = SPEW_DETECTED;
  210. fallthrough;
  211. case SPEW_DETECTED:
  212. /* only recalibrate when the overall delta to the cursor
  213. * is really small. if the spew is causing significant cursor
  214. * movement, it is probably a case of the user moving the
  215. * cursor very slowly across the screen. */
  216. if (abs(priv->x_tally) < 3 && abs(priv->y_tally) < 3) {
  217. psmouse_warn(psmouse, "packet spew detected (%d,%d)\n",
  218. priv->x_tally, priv->y_tally);
  219. priv->spew_flag = RECALIBRATING;
  220. psmouse_queue_work(psmouse, &priv->recalib_wq,
  221. msecs_to_jiffies(spew_delay));
  222. }
  223. break;
  224. case RECALIBRATING:
  225. /* we already detected a spew and requested a recalibration,
  226. * just wait for the queue to kick into action. */
  227. break;
  228. }
  229. }
  230. /*
  231. * HGPK Mouse Mode format (standard mouse format, sans middle button)
  232. *
  233. * byte 0: y-over x-over y-neg x-neg 1 0 swr swl
  234. * byte 1: x7 x6 x5 x4 x3 x2 x1 x0
  235. * byte 2: y7 y6 y5 y4 y3 y2 y1 y0
  236. *
  237. * swr/swl are the left/right buttons.
  238. * x-neg/y-neg are the x and y delta negative bits
  239. * x-over/y-over are the x and y overflow bits
  240. *
  241. * ---
  242. *
  243. * HGPK Advanced Mode - single-mode format
  244. *
  245. * byte 0(PT): 1 1 0 0 1 1 1 1
  246. * byte 0(GS): 1 1 1 1 1 1 1 1
  247. * byte 1: 0 x6 x5 x4 x3 x2 x1 x0
  248. * byte 2(PT): 0 0 x9 x8 x7 ? pt-dsw 0
  249. * byte 2(GS): 0 x10 x9 x8 x7 ? gs-dsw pt-dsw
  250. * byte 3: 0 y9 y8 y7 1 0 swr swl
  251. * byte 4: 0 y6 y5 y4 y3 y2 y1 y0
  252. * byte 5: 0 z6 z5 z4 z3 z2 z1 z0
  253. *
  254. * ?'s are not defined in the protocol spec, may vary between models.
  255. *
  256. * swr/swl are the left/right buttons.
  257. *
  258. * pt-dsw/gs-dsw indicate that the pt/gs sensor is detecting a
  259. * pen/finger
  260. */
  261. static bool hgpk_is_byte_valid(struct psmouse *psmouse, unsigned char *packet)
  262. {
  263. struct hgpk_data *priv = psmouse->private;
  264. int pktcnt = psmouse->pktcnt;
  265. bool valid;
  266. switch (priv->mode) {
  267. case HGPK_MODE_MOUSE:
  268. valid = (packet[0] & 0x0C) == 0x08;
  269. break;
  270. case HGPK_MODE_GLIDESENSOR:
  271. valid = pktcnt == 1 ?
  272. packet[0] == HGPK_GS : !(packet[pktcnt - 1] & 0x80);
  273. break;
  274. case HGPK_MODE_PENTABLET:
  275. valid = pktcnt == 1 ?
  276. packet[0] == HGPK_PT : !(packet[pktcnt - 1] & 0x80);
  277. break;
  278. default:
  279. valid = false;
  280. break;
  281. }
  282. if (!valid)
  283. psmouse_dbg(psmouse,
  284. "bad data, mode %d (%d) %*ph\n",
  285. priv->mode, pktcnt, 6, psmouse->packet);
  286. return valid;
  287. }
  288. static void hgpk_process_advanced_packet(struct psmouse *psmouse)
  289. {
  290. struct hgpk_data *priv = psmouse->private;
  291. struct input_dev *idev = psmouse->dev;
  292. unsigned char *packet = psmouse->packet;
  293. int down = !!(packet[2] & 2);
  294. int left = !!(packet[3] & 1);
  295. int right = !!(packet[3] & 2);
  296. int x = packet[1] | ((packet[2] & 0x78) << 4);
  297. int y = packet[4] | ((packet[3] & 0x70) << 3);
  298. if (priv->mode == HGPK_MODE_GLIDESENSOR) {
  299. int pt_down = !!(packet[2] & 1);
  300. int finger_down = !!(packet[2] & 2);
  301. int z = packet[5];
  302. input_report_abs(idev, ABS_PRESSURE, z);
  303. if (tpdebug)
  304. psmouse_dbg(psmouse, "pd=%d fd=%d z=%d",
  305. pt_down, finger_down, z);
  306. } else {
  307. /*
  308. * PenTablet mode does not report pressure, so we don't
  309. * report it here
  310. */
  311. if (tpdebug)
  312. psmouse_dbg(psmouse, "pd=%d ", down);
  313. }
  314. if (tpdebug)
  315. psmouse_dbg(psmouse, "l=%d r=%d x=%d y=%d\n",
  316. left, right, x, y);
  317. input_report_key(idev, BTN_TOUCH, down);
  318. input_report_key(idev, BTN_LEFT, left);
  319. input_report_key(idev, BTN_RIGHT, right);
  320. /*
  321. * If this packet says that the finger was removed, reset our position
  322. * tracking so that we don't erroneously detect a jump on next press.
  323. */
  324. if (!down) {
  325. hgpk_reset_hack_state(psmouse);
  326. goto done;
  327. }
  328. /*
  329. * Weed out duplicate packets (we get quite a few, and they mess up
  330. * our jump detection)
  331. */
  332. if (x == priv->abs_x && y == priv->abs_y) {
  333. if (++priv->dupe_count > SPEW_WATCH_COUNT) {
  334. if (tpdebug)
  335. psmouse_dbg(psmouse, "hard spew detected\n");
  336. priv->spew_flag = RECALIBRATING;
  337. psmouse_queue_work(psmouse, &priv->recalib_wq,
  338. msecs_to_jiffies(spew_delay));
  339. }
  340. goto done;
  341. }
  342. /* not a duplicate, continue with position reporting */
  343. priv->dupe_count = 0;
  344. /* Don't apply hacks in PT mode, it seems reliable */
  345. if (priv->mode != HGPK_MODE_PENTABLET && priv->abs_x != -1) {
  346. int x_diff = priv->abs_x - x;
  347. int y_diff = priv->abs_y - y;
  348. if (hgpk_discard_decay_hack(psmouse, x_diff, y_diff)) {
  349. if (tpdebug)
  350. psmouse_dbg(psmouse, "discarding\n");
  351. goto done;
  352. }
  353. hgpk_spewing_hack(psmouse, left, right, x_diff, y_diff);
  354. }
  355. input_report_abs(idev, ABS_X, x);
  356. input_report_abs(idev, ABS_Y, y);
  357. priv->abs_x = x;
  358. priv->abs_y = y;
  359. done:
  360. input_sync(idev);
  361. }
  362. static void hgpk_process_simple_packet(struct psmouse *psmouse)
  363. {
  364. struct input_dev *dev = psmouse->dev;
  365. unsigned char *packet = psmouse->packet;
  366. int left = packet[0] & 1;
  367. int right = (packet[0] >> 1) & 1;
  368. int x = packet[1] - ((packet[0] << 4) & 0x100);
  369. int y = ((packet[0] << 3) & 0x100) - packet[2];
  370. if (packet[0] & 0xc0)
  371. psmouse_dbg(psmouse,
  372. "overflow -- 0x%02x 0x%02x 0x%02x\n",
  373. packet[0], packet[1], packet[2]);
  374. if (hgpk_discard_decay_hack(psmouse, x, y)) {
  375. if (tpdebug)
  376. psmouse_dbg(psmouse, "discarding\n");
  377. return;
  378. }
  379. hgpk_spewing_hack(psmouse, left, right, x, y);
  380. if (tpdebug)
  381. psmouse_dbg(psmouse, "l=%d r=%d x=%d y=%d\n",
  382. left, right, x, y);
  383. input_report_key(dev, BTN_LEFT, left);
  384. input_report_key(dev, BTN_RIGHT, right);
  385. input_report_rel(dev, REL_X, x);
  386. input_report_rel(dev, REL_Y, y);
  387. input_sync(dev);
  388. }
  389. static psmouse_ret_t hgpk_process_byte(struct psmouse *psmouse)
  390. {
  391. struct hgpk_data *priv = psmouse->private;
  392. if (!hgpk_is_byte_valid(psmouse, psmouse->packet))
  393. return PSMOUSE_BAD_DATA;
  394. if (psmouse->pktcnt >= psmouse->pktsize) {
  395. if (priv->mode == HGPK_MODE_MOUSE)
  396. hgpk_process_simple_packet(psmouse);
  397. else
  398. hgpk_process_advanced_packet(psmouse);
  399. return PSMOUSE_FULL_PACKET;
  400. }
  401. if (priv->recalib_window) {
  402. if (time_before(jiffies, priv->recalib_window)) {
  403. /*
  404. * ugh, got a packet inside our recalibration
  405. * window, schedule another recalibration.
  406. */
  407. psmouse_dbg(psmouse,
  408. "packet inside calibration window, queueing another recalibration\n");
  409. psmouse_queue_work(psmouse, &priv->recalib_wq,
  410. msecs_to_jiffies(post_interrupt_delay));
  411. }
  412. priv->recalib_window = 0;
  413. }
  414. return PSMOUSE_GOOD_DATA;
  415. }
  416. static int hgpk_select_mode(struct psmouse *psmouse)
  417. {
  418. struct ps2dev *ps2dev = &psmouse->ps2dev;
  419. struct hgpk_data *priv = psmouse->private;
  420. int i;
  421. int cmd;
  422. /*
  423. * 4 disables to enable advanced mode
  424. * then 3 0xf2 bytes as the preamble for GS/PT selection
  425. */
  426. const int advanced_init[] = {
  427. PSMOUSE_CMD_DISABLE, PSMOUSE_CMD_DISABLE,
  428. PSMOUSE_CMD_DISABLE, PSMOUSE_CMD_DISABLE,
  429. 0xf2, 0xf2, 0xf2,
  430. };
  431. switch (priv->mode) {
  432. case HGPK_MODE_MOUSE:
  433. psmouse->pktsize = 3;
  434. break;
  435. case HGPK_MODE_GLIDESENSOR:
  436. case HGPK_MODE_PENTABLET:
  437. psmouse->pktsize = 6;
  438. /* Switch to 'Advanced mode.', four disables in a row. */
  439. for (i = 0; i < ARRAY_SIZE(advanced_init); i++)
  440. if (ps2_command(ps2dev, NULL, advanced_init[i]))
  441. return -EIO;
  442. /* select between GlideSensor (mouse) or PenTablet */
  443. cmd = priv->mode == HGPK_MODE_GLIDESENSOR ?
  444. PSMOUSE_CMD_SETSCALE11 : PSMOUSE_CMD_SETSCALE21;
  445. if (ps2_command(ps2dev, NULL, cmd))
  446. return -EIO;
  447. break;
  448. default:
  449. return -EINVAL;
  450. }
  451. return 0;
  452. }
  453. static void hgpk_setup_input_device(struct input_dev *input,
  454. struct input_dev *old_input,
  455. enum hgpk_mode mode)
  456. {
  457. if (old_input) {
  458. input->name = old_input->name;
  459. input->phys = old_input->phys;
  460. input->id = old_input->id;
  461. input->dev.parent = old_input->dev.parent;
  462. }
  463. memset(input->evbit, 0, sizeof(input->evbit));
  464. memset(input->relbit, 0, sizeof(input->relbit));
  465. memset(input->keybit, 0, sizeof(input->keybit));
  466. /* All modes report left and right buttons */
  467. __set_bit(EV_KEY, input->evbit);
  468. __set_bit(BTN_LEFT, input->keybit);
  469. __set_bit(BTN_RIGHT, input->keybit);
  470. switch (mode) {
  471. case HGPK_MODE_MOUSE:
  472. __set_bit(EV_REL, input->evbit);
  473. __set_bit(REL_X, input->relbit);
  474. __set_bit(REL_Y, input->relbit);
  475. break;
  476. case HGPK_MODE_GLIDESENSOR:
  477. __set_bit(BTN_TOUCH, input->keybit);
  478. __set_bit(BTN_TOOL_FINGER, input->keybit);
  479. __set_bit(EV_ABS, input->evbit);
  480. /* GlideSensor has pressure sensor, PenTablet does not */
  481. input_set_abs_params(input, ABS_PRESSURE, 0, 15, 0, 0);
  482. /* From device specs */
  483. input_set_abs_params(input, ABS_X, 0, 399, 0, 0);
  484. input_set_abs_params(input, ABS_Y, 0, 290, 0, 0);
  485. /* Calculated by hand based on usable size (52mm x 38mm) */
  486. input_abs_set_res(input, ABS_X, 8);
  487. input_abs_set_res(input, ABS_Y, 8);
  488. break;
  489. case HGPK_MODE_PENTABLET:
  490. __set_bit(BTN_TOUCH, input->keybit);
  491. __set_bit(BTN_TOOL_FINGER, input->keybit);
  492. __set_bit(EV_ABS, input->evbit);
  493. /* From device specs */
  494. input_set_abs_params(input, ABS_X, 0, 999, 0, 0);
  495. input_set_abs_params(input, ABS_Y, 5, 239, 0, 0);
  496. /* Calculated by hand based on usable size (156mm x 38mm) */
  497. input_abs_set_res(input, ABS_X, 6);
  498. input_abs_set_res(input, ABS_Y, 8);
  499. break;
  500. default:
  501. BUG();
  502. }
  503. }
  504. static int hgpk_reset_device(struct psmouse *psmouse, bool recalibrate)
  505. {
  506. int err;
  507. psmouse_reset(psmouse);
  508. if (recalibrate) {
  509. struct ps2dev *ps2dev = &psmouse->ps2dev;
  510. /* send the recalibrate request */
  511. if (ps2_command(ps2dev, NULL, 0xf5) ||
  512. ps2_command(ps2dev, NULL, 0xf5) ||
  513. ps2_command(ps2dev, NULL, 0xe6) ||
  514. ps2_command(ps2dev, NULL, 0xf5)) {
  515. return -1;
  516. }
  517. /* according to ALPS, 150mS is required for recalibration */
  518. msleep(150);
  519. }
  520. err = hgpk_select_mode(psmouse);
  521. if (err) {
  522. psmouse_err(psmouse, "failed to select mode\n");
  523. return err;
  524. }
  525. hgpk_reset_hack_state(psmouse);
  526. return 0;
  527. }
  528. static int hgpk_force_recalibrate(struct psmouse *psmouse)
  529. {
  530. struct hgpk_data *priv = psmouse->private;
  531. int err;
  532. /* C-series touchpads added the recalibrate command */
  533. if (psmouse->model < HGPK_MODEL_C)
  534. return 0;
  535. if (!autorecal) {
  536. psmouse_dbg(psmouse, "recalibration disabled, ignoring\n");
  537. return 0;
  538. }
  539. psmouse_dbg(psmouse, "recalibrating touchpad..\n");
  540. /* we don't want to race with the irq handler, nor with resyncs */
  541. psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
  542. /* start by resetting the device */
  543. err = hgpk_reset_device(psmouse, true);
  544. if (err)
  545. return err;
  546. /*
  547. * XXX: If a finger is down during this delay, recalibration will
  548. * detect capacitance incorrectly. This is a hardware bug, and
  549. * we don't have a good way to deal with it. The 2s window stuff
  550. * (below) is our best option for now.
  551. */
  552. if (psmouse_activate(psmouse))
  553. return -1;
  554. if (tpdebug)
  555. psmouse_dbg(psmouse, "touchpad reactivated\n");
  556. /*
  557. * If we get packets right away after recalibrating, it's likely
  558. * that a finger was on the touchpad. If so, it's probably
  559. * miscalibrated, so we optionally schedule another.
  560. */
  561. if (recal_guard_time)
  562. priv->recalib_window = jiffies +
  563. msecs_to_jiffies(recal_guard_time);
  564. return 0;
  565. }
  566. /*
  567. * This puts the touchpad in a power saving mode; according to ALPS, current
  568. * consumption goes down to 50uA after running this. To turn power back on,
  569. * we drive MS-DAT low. Measuring with a 1mA resolution ammeter says that
  570. * the current on the SUS_3.3V rail drops from 3mA or 4mA to 0 when we do this.
  571. *
  572. * We have no formal spec that details this operation -- the low-power
  573. * sequence came from a long-lost email trail.
  574. */
  575. static int hgpk_toggle_powersave(struct psmouse *psmouse, int enable)
  576. {
  577. struct ps2dev *ps2dev = &psmouse->ps2dev;
  578. int timeo;
  579. int err;
  580. /* Added on D-series touchpads */
  581. if (psmouse->model < HGPK_MODEL_D)
  582. return 0;
  583. if (enable) {
  584. psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
  585. /*
  586. * Sending a byte will drive MS-DAT low; this will wake up
  587. * the controller. Once we get an ACK back from it, it
  588. * means we can continue with the touchpad re-init. ALPS
  589. * tells us that 1s should be long enough, so set that as
  590. * the upper bound. (in practice, it takes about 3 loops.)
  591. */
  592. for (timeo = 20; timeo > 0; timeo--) {
  593. if (!ps2_sendbyte(ps2dev, PSMOUSE_CMD_DISABLE, 20))
  594. break;
  595. msleep(25);
  596. }
  597. err = hgpk_reset_device(psmouse, false);
  598. if (err) {
  599. psmouse_err(psmouse, "Failed to reset device!\n");
  600. return err;
  601. }
  602. /* should be all set, enable the touchpad */
  603. psmouse_activate(psmouse);
  604. psmouse_dbg(psmouse, "Touchpad powered up.\n");
  605. } else {
  606. psmouse_dbg(psmouse, "Powering off touchpad.\n");
  607. if (ps2_command(ps2dev, NULL, 0xec) ||
  608. ps2_command(ps2dev, NULL, 0xec) ||
  609. ps2_command(ps2dev, NULL, 0xea)) {
  610. return -1;
  611. }
  612. psmouse_set_state(psmouse, PSMOUSE_IGNORE);
  613. /* probably won't see an ACK, the touchpad will be off */
  614. ps2_sendbyte(ps2dev, 0xec, 20);
  615. }
  616. return 0;
  617. }
  618. static int hgpk_poll(struct psmouse *psmouse)
  619. {
  620. /* We can't poll, so always return failure. */
  621. return -1;
  622. }
  623. static int hgpk_reconnect(struct psmouse *psmouse)
  624. {
  625. struct hgpk_data *priv = psmouse->private;
  626. /*
  627. * During suspend/resume the ps2 rails remain powered. We don't want
  628. * to do a reset because it's flush data out of buffers; however,
  629. * earlier prototypes (B1) had some brokenness that required a reset.
  630. */
  631. if (olpc_board_at_least(olpc_board(0xb2)))
  632. if (psmouse->ps2dev.serio->dev.power.power_state.event !=
  633. PM_EVENT_ON)
  634. return 0;
  635. priv->powered = 1;
  636. return hgpk_reset_device(psmouse, false);
  637. }
  638. static ssize_t hgpk_show_powered(struct psmouse *psmouse, void *data, char *buf)
  639. {
  640. struct hgpk_data *priv = psmouse->private;
  641. return sprintf(buf, "%d\n", priv->powered);
  642. }
  643. static ssize_t hgpk_set_powered(struct psmouse *psmouse, void *data,
  644. const char *buf, size_t count)
  645. {
  646. struct hgpk_data *priv = psmouse->private;
  647. unsigned int value;
  648. int err;
  649. err = kstrtouint(buf, 10, &value);
  650. if (err)
  651. return err;
  652. if (value > 1)
  653. return -EINVAL;
  654. if (value != priv->powered) {
  655. /*
  656. * hgpk_toggle_power will deal w/ state so
  657. * we're not racing w/ irq
  658. */
  659. err = hgpk_toggle_powersave(psmouse, value);
  660. if (!err)
  661. priv->powered = value;
  662. }
  663. return err ? err : count;
  664. }
  665. __PSMOUSE_DEFINE_ATTR(powered, S_IWUSR | S_IRUGO, NULL,
  666. hgpk_show_powered, hgpk_set_powered, false);
  667. static ssize_t attr_show_mode(struct psmouse *psmouse, void *data, char *buf)
  668. {
  669. struct hgpk_data *priv = psmouse->private;
  670. return sprintf(buf, "%s\n", hgpk_mode_names[priv->mode]);
  671. }
  672. static ssize_t attr_set_mode(struct psmouse *psmouse, void *data,
  673. const char *buf, size_t len)
  674. {
  675. struct hgpk_data *priv = psmouse->private;
  676. enum hgpk_mode old_mode = priv->mode;
  677. enum hgpk_mode new_mode = hgpk_mode_from_name(buf, len);
  678. struct input_dev *old_dev = psmouse->dev;
  679. struct input_dev *new_dev;
  680. int err;
  681. if (new_mode == HGPK_MODE_INVALID)
  682. return -EINVAL;
  683. if (old_mode == new_mode)
  684. return len;
  685. new_dev = input_allocate_device();
  686. if (!new_dev)
  687. return -ENOMEM;
  688. psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
  689. /* Switch device into the new mode */
  690. priv->mode = new_mode;
  691. err = hgpk_reset_device(psmouse, false);
  692. if (err)
  693. goto err_try_restore;
  694. hgpk_setup_input_device(new_dev, old_dev, new_mode);
  695. psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
  696. err = input_register_device(new_dev);
  697. if (err)
  698. goto err_try_restore;
  699. psmouse->dev = new_dev;
  700. input_unregister_device(old_dev);
  701. return len;
  702. err_try_restore:
  703. input_free_device(new_dev);
  704. priv->mode = old_mode;
  705. hgpk_reset_device(psmouse, false);
  706. return err;
  707. }
  708. PSMOUSE_DEFINE_ATTR(hgpk_mode, S_IWUSR | S_IRUGO, NULL,
  709. attr_show_mode, attr_set_mode);
  710. static ssize_t hgpk_trigger_recal_show(struct psmouse *psmouse,
  711. void *data, char *buf)
  712. {
  713. return -EINVAL;
  714. }
  715. static ssize_t hgpk_trigger_recal(struct psmouse *psmouse, void *data,
  716. const char *buf, size_t count)
  717. {
  718. struct hgpk_data *priv = psmouse->private;
  719. unsigned int value;
  720. int err;
  721. err = kstrtouint(buf, 10, &value);
  722. if (err)
  723. return err;
  724. if (value != 1)
  725. return -EINVAL;
  726. /*
  727. * We queue work instead of doing recalibration right here
  728. * to avoid adding locking to to hgpk_force_recalibrate()
  729. * since workqueue provides serialization.
  730. */
  731. psmouse_queue_work(psmouse, &priv->recalib_wq, 0);
  732. return count;
  733. }
  734. __PSMOUSE_DEFINE_ATTR(recalibrate, S_IWUSR | S_IRUGO, NULL,
  735. hgpk_trigger_recal_show, hgpk_trigger_recal, false);
  736. static void hgpk_disconnect(struct psmouse *psmouse)
  737. {
  738. struct hgpk_data *priv = psmouse->private;
  739. device_remove_file(&psmouse->ps2dev.serio->dev,
  740. &psmouse_attr_powered.dattr);
  741. device_remove_file(&psmouse->ps2dev.serio->dev,
  742. &psmouse_attr_hgpk_mode.dattr);
  743. if (psmouse->model >= HGPK_MODEL_C)
  744. device_remove_file(&psmouse->ps2dev.serio->dev,
  745. &psmouse_attr_recalibrate.dattr);
  746. psmouse_reset(psmouse);
  747. kfree(priv);
  748. }
  749. static void hgpk_recalib_work(struct work_struct *work)
  750. {
  751. struct delayed_work *w = to_delayed_work(work);
  752. struct hgpk_data *priv = container_of(w, struct hgpk_data, recalib_wq);
  753. struct psmouse *psmouse = priv->psmouse;
  754. if (hgpk_force_recalibrate(psmouse))
  755. psmouse_err(psmouse, "recalibration failed!\n");
  756. }
  757. static int hgpk_register(struct psmouse *psmouse)
  758. {
  759. struct hgpk_data *priv = psmouse->private;
  760. int err;
  761. /* register handlers */
  762. psmouse->protocol_handler = hgpk_process_byte;
  763. psmouse->poll = hgpk_poll;
  764. psmouse->disconnect = hgpk_disconnect;
  765. psmouse->reconnect = hgpk_reconnect;
  766. /* Disable the idle resync. */
  767. psmouse->resync_time = 0;
  768. /* Reset after a lot of bad bytes. */
  769. psmouse->resetafter = 1024;
  770. hgpk_setup_input_device(psmouse->dev, NULL, priv->mode);
  771. err = device_create_file(&psmouse->ps2dev.serio->dev,
  772. &psmouse_attr_powered.dattr);
  773. if (err) {
  774. psmouse_err(psmouse, "Failed creating 'powered' sysfs node\n");
  775. return err;
  776. }
  777. err = device_create_file(&psmouse->ps2dev.serio->dev,
  778. &psmouse_attr_hgpk_mode.dattr);
  779. if (err) {
  780. psmouse_err(psmouse,
  781. "Failed creating 'hgpk_mode' sysfs node\n");
  782. goto err_remove_powered;
  783. }
  784. /* C-series touchpads added the recalibrate command */
  785. if (psmouse->model >= HGPK_MODEL_C) {
  786. err = device_create_file(&psmouse->ps2dev.serio->dev,
  787. &psmouse_attr_recalibrate.dattr);
  788. if (err) {
  789. psmouse_err(psmouse,
  790. "Failed creating 'recalibrate' sysfs node\n");
  791. goto err_remove_mode;
  792. }
  793. }
  794. return 0;
  795. err_remove_mode:
  796. device_remove_file(&psmouse->ps2dev.serio->dev,
  797. &psmouse_attr_hgpk_mode.dattr);
  798. err_remove_powered:
  799. device_remove_file(&psmouse->ps2dev.serio->dev,
  800. &psmouse_attr_powered.dattr);
  801. return err;
  802. }
  803. int hgpk_init(struct psmouse *psmouse)
  804. {
  805. struct hgpk_data *priv;
  806. int err;
  807. priv = kzalloc(sizeof(struct hgpk_data), GFP_KERNEL);
  808. if (!priv) {
  809. err = -ENOMEM;
  810. goto alloc_fail;
  811. }
  812. psmouse->private = priv;
  813. priv->psmouse = psmouse;
  814. priv->powered = true;
  815. priv->mode = hgpk_default_mode;
  816. INIT_DELAYED_WORK(&priv->recalib_wq, hgpk_recalib_work);
  817. err = hgpk_reset_device(psmouse, false);
  818. if (err)
  819. goto init_fail;
  820. err = hgpk_register(psmouse);
  821. if (err)
  822. goto init_fail;
  823. return 0;
  824. init_fail:
  825. kfree(priv);
  826. alloc_fail:
  827. return err;
  828. }
  829. static enum hgpk_model_t hgpk_get_model(struct psmouse *psmouse)
  830. {
  831. struct ps2dev *ps2dev = &psmouse->ps2dev;
  832. unsigned char param[3];
  833. /* E7, E7, E7, E9 gets us a 3 byte identifier */
  834. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
  835. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
  836. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
  837. ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) {
  838. return -EIO;
  839. }
  840. psmouse_dbg(psmouse, "ID: %*ph\n", 3, param);
  841. /* HGPK signature: 0x67, 0x00, 0x<model> */
  842. if (param[0] != 0x67 || param[1] != 0x00)
  843. return -ENODEV;
  844. psmouse_info(psmouse, "OLPC touchpad revision 0x%x\n", param[2]);
  845. return param[2];
  846. }
  847. int hgpk_detect(struct psmouse *psmouse, bool set_properties)
  848. {
  849. int version;
  850. version = hgpk_get_model(psmouse);
  851. if (version < 0)
  852. return version;
  853. if (set_properties) {
  854. psmouse->vendor = "ALPS";
  855. psmouse->name = "HGPK";
  856. psmouse->model = version;
  857. }
  858. return 0;
  859. }
  860. void hgpk_module_init(void)
  861. {
  862. hgpk_default_mode = hgpk_mode_from_name(hgpk_mode_name,
  863. strlen(hgpk_mode_name));
  864. if (hgpk_default_mode == HGPK_MODE_INVALID) {
  865. hgpk_default_mode = HGPK_MODE_MOUSE;
  866. strlcpy(hgpk_mode_name, hgpk_mode_names[HGPK_MODE_MOUSE],
  867. sizeof(hgpk_mode_name));
  868. }
  869. }