hgpk.c 13 KB

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