Commit | Line | Data |
---|---|---|
df08ef27 AS |
1 | /* |
2 | * OLPC HGPK (XO-1) touchpad PS/2 mouse driver | |
3 | * | |
4 | * Copyright (c) 2006-2008 One Laptop Per Child | |
5 | * Authors: | |
6 | * Zephaniah E. Hull | |
7 | * Andres Salomon <dilinger@debian.org> | |
8 | * | |
9 | * This driver is partly based on the ALPS driver, which is: | |
10 | * | |
11 | * Copyright (c) 2003 Neil Brown <neilb@cse.unsw.edu.au> | |
12 | * Copyright (c) 2003-2005 Peter Osterlund <petero2@telia.com> | |
13 | * Copyright (c) 2004 Dmitry Torokhov <dtor@mail.ru> | |
14 | * Copyright (c) 2005 Vojtech Pavlik <vojtech@suse.cz> | |
15 | * | |
16 | * This program is free software; you can redistribute it and/or modify | |
17 | * it under the terms of the GNU General Public License version 2 as | |
18 | * published by the Free Software Foundation. | |
19 | */ | |
20 | ||
21 | /* | |
22 | * The spec from ALPS is available from | |
23 | * <http://wiki.laptop.org/go/Touch_Pad/Tablet>. It refers to this | |
24 | * device as HGPK (Hybrid GS, PT, and Keymatrix). | |
25 | * | |
26 | * The earliest versions of the device had simultaneous reporting; that | |
27 | * was removed. After that, the device used the Advanced Mode GS/PT streaming | |
28 | * stuff. That turned out to be too buggy to support, so we've finally | |
29 | * switched to Mouse Mode (which utilizes only the center 1/3 of the touchpad). | |
30 | */ | |
31 | ||
32 | #define DEBUG | |
5a0e3ad6 | 33 | #include <linux/slab.h> |
df08ef27 AS |
34 | #include <linux/input.h> |
35 | #include <linux/serio.h> | |
36 | #include <linux/libps2.h> | |
37 | #include <linux/delay.h> | |
38 | #include <asm/olpc.h> | |
39 | ||
40 | #include "psmouse.h" | |
41 | #include "hgpk.h" | |
42 | ||
a62f0d27 DT |
43 | static bool tpdebug; |
44 | module_param(tpdebug, bool, 0644); | |
df08ef27 AS |
45 | MODULE_PARM_DESC(tpdebug, "enable debugging, dumping packets to KERN_DEBUG."); |
46 | ||
47 | static int recalib_delta = 100; | |
48 | module_param(recalib_delta, int, 0644); | |
49 | MODULE_PARM_DESC(recalib_delta, | |
50 | "packets containing a delta this large will cause a recalibration."); | |
51 | ||
8bbf2703 PF |
52 | static int jumpy_delay = 1000; |
53 | module_param(jumpy_delay, int, 0644); | |
54 | MODULE_PARM_DESC(jumpy_delay, | |
55 | "delay (ms) before recal after jumpiness detected"); | |
56 | ||
c0dc8342 | 57 | static int spew_delay = 1; |
8bbf2703 PF |
58 | module_param(spew_delay, int, 0644); |
59 | MODULE_PARM_DESC(spew_delay, | |
60 | "delay (ms) before recal after packet spew detected"); | |
61 | ||
62 | static int recal_guard_time = 2000; | |
63 | module_param(recal_guard_time, int, 0644); | |
64 | MODULE_PARM_DESC(recal_guard_time, | |
65 | "interval (ms) during which recal will be restarted if packet received"); | |
66 | ||
67 | static int post_interrupt_delay = 1000; | |
68 | module_param(post_interrupt_delay, int, 0644); | |
69 | MODULE_PARM_DESC(post_interrupt_delay, | |
70 | "delay (ms) before recal after recal interrupt detected"); | |
71 | ||
ca94ec43 DD |
72 | static char hgpk_mode_name[16]; |
73 | module_param_string(hgpk_mode, hgpk_mode_name, sizeof(hgpk_mode_name), 0644); | |
74 | MODULE_PARM_DESC(hgpk_mode, | |
75 | "default hgpk mode: mouse, glidesensor or pentablet"); | |
76 | ||
77 | static int hgpk_default_mode = HGPK_MODE_MOUSE; | |
78 | ||
79 | static const char * const hgpk_mode_names[] = { | |
80 | [HGPK_MODE_MOUSE] = "Mouse", | |
81 | [HGPK_MODE_GLIDESENSOR] = "GlideSensor", | |
82 | [HGPK_MODE_PENTABLET] = "PenTablet", | |
83 | }; | |
84 | ||
85 | static int hgpk_mode_from_name(const char *buf, int len) | |
86 | { | |
87 | int i; | |
88 | ||
89 | for (i = 0; i < ARRAY_SIZE(hgpk_mode_names); i++) { | |
90 | const char *name = hgpk_mode_names[i]; | |
91 | if (strlen(name) == len && !strncasecmp(name, buf, len)) | |
92 | return i; | |
93 | } | |
94 | ||
95 | return HGPK_MODE_INVALID; | |
96 | } | |
97 | ||
df08ef27 AS |
98 | /* |
99 | * When the touchpad gets ultra-sensitive, one can keep their finger 1/2" | |
100 | * above the pad and still have it send packets. This causes a jump cursor | |
101 | * when one places their finger on the pad. We can probably detect the | |
102 | * jump as we see a large deltas (>= 100px). In mouse mode, I've been | |
103 | * unable to even come close to 100px deltas during normal usage, so I think | |
104 | * this threshold is safe. If a large delta occurs, trigger a recalibration. | |
105 | */ | |
106 | static void hgpk_jumpy_hack(struct psmouse *psmouse, int x, int y) | |
107 | { | |
108 | struct hgpk_data *priv = psmouse->private; | |
109 | ||
110 | if (abs(x) > recalib_delta || abs(y) > recalib_delta) { | |
111 | hgpk_err(psmouse, ">%dpx jump detected (%d,%d)\n", | |
112 | recalib_delta, x, y); | |
113 | /* My car gets forty rods to the hogshead and that's the | |
114 | * way I likes it! */ | |
115 | psmouse_queue_work(psmouse, &priv->recalib_wq, | |
8bbf2703 | 116 | msecs_to_jiffies(jumpy_delay)); |
df08ef27 AS |
117 | } |
118 | } | |
119 | ||
c0dc8342 DD |
120 | static void hgpk_reset_spew_detection(struct hgpk_data *priv) |
121 | { | |
122 | priv->spew_count = 0; | |
123 | priv->dupe_count = 0; | |
124 | priv->x_tally = 0; | |
125 | priv->y_tally = 0; | |
126 | priv->spew_flag = NO_SPEW; | |
127 | } | |
128 | ||
129 | static void hgpk_reset_hack_state(struct psmouse *psmouse) | |
130 | { | |
131 | struct hgpk_data *priv = psmouse->private; | |
132 | ||
133 | priv->abs_x = priv->abs_y = -1; | |
134 | hgpk_reset_spew_detection(priv); | |
135 | } | |
136 | ||
df08ef27 AS |
137 | /* |
138 | * We have no idea why this particular hardware bug occurs. The touchpad | |
139 | * will randomly start spewing packets without anything touching the | |
140 | * pad. This wouldn't necessarily be bad, but it's indicative of a | |
141 | * severely miscalibrated pad; attempting to use the touchpad while it's | |
142 | * spewing means the cursor will jump all over the place, and act "drunk". | |
143 | * | |
144 | * The packets that are spewed tend to all have deltas between -2 and 2, and | |
145 | * the cursor will move around without really going very far. It will | |
146 | * tend to end up in the same location; if we tally up the changes over | |
147 | * 100 packets, we end up w/ a final delta of close to 0. This happens | |
148 | * pretty regularly when the touchpad is spewing, and is pretty hard to | |
149 | * manually trigger (at least for *my* fingers). So, it makes a perfect | |
150 | * scheme for detecting spews. | |
151 | */ | |
152 | static void hgpk_spewing_hack(struct psmouse *psmouse, | |
153 | int l, int r, int x, int y) | |
154 | { | |
155 | struct hgpk_data *priv = psmouse->private; | |
156 | ||
157 | /* ignore button press packets; many in a row could trigger | |
158 | * a false-positive! */ | |
159 | if (l || r) | |
160 | return; | |
161 | ||
c0dc8342 DD |
162 | /* don't track spew if the workaround feature has been turned off */ |
163 | if (!spew_delay) | |
164 | return; | |
165 | ||
166 | if (abs(x) > 3 || abs(y) > 3) { | |
167 | /* no spew, or spew ended */ | |
168 | hgpk_reset_spew_detection(priv); | |
169 | return; | |
170 | } | |
171 | ||
172 | /* Keep a tally of the overall delta to the cursor position caused by | |
173 | * the spew */ | |
df08ef27 AS |
174 | priv->x_tally += x; |
175 | priv->y_tally += y; | |
176 | ||
c0dc8342 DD |
177 | switch (priv->spew_flag) { |
178 | case NO_SPEW: | |
179 | /* we're not spewing, but this packet might be the start */ | |
180 | priv->spew_flag = MAYBE_SPEWING; | |
181 | ||
182 | /* fall-through */ | |
183 | ||
184 | case MAYBE_SPEWING: | |
185 | priv->spew_count++; | |
186 | ||
187 | if (priv->spew_count < SPEW_WATCH_COUNT) | |
188 | break; | |
189 | ||
190 | /* excessive spew detected, request recalibration */ | |
191 | priv->spew_flag = SPEW_DETECTED; | |
192 | ||
193 | /* fall-through */ | |
194 | ||
195 | case SPEW_DETECTED: | |
196 | /* only recalibrate when the overall delta to the cursor | |
197 | * is really small. if the spew is causing significant cursor | |
198 | * movement, it is probably a case of the user moving the | |
199 | * cursor very slowly across the screen. */ | |
df08ef27 | 200 | if (abs(priv->x_tally) < 3 && abs(priv->y_tally) < 3) { |
c0dc8342 | 201 | hgpk_err(psmouse, "packet spew detected (%d,%d)\n", |
df08ef27 | 202 | priv->x_tally, priv->y_tally); |
c0dc8342 | 203 | priv->spew_flag = RECALIBRATING; |
df08ef27 | 204 | psmouse_queue_work(psmouse, &priv->recalib_wq, |
8bbf2703 | 205 | msecs_to_jiffies(spew_delay)); |
df08ef27 | 206 | } |
c0dc8342 DD |
207 | |
208 | break; | |
209 | case RECALIBRATING: | |
210 | /* we already detected a spew and requested a recalibration, | |
211 | * just wait for the queue to kick into action. */ | |
212 | break; | |
df08ef27 AS |
213 | } |
214 | } | |
215 | ||
216 | /* | |
217 | * HGPK Mouse Mode format (standard mouse format, sans middle button) | |
218 | * | |
219 | * byte 0: y-over x-over y-neg x-neg 1 0 swr swl | |
220 | * byte 1: x7 x6 x5 x4 x3 x2 x1 x0 | |
221 | * byte 2: y7 y6 y5 y4 y3 y2 y1 y0 | |
222 | * | |
223 | * swr/swl are the left/right buttons. | |
224 | * x-neg/y-neg are the x and y delta negative bits | |
225 | * x-over/y-over are the x and y overflow bits | |
ca94ec43 DD |
226 | * |
227 | * --- | |
228 | * | |
229 | * HGPK Advanced Mode - single-mode format | |
230 | * | |
231 | * byte 0(PT): 1 1 0 0 1 1 1 1 | |
232 | * byte 0(GS): 1 1 1 1 1 1 1 1 | |
233 | * byte 1: 0 x6 x5 x4 x3 x2 x1 x0 | |
234 | * byte 2(PT): 0 0 x9 x8 x7 ? pt-dsw 0 | |
235 | * byte 2(GS): 0 x10 x9 x8 x7 ? gs-dsw pt-dsw | |
236 | * byte 3: 0 y9 y8 y7 1 0 swr swl | |
237 | * byte 4: 0 y6 y5 y4 y3 y2 y1 y0 | |
238 | * byte 5: 0 z6 z5 z4 z3 z2 z1 z0 | |
239 | * | |
240 | * ?'s are not defined in the protocol spec, may vary between models. | |
241 | * | |
242 | * swr/swl are the left/right buttons. | |
243 | * | |
244 | * pt-dsw/gs-dsw indicate that the pt/gs sensor is detecting a | |
245 | * pen/finger | |
df08ef27 | 246 | */ |
ca94ec43 | 247 | static bool hgpk_is_byte_valid(struct psmouse *psmouse, unsigned char *packet) |
df08ef27 | 248 | { |
ca94ec43 DD |
249 | struct hgpk_data *priv = psmouse->private; |
250 | int pktcnt = psmouse->pktcnt; | |
251 | bool valid; | |
252 | ||
253 | switch (priv->mode) { | |
254 | case HGPK_MODE_MOUSE: | |
255 | valid = (packet[0] & 0x0C) == 0x08; | |
256 | break; | |
257 | ||
258 | case HGPK_MODE_GLIDESENSOR: | |
259 | valid = pktcnt == 1 ? | |
260 | packet[0] == HGPK_GS : !(packet[pktcnt - 1] & 0x80); | |
261 | break; | |
262 | ||
263 | case HGPK_MODE_PENTABLET: | |
264 | valid = pktcnt == 1 ? | |
265 | packet[0] == HGPK_PT : !(packet[pktcnt - 1] & 0x80); | |
266 | break; | |
267 | ||
268 | default: | |
269 | valid = false; | |
270 | break; | |
271 | } | |
272 | ||
273 | if (!valid) | |
274 | hgpk_dbg(psmouse, | |
275 | "bad data, mode %d (%d) %02x %02x %02x %02x %02x %02x\n", | |
276 | priv->mode, pktcnt, | |
277 | psmouse->packet[0], psmouse->packet[1], | |
278 | psmouse->packet[2], psmouse->packet[3], | |
279 | psmouse->packet[4], psmouse->packet[5]); | |
280 | ||
281 | return valid; | |
df08ef27 AS |
282 | } |
283 | ||
ca94ec43 | 284 | static void hgpk_process_advanced_packet(struct psmouse *psmouse) |
df08ef27 | 285 | { |
ca94ec43 DD |
286 | struct hgpk_data *priv = psmouse->private; |
287 | struct input_dev *idev = psmouse->dev; | |
df08ef27 | 288 | unsigned char *packet = psmouse->packet; |
ca94ec43 DD |
289 | int down = !!(packet[2] & 2); |
290 | int left = !!(packet[3] & 1); | |
291 | int right = !!(packet[3] & 2); | |
292 | int x = packet[1] | ((packet[2] & 0x78) << 4); | |
293 | int y = packet[4] | ((packet[3] & 0x70) << 3); | |
294 | ||
295 | if (priv->mode == HGPK_MODE_GLIDESENSOR) { | |
296 | int pt_down = !!(packet[2] & 1); | |
297 | int finger_down = !!(packet[2] & 2); | |
298 | int z = packet[5]; | |
299 | ||
300 | input_report_abs(idev, ABS_PRESSURE, z); | |
301 | if (tpdebug) | |
302 | hgpk_dbg(psmouse, "pd=%d fd=%d z=%d", | |
303 | pt_down, finger_down, z); | |
304 | } else { | |
305 | /* | |
306 | * PenTablet mode does not report pressure, so we don't | |
307 | * report it here | |
308 | */ | |
309 | if (tpdebug) | |
310 | hgpk_dbg(psmouse, "pd=%d ", down); | |
311 | } | |
df08ef27 | 312 | |
ca94ec43 DD |
313 | if (tpdebug) |
314 | hgpk_dbg(psmouse, "l=%d r=%d x=%d y=%d\n", left, right, x, y); | |
df08ef27 | 315 | |
ca94ec43 DD |
316 | input_report_key(idev, BTN_TOUCH, down); |
317 | input_report_key(idev, BTN_LEFT, left); | |
318 | input_report_key(idev, BTN_RIGHT, right); | |
319 | ||
320 | /* | |
321 | * If this packet says that the finger was removed, reset our position | |
322 | * tracking so that we don't erroneously detect a jump on next press. | |
323 | */ | |
c0dc8342 DD |
324 | if (!down) { |
325 | hgpk_reset_hack_state(priv); | |
326 | goto done; | |
327 | } | |
ca94ec43 DD |
328 | |
329 | /* | |
c0dc8342 DD |
330 | * Weed out duplicate packets (we get quite a few, and they mess up |
331 | * our jump detection) | |
ca94ec43 | 332 | */ |
c0dc8342 DD |
333 | if (x == priv->abs_x && y == priv->abs_y) { |
334 | if (++priv->dupe_count > SPEW_WATCH_COUNT) { | |
335 | if (tpdebug) | |
336 | hgpk_dbg(psmouse, "hard spew detected\n"); | |
337 | priv->spew_flag = RECALIBRATING; | |
338 | psmouse_queue_work(psmouse, &priv->recalib_wq, | |
339 | msecs_to_jiffies(spew_delay)); | |
ca94ec43 | 340 | } |
c0dc8342 DD |
341 | goto done; |
342 | } | |
ca94ec43 | 343 | |
c0dc8342 DD |
344 | /* not a duplicate, continue with position reporting */ |
345 | priv->dupe_count = 0; | |
346 | ||
347 | /* Don't apply hacks in PT mode, it seems reliable */ | |
348 | if (priv->mode != HGPK_MODE_PENTABLET && priv->abs_x != -1) { | |
349 | hgpk_jumpy_hack(psmouse, | |
350 | priv->abs_x - x, priv->abs_y - y); | |
351 | hgpk_spewing_hack(psmouse, left, right, | |
352 | priv->abs_x - x, priv->abs_y - y); | |
ca94ec43 DD |
353 | } |
354 | ||
c0dc8342 DD |
355 | input_report_abs(idev, ABS_X, x); |
356 | input_report_abs(idev, ABS_Y, y); | |
357 | priv->abs_x = x; | |
358 | priv->abs_y = y; | |
359 | ||
360 | done: | |
ca94ec43 DD |
361 | input_sync(idev); |
362 | } | |
363 | ||
364 | static void hgpk_process_simple_packet(struct psmouse *psmouse) | |
365 | { | |
366 | struct input_dev *dev = psmouse->dev; | |
367 | unsigned char *packet = psmouse->packet; | |
368 | int left = packet[0] & 1; | |
369 | int right = (packet[0] >> 1) & 1; | |
370 | int x = packet[1] - ((packet[0] << 4) & 0x100); | |
371 | int y = ((packet[0] << 3) & 0x100) - packet[2]; | |
df08ef27 AS |
372 | |
373 | hgpk_jumpy_hack(psmouse, x, y); | |
374 | hgpk_spewing_hack(psmouse, left, right, x, y); | |
375 | ||
376 | if (tpdebug) | |
377 | hgpk_dbg(psmouse, "l=%d r=%d x=%d y=%d\n", left, right, x, y); | |
378 | ||
379 | input_report_key(dev, BTN_LEFT, left); | |
380 | input_report_key(dev, BTN_RIGHT, right); | |
381 | ||
382 | input_report_rel(dev, REL_X, x); | |
383 | input_report_rel(dev, REL_Y, y); | |
384 | ||
385 | input_sync(dev); | |
386 | } | |
387 | ||
388 | static psmouse_ret_t hgpk_process_byte(struct psmouse *psmouse) | |
389 | { | |
390 | struct hgpk_data *priv = psmouse->private; | |
391 | ||
ca94ec43 | 392 | if (!hgpk_is_byte_valid(psmouse, psmouse->packet)) |
df08ef27 | 393 | return PSMOUSE_BAD_DATA; |
df08ef27 AS |
394 | |
395 | if (psmouse->pktcnt >= psmouse->pktsize) { | |
ca94ec43 DD |
396 | if (priv->mode == HGPK_MODE_MOUSE) |
397 | hgpk_process_simple_packet(psmouse); | |
398 | else | |
399 | hgpk_process_advanced_packet(psmouse); | |
df08ef27 AS |
400 | return PSMOUSE_FULL_PACKET; |
401 | } | |
402 | ||
403 | if (priv->recalib_window) { | |
404 | if (time_before(jiffies, priv->recalib_window)) { | |
405 | /* | |
406 | * ugh, got a packet inside our recalibration | |
407 | * window, schedule another recalibration. | |
408 | */ | |
409 | hgpk_dbg(psmouse, | |
410 | "packet inside calibration window, " | |
411 | "queueing another recalibration\n"); | |
412 | psmouse_queue_work(psmouse, &priv->recalib_wq, | |
8bbf2703 | 413 | msecs_to_jiffies(post_interrupt_delay)); |
df08ef27 AS |
414 | } |
415 | priv->recalib_window = 0; | |
416 | } | |
417 | ||
418 | return PSMOUSE_GOOD_DATA; | |
419 | } | |
420 | ||
ca94ec43 DD |
421 | static int hgpk_select_mode(struct psmouse *psmouse) |
422 | { | |
423 | struct ps2dev *ps2dev = &psmouse->ps2dev; | |
424 | struct hgpk_data *priv = psmouse->private; | |
425 | int i; | |
426 | int cmd; | |
427 | ||
428 | /* | |
429 | * 4 disables to enable advanced mode | |
430 | * then 3 0xf2 bytes as the preamble for GS/PT selection | |
431 | */ | |
432 | const int advanced_init[] = { | |
433 | PSMOUSE_CMD_DISABLE, PSMOUSE_CMD_DISABLE, | |
434 | PSMOUSE_CMD_DISABLE, PSMOUSE_CMD_DISABLE, | |
435 | 0xf2, 0xf2, 0xf2, | |
436 | }; | |
437 | ||
438 | switch (priv->mode) { | |
439 | case HGPK_MODE_MOUSE: | |
440 | psmouse->pktsize = 3; | |
441 | break; | |
442 | ||
443 | case HGPK_MODE_GLIDESENSOR: | |
444 | case HGPK_MODE_PENTABLET: | |
445 | psmouse->pktsize = 6; | |
446 | ||
447 | /* Switch to 'Advanced mode.', four disables in a row. */ | |
448 | for (i = 0; i < ARRAY_SIZE(advanced_init); i++) | |
449 | if (ps2_command(ps2dev, NULL, advanced_init[i])) | |
450 | return -EIO; | |
451 | ||
452 | /* select between GlideSensor (mouse) or PenTablet */ | |
453 | cmd = priv->mode == HGPK_MODE_GLIDESENSOR ? | |
454 | PSMOUSE_CMD_SETSCALE11 : PSMOUSE_CMD_SETSCALE21; | |
455 | ||
456 | if (ps2_command(ps2dev, NULL, cmd)) | |
457 | return -EIO; | |
458 | break; | |
459 | ||
460 | default: | |
461 | return -EINVAL; | |
462 | } | |
463 | ||
464 | return 0; | |
465 | } | |
466 | ||
467 | static void hgpk_setup_input_device(struct input_dev *input, | |
468 | struct input_dev *old_input, | |
469 | enum hgpk_mode mode) | |
470 | { | |
471 | if (old_input) { | |
472 | input->name = old_input->name; | |
473 | input->phys = old_input->phys; | |
474 | input->id = old_input->id; | |
475 | input->dev.parent = old_input->dev.parent; | |
476 | } | |
477 | ||
478 | memset(input->evbit, 0, sizeof(input->evbit)); | |
479 | memset(input->relbit, 0, sizeof(input->relbit)); | |
480 | memset(input->keybit, 0, sizeof(input->keybit)); | |
481 | ||
482 | /* All modes report left and right buttons */ | |
483 | __set_bit(EV_KEY, input->evbit); | |
484 | __set_bit(BTN_LEFT, input->keybit); | |
485 | __set_bit(BTN_RIGHT, input->keybit); | |
486 | ||
487 | switch (mode) { | |
488 | case HGPK_MODE_MOUSE: | |
489 | __set_bit(EV_REL, input->evbit); | |
490 | __set_bit(REL_X, input->relbit); | |
491 | __set_bit(REL_Y, input->relbit); | |
492 | break; | |
493 | ||
494 | case HGPK_MODE_GLIDESENSOR: | |
495 | __set_bit(BTN_TOUCH, input->keybit); | |
496 | __set_bit(BTN_TOOL_FINGER, input->keybit); | |
497 | ||
498 | __set_bit(EV_ABS, input->evbit); | |
499 | ||
500 | /* GlideSensor has pressure sensor, PenTablet does not */ | |
501 | input_set_abs_params(input, ABS_PRESSURE, 0, 15, 0, 0); | |
502 | ||
503 | /* From device specs */ | |
504 | input_set_abs_params(input, ABS_X, 0, 399, 0, 0); | |
505 | input_set_abs_params(input, ABS_Y, 0, 290, 0, 0); | |
506 | ||
507 | /* Calculated by hand based on usable size (52mm x 38mm) */ | |
508 | input_abs_set_res(input, ABS_X, 8); | |
509 | input_abs_set_res(input, ABS_Y, 8); | |
510 | break; | |
511 | ||
512 | case HGPK_MODE_PENTABLET: | |
513 | __set_bit(BTN_TOUCH, input->keybit); | |
514 | __set_bit(BTN_TOOL_FINGER, input->keybit); | |
515 | ||
516 | __set_bit(EV_ABS, input->evbit); | |
517 | ||
518 | /* From device specs */ | |
519 | input_set_abs_params(input, ABS_X, 0, 999, 0, 0); | |
520 | input_set_abs_params(input, ABS_Y, 5, 239, 0, 0); | |
521 | ||
522 | /* Calculated by hand based on usable size (156mm x 38mm) */ | |
523 | input_abs_set_res(input, ABS_X, 6); | |
524 | input_abs_set_res(input, ABS_Y, 8); | |
525 | break; | |
526 | ||
527 | default: | |
528 | BUG(); | |
529 | } | |
530 | } | |
531 | ||
ca94ec43 DD |
532 | static int hgpk_reset_device(struct psmouse *psmouse, bool recalibrate) |
533 | { | |
534 | int err; | |
535 | ||
536 | psmouse_reset(psmouse); | |
537 | ||
538 | if (recalibrate) { | |
539 | struct ps2dev *ps2dev = &psmouse->ps2dev; | |
540 | ||
541 | /* send the recalibrate request */ | |
542 | if (ps2_command(ps2dev, NULL, 0xf5) || | |
543 | ps2_command(ps2dev, NULL, 0xf5) || | |
544 | ps2_command(ps2dev, NULL, 0xe6) || | |
545 | ps2_command(ps2dev, NULL, 0xf5)) { | |
546 | return -1; | |
547 | } | |
548 | ||
549 | /* according to ALPS, 150mS is required for recalibration */ | |
550 | msleep(150); | |
551 | } | |
552 | ||
553 | err = hgpk_select_mode(psmouse); | |
554 | if (err) { | |
555 | hgpk_err(psmouse, "failed to select mode\n"); | |
556 | return err; | |
557 | } | |
558 | ||
559 | hgpk_reset_hack_state(psmouse); | |
560 | ||
561 | return 0; | |
562 | } | |
563 | ||
df08ef27 AS |
564 | static int hgpk_force_recalibrate(struct psmouse *psmouse) |
565 | { | |
566 | struct ps2dev *ps2dev = &psmouse->ps2dev; | |
567 | struct hgpk_data *priv = psmouse->private; | |
ca94ec43 | 568 | int err; |
df08ef27 AS |
569 | |
570 | /* C-series touchpads added the recalibrate command */ | |
571 | if (psmouse->model < HGPK_MODEL_C) | |
572 | return 0; | |
573 | ||
574 | /* we don't want to race with the irq handler, nor with resyncs */ | |
575 | psmouse_set_state(psmouse, PSMOUSE_INITIALIZING); | |
576 | ||
577 | /* start by resetting the device */ | |
ca94ec43 DD |
578 | err = hgpk_reset_device(psmouse, true); |
579 | if (err) | |
580 | return err; | |
df08ef27 | 581 | |
ca94ec43 DD |
582 | /* |
583 | * XXX: If a finger is down during this delay, recalibration will | |
df08ef27 AS |
584 | * detect capacitance incorrectly. This is a hardware bug, and |
585 | * we don't have a good way to deal with it. The 2s window stuff | |
586 | * (below) is our best option for now. | |
587 | */ | |
588 | ||
589 | if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) | |
590 | return -1; | |
591 | ||
592 | psmouse_set_state(psmouse, PSMOUSE_ACTIVATED); | |
593 | ||
ca94ec43 DD |
594 | /* |
595 | * After we recalibrate, we shouldn't get any packets for 2s. If | |
df08ef27 AS |
596 | * we do, it's likely that someone's finger was on the touchpad. |
597 | * If someone's finger *was* on the touchpad, it's probably | |
598 | * miscalibrated. So, we should schedule another recalibration | |
599 | */ | |
ca94ec43 | 600 | priv->recalib_window = jiffies + msecs_to_jiffies(recal_guard_time); |
df08ef27 AS |
601 | |
602 | return 0; | |
603 | } | |
604 | ||
605 | /* | |
606 | * This kills power to the touchpad; according to ALPS, current consumption | |
607 | * goes down to 50uA after running this. To turn power back on, we drive | |
608 | * MS-DAT low. | |
609 | */ | |
610 | static int hgpk_toggle_power(struct psmouse *psmouse, int enable) | |
611 | { | |
612 | struct ps2dev *ps2dev = &psmouse->ps2dev; | |
613 | int timeo; | |
ca94ec43 | 614 | int err; |
df08ef27 AS |
615 | |
616 | /* Added on D-series touchpads */ | |
617 | if (psmouse->model < HGPK_MODEL_D) | |
618 | return 0; | |
619 | ||
620 | if (enable) { | |
621 | psmouse_set_state(psmouse, PSMOUSE_INITIALIZING); | |
622 | ||
623 | /* | |
624 | * Sending a byte will drive MS-DAT low; this will wake up | |
625 | * the controller. Once we get an ACK back from it, it | |
626 | * means we can continue with the touchpad re-init. ALPS | |
627 | * tells us that 1s should be long enough, so set that as | |
628 | * the upper bound. | |
629 | */ | |
630 | for (timeo = 20; timeo > 0; timeo--) { | |
631 | if (!ps2_sendbyte(&psmouse->ps2dev, | |
632 | PSMOUSE_CMD_DISABLE, 20)) | |
633 | break; | |
634 | msleep(50); | |
635 | } | |
636 | ||
ca94ec43 DD |
637 | err = hgpk_reset_device(psmouse, false); |
638 | if (err) { | |
639 | hgpk_err(psmouse, "Failed to reset device!\n"); | |
640 | return err; | |
641 | } | |
df08ef27 AS |
642 | |
643 | /* should be all set, enable the touchpad */ | |
644 | ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE); | |
645 | psmouse_set_state(psmouse, PSMOUSE_ACTIVATED); | |
646 | ||
647 | } else { | |
648 | hgpk_dbg(psmouse, "Powering off touchpad.\n"); | |
649 | psmouse_set_state(psmouse, PSMOUSE_IGNORE); | |
650 | ||
651 | if (ps2_command(ps2dev, NULL, 0xec) || | |
652 | ps2_command(ps2dev, NULL, 0xec) || | |
653 | ps2_command(ps2dev, NULL, 0xea)) { | |
654 | return -1; | |
655 | } | |
656 | ||
657 | /* probably won't see an ACK, the touchpad will be off */ | |
658 | ps2_sendbyte(&psmouse->ps2dev, 0xec, 20); | |
659 | } | |
660 | ||
661 | return 0; | |
662 | } | |
663 | ||
664 | static int hgpk_poll(struct psmouse *psmouse) | |
665 | { | |
666 | /* We can't poll, so always return failure. */ | |
667 | return -1; | |
668 | } | |
669 | ||
670 | static int hgpk_reconnect(struct psmouse *psmouse) | |
671 | { | |
ca94ec43 DD |
672 | /* |
673 | * During suspend/resume the ps2 rails remain powered. We don't want | |
df08ef27 | 674 | * to do a reset because it's flush data out of buffers; however, |
ca94ec43 DD |
675 | * earlier prototypes (B1) had some brokenness that required a reset. |
676 | */ | |
df08ef27 AS |
677 | if (olpc_board_at_least(olpc_board(0xb2))) |
678 | if (psmouse->ps2dev.serio->dev.power.power_state.event != | |
679 | PM_EVENT_ON) | |
680 | return 0; | |
681 | ||
ca94ec43 | 682 | return hgpk_reset_device(psmouse, false); |
df08ef27 AS |
683 | } |
684 | ||
685 | static ssize_t hgpk_show_powered(struct psmouse *psmouse, void *data, char *buf) | |
686 | { | |
687 | struct hgpk_data *priv = psmouse->private; | |
688 | ||
689 | return sprintf(buf, "%d\n", priv->powered); | |
690 | } | |
691 | ||
692 | static ssize_t hgpk_set_powered(struct psmouse *psmouse, void *data, | |
693 | const char *buf, size_t count) | |
694 | { | |
695 | struct hgpk_data *priv = psmouse->private; | |
696 | unsigned long value; | |
697 | int err; | |
698 | ||
699 | err = strict_strtoul(buf, 10, &value); | |
700 | if (err || value > 1) | |
701 | return -EINVAL; | |
702 | ||
703 | if (value != priv->powered) { | |
704 | /* | |
705 | * hgpk_toggle_power will deal w/ state so | |
706 | * we're not racing w/ irq | |
707 | */ | |
708 | err = hgpk_toggle_power(psmouse, value); | |
709 | if (!err) | |
710 | priv->powered = value; | |
711 | } | |
712 | ||
713 | return err ? err : count; | |
714 | } | |
715 | ||
716 | __PSMOUSE_DEFINE_ATTR(powered, S_IWUSR | S_IRUGO, NULL, | |
b7802c5c | 717 | hgpk_show_powered, hgpk_set_powered, false); |
df08ef27 | 718 | |
ca94ec43 DD |
719 | static ssize_t attr_show_mode(struct psmouse *psmouse, void *data, char *buf) |
720 | { | |
721 | struct hgpk_data *priv = psmouse->private; | |
722 | ||
723 | return sprintf(buf, "%s\n", hgpk_mode_names[priv->mode]); | |
724 | } | |
725 | ||
726 | static ssize_t attr_set_mode(struct psmouse *psmouse, void *data, | |
727 | const char *buf, size_t len) | |
728 | { | |
729 | struct hgpk_data *priv = psmouse->private; | |
730 | enum hgpk_mode old_mode = priv->mode; | |
731 | enum hgpk_mode new_mode = hgpk_mode_from_name(buf, len); | |
732 | struct input_dev *old_dev = psmouse->dev; | |
733 | struct input_dev *new_dev; | |
734 | int err; | |
735 | ||
736 | if (new_mode == HGPK_MODE_INVALID) | |
737 | return -EINVAL; | |
738 | ||
739 | if (old_mode == new_mode) | |
740 | return len; | |
741 | ||
742 | new_dev = input_allocate_device(); | |
743 | if (!new_dev) | |
744 | return -ENOMEM; | |
745 | ||
746 | psmouse_set_state(psmouse, PSMOUSE_INITIALIZING); | |
747 | ||
748 | /* Switch device into the new mode */ | |
749 | priv->mode = new_mode; | |
750 | err = hgpk_reset_device(psmouse, false); | |
751 | if (err) | |
752 | goto err_try_restore; | |
753 | ||
754 | hgpk_setup_input_device(new_dev, old_dev, new_mode); | |
755 | ||
756 | psmouse_set_state(psmouse, PSMOUSE_CMD_MODE); | |
757 | ||
758 | err = input_register_device(new_dev); | |
759 | if (err) | |
760 | goto err_try_restore; | |
761 | ||
762 | psmouse->dev = new_dev; | |
763 | input_unregister_device(old_dev); | |
764 | ||
765 | return len; | |
766 | ||
767 | err_try_restore: | |
768 | input_free_device(new_dev); | |
769 | priv->mode = old_mode; | |
770 | hgpk_reset_device(psmouse, false); | |
771 | ||
772 | return err; | |
773 | } | |
774 | ||
775 | PSMOUSE_DEFINE_ATTR(hgpk_mode, S_IWUSR | S_IRUGO, NULL, | |
776 | attr_show_mode, attr_set_mode); | |
777 | ||
c46dd1eb PF |
778 | static ssize_t hgpk_trigger_recal_show(struct psmouse *psmouse, |
779 | void *data, char *buf) | |
780 | { | |
781 | return -EINVAL; | |
782 | } | |
783 | ||
784 | static ssize_t hgpk_trigger_recal(struct psmouse *psmouse, void *data, | |
785 | const char *buf, size_t count) | |
786 | { | |
787 | struct hgpk_data *priv = psmouse->private; | |
788 | unsigned long value; | |
789 | int err; | |
790 | ||
791 | err = strict_strtoul(buf, 10, &value); | |
792 | if (err || value != 1) | |
793 | return -EINVAL; | |
794 | ||
795 | /* | |
796 | * We queue work instead of doing recalibration right here | |
797 | * to avoid adding locking to to hgpk_force_recalibrate() | |
798 | * since workqueue provides serialization. | |
799 | */ | |
800 | psmouse_queue_work(psmouse, &priv->recalib_wq, 0); | |
801 | return count; | |
802 | } | |
803 | ||
804 | __PSMOUSE_DEFINE_ATTR(recalibrate, S_IWUSR | S_IRUGO, NULL, | |
b7802c5c | 805 | hgpk_trigger_recal_show, hgpk_trigger_recal, false); |
c46dd1eb | 806 | |
df08ef27 AS |
807 | static void hgpk_disconnect(struct psmouse *psmouse) |
808 | { | |
809 | struct hgpk_data *priv = psmouse->private; | |
810 | ||
811 | device_remove_file(&psmouse->ps2dev.serio->dev, | |
812 | &psmouse_attr_powered.dattr); | |
ca94ec43 DD |
813 | device_remove_file(&psmouse->ps2dev.serio->dev, |
814 | &psmouse_attr_hgpk_mode.dattr); | |
c46dd1eb PF |
815 | |
816 | if (psmouse->model >= HGPK_MODEL_C) | |
817 | device_remove_file(&psmouse->ps2dev.serio->dev, | |
818 | &psmouse_attr_recalibrate.dattr); | |
819 | ||
df08ef27 AS |
820 | psmouse_reset(psmouse); |
821 | kfree(priv); | |
822 | } | |
823 | ||
824 | static void hgpk_recalib_work(struct work_struct *work) | |
825 | { | |
bf6aede7 | 826 | struct delayed_work *w = to_delayed_work(work); |
df08ef27 AS |
827 | struct hgpk_data *priv = container_of(w, struct hgpk_data, recalib_wq); |
828 | struct psmouse *psmouse = priv->psmouse; | |
829 | ||
830 | hgpk_dbg(psmouse, "recalibrating touchpad..\n"); | |
831 | ||
832 | if (hgpk_force_recalibrate(psmouse)) | |
833 | hgpk_err(psmouse, "recalibration failed!\n"); | |
834 | } | |
835 | ||
836 | static int hgpk_register(struct psmouse *psmouse) | |
837 | { | |
ca94ec43 | 838 | struct hgpk_data *priv = psmouse->private; |
df08ef27 AS |
839 | int err; |
840 | ||
df08ef27 AS |
841 | /* register handlers */ |
842 | psmouse->protocol_handler = hgpk_process_byte; | |
843 | psmouse->poll = hgpk_poll; | |
844 | psmouse->disconnect = hgpk_disconnect; | |
845 | psmouse->reconnect = hgpk_reconnect; | |
df08ef27 AS |
846 | |
847 | /* Disable the idle resync. */ | |
848 | psmouse->resync_time = 0; | |
849 | /* Reset after a lot of bad bytes. */ | |
850 | psmouse->resetafter = 1024; | |
851 | ||
ca94ec43 DD |
852 | hgpk_setup_input_device(psmouse->dev, NULL, priv->mode); |
853 | ||
df08ef27 AS |
854 | err = device_create_file(&psmouse->ps2dev.serio->dev, |
855 | &psmouse_attr_powered.dattr); | |
c46dd1eb PF |
856 | if (err) { |
857 | hgpk_err(psmouse, "Failed creating 'powered' sysfs node\n"); | |
858 | return err; | |
859 | } | |
860 | ||
ca94ec43 DD |
861 | err = device_create_file(&psmouse->ps2dev.serio->dev, |
862 | &psmouse_attr_hgpk_mode.dattr); | |
863 | if (err) { | |
864 | hgpk_err(psmouse, "Failed creating 'hgpk_mode' sysfs node\n"); | |
865 | goto err_remove_powered; | |
866 | } | |
867 | ||
c46dd1eb PF |
868 | /* C-series touchpads added the recalibrate command */ |
869 | if (psmouse->model >= HGPK_MODEL_C) { | |
870 | err = device_create_file(&psmouse->ps2dev.serio->dev, | |
871 | &psmouse_attr_recalibrate.dattr); | |
872 | if (err) { | |
873 | hgpk_err(psmouse, | |
874 | "Failed creating 'recalibrate' sysfs node\n"); | |
ca94ec43 | 875 | goto err_remove_mode; |
c46dd1eb PF |
876 | } |
877 | } | |
df08ef27 | 878 | |
c46dd1eb | 879 | return 0; |
ca94ec43 DD |
880 | |
881 | err_remove_mode: | |
882 | device_remove_file(&psmouse->ps2dev.serio->dev, | |
883 | &psmouse_attr_hgpk_mode.dattr); | |
884 | err_remove_powered: | |
885 | device_remove_file(&psmouse->ps2dev.serio->dev, | |
886 | &psmouse_attr_powered.dattr); | |
887 | return err; | |
df08ef27 AS |
888 | } |
889 | ||
890 | int hgpk_init(struct psmouse *psmouse) | |
891 | { | |
892 | struct hgpk_data *priv; | |
ca94ec43 | 893 | int err; |
df08ef27 AS |
894 | |
895 | priv = kzalloc(sizeof(struct hgpk_data), GFP_KERNEL); | |
ca94ec43 DD |
896 | if (!priv) { |
897 | err = -ENOMEM; | |
df08ef27 | 898 | goto alloc_fail; |
ca94ec43 | 899 | } |
df08ef27 AS |
900 | |
901 | psmouse->private = priv; | |
ca94ec43 | 902 | |
df08ef27 | 903 | priv->psmouse = psmouse; |
b7802c5c | 904 | priv->powered = true; |
ca94ec43 | 905 | priv->mode = hgpk_default_mode; |
df08ef27 AS |
906 | INIT_DELAYED_WORK(&priv->recalib_wq, hgpk_recalib_work); |
907 | ||
ca94ec43 | 908 | err = hgpk_reset_device(psmouse, false); |
df08ef27 AS |
909 | if (err) |
910 | goto init_fail; | |
911 | ||
912 | err = hgpk_register(psmouse); | |
913 | if (err) | |
914 | goto init_fail; | |
915 | ||
916 | return 0; | |
917 | ||
918 | init_fail: | |
919 | kfree(priv); | |
920 | alloc_fail: | |
921 | return err; | |
922 | } | |
923 | ||
924 | static enum hgpk_model_t hgpk_get_model(struct psmouse *psmouse) | |
925 | { | |
926 | struct ps2dev *ps2dev = &psmouse->ps2dev; | |
927 | unsigned char param[3]; | |
928 | ||
929 | /* E7, E7, E7, E9 gets us a 3 byte identifier */ | |
930 | if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) || | |
931 | ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) || | |
932 | ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) || | |
933 | ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) { | |
934 | return -EIO; | |
935 | } | |
936 | ||
0f495481 | 937 | hgpk_dbg(psmouse, "ID: %02x %02x %02x\n", param[0], param[1], param[2]); |
df08ef27 AS |
938 | |
939 | /* HGPK signature: 0x67, 0x00, 0x<model> */ | |
940 | if (param[0] != 0x67 || param[1] != 0x00) | |
941 | return -ENODEV; | |
942 | ||
943 | hgpk_info(psmouse, "OLPC touchpad revision 0x%x\n", param[2]); | |
944 | ||
945 | return param[2]; | |
946 | } | |
947 | ||
b7802c5c | 948 | int hgpk_detect(struct psmouse *psmouse, bool set_properties) |
df08ef27 AS |
949 | { |
950 | int version; | |
951 | ||
952 | version = hgpk_get_model(psmouse); | |
953 | if (version < 0) | |
954 | return version; | |
955 | ||
956 | if (set_properties) { | |
957 | psmouse->vendor = "ALPS"; | |
958 | psmouse->name = "HGPK"; | |
959 | psmouse->model = version; | |
960 | } | |
961 | ||
962 | return 0; | |
963 | } | |
ca94ec43 DD |
964 | |
965 | void hgpk_module_init(void) | |
966 | { | |
967 | hgpk_default_mode = hgpk_mode_from_name(hgpk_mode_name, | |
968 | strlen(hgpk_mode_name)); | |
969 | if (hgpk_default_mode == HGPK_MODE_INVALID) { | |
970 | hgpk_default_mode = HGPK_MODE_MOUSE; | |
971 | strlcpy(hgpk_mode_name, hgpk_mode_names[HGPK_MODE_MOUSE], | |
972 | sizeof(hgpk_mode_name)); | |
973 | } | |
974 | } |