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