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