blob: 7f6cb32d177a10e74b987ecb36ff040109939f15 [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
65static int recal_guard_time = 2000;
66module_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
70static int post_interrupt_delay = 1000;
71module_param(post_interrupt_delay, int, 0644);
72MODULE_PARM_DESC(post_interrupt_delay,
73 "delay (ms) before recal after recal interrupt detected");
74
Daniel Drakeca94ec42010-11-11 22:19:57 -080075static char hgpk_mode_name[16];
76module_param_string(hgpk_mode, hgpk_mode_name, sizeof(hgpk_mode_name), 0644);
77MODULE_PARM_DESC(hgpk_mode,
78 "default hgpk mode: mouse, glidesensor or pentablet");
79
80static int hgpk_default_mode = HGPK_MODE_MOUSE;
81
82static const char * const hgpk_mode_names[] = {
83 [HGPK_MODE_MOUSE] = "Mouse",
84 [HGPK_MODE_GLIDESENSOR] = "GlideSensor",
85 [HGPK_MODE_PENTABLET] = "PenTablet",
86};
87
88static int hgpk_mode_from_name(const char *buf, int len)
89{
90 int i;
91
92 for (i = 0; i < ARRAY_SIZE(hgpk_mode_names); i++) {
93 const char *name = hgpk_mode_names[i];
94 if (strlen(name) == len && !strncasecmp(name, buf, len))
95 return i;
96 }
97
98 return HGPK_MODE_INVALID;
99}
100
Andres Salomondf08ef22008-09-16 12:30:34 -0400101/*
Daniel Drakea309cdc2010-11-11 22:20:03 -0800102 * see if new value is within 20% of half of old value
Andres Salomondf08ef22008-09-16 12:30:34 -0400103 */
Daniel Drakea309cdc2010-11-11 22:20:03 -0800104static int approx_half(int curr, int prev)
105{
106 int belowhalf, abovehalf;
107
108 if (curr < 5 || prev < 5)
109 return 0;
110
111 belowhalf = (prev * 8) / 20;
112 abovehalf = (prev * 12) / 20;
113
114 return belowhalf < curr && curr <= abovehalf;
115}
116
117/*
118 * Throw out oddly large delta packets, and any that immediately follow whose
119 * values are each approximately half of the previous. It seems that the ALPS
120 * firmware emits errant packets, and they get averaged out slowly.
121 */
122static int hgpk_discard_decay_hack(struct psmouse *psmouse, int x, int y)
Andres Salomondf08ef22008-09-16 12:30:34 -0400123{
124 struct hgpk_data *priv = psmouse->private;
Daniel Drakea309cdc2010-11-11 22:20:03 -0800125 int avx, avy;
126 bool do_recal = false;
Andres Salomondf08ef22008-09-16 12:30:34 -0400127
Daniel Drakea309cdc2010-11-11 22:20:03 -0800128 avx = abs(x);
129 avy = abs(y);
130
131 /* discard if too big, or half that but > 4 times the prev delta */
132 if (avx > recalib_delta ||
133 (avx > recalib_delta / 2 && ((avx / 4) > priv->xlast))) {
134 hgpk_err(psmouse, "detected %dpx jump in x\n", x);
135 priv->xbigj = avx;
136 } else if (approx_half(avx, priv->xbigj)) {
137 hgpk_err(psmouse, "detected secondary %dpx jump in x\n", x);
138 priv->xbigj = avx;
139 priv->xsaw_secondary++;
140 } else {
141 if (priv->xbigj && priv->xsaw_secondary > 1)
142 do_recal = true;
143 priv->xbigj = 0;
144 priv->xsaw_secondary = 0;
145 }
146
147 if (avy > recalib_delta ||
148 (avy > recalib_delta / 2 && ((avy / 4) > priv->ylast))) {
149 hgpk_err(psmouse, "detected %dpx jump in y\n", y);
150 priv->ybigj = avy;
151 } else if (approx_half(avy, priv->ybigj)) {
152 hgpk_err(psmouse, "detected secondary %dpx jump in y\n", y);
153 priv->ybigj = avy;
154 priv->ysaw_secondary++;
155 } else {
156 if (priv->ybigj && priv->ysaw_secondary > 1)
157 do_recal = true;
158 priv->ybigj = 0;
159 priv->ysaw_secondary = 0;
160 }
161
162 priv->xlast = avx;
163 priv->ylast = avy;
164
165 if (do_recal && jumpy_delay) {
166 hgpk_err(psmouse, "scheduling recalibration\n");
Andres Salomondf08ef22008-09-16 12:30:34 -0400167 psmouse_queue_work(psmouse, &priv->recalib_wq,
Paul Fox8bbf2702008-12-20 03:58:11 -0500168 msecs_to_jiffies(jumpy_delay));
Andres Salomondf08ef22008-09-16 12:30:34 -0400169 }
Daniel Drakea309cdc2010-11-11 22:20:03 -0800170
171 return priv->xbigj || priv->ybigj;
Andres Salomondf08ef22008-09-16 12:30:34 -0400172}
173
Daniel Drakec0dc8342010-11-11 22:20:02 -0800174static void hgpk_reset_spew_detection(struct hgpk_data *priv)
175{
176 priv->spew_count = 0;
177 priv->dupe_count = 0;
178 priv->x_tally = 0;
179 priv->y_tally = 0;
180 priv->spew_flag = NO_SPEW;
181}
182
183static void hgpk_reset_hack_state(struct psmouse *psmouse)
184{
185 struct hgpk_data *priv = psmouse->private;
186
187 priv->abs_x = priv->abs_y = -1;
Daniel Drakea309cdc2010-11-11 22:20:03 -0800188 priv->xlast = priv->ylast = ILLEGAL_XY;
189 priv->xbigj = priv->ybigj = 0;
190 priv->xsaw_secondary = priv->ysaw_secondary = 0;
Daniel Drakec0dc8342010-11-11 22:20:02 -0800191 hgpk_reset_spew_detection(priv);
192}
193
Andres Salomondf08ef22008-09-16 12:30:34 -0400194/*
195 * We have no idea why this particular hardware bug occurs. The touchpad
196 * will randomly start spewing packets without anything touching the
197 * pad. This wouldn't necessarily be bad, but it's indicative of a
198 * severely miscalibrated pad; attempting to use the touchpad while it's
199 * spewing means the cursor will jump all over the place, and act "drunk".
200 *
201 * The packets that are spewed tend to all have deltas between -2 and 2, and
202 * the cursor will move around without really going very far. It will
203 * tend to end up in the same location; if we tally up the changes over
204 * 100 packets, we end up w/ a final delta of close to 0. This happens
205 * pretty regularly when the touchpad is spewing, and is pretty hard to
206 * manually trigger (at least for *my* fingers). So, it makes a perfect
207 * scheme for detecting spews.
208 */
209static void hgpk_spewing_hack(struct psmouse *psmouse,
210 int l, int r, int x, int y)
211{
212 struct hgpk_data *priv = psmouse->private;
213
214 /* ignore button press packets; many in a row could trigger
215 * a false-positive! */
216 if (l || r)
217 return;
218
Daniel Drakec0dc8342010-11-11 22:20:02 -0800219 /* don't track spew if the workaround feature has been turned off */
220 if (!spew_delay)
221 return;
222
223 if (abs(x) > 3 || abs(y) > 3) {
224 /* no spew, or spew ended */
225 hgpk_reset_spew_detection(priv);
226 return;
227 }
228
229 /* Keep a tally of the overall delta to the cursor position caused by
230 * the spew */
Andres Salomondf08ef22008-09-16 12:30:34 -0400231 priv->x_tally += x;
232 priv->y_tally += y;
233
Daniel Drakec0dc8342010-11-11 22:20:02 -0800234 switch (priv->spew_flag) {
235 case NO_SPEW:
236 /* we're not spewing, but this packet might be the start */
237 priv->spew_flag = MAYBE_SPEWING;
238
239 /* fall-through */
240
241 case MAYBE_SPEWING:
242 priv->spew_count++;
243
244 if (priv->spew_count < SPEW_WATCH_COUNT)
245 break;
246
247 /* excessive spew detected, request recalibration */
248 priv->spew_flag = SPEW_DETECTED;
249
250 /* fall-through */
251
252 case SPEW_DETECTED:
253 /* only recalibrate when the overall delta to the cursor
254 * is really small. if the spew is causing significant cursor
255 * movement, it is probably a case of the user moving the
256 * cursor very slowly across the screen. */
Andres Salomondf08ef22008-09-16 12:30:34 -0400257 if (abs(priv->x_tally) < 3 && abs(priv->y_tally) < 3) {
Daniel Drakec0dc8342010-11-11 22:20:02 -0800258 hgpk_err(psmouse, "packet spew detected (%d,%d)\n",
Andres Salomondf08ef22008-09-16 12:30:34 -0400259 priv->x_tally, priv->y_tally);
Daniel Drakec0dc8342010-11-11 22:20:02 -0800260 priv->spew_flag = RECALIBRATING;
Andres Salomondf08ef22008-09-16 12:30:34 -0400261 psmouse_queue_work(psmouse, &priv->recalib_wq,
Paul Fox8bbf2702008-12-20 03:58:11 -0500262 msecs_to_jiffies(spew_delay));
Andres Salomondf08ef22008-09-16 12:30:34 -0400263 }
Daniel Drakec0dc8342010-11-11 22:20:02 -0800264
265 break;
266 case RECALIBRATING:
267 /* we already detected a spew and requested a recalibration,
268 * just wait for the queue to kick into action. */
269 break;
Andres Salomondf08ef22008-09-16 12:30:34 -0400270 }
271}
272
273/*
274 * HGPK Mouse Mode format (standard mouse format, sans middle button)
275 *
276 * byte 0: y-over x-over y-neg x-neg 1 0 swr swl
277 * byte 1: x7 x6 x5 x4 x3 x2 x1 x0
278 * byte 2: y7 y6 y5 y4 y3 y2 y1 y0
279 *
280 * swr/swl are the left/right buttons.
281 * x-neg/y-neg are the x and y delta negative bits
282 * x-over/y-over are the x and y overflow bits
Daniel Drakeca94ec42010-11-11 22:19:57 -0800283 *
284 * ---
285 *
286 * HGPK Advanced Mode - single-mode format
287 *
288 * byte 0(PT): 1 1 0 0 1 1 1 1
289 * byte 0(GS): 1 1 1 1 1 1 1 1
290 * byte 1: 0 x6 x5 x4 x3 x2 x1 x0
291 * byte 2(PT): 0 0 x9 x8 x7 ? pt-dsw 0
292 * byte 2(GS): 0 x10 x9 x8 x7 ? gs-dsw pt-dsw
293 * byte 3: 0 y9 y8 y7 1 0 swr swl
294 * byte 4: 0 y6 y5 y4 y3 y2 y1 y0
295 * byte 5: 0 z6 z5 z4 z3 z2 z1 z0
296 *
297 * ?'s are not defined in the protocol spec, may vary between models.
298 *
299 * swr/swl are the left/right buttons.
300 *
301 * pt-dsw/gs-dsw indicate that the pt/gs sensor is detecting a
302 * pen/finger
Andres Salomondf08ef22008-09-16 12:30:34 -0400303 */
Daniel Drakeca94ec42010-11-11 22:19:57 -0800304static bool hgpk_is_byte_valid(struct psmouse *psmouse, unsigned char *packet)
Andres Salomondf08ef22008-09-16 12:30:34 -0400305{
Daniel Drakeca94ec42010-11-11 22:19:57 -0800306 struct hgpk_data *priv = psmouse->private;
307 int pktcnt = psmouse->pktcnt;
308 bool valid;
309
310 switch (priv->mode) {
311 case HGPK_MODE_MOUSE:
312 valid = (packet[0] & 0x0C) == 0x08;
313 break;
314
315 case HGPK_MODE_GLIDESENSOR:
316 valid = pktcnt == 1 ?
317 packet[0] == HGPK_GS : !(packet[pktcnt - 1] & 0x80);
318 break;
319
320 case HGPK_MODE_PENTABLET:
321 valid = pktcnt == 1 ?
322 packet[0] == HGPK_PT : !(packet[pktcnt - 1] & 0x80);
323 break;
324
325 default:
326 valid = false;
327 break;
328 }
329
330 if (!valid)
331 hgpk_dbg(psmouse,
332 "bad data, mode %d (%d) %02x %02x %02x %02x %02x %02x\n",
333 priv->mode, pktcnt,
334 psmouse->packet[0], psmouse->packet[1],
335 psmouse->packet[2], psmouse->packet[3],
336 psmouse->packet[4], psmouse->packet[5]);
337
338 return valid;
Andres Salomondf08ef22008-09-16 12:30:34 -0400339}
340
Daniel Drakeca94ec42010-11-11 22:19:57 -0800341static void hgpk_process_advanced_packet(struct psmouse *psmouse)
342{
343 struct hgpk_data *priv = psmouse->private;
344 struct input_dev *idev = psmouse->dev;
345 unsigned char *packet = psmouse->packet;
346 int down = !!(packet[2] & 2);
347 int left = !!(packet[3] & 1);
348 int right = !!(packet[3] & 2);
349 int x = packet[1] | ((packet[2] & 0x78) << 4);
350 int y = packet[4] | ((packet[3] & 0x70) << 3);
351
352 if (priv->mode == HGPK_MODE_GLIDESENSOR) {
353 int pt_down = !!(packet[2] & 1);
354 int finger_down = !!(packet[2] & 2);
355 int z = packet[5];
356
357 input_report_abs(idev, ABS_PRESSURE, z);
358 if (tpdebug)
359 hgpk_dbg(psmouse, "pd=%d fd=%d z=%d",
360 pt_down, finger_down, z);
361 } else {
362 /*
363 * PenTablet mode does not report pressure, so we don't
364 * report it here
365 */
366 if (tpdebug)
367 hgpk_dbg(psmouse, "pd=%d ", down);
368 }
369
370 if (tpdebug)
371 hgpk_dbg(psmouse, "l=%d r=%d x=%d y=%d\n", left, right, x, y);
372
373 input_report_key(idev, BTN_TOUCH, down);
374 input_report_key(idev, BTN_LEFT, left);
375 input_report_key(idev, BTN_RIGHT, right);
376
377 /*
378 * If this packet says that the finger was removed, reset our position
379 * tracking so that we don't erroneously detect a jump on next press.
380 */
Daniel Drakec0dc8342010-11-11 22:20:02 -0800381 if (!down) {
Daniel Drakea309cdc2010-11-11 22:20:03 -0800382 hgpk_reset_hack_state(psmouse);
Daniel Drakec0dc8342010-11-11 22:20:02 -0800383 goto done;
Daniel Drakeca94ec42010-11-11 22:19:57 -0800384 }
385
Daniel Drakec0dc8342010-11-11 22:20:02 -0800386 /*
387 * Weed out duplicate packets (we get quite a few, and they mess up
388 * our jump detection)
389 */
390 if (x == priv->abs_x && y == priv->abs_y) {
391 if (++priv->dupe_count > SPEW_WATCH_COUNT) {
392 if (tpdebug)
393 hgpk_dbg(psmouse, "hard spew detected\n");
394 priv->spew_flag = RECALIBRATING;
395 psmouse_queue_work(psmouse, &priv->recalib_wq,
396 msecs_to_jiffies(spew_delay));
397 }
398 goto done;
399 }
400
401 /* not a duplicate, continue with position reporting */
402 priv->dupe_count = 0;
403
404 /* Don't apply hacks in PT mode, it seems reliable */
405 if (priv->mode != HGPK_MODE_PENTABLET && priv->abs_x != -1) {
Daniel Drakea309cdc2010-11-11 22:20:03 -0800406 int x_diff = priv->abs_x - x;
407 int y_diff = priv->abs_y - y;
408 if (hgpk_discard_decay_hack(psmouse, x_diff, y_diff)) {
409 if (tpdebug)
410 hgpk_dbg(psmouse, "discarding\n");
411 goto done;
412 }
413 hgpk_spewing_hack(psmouse, left, right, x_diff, y_diff);
Daniel Drakec0dc8342010-11-11 22:20:02 -0800414 }
415
416 input_report_abs(idev, ABS_X, x);
417 input_report_abs(idev, ABS_Y, y);
418 priv->abs_x = x;
419 priv->abs_y = y;
420
421done:
Daniel Drakeca94ec42010-11-11 22:19:57 -0800422 input_sync(idev);
423}
424
425static void hgpk_process_simple_packet(struct psmouse *psmouse)
Andres Salomondf08ef22008-09-16 12:30:34 -0400426{
427 struct input_dev *dev = psmouse->dev;
428 unsigned char *packet = psmouse->packet;
Daniel Drakeca94ec42010-11-11 22:19:57 -0800429 int left = packet[0] & 1;
430 int right = (packet[0] >> 1) & 1;
431 int x = packet[1] - ((packet[0] << 4) & 0x100);
432 int y = ((packet[0] << 3) & 0x100) - packet[2];
Andres Salomondf08ef22008-09-16 12:30:34 -0400433
Daniel Drake67f56bb2010-11-15 01:28:54 -0800434 if (packet[0] & 0xc0)
435 hgpk_dbg(psmouse,
436 "overflow -- 0x%02x 0x%02x 0x%02x\n",
437 packet[0], packet[1], packet[2]);
438
Daniel Drakea309cdc2010-11-11 22:20:03 -0800439 if (hgpk_discard_decay_hack(psmouse, x, y)) {
440 if (tpdebug)
441 hgpk_dbg(psmouse, "discarding\n");
442 return;
443 }
444
Andres Salomondf08ef22008-09-16 12:30:34 -0400445 hgpk_spewing_hack(psmouse, left, right, x, y);
446
447 if (tpdebug)
448 hgpk_dbg(psmouse, "l=%d r=%d x=%d y=%d\n", left, right, x, y);
449
450 input_report_key(dev, BTN_LEFT, left);
451 input_report_key(dev, BTN_RIGHT, right);
452
453 input_report_rel(dev, REL_X, x);
454 input_report_rel(dev, REL_Y, y);
455
456 input_sync(dev);
457}
458
459static psmouse_ret_t hgpk_process_byte(struct psmouse *psmouse)
460{
461 struct hgpk_data *priv = psmouse->private;
462
Daniel Drakeca94ec42010-11-11 22:19:57 -0800463 if (!hgpk_is_byte_valid(psmouse, psmouse->packet))
Andres Salomondf08ef22008-09-16 12:30:34 -0400464 return PSMOUSE_BAD_DATA;
Andres Salomondf08ef22008-09-16 12:30:34 -0400465
466 if (psmouse->pktcnt >= psmouse->pktsize) {
Daniel Drakeca94ec42010-11-11 22:19:57 -0800467 if (priv->mode == HGPK_MODE_MOUSE)
468 hgpk_process_simple_packet(psmouse);
469 else
470 hgpk_process_advanced_packet(psmouse);
Andres Salomondf08ef22008-09-16 12:30:34 -0400471 return PSMOUSE_FULL_PACKET;
472 }
473
474 if (priv->recalib_window) {
475 if (time_before(jiffies, priv->recalib_window)) {
476 /*
477 * ugh, got a packet inside our recalibration
478 * window, schedule another recalibration.
479 */
480 hgpk_dbg(psmouse,
481 "packet inside calibration window, "
482 "queueing another recalibration\n");
483 psmouse_queue_work(psmouse, &priv->recalib_wq,
Paul Fox8bbf2702008-12-20 03:58:11 -0500484 msecs_to_jiffies(post_interrupt_delay));
Andres Salomondf08ef22008-09-16 12:30:34 -0400485 }
486 priv->recalib_window = 0;
487 }
488
489 return PSMOUSE_GOOD_DATA;
490}
491
Daniel Drakeca94ec42010-11-11 22:19:57 -0800492static int hgpk_select_mode(struct psmouse *psmouse)
493{
494 struct ps2dev *ps2dev = &psmouse->ps2dev;
495 struct hgpk_data *priv = psmouse->private;
496 int i;
497 int cmd;
498
499 /*
500 * 4 disables to enable advanced mode
501 * then 3 0xf2 bytes as the preamble for GS/PT selection
502 */
503 const int advanced_init[] = {
504 PSMOUSE_CMD_DISABLE, PSMOUSE_CMD_DISABLE,
505 PSMOUSE_CMD_DISABLE, PSMOUSE_CMD_DISABLE,
506 0xf2, 0xf2, 0xf2,
507 };
508
509 switch (priv->mode) {
510 case HGPK_MODE_MOUSE:
511 psmouse->pktsize = 3;
512 break;
513
514 case HGPK_MODE_GLIDESENSOR:
515 case HGPK_MODE_PENTABLET:
516 psmouse->pktsize = 6;
517
518 /* Switch to 'Advanced mode.', four disables in a row. */
519 for (i = 0; i < ARRAY_SIZE(advanced_init); i++)
520 if (ps2_command(ps2dev, NULL, advanced_init[i]))
521 return -EIO;
522
523 /* select between GlideSensor (mouse) or PenTablet */
524 cmd = priv->mode == HGPK_MODE_GLIDESENSOR ?
525 PSMOUSE_CMD_SETSCALE11 : PSMOUSE_CMD_SETSCALE21;
526
527 if (ps2_command(ps2dev, NULL, cmd))
528 return -EIO;
529 break;
530
531 default:
532 return -EINVAL;
533 }
534
535 return 0;
536}
537
538static void hgpk_setup_input_device(struct input_dev *input,
539 struct input_dev *old_input,
540 enum hgpk_mode mode)
541{
542 if (old_input) {
543 input->name = old_input->name;
544 input->phys = old_input->phys;
545 input->id = old_input->id;
546 input->dev.parent = old_input->dev.parent;
547 }
548
549 memset(input->evbit, 0, sizeof(input->evbit));
550 memset(input->relbit, 0, sizeof(input->relbit));
551 memset(input->keybit, 0, sizeof(input->keybit));
552
553 /* All modes report left and right buttons */
554 __set_bit(EV_KEY, input->evbit);
555 __set_bit(BTN_LEFT, input->keybit);
556 __set_bit(BTN_RIGHT, input->keybit);
557
558 switch (mode) {
559 case HGPK_MODE_MOUSE:
560 __set_bit(EV_REL, input->evbit);
561 __set_bit(REL_X, input->relbit);
562 __set_bit(REL_Y, input->relbit);
563 break;
564
565 case HGPK_MODE_GLIDESENSOR:
566 __set_bit(BTN_TOUCH, input->keybit);
567 __set_bit(BTN_TOOL_FINGER, input->keybit);
568
569 __set_bit(EV_ABS, input->evbit);
570
571 /* GlideSensor has pressure sensor, PenTablet does not */
572 input_set_abs_params(input, ABS_PRESSURE, 0, 15, 0, 0);
573
574 /* From device specs */
575 input_set_abs_params(input, ABS_X, 0, 399, 0, 0);
576 input_set_abs_params(input, ABS_Y, 0, 290, 0, 0);
577
578 /* Calculated by hand based on usable size (52mm x 38mm) */
579 input_abs_set_res(input, ABS_X, 8);
580 input_abs_set_res(input, ABS_Y, 8);
581 break;
582
583 case HGPK_MODE_PENTABLET:
584 __set_bit(BTN_TOUCH, input->keybit);
585 __set_bit(BTN_TOOL_FINGER, input->keybit);
586
587 __set_bit(EV_ABS, input->evbit);
588
589 /* From device specs */
590 input_set_abs_params(input, ABS_X, 0, 999, 0, 0);
591 input_set_abs_params(input, ABS_Y, 5, 239, 0, 0);
592
593 /* Calculated by hand based on usable size (156mm x 38mm) */
594 input_abs_set_res(input, ABS_X, 6);
595 input_abs_set_res(input, ABS_Y, 8);
596 break;
597
598 default:
599 BUG();
600 }
601}
602
Daniel Drakeca94ec42010-11-11 22:19:57 -0800603static int hgpk_reset_device(struct psmouse *psmouse, bool recalibrate)
604{
605 int err;
606
607 psmouse_reset(psmouse);
608
609 if (recalibrate) {
610 struct ps2dev *ps2dev = &psmouse->ps2dev;
611
612 /* send the recalibrate request */
613 if (ps2_command(ps2dev, NULL, 0xf5) ||
614 ps2_command(ps2dev, NULL, 0xf5) ||
615 ps2_command(ps2dev, NULL, 0xe6) ||
616 ps2_command(ps2dev, NULL, 0xf5)) {
617 return -1;
618 }
619
620 /* according to ALPS, 150mS is required for recalibration */
621 msleep(150);
622 }
623
624 err = hgpk_select_mode(psmouse);
625 if (err) {
626 hgpk_err(psmouse, "failed to select mode\n");
627 return err;
628 }
629
630 hgpk_reset_hack_state(psmouse);
631
632 return 0;
633}
634
Andres Salomondf08ef22008-09-16 12:30:34 -0400635static int hgpk_force_recalibrate(struct psmouse *psmouse)
636{
637 struct ps2dev *ps2dev = &psmouse->ps2dev;
638 struct hgpk_data *priv = psmouse->private;
Daniel Drakeca94ec42010-11-11 22:19:57 -0800639 int err;
Andres Salomondf08ef22008-09-16 12:30:34 -0400640
641 /* C-series touchpads added the recalibrate command */
642 if (psmouse->model < HGPK_MODEL_C)
643 return 0;
644
645 /* we don't want to race with the irq handler, nor with resyncs */
646 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
647
648 /* start by resetting the device */
Daniel Drakeca94ec42010-11-11 22:19:57 -0800649 err = hgpk_reset_device(psmouse, true);
650 if (err)
651 return err;
Andres Salomondf08ef22008-09-16 12:30:34 -0400652
Daniel Drakeca94ec42010-11-11 22:19:57 -0800653 /*
654 * XXX: If a finger is down during this delay, recalibration will
Andres Salomondf08ef22008-09-16 12:30:34 -0400655 * detect capacitance incorrectly. This is a hardware bug, and
656 * we don't have a good way to deal with it. The 2s window stuff
657 * (below) is our best option for now.
658 */
659
660 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
661 return -1;
662
663 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
664
Daniel Drakeca94ec42010-11-11 22:19:57 -0800665 /*
666 * After we recalibrate, we shouldn't get any packets for 2s. If
Andres Salomondf08ef22008-09-16 12:30:34 -0400667 * we do, it's likely that someone's finger was on the touchpad.
668 * If someone's finger *was* on the touchpad, it's probably
669 * miscalibrated. So, we should schedule another recalibration
670 */
Daniel Drakeca94ec42010-11-11 22:19:57 -0800671 priv->recalib_window = jiffies + msecs_to_jiffies(recal_guard_time);
Andres Salomondf08ef22008-09-16 12:30:34 -0400672
673 return 0;
674}
675
676/*
677 * This kills power to the touchpad; according to ALPS, current consumption
678 * goes down to 50uA after running this. To turn power back on, we drive
679 * MS-DAT low.
680 */
681static int hgpk_toggle_power(struct psmouse *psmouse, int enable)
682{
683 struct ps2dev *ps2dev = &psmouse->ps2dev;
684 int timeo;
Daniel Drakeca94ec42010-11-11 22:19:57 -0800685 int err;
Andres Salomondf08ef22008-09-16 12:30:34 -0400686
687 /* Added on D-series touchpads */
688 if (psmouse->model < HGPK_MODEL_D)
689 return 0;
690
691 if (enable) {
692 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
693
694 /*
695 * Sending a byte will drive MS-DAT low; this will wake up
696 * the controller. Once we get an ACK back from it, it
697 * means we can continue with the touchpad re-init. ALPS
698 * tells us that 1s should be long enough, so set that as
699 * the upper bound.
700 */
701 for (timeo = 20; timeo > 0; timeo--) {
702 if (!ps2_sendbyte(&psmouse->ps2dev,
703 PSMOUSE_CMD_DISABLE, 20))
704 break;
705 msleep(50);
706 }
707
Daniel Drakeca94ec42010-11-11 22:19:57 -0800708 err = hgpk_reset_device(psmouse, false);
709 if (err) {
710 hgpk_err(psmouse, "Failed to reset device!\n");
711 return err;
712 }
Andres Salomondf08ef22008-09-16 12:30:34 -0400713
714 /* should be all set, enable the touchpad */
715 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE);
716 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
717
718 } else {
719 hgpk_dbg(psmouse, "Powering off touchpad.\n");
720 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
721
722 if (ps2_command(ps2dev, NULL, 0xec) ||
723 ps2_command(ps2dev, NULL, 0xec) ||
724 ps2_command(ps2dev, NULL, 0xea)) {
725 return -1;
726 }
727
728 /* probably won't see an ACK, the touchpad will be off */
729 ps2_sendbyte(&psmouse->ps2dev, 0xec, 20);
730 }
731
732 return 0;
733}
734
735static int hgpk_poll(struct psmouse *psmouse)
736{
737 /* We can't poll, so always return failure. */
738 return -1;
739}
740
741static int hgpk_reconnect(struct psmouse *psmouse)
742{
Daniel Drakeca94ec42010-11-11 22:19:57 -0800743 /*
744 * During suspend/resume the ps2 rails remain powered. We don't want
Andres Salomondf08ef22008-09-16 12:30:34 -0400745 * to do a reset because it's flush data out of buffers; however,
Daniel Drakeca94ec42010-11-11 22:19:57 -0800746 * earlier prototypes (B1) had some brokenness that required a reset.
747 */
Andres Salomondf08ef22008-09-16 12:30:34 -0400748 if (olpc_board_at_least(olpc_board(0xb2)))
749 if (psmouse->ps2dev.serio->dev.power.power_state.event !=
750 PM_EVENT_ON)
751 return 0;
752
Daniel Drakeca94ec42010-11-11 22:19:57 -0800753 return hgpk_reset_device(psmouse, false);
Andres Salomondf08ef22008-09-16 12:30:34 -0400754}
755
756static ssize_t hgpk_show_powered(struct psmouse *psmouse, void *data, char *buf)
757{
758 struct hgpk_data *priv = psmouse->private;
759
760 return sprintf(buf, "%d\n", priv->powered);
761}
762
763static ssize_t hgpk_set_powered(struct psmouse *psmouse, void *data,
764 const char *buf, size_t count)
765{
766 struct hgpk_data *priv = psmouse->private;
767 unsigned long value;
768 int err;
769
770 err = strict_strtoul(buf, 10, &value);
771 if (err || value > 1)
772 return -EINVAL;
773
774 if (value != priv->powered) {
775 /*
776 * hgpk_toggle_power will deal w/ state so
777 * we're not racing w/ irq
778 */
779 err = hgpk_toggle_power(psmouse, value);
780 if (!err)
781 priv->powered = value;
782 }
783
784 return err ? err : count;
785}
786
787__PSMOUSE_DEFINE_ATTR(powered, S_IWUSR | S_IRUGO, NULL,
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700788 hgpk_show_powered, hgpk_set_powered, false);
Andres Salomondf08ef22008-09-16 12:30:34 -0400789
Daniel Drakeca94ec42010-11-11 22:19:57 -0800790static ssize_t attr_show_mode(struct psmouse *psmouse, void *data, char *buf)
791{
792 struct hgpk_data *priv = psmouse->private;
793
794 return sprintf(buf, "%s\n", hgpk_mode_names[priv->mode]);
795}
796
797static ssize_t attr_set_mode(struct psmouse *psmouse, void *data,
798 const char *buf, size_t len)
799{
800 struct hgpk_data *priv = psmouse->private;
801 enum hgpk_mode old_mode = priv->mode;
802 enum hgpk_mode new_mode = hgpk_mode_from_name(buf, len);
803 struct input_dev *old_dev = psmouse->dev;
804 struct input_dev *new_dev;
805 int err;
806
807 if (new_mode == HGPK_MODE_INVALID)
808 return -EINVAL;
809
810 if (old_mode == new_mode)
811 return len;
812
813 new_dev = input_allocate_device();
814 if (!new_dev)
815 return -ENOMEM;
816
817 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
818
819 /* Switch device into the new mode */
820 priv->mode = new_mode;
821 err = hgpk_reset_device(psmouse, false);
822 if (err)
823 goto err_try_restore;
824
825 hgpk_setup_input_device(new_dev, old_dev, new_mode);
826
827 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
828
829 err = input_register_device(new_dev);
830 if (err)
831 goto err_try_restore;
832
833 psmouse->dev = new_dev;
834 input_unregister_device(old_dev);
835
836 return len;
837
838err_try_restore:
839 input_free_device(new_dev);
840 priv->mode = old_mode;
841 hgpk_reset_device(psmouse, false);
842
843 return err;
844}
845
846PSMOUSE_DEFINE_ATTR(hgpk_mode, S_IWUSR | S_IRUGO, NULL,
847 attr_show_mode, attr_set_mode);
848
Paul Foxc46dd1e2009-08-05 00:30:31 -0700849static ssize_t hgpk_trigger_recal_show(struct psmouse *psmouse,
850 void *data, char *buf)
851{
852 return -EINVAL;
853}
854
855static ssize_t hgpk_trigger_recal(struct psmouse *psmouse, void *data,
856 const char *buf, size_t count)
857{
858 struct hgpk_data *priv = psmouse->private;
859 unsigned long value;
860 int err;
861
862 err = strict_strtoul(buf, 10, &value);
863 if (err || value != 1)
864 return -EINVAL;
865
866 /*
867 * We queue work instead of doing recalibration right here
868 * to avoid adding locking to to hgpk_force_recalibrate()
869 * since workqueue provides serialization.
870 */
871 psmouse_queue_work(psmouse, &priv->recalib_wq, 0);
872 return count;
873}
874
875__PSMOUSE_DEFINE_ATTR(recalibrate, S_IWUSR | S_IRUGO, NULL,
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700876 hgpk_trigger_recal_show, hgpk_trigger_recal, false);
Paul Foxc46dd1e2009-08-05 00:30:31 -0700877
Andres Salomondf08ef22008-09-16 12:30:34 -0400878static void hgpk_disconnect(struct psmouse *psmouse)
879{
880 struct hgpk_data *priv = psmouse->private;
881
882 device_remove_file(&psmouse->ps2dev.serio->dev,
883 &psmouse_attr_powered.dattr);
Daniel Drakeca94ec42010-11-11 22:19:57 -0800884 device_remove_file(&psmouse->ps2dev.serio->dev,
885 &psmouse_attr_hgpk_mode.dattr);
Paul Foxc46dd1e2009-08-05 00:30:31 -0700886
887 if (psmouse->model >= HGPK_MODEL_C)
888 device_remove_file(&psmouse->ps2dev.serio->dev,
889 &psmouse_attr_recalibrate.dattr);
890
Andres Salomondf08ef22008-09-16 12:30:34 -0400891 psmouse_reset(psmouse);
892 kfree(priv);
893}
894
895static void hgpk_recalib_work(struct work_struct *work)
896{
Jean Delvarebf6aede2009-04-02 16:56:54 -0700897 struct delayed_work *w = to_delayed_work(work);
Andres Salomondf08ef22008-09-16 12:30:34 -0400898 struct hgpk_data *priv = container_of(w, struct hgpk_data, recalib_wq);
899 struct psmouse *psmouse = priv->psmouse;
900
901 hgpk_dbg(psmouse, "recalibrating touchpad..\n");
902
903 if (hgpk_force_recalibrate(psmouse))
904 hgpk_err(psmouse, "recalibration failed!\n");
905}
906
907static int hgpk_register(struct psmouse *psmouse)
908{
Daniel Drakeca94ec42010-11-11 22:19:57 -0800909 struct hgpk_data *priv = psmouse->private;
Andres Salomondf08ef22008-09-16 12:30:34 -0400910 int err;
911
Andres Salomondf08ef22008-09-16 12:30:34 -0400912 /* register handlers */
913 psmouse->protocol_handler = hgpk_process_byte;
914 psmouse->poll = hgpk_poll;
915 psmouse->disconnect = hgpk_disconnect;
916 psmouse->reconnect = hgpk_reconnect;
Andres Salomondf08ef22008-09-16 12:30:34 -0400917
918 /* Disable the idle resync. */
919 psmouse->resync_time = 0;
920 /* Reset after a lot of bad bytes. */
921 psmouse->resetafter = 1024;
922
Daniel Drakeca94ec42010-11-11 22:19:57 -0800923 hgpk_setup_input_device(psmouse->dev, NULL, priv->mode);
924
Andres Salomondf08ef22008-09-16 12:30:34 -0400925 err = device_create_file(&psmouse->ps2dev.serio->dev,
926 &psmouse_attr_powered.dattr);
Paul Foxc46dd1e2009-08-05 00:30:31 -0700927 if (err) {
928 hgpk_err(psmouse, "Failed creating 'powered' sysfs node\n");
929 return err;
930 }
Andres Salomondf08ef22008-09-16 12:30:34 -0400931
Daniel Drakeca94ec42010-11-11 22:19:57 -0800932 err = device_create_file(&psmouse->ps2dev.serio->dev,
933 &psmouse_attr_hgpk_mode.dattr);
934 if (err) {
935 hgpk_err(psmouse, "Failed creating 'hgpk_mode' sysfs node\n");
936 goto err_remove_powered;
937 }
938
Paul Foxc46dd1e2009-08-05 00:30:31 -0700939 /* C-series touchpads added the recalibrate command */
940 if (psmouse->model >= HGPK_MODEL_C) {
941 err = device_create_file(&psmouse->ps2dev.serio->dev,
942 &psmouse_attr_recalibrate.dattr);
943 if (err) {
944 hgpk_err(psmouse,
945 "Failed creating 'recalibrate' sysfs node\n");
Daniel Drakeca94ec42010-11-11 22:19:57 -0800946 goto err_remove_mode;
Paul Foxc46dd1e2009-08-05 00:30:31 -0700947 }
948 }
949
950 return 0;
Daniel Drakeca94ec42010-11-11 22:19:57 -0800951
952err_remove_mode:
953 device_remove_file(&psmouse->ps2dev.serio->dev,
954 &psmouse_attr_hgpk_mode.dattr);
955err_remove_powered:
956 device_remove_file(&psmouse->ps2dev.serio->dev,
957 &psmouse_attr_powered.dattr);
958 return err;
Andres Salomondf08ef22008-09-16 12:30:34 -0400959}
960
961int hgpk_init(struct psmouse *psmouse)
962{
963 struct hgpk_data *priv;
Daniel Drakeca94ec42010-11-11 22:19:57 -0800964 int err;
Andres Salomondf08ef22008-09-16 12:30:34 -0400965
966 priv = kzalloc(sizeof(struct hgpk_data), GFP_KERNEL);
Daniel Drakeca94ec42010-11-11 22:19:57 -0800967 if (!priv) {
968 err = -ENOMEM;
Andres Salomondf08ef22008-09-16 12:30:34 -0400969 goto alloc_fail;
Daniel Drakeca94ec42010-11-11 22:19:57 -0800970 }
Andres Salomondf08ef22008-09-16 12:30:34 -0400971
972 psmouse->private = priv;
Daniel Drakeca94ec42010-11-11 22:19:57 -0800973
Andres Salomondf08ef22008-09-16 12:30:34 -0400974 priv->psmouse = psmouse;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700975 priv->powered = true;
Daniel Drakeca94ec42010-11-11 22:19:57 -0800976 priv->mode = hgpk_default_mode;
Andres Salomondf08ef22008-09-16 12:30:34 -0400977 INIT_DELAYED_WORK(&priv->recalib_wq, hgpk_recalib_work);
978
Daniel Drakeca94ec42010-11-11 22:19:57 -0800979 err = hgpk_reset_device(psmouse, false);
Andres Salomondf08ef22008-09-16 12:30:34 -0400980 if (err)
981 goto init_fail;
982
983 err = hgpk_register(psmouse);
984 if (err)
985 goto init_fail;
986
987 return 0;
988
989init_fail:
990 kfree(priv);
991alloc_fail:
992 return err;
993}
994
995static enum hgpk_model_t hgpk_get_model(struct psmouse *psmouse)
996{
997 struct ps2dev *ps2dev = &psmouse->ps2dev;
998 unsigned char param[3];
999
1000 /* E7, E7, E7, E9 gets us a 3 byte identifier */
1001 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1002 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1003 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1004 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) {
1005 return -EIO;
1006 }
1007
Andy Whitcroft0f495482009-02-28 14:55:46 -08001008 hgpk_dbg(psmouse, "ID: %02x %02x %02x\n", param[0], param[1], param[2]);
Andres Salomondf08ef22008-09-16 12:30:34 -04001009
1010 /* HGPK signature: 0x67, 0x00, 0x<model> */
1011 if (param[0] != 0x67 || param[1] != 0x00)
1012 return -ENODEV;
1013
1014 hgpk_info(psmouse, "OLPC touchpad revision 0x%x\n", param[2]);
1015
1016 return param[2];
1017}
1018
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001019int hgpk_detect(struct psmouse *psmouse, bool set_properties)
Andres Salomondf08ef22008-09-16 12:30:34 -04001020{
1021 int version;
1022
1023 version = hgpk_get_model(psmouse);
1024 if (version < 0)
1025 return version;
1026
1027 if (set_properties) {
1028 psmouse->vendor = "ALPS";
1029 psmouse->name = "HGPK";
1030 psmouse->model = version;
1031 }
1032
1033 return 0;
1034}
Daniel Drakeca94ec42010-11-11 22:19:57 -08001035
1036void hgpk_module_init(void)
1037{
1038 hgpk_default_mode = hgpk_mode_from_name(hgpk_mode_name,
1039 strlen(hgpk_mode_name));
1040 if (hgpk_default_mode == HGPK_MODE_INVALID) {
1041 hgpk_default_mode = HGPK_MODE_MOUSE;
1042 strlcpy(hgpk_mode_name, hgpk_mode_names[HGPK_MODE_MOUSE],
1043 sizeof(hgpk_mode_name));
1044 }
1045}