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>
28 * Definitions for ALPS version 3 and 4 command mode protocol
30 #define ALPS_CMD_NIBBLE_10 0x01f2
32 #define ALPS_REG_BASE_RUSHMORE 0xc2c0
33 #define ALPS_REG_BASE_PINNACLE 0x0000
35 static const struct alps_nibble_commands alps_v3_nibble_commands[] = {
36 { PSMOUSE_CMD_SETPOLL, 0x00 }, /* 0 */
37 { PSMOUSE_CMD_RESET_DIS, 0x00 }, /* 1 */
38 { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* 2 */
39 { PSMOUSE_CMD_SETRATE, 0x0a }, /* 3 */
40 { PSMOUSE_CMD_SETRATE, 0x14 }, /* 4 */
41 { PSMOUSE_CMD_SETRATE, 0x28 }, /* 5 */
42 { PSMOUSE_CMD_SETRATE, 0x3c }, /* 6 */
43 { PSMOUSE_CMD_SETRATE, 0x50 }, /* 7 */
44 { PSMOUSE_CMD_SETRATE, 0x64 }, /* 8 */
45 { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 9 */
46 { ALPS_CMD_NIBBLE_10, 0x00 }, /* a */
47 { PSMOUSE_CMD_SETRES, 0x00 }, /* b */
48 { PSMOUSE_CMD_SETRES, 0x01 }, /* c */
49 { PSMOUSE_CMD_SETRES, 0x02 }, /* d */
50 { PSMOUSE_CMD_SETRES, 0x03 }, /* e */
51 { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */
54 static const struct alps_nibble_commands alps_v4_nibble_commands[] = {
55 { PSMOUSE_CMD_ENABLE, 0x00 }, /* 0 */
56 { PSMOUSE_CMD_RESET_DIS, 0x00 }, /* 1 */
57 { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* 2 */
58 { PSMOUSE_CMD_SETRATE, 0x0a }, /* 3 */
59 { PSMOUSE_CMD_SETRATE, 0x14 }, /* 4 */
60 { PSMOUSE_CMD_SETRATE, 0x28 }, /* 5 */
61 { PSMOUSE_CMD_SETRATE, 0x3c }, /* 6 */
62 { PSMOUSE_CMD_SETRATE, 0x50 }, /* 7 */
63 { PSMOUSE_CMD_SETRATE, 0x64 }, /* 8 */
64 { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 9 */
65 { ALPS_CMD_NIBBLE_10, 0x00 }, /* a */
66 { PSMOUSE_CMD_SETRES, 0x00 }, /* b */
67 { PSMOUSE_CMD_SETRES, 0x01 }, /* c */
68 { PSMOUSE_CMD_SETRES, 0x02 }, /* d */
69 { PSMOUSE_CMD_SETRES, 0x03 }, /* e */
70 { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */
73 static const struct alps_nibble_commands alps_v6_nibble_commands[] = {
74 { PSMOUSE_CMD_ENABLE, 0x00 }, /* 0 */
75 { PSMOUSE_CMD_SETRATE, 0x0a }, /* 1 */
76 { PSMOUSE_CMD_SETRATE, 0x14 }, /* 2 */
77 { PSMOUSE_CMD_SETRATE, 0x28 }, /* 3 */
78 { PSMOUSE_CMD_SETRATE, 0x3c }, /* 4 */
79 { PSMOUSE_CMD_SETRATE, 0x50 }, /* 5 */
80 { PSMOUSE_CMD_SETRATE, 0x64 }, /* 6 */
81 { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 7 */
82 { PSMOUSE_CMD_GETID, 0x00 }, /* 8 */
83 { PSMOUSE_CMD_GETINFO, 0x00 }, /* 9 */
84 { PSMOUSE_CMD_SETRES, 0x00 }, /* a */
85 { PSMOUSE_CMD_SETRES, 0x01 }, /* b */
86 { PSMOUSE_CMD_SETRES, 0x02 }, /* c */
87 { PSMOUSE_CMD_SETRES, 0x03 }, /* d */
88 { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* e */
89 { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */
93 #define ALPS_DUALPOINT 0x02 /* touchpad has trackstick */
94 #define ALPS_PASS 0x04 /* device has a pass-through port */
96 #define ALPS_WHEEL 0x08 /* hardware wheel present */
97 #define ALPS_FW_BK_1 0x10 /* front & back buttons present */
98 #define ALPS_FW_BK_2 0x20 /* front & back buttons present */
99 #define ALPS_FOUR_BUTTONS 0x40 /* 4 direction button present */
100 #define ALPS_PS2_INTERLEAVED 0x80 /* 3-byte PS/2 packet interleaved with
101 6-byte ALPS packet */
103 static const struct alps_model_info alps_model_data[] = {
104 { { 0x32, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* Toshiba Salellite Pro M10 */
105 { { 0x33, 0x02, 0x0a }, 0x00, ALPS_PROTO_V1, 0x88, 0xf8, 0 }, /* UMAX-530T */
106 { { 0x53, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
107 { { 0x53, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
108 { { 0x60, 0x03, 0xc8 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 }, /* HP ze1115 */
109 { { 0x63, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
110 { { 0x63, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
111 { { 0x63, 0x02, 0x28 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 }, /* Fujitsu Siemens S6010 */
112 { { 0x63, 0x02, 0x3c }, 0x00, ALPS_PROTO_V2, 0x8f, 0x8f, ALPS_WHEEL }, /* Toshiba Satellite S2400-103 */
113 { { 0x63, 0x02, 0x50 }, 0x00, ALPS_PROTO_V2, 0xef, 0xef, ALPS_FW_BK_1 }, /* NEC Versa L320 */
114 { { 0x63, 0x02, 0x64 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
115 { { 0x63, 0x03, 0xc8 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* Dell Latitude D800 */
116 { { 0x73, 0x00, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_DUALPOINT }, /* ThinkPad R61 8918-5QG */
117 { { 0x73, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
118 { { 0x73, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 }, /* Ahtec Laptop */
119 { { 0x20, 0x02, 0x0e }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* XXX */
120 { { 0x22, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT },
121 { { 0x22, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT }, /* Dell Latitude D600 */
122 /* Dell Latitude E5500, E6400, E6500, Precision M4400 */
123 { { 0x62, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xcf, 0xcf,
124 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED },
125 { { 0x73, 0x00, 0x14 }, 0x00, ALPS_PROTO_V6, 0xff, 0xff, ALPS_DUALPOINT }, /* Dell XT2 */
126 { { 0x73, 0x02, 0x50 }, 0x00, ALPS_PROTO_V2, 0xcf, 0xcf, ALPS_FOUR_BUTTONS }, /* Dell Vostro 1400 */
127 { { 0x52, 0x01, 0x14 }, 0x00, ALPS_PROTO_V2, 0xff, 0xff,
128 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED }, /* Toshiba Tecra A11-11L */
129 { { 0x73, 0x02, 0x64 }, 0x8a, ALPS_PROTO_V4, 0x8f, 0x8f, 0 },
132 static void alps_set_abs_params_st(struct alps_data *priv,
133 struct input_dev *dev1);
134 static void alps_set_abs_params_mt(struct alps_data *priv,
135 struct input_dev *dev1);
138 * XXX - this entry is suspicious. First byte has zero lower nibble,
139 * which is what a normal mouse would report. Also, the value 0x0e
140 * isn't valid per PS/2 spec.
143 /* Packet formats are described in Documentation/input/alps.txt */
145 static bool alps_is_valid_first_byte(struct alps_data *priv,
148 return (data & priv->mask0) == priv->byte0;
151 static void alps_report_buttons(struct psmouse *psmouse,
152 struct input_dev *dev1, struct input_dev *dev2,
153 int left, int right, int middle)
155 struct input_dev *dev;
158 * If shared button has already been reported on the
159 * other device (dev2) then this event should be also
160 * sent through that device.
162 dev = test_bit(BTN_LEFT, dev2->key) ? dev2 : dev1;
163 input_report_key(dev, BTN_LEFT, left);
165 dev = test_bit(BTN_RIGHT, dev2->key) ? dev2 : dev1;
166 input_report_key(dev, BTN_RIGHT, right);
168 dev = test_bit(BTN_MIDDLE, dev2->key) ? dev2 : dev1;
169 input_report_key(dev, BTN_MIDDLE, middle);
172 * Sync the _other_ device now, we'll do the first
173 * device later once we report the rest of the events.
178 static void alps_process_packet_v1_v2(struct psmouse *psmouse)
180 struct alps_data *priv = psmouse->private;
181 unsigned char *packet = psmouse->packet;
182 struct input_dev *dev = psmouse->dev;
183 struct input_dev *dev2 = priv->dev2;
184 int x, y, z, ges, fin, left, right, middle;
185 int back = 0, forward = 0;
187 if (priv->proto_version == ALPS_PROTO_V1) {
188 left = packet[2] & 0x10;
189 right = packet[2] & 0x08;
191 x = packet[1] | ((packet[0] & 0x07) << 7);
192 y = packet[4] | ((packet[3] & 0x07) << 7);
195 left = packet[3] & 1;
196 right = packet[3] & 2;
197 middle = packet[3] & 4;
198 x = packet[1] | ((packet[2] & 0x78) << (7 - 3));
199 y = packet[4] | ((packet[3] & 0x70) << (7 - 4));
203 if (priv->flags & ALPS_FW_BK_1) {
204 back = packet[0] & 0x10;
205 forward = packet[2] & 4;
208 if (priv->flags & ALPS_FW_BK_2) {
209 back = packet[3] & 4;
210 forward = packet[2] & 4;
211 if ((middle = forward && back))
218 if ((priv->flags & ALPS_DUALPOINT) && z == 127) {
219 input_report_rel(dev2, REL_X, (x > 383 ? (x - 768) : x));
220 input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y));
222 alps_report_buttons(psmouse, dev2, dev, left, right, middle);
228 alps_report_buttons(psmouse, dev, dev2, left, right, middle);
230 /* Convert hardware tap to a reasonable Z value */
235 * A "tap and drag" operation is reported by the hardware as a transition
236 * from (!fin && ges) to (fin && ges). This should be translated to the
237 * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
239 if (ges && fin && !priv->prev_fin) {
240 input_report_abs(dev, ABS_X, x);
241 input_report_abs(dev, ABS_Y, y);
242 input_report_abs(dev, ABS_PRESSURE, 0);
243 input_report_key(dev, BTN_TOOL_FINGER, 0);
246 priv->prev_fin = fin;
249 input_report_key(dev, BTN_TOUCH, 1);
251 input_report_key(dev, BTN_TOUCH, 0);
254 input_report_abs(dev, ABS_X, x);
255 input_report_abs(dev, ABS_Y, y);
258 input_report_abs(dev, ABS_PRESSURE, z);
259 input_report_key(dev, BTN_TOOL_FINGER, z > 0);
261 if (priv->flags & ALPS_WHEEL)
262 input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
264 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
265 input_report_key(dev, BTN_FORWARD, forward);
266 input_report_key(dev, BTN_BACK, back);
269 if (priv->flags & ALPS_FOUR_BUTTONS) {
270 input_report_key(dev, BTN_0, packet[2] & 4);
271 input_report_key(dev, BTN_1, packet[0] & 0x10);
272 input_report_key(dev, BTN_2, packet[3] & 4);
273 input_report_key(dev, BTN_3, packet[0] & 0x20);
280 * Process bitmap data from v3 and v4 protocols. Returns the number of
281 * fingers detected. A return value of 0 means at least one of the
284 * The bitmaps don't have enough data to track fingers, so this function
285 * only generates points representing a bounding box of all contacts.
286 * These points are returned in x1, y1, x2, and y2 when the return value
289 static int alps_process_bitmap(struct alps_data *priv,
290 unsigned int x_map, unsigned int y_map,
291 int *x1, int *y1, int *x2, int *y2)
293 struct alps_bitmap_point {
298 int fingers_x = 0, fingers_y = 0, fingers;
299 int i, bit, prev_bit;
300 struct alps_bitmap_point x_low = {0,}, x_high = {0,};
301 struct alps_bitmap_point y_low = {0,}, y_high = {0,};
302 struct alps_bitmap_point *point;
304 if (!x_map || !y_map)
307 *x1 = *y1 = *x2 = *y2 = 0;
311 for (i = 0; x_map != 0; i++, x_map >>= 1) {
315 point->start_bit = i;
329 * y bitmap is reversed for what we need (lower positions are in
330 * higher bits), so we process from the top end.
332 y_map = y_map << (sizeof(y_map) * BITS_PER_BYTE - priv->y_bits);
335 for (i = 0; y_map != 0; i++, y_map <<= 1) {
336 bit = y_map & (1 << (sizeof(y_map) * BITS_PER_BYTE - 1));
339 point->start_bit = i;
353 * Fingers can overlap, so we use the maximum count of fingers
354 * on either axis as the finger count.
356 fingers = max(fingers_x, fingers_y);
359 * If total fingers is > 1 but either axis reports only a single
360 * contact, we have overlapping or adjacent fingers. For the
361 * purposes of creating a bounding box, divide the single contact
362 * (roughly) equally between the two points.
365 if (fingers_x == 1) {
366 i = x_low.num_bits / 2;
367 x_low.num_bits = x_low.num_bits - i;
368 x_high.start_bit = x_low.start_bit + i;
369 x_high.num_bits = max(i, 1);
370 } else if (fingers_y == 1) {
371 i = y_low.num_bits / 2;
372 y_low.num_bits = y_low.num_bits - i;
373 y_high.start_bit = y_low.start_bit + i;
374 y_high.num_bits = max(i, 1);
378 *x1 = (priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
379 (2 * (priv->x_bits - 1));
380 *y1 = (priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
381 (2 * (priv->y_bits - 1));
385 (2 * x_high.start_bit + x_high.num_bits - 1)) /
386 (2 * (priv->x_bits - 1));
388 (2 * y_high.start_bit + y_high.num_bits - 1)) /
389 (2 * (priv->y_bits - 1));
395 static void alps_set_slot(struct input_dev *dev, int slot, bool active,
398 input_mt_slot(dev, slot);
399 input_mt_report_slot_state(dev, MT_TOOL_FINGER, active);
401 input_report_abs(dev, ABS_MT_POSITION_X, x);
402 input_report_abs(dev, ABS_MT_POSITION_Y, y);
406 static void alps_report_semi_mt_data(struct input_dev *dev, int num_fingers,
407 int x1, int y1, int x2, int y2)
409 alps_set_slot(dev, 0, num_fingers != 0, x1, y1);
410 alps_set_slot(dev, 1, num_fingers == 2, x2, y2);
413 static void alps_process_trackstick_packet_v3(struct psmouse *psmouse)
415 struct alps_data *priv = psmouse->private;
416 unsigned char *packet = psmouse->packet;
417 struct input_dev *dev = priv->dev2;
418 int x, y, z, left, right, middle;
420 /* Sanity check packet */
421 if (!(packet[0] & 0x40)) {
422 psmouse_dbg(psmouse, "Bad trackstick packet, discarding\n");
427 * There's a special packet that seems to indicate the end
428 * of a stream of trackstick data. Filter these out.
430 if (packet[1] == 0x7f && packet[2] == 0x7f && packet[4] == 0x7f)
433 x = (s8)(((packet[0] & 0x20) << 2) | (packet[1] & 0x7f));
434 y = (s8)(((packet[0] & 0x10) << 3) | (packet[2] & 0x7f));
435 z = (packet[4] & 0x7c) >> 2;
438 * The x and y values tend to be quite large, and when used
439 * alone the trackstick is difficult to use. Scale them down
445 input_report_rel(dev, REL_X, x);
446 input_report_rel(dev, REL_Y, -y);
449 * Most ALPS models report the trackstick buttons in the touchpad
450 * packets, but a few report them here. No reliable way has been
451 * found to differentiate between the models upfront, so we enable
452 * the quirk in response to seeing a button press in the trackstick
455 left = packet[3] & 0x01;
456 right = packet[3] & 0x02;
457 middle = packet[3] & 0x04;
459 if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) &&
460 (left || right || middle))
461 priv->quirks |= ALPS_QUIRK_TRACKSTICK_BUTTONS;
463 if (priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) {
464 input_report_key(dev, BTN_LEFT, left);
465 input_report_key(dev, BTN_RIGHT, right);
466 input_report_key(dev, BTN_MIDDLE, middle);
473 static void alps_decode_buttons_v3(struct alps_fields *f, unsigned char *p)
475 f->left = !!(p[3] & 0x01);
476 f->right = !!(p[3] & 0x02);
477 f->middle = !!(p[3] & 0x04);
479 f->ts_left = !!(p[3] & 0x10);
480 f->ts_right = !!(p[3] & 0x20);
481 f->ts_middle = !!(p[3] & 0x40);
484 static void alps_decode_pinnacle(struct alps_fields *f, unsigned char *p)
486 f->first_mp = !!(p[4] & 0x40);
487 f->is_mp = !!(p[0] & 0x40);
489 f->fingers = (p[5] & 0x3) + 1;
490 f->x_map = ((p[4] & 0x7e) << 8) |
491 ((p[1] & 0x7f) << 2) |
492 ((p[0] & 0x30) >> 4);
493 f->y_map = ((p[3] & 0x70) << 4) |
494 ((p[2] & 0x7f) << 1) |
497 f->x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
498 ((p[0] & 0x30) >> 4);
499 f->y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
502 alps_decode_buttons_v3(f, p);
505 static void alps_decode_rushmore(struct alps_fields *f, unsigned char *p)
507 alps_decode_pinnacle(f, p);
509 f->x_map |= (p[5] & 0x10) << 11;
510 f->y_map |= (p[5] & 0x20) << 6;
513 static void alps_decode_dolphin(struct alps_fields *f, unsigned char *p)
515 f->first_mp = !!(p[0] & 0x02);
516 f->is_mp = !!(p[0] & 0x20);
518 f->fingers = ((p[0] & 0x6) >> 1 |
520 f->x_map = ((p[2] & 0x60) >> 5) |
521 ((p[4] & 0x7f) << 2) |
522 ((p[5] & 0x7f) << 9) |
523 ((p[3] & 0x07) << 16) |
524 ((p[3] & 0x70) << 15) |
525 ((p[0] & 0x01) << 22);
526 f->y_map = (p[1] & 0x7f) |
527 ((p[2] & 0x1f) << 7);
529 f->x = ((p[1] & 0x7f) | ((p[4] & 0x0f) << 7));
530 f->y = ((p[2] & 0x7f) | ((p[4] & 0xf0) << 3));
531 f->z = (p[0] & 4) ? 0 : p[5] & 0x7f;
533 alps_decode_buttons_v3(f, p);
536 static void alps_process_touchpad_packet_v3(struct psmouse *psmouse)
538 struct alps_data *priv = psmouse->private;
539 unsigned char *packet = psmouse->packet;
540 struct input_dev *dev = psmouse->dev;
541 struct input_dev *dev2 = priv->dev2;
542 int x1 = 0, y1 = 0, x2 = 0, y2 = 0;
543 int fingers = 0, bmap_fingers;
544 struct alps_fields f;
546 priv->decode_fields(&f, packet);
549 * There's no single feature of touchpad position and bitmap packets
550 * that can be used to distinguish between them. We rely on the fact
551 * that a bitmap packet should always follow a position packet with
552 * bit 6 of packet[4] set.
554 if (priv->multi_packet) {
556 * Sometimes a position packet will indicate a multi-packet
557 * sequence, but then what follows is another position
558 * packet. Check for this, and when it happens process the
559 * position packet as usual.
563 bmap_fingers = alps_process_bitmap(priv,
568 * We shouldn't report more than one finger if
569 * we don't have two coordinates.
571 if (fingers > 1 && bmap_fingers < 2)
572 fingers = bmap_fingers;
574 /* Now process position packet */
575 priv->decode_fields(&f, priv->multi_data);
577 priv->multi_packet = 0;
582 * Bit 6 of byte 0 is not usually set in position packets. The only
583 * times it seems to be set is in situations where the data is
584 * suspect anyway, e.g. a palm resting flat on the touchpad. Given
585 * this combined with the fact that this bit is useful for filtering
586 * out misidentified bitmap packets, we reject anything with this
592 if (!priv->multi_packet && f.first_mp) {
593 priv->multi_packet = 1;
594 memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
598 priv->multi_packet = 0;
601 * Sometimes the hardware sends a single packet with z = 0
602 * in the middle of a stream. Real releases generate packets
603 * with x, y, and z all zero, so these seem to be flukes.
606 if (f.x && f.y && !f.z)
610 * If we don't have MT data or the bitmaps were empty, we have
611 * to rely on ST data.
616 fingers = f.z > 0 ? 1 : 0;
620 input_report_key(dev, BTN_TOUCH, 1);
622 input_report_key(dev, BTN_TOUCH, 0);
624 alps_report_semi_mt_data(dev, fingers, x1, y1, x2, y2);
626 input_mt_report_finger_count(dev, fingers);
628 input_report_key(dev, BTN_LEFT, f.left);
629 input_report_key(dev, BTN_RIGHT, f.right);
630 input_report_key(dev, BTN_MIDDLE, f.middle);
633 input_report_abs(dev, ABS_X, f.x);
634 input_report_abs(dev, ABS_Y, f.y);
636 input_report_abs(dev, ABS_PRESSURE, f.z);
640 if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) {
641 input_report_key(dev2, BTN_LEFT, f.ts_left);
642 input_report_key(dev2, BTN_RIGHT, f.ts_right);
643 input_report_key(dev2, BTN_MIDDLE, f.ts_middle);
648 static void alps_process_packet_v3(struct psmouse *psmouse)
650 unsigned char *packet = psmouse->packet;
653 * v3 protocol packets come in three types, two representing
654 * touchpad data and one representing trackstick data.
655 * Trackstick packets seem to be distinguished by always
656 * having 0x3f in the last byte. This value has never been
657 * observed in the last byte of either of the other types
660 if (packet[5] == 0x3f) {
661 alps_process_trackstick_packet_v3(psmouse);
665 alps_process_touchpad_packet_v3(psmouse);
668 static void alps_process_packet_v6(struct psmouse *psmouse)
670 struct alps_data *priv = psmouse->private;
671 unsigned char *packet = psmouse->packet;
672 struct input_dev *dev = psmouse->dev;
673 struct input_dev *dev2 = priv->dev2;
674 int x, y, z, left, right, middle;
677 * We can use Byte5 to distinguish if the packet is from Touchpad
682 if (packet[5] == 0x7F) {
683 /* It should be a DualPoint when received Trackpoint packet */
684 if (!(priv->flags & ALPS_DUALPOINT))
687 /* Trackpoint packet */
688 x = packet[1] | ((packet[3] & 0x20) << 2);
689 y = packet[2] | ((packet[3] & 0x40) << 1);
691 left = packet[3] & 0x01;
692 right = packet[3] & 0x02;
693 middle = packet[3] & 0x04;
695 /* To prevent the cursor jump when finger lifted */
696 if (x == 0x7F && y == 0x7F && z == 0x7F)
699 /* Divide 4 since trackpoint's speed is too fast */
700 input_report_rel(dev2, REL_X, (char)x / 4);
701 input_report_rel(dev2, REL_Y, -((char)y / 4));
703 input_report_key(dev2, BTN_LEFT, left);
704 input_report_key(dev2, BTN_RIGHT, right);
705 input_report_key(dev2, BTN_MIDDLE, middle);
711 /* Touchpad packet */
712 x = packet[1] | ((packet[3] & 0x78) << 4);
713 y = packet[2] | ((packet[4] & 0x78) << 4);
715 left = packet[3] & 0x01;
716 right = packet[3] & 0x02;
719 input_report_key(dev, BTN_TOUCH, 1);
721 input_report_key(dev, BTN_TOUCH, 0);
724 input_report_abs(dev, ABS_X, x);
725 input_report_abs(dev, ABS_Y, y);
728 input_report_abs(dev, ABS_PRESSURE, z);
729 input_report_key(dev, BTN_TOOL_FINGER, z > 0);
731 /* v6 touchpad does not have middle button */
732 input_report_key(dev, BTN_LEFT, left);
733 input_report_key(dev, BTN_RIGHT, right);
738 static void alps_process_packet_v4(struct psmouse *psmouse)
740 struct alps_data *priv = psmouse->private;
741 unsigned char *packet = psmouse->packet;
742 struct input_dev *dev = psmouse->dev;
748 unsigned int x_bitmap, y_bitmap;
751 * v4 has a 6-byte encoding for bitmap data, but this data is
752 * broken up between 3 normal packets. Use priv->multi_packet to
753 * track our position in the bitmap packet.
755 if (packet[6] & 0x40) {
756 /* sync, reset position */
757 priv->multi_packet = 0;
760 if (WARN_ON_ONCE(priv->multi_packet > 2))
763 offset = 2 * priv->multi_packet;
764 priv->multi_data[offset] = packet[6];
765 priv->multi_data[offset + 1] = packet[7];
767 if (++priv->multi_packet > 2) {
768 priv->multi_packet = 0;
770 x_bitmap = ((priv->multi_data[2] & 0x1f) << 10) |
771 ((priv->multi_data[3] & 0x60) << 3) |
772 ((priv->multi_data[0] & 0x3f) << 2) |
773 ((priv->multi_data[1] & 0x60) >> 5);
774 y_bitmap = ((priv->multi_data[5] & 0x01) << 10) |
775 ((priv->multi_data[3] & 0x1f) << 5) |
776 (priv->multi_data[1] & 0x1f);
778 fingers = alps_process_bitmap(priv, x_bitmap, y_bitmap,
782 priv->fingers = fingers;
789 left = packet[4] & 0x01;
790 right = packet[4] & 0x02;
792 x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) |
793 ((packet[0] & 0x30) >> 4);
794 y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f);
795 z = packet[5] & 0x7f;
798 * If there were no contacts in the bitmap, use ST
799 * points in MT reports.
800 * If there were two contacts or more, report MT data.
802 if (priv->fingers < 2) {
805 fingers = z > 0 ? 1 : 0;
807 fingers = priv->fingers;
815 input_report_key(dev, BTN_TOUCH, 1);
817 input_report_key(dev, BTN_TOUCH, 0);
819 alps_report_semi_mt_data(dev, fingers, x1, y1, x2, y2);
821 input_mt_report_finger_count(dev, fingers);
823 input_report_key(dev, BTN_LEFT, left);
824 input_report_key(dev, BTN_RIGHT, right);
827 input_report_abs(dev, ABS_X, x);
828 input_report_abs(dev, ABS_Y, y);
830 input_report_abs(dev, ABS_PRESSURE, z);
835 static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
836 unsigned char packet[],
839 struct alps_data *priv = psmouse->private;
840 struct input_dev *dev2 = priv->dev2;
843 alps_report_buttons(psmouse, dev2, psmouse->dev,
844 packet[0] & 1, packet[0] & 2, packet[0] & 4);
846 input_report_rel(dev2, REL_X,
847 packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
848 input_report_rel(dev2, REL_Y,
849 packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
854 static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
856 struct alps_data *priv = psmouse->private;
858 if (psmouse->pktcnt < 6)
859 return PSMOUSE_GOOD_DATA;
861 if (psmouse->pktcnt == 6) {
863 * Start a timer to flush the packet if it ends up last
864 * 6-byte packet in the stream. Timer needs to fire
865 * psmouse core times out itself. 20 ms should be enough
866 * to decide if we are getting more data or not.
868 mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
869 return PSMOUSE_GOOD_DATA;
872 del_timer(&priv->timer);
874 if (psmouse->packet[6] & 0x80) {
877 * Highest bit is set - that means we either had
878 * complete ALPS packet and this is start of the
879 * next packet or we got garbage.
882 if (((psmouse->packet[3] |
884 psmouse->packet[5]) & 0x80) ||
885 (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) {
887 "refusing packet %4ph (suspected interleaved ps/2)\n",
888 psmouse->packet + 3);
889 return PSMOUSE_BAD_DATA;
892 priv->process_packet(psmouse);
894 /* Continue with the next packet */
895 psmouse->packet[0] = psmouse->packet[6];
901 * High bit is 0 - that means that we indeed got a PS/2
902 * packet in the middle of ALPS packet.
904 * There is also possibility that we got 6-byte ALPS
905 * packet followed by 3-byte packet from trackpoint. We
906 * can not distinguish between these 2 scenarios but
907 * because the latter is unlikely to happen in course of
908 * normal operation (user would need to press all
909 * buttons on the pad and start moving trackpoint
910 * without touching the pad surface) we assume former.
911 * Even if we are wrong the wost thing that would happen
912 * the cursor would jump but we should not get protocol
913 * de-synchronization.
916 alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
920 * Continue with the standard ALPS protocol handling,
921 * but make sure we won't process it as an interleaved
922 * packet again, which may happen if all buttons are
923 * pressed. To avoid this let's reset the 4th bit which
926 psmouse->packet[3] = psmouse->packet[6] & 0xf7;
930 return PSMOUSE_GOOD_DATA;
933 static void alps_flush_packet(unsigned long data)
935 struct psmouse *psmouse = (struct psmouse *)data;
936 struct alps_data *priv = psmouse->private;
938 serio_pause_rx(psmouse->ps2dev.serio);
940 if (psmouse->pktcnt == psmouse->pktsize) {
943 * We did not any more data in reasonable amount of time.
944 * Validate the last 3 bytes and process as a standard
947 if ((psmouse->packet[3] |
949 psmouse->packet[5]) & 0x80) {
951 "refusing packet %3ph (suspected interleaved ps/2)\n",
952 psmouse->packet + 3);
954 priv->process_packet(psmouse);
959 serio_continue_rx(psmouse->ps2dev.serio);
962 static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
964 struct alps_data *priv = psmouse->private;
966 if ((psmouse->packet[0] & 0xc8) == 0x08) { /* PS/2 packet */
967 if (psmouse->pktcnt == 3) {
968 alps_report_bare_ps2_packet(psmouse, psmouse->packet,
970 return PSMOUSE_FULL_PACKET;
972 return PSMOUSE_GOOD_DATA;
975 /* Check for PS/2 packet stuffed in the middle of ALPS packet. */
977 if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
978 psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
979 return alps_handle_interleaved_ps2(psmouse);
982 if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
984 "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
985 psmouse->packet[0], priv->mask0, priv->byte0);
986 return PSMOUSE_BAD_DATA;
989 /* Bytes 2 - pktsize should have 0 in the highest bit */
990 if ((priv->proto_version < ALPS_PROTO_V5) &&
991 psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
992 (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
993 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
995 psmouse->packet[psmouse->pktcnt - 1]);
996 return PSMOUSE_BAD_DATA;
999 if (psmouse->pktcnt == psmouse->pktsize) {
1000 priv->process_packet(psmouse);
1001 return PSMOUSE_FULL_PACKET;
1004 return PSMOUSE_GOOD_DATA;
1007 static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
1009 struct ps2dev *ps2dev = &psmouse->ps2dev;
1010 struct alps_data *priv = psmouse->private;
1012 unsigned char *param;
1013 unsigned char dummy[4];
1015 BUG_ON(nibble > 0xf);
1017 command = priv->nibble_commands[nibble].command;
1018 param = (command & 0x0f00) ?
1019 dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
1021 if (ps2_command(ps2dev, param, command))
1027 static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
1029 struct ps2dev *ps2dev = &psmouse->ps2dev;
1030 struct alps_data *priv = psmouse->private;
1033 if (ps2_command(ps2dev, NULL, priv->addr_command))
1036 for (i = 12; i >= 0; i -= 4) {
1037 nibble = (addr >> i) & 0xf;
1038 if (alps_command_mode_send_nibble(psmouse, nibble))
1045 static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1047 struct ps2dev *ps2dev = &psmouse->ps2dev;
1048 unsigned char param[4];
1050 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1054 * The address being read is returned in the first two bytes
1055 * of the result. Check that this address matches the expected
1058 if (addr != ((param[0] << 8) | param[1]))
1064 static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1066 if (alps_command_mode_set_addr(psmouse, addr))
1068 return __alps_command_mode_read_reg(psmouse, addr);
1071 static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
1073 if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
1075 if (alps_command_mode_send_nibble(psmouse, value & 0xf))
1080 static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
1083 if (alps_command_mode_set_addr(psmouse, addr))
1085 return __alps_command_mode_write_reg(psmouse, value);
1088 static int alps_rpt_cmd(struct psmouse *psmouse, int init_command,
1089 int repeated_command, unsigned char *param)
1091 struct ps2dev *ps2dev = &psmouse->ps2dev;
1094 if (init_command && ps2_command(ps2dev, param, init_command))
1097 if (ps2_command(ps2dev, NULL, repeated_command) ||
1098 ps2_command(ps2dev, NULL, repeated_command) ||
1099 ps2_command(ps2dev, NULL, repeated_command))
1102 param[0] = param[1] = param[2] = 0xff;
1103 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1106 psmouse_dbg(psmouse, "%2.2X report: %3ph\n",
1107 repeated_command, param);
1111 static int alps_enter_command_mode(struct psmouse *psmouse)
1113 unsigned char param[4];
1115 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) {
1116 psmouse_err(psmouse, "failed to enter command mode\n");
1120 if ((param[0] != 0x88 || (param[1] != 0x07 && param[1] != 0x08)) &&
1122 psmouse_dbg(psmouse,
1123 "unknown response while entering command mode\n");
1129 static inline int alps_exit_command_mode(struct psmouse *psmouse)
1131 struct ps2dev *ps2dev = &psmouse->ps2dev;
1132 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
1138 * For DualPoint devices select the device that should respond to
1139 * subsequent commands. It looks like glidepad is behind stickpointer,
1140 * I'd thought it would be other way around...
1142 static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
1144 struct ps2dev *ps2dev = &psmouse->ps2dev;
1145 int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
1147 if (ps2_command(ps2dev, NULL, cmd) ||
1148 ps2_command(ps2dev, NULL, cmd) ||
1149 ps2_command(ps2dev, NULL, cmd) ||
1150 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1153 /* we may get 3 more bytes, just ignore them */
1154 ps2_drain(ps2dev, 3, 100);
1159 static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
1161 struct ps2dev *ps2dev = &psmouse->ps2dev;
1163 /* Try ALPS magic knock - 4 disable before enable */
1164 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1165 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1166 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1167 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1168 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
1172 * Switch mouse to poll (remote) mode so motion data will not
1175 return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
1178 static int alps_monitor_mode_send_word(struct psmouse *psmouse, u16 word)
1183 * b0-b11 are valid bits, send sequence is inverse.
1184 * e.g. when word = 0x0123, nibble send sequence is 3, 2, 1
1186 for (i = 0; i <= 8; i += 4) {
1187 nibble = (word >> i) & 0xf;
1188 if (alps_command_mode_send_nibble(psmouse, nibble))
1195 static int alps_monitor_mode_write_reg(struct psmouse *psmouse,
1196 u16 addr, u16 value)
1198 struct ps2dev *ps2dev = &psmouse->ps2dev;
1200 /* 0x0A0 is the command to write the word */
1201 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE) ||
1202 alps_monitor_mode_send_word(psmouse, 0x0A0) ||
1203 alps_monitor_mode_send_word(psmouse, addr) ||
1204 alps_monitor_mode_send_word(psmouse, value) ||
1205 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1211 static int alps_monitor_mode(struct psmouse *psmouse, bool enable)
1213 struct ps2dev *ps2dev = &psmouse->ps2dev;
1216 /* EC E9 F5 F5 E7 E6 E7 E9 to enter monitor mode */
1217 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
1218 ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO) ||
1219 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1220 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1221 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1222 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1223 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1224 ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO))
1227 /* EC to exit monitor mode */
1228 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP))
1235 static int alps_absolute_mode_v6(struct psmouse *psmouse)
1237 u16 reg_val = 0x181;
1240 /* enter monitor mode, to write the register */
1241 if (alps_monitor_mode(psmouse, true))
1244 ret = alps_monitor_mode_write_reg(psmouse, 0x000, reg_val);
1246 if (alps_monitor_mode(psmouse, false))
1252 static int alps_get_status(struct psmouse *psmouse, char *param)
1254 /* Get status: 0xF5 0xF5 0xF5 0xE9 */
1255 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param))
1262 * Turn touchpad tapping on or off. The sequences are:
1263 * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
1264 * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
1265 * My guess that 0xE9 (GetInfo) is here as a sync point.
1266 * For models that also have stickpointer (DualPoints) its tapping
1267 * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
1268 * we don't fiddle with it.
1270 static int alps_tap_mode(struct psmouse *psmouse, int enable)
1272 struct ps2dev *ps2dev = &psmouse->ps2dev;
1273 int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
1274 unsigned char tap_arg = enable ? 0x0A : 0x00;
1275 unsigned char param[4];
1277 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
1278 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1279 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1280 ps2_command(ps2dev, &tap_arg, cmd))
1283 if (alps_get_status(psmouse, param))
1290 * alps_poll() - poll the touchpad for current motion packet.
1293 static int alps_poll(struct psmouse *psmouse)
1295 struct alps_data *priv = psmouse->private;
1296 unsigned char buf[sizeof(psmouse->packet)];
1299 if (priv->flags & ALPS_PASS)
1300 alps_passthrough_mode_v2(psmouse, true);
1302 poll_failed = ps2_command(&psmouse->ps2dev, buf,
1303 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
1305 if (priv->flags & ALPS_PASS)
1306 alps_passthrough_mode_v2(psmouse, false);
1308 if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
1311 if ((psmouse->badbyte & 0xc8) == 0x08) {
1313 * Poll the track stick ...
1315 if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
1319 memcpy(psmouse->packet, buf, sizeof(buf));
1323 static int alps_hw_init_v1_v2(struct psmouse *psmouse)
1325 struct alps_data *priv = psmouse->private;
1327 if ((priv->flags & ALPS_PASS) &&
1328 alps_passthrough_mode_v2(psmouse, true)) {
1332 if (alps_tap_mode(psmouse, true)) {
1333 psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
1337 if (alps_absolute_mode_v1_v2(psmouse)) {
1338 psmouse_err(psmouse, "Failed to enable absolute mode\n");
1342 if ((priv->flags & ALPS_PASS) &&
1343 alps_passthrough_mode_v2(psmouse, false)) {
1347 /* ALPS needs stream mode, otherwise it won't report any data */
1348 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
1349 psmouse_err(psmouse, "Failed to enable stream mode\n");
1356 static int alps_hw_init_v6(struct psmouse *psmouse)
1358 unsigned char param[2] = {0xC8, 0x14};
1360 /* Enter passthrough mode to let trackpoint enter 6byte raw mode */
1361 if (alps_passthrough_mode_v2(psmouse, true))
1364 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1365 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1366 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1367 ps2_command(&psmouse->ps2dev, ¶m[0], PSMOUSE_CMD_SETRATE) ||
1368 ps2_command(&psmouse->ps2dev, ¶m[1], PSMOUSE_CMD_SETRATE))
1371 if (alps_passthrough_mode_v2(psmouse, false))
1374 if (alps_absolute_mode_v6(psmouse)) {
1375 psmouse_err(psmouse, "Failed to enable absolute mode\n");
1383 * Enable or disable passthrough mode to the trackstick.
1385 static int alps_passthrough_mode_v3(struct psmouse *psmouse,
1386 int reg_base, bool enable)
1388 int reg_val, ret = -1;
1390 if (alps_enter_command_mode(psmouse))
1393 reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x0008);
1402 ret = __alps_command_mode_write_reg(psmouse, reg_val);
1405 if (alps_exit_command_mode(psmouse))
1410 /* Must be in command mode when calling this function */
1411 static int alps_absolute_mode_v3(struct psmouse *psmouse)
1415 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
1420 if (__alps_command_mode_write_reg(psmouse, reg_val))
1426 static int alps_probe_trackstick_v3(struct psmouse *psmouse, int reg_base)
1428 int ret = -EIO, reg_val;
1430 if (alps_enter_command_mode(psmouse))
1433 reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x08);
1437 /* bit 7: trackstick is present */
1438 ret = reg_val & 0x80 ? 0 : -ENODEV;
1441 alps_exit_command_mode(psmouse);
1445 static int alps_setup_trackstick_v3(struct psmouse *psmouse, int reg_base)
1447 struct ps2dev *ps2dev = &psmouse->ps2dev;
1449 unsigned char param[4];
1451 if (alps_passthrough_mode_v3(psmouse, reg_base, true))
1455 * E7 report for the trackstick
1457 * There have been reports of failures to seem to trace back
1458 * to the above trackstick check failing. When these occur
1459 * this E7 report fails, so when that happens we continue
1460 * with the assumption that there isn't a trackstick after
1463 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_SETSCALE21, param)) {
1464 psmouse_warn(psmouse, "trackstick E7 report failed\n");
1467 psmouse_dbg(psmouse, "trackstick E7 report: %3ph\n", param);
1470 * Not sure what this does, but it is absolutely
1471 * essential. Without it, the touchpad does not
1472 * work at all and the trackstick just emits normal
1475 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1476 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1477 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1478 alps_command_mode_send_nibble(psmouse, 0x9) ||
1479 alps_command_mode_send_nibble(psmouse, 0x4)) {
1480 psmouse_err(psmouse,
1481 "Error sending magic E6 sequence\n");
1487 * This ensures the trackstick packets are in the format
1488 * supported by this driver. If bit 1 isn't set the packet
1489 * format is different.
1491 if (alps_enter_command_mode(psmouse) ||
1492 alps_command_mode_write_reg(psmouse,
1493 reg_base + 0x08, 0x82) ||
1494 alps_exit_command_mode(psmouse))
1499 if (alps_passthrough_mode_v3(psmouse, reg_base, false))
1505 static int alps_hw_init_v3(struct psmouse *psmouse)
1507 struct ps2dev *ps2dev = &psmouse->ps2dev;
1509 unsigned char param[4];
1511 reg_val = alps_probe_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE);
1512 if (reg_val == -EIO)
1516 alps_setup_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE) == -EIO)
1519 if (alps_enter_command_mode(psmouse) ||
1520 alps_absolute_mode_v3(psmouse)) {
1521 psmouse_err(psmouse, "Failed to enter absolute mode\n");
1525 reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
1528 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
1531 reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
1534 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
1537 if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
1539 if (__alps_command_mode_write_reg(psmouse, 0x04))
1542 if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
1544 if (__alps_command_mode_write_reg(psmouse, 0x03))
1547 if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
1549 if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
1552 if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
1554 if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
1557 alps_exit_command_mode(psmouse);
1559 /* Set rate and enable data reporting */
1561 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
1562 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1563 psmouse_err(psmouse, "Failed to enable data reporting\n");
1571 * Leaving the touchpad in command mode will essentially render
1572 * it unusable until the machine reboots, so exit it here just
1575 alps_exit_command_mode(psmouse);
1579 static int alps_hw_init_rushmore_v3(struct psmouse *psmouse)
1581 struct alps_data *priv = psmouse->private;
1582 struct ps2dev *ps2dev = &psmouse->ps2dev;
1583 int reg_val, ret = -1;
1585 if (priv->flags & ALPS_DUALPOINT) {
1586 reg_val = alps_setup_trackstick_v3(psmouse,
1587 ALPS_REG_BASE_RUSHMORE);
1588 if (reg_val == -EIO)
1590 if (reg_val == -ENODEV)
1591 priv->flags &= ~ALPS_DUALPOINT;
1594 if (alps_enter_command_mode(psmouse) ||
1595 alps_command_mode_read_reg(psmouse, 0xc2d9) == -1 ||
1596 alps_command_mode_write_reg(psmouse, 0xc2cb, 0x00))
1599 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c6);
1602 if (__alps_command_mode_write_reg(psmouse, reg_val & 0xfd))
1605 if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
1608 /* enter absolute mode */
1609 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
1612 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
1615 alps_exit_command_mode(psmouse);
1616 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
1619 alps_exit_command_mode(psmouse);
1623 /* Must be in command mode when calling this function */
1624 static int alps_absolute_mode_v4(struct psmouse *psmouse)
1628 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
1633 if (__alps_command_mode_write_reg(psmouse, reg_val))
1639 static int alps_hw_init_v4(struct psmouse *psmouse)
1641 struct ps2dev *ps2dev = &psmouse->ps2dev;
1642 unsigned char param[4];
1644 if (alps_enter_command_mode(psmouse))
1647 if (alps_absolute_mode_v4(psmouse)) {
1648 psmouse_err(psmouse, "Failed to enter absolute mode\n");
1652 if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
1655 if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
1658 if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
1661 if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
1664 if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
1667 if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
1670 if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
1673 if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
1676 alps_exit_command_mode(psmouse);
1679 * This sequence changes the output from a 9-byte to an
1680 * 8-byte format. All the same data seems to be present,
1681 * just in a more compact format.
1686 if (ps2_command(ps2dev, ¶m[0], PSMOUSE_CMD_SETRATE) ||
1687 ps2_command(ps2dev, ¶m[1], PSMOUSE_CMD_SETRATE) ||
1688 ps2_command(ps2dev, ¶m[2], PSMOUSE_CMD_SETRATE) ||
1689 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
1692 /* Set rate and enable data reporting */
1694 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
1695 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1696 psmouse_err(psmouse, "Failed to enable data reporting\n");
1704 * Leaving the touchpad in command mode will essentially render
1705 * it unusable until the machine reboots, so exit it here just
1708 alps_exit_command_mode(psmouse);
1712 static int alps_hw_init_dolphin_v1(struct psmouse *psmouse)
1714 struct ps2dev *ps2dev = &psmouse->ps2dev;
1715 unsigned char param[2];
1717 /* This is dolphin "v1" as empirically defined by florin9doi */
1721 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
1722 ps2_command(ps2dev, ¶m[0], PSMOUSE_CMD_SETRATE) ||
1723 ps2_command(ps2dev, ¶m[1], PSMOUSE_CMD_SETRATE))
1729 static void alps_set_defaults(struct alps_data *priv)
1733 priv->flags = ALPS_DUALPOINT;
1740 switch (priv->proto_version) {
1743 priv->hw_init = alps_hw_init_v1_v2;
1744 priv->process_packet = alps_process_packet_v1_v2;
1745 priv->set_abs_params = alps_set_abs_params_st;
1750 priv->hw_init = alps_hw_init_v3;
1751 priv->process_packet = alps_process_packet_v3;
1752 priv->set_abs_params = alps_set_abs_params_mt;
1753 priv->decode_fields = alps_decode_pinnacle;
1754 priv->nibble_commands = alps_v3_nibble_commands;
1755 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
1758 priv->hw_init = alps_hw_init_v4;
1759 priv->process_packet = alps_process_packet_v4;
1760 priv->set_abs_params = alps_set_abs_params_mt;
1761 priv->nibble_commands = alps_v4_nibble_commands;
1762 priv->addr_command = PSMOUSE_CMD_DISABLE;
1765 priv->hw_init = alps_hw_init_dolphin_v1;
1766 priv->process_packet = alps_process_packet_v3;
1767 priv->decode_fields = alps_decode_dolphin;
1768 priv->set_abs_params = alps_set_abs_params_mt;
1769 priv->nibble_commands = alps_v3_nibble_commands;
1770 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
1780 priv->hw_init = alps_hw_init_v6;
1781 priv->process_packet = alps_process_packet_v6;
1782 priv->set_abs_params = alps_set_abs_params_st;
1783 priv->nibble_commands = alps_v6_nibble_commands;
1790 static int alps_match_table(struct psmouse *psmouse, struct alps_data *priv,
1791 unsigned char *e7, unsigned char *ec)
1793 const struct alps_model_info *model;
1796 for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
1797 model = &alps_model_data[i];
1799 if (!memcmp(e7, model->signature, sizeof(model->signature)) &&
1800 (!model->command_mode_resp ||
1801 model->command_mode_resp == ec[2])) {
1803 priv->proto_version = model->proto_version;
1804 alps_set_defaults(priv);
1806 priv->flags = model->flags;
1807 priv->byte0 = model->byte0;
1808 priv->mask0 = model->mask0;
1817 static int alps_identify(struct psmouse *psmouse, struct alps_data *priv)
1819 unsigned char e6[4], e7[4], ec[4];
1822 * First try "E6 report".
1823 * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
1824 * The bits 0-2 of the first byte will be 1s if some buttons are
1827 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
1828 PSMOUSE_CMD_SETSCALE11, e6))
1831 if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100))
1835 * Now get the "E7" and "EC" reports. These will uniquely identify
1836 * most ALPS touchpads.
1838 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
1839 PSMOUSE_CMD_SETSCALE21, e7) ||
1840 alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
1841 PSMOUSE_CMD_RESET_WRAP, ec) ||
1842 alps_exit_command_mode(psmouse))
1845 if (alps_match_table(psmouse, priv, e7, ec) == 0) {
1847 } else if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0x50 &&
1848 ec[0] == 0x73 && ec[1] == 0x01) {
1849 priv->proto_version = ALPS_PROTO_V5;
1850 alps_set_defaults(priv);
1853 } else if (ec[0] == 0x88 && ec[1] == 0x08) {
1854 priv->proto_version = ALPS_PROTO_V3;
1855 alps_set_defaults(priv);
1857 priv->hw_init = alps_hw_init_rushmore_v3;
1858 priv->decode_fields = alps_decode_rushmore;
1862 /* hack to make addr_command, nibble_command available */
1863 psmouse->private = priv;
1865 if (alps_probe_trackstick_v3(psmouse, ALPS_REG_BASE_RUSHMORE))
1866 priv->flags &= ~ALPS_DUALPOINT;
1869 } else if (ec[0] == 0x88 && ec[1] == 0x07 &&
1870 ec[2] >= 0x90 && ec[2] <= 0x9d) {
1871 priv->proto_version = ALPS_PROTO_V3;
1872 alps_set_defaults(priv);
1877 psmouse_info(psmouse,
1878 "Unknown ALPS touchpad: E7=%3ph, EC=%3ph\n", e7, ec);
1883 static int alps_reconnect(struct psmouse *psmouse)
1885 struct alps_data *priv = psmouse->private;
1887 psmouse_reset(psmouse);
1889 if (alps_identify(psmouse, priv) < 0)
1892 return priv->hw_init(psmouse);
1895 static void alps_disconnect(struct psmouse *psmouse)
1897 struct alps_data *priv = psmouse->private;
1899 psmouse_reset(psmouse);
1900 del_timer_sync(&priv->timer);
1901 input_unregister_device(priv->dev2);
1905 static void alps_set_abs_params_st(struct alps_data *priv,
1906 struct input_dev *dev1)
1908 input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
1909 input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
1912 static void alps_set_abs_params_mt(struct alps_data *priv,
1913 struct input_dev *dev1)
1915 set_bit(INPUT_PROP_SEMI_MT, dev1->propbit);
1916 input_mt_init_slots(dev1, 2, 0);
1917 input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
1918 input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
1920 set_bit(BTN_TOOL_DOUBLETAP, dev1->keybit);
1921 set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
1922 set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
1924 input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
1925 input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
1928 int alps_init(struct psmouse *psmouse)
1930 struct alps_data *priv;
1931 struct input_dev *dev1 = psmouse->dev, *dev2;
1933 priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
1934 dev2 = input_allocate_device();
1939 setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
1941 psmouse->private = priv;
1943 psmouse_reset(psmouse);
1945 if (alps_identify(psmouse, priv) < 0)
1948 if (priv->hw_init(psmouse))
1952 * Undo part of setup done for us by psmouse core since touchpad
1953 * is not a relative device.
1955 __clear_bit(EV_REL, dev1->evbit);
1956 __clear_bit(REL_X, dev1->relbit);
1957 __clear_bit(REL_Y, dev1->relbit);
1960 * Now set up our capabilities.
1962 dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
1963 dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
1964 dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
1965 dev1->keybit[BIT_WORD(BTN_LEFT)] |=
1966 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
1968 dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
1970 priv->set_abs_params(priv, dev1);
1971 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
1973 if (priv->flags & ALPS_WHEEL) {
1974 dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
1975 dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
1978 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
1979 dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
1980 dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
1983 if (priv->flags & ALPS_FOUR_BUTTONS) {
1984 dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
1985 dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
1986 dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
1987 dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
1989 dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
1992 snprintf(priv->phys, sizeof(priv->phys), "%s/input1", psmouse->ps2dev.serio->phys);
1993 dev2->phys = priv->phys;
1994 dev2->name = (priv->flags & ALPS_DUALPOINT) ?
1995 "DualPoint Stick" : "ALPS PS/2 Device";
1996 dev2->id.bustype = BUS_I8042;
1997 dev2->id.vendor = 0x0002;
1998 dev2->id.product = PSMOUSE_ALPS;
1999 dev2->id.version = 0x0000;
2000 dev2->dev.parent = &psmouse->ps2dev.serio->dev;
2002 dev2->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
2003 dev2->relbit[BIT_WORD(REL_X)] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
2004 dev2->keybit[BIT_WORD(BTN_LEFT)] =
2005 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_MIDDLE) | BIT_MASK(BTN_RIGHT);
2007 if (input_register_device(priv->dev2))
2010 psmouse->protocol_handler = alps_process_byte;
2011 psmouse->poll = alps_poll;
2012 psmouse->disconnect = alps_disconnect;
2013 psmouse->reconnect = alps_reconnect;
2014 psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
2016 /* We are having trouble resyncing ALPS touchpads so disable it for now */
2017 psmouse->resync_time = 0;
2022 psmouse_reset(psmouse);
2023 input_free_device(dev2);
2025 psmouse->private = NULL;
2029 int alps_detect(struct psmouse *psmouse, bool set_properties)
2031 struct alps_data dummy;
2033 if (alps_identify(psmouse, &dummy) < 0)
2036 if (set_properties) {
2037 psmouse->vendor = "ALPS";
2038 psmouse->name = dummy.flags & ALPS_DUALPOINT ?
2039 "DualPoint TouchPad" : "GlidePoint";
2040 psmouse->model = dummy.proto_version << 8;