Input: ALPS - move {addr,nibble}_command settings into alps_set_defaults()
[linux-2.6.git] / drivers / input / mouse / alps.c
blob2221a00b25d33df18fc0eabc7acf434714a6c5d2
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>
24 #include "psmouse.h"
25 #include "alps.h"
28 * Definitions for ALPS version 3 and 4 command mode protocol
30 #define ALPS_V3_X_MAX 2000
31 #define ALPS_V3_Y_MAX 1400
33 #define ALPS_BITMAP_X_BITS 15
34 #define ALPS_BITMAP_Y_BITS 11
36 #define ALPS_CMD_NIBBLE_10 0x01f2
38 static const struct alps_nibble_commands alps_v3_nibble_commands[] = {
39 { PSMOUSE_CMD_SETPOLL, 0x00 }, /* 0 */
40 { PSMOUSE_CMD_RESET_DIS, 0x00 }, /* 1 */
41 { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* 2 */
42 { PSMOUSE_CMD_SETRATE, 0x0a }, /* 3 */
43 { PSMOUSE_CMD_SETRATE, 0x14 }, /* 4 */
44 { PSMOUSE_CMD_SETRATE, 0x28 }, /* 5 */
45 { PSMOUSE_CMD_SETRATE, 0x3c }, /* 6 */
46 { PSMOUSE_CMD_SETRATE, 0x50 }, /* 7 */
47 { PSMOUSE_CMD_SETRATE, 0x64 }, /* 8 */
48 { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 9 */
49 { ALPS_CMD_NIBBLE_10, 0x00 }, /* a */
50 { PSMOUSE_CMD_SETRES, 0x00 }, /* b */
51 { PSMOUSE_CMD_SETRES, 0x01 }, /* c */
52 { PSMOUSE_CMD_SETRES, 0x02 }, /* d */
53 { PSMOUSE_CMD_SETRES, 0x03 }, /* e */
54 { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */
57 static const struct alps_nibble_commands alps_v4_nibble_commands[] = {
58 { PSMOUSE_CMD_ENABLE, 0x00 }, /* 0 */
59 { PSMOUSE_CMD_RESET_DIS, 0x00 }, /* 1 */
60 { PSMOUSE_CMD_SETSCALE21, 0x00 }, /* 2 */
61 { PSMOUSE_CMD_SETRATE, 0x0a }, /* 3 */
62 { PSMOUSE_CMD_SETRATE, 0x14 }, /* 4 */
63 { PSMOUSE_CMD_SETRATE, 0x28 }, /* 5 */
64 { PSMOUSE_CMD_SETRATE, 0x3c }, /* 6 */
65 { PSMOUSE_CMD_SETRATE, 0x50 }, /* 7 */
66 { PSMOUSE_CMD_SETRATE, 0x64 }, /* 8 */
67 { PSMOUSE_CMD_SETRATE, 0xc8 }, /* 9 */
68 { ALPS_CMD_NIBBLE_10, 0x00 }, /* a */
69 { PSMOUSE_CMD_SETRES, 0x00 }, /* b */
70 { PSMOUSE_CMD_SETRES, 0x01 }, /* c */
71 { PSMOUSE_CMD_SETRES, 0x02 }, /* d */
72 { PSMOUSE_CMD_SETRES, 0x03 }, /* e */
73 { PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */
77 #define ALPS_DUALPOINT 0x02 /* touchpad has trackstick */
78 #define ALPS_PASS 0x04 /* device has a pass-through port */
80 #define ALPS_WHEEL 0x08 /* hardware wheel present */
81 #define ALPS_FW_BK_1 0x10 /* front & back buttons present */
82 #define ALPS_FW_BK_2 0x20 /* front & back buttons present */
83 #define ALPS_FOUR_BUTTONS 0x40 /* 4 direction button present */
84 #define ALPS_PS2_INTERLEAVED 0x80 /* 3-byte PS/2 packet interleaved with
85 6-byte ALPS packet */
87 static const struct alps_model_info alps_model_data[] = {
88 { { 0x32, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* Toshiba Salellite Pro M10 */
89 { { 0x33, 0x02, 0x0a }, 0x00, ALPS_PROTO_V1, 0x88, 0xf8, 0 }, /* UMAX-530T */
90 { { 0x53, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
91 { { 0x53, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
92 { { 0x60, 0x03, 0xc8 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 }, /* HP ze1115 */
93 { { 0x63, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
94 { { 0x63, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
95 { { 0x63, 0x02, 0x28 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 }, /* Fujitsu Siemens S6010 */
96 { { 0x63, 0x02, 0x3c }, 0x00, ALPS_PROTO_V2, 0x8f, 0x8f, ALPS_WHEEL }, /* Toshiba Satellite S2400-103 */
97 { { 0x63, 0x02, 0x50 }, 0x00, ALPS_PROTO_V2, 0xef, 0xef, ALPS_FW_BK_1 }, /* NEC Versa L320 */
98 { { 0x63, 0x02, 0x64 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
99 { { 0x63, 0x03, 0xc8 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* Dell Latitude D800 */
100 { { 0x73, 0x00, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_DUALPOINT }, /* ThinkPad R61 8918-5QG */
101 { { 0x73, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
102 { { 0x73, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 }, /* Ahtec Laptop */
103 { { 0x20, 0x02, 0x0e }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* XXX */
104 { { 0x22, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT },
105 { { 0x22, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT }, /* Dell Latitude D600 */
106 /* Dell Latitude E5500, E6400, E6500, Precision M4400 */
107 { { 0x62, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xcf, 0xcf,
108 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED },
109 { { 0x73, 0x02, 0x50 }, 0x00, ALPS_PROTO_V2, 0xcf, 0xcf, ALPS_FOUR_BUTTONS }, /* Dell Vostro 1400 */
110 { { 0x52, 0x01, 0x14 }, 0x00, ALPS_PROTO_V2, 0xff, 0xff,
111 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED }, /* Toshiba Tecra A11-11L */
112 { { 0x73, 0x02, 0x64 }, 0x9b, ALPS_PROTO_V3, 0x8f, 0x8f, ALPS_DUALPOINT },
113 { { 0x73, 0x02, 0x64 }, 0x9d, ALPS_PROTO_V3, 0x8f, 0x8f, ALPS_DUALPOINT },
114 { { 0x73, 0x02, 0x64 }, 0x8a, ALPS_PROTO_V4, 0x8f, 0x8f, 0 },
117 static void alps_set_abs_params_st(struct alps_data *priv,
118 struct input_dev *dev1);
119 static void alps_set_abs_params_mt(struct alps_data *priv,
120 struct input_dev *dev1);
123 * XXX - this entry is suspicious. First byte has zero lower nibble,
124 * which is what a normal mouse would report. Also, the value 0x0e
125 * isn't valid per PS/2 spec.
128 /* Packet formats are described in Documentation/input/alps.txt */
130 static bool alps_is_valid_first_byte(struct alps_data *priv,
131 unsigned char data)
133 return (data & priv->mask0) == priv->byte0;
136 static void alps_report_buttons(struct psmouse *psmouse,
137 struct input_dev *dev1, struct input_dev *dev2,
138 int left, int right, int middle)
140 struct input_dev *dev;
143 * If shared button has already been reported on the
144 * other device (dev2) then this event should be also
145 * sent through that device.
147 dev = test_bit(BTN_LEFT, dev2->key) ? dev2 : dev1;
148 input_report_key(dev, BTN_LEFT, left);
150 dev = test_bit(BTN_RIGHT, dev2->key) ? dev2 : dev1;
151 input_report_key(dev, BTN_RIGHT, right);
153 dev = test_bit(BTN_MIDDLE, dev2->key) ? dev2 : dev1;
154 input_report_key(dev, BTN_MIDDLE, middle);
157 * Sync the _other_ device now, we'll do the first
158 * device later once we report the rest of the events.
160 input_sync(dev2);
163 static void alps_process_packet_v1_v2(struct psmouse *psmouse)
165 struct alps_data *priv = psmouse->private;
166 unsigned char *packet = psmouse->packet;
167 struct input_dev *dev = psmouse->dev;
168 struct input_dev *dev2 = priv->dev2;
169 int x, y, z, ges, fin, left, right, middle;
170 int back = 0, forward = 0;
172 if (priv->proto_version == ALPS_PROTO_V1) {
173 left = packet[2] & 0x10;
174 right = packet[2] & 0x08;
175 middle = 0;
176 x = packet[1] | ((packet[0] & 0x07) << 7);
177 y = packet[4] | ((packet[3] & 0x07) << 7);
178 z = packet[5];
179 } else {
180 left = packet[3] & 1;
181 right = packet[3] & 2;
182 middle = packet[3] & 4;
183 x = packet[1] | ((packet[2] & 0x78) << (7 - 3));
184 y = packet[4] | ((packet[3] & 0x70) << (7 - 4));
185 z = packet[5];
188 if (priv->flags & ALPS_FW_BK_1) {
189 back = packet[0] & 0x10;
190 forward = packet[2] & 4;
193 if (priv->flags & ALPS_FW_BK_2) {
194 back = packet[3] & 4;
195 forward = packet[2] & 4;
196 if ((middle = forward && back))
197 forward = back = 0;
200 ges = packet[2] & 1;
201 fin = packet[2] & 2;
203 if ((priv->flags & ALPS_DUALPOINT) && z == 127) {
204 input_report_rel(dev2, REL_X, (x > 383 ? (x - 768) : x));
205 input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y));
207 alps_report_buttons(psmouse, dev2, dev, left, right, middle);
209 input_sync(dev2);
210 return;
213 alps_report_buttons(psmouse, dev, dev2, left, right, middle);
215 /* Convert hardware tap to a reasonable Z value */
216 if (ges && !fin)
217 z = 40;
220 * A "tap and drag" operation is reported by the hardware as a transition
221 * from (!fin && ges) to (fin && ges). This should be translated to the
222 * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
224 if (ges && fin && !priv->prev_fin) {
225 input_report_abs(dev, ABS_X, x);
226 input_report_abs(dev, ABS_Y, y);
227 input_report_abs(dev, ABS_PRESSURE, 0);
228 input_report_key(dev, BTN_TOOL_FINGER, 0);
229 input_sync(dev);
231 priv->prev_fin = fin;
233 if (z > 30)
234 input_report_key(dev, BTN_TOUCH, 1);
235 if (z < 25)
236 input_report_key(dev, BTN_TOUCH, 0);
238 if (z > 0) {
239 input_report_abs(dev, ABS_X, x);
240 input_report_abs(dev, ABS_Y, y);
243 input_report_abs(dev, ABS_PRESSURE, z);
244 input_report_key(dev, BTN_TOOL_FINGER, z > 0);
246 if (priv->flags & ALPS_WHEEL)
247 input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
249 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
250 input_report_key(dev, BTN_FORWARD, forward);
251 input_report_key(dev, BTN_BACK, back);
254 if (priv->flags & ALPS_FOUR_BUTTONS) {
255 input_report_key(dev, BTN_0, packet[2] & 4);
256 input_report_key(dev, BTN_1, packet[0] & 0x10);
257 input_report_key(dev, BTN_2, packet[3] & 4);
258 input_report_key(dev, BTN_3, packet[0] & 0x20);
261 input_sync(dev);
265 * Process bitmap data from v3 and v4 protocols. Returns the number of
266 * fingers detected. A return value of 0 means at least one of the
267 * bitmaps was empty.
269 * The bitmaps don't have enough data to track fingers, so this function
270 * only generates points representing a bounding box of all contacts.
271 * These points are returned in x1, y1, x2, and y2 when the return value
272 * is greater than 0.
274 static int alps_process_bitmap(unsigned int x_map, unsigned int y_map,
275 int *x1, int *y1, int *x2, int *y2)
277 struct alps_bitmap_point {
278 int start_bit;
279 int num_bits;
282 int fingers_x = 0, fingers_y = 0, fingers;
283 int i, bit, prev_bit;
284 struct alps_bitmap_point x_low = {0,}, x_high = {0,};
285 struct alps_bitmap_point y_low = {0,}, y_high = {0,};
286 struct alps_bitmap_point *point;
288 if (!x_map || !y_map)
289 return 0;
291 *x1 = *y1 = *x2 = *y2 = 0;
293 prev_bit = 0;
294 point = &x_low;
295 for (i = 0; x_map != 0; i++, x_map >>= 1) {
296 bit = x_map & 1;
297 if (bit) {
298 if (!prev_bit) {
299 point->start_bit = i;
300 fingers_x++;
302 point->num_bits++;
303 } else {
304 if (prev_bit)
305 point = &x_high;
306 else
307 point->num_bits = 0;
309 prev_bit = bit;
313 * y bitmap is reversed for what we need (lower positions are in
314 * higher bits), so we process from the top end.
316 y_map = y_map << (sizeof(y_map) * BITS_PER_BYTE - ALPS_BITMAP_Y_BITS);
317 prev_bit = 0;
318 point = &y_low;
319 for (i = 0; y_map != 0; i++, y_map <<= 1) {
320 bit = y_map & (1 << (sizeof(y_map) * BITS_PER_BYTE - 1));
321 if (bit) {
322 if (!prev_bit) {
323 point->start_bit = i;
324 fingers_y++;
326 point->num_bits++;
327 } else {
328 if (prev_bit)
329 point = &y_high;
330 else
331 point->num_bits = 0;
333 prev_bit = bit;
337 * Fingers can overlap, so we use the maximum count of fingers
338 * on either axis as the finger count.
340 fingers = max(fingers_x, fingers_y);
343 * If total fingers is > 1 but either axis reports only a single
344 * contact, we have overlapping or adjacent fingers. For the
345 * purposes of creating a bounding box, divide the single contact
346 * (roughly) equally between the two points.
348 if (fingers > 1) {
349 if (fingers_x == 1) {
350 i = x_low.num_bits / 2;
351 x_low.num_bits = x_low.num_bits - i;
352 x_high.start_bit = x_low.start_bit + i;
353 x_high.num_bits = max(i, 1);
354 } else if (fingers_y == 1) {
355 i = y_low.num_bits / 2;
356 y_low.num_bits = y_low.num_bits - i;
357 y_high.start_bit = y_low.start_bit + i;
358 y_high.num_bits = max(i, 1);
362 *x1 = (ALPS_V3_X_MAX * (2 * x_low.start_bit + x_low.num_bits - 1)) /
363 (2 * (ALPS_BITMAP_X_BITS - 1));
364 *y1 = (ALPS_V3_Y_MAX * (2 * y_low.start_bit + y_low.num_bits - 1)) /
365 (2 * (ALPS_BITMAP_Y_BITS - 1));
367 if (fingers > 1) {
368 *x2 = (ALPS_V3_X_MAX * (2 * x_high.start_bit + x_high.num_bits - 1)) /
369 (2 * (ALPS_BITMAP_X_BITS - 1));
370 *y2 = (ALPS_V3_Y_MAX * (2 * y_high.start_bit + y_high.num_bits - 1)) /
371 (2 * (ALPS_BITMAP_Y_BITS - 1));
374 return fingers;
377 static void alps_set_slot(struct input_dev *dev, int slot, bool active,
378 int x, int y)
380 input_mt_slot(dev, slot);
381 input_mt_report_slot_state(dev, MT_TOOL_FINGER, active);
382 if (active) {
383 input_report_abs(dev, ABS_MT_POSITION_X, x);
384 input_report_abs(dev, ABS_MT_POSITION_Y, y);
388 static void alps_report_semi_mt_data(struct input_dev *dev, int num_fingers,
389 int x1, int y1, int x2, int y2)
391 alps_set_slot(dev, 0, num_fingers != 0, x1, y1);
392 alps_set_slot(dev, 1, num_fingers == 2, x2, y2);
395 static void alps_process_trackstick_packet_v3(struct psmouse *psmouse)
397 struct alps_data *priv = psmouse->private;
398 unsigned char *packet = psmouse->packet;
399 struct input_dev *dev = priv->dev2;
400 int x, y, z, left, right, middle;
402 /* Sanity check packet */
403 if (!(packet[0] & 0x40)) {
404 psmouse_dbg(psmouse, "Bad trackstick packet, discarding\n");
405 return;
409 * There's a special packet that seems to indicate the end
410 * of a stream of trackstick data. Filter these out.
412 if (packet[1] == 0x7f && packet[2] == 0x7f && packet[4] == 0x7f)
413 return;
415 x = (s8)(((packet[0] & 0x20) << 2) | (packet[1] & 0x7f));
416 y = (s8)(((packet[0] & 0x10) << 3) | (packet[2] & 0x7f));
417 z = (packet[4] & 0x7c) >> 2;
420 * The x and y values tend to be quite large, and when used
421 * alone the trackstick is difficult to use. Scale them down
422 * to compensate.
424 x /= 8;
425 y /= 8;
427 input_report_rel(dev, REL_X, x);
428 input_report_rel(dev, REL_Y, -y);
431 * Most ALPS models report the trackstick buttons in the touchpad
432 * packets, but a few report them here. No reliable way has been
433 * found to differentiate between the models upfront, so we enable
434 * the quirk in response to seeing a button press in the trackstick
435 * packet.
437 left = packet[3] & 0x01;
438 right = packet[3] & 0x02;
439 middle = packet[3] & 0x04;
441 if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) &&
442 (left || right || middle))
443 priv->quirks |= ALPS_QUIRK_TRACKSTICK_BUTTONS;
445 if (priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) {
446 input_report_key(dev, BTN_LEFT, left);
447 input_report_key(dev, BTN_RIGHT, right);
448 input_report_key(dev, BTN_MIDDLE, middle);
451 input_sync(dev);
452 return;
455 static void alps_process_touchpad_packet_v3(struct psmouse *psmouse)
457 struct alps_data *priv = psmouse->private;
458 unsigned char *packet = psmouse->packet;
459 struct input_dev *dev = psmouse->dev;
460 struct input_dev *dev2 = priv->dev2;
461 int x, y, z;
462 int left, right, middle;
463 int x1 = 0, y1 = 0, x2 = 0, y2 = 0;
464 int fingers = 0, bmap_fingers;
465 unsigned int x_bitmap, y_bitmap;
468 * There's no single feature of touchpad position and bitmap packets
469 * that can be used to distinguish between them. We rely on the fact
470 * that a bitmap packet should always follow a position packet with
471 * bit 6 of packet[4] set.
473 if (priv->multi_packet) {
475 * Sometimes a position packet will indicate a multi-packet
476 * sequence, but then what follows is another position
477 * packet. Check for this, and when it happens process the
478 * position packet as usual.
480 if (packet[0] & 0x40) {
481 fingers = (packet[5] & 0x3) + 1;
482 x_bitmap = ((packet[4] & 0x7e) << 8) |
483 ((packet[1] & 0x7f) << 2) |
484 ((packet[0] & 0x30) >> 4);
485 y_bitmap = ((packet[3] & 0x70) << 4) |
486 ((packet[2] & 0x7f) << 1) |
487 (packet[4] & 0x01);
489 bmap_fingers = alps_process_bitmap(x_bitmap, y_bitmap,
490 &x1, &y1, &x2, &y2);
493 * We shouldn't report more than one finger if
494 * we don't have two coordinates.
496 if (fingers > 1 && bmap_fingers < 2)
497 fingers = bmap_fingers;
499 /* Now process position packet */
500 packet = priv->multi_data;
501 } else {
502 priv->multi_packet = 0;
507 * Bit 6 of byte 0 is not usually set in position packets. The only
508 * times it seems to be set is in situations where the data is
509 * suspect anyway, e.g. a palm resting flat on the touchpad. Given
510 * this combined with the fact that this bit is useful for filtering
511 * out misidentified bitmap packets, we reject anything with this
512 * bit set.
514 if (packet[0] & 0x40)
515 return;
517 if (!priv->multi_packet && (packet[4] & 0x40)) {
518 priv->multi_packet = 1;
519 memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
520 return;
523 priv->multi_packet = 0;
525 left = packet[3] & 0x01;
526 right = packet[3] & 0x02;
527 middle = packet[3] & 0x04;
529 x = ((packet[1] & 0x7f) << 4) | ((packet[4] & 0x30) >> 2) |
530 ((packet[0] & 0x30) >> 4);
531 y = ((packet[2] & 0x7f) << 4) | (packet[4] & 0x0f);
532 z = packet[5] & 0x7f;
535 * Sometimes the hardware sends a single packet with z = 0
536 * in the middle of a stream. Real releases generate packets
537 * with x, y, and z all zero, so these seem to be flukes.
538 * Ignore them.
540 if (x && y && !z)
541 return;
544 * If we don't have MT data or the bitmaps were empty, we have
545 * to rely on ST data.
547 if (!fingers) {
548 x1 = x;
549 y1 = y;
550 fingers = z > 0 ? 1 : 0;
553 if (z >= 64)
554 input_report_key(dev, BTN_TOUCH, 1);
555 else
556 input_report_key(dev, BTN_TOUCH, 0);
558 alps_report_semi_mt_data(dev, fingers, x1, y1, x2, y2);
560 input_mt_report_finger_count(dev, fingers);
562 input_report_key(dev, BTN_LEFT, left);
563 input_report_key(dev, BTN_RIGHT, right);
564 input_report_key(dev, BTN_MIDDLE, middle);
566 if (z > 0) {
567 input_report_abs(dev, ABS_X, x);
568 input_report_abs(dev, ABS_Y, y);
570 input_report_abs(dev, ABS_PRESSURE, z);
572 input_sync(dev);
574 if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) {
575 left = packet[3] & 0x10;
576 right = packet[3] & 0x20;
577 middle = packet[3] & 0x40;
579 input_report_key(dev2, BTN_LEFT, left);
580 input_report_key(dev2, BTN_RIGHT, right);
581 input_report_key(dev2, BTN_MIDDLE, middle);
582 input_sync(dev2);
586 static void alps_process_packet_v3(struct psmouse *psmouse)
588 unsigned char *packet = psmouse->packet;
591 * v3 protocol packets come in three types, two representing
592 * touchpad data and one representing trackstick data.
593 * Trackstick packets seem to be distinguished by always
594 * having 0x3f in the last byte. This value has never been
595 * observed in the last byte of either of the other types
596 * of packets.
598 if (packet[5] == 0x3f) {
599 alps_process_trackstick_packet_v3(psmouse);
600 return;
603 alps_process_touchpad_packet_v3(psmouse);
606 static void alps_process_packet_v4(struct psmouse *psmouse)
608 struct alps_data *priv = psmouse->private;
609 unsigned char *packet = psmouse->packet;
610 struct input_dev *dev = psmouse->dev;
611 int offset;
612 int x, y, z;
613 int left, right;
614 int x1, y1, x2, y2;
615 int fingers = 0;
616 unsigned int x_bitmap, y_bitmap;
619 * v4 has a 6-byte encoding for bitmap data, but this data is
620 * broken up between 3 normal packets. Use priv->multi_packet to
621 * track our position in the bitmap packet.
623 if (packet[6] & 0x40) {
624 /* sync, reset position */
625 priv->multi_packet = 0;
628 if (WARN_ON_ONCE(priv->multi_packet > 2))
629 return;
631 offset = 2 * priv->multi_packet;
632 priv->multi_data[offset] = packet[6];
633 priv->multi_data[offset + 1] = packet[7];
635 if (++priv->multi_packet > 2) {
636 priv->multi_packet = 0;
638 x_bitmap = ((priv->multi_data[2] & 0x1f) << 10) |
639 ((priv->multi_data[3] & 0x60) << 3) |
640 ((priv->multi_data[0] & 0x3f) << 2) |
641 ((priv->multi_data[1] & 0x60) >> 5);
642 y_bitmap = ((priv->multi_data[5] & 0x01) << 10) |
643 ((priv->multi_data[3] & 0x1f) << 5) |
644 (priv->multi_data[1] & 0x1f);
646 fingers = alps_process_bitmap(x_bitmap, y_bitmap,
647 &x1, &y1, &x2, &y2);
649 /* Store MT data.*/
650 priv->fingers = fingers;
651 priv->x1 = x1;
652 priv->x2 = x2;
653 priv->y1 = y1;
654 priv->y2 = y2;
657 left = packet[4] & 0x01;
658 right = packet[4] & 0x02;
660 x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) |
661 ((packet[0] & 0x30) >> 4);
662 y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f);
663 z = packet[5] & 0x7f;
666 * If there were no contacts in the bitmap, use ST
667 * points in MT reports.
668 * If there were two contacts or more, report MT data.
670 if (priv->fingers < 2) {
671 x1 = x;
672 y1 = y;
673 fingers = z > 0 ? 1 : 0;
674 } else {
675 fingers = priv->fingers;
676 x1 = priv->x1;
677 x2 = priv->x2;
678 y1 = priv->y1;
679 y2 = priv->y2;
682 if (z >= 64)
683 input_report_key(dev, BTN_TOUCH, 1);
684 else
685 input_report_key(dev, BTN_TOUCH, 0);
687 alps_report_semi_mt_data(dev, fingers, x1, y1, x2, y2);
689 input_mt_report_finger_count(dev, fingers);
691 input_report_key(dev, BTN_LEFT, left);
692 input_report_key(dev, BTN_RIGHT, right);
694 if (z > 0) {
695 input_report_abs(dev, ABS_X, x);
696 input_report_abs(dev, ABS_Y, y);
698 input_report_abs(dev, ABS_PRESSURE, z);
700 input_sync(dev);
703 static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
704 unsigned char packet[],
705 bool report_buttons)
707 struct alps_data *priv = psmouse->private;
708 struct input_dev *dev2 = priv->dev2;
710 if (report_buttons)
711 alps_report_buttons(psmouse, dev2, psmouse->dev,
712 packet[0] & 1, packet[0] & 2, packet[0] & 4);
714 input_report_rel(dev2, REL_X,
715 packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
716 input_report_rel(dev2, REL_Y,
717 packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
719 input_sync(dev2);
722 static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
724 struct alps_data *priv = psmouse->private;
726 if (psmouse->pktcnt < 6)
727 return PSMOUSE_GOOD_DATA;
729 if (psmouse->pktcnt == 6) {
731 * Start a timer to flush the packet if it ends up last
732 * 6-byte packet in the stream. Timer needs to fire
733 * psmouse core times out itself. 20 ms should be enough
734 * to decide if we are getting more data or not.
736 mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
737 return PSMOUSE_GOOD_DATA;
740 del_timer(&priv->timer);
742 if (psmouse->packet[6] & 0x80) {
745 * Highest bit is set - that means we either had
746 * complete ALPS packet and this is start of the
747 * next packet or we got garbage.
750 if (((psmouse->packet[3] |
751 psmouse->packet[4] |
752 psmouse->packet[5]) & 0x80) ||
753 (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) {
754 psmouse_dbg(psmouse,
755 "refusing packet %4ph (suspected interleaved ps/2)\n",
756 psmouse->packet + 3);
757 return PSMOUSE_BAD_DATA;
760 priv->process_packet(psmouse);
762 /* Continue with the next packet */
763 psmouse->packet[0] = psmouse->packet[6];
764 psmouse->pktcnt = 1;
766 } else {
769 * High bit is 0 - that means that we indeed got a PS/2
770 * packet in the middle of ALPS packet.
772 * There is also possibility that we got 6-byte ALPS
773 * packet followed by 3-byte packet from trackpoint. We
774 * can not distinguish between these 2 scenarios but
775 * because the latter is unlikely to happen in course of
776 * normal operation (user would need to press all
777 * buttons on the pad and start moving trackpoint
778 * without touching the pad surface) we assume former.
779 * Even if we are wrong the wost thing that would happen
780 * the cursor would jump but we should not get protocol
781 * de-synchronization.
784 alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
785 false);
788 * Continue with the standard ALPS protocol handling,
789 * but make sure we won't process it as an interleaved
790 * packet again, which may happen if all buttons are
791 * pressed. To avoid this let's reset the 4th bit which
792 * is normally 1.
794 psmouse->packet[3] = psmouse->packet[6] & 0xf7;
795 psmouse->pktcnt = 4;
798 return PSMOUSE_GOOD_DATA;
801 static void alps_flush_packet(unsigned long data)
803 struct psmouse *psmouse = (struct psmouse *)data;
804 struct alps_data *priv = psmouse->private;
806 serio_pause_rx(psmouse->ps2dev.serio);
808 if (psmouse->pktcnt == psmouse->pktsize) {
811 * We did not any more data in reasonable amount of time.
812 * Validate the last 3 bytes and process as a standard
813 * ALPS packet.
815 if ((psmouse->packet[3] |
816 psmouse->packet[4] |
817 psmouse->packet[5]) & 0x80) {
818 psmouse_dbg(psmouse,
819 "refusing packet %3ph (suspected interleaved ps/2)\n",
820 psmouse->packet + 3);
821 } else {
822 priv->process_packet(psmouse);
824 psmouse->pktcnt = 0;
827 serio_continue_rx(psmouse->ps2dev.serio);
830 static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
832 struct alps_data *priv = psmouse->private;
834 if ((psmouse->packet[0] & 0xc8) == 0x08) { /* PS/2 packet */
835 if (psmouse->pktcnt == 3) {
836 alps_report_bare_ps2_packet(psmouse, psmouse->packet,
837 true);
838 return PSMOUSE_FULL_PACKET;
840 return PSMOUSE_GOOD_DATA;
843 /* Check for PS/2 packet stuffed in the middle of ALPS packet. */
845 if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
846 psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
847 return alps_handle_interleaved_ps2(psmouse);
850 if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
851 psmouse_dbg(psmouse,
852 "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
853 psmouse->packet[0], priv->mask0, priv->byte0);
854 return PSMOUSE_BAD_DATA;
857 /* Bytes 2 - pktsize should have 0 in the highest bit */
858 if (psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
859 (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
860 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
861 psmouse->pktcnt - 1,
862 psmouse->packet[psmouse->pktcnt - 1]);
863 return PSMOUSE_BAD_DATA;
866 if (psmouse->pktcnt == psmouse->pktsize) {
867 priv->process_packet(psmouse);
868 return PSMOUSE_FULL_PACKET;
871 return PSMOUSE_GOOD_DATA;
874 static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
876 struct ps2dev *ps2dev = &psmouse->ps2dev;
877 struct alps_data *priv = psmouse->private;
878 int command;
879 unsigned char *param;
880 unsigned char dummy[4];
882 BUG_ON(nibble > 0xf);
884 command = priv->nibble_commands[nibble].command;
885 param = (command & 0x0f00) ?
886 dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
888 if (ps2_command(ps2dev, param, command))
889 return -1;
891 return 0;
894 static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
896 struct ps2dev *ps2dev = &psmouse->ps2dev;
897 struct alps_data *priv = psmouse->private;
898 int i, nibble;
900 if (ps2_command(ps2dev, NULL, priv->addr_command))
901 return -1;
903 for (i = 12; i >= 0; i -= 4) {
904 nibble = (addr >> i) & 0xf;
905 if (alps_command_mode_send_nibble(psmouse, nibble))
906 return -1;
909 return 0;
912 static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
914 struct ps2dev *ps2dev = &psmouse->ps2dev;
915 unsigned char param[4];
917 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
918 return -1;
921 * The address being read is returned in the first two bytes
922 * of the result. Check that this address matches the expected
923 * address.
925 if (addr != ((param[0] << 8) | param[1]))
926 return -1;
928 return param[2];
931 static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
933 if (alps_command_mode_set_addr(psmouse, addr))
934 return -1;
935 return __alps_command_mode_read_reg(psmouse, addr);
938 static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
940 if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
941 return -1;
942 if (alps_command_mode_send_nibble(psmouse, value & 0xf))
943 return -1;
944 return 0;
947 static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
948 u8 value)
950 if (alps_command_mode_set_addr(psmouse, addr))
951 return -1;
952 return __alps_command_mode_write_reg(psmouse, value);
955 static int alps_rpt_cmd(struct psmouse *psmouse, int init_command,
956 int repeated_command, unsigned char *param)
958 struct ps2dev *ps2dev = &psmouse->ps2dev;
960 param[0] = 0;
961 if (init_command && ps2_command(ps2dev, param, init_command))
962 return -EIO;
964 if (ps2_command(ps2dev, NULL, repeated_command) ||
965 ps2_command(ps2dev, NULL, repeated_command) ||
966 ps2_command(ps2dev, NULL, repeated_command))
967 return -EIO;
969 param[0] = param[1] = param[2] = 0xff;
970 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
971 return -EIO;
973 psmouse_dbg(psmouse, "%2.2X report: %2.2x %2.2x %2.2x\n",
974 repeated_command, param[0], param[1], param[2]);
975 return 0;
978 static int alps_enter_command_mode(struct psmouse *psmouse,
979 unsigned char *resp)
981 unsigned char param[4];
983 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) {
984 psmouse_err(psmouse, "failed to enter command mode\n");
985 return -1;
988 if (param[0] != 0x88 && param[1] != 0x07) {
989 psmouse_dbg(psmouse,
990 "unknown response while entering command mode\n");
991 return -1;
994 if (resp)
995 *resp = param[2];
996 return 0;
999 static inline int alps_exit_command_mode(struct psmouse *psmouse)
1001 struct ps2dev *ps2dev = &psmouse->ps2dev;
1002 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
1003 return -1;
1004 return 0;
1008 * For DualPoint devices select the device that should respond to
1009 * subsequent commands. It looks like glidepad is behind stickpointer,
1010 * I'd thought it would be other way around...
1012 static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
1014 struct ps2dev *ps2dev = &psmouse->ps2dev;
1015 int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
1017 if (ps2_command(ps2dev, NULL, cmd) ||
1018 ps2_command(ps2dev, NULL, cmd) ||
1019 ps2_command(ps2dev, NULL, cmd) ||
1020 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1021 return -1;
1023 /* we may get 3 more bytes, just ignore them */
1024 ps2_drain(ps2dev, 3, 100);
1026 return 0;
1029 static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
1031 struct ps2dev *ps2dev = &psmouse->ps2dev;
1033 /* Try ALPS magic knock - 4 disable before enable */
1034 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1035 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1036 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1037 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1038 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
1039 return -1;
1042 * Switch mouse to poll (remote) mode so motion data will not
1043 * get in our way
1045 return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
1048 static int alps_get_status(struct psmouse *psmouse, char *param)
1050 /* Get status: 0xF5 0xF5 0xF5 0xE9 */
1051 if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param))
1052 return -1;
1054 return 0;
1058 * Turn touchpad tapping on or off. The sequences are:
1059 * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
1060 * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
1061 * My guess that 0xE9 (GetInfo) is here as a sync point.
1062 * For models that also have stickpointer (DualPoints) its tapping
1063 * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
1064 * we don't fiddle with it.
1066 static int alps_tap_mode(struct psmouse *psmouse, int enable)
1068 struct ps2dev *ps2dev = &psmouse->ps2dev;
1069 int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
1070 unsigned char tap_arg = enable ? 0x0A : 0x00;
1071 unsigned char param[4];
1073 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
1074 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1075 ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1076 ps2_command(ps2dev, &tap_arg, cmd))
1077 return -1;
1079 if (alps_get_status(psmouse, param))
1080 return -1;
1082 return 0;
1086 * alps_poll() - poll the touchpad for current motion packet.
1087 * Used in resync.
1089 static int alps_poll(struct psmouse *psmouse)
1091 struct alps_data *priv = psmouse->private;
1092 unsigned char buf[sizeof(psmouse->packet)];
1093 bool poll_failed;
1095 if (priv->flags & ALPS_PASS)
1096 alps_passthrough_mode_v2(psmouse, true);
1098 poll_failed = ps2_command(&psmouse->ps2dev, buf,
1099 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
1101 if (priv->flags & ALPS_PASS)
1102 alps_passthrough_mode_v2(psmouse, false);
1104 if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
1105 return -1;
1107 if ((psmouse->badbyte & 0xc8) == 0x08) {
1109 * Poll the track stick ...
1111 if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
1112 return -1;
1115 memcpy(psmouse->packet, buf, sizeof(buf));
1116 return 0;
1119 static int alps_hw_init_v1_v2(struct psmouse *psmouse)
1121 struct alps_data *priv = psmouse->private;
1123 if ((priv->flags & ALPS_PASS) &&
1124 alps_passthrough_mode_v2(psmouse, true)) {
1125 return -1;
1128 if (alps_tap_mode(psmouse, true)) {
1129 psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
1130 return -1;
1133 if (alps_absolute_mode_v1_v2(psmouse)) {
1134 psmouse_err(psmouse, "Failed to enable absolute mode\n");
1135 return -1;
1138 if ((priv->flags & ALPS_PASS) &&
1139 alps_passthrough_mode_v2(psmouse, false)) {
1140 return -1;
1143 /* ALPS needs stream mode, otherwise it won't report any data */
1144 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
1145 psmouse_err(psmouse, "Failed to enable stream mode\n");
1146 return -1;
1149 return 0;
1153 * Enable or disable passthrough mode to the trackstick. Must be in
1154 * command mode when calling this function.
1156 static int alps_passthrough_mode_v3(struct psmouse *psmouse, bool enable)
1158 int reg_val;
1160 reg_val = alps_command_mode_read_reg(psmouse, 0x0008);
1161 if (reg_val == -1)
1162 return -1;
1164 if (enable)
1165 reg_val |= 0x01;
1166 else
1167 reg_val &= ~0x01;
1169 if (__alps_command_mode_write_reg(psmouse, reg_val))
1170 return -1;
1172 return 0;
1175 /* Must be in command mode when calling this function */
1176 static int alps_absolute_mode_v3(struct psmouse *psmouse)
1178 int reg_val;
1180 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
1181 if (reg_val == -1)
1182 return -1;
1184 reg_val |= 0x06;
1185 if (__alps_command_mode_write_reg(psmouse, reg_val))
1186 return -1;
1188 return 0;
1191 static int alps_hw_init_v3(struct psmouse *psmouse)
1193 struct ps2dev *ps2dev = &psmouse->ps2dev;
1194 int reg_val;
1195 unsigned char param[4];
1197 if (alps_enter_command_mode(psmouse, NULL))
1198 goto error;
1200 /* Check for trackstick */
1201 reg_val = alps_command_mode_read_reg(psmouse, 0x0008);
1202 if (reg_val == -1)
1203 goto error;
1204 if (reg_val & 0x80) {
1205 if (alps_passthrough_mode_v3(psmouse, true))
1206 goto error;
1207 if (alps_exit_command_mode(psmouse))
1208 goto error;
1211 * E7 report for the trackstick
1213 * There have been reports of failures to seem to trace back
1214 * to the above trackstick check failing. When these occur
1215 * this E7 report fails, so when that happens we continue
1216 * with the assumption that there isn't a trackstick after
1217 * all.
1219 param[0] = 0x64;
1220 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1221 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1222 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1223 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) {
1224 psmouse_warn(psmouse, "trackstick E7 report failed\n");
1225 } else {
1226 psmouse_dbg(psmouse,
1227 "trackstick E7 report: %2.2x %2.2x %2.2x\n",
1228 param[0], param[1], param[2]);
1231 * Not sure what this does, but it is absolutely
1232 * essential. Without it, the touchpad does not
1233 * work at all and the trackstick just emits normal
1234 * PS/2 packets.
1236 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1237 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1238 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1239 alps_command_mode_send_nibble(psmouse, 0x9) ||
1240 alps_command_mode_send_nibble(psmouse, 0x4)) {
1241 psmouse_err(psmouse,
1242 "Error sending magic E6 sequence\n");
1243 goto error_passthrough;
1247 if (alps_enter_command_mode(psmouse, NULL))
1248 goto error_passthrough;
1249 if (alps_passthrough_mode_v3(psmouse, false))
1250 goto error;
1253 if (alps_absolute_mode_v3(psmouse)) {
1254 psmouse_err(psmouse, "Failed to enter absolute mode\n");
1255 goto error;
1258 reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
1259 if (reg_val == -1)
1260 goto error;
1261 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
1262 goto error;
1264 reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
1265 if (reg_val == -1)
1266 goto error;
1267 if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
1268 goto error;
1270 if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
1271 goto error;
1272 if (__alps_command_mode_write_reg(psmouse, 0x04))
1273 goto error;
1275 if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
1276 goto error;
1277 if (__alps_command_mode_write_reg(psmouse, 0x03))
1278 goto error;
1280 if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
1281 goto error;
1282 if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
1283 goto error;
1285 if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
1286 goto error;
1287 if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
1288 goto error;
1291 * This ensures the trackstick packets are in the format
1292 * supported by this driver. If bit 1 isn't set the packet
1293 * format is different.
1295 if (alps_command_mode_write_reg(psmouse, 0x0008, 0x82))
1296 goto error;
1298 alps_exit_command_mode(psmouse);
1300 /* Set rate and enable data reporting */
1301 param[0] = 0x64;
1302 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
1303 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1304 psmouse_err(psmouse, "Failed to enable data reporting\n");
1305 return -1;
1308 return 0;
1310 error_passthrough:
1311 /* Something failed while in passthrough mode, so try to get out */
1312 if (!alps_enter_command_mode(psmouse, NULL))
1313 alps_passthrough_mode_v3(psmouse, false);
1314 error:
1316 * Leaving the touchpad in command mode will essentially render
1317 * it unusable until the machine reboots, so exit it here just
1318 * to be safe
1320 alps_exit_command_mode(psmouse);
1321 return -1;
1324 /* Must be in command mode when calling this function */
1325 static int alps_absolute_mode_v4(struct psmouse *psmouse)
1327 int reg_val;
1329 reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
1330 if (reg_val == -1)
1331 return -1;
1333 reg_val |= 0x02;
1334 if (__alps_command_mode_write_reg(psmouse, reg_val))
1335 return -1;
1337 return 0;
1340 static int alps_hw_init_v4(struct psmouse *psmouse)
1342 struct ps2dev *ps2dev = &psmouse->ps2dev;
1343 unsigned char param[4];
1345 if (alps_enter_command_mode(psmouse, NULL))
1346 goto error;
1348 if (alps_absolute_mode_v4(psmouse)) {
1349 psmouse_err(psmouse, "Failed to enter absolute mode\n");
1350 goto error;
1353 if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
1354 goto error;
1356 if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
1357 goto error;
1359 if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
1360 goto error;
1362 if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
1363 goto error;
1365 if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
1366 goto error;
1368 if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
1369 goto error;
1371 if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
1372 goto error;
1374 if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
1375 goto error;
1377 alps_exit_command_mode(psmouse);
1380 * This sequence changes the output from a 9-byte to an
1381 * 8-byte format. All the same data seems to be present,
1382 * just in a more compact format.
1384 param[0] = 0xc8;
1385 param[1] = 0x64;
1386 param[2] = 0x50;
1387 if (ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
1388 ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE) ||
1389 ps2_command(ps2dev, &param[2], PSMOUSE_CMD_SETRATE) ||
1390 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
1391 return -1;
1393 /* Set rate and enable data reporting */
1394 param[0] = 0x64;
1395 if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
1396 ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1397 psmouse_err(psmouse, "Failed to enable data reporting\n");
1398 return -1;
1401 return 0;
1403 error:
1405 * Leaving the touchpad in command mode will essentially render
1406 * it unusable until the machine reboots, so exit it here just
1407 * to be safe
1409 alps_exit_command_mode(psmouse);
1410 return -1;
1413 static void alps_set_defaults(struct alps_data *priv)
1415 switch (priv->proto_version) {
1416 case ALPS_PROTO_V1:
1417 case ALPS_PROTO_V2:
1418 priv->hw_init = alps_hw_init_v1_v2;
1419 priv->process_packet = alps_process_packet_v1_v2;
1420 priv->set_abs_params = alps_set_abs_params_st;
1421 break;
1422 case ALPS_PROTO_V3:
1423 priv->hw_init = alps_hw_init_v3;
1424 priv->process_packet = alps_process_packet_v3;
1425 priv->set_abs_params = alps_set_abs_params_mt;
1426 priv->nibble_commands = alps_v3_nibble_commands;
1427 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
1428 break;
1429 case ALPS_PROTO_V4:
1430 priv->hw_init = alps_hw_init_v4;
1431 priv->process_packet = alps_process_packet_v4;
1432 priv->set_abs_params = alps_set_abs_params_mt;
1433 priv->nibble_commands = alps_v4_nibble_commands;
1434 priv->addr_command = PSMOUSE_CMD_DISABLE;
1435 break;
1439 static int alps_match_table(struct psmouse *psmouse, struct alps_data *priv,
1440 unsigned char *e7, unsigned char *ec)
1442 const struct alps_model_info *model;
1443 int i;
1445 for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
1446 model = &alps_model_data[i];
1448 if (!memcmp(e7, model->signature, sizeof(model->signature)) &&
1449 (!model->command_mode_resp ||
1450 model->command_mode_resp == ec[2])) {
1452 priv->proto_version = model->proto_version;
1453 alps_set_defaults(priv);
1455 priv->flags = model->flags;
1456 priv->byte0 = model->byte0;
1457 priv->mask0 = model->mask0;
1459 return 0;
1463 return -EINVAL;
1466 static int alps_identify(struct psmouse *psmouse, struct alps_data *priv)
1468 unsigned char e6[4], e7[4], ec[4];
1471 * First try "E6 report".
1472 * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
1473 * The bits 0-2 of the first byte will be 1s if some buttons are
1474 * pressed.
1476 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
1477 PSMOUSE_CMD_SETSCALE11, e6))
1478 return -EIO;
1480 if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100))
1481 return -EINVAL;
1484 * Now get the "E7" and "EC" reports. These will uniquely identify
1485 * most ALPS touchpads.
1487 if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
1488 PSMOUSE_CMD_SETSCALE21, e7) ||
1489 alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
1490 PSMOUSE_CMD_RESET_WRAP, ec) ||
1491 alps_exit_command_mode(psmouse))
1492 return -EIO;
1494 if (alps_match_table(psmouse, priv, e7, ec) == 0)
1495 return 0;
1497 psmouse_info(psmouse,
1498 "Unknown ALPS touchpad: E7=%2.2x %2.2x %2.2x, EC=%2.2x %2.2x %2.2x\n",
1499 e7[0], e7[1], e7[2], ec[0], ec[1], ec[2]);
1501 return -EINVAL;
1504 static int alps_reconnect(struct psmouse *psmouse)
1506 struct alps_data *priv = psmouse->private;
1508 psmouse_reset(psmouse);
1510 if (alps_identify(psmouse, priv) < 0)
1511 return -1;
1513 return priv->hw_init(psmouse);
1516 static void alps_disconnect(struct psmouse *psmouse)
1518 struct alps_data *priv = psmouse->private;
1520 psmouse_reset(psmouse);
1521 del_timer_sync(&priv->timer);
1522 input_unregister_device(priv->dev2);
1523 kfree(priv);
1526 static void alps_set_abs_params_st(struct alps_data *priv,
1527 struct input_dev *dev1)
1529 input_set_abs_params(dev1, ABS_X, 0, 1023, 0, 0);
1530 input_set_abs_params(dev1, ABS_Y, 0, 767, 0, 0);
1533 static void alps_set_abs_params_mt(struct alps_data *priv,
1534 struct input_dev *dev1)
1536 set_bit(INPUT_PROP_SEMI_MT, dev1->propbit);
1537 input_mt_init_slots(dev1, 2, 0);
1538 input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, ALPS_V3_X_MAX, 0, 0);
1539 input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, ALPS_V3_Y_MAX, 0, 0);
1541 set_bit(BTN_TOOL_DOUBLETAP, dev1->keybit);
1542 set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
1543 set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
1545 input_set_abs_params(dev1, ABS_X, 0, ALPS_V3_X_MAX, 0, 0);
1546 input_set_abs_params(dev1, ABS_Y, 0, ALPS_V3_Y_MAX, 0, 0);
1549 int alps_init(struct psmouse *psmouse)
1551 struct alps_data *priv;
1552 struct input_dev *dev1 = psmouse->dev, *dev2;
1554 priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
1555 dev2 = input_allocate_device();
1556 if (!priv || !dev2)
1557 goto init_fail;
1559 priv->dev2 = dev2;
1560 setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
1562 psmouse->private = priv;
1564 psmouse_reset(psmouse);
1566 if (alps_identify(psmouse, priv) < 0)
1567 goto init_fail;
1569 if (priv->hw_init(psmouse))
1570 goto init_fail;
1573 * Undo part of setup done for us by psmouse core since touchpad
1574 * is not a relative device.
1576 __clear_bit(EV_REL, dev1->evbit);
1577 __clear_bit(REL_X, dev1->relbit);
1578 __clear_bit(REL_Y, dev1->relbit);
1581 * Now set up our capabilities.
1583 dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
1584 dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
1585 dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
1586 dev1->keybit[BIT_WORD(BTN_LEFT)] |=
1587 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
1589 dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
1591 priv->set_abs_params(priv, dev1);
1592 input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
1594 if (priv->flags & ALPS_WHEEL) {
1595 dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
1596 dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
1599 if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
1600 dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
1601 dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
1604 if (priv->flags & ALPS_FOUR_BUTTONS) {
1605 dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
1606 dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
1607 dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
1608 dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
1609 } else {
1610 dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
1613 snprintf(priv->phys, sizeof(priv->phys), "%s/input1", psmouse->ps2dev.serio->phys);
1614 dev2->phys = priv->phys;
1615 dev2->name = (priv->flags & ALPS_DUALPOINT) ?
1616 "DualPoint Stick" : "PS/2 Mouse";
1617 dev2->id.bustype = BUS_I8042;
1618 dev2->id.vendor = 0x0002;
1619 dev2->id.product = PSMOUSE_ALPS;
1620 dev2->id.version = 0x0000;
1621 dev2->dev.parent = &psmouse->ps2dev.serio->dev;
1623 dev2->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
1624 dev2->relbit[BIT_WORD(REL_X)] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
1625 dev2->keybit[BIT_WORD(BTN_LEFT)] =
1626 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_MIDDLE) | BIT_MASK(BTN_RIGHT);
1628 if (input_register_device(priv->dev2))
1629 goto init_fail;
1631 psmouse->protocol_handler = alps_process_byte;
1632 psmouse->poll = alps_poll;
1633 psmouse->disconnect = alps_disconnect;
1634 psmouse->reconnect = alps_reconnect;
1635 psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
1637 /* We are having trouble resyncing ALPS touchpads so disable it for now */
1638 psmouse->resync_time = 0;
1640 return 0;
1642 init_fail:
1643 psmouse_reset(psmouse);
1644 input_free_device(dev2);
1645 kfree(priv);
1646 psmouse->private = NULL;
1647 return -1;
1650 int alps_detect(struct psmouse *psmouse, bool set_properties)
1652 struct alps_data dummy;
1654 if (alps_identify(psmouse, &dummy) < 0)
1655 return -1;
1657 if (set_properties) {
1658 psmouse->vendor = "ALPS";
1659 psmouse->name = dummy.flags & ALPS_DUALPOINT ?
1660 "DualPoint TouchPad" : "GlidePoint";
1661 psmouse->model = dummy.proto_version << 8;
1663 return 0;