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 */
74 #define ALPS_DUALPOINT 0x02 /* touchpad has trackstick */
75 #define ALPS_PASS 0x04 /* device has a pass-through port */
77 #define ALPS_WHEEL 0x08 /* hardware wheel present */
78 #define ALPS_FW_BK_1 0x10 /* front & back buttons present */
79 #define ALPS_FW_BK_2 0x20 /* front & back buttons present */
80 #define ALPS_FOUR_BUTTONS 0x40 /* 4 direction button present */
81 #define ALPS_PS2_INTERLEAVED 0x80 /* 3-byte PS/2 packet interleaved with
84 static const struct alps_model_info alps_model_data[] = {
85 { { 0x32, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* Toshiba Salellite Pro M10 */
86 { { 0x33, 0x02, 0x0a }, 0x00, ALPS_PROTO_V1, 0x88, 0xf8, 0 }, /* UMAX-530T */
87 { { 0x53, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
88 { { 0x53, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
89 { { 0x60, 0x03, 0xc8 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 }, /* HP ze1115 */
90 { { 0x63, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
91 { { 0x63, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
92 { { 0x63, 0x02, 0x28 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 }, /* Fujitsu Siemens S6010 */
93 { { 0x63, 0x02, 0x3c }, 0x00, ALPS_PROTO_V2, 0x8f, 0x8f, ALPS_WHEEL }, /* Toshiba Satellite S2400-103 */
94 { { 0x63, 0x02, 0x50 }, 0x00, ALPS_PROTO_V2, 0xef, 0xef, ALPS_FW_BK_1 }, /* NEC Versa L320 */
95 { { 0x63, 0x02, 0x64 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
96 { { 0x63, 0x03, 0xc8 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* Dell Latitude D800 */
97 { { 0x73, 0x00, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_DUALPOINT }, /* ThinkPad R61 8918-5QG */
98 { { 0x73, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
99 { { 0x73, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 }, /* Ahtec Laptop */
100 { { 0x20, 0x02, 0x0e }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* XXX */
101 { { 0x22, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT },
102 { { 0x22, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT }, /* Dell Latitude D600 */
103 /* Dell Latitude E5500, E6400, E6500, Precision M4400 */
104 { { 0x62, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xcf, 0xcf,
105 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED },
106 { { 0x73, 0x02, 0x50 }, 0x00, ALPS_PROTO_V2, 0xcf, 0xcf, ALPS_FOUR_BUTTONS }, /* Dell Vostro 1400 */
107 { { 0x52, 0x01, 0x14 }, 0x00, ALPS_PROTO_V2, 0xff, 0xff,
108 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED }, /* Toshiba Tecra A11-11L */
109 { { 0x73, 0x02, 0x64 }, 0x8a, ALPS_PROTO_V4, 0x8f, 0x8f, 0 },
112 static void alps_set_abs_params_st(struct alps_data *priv,
113 struct input_dev *dev1);
114 static void alps_set_abs_params_mt(struct alps_data *priv,
115 struct input_dev *dev1);
118 * XXX - this entry is suspicious. First byte has zero lower nibble,
119 * which is what a normal mouse would report. Also, the value 0x0e
120 * isn't valid per PS/2 spec.
123 /* Packet formats are described in Documentation/input/alps.txt */
125 static bool alps_is_valid_first_byte(struct alps_data *priv,
128 return (data & priv->mask0) == priv->byte0;
131 static void alps_report_buttons(struct psmouse *psmouse,
132 struct input_dev *dev1, struct input_dev *dev2,
133 int left, int right, int middle)
135 struct input_dev *dev;
138 * If shared button has already been reported on the
139 * other device (dev2) then this event should be also
140 * sent through that device.
142 dev = test_bit(BTN_LEFT, dev2->key) ? dev2 : dev1;
143 input_report_key(dev, BTN_LEFT, left);
145 dev = test_bit(BTN_RIGHT, dev2->key) ? dev2 : dev1;
146 input_report_key(dev, BTN_RIGHT, right);
148 dev = test_bit(BTN_MIDDLE, dev2->key) ? dev2 : dev1;
149 input_report_key(dev, BTN_MIDDLE, middle);
152 * Sync the _other_ device now, we'll do the first
153 * device later once we report the rest of the events.
158 static void alps_process_packet_v1_v2(struct psmouse *psmouse)
160 struct alps_data *priv = psmouse->private;
161 unsigned char *packet = psmouse->packet;
162 struct input_dev *dev = psmouse->dev;
163 struct input_dev *dev2 = priv->dev2;
164 int x, y, z, ges, fin, left, right, middle;
165 int back = 0, forward = 0;
167 if (priv->proto_version == ALPS_PROTO_V1) {
168 left = packet[2] & 0x10;
169 right = packet[2] & 0x08;
171 x = packet[1] | ((packet[0] & 0x07) << 7);
172 y = packet[4] | ((packet[3] & 0x07) << 7);
175 left = packet[3] & 1;
176 right = packet[3] & 2;
177 middle = packet[3] & 4;
178 x = packet[1] | ((packet[2] & 0x78) << (7 - 3));
179 y = packet[4] | ((packet[3] & 0x70) << (7 - 4));
183 if (priv->flags & ALPS_FW_BK_1) {
184 back = packet[0] & 0x10;
185 forward = packet[2] & 4;
188 if (priv->flags & ALPS_FW_BK_2) {
189 back = packet[3] & 4;
190 forward = packet[2] & 4;
191 if ((middle = forward && back))
198 if ((priv->flags & ALPS_DUALPOINT) && z == 127) {
199 input_report_rel(dev2, REL_X, (x > 383 ? (x - 768) : x));
200 input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y));
202 alps_report_buttons(psmouse, dev2, dev, left, right, middle);
208 alps_report_buttons(psmouse, dev, dev2, left, right, middle);
210 /* Convert hardware tap to a reasonable Z value */
215 * A "tap and drag" operation is reported by the hardware as a transition
216 * from (!fin && ges) to (fin && ges). This should be translated to the
217 * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
219 if (ges && fin && !priv->prev_fin) {
220 input_report_abs(dev, ABS_X, x);
221 input_report_abs(dev, ABS_Y, y);
222 input_report_abs(dev, ABS_PRESSURE, 0);
223 input_report_key(dev, BTN_TOOL_FINGER, 0);
226 priv->prev_fin = fin;
229 input_report_key(dev, BTN_TOUCH, 1);
231 input_report_key(dev, BTN_TOUCH, 0);
234 input_report_abs(dev, ABS_X, x);
235 input_report_abs(dev, ABS_Y, y);
238 input_report_abs(dev, ABS_PRESSURE, z);
239 input_report_key(dev, BTN_TOOL_FINGER, z > 0);
241 if (priv->flags & ALPS_WHEEL)
242 input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
244 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
245 input_report_key(dev, BTN_FORWARD, forward);
246 input_report_key(dev, BTN_BACK, back);
249 if (priv->flags & ALPS_FOUR_BUTTONS) {
250 input_report_key(dev, BTN_0, packet[2] & 4);
251 input_report_key(dev, BTN_1, packet[0] & 0x10);
252 input_report_key(dev, BTN_2, packet[3] & 4);
253 input_report_key(dev, BTN_3, packet[0] & 0x20);
260 * Process bitmap data from v3 and v4 protocols. Returns the number of
261 * fingers detected. A return value of 0 means at least one of the
264 * The bitmaps don't have enough data to track fingers, so this function
265 * only generates points representing a bounding box of all contacts.
266 * These points are returned in x1, y1, x2, and y2 when the return value
269 static int alps_process_bitmap(struct alps_data *priv,
270 unsigned int x_map, unsigned int y_map,
271 int *x1, int *y1, int *x2, int *y2)
273 struct alps_bitmap_point {
278 int fingers_x = 0, fingers_y = 0, fingers;
279 int i, bit, prev_bit;
280 struct alps_bitmap_point x_low = {0,}, x_high = {0,};
281 struct alps_bitmap_point y_low = {0,}, y_high = {0,};
282 struct alps_bitmap_point *point;
284 if (!x_map || !y_map)
287 *x1 = *y1 = *x2 = *y2 = 0;
291 for (i = 0; x_map != 0; i++, x_map >>= 1) {
295 point->start_bit = i;
309 * y bitmap is reversed for what we need (lower positions are in
310 * higher bits), so we process from the top end.
312 y_map = y_map << (sizeof(y_map) * BITS_PER_BYTE - priv->y_bits);
315 for (i = 0; y_map != 0; i++, y_map <<= 1) {
316 bit = y_map & (1 << (sizeof(y_map) * BITS_PER_BYTE - 1));
319 point->start_bit = i;
333 * Fingers can overlap, so we use the maximum count of fingers
334 * on either axis as the finger count.
336 fingers = max(fingers_x, fingers_y);
339 * If total fingers is > 1 but either axis reports only a single
340 * contact, we have overlapping or adjacent fingers. For the
341 * purposes of creating a bounding box, divide the single contact
342 * (roughly) equally between the two points.
345 if (fingers_x == 1) {
346 i = x_low.num_bits / 2;
347 x_low.num_bits = x_low.num_bits - i;
348 x_high.start_bit = x_low.start_bit + i;
349 x_high.num_bits = max(i, 1);
350 } else if (fingers_y == 1) {
351 i = y_low.num_bits / 2;
352 y_low.num_bits = y_low.num_bits - i;
353 y_high.start_bit = y_low.start_bit + i;
354 y_high.num_bits = max(i, 1);
358 *x1 = (priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
359 (2 * (priv->x_bits - 1));
360 *y1 = (priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
361 (2 * (priv->y_bits - 1));
365 (2 * x_high.start_bit + x_high.num_bits - 1)) /
366 (2 * (priv->x_bits - 1));
368 (2 * y_high.start_bit + y_high.num_bits - 1)) /
369 (2 * (priv->y_bits - 1));
375 static void alps_set_slot(struct input_dev *dev, int slot, bool active,
378 input_mt_slot(dev, slot);
379 input_mt_report_slot_state(dev, MT_TOOL_FINGER, active);
381 input_report_abs(dev, ABS_MT_POSITION_X, x);
382 input_report_abs(dev, ABS_MT_POSITION_Y, y);
386 static void alps_report_semi_mt_data(struct input_dev *dev, int num_fingers,
387 int x1, int y1, int x2, int y2)
389 alps_set_slot(dev, 0, num_fingers != 0, x1, y1);
390 alps_set_slot(dev, 1, num_fingers == 2, x2, y2);
393 static void alps_process_trackstick_packet_v3(struct psmouse *psmouse)
395 struct alps_data *priv = psmouse->private;
396 unsigned char *packet = psmouse->packet;
397 struct input_dev *dev = priv->dev2;
398 int x, y, z, left, right, middle;
400 /* Sanity check packet */
401 if (!(packet[0] & 0x40)) {
402 psmouse_dbg(psmouse, "Bad trackstick packet, discarding\n");
407 * There's a special packet that seems to indicate the end
408 * of a stream of trackstick data. Filter these out.
410 if (packet[1] == 0x7f && packet[2] == 0x7f && packet[4] == 0x7f)
413 x = (s8)(((packet[0] & 0x20) << 2) | (packet[1] & 0x7f));
414 y = (s8)(((packet[0] & 0x10) << 3) | (packet[2] & 0x7f));
415 z = (packet[4] & 0x7c) >> 2;
418 * The x and y values tend to be quite large, and when used
419 * alone the trackstick is difficult to use. Scale them down
425 input_report_rel(dev, REL_X, x);
426 input_report_rel(dev, REL_Y, -y);
429 * Most ALPS models report the trackstick buttons in the touchpad
430 * packets, but a few report them here. No reliable way has been
431 * found to differentiate between the models upfront, so we enable
432 * the quirk in response to seeing a button press in the trackstick
435 left = packet[3] & 0x01;
436 right = packet[3] & 0x02;
437 middle = packet[3] & 0x04;
439 if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) &&
440 (left || right || middle))
441 priv->quirks |= ALPS_QUIRK_TRACKSTICK_BUTTONS;
443 if (priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) {
444 input_report_key(dev, BTN_LEFT, left);
445 input_report_key(dev, BTN_RIGHT, right);
446 input_report_key(dev, BTN_MIDDLE, middle);
453 static void alps_decode_buttons_v3(struct alps_fields *f, unsigned char *p)
455 f->left = !!(p[3] & 0x01);
456 f->right = !!(p[3] & 0x02);
457 f->middle = !!(p[3] & 0x04);
459 f->ts_left = !!(p[3] & 0x10);
460 f->ts_right = !!(p[3] & 0x20);
461 f->ts_middle = !!(p[3] & 0x40);
464 static void alps_decode_pinnacle(struct alps_fields *f, unsigned char *p)
466 f->first_mp = !!(p[4] & 0x40);
467 f->is_mp = !!(p[0] & 0x40);
469 f->fingers = (p[5] & 0x3) + 1;
470 f->x_map = ((p[4] & 0x7e) << 8) |
471 ((p[1] & 0x7f) << 2) |
472 ((p[0] & 0x30) >> 4);
473 f->y_map = ((p[3] & 0x70) << 4) |
474 ((p[2] & 0x7f) << 1) |
477 f->x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
478 ((p[0] & 0x30) >> 4);
479 f->y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
482 alps_decode_buttons_v3(f, p);
485 static void alps_decode_rushmore(struct alps_fields *f, unsigned char *p)
487 alps_decode_pinnacle(f, p);
489 f->x_map |= (p[5] & 0x10) << 11;
490 f->y_map |= (p[5] & 0x20) << 6;
493 static void alps_decode_dolphin(struct alps_fields *f, unsigned char *p)
495 f->first_mp = !!(p[0] & 0x02);
496 f->is_mp = !!(p[0] & 0x20);
498 f->fingers = ((p[0] & 0x6) >> 1 |
500 f->x_map = ((p[2] & 0x60) >> 5) |
501 ((p[4] & 0x7f) << 2) |
502 ((p[5] & 0x7f) << 9) |
503 ((p[3] & 0x07) << 16) |
504 ((p[3] & 0x70) << 15) |
505 ((p[0] & 0x01) << 22);
506 f->y_map = (p[1] & 0x7f) |
507 ((p[2] & 0x1f) << 7);
509 f->x = ((p[1] & 0x7f) | ((p[4] & 0x0f) << 7));
510 f->y = ((p[2] & 0x7f) | ((p[4] & 0xf0) << 3));
511 f->z = (p[0] & 4) ? 0 : p[5] & 0x7f;
513 alps_decode_buttons_v3(f, p);
516 static void alps_process_touchpad_packet_v3(struct psmouse *psmouse)
518 struct alps_data *priv = psmouse->private;
519 unsigned char *packet = psmouse->packet;
520 struct input_dev *dev = psmouse->dev;
521 struct input_dev *dev2 = priv->dev2;
522 int x1 = 0, y1 = 0, x2 = 0, y2 = 0;
523 int fingers = 0, bmap_fingers;
524 struct alps_fields f;
526 priv->decode_fields(&f, packet);
529 * There's no single feature of touchpad position and bitmap packets
530 * that can be used to distinguish between them. We rely on the fact
531 * that a bitmap packet should always follow a position packet with
532 * bit 6 of packet[4] set.
534 if (priv->multi_packet) {
536 * Sometimes a position packet will indicate a multi-packet
537 * sequence, but then what follows is another position
538 * packet. Check for this, and when it happens process the
539 * position packet as usual.
543 bmap_fingers = alps_process_bitmap(priv,
548 * We shouldn't report more than one finger if
549 * we don't have two coordinates.
551 if (fingers > 1 && bmap_fingers < 2)
552 fingers = bmap_fingers;
554 /* Now process position packet */
555 priv->decode_fields(&f, priv->multi_data);
557 priv->multi_packet = 0;
562 * Bit 6 of byte 0 is not usually set in position packets. The only
563 * times it seems to be set is in situations where the data is
564 * suspect anyway, e.g. a palm resting flat on the touchpad. Given
565 * this combined with the fact that this bit is useful for filtering
566 * out misidentified bitmap packets, we reject anything with this
572 if (!priv->multi_packet && f.first_mp) {
573 priv->multi_packet = 1;
574 memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
578 priv->multi_packet = 0;
581 * Sometimes the hardware sends a single packet with z = 0
582 * in the middle of a stream. Real releases generate packets
583 * with x, y, and z all zero, so these seem to be flukes.
586 if (f.x && f.y && !f.z)
590 * If we don't have MT data or the bitmaps were empty, we have
591 * to rely on ST data.
596 fingers = f.z > 0 ? 1 : 0;
600 input_report_key(dev, BTN_TOUCH, 1);
602 input_report_key(dev, BTN_TOUCH, 0);
604 alps_report_semi_mt_data(dev, fingers, x1, y1, x2, y2);
606 input_mt_report_finger_count(dev, fingers);
608 input_report_key(dev, BTN_LEFT, f.left);
609 input_report_key(dev, BTN_RIGHT, f.right);
610 input_report_key(dev, BTN_MIDDLE, f.middle);
613 input_report_abs(dev, ABS_X, f.x);
614 input_report_abs(dev, ABS_Y, f.y);
616 input_report_abs(dev, ABS_PRESSURE, f.z);
620 if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) {
621 input_report_key(dev2, BTN_LEFT, f.ts_left);
622 input_report_key(dev2, BTN_RIGHT, f.ts_right);
623 input_report_key(dev2, BTN_MIDDLE, f.ts_middle);
628 static void alps_process_packet_v3(struct psmouse *psmouse)
630 unsigned char *packet = psmouse->packet;
633 * v3 protocol packets come in three types, two representing
634 * touchpad data and one representing trackstick data.
635 * Trackstick packets seem to be distinguished by always
636 * having 0x3f in the last byte. This value has never been
637 * observed in the last byte of either of the other types
640 if (packet[5] == 0x3f) {
641 alps_process_trackstick_packet_v3(psmouse);
645 alps_process_touchpad_packet_v3(psmouse);
648 static void alps_process_packet_v4(struct psmouse *psmouse)
650 struct alps_data *priv = psmouse->private;
651 unsigned char *packet = psmouse->packet;
652 struct input_dev *dev = psmouse->dev;
658 unsigned int x_bitmap, y_bitmap;
661 * v4 has a 6-byte encoding for bitmap data, but this data is
662 * broken up between 3 normal packets. Use priv->multi_packet to
663 * track our position in the bitmap packet.
665 if (packet[6] & 0x40) {
666 /* sync, reset position */
667 priv->multi_packet = 0;
670 if (WARN_ON_ONCE(priv->multi_packet > 2))
673 offset = 2 * priv->multi_packet;
674 priv->multi_data[offset] = packet[6];
675 priv->multi_data[offset + 1] = packet[7];
677 if (++priv->multi_packet > 2) {
678 priv->multi_packet = 0;
680 x_bitmap = ((priv->multi_data[2] & 0x1f) << 10) |
681 ((priv->multi_data[3] & 0x60) << 3) |
682 ((priv->multi_data[0] & 0x3f) << 2) |
683 ((priv->multi_data[1] & 0x60) >> 5);
684 y_bitmap = ((priv->multi_data[5] & 0x01) << 10) |
685 ((priv->multi_data[3] & 0x1f) << 5) |
686 (priv->multi_data[1] & 0x1f);
688 fingers = alps_process_bitmap(priv, x_bitmap, y_bitmap,
692 priv->fingers = fingers;
699 left = packet[4] & 0x01;
700 right = packet[4] & 0x02;
702 x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) |
703 ((packet[0] & 0x30) >> 4);
704 y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f);
705 z = packet[5] & 0x7f;
708 * If there were no contacts in the bitmap, use ST
709 * points in MT reports.
710 * If there were two contacts or more, report MT data.
712 if (priv->fingers < 2) {
715 fingers = z > 0 ? 1 : 0;
717 fingers = priv->fingers;
725 input_report_key(dev, BTN_TOUCH, 1);
727 input_report_key(dev, BTN_TOUCH, 0);
729 alps_report_semi_mt_data(dev, fingers, x1, y1, x2, y2);
731 input_mt_report_finger_count(dev, fingers);
733 input_report_key(dev, BTN_LEFT, left);
734 input_report_key(dev, BTN_RIGHT, right);
737 input_report_abs(dev, ABS_X, x);
738 input_report_abs(dev, ABS_Y, y);
740 input_report_abs(dev, ABS_PRESSURE, z);
745 static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
746 unsigned char packet[],
749 struct alps_data *priv = psmouse->private;
750 struct input_dev *dev2 = priv->dev2;
753 alps_report_buttons(psmouse, dev2, psmouse->dev,
754 packet[0] & 1, packet[0] & 2, packet[0] & 4);
756 input_report_rel(dev2, REL_X,
757 packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
758 input_report_rel(dev2, REL_Y,
759 packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
764 static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
766 struct alps_data *priv = psmouse->private;
768 if (psmouse->pktcnt < 6)
769 return PSMOUSE_GOOD_DATA;
771 if (psmouse->pktcnt == 6) {
773 * Start a timer to flush the packet if it ends up last
774 * 6-byte packet in the stream. Timer needs to fire
775 * psmouse core times out itself. 20 ms should be enough
776 * to decide if we are getting more data or not.
778 mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
779 return PSMOUSE_GOOD_DATA;
782 del_timer(&priv->timer);
784 if (psmouse->packet[6] & 0x80) {
787 * Highest bit is set - that means we either had
788 * complete ALPS packet and this is start of the
789 * next packet or we got garbage.
792 if (((psmouse->packet[3] |
794 psmouse->packet[5]) & 0x80) ||
795 (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) {
797 "refusing packet %4ph (suspected interleaved ps/2)\n",
798 psmouse->packet + 3);
799 return PSMOUSE_BAD_DATA;
802 priv->process_packet(psmouse);
804 /* Continue with the next packet */
805 psmouse->packet[0] = psmouse->packet[6];
811 * High bit is 0 - that means that we indeed got a PS/2
812 * packet in the middle of ALPS packet.
814 * There is also possibility that we got 6-byte ALPS
815 * packet followed by 3-byte packet from trackpoint. We
816 * can not distinguish between these 2 scenarios but
817 * because the latter is unlikely to happen in course of
818 * normal operation (user would need to press all
819 * buttons on the pad and start moving trackpoint
820 * without touching the pad surface) we assume former.
821 * Even if we are wrong the wost thing that would happen
822 * the cursor would jump but we should not get protocol
823 * de-synchronization.
826 alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
830 * Continue with the standard ALPS protocol handling,
831 * but make sure we won't process it as an interleaved
832 * packet again, which may happen if all buttons are
833 * pressed. To avoid this let's reset the 4th bit which
836 psmouse->packet[3] = psmouse->packet[6] & 0xf7;
840 return PSMOUSE_GOOD_DATA;
843 static void alps_flush_packet(unsigned long data)
845 struct psmouse *psmouse = (struct psmouse *)data;
846 struct alps_data *priv = psmouse->private;
848 serio_pause_rx(psmouse->ps2dev.serio);
850 if (psmouse->pktcnt == psmouse->pktsize) {
853 * We did not any more data in reasonable amount of time.
854 * Validate the last 3 bytes and process as a standard
857 if ((psmouse->packet[3] |
859 psmouse->packet[5]) & 0x80) {
861 "refusing packet %3ph (suspected interleaved ps/2)\n",
862 psmouse->packet + 3);
864 priv->process_packet(psmouse);
869 serio_continue_rx(psmouse->ps2dev.serio);
872 static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
874 struct alps_data *priv = psmouse->private;
877 * Check if we are dealing with a bare PS/2 packet, presumably from
878 * a device connected to the external PS/2 port. Because bare PS/2
879 * protocol does not have enough constant bits to self-synchronize
880 * properly we only do this if the device is fully synchronized.
882 if (!psmouse->out_of_sync_cnt && (psmouse->packet[0] & 0xc8) == 0x08) {
883 if (psmouse->pktcnt == 3) {
884 alps_report_bare_ps2_packet(psmouse, psmouse->packet,
886 return PSMOUSE_FULL_PACKET;
888 return PSMOUSE_GOOD_DATA;
891 /* Check for PS/2 packet stuffed in the middle of ALPS packet. */
893 if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
894 psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
895 return alps_handle_interleaved_ps2(psmouse);
898 if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
900 "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
901 psmouse->packet[0], priv->mask0, priv->byte0);
902 return PSMOUSE_BAD_DATA;
905 /* Bytes 2 - pktsize should have 0 in the highest bit */
906 if (priv->proto_version != ALPS_PROTO_V5 &&
907 psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
908 (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
909 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
911 psmouse->packet[psmouse->pktcnt - 1]);
912 return PSMOUSE_BAD_DATA;
915 if (psmouse->pktcnt == psmouse->pktsize) {
916 priv->process_packet(psmouse);
917 return PSMOUSE_FULL_PACKET;
920 return PSMOUSE_GOOD_DATA;
923 static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
925 struct ps2dev *ps2dev = &psmouse->ps2dev;
926 struct alps_data *priv = psmouse->private;
928 unsigned char *param;
929 unsigned char dummy[4];
931 BUG_ON(nibble > 0xf);
933 command = priv->nibble_commands[nibble].command;
934 param = (command & 0x0f00) ?
935 dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
937 if (ps2_command(ps2dev, param, command))
943 static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
945 struct ps2dev *ps2dev = &psmouse->ps2dev;
946 struct alps_data *priv = psmouse->private;
949 if (ps2_command(ps2dev, NULL, priv->addr_command))
952 for (i = 12; i >= 0; i -= 4) {
953 nibble = (addr >> i) & 0xf;
954 if (alps_command_mode_send_nibble(psmouse, nibble))
961 static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
963 struct ps2dev *ps2dev = &psmouse->ps2dev;
964 unsigned char param[4];
966 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
970 * The address being read is returned in the first two bytes
971 * of the result. Check that this address matches the expected
974 if (addr != ((param[0] << 8) | param[1]))
980 static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
982 if (alps_command_mode_set_addr(psmouse, addr))
984 return __alps_command_mode_read_reg(psmouse, addr);
987 static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
989 if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
991 if (alps_command_mode_send_nibble(psmouse, value & 0xf))
996 static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
999 if (alps_command_mode_set_addr(psmouse, addr))
1001 return __alps_command_mode_write_reg(psmouse, value);
1004 static int alps_rpt_cmd(struct psmouse *psmouse, int init_command,
1005 int repeated_command, unsigned char *param)
1007 struct ps2dev *ps2dev = &psmouse->ps2dev;
1010 if (init_command && ps2_command(ps2dev, param, init_command))
1013 if (ps2_command(ps2dev, NULL, repeated_command) ||
1014 ps2_command(ps2dev, NULL, repeated_command) ||
1015 ps2_command(ps2dev, NULL, repeated_command))
1018 param[0] = param[1] = param[2] = 0xff;
1019 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1022 psmouse_dbg(psmouse, "%2.2X report: %3ph\n",
1023 repeated_command, param);
1027 static int alps_enter_command_mode(struct psmouse *psmouse)
1029 unsigned char param[4];
1031 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) {
1032 psmouse_err(psmouse, "failed to enter command mode\n");
1036 if ((param[0] != 0x88 || (param[1] != 0x07 && param[1] != 0x08)) &&
1038 psmouse_dbg(psmouse,
1039 "unknown response while entering command mode\n");
1045 static inline int alps_exit_command_mode(struct psmouse *psmouse)
1047 struct ps2dev *ps2dev = &psmouse->ps2dev;
1048 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
1054 * For DualPoint devices select the device that should respond to
1055 * subsequent commands. It looks like glidepad is behind stickpointer,
1056 * I'd thought it would be other way around...
1058 static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
1060 struct ps2dev *ps2dev = &psmouse->ps2dev;
1061 int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
1063 if (ps2_command(ps2dev, NULL, cmd) ||
1064 ps2_command(ps2dev, NULL, cmd) ||
1065 ps2_command(ps2dev, NULL, cmd) ||
1066 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1069 /* we may get 3 more bytes, just ignore them */
1070 ps2_drain(ps2dev, 3, 100);
1075 static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
1077 struct ps2dev *ps2dev = &psmouse->ps2dev;
1079 /* Try ALPS magic knock - 4 disable before enable */
1080 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1081 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1082 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1083 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1084 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
1088 * Switch mouse to poll (remote) mode so motion data will not
1091 return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
1094 static int alps_get_status(struct psmouse *psmouse, char *param)
1096 /* Get status: 0xF5 0xF5 0xF5 0xE9 */
1097 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param))
1104 * Turn touchpad tapping on or off. The sequences are:
1105 * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
1106 * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
1107 * My guess that 0xE9 (GetInfo) is here as a sync point.
1108 * For models that also have stickpointer (DualPoints) its tapping
1109 * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
1110 * we don't fiddle with it.
1112 static int alps_tap_mode(struct psmouse *psmouse, int enable)
1114 struct ps2dev *ps2dev = &psmouse->ps2dev;
1115 int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
1116 unsigned char tap_arg = enable ? 0x0A : 0x00;
1117 unsigned char param[4];
1119 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
1120 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1121 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1122 ps2_command(ps2dev, &tap_arg, cmd))
1125 if (alps_get_status(psmouse, param))
1132 * alps_poll() - poll the touchpad for current motion packet.
1135 static int alps_poll(struct psmouse *psmouse)
1137 struct alps_data *priv = psmouse->private;
1138 unsigned char buf[sizeof(psmouse->packet)];
1141 if (priv->flags & ALPS_PASS)
1142 alps_passthrough_mode_v2(psmouse, true);
1144 poll_failed = ps2_command(&psmouse->ps2dev, buf,
1145 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
1147 if (priv->flags & ALPS_PASS)
1148 alps_passthrough_mode_v2(psmouse, false);
1150 if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
1153 if ((psmouse->badbyte & 0xc8) == 0x08) {
1155 * Poll the track stick ...
1157 if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
1161 memcpy(psmouse->packet, buf, sizeof(buf));
1165 static int alps_hw_init_v1_v2(struct psmouse *psmouse)
1167 struct alps_data *priv = psmouse->private;
1169 if ((priv->flags & ALPS_PASS) &&
1170 alps_passthrough_mode_v2(psmouse, true)) {
1174 if (alps_tap_mode(psmouse, true)) {
1175 psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
1179 if (alps_absolute_mode_v1_v2(psmouse)) {
1180 psmouse_err(psmouse, "Failed to enable absolute mode\n");
1184 if ((priv->flags & ALPS_PASS) &&
1185 alps_passthrough_mode_v2(psmouse, false)) {
1189 /* ALPS needs stream mode, otherwise it won't report any data */
1190 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
1191 psmouse_err(psmouse, "Failed to enable stream mode\n");
1199 * Enable or disable passthrough mode to the trackstick.
1201 static int alps_passthrough_mode_v3(struct psmouse *psmouse,
1202 int reg_base, bool enable)
1204 int reg_val, ret = -1;
1206 if (alps_enter_command_mode(psmouse))
1209 reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x0008);
1218 ret = __alps_command_mode_write_reg(psmouse, reg_val);
1221 if (alps_exit_command_mode(psmouse))
1226 /* Must be in command mode when calling this function */
1227 static int alps_absolute_mode_v3(struct psmouse *psmouse)
1231 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
1236 if (__alps_command_mode_write_reg(psmouse, reg_val))
1242 static int alps_probe_trackstick_v3(struct psmouse *psmouse, int reg_base)
1244 int ret = -EIO, reg_val;
1246 if (alps_enter_command_mode(psmouse))
1249 reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x08);
1253 /* bit 7: trackstick is present */
1254 ret = reg_val & 0x80 ? 0 : -ENODEV;
1257 alps_exit_command_mode(psmouse);
1261 static int alps_setup_trackstick_v3(struct psmouse *psmouse, int reg_base)
1263 struct ps2dev *ps2dev = &psmouse->ps2dev;
1265 unsigned char param[4];
1267 if (alps_passthrough_mode_v3(psmouse, reg_base, true))
1271 * E7 report for the trackstick
1273 * There have been reports of failures to seem to trace back
1274 * to the above trackstick check failing. When these occur
1275 * this E7 report fails, so when that happens we continue
1276 * with the assumption that there isn't a trackstick after
1279 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_SETSCALE21, param)) {
1280 psmouse_warn(psmouse, "trackstick E7 report failed\n");
1283 psmouse_dbg(psmouse, "trackstick E7 report: %3ph\n", param);
1286 * Not sure what this does, but it is absolutely
1287 * essential. Without it, the touchpad does not
1288 * work at all and the trackstick just emits normal
1291 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1292 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1293 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1294 alps_command_mode_send_nibble(psmouse, 0x9) ||
1295 alps_command_mode_send_nibble(psmouse, 0x4)) {
1296 psmouse_err(psmouse,
1297 "Error sending magic E6 sequence\n");
1303 * This ensures the trackstick packets are in the format
1304 * supported by this driver. If bit 1 isn't set the packet
1305 * format is different.
1307 if (alps_enter_command_mode(psmouse) ||
1308 alps_command_mode_write_reg(psmouse,
1309 reg_base + 0x08, 0x82) ||
1310 alps_exit_command_mode(psmouse))
1315 if (alps_passthrough_mode_v3(psmouse, reg_base, false))
1321 static int alps_hw_init_v3(struct psmouse *psmouse)
1323 struct ps2dev *ps2dev = &psmouse->ps2dev;
1325 unsigned char param[4];
1327 reg_val = alps_probe_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE);
1328 if (reg_val == -EIO)
1332 alps_setup_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE) == -EIO)
1335 if (alps_enter_command_mode(psmouse) ||
1336 alps_absolute_mode_v3(psmouse)) {
1337 psmouse_err(psmouse, "Failed to enter absolute mode\n");
1341 reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
1344 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
1347 reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
1350 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
1353 if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
1355 if (__alps_command_mode_write_reg(psmouse, 0x04))
1358 if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
1360 if (__alps_command_mode_write_reg(psmouse, 0x03))
1363 if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
1365 if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
1368 if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
1370 if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
1373 alps_exit_command_mode(psmouse);
1375 /* Set rate and enable data reporting */
1377 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
1378 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1379 psmouse_err(psmouse, "Failed to enable data reporting\n");
1387 * Leaving the touchpad in command mode will essentially render
1388 * it unusable until the machine reboots, so exit it here just
1391 alps_exit_command_mode(psmouse);
1395 static int alps_hw_init_rushmore_v3(struct psmouse *psmouse)
1397 struct alps_data *priv = psmouse->private;
1398 struct ps2dev *ps2dev = &psmouse->ps2dev;
1399 int reg_val, ret = -1;
1401 if (priv->flags & ALPS_DUALPOINT) {
1402 reg_val = alps_setup_trackstick_v3(psmouse,
1403 ALPS_REG_BASE_RUSHMORE);
1404 if (reg_val == -EIO)
1406 if (reg_val == -ENODEV)
1407 priv->flags &= ~ALPS_DUALPOINT;
1410 if (alps_enter_command_mode(psmouse) ||
1411 alps_command_mode_read_reg(psmouse, 0xc2d9) == -1 ||
1412 alps_command_mode_write_reg(psmouse, 0xc2cb, 0x00))
1415 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c6);
1418 if (__alps_command_mode_write_reg(psmouse, reg_val & 0xfd))
1421 if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
1424 /* enter absolute mode */
1425 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
1428 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
1431 alps_exit_command_mode(psmouse);
1432 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
1435 alps_exit_command_mode(psmouse);
1439 /* Must be in command mode when calling this function */
1440 static int alps_absolute_mode_v4(struct psmouse *psmouse)
1444 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
1449 if (__alps_command_mode_write_reg(psmouse, reg_val))
1455 static int alps_hw_init_v4(struct psmouse *psmouse)
1457 struct ps2dev *ps2dev = &psmouse->ps2dev;
1458 unsigned char param[4];
1460 if (alps_enter_command_mode(psmouse))
1463 if (alps_absolute_mode_v4(psmouse)) {
1464 psmouse_err(psmouse, "Failed to enter absolute mode\n");
1468 if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
1471 if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
1474 if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
1477 if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
1480 if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
1483 if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
1486 if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
1489 if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
1492 alps_exit_command_mode(psmouse);
1495 * This sequence changes the output from a 9-byte to an
1496 * 8-byte format. All the same data seems to be present,
1497 * just in a more compact format.
1502 if (ps2_command(ps2dev, ¶m[0], PSMOUSE_CMD_SETRATE) ||
1503 ps2_command(ps2dev, ¶m[1], PSMOUSE_CMD_SETRATE) ||
1504 ps2_command(ps2dev, ¶m[2], PSMOUSE_CMD_SETRATE) ||
1505 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
1508 /* Set rate and enable data reporting */
1510 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
1511 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1512 psmouse_err(psmouse, "Failed to enable data reporting\n");
1520 * Leaving the touchpad in command mode will essentially render
1521 * it unusable until the machine reboots, so exit it here just
1524 alps_exit_command_mode(psmouse);
1528 static int alps_hw_init_dolphin_v1(struct psmouse *psmouse)
1530 struct ps2dev *ps2dev = &psmouse->ps2dev;
1531 unsigned char param[2];
1533 /* This is dolphin "v1" as empirically defined by florin9doi */
1537 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
1538 ps2_command(ps2dev, ¶m[0], PSMOUSE_CMD_SETRATE) ||
1539 ps2_command(ps2dev, ¶m[1], PSMOUSE_CMD_SETRATE))
1545 static void alps_set_defaults(struct alps_data *priv)
1549 priv->flags = ALPS_DUALPOINT;
1556 switch (priv->proto_version) {
1559 priv->hw_init = alps_hw_init_v1_v2;
1560 priv->process_packet = alps_process_packet_v1_v2;
1561 priv->set_abs_params = alps_set_abs_params_st;
1564 priv->hw_init = alps_hw_init_v3;
1565 priv->process_packet = alps_process_packet_v3;
1566 priv->set_abs_params = alps_set_abs_params_mt;
1567 priv->decode_fields = alps_decode_pinnacle;
1568 priv->nibble_commands = alps_v3_nibble_commands;
1569 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
1572 priv->hw_init = alps_hw_init_v4;
1573 priv->process_packet = alps_process_packet_v4;
1574 priv->set_abs_params = alps_set_abs_params_mt;
1575 priv->nibble_commands = alps_v4_nibble_commands;
1576 priv->addr_command = PSMOUSE_CMD_DISABLE;
1579 priv->hw_init = alps_hw_init_dolphin_v1;
1580 priv->process_packet = alps_process_packet_v3;
1581 priv->decode_fields = alps_decode_dolphin;
1582 priv->set_abs_params = alps_set_abs_params_mt;
1583 priv->nibble_commands = alps_v3_nibble_commands;
1584 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
1596 static int alps_match_table(struct psmouse *psmouse, struct alps_data *priv,
1597 unsigned char *e7, unsigned char *ec)
1599 const struct alps_model_info *model;
1602 for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
1603 model = &alps_model_data[i];
1605 if (!memcmp(e7, model->signature, sizeof(model->signature)) &&
1606 (!model->command_mode_resp ||
1607 model->command_mode_resp == ec[2])) {
1609 priv->proto_version = model->proto_version;
1610 alps_set_defaults(priv);
1612 priv->flags = model->flags;
1613 priv->byte0 = model->byte0;
1614 priv->mask0 = model->mask0;
1623 static int alps_identify(struct psmouse *psmouse, struct alps_data *priv)
1625 unsigned char e6[4], e7[4], ec[4];
1628 * First try "E6 report".
1629 * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
1630 * The bits 0-2 of the first byte will be 1s if some buttons are
1633 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
1634 PSMOUSE_CMD_SETSCALE11, e6))
1637 if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100))
1641 * Now get the "E7" and "EC" reports. These will uniquely identify
1642 * most ALPS touchpads.
1644 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
1645 PSMOUSE_CMD_SETSCALE21, e7) ||
1646 alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
1647 PSMOUSE_CMD_RESET_WRAP, ec) ||
1648 alps_exit_command_mode(psmouse))
1651 if (alps_match_table(psmouse, priv, e7, ec) == 0) {
1653 } else if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0x50 &&
1654 ec[0] == 0x73 && ec[1] == 0x01) {
1655 priv->proto_version = ALPS_PROTO_V5;
1656 alps_set_defaults(priv);
1659 } else if (ec[0] == 0x88 && ec[1] == 0x08) {
1660 priv->proto_version = ALPS_PROTO_V3;
1661 alps_set_defaults(priv);
1663 priv->hw_init = alps_hw_init_rushmore_v3;
1664 priv->decode_fields = alps_decode_rushmore;
1668 /* hack to make addr_command, nibble_command available */
1669 psmouse->private = priv;
1671 if (alps_probe_trackstick_v3(psmouse, ALPS_REG_BASE_RUSHMORE))
1672 priv->flags &= ~ALPS_DUALPOINT;
1675 } else if (ec[0] == 0x88 && ec[1] == 0x07 &&
1676 ec[2] >= 0x90 && ec[2] <= 0x9d) {
1677 priv->proto_version = ALPS_PROTO_V3;
1678 alps_set_defaults(priv);
1683 psmouse_info(psmouse,
1684 "Unknown ALPS touchpad: E7=%3ph, EC=%3ph\n", e7, ec);
1689 static int alps_reconnect(struct psmouse *psmouse)
1691 struct alps_data *priv = psmouse->private;
1693 psmouse_reset(psmouse);
1695 if (alps_identify(psmouse, priv) < 0)
1698 return priv->hw_init(psmouse);
1701 static void alps_disconnect(struct psmouse *psmouse)
1703 struct alps_data *priv = psmouse->private;
1705 psmouse_reset(psmouse);
1706 del_timer_sync(&priv->timer);
1707 input_unregister_device(priv->dev2);
1711 static void alps_set_abs_params_st(struct alps_data *priv,
1712 struct input_dev *dev1)
1714 input_set_abs_params(dev1, ABS_X, 0, 1023, 0, 0);
1715 input_set_abs_params(dev1, ABS_Y, 0, 767, 0, 0);
1718 static void alps_set_abs_params_mt(struct alps_data *priv,
1719 struct input_dev *dev1)
1721 set_bit(INPUT_PROP_SEMI_MT, dev1->propbit);
1722 input_mt_init_slots(dev1, 2, 0);
1723 input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
1724 input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
1726 set_bit(BTN_TOOL_DOUBLETAP, dev1->keybit);
1727 set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
1728 set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
1730 input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
1731 input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
1734 int alps_init(struct psmouse *psmouse)
1736 struct alps_data *priv;
1737 struct input_dev *dev1 = psmouse->dev, *dev2;
1739 priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
1740 dev2 = input_allocate_device();
1745 setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
1747 psmouse->private = priv;
1749 psmouse_reset(psmouse);
1751 if (alps_identify(psmouse, priv) < 0)
1754 if (priv->hw_init(psmouse))
1758 * Undo part of setup done for us by psmouse core since touchpad
1759 * is not a relative device.
1761 __clear_bit(EV_REL, dev1->evbit);
1762 __clear_bit(REL_X, dev1->relbit);
1763 __clear_bit(REL_Y, dev1->relbit);
1766 * Now set up our capabilities.
1768 dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
1769 dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
1770 dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
1771 dev1->keybit[BIT_WORD(BTN_LEFT)] |=
1772 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
1774 dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
1776 priv->set_abs_params(priv, dev1);
1777 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
1779 if (priv->flags & ALPS_WHEEL) {
1780 dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
1781 dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
1784 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
1785 dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
1786 dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
1789 if (priv->flags & ALPS_FOUR_BUTTONS) {
1790 dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
1791 dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
1792 dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
1793 dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
1795 dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
1798 snprintf(priv->phys, sizeof(priv->phys), "%s/input1", psmouse->ps2dev.serio->phys);
1799 dev2->phys = priv->phys;
1800 dev2->name = (priv->flags & ALPS_DUALPOINT) ?
1801 "DualPoint Stick" : "PS/2 Mouse";
1802 dev2->id.bustype = BUS_I8042;
1803 dev2->id.vendor = 0x0002;
1804 dev2->id.product = PSMOUSE_ALPS;
1805 dev2->id.version = 0x0000;
1806 dev2->dev.parent = &psmouse->ps2dev.serio->dev;
1808 dev2->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
1809 dev2->relbit[BIT_WORD(REL_X)] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
1810 dev2->keybit[BIT_WORD(BTN_LEFT)] =
1811 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_MIDDLE) | BIT_MASK(BTN_RIGHT);
1813 if (input_register_device(priv->dev2))
1816 psmouse->protocol_handler = alps_process_byte;
1817 psmouse->poll = alps_poll;
1818 psmouse->disconnect = alps_disconnect;
1819 psmouse->reconnect = alps_reconnect;
1820 psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
1822 /* We are having trouble resyncing ALPS touchpads so disable it for now */
1823 psmouse->resync_time = 0;
1825 /* Allow 2 invalid packets without resetting device */
1826 psmouse->resetafter = psmouse->pktsize * 2;
1831 psmouse_reset(psmouse);
1832 input_free_device(dev2);
1834 psmouse->private = NULL;
1838 int alps_detect(struct psmouse *psmouse, bool set_properties)
1840 struct alps_data dummy;
1842 if (alps_identify(psmouse, &dummy) < 0)
1845 if (set_properties) {
1846 psmouse->vendor = "ALPS";
1847 psmouse->name = dummy.flags & ALPS_DUALPOINT ?
1848 "DualPoint TouchPad" : "GlidePoint";
1849 psmouse->model = dummy.proto_version << 8;