xref: /linux/drivers/input/mouse/alps.c (revision 3ce095c16263630dde46d6051854073edaacf3d7)
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 		/* Detect false-postive touches where x & y report max value */
945 		if (mt[1].y == 0x7ff && mt[1].x == 0xff0) {
946 			mt[1].x = 0;
947 			/* y gets set to 0 at the end of this function */
948 		}
949 		break;
950 
951 	case V7_PACKET_ID_MULTI:
952 		mt[1].x &= ~0x003F;
953 		mt[1].y &= ~0x0020;
954 		mt[1].y |= ((pkt[4] & 0x02) << 4);
955 		mt[1].y |= 0x001F;
956 		break;
957 
958 	case V7_PACKET_ID_NEW:
959 		mt[1].x &= ~0x003F;
960 		mt[1].x |= (pkt[0] & 0x20);
961 		mt[1].y |= 0x000F;
962 		break;
963 	}
964 
965 	mt[0].y = 0x7FF - mt[0].y;
966 	mt[1].y = 0x7FF - mt[1].y;
967 }
968 
969 static int alps_get_mt_count(struct input_mt_pos *mt)
970 {
971 	int i, fingers = 0;
972 
973 	for (i = 0; i < MAX_TOUCHES; i++) {
974 		if (mt[i].x != 0 || mt[i].y != 0)
975 			fingers++;
976 	}
977 
978 	return fingers;
979 }
980 
981 static int alps_decode_packet_v7(struct alps_fields *f,
982 				  unsigned char *p,
983 				  struct psmouse *psmouse)
984 {
985 	struct alps_data *priv = psmouse->private;
986 	unsigned char pkt_id;
987 
988 	pkt_id = alps_get_packet_id_v7(p);
989 	if (pkt_id == V7_PACKET_ID_IDLE)
990 		return 0;
991 	if (pkt_id == V7_PACKET_ID_UNKNOWN)
992 		return -1;
993 	/*
994 	 * NEW packets are send to indicate a discontinuity in the finger
995 	 * coordinate reporting. Specifically a finger may have moved from
996 	 * slot 0 to 1 or vice versa. INPUT_MT_TRACK takes care of this for
997 	 * us.
998 	 *
999 	 * NEW packets have 3 problems:
1000 	 * 1) They do not contain middle / right button info (on non clickpads)
1001 	 *    this can be worked around by preserving the old button state
1002 	 * 2) They do not contain an accurate fingercount, and they are
1003 	 *    typically send when the number of fingers changes. We cannot use
1004 	 *    the old finger count as that may mismatch with the amount of
1005 	 *    touch coordinates we've available in the NEW packet
1006 	 * 3) Their x data for the second touch is inaccurate leading to
1007 	 *    a possible jump of the x coordinate by 16 units when the first
1008 	 *    non NEW packet comes in
1009 	 * Since problems 2 & 3 cannot be worked around, just ignore them.
1010 	 */
1011 	if (pkt_id == V7_PACKET_ID_NEW)
1012 		return 1;
1013 
1014 	alps_get_finger_coordinate_v7(f->mt, p, pkt_id);
1015 
1016 	if (pkt_id == V7_PACKET_ID_TWO)
1017 		f->fingers = alps_get_mt_count(f->mt);
1018 	else /* pkt_id == V7_PACKET_ID_MULTI */
1019 		f->fingers = 3 + (p[5] & 0x03);
1020 
1021 	f->left = (p[0] & 0x80) >> 7;
1022 	if (priv->flags & ALPS_BUTTONPAD) {
1023 		if (p[0] & 0x20)
1024 			f->fingers++;
1025 		if (p[0] & 0x10)
1026 			f->fingers++;
1027 	} else {
1028 		f->right = (p[0] & 0x20) >> 5;
1029 		f->middle = (p[0] & 0x10) >> 4;
1030 	}
1031 
1032 	/* Sometimes a single touch is reported in mt[1] rather then mt[0] */
1033 	if (f->fingers == 1 && f->mt[0].x == 0 && f->mt[0].y == 0) {
1034 		f->mt[0].x = f->mt[1].x;
1035 		f->mt[0].y = f->mt[1].y;
1036 		f->mt[1].x = 0;
1037 		f->mt[1].y = 0;
1038 	}
1039 
1040 	return 0;
1041 }
1042 
1043 static void alps_process_trackstick_packet_v7(struct psmouse *psmouse)
1044 {
1045 	struct alps_data *priv = psmouse->private;
1046 	unsigned char *packet = psmouse->packet;
1047 	struct input_dev *dev2 = priv->dev2;
1048 	int x, y, z, left, right, middle;
1049 
1050 	/* It should be a DualPoint when received trackstick packet */
1051 	if (!(priv->flags & ALPS_DUALPOINT)) {
1052 		psmouse_warn(psmouse,
1053 			     "Rejected trackstick packet from non DualPoint device");
1054 		return;
1055 	}
1056 
1057 	x = ((packet[2] & 0xbf)) | ((packet[3] & 0x10) << 2);
1058 	y = (packet[3] & 0x07) | (packet[4] & 0xb8) |
1059 	    ((packet[3] & 0x20) << 1);
1060 	z = (packet[5] & 0x3f) | ((packet[3] & 0x80) >> 1);
1061 
1062 	left = (packet[1] & 0x01);
1063 	right = (packet[1] & 0x02) >> 1;
1064 	middle = (packet[1] & 0x04) >> 2;
1065 
1066 	input_report_rel(dev2, REL_X, (char)x);
1067 	input_report_rel(dev2, REL_Y, -((char)y));
1068 
1069 	input_report_key(dev2, BTN_LEFT, left);
1070 	input_report_key(dev2, BTN_RIGHT, right);
1071 	input_report_key(dev2, BTN_MIDDLE, middle);
1072 
1073 	input_sync(dev2);
1074 }
1075 
1076 static void alps_process_touchpad_packet_v7(struct psmouse *psmouse)
1077 {
1078 	struct alps_data *priv = psmouse->private;
1079 	struct input_dev *dev = psmouse->dev;
1080 	struct alps_fields *f = &priv->f;
1081 
1082 	memset(f, 0, sizeof(*f));
1083 
1084 	if (priv->decode_fields(f, psmouse->packet, psmouse))
1085 		return;
1086 
1087 	alps_report_mt_data(psmouse, alps_get_mt_count(f->mt));
1088 
1089 	input_mt_report_finger_count(dev, f->fingers);
1090 
1091 	input_report_key(dev, BTN_LEFT, f->left);
1092 	input_report_key(dev, BTN_RIGHT, f->right);
1093 	input_report_key(dev, BTN_MIDDLE, f->middle);
1094 
1095 	input_sync(dev);
1096 }
1097 
1098 static void alps_process_packet_v7(struct psmouse *psmouse)
1099 {
1100 	unsigned char *packet = psmouse->packet;
1101 
1102 	if (packet[0] == 0x48 && (packet[4] & 0x47) == 0x06)
1103 		alps_process_trackstick_packet_v7(psmouse);
1104 	else
1105 		alps_process_touchpad_packet_v7(psmouse);
1106 }
1107 
1108 static unsigned char alps_get_pkt_id_ss4_v2(unsigned char *byte)
1109 {
1110 	unsigned char pkt_id = SS4_PACKET_ID_IDLE;
1111 
1112 	if (byte[0] == 0x18 && byte[1] == 0x10 && byte[2] == 0x00 &&
1113 	    (byte[3] & 0x88) == 0x08 && byte[4] == 0x10 && byte[5] == 0x00) {
1114 		pkt_id = SS4_PACKET_ID_IDLE;
1115 	} else if (!(byte[3] & 0x10)) {
1116 		pkt_id = SS4_PACKET_ID_ONE;
1117 	} else if (!(byte[3] & 0x20)) {
1118 		pkt_id = SS4_PACKET_ID_TWO;
1119 	} else {
1120 		pkt_id = SS4_PACKET_ID_MULTI;
1121 	}
1122 
1123 	return pkt_id;
1124 }
1125 
1126 static int alps_decode_ss4_v2(struct alps_fields *f,
1127 			      unsigned char *p, struct psmouse *psmouse)
1128 {
1129 	struct alps_data *priv = psmouse->private;
1130 	unsigned char pkt_id;
1131 	unsigned int no_data_x, no_data_y;
1132 
1133 	pkt_id = alps_get_pkt_id_ss4_v2(p);
1134 
1135 	/* Current packet is 1Finger coordinate packet */
1136 	switch (pkt_id) {
1137 	case SS4_PACKET_ID_ONE:
1138 		f->mt[0].x = SS4_1F_X_V2(p);
1139 		f->mt[0].y = SS4_1F_Y_V2(p);
1140 		f->pressure = ((SS4_1F_Z_V2(p)) * 2) & 0x7f;
1141 		f->fingers = 1;
1142 		f->first_mp = 0;
1143 		f->is_mp = 0;
1144 		break;
1145 
1146 	case SS4_PACKET_ID_TWO:
1147 		if (priv->flags & ALPS_BUTTONPAD) {
1148 			f->mt[0].x = SS4_BTL_MF_X_V2(p, 0);
1149 			f->mt[0].y = SS4_BTL_MF_Y_V2(p, 0);
1150 			f->mt[1].x = SS4_BTL_MF_X_V2(p, 1);
1151 			f->mt[1].y = SS4_BTL_MF_Y_V2(p, 1);
1152 		} else {
1153 			f->mt[0].x = SS4_STD_MF_X_V2(p, 0);
1154 			f->mt[0].y = SS4_STD_MF_Y_V2(p, 0);
1155 			f->mt[1].x = SS4_STD_MF_X_V2(p, 1);
1156 			f->mt[1].y = SS4_STD_MF_Y_V2(p, 1);
1157 		}
1158 		f->pressure = SS4_MF_Z_V2(p, 0) ? 0x30 : 0;
1159 
1160 		if (SS4_IS_MF_CONTINUE(p)) {
1161 			f->first_mp = 1;
1162 		} else {
1163 			f->fingers = 2;
1164 			f->first_mp = 0;
1165 		}
1166 		f->is_mp = 0;
1167 
1168 		break;
1169 
1170 	case SS4_PACKET_ID_MULTI:
1171 		if (priv->flags & ALPS_BUTTONPAD) {
1172 			f->mt[2].x = SS4_BTL_MF_X_V2(p, 0);
1173 			f->mt[2].y = SS4_BTL_MF_Y_V2(p, 0);
1174 			f->mt[3].x = SS4_BTL_MF_X_V2(p, 1);
1175 			f->mt[3].y = SS4_BTL_MF_Y_V2(p, 1);
1176 			no_data_x = SS4_MFPACKET_NO_AX_BL;
1177 			no_data_y = SS4_MFPACKET_NO_AY_BL;
1178 		} else {
1179 			f->mt[2].x = SS4_STD_MF_X_V2(p, 0);
1180 			f->mt[2].y = SS4_STD_MF_Y_V2(p, 0);
1181 			f->mt[3].x = SS4_STD_MF_X_V2(p, 1);
1182 			f->mt[3].y = SS4_STD_MF_Y_V2(p, 1);
1183 			no_data_x = SS4_MFPACKET_NO_AX;
1184 			no_data_y = SS4_MFPACKET_NO_AY;
1185 		}
1186 
1187 		f->first_mp = 0;
1188 		f->is_mp = 1;
1189 
1190 		if (SS4_IS_5F_DETECTED(p)) {
1191 			f->fingers = 5;
1192 		} else if (f->mt[3].x == no_data_x &&
1193 			     f->mt[3].y == no_data_y) {
1194 			f->mt[3].x = 0;
1195 			f->mt[3].y = 0;
1196 			f->fingers = 3;
1197 		} else {
1198 			f->fingers = 4;
1199 		}
1200 		break;
1201 
1202 	case SS4_PACKET_ID_IDLE:
1203 	default:
1204 		memset(f, 0, sizeof(struct alps_fields));
1205 		break;
1206 	}
1207 
1208 	f->left = !!(SS4_BTN_V2(p) & 0x01);
1209 	if (!(priv->flags & ALPS_BUTTONPAD)) {
1210 		f->right = !!(SS4_BTN_V2(p) & 0x02);
1211 		f->middle = !!(SS4_BTN_V2(p) & 0x04);
1212 	}
1213 
1214 	return 0;
1215 }
1216 
1217 static void alps_process_packet_ss4_v2(struct psmouse *psmouse)
1218 {
1219 	struct alps_data *priv = psmouse->private;
1220 	unsigned char *packet = psmouse->packet;
1221 	struct input_dev *dev = psmouse->dev;
1222 	struct alps_fields *f = &priv->f;
1223 
1224 	memset(f, 0, sizeof(struct alps_fields));
1225 	priv->decode_fields(f, packet, psmouse);
1226 	if (priv->multi_packet) {
1227 		/*
1228 		 * Sometimes the first packet will indicate a multi-packet
1229 		 * sequence, but sometimes the next multi-packet would not
1230 		 * come. Check for this, and when it happens process the
1231 		 * position packet as usual.
1232 		 */
1233 		if (f->is_mp) {
1234 			/* Now process the 1st packet */
1235 			priv->decode_fields(f, priv->multi_data, psmouse);
1236 		} else {
1237 			priv->multi_packet = 0;
1238 		}
1239 	}
1240 
1241 	/*
1242 	 * "f.is_mp" would always be '0' after merging the 1st and 2nd packet.
1243 	 * When it is set, it means 2nd packet comes without 1st packet come.
1244 	 */
1245 	if (f->is_mp)
1246 		return;
1247 
1248 	/* Save the first packet */
1249 	if (!priv->multi_packet && f->first_mp) {
1250 		priv->multi_packet = 1;
1251 		memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
1252 		return;
1253 	}
1254 
1255 	priv->multi_packet = 0;
1256 
1257 	alps_report_mt_data(psmouse, (f->fingers <= 4) ? f->fingers : 4);
1258 
1259 	input_mt_report_finger_count(dev, f->fingers);
1260 
1261 	input_report_key(dev, BTN_LEFT, f->left);
1262 	input_report_key(dev, BTN_RIGHT, f->right);
1263 	input_report_key(dev, BTN_MIDDLE, f->middle);
1264 
1265 	input_report_abs(dev, ABS_PRESSURE, f->pressure);
1266 	input_sync(dev);
1267 }
1268 
1269 static bool alps_is_valid_package_ss4_v2(struct psmouse *psmouse)
1270 {
1271 	if (psmouse->pktcnt == 4 && ((psmouse->packet[3] & 0x08) != 0x08))
1272 		return false;
1273 	if (psmouse->pktcnt == 6 && ((psmouse->packet[5] & 0x10) != 0x0))
1274 		return false;
1275 	return true;
1276 }
1277 
1278 static DEFINE_MUTEX(alps_mutex);
1279 
1280 static void alps_register_bare_ps2_mouse(struct work_struct *work)
1281 {
1282 	struct alps_data *priv =
1283 		container_of(work, struct alps_data, dev3_register_work.work);
1284 	struct psmouse *psmouse = priv->psmouse;
1285 	struct input_dev *dev3;
1286 	int error = 0;
1287 
1288 	mutex_lock(&alps_mutex);
1289 
1290 	if (priv->dev3)
1291 		goto out;
1292 
1293 	dev3 = input_allocate_device();
1294 	if (!dev3) {
1295 		psmouse_err(psmouse, "failed to allocate secondary device\n");
1296 		error = -ENOMEM;
1297 		goto out;
1298 	}
1299 
1300 	snprintf(priv->phys3, sizeof(priv->phys3), "%s/%s",
1301 		 psmouse->ps2dev.serio->phys,
1302 		 (priv->dev2 ? "input2" : "input1"));
1303 	dev3->phys = priv->phys3;
1304 
1305 	/*
1306 	 * format of input device name is: "protocol vendor name"
1307 	 * see function psmouse_switch_protocol() in psmouse-base.c
1308 	 */
1309 	dev3->name = "PS/2 ALPS Mouse";
1310 
1311 	dev3->id.bustype = BUS_I8042;
1312 	dev3->id.vendor  = 0x0002;
1313 	dev3->id.product = PSMOUSE_PS2;
1314 	dev3->id.version = 0x0000;
1315 	dev3->dev.parent = &psmouse->ps2dev.serio->dev;
1316 
1317 	input_set_capability(dev3, EV_REL, REL_X);
1318 	input_set_capability(dev3, EV_REL, REL_Y);
1319 	input_set_capability(dev3, EV_KEY, BTN_LEFT);
1320 	input_set_capability(dev3, EV_KEY, BTN_RIGHT);
1321 	input_set_capability(dev3, EV_KEY, BTN_MIDDLE);
1322 
1323 	__set_bit(INPUT_PROP_POINTER, dev3->propbit);
1324 
1325 	error = input_register_device(dev3);
1326 	if (error) {
1327 		psmouse_err(psmouse,
1328 			    "failed to register secondary device: %d\n",
1329 			    error);
1330 		input_free_device(dev3);
1331 		goto out;
1332 	}
1333 
1334 	priv->dev3 = dev3;
1335 
1336 out:
1337 	/*
1338 	 * Save the error code so that we can detect that we
1339 	 * already tried to create the device.
1340 	 */
1341 	if (error)
1342 		priv->dev3 = ERR_PTR(error);
1343 
1344 	mutex_unlock(&alps_mutex);
1345 }
1346 
1347 static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
1348 					unsigned char packet[],
1349 					bool report_buttons)
1350 {
1351 	struct alps_data *priv = psmouse->private;
1352 	struct input_dev *dev, *dev2 = NULL;
1353 
1354 	/* Figure out which device to use to report the bare packet */
1355 	if (priv->proto_version == ALPS_PROTO_V2 &&
1356 	    (priv->flags & ALPS_DUALPOINT)) {
1357 		/* On V2 devices the DualPoint Stick reports bare packets */
1358 		dev = priv->dev2;
1359 		dev2 = psmouse->dev;
1360 	} else if (unlikely(IS_ERR_OR_NULL(priv->dev3))) {
1361 		/* Register dev3 mouse if we received PS/2 packet first time */
1362 		if (!IS_ERR(priv->dev3))
1363 			psmouse_queue_work(psmouse, &priv->dev3_register_work,
1364 					   0);
1365 		return;
1366 	} else {
1367 		dev = priv->dev3;
1368 	}
1369 
1370 	if (report_buttons)
1371 		alps_report_buttons(dev, dev2,
1372 				packet[0] & 1, packet[0] & 2, packet[0] & 4);
1373 
1374 	input_report_rel(dev, REL_X,
1375 		packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
1376 	input_report_rel(dev, REL_Y,
1377 		packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
1378 
1379 	input_sync(dev);
1380 }
1381 
1382 static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
1383 {
1384 	struct alps_data *priv = psmouse->private;
1385 
1386 	if (psmouse->pktcnt < 6)
1387 		return PSMOUSE_GOOD_DATA;
1388 
1389 	if (psmouse->pktcnt == 6) {
1390 		/*
1391 		 * Start a timer to flush the packet if it ends up last
1392 		 * 6-byte packet in the stream. Timer needs to fire
1393 		 * psmouse core times out itself. 20 ms should be enough
1394 		 * to decide if we are getting more data or not.
1395 		 */
1396 		mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
1397 		return PSMOUSE_GOOD_DATA;
1398 	}
1399 
1400 	del_timer(&priv->timer);
1401 
1402 	if (psmouse->packet[6] & 0x80) {
1403 
1404 		/*
1405 		 * Highest bit is set - that means we either had
1406 		 * complete ALPS packet and this is start of the
1407 		 * next packet or we got garbage.
1408 		 */
1409 
1410 		if (((psmouse->packet[3] |
1411 		      psmouse->packet[4] |
1412 		      psmouse->packet[5]) & 0x80) ||
1413 		    (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) {
1414 			psmouse_dbg(psmouse,
1415 				    "refusing packet %4ph (suspected interleaved ps/2)\n",
1416 				    psmouse->packet + 3);
1417 			return PSMOUSE_BAD_DATA;
1418 		}
1419 
1420 		priv->process_packet(psmouse);
1421 
1422 		/* Continue with the next packet */
1423 		psmouse->packet[0] = psmouse->packet[6];
1424 		psmouse->pktcnt = 1;
1425 
1426 	} else {
1427 
1428 		/*
1429 		 * High bit is 0 - that means that we indeed got a PS/2
1430 		 * packet in the middle of ALPS packet.
1431 		 *
1432 		 * There is also possibility that we got 6-byte ALPS
1433 		 * packet followed  by 3-byte packet from trackpoint. We
1434 		 * can not distinguish between these 2 scenarios but
1435 		 * because the latter is unlikely to happen in course of
1436 		 * normal operation (user would need to press all
1437 		 * buttons on the pad and start moving trackpoint
1438 		 * without touching the pad surface) we assume former.
1439 		 * Even if we are wrong the wost thing that would happen
1440 		 * the cursor would jump but we should not get protocol
1441 		 * de-synchronization.
1442 		 */
1443 
1444 		alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
1445 					    false);
1446 
1447 		/*
1448 		 * Continue with the standard ALPS protocol handling,
1449 		 * but make sure we won't process it as an interleaved
1450 		 * packet again, which may happen if all buttons are
1451 		 * pressed. To avoid this let's reset the 4th bit which
1452 		 * is normally 1.
1453 		 */
1454 		psmouse->packet[3] = psmouse->packet[6] & 0xf7;
1455 		psmouse->pktcnt = 4;
1456 	}
1457 
1458 	return PSMOUSE_GOOD_DATA;
1459 }
1460 
1461 static void alps_flush_packet(unsigned long data)
1462 {
1463 	struct psmouse *psmouse = (struct psmouse *)data;
1464 	struct alps_data *priv = psmouse->private;
1465 
1466 	serio_pause_rx(psmouse->ps2dev.serio);
1467 
1468 	if (psmouse->pktcnt == psmouse->pktsize) {
1469 
1470 		/*
1471 		 * We did not any more data in reasonable amount of time.
1472 		 * Validate the last 3 bytes and process as a standard
1473 		 * ALPS packet.
1474 		 */
1475 		if ((psmouse->packet[3] |
1476 		     psmouse->packet[4] |
1477 		     psmouse->packet[5]) & 0x80) {
1478 			psmouse_dbg(psmouse,
1479 				    "refusing packet %3ph (suspected interleaved ps/2)\n",
1480 				    psmouse->packet + 3);
1481 		} else {
1482 			priv->process_packet(psmouse);
1483 		}
1484 		psmouse->pktcnt = 0;
1485 	}
1486 
1487 	serio_continue_rx(psmouse->ps2dev.serio);
1488 }
1489 
1490 static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
1491 {
1492 	struct alps_data *priv = psmouse->private;
1493 
1494 	/*
1495 	 * Check if we are dealing with a bare PS/2 packet, presumably from
1496 	 * a device connected to the external PS/2 port. Because bare PS/2
1497 	 * protocol does not have enough constant bits to self-synchronize
1498 	 * properly we only do this if the device is fully synchronized.
1499 	 * Can not distinguish V8's first byte from PS/2 packet's
1500 	 */
1501 	if (priv->proto_version != ALPS_PROTO_V8 &&
1502 	    !psmouse->out_of_sync_cnt &&
1503 	    (psmouse->packet[0] & 0xc8) == 0x08) {
1504 
1505 		if (psmouse->pktcnt == 3) {
1506 			alps_report_bare_ps2_packet(psmouse, psmouse->packet,
1507 						    true);
1508 			return PSMOUSE_FULL_PACKET;
1509 		}
1510 		return PSMOUSE_GOOD_DATA;
1511 	}
1512 
1513 	/* Check for PS/2 packet stuffed in the middle of ALPS packet. */
1514 
1515 	if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
1516 	    psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
1517 		return alps_handle_interleaved_ps2(psmouse);
1518 	}
1519 
1520 	if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
1521 		psmouse_dbg(psmouse,
1522 			    "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
1523 			    psmouse->packet[0], priv->mask0, priv->byte0);
1524 		return PSMOUSE_BAD_DATA;
1525 	}
1526 
1527 	/* Bytes 2 - pktsize should have 0 in the highest bit */
1528 	if (priv->proto_version < ALPS_PROTO_V5 &&
1529 	    psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
1530 	    (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
1531 		psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1532 			    psmouse->pktcnt - 1,
1533 			    psmouse->packet[psmouse->pktcnt - 1]);
1534 
1535 		if (priv->proto_version == ALPS_PROTO_V3_RUSHMORE &&
1536 		    psmouse->pktcnt == psmouse->pktsize) {
1537 			/*
1538 			 * Some Dell boxes, such as Latitude E6440 or E7440
1539 			 * with closed lid, quite often smash last byte of
1540 			 * otherwise valid packet with 0xff. Given that the
1541 			 * next packet is very likely to be valid let's
1542 			 * report PSMOUSE_FULL_PACKET but not process data,
1543 			 * rather than reporting PSMOUSE_BAD_DATA and
1544 			 * filling the logs.
1545 			 */
1546 			return PSMOUSE_FULL_PACKET;
1547 		}
1548 
1549 		return PSMOUSE_BAD_DATA;
1550 	}
1551 
1552 	if ((priv->proto_version == ALPS_PROTO_V7 &&
1553 			!alps_is_valid_package_v7(psmouse)) ||
1554 	    (priv->proto_version == ALPS_PROTO_V8 &&
1555 			!alps_is_valid_package_ss4_v2(psmouse))) {
1556 		psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1557 			    psmouse->pktcnt - 1,
1558 			    psmouse->packet[psmouse->pktcnt - 1]);
1559 		return PSMOUSE_BAD_DATA;
1560 	}
1561 
1562 	if (psmouse->pktcnt == psmouse->pktsize) {
1563 		priv->process_packet(psmouse);
1564 		return PSMOUSE_FULL_PACKET;
1565 	}
1566 
1567 	return PSMOUSE_GOOD_DATA;
1568 }
1569 
1570 static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
1571 {
1572 	struct ps2dev *ps2dev = &psmouse->ps2dev;
1573 	struct alps_data *priv = psmouse->private;
1574 	int command;
1575 	unsigned char *param;
1576 	unsigned char dummy[4];
1577 
1578 	BUG_ON(nibble > 0xf);
1579 
1580 	command = priv->nibble_commands[nibble].command;
1581 	param = (command & 0x0f00) ?
1582 		dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
1583 
1584 	if (ps2_command(ps2dev, param, command))
1585 		return -1;
1586 
1587 	return 0;
1588 }
1589 
1590 static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
1591 {
1592 	struct ps2dev *ps2dev = &psmouse->ps2dev;
1593 	struct alps_data *priv = psmouse->private;
1594 	int i, nibble;
1595 
1596 	if (ps2_command(ps2dev, NULL, priv->addr_command))
1597 		return -1;
1598 
1599 	for (i = 12; i >= 0; i -= 4) {
1600 		nibble = (addr >> i) & 0xf;
1601 		if (alps_command_mode_send_nibble(psmouse, nibble))
1602 			return -1;
1603 	}
1604 
1605 	return 0;
1606 }
1607 
1608 static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1609 {
1610 	struct ps2dev *ps2dev = &psmouse->ps2dev;
1611 	unsigned char param[4];
1612 
1613 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1614 		return -1;
1615 
1616 	/*
1617 	 * The address being read is returned in the first two bytes
1618 	 * of the result. Check that this address matches the expected
1619 	 * address.
1620 	 */
1621 	if (addr != ((param[0] << 8) | param[1]))
1622 		return -1;
1623 
1624 	return param[2];
1625 }
1626 
1627 static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1628 {
1629 	if (alps_command_mode_set_addr(psmouse, addr))
1630 		return -1;
1631 	return __alps_command_mode_read_reg(psmouse, addr);
1632 }
1633 
1634 static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
1635 {
1636 	if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
1637 		return -1;
1638 	if (alps_command_mode_send_nibble(psmouse, value & 0xf))
1639 		return -1;
1640 	return 0;
1641 }
1642 
1643 static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
1644 				       u8 value)
1645 {
1646 	if (alps_command_mode_set_addr(psmouse, addr))
1647 		return -1;
1648 	return __alps_command_mode_write_reg(psmouse, value);
1649 }
1650 
1651 static int alps_rpt_cmd(struct psmouse *psmouse, int init_command,
1652 			int repeated_command, unsigned char *param)
1653 {
1654 	struct ps2dev *ps2dev = &psmouse->ps2dev;
1655 
1656 	param[0] = 0;
1657 	if (init_command && ps2_command(ps2dev, param, init_command))
1658 		return -EIO;
1659 
1660 	if (ps2_command(ps2dev,  NULL, repeated_command) ||
1661 	    ps2_command(ps2dev,  NULL, repeated_command) ||
1662 	    ps2_command(ps2dev,  NULL, repeated_command))
1663 		return -EIO;
1664 
1665 	param[0] = param[1] = param[2] = 0xff;
1666 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1667 		return -EIO;
1668 
1669 	psmouse_dbg(psmouse, "%2.2X report: %3ph\n",
1670 		    repeated_command, param);
1671 	return 0;
1672 }
1673 
1674 static bool alps_check_valid_firmware_id(unsigned char id[])
1675 {
1676 	if (id[0] == 0x73)
1677 		return true;
1678 
1679 	if (id[0] == 0x88 &&
1680 	    (id[1] == 0x07 ||
1681 	     id[1] == 0x08 ||
1682 	     (id[1] & 0xf0) == 0xb0 ||
1683 	     (id[1] & 0xf0) == 0xc0)) {
1684 		return true;
1685 	}
1686 
1687 	return false;
1688 }
1689 
1690 static int alps_enter_command_mode(struct psmouse *psmouse)
1691 {
1692 	unsigned char param[4];
1693 
1694 	if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) {
1695 		psmouse_err(psmouse, "failed to enter command mode\n");
1696 		return -1;
1697 	}
1698 
1699 	if (!alps_check_valid_firmware_id(param)) {
1700 		psmouse_dbg(psmouse,
1701 			    "unknown response while entering command mode\n");
1702 		return -1;
1703 	}
1704 	return 0;
1705 }
1706 
1707 static inline int alps_exit_command_mode(struct psmouse *psmouse)
1708 {
1709 	struct ps2dev *ps2dev = &psmouse->ps2dev;
1710 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
1711 		return -1;
1712 	return 0;
1713 }
1714 
1715 /*
1716  * For DualPoint devices select the device that should respond to
1717  * subsequent commands. It looks like glidepad is behind stickpointer,
1718  * I'd thought it would be other way around...
1719  */
1720 static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
1721 {
1722 	struct ps2dev *ps2dev = &psmouse->ps2dev;
1723 	int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
1724 
1725 	if (ps2_command(ps2dev, NULL, cmd) ||
1726 	    ps2_command(ps2dev, NULL, cmd) ||
1727 	    ps2_command(ps2dev, NULL, cmd) ||
1728 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1729 		return -1;
1730 
1731 	/* we may get 3 more bytes, just ignore them */
1732 	ps2_drain(ps2dev, 3, 100);
1733 
1734 	return 0;
1735 }
1736 
1737 static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
1738 {
1739 	struct ps2dev *ps2dev = &psmouse->ps2dev;
1740 
1741 	/* Try ALPS magic knock - 4 disable before enable */
1742 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1743 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1744 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1745 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1746 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
1747 		return -1;
1748 
1749 	/*
1750 	 * Switch mouse to poll (remote) mode so motion data will not
1751 	 * get in our way
1752 	 */
1753 	return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
1754 }
1755 
1756 static int alps_monitor_mode_send_word(struct psmouse *psmouse, u16 word)
1757 {
1758 	int i, nibble;
1759 
1760 	/*
1761 	 * b0-b11 are valid bits, send sequence is inverse.
1762 	 * e.g. when word = 0x0123, nibble send sequence is 3, 2, 1
1763 	 */
1764 	for (i = 0; i <= 8; i += 4) {
1765 		nibble = (word >> i) & 0xf;
1766 		if (alps_command_mode_send_nibble(psmouse, nibble))
1767 			return -1;
1768 	}
1769 
1770 	return 0;
1771 }
1772 
1773 static int alps_monitor_mode_write_reg(struct psmouse *psmouse,
1774 				       u16 addr, u16 value)
1775 {
1776 	struct ps2dev *ps2dev = &psmouse->ps2dev;
1777 
1778 	/* 0x0A0 is the command to write the word */
1779 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE) ||
1780 	    alps_monitor_mode_send_word(psmouse, 0x0A0) ||
1781 	    alps_monitor_mode_send_word(psmouse, addr) ||
1782 	    alps_monitor_mode_send_word(psmouse, value) ||
1783 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1784 		return -1;
1785 
1786 	return 0;
1787 }
1788 
1789 static int alps_monitor_mode(struct psmouse *psmouse, bool enable)
1790 {
1791 	struct ps2dev *ps2dev = &psmouse->ps2dev;
1792 
1793 	if (enable) {
1794 		/* EC E9 F5 F5 E7 E6 E7 E9 to enter monitor mode */
1795 		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
1796 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO) ||
1797 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1798 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1799 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1800 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1801 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1802 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO))
1803 			return -1;
1804 	} else {
1805 		/* EC to exit monitor mode */
1806 		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP))
1807 			return -1;
1808 	}
1809 
1810 	return 0;
1811 }
1812 
1813 static int alps_absolute_mode_v6(struct psmouse *psmouse)
1814 {
1815 	u16 reg_val = 0x181;
1816 	int ret = -1;
1817 
1818 	/* enter monitor mode, to write the register */
1819 	if (alps_monitor_mode(psmouse, true))
1820 		return -1;
1821 
1822 	ret = alps_monitor_mode_write_reg(psmouse, 0x000, reg_val);
1823 
1824 	if (alps_monitor_mode(psmouse, false))
1825 		ret = -1;
1826 
1827 	return ret;
1828 }
1829 
1830 static int alps_get_status(struct psmouse *psmouse, char *param)
1831 {
1832 	/* Get status: 0xF5 0xF5 0xF5 0xE9 */
1833 	if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param))
1834 		return -1;
1835 
1836 	return 0;
1837 }
1838 
1839 /*
1840  * Turn touchpad tapping on or off. The sequences are:
1841  * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
1842  * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
1843  * My guess that 0xE9 (GetInfo) is here as a sync point.
1844  * For models that also have stickpointer (DualPoints) its tapping
1845  * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
1846  * we don't fiddle with it.
1847  */
1848 static int alps_tap_mode(struct psmouse *psmouse, int enable)
1849 {
1850 	struct ps2dev *ps2dev = &psmouse->ps2dev;
1851 	int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
1852 	unsigned char tap_arg = enable ? 0x0A : 0x00;
1853 	unsigned char param[4];
1854 
1855 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
1856 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1857 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1858 	    ps2_command(ps2dev, &tap_arg, cmd))
1859 		return -1;
1860 
1861 	if (alps_get_status(psmouse, param))
1862 		return -1;
1863 
1864 	return 0;
1865 }
1866 
1867 /*
1868  * alps_poll() - poll the touchpad for current motion packet.
1869  * Used in resync.
1870  */
1871 static int alps_poll(struct psmouse *psmouse)
1872 {
1873 	struct alps_data *priv = psmouse->private;
1874 	unsigned char buf[sizeof(psmouse->packet)];
1875 	bool poll_failed;
1876 
1877 	if (priv->flags & ALPS_PASS)
1878 		alps_passthrough_mode_v2(psmouse, true);
1879 
1880 	poll_failed = ps2_command(&psmouse->ps2dev, buf,
1881 				  PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
1882 
1883 	if (priv->flags & ALPS_PASS)
1884 		alps_passthrough_mode_v2(psmouse, false);
1885 
1886 	if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
1887 		return -1;
1888 
1889 	if ((psmouse->badbyte & 0xc8) == 0x08) {
1890 /*
1891  * Poll the track stick ...
1892  */
1893 		if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
1894 			return -1;
1895 	}
1896 
1897 	memcpy(psmouse->packet, buf, sizeof(buf));
1898 	return 0;
1899 }
1900 
1901 static int alps_hw_init_v1_v2(struct psmouse *psmouse)
1902 {
1903 	struct alps_data *priv = psmouse->private;
1904 
1905 	if ((priv->flags & ALPS_PASS) &&
1906 	    alps_passthrough_mode_v2(psmouse, true)) {
1907 		return -1;
1908 	}
1909 
1910 	if (alps_tap_mode(psmouse, true)) {
1911 		psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
1912 		return -1;
1913 	}
1914 
1915 	if (alps_absolute_mode_v1_v2(psmouse)) {
1916 		psmouse_err(psmouse, "Failed to enable absolute mode\n");
1917 		return -1;
1918 	}
1919 
1920 	if ((priv->flags & ALPS_PASS) &&
1921 	    alps_passthrough_mode_v2(psmouse, false)) {
1922 		return -1;
1923 	}
1924 
1925 	/* ALPS needs stream mode, otherwise it won't report any data */
1926 	if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
1927 		psmouse_err(psmouse, "Failed to enable stream mode\n");
1928 		return -1;
1929 	}
1930 
1931 	return 0;
1932 }
1933 
1934 static int alps_hw_init_v6(struct psmouse *psmouse)
1935 {
1936 	unsigned char param[2] = {0xC8, 0x14};
1937 
1938 	/* Enter passthrough mode to let trackpoint enter 6byte raw mode */
1939 	if (alps_passthrough_mode_v2(psmouse, true))
1940 		return -1;
1941 
1942 	if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1943 	    ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1944 	    ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1945 	    ps2_command(&psmouse->ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
1946 	    ps2_command(&psmouse->ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
1947 		return -1;
1948 
1949 	if (alps_passthrough_mode_v2(psmouse, false))
1950 		return -1;
1951 
1952 	if (alps_absolute_mode_v6(psmouse)) {
1953 		psmouse_err(psmouse, "Failed to enable absolute mode\n");
1954 		return -1;
1955 	}
1956 
1957 	return 0;
1958 }
1959 
1960 /*
1961  * Enable or disable passthrough mode to the trackstick.
1962  */
1963 static int alps_passthrough_mode_v3(struct psmouse *psmouse,
1964 				    int reg_base, bool enable)
1965 {
1966 	int reg_val, ret = -1;
1967 
1968 	if (alps_enter_command_mode(psmouse))
1969 		return -1;
1970 
1971 	reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x0008);
1972 	if (reg_val == -1)
1973 		goto error;
1974 
1975 	if (enable)
1976 		reg_val |= 0x01;
1977 	else
1978 		reg_val &= ~0x01;
1979 
1980 	ret = __alps_command_mode_write_reg(psmouse, reg_val);
1981 
1982 error:
1983 	if (alps_exit_command_mode(psmouse))
1984 		ret = -1;
1985 	return ret;
1986 }
1987 
1988 /* Must be in command mode when calling this function */
1989 static int alps_absolute_mode_v3(struct psmouse *psmouse)
1990 {
1991 	int reg_val;
1992 
1993 	reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
1994 	if (reg_val == -1)
1995 		return -1;
1996 
1997 	reg_val |= 0x06;
1998 	if (__alps_command_mode_write_reg(psmouse, reg_val))
1999 		return -1;
2000 
2001 	return 0;
2002 }
2003 
2004 static int alps_probe_trackstick_v3(struct psmouse *psmouse, int reg_base)
2005 {
2006 	int ret = -EIO, reg_val;
2007 
2008 	if (alps_enter_command_mode(psmouse))
2009 		goto error;
2010 
2011 	reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x08);
2012 	if (reg_val == -1)
2013 		goto error;
2014 
2015 	/* bit 7: trackstick is present */
2016 	ret = reg_val & 0x80 ? 0 : -ENODEV;
2017 
2018 error:
2019 	alps_exit_command_mode(psmouse);
2020 	return ret;
2021 }
2022 
2023 static int alps_setup_trackstick_v3(struct psmouse *psmouse, int reg_base)
2024 {
2025 	struct ps2dev *ps2dev = &psmouse->ps2dev;
2026 	int ret = 0;
2027 	unsigned char param[4];
2028 
2029 	if (alps_passthrough_mode_v3(psmouse, reg_base, true))
2030 		return -EIO;
2031 
2032 	/*
2033 	 * E7 report for the trackstick
2034 	 *
2035 	 * There have been reports of failures to seem to trace back
2036 	 * to the above trackstick check failing. When these occur
2037 	 * this E7 report fails, so when that happens we continue
2038 	 * with the assumption that there isn't a trackstick after
2039 	 * all.
2040 	 */
2041 	if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_SETSCALE21, param)) {
2042 		psmouse_warn(psmouse, "Failed to initialize trackstick (E7 report failed)\n");
2043 		ret = -ENODEV;
2044 	} else {
2045 		psmouse_dbg(psmouse, "trackstick E7 report: %3ph\n", param);
2046 
2047 		/*
2048 		 * Not sure what this does, but it is absolutely
2049 		 * essential. Without it, the touchpad does not
2050 		 * work at all and the trackstick just emits normal
2051 		 * PS/2 packets.
2052 		 */
2053 		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2054 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2055 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2056 		    alps_command_mode_send_nibble(psmouse, 0x9) ||
2057 		    alps_command_mode_send_nibble(psmouse, 0x4)) {
2058 			psmouse_err(psmouse,
2059 				    "Error sending magic E6 sequence\n");
2060 			ret = -EIO;
2061 			goto error;
2062 		}
2063 
2064 		/*
2065 		 * This ensures the trackstick packets are in the format
2066 		 * supported by this driver. If bit 1 isn't set the packet
2067 		 * format is different.
2068 		 */
2069 		if (alps_enter_command_mode(psmouse) ||
2070 		    alps_command_mode_write_reg(psmouse,
2071 						reg_base + 0x08, 0x82) ||
2072 		    alps_exit_command_mode(psmouse))
2073 			ret = -EIO;
2074 	}
2075 
2076 error:
2077 	if (alps_passthrough_mode_v3(psmouse, reg_base, false))
2078 		ret = -EIO;
2079 
2080 	return ret;
2081 }
2082 
2083 static int alps_hw_init_v3(struct psmouse *psmouse)
2084 {
2085 	struct ps2dev *ps2dev = &psmouse->ps2dev;
2086 	int reg_val;
2087 	unsigned char param[4];
2088 
2089 	reg_val = alps_probe_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE);
2090 	if (reg_val == -EIO)
2091 		goto error;
2092 
2093 	if (reg_val == 0 &&
2094 	    alps_setup_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE) == -EIO)
2095 		goto error;
2096 
2097 	if (alps_enter_command_mode(psmouse) ||
2098 	    alps_absolute_mode_v3(psmouse)) {
2099 		psmouse_err(psmouse, "Failed to enter absolute mode\n");
2100 		goto error;
2101 	}
2102 
2103 	reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
2104 	if (reg_val == -1)
2105 		goto error;
2106 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
2107 		goto error;
2108 
2109 	reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
2110 	if (reg_val == -1)
2111 		goto error;
2112 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
2113 		goto error;
2114 
2115 	if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
2116 		goto error;
2117 	if (__alps_command_mode_write_reg(psmouse, 0x04))
2118 		goto error;
2119 
2120 	if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
2121 		goto error;
2122 	if (__alps_command_mode_write_reg(psmouse, 0x03))
2123 		goto error;
2124 
2125 	if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
2126 		goto error;
2127 	if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
2128 		goto error;
2129 
2130 	if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
2131 		goto error;
2132 	if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
2133 		goto error;
2134 
2135 	alps_exit_command_mode(psmouse);
2136 
2137 	/* Set rate and enable data reporting */
2138 	param[0] = 0x64;
2139 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
2140 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
2141 		psmouse_err(psmouse, "Failed to enable data reporting\n");
2142 		return -1;
2143 	}
2144 
2145 	return 0;
2146 
2147 error:
2148 	/*
2149 	 * Leaving the touchpad in command mode will essentially render
2150 	 * it unusable until the machine reboots, so exit it here just
2151 	 * to be safe
2152 	 */
2153 	alps_exit_command_mode(psmouse);
2154 	return -1;
2155 }
2156 
2157 static int alps_get_v3_v7_resolution(struct psmouse *psmouse, int reg_pitch)
2158 {
2159 	int reg, x_pitch, y_pitch, x_electrode, y_electrode, x_phys, y_phys;
2160 	struct alps_data *priv = psmouse->private;
2161 
2162 	reg = alps_command_mode_read_reg(psmouse, reg_pitch);
2163 	if (reg < 0)
2164 		return reg;
2165 
2166 	x_pitch = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
2167 	x_pitch = 50 + 2 * x_pitch; /* In 0.1 mm units */
2168 
2169 	y_pitch = (char)reg >> 4; /* sign extend upper 4 bits */
2170 	y_pitch = 36 + 2 * y_pitch; /* In 0.1 mm units */
2171 
2172 	reg = alps_command_mode_read_reg(psmouse, reg_pitch + 1);
2173 	if (reg < 0)
2174 		return reg;
2175 
2176 	x_electrode = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
2177 	x_electrode = 17 + x_electrode;
2178 
2179 	y_electrode = (char)reg >> 4; /* sign extend upper 4 bits */
2180 	y_electrode = 13 + y_electrode;
2181 
2182 	x_phys = x_pitch * (x_electrode - 1); /* In 0.1 mm units */
2183 	y_phys = y_pitch * (y_electrode - 1); /* In 0.1 mm units */
2184 
2185 	priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
2186 	priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
2187 
2188 	psmouse_dbg(psmouse,
2189 		    "pitch %dx%d num-electrodes %dx%d physical size %dx%d mm res %dx%d\n",
2190 		    x_pitch, y_pitch, x_electrode, y_electrode,
2191 		    x_phys / 10, y_phys / 10, priv->x_res, priv->y_res);
2192 
2193 	return 0;
2194 }
2195 
2196 static int alps_hw_init_rushmore_v3(struct psmouse *psmouse)
2197 {
2198 	struct alps_data *priv = psmouse->private;
2199 	struct ps2dev *ps2dev = &psmouse->ps2dev;
2200 	int reg_val, ret = -1;
2201 
2202 	if (priv->flags & ALPS_DUALPOINT) {
2203 		reg_val = alps_setup_trackstick_v3(psmouse,
2204 						   ALPS_REG_BASE_RUSHMORE);
2205 		if (reg_val == -EIO)
2206 			goto error;
2207 	}
2208 
2209 	if (alps_enter_command_mode(psmouse) ||
2210 	    alps_command_mode_read_reg(psmouse, 0xc2d9) == -1 ||
2211 	    alps_command_mode_write_reg(psmouse, 0xc2cb, 0x00))
2212 		goto error;
2213 
2214 	if (alps_get_v3_v7_resolution(psmouse, 0xc2da))
2215 		goto error;
2216 
2217 	reg_val = alps_command_mode_read_reg(psmouse, 0xc2c6);
2218 	if (reg_val == -1)
2219 		goto error;
2220 	if (__alps_command_mode_write_reg(psmouse, reg_val & 0xfd))
2221 		goto error;
2222 
2223 	if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
2224 		goto error;
2225 
2226 	/* enter absolute mode */
2227 	reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
2228 	if (reg_val == -1)
2229 		goto error;
2230 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
2231 		goto error;
2232 
2233 	alps_exit_command_mode(psmouse);
2234 	return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2235 
2236 error:
2237 	alps_exit_command_mode(psmouse);
2238 	return ret;
2239 }
2240 
2241 /* Must be in command mode when calling this function */
2242 static int alps_absolute_mode_v4(struct psmouse *psmouse)
2243 {
2244 	int reg_val;
2245 
2246 	reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
2247 	if (reg_val == -1)
2248 		return -1;
2249 
2250 	reg_val |= 0x02;
2251 	if (__alps_command_mode_write_reg(psmouse, reg_val))
2252 		return -1;
2253 
2254 	return 0;
2255 }
2256 
2257 static int alps_hw_init_v4(struct psmouse *psmouse)
2258 {
2259 	struct ps2dev *ps2dev = &psmouse->ps2dev;
2260 	unsigned char param[4];
2261 
2262 	if (alps_enter_command_mode(psmouse))
2263 		goto error;
2264 
2265 	if (alps_absolute_mode_v4(psmouse)) {
2266 		psmouse_err(psmouse, "Failed to enter absolute mode\n");
2267 		goto error;
2268 	}
2269 
2270 	if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
2271 		goto error;
2272 
2273 	if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
2274 		goto error;
2275 
2276 	if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
2277 		goto error;
2278 
2279 	if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
2280 		goto error;
2281 
2282 	if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
2283 		goto error;
2284 
2285 	if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
2286 		goto error;
2287 
2288 	if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
2289 		goto error;
2290 
2291 	if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
2292 		goto error;
2293 
2294 	alps_exit_command_mode(psmouse);
2295 
2296 	/*
2297 	 * This sequence changes the output from a 9-byte to an
2298 	 * 8-byte format. All the same data seems to be present,
2299 	 * just in a more compact format.
2300 	 */
2301 	param[0] = 0xc8;
2302 	param[1] = 0x64;
2303 	param[2] = 0x50;
2304 	if (ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2305 	    ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE) ||
2306 	    ps2_command(ps2dev, &param[2], PSMOUSE_CMD_SETRATE) ||
2307 	    ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
2308 		return -1;
2309 
2310 	/* Set rate and enable data reporting */
2311 	param[0] = 0x64;
2312 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
2313 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
2314 		psmouse_err(psmouse, "Failed to enable data reporting\n");
2315 		return -1;
2316 	}
2317 
2318 	return 0;
2319 
2320 error:
2321 	/*
2322 	 * Leaving the touchpad in command mode will essentially render
2323 	 * it unusable until the machine reboots, so exit it here just
2324 	 * to be safe
2325 	 */
2326 	alps_exit_command_mode(psmouse);
2327 	return -1;
2328 }
2329 
2330 static int alps_get_otp_values_ss4_v2(struct psmouse *psmouse,
2331 				      unsigned char index, unsigned char otp[])
2332 {
2333 	struct ps2dev *ps2dev = &psmouse->ps2dev;
2334 
2335 	switch (index) {
2336 	case 0:
2337 		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)  ||
2338 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)  ||
2339 		    ps2_command(ps2dev, otp, PSMOUSE_CMD_GETINFO))
2340 			return -1;
2341 
2342 		break;
2343 
2344 	case 1:
2345 		if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL)  ||
2346 		    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL)  ||
2347 		    ps2_command(ps2dev, otp, PSMOUSE_CMD_GETINFO))
2348 			return -1;
2349 
2350 		break;
2351 	}
2352 
2353 	return 0;
2354 }
2355 
2356 static int alps_update_device_area_ss4_v2(unsigned char otp[][4],
2357 					  struct alps_data *priv)
2358 {
2359 	int num_x_electrode;
2360 	int num_y_electrode;
2361 	int x_pitch, y_pitch, x_phys, y_phys;
2362 
2363 	num_x_electrode = SS4_NUMSENSOR_XOFFSET + (otp[1][0] & 0x0F);
2364 	num_y_electrode = SS4_NUMSENSOR_YOFFSET + ((otp[1][0] >> 4) & 0x0F);
2365 
2366 	priv->x_max = (num_x_electrode - 1) * SS4_COUNT_PER_ELECTRODE;
2367 	priv->y_max = (num_y_electrode - 1) * SS4_COUNT_PER_ELECTRODE;
2368 
2369 	x_pitch = ((otp[1][2] >> 2) & 0x07) + SS4_MIN_PITCH_MM;
2370 	y_pitch = ((otp[1][2] >> 5) & 0x07) + SS4_MIN_PITCH_MM;
2371 
2372 	x_phys = x_pitch * (num_x_electrode - 1); /* In 0.1 mm units */
2373 	y_phys = y_pitch * (num_y_electrode - 1); /* In 0.1 mm units */
2374 
2375 	priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
2376 	priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
2377 
2378 	return 0;
2379 }
2380 
2381 static int alps_update_btn_info_ss4_v2(unsigned char otp[][4],
2382 				       struct alps_data *priv)
2383 {
2384 	unsigned char is_btnless;
2385 
2386 	is_btnless = (otp[1][1] >> 3) & 0x01;
2387 
2388 	if (is_btnless)
2389 		priv->flags |= ALPS_BUTTONPAD;
2390 
2391 	return 0;
2392 }
2393 
2394 static int alps_set_defaults_ss4_v2(struct psmouse *psmouse,
2395 				    struct alps_data *priv)
2396 {
2397 	unsigned char otp[2][4];
2398 
2399 	memset(otp, 0, sizeof(otp));
2400 
2401 	if (alps_get_otp_values_ss4_v2(psmouse, 0, &otp[0][0]) ||
2402 	    alps_get_otp_values_ss4_v2(psmouse, 1, &otp[1][0]))
2403 		return -1;
2404 
2405 	alps_update_device_area_ss4_v2(otp, priv);
2406 
2407 	alps_update_btn_info_ss4_v2(otp, priv);
2408 
2409 	return 0;
2410 }
2411 
2412 static int alps_dolphin_get_device_area(struct psmouse *psmouse,
2413 					struct alps_data *priv)
2414 {
2415 	struct ps2dev *ps2dev = &psmouse->ps2dev;
2416 	unsigned char param[4] = {0};
2417 	int num_x_electrode, num_y_electrode;
2418 
2419 	if (alps_enter_command_mode(psmouse))
2420 		return -1;
2421 
2422 	param[0] = 0x0a;
2423 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
2424 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2425 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2426 	    ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2427 	    ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE))
2428 		return -1;
2429 
2430 	if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
2431 		return -1;
2432 
2433 	/*
2434 	 * Dolphin's sensor line number is not fixed. It can be calculated
2435 	 * by adding the device's register value with DOLPHIN_PROFILE_X/YOFFSET.
2436 	 * Further more, we can get device's x_max and y_max by multiplying
2437 	 * sensor line number with DOLPHIN_COUNT_PER_ELECTRODE.
2438 	 *
2439 	 * e.g. When we get register's sensor_x = 11 & sensor_y = 8,
2440 	 *	real sensor line number X = 11 + 8 = 19, and
2441 	 *	real sensor line number Y = 8 + 1 = 9.
2442 	 *	So, x_max = (19 - 1) * 64 = 1152, and
2443 	 *	    y_max = (9 - 1) * 64 = 512.
2444 	 */
2445 	num_x_electrode = DOLPHIN_PROFILE_XOFFSET + (param[2] & 0x0F);
2446 	num_y_electrode = DOLPHIN_PROFILE_YOFFSET + ((param[2] >> 4) & 0x0F);
2447 	priv->x_bits = num_x_electrode;
2448 	priv->y_bits = num_y_electrode;
2449 	priv->x_max = (num_x_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2450 	priv->y_max = (num_y_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2451 
2452 	if (alps_exit_command_mode(psmouse))
2453 		return -1;
2454 
2455 	return 0;
2456 }
2457 
2458 static int alps_hw_init_dolphin_v1(struct psmouse *psmouse)
2459 {
2460 	struct ps2dev *ps2dev = &psmouse->ps2dev;
2461 	unsigned char param[2];
2462 
2463 	/* This is dolphin "v1" as empirically defined by florin9doi */
2464 	param[0] = 0x64;
2465 	param[1] = 0x28;
2466 
2467 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2468 	    ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2469 	    ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
2470 		return -1;
2471 
2472 	return 0;
2473 }
2474 
2475 static int alps_hw_init_v7(struct psmouse *psmouse)
2476 {
2477 	struct ps2dev *ps2dev = &psmouse->ps2dev;
2478 	int reg_val, ret = -1;
2479 
2480 	if (alps_enter_command_mode(psmouse) ||
2481 	    alps_command_mode_read_reg(psmouse, 0xc2d9) == -1)
2482 		goto error;
2483 
2484 	if (alps_get_v3_v7_resolution(psmouse, 0xc397))
2485 		goto error;
2486 
2487 	if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
2488 		goto error;
2489 
2490 	reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
2491 	if (reg_val == -1)
2492 		goto error;
2493 	if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
2494 		goto error;
2495 
2496 	alps_exit_command_mode(psmouse);
2497 	return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2498 
2499 error:
2500 	alps_exit_command_mode(psmouse);
2501 	return ret;
2502 }
2503 
2504 static int alps_hw_init_ss4_v2(struct psmouse *psmouse)
2505 {
2506 	struct ps2dev *ps2dev = &psmouse->ps2dev;
2507 	char param[2] = {0x64, 0x28};
2508 	int ret = -1;
2509 
2510 	/* enter absolute mode */
2511 	if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2512 	    ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2513 	    ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2514 	    ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE)) {
2515 		goto error;
2516 	}
2517 
2518 	/* T.B.D. Decread noise packet number, delete in the future */
2519 	if (alps_exit_command_mode(psmouse) ||
2520 	    alps_enter_command_mode(psmouse) ||
2521 	    alps_command_mode_write_reg(psmouse, 0x001D, 0x20)) {
2522 		goto error;
2523 	}
2524 	alps_exit_command_mode(psmouse);
2525 
2526 	return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2527 
2528 error:
2529 	alps_exit_command_mode(psmouse);
2530 	return ret;
2531 }
2532 
2533 static int alps_set_protocol(struct psmouse *psmouse,
2534 			     struct alps_data *priv,
2535 			     const struct alps_protocol_info *protocol)
2536 {
2537 	psmouse->private = priv;
2538 
2539 	setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
2540 
2541 	priv->proto_version = protocol->version;
2542 	priv->byte0 = protocol->byte0;
2543 	priv->mask0 = protocol->mask0;
2544 	priv->flags = protocol->flags;
2545 
2546 	priv->x_max = 2000;
2547 	priv->y_max = 1400;
2548 	priv->x_bits = 15;
2549 	priv->y_bits = 11;
2550 
2551 	switch (priv->proto_version) {
2552 	case ALPS_PROTO_V1:
2553 	case ALPS_PROTO_V2:
2554 		priv->hw_init = alps_hw_init_v1_v2;
2555 		priv->process_packet = alps_process_packet_v1_v2;
2556 		priv->set_abs_params = alps_set_abs_params_st;
2557 		priv->x_max = 1023;
2558 		priv->y_max = 767;
2559 		break;
2560 
2561 	case ALPS_PROTO_V3:
2562 		priv->hw_init = alps_hw_init_v3;
2563 		priv->process_packet = alps_process_packet_v3;
2564 		priv->set_abs_params = alps_set_abs_params_mt;
2565 		priv->decode_fields = alps_decode_pinnacle;
2566 		priv->nibble_commands = alps_v3_nibble_commands;
2567 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2568 		break;
2569 
2570 	case ALPS_PROTO_V3_RUSHMORE:
2571 		priv->hw_init = alps_hw_init_rushmore_v3;
2572 		priv->process_packet = alps_process_packet_v3;
2573 		priv->set_abs_params = alps_set_abs_params_mt;
2574 		priv->decode_fields = alps_decode_rushmore;
2575 		priv->nibble_commands = alps_v3_nibble_commands;
2576 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2577 		priv->x_bits = 16;
2578 		priv->y_bits = 12;
2579 
2580 		if (alps_probe_trackstick_v3(psmouse,
2581 					     ALPS_REG_BASE_RUSHMORE) < 0)
2582 			priv->flags &= ~ALPS_DUALPOINT;
2583 
2584 		break;
2585 
2586 	case ALPS_PROTO_V4:
2587 		priv->hw_init = alps_hw_init_v4;
2588 		priv->process_packet = alps_process_packet_v4;
2589 		priv->set_abs_params = alps_set_abs_params_mt;
2590 		priv->nibble_commands = alps_v4_nibble_commands;
2591 		priv->addr_command = PSMOUSE_CMD_DISABLE;
2592 		break;
2593 
2594 	case ALPS_PROTO_V5:
2595 		priv->hw_init = alps_hw_init_dolphin_v1;
2596 		priv->process_packet = alps_process_touchpad_packet_v3_v5;
2597 		priv->decode_fields = alps_decode_dolphin;
2598 		priv->set_abs_params = alps_set_abs_params_mt;
2599 		priv->nibble_commands = alps_v3_nibble_commands;
2600 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2601 		priv->x_bits = 23;
2602 		priv->y_bits = 12;
2603 
2604 		if (alps_dolphin_get_device_area(psmouse, priv))
2605 			return -EIO;
2606 
2607 		break;
2608 
2609 	case ALPS_PROTO_V6:
2610 		priv->hw_init = alps_hw_init_v6;
2611 		priv->process_packet = alps_process_packet_v6;
2612 		priv->set_abs_params = alps_set_abs_params_st;
2613 		priv->nibble_commands = alps_v6_nibble_commands;
2614 		priv->x_max = 2047;
2615 		priv->y_max = 1535;
2616 		break;
2617 
2618 	case ALPS_PROTO_V7:
2619 		priv->hw_init = alps_hw_init_v7;
2620 		priv->process_packet = alps_process_packet_v7;
2621 		priv->decode_fields = alps_decode_packet_v7;
2622 		priv->set_abs_params = alps_set_abs_params_v7;
2623 		priv->nibble_commands = alps_v3_nibble_commands;
2624 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2625 		priv->x_max = 0xfff;
2626 		priv->y_max = 0x7ff;
2627 
2628 		if (priv->fw_ver[1] != 0xba)
2629 			priv->flags |= ALPS_BUTTONPAD;
2630 
2631 		break;
2632 
2633 	case ALPS_PROTO_V8:
2634 		priv->hw_init = alps_hw_init_ss4_v2;
2635 		priv->process_packet = alps_process_packet_ss4_v2;
2636 		priv->decode_fields = alps_decode_ss4_v2;
2637 		priv->set_abs_params = alps_set_abs_params_ss4_v2;
2638 		priv->nibble_commands = alps_v3_nibble_commands;
2639 		priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2640 
2641 		if (alps_set_defaults_ss4_v2(psmouse, priv))
2642 			return -EIO;
2643 
2644 		break;
2645 	}
2646 
2647 	return 0;
2648 }
2649 
2650 static const struct alps_protocol_info *alps_match_table(unsigned char *e7,
2651 							 unsigned char *ec)
2652 {
2653 	const struct alps_model_info *model;
2654 	int i;
2655 
2656 	for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
2657 		model = &alps_model_data[i];
2658 
2659 		if (!memcmp(e7, model->signature, sizeof(model->signature)) &&
2660 		    (!model->command_mode_resp ||
2661 		     model->command_mode_resp == ec[2])) {
2662 
2663 			return &model->protocol_info;
2664 		}
2665 	}
2666 
2667 	return NULL;
2668 }
2669 
2670 static int alps_identify(struct psmouse *psmouse, struct alps_data *priv)
2671 {
2672 	const struct alps_protocol_info *protocol;
2673 	unsigned char e6[4], e7[4], ec[4];
2674 	int error;
2675 
2676 	/*
2677 	 * First try "E6 report".
2678 	 * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
2679 	 * The bits 0-2 of the first byte will be 1s if some buttons are
2680 	 * pressed.
2681 	 */
2682 	if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2683 			 PSMOUSE_CMD_SETSCALE11, e6))
2684 		return -EIO;
2685 
2686 	if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100))
2687 		return -EINVAL;
2688 
2689 	/*
2690 	 * Now get the "E7" and "EC" reports.  These will uniquely identify
2691 	 * most ALPS touchpads.
2692 	 */
2693 	if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2694 			 PSMOUSE_CMD_SETSCALE21, e7) ||
2695 	    alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2696 			 PSMOUSE_CMD_RESET_WRAP, ec) ||
2697 	    alps_exit_command_mode(psmouse))
2698 		return -EIO;
2699 
2700 	protocol = alps_match_table(e7, ec);
2701 	if (!protocol) {
2702 		if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0x50 &&
2703 			   ec[0] == 0x73 && (ec[1] == 0x01 || ec[1] == 0x02)) {
2704 			protocol = &alps_v5_protocol_data;
2705 		} else if (ec[0] == 0x88 &&
2706 			   ((ec[1] & 0xf0) == 0xb0 || (ec[1] & 0xf0) == 0xc0)) {
2707 			protocol = &alps_v7_protocol_data;
2708 		} else if (ec[0] == 0x88 && ec[1] == 0x08) {
2709 			protocol = &alps_v3_rushmore_data;
2710 		} else if (ec[0] == 0x88 && ec[1] == 0x07 &&
2711 			   ec[2] >= 0x90 && ec[2] <= 0x9d) {
2712 			protocol = &alps_v3_protocol_data;
2713 		} else if (e7[0] == 0x73 && e7[1] == 0x03 &&
2714 			   e7[2] == 0x14 && ec[1] == 0x02) {
2715 			protocol = &alps_v8_protocol_data;
2716 		} else {
2717 			psmouse_dbg(psmouse,
2718 				    "Likely not an ALPS touchpad: E7=%3ph, EC=%3ph\n", e7, ec);
2719 			return -EINVAL;
2720 		}
2721 	}
2722 
2723 	if (priv) {
2724 		/* Save the Firmware version */
2725 		memcpy(priv->fw_ver, ec, 3);
2726 		error = alps_set_protocol(psmouse, priv, protocol);
2727 		if (error)
2728 			return error;
2729 	}
2730 
2731 	return 0;
2732 }
2733 
2734 static int alps_reconnect(struct psmouse *psmouse)
2735 {
2736 	struct alps_data *priv = psmouse->private;
2737 
2738 	psmouse_reset(psmouse);
2739 
2740 	if (alps_identify(psmouse, priv) < 0)
2741 		return -1;
2742 
2743 	return priv->hw_init(psmouse);
2744 }
2745 
2746 static void alps_disconnect(struct psmouse *psmouse)
2747 {
2748 	struct alps_data *priv = psmouse->private;
2749 
2750 	psmouse_reset(psmouse);
2751 	del_timer_sync(&priv->timer);
2752 	if (priv->dev2)
2753 		input_unregister_device(priv->dev2);
2754 	if (!IS_ERR_OR_NULL(priv->dev3))
2755 		input_unregister_device(priv->dev3);
2756 	kfree(priv);
2757 }
2758 
2759 static void alps_set_abs_params_st(struct alps_data *priv,
2760 				   struct input_dev *dev1)
2761 {
2762 	input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
2763 	input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
2764 	input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
2765 }
2766 
2767 static void alps_set_abs_params_mt_common(struct alps_data *priv,
2768 					  struct input_dev *dev1)
2769 {
2770 	input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
2771 	input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
2772 
2773 	input_abs_set_res(dev1, ABS_MT_POSITION_X, priv->x_res);
2774 	input_abs_set_res(dev1, ABS_MT_POSITION_Y, priv->y_res);
2775 
2776 	set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
2777 	set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
2778 }
2779 
2780 static void alps_set_abs_params_mt(struct alps_data *priv,
2781 				   struct input_dev *dev1)
2782 {
2783 	alps_set_abs_params_mt_common(priv, dev1);
2784 	input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
2785 
2786 	input_mt_init_slots(dev1, MAX_TOUCHES,
2787 			    INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
2788 				INPUT_MT_TRACK | INPUT_MT_SEMI_MT);
2789 }
2790 
2791 static void alps_set_abs_params_v7(struct alps_data *priv,
2792 				   struct input_dev *dev1)
2793 {
2794 	alps_set_abs_params_mt_common(priv, dev1);
2795 	set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
2796 
2797 	input_mt_init_slots(dev1, MAX_TOUCHES,
2798 			    INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
2799 				INPUT_MT_TRACK);
2800 
2801 	set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
2802 }
2803 
2804 static void alps_set_abs_params_ss4_v2(struct alps_data *priv,
2805 				       struct input_dev *dev1)
2806 {
2807 	alps_set_abs_params_mt_common(priv, dev1);
2808 	input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
2809 	set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
2810 
2811 	input_mt_init_slots(dev1, MAX_TOUCHES,
2812 			    INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
2813 				INPUT_MT_TRACK);
2814 }
2815 
2816 int alps_init(struct psmouse *psmouse)
2817 {
2818 	struct alps_data *priv = psmouse->private;
2819 	struct input_dev *dev1 = psmouse->dev;
2820 	int error;
2821 
2822 	error = priv->hw_init(psmouse);
2823 	if (error)
2824 		goto init_fail;
2825 
2826 	/*
2827 	 * Undo part of setup done for us by psmouse core since touchpad
2828 	 * is not a relative device.
2829 	 */
2830 	__clear_bit(EV_REL, dev1->evbit);
2831 	__clear_bit(REL_X, dev1->relbit);
2832 	__clear_bit(REL_Y, dev1->relbit);
2833 
2834 	/*
2835 	 * Now set up our capabilities.
2836 	 */
2837 	dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
2838 	dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
2839 	dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
2840 	dev1->keybit[BIT_WORD(BTN_LEFT)] |=
2841 		BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
2842 
2843 	dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
2844 
2845 	priv->set_abs_params(priv, dev1);
2846 
2847 	if (priv->flags & ALPS_WHEEL) {
2848 		dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
2849 		dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
2850 	}
2851 
2852 	if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
2853 		dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
2854 		dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
2855 	}
2856 
2857 	if (priv->flags & ALPS_FOUR_BUTTONS) {
2858 		dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
2859 		dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
2860 		dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
2861 		dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
2862 	} else if (priv->flags & ALPS_BUTTONPAD) {
2863 		set_bit(INPUT_PROP_BUTTONPAD, dev1->propbit);
2864 		clear_bit(BTN_RIGHT, dev1->keybit);
2865 	} else {
2866 		dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
2867 	}
2868 
2869 	if (priv->flags & ALPS_DUALPOINT) {
2870 		struct input_dev *dev2;
2871 
2872 		dev2 = input_allocate_device();
2873 		if (!dev2) {
2874 			psmouse_err(psmouse,
2875 				    "failed to allocate trackstick device\n");
2876 			error = -ENOMEM;
2877 			goto init_fail;
2878 		}
2879 
2880 		snprintf(priv->phys2, sizeof(priv->phys2), "%s/input1",
2881 			 psmouse->ps2dev.serio->phys);
2882 		dev2->phys = priv->phys2;
2883 
2884 		/*
2885 		 * format of input device name is: "protocol vendor name"
2886 		 * see function psmouse_switch_protocol() in psmouse-base.c
2887 		 */
2888 		dev2->name = "AlpsPS/2 ALPS DualPoint Stick";
2889 
2890 		dev2->id.bustype = BUS_I8042;
2891 		dev2->id.vendor  = 0x0002;
2892 		dev2->id.product = PSMOUSE_ALPS;
2893 		dev2->id.version = priv->proto_version;
2894 		dev2->dev.parent = &psmouse->ps2dev.serio->dev;
2895 
2896 		input_set_capability(dev2, EV_REL, REL_X);
2897 		input_set_capability(dev2, EV_REL, REL_Y);
2898 		input_set_capability(dev2, EV_KEY, BTN_LEFT);
2899 		input_set_capability(dev2, EV_KEY, BTN_RIGHT);
2900 		input_set_capability(dev2, EV_KEY, BTN_MIDDLE);
2901 
2902 		__set_bit(INPUT_PROP_POINTER, dev2->propbit);
2903 		__set_bit(INPUT_PROP_POINTING_STICK, dev2->propbit);
2904 
2905 		error = input_register_device(dev2);
2906 		if (error) {
2907 			psmouse_err(psmouse,
2908 				    "failed to register trackstick device: %d\n",
2909 				    error);
2910 			input_free_device(dev2);
2911 			goto init_fail;
2912 		}
2913 
2914 		priv->dev2 = dev2;
2915 	}
2916 
2917 	priv->psmouse = psmouse;
2918 
2919 	INIT_DELAYED_WORK(&priv->dev3_register_work,
2920 			  alps_register_bare_ps2_mouse);
2921 
2922 	psmouse->protocol_handler = alps_process_byte;
2923 	psmouse->poll = alps_poll;
2924 	psmouse->disconnect = alps_disconnect;
2925 	psmouse->reconnect = alps_reconnect;
2926 	psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
2927 
2928 	/* We are having trouble resyncing ALPS touchpads so disable it for now */
2929 	psmouse->resync_time = 0;
2930 
2931 	/* Allow 2 invalid packets without resetting device */
2932 	psmouse->resetafter = psmouse->pktsize * 2;
2933 
2934 	return 0;
2935 
2936 init_fail:
2937 	psmouse_reset(psmouse);
2938 	/*
2939 	 * Even though we did not allocate psmouse->private we do free
2940 	 * it here.
2941 	 */
2942 	kfree(psmouse->private);
2943 	psmouse->private = NULL;
2944 	return error;
2945 }
2946 
2947 int alps_detect(struct psmouse *psmouse, bool set_properties)
2948 {
2949 	struct alps_data *priv;
2950 	int error;
2951 
2952 	error = alps_identify(psmouse, NULL);
2953 	if (error)
2954 		return error;
2955 
2956 	/*
2957 	 * Reset the device to make sure it is fully operational:
2958 	 * on some laptops, like certain Dell Latitudes, we may
2959 	 * fail to properly detect presence of trackstick if device
2960 	 * has not been reset.
2961 	 */
2962 	psmouse_reset(psmouse);
2963 
2964 	priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
2965 	if (!priv)
2966 		return -ENOMEM;
2967 
2968 	error = alps_identify(psmouse, priv);
2969 	if (error) {
2970 		kfree(priv);
2971 		return error;
2972 	}
2973 
2974 	if (set_properties) {
2975 		psmouse->vendor = "ALPS";
2976 		psmouse->name = priv->flags & ALPS_DUALPOINT ?
2977 				"DualPoint TouchPad" : "GlidePoint";
2978 		psmouse->model = priv->proto_version;
2979 	} else {
2980 		/*
2981 		 * Destroy alps_data structure we allocated earlier since
2982 		 * this was just a "trial run". Otherwise we'll keep it
2983 		 * to be used by alps_init() which has to be called if
2984 		 * we succeed and set_properties is true.
2985 		 */
2986 		kfree(priv);
2987 		psmouse->private = NULL;
2988 	}
2989 
2990 	return 0;
2991 }
2992 
2993