xref: /linux/drivers/input/mouse/alps.c (revision 2ba9268dd603d23e17643437b2246acb6844953b)
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 static const struct alps_nibble_commands alps_v6_nibble_commands[] = {
74 	{ PSMOUSE_CMD_ENABLE,		0x00 }, /* 0 */
75 	{ PSMOUSE_CMD_SETRATE,		0x0a }, /* 1 */
76 	{ PSMOUSE_CMD_SETRATE,		0x14 }, /* 2 */
77 	{ PSMOUSE_CMD_SETRATE,		0x28 }, /* 3 */
78 	{ PSMOUSE_CMD_SETRATE,		0x3c }, /* 4 */
79 	{ PSMOUSE_CMD_SETRATE,		0x50 }, /* 5 */
80 	{ PSMOUSE_CMD_SETRATE,		0x64 }, /* 6 */
81 	{ PSMOUSE_CMD_SETRATE,		0xc8 }, /* 7 */
82 	{ PSMOUSE_CMD_GETID,		0x00 }, /* 8 */
83 	{ PSMOUSE_CMD_GETINFO,		0x00 }, /* 9 */
84 	{ PSMOUSE_CMD_SETRES,		0x00 }, /* a */
85 	{ PSMOUSE_CMD_SETRES,		0x01 }, /* b */
86 	{ PSMOUSE_CMD_SETRES,		0x02 }, /* c */
87 	{ PSMOUSE_CMD_SETRES,		0x03 }, /* d */
88 	{ PSMOUSE_CMD_SETSCALE21,	0x00 }, /* e */
89 	{ PSMOUSE_CMD_SETSCALE11,	0x00 }, /* f */
90 };
91 
92 
93 #define ALPS_DUALPOINT		0x02	/* touchpad has trackstick */
94 #define ALPS_PASS		0x04	/* device has a pass-through port */
95 
96 #define ALPS_WHEEL		0x08	/* hardware wheel present */
97 #define ALPS_FW_BK_1		0x10	/* front & back buttons present */
98 #define ALPS_FW_BK_2		0x20	/* front & back buttons present */
99 #define ALPS_FOUR_BUTTONS	0x40	/* 4 direction button present */
100 #define ALPS_PS2_INTERLEAVED	0x80	/* 3-byte PS/2 packet interleaved with
101 					   6-byte ALPS packet */
102 #define ALPS_BUTTONPAD		0x200	/* device is a clickpad */
103 
104 static const struct alps_model_info alps_model_data[] = {
105 	{ { 0x32, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },	/* Toshiba Salellite Pro M10 */
106 	{ { 0x33, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V1, 0x88, 0xf8, 0 } },				/* UMAX-530T */
107 	{ { 0x53, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
108 	{ { 0x53, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
109 	{ { 0x60, 0x03, 0xc8 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },				/* HP ze1115 */
110 	{ { 0x63, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
111 	{ { 0x63, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
112 	{ { 0x63, 0x02, 0x28 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 } },		/* Fujitsu Siemens S6010 */
113 	{ { 0x63, 0x02, 0x3c }, 0x00, { ALPS_PROTO_V2, 0x8f, 0x8f, ALPS_WHEEL } },		/* Toshiba Satellite S2400-103 */
114 	{ { 0x63, 0x02, 0x50 }, 0x00, { ALPS_PROTO_V2, 0xef, 0xef, ALPS_FW_BK_1 } },		/* NEC Versa L320 */
115 	{ { 0x63, 0x02, 0x64 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
116 	{ { 0x63, 0x03, 0xc8 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },	/* Dell Latitude D800 */
117 	{ { 0x73, 0x00, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_DUALPOINT } },		/* ThinkPad R61 8918-5QG */
118 	{ { 0x73, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
119 	{ { 0x73, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 } },		/* Ahtec Laptop */
120 
121 	/*
122 	 * XXX This entry is suspicious. First byte has zero lower nibble,
123 	 * which is what a normal mouse would report. Also, the value 0x0e
124 	 * isn't valid per PS/2 spec.
125 	 */
126 	{ { 0x20, 0x02, 0x0e }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },
127 
128 	{ { 0x22, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },
129 	{ { 0x22, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT } },	/* Dell Latitude D600 */
130 	/* Dell Latitude E5500, E6400, E6500, Precision M4400 */
131 	{ { 0x62, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xcf, 0xcf,
132 		ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED } },
133 	{ { 0x73, 0x00, 0x14 }, 0x00, { ALPS_PROTO_V6, 0xff, 0xff, ALPS_DUALPOINT } },		/* Dell XT2 */
134 	{ { 0x73, 0x02, 0x50 }, 0x00, { ALPS_PROTO_V2, 0xcf, 0xcf, ALPS_FOUR_BUTTONS } },	/* Dell Vostro 1400 */
135 	{ { 0x52, 0x01, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xff, 0xff,
136 		ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED } },				/* Toshiba Tecra A11-11L */
137 	{ { 0x73, 0x02, 0x64 }, 0x8a, { ALPS_PROTO_V4, 0x8f, 0x8f, 0 } },
138 };
139 
140 static const struct alps_protocol_info alps_v3_protocol_data = {
141 	ALPS_PROTO_V3, 0x8f, 0x8f, ALPS_DUALPOINT
142 };
143 
144 static const struct alps_protocol_info alps_v3_rushmore_data = {
145 	ALPS_PROTO_V3_RUSHMORE, 0x8f, 0x8f, ALPS_DUALPOINT
146 };
147 
148 static const struct alps_protocol_info alps_v5_protocol_data = {
149 	ALPS_PROTO_V5, 0xc8, 0xd8, 0
150 };
151 
152 static const struct alps_protocol_info alps_v7_protocol_data = {
153 	ALPS_PROTO_V7, 0x48, 0x48, ALPS_DUALPOINT
154 };
155 
156 static void alps_set_abs_params_st(struct alps_data *priv,
157 				   struct input_dev *dev1);
158 static void alps_set_abs_params_mt(struct alps_data *priv,
159 				   struct input_dev *dev1);
160 
161 /* Packet formats are described in Documentation/input/alps.txt */
162 
163 static bool alps_is_valid_first_byte(struct alps_data *priv,
164 				     unsigned char data)
165 {
166 	return (data & priv->mask0) == priv->byte0;
167 }
168 
169 static void alps_report_buttons(struct input_dev *dev1, struct input_dev *dev2,
170 				int left, int right, int middle)
171 {
172 	struct input_dev *dev;
173 
174 	/*
175 	 * If shared button has already been reported on the
176 	 * other device (dev2) then this event should be also
177 	 * sent through that device.
178 	 */
179 	dev = (dev2 && test_bit(BTN_LEFT, dev2->key)) ? dev2 : dev1;
180 	input_report_key(dev, BTN_LEFT, left);
181 
182 	dev = (dev2 && test_bit(BTN_RIGHT, dev2->key)) ? dev2 : dev1;
183 	input_report_key(dev, BTN_RIGHT, right);
184 
185 	dev = (dev2 && test_bit(BTN_MIDDLE, dev2->key)) ? dev2 : dev1;
186 	input_report_key(dev, BTN_MIDDLE, middle);
187 
188 	/*
189 	 * Sync the _other_ device now, we'll do the first
190 	 * device later once we report the rest of the events.
191 	 */
192 	if (dev2)
193 		input_sync(dev2);
194 }
195 
196 static void alps_process_packet_v1_v2(struct psmouse *psmouse)
197 {
198 	struct alps_data *priv = psmouse->private;
199 	unsigned char *packet = psmouse->packet;
200 	struct input_dev *dev = psmouse->dev;
201 	struct input_dev *dev2 = priv->dev2;
202 	int x, y, z, ges, fin, left, right, middle;
203 	int back = 0, forward = 0;
204 
205 	if (priv->proto_version == ALPS_PROTO_V1) {
206 		left = packet[2] & 0x10;
207 		right = packet[2] & 0x08;
208 		middle = 0;
209 		x = packet[1] | ((packet[0] & 0x07) << 7);
210 		y = packet[4] | ((packet[3] & 0x07) << 7);
211 		z = packet[5];
212 	} else {
213 		left = packet[3] & 1;
214 		right = packet[3] & 2;
215 		middle = packet[3] & 4;
216 		x = packet[1] | ((packet[2] & 0x78) << (7 - 3));
217 		y = packet[4] | ((packet[3] & 0x70) << (7 - 4));
218 		z = packet[5];
219 	}
220 
221 	if (priv->flags & ALPS_FW_BK_1) {
222 		back = packet[0] & 0x10;
223 		forward = packet[2] & 4;
224 	}
225 
226 	if (priv->flags & ALPS_FW_BK_2) {
227 		back = packet[3] & 4;
228 		forward = packet[2] & 4;
229 		if ((middle = forward && back))
230 			forward = back = 0;
231 	}
232 
233 	ges = packet[2] & 1;
234 	fin = packet[2] & 2;
235 
236 	if ((priv->flags & ALPS_DUALPOINT) && z == 127) {
237 		input_report_rel(dev2, REL_X,  (x > 383 ? (x - 768) : x));
238 		input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y));
239 
240 		alps_report_buttons(dev2, dev, left, right, middle);
241 
242 		input_sync(dev2);
243 		return;
244 	}
245 
246 	alps_report_buttons(dev, dev2, left, right, middle);
247 
248 	/* Convert hardware tap to a reasonable Z value */
249 	if (ges && !fin)
250 		z = 40;
251 
252 	/*
253 	 * A "tap and drag" operation is reported by the hardware as a transition
254 	 * from (!fin && ges) to (fin && ges). This should be translated to the
255 	 * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
256 	 */
257 	if (ges && fin && !priv->prev_fin) {
258 		input_report_abs(dev, ABS_X, x);
259 		input_report_abs(dev, ABS_Y, y);
260 		input_report_abs(dev, ABS_PRESSURE, 0);
261 		input_report_key(dev, BTN_TOOL_FINGER, 0);
262 		input_sync(dev);
263 	}
264 	priv->prev_fin = fin;
265 
266 	if (z > 30)
267 		input_report_key(dev, BTN_TOUCH, 1);
268 	if (z < 25)
269 		input_report_key(dev, BTN_TOUCH, 0);
270 
271 	if (z > 0) {
272 		input_report_abs(dev, ABS_X, x);
273 		input_report_abs(dev, ABS_Y, y);
274 	}
275 
276 	input_report_abs(dev, ABS_PRESSURE, z);
277 	input_report_key(dev, BTN_TOOL_FINGER, z > 0);
278 
279 	if (priv->flags & ALPS_WHEEL)
280 		input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
281 
282 	if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
283 		input_report_key(dev, BTN_FORWARD, forward);
284 		input_report_key(dev, BTN_BACK, back);
285 	}
286 
287 	if (priv->flags & ALPS_FOUR_BUTTONS) {
288 		input_report_key(dev, BTN_0, packet[2] & 4);
289 		input_report_key(dev, BTN_1, packet[0] & 0x10);
290 		input_report_key(dev, BTN_2, packet[3] & 4);
291 		input_report_key(dev, BTN_3, packet[0] & 0x20);
292 	}
293 
294 	input_sync(dev);
295 }
296 
297 /*
298  * Process bitmap data for V5 protocols. Return value is null.
299  *
300  * The bitmaps don't have enough data to track fingers, so this function
301  * only generates points representing a bounding box of at most two contacts.
302  * These two points are returned in fields->mt.
303  */
304 static void alps_process_bitmap_dolphin(struct alps_data *priv,
305 					struct alps_fields *fields)
306 {
307 	int box_middle_x, box_middle_y;
308 	unsigned int x_map, y_map;
309 	unsigned char start_bit, end_bit;
310 	unsigned char x_msb, x_lsb, y_msb, y_lsb;
311 
312 	x_map = fields->x_map;
313 	y_map = fields->y_map;
314 
315 	if (!x_map || !y_map)
316 		return;
317 
318 	/* Get Most-significant and Least-significant bit */
319 	x_msb = fls(x_map);
320 	x_lsb = ffs(x_map);
321 	y_msb = fls(y_map);
322 	y_lsb = ffs(y_map);
323 
324 	/* Most-significant bit should never exceed max sensor line number */
325 	if (x_msb > priv->x_bits || y_msb > priv->y_bits)
326 		return;
327 
328 	if (fields->fingers > 1) {
329 		start_bit = priv->x_bits - x_msb;
330 		end_bit = priv->x_bits - x_lsb;
331 		box_middle_x = (priv->x_max * (start_bit + end_bit)) /
332 				(2 * (priv->x_bits - 1));
333 
334 		start_bit = y_lsb - 1;
335 		end_bit = y_msb - 1;
336 		box_middle_y = (priv->y_max * (start_bit + end_bit)) /
337 				(2 * (priv->y_bits - 1));
338 		fields->mt[0] = fields->st;
339 		fields->mt[1].x = 2 * box_middle_x - fields->mt[0].x;
340 		fields->mt[1].y = 2 * box_middle_y - fields->mt[0].y;
341 	}
342 }
343 
344 static void alps_get_bitmap_points(unsigned int map,
345 				   struct alps_bitmap_point *low,
346 				   struct alps_bitmap_point *high,
347 				   int *fingers)
348 {
349 	struct alps_bitmap_point *point;
350 	int i, bit, prev_bit = 0;
351 
352 	point = low;
353 	for (i = 0; map != 0; i++, map >>= 1) {
354 		bit = map & 1;
355 		if (bit) {
356 			if (!prev_bit) {
357 				point->start_bit = i;
358 				point->num_bits = 0;
359 				(*fingers)++;
360 			}
361 			point->num_bits++;
362 		} else {
363 			if (prev_bit)
364 				point = high;
365 		}
366 		prev_bit = bit;
367 	}
368 }
369 
370 /*
371  * Process bitmap data from v3 and v4 protocols. Returns the number of
372  * fingers detected. A return value of 0 means at least one of the
373  * bitmaps was empty.
374  *
375  * The bitmaps don't have enough data to track fingers, so this function
376  * only generates points representing a bounding box of all contacts.
377  * These points are returned in fields->mt when the return value
378  * is greater than 0.
379  */
380 static int alps_process_bitmap(struct alps_data *priv,
381 			       struct alps_fields *fields)
382 {
383 	int i, fingers_x = 0, fingers_y = 0, fingers;
384 	struct alps_bitmap_point x_low = {0,}, x_high = {0,};
385 	struct alps_bitmap_point y_low = {0,}, y_high = {0,};
386 
387 	if (!fields->x_map || !fields->y_map)
388 		return 0;
389 
390 	alps_get_bitmap_points(fields->x_map, &x_low, &x_high, &fingers_x);
391 	alps_get_bitmap_points(fields->y_map, &y_low, &y_high, &fingers_y);
392 
393 	/*
394 	 * Fingers can overlap, so we use the maximum count of fingers
395 	 * on either axis as the finger count.
396 	 */
397 	fingers = max(fingers_x, fingers_y);
398 
399 	/*
400 	 * If an axis reports only a single contact, we have overlapping or
401 	 * adjacent fingers. Divide the single contact between the two points.
402 	 */
403 	if (fingers_x == 1) {
404 		i = (x_low.num_bits - 1) / 2;
405 		x_low.num_bits = x_low.num_bits - i;
406 		x_high.start_bit = x_low.start_bit + i;
407 		x_high.num_bits = max(i, 1);
408 	}
409 	if (fingers_y == 1) {
410 		i = (y_low.num_bits - 1) / 2;
411 		y_low.num_bits = y_low.num_bits - i;
412 		y_high.start_bit = y_low.start_bit + i;
413 		y_high.num_bits = max(i, 1);
414 	}
415 
416 	fields->mt[0].x =
417 		(priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
418 		(2 * (priv->x_bits - 1));
419 	fields->mt[0].y =
420 		(priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
421 		(2 * (priv->y_bits - 1));
422 
423 	fields->mt[1].x =
424 		(priv->x_max * (2 * x_high.start_bit + x_high.num_bits - 1)) /
425 		(2 * (priv->x_bits - 1));
426 	fields->mt[1].y =
427 		(priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) /
428 		(2 * (priv->y_bits - 1));
429 
430 	/* y-bitmap order is reversed, except on rushmore */
431 	if (priv->proto_version != ALPS_PROTO_V3_RUSHMORE) {
432 		fields->mt[0].y = priv->y_max - fields->mt[0].y;
433 		fields->mt[1].y = priv->y_max - fields->mt[1].y;
434 	}
435 
436 	return fingers;
437 }
438 
439 static void alps_set_slot(struct input_dev *dev, int slot, int x, int y)
440 {
441 	input_mt_slot(dev, slot);
442 	input_mt_report_slot_state(dev, MT_TOOL_FINGER, true);
443 	input_report_abs(dev, ABS_MT_POSITION_X, x);
444 	input_report_abs(dev, ABS_MT_POSITION_Y, y);
445 }
446 
447 static void alps_report_mt_data(struct psmouse *psmouse, int n)
448 {
449 	struct alps_data *priv = psmouse->private;
450 	struct input_dev *dev = psmouse->dev;
451 	struct alps_fields *f = &priv->f;
452 	int i, slot[MAX_TOUCHES];
453 
454 	input_mt_assign_slots(dev, slot, f->mt, n, 0);
455 	for (i = 0; i < n; i++)
456 		alps_set_slot(dev, slot[i], f->mt[i].x, f->mt[i].y);
457 
458 	input_mt_sync_frame(dev);
459 }
460 
461 static void alps_report_semi_mt_data(struct psmouse *psmouse, int fingers)
462 {
463 	struct alps_data *priv = psmouse->private;
464 	struct input_dev *dev = psmouse->dev;
465 	struct alps_fields *f = &priv->f;
466 
467 	/* Use st data when we don't have mt data */
468 	if (fingers < 2) {
469 		f->mt[0].x = f->st.x;
470 		f->mt[0].y = f->st.y;
471 		fingers = f->pressure > 0 ? 1 : 0;
472 	}
473 
474 	alps_report_mt_data(psmouse, (fingers <= 2) ? fingers : 2);
475 
476 	input_mt_report_finger_count(dev, fingers);
477 
478 	input_report_key(dev, BTN_LEFT, f->left);
479 	input_report_key(dev, BTN_RIGHT, f->right);
480 	input_report_key(dev, BTN_MIDDLE, f->middle);
481 
482 	input_report_abs(dev, ABS_PRESSURE, f->pressure);
483 
484 	input_sync(dev);
485 }
486 
487 static void alps_process_trackstick_packet_v3(struct psmouse *psmouse)
488 {
489 	struct alps_data *priv = psmouse->private;
490 	unsigned char *packet = psmouse->packet;
491 	struct input_dev *dev = priv->dev2;
492 	int x, y, z, left, right, middle;
493 
494 	/* It should be a DualPoint when received trackstick packet */
495 	if (!(priv->flags & ALPS_DUALPOINT)) {
496 		psmouse_warn(psmouse,
497 			     "Rejected trackstick packet from non DualPoint device");
498 		return;
499 	}
500 
501 	/* Sanity check packet */
502 	if (!(packet[0] & 0x40)) {
503 		psmouse_dbg(psmouse, "Bad trackstick packet, discarding\n");
504 		return;
505 	}
506 
507 	/*
508 	 * There's a special packet that seems to indicate the end
509 	 * of a stream of trackstick data. Filter these out.
510 	 */
511 	if (packet[1] == 0x7f && packet[2] == 0x7f && packet[4] == 0x7f)
512 		return;
513 
514 	x = (s8)(((packet[0] & 0x20) << 2) | (packet[1] & 0x7f));
515 	y = (s8)(((packet[0] & 0x10) << 3) | (packet[2] & 0x7f));
516 	z = (packet[4] & 0x7c) >> 2;
517 
518 	/*
519 	 * The x and y values tend to be quite large, and when used
520 	 * alone the trackstick is difficult to use. Scale them down
521 	 * to compensate.
522 	 */
523 	x /= 8;
524 	y /= 8;
525 
526 	input_report_rel(dev, REL_X, x);
527 	input_report_rel(dev, REL_Y, -y);
528 
529 	/*
530 	 * Most ALPS models report the trackstick buttons in the touchpad
531 	 * packets, but a few report them here. No reliable way has been
532 	 * found to differentiate between the models upfront, so we enable
533 	 * the quirk in response to seeing a button press in the trackstick
534 	 * packet.
535 	 */
536 	left = packet[3] & 0x01;
537 	right = packet[3] & 0x02;
538 	middle = packet[3] & 0x04;
539 
540 	if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) &&
541 	    (left || right || middle))
542 		priv->quirks |= ALPS_QUIRK_TRACKSTICK_BUTTONS;
543 
544 	if (priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) {
545 		input_report_key(dev, BTN_LEFT, left);
546 		input_report_key(dev, BTN_RIGHT, right);
547 		input_report_key(dev, BTN_MIDDLE, middle);
548 	}
549 
550 	input_sync(dev);
551 	return;
552 }
553 
554 static void alps_decode_buttons_v3(struct alps_fields *f, unsigned char *p)
555 {
556 	f->left = !!(p[3] & 0x01);
557 	f->right = !!(p[3] & 0x02);
558 	f->middle = !!(p[3] & 0x04);
559 
560 	f->ts_left = !!(p[3] & 0x10);
561 	f->ts_right = !!(p[3] & 0x20);
562 	f->ts_middle = !!(p[3] & 0x40);
563 }
564 
565 static int alps_decode_pinnacle(struct alps_fields *f, unsigned char *p,
566 				 struct psmouse *psmouse)
567 {
568 	f->first_mp = !!(p[4] & 0x40);
569 	f->is_mp = !!(p[0] & 0x40);
570 
571 	f->fingers = (p[5] & 0x3) + 1;
572 	f->x_map = ((p[4] & 0x7e) << 8) |
573 		   ((p[1] & 0x7f) << 2) |
574 		   ((p[0] & 0x30) >> 4);
575 	f->y_map = ((p[3] & 0x70) << 4) |
576 		   ((p[2] & 0x7f) << 1) |
577 		   (p[4] & 0x01);
578 
579 	f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
580 	       ((p[0] & 0x30) >> 4);
581 	f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
582 	f->pressure = p[5] & 0x7f;
583 
584 	alps_decode_buttons_v3(f, p);
585 
586 	return 0;
587 }
588 
589 static int alps_decode_rushmore(struct alps_fields *f, unsigned char *p,
590 				 struct psmouse *psmouse)
591 {
592 	alps_decode_pinnacle(f, p, psmouse);
593 
594 	/* Rushmore's packet decode has a bit difference with Pinnacle's */
595 	f->is_mp = !!(p[5] & 0x40);
596 	f->fingers = max((p[5] & 0x3), ((p[5] >> 2) & 0x3)) + 1;
597 	f->x_map |= (p[5] & 0x10) << 11;
598 	f->y_map |= (p[5] & 0x20) << 6;
599 
600 	return 0;
601 }
602 
603 static int alps_decode_dolphin(struct alps_fields *f, unsigned char *p,
604 				struct psmouse *psmouse)
605 {
606 	u64 palm_data = 0;
607 	struct alps_data *priv = psmouse->private;
608 
609 	f->first_mp = !!(p[0] & 0x02);
610 	f->is_mp = !!(p[0] & 0x20);
611 
612 	if (!f->is_mp) {
613 		f->st.x = ((p[1] & 0x7f) | ((p[4] & 0x0f) << 7));
614 		f->st.y = ((p[2] & 0x7f) | ((p[4] & 0xf0) << 3));
615 		f->pressure = (p[0] & 4) ? 0 : p[5] & 0x7f;
616 		alps_decode_buttons_v3(f, p);
617 	} else {
618 		f->fingers = ((p[0] & 0x6) >> 1 |
619 		     (p[0] & 0x10) >> 2);
620 
621 		palm_data = (p[1] & 0x7f) |
622 			    ((p[2] & 0x7f) << 7) |
623 			    ((p[4] & 0x7f) << 14) |
624 			    ((p[5] & 0x7f) << 21) |
625 			    ((p[3] & 0x07) << 28) |
626 			    (((u64)p[3] & 0x70) << 27) |
627 			    (((u64)p[0] & 0x01) << 34);
628 
629 		/* Y-profile is stored in P(0) to p(n-1), n = y_bits; */
630 		f->y_map = palm_data & (BIT(priv->y_bits) - 1);
631 
632 		/* X-profile is stored in p(n) to p(n+m-1), m = x_bits; */
633 		f->x_map = (palm_data >> priv->y_bits) &
634 			   (BIT(priv->x_bits) - 1);
635 	}
636 
637 	return 0;
638 }
639 
640 static void alps_process_touchpad_packet_v3_v5(struct psmouse *psmouse)
641 {
642 	struct alps_data *priv = psmouse->private;
643 	unsigned char *packet = psmouse->packet;
644 	struct input_dev *dev2 = priv->dev2;
645 	struct alps_fields *f = &priv->f;
646 	int fingers = 0;
647 
648 	memset(f, 0, sizeof(*f));
649 
650 	priv->decode_fields(f, packet, psmouse);
651 
652 	/*
653 	 * There's no single feature of touchpad position and bitmap packets
654 	 * that can be used to distinguish between them. We rely on the fact
655 	 * that a bitmap packet should always follow a position packet with
656 	 * bit 6 of packet[4] set.
657 	 */
658 	if (priv->multi_packet) {
659 		/*
660 		 * Sometimes a position packet will indicate a multi-packet
661 		 * sequence, but then what follows is another position
662 		 * packet. Check for this, and when it happens process the
663 		 * position packet as usual.
664 		 */
665 		if (f->is_mp) {
666 			fingers = f->fingers;
667 			if (priv->proto_version == ALPS_PROTO_V3 ||
668 			    priv->proto_version == ALPS_PROTO_V3_RUSHMORE) {
669 				if (alps_process_bitmap(priv, f) == 0)
670 					fingers = 0; /* Use st data */
671 
672 				/* Now process position packet */
673 				priv->decode_fields(f, priv->multi_data,
674 						    psmouse);
675 			} else {
676 				/*
677 				 * Because Dolphin uses position packet's
678 				 * coordinate data as Pt1 and uses it to
679 				 * calculate Pt2, so we need to do position
680 				 * packet decode first.
681 				 */
682 				priv->decode_fields(f, priv->multi_data,
683 						    psmouse);
684 
685 				/*
686 				 * Since Dolphin's finger number is reliable,
687 				 * there is no need to compare with bmap_fn.
688 				 */
689 				alps_process_bitmap_dolphin(priv, f);
690 			}
691 		} else {
692 			priv->multi_packet = 0;
693 		}
694 	}
695 
696 	/*
697 	 * Bit 6 of byte 0 is not usually set in position packets. The only
698 	 * times it seems to be set is in situations where the data is
699 	 * suspect anyway, e.g. a palm resting flat on the touchpad. Given
700 	 * this combined with the fact that this bit is useful for filtering
701 	 * out misidentified bitmap packets, we reject anything with this
702 	 * bit set.
703 	 */
704 	if (f->is_mp)
705 		return;
706 
707 	if (!priv->multi_packet && f->first_mp) {
708 		priv->multi_packet = 1;
709 		memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
710 		return;
711 	}
712 
713 	priv->multi_packet = 0;
714 
715 	/*
716 	 * Sometimes the hardware sends a single packet with z = 0
717 	 * in the middle of a stream. Real releases generate packets
718 	 * with x, y, and z all zero, so these seem to be flukes.
719 	 * Ignore them.
720 	 */
721 	if (f->st.x && f->st.y && !f->pressure)
722 		return;
723 
724 	alps_report_semi_mt_data(psmouse, fingers);
725 
726 	if ((priv->flags & ALPS_DUALPOINT) &&
727 	    !(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) {
728 		input_report_key(dev2, BTN_LEFT, f->ts_left);
729 		input_report_key(dev2, BTN_RIGHT, f->ts_right);
730 		input_report_key(dev2, BTN_MIDDLE, f->ts_middle);
731 		input_sync(dev2);
732 	}
733 }
734 
735 static void alps_process_packet_v3(struct psmouse *psmouse)
736 {
737 	unsigned char *packet = psmouse->packet;
738 
739 	/*
740 	 * v3 protocol packets come in three types, two representing
741 	 * touchpad data and one representing trackstick data.
742 	 * Trackstick packets seem to be distinguished by always
743 	 * having 0x3f in the last byte. This value has never been
744 	 * observed in the last byte of either of the other types
745 	 * of packets.
746 	 */
747 	if (packet[5] == 0x3f) {
748 		alps_process_trackstick_packet_v3(psmouse);
749 		return;
750 	}
751 
752 	alps_process_touchpad_packet_v3_v5(psmouse);
753 }
754 
755 static void alps_process_packet_v6(struct psmouse *psmouse)
756 {
757 	struct alps_data *priv = psmouse->private;
758 	unsigned char *packet = psmouse->packet;
759 	struct input_dev *dev = psmouse->dev;
760 	struct input_dev *dev2 = priv->dev2;
761 	int x, y, z, left, right, middle;
762 
763 	/*
764 	 * We can use Byte5 to distinguish if the packet is from Touchpad
765 	 * or Trackpoint.
766 	 * Touchpad:	0 - 0x7E
767 	 * Trackpoint:	0x7F
768 	 */
769 	if (packet[5] == 0x7F) {
770 		/* It should be a DualPoint when received Trackpoint packet */
771 		if (!(priv->flags & ALPS_DUALPOINT)) {
772 			psmouse_warn(psmouse,
773 				     "Rejected trackstick packet from non DualPoint device");
774 			return;
775 		}
776 
777 		/* Trackpoint packet */
778 		x = packet[1] | ((packet[3] & 0x20) << 2);
779 		y = packet[2] | ((packet[3] & 0x40) << 1);
780 		z = packet[4];
781 		left = packet[3] & 0x01;
782 		right = packet[3] & 0x02;
783 		middle = packet[3] & 0x04;
784 
785 		/* To prevent the cursor jump when finger lifted */
786 		if (x == 0x7F && y == 0x7F && z == 0x7F)
787 			x = y = z = 0;
788 
789 		/* Divide 4 since trackpoint's speed is too fast */
790 		input_report_rel(dev2, REL_X, (char)x / 4);
791 		input_report_rel(dev2, REL_Y, -((char)y / 4));
792 
793 		input_report_key(dev2, BTN_LEFT, left);
794 		input_report_key(dev2, BTN_RIGHT, right);
795 		input_report_key(dev2, BTN_MIDDLE, middle);
796 
797 		input_sync(dev2);
798 		return;
799 	}
800 
801 	/* Touchpad packet */
802 	x = packet[1] | ((packet[3] & 0x78) << 4);
803 	y = packet[2] | ((packet[4] & 0x78) << 4);
804 	z = packet[5];
805 	left = packet[3] & 0x01;
806 	right = packet[3] & 0x02;
807 
808 	if (z > 30)
809 		input_report_key(dev, BTN_TOUCH, 1);
810 	if (z < 25)
811 		input_report_key(dev, BTN_TOUCH, 0);
812 
813 	if (z > 0) {
814 		input_report_abs(dev, ABS_X, x);
815 		input_report_abs(dev, ABS_Y, y);
816 	}
817 
818 	input_report_abs(dev, ABS_PRESSURE, z);
819 	input_report_key(dev, BTN_TOOL_FINGER, z > 0);
820 
821 	/* v6 touchpad does not have middle button */
822 	input_report_key(dev, BTN_LEFT, left);
823 	input_report_key(dev, BTN_RIGHT, right);
824 
825 	input_sync(dev);
826 }
827 
828 static void alps_process_packet_v4(struct psmouse *psmouse)
829 {
830 	struct alps_data *priv = psmouse->private;
831 	unsigned char *packet = psmouse->packet;
832 	struct alps_fields *f = &priv->f;
833 	int offset;
834 
835 	/*
836 	 * v4 has a 6-byte encoding for bitmap data, but this data is
837 	 * broken up between 3 normal packets. Use priv->multi_packet to
838 	 * track our position in the bitmap packet.
839 	 */
840 	if (packet[6] & 0x40) {
841 		/* sync, reset position */
842 		priv->multi_packet = 0;
843 	}
844 
845 	if (WARN_ON_ONCE(priv->multi_packet > 2))
846 		return;
847 
848 	offset = 2 * priv->multi_packet;
849 	priv->multi_data[offset] = packet[6];
850 	priv->multi_data[offset + 1] = packet[7];
851 
852 	if (++priv->multi_packet > 2) {
853 		priv->multi_packet = 0;
854 
855 		f->x_map = ((priv->multi_data[2] & 0x1f) << 10) |
856 			   ((priv->multi_data[3] & 0x60) << 3) |
857 			   ((priv->multi_data[0] & 0x3f) << 2) |
858 			   ((priv->multi_data[1] & 0x60) >> 5);
859 		f->y_map = ((priv->multi_data[5] & 0x01) << 10) |
860 			   ((priv->multi_data[3] & 0x1f) << 5) |
861 			    (priv->multi_data[1] & 0x1f);
862 
863 		f->fingers = alps_process_bitmap(priv, f);
864 	}
865 
866 	f->left = !!(packet[4] & 0x01);
867 	f->right = !!(packet[4] & 0x02);
868 
869 	f->st.x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) |
870 		  ((packet[0] & 0x30) >> 4);
871 	f->st.y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f);
872 	f->pressure = packet[5] & 0x7f;
873 
874 	alps_report_semi_mt_data(psmouse, f->fingers);
875 }
876 
877 static bool alps_is_valid_package_v7(struct psmouse *psmouse)
878 {
879 	switch (psmouse->pktcnt) {
880 	case 3:
881 		return (psmouse->packet[2] & 0x40) == 0x40;
882 	case 4:
883 		return (psmouse->packet[3] & 0x48) == 0x48;
884 	case 6:
885 		return (psmouse->packet[5] & 0x40) == 0x00;
886 	}
887 	return true;
888 }
889 
890 static unsigned char alps_get_packet_id_v7(char *byte)
891 {
892 	unsigned char packet_id;
893 
894 	if (byte[4] & 0x40)
895 		packet_id = V7_PACKET_ID_TWO;
896 	else if (byte[4] & 0x01)
897 		packet_id = V7_PACKET_ID_MULTI;
898 	else if ((byte[0] & 0x10) && !(byte[4] & 0x43))
899 		packet_id = V7_PACKET_ID_NEW;
900 	else if (byte[1] == 0x00 && byte[4] == 0x00)
901 		packet_id = V7_PACKET_ID_IDLE;
902 	else
903 		packet_id = V7_PACKET_ID_UNKNOWN;
904 
905 	return packet_id;
906 }
907 
908 static void alps_get_finger_coordinate_v7(struct input_mt_pos *mt,
909 					  unsigned char *pkt,
910 					  unsigned char pkt_id)
911 {
912 	mt[0].x = ((pkt[2] & 0x80) << 4);
913 	mt[0].x |= ((pkt[2] & 0x3F) << 5);
914 	mt[0].x |= ((pkt[3] & 0x30) >> 1);
915 	mt[0].x |= (pkt[3] & 0x07);
916 	mt[0].y = (pkt[1] << 3) | (pkt[0] & 0x07);
917 
918 	mt[1].x = ((pkt[3] & 0x80) << 4);
919 	mt[1].x |= ((pkt[4] & 0x80) << 3);
920 	mt[1].x |= ((pkt[4] & 0x3F) << 4);
921 	mt[1].y = ((pkt[5] & 0x80) << 3);
922 	mt[1].y |= ((pkt[5] & 0x3F) << 4);
923 
924 	switch (pkt_id) {
925 	case V7_PACKET_ID_TWO:
926 		mt[1].x &= ~0x000F;
927 		mt[1].y |= 0x000F;
928 		break;
929 
930 	case V7_PACKET_ID_MULTI:
931 		mt[1].x &= ~0x003F;
932 		mt[1].y &= ~0x0020;
933 		mt[1].y |= ((pkt[4] & 0x02) << 4);
934 		mt[1].y |= 0x001F;
935 		break;
936 
937 	case V7_PACKET_ID_NEW:
938 		mt[1].x &= ~0x003F;
939 		mt[1].x |= (pkt[0] & 0x20);
940 		mt[1].y |= 0x000F;
941 		break;
942 	}
943 
944 	mt[0].y = 0x7FF - mt[0].y;
945 	mt[1].y = 0x7FF - mt[1].y;
946 }
947 
948 static int alps_get_mt_count(struct input_mt_pos *mt)
949 {
950 	int i, fingers = 0;
951 
952 	for (i = 0; i < MAX_TOUCHES; i++) {
953 		if (mt[i].x != 0 || mt[i].y != 0)
954 			fingers++;
955 	}
956 
957 	return fingers;
958 }
959 
960 static int alps_decode_packet_v7(struct alps_fields *f,
961 				  unsigned char *p,
962 				  struct psmouse *psmouse)
963 {
964 	struct alps_data *priv = psmouse->private;
965 	unsigned char pkt_id;
966 
967 	pkt_id = alps_get_packet_id_v7(p);
968 	if (pkt_id == V7_PACKET_ID_IDLE)
969 		return 0;
970 	if (pkt_id == V7_PACKET_ID_UNKNOWN)
971 		return -1;
972 	/*
973 	 * NEW packets are send to indicate a discontinuity in the finger
974 	 * coordinate reporting. Specifically a finger may have moved from
975 	 * slot 0 to 1 or vice versa. INPUT_MT_TRACK takes care of this for
976 	 * us.
977 	 *
978 	 * NEW packets have 3 problems:
979 	 * 1) They do not contain middle / right button info (on non clickpads)
980 	 *    this can be worked around by preserving the old button state
981 	 * 2) They do not contain an accurate fingercount, and they are
982 	 *    typically send when the number of fingers changes. We cannot use
983 	 *    the old finger count as that may mismatch with the amount of
984 	 *    touch coordinates we've available in the NEW packet
985 	 * 3) Their x data for the second touch is inaccurate leading to
986 	 *    a possible jump of the x coordinate by 16 units when the first
987 	 *    non NEW packet comes in
988 	 * Since problems 2 & 3 cannot be worked around, just ignore them.
989 	 */
990 	if (pkt_id == V7_PACKET_ID_NEW)
991 		return 1;
992 
993 	alps_get_finger_coordinate_v7(f->mt, p, pkt_id);
994 
995 	if (pkt_id == V7_PACKET_ID_TWO)
996 		f->fingers = alps_get_mt_count(f->mt);
997 	else /* pkt_id == V7_PACKET_ID_MULTI */
998 		f->fingers = 3 + (p[5] & 0x03);
999 
1000 	f->left = (p[0] & 0x80) >> 7;
1001 	if (priv->flags & ALPS_BUTTONPAD) {
1002 		if (p[0] & 0x20)
1003 			f->fingers++;
1004 		if (p[0] & 0x10)
1005 			f->fingers++;
1006 	} else {
1007 		f->right = (p[0] & 0x20) >> 5;
1008 		f->middle = (p[0] & 0x10) >> 4;
1009 	}
1010 
1011 	/* Sometimes a single touch is reported in mt[1] rather then mt[0] */
1012 	if (f->fingers == 1 && f->mt[0].x == 0 && f->mt[0].y == 0) {
1013 		f->mt[0].x = f->mt[1].x;
1014 		f->mt[0].y = f->mt[1].y;
1015 		f->mt[1].x = 0;
1016 		f->mt[1].y = 0;
1017 	}
1018 
1019 	return 0;
1020 }
1021 
1022 static void alps_process_trackstick_packet_v7(struct psmouse *psmouse)
1023 {
1024 	struct alps_data *priv = psmouse->private;
1025 	unsigned char *packet = psmouse->packet;
1026 	struct input_dev *dev2 = priv->dev2;
1027 	int x, y, z, left, right, middle;
1028 
1029 	/* It should be a DualPoint when received trackstick packet */
1030 	if (!(priv->flags & ALPS_DUALPOINT)) {
1031 		psmouse_warn(psmouse,
1032 			     "Rejected trackstick packet from non DualPoint device");
1033 		return;
1034 	}
1035 
1036 	x = ((packet[2] & 0xbf)) | ((packet[3] & 0x10) << 2);
1037 	y = (packet[3] & 0x07) | (packet[4] & 0xb8) |
1038 	    ((packet[3] & 0x20) << 1);
1039 	z = (packet[5] & 0x3f) | ((packet[3] & 0x80) >> 1);
1040 
1041 	left = (packet[1] & 0x01);
1042 	right = (packet[1] & 0x02) >> 1;
1043 	middle = (packet[1] & 0x04) >> 2;
1044 
1045 	/* Divide 2 since trackpoint's speed is too fast */
1046 	input_report_rel(dev2, REL_X, (char)x / 2);
1047 	input_report_rel(dev2, REL_Y, -((char)y / 2));
1048 
1049 	input_report_key(dev2, BTN_LEFT, left);
1050 	input_report_key(dev2, BTN_RIGHT, right);
1051 	input_report_key(dev2, BTN_MIDDLE, middle);
1052 
1053 	input_sync(dev2);
1054 }
1055 
1056 static void alps_process_touchpad_packet_v7(struct psmouse *psmouse)
1057 {
1058 	struct alps_data *priv = psmouse->private;
1059 	struct input_dev *dev = psmouse->dev;
1060 	struct alps_fields *f = &priv->f;
1061 
1062 	memset(f, 0, sizeof(*f));
1063 
1064 	if (priv->decode_fields(f, psmouse->packet, psmouse))
1065 		return;
1066 
1067 	alps_report_mt_data(psmouse, alps_get_mt_count(f->mt));
1068 
1069 	input_mt_report_finger_count(dev, f->fingers);
1070 
1071 	input_report_key(dev, BTN_LEFT, f->left);
1072 	input_report_key(dev, BTN_RIGHT, f->right);
1073 	input_report_key(dev, BTN_MIDDLE, f->middle);
1074 
1075 	input_sync(dev);
1076 }
1077 
1078 static void alps_process_packet_v7(struct psmouse *psmouse)
1079 {
1080 	unsigned char *packet = psmouse->packet;
1081 
1082 	if (packet[0] == 0x48 && (packet[4] & 0x47) == 0x06)
1083 		alps_process_trackstick_packet_v7(psmouse);
1084 	else
1085 		alps_process_touchpad_packet_v7(psmouse);
1086 }
1087 
1088 static DEFINE_MUTEX(alps_mutex);
1089 
1090 static void alps_register_bare_ps2_mouse(struct work_struct *work)
1091 {
1092 	struct alps_data *priv =
1093 		container_of(work, struct alps_data, dev3_register_work.work);
1094 	struct psmouse *psmouse = priv->psmouse;
1095 	struct input_dev *dev3;
1096 	int error = 0;
1097 
1098 	mutex_lock(&alps_mutex);
1099 
1100 	if (priv->dev3)
1101 		goto out;
1102 
1103 	dev3 = input_allocate_device();
1104 	if (!dev3) {
1105 		psmouse_err(psmouse, "failed to allocate secondary device\n");
1106 		error = -ENOMEM;
1107 		goto out;
1108 	}
1109 
1110 	snprintf(priv->phys3, sizeof(priv->phys3), "%s/%s",
1111 		 psmouse->ps2dev.serio->phys,
1112 		 (priv->dev2 ? "input2" : "input1"));
1113 	dev3->phys = priv->phys3;
1114 
1115 	/*
1116 	 * format of input device name is: "protocol vendor name"
1117 	 * see function psmouse_switch_protocol() in psmouse-base.c
1118 	 */
1119 	dev3->name = "PS/2 ALPS Mouse";
1120 
1121 	dev3->id.bustype = BUS_I8042;
1122 	dev3->id.vendor  = 0x0002;
1123 	dev3->id.product = PSMOUSE_PS2;
1124 	dev3->id.version = 0x0000;
1125 	dev3->dev.parent = &psmouse->ps2dev.serio->dev;
1126 
1127 	input_set_capability(dev3, EV_REL, REL_X);
1128 	input_set_capability(dev3, EV_REL, REL_Y);
1129 	input_set_capability(dev3, EV_KEY, BTN_LEFT);
1130 	input_set_capability(dev3, EV_KEY, BTN_RIGHT);
1131 	input_set_capability(dev3, EV_KEY, BTN_MIDDLE);
1132 
1133 	__set_bit(INPUT_PROP_POINTER, dev3->propbit);
1134 
1135 	error = input_register_device(dev3);
1136 	if (error) {
1137 		psmouse_err(psmouse,
1138 			    "failed to register secondary device: %d\n",
1139 			    error);
1140 		input_free_device(dev3);
1141 		goto out;
1142 	}
1143 
1144 	priv->dev3 = dev3;
1145 
1146 out:
1147 	/*
1148 	 * Save the error code so that we can detect that we
1149 	 * already tried to create the device.
1150 	 */
1151 	if (error)
1152 		priv->dev3 = ERR_PTR(error);
1153 
1154 	mutex_unlock(&alps_mutex);
1155 }
1156 
1157 static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
1158 					unsigned char packet[],
1159 					bool report_buttons)
1160 {
1161 	struct alps_data *priv = psmouse->private;
1162 	struct input_dev *dev;
1163 
1164 	/* Figure out which device to use to report the bare packet */
1165 	if (priv->proto_version == ALPS_PROTO_V2 &&
1166 	    (priv->flags & ALPS_DUALPOINT)) {
1167 		/* On V2 devices the DualPoint Stick reports bare packets */
1168 		dev = priv->dev2;
1169 	} else if (unlikely(IS_ERR_OR_NULL(priv->dev3))) {
1170 		/* Register dev3 mouse if we received PS/2 packet first time */
1171 		if (!IS_ERR(priv->dev3))
1172 			psmouse_queue_work(psmouse, &priv->dev3_register_work,
1173 					   0);
1174 		return;
1175 	} else {
1176 		dev = priv->dev3;
1177 	}
1178 
1179 	if (report_buttons)
1180 		alps_report_buttons(dev, NULL,
1181 				packet[0] & 1, packet[0] & 2, packet[0] & 4);
1182 
1183 	input_report_rel(dev, REL_X,
1184 		packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
1185 	input_report_rel(dev, REL_Y,
1186 		packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
1187 
1188 	input_sync(dev);
1189 }
1190 
1191 static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
1192 {
1193 	struct alps_data *priv = psmouse->private;
1194 
1195 	if (psmouse->pktcnt < 6)
1196 		return PSMOUSE_GOOD_DATA;
1197 
1198 	if (psmouse->pktcnt == 6) {
1199 		/*
1200 		 * Start a timer to flush the packet if it ends up last
1201 		 * 6-byte packet in the stream. Timer needs to fire
1202 		 * psmouse core times out itself. 20 ms should be enough
1203 		 * to decide if we are getting more data or not.
1204 		 */
1205 		mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
1206 		return PSMOUSE_GOOD_DATA;
1207 	}
1208 
1209 	del_timer(&priv->timer);
1210 
1211 	if (psmouse->packet[6] & 0x80) {
1212 
1213 		/*
1214 		 * Highest bit is set - that means we either had
1215 		 * complete ALPS packet and this is start of the
1216 		 * next packet or we got garbage.
1217 		 */
1218 
1219 		if (((psmouse->packet[3] |
1220 		      psmouse->packet[4] |
1221 		      psmouse->packet[5]) & 0x80) ||
1222 		    (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) {
1223 			psmouse_dbg(psmouse,
1224 				    "refusing packet %4ph (suspected interleaved ps/2)\n",
1225 				    psmouse->packet + 3);
1226 			return PSMOUSE_BAD_DATA;
1227 		}
1228 
1229 		priv->process_packet(psmouse);
1230 
1231 		/* Continue with the next packet */
1232 		psmouse->packet[0] = psmouse->packet[6];
1233 		psmouse->pktcnt = 1;
1234 
1235 	} else {
1236 
1237 		/*
1238 		 * High bit is 0 - that means that we indeed got a PS/2
1239 		 * packet in the middle of ALPS packet.
1240 		 *
1241 		 * There is also possibility that we got 6-byte ALPS
1242 		 * packet followed  by 3-byte packet from trackpoint. We
1243 		 * can not distinguish between these 2 scenarios but
1244 		 * because the latter is unlikely to happen in course of
1245 		 * normal operation (user would need to press all
1246 		 * buttons on the pad and start moving trackpoint
1247 		 * without touching the pad surface) we assume former.
1248 		 * Even if we are wrong the wost thing that would happen
1249 		 * the cursor would jump but we should not get protocol
1250 		 * de-synchronization.
1251 		 */
1252 
1253 		alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
1254 					    false);
1255 
1256 		/*
1257 		 * Continue with the standard ALPS protocol handling,
1258 		 * but make sure we won't process it as an interleaved
1259 		 * packet again, which may happen if all buttons are
1260 		 * pressed. To avoid this let's reset the 4th bit which
1261 		 * is normally 1.
1262 		 */
1263 		psmouse->packet[3] = psmouse->packet[6] & 0xf7;
1264 		psmouse->pktcnt = 4;
1265 	}
1266 
1267 	return PSMOUSE_GOOD_DATA;
1268 }
1269 
1270 static void alps_flush_packet(unsigned long data)
1271 {
1272 	struct psmouse *psmouse = (struct psmouse *)data;
1273 	struct alps_data *priv = psmouse->private;
1274 
1275 	serio_pause_rx(psmouse->ps2dev.serio);
1276 
1277 	if (psmouse->pktcnt == psmouse->pktsize) {
1278 
1279 		/*
1280 		 * We did not any more data in reasonable amount of time.
1281 		 * Validate the last 3 bytes and process as a standard
1282 		 * ALPS packet.
1283 		 */
1284 		if ((psmouse->packet[3] |
1285 		     psmouse->packet[4] |
1286 		     psmouse->packet[5]) & 0x80) {
1287 			psmouse_dbg(psmouse,
1288 				    "refusing packet %3ph (suspected interleaved ps/2)\n",
1289 				    psmouse->packet + 3);
1290 		} else {
1291 			priv->process_packet(psmouse);
1292 		}
1293 		psmouse->pktcnt = 0;
1294 	}
1295 
1296 	serio_continue_rx(psmouse->ps2dev.serio);
1297 }
1298 
1299 static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
1300 {
1301 	struct alps_data *priv = psmouse->private;
1302 
1303 	/*
1304 	 * Check if we are dealing with a bare PS/2 packet, presumably from
1305 	 * a device connected to the external PS/2 port. Because bare PS/2
1306 	 * protocol does not have enough constant bits to self-synchronize
1307 	 * properly we only do this if the device is fully synchronized.
1308 	 */
1309 	if (!psmouse->out_of_sync_cnt && (psmouse->packet[0] & 0xc8) == 0x08) {
1310 		if (psmouse->pktcnt == 3) {
1311 			alps_report_bare_ps2_packet(psmouse, psmouse->packet,
1312 						    true);
1313 			return PSMOUSE_FULL_PACKET;
1314 		}
1315 		return PSMOUSE_GOOD_DATA;
1316 	}
1317 
1318 	/* Check for PS/2 packet stuffed in the middle of ALPS packet. */
1319 
1320 	if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
1321 	    psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
1322 		return alps_handle_interleaved_ps2(psmouse);
1323 	}
1324 
1325 	if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
1326 		psmouse_dbg(psmouse,
1327 			    "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
1328 			    psmouse->packet[0], priv->mask0, priv->byte0);
1329 		return PSMOUSE_BAD_DATA;
1330 	}
1331 
1332 	/* Bytes 2 - pktsize should have 0 in the highest bit */
1333 	if (priv->proto_version < ALPS_PROTO_V5 &&
1334 	    psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
1335 	    (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
1336 		psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1337 			    psmouse->pktcnt - 1,
1338 			    psmouse->packet[psmouse->pktcnt - 1]);
1339 
1340 		if (priv->proto_version == ALPS_PROTO_V3_RUSHMORE &&
1341 		    psmouse->pktcnt == psmouse->pktsize) {
1342 			/*
1343 			 * Some Dell boxes, such as Latitude E6440 or E7440
1344 			 * with closed lid, quite often smash last byte of
1345 			 * otherwise valid packet with 0xff. Given that the
1346 			 * next packet is very likely to be valid let's
1347 			 * report PSMOUSE_FULL_PACKET but not process data,
1348 			 * rather than reporting PSMOUSE_BAD_DATA and
1349 			 * filling the logs.
1350 			 */
1351 			return PSMOUSE_FULL_PACKET;
1352 		}
1353 
1354 		return PSMOUSE_BAD_DATA;
1355 	}
1356 
1357 	if (priv->proto_version == ALPS_PROTO_V7 &&
1358 	    !alps_is_valid_package_v7(psmouse)) {
1359 		psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1360 			    psmouse->pktcnt - 1,
1361 			    psmouse->packet[psmouse->pktcnt - 1]);
1362 		return PSMOUSE_BAD_DATA;
1363 	}
1364 
1365 	if (psmouse->pktcnt == psmouse->pktsize) {
1366 		priv->process_packet(psmouse);
1367 		return PSMOUSE_FULL_PACKET;
1368 	}
1369 
1370 	return PSMOUSE_GOOD_DATA;
1371 }
1372 
1373 static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
1374 {
1375 	struct ps2dev *ps2dev = &psmouse->ps2dev;
1376 	struct alps_data *priv = psmouse->private;
1377 	int command;
1378 	unsigned char *param;
1379 	unsigned char dummy[4];
1380 
1381 	BUG_ON(nibble > 0xf);
1382 
1383 	command = priv->nibble_commands[nibble].command;
1384 	param = (command & 0x0f00) ?
1385 		dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
1386 
1387 	if (ps2_command(ps2dev, param, command))
1388 		return -1;
1389 
1390 	return 0;
1391 }
1392 
1393 static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
1394 {
1395 	struct ps2dev *ps2dev = &psmouse->ps2dev;
1396 	struct alps_data *priv = psmouse->private;
1397 	int i, nibble;
1398 
1399 	if (ps2_command(ps2dev, NULL, priv->addr_command))
1400 		return -1;
1401 
1402 	for (i = 12; i >= 0; i -= 4) {
1403 		nibble = (addr >> i) & 0xf;
1404 		if (alps_command_mode_send_nibble(psmouse, nibble))
1405 			return -1;
1406 	}
1407 
1408 	return 0;
1409 }
1410 
1411 static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1412 {
1413 	struct ps2dev *ps2dev = &psmouse->ps2dev;
1414 	unsigned char param[4];
1415 
1416 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1417 		return -1;
1418 
1419 	/*
1420 	 * The address being read is returned in the first two bytes
1421 	 * of the result. Check that this address matches the expected
1422 	 * address.
1423 	 */
1424 	if (addr != ((param[0] << 8) | param[1]))
1425 		return -1;
1426 
1427 	return param[2];
1428 }
1429 
1430 static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1431 {
1432 	if (alps_command_mode_set_addr(psmouse, addr))
1433 		return -1;
1434 	return __alps_command_mode_read_reg(psmouse, addr);
1435 }
1436 
1437 static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
1438 {
1439 	if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
1440 		return -1;
1441 	if (alps_command_mode_send_nibble(psmouse, value & 0xf))
1442 		return -1;
1443 	return 0;
1444 }
1445 
1446 static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
1447 				       u8 value)
1448 {
1449 	if (alps_command_mode_set_addr(psmouse, addr))
1450 		return -1;
1451 	return __alps_command_mode_write_reg(psmouse, value);
1452 }
1453 
1454 static int alps_rpt_cmd(struct psmouse *psmouse, int init_command,
1455 			int repeated_command, unsigned char *param)
1456 {
1457 	struct ps2dev *ps2dev = &psmouse->ps2dev;
1458 
1459 	param[0] = 0;
1460 	if (init_command && ps2_command(ps2dev, param, init_command))
1461 		return -EIO;
1462 
1463 	if (ps2_command(ps2dev,  NULL, repeated_command) ||
1464 	    ps2_command(ps2dev,  NULL, repeated_command) ||
1465 	    ps2_command(ps2dev,  NULL, repeated_command))
1466 		return -EIO;
1467 
1468 	param[0] = param[1] = param[2] = 0xff;
1469 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1470 		return -EIO;
1471 
1472 	psmouse_dbg(psmouse, "%2.2X report: %3ph\n",
1473 		    repeated_command, param);
1474 	return 0;
1475 }
1476 
1477 static bool alps_check_valid_firmware_id(unsigned char id[])
1478 {
1479 	if (id[0] == 0x73)
1480 		return true;
1481 
1482 	if (id[0] == 0x88 &&
1483 	    (id[1] == 0x07 ||
1484 	     id[1] == 0x08 ||
1485 	     (id[1] & 0xf0) == 0xb0 ||
1486 	     (id[1] & 0xf0) == 0xc0)) {
1487 		return true;
1488 	}
1489 
1490 	return false;
1491 }
1492 
1493 static int alps_enter_command_mode(struct psmouse *psmouse)
1494 {
1495 	unsigned char param[4];
1496 
1497 	if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) {
1498 		psmouse_err(psmouse, "failed to enter command mode\n");
1499 		return -1;
1500 	}
1501 
1502 	if (!alps_check_valid_firmware_id(param)) {
1503 		psmouse_dbg(psmouse,
1504 			    "unknown response while entering command mode\n");
1505 		return -1;
1506 	}
1507 	return 0;
1508 }
1509 
1510 static inline int alps_exit_command_mode(struct psmouse *psmouse)
1511 {
1512 	struct ps2dev *ps2dev = &psmouse->ps2dev;
1513 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
1514 		return -1;
1515 	return 0;
1516 }
1517 
1518 /*
1519  * For DualPoint devices select the device that should respond to
1520  * subsequent commands. It looks like glidepad is behind stickpointer,
1521  * I'd thought it would be other way around...
1522  */
1523 static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
1524 {
1525 	struct ps2dev *ps2dev = &psmouse->ps2dev;
1526 	int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
1527 
1528 	if (ps2_command(ps2dev, NULL, cmd) ||
1529 	    ps2_command(ps2dev, NULL, cmd) ||
1530 	    ps2_command(ps2dev, NULL, cmd) ||
1531 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1532 		return -1;
1533 
1534 	/* we may get 3 more bytes, just ignore them */
1535 	ps2_drain(ps2dev, 3, 100);
1536 
1537 	return 0;
1538 }
1539 
1540 static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
1541 {
1542 	struct ps2dev *ps2dev = &psmouse->ps2dev;
1543 
1544 	/* Try ALPS magic knock - 4 disable before enable */
1545 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1546 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1547 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1548 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1549 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
1550 		return -1;
1551 
1552 	/*
1553 	 * Switch mouse to poll (remote) mode so motion data will not
1554 	 * get in our way
1555 	 */
1556 	return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
1557 }
1558 
1559 static int alps_monitor_mode_send_word(struct psmouse *psmouse, u16 word)
1560 {
1561 	int i, nibble;
1562 
1563 	/*
1564 	 * b0-b11 are valid bits, send sequence is inverse.
1565 	 * e.g. when word = 0x0123, nibble send sequence is 3, 2, 1
1566 	 */
1567 	for (i = 0; i <= 8; i += 4) {
1568 		nibble = (word >> i) & 0xf;
1569 		if (alps_command_mode_send_nibble(psmouse, nibble))
1570 			return -1;
1571 	}
1572 
1573 	return 0;
1574 }
1575 
1576 static int alps_monitor_mode_write_reg(struct psmouse *psmouse,
1577 				       u16 addr, u16 value)
1578 {
1579 	struct ps2dev *ps2dev = &psmouse->ps2dev;
1580 
1581 	/* 0x0A0 is the command to write the word */
1582 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE) ||
1583 	    alps_monitor_mode_send_word(psmouse, 0x0A0) ||
1584 	    alps_monitor_mode_send_word(psmouse, addr) ||
1585 	    alps_monitor_mode_send_word(psmouse, value) ||
1586 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1587 		return -1;
1588 
1589 	return 0;
1590 }
1591 
1592 static int alps_monitor_mode(struct psmouse *psmouse, bool enable)
1593 {
1594 	struct ps2dev *ps2dev = &psmouse->ps2dev;
1595 
1596 	if (enable) {
1597 		/* EC E9 F5 F5 E7 E6 E7 E9 to enter monitor mode */
1598 		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
1599 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO) ||
1600 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1601 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1602 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1603 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1604 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1605 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO))
1606 			return -1;
1607 	} else {
1608 		/* EC to exit monitor mode */
1609 		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP))
1610 			return -1;
1611 	}
1612 
1613 	return 0;
1614 }
1615 
1616 static int alps_absolute_mode_v6(struct psmouse *psmouse)
1617 {
1618 	u16 reg_val = 0x181;
1619 	int ret = -1;
1620 
1621 	/* enter monitor mode, to write the register */
1622 	if (alps_monitor_mode(psmouse, true))
1623 		return -1;
1624 
1625 	ret = alps_monitor_mode_write_reg(psmouse, 0x000, reg_val);
1626 
1627 	if (alps_monitor_mode(psmouse, false))
1628 		ret = -1;
1629 
1630 	return ret;
1631 }
1632 
1633 static int alps_get_status(struct psmouse *psmouse, char *param)
1634 {
1635 	/* Get status: 0xF5 0xF5 0xF5 0xE9 */
1636 	if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param))
1637 		return -1;
1638 
1639 	return 0;
1640 }
1641 
1642 /*
1643  * Turn touchpad tapping on or off. The sequences are:
1644  * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
1645  * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
1646  * My guess that 0xE9 (GetInfo) is here as a sync point.
1647  * For models that also have stickpointer (DualPoints) its tapping
1648  * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
1649  * we don't fiddle with it.
1650  */
1651 static int alps_tap_mode(struct psmouse *psmouse, int enable)
1652 {
1653 	struct ps2dev *ps2dev = &psmouse->ps2dev;
1654 	int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
1655 	unsigned char tap_arg = enable ? 0x0A : 0x00;
1656 	unsigned char param[4];
1657 
1658 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
1659 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1660 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1661 	    ps2_command(ps2dev, &tap_arg, cmd))
1662 		return -1;
1663 
1664 	if (alps_get_status(psmouse, param))
1665 		return -1;
1666 
1667 	return 0;
1668 }
1669 
1670 /*
1671  * alps_poll() - poll the touchpad for current motion packet.
1672  * Used in resync.
1673  */
1674 static int alps_poll(struct psmouse *psmouse)
1675 {
1676 	struct alps_data *priv = psmouse->private;
1677 	unsigned char buf[sizeof(psmouse->packet)];
1678 	bool poll_failed;
1679 
1680 	if (priv->flags & ALPS_PASS)
1681 		alps_passthrough_mode_v2(psmouse, true);
1682 
1683 	poll_failed = ps2_command(&psmouse->ps2dev, buf,
1684 				  PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
1685 
1686 	if (priv->flags & ALPS_PASS)
1687 		alps_passthrough_mode_v2(psmouse, false);
1688 
1689 	if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
1690 		return -1;
1691 
1692 	if ((psmouse->badbyte & 0xc8) == 0x08) {
1693 /*
1694  * Poll the track stick ...
1695  */
1696 		if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
1697 			return -1;
1698 	}
1699 
1700 	memcpy(psmouse->packet, buf, sizeof(buf));
1701 	return 0;
1702 }
1703 
1704 static int alps_hw_init_v1_v2(struct psmouse *psmouse)
1705 {
1706 	struct alps_data *priv = psmouse->private;
1707 
1708 	if ((priv->flags & ALPS_PASS) &&
1709 	    alps_passthrough_mode_v2(psmouse, true)) {
1710 		return -1;
1711 	}
1712 
1713 	if (alps_tap_mode(psmouse, true)) {
1714 		psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
1715 		return -1;
1716 	}
1717 
1718 	if (alps_absolute_mode_v1_v2(psmouse)) {
1719 		psmouse_err(psmouse, "Failed to enable absolute mode\n");
1720 		return -1;
1721 	}
1722 
1723 	if ((priv->flags & ALPS_PASS) &&
1724 	    alps_passthrough_mode_v2(psmouse, false)) {
1725 		return -1;
1726 	}
1727 
1728 	/* ALPS needs stream mode, otherwise it won't report any data */
1729 	if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
1730 		psmouse_err(psmouse, "Failed to enable stream mode\n");
1731 		return -1;
1732 	}
1733 
1734 	return 0;
1735 }
1736 
1737 static int alps_hw_init_v6(struct psmouse *psmouse)
1738 {
1739 	unsigned char param[2] = {0xC8, 0x14};
1740 
1741 	/* Enter passthrough mode to let trackpoint enter 6byte raw mode */
1742 	if (alps_passthrough_mode_v2(psmouse, true))
1743 		return -1;
1744 
1745 	if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1746 	    ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1747 	    ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1748 	    ps2_command(&psmouse->ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
1749 	    ps2_command(&psmouse->ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
1750 		return -1;
1751 
1752 	if (alps_passthrough_mode_v2(psmouse, false))
1753 		return -1;
1754 
1755 	if (alps_absolute_mode_v6(psmouse)) {
1756 		psmouse_err(psmouse, "Failed to enable absolute mode\n");
1757 		return -1;
1758 	}
1759 
1760 	return 0;
1761 }
1762 
1763 /*
1764  * Enable or disable passthrough mode to the trackstick.
1765  */
1766 static int alps_passthrough_mode_v3(struct psmouse *psmouse,
1767 				    int reg_base, bool enable)
1768 {
1769 	int reg_val, ret = -1;
1770 
1771 	if (alps_enter_command_mode(psmouse))
1772 		return -1;
1773 
1774 	reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x0008);
1775 	if (reg_val == -1)
1776 		goto error;
1777 
1778 	if (enable)
1779 		reg_val |= 0x01;
1780 	else
1781 		reg_val &= ~0x01;
1782 
1783 	ret = __alps_command_mode_write_reg(psmouse, reg_val);
1784 
1785 error:
1786 	if (alps_exit_command_mode(psmouse))
1787 		ret = -1;
1788 	return ret;
1789 }
1790 
1791 /* Must be in command mode when calling this function */
1792 static int alps_absolute_mode_v3(struct psmouse *psmouse)
1793 {
1794 	int reg_val;
1795 
1796 	reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
1797 	if (reg_val == -1)
1798 		return -1;
1799 
1800 	reg_val |= 0x06;
1801 	if (__alps_command_mode_write_reg(psmouse, reg_val))
1802 		return -1;
1803 
1804 	return 0;
1805 }
1806 
1807 static int alps_probe_trackstick_v3(struct psmouse *psmouse, int reg_base)
1808 {
1809 	int ret = -EIO, reg_val;
1810 
1811 	if (alps_enter_command_mode(psmouse))
1812 		goto error;
1813 
1814 	reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x08);
1815 	if (reg_val == -1)
1816 		goto error;
1817 
1818 	/* bit 7: trackstick is present */
1819 	ret = reg_val & 0x80 ? 0 : -ENODEV;
1820 
1821 error:
1822 	alps_exit_command_mode(psmouse);
1823 	return ret;
1824 }
1825 
1826 static int alps_setup_trackstick_v3(struct psmouse *psmouse, int reg_base)
1827 {
1828 	struct ps2dev *ps2dev = &psmouse->ps2dev;
1829 	int ret = 0;
1830 	unsigned char param[4];
1831 
1832 	if (alps_passthrough_mode_v3(psmouse, reg_base, true))
1833 		return -EIO;
1834 
1835 	/*
1836 	 * E7 report for the trackstick
1837 	 *
1838 	 * There have been reports of failures to seem to trace back
1839 	 * to the above trackstick check failing. When these occur
1840 	 * this E7 report fails, so when that happens we continue
1841 	 * with the assumption that there isn't a trackstick after
1842 	 * all.
1843 	 */
1844 	if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_SETSCALE21, param)) {
1845 		psmouse_warn(psmouse, "Failed to initialize trackstick (E7 report failed)\n");
1846 		ret = -ENODEV;
1847 	} else {
1848 		psmouse_dbg(psmouse, "trackstick E7 report: %3ph\n", param);
1849 
1850 		/*
1851 		 * Not sure what this does, but it is absolutely
1852 		 * essential. Without it, the touchpad does not
1853 		 * work at all and the trackstick just emits normal
1854 		 * PS/2 packets.
1855 		 */
1856 		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1857 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1858 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1859 		    alps_command_mode_send_nibble(psmouse, 0x9) ||
1860 		    alps_command_mode_send_nibble(psmouse, 0x4)) {
1861 			psmouse_err(psmouse,
1862 				    "Error sending magic E6 sequence\n");
1863 			ret = -EIO;
1864 			goto error;
1865 		}
1866 
1867 		/*
1868 		 * This ensures the trackstick packets are in the format
1869 		 * supported by this driver. If bit 1 isn't set the packet
1870 		 * format is different.
1871 		 */
1872 		if (alps_enter_command_mode(psmouse) ||
1873 		    alps_command_mode_write_reg(psmouse,
1874 						reg_base + 0x08, 0x82) ||
1875 		    alps_exit_command_mode(psmouse))
1876 			ret = -EIO;
1877 	}
1878 
1879 error:
1880 	if (alps_passthrough_mode_v3(psmouse, reg_base, false))
1881 		ret = -EIO;
1882 
1883 	return ret;
1884 }
1885 
1886 static int alps_hw_init_v3(struct psmouse *psmouse)
1887 {
1888 	struct ps2dev *ps2dev = &psmouse->ps2dev;
1889 	int reg_val;
1890 	unsigned char param[4];
1891 
1892 	reg_val = alps_probe_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE);
1893 	if (reg_val == -EIO)
1894 		goto error;
1895 
1896 	if (reg_val == 0 &&
1897 	    alps_setup_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE) == -EIO)
1898 		goto error;
1899 
1900 	if (alps_enter_command_mode(psmouse) ||
1901 	    alps_absolute_mode_v3(psmouse)) {
1902 		psmouse_err(psmouse, "Failed to enter absolute mode\n");
1903 		goto error;
1904 	}
1905 
1906 	reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
1907 	if (reg_val == -1)
1908 		goto error;
1909 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
1910 		goto error;
1911 
1912 	reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
1913 	if (reg_val == -1)
1914 		goto error;
1915 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
1916 		goto error;
1917 
1918 	if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
1919 		goto error;
1920 	if (__alps_command_mode_write_reg(psmouse, 0x04))
1921 		goto error;
1922 
1923 	if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
1924 		goto error;
1925 	if (__alps_command_mode_write_reg(psmouse, 0x03))
1926 		goto error;
1927 
1928 	if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
1929 		goto error;
1930 	if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
1931 		goto error;
1932 
1933 	if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
1934 		goto error;
1935 	if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
1936 		goto error;
1937 
1938 	alps_exit_command_mode(psmouse);
1939 
1940 	/* Set rate and enable data reporting */
1941 	param[0] = 0x64;
1942 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
1943 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1944 		psmouse_err(psmouse, "Failed to enable data reporting\n");
1945 		return -1;
1946 	}
1947 
1948 	return 0;
1949 
1950 error:
1951 	/*
1952 	 * Leaving the touchpad in command mode will essentially render
1953 	 * it unusable until the machine reboots, so exit it here just
1954 	 * to be safe
1955 	 */
1956 	alps_exit_command_mode(psmouse);
1957 	return -1;
1958 }
1959 
1960 static int alps_get_v3_v7_resolution(struct psmouse *psmouse, int reg_pitch)
1961 {
1962 	int reg, x_pitch, y_pitch, x_electrode, y_electrode, x_phys, y_phys;
1963 	struct alps_data *priv = psmouse->private;
1964 
1965 	reg = alps_command_mode_read_reg(psmouse, reg_pitch);
1966 	if (reg < 0)
1967 		return reg;
1968 
1969 	x_pitch = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
1970 	x_pitch = 50 + 2 * x_pitch; /* In 0.1 mm units */
1971 
1972 	y_pitch = (char)reg >> 4; /* sign extend upper 4 bits */
1973 	y_pitch = 36 + 2 * y_pitch; /* In 0.1 mm units */
1974 
1975 	reg = alps_command_mode_read_reg(psmouse, reg_pitch + 1);
1976 	if (reg < 0)
1977 		return reg;
1978 
1979 	x_electrode = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
1980 	x_electrode = 17 + x_electrode;
1981 
1982 	y_electrode = (char)reg >> 4; /* sign extend upper 4 bits */
1983 	y_electrode = 13 + y_electrode;
1984 
1985 	x_phys = x_pitch * (x_electrode - 1); /* In 0.1 mm units */
1986 	y_phys = y_pitch * (y_electrode - 1); /* In 0.1 mm units */
1987 
1988 	priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
1989 	priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
1990 
1991 	psmouse_dbg(psmouse,
1992 		    "pitch %dx%d num-electrodes %dx%d physical size %dx%d mm res %dx%d\n",
1993 		    x_pitch, y_pitch, x_electrode, y_electrode,
1994 		    x_phys / 10, y_phys / 10, priv->x_res, priv->y_res);
1995 
1996 	return 0;
1997 }
1998 
1999 static int alps_hw_init_rushmore_v3(struct psmouse *psmouse)
2000 {
2001 	struct alps_data *priv = psmouse->private;
2002 	struct ps2dev *ps2dev = &psmouse->ps2dev;
2003 	int reg_val, ret = -1;
2004 
2005 	if (priv->flags & ALPS_DUALPOINT) {
2006 		reg_val = alps_setup_trackstick_v3(psmouse,
2007 						   ALPS_REG_BASE_RUSHMORE);
2008 		if (reg_val == -EIO)
2009 			goto error;
2010 	}
2011 
2012 	if (alps_enter_command_mode(psmouse) ||
2013 	    alps_command_mode_read_reg(psmouse, 0xc2d9) == -1 ||
2014 	    alps_command_mode_write_reg(psmouse, 0xc2cb, 0x00))
2015 		goto error;
2016 
2017 	if (alps_get_v3_v7_resolution(psmouse, 0xc2da))
2018 		goto error;
2019 
2020 	reg_val = alps_command_mode_read_reg(psmouse, 0xc2c6);
2021 	if (reg_val == -1)
2022 		goto error;
2023 	if (__alps_command_mode_write_reg(psmouse, reg_val & 0xfd))
2024 		goto error;
2025 
2026 	if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
2027 		goto error;
2028 
2029 	/* enter absolute mode */
2030 	reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
2031 	if (reg_val == -1)
2032 		goto error;
2033 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
2034 		goto error;
2035 
2036 	alps_exit_command_mode(psmouse);
2037 	return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2038 
2039 error:
2040 	alps_exit_command_mode(psmouse);
2041 	return ret;
2042 }
2043 
2044 /* Must be in command mode when calling this function */
2045 static int alps_absolute_mode_v4(struct psmouse *psmouse)
2046 {
2047 	int reg_val;
2048 
2049 	reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
2050 	if (reg_val == -1)
2051 		return -1;
2052 
2053 	reg_val |= 0x02;
2054 	if (__alps_command_mode_write_reg(psmouse, reg_val))
2055 		return -1;
2056 
2057 	return 0;
2058 }
2059 
2060 static int alps_hw_init_v4(struct psmouse *psmouse)
2061 {
2062 	struct ps2dev *ps2dev = &psmouse->ps2dev;
2063 	unsigned char param[4];
2064 
2065 	if (alps_enter_command_mode(psmouse))
2066 		goto error;
2067 
2068 	if (alps_absolute_mode_v4(psmouse)) {
2069 		psmouse_err(psmouse, "Failed to enter absolute mode\n");
2070 		goto error;
2071 	}
2072 
2073 	if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
2074 		goto error;
2075 
2076 	if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
2077 		goto error;
2078 
2079 	if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
2080 		goto error;
2081 
2082 	if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
2083 		goto error;
2084 
2085 	if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
2086 		goto error;
2087 
2088 	if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
2089 		goto error;
2090 
2091 	if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
2092 		goto error;
2093 
2094 	if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
2095 		goto error;
2096 
2097 	alps_exit_command_mode(psmouse);
2098 
2099 	/*
2100 	 * This sequence changes the output from a 9-byte to an
2101 	 * 8-byte format. All the same data seems to be present,
2102 	 * just in a more compact format.
2103 	 */
2104 	param[0] = 0xc8;
2105 	param[1] = 0x64;
2106 	param[2] = 0x50;
2107 	if (ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2108 	    ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE) ||
2109 	    ps2_command(ps2dev, &param[2], PSMOUSE_CMD_SETRATE) ||
2110 	    ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
2111 		return -1;
2112 
2113 	/* Set rate and enable data reporting */
2114 	param[0] = 0x64;
2115 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
2116 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
2117 		psmouse_err(psmouse, "Failed to enable data reporting\n");
2118 		return -1;
2119 	}
2120 
2121 	return 0;
2122 
2123 error:
2124 	/*
2125 	 * Leaving the touchpad in command mode will essentially render
2126 	 * it unusable until the machine reboots, so exit it here just
2127 	 * to be safe
2128 	 */
2129 	alps_exit_command_mode(psmouse);
2130 	return -1;
2131 }
2132 
2133 static int alps_dolphin_get_device_area(struct psmouse *psmouse,
2134 					struct alps_data *priv)
2135 {
2136 	struct ps2dev *ps2dev = &psmouse->ps2dev;
2137 	unsigned char param[4] = {0};
2138 	int num_x_electrode, num_y_electrode;
2139 
2140 	if (alps_enter_command_mode(psmouse))
2141 		return -1;
2142 
2143 	param[0] = 0x0a;
2144 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
2145 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2146 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2147 	    ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2148 	    ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE))
2149 		return -1;
2150 
2151 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
2152 		return -1;
2153 
2154 	/*
2155 	 * Dolphin's sensor line number is not fixed. It can be calculated
2156 	 * by adding the device's register value with DOLPHIN_PROFILE_X/YOFFSET.
2157 	 * Further more, we can get device's x_max and y_max by multiplying
2158 	 * sensor line number with DOLPHIN_COUNT_PER_ELECTRODE.
2159 	 *
2160 	 * e.g. When we get register's sensor_x = 11 & sensor_y = 8,
2161 	 *	real sensor line number X = 11 + 8 = 19, and
2162 	 *	real sensor line number Y = 8 + 1 = 9.
2163 	 *	So, x_max = (19 - 1) * 64 = 1152, and
2164 	 *	    y_max = (9 - 1) * 64 = 512.
2165 	 */
2166 	num_x_electrode = DOLPHIN_PROFILE_XOFFSET + (param[2] & 0x0F);
2167 	num_y_electrode = DOLPHIN_PROFILE_YOFFSET + ((param[2] >> 4) & 0x0F);
2168 	priv->x_bits = num_x_electrode;
2169 	priv->y_bits = num_y_electrode;
2170 	priv->x_max = (num_x_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2171 	priv->y_max = (num_y_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2172 
2173 	if (alps_exit_command_mode(psmouse))
2174 		return -1;
2175 
2176 	return 0;
2177 }
2178 
2179 static int alps_hw_init_dolphin_v1(struct psmouse *psmouse)
2180 {
2181 	struct ps2dev *ps2dev = &psmouse->ps2dev;
2182 	unsigned char param[2];
2183 
2184 	/* This is dolphin "v1" as empirically defined by florin9doi */
2185 	param[0] = 0x64;
2186 	param[1] = 0x28;
2187 
2188 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2189 	    ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2190 	    ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
2191 		return -1;
2192 
2193 	return 0;
2194 }
2195 
2196 static int alps_hw_init_v7(struct psmouse *psmouse)
2197 {
2198 	struct ps2dev *ps2dev = &psmouse->ps2dev;
2199 	int reg_val, ret = -1;
2200 
2201 	if (alps_enter_command_mode(psmouse) ||
2202 	    alps_command_mode_read_reg(psmouse, 0xc2d9) == -1)
2203 		goto error;
2204 
2205 	if (alps_get_v3_v7_resolution(psmouse, 0xc397))
2206 		goto error;
2207 
2208 	if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
2209 		goto error;
2210 
2211 	reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
2212 	if (reg_val == -1)
2213 		goto error;
2214 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
2215 		goto error;
2216 
2217 	alps_exit_command_mode(psmouse);
2218 	return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2219 
2220 error:
2221 	alps_exit_command_mode(psmouse);
2222 	return ret;
2223 }
2224 
2225 static int alps_set_protocol(struct psmouse *psmouse,
2226 			     struct alps_data *priv,
2227 			     const struct alps_protocol_info *protocol)
2228 {
2229 	psmouse->private = priv;
2230 
2231 	setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
2232 
2233 	priv->proto_version = protocol->version;
2234 	priv->byte0 = protocol->byte0;
2235 	priv->mask0 = protocol->mask0;
2236 	priv->flags = protocol->flags;
2237 
2238 	priv->x_max = 2000;
2239 	priv->y_max = 1400;
2240 	priv->x_bits = 15;
2241 	priv->y_bits = 11;
2242 
2243 	switch (priv->proto_version) {
2244 	case ALPS_PROTO_V1:
2245 	case ALPS_PROTO_V2:
2246 		priv->hw_init = alps_hw_init_v1_v2;
2247 		priv->process_packet = alps_process_packet_v1_v2;
2248 		priv->set_abs_params = alps_set_abs_params_st;
2249 		priv->x_max = 1023;
2250 		priv->y_max = 767;
2251 		break;
2252 
2253 	case ALPS_PROTO_V3:
2254 		priv->hw_init = alps_hw_init_v3;
2255 		priv->process_packet = alps_process_packet_v3;
2256 		priv->set_abs_params = alps_set_abs_params_mt;
2257 		priv->decode_fields = alps_decode_pinnacle;
2258 		priv->nibble_commands = alps_v3_nibble_commands;
2259 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2260 		break;
2261 
2262 	case ALPS_PROTO_V3_RUSHMORE:
2263 		priv->hw_init = alps_hw_init_rushmore_v3;
2264 		priv->process_packet = alps_process_packet_v3;
2265 		priv->set_abs_params = alps_set_abs_params_mt;
2266 		priv->decode_fields = alps_decode_rushmore;
2267 		priv->nibble_commands = alps_v3_nibble_commands;
2268 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2269 		priv->x_bits = 16;
2270 		priv->y_bits = 12;
2271 
2272 		if (alps_probe_trackstick_v3(psmouse,
2273 					     ALPS_REG_BASE_RUSHMORE) < 0)
2274 			priv->flags &= ~ALPS_DUALPOINT;
2275 
2276 		break;
2277 
2278 	case ALPS_PROTO_V4:
2279 		priv->hw_init = alps_hw_init_v4;
2280 		priv->process_packet = alps_process_packet_v4;
2281 		priv->set_abs_params = alps_set_abs_params_mt;
2282 		priv->nibble_commands = alps_v4_nibble_commands;
2283 		priv->addr_command = PSMOUSE_CMD_DISABLE;
2284 		break;
2285 
2286 	case ALPS_PROTO_V5:
2287 		priv->hw_init = alps_hw_init_dolphin_v1;
2288 		priv->process_packet = alps_process_touchpad_packet_v3_v5;
2289 		priv->decode_fields = alps_decode_dolphin;
2290 		priv->set_abs_params = alps_set_abs_params_mt;
2291 		priv->nibble_commands = alps_v3_nibble_commands;
2292 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2293 		priv->x_bits = 23;
2294 		priv->y_bits = 12;
2295 
2296 		if (alps_dolphin_get_device_area(psmouse, priv))
2297 			return -EIO;
2298 
2299 		break;
2300 
2301 	case ALPS_PROTO_V6:
2302 		priv->hw_init = alps_hw_init_v6;
2303 		priv->process_packet = alps_process_packet_v6;
2304 		priv->set_abs_params = alps_set_abs_params_st;
2305 		priv->nibble_commands = alps_v6_nibble_commands;
2306 		priv->x_max = 2047;
2307 		priv->y_max = 1535;
2308 		break;
2309 
2310 	case ALPS_PROTO_V7:
2311 		priv->hw_init = alps_hw_init_v7;
2312 		priv->process_packet = alps_process_packet_v7;
2313 		priv->decode_fields = alps_decode_packet_v7;
2314 		priv->set_abs_params = alps_set_abs_params_mt;
2315 		priv->nibble_commands = alps_v3_nibble_commands;
2316 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2317 		priv->x_max = 0xfff;
2318 		priv->y_max = 0x7ff;
2319 
2320 		if (priv->fw_ver[1] != 0xba)
2321 			priv->flags |= ALPS_BUTTONPAD;
2322 
2323 		break;
2324 	}
2325 
2326 	return 0;
2327 }
2328 
2329 static const struct alps_protocol_info *alps_match_table(unsigned char *e7,
2330 							 unsigned char *ec)
2331 {
2332 	const struct alps_model_info *model;
2333 	int i;
2334 
2335 	for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
2336 		model = &alps_model_data[i];
2337 
2338 		if (!memcmp(e7, model->signature, sizeof(model->signature)) &&
2339 		    (!model->command_mode_resp ||
2340 		     model->command_mode_resp == ec[2])) {
2341 
2342 			return &model->protocol_info;
2343 		}
2344 	}
2345 
2346 	return NULL;
2347 }
2348 
2349 static int alps_identify(struct psmouse *psmouse, struct alps_data *priv)
2350 {
2351 	const struct alps_protocol_info *protocol;
2352 	unsigned char e6[4], e7[4], ec[4];
2353 	int error;
2354 
2355 	/*
2356 	 * First try "E6 report".
2357 	 * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
2358 	 * The bits 0-2 of the first byte will be 1s if some buttons are
2359 	 * pressed.
2360 	 */
2361 	if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2362 			 PSMOUSE_CMD_SETSCALE11, e6))
2363 		return -EIO;
2364 
2365 	if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100))
2366 		return -EINVAL;
2367 
2368 	/*
2369 	 * Now get the "E7" and "EC" reports.  These will uniquely identify
2370 	 * most ALPS touchpads.
2371 	 */
2372 	if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2373 			 PSMOUSE_CMD_SETSCALE21, e7) ||
2374 	    alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2375 			 PSMOUSE_CMD_RESET_WRAP, ec) ||
2376 	    alps_exit_command_mode(psmouse))
2377 		return -EIO;
2378 
2379 	protocol = alps_match_table(e7, ec);
2380 	if (!protocol) {
2381 		if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0x50 &&
2382 			   ec[0] == 0x73 && (ec[1] == 0x01 || ec[1] == 0x02)) {
2383 			protocol = &alps_v5_protocol_data;
2384 		} else if (ec[0] == 0x88 &&
2385 			   ((ec[1] & 0xf0) == 0xb0 || (ec[1] & 0xf0) == 0xc0)) {
2386 			protocol = &alps_v7_protocol_data;
2387 		} else if (ec[0] == 0x88 && ec[1] == 0x08) {
2388 			protocol = &alps_v3_rushmore_data;
2389 		} else if (ec[0] == 0x88 && ec[1] == 0x07 &&
2390 			   ec[2] >= 0x90 && ec[2] <= 0x9d) {
2391 			protocol = &alps_v3_protocol_data;
2392 		} else {
2393 			psmouse_dbg(psmouse,
2394 				    "Likely not an ALPS touchpad: E7=%3ph, EC=%3ph\n", e7, ec);
2395 			return -EINVAL;
2396 		}
2397 	}
2398 
2399 	if (priv) {
2400 		/* Save the Firmware version */
2401 		memcpy(priv->fw_ver, ec, 3);
2402 		error = alps_set_protocol(psmouse, priv, protocol);
2403 		if (error)
2404 			return error;
2405 	}
2406 
2407 	return 0;
2408 }
2409 
2410 static int alps_reconnect(struct psmouse *psmouse)
2411 {
2412 	struct alps_data *priv = psmouse->private;
2413 
2414 	psmouse_reset(psmouse);
2415 
2416 	if (alps_identify(psmouse, priv) < 0)
2417 		return -1;
2418 
2419 	return priv->hw_init(psmouse);
2420 }
2421 
2422 static void alps_disconnect(struct psmouse *psmouse)
2423 {
2424 	struct alps_data *priv = psmouse->private;
2425 
2426 	psmouse_reset(psmouse);
2427 	del_timer_sync(&priv->timer);
2428 	if (priv->dev2)
2429 		input_unregister_device(priv->dev2);
2430 	if (!IS_ERR_OR_NULL(priv->dev3))
2431 		input_unregister_device(priv->dev3);
2432 	kfree(priv);
2433 }
2434 
2435 static void alps_set_abs_params_st(struct alps_data *priv,
2436 				   struct input_dev *dev1)
2437 {
2438 	input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
2439 	input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
2440 }
2441 
2442 static void alps_set_abs_params_mt(struct alps_data *priv,
2443 				   struct input_dev *dev1)
2444 {
2445 	input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
2446 	input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
2447 
2448 	input_abs_set_res(dev1, ABS_MT_POSITION_X, priv->x_res);
2449 	input_abs_set_res(dev1, ABS_MT_POSITION_Y, priv->y_res);
2450 
2451 	input_mt_init_slots(dev1, MAX_TOUCHES, INPUT_MT_POINTER |
2452 		INPUT_MT_DROP_UNUSED | INPUT_MT_TRACK | INPUT_MT_SEMI_MT);
2453 
2454 	set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
2455 	set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
2456 
2457 	/* V7 is real multi-touch */
2458 	if (priv->proto_version == ALPS_PROTO_V7)
2459 		clear_bit(INPUT_PROP_SEMI_MT, dev1->propbit);
2460 }
2461 
2462 int alps_init(struct psmouse *psmouse)
2463 {
2464 	struct alps_data *priv = psmouse->private;
2465 	struct input_dev *dev1 = psmouse->dev;
2466 	int error;
2467 
2468 	error = priv->hw_init(psmouse);
2469 	if (error)
2470 		goto init_fail;
2471 
2472 	/*
2473 	 * Undo part of setup done for us by psmouse core since touchpad
2474 	 * is not a relative device.
2475 	 */
2476 	__clear_bit(EV_REL, dev1->evbit);
2477 	__clear_bit(REL_X, dev1->relbit);
2478 	__clear_bit(REL_Y, dev1->relbit);
2479 
2480 	/*
2481 	 * Now set up our capabilities.
2482 	 */
2483 	dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
2484 	dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
2485 	dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
2486 	dev1->keybit[BIT_WORD(BTN_LEFT)] |=
2487 		BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
2488 
2489 	dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
2490 
2491 	priv->set_abs_params(priv, dev1);
2492 	/* No pressure on V7 */
2493 	if (priv->proto_version != ALPS_PROTO_V7)
2494 		input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
2495 
2496 	if (priv->flags & ALPS_WHEEL) {
2497 		dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
2498 		dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
2499 	}
2500 
2501 	if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
2502 		dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
2503 		dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
2504 	}
2505 
2506 	if (priv->flags & ALPS_FOUR_BUTTONS) {
2507 		dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
2508 		dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
2509 		dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
2510 		dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
2511 	} else if (priv->flags & ALPS_BUTTONPAD) {
2512 		set_bit(INPUT_PROP_BUTTONPAD, dev1->propbit);
2513 		clear_bit(BTN_RIGHT, dev1->keybit);
2514 	} else {
2515 		dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
2516 	}
2517 
2518 	if (priv->flags & ALPS_DUALPOINT) {
2519 		struct input_dev *dev2;
2520 
2521 		dev2 = input_allocate_device();
2522 		if (!dev2) {
2523 			psmouse_err(psmouse,
2524 				    "failed to allocate trackstick device\n");
2525 			error = -ENOMEM;
2526 			goto init_fail;
2527 		}
2528 
2529 		snprintf(priv->phys2, sizeof(priv->phys2), "%s/input1",
2530 			 psmouse->ps2dev.serio->phys);
2531 		dev2->phys = priv->phys2;
2532 
2533 		/*
2534 		 * format of input device name is: "protocol vendor name"
2535 		 * see function psmouse_switch_protocol() in psmouse-base.c
2536 		 */
2537 		dev2->name = "AlpsPS/2 ALPS DualPoint Stick";
2538 
2539 		dev2->id.bustype = BUS_I8042;
2540 		dev2->id.vendor  = 0x0002;
2541 		dev2->id.product = PSMOUSE_ALPS;
2542 		dev2->id.version = priv->proto_version;
2543 		dev2->dev.parent = &psmouse->ps2dev.serio->dev;
2544 
2545 		input_set_capability(dev2, EV_REL, REL_X);
2546 		input_set_capability(dev2, EV_REL, REL_Y);
2547 		input_set_capability(dev2, EV_KEY, BTN_LEFT);
2548 		input_set_capability(dev2, EV_KEY, BTN_RIGHT);
2549 		input_set_capability(dev2, EV_KEY, BTN_MIDDLE);
2550 
2551 		__set_bit(INPUT_PROP_POINTER, dev2->propbit);
2552 		__set_bit(INPUT_PROP_POINTING_STICK, dev2->propbit);
2553 
2554 		error = input_register_device(dev2);
2555 		if (error) {
2556 			psmouse_err(psmouse,
2557 				    "failed to register trackstick device: %d\n",
2558 				    error);
2559 			input_free_device(dev2);
2560 			goto init_fail;
2561 		}
2562 
2563 		priv->dev2 = dev2;
2564 	}
2565 
2566 	priv->psmouse = psmouse;
2567 
2568 	INIT_DELAYED_WORK(&priv->dev3_register_work,
2569 			  alps_register_bare_ps2_mouse);
2570 
2571 	psmouse->protocol_handler = alps_process_byte;
2572 	psmouse->poll = alps_poll;
2573 	psmouse->disconnect = alps_disconnect;
2574 	psmouse->reconnect = alps_reconnect;
2575 	psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
2576 
2577 	/* We are having trouble resyncing ALPS touchpads so disable it for now */
2578 	psmouse->resync_time = 0;
2579 
2580 	/* Allow 2 invalid packets without resetting device */
2581 	psmouse->resetafter = psmouse->pktsize * 2;
2582 
2583 	return 0;
2584 
2585 init_fail:
2586 	psmouse_reset(psmouse);
2587 	/*
2588 	 * Even though we did not allocate psmouse->private we do free
2589 	 * it here.
2590 	 */
2591 	kfree(psmouse->private);
2592 	psmouse->private = NULL;
2593 	return error;
2594 }
2595 
2596 int alps_detect(struct psmouse *psmouse, bool set_properties)
2597 {
2598 	struct alps_data *priv;
2599 	int error;
2600 
2601 	error = alps_identify(psmouse, NULL);
2602 	if (error)
2603 		return error;
2604 
2605 	/*
2606 	 * Reset the device to make sure it is fully operational:
2607 	 * on some laptops, like certain Dell Latitudes, we may
2608 	 * fail to properly detect presence of trackstick if device
2609 	 * has not been reset.
2610 	 */
2611 	psmouse_reset(psmouse);
2612 
2613 	priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
2614 	if (!priv)
2615 		return -ENOMEM;
2616 
2617 	error = alps_identify(psmouse, priv);
2618 	if (error) {
2619 		kfree(priv);
2620 		return error;
2621 	}
2622 
2623 	if (set_properties) {
2624 		psmouse->vendor = "ALPS";
2625 		psmouse->name = priv->flags & ALPS_DUALPOINT ?
2626 				"DualPoint TouchPad" : "GlidePoint";
2627 		psmouse->model = priv->proto_version;
2628 	} else {
2629 		/*
2630 		 * Destroy alps_data structure we allocated earlier since
2631 		 * this was just a "trial run". Otherwise we'll keep it
2632 		 * to be used by alps_init() which has to be called if
2633 		 * we succeed and set_properties is true.
2634 		 */
2635 		kfree(priv);
2636 		psmouse->private = NULL;
2637 	}
2638 
2639 	return 0;
2640 }
2641 
2642