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 */
102 #define ALPS_IS_RUSHMORE 0x100 /* device is a rushmore */
103 #define ALPS_BUTTONPAD 0x200 /* device is a clickpad */
105 static const struct alps_model_info alps_model_data[] = {
106 { { 0x32, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* Toshiba Salellite Pro M10 */
107 { { 0x33, 0x02, 0x0a }, 0x00, ALPS_PROTO_V1, 0x88, 0xf8, 0 }, /* UMAX-530T */
108 { { 0x53, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
109 { { 0x53, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
110 { { 0x60, 0x03, 0xc8 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 }, /* HP ze1115 */
111 { { 0x63, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
112 { { 0x63, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
113 { { 0x63, 0x02, 0x28 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 }, /* Fujitsu Siemens S6010 */
114 { { 0x63, 0x02, 0x3c }, 0x00, ALPS_PROTO_V2, 0x8f, 0x8f, ALPS_WHEEL }, /* Toshiba Satellite S2400-103 */
115 { { 0x63, 0x02, 0x50 }, 0x00, ALPS_PROTO_V2, 0xef, 0xef, ALPS_FW_BK_1 }, /* NEC Versa L320 */
116 { { 0x63, 0x02, 0x64 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
117 { { 0x63, 0x03, 0xc8 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* Dell Latitude D800 */
118 { { 0x73, 0x00, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_DUALPOINT }, /* ThinkPad R61 8918-5QG */
119 { { 0x73, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
120 { { 0x73, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 }, /* Ahtec Laptop */
121 { { 0x20, 0x02, 0x0e }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* XXX */
122 { { 0x22, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT },
123 { { 0x22, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT }, /* Dell Latitude D600 */
124 /* Dell Latitude E5500, E6400, E6500, Precision M4400 */
125 { { 0x62, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xcf, 0xcf,
126 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED },
127 { { 0x73, 0x00, 0x14 }, 0x00, ALPS_PROTO_V6, 0xff, 0xff, ALPS_DUALPOINT }, /* Dell XT2 */
128 { { 0x73, 0x02, 0x50 }, 0x00, ALPS_PROTO_V2, 0xcf, 0xcf, ALPS_FOUR_BUTTONS }, /* Dell Vostro 1400 */
129 { { 0x52, 0x01, 0x14 }, 0x00, ALPS_PROTO_V2, 0xff, 0xff,
130 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED }, /* Toshiba Tecra A11-11L */
131 { { 0x73, 0x02, 0x64 }, 0x8a, ALPS_PROTO_V4, 0x8f, 0x8f, 0 },
134 static void alps_set_abs_params_st(struct alps_data *priv,
135 struct input_dev *dev1);
136 static void alps_set_abs_params_mt(struct alps_data *priv,
137 struct input_dev *dev1);
140 * XXX - this entry is suspicious. First byte has zero lower nibble,
141 * which is what a normal mouse would report. Also, the value 0x0e
142 * isn't valid per PS/2 spec.
145 /* Packet formats are described in Documentation/input/alps.txt */
147 static bool alps_is_valid_first_byte(struct alps_data *priv,
150 return (data & priv->mask0) == priv->byte0;
153 static void alps_report_buttons(struct psmouse *psmouse,
154 struct input_dev *dev1, struct input_dev *dev2,
155 int left, int right, int middle)
157 struct input_dev *dev;
160 * If shared button has already been reported on the
161 * other device (dev2) then this event should be also
162 * sent through that device.
164 dev = test_bit(BTN_LEFT, dev2->key) ? dev2 : dev1;
165 input_report_key(dev, BTN_LEFT, left);
167 dev = test_bit(BTN_RIGHT, dev2->key) ? dev2 : dev1;
168 input_report_key(dev, BTN_RIGHT, right);
170 dev = test_bit(BTN_MIDDLE, dev2->key) ? dev2 : dev1;
171 input_report_key(dev, BTN_MIDDLE, middle);
174 * Sync the _other_ device now, we'll do the first
175 * device later once we report the rest of the events.
180 static void alps_process_packet_v1_v2(struct psmouse *psmouse)
182 struct alps_data *priv = psmouse->private;
183 unsigned char *packet = psmouse->packet;
184 struct input_dev *dev = psmouse->dev;
185 struct input_dev *dev2 = priv->dev2;
186 int x, y, z, ges, fin, left, right, middle;
187 int back = 0, forward = 0;
189 if (priv->proto_version == ALPS_PROTO_V1) {
190 left = packet[2] & 0x10;
191 right = packet[2] & 0x08;
193 x = packet[1] | ((packet[0] & 0x07) << 7);
194 y = packet[4] | ((packet[3] & 0x07) << 7);
197 left = packet[3] & 1;
198 right = packet[3] & 2;
199 middle = packet[3] & 4;
200 x = packet[1] | ((packet[2] & 0x78) << (7 - 3));
201 y = packet[4] | ((packet[3] & 0x70) << (7 - 4));
205 if (priv->flags & ALPS_FW_BK_1) {
206 back = packet[0] & 0x10;
207 forward = packet[2] & 4;
210 if (priv->flags & ALPS_FW_BK_2) {
211 back = packet[3] & 4;
212 forward = packet[2] & 4;
213 if ((middle = forward && back))
220 if ((priv->flags & ALPS_DUALPOINT) && z == 127) {
221 input_report_rel(dev2, REL_X, (x > 383 ? (x - 768) : x));
222 input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y));
224 alps_report_buttons(psmouse, dev2, dev, left, right, middle);
230 alps_report_buttons(psmouse, dev, dev2, left, right, middle);
232 /* Convert hardware tap to a reasonable Z value */
237 * A "tap and drag" operation is reported by the hardware as a transition
238 * from (!fin && ges) to (fin && ges). This should be translated to the
239 * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
241 if (ges && fin && !priv->prev_fin) {
242 input_report_abs(dev, ABS_X, x);
243 input_report_abs(dev, ABS_Y, y);
244 input_report_abs(dev, ABS_PRESSURE, 0);
245 input_report_key(dev, BTN_TOOL_FINGER, 0);
248 priv->prev_fin = fin;
251 input_report_key(dev, BTN_TOUCH, 1);
253 input_report_key(dev, BTN_TOUCH, 0);
256 input_report_abs(dev, ABS_X, x);
257 input_report_abs(dev, ABS_Y, y);
260 input_report_abs(dev, ABS_PRESSURE, z);
261 input_report_key(dev, BTN_TOOL_FINGER, z > 0);
263 if (priv->flags & ALPS_WHEEL)
264 input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
266 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
267 input_report_key(dev, BTN_FORWARD, forward);
268 input_report_key(dev, BTN_BACK, back);
271 if (priv->flags & ALPS_FOUR_BUTTONS) {
272 input_report_key(dev, BTN_0, packet[2] & 4);
273 input_report_key(dev, BTN_1, packet[0] & 0x10);
274 input_report_key(dev, BTN_2, packet[3] & 4);
275 input_report_key(dev, BTN_3, packet[0] & 0x20);
282 * Process bitmap data for V5 protocols. Return value is null.
284 * The bitmaps don't have enough data to track fingers, so this function
285 * only generates points representing a bounding box of at most two contacts.
286 * These two points are returned in fields->mt.
288 static void alps_process_bitmap_dolphin(struct alps_data *priv,
289 struct alps_fields *fields)
291 int box_middle_x, box_middle_y;
292 unsigned int x_map, y_map;
293 unsigned char start_bit, end_bit;
294 unsigned char x_msb, x_lsb, y_msb, y_lsb;
296 x_map = fields->x_map;
297 y_map = fields->y_map;
299 if (!x_map || !y_map)
302 /* Get Most-significant and Least-significant bit */
308 /* Most-significant bit should never exceed max sensor line number */
309 if (x_msb > priv->x_bits || y_msb > priv->y_bits)
312 if (fields->fingers > 1) {
313 start_bit = priv->x_bits - x_msb;
314 end_bit = priv->x_bits - x_lsb;
315 box_middle_x = (priv->x_max * (start_bit + end_bit)) /
316 (2 * (priv->x_bits - 1));
318 start_bit = y_lsb - 1;
320 box_middle_y = (priv->y_max * (start_bit + end_bit)) /
321 (2 * (priv->y_bits - 1));
322 fields->mt[0] = fields->st;
323 fields->mt[1].x = 2 * box_middle_x - fields->mt[0].x;
324 fields->mt[1].y = 2 * box_middle_y - fields->mt[0].y;
328 static void alps_get_bitmap_points(unsigned int map,
329 struct alps_bitmap_point *low,
330 struct alps_bitmap_point *high,
333 struct alps_bitmap_point *point;
334 int i, bit, prev_bit = 0;
337 for (i = 0; map != 0; i++, map >>= 1) {
341 point->start_bit = i;
355 * Process bitmap data from v3 and v4 protocols. Returns the number of
356 * fingers detected. A return value of 0 means at least one of the
359 * The bitmaps don't have enough data to track fingers, so this function
360 * only generates points representing a bounding box of all contacts.
361 * These points are returned in fields->mt when the return value
364 static int alps_process_bitmap(struct alps_data *priv,
365 struct alps_fields *fields)
367 int i, fingers_x = 0, fingers_y = 0, fingers;
368 struct alps_bitmap_point x_low = {0,}, x_high = {0,};
369 struct alps_bitmap_point y_low = {0,}, y_high = {0,};
371 if (!fields->x_map || !fields->y_map)
374 alps_get_bitmap_points(fields->x_map, &x_low, &x_high, &fingers_x);
375 alps_get_bitmap_points(fields->y_map, &y_low, &y_high, &fingers_y);
378 * Fingers can overlap, so we use the maximum count of fingers
379 * on either axis as the finger count.
381 fingers = max(fingers_x, fingers_y);
384 * If an axis reports only a single contact, we have overlapping or
385 * adjacent fingers. Divide the single contact between the two points.
387 if (fingers_x == 1) {
388 i = (x_low.num_bits - 1) / 2;
389 x_low.num_bits = x_low.num_bits - i;
390 x_high.start_bit = x_low.start_bit + i;
391 x_high.num_bits = max(i, 1);
393 if (fingers_y == 1) {
394 i = (y_low.num_bits - 1) / 2;
395 y_low.num_bits = y_low.num_bits - i;
396 y_high.start_bit = y_low.start_bit + i;
397 y_high.num_bits = max(i, 1);
401 (priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
402 (2 * (priv->x_bits - 1));
404 (priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
405 (2 * (priv->y_bits - 1));
408 (priv->x_max * (2 * x_high.start_bit + x_high.num_bits - 1)) /
409 (2 * (priv->x_bits - 1));
411 (priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) /
412 (2 * (priv->y_bits - 1));
414 /* y-bitmap order is reversed, except on rushmore */
415 if (!(priv->flags & ALPS_IS_RUSHMORE)) {
416 fields->mt[0].y = priv->y_max - fields->mt[0].y;
417 fields->mt[1].y = priv->y_max - fields->mt[1].y;
423 static void alps_set_slot(struct input_dev *dev, int slot, int x, int y)
425 input_mt_slot(dev, slot);
426 input_mt_report_slot_state(dev, MT_TOOL_FINGER, true);
427 input_report_abs(dev, ABS_MT_POSITION_X, x);
428 input_report_abs(dev, ABS_MT_POSITION_Y, y);
431 static void alps_report_mt_data(struct psmouse *psmouse, int n)
433 struct alps_data *priv = psmouse->private;
434 struct input_dev *dev = psmouse->dev;
435 struct alps_fields *f = &priv->f;
436 int i, slot[MAX_TOUCHES];
438 input_mt_assign_slots(dev, slot, f->mt, n);
439 for (i = 0; i < n; i++)
440 alps_set_slot(dev, slot[i], f->mt[i].x, f->mt[i].y);
442 input_mt_sync_frame(dev);
445 static void alps_report_semi_mt_data(struct psmouse *psmouse, int fingers)
447 struct alps_data *priv = psmouse->private;
448 struct input_dev *dev = psmouse->dev;
449 struct alps_fields *f = &priv->f;
451 /* Use st data when we don't have mt data */
453 f->mt[0].x = f->st.x;
454 f->mt[0].y = f->st.y;
455 fingers = f->pressure > 0 ? 1 : 0;
458 alps_report_mt_data(psmouse, (fingers <= 2) ? fingers : 2);
460 input_mt_report_finger_count(dev, fingers);
462 input_report_key(dev, BTN_LEFT, f->left);
463 input_report_key(dev, BTN_RIGHT, f->right);
464 input_report_key(dev, BTN_MIDDLE, f->middle);
466 input_report_abs(dev, ABS_PRESSURE, f->pressure);
471 static void alps_process_trackstick_packet_v3(struct psmouse *psmouse)
473 struct alps_data *priv = psmouse->private;
474 unsigned char *packet = psmouse->packet;
475 struct input_dev *dev = priv->dev2;
476 int x, y, z, left, right, middle;
478 /* Sanity check packet */
479 if (!(packet[0] & 0x40)) {
480 psmouse_dbg(psmouse, "Bad trackstick packet, discarding\n");
485 * There's a special packet that seems to indicate the end
486 * of a stream of trackstick data. Filter these out.
488 if (packet[1] == 0x7f && packet[2] == 0x7f && packet[4] == 0x7f)
491 x = (s8)(((packet[0] & 0x20) << 2) | (packet[1] & 0x7f));
492 y = (s8)(((packet[0] & 0x10) << 3) | (packet[2] & 0x7f));
493 z = (packet[4] & 0x7c) >> 2;
496 * The x and y values tend to be quite large, and when used
497 * alone the trackstick is difficult to use. Scale them down
503 input_report_rel(dev, REL_X, x);
504 input_report_rel(dev, REL_Y, -y);
507 * Most ALPS models report the trackstick buttons in the touchpad
508 * packets, but a few report them here. No reliable way has been
509 * found to differentiate between the models upfront, so we enable
510 * the quirk in response to seeing a button press in the trackstick
513 left = packet[3] & 0x01;
514 right = packet[3] & 0x02;
515 middle = packet[3] & 0x04;
517 if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) &&
518 (left || right || middle))
519 priv->quirks |= ALPS_QUIRK_TRACKSTICK_BUTTONS;
521 if (priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) {
522 input_report_key(dev, BTN_LEFT, left);
523 input_report_key(dev, BTN_RIGHT, right);
524 input_report_key(dev, BTN_MIDDLE, middle);
531 static void alps_decode_buttons_v3(struct alps_fields *f, unsigned char *p)
533 f->left = !!(p[3] & 0x01);
534 f->right = !!(p[3] & 0x02);
535 f->middle = !!(p[3] & 0x04);
537 f->ts_left = !!(p[3] & 0x10);
538 f->ts_right = !!(p[3] & 0x20);
539 f->ts_middle = !!(p[3] & 0x40);
542 static int alps_decode_pinnacle(struct alps_fields *f, unsigned char *p,
543 struct psmouse *psmouse)
545 f->first_mp = !!(p[4] & 0x40);
546 f->is_mp = !!(p[0] & 0x40);
548 f->fingers = (p[5] & 0x3) + 1;
549 f->x_map = ((p[4] & 0x7e) << 8) |
550 ((p[1] & 0x7f) << 2) |
551 ((p[0] & 0x30) >> 4);
552 f->y_map = ((p[3] & 0x70) << 4) |
553 ((p[2] & 0x7f) << 1) |
556 f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
557 ((p[0] & 0x30) >> 4);
558 f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
559 f->pressure = p[5] & 0x7f;
561 alps_decode_buttons_v3(f, p);
566 static int alps_decode_rushmore(struct alps_fields *f, unsigned char *p,
567 struct psmouse *psmouse)
569 alps_decode_pinnacle(f, p, psmouse);
571 /* Rushmore's packet decode has a bit difference with Pinnacle's */
572 f->is_mp = !!(p[5] & 0x40);
573 f->fingers = max((p[5] & 0x3), ((p[5] >> 2) & 0x3)) + 1;
574 f->x_map |= (p[5] & 0x10) << 11;
575 f->y_map |= (p[5] & 0x20) << 6;
580 static int alps_decode_dolphin(struct alps_fields *f, unsigned char *p,
581 struct psmouse *psmouse)
584 struct alps_data *priv = psmouse->private;
586 f->first_mp = !!(p[0] & 0x02);
587 f->is_mp = !!(p[0] & 0x20);
590 f->st.x = ((p[1] & 0x7f) | ((p[4] & 0x0f) << 7));
591 f->st.y = ((p[2] & 0x7f) | ((p[4] & 0xf0) << 3));
592 f->pressure = (p[0] & 4) ? 0 : p[5] & 0x7f;
593 alps_decode_buttons_v3(f, p);
595 f->fingers = ((p[0] & 0x6) >> 1 |
598 palm_data = (p[1] & 0x7f) |
599 ((p[2] & 0x7f) << 7) |
600 ((p[4] & 0x7f) << 14) |
601 ((p[5] & 0x7f) << 21) |
602 ((p[3] & 0x07) << 28) |
603 (((u64)p[3] & 0x70) << 27) |
604 (((u64)p[0] & 0x01) << 34);
606 /* Y-profile is stored in P(0) to p(n-1), n = y_bits; */
607 f->y_map = palm_data & (BIT(priv->y_bits) - 1);
609 /* X-profile is stored in p(n) to p(n+m-1), m = x_bits; */
610 f->x_map = (palm_data >> priv->y_bits) &
611 (BIT(priv->x_bits) - 1);
617 static void alps_process_touchpad_packet_v3_v5(struct psmouse *psmouse)
619 struct alps_data *priv = psmouse->private;
620 unsigned char *packet = psmouse->packet;
621 struct input_dev *dev2 = priv->dev2;
622 struct alps_fields *f = &priv->f;
625 memset(f, 0, sizeof(*f));
627 priv->decode_fields(f, packet, psmouse);
630 * There's no single feature of touchpad position and bitmap packets
631 * that can be used to distinguish between them. We rely on the fact
632 * that a bitmap packet should always follow a position packet with
633 * bit 6 of packet[4] set.
635 if (priv->multi_packet) {
637 * Sometimes a position packet will indicate a multi-packet
638 * sequence, but then what follows is another position
639 * packet. Check for this, and when it happens process the
640 * position packet as usual.
643 fingers = f->fingers;
644 if (priv->proto_version == ALPS_PROTO_V3) {
645 if (alps_process_bitmap(priv, f) == 0)
646 fingers = 0; /* Use st data */
648 /* Now process position packet */
649 priv->decode_fields(f, priv->multi_data,
653 * Because Dolphin uses position packet's
654 * coordinate data as Pt1 and uses it to
655 * calculate Pt2, so we need to do position
656 * packet decode first.
658 priv->decode_fields(f, priv->multi_data,
662 * Since Dolphin's finger number is reliable,
663 * there is no need to compare with bmap_fn.
665 alps_process_bitmap_dolphin(priv, f);
668 priv->multi_packet = 0;
673 * Bit 6 of byte 0 is not usually set in position packets. The only
674 * times it seems to be set is in situations where the data is
675 * suspect anyway, e.g. a palm resting flat on the touchpad. Given
676 * this combined with the fact that this bit is useful for filtering
677 * out misidentified bitmap packets, we reject anything with this
683 if (!priv->multi_packet && f->first_mp) {
684 priv->multi_packet = 1;
685 memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
689 priv->multi_packet = 0;
692 * Sometimes the hardware sends a single packet with z = 0
693 * in the middle of a stream. Real releases generate packets
694 * with x, y, and z all zero, so these seem to be flukes.
697 if (f->st.x && f->st.y && !f->pressure)
700 alps_report_semi_mt_data(psmouse, fingers);
702 if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) {
703 input_report_key(dev2, BTN_LEFT, f->ts_left);
704 input_report_key(dev2, BTN_RIGHT, f->ts_right);
705 input_report_key(dev2, BTN_MIDDLE, f->ts_middle);
710 static void alps_process_packet_v3(struct psmouse *psmouse)
712 unsigned char *packet = psmouse->packet;
715 * v3 protocol packets come in three types, two representing
716 * touchpad data and one representing trackstick data.
717 * Trackstick packets seem to be distinguished by always
718 * having 0x3f in the last byte. This value has never been
719 * observed in the last byte of either of the other types
722 if (packet[5] == 0x3f) {
723 alps_process_trackstick_packet_v3(psmouse);
727 alps_process_touchpad_packet_v3_v5(psmouse);
730 static void alps_process_packet_v6(struct psmouse *psmouse)
732 struct alps_data *priv = psmouse->private;
733 unsigned char *packet = psmouse->packet;
734 struct input_dev *dev = psmouse->dev;
735 struct input_dev *dev2 = priv->dev2;
736 int x, y, z, left, right, middle;
739 * We can use Byte5 to distinguish if the packet is from Touchpad
744 if (packet[5] == 0x7F) {
745 /* It should be a DualPoint when received Trackpoint packet */
746 if (!(priv->flags & ALPS_DUALPOINT))
749 /* Trackpoint packet */
750 x = packet[1] | ((packet[3] & 0x20) << 2);
751 y = packet[2] | ((packet[3] & 0x40) << 1);
753 left = packet[3] & 0x01;
754 right = packet[3] & 0x02;
755 middle = packet[3] & 0x04;
757 /* To prevent the cursor jump when finger lifted */
758 if (x == 0x7F && y == 0x7F && z == 0x7F)
761 /* Divide 4 since trackpoint's speed is too fast */
762 input_report_rel(dev2, REL_X, (char)x / 4);
763 input_report_rel(dev2, REL_Y, -((char)y / 4));
765 input_report_key(dev2, BTN_LEFT, left);
766 input_report_key(dev2, BTN_RIGHT, right);
767 input_report_key(dev2, BTN_MIDDLE, middle);
773 /* Touchpad packet */
774 x = packet[1] | ((packet[3] & 0x78) << 4);
775 y = packet[2] | ((packet[4] & 0x78) << 4);
777 left = packet[3] & 0x01;
778 right = packet[3] & 0x02;
781 input_report_key(dev, BTN_TOUCH, 1);
783 input_report_key(dev, BTN_TOUCH, 0);
786 input_report_abs(dev, ABS_X, x);
787 input_report_abs(dev, ABS_Y, y);
790 input_report_abs(dev, ABS_PRESSURE, z);
791 input_report_key(dev, BTN_TOOL_FINGER, z > 0);
793 /* v6 touchpad does not have middle button */
794 input_report_key(dev, BTN_LEFT, left);
795 input_report_key(dev, BTN_RIGHT, right);
800 static void alps_process_packet_v4(struct psmouse *psmouse)
802 struct alps_data *priv = psmouse->private;
803 unsigned char *packet = psmouse->packet;
804 struct alps_fields *f = &priv->f;
808 * v4 has a 6-byte encoding for bitmap data, but this data is
809 * broken up between 3 normal packets. Use priv->multi_packet to
810 * track our position in the bitmap packet.
812 if (packet[6] & 0x40) {
813 /* sync, reset position */
814 priv->multi_packet = 0;
817 if (WARN_ON_ONCE(priv->multi_packet > 2))
820 offset = 2 * priv->multi_packet;
821 priv->multi_data[offset] = packet[6];
822 priv->multi_data[offset + 1] = packet[7];
824 if (++priv->multi_packet > 2) {
825 priv->multi_packet = 0;
827 f->x_map = ((priv->multi_data[2] & 0x1f) << 10) |
828 ((priv->multi_data[3] & 0x60) << 3) |
829 ((priv->multi_data[0] & 0x3f) << 2) |
830 ((priv->multi_data[1] & 0x60) >> 5);
831 f->y_map = ((priv->multi_data[5] & 0x01) << 10) |
832 ((priv->multi_data[3] & 0x1f) << 5) |
833 (priv->multi_data[1] & 0x1f);
835 f->fingers = alps_process_bitmap(priv, f);
838 f->left = !!(packet[4] & 0x01);
839 f->right = !!(packet[4] & 0x02);
841 f->st.x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) |
842 ((packet[0] & 0x30) >> 4);
843 f->st.y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f);
844 f->pressure = packet[5] & 0x7f;
846 alps_report_semi_mt_data(psmouse, f->fingers);
849 static bool alps_is_valid_package_v7(struct psmouse *psmouse)
851 switch (psmouse->pktcnt) {
853 return (psmouse->packet[2] & 0x40) == 0x40;
855 return (psmouse->packet[3] & 0x48) == 0x48;
857 return (psmouse->packet[5] & 0x40) == 0x00;
862 static unsigned char alps_get_packet_id_v7(char *byte)
864 unsigned char packet_id;
867 packet_id = V7_PACKET_ID_TWO;
868 else if (byte[4] & 0x01)
869 packet_id = V7_PACKET_ID_MULTI;
870 else if ((byte[0] & 0x10) && !(byte[4] & 0x43))
871 packet_id = V7_PACKET_ID_NEW;
872 else if (byte[1] == 0x00 && byte[4] == 0x00)
873 packet_id = V7_PACKET_ID_IDLE;
875 packet_id = V7_PACKET_ID_UNKNOWN;
880 static void alps_get_finger_coordinate_v7(struct input_mt_pos *mt,
882 unsigned char pkt_id)
884 mt[0].x = ((pkt[2] & 0x80) << 4);
885 mt[0].x |= ((pkt[2] & 0x3F) << 5);
886 mt[0].x |= ((pkt[3] & 0x30) >> 1);
887 mt[0].x |= (pkt[3] & 0x07);
888 mt[0].y = (pkt[1] << 3) | (pkt[0] & 0x07);
890 mt[1].x = ((pkt[3] & 0x80) << 4);
891 mt[1].x |= ((pkt[4] & 0x80) << 3);
892 mt[1].x |= ((pkt[4] & 0x3F) << 4);
893 mt[1].y = ((pkt[5] & 0x80) << 3);
894 mt[1].y |= ((pkt[5] & 0x3F) << 4);
897 case V7_PACKET_ID_TWO:
902 case V7_PACKET_ID_MULTI:
905 mt[1].y |= ((pkt[4] & 0x02) << 4);
909 case V7_PACKET_ID_NEW:
911 mt[1].x |= (pkt[0] & 0x20);
916 mt[0].y = 0x7FF - mt[0].y;
917 mt[1].y = 0x7FF - mt[1].y;
920 static int alps_get_mt_count(struct input_mt_pos *mt)
924 for (i = 0; i < MAX_TOUCHES && mt[i].x != 0 && mt[i].y != 0; i++)
930 static int alps_decode_packet_v7(struct alps_fields *f,
932 struct psmouse *psmouse)
934 unsigned char pkt_id;
936 pkt_id = alps_get_packet_id_v7(p);
937 if (pkt_id == V7_PACKET_ID_IDLE)
939 if (pkt_id == V7_PACKET_ID_UNKNOWN)
942 alps_get_finger_coordinate_v7(f->mt, p, pkt_id);
944 if (pkt_id == V7_PACKET_ID_TWO || pkt_id == V7_PACKET_ID_MULTI) {
945 f->left = (p[0] & 0x80) >> 7;
946 f->right = (p[0] & 0x20) >> 5;
947 f->middle = (p[0] & 0x10) >> 4;
950 if (pkt_id == V7_PACKET_ID_TWO)
951 f->fingers = alps_get_mt_count(f->mt);
952 else if (pkt_id == V7_PACKET_ID_MULTI)
953 f->fingers = 3 + (p[5] & 0x03);
958 static void alps_process_trackstick_packet_v7(struct psmouse *psmouse)
960 struct alps_data *priv = psmouse->private;
961 unsigned char *packet = psmouse->packet;
962 struct input_dev *dev2 = priv->dev2;
963 int x, y, z, left, right, middle;
966 * b7 b6 b5 b4 b3 b2 b1 b0
967 * Byte0 0 1 0 0 1 0 0 0
968 * Byte1 1 1 * * 1 M R L
969 * Byte2 X7 1 X5 X4 X3 X2 X1 X0
970 * Byte3 Z6 1 Y6 X6 1 Y2 Y1 Y0
971 * Byte4 Y7 0 Y5 Y4 Y3 1 1 0
972 * Byte5 T&P 0 Z5 Z4 Z3 Z2 Z1 Z0
973 * M / R / L: Middle / Right / Left button
976 x = ((packet[2] & 0xbf)) | ((packet[3] & 0x10) << 2);
977 y = (packet[3] & 0x07) | (packet[4] & 0xb8) |
978 ((packet[3] & 0x20) << 1);
979 z = (packet[5] & 0x3f) | ((packet[3] & 0x80) >> 1);
981 left = (packet[1] & 0x01);
982 right = (packet[1] & 0x02) >> 1;
983 middle = (packet[1] & 0x04) >> 2;
985 /* Divide 2 since trackpoint's speed is too fast */
986 input_report_rel(dev2, REL_X, (char)x / 2);
987 input_report_rel(dev2, REL_Y, -((char)y / 2));
989 input_report_key(dev2, BTN_LEFT, left);
990 input_report_key(dev2, BTN_RIGHT, right);
991 input_report_key(dev2, BTN_MIDDLE, middle);
996 static void alps_process_touchpad_packet_v7(struct psmouse *psmouse)
998 struct alps_data *priv = psmouse->private;
999 struct input_dev *dev = psmouse->dev;
1000 struct alps_fields *f = &priv->f;
1002 memset(f, 0, sizeof(*f));
1004 if (priv->decode_fields(f, psmouse->packet, psmouse))
1007 alps_report_mt_data(psmouse, alps_get_mt_count(f->mt));
1009 input_mt_report_finger_count(dev, f->fingers);
1011 input_report_key(dev, BTN_LEFT, f->left);
1012 input_report_key(dev, BTN_RIGHT, f->right);
1013 input_report_key(dev, BTN_MIDDLE, f->middle);
1018 static void alps_process_packet_v7(struct psmouse *psmouse)
1020 unsigned char *packet = psmouse->packet;
1022 if (packet[0] == 0x48 && (packet[4] & 0x47) == 0x06)
1023 alps_process_trackstick_packet_v7(psmouse);
1025 alps_process_touchpad_packet_v7(psmouse);
1028 static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
1029 unsigned char packet[],
1030 bool report_buttons)
1032 struct alps_data *priv = psmouse->private;
1033 struct input_dev *dev2 = priv->dev2;
1036 alps_report_buttons(psmouse, dev2, psmouse->dev,
1037 packet[0] & 1, packet[0] & 2, packet[0] & 4);
1039 input_report_rel(dev2, REL_X,
1040 packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
1041 input_report_rel(dev2, REL_Y,
1042 packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
1047 static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
1049 struct alps_data *priv = psmouse->private;
1051 if (psmouse->pktcnt < 6)
1052 return PSMOUSE_GOOD_DATA;
1054 if (psmouse->pktcnt == 6) {
1056 * Start a timer to flush the packet if it ends up last
1057 * 6-byte packet in the stream. Timer needs to fire
1058 * psmouse core times out itself. 20 ms should be enough
1059 * to decide if we are getting more data or not.
1061 mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
1062 return PSMOUSE_GOOD_DATA;
1065 del_timer(&priv->timer);
1067 if (psmouse->packet[6] & 0x80) {
1070 * Highest bit is set - that means we either had
1071 * complete ALPS packet and this is start of the
1072 * next packet or we got garbage.
1075 if (((psmouse->packet[3] |
1076 psmouse->packet[4] |
1077 psmouse->packet[5]) & 0x80) ||
1078 (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) {
1079 psmouse_dbg(psmouse,
1080 "refusing packet %4ph (suspected interleaved ps/2)\n",
1081 psmouse->packet + 3);
1082 return PSMOUSE_BAD_DATA;
1085 priv->process_packet(psmouse);
1087 /* Continue with the next packet */
1088 psmouse->packet[0] = psmouse->packet[6];
1089 psmouse->pktcnt = 1;
1094 * High bit is 0 - that means that we indeed got a PS/2
1095 * packet in the middle of ALPS packet.
1097 * There is also possibility that we got 6-byte ALPS
1098 * packet followed by 3-byte packet from trackpoint. We
1099 * can not distinguish between these 2 scenarios but
1100 * because the latter is unlikely to happen in course of
1101 * normal operation (user would need to press all
1102 * buttons on the pad and start moving trackpoint
1103 * without touching the pad surface) we assume former.
1104 * Even if we are wrong the wost thing that would happen
1105 * the cursor would jump but we should not get protocol
1106 * de-synchronization.
1109 alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
1113 * Continue with the standard ALPS protocol handling,
1114 * but make sure we won't process it as an interleaved
1115 * packet again, which may happen if all buttons are
1116 * pressed. To avoid this let's reset the 4th bit which
1119 psmouse->packet[3] = psmouse->packet[6] & 0xf7;
1120 psmouse->pktcnt = 4;
1123 return PSMOUSE_GOOD_DATA;
1126 static void alps_flush_packet(unsigned long data)
1128 struct psmouse *psmouse = (struct psmouse *)data;
1129 struct alps_data *priv = psmouse->private;
1131 serio_pause_rx(psmouse->ps2dev.serio);
1133 if (psmouse->pktcnt == psmouse->pktsize) {
1136 * We did not any more data in reasonable amount of time.
1137 * Validate the last 3 bytes and process as a standard
1140 if ((psmouse->packet[3] |
1141 psmouse->packet[4] |
1142 psmouse->packet[5]) & 0x80) {
1143 psmouse_dbg(psmouse,
1144 "refusing packet %3ph (suspected interleaved ps/2)\n",
1145 psmouse->packet + 3);
1147 priv->process_packet(psmouse);
1149 psmouse->pktcnt = 0;
1152 serio_continue_rx(psmouse->ps2dev.serio);
1155 static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
1157 struct alps_data *priv = psmouse->private;
1159 if ((psmouse->packet[0] & 0xc8) == 0x08) { /* PS/2 packet */
1160 if (psmouse->pktcnt == 3) {
1161 alps_report_bare_ps2_packet(psmouse, psmouse->packet,
1163 return PSMOUSE_FULL_PACKET;
1165 return PSMOUSE_GOOD_DATA;
1168 /* Check for PS/2 packet stuffed in the middle of ALPS packet. */
1170 if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
1171 psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
1172 return alps_handle_interleaved_ps2(psmouse);
1175 if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
1176 psmouse_dbg(psmouse,
1177 "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
1178 psmouse->packet[0], priv->mask0, priv->byte0);
1179 return PSMOUSE_BAD_DATA;
1182 /* Bytes 2 - pktsize should have 0 in the highest bit */
1183 if ((priv->proto_version < ALPS_PROTO_V5) &&
1184 psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
1185 (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
1186 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1187 psmouse->pktcnt - 1,
1188 psmouse->packet[psmouse->pktcnt - 1]);
1189 return PSMOUSE_BAD_DATA;
1192 if (priv->proto_version == ALPS_PROTO_V7 &&
1193 !alps_is_valid_package_v7(psmouse)) {
1194 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1195 psmouse->pktcnt - 1,
1196 psmouse->packet[psmouse->pktcnt - 1]);
1197 return PSMOUSE_BAD_DATA;
1200 if (psmouse->pktcnt == psmouse->pktsize) {
1201 priv->process_packet(psmouse);
1202 return PSMOUSE_FULL_PACKET;
1205 return PSMOUSE_GOOD_DATA;
1208 static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
1210 struct ps2dev *ps2dev = &psmouse->ps2dev;
1211 struct alps_data *priv = psmouse->private;
1213 unsigned char *param;
1214 unsigned char dummy[4];
1216 BUG_ON(nibble > 0xf);
1218 command = priv->nibble_commands[nibble].command;
1219 param = (command & 0x0f00) ?
1220 dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
1222 if (ps2_command(ps2dev, param, command))
1228 static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
1230 struct ps2dev *ps2dev = &psmouse->ps2dev;
1231 struct alps_data *priv = psmouse->private;
1234 if (ps2_command(ps2dev, NULL, priv->addr_command))
1237 for (i = 12; i >= 0; i -= 4) {
1238 nibble = (addr >> i) & 0xf;
1239 if (alps_command_mode_send_nibble(psmouse, nibble))
1246 static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1248 struct ps2dev *ps2dev = &psmouse->ps2dev;
1249 unsigned char param[4];
1251 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1255 * The address being read is returned in the first two bytes
1256 * of the result. Check that this address matches the expected
1259 if (addr != ((param[0] << 8) | param[1]))
1265 static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1267 if (alps_command_mode_set_addr(psmouse, addr))
1269 return __alps_command_mode_read_reg(psmouse, addr);
1272 static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
1274 if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
1276 if (alps_command_mode_send_nibble(psmouse, value & 0xf))
1281 static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
1284 if (alps_command_mode_set_addr(psmouse, addr))
1286 return __alps_command_mode_write_reg(psmouse, value);
1289 static int alps_rpt_cmd(struct psmouse *psmouse, int init_command,
1290 int repeated_command, unsigned char *param)
1292 struct ps2dev *ps2dev = &psmouse->ps2dev;
1295 if (init_command && ps2_command(ps2dev, param, init_command))
1298 if (ps2_command(ps2dev, NULL, repeated_command) ||
1299 ps2_command(ps2dev, NULL, repeated_command) ||
1300 ps2_command(ps2dev, NULL, repeated_command))
1303 param[0] = param[1] = param[2] = 0xff;
1304 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1307 psmouse_dbg(psmouse, "%2.2X report: %3ph\n",
1308 repeated_command, param);
1312 static bool alps_check_valid_firmware_id(unsigned char id[])
1317 if (id[0] == 0x88 &&
1320 (id[1] & 0xf0) == 0xb0 ||
1321 (id[1] & 0xf0) == 0xc0)) {
1328 static int alps_enter_command_mode(struct psmouse *psmouse)
1330 unsigned char param[4];
1332 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) {
1333 psmouse_err(psmouse, "failed to enter command mode\n");
1337 if (!alps_check_valid_firmware_id(param)) {
1338 psmouse_dbg(psmouse,
1339 "unknown response while entering command mode\n");
1345 static inline int alps_exit_command_mode(struct psmouse *psmouse)
1347 struct ps2dev *ps2dev = &psmouse->ps2dev;
1348 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
1354 * For DualPoint devices select the device that should respond to
1355 * subsequent commands. It looks like glidepad is behind stickpointer,
1356 * I'd thought it would be other way around...
1358 static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
1360 struct ps2dev *ps2dev = &psmouse->ps2dev;
1361 int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
1363 if (ps2_command(ps2dev, NULL, cmd) ||
1364 ps2_command(ps2dev, NULL, cmd) ||
1365 ps2_command(ps2dev, NULL, cmd) ||
1366 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1369 /* we may get 3 more bytes, just ignore them */
1370 ps2_drain(ps2dev, 3, 100);
1375 static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
1377 struct ps2dev *ps2dev = &psmouse->ps2dev;
1379 /* Try ALPS magic knock - 4 disable before enable */
1380 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1381 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1382 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1383 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1384 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
1388 * Switch mouse to poll (remote) mode so motion data will not
1391 return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
1394 static int alps_monitor_mode_send_word(struct psmouse *psmouse, u16 word)
1399 * b0-b11 are valid bits, send sequence is inverse.
1400 * e.g. when word = 0x0123, nibble send sequence is 3, 2, 1
1402 for (i = 0; i <= 8; i += 4) {
1403 nibble = (word >> i) & 0xf;
1404 if (alps_command_mode_send_nibble(psmouse, nibble))
1411 static int alps_monitor_mode_write_reg(struct psmouse *psmouse,
1412 u16 addr, u16 value)
1414 struct ps2dev *ps2dev = &psmouse->ps2dev;
1416 /* 0x0A0 is the command to write the word */
1417 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE) ||
1418 alps_monitor_mode_send_word(psmouse, 0x0A0) ||
1419 alps_monitor_mode_send_word(psmouse, addr) ||
1420 alps_monitor_mode_send_word(psmouse, value) ||
1421 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1427 static int alps_monitor_mode(struct psmouse *psmouse, bool enable)
1429 struct ps2dev *ps2dev = &psmouse->ps2dev;
1432 /* EC E9 F5 F5 E7 E6 E7 E9 to enter monitor mode */
1433 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
1434 ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO) ||
1435 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1436 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1437 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1438 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1439 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1440 ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO))
1443 /* EC to exit monitor mode */
1444 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP))
1451 static int alps_absolute_mode_v6(struct psmouse *psmouse)
1453 u16 reg_val = 0x181;
1456 /* enter monitor mode, to write the register */
1457 if (alps_monitor_mode(psmouse, true))
1460 ret = alps_monitor_mode_write_reg(psmouse, 0x000, reg_val);
1462 if (alps_monitor_mode(psmouse, false))
1468 static int alps_get_status(struct psmouse *psmouse, char *param)
1470 /* Get status: 0xF5 0xF5 0xF5 0xE9 */
1471 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param))
1478 * Turn touchpad tapping on or off. The sequences are:
1479 * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
1480 * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
1481 * My guess that 0xE9 (GetInfo) is here as a sync point.
1482 * For models that also have stickpointer (DualPoints) its tapping
1483 * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
1484 * we don't fiddle with it.
1486 static int alps_tap_mode(struct psmouse *psmouse, int enable)
1488 struct ps2dev *ps2dev = &psmouse->ps2dev;
1489 int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
1490 unsigned char tap_arg = enable ? 0x0A : 0x00;
1491 unsigned char param[4];
1493 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
1494 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1495 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1496 ps2_command(ps2dev, &tap_arg, cmd))
1499 if (alps_get_status(psmouse, param))
1506 * alps_poll() - poll the touchpad for current motion packet.
1509 static int alps_poll(struct psmouse *psmouse)
1511 struct alps_data *priv = psmouse->private;
1512 unsigned char buf[sizeof(psmouse->packet)];
1515 if (priv->flags & ALPS_PASS)
1516 alps_passthrough_mode_v2(psmouse, true);
1518 poll_failed = ps2_command(&psmouse->ps2dev, buf,
1519 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
1521 if (priv->flags & ALPS_PASS)
1522 alps_passthrough_mode_v2(psmouse, false);
1524 if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
1527 if ((psmouse->badbyte & 0xc8) == 0x08) {
1529 * Poll the track stick ...
1531 if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
1535 memcpy(psmouse->packet, buf, sizeof(buf));
1539 static int alps_hw_init_v1_v2(struct psmouse *psmouse)
1541 struct alps_data *priv = psmouse->private;
1543 if ((priv->flags & ALPS_PASS) &&
1544 alps_passthrough_mode_v2(psmouse, true)) {
1548 if (alps_tap_mode(psmouse, true)) {
1549 psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
1553 if (alps_absolute_mode_v1_v2(psmouse)) {
1554 psmouse_err(psmouse, "Failed to enable absolute mode\n");
1558 if ((priv->flags & ALPS_PASS) &&
1559 alps_passthrough_mode_v2(psmouse, false)) {
1563 /* ALPS needs stream mode, otherwise it won't report any data */
1564 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
1565 psmouse_err(psmouse, "Failed to enable stream mode\n");
1572 static int alps_hw_init_v6(struct psmouse *psmouse)
1574 unsigned char param[2] = {0xC8, 0x14};
1576 /* Enter passthrough mode to let trackpoint enter 6byte raw mode */
1577 if (alps_passthrough_mode_v2(psmouse, true))
1580 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1581 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1582 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1583 ps2_command(&psmouse->ps2dev, ¶m[0], PSMOUSE_CMD_SETRATE) ||
1584 ps2_command(&psmouse->ps2dev, ¶m[1], PSMOUSE_CMD_SETRATE))
1587 if (alps_passthrough_mode_v2(psmouse, false))
1590 if (alps_absolute_mode_v6(psmouse)) {
1591 psmouse_err(psmouse, "Failed to enable absolute mode\n");
1599 * Enable or disable passthrough mode to the trackstick.
1601 static int alps_passthrough_mode_v3(struct psmouse *psmouse,
1602 int reg_base, bool enable)
1604 int reg_val, ret = -1;
1606 if (alps_enter_command_mode(psmouse))
1609 reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x0008);
1618 ret = __alps_command_mode_write_reg(psmouse, reg_val);
1621 if (alps_exit_command_mode(psmouse))
1626 /* Must be in command mode when calling this function */
1627 static int alps_absolute_mode_v3(struct psmouse *psmouse)
1631 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
1636 if (__alps_command_mode_write_reg(psmouse, reg_val))
1642 static int alps_probe_trackstick_v3(struct psmouse *psmouse, int reg_base)
1644 int ret = -EIO, reg_val;
1646 if (alps_enter_command_mode(psmouse))
1649 reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x08);
1653 /* bit 7: trackstick is present */
1654 ret = reg_val & 0x80 ? 0 : -ENODEV;
1657 alps_exit_command_mode(psmouse);
1661 static int alps_setup_trackstick_v3(struct psmouse *psmouse, int reg_base)
1663 struct ps2dev *ps2dev = &psmouse->ps2dev;
1665 unsigned char param[4];
1667 if (alps_passthrough_mode_v3(psmouse, reg_base, true))
1671 * E7 report for the trackstick
1673 * There have been reports of failures to seem to trace back
1674 * to the above trackstick check failing. When these occur
1675 * this E7 report fails, so when that happens we continue
1676 * with the assumption that there isn't a trackstick after
1679 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_SETSCALE21, param)) {
1680 psmouse_warn(psmouse, "trackstick E7 report failed\n");
1683 psmouse_dbg(psmouse, "trackstick E7 report: %3ph\n", param);
1686 * Not sure what this does, but it is absolutely
1687 * essential. Without it, the touchpad does not
1688 * work at all and the trackstick just emits normal
1691 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1692 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1693 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1694 alps_command_mode_send_nibble(psmouse, 0x9) ||
1695 alps_command_mode_send_nibble(psmouse, 0x4)) {
1696 psmouse_err(psmouse,
1697 "Error sending magic E6 sequence\n");
1703 * This ensures the trackstick packets are in the format
1704 * supported by this driver. If bit 1 isn't set the packet
1705 * format is different.
1707 if (alps_enter_command_mode(psmouse) ||
1708 alps_command_mode_write_reg(psmouse,
1709 reg_base + 0x08, 0x82) ||
1710 alps_exit_command_mode(psmouse))
1715 if (alps_passthrough_mode_v3(psmouse, reg_base, false))
1721 static int alps_hw_init_v3(struct psmouse *psmouse)
1723 struct ps2dev *ps2dev = &psmouse->ps2dev;
1725 unsigned char param[4];
1727 reg_val = alps_probe_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE);
1728 if (reg_val == -EIO)
1732 alps_setup_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE) == -EIO)
1735 if (alps_enter_command_mode(psmouse) ||
1736 alps_absolute_mode_v3(psmouse)) {
1737 psmouse_err(psmouse, "Failed to enter absolute mode\n");
1741 reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
1744 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
1747 reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
1750 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
1753 if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
1755 if (__alps_command_mode_write_reg(psmouse, 0x04))
1758 if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
1760 if (__alps_command_mode_write_reg(psmouse, 0x03))
1763 if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
1765 if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
1768 if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
1770 if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
1773 alps_exit_command_mode(psmouse);
1775 /* Set rate and enable data reporting */
1777 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
1778 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1779 psmouse_err(psmouse, "Failed to enable data reporting\n");
1787 * Leaving the touchpad in command mode will essentially render
1788 * it unusable until the machine reboots, so exit it here just
1791 alps_exit_command_mode(psmouse);
1795 static int alps_get_v3_v7_resolution(struct psmouse *psmouse, int reg_pitch)
1797 int reg, x_pitch, y_pitch, x_electrode, y_electrode, x_phys, y_phys;
1798 struct alps_data *priv = psmouse->private;
1800 reg = alps_command_mode_read_reg(psmouse, reg_pitch);
1804 x_pitch = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
1805 x_pitch = 50 + 2 * x_pitch; /* In 0.1 mm units */
1807 y_pitch = (char)reg >> 4; /* sign extend upper 4 bits */
1808 y_pitch = 36 + 2 * y_pitch; /* In 0.1 mm units */
1810 reg = alps_command_mode_read_reg(psmouse, reg_pitch + 1);
1814 x_electrode = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
1815 x_electrode = 17 + x_electrode;
1817 y_electrode = (char)reg >> 4; /* sign extend upper 4 bits */
1818 y_electrode = 13 + y_electrode;
1820 x_phys = x_pitch * (x_electrode - 1); /* In 0.1 mm units */
1821 y_phys = y_pitch * (y_electrode - 1); /* In 0.1 mm units */
1823 priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
1824 priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
1826 psmouse_dbg(psmouse,
1827 "pitch %dx%d num-electrodes %dx%d physical size %dx%d mm res %dx%d\n",
1828 x_pitch, y_pitch, x_electrode, y_electrode,
1829 x_phys / 10, y_phys / 10, priv->x_res, priv->y_res);
1834 static int alps_hw_init_rushmore_v3(struct psmouse *psmouse)
1836 struct alps_data *priv = psmouse->private;
1837 struct ps2dev *ps2dev = &psmouse->ps2dev;
1838 int reg_val, ret = -1;
1840 if (priv->flags & ALPS_DUALPOINT) {
1841 reg_val = alps_setup_trackstick_v3(psmouse,
1842 ALPS_REG_BASE_RUSHMORE);
1843 if (reg_val == -EIO)
1845 if (reg_val == -ENODEV)
1846 priv->flags &= ~ALPS_DUALPOINT;
1849 if (alps_enter_command_mode(psmouse) ||
1850 alps_command_mode_read_reg(psmouse, 0xc2d9) == -1 ||
1851 alps_command_mode_write_reg(psmouse, 0xc2cb, 0x00))
1854 if (alps_get_v3_v7_resolution(psmouse, 0xc2da))
1857 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c6);
1860 if (__alps_command_mode_write_reg(psmouse, reg_val & 0xfd))
1863 if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
1866 /* enter absolute mode */
1867 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
1870 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
1873 alps_exit_command_mode(psmouse);
1874 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
1877 alps_exit_command_mode(psmouse);
1881 /* Must be in command mode when calling this function */
1882 static int alps_absolute_mode_v4(struct psmouse *psmouse)
1886 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
1891 if (__alps_command_mode_write_reg(psmouse, reg_val))
1897 static int alps_hw_init_v4(struct psmouse *psmouse)
1899 struct ps2dev *ps2dev = &psmouse->ps2dev;
1900 unsigned char param[4];
1902 if (alps_enter_command_mode(psmouse))
1905 if (alps_absolute_mode_v4(psmouse)) {
1906 psmouse_err(psmouse, "Failed to enter absolute mode\n");
1910 if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
1913 if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
1916 if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
1919 if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
1922 if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
1925 if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
1928 if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
1931 if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
1934 alps_exit_command_mode(psmouse);
1937 * This sequence changes the output from a 9-byte to an
1938 * 8-byte format. All the same data seems to be present,
1939 * just in a more compact format.
1944 if (ps2_command(ps2dev, ¶m[0], PSMOUSE_CMD_SETRATE) ||
1945 ps2_command(ps2dev, ¶m[1], PSMOUSE_CMD_SETRATE) ||
1946 ps2_command(ps2dev, ¶m[2], PSMOUSE_CMD_SETRATE) ||
1947 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
1950 /* Set rate and enable data reporting */
1952 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
1953 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1954 psmouse_err(psmouse, "Failed to enable data reporting\n");
1962 * Leaving the touchpad in command mode will essentially render
1963 * it unusable until the machine reboots, so exit it here just
1966 alps_exit_command_mode(psmouse);
1970 static int alps_dolphin_get_device_area(struct psmouse *psmouse,
1971 struct alps_data *priv)
1973 struct ps2dev *ps2dev = &psmouse->ps2dev;
1974 unsigned char param[4] = {0};
1975 int num_x_electrode, num_y_electrode;
1977 if (alps_enter_command_mode(psmouse))
1981 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
1982 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
1983 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
1984 ps2_command(ps2dev, ¶m[0], PSMOUSE_CMD_SETRATE) ||
1985 ps2_command(ps2dev, ¶m[0], PSMOUSE_CMD_SETRATE))
1988 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1992 * Dolphin's sensor line number is not fixed. It can be calculated
1993 * by adding the device's register value with DOLPHIN_PROFILE_X/YOFFSET.
1994 * Further more, we can get device's x_max and y_max by multiplying
1995 * sensor line number with DOLPHIN_COUNT_PER_ELECTRODE.
1997 * e.g. When we get register's sensor_x = 11 & sensor_y = 8,
1998 * real sensor line number X = 11 + 8 = 19, and
1999 * real sensor line number Y = 8 + 1 = 9.
2000 * So, x_max = (19 - 1) * 64 = 1152, and
2001 * y_max = (9 - 1) * 64 = 512.
2003 num_x_electrode = DOLPHIN_PROFILE_XOFFSET + (param[2] & 0x0F);
2004 num_y_electrode = DOLPHIN_PROFILE_YOFFSET + ((param[2] >> 4) & 0x0F);
2005 priv->x_bits = num_x_electrode;
2006 priv->y_bits = num_y_electrode;
2007 priv->x_max = (num_x_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2008 priv->y_max = (num_y_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2010 if (alps_exit_command_mode(psmouse))
2016 static int alps_hw_init_dolphin_v1(struct psmouse *psmouse)
2018 struct ps2dev *ps2dev = &psmouse->ps2dev;
2019 unsigned char param[2];
2021 /* This is dolphin "v1" as empirically defined by florin9doi */
2025 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2026 ps2_command(ps2dev, ¶m[0], PSMOUSE_CMD_SETRATE) ||
2027 ps2_command(ps2dev, ¶m[1], PSMOUSE_CMD_SETRATE))
2033 static int alps_hw_init_v7(struct psmouse *psmouse)
2035 struct ps2dev *ps2dev = &psmouse->ps2dev;
2036 int reg_val, ret = -1;
2038 if (alps_enter_command_mode(psmouse) ||
2039 alps_command_mode_read_reg(psmouse, 0xc2d9) == -1)
2042 if (alps_get_v3_v7_resolution(psmouse, 0xc397))
2045 if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
2048 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
2051 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
2054 alps_exit_command_mode(psmouse);
2055 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2058 alps_exit_command_mode(psmouse);
2062 static void alps_set_defaults(struct alps_data *priv)
2066 priv->flags = ALPS_DUALPOINT;
2073 switch (priv->proto_version) {
2076 priv->hw_init = alps_hw_init_v1_v2;
2077 priv->process_packet = alps_process_packet_v1_v2;
2078 priv->set_abs_params = alps_set_abs_params_st;
2083 priv->hw_init = alps_hw_init_v3;
2084 priv->process_packet = alps_process_packet_v3;
2085 priv->set_abs_params = alps_set_abs_params_mt;
2086 priv->decode_fields = alps_decode_pinnacle;
2087 priv->nibble_commands = alps_v3_nibble_commands;
2088 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2091 priv->hw_init = alps_hw_init_v4;
2092 priv->process_packet = alps_process_packet_v4;
2093 priv->set_abs_params = alps_set_abs_params_mt;
2094 priv->nibble_commands = alps_v4_nibble_commands;
2095 priv->addr_command = PSMOUSE_CMD_DISABLE;
2098 priv->hw_init = alps_hw_init_dolphin_v1;
2099 priv->process_packet = alps_process_touchpad_packet_v3_v5;
2100 priv->decode_fields = alps_decode_dolphin;
2101 priv->set_abs_params = alps_set_abs_params_mt;
2102 priv->nibble_commands = alps_v3_nibble_commands;
2103 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2113 priv->hw_init = alps_hw_init_v6;
2114 priv->process_packet = alps_process_packet_v6;
2115 priv->set_abs_params = alps_set_abs_params_st;
2116 priv->nibble_commands = alps_v6_nibble_commands;
2121 priv->hw_init = alps_hw_init_v7;
2122 priv->process_packet = alps_process_packet_v7;
2123 priv->decode_fields = alps_decode_packet_v7;
2124 priv->set_abs_params = alps_set_abs_params_mt;
2125 priv->nibble_commands = alps_v3_nibble_commands;
2126 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2127 priv->x_max = 0xfff;
2128 priv->y_max = 0x7ff;
2132 if (priv->fw_ver[1] != 0xba)
2133 priv->flags |= ALPS_BUTTONPAD;
2138 static int alps_match_table(struct psmouse *psmouse, struct alps_data *priv,
2139 unsigned char *e7, unsigned char *ec)
2141 const struct alps_model_info *model;
2144 for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
2145 model = &alps_model_data[i];
2147 if (!memcmp(e7, model->signature, sizeof(model->signature)) &&
2148 (!model->command_mode_resp ||
2149 model->command_mode_resp == ec[2])) {
2151 priv->proto_version = model->proto_version;
2152 alps_set_defaults(priv);
2154 priv->flags = model->flags;
2155 priv->byte0 = model->byte0;
2156 priv->mask0 = model->mask0;
2165 static int alps_identify(struct psmouse *psmouse, struct alps_data *priv)
2167 unsigned char e6[4], e7[4], ec[4];
2170 * First try "E6 report".
2171 * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
2172 * The bits 0-2 of the first byte will be 1s if some buttons are
2175 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2176 PSMOUSE_CMD_SETSCALE11, e6))
2179 if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100))
2183 * Now get the "E7" and "EC" reports. These will uniquely identify
2184 * most ALPS touchpads.
2186 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2187 PSMOUSE_CMD_SETSCALE21, e7) ||
2188 alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2189 PSMOUSE_CMD_RESET_WRAP, ec) ||
2190 alps_exit_command_mode(psmouse))
2193 /* Save the Firmware version */
2194 memcpy(priv->fw_ver, ec, 3);
2196 if (alps_match_table(psmouse, priv, e7, ec) == 0) {
2198 } else if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0x50 &&
2199 ec[0] == 0x73 && (ec[1] == 0x01 || ec[1] == 0x02)) {
2200 priv->proto_version = ALPS_PROTO_V5;
2201 alps_set_defaults(priv);
2202 if (alps_dolphin_get_device_area(psmouse, priv))
2206 } else if (ec[0] == 0x88 &&
2207 ((ec[1] & 0xf0) == 0xb0 || (ec[1] & 0xf0) == 0xc0)) {
2208 priv->proto_version = ALPS_PROTO_V7;
2209 alps_set_defaults(priv);
2212 } else if (ec[0] == 0x88 && ec[1] == 0x08) {
2213 priv->proto_version = ALPS_PROTO_V3;
2214 alps_set_defaults(priv);
2216 priv->hw_init = alps_hw_init_rushmore_v3;
2217 priv->decode_fields = alps_decode_rushmore;
2220 priv->flags |= ALPS_IS_RUSHMORE;
2222 /* hack to make addr_command, nibble_command available */
2223 psmouse->private = priv;
2225 if (alps_probe_trackstick_v3(psmouse, ALPS_REG_BASE_RUSHMORE))
2226 priv->flags &= ~ALPS_DUALPOINT;
2229 } else if (ec[0] == 0x88 && ec[1] == 0x07 &&
2230 ec[2] >= 0x90 && ec[2] <= 0x9d) {
2231 priv->proto_version = ALPS_PROTO_V3;
2232 alps_set_defaults(priv);
2237 psmouse_dbg(psmouse,
2238 "Likely not an ALPS touchpad: E7=%3ph, EC=%3ph\n", e7, ec);
2243 static int alps_reconnect(struct psmouse *psmouse)
2245 struct alps_data *priv = psmouse->private;
2247 psmouse_reset(psmouse);
2249 if (alps_identify(psmouse, priv) < 0)
2252 return priv->hw_init(psmouse);
2255 static void alps_disconnect(struct psmouse *psmouse)
2257 struct alps_data *priv = psmouse->private;
2259 psmouse_reset(psmouse);
2260 del_timer_sync(&priv->timer);
2261 input_unregister_device(priv->dev2);
2265 static void alps_set_abs_params_st(struct alps_data *priv,
2266 struct input_dev *dev1)
2268 input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
2269 input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
2272 static void alps_set_abs_params_mt(struct alps_data *priv,
2273 struct input_dev *dev1)
2275 input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
2276 input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
2278 input_abs_set_res(dev1, ABS_MT_POSITION_X, priv->x_res);
2279 input_abs_set_res(dev1, ABS_MT_POSITION_Y, priv->y_res);
2281 input_mt_init_slots(dev1, MAX_TOUCHES, INPUT_MT_POINTER |
2282 INPUT_MT_DROP_UNUSED | INPUT_MT_TRACK | INPUT_MT_SEMI_MT);
2284 set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
2285 set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
2287 /* V7 is real multi-touch */
2288 if (priv->proto_version == ALPS_PROTO_V7)
2289 clear_bit(INPUT_PROP_SEMI_MT, dev1->propbit);
2292 int alps_init(struct psmouse *psmouse)
2294 struct alps_data *priv;
2295 struct input_dev *dev1 = psmouse->dev, *dev2;
2297 priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
2298 dev2 = input_allocate_device();
2303 setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
2305 psmouse->private = priv;
2307 psmouse_reset(psmouse);
2309 if (alps_identify(psmouse, priv) < 0)
2312 if (priv->hw_init(psmouse))
2316 * Undo part of setup done for us by psmouse core since touchpad
2317 * is not a relative device.
2319 __clear_bit(EV_REL, dev1->evbit);
2320 __clear_bit(REL_X, dev1->relbit);
2321 __clear_bit(REL_Y, dev1->relbit);
2324 * Now set up our capabilities.
2326 dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
2327 dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
2328 dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
2329 dev1->keybit[BIT_WORD(BTN_LEFT)] |=
2330 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
2332 dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
2334 priv->set_abs_params(priv, dev1);
2335 /* No pressure on V7 */
2336 if (priv->proto_version != ALPS_PROTO_V7)
2337 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
2339 if (priv->flags & ALPS_WHEEL) {
2340 dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
2341 dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
2344 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
2345 dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
2346 dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
2349 if (priv->flags & ALPS_FOUR_BUTTONS) {
2350 dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
2351 dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
2352 dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
2353 dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
2354 } else if (priv->flags & ALPS_BUTTONPAD) {
2355 set_bit(INPUT_PROP_BUTTONPAD, dev1->propbit);
2356 clear_bit(BTN_RIGHT, dev1->keybit);
2358 dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
2361 snprintf(priv->phys, sizeof(priv->phys), "%s/input1", psmouse->ps2dev.serio->phys);
2362 dev2->phys = priv->phys;
2363 dev2->name = (priv->flags & ALPS_DUALPOINT) ?
2364 "DualPoint Stick" : "ALPS PS/2 Device";
2365 dev2->id.bustype = BUS_I8042;
2366 dev2->id.vendor = 0x0002;
2367 dev2->id.product = PSMOUSE_ALPS;
2368 dev2->id.version = 0x0000;
2369 dev2->dev.parent = &psmouse->ps2dev.serio->dev;
2371 dev2->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
2372 dev2->relbit[BIT_WORD(REL_X)] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
2373 dev2->keybit[BIT_WORD(BTN_LEFT)] =
2374 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_MIDDLE) | BIT_MASK(BTN_RIGHT);
2376 __set_bit(INPUT_PROP_POINTER, dev2->propbit);
2377 if (priv->flags & ALPS_DUALPOINT)
2378 __set_bit(INPUT_PROP_POINTING_STICK, dev2->propbit);
2380 if (input_register_device(priv->dev2))
2383 psmouse->protocol_handler = alps_process_byte;
2384 psmouse->poll = alps_poll;
2385 psmouse->disconnect = alps_disconnect;
2386 psmouse->reconnect = alps_reconnect;
2387 psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
2389 /* We are having trouble resyncing ALPS touchpads so disable it for now */
2390 psmouse->resync_time = 0;
2395 psmouse_reset(psmouse);
2396 input_free_device(dev2);
2398 psmouse->private = NULL;
2402 int alps_detect(struct psmouse *psmouse, bool set_properties)
2404 struct alps_data dummy;
2406 if (alps_identify(psmouse, &dummy) < 0)
2409 if (set_properties) {
2410 psmouse->vendor = "ALPS";
2411 psmouse->name = dummy.flags & ALPS_DUALPOINT ?
2412 "DualPoint TouchPad" : "GlidePoint";
2413 psmouse->model = dummy.proto_version << 8;