olpc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. /*
  2. * OLPC touchpad PS/2 mouse driver
  3. *
  4. * Copyright (c) 2006 One Laptop Per Child, inc.
  5. * Authors Zephaniah E. Hull and Andres Salomon <[email protected]>
  6. *
  7. * This driver is partly based on the ALPS driver, which is:
  8. *
  9. * Copyright (c) 2003 Neil Brown <[email protected]>
  10. * Copyright (c) 2003-2005 Peter Osterlund <[email protected]>
  11. * Copyright (c) 2004 Dmitry Torokhov <[email protected]>
  12. * Copyright (c) 2005 Vojtech Pavlik <[email protected]>
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License version 2 as
  16. * published by the Free Software Foundation.
  17. */
  18. /*
  19. * The touchpad on the OLPC is fairly wide, with the entire area usable
  20. * as a tablet ("PT mode"), and the center 1/3rd also usable as a touchpad
  21. * ("GS mode").
  22. *
  23. * Earlier version of the device had simultaneous reporting; however, that
  24. * was removed. Instead, the device now reports packets in one mode, and
  25. * tells the driver when a mode switch needs to happen.
  26. */
  27. #include <linux/input.h>
  28. #include <linux/serio.h>
  29. #include <linux/libps2.h>
  30. #include <linux/delay.h>
  31. #include <asm/olpc.h>
  32. #include "psmouse.h"
  33. #include "olpc.h"
  34. static int tpdebug;
  35. module_param(tpdebug, int, 0644);
  36. #define OLPC_GS 1 /* The GS sensor. */
  37. #define OLPC_PT 2 /* The PT sensor. */
  38. static struct olpc_model_info olpc_model_data[] = {
  39. { { 0x67, 0x00, 0x00 }, OLPC_GS | OLPC_PT }, /* unknown ID */
  40. { { 0x67, 0x00, 0x0a }, OLPC_GS | OLPC_PT }, /* pre-B1 */
  41. { { 0x67, 0x00, 0x14 }, OLPC_GS }, /* B1.*/
  42. { { 0x67, 0x00, 0x28 }, OLPC_GS | OLPC_PT }, /* B2 */
  43. { { 0x67, 0x00, 0x3c }, OLPC_GS | OLPC_PT }, /* B2-2 */
  44. { { 0x67, 0x00, 0x50 }, OLPC_GS | OLPC_PT }, /* C1 */
  45. };
  46. #define OLPC_PKT_PT 0xcf
  47. #define OLPC_PKT_GS 0xff
  48. static int olpc_absolute_mode(struct psmouse *psmouse, int mode);
  49. /*
  50. * OLPC absolute Mode - single-mode format
  51. *
  52. * byte 0: 1 1 0 0 1 1 1 1
  53. * byte 1: 0 x6 x5 x4 x3 x2 x1 x0
  54. * byte 2(PT): 0 0 x9 x8 x7 ? pt-dsw gs-dsw
  55. * byte 2(GS): 0 x10 x9 x8 x7 ? gs-dsw pt-dsw
  56. * byte 3: 0 y9 y8 y7 1 0 swr swl
  57. * byte 4: 0 y6 y5 y4 y3 y2 y1 y0
  58. * byte 5: 0 z6 z5 z4 z3 z2 z1 z0
  59. *
  60. * ?'s are not defined in the protocol spec, may vary between models.
  61. *
  62. * swr/swl are the left/right buttons.
  63. *
  64. * pt-dsw/gs-dsw indicate that the pt/gs sensor is detecting a
  65. * pen/finger
  66. */
  67. static void olpc_process_packet_gspt(struct psmouse *psmouse)
  68. {
  69. struct olpc_data *priv = psmouse->private;
  70. unsigned char *packet = psmouse->packet;
  71. struct input_dev *dev = psmouse->dev;
  72. struct input_dev *dev2 = priv->dev2;
  73. int x, y, z, gs_down = 0, pt_down = 0, left, right;
  74. struct timeval now_tv;
  75. s64 now_ns;
  76. left = packet[3] & 1;
  77. right = packet[3] & 2;
  78. x = packet[1] | ((packet[2] & 0x78) << 4);
  79. y = packet[4] | ((packet[3] & 0x70) << 3);
  80. z = packet[5];
  81. if (psmouse->packet[0] == OLPC_PKT_GS) {
  82. pt_down = !!(packet[2] & 1);
  83. gs_down = !!(packet[2] & 2);
  84. } else if (psmouse->packet[0] == OLPC_PKT_PT) {
  85. gs_down = !!(packet[2] & 1);
  86. pt_down = !!(packet[2] & 2);
  87. }
  88. /*
  89. * XXX: Kludge.
  90. * If it's been more than 30ms since the last packet,
  91. * assume that there was a lift we were never told about.
  92. */
  93. do_gettimeofday(&now_tv);
  94. now_ns = timeval_to_ns (&now_tv);
  95. if (now_ns >= priv->late) {
  96. input_report_key(dev, BTN_TOUCH, 0);
  97. input_report_key(dev, BTN_TOOL_PEN, 0);
  98. input_report_key(dev2, BTN_TOUCH, 0);
  99. input_report_key(dev2, BTN_TOOL_FINGER, 0);
  100. input_sync(dev);
  101. input_sync(dev2);
  102. }
  103. priv->late = now_ns + (30 * NSEC_PER_MSEC);
  104. if (tpdebug) {
  105. printk(KERN_DEBUG "%s %02x %02x %02x %02x %02x %02x\n",
  106. __FUNCTION__, psmouse->packet[0], psmouse->packet[1],
  107. psmouse->packet[2], psmouse->packet[3], psmouse->packet[4],
  108. psmouse->packet[5]);
  109. printk(KERN_DEBUG "l=%d r=%d p=%d g=%d x=%d y=%d z=%d\n",
  110. left, right, pt_down, gs_down, x, y, z);
  111. }
  112. if (psmouse->packet[0] == OLPC_PKT_PT) {
  113. input_report_key(dev, BTN_LEFT, left);
  114. input_report_key(dev, BTN_RIGHT, right);
  115. } else if (psmouse->packet[0] == OLPC_PKT_GS) {
  116. input_report_key(dev, BTN_LEFT, left);
  117. input_report_key(dev, BTN_RIGHT, right);
  118. input_report_key(dev2, BTN_LEFT, left);
  119. input_report_key(dev2, BTN_RIGHT, right);
  120. }
  121. input_report_key(dev, BTN_TOUCH, pt_down);
  122. input_report_key(dev, BTN_TOOL_PEN, pt_down);
  123. input_report_key(dev2, BTN_TOUCH, gs_down);
  124. input_report_key(dev2, BTN_TOOL_FINGER, gs_down);
  125. input_report_abs(dev2, ABS_PRESSURE, z);
  126. if (psmouse->packet[0] == OLPC_PKT_PT && pt_down) {
  127. input_report_abs(dev, ABS_X, x);
  128. input_report_abs(dev, ABS_Y, y);
  129. } else if (psmouse->packet[0] == OLPC_PKT_GS && gs_down) {
  130. input_report_abs(dev2, ABS_X, x);
  131. input_report_abs(dev2, ABS_Y, y);
  132. }
  133. input_sync(dev);
  134. input_sync(dev2);
  135. if (priv->pending_mode == OLPC_GS &&
  136. psmouse->packet[0] == OLPC_PKT_PT && pt_down) {
  137. priv->pending_mode = 0;
  138. cancel_delayed_work(&priv->mode_switch);
  139. }
  140. if (priv->i->flags & (OLPC_PT|OLPC_GS)) {
  141. int pending = 0;
  142. if (psmouse->packet[0] == OLPC_PKT_PT && !pt_down)
  143. pending = OLPC_GS;
  144. else if (psmouse->packet[0] == OLPC_PKT_GS && pt_down)
  145. pending = OLPC_PT;
  146. if (priv->current_mode == pending) {
  147. priv->pending_mode = 0;
  148. pending = priv->current_mode;
  149. }
  150. else if (priv->pending_mode != pending) {
  151. priv->pending_mode = pending;
  152. if (tpdebug)
  153. printk(KERN_WARNING "Scheduling mode switch to %s.\n",
  154. pending == OLPC_GS ? "GS" : "PT");
  155. /*
  156. * Apply a de-bounce when switching from PT to GS, to allow for
  157. * spurious PT-up packets.
  158. */
  159. if (priv->pending_mode == OLPC_GS)
  160. queue_delayed_work(kpsmoused_wq, &priv->mode_switch, msecs_to_jiffies(50));
  161. else
  162. queue_delayed_work(kpsmoused_wq, &priv->mode_switch, 0);
  163. }
  164. }
  165. }
  166. static psmouse_ret_t olpc_process_byte(struct psmouse *psmouse)
  167. {
  168. psmouse_ret_t ret = PSMOUSE_BAD_DATA;
  169. if (psmouse->packet[0] != OLPC_PKT_PT &&
  170. psmouse->packet[0] != OLPC_PKT_GS)
  171. goto out;
  172. /* Bytes 2 - 6 should have 0 in the highest bit */
  173. if (psmouse->pktcnt >= 2 && psmouse->pktcnt <= 6 &&
  174. (psmouse->packet[psmouse->pktcnt - 1] & 0x80))
  175. goto out;
  176. if (psmouse->pktcnt == 6) {
  177. olpc_process_packet_gspt(psmouse);
  178. ret = PSMOUSE_FULL_PACKET;
  179. goto out;
  180. }
  181. ret = PSMOUSE_GOOD_DATA;
  182. out:
  183. if (ret != PSMOUSE_GOOD_DATA && ret != PSMOUSE_FULL_PACKET)
  184. printk(KERN_DEBUG "%s: (%d) %02x %02x %02x %02x %02x %02x\n",
  185. __FUNCTION__, psmouse->pktcnt, psmouse->packet[0],
  186. psmouse->packet[1], psmouse->packet[2],
  187. psmouse->packet[3], psmouse->packet[4],
  188. psmouse->packet[5]);
  189. return ret;
  190. }
  191. static struct olpc_model_info *olpc_get_model(struct psmouse *psmouse)
  192. {
  193. struct ps2dev *ps2dev = &psmouse->ps2dev;
  194. unsigned char param[4];
  195. int i;
  196. /*
  197. * Now try "E7 report". Allowed responses are in
  198. * olpc_model_data[].signature
  199. */
  200. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
  201. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
  202. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21))
  203. return NULL;
  204. param[0] = param[1] = param[2] = 0xff;
  205. if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
  206. return NULL;
  207. pr_debug("olpc.c(%d): E7 report: %2.2x %2.2x %2.2x",
  208. __LINE__, param[0], param[1], param[2]);
  209. for (i = 0; i < ARRAY_SIZE(olpc_model_data); i++) {
  210. if (!memcmp(param, olpc_model_data[i].signature,
  211. sizeof(olpc_model_data[i].signature))) {
  212. printk(KERN_INFO __FILE__ ": OLPC touchpad revision 0x%x.\n", param[2]);
  213. return olpc_model_data + i;
  214. }
  215. }
  216. /*
  217. * ALPS creates new IDs pretty frequently; rather than listing them
  218. * all, just assume they support the defaults. We've set aside the
  219. * first entry of olpc_model_data as the catch-all.
  220. */
  221. if (!memcmp(param, olpc_model_data[0].signature, 2)) {
  222. printk(KERN_INFO __FILE__ ": unknown ALPS revision %x, assuming default flags.\n", param[2]);
  223. return &olpc_model_data[0];
  224. }
  225. return NULL;
  226. }
  227. static int olpc_find_mode(struct psmouse *psmouse)
  228. {
  229. struct olpc_data *priv = psmouse->private;
  230. int mode = priv->i->flags;
  231. if (mode & OLPC_GS)
  232. mode = OLPC_GS;
  233. else if (mode & OLPC_PT)
  234. mode = OLPC_PT;
  235. else
  236. mode = -1;
  237. return mode;
  238. }
  239. /*
  240. * Touchpad should be disabled before calling this!
  241. */
  242. static int olpc_new_mode(struct psmouse *psmouse, int mode)
  243. {
  244. struct ps2dev *ps2dev = &psmouse->ps2dev;
  245. struct olpc_data *priv = psmouse->private;
  246. unsigned char param;
  247. int ret;
  248. if (tpdebug)
  249. printk(KERN_WARNING __FILE__ ": Switching to %d. [%lu]\n", mode, jiffies);
  250. if ((ret = ps2_command(ps2dev, &param, 0x01F2)))
  251. goto failed;
  252. if ((ret = ps2_command(ps2dev, &param, 0x01F2)))
  253. goto failed;
  254. if ((ret = ps2_command(ps2dev, &param, 0x01F2)))
  255. goto failed;
  256. switch (mode) {
  257. default:
  258. printk(KERN_WARNING __FILE__ ": Invalid mode %d. Defaulting to OLPC_GS.\n", mode);
  259. case OLPC_GS:
  260. ret = ps2_command(ps2dev, NULL, 0xE6);
  261. break;
  262. case OLPC_PT:
  263. ret = ps2_command(ps2dev, NULL, 0xE7);
  264. break;
  265. }
  266. if (ret)
  267. goto failed;
  268. /* XXX: This is a bit hacky, make sure this isn't screwing stuff up. */
  269. psmouse->pktcnt = psmouse->out_of_sync = 0;
  270. psmouse->last = jiffies;
  271. psmouse->state = PSMOUSE_ACTIVATED;
  272. if ((ret = ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)))
  273. goto failed;
  274. priv->current_mode = mode;
  275. priv->pending_mode = 0;
  276. if (tpdebug)
  277. printk(KERN_WARNING __FILE__ ": Switched to mode %d successful.\n", mode);
  278. failed:
  279. if (ret)
  280. printk(KERN_WARNING __FILE__ ": Mode switch to %d failed! (%d) [%lu]\n", mode, ret, jiffies);
  281. return ret;
  282. }
  283. static int olpc_absolute_mode(struct psmouse *psmouse, int mode)
  284. {
  285. struct ps2dev *ps2dev = &psmouse->ps2dev;
  286. /* Switch to 'Advanced mode.', four disables in a row. */
  287. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  288. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  289. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  290. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
  291. return -1;
  292. return olpc_new_mode(psmouse, mode);
  293. }
  294. /*
  295. * olpc_poll() - poll the touchpad for current motion packet.
  296. * Used in resync.
  297. * Note: We can't poll, so always return failure.
  298. */
  299. static int olpc_poll(struct psmouse *psmouse)
  300. {
  301. return -1;
  302. }
  303. static int olpc_reconnect(struct psmouse *psmouse)
  304. {
  305. struct olpc_data *priv = psmouse->private;
  306. int mode;
  307. if (olpc_rev_after(OLPC_REV_B2))
  308. if (psmouse->ps2dev.serio->dev.power.power_state.event != PM_EVENT_ON)
  309. return 0;
  310. psmouse_reset(psmouse);
  311. if (!(priv->i = olpc_get_model(psmouse)))
  312. return -1;
  313. mode = olpc_find_mode(psmouse);
  314. if (mode < 0)
  315. return -1;
  316. if (olpc_absolute_mode(psmouse, mode)) {
  317. printk(KERN_ERR __FILE__ ": Failed to reenable absolute mode.\n");
  318. return -1;
  319. }
  320. return 0;
  321. }
  322. static void olpc_disconnect(struct psmouse *psmouse)
  323. {
  324. struct olpc_data *priv = psmouse->private;
  325. psmouse_reset(psmouse);
  326. input_unregister_device(priv->dev2);
  327. kfree(priv);
  328. }
  329. static void olpc_mode_switch(struct work_struct *w)
  330. {
  331. struct delayed_work *work = container_of(w, struct delayed_work, work);
  332. struct olpc_data *priv = container_of(work, struct olpc_data, mode_switch);
  333. struct psmouse *psmouse = priv->psmouse;
  334. struct ps2dev *ps2dev = &psmouse->ps2dev;
  335. int pending_mode, ret;
  336. if (priv->pending_mode == priv->current_mode) {
  337. priv->pending_mode = 0;
  338. printk (KERN_DEBUG __FILE__ ": In switch_mode, no target mode.\n");
  339. return;
  340. }
  341. if (tpdebug)
  342. printk(KERN_WARNING __FILE__ ": Disable for switch to %d. [%lu]\n", priv->pending_mode, jiffies);
  343. /* XXX: This is a bit hacky, make sure this isn't screwing stuff up. */
  344. psmouse->state = PSMOUSE_INITIALIZING;
  345. ret = ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE);
  346. if (ret) {
  347. /* XXX: if this ever fails, we need to do a full reset! */
  348. printk(KERN_WARNING __FILE__ ": Disable failed for switch to %d. (%d) [%lu]\n", priv->pending_mode, ret, jiffies);
  349. return;
  350. }
  351. /*
  352. * ALPS tells us that it may take up to 20msec for the disable to
  353. * take effect; however, ps2_command() will wait up to 200msec for
  354. * the ACK to come back (and I'm assuming that by the time the
  355. * hardware sends back its ACK, it has stopped sending bytes).
  356. */
  357. pending_mode = priv->pending_mode;
  358. if (olpc_new_mode(psmouse, priv->pending_mode))
  359. goto bad;
  360. /*
  361. * Deal with a potential race condition.
  362. *
  363. * If there is a brief tap of a stylus or a fingernail that
  364. * triggers a mode switch to PT mode, and the stylus/fingernail is
  365. * lifted after the DISABLE above, but before we reenable in the new mode,
  366. * then we can get stuck in PT mode.
  367. */
  368. if (pending_mode == OLPC_PT) {
  369. priv->pending_mode = OLPC_GS;
  370. queue_delayed_work(kpsmoused_wq, &priv->mode_switch, msecs_to_jiffies(50));
  371. }
  372. return;
  373. bad:
  374. printk(KERN_WARNING __FILE__ ": Failure to switch modes, resetting device...\n");
  375. olpc_reconnect(psmouse);
  376. }
  377. int olpc_init(struct psmouse *psmouse)
  378. {
  379. struct olpc_data *priv;
  380. struct input_dev *dev = psmouse->dev;
  381. struct input_dev *dev2;
  382. int mode;
  383. priv = kzalloc(sizeof(struct olpc_data), GFP_KERNEL);
  384. dev2 = input_allocate_device();
  385. if (!priv || !dev2)
  386. goto init_fail;
  387. psmouse->private = priv;
  388. priv->dev2 = dev2;
  389. priv->psmouse = psmouse;
  390. psmouse_reset(psmouse);
  391. if (!(priv->i = olpc_get_model(psmouse)))
  392. goto init_fail;
  393. mode = olpc_find_mode(psmouse);
  394. if (mode < 0) {
  395. printk(KERN_ERR __FILE__ ": Failed to identify proper mode\n");
  396. goto init_fail;
  397. }
  398. if (olpc_absolute_mode(psmouse, mode)) {
  399. printk(KERN_ERR __FILE__ ": Failed to enable absolute mode\n");
  400. goto init_fail;
  401. }
  402. /*
  403. * Unset some of the default bits for things we don't have.
  404. */
  405. dev->evbit[LONG(EV_REL)] &= ~BIT(EV_REL);
  406. dev->relbit[LONG(REL_X)] &= ~(BIT(REL_X) | BIT(REL_Y));
  407. dev->keybit[LONG(BTN_MIDDLE)] &= ~BIT(BTN_MIDDLE);
  408. dev->evbit[LONG(EV_KEY)] |= BIT(EV_KEY);
  409. dev->keybit[LONG(BTN_TOUCH)] |= BIT(BTN_TOUCH);
  410. dev->keybit[LONG(BTN_TOOL_PEN)] |= BIT(BTN_TOOL_PEN);
  411. dev->keybit[LONG(BTN_LEFT)] |= BIT(BTN_LEFT) | BIT(BTN_RIGHT);
  412. dev->evbit[LONG(EV_ABS)] |= BIT(EV_ABS);
  413. input_set_abs_params(dev, ABS_X, 2, 1000, 0, 0);
  414. input_set_abs_params(dev, ABS_Y, 0, 717, 0, 0);
  415. snprintf(priv->phys, sizeof(priv->phys),
  416. "%s/input1", psmouse->ps2dev.serio->phys);
  417. dev2->phys = priv->phys;
  418. dev2->name = "OLPC ALPS GlideSensor";
  419. dev2->id.bustype = BUS_I8042;
  420. dev2->id.vendor = 0x0002;
  421. dev2->id.product = PSMOUSE_OLPC;
  422. dev2->id.version = 0x0000;
  423. dev2->evbit[LONG(EV_KEY)] |= BIT(EV_KEY);
  424. dev2->keybit[LONG(BTN_TOUCH)] |= BIT(BTN_TOUCH);
  425. dev2->keybit[LONG(BTN_TOOL_FINGER)] |= BIT(BTN_TOOL_FINGER);
  426. dev2->keybit[LONG(BTN_LEFT)] |= BIT(BTN_LEFT) | BIT(BTN_RIGHT);
  427. dev2->evbit[LONG(EV_ABS)] |= BIT(EV_ABS);
  428. input_set_abs_params(dev2, ABS_X, 350, 512, 0, 0);
  429. input_set_abs_params(dev2, ABS_Y, 70, 325, 0, 0);
  430. input_set_abs_params(dev2, ABS_PRESSURE, 0, 63, 0, 0);
  431. if (input_register_device(dev2)) {
  432. printk(KERN_ERR __FILE__ ": Failed to register GlideSensor\n");
  433. goto init_fail;
  434. }
  435. psmouse->protocol_handler = olpc_process_byte;
  436. psmouse->poll = olpc_poll;
  437. psmouse->disconnect = olpc_disconnect;
  438. psmouse->reconnect = olpc_reconnect;
  439. psmouse->pktsize = 6;
  440. /* Disable the idle resync. */
  441. psmouse->resync_time = 0;
  442. /* Reset after a lot of bad bytes. */
  443. psmouse->resetafter = 1024;
  444. INIT_DELAYED_WORK(&priv->mode_switch, olpc_mode_switch);
  445. return 0;
  446. init_fail:
  447. input_free_device(dev2);
  448. kfree(priv);
  449. return -1;
  450. }
  451. int olpc_detect(struct psmouse *psmouse, int set_properties)
  452. {
  453. if (!olpc_get_model(psmouse))
  454. return -1;
  455. if (set_properties) {
  456. psmouse->vendor = "ALPS";
  457. psmouse->name = "PenTablet";
  458. psmouse->model = 0;
  459. }
  460. return 0;
  461. }