]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/input/mouse/alps.c
HID: hidraw: don't deallocate memory when it is in use
[karo-tx-linux.git] / drivers / input / mouse / alps.c
1 /*
2  * ALPS touchpad PS/2 mouse driver
3  *
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>
9  *
10  * ALPS detection, tap switching and status querying info is taken from
11  * tpconfig utility (by C. Scott Ananian and Bruce Kall).
12  *
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.
16  */
17
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
24 #include "psmouse.h"
25 #include "alps.h"
26
27 /*
28  * Definitions for ALPS version 3 and 4 command mode protocol
29  */
30 #define ALPS_V3_X_MAX   2000
31 #define ALPS_V3_Y_MAX   1400
32
33 #define ALPS_BITMAP_X_BITS      15
34 #define ALPS_BITMAP_Y_BITS      11
35
36 #define ALPS_CMD_NIBBLE_10      0x01f2
37
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 */
55 };
56
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 */
74 };
75
76
77 #define ALPS_DUALPOINT          0x02    /* touchpad has trackstick */
78 #define ALPS_PASS               0x04    /* device has a pass-through port */
79
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 */
86
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 },
115 };
116
117 /*
118  * XXX - this entry is suspicious. First byte has zero lower nibble,
119  * which is what a normal mouse would report. Also, the value 0x0e
120  * isn't valid per PS/2 spec.
121  */
122
123 /* Packet formats are described in Documentation/input/alps.txt */
124
125 static bool alps_is_valid_first_byte(const struct alps_model_info *model,
126                                      unsigned char data)
127 {
128         return (data & model->mask0) == model->byte0;
129 }
130
131 static void alps_report_buttons(struct psmouse *psmouse,
132                                 struct input_dev *dev1, struct input_dev *dev2,
133                                 int left, int right, int middle)
134 {
135         struct input_dev *dev;
136
137         /*
138          * If shared button has already been reported on the
139          * other device (dev2) then this event should be also
140          * sent through that device.
141          */
142         dev = test_bit(BTN_LEFT, dev2->key) ? dev2 : dev1;
143         input_report_key(dev, BTN_LEFT, left);
144
145         dev = test_bit(BTN_RIGHT, dev2->key) ? dev2 : dev1;
146         input_report_key(dev, BTN_RIGHT, right);
147
148         dev = test_bit(BTN_MIDDLE, dev2->key) ? dev2 : dev1;
149         input_report_key(dev, BTN_MIDDLE, middle);
150
151         /*
152          * Sync the _other_ device now, we'll do the first
153          * device later once we report the rest of the events.
154          */
155         input_sync(dev2);
156 }
157
158 static void alps_process_packet_v1_v2(struct psmouse *psmouse)
159 {
160         struct alps_data *priv = psmouse->private;
161         const struct alps_model_info *model = priv->i;
162         unsigned char *packet = psmouse->packet;
163         struct input_dev *dev = psmouse->dev;
164         struct input_dev *dev2 = priv->dev2;
165         int x, y, z, ges, fin, left, right, middle;
166         int back = 0, forward = 0;
167
168         if (model->proto_version == ALPS_PROTO_V1) {
169                 left = packet[2] & 0x10;
170                 right = packet[2] & 0x08;
171                 middle = 0;
172                 x = packet[1] | ((packet[0] & 0x07) << 7);
173                 y = packet[4] | ((packet[3] & 0x07) << 7);
174                 z = packet[5];
175         } else {
176                 left = packet[3] & 1;
177                 right = packet[3] & 2;
178                 middle = packet[3] & 4;
179                 x = packet[1] | ((packet[2] & 0x78) << (7 - 3));
180                 y = packet[4] | ((packet[3] & 0x70) << (7 - 4));
181                 z = packet[5];
182         }
183
184         if (model->flags & ALPS_FW_BK_1) {
185                 back = packet[0] & 0x10;
186                 forward = packet[2] & 4;
187         }
188
189         if (model->flags & ALPS_FW_BK_2) {
190                 back = packet[3] & 4;
191                 forward = packet[2] & 4;
192                 if ((middle = forward && back))
193                         forward = back = 0;
194         }
195
196         ges = packet[2] & 1;
197         fin = packet[2] & 2;
198
199         if ((model->flags & ALPS_DUALPOINT) && z == 127) {
200                 input_report_rel(dev2, REL_X,  (x > 383 ? (x - 768) : x));
201                 input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y));
202
203                 alps_report_buttons(psmouse, dev2, dev, left, right, middle);
204
205                 input_sync(dev2);
206                 return;
207         }
208
209         alps_report_buttons(psmouse, dev, dev2, left, right, middle);
210
211         /* Convert hardware tap to a reasonable Z value */
212         if (ges && !fin)
213                 z = 40;
214
215         /*
216          * A "tap and drag" operation is reported by the hardware as a transition
217          * from (!fin && ges) to (fin && ges). This should be translated to the
218          * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
219          */
220         if (ges && fin && !priv->prev_fin) {
221                 input_report_abs(dev, ABS_X, x);
222                 input_report_abs(dev, ABS_Y, y);
223                 input_report_abs(dev, ABS_PRESSURE, 0);
224                 input_report_key(dev, BTN_TOOL_FINGER, 0);
225                 input_sync(dev);
226         }
227         priv->prev_fin = fin;
228
229         if (z > 30)
230                 input_report_key(dev, BTN_TOUCH, 1);
231         if (z < 25)
232                 input_report_key(dev, BTN_TOUCH, 0);
233
234         if (z > 0) {
235                 input_report_abs(dev, ABS_X, x);
236                 input_report_abs(dev, ABS_Y, y);
237         }
238
239         input_report_abs(dev, ABS_PRESSURE, z);
240         input_report_key(dev, BTN_TOOL_FINGER, z > 0);
241
242         if (model->flags & ALPS_WHEEL)
243                 input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
244
245         if (model->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
246                 input_report_key(dev, BTN_FORWARD, forward);
247                 input_report_key(dev, BTN_BACK, back);
248         }
249
250         if (model->flags & ALPS_FOUR_BUTTONS) {
251                 input_report_key(dev, BTN_0, packet[2] & 4);
252                 input_report_key(dev, BTN_1, packet[0] & 0x10);
253                 input_report_key(dev, BTN_2, packet[3] & 4);
254                 input_report_key(dev, BTN_3, packet[0] & 0x20);
255         }
256
257         input_sync(dev);
258 }
259
260 /*
261  * Process bitmap data from v3 and v4 protocols. Returns the number of
262  * fingers detected. A return value of 0 means at least one of the
263  * bitmaps was empty.
264  *
265  * The bitmaps don't have enough data to track fingers, so this function
266  * only generates points representing a bounding box of all contacts.
267  * These points are returned in x1, y1, x2, and y2 when the return value
268  * is greater than 0.
269  */
270 static int alps_process_bitmap(unsigned int x_map, unsigned int y_map,
271                                int *x1, int *y1, int *x2, int *y2)
272 {
273         struct alps_bitmap_point {
274                 int start_bit;
275                 int num_bits;
276         };
277
278         int fingers_x = 0, fingers_y = 0, fingers;
279         int i, bit, prev_bit;
280         struct alps_bitmap_point x_low = {0,}, x_high = {0,};
281         struct alps_bitmap_point y_low = {0,}, y_high = {0,};
282         struct alps_bitmap_point *point;
283
284         if (!x_map || !y_map)
285                 return 0;
286
287         *x1 = *y1 = *x2 = *y2 = 0;
288
289         prev_bit = 0;
290         point = &x_low;
291         for (i = 0; x_map != 0; i++, x_map >>= 1) {
292                 bit = x_map & 1;
293                 if (bit) {
294                         if (!prev_bit) {
295                                 point->start_bit = i;
296                                 fingers_x++;
297                         }
298                         point->num_bits++;
299                 } else {
300                         if (prev_bit)
301                                 point = &x_high;
302                         else
303                                 point->num_bits = 0;
304                 }
305                 prev_bit = bit;
306         }
307
308         /*
309          * y bitmap is reversed for what we need (lower positions are in
310          * higher bits), so we process from the top end.
311          */
312         y_map = y_map << (sizeof(y_map) * BITS_PER_BYTE - ALPS_BITMAP_Y_BITS);
313         prev_bit = 0;
314         point = &y_low;
315         for (i = 0; y_map != 0; i++, y_map <<= 1) {
316                 bit = y_map & (1 << (sizeof(y_map) * BITS_PER_BYTE - 1));
317                 if (bit) {
318                         if (!prev_bit) {
319                                 point->start_bit = i;
320                                 fingers_y++;
321                         }
322                         point->num_bits++;
323                 } else {
324                         if (prev_bit)
325                                 point = &y_high;
326                         else
327                                 point->num_bits = 0;
328                 }
329                 prev_bit = bit;
330         }
331
332         /*
333          * Fingers can overlap, so we use the maximum count of fingers
334          * on either axis as the finger count.
335          */
336         fingers = max(fingers_x, fingers_y);
337
338         /*
339          * If total fingers is > 1 but either axis reports only a single
340          * contact, we have overlapping or adjacent fingers. For the
341          * purposes of creating a bounding box, divide the single contact
342          * (roughly) equally between the two points.
343          */
344         if (fingers > 1) {
345                 if (fingers_x == 1) {
346                         i = x_low.num_bits / 2;
347                         x_low.num_bits = x_low.num_bits - i;
348                         x_high.start_bit = x_low.start_bit + i;
349                         x_high.num_bits = max(i, 1);
350                 } else if (fingers_y == 1) {
351                         i = y_low.num_bits / 2;
352                         y_low.num_bits = y_low.num_bits - i;
353                         y_high.start_bit = y_low.start_bit + i;
354                         y_high.num_bits = max(i, 1);
355                 }
356         }
357
358         *x1 = (ALPS_V3_X_MAX * (2 * x_low.start_bit + x_low.num_bits - 1)) /
359               (2 * (ALPS_BITMAP_X_BITS - 1));
360         *y1 = (ALPS_V3_Y_MAX * (2 * y_low.start_bit + y_low.num_bits - 1)) /
361               (2 * (ALPS_BITMAP_Y_BITS - 1));
362
363         if (fingers > 1) {
364                 *x2 = (ALPS_V3_X_MAX * (2 * x_high.start_bit + x_high.num_bits - 1)) /
365                       (2 * (ALPS_BITMAP_X_BITS - 1));
366                 *y2 = (ALPS_V3_Y_MAX * (2 * y_high.start_bit + y_high.num_bits - 1)) /
367                       (2 * (ALPS_BITMAP_Y_BITS - 1));
368         }
369
370         return fingers;
371 }
372
373 static void alps_set_slot(struct input_dev *dev, int slot, bool active,
374                           int x, int y)
375 {
376         input_mt_slot(dev, slot);
377         input_mt_report_slot_state(dev, MT_TOOL_FINGER, active);
378         if (active) {
379                 input_report_abs(dev, ABS_MT_POSITION_X, x);
380                 input_report_abs(dev, ABS_MT_POSITION_Y, y);
381         }
382 }
383
384 static void alps_report_semi_mt_data(struct input_dev *dev, int num_fingers,
385                                      int x1, int y1, int x2, int y2)
386 {
387         alps_set_slot(dev, 0, num_fingers != 0, x1, y1);
388         alps_set_slot(dev, 1, num_fingers == 2, x2, y2);
389 }
390
391 static void alps_process_trackstick_packet_v3(struct psmouse *psmouse)
392 {
393         struct alps_data *priv = psmouse->private;
394         unsigned char *packet = psmouse->packet;
395         struct input_dev *dev = priv->dev2;
396         int x, y, z, left, right, middle;
397
398         /* Sanity check packet */
399         if (!(packet[0] & 0x40)) {
400                 psmouse_dbg(psmouse, "Bad trackstick packet, discarding\n");
401                 return;
402         }
403
404         /*
405          * There's a special packet that seems to indicate the end
406          * of a stream of trackstick data. Filter these out.
407          */
408         if (packet[1] == 0x7f && packet[2] == 0x7f && packet[4] == 0x7f)
409                 return;
410
411         x = (s8)(((packet[0] & 0x20) << 2) | (packet[1] & 0x7f));
412         y = (s8)(((packet[0] & 0x10) << 3) | (packet[2] & 0x7f));
413         z = (packet[4] & 0x7c) >> 2;
414
415         /*
416          * The x and y values tend to be quite large, and when used
417          * alone the trackstick is difficult to use. Scale them down
418          * to compensate.
419          */
420         x /= 8;
421         y /= 8;
422
423         input_report_rel(dev, REL_X, x);
424         input_report_rel(dev, REL_Y, -y);
425
426         /*
427          * Most ALPS models report the trackstick buttons in the touchpad
428          * packets, but a few report them here. No reliable way has been
429          * found to differentiate between the models upfront, so we enable
430          * the quirk in response to seeing a button press in the trackstick
431          * packet.
432          */
433         left = packet[3] & 0x01;
434         right = packet[3] & 0x02;
435         middle = packet[3] & 0x04;
436
437         if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) &&
438             (left || right || middle))
439                 priv->quirks |= ALPS_QUIRK_TRACKSTICK_BUTTONS;
440
441         if (priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) {
442                 input_report_key(dev, BTN_LEFT, left);
443                 input_report_key(dev, BTN_RIGHT, right);
444                 input_report_key(dev, BTN_MIDDLE, middle);
445         }
446
447         input_sync(dev);
448         return;
449 }
450
451 static void alps_process_touchpad_packet_v3(struct psmouse *psmouse)
452 {
453         struct alps_data *priv = psmouse->private;
454         unsigned char *packet = psmouse->packet;
455         struct input_dev *dev = psmouse->dev;
456         struct input_dev *dev2 = priv->dev2;
457         int x, y, z;
458         int left, right, middle;
459         int x1 = 0, y1 = 0, x2 = 0, y2 = 0;
460         int fingers = 0, bmap_fingers;
461         unsigned int x_bitmap, y_bitmap;
462
463         /*
464          * There's no single feature of touchpad position and bitmap packets
465          * that can be used to distinguish between them. We rely on the fact
466          * that a bitmap packet should always follow a position packet with
467          * bit 6 of packet[4] set.
468          */
469         if (priv->multi_packet) {
470                 /*
471                  * Sometimes a position packet will indicate a multi-packet
472                  * sequence, but then what follows is another position
473                  * packet. Check for this, and when it happens process the
474                  * position packet as usual.
475                  */
476                 if (packet[0] & 0x40) {
477                         fingers = (packet[5] & 0x3) + 1;
478                         x_bitmap = ((packet[4] & 0x7e) << 8) |
479                                    ((packet[1] & 0x7f) << 2) |
480                                    ((packet[0] & 0x30) >> 4);
481                         y_bitmap = ((packet[3] & 0x70) << 4) |
482                                    ((packet[2] & 0x7f) << 1) |
483                                    (packet[4] & 0x01);
484
485                         bmap_fingers = alps_process_bitmap(x_bitmap, y_bitmap,
486                                                            &x1, &y1, &x2, &y2);
487
488                         /*
489                          * We shouldn't report more than one finger if
490                          * we don't have two coordinates.
491                          */
492                         if (fingers > 1 && bmap_fingers < 2)
493                                 fingers = bmap_fingers;
494
495                         /* Now process position packet */
496                         packet = priv->multi_data;
497                 } else {
498                         priv->multi_packet = 0;
499                 }
500         }
501
502         /*
503          * Bit 6 of byte 0 is not usually set in position packets. The only
504          * times it seems to be set is in situations where the data is
505          * suspect anyway, e.g. a palm resting flat on the touchpad. Given
506          * this combined with the fact that this bit is useful for filtering
507          * out misidentified bitmap packets, we reject anything with this
508          * bit set.
509          */
510         if (packet[0] & 0x40)
511                 return;
512
513         if (!priv->multi_packet && (packet[4] & 0x40)) {
514                 priv->multi_packet = 1;
515                 memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
516                 return;
517         }
518
519         priv->multi_packet = 0;
520
521         left = packet[3] & 0x01;
522         right = packet[3] & 0x02;
523         middle = packet[3] & 0x04;
524
525         x = ((packet[1] & 0x7f) << 4) | ((packet[4] & 0x30) >> 2) |
526             ((packet[0] & 0x30) >> 4);
527         y = ((packet[2] & 0x7f) << 4) | (packet[4] & 0x0f);
528         z = packet[5] & 0x7f;
529
530         /*
531          * Sometimes the hardware sends a single packet with z = 0
532          * in the middle of a stream. Real releases generate packets
533          * with x, y, and z all zero, so these seem to be flukes.
534          * Ignore them.
535          */
536         if (x && y && !z)
537                 return;
538
539         /*
540          * If we don't have MT data or the bitmaps were empty, we have
541          * to rely on ST data.
542          */
543         if (!fingers) {
544                 x1 = x;
545                 y1 = y;
546                 fingers = z > 0 ? 1 : 0;
547         }
548
549         if (z >= 64)
550                 input_report_key(dev, BTN_TOUCH, 1);
551         else
552                 input_report_key(dev, BTN_TOUCH, 0);
553
554         alps_report_semi_mt_data(dev, fingers, x1, y1, x2, y2);
555
556         input_mt_report_finger_count(dev, fingers);
557
558         input_report_key(dev, BTN_LEFT, left);
559         input_report_key(dev, BTN_RIGHT, right);
560         input_report_key(dev, BTN_MIDDLE, middle);
561
562         if (z > 0) {
563                 input_report_abs(dev, ABS_X, x);
564                 input_report_abs(dev, ABS_Y, y);
565         }
566         input_report_abs(dev, ABS_PRESSURE, z);
567
568         input_sync(dev);
569
570         if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) {
571                 left = packet[3] & 0x10;
572                 right = packet[3] & 0x20;
573                 middle = packet[3] & 0x40;
574
575                 input_report_key(dev2, BTN_LEFT, left);
576                 input_report_key(dev2, BTN_RIGHT, right);
577                 input_report_key(dev2, BTN_MIDDLE, middle);
578                 input_sync(dev2);
579         }
580 }
581
582 static void alps_process_packet_v3(struct psmouse *psmouse)
583 {
584         unsigned char *packet = psmouse->packet;
585
586         /*
587          * v3 protocol packets come in three types, two representing
588          * touchpad data and one representing trackstick data.
589          * Trackstick packets seem to be distinguished by always
590          * having 0x3f in the last byte. This value has never been
591          * observed in the last byte of either of the other types
592          * of packets.
593          */
594         if (packet[5] == 0x3f) {
595                 alps_process_trackstick_packet_v3(psmouse);
596                 return;
597         }
598
599         alps_process_touchpad_packet_v3(psmouse);
600 }
601
602 static void alps_process_packet_v4(struct psmouse *psmouse)
603 {
604         struct alps_data *priv = psmouse->private;
605         unsigned char *packet = psmouse->packet;
606         struct input_dev *dev = psmouse->dev;
607         int offset;
608         int x, y, z;
609         int left, right;
610         int x1, y1, x2, y2;
611         int fingers = 0;
612         unsigned int x_bitmap, y_bitmap;
613
614         /*
615          * v4 has a 6-byte encoding for bitmap data, but this data is
616          * broken up between 3 normal packets. Use priv->multi_packet to
617          * track our position in the bitmap packet.
618          */
619         if (packet[6] & 0x40) {
620                 /* sync, reset position */
621                 priv->multi_packet = 0;
622         }
623
624         if (WARN_ON_ONCE(priv->multi_packet > 2))
625                 return;
626
627         offset = 2 * priv->multi_packet;
628         priv->multi_data[offset] = packet[6];
629         priv->multi_data[offset + 1] = packet[7];
630
631         if (++priv->multi_packet > 2) {
632                 priv->multi_packet = 0;
633
634                 x_bitmap = ((priv->multi_data[2] & 0x1f) << 10) |
635                            ((priv->multi_data[3] & 0x60) << 3) |
636                            ((priv->multi_data[0] & 0x3f) << 2) |
637                            ((priv->multi_data[1] & 0x60) >> 5);
638                 y_bitmap = ((priv->multi_data[5] & 0x01) << 10) |
639                            ((priv->multi_data[3] & 0x1f) << 5) |
640                             (priv->multi_data[1] & 0x1f);
641
642                 fingers = alps_process_bitmap(x_bitmap, y_bitmap,
643                                               &x1, &y1, &x2, &y2);
644
645                 /* Store MT data.*/
646                 priv->fingers = fingers;
647                 priv->x1 = x1;
648                 priv->x2 = x2;
649                 priv->y1 = y1;
650                 priv->y2 = y2;
651         }
652
653         left = packet[4] & 0x01;
654         right = packet[4] & 0x02;
655
656         x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) |
657             ((packet[0] & 0x30) >> 4);
658         y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f);
659         z = packet[5] & 0x7f;
660
661         /*
662          * If there were no contacts in the bitmap, use ST
663          * points in MT reports.
664          * If there were two contacts or more, report MT data.
665          */
666         if (priv->fingers < 2) {
667                 x1 = x;
668                 y1 = y;
669                 fingers = z > 0 ? 1 : 0;
670         } else {
671                 fingers = priv->fingers;
672                 x1 = priv->x1;
673                 x2 = priv->x2;
674                 y1 = priv->y1;
675                 y2 = priv->y2;
676         }
677
678         if (z >= 64)
679                 input_report_key(dev, BTN_TOUCH, 1);
680         else
681                 input_report_key(dev, BTN_TOUCH, 0);
682
683         alps_report_semi_mt_data(dev, fingers, x1, y1, x2, y2);
684
685         input_mt_report_finger_count(dev, fingers);
686
687         input_report_key(dev, BTN_LEFT, left);
688         input_report_key(dev, BTN_RIGHT, right);
689
690         if (z > 0) {
691                 input_report_abs(dev, ABS_X, x);
692                 input_report_abs(dev, ABS_Y, y);
693         }
694         input_report_abs(dev, ABS_PRESSURE, z);
695
696         input_sync(dev);
697 }
698
699 static void alps_process_packet(struct psmouse *psmouse)
700 {
701         struct alps_data *priv = psmouse->private;
702         const struct alps_model_info *model = priv->i;
703
704         switch (model->proto_version) {
705         case ALPS_PROTO_V1:
706         case ALPS_PROTO_V2:
707                 alps_process_packet_v1_v2(psmouse);
708                 break;
709         case ALPS_PROTO_V3:
710                 alps_process_packet_v3(psmouse);
711                 break;
712         case ALPS_PROTO_V4:
713                 alps_process_packet_v4(psmouse);
714                 break;
715         }
716 }
717
718 static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
719                                         unsigned char packet[],
720                                         bool report_buttons)
721 {
722         struct alps_data *priv = psmouse->private;
723         struct input_dev *dev2 = priv->dev2;
724
725         if (report_buttons)
726                 alps_report_buttons(psmouse, dev2, psmouse->dev,
727                                 packet[0] & 1, packet[0] & 2, packet[0] & 4);
728
729         input_report_rel(dev2, REL_X,
730                 packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
731         input_report_rel(dev2, REL_Y,
732                 packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
733
734         input_sync(dev2);
735 }
736
737 static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
738 {
739         struct alps_data *priv = psmouse->private;
740
741         if (psmouse->pktcnt < 6)
742                 return PSMOUSE_GOOD_DATA;
743
744         if (psmouse->pktcnt == 6) {
745                 /*
746                  * Start a timer to flush the packet if it ends up last
747                  * 6-byte packet in the stream. Timer needs to fire
748                  * psmouse core times out itself. 20 ms should be enough
749                  * to decide if we are getting more data or not.
750                  */
751                 mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
752                 return PSMOUSE_GOOD_DATA;
753         }
754
755         del_timer(&priv->timer);
756
757         if (psmouse->packet[6] & 0x80) {
758
759                 /*
760                  * Highest bit is set - that means we either had
761                  * complete ALPS packet and this is start of the
762                  * next packet or we got garbage.
763                  */
764
765                 if (((psmouse->packet[3] |
766                       psmouse->packet[4] |
767                       psmouse->packet[5]) & 0x80) ||
768                     (!alps_is_valid_first_byte(priv->i, psmouse->packet[6]))) {
769                         psmouse_dbg(psmouse,
770                                     "refusing packet %x %x %x %x (suspected interleaved ps/2)\n",
771                                     psmouse->packet[3], psmouse->packet[4],
772                                     psmouse->packet[5], psmouse->packet[6]);
773                         return PSMOUSE_BAD_DATA;
774                 }
775
776                 alps_process_packet(psmouse);
777
778                 /* Continue with the next packet */
779                 psmouse->packet[0] = psmouse->packet[6];
780                 psmouse->pktcnt = 1;
781
782         } else {
783
784                 /*
785                  * High bit is 0 - that means that we indeed got a PS/2
786                  * packet in the middle of ALPS packet.
787                  *
788                  * There is also possibility that we got 6-byte ALPS
789                  * packet followed  by 3-byte packet from trackpoint. We
790                  * can not distinguish between these 2 scenarios but
791                  * because the latter is unlikely to happen in course of
792                  * normal operation (user would need to press all
793                  * buttons on the pad and start moving trackpoint
794                  * without touching the pad surface) we assume former.
795                  * Even if we are wrong the wost thing that would happen
796                  * the cursor would jump but we should not get protocol
797                  * de-synchronization.
798                  */
799
800                 alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
801                                             false);
802
803                 /*
804                  * Continue with the standard ALPS protocol handling,
805                  * but make sure we won't process it as an interleaved
806                  * packet again, which may happen if all buttons are
807                  * pressed. To avoid this let's reset the 4th bit which
808                  * is normally 1.
809                  */
810                 psmouse->packet[3] = psmouse->packet[6] & 0xf7;
811                 psmouse->pktcnt = 4;
812         }
813
814         return PSMOUSE_GOOD_DATA;
815 }
816
817 static void alps_flush_packet(unsigned long data)
818 {
819         struct psmouse *psmouse = (struct psmouse *)data;
820
821         serio_pause_rx(psmouse->ps2dev.serio);
822
823         if (psmouse->pktcnt == psmouse->pktsize) {
824
825                 /*
826                  * We did not any more data in reasonable amount of time.
827                  * Validate the last 3 bytes and process as a standard
828                  * ALPS packet.
829                  */
830                 if ((psmouse->packet[3] |
831                      psmouse->packet[4] |
832                      psmouse->packet[5]) & 0x80) {
833                         psmouse_dbg(psmouse,
834                                     "refusing packet %x %x %x (suspected interleaved ps/2)\n",
835                                     psmouse->packet[3], psmouse->packet[4],
836                                     psmouse->packet[5]);
837                 } else {
838                         alps_process_packet(psmouse);
839                 }
840                 psmouse->pktcnt = 0;
841         }
842
843         serio_continue_rx(psmouse->ps2dev.serio);
844 }
845
846 static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
847 {
848         struct alps_data *priv = psmouse->private;
849         const struct alps_model_info *model = priv->i;
850
851         if ((psmouse->packet[0] & 0xc8) == 0x08) { /* PS/2 packet */
852                 if (psmouse->pktcnt == 3) {
853                         alps_report_bare_ps2_packet(psmouse, psmouse->packet,
854                                                     true);
855                         return PSMOUSE_FULL_PACKET;
856                 }
857                 return PSMOUSE_GOOD_DATA;
858         }
859
860         /* Check for PS/2 packet stuffed in the middle of ALPS packet. */
861
862         if ((model->flags & ALPS_PS2_INTERLEAVED) &&
863             psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
864                 return alps_handle_interleaved_ps2(psmouse);
865         }
866
867         if (!alps_is_valid_first_byte(model, psmouse->packet[0])) {
868                 psmouse_dbg(psmouse,
869                             "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
870                             psmouse->packet[0], model->mask0, model->byte0);
871                 return PSMOUSE_BAD_DATA;
872         }
873
874         /* Bytes 2 - pktsize should have 0 in the highest bit */
875         if (psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
876             (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
877                 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
878                             psmouse->pktcnt - 1,
879                             psmouse->packet[psmouse->pktcnt - 1]);
880                 return PSMOUSE_BAD_DATA;
881         }
882
883         if (psmouse->pktcnt == psmouse->pktsize) {
884                 alps_process_packet(psmouse);
885                 return PSMOUSE_FULL_PACKET;
886         }
887
888         return PSMOUSE_GOOD_DATA;
889 }
890
891 static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
892 {
893         struct ps2dev *ps2dev = &psmouse->ps2dev;
894         struct alps_data *priv = psmouse->private;
895         int command;
896         unsigned char *param;
897         unsigned char dummy[4];
898
899         BUG_ON(nibble > 0xf);
900
901         command = priv->nibble_commands[nibble].command;
902         param = (command & 0x0f00) ?
903                 dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
904
905         if (ps2_command(ps2dev, param, command))
906                 return -1;
907
908         return 0;
909 }
910
911 static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
912 {
913         struct ps2dev *ps2dev = &psmouse->ps2dev;
914         struct alps_data *priv = psmouse->private;
915         int i, nibble;
916
917         if (ps2_command(ps2dev, NULL, priv->addr_command))
918                 return -1;
919
920         for (i = 12; i >= 0; i -= 4) {
921                 nibble = (addr >> i) & 0xf;
922                 if (alps_command_mode_send_nibble(psmouse, nibble))
923                         return -1;
924         }
925
926         return 0;
927 }
928
929 static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
930 {
931         struct ps2dev *ps2dev = &psmouse->ps2dev;
932         unsigned char param[4];
933
934         if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
935                 return -1;
936
937         /*
938          * The address being read is returned in the first two bytes
939          * of the result. Check that this address matches the expected
940          * address.
941          */
942         if (addr != ((param[0] << 8) | param[1]))
943                 return -1;
944
945         return param[2];
946 }
947
948 static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
949 {
950         if (alps_command_mode_set_addr(psmouse, addr))
951                 return -1;
952         return __alps_command_mode_read_reg(psmouse, addr);
953 }
954
955 static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
956 {
957         if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
958                 return -1;
959         if (alps_command_mode_send_nibble(psmouse, value & 0xf))
960                 return -1;
961         return 0;
962 }
963
964 static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
965                                        u8 value)
966 {
967         if (alps_command_mode_set_addr(psmouse, addr))
968                 return -1;
969         return __alps_command_mode_write_reg(psmouse, value);
970 }
971
972 static int alps_enter_command_mode(struct psmouse *psmouse,
973                                    unsigned char *resp)
974 {
975         unsigned char param[4];
976         struct ps2dev *ps2dev = &psmouse->ps2dev;
977
978         if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
979             ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
980             ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
981             ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) {
982                 psmouse_err(psmouse, "failed to enter command mode\n");
983                 return -1;
984         }
985
986         if (param[0] != 0x88 && param[1] != 0x07) {
987                 psmouse_dbg(psmouse,
988                             "unknown response while entering command mode: %2.2x %2.2x %2.2x\n",
989                             param[0], param[1], param[2]);
990                 return -1;
991         }
992
993         if (resp)
994                 *resp = param[2];
995         return 0;
996 }
997
998 static inline int alps_exit_command_mode(struct psmouse *psmouse)
999 {
1000         struct ps2dev *ps2dev = &psmouse->ps2dev;
1001         if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
1002                 return -1;
1003         return 0;
1004 }
1005
1006 static const struct alps_model_info *alps_get_model(struct psmouse *psmouse, int *version)
1007 {
1008         struct ps2dev *ps2dev = &psmouse->ps2dev;
1009         static const unsigned char rates[] = { 0, 10, 20, 40, 60, 80, 100, 200 };
1010         unsigned char param[4];
1011         const struct alps_model_info *model = NULL;
1012         int i;
1013
1014         /*
1015          * First try "E6 report".
1016          * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
1017          * The bits 0-2 of the first byte will be 1s if some buttons are
1018          * pressed.
1019          */
1020         param[0] = 0;
1021         if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES) ||
1022             ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE11) ||
1023             ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE11) ||
1024             ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE11))
1025                 return NULL;
1026
1027         param[0] = param[1] = param[2] = 0xff;
1028         if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1029                 return NULL;
1030
1031         psmouse_dbg(psmouse, "E6 report: %2.2x %2.2x %2.2x",
1032                     param[0], param[1], param[2]);
1033
1034         if ((param[0] & 0xf8) != 0 || param[1] != 0 ||
1035             (param[2] != 10 && param[2] != 100))
1036                 return NULL;
1037
1038         /*
1039          * Now try "E7 report". Allowed responses are in
1040          * alps_model_data[].signature
1041          */
1042         param[0] = 0;
1043         if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES) ||
1044             ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE21) ||
1045             ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE21) ||
1046             ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE21))
1047                 return NULL;
1048
1049         param[0] = param[1] = param[2] = 0xff;
1050         if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1051                 return NULL;
1052
1053         psmouse_dbg(psmouse, "E7 report: %2.2x %2.2x %2.2x",
1054                     param[0], param[1], param[2]);
1055
1056         if (version) {
1057                 for (i = 0; i < ARRAY_SIZE(rates) && param[2] != rates[i]; i++)
1058                         /* empty */;
1059                 *version = (param[0] << 8) | (param[1] << 4) | i;
1060         }
1061
1062         for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
1063                 if (!memcmp(param, alps_model_data[i].signature,
1064                             sizeof(alps_model_data[i].signature))) {
1065                         model = alps_model_data + i;
1066                         break;
1067                 }
1068         }
1069
1070         if (model && model->proto_version > ALPS_PROTO_V2) {
1071                 /*
1072                  * Need to check command mode response to identify
1073                  * model
1074                  */
1075                 model = NULL;
1076                 if (alps_enter_command_mode(psmouse, param)) {
1077                         psmouse_warn(psmouse,
1078                                      "touchpad failed to enter command mode\n");
1079                 } else {
1080                         for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
1081                                 if (alps_model_data[i].proto_version > ALPS_PROTO_V2 &&
1082                                     alps_model_data[i].command_mode_resp == param[0]) {
1083                                         model = alps_model_data + i;
1084                                         break;
1085                                 }
1086                         }
1087                         alps_exit_command_mode(psmouse);
1088
1089                         if (!model)
1090                                 psmouse_dbg(psmouse,
1091                                             "Unknown command mode response %2.2x\n",
1092                                             param[0]);
1093                 }
1094         }
1095
1096         return model;
1097 }
1098
1099 /*
1100  * For DualPoint devices select the device that should respond to
1101  * subsequent commands. It looks like glidepad is behind stickpointer,
1102  * I'd thought it would be other way around...
1103  */
1104 static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
1105 {
1106         struct ps2dev *ps2dev = &psmouse->ps2dev;
1107         int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
1108
1109         if (ps2_command(ps2dev, NULL, cmd) ||
1110             ps2_command(ps2dev, NULL, cmd) ||
1111             ps2_command(ps2dev, NULL, cmd) ||
1112             ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1113                 return -1;
1114
1115         /* we may get 3 more bytes, just ignore them */
1116         ps2_drain(ps2dev, 3, 100);
1117
1118         return 0;
1119 }
1120
1121 static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
1122 {
1123         struct ps2dev *ps2dev = &psmouse->ps2dev;
1124
1125         /* Try ALPS magic knock - 4 disable before enable */
1126         if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1127             ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1128             ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1129             ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1130             ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
1131                 return -1;
1132
1133         /*
1134          * Switch mouse to poll (remote) mode so motion data will not
1135          * get in our way
1136          */
1137         return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
1138 }
1139
1140 static int alps_get_status(struct psmouse *psmouse, char *param)
1141 {
1142         struct ps2dev *ps2dev = &psmouse->ps2dev;
1143
1144         /* Get status: 0xF5 0xF5 0xF5 0xE9 */
1145         if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1146             ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1147             ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1148             ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1149                 return -1;
1150
1151         psmouse_dbg(psmouse, "Status: %2.2x %2.2x %2.2x",
1152                     param[0], param[1], param[2]);
1153
1154         return 0;
1155 }
1156
1157 /*
1158  * Turn touchpad tapping on or off. The sequences are:
1159  * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
1160  * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
1161  * My guess that 0xE9 (GetInfo) is here as a sync point.
1162  * For models that also have stickpointer (DualPoints) its tapping
1163  * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
1164  * we don't fiddle with it.
1165  */
1166 static int alps_tap_mode(struct psmouse *psmouse, int enable)
1167 {
1168         struct ps2dev *ps2dev = &psmouse->ps2dev;
1169         int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
1170         unsigned char tap_arg = enable ? 0x0A : 0x00;
1171         unsigned char param[4];
1172
1173         if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
1174             ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1175             ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1176             ps2_command(ps2dev, &tap_arg, cmd))
1177                 return -1;
1178
1179         if (alps_get_status(psmouse, param))
1180                 return -1;
1181
1182         return 0;
1183 }
1184
1185 /*
1186  * alps_poll() - poll the touchpad for current motion packet.
1187  * Used in resync.
1188  */
1189 static int alps_poll(struct psmouse *psmouse)
1190 {
1191         struct alps_data *priv = psmouse->private;
1192         unsigned char buf[sizeof(psmouse->packet)];
1193         bool poll_failed;
1194
1195         if (priv->i->flags & ALPS_PASS)
1196                 alps_passthrough_mode_v2(psmouse, true);
1197
1198         poll_failed = ps2_command(&psmouse->ps2dev, buf,
1199                                   PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
1200
1201         if (priv->i->flags & ALPS_PASS)
1202                 alps_passthrough_mode_v2(psmouse, false);
1203
1204         if (poll_failed || (buf[0] & priv->i->mask0) != priv->i->byte0)
1205                 return -1;
1206
1207         if ((psmouse->badbyte & 0xc8) == 0x08) {
1208 /*
1209  * Poll the track stick ...
1210  */
1211                 if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
1212                         return -1;
1213         }
1214
1215         memcpy(psmouse->packet, buf, sizeof(buf));
1216         return 0;
1217 }
1218
1219 static int alps_hw_init_v1_v2(struct psmouse *psmouse)
1220 {
1221         struct alps_data *priv = psmouse->private;
1222         const struct alps_model_info *model = priv->i;
1223
1224         if ((model->flags & ALPS_PASS) &&
1225             alps_passthrough_mode_v2(psmouse, true)) {
1226                 return -1;
1227         }
1228
1229         if (alps_tap_mode(psmouse, true)) {
1230                 psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
1231                 return -1;
1232         }
1233
1234         if (alps_absolute_mode_v1_v2(psmouse)) {
1235                 psmouse_err(psmouse, "Failed to enable absolute mode\n");
1236                 return -1;
1237         }
1238
1239         if ((model->flags & ALPS_PASS) &&
1240             alps_passthrough_mode_v2(psmouse, false)) {
1241                 return -1;
1242         }
1243
1244         /* ALPS needs stream mode, otherwise it won't report any data */
1245         if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
1246                 psmouse_err(psmouse, "Failed to enable stream mode\n");
1247                 return -1;
1248         }
1249
1250         return 0;
1251 }
1252
1253 /*
1254  * Enable or disable passthrough mode to the trackstick. Must be in
1255  * command mode when calling this function.
1256  */
1257 static int alps_passthrough_mode_v3(struct psmouse *psmouse, bool enable)
1258 {
1259         int reg_val;
1260
1261         reg_val = alps_command_mode_read_reg(psmouse, 0x0008);
1262         if (reg_val == -1)
1263                 return -1;
1264
1265         if (enable)
1266                 reg_val |= 0x01;
1267         else
1268                 reg_val &= ~0x01;
1269
1270         if (__alps_command_mode_write_reg(psmouse, reg_val))
1271                 return -1;
1272
1273         return 0;
1274 }
1275
1276 /* Must be in command mode when calling this function */
1277 static int alps_absolute_mode_v3(struct psmouse *psmouse)
1278 {
1279         int reg_val;
1280
1281         reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
1282         if (reg_val == -1)
1283                 return -1;
1284
1285         reg_val |= 0x06;
1286         if (__alps_command_mode_write_reg(psmouse, reg_val))
1287                 return -1;
1288
1289         return 0;
1290 }
1291
1292 static int alps_hw_init_v3(struct psmouse *psmouse)
1293 {
1294         struct alps_data *priv = psmouse->private;
1295         struct ps2dev *ps2dev = &psmouse->ps2dev;
1296         int reg_val;
1297         unsigned char param[4];
1298
1299         priv->nibble_commands = alps_v3_nibble_commands;
1300         priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
1301
1302         if (alps_enter_command_mode(psmouse, NULL))
1303                 goto error;
1304
1305         /* Check for trackstick */
1306         reg_val = alps_command_mode_read_reg(psmouse, 0x0008);
1307         if (reg_val == -1)
1308                 goto error;
1309         if (reg_val & 0x80) {
1310                 if (alps_passthrough_mode_v3(psmouse, true))
1311                         goto error;
1312                 if (alps_exit_command_mode(psmouse))
1313                         goto error;
1314
1315                 /*
1316                  * E7 report for the trackstick
1317                  *
1318                  * There have been reports of failures to seem to trace back
1319                  * to the above trackstick check failing. When these occur
1320                  * this E7 report fails, so when that happens we continue
1321                  * with the assumption that there isn't a trackstick after
1322                  * all.
1323                  */
1324                 param[0] = 0x64;
1325                 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1326                     ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1327                     ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1328                     ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO)) {
1329                         psmouse_warn(psmouse, "trackstick E7 report failed\n");
1330                 } else {
1331                         psmouse_dbg(psmouse,
1332                                     "trackstick E7 report: %2.2x %2.2x %2.2x\n",
1333                                     param[0], param[1], param[2]);
1334
1335                         /*
1336                          * Not sure what this does, but it is absolutely
1337                          * essential. Without it, the touchpad does not
1338                          * work at all and the trackstick just emits normal
1339                          * PS/2 packets.
1340                          */
1341                         if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1342                             ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1343                             ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1344                             alps_command_mode_send_nibble(psmouse, 0x9) ||
1345                             alps_command_mode_send_nibble(psmouse, 0x4)) {
1346                                 psmouse_err(psmouse,
1347                                             "Error sending magic E6 sequence\n");
1348                                 goto error_passthrough;
1349                         }
1350                 }
1351
1352                 if (alps_enter_command_mode(psmouse, NULL))
1353                         goto error_passthrough;
1354                 if (alps_passthrough_mode_v3(psmouse, false))
1355                         goto error;
1356         }
1357
1358         if (alps_absolute_mode_v3(psmouse)) {
1359                 psmouse_err(psmouse, "Failed to enter absolute mode\n");
1360                 goto error;
1361         }
1362
1363         reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
1364         if (reg_val == -1)
1365                 goto error;
1366         if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
1367                 goto error;
1368
1369         reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
1370         if (reg_val == -1)
1371                 goto error;
1372         if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
1373                 goto error;
1374
1375         if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
1376                 goto error;
1377         if (__alps_command_mode_write_reg(psmouse, 0x04))
1378                 goto error;
1379
1380         if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
1381                 goto error;
1382         if (__alps_command_mode_write_reg(psmouse, 0x03))
1383                 goto error;
1384
1385         if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
1386                 goto error;
1387         if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
1388                 goto error;
1389
1390         if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
1391                 goto error;
1392         if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
1393                 goto error;
1394
1395         /*
1396          * This ensures the trackstick packets are in the format
1397          * supported by this driver. If bit 1 isn't set the packet
1398          * format is different.
1399          */
1400         if (alps_command_mode_write_reg(psmouse, 0x0008, 0x82))
1401                 goto error;
1402
1403         alps_exit_command_mode(psmouse);
1404
1405         /* Set rate and enable data reporting */
1406         param[0] = 0x64;
1407         if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
1408             ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1409                 psmouse_err(psmouse, "Failed to enable data reporting\n");
1410                 return -1;
1411         }
1412
1413         return 0;
1414
1415 error_passthrough:
1416         /* Something failed while in passthrough mode, so try to get out */
1417         if (!alps_enter_command_mode(psmouse, NULL))
1418                 alps_passthrough_mode_v3(psmouse, false);
1419 error:
1420         /*
1421          * Leaving the touchpad in command mode will essentially render
1422          * it unusable until the machine reboots, so exit it here just
1423          * to be safe
1424          */
1425         alps_exit_command_mode(psmouse);
1426         return -1;
1427 }
1428
1429 /* Must be in command mode when calling this function */
1430 static int alps_absolute_mode_v4(struct psmouse *psmouse)
1431 {
1432         int reg_val;
1433
1434         reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
1435         if (reg_val == -1)
1436                 return -1;
1437
1438         reg_val |= 0x02;
1439         if (__alps_command_mode_write_reg(psmouse, reg_val))
1440                 return -1;
1441
1442         return 0;
1443 }
1444
1445 static int alps_hw_init_v4(struct psmouse *psmouse)
1446 {
1447         struct alps_data *priv = psmouse->private;
1448         struct ps2dev *ps2dev = &psmouse->ps2dev;
1449         unsigned char param[4];
1450
1451         priv->nibble_commands = alps_v4_nibble_commands;
1452         priv->addr_command = PSMOUSE_CMD_DISABLE;
1453
1454         if (alps_enter_command_mode(psmouse, NULL))
1455                 goto error;
1456
1457         if (alps_absolute_mode_v4(psmouse)) {
1458                 psmouse_err(psmouse, "Failed to enter absolute mode\n");
1459                 goto error;
1460         }
1461
1462         if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
1463                 goto error;
1464
1465         if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
1466                 goto error;
1467
1468         if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
1469                 goto error;
1470
1471         if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
1472                 goto error;
1473
1474         if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
1475                 goto error;
1476
1477         if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
1478                 goto error;
1479
1480         if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
1481                 goto error;
1482
1483         if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
1484                 goto error;
1485
1486         alps_exit_command_mode(psmouse);
1487
1488         /*
1489          * This sequence changes the output from a 9-byte to an
1490          * 8-byte format. All the same data seems to be present,
1491          * just in a more compact format.
1492          */
1493         param[0] = 0xc8;
1494         param[1] = 0x64;
1495         param[2] = 0x50;
1496         if (ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
1497             ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE) ||
1498             ps2_command(ps2dev, &param[2], PSMOUSE_CMD_SETRATE) ||
1499             ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
1500                 return -1;
1501
1502         /* Set rate and enable data reporting */
1503         param[0] = 0x64;
1504         if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
1505             ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1506                 psmouse_err(psmouse, "Failed to enable data reporting\n");
1507                 return -1;
1508         }
1509
1510         return 0;
1511
1512 error:
1513         /*
1514          * Leaving the touchpad in command mode will essentially render
1515          * it unusable until the machine reboots, so exit it here just
1516          * to be safe
1517          */
1518         alps_exit_command_mode(psmouse);
1519         return -1;
1520 }
1521
1522 static int alps_hw_init(struct psmouse *psmouse)
1523 {
1524         struct alps_data *priv = psmouse->private;
1525         const struct alps_model_info *model = priv->i;
1526         int ret = -1;
1527
1528         switch (model->proto_version) {
1529         case ALPS_PROTO_V1:
1530         case ALPS_PROTO_V2:
1531                 ret = alps_hw_init_v1_v2(psmouse);
1532                 break;
1533         case ALPS_PROTO_V3:
1534                 ret = alps_hw_init_v3(psmouse);
1535                 break;
1536         case ALPS_PROTO_V4:
1537                 ret = alps_hw_init_v4(psmouse);
1538                 break;
1539         }
1540
1541         return ret;
1542 }
1543
1544 static int alps_reconnect(struct psmouse *psmouse)
1545 {
1546         const struct alps_model_info *model;
1547
1548         psmouse_reset(psmouse);
1549
1550         model = alps_get_model(psmouse, NULL);
1551         if (!model)
1552                 return -1;
1553
1554         return alps_hw_init(psmouse);
1555 }
1556
1557 static void alps_disconnect(struct psmouse *psmouse)
1558 {
1559         struct alps_data *priv = psmouse->private;
1560
1561         psmouse_reset(psmouse);
1562         del_timer_sync(&priv->timer);
1563         input_unregister_device(priv->dev2);
1564         kfree(priv);
1565 }
1566
1567 int alps_init(struct psmouse *psmouse)
1568 {
1569         struct alps_data *priv;
1570         const struct alps_model_info *model;
1571         struct input_dev *dev1 = psmouse->dev, *dev2;
1572         int version;
1573
1574         priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
1575         dev2 = input_allocate_device();
1576         if (!priv || !dev2)
1577                 goto init_fail;
1578
1579         priv->dev2 = dev2;
1580         setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
1581
1582         psmouse->private = priv;
1583
1584         psmouse_reset(psmouse);
1585
1586         model = alps_get_model(psmouse, &version);
1587         if (!model)
1588                 goto init_fail;
1589
1590         priv->i = model;
1591
1592         if (alps_hw_init(psmouse))
1593                 goto init_fail;
1594
1595         /*
1596          * Undo part of setup done for us by psmouse core since touchpad
1597          * is not a relative device.
1598          */
1599         __clear_bit(EV_REL, dev1->evbit);
1600         __clear_bit(REL_X, dev1->relbit);
1601         __clear_bit(REL_Y, dev1->relbit);
1602
1603         /*
1604          * Now set up our capabilities.
1605          */
1606         dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
1607         dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
1608         dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
1609         dev1->keybit[BIT_WORD(BTN_LEFT)] |=
1610                 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
1611
1612         dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
1613
1614         switch (model->proto_version) {
1615         case ALPS_PROTO_V1:
1616         case ALPS_PROTO_V2:
1617                 input_set_abs_params(dev1, ABS_X, 0, 1023, 0, 0);
1618                 input_set_abs_params(dev1, ABS_Y, 0, 767, 0, 0);
1619                 break;
1620         case ALPS_PROTO_V3:
1621         case ALPS_PROTO_V4:
1622                 set_bit(INPUT_PROP_SEMI_MT, dev1->propbit);
1623                 input_mt_init_slots(dev1, 2);
1624                 input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, ALPS_V3_X_MAX, 0, 0);
1625                 input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, ALPS_V3_Y_MAX, 0, 0);
1626
1627                 set_bit(BTN_TOOL_DOUBLETAP, dev1->keybit);
1628                 set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
1629                 set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
1630
1631                 input_set_abs_params(dev1, ABS_X, 0, ALPS_V3_X_MAX, 0, 0);
1632                 input_set_abs_params(dev1, ABS_Y, 0, ALPS_V3_Y_MAX, 0, 0);
1633                 break;
1634         }
1635
1636         input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
1637
1638         if (model->flags & ALPS_WHEEL) {
1639                 dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
1640                 dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
1641         }
1642
1643         if (model->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
1644                 dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
1645                 dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
1646         }
1647
1648         if (model->flags & ALPS_FOUR_BUTTONS) {
1649                 dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
1650                 dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
1651                 dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
1652                 dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
1653         } else {
1654                 dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
1655         }
1656
1657         snprintf(priv->phys, sizeof(priv->phys), "%s/input1", psmouse->ps2dev.serio->phys);
1658         dev2->phys = priv->phys;
1659         dev2->name = (model->flags & ALPS_DUALPOINT) ? "DualPoint Stick" : "PS/2 Mouse";
1660         dev2->id.bustype = BUS_I8042;
1661         dev2->id.vendor  = 0x0002;
1662         dev2->id.product = PSMOUSE_ALPS;
1663         dev2->id.version = 0x0000;
1664         dev2->dev.parent = &psmouse->ps2dev.serio->dev;
1665
1666         dev2->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
1667         dev2->relbit[BIT_WORD(REL_X)] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
1668         dev2->keybit[BIT_WORD(BTN_LEFT)] =
1669                 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_MIDDLE) | BIT_MASK(BTN_RIGHT);
1670
1671         if (input_register_device(priv->dev2))
1672                 goto init_fail;
1673
1674         psmouse->protocol_handler = alps_process_byte;
1675         psmouse->poll = alps_poll;
1676         psmouse->disconnect = alps_disconnect;
1677         psmouse->reconnect = alps_reconnect;
1678         psmouse->pktsize = model->proto_version == ALPS_PROTO_V4 ? 8 : 6;
1679
1680         /* We are having trouble resyncing ALPS touchpads so disable it for now */
1681         psmouse->resync_time = 0;
1682
1683         return 0;
1684
1685 init_fail:
1686         psmouse_reset(psmouse);
1687         input_free_device(dev2);
1688         kfree(priv);
1689         psmouse->private = NULL;
1690         return -1;
1691 }
1692
1693 int alps_detect(struct psmouse *psmouse, bool set_properties)
1694 {
1695         int version;
1696         const struct alps_model_info *model;
1697
1698         model = alps_get_model(psmouse, &version);
1699         if (!model)
1700                 return -1;
1701
1702         if (set_properties) {
1703                 psmouse->vendor = "ALPS";
1704                 psmouse->name = model->flags & ALPS_DUALPOINT ?
1705                                 "DualPoint TouchPad" : "GlidePoint";
1706                 psmouse->model = version;
1707         }
1708         return 0;
1709 }
1710