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[] = {
110 * XXX This entry is suspicious. First byte has zero lower nibble,
111 * which is what a normal mouse would report. Also, the value 0x0e
112 * isn't valid per PS/2 spec.
114 { { 0x20, 0x02, 0x0e }, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },
116 { { 0x22, 0x02, 0x0a }, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },
117 { { 0x22, 0x02, 0x14 }, { ALPS_PROTO_V2, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT } }, /* Dell Latitude D600 */
118 { { 0x32, 0x02, 0x14 }, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } }, /* Toshiba Salellite Pro M10 */
119 { { 0x33, 0x02, 0x0a }, { ALPS_PROTO_V1, 0x88, 0xf8, 0 } }, /* UMAX-530T */
120 { { 0x52, 0x01, 0x14 }, { ALPS_PROTO_V2, 0xff, 0xff,
121 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED } }, /* Toshiba Tecra A11-11L */
122 { { 0x53, 0x02, 0x0a }, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
123 { { 0x53, 0x02, 0x14 }, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
124 { { 0x60, 0x03, 0xc8 }, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } }, /* HP ze1115 */
125 { { 0x62, 0x02, 0x14 }, { ALPS_PROTO_V2, 0xcf, 0xcf,
126 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED } }, /* Dell Latitude E5500, E6400, E6500, Precision M4400 */
127 { { 0x63, 0x02, 0x0a }, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
128 { { 0x63, 0x02, 0x14 }, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
129 { { 0x63, 0x02, 0x28 }, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 } }, /* Fujitsu Siemens S6010 */
130 { { 0x63, 0x02, 0x3c }, { ALPS_PROTO_V2, 0x8f, 0x8f, ALPS_WHEEL } }, /* Toshiba Satellite S2400-103 */
131 { { 0x63, 0x02, 0x50 }, { ALPS_PROTO_V2, 0xef, 0xef, ALPS_FW_BK_1 } }, /* NEC Versa L320 */
132 { { 0x63, 0x02, 0x64 }, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
133 { { 0x63, 0x03, 0xc8 }, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } }, /* Dell Latitude D800 */
134 { { 0x73, 0x00, 0x0a }, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_DUALPOINT } }, /* ThinkPad R61 8918-5QG */
135 { { 0x73, 0x00, 0x14 }, { ALPS_PROTO_V6, 0xff, 0xff, ALPS_DUALPOINT } }, /* Dell XT2 */
136 { { 0x73, 0x02, 0x0a }, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
137 { { 0x73, 0x02, 0x14 }, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 } }, /* Ahtec Laptop */
138 { { 0x73, 0x02, 0x50 }, { ALPS_PROTO_V2, 0xcf, 0xcf, ALPS_FOUR_BUTTONS } }, /* Dell Vostro 1400 */
141 static const struct alps_protocol_info alps_v3_protocol_data = {
142 ALPS_PROTO_V3, 0x8f, 0x8f, ALPS_DUALPOINT
145 static const struct alps_protocol_info alps_v3_rushmore_data = {
146 ALPS_PROTO_V3_RUSHMORE, 0x8f, 0x8f, ALPS_DUALPOINT
149 static const struct alps_protocol_info alps_v4_protocol_data = {
150 ALPS_PROTO_V4, 0x8f, 0x8f, 0
153 static const struct alps_protocol_info alps_v5_protocol_data = {
154 ALPS_PROTO_V5, 0xc8, 0xd8, 0
157 static const struct alps_protocol_info alps_v7_protocol_data = {
158 ALPS_PROTO_V7, 0x48, 0x48, ALPS_DUALPOINT
161 static const struct alps_protocol_info alps_v8_protocol_data = {
162 ALPS_PROTO_V8, 0x18, 0x18, 0
165 static const struct alps_protocol_info alps_v9_protocol_data = {
166 ALPS_PROTO_V9, 0xc8, 0xc8, 0
170 * Some v2 models report the stick buttons in separate bits
172 static const struct dmi_system_id alps_dmi_has_separate_stick_buttons[] = {
173 #if defined(CONFIG_DMI) && defined(CONFIG_X86)
175 /* Extrapolated from other entries */
177 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
178 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D420"),
182 /* Reported-by: Hans de Bruin <jmdebruin@xmsnet.nl> */
184 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
185 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D430"),
189 /* Reported-by: Hans de Goede <hdegoede@redhat.com> */
191 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
192 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D620"),
196 /* Extrapolated from other entries */
198 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
199 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D630"),
206 static void alps_set_abs_params_st(struct alps_data *priv,
207 struct input_dev *dev1);
208 static void alps_set_abs_params_semi_mt(struct alps_data *priv,
209 struct input_dev *dev1);
210 static void alps_set_abs_params_v7(struct alps_data *priv,
211 struct input_dev *dev1);
212 static void alps_set_abs_params_ss4_v2(struct alps_data *priv,
213 struct input_dev *dev1);
215 /* Packet formats are described in Documentation/input/alps.txt */
217 static bool alps_is_valid_first_byte(struct alps_data *priv,
220 return (data & priv->mask0) == priv->byte0;
223 static void alps_report_buttons(struct input_dev *dev1, struct input_dev *dev2,
224 int left, int right, int middle)
226 struct input_dev *dev;
229 * If shared button has already been reported on the
230 * other device (dev2) then this event should be also
231 * sent through that device.
233 dev = (dev2 && test_bit(BTN_LEFT, dev2->key)) ? dev2 : dev1;
234 input_report_key(dev, BTN_LEFT, left);
236 dev = (dev2 && test_bit(BTN_RIGHT, dev2->key)) ? dev2 : dev1;
237 input_report_key(dev, BTN_RIGHT, right);
239 dev = (dev2 && test_bit(BTN_MIDDLE, dev2->key)) ? dev2 : dev1;
240 input_report_key(dev, BTN_MIDDLE, middle);
243 * Sync the _other_ device now, we'll do the first
244 * device later once we report the rest of the events.
250 static void alps_process_packet_v1_v2(struct psmouse *psmouse)
252 struct alps_data *priv = psmouse->private;
253 unsigned char *packet = psmouse->packet;
254 struct input_dev *dev = psmouse->dev;
255 struct input_dev *dev2 = priv->dev2;
256 int x, y, z, ges, fin, left, right, middle;
257 int back = 0, forward = 0;
259 if (priv->proto_version == ALPS_PROTO_V1) {
260 left = packet[2] & 0x10;
261 right = packet[2] & 0x08;
263 x = packet[1] | ((packet[0] & 0x07) << 7);
264 y = packet[4] | ((packet[3] & 0x07) << 7);
267 left = packet[3] & 1;
268 right = packet[3] & 2;
269 middle = packet[3] & 4;
270 x = packet[1] | ((packet[2] & 0x78) << (7 - 3));
271 y = packet[4] | ((packet[3] & 0x70) << (7 - 4));
275 if (priv->flags & ALPS_FW_BK_1) {
276 back = packet[0] & 0x10;
277 forward = packet[2] & 4;
280 if (priv->flags & ALPS_FW_BK_2) {
281 back = packet[3] & 4;
282 forward = packet[2] & 4;
283 if ((middle = forward && back))
290 if ((priv->flags & ALPS_DUALPOINT) && z == 127) {
291 input_report_rel(dev2, REL_X, (x > 383 ? (x - 768) : x));
292 input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y));
294 alps_report_buttons(dev2, dev, left, right, middle);
300 /* Some models have separate stick button bits */
301 if (priv->flags & ALPS_STICK_BITS) {
302 left |= packet[0] & 1;
303 right |= packet[0] & 2;
304 middle |= packet[0] & 4;
307 alps_report_buttons(dev, dev2, left, right, middle);
309 /* Convert hardware tap to a reasonable Z value */
314 * A "tap and drag" operation is reported by the hardware as a transition
315 * from (!fin && ges) to (fin && ges). This should be translated to the
316 * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
318 if (ges && fin && !priv->prev_fin) {
319 input_report_abs(dev, ABS_X, x);
320 input_report_abs(dev, ABS_Y, y);
321 input_report_abs(dev, ABS_PRESSURE, 0);
322 input_report_key(dev, BTN_TOOL_FINGER, 0);
325 priv->prev_fin = fin;
328 input_report_key(dev, BTN_TOUCH, 1);
330 input_report_key(dev, BTN_TOUCH, 0);
333 input_report_abs(dev, ABS_X, x);
334 input_report_abs(dev, ABS_Y, y);
337 input_report_abs(dev, ABS_PRESSURE, z);
338 input_report_key(dev, BTN_TOOL_FINGER, z > 0);
340 if (priv->flags & ALPS_WHEEL)
341 input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
343 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
344 input_report_key(dev, BTN_FORWARD, forward);
345 input_report_key(dev, BTN_BACK, back);
348 if (priv->flags & ALPS_FOUR_BUTTONS) {
349 input_report_key(dev, BTN_0, packet[2] & 4);
350 input_report_key(dev, BTN_1, packet[0] & 0x10);
351 input_report_key(dev, BTN_2, packet[3] & 4);
352 input_report_key(dev, BTN_3, packet[0] & 0x20);
358 static void alps_get_bitmap_points(unsigned int map,
359 struct alps_bitmap_point *low,
360 struct alps_bitmap_point *high,
363 struct alps_bitmap_point *point;
364 int i, bit, prev_bit = 0;
367 for (i = 0; map != 0; i++, map >>= 1) {
371 point->start_bit = i;
385 * Process bitmap data from semi-mt protocols. Returns the number of
386 * fingers detected. A return value of 0 means at least one of the
389 * The bitmaps don't have enough data to track fingers, so this function
390 * only generates points representing a bounding box of all contacts.
391 * These points are returned in fields->mt when the return value
394 static int alps_process_bitmap(struct alps_data *priv,
395 struct alps_fields *fields)
397 int i, fingers_x = 0, fingers_y = 0, fingers, closest;
398 struct alps_bitmap_point x_low = {0,}, x_high = {0,};
399 struct alps_bitmap_point y_low = {0,}, y_high = {0,};
400 struct input_mt_pos corner[4];
402 if (!fields->x_map || !fields->y_map)
405 alps_get_bitmap_points(fields->x_map, &x_low, &x_high, &fingers_x);
406 alps_get_bitmap_points(fields->y_map, &y_low, &y_high, &fingers_y);
409 * Fingers can overlap, so we use the maximum count of fingers
410 * on either axis as the finger count.
412 fingers = max(fingers_x, fingers_y);
415 * If an axis reports only a single contact, we have overlapping or
416 * adjacent fingers. Divide the single contact between the two points.
418 if (fingers_x == 1) {
419 i = (x_low.num_bits - 1) / 2;
420 x_low.num_bits = x_low.num_bits - i;
421 x_high.start_bit = x_low.start_bit + i;
422 x_high.num_bits = max(i, 1);
424 if (fingers_y == 1) {
425 i = (y_low.num_bits - 1) / 2;
426 y_low.num_bits = y_low.num_bits - i;
427 y_high.start_bit = y_low.start_bit + i;
428 y_high.num_bits = max(i, 1);
431 /* top-left corner */
433 (priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
434 (2 * (priv->x_bits - 1));
436 (priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
437 (2 * (priv->y_bits - 1));
439 /* top-right corner */
441 (priv->x_max * (2 * x_high.start_bit + x_high.num_bits - 1)) /
442 (2 * (priv->x_bits - 1));
444 (priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
445 (2 * (priv->y_bits - 1));
447 /* bottom-right corner */
449 (priv->x_max * (2 * x_high.start_bit + x_high.num_bits - 1)) /
450 (2 * (priv->x_bits - 1));
452 (priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) /
453 (2 * (priv->y_bits - 1));
455 /* bottom-left corner */
457 (priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
458 (2 * (priv->x_bits - 1));
460 (priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) /
461 (2 * (priv->y_bits - 1));
463 /* x-bitmap order is reversed on v5 touchpads */
464 if (priv->proto_version == ALPS_PROTO_V5) {
465 for (i = 0; i < 4; i++)
466 corner[i].x = priv->x_max - corner[i].x;
469 /* y-bitmap order is reversed on v3 and v4 touchpads */
470 if (priv->proto_version == ALPS_PROTO_V3 ||
471 priv->proto_version == ALPS_PROTO_V4) {
472 for (i = 0; i < 4; i++)
473 corner[i].y = priv->y_max - corner[i].y;
477 * We only select a corner for the second touch once per 2 finger
478 * touch sequence to avoid the chosen corner (and thus the coordinates)
479 * jumping around when the first touch is in the middle.
481 if (priv->second_touch == -1) {
482 /* Find corner closest to our st coordinates */
483 closest = 0x7fffffff;
484 for (i = 0; i < 4; i++) {
485 int dx = fields->st.x - corner[i].x;
486 int dy = fields->st.y - corner[i].y;
487 int distance = dx * dx + dy * dy;
489 if (distance < closest) {
490 priv->second_touch = i;
494 /* And select the opposite corner to use for the 2nd touch */
495 priv->second_touch = (priv->second_touch + 2) % 4;
498 fields->mt[0] = fields->st;
499 fields->mt[1] = corner[priv->second_touch];
504 static void alps_set_slot(struct input_dev *dev, int slot, int x, int y)
506 input_mt_slot(dev, slot);
507 input_mt_report_slot_state(dev, MT_TOOL_FINGER, true);
508 input_report_abs(dev, ABS_MT_POSITION_X, x);
509 input_report_abs(dev, ABS_MT_POSITION_Y, y);
512 static void alps_report_mt_data(struct psmouse *psmouse, int n)
514 struct alps_data *priv = psmouse->private;
515 struct input_dev *dev = psmouse->dev;
516 struct alps_fields *f = &priv->f;
517 int i, slot[MAX_TOUCHES];
519 input_mt_assign_slots(dev, slot, f->mt, n, 0);
520 for (i = 0; i < n; i++)
521 alps_set_slot(dev, slot[i], f->mt[i].x, f->mt[i].y);
523 input_mt_sync_frame(dev);
526 static void alps_report_semi_mt_data(struct psmouse *psmouse, int fingers)
528 struct alps_data *priv = psmouse->private;
529 struct input_dev *dev = psmouse->dev;
530 struct alps_fields *f = &priv->f;
532 /* Use st data when we don't have mt data */
534 f->mt[0].x = f->st.x;
535 f->mt[0].y = f->st.y;
536 fingers = f->pressure > 0 ? 1 : 0;
537 priv->second_touch = -1;
541 alps_set_slot(dev, 0, f->mt[0].x, f->mt[0].y);
543 alps_set_slot(dev, 1, f->mt[1].x, f->mt[1].y);
544 input_mt_sync_frame(dev);
546 input_mt_report_finger_count(dev, fingers);
548 input_report_key(dev, BTN_LEFT, f->left);
549 input_report_key(dev, BTN_RIGHT, f->right);
550 input_report_key(dev, BTN_MIDDLE, f->middle);
552 input_report_abs(dev, ABS_PRESSURE, f->pressure);
557 static void alps_process_trackstick_packet_v3(struct psmouse *psmouse)
559 struct alps_data *priv = psmouse->private;
560 unsigned char *packet = psmouse->packet;
561 struct input_dev *dev = priv->dev2;
562 int x, y, z, left, right, middle;
564 /* It should be a DualPoint when received trackstick packet */
565 if (!(priv->flags & ALPS_DUALPOINT)) {
566 psmouse_warn(psmouse,
567 "Rejected trackstick packet from non DualPoint device");
571 /* Sanity check packet */
572 if (!(packet[0] & 0x40)) {
573 psmouse_dbg(psmouse, "Bad trackstick packet, discarding\n");
578 * There's a special packet that seems to indicate the end
579 * of a stream of trackstick data. Filter these out.
581 if (packet[1] == 0x7f && packet[2] == 0x7f && packet[4] == 0x7f)
584 x = (s8)(((packet[0] & 0x20) << 2) | (packet[1] & 0x7f));
585 y = (s8)(((packet[0] & 0x10) << 3) | (packet[2] & 0x7f));
586 z = (packet[4] & 0x7c) >> 2;
589 * The x and y values tend to be quite large, and when used
590 * alone the trackstick is difficult to use. Scale them down
596 input_report_rel(dev, REL_X, x);
597 input_report_rel(dev, REL_Y, -y);
600 * Most ALPS models report the trackstick buttons in the touchpad
601 * packets, but a few report them here. No reliable way has been
602 * found to differentiate between the models upfront, so we enable
603 * the quirk in response to seeing a button press in the trackstick
606 left = packet[3] & 0x01;
607 right = packet[3] & 0x02;
608 middle = packet[3] & 0x04;
610 if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) &&
611 (left || right || middle))
612 priv->quirks |= ALPS_QUIRK_TRACKSTICK_BUTTONS;
614 if (priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) {
615 input_report_key(dev, BTN_LEFT, left);
616 input_report_key(dev, BTN_RIGHT, right);
617 input_report_key(dev, BTN_MIDDLE, middle);
624 static void alps_decode_buttons_v3(struct alps_fields *f, unsigned char *p)
626 f->left = !!(p[3] & 0x01);
627 f->right = !!(p[3] & 0x02);
628 f->middle = !!(p[3] & 0x04);
630 f->ts_left = !!(p[3] & 0x10);
631 f->ts_right = !!(p[3] & 0x20);
632 f->ts_middle = !!(p[3] & 0x40);
635 static int alps_decode_pinnacle(struct alps_fields *f, unsigned char *p,
636 struct psmouse *psmouse)
638 f->first_mp = !!(p[4] & 0x40);
639 f->is_mp = !!(p[0] & 0x40);
642 f->fingers = (p[5] & 0x3) + 1;
643 f->x_map = ((p[4] & 0x7e) << 8) |
644 ((p[1] & 0x7f) << 2) |
645 ((p[0] & 0x30) >> 4);
646 f->y_map = ((p[3] & 0x70) << 4) |
647 ((p[2] & 0x7f) << 1) |
650 f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
651 ((p[0] & 0x30) >> 4);
652 f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
653 f->pressure = p[5] & 0x7f;
655 alps_decode_buttons_v3(f, p);
661 static int alps_decode_rushmore(struct alps_fields *f, unsigned char *p,
662 struct psmouse *psmouse)
664 f->first_mp = !!(p[4] & 0x40);
665 f->is_mp = !!(p[5] & 0x40);
668 f->fingers = max((p[5] & 0x3), ((p[5] >> 2) & 0x3)) + 1;
669 f->x_map = ((p[5] & 0x10) << 11) |
670 ((p[4] & 0x7e) << 8) |
671 ((p[1] & 0x7f) << 2) |
672 ((p[0] & 0x30) >> 4);
673 f->y_map = ((p[5] & 0x20) << 6) |
674 ((p[3] & 0x70) << 4) |
675 ((p[2] & 0x7f) << 1) |
678 f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
679 ((p[0] & 0x30) >> 4);
680 f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
681 f->pressure = p[5] & 0x7f;
683 alps_decode_buttons_v3(f, p);
689 static int alps_decode_dolphin(struct alps_fields *f, unsigned char *p,
690 struct psmouse *psmouse)
693 struct alps_data *priv = psmouse->private;
695 f->first_mp = !!(p[0] & 0x02);
696 f->is_mp = !!(p[0] & 0x20);
699 f->st.x = ((p[1] & 0x7f) | ((p[4] & 0x0f) << 7));
700 f->st.y = ((p[2] & 0x7f) | ((p[4] & 0xf0) << 3));
701 f->pressure = (p[0] & 4) ? 0 : p[5] & 0x7f;
702 alps_decode_buttons_v3(f, p);
704 f->fingers = ((p[0] & 0x6) >> 1 |
707 palm_data = (p[1] & 0x7f) |
708 ((p[2] & 0x7f) << 7) |
709 ((p[4] & 0x7f) << 14) |
710 ((p[5] & 0x7f) << 21) |
711 ((p[3] & 0x07) << 28) |
712 (((u64)p[3] & 0x70) << 27) |
713 (((u64)p[0] & 0x01) << 34);
715 /* Y-profile is stored in P(0) to p(n-1), n = y_bits; */
716 f->y_map = palm_data & (BIT(priv->y_bits) - 1);
718 /* X-profile is stored in p(n) to p(n+m-1), m = x_bits; */
719 f->x_map = (palm_data >> priv->y_bits) &
720 (BIT(priv->x_bits) - 1);
726 static void alps_process_touchpad_packet_v3_v5(struct psmouse *psmouse)
728 struct alps_data *priv = psmouse->private;
729 unsigned char *packet = psmouse->packet;
730 struct input_dev *dev2 = priv->dev2;
731 struct alps_fields *f = &priv->f;
734 memset(f, 0, sizeof(*f));
736 priv->decode_fields(f, packet, psmouse);
739 * There's no single feature of touchpad position and bitmap packets
740 * that can be used to distinguish between them. We rely on the fact
741 * that a bitmap packet should always follow a position packet with
742 * bit 6 of packet[4] set.
744 if (priv->multi_packet) {
746 * Sometimes a position packet will indicate a multi-packet
747 * sequence, but then what follows is another position
748 * packet. Check for this, and when it happens process the
749 * position packet as usual.
752 fingers = f->fingers;
754 * Bitmap processing uses position packet's coordinate
755 * data, so we need to do decode it first.
757 priv->decode_fields(f, priv->multi_data, psmouse);
758 if (alps_process_bitmap(priv, f) == 0)
759 fingers = 0; /* Use st data */
761 priv->multi_packet = 0;
766 * Bit 6 of byte 0 is not usually set in position packets. The only
767 * times it seems to be set is in situations where the data is
768 * suspect anyway, e.g. a palm resting flat on the touchpad. Given
769 * this combined with the fact that this bit is useful for filtering
770 * out misidentified bitmap packets, we reject anything with this
776 if (!priv->multi_packet && f->first_mp) {
777 priv->multi_packet = 1;
778 memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
782 priv->multi_packet = 0;
785 * Sometimes the hardware sends a single packet with z = 0
786 * in the middle of a stream. Real releases generate packets
787 * with x, y, and z all zero, so these seem to be flukes.
790 if (f->st.x && f->st.y && !f->pressure)
793 alps_report_semi_mt_data(psmouse, fingers);
795 if ((priv->flags & ALPS_DUALPOINT) &&
796 !(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) {
797 input_report_key(dev2, BTN_LEFT, f->ts_left);
798 input_report_key(dev2, BTN_RIGHT, f->ts_right);
799 input_report_key(dev2, BTN_MIDDLE, f->ts_middle);
804 static void alps_process_packet_v3(struct psmouse *psmouse)
806 unsigned char *packet = psmouse->packet;
809 * v3 protocol packets come in three types, two representing
810 * touchpad data and one representing trackstick data.
811 * Trackstick packets seem to be distinguished by always
812 * having 0x3f in the last byte. This value has never been
813 * observed in the last byte of either of the other types
816 if (packet[5] == 0x3f) {
817 alps_process_trackstick_packet_v3(psmouse);
821 alps_process_touchpad_packet_v3_v5(psmouse);
824 static void alps_process_packet_v6(struct psmouse *psmouse)
826 struct alps_data *priv = psmouse->private;
827 unsigned char *packet = psmouse->packet;
828 struct input_dev *dev = psmouse->dev;
829 struct input_dev *dev2 = priv->dev2;
830 int x, y, z, left, right, middle;
833 * We can use Byte5 to distinguish if the packet is from Touchpad
838 if (packet[5] == 0x7F) {
839 /* It should be a DualPoint when received Trackpoint packet */
840 if (!(priv->flags & ALPS_DUALPOINT)) {
841 psmouse_warn(psmouse,
842 "Rejected trackstick packet from non DualPoint device");
846 /* Trackpoint packet */
847 x = packet[1] | ((packet[3] & 0x20) << 2);
848 y = packet[2] | ((packet[3] & 0x40) << 1);
850 left = packet[3] & 0x01;
851 right = packet[3] & 0x02;
852 middle = packet[3] & 0x04;
854 /* To prevent the cursor jump when finger lifted */
855 if (x == 0x7F && y == 0x7F && z == 0x7F)
858 /* Divide 4 since trackpoint's speed is too fast */
859 input_report_rel(dev2, REL_X, (char)x / 4);
860 input_report_rel(dev2, REL_Y, -((char)y / 4));
862 input_report_key(dev2, BTN_LEFT, left);
863 input_report_key(dev2, BTN_RIGHT, right);
864 input_report_key(dev2, BTN_MIDDLE, middle);
870 /* Touchpad packet */
871 x = packet[1] | ((packet[3] & 0x78) << 4);
872 y = packet[2] | ((packet[4] & 0x78) << 4);
874 left = packet[3] & 0x01;
875 right = packet[3] & 0x02;
878 input_report_key(dev, BTN_TOUCH, 1);
880 input_report_key(dev, BTN_TOUCH, 0);
883 input_report_abs(dev, ABS_X, x);
884 input_report_abs(dev, ABS_Y, y);
887 input_report_abs(dev, ABS_PRESSURE, z);
888 input_report_key(dev, BTN_TOOL_FINGER, z > 0);
890 /* v6 touchpad does not have middle button */
891 input_report_key(dev, BTN_LEFT, left);
892 input_report_key(dev, BTN_RIGHT, right);
897 static void alps_process_packet_v4(struct psmouse *psmouse)
899 struct alps_data *priv = psmouse->private;
900 unsigned char *packet = psmouse->packet;
901 struct alps_fields *f = &priv->f;
905 * v4 has a 6-byte encoding for bitmap data, but this data is
906 * broken up between 3 normal packets. Use priv->multi_packet to
907 * track our position in the bitmap packet.
909 if (packet[6] & 0x40) {
910 /* sync, reset position */
911 priv->multi_packet = 0;
914 if (WARN_ON_ONCE(priv->multi_packet > 2))
917 offset = 2 * priv->multi_packet;
918 priv->multi_data[offset] = packet[6];
919 priv->multi_data[offset + 1] = packet[7];
921 f->left = !!(packet[4] & 0x01);
922 f->right = !!(packet[4] & 0x02);
924 f->st.x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) |
925 ((packet[0] & 0x30) >> 4);
926 f->st.y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f);
927 f->pressure = packet[5] & 0x7f;
929 if (++priv->multi_packet > 2) {
930 priv->multi_packet = 0;
932 f->x_map = ((priv->multi_data[2] & 0x1f) << 10) |
933 ((priv->multi_data[3] & 0x60) << 3) |
934 ((priv->multi_data[0] & 0x3f) << 2) |
935 ((priv->multi_data[1] & 0x60) >> 5);
936 f->y_map = ((priv->multi_data[5] & 0x01) << 10) |
937 ((priv->multi_data[3] & 0x1f) << 5) |
938 (priv->multi_data[1] & 0x1f);
940 f->fingers = alps_process_bitmap(priv, f);
943 alps_report_semi_mt_data(psmouse, f->fingers);
946 static bool alps_is_valid_package_v7(struct psmouse *psmouse)
948 switch (psmouse->pktcnt) {
950 return (psmouse->packet[2] & 0x40) == 0x40;
952 return (psmouse->packet[3] & 0x48) == 0x48;
954 return (psmouse->packet[5] & 0x40) == 0x00;
959 static unsigned char alps_get_packet_id_v7(char *byte)
961 unsigned char packet_id;
964 packet_id = V7_PACKET_ID_TWO;
965 else if (byte[4] & 0x01)
966 packet_id = V7_PACKET_ID_MULTI;
967 else if ((byte[0] & 0x10) && !(byte[4] & 0x43))
968 packet_id = V7_PACKET_ID_NEW;
969 else if (byte[1] == 0x00 && byte[4] == 0x00)
970 packet_id = V7_PACKET_ID_IDLE;
972 packet_id = V7_PACKET_ID_UNKNOWN;
977 static void alps_get_finger_coordinate_v7(struct input_mt_pos *mt,
979 unsigned char pkt_id)
981 mt[0].x = ((pkt[2] & 0x80) << 4);
982 mt[0].x |= ((pkt[2] & 0x3F) << 5);
983 mt[0].x |= ((pkt[3] & 0x30) >> 1);
984 mt[0].x |= (pkt[3] & 0x07);
985 mt[0].y = (pkt[1] << 3) | (pkt[0] & 0x07);
987 mt[1].x = ((pkt[3] & 0x80) << 4);
988 mt[1].x |= ((pkt[4] & 0x80) << 3);
989 mt[1].x |= ((pkt[4] & 0x3F) << 4);
990 mt[1].y = ((pkt[5] & 0x80) << 3);
991 mt[1].y |= ((pkt[5] & 0x3F) << 4);
994 case V7_PACKET_ID_TWO:
997 /* Detect false-postive touches where x & y report max value */
998 if (mt[1].y == 0x7ff && mt[1].x == 0xff0) {
1000 /* y gets set to 0 at the end of this function */
1004 case V7_PACKET_ID_MULTI:
1007 mt[1].y |= ((pkt[4] & 0x02) << 4);
1011 case V7_PACKET_ID_NEW:
1013 mt[1].x |= (pkt[0] & 0x20);
1018 mt[0].y = 0x7FF - mt[0].y;
1019 mt[1].y = 0x7FF - mt[1].y;
1022 static int alps_get_mt_count(struct input_mt_pos *mt)
1026 for (i = 0; i < MAX_TOUCHES; i++) {
1027 if (mt[i].x != 0 || mt[i].y != 0)
1034 static int alps_decode_packet_v7(struct alps_fields *f,
1036 struct psmouse *psmouse)
1038 struct alps_data *priv = psmouse->private;
1039 unsigned char pkt_id;
1041 pkt_id = alps_get_packet_id_v7(p);
1042 if (pkt_id == V7_PACKET_ID_IDLE)
1044 if (pkt_id == V7_PACKET_ID_UNKNOWN)
1047 * NEW packets are send to indicate a discontinuity in the finger
1048 * coordinate reporting. Specifically a finger may have moved from
1049 * slot 0 to 1 or vice versa. INPUT_MT_TRACK takes care of this for
1052 * NEW packets have 3 problems:
1053 * 1) They do not contain middle / right button info (on non clickpads)
1054 * this can be worked around by preserving the old button state
1055 * 2) They do not contain an accurate fingercount, and they are
1056 * typically send when the number of fingers changes. We cannot use
1057 * the old finger count as that may mismatch with the amount of
1058 * touch coordinates we've available in the NEW packet
1059 * 3) Their x data for the second touch is inaccurate leading to
1060 * a possible jump of the x coordinate by 16 units when the first
1061 * non NEW packet comes in
1062 * Since problems 2 & 3 cannot be worked around, just ignore them.
1064 if (pkt_id == V7_PACKET_ID_NEW)
1067 alps_get_finger_coordinate_v7(f->mt, p, pkt_id);
1069 if (pkt_id == V7_PACKET_ID_TWO)
1070 f->fingers = alps_get_mt_count(f->mt);
1071 else /* pkt_id == V7_PACKET_ID_MULTI */
1072 f->fingers = 3 + (p[5] & 0x03);
1074 f->left = (p[0] & 0x80) >> 7;
1075 if (priv->flags & ALPS_BUTTONPAD) {
1081 f->right = (p[0] & 0x20) >> 5;
1082 f->middle = (p[0] & 0x10) >> 4;
1085 /* Sometimes a single touch is reported in mt[1] rather then mt[0] */
1086 if (f->fingers == 1 && f->mt[0].x == 0 && f->mt[0].y == 0) {
1087 f->mt[0].x = f->mt[1].x;
1088 f->mt[0].y = f->mt[1].y;
1096 static void alps_process_trackstick_packet_v7(struct psmouse *psmouse)
1098 struct alps_data *priv = psmouse->private;
1099 unsigned char *packet = psmouse->packet;
1100 struct input_dev *dev2 = priv->dev2;
1101 int x, y, z, left, right, middle;
1103 /* It should be a DualPoint when received trackstick packet */
1104 if (!(priv->flags & ALPS_DUALPOINT)) {
1105 psmouse_warn(psmouse,
1106 "Rejected trackstick packet from non DualPoint device");
1110 x = ((packet[2] & 0xbf)) | ((packet[3] & 0x10) << 2);
1111 y = (packet[3] & 0x07) | (packet[4] & 0xb8) |
1112 ((packet[3] & 0x20) << 1);
1113 z = (packet[5] & 0x3f) | ((packet[3] & 0x80) >> 1);
1115 left = (packet[1] & 0x01);
1116 right = (packet[1] & 0x02) >> 1;
1117 middle = (packet[1] & 0x04) >> 2;
1119 input_report_rel(dev2, REL_X, (char)x);
1120 input_report_rel(dev2, REL_Y, -((char)y));
1122 input_report_key(dev2, BTN_LEFT, left);
1123 input_report_key(dev2, BTN_RIGHT, right);
1124 input_report_key(dev2, BTN_MIDDLE, middle);
1129 static void alps_process_touchpad_packet_v7(struct psmouse *psmouse)
1131 struct alps_data *priv = psmouse->private;
1132 struct input_dev *dev = psmouse->dev;
1133 struct alps_fields *f = &priv->f;
1135 memset(f, 0, sizeof(*f));
1137 if (priv->decode_fields(f, psmouse->packet, psmouse))
1140 alps_report_mt_data(psmouse, alps_get_mt_count(f->mt));
1142 input_mt_report_finger_count(dev, f->fingers);
1144 input_report_key(dev, BTN_LEFT, f->left);
1145 input_report_key(dev, BTN_RIGHT, f->right);
1146 input_report_key(dev, BTN_MIDDLE, f->middle);
1151 static void alps_process_packet_v7(struct psmouse *psmouse)
1153 unsigned char *packet = psmouse->packet;
1155 if (packet[0] == 0x48 && (packet[4] & 0x47) == 0x06)
1156 alps_process_trackstick_packet_v7(psmouse);
1158 alps_process_touchpad_packet_v7(psmouse);
1161 static enum SS4_PACKET_ID alps_get_pkt_id_ss4_v2(unsigned char *byte)
1163 enum SS4_PACKET_ID pkt_id = SS4_PACKET_ID_IDLE;
1165 switch (byte[3] & 0x30) {
1167 if (SS4_IS_IDLE_V2(byte)) {
1168 pkt_id = SS4_PACKET_ID_IDLE;
1170 pkt_id = SS4_PACKET_ID_ONE;
1174 /* two-finger finger positions */
1175 pkt_id = SS4_PACKET_ID_TWO;
1179 pkt_id = SS4_PACKET_ID_STICK;
1182 /* third and fourth finger positions */
1183 pkt_id = SS4_PACKET_ID_MULTI;
1190 static int alps_decode_ss4_v2(struct alps_fields *f,
1191 unsigned char *p, struct psmouse *psmouse)
1193 struct alps_data *priv = psmouse->private;
1194 enum SS4_PACKET_ID pkt_id;
1195 unsigned int no_data_x, no_data_y;
1197 pkt_id = alps_get_pkt_id_ss4_v2(p);
1199 /* Current packet is 1Finger coordinate packet */
1201 case SS4_PACKET_ID_ONE:
1202 f->mt[0].x = SS4_1F_X_V2(p);
1203 f->mt[0].y = SS4_1F_Y_V2(p);
1204 f->pressure = ((SS4_1F_Z_V2(p)) * 2) & 0x7f;
1206 * When a button is held the device will give us events
1207 * with x, y, and pressure of 0. This causes annoying jumps
1208 * if a touch is released while the button is held.
1209 * Handle this by claiming zero contacts.
1211 f->fingers = f->pressure > 0 ? 1 : 0;
1216 case SS4_PACKET_ID_TWO:
1217 if (priv->flags & ALPS_BUTTONPAD) {
1218 f->mt[0].x = SS4_BTL_MF_X_V2(p, 0);
1219 f->mt[0].y = SS4_BTL_MF_Y_V2(p, 0);
1220 f->mt[1].x = SS4_BTL_MF_X_V2(p, 1);
1221 f->mt[1].y = SS4_BTL_MF_Y_V2(p, 1);
1223 f->mt[0].x = SS4_STD_MF_X_V2(p, 0);
1224 f->mt[0].y = SS4_STD_MF_Y_V2(p, 0);
1225 f->mt[1].x = SS4_STD_MF_X_V2(p, 1);
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)) {
1240 case SS4_PACKET_ID_MULTI:
1241 if (priv->flags & ALPS_BUTTONPAD) {
1242 f->mt[2].x = SS4_BTL_MF_X_V2(p, 0);
1243 f->mt[2].y = SS4_BTL_MF_Y_V2(p, 0);
1244 f->mt[3].x = SS4_BTL_MF_X_V2(p, 1);
1245 f->mt[3].y = SS4_BTL_MF_Y_V2(p, 1);
1246 no_data_x = SS4_MFPACKET_NO_AX_BL;
1247 no_data_y = SS4_MFPACKET_NO_AY_BL;
1249 f->mt[2].x = SS4_STD_MF_X_V2(p, 0);
1250 f->mt[2].y = SS4_STD_MF_Y_V2(p, 0);
1251 f->mt[3].x = SS4_STD_MF_X_V2(p, 1);
1252 f->mt[3].y = SS4_STD_MF_Y_V2(p, 1);
1253 no_data_x = SS4_MFPACKET_NO_AX;
1254 no_data_y = SS4_MFPACKET_NO_AY;
1260 if (SS4_IS_5F_DETECTED(p)) {
1262 } else if (f->mt[3].x == no_data_x &&
1263 f->mt[3].y == no_data_y) {
1272 case SS4_PACKET_ID_STICK:
1274 * x, y, and pressure are decoded in
1275 * alps_process_packet_ss4_v2()
1281 case SS4_PACKET_ID_IDLE:
1283 memset(f, 0, sizeof(struct alps_fields));
1287 /* handle buttons */
1288 if (pkt_id == SS4_PACKET_ID_STICK) {
1289 f->ts_left = !!(SS4_BTN_V2(p) & 0x01);
1290 f->ts_right = !!(SS4_BTN_V2(p) & 0x02);
1291 f->ts_middle = !!(SS4_BTN_V2(p) & 0x04);
1293 f->left = !!(SS4_BTN_V2(p) & 0x01);
1294 if (!(priv->flags & ALPS_BUTTONPAD)) {
1295 f->right = !!(SS4_BTN_V2(p) & 0x02);
1296 f->middle = !!(SS4_BTN_V2(p) & 0x04);
1303 static void alps_process_packet_ss4_v2(struct psmouse *psmouse)
1305 struct alps_data *priv = psmouse->private;
1306 unsigned char *packet = psmouse->packet;
1307 struct input_dev *dev = psmouse->dev;
1308 struct input_dev *dev2 = priv->dev2;
1309 struct alps_fields *f = &priv->f;
1311 memset(f, 0, sizeof(struct alps_fields));
1312 priv->decode_fields(f, packet, psmouse);
1313 if (priv->multi_packet) {
1315 * Sometimes the first packet will indicate a multi-packet
1316 * sequence, but sometimes the next multi-packet would not
1317 * come. Check for this, and when it happens process the
1318 * position packet as usual.
1321 /* Now process the 1st packet */
1322 priv->decode_fields(f, priv->multi_data, psmouse);
1324 priv->multi_packet = 0;
1329 * "f.is_mp" would always be '0' after merging the 1st and 2nd packet.
1330 * When it is set, it means 2nd packet comes without 1st packet come.
1335 /* Save the first packet */
1336 if (!priv->multi_packet && f->first_mp) {
1337 priv->multi_packet = 1;
1338 memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
1342 priv->multi_packet = 0;
1344 /* Report trackstick */
1345 if (alps_get_pkt_id_ss4_v2(packet) == SS4_PACKET_ID_STICK) {
1346 if (!(priv->flags & ALPS_DUALPOINT)) {
1347 psmouse_warn(psmouse,
1348 "Rejected trackstick packet from non DualPoint device");
1352 input_report_rel(dev2, REL_X, SS4_TS_X_V2(packet));
1353 input_report_rel(dev2, REL_Y, SS4_TS_Y_V2(packet));
1354 input_report_abs(dev2, ABS_PRESSURE, SS4_TS_Z_V2(packet));
1356 input_report_key(dev2, BTN_LEFT, f->ts_left);
1357 input_report_key(dev2, BTN_RIGHT, f->ts_right);
1358 input_report_key(dev2, BTN_MIDDLE, f->ts_middle);
1364 /* Report touchpad */
1365 alps_report_mt_data(psmouse, (f->fingers <= 4) ? f->fingers : 4);
1367 input_mt_report_finger_count(dev, f->fingers);
1369 input_report_key(dev, BTN_LEFT, f->left);
1370 input_report_key(dev, BTN_RIGHT, f->right);
1371 input_report_key(dev, BTN_MIDDLE, f->middle);
1373 input_report_abs(dev, ABS_PRESSURE, f->pressure);
1377 static bool alps_is_valid_package_ss4_v2(struct psmouse *psmouse)
1379 if (psmouse->pktcnt == 4 && ((psmouse->packet[3] & 0x08) != 0x08))
1381 if (psmouse->pktcnt == 6 && ((psmouse->packet[5] & 0x10) != 0x0))
1386 static DEFINE_MUTEX(alps_mutex);
1388 static void alps_register_bare_ps2_mouse(struct work_struct *work)
1390 struct alps_data *priv =
1391 container_of(work, struct alps_data, dev3_register_work.work);
1392 struct psmouse *psmouse = priv->psmouse;
1393 struct input_dev *dev3;
1396 mutex_lock(&alps_mutex);
1401 dev3 = input_allocate_device();
1403 psmouse_err(psmouse, "failed to allocate secondary device\n");
1408 snprintf(priv->phys3, sizeof(priv->phys3), "%s/%s",
1409 psmouse->ps2dev.serio->phys,
1410 (priv->dev2 ? "input2" : "input1"));
1411 dev3->phys = priv->phys3;
1414 * format of input device name is: "protocol vendor name"
1415 * see function psmouse_switch_protocol() in psmouse-base.c
1417 dev3->name = "PS/2 ALPS Mouse";
1419 dev3->id.bustype = BUS_I8042;
1420 dev3->id.vendor = 0x0002;
1421 dev3->id.product = PSMOUSE_PS2;
1422 dev3->id.version = 0x0000;
1423 dev3->dev.parent = &psmouse->ps2dev.serio->dev;
1425 input_set_capability(dev3, EV_REL, REL_X);
1426 input_set_capability(dev3, EV_REL, REL_Y);
1427 input_set_capability(dev3, EV_KEY, BTN_LEFT);
1428 input_set_capability(dev3, EV_KEY, BTN_RIGHT);
1429 input_set_capability(dev3, EV_KEY, BTN_MIDDLE);
1431 __set_bit(INPUT_PROP_POINTER, dev3->propbit);
1433 error = input_register_device(dev3);
1435 psmouse_err(psmouse,
1436 "failed to register secondary device: %d\n",
1438 input_free_device(dev3);
1446 * Save the error code so that we can detect that we
1447 * already tried to create the device.
1450 priv->dev3 = ERR_PTR(error);
1452 mutex_unlock(&alps_mutex);
1455 static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
1456 unsigned char packet[],
1457 bool report_buttons)
1459 struct alps_data *priv = psmouse->private;
1460 struct input_dev *dev, *dev2 = NULL;
1462 /* Figure out which device to use to report the bare packet */
1463 if (priv->proto_version == ALPS_PROTO_V2 &&
1464 (priv->flags & ALPS_DUALPOINT)) {
1465 /* On V2 devices the DualPoint Stick reports bare packets */
1467 dev2 = psmouse->dev;
1468 } else if (unlikely(IS_ERR_OR_NULL(priv->dev3))) {
1469 /* Register dev3 mouse if we received PS/2 packet first time */
1470 if (!IS_ERR(priv->dev3))
1471 psmouse_queue_work(psmouse, &priv->dev3_register_work,
1479 alps_report_buttons(dev, dev2,
1480 packet[0] & 1, packet[0] & 2, packet[0] & 4);
1482 input_report_rel(dev, REL_X,
1483 packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
1484 input_report_rel(dev, REL_Y,
1485 packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
1490 static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
1492 struct alps_data *priv = psmouse->private;
1494 if (psmouse->pktcnt < 6)
1495 return PSMOUSE_GOOD_DATA;
1497 if (psmouse->pktcnt == 6) {
1499 * Start a timer to flush the packet if it ends up last
1500 * 6-byte packet in the stream. Timer needs to fire
1501 * psmouse core times out itself. 20 ms should be enough
1502 * to decide if we are getting more data or not.
1504 mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
1505 return PSMOUSE_GOOD_DATA;
1508 del_timer(&priv->timer);
1510 if (psmouse->packet[6] & 0x80) {
1513 * Highest bit is set - that means we either had
1514 * complete ALPS packet and this is start of the
1515 * next packet or we got garbage.
1518 if (((psmouse->packet[3] |
1519 psmouse->packet[4] |
1520 psmouse->packet[5]) & 0x80) ||
1521 (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) {
1522 psmouse_dbg(psmouse,
1523 "refusing packet %4ph (suspected interleaved ps/2)\n",
1524 psmouse->packet + 3);
1525 return PSMOUSE_BAD_DATA;
1528 priv->process_packet(psmouse);
1530 /* Continue with the next packet */
1531 psmouse->packet[0] = psmouse->packet[6];
1532 psmouse->pktcnt = 1;
1537 * High bit is 0 - that means that we indeed got a PS/2
1538 * packet in the middle of ALPS packet.
1540 * There is also possibility that we got 6-byte ALPS
1541 * packet followed by 3-byte packet from trackpoint. We
1542 * can not distinguish between these 2 scenarios but
1543 * because the latter is unlikely to happen in course of
1544 * normal operation (user would need to press all
1545 * buttons on the pad and start moving trackpoint
1546 * without touching the pad surface) we assume former.
1547 * Even if we are wrong the wost thing that would happen
1548 * the cursor would jump but we should not get protocol
1549 * de-synchronization.
1552 alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
1556 * Continue with the standard ALPS protocol handling,
1557 * but make sure we won't process it as an interleaved
1558 * packet again, which may happen if all buttons are
1559 * pressed. To avoid this let's reset the 4th bit which
1562 psmouse->packet[3] = psmouse->packet[6] & 0xf7;
1563 psmouse->pktcnt = 4;
1566 return PSMOUSE_GOOD_DATA;
1569 static void alps_flush_packet(unsigned long data)
1571 struct psmouse *psmouse = (struct psmouse *)data;
1572 struct alps_data *priv = psmouse->private;
1574 serio_pause_rx(psmouse->ps2dev.serio);
1576 if (psmouse->pktcnt == psmouse->pktsize) {
1579 * We did not any more data in reasonable amount of time.
1580 * Validate the last 3 bytes and process as a standard
1583 if ((psmouse->packet[3] |
1584 psmouse->packet[4] |
1585 psmouse->packet[5]) & 0x80) {
1586 psmouse_dbg(psmouse,
1587 "refusing packet %3ph (suspected interleaved ps/2)\n",
1588 psmouse->packet + 3);
1590 priv->process_packet(psmouse);
1592 psmouse->pktcnt = 0;
1595 serio_continue_rx(psmouse->ps2dev.serio);
1598 static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
1600 struct alps_data *priv = psmouse->private;
1603 * Check if we are dealing with a bare PS/2 packet, presumably from
1604 * a device connected to the external PS/2 port. Because bare PS/2
1605 * protocol does not have enough constant bits to self-synchronize
1606 * properly we only do this if the device is fully synchronized.
1607 * Can not distinguish V8's first byte from PS/2 packet's
1609 if (priv->proto_version != ALPS_PROTO_V8 &&
1610 !psmouse->out_of_sync_cnt &&
1611 (psmouse->packet[0] & 0xc8) == 0x08) {
1613 if (psmouse->pktcnt == 3) {
1614 alps_report_bare_ps2_packet(psmouse, psmouse->packet,
1616 return PSMOUSE_FULL_PACKET;
1618 return PSMOUSE_GOOD_DATA;
1621 /* Check for PS/2 packet stuffed in the middle of ALPS packet. */
1623 if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
1624 psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
1625 return alps_handle_interleaved_ps2(psmouse);
1628 if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
1629 psmouse_dbg(psmouse,
1630 "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
1631 psmouse->packet[0], priv->mask0, priv->byte0);
1632 return PSMOUSE_BAD_DATA;
1635 /* Bytes 2 - pktsize should have 0 in the highest bit */
1636 if (priv->proto_version < ALPS_PROTO_V5 &&
1637 psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
1638 (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
1639 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1640 psmouse->pktcnt - 1,
1641 psmouse->packet[psmouse->pktcnt - 1]);
1643 if (priv->proto_version == ALPS_PROTO_V3_RUSHMORE &&
1644 psmouse->pktcnt == psmouse->pktsize) {
1646 * Some Dell boxes, such as Latitude E6440 or E7440
1647 * with closed lid, quite often smash last byte of
1648 * otherwise valid packet with 0xff. Given that the
1649 * next packet is very likely to be valid let's
1650 * report PSMOUSE_FULL_PACKET but not process data,
1651 * rather than reporting PSMOUSE_BAD_DATA and
1654 return PSMOUSE_FULL_PACKET;
1657 return PSMOUSE_BAD_DATA;
1660 if ((priv->proto_version == ALPS_PROTO_V7 &&
1661 !alps_is_valid_package_v7(psmouse)) ||
1662 (priv->proto_version == ALPS_PROTO_V8 &&
1663 !alps_is_valid_package_ss4_v2(psmouse))) {
1664 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1665 psmouse->pktcnt - 1,
1666 psmouse->packet[psmouse->pktcnt - 1]);
1667 return PSMOUSE_BAD_DATA;
1670 if (psmouse->pktcnt == psmouse->pktsize) {
1671 priv->process_packet(psmouse);
1672 return PSMOUSE_FULL_PACKET;
1675 return PSMOUSE_GOOD_DATA;
1678 static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
1680 struct ps2dev *ps2dev = &psmouse->ps2dev;
1681 struct alps_data *priv = psmouse->private;
1683 unsigned char *param;
1684 unsigned char dummy[4];
1686 BUG_ON(nibble > 0xf);
1688 command = priv->nibble_commands[nibble].command;
1689 param = (command & 0x0f00) ?
1690 dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
1692 if (ps2_command(ps2dev, param, command))
1698 static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
1700 struct ps2dev *ps2dev = &psmouse->ps2dev;
1701 struct alps_data *priv = psmouse->private;
1704 if (ps2_command(ps2dev, NULL, priv->addr_command))
1707 for (i = 12; i >= 0; i -= 4) {
1708 nibble = (addr >> i) & 0xf;
1709 if (alps_command_mode_send_nibble(psmouse, nibble))
1716 static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1718 struct ps2dev *ps2dev = &psmouse->ps2dev;
1719 unsigned char param[4];
1721 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1725 * The address being read is returned in the first two bytes
1726 * of the result. Check that this address matches the expected
1729 if (addr != ((param[0] << 8) | param[1]))
1735 static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1737 if (alps_command_mode_set_addr(psmouse, addr))
1739 return __alps_command_mode_read_reg(psmouse, addr);
1742 static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
1744 if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
1746 if (alps_command_mode_send_nibble(psmouse, value & 0xf))
1751 static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
1754 if (alps_command_mode_set_addr(psmouse, addr))
1756 return __alps_command_mode_write_reg(psmouse, value);
1759 static int alps_rpt_cmd(struct psmouse *psmouse, int init_command,
1760 int repeated_command, unsigned char *param)
1762 struct ps2dev *ps2dev = &psmouse->ps2dev;
1765 if (init_command && ps2_command(ps2dev, param, init_command))
1768 if (ps2_command(ps2dev, NULL, repeated_command) ||
1769 ps2_command(ps2dev, NULL, repeated_command) ||
1770 ps2_command(ps2dev, NULL, repeated_command))
1773 param[0] = param[1] = param[2] = 0xff;
1774 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1777 psmouse_dbg(psmouse, "%2.2X report: %3ph\n",
1778 repeated_command, param);
1782 static bool alps_check_valid_firmware_id(unsigned char id[])
1787 if (id[0] == 0x88 &&
1790 (id[1] & 0xf0) == 0xb0 ||
1791 (id[1] & 0xf0) == 0xc0)) {
1798 static int alps_enter_command_mode(struct psmouse *psmouse)
1800 unsigned char param[4];
1802 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) {
1803 psmouse_err(psmouse, "failed to enter command mode\n");
1807 if (!alps_check_valid_firmware_id(param)) {
1808 psmouse_dbg(psmouse,
1809 "unknown response while entering command mode\n");
1815 static inline int alps_exit_command_mode(struct psmouse *psmouse)
1817 struct ps2dev *ps2dev = &psmouse->ps2dev;
1818 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
1824 * For DualPoint devices select the device that should respond to
1825 * subsequent commands. It looks like glidepad is behind stickpointer,
1826 * I'd thought it would be other way around...
1828 static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
1830 struct ps2dev *ps2dev = &psmouse->ps2dev;
1831 int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
1833 if (ps2_command(ps2dev, NULL, cmd) ||
1834 ps2_command(ps2dev, NULL, cmd) ||
1835 ps2_command(ps2dev, NULL, cmd) ||
1836 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1839 /* we may get 3 more bytes, just ignore them */
1840 ps2_drain(ps2dev, 3, 100);
1845 static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
1847 struct ps2dev *ps2dev = &psmouse->ps2dev;
1849 /* Try ALPS magic knock - 4 disable before enable */
1850 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1851 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1852 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1853 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1854 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
1858 * Switch mouse to poll (remote) mode so motion data will not
1861 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
1864 static int alps_monitor_mode_send_word(struct psmouse *psmouse, u16 word)
1869 * b0-b11 are valid bits, send sequence is inverse.
1870 * e.g. when word = 0x0123, nibble send sequence is 3, 2, 1
1872 for (i = 0; i <= 8; i += 4) {
1873 nibble = (word >> i) & 0xf;
1874 if (alps_command_mode_send_nibble(psmouse, nibble))
1881 static int alps_monitor_mode_write_reg(struct psmouse *psmouse,
1882 u16 addr, u16 value)
1884 struct ps2dev *ps2dev = &psmouse->ps2dev;
1886 /* 0x0A0 is the command to write the word */
1887 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE) ||
1888 alps_monitor_mode_send_word(psmouse, 0x0A0) ||
1889 alps_monitor_mode_send_word(psmouse, addr) ||
1890 alps_monitor_mode_send_word(psmouse, value) ||
1891 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1897 static int alps_monitor_mode(struct psmouse *psmouse, bool enable)
1899 struct ps2dev *ps2dev = &psmouse->ps2dev;
1902 /* EC E9 F5 F5 E7 E6 E7 E9 to enter monitor mode */
1903 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
1904 ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO) ||
1905 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1906 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1907 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1908 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1909 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1910 ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO))
1913 /* EC to exit monitor mode */
1914 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP))
1921 static int alps_absolute_mode_v6(struct psmouse *psmouse)
1923 u16 reg_val = 0x181;
1926 /* enter monitor mode, to write the register */
1927 if (alps_monitor_mode(psmouse, true))
1930 ret = alps_monitor_mode_write_reg(psmouse, 0x000, reg_val);
1932 if (alps_monitor_mode(psmouse, false))
1938 static int alps_get_status(struct psmouse *psmouse, char *param)
1940 /* Get status: 0xF5 0xF5 0xF5 0xE9 */
1941 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param))
1948 * Turn touchpad tapping on or off. The sequences are:
1949 * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
1950 * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
1951 * My guess that 0xE9 (GetInfo) is here as a sync point.
1952 * For models that also have stickpointer (DualPoints) its tapping
1953 * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
1954 * we don't fiddle with it.
1956 static int alps_tap_mode(struct psmouse *psmouse, int enable)
1958 struct ps2dev *ps2dev = &psmouse->ps2dev;
1959 int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
1960 unsigned char tap_arg = enable ? 0x0A : 0x00;
1961 unsigned char param[4];
1963 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
1964 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1965 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1966 ps2_command(ps2dev, &tap_arg, cmd))
1969 if (alps_get_status(psmouse, param))
1976 * alps_poll() - poll the touchpad for current motion packet.
1979 static int alps_poll(struct psmouse *psmouse)
1981 struct alps_data *priv = psmouse->private;
1982 unsigned char buf[sizeof(psmouse->packet)];
1985 if (priv->flags & ALPS_PASS)
1986 alps_passthrough_mode_v2(psmouse, true);
1988 poll_failed = ps2_command(&psmouse->ps2dev, buf,
1989 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
1991 if (priv->flags & ALPS_PASS)
1992 alps_passthrough_mode_v2(psmouse, false);
1994 if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
1997 if ((psmouse->badbyte & 0xc8) == 0x08) {
1999 * Poll the track stick ...
2001 if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
2005 memcpy(psmouse->packet, buf, sizeof(buf));
2009 static int alps_hw_init_v1_v2(struct psmouse *psmouse)
2011 struct alps_data *priv = psmouse->private;
2013 if ((priv->flags & ALPS_PASS) &&
2014 alps_passthrough_mode_v2(psmouse, true)) {
2018 if (alps_tap_mode(psmouse, true)) {
2019 psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
2023 if (alps_absolute_mode_v1_v2(psmouse)) {
2024 psmouse_err(psmouse, "Failed to enable absolute mode\n");
2028 if ((priv->flags & ALPS_PASS) &&
2029 alps_passthrough_mode_v2(psmouse, false)) {
2033 /* ALPS needs stream mode, otherwise it won't report any data */
2034 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
2035 psmouse_err(psmouse, "Failed to enable stream mode\n");
2042 static int alps_hw_init_v6(struct psmouse *psmouse)
2044 unsigned char param[2] = {0xC8, 0x14};
2046 /* Enter passthrough mode to let trackpoint enter 6byte raw mode */
2047 if (alps_passthrough_mode_v2(psmouse, true))
2050 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2051 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2052 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2053 ps2_command(&psmouse->ps2dev, ¶m[0], PSMOUSE_CMD_SETRATE) ||
2054 ps2_command(&psmouse->ps2dev, ¶m[1], PSMOUSE_CMD_SETRATE))
2057 if (alps_passthrough_mode_v2(psmouse, false))
2060 if (alps_absolute_mode_v6(psmouse)) {
2061 psmouse_err(psmouse, "Failed to enable absolute mode\n");
2069 * Enable or disable passthrough mode to the trackstick.
2071 static int alps_passthrough_mode_v3(struct psmouse *psmouse,
2072 int reg_base, bool enable)
2074 int reg_val, ret = -1;
2076 if (alps_enter_command_mode(psmouse))
2079 reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x0008);
2088 ret = __alps_command_mode_write_reg(psmouse, reg_val);
2091 if (alps_exit_command_mode(psmouse))
2096 /* Must be in command mode when calling this function */
2097 static int alps_absolute_mode_v3(struct psmouse *psmouse)
2101 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
2106 if (__alps_command_mode_write_reg(psmouse, reg_val))
2112 static int alps_probe_trackstick_v3_v7(struct psmouse *psmouse, int reg_base)
2114 int ret = -EIO, reg_val;
2116 if (alps_enter_command_mode(psmouse))
2119 reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x08);
2123 /* bit 7: trackstick is present */
2124 ret = reg_val & 0x80 ? 0 : -ENODEV;
2127 alps_exit_command_mode(psmouse);
2131 static int alps_setup_trackstick_v3(struct psmouse *psmouse, int reg_base)
2133 struct ps2dev *ps2dev = &psmouse->ps2dev;
2135 unsigned char param[4];
2137 if (alps_passthrough_mode_v3(psmouse, reg_base, true))
2141 * E7 report for the trackstick
2143 * There have been reports of failures to seem to trace back
2144 * to the above trackstick check failing. When these occur
2145 * this E7 report fails, so when that happens we continue
2146 * with the assumption that there isn't a trackstick after
2149 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_SETSCALE21, param)) {
2150 psmouse_warn(psmouse, "Failed to initialize trackstick (E7 report failed)\n");
2153 psmouse_dbg(psmouse, "trackstick E7 report: %3ph\n", param);
2156 * Not sure what this does, but it is absolutely
2157 * essential. Without it, the touchpad does not
2158 * work at all and the trackstick just emits normal
2161 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2162 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2163 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2164 alps_command_mode_send_nibble(psmouse, 0x9) ||
2165 alps_command_mode_send_nibble(psmouse, 0x4)) {
2166 psmouse_err(psmouse,
2167 "Error sending magic E6 sequence\n");
2173 * This ensures the trackstick packets are in the format
2174 * supported by this driver. If bit 1 isn't set the packet
2175 * format is different.
2177 if (alps_enter_command_mode(psmouse) ||
2178 alps_command_mode_write_reg(psmouse,
2179 reg_base + 0x08, 0x82) ||
2180 alps_exit_command_mode(psmouse))
2185 if (alps_passthrough_mode_v3(psmouse, reg_base, false))
2191 static int alps_hw_init_v3(struct psmouse *psmouse)
2193 struct alps_data *priv = psmouse->private;
2194 struct ps2dev *ps2dev = &psmouse->ps2dev;
2196 unsigned char param[4];
2198 if ((priv->flags & ALPS_DUALPOINT) &&
2199 alps_setup_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE) == -EIO)
2202 if (alps_enter_command_mode(psmouse) ||
2203 alps_absolute_mode_v3(psmouse)) {
2204 psmouse_err(psmouse, "Failed to enter absolute mode\n");
2208 reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
2211 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
2214 reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
2217 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
2220 if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
2222 if (__alps_command_mode_write_reg(psmouse, 0x04))
2225 if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
2227 if (__alps_command_mode_write_reg(psmouse, 0x03))
2230 if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
2232 if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
2235 if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
2237 if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
2240 alps_exit_command_mode(psmouse);
2242 /* Set rate and enable data reporting */
2244 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
2245 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
2246 psmouse_err(psmouse, "Failed to enable data reporting\n");
2254 * Leaving the touchpad in command mode will essentially render
2255 * it unusable until the machine reboots, so exit it here just
2258 alps_exit_command_mode(psmouse);
2262 static int alps_get_v3_v7_resolution(struct psmouse *psmouse, int reg_pitch)
2264 int reg, x_pitch, y_pitch, x_electrode, y_electrode, x_phys, y_phys;
2265 struct alps_data *priv = psmouse->private;
2267 reg = alps_command_mode_read_reg(psmouse, reg_pitch);
2271 x_pitch = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
2272 x_pitch = 50 + 2 * x_pitch; /* In 0.1 mm units */
2274 y_pitch = (char)reg >> 4; /* sign extend upper 4 bits */
2275 y_pitch = 36 + 2 * y_pitch; /* In 0.1 mm units */
2277 reg = alps_command_mode_read_reg(psmouse, reg_pitch + 1);
2281 x_electrode = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
2282 x_electrode = 17 + x_electrode;
2284 y_electrode = (char)reg >> 4; /* sign extend upper 4 bits */
2285 y_electrode = 13 + y_electrode;
2287 x_phys = x_pitch * (x_electrode - 1); /* In 0.1 mm units */
2288 y_phys = y_pitch * (y_electrode - 1); /* In 0.1 mm units */
2290 priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
2291 priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
2293 psmouse_dbg(psmouse,
2294 "pitch %dx%d num-electrodes %dx%d physical size %dx%d mm res %dx%d\n",
2295 x_pitch, y_pitch, x_electrode, y_electrode,
2296 x_phys / 10, y_phys / 10, priv->x_res, priv->y_res);
2301 static int alps_hw_init_rushmore_v3(struct psmouse *psmouse)
2303 struct alps_data *priv = psmouse->private;
2304 struct ps2dev *ps2dev = &psmouse->ps2dev;
2305 int reg_val, ret = -1;
2307 if (priv->flags & ALPS_DUALPOINT) {
2308 reg_val = alps_setup_trackstick_v3(psmouse,
2309 ALPS_REG_BASE_RUSHMORE);
2310 if (reg_val == -EIO)
2314 if (alps_enter_command_mode(psmouse) ||
2315 alps_command_mode_read_reg(psmouse, 0xc2d9) == -1 ||
2316 alps_command_mode_write_reg(psmouse, 0xc2cb, 0x00))
2319 if (alps_get_v3_v7_resolution(psmouse, 0xc2da))
2322 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c6);
2325 if (__alps_command_mode_write_reg(psmouse, reg_val & 0xfd))
2328 if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
2331 /* enter absolute mode */
2332 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
2335 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
2338 alps_exit_command_mode(psmouse);
2339 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2342 alps_exit_command_mode(psmouse);
2346 /* Must be in command mode when calling this function */
2347 static int alps_absolute_mode_v4(struct psmouse *psmouse)
2351 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
2356 if (__alps_command_mode_write_reg(psmouse, reg_val))
2362 static int alps_hw_init_v4(struct psmouse *psmouse)
2364 struct ps2dev *ps2dev = &psmouse->ps2dev;
2365 unsigned char param[4];
2367 if (alps_enter_command_mode(psmouse))
2370 if (alps_absolute_mode_v4(psmouse)) {
2371 psmouse_err(psmouse, "Failed to enter absolute mode\n");
2375 if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
2378 if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
2381 if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
2384 if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
2387 if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
2390 if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
2393 if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
2396 if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
2399 alps_exit_command_mode(psmouse);
2402 * This sequence changes the output from a 9-byte to an
2403 * 8-byte format. All the same data seems to be present,
2404 * just in a more compact format.
2409 if (ps2_command(ps2dev, ¶m[0], PSMOUSE_CMD_SETRATE) ||
2410 ps2_command(ps2dev, ¶m[1], PSMOUSE_CMD_SETRATE) ||
2411 ps2_command(ps2dev, ¶m[2], PSMOUSE_CMD_SETRATE) ||
2412 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
2415 /* Set rate and enable data reporting */
2417 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
2418 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
2419 psmouse_err(psmouse, "Failed to enable data reporting\n");
2427 * Leaving the touchpad in command mode will essentially render
2428 * it unusable until the machine reboots, so exit it here just
2431 alps_exit_command_mode(psmouse);
2435 static int alps_get_otp_values_ss4_v2(struct psmouse *psmouse,
2436 unsigned char index, unsigned char otp[])
2438 struct ps2dev *ps2dev = &psmouse->ps2dev;
2442 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2443 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2444 ps2_command(ps2dev, otp, PSMOUSE_CMD_GETINFO))
2450 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2451 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2452 ps2_command(ps2dev, otp, PSMOUSE_CMD_GETINFO))
2461 static int alps_update_device_area_ss4_v2(unsigned char otp[][4],
2462 struct alps_data *priv)
2464 int num_x_electrode;
2465 int num_y_electrode;
2466 int x_pitch, y_pitch, x_phys, y_phys;
2468 if (IS_SS4PLUS_DEV(priv->dev_id)) {
2470 SS4PLUS_NUMSENSOR_XOFFSET + (otp[0][2] & 0x0F);
2472 SS4PLUS_NUMSENSOR_YOFFSET + ((otp[0][2] >> 4) & 0x0F);
2475 (num_x_electrode - 1) * SS4PLUS_COUNT_PER_ELECTRODE;
2477 (num_y_electrode - 1) * SS4PLUS_COUNT_PER_ELECTRODE;
2479 x_pitch = (otp[0][1] & 0x0F) + SS4PLUS_MIN_PITCH_MM;
2480 y_pitch = ((otp[0][1] >> 4) & 0x0F) + SS4PLUS_MIN_PITCH_MM;
2484 SS4_NUMSENSOR_XOFFSET + (otp[1][0] & 0x0F);
2486 SS4_NUMSENSOR_YOFFSET + ((otp[1][0] >> 4) & 0x0F);
2489 (num_x_electrode - 1) * SS4_COUNT_PER_ELECTRODE;
2491 (num_y_electrode - 1) * SS4_COUNT_PER_ELECTRODE;
2493 x_pitch = ((otp[1][2] >> 2) & 0x07) + SS4_MIN_PITCH_MM;
2494 y_pitch = ((otp[1][2] >> 5) & 0x07) + SS4_MIN_PITCH_MM;
2497 x_phys = x_pitch * (num_x_electrode - 1); /* In 0.1 mm units */
2498 y_phys = y_pitch * (num_y_electrode - 1); /* In 0.1 mm units */
2500 priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
2501 priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
2506 static int alps_update_btn_info_ss4_v2(unsigned char otp[][4],
2507 struct alps_data *priv)
2509 unsigned char is_btnless;
2511 if (IS_SS4PLUS_DEV(priv->dev_id))
2512 is_btnless = (otp[1][0] >> 1) & 0x01;
2514 is_btnless = (otp[1][1] >> 3) & 0x01;
2517 priv->flags |= ALPS_BUTTONPAD;
2522 static int alps_update_dual_info_ss4_v2(unsigned char otp[][4],
2523 struct alps_data *priv)
2525 bool is_dual = false;
2527 if (IS_SS4PLUS_DEV(priv->dev_id))
2528 is_dual = (otp[0][0] >> 4) & 0x01;
2531 priv->flags |= ALPS_DUALPOINT |
2532 ALPS_DUALPOINT_WITH_PRESSURE;
2537 static int alps_set_defaults_ss4_v2(struct psmouse *psmouse,
2538 struct alps_data *priv)
2540 unsigned char otp[2][4];
2542 memset(otp, 0, sizeof(otp));
2544 if (alps_get_otp_values_ss4_v2(psmouse, 0, &otp[0][0]) ||
2545 alps_get_otp_values_ss4_v2(psmouse, 1, &otp[1][0]))
2548 alps_update_device_area_ss4_v2(otp, priv);
2550 alps_update_btn_info_ss4_v2(otp, priv);
2552 alps_update_dual_info_ss4_v2(otp, priv);
2557 static int alps_dolphin_get_device_area(struct psmouse *psmouse,
2558 struct alps_data *priv)
2560 struct ps2dev *ps2dev = &psmouse->ps2dev;
2561 unsigned char param[4] = {0};
2562 int num_x_electrode, num_y_electrode;
2564 if (alps_enter_command_mode(psmouse))
2568 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
2569 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2570 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2571 ps2_command(ps2dev, ¶m[0], PSMOUSE_CMD_SETRATE) ||
2572 ps2_command(ps2dev, ¶m[0], PSMOUSE_CMD_SETRATE))
2575 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
2579 * Dolphin's sensor line number is not fixed. It can be calculated
2580 * by adding the device's register value with DOLPHIN_PROFILE_X/YOFFSET.
2581 * Further more, we can get device's x_max and y_max by multiplying
2582 * sensor line number with DOLPHIN_COUNT_PER_ELECTRODE.
2584 * e.g. When we get register's sensor_x = 11 & sensor_y = 8,
2585 * real sensor line number X = 11 + 8 = 19, and
2586 * real sensor line number Y = 8 + 1 = 9.
2587 * So, x_max = (19 - 1) * 64 = 1152, and
2588 * y_max = (9 - 1) * 64 = 512.
2590 num_x_electrode = DOLPHIN_PROFILE_XOFFSET + (param[2] & 0x0F);
2591 num_y_electrode = DOLPHIN_PROFILE_YOFFSET + ((param[2] >> 4) & 0x0F);
2592 priv->x_bits = num_x_electrode;
2593 priv->y_bits = num_y_electrode;
2594 priv->x_max = (num_x_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2595 priv->y_max = (num_y_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2597 if (alps_exit_command_mode(psmouse))
2603 static int alps_hw_init_dolphin_v1(struct psmouse *psmouse)
2605 struct ps2dev *ps2dev = &psmouse->ps2dev;
2606 unsigned char param[2];
2608 /* This is dolphin "v1" as empirically defined by florin9doi */
2612 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2613 ps2_command(ps2dev, ¶m[0], PSMOUSE_CMD_SETRATE) ||
2614 ps2_command(ps2dev, ¶m[1], PSMOUSE_CMD_SETRATE))
2620 static int alps_hw_init_v7(struct psmouse *psmouse)
2622 struct ps2dev *ps2dev = &psmouse->ps2dev;
2623 int reg_val, ret = -1;
2625 if (alps_enter_command_mode(psmouse) ||
2626 alps_command_mode_read_reg(psmouse, 0xc2d9) == -1)
2629 if (alps_get_v3_v7_resolution(psmouse, 0xc397))
2632 if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
2635 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
2638 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
2641 alps_exit_command_mode(psmouse);
2642 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2645 alps_exit_command_mode(psmouse);
2649 static int alps_hw_init_ss4_v2(struct psmouse *psmouse)
2651 struct ps2dev *ps2dev = &psmouse->ps2dev;
2652 char param[2] = {0x64, 0x28};
2655 /* enter absolute mode */
2656 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2657 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2658 ps2_command(ps2dev, ¶m[0], PSMOUSE_CMD_SETRATE) ||
2659 ps2_command(ps2dev, ¶m[1], PSMOUSE_CMD_SETRATE)) {
2663 /* T.B.D. Decread noise packet number, delete in the future */
2664 if (alps_exit_command_mode(psmouse) ||
2665 alps_enter_command_mode(psmouse) ||
2666 alps_command_mode_write_reg(psmouse, 0x001D, 0x20)) {
2669 alps_exit_command_mode(psmouse);
2671 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2674 alps_exit_command_mode(psmouse);
2678 static int alps_set_protocol(struct psmouse *psmouse,
2679 struct alps_data *priv,
2680 const struct alps_protocol_info *protocol)
2682 psmouse->private = priv;
2684 setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
2686 priv->proto_version = protocol->version;
2687 priv->byte0 = protocol->byte0;
2688 priv->mask0 = protocol->mask0;
2689 priv->flags = protocol->flags;
2696 switch (priv->proto_version) {
2699 priv->hw_init = alps_hw_init_v1_v2;
2700 priv->process_packet = alps_process_packet_v1_v2;
2701 priv->set_abs_params = alps_set_abs_params_st;
2704 if (dmi_check_system(alps_dmi_has_separate_stick_buttons))
2705 priv->flags |= ALPS_STICK_BITS;
2709 priv->hw_init = alps_hw_init_v3;
2710 priv->process_packet = alps_process_packet_v3;
2711 priv->set_abs_params = alps_set_abs_params_semi_mt;
2712 priv->decode_fields = alps_decode_pinnacle;
2713 priv->nibble_commands = alps_v3_nibble_commands;
2714 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2716 if (alps_probe_trackstick_v3_v7(psmouse,
2717 ALPS_REG_BASE_PINNACLE) < 0)
2718 priv->flags &= ~ALPS_DUALPOINT;
2722 case ALPS_PROTO_V3_RUSHMORE:
2723 priv->hw_init = alps_hw_init_rushmore_v3;
2724 priv->process_packet = alps_process_packet_v3;
2725 priv->set_abs_params = alps_set_abs_params_semi_mt;
2726 priv->decode_fields = alps_decode_rushmore;
2727 priv->nibble_commands = alps_v3_nibble_commands;
2728 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2732 if (alps_probe_trackstick_v3_v7(psmouse,
2733 ALPS_REG_BASE_RUSHMORE) < 0)
2734 priv->flags &= ~ALPS_DUALPOINT;
2739 priv->hw_init = alps_hw_init_v4;
2740 priv->process_packet = alps_process_packet_v4;
2741 priv->set_abs_params = alps_set_abs_params_semi_mt;
2742 priv->nibble_commands = alps_v4_nibble_commands;
2743 priv->addr_command = PSMOUSE_CMD_DISABLE;
2747 priv->hw_init = alps_hw_init_dolphin_v1;
2748 priv->process_packet = alps_process_touchpad_packet_v3_v5;
2749 priv->decode_fields = alps_decode_dolphin;
2750 priv->set_abs_params = alps_set_abs_params_semi_mt;
2751 priv->nibble_commands = alps_v3_nibble_commands;
2752 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2756 if (alps_dolphin_get_device_area(psmouse, priv))
2762 priv->hw_init = alps_hw_init_v6;
2763 priv->process_packet = alps_process_packet_v6;
2764 priv->set_abs_params = alps_set_abs_params_st;
2765 priv->nibble_commands = alps_v6_nibble_commands;
2771 priv->hw_init = alps_hw_init_v7;
2772 priv->process_packet = alps_process_packet_v7;
2773 priv->decode_fields = alps_decode_packet_v7;
2774 priv->set_abs_params = alps_set_abs_params_v7;
2775 priv->nibble_commands = alps_v3_nibble_commands;
2776 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2777 priv->x_max = 0xfff;
2778 priv->y_max = 0x7ff;
2780 if (priv->fw_ver[1] != 0xba)
2781 priv->flags |= ALPS_BUTTONPAD;
2783 if (alps_probe_trackstick_v3_v7(psmouse, ALPS_REG_BASE_V7) < 0)
2784 priv->flags &= ~ALPS_DUALPOINT;
2789 priv->hw_init = alps_hw_init_ss4_v2;
2790 priv->process_packet = alps_process_packet_ss4_v2;
2791 priv->decode_fields = alps_decode_ss4_v2;
2792 priv->set_abs_params = alps_set_abs_params_ss4_v2;
2793 priv->nibble_commands = alps_v3_nibble_commands;
2794 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2796 if (alps_set_defaults_ss4_v2(psmouse, priv))
2805 static const struct alps_protocol_info *alps_match_table(unsigned char *e7,
2808 const struct alps_model_info *model;
2811 for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
2812 model = &alps_model_data[i];
2814 if (!memcmp(e7, model->signature, sizeof(model->signature)))
2815 return &model->protocol_info;
2821 static int alps_identify(struct psmouse *psmouse, struct alps_data *priv)
2823 const struct alps_protocol_info *protocol;
2824 unsigned char e6[4], e7[4], ec[4];
2828 * First try "E6 report".
2829 * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
2830 * The bits 0-2 of the first byte will be 1s if some buttons are
2833 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2834 PSMOUSE_CMD_SETSCALE11, e6))
2837 if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100))
2841 * Now get the "E7" and "EC" reports. These will uniquely identify
2842 * most ALPS touchpads.
2844 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2845 PSMOUSE_CMD_SETSCALE21, e7) ||
2846 alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2847 PSMOUSE_CMD_RESET_WRAP, ec) ||
2848 alps_exit_command_mode(psmouse))
2851 protocol = alps_match_table(e7, ec);
2853 if (e7[0] == 0x73 && e7[1] == 0x02 && e7[2] == 0x64 &&
2855 protocol = &alps_v4_protocol_data;
2856 } else if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0x50 &&
2857 ec[0] == 0x73 && (ec[1] == 0x01 || ec[1] == 0x02)) {
2858 protocol = &alps_v5_protocol_data;
2859 } else if (ec[0] == 0x88 &&
2860 ((ec[1] & 0xf0) == 0xb0 || (ec[1] & 0xf0) == 0xc0)) {
2861 protocol = &alps_v7_protocol_data;
2862 } else if (ec[0] == 0x88 && ec[1] == 0x08) {
2863 protocol = &alps_v3_rushmore_data;
2864 } else if (ec[0] == 0x88 && ec[1] == 0x07 &&
2865 ec[2] >= 0x90 && ec[2] <= 0x9d) {
2866 protocol = &alps_v3_protocol_data;
2867 } else if (e7[0] == 0x73 && e7[1] == 0x03 &&
2868 (e7[2] == 0x14 || e7[2] == 0x28)) {
2869 protocol = &alps_v8_protocol_data;
2870 } else if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0xc8) {
2871 protocol = &alps_v9_protocol_data;
2872 psmouse_warn(psmouse,
2873 "Unsupported ALPS V9 touchpad: E7=%3ph, EC=%3ph\n",
2877 psmouse_dbg(psmouse,
2878 "Likely not an ALPS touchpad: E7=%3ph, EC=%3ph\n", e7, ec);
2884 /* Save Device ID and Firmware version */
2885 memcpy(priv->dev_id, e7, 3);
2886 memcpy(priv->fw_ver, ec, 3);
2887 error = alps_set_protocol(psmouse, priv, protocol);
2895 static int alps_reconnect(struct psmouse *psmouse)
2897 struct alps_data *priv = psmouse->private;
2899 psmouse_reset(psmouse);
2901 if (alps_identify(psmouse, priv) < 0)
2904 return priv->hw_init(psmouse);
2907 static void alps_disconnect(struct psmouse *psmouse)
2909 struct alps_data *priv = psmouse->private;
2911 psmouse_reset(psmouse);
2912 del_timer_sync(&priv->timer);
2914 input_unregister_device(priv->dev2);
2915 if (!IS_ERR_OR_NULL(priv->dev3))
2916 input_unregister_device(priv->dev3);
2920 static void alps_set_abs_params_st(struct alps_data *priv,
2921 struct input_dev *dev1)
2923 input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
2924 input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
2925 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
2928 static void alps_set_abs_params_mt_common(struct alps_data *priv,
2929 struct input_dev *dev1)
2931 input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
2932 input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
2934 input_abs_set_res(dev1, ABS_MT_POSITION_X, priv->x_res);
2935 input_abs_set_res(dev1, ABS_MT_POSITION_Y, priv->y_res);
2937 set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
2938 set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
2941 static void alps_set_abs_params_semi_mt(struct alps_data *priv,
2942 struct input_dev *dev1)
2944 alps_set_abs_params_mt_common(priv, dev1);
2945 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
2947 input_mt_init_slots(dev1, MAX_TOUCHES,
2948 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
2952 static void alps_set_abs_params_v7(struct alps_data *priv,
2953 struct input_dev *dev1)
2955 alps_set_abs_params_mt_common(priv, dev1);
2956 set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
2958 input_mt_init_slots(dev1, MAX_TOUCHES,
2959 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
2962 set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
2965 static void alps_set_abs_params_ss4_v2(struct alps_data *priv,
2966 struct input_dev *dev1)
2968 alps_set_abs_params_mt_common(priv, dev1);
2969 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
2970 set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
2972 input_mt_init_slots(dev1, MAX_TOUCHES,
2973 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
2977 int alps_init(struct psmouse *psmouse)
2979 struct alps_data *priv = psmouse->private;
2980 struct input_dev *dev1 = psmouse->dev;
2983 error = priv->hw_init(psmouse);
2988 * Undo part of setup done for us by psmouse core since touchpad
2989 * is not a relative device.
2991 __clear_bit(EV_REL, dev1->evbit);
2992 __clear_bit(REL_X, dev1->relbit);
2993 __clear_bit(REL_Y, dev1->relbit);
2996 * Now set up our capabilities.
2998 dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
2999 dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
3000 dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
3001 dev1->keybit[BIT_WORD(BTN_LEFT)] |=
3002 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
3004 dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
3006 priv->set_abs_params(priv, dev1);
3008 if (priv->flags & ALPS_WHEEL) {
3009 dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
3010 dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
3013 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
3014 dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
3015 dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
3018 if (priv->flags & ALPS_FOUR_BUTTONS) {
3019 dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
3020 dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
3021 dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
3022 dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
3023 } else if (priv->flags & ALPS_BUTTONPAD) {
3024 set_bit(INPUT_PROP_BUTTONPAD, dev1->propbit);
3025 clear_bit(BTN_RIGHT, dev1->keybit);
3027 dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
3030 if (priv->flags & ALPS_DUALPOINT) {
3031 struct input_dev *dev2;
3033 dev2 = input_allocate_device();
3035 psmouse_err(psmouse,
3036 "failed to allocate trackstick device\n");
3041 snprintf(priv->phys2, sizeof(priv->phys2), "%s/input1",
3042 psmouse->ps2dev.serio->phys);
3043 dev2->phys = priv->phys2;
3046 * format of input device name is: "protocol vendor name"
3047 * see function psmouse_switch_protocol() in psmouse-base.c
3049 dev2->name = "AlpsPS/2 ALPS DualPoint Stick";
3051 dev2->id.bustype = BUS_I8042;
3052 dev2->id.vendor = 0x0002;
3053 dev2->id.product = PSMOUSE_ALPS;
3054 dev2->id.version = priv->proto_version;
3055 dev2->dev.parent = &psmouse->ps2dev.serio->dev;
3057 input_set_capability(dev2, EV_REL, REL_X);
3058 input_set_capability(dev2, EV_REL, REL_Y);
3059 if (priv->flags & ALPS_DUALPOINT_WITH_PRESSURE) {
3060 input_set_capability(dev2, EV_ABS, ABS_PRESSURE);
3061 input_set_abs_params(dev2, ABS_PRESSURE, 0, 127, 0, 0);
3063 input_set_capability(dev2, EV_KEY, BTN_LEFT);
3064 input_set_capability(dev2, EV_KEY, BTN_RIGHT);
3065 input_set_capability(dev2, EV_KEY, BTN_MIDDLE);
3067 __set_bit(INPUT_PROP_POINTER, dev2->propbit);
3068 __set_bit(INPUT_PROP_POINTING_STICK, dev2->propbit);
3070 error = input_register_device(dev2);
3072 psmouse_err(psmouse,
3073 "failed to register trackstick device: %d\n",
3075 input_free_device(dev2);
3082 priv->psmouse = psmouse;
3084 INIT_DELAYED_WORK(&priv->dev3_register_work,
3085 alps_register_bare_ps2_mouse);
3087 psmouse->protocol_handler = alps_process_byte;
3088 psmouse->poll = alps_poll;
3089 psmouse->disconnect = alps_disconnect;
3090 psmouse->reconnect = alps_reconnect;
3091 psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
3093 /* We are having trouble resyncing ALPS touchpads so disable it for now */
3094 psmouse->resync_time = 0;
3096 /* Allow 2 invalid packets without resetting device */
3097 psmouse->resetafter = psmouse->pktsize * 2;
3102 psmouse_reset(psmouse);
3104 * Even though we did not allocate psmouse->private we do free
3107 kfree(psmouse->private);
3108 psmouse->private = NULL;
3112 int alps_detect(struct psmouse *psmouse, bool set_properties)
3114 struct alps_data *priv;
3117 error = alps_identify(psmouse, NULL);
3122 * Reset the device to make sure it is fully operational:
3123 * on some laptops, like certain Dell Latitudes, we may
3124 * fail to properly detect presence of trackstick if device
3125 * has not been reset.
3127 psmouse_reset(psmouse);
3129 priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
3133 error = alps_identify(psmouse, priv);
3139 if (set_properties) {
3140 psmouse->vendor = "ALPS";
3141 psmouse->name = priv->flags & ALPS_DUALPOINT ?
3142 "DualPoint TouchPad" : "GlidePoint";
3143 psmouse->model = priv->proto_version;
3146 * Destroy alps_data structure we allocated earlier since
3147 * this was just a "trial run". Otherwise we'll keep it
3148 * to be used by alps_init() which has to be called if
3149 * we succeed and set_properties is true.
3152 psmouse->private = NULL;