hgpk.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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/input.h>
  32. #include <linux/serio.h>
  33. #include <linux/libps2.h>
  34. #include <linux/delay.h>
  35. #include <asm/olpc.h>
  36. #include "psmouse.h"
  37. #include "hgpk.h"
  38. static int tpdebug;
  39. module_param(tpdebug, int, 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 cause a recalibration.");
  45. static int jumpy_delay = 1000;
  46. module_param(jumpy_delay, int, 0644);
  47. MODULE_PARM_DESC(jumpy_delay,
  48. "delay (ms) before recal after jumpiness detected");
  49. static int spew_delay = 1000;
  50. module_param(spew_delay, int, 0644);
  51. MODULE_PARM_DESC(spew_delay,
  52. "delay (ms) before recal after packet spew detected");
  53. static int recal_guard_time = 2000;
  54. module_param(recal_guard_time, int, 0644);
  55. MODULE_PARM_DESC(recal_guard_time,
  56. "interval (ms) during which recal will be restarted if packet received");
  57. static int post_interrupt_delay = 1000;
  58. module_param(post_interrupt_delay, int, 0644);
  59. MODULE_PARM_DESC(post_interrupt_delay,
  60. "delay (ms) before recal after recal interrupt detected");
  61. /*
  62. * When the touchpad gets ultra-sensitive, one can keep their finger 1/2"
  63. * above the pad and still have it send packets. This causes a jump cursor
  64. * when one places their finger on the pad. We can probably detect the
  65. * jump as we see a large deltas (>= 100px). In mouse mode, I've been
  66. * unable to even come close to 100px deltas during normal usage, so I think
  67. * this threshold is safe. If a large delta occurs, trigger a recalibration.
  68. */
  69. static void hgpk_jumpy_hack(struct psmouse *psmouse, int x, int y)
  70. {
  71. struct hgpk_data *priv = psmouse->private;
  72. if (abs(x) > recalib_delta || abs(y) > recalib_delta) {
  73. hgpk_err(psmouse, ">%dpx jump detected (%d,%d)\n",
  74. recalib_delta, x, y);
  75. /* My car gets forty rods to the hogshead and that's the
  76. * way I likes it! */
  77. psmouse_queue_work(psmouse, &priv->recalib_wq,
  78. msecs_to_jiffies(jumpy_delay));
  79. }
  80. }
  81. /*
  82. * We have no idea why this particular hardware bug occurs. The touchpad
  83. * will randomly start spewing packets without anything touching the
  84. * pad. This wouldn't necessarily be bad, but it's indicative of a
  85. * severely miscalibrated pad; attempting to use the touchpad while it's
  86. * spewing means the cursor will jump all over the place, and act "drunk".
  87. *
  88. * The packets that are spewed tend to all have deltas between -2 and 2, and
  89. * the cursor will move around without really going very far. It will
  90. * tend to end up in the same location; if we tally up the changes over
  91. * 100 packets, we end up w/ a final delta of close to 0. This happens
  92. * pretty regularly when the touchpad is spewing, and is pretty hard to
  93. * manually trigger (at least for *my* fingers). So, it makes a perfect
  94. * scheme for detecting spews.
  95. */
  96. static void hgpk_spewing_hack(struct psmouse *psmouse,
  97. int l, int r, int x, int y)
  98. {
  99. struct hgpk_data *priv = psmouse->private;
  100. /* ignore button press packets; many in a row could trigger
  101. * a false-positive! */
  102. if (l || r)
  103. return;
  104. priv->x_tally += x;
  105. priv->y_tally += y;
  106. if (++priv->count > 100) {
  107. if (abs(priv->x_tally) < 3 && abs(priv->y_tally) < 3) {
  108. hgpk_dbg(psmouse, "packet spew detected (%d,%d)\n",
  109. priv->x_tally, priv->y_tally);
  110. psmouse_queue_work(psmouse, &priv->recalib_wq,
  111. msecs_to_jiffies(spew_delay));
  112. }
  113. /* reset every 100 packets */
  114. priv->count = 0;
  115. priv->x_tally = 0;
  116. priv->y_tally = 0;
  117. }
  118. }
  119. /*
  120. * HGPK Mouse Mode format (standard mouse format, sans middle button)
  121. *
  122. * byte 0: y-over x-over y-neg x-neg 1 0 swr swl
  123. * byte 1: x7 x6 x5 x4 x3 x2 x1 x0
  124. * byte 2: y7 y6 y5 y4 y3 y2 y1 y0
  125. *
  126. * swr/swl are the left/right buttons.
  127. * x-neg/y-neg are the x and y delta negative bits
  128. * x-over/y-over are the x and y overflow bits
  129. */
  130. static int hgpk_validate_byte(unsigned char *packet)
  131. {
  132. return (packet[0] & 0x0C) != 0x08;
  133. }
  134. static void hgpk_process_packet(struct psmouse *psmouse)
  135. {
  136. struct input_dev *dev = psmouse->dev;
  137. unsigned char *packet = psmouse->packet;
  138. int x, y, left, right;
  139. left = packet[0] & 1;
  140. right = (packet[0] >> 1) & 1;
  141. x = packet[1] - ((packet[0] << 4) & 0x100);
  142. y = ((packet[0] << 3) & 0x100) - packet[2];
  143. hgpk_jumpy_hack(psmouse, x, y);
  144. hgpk_spewing_hack(psmouse, left, right, x, y);
  145. if (tpdebug)
  146. hgpk_dbg(psmouse, "l=%d r=%d x=%d y=%d\n", left, right, x, y);
  147. input_report_key(dev, BTN_LEFT, left);
  148. input_report_key(dev, BTN_RIGHT, right);
  149. input_report_rel(dev, REL_X, x);
  150. input_report_rel(dev, REL_Y, y);
  151. input_sync(dev);
  152. }
  153. static psmouse_ret_t hgpk_process_byte(struct psmouse *psmouse)
  154. {
  155. struct hgpk_data *priv = psmouse->private;
  156. if (hgpk_validate_byte(psmouse->packet)) {
  157. hgpk_dbg(psmouse, "%s: (%d) %02x %02x %02x\n",
  158. __func__, psmouse->pktcnt, psmouse->packet[0],
  159. psmouse->packet[1], psmouse->packet[2]);
  160. return PSMOUSE_BAD_DATA;
  161. }
  162. if (psmouse->pktcnt >= psmouse->pktsize) {
  163. hgpk_process_packet(psmouse);
  164. return PSMOUSE_FULL_PACKET;
  165. }
  166. if (priv->recalib_window) {
  167. if (time_before(jiffies, priv->recalib_window)) {
  168. /*
  169. * ugh, got a packet inside our recalibration
  170. * window, schedule another recalibration.
  171. */
  172. hgpk_dbg(psmouse,
  173. "packet inside calibration window, "
  174. "queueing another recalibration\n");
  175. psmouse_queue_work(psmouse, &priv->recalib_wq,
  176. msecs_to_jiffies(post_interrupt_delay));
  177. }
  178. priv->recalib_window = 0;
  179. }
  180. return PSMOUSE_GOOD_DATA;
  181. }
  182. static int hgpk_force_recalibrate(struct psmouse *psmouse)
  183. {
  184. struct ps2dev *ps2dev = &psmouse->ps2dev;
  185. struct hgpk_data *priv = psmouse->private;
  186. /* C-series touchpads added the recalibrate command */
  187. if (psmouse->model < HGPK_MODEL_C)
  188. return 0;
  189. /* we don't want to race with the irq handler, nor with resyncs */
  190. psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
  191. /* start by resetting the device */
  192. psmouse_reset(psmouse);
  193. /* send the recalibrate request */
  194. if (ps2_command(ps2dev, NULL, 0xf5) ||
  195. ps2_command(ps2dev, NULL, 0xf5) ||
  196. ps2_command(ps2dev, NULL, 0xe6) ||
  197. ps2_command(ps2dev, NULL, 0xf5)) {
  198. return -1;
  199. }
  200. /* according to ALPS, 150mS is required for recalibration */
  201. msleep(150);
  202. /* XXX: If a finger is down during this delay, recalibration will
  203. * detect capacitance incorrectly. This is a hardware bug, and
  204. * we don't have a good way to deal with it. The 2s window stuff
  205. * (below) is our best option for now.
  206. */
  207. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
  208. return -1;
  209. psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
  210. /* After we recalibrate, we shouldn't get any packets for 2s. If
  211. * we do, it's likely that someone's finger was on the touchpad.
  212. * If someone's finger *was* on the touchpad, it's probably
  213. * miscalibrated. So, we should schedule another recalibration
  214. */
  215. priv->recalib_window = jiffies + msecs_to_jiffies(recal_guard_time);
  216. return 0;
  217. }
  218. /*
  219. * This kills power to the touchpad; according to ALPS, current consumption
  220. * goes down to 50uA after running this. To turn power back on, we drive
  221. * MS-DAT low.
  222. */
  223. static int hgpk_toggle_power(struct psmouse *psmouse, int enable)
  224. {
  225. struct ps2dev *ps2dev = &psmouse->ps2dev;
  226. int timeo;
  227. /* Added on D-series touchpads */
  228. if (psmouse->model < HGPK_MODEL_D)
  229. return 0;
  230. if (enable) {
  231. psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
  232. /*
  233. * Sending a byte will drive MS-DAT low; this will wake up
  234. * the controller. Once we get an ACK back from it, it
  235. * means we can continue with the touchpad re-init. ALPS
  236. * tells us that 1s should be long enough, so set that as
  237. * the upper bound.
  238. */
  239. for (timeo = 20; timeo > 0; timeo--) {
  240. if (!ps2_sendbyte(&psmouse->ps2dev,
  241. PSMOUSE_CMD_DISABLE, 20))
  242. break;
  243. msleep(50);
  244. }
  245. psmouse_reset(psmouse);
  246. /* should be all set, enable the touchpad */
  247. ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE);
  248. psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
  249. } else {
  250. hgpk_dbg(psmouse, "Powering off touchpad.\n");
  251. psmouse_set_state(psmouse, PSMOUSE_IGNORE);
  252. if (ps2_command(ps2dev, NULL, 0xec) ||
  253. ps2_command(ps2dev, NULL, 0xec) ||
  254. ps2_command(ps2dev, NULL, 0xea)) {
  255. return -1;
  256. }
  257. /* probably won't see an ACK, the touchpad will be off */
  258. ps2_sendbyte(&psmouse->ps2dev, 0xec, 20);
  259. }
  260. return 0;
  261. }
  262. static int hgpk_poll(struct psmouse *psmouse)
  263. {
  264. /* We can't poll, so always return failure. */
  265. return -1;
  266. }
  267. static int hgpk_reconnect(struct psmouse *psmouse)
  268. {
  269. /* During suspend/resume the ps2 rails remain powered. We don't want
  270. * to do a reset because it's flush data out of buffers; however,
  271. * earlier prototypes (B1) had some brokenness that required a reset. */
  272. if (olpc_board_at_least(olpc_board(0xb2)))
  273. if (psmouse->ps2dev.serio->dev.power.power_state.event !=
  274. PM_EVENT_ON)
  275. return 0;
  276. psmouse_reset(psmouse);
  277. return 0;
  278. }
  279. static ssize_t hgpk_show_powered(struct psmouse *psmouse, void *data, char *buf)
  280. {
  281. struct hgpk_data *priv = psmouse->private;
  282. return sprintf(buf, "%d\n", priv->powered);
  283. }
  284. static ssize_t hgpk_set_powered(struct psmouse *psmouse, void *data,
  285. const char *buf, size_t count)
  286. {
  287. struct hgpk_data *priv = psmouse->private;
  288. unsigned long value;
  289. int err;
  290. err = strict_strtoul(buf, 10, &value);
  291. if (err || value > 1)
  292. return -EINVAL;
  293. if (value != priv->powered) {
  294. /*
  295. * hgpk_toggle_power will deal w/ state so
  296. * we're not racing w/ irq
  297. */
  298. err = hgpk_toggle_power(psmouse, value);
  299. if (!err)
  300. priv->powered = value;
  301. }
  302. return err ? err : count;
  303. }
  304. __PSMOUSE_DEFINE_ATTR(powered, S_IWUSR | S_IRUGO, NULL,
  305. hgpk_show_powered, hgpk_set_powered, false);
  306. static ssize_t hgpk_trigger_recal_show(struct psmouse *psmouse,
  307. void *data, char *buf)
  308. {
  309. return -EINVAL;
  310. }
  311. static ssize_t hgpk_trigger_recal(struct psmouse *psmouse, void *data,
  312. const char *buf, size_t count)
  313. {
  314. struct hgpk_data *priv = psmouse->private;
  315. unsigned long value;
  316. int err;
  317. err = strict_strtoul(buf, 10, &value);
  318. if (err || value != 1)
  319. return -EINVAL;
  320. /*
  321. * We queue work instead of doing recalibration right here
  322. * to avoid adding locking to to hgpk_force_recalibrate()
  323. * since workqueue provides serialization.
  324. */
  325. psmouse_queue_work(psmouse, &priv->recalib_wq, 0);
  326. return count;
  327. }
  328. __PSMOUSE_DEFINE_ATTR(recalibrate, S_IWUSR | S_IRUGO, NULL,
  329. hgpk_trigger_recal_show, hgpk_trigger_recal, false);
  330. static void hgpk_disconnect(struct psmouse *psmouse)
  331. {
  332. struct hgpk_data *priv = psmouse->private;
  333. device_remove_file(&psmouse->ps2dev.serio->dev,
  334. &psmouse_attr_powered.dattr);
  335. if (psmouse->model >= HGPK_MODEL_C)
  336. device_remove_file(&psmouse->ps2dev.serio->dev,
  337. &psmouse_attr_recalibrate.dattr);
  338. psmouse_reset(psmouse);
  339. kfree(priv);
  340. }
  341. static void hgpk_recalib_work(struct work_struct *work)
  342. {
  343. struct delayed_work *w = to_delayed_work(work);
  344. struct hgpk_data *priv = container_of(w, struct hgpk_data, recalib_wq);
  345. struct psmouse *psmouse = priv->psmouse;
  346. hgpk_dbg(psmouse, "recalibrating touchpad..\n");
  347. if (hgpk_force_recalibrate(psmouse))
  348. hgpk_err(psmouse, "recalibration failed!\n");
  349. }
  350. static int hgpk_register(struct psmouse *psmouse)
  351. {
  352. int err;
  353. /* register handlers */
  354. psmouse->protocol_handler = hgpk_process_byte;
  355. psmouse->poll = hgpk_poll;
  356. psmouse->disconnect = hgpk_disconnect;
  357. psmouse->reconnect = hgpk_reconnect;
  358. psmouse->pktsize = 3;
  359. /* Disable the idle resync. */
  360. psmouse->resync_time = 0;
  361. /* Reset after a lot of bad bytes. */
  362. psmouse->resetafter = 1024;
  363. err = device_create_file(&psmouse->ps2dev.serio->dev,
  364. &psmouse_attr_powered.dattr);
  365. if (err) {
  366. hgpk_err(psmouse, "Failed creating 'powered' sysfs node\n");
  367. return err;
  368. }
  369. /* C-series touchpads added the recalibrate command */
  370. if (psmouse->model >= HGPK_MODEL_C) {
  371. err = device_create_file(&psmouse->ps2dev.serio->dev,
  372. &psmouse_attr_recalibrate.dattr);
  373. if (err) {
  374. hgpk_err(psmouse,
  375. "Failed creating 'recalibrate' sysfs node\n");
  376. device_remove_file(&psmouse->ps2dev.serio->dev,
  377. &psmouse_attr_powered.dattr);
  378. return err;
  379. }
  380. }
  381. return 0;
  382. }
  383. int hgpk_init(struct psmouse *psmouse)
  384. {
  385. struct hgpk_data *priv;
  386. int err = -ENOMEM;
  387. priv = kzalloc(sizeof(struct hgpk_data), GFP_KERNEL);
  388. if (!priv)
  389. goto alloc_fail;
  390. psmouse->private = priv;
  391. priv->psmouse = psmouse;
  392. priv->powered = true;
  393. INIT_DELAYED_WORK(&priv->recalib_wq, hgpk_recalib_work);
  394. err = psmouse_reset(psmouse);
  395. if (err)
  396. goto init_fail;
  397. err = hgpk_register(psmouse);
  398. if (err)
  399. goto init_fail;
  400. return 0;
  401. init_fail:
  402. kfree(priv);
  403. alloc_fail:
  404. return err;
  405. }
  406. static enum hgpk_model_t hgpk_get_model(struct psmouse *psmouse)
  407. {
  408. struct ps2dev *ps2dev = &psmouse->ps2dev;
  409. unsigned char param[3];
  410. /* E7, E7, E7, E9 gets us a 3 byte identifier */
  411. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
  412. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
  413. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
  414. ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) {
  415. return -EIO;
  416. }
  417. hgpk_dbg(psmouse, "ID: %02x %02x %02x\n", param[0], param[1], param[2]);
  418. /* HGPK signature: 0x67, 0x00, 0x<model> */
  419. if (param[0] != 0x67 || param[1] != 0x00)
  420. return -ENODEV;
  421. hgpk_info(psmouse, "OLPC touchpad revision 0x%x\n", param[2]);
  422. return param[2];
  423. }
  424. int hgpk_detect(struct psmouse *psmouse, bool set_properties)
  425. {
  426. int version;
  427. version = hgpk_get_model(psmouse);
  428. if (version < 0)
  429. return version;
  430. if (set_properties) {
  431. psmouse->vendor = "ALPS";
  432. psmouse->name = "HGPK";
  433. psmouse->model = version;
  434. }
  435. return 0;
  436. }