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