2 * OLPC HGPK (XO-1) touchpad PS/2 mouse driver
4 * Copyright (c) 2006-2008 One Laptop Per Child
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).
33 #include <linux/slab.h>
34 #include <linux/input.h>
35 #include <linux/serio.h>
36 #include <linux/libps2.h>
37 #include <linux/delay.h>
44 module_param(tpdebug, bool, 0644);
45 MODULE_PARM_DESC(tpdebug, "enable debugging, dumping packets to KERN_DEBUG.");
47 static int recalib_delta = 100;
48 module_param(recalib_delta, int, 0644);
49 MODULE_PARM_DESC(recalib_delta,
50 "packets containing a delta this large will cause a recalibration.");
52 static int jumpy_delay = 1000;
53 module_param(jumpy_delay, int, 0644);
54 MODULE_PARM_DESC(jumpy_delay,
55 "delay (ms) before recal after jumpiness detected");
57 static int spew_delay = 1;
58 module_param(spew_delay, int, 0644);
59 MODULE_PARM_DESC(spew_delay,
60 "delay (ms) before recal after packet spew detected");
62 static int recal_guard_time = 2000;
63 module_param(recal_guard_time, int, 0644);
64 MODULE_PARM_DESC(recal_guard_time,
65 "interval (ms) during which recal will be restarted if packet received");
67 static int post_interrupt_delay = 1000;
68 module_param(post_interrupt_delay, int, 0644);
69 MODULE_PARM_DESC(post_interrupt_delay,
70 "delay (ms) before recal after recal interrupt detected");
72 static char hgpk_mode_name[16];
73 module_param_string(hgpk_mode, hgpk_mode_name, sizeof(hgpk_mode_name), 0644);
74 MODULE_PARM_DESC(hgpk_mode,
75 "default hgpk mode: mouse, glidesensor or pentablet");
77 static int hgpk_default_mode = HGPK_MODE_MOUSE;
79 static const char * const hgpk_mode_names[] = {
80 [HGPK_MODE_MOUSE] = "Mouse",
81 [HGPK_MODE_GLIDESENSOR] = "GlideSensor",
82 [HGPK_MODE_PENTABLET] = "PenTablet",
85 static int hgpk_mode_from_name(const char *buf, int len)
89 for (i = 0; i < ARRAY_SIZE(hgpk_mode_names); i++) {
90 const char *name = hgpk_mode_names[i];
91 if (strlen(name) == len && !strncasecmp(name, buf, len))
95 return HGPK_MODE_INVALID;
99 * When the touchpad gets ultra-sensitive, one can keep their finger 1/2"
100 * above the pad and still have it send packets. This causes a jump cursor
101 * when one places their finger on the pad. We can probably detect the
102 * jump as we see a large deltas (>= 100px). In mouse mode, I've been
103 * unable to even come close to 100px deltas during normal usage, so I think
104 * this threshold is safe. If a large delta occurs, trigger a recalibration.
106 static void hgpk_jumpy_hack(struct psmouse *psmouse, int x, int y)
108 struct hgpk_data *priv = psmouse->private;
110 if (abs(x) > recalib_delta || abs(y) > recalib_delta) {
111 hgpk_err(psmouse, ">%dpx jump detected (%d,%d)\n",
112 recalib_delta, x, y);
113 /* My car gets forty rods to the hogshead and that's the
115 psmouse_queue_work(psmouse, &priv->recalib_wq,
116 msecs_to_jiffies(jumpy_delay));
120 static void hgpk_reset_spew_detection(struct hgpk_data *priv)
122 priv->spew_count = 0;
123 priv->dupe_count = 0;
126 priv->spew_flag = NO_SPEW;
129 static void hgpk_reset_hack_state(struct psmouse *psmouse)
131 struct hgpk_data *priv = psmouse->private;
133 priv->abs_x = priv->abs_y = -1;
134 hgpk_reset_spew_detection(priv);
138 * We have no idea why this particular hardware bug occurs. The touchpad
139 * will randomly start spewing packets without anything touching the
140 * pad. This wouldn't necessarily be bad, but it's indicative of a
141 * severely miscalibrated pad; attempting to use the touchpad while it's
142 * spewing means the cursor will jump all over the place, and act "drunk".
144 * The packets that are spewed tend to all have deltas between -2 and 2, and
145 * the cursor will move around without really going very far. It will
146 * tend to end up in the same location; if we tally up the changes over
147 * 100 packets, we end up w/ a final delta of close to 0. This happens
148 * pretty regularly when the touchpad is spewing, and is pretty hard to
149 * manually trigger (at least for *my* fingers). So, it makes a perfect
150 * scheme for detecting spews.
152 static void hgpk_spewing_hack(struct psmouse *psmouse,
153 int l, int r, int x, int y)
155 struct hgpk_data *priv = psmouse->private;
157 /* ignore button press packets; many in a row could trigger
158 * a false-positive! */
162 /* don't track spew if the workaround feature has been turned off */
166 if (abs(x) > 3 || abs(y) > 3) {
167 /* no spew, or spew ended */
168 hgpk_reset_spew_detection(priv);
172 /* Keep a tally of the overall delta to the cursor position caused by
177 switch (priv->spew_flag) {
179 /* we're not spewing, but this packet might be the start */
180 priv->spew_flag = MAYBE_SPEWING;
187 if (priv->spew_count < SPEW_WATCH_COUNT)
190 /* excessive spew detected, request recalibration */
191 priv->spew_flag = SPEW_DETECTED;
196 /* only recalibrate when the overall delta to the cursor
197 * is really small. if the spew is causing significant cursor
198 * movement, it is probably a case of the user moving the
199 * cursor very slowly across the screen. */
200 if (abs(priv->x_tally) < 3 && abs(priv->y_tally) < 3) {
201 hgpk_err(psmouse, "packet spew detected (%d,%d)\n",
202 priv->x_tally, priv->y_tally);
203 priv->spew_flag = RECALIBRATING;
204 psmouse_queue_work(psmouse, &priv->recalib_wq,
205 msecs_to_jiffies(spew_delay));
210 /* we already detected a spew and requested a recalibration,
211 * just wait for the queue to kick into action. */
217 * HGPK Mouse Mode format (standard mouse format, sans middle button)
219 * byte 0: y-over x-over y-neg x-neg 1 0 swr swl
220 * byte 1: x7 x6 x5 x4 x3 x2 x1 x0
221 * byte 2: y7 y6 y5 y4 y3 y2 y1 y0
223 * swr/swl are the left/right buttons.
224 * x-neg/y-neg are the x and y delta negative bits
225 * x-over/y-over are the x and y overflow bits
229 * HGPK Advanced Mode - single-mode format
231 * byte 0(PT): 1 1 0 0 1 1 1 1
232 * byte 0(GS): 1 1 1 1 1 1 1 1
233 * byte 1: 0 x6 x5 x4 x3 x2 x1 x0
234 * byte 2(PT): 0 0 x9 x8 x7 ? pt-dsw 0
235 * byte 2(GS): 0 x10 x9 x8 x7 ? gs-dsw pt-dsw
236 * byte 3: 0 y9 y8 y7 1 0 swr swl
237 * byte 4: 0 y6 y5 y4 y3 y2 y1 y0
238 * byte 5: 0 z6 z5 z4 z3 z2 z1 z0
240 * ?'s are not defined in the protocol spec, may vary between models.
242 * swr/swl are the left/right buttons.
244 * pt-dsw/gs-dsw indicate that the pt/gs sensor is detecting a
247 static bool hgpk_is_byte_valid(struct psmouse *psmouse, unsigned char *packet)
249 struct hgpk_data *priv = psmouse->private;
250 int pktcnt = psmouse->pktcnt;
253 switch (priv->mode) {
254 case HGPK_MODE_MOUSE:
255 valid = (packet[0] & 0x0C) == 0x08;
258 case HGPK_MODE_GLIDESENSOR:
259 valid = pktcnt == 1 ?
260 packet[0] == HGPK_GS : !(packet[pktcnt - 1] & 0x80);
263 case HGPK_MODE_PENTABLET:
264 valid = pktcnt == 1 ?
265 packet[0] == HGPK_PT : !(packet[pktcnt - 1] & 0x80);
275 "bad data, mode %d (%d) %02x %02x %02x %02x %02x %02x\n",
277 psmouse->packet[0], psmouse->packet[1],
278 psmouse->packet[2], psmouse->packet[3],
279 psmouse->packet[4], psmouse->packet[5]);
284 static void hgpk_process_advanced_packet(struct psmouse *psmouse)
286 struct hgpk_data *priv = psmouse->private;
287 struct input_dev *idev = psmouse->dev;
288 unsigned char *packet = psmouse->packet;
289 int down = !!(packet[2] & 2);
290 int left = !!(packet[3] & 1);
291 int right = !!(packet[3] & 2);
292 int x = packet[1] | ((packet[2] & 0x78) << 4);
293 int y = packet[4] | ((packet[3] & 0x70) << 3);
295 if (priv->mode == HGPK_MODE_GLIDESENSOR) {
296 int pt_down = !!(packet[2] & 1);
297 int finger_down = !!(packet[2] & 2);
300 input_report_abs(idev, ABS_PRESSURE, z);
302 hgpk_dbg(psmouse, "pd=%d fd=%d z=%d",
303 pt_down, finger_down, z);
306 * PenTablet mode does not report pressure, so we don't
310 hgpk_dbg(psmouse, "pd=%d ", down);
314 hgpk_dbg(psmouse, "l=%d r=%d x=%d y=%d\n", left, right, x, y);
316 input_report_key(idev, BTN_TOUCH, down);
317 input_report_key(idev, BTN_LEFT, left);
318 input_report_key(idev, BTN_RIGHT, right);
321 * If this packet says that the finger was removed, reset our position
322 * tracking so that we don't erroneously detect a jump on next press.
325 hgpk_reset_hack_state(priv);
330 * Weed out duplicate packets (we get quite a few, and they mess up
331 * our jump detection)
333 if (x == priv->abs_x && y == priv->abs_y) {
334 if (++priv->dupe_count > SPEW_WATCH_COUNT) {
336 hgpk_dbg(psmouse, "hard spew detected\n");
337 priv->spew_flag = RECALIBRATING;
338 psmouse_queue_work(psmouse, &priv->recalib_wq,
339 msecs_to_jiffies(spew_delay));
344 /* not a duplicate, continue with position reporting */
345 priv->dupe_count = 0;
347 /* Don't apply hacks in PT mode, it seems reliable */
348 if (priv->mode != HGPK_MODE_PENTABLET && priv->abs_x != -1) {
349 hgpk_jumpy_hack(psmouse,
350 priv->abs_x - x, priv->abs_y - y);
351 hgpk_spewing_hack(psmouse, left, right,
352 priv->abs_x - x, priv->abs_y - y);
355 input_report_abs(idev, ABS_X, x);
356 input_report_abs(idev, ABS_Y, y);
364 static void hgpk_process_simple_packet(struct psmouse *psmouse)
366 struct input_dev *dev = psmouse->dev;
367 unsigned char *packet = psmouse->packet;
368 int left = packet[0] & 1;
369 int right = (packet[0] >> 1) & 1;
370 int x = packet[1] - ((packet[0] << 4) & 0x100);
371 int y = ((packet[0] << 3) & 0x100) - packet[2];
373 hgpk_jumpy_hack(psmouse, x, y);
374 hgpk_spewing_hack(psmouse, left, right, x, y);
377 hgpk_dbg(psmouse, "l=%d r=%d x=%d y=%d\n", left, right, x, y);
379 input_report_key(dev, BTN_LEFT, left);
380 input_report_key(dev, BTN_RIGHT, right);
382 input_report_rel(dev, REL_X, x);
383 input_report_rel(dev, REL_Y, y);
388 static psmouse_ret_t hgpk_process_byte(struct psmouse *psmouse)
390 struct hgpk_data *priv = psmouse->private;
392 if (!hgpk_is_byte_valid(psmouse, psmouse->packet))
393 return PSMOUSE_BAD_DATA;
395 if (psmouse->pktcnt >= psmouse->pktsize) {
396 if (priv->mode == HGPK_MODE_MOUSE)
397 hgpk_process_simple_packet(psmouse);
399 hgpk_process_advanced_packet(psmouse);
400 return PSMOUSE_FULL_PACKET;
403 if (priv->recalib_window) {
404 if (time_before(jiffies, priv->recalib_window)) {
406 * ugh, got a packet inside our recalibration
407 * window, schedule another recalibration.
410 "packet inside calibration window, "
411 "queueing another recalibration\n");
412 psmouse_queue_work(psmouse, &priv->recalib_wq,
413 msecs_to_jiffies(post_interrupt_delay));
415 priv->recalib_window = 0;
418 return PSMOUSE_GOOD_DATA;
421 static int hgpk_select_mode(struct psmouse *psmouse)
423 struct ps2dev *ps2dev = &psmouse->ps2dev;
424 struct hgpk_data *priv = psmouse->private;
429 * 4 disables to enable advanced mode
430 * then 3 0xf2 bytes as the preamble for GS/PT selection
432 const int advanced_init[] = {
433 PSMOUSE_CMD_DISABLE, PSMOUSE_CMD_DISABLE,
434 PSMOUSE_CMD_DISABLE, PSMOUSE_CMD_DISABLE,
438 switch (priv->mode) {
439 case HGPK_MODE_MOUSE:
440 psmouse->pktsize = 3;
443 case HGPK_MODE_GLIDESENSOR:
444 case HGPK_MODE_PENTABLET:
445 psmouse->pktsize = 6;
447 /* Switch to 'Advanced mode.', four disables in a row. */
448 for (i = 0; i < ARRAY_SIZE(advanced_init); i++)
449 if (ps2_command(ps2dev, NULL, advanced_init[i]))
452 /* select between GlideSensor (mouse) or PenTablet */
453 cmd = priv->mode == HGPK_MODE_GLIDESENSOR ?
454 PSMOUSE_CMD_SETSCALE11 : PSMOUSE_CMD_SETSCALE21;
456 if (ps2_command(ps2dev, NULL, cmd))
467 static void hgpk_setup_input_device(struct input_dev *input,
468 struct input_dev *old_input,
472 input->name = old_input->name;
473 input->phys = old_input->phys;
474 input->id = old_input->id;
475 input->dev.parent = old_input->dev.parent;
478 memset(input->evbit, 0, sizeof(input->evbit));
479 memset(input->relbit, 0, sizeof(input->relbit));
480 memset(input->keybit, 0, sizeof(input->keybit));
482 /* All modes report left and right buttons */
483 __set_bit(EV_KEY, input->evbit);
484 __set_bit(BTN_LEFT, input->keybit);
485 __set_bit(BTN_RIGHT, input->keybit);
488 case HGPK_MODE_MOUSE:
489 __set_bit(EV_REL, input->evbit);
490 __set_bit(REL_X, input->relbit);
491 __set_bit(REL_Y, input->relbit);
494 case HGPK_MODE_GLIDESENSOR:
495 __set_bit(BTN_TOUCH, input->keybit);
496 __set_bit(BTN_TOOL_FINGER, input->keybit);
498 __set_bit(EV_ABS, input->evbit);
500 /* GlideSensor has pressure sensor, PenTablet does not */
501 input_set_abs_params(input, ABS_PRESSURE, 0, 15, 0, 0);
503 /* From device specs */
504 input_set_abs_params(input, ABS_X, 0, 399, 0, 0);
505 input_set_abs_params(input, ABS_Y, 0, 290, 0, 0);
507 /* Calculated by hand based on usable size (52mm x 38mm) */
508 input_abs_set_res(input, ABS_X, 8);
509 input_abs_set_res(input, ABS_Y, 8);
512 case HGPK_MODE_PENTABLET:
513 __set_bit(BTN_TOUCH, input->keybit);
514 __set_bit(BTN_TOOL_FINGER, input->keybit);
516 __set_bit(EV_ABS, input->evbit);
518 /* From device specs */
519 input_set_abs_params(input, ABS_X, 0, 999, 0, 0);
520 input_set_abs_params(input, ABS_Y, 5, 239, 0, 0);
522 /* Calculated by hand based on usable size (156mm x 38mm) */
523 input_abs_set_res(input, ABS_X, 6);
524 input_abs_set_res(input, ABS_Y, 8);
532 static int hgpk_reset_device(struct psmouse *psmouse, bool recalibrate)
536 psmouse_reset(psmouse);
539 struct ps2dev *ps2dev = &psmouse->ps2dev;
541 /* send the recalibrate request */
542 if (ps2_command(ps2dev, NULL, 0xf5) ||
543 ps2_command(ps2dev, NULL, 0xf5) ||
544 ps2_command(ps2dev, NULL, 0xe6) ||
545 ps2_command(ps2dev, NULL, 0xf5)) {
549 /* according to ALPS, 150mS is required for recalibration */
553 err = hgpk_select_mode(psmouse);
555 hgpk_err(psmouse, "failed to select mode\n");
559 hgpk_reset_hack_state(psmouse);
564 static int hgpk_force_recalibrate(struct psmouse *psmouse)
566 struct ps2dev *ps2dev = &psmouse->ps2dev;
567 struct hgpk_data *priv = psmouse->private;
570 /* C-series touchpads added the recalibrate command */
571 if (psmouse->model < HGPK_MODEL_C)
574 /* we don't want to race with the irq handler, nor with resyncs */
575 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
577 /* start by resetting the device */
578 err = hgpk_reset_device(psmouse, true);
583 * XXX: If a finger is down during this delay, recalibration will
584 * detect capacitance incorrectly. This is a hardware bug, and
585 * we don't have a good way to deal with it. The 2s window stuff
586 * (below) is our best option for now.
589 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
592 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
595 * After we recalibrate, we shouldn't get any packets for 2s. If
596 * we do, it's likely that someone's finger was on the touchpad.
597 * If someone's finger *was* on the touchpad, it's probably
598 * miscalibrated. So, we should schedule another recalibration
600 priv->recalib_window = jiffies + msecs_to_jiffies(recal_guard_time);
606 * This kills power to the touchpad; according to ALPS, current consumption
607 * goes down to 50uA after running this. To turn power back on, we drive
610 static int hgpk_toggle_power(struct psmouse *psmouse, int enable)
612 struct ps2dev *ps2dev = &psmouse->ps2dev;
616 /* Added on D-series touchpads */
617 if (psmouse->model < HGPK_MODEL_D)
621 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
624 * Sending a byte will drive MS-DAT low; this will wake up
625 * the controller. Once we get an ACK back from it, it
626 * means we can continue with the touchpad re-init. ALPS
627 * tells us that 1s should be long enough, so set that as
630 for (timeo = 20; timeo > 0; timeo--) {
631 if (!ps2_sendbyte(&psmouse->ps2dev,
632 PSMOUSE_CMD_DISABLE, 20))
637 err = hgpk_reset_device(psmouse, false);
639 hgpk_err(psmouse, "Failed to reset device!\n");
643 /* should be all set, enable the touchpad */
644 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE);
645 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
648 hgpk_dbg(psmouse, "Powering off touchpad.\n");
649 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
651 if (ps2_command(ps2dev, NULL, 0xec) ||
652 ps2_command(ps2dev, NULL, 0xec) ||
653 ps2_command(ps2dev, NULL, 0xea)) {
657 /* probably won't see an ACK, the touchpad will be off */
658 ps2_sendbyte(&psmouse->ps2dev, 0xec, 20);
664 static int hgpk_poll(struct psmouse *psmouse)
666 /* We can't poll, so always return failure. */
670 static int hgpk_reconnect(struct psmouse *psmouse)
673 * During suspend/resume the ps2 rails remain powered. We don't want
674 * to do a reset because it's flush data out of buffers; however,
675 * earlier prototypes (B1) had some brokenness that required a reset.
677 if (olpc_board_at_least(olpc_board(0xb2)))
678 if (psmouse->ps2dev.serio->dev.power.power_state.event !=
682 return hgpk_reset_device(psmouse, false);
685 static ssize_t hgpk_show_powered(struct psmouse *psmouse, void *data, char *buf)
687 struct hgpk_data *priv = psmouse->private;
689 return sprintf(buf, "%d\n", priv->powered);
692 static ssize_t hgpk_set_powered(struct psmouse *psmouse, void *data,
693 const char *buf, size_t count)
695 struct hgpk_data *priv = psmouse->private;
699 err = strict_strtoul(buf, 10, &value);
700 if (err || value > 1)
703 if (value != priv->powered) {
705 * hgpk_toggle_power will deal w/ state so
706 * we're not racing w/ irq
708 err = hgpk_toggle_power(psmouse, value);
710 priv->powered = value;
713 return err ? err : count;
716 __PSMOUSE_DEFINE_ATTR(powered, S_IWUSR | S_IRUGO, NULL,
717 hgpk_show_powered, hgpk_set_powered, false);
719 static ssize_t attr_show_mode(struct psmouse *psmouse, void *data, char *buf)
721 struct hgpk_data *priv = psmouse->private;
723 return sprintf(buf, "%s\n", hgpk_mode_names[priv->mode]);
726 static ssize_t attr_set_mode(struct psmouse *psmouse, void *data,
727 const char *buf, size_t len)
729 struct hgpk_data *priv = psmouse->private;
730 enum hgpk_mode old_mode = priv->mode;
731 enum hgpk_mode new_mode = hgpk_mode_from_name(buf, len);
732 struct input_dev *old_dev = psmouse->dev;
733 struct input_dev *new_dev;
736 if (new_mode == HGPK_MODE_INVALID)
739 if (old_mode == new_mode)
742 new_dev = input_allocate_device();
746 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
748 /* Switch device into the new mode */
749 priv->mode = new_mode;
750 err = hgpk_reset_device(psmouse, false);
752 goto err_try_restore;
754 hgpk_setup_input_device(new_dev, old_dev, new_mode);
756 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
758 err = input_register_device(new_dev);
760 goto err_try_restore;
762 psmouse->dev = new_dev;
763 input_unregister_device(old_dev);
768 input_free_device(new_dev);
769 priv->mode = old_mode;
770 hgpk_reset_device(psmouse, false);
775 PSMOUSE_DEFINE_ATTR(hgpk_mode, S_IWUSR | S_IRUGO, NULL,
776 attr_show_mode, attr_set_mode);
778 static ssize_t hgpk_trigger_recal_show(struct psmouse *psmouse,
779 void *data, char *buf)
784 static ssize_t hgpk_trigger_recal(struct psmouse *psmouse, void *data,
785 const char *buf, size_t count)
787 struct hgpk_data *priv = psmouse->private;
791 err = strict_strtoul(buf, 10, &value);
792 if (err || value != 1)
796 * We queue work instead of doing recalibration right here
797 * to avoid adding locking to to hgpk_force_recalibrate()
798 * since workqueue provides serialization.
800 psmouse_queue_work(psmouse, &priv->recalib_wq, 0);
804 __PSMOUSE_DEFINE_ATTR(recalibrate, S_IWUSR | S_IRUGO, NULL,
805 hgpk_trigger_recal_show, hgpk_trigger_recal, false);
807 static void hgpk_disconnect(struct psmouse *psmouse)
809 struct hgpk_data *priv = psmouse->private;
811 device_remove_file(&psmouse->ps2dev.serio->dev,
812 &psmouse_attr_powered.dattr);
813 device_remove_file(&psmouse->ps2dev.serio->dev,
814 &psmouse_attr_hgpk_mode.dattr);
816 if (psmouse->model >= HGPK_MODEL_C)
817 device_remove_file(&psmouse->ps2dev.serio->dev,
818 &psmouse_attr_recalibrate.dattr);
820 psmouse_reset(psmouse);
824 static void hgpk_recalib_work(struct work_struct *work)
826 struct delayed_work *w = to_delayed_work(work);
827 struct hgpk_data *priv = container_of(w, struct hgpk_data, recalib_wq);
828 struct psmouse *psmouse = priv->psmouse;
830 hgpk_dbg(psmouse, "recalibrating touchpad..\n");
832 if (hgpk_force_recalibrate(psmouse))
833 hgpk_err(psmouse, "recalibration failed!\n");
836 static int hgpk_register(struct psmouse *psmouse)
838 struct hgpk_data *priv = psmouse->private;
841 /* register handlers */
842 psmouse->protocol_handler = hgpk_process_byte;
843 psmouse->poll = hgpk_poll;
844 psmouse->disconnect = hgpk_disconnect;
845 psmouse->reconnect = hgpk_reconnect;
847 /* Disable the idle resync. */
848 psmouse->resync_time = 0;
849 /* Reset after a lot of bad bytes. */
850 psmouse->resetafter = 1024;
852 hgpk_setup_input_device(psmouse->dev, NULL, priv->mode);
854 err = device_create_file(&psmouse->ps2dev.serio->dev,
855 &psmouse_attr_powered.dattr);
857 hgpk_err(psmouse, "Failed creating 'powered' sysfs node\n");
861 err = device_create_file(&psmouse->ps2dev.serio->dev,
862 &psmouse_attr_hgpk_mode.dattr);
864 hgpk_err(psmouse, "Failed creating 'hgpk_mode' sysfs node\n");
865 goto err_remove_powered;
868 /* C-series touchpads added the recalibrate command */
869 if (psmouse->model >= HGPK_MODEL_C) {
870 err = device_create_file(&psmouse->ps2dev.serio->dev,
871 &psmouse_attr_recalibrate.dattr);
874 "Failed creating 'recalibrate' sysfs node\n");
875 goto err_remove_mode;
882 device_remove_file(&psmouse->ps2dev.serio->dev,
883 &psmouse_attr_hgpk_mode.dattr);
885 device_remove_file(&psmouse->ps2dev.serio->dev,
886 &psmouse_attr_powered.dattr);
890 int hgpk_init(struct psmouse *psmouse)
892 struct hgpk_data *priv;
895 priv = kzalloc(sizeof(struct hgpk_data), GFP_KERNEL);
901 psmouse->private = priv;
903 priv->psmouse = psmouse;
904 priv->powered = true;
905 priv->mode = hgpk_default_mode;
906 INIT_DELAYED_WORK(&priv->recalib_wq, hgpk_recalib_work);
908 err = hgpk_reset_device(psmouse, false);
912 err = hgpk_register(psmouse);
924 static enum hgpk_model_t hgpk_get_model(struct psmouse *psmouse)
926 struct ps2dev *ps2dev = &psmouse->ps2dev;
927 unsigned char param[3];
929 /* E7, E7, E7, E9 gets us a 3 byte identifier */
930 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
931 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
932 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
933 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) {
937 hgpk_dbg(psmouse, "ID: %02x %02x %02x\n", param[0], param[1], param[2]);
939 /* HGPK signature: 0x67, 0x00, 0x<model> */
940 if (param[0] != 0x67 || param[1] != 0x00)
943 hgpk_info(psmouse, "OLPC touchpad revision 0x%x\n", param[2]);
948 int hgpk_detect(struct psmouse *psmouse, bool set_properties)
952 version = hgpk_get_model(psmouse);
956 if (set_properties) {
957 psmouse->vendor = "ALPS";
958 psmouse->name = "HGPK";
959 psmouse->model = version;
965 void hgpk_module_init(void)
967 hgpk_default_mode = hgpk_mode_from_name(hgpk_mode_name,
968 strlen(hgpk_mode_name));
969 if (hgpk_default_mode == HGPK_MODE_INVALID) {
970 hgpk_default_mode = HGPK_MODE_MOUSE;
971 strlcpy(hgpk_mode_name, hgpk_mode_names[HGPK_MODE_MOUSE],
972 sizeof(hgpk_mode_name));