hgpk.c 28 KB

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