]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/input/mouse/hgpk.c
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[net-next-2.6.git] / drivers / input / mouse / hgpk.c
CommitLineData
df08ef27
AS
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/*
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).
25 *
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).
30 */
31
32#define DEBUG
5a0e3ad6 33#include <linux/slab.h>
df08ef27
AS
34#include <linux/input.h>
35#include <linux/serio.h>
36#include <linux/libps2.h>
37#include <linux/delay.h>
38#include <asm/olpc.h>
39
40#include "psmouse.h"
41#include "hgpk.h"
42
43static int tpdebug;
44module_param(tpdebug, int, 0644);
45MODULE_PARM_DESC(tpdebug, "enable debugging, dumping packets to KERN_DEBUG.");
46
47static int recalib_delta = 100;
48module_param(recalib_delta, int, 0644);
49MODULE_PARM_DESC(recalib_delta,
50 "packets containing a delta this large will cause a recalibration.");
51
8bbf2703
PF
52static int jumpy_delay = 1000;
53module_param(jumpy_delay, int, 0644);
54MODULE_PARM_DESC(jumpy_delay,
55 "delay (ms) before recal after jumpiness detected");
56
57static int spew_delay = 1000;
58module_param(spew_delay, int, 0644);
59MODULE_PARM_DESC(spew_delay,
60 "delay (ms) before recal after packet spew detected");
61
62static int recal_guard_time = 2000;
63module_param(recal_guard_time, int, 0644);
64MODULE_PARM_DESC(recal_guard_time,
65 "interval (ms) during which recal will be restarted if packet received");
66
67static int post_interrupt_delay = 1000;
68module_param(post_interrupt_delay, int, 0644);
69MODULE_PARM_DESC(post_interrupt_delay,
70 "delay (ms) before recal after recal interrupt detected");
71
df08ef27
AS
72/*
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.
79 */
80static void hgpk_jumpy_hack(struct psmouse *psmouse, int x, int y)
81{
82 struct hgpk_data *priv = psmouse->private;
83
84 if (abs(x) > recalib_delta || abs(y) > recalib_delta) {
85 hgpk_err(psmouse, ">%dpx jump detected (%d,%d)\n",
86 recalib_delta, x, y);
87 /* My car gets forty rods to the hogshead and that's the
88 * way I likes it! */
89 psmouse_queue_work(psmouse, &priv->recalib_wq,
8bbf2703 90 msecs_to_jiffies(jumpy_delay));
df08ef27
AS
91 }
92}
93
94/*
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".
100 *
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.
108 */
109static void hgpk_spewing_hack(struct psmouse *psmouse,
110 int l, int r, int x, int y)
111{
112 struct hgpk_data *priv = psmouse->private;
113
114 /* ignore button press packets; many in a row could trigger
115 * a false-positive! */
116 if (l || r)
117 return;
118
119 priv->x_tally += x;
120 priv->y_tally += y;
121
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,
8bbf2703 127 msecs_to_jiffies(spew_delay));
df08ef27
AS
128 }
129 /* reset every 100 packets */
130 priv->count = 0;
131 priv->x_tally = 0;
132 priv->y_tally = 0;
133 }
134}
135
136/*
137 * HGPK Mouse Mode format (standard mouse format, sans middle button)
138 *
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
142 *
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
146 */
147static int hgpk_validate_byte(unsigned char *packet)
148{
5fb17fd9 149 return (packet[0] & 0x0C) != 0x08;
df08ef27
AS
150}
151
152static void hgpk_process_packet(struct psmouse *psmouse)
153{
154 struct input_dev *dev = psmouse->dev;
155 unsigned char *packet = psmouse->packet;
156 int x, y, left, right;
157
158 left = packet[0] & 1;
159 right = (packet[0] >> 1) & 1;
160
161 x = packet[1] - ((packet[0] << 4) & 0x100);
162 y = ((packet[0] << 3) & 0x100) - packet[2];
163
164 hgpk_jumpy_hack(psmouse, x, y);
165 hgpk_spewing_hack(psmouse, left, right, x, y);
166
167 if (tpdebug)
168 hgpk_dbg(psmouse, "l=%d r=%d x=%d y=%d\n", left, right, x, y);
169
170 input_report_key(dev, BTN_LEFT, left);
171 input_report_key(dev, BTN_RIGHT, right);
172
173 input_report_rel(dev, REL_X, x);
174 input_report_rel(dev, REL_Y, y);
175
176 input_sync(dev);
177}
178
179static psmouse_ret_t hgpk_process_byte(struct psmouse *psmouse)
180{
181 struct hgpk_data *priv = psmouse->private;
182
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;
188 }
189
190 if (psmouse->pktcnt >= psmouse->pktsize) {
191 hgpk_process_packet(psmouse);
192 return PSMOUSE_FULL_PACKET;
193 }
194
195 if (priv->recalib_window) {
196 if (time_before(jiffies, priv->recalib_window)) {
197 /*
198 * ugh, got a packet inside our recalibration
199 * window, schedule another recalibration.
200 */
201 hgpk_dbg(psmouse,
202 "packet inside calibration window, "
203 "queueing another recalibration\n");
204 psmouse_queue_work(psmouse, &priv->recalib_wq,
8bbf2703 205 msecs_to_jiffies(post_interrupt_delay));
df08ef27
AS
206 }
207 priv->recalib_window = 0;
208 }
209
210 return PSMOUSE_GOOD_DATA;
211}
212
213static int hgpk_force_recalibrate(struct psmouse *psmouse)
214{
215 struct ps2dev *ps2dev = &psmouse->ps2dev;
216 struct hgpk_data *priv = psmouse->private;
217
218 /* C-series touchpads added the recalibrate command */
219 if (psmouse->model < HGPK_MODEL_C)
220 return 0;
221
222 /* we don't want to race with the irq handler, nor with resyncs */
223 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
224
225 /* start by resetting the device */
226 psmouse_reset(psmouse);
227
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)) {
233 return -1;
234 }
235
236 /* according to ALPS, 150mS is required for recalibration */
237 msleep(150);
238
239 /* XXX: If a finger is down during this delay, recalibration will
240 * detect capacitance incorrectly. This is a hardware bug, and
241 * we don't have a good way to deal with it. The 2s window stuff
242 * (below) is our best option for now.
243 */
244
245 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
246 return -1;
247
248 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
249
250 /* After we recalibrate, we shouldn't get any packets for 2s. If
251 * we do, it's likely that someone's finger was on the touchpad.
252 * If someone's finger *was* on the touchpad, it's probably
253 * miscalibrated. So, we should schedule another recalibration
254 */
8bbf2703 255 priv->recalib_window = jiffies + msecs_to_jiffies(recal_guard_time);
df08ef27
AS
256
257 return 0;
258}
259
260/*
261 * This kills power to the touchpad; according to ALPS, current consumption
262 * goes down to 50uA after running this. To turn power back on, we drive
263 * MS-DAT low.
264 */
265static int hgpk_toggle_power(struct psmouse *psmouse, int enable)
266{
267 struct ps2dev *ps2dev = &psmouse->ps2dev;
268 int timeo;
269
270 /* Added on D-series touchpads */
271 if (psmouse->model < HGPK_MODEL_D)
272 return 0;
273
274 if (enable) {
275 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
276
277 /*
278 * Sending a byte will drive MS-DAT low; this will wake up
279 * the controller. Once we get an ACK back from it, it
280 * means we can continue with the touchpad re-init. ALPS
281 * tells us that 1s should be long enough, so set that as
282 * the upper bound.
283 */
284 for (timeo = 20; timeo > 0; timeo--) {
285 if (!ps2_sendbyte(&psmouse->ps2dev,
286 PSMOUSE_CMD_DISABLE, 20))
287 break;
288 msleep(50);
289 }
290
291 psmouse_reset(psmouse);
292
293 /* should be all set, enable the touchpad */
294 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE);
295 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
296
297 } else {
298 hgpk_dbg(psmouse, "Powering off touchpad.\n");
299 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
300
301 if (ps2_command(ps2dev, NULL, 0xec) ||
302 ps2_command(ps2dev, NULL, 0xec) ||
303 ps2_command(ps2dev, NULL, 0xea)) {
304 return -1;
305 }
306
307 /* probably won't see an ACK, the touchpad will be off */
308 ps2_sendbyte(&psmouse->ps2dev, 0xec, 20);
309 }
310
311 return 0;
312}
313
314static int hgpk_poll(struct psmouse *psmouse)
315{
316 /* We can't poll, so always return failure. */
317 return -1;
318}
319
320static int hgpk_reconnect(struct psmouse *psmouse)
321{
322 /* During suspend/resume the ps2 rails remain powered. We don't want
323 * to do a reset because it's flush data out of buffers; however,
324 * earlier prototypes (B1) had some brokenness that required a reset. */
325 if (olpc_board_at_least(olpc_board(0xb2)))
326 if (psmouse->ps2dev.serio->dev.power.power_state.event !=
327 PM_EVENT_ON)
328 return 0;
329
330 psmouse_reset(psmouse);
331
332 return 0;
333}
334
335static ssize_t hgpk_show_powered(struct psmouse *psmouse, void *data, char *buf)
336{
337 struct hgpk_data *priv = psmouse->private;
338
339 return sprintf(buf, "%d\n", priv->powered);
340}
341
342static ssize_t hgpk_set_powered(struct psmouse *psmouse, void *data,
343 const char *buf, size_t count)
344{
345 struct hgpk_data *priv = psmouse->private;
346 unsigned long value;
347 int err;
348
349 err = strict_strtoul(buf, 10, &value);
350 if (err || value > 1)
351 return -EINVAL;
352
353 if (value != priv->powered) {
354 /*
355 * hgpk_toggle_power will deal w/ state so
356 * we're not racing w/ irq
357 */
358 err = hgpk_toggle_power(psmouse, value);
359 if (!err)
360 priv->powered = value;
361 }
362
363 return err ? err : count;
364}
365
366__PSMOUSE_DEFINE_ATTR(powered, S_IWUSR | S_IRUGO, NULL,
b7802c5c 367 hgpk_show_powered, hgpk_set_powered, false);
df08ef27 368
c46dd1eb
PF
369static ssize_t hgpk_trigger_recal_show(struct psmouse *psmouse,
370 void *data, char *buf)
371{
372 return -EINVAL;
373}
374
375static ssize_t hgpk_trigger_recal(struct psmouse *psmouse, void *data,
376 const char *buf, size_t count)
377{
378 struct hgpk_data *priv = psmouse->private;
379 unsigned long value;
380 int err;
381
382 err = strict_strtoul(buf, 10, &value);
383 if (err || value != 1)
384 return -EINVAL;
385
386 /*
387 * We queue work instead of doing recalibration right here
388 * to avoid adding locking to to hgpk_force_recalibrate()
389 * since workqueue provides serialization.
390 */
391 psmouse_queue_work(psmouse, &priv->recalib_wq, 0);
392 return count;
393}
394
395__PSMOUSE_DEFINE_ATTR(recalibrate, S_IWUSR | S_IRUGO, NULL,
b7802c5c 396 hgpk_trigger_recal_show, hgpk_trigger_recal, false);
c46dd1eb 397
df08ef27
AS
398static void hgpk_disconnect(struct psmouse *psmouse)
399{
400 struct hgpk_data *priv = psmouse->private;
401
402 device_remove_file(&psmouse->ps2dev.serio->dev,
403 &psmouse_attr_powered.dattr);
c46dd1eb
PF
404
405 if (psmouse->model >= HGPK_MODEL_C)
406 device_remove_file(&psmouse->ps2dev.serio->dev,
407 &psmouse_attr_recalibrate.dattr);
408
df08ef27
AS
409 psmouse_reset(psmouse);
410 kfree(priv);
411}
412
413static void hgpk_recalib_work(struct work_struct *work)
414{
bf6aede7 415 struct delayed_work *w = to_delayed_work(work);
df08ef27
AS
416 struct hgpk_data *priv = container_of(w, struct hgpk_data, recalib_wq);
417 struct psmouse *psmouse = priv->psmouse;
418
419 hgpk_dbg(psmouse, "recalibrating touchpad..\n");
420
421 if (hgpk_force_recalibrate(psmouse))
422 hgpk_err(psmouse, "recalibration failed!\n");
423}
424
425static int hgpk_register(struct psmouse *psmouse)
426{
df08ef27
AS
427 int err;
428
df08ef27
AS
429 /* register handlers */
430 psmouse->protocol_handler = hgpk_process_byte;
431 psmouse->poll = hgpk_poll;
432 psmouse->disconnect = hgpk_disconnect;
433 psmouse->reconnect = hgpk_reconnect;
434 psmouse->pktsize = 3;
435
436 /* Disable the idle resync. */
437 psmouse->resync_time = 0;
438 /* Reset after a lot of bad bytes. */
439 psmouse->resetafter = 1024;
440
441 err = device_create_file(&psmouse->ps2dev.serio->dev,
442 &psmouse_attr_powered.dattr);
c46dd1eb
PF
443 if (err) {
444 hgpk_err(psmouse, "Failed creating 'powered' sysfs node\n");
445 return err;
446 }
447
448 /* C-series touchpads added the recalibrate command */
449 if (psmouse->model >= HGPK_MODEL_C) {
450 err = device_create_file(&psmouse->ps2dev.serio->dev,
451 &psmouse_attr_recalibrate.dattr);
452 if (err) {
453 hgpk_err(psmouse,
454 "Failed creating 'recalibrate' sysfs node\n");
455 device_remove_file(&psmouse->ps2dev.serio->dev,
456 &psmouse_attr_powered.dattr);
457 return err;
458 }
459 }
df08ef27 460
c46dd1eb 461 return 0;
df08ef27
AS
462}
463
464int hgpk_init(struct psmouse *psmouse)
465{
466 struct hgpk_data *priv;
467 int err = -ENOMEM;
468
469 priv = kzalloc(sizeof(struct hgpk_data), GFP_KERNEL);
470 if (!priv)
471 goto alloc_fail;
472
473 psmouse->private = priv;
474 priv->psmouse = psmouse;
b7802c5c 475 priv->powered = true;
df08ef27
AS
476 INIT_DELAYED_WORK(&priv->recalib_wq, hgpk_recalib_work);
477
478 err = psmouse_reset(psmouse);
479 if (err)
480 goto init_fail;
481
482 err = hgpk_register(psmouse);
483 if (err)
484 goto init_fail;
485
486 return 0;
487
488init_fail:
489 kfree(priv);
490alloc_fail:
491 return err;
492}
493
494static enum hgpk_model_t hgpk_get_model(struct psmouse *psmouse)
495{
496 struct ps2dev *ps2dev = &psmouse->ps2dev;
497 unsigned char param[3];
498
499 /* E7, E7, E7, E9 gets us a 3 byte identifier */
500 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
501 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
502 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
503 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) {
504 return -EIO;
505 }
506
0f495481 507 hgpk_dbg(psmouse, "ID: %02x %02x %02x\n", param[0], param[1], param[2]);
df08ef27
AS
508
509 /* HGPK signature: 0x67, 0x00, 0x<model> */
510 if (param[0] != 0x67 || param[1] != 0x00)
511 return -ENODEV;
512
513 hgpk_info(psmouse, "OLPC touchpad revision 0x%x\n", param[2]);
514
515 return param[2];
516}
517
b7802c5c 518int hgpk_detect(struct psmouse *psmouse, bool set_properties)
df08ef27
AS
519{
520 int version;
521
522 version = hgpk_get_model(psmouse);
523 if (version < 0)
524 return version;
525
526 if (set_properties) {
527 psmouse->vendor = "ALPS";
528 psmouse->name = "HGPK";
529 psmouse->model = version;
530 }
531
532 return 0;
533}