xref: /linux/drivers/input/mouse/alps.c (revision a33f32244d8550da8b4a26e277ce07d5c6d158b5)
1 /*
2  * ALPS touchpad PS/2 mouse driver
3  *
4  * Copyright (c) 2003 Neil Brown <neilb@cse.unsw.edu.au>
5  * Copyright (c) 2003-2005 Peter Osterlund <petero2@telia.com>
6  * Copyright (c) 2004 Dmitry Torokhov <dtor@mail.ru>
7  * Copyright (c) 2005 Vojtech Pavlik <vojtech@suse.cz>
8  * Copyright (c) 2009 Sebastian Kapfer <sebastian_kapfer@gmx.net>
9  *
10  * ALPS detection, tap switching and status querying info is taken from
11  * tpconfig utility (by C. Scott Ananian and Bruce Kall).
12  *
13  * This program is free software; you can redistribute it and/or modify it
14  * under the terms of the GNU General Public License version 2 as published by
15  * the Free Software Foundation.
16  */
17 
18 #include <linux/slab.h>
19 #include <linux/input.h>
20 #include <linux/serio.h>
21 #include <linux/libps2.h>
22 
23 #include "psmouse.h"
24 #include "alps.h"
25 
26 #undef DEBUG
27 #ifdef DEBUG
28 #define dbg(format, arg...) printk(KERN_INFO "alps.c: " format "\n", ## arg)
29 #else
30 #define dbg(format, arg...) do {} while (0)
31 #endif
32 
33 #define ALPS_OLDPROTO		0x01	/* old style input */
34 #define ALPS_DUALPOINT		0x02	/* touchpad has trackstick */
35 #define ALPS_PASS		0x04	/* device has a pass-through port */
36 
37 #define ALPS_WHEEL		0x08	/* hardware wheel present */
38 #define ALPS_FW_BK_1		0x10	/* front & back buttons present */
39 #define ALPS_FW_BK_2		0x20	/* front & back buttons present */
40 #define ALPS_FOUR_BUTTONS	0x40	/* 4 direction button present */
41 #define ALPS_PS2_INTERLEAVED	0x80	/* 3-byte PS/2 packet interleaved with
42 					   6-byte ALPS packet */
43 
44 static const struct alps_model_info alps_model_data[] = {
45 	{ { 0x32, 0x02, 0x14 },	0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* Toshiba Salellite Pro M10 */
46 	{ { 0x33, 0x02, 0x0a },	0x88, 0xf8, ALPS_OLDPROTO },		  /* UMAX-530T */
47 	{ { 0x53, 0x02, 0x0a },	0xf8, 0xf8, 0 },
48 	{ { 0x53, 0x02, 0x14 },	0xf8, 0xf8, 0 },
49 	{ { 0x60, 0x03, 0xc8 }, 0xf8, 0xf8, 0 },			  /* HP ze1115 */
50 	{ { 0x63, 0x02, 0x0a },	0xf8, 0xf8, 0 },
51 	{ { 0x63, 0x02, 0x14 },	0xf8, 0xf8, 0 },
52 	{ { 0x63, 0x02, 0x28 },	0xf8, 0xf8, ALPS_FW_BK_2 },		  /* Fujitsu Siemens S6010 */
53 	{ { 0x63, 0x02, 0x3c },	0x8f, 0x8f, ALPS_WHEEL },		  /* Toshiba Satellite S2400-103 */
54 	{ { 0x63, 0x02, 0x50 },	0xef, 0xef, ALPS_FW_BK_1 },		  /* NEC Versa L320 */
55 	{ { 0x63, 0x02, 0x64 },	0xf8, 0xf8, 0 },
56 	{ { 0x63, 0x03, 0xc8 }, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* Dell Latitude D800 */
57 	{ { 0x73, 0x00, 0x0a },	0xf8, 0xf8, ALPS_DUALPOINT },		  /* ThinkPad R61 8918-5QG */
58 	{ { 0x73, 0x02, 0x0a },	0xf8, 0xf8, 0 },
59 	{ { 0x73, 0x02, 0x14 },	0xf8, 0xf8, ALPS_FW_BK_2 },		  /* Ahtec Laptop */
60 	{ { 0x20, 0x02, 0x0e },	0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* XXX */
61 	{ { 0x22, 0x02, 0x0a },	0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT },
62 	{ { 0x22, 0x02, 0x14 }, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT }, /* Dell Latitude D600 */
63 	/* Dell Latitude E5500, E6400, E6500, Precision M4400 */
64 	{ { 0x62, 0x02, 0x14 }, 0xcf, 0xcf,
65 		ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED },
66 	{ { 0x73, 0x02, 0x50 }, 0xcf, 0xcf, ALPS_FOUR_BUTTONS },	  /* Dell Vostro 1400 */
67 	{ { 0x73, 0x02, 0x64 }, 0xf8, 0xf8, 0 },			  /* HP Pavilion dm3 */
68 	{ { 0x52, 0x01, 0x14 }, 0xff, 0xff,
69 		ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED },	  /* Toshiba Tecra A11-11L */
70 };
71 
72 /*
73  * XXX - this entry is suspicious. First byte has zero lower nibble,
74  * which is what a normal mouse would report. Also, the value 0x0e
75  * isn't valid per PS/2 spec.
76  */
77 
78 /*
79  * PS/2 packet format
80  *
81  * byte 0:  0    0 YSGN XSGN    1    M    R    L
82  * byte 1: X7   X6   X5   X4   X3   X2   X1   X0
83  * byte 2: Y7   Y6   Y5   Y4   Y3   Y2   Y1   Y0
84  *
85  * Note that the device never signals overflow condition.
86  *
87  * ALPS absolute Mode - new format
88  *
89  * byte 0:  1    ?    ?    ?    1    ?    ?    ?
90  * byte 1:  0   x6   x5   x4   x3   x2   x1   x0
91  * byte 2:  0  x10   x9   x8   x7    ?  fin  ges
92  * byte 3:  0   y9   y8   y7    1    M    R    L
93  * byte 4:  0   y6   y5   y4   y3   y2   y1   y0
94  * byte 5:  0   z6   z5   z4   z3   z2   z1   z0
95  *
96  * Dualpoint device -- interleaved packet format
97  *
98  * byte 0:    1    1    0    0    1    1    1    1
99  * byte 1:    0   x6   x5   x4   x3   x2   x1   x0
100  * byte 2:    0  x10   x9   x8   x7    0  fin  ges
101  * byte 3:    0    0 YSGN XSGN    1    1    1    1
102  * byte 4:   X7   X6   X5   X4   X3   X2   X1   X0
103  * byte 5:   Y7   Y6   Y5   Y4   Y3   Y2   Y1   Y0
104  * byte 6:    0   y9   y8   y7    1    m    r    l
105  * byte 7:    0   y6   y5   y4   y3   y2   y1   y0
106  * byte 8:    0   z6   z5   z4   z3   z2   z1   z0
107  *
108  * CAPITALS = stick, miniscules = touchpad
109  *
110  * ?'s can have different meanings on different models,
111  * such as wheel rotation, extra buttons, stick buttons
112  * on a dualpoint, etc.
113  */
114 
115 static bool alps_is_valid_first_byte(const struct alps_model_info *model,
116 				     unsigned char data)
117 {
118 	return (data & model->mask0) == model->byte0;
119 }
120 
121 static void alps_report_buttons(struct psmouse *psmouse,
122 				struct input_dev *dev1, struct input_dev *dev2,
123 				int left, int right, int middle)
124 {
125 	struct input_dev *dev;
126 
127 	/*
128 	 * If shared button has already been reported on the
129 	 * other device (dev2) then this event should be also
130 	 * sent through that device.
131 	 */
132 	dev = test_bit(BTN_LEFT, dev2->key) ? dev2 : dev1;
133 	input_report_key(dev, BTN_LEFT, left);
134 
135 	dev = test_bit(BTN_RIGHT, dev2->key) ? dev2 : dev1;
136 	input_report_key(dev, BTN_RIGHT, right);
137 
138 	dev = test_bit(BTN_MIDDLE, dev2->key) ? dev2 : dev1;
139 	input_report_key(dev, BTN_MIDDLE, middle);
140 
141 	/*
142 	 * Sync the _other_ device now, we'll do the first
143 	 * device later once we report the rest of the events.
144 	 */
145 	input_sync(dev2);
146 }
147 
148 static void alps_process_packet(struct psmouse *psmouse)
149 {
150 	struct alps_data *priv = psmouse->private;
151 	const struct alps_model_info *model = priv->i;
152 	unsigned char *packet = psmouse->packet;
153 	struct input_dev *dev = psmouse->dev;
154 	struct input_dev *dev2 = priv->dev2;
155 	int x, y, z, ges, fin, left, right, middle;
156 	int back = 0, forward = 0;
157 
158 	if (model->flags & ALPS_OLDPROTO) {
159 		left = packet[2] & 0x10;
160 		right = packet[2] & 0x08;
161 		middle = 0;
162 		x = packet[1] | ((packet[0] & 0x07) << 7);
163 		y = packet[4] | ((packet[3] & 0x07) << 7);
164 		z = packet[5];
165 	} else {
166 		left = packet[3] & 1;
167 		right = packet[3] & 2;
168 		middle = packet[3] & 4;
169 		x = packet[1] | ((packet[2] & 0x78) << (7 - 3));
170 		y = packet[4] | ((packet[3] & 0x70) << (7 - 4));
171 		z = packet[5];
172 	}
173 
174 	if (model->flags & ALPS_FW_BK_1) {
175 		back = packet[0] & 0x10;
176 		forward = packet[2] & 4;
177 	}
178 
179 	if (model->flags & ALPS_FW_BK_2) {
180 		back = packet[3] & 4;
181 		forward = packet[2] & 4;
182 		if ((middle = forward && back))
183 			forward = back = 0;
184 	}
185 
186 	ges = packet[2] & 1;
187 	fin = packet[2] & 2;
188 
189 	if ((model->flags & ALPS_DUALPOINT) && z == 127) {
190 		input_report_rel(dev2, REL_X,  (x > 383 ? (x - 768) : x));
191 		input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y));
192 
193 		alps_report_buttons(psmouse, dev2, dev, left, right, middle);
194 
195 		input_sync(dev2);
196 		return;
197 	}
198 
199 	alps_report_buttons(psmouse, dev, dev2, left, right, middle);
200 
201 	/* Convert hardware tap to a reasonable Z value */
202 	if (ges && !fin)
203 		z = 40;
204 
205 	/*
206 	 * A "tap and drag" operation is reported by the hardware as a transition
207 	 * from (!fin && ges) to (fin && ges). This should be translated to the
208 	 * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
209 	 */
210 	if (ges && fin && !priv->prev_fin) {
211 		input_report_abs(dev, ABS_X, x);
212 		input_report_abs(dev, ABS_Y, y);
213 		input_report_abs(dev, ABS_PRESSURE, 0);
214 		input_report_key(dev, BTN_TOOL_FINGER, 0);
215 		input_sync(dev);
216 	}
217 	priv->prev_fin = fin;
218 
219 	if (z > 30)
220 		input_report_key(dev, BTN_TOUCH, 1);
221 	if (z < 25)
222 		input_report_key(dev, BTN_TOUCH, 0);
223 
224 	if (z > 0) {
225 		input_report_abs(dev, ABS_X, x);
226 		input_report_abs(dev, ABS_Y, y);
227 	}
228 
229 	input_report_abs(dev, ABS_PRESSURE, z);
230 	input_report_key(dev, BTN_TOOL_FINGER, z > 0);
231 
232 	if (model->flags & ALPS_WHEEL)
233 		input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
234 
235 	if (model->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
236 		input_report_key(dev, BTN_FORWARD, forward);
237 		input_report_key(dev, BTN_BACK, back);
238 	}
239 
240 	if (model->flags & ALPS_FOUR_BUTTONS) {
241 		input_report_key(dev, BTN_0, packet[2] & 4);
242 		input_report_key(dev, BTN_1, packet[0] & 0x10);
243 		input_report_key(dev, BTN_2, packet[3] & 4);
244 		input_report_key(dev, BTN_3, packet[0] & 0x20);
245 	}
246 
247 	input_sync(dev);
248 }
249 
250 static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
251 					unsigned char packet[],
252 					bool report_buttons)
253 {
254 	struct alps_data *priv = psmouse->private;
255 	struct input_dev *dev2 = priv->dev2;
256 
257 	if (report_buttons)
258 		alps_report_buttons(psmouse, dev2, psmouse->dev,
259 				packet[0] & 1, packet[0] & 2, packet[0] & 4);
260 
261 	input_report_rel(dev2, REL_X,
262 		packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
263 	input_report_rel(dev2, REL_Y,
264 		packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
265 
266 	input_sync(dev2);
267 }
268 
269 static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
270 {
271 	struct alps_data *priv = psmouse->private;
272 
273 	if (psmouse->pktcnt < 6)
274 		return PSMOUSE_GOOD_DATA;
275 
276 	if (psmouse->pktcnt == 6) {
277 		/*
278 		 * Start a timer to flush the packet if it ends up last
279 		 * 6-byte packet in the stream. Timer needs to fire
280 		 * psmouse core times out itself. 20 ms should be enough
281 		 * to decide if we are getting more data or not.
282 		 */
283 		mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
284 		return PSMOUSE_GOOD_DATA;
285 	}
286 
287 	del_timer(&priv->timer);
288 
289 	if (psmouse->packet[6] & 0x80) {
290 
291 		/*
292 		 * Highest bit is set - that means we either had
293 		 * complete ALPS packet and this is start of the
294 		 * next packet or we got garbage.
295 		 */
296 
297 		if (((psmouse->packet[3] |
298 		      psmouse->packet[4] |
299 		      psmouse->packet[5]) & 0x80) ||
300 		    (!alps_is_valid_first_byte(priv->i, psmouse->packet[6]))) {
301 			dbg("refusing packet %x %x %x %x "
302 			    "(suspected interleaved ps/2)\n",
303 			    psmouse->packet[3], psmouse->packet[4],
304 			    psmouse->packet[5], psmouse->packet[6]);
305 			return PSMOUSE_BAD_DATA;
306 		}
307 
308 		alps_process_packet(psmouse);
309 
310 		/* Continue with the next packet */
311 		psmouse->packet[0] = psmouse->packet[6];
312 		psmouse->pktcnt = 1;
313 
314 	} else {
315 
316 		/*
317 		 * High bit is 0 - that means that we indeed got a PS/2
318 		 * packet in the middle of ALPS packet.
319 		 *
320 		 * There is also possibility that we got 6-byte ALPS
321 		 * packet followed  by 3-byte packet from trackpoint. We
322 		 * can not distinguish between these 2 scenarios but
323 		 * becase the latter is unlikely to happen in course of
324 		 * normal operation (user would need to press all
325 		 * buttons on the pad and start moving trackpoint
326 		 * without touching the pad surface) we assume former.
327 		 * Even if we are wrong the wost thing that would happen
328 		 * the cursor would jump but we should not get protocol
329 		 * desynchronization.
330 		 */
331 
332 		alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
333 					    false);
334 
335 		/*
336 		 * Continue with the standard ALPS protocol handling,
337 		 * but make sure we won't process it as an interleaved
338 		 * packet again, which may happen if all buttons are
339 		 * pressed. To avoid this let's reset the 4th bit which
340 		 * is normally 1.
341 		 */
342 		psmouse->packet[3] = psmouse->packet[6] & 0xf7;
343 		psmouse->pktcnt = 4;
344 	}
345 
346 	return PSMOUSE_GOOD_DATA;
347 }
348 
349 static void alps_flush_packet(unsigned long data)
350 {
351 	struct psmouse *psmouse = (struct psmouse *)data;
352 
353 	serio_pause_rx(psmouse->ps2dev.serio);
354 
355 	if (psmouse->pktcnt == 6) {
356 
357 		/*
358 		 * We did not any more data in reasonable amount of time.
359 		 * Validate the last 3 bytes and process as a standard
360 		 * ALPS packet.
361 		 */
362 		if ((psmouse->packet[3] |
363 		     psmouse->packet[4] |
364 		     psmouse->packet[5]) & 0x80) {
365 			dbg("refusing packet %x %x %x "
366 			    "(suspected interleaved ps/2)\n",
367 			    psmouse->packet[3], psmouse->packet[4],
368 			    psmouse->packet[5]);
369 		} else {
370 			alps_process_packet(psmouse);
371 		}
372 		psmouse->pktcnt = 0;
373 	}
374 
375 	serio_continue_rx(psmouse->ps2dev.serio);
376 }
377 
378 static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
379 {
380 	struct alps_data *priv = psmouse->private;
381 	const struct alps_model_info *model = priv->i;
382 
383 	if ((psmouse->packet[0] & 0xc8) == 0x08) { /* PS/2 packet */
384 		if (psmouse->pktcnt == 3) {
385 			alps_report_bare_ps2_packet(psmouse, psmouse->packet,
386 						    true);
387 			return PSMOUSE_FULL_PACKET;
388 		}
389 		return PSMOUSE_GOOD_DATA;
390 	}
391 
392 	/* Check for PS/2 packet stuffed in the middle of ALPS packet. */
393 
394 	if ((model->flags & ALPS_PS2_INTERLEAVED) &&
395 	    psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
396 		return alps_handle_interleaved_ps2(psmouse);
397 	}
398 
399 	if (!alps_is_valid_first_byte(model, psmouse->packet[0])) {
400 		dbg("refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
401 		    psmouse->packet[0], model->mask0, model->byte0);
402 		return PSMOUSE_BAD_DATA;
403 	}
404 
405 	/* Bytes 2 - 6 should have 0 in the highest bit */
406 	if (psmouse->pktcnt >= 2 && psmouse->pktcnt <= 6 &&
407 	    (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
408 		dbg("refusing packet[%i] = %x\n",
409 		    psmouse->pktcnt - 1, psmouse->packet[psmouse->pktcnt - 1]);
410 		return PSMOUSE_BAD_DATA;
411 	}
412 
413 	if (psmouse->pktcnt == 6) {
414 		alps_process_packet(psmouse);
415 		return PSMOUSE_FULL_PACKET;
416 	}
417 
418 	return PSMOUSE_GOOD_DATA;
419 }
420 
421 static const struct alps_model_info *alps_get_model(struct psmouse *psmouse, int *version)
422 {
423 	struct ps2dev *ps2dev = &psmouse->ps2dev;
424 	static const unsigned char rates[] = { 0, 10, 20, 40, 60, 80, 100, 200 };
425 	unsigned char param[4];
426 	int i;
427 
428 	/*
429 	 * First try "E6 report".
430 	 * ALPS should return 0,0,10 or 0,0,100
431 	 */
432 	param[0] = 0;
433 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES) ||
434 	    ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE11) ||
435 	    ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE11) ||
436 	    ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE11))
437 		return NULL;
438 
439 	param[0] = param[1] = param[2] = 0xff;
440 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
441 		return NULL;
442 
443 	dbg("E6 report: %2.2x %2.2x %2.2x", param[0], param[1], param[2]);
444 
445 	if (param[0] != 0 || param[1] != 0 || (param[2] != 10 && param[2] != 100))
446 		return NULL;
447 
448 	/*
449 	 * Now try "E7 report". Allowed responses are in
450 	 * alps_model_data[].signature
451 	 */
452 	param[0] = 0;
453 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES) ||
454 	    ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE21) ||
455 	    ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE21) ||
456 	    ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE21))
457 		return NULL;
458 
459 	param[0] = param[1] = param[2] = 0xff;
460 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
461 		return NULL;
462 
463 	dbg("E7 report: %2.2x %2.2x %2.2x", param[0], param[1], param[2]);
464 
465 	if (version) {
466 		for (i = 0; i < ARRAY_SIZE(rates) && param[2] != rates[i]; i++)
467 			/* empty */;
468 		*version = (param[0] << 8) | (param[1] << 4) | i;
469 	}
470 
471 	for (i = 0; i < ARRAY_SIZE(alps_model_data); i++)
472 		if (!memcmp(param, alps_model_data[i].signature,
473 			    sizeof(alps_model_data[i].signature)))
474 			return alps_model_data + i;
475 
476 	return NULL;
477 }
478 
479 /*
480  * For DualPoint devices select the device that should respond to
481  * subsequent commands. It looks like glidepad is behind stickpointer,
482  * I'd thought it would be other way around...
483  */
484 static int alps_passthrough_mode(struct psmouse *psmouse, bool enable)
485 {
486 	struct ps2dev *ps2dev = &psmouse->ps2dev;
487 	int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
488 
489 	if (ps2_command(ps2dev, NULL, cmd) ||
490 	    ps2_command(ps2dev, NULL, cmd) ||
491 	    ps2_command(ps2dev, NULL, cmd) ||
492 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
493 		return -1;
494 
495 	/* we may get 3 more bytes, just ignore them */
496 	ps2_drain(ps2dev, 3, 100);
497 
498 	return 0;
499 }
500 
501 static int alps_absolute_mode(struct psmouse *psmouse)
502 {
503 	struct ps2dev *ps2dev = &psmouse->ps2dev;
504 
505 	/* Try ALPS magic knock - 4 disable before enable */
506 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
507 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
508 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
509 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
510 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
511 		return -1;
512 
513 	/*
514 	 * Switch mouse to poll (remote) mode so motion data will not
515 	 * get in our way
516 	 */
517 	return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
518 }
519 
520 static int alps_get_status(struct psmouse *psmouse, char *param)
521 {
522 	struct ps2dev *ps2dev = &psmouse->ps2dev;
523 
524 	/* Get status: 0xF5 0xF5 0xF5 0xE9 */
525 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
526 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
527 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
528 	    ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
529 		return -1;
530 
531 	dbg("Status: %2.2x %2.2x %2.2x", param[0], param[1], param[2]);
532 
533 	return 0;
534 }
535 
536 /*
537  * Turn touchpad tapping on or off. The sequences are:
538  * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
539  * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
540  * My guess that 0xE9 (GetInfo) is here as a sync point.
541  * For models that also have stickpointer (DualPoints) its tapping
542  * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
543  * we don't fiddle with it.
544  */
545 static int alps_tap_mode(struct psmouse *psmouse, int enable)
546 {
547 	struct ps2dev *ps2dev = &psmouse->ps2dev;
548 	int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
549 	unsigned char tap_arg = enable ? 0x0A : 0x00;
550 	unsigned char param[4];
551 
552 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
553 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
554 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
555 	    ps2_command(ps2dev, &tap_arg, cmd))
556 		return -1;
557 
558 	if (alps_get_status(psmouse, param))
559 		return -1;
560 
561 	return 0;
562 }
563 
564 /*
565  * alps_poll() - poll the touchpad for current motion packet.
566  * Used in resync.
567  */
568 static int alps_poll(struct psmouse *psmouse)
569 {
570 	struct alps_data *priv = psmouse->private;
571 	unsigned char buf[6];
572 	bool poll_failed;
573 
574 	if (priv->i->flags & ALPS_PASS)
575 		alps_passthrough_mode(psmouse, true);
576 
577 	poll_failed = ps2_command(&psmouse->ps2dev, buf,
578 				  PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
579 
580 	if (priv->i->flags & ALPS_PASS)
581 		alps_passthrough_mode(psmouse, false);
582 
583 	if (poll_failed || (buf[0] & priv->i->mask0) != priv->i->byte0)
584 		return -1;
585 
586 	if ((psmouse->badbyte & 0xc8) == 0x08) {
587 /*
588  * Poll the track stick ...
589  */
590 		if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
591 			return -1;
592 	}
593 
594 	memcpy(psmouse->packet, buf, sizeof(buf));
595 	return 0;
596 }
597 
598 static int alps_hw_init(struct psmouse *psmouse)
599 {
600 	struct alps_data *priv = psmouse->private;
601 	const struct alps_model_info *model = priv->i;
602 
603 	if ((model->flags & ALPS_PASS) &&
604 	    alps_passthrough_mode(psmouse, true)) {
605 		return -1;
606 	}
607 
608 	if (alps_tap_mode(psmouse, true)) {
609 		printk(KERN_WARNING "alps.c: Failed to enable hardware tapping\n");
610 		return -1;
611 	}
612 
613 	if (alps_absolute_mode(psmouse)) {
614 		printk(KERN_ERR "alps.c: Failed to enable absolute mode\n");
615 		return -1;
616 	}
617 
618 	if ((model->flags & ALPS_PASS) &&
619 	    alps_passthrough_mode(psmouse, false)) {
620 		return -1;
621 	}
622 
623 	/* ALPS needs stream mode, otherwise it won't report any data */
624 	if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
625 		printk(KERN_ERR "alps.c: Failed to enable stream mode\n");
626 		return -1;
627 	}
628 
629 	return 0;
630 }
631 
632 static int alps_reconnect(struct psmouse *psmouse)
633 {
634 	const struct alps_model_info *model;
635 
636 	psmouse_reset(psmouse);
637 
638 	model = alps_get_model(psmouse, NULL);
639 	if (!model)
640 		return -1;
641 
642 	return alps_hw_init(psmouse);
643 }
644 
645 static void alps_disconnect(struct psmouse *psmouse)
646 {
647 	struct alps_data *priv = psmouse->private;
648 
649 	psmouse_reset(psmouse);
650 	del_timer_sync(&priv->timer);
651 	input_unregister_device(priv->dev2);
652 	kfree(priv);
653 }
654 
655 int alps_init(struct psmouse *psmouse)
656 {
657 	struct alps_data *priv;
658 	const struct alps_model_info *model;
659 	struct input_dev *dev1 = psmouse->dev, *dev2;
660 	int version;
661 
662 	priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
663 	dev2 = input_allocate_device();
664 	if (!priv || !dev2)
665 		goto init_fail;
666 
667 	priv->dev2 = dev2;
668 	setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
669 
670 	psmouse->private = priv;
671 
672 	model = alps_get_model(psmouse, &version);
673 	if (!model)
674 		goto init_fail;
675 
676 	priv->i = model;
677 
678 	if (alps_hw_init(psmouse))
679 		goto init_fail;
680 
681 	/*
682 	 * Undo part of setup done for us by psmouse core since touchpad
683 	 * is not a relative device.
684 	 */
685 	__clear_bit(EV_REL, dev1->evbit);
686 	__clear_bit(REL_X, dev1->relbit);
687 	__clear_bit(REL_Y, dev1->relbit);
688 
689 	/*
690 	 * Now set up our capabilities.
691 	 */
692 	dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
693 	dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
694 	dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
695 	dev1->keybit[BIT_WORD(BTN_LEFT)] |=
696 		BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
697 
698 	dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
699 	input_set_abs_params(dev1, ABS_X, 0, 1023, 0, 0);
700 	input_set_abs_params(dev1, ABS_Y, 0, 767, 0, 0);
701 	input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
702 
703 	if (model->flags & ALPS_WHEEL) {
704 		dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
705 		dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
706 	}
707 
708 	if (model->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
709 		dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
710 		dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
711 	}
712 
713 	if (model->flags & ALPS_FOUR_BUTTONS) {
714 		dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
715 		dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
716 		dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
717 		dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
718 	} else {
719 		dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
720 	}
721 
722 	snprintf(priv->phys, sizeof(priv->phys), "%s/input1", psmouse->ps2dev.serio->phys);
723 	dev2->phys = priv->phys;
724 	dev2->name = (model->flags & ALPS_DUALPOINT) ? "DualPoint Stick" : "PS/2 Mouse";
725 	dev2->id.bustype = BUS_I8042;
726 	dev2->id.vendor  = 0x0002;
727 	dev2->id.product = PSMOUSE_ALPS;
728 	dev2->id.version = 0x0000;
729 	dev2->dev.parent = &psmouse->ps2dev.serio->dev;
730 
731 	dev2->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
732 	dev2->relbit[BIT_WORD(REL_X)] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
733 	dev2->keybit[BIT_WORD(BTN_LEFT)] =
734 		BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_MIDDLE) | BIT_MASK(BTN_RIGHT);
735 
736 	if (input_register_device(priv->dev2))
737 		goto init_fail;
738 
739 	psmouse->protocol_handler = alps_process_byte;
740 	psmouse->poll = alps_poll;
741 	psmouse->disconnect = alps_disconnect;
742 	psmouse->reconnect = alps_reconnect;
743 	psmouse->pktsize = 6;
744 
745 	/* We are having trouble resyncing ALPS touchpads so disable it for now */
746 	psmouse->resync_time = 0;
747 
748 	return 0;
749 
750 init_fail:
751 	psmouse_reset(psmouse);
752 	input_free_device(dev2);
753 	kfree(priv);
754 	psmouse->private = NULL;
755 	return -1;
756 }
757 
758 int alps_detect(struct psmouse *psmouse, bool set_properties)
759 {
760 	int version;
761 	const struct alps_model_info *model;
762 
763 	model = alps_get_model(psmouse, &version);
764 	if (!model)
765 		return -1;
766 
767 	if (set_properties) {
768 		psmouse->vendor = "ALPS";
769 		psmouse->name = model->flags & ALPS_DUALPOINT ?
770 				"DualPoint TouchPad" : "GlidePoint";
771 		psmouse->model = version;
772 	}
773 	return 0;
774 }
775 
776