hgpk.c 14 KB

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