blob: 4d17d9f3320ba821c1350c9d74bed595105ae4e0 [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>
Randy Dunlapab3d0ab2011-07-30 11:53:47 -070035#include <linux/module.h>
Andres Salomondf08ef22008-09-16 12:30:34 -040036#include <linux/serio.h>
37#include <linux/libps2.h>
38#include <linux/delay.h>
39#include <asm/olpc.h>
40
41#include "psmouse.h"
42#include "hgpk.h"
43
Daniel Drakea309cdc2010-11-11 22:20:03 -080044#define ILLEGAL_XY 999999
45
Dmitry Torokhova62f0d22010-05-19 10:39:17 -070046static bool tpdebug;
47module_param(tpdebug, bool, 0644);
Andres Salomondf08ef22008-09-16 12:30:34 -040048MODULE_PARM_DESC(tpdebug, "enable debugging, dumping packets to KERN_DEBUG.");
49
50static int recalib_delta = 100;
51module_param(recalib_delta, int, 0644);
52MODULE_PARM_DESC(recalib_delta,
Daniel Drakea309cdc2010-11-11 22:20:03 -080053 "packets containing a delta this large will be discarded, and a "
54 "recalibration may be scheduled.");
Andres Salomondf08ef22008-09-16 12:30:34 -040055
Daniel Drakea309cdc2010-11-11 22:20:03 -080056static int jumpy_delay = 20;
Paul Fox8bbf2702008-12-20 03:58:11 -050057module_param(jumpy_delay, int, 0644);
58MODULE_PARM_DESC(jumpy_delay,
59 "delay (ms) before recal after jumpiness detected");
60
Daniel Drakec0dc8342010-11-11 22:20:02 -080061static int spew_delay = 1;
Paul Fox8bbf2702008-12-20 03:58:11 -050062module_param(spew_delay, int, 0644);
63MODULE_PARM_DESC(spew_delay,
64 "delay (ms) before recal after packet spew detected");
65
Daniel Drake34caed22010-11-15 01:33:22 -080066static int recal_guard_time;
Paul Fox8bbf2702008-12-20 03:58:11 -050067module_param(recal_guard_time, int, 0644);
68MODULE_PARM_DESC(recal_guard_time,
69 "interval (ms) during which recal will be restarted if packet received");
70
Daniel Drake34caed22010-11-15 01:33:22 -080071static int post_interrupt_delay = 40;
Paul Fox8bbf2702008-12-20 03:58:11 -050072module_param(post_interrupt_delay, int, 0644);
73MODULE_PARM_DESC(post_interrupt_delay,
74 "delay (ms) before recal after recal interrupt detected");
75
Daniel Drake34caed22010-11-15 01:33:22 -080076static bool autorecal = true;
77module_param(autorecal, bool, 0644);
78MODULE_PARM_DESC(autorecal, "enable recalibration in the driver");
79
Daniel Drakeca94ec42010-11-11 22:19:57 -080080static char hgpk_mode_name[16];
81module_param_string(hgpk_mode, hgpk_mode_name, sizeof(hgpk_mode_name), 0644);
82MODULE_PARM_DESC(hgpk_mode,
83 "default hgpk mode: mouse, glidesensor or pentablet");
84
85static int hgpk_default_mode = HGPK_MODE_MOUSE;
86
87static const char * const hgpk_mode_names[] = {
88 [HGPK_MODE_MOUSE] = "Mouse",
89 [HGPK_MODE_GLIDESENSOR] = "GlideSensor",
90 [HGPK_MODE_PENTABLET] = "PenTablet",
91};
92
93static int hgpk_mode_from_name(const char *buf, int len)
94{
95 int i;
96
97 for (i = 0; i < ARRAY_SIZE(hgpk_mode_names); i++) {
98 const char *name = hgpk_mode_names[i];
99 if (strlen(name) == len && !strncasecmp(name, buf, len))
100 return i;
101 }
102
103 return HGPK_MODE_INVALID;
104}
105
Andres Salomondf08ef22008-09-16 12:30:34 -0400106/*
Daniel Drakea309cdc2010-11-11 22:20:03 -0800107 * see if new value is within 20% of half of old value
Andres Salomondf08ef22008-09-16 12:30:34 -0400108 */
Daniel Drakea309cdc2010-11-11 22:20:03 -0800109static int approx_half(int curr, int prev)
110{
111 int belowhalf, abovehalf;
112
113 if (curr < 5 || prev < 5)
114 return 0;
115
116 belowhalf = (prev * 8) / 20;
117 abovehalf = (prev * 12) / 20;
118
119 return belowhalf < curr && curr <= abovehalf;
120}
121
122/*
123 * Throw out oddly large delta packets, and any that immediately follow whose
124 * values are each approximately half of the previous. It seems that the ALPS
125 * firmware emits errant packets, and they get averaged out slowly.
126 */
127static int hgpk_discard_decay_hack(struct psmouse *psmouse, int x, int y)
Andres Salomondf08ef22008-09-16 12:30:34 -0400128{
129 struct hgpk_data *priv = psmouse->private;
Daniel Drakea309cdc2010-11-11 22:20:03 -0800130 int avx, avy;
131 bool do_recal = false;
Andres Salomondf08ef22008-09-16 12:30:34 -0400132
Daniel Drakea309cdc2010-11-11 22:20:03 -0800133 avx = abs(x);
134 avy = abs(y);
135
136 /* discard if too big, or half that but > 4 times the prev delta */
137 if (avx > recalib_delta ||
138 (avx > recalib_delta / 2 && ((avx / 4) > priv->xlast))) {
139 hgpk_err(psmouse, "detected %dpx jump in x\n", x);
140 priv->xbigj = avx;
141 } else if (approx_half(avx, priv->xbigj)) {
142 hgpk_err(psmouse, "detected secondary %dpx jump in x\n", x);
143 priv->xbigj = avx;
144 priv->xsaw_secondary++;
145 } else {
146 if (priv->xbigj && priv->xsaw_secondary > 1)
147 do_recal = true;
148 priv->xbigj = 0;
149 priv->xsaw_secondary = 0;
150 }
151
152 if (avy > recalib_delta ||
153 (avy > recalib_delta / 2 && ((avy / 4) > priv->ylast))) {
154 hgpk_err(psmouse, "detected %dpx jump in y\n", y);
155 priv->ybigj = avy;
156 } else if (approx_half(avy, priv->ybigj)) {
157 hgpk_err(psmouse, "detected secondary %dpx jump in y\n", y);
158 priv->ybigj = avy;
159 priv->ysaw_secondary++;
160 } else {
161 if (priv->ybigj && priv->ysaw_secondary > 1)
162 do_recal = true;
163 priv->ybigj = 0;
164 priv->ysaw_secondary = 0;
165 }
166
167 priv->xlast = avx;
168 priv->ylast = avy;
169
170 if (do_recal && jumpy_delay) {
171 hgpk_err(psmouse, "scheduling recalibration\n");
Andres Salomondf08ef22008-09-16 12:30:34 -0400172 psmouse_queue_work(psmouse, &priv->recalib_wq,
Paul Fox8bbf2702008-12-20 03:58:11 -0500173 msecs_to_jiffies(jumpy_delay));
Andres Salomondf08ef22008-09-16 12:30:34 -0400174 }
Daniel Drakea309cdc2010-11-11 22:20:03 -0800175
176 return priv->xbigj || priv->ybigj;
Andres Salomondf08ef22008-09-16 12:30:34 -0400177}
178
Daniel Drakec0dc8342010-11-11 22:20:02 -0800179static void hgpk_reset_spew_detection(struct hgpk_data *priv)
180{
181 priv->spew_count = 0;
182 priv->dupe_count = 0;
183 priv->x_tally = 0;
184 priv->y_tally = 0;
185 priv->spew_flag = NO_SPEW;
186}
187
188static void hgpk_reset_hack_state(struct psmouse *psmouse)
189{
190 struct hgpk_data *priv = psmouse->private;
191
192 priv->abs_x = priv->abs_y = -1;
Daniel Drakea309cdc2010-11-11 22:20:03 -0800193 priv->xlast = priv->ylast = ILLEGAL_XY;
194 priv->xbigj = priv->ybigj = 0;
195 priv->xsaw_secondary = priv->ysaw_secondary = 0;
Daniel Drakec0dc8342010-11-11 22:20:02 -0800196 hgpk_reset_spew_detection(priv);
197}
198
Andres Salomondf08ef22008-09-16 12:30:34 -0400199/*
200 * We have no idea why this particular hardware bug occurs. The touchpad
201 * will randomly start spewing packets without anything touching the
202 * pad. This wouldn't necessarily be bad, but it's indicative of a
203 * severely miscalibrated pad; attempting to use the touchpad while it's
204 * spewing means the cursor will jump all over the place, and act "drunk".
205 *
206 * The packets that are spewed tend to all have deltas between -2 and 2, and
207 * the cursor will move around without really going very far. It will
208 * tend to end up in the same location; if we tally up the changes over
209 * 100 packets, we end up w/ a final delta of close to 0. This happens
210 * pretty regularly when the touchpad is spewing, and is pretty hard to
211 * manually trigger (at least for *my* fingers). So, it makes a perfect
212 * scheme for detecting spews.
213 */
214static void hgpk_spewing_hack(struct psmouse *psmouse,
215 int l, int r, int x, int y)
216{
217 struct hgpk_data *priv = psmouse->private;
218
219 /* ignore button press packets; many in a row could trigger
220 * a false-positive! */
221 if (l || r)
222 return;
223
Daniel Drakec0dc8342010-11-11 22:20:02 -0800224 /* don't track spew if the workaround feature has been turned off */
225 if (!spew_delay)
226 return;
227
228 if (abs(x) > 3 || abs(y) > 3) {
229 /* no spew, or spew ended */
230 hgpk_reset_spew_detection(priv);
231 return;
232 }
233
234 /* Keep a tally of the overall delta to the cursor position caused by
235 * the spew */
Andres Salomondf08ef22008-09-16 12:30:34 -0400236 priv->x_tally += x;
237 priv->y_tally += y;
238
Daniel Drakec0dc8342010-11-11 22:20:02 -0800239 switch (priv->spew_flag) {
240 case NO_SPEW:
241 /* we're not spewing, but this packet might be the start */
242 priv->spew_flag = MAYBE_SPEWING;
243
244 /* fall-through */
245
246 case MAYBE_SPEWING:
247 priv->spew_count++;
248
249 if (priv->spew_count < SPEW_WATCH_COUNT)
250 break;
251
252 /* excessive spew detected, request recalibration */
253 priv->spew_flag = SPEW_DETECTED;
254
255 /* fall-through */
256
257 case SPEW_DETECTED:
258 /* only recalibrate when the overall delta to the cursor
259 * is really small. if the spew is causing significant cursor
260 * movement, it is probably a case of the user moving the
261 * cursor very slowly across the screen. */
Andres Salomondf08ef22008-09-16 12:30:34 -0400262 if (abs(priv->x_tally) < 3 && abs(priv->y_tally) < 3) {
Daniel Drakec0dc8342010-11-11 22:20:02 -0800263 hgpk_err(psmouse, "packet spew detected (%d,%d)\n",
Andres Salomondf08ef22008-09-16 12:30:34 -0400264 priv->x_tally, priv->y_tally);
Daniel Drakec0dc8342010-11-11 22:20:02 -0800265 priv->spew_flag = RECALIBRATING;
Andres Salomondf08ef22008-09-16 12:30:34 -0400266 psmouse_queue_work(psmouse, &priv->recalib_wq,
Paul Fox8bbf2702008-12-20 03:58:11 -0500267 msecs_to_jiffies(spew_delay));
Andres Salomondf08ef22008-09-16 12:30:34 -0400268 }
Daniel Drakec0dc8342010-11-11 22:20:02 -0800269
270 break;
271 case RECALIBRATING:
272 /* we already detected a spew and requested a recalibration,
273 * just wait for the queue to kick into action. */
274 break;
Andres Salomondf08ef22008-09-16 12:30:34 -0400275 }
276}
277
278/*
279 * HGPK Mouse Mode format (standard mouse format, sans middle button)
280 *
281 * byte 0: y-over x-over y-neg x-neg 1 0 swr swl
282 * byte 1: x7 x6 x5 x4 x3 x2 x1 x0
283 * byte 2: y7 y6 y5 y4 y3 y2 y1 y0
284 *
285 * swr/swl are the left/right buttons.
286 * x-neg/y-neg are the x and y delta negative bits
287 * x-over/y-over are the x and y overflow bits
Daniel Drakeca94ec42010-11-11 22:19:57 -0800288 *
289 * ---
290 *
291 * HGPK Advanced Mode - single-mode format
292 *
293 * byte 0(PT): 1 1 0 0 1 1 1 1
294 * byte 0(GS): 1 1 1 1 1 1 1 1
295 * byte 1: 0 x6 x5 x4 x3 x2 x1 x0
296 * byte 2(PT): 0 0 x9 x8 x7 ? pt-dsw 0
297 * byte 2(GS): 0 x10 x9 x8 x7 ? gs-dsw pt-dsw
298 * byte 3: 0 y9 y8 y7 1 0 swr swl
299 * byte 4: 0 y6 y5 y4 y3 y2 y1 y0
300 * byte 5: 0 z6 z5 z4 z3 z2 z1 z0
301 *
302 * ?'s are not defined in the protocol spec, may vary between models.
303 *
304 * swr/swl are the left/right buttons.
305 *
306 * pt-dsw/gs-dsw indicate that the pt/gs sensor is detecting a
307 * pen/finger
Andres Salomondf08ef22008-09-16 12:30:34 -0400308 */
Daniel Drakeca94ec42010-11-11 22:19:57 -0800309static bool hgpk_is_byte_valid(struct psmouse *psmouse, unsigned char *packet)
Andres Salomondf08ef22008-09-16 12:30:34 -0400310{
Daniel Drakeca94ec42010-11-11 22:19:57 -0800311 struct hgpk_data *priv = psmouse->private;
312 int pktcnt = psmouse->pktcnt;
313 bool valid;
314
315 switch (priv->mode) {
316 case HGPK_MODE_MOUSE:
317 valid = (packet[0] & 0x0C) == 0x08;
318 break;
319
320 case HGPK_MODE_GLIDESENSOR:
321 valid = pktcnt == 1 ?
322 packet[0] == HGPK_GS : !(packet[pktcnt - 1] & 0x80);
323 break;
324
325 case HGPK_MODE_PENTABLET:
326 valid = pktcnt == 1 ?
327 packet[0] == HGPK_PT : !(packet[pktcnt - 1] & 0x80);
328 break;
329
330 default:
331 valid = false;
332 break;
333 }
334
335 if (!valid)
336 hgpk_dbg(psmouse,
337 "bad data, mode %d (%d) %02x %02x %02x %02x %02x %02x\n",
338 priv->mode, pktcnt,
339 psmouse->packet[0], psmouse->packet[1],
340 psmouse->packet[2], psmouse->packet[3],
341 psmouse->packet[4], psmouse->packet[5]);
342
343 return valid;
Andres Salomondf08ef22008-09-16 12:30:34 -0400344}
345
Daniel Drakeca94ec42010-11-11 22:19:57 -0800346static void hgpk_process_advanced_packet(struct psmouse *psmouse)
347{
348 struct hgpk_data *priv = psmouse->private;
349 struct input_dev *idev = psmouse->dev;
350 unsigned char *packet = psmouse->packet;
351 int down = !!(packet[2] & 2);
352 int left = !!(packet[3] & 1);
353 int right = !!(packet[3] & 2);
354 int x = packet[1] | ((packet[2] & 0x78) << 4);
355 int y = packet[4] | ((packet[3] & 0x70) << 3);
356
357 if (priv->mode == HGPK_MODE_GLIDESENSOR) {
358 int pt_down = !!(packet[2] & 1);
359 int finger_down = !!(packet[2] & 2);
360 int z = packet[5];
361
362 input_report_abs(idev, ABS_PRESSURE, z);
363 if (tpdebug)
364 hgpk_dbg(psmouse, "pd=%d fd=%d z=%d",
365 pt_down, finger_down, z);
366 } else {
367 /*
368 * PenTablet mode does not report pressure, so we don't
369 * report it here
370 */
371 if (tpdebug)
372 hgpk_dbg(psmouse, "pd=%d ", down);
373 }
374
375 if (tpdebug)
376 hgpk_dbg(psmouse, "l=%d r=%d x=%d y=%d\n", left, right, x, y);
377
378 input_report_key(idev, BTN_TOUCH, down);
379 input_report_key(idev, BTN_LEFT, left);
380 input_report_key(idev, BTN_RIGHT, right);
381
382 /*
383 * If this packet says that the finger was removed, reset our position
384 * tracking so that we don't erroneously detect a jump on next press.
385 */
Daniel Drakec0dc8342010-11-11 22:20:02 -0800386 if (!down) {
Daniel Drakea309cdc2010-11-11 22:20:03 -0800387 hgpk_reset_hack_state(psmouse);
Daniel Drakec0dc8342010-11-11 22:20:02 -0800388 goto done;
Daniel Drakeca94ec42010-11-11 22:19:57 -0800389 }
390
Daniel Drakec0dc8342010-11-11 22:20:02 -0800391 /*
392 * Weed out duplicate packets (we get quite a few, and they mess up
393 * our jump detection)
394 */
395 if (x == priv->abs_x && y == priv->abs_y) {
396 if (++priv->dupe_count > SPEW_WATCH_COUNT) {
397 if (tpdebug)
398 hgpk_dbg(psmouse, "hard spew detected\n");
399 priv->spew_flag = RECALIBRATING;
400 psmouse_queue_work(psmouse, &priv->recalib_wq,
401 msecs_to_jiffies(spew_delay));
402 }
403 goto done;
404 }
405
406 /* not a duplicate, continue with position reporting */
407 priv->dupe_count = 0;
408
409 /* Don't apply hacks in PT mode, it seems reliable */
410 if (priv->mode != HGPK_MODE_PENTABLET && priv->abs_x != -1) {
Daniel Drakea309cdc2010-11-11 22:20:03 -0800411 int x_diff = priv->abs_x - x;
412 int y_diff = priv->abs_y - y;
413 if (hgpk_discard_decay_hack(psmouse, x_diff, y_diff)) {
414 if (tpdebug)
415 hgpk_dbg(psmouse, "discarding\n");
416 goto done;
417 }
418 hgpk_spewing_hack(psmouse, left, right, x_diff, y_diff);
Daniel Drakec0dc8342010-11-11 22:20:02 -0800419 }
420
421 input_report_abs(idev, ABS_X, x);
422 input_report_abs(idev, ABS_Y, y);
423 priv->abs_x = x;
424 priv->abs_y = y;
425
426done:
Daniel Drakeca94ec42010-11-11 22:19:57 -0800427 input_sync(idev);
428}
429
430static void hgpk_process_simple_packet(struct psmouse *psmouse)
Andres Salomondf08ef22008-09-16 12:30:34 -0400431{
432 struct input_dev *dev = psmouse->dev;
433 unsigned char *packet = psmouse->packet;
Daniel Drakeca94ec42010-11-11 22:19:57 -0800434 int left = packet[0] & 1;
435 int right = (packet[0] >> 1) & 1;
436 int x = packet[1] - ((packet[0] << 4) & 0x100);
437 int y = ((packet[0] << 3) & 0x100) - packet[2];
Andres Salomondf08ef22008-09-16 12:30:34 -0400438
Daniel Drake67f56bb2010-11-15 01:28:54 -0800439 if (packet[0] & 0xc0)
440 hgpk_dbg(psmouse,
441 "overflow -- 0x%02x 0x%02x 0x%02x\n",
442 packet[0], packet[1], packet[2]);
443
Daniel Drakea309cdc2010-11-11 22:20:03 -0800444 if (hgpk_discard_decay_hack(psmouse, x, y)) {
445 if (tpdebug)
446 hgpk_dbg(psmouse, "discarding\n");
447 return;
448 }
449
Andres Salomondf08ef22008-09-16 12:30:34 -0400450 hgpk_spewing_hack(psmouse, left, right, x, y);
451
452 if (tpdebug)
453 hgpk_dbg(psmouse, "l=%d r=%d x=%d y=%d\n", left, right, x, y);
454
455 input_report_key(dev, BTN_LEFT, left);
456 input_report_key(dev, BTN_RIGHT, right);
457
458 input_report_rel(dev, REL_X, x);
459 input_report_rel(dev, REL_Y, y);
460
461 input_sync(dev);
462}
463
464static psmouse_ret_t hgpk_process_byte(struct psmouse *psmouse)
465{
466 struct hgpk_data *priv = psmouse->private;
467
Daniel Drakeca94ec42010-11-11 22:19:57 -0800468 if (!hgpk_is_byte_valid(psmouse, psmouse->packet))
Andres Salomondf08ef22008-09-16 12:30:34 -0400469 return PSMOUSE_BAD_DATA;
Andres Salomondf08ef22008-09-16 12:30:34 -0400470
471 if (psmouse->pktcnt >= psmouse->pktsize) {
Daniel Drakeca94ec42010-11-11 22:19:57 -0800472 if (priv->mode == HGPK_MODE_MOUSE)
473 hgpk_process_simple_packet(psmouse);
474 else
475 hgpk_process_advanced_packet(psmouse);
Andres Salomondf08ef22008-09-16 12:30:34 -0400476 return PSMOUSE_FULL_PACKET;
477 }
478
479 if (priv->recalib_window) {
480 if (time_before(jiffies, priv->recalib_window)) {
481 /*
482 * ugh, got a packet inside our recalibration
483 * window, schedule another recalibration.
484 */
485 hgpk_dbg(psmouse,
486 "packet inside calibration window, "
487 "queueing another recalibration\n");
488 psmouse_queue_work(psmouse, &priv->recalib_wq,
Paul Fox8bbf2702008-12-20 03:58:11 -0500489 msecs_to_jiffies(post_interrupt_delay));
Andres Salomondf08ef22008-09-16 12:30:34 -0400490 }
491 priv->recalib_window = 0;
492 }
493
494 return PSMOUSE_GOOD_DATA;
495}
496
Daniel Drakeca94ec42010-11-11 22:19:57 -0800497static int hgpk_select_mode(struct psmouse *psmouse)
498{
499 struct ps2dev *ps2dev = &psmouse->ps2dev;
500 struct hgpk_data *priv = psmouse->private;
501 int i;
502 int cmd;
503
504 /*
505 * 4 disables to enable advanced mode
506 * then 3 0xf2 bytes as the preamble for GS/PT selection
507 */
508 const int advanced_init[] = {
509 PSMOUSE_CMD_DISABLE, PSMOUSE_CMD_DISABLE,
510 PSMOUSE_CMD_DISABLE, PSMOUSE_CMD_DISABLE,
511 0xf2, 0xf2, 0xf2,
512 };
513
514 switch (priv->mode) {
515 case HGPK_MODE_MOUSE:
516 psmouse->pktsize = 3;
517 break;
518
519 case HGPK_MODE_GLIDESENSOR:
520 case HGPK_MODE_PENTABLET:
521 psmouse->pktsize = 6;
522
523 /* Switch to 'Advanced mode.', four disables in a row. */
524 for (i = 0; i < ARRAY_SIZE(advanced_init); i++)
525 if (ps2_command(ps2dev, NULL, advanced_init[i]))
526 return -EIO;
527
528 /* select between GlideSensor (mouse) or PenTablet */
529 cmd = priv->mode == HGPK_MODE_GLIDESENSOR ?
530 PSMOUSE_CMD_SETSCALE11 : PSMOUSE_CMD_SETSCALE21;
531
532 if (ps2_command(ps2dev, NULL, cmd))
533 return -EIO;
534 break;
535
536 default:
537 return -EINVAL;
538 }
539
540 return 0;
541}
542
543static void hgpk_setup_input_device(struct input_dev *input,
544 struct input_dev *old_input,
545 enum hgpk_mode mode)
546{
547 if (old_input) {
548 input->name = old_input->name;
549 input->phys = old_input->phys;
550 input->id = old_input->id;
551 input->dev.parent = old_input->dev.parent;
552 }
553
554 memset(input->evbit, 0, sizeof(input->evbit));
555 memset(input->relbit, 0, sizeof(input->relbit));
556 memset(input->keybit, 0, sizeof(input->keybit));
557
558 /* All modes report left and right buttons */
559 __set_bit(EV_KEY, input->evbit);
560 __set_bit(BTN_LEFT, input->keybit);
561 __set_bit(BTN_RIGHT, input->keybit);
562
563 switch (mode) {
564 case HGPK_MODE_MOUSE:
565 __set_bit(EV_REL, input->evbit);
566 __set_bit(REL_X, input->relbit);
567 __set_bit(REL_Y, input->relbit);
568 break;
569
570 case HGPK_MODE_GLIDESENSOR:
571 __set_bit(BTN_TOUCH, input->keybit);
572 __set_bit(BTN_TOOL_FINGER, input->keybit);
573
574 __set_bit(EV_ABS, input->evbit);
575
576 /* GlideSensor has pressure sensor, PenTablet does not */
577 input_set_abs_params(input, ABS_PRESSURE, 0, 15, 0, 0);
578
579 /* From device specs */
580 input_set_abs_params(input, ABS_X, 0, 399, 0, 0);
581 input_set_abs_params(input, ABS_Y, 0, 290, 0, 0);
582
583 /* Calculated by hand based on usable size (52mm x 38mm) */
584 input_abs_set_res(input, ABS_X, 8);
585 input_abs_set_res(input, ABS_Y, 8);
586 break;
587
588 case HGPK_MODE_PENTABLET:
589 __set_bit(BTN_TOUCH, input->keybit);
590 __set_bit(BTN_TOOL_FINGER, input->keybit);
591
592 __set_bit(EV_ABS, input->evbit);
593
594 /* From device specs */
595 input_set_abs_params(input, ABS_X, 0, 999, 0, 0);
596 input_set_abs_params(input, ABS_Y, 5, 239, 0, 0);
597
598 /* Calculated by hand based on usable size (156mm x 38mm) */
599 input_abs_set_res(input, ABS_X, 6);
600 input_abs_set_res(input, ABS_Y, 8);
601 break;
602
603 default:
604 BUG();
605 }
606}
607
Daniel Drakeca94ec42010-11-11 22:19:57 -0800608static int hgpk_reset_device(struct psmouse *psmouse, bool recalibrate)
609{
610 int err;
611
612 psmouse_reset(psmouse);
613
614 if (recalibrate) {
615 struct ps2dev *ps2dev = &psmouse->ps2dev;
616
617 /* send the recalibrate request */
618 if (ps2_command(ps2dev, NULL, 0xf5) ||
619 ps2_command(ps2dev, NULL, 0xf5) ||
620 ps2_command(ps2dev, NULL, 0xe6) ||
621 ps2_command(ps2dev, NULL, 0xf5)) {
622 return -1;
623 }
624
625 /* according to ALPS, 150mS is required for recalibration */
626 msleep(150);
627 }
628
629 err = hgpk_select_mode(psmouse);
630 if (err) {
631 hgpk_err(psmouse, "failed to select mode\n");
632 return err;
633 }
634
635 hgpk_reset_hack_state(psmouse);
636
637 return 0;
638}
639
Andres Salomondf08ef22008-09-16 12:30:34 -0400640static int hgpk_force_recalibrate(struct psmouse *psmouse)
641{
642 struct ps2dev *ps2dev = &psmouse->ps2dev;
643 struct hgpk_data *priv = psmouse->private;
Daniel Drakeca94ec42010-11-11 22:19:57 -0800644 int err;
Andres Salomondf08ef22008-09-16 12:30:34 -0400645
646 /* C-series touchpads added the recalibrate command */
647 if (psmouse->model < HGPK_MODEL_C)
648 return 0;
649
Daniel Drake34caed22010-11-15 01:33:22 -0800650 if (!autorecal) {
651 hgpk_dbg(psmouse, "recalibrations disabled, ignoring\n");
652 return 0;
653 }
654
655 hgpk_dbg(psmouse, "recalibrating touchpad..\n");
656
Andres Salomondf08ef22008-09-16 12:30:34 -0400657 /* we don't want to race with the irq handler, nor with resyncs */
658 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
659
660 /* start by resetting the device */
Daniel Drakeca94ec42010-11-11 22:19:57 -0800661 err = hgpk_reset_device(psmouse, true);
662 if (err)
663 return err;
Andres Salomondf08ef22008-09-16 12:30:34 -0400664
Daniel Drakeca94ec42010-11-11 22:19:57 -0800665 /*
666 * XXX: If a finger is down during this delay, recalibration will
Andres Salomondf08ef22008-09-16 12:30:34 -0400667 * detect capacitance incorrectly. This is a hardware bug, and
668 * we don't have a good way to deal with it. The 2s window stuff
669 * (below) is our best option for now.
670 */
671
672 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
673 return -1;
674
675 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
676
Daniel Drake34caed22010-11-15 01:33:22 -0800677 if (tpdebug)
678 hgpk_dbg(psmouse, "touchpad reactivated\n");
679
Daniel Drakeca94ec42010-11-11 22:19:57 -0800680 /*
Daniel Drake34caed22010-11-15 01:33:22 -0800681 * If we get packets right away after recalibrating, it's likely
682 * that a finger was on the touchpad. If so, it's probably
683 * miscalibrated, so we optionally schedule another.
Andres Salomondf08ef22008-09-16 12:30:34 -0400684 */
Daniel Drake34caed22010-11-15 01:33:22 -0800685 if (recal_guard_time)
686 priv->recalib_window = jiffies +
687 msecs_to_jiffies(recal_guard_time);
Andres Salomondf08ef22008-09-16 12:30:34 -0400688
689 return 0;
690}
691
692/*
Paul Fox20a4c262010-11-15 01:33:24 -0800693 * This puts the touchpad in a power saving mode; according to ALPS, current
694 * consumption goes down to 50uA after running this. To turn power back on,
695 * we drive MS-DAT low. Measuring with a 1mA resolution ammeter says that
696 * the current on the SUS_3.3V rail drops from 3mA or 4mA to 0 when we do this.
697 *
698 * We have no formal spec that details this operation -- the low-power
699 * sequence came from a long-lost email trail.
Andres Salomondf08ef22008-09-16 12:30:34 -0400700 */
Paul Fox20a4c262010-11-15 01:33:24 -0800701static int hgpk_toggle_powersave(struct psmouse *psmouse, int enable)
Andres Salomondf08ef22008-09-16 12:30:34 -0400702{
703 struct ps2dev *ps2dev = &psmouse->ps2dev;
704 int timeo;
Daniel Drakeca94ec42010-11-11 22:19:57 -0800705 int err;
Andres Salomondf08ef22008-09-16 12:30:34 -0400706
707 /* Added on D-series touchpads */
708 if (psmouse->model < HGPK_MODEL_D)
709 return 0;
710
711 if (enable) {
712 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
713
714 /*
715 * Sending a byte will drive MS-DAT low; this will wake up
716 * the controller. Once we get an ACK back from it, it
717 * means we can continue with the touchpad re-init. ALPS
718 * tells us that 1s should be long enough, so set that as
Paul Fox20a4c262010-11-15 01:33:24 -0800719 * the upper bound. (in practice, it takes about 3 loops.)
Andres Salomondf08ef22008-09-16 12:30:34 -0400720 */
721 for (timeo = 20; timeo > 0; timeo--) {
722 if (!ps2_sendbyte(&psmouse->ps2dev,
723 PSMOUSE_CMD_DISABLE, 20))
724 break;
Paul Fox20a4c262010-11-15 01:33:24 -0800725 msleep(25);
Andres Salomondf08ef22008-09-16 12:30:34 -0400726 }
727
Daniel Drakeca94ec42010-11-11 22:19:57 -0800728 err = hgpk_reset_device(psmouse, false);
729 if (err) {
730 hgpk_err(psmouse, "Failed to reset device!\n");
731 return err;
732 }
Andres Salomondf08ef22008-09-16 12:30:34 -0400733
734 /* should be all set, enable the touchpad */
735 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE);
736 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
Paul Fox20a4c262010-11-15 01:33:24 -0800737 hgpk_dbg(psmouse, "Touchpad powered up.\n");
Andres Salomondf08ef22008-09-16 12:30:34 -0400738 } else {
739 hgpk_dbg(psmouse, "Powering off touchpad.\n");
Andres Salomondf08ef22008-09-16 12:30:34 -0400740
741 if (ps2_command(ps2dev, NULL, 0xec) ||
742 ps2_command(ps2dev, NULL, 0xec) ||
743 ps2_command(ps2dev, NULL, 0xea)) {
744 return -1;
745 }
746
Paul Fox20a4c262010-11-15 01:33:24 -0800747 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
748
Andres Salomondf08ef22008-09-16 12:30:34 -0400749 /* probably won't see an ACK, the touchpad will be off */
750 ps2_sendbyte(&psmouse->ps2dev, 0xec, 20);
751 }
752
753 return 0;
754}
755
756static int hgpk_poll(struct psmouse *psmouse)
757{
758 /* We can't poll, so always return failure. */
759 return -1;
760}
761
762static int hgpk_reconnect(struct psmouse *psmouse)
763{
Paul Fox20a4c262010-11-15 01:33:24 -0800764 struct hgpk_data *priv = psmouse->private;
765
Daniel Drakeca94ec42010-11-11 22:19:57 -0800766 /*
767 * During suspend/resume the ps2 rails remain powered. We don't want
Andres Salomondf08ef22008-09-16 12:30:34 -0400768 * to do a reset because it's flush data out of buffers; however,
Daniel Drakeca94ec42010-11-11 22:19:57 -0800769 * earlier prototypes (B1) had some brokenness that required a reset.
770 */
Andres Salomondf08ef22008-09-16 12:30:34 -0400771 if (olpc_board_at_least(olpc_board(0xb2)))
772 if (psmouse->ps2dev.serio->dev.power.power_state.event !=
773 PM_EVENT_ON)
774 return 0;
775
Paul Fox20a4c262010-11-15 01:33:24 -0800776 priv->powered = 1;
Daniel Drakeca94ec42010-11-11 22:19:57 -0800777 return hgpk_reset_device(psmouse, false);
Andres Salomondf08ef22008-09-16 12:30:34 -0400778}
779
780static ssize_t hgpk_show_powered(struct psmouse *psmouse, void *data, char *buf)
781{
782 struct hgpk_data *priv = psmouse->private;
783
784 return sprintf(buf, "%d\n", priv->powered);
785}
786
787static ssize_t hgpk_set_powered(struct psmouse *psmouse, void *data,
788 const char *buf, size_t count)
789{
790 struct hgpk_data *priv = psmouse->private;
791 unsigned long value;
792 int err;
793
794 err = strict_strtoul(buf, 10, &value);
795 if (err || value > 1)
796 return -EINVAL;
797
798 if (value != priv->powered) {
799 /*
800 * hgpk_toggle_power will deal w/ state so
801 * we're not racing w/ irq
802 */
Paul Fox20a4c262010-11-15 01:33:24 -0800803 err = hgpk_toggle_powersave(psmouse, value);
Andres Salomondf08ef22008-09-16 12:30:34 -0400804 if (!err)
805 priv->powered = value;
806 }
807
808 return err ? err : count;
809}
810
811__PSMOUSE_DEFINE_ATTR(powered, S_IWUSR | S_IRUGO, NULL,
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700812 hgpk_show_powered, hgpk_set_powered, false);
Andres Salomondf08ef22008-09-16 12:30:34 -0400813
Daniel Drakeca94ec42010-11-11 22:19:57 -0800814static ssize_t attr_show_mode(struct psmouse *psmouse, void *data, char *buf)
815{
816 struct hgpk_data *priv = psmouse->private;
817
818 return sprintf(buf, "%s\n", hgpk_mode_names[priv->mode]);
819}
820
821static ssize_t attr_set_mode(struct psmouse *psmouse, void *data,
822 const char *buf, size_t len)
823{
824 struct hgpk_data *priv = psmouse->private;
825 enum hgpk_mode old_mode = priv->mode;
826 enum hgpk_mode new_mode = hgpk_mode_from_name(buf, len);
827 struct input_dev *old_dev = psmouse->dev;
828 struct input_dev *new_dev;
829 int err;
830
831 if (new_mode == HGPK_MODE_INVALID)
832 return -EINVAL;
833
834 if (old_mode == new_mode)
835 return len;
836
837 new_dev = input_allocate_device();
838 if (!new_dev)
839 return -ENOMEM;
840
841 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
842
843 /* Switch device into the new mode */
844 priv->mode = new_mode;
845 err = hgpk_reset_device(psmouse, false);
846 if (err)
847 goto err_try_restore;
848
849 hgpk_setup_input_device(new_dev, old_dev, new_mode);
850
851 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
852
853 err = input_register_device(new_dev);
854 if (err)
855 goto err_try_restore;
856
857 psmouse->dev = new_dev;
858 input_unregister_device(old_dev);
859
860 return len;
861
862err_try_restore:
863 input_free_device(new_dev);
864 priv->mode = old_mode;
865 hgpk_reset_device(psmouse, false);
866
867 return err;
868}
869
870PSMOUSE_DEFINE_ATTR(hgpk_mode, S_IWUSR | S_IRUGO, NULL,
871 attr_show_mode, attr_set_mode);
872
Paul Foxc46dd1e2009-08-05 00:30:31 -0700873static ssize_t hgpk_trigger_recal_show(struct psmouse *psmouse,
874 void *data, char *buf)
875{
876 return -EINVAL;
877}
878
879static ssize_t hgpk_trigger_recal(struct psmouse *psmouse, void *data,
880 const char *buf, size_t count)
881{
882 struct hgpk_data *priv = psmouse->private;
883 unsigned long value;
884 int err;
885
886 err = strict_strtoul(buf, 10, &value);
887 if (err || value != 1)
888 return -EINVAL;
889
890 /*
891 * We queue work instead of doing recalibration right here
892 * to avoid adding locking to to hgpk_force_recalibrate()
893 * since workqueue provides serialization.
894 */
895 psmouse_queue_work(psmouse, &priv->recalib_wq, 0);
896 return count;
897}
898
899__PSMOUSE_DEFINE_ATTR(recalibrate, S_IWUSR | S_IRUGO, NULL,
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700900 hgpk_trigger_recal_show, hgpk_trigger_recal, false);
Paul Foxc46dd1e2009-08-05 00:30:31 -0700901
Andres Salomondf08ef22008-09-16 12:30:34 -0400902static void hgpk_disconnect(struct psmouse *psmouse)
903{
904 struct hgpk_data *priv = psmouse->private;
905
906 device_remove_file(&psmouse->ps2dev.serio->dev,
907 &psmouse_attr_powered.dattr);
Daniel Drakeca94ec42010-11-11 22:19:57 -0800908 device_remove_file(&psmouse->ps2dev.serio->dev,
909 &psmouse_attr_hgpk_mode.dattr);
Paul Foxc46dd1e2009-08-05 00:30:31 -0700910
911 if (psmouse->model >= HGPK_MODEL_C)
912 device_remove_file(&psmouse->ps2dev.serio->dev,
913 &psmouse_attr_recalibrate.dattr);
914
Andres Salomondf08ef22008-09-16 12:30:34 -0400915 psmouse_reset(psmouse);
916 kfree(priv);
917}
918
919static void hgpk_recalib_work(struct work_struct *work)
920{
Jean Delvarebf6aede2009-04-02 16:56:54 -0700921 struct delayed_work *w = to_delayed_work(work);
Andres Salomondf08ef22008-09-16 12:30:34 -0400922 struct hgpk_data *priv = container_of(w, struct hgpk_data, recalib_wq);
923 struct psmouse *psmouse = priv->psmouse;
924
Andres Salomondf08ef22008-09-16 12:30:34 -0400925 if (hgpk_force_recalibrate(psmouse))
926 hgpk_err(psmouse, "recalibration failed!\n");
927}
928
929static int hgpk_register(struct psmouse *psmouse)
930{
Daniel Drakeca94ec42010-11-11 22:19:57 -0800931 struct hgpk_data *priv = psmouse->private;
Andres Salomondf08ef22008-09-16 12:30:34 -0400932 int err;
933
Andres Salomondf08ef22008-09-16 12:30:34 -0400934 /* register handlers */
935 psmouse->protocol_handler = hgpk_process_byte;
936 psmouse->poll = hgpk_poll;
937 psmouse->disconnect = hgpk_disconnect;
938 psmouse->reconnect = hgpk_reconnect;
Andres Salomondf08ef22008-09-16 12:30:34 -0400939
940 /* Disable the idle resync. */
941 psmouse->resync_time = 0;
942 /* Reset after a lot of bad bytes. */
943 psmouse->resetafter = 1024;
944
Daniel Drakeca94ec42010-11-11 22:19:57 -0800945 hgpk_setup_input_device(psmouse->dev, NULL, priv->mode);
946
Andres Salomondf08ef22008-09-16 12:30:34 -0400947 err = device_create_file(&psmouse->ps2dev.serio->dev,
948 &psmouse_attr_powered.dattr);
Paul Foxc46dd1e2009-08-05 00:30:31 -0700949 if (err) {
950 hgpk_err(psmouse, "Failed creating 'powered' sysfs node\n");
951 return err;
952 }
Andres Salomondf08ef22008-09-16 12:30:34 -0400953
Daniel Drakeca94ec42010-11-11 22:19:57 -0800954 err = device_create_file(&psmouse->ps2dev.serio->dev,
955 &psmouse_attr_hgpk_mode.dattr);
956 if (err) {
957 hgpk_err(psmouse, "Failed creating 'hgpk_mode' sysfs node\n");
958 goto err_remove_powered;
959 }
960
Paul Foxc46dd1e2009-08-05 00:30:31 -0700961 /* C-series touchpads added the recalibrate command */
962 if (psmouse->model >= HGPK_MODEL_C) {
963 err = device_create_file(&psmouse->ps2dev.serio->dev,
964 &psmouse_attr_recalibrate.dattr);
965 if (err) {
966 hgpk_err(psmouse,
967 "Failed creating 'recalibrate' sysfs node\n");
Daniel Drakeca94ec42010-11-11 22:19:57 -0800968 goto err_remove_mode;
Paul Foxc46dd1e2009-08-05 00:30:31 -0700969 }
970 }
971
972 return 0;
Daniel Drakeca94ec42010-11-11 22:19:57 -0800973
974err_remove_mode:
975 device_remove_file(&psmouse->ps2dev.serio->dev,
976 &psmouse_attr_hgpk_mode.dattr);
977err_remove_powered:
978 device_remove_file(&psmouse->ps2dev.serio->dev,
979 &psmouse_attr_powered.dattr);
980 return err;
Andres Salomondf08ef22008-09-16 12:30:34 -0400981}
982
983int hgpk_init(struct psmouse *psmouse)
984{
985 struct hgpk_data *priv;
Daniel Drakeca94ec42010-11-11 22:19:57 -0800986 int err;
Andres Salomondf08ef22008-09-16 12:30:34 -0400987
988 priv = kzalloc(sizeof(struct hgpk_data), GFP_KERNEL);
Daniel Drakeca94ec42010-11-11 22:19:57 -0800989 if (!priv) {
990 err = -ENOMEM;
Andres Salomondf08ef22008-09-16 12:30:34 -0400991 goto alloc_fail;
Daniel Drakeca94ec42010-11-11 22:19:57 -0800992 }
Andres Salomondf08ef22008-09-16 12:30:34 -0400993
994 psmouse->private = priv;
Daniel Drakeca94ec42010-11-11 22:19:57 -0800995
Andres Salomondf08ef22008-09-16 12:30:34 -0400996 priv->psmouse = psmouse;
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700997 priv->powered = true;
Daniel Drakeca94ec42010-11-11 22:19:57 -0800998 priv->mode = hgpk_default_mode;
Andres Salomondf08ef22008-09-16 12:30:34 -0400999 INIT_DELAYED_WORK(&priv->recalib_wq, hgpk_recalib_work);
1000
Daniel Drakeca94ec42010-11-11 22:19:57 -08001001 err = hgpk_reset_device(psmouse, false);
Andres Salomondf08ef22008-09-16 12:30:34 -04001002 if (err)
1003 goto init_fail;
1004
1005 err = hgpk_register(psmouse);
1006 if (err)
1007 goto init_fail;
1008
1009 return 0;
1010
1011init_fail:
1012 kfree(priv);
1013alloc_fail:
1014 return err;
1015}
1016
1017static enum hgpk_model_t hgpk_get_model(struct psmouse *psmouse)
1018{
1019 struct ps2dev *ps2dev = &psmouse->ps2dev;
1020 unsigned char param[3];
1021
1022 /* E7, E7, E7, E9 gets us a 3 byte identifier */
1023 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1024 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1025 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1026 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) {
1027 return -EIO;
1028 }
1029
Andy Whitcroft0f495482009-02-28 14:55:46 -08001030 hgpk_dbg(psmouse, "ID: %02x %02x %02x\n", param[0], param[1], param[2]);
Andres Salomondf08ef22008-09-16 12:30:34 -04001031
1032 /* HGPK signature: 0x67, 0x00, 0x<model> */
1033 if (param[0] != 0x67 || param[1] != 0x00)
1034 return -ENODEV;
1035
1036 hgpk_info(psmouse, "OLPC touchpad revision 0x%x\n", param[2]);
1037
1038 return param[2];
1039}
1040
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001041int hgpk_detect(struct psmouse *psmouse, bool set_properties)
Andres Salomondf08ef22008-09-16 12:30:34 -04001042{
1043 int version;
1044
1045 version = hgpk_get_model(psmouse);
1046 if (version < 0)
1047 return version;
1048
1049 if (set_properties) {
1050 psmouse->vendor = "ALPS";
1051 psmouse->name = "HGPK";
1052 psmouse->model = version;
1053 }
1054
1055 return 0;
1056}
Daniel Drakeca94ec42010-11-11 22:19:57 -08001057
1058void hgpk_module_init(void)
1059{
1060 hgpk_default_mode = hgpk_mode_from_name(hgpk_mode_name,
1061 strlen(hgpk_mode_name));
1062 if (hgpk_default_mode == HGPK_MODE_INVALID) {
1063 hgpk_default_mode = HGPK_MODE_MOUSE;
1064 strlcpy(hgpk_mode_name, hgpk_mode_names[HGPK_MODE_MOUSE],
1065 sizeof(hgpk_mode_name));
1066 }
1067}