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_BUTTONPAD 0x200 /* device is a clickpad */
104 static const struct alps_model_info alps_model_data[] = {
105 { { 0x32, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } }, /* Toshiba Salellite Pro M10 */
106 { { 0x33, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V1, 0x88, 0xf8, 0 } }, /* UMAX-530T */
107 { { 0x53, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
108 { { 0x53, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
109 { { 0x60, 0x03, 0xc8 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } }, /* HP ze1115 */
110 { { 0x63, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
111 { { 0x63, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
112 { { 0x63, 0x02, 0x28 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 } }, /* Fujitsu Siemens S6010 */
113 { { 0x63, 0x02, 0x3c }, 0x00, { ALPS_PROTO_V2, 0x8f, 0x8f, ALPS_WHEEL } }, /* Toshiba Satellite S2400-103 */
114 { { 0x63, 0x02, 0x50 }, 0x00, { ALPS_PROTO_V2, 0xef, 0xef, ALPS_FW_BK_1 } }, /* NEC Versa L320 */
115 { { 0x63, 0x02, 0x64 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
116 { { 0x63, 0x03, 0xc8 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } }, /* Dell Latitude D800 */
117 { { 0x73, 0x00, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_DUALPOINT } }, /* ThinkPad R61 8918-5QG */
118 { { 0x73, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
119 { { 0x73, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 } }, /* Ahtec Laptop */
122 * XXX This entry is suspicious. First byte has zero lower nibble,
123 * which is what a normal mouse would report. Also, the value 0x0e
124 * isn't valid per PS/2 spec.
126 { { 0x20, 0x02, 0x0e }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },
128 { { 0x22, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },
129 { { 0x22, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT } }, /* Dell Latitude D600 */
130 /* Dell Latitude E5500, E6400, E6500, Precision M4400 */
131 { { 0x62, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xcf, 0xcf,
132 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED } },
133 { { 0x73, 0x00, 0x14 }, 0x00, { ALPS_PROTO_V6, 0xff, 0xff, ALPS_DUALPOINT } }, /* Dell XT2 */
134 { { 0x73, 0x02, 0x50 }, 0x00, { ALPS_PROTO_V2, 0xcf, 0xcf, ALPS_FOUR_BUTTONS } }, /* Dell Vostro 1400 */
135 { { 0x52, 0x01, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xff, 0xff,
136 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED } }, /* Toshiba Tecra A11-11L */
137 { { 0x73, 0x02, 0x64 }, 0x8a, { ALPS_PROTO_V4, 0x8f, 0x8f, 0 } },
140 static const struct alps_protocol_info alps_v3_protocol_data = {
141 ALPS_PROTO_V3, 0x8f, 0x8f, ALPS_DUALPOINT
144 static const struct alps_protocol_info alps_v3_rushmore_data = {
145 ALPS_PROTO_V3_RUSHMORE, 0x8f, 0x8f, ALPS_DUALPOINT
148 static const struct alps_protocol_info alps_v5_protocol_data = {
149 ALPS_PROTO_V5, 0xc8, 0xd8, 0
152 static const struct alps_protocol_info alps_v7_protocol_data = {
153 ALPS_PROTO_V7, 0x48, 0x48, ALPS_DUALPOINT
156 static const struct alps_protocol_info alps_v8_protocol_data = {
157 ALPS_PROTO_V8, 0x18, 0x18, 0
160 static void alps_set_abs_params_st(struct alps_data *priv,
161 struct input_dev *dev1);
162 static void alps_set_abs_params_semi_mt(struct alps_data *priv,
163 struct input_dev *dev1);
164 static void alps_set_abs_params_v7(struct alps_data *priv,
165 struct input_dev *dev1);
166 static void alps_set_abs_params_ss4_v2(struct alps_data *priv,
167 struct input_dev *dev1);
169 /* Packet formats are described in Documentation/input/alps.txt */
171 static bool alps_is_valid_first_byte(struct alps_data *priv,
174 return (data & priv->mask0) == priv->byte0;
177 static void alps_report_buttons(struct input_dev *dev1, struct input_dev *dev2,
178 int left, int right, int middle)
180 struct input_dev *dev;
183 * If shared button has already been reported on the
184 * other device (dev2) then this event should be also
185 * sent through that device.
187 dev = (dev2 && test_bit(BTN_LEFT, dev2->key)) ? dev2 : dev1;
188 input_report_key(dev, BTN_LEFT, left);
190 dev = (dev2 && test_bit(BTN_RIGHT, dev2->key)) ? dev2 : dev1;
191 input_report_key(dev, BTN_RIGHT, right);
193 dev = (dev2 && test_bit(BTN_MIDDLE, dev2->key)) ? dev2 : dev1;
194 input_report_key(dev, BTN_MIDDLE, middle);
197 * Sync the _other_ device now, we'll do the first
198 * device later once we report the rest of the events.
204 static void alps_process_packet_v1_v2(struct psmouse *psmouse)
206 struct alps_data *priv = psmouse->private;
207 unsigned char *packet = psmouse->packet;
208 struct input_dev *dev = psmouse->dev;
209 struct input_dev *dev2 = priv->dev2;
210 int x, y, z, ges, fin, left, right, middle;
211 int back = 0, forward = 0;
213 if (priv->proto_version == ALPS_PROTO_V1) {
214 left = packet[2] & 0x10;
215 right = packet[2] & 0x08;
217 x = packet[1] | ((packet[0] & 0x07) << 7);
218 y = packet[4] | ((packet[3] & 0x07) << 7);
221 left = packet[3] & 1;
222 right = packet[3] & 2;
223 middle = packet[3] & 4;
224 x = packet[1] | ((packet[2] & 0x78) << (7 - 3));
225 y = packet[4] | ((packet[3] & 0x70) << (7 - 4));
229 if (priv->flags & ALPS_FW_BK_1) {
230 back = packet[0] & 0x10;
231 forward = packet[2] & 4;
234 if (priv->flags & ALPS_FW_BK_2) {
235 back = packet[3] & 4;
236 forward = packet[2] & 4;
237 if ((middle = forward && back))
244 if ((priv->flags & ALPS_DUALPOINT) && z == 127) {
245 input_report_rel(dev2, REL_X, (x > 383 ? (x - 768) : x));
246 input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y));
248 alps_report_buttons(dev2, dev, left, right, middle);
254 /* Non interleaved V2 dualpoint has separate stick button bits */
255 if (priv->proto_version == ALPS_PROTO_V2 &&
256 priv->flags == (ALPS_PASS | ALPS_DUALPOINT)) {
257 left |= packet[0] & 1;
258 right |= packet[0] & 2;
259 middle |= packet[0] & 4;
262 alps_report_buttons(dev, dev2, left, right, middle);
264 /* Convert hardware tap to a reasonable Z value */
269 * A "tap and drag" operation is reported by the hardware as a transition
270 * from (!fin && ges) to (fin && ges). This should be translated to the
271 * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
273 if (ges && fin && !priv->prev_fin) {
274 input_report_abs(dev, ABS_X, x);
275 input_report_abs(dev, ABS_Y, y);
276 input_report_abs(dev, ABS_PRESSURE, 0);
277 input_report_key(dev, BTN_TOOL_FINGER, 0);
280 priv->prev_fin = fin;
283 input_report_key(dev, BTN_TOUCH, 1);
285 input_report_key(dev, BTN_TOUCH, 0);
288 input_report_abs(dev, ABS_X, x);
289 input_report_abs(dev, ABS_Y, y);
292 input_report_abs(dev, ABS_PRESSURE, z);
293 input_report_key(dev, BTN_TOOL_FINGER, z > 0);
295 if (priv->flags & ALPS_WHEEL)
296 input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
298 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
299 input_report_key(dev, BTN_FORWARD, forward);
300 input_report_key(dev, BTN_BACK, back);
303 if (priv->flags & ALPS_FOUR_BUTTONS) {
304 input_report_key(dev, BTN_0, packet[2] & 4);
305 input_report_key(dev, BTN_1, packet[0] & 0x10);
306 input_report_key(dev, BTN_2, packet[3] & 4);
307 input_report_key(dev, BTN_3, packet[0] & 0x20);
313 static void alps_get_bitmap_points(unsigned int map,
314 struct alps_bitmap_point *low,
315 struct alps_bitmap_point *high,
318 struct alps_bitmap_point *point;
319 int i, bit, prev_bit = 0;
322 for (i = 0; map != 0; i++, map >>= 1) {
326 point->start_bit = i;
340 * Process bitmap data from semi-mt protocols. Returns the number of
341 * fingers detected. A return value of 0 means at least one of the
344 * The bitmaps don't have enough data to track fingers, so this function
345 * only generates points representing a bounding box of all contacts.
346 * These points are returned in fields->mt when the return value
349 static int alps_process_bitmap(struct alps_data *priv,
350 struct alps_fields *fields)
352 int i, fingers_x = 0, fingers_y = 0, fingers, closest;
353 struct alps_bitmap_point x_low = {0,}, x_high = {0,};
354 struct alps_bitmap_point y_low = {0,}, y_high = {0,};
355 struct input_mt_pos corner[4];
357 if (!fields->x_map || !fields->y_map)
360 alps_get_bitmap_points(fields->x_map, &x_low, &x_high, &fingers_x);
361 alps_get_bitmap_points(fields->y_map, &y_low, &y_high, &fingers_y);
364 * Fingers can overlap, so we use the maximum count of fingers
365 * on either axis as the finger count.
367 fingers = max(fingers_x, fingers_y);
370 * If an axis reports only a single contact, we have overlapping or
371 * adjacent fingers. Divide the single contact between the two points.
373 if (fingers_x == 1) {
374 i = (x_low.num_bits - 1) / 2;
375 x_low.num_bits = x_low.num_bits - i;
376 x_high.start_bit = x_low.start_bit + i;
377 x_high.num_bits = max(i, 1);
379 if (fingers_y == 1) {
380 i = (y_low.num_bits - 1) / 2;
381 y_low.num_bits = y_low.num_bits - i;
382 y_high.start_bit = y_low.start_bit + i;
383 y_high.num_bits = max(i, 1);
386 /* top-left corner */
388 (priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
389 (2 * (priv->x_bits - 1));
391 (priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
392 (2 * (priv->y_bits - 1));
394 /* top-right corner */
396 (priv->x_max * (2 * x_high.start_bit + x_high.num_bits - 1)) /
397 (2 * (priv->x_bits - 1));
399 (priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
400 (2 * (priv->y_bits - 1));
402 /* bottom-right corner */
404 (priv->x_max * (2 * x_high.start_bit + x_high.num_bits - 1)) /
405 (2 * (priv->x_bits - 1));
407 (priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) /
408 (2 * (priv->y_bits - 1));
410 /* bottom-left corner */
412 (priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
413 (2 * (priv->x_bits - 1));
415 (priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) /
416 (2 * (priv->y_bits - 1));
418 /* x-bitmap order is reversed on v5 touchpads */
419 if (priv->proto_version == ALPS_PROTO_V5) {
420 for (i = 0; i < 4; i++)
421 corner[i].x = priv->x_max - corner[i].x;
424 /* y-bitmap order is reversed on v3 and v4 touchpads */
425 if (priv->proto_version == ALPS_PROTO_V3 ||
426 priv->proto_version == ALPS_PROTO_V4) {
427 for (i = 0; i < 4; i++)
428 corner[i].y = priv->y_max - corner[i].y;
432 * We only select a corner for the second touch once per 2 finger
433 * touch sequence to avoid the chosen corner (and thus the coordinates)
434 * jumping around when the first touch is in the middle.
436 if (priv->second_touch == -1) {
437 /* Find corner closest to our st coordinates */
438 closest = 0x7fffffff;
439 for (i = 0; i < 4; i++) {
440 int dx = fields->st.x - corner[i].x;
441 int dy = fields->st.y - corner[i].y;
442 int distance = dx * dx + dy * dy;
444 if (distance < closest) {
445 priv->second_touch = i;
449 /* And select the opposite corner to use for the 2nd touch */
450 priv->second_touch = (priv->second_touch + 2) % 4;
453 fields->mt[0] = fields->st;
454 fields->mt[1] = corner[priv->second_touch];
459 static void alps_set_slot(struct input_dev *dev, int slot, int x, int y)
461 input_mt_slot(dev, slot);
462 input_mt_report_slot_state(dev, MT_TOOL_FINGER, true);
463 input_report_abs(dev, ABS_MT_POSITION_X, x);
464 input_report_abs(dev, ABS_MT_POSITION_Y, y);
467 static void alps_report_mt_data(struct psmouse *psmouse, int n)
469 struct alps_data *priv = psmouse->private;
470 struct input_dev *dev = psmouse->dev;
471 struct alps_fields *f = &priv->f;
472 int i, slot[MAX_TOUCHES];
474 input_mt_assign_slots(dev, slot, f->mt, n, 0);
475 for (i = 0; i < n; i++)
476 alps_set_slot(dev, slot[i], f->mt[i].x, f->mt[i].y);
478 input_mt_sync_frame(dev);
481 static void alps_report_semi_mt_data(struct psmouse *psmouse, int fingers)
483 struct alps_data *priv = psmouse->private;
484 struct input_dev *dev = psmouse->dev;
485 struct alps_fields *f = &priv->f;
487 /* Use st data when we don't have mt data */
489 f->mt[0].x = f->st.x;
490 f->mt[0].y = f->st.y;
491 fingers = f->pressure > 0 ? 1 : 0;
492 priv->second_touch = -1;
496 alps_set_slot(dev, 0, f->mt[0].x, f->mt[0].y);
498 alps_set_slot(dev, 1, f->mt[1].x, f->mt[1].y);
499 input_mt_sync_frame(dev);
501 input_mt_report_finger_count(dev, fingers);
503 input_report_key(dev, BTN_LEFT, f->left);
504 input_report_key(dev, BTN_RIGHT, f->right);
505 input_report_key(dev, BTN_MIDDLE, f->middle);
507 input_report_abs(dev, ABS_PRESSURE, f->pressure);
512 static void alps_process_trackstick_packet_v3(struct psmouse *psmouse)
514 struct alps_data *priv = psmouse->private;
515 unsigned char *packet = psmouse->packet;
516 struct input_dev *dev = priv->dev2;
517 int x, y, z, left, right, middle;
519 /* It should be a DualPoint when received trackstick packet */
520 if (!(priv->flags & ALPS_DUALPOINT)) {
521 psmouse_warn(psmouse,
522 "Rejected trackstick packet from non DualPoint device");
526 /* Sanity check packet */
527 if (!(packet[0] & 0x40)) {
528 psmouse_dbg(psmouse, "Bad trackstick packet, discarding\n");
533 * There's a special packet that seems to indicate the end
534 * of a stream of trackstick data. Filter these out.
536 if (packet[1] == 0x7f && packet[2] == 0x7f && packet[4] == 0x7f)
539 x = (s8)(((packet[0] & 0x20) << 2) | (packet[1] & 0x7f));
540 y = (s8)(((packet[0] & 0x10) << 3) | (packet[2] & 0x7f));
541 z = (packet[4] & 0x7c) >> 2;
544 * The x and y values tend to be quite large, and when used
545 * alone the trackstick is difficult to use. Scale them down
551 input_report_rel(dev, REL_X, x);
552 input_report_rel(dev, REL_Y, -y);
555 * Most ALPS models report the trackstick buttons in the touchpad
556 * packets, but a few report them here. No reliable way has been
557 * found to differentiate between the models upfront, so we enable
558 * the quirk in response to seeing a button press in the trackstick
561 left = packet[3] & 0x01;
562 right = packet[3] & 0x02;
563 middle = packet[3] & 0x04;
565 if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) &&
566 (left || right || middle))
567 priv->quirks |= ALPS_QUIRK_TRACKSTICK_BUTTONS;
569 if (priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) {
570 input_report_key(dev, BTN_LEFT, left);
571 input_report_key(dev, BTN_RIGHT, right);
572 input_report_key(dev, BTN_MIDDLE, middle);
579 static void alps_decode_buttons_v3(struct alps_fields *f, unsigned char *p)
581 f->left = !!(p[3] & 0x01);
582 f->right = !!(p[3] & 0x02);
583 f->middle = !!(p[3] & 0x04);
585 f->ts_left = !!(p[3] & 0x10);
586 f->ts_right = !!(p[3] & 0x20);
587 f->ts_middle = !!(p[3] & 0x40);
590 static int alps_decode_pinnacle(struct alps_fields *f, unsigned char *p,
591 struct psmouse *psmouse)
593 f->first_mp = !!(p[4] & 0x40);
594 f->is_mp = !!(p[0] & 0x40);
597 f->fingers = (p[5] & 0x3) + 1;
598 f->x_map = ((p[4] & 0x7e) << 8) |
599 ((p[1] & 0x7f) << 2) |
600 ((p[0] & 0x30) >> 4);
601 f->y_map = ((p[3] & 0x70) << 4) |
602 ((p[2] & 0x7f) << 1) |
605 f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
606 ((p[0] & 0x30) >> 4);
607 f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
608 f->pressure = p[5] & 0x7f;
610 alps_decode_buttons_v3(f, p);
616 static int alps_decode_rushmore(struct alps_fields *f, unsigned char *p,
617 struct psmouse *psmouse)
619 f->first_mp = !!(p[4] & 0x40);
620 f->is_mp = !!(p[5] & 0x40);
623 f->fingers = max((p[5] & 0x3), ((p[5] >> 2) & 0x3)) + 1;
624 f->x_map = ((p[5] & 0x10) << 11) |
625 ((p[4] & 0x7e) << 8) |
626 ((p[1] & 0x7f) << 2) |
627 ((p[0] & 0x30) >> 4);
628 f->y_map = ((p[5] & 0x20) << 6) |
629 ((p[3] & 0x70) << 4) |
630 ((p[2] & 0x7f) << 1) |
633 f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
634 ((p[0] & 0x30) >> 4);
635 f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
636 f->pressure = p[5] & 0x7f;
638 alps_decode_buttons_v3(f, p);
644 static int alps_decode_dolphin(struct alps_fields *f, unsigned char *p,
645 struct psmouse *psmouse)
648 struct alps_data *priv = psmouse->private;
650 f->first_mp = !!(p[0] & 0x02);
651 f->is_mp = !!(p[0] & 0x20);
654 f->st.x = ((p[1] & 0x7f) | ((p[4] & 0x0f) << 7));
655 f->st.y = ((p[2] & 0x7f) | ((p[4] & 0xf0) << 3));
656 f->pressure = (p[0] & 4) ? 0 : p[5] & 0x7f;
657 alps_decode_buttons_v3(f, p);
659 f->fingers = ((p[0] & 0x6) >> 1 |
662 palm_data = (p[1] & 0x7f) |
663 ((p[2] & 0x7f) << 7) |
664 ((p[4] & 0x7f) << 14) |
665 ((p[5] & 0x7f) << 21) |
666 ((p[3] & 0x07) << 28) |
667 (((u64)p[3] & 0x70) << 27) |
668 (((u64)p[0] & 0x01) << 34);
670 /* Y-profile is stored in P(0) to p(n-1), n = y_bits; */
671 f->y_map = palm_data & (BIT(priv->y_bits) - 1);
673 /* X-profile is stored in p(n) to p(n+m-1), m = x_bits; */
674 f->x_map = (palm_data >> priv->y_bits) &
675 (BIT(priv->x_bits) - 1);
681 static void alps_process_touchpad_packet_v3_v5(struct psmouse *psmouse)
683 struct alps_data *priv = psmouse->private;
684 unsigned char *packet = psmouse->packet;
685 struct input_dev *dev2 = priv->dev2;
686 struct alps_fields *f = &priv->f;
689 memset(f, 0, sizeof(*f));
691 priv->decode_fields(f, packet, psmouse);
694 * There's no single feature of touchpad position and bitmap packets
695 * that can be used to distinguish between them. We rely on the fact
696 * that a bitmap packet should always follow a position packet with
697 * bit 6 of packet[4] set.
699 if (priv->multi_packet) {
701 * Sometimes a position packet will indicate a multi-packet
702 * sequence, but then what follows is another position
703 * packet. Check for this, and when it happens process the
704 * position packet as usual.
707 fingers = f->fingers;
709 * Bitmap processing uses position packet's coordinate
710 * data, so we need to do decode it first.
712 priv->decode_fields(f, priv->multi_data, psmouse);
713 if (alps_process_bitmap(priv, f) == 0)
714 fingers = 0; /* Use st data */
716 priv->multi_packet = 0;
721 * Bit 6 of byte 0 is not usually set in position packets. The only
722 * times it seems to be set is in situations where the data is
723 * suspect anyway, e.g. a palm resting flat on the touchpad. Given
724 * this combined with the fact that this bit is useful for filtering
725 * out misidentified bitmap packets, we reject anything with this
731 if (!priv->multi_packet && f->first_mp) {
732 priv->multi_packet = 1;
733 memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
737 priv->multi_packet = 0;
740 * Sometimes the hardware sends a single packet with z = 0
741 * in the middle of a stream. Real releases generate packets
742 * with x, y, and z all zero, so these seem to be flukes.
745 if (f->st.x && f->st.y && !f->pressure)
748 alps_report_semi_mt_data(psmouse, fingers);
750 if ((priv->flags & ALPS_DUALPOINT) &&
751 !(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) {
752 input_report_key(dev2, BTN_LEFT, f->ts_left);
753 input_report_key(dev2, BTN_RIGHT, f->ts_right);
754 input_report_key(dev2, BTN_MIDDLE, f->ts_middle);
759 static void alps_process_packet_v3(struct psmouse *psmouse)
761 unsigned char *packet = psmouse->packet;
764 * v3 protocol packets come in three types, two representing
765 * touchpad data and one representing trackstick data.
766 * Trackstick packets seem to be distinguished by always
767 * having 0x3f in the last byte. This value has never been
768 * observed in the last byte of either of the other types
771 if (packet[5] == 0x3f) {
772 alps_process_trackstick_packet_v3(psmouse);
776 alps_process_touchpad_packet_v3_v5(psmouse);
779 static void alps_process_packet_v6(struct psmouse *psmouse)
781 struct alps_data *priv = psmouse->private;
782 unsigned char *packet = psmouse->packet;
783 struct input_dev *dev = psmouse->dev;
784 struct input_dev *dev2 = priv->dev2;
785 int x, y, z, left, right, middle;
788 * We can use Byte5 to distinguish if the packet is from Touchpad
793 if (packet[5] == 0x7F) {
794 /* It should be a DualPoint when received Trackpoint packet */
795 if (!(priv->flags & ALPS_DUALPOINT)) {
796 psmouse_warn(psmouse,
797 "Rejected trackstick packet from non DualPoint device");
801 /* Trackpoint packet */
802 x = packet[1] | ((packet[3] & 0x20) << 2);
803 y = packet[2] | ((packet[3] & 0x40) << 1);
805 left = packet[3] & 0x01;
806 right = packet[3] & 0x02;
807 middle = packet[3] & 0x04;
809 /* To prevent the cursor jump when finger lifted */
810 if (x == 0x7F && y == 0x7F && z == 0x7F)
813 /* Divide 4 since trackpoint's speed is too fast */
814 input_report_rel(dev2, REL_X, (char)x / 4);
815 input_report_rel(dev2, REL_Y, -((char)y / 4));
817 input_report_key(dev2, BTN_LEFT, left);
818 input_report_key(dev2, BTN_RIGHT, right);
819 input_report_key(dev2, BTN_MIDDLE, middle);
825 /* Touchpad packet */
826 x = packet[1] | ((packet[3] & 0x78) << 4);
827 y = packet[2] | ((packet[4] & 0x78) << 4);
829 left = packet[3] & 0x01;
830 right = packet[3] & 0x02;
833 input_report_key(dev, BTN_TOUCH, 1);
835 input_report_key(dev, BTN_TOUCH, 0);
838 input_report_abs(dev, ABS_X, x);
839 input_report_abs(dev, ABS_Y, y);
842 input_report_abs(dev, ABS_PRESSURE, z);
843 input_report_key(dev, BTN_TOOL_FINGER, z > 0);
845 /* v6 touchpad does not have middle button */
846 input_report_key(dev, BTN_LEFT, left);
847 input_report_key(dev, BTN_RIGHT, right);
852 static void alps_process_packet_v4(struct psmouse *psmouse)
854 struct alps_data *priv = psmouse->private;
855 unsigned char *packet = psmouse->packet;
856 struct alps_fields *f = &priv->f;
860 * v4 has a 6-byte encoding for bitmap data, but this data is
861 * broken up between 3 normal packets. Use priv->multi_packet to
862 * track our position in the bitmap packet.
864 if (packet[6] & 0x40) {
865 /* sync, reset position */
866 priv->multi_packet = 0;
869 if (WARN_ON_ONCE(priv->multi_packet > 2))
872 offset = 2 * priv->multi_packet;
873 priv->multi_data[offset] = packet[6];
874 priv->multi_data[offset + 1] = packet[7];
876 f->left = !!(packet[4] & 0x01);
877 f->right = !!(packet[4] & 0x02);
879 f->st.x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) |
880 ((packet[0] & 0x30) >> 4);
881 f->st.y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f);
882 f->pressure = packet[5] & 0x7f;
884 if (++priv->multi_packet > 2) {
885 priv->multi_packet = 0;
887 f->x_map = ((priv->multi_data[2] & 0x1f) << 10) |
888 ((priv->multi_data[3] & 0x60) << 3) |
889 ((priv->multi_data[0] & 0x3f) << 2) |
890 ((priv->multi_data[1] & 0x60) >> 5);
891 f->y_map = ((priv->multi_data[5] & 0x01) << 10) |
892 ((priv->multi_data[3] & 0x1f) << 5) |
893 (priv->multi_data[1] & 0x1f);
895 f->fingers = alps_process_bitmap(priv, f);
898 alps_report_semi_mt_data(psmouse, f->fingers);
901 static bool alps_is_valid_package_v7(struct psmouse *psmouse)
903 switch (psmouse->pktcnt) {
905 return (psmouse->packet[2] & 0x40) == 0x40;
907 return (psmouse->packet[3] & 0x48) == 0x48;
909 return (psmouse->packet[5] & 0x40) == 0x00;
914 static unsigned char alps_get_packet_id_v7(char *byte)
916 unsigned char packet_id;
919 packet_id = V7_PACKET_ID_TWO;
920 else if (byte[4] & 0x01)
921 packet_id = V7_PACKET_ID_MULTI;
922 else if ((byte[0] & 0x10) && !(byte[4] & 0x43))
923 packet_id = V7_PACKET_ID_NEW;
924 else if (byte[1] == 0x00 && byte[4] == 0x00)
925 packet_id = V7_PACKET_ID_IDLE;
927 packet_id = V7_PACKET_ID_UNKNOWN;
932 static void alps_get_finger_coordinate_v7(struct input_mt_pos *mt,
934 unsigned char pkt_id)
936 mt[0].x = ((pkt[2] & 0x80) << 4);
937 mt[0].x |= ((pkt[2] & 0x3F) << 5);
938 mt[0].x |= ((pkt[3] & 0x30) >> 1);
939 mt[0].x |= (pkt[3] & 0x07);
940 mt[0].y = (pkt[1] << 3) | (pkt[0] & 0x07);
942 mt[1].x = ((pkt[3] & 0x80) << 4);
943 mt[1].x |= ((pkt[4] & 0x80) << 3);
944 mt[1].x |= ((pkt[4] & 0x3F) << 4);
945 mt[1].y = ((pkt[5] & 0x80) << 3);
946 mt[1].y |= ((pkt[5] & 0x3F) << 4);
949 case V7_PACKET_ID_TWO:
952 /* Detect false-postive touches where x & y report max value */
953 if (mt[1].y == 0x7ff && mt[1].x == 0xff0) {
955 /* y gets set to 0 at the end of this function */
959 case V7_PACKET_ID_MULTI:
962 mt[1].y |= ((pkt[4] & 0x02) << 4);
966 case V7_PACKET_ID_NEW:
968 mt[1].x |= (pkt[0] & 0x20);
973 mt[0].y = 0x7FF - mt[0].y;
974 mt[1].y = 0x7FF - mt[1].y;
977 static int alps_get_mt_count(struct input_mt_pos *mt)
981 for (i = 0; i < MAX_TOUCHES; i++) {
982 if (mt[i].x != 0 || mt[i].y != 0)
989 static int alps_decode_packet_v7(struct alps_fields *f,
991 struct psmouse *psmouse)
993 struct alps_data *priv = psmouse->private;
994 unsigned char pkt_id;
996 pkt_id = alps_get_packet_id_v7(p);
997 if (pkt_id == V7_PACKET_ID_IDLE)
999 if (pkt_id == V7_PACKET_ID_UNKNOWN)
1002 * NEW packets are send to indicate a discontinuity in the finger
1003 * coordinate reporting. Specifically a finger may have moved from
1004 * slot 0 to 1 or vice versa. INPUT_MT_TRACK takes care of this for
1007 * NEW packets have 3 problems:
1008 * 1) They do not contain middle / right button info (on non clickpads)
1009 * this can be worked around by preserving the old button state
1010 * 2) They do not contain an accurate fingercount, and they are
1011 * typically send when the number of fingers changes. We cannot use
1012 * the old finger count as that may mismatch with the amount of
1013 * touch coordinates we've available in the NEW packet
1014 * 3) Their x data for the second touch is inaccurate leading to
1015 * a possible jump of the x coordinate by 16 units when the first
1016 * non NEW packet comes in
1017 * Since problems 2 & 3 cannot be worked around, just ignore them.
1019 if (pkt_id == V7_PACKET_ID_NEW)
1022 alps_get_finger_coordinate_v7(f->mt, p, pkt_id);
1024 if (pkt_id == V7_PACKET_ID_TWO)
1025 f->fingers = alps_get_mt_count(f->mt);
1026 else /* pkt_id == V7_PACKET_ID_MULTI */
1027 f->fingers = 3 + (p[5] & 0x03);
1029 f->left = (p[0] & 0x80) >> 7;
1030 if (priv->flags & ALPS_BUTTONPAD) {
1036 f->right = (p[0] & 0x20) >> 5;
1037 f->middle = (p[0] & 0x10) >> 4;
1040 /* Sometimes a single touch is reported in mt[1] rather then mt[0] */
1041 if (f->fingers == 1 && f->mt[0].x == 0 && f->mt[0].y == 0) {
1042 f->mt[0].x = f->mt[1].x;
1043 f->mt[0].y = f->mt[1].y;
1051 static void alps_process_trackstick_packet_v7(struct psmouse *psmouse)
1053 struct alps_data *priv = psmouse->private;
1054 unsigned char *packet = psmouse->packet;
1055 struct input_dev *dev2 = priv->dev2;
1056 int x, y, z, left, right, middle;
1058 /* It should be a DualPoint when received trackstick packet */
1059 if (!(priv->flags & ALPS_DUALPOINT)) {
1060 psmouse_warn(psmouse,
1061 "Rejected trackstick packet from non DualPoint device");
1065 x = ((packet[2] & 0xbf)) | ((packet[3] & 0x10) << 2);
1066 y = (packet[3] & 0x07) | (packet[4] & 0xb8) |
1067 ((packet[3] & 0x20) << 1);
1068 z = (packet[5] & 0x3f) | ((packet[3] & 0x80) >> 1);
1070 left = (packet[1] & 0x01);
1071 right = (packet[1] & 0x02) >> 1;
1072 middle = (packet[1] & 0x04) >> 2;
1074 input_report_rel(dev2, REL_X, (char)x);
1075 input_report_rel(dev2, REL_Y, -((char)y));
1077 input_report_key(dev2, BTN_LEFT, left);
1078 input_report_key(dev2, BTN_RIGHT, right);
1079 input_report_key(dev2, BTN_MIDDLE, middle);
1084 static void alps_process_touchpad_packet_v7(struct psmouse *psmouse)
1086 struct alps_data *priv = psmouse->private;
1087 struct input_dev *dev = psmouse->dev;
1088 struct alps_fields *f = &priv->f;
1090 memset(f, 0, sizeof(*f));
1092 if (priv->decode_fields(f, psmouse->packet, psmouse))
1095 alps_report_mt_data(psmouse, alps_get_mt_count(f->mt));
1097 input_mt_report_finger_count(dev, f->fingers);
1099 input_report_key(dev, BTN_LEFT, f->left);
1100 input_report_key(dev, BTN_RIGHT, f->right);
1101 input_report_key(dev, BTN_MIDDLE, f->middle);
1106 static void alps_process_packet_v7(struct psmouse *psmouse)
1108 unsigned char *packet = psmouse->packet;
1110 if (packet[0] == 0x48 && (packet[4] & 0x47) == 0x06)
1111 alps_process_trackstick_packet_v7(psmouse);
1113 alps_process_touchpad_packet_v7(psmouse);
1116 static unsigned char alps_get_pkt_id_ss4_v2(unsigned char *byte)
1118 unsigned char pkt_id = SS4_PACKET_ID_IDLE;
1120 if (byte[0] == 0x18 && byte[1] == 0x10 && byte[2] == 0x00 &&
1121 (byte[3] & 0x88) == 0x08 && byte[4] == 0x10 && byte[5] == 0x00) {
1122 pkt_id = SS4_PACKET_ID_IDLE;
1123 } else if (!(byte[3] & 0x10)) {
1124 pkt_id = SS4_PACKET_ID_ONE;
1125 } else if (!(byte[3] & 0x20)) {
1126 pkt_id = SS4_PACKET_ID_TWO;
1128 pkt_id = SS4_PACKET_ID_MULTI;
1134 static int alps_decode_ss4_v2(struct alps_fields *f,
1135 unsigned char *p, struct psmouse *psmouse)
1137 struct alps_data *priv = psmouse->private;
1138 unsigned char pkt_id;
1139 unsigned int no_data_x, no_data_y;
1141 pkt_id = alps_get_pkt_id_ss4_v2(p);
1143 /* Current packet is 1Finger coordinate packet */
1145 case SS4_PACKET_ID_ONE:
1146 f->mt[0].x = SS4_1F_X_V2(p);
1147 f->mt[0].y = SS4_1F_Y_V2(p);
1148 f->pressure = ((SS4_1F_Z_V2(p)) * 2) & 0x7f;
1154 case SS4_PACKET_ID_TWO:
1155 if (priv->flags & ALPS_BUTTONPAD) {
1156 f->mt[0].x = SS4_BTL_MF_X_V2(p, 0);
1157 f->mt[0].y = SS4_BTL_MF_Y_V2(p, 0);
1158 f->mt[1].x = SS4_BTL_MF_X_V2(p, 1);
1159 f->mt[1].y = SS4_BTL_MF_Y_V2(p, 1);
1161 f->mt[0].x = SS4_STD_MF_X_V2(p, 0);
1162 f->mt[0].y = SS4_STD_MF_Y_V2(p, 0);
1163 f->mt[1].x = SS4_STD_MF_X_V2(p, 1);
1164 f->mt[1].y = SS4_STD_MF_Y_V2(p, 1);
1166 f->pressure = SS4_MF_Z_V2(p, 0) ? 0x30 : 0;
1168 if (SS4_IS_MF_CONTINUE(p)) {
1178 case SS4_PACKET_ID_MULTI:
1179 if (priv->flags & ALPS_BUTTONPAD) {
1180 f->mt[2].x = SS4_BTL_MF_X_V2(p, 0);
1181 f->mt[2].y = SS4_BTL_MF_Y_V2(p, 0);
1182 f->mt[3].x = SS4_BTL_MF_X_V2(p, 1);
1183 f->mt[3].y = SS4_BTL_MF_Y_V2(p, 1);
1184 no_data_x = SS4_MFPACKET_NO_AX_BL;
1185 no_data_y = SS4_MFPACKET_NO_AY_BL;
1187 f->mt[2].x = SS4_STD_MF_X_V2(p, 0);
1188 f->mt[2].y = SS4_STD_MF_Y_V2(p, 0);
1189 f->mt[3].x = SS4_STD_MF_X_V2(p, 1);
1190 f->mt[3].y = SS4_STD_MF_Y_V2(p, 1);
1191 no_data_x = SS4_MFPACKET_NO_AX;
1192 no_data_y = SS4_MFPACKET_NO_AY;
1198 if (SS4_IS_5F_DETECTED(p)) {
1200 } else if (f->mt[3].x == no_data_x &&
1201 f->mt[3].y == no_data_y) {
1210 case SS4_PACKET_ID_IDLE:
1212 memset(f, 0, sizeof(struct alps_fields));
1216 f->left = !!(SS4_BTN_V2(p) & 0x01);
1217 if (!(priv->flags & ALPS_BUTTONPAD)) {
1218 f->right = !!(SS4_BTN_V2(p) & 0x02);
1219 f->middle = !!(SS4_BTN_V2(p) & 0x04);
1225 static void alps_process_packet_ss4_v2(struct psmouse *psmouse)
1227 struct alps_data *priv = psmouse->private;
1228 unsigned char *packet = psmouse->packet;
1229 struct input_dev *dev = psmouse->dev;
1230 struct alps_fields *f = &priv->f;
1232 memset(f, 0, sizeof(struct alps_fields));
1233 priv->decode_fields(f, packet, psmouse);
1234 if (priv->multi_packet) {
1236 * Sometimes the first packet will indicate a multi-packet
1237 * sequence, but sometimes the next multi-packet would not
1238 * come. Check for this, and when it happens process the
1239 * position packet as usual.
1242 /* Now process the 1st packet */
1243 priv->decode_fields(f, priv->multi_data, psmouse);
1245 priv->multi_packet = 0;
1250 * "f.is_mp" would always be '0' after merging the 1st and 2nd packet.
1251 * When it is set, it means 2nd packet comes without 1st packet come.
1256 /* Save the first packet */
1257 if (!priv->multi_packet && f->first_mp) {
1258 priv->multi_packet = 1;
1259 memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
1263 priv->multi_packet = 0;
1265 alps_report_mt_data(psmouse, (f->fingers <= 4) ? f->fingers : 4);
1267 input_mt_report_finger_count(dev, f->fingers);
1269 input_report_key(dev, BTN_LEFT, f->left);
1270 input_report_key(dev, BTN_RIGHT, f->right);
1271 input_report_key(dev, BTN_MIDDLE, f->middle);
1273 input_report_abs(dev, ABS_PRESSURE, f->pressure);
1277 static bool alps_is_valid_package_ss4_v2(struct psmouse *psmouse)
1279 if (psmouse->pktcnt == 4 && ((psmouse->packet[3] & 0x08) != 0x08))
1281 if (psmouse->pktcnt == 6 && ((psmouse->packet[5] & 0x10) != 0x0))
1286 static DEFINE_MUTEX(alps_mutex);
1288 static void alps_register_bare_ps2_mouse(struct work_struct *work)
1290 struct alps_data *priv =
1291 container_of(work, struct alps_data, dev3_register_work.work);
1292 struct psmouse *psmouse = priv->psmouse;
1293 struct input_dev *dev3;
1296 mutex_lock(&alps_mutex);
1301 dev3 = input_allocate_device();
1303 psmouse_err(psmouse, "failed to allocate secondary device\n");
1308 snprintf(priv->phys3, sizeof(priv->phys3), "%s/%s",
1309 psmouse->ps2dev.serio->phys,
1310 (priv->dev2 ? "input2" : "input1"));
1311 dev3->phys = priv->phys3;
1314 * format of input device name is: "protocol vendor name"
1315 * see function psmouse_switch_protocol() in psmouse-base.c
1317 dev3->name = "PS/2 ALPS Mouse";
1319 dev3->id.bustype = BUS_I8042;
1320 dev3->id.vendor = 0x0002;
1321 dev3->id.product = PSMOUSE_PS2;
1322 dev3->id.version = 0x0000;
1323 dev3->dev.parent = &psmouse->ps2dev.serio->dev;
1325 input_set_capability(dev3, EV_REL, REL_X);
1326 input_set_capability(dev3, EV_REL, REL_Y);
1327 input_set_capability(dev3, EV_KEY, BTN_LEFT);
1328 input_set_capability(dev3, EV_KEY, BTN_RIGHT);
1329 input_set_capability(dev3, EV_KEY, BTN_MIDDLE);
1331 __set_bit(INPUT_PROP_POINTER, dev3->propbit);
1333 error = input_register_device(dev3);
1335 psmouse_err(psmouse,
1336 "failed to register secondary device: %d\n",
1338 input_free_device(dev3);
1346 * Save the error code so that we can detect that we
1347 * already tried to create the device.
1350 priv->dev3 = ERR_PTR(error);
1352 mutex_unlock(&alps_mutex);
1355 static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
1356 unsigned char packet[],
1357 bool report_buttons)
1359 struct alps_data *priv = psmouse->private;
1360 struct input_dev *dev, *dev2 = NULL;
1362 /* Figure out which device to use to report the bare packet */
1363 if (priv->proto_version == ALPS_PROTO_V2 &&
1364 (priv->flags & ALPS_DUALPOINT)) {
1365 /* On V2 devices the DualPoint Stick reports bare packets */
1367 dev2 = psmouse->dev;
1368 } else if (unlikely(IS_ERR_OR_NULL(priv->dev3))) {
1369 /* Register dev3 mouse if we received PS/2 packet first time */
1370 if (!IS_ERR(priv->dev3))
1371 psmouse_queue_work(psmouse, &priv->dev3_register_work,
1379 alps_report_buttons(dev, dev2,
1380 packet[0] & 1, packet[0] & 2, packet[0] & 4);
1382 input_report_rel(dev, REL_X,
1383 packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
1384 input_report_rel(dev, REL_Y,
1385 packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
1390 static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
1392 struct alps_data *priv = psmouse->private;
1394 if (psmouse->pktcnt < 6)
1395 return PSMOUSE_GOOD_DATA;
1397 if (psmouse->pktcnt == 6) {
1399 * Start a timer to flush the packet if it ends up last
1400 * 6-byte packet in the stream. Timer needs to fire
1401 * psmouse core times out itself. 20 ms should be enough
1402 * to decide if we are getting more data or not.
1404 mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
1405 return PSMOUSE_GOOD_DATA;
1408 del_timer(&priv->timer);
1410 if (psmouse->packet[6] & 0x80) {
1413 * Highest bit is set - that means we either had
1414 * complete ALPS packet and this is start of the
1415 * next packet or we got garbage.
1418 if (((psmouse->packet[3] |
1419 psmouse->packet[4] |
1420 psmouse->packet[5]) & 0x80) ||
1421 (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) {
1422 psmouse_dbg(psmouse,
1423 "refusing packet %4ph (suspected interleaved ps/2)\n",
1424 psmouse->packet + 3);
1425 return PSMOUSE_BAD_DATA;
1428 priv->process_packet(psmouse);
1430 /* Continue with the next packet */
1431 psmouse->packet[0] = psmouse->packet[6];
1432 psmouse->pktcnt = 1;
1437 * High bit is 0 - that means that we indeed got a PS/2
1438 * packet in the middle of ALPS packet.
1440 * There is also possibility that we got 6-byte ALPS
1441 * packet followed by 3-byte packet from trackpoint. We
1442 * can not distinguish between these 2 scenarios but
1443 * because the latter is unlikely to happen in course of
1444 * normal operation (user would need to press all
1445 * buttons on the pad and start moving trackpoint
1446 * without touching the pad surface) we assume former.
1447 * Even if we are wrong the wost thing that would happen
1448 * the cursor would jump but we should not get protocol
1449 * de-synchronization.
1452 alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
1456 * Continue with the standard ALPS protocol handling,
1457 * but make sure we won't process it as an interleaved
1458 * packet again, which may happen if all buttons are
1459 * pressed. To avoid this let's reset the 4th bit which
1462 psmouse->packet[3] = psmouse->packet[6] & 0xf7;
1463 psmouse->pktcnt = 4;
1466 return PSMOUSE_GOOD_DATA;
1469 static void alps_flush_packet(unsigned long data)
1471 struct psmouse *psmouse = (struct psmouse *)data;
1472 struct alps_data *priv = psmouse->private;
1474 serio_pause_rx(psmouse->ps2dev.serio);
1476 if (psmouse->pktcnt == psmouse->pktsize) {
1479 * We did not any more data in reasonable amount of time.
1480 * Validate the last 3 bytes and process as a standard
1483 if ((psmouse->packet[3] |
1484 psmouse->packet[4] |
1485 psmouse->packet[5]) & 0x80) {
1486 psmouse_dbg(psmouse,
1487 "refusing packet %3ph (suspected interleaved ps/2)\n",
1488 psmouse->packet + 3);
1490 priv->process_packet(psmouse);
1492 psmouse->pktcnt = 0;
1495 serio_continue_rx(psmouse->ps2dev.serio);
1498 static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
1500 struct alps_data *priv = psmouse->private;
1503 * Check if we are dealing with a bare PS/2 packet, presumably from
1504 * a device connected to the external PS/2 port. Because bare PS/2
1505 * protocol does not have enough constant bits to self-synchronize
1506 * properly we only do this if the device is fully synchronized.
1507 * Can not distinguish V8's first byte from PS/2 packet's
1509 if (priv->proto_version != ALPS_PROTO_V8 &&
1510 !psmouse->out_of_sync_cnt &&
1511 (psmouse->packet[0] & 0xc8) == 0x08) {
1513 if (psmouse->pktcnt == 3) {
1514 alps_report_bare_ps2_packet(psmouse, psmouse->packet,
1516 return PSMOUSE_FULL_PACKET;
1518 return PSMOUSE_GOOD_DATA;
1521 /* Check for PS/2 packet stuffed in the middle of ALPS packet. */
1523 if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
1524 psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
1525 return alps_handle_interleaved_ps2(psmouse);
1528 if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
1529 psmouse_dbg(psmouse,
1530 "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
1531 psmouse->packet[0], priv->mask0, priv->byte0);
1532 return PSMOUSE_BAD_DATA;
1535 /* Bytes 2 - pktsize should have 0 in the highest bit */
1536 if (priv->proto_version < ALPS_PROTO_V5 &&
1537 psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
1538 (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
1539 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1540 psmouse->pktcnt - 1,
1541 psmouse->packet[psmouse->pktcnt - 1]);
1543 if (priv->proto_version == ALPS_PROTO_V3_RUSHMORE &&
1544 psmouse->pktcnt == psmouse->pktsize) {
1546 * Some Dell boxes, such as Latitude E6440 or E7440
1547 * with closed lid, quite often smash last byte of
1548 * otherwise valid packet with 0xff. Given that the
1549 * next packet is very likely to be valid let's
1550 * report PSMOUSE_FULL_PACKET but not process data,
1551 * rather than reporting PSMOUSE_BAD_DATA and
1554 return PSMOUSE_FULL_PACKET;
1557 return PSMOUSE_BAD_DATA;
1560 if ((priv->proto_version == ALPS_PROTO_V7 &&
1561 !alps_is_valid_package_v7(psmouse)) ||
1562 (priv->proto_version == ALPS_PROTO_V8 &&
1563 !alps_is_valid_package_ss4_v2(psmouse))) {
1564 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1565 psmouse->pktcnt - 1,
1566 psmouse->packet[psmouse->pktcnt - 1]);
1567 return PSMOUSE_BAD_DATA;
1570 if (psmouse->pktcnt == psmouse->pktsize) {
1571 priv->process_packet(psmouse);
1572 return PSMOUSE_FULL_PACKET;
1575 return PSMOUSE_GOOD_DATA;
1578 static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
1580 struct ps2dev *ps2dev = &psmouse->ps2dev;
1581 struct alps_data *priv = psmouse->private;
1583 unsigned char *param;
1584 unsigned char dummy[4];
1586 BUG_ON(nibble > 0xf);
1588 command = priv->nibble_commands[nibble].command;
1589 param = (command & 0x0f00) ?
1590 dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
1592 if (ps2_command(ps2dev, param, command))
1598 static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
1600 struct ps2dev *ps2dev = &psmouse->ps2dev;
1601 struct alps_data *priv = psmouse->private;
1604 if (ps2_command(ps2dev, NULL, priv->addr_command))
1607 for (i = 12; i >= 0; i -= 4) {
1608 nibble = (addr >> i) & 0xf;
1609 if (alps_command_mode_send_nibble(psmouse, nibble))
1616 static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1618 struct ps2dev *ps2dev = &psmouse->ps2dev;
1619 unsigned char param[4];
1621 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1625 * The address being read is returned in the first two bytes
1626 * of the result. Check that this address matches the expected
1629 if (addr != ((param[0] << 8) | param[1]))
1635 static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1637 if (alps_command_mode_set_addr(psmouse, addr))
1639 return __alps_command_mode_read_reg(psmouse, addr);
1642 static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
1644 if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
1646 if (alps_command_mode_send_nibble(psmouse, value & 0xf))
1651 static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
1654 if (alps_command_mode_set_addr(psmouse, addr))
1656 return __alps_command_mode_write_reg(psmouse, value);
1659 static int alps_rpt_cmd(struct psmouse *psmouse, int init_command,
1660 int repeated_command, unsigned char *param)
1662 struct ps2dev *ps2dev = &psmouse->ps2dev;
1665 if (init_command && ps2_command(ps2dev, param, init_command))
1668 if (ps2_command(ps2dev, NULL, repeated_command) ||
1669 ps2_command(ps2dev, NULL, repeated_command) ||
1670 ps2_command(ps2dev, NULL, repeated_command))
1673 param[0] = param[1] = param[2] = 0xff;
1674 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1677 psmouse_dbg(psmouse, "%2.2X report: %3ph\n",
1678 repeated_command, param);
1682 static bool alps_check_valid_firmware_id(unsigned char id[])
1687 if (id[0] == 0x88 &&
1690 (id[1] & 0xf0) == 0xb0 ||
1691 (id[1] & 0xf0) == 0xc0)) {
1698 static int alps_enter_command_mode(struct psmouse *psmouse)
1700 unsigned char param[4];
1702 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) {
1703 psmouse_err(psmouse, "failed to enter command mode\n");
1707 if (!alps_check_valid_firmware_id(param)) {
1708 psmouse_dbg(psmouse,
1709 "unknown response while entering command mode\n");
1715 static inline int alps_exit_command_mode(struct psmouse *psmouse)
1717 struct ps2dev *ps2dev = &psmouse->ps2dev;
1718 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
1724 * For DualPoint devices select the device that should respond to
1725 * subsequent commands. It looks like glidepad is behind stickpointer,
1726 * I'd thought it would be other way around...
1728 static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
1730 struct ps2dev *ps2dev = &psmouse->ps2dev;
1731 int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
1733 if (ps2_command(ps2dev, NULL, cmd) ||
1734 ps2_command(ps2dev, NULL, cmd) ||
1735 ps2_command(ps2dev, NULL, cmd) ||
1736 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1739 /* we may get 3 more bytes, just ignore them */
1740 ps2_drain(ps2dev, 3, 100);
1745 static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
1747 struct ps2dev *ps2dev = &psmouse->ps2dev;
1749 /* Try ALPS magic knock - 4 disable before enable */
1750 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1751 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1752 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1753 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1754 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
1758 * Switch mouse to poll (remote) mode so motion data will not
1761 return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
1764 static int alps_monitor_mode_send_word(struct psmouse *psmouse, u16 word)
1769 * b0-b11 are valid bits, send sequence is inverse.
1770 * e.g. when word = 0x0123, nibble send sequence is 3, 2, 1
1772 for (i = 0; i <= 8; i += 4) {
1773 nibble = (word >> i) & 0xf;
1774 if (alps_command_mode_send_nibble(psmouse, nibble))
1781 static int alps_monitor_mode_write_reg(struct psmouse *psmouse,
1782 u16 addr, u16 value)
1784 struct ps2dev *ps2dev = &psmouse->ps2dev;
1786 /* 0x0A0 is the command to write the word */
1787 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE) ||
1788 alps_monitor_mode_send_word(psmouse, 0x0A0) ||
1789 alps_monitor_mode_send_word(psmouse, addr) ||
1790 alps_monitor_mode_send_word(psmouse, value) ||
1791 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1797 static int alps_monitor_mode(struct psmouse *psmouse, bool enable)
1799 struct ps2dev *ps2dev = &psmouse->ps2dev;
1802 /* EC E9 F5 F5 E7 E6 E7 E9 to enter monitor mode */
1803 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
1804 ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO) ||
1805 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1806 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1807 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1808 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1809 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1810 ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO))
1813 /* EC to exit monitor mode */
1814 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP))
1821 static int alps_absolute_mode_v6(struct psmouse *psmouse)
1823 u16 reg_val = 0x181;
1826 /* enter monitor mode, to write the register */
1827 if (alps_monitor_mode(psmouse, true))
1830 ret = alps_monitor_mode_write_reg(psmouse, 0x000, reg_val);
1832 if (alps_monitor_mode(psmouse, false))
1838 static int alps_get_status(struct psmouse *psmouse, char *param)
1840 /* Get status: 0xF5 0xF5 0xF5 0xE9 */
1841 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param))
1848 * Turn touchpad tapping on or off. The sequences are:
1849 * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
1850 * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
1851 * My guess that 0xE9 (GetInfo) is here as a sync point.
1852 * For models that also have stickpointer (DualPoints) its tapping
1853 * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
1854 * we don't fiddle with it.
1856 static int alps_tap_mode(struct psmouse *psmouse, int enable)
1858 struct ps2dev *ps2dev = &psmouse->ps2dev;
1859 int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
1860 unsigned char tap_arg = enable ? 0x0A : 0x00;
1861 unsigned char param[4];
1863 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
1864 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1865 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1866 ps2_command(ps2dev, &tap_arg, cmd))
1869 if (alps_get_status(psmouse, param))
1876 * alps_poll() - poll the touchpad for current motion packet.
1879 static int alps_poll(struct psmouse *psmouse)
1881 struct alps_data *priv = psmouse->private;
1882 unsigned char buf[sizeof(psmouse->packet)];
1885 if (priv->flags & ALPS_PASS)
1886 alps_passthrough_mode_v2(psmouse, true);
1888 poll_failed = ps2_command(&psmouse->ps2dev, buf,
1889 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
1891 if (priv->flags & ALPS_PASS)
1892 alps_passthrough_mode_v2(psmouse, false);
1894 if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
1897 if ((psmouse->badbyte & 0xc8) == 0x08) {
1899 * Poll the track stick ...
1901 if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
1905 memcpy(psmouse->packet, buf, sizeof(buf));
1909 static int alps_hw_init_v1_v2(struct psmouse *psmouse)
1911 struct alps_data *priv = psmouse->private;
1913 if ((priv->flags & ALPS_PASS) &&
1914 alps_passthrough_mode_v2(psmouse, true)) {
1918 if (alps_tap_mode(psmouse, true)) {
1919 psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
1923 if (alps_absolute_mode_v1_v2(psmouse)) {
1924 psmouse_err(psmouse, "Failed to enable absolute mode\n");
1928 if ((priv->flags & ALPS_PASS) &&
1929 alps_passthrough_mode_v2(psmouse, false)) {
1933 /* ALPS needs stream mode, otherwise it won't report any data */
1934 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
1935 psmouse_err(psmouse, "Failed to enable stream mode\n");
1942 static int alps_hw_init_v6(struct psmouse *psmouse)
1944 unsigned char param[2] = {0xC8, 0x14};
1946 /* Enter passthrough mode to let trackpoint enter 6byte raw mode */
1947 if (alps_passthrough_mode_v2(psmouse, true))
1950 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1951 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1952 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1953 ps2_command(&psmouse->ps2dev, ¶m[0], PSMOUSE_CMD_SETRATE) ||
1954 ps2_command(&psmouse->ps2dev, ¶m[1], PSMOUSE_CMD_SETRATE))
1957 if (alps_passthrough_mode_v2(psmouse, false))
1960 if (alps_absolute_mode_v6(psmouse)) {
1961 psmouse_err(psmouse, "Failed to enable absolute mode\n");
1969 * Enable or disable passthrough mode to the trackstick.
1971 static int alps_passthrough_mode_v3(struct psmouse *psmouse,
1972 int reg_base, bool enable)
1974 int reg_val, ret = -1;
1976 if (alps_enter_command_mode(psmouse))
1979 reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x0008);
1988 ret = __alps_command_mode_write_reg(psmouse, reg_val);
1991 if (alps_exit_command_mode(psmouse))
1996 /* Must be in command mode when calling this function */
1997 static int alps_absolute_mode_v3(struct psmouse *psmouse)
2001 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
2006 if (__alps_command_mode_write_reg(psmouse, reg_val))
2012 static int alps_probe_trackstick_v3(struct psmouse *psmouse, int reg_base)
2014 int ret = -EIO, reg_val;
2016 if (alps_enter_command_mode(psmouse))
2019 reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x08);
2023 /* bit 7: trackstick is present */
2024 ret = reg_val & 0x80 ? 0 : -ENODEV;
2027 alps_exit_command_mode(psmouse);
2031 static int alps_setup_trackstick_v3(struct psmouse *psmouse, int reg_base)
2033 struct ps2dev *ps2dev = &psmouse->ps2dev;
2035 unsigned char param[4];
2037 if (alps_passthrough_mode_v3(psmouse, reg_base, true))
2041 * E7 report for the trackstick
2043 * There have been reports of failures to seem to trace back
2044 * to the above trackstick check failing. When these occur
2045 * this E7 report fails, so when that happens we continue
2046 * with the assumption that there isn't a trackstick after
2049 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_SETSCALE21, param)) {
2050 psmouse_warn(psmouse, "Failed to initialize trackstick (E7 report failed)\n");
2053 psmouse_dbg(psmouse, "trackstick E7 report: %3ph\n", param);
2056 * Not sure what this does, but it is absolutely
2057 * essential. Without it, the touchpad does not
2058 * work at all and the trackstick just emits normal
2061 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2062 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2063 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2064 alps_command_mode_send_nibble(psmouse, 0x9) ||
2065 alps_command_mode_send_nibble(psmouse, 0x4)) {
2066 psmouse_err(psmouse,
2067 "Error sending magic E6 sequence\n");
2073 * This ensures the trackstick packets are in the format
2074 * supported by this driver. If bit 1 isn't set the packet
2075 * format is different.
2077 if (alps_enter_command_mode(psmouse) ||
2078 alps_command_mode_write_reg(psmouse,
2079 reg_base + 0x08, 0x82) ||
2080 alps_exit_command_mode(psmouse))
2085 if (alps_passthrough_mode_v3(psmouse, reg_base, false))
2091 static int alps_hw_init_v3(struct psmouse *psmouse)
2093 struct ps2dev *ps2dev = &psmouse->ps2dev;
2095 unsigned char param[4];
2097 reg_val = alps_probe_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE);
2098 if (reg_val == -EIO)
2102 alps_setup_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE) == -EIO)
2105 if (alps_enter_command_mode(psmouse) ||
2106 alps_absolute_mode_v3(psmouse)) {
2107 psmouse_err(psmouse, "Failed to enter absolute mode\n");
2111 reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
2114 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
2117 reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
2120 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
2123 if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
2125 if (__alps_command_mode_write_reg(psmouse, 0x04))
2128 if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
2130 if (__alps_command_mode_write_reg(psmouse, 0x03))
2133 if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
2135 if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
2138 if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
2140 if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
2143 alps_exit_command_mode(psmouse);
2145 /* Set rate and enable data reporting */
2147 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
2148 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
2149 psmouse_err(psmouse, "Failed to enable data reporting\n");
2157 * Leaving the touchpad in command mode will essentially render
2158 * it unusable until the machine reboots, so exit it here just
2161 alps_exit_command_mode(psmouse);
2165 static int alps_get_v3_v7_resolution(struct psmouse *psmouse, int reg_pitch)
2167 int reg, x_pitch, y_pitch, x_electrode, y_electrode, x_phys, y_phys;
2168 struct alps_data *priv = psmouse->private;
2170 reg = alps_command_mode_read_reg(psmouse, reg_pitch);
2174 x_pitch = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
2175 x_pitch = 50 + 2 * x_pitch; /* In 0.1 mm units */
2177 y_pitch = (char)reg >> 4; /* sign extend upper 4 bits */
2178 y_pitch = 36 + 2 * y_pitch; /* In 0.1 mm units */
2180 reg = alps_command_mode_read_reg(psmouse, reg_pitch + 1);
2184 x_electrode = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
2185 x_electrode = 17 + x_electrode;
2187 y_electrode = (char)reg >> 4; /* sign extend upper 4 bits */
2188 y_electrode = 13 + y_electrode;
2190 x_phys = x_pitch * (x_electrode - 1); /* In 0.1 mm units */
2191 y_phys = y_pitch * (y_electrode - 1); /* In 0.1 mm units */
2193 priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
2194 priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
2196 psmouse_dbg(psmouse,
2197 "pitch %dx%d num-electrodes %dx%d physical size %dx%d mm res %dx%d\n",
2198 x_pitch, y_pitch, x_electrode, y_electrode,
2199 x_phys / 10, y_phys / 10, priv->x_res, priv->y_res);
2204 static int alps_hw_init_rushmore_v3(struct psmouse *psmouse)
2206 struct alps_data *priv = psmouse->private;
2207 struct ps2dev *ps2dev = &psmouse->ps2dev;
2208 int reg_val, ret = -1;
2210 if (priv->flags & ALPS_DUALPOINT) {
2211 reg_val = alps_setup_trackstick_v3(psmouse,
2212 ALPS_REG_BASE_RUSHMORE);
2213 if (reg_val == -EIO)
2217 if (alps_enter_command_mode(psmouse) ||
2218 alps_command_mode_read_reg(psmouse, 0xc2d9) == -1 ||
2219 alps_command_mode_write_reg(psmouse, 0xc2cb, 0x00))
2222 if (alps_get_v3_v7_resolution(psmouse, 0xc2da))
2225 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c6);
2228 if (__alps_command_mode_write_reg(psmouse, reg_val & 0xfd))
2231 if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
2234 /* enter absolute mode */
2235 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
2238 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
2241 alps_exit_command_mode(psmouse);
2242 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2245 alps_exit_command_mode(psmouse);
2249 /* Must be in command mode when calling this function */
2250 static int alps_absolute_mode_v4(struct psmouse *psmouse)
2254 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
2259 if (__alps_command_mode_write_reg(psmouse, reg_val))
2265 static int alps_hw_init_v4(struct psmouse *psmouse)
2267 struct ps2dev *ps2dev = &psmouse->ps2dev;
2268 unsigned char param[4];
2270 if (alps_enter_command_mode(psmouse))
2273 if (alps_absolute_mode_v4(psmouse)) {
2274 psmouse_err(psmouse, "Failed to enter absolute mode\n");
2278 if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
2281 if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
2284 if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
2287 if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
2290 if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
2293 if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
2296 if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
2299 if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
2302 alps_exit_command_mode(psmouse);
2305 * This sequence changes the output from a 9-byte to an
2306 * 8-byte format. All the same data seems to be present,
2307 * just in a more compact format.
2312 if (ps2_command(ps2dev, ¶m[0], PSMOUSE_CMD_SETRATE) ||
2313 ps2_command(ps2dev, ¶m[1], PSMOUSE_CMD_SETRATE) ||
2314 ps2_command(ps2dev, ¶m[2], PSMOUSE_CMD_SETRATE) ||
2315 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
2318 /* Set rate and enable data reporting */
2320 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
2321 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
2322 psmouse_err(psmouse, "Failed to enable data reporting\n");
2330 * Leaving the touchpad in command mode will essentially render
2331 * it unusable until the machine reboots, so exit it here just
2334 alps_exit_command_mode(psmouse);
2338 static int alps_get_otp_values_ss4_v2(struct psmouse *psmouse,
2339 unsigned char index, unsigned char otp[])
2341 struct ps2dev *ps2dev = &psmouse->ps2dev;
2345 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2346 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2347 ps2_command(ps2dev, otp, PSMOUSE_CMD_GETINFO))
2353 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2354 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2355 ps2_command(ps2dev, otp, PSMOUSE_CMD_GETINFO))
2364 static int alps_update_device_area_ss4_v2(unsigned char otp[][4],
2365 struct alps_data *priv)
2367 int num_x_electrode;
2368 int num_y_electrode;
2369 int x_pitch, y_pitch, x_phys, y_phys;
2371 num_x_electrode = SS4_NUMSENSOR_XOFFSET + (otp[1][0] & 0x0F);
2372 num_y_electrode = SS4_NUMSENSOR_YOFFSET + ((otp[1][0] >> 4) & 0x0F);
2374 priv->x_max = (num_x_electrode - 1) * SS4_COUNT_PER_ELECTRODE;
2375 priv->y_max = (num_y_electrode - 1) * SS4_COUNT_PER_ELECTRODE;
2377 x_pitch = ((otp[1][2] >> 2) & 0x07) + SS4_MIN_PITCH_MM;
2378 y_pitch = ((otp[1][2] >> 5) & 0x07) + SS4_MIN_PITCH_MM;
2380 x_phys = x_pitch * (num_x_electrode - 1); /* In 0.1 mm units */
2381 y_phys = y_pitch * (num_y_electrode - 1); /* In 0.1 mm units */
2383 priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
2384 priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
2389 static int alps_update_btn_info_ss4_v2(unsigned char otp[][4],
2390 struct alps_data *priv)
2392 unsigned char is_btnless;
2394 is_btnless = (otp[1][1] >> 3) & 0x01;
2397 priv->flags |= ALPS_BUTTONPAD;
2402 static int alps_set_defaults_ss4_v2(struct psmouse *psmouse,
2403 struct alps_data *priv)
2405 unsigned char otp[2][4];
2407 memset(otp, 0, sizeof(otp));
2409 if (alps_get_otp_values_ss4_v2(psmouse, 0, &otp[0][0]) ||
2410 alps_get_otp_values_ss4_v2(psmouse, 1, &otp[1][0]))
2413 alps_update_device_area_ss4_v2(otp, priv);
2415 alps_update_btn_info_ss4_v2(otp, priv);
2420 static int alps_dolphin_get_device_area(struct psmouse *psmouse,
2421 struct alps_data *priv)
2423 struct ps2dev *ps2dev = &psmouse->ps2dev;
2424 unsigned char param[4] = {0};
2425 int num_x_electrode, num_y_electrode;
2427 if (alps_enter_command_mode(psmouse))
2431 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
2432 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2433 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2434 ps2_command(ps2dev, ¶m[0], PSMOUSE_CMD_SETRATE) ||
2435 ps2_command(ps2dev, ¶m[0], PSMOUSE_CMD_SETRATE))
2438 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
2442 * Dolphin's sensor line number is not fixed. It can be calculated
2443 * by adding the device's register value with DOLPHIN_PROFILE_X/YOFFSET.
2444 * Further more, we can get device's x_max and y_max by multiplying
2445 * sensor line number with DOLPHIN_COUNT_PER_ELECTRODE.
2447 * e.g. When we get register's sensor_x = 11 & sensor_y = 8,
2448 * real sensor line number X = 11 + 8 = 19, and
2449 * real sensor line number Y = 8 + 1 = 9.
2450 * So, x_max = (19 - 1) * 64 = 1152, and
2451 * y_max = (9 - 1) * 64 = 512.
2453 num_x_electrode = DOLPHIN_PROFILE_XOFFSET + (param[2] & 0x0F);
2454 num_y_electrode = DOLPHIN_PROFILE_YOFFSET + ((param[2] >> 4) & 0x0F);
2455 priv->x_bits = num_x_electrode;
2456 priv->y_bits = num_y_electrode;
2457 priv->x_max = (num_x_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2458 priv->y_max = (num_y_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2460 if (alps_exit_command_mode(psmouse))
2466 static int alps_hw_init_dolphin_v1(struct psmouse *psmouse)
2468 struct ps2dev *ps2dev = &psmouse->ps2dev;
2469 unsigned char param[2];
2471 /* This is dolphin "v1" as empirically defined by florin9doi */
2475 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2476 ps2_command(ps2dev, ¶m[0], PSMOUSE_CMD_SETRATE) ||
2477 ps2_command(ps2dev, ¶m[1], PSMOUSE_CMD_SETRATE))
2483 static int alps_hw_init_v7(struct psmouse *psmouse)
2485 struct ps2dev *ps2dev = &psmouse->ps2dev;
2486 int reg_val, ret = -1;
2488 if (alps_enter_command_mode(psmouse) ||
2489 alps_command_mode_read_reg(psmouse, 0xc2d9) == -1)
2492 if (alps_get_v3_v7_resolution(psmouse, 0xc397))
2495 if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
2498 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
2501 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
2504 alps_exit_command_mode(psmouse);
2505 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2508 alps_exit_command_mode(psmouse);
2512 static int alps_hw_init_ss4_v2(struct psmouse *psmouse)
2514 struct ps2dev *ps2dev = &psmouse->ps2dev;
2515 char param[2] = {0x64, 0x28};
2518 /* enter absolute mode */
2519 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2520 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2521 ps2_command(ps2dev, ¶m[0], PSMOUSE_CMD_SETRATE) ||
2522 ps2_command(ps2dev, ¶m[1], PSMOUSE_CMD_SETRATE)) {
2526 /* T.B.D. Decread noise packet number, delete in the future */
2527 if (alps_exit_command_mode(psmouse) ||
2528 alps_enter_command_mode(psmouse) ||
2529 alps_command_mode_write_reg(psmouse, 0x001D, 0x20)) {
2532 alps_exit_command_mode(psmouse);
2534 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2537 alps_exit_command_mode(psmouse);
2541 static int alps_set_protocol(struct psmouse *psmouse,
2542 struct alps_data *priv,
2543 const struct alps_protocol_info *protocol)
2545 psmouse->private = priv;
2547 setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
2549 priv->proto_version = protocol->version;
2550 priv->byte0 = protocol->byte0;
2551 priv->mask0 = protocol->mask0;
2552 priv->flags = protocol->flags;
2559 switch (priv->proto_version) {
2562 priv->hw_init = alps_hw_init_v1_v2;
2563 priv->process_packet = alps_process_packet_v1_v2;
2564 priv->set_abs_params = alps_set_abs_params_st;
2570 priv->hw_init = alps_hw_init_v3;
2571 priv->process_packet = alps_process_packet_v3;
2572 priv->set_abs_params = alps_set_abs_params_semi_mt;
2573 priv->decode_fields = alps_decode_pinnacle;
2574 priv->nibble_commands = alps_v3_nibble_commands;
2575 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2578 case ALPS_PROTO_V3_RUSHMORE:
2579 priv->hw_init = alps_hw_init_rushmore_v3;
2580 priv->process_packet = alps_process_packet_v3;
2581 priv->set_abs_params = alps_set_abs_params_semi_mt;
2582 priv->decode_fields = alps_decode_rushmore;
2583 priv->nibble_commands = alps_v3_nibble_commands;
2584 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2588 if (alps_probe_trackstick_v3(psmouse,
2589 ALPS_REG_BASE_RUSHMORE) < 0)
2590 priv->flags &= ~ALPS_DUALPOINT;
2595 priv->hw_init = alps_hw_init_v4;
2596 priv->process_packet = alps_process_packet_v4;
2597 priv->set_abs_params = alps_set_abs_params_semi_mt;
2598 priv->nibble_commands = alps_v4_nibble_commands;
2599 priv->addr_command = PSMOUSE_CMD_DISABLE;
2603 priv->hw_init = alps_hw_init_dolphin_v1;
2604 priv->process_packet = alps_process_touchpad_packet_v3_v5;
2605 priv->decode_fields = alps_decode_dolphin;
2606 priv->set_abs_params = alps_set_abs_params_semi_mt;
2607 priv->nibble_commands = alps_v3_nibble_commands;
2608 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2612 if (alps_dolphin_get_device_area(psmouse, priv))
2618 priv->hw_init = alps_hw_init_v6;
2619 priv->process_packet = alps_process_packet_v6;
2620 priv->set_abs_params = alps_set_abs_params_st;
2621 priv->nibble_commands = alps_v6_nibble_commands;
2627 priv->hw_init = alps_hw_init_v7;
2628 priv->process_packet = alps_process_packet_v7;
2629 priv->decode_fields = alps_decode_packet_v7;
2630 priv->set_abs_params = alps_set_abs_params_v7;
2631 priv->nibble_commands = alps_v3_nibble_commands;
2632 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2633 priv->x_max = 0xfff;
2634 priv->y_max = 0x7ff;
2636 if (priv->fw_ver[1] != 0xba)
2637 priv->flags |= ALPS_BUTTONPAD;
2642 priv->hw_init = alps_hw_init_ss4_v2;
2643 priv->process_packet = alps_process_packet_ss4_v2;
2644 priv->decode_fields = alps_decode_ss4_v2;
2645 priv->set_abs_params = alps_set_abs_params_ss4_v2;
2646 priv->nibble_commands = alps_v3_nibble_commands;
2647 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2649 if (alps_set_defaults_ss4_v2(psmouse, priv))
2658 static const struct alps_protocol_info *alps_match_table(unsigned char *e7,
2661 const struct alps_model_info *model;
2664 for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
2665 model = &alps_model_data[i];
2667 if (!memcmp(e7, model->signature, sizeof(model->signature)) &&
2668 (!model->command_mode_resp ||
2669 model->command_mode_resp == ec[2])) {
2671 return &model->protocol_info;
2678 static int alps_identify(struct psmouse *psmouse, struct alps_data *priv)
2680 const struct alps_protocol_info *protocol;
2681 unsigned char e6[4], e7[4], ec[4];
2685 * First try "E6 report".
2686 * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
2687 * The bits 0-2 of the first byte will be 1s if some buttons are
2690 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2691 PSMOUSE_CMD_SETSCALE11, e6))
2694 if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100))
2698 * Now get the "E7" and "EC" reports. These will uniquely identify
2699 * most ALPS touchpads.
2701 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2702 PSMOUSE_CMD_SETSCALE21, e7) ||
2703 alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2704 PSMOUSE_CMD_RESET_WRAP, ec) ||
2705 alps_exit_command_mode(psmouse))
2708 protocol = alps_match_table(e7, ec);
2710 if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0x50 &&
2711 ec[0] == 0x73 && (ec[1] == 0x01 || ec[1] == 0x02)) {
2712 protocol = &alps_v5_protocol_data;
2713 } else if (ec[0] == 0x88 &&
2714 ((ec[1] & 0xf0) == 0xb0 || (ec[1] & 0xf0) == 0xc0)) {
2715 protocol = &alps_v7_protocol_data;
2716 } else if (ec[0] == 0x88 && ec[1] == 0x08) {
2717 protocol = &alps_v3_rushmore_data;
2718 } else if (ec[0] == 0x88 && ec[1] == 0x07 &&
2719 ec[2] >= 0x90 && ec[2] <= 0x9d) {
2720 protocol = &alps_v3_protocol_data;
2721 } else if (e7[0] == 0x73 && e7[1] == 0x03 &&
2722 e7[2] == 0x14 && ec[1] == 0x02) {
2723 protocol = &alps_v8_protocol_data;
2725 psmouse_dbg(psmouse,
2726 "Likely not an ALPS touchpad: E7=%3ph, EC=%3ph\n", e7, ec);
2732 /* Save the Firmware version */
2733 memcpy(priv->fw_ver, ec, 3);
2734 error = alps_set_protocol(psmouse, priv, protocol);
2742 static int alps_reconnect(struct psmouse *psmouse)
2744 struct alps_data *priv = psmouse->private;
2746 psmouse_reset(psmouse);
2748 if (alps_identify(psmouse, priv) < 0)
2751 return priv->hw_init(psmouse);
2754 static void alps_disconnect(struct psmouse *psmouse)
2756 struct alps_data *priv = psmouse->private;
2758 psmouse_reset(psmouse);
2759 del_timer_sync(&priv->timer);
2761 input_unregister_device(priv->dev2);
2762 if (!IS_ERR_OR_NULL(priv->dev3))
2763 input_unregister_device(priv->dev3);
2767 static void alps_set_abs_params_st(struct alps_data *priv,
2768 struct input_dev *dev1)
2770 input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
2771 input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
2772 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
2775 static void alps_set_abs_params_mt_common(struct alps_data *priv,
2776 struct input_dev *dev1)
2778 input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
2779 input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
2781 input_abs_set_res(dev1, ABS_MT_POSITION_X, priv->x_res);
2782 input_abs_set_res(dev1, ABS_MT_POSITION_Y, priv->y_res);
2784 set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
2785 set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
2788 static void alps_set_abs_params_semi_mt(struct alps_data *priv,
2789 struct input_dev *dev1)
2791 alps_set_abs_params_mt_common(priv, dev1);
2792 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
2794 input_mt_init_slots(dev1, MAX_TOUCHES,
2795 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
2799 static void alps_set_abs_params_v7(struct alps_data *priv,
2800 struct input_dev *dev1)
2802 alps_set_abs_params_mt_common(priv, dev1);
2803 set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
2805 input_mt_init_slots(dev1, MAX_TOUCHES,
2806 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
2809 set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
2812 static void alps_set_abs_params_ss4_v2(struct alps_data *priv,
2813 struct input_dev *dev1)
2815 alps_set_abs_params_mt_common(priv, dev1);
2816 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
2817 set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
2819 input_mt_init_slots(dev1, MAX_TOUCHES,
2820 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
2824 int alps_init(struct psmouse *psmouse)
2826 struct alps_data *priv = psmouse->private;
2827 struct input_dev *dev1 = psmouse->dev;
2830 error = priv->hw_init(psmouse);
2835 * Undo part of setup done for us by psmouse core since touchpad
2836 * is not a relative device.
2838 __clear_bit(EV_REL, dev1->evbit);
2839 __clear_bit(REL_X, dev1->relbit);
2840 __clear_bit(REL_Y, dev1->relbit);
2843 * Now set up our capabilities.
2845 dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
2846 dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
2847 dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
2848 dev1->keybit[BIT_WORD(BTN_LEFT)] |=
2849 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
2851 dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
2853 priv->set_abs_params(priv, dev1);
2855 if (priv->flags & ALPS_WHEEL) {
2856 dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
2857 dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
2860 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
2861 dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
2862 dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
2865 if (priv->flags & ALPS_FOUR_BUTTONS) {
2866 dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
2867 dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
2868 dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
2869 dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
2870 } else if (priv->flags & ALPS_BUTTONPAD) {
2871 set_bit(INPUT_PROP_BUTTONPAD, dev1->propbit);
2872 clear_bit(BTN_RIGHT, dev1->keybit);
2874 dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
2877 if (priv->flags & ALPS_DUALPOINT) {
2878 struct input_dev *dev2;
2880 dev2 = input_allocate_device();
2882 psmouse_err(psmouse,
2883 "failed to allocate trackstick device\n");
2888 snprintf(priv->phys2, sizeof(priv->phys2), "%s/input1",
2889 psmouse->ps2dev.serio->phys);
2890 dev2->phys = priv->phys2;
2893 * format of input device name is: "protocol vendor name"
2894 * see function psmouse_switch_protocol() in psmouse-base.c
2896 dev2->name = "AlpsPS/2 ALPS DualPoint Stick";
2898 dev2->id.bustype = BUS_I8042;
2899 dev2->id.vendor = 0x0002;
2900 dev2->id.product = PSMOUSE_ALPS;
2901 dev2->id.version = priv->proto_version;
2902 dev2->dev.parent = &psmouse->ps2dev.serio->dev;
2904 input_set_capability(dev2, EV_REL, REL_X);
2905 input_set_capability(dev2, EV_REL, REL_Y);
2906 input_set_capability(dev2, EV_KEY, BTN_LEFT);
2907 input_set_capability(dev2, EV_KEY, BTN_RIGHT);
2908 input_set_capability(dev2, EV_KEY, BTN_MIDDLE);
2910 __set_bit(INPUT_PROP_POINTER, dev2->propbit);
2911 __set_bit(INPUT_PROP_POINTING_STICK, dev2->propbit);
2913 error = input_register_device(dev2);
2915 psmouse_err(psmouse,
2916 "failed to register trackstick device: %d\n",
2918 input_free_device(dev2);
2925 priv->psmouse = psmouse;
2927 INIT_DELAYED_WORK(&priv->dev3_register_work,
2928 alps_register_bare_ps2_mouse);
2930 psmouse->protocol_handler = alps_process_byte;
2931 psmouse->poll = alps_poll;
2932 psmouse->disconnect = alps_disconnect;
2933 psmouse->reconnect = alps_reconnect;
2934 psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
2936 /* We are having trouble resyncing ALPS touchpads so disable it for now */
2937 psmouse->resync_time = 0;
2939 /* Allow 2 invalid packets without resetting device */
2940 psmouse->resetafter = psmouse->pktsize * 2;
2945 psmouse_reset(psmouse);
2947 * Even though we did not allocate psmouse->private we do free
2950 kfree(psmouse->private);
2951 psmouse->private = NULL;
2955 int alps_detect(struct psmouse *psmouse, bool set_properties)
2957 struct alps_data *priv;
2960 error = alps_identify(psmouse, NULL);
2965 * Reset the device to make sure it is fully operational:
2966 * on some laptops, like certain Dell Latitudes, we may
2967 * fail to properly detect presence of trackstick if device
2968 * has not been reset.
2970 psmouse_reset(psmouse);
2972 priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
2976 error = alps_identify(psmouse, priv);
2982 if (set_properties) {
2983 psmouse->vendor = "ALPS";
2984 psmouse->name = priv->flags & ALPS_DUALPOINT ?
2985 "DualPoint TouchPad" : "GlidePoint";
2986 psmouse->model = priv->proto_version;
2989 * Destroy alps_data structure we allocated earlier since
2990 * this was just a "trial run". Otherwise we'll keep it
2991 * to be used by alps_init() which has to be called if
2992 * we succeed and set_properties is true.
2995 psmouse->private = NULL;