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