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