xref: /openbmc/linux/drivers/input/mouse/alps.c (revision 161f4089)
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/input/mt.h>
21 #include <linux/serio.h>
22 #include <linux/libps2.h>
23 
24 #include "psmouse.h"
25 #include "alps.h"
26 
27 /*
28  * Definitions for ALPS version 3 and 4 command mode protocol
29  */
30 #define ALPS_CMD_NIBBLE_10	0x01f2
31 
32 #define ALPS_REG_BASE_RUSHMORE	0xc2c0
33 #define ALPS_REG_BASE_PINNACLE	0x0000
34 
35 static const struct alps_nibble_commands alps_v3_nibble_commands[] = {
36 	{ PSMOUSE_CMD_SETPOLL,		0x00 }, /* 0 */
37 	{ PSMOUSE_CMD_RESET_DIS,	0x00 }, /* 1 */
38 	{ PSMOUSE_CMD_SETSCALE21,	0x00 }, /* 2 */
39 	{ PSMOUSE_CMD_SETRATE,		0x0a }, /* 3 */
40 	{ PSMOUSE_CMD_SETRATE,		0x14 }, /* 4 */
41 	{ PSMOUSE_CMD_SETRATE,		0x28 }, /* 5 */
42 	{ PSMOUSE_CMD_SETRATE,		0x3c }, /* 6 */
43 	{ PSMOUSE_CMD_SETRATE,		0x50 }, /* 7 */
44 	{ PSMOUSE_CMD_SETRATE,		0x64 }, /* 8 */
45 	{ PSMOUSE_CMD_SETRATE,		0xc8 }, /* 9 */
46 	{ ALPS_CMD_NIBBLE_10,		0x00 }, /* a */
47 	{ PSMOUSE_CMD_SETRES,		0x00 }, /* b */
48 	{ PSMOUSE_CMD_SETRES,		0x01 }, /* c */
49 	{ PSMOUSE_CMD_SETRES,		0x02 }, /* d */
50 	{ PSMOUSE_CMD_SETRES,		0x03 }, /* e */
51 	{ PSMOUSE_CMD_SETSCALE11,	0x00 }, /* f */
52 };
53 
54 static const struct alps_nibble_commands alps_v4_nibble_commands[] = {
55 	{ PSMOUSE_CMD_ENABLE,		0x00 }, /* 0 */
56 	{ PSMOUSE_CMD_RESET_DIS,	0x00 }, /* 1 */
57 	{ PSMOUSE_CMD_SETSCALE21,	0x00 }, /* 2 */
58 	{ PSMOUSE_CMD_SETRATE,		0x0a }, /* 3 */
59 	{ PSMOUSE_CMD_SETRATE,		0x14 }, /* 4 */
60 	{ PSMOUSE_CMD_SETRATE,		0x28 }, /* 5 */
61 	{ PSMOUSE_CMD_SETRATE,		0x3c }, /* 6 */
62 	{ PSMOUSE_CMD_SETRATE,		0x50 }, /* 7 */
63 	{ PSMOUSE_CMD_SETRATE,		0x64 }, /* 8 */
64 	{ PSMOUSE_CMD_SETRATE,		0xc8 }, /* 9 */
65 	{ ALPS_CMD_NIBBLE_10,		0x00 }, /* a */
66 	{ PSMOUSE_CMD_SETRES,		0x00 }, /* b */
67 	{ PSMOUSE_CMD_SETRES,		0x01 }, /* c */
68 	{ PSMOUSE_CMD_SETRES,		0x02 }, /* d */
69 	{ PSMOUSE_CMD_SETRES,		0x03 }, /* e */
70 	{ PSMOUSE_CMD_SETSCALE11,	0x00 }, /* f */
71 };
72 
73 
74 #define ALPS_DUALPOINT		0x02	/* touchpad has trackstick */
75 #define ALPS_PASS		0x04	/* device has a pass-through port */
76 
77 #define ALPS_WHEEL		0x08	/* hardware wheel present */
78 #define ALPS_FW_BK_1		0x10	/* front & back buttons present */
79 #define ALPS_FW_BK_2		0x20	/* front & back buttons present */
80 #define ALPS_FOUR_BUTTONS	0x40	/* 4 direction button present */
81 #define ALPS_PS2_INTERLEAVED	0x80	/* 3-byte PS/2 packet interleaved with
82 					   6-byte ALPS packet */
83 
84 static const struct alps_model_info alps_model_data[] = {
85 	{ { 0x32, 0x02, 0x14 },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT },	/* Toshiba Salellite Pro M10 */
86 	{ { 0x33, 0x02, 0x0a },	0x00, ALPS_PROTO_V1, 0x88, 0xf8, 0 },				/* UMAX-530T */
87 	{ { 0x53, 0x02, 0x0a },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
88 	{ { 0x53, 0x02, 0x14 },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
89 	{ { 0x60, 0x03, 0xc8 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },				/* HP ze1115 */
90 	{ { 0x63, 0x02, 0x0a },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
91 	{ { 0x63, 0x02, 0x14 },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
92 	{ { 0x63, 0x02, 0x28 },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 },		/* Fujitsu Siemens S6010 */
93 	{ { 0x63, 0x02, 0x3c },	0x00, ALPS_PROTO_V2, 0x8f, 0x8f, ALPS_WHEEL },			/* Toshiba Satellite S2400-103 */
94 	{ { 0x63, 0x02, 0x50 },	0x00, ALPS_PROTO_V2, 0xef, 0xef, ALPS_FW_BK_1 },		/* NEC Versa L320 */
95 	{ { 0x63, 0x02, 0x64 },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
96 	{ { 0x63, 0x03, 0xc8 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT },	/* Dell Latitude D800 */
97 	{ { 0x73, 0x00, 0x0a },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_DUALPOINT },		/* ThinkPad R61 8918-5QG */
98 	{ { 0x73, 0x02, 0x0a },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
99 	{ { 0x73, 0x02, 0x14 },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 },		/* Ahtec Laptop */
100 	{ { 0x20, 0x02, 0x0e },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT },	/* XXX */
101 	{ { 0x22, 0x02, 0x0a },	0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT },
102 	{ { 0x22, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT },	/* Dell Latitude D600 */
103 	/* Dell Latitude E5500, E6400, E6500, Precision M4400 */
104 	{ { 0x62, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xcf, 0xcf,
105 		ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED },
106 	{ { 0x73, 0x00, 0x14 }, 0x00, ALPS_PROTO_V2, 0xcf, 0xcf, ALPS_DUALPOINT },		/* Dell XT2 */
107 	{ { 0x73, 0x02, 0x50 }, 0x00, ALPS_PROTO_V2, 0xcf, 0xcf, ALPS_FOUR_BUTTONS },		/* Dell Vostro 1400 */
108 	{ { 0x52, 0x01, 0x14 }, 0x00, ALPS_PROTO_V2, 0xff, 0xff,
109 		ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED },				/* Toshiba Tecra A11-11L */
110 	{ { 0x73, 0x02, 0x64 },	0x8a, ALPS_PROTO_V4, 0x8f, 0x8f, 0 },
111 };
112 
113 static void alps_set_abs_params_st(struct alps_data *priv,
114 				   struct input_dev *dev1);
115 static void alps_set_abs_params_mt(struct alps_data *priv,
116 				   struct input_dev *dev1);
117 
118 /*
119  * XXX - this entry is suspicious. First byte has zero lower nibble,
120  * which is what a normal mouse would report. Also, the value 0x0e
121  * isn't valid per PS/2 spec.
122  */
123 
124 /* Packet formats are described in Documentation/input/alps.txt */
125 
126 static bool alps_is_valid_first_byte(struct alps_data *priv,
127 				     unsigned char data)
128 {
129 	return (data & priv->mask0) == priv->byte0;
130 }
131 
132 static void alps_report_buttons(struct psmouse *psmouse,
133 				struct input_dev *dev1, struct input_dev *dev2,
134 				int left, int right, int middle)
135 {
136 	struct input_dev *dev;
137 
138 	/*
139 	 * If shared button has already been reported on the
140 	 * other device (dev2) then this event should be also
141 	 * sent through that device.
142 	 */
143 	dev = test_bit(BTN_LEFT, dev2->key) ? dev2 : dev1;
144 	input_report_key(dev, BTN_LEFT, left);
145 
146 	dev = test_bit(BTN_RIGHT, dev2->key) ? dev2 : dev1;
147 	input_report_key(dev, BTN_RIGHT, right);
148 
149 	dev = test_bit(BTN_MIDDLE, dev2->key) ? dev2 : dev1;
150 	input_report_key(dev, BTN_MIDDLE, middle);
151 
152 	/*
153 	 * Sync the _other_ device now, we'll do the first
154 	 * device later once we report the rest of the events.
155 	 */
156 	input_sync(dev2);
157 }
158 
159 static void alps_process_packet_v1_v2(struct psmouse *psmouse)
160 {
161 	struct alps_data *priv = psmouse->private;
162 	unsigned char *packet = psmouse->packet;
163 	struct input_dev *dev = psmouse->dev;
164 	struct input_dev *dev2 = priv->dev2;
165 	int x, y, z, ges, fin, left, right, middle;
166 	int back = 0, forward = 0;
167 
168 	if (priv->proto_version == ALPS_PROTO_V1) {
169 		left = packet[2] & 0x10;
170 		right = packet[2] & 0x08;
171 		middle = 0;
172 		x = packet[1] | ((packet[0] & 0x07) << 7);
173 		y = packet[4] | ((packet[3] & 0x07) << 7);
174 		z = packet[5];
175 	} else {
176 		left = packet[3] & 1;
177 		right = packet[3] & 2;
178 		middle = packet[3] & 4;
179 		x = packet[1] | ((packet[2] & 0x78) << (7 - 3));
180 		y = packet[4] | ((packet[3] & 0x70) << (7 - 4));
181 		z = packet[5];
182 	}
183 
184 	if (priv->flags & ALPS_FW_BK_1) {
185 		back = packet[0] & 0x10;
186 		forward = packet[2] & 4;
187 	}
188 
189 	if (priv->flags & ALPS_FW_BK_2) {
190 		back = packet[3] & 4;
191 		forward = packet[2] & 4;
192 		if ((middle = forward && back))
193 			forward = back = 0;
194 	}
195 
196 	ges = packet[2] & 1;
197 	fin = packet[2] & 2;
198 
199 	if ((priv->flags & ALPS_DUALPOINT) && z == 127) {
200 		input_report_rel(dev2, REL_X,  (x > 383 ? (x - 768) : x));
201 		input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y));
202 
203 		alps_report_buttons(psmouse, dev2, dev, left, right, middle);
204 
205 		input_sync(dev2);
206 		return;
207 	}
208 
209 	alps_report_buttons(psmouse, dev, dev2, left, right, middle);
210 
211 	/* Convert hardware tap to a reasonable Z value */
212 	if (ges && !fin)
213 		z = 40;
214 
215 	/*
216 	 * A "tap and drag" operation is reported by the hardware as a transition
217 	 * from (!fin && ges) to (fin && ges). This should be translated to the
218 	 * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
219 	 */
220 	if (ges && fin && !priv->prev_fin) {
221 		input_report_abs(dev, ABS_X, x);
222 		input_report_abs(dev, ABS_Y, y);
223 		input_report_abs(dev, ABS_PRESSURE, 0);
224 		input_report_key(dev, BTN_TOOL_FINGER, 0);
225 		input_sync(dev);
226 	}
227 	priv->prev_fin = fin;
228 
229 	if (z > 30)
230 		input_report_key(dev, BTN_TOUCH, 1);
231 	if (z < 25)
232 		input_report_key(dev, BTN_TOUCH, 0);
233 
234 	if (z > 0) {
235 		input_report_abs(dev, ABS_X, x);
236 		input_report_abs(dev, ABS_Y, y);
237 	}
238 
239 	input_report_abs(dev, ABS_PRESSURE, z);
240 	input_report_key(dev, BTN_TOOL_FINGER, z > 0);
241 
242 	if (priv->flags & ALPS_WHEEL)
243 		input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
244 
245 	if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
246 		input_report_key(dev, BTN_FORWARD, forward);
247 		input_report_key(dev, BTN_BACK, back);
248 	}
249 
250 	if (priv->flags & ALPS_FOUR_BUTTONS) {
251 		input_report_key(dev, BTN_0, packet[2] & 4);
252 		input_report_key(dev, BTN_1, packet[0] & 0x10);
253 		input_report_key(dev, BTN_2, packet[3] & 4);
254 		input_report_key(dev, BTN_3, packet[0] & 0x20);
255 	}
256 
257 	input_sync(dev);
258 }
259 
260 /*
261  * Process bitmap data from v3 and v4 protocols. Returns the number of
262  * fingers detected. A return value of 0 means at least one of the
263  * bitmaps was empty.
264  *
265  * The bitmaps don't have enough data to track fingers, so this function
266  * only generates points representing a bounding box of all contacts.
267  * These points are returned in x1, y1, x2, and y2 when the return value
268  * is greater than 0.
269  */
270 static int alps_process_bitmap(struct alps_data *priv,
271 			       unsigned int x_map, unsigned int y_map,
272 			       int *x1, int *y1, int *x2, int *y2)
273 {
274 	struct alps_bitmap_point {
275 		int start_bit;
276 		int num_bits;
277 	};
278 
279 	int fingers_x = 0, fingers_y = 0, fingers;
280 	int i, bit, prev_bit;
281 	struct alps_bitmap_point x_low = {0,}, x_high = {0,};
282 	struct alps_bitmap_point y_low = {0,}, y_high = {0,};
283 	struct alps_bitmap_point *point;
284 
285 	if (!x_map || !y_map)
286 		return 0;
287 
288 	*x1 = *y1 = *x2 = *y2 = 0;
289 
290 	prev_bit = 0;
291 	point = &x_low;
292 	for (i = 0; x_map != 0; i++, x_map >>= 1) {
293 		bit = x_map & 1;
294 		if (bit) {
295 			if (!prev_bit) {
296 				point->start_bit = i;
297 				fingers_x++;
298 			}
299 			point->num_bits++;
300 		} else {
301 			if (prev_bit)
302 				point = &x_high;
303 			else
304 				point->num_bits = 0;
305 		}
306 		prev_bit = bit;
307 	}
308 
309 	/*
310 	 * y bitmap is reversed for what we need (lower positions are in
311 	 * higher bits), so we process from the top end.
312 	 */
313 	y_map = y_map << (sizeof(y_map) * BITS_PER_BYTE - priv->y_bits);
314 	prev_bit = 0;
315 	point = &y_low;
316 	for (i = 0; y_map != 0; i++, y_map <<= 1) {
317 		bit = y_map & (1 << (sizeof(y_map) * BITS_PER_BYTE - 1));
318 		if (bit) {
319 			if (!prev_bit) {
320 				point->start_bit = i;
321 				fingers_y++;
322 			}
323 			point->num_bits++;
324 		} else {
325 			if (prev_bit)
326 				point = &y_high;
327 			else
328 				point->num_bits = 0;
329 		}
330 		prev_bit = bit;
331 	}
332 
333 	/*
334 	 * Fingers can overlap, so we use the maximum count of fingers
335 	 * on either axis as the finger count.
336 	 */
337 	fingers = max(fingers_x, fingers_y);
338 
339 	/*
340 	 * If total fingers is > 1 but either axis reports only a single
341 	 * contact, we have overlapping or adjacent fingers. For the
342 	 * purposes of creating a bounding box, divide the single contact
343 	 * (roughly) equally between the two points.
344 	 */
345 	if (fingers > 1) {
346 		if (fingers_x == 1) {
347 			i = x_low.num_bits / 2;
348 			x_low.num_bits = x_low.num_bits - i;
349 			x_high.start_bit = x_low.start_bit + i;
350 			x_high.num_bits = max(i, 1);
351 		} else if (fingers_y == 1) {
352 			i = y_low.num_bits / 2;
353 			y_low.num_bits = y_low.num_bits - i;
354 			y_high.start_bit = y_low.start_bit + i;
355 			y_high.num_bits = max(i, 1);
356 		}
357 	}
358 
359 	*x1 = (priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
360 	      (2 * (priv->x_bits - 1));
361 	*y1 = (priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
362 	      (2 * (priv->y_bits - 1));
363 
364 	if (fingers > 1) {
365 		*x2 = (priv->x_max *
366 		       (2 * x_high.start_bit + x_high.num_bits - 1)) /
367 		      (2 * (priv->x_bits - 1));
368 		*y2 = (priv->y_max *
369 		       (2 * y_high.start_bit + y_high.num_bits - 1)) /
370 		      (2 * (priv->y_bits - 1));
371 	}
372 
373 	return fingers;
374 }
375 
376 static void alps_set_slot(struct input_dev *dev, int slot, bool active,
377 			  int x, int y)
378 {
379 	input_mt_slot(dev, slot);
380 	input_mt_report_slot_state(dev, MT_TOOL_FINGER, active);
381 	if (active) {
382 		input_report_abs(dev, ABS_MT_POSITION_X, x);
383 		input_report_abs(dev, ABS_MT_POSITION_Y, y);
384 	}
385 }
386 
387 static void alps_report_semi_mt_data(struct input_dev *dev, int num_fingers,
388 				     int x1, int y1, int x2, int y2)
389 {
390 	alps_set_slot(dev, 0, num_fingers != 0, x1, y1);
391 	alps_set_slot(dev, 1, num_fingers == 2, x2, y2);
392 }
393 
394 static void alps_process_trackstick_packet_v3(struct psmouse *psmouse)
395 {
396 	struct alps_data *priv = psmouse->private;
397 	unsigned char *packet = psmouse->packet;
398 	struct input_dev *dev = priv->dev2;
399 	int x, y, z, left, right, middle;
400 
401 	/* Sanity check packet */
402 	if (!(packet[0] & 0x40)) {
403 		psmouse_dbg(psmouse, "Bad trackstick packet, discarding\n");
404 		return;
405 	}
406 
407 	/*
408 	 * There's a special packet that seems to indicate the end
409 	 * of a stream of trackstick data. Filter these out.
410 	 */
411 	if (packet[1] == 0x7f && packet[2] == 0x7f && packet[4] == 0x7f)
412 		return;
413 
414 	x = (s8)(((packet[0] & 0x20) << 2) | (packet[1] & 0x7f));
415 	y = (s8)(((packet[0] & 0x10) << 3) | (packet[2] & 0x7f));
416 	z = (packet[4] & 0x7c) >> 2;
417 
418 	/*
419 	 * The x and y values tend to be quite large, and when used
420 	 * alone the trackstick is difficult to use. Scale them down
421 	 * to compensate.
422 	 */
423 	x /= 8;
424 	y /= 8;
425 
426 	input_report_rel(dev, REL_X, x);
427 	input_report_rel(dev, REL_Y, -y);
428 
429 	/*
430 	 * Most ALPS models report the trackstick buttons in the touchpad
431 	 * packets, but a few report them here. No reliable way has been
432 	 * found to differentiate between the models upfront, so we enable
433 	 * the quirk in response to seeing a button press in the trackstick
434 	 * packet.
435 	 */
436 	left = packet[3] & 0x01;
437 	right = packet[3] & 0x02;
438 	middle = packet[3] & 0x04;
439 
440 	if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) &&
441 	    (left || right || middle))
442 		priv->quirks |= ALPS_QUIRK_TRACKSTICK_BUTTONS;
443 
444 	if (priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) {
445 		input_report_key(dev, BTN_LEFT, left);
446 		input_report_key(dev, BTN_RIGHT, right);
447 		input_report_key(dev, BTN_MIDDLE, middle);
448 	}
449 
450 	input_sync(dev);
451 	return;
452 }
453 
454 static void alps_decode_buttons_v3(struct alps_fields *f, unsigned char *p)
455 {
456 	f->left = !!(p[3] & 0x01);
457 	f->right = !!(p[3] & 0x02);
458 	f->middle = !!(p[3] & 0x04);
459 
460 	f->ts_left = !!(p[3] & 0x10);
461 	f->ts_right = !!(p[3] & 0x20);
462 	f->ts_middle = !!(p[3] & 0x40);
463 }
464 
465 static void alps_decode_pinnacle(struct alps_fields *f, unsigned char *p)
466 {
467 	f->first_mp = !!(p[4] & 0x40);
468 	f->is_mp = !!(p[0] & 0x40);
469 
470 	f->fingers = (p[5] & 0x3) + 1;
471 	f->x_map = ((p[4] & 0x7e) << 8) |
472 		   ((p[1] & 0x7f) << 2) |
473 		   ((p[0] & 0x30) >> 4);
474 	f->y_map = ((p[3] & 0x70) << 4) |
475 		   ((p[2] & 0x7f) << 1) |
476 		   (p[4] & 0x01);
477 
478 	f->x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
479 	       ((p[0] & 0x30) >> 4);
480 	f->y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
481 	f->z = p[5] & 0x7f;
482 
483 	alps_decode_buttons_v3(f, p);
484 }
485 
486 static void alps_decode_rushmore(struct alps_fields *f, unsigned char *p)
487 {
488 	alps_decode_pinnacle(f, p);
489 
490 	f->x_map |= (p[5] & 0x10) << 11;
491 	f->y_map |= (p[5] & 0x20) << 6;
492 }
493 
494 static void alps_decode_dolphin(struct alps_fields *f, unsigned char *p)
495 {
496 	f->first_mp = !!(p[0] & 0x02);
497 	f->is_mp = !!(p[0] & 0x20);
498 
499 	f->fingers = ((p[0] & 0x6) >> 1 |
500 		     (p[0] & 0x10) >> 2);
501 	f->x_map = ((p[2] & 0x60) >> 5) |
502 		   ((p[4] & 0x7f) << 2) |
503 		   ((p[5] & 0x7f) << 9) |
504 		   ((p[3] & 0x07) << 16) |
505 		   ((p[3] & 0x70) << 15) |
506 		   ((p[0] & 0x01) << 22);
507 	f->y_map = (p[1] & 0x7f) |
508 		   ((p[2] & 0x1f) << 7);
509 
510 	f->x = ((p[1] & 0x7f) | ((p[4] & 0x0f) << 7));
511 	f->y = ((p[2] & 0x7f) | ((p[4] & 0xf0) << 3));
512 	f->z = (p[0] & 4) ? 0 : p[5] & 0x7f;
513 
514 	alps_decode_buttons_v3(f, p);
515 }
516 
517 static void alps_process_touchpad_packet_v3(struct psmouse *psmouse)
518 {
519 	struct alps_data *priv = psmouse->private;
520 	unsigned char *packet = psmouse->packet;
521 	struct input_dev *dev = psmouse->dev;
522 	struct input_dev *dev2 = priv->dev2;
523 	int x1 = 0, y1 = 0, x2 = 0, y2 = 0;
524 	int fingers = 0, bmap_fingers;
525 	struct alps_fields f;
526 
527 	priv->decode_fields(&f, packet);
528 
529 	/*
530 	 * There's no single feature of touchpad position and bitmap packets
531 	 * that can be used to distinguish between them. We rely on the fact
532 	 * that a bitmap packet should always follow a position packet with
533 	 * bit 6 of packet[4] set.
534 	 */
535 	if (priv->multi_packet) {
536 		/*
537 		 * Sometimes a position packet will indicate a multi-packet
538 		 * sequence, but then what follows is another position
539 		 * packet. Check for this, and when it happens process the
540 		 * position packet as usual.
541 		 */
542 		if (f.is_mp) {
543 			fingers = f.fingers;
544 			bmap_fingers = alps_process_bitmap(priv,
545 							   f.x_map, f.y_map,
546 							   &x1, &y1, &x2, &y2);
547 
548 			/*
549 			 * We shouldn't report more than one finger if
550 			 * we don't have two coordinates.
551 			 */
552 			if (fingers > 1 && bmap_fingers < 2)
553 				fingers = bmap_fingers;
554 
555 			/* Now process position packet */
556 			priv->decode_fields(&f, priv->multi_data);
557 		} else {
558 			priv->multi_packet = 0;
559 		}
560 	}
561 
562 	/*
563 	 * Bit 6 of byte 0 is not usually set in position packets. The only
564 	 * times it seems to be set is in situations where the data is
565 	 * suspect anyway, e.g. a palm resting flat on the touchpad. Given
566 	 * this combined with the fact that this bit is useful for filtering
567 	 * out misidentified bitmap packets, we reject anything with this
568 	 * bit set.
569 	 */
570 	if (f.is_mp)
571 		return;
572 
573 	if (!priv->multi_packet && f.first_mp) {
574 		priv->multi_packet = 1;
575 		memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
576 		return;
577 	}
578 
579 	priv->multi_packet = 0;
580 
581 	/*
582 	 * Sometimes the hardware sends a single packet with z = 0
583 	 * in the middle of a stream. Real releases generate packets
584 	 * with x, y, and z all zero, so these seem to be flukes.
585 	 * Ignore them.
586 	 */
587 	if (f.x && f.y && !f.z)
588 		return;
589 
590 	/*
591 	 * If we don't have MT data or the bitmaps were empty, we have
592 	 * to rely on ST data.
593 	 */
594 	if (!fingers) {
595 		x1 = f.x;
596 		y1 = f.y;
597 		fingers = f.z > 0 ? 1 : 0;
598 	}
599 
600 	if (f.z >= 64)
601 		input_report_key(dev, BTN_TOUCH, 1);
602 	else
603 		input_report_key(dev, BTN_TOUCH, 0);
604 
605 	alps_report_semi_mt_data(dev, fingers, x1, y1, x2, y2);
606 
607 	input_mt_report_finger_count(dev, fingers);
608 
609 	input_report_key(dev, BTN_LEFT, f.left);
610 	input_report_key(dev, BTN_RIGHT, f.right);
611 	input_report_key(dev, BTN_MIDDLE, f.middle);
612 
613 	if (f.z > 0) {
614 		input_report_abs(dev, ABS_X, f.x);
615 		input_report_abs(dev, ABS_Y, f.y);
616 	}
617 	input_report_abs(dev, ABS_PRESSURE, f.z);
618 
619 	input_sync(dev);
620 
621 	if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) {
622 		input_report_key(dev2, BTN_LEFT, f.ts_left);
623 		input_report_key(dev2, BTN_RIGHT, f.ts_right);
624 		input_report_key(dev2, BTN_MIDDLE, f.ts_middle);
625 		input_sync(dev2);
626 	}
627 }
628 
629 static void alps_process_packet_v3(struct psmouse *psmouse)
630 {
631 	unsigned char *packet = psmouse->packet;
632 
633 	/*
634 	 * v3 protocol packets come in three types, two representing
635 	 * touchpad data and one representing trackstick data.
636 	 * Trackstick packets seem to be distinguished by always
637 	 * having 0x3f in the last byte. This value has never been
638 	 * observed in the last byte of either of the other types
639 	 * of packets.
640 	 */
641 	if (packet[5] == 0x3f) {
642 		alps_process_trackstick_packet_v3(psmouse);
643 		return;
644 	}
645 
646 	alps_process_touchpad_packet_v3(psmouse);
647 }
648 
649 static void alps_process_packet_v4(struct psmouse *psmouse)
650 {
651 	struct alps_data *priv = psmouse->private;
652 	unsigned char *packet = psmouse->packet;
653 	struct input_dev *dev = psmouse->dev;
654 	int offset;
655 	int x, y, z;
656 	int left, right;
657 	int x1, y1, x2, y2;
658 	int fingers = 0;
659 	unsigned int x_bitmap, y_bitmap;
660 
661 	/*
662 	 * v4 has a 6-byte encoding for bitmap data, but this data is
663 	 * broken up between 3 normal packets. Use priv->multi_packet to
664 	 * track our position in the bitmap packet.
665 	 */
666 	if (packet[6] & 0x40) {
667 		/* sync, reset position */
668 		priv->multi_packet = 0;
669 	}
670 
671 	if (WARN_ON_ONCE(priv->multi_packet > 2))
672 		return;
673 
674 	offset = 2 * priv->multi_packet;
675 	priv->multi_data[offset] = packet[6];
676 	priv->multi_data[offset + 1] = packet[7];
677 
678 	if (++priv->multi_packet > 2) {
679 		priv->multi_packet = 0;
680 
681 		x_bitmap = ((priv->multi_data[2] & 0x1f) << 10) |
682 			   ((priv->multi_data[3] & 0x60) << 3) |
683 			   ((priv->multi_data[0] & 0x3f) << 2) |
684 			   ((priv->multi_data[1] & 0x60) >> 5);
685 		y_bitmap = ((priv->multi_data[5] & 0x01) << 10) |
686 			   ((priv->multi_data[3] & 0x1f) << 5) |
687 			    (priv->multi_data[1] & 0x1f);
688 
689 		fingers = alps_process_bitmap(priv, x_bitmap, y_bitmap,
690 					      &x1, &y1, &x2, &y2);
691 
692 		/* Store MT data.*/
693 		priv->fingers = fingers;
694 		priv->x1 = x1;
695 		priv->x2 = x2;
696 		priv->y1 = y1;
697 		priv->y2 = y2;
698 	}
699 
700 	left = packet[4] & 0x01;
701 	right = packet[4] & 0x02;
702 
703 	x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) |
704 	    ((packet[0] & 0x30) >> 4);
705 	y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f);
706 	z = packet[5] & 0x7f;
707 
708 	/*
709 	 * If there were no contacts in the bitmap, use ST
710 	 * points in MT reports.
711 	 * If there were two contacts or more, report MT data.
712 	 */
713 	if (priv->fingers < 2) {
714 		x1 = x;
715 		y1 = y;
716 		fingers = z > 0 ? 1 : 0;
717 	} else {
718 		fingers = priv->fingers;
719 		x1 = priv->x1;
720 		x2 = priv->x2;
721 		y1 = priv->y1;
722 		y2 = priv->y2;
723 	}
724 
725 	if (z >= 64)
726 		input_report_key(dev, BTN_TOUCH, 1);
727 	else
728 		input_report_key(dev, BTN_TOUCH, 0);
729 
730 	alps_report_semi_mt_data(dev, fingers, x1, y1, x2, y2);
731 
732 	input_mt_report_finger_count(dev, fingers);
733 
734 	input_report_key(dev, BTN_LEFT, left);
735 	input_report_key(dev, BTN_RIGHT, right);
736 
737 	if (z > 0) {
738 		input_report_abs(dev, ABS_X, x);
739 		input_report_abs(dev, ABS_Y, y);
740 	}
741 	input_report_abs(dev, ABS_PRESSURE, z);
742 
743 	input_sync(dev);
744 }
745 
746 static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
747 					unsigned char packet[],
748 					bool report_buttons)
749 {
750 	struct alps_data *priv = psmouse->private;
751 	struct input_dev *dev2 = priv->dev2;
752 
753 	if (report_buttons)
754 		alps_report_buttons(psmouse, dev2, psmouse->dev,
755 				packet[0] & 1, packet[0] & 2, packet[0] & 4);
756 
757 	input_report_rel(dev2, REL_X,
758 		packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
759 	input_report_rel(dev2, REL_Y,
760 		packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
761 
762 	input_sync(dev2);
763 }
764 
765 static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
766 {
767 	struct alps_data *priv = psmouse->private;
768 
769 	if (psmouse->pktcnt < 6)
770 		return PSMOUSE_GOOD_DATA;
771 
772 	if (psmouse->pktcnt == 6) {
773 		/*
774 		 * Start a timer to flush the packet if it ends up last
775 		 * 6-byte packet in the stream. Timer needs to fire
776 		 * psmouse core times out itself. 20 ms should be enough
777 		 * to decide if we are getting more data or not.
778 		 */
779 		mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
780 		return PSMOUSE_GOOD_DATA;
781 	}
782 
783 	del_timer(&priv->timer);
784 
785 	if (psmouse->packet[6] & 0x80) {
786 
787 		/*
788 		 * Highest bit is set - that means we either had
789 		 * complete ALPS packet and this is start of the
790 		 * next packet or we got garbage.
791 		 */
792 
793 		if (((psmouse->packet[3] |
794 		      psmouse->packet[4] |
795 		      psmouse->packet[5]) & 0x80) ||
796 		    (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) {
797 			psmouse_dbg(psmouse,
798 				    "refusing packet %4ph (suspected interleaved ps/2)\n",
799 				    psmouse->packet + 3);
800 			return PSMOUSE_BAD_DATA;
801 		}
802 
803 		priv->process_packet(psmouse);
804 
805 		/* Continue with the next packet */
806 		psmouse->packet[0] = psmouse->packet[6];
807 		psmouse->pktcnt = 1;
808 
809 	} else {
810 
811 		/*
812 		 * High bit is 0 - that means that we indeed got a PS/2
813 		 * packet in the middle of ALPS packet.
814 		 *
815 		 * There is also possibility that we got 6-byte ALPS
816 		 * packet followed  by 3-byte packet from trackpoint. We
817 		 * can not distinguish between these 2 scenarios but
818 		 * because the latter is unlikely to happen in course of
819 		 * normal operation (user would need to press all
820 		 * buttons on the pad and start moving trackpoint
821 		 * without touching the pad surface) we assume former.
822 		 * Even if we are wrong the wost thing that would happen
823 		 * the cursor would jump but we should not get protocol
824 		 * de-synchronization.
825 		 */
826 
827 		alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
828 					    false);
829 
830 		/*
831 		 * Continue with the standard ALPS protocol handling,
832 		 * but make sure we won't process it as an interleaved
833 		 * packet again, which may happen if all buttons are
834 		 * pressed. To avoid this let's reset the 4th bit which
835 		 * is normally 1.
836 		 */
837 		psmouse->packet[3] = psmouse->packet[6] & 0xf7;
838 		psmouse->pktcnt = 4;
839 	}
840 
841 	return PSMOUSE_GOOD_DATA;
842 }
843 
844 static void alps_flush_packet(unsigned long data)
845 {
846 	struct psmouse *psmouse = (struct psmouse *)data;
847 	struct alps_data *priv = psmouse->private;
848 
849 	serio_pause_rx(psmouse->ps2dev.serio);
850 
851 	if (psmouse->pktcnt == psmouse->pktsize) {
852 
853 		/*
854 		 * We did not any more data in reasonable amount of time.
855 		 * Validate the last 3 bytes and process as a standard
856 		 * ALPS packet.
857 		 */
858 		if ((psmouse->packet[3] |
859 		     psmouse->packet[4] |
860 		     psmouse->packet[5]) & 0x80) {
861 			psmouse_dbg(psmouse,
862 				    "refusing packet %3ph (suspected interleaved ps/2)\n",
863 				    psmouse->packet + 3);
864 		} else {
865 			priv->process_packet(psmouse);
866 		}
867 		psmouse->pktcnt = 0;
868 	}
869 
870 	serio_continue_rx(psmouse->ps2dev.serio);
871 }
872 
873 static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
874 {
875 	struct alps_data *priv = psmouse->private;
876 
877 	if ((psmouse->packet[0] & 0xc8) == 0x08) { /* PS/2 packet */
878 		if (psmouse->pktcnt == 3) {
879 			alps_report_bare_ps2_packet(psmouse, psmouse->packet,
880 						    true);
881 			return PSMOUSE_FULL_PACKET;
882 		}
883 		return PSMOUSE_GOOD_DATA;
884 	}
885 
886 	/* Check for PS/2 packet stuffed in the middle of ALPS packet. */
887 
888 	if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
889 	    psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
890 		return alps_handle_interleaved_ps2(psmouse);
891 	}
892 
893 	if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
894 		psmouse_dbg(psmouse,
895 			    "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
896 			    psmouse->packet[0], priv->mask0, priv->byte0);
897 		return PSMOUSE_BAD_DATA;
898 	}
899 
900 	/* Bytes 2 - pktsize should have 0 in the highest bit */
901 	if (priv->proto_version != ALPS_PROTO_V5 &&
902 	    psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
903 	    (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
904 		psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
905 			    psmouse->pktcnt - 1,
906 			    psmouse->packet[psmouse->pktcnt - 1]);
907 		return PSMOUSE_BAD_DATA;
908 	}
909 
910 	if (psmouse->pktcnt == psmouse->pktsize) {
911 		priv->process_packet(psmouse);
912 		return PSMOUSE_FULL_PACKET;
913 	}
914 
915 	return PSMOUSE_GOOD_DATA;
916 }
917 
918 static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
919 {
920 	struct ps2dev *ps2dev = &psmouse->ps2dev;
921 	struct alps_data *priv = psmouse->private;
922 	int command;
923 	unsigned char *param;
924 	unsigned char dummy[4];
925 
926 	BUG_ON(nibble > 0xf);
927 
928 	command = priv->nibble_commands[nibble].command;
929 	param = (command & 0x0f00) ?
930 		dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
931 
932 	if (ps2_command(ps2dev, param, command))
933 		return -1;
934 
935 	return 0;
936 }
937 
938 static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
939 {
940 	struct ps2dev *ps2dev = &psmouse->ps2dev;
941 	struct alps_data *priv = psmouse->private;
942 	int i, nibble;
943 
944 	if (ps2_command(ps2dev, NULL, priv->addr_command))
945 		return -1;
946 
947 	for (i = 12; i >= 0; i -= 4) {
948 		nibble = (addr >> i) & 0xf;
949 		if (alps_command_mode_send_nibble(psmouse, nibble))
950 			return -1;
951 	}
952 
953 	return 0;
954 }
955 
956 static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
957 {
958 	struct ps2dev *ps2dev = &psmouse->ps2dev;
959 	unsigned char param[4];
960 
961 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
962 		return -1;
963 
964 	/*
965 	 * The address being read is returned in the first two bytes
966 	 * of the result. Check that this address matches the expected
967 	 * address.
968 	 */
969 	if (addr != ((param[0] << 8) | param[1]))
970 		return -1;
971 
972 	return param[2];
973 }
974 
975 static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
976 {
977 	if (alps_command_mode_set_addr(psmouse, addr))
978 		return -1;
979 	return __alps_command_mode_read_reg(psmouse, addr);
980 }
981 
982 static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
983 {
984 	if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
985 		return -1;
986 	if (alps_command_mode_send_nibble(psmouse, value & 0xf))
987 		return -1;
988 	return 0;
989 }
990 
991 static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
992 				       u8 value)
993 {
994 	if (alps_command_mode_set_addr(psmouse, addr))
995 		return -1;
996 	return __alps_command_mode_write_reg(psmouse, value);
997 }
998 
999 static int alps_rpt_cmd(struct psmouse *psmouse, int init_command,
1000 			int repeated_command, unsigned char *param)
1001 {
1002 	struct ps2dev *ps2dev = &psmouse->ps2dev;
1003 
1004 	param[0] = 0;
1005 	if (init_command && ps2_command(ps2dev, param, init_command))
1006 		return -EIO;
1007 
1008 	if (ps2_command(ps2dev,  NULL, repeated_command) ||
1009 	    ps2_command(ps2dev,  NULL, repeated_command) ||
1010 	    ps2_command(ps2dev,  NULL, repeated_command))
1011 		return -EIO;
1012 
1013 	param[0] = param[1] = param[2] = 0xff;
1014 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1015 		return -EIO;
1016 
1017 	psmouse_dbg(psmouse, "%2.2X report: %3ph\n",
1018 		    repeated_command, param);
1019 	return 0;
1020 }
1021 
1022 static int alps_enter_command_mode(struct psmouse *psmouse)
1023 {
1024 	unsigned char param[4];
1025 
1026 	if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) {
1027 		psmouse_err(psmouse, "failed to enter command mode\n");
1028 		return -1;
1029 	}
1030 
1031 	if ((param[0] != 0x88 || (param[1] != 0x07 && param[1] != 0x08)) &&
1032 	    param[0] != 0x73) {
1033 		psmouse_dbg(psmouse,
1034 			    "unknown response while entering command mode\n");
1035 		return -1;
1036 	}
1037 	return 0;
1038 }
1039 
1040 static inline int alps_exit_command_mode(struct psmouse *psmouse)
1041 {
1042 	struct ps2dev *ps2dev = &psmouse->ps2dev;
1043 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
1044 		return -1;
1045 	return 0;
1046 }
1047 
1048 /*
1049  * For DualPoint devices select the device that should respond to
1050  * subsequent commands. It looks like glidepad is behind stickpointer,
1051  * I'd thought it would be other way around...
1052  */
1053 static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
1054 {
1055 	struct ps2dev *ps2dev = &psmouse->ps2dev;
1056 	int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
1057 
1058 	if (ps2_command(ps2dev, NULL, cmd) ||
1059 	    ps2_command(ps2dev, NULL, cmd) ||
1060 	    ps2_command(ps2dev, NULL, cmd) ||
1061 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1062 		return -1;
1063 
1064 	/* we may get 3 more bytes, just ignore them */
1065 	ps2_drain(ps2dev, 3, 100);
1066 
1067 	return 0;
1068 }
1069 
1070 static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
1071 {
1072 	struct ps2dev *ps2dev = &psmouse->ps2dev;
1073 
1074 	/* Try ALPS magic knock - 4 disable before enable */
1075 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1076 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1077 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1078 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1079 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
1080 		return -1;
1081 
1082 	/*
1083 	 * Switch mouse to poll (remote) mode so motion data will not
1084 	 * get in our way
1085 	 */
1086 	return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
1087 }
1088 
1089 static int alps_get_status(struct psmouse *psmouse, char *param)
1090 {
1091 	/* Get status: 0xF5 0xF5 0xF5 0xE9 */
1092 	if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param))
1093 		return -1;
1094 
1095 	return 0;
1096 }
1097 
1098 /*
1099  * Turn touchpad tapping on or off. The sequences are:
1100  * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
1101  * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
1102  * My guess that 0xE9 (GetInfo) is here as a sync point.
1103  * For models that also have stickpointer (DualPoints) its tapping
1104  * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
1105  * we don't fiddle with it.
1106  */
1107 static int alps_tap_mode(struct psmouse *psmouse, int enable)
1108 {
1109 	struct ps2dev *ps2dev = &psmouse->ps2dev;
1110 	int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
1111 	unsigned char tap_arg = enable ? 0x0A : 0x00;
1112 	unsigned char param[4];
1113 
1114 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
1115 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1116 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1117 	    ps2_command(ps2dev, &tap_arg, cmd))
1118 		return -1;
1119 
1120 	if (alps_get_status(psmouse, param))
1121 		return -1;
1122 
1123 	return 0;
1124 }
1125 
1126 /*
1127  * alps_poll() - poll the touchpad for current motion packet.
1128  * Used in resync.
1129  */
1130 static int alps_poll(struct psmouse *psmouse)
1131 {
1132 	struct alps_data *priv = psmouse->private;
1133 	unsigned char buf[sizeof(psmouse->packet)];
1134 	bool poll_failed;
1135 
1136 	if (priv->flags & ALPS_PASS)
1137 		alps_passthrough_mode_v2(psmouse, true);
1138 
1139 	poll_failed = ps2_command(&psmouse->ps2dev, buf,
1140 				  PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
1141 
1142 	if (priv->flags & ALPS_PASS)
1143 		alps_passthrough_mode_v2(psmouse, false);
1144 
1145 	if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
1146 		return -1;
1147 
1148 	if ((psmouse->badbyte & 0xc8) == 0x08) {
1149 /*
1150  * Poll the track stick ...
1151  */
1152 		if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
1153 			return -1;
1154 	}
1155 
1156 	memcpy(psmouse->packet, buf, sizeof(buf));
1157 	return 0;
1158 }
1159 
1160 static int alps_hw_init_v1_v2(struct psmouse *psmouse)
1161 {
1162 	struct alps_data *priv = psmouse->private;
1163 
1164 	if ((priv->flags & ALPS_PASS) &&
1165 	    alps_passthrough_mode_v2(psmouse, true)) {
1166 		return -1;
1167 	}
1168 
1169 	if (alps_tap_mode(psmouse, true)) {
1170 		psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
1171 		return -1;
1172 	}
1173 
1174 	if (alps_absolute_mode_v1_v2(psmouse)) {
1175 		psmouse_err(psmouse, "Failed to enable absolute mode\n");
1176 		return -1;
1177 	}
1178 
1179 	if ((priv->flags & ALPS_PASS) &&
1180 	    alps_passthrough_mode_v2(psmouse, false)) {
1181 		return -1;
1182 	}
1183 
1184 	/* ALPS needs stream mode, otherwise it won't report any data */
1185 	if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
1186 		psmouse_err(psmouse, "Failed to enable stream mode\n");
1187 		return -1;
1188 	}
1189 
1190 	return 0;
1191 }
1192 
1193 /*
1194  * Enable or disable passthrough mode to the trackstick.
1195  */
1196 static int alps_passthrough_mode_v3(struct psmouse *psmouse,
1197 				    int reg_base, bool enable)
1198 {
1199 	int reg_val, ret = -1;
1200 
1201 	if (alps_enter_command_mode(psmouse))
1202 		return -1;
1203 
1204 	reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x0008);
1205 	if (reg_val == -1)
1206 		goto error;
1207 
1208 	if (enable)
1209 		reg_val |= 0x01;
1210 	else
1211 		reg_val &= ~0x01;
1212 
1213 	ret = __alps_command_mode_write_reg(psmouse, reg_val);
1214 
1215 error:
1216 	if (alps_exit_command_mode(psmouse))
1217 		ret = -1;
1218 	return ret;
1219 }
1220 
1221 /* Must be in command mode when calling this function */
1222 static int alps_absolute_mode_v3(struct psmouse *psmouse)
1223 {
1224 	int reg_val;
1225 
1226 	reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
1227 	if (reg_val == -1)
1228 		return -1;
1229 
1230 	reg_val |= 0x06;
1231 	if (__alps_command_mode_write_reg(psmouse, reg_val))
1232 		return -1;
1233 
1234 	return 0;
1235 }
1236 
1237 static int alps_probe_trackstick_v3(struct psmouse *psmouse, int reg_base)
1238 {
1239 	int ret = -EIO, reg_val;
1240 
1241 	if (alps_enter_command_mode(psmouse))
1242 		goto error;
1243 
1244 	reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x08);
1245 	if (reg_val == -1)
1246 		goto error;
1247 
1248 	/* bit 7: trackstick is present */
1249 	ret = reg_val & 0x80 ? 0 : -ENODEV;
1250 
1251 error:
1252 	alps_exit_command_mode(psmouse);
1253 	return ret;
1254 }
1255 
1256 static int alps_setup_trackstick_v3(struct psmouse *psmouse, int reg_base)
1257 {
1258 	struct ps2dev *ps2dev = &psmouse->ps2dev;
1259 	int ret = 0;
1260 	unsigned char param[4];
1261 
1262 	if (alps_passthrough_mode_v3(psmouse, reg_base, true))
1263 		return -EIO;
1264 
1265 	/*
1266 	 * E7 report for the trackstick
1267 	 *
1268 	 * There have been reports of failures to seem to trace back
1269 	 * to the above trackstick check failing. When these occur
1270 	 * this E7 report fails, so when that happens we continue
1271 	 * with the assumption that there isn't a trackstick after
1272 	 * all.
1273 	 */
1274 	if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_SETSCALE21, param)) {
1275 		psmouse_warn(psmouse, "trackstick E7 report failed\n");
1276 		ret = -ENODEV;
1277 	} else {
1278 		psmouse_dbg(psmouse, "trackstick E7 report: %3ph\n", param);
1279 
1280 		/*
1281 		 * Not sure what this does, but it is absolutely
1282 		 * essential. Without it, the touchpad does not
1283 		 * work at all and the trackstick just emits normal
1284 		 * PS/2 packets.
1285 		 */
1286 		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1287 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1288 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1289 		    alps_command_mode_send_nibble(psmouse, 0x9) ||
1290 		    alps_command_mode_send_nibble(psmouse, 0x4)) {
1291 			psmouse_err(psmouse,
1292 				    "Error sending magic E6 sequence\n");
1293 			ret = -EIO;
1294 			goto error;
1295 		}
1296 
1297 		/*
1298 		 * This ensures the trackstick packets are in the format
1299 		 * supported by this driver. If bit 1 isn't set the packet
1300 		 * format is different.
1301 		 */
1302 		if (alps_enter_command_mode(psmouse) ||
1303 		    alps_command_mode_write_reg(psmouse,
1304 						reg_base + 0x08, 0x82) ||
1305 		    alps_exit_command_mode(psmouse))
1306 			ret = -EIO;
1307 	}
1308 
1309 error:
1310 	if (alps_passthrough_mode_v3(psmouse, reg_base, false))
1311 		ret = -EIO;
1312 
1313 	return ret;
1314 }
1315 
1316 static int alps_hw_init_v3(struct psmouse *psmouse)
1317 {
1318 	struct ps2dev *ps2dev = &psmouse->ps2dev;
1319 	int reg_val;
1320 	unsigned char param[4];
1321 
1322 	reg_val = alps_probe_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE);
1323 	if (reg_val == -EIO)
1324 		goto error;
1325 
1326 	if (reg_val == 0 &&
1327 	    alps_setup_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE) == -EIO)
1328 		goto error;
1329 
1330 	if (alps_enter_command_mode(psmouse) ||
1331 	    alps_absolute_mode_v3(psmouse)) {
1332 		psmouse_err(psmouse, "Failed to enter absolute mode\n");
1333 		goto error;
1334 	}
1335 
1336 	reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
1337 	if (reg_val == -1)
1338 		goto error;
1339 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
1340 		goto error;
1341 
1342 	reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
1343 	if (reg_val == -1)
1344 		goto error;
1345 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
1346 		goto error;
1347 
1348 	if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
1349 		goto error;
1350 	if (__alps_command_mode_write_reg(psmouse, 0x04))
1351 		goto error;
1352 
1353 	if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
1354 		goto error;
1355 	if (__alps_command_mode_write_reg(psmouse, 0x03))
1356 		goto error;
1357 
1358 	if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
1359 		goto error;
1360 	if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
1361 		goto error;
1362 
1363 	if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
1364 		goto error;
1365 	if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
1366 		goto error;
1367 
1368 	alps_exit_command_mode(psmouse);
1369 
1370 	/* Set rate and enable data reporting */
1371 	param[0] = 0x64;
1372 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
1373 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1374 		psmouse_err(psmouse, "Failed to enable data reporting\n");
1375 		return -1;
1376 	}
1377 
1378 	return 0;
1379 
1380 error:
1381 	/*
1382 	 * Leaving the touchpad in command mode will essentially render
1383 	 * it unusable until the machine reboots, so exit it here just
1384 	 * to be safe
1385 	 */
1386 	alps_exit_command_mode(psmouse);
1387 	return -1;
1388 }
1389 
1390 static int alps_hw_init_rushmore_v3(struct psmouse *psmouse)
1391 {
1392 	struct alps_data *priv = psmouse->private;
1393 	struct ps2dev *ps2dev = &psmouse->ps2dev;
1394 	int reg_val, ret = -1;
1395 
1396 	if (priv->flags & ALPS_DUALPOINT) {
1397 		reg_val = alps_setup_trackstick_v3(psmouse,
1398 						   ALPS_REG_BASE_RUSHMORE);
1399 		if (reg_val == -EIO)
1400 			goto error;
1401 		if (reg_val == -ENODEV)
1402 			priv->flags &= ~ALPS_DUALPOINT;
1403 	}
1404 
1405 	if (alps_enter_command_mode(psmouse) ||
1406 	    alps_command_mode_read_reg(psmouse, 0xc2d9) == -1 ||
1407 	    alps_command_mode_write_reg(psmouse, 0xc2cb, 0x00))
1408 		goto error;
1409 
1410 	reg_val = alps_command_mode_read_reg(psmouse, 0xc2c6);
1411 	if (reg_val == -1)
1412 		goto error;
1413 	if (__alps_command_mode_write_reg(psmouse, reg_val & 0xfd))
1414 		goto error;
1415 
1416 	if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
1417 		goto error;
1418 
1419 	/* enter absolute mode */
1420 	reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
1421 	if (reg_val == -1)
1422 		goto error;
1423 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
1424 		goto error;
1425 
1426 	alps_exit_command_mode(psmouse);
1427 	return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
1428 
1429 error:
1430 	alps_exit_command_mode(psmouse);
1431 	return ret;
1432 }
1433 
1434 /* Must be in command mode when calling this function */
1435 static int alps_absolute_mode_v4(struct psmouse *psmouse)
1436 {
1437 	int reg_val;
1438 
1439 	reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
1440 	if (reg_val == -1)
1441 		return -1;
1442 
1443 	reg_val |= 0x02;
1444 	if (__alps_command_mode_write_reg(psmouse, reg_val))
1445 		return -1;
1446 
1447 	return 0;
1448 }
1449 
1450 static int alps_hw_init_v4(struct psmouse *psmouse)
1451 {
1452 	struct ps2dev *ps2dev = &psmouse->ps2dev;
1453 	unsigned char param[4];
1454 
1455 	if (alps_enter_command_mode(psmouse))
1456 		goto error;
1457 
1458 	if (alps_absolute_mode_v4(psmouse)) {
1459 		psmouse_err(psmouse, "Failed to enter absolute mode\n");
1460 		goto error;
1461 	}
1462 
1463 	if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
1464 		goto error;
1465 
1466 	if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
1467 		goto error;
1468 
1469 	if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
1470 		goto error;
1471 
1472 	if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
1473 		goto error;
1474 
1475 	if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
1476 		goto error;
1477 
1478 	if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
1479 		goto error;
1480 
1481 	if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
1482 		goto error;
1483 
1484 	if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
1485 		goto error;
1486 
1487 	alps_exit_command_mode(psmouse);
1488 
1489 	/*
1490 	 * This sequence changes the output from a 9-byte to an
1491 	 * 8-byte format. All the same data seems to be present,
1492 	 * just in a more compact format.
1493 	 */
1494 	param[0] = 0xc8;
1495 	param[1] = 0x64;
1496 	param[2] = 0x50;
1497 	if (ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
1498 	    ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE) ||
1499 	    ps2_command(ps2dev, &param[2], PSMOUSE_CMD_SETRATE) ||
1500 	    ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
1501 		return -1;
1502 
1503 	/* Set rate and enable data reporting */
1504 	param[0] = 0x64;
1505 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
1506 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1507 		psmouse_err(psmouse, "Failed to enable data reporting\n");
1508 		return -1;
1509 	}
1510 
1511 	return 0;
1512 
1513 error:
1514 	/*
1515 	 * Leaving the touchpad in command mode will essentially render
1516 	 * it unusable until the machine reboots, so exit it here just
1517 	 * to be safe
1518 	 */
1519 	alps_exit_command_mode(psmouse);
1520 	return -1;
1521 }
1522 
1523 static int alps_hw_init_dolphin_v1(struct psmouse *psmouse)
1524 {
1525 	struct ps2dev *ps2dev = &psmouse->ps2dev;
1526 	unsigned char param[2];
1527 
1528 	/* This is dolphin "v1" as empirically defined by florin9doi */
1529 	param[0] = 0x64;
1530 	param[1] = 0x28;
1531 
1532 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
1533 	    ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
1534 	    ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
1535 		return -1;
1536 
1537 	return 0;
1538 }
1539 
1540 static void alps_set_defaults(struct alps_data *priv)
1541 {
1542 	priv->byte0 = 0x8f;
1543 	priv->mask0 = 0x8f;
1544 	priv->flags = ALPS_DUALPOINT;
1545 
1546 	priv->x_max = 2000;
1547 	priv->y_max = 1400;
1548 	priv->x_bits = 15;
1549 	priv->y_bits = 11;
1550 
1551 	switch (priv->proto_version) {
1552 	case ALPS_PROTO_V1:
1553 	case ALPS_PROTO_V2:
1554 		priv->hw_init = alps_hw_init_v1_v2;
1555 		priv->process_packet = alps_process_packet_v1_v2;
1556 		priv->set_abs_params = alps_set_abs_params_st;
1557 		break;
1558 	case ALPS_PROTO_V3:
1559 		priv->hw_init = alps_hw_init_v3;
1560 		priv->process_packet = alps_process_packet_v3;
1561 		priv->set_abs_params = alps_set_abs_params_mt;
1562 		priv->decode_fields = alps_decode_pinnacle;
1563 		priv->nibble_commands = alps_v3_nibble_commands;
1564 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
1565 		break;
1566 	case ALPS_PROTO_V4:
1567 		priv->hw_init = alps_hw_init_v4;
1568 		priv->process_packet = alps_process_packet_v4;
1569 		priv->set_abs_params = alps_set_abs_params_mt;
1570 		priv->nibble_commands = alps_v4_nibble_commands;
1571 		priv->addr_command = PSMOUSE_CMD_DISABLE;
1572 		break;
1573 	case ALPS_PROTO_V5:
1574 		priv->hw_init = alps_hw_init_dolphin_v1;
1575 		priv->process_packet = alps_process_packet_v3;
1576 		priv->decode_fields = alps_decode_dolphin;
1577 		priv->set_abs_params = alps_set_abs_params_mt;
1578 		priv->nibble_commands = alps_v3_nibble_commands;
1579 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
1580 		priv->byte0 = 0xc8;
1581 		priv->mask0 = 0xc8;
1582 		priv->flags = 0;
1583 		priv->x_max = 1360;
1584 		priv->y_max = 660;
1585 		priv->x_bits = 23;
1586 		priv->y_bits = 12;
1587 		break;
1588 	}
1589 }
1590 
1591 static int alps_match_table(struct psmouse *psmouse, struct alps_data *priv,
1592 			    unsigned char *e7, unsigned char *ec)
1593 {
1594 	const struct alps_model_info *model;
1595 	int i;
1596 
1597 	for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
1598 		model = &alps_model_data[i];
1599 
1600 		if (!memcmp(e7, model->signature, sizeof(model->signature)) &&
1601 		    (!model->command_mode_resp ||
1602 		     model->command_mode_resp == ec[2])) {
1603 
1604 			priv->proto_version = model->proto_version;
1605 			alps_set_defaults(priv);
1606 
1607 			priv->flags = model->flags;
1608 			priv->byte0 = model->byte0;
1609 			priv->mask0 = model->mask0;
1610 
1611 			return 0;
1612 		}
1613 	}
1614 
1615 	return -EINVAL;
1616 }
1617 
1618 static int alps_identify(struct psmouse *psmouse, struct alps_data *priv)
1619 {
1620 	unsigned char e6[4], e7[4], ec[4];
1621 
1622 	/*
1623 	 * First try "E6 report".
1624 	 * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
1625 	 * The bits 0-2 of the first byte will be 1s if some buttons are
1626 	 * pressed.
1627 	 */
1628 	if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
1629 			 PSMOUSE_CMD_SETSCALE11, e6))
1630 		return -EIO;
1631 
1632 	if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100))
1633 		return -EINVAL;
1634 
1635 	/*
1636 	 * Now get the "E7" and "EC" reports.  These will uniquely identify
1637 	 * most ALPS touchpads.
1638 	 */
1639 	if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
1640 			 PSMOUSE_CMD_SETSCALE21, e7) ||
1641 	    alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
1642 			 PSMOUSE_CMD_RESET_WRAP, ec) ||
1643 	    alps_exit_command_mode(psmouse))
1644 		return -EIO;
1645 
1646 	if (alps_match_table(psmouse, priv, e7, ec) == 0) {
1647 		return 0;
1648 	} else if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0x50 &&
1649 		   ec[0] == 0x73 && ec[1] == 0x01) {
1650 		priv->proto_version = ALPS_PROTO_V5;
1651 		alps_set_defaults(priv);
1652 
1653 		return 0;
1654 	} else if (ec[0] == 0x88 && ec[1] == 0x08) {
1655 		priv->proto_version = ALPS_PROTO_V3;
1656 		alps_set_defaults(priv);
1657 
1658 		priv->hw_init = alps_hw_init_rushmore_v3;
1659 		priv->decode_fields = alps_decode_rushmore;
1660 		priv->x_bits = 16;
1661 		priv->y_bits = 12;
1662 
1663 		/* hack to make addr_command, nibble_command available */
1664 		psmouse->private = priv;
1665 
1666 		if (alps_probe_trackstick_v3(psmouse, ALPS_REG_BASE_RUSHMORE))
1667 			priv->flags &= ~ALPS_DUALPOINT;
1668 
1669 		return 0;
1670 	} else if (ec[0] == 0x88 && ec[1] == 0x07 &&
1671 		   ec[2] >= 0x90 && ec[2] <= 0x9d) {
1672 		priv->proto_version = ALPS_PROTO_V3;
1673 		alps_set_defaults(priv);
1674 
1675 		return 0;
1676 	}
1677 
1678 	psmouse_info(psmouse,
1679 		     "Unknown ALPS touchpad: E7=%3ph, EC=%3ph\n", e7, ec);
1680 
1681 	return -EINVAL;
1682 }
1683 
1684 static int alps_reconnect(struct psmouse *psmouse)
1685 {
1686 	struct alps_data *priv = psmouse->private;
1687 
1688 	psmouse_reset(psmouse);
1689 
1690 	if (alps_identify(psmouse, priv) < 0)
1691 		return -1;
1692 
1693 	return priv->hw_init(psmouse);
1694 }
1695 
1696 static void alps_disconnect(struct psmouse *psmouse)
1697 {
1698 	struct alps_data *priv = psmouse->private;
1699 
1700 	psmouse_reset(psmouse);
1701 	del_timer_sync(&priv->timer);
1702 	input_unregister_device(priv->dev2);
1703 	kfree(priv);
1704 }
1705 
1706 static void alps_set_abs_params_st(struct alps_data *priv,
1707 				   struct input_dev *dev1)
1708 {
1709 	input_set_abs_params(dev1, ABS_X, 0, 1023, 0, 0);
1710 	input_set_abs_params(dev1, ABS_Y, 0, 767, 0, 0);
1711 }
1712 
1713 static void alps_set_abs_params_mt(struct alps_data *priv,
1714 				   struct input_dev *dev1)
1715 {
1716 	set_bit(INPUT_PROP_SEMI_MT, dev1->propbit);
1717 	input_mt_init_slots(dev1, 2, 0);
1718 	input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
1719 	input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
1720 
1721 	set_bit(BTN_TOOL_DOUBLETAP, dev1->keybit);
1722 	set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
1723 	set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
1724 
1725 	input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
1726 	input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
1727 }
1728 
1729 int alps_init(struct psmouse *psmouse)
1730 {
1731 	struct alps_data *priv;
1732 	struct input_dev *dev1 = psmouse->dev, *dev2;
1733 
1734 	priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
1735 	dev2 = input_allocate_device();
1736 	if (!priv || !dev2)
1737 		goto init_fail;
1738 
1739 	priv->dev2 = dev2;
1740 	setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
1741 
1742 	psmouse->private = priv;
1743 
1744 	psmouse_reset(psmouse);
1745 
1746 	if (alps_identify(psmouse, priv) < 0)
1747 		goto init_fail;
1748 
1749 	if (priv->hw_init(psmouse))
1750 		goto init_fail;
1751 
1752 	/*
1753 	 * Undo part of setup done for us by psmouse core since touchpad
1754 	 * is not a relative device.
1755 	 */
1756 	__clear_bit(EV_REL, dev1->evbit);
1757 	__clear_bit(REL_X, dev1->relbit);
1758 	__clear_bit(REL_Y, dev1->relbit);
1759 
1760 	/*
1761 	 * Now set up our capabilities.
1762 	 */
1763 	dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
1764 	dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
1765 	dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
1766 	dev1->keybit[BIT_WORD(BTN_LEFT)] |=
1767 		BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
1768 
1769 	dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
1770 
1771 	priv->set_abs_params(priv, dev1);
1772 	input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
1773 
1774 	if (priv->flags & ALPS_WHEEL) {
1775 		dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
1776 		dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
1777 	}
1778 
1779 	if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
1780 		dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
1781 		dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
1782 	}
1783 
1784 	if (priv->flags & ALPS_FOUR_BUTTONS) {
1785 		dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
1786 		dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
1787 		dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
1788 		dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
1789 	} else {
1790 		dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
1791 	}
1792 
1793 	snprintf(priv->phys, sizeof(priv->phys), "%s/input1", psmouse->ps2dev.serio->phys);
1794 	dev2->phys = priv->phys;
1795 	dev2->name = (priv->flags & ALPS_DUALPOINT) ?
1796 		     "DualPoint Stick" : "PS/2 Mouse";
1797 	dev2->id.bustype = BUS_I8042;
1798 	dev2->id.vendor  = 0x0002;
1799 	dev2->id.product = PSMOUSE_ALPS;
1800 	dev2->id.version = 0x0000;
1801 	dev2->dev.parent = &psmouse->ps2dev.serio->dev;
1802 
1803 	dev2->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
1804 	dev2->relbit[BIT_WORD(REL_X)] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
1805 	dev2->keybit[BIT_WORD(BTN_LEFT)] =
1806 		BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_MIDDLE) | BIT_MASK(BTN_RIGHT);
1807 
1808 	if (input_register_device(priv->dev2))
1809 		goto init_fail;
1810 
1811 	psmouse->protocol_handler = alps_process_byte;
1812 	psmouse->poll = alps_poll;
1813 	psmouse->disconnect = alps_disconnect;
1814 	psmouse->reconnect = alps_reconnect;
1815 	psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
1816 
1817 	/* We are having trouble resyncing ALPS touchpads so disable it for now */
1818 	psmouse->resync_time = 0;
1819 
1820 	return 0;
1821 
1822 init_fail:
1823 	psmouse_reset(psmouse);
1824 	input_free_device(dev2);
1825 	kfree(priv);
1826 	psmouse->private = NULL;
1827 	return -1;
1828 }
1829 
1830 int alps_detect(struct psmouse *psmouse, bool set_properties)
1831 {
1832 	struct alps_data dummy;
1833 
1834 	if (alps_identify(psmouse, &dummy) < 0)
1835 		return -1;
1836 
1837 	if (set_properties) {
1838 		psmouse->vendor = "ALPS";
1839 		psmouse->name = dummy.flags & ALPS_DUALPOINT ?
1840 				"DualPoint TouchPad" : "GlidePoint";
1841 		psmouse->model = dummy.proto_version << 8;
1842 	}
1843 	return 0;
1844 }
1845 
1846