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