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