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>
29 * Definitions for ALPS version 3 and 4 command mode protocol
31 #define ALPS_CMD_NIBBLE_10 0x01f2
33 #define ALPS_REG_BASE_RUSHMORE 0xc2c0
34 #define ALPS_REG_BASE_V7 0xc2c0
35 #define ALPS_REG_BASE_PINNACLE 0x0000
37 static const struct alps_nibble_commands alps_v3_nibble_commands[] = {
38 { PSMOUSE_CMD_SETPOLL, 0x00 }, /* 0 */
39 { PSMOUSE_CMD_RESET_DIS, 0x00 }, /* 1 */
40 { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* 2 */
41 { PSMOUSE_CMD_SETRATE, 0x0a }, /* 3 */
42 { PSMOUSE_CMD_SETRATE, 0x14 }, /* 4 */
43 { PSMOUSE_CMD_SETRATE, 0x28 }, /* 5 */
44 { PSMOUSE_CMD_SETRATE, 0x3c }, /* 6 */
45 { PSMOUSE_CMD_SETRATE, 0x50 }, /* 7 */
46 { PSMOUSE_CMD_SETRATE, 0x64 }, /* 8 */
47 { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 9 */
48 { ALPS_CMD_NIBBLE_10, 0x00 }, /* a */
49 { PSMOUSE_CMD_SETRES, 0x00 }, /* b */
50 { PSMOUSE_CMD_SETRES, 0x01 }, /* c */
51 { PSMOUSE_CMD_SETRES, 0x02 }, /* d */
52 { PSMOUSE_CMD_SETRES, 0x03 }, /* e */
53 { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */
56 static const struct alps_nibble_commands alps_v4_nibble_commands[] = {
57 { PSMOUSE_CMD_ENABLE, 0x00 }, /* 0 */
58 { PSMOUSE_CMD_RESET_DIS, 0x00 }, /* 1 */
59 { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* 2 */
60 { PSMOUSE_CMD_SETRATE, 0x0a }, /* 3 */
61 { PSMOUSE_CMD_SETRATE, 0x14 }, /* 4 */
62 { PSMOUSE_CMD_SETRATE, 0x28 }, /* 5 */
63 { PSMOUSE_CMD_SETRATE, 0x3c }, /* 6 */
64 { PSMOUSE_CMD_SETRATE, 0x50 }, /* 7 */
65 { PSMOUSE_CMD_SETRATE, 0x64 }, /* 8 */
66 { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 9 */
67 { ALPS_CMD_NIBBLE_10, 0x00 }, /* a */
68 { PSMOUSE_CMD_SETRES, 0x00 }, /* b */
69 { PSMOUSE_CMD_SETRES, 0x01 }, /* c */
70 { PSMOUSE_CMD_SETRES, 0x02 }, /* d */
71 { PSMOUSE_CMD_SETRES, 0x03 }, /* e */
72 { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */
75 static const struct alps_nibble_commands alps_v6_nibble_commands[] = {
76 { PSMOUSE_CMD_ENABLE, 0x00 }, /* 0 */
77 { PSMOUSE_CMD_SETRATE, 0x0a }, /* 1 */
78 { PSMOUSE_CMD_SETRATE, 0x14 }, /* 2 */
79 { PSMOUSE_CMD_SETRATE, 0x28 }, /* 3 */
80 { PSMOUSE_CMD_SETRATE, 0x3c }, /* 4 */
81 { PSMOUSE_CMD_SETRATE, 0x50 }, /* 5 */
82 { PSMOUSE_CMD_SETRATE, 0x64 }, /* 6 */
83 { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 7 */
84 { PSMOUSE_CMD_GETID, 0x00 }, /* 8 */
85 { PSMOUSE_CMD_GETINFO, 0x00 }, /* 9 */
86 { PSMOUSE_CMD_SETRES, 0x00 }, /* a */
87 { PSMOUSE_CMD_SETRES, 0x01 }, /* b */
88 { PSMOUSE_CMD_SETRES, 0x02 }, /* c */
89 { PSMOUSE_CMD_SETRES, 0x03 }, /* d */
90 { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* e */
91 { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */
95 #define ALPS_DUALPOINT 0x02 /* touchpad has trackstick */
96 #define ALPS_PASS 0x04 /* device has a pass-through port */
98 #define ALPS_WHEEL 0x08 /* hardware wheel present */
99 #define ALPS_FW_BK_1 0x10 /* front & back buttons present */
100 #define ALPS_FW_BK_2 0x20 /* front & back buttons present */
101 #define ALPS_FOUR_BUTTONS 0x40 /* 4 direction button present */
102 #define ALPS_PS2_INTERLEAVED 0x80 /* 3-byte PS/2 packet interleaved with
103 6-byte ALPS packet */
104 #define ALPS_STICK_BITS 0x100 /* separate stick button bits */
105 #define ALPS_BUTTONPAD 0x200 /* device is a clickpad */
106 #define ALPS_DUALPOINT_WITH_PRESSURE 0x400 /* device can report trackpoint pressure */
108 static const struct alps_model_info alps_model_data[] = {
109 { { 0x32, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } }, /* Toshiba Salellite Pro M10 */
110 { { 0x33, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V1, 0x88, 0xf8, 0 } }, /* UMAX-530T */
111 { { 0x53, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
112 { { 0x53, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
113 { { 0x60, 0x03, 0xc8 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } }, /* HP ze1115 */
114 { { 0x63, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
115 { { 0x63, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
116 { { 0x63, 0x02, 0x28 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 } }, /* Fujitsu Siemens S6010 */
117 { { 0x63, 0x02, 0x3c }, 0x00, { ALPS_PROTO_V2, 0x8f, 0x8f, ALPS_WHEEL } }, /* Toshiba Satellite S2400-103 */
118 { { 0x63, 0x02, 0x50 }, 0x00, { ALPS_PROTO_V2, 0xef, 0xef, ALPS_FW_BK_1 } }, /* NEC Versa L320 */
119 { { 0x63, 0x02, 0x64 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
120 { { 0x63, 0x03, 0xc8 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } }, /* Dell Latitude D800 */
121 { { 0x73, 0x00, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_DUALPOINT } }, /* ThinkPad R61 8918-5QG */
122 { { 0x73, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
123 { { 0x73, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 } }, /* Ahtec Laptop */
126 * XXX This entry is suspicious. First byte has zero lower nibble,
127 * which is what a normal mouse would report. Also, the value 0x0e
128 * isn't valid per PS/2 spec.
130 { { 0x20, 0x02, 0x0e }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },
132 { { 0x22, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },
133 { { 0x22, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT } }, /* Dell Latitude D600 */
134 /* Dell Latitude E5500, E6400, E6500, Precision M4400 */
135 { { 0x62, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xcf, 0xcf,
136 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED } },
137 { { 0x73, 0x00, 0x14 }, 0x00, { ALPS_PROTO_V6, 0xff, 0xff, ALPS_DUALPOINT } }, /* Dell XT2 */
138 { { 0x73, 0x02, 0x50 }, 0x00, { ALPS_PROTO_V2, 0xcf, 0xcf, ALPS_FOUR_BUTTONS } }, /* Dell Vostro 1400 */
139 { { 0x52, 0x01, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xff, 0xff,
140 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED } }, /* Toshiba Tecra A11-11L */
141 { { 0x73, 0x02, 0x64 }, 0x8a, { ALPS_PROTO_V4, 0x8f, 0x8f, 0 } },
144 static const struct alps_protocol_info alps_v3_protocol_data = {
145 ALPS_PROTO_V3, 0x8f, 0x8f, ALPS_DUALPOINT
148 static const struct alps_protocol_info alps_v3_rushmore_data = {
149 ALPS_PROTO_V3_RUSHMORE, 0x8f, 0x8f, ALPS_DUALPOINT
152 static const struct alps_protocol_info alps_v5_protocol_data = {
153 ALPS_PROTO_V5, 0xc8, 0xd8, 0
156 static const struct alps_protocol_info alps_v7_protocol_data = {
157 ALPS_PROTO_V7, 0x48, 0x48, ALPS_DUALPOINT
160 static const struct alps_protocol_info alps_v8_protocol_data = {
161 ALPS_PROTO_V8, 0x18, 0x18, 0
165 * Some v2 models report the stick buttons in separate bits
167 static const struct dmi_system_id alps_dmi_has_separate_stick_buttons[] = {
168 #if defined(CONFIG_DMI) && defined(CONFIG_X86)
170 /* Extrapolated from other entries */
172 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
173 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D420"),
177 /* Reported-by: Hans de Bruin <jmdebruin@xmsnet.nl> */
179 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
180 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D430"),
184 /* Reported-by: Hans de Goede <hdegoede@redhat.com> */
186 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
187 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D620"),
191 /* Extrapolated from other entries */
193 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
194 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D630"),
201 static void alps_set_abs_params_st(struct alps_data *priv,
202 struct input_dev *dev1);
203 static void alps_set_abs_params_semi_mt(struct alps_data *priv,
204 struct input_dev *dev1);
205 static void alps_set_abs_params_v7(struct alps_data *priv,
206 struct input_dev *dev1);
207 static void alps_set_abs_params_ss4_v2(struct alps_data *priv,
208 struct input_dev *dev1);
210 /* Packet formats are described in Documentation/input/alps.txt */
212 static bool alps_is_valid_first_byte(struct alps_data *priv,
215 return (data & priv->mask0) == priv->byte0;
218 static void alps_report_buttons(struct input_dev *dev1, struct input_dev *dev2,
219 int left, int right, int middle)
221 struct input_dev *dev;
224 * If shared button has already been reported on the
225 * other device (dev2) then this event should be also
226 * sent through that device.
228 dev = (dev2 && test_bit(BTN_LEFT, dev2->key)) ? dev2 : dev1;
229 input_report_key(dev, BTN_LEFT, left);
231 dev = (dev2 && test_bit(BTN_RIGHT, dev2->key)) ? dev2 : dev1;
232 input_report_key(dev, BTN_RIGHT, right);
234 dev = (dev2 && test_bit(BTN_MIDDLE, dev2->key)) ? dev2 : dev1;
235 input_report_key(dev, BTN_MIDDLE, middle);
238 * Sync the _other_ device now, we'll do the first
239 * device later once we report the rest of the events.
245 static void alps_process_packet_v1_v2(struct psmouse *psmouse)
247 struct alps_data *priv = psmouse->private;
248 unsigned char *packet = psmouse->packet;
249 struct input_dev *dev = psmouse->dev;
250 struct input_dev *dev2 = priv->dev2;
251 int x, y, z, ges, fin, left, right, middle;
252 int back = 0, forward = 0;
254 if (priv->proto_version == ALPS_PROTO_V1) {
255 left = packet[2] & 0x10;
256 right = packet[2] & 0x08;
258 x = packet[1] | ((packet[0] & 0x07) << 7);
259 y = packet[4] | ((packet[3] & 0x07) << 7);
262 left = packet[3] & 1;
263 right = packet[3] & 2;
264 middle = packet[3] & 4;
265 x = packet[1] | ((packet[2] & 0x78) << (7 - 3));
266 y = packet[4] | ((packet[3] & 0x70) << (7 - 4));
270 if (priv->flags & ALPS_FW_BK_1) {
271 back = packet[0] & 0x10;
272 forward = packet[2] & 4;
275 if (priv->flags & ALPS_FW_BK_2) {
276 back = packet[3] & 4;
277 forward = packet[2] & 4;
278 if ((middle = forward && back))
285 if ((priv->flags & ALPS_DUALPOINT) && z == 127) {
286 input_report_rel(dev2, REL_X, (x > 383 ? (x - 768) : x));
287 input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y));
289 alps_report_buttons(dev2, dev, left, right, middle);
295 /* Some models have separate stick button bits */
296 if (priv->flags & ALPS_STICK_BITS) {
297 left |= packet[0] & 1;
298 right |= packet[0] & 2;
299 middle |= packet[0] & 4;
302 alps_report_buttons(dev, dev2, left, right, middle);
304 /* Convert hardware tap to a reasonable Z value */
309 * A "tap and drag" operation is reported by the hardware as a transition
310 * from (!fin && ges) to (fin && ges). This should be translated to the
311 * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
313 if (ges && fin && !priv->prev_fin) {
314 input_report_abs(dev, ABS_X, x);
315 input_report_abs(dev, ABS_Y, y);
316 input_report_abs(dev, ABS_PRESSURE, 0);
317 input_report_key(dev, BTN_TOOL_FINGER, 0);
320 priv->prev_fin = fin;
323 input_report_key(dev, BTN_TOUCH, 1);
325 input_report_key(dev, BTN_TOUCH, 0);
328 input_report_abs(dev, ABS_X, x);
329 input_report_abs(dev, ABS_Y, y);
332 input_report_abs(dev, ABS_PRESSURE, z);
333 input_report_key(dev, BTN_TOOL_FINGER, z > 0);
335 if (priv->flags & ALPS_WHEEL)
336 input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
338 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
339 input_report_key(dev, BTN_FORWARD, forward);
340 input_report_key(dev, BTN_BACK, back);
343 if (priv->flags & ALPS_FOUR_BUTTONS) {
344 input_report_key(dev, BTN_0, packet[2] & 4);
345 input_report_key(dev, BTN_1, packet[0] & 0x10);
346 input_report_key(dev, BTN_2, packet[3] & 4);
347 input_report_key(dev, BTN_3, packet[0] & 0x20);
353 static void alps_get_bitmap_points(unsigned int map,
354 struct alps_bitmap_point *low,
355 struct alps_bitmap_point *high,
358 struct alps_bitmap_point *point;
359 int i, bit, prev_bit = 0;
362 for (i = 0; map != 0; i++, map >>= 1) {
366 point->start_bit = i;
380 * Process bitmap data from semi-mt protocols. Returns the number of
381 * fingers detected. A return value of 0 means at least one of the
384 * The bitmaps don't have enough data to track fingers, so this function
385 * only generates points representing a bounding box of all contacts.
386 * These points are returned in fields->mt when the return value
389 static int alps_process_bitmap(struct alps_data *priv,
390 struct alps_fields *fields)
392 int i, fingers_x = 0, fingers_y = 0, fingers, closest;
393 struct alps_bitmap_point x_low = {0,}, x_high = {0,};
394 struct alps_bitmap_point y_low = {0,}, y_high = {0,};
395 struct input_mt_pos corner[4];
397 if (!fields->x_map || !fields->y_map)
400 alps_get_bitmap_points(fields->x_map, &x_low, &x_high, &fingers_x);
401 alps_get_bitmap_points(fields->y_map, &y_low, &y_high, &fingers_y);
404 * Fingers can overlap, so we use the maximum count of fingers
405 * on either axis as the finger count.
407 fingers = max(fingers_x, fingers_y);
410 * If an axis reports only a single contact, we have overlapping or
411 * adjacent fingers. Divide the single contact between the two points.
413 if (fingers_x == 1) {
414 i = (x_low.num_bits - 1) / 2;
415 x_low.num_bits = x_low.num_bits - i;
416 x_high.start_bit = x_low.start_bit + i;
417 x_high.num_bits = max(i, 1);
419 if (fingers_y == 1) {
420 i = (y_low.num_bits - 1) / 2;
421 y_low.num_bits = y_low.num_bits - i;
422 y_high.start_bit = y_low.start_bit + i;
423 y_high.num_bits = max(i, 1);
426 /* top-left corner */
428 (priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
429 (2 * (priv->x_bits - 1));
431 (priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
432 (2 * (priv->y_bits - 1));
434 /* top-right corner */
436 (priv->x_max * (2 * x_high.start_bit + x_high.num_bits - 1)) /
437 (2 * (priv->x_bits - 1));
439 (priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
440 (2 * (priv->y_bits - 1));
442 /* bottom-right corner */
444 (priv->x_max * (2 * x_high.start_bit + x_high.num_bits - 1)) /
445 (2 * (priv->x_bits - 1));
447 (priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) /
448 (2 * (priv->y_bits - 1));
450 /* bottom-left corner */
452 (priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
453 (2 * (priv->x_bits - 1));
455 (priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) /
456 (2 * (priv->y_bits - 1));
458 /* x-bitmap order is reversed on v5 touchpads */
459 if (priv->proto_version == ALPS_PROTO_V5) {
460 for (i = 0; i < 4; i++)
461 corner[i].x = priv->x_max - corner[i].x;
464 /* y-bitmap order is reversed on v3 and v4 touchpads */
465 if (priv->proto_version == ALPS_PROTO_V3 ||
466 priv->proto_version == ALPS_PROTO_V4) {
467 for (i = 0; i < 4; i++)
468 corner[i].y = priv->y_max - corner[i].y;
472 * We only select a corner for the second touch once per 2 finger
473 * touch sequence to avoid the chosen corner (and thus the coordinates)
474 * jumping around when the first touch is in the middle.
476 if (priv->second_touch == -1) {
477 /* Find corner closest to our st coordinates */
478 closest = 0x7fffffff;
479 for (i = 0; i < 4; i++) {
480 int dx = fields->st.x - corner[i].x;
481 int dy = fields->st.y - corner[i].y;
482 int distance = dx * dx + dy * dy;
484 if (distance < closest) {
485 priv->second_touch = i;
489 /* And select the opposite corner to use for the 2nd touch */
490 priv->second_touch = (priv->second_touch + 2) % 4;
493 fields->mt[0] = fields->st;
494 fields->mt[1] = corner[priv->second_touch];
499 static void alps_set_slot(struct input_dev *dev, int slot, int x, int y)
501 input_mt_slot(dev, slot);
502 input_mt_report_slot_state(dev, MT_TOOL_FINGER, true);
503 input_report_abs(dev, ABS_MT_POSITION_X, x);
504 input_report_abs(dev, ABS_MT_POSITION_Y, y);
507 static void alps_report_mt_data(struct psmouse *psmouse, int n)
509 struct alps_data *priv = psmouse->private;
510 struct input_dev *dev = psmouse->dev;
511 struct alps_fields *f = &priv->f;
512 int i, slot[MAX_TOUCHES];
514 input_mt_assign_slots(dev, slot, f->mt, n, 0);
515 for (i = 0; i < n; i++)
516 alps_set_slot(dev, slot[i], f->mt[i].x, f->mt[i].y);
518 input_mt_sync_frame(dev);
521 static void alps_report_semi_mt_data(struct psmouse *psmouse, int fingers)
523 struct alps_data *priv = psmouse->private;
524 struct input_dev *dev = psmouse->dev;
525 struct alps_fields *f = &priv->f;
527 /* Use st data when we don't have mt data */
529 f->mt[0].x = f->st.x;
530 f->mt[0].y = f->st.y;
531 fingers = f->pressure > 0 ? 1 : 0;
532 priv->second_touch = -1;
536 alps_set_slot(dev, 0, f->mt[0].x, f->mt[0].y);
538 alps_set_slot(dev, 1, f->mt[1].x, f->mt[1].y);
539 input_mt_sync_frame(dev);
541 input_mt_report_finger_count(dev, fingers);
543 input_report_key(dev, BTN_LEFT, f->left);
544 input_report_key(dev, BTN_RIGHT, f->right);
545 input_report_key(dev, BTN_MIDDLE, f->middle);
547 input_report_abs(dev, ABS_PRESSURE, f->pressure);
552 static void alps_process_trackstick_packet_v3(struct psmouse *psmouse)
554 struct alps_data *priv = psmouse->private;
555 unsigned char *packet = psmouse->packet;
556 struct input_dev *dev = priv->dev2;
557 int x, y, z, left, right, middle;
559 /* It should be a DualPoint when received trackstick packet */
560 if (!(priv->flags & ALPS_DUALPOINT)) {
561 psmouse_warn(psmouse,
562 "Rejected trackstick packet from non DualPoint device");
566 /* Sanity check packet */
567 if (!(packet[0] & 0x40)) {
568 psmouse_dbg(psmouse, "Bad trackstick packet, discarding\n");
573 * There's a special packet that seems to indicate the end
574 * of a stream of trackstick data. Filter these out.
576 if (packet[1] == 0x7f && packet[2] == 0x7f && packet[4] == 0x7f)
579 x = (s8)(((packet[0] & 0x20) << 2) | (packet[1] & 0x7f));
580 y = (s8)(((packet[0] & 0x10) << 3) | (packet[2] & 0x7f));
581 z = (packet[4] & 0x7c) >> 2;
584 * The x and y values tend to be quite large, and when used
585 * alone the trackstick is difficult to use. Scale them down
591 input_report_rel(dev, REL_X, x);
592 input_report_rel(dev, REL_Y, -y);
595 * Most ALPS models report the trackstick buttons in the touchpad
596 * packets, but a few report them here. No reliable way has been
597 * found to differentiate between the models upfront, so we enable
598 * the quirk in response to seeing a button press in the trackstick
601 left = packet[3] & 0x01;
602 right = packet[3] & 0x02;
603 middle = packet[3] & 0x04;
605 if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) &&
606 (left || right || middle))
607 priv->quirks |= ALPS_QUIRK_TRACKSTICK_BUTTONS;
609 if (priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) {
610 input_report_key(dev, BTN_LEFT, left);
611 input_report_key(dev, BTN_RIGHT, right);
612 input_report_key(dev, BTN_MIDDLE, middle);
619 static void alps_decode_buttons_v3(struct alps_fields *f, unsigned char *p)
621 f->left = !!(p[3] & 0x01);
622 f->right = !!(p[3] & 0x02);
623 f->middle = !!(p[3] & 0x04);
625 f->ts_left = !!(p[3] & 0x10);
626 f->ts_right = !!(p[3] & 0x20);
627 f->ts_middle = !!(p[3] & 0x40);
630 static int alps_decode_pinnacle(struct alps_fields *f, unsigned char *p,
631 struct psmouse *psmouse)
633 f->first_mp = !!(p[4] & 0x40);
634 f->is_mp = !!(p[0] & 0x40);
637 f->fingers = (p[5] & 0x3) + 1;
638 f->x_map = ((p[4] & 0x7e) << 8) |
639 ((p[1] & 0x7f) << 2) |
640 ((p[0] & 0x30) >> 4);
641 f->y_map = ((p[3] & 0x70) << 4) |
642 ((p[2] & 0x7f) << 1) |
645 f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
646 ((p[0] & 0x30) >> 4);
647 f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
648 f->pressure = p[5] & 0x7f;
650 alps_decode_buttons_v3(f, p);
656 static int alps_decode_rushmore(struct alps_fields *f, unsigned char *p,
657 struct psmouse *psmouse)
659 f->first_mp = !!(p[4] & 0x40);
660 f->is_mp = !!(p[5] & 0x40);
663 f->fingers = max((p[5] & 0x3), ((p[5] >> 2) & 0x3)) + 1;
664 f->x_map = ((p[5] & 0x10) << 11) |
665 ((p[4] & 0x7e) << 8) |
666 ((p[1] & 0x7f) << 2) |
667 ((p[0] & 0x30) >> 4);
668 f->y_map = ((p[5] & 0x20) << 6) |
669 ((p[3] & 0x70) << 4) |
670 ((p[2] & 0x7f) << 1) |
673 f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
674 ((p[0] & 0x30) >> 4);
675 f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
676 f->pressure = p[5] & 0x7f;
678 alps_decode_buttons_v3(f, p);
684 static int alps_decode_dolphin(struct alps_fields *f, unsigned char *p,
685 struct psmouse *psmouse)
688 struct alps_data *priv = psmouse->private;
690 f->first_mp = !!(p[0] & 0x02);
691 f->is_mp = !!(p[0] & 0x20);
694 f->st.x = ((p[1] & 0x7f) | ((p[4] & 0x0f) << 7));
695 f->st.y = ((p[2] & 0x7f) | ((p[4] & 0xf0) << 3));
696 f->pressure = (p[0] & 4) ? 0 : p[5] & 0x7f;
697 alps_decode_buttons_v3(f, p);
699 f->fingers = ((p[0] & 0x6) >> 1 |
702 palm_data = (p[1] & 0x7f) |
703 ((p[2] & 0x7f) << 7) |
704 ((p[4] & 0x7f) << 14) |
705 ((p[5] & 0x7f) << 21) |
706 ((p[3] & 0x07) << 28) |
707 (((u64)p[3] & 0x70) << 27) |
708 (((u64)p[0] & 0x01) << 34);
710 /* Y-profile is stored in P(0) to p(n-1), n = y_bits; */
711 f->y_map = palm_data & (BIT(priv->y_bits) - 1);
713 /* X-profile is stored in p(n) to p(n+m-1), m = x_bits; */
714 f->x_map = (palm_data >> priv->y_bits) &
715 (BIT(priv->x_bits) - 1);
721 static void alps_process_touchpad_packet_v3_v5(struct psmouse *psmouse)
723 struct alps_data *priv = psmouse->private;
724 unsigned char *packet = psmouse->packet;
725 struct input_dev *dev2 = priv->dev2;
726 struct alps_fields *f = &priv->f;
729 memset(f, 0, sizeof(*f));
731 priv->decode_fields(f, packet, psmouse);
734 * There's no single feature of touchpad position and bitmap packets
735 * that can be used to distinguish between them. We rely on the fact
736 * that a bitmap packet should always follow a position packet with
737 * bit 6 of packet[4] set.
739 if (priv->multi_packet) {
741 * Sometimes a position packet will indicate a multi-packet
742 * sequence, but then what follows is another position
743 * packet. Check for this, and when it happens process the
744 * position packet as usual.
747 fingers = f->fingers;
749 * Bitmap processing uses position packet's coordinate
750 * data, so we need to do decode it first.
752 priv->decode_fields(f, priv->multi_data, psmouse);
753 if (alps_process_bitmap(priv, f) == 0)
754 fingers = 0; /* Use st data */
756 priv->multi_packet = 0;
761 * Bit 6 of byte 0 is not usually set in position packets. The only
762 * times it seems to be set is in situations where the data is
763 * suspect anyway, e.g. a palm resting flat on the touchpad. Given
764 * this combined with the fact that this bit is useful for filtering
765 * out misidentified bitmap packets, we reject anything with this
771 if (!priv->multi_packet && f->first_mp) {
772 priv->multi_packet = 1;
773 memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
777 priv->multi_packet = 0;
780 * Sometimes the hardware sends a single packet with z = 0
781 * in the middle of a stream. Real releases generate packets
782 * with x, y, and z all zero, so these seem to be flukes.
785 if (f->st.x && f->st.y && !f->pressure)
788 alps_report_semi_mt_data(psmouse, fingers);
790 if ((priv->flags & ALPS_DUALPOINT) &&
791 !(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) {
792 input_report_key(dev2, BTN_LEFT, f->ts_left);
793 input_report_key(dev2, BTN_RIGHT, f->ts_right);
794 input_report_key(dev2, BTN_MIDDLE, f->ts_middle);
799 static void alps_process_packet_v3(struct psmouse *psmouse)
801 unsigned char *packet = psmouse->packet;
804 * v3 protocol packets come in three types, two representing
805 * touchpad data and one representing trackstick data.
806 * Trackstick packets seem to be distinguished by always
807 * having 0x3f in the last byte. This value has never been
808 * observed in the last byte of either of the other types
811 if (packet[5] == 0x3f) {
812 alps_process_trackstick_packet_v3(psmouse);
816 alps_process_touchpad_packet_v3_v5(psmouse);
819 static void alps_process_packet_v6(struct psmouse *psmouse)
821 struct alps_data *priv = psmouse->private;
822 unsigned char *packet = psmouse->packet;
823 struct input_dev *dev = psmouse->dev;
824 struct input_dev *dev2 = priv->dev2;
825 int x, y, z, left, right, middle;
828 * We can use Byte5 to distinguish if the packet is from Touchpad
833 if (packet[5] == 0x7F) {
834 /* It should be a DualPoint when received Trackpoint packet */
835 if (!(priv->flags & ALPS_DUALPOINT)) {
836 psmouse_warn(psmouse,
837 "Rejected trackstick packet from non DualPoint device");
841 /* Trackpoint packet */
842 x = packet[1] | ((packet[3] & 0x20) << 2);
843 y = packet[2] | ((packet[3] & 0x40) << 1);
845 left = packet[3] & 0x01;
846 right = packet[3] & 0x02;
847 middle = packet[3] & 0x04;
849 /* To prevent the cursor jump when finger lifted */
850 if (x == 0x7F && y == 0x7F && z == 0x7F)
853 /* Divide 4 since trackpoint's speed is too fast */
854 input_report_rel(dev2, REL_X, (char)x / 4);
855 input_report_rel(dev2, REL_Y, -((char)y / 4));
857 input_report_key(dev2, BTN_LEFT, left);
858 input_report_key(dev2, BTN_RIGHT, right);
859 input_report_key(dev2, BTN_MIDDLE, middle);
865 /* Touchpad packet */
866 x = packet[1] | ((packet[3] & 0x78) << 4);
867 y = packet[2] | ((packet[4] & 0x78) << 4);
869 left = packet[3] & 0x01;
870 right = packet[3] & 0x02;
873 input_report_key(dev, BTN_TOUCH, 1);
875 input_report_key(dev, BTN_TOUCH, 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 input_report_key(dev, BTN_LEFT, left);
887 input_report_key(dev, BTN_RIGHT, right);
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;
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))
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) {
945 return (psmouse->packet[2] & 0x40) == 0x40;
947 return (psmouse->packet[3] & 0x48) == 0x48;
949 return (psmouse->packet[5] & 0x40) == 0x00;
954 static unsigned char alps_get_packet_id_v7(char *byte)
956 unsigned char packet_id;
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;
967 packet_id = V7_PACKET_ID_UNKNOWN;
972 static void alps_get_finger_coordinate_v7(struct input_mt_pos *mt,
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);
989 case V7_PACKET_ID_TWO:
992 /* Detect false-postive touches where x & y report max value */
993 if (mt[1].y == 0x7ff && mt[1].x == 0xff0) {
995 /* y gets set to 0 at the end of this function */
999 case V7_PACKET_ID_MULTI:
1002 mt[1].y |= ((pkt[4] & 0x02) << 4);
1006 case V7_PACKET_ID_NEW:
1008 mt[1].x |= (pkt[0] & 0x20);
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)
1021 for (i = 0; i < MAX_TOUCHES; i++) {
1022 if (mt[i].x != 0 || mt[i].y != 0)
1029 static int alps_decode_packet_v7(struct alps_fields *f,
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)
1039 if (pkt_id == V7_PACKET_ID_UNKNOWN)
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
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)
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) {
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;
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, left, right, middle;
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");
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 left = (packet[1] & 0x01);
1111 right = (packet[1] & 0x02) >> 1;
1112 middle = (packet[1] & 0x04) >> 2;
1114 input_report_rel(dev2, REL_X, (char)x);
1115 input_report_rel(dev2, REL_Y, -((char)y));
1117 input_report_key(dev2, BTN_LEFT, left);
1118 input_report_key(dev2, BTN_RIGHT, right);
1119 input_report_key(dev2, BTN_MIDDLE, middle);
1124 static void alps_process_touchpad_packet_v7(struct psmouse *psmouse)
1126 struct alps_data *priv = psmouse->private;
1127 struct input_dev *dev = psmouse->dev;
1128 struct alps_fields *f = &priv->f;
1130 memset(f, 0, sizeof(*f));
1132 if (priv->decode_fields(f, psmouse->packet, psmouse))
1135 alps_report_mt_data(psmouse, alps_get_mt_count(f->mt));
1137 input_mt_report_finger_count(dev, f->fingers);
1139 input_report_key(dev, BTN_LEFT, f->left);
1140 input_report_key(dev, BTN_RIGHT, f->right);
1141 input_report_key(dev, BTN_MIDDLE, f->middle);
1146 static void alps_process_packet_v7(struct psmouse *psmouse)
1148 unsigned char *packet = psmouse->packet;
1150 if (packet[0] == 0x48 && (packet[4] & 0x47) == 0x06)
1151 alps_process_trackstick_packet_v7(psmouse);
1153 alps_process_touchpad_packet_v7(psmouse);
1156 static unsigned char alps_get_pkt_id_ss4_v2(unsigned char *byte)
1158 unsigned char pkt_id = SS4_PACKET_ID_IDLE;
1160 switch (byte[3] & 0x30) {
1162 if (byte[0] == 0x18 && byte[1] == 0x10 && byte[2] == 0x00 &&
1163 (byte[3] & 0x88) == 0x08 && byte[4] == 0x10 &&
1165 pkt_id = SS4_PACKET_ID_IDLE;
1167 pkt_id = SS4_PACKET_ID_ONE;
1171 /* two-finger finger positions */
1172 pkt_id = SS4_PACKET_ID_TWO;
1176 pkt_id = SS4_PACKET_ID_STICK;
1179 /* third and fourth finger positions */
1180 pkt_id = SS4_PACKET_ID_MULTI;
1187 static int alps_decode_ss4_v2(struct alps_fields *f,
1188 unsigned char *p, struct psmouse *psmouse)
1190 struct alps_data *priv = psmouse->private;
1191 unsigned char pkt_id;
1192 unsigned int no_data_x, no_data_y;
1194 pkt_id = alps_get_pkt_id_ss4_v2(p);
1196 /* Current packet is 1Finger coordinate packet */
1198 case SS4_PACKET_ID_ONE:
1199 f->mt[0].x = SS4_1F_X_V2(p);
1200 f->mt[0].y = SS4_1F_Y_V2(p);
1201 f->pressure = ((SS4_1F_Z_V2(p)) * 2) & 0x7f;
1203 * When a button is held the device will give us events
1204 * with x, y, and pressure of 0. This causes annoying jumps
1205 * if a touch is released while the button is held.
1206 * Handle this by claiming zero contacts.
1208 f->fingers = f->pressure > 0 ? 1 : 0;
1213 case SS4_PACKET_ID_TWO:
1214 if (priv->flags & ALPS_BUTTONPAD) {
1215 f->mt[0].x = SS4_BTL_MF_X_V2(p, 0);
1216 f->mt[0].y = SS4_BTL_MF_Y_V2(p, 0);
1217 f->mt[1].x = SS4_BTL_MF_X_V2(p, 1);
1218 f->mt[1].y = SS4_BTL_MF_Y_V2(p, 1);
1220 f->mt[0].x = SS4_STD_MF_X_V2(p, 0);
1221 f->mt[0].y = SS4_STD_MF_Y_V2(p, 0);
1222 f->mt[1].x = SS4_STD_MF_X_V2(p, 1);
1223 f->mt[1].y = SS4_STD_MF_Y_V2(p, 1);
1225 f->pressure = SS4_MF_Z_V2(p, 0) ? 0x30 : 0;
1227 if (SS4_IS_MF_CONTINUE(p)) {
1237 case SS4_PACKET_ID_MULTI:
1238 if (priv->flags & ALPS_BUTTONPAD) {
1239 f->mt[2].x = SS4_BTL_MF_X_V2(p, 0);
1240 f->mt[2].y = SS4_BTL_MF_Y_V2(p, 0);
1241 f->mt[3].x = SS4_BTL_MF_X_V2(p, 1);
1242 f->mt[3].y = SS4_BTL_MF_Y_V2(p, 1);
1243 no_data_x = SS4_MFPACKET_NO_AX_BL;
1244 no_data_y = SS4_MFPACKET_NO_AY_BL;
1246 f->mt[2].x = SS4_STD_MF_X_V2(p, 0);
1247 f->mt[2].y = SS4_STD_MF_Y_V2(p, 0);
1248 f->mt[3].x = SS4_STD_MF_X_V2(p, 1);
1249 f->mt[3].y = SS4_STD_MF_Y_V2(p, 1);
1250 no_data_x = SS4_MFPACKET_NO_AX;
1251 no_data_y = SS4_MFPACKET_NO_AY;
1257 if (SS4_IS_5F_DETECTED(p)) {
1259 } else if (f->mt[3].x == no_data_x &&
1260 f->mt[3].y == no_data_y) {
1269 case SS4_PACKET_ID_STICK:
1270 if (!(priv->flags & ALPS_DUALPOINT)) {
1271 psmouse_warn(psmouse,
1272 "Rejected trackstick packet from non DualPoint device");
1274 int x = (s8)(((p[0] & 1) << 7) | (p[1] & 0x7f));
1275 int y = (s8)(((p[3] & 1) << 7) | (p[2] & 0x7f));
1276 int pressure = (s8)(p[4] & 0x7f);
1278 input_report_rel(priv->dev2, REL_X, x);
1279 input_report_rel(priv->dev2, REL_Y, -y);
1280 input_report_abs(priv->dev2, ABS_PRESSURE, pressure);
1284 case SS4_PACKET_ID_IDLE:
1286 memset(f, 0, sizeof(struct alps_fields));
1290 /* handle buttons */
1291 if (pkt_id == SS4_PACKET_ID_STICK) {
1292 f->ts_left = !!(SS4_BTN_V2(p) & 0x01);
1293 if (!(priv->flags & ALPS_BUTTONPAD)) {
1294 f->ts_right = !!(SS4_BTN_V2(p) & 0x02);
1295 f->ts_middle = !!(SS4_BTN_V2(p) & 0x04);
1298 f->left = !!(SS4_BTN_V2(p) & 0x01);
1299 if (!(priv->flags & ALPS_BUTTONPAD)) {
1300 f->right = !!(SS4_BTN_V2(p) & 0x02);
1301 f->middle = !!(SS4_BTN_V2(p) & 0x04);
1308 static void alps_process_packet_ss4_v2(struct psmouse *psmouse)
1310 struct alps_data *priv = psmouse->private;
1311 unsigned char *packet = psmouse->packet;
1312 struct input_dev *dev = psmouse->dev;
1313 struct input_dev *dev2 = priv->dev2;
1314 struct alps_fields *f = &priv->f;
1316 memset(f, 0, sizeof(struct alps_fields));
1317 priv->decode_fields(f, packet, psmouse);
1318 if (priv->multi_packet) {
1320 * Sometimes the first packet will indicate a multi-packet
1321 * sequence, but sometimes the next multi-packet would not
1322 * come. Check for this, and when it happens process the
1323 * position packet as usual.
1326 /* Now process the 1st packet */
1327 priv->decode_fields(f, priv->multi_data, psmouse);
1329 priv->multi_packet = 0;
1334 * "f.is_mp" would always be '0' after merging the 1st and 2nd packet.
1335 * When it is set, it means 2nd packet comes without 1st packet come.
1340 /* Save the first packet */
1341 if (!priv->multi_packet && f->first_mp) {
1342 priv->multi_packet = 1;
1343 memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
1347 priv->multi_packet = 0;
1349 alps_report_mt_data(psmouse, (f->fingers <= 4) ? f->fingers : 4);
1351 input_mt_report_finger_count(dev, f->fingers);
1353 input_report_key(dev, BTN_LEFT, f->left);
1354 input_report_key(dev, BTN_RIGHT, f->right);
1355 input_report_key(dev, BTN_MIDDLE, f->middle);
1357 input_report_abs(dev, ABS_PRESSURE, f->pressure);
1360 if (priv->flags & ALPS_DUALPOINT) {
1361 input_report_key(dev2, BTN_LEFT, f->ts_left);
1362 input_report_key(dev2, BTN_RIGHT, f->ts_right);
1363 input_report_key(dev2, BTN_MIDDLE, f->ts_middle);
1368 static bool alps_is_valid_package_ss4_v2(struct psmouse *psmouse)
1370 if (psmouse->pktcnt == 4 && ((psmouse->packet[3] & 0x08) != 0x08))
1372 if (psmouse->pktcnt == 6 && ((psmouse->packet[5] & 0x10) != 0x0))
1377 static DEFINE_MUTEX(alps_mutex);
1379 static void alps_register_bare_ps2_mouse(struct work_struct *work)
1381 struct alps_data *priv =
1382 container_of(work, struct alps_data, dev3_register_work.work);
1383 struct psmouse *psmouse = priv->psmouse;
1384 struct input_dev *dev3;
1387 mutex_lock(&alps_mutex);
1392 dev3 = input_allocate_device();
1394 psmouse_err(psmouse, "failed to allocate secondary device\n");
1399 snprintf(priv->phys3, sizeof(priv->phys3), "%s/%s",
1400 psmouse->ps2dev.serio->phys,
1401 (priv->dev2 ? "input2" : "input1"));
1402 dev3->phys = priv->phys3;
1405 * format of input device name is: "protocol vendor name"
1406 * see function psmouse_switch_protocol() in psmouse-base.c
1408 dev3->name = "PS/2 ALPS Mouse";
1410 dev3->id.bustype = BUS_I8042;
1411 dev3->id.vendor = 0x0002;
1412 dev3->id.product = PSMOUSE_PS2;
1413 dev3->id.version = 0x0000;
1414 dev3->dev.parent = &psmouse->ps2dev.serio->dev;
1416 input_set_capability(dev3, EV_REL, REL_X);
1417 input_set_capability(dev3, EV_REL, REL_Y);
1418 input_set_capability(dev3, EV_KEY, BTN_LEFT);
1419 input_set_capability(dev3, EV_KEY, BTN_RIGHT);
1420 input_set_capability(dev3, EV_KEY, BTN_MIDDLE);
1422 __set_bit(INPUT_PROP_POINTER, dev3->propbit);
1424 error = input_register_device(dev3);
1426 psmouse_err(psmouse,
1427 "failed to register secondary device: %d\n",
1429 input_free_device(dev3);
1437 * Save the error code so that we can detect that we
1438 * already tried to create the device.
1441 priv->dev3 = ERR_PTR(error);
1443 mutex_unlock(&alps_mutex);
1446 static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
1447 unsigned char packet[],
1448 bool report_buttons)
1450 struct alps_data *priv = psmouse->private;
1451 struct input_dev *dev, *dev2 = NULL;
1453 /* Figure out which device to use to report the bare packet */
1454 if (priv->proto_version == ALPS_PROTO_V2 &&
1455 (priv->flags & ALPS_DUALPOINT)) {
1456 /* On V2 devices the DualPoint Stick reports bare packets */
1458 dev2 = psmouse->dev;
1459 } else if (unlikely(IS_ERR_OR_NULL(priv->dev3))) {
1460 /* Register dev3 mouse if we received PS/2 packet first time */
1461 if (!IS_ERR(priv->dev3))
1462 psmouse_queue_work(psmouse, &priv->dev3_register_work,
1470 alps_report_buttons(dev, dev2,
1471 packet[0] & 1, packet[0] & 2, packet[0] & 4);
1473 input_report_rel(dev, REL_X,
1474 packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
1475 input_report_rel(dev, REL_Y,
1476 packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
1481 static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
1483 struct alps_data *priv = psmouse->private;
1485 if (psmouse->pktcnt < 6)
1486 return PSMOUSE_GOOD_DATA;
1488 if (psmouse->pktcnt == 6) {
1490 * Start a timer to flush the packet if it ends up last
1491 * 6-byte packet in the stream. Timer needs to fire
1492 * psmouse core times out itself. 20 ms should be enough
1493 * to decide if we are getting more data or not.
1495 mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
1496 return PSMOUSE_GOOD_DATA;
1499 del_timer(&priv->timer);
1501 if (psmouse->packet[6] & 0x80) {
1504 * Highest bit is set - that means we either had
1505 * complete ALPS packet and this is start of the
1506 * next packet or we got garbage.
1509 if (((psmouse->packet[3] |
1510 psmouse->packet[4] |
1511 psmouse->packet[5]) & 0x80) ||
1512 (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) {
1513 psmouse_dbg(psmouse,
1514 "refusing packet %4ph (suspected interleaved ps/2)\n",
1515 psmouse->packet + 3);
1516 return PSMOUSE_BAD_DATA;
1519 priv->process_packet(psmouse);
1521 /* Continue with the next packet */
1522 psmouse->packet[0] = psmouse->packet[6];
1523 psmouse->pktcnt = 1;
1528 * High bit is 0 - that means that we indeed got a PS/2
1529 * packet in the middle of ALPS packet.
1531 * There is also possibility that we got 6-byte ALPS
1532 * packet followed by 3-byte packet from trackpoint. We
1533 * can not distinguish between these 2 scenarios but
1534 * because the latter is unlikely to happen in course of
1535 * normal operation (user would need to press all
1536 * buttons on the pad and start moving trackpoint
1537 * without touching the pad surface) we assume former.
1538 * Even if we are wrong the wost thing that would happen
1539 * the cursor would jump but we should not get protocol
1540 * de-synchronization.
1543 alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
1547 * Continue with the standard ALPS protocol handling,
1548 * but make sure we won't process it as an interleaved
1549 * packet again, which may happen if all buttons are
1550 * pressed. To avoid this let's reset the 4th bit which
1553 psmouse->packet[3] = psmouse->packet[6] & 0xf7;
1554 psmouse->pktcnt = 4;
1557 return PSMOUSE_GOOD_DATA;
1560 static void alps_flush_packet(unsigned long data)
1562 struct psmouse *psmouse = (struct psmouse *)data;
1563 struct alps_data *priv = psmouse->private;
1565 serio_pause_rx(psmouse->ps2dev.serio);
1567 if (psmouse->pktcnt == psmouse->pktsize) {
1570 * We did not any more data in reasonable amount of time.
1571 * Validate the last 3 bytes and process as a standard
1574 if ((psmouse->packet[3] |
1575 psmouse->packet[4] |
1576 psmouse->packet[5]) & 0x80) {
1577 psmouse_dbg(psmouse,
1578 "refusing packet %3ph (suspected interleaved ps/2)\n",
1579 psmouse->packet + 3);
1581 priv->process_packet(psmouse);
1583 psmouse->pktcnt = 0;
1586 serio_continue_rx(psmouse->ps2dev.serio);
1589 static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
1591 struct alps_data *priv = psmouse->private;
1594 * Check if we are dealing with a bare PS/2 packet, presumably from
1595 * a device connected to the external PS/2 port. Because bare PS/2
1596 * protocol does not have enough constant bits to self-synchronize
1597 * properly we only do this if the device is fully synchronized.
1598 * Can not distinguish V8's first byte from PS/2 packet's
1600 if (priv->proto_version != ALPS_PROTO_V8 &&
1601 !psmouse->out_of_sync_cnt &&
1602 (psmouse->packet[0] & 0xc8) == 0x08) {
1604 if (psmouse->pktcnt == 3) {
1605 alps_report_bare_ps2_packet(psmouse, psmouse->packet,
1607 return PSMOUSE_FULL_PACKET;
1609 return PSMOUSE_GOOD_DATA;
1612 /* Check for PS/2 packet stuffed in the middle of ALPS packet. */
1614 if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
1615 psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
1616 return alps_handle_interleaved_ps2(psmouse);
1619 if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
1620 psmouse_dbg(psmouse,
1621 "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
1622 psmouse->packet[0], priv->mask0, priv->byte0);
1623 return PSMOUSE_BAD_DATA;
1626 /* Bytes 2 - pktsize should have 0 in the highest bit */
1627 if (priv->proto_version < ALPS_PROTO_V5 &&
1628 psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
1629 (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
1630 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1631 psmouse->pktcnt - 1,
1632 psmouse->packet[psmouse->pktcnt - 1]);
1634 if (priv->proto_version == ALPS_PROTO_V3_RUSHMORE &&
1635 psmouse->pktcnt == psmouse->pktsize) {
1637 * Some Dell boxes, such as Latitude E6440 or E7440
1638 * with closed lid, quite often smash last byte of
1639 * otherwise valid packet with 0xff. Given that the
1640 * next packet is very likely to be valid let's
1641 * report PSMOUSE_FULL_PACKET but not process data,
1642 * rather than reporting PSMOUSE_BAD_DATA and
1645 return PSMOUSE_FULL_PACKET;
1648 return PSMOUSE_BAD_DATA;
1651 if ((priv->proto_version == ALPS_PROTO_V7 &&
1652 !alps_is_valid_package_v7(psmouse)) ||
1653 (priv->proto_version == ALPS_PROTO_V8 &&
1654 !alps_is_valid_package_ss4_v2(psmouse))) {
1655 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1656 psmouse->pktcnt - 1,
1657 psmouse->packet[psmouse->pktcnt - 1]);
1658 return PSMOUSE_BAD_DATA;
1661 if (psmouse->pktcnt == psmouse->pktsize) {
1662 priv->process_packet(psmouse);
1663 return PSMOUSE_FULL_PACKET;
1666 return PSMOUSE_GOOD_DATA;
1669 static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
1671 struct ps2dev *ps2dev = &psmouse->ps2dev;
1672 struct alps_data *priv = psmouse->private;
1674 unsigned char *param;
1675 unsigned char dummy[4];
1677 BUG_ON(nibble > 0xf);
1679 command = priv->nibble_commands[nibble].command;
1680 param = (command & 0x0f00) ?
1681 dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
1683 if (ps2_command(ps2dev, param, command))
1689 static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
1691 struct ps2dev *ps2dev = &psmouse->ps2dev;
1692 struct alps_data *priv = psmouse->private;
1695 if (ps2_command(ps2dev, NULL, priv->addr_command))
1698 for (i = 12; i >= 0; i -= 4) {
1699 nibble = (addr >> i) & 0xf;
1700 if (alps_command_mode_send_nibble(psmouse, nibble))
1707 static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1709 struct ps2dev *ps2dev = &psmouse->ps2dev;
1710 unsigned char param[4];
1712 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1716 * The address being read is returned in the first two bytes
1717 * of the result. Check that this address matches the expected
1720 if (addr != ((param[0] << 8) | param[1]))
1726 static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1728 if (alps_command_mode_set_addr(psmouse, addr))
1730 return __alps_command_mode_read_reg(psmouse, addr);
1733 static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
1735 if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
1737 if (alps_command_mode_send_nibble(psmouse, value & 0xf))
1742 static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
1745 if (alps_command_mode_set_addr(psmouse, addr))
1747 return __alps_command_mode_write_reg(psmouse, value);
1750 static int alps_rpt_cmd(struct psmouse *psmouse, int init_command,
1751 int repeated_command, unsigned char *param)
1753 struct ps2dev *ps2dev = &psmouse->ps2dev;
1756 if (init_command && ps2_command(ps2dev, param, init_command))
1759 if (ps2_command(ps2dev, NULL, repeated_command) ||
1760 ps2_command(ps2dev, NULL, repeated_command) ||
1761 ps2_command(ps2dev, NULL, repeated_command))
1764 param[0] = param[1] = param[2] = 0xff;
1765 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1768 psmouse_dbg(psmouse, "%2.2X report: %3ph\n",
1769 repeated_command, param);
1773 static bool alps_check_valid_firmware_id(unsigned char id[])
1778 if (id[0] == 0x88 &&
1781 (id[1] & 0xf0) == 0xb0 ||
1782 (id[1] & 0xf0) == 0xc0)) {
1789 static int alps_enter_command_mode(struct psmouse *psmouse)
1791 unsigned char param[4];
1793 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) {
1794 psmouse_err(psmouse, "failed to enter command mode\n");
1798 if (!alps_check_valid_firmware_id(param)) {
1799 psmouse_dbg(psmouse,
1800 "unknown response while entering command mode\n");
1806 static inline int alps_exit_command_mode(struct psmouse *psmouse)
1808 struct ps2dev *ps2dev = &psmouse->ps2dev;
1809 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
1815 * For DualPoint devices select the device that should respond to
1816 * subsequent commands. It looks like glidepad is behind stickpointer,
1817 * I'd thought it would be other way around...
1819 static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
1821 struct ps2dev *ps2dev = &psmouse->ps2dev;
1822 int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
1824 if (ps2_command(ps2dev, NULL, cmd) ||
1825 ps2_command(ps2dev, NULL, cmd) ||
1826 ps2_command(ps2dev, NULL, cmd) ||
1827 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1830 /* we may get 3 more bytes, just ignore them */
1831 ps2_drain(ps2dev, 3, 100);
1836 static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
1838 struct ps2dev *ps2dev = &psmouse->ps2dev;
1840 /* Try ALPS magic knock - 4 disable before enable */
1841 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1842 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1843 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1844 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1845 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
1849 * Switch mouse to poll (remote) mode so motion data will not
1852 return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
1855 static int alps_monitor_mode_send_word(struct psmouse *psmouse, u16 word)
1860 * b0-b11 are valid bits, send sequence is inverse.
1861 * e.g. when word = 0x0123, nibble send sequence is 3, 2, 1
1863 for (i = 0; i <= 8; i += 4) {
1864 nibble = (word >> i) & 0xf;
1865 if (alps_command_mode_send_nibble(psmouse, nibble))
1872 static int alps_monitor_mode_write_reg(struct psmouse *psmouse,
1873 u16 addr, u16 value)
1875 struct ps2dev *ps2dev = &psmouse->ps2dev;
1877 /* 0x0A0 is the command to write the word */
1878 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE) ||
1879 alps_monitor_mode_send_word(psmouse, 0x0A0) ||
1880 alps_monitor_mode_send_word(psmouse, addr) ||
1881 alps_monitor_mode_send_word(psmouse, value) ||
1882 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1888 static int alps_monitor_mode(struct psmouse *psmouse, bool enable)
1890 struct ps2dev *ps2dev = &psmouse->ps2dev;
1893 /* EC E9 F5 F5 E7 E6 E7 E9 to enter monitor mode */
1894 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
1895 ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO) ||
1896 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1897 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1898 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1899 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1900 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1901 ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO))
1904 /* EC to exit monitor mode */
1905 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP))
1912 static int alps_absolute_mode_v6(struct psmouse *psmouse)
1914 u16 reg_val = 0x181;
1917 /* enter monitor mode, to write the register */
1918 if (alps_monitor_mode(psmouse, true))
1921 ret = alps_monitor_mode_write_reg(psmouse, 0x000, reg_val);
1923 if (alps_monitor_mode(psmouse, false))
1929 static int alps_get_status(struct psmouse *psmouse, char *param)
1931 /* Get status: 0xF5 0xF5 0xF5 0xE9 */
1932 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param))
1939 * Turn touchpad tapping on or off. The sequences are:
1940 * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
1941 * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
1942 * My guess that 0xE9 (GetInfo) is here as a sync point.
1943 * For models that also have stickpointer (DualPoints) its tapping
1944 * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
1945 * we don't fiddle with it.
1947 static int alps_tap_mode(struct psmouse *psmouse, int enable)
1949 struct ps2dev *ps2dev = &psmouse->ps2dev;
1950 int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
1951 unsigned char tap_arg = enable ? 0x0A : 0x00;
1952 unsigned char param[4];
1954 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
1955 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1956 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1957 ps2_command(ps2dev, &tap_arg, cmd))
1960 if (alps_get_status(psmouse, param))
1967 * alps_poll() - poll the touchpad for current motion packet.
1970 static int alps_poll(struct psmouse *psmouse)
1972 struct alps_data *priv = psmouse->private;
1973 unsigned char buf[sizeof(psmouse->packet)];
1976 if (priv->flags & ALPS_PASS)
1977 alps_passthrough_mode_v2(psmouse, true);
1979 poll_failed = ps2_command(&psmouse->ps2dev, buf,
1980 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
1982 if (priv->flags & ALPS_PASS)
1983 alps_passthrough_mode_v2(psmouse, false);
1985 if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
1988 if ((psmouse->badbyte & 0xc8) == 0x08) {
1990 * Poll the track stick ...
1992 if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
1996 memcpy(psmouse->packet, buf, sizeof(buf));
2000 static int alps_hw_init_v1_v2(struct psmouse *psmouse)
2002 struct alps_data *priv = psmouse->private;
2004 if ((priv->flags & ALPS_PASS) &&
2005 alps_passthrough_mode_v2(psmouse, true)) {
2009 if (alps_tap_mode(psmouse, true)) {
2010 psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
2014 if (alps_absolute_mode_v1_v2(psmouse)) {
2015 psmouse_err(psmouse, "Failed to enable absolute mode\n");
2019 if ((priv->flags & ALPS_PASS) &&
2020 alps_passthrough_mode_v2(psmouse, false)) {
2024 /* ALPS needs stream mode, otherwise it won't report any data */
2025 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
2026 psmouse_err(psmouse, "Failed to enable stream mode\n");
2033 static int alps_hw_init_v6(struct psmouse *psmouse)
2035 unsigned char param[2] = {0xC8, 0x14};
2037 /* Enter passthrough mode to let trackpoint enter 6byte raw mode */
2038 if (alps_passthrough_mode_v2(psmouse, true))
2041 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2042 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2043 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2044 ps2_command(&psmouse->ps2dev, ¶m[0], PSMOUSE_CMD_SETRATE) ||
2045 ps2_command(&psmouse->ps2dev, ¶m[1], PSMOUSE_CMD_SETRATE))
2048 if (alps_passthrough_mode_v2(psmouse, false))
2051 if (alps_absolute_mode_v6(psmouse)) {
2052 psmouse_err(psmouse, "Failed to enable absolute mode\n");
2060 * Enable or disable passthrough mode to the trackstick.
2062 static int alps_passthrough_mode_v3(struct psmouse *psmouse,
2063 int reg_base, bool enable)
2065 int reg_val, ret = -1;
2067 if (alps_enter_command_mode(psmouse))
2070 reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x0008);
2079 ret = __alps_command_mode_write_reg(psmouse, reg_val);
2082 if (alps_exit_command_mode(psmouse))
2087 /* Must be in command mode when calling this function */
2088 static int alps_absolute_mode_v3(struct psmouse *psmouse)
2092 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
2097 if (__alps_command_mode_write_reg(psmouse, reg_val))
2103 static int alps_probe_trackstick_v3_v7(struct psmouse *psmouse, int reg_base)
2105 int ret = -EIO, reg_val;
2107 if (alps_enter_command_mode(psmouse))
2110 reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x08);
2114 /* bit 7: trackstick is present */
2115 ret = reg_val & 0x80 ? 0 : -ENODEV;
2118 alps_exit_command_mode(psmouse);
2122 static int alps_setup_trackstick_v3(struct psmouse *psmouse, int reg_base)
2124 struct ps2dev *ps2dev = &psmouse->ps2dev;
2126 unsigned char param[4];
2128 if (alps_passthrough_mode_v3(psmouse, reg_base, true))
2132 * E7 report for the trackstick
2134 * There have been reports of failures to seem to trace back
2135 * to the above trackstick check failing. When these occur
2136 * this E7 report fails, so when that happens we continue
2137 * with the assumption that there isn't a trackstick after
2140 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_SETSCALE21, param)) {
2141 psmouse_warn(psmouse, "Failed to initialize trackstick (E7 report failed)\n");
2144 psmouse_dbg(psmouse, "trackstick E7 report: %3ph\n", param);
2147 * Not sure what this does, but it is absolutely
2148 * essential. Without it, the touchpad does not
2149 * work at all and the trackstick just emits normal
2152 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2153 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2154 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2155 alps_command_mode_send_nibble(psmouse, 0x9) ||
2156 alps_command_mode_send_nibble(psmouse, 0x4)) {
2157 psmouse_err(psmouse,
2158 "Error sending magic E6 sequence\n");
2164 * This ensures the trackstick packets are in the format
2165 * supported by this driver. If bit 1 isn't set the packet
2166 * format is different.
2168 if (alps_enter_command_mode(psmouse) ||
2169 alps_command_mode_write_reg(psmouse,
2170 reg_base + 0x08, 0x82) ||
2171 alps_exit_command_mode(psmouse))
2176 if (alps_passthrough_mode_v3(psmouse, reg_base, false))
2182 static int alps_hw_init_v3(struct psmouse *psmouse)
2184 struct alps_data *priv = psmouse->private;
2185 struct ps2dev *ps2dev = &psmouse->ps2dev;
2187 unsigned char param[4];
2189 if ((priv->flags & ALPS_DUALPOINT) &&
2190 alps_setup_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE) == -EIO)
2193 if (alps_enter_command_mode(psmouse) ||
2194 alps_absolute_mode_v3(psmouse)) {
2195 psmouse_err(psmouse, "Failed to enter absolute mode\n");
2199 reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
2202 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
2205 reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
2208 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
2211 if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
2213 if (__alps_command_mode_write_reg(psmouse, 0x04))
2216 if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
2218 if (__alps_command_mode_write_reg(psmouse, 0x03))
2221 if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
2223 if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
2226 if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
2228 if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
2231 alps_exit_command_mode(psmouse);
2233 /* Set rate and enable data reporting */
2235 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
2236 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
2237 psmouse_err(psmouse, "Failed to enable data reporting\n");
2245 * Leaving the touchpad in command mode will essentially render
2246 * it unusable until the machine reboots, so exit it here just
2249 alps_exit_command_mode(psmouse);
2253 static int alps_get_v3_v7_resolution(struct psmouse *psmouse, int reg_pitch)
2255 int reg, x_pitch, y_pitch, x_electrode, y_electrode, x_phys, y_phys;
2256 struct alps_data *priv = psmouse->private;
2258 reg = alps_command_mode_read_reg(psmouse, reg_pitch);
2262 x_pitch = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
2263 x_pitch = 50 + 2 * x_pitch; /* In 0.1 mm units */
2265 y_pitch = (char)reg >> 4; /* sign extend upper 4 bits */
2266 y_pitch = 36 + 2 * y_pitch; /* In 0.1 mm units */
2268 reg = alps_command_mode_read_reg(psmouse, reg_pitch + 1);
2272 x_electrode = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
2273 x_electrode = 17 + x_electrode;
2275 y_electrode = (char)reg >> 4; /* sign extend upper 4 bits */
2276 y_electrode = 13 + y_electrode;
2278 x_phys = x_pitch * (x_electrode - 1); /* In 0.1 mm units */
2279 y_phys = y_pitch * (y_electrode - 1); /* In 0.1 mm units */
2281 priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
2282 priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
2284 psmouse_dbg(psmouse,
2285 "pitch %dx%d num-electrodes %dx%d physical size %dx%d mm res %dx%d\n",
2286 x_pitch, y_pitch, x_electrode, y_electrode,
2287 x_phys / 10, y_phys / 10, priv->x_res, priv->y_res);
2292 static int alps_hw_init_rushmore_v3(struct psmouse *psmouse)
2294 struct alps_data *priv = psmouse->private;
2295 struct ps2dev *ps2dev = &psmouse->ps2dev;
2296 int reg_val, ret = -1;
2298 if (priv->flags & ALPS_DUALPOINT) {
2299 reg_val = alps_setup_trackstick_v3(psmouse,
2300 ALPS_REG_BASE_RUSHMORE);
2301 if (reg_val == -EIO)
2305 if (alps_enter_command_mode(psmouse) ||
2306 alps_command_mode_read_reg(psmouse, 0xc2d9) == -1 ||
2307 alps_command_mode_write_reg(psmouse, 0xc2cb, 0x00))
2310 if (alps_get_v3_v7_resolution(psmouse, 0xc2da))
2313 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c6);
2316 if (__alps_command_mode_write_reg(psmouse, reg_val & 0xfd))
2319 if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
2322 /* enter absolute mode */
2323 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
2326 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
2329 alps_exit_command_mode(psmouse);
2330 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2333 alps_exit_command_mode(psmouse);
2337 /* Must be in command mode when calling this function */
2338 static int alps_absolute_mode_v4(struct psmouse *psmouse)
2342 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
2347 if (__alps_command_mode_write_reg(psmouse, reg_val))
2353 static int alps_hw_init_v4(struct psmouse *psmouse)
2355 struct ps2dev *ps2dev = &psmouse->ps2dev;
2356 unsigned char param[4];
2358 if (alps_enter_command_mode(psmouse))
2361 if (alps_absolute_mode_v4(psmouse)) {
2362 psmouse_err(psmouse, "Failed to enter absolute mode\n");
2366 if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
2369 if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
2372 if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
2375 if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
2378 if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
2381 if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
2384 if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
2387 if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
2390 alps_exit_command_mode(psmouse);
2393 * This sequence changes the output from a 9-byte to an
2394 * 8-byte format. All the same data seems to be present,
2395 * just in a more compact format.
2400 if (ps2_command(ps2dev, ¶m[0], PSMOUSE_CMD_SETRATE) ||
2401 ps2_command(ps2dev, ¶m[1], PSMOUSE_CMD_SETRATE) ||
2402 ps2_command(ps2dev, ¶m[2], PSMOUSE_CMD_SETRATE) ||
2403 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
2406 /* Set rate and enable data reporting */
2408 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
2409 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
2410 psmouse_err(psmouse, "Failed to enable data reporting\n");
2418 * Leaving the touchpad in command mode will essentially render
2419 * it unusable until the machine reboots, so exit it here just
2422 alps_exit_command_mode(psmouse);
2426 static int alps_get_otp_values_ss4_v2(struct psmouse *psmouse,
2427 unsigned char index, unsigned char otp[])
2429 struct ps2dev *ps2dev = &psmouse->ps2dev;
2433 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2434 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2435 ps2_command(ps2dev, otp, PSMOUSE_CMD_GETINFO))
2441 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2442 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2443 ps2_command(ps2dev, otp, PSMOUSE_CMD_GETINFO))
2452 static int alps_update_device_area_ss4_v2(unsigned char otp[][4],
2453 struct alps_data *priv)
2455 int num_x_electrode;
2456 int num_y_electrode;
2457 int x_pitch, y_pitch, x_phys, y_phys;
2459 num_x_electrode = SS4_NUMSENSOR_XOFFSET + (otp[1][0] & 0x0F);
2460 num_y_electrode = SS4_NUMSENSOR_YOFFSET + ((otp[1][0] >> 4) & 0x0F);
2462 priv->x_max = (num_x_electrode - 1) * SS4_COUNT_PER_ELECTRODE;
2463 priv->y_max = (num_y_electrode - 1) * SS4_COUNT_PER_ELECTRODE;
2465 x_pitch = ((otp[1][2] >> 2) & 0x07) + SS4_MIN_PITCH_MM;
2466 y_pitch = ((otp[1][2] >> 5) & 0x07) + SS4_MIN_PITCH_MM;
2468 x_phys = x_pitch * (num_x_electrode - 1); /* In 0.1 mm units */
2469 y_phys = y_pitch * (num_y_electrode - 1); /* In 0.1 mm units */
2471 priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
2472 priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
2477 static int alps_update_btn_info_ss4_v2(unsigned char otp[][4],
2478 struct alps_data *priv)
2480 unsigned char is_btnless;
2482 is_btnless = (otp[1][1] >> 3) & 0x01;
2485 priv->flags |= ALPS_BUTTONPAD;
2490 static int alps_set_defaults_ss4_v2(struct psmouse *psmouse,
2491 struct alps_data *priv)
2493 unsigned char otp[2][4];
2495 memset(otp, 0, sizeof(otp));
2497 if (alps_get_otp_values_ss4_v2(psmouse, 0, &otp[0][0]) ||
2498 alps_get_otp_values_ss4_v2(psmouse, 1, &otp[1][0]))
2501 alps_update_device_area_ss4_v2(otp, priv);
2503 alps_update_btn_info_ss4_v2(otp, priv);
2508 static int alps_dolphin_get_device_area(struct psmouse *psmouse,
2509 struct alps_data *priv)
2511 struct ps2dev *ps2dev = &psmouse->ps2dev;
2512 unsigned char param[4] = {0};
2513 int num_x_electrode, num_y_electrode;
2515 if (alps_enter_command_mode(psmouse))
2519 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
2520 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2521 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2522 ps2_command(ps2dev, ¶m[0], PSMOUSE_CMD_SETRATE) ||
2523 ps2_command(ps2dev, ¶m[0], PSMOUSE_CMD_SETRATE))
2526 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
2530 * Dolphin's sensor line number is not fixed. It can be calculated
2531 * by adding the device's register value with DOLPHIN_PROFILE_X/YOFFSET.
2532 * Further more, we can get device's x_max and y_max by multiplying
2533 * sensor line number with DOLPHIN_COUNT_PER_ELECTRODE.
2535 * e.g. When we get register's sensor_x = 11 & sensor_y = 8,
2536 * real sensor line number X = 11 + 8 = 19, and
2537 * real sensor line number Y = 8 + 1 = 9.
2538 * So, x_max = (19 - 1) * 64 = 1152, and
2539 * y_max = (9 - 1) * 64 = 512.
2541 num_x_electrode = DOLPHIN_PROFILE_XOFFSET + (param[2] & 0x0F);
2542 num_y_electrode = DOLPHIN_PROFILE_YOFFSET + ((param[2] >> 4) & 0x0F);
2543 priv->x_bits = num_x_electrode;
2544 priv->y_bits = num_y_electrode;
2545 priv->x_max = (num_x_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2546 priv->y_max = (num_y_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2548 if (alps_exit_command_mode(psmouse))
2554 static int alps_hw_init_dolphin_v1(struct psmouse *psmouse)
2556 struct ps2dev *ps2dev = &psmouse->ps2dev;
2557 unsigned char param[2];
2559 /* This is dolphin "v1" as empirically defined by florin9doi */
2563 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2564 ps2_command(ps2dev, ¶m[0], PSMOUSE_CMD_SETRATE) ||
2565 ps2_command(ps2dev, ¶m[1], PSMOUSE_CMD_SETRATE))
2571 static int alps_hw_init_v7(struct psmouse *psmouse)
2573 struct ps2dev *ps2dev = &psmouse->ps2dev;
2574 int reg_val, ret = -1;
2576 if (alps_enter_command_mode(psmouse) ||
2577 alps_command_mode_read_reg(psmouse, 0xc2d9) == -1)
2580 if (alps_get_v3_v7_resolution(psmouse, 0xc397))
2583 if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
2586 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
2589 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
2592 alps_exit_command_mode(psmouse);
2593 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2596 alps_exit_command_mode(psmouse);
2600 static int alps_hw_init_ss4_v2(struct psmouse *psmouse)
2602 struct ps2dev *ps2dev = &psmouse->ps2dev;
2603 char param[2] = {0x64, 0x28};
2606 /* enter absolute mode */
2607 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2608 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2609 ps2_command(ps2dev, ¶m[0], PSMOUSE_CMD_SETRATE) ||
2610 ps2_command(ps2dev, ¶m[1], PSMOUSE_CMD_SETRATE)) {
2614 /* T.B.D. Decread noise packet number, delete in the future */
2615 if (alps_exit_command_mode(psmouse) ||
2616 alps_enter_command_mode(psmouse) ||
2617 alps_command_mode_write_reg(psmouse, 0x001D, 0x20)) {
2620 alps_exit_command_mode(psmouse);
2622 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2625 alps_exit_command_mode(psmouse);
2629 static int alps_set_protocol(struct psmouse *psmouse,
2630 struct alps_data *priv,
2631 const struct alps_protocol_info *protocol)
2633 psmouse->private = priv;
2635 setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
2637 priv->proto_version = protocol->version;
2638 priv->byte0 = protocol->byte0;
2639 priv->mask0 = protocol->mask0;
2640 priv->flags = protocol->flags;
2647 switch (priv->proto_version) {
2650 priv->hw_init = alps_hw_init_v1_v2;
2651 priv->process_packet = alps_process_packet_v1_v2;
2652 priv->set_abs_params = alps_set_abs_params_st;
2655 if (dmi_check_system(alps_dmi_has_separate_stick_buttons))
2656 priv->flags |= ALPS_STICK_BITS;
2660 priv->hw_init = alps_hw_init_v3;
2661 priv->process_packet = alps_process_packet_v3;
2662 priv->set_abs_params = alps_set_abs_params_semi_mt;
2663 priv->decode_fields = alps_decode_pinnacle;
2664 priv->nibble_commands = alps_v3_nibble_commands;
2665 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2667 if (alps_probe_trackstick_v3_v7(psmouse,
2668 ALPS_REG_BASE_PINNACLE) < 0)
2669 priv->flags &= ~ALPS_DUALPOINT;
2673 case ALPS_PROTO_V3_RUSHMORE:
2674 priv->hw_init = alps_hw_init_rushmore_v3;
2675 priv->process_packet = alps_process_packet_v3;
2676 priv->set_abs_params = alps_set_abs_params_semi_mt;
2677 priv->decode_fields = alps_decode_rushmore;
2678 priv->nibble_commands = alps_v3_nibble_commands;
2679 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2683 if (alps_probe_trackstick_v3_v7(psmouse,
2684 ALPS_REG_BASE_RUSHMORE) < 0)
2685 priv->flags &= ~ALPS_DUALPOINT;
2690 priv->hw_init = alps_hw_init_v4;
2691 priv->process_packet = alps_process_packet_v4;
2692 priv->set_abs_params = alps_set_abs_params_semi_mt;
2693 priv->nibble_commands = alps_v4_nibble_commands;
2694 priv->addr_command = PSMOUSE_CMD_DISABLE;
2698 priv->hw_init = alps_hw_init_dolphin_v1;
2699 priv->process_packet = alps_process_touchpad_packet_v3_v5;
2700 priv->decode_fields = alps_decode_dolphin;
2701 priv->set_abs_params = alps_set_abs_params_semi_mt;
2702 priv->nibble_commands = alps_v3_nibble_commands;
2703 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2707 if (alps_dolphin_get_device_area(psmouse, priv))
2713 priv->hw_init = alps_hw_init_v6;
2714 priv->process_packet = alps_process_packet_v6;
2715 priv->set_abs_params = alps_set_abs_params_st;
2716 priv->nibble_commands = alps_v6_nibble_commands;
2722 priv->hw_init = alps_hw_init_v7;
2723 priv->process_packet = alps_process_packet_v7;
2724 priv->decode_fields = alps_decode_packet_v7;
2725 priv->set_abs_params = alps_set_abs_params_v7;
2726 priv->nibble_commands = alps_v3_nibble_commands;
2727 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2728 priv->x_max = 0xfff;
2729 priv->y_max = 0x7ff;
2731 if (priv->fw_ver[1] != 0xba)
2732 priv->flags |= ALPS_BUTTONPAD;
2734 if (alps_probe_trackstick_v3_v7(psmouse, ALPS_REG_BASE_V7) < 0)
2735 priv->flags &= ~ALPS_DUALPOINT;
2740 priv->hw_init = alps_hw_init_ss4_v2;
2741 priv->process_packet = alps_process_packet_ss4_v2;
2742 priv->decode_fields = alps_decode_ss4_v2;
2743 priv->set_abs_params = alps_set_abs_params_ss4_v2;
2744 priv->nibble_commands = alps_v3_nibble_commands;
2745 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2747 if (alps_set_defaults_ss4_v2(psmouse, priv))
2750 if (priv->fw_ver[1] == 0x1)
2751 priv->flags |= ALPS_DUALPOINT |
2752 ALPS_DUALPOINT_WITH_PRESSURE;
2760 static const struct alps_protocol_info *alps_match_table(unsigned char *e7,
2763 const struct alps_model_info *model;
2766 for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
2767 model = &alps_model_data[i];
2769 if (!memcmp(e7, model->signature, sizeof(model->signature)) &&
2770 (!model->command_mode_resp ||
2771 model->command_mode_resp == ec[2])) {
2773 return &model->protocol_info;
2780 static int alps_identify(struct psmouse *psmouse, struct alps_data *priv)
2782 const struct alps_protocol_info *protocol;
2783 unsigned char e6[4], e7[4], ec[4];
2787 * First try "E6 report".
2788 * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
2789 * The bits 0-2 of the first byte will be 1s if some buttons are
2792 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2793 PSMOUSE_CMD_SETSCALE11, e6))
2796 if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100))
2800 * Now get the "E7" and "EC" reports. These will uniquely identify
2801 * most ALPS touchpads.
2803 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2804 PSMOUSE_CMD_SETSCALE21, e7) ||
2805 alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2806 PSMOUSE_CMD_RESET_WRAP, ec) ||
2807 alps_exit_command_mode(psmouse))
2810 protocol = alps_match_table(e7, ec);
2812 if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0x50 &&
2813 ec[0] == 0x73 && (ec[1] == 0x01 || ec[1] == 0x02)) {
2814 protocol = &alps_v5_protocol_data;
2815 } else if (ec[0] == 0x88 &&
2816 ((ec[1] & 0xf0) == 0xb0 || (ec[1] & 0xf0) == 0xc0)) {
2817 protocol = &alps_v7_protocol_data;
2818 } else if (ec[0] == 0x88 && ec[1] == 0x08) {
2819 protocol = &alps_v3_rushmore_data;
2820 } else if (ec[0] == 0x88 && ec[1] == 0x07 &&
2821 ec[2] >= 0x90 && ec[2] <= 0x9d) {
2822 protocol = &alps_v3_protocol_data;
2823 } else if (e7[0] == 0x73 && e7[1] == 0x03 &&
2824 e7[2] == 0x14 && ec[1] == 0x02) {
2825 protocol = &alps_v8_protocol_data;
2826 } else if (e7[0] == 0x73 && e7[1] == 0x03 &&
2827 e7[2] == 0x28 && ec[1] == 0x01) {
2828 protocol = &alps_v8_protocol_data;
2830 psmouse_dbg(psmouse,
2831 "Likely not an ALPS touchpad: E7=%3ph, EC=%3ph\n", e7, ec);
2837 /* Save the Firmware version */
2838 memcpy(priv->fw_ver, ec, 3);
2839 error = alps_set_protocol(psmouse, priv, protocol);
2847 static int alps_reconnect(struct psmouse *psmouse)
2849 struct alps_data *priv = psmouse->private;
2851 psmouse_reset(psmouse);
2853 if (alps_identify(psmouse, priv) < 0)
2856 return priv->hw_init(psmouse);
2859 static void alps_disconnect(struct psmouse *psmouse)
2861 struct alps_data *priv = psmouse->private;
2863 psmouse_reset(psmouse);
2864 del_timer_sync(&priv->timer);
2866 input_unregister_device(priv->dev2);
2867 if (!IS_ERR_OR_NULL(priv->dev3))
2868 input_unregister_device(priv->dev3);
2872 static void alps_set_abs_params_st(struct alps_data *priv,
2873 struct input_dev *dev1)
2875 input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
2876 input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
2877 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
2880 static void alps_set_abs_params_mt_common(struct alps_data *priv,
2881 struct input_dev *dev1)
2883 input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
2884 input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
2886 input_abs_set_res(dev1, ABS_MT_POSITION_X, priv->x_res);
2887 input_abs_set_res(dev1, ABS_MT_POSITION_Y, priv->y_res);
2889 set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
2890 set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
2893 static void alps_set_abs_params_semi_mt(struct alps_data *priv,
2894 struct input_dev *dev1)
2896 alps_set_abs_params_mt_common(priv, dev1);
2897 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
2899 input_mt_init_slots(dev1, MAX_TOUCHES,
2900 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
2904 static void alps_set_abs_params_v7(struct alps_data *priv,
2905 struct input_dev *dev1)
2907 alps_set_abs_params_mt_common(priv, dev1);
2908 set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
2910 input_mt_init_slots(dev1, MAX_TOUCHES,
2911 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
2914 set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
2917 static void alps_set_abs_params_ss4_v2(struct alps_data *priv,
2918 struct input_dev *dev1)
2920 alps_set_abs_params_mt_common(priv, dev1);
2921 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
2922 set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
2924 input_mt_init_slots(dev1, MAX_TOUCHES,
2925 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
2929 int alps_init(struct psmouse *psmouse)
2931 struct alps_data *priv = psmouse->private;
2932 struct input_dev *dev1 = psmouse->dev;
2935 error = priv->hw_init(psmouse);
2940 * Undo part of setup done for us by psmouse core since touchpad
2941 * is not a relative device.
2943 __clear_bit(EV_REL, dev1->evbit);
2944 __clear_bit(REL_X, dev1->relbit);
2945 __clear_bit(REL_Y, dev1->relbit);
2948 * Now set up our capabilities.
2950 dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
2951 dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
2952 dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
2953 dev1->keybit[BIT_WORD(BTN_LEFT)] |=
2954 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
2956 dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
2958 priv->set_abs_params(priv, dev1);
2960 if (priv->flags & ALPS_WHEEL) {
2961 dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
2962 dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
2965 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
2966 dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
2967 dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
2970 if (priv->flags & ALPS_FOUR_BUTTONS) {
2971 dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
2972 dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
2973 dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
2974 dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
2975 } else if (priv->flags & ALPS_BUTTONPAD) {
2976 set_bit(INPUT_PROP_BUTTONPAD, dev1->propbit);
2977 clear_bit(BTN_RIGHT, dev1->keybit);
2979 dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
2982 if (priv->flags & ALPS_DUALPOINT) {
2983 struct input_dev *dev2;
2985 dev2 = input_allocate_device();
2987 psmouse_err(psmouse,
2988 "failed to allocate trackstick device\n");
2993 snprintf(priv->phys2, sizeof(priv->phys2), "%s/input1",
2994 psmouse->ps2dev.serio->phys);
2995 dev2->phys = priv->phys2;
2998 * format of input device name is: "protocol vendor name"
2999 * see function psmouse_switch_protocol() in psmouse-base.c
3001 dev2->name = "AlpsPS/2 ALPS DualPoint Stick";
3003 dev2->id.bustype = BUS_I8042;
3004 dev2->id.vendor = 0x0002;
3005 dev2->id.product = PSMOUSE_ALPS;
3006 dev2->id.version = priv->proto_version;
3007 dev2->dev.parent = &psmouse->ps2dev.serio->dev;
3009 input_set_capability(dev2, EV_REL, REL_X);
3010 input_set_capability(dev2, EV_REL, REL_Y);
3011 if (priv->flags & ALPS_DUALPOINT_WITH_PRESSURE) {
3012 input_set_capability(dev2, EV_ABS, ABS_PRESSURE);
3013 input_set_abs_params(dev2, ABS_PRESSURE, 0, 127, 0, 0);
3015 input_set_capability(dev2, EV_KEY, BTN_LEFT);
3016 input_set_capability(dev2, EV_KEY, BTN_RIGHT);
3017 input_set_capability(dev2, EV_KEY, BTN_MIDDLE);
3019 __set_bit(INPUT_PROP_POINTER, dev2->propbit);
3020 __set_bit(INPUT_PROP_POINTING_STICK, dev2->propbit);
3022 error = input_register_device(dev2);
3024 psmouse_err(psmouse,
3025 "failed to register trackstick device: %d\n",
3027 input_free_device(dev2);
3034 priv->psmouse = psmouse;
3036 INIT_DELAYED_WORK(&priv->dev3_register_work,
3037 alps_register_bare_ps2_mouse);
3039 psmouse->protocol_handler = alps_process_byte;
3040 psmouse->poll = alps_poll;
3041 psmouse->disconnect = alps_disconnect;
3042 psmouse->reconnect = alps_reconnect;
3043 psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
3045 /* We are having trouble resyncing ALPS touchpads so disable it for now */
3046 psmouse->resync_time = 0;
3048 /* Allow 2 invalid packets without resetting device */
3049 psmouse->resetafter = psmouse->pktsize * 2;
3054 psmouse_reset(psmouse);
3056 * Even though we did not allocate psmouse->private we do free
3059 kfree(psmouse->private);
3060 psmouse->private = NULL;
3064 int alps_detect(struct psmouse *psmouse, bool set_properties)
3066 struct alps_data *priv;
3069 error = alps_identify(psmouse, NULL);
3074 * Reset the device to make sure it is fully operational:
3075 * on some laptops, like certain Dell Latitudes, we may
3076 * fail to properly detect presence of trackstick if device
3077 * has not been reset.
3079 psmouse_reset(psmouse);
3081 priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
3085 error = alps_identify(psmouse, priv);
3091 if (set_properties) {
3092 psmouse->vendor = "ALPS";
3093 psmouse->name = priv->flags & ALPS_DUALPOINT ?
3094 "DualPoint TouchPad" : "GlidePoint";
3095 psmouse->model = priv->proto_version;
3098 * Destroy alps_data structure we allocated earlier since
3099 * this was just a "trial run". Otherwise we'll keep it
3100 * to be used by alps_init() which has to be called if
3101 * we succeed and set_properties is true.
3104 psmouse->private = NULL;