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
= 1000;
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");
73 * When the touchpad gets ultra-sensitive, one can keep their finger 1/2"
74 * above the pad and still have it send packets. This causes a jump cursor
75 * when one places their finger on the pad. We can probably detect the
76 * jump as we see a large deltas (>= 100px). In mouse mode, I've been
77 * unable to even come close to 100px deltas during normal usage, so I think
78 * this threshold is safe. If a large delta occurs, trigger a recalibration.
80 static void hgpk_jumpy_hack(struct psmouse
*psmouse
, int x
, int y
)
82 struct hgpk_data
*priv
= psmouse
->private;
84 if (abs(x
) > recalib_delta
|| abs(y
) > recalib_delta
) {
85 hgpk_err(psmouse
, ">%dpx jump detected (%d,%d)\n",
87 /* My car gets forty rods to the hogshead and that's the
89 psmouse_queue_work(psmouse
, &priv
->recalib_wq
,
90 msecs_to_jiffies(jumpy_delay
));
95 * We have no idea why this particular hardware bug occurs. The touchpad
96 * will randomly start spewing packets without anything touching the
97 * pad. This wouldn't necessarily be bad, but it's indicative of a
98 * severely miscalibrated pad; attempting to use the touchpad while it's
99 * spewing means the cursor will jump all over the place, and act "drunk".
101 * The packets that are spewed tend to all have deltas between -2 and 2, and
102 * the cursor will move around without really going very far. It will
103 * tend to end up in the same location; if we tally up the changes over
104 * 100 packets, we end up w/ a final delta of close to 0. This happens
105 * pretty regularly when the touchpad is spewing, and is pretty hard to
106 * manually trigger (at least for *my* fingers). So, it makes a perfect
107 * scheme for detecting spews.
109 static void hgpk_spewing_hack(struct psmouse
*psmouse
,
110 int l
, int r
, int x
, int y
)
112 struct hgpk_data
*priv
= psmouse
->private;
114 /* ignore button press packets; many in a row could trigger
115 * a false-positive! */
122 if (++priv
->count
> 100) {
123 if (abs(priv
->x_tally
) < 3 && abs(priv
->y_tally
) < 3) {
124 hgpk_dbg(psmouse
, "packet spew detected (%d,%d)\n",
125 priv
->x_tally
, priv
->y_tally
);
126 psmouse_queue_work(psmouse
, &priv
->recalib_wq
,
127 msecs_to_jiffies(spew_delay
));
129 /* reset every 100 packets */
137 * HGPK Mouse Mode format (standard mouse format, sans middle button)
139 * byte 0: y-over x-over y-neg x-neg 1 0 swr swl
140 * byte 1: x7 x6 x5 x4 x3 x2 x1 x0
141 * byte 2: y7 y6 y5 y4 y3 y2 y1 y0
143 * swr/swl are the left/right buttons.
144 * x-neg/y-neg are the x and y delta negative bits
145 * x-over/y-over are the x and y overflow bits
147 static int hgpk_validate_byte(unsigned char *packet
)
149 return (packet
[0] & 0x0C) != 0x08;
152 static void hgpk_process_packet(struct psmouse
*psmouse
)
154 struct input_dev
*dev
= psmouse
->dev
;
155 unsigned char *packet
= psmouse
->packet
;
156 int x
, y
, left
, right
;
158 left
= packet
[0] & 1;
159 right
= (packet
[0] >> 1) & 1;
161 x
= packet
[1] - ((packet
[0] << 4) & 0x100);
162 y
= ((packet
[0] << 3) & 0x100) - packet
[2];
164 hgpk_jumpy_hack(psmouse
, x
, y
);
165 hgpk_spewing_hack(psmouse
, left
, right
, x
, y
);
168 hgpk_dbg(psmouse
, "l=%d r=%d x=%d y=%d\n", left
, right
, x
, y
);
170 input_report_key(dev
, BTN_LEFT
, left
);
171 input_report_key(dev
, BTN_RIGHT
, right
);
173 input_report_rel(dev
, REL_X
, x
);
174 input_report_rel(dev
, REL_Y
, y
);
179 static psmouse_ret_t
hgpk_process_byte(struct psmouse
*psmouse
)
181 struct hgpk_data
*priv
= psmouse
->private;
183 if (hgpk_validate_byte(psmouse
->packet
)) {
184 hgpk_dbg(psmouse
, "%s: (%d) %02x %02x %02x\n",
185 __func__
, psmouse
->pktcnt
, psmouse
->packet
[0],
186 psmouse
->packet
[1], psmouse
->packet
[2]);
187 return PSMOUSE_BAD_DATA
;
190 if (psmouse
->pktcnt
>= psmouse
->pktsize
) {
191 hgpk_process_packet(psmouse
);
192 return PSMOUSE_FULL_PACKET
;
195 if (priv
->recalib_window
) {
196 if (time_before(jiffies
, priv
->recalib_window
)) {
198 * ugh, got a packet inside our recalibration
199 * window, schedule another recalibration.
202 "packet inside calibration window, "
203 "queueing another recalibration\n");
204 psmouse_queue_work(psmouse
, &priv
->recalib_wq
,
205 msecs_to_jiffies(post_interrupt_delay
));
207 priv
->recalib_window
= 0;
210 return PSMOUSE_GOOD_DATA
;
213 static int hgpk_force_recalibrate(struct psmouse
*psmouse
)
215 struct ps2dev
*ps2dev
= &psmouse
->ps2dev
;
216 struct hgpk_data
*priv
= psmouse
->private;
218 /* C-series touchpads added the recalibrate command */
219 if (psmouse
->model
< HGPK_MODEL_C
)
222 /* we don't want to race with the irq handler, nor with resyncs */
223 psmouse_set_state(psmouse
, PSMOUSE_INITIALIZING
);
225 /* start by resetting the device */
226 psmouse_reset(psmouse
);
228 /* send the recalibrate request */
229 if (ps2_command(ps2dev
, NULL
, 0xf5) ||
230 ps2_command(ps2dev
, NULL
, 0xf5) ||
231 ps2_command(ps2dev
, NULL
, 0xe6) ||
232 ps2_command(ps2dev
, NULL
, 0xf5)) {
236 /* according to ALPS, 150mS is required for recalibration */
240 if (ps2_command(ps2dev
, NULL
, PSMOUSE_CMD_ENABLE
))
243 psmouse_set_state(psmouse
, PSMOUSE_ACTIVATED
);
245 /* After we recalibrate, we shouldn't get any packets for 2s. If
246 * we do, it's likely that someone's finger was on the touchpad.
247 * If someone's finger *was* on the touchpad, it's probably
248 * miscalibrated. So, we should schedule another recalibration
250 priv
->recalib_window
= jiffies
+ msecs_to_jiffies(recal_guard_time
);
256 * This kills power to the touchpad; according to ALPS, current consumption
257 * goes down to 50uA after running this. To turn power back on, we drive
260 static int hgpk_toggle_power(struct psmouse
*psmouse
, int enable
)
262 struct ps2dev
*ps2dev
= &psmouse
->ps2dev
;
265 /* Added on D-series touchpads */
266 if (psmouse
->model
< HGPK_MODEL_D
)
270 psmouse_set_state(psmouse
, PSMOUSE_INITIALIZING
);
273 * Sending a byte will drive MS-DAT low; this will wake up
274 * the controller. Once we get an ACK back from it, it
275 * means we can continue with the touchpad re-init. ALPS
276 * tells us that 1s should be long enough, so set that as
279 for (timeo
= 20; timeo
> 0; timeo
--) {
280 if (!ps2_sendbyte(&psmouse
->ps2dev
,
281 PSMOUSE_CMD_DISABLE
, 20))
286 psmouse_reset(psmouse
);
288 /* should be all set, enable the touchpad */
289 ps2_command(&psmouse
->ps2dev
, NULL
, PSMOUSE_CMD_ENABLE
);
290 psmouse_set_state(psmouse
, PSMOUSE_ACTIVATED
);
293 hgpk_dbg(psmouse
, "Powering off touchpad.\n");
294 psmouse_set_state(psmouse
, PSMOUSE_IGNORE
);
296 if (ps2_command(ps2dev
, NULL
, 0xec) ||
297 ps2_command(ps2dev
, NULL
, 0xec) ||
298 ps2_command(ps2dev
, NULL
, 0xea)) {
302 /* probably won't see an ACK, the touchpad will be off */
303 ps2_sendbyte(&psmouse
->ps2dev
, 0xec, 20);
309 static int hgpk_poll(struct psmouse
*psmouse
)
311 /* We can't poll, so always return failure. */
315 static int hgpk_reconnect(struct psmouse
*psmouse
)
317 /* During suspend/resume the ps2 rails remain powered. We don't want
318 * to do a reset because it's flush data out of buffers; however,
319 * earlier prototypes (B1) had some brokenness that required a reset. */
320 if (olpc_board_at_least(olpc_board(0xb2)))
321 if (psmouse
->ps2dev
.serio
->dev
.power
.power_state
.event
!=
325 psmouse_reset(psmouse
);
330 static ssize_t
hgpk_show_powered(struct psmouse
*psmouse
, void *data
, char *buf
)
332 struct hgpk_data
*priv
= psmouse
->private;
334 return sprintf(buf
, "%d\n", priv
->powered
);
337 static ssize_t
hgpk_set_powered(struct psmouse
*psmouse
, void *data
,
338 const char *buf
, size_t count
)
340 struct hgpk_data
*priv
= psmouse
->private;
344 err
= strict_strtoul(buf
, 10, &value
);
345 if (err
|| value
> 1)
348 if (value
!= priv
->powered
) {
350 * hgpk_toggle_power will deal w/ state so
351 * we're not racing w/ irq
353 err
= hgpk_toggle_power(psmouse
, value
);
355 priv
->powered
= value
;
358 return err
? err
: count
;
361 __PSMOUSE_DEFINE_ATTR(powered
, S_IWUSR
| S_IRUGO
, NULL
,
362 hgpk_show_powered
, hgpk_set_powered
, false);
364 static ssize_t
hgpk_trigger_recal_show(struct psmouse
*psmouse
,
365 void *data
, char *buf
)
370 static ssize_t
hgpk_trigger_recal(struct psmouse
*psmouse
, void *data
,
371 const char *buf
, size_t count
)
373 struct hgpk_data
*priv
= psmouse
->private;
377 err
= strict_strtoul(buf
, 10, &value
);
378 if (err
|| value
!= 1)
382 * We queue work instead of doing recalibration right here
383 * to avoid adding locking to to hgpk_force_recalibrate()
384 * since workqueue provides serialization.
386 psmouse_queue_work(psmouse
, &priv
->recalib_wq
, 0);
390 __PSMOUSE_DEFINE_ATTR(recalibrate
, S_IWUSR
| S_IRUGO
, NULL
,
391 hgpk_trigger_recal_show
, hgpk_trigger_recal
, false);
393 static void hgpk_disconnect(struct psmouse
*psmouse
)
395 struct hgpk_data
*priv
= psmouse
->private;
397 device_remove_file(&psmouse
->ps2dev
.serio
->dev
,
398 &psmouse_attr_powered
.dattr
);
400 if (psmouse
->model
>= HGPK_MODEL_C
)
401 device_remove_file(&psmouse
->ps2dev
.serio
->dev
,
402 &psmouse_attr_recalibrate
.dattr
);
404 psmouse_reset(psmouse
);
408 static void hgpk_recalib_work(struct work_struct
*work
)
410 struct delayed_work
*w
= to_delayed_work(work
);
411 struct hgpk_data
*priv
= container_of(w
, struct hgpk_data
, recalib_wq
);
412 struct psmouse
*psmouse
= priv
->psmouse
;
414 hgpk_dbg(psmouse
, "recalibrating touchpad..\n");
416 if (hgpk_force_recalibrate(psmouse
))
417 hgpk_err(psmouse
, "recalibration failed!\n");
420 static int hgpk_register(struct psmouse
*psmouse
)
424 /* register handlers */
425 psmouse
->protocol_handler
= hgpk_process_byte
;
426 psmouse
->poll
= hgpk_poll
;
427 psmouse
->disconnect
= hgpk_disconnect
;
428 psmouse
->reconnect
= hgpk_reconnect
;
429 psmouse
->pktsize
= 3;
431 /* Disable the idle resync. */
432 psmouse
->resync_time
= 0;
433 /* Reset after a lot of bad bytes. */
434 psmouse
->resetafter
= 1024;
436 err
= device_create_file(&psmouse
->ps2dev
.serio
->dev
,
437 &psmouse_attr_powered
.dattr
);
439 hgpk_err(psmouse
, "Failed creating 'powered' sysfs node\n");
443 /* C-series touchpads added the recalibrate command */
444 if (psmouse
->model
>= HGPK_MODEL_C
) {
445 err
= device_create_file(&psmouse
->ps2dev
.serio
->dev
,
446 &psmouse_attr_recalibrate
.dattr
);
449 "Failed creating 'recalibrate' sysfs node\n");
450 device_remove_file(&psmouse
->ps2dev
.serio
->dev
,
451 &psmouse_attr_powered
.dattr
);
459 int hgpk_init(struct psmouse
*psmouse
)
461 struct hgpk_data
*priv
;
464 priv
= kzalloc(sizeof(struct hgpk_data
), GFP_KERNEL
);
468 psmouse
->private = priv
;
469 priv
->psmouse
= psmouse
;
470 priv
->powered
= true;
471 INIT_DELAYED_WORK(&priv
->recalib_wq
, hgpk_recalib_work
);
473 err
= psmouse_reset(psmouse
);
477 err
= hgpk_register(psmouse
);
489 static enum hgpk_model_t
hgpk_get_model(struct psmouse
*psmouse
)
491 struct ps2dev
*ps2dev
= &psmouse
->ps2dev
;
492 unsigned char param
[3];
494 /* E7, E7, E7, E9 gets us a 3 byte identifier */
495 if (ps2_command(ps2dev
, NULL
, PSMOUSE_CMD_SETSCALE21
) ||
496 ps2_command(ps2dev
, NULL
, PSMOUSE_CMD_SETSCALE21
) ||
497 ps2_command(ps2dev
, NULL
, PSMOUSE_CMD_SETSCALE21
) ||
498 ps2_command(ps2dev
, param
, PSMOUSE_CMD_GETINFO
)) {
502 hgpk_dbg(psmouse
, "ID: %02x %02x %02x\n", param
[0], param
[1], param
[2]);
504 /* HGPK signature: 0x67, 0x00, 0x<model> */
505 if (param
[0] != 0x67 || param
[1] != 0x00)
508 hgpk_info(psmouse
, "OLPC touchpad revision 0x%x\n", param
[2]);
513 int hgpk_detect(struct psmouse
*psmouse
, bool set_properties
)
517 version
= hgpk_get_model(psmouse
);
521 if (set_properties
) {
522 psmouse
->vendor
= "ALPS";
523 psmouse
->name
= "HGPK";
524 psmouse
->model
= version
;