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