hgpk.c 14 KB

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