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