blob: 95577c15ae56b68f832381f29f158dd7d81d34eb [file] [log] [blame]
Andres Salomondf08ef22008-09-16 12:30:34 -04001/*
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
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Andres Salomondf08ef22008-09-16 12:30:34 -040034#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
Daniel Drakea309cdc2010-11-11 22:20:03 -080043#define ILLEGAL_XY 999999
44
Dmitry Torokhova62f0d22010-05-19 10:39:17 -070045static bool tpdebug;
46module_param(tpdebug, bool, 0644);
Andres Salomondf08ef22008-09-16 12:30:34 -040047MODULE_PARM_DESC(tpdebug, "enable debugging, dumping packets to KERN_DEBUG.");
48
49static int recalib_delta = 100;
50module_param(recalib_delta, int, 0644);
51MODULE_PARM_DESC(recalib_delta,
Daniel Drakea309cdc2010-11-11 22:20:03 -080052 "packets containing a delta this large will be discarded, and a "
53 "recalibration may be scheduled.");
Andres Salomondf08ef22008-09-16 12:30:34 -040054
Daniel Drakea309cdc2010-11-11 22:20:03 -080055static int jumpy_delay = 20;
Paul Fox8bbf2702008-12-20 03:58:11 -050056module_param(jumpy_delay, int, 0644);
57MODULE_PARM_DESC(jumpy_delay,
58 "delay (ms) before recal after jumpiness detected");
59
Daniel Drakec0dc8342010-11-11 22:20:02 -080060static int spew_delay = 1;
Paul Fox8bbf2702008-12-20 03:58:11 -050061module_param(spew_delay, int, 0644);
62MODULE_PARM_DESC(spew_delay,
63 "delay (ms) before recal after packet spew detected");
64
Daniel Drake34caed22010-11-15 01:33:22 -080065static int recal_guard_time;
Paul Fox8bbf2702008-12-20 03:58:11 -050066module_param(recal_guard_time, int, 0644);
67MODULE_PARM_DESC(recal_guard_time,
68 "interval (ms) during which recal will be restarted if packet received");
69
Daniel Drake34caed22010-11-15 01:33:22 -080070static int post_interrupt_delay = 40;
Paul Fox8bbf2702008-12-20 03:58:11 -050071module_param(post_interrupt_delay, int, 0644);
72MODULE_PARM_DESC(post_interrupt_delay,
73 "delay (ms) before recal after recal interrupt detected");
74
Daniel Drake34caed22010-11-15 01:33:22 -080075static bool autorecal = true;
76module_param(autorecal, bool, 0644);
77MODULE_PARM_DESC(autorecal, "enable recalibration in the driver");
78
Daniel Drakeca94ec42010-11-11 22:19:57 -080079static char hgpk_mode_name[16];
80module_param_string(hgpk_mode, hgpk_mode_name, sizeof(hgpk_mode_name), 0644);
81MODULE_PARM_DESC(hgpk_mode,
82 "default hgpk mode: mouse, glidesensor or pentablet");
83
84static int hgpk_default_mode = HGPK_MODE_MOUSE;
85
86static const char * const hgpk_mode_names[] = {
87 [HGPK_MODE_MOUSE] = "Mouse",
88 [HGPK_MODE_GLIDESENSOR] = "GlideSensor",
89 [HGPK_MODE_PENTABLET] = "PenTablet",
90};
91
92static int hgpk_mode_from_name(const char *buf, int len)
93{
94 int i;
95
96 for (i = 0; i < ARRAY_SIZE(hgpk_mode_names); i++) {
97 const char *name = hgpk_mode_names[i];
98 if (strlen(name) == len && !strncasecmp(name, buf, len))
99 return i;
100 }
101
102 return HGPK_MODE_INVALID;
103}
104
Andres Salomondf08ef22008-09-16 12:30:34 -0400105/*
Daniel Drakea309cdc2010-11-11 22:20:03 -0800106 * see if new value is within 20% of half of old value
Andres Salomondf08ef22008-09-16 12:30:34 -0400107 */
Daniel Drakea309cdc2010-11-11 22:20:03 -0800108static int approx_half(int curr, int prev)
109{
110 int belowhalf, abovehalf;
111
112 if (curr < 5 || prev < 5)
113 return 0;
114
115 belowhalf = (prev * 8) / 20;
116 abovehalf = (prev * 12) / 20;
117
118 return belowhalf < curr && curr <= abovehalf;
119}
120
121/*
122 * Throw out oddly large delta packets, and any that immediately follow whose
123 * values are each approximately half of the previous. It seems that the ALPS
124 * firmware emits errant packets, and they get averaged out slowly.
125 */
126static int hgpk_discard_decay_hack(struct psmouse *psmouse, int x, int y)
Andres Salomondf08ef22008-09-16 12:30:34 -0400127{
128 struct hgpk_data *priv = psmouse->private;
Daniel Drakea309cdc2010-11-11 22:20:03 -0800129 int avx, avy;
130 bool do_recal = false;
Andres Salomondf08ef22008-09-16 12:30:34 -0400131
Daniel Drakea309cdc2010-11-11 22:20:03 -0800132 avx = abs(x);
133 avy = abs(y);
134
135 /* discard if too big, or half that but > 4 times the prev delta */
136 if (avx > recalib_delta ||
137 (avx > recalib_delta / 2 && ((avx / 4) > priv->xlast))) {
138 hgpk_err(psmouse, "detected %dpx jump in x\n", x);
139 priv->xbigj = avx;
140 } else if (approx_half(avx, priv->xbigj)) {
141 hgpk_err(psmouse, "detected secondary %dpx jump in x\n", x);
142 priv->xbigj = avx;
143 priv->xsaw_secondary++;
144 } else {
145 if (priv->xbigj && priv->xsaw_secondary > 1)
146 do_recal = true;
147 priv->xbigj = 0;
148 priv->xsaw_secondary = 0;
149 }
150
151 if (avy > recalib_delta ||
152 (avy > recalib_delta / 2 && ((avy / 4) > priv->ylast))) {
153 hgpk_err(psmouse, "detected %dpx jump in y\n", y);
154 priv->ybigj = avy;
155 } else if (approx_half(avy, priv->ybigj)) {
156 hgpk_err(psmouse, "detected secondary %dpx jump in y\n", y);
157 priv->ybigj = avy;
158 priv->ysaw_secondary++;
159 } else {
160 if (priv->ybigj && priv->ysaw_secondary > 1)
161 do_recal = true;
162 priv->ybigj = 0;
163 priv->ysaw_secondary = 0;
164 }
165
166 priv->xlast = avx;
167 priv->ylast = avy;
168
169 if (do_recal && jumpy_delay) {
170 hgpk_err(psmouse, "scheduling recalibration\n");
Andres Salomondf08ef22008-09-16 12:30:34 -0400171 psmouse_queue_work(psmouse, &priv->recalib_wq,
Paul Fox8bbf2702008-12-20 03:58:11 -0500172 msecs_to_jiffies(jumpy_delay));
Andres Salomondf08ef22008-09-16 12:30:34 -0400173 }
Daniel Drakea309cdc2010-11-11 22:20:03 -0800174
175 return priv->xbigj || priv->ybigj;
Andres Salomondf08ef22008-09-16 12:30:34 -0400176}
177
Daniel Drakec0dc8342010-11-11 22:20:02 -0800178static void hgpk_reset_spew_detection(struct hgpk_data *priv)
179{
180 priv->spew_count = 0;
181 priv->dupe_count = 0;
182 priv->x_tally = 0;
183 priv->y_tally = 0;
184 priv->spew_flag = NO_SPEW;
185}
186
187static void hgpk_reset_hack_state(struct psmouse *psmouse)
188{
189 struct hgpk_data *priv = psmouse->private;
190
191 priv->abs_x = priv->abs_y = -1;
Daniel Drakea309cdc2010-11-11 22:20:03 -0800192 priv->xlast = priv->ylast = ILLEGAL_XY;
193 priv->xbigj = priv->ybigj = 0;
194 priv->xsaw_secondary = priv->ysaw_secondary = 0;
Daniel Drakec0dc8342010-11-11 22:20:02 -0800195 hgpk_reset_spew_detection(priv);
196}
197
Andres Salomondf08ef22008-09-16 12:30:34 -0400198/*
199 * We have no idea why this particular hardware bug occurs. The touchpad
200 * will randomly start spewing packets without anything touching the
201 * pad. This wouldn't necessarily be bad, but it's indicative of a
202 * severely miscalibrated pad; attempting to use the touchpad while it's
203 * spewing means the cursor will jump all over the place, and act "drunk".
204 *
205 * The packets that are spewed tend to all have deltas between -2 and 2, and
206 * the cursor will move around without really going very far. It will
207 * tend to end up in the same location; if we tally up the changes over
208 * 100 packets, we end up w/ a final delta of close to 0. This happens
209 * pretty regularly when the touchpad is spewing, and is pretty hard to
210 * manually trigger (at least for *my* fingers). So, it makes a perfect
211 * scheme for detecting spews.
212 */
213static void hgpk_spewing_hack(struct psmouse *psmouse,
214 int l, int r, int x, int y)
215{
216 struct hgpk_data *priv = psmouse->private;
217
218 /* ignore button press packets; many in a row could trigger
219 * a false-positive! */
220 if (l || r)
221 return;
222
Daniel Drakec0dc8342010-11-11 22:20:02 -0800223 /* don't track spew if the workaround feature has been turned off */
224 if (!spew_delay)
225 return;
226
227 if (abs(x) > 3 || abs(y) > 3) {
228 /* no spew, or spew ended */
229 hgpk_reset_spew_detection(priv);
230 return;
231 }
232
233 /* Keep a tally of the overall delta to the cursor position caused by
234 * the spew */
Andres Salomondf08ef22008-09-16 12:30:34 -0400235 priv->x_tally += x;
236 priv->y_tally += y;
237
Daniel Drakec0dc8342010-11-11 22:20:02 -0800238 switch (priv->spew_flag) {
239 case NO_SPEW:
240 /* we're not spewing, but this packet might be the start */
241 priv->spew_flag = MAYBE_SPEWING;
242
243 /* fall-through */
244
245 case MAYBE_SPEWING:
246 priv->spew_count++;
247
248 if (priv->spew_count < SPEW_WATCH_COUNT)
249 break;
250
251 /* excessive spew detected, request recalibration */
252 priv->spew_flag = SPEW_DETECTED;
253
254 /* fall-through */
255
256 case SPEW_DETECTED:
257 /* only recalibrate when the overall delta to the cursor
258 * is really small. if the spew is causing significant cursor
259 * movement, it is probably a case of the user moving the
260 * cursor very slowly across the screen. */
Andres Salomondf08ef22008-09-16 12:30:34 -0400261 if (abs(priv->x_tally) < 3 && abs(priv->y_tally) < 3) {
Daniel Drakec0dc8342010-11-11 22:20:02 -0800262 hgpk_err(psmouse, "packet spew detected (%d,%d)\n",
Andres Salomondf08ef22008-09-16 12:30:34 -0400263 priv->x_tally, priv->y_tally);
Daniel Drakec0dc8342010-11-11 22:20:02 -0800264 priv->spew_flag = RECALIBRATING;
Andres Salomondf08ef22008-09-16 12:30:34 -0400265 psmouse_queue_work(psmouse, &priv->recalib_wq,
Paul Fox8bbf2702008-12-20 03:58:11 -0500266 msecs_to_jiffies(spew_delay));
Andres Salomondf08ef22008-09-16 12:30:34 -0400267 }
Daniel Drakec0dc8342010-11-11 22:20:02 -0800268
269 break;
270 case RECALIBRATING:
271 /* we already detected a spew and requested a recalibration,
272 * just wait for the queue to kick into action. */
273 break;
Andres Salomondf08ef22008-09-16 12:30:34 -0400274 }
275}
276
277/*
278 * HGPK Mouse Mode format (standard mouse format, sans middle button)
279 *
280 * byte 0: y-over x-over y-neg x-neg 1 0 swr swl
281 * byte 1: x7 x6 x5 x4 x3 x2 x1 x0
282 * byte 2: y7 y6 y5 y4 y3 y2 y1 y0
283 *
284 * swr/swl are the left/right buttons.
285 * x-neg/y-neg are the x and y delta negative bits
286 * x-over/y-over are the x and y overflow bits
Daniel Drakeca94ec42010-11-11 22:19:57 -0800287 *
288 * ---
289 *
290 * HGPK Advanced Mode - single-mode format
291 *
292 * byte 0(PT): 1 1 0 0 1 1 1 1
293 * byte 0(GS): 1 1 1 1 1 1 1 1
294 * byte 1: 0 x6 x5 x4 x3 x2 x1 x0
295 * byte 2(PT): 0 0 x9 x8 x7 ? pt-dsw 0
296 * byte 2(GS): 0 x10 x9 x8 x7 ? gs-dsw pt-dsw
297 * byte 3: 0 y9 y8 y7 1 0 swr swl
298 * byte 4: 0 y6 y5 y4 y3 y2 y1 y0
299 * byte 5: 0 z6 z5 z4 z3 z2 z1 z0
300 *
301 * ?'s are not defined in the protocol spec, may vary between models.
302 *
303 * swr/swl are the left/right buttons.
304 *
305 * pt-dsw/gs-dsw indicate that the pt/gs sensor is detecting a
306 * pen/finger
Andres Salomondf08ef22008-09-16 12:30:34 -0400307 */
Daniel Drakeca94ec42010-11-11 22:19:57 -0800308static bool hgpk_is_byte_valid(struct psmouse *psmouse, unsigned char *packet)
Andres Salomondf08ef22008-09-16 12:30:34 -0400309{
Daniel Drakeca94ec42010-11-11 22:19:57 -0800310 struct hgpk_data *priv = psmouse->private;
311 int pktcnt = psmouse->pktcnt;
312 bool valid;
313
314 switch (priv->mode) {
315 case HGPK_MODE_MOUSE:
316 valid = (packet[0] & 0x0C) == 0x08;
317 break;
318
319 case HGPK_MODE_GLIDESENSOR:
320 valid = pktcnt == 1 ?
321 packet[0] == HGPK_GS : !(packet[pktcnt - 1] & 0x80);
322 break;
323
324 case HGPK_MODE_PENTABLET:
325 valid = pktcnt == 1 ?
326 packet[0] == HGPK_PT : !(packet[pktcnt - 1] & 0x80);
327 break;
328
329 default:
330 valid = false;
331 break;
332 }
333
334 if (!valid)
335 hgpk_dbg(psmouse,
336 "bad data, mode %d (%d) %02x %02x %02x %02x %02x %02x\n",
337 priv->mode, pktcnt,
338 psmouse->packet[0], psmouse->packet[1],
339 psmouse->packet[2], psmouse->packet[3],
340 psmouse->packet[4], psmouse->packet[5]);
341
342 return valid;
Andres Salomondf08ef22008-09-16 12:30:34 -0400343}
344
Daniel Drakeca94ec42010-11-11 22:19:57 -0800345static void hgpk_process_advanced_packet(struct psmouse *psmouse)
346{
347 struct hgpk_data *priv = psmouse->private;
348 struct input_dev *idev = psmouse->dev;
349 unsigned char *packet = psmouse->packet;
350 int down = !!(packet[2] & 2);
351 int left = !!(packet[3] & 1);
352 int right = !!(packet[3] & 2);
353 int x = packet[1] | ((packet[2] & 0x78) << 4);
354 int y = packet[4] | ((packet[3] & 0x70) << 3);
355
356 if (priv->mode == HGPK_MODE_GLIDESENSOR) {
357 int pt_down = !!(packet[2] & 1);
358 int finger_down = !!(packet[2] & 2);
359 int z = packet[5];
360
361 input_report_abs(idev, ABS_PRESSURE, z);
362 if (tpdebug)
363 hgpk_dbg(psmouse, "pd=%d fd=%d z=%d",
364 pt_down, finger_down, z);
365 } else {
366 /*
367 * PenTablet mode does not report pressure, so we don't
368 * report it here
369 */
370 if (tpdebug)
371 hgpk_dbg(psmouse, "pd=%d ", down);
372 }
373
374 if (tpdebug)
375 hgpk_dbg(psmouse, "l=%d r=%d x=%d y=%d\n", left, right, x, y);
376
377 input_report_key(idev, BTN_TOUCH, down);
378 input_report_key(idev, BTN_LEFT, left);
379 input_report_key(idev, BTN_RIGHT, right);
380
381 /*
382 * If this packet says that the finger was removed, reset our position
383 * tracking so that we don't erroneously detect a jump on next press.
384 */
Daniel Drakec0dc8342010-11-11 22:20:02 -0800385 if (!down) {
Daniel Drakea309cdc2010-11-11 22:20:03 -0800386 hgpk_reset_hack_state(psmouse);
Daniel Drakec0dc8342010-11-11 22:20:02 -0800387 goto done;
Daniel Drakeca94ec42010-11-11 22:19:57 -0800388 }
389
Daniel Drakec0dc8342010-11-11 22:20:02 -0800390 /*
391 * Weed out duplicate packets (we get quite a few, and they mess up
392 * our jump detection)
393 */
394 if (x == priv->abs_x && y == priv->abs_y) {
395 if (++priv->dupe_count > SPEW_WATCH_COUNT) {
396 if (tpdebug)
397 hgpk_dbg(psmouse, "hard spew detected\n");
398 priv->spew_flag = RECALIBRATING;
399 psmouse_queue_work(psmouse, &priv->recalib_wq,
400 msecs_to_jiffies(spew_delay));
401 }
402 goto done;
403 }
404
405 /* not a duplicate, continue with position reporting */
406 priv->dupe_count = 0;
407
408 /* Don't apply hacks in PT mode, it seems reliable */
409 if (priv->mode != HGPK_MODE_PENTABLET && priv->abs_x != -1) {
Daniel Drakea309cdc2010-11-11 22:20:03 -0800410 int x_diff = priv->abs_x - x;
411 int y_diff = priv->abs_y - y;
412 if (hgpk_discard_decay_hack(psmouse, x_diff, y_diff)) {
413 if (tpdebug)
414 hgpk_dbg(psmouse, "discarding\n");
415 goto done;
416 }
417 hgpk_spewing_hack(psmouse, left, right, x_diff, y_diff);
Daniel Drakec0dc8342010-11-11 22:20:02 -0800418 }
419
420 input_report_abs(idev, ABS_X, x);
421 input_report_abs(idev, ABS_Y, y);
422 priv->abs_x = x;
423 priv->abs_y = y;
424
425done:
Daniel Drakeca94ec42010-11-11 22:19:57 -0800426 input_sync(idev);
427}
428
429static void hgpk_process_simple_packet(struct psmouse *psmouse)
Andres Salomondf08ef22008-09-16 12:30:34 -0400430{
431 struct input_dev *dev = psmouse->dev;
432 unsigned char *packet = psmouse->packet;
Daniel Drakeca94ec42010-11-11 22:19:57 -0800433 int left = packet[0] & 1;
434 int right = (packet[0] >> 1) & 1;
435 int x = packet[1] - ((packet[0] << 4) & 0x100);
436 int y = ((packet[0] << 3) & 0x100) - packet[2];
Andres Salomondf08ef22008-09-16 12:30:34 -0400437
Daniel Drake67f56bb2010-11-15 01:28:54 -0800438 if (packet[0] & 0xc0)
439 hgpk_dbg(psmouse,
440 "overflow -- 0x%02x 0x%02x 0x%02x\n",
441 packet[0], packet[1], packet[2]);
442
Daniel Drakea309cdc2010-11-11 22:20:03 -0800443 if (hgpk_discard_decay_hack(psmouse, x, y)) {
444 if (tpdebug)
445 hgpk_dbg(psmouse, "discarding\n");
446 return;
447 }
448
Andres Salomondf08ef22008-09-16 12:30:34 -0400449 hgpk_spewing_hack(psmouse, left, right, x, y);
450
451 if (tpdebug)
452 hgpk_dbg(psmouse, "l=%d r=%d x=%d y=%d\n", left, right, x, y);
453
454 input_report_key(dev, BTN_LEFT, left);
455 input_report_key(dev, BTN_RIGHT, right);
456
457 input_report_rel(dev, REL_X, x);
458 input_report_rel(dev, REL_Y, y);
459
460 input_sync(dev);
461}
462
463static psmouse_ret_t hgpk_process_byte(struct psmouse *psmouse)
464{
465 struct hgpk_data *priv = psmouse->private;
466
Daniel Drakeca94ec42010-11-11 22:19:57 -0800467 if (!hgpk_is_byte_valid(psmouse, psmouse->packet))
Andres Salomondf08ef22008-09-16 12:30:34 -0400468 return PSMOUSE_BAD_DATA;
Andres Salomondf08ef22008-09-16 12:30:34 -0400469
470 if (psmouse->pktcnt >= psmouse->pktsize) {
Daniel Drakeca94ec42010-11-11 22:19:57 -0800471 if (priv->mode == HGPK_MODE_MOUSE)
472 hgpk_process_simple_packet(psmouse);
473 else
474 hgpk_process_advanced_packet(psmouse);
Andres Salomondf08ef22008-09-16 12:30:34 -0400475 return PSMOUSE_FULL_PACKET;
476 }
477
478 if (priv->recalib_window) {
479 if (time_before(jiffies, priv->recalib_window)) {
480 /*
481 * ugh, got a packet inside our recalibration
482 * window, schedule another recalibration.
483 */
484 hgpk_dbg(psmouse,
485 "packet inside calibration window, "
486 "queueing another recalibration\n");
487 psmouse_queue_work(psmouse, &priv->recalib_wq,
Paul Fox8bbf2702008-12-20 03:58:11 -0500488 msecs_to_jiffies(post_interrupt_delay));
Andres Salomondf08ef22008-09-16 12:30:34 -0400489 }
490 priv->recalib_window = 0;
491 }
492
493 return PSMOUSE_GOOD_DATA;
494}
495
Daniel Drakeca94ec42010-11-11 22:19:57 -0800496static int hgpk_select_mode(struct psmouse *psmouse)
497{
498 struct ps2dev *ps2dev = &psmouse->ps2dev;
499 struct hgpk_data *priv = psmouse->private;
500 int i;
501 int cmd;
502
503 /*
504 * 4 disables to enable advanced mode
505 * then 3 0xf2 bytes as the preamble for GS/PT selection
506 */
507 const int advanced_init[] = {
508 PSMOUSE_CMD_DISABLE, PSMOUSE_CMD_DISABLE,
509 PSMOUSE_CMD_DISABLE, PSMOUSE_CMD_DISABLE,
510 0xf2, 0xf2, 0xf2,
511 };
512
513 switch (priv->mode) {
514 case HGPK_MODE_MOUSE:
515 psmouse->pktsize = 3;
516 break;
517
518 case HGPK_MODE_GLIDESENSOR:
519 case HGPK_MODE_PENTABLET:
520 psmouse->pktsize = 6;
521
522 /* Switch to 'Advanced mode.', four disables in a row. */
523 for (i = 0; i < ARRAY_SIZE(advanced_init); i++)
524 if (ps2_command(ps2dev, NULL, advanced_init[i]))
525 return -EIO;
526
527 /* select between GlideSensor (mouse) or PenTablet */
528 cmd = priv->mode == HGPK_MODE_GLIDESENSOR ?
529 PSMOUSE_CMD_SETSCALE11 : PSMOUSE_CMD_SETSCALE21;
530
531 if (ps2_command(ps2dev, NULL, cmd))
532 return -EIO;
533 break;
534
535 default:
536 return -EINVAL;
537 }
538
539 return 0;
540}
541
542static void hgpk_setup_input_device(struct input_dev *input,
543 struct input_dev *old_input,
544 enum hgpk_mode mode)
545{
546 if (old_input) {
547 input->name = old_input->name;
548 input->phys = old_input->phys;
549 input->id = old_input->id;
550 input->dev.parent = old_input->dev.parent;
551 }
552
553 memset(input->evbit, 0, sizeof(input->evbit));
554 memset(input->relbit, 0, sizeof(input->relbit));
555 memset(input->keybit, 0, sizeof(input->keybit));
556
557 /* All modes report left and right buttons */
558 __set_bit(EV_KEY, input->evbit);
559 __set_bit(BTN_LEFT, input->keybit);
560 __set_bit(BTN_RIGHT, input->keybit);
561
562 switch (mode) {
563 case HGPK_MODE_MOUSE:
564 __set_bit(EV_REL, input->evbit);
565 __set_bit(REL_X, input->relbit);
566 __set_bit(REL_Y, input->relbit);
567 break;
568
569 case HGPK_MODE_GLIDESENSOR:
570 __set_bit(BTN_TOUCH, input->keybit);
571 __set_bit(BTN_TOOL_FINGER, input->keybit);
572
573 __set_bit(EV_ABS, input->evbit);
574
575 /* GlideSensor has pressure sensor, PenTablet does not */
576 input_set_abs_params(input, ABS_PRESSURE, 0, 15, 0, 0);
577
578 /* From device specs */
579 input_set_abs_params(input, ABS_X, 0, 399, 0, 0);
580 input_set_abs_params(input, ABS_Y, 0, 290, 0, 0);
581
582 /* Calculated by hand based on usable size (52mm x 38mm) */
583 input_abs_set_res(input, ABS_X, 8);
584 input_abs_set_res(input, ABS_Y, 8);
585 break;
586
587 case HGPK_MODE_PENTABLET:
588 __set_bit(BTN_TOUCH, input->keybit);
589 __set_bit(BTN_TOOL_FINGER, input->keybit);
590
591 __set_bit(EV_ABS, input->evbit);
592
593 /* From device specs */
594 input_set_abs_params(input, ABS_X, 0, 999, 0, 0);
595 input_set_abs_params(input, ABS_Y, 5, 239, 0, 0);
596
597 /* Calculated by hand based on usable size (156mm x 38mm) */
598 input_abs_set_res(input, ABS_X, 6);
599 input_abs_set_res(input, ABS_Y, 8);
600 break;
601
602 default:
603 BUG();
604 }
605}
606
Daniel Drakeca94ec42010-11-11 22:19:57 -0800607static int hgpk_reset_device(struct psmouse *psmouse, bool recalibrate)
608{
609 int err;
610
611 psmouse_reset(psmouse);
612
613 if (recalibrate) {
614 struct ps2dev *ps2dev = &psmouse->ps2dev;
615
616 /* send the recalibrate request */
617 if (ps2_command(ps2dev, NULL, 0xf5) ||
618 ps2_command(ps2dev, NULL, 0xf5) ||
619 ps2_command(ps2dev, NULL, 0xe6) ||
620 ps2_command(ps2dev, NULL, 0xf5)) {
621 return -1;
622 }
623
624 /* according to ALPS, 150mS is required for recalibration */
625 msleep(150);
626 }
627
628 err = hgpk_select_mode(psmouse);
629 if (err) {
630 hgpk_err(psmouse, "failed to select mode\n");
631 return err;
632 }
633
634 hgpk_reset_hack_state(psmouse);
635
636 return 0;
637}
638
Andres Salomondf08ef22008-09-16 12:30:34 -0400639static int hgpk_force_recalibrate(struct psmouse *psmouse)
640{
641 struct ps2dev *ps2dev = &psmouse->ps2dev;
642 struct hgpk_data *priv = psmouse->private;
Daniel Drakeca94ec42010-11-11 22:19:57 -0800643 int err;
Andres Salomondf08ef22008-09-16 12:30:34 -0400644
645 /* C-series touchpads added the recalibrate command */
646 if (psmouse->model < HGPK_MODEL_C)
647 return 0;
648
Daniel Drake34caed22010-11-15 01:33:22 -0800649 if (!autorecal) {
650 hgpk_dbg(psmouse, "recalibrations disabled, ignoring\n");
651 return 0;
652 }
653
654 hgpk_dbg(psmouse, "recalibrating touchpad..\n");
655
Andres Salomondf08ef22008-09-16 12:30:34 -0400656 /* we don't want to race with the irq handler, nor with resyncs */
657 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
658
659 /* start by resetting the device */
Daniel Drakeca94ec42010-11-11 22:19:57 -0800660 err = hgpk_reset_device(psmouse, true);
661 if (err)
662 return err;
Andres Salomondf08ef22008-09-16 12:30:34 -0400663
Daniel Drakeca94ec42010-11-11 22:19:57 -0800664 /*
665 * XXX: If a finger is down during this delay, recalibration will
Andres Salomondf08ef22008-09-16 12:30:34 -0400666 * detect capacitance incorrectly. This is a hardware bug, and
667 * we don't have a good way to deal with it. The 2s window stuff
668 * (below) is our best option for now.
669 */
670
671 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
672 return -1;
673
674 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
675
Daniel Drake34caed22010-11-15 01:33:22 -0800676 if (tpdebug)
677 hgpk_dbg(psmouse, "touchpad reactivated\n");
678
Daniel Drakeca94ec42010-11-11 22:19:57 -0800679 /*
Daniel Drake34caed22010-11-15 01:33:22 -0800680 * If we get packets right away after recalibrating, it's likely
681 * that a finger was on the touchpad. If so, it's probably
682 * miscalibrated, so we optionally schedule another.
Andres Salomondf08ef22008-09-16 12:30:34 -0400683 */
Daniel Drake34caed22010-11-15 01:33:22 -0800684 if (recal_guard_time)
685 priv->recalib_window = jiffies +
686 msecs_to_jiffies(recal_guard_time);
Andres Salomondf08ef22008-09-16 12:30:34 -0400687
688 return 0;
689}
690
691/*
Paul Fox20a4c262010-11-15 01:33:24 -0800692 * This puts the touchpad in a power saving mode; according to ALPS, current
693 * consumption goes down to 50uA after running this. To turn power back on,
694 * we drive MS-DAT low. Measuring with a 1mA resolution ammeter says that
695 * the current on the SUS_3.3V rail drops from 3mA or 4mA to 0 when we do this.
696 *
697 * We have no formal spec that details this operation -- the low-power
698 * sequence came from a long-lost email trail.
Andres Salomondf08ef22008-09-16 12:30:34 -0400699 */
Paul Fox20a4c262010-11-15 01:33:24 -0800700static int hgpk_toggle_powersave(struct psmouse *psmouse, int enable)
Andres Salomondf08ef22008-09-16 12:30:34 -0400701{
702 struct ps2dev *ps2dev = &psmouse->ps2dev;
703 int timeo;
Daniel Drakeca94ec42010-11-11 22:19:57 -0800704 int err;
Andres Salomondf08ef22008-09-16 12:30:34 -0400705
706 /* Added on D-series touchpads */
707 if (psmouse->model < HGPK_MODEL_D)
708 return 0;
709
710 if (enable) {
711 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
712
713 /*
714 * Sending a byte will drive MS-DAT low; this will wake up
715 * the controller. Once we get an ACK back from it, it
716 * means we can continue with the touchpad re-init. ALPS
717 * tells us that 1s should be long enough, so set that as
Paul Fox20a4c262010-11-15 01:33:24 -0800718 * the upper bound. (in practice, it takes about 3 loops.)
Andres Salomondf08ef22008-09-16 12:30:34 -0400719 */
720 for (timeo = 20; timeo > 0; timeo--) {
721 if (!ps2_sendbyte(&psmouse->ps2dev,
722 PSMOUSE_CMD_DISABLE, 20))
723 break;
Paul Fox20a4c262010-11-15 01:33:24 -0800724 msleep(25);
Andres Salomondf08ef22008-09-16 12:30:34 -0400725 }
726
Daniel Drakeca94ec42010-11-11 22:19:57 -0800727 err = hgpk_reset_device(psmouse, false);
728 if (err) {
729 hgpk_err(psmouse, "Failed to reset device!\n");
730 return err;
731 }
Andres Salomondf08ef22008-09-16 12:30:34 -0400732
733 /* should be all set, enable the touchpad */
734 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE);
735 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
Paul Fox20a4c262010-11-15 01:33:24 -0800736 hgpk_dbg(psmouse, "Touchpad powered up.\n");
Andres Salomondf08ef22008-09-16 12:30:34 -0400737 } else {
738 hgpk_dbg(psmouse, "Powering off touchpad.\n");
Andres Salomondf08ef22008-09-16 12:30:34 -0400739
740 if (ps2_command(ps2dev, NULL, 0xec) ||
741 ps2_command(ps2dev, NULL, 0xec) ||
742 ps2_command(ps2dev, NULL, 0xea)) {
743 return -1;
744 }
745
Paul Fox20a4c262010-11-15 01:33:24 -0800746 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
747
Andres Salomondf08ef22008-09-16 12:30:34 -0400748 /* probably won't see an ACK, the touchpad will be off */
749 ps2_sendbyte(&psmouse->ps2dev, 0xec, 20);
750 }
751
752 return 0;
753}
754
755static int hgpk_poll(struct psmouse *psmouse)
756{
757 /* We can't poll, so always return failure. */
758 return -1;
759}
760
761static int hgpk_reconnect(struct psmouse *psmouse)
762{
Paul Fox20a4c262010-11-15 01:33:24 -0800763 struct hgpk_data *priv = psmouse->private;
764
Daniel Drakeca94ec42010-11-11 22:19:57 -0800765 /*
766 * During suspend/resume the ps2 rails remain powered. We don't want
Andres Salomondf08ef22008-09-16 12:30:34 -0400767 * to do a reset because it's flush data out of buffers; however,
Daniel Drakeca94ec42010-11-11 22:19:57 -0800768 * earlier prototypes (B1) had some brokenness that required a reset.
769 */
Andres Salomondf08ef22008-09-16 12:30:34 -0400770 if (olpc_board_at_least(olpc_board(0xb2)))
771 if (psmouse->ps2dev.serio->dev.power.power_state.event !=
772 PM_EVENT_ON)
773 return 0;
774
Paul Fox20a4c262010-11-15 01:33:24 -0800775 priv->powered = 1;
Daniel Drakeca94ec42010-11-11 22:19:57 -0800776 return hgpk_reset_device(psmouse, false);
Andres Salomondf08ef22008-09-16 12:30:34 -0400777}
778
779static ssize_t hgpk_show_powered(struct psmouse *psmouse, void *data, char *buf)
780{
781 struct hgpk_data *priv = psmouse->private;
782
783 return sprintf(buf, "%d\n", priv->powered);
784}
785
786static ssize_t hgpk_set_powered(struct psmouse *psmouse, void *data,
787 const char *buf, size_t count)
788{
789 struct hgpk_data *priv = psmouse->private;
790 unsigned long value;
791 int err;
792
793 err = strict_strtoul(buf, 10, &value);
794 if (err || value > 1)
795 return -EINVAL;
796
797 if (value != priv->powered) {
798 /*
799 * hgpk_toggle_power will deal w/ state so
800 * we're not racing w/ irq
801 */
Paul Fox20a4c262010-11-15 01:33:24 -0800802 err = hgpk_toggle_powersave(psmouse, value);
Andres Salomondf08ef22008-09-16 12:30:34 -0400803 if (!err)
804 priv->powered = value;
805 }
806
807 return err ? err : count;
808}
809
810__PSMOUSE_DEFINE_ATTR(powered, S_IWUSR | S_IRUGO, NULL,
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700811 hgpk_show_powered, hgpk_set_powered, false);
Andres Salomondf08ef22008-09-16 12:30:34 -0400812
Daniel Drakeca94ec42010-11-11 22:19:57 -0800813static ssize_t attr_show_mode(struct psmouse *psmouse, void *data, char *buf)
814{
815 struct hgpk_data *priv = psmouse->private;
816
817 return sprintf(buf, "%s\n", hgpk_mode_names[priv->mode]);
818}
819
820static ssize_t attr_set_mode(struct psmouse *psmouse, void *data,
821 const char *buf, size_t len)
822{
823 struct hgpk_data *priv = psmouse->private;
824 enum hgpk_mode old_mode = priv->mode;
825 enum hgpk_mode new_mode = hgpk_mode_from_name(buf, len);
826 struct input_dev *old_dev = psmouse->dev;
827 struct input_dev *new_dev;
828 int err;
829
830 if (new_mode == HGPK_MODE_INVALID)
831 return -EINVAL;
832
833 if (old_mode == new_mode)
834 return len;
835
836 new_dev = input_allocate_device();
837 if (!new_dev)
838 return -ENOMEM;
839
840 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
841
842 /* Switch device into the new mode */
843 priv->mode = new_mode;
844 err = hgpk_reset_device(psmouse, false);
845 if (err)
846 goto err_try_restore;
847
848 hgpk_setup_input_device(new_dev, old_dev, new_mode);
849
850 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
851
852 err = input_register_device(new_dev);
853 if (err)
854 goto err_try_restore;
855
856 psmouse->dev = new_dev;
857 input_unregister_device(old_dev);
858
859 return len;
860
861err_try_restore:
862 input_free_device(new_dev);
863 priv->mode = old_mode;
864 hgpk_reset_device(psmouse, false);
865
866 return err;
867}
868
869PSMOUSE_DEFINE_ATTR(hgpk_mode, S_IWUSR | S_IRUGO, NULL,
870 attr_show_mode, attr_set_mode);
871
Paul Foxc46dd1e2009-08-05 00:30:31 -0700872static ssize_t hgpk_trigger_recal_show(struct psmouse *psmouse,
873 void *data, char *buf)
874{
875 return -EINVAL;
876}
877
878static ssize_t hgpk_trigger_recal(struct psmouse *psmouse, void *data,
879 const char *buf, size_t count)
880{
881 struct hgpk_data *priv = psmouse->private;
882 unsigned long value;
883 int err;
884
885 err = strict_strtoul(buf, 10, &value);
886 if (err || value != 1)
887 return -EINVAL;
888
889 /*
890 * We queue work instead of doing recalibration right here
891 * to avoid adding locking to to hgpk_force_recalibrate()
892 * since workqueue provides serialization.
893 */
894 psmouse_queue_work(psmouse, &priv->recalib_wq, 0);
895 return count;
896}
897
898__PSMOUSE_DEFINE_ATTR(recalibrate, S_IWUSR | S_IRUGO, NULL,
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700899 hgpk_trigger_recal_show, hgpk_trigger_recal, false);
Paul Foxc46dd1e2009-08-05 00:30:31 -0700900
Andres Salomondf08ef22008-09-16 12:30:34 -0400901static void hgpk_disconnect(struct psmouse *psmouse)
902{
903 struct hgpk_data *priv = psmouse->private;
904
905 device_remove_file(&psmouse->ps2dev.serio->dev,
906 &psmouse_attr_powered.dattr);
Daniel Drakeca94ec42010-11-11 22:19:57 -0800907 device_remove_file(&psmouse->ps2dev.serio->dev,
908 &psmouse_attr_hgpk_mode.dattr);
Paul Foxc46dd1e2009-08-05 00:30:31 -0700909
910 if (psmouse->model >= HGPK_MODEL_C)
911 device_remove_file(&psmouse->ps2dev.serio->dev,
912 &psmouse_attr_recalibrate.dattr);
913
Andres Salomondf08ef22008-09-16 12:30:34 -0400914 psmouse_reset(psmouse);
915 kfree(priv);
916}
917
918static void hgpk_recalib_work(struct work_struct *work)
919{
Jean Delvarebf6aede2009-04-02 16:56:54 -0700920 struct delayed_work *w = to_delayed_work(work);
Andres Salomondf08ef22008-09-16 12:30:34 -0400921 struct hgpk_data *priv = container_of(w, struct hgpk_data, recalib_wq);
922 struct psmouse *psmouse = priv->psmouse;
923
Andres Salomondf08ef22008-09-16 12:30:34 -0400924 if (hgpk_force_recalibrate(psmouse))
925 hgpk_err(psmouse, "recalibration failed!\n");
926}
927
928static int hgpk_register(struct psmouse *psmouse)
929{
Daniel Drakeca94ec42010-11-11 22:19:57 -0800930 struct hgpk_data *priv = psmouse->private;
Andres Salomondf08ef22008-09-16 12:30:34 -0400931 int err;
932
Andres Salomondf08ef22008-09-16 12:30:34 -0400933 /* register handlers */
934 psmouse->protocol_handler = hgpk_process_byte;
935 psmouse->poll = hgpk_poll;
936 psmouse->disconnect = hgpk_disconnect;
937 psmouse->reconnect = hgpk_reconnect;
Andres Salomondf08ef22008-09-16 12:30:34 -0400938
939 /* Disable the idle resync. */
940 psmouse->resync_time = 0;
941 /* Reset after a lot of bad bytes. */
942 psmouse->resetafter = 1024;
943
Daniel Drakeca94ec42010-11-11 22:19:57 -0800944 hgpk_setup_input_device(psmouse->dev, NULL, priv->mode);
945
Andres Salomondf08ef22008-09-16 12:30:34 -0400946 err = device_create_file(&psmouse->ps2dev.serio->dev,
947 &psmouse_attr_powered.dattr);
Paul Foxc46dd1e2009-08-05 00:30:31 -0700948 if (err) {
949 hgpk_err(psmouse, "Failed creating 'powered' sysfs node\n");
950 return err;
951 }
Andres Salomondf08ef22008-09-16 12:30:34 -0400952
Daniel Drakeca94ec42010-11-11 22:19:57 -0800953 err = device_create_file(&psmouse->ps2dev.serio->dev,
954 &psmouse_attr_hgpk_mode.dattr);
955 if (err) {
956 hgpk_err(psmouse, "Failed creating 'hgpk_mode' sysfs node\n");
957 goto err_remove_powered;
958 }
959
Paul Foxc46dd1e2009-08-05 00:30:31 -0700960 /* C-series touchpads added the recalibrate command */
961 if (psmouse->model >= HGPK_MODEL_C) {
962 err = device_create_file(&psmouse->ps2dev.serio->dev,
963 &psmouse_attr_recalibrate.dattr);
964 if (err) {
965 hgpk_err(psmouse,
966 "Failed creating 'recalibrate' sysfs node\n");
Daniel Drakeca94ec42010-11-11 22:19:57 -0800967 goto err_remove_mode;
Paul Foxc46dd1e2009-08-05 00:30:31 -0700968 }
969 }
970
971 return 0;
Daniel Drakeca94ec42010-11-11 22:19:57 -0800972
973err_remove_mode:
974 device_remove_file(&psmouse->ps2dev.serio->dev,
975 &psmouse_attr_hgpk_mode.dattr);
976err_remove_powered:
977 device_remove_file(&psmouse->ps2dev.serio->dev,
978 &psmouse_attr_powered.dattr);
979 return err;
Andres Salomondf08ef22008-09-16 12:30:34 -0400980}
981
982int hgpk_init(struct psmouse *psmouse)
983{
984 struct hgpk_data *priv;
Daniel Drakeca94ec42010-11-11 22:19:57 -0800985 int err;
Andres Salomondf08ef22008-09-16 12:30:34 -0400986
987 priv = kzalloc(sizeof(struct hgpk_data), GFP_KERNEL);
Daniel Drakeca94ec42010-11-11 22:19:57 -0800988 if (!priv) {
989 err = -ENOMEM;
Andres Salomondf08ef22008-09-16 12:30:34 -0400990 goto alloc_fail;
Daniel Drakeca94ec42010-11-11 22:19:57 -0800991 }
Andres Salomondf08ef22008-09-16 12:30:34 -0400992
993 psmouse->private = priv;
Daniel Drakeca94ec42010-11-11 22:19:57 -0800994
Andres Salomondf08ef22008-09-16 12:30:34 -0400995 priv->psmouse = psmouse;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700996 priv->powered = true;
Daniel Drakeca94ec42010-11-11 22:19:57 -0800997 priv->mode = hgpk_default_mode;
Andres Salomondf08ef22008-09-16 12:30:34 -0400998 INIT_DELAYED_WORK(&priv->recalib_wq, hgpk_recalib_work);
999
Daniel Drakeca94ec42010-11-11 22:19:57 -08001000 err = hgpk_reset_device(psmouse, false);
Andres Salomondf08ef22008-09-16 12:30:34 -04001001 if (err)
1002 goto init_fail;
1003
1004 err = hgpk_register(psmouse);
1005 if (err)
1006 goto init_fail;
1007
1008 return 0;
1009
1010init_fail:
1011 kfree(priv);
1012alloc_fail:
1013 return err;
1014}
1015
1016static enum hgpk_model_t hgpk_get_model(struct psmouse *psmouse)
1017{
1018 struct ps2dev *ps2dev = &psmouse->ps2dev;
1019 unsigned char param[3];
1020
1021 /* E7, E7, E7, E9 gets us a 3 byte identifier */
1022 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1023 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1024 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1025 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) {
1026 return -EIO;
1027 }
1028
Andy Whitcroft0f495482009-02-28 14:55:46 -08001029 hgpk_dbg(psmouse, "ID: %02x %02x %02x\n", param[0], param[1], param[2]);
Andres Salomondf08ef22008-09-16 12:30:34 -04001030
1031 /* HGPK signature: 0x67, 0x00, 0x<model> */
1032 if (param[0] != 0x67 || param[1] != 0x00)
1033 return -ENODEV;
1034
1035 hgpk_info(psmouse, "OLPC touchpad revision 0x%x\n", param[2]);
1036
1037 return param[2];
1038}
1039
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001040int hgpk_detect(struct psmouse *psmouse, bool set_properties)
Andres Salomondf08ef22008-09-16 12:30:34 -04001041{
1042 int version;
1043
1044 version = hgpk_get_model(psmouse);
1045 if (version < 0)
1046 return version;
1047
1048 if (set_properties) {
1049 psmouse->vendor = "ALPS";
1050 psmouse->name = "HGPK";
1051 psmouse->model = version;
1052 }
1053
1054 return 0;
1055}
Daniel Drakeca94ec42010-11-11 22:19:57 -08001056
1057void hgpk_module_init(void)
1058{
1059 hgpk_default_mode = hgpk_mode_from_name(hgpk_mode_name,
1060 strlen(hgpk_mode_name));
1061 if (hgpk_default_mode == HGPK_MODE_INVALID) {
1062 hgpk_default_mode = HGPK_MODE_MOUSE;
1063 strlcpy(hgpk_mode_name, hgpk_mode_names[HGPK_MODE_MOUSE],
1064 sizeof(hgpk_mode_name));
1065 }
1066}