Merge tag 'backport/v3.14.24-ltsi-rc1/phy-rcar-gen2-usb-to-v3.15' into backport/v3...
[platform/adaptation/renesas_rcar/renesas_kernel.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_CMD_NIBBLE_10      0x01f2
31
32 #define ALPS_REG_BASE_RUSHMORE  0xc2c0
33 #define ALPS_REG_BASE_PINNACLE  0x0000
34
35 static const struct alps_nibble_commands alps_v3_nibble_commands[] = {
36         { PSMOUSE_CMD_SETPOLL,          0x00 }, /* 0 */
37         { PSMOUSE_CMD_RESET_DIS,        0x00 }, /* 1 */
38         { PSMOUSE_CMD_SETSCALE21,       0x00 }, /* 2 */
39         { PSMOUSE_CMD_SETRATE,          0x0a }, /* 3 */
40         { PSMOUSE_CMD_SETRATE,          0x14 }, /* 4 */
41         { PSMOUSE_CMD_SETRATE,          0x28 }, /* 5 */
42         { PSMOUSE_CMD_SETRATE,          0x3c }, /* 6 */
43         { PSMOUSE_CMD_SETRATE,          0x50 }, /* 7 */
44         { PSMOUSE_CMD_SETRATE,          0x64 }, /* 8 */
45         { PSMOUSE_CMD_SETRATE,          0xc8 }, /* 9 */
46         { ALPS_CMD_NIBBLE_10,           0x00 }, /* a */
47         { PSMOUSE_CMD_SETRES,           0x00 }, /* b */
48         { PSMOUSE_CMD_SETRES,           0x01 }, /* c */
49         { PSMOUSE_CMD_SETRES,           0x02 }, /* d */
50         { PSMOUSE_CMD_SETRES,           0x03 }, /* e */
51         { PSMOUSE_CMD_SETSCALE11,       0x00 }, /* f */
52 };
53
54 static const struct alps_nibble_commands alps_v4_nibble_commands[] = {
55         { PSMOUSE_CMD_ENABLE,           0x00 }, /* 0 */
56         { PSMOUSE_CMD_RESET_DIS,        0x00 }, /* 1 */
57         { PSMOUSE_CMD_SETSCALE21,       0x00 }, /* 2 */
58         { PSMOUSE_CMD_SETRATE,          0x0a }, /* 3 */
59         { PSMOUSE_CMD_SETRATE,          0x14 }, /* 4 */
60         { PSMOUSE_CMD_SETRATE,          0x28 }, /* 5 */
61         { PSMOUSE_CMD_SETRATE,          0x3c }, /* 6 */
62         { PSMOUSE_CMD_SETRATE,          0x50 }, /* 7 */
63         { PSMOUSE_CMD_SETRATE,          0x64 }, /* 8 */
64         { PSMOUSE_CMD_SETRATE,          0xc8 }, /* 9 */
65         { ALPS_CMD_NIBBLE_10,           0x00 }, /* a */
66         { PSMOUSE_CMD_SETRES,           0x00 }, /* b */
67         { PSMOUSE_CMD_SETRES,           0x01 }, /* c */
68         { PSMOUSE_CMD_SETRES,           0x02 }, /* d */
69         { PSMOUSE_CMD_SETRES,           0x03 }, /* e */
70         { PSMOUSE_CMD_SETSCALE11,       0x00 }, /* f */
71 };
72
73 static const struct alps_nibble_commands alps_v6_nibble_commands[] = {
74         { PSMOUSE_CMD_ENABLE,           0x00 }, /* 0 */
75         { PSMOUSE_CMD_SETRATE,          0x0a }, /* 1 */
76         { PSMOUSE_CMD_SETRATE,          0x14 }, /* 2 */
77         { PSMOUSE_CMD_SETRATE,          0x28 }, /* 3 */
78         { PSMOUSE_CMD_SETRATE,          0x3c }, /* 4 */
79         { PSMOUSE_CMD_SETRATE,          0x50 }, /* 5 */
80         { PSMOUSE_CMD_SETRATE,          0x64 }, /* 6 */
81         { PSMOUSE_CMD_SETRATE,          0xc8 }, /* 7 */
82         { PSMOUSE_CMD_GETID,            0x00 }, /* 8 */
83         { PSMOUSE_CMD_GETINFO,          0x00 }, /* 9 */
84         { PSMOUSE_CMD_SETRES,           0x00 }, /* a */
85         { PSMOUSE_CMD_SETRES,           0x01 }, /* b */
86         { PSMOUSE_CMD_SETRES,           0x02 }, /* c */
87         { PSMOUSE_CMD_SETRES,           0x03 }, /* d */
88         { PSMOUSE_CMD_SETSCALE21,       0x00 }, /* e */
89         { PSMOUSE_CMD_SETSCALE11,       0x00 }, /* f */
90 };
91
92
93 #define ALPS_DUALPOINT          0x02    /* touchpad has trackstick */
94 #define ALPS_PASS               0x04    /* device has a pass-through port */
95
96 #define ALPS_WHEEL              0x08    /* hardware wheel present */
97 #define ALPS_FW_BK_1            0x10    /* front & back buttons present */
98 #define ALPS_FW_BK_2            0x20    /* front & back buttons present */
99 #define ALPS_FOUR_BUTTONS       0x40    /* 4 direction button present */
100 #define ALPS_PS2_INTERLEAVED    0x80    /* 3-byte PS/2 packet interleaved with
101                                            6-byte ALPS packet */
102
103 static const struct alps_model_info alps_model_data[] = {
104         { { 0x32, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT },  /* Toshiba Salellite Pro M10 */
105         { { 0x33, 0x02, 0x0a }, 0x00, ALPS_PROTO_V1, 0x88, 0xf8, 0 },                           /* UMAX-530T */
106         { { 0x53, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
107         { { 0x53, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
108         { { 0x60, 0x03, 0xc8 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },                           /* HP ze1115 */
109         { { 0x63, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
110         { { 0x63, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
111         { { 0x63, 0x02, 0x28 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 },                /* Fujitsu Siemens S6010 */
112         { { 0x63, 0x02, 0x3c }, 0x00, ALPS_PROTO_V2, 0x8f, 0x8f, ALPS_WHEEL },                  /* Toshiba Satellite S2400-103 */
113         { { 0x63, 0x02, 0x50 }, 0x00, ALPS_PROTO_V2, 0xef, 0xef, ALPS_FW_BK_1 },                /* NEC Versa L320 */
114         { { 0x63, 0x02, 0x64 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
115         { { 0x63, 0x03, 0xc8 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT },  /* Dell Latitude D800 */
116         { { 0x73, 0x00, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_DUALPOINT },              /* ThinkPad R61 8918-5QG */
117         { { 0x73, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
118         { { 0x73, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 },                /* Ahtec Laptop */
119         { { 0x20, 0x02, 0x0e }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT },  /* XXX */
120         { { 0x22, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT },
121         { { 0x22, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT },  /* Dell Latitude D600 */
122         /* Dell Latitude E5500, E6400, E6500, Precision M4400 */
123         { { 0x62, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xcf, 0xcf,
124                 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED },
125         { { 0x73, 0x00, 0x14 }, 0x00, ALPS_PROTO_V6, 0xff, 0xff, ALPS_DUALPOINT },              /* Dell XT2 */
126         { { 0x73, 0x02, 0x50 }, 0x00, ALPS_PROTO_V2, 0xcf, 0xcf, ALPS_FOUR_BUTTONS },           /* Dell Vostro 1400 */
127         { { 0x52, 0x01, 0x14 }, 0x00, ALPS_PROTO_V2, 0xff, 0xff,
128                 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED },                            /* Toshiba Tecra A11-11L */
129         { { 0x73, 0x02, 0x64 }, 0x8a, ALPS_PROTO_V4, 0x8f, 0x8f, 0 },
130 };
131
132 static void alps_set_abs_params_st(struct alps_data *priv,
133                                    struct input_dev *dev1);
134 static void alps_set_abs_params_mt(struct alps_data *priv,
135                                    struct input_dev *dev1);
136
137 /*
138  * XXX - this entry is suspicious. First byte has zero lower nibble,
139  * which is what a normal mouse would report. Also, the value 0x0e
140  * isn't valid per PS/2 spec.
141  */
142
143 /* Packet formats are described in Documentation/input/alps.txt */
144
145 static bool alps_is_valid_first_byte(struct alps_data *priv,
146                                      unsigned char data)
147 {
148         return (data & priv->mask0) == priv->byte0;
149 }
150
151 static void alps_report_buttons(struct psmouse *psmouse,
152                                 struct input_dev *dev1, struct input_dev *dev2,
153                                 int left, int right, int middle)
154 {
155         struct input_dev *dev;
156
157         /*
158          * If shared button has already been reported on the
159          * other device (dev2) then this event should be also
160          * sent through that device.
161          */
162         dev = test_bit(BTN_LEFT, dev2->key) ? dev2 : dev1;
163         input_report_key(dev, BTN_LEFT, left);
164
165         dev = test_bit(BTN_RIGHT, dev2->key) ? dev2 : dev1;
166         input_report_key(dev, BTN_RIGHT, right);
167
168         dev = test_bit(BTN_MIDDLE, dev2->key) ? dev2 : dev1;
169         input_report_key(dev, BTN_MIDDLE, middle);
170
171         /*
172          * Sync the _other_ device now, we'll do the first
173          * device later once we report the rest of the events.
174          */
175         input_sync(dev2);
176 }
177
178 static void alps_process_packet_v1_v2(struct psmouse *psmouse)
179 {
180         struct alps_data *priv = psmouse->private;
181         unsigned char *packet = psmouse->packet;
182         struct input_dev *dev = psmouse->dev;
183         struct input_dev *dev2 = priv->dev2;
184         int x, y, z, ges, fin, left, right, middle;
185         int back = 0, forward = 0;
186
187         if (priv->proto_version == ALPS_PROTO_V1) {
188                 left = packet[2] & 0x10;
189                 right = packet[2] & 0x08;
190                 middle = 0;
191                 x = packet[1] | ((packet[0] & 0x07) << 7);
192                 y = packet[4] | ((packet[3] & 0x07) << 7);
193                 z = packet[5];
194         } else {
195                 left = packet[3] & 1;
196                 right = packet[3] & 2;
197                 middle = packet[3] & 4;
198                 x = packet[1] | ((packet[2] & 0x78) << (7 - 3));
199                 y = packet[4] | ((packet[3] & 0x70) << (7 - 4));
200                 z = packet[5];
201         }
202
203         if (priv->flags & ALPS_FW_BK_1) {
204                 back = packet[0] & 0x10;
205                 forward = packet[2] & 4;
206         }
207
208         if (priv->flags & ALPS_FW_BK_2) {
209                 back = packet[3] & 4;
210                 forward = packet[2] & 4;
211                 if ((middle = forward && back))
212                         forward = back = 0;
213         }
214
215         ges = packet[2] & 1;
216         fin = packet[2] & 2;
217
218         if ((priv->flags & ALPS_DUALPOINT) && z == 127) {
219                 input_report_rel(dev2, REL_X,  (x > 383 ? (x - 768) : x));
220                 input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y));
221
222                 alps_report_buttons(psmouse, dev2, dev, left, right, middle);
223
224                 input_sync(dev2);
225                 return;
226         }
227
228         alps_report_buttons(psmouse, dev, dev2, left, right, middle);
229
230         /* Convert hardware tap to a reasonable Z value */
231         if (ges && !fin)
232                 z = 40;
233
234         /*
235          * A "tap and drag" operation is reported by the hardware as a transition
236          * from (!fin && ges) to (fin && ges). This should be translated to the
237          * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
238          */
239         if (ges && fin && !priv->prev_fin) {
240                 input_report_abs(dev, ABS_X, x);
241                 input_report_abs(dev, ABS_Y, y);
242                 input_report_abs(dev, ABS_PRESSURE, 0);
243                 input_report_key(dev, BTN_TOOL_FINGER, 0);
244                 input_sync(dev);
245         }
246         priv->prev_fin = fin;
247
248         if (z > 30)
249                 input_report_key(dev, BTN_TOUCH, 1);
250         if (z < 25)
251                 input_report_key(dev, BTN_TOUCH, 0);
252
253         if (z > 0) {
254                 input_report_abs(dev, ABS_X, x);
255                 input_report_abs(dev, ABS_Y, y);
256         }
257
258         input_report_abs(dev, ABS_PRESSURE, z);
259         input_report_key(dev, BTN_TOOL_FINGER, z > 0);
260
261         if (priv->flags & ALPS_WHEEL)
262                 input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
263
264         if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
265                 input_report_key(dev, BTN_FORWARD, forward);
266                 input_report_key(dev, BTN_BACK, back);
267         }
268
269         if (priv->flags & ALPS_FOUR_BUTTONS) {
270                 input_report_key(dev, BTN_0, packet[2] & 4);
271                 input_report_key(dev, BTN_1, packet[0] & 0x10);
272                 input_report_key(dev, BTN_2, packet[3] & 4);
273                 input_report_key(dev, BTN_3, packet[0] & 0x20);
274         }
275
276         input_sync(dev);
277 }
278
279 /*
280  * Process bitmap data for V5 protocols. Return value is null.
281  *
282  * The bitmaps don't have enough data to track fingers, so this function
283  * only generates points representing a bounding box of at most two contacts.
284  * These two points are returned in x1, y1, x2, and y2.
285  */
286 static void alps_process_bitmap_dolphin(struct alps_data *priv,
287                                         struct alps_fields *fields,
288                                         int *x1, int *y1, int *x2, int *y2)
289 {
290         int box_middle_x, box_middle_y;
291         unsigned int x_map, y_map;
292         unsigned char start_bit, end_bit;
293         unsigned char x_msb, x_lsb, y_msb, y_lsb;
294
295         x_map = fields->x_map;
296         y_map = fields->y_map;
297
298         if (!x_map || !y_map)
299                 return;
300
301         /* Get Most-significant and Least-significant bit */
302         x_msb = fls(x_map);
303         x_lsb = ffs(x_map);
304         y_msb = fls(y_map);
305         y_lsb = ffs(y_map);
306
307         /* Most-significant bit should never exceed max sensor line number */
308         if (x_msb > priv->x_bits || y_msb > priv->y_bits)
309                 return;
310
311         *x1 = *y1 = *x2 = *y2 = 0;
312
313         if (fields->fingers > 1) {
314                 start_bit = priv->x_bits - x_msb;
315                 end_bit = priv->x_bits - x_lsb;
316                 box_middle_x = (priv->x_max * (start_bit + end_bit)) /
317                                 (2 * (priv->x_bits - 1));
318
319                 start_bit = y_lsb - 1;
320                 end_bit = y_msb - 1;
321                 box_middle_y = (priv->y_max * (start_bit + end_bit)) /
322                                 (2 * (priv->y_bits - 1));
323                 *x1 = fields->x;
324                 *y1 = fields->y;
325                 *x2 = 2 * box_middle_x - *x1;
326                 *y2 = 2 * box_middle_y - *y1;
327         }
328 }
329
330 /*
331  * Process bitmap data from v3 and v4 protocols. Returns the number of
332  * fingers detected. A return value of 0 means at least one of the
333  * bitmaps was empty.
334  *
335  * The bitmaps don't have enough data to track fingers, so this function
336  * only generates points representing a bounding box of all contacts.
337  * These points are returned in x1, y1, x2, and y2 when the return value
338  * is greater than 0.
339  */
340 static int alps_process_bitmap(struct alps_data *priv,
341                                unsigned int x_map, unsigned int y_map,
342                                int *x1, int *y1, int *x2, int *y2)
343 {
344         struct alps_bitmap_point {
345                 int start_bit;
346                 int num_bits;
347         };
348
349         int fingers_x = 0, fingers_y = 0, fingers;
350         int i, bit, prev_bit;
351         struct alps_bitmap_point x_low = {0,}, x_high = {0,};
352         struct alps_bitmap_point y_low = {0,}, y_high = {0,};
353         struct alps_bitmap_point *point;
354
355         if (!x_map || !y_map)
356                 return 0;
357
358         *x1 = *y1 = *x2 = *y2 = 0;
359
360         prev_bit = 0;
361         point = &x_low;
362         for (i = 0; x_map != 0; i++, x_map >>= 1) {
363                 bit = x_map & 1;
364                 if (bit) {
365                         if (!prev_bit) {
366                                 point->start_bit = i;
367                                 fingers_x++;
368                         }
369                         point->num_bits++;
370                 } else {
371                         if (prev_bit)
372                                 point = &x_high;
373                         else
374                                 point->num_bits = 0;
375                 }
376                 prev_bit = bit;
377         }
378
379         /*
380          * y bitmap is reversed for what we need (lower positions are in
381          * higher bits), so we process from the top end.
382          */
383         y_map = y_map << (sizeof(y_map) * BITS_PER_BYTE - priv->y_bits);
384         prev_bit = 0;
385         point = &y_low;
386         for (i = 0; y_map != 0; i++, y_map <<= 1) {
387                 bit = y_map & (1 << (sizeof(y_map) * BITS_PER_BYTE - 1));
388                 if (bit) {
389                         if (!prev_bit) {
390                                 point->start_bit = i;
391                                 fingers_y++;
392                         }
393                         point->num_bits++;
394                 } else {
395                         if (prev_bit)
396                                 point = &y_high;
397                         else
398                                 point->num_bits = 0;
399                 }
400                 prev_bit = bit;
401         }
402
403         /*
404          * Fingers can overlap, so we use the maximum count of fingers
405          * on either axis as the finger count.
406          */
407         fingers = max(fingers_x, fingers_y);
408
409         /*
410          * If total fingers is > 1 but either axis reports only a single
411          * contact, we have overlapping or adjacent fingers. For the
412          * purposes of creating a bounding box, divide the single contact
413          * (roughly) equally between the two points.
414          */
415         if (fingers > 1) {
416                 if (fingers_x == 1) {
417                         i = x_low.num_bits / 2;
418                         x_low.num_bits = x_low.num_bits - i;
419                         x_high.start_bit = x_low.start_bit + i;
420                         x_high.num_bits = max(i, 1);
421                 } else if (fingers_y == 1) {
422                         i = y_low.num_bits / 2;
423                         y_low.num_bits = y_low.num_bits - i;
424                         y_high.start_bit = y_low.start_bit + i;
425                         y_high.num_bits = max(i, 1);
426                 }
427         }
428
429         *x1 = (priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
430               (2 * (priv->x_bits - 1));
431         *y1 = (priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
432               (2 * (priv->y_bits - 1));
433
434         if (fingers > 1) {
435                 *x2 = (priv->x_max *
436                        (2 * x_high.start_bit + x_high.num_bits - 1)) /
437                       (2 * (priv->x_bits - 1));
438                 *y2 = (priv->y_max *
439                        (2 * y_high.start_bit + y_high.num_bits - 1)) /
440                       (2 * (priv->y_bits - 1));
441         }
442
443         return fingers;
444 }
445
446 static void alps_set_slot(struct input_dev *dev, int slot, bool active,
447                           int x, int y)
448 {
449         input_mt_slot(dev, slot);
450         input_mt_report_slot_state(dev, MT_TOOL_FINGER, active);
451         if (active) {
452                 input_report_abs(dev, ABS_MT_POSITION_X, x);
453                 input_report_abs(dev, ABS_MT_POSITION_Y, y);
454         }
455 }
456
457 static void alps_report_semi_mt_data(struct input_dev *dev, int num_fingers,
458                                      int x1, int y1, int x2, int y2)
459 {
460         alps_set_slot(dev, 0, num_fingers != 0, x1, y1);
461         alps_set_slot(dev, 1, num_fingers == 2, x2, y2);
462 }
463
464 static void alps_process_trackstick_packet_v3(struct psmouse *psmouse)
465 {
466         struct alps_data *priv = psmouse->private;
467         unsigned char *packet = psmouse->packet;
468         struct input_dev *dev = priv->dev2;
469         int x, y, z, left, right, middle;
470
471         /* Sanity check packet */
472         if (!(packet[0] & 0x40)) {
473                 psmouse_dbg(psmouse, "Bad trackstick packet, discarding\n");
474                 return;
475         }
476
477         /*
478          * There's a special packet that seems to indicate the end
479          * of a stream of trackstick data. Filter these out.
480          */
481         if (packet[1] == 0x7f && packet[2] == 0x7f && packet[4] == 0x7f)
482                 return;
483
484         x = (s8)(((packet[0] & 0x20) << 2) | (packet[1] & 0x7f));
485         y = (s8)(((packet[0] & 0x10) << 3) | (packet[2] & 0x7f));
486         z = (packet[4] & 0x7c) >> 2;
487
488         /*
489          * The x and y values tend to be quite large, and when used
490          * alone the trackstick is difficult to use. Scale them down
491          * to compensate.
492          */
493         x /= 8;
494         y /= 8;
495
496         input_report_rel(dev, REL_X, x);
497         input_report_rel(dev, REL_Y, -y);
498
499         /*
500          * Most ALPS models report the trackstick buttons in the touchpad
501          * packets, but a few report them here. No reliable way has been
502          * found to differentiate between the models upfront, so we enable
503          * the quirk in response to seeing a button press in the trackstick
504          * packet.
505          */
506         left = packet[3] & 0x01;
507         right = packet[3] & 0x02;
508         middle = packet[3] & 0x04;
509
510         if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) &&
511             (left || right || middle))
512                 priv->quirks |= ALPS_QUIRK_TRACKSTICK_BUTTONS;
513
514         if (priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) {
515                 input_report_key(dev, BTN_LEFT, left);
516                 input_report_key(dev, BTN_RIGHT, right);
517                 input_report_key(dev, BTN_MIDDLE, middle);
518         }
519
520         input_sync(dev);
521         return;
522 }
523
524 static void alps_decode_buttons_v3(struct alps_fields *f, unsigned char *p)
525 {
526         f->left = !!(p[3] & 0x01);
527         f->right = !!(p[3] & 0x02);
528         f->middle = !!(p[3] & 0x04);
529
530         f->ts_left = !!(p[3] & 0x10);
531         f->ts_right = !!(p[3] & 0x20);
532         f->ts_middle = !!(p[3] & 0x40);
533 }
534
535 static void alps_decode_pinnacle(struct alps_fields *f, unsigned char *p,
536                                  struct psmouse *psmouse)
537 {
538         f->first_mp = !!(p[4] & 0x40);
539         f->is_mp = !!(p[0] & 0x40);
540
541         f->fingers = (p[5] & 0x3) + 1;
542         f->x_map = ((p[4] & 0x7e) << 8) |
543                    ((p[1] & 0x7f) << 2) |
544                    ((p[0] & 0x30) >> 4);
545         f->y_map = ((p[3] & 0x70) << 4) |
546                    ((p[2] & 0x7f) << 1) |
547                    (p[4] & 0x01);
548
549         f->x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
550                ((p[0] & 0x30) >> 4);
551         f->y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
552         f->z = p[5] & 0x7f;
553
554         alps_decode_buttons_v3(f, p);
555 }
556
557 static void alps_decode_rushmore(struct alps_fields *f, unsigned char *p,
558                                  struct psmouse *psmouse)
559 {
560         alps_decode_pinnacle(f, p, psmouse);
561
562         f->x_map |= (p[5] & 0x10) << 11;
563         f->y_map |= (p[5] & 0x20) << 6;
564 }
565
566 static void alps_decode_dolphin(struct alps_fields *f, unsigned char *p,
567                                 struct psmouse *psmouse)
568 {
569         u64 palm_data = 0;
570         struct alps_data *priv = psmouse->private;
571
572         f->first_mp = !!(p[0] & 0x02);
573         f->is_mp = !!(p[0] & 0x20);
574
575         if (!f->is_mp) {
576                 f->x = ((p[1] & 0x7f) | ((p[4] & 0x0f) << 7));
577                 f->y = ((p[2] & 0x7f) | ((p[4] & 0xf0) << 3));
578                 f->z = (p[0] & 4) ? 0 : p[5] & 0x7f;
579                 alps_decode_buttons_v3(f, p);
580         } else {
581                 f->fingers = ((p[0] & 0x6) >> 1 |
582                      (p[0] & 0x10) >> 2);
583
584                 palm_data = (p[1] & 0x7f) |
585                             ((p[2] & 0x7f) << 7) |
586                             ((p[4] & 0x7f) << 14) |
587                             ((p[5] & 0x7f) << 21) |
588                             ((p[3] & 0x07) << 28) |
589                             (((u64)p[3] & 0x70) << 27) |
590                             (((u64)p[0] & 0x01) << 34);
591
592                 /* Y-profile is stored in P(0) to p(n-1), n = y_bits; */
593                 f->y_map = palm_data & (BIT(priv->y_bits) - 1);
594
595                 /* X-profile is stored in p(n) to p(n+m-1), m = x_bits; */
596                 f->x_map = (palm_data >> priv->y_bits) &
597                            (BIT(priv->x_bits) - 1);
598         }
599 }
600
601 static void alps_process_touchpad_packet_v3_v5(struct psmouse *psmouse)
602 {
603         struct alps_data *priv = psmouse->private;
604         unsigned char *packet = psmouse->packet;
605         struct input_dev *dev = psmouse->dev;
606         struct input_dev *dev2 = priv->dev2;
607         int x1 = 0, y1 = 0, x2 = 0, y2 = 0;
608         int fingers = 0, bmap_fn;
609         struct alps_fields f = {0};
610
611         priv->decode_fields(&f, packet, psmouse);
612
613         /*
614          * There's no single feature of touchpad position and bitmap packets
615          * that can be used to distinguish between them. We rely on the fact
616          * that a bitmap packet should always follow a position packet with
617          * bit 6 of packet[4] set.
618          */
619         if (priv->multi_packet) {
620                 /*
621                  * Sometimes a position packet will indicate a multi-packet
622                  * sequence, but then what follows is another position
623                  * packet. Check for this, and when it happens process the
624                  * position packet as usual.
625                  */
626                 if (f.is_mp) {
627                         fingers = f.fingers;
628                         if (priv->proto_version == ALPS_PROTO_V3) {
629                                 bmap_fn = alps_process_bitmap(priv, f.x_map,
630                                                               f.y_map, &x1, &y1,
631                                                               &x2, &y2);
632
633                                 /*
634                                  * We shouldn't report more than one finger if
635                                  * we don't have two coordinates.
636                                  */
637                                 if (fingers > 1 && bmap_fn < 2)
638                                         fingers = bmap_fn;
639
640                                 /* Now process position packet */
641                                 priv->decode_fields(&f, priv->multi_data,
642                                                     psmouse);
643                         } else {
644                                 /*
645                                  * Because Dolphin uses position packet's
646                                  * coordinate data as Pt1 and uses it to
647                                  * calculate Pt2, so we need to do position
648                                  * packet decode first.
649                                  */
650                                 priv->decode_fields(&f, priv->multi_data,
651                                                     psmouse);
652
653                                 /*
654                                  * Since Dolphin's finger number is reliable,
655                                  * there is no need to compare with bmap_fn.
656                                  */
657                                 alps_process_bitmap_dolphin(priv, &f, &x1, &y1,
658                                                             &x2, &y2);
659                         }
660                 } else {
661                         priv->multi_packet = 0;
662                 }
663         }
664
665         /*
666          * Bit 6 of byte 0 is not usually set in position packets. The only
667          * times it seems to be set is in situations where the data is
668          * suspect anyway, e.g. a palm resting flat on the touchpad. Given
669          * this combined with the fact that this bit is useful for filtering
670          * out misidentified bitmap packets, we reject anything with this
671          * bit set.
672          */
673         if (f.is_mp)
674                 return;
675
676         if (!priv->multi_packet && f.first_mp) {
677                 priv->multi_packet = 1;
678                 memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
679                 return;
680         }
681
682         priv->multi_packet = 0;
683
684         /*
685          * Sometimes the hardware sends a single packet with z = 0
686          * in the middle of a stream. Real releases generate packets
687          * with x, y, and z all zero, so these seem to be flukes.
688          * Ignore them.
689          */
690         if (f.x && f.y && !f.z)
691                 return;
692
693         /*
694          * If we don't have MT data or the bitmaps were empty, we have
695          * to rely on ST data.
696          */
697         if (!fingers) {
698                 x1 = f.x;
699                 y1 = f.y;
700                 fingers = f.z > 0 ? 1 : 0;
701         }
702
703         if (f.z >= 64)
704                 input_report_key(dev, BTN_TOUCH, 1);
705         else
706                 input_report_key(dev, BTN_TOUCH, 0);
707
708         alps_report_semi_mt_data(dev, fingers, x1, y1, x2, y2);
709
710         input_mt_report_finger_count(dev, fingers);
711
712         input_report_key(dev, BTN_LEFT, f.left);
713         input_report_key(dev, BTN_RIGHT, f.right);
714         input_report_key(dev, BTN_MIDDLE, f.middle);
715
716         if (f.z > 0) {
717                 input_report_abs(dev, ABS_X, f.x);
718                 input_report_abs(dev, ABS_Y, f.y);
719         }
720         input_report_abs(dev, ABS_PRESSURE, f.z);
721
722         input_sync(dev);
723
724         if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) {
725                 input_report_key(dev2, BTN_LEFT, f.ts_left);
726                 input_report_key(dev2, BTN_RIGHT, f.ts_right);
727                 input_report_key(dev2, BTN_MIDDLE, f.ts_middle);
728                 input_sync(dev2);
729         }
730 }
731
732 static void alps_process_packet_v3(struct psmouse *psmouse)
733 {
734         unsigned char *packet = psmouse->packet;
735
736         /*
737          * v3 protocol packets come in three types, two representing
738          * touchpad data and one representing trackstick data.
739          * Trackstick packets seem to be distinguished by always
740          * having 0x3f in the last byte. This value has never been
741          * observed in the last byte of either of the other types
742          * of packets.
743          */
744         if (packet[5] == 0x3f) {
745                 alps_process_trackstick_packet_v3(psmouse);
746                 return;
747         }
748
749         alps_process_touchpad_packet_v3_v5(psmouse);
750 }
751
752 static void alps_process_packet_v6(struct psmouse *psmouse)
753 {
754         struct alps_data *priv = psmouse->private;
755         unsigned char *packet = psmouse->packet;
756         struct input_dev *dev = psmouse->dev;
757         struct input_dev *dev2 = priv->dev2;
758         int x, y, z, left, right, middle;
759
760         /*
761          * We can use Byte5 to distinguish if the packet is from Touchpad
762          * or Trackpoint.
763          * Touchpad:    0 - 0x7E
764          * Trackpoint:  0x7F
765          */
766         if (packet[5] == 0x7F) {
767                 /* It should be a DualPoint when received Trackpoint packet */
768                 if (!(priv->flags & ALPS_DUALPOINT))
769                         return;
770
771                 /* Trackpoint packet */
772                 x = packet[1] | ((packet[3] & 0x20) << 2);
773                 y = packet[2] | ((packet[3] & 0x40) << 1);
774                 z = packet[4];
775                 left = packet[3] & 0x01;
776                 right = packet[3] & 0x02;
777                 middle = packet[3] & 0x04;
778
779                 /* To prevent the cursor jump when finger lifted */
780                 if (x == 0x7F && y == 0x7F && z == 0x7F)
781                         x = y = z = 0;
782
783                 /* Divide 4 since trackpoint's speed is too fast */
784                 input_report_rel(dev2, REL_X, (char)x / 4);
785                 input_report_rel(dev2, REL_Y, -((char)y / 4));
786
787                 input_report_key(dev2, BTN_LEFT, left);
788                 input_report_key(dev2, BTN_RIGHT, right);
789                 input_report_key(dev2, BTN_MIDDLE, middle);
790
791                 input_sync(dev2);
792                 return;
793         }
794
795         /* Touchpad packet */
796         x = packet[1] | ((packet[3] & 0x78) << 4);
797         y = packet[2] | ((packet[4] & 0x78) << 4);
798         z = packet[5];
799         left = packet[3] & 0x01;
800         right = packet[3] & 0x02;
801
802         if (z > 30)
803                 input_report_key(dev, BTN_TOUCH, 1);
804         if (z < 25)
805                 input_report_key(dev, BTN_TOUCH, 0);
806
807         if (z > 0) {
808                 input_report_abs(dev, ABS_X, x);
809                 input_report_abs(dev, ABS_Y, y);
810         }
811
812         input_report_abs(dev, ABS_PRESSURE, z);
813         input_report_key(dev, BTN_TOOL_FINGER, z > 0);
814
815         /* v6 touchpad does not have middle button */
816         input_report_key(dev, BTN_LEFT, left);
817         input_report_key(dev, BTN_RIGHT, right);
818
819         input_sync(dev);
820 }
821
822 static void alps_process_packet_v4(struct psmouse *psmouse)
823 {
824         struct alps_data *priv = psmouse->private;
825         unsigned char *packet = psmouse->packet;
826         struct input_dev *dev = psmouse->dev;
827         int offset;
828         int x, y, z;
829         int left, right;
830         int x1, y1, x2, y2;
831         int fingers = 0;
832         unsigned int x_bitmap, y_bitmap;
833
834         /*
835          * v4 has a 6-byte encoding for bitmap data, but this data is
836          * broken up between 3 normal packets. Use priv->multi_packet to
837          * track our position in the bitmap packet.
838          */
839         if (packet[6] & 0x40) {
840                 /* sync, reset position */
841                 priv->multi_packet = 0;
842         }
843
844         if (WARN_ON_ONCE(priv->multi_packet > 2))
845                 return;
846
847         offset = 2 * priv->multi_packet;
848         priv->multi_data[offset] = packet[6];
849         priv->multi_data[offset + 1] = packet[7];
850
851         if (++priv->multi_packet > 2) {
852                 priv->multi_packet = 0;
853
854                 x_bitmap = ((priv->multi_data[2] & 0x1f) << 10) |
855                            ((priv->multi_data[3] & 0x60) << 3) |
856                            ((priv->multi_data[0] & 0x3f) << 2) |
857                            ((priv->multi_data[1] & 0x60) >> 5);
858                 y_bitmap = ((priv->multi_data[5] & 0x01) << 10) |
859                            ((priv->multi_data[3] & 0x1f) << 5) |
860                             (priv->multi_data[1] & 0x1f);
861
862                 fingers = alps_process_bitmap(priv, x_bitmap, y_bitmap,
863                                               &x1, &y1, &x2, &y2);
864
865                 /* Store MT data.*/
866                 priv->fingers = fingers;
867                 priv->x1 = x1;
868                 priv->x2 = x2;
869                 priv->y1 = y1;
870                 priv->y2 = y2;
871         }
872
873         left = packet[4] & 0x01;
874         right = packet[4] & 0x02;
875
876         x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) |
877             ((packet[0] & 0x30) >> 4);
878         y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f);
879         z = packet[5] & 0x7f;
880
881         /*
882          * If there were no contacts in the bitmap, use ST
883          * points in MT reports.
884          * If there were two contacts or more, report MT data.
885          */
886         if (priv->fingers < 2) {
887                 x1 = x;
888                 y1 = y;
889                 fingers = z > 0 ? 1 : 0;
890         } else {
891                 fingers = priv->fingers;
892                 x1 = priv->x1;
893                 x2 = priv->x2;
894                 y1 = priv->y1;
895                 y2 = priv->y2;
896         }
897
898         if (z >= 64)
899                 input_report_key(dev, BTN_TOUCH, 1);
900         else
901                 input_report_key(dev, BTN_TOUCH, 0);
902
903         alps_report_semi_mt_data(dev, fingers, x1, y1, x2, y2);
904
905         input_mt_report_finger_count(dev, fingers);
906
907         input_report_key(dev, BTN_LEFT, left);
908         input_report_key(dev, BTN_RIGHT, right);
909
910         if (z > 0) {
911                 input_report_abs(dev, ABS_X, x);
912                 input_report_abs(dev, ABS_Y, y);
913         }
914         input_report_abs(dev, ABS_PRESSURE, z);
915
916         input_sync(dev);
917 }
918
919 static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
920                                         unsigned char packet[],
921                                         bool report_buttons)
922 {
923         struct alps_data *priv = psmouse->private;
924         struct input_dev *dev2 = priv->dev2;
925
926         if (report_buttons)
927                 alps_report_buttons(psmouse, dev2, psmouse->dev,
928                                 packet[0] & 1, packet[0] & 2, packet[0] & 4);
929
930         input_report_rel(dev2, REL_X,
931                 packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
932         input_report_rel(dev2, REL_Y,
933                 packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
934
935         input_sync(dev2);
936 }
937
938 static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
939 {
940         struct alps_data *priv = psmouse->private;
941
942         if (psmouse->pktcnt < 6)
943                 return PSMOUSE_GOOD_DATA;
944
945         if (psmouse->pktcnt == 6) {
946                 /*
947                  * Start a timer to flush the packet if it ends up last
948                  * 6-byte packet in the stream. Timer needs to fire
949                  * psmouse core times out itself. 20 ms should be enough
950                  * to decide if we are getting more data or not.
951                  */
952                 mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
953                 return PSMOUSE_GOOD_DATA;
954         }
955
956         del_timer(&priv->timer);
957
958         if (psmouse->packet[6] & 0x80) {
959
960                 /*
961                  * Highest bit is set - that means we either had
962                  * complete ALPS packet and this is start of the
963                  * next packet or we got garbage.
964                  */
965
966                 if (((psmouse->packet[3] |
967                       psmouse->packet[4] |
968                       psmouse->packet[5]) & 0x80) ||
969                     (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) {
970                         psmouse_dbg(psmouse,
971                                     "refusing packet %4ph (suspected interleaved ps/2)\n",
972                                     psmouse->packet + 3);
973                         return PSMOUSE_BAD_DATA;
974                 }
975
976                 priv->process_packet(psmouse);
977
978                 /* Continue with the next packet */
979                 psmouse->packet[0] = psmouse->packet[6];
980                 psmouse->pktcnt = 1;
981
982         } else {
983
984                 /*
985                  * High bit is 0 - that means that we indeed got a PS/2
986                  * packet in the middle of ALPS packet.
987                  *
988                  * There is also possibility that we got 6-byte ALPS
989                  * packet followed  by 3-byte packet from trackpoint. We
990                  * can not distinguish between these 2 scenarios but
991                  * because the latter is unlikely to happen in course of
992                  * normal operation (user would need to press all
993                  * buttons on the pad and start moving trackpoint
994                  * without touching the pad surface) we assume former.
995                  * Even if we are wrong the wost thing that would happen
996                  * the cursor would jump but we should not get protocol
997                  * de-synchronization.
998                  */
999
1000                 alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
1001                                             false);
1002
1003                 /*
1004                  * Continue with the standard ALPS protocol handling,
1005                  * but make sure we won't process it as an interleaved
1006                  * packet again, which may happen if all buttons are
1007                  * pressed. To avoid this let's reset the 4th bit which
1008                  * is normally 1.
1009                  */
1010                 psmouse->packet[3] = psmouse->packet[6] & 0xf7;
1011                 psmouse->pktcnt = 4;
1012         }
1013
1014         return PSMOUSE_GOOD_DATA;
1015 }
1016
1017 static void alps_flush_packet(unsigned long data)
1018 {
1019         struct psmouse *psmouse = (struct psmouse *)data;
1020         struct alps_data *priv = psmouse->private;
1021
1022         serio_pause_rx(psmouse->ps2dev.serio);
1023
1024         if (psmouse->pktcnt == psmouse->pktsize) {
1025
1026                 /*
1027                  * We did not any more data in reasonable amount of time.
1028                  * Validate the last 3 bytes and process as a standard
1029                  * ALPS packet.
1030                  */
1031                 if ((psmouse->packet[3] |
1032                      psmouse->packet[4] |
1033                      psmouse->packet[5]) & 0x80) {
1034                         psmouse_dbg(psmouse,
1035                                     "refusing packet %3ph (suspected interleaved ps/2)\n",
1036                                     psmouse->packet + 3);
1037                 } else {
1038                         priv->process_packet(psmouse);
1039                 }
1040                 psmouse->pktcnt = 0;
1041         }
1042
1043         serio_continue_rx(psmouse->ps2dev.serio);
1044 }
1045
1046 static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
1047 {
1048         struct alps_data *priv = psmouse->private;
1049
1050         /*
1051          * Check if we are dealing with a bare PS/2 packet, presumably from
1052          * a device connected to the external PS/2 port. Because bare PS/2
1053          * protocol does not have enough constant bits to self-synchronize
1054          * properly we only do this if the device is fully synchronized.
1055          */
1056         if (!psmouse->out_of_sync_cnt && (psmouse->packet[0] & 0xc8) == 0x08) {
1057                 if (psmouse->pktcnt == 3) {
1058                         alps_report_bare_ps2_packet(psmouse, psmouse->packet,
1059                                                     true);
1060                         return PSMOUSE_FULL_PACKET;
1061                 }
1062                 return PSMOUSE_GOOD_DATA;
1063         }
1064
1065         /* Check for PS/2 packet stuffed in the middle of ALPS packet. */
1066
1067         if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
1068             psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
1069                 return alps_handle_interleaved_ps2(psmouse);
1070         }
1071
1072         if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
1073                 psmouse_dbg(psmouse,
1074                             "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
1075                             psmouse->packet[0], priv->mask0, priv->byte0);
1076                 return PSMOUSE_BAD_DATA;
1077         }
1078
1079         /* Bytes 2 - pktsize should have 0 in the highest bit */
1080         if (priv->proto_version < ALPS_PROTO_V5 &&
1081             psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
1082             (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
1083                 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1084                             psmouse->pktcnt - 1,
1085                             psmouse->packet[psmouse->pktcnt - 1]);
1086
1087                 if (priv->proto_version == ALPS_PROTO_V3 &&
1088                     psmouse->pktcnt == psmouse->pktsize) {
1089                         /*
1090                          * Some Dell boxes, such as Latitude E6440 or E7440
1091                          * with closed lid, quite often smash last byte of
1092                          * otherwise valid packet with 0xff. Given that the
1093                          * next packet is very likely to be valid let's
1094                          * report PSMOUSE_FULL_PACKET but not process data,
1095                          * rather than reporting PSMOUSE_BAD_DATA and
1096                          * filling the logs.
1097                          */
1098                         return PSMOUSE_FULL_PACKET;
1099                 }
1100
1101                 return PSMOUSE_BAD_DATA;
1102         }
1103
1104         if (psmouse->pktcnt == psmouse->pktsize) {
1105                 priv->process_packet(psmouse);
1106                 return PSMOUSE_FULL_PACKET;
1107         }
1108
1109         return PSMOUSE_GOOD_DATA;
1110 }
1111
1112 static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
1113 {
1114         struct ps2dev *ps2dev = &psmouse->ps2dev;
1115         struct alps_data *priv = psmouse->private;
1116         int command;
1117         unsigned char *param;
1118         unsigned char dummy[4];
1119
1120         BUG_ON(nibble > 0xf);
1121
1122         command = priv->nibble_commands[nibble].command;
1123         param = (command & 0x0f00) ?
1124                 dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
1125
1126         if (ps2_command(ps2dev, param, command))
1127                 return -1;
1128
1129         return 0;
1130 }
1131
1132 static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
1133 {
1134         struct ps2dev *ps2dev = &psmouse->ps2dev;
1135         struct alps_data *priv = psmouse->private;
1136         int i, nibble;
1137
1138         if (ps2_command(ps2dev, NULL, priv->addr_command))
1139                 return -1;
1140
1141         for (i = 12; i >= 0; i -= 4) {
1142                 nibble = (addr >> i) & 0xf;
1143                 if (alps_command_mode_send_nibble(psmouse, nibble))
1144                         return -1;
1145         }
1146
1147         return 0;
1148 }
1149
1150 static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1151 {
1152         struct ps2dev *ps2dev = &psmouse->ps2dev;
1153         unsigned char param[4];
1154
1155         if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1156                 return -1;
1157
1158         /*
1159          * The address being read is returned in the first two bytes
1160          * of the result. Check that this address matches the expected
1161          * address.
1162          */
1163         if (addr != ((param[0] << 8) | param[1]))
1164                 return -1;
1165
1166         return param[2];
1167 }
1168
1169 static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1170 {
1171         if (alps_command_mode_set_addr(psmouse, addr))
1172                 return -1;
1173         return __alps_command_mode_read_reg(psmouse, addr);
1174 }
1175
1176 static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
1177 {
1178         if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
1179                 return -1;
1180         if (alps_command_mode_send_nibble(psmouse, value & 0xf))
1181                 return -1;
1182         return 0;
1183 }
1184
1185 static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
1186                                        u8 value)
1187 {
1188         if (alps_command_mode_set_addr(psmouse, addr))
1189                 return -1;
1190         return __alps_command_mode_write_reg(psmouse, value);
1191 }
1192
1193 static int alps_rpt_cmd(struct psmouse *psmouse, int init_command,
1194                         int repeated_command, unsigned char *param)
1195 {
1196         struct ps2dev *ps2dev = &psmouse->ps2dev;
1197
1198         param[0] = 0;
1199         if (init_command && ps2_command(ps2dev, param, init_command))
1200                 return -EIO;
1201
1202         if (ps2_command(ps2dev,  NULL, repeated_command) ||
1203             ps2_command(ps2dev,  NULL, repeated_command) ||
1204             ps2_command(ps2dev,  NULL, repeated_command))
1205                 return -EIO;
1206
1207         param[0] = param[1] = param[2] = 0xff;
1208         if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1209                 return -EIO;
1210
1211         psmouse_dbg(psmouse, "%2.2X report: %3ph\n",
1212                     repeated_command, param);
1213         return 0;
1214 }
1215
1216 static int alps_enter_command_mode(struct psmouse *psmouse)
1217 {
1218         unsigned char param[4];
1219
1220         if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) {
1221                 psmouse_err(psmouse, "failed to enter command mode\n");
1222                 return -1;
1223         }
1224
1225         if ((param[0] != 0x88 || (param[1] != 0x07 && param[1] != 0x08)) &&
1226             param[0] != 0x73) {
1227                 psmouse_dbg(psmouse,
1228                             "unknown response while entering command mode\n");
1229                 return -1;
1230         }
1231         return 0;
1232 }
1233
1234 static inline int alps_exit_command_mode(struct psmouse *psmouse)
1235 {
1236         struct ps2dev *ps2dev = &psmouse->ps2dev;
1237         if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
1238                 return -1;
1239         return 0;
1240 }
1241
1242 /*
1243  * For DualPoint devices select the device that should respond to
1244  * subsequent commands. It looks like glidepad is behind stickpointer,
1245  * I'd thought it would be other way around...
1246  */
1247 static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
1248 {
1249         struct ps2dev *ps2dev = &psmouse->ps2dev;
1250         int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
1251
1252         if (ps2_command(ps2dev, NULL, cmd) ||
1253             ps2_command(ps2dev, NULL, cmd) ||
1254             ps2_command(ps2dev, NULL, cmd) ||
1255             ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1256                 return -1;
1257
1258         /* we may get 3 more bytes, just ignore them */
1259         ps2_drain(ps2dev, 3, 100);
1260
1261         return 0;
1262 }
1263
1264 static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
1265 {
1266         struct ps2dev *ps2dev = &psmouse->ps2dev;
1267
1268         /* Try ALPS magic knock - 4 disable before enable */
1269         if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1270             ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1271             ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1272             ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1273             ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
1274                 return -1;
1275
1276         /*
1277          * Switch mouse to poll (remote) mode so motion data will not
1278          * get in our way
1279          */
1280         return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
1281 }
1282
1283 static int alps_monitor_mode_send_word(struct psmouse *psmouse, u16 word)
1284 {
1285         int i, nibble;
1286
1287         /*
1288          * b0-b11 are valid bits, send sequence is inverse.
1289          * e.g. when word = 0x0123, nibble send sequence is 3, 2, 1
1290          */
1291         for (i = 0; i <= 8; i += 4) {
1292                 nibble = (word >> i) & 0xf;
1293                 if (alps_command_mode_send_nibble(psmouse, nibble))
1294                         return -1;
1295         }
1296
1297         return 0;
1298 }
1299
1300 static int alps_monitor_mode_write_reg(struct psmouse *psmouse,
1301                                        u16 addr, u16 value)
1302 {
1303         struct ps2dev *ps2dev = &psmouse->ps2dev;
1304
1305         /* 0x0A0 is the command to write the word */
1306         if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE) ||
1307             alps_monitor_mode_send_word(psmouse, 0x0A0) ||
1308             alps_monitor_mode_send_word(psmouse, addr) ||
1309             alps_monitor_mode_send_word(psmouse, value) ||
1310             ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1311                 return -1;
1312
1313         return 0;
1314 }
1315
1316 static int alps_monitor_mode(struct psmouse *psmouse, bool enable)
1317 {
1318         struct ps2dev *ps2dev = &psmouse->ps2dev;
1319
1320         if (enable) {
1321                 /* EC E9 F5 F5 E7 E6 E7 E9 to enter monitor mode */
1322                 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
1323                     ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO) ||
1324                     ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1325                     ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1326                     ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1327                     ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1328                     ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1329                     ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO))
1330                         return -1;
1331         } else {
1332                 /* EC to exit monitor mode */
1333                 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP))
1334                         return -1;
1335         }
1336
1337         return 0;
1338 }
1339
1340 static int alps_absolute_mode_v6(struct psmouse *psmouse)
1341 {
1342         u16 reg_val = 0x181;
1343         int ret = -1;
1344
1345         /* enter monitor mode, to write the register */
1346         if (alps_monitor_mode(psmouse, true))
1347                 return -1;
1348
1349         ret = alps_monitor_mode_write_reg(psmouse, 0x000, reg_val);
1350
1351         if (alps_monitor_mode(psmouse, false))
1352                 ret = -1;
1353
1354         return ret;
1355 }
1356
1357 static int alps_get_status(struct psmouse *psmouse, char *param)
1358 {
1359         /* Get status: 0xF5 0xF5 0xF5 0xE9 */
1360         if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param))
1361                 return -1;
1362
1363         return 0;
1364 }
1365
1366 /*
1367  * Turn touchpad tapping on or off. The sequences are:
1368  * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
1369  * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
1370  * My guess that 0xE9 (GetInfo) is here as a sync point.
1371  * For models that also have stickpointer (DualPoints) its tapping
1372  * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
1373  * we don't fiddle with it.
1374  */
1375 static int alps_tap_mode(struct psmouse *psmouse, int enable)
1376 {
1377         struct ps2dev *ps2dev = &psmouse->ps2dev;
1378         int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
1379         unsigned char tap_arg = enable ? 0x0A : 0x00;
1380         unsigned char param[4];
1381
1382         if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
1383             ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1384             ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1385             ps2_command(ps2dev, &tap_arg, cmd))
1386                 return -1;
1387
1388         if (alps_get_status(psmouse, param))
1389                 return -1;
1390
1391         return 0;
1392 }
1393
1394 /*
1395  * alps_poll() - poll the touchpad for current motion packet.
1396  * Used in resync.
1397  */
1398 static int alps_poll(struct psmouse *psmouse)
1399 {
1400         struct alps_data *priv = psmouse->private;
1401         unsigned char buf[sizeof(psmouse->packet)];
1402         bool poll_failed;
1403
1404         if (priv->flags & ALPS_PASS)
1405                 alps_passthrough_mode_v2(psmouse, true);
1406
1407         poll_failed = ps2_command(&psmouse->ps2dev, buf,
1408                                   PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
1409
1410         if (priv->flags & ALPS_PASS)
1411                 alps_passthrough_mode_v2(psmouse, false);
1412
1413         if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
1414                 return -1;
1415
1416         if ((psmouse->badbyte & 0xc8) == 0x08) {
1417 /*
1418  * Poll the track stick ...
1419  */
1420                 if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
1421                         return -1;
1422         }
1423
1424         memcpy(psmouse->packet, buf, sizeof(buf));
1425         return 0;
1426 }
1427
1428 static int alps_hw_init_v1_v2(struct psmouse *psmouse)
1429 {
1430         struct alps_data *priv = psmouse->private;
1431
1432         if ((priv->flags & ALPS_PASS) &&
1433             alps_passthrough_mode_v2(psmouse, true)) {
1434                 return -1;
1435         }
1436
1437         if (alps_tap_mode(psmouse, true)) {
1438                 psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
1439                 return -1;
1440         }
1441
1442         if (alps_absolute_mode_v1_v2(psmouse)) {
1443                 psmouse_err(psmouse, "Failed to enable absolute mode\n");
1444                 return -1;
1445         }
1446
1447         if ((priv->flags & ALPS_PASS) &&
1448             alps_passthrough_mode_v2(psmouse, false)) {
1449                 return -1;
1450         }
1451
1452         /* ALPS needs stream mode, otherwise it won't report any data */
1453         if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
1454                 psmouse_err(psmouse, "Failed to enable stream mode\n");
1455                 return -1;
1456         }
1457
1458         return 0;
1459 }
1460
1461 static int alps_hw_init_v6(struct psmouse *psmouse)
1462 {
1463         unsigned char param[2] = {0xC8, 0x14};
1464
1465         /* Enter passthrough mode to let trackpoint enter 6byte raw mode */
1466         if (alps_passthrough_mode_v2(psmouse, true))
1467                 return -1;
1468
1469         if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1470             ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1471             ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1472             ps2_command(&psmouse->ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
1473             ps2_command(&psmouse->ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
1474                 return -1;
1475
1476         if (alps_passthrough_mode_v2(psmouse, false))
1477                 return -1;
1478
1479         if (alps_absolute_mode_v6(psmouse)) {
1480                 psmouse_err(psmouse, "Failed to enable absolute mode\n");
1481                 return -1;
1482         }
1483
1484         return 0;
1485 }
1486
1487 /*
1488  * Enable or disable passthrough mode to the trackstick.
1489  */
1490 static int alps_passthrough_mode_v3(struct psmouse *psmouse,
1491                                     int reg_base, bool enable)
1492 {
1493         int reg_val, ret = -1;
1494
1495         if (alps_enter_command_mode(psmouse))
1496                 return -1;
1497
1498         reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x0008);
1499         if (reg_val == -1)
1500                 goto error;
1501
1502         if (enable)
1503                 reg_val |= 0x01;
1504         else
1505                 reg_val &= ~0x01;
1506
1507         ret = __alps_command_mode_write_reg(psmouse, reg_val);
1508
1509 error:
1510         if (alps_exit_command_mode(psmouse))
1511                 ret = -1;
1512         return ret;
1513 }
1514
1515 /* Must be in command mode when calling this function */
1516 static int alps_absolute_mode_v3(struct psmouse *psmouse)
1517 {
1518         int reg_val;
1519
1520         reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
1521         if (reg_val == -1)
1522                 return -1;
1523
1524         reg_val |= 0x06;
1525         if (__alps_command_mode_write_reg(psmouse, reg_val))
1526                 return -1;
1527
1528         return 0;
1529 }
1530
1531 static int alps_probe_trackstick_v3(struct psmouse *psmouse, int reg_base)
1532 {
1533         int ret = -EIO, reg_val;
1534
1535         if (alps_enter_command_mode(psmouse))
1536                 goto error;
1537
1538         reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x08);
1539         if (reg_val == -1)
1540                 goto error;
1541
1542         /* bit 7: trackstick is present */
1543         ret = reg_val & 0x80 ? 0 : -ENODEV;
1544
1545 error:
1546         alps_exit_command_mode(psmouse);
1547         return ret;
1548 }
1549
1550 static int alps_setup_trackstick_v3(struct psmouse *psmouse, int reg_base)
1551 {
1552         struct ps2dev *ps2dev = &psmouse->ps2dev;
1553         int ret = 0;
1554         unsigned char param[4];
1555
1556         if (alps_passthrough_mode_v3(psmouse, reg_base, true))
1557                 return -EIO;
1558
1559         /*
1560          * E7 report for the trackstick
1561          *
1562          * There have been reports of failures to seem to trace back
1563          * to the above trackstick check failing. When these occur
1564          * this E7 report fails, so when that happens we continue
1565          * with the assumption that there isn't a trackstick after
1566          * all.
1567          */
1568         if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_SETSCALE21, param)) {
1569                 psmouse_warn(psmouse, "trackstick E7 report failed\n");
1570                 ret = -ENODEV;
1571         } else {
1572                 psmouse_dbg(psmouse, "trackstick E7 report: %3ph\n", param);
1573
1574                 /*
1575                  * Not sure what this does, but it is absolutely
1576                  * essential. Without it, the touchpad does not
1577                  * work at all and the trackstick just emits normal
1578                  * PS/2 packets.
1579                  */
1580                 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1581                     ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1582                     ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1583                     alps_command_mode_send_nibble(psmouse, 0x9) ||
1584                     alps_command_mode_send_nibble(psmouse, 0x4)) {
1585                         psmouse_err(psmouse,
1586                                     "Error sending magic E6 sequence\n");
1587                         ret = -EIO;
1588                         goto error;
1589                 }
1590
1591                 /*
1592                  * This ensures the trackstick packets are in the format
1593                  * supported by this driver. If bit 1 isn't set the packet
1594                  * format is different.
1595                  */
1596                 if (alps_enter_command_mode(psmouse) ||
1597                     alps_command_mode_write_reg(psmouse,
1598                                                 reg_base + 0x08, 0x82) ||
1599                     alps_exit_command_mode(psmouse))
1600                         ret = -EIO;
1601         }
1602
1603 error:
1604         if (alps_passthrough_mode_v3(psmouse, reg_base, false))
1605                 ret = -EIO;
1606
1607         return ret;
1608 }
1609
1610 static int alps_hw_init_v3(struct psmouse *psmouse)
1611 {
1612         struct ps2dev *ps2dev = &psmouse->ps2dev;
1613         int reg_val;
1614         unsigned char param[4];
1615
1616         reg_val = alps_probe_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE);
1617         if (reg_val == -EIO)
1618                 goto error;
1619
1620         if (reg_val == 0 &&
1621             alps_setup_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE) == -EIO)
1622                 goto error;
1623
1624         if (alps_enter_command_mode(psmouse) ||
1625             alps_absolute_mode_v3(psmouse)) {
1626                 psmouse_err(psmouse, "Failed to enter absolute mode\n");
1627                 goto error;
1628         }
1629
1630         reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
1631         if (reg_val == -1)
1632                 goto error;
1633         if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
1634                 goto error;
1635
1636         reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
1637         if (reg_val == -1)
1638                 goto error;
1639         if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
1640                 goto error;
1641
1642         if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
1643                 goto error;
1644         if (__alps_command_mode_write_reg(psmouse, 0x04))
1645                 goto error;
1646
1647         if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
1648                 goto error;
1649         if (__alps_command_mode_write_reg(psmouse, 0x03))
1650                 goto error;
1651
1652         if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
1653                 goto error;
1654         if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
1655                 goto error;
1656
1657         if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
1658                 goto error;
1659         if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
1660                 goto error;
1661
1662         alps_exit_command_mode(psmouse);
1663
1664         /* Set rate and enable data reporting */
1665         param[0] = 0x64;
1666         if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
1667             ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1668                 psmouse_err(psmouse, "Failed to enable data reporting\n");
1669                 return -1;
1670         }
1671
1672         return 0;
1673
1674 error:
1675         /*
1676          * Leaving the touchpad in command mode will essentially render
1677          * it unusable until the machine reboots, so exit it here just
1678          * to be safe
1679          */
1680         alps_exit_command_mode(psmouse);
1681         return -1;
1682 }
1683
1684 static int alps_hw_init_rushmore_v3(struct psmouse *psmouse)
1685 {
1686         struct alps_data *priv = psmouse->private;
1687         struct ps2dev *ps2dev = &psmouse->ps2dev;
1688         int reg_val, ret = -1;
1689
1690         if (priv->flags & ALPS_DUALPOINT) {
1691                 reg_val = alps_setup_trackstick_v3(psmouse,
1692                                                    ALPS_REG_BASE_RUSHMORE);
1693                 if (reg_val == -EIO)
1694                         goto error;
1695                 if (reg_val == -ENODEV)
1696                         priv->flags &= ~ALPS_DUALPOINT;
1697         }
1698
1699         if (alps_enter_command_mode(psmouse) ||
1700             alps_command_mode_read_reg(psmouse, 0xc2d9) == -1 ||
1701             alps_command_mode_write_reg(psmouse, 0xc2cb, 0x00))
1702                 goto error;
1703
1704         reg_val = alps_command_mode_read_reg(psmouse, 0xc2c6);
1705         if (reg_val == -1)
1706                 goto error;
1707         if (__alps_command_mode_write_reg(psmouse, reg_val & 0xfd))
1708                 goto error;
1709
1710         if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
1711                 goto error;
1712
1713         /* enter absolute mode */
1714         reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
1715         if (reg_val == -1)
1716                 goto error;
1717         if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
1718                 goto error;
1719
1720         alps_exit_command_mode(psmouse);
1721         return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
1722
1723 error:
1724         alps_exit_command_mode(psmouse);
1725         return ret;
1726 }
1727
1728 /* Must be in command mode when calling this function */
1729 static int alps_absolute_mode_v4(struct psmouse *psmouse)
1730 {
1731         int reg_val;
1732
1733         reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
1734         if (reg_val == -1)
1735                 return -1;
1736
1737         reg_val |= 0x02;
1738         if (__alps_command_mode_write_reg(psmouse, reg_val))
1739                 return -1;
1740
1741         return 0;
1742 }
1743
1744 static int alps_hw_init_v4(struct psmouse *psmouse)
1745 {
1746         struct ps2dev *ps2dev = &psmouse->ps2dev;
1747         unsigned char param[4];
1748
1749         if (alps_enter_command_mode(psmouse))
1750                 goto error;
1751
1752         if (alps_absolute_mode_v4(psmouse)) {
1753                 psmouse_err(psmouse, "Failed to enter absolute mode\n");
1754                 goto error;
1755         }
1756
1757         if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
1758                 goto error;
1759
1760         if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
1761                 goto error;
1762
1763         if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
1764                 goto error;
1765
1766         if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
1767                 goto error;
1768
1769         if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
1770                 goto error;
1771
1772         if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
1773                 goto error;
1774
1775         if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
1776                 goto error;
1777
1778         if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
1779                 goto error;
1780
1781         alps_exit_command_mode(psmouse);
1782
1783         /*
1784          * This sequence changes the output from a 9-byte to an
1785          * 8-byte format. All the same data seems to be present,
1786          * just in a more compact format.
1787          */
1788         param[0] = 0xc8;
1789         param[1] = 0x64;
1790         param[2] = 0x50;
1791         if (ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
1792             ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE) ||
1793             ps2_command(ps2dev, &param[2], PSMOUSE_CMD_SETRATE) ||
1794             ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
1795                 return -1;
1796
1797         /* Set rate and enable data reporting */
1798         param[0] = 0x64;
1799         if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
1800             ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1801                 psmouse_err(psmouse, "Failed to enable data reporting\n");
1802                 return -1;
1803         }
1804
1805         return 0;
1806
1807 error:
1808         /*
1809          * Leaving the touchpad in command mode will essentially render
1810          * it unusable until the machine reboots, so exit it here just
1811          * to be safe
1812          */
1813         alps_exit_command_mode(psmouse);
1814         return -1;
1815 }
1816
1817 static int alps_dolphin_get_device_area(struct psmouse *psmouse,
1818                                         struct alps_data *priv)
1819 {
1820         struct ps2dev *ps2dev = &psmouse->ps2dev;
1821         unsigned char param[4] = {0};
1822         int num_x_electrode, num_y_electrode;
1823
1824         if (alps_enter_command_mode(psmouse))
1825                 return -1;
1826
1827         param[0] = 0x0a;
1828         if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
1829             ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
1830             ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
1831             ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
1832             ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE))
1833                 return -1;
1834
1835         if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1836                 return -1;
1837
1838         /*
1839          * Dolphin's sensor line number is not fixed. It can be calculated
1840          * by adding the device's register value with DOLPHIN_PROFILE_X/YOFFSET.
1841          * Further more, we can get device's x_max and y_max by multiplying
1842          * sensor line number with DOLPHIN_COUNT_PER_ELECTRODE.
1843          *
1844          * e.g. When we get register's sensor_x = 11 & sensor_y = 8,
1845          *      real sensor line number X = 11 + 8 = 19, and
1846          *      real sensor line number Y = 8 + 1 = 9.
1847          *      So, x_max = (19 - 1) * 64 = 1152, and
1848          *          y_max = (9 - 1) * 64 = 512.
1849          */
1850         num_x_electrode = DOLPHIN_PROFILE_XOFFSET + (param[2] & 0x0F);
1851         num_y_electrode = DOLPHIN_PROFILE_YOFFSET + ((param[2] >> 4) & 0x0F);
1852         priv->x_bits = num_x_electrode;
1853         priv->y_bits = num_y_electrode;
1854         priv->x_max = (num_x_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
1855         priv->y_max = (num_y_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
1856
1857         if (alps_exit_command_mode(psmouse))
1858                 return -1;
1859
1860         return 0;
1861 }
1862
1863 static int alps_hw_init_dolphin_v1(struct psmouse *psmouse)
1864 {
1865         struct ps2dev *ps2dev = &psmouse->ps2dev;
1866         unsigned char param[2];
1867
1868         /* This is dolphin "v1" as empirically defined by florin9doi */
1869         param[0] = 0x64;
1870         param[1] = 0x28;
1871
1872         if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
1873             ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
1874             ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
1875                 return -1;
1876
1877         return 0;
1878 }
1879
1880 static void alps_set_defaults(struct alps_data *priv)
1881 {
1882         priv->byte0 = 0x8f;
1883         priv->mask0 = 0x8f;
1884         priv->flags = ALPS_DUALPOINT;
1885
1886         priv->x_max = 2000;
1887         priv->y_max = 1400;
1888         priv->x_bits = 15;
1889         priv->y_bits = 11;
1890
1891         switch (priv->proto_version) {
1892         case ALPS_PROTO_V1:
1893         case ALPS_PROTO_V2:
1894                 priv->hw_init = alps_hw_init_v1_v2;
1895                 priv->process_packet = alps_process_packet_v1_v2;
1896                 priv->set_abs_params = alps_set_abs_params_st;
1897                 priv->x_max = 1023;
1898                 priv->y_max = 767;
1899                 break;
1900         case ALPS_PROTO_V3:
1901                 priv->hw_init = alps_hw_init_v3;
1902                 priv->process_packet = alps_process_packet_v3;
1903                 priv->set_abs_params = alps_set_abs_params_mt;
1904                 priv->decode_fields = alps_decode_pinnacle;
1905                 priv->nibble_commands = alps_v3_nibble_commands;
1906                 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
1907                 break;
1908         case ALPS_PROTO_V4:
1909                 priv->hw_init = alps_hw_init_v4;
1910                 priv->process_packet = alps_process_packet_v4;
1911                 priv->set_abs_params = alps_set_abs_params_mt;
1912                 priv->nibble_commands = alps_v4_nibble_commands;
1913                 priv->addr_command = PSMOUSE_CMD_DISABLE;
1914                 break;
1915         case ALPS_PROTO_V5:
1916                 priv->hw_init = alps_hw_init_dolphin_v1;
1917                 priv->process_packet = alps_process_touchpad_packet_v3_v5;
1918                 priv->decode_fields = alps_decode_dolphin;
1919                 priv->set_abs_params = alps_set_abs_params_mt;
1920                 priv->nibble_commands = alps_v3_nibble_commands;
1921                 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
1922                 priv->byte0 = 0xc8;
1923                 priv->mask0 = 0xd8;
1924                 priv->flags = 0;
1925                 priv->x_max = 1360;
1926                 priv->y_max = 660;
1927                 priv->x_bits = 23;
1928                 priv->y_bits = 12;
1929                 break;
1930         case ALPS_PROTO_V6:
1931                 priv->hw_init = alps_hw_init_v6;
1932                 priv->process_packet = alps_process_packet_v6;
1933                 priv->set_abs_params = alps_set_abs_params_st;
1934                 priv->nibble_commands = alps_v6_nibble_commands;
1935                 priv->x_max = 2047;
1936                 priv->y_max = 1535;
1937                 break;
1938         }
1939 }
1940
1941 static int alps_match_table(struct psmouse *psmouse, struct alps_data *priv,
1942                             unsigned char *e7, unsigned char *ec)
1943 {
1944         const struct alps_model_info *model;
1945         int i;
1946
1947         for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
1948                 model = &alps_model_data[i];
1949
1950                 if (!memcmp(e7, model->signature, sizeof(model->signature)) &&
1951                     (!model->command_mode_resp ||
1952                      model->command_mode_resp == ec[2])) {
1953
1954                         priv->proto_version = model->proto_version;
1955                         alps_set_defaults(priv);
1956
1957                         priv->flags = model->flags;
1958                         priv->byte0 = model->byte0;
1959                         priv->mask0 = model->mask0;
1960
1961                         return 0;
1962                 }
1963         }
1964
1965         return -EINVAL;
1966 }
1967
1968 static int alps_identify(struct psmouse *psmouse, struct alps_data *priv)
1969 {
1970         unsigned char e6[4], e7[4], ec[4];
1971
1972         /*
1973          * First try "E6 report".
1974          * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
1975          * The bits 0-2 of the first byte will be 1s if some buttons are
1976          * pressed.
1977          */
1978         if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
1979                          PSMOUSE_CMD_SETSCALE11, e6))
1980                 return -EIO;
1981
1982         if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100))
1983                 return -EINVAL;
1984
1985         /*
1986          * Now get the "E7" and "EC" reports.  These will uniquely identify
1987          * most ALPS touchpads.
1988          */
1989         if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
1990                          PSMOUSE_CMD_SETSCALE21, e7) ||
1991             alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
1992                          PSMOUSE_CMD_RESET_WRAP, ec) ||
1993             alps_exit_command_mode(psmouse))
1994                 return -EIO;
1995
1996         if (alps_match_table(psmouse, priv, e7, ec) == 0) {
1997                 return 0;
1998         } else if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0x50 &&
1999                    ec[0] == 0x73 && (ec[1] == 0x01 || ec[1] == 0x02)) {
2000                 priv->proto_version = ALPS_PROTO_V5;
2001                 alps_set_defaults(priv);
2002                 if (alps_dolphin_get_device_area(psmouse, priv))
2003                         return -EIO;
2004                 else
2005                         return 0;
2006         } else if (ec[0] == 0x88 && ec[1] == 0x08) {
2007                 priv->proto_version = ALPS_PROTO_V3;
2008                 alps_set_defaults(priv);
2009
2010                 priv->hw_init = alps_hw_init_rushmore_v3;
2011                 priv->decode_fields = alps_decode_rushmore;
2012                 priv->x_bits = 16;
2013                 priv->y_bits = 12;
2014
2015                 /* hack to make addr_command, nibble_command available */
2016                 psmouse->private = priv;
2017
2018                 if (alps_probe_trackstick_v3(psmouse, ALPS_REG_BASE_RUSHMORE))
2019                         priv->flags &= ~ALPS_DUALPOINT;
2020
2021                 return 0;
2022         } else if (ec[0] == 0x88 && ec[1] == 0x07 &&
2023                    ec[2] >= 0x90 && ec[2] <= 0x9d) {
2024                 priv->proto_version = ALPS_PROTO_V3;
2025                 alps_set_defaults(priv);
2026
2027                 return 0;
2028         }
2029
2030         psmouse_info(psmouse,
2031                      "Unknown ALPS touchpad: E7=%3ph, EC=%3ph\n", e7, ec);
2032
2033         return -EINVAL;
2034 }
2035
2036 static int alps_reconnect(struct psmouse *psmouse)
2037 {
2038         struct alps_data *priv = psmouse->private;
2039
2040         psmouse_reset(psmouse);
2041
2042         if (alps_identify(psmouse, priv) < 0)
2043                 return -1;
2044
2045         return priv->hw_init(psmouse);
2046 }
2047
2048 static void alps_disconnect(struct psmouse *psmouse)
2049 {
2050         struct alps_data *priv = psmouse->private;
2051
2052         psmouse_reset(psmouse);
2053         del_timer_sync(&priv->timer);
2054         input_unregister_device(priv->dev2);
2055         kfree(priv);
2056 }
2057
2058 static void alps_set_abs_params_st(struct alps_data *priv,
2059                                    struct input_dev *dev1)
2060 {
2061         input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
2062         input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
2063 }
2064
2065 static void alps_set_abs_params_mt(struct alps_data *priv,
2066                                    struct input_dev *dev1)
2067 {
2068         set_bit(INPUT_PROP_SEMI_MT, dev1->propbit);
2069         input_mt_init_slots(dev1, 2, 0);
2070         input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
2071         input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
2072
2073         set_bit(BTN_TOOL_DOUBLETAP, dev1->keybit);
2074         set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
2075         set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
2076
2077         input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
2078         input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
2079 }
2080
2081 int alps_init(struct psmouse *psmouse)
2082 {
2083         struct alps_data *priv;
2084         struct input_dev *dev1 = psmouse->dev, *dev2;
2085
2086         priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
2087         dev2 = input_allocate_device();
2088         if (!priv || !dev2)
2089                 goto init_fail;
2090
2091         priv->dev2 = dev2;
2092         setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
2093
2094         psmouse->private = priv;
2095
2096         psmouse_reset(psmouse);
2097
2098         if (alps_identify(psmouse, priv) < 0)
2099                 goto init_fail;
2100
2101         if (priv->hw_init(psmouse))
2102                 goto init_fail;
2103
2104         /*
2105          * Undo part of setup done for us by psmouse core since touchpad
2106          * is not a relative device.
2107          */
2108         __clear_bit(EV_REL, dev1->evbit);
2109         __clear_bit(REL_X, dev1->relbit);
2110         __clear_bit(REL_Y, dev1->relbit);
2111
2112         /*
2113          * Now set up our capabilities.
2114          */
2115         dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
2116         dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
2117         dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
2118         dev1->keybit[BIT_WORD(BTN_LEFT)] |=
2119                 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
2120
2121         dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
2122
2123         priv->set_abs_params(priv, dev1);
2124         input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
2125
2126         if (priv->flags & ALPS_WHEEL) {
2127                 dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
2128                 dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
2129         }
2130
2131         if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
2132                 dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
2133                 dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
2134         }
2135
2136         if (priv->flags & ALPS_FOUR_BUTTONS) {
2137                 dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
2138                 dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
2139                 dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
2140                 dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
2141         } else {
2142                 dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
2143         }
2144
2145         snprintf(priv->phys, sizeof(priv->phys), "%s/input1", psmouse->ps2dev.serio->phys);
2146         dev2->phys = priv->phys;
2147         dev2->name = (priv->flags & ALPS_DUALPOINT) ?
2148                      "DualPoint Stick" : "ALPS PS/2 Device";
2149         dev2->id.bustype = BUS_I8042;
2150         dev2->id.vendor  = 0x0002;
2151         dev2->id.product = PSMOUSE_ALPS;
2152         dev2->id.version = 0x0000;
2153         dev2->dev.parent = &psmouse->ps2dev.serio->dev;
2154
2155         dev2->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
2156         dev2->relbit[BIT_WORD(REL_X)] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
2157         dev2->keybit[BIT_WORD(BTN_LEFT)] =
2158                 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_MIDDLE) | BIT_MASK(BTN_RIGHT);
2159
2160         if (input_register_device(priv->dev2))
2161                 goto init_fail;
2162
2163         psmouse->protocol_handler = alps_process_byte;
2164         psmouse->poll = alps_poll;
2165         psmouse->disconnect = alps_disconnect;
2166         psmouse->reconnect = alps_reconnect;
2167         psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
2168
2169         /* We are having trouble resyncing ALPS touchpads so disable it for now */
2170         psmouse->resync_time = 0;
2171
2172         /* Allow 2 invalid packets without resetting device */
2173         psmouse->resetafter = psmouse->pktsize * 2;
2174
2175         return 0;
2176
2177 init_fail:
2178         psmouse_reset(psmouse);
2179         input_free_device(dev2);
2180         kfree(priv);
2181         psmouse->private = NULL;
2182         return -1;
2183 }
2184
2185 int alps_detect(struct psmouse *psmouse, bool set_properties)
2186 {
2187         struct alps_data dummy;
2188
2189         if (alps_identify(psmouse, &dummy) < 0)
2190                 return -1;
2191
2192         if (set_properties) {
2193                 psmouse->vendor = "ALPS";
2194                 psmouse->name = dummy.flags & ALPS_DUALPOINT ?
2195                                 "DualPoint TouchPad" : "GlidePoint";
2196                 psmouse->model = dummy.proto_version << 8;
2197         }
2198         return 0;
2199 }
2200