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