Linux 4.2.1
[linux/fpc-iii.git] / drivers / input / mouse / alps.c
blob4d246861d692b810f3074aa7917cda86893ac6c2
1 /*
2 * ALPS touchpad PS/2 mouse driver
4 * Copyright (c) 2003 Neil Brown <neilb@cse.unsw.edu.au>
5 * Copyright (c) 2003-2005 Peter Osterlund <petero2@telia.com>
6 * Copyright (c) 2004 Dmitry Torokhov <dtor@mail.ru>
7 * Copyright (c) 2005 Vojtech Pavlik <vojtech@suse.cz>
8 * Copyright (c) 2009 Sebastian Kapfer <sebastian_kapfer@gmx.net>
10 * ALPS detection, tap switching and status querying info is taken from
11 * tpconfig utility (by C. Scott Ananian and Bruce Kall).
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License version 2 as published by
15 * the Free Software Foundation.
18 #include <linux/slab.h>
19 #include <linux/input.h>
20 #include <linux/input/mt.h>
21 #include <linux/serio.h>
22 #include <linux/libps2.h>
23 #include <linux/dmi.h>
25 #include "psmouse.h"
26 #include "alps.h"
29 * Definitions for ALPS version 3 and 4 command mode protocol
31 #define ALPS_CMD_NIBBLE_10 0x01f2
33 #define ALPS_REG_BASE_RUSHMORE 0xc2c0
34 #define ALPS_REG_BASE_PINNACLE 0x0000
36 static const struct alps_nibble_commands alps_v3_nibble_commands[] = {
37 { PSMOUSE_CMD_SETPOLL, 0x00 }, /* 0 */
38 { PSMOUSE_CMD_RESET_DIS, 0x00 }, /* 1 */
39 { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* 2 */
40 { PSMOUSE_CMD_SETRATE, 0x0a }, /* 3 */
41 { PSMOUSE_CMD_SETRATE, 0x14 }, /* 4 */
42 { PSMOUSE_CMD_SETRATE, 0x28 }, /* 5 */
43 { PSMOUSE_CMD_SETRATE, 0x3c }, /* 6 */
44 { PSMOUSE_CMD_SETRATE, 0x50 }, /* 7 */
45 { PSMOUSE_CMD_SETRATE, 0x64 }, /* 8 */
46 { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 9 */
47 { ALPS_CMD_NIBBLE_10, 0x00 }, /* a */
48 { PSMOUSE_CMD_SETRES, 0x00 }, /* b */
49 { PSMOUSE_CMD_SETRES, 0x01 }, /* c */
50 { PSMOUSE_CMD_SETRES, 0x02 }, /* d */
51 { PSMOUSE_CMD_SETRES, 0x03 }, /* e */
52 { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */
55 static const struct alps_nibble_commands alps_v4_nibble_commands[] = {
56 { PSMOUSE_CMD_ENABLE, 0x00 }, /* 0 */
57 { PSMOUSE_CMD_RESET_DIS, 0x00 }, /* 1 */
58 { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* 2 */
59 { PSMOUSE_CMD_SETRATE, 0x0a }, /* 3 */
60 { PSMOUSE_CMD_SETRATE, 0x14 }, /* 4 */
61 { PSMOUSE_CMD_SETRATE, 0x28 }, /* 5 */
62 { PSMOUSE_CMD_SETRATE, 0x3c }, /* 6 */
63 { PSMOUSE_CMD_SETRATE, 0x50 }, /* 7 */
64 { PSMOUSE_CMD_SETRATE, 0x64 }, /* 8 */
65 { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 9 */
66 { ALPS_CMD_NIBBLE_10, 0x00 }, /* a */
67 { PSMOUSE_CMD_SETRES, 0x00 }, /* b */
68 { PSMOUSE_CMD_SETRES, 0x01 }, /* c */
69 { PSMOUSE_CMD_SETRES, 0x02 }, /* d */
70 { PSMOUSE_CMD_SETRES, 0x03 }, /* e */
71 { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */
74 static const struct alps_nibble_commands alps_v6_nibble_commands[] = {
75 { PSMOUSE_CMD_ENABLE, 0x00 }, /* 0 */
76 { PSMOUSE_CMD_SETRATE, 0x0a }, /* 1 */
77 { PSMOUSE_CMD_SETRATE, 0x14 }, /* 2 */
78 { PSMOUSE_CMD_SETRATE, 0x28 }, /* 3 */
79 { PSMOUSE_CMD_SETRATE, 0x3c }, /* 4 */
80 { PSMOUSE_CMD_SETRATE, 0x50 }, /* 5 */
81 { PSMOUSE_CMD_SETRATE, 0x64 }, /* 6 */
82 { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 7 */
83 { PSMOUSE_CMD_GETID, 0x00 }, /* 8 */
84 { PSMOUSE_CMD_GETINFO, 0x00 }, /* 9 */
85 { PSMOUSE_CMD_SETRES, 0x00 }, /* a */
86 { PSMOUSE_CMD_SETRES, 0x01 }, /* b */
87 { PSMOUSE_CMD_SETRES, 0x02 }, /* c */
88 { PSMOUSE_CMD_SETRES, 0x03 }, /* d */
89 { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* e */
90 { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */
94 #define ALPS_DUALPOINT 0x02 /* touchpad has trackstick */
95 #define ALPS_PASS 0x04 /* device has a pass-through port */
97 #define ALPS_WHEEL 0x08 /* hardware wheel present */
98 #define ALPS_FW_BK_1 0x10 /* front & back buttons present */
99 #define ALPS_FW_BK_2 0x20 /* front & back buttons present */
100 #define ALPS_FOUR_BUTTONS 0x40 /* 4 direction button present */
101 #define ALPS_PS2_INTERLEAVED 0x80 /* 3-byte PS/2 packet interleaved with
102 6-byte ALPS packet */
103 #define ALPS_DELL 0x100 /* device is a Dell laptop */
104 #define ALPS_BUTTONPAD 0x200 /* device is a clickpad */
106 static const struct alps_model_info alps_model_data[] = {
107 { { 0x32, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } }, /* Toshiba Salellite Pro M10 */
108 { { 0x33, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V1, 0x88, 0xf8, 0 } }, /* UMAX-530T */
109 { { 0x53, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
110 { { 0x53, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
111 { { 0x60, 0x03, 0xc8 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } }, /* HP ze1115 */
112 { { 0x63, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
113 { { 0x63, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
114 { { 0x63, 0x02, 0x28 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 } }, /* Fujitsu Siemens S6010 */
115 { { 0x63, 0x02, 0x3c }, 0x00, { ALPS_PROTO_V2, 0x8f, 0x8f, ALPS_WHEEL } }, /* Toshiba Satellite S2400-103 */
116 { { 0x63, 0x02, 0x50 }, 0x00, { ALPS_PROTO_V2, 0xef, 0xef, ALPS_FW_BK_1 } }, /* NEC Versa L320 */
117 { { 0x63, 0x02, 0x64 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
118 { { 0x63, 0x03, 0xc8 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } }, /* Dell Latitude D800 */
119 { { 0x73, 0x00, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_DUALPOINT } }, /* ThinkPad R61 8918-5QG */
120 { { 0x73, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
121 { { 0x73, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 } }, /* Ahtec Laptop */
124 * XXX This entry is suspicious. First byte has zero lower nibble,
125 * which is what a normal mouse would report. Also, the value 0x0e
126 * isn't valid per PS/2 spec.
128 { { 0x20, 0x02, 0x0e }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },
130 { { 0x22, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },
131 { { 0x22, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT } }, /* Dell Latitude D600 */
132 /* Dell Latitude E5500, E6400, E6500, Precision M4400 */
133 { { 0x62, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xcf, 0xcf,
134 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED } },
135 { { 0x73, 0x00, 0x14 }, 0x00, { ALPS_PROTO_V6, 0xff, 0xff, ALPS_DUALPOINT } }, /* Dell XT2 */
136 { { 0x73, 0x02, 0x50 }, 0x00, { ALPS_PROTO_V2, 0xcf, 0xcf, ALPS_FOUR_BUTTONS } }, /* Dell Vostro 1400 */
137 { { 0x52, 0x01, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xff, 0xff,
138 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED } }, /* Toshiba Tecra A11-11L */
139 { { 0x73, 0x02, 0x64 }, 0x8a, { ALPS_PROTO_V4, 0x8f, 0x8f, 0 } },
142 static const struct alps_protocol_info alps_v3_protocol_data = {
143 ALPS_PROTO_V3, 0x8f, 0x8f, ALPS_DUALPOINT
146 static const struct alps_protocol_info alps_v3_rushmore_data = {
147 ALPS_PROTO_V3_RUSHMORE, 0x8f, 0x8f, ALPS_DUALPOINT
150 static const struct alps_protocol_info alps_v5_protocol_data = {
151 ALPS_PROTO_V5, 0xc8, 0xd8, 0
154 static const struct alps_protocol_info alps_v7_protocol_data = {
155 ALPS_PROTO_V7, 0x48, 0x48, ALPS_DUALPOINT
158 static const struct alps_protocol_info alps_v8_protocol_data = {
159 ALPS_PROTO_V8, 0x18, 0x18, 0
162 static void alps_set_abs_params_st(struct alps_data *priv,
163 struct input_dev *dev1);
164 static void alps_set_abs_params_semi_mt(struct alps_data *priv,
165 struct input_dev *dev1);
166 static void alps_set_abs_params_v7(struct alps_data *priv,
167 struct input_dev *dev1);
168 static void alps_set_abs_params_ss4_v2(struct alps_data *priv,
169 struct input_dev *dev1);
171 /* Packet formats are described in Documentation/input/alps.txt */
173 static bool alps_is_valid_first_byte(struct alps_data *priv,
174 unsigned char data)
176 return (data & priv->mask0) == priv->byte0;
179 static void alps_report_buttons(struct input_dev *dev1, struct input_dev *dev2,
180 int left, int right, int middle)
182 struct input_dev *dev;
185 * If shared button has already been reported on the
186 * other device (dev2) then this event should be also
187 * sent through that device.
189 dev = (dev2 && test_bit(BTN_LEFT, dev2->key)) ? dev2 : dev1;
190 input_report_key(dev, BTN_LEFT, left);
192 dev = (dev2 && test_bit(BTN_RIGHT, dev2->key)) ? dev2 : dev1;
193 input_report_key(dev, BTN_RIGHT, right);
195 dev = (dev2 && test_bit(BTN_MIDDLE, dev2->key)) ? dev2 : dev1;
196 input_report_key(dev, BTN_MIDDLE, middle);
199 * Sync the _other_ device now, we'll do the first
200 * device later once we report the rest of the events.
202 if (dev2)
203 input_sync(dev2);
206 static void alps_process_packet_v1_v2(struct psmouse *psmouse)
208 struct alps_data *priv = psmouse->private;
209 unsigned char *packet = psmouse->packet;
210 struct input_dev *dev = psmouse->dev;
211 struct input_dev *dev2 = priv->dev2;
212 int x, y, z, ges, fin, left, right, middle;
213 int back = 0, forward = 0;
215 if (priv->proto_version == ALPS_PROTO_V1) {
216 left = packet[2] & 0x10;
217 right = packet[2] & 0x08;
218 middle = 0;
219 x = packet[1] | ((packet[0] & 0x07) << 7);
220 y = packet[4] | ((packet[3] & 0x07) << 7);
221 z = packet[5];
222 } else {
223 left = packet[3] & 1;
224 right = packet[3] & 2;
225 middle = packet[3] & 4;
226 x = packet[1] | ((packet[2] & 0x78) << (7 - 3));
227 y = packet[4] | ((packet[3] & 0x70) << (7 - 4));
228 z = packet[5];
231 if (priv->flags & ALPS_FW_BK_1) {
232 back = packet[0] & 0x10;
233 forward = packet[2] & 4;
236 if (priv->flags & ALPS_FW_BK_2) {
237 back = packet[3] & 4;
238 forward = packet[2] & 4;
239 if ((middle = forward && back))
240 forward = back = 0;
243 ges = packet[2] & 1;
244 fin = packet[2] & 2;
246 if ((priv->flags & ALPS_DUALPOINT) && z == 127) {
247 input_report_rel(dev2, REL_X, (x > 383 ? (x - 768) : x));
248 input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y));
250 alps_report_buttons(dev2, dev, left, right, middle);
252 input_sync(dev2);
253 return;
256 /* Dell non interleaved V2 dualpoint has separate stick button bits */
257 if (priv->proto_version == ALPS_PROTO_V2 &&
258 priv->flags == (ALPS_DELL | ALPS_PASS | ALPS_DUALPOINT)) {
259 left |= packet[0] & 1;
260 right |= packet[0] & 2;
261 middle |= packet[0] & 4;
264 alps_report_buttons(dev, dev2, left, right, middle);
266 /* Convert hardware tap to a reasonable Z value */
267 if (ges && !fin)
268 z = 40;
271 * A "tap and drag" operation is reported by the hardware as a transition
272 * from (!fin && ges) to (fin && ges). This should be translated to the
273 * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
275 if (ges && fin && !priv->prev_fin) {
276 input_report_abs(dev, ABS_X, x);
277 input_report_abs(dev, ABS_Y, y);
278 input_report_abs(dev, ABS_PRESSURE, 0);
279 input_report_key(dev, BTN_TOOL_FINGER, 0);
280 input_sync(dev);
282 priv->prev_fin = fin;
284 if (z > 30)
285 input_report_key(dev, BTN_TOUCH, 1);
286 if (z < 25)
287 input_report_key(dev, BTN_TOUCH, 0);
289 if (z > 0) {
290 input_report_abs(dev, ABS_X, x);
291 input_report_abs(dev, ABS_Y, y);
294 input_report_abs(dev, ABS_PRESSURE, z);
295 input_report_key(dev, BTN_TOOL_FINGER, z > 0);
297 if (priv->flags & ALPS_WHEEL)
298 input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
300 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
301 input_report_key(dev, BTN_FORWARD, forward);
302 input_report_key(dev, BTN_BACK, back);
305 if (priv->flags & ALPS_FOUR_BUTTONS) {
306 input_report_key(dev, BTN_0, packet[2] & 4);
307 input_report_key(dev, BTN_1, packet[0] & 0x10);
308 input_report_key(dev, BTN_2, packet[3] & 4);
309 input_report_key(dev, BTN_3, packet[0] & 0x20);
312 input_sync(dev);
315 static void alps_get_bitmap_points(unsigned int map,
316 struct alps_bitmap_point *low,
317 struct alps_bitmap_point *high,
318 int *fingers)
320 struct alps_bitmap_point *point;
321 int i, bit, prev_bit = 0;
323 point = low;
324 for (i = 0; map != 0; i++, map >>= 1) {
325 bit = map & 1;
326 if (bit) {
327 if (!prev_bit) {
328 point->start_bit = i;
329 point->num_bits = 0;
330 (*fingers)++;
332 point->num_bits++;
333 } else {
334 if (prev_bit)
335 point = high;
337 prev_bit = bit;
342 * Process bitmap data from semi-mt protocols. Returns the number of
343 * fingers detected. A return value of 0 means at least one of the
344 * bitmaps was empty.
346 * The bitmaps don't have enough data to track fingers, so this function
347 * only generates points representing a bounding box of all contacts.
348 * These points are returned in fields->mt when the return value
349 * is greater than 0.
351 static int alps_process_bitmap(struct alps_data *priv,
352 struct alps_fields *fields)
354 int i, fingers_x = 0, fingers_y = 0, fingers, closest;
355 struct alps_bitmap_point x_low = {0,}, x_high = {0,};
356 struct alps_bitmap_point y_low = {0,}, y_high = {0,};
357 struct input_mt_pos corner[4];
359 if (!fields->x_map || !fields->y_map)
360 return 0;
362 alps_get_bitmap_points(fields->x_map, &x_low, &x_high, &fingers_x);
363 alps_get_bitmap_points(fields->y_map, &y_low, &y_high, &fingers_y);
366 * Fingers can overlap, so we use the maximum count of fingers
367 * on either axis as the finger count.
369 fingers = max(fingers_x, fingers_y);
372 * If an axis reports only a single contact, we have overlapping or
373 * adjacent fingers. Divide the single contact between the two points.
375 if (fingers_x == 1) {
376 i = (x_low.num_bits - 1) / 2;
377 x_low.num_bits = x_low.num_bits - i;
378 x_high.start_bit = x_low.start_bit + i;
379 x_high.num_bits = max(i, 1);
381 if (fingers_y == 1) {
382 i = (y_low.num_bits - 1) / 2;
383 y_low.num_bits = y_low.num_bits - i;
384 y_high.start_bit = y_low.start_bit + i;
385 y_high.num_bits = max(i, 1);
388 /* top-left corner */
389 corner[0].x =
390 (priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
391 (2 * (priv->x_bits - 1));
392 corner[0].y =
393 (priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
394 (2 * (priv->y_bits - 1));
396 /* top-right corner */
397 corner[1].x =
398 (priv->x_max * (2 * x_high.start_bit + x_high.num_bits - 1)) /
399 (2 * (priv->x_bits - 1));
400 corner[1].y =
401 (priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
402 (2 * (priv->y_bits - 1));
404 /* bottom-right corner */
405 corner[2].x =
406 (priv->x_max * (2 * x_high.start_bit + x_high.num_bits - 1)) /
407 (2 * (priv->x_bits - 1));
408 corner[2].y =
409 (priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) /
410 (2 * (priv->y_bits - 1));
412 /* bottom-left corner */
413 corner[3].x =
414 (priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
415 (2 * (priv->x_bits - 1));
416 corner[3].y =
417 (priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) /
418 (2 * (priv->y_bits - 1));
420 /* x-bitmap order is reversed on v5 touchpads */
421 if (priv->proto_version == ALPS_PROTO_V5) {
422 for (i = 0; i < 4; i++)
423 corner[i].x = priv->x_max - corner[i].x;
426 /* y-bitmap order is reversed on v3 and v4 touchpads */
427 if (priv->proto_version == ALPS_PROTO_V3 ||
428 priv->proto_version == ALPS_PROTO_V4) {
429 for (i = 0; i < 4; i++)
430 corner[i].y = priv->y_max - corner[i].y;
434 * We only select a corner for the second touch once per 2 finger
435 * touch sequence to avoid the chosen corner (and thus the coordinates)
436 * jumping around when the first touch is in the middle.
438 if (priv->second_touch == -1) {
439 /* Find corner closest to our st coordinates */
440 closest = 0x7fffffff;
441 for (i = 0; i < 4; i++) {
442 int dx = fields->st.x - corner[i].x;
443 int dy = fields->st.y - corner[i].y;
444 int distance = dx * dx + dy * dy;
446 if (distance < closest) {
447 priv->second_touch = i;
448 closest = distance;
451 /* And select the opposite corner to use for the 2nd touch */
452 priv->second_touch = (priv->second_touch + 2) % 4;
455 fields->mt[0] = fields->st;
456 fields->mt[1] = corner[priv->second_touch];
458 return fingers;
461 static void alps_set_slot(struct input_dev *dev, int slot, int x, int y)
463 input_mt_slot(dev, slot);
464 input_mt_report_slot_state(dev, MT_TOOL_FINGER, true);
465 input_report_abs(dev, ABS_MT_POSITION_X, x);
466 input_report_abs(dev, ABS_MT_POSITION_Y, y);
469 static void alps_report_mt_data(struct psmouse *psmouse, int n)
471 struct alps_data *priv = psmouse->private;
472 struct input_dev *dev = psmouse->dev;
473 struct alps_fields *f = &priv->f;
474 int i, slot[MAX_TOUCHES];
476 input_mt_assign_slots(dev, slot, f->mt, n, 0);
477 for (i = 0; i < n; i++)
478 alps_set_slot(dev, slot[i], f->mt[i].x, f->mt[i].y);
480 input_mt_sync_frame(dev);
483 static void alps_report_semi_mt_data(struct psmouse *psmouse, int fingers)
485 struct alps_data *priv = psmouse->private;
486 struct input_dev *dev = psmouse->dev;
487 struct alps_fields *f = &priv->f;
489 /* Use st data when we don't have mt data */
490 if (fingers < 2) {
491 f->mt[0].x = f->st.x;
492 f->mt[0].y = f->st.y;
493 fingers = f->pressure > 0 ? 1 : 0;
494 priv->second_touch = -1;
497 if (fingers >= 1)
498 alps_set_slot(dev, 0, f->mt[0].x, f->mt[0].y);
499 if (fingers >= 2)
500 alps_set_slot(dev, 1, f->mt[1].x, f->mt[1].y);
501 input_mt_sync_frame(dev);
503 input_mt_report_finger_count(dev, fingers);
505 input_report_key(dev, BTN_LEFT, f->left);
506 input_report_key(dev, BTN_RIGHT, f->right);
507 input_report_key(dev, BTN_MIDDLE, f->middle);
509 input_report_abs(dev, ABS_PRESSURE, f->pressure);
511 input_sync(dev);
514 static void alps_process_trackstick_packet_v3(struct psmouse *psmouse)
516 struct alps_data *priv = psmouse->private;
517 unsigned char *packet = psmouse->packet;
518 struct input_dev *dev = priv->dev2;
519 int x, y, z, left, right, middle;
521 /* It should be a DualPoint when received trackstick packet */
522 if (!(priv->flags & ALPS_DUALPOINT)) {
523 psmouse_warn(psmouse,
524 "Rejected trackstick packet from non DualPoint device");
525 return;
528 /* Sanity check packet */
529 if (!(packet[0] & 0x40)) {
530 psmouse_dbg(psmouse, "Bad trackstick packet, discarding\n");
531 return;
535 * There's a special packet that seems to indicate the end
536 * of a stream of trackstick data. Filter these out.
538 if (packet[1] == 0x7f && packet[2] == 0x7f && packet[4] == 0x7f)
539 return;
541 x = (s8)(((packet[0] & 0x20) << 2) | (packet[1] & 0x7f));
542 y = (s8)(((packet[0] & 0x10) << 3) | (packet[2] & 0x7f));
543 z = (packet[4] & 0x7c) >> 2;
546 * The x and y values tend to be quite large, and when used
547 * alone the trackstick is difficult to use. Scale them down
548 * to compensate.
550 x /= 8;
551 y /= 8;
553 input_report_rel(dev, REL_X, x);
554 input_report_rel(dev, REL_Y, -y);
557 * Most ALPS models report the trackstick buttons in the touchpad
558 * packets, but a few report them here. No reliable way has been
559 * found to differentiate between the models upfront, so we enable
560 * the quirk in response to seeing a button press in the trackstick
561 * packet.
563 left = packet[3] & 0x01;
564 right = packet[3] & 0x02;
565 middle = packet[3] & 0x04;
567 if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) &&
568 (left || right || middle))
569 priv->quirks |= ALPS_QUIRK_TRACKSTICK_BUTTONS;
571 if (priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) {
572 input_report_key(dev, BTN_LEFT, left);
573 input_report_key(dev, BTN_RIGHT, right);
574 input_report_key(dev, BTN_MIDDLE, middle);
577 input_sync(dev);
578 return;
581 static void alps_decode_buttons_v3(struct alps_fields *f, unsigned char *p)
583 f->left = !!(p[3] & 0x01);
584 f->right = !!(p[3] & 0x02);
585 f->middle = !!(p[3] & 0x04);
587 f->ts_left = !!(p[3] & 0x10);
588 f->ts_right = !!(p[3] & 0x20);
589 f->ts_middle = !!(p[3] & 0x40);
592 static int alps_decode_pinnacle(struct alps_fields *f, unsigned char *p,
593 struct psmouse *psmouse)
595 f->first_mp = !!(p[4] & 0x40);
596 f->is_mp = !!(p[0] & 0x40);
598 if (f->is_mp) {
599 f->fingers = (p[5] & 0x3) + 1;
600 f->x_map = ((p[4] & 0x7e) << 8) |
601 ((p[1] & 0x7f) << 2) |
602 ((p[0] & 0x30) >> 4);
603 f->y_map = ((p[3] & 0x70) << 4) |
604 ((p[2] & 0x7f) << 1) |
605 (p[4] & 0x01);
606 } else {
607 f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
608 ((p[0] & 0x30) >> 4);
609 f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
610 f->pressure = p[5] & 0x7f;
612 alps_decode_buttons_v3(f, p);
615 return 0;
618 static int alps_decode_rushmore(struct alps_fields *f, unsigned char *p,
619 struct psmouse *psmouse)
621 f->first_mp = !!(p[4] & 0x40);
622 f->is_mp = !!(p[5] & 0x40);
624 if (f->is_mp) {
625 f->fingers = max((p[5] & 0x3), ((p[5] >> 2) & 0x3)) + 1;
626 f->x_map = ((p[5] & 0x10) << 11) |
627 ((p[4] & 0x7e) << 8) |
628 ((p[1] & 0x7f) << 2) |
629 ((p[0] & 0x30) >> 4);
630 f->y_map = ((p[5] & 0x20) << 6) |
631 ((p[3] & 0x70) << 4) |
632 ((p[2] & 0x7f) << 1) |
633 (p[4] & 0x01);
634 } else {
635 f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
636 ((p[0] & 0x30) >> 4);
637 f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
638 f->pressure = p[5] & 0x7f;
640 alps_decode_buttons_v3(f, p);
643 return 0;
646 static int alps_decode_dolphin(struct alps_fields *f, unsigned char *p,
647 struct psmouse *psmouse)
649 u64 palm_data = 0;
650 struct alps_data *priv = psmouse->private;
652 f->first_mp = !!(p[0] & 0x02);
653 f->is_mp = !!(p[0] & 0x20);
655 if (!f->is_mp) {
656 f->st.x = ((p[1] & 0x7f) | ((p[4] & 0x0f) << 7));
657 f->st.y = ((p[2] & 0x7f) | ((p[4] & 0xf0) << 3));
658 f->pressure = (p[0] & 4) ? 0 : p[5] & 0x7f;
659 alps_decode_buttons_v3(f, p);
660 } else {
661 f->fingers = ((p[0] & 0x6) >> 1 |
662 (p[0] & 0x10) >> 2);
664 palm_data = (p[1] & 0x7f) |
665 ((p[2] & 0x7f) << 7) |
666 ((p[4] & 0x7f) << 14) |
667 ((p[5] & 0x7f) << 21) |
668 ((p[3] & 0x07) << 28) |
669 (((u64)p[3] & 0x70) << 27) |
670 (((u64)p[0] & 0x01) << 34);
672 /* Y-profile is stored in P(0) to p(n-1), n = y_bits; */
673 f->y_map = palm_data & (BIT(priv->y_bits) - 1);
675 /* X-profile is stored in p(n) to p(n+m-1), m = x_bits; */
676 f->x_map = (palm_data >> priv->y_bits) &
677 (BIT(priv->x_bits) - 1);
680 return 0;
683 static void alps_process_touchpad_packet_v3_v5(struct psmouse *psmouse)
685 struct alps_data *priv = psmouse->private;
686 unsigned char *packet = psmouse->packet;
687 struct input_dev *dev2 = priv->dev2;
688 struct alps_fields *f = &priv->f;
689 int fingers = 0;
691 memset(f, 0, sizeof(*f));
693 priv->decode_fields(f, packet, psmouse);
696 * There's no single feature of touchpad position and bitmap packets
697 * that can be used to distinguish between them. We rely on the fact
698 * that a bitmap packet should always follow a position packet with
699 * bit 6 of packet[4] set.
701 if (priv->multi_packet) {
703 * Sometimes a position packet will indicate a multi-packet
704 * sequence, but then what follows is another position
705 * packet. Check for this, and when it happens process the
706 * position packet as usual.
708 if (f->is_mp) {
709 fingers = f->fingers;
711 * Bitmap processing uses position packet's coordinate
712 * data, so we need to do decode it first.
714 priv->decode_fields(f, priv->multi_data, psmouse);
715 if (alps_process_bitmap(priv, f) == 0)
716 fingers = 0; /* Use st data */
717 } else {
718 priv->multi_packet = 0;
723 * Bit 6 of byte 0 is not usually set in position packets. The only
724 * times it seems to be set is in situations where the data is
725 * suspect anyway, e.g. a palm resting flat on the touchpad. Given
726 * this combined with the fact that this bit is useful for filtering
727 * out misidentified bitmap packets, we reject anything with this
728 * bit set.
730 if (f->is_mp)
731 return;
733 if (!priv->multi_packet && f->first_mp) {
734 priv->multi_packet = 1;
735 memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
736 return;
739 priv->multi_packet = 0;
742 * Sometimes the hardware sends a single packet with z = 0
743 * in the middle of a stream. Real releases generate packets
744 * with x, y, and z all zero, so these seem to be flukes.
745 * Ignore them.
747 if (f->st.x && f->st.y && !f->pressure)
748 return;
750 alps_report_semi_mt_data(psmouse, fingers);
752 if ((priv->flags & ALPS_DUALPOINT) &&
753 !(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) {
754 input_report_key(dev2, BTN_LEFT, f->ts_left);
755 input_report_key(dev2, BTN_RIGHT, f->ts_right);
756 input_report_key(dev2, BTN_MIDDLE, f->ts_middle);
757 input_sync(dev2);
761 static void alps_process_packet_v3(struct psmouse *psmouse)
763 unsigned char *packet = psmouse->packet;
766 * v3 protocol packets come in three types, two representing
767 * touchpad data and one representing trackstick data.
768 * Trackstick packets seem to be distinguished by always
769 * having 0x3f in the last byte. This value has never been
770 * observed in the last byte of either of the other types
771 * of packets.
773 if (packet[5] == 0x3f) {
774 alps_process_trackstick_packet_v3(psmouse);
775 return;
778 alps_process_touchpad_packet_v3_v5(psmouse);
781 static void alps_process_packet_v6(struct psmouse *psmouse)
783 struct alps_data *priv = psmouse->private;
784 unsigned char *packet = psmouse->packet;
785 struct input_dev *dev = psmouse->dev;
786 struct input_dev *dev2 = priv->dev2;
787 int x, y, z, left, right, middle;
790 * We can use Byte5 to distinguish if the packet is from Touchpad
791 * or Trackpoint.
792 * Touchpad: 0 - 0x7E
793 * Trackpoint: 0x7F
795 if (packet[5] == 0x7F) {
796 /* It should be a DualPoint when received Trackpoint packet */
797 if (!(priv->flags & ALPS_DUALPOINT)) {
798 psmouse_warn(psmouse,
799 "Rejected trackstick packet from non DualPoint device");
800 return;
803 /* Trackpoint packet */
804 x = packet[1] | ((packet[3] & 0x20) << 2);
805 y = packet[2] | ((packet[3] & 0x40) << 1);
806 z = packet[4];
807 left = packet[3] & 0x01;
808 right = packet[3] & 0x02;
809 middle = packet[3] & 0x04;
811 /* To prevent the cursor jump when finger lifted */
812 if (x == 0x7F && y == 0x7F && z == 0x7F)
813 x = y = z = 0;
815 /* Divide 4 since trackpoint's speed is too fast */
816 input_report_rel(dev2, REL_X, (char)x / 4);
817 input_report_rel(dev2, REL_Y, -((char)y / 4));
819 input_report_key(dev2, BTN_LEFT, left);
820 input_report_key(dev2, BTN_RIGHT, right);
821 input_report_key(dev2, BTN_MIDDLE, middle);
823 input_sync(dev2);
824 return;
827 /* Touchpad packet */
828 x = packet[1] | ((packet[3] & 0x78) << 4);
829 y = packet[2] | ((packet[4] & 0x78) << 4);
830 z = packet[5];
831 left = packet[3] & 0x01;
832 right = packet[3] & 0x02;
834 if (z > 30)
835 input_report_key(dev, BTN_TOUCH, 1);
836 if (z < 25)
837 input_report_key(dev, BTN_TOUCH, 0);
839 if (z > 0) {
840 input_report_abs(dev, ABS_X, x);
841 input_report_abs(dev, ABS_Y, y);
844 input_report_abs(dev, ABS_PRESSURE, z);
845 input_report_key(dev, BTN_TOOL_FINGER, z > 0);
847 /* v6 touchpad does not have middle button */
848 input_report_key(dev, BTN_LEFT, left);
849 input_report_key(dev, BTN_RIGHT, right);
851 input_sync(dev);
854 static void alps_process_packet_v4(struct psmouse *psmouse)
856 struct alps_data *priv = psmouse->private;
857 unsigned char *packet = psmouse->packet;
858 struct alps_fields *f = &priv->f;
859 int offset;
862 * v4 has a 6-byte encoding for bitmap data, but this data is
863 * broken up between 3 normal packets. Use priv->multi_packet to
864 * track our position in the bitmap packet.
866 if (packet[6] & 0x40) {
867 /* sync, reset position */
868 priv->multi_packet = 0;
871 if (WARN_ON_ONCE(priv->multi_packet > 2))
872 return;
874 offset = 2 * priv->multi_packet;
875 priv->multi_data[offset] = packet[6];
876 priv->multi_data[offset + 1] = packet[7];
878 f->left = !!(packet[4] & 0x01);
879 f->right = !!(packet[4] & 0x02);
881 f->st.x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) |
882 ((packet[0] & 0x30) >> 4);
883 f->st.y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f);
884 f->pressure = packet[5] & 0x7f;
886 if (++priv->multi_packet > 2) {
887 priv->multi_packet = 0;
889 f->x_map = ((priv->multi_data[2] & 0x1f) << 10) |
890 ((priv->multi_data[3] & 0x60) << 3) |
891 ((priv->multi_data[0] & 0x3f) << 2) |
892 ((priv->multi_data[1] & 0x60) >> 5);
893 f->y_map = ((priv->multi_data[5] & 0x01) << 10) |
894 ((priv->multi_data[3] & 0x1f) << 5) |
895 (priv->multi_data[1] & 0x1f);
897 f->fingers = alps_process_bitmap(priv, f);
900 alps_report_semi_mt_data(psmouse, f->fingers);
903 static bool alps_is_valid_package_v7(struct psmouse *psmouse)
905 switch (psmouse->pktcnt) {
906 case 3:
907 return (psmouse->packet[2] & 0x40) == 0x40;
908 case 4:
909 return (psmouse->packet[3] & 0x48) == 0x48;
910 case 6:
911 return (psmouse->packet[5] & 0x40) == 0x00;
913 return true;
916 static unsigned char alps_get_packet_id_v7(char *byte)
918 unsigned char packet_id;
920 if (byte[4] & 0x40)
921 packet_id = V7_PACKET_ID_TWO;
922 else if (byte[4] & 0x01)
923 packet_id = V7_PACKET_ID_MULTI;
924 else if ((byte[0] & 0x10) && !(byte[4] & 0x43))
925 packet_id = V7_PACKET_ID_NEW;
926 else if (byte[1] == 0x00 && byte[4] == 0x00)
927 packet_id = V7_PACKET_ID_IDLE;
928 else
929 packet_id = V7_PACKET_ID_UNKNOWN;
931 return packet_id;
934 static void alps_get_finger_coordinate_v7(struct input_mt_pos *mt,
935 unsigned char *pkt,
936 unsigned char pkt_id)
938 mt[0].x = ((pkt[2] & 0x80) << 4);
939 mt[0].x |= ((pkt[2] & 0x3F) << 5);
940 mt[0].x |= ((pkt[3] & 0x30) >> 1);
941 mt[0].x |= (pkt[3] & 0x07);
942 mt[0].y = (pkt[1] << 3) | (pkt[0] & 0x07);
944 mt[1].x = ((pkt[3] & 0x80) << 4);
945 mt[1].x |= ((pkt[4] & 0x80) << 3);
946 mt[1].x |= ((pkt[4] & 0x3F) << 4);
947 mt[1].y = ((pkt[5] & 0x80) << 3);
948 mt[1].y |= ((pkt[5] & 0x3F) << 4);
950 switch (pkt_id) {
951 case V7_PACKET_ID_TWO:
952 mt[1].x &= ~0x000F;
953 mt[1].y |= 0x000F;
954 /* Detect false-postive touches where x & y report max value */
955 if (mt[1].y == 0x7ff && mt[1].x == 0xff0) {
956 mt[1].x = 0;
957 /* y gets set to 0 at the end of this function */
959 break;
961 case V7_PACKET_ID_MULTI:
962 mt[1].x &= ~0x003F;
963 mt[1].y &= ~0x0020;
964 mt[1].y |= ((pkt[4] & 0x02) << 4);
965 mt[1].y |= 0x001F;
966 break;
968 case V7_PACKET_ID_NEW:
969 mt[1].x &= ~0x003F;
970 mt[1].x |= (pkt[0] & 0x20);
971 mt[1].y |= 0x000F;
972 break;
975 mt[0].y = 0x7FF - mt[0].y;
976 mt[1].y = 0x7FF - mt[1].y;
979 static int alps_get_mt_count(struct input_mt_pos *mt)
981 int i, fingers = 0;
983 for (i = 0; i < MAX_TOUCHES; i++) {
984 if (mt[i].x != 0 || mt[i].y != 0)
985 fingers++;
988 return fingers;
991 static int alps_decode_packet_v7(struct alps_fields *f,
992 unsigned char *p,
993 struct psmouse *psmouse)
995 struct alps_data *priv = psmouse->private;
996 unsigned char pkt_id;
998 pkt_id = alps_get_packet_id_v7(p);
999 if (pkt_id == V7_PACKET_ID_IDLE)
1000 return 0;
1001 if (pkt_id == V7_PACKET_ID_UNKNOWN)
1002 return -1;
1004 * NEW packets are send to indicate a discontinuity in the finger
1005 * coordinate reporting. Specifically a finger may have moved from
1006 * slot 0 to 1 or vice versa. INPUT_MT_TRACK takes care of this for
1007 * us.
1009 * NEW packets have 3 problems:
1010 * 1) They do not contain middle / right button info (on non clickpads)
1011 * this can be worked around by preserving the old button state
1012 * 2) They do not contain an accurate fingercount, and they are
1013 * typically send when the number of fingers changes. We cannot use
1014 * the old finger count as that may mismatch with the amount of
1015 * touch coordinates we've available in the NEW packet
1016 * 3) Their x data for the second touch is inaccurate leading to
1017 * a possible jump of the x coordinate by 16 units when the first
1018 * non NEW packet comes in
1019 * Since problems 2 & 3 cannot be worked around, just ignore them.
1021 if (pkt_id == V7_PACKET_ID_NEW)
1022 return 1;
1024 alps_get_finger_coordinate_v7(f->mt, p, pkt_id);
1026 if (pkt_id == V7_PACKET_ID_TWO)
1027 f->fingers = alps_get_mt_count(f->mt);
1028 else /* pkt_id == V7_PACKET_ID_MULTI */
1029 f->fingers = 3 + (p[5] & 0x03);
1031 f->left = (p[0] & 0x80) >> 7;
1032 if (priv->flags & ALPS_BUTTONPAD) {
1033 if (p[0] & 0x20)
1034 f->fingers++;
1035 if (p[0] & 0x10)
1036 f->fingers++;
1037 } else {
1038 f->right = (p[0] & 0x20) >> 5;
1039 f->middle = (p[0] & 0x10) >> 4;
1042 /* Sometimes a single touch is reported in mt[1] rather then mt[0] */
1043 if (f->fingers == 1 && f->mt[0].x == 0 && f->mt[0].y == 0) {
1044 f->mt[0].x = f->mt[1].x;
1045 f->mt[0].y = f->mt[1].y;
1046 f->mt[1].x = 0;
1047 f->mt[1].y = 0;
1050 return 0;
1053 static void alps_process_trackstick_packet_v7(struct psmouse *psmouse)
1055 struct alps_data *priv = psmouse->private;
1056 unsigned char *packet = psmouse->packet;
1057 struct input_dev *dev2 = priv->dev2;
1058 int x, y, z, left, right, middle;
1060 /* It should be a DualPoint when received trackstick packet */
1061 if (!(priv->flags & ALPS_DUALPOINT)) {
1062 psmouse_warn(psmouse,
1063 "Rejected trackstick packet from non DualPoint device");
1064 return;
1067 x = ((packet[2] & 0xbf)) | ((packet[3] & 0x10) << 2);
1068 y = (packet[3] & 0x07) | (packet[4] & 0xb8) |
1069 ((packet[3] & 0x20) << 1);
1070 z = (packet[5] & 0x3f) | ((packet[3] & 0x80) >> 1);
1072 left = (packet[1] & 0x01);
1073 right = (packet[1] & 0x02) >> 1;
1074 middle = (packet[1] & 0x04) >> 2;
1076 input_report_rel(dev2, REL_X, (char)x);
1077 input_report_rel(dev2, REL_Y, -((char)y));
1079 input_report_key(dev2, BTN_LEFT, left);
1080 input_report_key(dev2, BTN_RIGHT, right);
1081 input_report_key(dev2, BTN_MIDDLE, middle);
1083 input_sync(dev2);
1086 static void alps_process_touchpad_packet_v7(struct psmouse *psmouse)
1088 struct alps_data *priv = psmouse->private;
1089 struct input_dev *dev = psmouse->dev;
1090 struct alps_fields *f = &priv->f;
1092 memset(f, 0, sizeof(*f));
1094 if (priv->decode_fields(f, psmouse->packet, psmouse))
1095 return;
1097 alps_report_mt_data(psmouse, alps_get_mt_count(f->mt));
1099 input_mt_report_finger_count(dev, f->fingers);
1101 input_report_key(dev, BTN_LEFT, f->left);
1102 input_report_key(dev, BTN_RIGHT, f->right);
1103 input_report_key(dev, BTN_MIDDLE, f->middle);
1105 input_sync(dev);
1108 static void alps_process_packet_v7(struct psmouse *psmouse)
1110 unsigned char *packet = psmouse->packet;
1112 if (packet[0] == 0x48 && (packet[4] & 0x47) == 0x06)
1113 alps_process_trackstick_packet_v7(psmouse);
1114 else
1115 alps_process_touchpad_packet_v7(psmouse);
1118 static unsigned char alps_get_pkt_id_ss4_v2(unsigned char *byte)
1120 unsigned char pkt_id = SS4_PACKET_ID_IDLE;
1122 if (byte[0] == 0x18 && byte[1] == 0x10 && byte[2] == 0x00 &&
1123 (byte[3] & 0x88) == 0x08 && byte[4] == 0x10 && byte[5] == 0x00) {
1124 pkt_id = SS4_PACKET_ID_IDLE;
1125 } else if (!(byte[3] & 0x10)) {
1126 pkt_id = SS4_PACKET_ID_ONE;
1127 } else if (!(byte[3] & 0x20)) {
1128 pkt_id = SS4_PACKET_ID_TWO;
1129 } else {
1130 pkt_id = SS4_PACKET_ID_MULTI;
1133 return pkt_id;
1136 static int alps_decode_ss4_v2(struct alps_fields *f,
1137 unsigned char *p, struct psmouse *psmouse)
1139 struct alps_data *priv = psmouse->private;
1140 unsigned char pkt_id;
1141 unsigned int no_data_x, no_data_y;
1143 pkt_id = alps_get_pkt_id_ss4_v2(p);
1145 /* Current packet is 1Finger coordinate packet */
1146 switch (pkt_id) {
1147 case SS4_PACKET_ID_ONE:
1148 f->mt[0].x = SS4_1F_X_V2(p);
1149 f->mt[0].y = SS4_1F_Y_V2(p);
1150 f->pressure = ((SS4_1F_Z_V2(p)) * 2) & 0x7f;
1151 f->fingers = 1;
1152 f->first_mp = 0;
1153 f->is_mp = 0;
1154 break;
1156 case SS4_PACKET_ID_TWO:
1157 if (priv->flags & ALPS_BUTTONPAD) {
1158 f->mt[0].x = SS4_BTL_MF_X_V2(p, 0);
1159 f->mt[0].y = SS4_BTL_MF_Y_V2(p, 0);
1160 f->mt[1].x = SS4_BTL_MF_X_V2(p, 1);
1161 f->mt[1].y = SS4_BTL_MF_Y_V2(p, 1);
1162 } else {
1163 f->mt[0].x = SS4_STD_MF_X_V2(p, 0);
1164 f->mt[0].y = SS4_STD_MF_Y_V2(p, 0);
1165 f->mt[1].x = SS4_STD_MF_X_V2(p, 1);
1166 f->mt[1].y = SS4_STD_MF_Y_V2(p, 1);
1168 f->pressure = SS4_MF_Z_V2(p, 0) ? 0x30 : 0;
1170 if (SS4_IS_MF_CONTINUE(p)) {
1171 f->first_mp = 1;
1172 } else {
1173 f->fingers = 2;
1174 f->first_mp = 0;
1176 f->is_mp = 0;
1178 break;
1180 case SS4_PACKET_ID_MULTI:
1181 if (priv->flags & ALPS_BUTTONPAD) {
1182 f->mt[2].x = SS4_BTL_MF_X_V2(p, 0);
1183 f->mt[2].y = SS4_BTL_MF_Y_V2(p, 0);
1184 f->mt[3].x = SS4_BTL_MF_X_V2(p, 1);
1185 f->mt[3].y = SS4_BTL_MF_Y_V2(p, 1);
1186 no_data_x = SS4_MFPACKET_NO_AX_BL;
1187 no_data_y = SS4_MFPACKET_NO_AY_BL;
1188 } else {
1189 f->mt[2].x = SS4_STD_MF_X_V2(p, 0);
1190 f->mt[2].y = SS4_STD_MF_Y_V2(p, 0);
1191 f->mt[3].x = SS4_STD_MF_X_V2(p, 1);
1192 f->mt[3].y = SS4_STD_MF_Y_V2(p, 1);
1193 no_data_x = SS4_MFPACKET_NO_AX;
1194 no_data_y = SS4_MFPACKET_NO_AY;
1197 f->first_mp = 0;
1198 f->is_mp = 1;
1200 if (SS4_IS_5F_DETECTED(p)) {
1201 f->fingers = 5;
1202 } else if (f->mt[3].x == no_data_x &&
1203 f->mt[3].y == no_data_y) {
1204 f->mt[3].x = 0;
1205 f->mt[3].y = 0;
1206 f->fingers = 3;
1207 } else {
1208 f->fingers = 4;
1210 break;
1212 case SS4_PACKET_ID_IDLE:
1213 default:
1214 memset(f, 0, sizeof(struct alps_fields));
1215 break;
1218 f->left = !!(SS4_BTN_V2(p) & 0x01);
1219 if (!(priv->flags & ALPS_BUTTONPAD)) {
1220 f->right = !!(SS4_BTN_V2(p) & 0x02);
1221 f->middle = !!(SS4_BTN_V2(p) & 0x04);
1224 return 0;
1227 static void alps_process_packet_ss4_v2(struct psmouse *psmouse)
1229 struct alps_data *priv = psmouse->private;
1230 unsigned char *packet = psmouse->packet;
1231 struct input_dev *dev = psmouse->dev;
1232 struct alps_fields *f = &priv->f;
1234 memset(f, 0, sizeof(struct alps_fields));
1235 priv->decode_fields(f, packet, psmouse);
1236 if (priv->multi_packet) {
1238 * Sometimes the first packet will indicate a multi-packet
1239 * sequence, but sometimes the next multi-packet would not
1240 * come. Check for this, and when it happens process the
1241 * position packet as usual.
1243 if (f->is_mp) {
1244 /* Now process the 1st packet */
1245 priv->decode_fields(f, priv->multi_data, psmouse);
1246 } else {
1247 priv->multi_packet = 0;
1252 * "f.is_mp" would always be '0' after merging the 1st and 2nd packet.
1253 * When it is set, it means 2nd packet comes without 1st packet come.
1255 if (f->is_mp)
1256 return;
1258 /* Save the first packet */
1259 if (!priv->multi_packet && f->first_mp) {
1260 priv->multi_packet = 1;
1261 memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
1262 return;
1265 priv->multi_packet = 0;
1267 alps_report_mt_data(psmouse, (f->fingers <= 4) ? f->fingers : 4);
1269 input_mt_report_finger_count(dev, f->fingers);
1271 input_report_key(dev, BTN_LEFT, f->left);
1272 input_report_key(dev, BTN_RIGHT, f->right);
1273 input_report_key(dev, BTN_MIDDLE, f->middle);
1275 input_report_abs(dev, ABS_PRESSURE, f->pressure);
1276 input_sync(dev);
1279 static bool alps_is_valid_package_ss4_v2(struct psmouse *psmouse)
1281 if (psmouse->pktcnt == 4 && ((psmouse->packet[3] & 0x08) != 0x08))
1282 return false;
1283 if (psmouse->pktcnt == 6 && ((psmouse->packet[5] & 0x10) != 0x0))
1284 return false;
1285 return true;
1288 static DEFINE_MUTEX(alps_mutex);
1290 static void alps_register_bare_ps2_mouse(struct work_struct *work)
1292 struct alps_data *priv =
1293 container_of(work, struct alps_data, dev3_register_work.work);
1294 struct psmouse *psmouse = priv->psmouse;
1295 struct input_dev *dev3;
1296 int error = 0;
1298 mutex_lock(&alps_mutex);
1300 if (priv->dev3)
1301 goto out;
1303 dev3 = input_allocate_device();
1304 if (!dev3) {
1305 psmouse_err(psmouse, "failed to allocate secondary device\n");
1306 error = -ENOMEM;
1307 goto out;
1310 snprintf(priv->phys3, sizeof(priv->phys3), "%s/%s",
1311 psmouse->ps2dev.serio->phys,
1312 (priv->dev2 ? "input2" : "input1"));
1313 dev3->phys = priv->phys3;
1316 * format of input device name is: "protocol vendor name"
1317 * see function psmouse_switch_protocol() in psmouse-base.c
1319 dev3->name = "PS/2 ALPS Mouse";
1321 dev3->id.bustype = BUS_I8042;
1322 dev3->id.vendor = 0x0002;
1323 dev3->id.product = PSMOUSE_PS2;
1324 dev3->id.version = 0x0000;
1325 dev3->dev.parent = &psmouse->ps2dev.serio->dev;
1327 input_set_capability(dev3, EV_REL, REL_X);
1328 input_set_capability(dev3, EV_REL, REL_Y);
1329 input_set_capability(dev3, EV_KEY, BTN_LEFT);
1330 input_set_capability(dev3, EV_KEY, BTN_RIGHT);
1331 input_set_capability(dev3, EV_KEY, BTN_MIDDLE);
1333 __set_bit(INPUT_PROP_POINTER, dev3->propbit);
1335 error = input_register_device(dev3);
1336 if (error) {
1337 psmouse_err(psmouse,
1338 "failed to register secondary device: %d\n",
1339 error);
1340 input_free_device(dev3);
1341 goto out;
1344 priv->dev3 = dev3;
1346 out:
1348 * Save the error code so that we can detect that we
1349 * already tried to create the device.
1351 if (error)
1352 priv->dev3 = ERR_PTR(error);
1354 mutex_unlock(&alps_mutex);
1357 static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
1358 unsigned char packet[],
1359 bool report_buttons)
1361 struct alps_data *priv = psmouse->private;
1362 struct input_dev *dev, *dev2 = NULL;
1364 /* Figure out which device to use to report the bare packet */
1365 if (priv->proto_version == ALPS_PROTO_V2 &&
1366 (priv->flags & ALPS_DUALPOINT)) {
1367 /* On V2 devices the DualPoint Stick reports bare packets */
1368 dev = priv->dev2;
1369 dev2 = psmouse->dev;
1370 } else if (unlikely(IS_ERR_OR_NULL(priv->dev3))) {
1371 /* Register dev3 mouse if we received PS/2 packet first time */
1372 if (!IS_ERR(priv->dev3))
1373 psmouse_queue_work(psmouse, &priv->dev3_register_work,
1375 return;
1376 } else {
1377 dev = priv->dev3;
1380 if (report_buttons)
1381 alps_report_buttons(dev, dev2,
1382 packet[0] & 1, packet[0] & 2, packet[0] & 4);
1384 input_report_rel(dev, REL_X,
1385 packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
1386 input_report_rel(dev, REL_Y,
1387 packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
1389 input_sync(dev);
1392 static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
1394 struct alps_data *priv = psmouse->private;
1396 if (psmouse->pktcnt < 6)
1397 return PSMOUSE_GOOD_DATA;
1399 if (psmouse->pktcnt == 6) {
1401 * Start a timer to flush the packet if it ends up last
1402 * 6-byte packet in the stream. Timer needs to fire
1403 * psmouse core times out itself. 20 ms should be enough
1404 * to decide if we are getting more data or not.
1406 mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
1407 return PSMOUSE_GOOD_DATA;
1410 del_timer(&priv->timer);
1412 if (psmouse->packet[6] & 0x80) {
1415 * Highest bit is set - that means we either had
1416 * complete ALPS packet and this is start of the
1417 * next packet or we got garbage.
1420 if (((psmouse->packet[3] |
1421 psmouse->packet[4] |
1422 psmouse->packet[5]) & 0x80) ||
1423 (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) {
1424 psmouse_dbg(psmouse,
1425 "refusing packet %4ph (suspected interleaved ps/2)\n",
1426 psmouse->packet + 3);
1427 return PSMOUSE_BAD_DATA;
1430 priv->process_packet(psmouse);
1432 /* Continue with the next packet */
1433 psmouse->packet[0] = psmouse->packet[6];
1434 psmouse->pktcnt = 1;
1436 } else {
1439 * High bit is 0 - that means that we indeed got a PS/2
1440 * packet in the middle of ALPS packet.
1442 * There is also possibility that we got 6-byte ALPS
1443 * packet followed by 3-byte packet from trackpoint. We
1444 * can not distinguish between these 2 scenarios but
1445 * because the latter is unlikely to happen in course of
1446 * normal operation (user would need to press all
1447 * buttons on the pad and start moving trackpoint
1448 * without touching the pad surface) we assume former.
1449 * Even if we are wrong the wost thing that would happen
1450 * the cursor would jump but we should not get protocol
1451 * de-synchronization.
1454 alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
1455 false);
1458 * Continue with the standard ALPS protocol handling,
1459 * but make sure we won't process it as an interleaved
1460 * packet again, which may happen if all buttons are
1461 * pressed. To avoid this let's reset the 4th bit which
1462 * is normally 1.
1464 psmouse->packet[3] = psmouse->packet[6] & 0xf7;
1465 psmouse->pktcnt = 4;
1468 return PSMOUSE_GOOD_DATA;
1471 static void alps_flush_packet(unsigned long data)
1473 struct psmouse *psmouse = (struct psmouse *)data;
1474 struct alps_data *priv = psmouse->private;
1476 serio_pause_rx(psmouse->ps2dev.serio);
1478 if (psmouse->pktcnt == psmouse->pktsize) {
1481 * We did not any more data in reasonable amount of time.
1482 * Validate the last 3 bytes and process as a standard
1483 * ALPS packet.
1485 if ((psmouse->packet[3] |
1486 psmouse->packet[4] |
1487 psmouse->packet[5]) & 0x80) {
1488 psmouse_dbg(psmouse,
1489 "refusing packet %3ph (suspected interleaved ps/2)\n",
1490 psmouse->packet + 3);
1491 } else {
1492 priv->process_packet(psmouse);
1494 psmouse->pktcnt = 0;
1497 serio_continue_rx(psmouse->ps2dev.serio);
1500 static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
1502 struct alps_data *priv = psmouse->private;
1505 * Check if we are dealing with a bare PS/2 packet, presumably from
1506 * a device connected to the external PS/2 port. Because bare PS/2
1507 * protocol does not have enough constant bits to self-synchronize
1508 * properly we only do this if the device is fully synchronized.
1509 * Can not distinguish V8's first byte from PS/2 packet's
1511 if (priv->proto_version != ALPS_PROTO_V8 &&
1512 !psmouse->out_of_sync_cnt &&
1513 (psmouse->packet[0] & 0xc8) == 0x08) {
1515 if (psmouse->pktcnt == 3) {
1516 alps_report_bare_ps2_packet(psmouse, psmouse->packet,
1517 true);
1518 return PSMOUSE_FULL_PACKET;
1520 return PSMOUSE_GOOD_DATA;
1523 /* Check for PS/2 packet stuffed in the middle of ALPS packet. */
1525 if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
1526 psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
1527 return alps_handle_interleaved_ps2(psmouse);
1530 if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
1531 psmouse_dbg(psmouse,
1532 "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
1533 psmouse->packet[0], priv->mask0, priv->byte0);
1534 return PSMOUSE_BAD_DATA;
1537 /* Bytes 2 - pktsize should have 0 in the highest bit */
1538 if (priv->proto_version < ALPS_PROTO_V5 &&
1539 psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
1540 (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
1541 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1542 psmouse->pktcnt - 1,
1543 psmouse->packet[psmouse->pktcnt - 1]);
1545 if (priv->proto_version == ALPS_PROTO_V3_RUSHMORE &&
1546 psmouse->pktcnt == psmouse->pktsize) {
1548 * Some Dell boxes, such as Latitude E6440 or E7440
1549 * with closed lid, quite often smash last byte of
1550 * otherwise valid packet with 0xff. Given that the
1551 * next packet is very likely to be valid let's
1552 * report PSMOUSE_FULL_PACKET but not process data,
1553 * rather than reporting PSMOUSE_BAD_DATA and
1554 * filling the logs.
1556 return PSMOUSE_FULL_PACKET;
1559 return PSMOUSE_BAD_DATA;
1562 if ((priv->proto_version == ALPS_PROTO_V7 &&
1563 !alps_is_valid_package_v7(psmouse)) ||
1564 (priv->proto_version == ALPS_PROTO_V8 &&
1565 !alps_is_valid_package_ss4_v2(psmouse))) {
1566 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1567 psmouse->pktcnt - 1,
1568 psmouse->packet[psmouse->pktcnt - 1]);
1569 return PSMOUSE_BAD_DATA;
1572 if (psmouse->pktcnt == psmouse->pktsize) {
1573 priv->process_packet(psmouse);
1574 return PSMOUSE_FULL_PACKET;
1577 return PSMOUSE_GOOD_DATA;
1580 static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
1582 struct ps2dev *ps2dev = &psmouse->ps2dev;
1583 struct alps_data *priv = psmouse->private;
1584 int command;
1585 unsigned char *param;
1586 unsigned char dummy[4];
1588 BUG_ON(nibble > 0xf);
1590 command = priv->nibble_commands[nibble].command;
1591 param = (command & 0x0f00) ?
1592 dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
1594 if (ps2_command(ps2dev, param, command))
1595 return -1;
1597 return 0;
1600 static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
1602 struct ps2dev *ps2dev = &psmouse->ps2dev;
1603 struct alps_data *priv = psmouse->private;
1604 int i, nibble;
1606 if (ps2_command(ps2dev, NULL, priv->addr_command))
1607 return -1;
1609 for (i = 12; i >= 0; i -= 4) {
1610 nibble = (addr >> i) & 0xf;
1611 if (alps_command_mode_send_nibble(psmouse, nibble))
1612 return -1;
1615 return 0;
1618 static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1620 struct ps2dev *ps2dev = &psmouse->ps2dev;
1621 unsigned char param[4];
1623 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1624 return -1;
1627 * The address being read is returned in the first two bytes
1628 * of the result. Check that this address matches the expected
1629 * address.
1631 if (addr != ((param[0] << 8) | param[1]))
1632 return -1;
1634 return param[2];
1637 static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1639 if (alps_command_mode_set_addr(psmouse, addr))
1640 return -1;
1641 return __alps_command_mode_read_reg(psmouse, addr);
1644 static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
1646 if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
1647 return -1;
1648 if (alps_command_mode_send_nibble(psmouse, value & 0xf))
1649 return -1;
1650 return 0;
1653 static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
1654 u8 value)
1656 if (alps_command_mode_set_addr(psmouse, addr))
1657 return -1;
1658 return __alps_command_mode_write_reg(psmouse, value);
1661 static int alps_rpt_cmd(struct psmouse *psmouse, int init_command,
1662 int repeated_command, unsigned char *param)
1664 struct ps2dev *ps2dev = &psmouse->ps2dev;
1666 param[0] = 0;
1667 if (init_command && ps2_command(ps2dev, param, init_command))
1668 return -EIO;
1670 if (ps2_command(ps2dev, NULL, repeated_command) ||
1671 ps2_command(ps2dev, NULL, repeated_command) ||
1672 ps2_command(ps2dev, NULL, repeated_command))
1673 return -EIO;
1675 param[0] = param[1] = param[2] = 0xff;
1676 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1677 return -EIO;
1679 psmouse_dbg(psmouse, "%2.2X report: %3ph\n",
1680 repeated_command, param);
1681 return 0;
1684 static bool alps_check_valid_firmware_id(unsigned char id[])
1686 if (id[0] == 0x73)
1687 return true;
1689 if (id[0] == 0x88 &&
1690 (id[1] == 0x07 ||
1691 id[1] == 0x08 ||
1692 (id[1] & 0xf0) == 0xb0 ||
1693 (id[1] & 0xf0) == 0xc0)) {
1694 return true;
1697 return false;
1700 static int alps_enter_command_mode(struct psmouse *psmouse)
1702 unsigned char param[4];
1704 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) {
1705 psmouse_err(psmouse, "failed to enter command mode\n");
1706 return -1;
1709 if (!alps_check_valid_firmware_id(param)) {
1710 psmouse_dbg(psmouse,
1711 "unknown response while entering command mode\n");
1712 return -1;
1714 return 0;
1717 static inline int alps_exit_command_mode(struct psmouse *psmouse)
1719 struct ps2dev *ps2dev = &psmouse->ps2dev;
1720 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
1721 return -1;
1722 return 0;
1726 * For DualPoint devices select the device that should respond to
1727 * subsequent commands. It looks like glidepad is behind stickpointer,
1728 * I'd thought it would be other way around...
1730 static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
1732 struct ps2dev *ps2dev = &psmouse->ps2dev;
1733 int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
1735 if (ps2_command(ps2dev, NULL, cmd) ||
1736 ps2_command(ps2dev, NULL, cmd) ||
1737 ps2_command(ps2dev, NULL, cmd) ||
1738 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1739 return -1;
1741 /* we may get 3 more bytes, just ignore them */
1742 ps2_drain(ps2dev, 3, 100);
1744 return 0;
1747 static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
1749 struct ps2dev *ps2dev = &psmouse->ps2dev;
1751 /* Try ALPS magic knock - 4 disable before enable */
1752 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1753 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1754 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1755 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1756 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
1757 return -1;
1760 * Switch mouse to poll (remote) mode so motion data will not
1761 * get in our way
1763 return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
1766 static int alps_monitor_mode_send_word(struct psmouse *psmouse, u16 word)
1768 int i, nibble;
1771 * b0-b11 are valid bits, send sequence is inverse.
1772 * e.g. when word = 0x0123, nibble send sequence is 3, 2, 1
1774 for (i = 0; i <= 8; i += 4) {
1775 nibble = (word >> i) & 0xf;
1776 if (alps_command_mode_send_nibble(psmouse, nibble))
1777 return -1;
1780 return 0;
1783 static int alps_monitor_mode_write_reg(struct psmouse *psmouse,
1784 u16 addr, u16 value)
1786 struct ps2dev *ps2dev = &psmouse->ps2dev;
1788 /* 0x0A0 is the command to write the word */
1789 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE) ||
1790 alps_monitor_mode_send_word(psmouse, 0x0A0) ||
1791 alps_monitor_mode_send_word(psmouse, addr) ||
1792 alps_monitor_mode_send_word(psmouse, value) ||
1793 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1794 return -1;
1796 return 0;
1799 static int alps_monitor_mode(struct psmouse *psmouse, bool enable)
1801 struct ps2dev *ps2dev = &psmouse->ps2dev;
1803 if (enable) {
1804 /* EC E9 F5 F5 E7 E6 E7 E9 to enter monitor mode */
1805 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
1806 ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO) ||
1807 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1808 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1809 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1810 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1811 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1812 ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO))
1813 return -1;
1814 } else {
1815 /* EC to exit monitor mode */
1816 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP))
1817 return -1;
1820 return 0;
1823 static int alps_absolute_mode_v6(struct psmouse *psmouse)
1825 u16 reg_val = 0x181;
1826 int ret = -1;
1828 /* enter monitor mode, to write the register */
1829 if (alps_monitor_mode(psmouse, true))
1830 return -1;
1832 ret = alps_monitor_mode_write_reg(psmouse, 0x000, reg_val);
1834 if (alps_monitor_mode(psmouse, false))
1835 ret = -1;
1837 return ret;
1840 static int alps_get_status(struct psmouse *psmouse, char *param)
1842 /* Get status: 0xF5 0xF5 0xF5 0xE9 */
1843 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param))
1844 return -1;
1846 return 0;
1850 * Turn touchpad tapping on or off. The sequences are:
1851 * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
1852 * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
1853 * My guess that 0xE9 (GetInfo) is here as a sync point.
1854 * For models that also have stickpointer (DualPoints) its tapping
1855 * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
1856 * we don't fiddle with it.
1858 static int alps_tap_mode(struct psmouse *psmouse, int enable)
1860 struct ps2dev *ps2dev = &psmouse->ps2dev;
1861 int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
1862 unsigned char tap_arg = enable ? 0x0A : 0x00;
1863 unsigned char param[4];
1865 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
1866 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1867 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1868 ps2_command(ps2dev, &tap_arg, cmd))
1869 return -1;
1871 if (alps_get_status(psmouse, param))
1872 return -1;
1874 return 0;
1878 * alps_poll() - poll the touchpad for current motion packet.
1879 * Used in resync.
1881 static int alps_poll(struct psmouse *psmouse)
1883 struct alps_data *priv = psmouse->private;
1884 unsigned char buf[sizeof(psmouse->packet)];
1885 bool poll_failed;
1887 if (priv->flags & ALPS_PASS)
1888 alps_passthrough_mode_v2(psmouse, true);
1890 poll_failed = ps2_command(&psmouse->ps2dev, buf,
1891 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
1893 if (priv->flags & ALPS_PASS)
1894 alps_passthrough_mode_v2(psmouse, false);
1896 if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
1897 return -1;
1899 if ((psmouse->badbyte & 0xc8) == 0x08) {
1901 * Poll the track stick ...
1903 if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
1904 return -1;
1907 memcpy(psmouse->packet, buf, sizeof(buf));
1908 return 0;
1911 static int alps_hw_init_v1_v2(struct psmouse *psmouse)
1913 struct alps_data *priv = psmouse->private;
1915 if ((priv->flags & ALPS_PASS) &&
1916 alps_passthrough_mode_v2(psmouse, true)) {
1917 return -1;
1920 if (alps_tap_mode(psmouse, true)) {
1921 psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
1922 return -1;
1925 if (alps_absolute_mode_v1_v2(psmouse)) {
1926 psmouse_err(psmouse, "Failed to enable absolute mode\n");
1927 return -1;
1930 if ((priv->flags & ALPS_PASS) &&
1931 alps_passthrough_mode_v2(psmouse, false)) {
1932 return -1;
1935 /* ALPS needs stream mode, otherwise it won't report any data */
1936 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
1937 psmouse_err(psmouse, "Failed to enable stream mode\n");
1938 return -1;
1941 return 0;
1944 static int alps_hw_init_v6(struct psmouse *psmouse)
1946 unsigned char param[2] = {0xC8, 0x14};
1948 /* Enter passthrough mode to let trackpoint enter 6byte raw mode */
1949 if (alps_passthrough_mode_v2(psmouse, true))
1950 return -1;
1952 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1953 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1954 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1955 ps2_command(&psmouse->ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
1956 ps2_command(&psmouse->ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
1957 return -1;
1959 if (alps_passthrough_mode_v2(psmouse, false))
1960 return -1;
1962 if (alps_absolute_mode_v6(psmouse)) {
1963 psmouse_err(psmouse, "Failed to enable absolute mode\n");
1964 return -1;
1967 return 0;
1971 * Enable or disable passthrough mode to the trackstick.
1973 static int alps_passthrough_mode_v3(struct psmouse *psmouse,
1974 int reg_base, bool enable)
1976 int reg_val, ret = -1;
1978 if (alps_enter_command_mode(psmouse))
1979 return -1;
1981 reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x0008);
1982 if (reg_val == -1)
1983 goto error;
1985 if (enable)
1986 reg_val |= 0x01;
1987 else
1988 reg_val &= ~0x01;
1990 ret = __alps_command_mode_write_reg(psmouse, reg_val);
1992 error:
1993 if (alps_exit_command_mode(psmouse))
1994 ret = -1;
1995 return ret;
1998 /* Must be in command mode when calling this function */
1999 static int alps_absolute_mode_v3(struct psmouse *psmouse)
2001 int reg_val;
2003 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
2004 if (reg_val == -1)
2005 return -1;
2007 reg_val |= 0x06;
2008 if (__alps_command_mode_write_reg(psmouse, reg_val))
2009 return -1;
2011 return 0;
2014 static int alps_probe_trackstick_v3(struct psmouse *psmouse, int reg_base)
2016 int ret = -EIO, reg_val;
2018 if (alps_enter_command_mode(psmouse))
2019 goto error;
2021 reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x08);
2022 if (reg_val == -1)
2023 goto error;
2025 /* bit 7: trackstick is present */
2026 ret = reg_val & 0x80 ? 0 : -ENODEV;
2028 error:
2029 alps_exit_command_mode(psmouse);
2030 return ret;
2033 static int alps_setup_trackstick_v3(struct psmouse *psmouse, int reg_base)
2035 struct ps2dev *ps2dev = &psmouse->ps2dev;
2036 int ret = 0;
2037 unsigned char param[4];
2039 if (alps_passthrough_mode_v3(psmouse, reg_base, true))
2040 return -EIO;
2043 * E7 report for the trackstick
2045 * There have been reports of failures to seem to trace back
2046 * to the above trackstick check failing. When these occur
2047 * this E7 report fails, so when that happens we continue
2048 * with the assumption that there isn't a trackstick after
2049 * all.
2051 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_SETSCALE21, param)) {
2052 psmouse_warn(psmouse, "Failed to initialize trackstick (E7 report failed)\n");
2053 ret = -ENODEV;
2054 } else {
2055 psmouse_dbg(psmouse, "trackstick E7 report: %3ph\n", param);
2058 * Not sure what this does, but it is absolutely
2059 * essential. Without it, the touchpad does not
2060 * work at all and the trackstick just emits normal
2061 * PS/2 packets.
2063 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2064 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2065 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2066 alps_command_mode_send_nibble(psmouse, 0x9) ||
2067 alps_command_mode_send_nibble(psmouse, 0x4)) {
2068 psmouse_err(psmouse,
2069 "Error sending magic E6 sequence\n");
2070 ret = -EIO;
2071 goto error;
2075 * This ensures the trackstick packets are in the format
2076 * supported by this driver. If bit 1 isn't set the packet
2077 * format is different.
2079 if (alps_enter_command_mode(psmouse) ||
2080 alps_command_mode_write_reg(psmouse,
2081 reg_base + 0x08, 0x82) ||
2082 alps_exit_command_mode(psmouse))
2083 ret = -EIO;
2086 error:
2087 if (alps_passthrough_mode_v3(psmouse, reg_base, false))
2088 ret = -EIO;
2090 return ret;
2093 static int alps_hw_init_v3(struct psmouse *psmouse)
2095 struct ps2dev *ps2dev = &psmouse->ps2dev;
2096 int reg_val;
2097 unsigned char param[4];
2099 reg_val = alps_probe_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE);
2100 if (reg_val == -EIO)
2101 goto error;
2103 if (reg_val == 0 &&
2104 alps_setup_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE) == -EIO)
2105 goto error;
2107 if (alps_enter_command_mode(psmouse) ||
2108 alps_absolute_mode_v3(psmouse)) {
2109 psmouse_err(psmouse, "Failed to enter absolute mode\n");
2110 goto error;
2113 reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
2114 if (reg_val == -1)
2115 goto error;
2116 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
2117 goto error;
2119 reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
2120 if (reg_val == -1)
2121 goto error;
2122 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
2123 goto error;
2125 if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
2126 goto error;
2127 if (__alps_command_mode_write_reg(psmouse, 0x04))
2128 goto error;
2130 if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
2131 goto error;
2132 if (__alps_command_mode_write_reg(psmouse, 0x03))
2133 goto error;
2135 if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
2136 goto error;
2137 if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
2138 goto error;
2140 if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
2141 goto error;
2142 if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
2143 goto error;
2145 alps_exit_command_mode(psmouse);
2147 /* Set rate and enable data reporting */
2148 param[0] = 0x64;
2149 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
2150 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
2151 psmouse_err(psmouse, "Failed to enable data reporting\n");
2152 return -1;
2155 return 0;
2157 error:
2159 * Leaving the touchpad in command mode will essentially render
2160 * it unusable until the machine reboots, so exit it here just
2161 * to be safe
2163 alps_exit_command_mode(psmouse);
2164 return -1;
2167 static int alps_get_v3_v7_resolution(struct psmouse *psmouse, int reg_pitch)
2169 int reg, x_pitch, y_pitch, x_electrode, y_electrode, x_phys, y_phys;
2170 struct alps_data *priv = psmouse->private;
2172 reg = alps_command_mode_read_reg(psmouse, reg_pitch);
2173 if (reg < 0)
2174 return reg;
2176 x_pitch = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
2177 x_pitch = 50 + 2 * x_pitch; /* In 0.1 mm units */
2179 y_pitch = (char)reg >> 4; /* sign extend upper 4 bits */
2180 y_pitch = 36 + 2 * y_pitch; /* In 0.1 mm units */
2182 reg = alps_command_mode_read_reg(psmouse, reg_pitch + 1);
2183 if (reg < 0)
2184 return reg;
2186 x_electrode = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
2187 x_electrode = 17 + x_electrode;
2189 y_electrode = (char)reg >> 4; /* sign extend upper 4 bits */
2190 y_electrode = 13 + y_electrode;
2192 x_phys = x_pitch * (x_electrode - 1); /* In 0.1 mm units */
2193 y_phys = y_pitch * (y_electrode - 1); /* In 0.1 mm units */
2195 priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
2196 priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
2198 psmouse_dbg(psmouse,
2199 "pitch %dx%d num-electrodes %dx%d physical size %dx%d mm res %dx%d\n",
2200 x_pitch, y_pitch, x_electrode, y_electrode,
2201 x_phys / 10, y_phys / 10, priv->x_res, priv->y_res);
2203 return 0;
2206 static int alps_hw_init_rushmore_v3(struct psmouse *psmouse)
2208 struct alps_data *priv = psmouse->private;
2209 struct ps2dev *ps2dev = &psmouse->ps2dev;
2210 int reg_val, ret = -1;
2212 if (priv->flags & ALPS_DUALPOINT) {
2213 reg_val = alps_setup_trackstick_v3(psmouse,
2214 ALPS_REG_BASE_RUSHMORE);
2215 if (reg_val == -EIO)
2216 goto error;
2219 if (alps_enter_command_mode(psmouse) ||
2220 alps_command_mode_read_reg(psmouse, 0xc2d9) == -1 ||
2221 alps_command_mode_write_reg(psmouse, 0xc2cb, 0x00))
2222 goto error;
2224 if (alps_get_v3_v7_resolution(psmouse, 0xc2da))
2225 goto error;
2227 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c6);
2228 if (reg_val == -1)
2229 goto error;
2230 if (__alps_command_mode_write_reg(psmouse, reg_val & 0xfd))
2231 goto error;
2233 if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
2234 goto error;
2236 /* enter absolute mode */
2237 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
2238 if (reg_val == -1)
2239 goto error;
2240 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
2241 goto error;
2243 alps_exit_command_mode(psmouse);
2244 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2246 error:
2247 alps_exit_command_mode(psmouse);
2248 return ret;
2251 /* Must be in command mode when calling this function */
2252 static int alps_absolute_mode_v4(struct psmouse *psmouse)
2254 int reg_val;
2256 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
2257 if (reg_val == -1)
2258 return -1;
2260 reg_val |= 0x02;
2261 if (__alps_command_mode_write_reg(psmouse, reg_val))
2262 return -1;
2264 return 0;
2267 static int alps_hw_init_v4(struct psmouse *psmouse)
2269 struct ps2dev *ps2dev = &psmouse->ps2dev;
2270 unsigned char param[4];
2272 if (alps_enter_command_mode(psmouse))
2273 goto error;
2275 if (alps_absolute_mode_v4(psmouse)) {
2276 psmouse_err(psmouse, "Failed to enter absolute mode\n");
2277 goto error;
2280 if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
2281 goto error;
2283 if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
2284 goto error;
2286 if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
2287 goto error;
2289 if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
2290 goto error;
2292 if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
2293 goto error;
2295 if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
2296 goto error;
2298 if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
2299 goto error;
2301 if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
2302 goto error;
2304 alps_exit_command_mode(psmouse);
2307 * This sequence changes the output from a 9-byte to an
2308 * 8-byte format. All the same data seems to be present,
2309 * just in a more compact format.
2311 param[0] = 0xc8;
2312 param[1] = 0x64;
2313 param[2] = 0x50;
2314 if (ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2315 ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE) ||
2316 ps2_command(ps2dev, &param[2], PSMOUSE_CMD_SETRATE) ||
2317 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
2318 return -1;
2320 /* Set rate and enable data reporting */
2321 param[0] = 0x64;
2322 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
2323 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
2324 psmouse_err(psmouse, "Failed to enable data reporting\n");
2325 return -1;
2328 return 0;
2330 error:
2332 * Leaving the touchpad in command mode will essentially render
2333 * it unusable until the machine reboots, so exit it here just
2334 * to be safe
2336 alps_exit_command_mode(psmouse);
2337 return -1;
2340 static int alps_get_otp_values_ss4_v2(struct psmouse *psmouse,
2341 unsigned char index, unsigned char otp[])
2343 struct ps2dev *ps2dev = &psmouse->ps2dev;
2345 switch (index) {
2346 case 0:
2347 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2348 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2349 ps2_command(ps2dev, otp, PSMOUSE_CMD_GETINFO))
2350 return -1;
2352 break;
2354 case 1:
2355 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2356 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2357 ps2_command(ps2dev, otp, PSMOUSE_CMD_GETINFO))
2358 return -1;
2360 break;
2363 return 0;
2366 static int alps_update_device_area_ss4_v2(unsigned char otp[][4],
2367 struct alps_data *priv)
2369 int num_x_electrode;
2370 int num_y_electrode;
2371 int x_pitch, y_pitch, x_phys, y_phys;
2373 num_x_electrode = SS4_NUMSENSOR_XOFFSET + (otp[1][0] & 0x0F);
2374 num_y_electrode = SS4_NUMSENSOR_YOFFSET + ((otp[1][0] >> 4) & 0x0F);
2376 priv->x_max = (num_x_electrode - 1) * SS4_COUNT_PER_ELECTRODE;
2377 priv->y_max = (num_y_electrode - 1) * SS4_COUNT_PER_ELECTRODE;
2379 x_pitch = ((otp[1][2] >> 2) & 0x07) + SS4_MIN_PITCH_MM;
2380 y_pitch = ((otp[1][2] >> 5) & 0x07) + SS4_MIN_PITCH_MM;
2382 x_phys = x_pitch * (num_x_electrode - 1); /* In 0.1 mm units */
2383 y_phys = y_pitch * (num_y_electrode - 1); /* In 0.1 mm units */
2385 priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
2386 priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
2388 return 0;
2391 static int alps_update_btn_info_ss4_v2(unsigned char otp[][4],
2392 struct alps_data *priv)
2394 unsigned char is_btnless;
2396 is_btnless = (otp[1][1] >> 3) & 0x01;
2398 if (is_btnless)
2399 priv->flags |= ALPS_BUTTONPAD;
2401 return 0;
2404 static int alps_set_defaults_ss4_v2(struct psmouse *psmouse,
2405 struct alps_data *priv)
2407 unsigned char otp[2][4];
2409 memset(otp, 0, sizeof(otp));
2411 if (alps_get_otp_values_ss4_v2(psmouse, 0, &otp[0][0]) ||
2412 alps_get_otp_values_ss4_v2(psmouse, 1, &otp[1][0]))
2413 return -1;
2415 alps_update_device_area_ss4_v2(otp, priv);
2417 alps_update_btn_info_ss4_v2(otp, priv);
2419 return 0;
2422 static int alps_dolphin_get_device_area(struct psmouse *psmouse,
2423 struct alps_data *priv)
2425 struct ps2dev *ps2dev = &psmouse->ps2dev;
2426 unsigned char param[4] = {0};
2427 int num_x_electrode, num_y_electrode;
2429 if (alps_enter_command_mode(psmouse))
2430 return -1;
2432 param[0] = 0x0a;
2433 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
2434 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2435 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2436 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2437 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE))
2438 return -1;
2440 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
2441 return -1;
2444 * Dolphin's sensor line number is not fixed. It can be calculated
2445 * by adding the device's register value with DOLPHIN_PROFILE_X/YOFFSET.
2446 * Further more, we can get device's x_max and y_max by multiplying
2447 * sensor line number with DOLPHIN_COUNT_PER_ELECTRODE.
2449 * e.g. When we get register's sensor_x = 11 & sensor_y = 8,
2450 * real sensor line number X = 11 + 8 = 19, and
2451 * real sensor line number Y = 8 + 1 = 9.
2452 * So, x_max = (19 - 1) * 64 = 1152, and
2453 * y_max = (9 - 1) * 64 = 512.
2455 num_x_electrode = DOLPHIN_PROFILE_XOFFSET + (param[2] & 0x0F);
2456 num_y_electrode = DOLPHIN_PROFILE_YOFFSET + ((param[2] >> 4) & 0x0F);
2457 priv->x_bits = num_x_electrode;
2458 priv->y_bits = num_y_electrode;
2459 priv->x_max = (num_x_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2460 priv->y_max = (num_y_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2462 if (alps_exit_command_mode(psmouse))
2463 return -1;
2465 return 0;
2468 static int alps_hw_init_dolphin_v1(struct psmouse *psmouse)
2470 struct ps2dev *ps2dev = &psmouse->ps2dev;
2471 unsigned char param[2];
2473 /* This is dolphin "v1" as empirically defined by florin9doi */
2474 param[0] = 0x64;
2475 param[1] = 0x28;
2477 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2478 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2479 ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
2480 return -1;
2482 return 0;
2485 static int alps_hw_init_v7(struct psmouse *psmouse)
2487 struct ps2dev *ps2dev = &psmouse->ps2dev;
2488 int reg_val, ret = -1;
2490 if (alps_enter_command_mode(psmouse) ||
2491 alps_command_mode_read_reg(psmouse, 0xc2d9) == -1)
2492 goto error;
2494 if (alps_get_v3_v7_resolution(psmouse, 0xc397))
2495 goto error;
2497 if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
2498 goto error;
2500 reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
2501 if (reg_val == -1)
2502 goto error;
2503 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
2504 goto error;
2506 alps_exit_command_mode(psmouse);
2507 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2509 error:
2510 alps_exit_command_mode(psmouse);
2511 return ret;
2514 static int alps_hw_init_ss4_v2(struct psmouse *psmouse)
2516 struct ps2dev *ps2dev = &psmouse->ps2dev;
2517 char param[2] = {0x64, 0x28};
2518 int ret = -1;
2520 /* enter absolute mode */
2521 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2522 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2523 ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2524 ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE)) {
2525 goto error;
2528 /* T.B.D. Decread noise packet number, delete in the future */
2529 if (alps_exit_command_mode(psmouse) ||
2530 alps_enter_command_mode(psmouse) ||
2531 alps_command_mode_write_reg(psmouse, 0x001D, 0x20)) {
2532 goto error;
2534 alps_exit_command_mode(psmouse);
2536 return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2538 error:
2539 alps_exit_command_mode(psmouse);
2540 return ret;
2543 static int alps_set_protocol(struct psmouse *psmouse,
2544 struct alps_data *priv,
2545 const struct alps_protocol_info *protocol)
2547 psmouse->private = priv;
2549 setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
2551 priv->proto_version = protocol->version;
2552 priv->byte0 = protocol->byte0;
2553 priv->mask0 = protocol->mask0;
2554 priv->flags = protocol->flags;
2555 if (dmi_name_in_vendors("Dell"))
2556 priv->flags |= ALPS_DELL;
2558 priv->x_max = 2000;
2559 priv->y_max = 1400;
2560 priv->x_bits = 15;
2561 priv->y_bits = 11;
2563 switch (priv->proto_version) {
2564 case ALPS_PROTO_V1:
2565 case ALPS_PROTO_V2:
2566 priv->hw_init = alps_hw_init_v1_v2;
2567 priv->process_packet = alps_process_packet_v1_v2;
2568 priv->set_abs_params = alps_set_abs_params_st;
2569 priv->x_max = 1023;
2570 priv->y_max = 767;
2571 break;
2573 case ALPS_PROTO_V3:
2574 priv->hw_init = alps_hw_init_v3;
2575 priv->process_packet = alps_process_packet_v3;
2576 priv->set_abs_params = alps_set_abs_params_semi_mt;
2577 priv->decode_fields = alps_decode_pinnacle;
2578 priv->nibble_commands = alps_v3_nibble_commands;
2579 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2580 break;
2582 case ALPS_PROTO_V3_RUSHMORE:
2583 priv->hw_init = alps_hw_init_rushmore_v3;
2584 priv->process_packet = alps_process_packet_v3;
2585 priv->set_abs_params = alps_set_abs_params_semi_mt;
2586 priv->decode_fields = alps_decode_rushmore;
2587 priv->nibble_commands = alps_v3_nibble_commands;
2588 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2589 priv->x_bits = 16;
2590 priv->y_bits = 12;
2592 if (alps_probe_trackstick_v3(psmouse,
2593 ALPS_REG_BASE_RUSHMORE) < 0)
2594 priv->flags &= ~ALPS_DUALPOINT;
2596 break;
2598 case ALPS_PROTO_V4:
2599 priv->hw_init = alps_hw_init_v4;
2600 priv->process_packet = alps_process_packet_v4;
2601 priv->set_abs_params = alps_set_abs_params_semi_mt;
2602 priv->nibble_commands = alps_v4_nibble_commands;
2603 priv->addr_command = PSMOUSE_CMD_DISABLE;
2604 break;
2606 case ALPS_PROTO_V5:
2607 priv->hw_init = alps_hw_init_dolphin_v1;
2608 priv->process_packet = alps_process_touchpad_packet_v3_v5;
2609 priv->decode_fields = alps_decode_dolphin;
2610 priv->set_abs_params = alps_set_abs_params_semi_mt;
2611 priv->nibble_commands = alps_v3_nibble_commands;
2612 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2613 priv->x_bits = 23;
2614 priv->y_bits = 12;
2616 if (alps_dolphin_get_device_area(psmouse, priv))
2617 return -EIO;
2619 break;
2621 case ALPS_PROTO_V6:
2622 priv->hw_init = alps_hw_init_v6;
2623 priv->process_packet = alps_process_packet_v6;
2624 priv->set_abs_params = alps_set_abs_params_st;
2625 priv->nibble_commands = alps_v6_nibble_commands;
2626 priv->x_max = 2047;
2627 priv->y_max = 1535;
2628 break;
2630 case ALPS_PROTO_V7:
2631 priv->hw_init = alps_hw_init_v7;
2632 priv->process_packet = alps_process_packet_v7;
2633 priv->decode_fields = alps_decode_packet_v7;
2634 priv->set_abs_params = alps_set_abs_params_v7;
2635 priv->nibble_commands = alps_v3_nibble_commands;
2636 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2637 priv->x_max = 0xfff;
2638 priv->y_max = 0x7ff;
2640 if (priv->fw_ver[1] != 0xba)
2641 priv->flags |= ALPS_BUTTONPAD;
2643 break;
2645 case ALPS_PROTO_V8:
2646 priv->hw_init = alps_hw_init_ss4_v2;
2647 priv->process_packet = alps_process_packet_ss4_v2;
2648 priv->decode_fields = alps_decode_ss4_v2;
2649 priv->set_abs_params = alps_set_abs_params_ss4_v2;
2650 priv->nibble_commands = alps_v3_nibble_commands;
2651 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2653 if (alps_set_defaults_ss4_v2(psmouse, priv))
2654 return -EIO;
2656 break;
2659 return 0;
2662 static const struct alps_protocol_info *alps_match_table(unsigned char *e7,
2663 unsigned char *ec)
2665 const struct alps_model_info *model;
2666 int i;
2668 for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
2669 model = &alps_model_data[i];
2671 if (!memcmp(e7, model->signature, sizeof(model->signature)) &&
2672 (!model->command_mode_resp ||
2673 model->command_mode_resp == ec[2])) {
2675 return &model->protocol_info;
2679 return NULL;
2682 static int alps_identify(struct psmouse *psmouse, struct alps_data *priv)
2684 const struct alps_protocol_info *protocol;
2685 unsigned char e6[4], e7[4], ec[4];
2686 int error;
2689 * First try "E6 report".
2690 * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
2691 * The bits 0-2 of the first byte will be 1s if some buttons are
2692 * pressed.
2694 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2695 PSMOUSE_CMD_SETSCALE11, e6))
2696 return -EIO;
2698 if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100))
2699 return -EINVAL;
2702 * Now get the "E7" and "EC" reports. These will uniquely identify
2703 * most ALPS touchpads.
2705 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2706 PSMOUSE_CMD_SETSCALE21, e7) ||
2707 alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2708 PSMOUSE_CMD_RESET_WRAP, ec) ||
2709 alps_exit_command_mode(psmouse))
2710 return -EIO;
2712 protocol = alps_match_table(e7, ec);
2713 if (!protocol) {
2714 if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0x50 &&
2715 ec[0] == 0x73 && (ec[1] == 0x01 || ec[1] == 0x02)) {
2716 protocol = &alps_v5_protocol_data;
2717 } else if (ec[0] == 0x88 &&
2718 ((ec[1] & 0xf0) == 0xb0 || (ec[1] & 0xf0) == 0xc0)) {
2719 protocol = &alps_v7_protocol_data;
2720 } else if (ec[0] == 0x88 && ec[1] == 0x08) {
2721 protocol = &alps_v3_rushmore_data;
2722 } else if (ec[0] == 0x88 && ec[1] == 0x07 &&
2723 ec[2] >= 0x90 && ec[2] <= 0x9d) {
2724 protocol = &alps_v3_protocol_data;
2725 } else if (e7[0] == 0x73 && e7[1] == 0x03 &&
2726 e7[2] == 0x14 && ec[1] == 0x02) {
2727 protocol = &alps_v8_protocol_data;
2728 } else {
2729 psmouse_dbg(psmouse,
2730 "Likely not an ALPS touchpad: E7=%3ph, EC=%3ph\n", e7, ec);
2731 return -EINVAL;
2735 if (priv) {
2736 /* Save the Firmware version */
2737 memcpy(priv->fw_ver, ec, 3);
2738 error = alps_set_protocol(psmouse, priv, protocol);
2739 if (error)
2740 return error;
2743 return 0;
2746 static int alps_reconnect(struct psmouse *psmouse)
2748 struct alps_data *priv = psmouse->private;
2750 psmouse_reset(psmouse);
2752 if (alps_identify(psmouse, priv) < 0)
2753 return -1;
2755 return priv->hw_init(psmouse);
2758 static void alps_disconnect(struct psmouse *psmouse)
2760 struct alps_data *priv = psmouse->private;
2762 psmouse_reset(psmouse);
2763 del_timer_sync(&priv->timer);
2764 if (priv->dev2)
2765 input_unregister_device(priv->dev2);
2766 if (!IS_ERR_OR_NULL(priv->dev3))
2767 input_unregister_device(priv->dev3);
2768 kfree(priv);
2771 static void alps_set_abs_params_st(struct alps_data *priv,
2772 struct input_dev *dev1)
2774 input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
2775 input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
2776 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
2779 static void alps_set_abs_params_mt_common(struct alps_data *priv,
2780 struct input_dev *dev1)
2782 input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
2783 input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
2785 input_abs_set_res(dev1, ABS_MT_POSITION_X, priv->x_res);
2786 input_abs_set_res(dev1, ABS_MT_POSITION_Y, priv->y_res);
2788 set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
2789 set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
2792 static void alps_set_abs_params_semi_mt(struct alps_data *priv,
2793 struct input_dev *dev1)
2795 alps_set_abs_params_mt_common(priv, dev1);
2796 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
2798 input_mt_init_slots(dev1, MAX_TOUCHES,
2799 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
2800 INPUT_MT_SEMI_MT);
2803 static void alps_set_abs_params_v7(struct alps_data *priv,
2804 struct input_dev *dev1)
2806 alps_set_abs_params_mt_common(priv, dev1);
2807 set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
2809 input_mt_init_slots(dev1, MAX_TOUCHES,
2810 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
2811 INPUT_MT_TRACK);
2813 set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
2816 static void alps_set_abs_params_ss4_v2(struct alps_data *priv,
2817 struct input_dev *dev1)
2819 alps_set_abs_params_mt_common(priv, dev1);
2820 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
2821 set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
2823 input_mt_init_slots(dev1, MAX_TOUCHES,
2824 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
2825 INPUT_MT_TRACK);
2828 int alps_init(struct psmouse *psmouse)
2830 struct alps_data *priv = psmouse->private;
2831 struct input_dev *dev1 = psmouse->dev;
2832 int error;
2834 error = priv->hw_init(psmouse);
2835 if (error)
2836 goto init_fail;
2839 * Undo part of setup done for us by psmouse core since touchpad
2840 * is not a relative device.
2842 __clear_bit(EV_REL, dev1->evbit);
2843 __clear_bit(REL_X, dev1->relbit);
2844 __clear_bit(REL_Y, dev1->relbit);
2847 * Now set up our capabilities.
2849 dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
2850 dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
2851 dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
2852 dev1->keybit[BIT_WORD(BTN_LEFT)] |=
2853 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
2855 dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
2857 priv->set_abs_params(priv, dev1);
2859 if (priv->flags & ALPS_WHEEL) {
2860 dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
2861 dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
2864 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
2865 dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
2866 dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
2869 if (priv->flags & ALPS_FOUR_BUTTONS) {
2870 dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
2871 dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
2872 dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
2873 dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
2874 } else if (priv->flags & ALPS_BUTTONPAD) {
2875 set_bit(INPUT_PROP_BUTTONPAD, dev1->propbit);
2876 clear_bit(BTN_RIGHT, dev1->keybit);
2877 } else {
2878 dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
2881 if (priv->flags & ALPS_DUALPOINT) {
2882 struct input_dev *dev2;
2884 dev2 = input_allocate_device();
2885 if (!dev2) {
2886 psmouse_err(psmouse,
2887 "failed to allocate trackstick device\n");
2888 error = -ENOMEM;
2889 goto init_fail;
2892 snprintf(priv->phys2, sizeof(priv->phys2), "%s/input1",
2893 psmouse->ps2dev.serio->phys);
2894 dev2->phys = priv->phys2;
2897 * format of input device name is: "protocol vendor name"
2898 * see function psmouse_switch_protocol() in psmouse-base.c
2900 dev2->name = "AlpsPS/2 ALPS DualPoint Stick";
2902 dev2->id.bustype = BUS_I8042;
2903 dev2->id.vendor = 0x0002;
2904 dev2->id.product = PSMOUSE_ALPS;
2905 dev2->id.version = priv->proto_version;
2906 dev2->dev.parent = &psmouse->ps2dev.serio->dev;
2908 input_set_capability(dev2, EV_REL, REL_X);
2909 input_set_capability(dev2, EV_REL, REL_Y);
2910 input_set_capability(dev2, EV_KEY, BTN_LEFT);
2911 input_set_capability(dev2, EV_KEY, BTN_RIGHT);
2912 input_set_capability(dev2, EV_KEY, BTN_MIDDLE);
2914 __set_bit(INPUT_PROP_POINTER, dev2->propbit);
2915 __set_bit(INPUT_PROP_POINTING_STICK, dev2->propbit);
2917 error = input_register_device(dev2);
2918 if (error) {
2919 psmouse_err(psmouse,
2920 "failed to register trackstick device: %d\n",
2921 error);
2922 input_free_device(dev2);
2923 goto init_fail;
2926 priv->dev2 = dev2;
2929 priv->psmouse = psmouse;
2931 INIT_DELAYED_WORK(&priv->dev3_register_work,
2932 alps_register_bare_ps2_mouse);
2934 psmouse->protocol_handler = alps_process_byte;
2935 psmouse->poll = alps_poll;
2936 psmouse->disconnect = alps_disconnect;
2937 psmouse->reconnect = alps_reconnect;
2938 psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
2940 /* We are having trouble resyncing ALPS touchpads so disable it for now */
2941 psmouse->resync_time = 0;
2943 /* Allow 2 invalid packets without resetting device */
2944 psmouse->resetafter = psmouse->pktsize * 2;
2946 return 0;
2948 init_fail:
2949 psmouse_reset(psmouse);
2951 * Even though we did not allocate psmouse->private we do free
2952 * it here.
2954 kfree(psmouse->private);
2955 psmouse->private = NULL;
2956 return error;
2959 int alps_detect(struct psmouse *psmouse, bool set_properties)
2961 struct alps_data *priv;
2962 int error;
2964 error = alps_identify(psmouse, NULL);
2965 if (error)
2966 return error;
2969 * Reset the device to make sure it is fully operational:
2970 * on some laptops, like certain Dell Latitudes, we may
2971 * fail to properly detect presence of trackstick if device
2972 * has not been reset.
2974 psmouse_reset(psmouse);
2976 priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
2977 if (!priv)
2978 return -ENOMEM;
2980 error = alps_identify(psmouse, priv);
2981 if (error) {
2982 kfree(priv);
2983 return error;
2986 if (set_properties) {
2987 psmouse->vendor = "ALPS";
2988 psmouse->name = priv->flags & ALPS_DUALPOINT ?
2989 "DualPoint TouchPad" : "GlidePoint";
2990 psmouse->model = priv->proto_version;
2991 } else {
2993 * Destroy alps_data structure we allocated earlier since
2994 * this was just a "trial run". Otherwise we'll keep it
2995 * to be used by alps_init() which has to be called if
2996 * we succeed and set_properties is true.
2998 kfree(priv);
2999 psmouse->private = NULL;
3002 return 0;