drm/radeon/kms: move panel mode setup into encoder mode set
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / input / mouse / hgpk.c
blob0470dd46b566c99faa8c8aaa94f2795ad25e58dc
1 /*
2 * OLPC HGPK (XO-1) touchpad PS/2 mouse driver
4 * Copyright (c) 2006-2008 One Laptop Per Child
5 * Authors:
6 * Zephaniah E. Hull
7 * Andres Salomon <dilinger@debian.org>
9 * This driver is partly based on the ALPS driver, which is:
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>
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.
22 * The spec from ALPS is available from
23 * <http://wiki.laptop.org/go/Touch_Pad/Tablet>. It refers to this
24 * device as HGPK (Hybrid GS, PT, and Keymatrix).
26 * The earliest versions of the device had simultaneous reporting; that
27 * was removed. After that, the device used the Advanced Mode GS/PT streaming
28 * stuff. That turned out to be too buggy to support, so we've finally
29 * switched to Mouse Mode (which utilizes only the center 1/3 of the touchpad).
32 #define DEBUG
33 #include <linux/slab.h>
34 #include <linux/input.h>
35 #include <linux/module.h>
36 #include <linux/serio.h>
37 #include <linux/libps2.h>
38 #include <linux/delay.h>
39 #include <asm/olpc.h>
41 #include "psmouse.h"
42 #include "hgpk.h"
44 #define ILLEGAL_XY 999999
46 static bool tpdebug;
47 module_param(tpdebug, bool, 0644);
48 MODULE_PARM_DESC(tpdebug, "enable debugging, dumping packets to KERN_DEBUG.");
50 static int recalib_delta = 100;
51 module_param(recalib_delta, int, 0644);
52 MODULE_PARM_DESC(recalib_delta,
53 "packets containing a delta this large will be discarded, and a "
54 "recalibration may be scheduled.");
56 static int jumpy_delay = 20;
57 module_param(jumpy_delay, int, 0644);
58 MODULE_PARM_DESC(jumpy_delay,
59 "delay (ms) before recal after jumpiness detected");
61 static int spew_delay = 1;
62 module_param(spew_delay, int, 0644);
63 MODULE_PARM_DESC(spew_delay,
64 "delay (ms) before recal after packet spew detected");
66 static int recal_guard_time;
67 module_param(recal_guard_time, int, 0644);
68 MODULE_PARM_DESC(recal_guard_time,
69 "interval (ms) during which recal will be restarted if packet received");
71 static int post_interrupt_delay = 40;
72 module_param(post_interrupt_delay, int, 0644);
73 MODULE_PARM_DESC(post_interrupt_delay,
74 "delay (ms) before recal after recal interrupt detected");
76 static bool autorecal = true;
77 module_param(autorecal, bool, 0644);
78 MODULE_PARM_DESC(autorecal, "enable recalibration in the driver");
80 static char hgpk_mode_name[16];
81 module_param_string(hgpk_mode, hgpk_mode_name, sizeof(hgpk_mode_name), 0644);
82 MODULE_PARM_DESC(hgpk_mode,
83 "default hgpk mode: mouse, glidesensor or pentablet");
85 static int hgpk_default_mode = HGPK_MODE_MOUSE;
87 static const char * const hgpk_mode_names[] = {
88 [HGPK_MODE_MOUSE] = "Mouse",
89 [HGPK_MODE_GLIDESENSOR] = "GlideSensor",
90 [HGPK_MODE_PENTABLET] = "PenTablet",
93 static int hgpk_mode_from_name(const char *buf, int len)
95 int i;
97 for (i = 0; i < ARRAY_SIZE(hgpk_mode_names); i++) {
98 const char *name = hgpk_mode_names[i];
99 if (strlen(name) == len && !strncasecmp(name, buf, len))
100 return i;
103 return HGPK_MODE_INVALID;
107 * see if new value is within 20% of half of old value
109 static int approx_half(int curr, int prev)
111 int belowhalf, abovehalf;
113 if (curr < 5 || prev < 5)
114 return 0;
116 belowhalf = (prev * 8) / 20;
117 abovehalf = (prev * 12) / 20;
119 return belowhalf < curr && curr <= abovehalf;
123 * Throw out oddly large delta packets, and any that immediately follow whose
124 * values are each approximately half of the previous. It seems that the ALPS
125 * firmware emits errant packets, and they get averaged out slowly.
127 static int hgpk_discard_decay_hack(struct psmouse *psmouse, int x, int y)
129 struct hgpk_data *priv = psmouse->private;
130 int avx, avy;
131 bool do_recal = false;
133 avx = abs(x);
134 avy = abs(y);
136 /* discard if too big, or half that but > 4 times the prev delta */
137 if (avx > recalib_delta ||
138 (avx > recalib_delta / 2 && ((avx / 4) > priv->xlast))) {
139 psmouse_warn(psmouse, "detected %dpx jump in x\n", x);
140 priv->xbigj = avx;
141 } else if (approx_half(avx, priv->xbigj)) {
142 psmouse_warn(psmouse, "detected secondary %dpx jump in x\n", x);
143 priv->xbigj = avx;
144 priv->xsaw_secondary++;
145 } else {
146 if (priv->xbigj && priv->xsaw_secondary > 1)
147 do_recal = true;
148 priv->xbigj = 0;
149 priv->xsaw_secondary = 0;
152 if (avy > recalib_delta ||
153 (avy > recalib_delta / 2 && ((avy / 4) > priv->ylast))) {
154 psmouse_warn(psmouse, "detected %dpx jump in y\n", y);
155 priv->ybigj = avy;
156 } else if (approx_half(avy, priv->ybigj)) {
157 psmouse_warn(psmouse, "detected secondary %dpx jump in y\n", y);
158 priv->ybigj = avy;
159 priv->ysaw_secondary++;
160 } else {
161 if (priv->ybigj && priv->ysaw_secondary > 1)
162 do_recal = true;
163 priv->ybigj = 0;
164 priv->ysaw_secondary = 0;
167 priv->xlast = avx;
168 priv->ylast = avy;
170 if (do_recal && jumpy_delay) {
171 psmouse_warn(psmouse, "scheduling recalibration\n");
172 psmouse_queue_work(psmouse, &priv->recalib_wq,
173 msecs_to_jiffies(jumpy_delay));
176 return priv->xbigj || priv->ybigj;
179 static void hgpk_reset_spew_detection(struct hgpk_data *priv)
181 priv->spew_count = 0;
182 priv->dupe_count = 0;
183 priv->x_tally = 0;
184 priv->y_tally = 0;
185 priv->spew_flag = NO_SPEW;
188 static void hgpk_reset_hack_state(struct psmouse *psmouse)
190 struct hgpk_data *priv = psmouse->private;
192 priv->abs_x = priv->abs_y = -1;
193 priv->xlast = priv->ylast = ILLEGAL_XY;
194 priv->xbigj = priv->ybigj = 0;
195 priv->xsaw_secondary = priv->ysaw_secondary = 0;
196 hgpk_reset_spew_detection(priv);
200 * We have no idea why this particular hardware bug occurs. The touchpad
201 * will randomly start spewing packets without anything touching the
202 * pad. This wouldn't necessarily be bad, but it's indicative of a
203 * severely miscalibrated pad; attempting to use the touchpad while it's
204 * spewing means the cursor will jump all over the place, and act "drunk".
206 * The packets that are spewed tend to all have deltas between -2 and 2, and
207 * the cursor will move around without really going very far. It will
208 * tend to end up in the same location; if we tally up the changes over
209 * 100 packets, we end up w/ a final delta of close to 0. This happens
210 * pretty regularly when the touchpad is spewing, and is pretty hard to
211 * manually trigger (at least for *my* fingers). So, it makes a perfect
212 * scheme for detecting spews.
214 static void hgpk_spewing_hack(struct psmouse *psmouse,
215 int l, int r, int x, int y)
217 struct hgpk_data *priv = psmouse->private;
219 /* ignore button press packets; many in a row could trigger
220 * a false-positive! */
221 if (l || r)
222 return;
224 /* don't track spew if the workaround feature has been turned off */
225 if (!spew_delay)
226 return;
228 if (abs(x) > 3 || abs(y) > 3) {
229 /* no spew, or spew ended */
230 hgpk_reset_spew_detection(priv);
231 return;
234 /* Keep a tally of the overall delta to the cursor position caused by
235 * the spew */
236 priv->x_tally += x;
237 priv->y_tally += y;
239 switch (priv->spew_flag) {
240 case NO_SPEW:
241 /* we're not spewing, but this packet might be the start */
242 priv->spew_flag = MAYBE_SPEWING;
244 /* fall-through */
246 case MAYBE_SPEWING:
247 priv->spew_count++;
249 if (priv->spew_count < SPEW_WATCH_COUNT)
250 break;
252 /* excessive spew detected, request recalibration */
253 priv->spew_flag = SPEW_DETECTED;
255 /* fall-through */
257 case SPEW_DETECTED:
258 /* only recalibrate when the overall delta to the cursor
259 * is really small. if the spew is causing significant cursor
260 * movement, it is probably a case of the user moving the
261 * cursor very slowly across the screen. */
262 if (abs(priv->x_tally) < 3 && abs(priv->y_tally) < 3) {
263 psmouse_warn(psmouse, "packet spew detected (%d,%d)\n",
264 priv->x_tally, priv->y_tally);
265 priv->spew_flag = RECALIBRATING;
266 psmouse_queue_work(psmouse, &priv->recalib_wq,
267 msecs_to_jiffies(spew_delay));
270 break;
271 case RECALIBRATING:
272 /* we already detected a spew and requested a recalibration,
273 * just wait for the queue to kick into action. */
274 break;
279 * HGPK Mouse Mode format (standard mouse format, sans middle button)
281 * byte 0: y-over x-over y-neg x-neg 1 0 swr swl
282 * byte 1: x7 x6 x5 x4 x3 x2 x1 x0
283 * byte 2: y7 y6 y5 y4 y3 y2 y1 y0
285 * swr/swl are the left/right buttons.
286 * x-neg/y-neg are the x and y delta negative bits
287 * x-over/y-over are the x and y overflow bits
289 * ---
291 * HGPK Advanced Mode - single-mode format
293 * byte 0(PT): 1 1 0 0 1 1 1 1
294 * byte 0(GS): 1 1 1 1 1 1 1 1
295 * byte 1: 0 x6 x5 x4 x3 x2 x1 x0
296 * byte 2(PT): 0 0 x9 x8 x7 ? pt-dsw 0
297 * byte 2(GS): 0 x10 x9 x8 x7 ? gs-dsw pt-dsw
298 * byte 3: 0 y9 y8 y7 1 0 swr swl
299 * byte 4: 0 y6 y5 y4 y3 y2 y1 y0
300 * byte 5: 0 z6 z5 z4 z3 z2 z1 z0
302 * ?'s are not defined in the protocol spec, may vary between models.
304 * swr/swl are the left/right buttons.
306 * pt-dsw/gs-dsw indicate that the pt/gs sensor is detecting a
307 * pen/finger
309 static bool hgpk_is_byte_valid(struct psmouse *psmouse, unsigned char *packet)
311 struct hgpk_data *priv = psmouse->private;
312 int pktcnt = psmouse->pktcnt;
313 bool valid;
315 switch (priv->mode) {
316 case HGPK_MODE_MOUSE:
317 valid = (packet[0] & 0x0C) == 0x08;
318 break;
320 case HGPK_MODE_GLIDESENSOR:
321 valid = pktcnt == 1 ?
322 packet[0] == HGPK_GS : !(packet[pktcnt - 1] & 0x80);
323 break;
325 case HGPK_MODE_PENTABLET:
326 valid = pktcnt == 1 ?
327 packet[0] == HGPK_PT : !(packet[pktcnt - 1] & 0x80);
328 break;
330 default:
331 valid = false;
332 break;
335 if (!valid)
336 psmouse_dbg(psmouse,
337 "bad data, mode %d (%d) %02x %02x %02x %02x %02x %02x\n",
338 priv->mode, pktcnt,
339 psmouse->packet[0], psmouse->packet[1],
340 psmouse->packet[2], psmouse->packet[3],
341 psmouse->packet[4], psmouse->packet[5]);
343 return valid;
346 static void hgpk_process_advanced_packet(struct psmouse *psmouse)
348 struct hgpk_data *priv = psmouse->private;
349 struct input_dev *idev = psmouse->dev;
350 unsigned char *packet = psmouse->packet;
351 int down = !!(packet[2] & 2);
352 int left = !!(packet[3] & 1);
353 int right = !!(packet[3] & 2);
354 int x = packet[1] | ((packet[2] & 0x78) << 4);
355 int y = packet[4] | ((packet[3] & 0x70) << 3);
357 if (priv->mode == HGPK_MODE_GLIDESENSOR) {
358 int pt_down = !!(packet[2] & 1);
359 int finger_down = !!(packet[2] & 2);
360 int z = packet[5];
362 input_report_abs(idev, ABS_PRESSURE, z);
363 if (tpdebug)
364 psmouse_dbg(psmouse, "pd=%d fd=%d z=%d",
365 pt_down, finger_down, z);
366 } else {
368 * PenTablet mode does not report pressure, so we don't
369 * report it here
371 if (tpdebug)
372 psmouse_dbg(psmouse, "pd=%d ", down);
375 if (tpdebug)
376 psmouse_dbg(psmouse, "l=%d r=%d x=%d y=%d\n",
377 left, right, x, y);
379 input_report_key(idev, BTN_TOUCH, down);
380 input_report_key(idev, BTN_LEFT, left);
381 input_report_key(idev, BTN_RIGHT, right);
384 * If this packet says that the finger was removed, reset our position
385 * tracking so that we don't erroneously detect a jump on next press.
387 if (!down) {
388 hgpk_reset_hack_state(psmouse);
389 goto done;
393 * Weed out duplicate packets (we get quite a few, and they mess up
394 * our jump detection)
396 if (x == priv->abs_x && y == priv->abs_y) {
397 if (++priv->dupe_count > SPEW_WATCH_COUNT) {
398 if (tpdebug)
399 psmouse_dbg(psmouse, "hard spew detected\n");
400 priv->spew_flag = RECALIBRATING;
401 psmouse_queue_work(psmouse, &priv->recalib_wq,
402 msecs_to_jiffies(spew_delay));
404 goto done;
407 /* not a duplicate, continue with position reporting */
408 priv->dupe_count = 0;
410 /* Don't apply hacks in PT mode, it seems reliable */
411 if (priv->mode != HGPK_MODE_PENTABLET && priv->abs_x != -1) {
412 int x_diff = priv->abs_x - x;
413 int y_diff = priv->abs_y - y;
414 if (hgpk_discard_decay_hack(psmouse, x_diff, y_diff)) {
415 if (tpdebug)
416 psmouse_dbg(psmouse, "discarding\n");
417 goto done;
419 hgpk_spewing_hack(psmouse, left, right, x_diff, y_diff);
422 input_report_abs(idev, ABS_X, x);
423 input_report_abs(idev, ABS_Y, y);
424 priv->abs_x = x;
425 priv->abs_y = y;
427 done:
428 input_sync(idev);
431 static void hgpk_process_simple_packet(struct psmouse *psmouse)
433 struct input_dev *dev = psmouse->dev;
434 unsigned char *packet = psmouse->packet;
435 int left = packet[0] & 1;
436 int right = (packet[0] >> 1) & 1;
437 int x = packet[1] - ((packet[0] << 4) & 0x100);
438 int y = ((packet[0] << 3) & 0x100) - packet[2];
440 if (packet[0] & 0xc0)
441 psmouse_dbg(psmouse,
442 "overflow -- 0x%02x 0x%02x 0x%02x\n",
443 packet[0], packet[1], packet[2]);
445 if (hgpk_discard_decay_hack(psmouse, x, y)) {
446 if (tpdebug)
447 psmouse_dbg(psmouse, "discarding\n");
448 return;
451 hgpk_spewing_hack(psmouse, left, right, x, y);
453 if (tpdebug)
454 psmouse_dbg(psmouse, "l=%d r=%d x=%d y=%d\n",
455 left, right, x, y);
457 input_report_key(dev, BTN_LEFT, left);
458 input_report_key(dev, BTN_RIGHT, right);
460 input_report_rel(dev, REL_X, x);
461 input_report_rel(dev, REL_Y, y);
463 input_sync(dev);
466 static psmouse_ret_t hgpk_process_byte(struct psmouse *psmouse)
468 struct hgpk_data *priv = psmouse->private;
470 if (!hgpk_is_byte_valid(psmouse, psmouse->packet))
471 return PSMOUSE_BAD_DATA;
473 if (psmouse->pktcnt >= psmouse->pktsize) {
474 if (priv->mode == HGPK_MODE_MOUSE)
475 hgpk_process_simple_packet(psmouse);
476 else
477 hgpk_process_advanced_packet(psmouse);
478 return PSMOUSE_FULL_PACKET;
481 if (priv->recalib_window) {
482 if (time_before(jiffies, priv->recalib_window)) {
484 * ugh, got a packet inside our recalibration
485 * window, schedule another recalibration.
487 psmouse_dbg(psmouse,
488 "packet inside calibration window, queueing another recalibration\n");
489 psmouse_queue_work(psmouse, &priv->recalib_wq,
490 msecs_to_jiffies(post_interrupt_delay));
492 priv->recalib_window = 0;
495 return PSMOUSE_GOOD_DATA;
498 static int hgpk_select_mode(struct psmouse *psmouse)
500 struct ps2dev *ps2dev = &psmouse->ps2dev;
501 struct hgpk_data *priv = psmouse->private;
502 int i;
503 int cmd;
506 * 4 disables to enable advanced mode
507 * then 3 0xf2 bytes as the preamble for GS/PT selection
509 const int advanced_init[] = {
510 PSMOUSE_CMD_DISABLE, PSMOUSE_CMD_DISABLE,
511 PSMOUSE_CMD_DISABLE, PSMOUSE_CMD_DISABLE,
512 0xf2, 0xf2, 0xf2,
515 switch (priv->mode) {
516 case HGPK_MODE_MOUSE:
517 psmouse->pktsize = 3;
518 break;
520 case HGPK_MODE_GLIDESENSOR:
521 case HGPK_MODE_PENTABLET:
522 psmouse->pktsize = 6;
524 /* Switch to 'Advanced mode.', four disables in a row. */
525 for (i = 0; i < ARRAY_SIZE(advanced_init); i++)
526 if (ps2_command(ps2dev, NULL, advanced_init[i]))
527 return -EIO;
529 /* select between GlideSensor (mouse) or PenTablet */
530 cmd = priv->mode == HGPK_MODE_GLIDESENSOR ?
531 PSMOUSE_CMD_SETSCALE11 : PSMOUSE_CMD_SETSCALE21;
533 if (ps2_command(ps2dev, NULL, cmd))
534 return -EIO;
535 break;
537 default:
538 return -EINVAL;
541 return 0;
544 static void hgpk_setup_input_device(struct input_dev *input,
545 struct input_dev *old_input,
546 enum hgpk_mode mode)
548 if (old_input) {
549 input->name = old_input->name;
550 input->phys = old_input->phys;
551 input->id = old_input->id;
552 input->dev.parent = old_input->dev.parent;
555 memset(input->evbit, 0, sizeof(input->evbit));
556 memset(input->relbit, 0, sizeof(input->relbit));
557 memset(input->keybit, 0, sizeof(input->keybit));
559 /* All modes report left and right buttons */
560 __set_bit(EV_KEY, input->evbit);
561 __set_bit(BTN_LEFT, input->keybit);
562 __set_bit(BTN_RIGHT, input->keybit);
564 switch (mode) {
565 case HGPK_MODE_MOUSE:
566 __set_bit(EV_REL, input->evbit);
567 __set_bit(REL_X, input->relbit);
568 __set_bit(REL_Y, input->relbit);
569 break;
571 case HGPK_MODE_GLIDESENSOR:
572 __set_bit(BTN_TOUCH, input->keybit);
573 __set_bit(BTN_TOOL_FINGER, input->keybit);
575 __set_bit(EV_ABS, input->evbit);
577 /* GlideSensor has pressure sensor, PenTablet does not */
578 input_set_abs_params(input, ABS_PRESSURE, 0, 15, 0, 0);
580 /* From device specs */
581 input_set_abs_params(input, ABS_X, 0, 399, 0, 0);
582 input_set_abs_params(input, ABS_Y, 0, 290, 0, 0);
584 /* Calculated by hand based on usable size (52mm x 38mm) */
585 input_abs_set_res(input, ABS_X, 8);
586 input_abs_set_res(input, ABS_Y, 8);
587 break;
589 case HGPK_MODE_PENTABLET:
590 __set_bit(BTN_TOUCH, input->keybit);
591 __set_bit(BTN_TOOL_FINGER, input->keybit);
593 __set_bit(EV_ABS, input->evbit);
595 /* From device specs */
596 input_set_abs_params(input, ABS_X, 0, 999, 0, 0);
597 input_set_abs_params(input, ABS_Y, 5, 239, 0, 0);
599 /* Calculated by hand based on usable size (156mm x 38mm) */
600 input_abs_set_res(input, ABS_X, 6);
601 input_abs_set_res(input, ABS_Y, 8);
602 break;
604 default:
605 BUG();
609 static int hgpk_reset_device(struct psmouse *psmouse, bool recalibrate)
611 int err;
613 psmouse_reset(psmouse);
615 if (recalibrate) {
616 struct ps2dev *ps2dev = &psmouse->ps2dev;
618 /* send the recalibrate request */
619 if (ps2_command(ps2dev, NULL, 0xf5) ||
620 ps2_command(ps2dev, NULL, 0xf5) ||
621 ps2_command(ps2dev, NULL, 0xe6) ||
622 ps2_command(ps2dev, NULL, 0xf5)) {
623 return -1;
626 /* according to ALPS, 150mS is required for recalibration */
627 msleep(150);
630 err = hgpk_select_mode(psmouse);
631 if (err) {
632 psmouse_err(psmouse, "failed to select mode\n");
633 return err;
636 hgpk_reset_hack_state(psmouse);
638 return 0;
641 static int hgpk_force_recalibrate(struct psmouse *psmouse)
643 struct ps2dev *ps2dev = &psmouse->ps2dev;
644 struct hgpk_data *priv = psmouse->private;
645 int err;
647 /* C-series touchpads added the recalibrate command */
648 if (psmouse->model < HGPK_MODEL_C)
649 return 0;
651 if (!autorecal) {
652 psmouse_dbg(psmouse, "recalibration disabled, ignoring\n");
653 return 0;
656 psmouse_dbg(psmouse, "recalibrating touchpad..\n");
658 /* we don't want to race with the irq handler, nor with resyncs */
659 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
661 /* start by resetting the device */
662 err = hgpk_reset_device(psmouse, true);
663 if (err)
664 return err;
667 * XXX: If a finger is down during this delay, recalibration will
668 * detect capacitance incorrectly. This is a hardware bug, and
669 * we don't have a good way to deal with it. The 2s window stuff
670 * (below) is our best option for now.
673 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
674 return -1;
676 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
678 if (tpdebug)
679 psmouse_dbg(psmouse, "touchpad reactivated\n");
682 * If we get packets right away after recalibrating, it's likely
683 * that a finger was on the touchpad. If so, it's probably
684 * miscalibrated, so we optionally schedule another.
686 if (recal_guard_time)
687 priv->recalib_window = jiffies +
688 msecs_to_jiffies(recal_guard_time);
690 return 0;
694 * This puts the touchpad in a power saving mode; according to ALPS, current
695 * consumption goes down to 50uA after running this. To turn power back on,
696 * we drive MS-DAT low. Measuring with a 1mA resolution ammeter says that
697 * the current on the SUS_3.3V rail drops from 3mA or 4mA to 0 when we do this.
699 * We have no formal spec that details this operation -- the low-power
700 * sequence came from a long-lost email trail.
702 static int hgpk_toggle_powersave(struct psmouse *psmouse, int enable)
704 struct ps2dev *ps2dev = &psmouse->ps2dev;
705 int timeo;
706 int err;
708 /* Added on D-series touchpads */
709 if (psmouse->model < HGPK_MODEL_D)
710 return 0;
712 if (enable) {
713 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
716 * Sending a byte will drive MS-DAT low; this will wake up
717 * the controller. Once we get an ACK back from it, it
718 * means we can continue with the touchpad re-init. ALPS
719 * tells us that 1s should be long enough, so set that as
720 * the upper bound. (in practice, it takes about 3 loops.)
722 for (timeo = 20; timeo > 0; timeo--) {
723 if (!ps2_sendbyte(&psmouse->ps2dev,
724 PSMOUSE_CMD_DISABLE, 20))
725 break;
726 msleep(25);
729 err = hgpk_reset_device(psmouse, false);
730 if (err) {
731 psmouse_err(psmouse, "Failed to reset device!\n");
732 return err;
735 /* should be all set, enable the touchpad */
736 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE);
737 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
738 psmouse_dbg(psmouse, "Touchpad powered up.\n");
739 } else {
740 psmouse_dbg(psmouse, "Powering off touchpad.\n");
742 if (ps2_command(ps2dev, NULL, 0xec) ||
743 ps2_command(ps2dev, NULL, 0xec) ||
744 ps2_command(ps2dev, NULL, 0xea)) {
745 return -1;
748 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
750 /* probably won't see an ACK, the touchpad will be off */
751 ps2_sendbyte(&psmouse->ps2dev, 0xec, 20);
754 return 0;
757 static int hgpk_poll(struct psmouse *psmouse)
759 /* We can't poll, so always return failure. */
760 return -1;
763 static int hgpk_reconnect(struct psmouse *psmouse)
765 struct hgpk_data *priv = psmouse->private;
768 * During suspend/resume the ps2 rails remain powered. We don't want
769 * to do a reset because it's flush data out of buffers; however,
770 * earlier prototypes (B1) had some brokenness that required a reset.
772 if (olpc_board_at_least(olpc_board(0xb2)))
773 if (psmouse->ps2dev.serio->dev.power.power_state.event !=
774 PM_EVENT_ON)
775 return 0;
777 priv->powered = 1;
778 return hgpk_reset_device(psmouse, false);
781 static ssize_t hgpk_show_powered(struct psmouse *psmouse, void *data, char *buf)
783 struct hgpk_data *priv = psmouse->private;
785 return sprintf(buf, "%d\n", priv->powered);
788 static ssize_t hgpk_set_powered(struct psmouse *psmouse, void *data,
789 const char *buf, size_t count)
791 struct hgpk_data *priv = psmouse->private;
792 unsigned long value;
793 int err;
795 err = strict_strtoul(buf, 10, &value);
796 if (err || value > 1)
797 return -EINVAL;
799 if (value != priv->powered) {
801 * hgpk_toggle_power will deal w/ state so
802 * we're not racing w/ irq
804 err = hgpk_toggle_powersave(psmouse, value);
805 if (!err)
806 priv->powered = value;
809 return err ? err : count;
812 __PSMOUSE_DEFINE_ATTR(powered, S_IWUSR | S_IRUGO, NULL,
813 hgpk_show_powered, hgpk_set_powered, false);
815 static ssize_t attr_show_mode(struct psmouse *psmouse, void *data, char *buf)
817 struct hgpk_data *priv = psmouse->private;
819 return sprintf(buf, "%s\n", hgpk_mode_names[priv->mode]);
822 static ssize_t attr_set_mode(struct psmouse *psmouse, void *data,
823 const char *buf, size_t len)
825 struct hgpk_data *priv = psmouse->private;
826 enum hgpk_mode old_mode = priv->mode;
827 enum hgpk_mode new_mode = hgpk_mode_from_name(buf, len);
828 struct input_dev *old_dev = psmouse->dev;
829 struct input_dev *new_dev;
830 int err;
832 if (new_mode == HGPK_MODE_INVALID)
833 return -EINVAL;
835 if (old_mode == new_mode)
836 return len;
838 new_dev = input_allocate_device();
839 if (!new_dev)
840 return -ENOMEM;
842 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
844 /* Switch device into the new mode */
845 priv->mode = new_mode;
846 err = hgpk_reset_device(psmouse, false);
847 if (err)
848 goto err_try_restore;
850 hgpk_setup_input_device(new_dev, old_dev, new_mode);
852 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
854 err = input_register_device(new_dev);
855 if (err)
856 goto err_try_restore;
858 psmouse->dev = new_dev;
859 input_unregister_device(old_dev);
861 return len;
863 err_try_restore:
864 input_free_device(new_dev);
865 priv->mode = old_mode;
866 hgpk_reset_device(psmouse, false);
868 return err;
871 PSMOUSE_DEFINE_ATTR(hgpk_mode, S_IWUSR | S_IRUGO, NULL,
872 attr_show_mode, attr_set_mode);
874 static ssize_t hgpk_trigger_recal_show(struct psmouse *psmouse,
875 void *data, char *buf)
877 return -EINVAL;
880 static ssize_t hgpk_trigger_recal(struct psmouse *psmouse, void *data,
881 const char *buf, size_t count)
883 struct hgpk_data *priv = psmouse->private;
884 unsigned long value;
885 int err;
887 err = strict_strtoul(buf, 10, &value);
888 if (err || value != 1)
889 return -EINVAL;
892 * We queue work instead of doing recalibration right here
893 * to avoid adding locking to to hgpk_force_recalibrate()
894 * since workqueue provides serialization.
896 psmouse_queue_work(psmouse, &priv->recalib_wq, 0);
897 return count;
900 __PSMOUSE_DEFINE_ATTR(recalibrate, S_IWUSR | S_IRUGO, NULL,
901 hgpk_trigger_recal_show, hgpk_trigger_recal, false);
903 static void hgpk_disconnect(struct psmouse *psmouse)
905 struct hgpk_data *priv = psmouse->private;
907 device_remove_file(&psmouse->ps2dev.serio->dev,
908 &psmouse_attr_powered.dattr);
909 device_remove_file(&psmouse->ps2dev.serio->dev,
910 &psmouse_attr_hgpk_mode.dattr);
912 if (psmouse->model >= HGPK_MODEL_C)
913 device_remove_file(&psmouse->ps2dev.serio->dev,
914 &psmouse_attr_recalibrate.dattr);
916 psmouse_reset(psmouse);
917 kfree(priv);
920 static void hgpk_recalib_work(struct work_struct *work)
922 struct delayed_work *w = to_delayed_work(work);
923 struct hgpk_data *priv = container_of(w, struct hgpk_data, recalib_wq);
924 struct psmouse *psmouse = priv->psmouse;
926 if (hgpk_force_recalibrate(psmouse))
927 psmouse_err(psmouse, "recalibration failed!\n");
930 static int hgpk_register(struct psmouse *psmouse)
932 struct hgpk_data *priv = psmouse->private;
933 int err;
935 /* register handlers */
936 psmouse->protocol_handler = hgpk_process_byte;
937 psmouse->poll = hgpk_poll;
938 psmouse->disconnect = hgpk_disconnect;
939 psmouse->reconnect = hgpk_reconnect;
941 /* Disable the idle resync. */
942 psmouse->resync_time = 0;
943 /* Reset after a lot of bad bytes. */
944 psmouse->resetafter = 1024;
946 hgpk_setup_input_device(psmouse->dev, NULL, priv->mode);
948 err = device_create_file(&psmouse->ps2dev.serio->dev,
949 &psmouse_attr_powered.dattr);
950 if (err) {
951 psmouse_err(psmouse, "Failed creating 'powered' sysfs node\n");
952 return err;
955 err = device_create_file(&psmouse->ps2dev.serio->dev,
956 &psmouse_attr_hgpk_mode.dattr);
957 if (err) {
958 psmouse_err(psmouse,
959 "Failed creating 'hgpk_mode' sysfs node\n");
960 goto err_remove_powered;
963 /* C-series touchpads added the recalibrate command */
964 if (psmouse->model >= HGPK_MODEL_C) {
965 err = device_create_file(&psmouse->ps2dev.serio->dev,
966 &psmouse_attr_recalibrate.dattr);
967 if (err) {
968 psmouse_err(psmouse,
969 "Failed creating 'recalibrate' sysfs node\n");
970 goto err_remove_mode;
974 return 0;
976 err_remove_mode:
977 device_remove_file(&psmouse->ps2dev.serio->dev,
978 &psmouse_attr_hgpk_mode.dattr);
979 err_remove_powered:
980 device_remove_file(&psmouse->ps2dev.serio->dev,
981 &psmouse_attr_powered.dattr);
982 return err;
985 int hgpk_init(struct psmouse *psmouse)
987 struct hgpk_data *priv;
988 int err;
990 priv = kzalloc(sizeof(struct hgpk_data), GFP_KERNEL);
991 if (!priv) {
992 err = -ENOMEM;
993 goto alloc_fail;
996 psmouse->private = priv;
998 priv->psmouse = psmouse;
999 priv->powered = true;
1000 priv->mode = hgpk_default_mode;
1001 INIT_DELAYED_WORK(&priv->recalib_wq, hgpk_recalib_work);
1003 err = hgpk_reset_device(psmouse, false);
1004 if (err)
1005 goto init_fail;
1007 err = hgpk_register(psmouse);
1008 if (err)
1009 goto init_fail;
1011 return 0;
1013 init_fail:
1014 kfree(priv);
1015 alloc_fail:
1016 return err;
1019 static enum hgpk_model_t hgpk_get_model(struct psmouse *psmouse)
1021 struct ps2dev *ps2dev = &psmouse->ps2dev;
1022 unsigned char param[3];
1024 /* E7, E7, E7, E9 gets us a 3 byte identifier */
1025 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1026 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1027 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1028 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) {
1029 return -EIO;
1032 psmouse_dbg(psmouse, "ID: %02x %02x %02x\n", param[0], param[1], param[2]);
1034 /* HGPK signature: 0x67, 0x00, 0x<model> */
1035 if (param[0] != 0x67 || param[1] != 0x00)
1036 return -ENODEV;
1038 psmouse_info(psmouse, "OLPC touchpad revision 0x%x\n", param[2]);
1040 return param[2];
1043 int hgpk_detect(struct psmouse *psmouse, bool set_properties)
1045 int version;
1047 version = hgpk_get_model(psmouse);
1048 if (version < 0)
1049 return version;
1051 if (set_properties) {
1052 psmouse->vendor = "ALPS";
1053 psmouse->name = "HGPK";
1054 psmouse->model = version;
1057 return 0;
1060 void hgpk_module_init(void)
1062 hgpk_default_mode = hgpk_mode_from_name(hgpk_mode_name,
1063 strlen(hgpk_mode_name));
1064 if (hgpk_default_mode == HGPK_MODE_INVALID) {
1065 hgpk_default_mode = HGPK_MODE_MOUSE;
1066 strlcpy(hgpk_mode_name, hgpk_mode_names[HGPK_MODE_MOUSE],
1067 sizeof(hgpk_mode_name));