xref: /openbmc/linux/drivers/input/mouse/hgpk.c (revision 8fa5723aa7e053d498336b48448b292fc2e0458b)
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
33 #include <linux/input.h>
34 #include <linux/serio.h>
35 #include <linux/libps2.h>
36 #include <linux/delay.h>
37 #include <asm/olpc.h>
38 
39 #include "psmouse.h"
40 #include "hgpk.h"
41 
42 static int tpdebug;
43 module_param(tpdebug, int, 0644);
44 MODULE_PARM_DESC(tpdebug, "enable debugging, dumping packets to KERN_DEBUG.");
45 
46 static int recalib_delta = 100;
47 module_param(recalib_delta, int, 0644);
48 MODULE_PARM_DESC(recalib_delta,
49 	"packets containing a delta this large will cause a recalibration.");
50 
51 /*
52  * When the touchpad gets ultra-sensitive, one can keep their finger 1/2"
53  * above the pad and still have it send packets.  This causes a jump cursor
54  * when one places their finger on the pad.  We can probably detect the
55  * jump as we see a large deltas (>= 100px).  In mouse mode, I've been
56  * unable to even come close to 100px deltas during normal usage, so I think
57  * this threshold is safe.  If a large delta occurs, trigger a recalibration.
58  */
59 static void hgpk_jumpy_hack(struct psmouse *psmouse, int x, int y)
60 {
61 	struct hgpk_data *priv = psmouse->private;
62 
63 	if (abs(x) > recalib_delta || abs(y) > recalib_delta) {
64 		hgpk_err(psmouse, ">%dpx jump detected (%d,%d)\n",
65 				recalib_delta, x, y);
66 		/* My car gets forty rods to the hogshead and that's the
67 		 * way I likes it! */
68 		psmouse_queue_work(psmouse, &priv->recalib_wq,
69 				msecs_to_jiffies(1000));
70 	}
71 }
72 
73 /*
74  * We have no idea why this particular hardware bug occurs.  The touchpad
75  * will randomly start spewing packets without anything touching the
76  * pad.  This wouldn't necessarily be bad, but it's indicative of a
77  * severely miscalibrated pad; attempting to use the touchpad while it's
78  * spewing means the cursor will jump all over the place, and act "drunk".
79  *
80  * The packets that are spewed tend to all have deltas between -2 and 2, and
81  * the cursor will move around without really going very far.  It will
82  * tend to end up in the same location; if we tally up the changes over
83  * 100 packets, we end up w/ a final delta of close to 0.  This happens
84  * pretty regularly when the touchpad is spewing, and is pretty hard to
85  * manually trigger (at least for *my* fingers).  So, it makes a perfect
86  * scheme for detecting spews.
87  */
88 static void hgpk_spewing_hack(struct psmouse *psmouse,
89 			      int l, int r, int x, int y)
90 {
91 	struct hgpk_data *priv = psmouse->private;
92 
93 	/* ignore button press packets; many in a row could trigger
94 	 * a false-positive! */
95 	if (l || r)
96 		return;
97 
98 	priv->x_tally += x;
99 	priv->y_tally += y;
100 
101 	if (++priv->count > 100) {
102 		if (abs(priv->x_tally) < 3 && abs(priv->y_tally) < 3) {
103 			hgpk_dbg(psmouse, "packet spew detected (%d,%d)\n",
104 				 priv->x_tally, priv->y_tally);
105 			psmouse_queue_work(psmouse, &priv->recalib_wq,
106 					   msecs_to_jiffies(1000));
107 		}
108 		/* reset every 100 packets */
109 		priv->count = 0;
110 		priv->x_tally = 0;
111 		priv->y_tally = 0;
112 	}
113 }
114 
115 /*
116  * HGPK Mouse Mode format (standard mouse format, sans middle button)
117  *
118  * byte 0:	y-over	x-over	y-neg	x-neg	1	0	swr	swl
119  * byte 1:	x7	x6	x5	x4	x3	x2	x1	x0
120  * byte 2:	y7	y6	y5	y4	y3	y2	y1	y0
121  *
122  * swr/swl are the left/right buttons.
123  * x-neg/y-neg are the x and y delta negative bits
124  * x-over/y-over are the x and y overflow bits
125  */
126 static int hgpk_validate_byte(unsigned char *packet)
127 {
128 	return (packet[0] & 0x0C) == 0x08;
129 }
130 
131 static void hgpk_process_packet(struct psmouse *psmouse)
132 {
133 	struct input_dev *dev = psmouse->dev;
134 	unsigned char *packet = psmouse->packet;
135 	int x, y, left, right;
136 
137 	left = packet[0] & 1;
138 	right = (packet[0] >> 1) & 1;
139 
140 	x = packet[1] - ((packet[0] << 4) & 0x100);
141 	y = ((packet[0] << 3) & 0x100) - packet[2];
142 
143 	hgpk_jumpy_hack(psmouse, x, y);
144 	hgpk_spewing_hack(psmouse, left, right, x, y);
145 
146 	if (tpdebug)
147 		hgpk_dbg(psmouse, "l=%d r=%d x=%d y=%d\n", left, right, x, y);
148 
149 	input_report_key(dev, BTN_LEFT, left);
150 	input_report_key(dev, BTN_RIGHT, right);
151 
152 	input_report_rel(dev, REL_X, x);
153 	input_report_rel(dev, REL_Y, y);
154 
155 	input_sync(dev);
156 }
157 
158 static psmouse_ret_t hgpk_process_byte(struct psmouse *psmouse)
159 {
160 	struct hgpk_data *priv = psmouse->private;
161 
162 	if (hgpk_validate_byte(psmouse->packet)) {
163 		hgpk_dbg(psmouse, "%s: (%d) %02x %02x %02x\n",
164 				__func__, psmouse->pktcnt, psmouse->packet[0],
165 				psmouse->packet[1], psmouse->packet[2]);
166 		return PSMOUSE_BAD_DATA;
167 	}
168 
169 	if (psmouse->pktcnt >= psmouse->pktsize) {
170 		hgpk_process_packet(psmouse);
171 		return PSMOUSE_FULL_PACKET;
172 	}
173 
174 	if (priv->recalib_window) {
175 		if (time_before(jiffies, priv->recalib_window)) {
176 			/*
177 			 * ugh, got a packet inside our recalibration
178 			 * window, schedule another recalibration.
179 			 */
180 			hgpk_dbg(psmouse,
181 				 "packet inside calibration window, "
182 				 "queueing another recalibration\n");
183 			psmouse_queue_work(psmouse, &priv->recalib_wq,
184 					msecs_to_jiffies(1000));
185 		}
186 		priv->recalib_window = 0;
187 	}
188 
189 	return PSMOUSE_GOOD_DATA;
190 }
191 
192 static int hgpk_force_recalibrate(struct psmouse *psmouse)
193 {
194 	struct ps2dev *ps2dev = &psmouse->ps2dev;
195 	struct hgpk_data *priv = psmouse->private;
196 
197 	/* C-series touchpads added the recalibrate command */
198 	if (psmouse->model < HGPK_MODEL_C)
199 		return 0;
200 
201 	/* we don't want to race with the irq handler, nor with resyncs */
202 	psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
203 
204 	/* start by resetting the device */
205 	psmouse_reset(psmouse);
206 
207 	/* send the recalibrate request */
208 	if (ps2_command(ps2dev, NULL, 0xf5) ||
209 	    ps2_command(ps2dev, NULL, 0xf5) ||
210 	    ps2_command(ps2dev, NULL, 0xe6) ||
211 	    ps2_command(ps2dev, NULL, 0xf5)) {
212 		return -1;
213 	}
214 
215 	/* according to ALPS, 150mS is required for recalibration */
216 	msleep(150);
217 
218 	/* XXX: If a finger is down during this delay, recalibration will
219 	 * detect capacitance incorrectly.  This is a hardware bug, and
220 	 * we don't have a good way to deal with it.  The 2s window stuff
221 	 * (below) is our best option for now.
222 	 */
223 
224 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
225 		return -1;
226 
227 	psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
228 
229 	/* After we recalibrate, we shouldn't get any packets for 2s.  If
230 	 * we do, it's likely that someone's finger was on the touchpad.
231 	 * If someone's finger *was* on the touchpad, it's probably
232 	 * miscalibrated.  So, we should schedule another recalibration
233 	 */
234 	priv->recalib_window = jiffies +  msecs_to_jiffies(2000);
235 
236 	return 0;
237 }
238 
239 /*
240  * This kills power to the touchpad; according to ALPS, current consumption
241  * goes down to 50uA after running this.  To turn power back on, we drive
242  * MS-DAT low.
243  */
244 static int hgpk_toggle_power(struct psmouse *psmouse, int enable)
245 {
246 	struct ps2dev *ps2dev = &psmouse->ps2dev;
247 	int timeo;
248 
249 	/* Added on D-series touchpads */
250 	if (psmouse->model < HGPK_MODEL_D)
251 		return 0;
252 
253 	if (enable) {
254 		psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
255 
256 		/*
257 		 * Sending a byte will drive MS-DAT low; this will wake up
258 		 * the controller.  Once we get an ACK back from it, it
259 		 * means we can continue with the touchpad re-init.  ALPS
260 		 * tells us that 1s should be long enough, so set that as
261 		 * the upper bound.
262 		 */
263 		for (timeo = 20; timeo > 0; timeo--) {
264 			if (!ps2_sendbyte(&psmouse->ps2dev,
265 					PSMOUSE_CMD_DISABLE, 20))
266 				break;
267 			msleep(50);
268 		}
269 
270 		psmouse_reset(psmouse);
271 
272 		/* should be all set, enable the touchpad */
273 		ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE);
274 		psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
275 
276 	} else {
277 		hgpk_dbg(psmouse, "Powering off touchpad.\n");
278 		psmouse_set_state(psmouse, PSMOUSE_IGNORE);
279 
280 		if (ps2_command(ps2dev, NULL, 0xec) ||
281 		    ps2_command(ps2dev, NULL, 0xec) ||
282 		    ps2_command(ps2dev, NULL, 0xea)) {
283 			return -1;
284 		}
285 
286 		/* probably won't see an ACK, the touchpad will be off */
287 		ps2_sendbyte(&psmouse->ps2dev, 0xec, 20);
288 	}
289 
290 	return 0;
291 }
292 
293 static int hgpk_poll(struct psmouse *psmouse)
294 {
295 	/* We can't poll, so always return failure. */
296 	return -1;
297 }
298 
299 static int hgpk_reconnect(struct psmouse *psmouse)
300 {
301 	/* During suspend/resume the ps2 rails remain powered.  We don't want
302 	 * to do a reset because it's flush data out of buffers; however,
303 	 * earlier prototypes (B1) had some brokenness that required a reset. */
304 	if (olpc_board_at_least(olpc_board(0xb2)))
305 		if (psmouse->ps2dev.serio->dev.power.power_state.event !=
306 				PM_EVENT_ON)
307 			return 0;
308 
309 	psmouse_reset(psmouse);
310 
311 	return 0;
312 }
313 
314 static ssize_t hgpk_show_powered(struct psmouse *psmouse, void *data, char *buf)
315 {
316 	struct hgpk_data *priv = psmouse->private;
317 
318 	return sprintf(buf, "%d\n", priv->powered);
319 }
320 
321 static ssize_t hgpk_set_powered(struct psmouse *psmouse, void *data,
322 				const char *buf, size_t count)
323 {
324 	struct hgpk_data *priv = psmouse->private;
325 	unsigned long value;
326 	int err;
327 
328 	err = strict_strtoul(buf, 10, &value);
329 	if (err || value > 1)
330 		return -EINVAL;
331 
332 	if (value != priv->powered) {
333 		/*
334 		 * hgpk_toggle_power will deal w/ state so
335 		 * we're not racing w/ irq
336 		 */
337 		err = hgpk_toggle_power(psmouse, value);
338 		if (!err)
339 			priv->powered = value;
340 	}
341 
342 	return err ? err : count;
343 }
344 
345 __PSMOUSE_DEFINE_ATTR(powered, S_IWUSR | S_IRUGO, NULL,
346 		      hgpk_show_powered, hgpk_set_powered, 0);
347 
348 static void hgpk_disconnect(struct psmouse *psmouse)
349 {
350 	struct hgpk_data *priv = psmouse->private;
351 
352 	device_remove_file(&psmouse->ps2dev.serio->dev,
353 			   &psmouse_attr_powered.dattr);
354 	psmouse_reset(psmouse);
355 	kfree(priv);
356 }
357 
358 static void hgpk_recalib_work(struct work_struct *work)
359 {
360 	struct delayed_work *w = container_of(work, struct delayed_work, work);
361 	struct hgpk_data *priv = container_of(w, struct hgpk_data, recalib_wq);
362 	struct psmouse *psmouse = priv->psmouse;
363 
364 	hgpk_dbg(psmouse, "recalibrating touchpad..\n");
365 
366 	if (hgpk_force_recalibrate(psmouse))
367 		hgpk_err(psmouse, "recalibration failed!\n");
368 }
369 
370 static int hgpk_register(struct psmouse *psmouse)
371 {
372 	struct input_dev *dev = psmouse->dev;
373 	int err;
374 
375 	/* unset the things that psmouse-base sets which we don't have */
376 	__clear_bit(BTN_MIDDLE, dev->keybit);
377 
378 	/* set the things we do have */
379 	__set_bit(EV_KEY, dev->evbit);
380 	__set_bit(EV_REL, dev->evbit);
381 
382 	__set_bit(REL_X, dev->relbit);
383 	__set_bit(REL_Y, dev->relbit);
384 
385 	__set_bit(BTN_LEFT, dev->keybit);
386 	__set_bit(BTN_RIGHT, dev->keybit);
387 
388 	/* register handlers */
389 	psmouse->protocol_handler = hgpk_process_byte;
390 	psmouse->poll = hgpk_poll;
391 	psmouse->disconnect = hgpk_disconnect;
392 	psmouse->reconnect = hgpk_reconnect;
393 	psmouse->pktsize = 3;
394 
395 	/* Disable the idle resync. */
396 	psmouse->resync_time = 0;
397 	/* Reset after a lot of bad bytes. */
398 	psmouse->resetafter = 1024;
399 
400 	err = device_create_file(&psmouse->ps2dev.serio->dev,
401 				 &psmouse_attr_powered.dattr);
402 	if (err)
403 		hgpk_err(psmouse, "Failed to create sysfs attribute\n");
404 
405 	return err;
406 }
407 
408 int hgpk_init(struct psmouse *psmouse)
409 {
410 	struct hgpk_data *priv;
411 	int err = -ENOMEM;
412 
413 	priv = kzalloc(sizeof(struct hgpk_data), GFP_KERNEL);
414 	if (!priv)
415 		goto alloc_fail;
416 
417 	psmouse->private = priv;
418 	priv->psmouse = psmouse;
419 	priv->powered = 1;
420 	INIT_DELAYED_WORK(&priv->recalib_wq, hgpk_recalib_work);
421 
422 	err = psmouse_reset(psmouse);
423 	if (err)
424 		goto init_fail;
425 
426 	err = hgpk_register(psmouse);
427 	if (err)
428 		goto init_fail;
429 
430 	return 0;
431 
432 init_fail:
433 	kfree(priv);
434 alloc_fail:
435 	return err;
436 }
437 
438 static enum hgpk_model_t hgpk_get_model(struct psmouse *psmouse)
439 {
440 	struct ps2dev *ps2dev = &psmouse->ps2dev;
441 	unsigned char param[3];
442 
443 	/* E7, E7, E7, E9 gets us a 3 byte identifier */
444 	if (ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE21) ||
445 	    ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE21) ||
446 	    ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE21) ||
447 	    ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) {
448 		return -EIO;
449 	}
450 
451 	hgpk_dbg(psmouse, "ID: %02x %02x %02x", param[0], param[1], param[2]);
452 
453 	/* HGPK signature: 0x67, 0x00, 0x<model> */
454 	if (param[0] != 0x67 || param[1] != 0x00)
455 		return -ENODEV;
456 
457 	hgpk_info(psmouse, "OLPC touchpad revision 0x%x\n", param[2]);
458 
459 	return param[2];
460 }
461 
462 int hgpk_detect(struct psmouse *psmouse, int set_properties)
463 {
464 	int version;
465 
466 	version = hgpk_get_model(psmouse);
467 	if (version < 0)
468 		return version;
469 
470 	if (set_properties) {
471 		psmouse->vendor = "ALPS";
472 		psmouse->name = "HGPK";
473 		psmouse->model = version;
474 	}
475 
476 	return 0;
477 }
478