Linux 4.19.133
[linux/fpc-iii.git] / drivers / input / mouse / alps.c
blobdd80ff6cc4273079fa8f75806d89df97e914fa01
1 /*
2 * ALPS touchpad PS/2 mouse driver
4 * Copyright (c) 2003 Neil Brown <neilb@cse.unsw.edu.au>
5 * Copyright (c) 2003-2005 Peter Osterlund <petero2@telia.com>
6 * Copyright (c) 2004 Dmitry Torokhov <dtor@mail.ru>
7 * Copyright (c) 2005 Vojtech Pavlik <vojtech@suse.cz>
8 * Copyright (c) 2009 Sebastian Kapfer <sebastian_kapfer@gmx.net>
10 * ALPS detection, tap switching and status querying info is taken from
11 * tpconfig utility (by C. Scott Ananian and Bruce Kall).
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License version 2 as published by
15 * the Free Software Foundation.
18 #include <linux/slab.h>
19 #include <linux/input.h>
20 #include <linux/input/mt.h>
21 #include <linux/serio.h>
22 #include <linux/libps2.h>
23 #include <linux/dmi.h>
25 #include "psmouse.h"
26 #include "alps.h"
27 #include "trackpoint.h"
30 * Definitions for ALPS version 3 and 4 command mode protocol
32 #define ALPS_CMD_NIBBLE_10 0x01f2
34 #define ALPS_REG_BASE_RUSHMORE 0xc2c0
35 #define ALPS_REG_BASE_V7 0xc2c0
36 #define ALPS_REG_BASE_PINNACLE 0x0000
38 static const struct alps_nibble_commands alps_v3_nibble_commands[] = {
39 { PSMOUSE_CMD_SETPOLL, 0x00 }, /* 0 */
40 { PSMOUSE_CMD_RESET_DIS, 0x00 }, /* 1 */
41 { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* 2 */
42 { PSMOUSE_CMD_SETRATE, 0x0a }, /* 3 */
43 { PSMOUSE_CMD_SETRATE, 0x14 }, /* 4 */
44 { PSMOUSE_CMD_SETRATE, 0x28 }, /* 5 */
45 { PSMOUSE_CMD_SETRATE, 0x3c }, /* 6 */
46 { PSMOUSE_CMD_SETRATE, 0x50 }, /* 7 */
47 { PSMOUSE_CMD_SETRATE, 0x64 }, /* 8 */
48 { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 9 */
49 { ALPS_CMD_NIBBLE_10, 0x00 }, /* a */
50 { PSMOUSE_CMD_SETRES, 0x00 }, /* b */
51 { PSMOUSE_CMD_SETRES, 0x01 }, /* c */
52 { PSMOUSE_CMD_SETRES, 0x02 }, /* d */
53 { PSMOUSE_CMD_SETRES, 0x03 }, /* e */
54 { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */
57 static const struct alps_nibble_commands alps_v4_nibble_commands[] = {
58 { PSMOUSE_CMD_ENABLE, 0x00 }, /* 0 */
59 { PSMOUSE_CMD_RESET_DIS, 0x00 }, /* 1 */
60 { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* 2 */
61 { PSMOUSE_CMD_SETRATE, 0x0a }, /* 3 */
62 { PSMOUSE_CMD_SETRATE, 0x14 }, /* 4 */
63 { PSMOUSE_CMD_SETRATE, 0x28 }, /* 5 */
64 { PSMOUSE_CMD_SETRATE, 0x3c }, /* 6 */
65 { PSMOUSE_CMD_SETRATE, 0x50 }, /* 7 */
66 { PSMOUSE_CMD_SETRATE, 0x64 }, /* 8 */
67 { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 9 */
68 { ALPS_CMD_NIBBLE_10, 0x00 }, /* a */
69 { PSMOUSE_CMD_SETRES, 0x00 }, /* b */
70 { PSMOUSE_CMD_SETRES, 0x01 }, /* c */
71 { PSMOUSE_CMD_SETRES, 0x02 }, /* d */
72 { PSMOUSE_CMD_SETRES, 0x03 }, /* e */
73 { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */
76 static const struct alps_nibble_commands alps_v6_nibble_commands[] = {
77 { PSMOUSE_CMD_ENABLE, 0x00 }, /* 0 */
78 { PSMOUSE_CMD_SETRATE, 0x0a }, /* 1 */
79 { PSMOUSE_CMD_SETRATE, 0x14 }, /* 2 */
80 { PSMOUSE_CMD_SETRATE, 0x28 }, /* 3 */
81 { PSMOUSE_CMD_SETRATE, 0x3c }, /* 4 */
82 { PSMOUSE_CMD_SETRATE, 0x50 }, /* 5 */
83 { PSMOUSE_CMD_SETRATE, 0x64 }, /* 6 */
84 { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 7 */
85 { PSMOUSE_CMD_GETID, 0x00 }, /* 8 */
86 { PSMOUSE_CMD_GETINFO, 0x00 }, /* 9 */
87 { PSMOUSE_CMD_SETRES, 0x00 }, /* a */
88 { PSMOUSE_CMD_SETRES, 0x01 }, /* b */
89 { PSMOUSE_CMD_SETRES, 0x02 }, /* c */
90 { PSMOUSE_CMD_SETRES, 0x03 }, /* d */
91 { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* e */
92 { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */
96 #define ALPS_DUALPOINT 0x02 /* touchpad has trackstick */
97 #define ALPS_PASS 0x04 /* device has a pass-through port */
99 #define ALPS_WHEEL 0x08 /* hardware wheel present */
100 #define ALPS_FW_BK_1 0x10 /* front & back buttons present */
101 #define ALPS_FW_BK_2 0x20 /* front & back buttons present */
102 #define ALPS_FOUR_BUTTONS 0x40 /* 4 direction button present */
103 #define ALPS_PS2_INTERLEAVED 0x80 /* 3-byte PS/2 packet interleaved with
104 6-byte ALPS packet */
105 #define ALPS_STICK_BITS 0x100 /* separate stick button bits */
106 #define ALPS_BUTTONPAD 0x200 /* device is a clickpad */
107 #define ALPS_DUALPOINT_WITH_PRESSURE 0x400 /* device can report trackpoint pressure */
109 static const struct alps_model_info alps_model_data[] = {
111 * XXX This entry is suspicious. First byte has zero lower nibble,
112 * which is what a normal mouse would report. Also, the value 0x0e
113 * isn't valid per PS/2 spec.
115 { { 0x20, 0x02, 0x0e }, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },
117 { { 0x22, 0x02, 0x0a }, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },
118 { { 0x22, 0x02, 0x14 }, { ALPS_PROTO_V2, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT } }, /* Dell Latitude D600 */
119 { { 0x32, 0x02, 0x14 }, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } }, /* Toshiba Salellite Pro M10 */
120 { { 0x33, 0x02, 0x0a }, { ALPS_PROTO_V1, 0x88, 0xf8, 0 } }, /* UMAX-530T */
121 { { 0x52, 0x01, 0x14 }, { ALPS_PROTO_V2, 0xff, 0xff,
122 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED } }, /* Toshiba Tecra A11-11L */
123 { { 0x53, 0x02, 0x0a }, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
124 { { 0x53, 0x02, 0x14 }, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
125 { { 0x60, 0x03, 0xc8 }, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } }, /* HP ze1115 */
126 { { 0x62, 0x02, 0x14 }, { ALPS_PROTO_V2, 0xcf, 0xcf,
127 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED } }, /* Dell Latitude E5500, E6400, E6500, Precision M4400 */
128 { { 0x63, 0x02, 0x0a }, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
129 { { 0x63, 0x02, 0x14 }, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
130 { { 0x63, 0x02, 0x28 }, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 } }, /* Fujitsu Siemens S6010 */
131 { { 0x63, 0x02, 0x3c }, { ALPS_PROTO_V2, 0x8f, 0x8f, ALPS_WHEEL } }, /* Toshiba Satellite S2400-103 */
132 { { 0x63, 0x02, 0x50 }, { ALPS_PROTO_V2, 0xef, 0xef, ALPS_FW_BK_1 } }, /* NEC Versa L320 */
133 { { 0x63, 0x02, 0x64 }, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
134 { { 0x63, 0x03, 0xc8 }, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } }, /* Dell Latitude D800 */
135 { { 0x73, 0x00, 0x0a }, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_DUALPOINT } }, /* ThinkPad R61 8918-5QG */
136 { { 0x73, 0x00, 0x14 }, { ALPS_PROTO_V6, 0xff, 0xff, ALPS_DUALPOINT } }, /* Dell XT2 */
137 { { 0x73, 0x02, 0x0a }, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
138 { { 0x73, 0x02, 0x14 }, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 } }, /* Ahtec Laptop */
139 { { 0x73, 0x02, 0x50 }, { ALPS_PROTO_V2, 0xcf, 0xcf, ALPS_FOUR_BUTTONS } }, /* Dell Vostro 1400 */
142 static const struct alps_protocol_info alps_v3_protocol_data = {
143 ALPS_PROTO_V3, 0x8f, 0x8f, ALPS_DUALPOINT | ALPS_DUALPOINT_WITH_PRESSURE
146 static const struct alps_protocol_info alps_v3_rushmore_data = {
147 ALPS_PROTO_V3_RUSHMORE, 0x8f, 0x8f, ALPS_DUALPOINT | ALPS_DUALPOINT_WITH_PRESSURE
150 static const struct alps_protocol_info alps_v4_protocol_data = {
151 ALPS_PROTO_V4, 0x8f, 0x8f, 0
154 static const struct alps_protocol_info alps_v5_protocol_data = {
155 ALPS_PROTO_V5, 0xc8, 0xd8, 0
158 static const struct alps_protocol_info alps_v7_protocol_data = {
159 ALPS_PROTO_V7, 0x48, 0x48, ALPS_DUALPOINT | ALPS_DUALPOINT_WITH_PRESSURE
162 static const struct alps_protocol_info alps_v8_protocol_data = {
163 ALPS_PROTO_V8, 0x18, 0x18, 0
166 static const struct alps_protocol_info alps_v9_protocol_data = {
167 ALPS_PROTO_V9, 0xc8, 0xc8, 0
171 * Some v2 models report the stick buttons in separate bits
173 static const struct dmi_system_id alps_dmi_has_separate_stick_buttons[] = {
174 #if defined(CONFIG_DMI) && defined(CONFIG_X86)
176 /* Extrapolated from other entries */
177 .matches = {
178 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
179 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D420"),
183 /* Reported-by: Hans de Bruin <jmdebruin@xmsnet.nl> */
184 .matches = {
185 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
186 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D430"),
190 /* Reported-by: Hans de Goede <hdegoede@redhat.com> */
191 .matches = {
192 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
193 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D620"),
197 /* Extrapolated from other entries */
198 .matches = {
199 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
200 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D630"),
203 #endif
207 static void alps_set_abs_params_st(struct alps_data *priv,
208 struct input_dev *dev1);
209 static void alps_set_abs_params_semi_mt(struct alps_data *priv,
210 struct input_dev *dev1);
211 static void alps_set_abs_params_v7(struct alps_data *priv,
212 struct input_dev *dev1);
213 static void alps_set_abs_params_ss4_v2(struct alps_data *priv,
214 struct input_dev *dev1);
216 /* Packet formats are described in Documentation/input/devices/alps.rst */
218 static bool alps_is_valid_first_byte(struct alps_data *priv,
219 unsigned char data)
221 return (data & priv->mask0) == priv->byte0;
224 static void alps_report_buttons(struct input_dev *dev1, struct input_dev *dev2,
225 int left, int right, int middle)
227 struct input_dev *dev;
230 * If shared button has already been reported on the
231 * other device (dev2) then this event should be also
232 * sent through that device.
234 dev = (dev2 && test_bit(BTN_LEFT, dev2->key)) ? dev2 : dev1;
235 input_report_key(dev, BTN_LEFT, left);
237 dev = (dev2 && test_bit(BTN_RIGHT, dev2->key)) ? dev2 : dev1;
238 input_report_key(dev, BTN_RIGHT, right);
240 dev = (dev2 && test_bit(BTN_MIDDLE, dev2->key)) ? dev2 : dev1;
241 input_report_key(dev, BTN_MIDDLE, middle);
244 * Sync the _other_ device now, we'll do the first
245 * device later once we report the rest of the events.
247 if (dev2)
248 input_sync(dev2);
251 static void alps_process_packet_v1_v2(struct psmouse *psmouse)
253 struct alps_data *priv = psmouse->private;
254 unsigned char *packet = psmouse->packet;
255 struct input_dev *dev = psmouse->dev;
256 struct input_dev *dev2 = priv->dev2;
257 int x, y, z, ges, fin, left, right, middle;
258 int back = 0, forward = 0;
260 if (priv->proto_version == ALPS_PROTO_V1) {
261 left = packet[2] & 0x10;
262 right = packet[2] & 0x08;
263 middle = 0;
264 x = packet[1] | ((packet[0] & 0x07) << 7);
265 y = packet[4] | ((packet[3] & 0x07) << 7);
266 z = packet[5];
267 } else {
268 left = packet[3] & 1;
269 right = packet[3] & 2;
270 middle = packet[3] & 4;
271 x = packet[1] | ((packet[2] & 0x78) << (7 - 3));
272 y = packet[4] | ((packet[3] & 0x70) << (7 - 4));
273 z = packet[5];
276 if (priv->flags & ALPS_FW_BK_1) {
277 back = packet[0] & 0x10;
278 forward = packet[2] & 4;
281 if (priv->flags & ALPS_FW_BK_2) {
282 back = packet[3] & 4;
283 forward = packet[2] & 4;
284 if ((middle = forward && back))
285 forward = back = 0;
288 ges = packet[2] & 1;
289 fin = packet[2] & 2;
291 if ((priv->flags & ALPS_DUALPOINT) && z == 127) {
292 input_report_rel(dev2, REL_X, (x > 383 ? (x - 768) : x));
293 input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y));
295 alps_report_buttons(dev2, dev, left, right, middle);
297 input_sync(dev2);
298 return;
301 /* Some models have separate stick button bits */
302 if (priv->flags & ALPS_STICK_BITS) {
303 left |= packet[0] & 1;
304 right |= packet[0] & 2;
305 middle |= packet[0] & 4;
308 alps_report_buttons(dev, dev2, left, right, middle);
310 /* Convert hardware tap to a reasonable Z value */
311 if (ges && !fin)
312 z = 40;
315 * A "tap and drag" operation is reported by the hardware as a transition
316 * from (!fin && ges) to (fin && ges). This should be translated to the
317 * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
319 if (ges && fin && !priv->prev_fin) {
320 input_report_abs(dev, ABS_X, x);
321 input_report_abs(dev, ABS_Y, y);
322 input_report_abs(dev, ABS_PRESSURE, 0);
323 input_report_key(dev, BTN_TOOL_FINGER, 0);
324 input_sync(dev);
326 priv->prev_fin = fin;
328 if (z > 30)
329 input_report_key(dev, BTN_TOUCH, 1);
330 if (z < 25)
331 input_report_key(dev, BTN_TOUCH, 0);
333 if (z > 0) {
334 input_report_abs(dev, ABS_X, x);
335 input_report_abs(dev, ABS_Y, y);
338 input_report_abs(dev, ABS_PRESSURE, z);
339 input_report_key(dev, BTN_TOOL_FINGER, z > 0);
341 if (priv->flags & ALPS_WHEEL)
342 input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
344 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
345 input_report_key(dev, BTN_FORWARD, forward);
346 input_report_key(dev, BTN_BACK, back);
349 if (priv->flags & ALPS_FOUR_BUTTONS) {
350 input_report_key(dev, BTN_0, packet[2] & 4);
351 input_report_key(dev, BTN_1, packet[0] & 0x10);
352 input_report_key(dev, BTN_2, packet[3] & 4);
353 input_report_key(dev, BTN_3, packet[0] & 0x20);
356 input_sync(dev);
359 static void alps_get_bitmap_points(unsigned int map,
360 struct alps_bitmap_point *low,
361 struct alps_bitmap_point *high,
362 int *fingers)
364 struct alps_bitmap_point *point;
365 int i, bit, prev_bit = 0;
367 point = low;
368 for (i = 0; map != 0; i++, map >>= 1) {
369 bit = map & 1;
370 if (bit) {
371 if (!prev_bit) {
372 point->start_bit = i;
373 point->num_bits = 0;
374 (*fingers)++;
376 point->num_bits++;
377 } else {
378 if (prev_bit)
379 point = high;
381 prev_bit = bit;
386 * Process bitmap data from semi-mt protocols. Returns the number of
387 * fingers detected. A return value of 0 means at least one of the
388 * bitmaps was empty.
390 * The bitmaps don't have enough data to track fingers, so this function
391 * only generates points representing a bounding box of all contacts.
392 * These points are returned in fields->mt when the return value
393 * is greater than 0.
395 static int alps_process_bitmap(struct alps_data *priv,
396 struct alps_fields *fields)
398 int i, fingers_x = 0, fingers_y = 0, fingers, closest;
399 struct alps_bitmap_point x_low = {0,}, x_high = {0,};
400 struct alps_bitmap_point y_low = {0,}, y_high = {0,};
401 struct input_mt_pos corner[4];
403 if (!fields->x_map || !fields->y_map)
404 return 0;
406 alps_get_bitmap_points(fields->x_map, &x_low, &x_high, &fingers_x);
407 alps_get_bitmap_points(fields->y_map, &y_low, &y_high, &fingers_y);
410 * Fingers can overlap, so we use the maximum count of fingers
411 * on either axis as the finger count.
413 fingers = max(fingers_x, fingers_y);
416 * If an axis reports only a single contact, we have overlapping or
417 * adjacent fingers. Divide the single contact between the two points.
419 if (fingers_x == 1) {
420 i = (x_low.num_bits - 1) / 2;
421 x_low.num_bits = x_low.num_bits - i;
422 x_high.start_bit = x_low.start_bit + i;
423 x_high.num_bits = max(i, 1);
425 if (fingers_y == 1) {
426 i = (y_low.num_bits - 1) / 2;
427 y_low.num_bits = y_low.num_bits - i;
428 y_high.start_bit = y_low.start_bit + i;
429 y_high.num_bits = max(i, 1);
432 /* top-left corner */
433 corner[0].x =
434 (priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
435 (2 * (priv->x_bits - 1));
436 corner[0].y =
437 (priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
438 (2 * (priv->y_bits - 1));
440 /* top-right corner */
441 corner[1].x =
442 (priv->x_max * (2 * x_high.start_bit + x_high.num_bits - 1)) /
443 (2 * (priv->x_bits - 1));
444 corner[1].y =
445 (priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
446 (2 * (priv->y_bits - 1));
448 /* bottom-right corner */
449 corner[2].x =
450 (priv->x_max * (2 * x_high.start_bit + x_high.num_bits - 1)) /
451 (2 * (priv->x_bits - 1));
452 corner[2].y =
453 (priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) /
454 (2 * (priv->y_bits - 1));
456 /* bottom-left corner */
457 corner[3].x =
458 (priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
459 (2 * (priv->x_bits - 1));
460 corner[3].y =
461 (priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) /
462 (2 * (priv->y_bits - 1));
464 /* x-bitmap order is reversed on v5 touchpads */
465 if (priv->proto_version == ALPS_PROTO_V5) {
466 for (i = 0; i < 4; i++)
467 corner[i].x = priv->x_max - corner[i].x;
470 /* y-bitmap order is reversed on v3 and v4 touchpads */
471 if (priv->proto_version == ALPS_PROTO_V3 ||
472 priv->proto_version == ALPS_PROTO_V4) {
473 for (i = 0; i < 4; i++)
474 corner[i].y = priv->y_max - corner[i].y;
478 * We only select a corner for the second touch once per 2 finger
479 * touch sequence to avoid the chosen corner (and thus the coordinates)
480 * jumping around when the first touch is in the middle.
482 if (priv->second_touch == -1) {
483 /* Find corner closest to our st coordinates */
484 closest = 0x7fffffff;
485 for (i = 0; i < 4; i++) {
486 int dx = fields->st.x - corner[i].x;
487 int dy = fields->st.y - corner[i].y;
488 int distance = dx * dx + dy * dy;
490 if (distance < closest) {
491 priv->second_touch = i;
492 closest = distance;
495 /* And select the opposite corner to use for the 2nd touch */
496 priv->second_touch = (priv->second_touch + 2) % 4;
499 fields->mt[0] = fields->st;
500 fields->mt[1] = corner[priv->second_touch];
502 return fingers;
505 static void alps_set_slot(struct input_dev *dev, int slot, int x, int y)
507 input_mt_slot(dev, slot);
508 input_mt_report_slot_state(dev, MT_TOOL_FINGER, true);
509 input_report_abs(dev, ABS_MT_POSITION_X, x);
510 input_report_abs(dev, ABS_MT_POSITION_Y, y);
513 static void alps_report_mt_data(struct psmouse *psmouse, int n)
515 struct alps_data *priv = psmouse->private;
516 struct input_dev *dev = psmouse->dev;
517 struct alps_fields *f = &priv->f;
518 int i, slot[MAX_TOUCHES];
520 input_mt_assign_slots(dev, slot, f->mt, n, 0);
521 for (i = 0; i < n; i++)
522 alps_set_slot(dev, slot[i], f->mt[i].x, f->mt[i].y);
524 input_mt_sync_frame(dev);
527 static void alps_report_semi_mt_data(struct psmouse *psmouse, int fingers)
529 struct alps_data *priv = psmouse->private;
530 struct input_dev *dev = psmouse->dev;
531 struct alps_fields *f = &priv->f;
533 /* Use st data when we don't have mt data */
534 if (fingers < 2) {
535 f->mt[0].x = f->st.x;
536 f->mt[0].y = f->st.y;
537 fingers = f->pressure > 0 ? 1 : 0;
538 priv->second_touch = -1;
541 if (fingers >= 1)
542 alps_set_slot(dev, 0, f->mt[0].x, f->mt[0].y);
543 if (fingers >= 2)
544 alps_set_slot(dev, 1, f->mt[1].x, f->mt[1].y);
545 input_mt_sync_frame(dev);
547 input_mt_report_finger_count(dev, fingers);
549 input_report_key(dev, BTN_LEFT, f->left);
550 input_report_key(dev, BTN_RIGHT, f->right);
551 input_report_key(dev, BTN_MIDDLE, f->middle);
553 input_report_abs(dev, ABS_PRESSURE, f->pressure);
555 input_sync(dev);
558 static void alps_process_trackstick_packet_v3(struct psmouse *psmouse)
560 struct alps_data *priv = psmouse->private;
561 unsigned char *packet = psmouse->packet;
562 struct input_dev *dev = priv->dev2;
563 int x, y, z, left, right, middle;
565 /* It should be a DualPoint when received trackstick packet */
566 if (!(priv->flags & ALPS_DUALPOINT)) {
567 psmouse_warn(psmouse,
568 "Rejected trackstick packet from non DualPoint device");
569 return;
572 /* Sanity check packet */
573 if (!(packet[0] & 0x40)) {
574 psmouse_dbg(psmouse, "Bad trackstick packet, discarding\n");
575 return;
579 * There's a special packet that seems to indicate the end
580 * of a stream of trackstick data. Filter these out.
582 if (packet[1] == 0x7f && packet[2] == 0x7f && packet[4] == 0x7f)
583 return;
585 x = (s8)(((packet[0] & 0x20) << 2) | (packet[1] & 0x7f));
586 y = (s8)(((packet[0] & 0x10) << 3) | (packet[2] & 0x7f));
587 z = packet[4] & 0x7f;
590 * The x and y values tend to be quite large, and when used
591 * alone the trackstick is difficult to use. Scale them down
592 * to compensate.
594 x /= 8;
595 y /= 8;
597 input_report_rel(dev, REL_X, x);
598 input_report_rel(dev, REL_Y, -y);
599 input_report_abs(dev, ABS_PRESSURE, z);
602 * Most ALPS models report the trackstick buttons in the touchpad
603 * packets, but a few report them here. No reliable way has been
604 * found to differentiate between the models upfront, so we enable
605 * the quirk in response to seeing a button press in the trackstick
606 * packet.
608 left = packet[3] & 0x01;
609 right = packet[3] & 0x02;
610 middle = packet[3] & 0x04;
612 if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) &&
613 (left || right || middle))
614 priv->quirks |= ALPS_QUIRK_TRACKSTICK_BUTTONS;
616 if (priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) {
617 input_report_key(dev, BTN_LEFT, left);
618 input_report_key(dev, BTN_RIGHT, right);
619 input_report_key(dev, BTN_MIDDLE, middle);
622 input_sync(dev);
623 return;
626 static void alps_decode_buttons_v3(struct alps_fields *f, unsigned char *p)
628 f->left = !!(p[3] & 0x01);
629 f->right = !!(p[3] & 0x02);
630 f->middle = !!(p[3] & 0x04);
632 f->ts_left = !!(p[3] & 0x10);
633 f->ts_right = !!(p[3] & 0x20);
634 f->ts_middle = !!(p[3] & 0x40);
637 static int alps_decode_pinnacle(struct alps_fields *f, unsigned char *p,
638 struct psmouse *psmouse)
640 f->first_mp = !!(p[4] & 0x40);
641 f->is_mp = !!(p[0] & 0x40);
643 if (f->is_mp) {
644 f->fingers = (p[5] & 0x3) + 1;
645 f->x_map = ((p[4] & 0x7e) << 8) |
646 ((p[1] & 0x7f) << 2) |
647 ((p[0] & 0x30) >> 4);
648 f->y_map = ((p[3] & 0x70) << 4) |
649 ((p[2] & 0x7f) << 1) |
650 (p[4] & 0x01);
651 } else {
652 f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
653 ((p[0] & 0x30) >> 4);
654 f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
655 f->pressure = p[5] & 0x7f;
657 alps_decode_buttons_v3(f, p);
660 return 0;
663 static int alps_decode_rushmore(struct alps_fields *f, unsigned char *p,
664 struct psmouse *psmouse)
666 f->first_mp = !!(p[4] & 0x40);
667 f->is_mp = !!(p[5] & 0x40);
669 if (f->is_mp) {
670 f->fingers = max((p[5] & 0x3), ((p[5] >> 2) & 0x3)) + 1;
671 f->x_map = ((p[5] & 0x10) << 11) |
672 ((p[4] & 0x7e) << 8) |
673 ((p[1] & 0x7f) << 2) |
674 ((p[0] & 0x30) >> 4);
675 f->y_map = ((p[5] & 0x20) << 6) |
676 ((p[3] & 0x70) << 4) |
677 ((p[2] & 0x7f) << 1) |
678 (p[4] & 0x01);
679 } else {
680 f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
681 ((p[0] & 0x30) >> 4);
682 f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
683 f->pressure = p[5] & 0x7f;
685 alps_decode_buttons_v3(f, p);
688 return 0;
691 static int alps_decode_dolphin(struct alps_fields *f, unsigned char *p,
692 struct psmouse *psmouse)
694 u64 palm_data = 0;
695 struct alps_data *priv = psmouse->private;
697 f->first_mp = !!(p[0] & 0x02);
698 f->is_mp = !!(p[0] & 0x20);
700 if (!f->is_mp) {
701 f->st.x = ((p[1] & 0x7f) | ((p[4] & 0x0f) << 7));
702 f->st.y = ((p[2] & 0x7f) | ((p[4] & 0xf0) << 3));
703 f->pressure = (p[0] & 4) ? 0 : p[5] & 0x7f;
704 alps_decode_buttons_v3(f, p);
705 } else {
706 f->fingers = ((p[0] & 0x6) >> 1 |
707 (p[0] & 0x10) >> 2);
709 palm_data = (p[1] & 0x7f) |
710 ((p[2] & 0x7f) << 7) |
711 ((p[4] & 0x7f) << 14) |
712 ((p[5] & 0x7f) << 21) |
713 ((p[3] & 0x07) << 28) |
714 (((u64)p[3] & 0x70) << 27) |
715 (((u64)p[0] & 0x01) << 34);
717 /* Y-profile is stored in P(0) to p(n-1), n = y_bits; */
718 f->y_map = palm_data & (BIT(priv->y_bits) - 1);
720 /* X-profile is stored in p(n) to p(n+m-1), m = x_bits; */
721 f->x_map = (palm_data >> priv->y_bits) &
722 (BIT(priv->x_bits) - 1);
725 return 0;
728 static void alps_process_touchpad_packet_v3_v5(struct psmouse *psmouse)
730 struct alps_data *priv = psmouse->private;
731 unsigned char *packet = psmouse->packet;
732 struct input_dev *dev2 = priv->dev2;
733 struct alps_fields *f = &priv->f;
734 int fingers = 0;
736 memset(f, 0, sizeof(*f));
738 priv->decode_fields(f, packet, psmouse);
741 * There's no single feature of touchpad position and bitmap packets
742 * that can be used to distinguish between them. We rely on the fact
743 * that a bitmap packet should always follow a position packet with
744 * bit 6 of packet[4] set.
746 if (priv->multi_packet) {
748 * Sometimes a position packet will indicate a multi-packet
749 * sequence, but then what follows is another position
750 * packet. Check for this, and when it happens process the
751 * position packet as usual.
753 if (f->is_mp) {
754 fingers = f->fingers;
756 * Bitmap processing uses position packet's coordinate
757 * data, so we need to do decode it first.
759 priv->decode_fields(f, priv->multi_data, psmouse);
760 if (alps_process_bitmap(priv, f) == 0)
761 fingers = 0; /* Use st data */
762 } else {
763 priv->multi_packet = 0;
768 * Bit 6 of byte 0 is not usually set in position packets. The only
769 * times it seems to be set is in situations where the data is
770 * suspect anyway, e.g. a palm resting flat on the touchpad. Given
771 * this combined with the fact that this bit is useful for filtering
772 * out misidentified bitmap packets, we reject anything with this
773 * bit set.
775 if (f->is_mp)
776 return;
778 if (!priv->multi_packet && f->first_mp) {
779 priv->multi_packet = 1;
780 memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
781 return;
784 priv->multi_packet = 0;
787 * Sometimes the hardware sends a single packet with z = 0
788 * in the middle of a stream. Real releases generate packets
789 * with x, y, and z all zero, so these seem to be flukes.
790 * Ignore them.
792 if (f->st.x && f->st.y && !f->pressure)
793 return;
795 alps_report_semi_mt_data(psmouse, fingers);
797 if ((priv->flags & ALPS_DUALPOINT) &&
798 !(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) {
799 input_report_key(dev2, BTN_LEFT, f->ts_left);
800 input_report_key(dev2, BTN_RIGHT, f->ts_right);
801 input_report_key(dev2, BTN_MIDDLE, f->ts_middle);
802 input_sync(dev2);
806 static void alps_process_packet_v3(struct psmouse *psmouse)
808 unsigned char *packet = psmouse->packet;
811 * v3 protocol packets come in three types, two representing
812 * touchpad data and one representing trackstick data.
813 * Trackstick packets seem to be distinguished by always
814 * having 0x3f in the last byte. This value has never been
815 * observed in the last byte of either of the other types
816 * of packets.
818 if (packet[5] == 0x3f) {
819 alps_process_trackstick_packet_v3(psmouse);
820 return;
823 alps_process_touchpad_packet_v3_v5(psmouse);
826 static void alps_process_packet_v6(struct psmouse *psmouse)
828 struct alps_data *priv = psmouse->private;
829 unsigned char *packet = psmouse->packet;
830 struct input_dev *dev = psmouse->dev;
831 struct input_dev *dev2 = priv->dev2;
832 int x, y, z;
835 * We can use Byte5 to distinguish if the packet is from Touchpad
836 * or Trackpoint.
837 * Touchpad: 0 - 0x7E
838 * Trackpoint: 0x7F
840 if (packet[5] == 0x7F) {
841 /* It should be a DualPoint when received Trackpoint packet */
842 if (!(priv->flags & ALPS_DUALPOINT)) {
843 psmouse_warn(psmouse,
844 "Rejected trackstick packet from non DualPoint device");
845 return;
848 /* Trackpoint packet */
849 x = packet[1] | ((packet[3] & 0x20) << 2);
850 y = packet[2] | ((packet[3] & 0x40) << 1);
851 z = packet[4];
853 /* To prevent the cursor jump when finger lifted */
854 if (x == 0x7F && y == 0x7F && z == 0x7F)
855 x = y = z = 0;
857 /* Divide 4 since trackpoint's speed is too fast */
858 input_report_rel(dev2, REL_X, (char)x / 4);
859 input_report_rel(dev2, REL_Y, -((char)y / 4));
861 psmouse_report_standard_buttons(dev2, packet[3]);
863 input_sync(dev2);
864 return;
867 /* Touchpad packet */
868 x = packet[1] | ((packet[3] & 0x78) << 4);
869 y = packet[2] | ((packet[4] & 0x78) << 4);
870 z = packet[5];
872 if (z > 30)
873 input_report_key(dev, BTN_TOUCH, 1);
874 if (z < 25)
875 input_report_key(dev, BTN_TOUCH, 0);
877 if (z > 0) {
878 input_report_abs(dev, ABS_X, x);
879 input_report_abs(dev, ABS_Y, y);
882 input_report_abs(dev, ABS_PRESSURE, z);
883 input_report_key(dev, BTN_TOOL_FINGER, z > 0);
885 /* v6 touchpad does not have middle button */
886 packet[3] &= ~BIT(2);
887 psmouse_report_standard_buttons(dev2, packet[3]);
889 input_sync(dev);
892 static void alps_process_packet_v4(struct psmouse *psmouse)
894 struct alps_data *priv = psmouse->private;
895 unsigned char *packet = psmouse->packet;
896 struct alps_fields *f = &priv->f;
897 int offset;
900 * v4 has a 6-byte encoding for bitmap data, but this data is
901 * broken up between 3 normal packets. Use priv->multi_packet to
902 * track our position in the bitmap packet.
904 if (packet[6] & 0x40) {
905 /* sync, reset position */
906 priv->multi_packet = 0;
909 if (WARN_ON_ONCE(priv->multi_packet > 2))
910 return;
912 offset = 2 * priv->multi_packet;
913 priv->multi_data[offset] = packet[6];
914 priv->multi_data[offset + 1] = packet[7];
916 f->left = !!(packet[4] & 0x01);
917 f->right = !!(packet[4] & 0x02);
919 f->st.x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) |
920 ((packet[0] & 0x30) >> 4);
921 f->st.y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f);
922 f->pressure = packet[5] & 0x7f;
924 if (++priv->multi_packet > 2) {
925 priv->multi_packet = 0;
927 f->x_map = ((priv->multi_data[2] & 0x1f) << 10) |
928 ((priv->multi_data[3] & 0x60) << 3) |
929 ((priv->multi_data[0] & 0x3f) << 2) |
930 ((priv->multi_data[1] & 0x60) >> 5);
931 f->y_map = ((priv->multi_data[5] & 0x01) << 10) |
932 ((priv->multi_data[3] & 0x1f) << 5) |
933 (priv->multi_data[1] & 0x1f);
935 f->fingers = alps_process_bitmap(priv, f);
938 alps_report_semi_mt_data(psmouse, f->fingers);
941 static bool alps_is_valid_package_v7(struct psmouse *psmouse)
943 switch (psmouse->pktcnt) {
944 case 3:
945 return (psmouse->packet[2] & 0x40) == 0x40;
946 case 4:
947 return (psmouse->packet[3] & 0x48) == 0x48;
948 case 6:
949 return (psmouse->packet[5] & 0x40) == 0x00;
951 return true;
954 static unsigned char alps_get_packet_id_v7(char *byte)
956 unsigned char packet_id;
958 if (byte[4] & 0x40)
959 packet_id = V7_PACKET_ID_TWO;
960 else if (byte[4] & 0x01)
961 packet_id = V7_PACKET_ID_MULTI;
962 else if ((byte[0] & 0x10) && !(byte[4] & 0x43))
963 packet_id = V7_PACKET_ID_NEW;
964 else if (byte[1] == 0x00 && byte[4] == 0x00)
965 packet_id = V7_PACKET_ID_IDLE;
966 else
967 packet_id = V7_PACKET_ID_UNKNOWN;
969 return packet_id;
972 static void alps_get_finger_coordinate_v7(struct input_mt_pos *mt,
973 unsigned char *pkt,
974 unsigned char pkt_id)
976 mt[0].x = ((pkt[2] & 0x80) << 4);
977 mt[0].x |= ((pkt[2] & 0x3F) << 5);
978 mt[0].x |= ((pkt[3] & 0x30) >> 1);
979 mt[0].x |= (pkt[3] & 0x07);
980 mt[0].y = (pkt[1] << 3) | (pkt[0] & 0x07);
982 mt[1].x = ((pkt[3] & 0x80) << 4);
983 mt[1].x |= ((pkt[4] & 0x80) << 3);
984 mt[1].x |= ((pkt[4] & 0x3F) << 4);
985 mt[1].y = ((pkt[5] & 0x80) << 3);
986 mt[1].y |= ((pkt[5] & 0x3F) << 4);
988 switch (pkt_id) {
989 case V7_PACKET_ID_TWO:
990 mt[1].x &= ~0x000F;
991 mt[1].y |= 0x000F;
992 /* Detect false-postive touches where x & y report max value */
993 if (mt[1].y == 0x7ff && mt[1].x == 0xff0) {
994 mt[1].x = 0;
995 /* y gets set to 0 at the end of this function */
997 break;
999 case V7_PACKET_ID_MULTI:
1000 mt[1].x &= ~0x003F;
1001 mt[1].y &= ~0x0020;
1002 mt[1].y |= ((pkt[4] & 0x02) << 4);
1003 mt[1].y |= 0x001F;
1004 break;
1006 case V7_PACKET_ID_NEW:
1007 mt[1].x &= ~0x003F;
1008 mt[1].x |= (pkt[0] & 0x20);
1009 mt[1].y |= 0x000F;
1010 break;
1013 mt[0].y = 0x7FF - mt[0].y;
1014 mt[1].y = 0x7FF - mt[1].y;
1017 static int alps_get_mt_count(struct input_mt_pos *mt)
1019 int i, fingers = 0;
1021 for (i = 0; i < MAX_TOUCHES; i++) {
1022 if (mt[i].x != 0 || mt[i].y != 0)
1023 fingers++;
1026 return fingers;
1029 static int alps_decode_packet_v7(struct alps_fields *f,
1030 unsigned char *p,
1031 struct psmouse *psmouse)
1033 struct alps_data *priv = psmouse->private;
1034 unsigned char pkt_id;
1036 pkt_id = alps_get_packet_id_v7(p);
1037 if (pkt_id == V7_PACKET_ID_IDLE)
1038 return 0;
1039 if (pkt_id == V7_PACKET_ID_UNKNOWN)
1040 return -1;
1042 * NEW packets are send to indicate a discontinuity in the finger
1043 * coordinate reporting. Specifically a finger may have moved from
1044 * slot 0 to 1 or vice versa. INPUT_MT_TRACK takes care of this for
1045 * us.
1047 * NEW packets have 3 problems:
1048 * 1) They do not contain middle / right button info (on non clickpads)
1049 * this can be worked around by preserving the old button state
1050 * 2) They do not contain an accurate fingercount, and they are
1051 * typically send when the number of fingers changes. We cannot use
1052 * the old finger count as that may mismatch with the amount of
1053 * touch coordinates we've available in the NEW packet
1054 * 3) Their x data for the second touch is inaccurate leading to
1055 * a possible jump of the x coordinate by 16 units when the first
1056 * non NEW packet comes in
1057 * Since problems 2 & 3 cannot be worked around, just ignore them.
1059 if (pkt_id == V7_PACKET_ID_NEW)
1060 return 1;
1062 alps_get_finger_coordinate_v7(f->mt, p, pkt_id);
1064 if (pkt_id == V7_PACKET_ID_TWO)
1065 f->fingers = alps_get_mt_count(f->mt);
1066 else /* pkt_id == V7_PACKET_ID_MULTI */
1067 f->fingers = 3 + (p[5] & 0x03);
1069 f->left = (p[0] & 0x80) >> 7;
1070 if (priv->flags & ALPS_BUTTONPAD) {
1071 if (p[0] & 0x20)
1072 f->fingers++;
1073 if (p[0] & 0x10)
1074 f->fingers++;
1075 } else {
1076 f->right = (p[0] & 0x20) >> 5;
1077 f->middle = (p[0] & 0x10) >> 4;
1080 /* Sometimes a single touch is reported in mt[1] rather then mt[0] */
1081 if (f->fingers == 1 && f->mt[0].x == 0 && f->mt[0].y == 0) {
1082 f->mt[0].x = f->mt[1].x;
1083 f->mt[0].y = f->mt[1].y;
1084 f->mt[1].x = 0;
1085 f->mt[1].y = 0;
1088 return 0;
1091 static void alps_process_trackstick_packet_v7(struct psmouse *psmouse)
1093 struct alps_data *priv = psmouse->private;
1094 unsigned char *packet = psmouse->packet;
1095 struct input_dev *dev2 = priv->dev2;
1096 int x, y, z;
1098 /* It should be a DualPoint when received trackstick packet */
1099 if (!(priv->flags & ALPS_DUALPOINT)) {
1100 psmouse_warn(psmouse,
1101 "Rejected trackstick packet from non DualPoint device");
1102 return;
1105 x = ((packet[2] & 0xbf)) | ((packet[3] & 0x10) << 2);
1106 y = (packet[3] & 0x07) | (packet[4] & 0xb8) |
1107 ((packet[3] & 0x20) << 1);
1108 z = (packet[5] & 0x3f) | ((packet[3] & 0x80) >> 1);
1110 input_report_rel(dev2, REL_X, (char)x);
1111 input_report_rel(dev2, REL_Y, -((char)y));
1112 input_report_abs(dev2, ABS_PRESSURE, z);
1114 psmouse_report_standard_buttons(dev2, packet[1]);
1116 input_sync(dev2);
1119 static void alps_process_touchpad_packet_v7(struct psmouse *psmouse)
1121 struct alps_data *priv = psmouse->private;
1122 struct input_dev *dev = psmouse->dev;
1123 struct alps_fields *f = &priv->f;
1125 memset(f, 0, sizeof(*f));
1127 if (priv->decode_fields(f, psmouse->packet, psmouse))
1128 return;
1130 alps_report_mt_data(psmouse, alps_get_mt_count(f->mt));
1132 input_mt_report_finger_count(dev, f->fingers);
1134 input_report_key(dev, BTN_LEFT, f->left);
1135 input_report_key(dev, BTN_RIGHT, f->right);
1136 input_report_key(dev, BTN_MIDDLE, f->middle);
1138 input_sync(dev);
1141 static void alps_process_packet_v7(struct psmouse *psmouse)
1143 unsigned char *packet = psmouse->packet;
1145 if (packet[0] == 0x48 && (packet[4] & 0x47) == 0x06)
1146 alps_process_trackstick_packet_v7(psmouse);
1147 else
1148 alps_process_touchpad_packet_v7(psmouse);
1151 static enum SS4_PACKET_ID alps_get_pkt_id_ss4_v2(unsigned char *byte)
1153 enum SS4_PACKET_ID pkt_id = SS4_PACKET_ID_IDLE;
1155 switch (byte[3] & 0x30) {
1156 case 0x00:
1157 if (SS4_IS_IDLE_V2(byte)) {
1158 pkt_id = SS4_PACKET_ID_IDLE;
1159 } else {
1160 pkt_id = SS4_PACKET_ID_ONE;
1162 break;
1163 case 0x10:
1164 /* two-finger finger positions */
1165 pkt_id = SS4_PACKET_ID_TWO;
1166 break;
1167 case 0x20:
1168 /* stick pointer */
1169 pkt_id = SS4_PACKET_ID_STICK;
1170 break;
1171 case 0x30:
1172 /* third and fourth finger positions */
1173 pkt_id = SS4_PACKET_ID_MULTI;
1174 break;
1177 return pkt_id;
1180 static int alps_decode_ss4_v2(struct alps_fields *f,
1181 unsigned char *p, struct psmouse *psmouse)
1183 struct alps_data *priv = psmouse->private;
1184 enum SS4_PACKET_ID pkt_id;
1185 unsigned int no_data_x, no_data_y;
1187 pkt_id = alps_get_pkt_id_ss4_v2(p);
1189 /* Current packet is 1Finger coordinate packet */
1190 switch (pkt_id) {
1191 case SS4_PACKET_ID_ONE:
1192 f->mt[0].x = SS4_1F_X_V2(p);
1193 f->mt[0].y = SS4_1F_Y_V2(p);
1194 f->pressure = ((SS4_1F_Z_V2(p)) * 2) & 0x7f;
1196 * When a button is held the device will give us events
1197 * with x, y, and pressure of 0. This causes annoying jumps
1198 * if a touch is released while the button is held.
1199 * Handle this by claiming zero contacts.
1201 f->fingers = f->pressure > 0 ? 1 : 0;
1202 f->first_mp = 0;
1203 f->is_mp = 0;
1204 break;
1206 case SS4_PACKET_ID_TWO:
1207 if (priv->flags & ALPS_BUTTONPAD) {
1208 if (IS_SS4PLUS_DEV(priv->dev_id)) {
1209 f->mt[0].x = SS4_PLUS_BTL_MF_X_V2(p, 0);
1210 f->mt[1].x = SS4_PLUS_BTL_MF_X_V2(p, 1);
1211 } else {
1212 f->mt[0].x = SS4_BTL_MF_X_V2(p, 0);
1213 f->mt[1].x = SS4_BTL_MF_X_V2(p, 1);
1215 f->mt[0].y = SS4_BTL_MF_Y_V2(p, 0);
1216 f->mt[1].y = SS4_BTL_MF_Y_V2(p, 1);
1217 } else {
1218 if (IS_SS4PLUS_DEV(priv->dev_id)) {
1219 f->mt[0].x = SS4_PLUS_STD_MF_X_V2(p, 0);
1220 f->mt[1].x = SS4_PLUS_STD_MF_X_V2(p, 1);
1221 } else {
1222 f->mt[0].x = SS4_STD_MF_X_V2(p, 0);
1223 f->mt[1].x = SS4_STD_MF_X_V2(p, 1);
1225 f->mt[0].y = SS4_STD_MF_Y_V2(p, 0);
1226 f->mt[1].y = SS4_STD_MF_Y_V2(p, 1);
1228 f->pressure = SS4_MF_Z_V2(p, 0) ? 0x30 : 0;
1230 if (SS4_IS_MF_CONTINUE(p)) {
1231 f->first_mp = 1;
1232 } else {
1233 f->fingers = 2;
1234 f->first_mp = 0;
1236 f->is_mp = 0;
1238 break;
1240 case SS4_PACKET_ID_MULTI:
1241 if (priv->flags & ALPS_BUTTONPAD) {
1242 if (IS_SS4PLUS_DEV(priv->dev_id)) {
1243 f->mt[2].x = SS4_PLUS_BTL_MF_X_V2(p, 0);
1244 f->mt[3].x = SS4_PLUS_BTL_MF_X_V2(p, 1);
1245 no_data_x = SS4_PLUS_MFPACKET_NO_AX_BL;
1246 } else {
1247 f->mt[2].x = SS4_BTL_MF_X_V2(p, 0);
1248 f->mt[3].x = SS4_BTL_MF_X_V2(p, 1);
1249 no_data_x = SS4_MFPACKET_NO_AX_BL;
1251 no_data_y = SS4_MFPACKET_NO_AY_BL;
1253 f->mt[2].y = SS4_BTL_MF_Y_V2(p, 0);
1254 f->mt[3].y = SS4_BTL_MF_Y_V2(p, 1);
1255 } else {
1256 if (IS_SS4PLUS_DEV(priv->dev_id)) {
1257 f->mt[2].x = SS4_PLUS_STD_MF_X_V2(p, 0);
1258 f->mt[3].x = SS4_PLUS_STD_MF_X_V2(p, 1);
1259 no_data_x = SS4_PLUS_MFPACKET_NO_AX;
1260 } else {
1261 f->mt[2].x = SS4_STD_MF_X_V2(p, 0);
1262 f->mt[3].x = SS4_STD_MF_X_V2(p, 1);
1263 no_data_x = SS4_MFPACKET_NO_AX;
1265 no_data_y = SS4_MFPACKET_NO_AY;
1267 f->mt[2].y = SS4_STD_MF_Y_V2(p, 0);
1268 f->mt[3].y = SS4_STD_MF_Y_V2(p, 1);
1271 f->first_mp = 0;
1272 f->is_mp = 1;
1274 if (SS4_IS_5F_DETECTED(p)) {
1275 f->fingers = 5;
1276 } else if (f->mt[3].x == no_data_x &&
1277 f->mt[3].y == no_data_y) {
1278 f->mt[3].x = 0;
1279 f->mt[3].y = 0;
1280 f->fingers = 3;
1281 } else {
1282 f->fingers = 4;
1284 break;
1286 case SS4_PACKET_ID_STICK:
1288 * x, y, and pressure are decoded in
1289 * alps_process_packet_ss4_v2()
1291 f->first_mp = 0;
1292 f->is_mp = 0;
1293 break;
1295 case SS4_PACKET_ID_IDLE:
1296 default:
1297 memset(f, 0, sizeof(struct alps_fields));
1298 break;
1301 /* handle buttons */
1302 if (pkt_id == SS4_PACKET_ID_STICK) {
1303 f->ts_left = !!(SS4_BTN_V2(p) & 0x01);
1304 f->ts_right = !!(SS4_BTN_V2(p) & 0x02);
1305 f->ts_middle = !!(SS4_BTN_V2(p) & 0x04);
1306 } else {
1307 f->left = !!(SS4_BTN_V2(p) & 0x01);
1308 if (!(priv->flags & ALPS_BUTTONPAD)) {
1309 f->right = !!(SS4_BTN_V2(p) & 0x02);
1310 f->middle = !!(SS4_BTN_V2(p) & 0x04);
1314 return 0;
1317 static void alps_process_packet_ss4_v2(struct psmouse *psmouse)
1319 struct alps_data *priv = psmouse->private;
1320 unsigned char *packet = psmouse->packet;
1321 struct input_dev *dev = psmouse->dev;
1322 struct input_dev *dev2 = priv->dev2;
1323 struct alps_fields *f = &priv->f;
1325 memset(f, 0, sizeof(struct alps_fields));
1326 priv->decode_fields(f, packet, psmouse);
1327 if (priv->multi_packet) {
1329 * Sometimes the first packet will indicate a multi-packet
1330 * sequence, but sometimes the next multi-packet would not
1331 * come. Check for this, and when it happens process the
1332 * position packet as usual.
1334 if (f->is_mp) {
1335 /* Now process the 1st packet */
1336 priv->decode_fields(f, priv->multi_data, psmouse);
1337 } else {
1338 priv->multi_packet = 0;
1343 * "f.is_mp" would always be '0' after merging the 1st and 2nd packet.
1344 * When it is set, it means 2nd packet comes without 1st packet come.
1346 if (f->is_mp)
1347 return;
1349 /* Save the first packet */
1350 if (!priv->multi_packet && f->first_mp) {
1351 priv->multi_packet = 1;
1352 memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
1353 return;
1356 priv->multi_packet = 0;
1358 /* Report trackstick */
1359 if (alps_get_pkt_id_ss4_v2(packet) == SS4_PACKET_ID_STICK) {
1360 if (!(priv->flags & ALPS_DUALPOINT)) {
1361 psmouse_warn(psmouse,
1362 "Rejected trackstick packet from non DualPoint device");
1363 return;
1366 input_report_rel(dev2, REL_X, SS4_TS_X_V2(packet));
1367 input_report_rel(dev2, REL_Y, SS4_TS_Y_V2(packet));
1368 input_report_abs(dev2, ABS_PRESSURE, SS4_TS_Z_V2(packet));
1370 input_report_key(dev2, BTN_LEFT, f->ts_left);
1371 input_report_key(dev2, BTN_RIGHT, f->ts_right);
1372 input_report_key(dev2, BTN_MIDDLE, f->ts_middle);
1374 input_sync(dev2);
1375 return;
1378 /* Report touchpad */
1379 alps_report_mt_data(psmouse, (f->fingers <= 4) ? f->fingers : 4);
1381 input_mt_report_finger_count(dev, f->fingers);
1383 input_report_key(dev, BTN_LEFT, f->left);
1384 input_report_key(dev, BTN_RIGHT, f->right);
1385 input_report_key(dev, BTN_MIDDLE, f->middle);
1387 input_report_abs(dev, ABS_PRESSURE, f->pressure);
1388 input_sync(dev);
1391 static bool alps_is_valid_package_ss4_v2(struct psmouse *psmouse)
1393 if (psmouse->pktcnt == 4 && ((psmouse->packet[3] & 0x08) != 0x08))
1394 return false;
1395 if (psmouse->pktcnt == 6 && ((psmouse->packet[5] & 0x10) != 0x0))
1396 return false;
1397 return true;
1400 static DEFINE_MUTEX(alps_mutex);
1402 static void alps_register_bare_ps2_mouse(struct work_struct *work)
1404 struct alps_data *priv =
1405 container_of(work, struct alps_data, dev3_register_work.work);
1406 struct psmouse *psmouse = priv->psmouse;
1407 struct input_dev *dev3;
1408 int error = 0;
1410 mutex_lock(&alps_mutex);
1412 if (priv->dev3)
1413 goto out;
1415 dev3 = input_allocate_device();
1416 if (!dev3) {
1417 psmouse_err(psmouse, "failed to allocate secondary device\n");
1418 error = -ENOMEM;
1419 goto out;
1422 snprintf(priv->phys3, sizeof(priv->phys3), "%s/%s",
1423 psmouse->ps2dev.serio->phys,
1424 (priv->dev2 ? "input2" : "input1"));
1425 dev3->phys = priv->phys3;
1428 * format of input device name is: "protocol vendor name"
1429 * see function psmouse_switch_protocol() in psmouse-base.c
1431 dev3->name = "PS/2 ALPS Mouse";
1433 dev3->id.bustype = BUS_I8042;
1434 dev3->id.vendor = 0x0002;
1435 dev3->id.product = PSMOUSE_PS2;
1436 dev3->id.version = 0x0000;
1437 dev3->dev.parent = &psmouse->ps2dev.serio->dev;
1439 input_set_capability(dev3, EV_REL, REL_X);
1440 input_set_capability(dev3, EV_REL, REL_Y);
1441 input_set_capability(dev3, EV_KEY, BTN_LEFT);
1442 input_set_capability(dev3, EV_KEY, BTN_RIGHT);
1443 input_set_capability(dev3, EV_KEY, BTN_MIDDLE);
1445 __set_bit(INPUT_PROP_POINTER, dev3->propbit);
1447 error = input_register_device(dev3);
1448 if (error) {
1449 psmouse_err(psmouse,
1450 "failed to register secondary device: %d\n",
1451 error);
1452 input_free_device(dev3);
1453 goto out;
1456 priv->dev3 = dev3;
1458 out:
1460 * Save the error code so that we can detect that we
1461 * already tried to create the device.
1463 if (error)
1464 priv->dev3 = ERR_PTR(error);
1466 mutex_unlock(&alps_mutex);
1469 static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
1470 unsigned char packet[],
1471 bool report_buttons)
1473 struct alps_data *priv = psmouse->private;
1474 struct input_dev *dev, *dev2 = NULL;
1476 /* Figure out which device to use to report the bare packet */
1477 if (priv->proto_version == ALPS_PROTO_V2 &&
1478 (priv->flags & ALPS_DUALPOINT)) {
1479 /* On V2 devices the DualPoint Stick reports bare packets */
1480 dev = priv->dev2;
1481 dev2 = psmouse->dev;
1482 } else if (unlikely(IS_ERR_OR_NULL(priv->dev3))) {
1483 /* Register dev3 mouse if we received PS/2 packet first time */
1484 if (!IS_ERR(priv->dev3))
1485 psmouse_queue_work(psmouse, &priv->dev3_register_work,
1487 return;
1488 } else {
1489 dev = priv->dev3;
1492 if (report_buttons)
1493 alps_report_buttons(dev, dev2,
1494 packet[0] & 1, packet[0] & 2, packet[0] & 4);
1496 psmouse_report_standard_motion(dev, packet);
1498 input_sync(dev);
1501 static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
1503 struct alps_data *priv = psmouse->private;
1505 if (psmouse->pktcnt < 6)
1506 return PSMOUSE_GOOD_DATA;
1508 if (psmouse->pktcnt == 6) {
1510 * Start a timer to flush the packet if it ends up last
1511 * 6-byte packet in the stream. Timer needs to fire
1512 * psmouse core times out itself. 20 ms should be enough
1513 * to decide if we are getting more data or not.
1515 mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
1516 return PSMOUSE_GOOD_DATA;
1519 del_timer(&priv->timer);
1521 if (psmouse->packet[6] & 0x80) {
1524 * Highest bit is set - that means we either had
1525 * complete ALPS packet and this is start of the
1526 * next packet or we got garbage.
1529 if (((psmouse->packet[3] |
1530 psmouse->packet[4] |
1531 psmouse->packet[5]) & 0x80) ||
1532 (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) {
1533 psmouse_dbg(psmouse,
1534 "refusing packet %4ph (suspected interleaved ps/2)\n",
1535 psmouse->packet + 3);
1536 return PSMOUSE_BAD_DATA;
1539 priv->process_packet(psmouse);
1541 /* Continue with the next packet */
1542 psmouse->packet[0] = psmouse->packet[6];
1543 psmouse->pktcnt = 1;
1545 } else {
1548 * High bit is 0 - that means that we indeed got a PS/2
1549 * packet in the middle of ALPS packet.
1551 * There is also possibility that we got 6-byte ALPS
1552 * packet followed by 3-byte packet from trackpoint. We
1553 * can not distinguish between these 2 scenarios but
1554 * because the latter is unlikely to happen in course of
1555 * normal operation (user would need to press all
1556 * buttons on the pad and start moving trackpoint
1557 * without touching the pad surface) we assume former.
1558 * Even if we are wrong the wost thing that would happen
1559 * the cursor would jump but we should not get protocol
1560 * de-synchronization.
1563 alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
1564 false);
1567 * Continue with the standard ALPS protocol handling,
1568 * but make sure we won't process it as an interleaved
1569 * packet again, which may happen if all buttons are
1570 * pressed. To avoid this let's reset the 4th bit which
1571 * is normally 1.
1573 psmouse->packet[3] = psmouse->packet[6] & 0xf7;
1574 psmouse->pktcnt = 4;
1577 return PSMOUSE_GOOD_DATA;
1580 static void alps_flush_packet(struct timer_list *t)
1582 struct alps_data *priv = from_timer(priv, t, timer);
1583 struct psmouse *psmouse = priv->psmouse;
1585 serio_pause_rx(psmouse->ps2dev.serio);
1587 if (psmouse->pktcnt == psmouse->pktsize) {
1590 * We did not any more data in reasonable amount of time.
1591 * Validate the last 3 bytes and process as a standard
1592 * ALPS packet.
1594 if ((psmouse->packet[3] |
1595 psmouse->packet[4] |
1596 psmouse->packet[5]) & 0x80) {
1597 psmouse_dbg(psmouse,
1598 "refusing packet %3ph (suspected interleaved ps/2)\n",
1599 psmouse->packet + 3);
1600 } else {
1601 priv->process_packet(psmouse);
1603 psmouse->pktcnt = 0;
1606 serio_continue_rx(psmouse->ps2dev.serio);
1609 static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
1611 struct alps_data *priv = psmouse->private;
1614 * Check if we are dealing with a bare PS/2 packet, presumably from
1615 * a device connected to the external PS/2 port. Because bare PS/2
1616 * protocol does not have enough constant bits to self-synchronize
1617 * properly we only do this if the device is fully synchronized.
1618 * Can not distinguish V8's first byte from PS/2 packet's
1620 if (priv->proto_version != ALPS_PROTO_V8 &&
1621 !psmouse->out_of_sync_cnt &&
1622 (psmouse->packet[0] & 0xc8) == 0x08) {
1624 if (psmouse->pktcnt == 3) {
1625 alps_report_bare_ps2_packet(psmouse, psmouse->packet,
1626 true);
1627 return PSMOUSE_FULL_PACKET;
1629 return PSMOUSE_GOOD_DATA;
1632 /* Check for PS/2 packet stuffed in the middle of ALPS packet. */
1634 if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
1635 psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
1636 return alps_handle_interleaved_ps2(psmouse);
1639 if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
1640 psmouse_dbg(psmouse,
1641 "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
1642 psmouse->packet[0], priv->mask0, priv->byte0);
1643 return PSMOUSE_BAD_DATA;
1646 /* Bytes 2 - pktsize should have 0 in the highest bit */
1647 if (priv->proto_version < ALPS_PROTO_V5 &&
1648 psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
1649 (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
1650 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1651 psmouse->pktcnt - 1,
1652 psmouse->packet[psmouse->pktcnt - 1]);
1654 if (priv->proto_version == ALPS_PROTO_V3_RUSHMORE &&
1655 psmouse->pktcnt == psmouse->pktsize) {
1657 * Some Dell boxes, such as Latitude E6440 or E7440
1658 * with closed lid, quite often smash last byte of
1659 * otherwise valid packet with 0xff. Given that the
1660 * next packet is very likely to be valid let's
1661 * report PSMOUSE_FULL_PACKET but not process data,
1662 * rather than reporting PSMOUSE_BAD_DATA and
1663 * filling the logs.
1665 return PSMOUSE_FULL_PACKET;
1668 return PSMOUSE_BAD_DATA;
1671 if ((priv->proto_version == ALPS_PROTO_V7 &&
1672 !alps_is_valid_package_v7(psmouse)) ||
1673 (priv->proto_version == ALPS_PROTO_V8 &&
1674 !alps_is_valid_package_ss4_v2(psmouse))) {
1675 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1676 psmouse->pktcnt - 1,
1677 psmouse->packet[psmouse->pktcnt - 1]);
1678 return PSMOUSE_BAD_DATA;
1681 if (psmouse->pktcnt == psmouse->pktsize) {
1682 priv->process_packet(psmouse);
1683 return PSMOUSE_FULL_PACKET;
1686 return PSMOUSE_GOOD_DATA;
1689 static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
1691 struct ps2dev *ps2dev = &psmouse->ps2dev;
1692 struct alps_data *priv = psmouse->private;
1693 int command;
1694 unsigned char *param;
1695 unsigned char dummy[4];
1697 BUG_ON(nibble > 0xf);
1699 command = priv->nibble_commands[nibble].command;
1700 param = (command & 0x0f00) ?
1701 dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
1703 if (ps2_command(ps2dev, param, command))
1704 return -1;
1706 return 0;
1709 static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
1711 struct ps2dev *ps2dev = &psmouse->ps2dev;
1712 struct alps_data *priv = psmouse->private;
1713 int i, nibble;
1715 if (ps2_command(ps2dev, NULL, priv->addr_command))
1716 return -1;
1718 for (i = 12; i >= 0; i -= 4) {
1719 nibble = (addr >> i) & 0xf;
1720 if (alps_command_mode_send_nibble(psmouse, nibble))
1721 return -1;
1724 return 0;
1727 static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1729 struct ps2dev *ps2dev = &psmouse->ps2dev;
1730 unsigned char param[4];
1732 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1733 return -1;
1736 * The address being read is returned in the first two bytes
1737 * of the result. Check that this address matches the expected
1738 * address.
1740 if (addr != ((param[0] << 8) | param[1]))
1741 return -1;
1743 return param[2];
1746 static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1748 if (alps_command_mode_set_addr(psmouse, addr))
1749 return -1;
1750 return __alps_command_mode_read_reg(psmouse, addr);
1753 static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
1755 if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
1756 return -1;
1757 if (alps_command_mode_send_nibble(psmouse, value & 0xf))
1758 return -1;
1759 return 0;
1762 static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
1763 u8 value)
1765 if (alps_command_mode_set_addr(psmouse, addr))
1766 return -1;
1767 return __alps_command_mode_write_reg(psmouse, value);
1770 static int alps_rpt_cmd(struct psmouse *psmouse, int init_command,
1771 int repeated_command, unsigned char *param)
1773 struct ps2dev *ps2dev = &psmouse->ps2dev;
1775 param[0] = 0;
1776 if (init_command && ps2_command(ps2dev, param, init_command))
1777 return -EIO;
1779 if (ps2_command(ps2dev, NULL, repeated_command) ||
1780 ps2_command(ps2dev, NULL, repeated_command) ||
1781 ps2_command(ps2dev, NULL, repeated_command))
1782 return -EIO;
1784 param[0] = param[1] = param[2] = 0xff;
1785 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1786 return -EIO;
1788 psmouse_dbg(psmouse, "%2.2X report: %3ph\n",
1789 repeated_command, param);
1790 return 0;
1793 static bool alps_check_valid_firmware_id(unsigned char id[])
1795 if (id[0] == 0x73)
1796 return true;
1798 if (id[0] == 0x88 &&
1799 (id[1] == 0x07 ||
1800 id[1] == 0x08 ||
1801 (id[1] & 0xf0) == 0xb0 ||
1802 (id[1] & 0xf0) == 0xc0)) {
1803 return true;
1806 return false;
1809 static int alps_enter_command_mode(struct psmouse *psmouse)
1811 unsigned char param[4];
1813 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) {
1814 psmouse_err(psmouse, "failed to enter command mode\n");
1815 return -1;
1818 if (!alps_check_valid_firmware_id(param)) {
1819 psmouse_dbg(psmouse,
1820 "unknown response while entering command mode\n");
1821 return -1;
1823 return 0;
1826 static inline int alps_exit_command_mode(struct psmouse *psmouse)
1828 struct ps2dev *ps2dev = &psmouse->ps2dev;
1829 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
1830 return -1;
1831 return 0;
1835 * For DualPoint devices select the device that should respond to
1836 * subsequent commands. It looks like glidepad is behind stickpointer,
1837 * I'd thought it would be other way around...
1839 static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
1841 struct ps2dev *ps2dev = &psmouse->ps2dev;
1842 int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
1844 if (ps2_command(ps2dev, NULL, cmd) ||
1845 ps2_command(ps2dev, NULL, cmd) ||
1846 ps2_command(ps2dev, NULL, cmd) ||
1847 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1848 return -1;
1850 /* we may get 3 more bytes, just ignore them */
1851 ps2_drain(ps2dev, 3, 100);
1853 return 0;
1856 static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
1858 struct ps2dev *ps2dev = &psmouse->ps2dev;
1860 /* Try ALPS magic knock - 4 disable before enable */
1861 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1862 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1863 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1864 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1865 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
1866 return -1;
1869 * Switch mouse to poll (remote) mode so motion data will not
1870 * get in our way
1872 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
1875 static int alps_monitor_mode_send_word(struct psmouse *psmouse, u16 word)
1877 int i, nibble;
1880 * b0-b11 are valid bits, send sequence is inverse.
1881 * e.g. when word = 0x0123, nibble send sequence is 3, 2, 1
1883 for (i = 0; i <= 8; i += 4) {
1884 nibble = (word >> i) & 0xf;
1885 if (alps_command_mode_send_nibble(psmouse, nibble))
1886 return -1;
1889 return 0;
1892 static int alps_monitor_mode_write_reg(struct psmouse *psmouse,
1893 u16 addr, u16 value)
1895 struct ps2dev *ps2dev = &psmouse->ps2dev;
1897 /* 0x0A0 is the command to write the word */
1898 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE) ||
1899 alps_monitor_mode_send_word(psmouse, 0x0A0) ||
1900 alps_monitor_mode_send_word(psmouse, addr) ||
1901 alps_monitor_mode_send_word(psmouse, value) ||
1902 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1903 return -1;
1905 return 0;
1908 static int alps_monitor_mode(struct psmouse *psmouse, bool enable)
1910 struct ps2dev *ps2dev = &psmouse->ps2dev;
1912 if (enable) {
1913 /* EC E9 F5 F5 E7 E6 E7 E9 to enter monitor mode */
1914 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
1915 ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO) ||
1916 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1917 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1918 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1919 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1920 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1921 ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO))
1922 return -1;
1923 } else {
1924 /* EC to exit monitor mode */
1925 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP))
1926 return -1;
1929 return 0;
1932 static int alps_absolute_mode_v6(struct psmouse *psmouse)
1934 u16 reg_val = 0x181;
1935 int ret = -1;
1937 /* enter monitor mode, to write the register */
1938 if (alps_monitor_mode(psmouse, true))
1939 return -1;
1941 ret = alps_monitor_mode_write_reg(psmouse, 0x000, reg_val);
1943 if (alps_monitor_mode(psmouse, false))
1944 ret = -1;
1946 return ret;
1949 static int alps_get_status(struct psmouse *psmouse, char *param)
1951 /* Get status: 0xF5 0xF5 0xF5 0xE9 */
1952 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param))
1953 return -1;
1955 return 0;
1959 * Turn touchpad tapping on or off. The sequences are:
1960 * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
1961 * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
1962 * My guess that 0xE9 (GetInfo) is here as a sync point.
1963 * For models that also have stickpointer (DualPoints) its tapping
1964 * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
1965 * we don't fiddle with it.
1967 static int alps_tap_mode(struct psmouse *psmouse, int enable)
1969 struct ps2dev *ps2dev = &psmouse->ps2dev;
1970 int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
1971 unsigned char tap_arg = enable ? 0x0A : 0x00;
1972 unsigned char param[4];
1974 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
1975 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1976 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1977 ps2_command(ps2dev, &tap_arg, cmd))
1978 return -1;
1980 if (alps_get_status(psmouse, param))
1981 return -1;
1983 return 0;
1987 * alps_poll() - poll the touchpad for current motion packet.
1988 * Used in resync.
1990 static int alps_poll(struct psmouse *psmouse)
1992 struct alps_data *priv = psmouse->private;
1993 unsigned char buf[sizeof(psmouse->packet)];
1994 bool poll_failed;
1996 if (priv->flags & ALPS_PASS)
1997 alps_passthrough_mode_v2(psmouse, true);
1999 poll_failed = ps2_command(&psmouse->ps2dev, buf,
2000 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
2002 if (priv->flags & ALPS_PASS)
2003 alps_passthrough_mode_v2(psmouse, false);
2005 if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
2006 return -1;
2008 if ((psmouse->badbyte & 0xc8) == 0x08) {
2010 * Poll the track stick ...
2012 if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
2013 return -1;
2016 memcpy(psmouse->packet, buf, sizeof(buf));
2017 return 0;
2020 static int alps_hw_init_v1_v2(struct psmouse *psmouse)
2022 struct alps_data *priv = psmouse->private;
2024 if ((priv->flags & ALPS_PASS) &&
2025 alps_passthrough_mode_v2(psmouse, true)) {
2026 return -1;
2029 if (alps_tap_mode(psmouse, true)) {
2030 psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
2031 return -1;
2034 if (alps_absolute_mode_v1_v2(psmouse)) {
2035 psmouse_err(psmouse, "Failed to enable absolute mode\n");
2036 return -1;
2039 if ((priv->flags & ALPS_PASS) &&
2040 alps_passthrough_mode_v2(psmouse, false)) {
2041 return -1;
2044 /* ALPS needs stream mode, otherwise it won't report any data */
2045 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
2046 psmouse_err(psmouse, "Failed to enable stream mode\n");
2047 return -1;
2050 return 0;
2053 /* Must be in passthrough mode when calling this function */
2054 static int alps_trackstick_enter_extended_mode_v3_v6(struct psmouse *psmouse)
2056 unsigned char param[2] = {0xC8, 0x14};
2058 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2059 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2060 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2061 ps2_command(&psmouse->ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2062 ps2_command(&psmouse->ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
2063 return -1;
2065 return 0;
2068 static int alps_hw_init_v6(struct psmouse *psmouse)
2070 int ret;
2072 /* Enter passthrough mode to let trackpoint enter 6byte raw mode */
2073 if (alps_passthrough_mode_v2(psmouse, true))
2074 return -1;
2076 ret = alps_trackstick_enter_extended_mode_v3_v6(psmouse);
2078 if (alps_passthrough_mode_v2(psmouse, false))
2079 return -1;
2081 if (ret)
2082 return ret;
2084 if (alps_absolute_mode_v6(psmouse)) {
2085 psmouse_err(psmouse, "Failed to enable absolute mode\n");
2086 return -1;
2089 return 0;
2093 * Enable or disable passthrough mode to the trackstick.
2095 static int alps_passthrough_mode_v3(struct psmouse *psmouse,
2096 int reg_base, bool enable)
2098 int reg_val, ret = -1;
2100 if (alps_enter_command_mode(psmouse))
2101 return -1;
2103 reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x0008);
2104 if (reg_val == -1)
2105 goto error;
2107 if (enable)
2108 reg_val |= 0x01;
2109 else
2110 reg_val &= ~0x01;
2112 ret = __alps_command_mode_write_reg(psmouse, reg_val);
2114 error:
2115 if (alps_exit_command_mode(psmouse))
2116 ret = -1;
2117 return ret;
2120 /* Must be in command mode when calling this function */
2121 static int alps_absolute_mode_v3(struct psmouse *psmouse)
2123 int reg_val;
2125 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
2126 if (reg_val == -1)
2127 return -1;
2129 reg_val |= 0x06;
2130 if (__alps_command_mode_write_reg(psmouse, reg_val))
2131 return -1;
2133 return 0;
2136 static int alps_probe_trackstick_v3_v7(struct psmouse *psmouse, int reg_base)
2138 int ret = -EIO, reg_val;
2140 if (alps_enter_command_mode(psmouse))
2141 goto error;
2143 reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x08);
2144 if (reg_val == -1)
2145 goto error;
2147 /* bit 7: trackstick is present */
2148 ret = reg_val & 0x80 ? 0 : -ENODEV;
2150 error:
2151 alps_exit_command_mode(psmouse);
2152 return ret;
2155 static int alps_setup_trackstick_v3(struct psmouse *psmouse, int reg_base)
2157 int ret = 0;
2158 int reg_val;
2159 unsigned char param[4];
2162 * We need to configure trackstick to report data for touchpad in
2163 * extended format. And also we need to tell touchpad to expect data
2164 * from trackstick in extended format. Without this configuration
2165 * trackstick packets sent from touchpad are in basic format which is
2166 * different from what we expect.
2169 if (alps_passthrough_mode_v3(psmouse, reg_base, true))
2170 return -EIO;
2173 * E7 report for the trackstick
2175 * There have been reports of failures to seem to trace back
2176 * to the above trackstick check failing. When these occur
2177 * this E7 report fails, so when that happens we continue
2178 * with the assumption that there isn't a trackstick after
2179 * all.
2181 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_SETSCALE21, param)) {
2182 psmouse_warn(psmouse, "Failed to initialize trackstick (E7 report failed)\n");
2183 ret = -ENODEV;
2184 } else {
2185 psmouse_dbg(psmouse, "trackstick E7 report: %3ph\n", param);
2186 if (alps_trackstick_enter_extended_mode_v3_v6(psmouse)) {
2187 psmouse_err(psmouse, "Failed to enter into trackstick extended mode\n");
2188 ret = -EIO;
2192 if (alps_passthrough_mode_v3(psmouse, reg_base, false))
2193 return -EIO;
2195 if (ret)
2196 return ret;
2198 if (alps_enter_command_mode(psmouse))
2199 return -EIO;
2201 reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x08);
2202 if (reg_val == -1) {
2203 ret = -EIO;
2204 } else {
2206 * Tell touchpad that trackstick is now in extended mode.
2207 * If bit 1 isn't set the packet format is different.
2209 reg_val |= BIT(1);
2210 if (__alps_command_mode_write_reg(psmouse, reg_val))
2211 ret = -EIO;
2214 if (alps_exit_command_mode(psmouse))
2215 return -EIO;
2217 return ret;
2220 static int alps_hw_init_v3(struct psmouse *psmouse)
2222 struct alps_data *priv = psmouse->private;
2223 struct ps2dev *ps2dev = &psmouse->ps2dev;
2224 int reg_val;
2225 unsigned char param[4];
2227 if ((priv->flags & ALPS_DUALPOINT) &&
2228 alps_setup_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE) == -EIO)
2229 goto error;
2231 if (alps_enter_command_mode(psmouse) ||
2232 alps_absolute_mode_v3(psmouse)) {
2233 psmouse_err(psmouse, "Failed to enter absolute mode\n");
2234 goto error;
2237 reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
2238 if (reg_val == -1)
2239 goto error;
2240 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
2241 goto error;
2243 reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
2244 if (reg_val == -1)
2245 goto error;
2246 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
2247 goto error;
2249 if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
2250 goto error;
2251 if (__alps_command_mode_write_reg(psmouse, 0x04))
2252 goto error;
2254 if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
2255 goto error;
2256 if (__alps_command_mode_write_reg(psmouse, 0x03))
2257 goto error;
2259 if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
2260 goto error;
2261 if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
2262 goto error;
2264 if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
2265 goto error;
2266 if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
2267 goto error;
2269 alps_exit_command_mode(psmouse);
2271 /* Set rate and enable data reporting */
2272 param[0] = 0x64;
2273 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
2274 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
2275 psmouse_err(psmouse, "Failed to enable data reporting\n");
2276 return -1;
2279 return 0;
2281 error:
2283 * Leaving the touchpad in command mode will essentially render
2284 * it unusable until the machine reboots, so exit it here just
2285 * to be safe
2287 alps_exit_command_mode(psmouse);
2288 return -1;
2291 static int alps_get_v3_v7_resolution(struct psmouse *psmouse, int reg_pitch)
2293 int reg, x_pitch, y_pitch, x_electrode, y_electrode, x_phys, y_phys;
2294 struct alps_data *priv = psmouse->private;
2296 reg = alps_command_mode_read_reg(psmouse, reg_pitch);
2297 if (reg < 0)
2298 return reg;
2300 x_pitch = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
2301 x_pitch = 50 + 2 * x_pitch; /* In 0.1 mm units */
2303 y_pitch = (char)reg >> 4; /* sign extend upper 4 bits */
2304 y_pitch = 36 + 2 * y_pitch; /* In 0.1 mm units */
2306 reg = alps_command_mode_read_reg(psmouse, reg_pitch + 1);
2307 if (reg < 0)
2308 return reg;
2310 x_electrode = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
2311 x_electrode = 17 + x_electrode;
2313 y_electrode = (char)reg >> 4; /* sign extend upper 4 bits */
2314 y_electrode = 13 + y_electrode;
2316 x_phys = x_pitch * (x_electrode - 1); /* In 0.1 mm units */
2317 y_phys = y_pitch * (y_electrode - 1); /* In 0.1 mm units */
2319 priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
2320 priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
2322 psmouse_dbg(psmouse,
2323 "pitch %dx%d num-electrodes %dx%d physical size %dx%d mm res %dx%d\n",
2324 x_pitch, y_pitch, x_electrode, y_electrode,
2325 x_phys / 10, y_phys / 10, priv->x_res, priv->y_res);
2327 return 0;
2330 static int alps_hw_init_rushmore_v3(struct psmouse *psmouse)
2332 struct alps_data *priv = psmouse->private;
2333 struct ps2dev *ps2dev = &psmouse->ps2dev;
2334 int reg_val, ret = -1;
2336 if (priv->flags & ALPS_DUALPOINT) {
2337 reg_val = alps_setup_trackstick_v3(psmouse,
2338 ALPS_REG_BASE_RUSHMORE);
2339 if (reg_val == -EIO)
2340 goto error;
2343 if (alps_enter_command_mode(psmouse) ||
2344 alps_command_mode_read_reg(psmouse, 0xc2d9) == -1 ||
2345 alps_command_mode_write_reg(psmouse, 0xc2cb, 0x00))
2346 goto error;
2348 if (alps_get_v3_v7_resolution(psmouse, 0xc2da))
2349 goto error;
2351 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c6);
2352 if (reg_val == -1)
2353 goto error;
2354 if (__alps_command_mode_write_reg(psmouse, reg_val & 0xfd))
2355 goto error;
2357 if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
2358 goto error;
2360 /* enter absolute mode */
2361 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
2362 if (reg_val == -1)
2363 goto error;
2364 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
2365 goto error;
2367 alps_exit_command_mode(psmouse);
2368 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2370 error:
2371 alps_exit_command_mode(psmouse);
2372 return ret;
2375 /* Must be in command mode when calling this function */
2376 static int alps_absolute_mode_v4(struct psmouse *psmouse)
2378 int reg_val;
2380 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
2381 if (reg_val == -1)
2382 return -1;
2384 reg_val |= 0x02;
2385 if (__alps_command_mode_write_reg(psmouse, reg_val))
2386 return -1;
2388 return 0;
2391 static int alps_hw_init_v4(struct psmouse *psmouse)
2393 struct ps2dev *ps2dev = &psmouse->ps2dev;
2394 unsigned char param[4];
2396 if (alps_enter_command_mode(psmouse))
2397 goto error;
2399 if (alps_absolute_mode_v4(psmouse)) {
2400 psmouse_err(psmouse, "Failed to enter absolute mode\n");
2401 goto error;
2404 if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
2405 goto error;
2407 if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
2408 goto error;
2410 if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
2411 goto error;
2413 if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
2414 goto error;
2416 if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
2417 goto error;
2419 if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
2420 goto error;
2422 if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
2423 goto error;
2425 if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
2426 goto error;
2428 alps_exit_command_mode(psmouse);
2431 * This sequence changes the output from a 9-byte to an
2432 * 8-byte format. All the same data seems to be present,
2433 * just in a more compact format.
2435 param[0] = 0xc8;
2436 param[1] = 0x64;
2437 param[2] = 0x50;
2438 if (ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2439 ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE) ||
2440 ps2_command(ps2dev, &param[2], PSMOUSE_CMD_SETRATE) ||
2441 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
2442 return -1;
2444 /* Set rate and enable data reporting */
2445 param[0] = 0x64;
2446 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
2447 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
2448 psmouse_err(psmouse, "Failed to enable data reporting\n");
2449 return -1;
2452 return 0;
2454 error:
2456 * Leaving the touchpad in command mode will essentially render
2457 * it unusable until the machine reboots, so exit it here just
2458 * to be safe
2460 alps_exit_command_mode(psmouse);
2461 return -1;
2464 static int alps_get_otp_values_ss4_v2(struct psmouse *psmouse,
2465 unsigned char index, unsigned char otp[])
2467 struct ps2dev *ps2dev = &psmouse->ps2dev;
2469 switch (index) {
2470 case 0:
2471 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2472 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2473 ps2_command(ps2dev, otp, PSMOUSE_CMD_GETINFO))
2474 return -1;
2476 break;
2478 case 1:
2479 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2480 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2481 ps2_command(ps2dev, otp, PSMOUSE_CMD_GETINFO))
2482 return -1;
2484 break;
2487 return 0;
2490 static int alps_update_device_area_ss4_v2(unsigned char otp[][4],
2491 struct alps_data *priv)
2493 int num_x_electrode;
2494 int num_y_electrode;
2495 int x_pitch, y_pitch, x_phys, y_phys;
2497 if (IS_SS4PLUS_DEV(priv->dev_id)) {
2498 num_x_electrode =
2499 SS4PLUS_NUMSENSOR_XOFFSET + (otp[0][2] & 0x0F);
2500 num_y_electrode =
2501 SS4PLUS_NUMSENSOR_YOFFSET + ((otp[0][2] >> 4) & 0x0F);
2503 priv->x_max =
2504 (num_x_electrode - 1) * SS4PLUS_COUNT_PER_ELECTRODE;
2505 priv->y_max =
2506 (num_y_electrode - 1) * SS4PLUS_COUNT_PER_ELECTRODE;
2508 x_pitch = (otp[0][1] & 0x0F) + SS4PLUS_MIN_PITCH_MM;
2509 y_pitch = ((otp[0][1] >> 4) & 0x0F) + SS4PLUS_MIN_PITCH_MM;
2511 } else {
2512 num_x_electrode =
2513 SS4_NUMSENSOR_XOFFSET + (otp[1][0] & 0x0F);
2514 num_y_electrode =
2515 SS4_NUMSENSOR_YOFFSET + ((otp[1][0] >> 4) & 0x0F);
2517 priv->x_max =
2518 (num_x_electrode - 1) * SS4_COUNT_PER_ELECTRODE;
2519 priv->y_max =
2520 (num_y_electrode - 1) * SS4_COUNT_PER_ELECTRODE;
2522 x_pitch = ((otp[1][2] >> 2) & 0x07) + SS4_MIN_PITCH_MM;
2523 y_pitch = ((otp[1][2] >> 5) & 0x07) + SS4_MIN_PITCH_MM;
2526 x_phys = x_pitch * (num_x_electrode - 1); /* In 0.1 mm units */
2527 y_phys = y_pitch * (num_y_electrode - 1); /* In 0.1 mm units */
2529 priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
2530 priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
2532 return 0;
2535 static int alps_update_btn_info_ss4_v2(unsigned char otp[][4],
2536 struct alps_data *priv)
2538 unsigned char is_btnless;
2540 if (IS_SS4PLUS_DEV(priv->dev_id))
2541 is_btnless = (otp[1][0] >> 1) & 0x01;
2542 else
2543 is_btnless = (otp[1][1] >> 3) & 0x01;
2545 if (is_btnless)
2546 priv->flags |= ALPS_BUTTONPAD;
2548 return 0;
2551 static int alps_update_dual_info_ss4_v2(unsigned char otp[][4],
2552 struct alps_data *priv,
2553 struct psmouse *psmouse)
2555 bool is_dual = false;
2556 int reg_val = 0;
2557 struct ps2dev *ps2dev = &psmouse->ps2dev;
2559 if (IS_SS4PLUS_DEV(priv->dev_id)) {
2560 is_dual = (otp[0][0] >> 4) & 0x01;
2562 if (!is_dual) {
2563 /* For support TrackStick of Thinkpad L/E series */
2564 if (alps_exit_command_mode(psmouse) == 0 &&
2565 alps_enter_command_mode(psmouse) == 0) {
2566 reg_val = alps_command_mode_read_reg(psmouse,
2567 0xD7);
2569 alps_exit_command_mode(psmouse);
2570 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2572 if (reg_val == 0x0C || reg_val == 0x1D)
2573 is_dual = true;
2577 if (is_dual)
2578 priv->flags |= ALPS_DUALPOINT |
2579 ALPS_DUALPOINT_WITH_PRESSURE;
2581 return 0;
2584 static int alps_set_defaults_ss4_v2(struct psmouse *psmouse,
2585 struct alps_data *priv)
2587 unsigned char otp[2][4];
2589 memset(otp, 0, sizeof(otp));
2591 if (alps_get_otp_values_ss4_v2(psmouse, 1, &otp[1][0]) ||
2592 alps_get_otp_values_ss4_v2(psmouse, 0, &otp[0][0]))
2593 return -1;
2595 alps_update_device_area_ss4_v2(otp, priv);
2597 alps_update_btn_info_ss4_v2(otp, priv);
2599 alps_update_dual_info_ss4_v2(otp, priv, psmouse);
2601 return 0;
2604 static int alps_dolphin_get_device_area(struct psmouse *psmouse,
2605 struct alps_data *priv)
2607 struct ps2dev *ps2dev = &psmouse->ps2dev;
2608 unsigned char param[4] = {0};
2609 int num_x_electrode, num_y_electrode;
2611 if (alps_enter_command_mode(psmouse))
2612 return -1;
2614 param[0] = 0x0a;
2615 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
2616 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2617 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2618 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2619 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE))
2620 return -1;
2622 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
2623 return -1;
2626 * Dolphin's sensor line number is not fixed. It can be calculated
2627 * by adding the device's register value with DOLPHIN_PROFILE_X/YOFFSET.
2628 * Further more, we can get device's x_max and y_max by multiplying
2629 * sensor line number with DOLPHIN_COUNT_PER_ELECTRODE.
2631 * e.g. When we get register's sensor_x = 11 & sensor_y = 8,
2632 * real sensor line number X = 11 + 8 = 19, and
2633 * real sensor line number Y = 8 + 1 = 9.
2634 * So, x_max = (19 - 1) * 64 = 1152, and
2635 * y_max = (9 - 1) * 64 = 512.
2637 num_x_electrode = DOLPHIN_PROFILE_XOFFSET + (param[2] & 0x0F);
2638 num_y_electrode = DOLPHIN_PROFILE_YOFFSET + ((param[2] >> 4) & 0x0F);
2639 priv->x_bits = num_x_electrode;
2640 priv->y_bits = num_y_electrode;
2641 priv->x_max = (num_x_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2642 priv->y_max = (num_y_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2644 if (alps_exit_command_mode(psmouse))
2645 return -1;
2647 return 0;
2650 static int alps_hw_init_dolphin_v1(struct psmouse *psmouse)
2652 struct ps2dev *ps2dev = &psmouse->ps2dev;
2653 unsigned char param[2];
2655 /* This is dolphin "v1" as empirically defined by florin9doi */
2656 param[0] = 0x64;
2657 param[1] = 0x28;
2659 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2660 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2661 ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
2662 return -1;
2664 return 0;
2667 static int alps_hw_init_v7(struct psmouse *psmouse)
2669 struct ps2dev *ps2dev = &psmouse->ps2dev;
2670 int reg_val, ret = -1;
2672 if (alps_enter_command_mode(psmouse) ||
2673 alps_command_mode_read_reg(psmouse, 0xc2d9) == -1)
2674 goto error;
2676 if (alps_get_v3_v7_resolution(psmouse, 0xc397))
2677 goto error;
2679 if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
2680 goto error;
2682 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
2683 if (reg_val == -1)
2684 goto error;
2685 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
2686 goto error;
2688 alps_exit_command_mode(psmouse);
2689 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2691 error:
2692 alps_exit_command_mode(psmouse);
2693 return ret;
2696 static int alps_hw_init_ss4_v2(struct psmouse *psmouse)
2698 struct ps2dev *ps2dev = &psmouse->ps2dev;
2699 char param[2] = {0x64, 0x28};
2700 int ret = -1;
2702 /* enter absolute mode */
2703 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2704 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2705 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2706 ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE)) {
2707 goto error;
2710 /* T.B.D. Decread noise packet number, delete in the future */
2711 if (alps_exit_command_mode(psmouse) ||
2712 alps_enter_command_mode(psmouse) ||
2713 alps_command_mode_write_reg(psmouse, 0x001D, 0x20)) {
2714 goto error;
2716 alps_exit_command_mode(psmouse);
2718 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2720 error:
2721 alps_exit_command_mode(psmouse);
2722 return ret;
2725 static int alps_set_protocol(struct psmouse *psmouse,
2726 struct alps_data *priv,
2727 const struct alps_protocol_info *protocol)
2729 psmouse->private = priv;
2731 timer_setup(&priv->timer, alps_flush_packet, 0);
2733 priv->proto_version = protocol->version;
2734 priv->byte0 = protocol->byte0;
2735 priv->mask0 = protocol->mask0;
2736 priv->flags = protocol->flags;
2738 priv->x_max = 2000;
2739 priv->y_max = 1400;
2740 priv->x_bits = 15;
2741 priv->y_bits = 11;
2743 switch (priv->proto_version) {
2744 case ALPS_PROTO_V1:
2745 case ALPS_PROTO_V2:
2746 priv->hw_init = alps_hw_init_v1_v2;
2747 priv->process_packet = alps_process_packet_v1_v2;
2748 priv->set_abs_params = alps_set_abs_params_st;
2749 priv->x_max = 1023;
2750 priv->y_max = 767;
2751 if (dmi_check_system(alps_dmi_has_separate_stick_buttons))
2752 priv->flags |= ALPS_STICK_BITS;
2753 break;
2755 case ALPS_PROTO_V3:
2756 priv->hw_init = alps_hw_init_v3;
2757 priv->process_packet = alps_process_packet_v3;
2758 priv->set_abs_params = alps_set_abs_params_semi_mt;
2759 priv->decode_fields = alps_decode_pinnacle;
2760 priv->nibble_commands = alps_v3_nibble_commands;
2761 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2763 if (alps_probe_trackstick_v3_v7(psmouse,
2764 ALPS_REG_BASE_PINNACLE) < 0)
2765 priv->flags &= ~ALPS_DUALPOINT;
2767 break;
2769 case ALPS_PROTO_V3_RUSHMORE:
2770 priv->hw_init = alps_hw_init_rushmore_v3;
2771 priv->process_packet = alps_process_packet_v3;
2772 priv->set_abs_params = alps_set_abs_params_semi_mt;
2773 priv->decode_fields = alps_decode_rushmore;
2774 priv->nibble_commands = alps_v3_nibble_commands;
2775 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2776 priv->x_bits = 16;
2777 priv->y_bits = 12;
2779 if (alps_probe_trackstick_v3_v7(psmouse,
2780 ALPS_REG_BASE_RUSHMORE) < 0)
2781 priv->flags &= ~ALPS_DUALPOINT;
2783 break;
2785 case ALPS_PROTO_V4:
2786 priv->hw_init = alps_hw_init_v4;
2787 priv->process_packet = alps_process_packet_v4;
2788 priv->set_abs_params = alps_set_abs_params_semi_mt;
2789 priv->nibble_commands = alps_v4_nibble_commands;
2790 priv->addr_command = PSMOUSE_CMD_DISABLE;
2791 break;
2793 case ALPS_PROTO_V5:
2794 priv->hw_init = alps_hw_init_dolphin_v1;
2795 priv->process_packet = alps_process_touchpad_packet_v3_v5;
2796 priv->decode_fields = alps_decode_dolphin;
2797 priv->set_abs_params = alps_set_abs_params_semi_mt;
2798 priv->nibble_commands = alps_v3_nibble_commands;
2799 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2800 priv->x_bits = 23;
2801 priv->y_bits = 12;
2803 if (alps_dolphin_get_device_area(psmouse, priv))
2804 return -EIO;
2806 break;
2808 case ALPS_PROTO_V6:
2809 priv->hw_init = alps_hw_init_v6;
2810 priv->process_packet = alps_process_packet_v6;
2811 priv->set_abs_params = alps_set_abs_params_st;
2812 priv->nibble_commands = alps_v6_nibble_commands;
2813 priv->x_max = 2047;
2814 priv->y_max = 1535;
2815 break;
2817 case ALPS_PROTO_V7:
2818 priv->hw_init = alps_hw_init_v7;
2819 priv->process_packet = alps_process_packet_v7;
2820 priv->decode_fields = alps_decode_packet_v7;
2821 priv->set_abs_params = alps_set_abs_params_v7;
2822 priv->nibble_commands = alps_v3_nibble_commands;
2823 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2824 priv->x_max = 0xfff;
2825 priv->y_max = 0x7ff;
2827 if (priv->fw_ver[1] != 0xba)
2828 priv->flags |= ALPS_BUTTONPAD;
2830 if (alps_probe_trackstick_v3_v7(psmouse, ALPS_REG_BASE_V7) < 0)
2831 priv->flags &= ~ALPS_DUALPOINT;
2833 break;
2835 case ALPS_PROTO_V8:
2836 priv->hw_init = alps_hw_init_ss4_v2;
2837 priv->process_packet = alps_process_packet_ss4_v2;
2838 priv->decode_fields = alps_decode_ss4_v2;
2839 priv->set_abs_params = alps_set_abs_params_ss4_v2;
2840 priv->nibble_commands = alps_v3_nibble_commands;
2841 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2843 if (alps_set_defaults_ss4_v2(psmouse, priv))
2844 return -EIO;
2846 break;
2849 return 0;
2852 static const struct alps_protocol_info *alps_match_table(unsigned char *e7,
2853 unsigned char *ec)
2855 const struct alps_model_info *model;
2856 int i;
2858 for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
2859 model = &alps_model_data[i];
2861 if (!memcmp(e7, model->signature, sizeof(model->signature)))
2862 return &model->protocol_info;
2865 return NULL;
2868 static bool alps_is_cs19_trackpoint(struct psmouse *psmouse)
2870 u8 param[2] = { 0 };
2872 if (ps2_command(&psmouse->ps2dev,
2873 param, MAKE_PS2_CMD(0, 2, TP_READ_ID)))
2874 return false;
2877 * param[0] contains the trackpoint device variant_id while
2878 * param[1] contains the firmware_id. So far all alps
2879 * trackpoint-only devices have their variant_ids equal
2880 * TP_VARIANT_ALPS and their firmware_ids are in 0x20~0x2f range.
2882 return param[0] == TP_VARIANT_ALPS && ((param[1] & 0xf0) == 0x20);
2885 static int alps_identify(struct psmouse *psmouse, struct alps_data *priv)
2887 const struct alps_protocol_info *protocol;
2888 unsigned char e6[4], e7[4], ec[4];
2889 int error;
2892 * First try "E6 report".
2893 * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
2894 * The bits 0-2 of the first byte will be 1s if some buttons are
2895 * pressed.
2897 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2898 PSMOUSE_CMD_SETSCALE11, e6))
2899 return -EIO;
2901 if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100))
2902 return -EINVAL;
2905 * Now get the "E7" and "EC" reports. These will uniquely identify
2906 * most ALPS touchpads.
2908 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2909 PSMOUSE_CMD_SETSCALE21, e7) ||
2910 alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2911 PSMOUSE_CMD_RESET_WRAP, ec) ||
2912 alps_exit_command_mode(psmouse))
2913 return -EIO;
2915 protocol = alps_match_table(e7, ec);
2916 if (!protocol) {
2917 if (e7[0] == 0x73 && e7[1] == 0x02 && e7[2] == 0x64 &&
2918 ec[2] == 0x8a) {
2919 protocol = &alps_v4_protocol_data;
2920 } else if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0x50 &&
2921 ec[0] == 0x73 && (ec[1] == 0x01 || ec[1] == 0x02)) {
2922 protocol = &alps_v5_protocol_data;
2923 } else if (ec[0] == 0x88 &&
2924 ((ec[1] & 0xf0) == 0xb0 || (ec[1] & 0xf0) == 0xc0)) {
2925 protocol = &alps_v7_protocol_data;
2926 } else if (ec[0] == 0x88 && ec[1] == 0x08) {
2927 protocol = &alps_v3_rushmore_data;
2928 } else if (ec[0] == 0x88 && ec[1] == 0x07 &&
2929 ec[2] >= 0x90 && ec[2] <= 0x9d) {
2930 protocol = &alps_v3_protocol_data;
2931 } else if (e7[0] == 0x73 && e7[1] == 0x03 &&
2932 (e7[2] == 0x14 || e7[2] == 0x28)) {
2933 protocol = &alps_v8_protocol_data;
2934 } else if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0xc8) {
2935 protocol = &alps_v9_protocol_data;
2936 psmouse_warn(psmouse,
2937 "Unsupported ALPS V9 touchpad: E7=%3ph, EC=%3ph\n",
2938 e7, ec);
2939 return -EINVAL;
2940 } else {
2941 psmouse_dbg(psmouse,
2942 "Likely not an ALPS touchpad: E7=%3ph, EC=%3ph\n", e7, ec);
2943 return -EINVAL;
2947 if (priv) {
2948 /* Save Device ID and Firmware version */
2949 memcpy(priv->dev_id, e7, 3);
2950 memcpy(priv->fw_ver, ec, 3);
2951 error = alps_set_protocol(psmouse, priv, protocol);
2952 if (error)
2953 return error;
2956 return 0;
2959 static int alps_reconnect(struct psmouse *psmouse)
2961 struct alps_data *priv = psmouse->private;
2963 psmouse_reset(psmouse);
2965 if (alps_identify(psmouse, priv) < 0)
2966 return -1;
2968 return priv->hw_init(psmouse);
2971 static void alps_disconnect(struct psmouse *psmouse)
2973 struct alps_data *priv = psmouse->private;
2975 psmouse_reset(psmouse);
2976 del_timer_sync(&priv->timer);
2977 if (priv->dev2)
2978 input_unregister_device(priv->dev2);
2979 if (!IS_ERR_OR_NULL(priv->dev3))
2980 input_unregister_device(priv->dev3);
2981 kfree(priv);
2984 static void alps_set_abs_params_st(struct alps_data *priv,
2985 struct input_dev *dev1)
2987 input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
2988 input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
2989 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
2992 static void alps_set_abs_params_mt_common(struct alps_data *priv,
2993 struct input_dev *dev1)
2995 input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
2996 input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
2998 input_abs_set_res(dev1, ABS_MT_POSITION_X, priv->x_res);
2999 input_abs_set_res(dev1, ABS_MT_POSITION_Y, priv->y_res);
3001 set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
3002 set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
3005 static void alps_set_abs_params_semi_mt(struct alps_data *priv,
3006 struct input_dev *dev1)
3008 alps_set_abs_params_mt_common(priv, dev1);
3009 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
3011 input_mt_init_slots(dev1, MAX_TOUCHES,
3012 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
3013 INPUT_MT_SEMI_MT);
3016 static void alps_set_abs_params_v7(struct alps_data *priv,
3017 struct input_dev *dev1)
3019 alps_set_abs_params_mt_common(priv, dev1);
3020 set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
3022 input_mt_init_slots(dev1, MAX_TOUCHES,
3023 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
3024 INPUT_MT_TRACK);
3026 set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
3029 static void alps_set_abs_params_ss4_v2(struct alps_data *priv,
3030 struct input_dev *dev1)
3032 alps_set_abs_params_mt_common(priv, dev1);
3033 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
3034 set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
3036 input_mt_init_slots(dev1, MAX_TOUCHES,
3037 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
3038 INPUT_MT_TRACK);
3041 int alps_init(struct psmouse *psmouse)
3043 struct alps_data *priv = psmouse->private;
3044 struct input_dev *dev1 = psmouse->dev;
3045 int error;
3047 error = priv->hw_init(psmouse);
3048 if (error)
3049 goto init_fail;
3052 * Undo part of setup done for us by psmouse core since touchpad
3053 * is not a relative device.
3055 __clear_bit(EV_REL, dev1->evbit);
3056 __clear_bit(REL_X, dev1->relbit);
3057 __clear_bit(REL_Y, dev1->relbit);
3060 * Now set up our capabilities.
3062 dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
3063 dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
3064 dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
3065 dev1->keybit[BIT_WORD(BTN_LEFT)] |=
3066 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
3068 dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
3070 priv->set_abs_params(priv, dev1);
3072 if (priv->flags & ALPS_WHEEL) {
3073 dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
3074 dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
3077 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
3078 dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
3079 dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
3082 if (priv->flags & ALPS_FOUR_BUTTONS) {
3083 dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
3084 dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
3085 dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
3086 dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
3087 } else if (priv->flags & ALPS_BUTTONPAD) {
3088 set_bit(INPUT_PROP_BUTTONPAD, dev1->propbit);
3089 clear_bit(BTN_RIGHT, dev1->keybit);
3090 } else {
3091 dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
3094 if (priv->flags & ALPS_DUALPOINT) {
3095 struct input_dev *dev2;
3097 dev2 = input_allocate_device();
3098 if (!dev2) {
3099 psmouse_err(psmouse,
3100 "failed to allocate trackstick device\n");
3101 error = -ENOMEM;
3102 goto init_fail;
3105 snprintf(priv->phys2, sizeof(priv->phys2), "%s/input1",
3106 psmouse->ps2dev.serio->phys);
3107 dev2->phys = priv->phys2;
3110 * format of input device name is: "protocol vendor name"
3111 * see function psmouse_switch_protocol() in psmouse-base.c
3113 dev2->name = "AlpsPS/2 ALPS DualPoint Stick";
3115 dev2->id.bustype = BUS_I8042;
3116 dev2->id.vendor = 0x0002;
3117 dev2->id.product = PSMOUSE_ALPS;
3118 dev2->id.version = priv->proto_version;
3119 dev2->dev.parent = &psmouse->ps2dev.serio->dev;
3121 input_set_capability(dev2, EV_REL, REL_X);
3122 input_set_capability(dev2, EV_REL, REL_Y);
3123 if (priv->flags & ALPS_DUALPOINT_WITH_PRESSURE) {
3124 input_set_capability(dev2, EV_ABS, ABS_PRESSURE);
3125 input_set_abs_params(dev2, ABS_PRESSURE, 0, 127, 0, 0);
3127 input_set_capability(dev2, EV_KEY, BTN_LEFT);
3128 input_set_capability(dev2, EV_KEY, BTN_RIGHT);
3129 input_set_capability(dev2, EV_KEY, BTN_MIDDLE);
3131 __set_bit(INPUT_PROP_POINTER, dev2->propbit);
3132 __set_bit(INPUT_PROP_POINTING_STICK, dev2->propbit);
3134 error = input_register_device(dev2);
3135 if (error) {
3136 psmouse_err(psmouse,
3137 "failed to register trackstick device: %d\n",
3138 error);
3139 input_free_device(dev2);
3140 goto init_fail;
3143 priv->dev2 = dev2;
3146 priv->psmouse = psmouse;
3148 INIT_DELAYED_WORK(&priv->dev3_register_work,
3149 alps_register_bare_ps2_mouse);
3151 psmouse->protocol_handler = alps_process_byte;
3152 psmouse->poll = alps_poll;
3153 psmouse->disconnect = alps_disconnect;
3154 psmouse->reconnect = alps_reconnect;
3155 psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
3157 /* We are having trouble resyncing ALPS touchpads so disable it for now */
3158 psmouse->resync_time = 0;
3160 /* Allow 2 invalid packets without resetting device */
3161 psmouse->resetafter = psmouse->pktsize * 2;
3163 return 0;
3165 init_fail:
3166 psmouse_reset(psmouse);
3168 * Even though we did not allocate psmouse->private we do free
3169 * it here.
3171 kfree(psmouse->private);
3172 psmouse->private = NULL;
3173 return error;
3176 int alps_detect(struct psmouse *psmouse, bool set_properties)
3178 struct alps_data *priv;
3179 int error;
3181 error = alps_identify(psmouse, NULL);
3182 if (error)
3183 return error;
3186 * ALPS cs19 is a trackpoint-only device, and uses different
3187 * protocol than DualPoint ones, so we return -EINVAL here and let
3188 * trackpoint.c drive this device. If the trackpoint driver is not
3189 * enabled, the device will fall back to a bare PS/2 mouse.
3190 * If ps2_command() fails here, we depend on the immediately
3191 * followed psmouse_reset() to reset the device to normal state.
3193 if (alps_is_cs19_trackpoint(psmouse)) {
3194 psmouse_dbg(psmouse,
3195 "ALPS CS19 trackpoint-only device detected, ignoring\n");
3196 return -EINVAL;
3200 * Reset the device to make sure it is fully operational:
3201 * on some laptops, like certain Dell Latitudes, we may
3202 * fail to properly detect presence of trackstick if device
3203 * has not been reset.
3205 psmouse_reset(psmouse);
3207 priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
3208 if (!priv)
3209 return -ENOMEM;
3211 error = alps_identify(psmouse, priv);
3212 if (error) {
3213 kfree(priv);
3214 return error;
3217 if (set_properties) {
3218 psmouse->vendor = "ALPS";
3219 psmouse->name = priv->flags & ALPS_DUALPOINT ?
3220 "DualPoint TouchPad" : "GlidePoint";
3221 psmouse->model = priv->proto_version;
3222 } else {
3224 * Destroy alps_data structure we allocated earlier since
3225 * this was just a "trial run". Otherwise we'll keep it
3226 * to be used by alps_init() which has to be called if
3227 * we succeed and set_properties is true.
3229 kfree(priv);
3230 psmouse->private = NULL;
3233 return 0;