]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/input/mouse/alps.c
f066761e898523636d53224cb3eeaae1948d1681
[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_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 #define ALPS_BUTTONPAD          0x200   /* device is a clickpad */
103
104 static const struct alps_model_info alps_model_data[] = {
105         { { 0x32, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },      /* Toshiba Salellite Pro M10 */
106         { { 0x33, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V1, 0x88, 0xf8, 0 } },                               /* UMAX-530T */
107         { { 0x53, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
108         { { 0x53, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
109         { { 0x60, 0x03, 0xc8 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },                               /* HP ze1115 */
110         { { 0x63, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
111         { { 0x63, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
112         { { 0x63, 0x02, 0x28 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 } },            /* Fujitsu Siemens S6010 */
113         { { 0x63, 0x02, 0x3c }, 0x00, { ALPS_PROTO_V2, 0x8f, 0x8f, ALPS_WHEEL } },              /* Toshiba Satellite S2400-103 */
114         { { 0x63, 0x02, 0x50 }, 0x00, { ALPS_PROTO_V2, 0xef, 0xef, ALPS_FW_BK_1 } },            /* NEC Versa L320 */
115         { { 0x63, 0x02, 0x64 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
116         { { 0x63, 0x03, 0xc8 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },      /* Dell Latitude D800 */
117         { { 0x73, 0x00, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_DUALPOINT } },          /* ThinkPad R61 8918-5QG */
118         { { 0x73, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, 0 } },
119         { { 0x73, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 } },            /* Ahtec Laptop */
120
121         /*
122          * XXX This entry is suspicious. First byte has zero lower nibble,
123          * which is what a normal mouse would report. Also, the value 0x0e
124          * isn't valid per PS/2 spec.
125          */
126         { { 0x20, 0x02, 0x0e }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },
127
128         { { 0x22, 0x02, 0x0a }, 0x00, { ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT } },
129         { { 0x22, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT } },      /* Dell Latitude D600 */
130         /* Dell Latitude E5500, E6400, E6500, Precision M4400 */
131         { { 0x62, 0x02, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xcf, 0xcf,
132                 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED } },
133         { { 0x73, 0x00, 0x14 }, 0x00, { ALPS_PROTO_V6, 0xff, 0xff, ALPS_DUALPOINT } },          /* Dell XT2 */
134         { { 0x73, 0x02, 0x50 }, 0x00, { ALPS_PROTO_V2, 0xcf, 0xcf, ALPS_FOUR_BUTTONS } },       /* Dell Vostro 1400 */
135         { { 0x52, 0x01, 0x14 }, 0x00, { ALPS_PROTO_V2, 0xff, 0xff,
136                 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED } },                          /* Toshiba Tecra A11-11L */
137         { { 0x73, 0x02, 0x64 }, 0x8a, { ALPS_PROTO_V4, 0x8f, 0x8f, 0 } },
138 };
139
140 static const struct alps_protocol_info alps_v3_protocol_data = {
141         ALPS_PROTO_V3, 0x8f, 0x8f, ALPS_DUALPOINT
142 };
143
144 static const struct alps_protocol_info alps_v3_rushmore_data = {
145         ALPS_PROTO_V3_RUSHMORE, 0x8f, 0x8f, ALPS_DUALPOINT
146 };
147
148 static const struct alps_protocol_info alps_v5_protocol_data = {
149         ALPS_PROTO_V5, 0xc8, 0xd8, 0
150 };
151
152 static const struct alps_protocol_info alps_v7_protocol_data = {
153         ALPS_PROTO_V7, 0x48, 0x48, ALPS_DUALPOINT
154 };
155
156 static const struct alps_protocol_info alps_v8_protocol_data = {
157         ALPS_PROTO_V8, 0x18, 0x18, 0
158 };
159
160 static void alps_set_abs_params_st(struct alps_data *priv,
161                                    struct input_dev *dev1);
162 static void alps_set_abs_params_mt(struct alps_data *priv,
163                                    struct input_dev *dev1);
164 static void alps_set_abs_params_v7(struct alps_data *priv,
165                                    struct input_dev *dev1);
166 static void alps_set_abs_params_ss4_v2(struct alps_data *priv,
167                                        struct input_dev *dev1);
168
169 /* Packet formats are described in Documentation/input/alps.txt */
170
171 static bool alps_is_valid_first_byte(struct alps_data *priv,
172                                      unsigned char data)
173 {
174         return (data & priv->mask0) == priv->byte0;
175 }
176
177 static void alps_report_buttons(struct input_dev *dev1, struct input_dev *dev2,
178                                 int left, int right, int middle)
179 {
180         struct input_dev *dev;
181
182         /*
183          * If shared button has already been reported on the
184          * other device (dev2) then this event should be also
185          * sent through that device.
186          */
187         dev = (dev2 && test_bit(BTN_LEFT, dev2->key)) ? dev2 : dev1;
188         input_report_key(dev, BTN_LEFT, left);
189
190         dev = (dev2 && test_bit(BTN_RIGHT, dev2->key)) ? dev2 : dev1;
191         input_report_key(dev, BTN_RIGHT, right);
192
193         dev = (dev2 && test_bit(BTN_MIDDLE, dev2->key)) ? dev2 : dev1;
194         input_report_key(dev, BTN_MIDDLE, middle);
195
196         /*
197          * Sync the _other_ device now, we'll do the first
198          * device later once we report the rest of the events.
199          */
200         if (dev2)
201                 input_sync(dev2);
202 }
203
204 static void alps_process_packet_v1_v2(struct psmouse *psmouse)
205 {
206         struct alps_data *priv = psmouse->private;
207         unsigned char *packet = psmouse->packet;
208         struct input_dev *dev = psmouse->dev;
209         struct input_dev *dev2 = priv->dev2;
210         int x, y, z, ges, fin, left, right, middle;
211         int back = 0, forward = 0;
212
213         if (priv->proto_version == ALPS_PROTO_V1) {
214                 left = packet[2] & 0x10;
215                 right = packet[2] & 0x08;
216                 middle = 0;
217                 x = packet[1] | ((packet[0] & 0x07) << 7);
218                 y = packet[4] | ((packet[3] & 0x07) << 7);
219                 z = packet[5];
220         } else {
221                 left = packet[3] & 1;
222                 right = packet[3] & 2;
223                 middle = packet[3] & 4;
224                 x = packet[1] | ((packet[2] & 0x78) << (7 - 3));
225                 y = packet[4] | ((packet[3] & 0x70) << (7 - 4));
226                 z = packet[5];
227         }
228
229         if (priv->flags & ALPS_FW_BK_1) {
230                 back = packet[0] & 0x10;
231                 forward = packet[2] & 4;
232         }
233
234         if (priv->flags & ALPS_FW_BK_2) {
235                 back = packet[3] & 4;
236                 forward = packet[2] & 4;
237                 if ((middle = forward && back))
238                         forward = back = 0;
239         }
240
241         ges = packet[2] & 1;
242         fin = packet[2] & 2;
243
244         if ((priv->flags & ALPS_DUALPOINT) && z == 127) {
245                 input_report_rel(dev2, REL_X,  (x > 383 ? (x - 768) : x));
246                 input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y));
247
248                 alps_report_buttons(dev2, dev, left, right, middle);
249
250                 input_sync(dev2);
251                 return;
252         }
253
254         alps_report_buttons(dev, dev2, left, right, middle);
255
256         /* Convert hardware tap to a reasonable Z value */
257         if (ges && !fin)
258                 z = 40;
259
260         /*
261          * A "tap and drag" operation is reported by the hardware as a transition
262          * from (!fin && ges) to (fin && ges). This should be translated to the
263          * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
264          */
265         if (ges && fin && !priv->prev_fin) {
266                 input_report_abs(dev, ABS_X, x);
267                 input_report_abs(dev, ABS_Y, y);
268                 input_report_abs(dev, ABS_PRESSURE, 0);
269                 input_report_key(dev, BTN_TOOL_FINGER, 0);
270                 input_sync(dev);
271         }
272         priv->prev_fin = fin;
273
274         if (z > 30)
275                 input_report_key(dev, BTN_TOUCH, 1);
276         if (z < 25)
277                 input_report_key(dev, BTN_TOUCH, 0);
278
279         if (z > 0) {
280                 input_report_abs(dev, ABS_X, x);
281                 input_report_abs(dev, ABS_Y, y);
282         }
283
284         input_report_abs(dev, ABS_PRESSURE, z);
285         input_report_key(dev, BTN_TOOL_FINGER, z > 0);
286
287         if (priv->flags & ALPS_WHEEL)
288                 input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
289
290         if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
291                 input_report_key(dev, BTN_FORWARD, forward);
292                 input_report_key(dev, BTN_BACK, back);
293         }
294
295         if (priv->flags & ALPS_FOUR_BUTTONS) {
296                 input_report_key(dev, BTN_0, packet[2] & 4);
297                 input_report_key(dev, BTN_1, packet[0] & 0x10);
298                 input_report_key(dev, BTN_2, packet[3] & 4);
299                 input_report_key(dev, BTN_3, packet[0] & 0x20);
300         }
301
302         input_sync(dev);
303 }
304
305 /*
306  * Process bitmap data for V5 protocols. Return value is null.
307  *
308  * The bitmaps don't have enough data to track fingers, so this function
309  * only generates points representing a bounding box of at most two contacts.
310  * These two points are returned in fields->mt.
311  */
312 static void alps_process_bitmap_dolphin(struct alps_data *priv,
313                                         struct alps_fields *fields)
314 {
315         int box_middle_x, box_middle_y;
316         unsigned int x_map, y_map;
317         unsigned char start_bit, end_bit;
318         unsigned char x_msb, x_lsb, y_msb, y_lsb;
319
320         x_map = fields->x_map;
321         y_map = fields->y_map;
322
323         if (!x_map || !y_map)
324                 return;
325
326         /* Get Most-significant and Least-significant bit */
327         x_msb = fls(x_map);
328         x_lsb = ffs(x_map);
329         y_msb = fls(y_map);
330         y_lsb = ffs(y_map);
331
332         /* Most-significant bit should never exceed max sensor line number */
333         if (x_msb > priv->x_bits || y_msb > priv->y_bits)
334                 return;
335
336         if (fields->fingers > 1) {
337                 start_bit = priv->x_bits - x_msb;
338                 end_bit = priv->x_bits - x_lsb;
339                 box_middle_x = (priv->x_max * (start_bit + end_bit)) /
340                                 (2 * (priv->x_bits - 1));
341
342                 start_bit = y_lsb - 1;
343                 end_bit = y_msb - 1;
344                 box_middle_y = (priv->y_max * (start_bit + end_bit)) /
345                                 (2 * (priv->y_bits - 1));
346                 fields->mt[0] = fields->st;
347                 fields->mt[1].x = 2 * box_middle_x - fields->mt[0].x;
348                 fields->mt[1].y = 2 * box_middle_y - fields->mt[0].y;
349         }
350 }
351
352 static void alps_get_bitmap_points(unsigned int map,
353                                    struct alps_bitmap_point *low,
354                                    struct alps_bitmap_point *high,
355                                    int *fingers)
356 {
357         struct alps_bitmap_point *point;
358         int i, bit, prev_bit = 0;
359
360         point = low;
361         for (i = 0; map != 0; i++, map >>= 1) {
362                 bit = map & 1;
363                 if (bit) {
364                         if (!prev_bit) {
365                                 point->start_bit = i;
366                                 point->num_bits = 0;
367                                 (*fingers)++;
368                         }
369                         point->num_bits++;
370                 } else {
371                         if (prev_bit)
372                                 point = high;
373                 }
374                 prev_bit = bit;
375         }
376 }
377
378 /*
379  * Process bitmap data from v3 and v4 protocols. Returns the number of
380  * fingers detected. A return value of 0 means at least one of the
381  * bitmaps was empty.
382  *
383  * The bitmaps don't have enough data to track fingers, so this function
384  * only generates points representing a bounding box of all contacts.
385  * These points are returned in fields->mt when the return value
386  * is greater than 0.
387  */
388 static int alps_process_bitmap(struct alps_data *priv,
389                                struct alps_fields *fields)
390 {
391         int i, fingers_x = 0, fingers_y = 0, fingers, closest;
392         struct alps_bitmap_point x_low = {0,}, x_high = {0,};
393         struct alps_bitmap_point y_low = {0,}, y_high = {0,};
394         struct input_mt_pos corner[4];
395
396         if (!fields->x_map || !fields->y_map)
397                 return 0;
398
399         alps_get_bitmap_points(fields->x_map, &x_low, &x_high, &fingers_x);
400         alps_get_bitmap_points(fields->y_map, &y_low, &y_high, &fingers_y);
401
402         /*
403          * Fingers can overlap, so we use the maximum count of fingers
404          * on either axis as the finger count.
405          */
406         fingers = max(fingers_x, fingers_y);
407
408         /*
409          * If an axis reports only a single contact, we have overlapping or
410          * adjacent fingers. Divide the single contact between the two points.
411          */
412         if (fingers_x == 1) {
413                 i = (x_low.num_bits - 1) / 2;
414                 x_low.num_bits = x_low.num_bits - i;
415                 x_high.start_bit = x_low.start_bit + i;
416                 x_high.num_bits = max(i, 1);
417         }
418         if (fingers_y == 1) {
419                 i = (y_low.num_bits - 1) / 2;
420                 y_low.num_bits = y_low.num_bits - i;
421                 y_high.start_bit = y_low.start_bit + i;
422                 y_high.num_bits = max(i, 1);
423         }
424
425         /* top-left corner */
426         corner[0].x =
427                 (priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
428                 (2 * (priv->x_bits - 1));
429         corner[0].y =
430                 (priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
431                 (2 * (priv->y_bits - 1));
432
433         /* top-right corner */
434         corner[1].x =
435                 (priv->x_max * (2 * x_high.start_bit + x_high.num_bits - 1)) /
436                 (2 * (priv->x_bits - 1));
437         corner[1].y =
438                 (priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
439                 (2 * (priv->y_bits - 1));
440
441         /* bottom-right corner */
442         corner[2].x =
443                 (priv->x_max * (2 * x_high.start_bit + x_high.num_bits - 1)) /
444                 (2 * (priv->x_bits - 1));
445         corner[2].y =
446                 (priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) /
447                 (2 * (priv->y_bits - 1));
448
449         /* bottom-left corner */
450         corner[3].x =
451                 (priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
452                 (2 * (priv->x_bits - 1));
453         corner[3].y =
454                 (priv->y_max * (2 * y_high.start_bit + y_high.num_bits - 1)) /
455                 (2 * (priv->y_bits - 1));
456
457         /* y-bitmap order is reversed, except on rushmore */
458         if (priv->proto_version != ALPS_PROTO_V3_RUSHMORE) {
459                 for (i = 0; i < 4; i++)
460                         corner[i].y = priv->y_max - corner[i].y;
461         }
462
463         /*
464          * We only select a corner for the second touch once per 2 finger
465          * touch sequence to avoid the chosen corner (and thus the coordinates)
466          * jumping around when the first touch is in the middle.
467          */
468         if (priv->second_touch == -1) {
469                 /* Find corner closest to our st coordinates */
470                 closest = 0x7fffffff;
471                 for (i = 0; i < 4; i++) {
472                         int dx = fields->st.x - corner[i].x;
473                         int dy = fields->st.y - corner[i].y;
474                         int distance = dx * dx + dy * dy;
475
476                         if (distance < closest) {
477                                 priv->second_touch = i;
478                                 closest = distance;
479                         }
480                 }
481                 /* And select the opposite corner to use for the 2nd touch */
482                 priv->second_touch = (priv->second_touch + 2) % 4;
483         }
484
485         fields->mt[0] = fields->st;
486         fields->mt[1] = corner[priv->second_touch];
487
488         return fingers;
489 }
490
491 static void alps_set_slot(struct input_dev *dev, int slot, int x, int y)
492 {
493         input_mt_slot(dev, slot);
494         input_mt_report_slot_state(dev, MT_TOOL_FINGER, true);
495         input_report_abs(dev, ABS_MT_POSITION_X, x);
496         input_report_abs(dev, ABS_MT_POSITION_Y, y);
497 }
498
499 static void alps_report_mt_data(struct psmouse *psmouse, int n)
500 {
501         struct alps_data *priv = psmouse->private;
502         struct input_dev *dev = psmouse->dev;
503         struct alps_fields *f = &priv->f;
504         int i, slot[MAX_TOUCHES];
505
506         input_mt_assign_slots(dev, slot, f->mt, n, 0);
507         for (i = 0; i < n; i++)
508                 alps_set_slot(dev, slot[i], f->mt[i].x, f->mt[i].y);
509
510         input_mt_sync_frame(dev);
511 }
512
513 static void alps_report_semi_mt_data(struct psmouse *psmouse, int fingers)
514 {
515         struct alps_data *priv = psmouse->private;
516         struct input_dev *dev = psmouse->dev;
517         struct alps_fields *f = &priv->f;
518
519         /* Use st data when we don't have mt data */
520         if (fingers < 2) {
521                 f->mt[0].x = f->st.x;
522                 f->mt[0].y = f->st.y;
523                 fingers = f->pressure > 0 ? 1 : 0;
524                 priv->second_touch = -1;
525         }
526
527         if (fingers >= 1)
528                 alps_set_slot(dev, 0, f->mt[0].x, f->mt[0].y);
529         if (fingers >= 2)
530                 alps_set_slot(dev, 1, f->mt[1].x, f->mt[1].y);
531         input_mt_sync_frame(dev);
532
533         input_mt_report_finger_count(dev, fingers);
534
535         input_report_key(dev, BTN_LEFT, f->left);
536         input_report_key(dev, BTN_RIGHT, f->right);
537         input_report_key(dev, BTN_MIDDLE, f->middle);
538
539         input_report_abs(dev, ABS_PRESSURE, f->pressure);
540
541         input_sync(dev);
542 }
543
544 static void alps_process_trackstick_packet_v3(struct psmouse *psmouse)
545 {
546         struct alps_data *priv = psmouse->private;
547         unsigned char *packet = psmouse->packet;
548         struct input_dev *dev = priv->dev2;
549         int x, y, z, left, right, middle;
550
551         /* It should be a DualPoint when received trackstick packet */
552         if (!(priv->flags & ALPS_DUALPOINT)) {
553                 psmouse_warn(psmouse,
554                              "Rejected trackstick packet from non DualPoint device");
555                 return;
556         }
557
558         /* Sanity check packet */
559         if (!(packet[0] & 0x40)) {
560                 psmouse_dbg(psmouse, "Bad trackstick packet, discarding\n");
561                 return;
562         }
563
564         /*
565          * There's a special packet that seems to indicate the end
566          * of a stream of trackstick data. Filter these out.
567          */
568         if (packet[1] == 0x7f && packet[2] == 0x7f && packet[4] == 0x7f)
569                 return;
570
571         x = (s8)(((packet[0] & 0x20) << 2) | (packet[1] & 0x7f));
572         y = (s8)(((packet[0] & 0x10) << 3) | (packet[2] & 0x7f));
573         z = (packet[4] & 0x7c) >> 2;
574
575         /*
576          * The x and y values tend to be quite large, and when used
577          * alone the trackstick is difficult to use. Scale them down
578          * to compensate.
579          */
580         x /= 8;
581         y /= 8;
582
583         input_report_rel(dev, REL_X, x);
584         input_report_rel(dev, REL_Y, -y);
585
586         /*
587          * Most ALPS models report the trackstick buttons in the touchpad
588          * packets, but a few report them here. No reliable way has been
589          * found to differentiate between the models upfront, so we enable
590          * the quirk in response to seeing a button press in the trackstick
591          * packet.
592          */
593         left = packet[3] & 0x01;
594         right = packet[3] & 0x02;
595         middle = packet[3] & 0x04;
596
597         if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) &&
598             (left || right || middle))
599                 priv->quirks |= ALPS_QUIRK_TRACKSTICK_BUTTONS;
600
601         if (priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) {
602                 input_report_key(dev, BTN_LEFT, left);
603                 input_report_key(dev, BTN_RIGHT, right);
604                 input_report_key(dev, BTN_MIDDLE, middle);
605         }
606
607         input_sync(dev);
608         return;
609 }
610
611 static void alps_decode_buttons_v3(struct alps_fields *f, unsigned char *p)
612 {
613         f->left = !!(p[3] & 0x01);
614         f->right = !!(p[3] & 0x02);
615         f->middle = !!(p[3] & 0x04);
616
617         f->ts_left = !!(p[3] & 0x10);
618         f->ts_right = !!(p[3] & 0x20);
619         f->ts_middle = !!(p[3] & 0x40);
620 }
621
622 static int alps_decode_pinnacle(struct alps_fields *f, unsigned char *p,
623                                  struct psmouse *psmouse)
624 {
625         f->first_mp = !!(p[4] & 0x40);
626         f->is_mp = !!(p[0] & 0x40);
627
628         if (f->is_mp) {
629                 f->fingers = (p[5] & 0x3) + 1;
630                 f->x_map = ((p[4] & 0x7e) << 8) |
631                            ((p[1] & 0x7f) << 2) |
632                            ((p[0] & 0x30) >> 4);
633                 f->y_map = ((p[3] & 0x70) << 4) |
634                            ((p[2] & 0x7f) << 1) |
635                            (p[4] & 0x01);
636         } else {
637                 f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
638                        ((p[0] & 0x30) >> 4);
639                 f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
640                 f->pressure = p[5] & 0x7f;
641
642                 alps_decode_buttons_v3(f, p);
643         }
644
645         return 0;
646 }
647
648 static int alps_decode_rushmore(struct alps_fields *f, unsigned char *p,
649                                  struct psmouse *psmouse)
650 {
651         f->first_mp = !!(p[4] & 0x40);
652         f->is_mp = !!(p[5] & 0x40);
653
654         if (f->is_mp) {
655                 f->fingers = max((p[5] & 0x3), ((p[5] >> 2) & 0x3)) + 1;
656                 f->x_map = ((p[5] & 0x10) << 11) |
657                            ((p[4] & 0x7e) << 8) |
658                            ((p[1] & 0x7f) << 2) |
659                            ((p[0] & 0x30) >> 4);
660                 f->y_map = ((p[5] & 0x20) << 6) |
661                            ((p[3] & 0x70) << 4) |
662                            ((p[2] & 0x7f) << 1) |
663                            (p[4] & 0x01);
664         } else {
665                 f->st.x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
666                        ((p[0] & 0x30) >> 4);
667                 f->st.y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
668                 f->pressure = p[5] & 0x7f;
669
670                 alps_decode_buttons_v3(f, p);
671         }
672
673         return 0;
674 }
675
676 static int alps_decode_dolphin(struct alps_fields *f, unsigned char *p,
677                                 struct psmouse *psmouse)
678 {
679         u64 palm_data = 0;
680         struct alps_data *priv = psmouse->private;
681
682         f->first_mp = !!(p[0] & 0x02);
683         f->is_mp = !!(p[0] & 0x20);
684
685         if (!f->is_mp) {
686                 f->st.x = ((p[1] & 0x7f) | ((p[4] & 0x0f) << 7));
687                 f->st.y = ((p[2] & 0x7f) | ((p[4] & 0xf0) << 3));
688                 f->pressure = (p[0] & 4) ? 0 : p[5] & 0x7f;
689                 alps_decode_buttons_v3(f, p);
690         } else {
691                 f->fingers = ((p[0] & 0x6) >> 1 |
692                      (p[0] & 0x10) >> 2);
693
694                 palm_data = (p[1] & 0x7f) |
695                             ((p[2] & 0x7f) << 7) |
696                             ((p[4] & 0x7f) << 14) |
697                             ((p[5] & 0x7f) << 21) |
698                             ((p[3] & 0x07) << 28) |
699                             (((u64)p[3] & 0x70) << 27) |
700                             (((u64)p[0] & 0x01) << 34);
701
702                 /* Y-profile is stored in P(0) to p(n-1), n = y_bits; */
703                 f->y_map = palm_data & (BIT(priv->y_bits) - 1);
704
705                 /* X-profile is stored in p(n) to p(n+m-1), m = x_bits; */
706                 f->x_map = (palm_data >> priv->y_bits) &
707                            (BIT(priv->x_bits) - 1);
708         }
709
710         return 0;
711 }
712
713 static void alps_process_touchpad_packet_v3_v5(struct psmouse *psmouse)
714 {
715         struct alps_data *priv = psmouse->private;
716         unsigned char *packet = psmouse->packet;
717         struct input_dev *dev2 = priv->dev2;
718         struct alps_fields *f = &priv->f;
719         int fingers = 0;
720
721         memset(f, 0, sizeof(*f));
722
723         priv->decode_fields(f, packet, psmouse);
724
725         /*
726          * There's no single feature of touchpad position and bitmap packets
727          * that can be used to distinguish between them. We rely on the fact
728          * that a bitmap packet should always follow a position packet with
729          * bit 6 of packet[4] set.
730          */
731         if (priv->multi_packet) {
732                 /*
733                  * Sometimes a position packet will indicate a multi-packet
734                  * sequence, but then what follows is another position
735                  * packet. Check for this, and when it happens process the
736                  * position packet as usual.
737                  */
738                 if (f->is_mp) {
739                         fingers = f->fingers;
740                         /*
741                          * Bitmap processing uses position packet's coordinate
742                          * data, so we need to do decode it first.
743                          */
744                         priv->decode_fields(f, priv->multi_data, psmouse);
745
746                         if (priv->proto_version == ALPS_PROTO_V3 ||
747                             priv->proto_version == ALPS_PROTO_V3_RUSHMORE) {
748                                 if (alps_process_bitmap(priv, f) == 0)
749                                         fingers = 0; /* Use st data */
750                         } else {
751                                 /*
752                                  * Since Dolphin's finger number is reliable,
753                                  * there is no need to compare with bmap_fn.
754                                  */
755                                 alps_process_bitmap_dolphin(priv, f);
756                         }
757                 } else {
758                         priv->multi_packet = 0;
759                 }
760         }
761
762         /*
763          * Bit 6 of byte 0 is not usually set in position packets. The only
764          * times it seems to be set is in situations where the data is
765          * suspect anyway, e.g. a palm resting flat on the touchpad. Given
766          * this combined with the fact that this bit is useful for filtering
767          * out misidentified bitmap packets, we reject anything with this
768          * bit set.
769          */
770         if (f->is_mp)
771                 return;
772
773         if (!priv->multi_packet && f->first_mp) {
774                 priv->multi_packet = 1;
775                 memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
776                 return;
777         }
778
779         priv->multi_packet = 0;
780
781         /*
782          * Sometimes the hardware sends a single packet with z = 0
783          * in the middle of a stream. Real releases generate packets
784          * with x, y, and z all zero, so these seem to be flukes.
785          * Ignore them.
786          */
787         if (f->st.x && f->st.y && !f->pressure)
788                 return;
789
790         alps_report_semi_mt_data(psmouse, fingers);
791
792         if ((priv->flags & ALPS_DUALPOINT) &&
793             !(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) {
794                 input_report_key(dev2, BTN_LEFT, f->ts_left);
795                 input_report_key(dev2, BTN_RIGHT, f->ts_right);
796                 input_report_key(dev2, BTN_MIDDLE, f->ts_middle);
797                 input_sync(dev2);
798         }
799 }
800
801 static void alps_process_packet_v3(struct psmouse *psmouse)
802 {
803         unsigned char *packet = psmouse->packet;
804
805         /*
806          * v3 protocol packets come in three types, two representing
807          * touchpad data and one representing trackstick data.
808          * Trackstick packets seem to be distinguished by always
809          * having 0x3f in the last byte. This value has never been
810          * observed in the last byte of either of the other types
811          * of packets.
812          */
813         if (packet[5] == 0x3f) {
814                 alps_process_trackstick_packet_v3(psmouse);
815                 return;
816         }
817
818         alps_process_touchpad_packet_v3_v5(psmouse);
819 }
820
821 static void alps_process_packet_v6(struct psmouse *psmouse)
822 {
823         struct alps_data *priv = psmouse->private;
824         unsigned char *packet = psmouse->packet;
825         struct input_dev *dev = psmouse->dev;
826         struct input_dev *dev2 = priv->dev2;
827         int x, y, z, left, right, middle;
828
829         /*
830          * We can use Byte5 to distinguish if the packet is from Touchpad
831          * or Trackpoint.
832          * Touchpad:    0 - 0x7E
833          * Trackpoint:  0x7F
834          */
835         if (packet[5] == 0x7F) {
836                 /* It should be a DualPoint when received Trackpoint packet */
837                 if (!(priv->flags & ALPS_DUALPOINT)) {
838                         psmouse_warn(psmouse,
839                                      "Rejected trackstick packet from non DualPoint device");
840                         return;
841                 }
842
843                 /* Trackpoint packet */
844                 x = packet[1] | ((packet[3] & 0x20) << 2);
845                 y = packet[2] | ((packet[3] & 0x40) << 1);
846                 z = packet[4];
847                 left = packet[3] & 0x01;
848                 right = packet[3] & 0x02;
849                 middle = packet[3] & 0x04;
850
851                 /* To prevent the cursor jump when finger lifted */
852                 if (x == 0x7F && y == 0x7F && z == 0x7F)
853                         x = y = z = 0;
854
855                 /* Divide 4 since trackpoint's speed is too fast */
856                 input_report_rel(dev2, REL_X, (char)x / 4);
857                 input_report_rel(dev2, REL_Y, -((char)y / 4));
858
859                 input_report_key(dev2, BTN_LEFT, left);
860                 input_report_key(dev2, BTN_RIGHT, right);
861                 input_report_key(dev2, BTN_MIDDLE, middle);
862
863                 input_sync(dev2);
864                 return;
865         }
866
867         /* Touchpad packet */
868         x = packet[1] | ((packet[3] & 0x78) << 4);
869         y = packet[2] | ((packet[4] & 0x78) << 4);
870         z = packet[5];
871         left = packet[3] & 0x01;
872         right = packet[3] & 0x02;
873
874         if (z > 30)
875                 input_report_key(dev, BTN_TOUCH, 1);
876         if (z < 25)
877                 input_report_key(dev, BTN_TOUCH, 0);
878
879         if (z > 0) {
880                 input_report_abs(dev, ABS_X, x);
881                 input_report_abs(dev, ABS_Y, y);
882         }
883
884         input_report_abs(dev, ABS_PRESSURE, z);
885         input_report_key(dev, BTN_TOOL_FINGER, z > 0);
886
887         /* v6 touchpad does not have middle button */
888         input_report_key(dev, BTN_LEFT, left);
889         input_report_key(dev, BTN_RIGHT, right);
890
891         input_sync(dev);
892 }
893
894 static void alps_process_packet_v4(struct psmouse *psmouse)
895 {
896         struct alps_data *priv = psmouse->private;
897         unsigned char *packet = psmouse->packet;
898         struct alps_fields *f = &priv->f;
899         int offset;
900
901         /*
902          * v4 has a 6-byte encoding for bitmap data, but this data is
903          * broken up between 3 normal packets. Use priv->multi_packet to
904          * track our position in the bitmap packet.
905          */
906         if (packet[6] & 0x40) {
907                 /* sync, reset position */
908                 priv->multi_packet = 0;
909         }
910
911         if (WARN_ON_ONCE(priv->multi_packet > 2))
912                 return;
913
914         offset = 2 * priv->multi_packet;
915         priv->multi_data[offset] = packet[6];
916         priv->multi_data[offset + 1] = packet[7];
917
918         f->left = !!(packet[4] & 0x01);
919         f->right = !!(packet[4] & 0x02);
920
921         f->st.x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) |
922                   ((packet[0] & 0x30) >> 4);
923         f->st.y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f);
924         f->pressure = packet[5] & 0x7f;
925
926         if (++priv->multi_packet > 2) {
927                 priv->multi_packet = 0;
928
929                 f->x_map = ((priv->multi_data[2] & 0x1f) << 10) |
930                            ((priv->multi_data[3] & 0x60) << 3) |
931                            ((priv->multi_data[0] & 0x3f) << 2) |
932                            ((priv->multi_data[1] & 0x60) >> 5);
933                 f->y_map = ((priv->multi_data[5] & 0x01) << 10) |
934                            ((priv->multi_data[3] & 0x1f) << 5) |
935                             (priv->multi_data[1] & 0x1f);
936
937                 f->fingers = alps_process_bitmap(priv, f);
938         }
939
940         alps_report_semi_mt_data(psmouse, f->fingers);
941 }
942
943 static bool alps_is_valid_package_v7(struct psmouse *psmouse)
944 {
945         switch (psmouse->pktcnt) {
946         case 3:
947                 return (psmouse->packet[2] & 0x40) == 0x40;
948         case 4:
949                 return (psmouse->packet[3] & 0x48) == 0x48;
950         case 6:
951                 return (psmouse->packet[5] & 0x40) == 0x00;
952         }
953         return true;
954 }
955
956 static unsigned char alps_get_packet_id_v7(char *byte)
957 {
958         unsigned char packet_id;
959
960         if (byte[4] & 0x40)
961                 packet_id = V7_PACKET_ID_TWO;
962         else if (byte[4] & 0x01)
963                 packet_id = V7_PACKET_ID_MULTI;
964         else if ((byte[0] & 0x10) && !(byte[4] & 0x43))
965                 packet_id = V7_PACKET_ID_NEW;
966         else if (byte[1] == 0x00 && byte[4] == 0x00)
967                 packet_id = V7_PACKET_ID_IDLE;
968         else
969                 packet_id = V7_PACKET_ID_UNKNOWN;
970
971         return packet_id;
972 }
973
974 static void alps_get_finger_coordinate_v7(struct input_mt_pos *mt,
975                                           unsigned char *pkt,
976                                           unsigned char pkt_id)
977 {
978         mt[0].x = ((pkt[2] & 0x80) << 4);
979         mt[0].x |= ((pkt[2] & 0x3F) << 5);
980         mt[0].x |= ((pkt[3] & 0x30) >> 1);
981         mt[0].x |= (pkt[3] & 0x07);
982         mt[0].y = (pkt[1] << 3) | (pkt[0] & 0x07);
983
984         mt[1].x = ((pkt[3] & 0x80) << 4);
985         mt[1].x |= ((pkt[4] & 0x80) << 3);
986         mt[1].x |= ((pkt[4] & 0x3F) << 4);
987         mt[1].y = ((pkt[5] & 0x80) << 3);
988         mt[1].y |= ((pkt[5] & 0x3F) << 4);
989
990         switch (pkt_id) {
991         case V7_PACKET_ID_TWO:
992                 mt[1].x &= ~0x000F;
993                 mt[1].y |= 0x000F;
994                 break;
995
996         case V7_PACKET_ID_MULTI:
997                 mt[1].x &= ~0x003F;
998                 mt[1].y &= ~0x0020;
999                 mt[1].y |= ((pkt[4] & 0x02) << 4);
1000                 mt[1].y |= 0x001F;
1001                 break;
1002
1003         case V7_PACKET_ID_NEW:
1004                 mt[1].x &= ~0x003F;
1005                 mt[1].x |= (pkt[0] & 0x20);
1006                 mt[1].y |= 0x000F;
1007                 break;
1008         }
1009
1010         mt[0].y = 0x7FF - mt[0].y;
1011         mt[1].y = 0x7FF - mt[1].y;
1012 }
1013
1014 static int alps_get_mt_count(struct input_mt_pos *mt)
1015 {
1016         int i, fingers = 0;
1017
1018         for (i = 0; i < MAX_TOUCHES; i++) {
1019                 if (mt[i].x != 0 || mt[i].y != 0)
1020                         fingers++;
1021         }
1022
1023         return fingers;
1024 }
1025
1026 static int alps_decode_packet_v7(struct alps_fields *f,
1027                                   unsigned char *p,
1028                                   struct psmouse *psmouse)
1029 {
1030         struct alps_data *priv = psmouse->private;
1031         unsigned char pkt_id;
1032
1033         pkt_id = alps_get_packet_id_v7(p);
1034         if (pkt_id == V7_PACKET_ID_IDLE)
1035                 return 0;
1036         if (pkt_id == V7_PACKET_ID_UNKNOWN)
1037                 return -1;
1038         /*
1039          * NEW packets are send to indicate a discontinuity in the finger
1040          * coordinate reporting. Specifically a finger may have moved from
1041          * slot 0 to 1 or vice versa. INPUT_MT_TRACK takes care of this for
1042          * us.
1043          *
1044          * NEW packets have 3 problems:
1045          * 1) They do not contain middle / right button info (on non clickpads)
1046          *    this can be worked around by preserving the old button state
1047          * 2) They do not contain an accurate fingercount, and they are
1048          *    typically send when the number of fingers changes. We cannot use
1049          *    the old finger count as that may mismatch with the amount of
1050          *    touch coordinates we've available in the NEW packet
1051          * 3) Their x data for the second touch is inaccurate leading to
1052          *    a possible jump of the x coordinate by 16 units when the first
1053          *    non NEW packet comes in
1054          * Since problems 2 & 3 cannot be worked around, just ignore them.
1055          */
1056         if (pkt_id == V7_PACKET_ID_NEW)
1057                 return 1;
1058
1059         alps_get_finger_coordinate_v7(f->mt, p, pkt_id);
1060
1061         if (pkt_id == V7_PACKET_ID_TWO)
1062                 f->fingers = alps_get_mt_count(f->mt);
1063         else /* pkt_id == V7_PACKET_ID_MULTI */
1064                 f->fingers = 3 + (p[5] & 0x03);
1065
1066         f->left = (p[0] & 0x80) >> 7;
1067         if (priv->flags & ALPS_BUTTONPAD) {
1068                 if (p[0] & 0x20)
1069                         f->fingers++;
1070                 if (p[0] & 0x10)
1071                         f->fingers++;
1072         } else {
1073                 f->right = (p[0] & 0x20) >> 5;
1074                 f->middle = (p[0] & 0x10) >> 4;
1075         }
1076
1077         /* Sometimes a single touch is reported in mt[1] rather then mt[0] */
1078         if (f->fingers == 1 && f->mt[0].x == 0 && f->mt[0].y == 0) {
1079                 f->mt[0].x = f->mt[1].x;
1080                 f->mt[0].y = f->mt[1].y;
1081                 f->mt[1].x = 0;
1082                 f->mt[1].y = 0;
1083         }
1084
1085         return 0;
1086 }
1087
1088 static void alps_process_trackstick_packet_v7(struct psmouse *psmouse)
1089 {
1090         struct alps_data *priv = psmouse->private;
1091         unsigned char *packet = psmouse->packet;
1092         struct input_dev *dev2 = priv->dev2;
1093         int x, y, z, left, right, middle;
1094
1095         /* It should be a DualPoint when received trackstick packet */
1096         if (!(priv->flags & ALPS_DUALPOINT)) {
1097                 psmouse_warn(psmouse,
1098                              "Rejected trackstick packet from non DualPoint device");
1099                 return;
1100         }
1101
1102         x = ((packet[2] & 0xbf)) | ((packet[3] & 0x10) << 2);
1103         y = (packet[3] & 0x07) | (packet[4] & 0xb8) |
1104             ((packet[3] & 0x20) << 1);
1105         z = (packet[5] & 0x3f) | ((packet[3] & 0x80) >> 1);
1106
1107         left = (packet[1] & 0x01);
1108         right = (packet[1] & 0x02) >> 1;
1109         middle = (packet[1] & 0x04) >> 2;
1110
1111         /* Divide 2 since trackpoint's speed is too fast */
1112         input_report_rel(dev2, REL_X, (char)x / 2);
1113         input_report_rel(dev2, REL_Y, -((char)y / 2));
1114
1115         input_report_key(dev2, BTN_LEFT, left);
1116         input_report_key(dev2, BTN_RIGHT, right);
1117         input_report_key(dev2, BTN_MIDDLE, middle);
1118
1119         input_sync(dev2);
1120 }
1121
1122 static void alps_process_touchpad_packet_v7(struct psmouse *psmouse)
1123 {
1124         struct alps_data *priv = psmouse->private;
1125         struct input_dev *dev = psmouse->dev;
1126         struct alps_fields *f = &priv->f;
1127
1128         memset(f, 0, sizeof(*f));
1129
1130         if (priv->decode_fields(f, psmouse->packet, psmouse))
1131                 return;
1132
1133         alps_report_mt_data(psmouse, alps_get_mt_count(f->mt));
1134
1135         input_mt_report_finger_count(dev, f->fingers);
1136
1137         input_report_key(dev, BTN_LEFT, f->left);
1138         input_report_key(dev, BTN_RIGHT, f->right);
1139         input_report_key(dev, BTN_MIDDLE, f->middle);
1140
1141         input_sync(dev);
1142 }
1143
1144 static void alps_process_packet_v7(struct psmouse *psmouse)
1145 {
1146         unsigned char *packet = psmouse->packet;
1147
1148         if (packet[0] == 0x48 && (packet[4] & 0x47) == 0x06)
1149                 alps_process_trackstick_packet_v7(psmouse);
1150         else
1151                 alps_process_touchpad_packet_v7(psmouse);
1152 }
1153
1154 static unsigned char alps_get_pkt_id_ss4_v2(unsigned char *byte)
1155 {
1156         unsigned char pkt_id = SS4_PACKET_ID_IDLE;
1157
1158         if (byte[0] == 0x18 && byte[1] == 0x10 && byte[2] == 0x00 &&
1159             (byte[3] & 0x88) == 0x08 && byte[4] == 0x10 && byte[5] == 0x00) {
1160                 pkt_id = SS4_PACKET_ID_IDLE;
1161         } else if (!(byte[3] & 0x10)) {
1162                 pkt_id = SS4_PACKET_ID_ONE;
1163         } else if (!(byte[3] & 0x20)) {
1164                 pkt_id = SS4_PACKET_ID_TWO;
1165         } else {
1166                 pkt_id = SS4_PACKET_ID_MULTI;
1167         }
1168
1169         return pkt_id;
1170 }
1171
1172 static int alps_decode_ss4_v2(struct alps_fields *f,
1173                               unsigned char *p, struct psmouse *psmouse)
1174 {
1175         struct alps_data *priv = psmouse->private;
1176         unsigned char pkt_id;
1177         unsigned int no_data_x, no_data_y;
1178
1179         pkt_id = alps_get_pkt_id_ss4_v2(p);
1180
1181         /* Current packet is 1Finger coordinate packet */
1182         switch (pkt_id) {
1183         case SS4_PACKET_ID_ONE:
1184                 f->mt[0].x = SS4_1F_X_V2(p);
1185                 f->mt[0].y = SS4_1F_Y_V2(p);
1186                 f->pressure = ((SS4_1F_Z_V2(p)) * 2) & 0x7f;
1187                 f->fingers = 1;
1188                 f->first_mp = 0;
1189                 f->is_mp = 0;
1190                 break;
1191
1192         case SS4_PACKET_ID_TWO:
1193                 if (priv->flags & ALPS_BUTTONPAD) {
1194                         f->mt[0].x = SS4_BTL_MF_X_V2(p, 0);
1195                         f->mt[0].y = SS4_BTL_MF_Y_V2(p, 0);
1196                         f->mt[1].x = SS4_BTL_MF_X_V2(p, 1);
1197                         f->mt[1].y = SS4_BTL_MF_Y_V2(p, 1);
1198                 } else {
1199                         f->mt[0].x = SS4_STD_MF_X_V2(p, 0);
1200                         f->mt[0].y = SS4_STD_MF_Y_V2(p, 0);
1201                         f->mt[1].x = SS4_STD_MF_X_V2(p, 1);
1202                         f->mt[1].y = SS4_STD_MF_Y_V2(p, 1);
1203                 }
1204                 f->pressure = SS4_MF_Z_V2(p, 0) ? 0x30 : 0;
1205
1206                 if (SS4_IS_MF_CONTINUE(p)) {
1207                         f->first_mp = 1;
1208                 } else {
1209                         f->fingers = 2;
1210                         f->first_mp = 0;
1211                 }
1212                 f->is_mp = 0;
1213
1214                 break;
1215
1216         case SS4_PACKET_ID_MULTI:
1217                 if (priv->flags & ALPS_BUTTONPAD) {
1218                         f->mt[2].x = SS4_BTL_MF_X_V2(p, 0);
1219                         f->mt[2].y = SS4_BTL_MF_Y_V2(p, 0);
1220                         f->mt[3].x = SS4_BTL_MF_X_V2(p, 1);
1221                         f->mt[3].y = SS4_BTL_MF_Y_V2(p, 1);
1222                         no_data_x = SS4_MFPACKET_NO_AX_BL;
1223                         no_data_y = SS4_MFPACKET_NO_AY_BL;
1224                 } else {
1225                         f->mt[2].x = SS4_STD_MF_X_V2(p, 0);
1226                         f->mt[2].y = SS4_STD_MF_Y_V2(p, 0);
1227                         f->mt[3].x = SS4_STD_MF_X_V2(p, 1);
1228                         f->mt[3].y = SS4_STD_MF_Y_V2(p, 1);
1229                         no_data_x = SS4_MFPACKET_NO_AX;
1230                         no_data_y = SS4_MFPACKET_NO_AY;
1231                 }
1232
1233                 f->first_mp = 0;
1234                 f->is_mp = 1;
1235
1236                 if (SS4_IS_5F_DETECTED(p)) {
1237                         f->fingers = 5;
1238                 } else if (f->mt[3].x == no_data_x &&
1239                              f->mt[3].y == no_data_y) {
1240                         f->mt[3].x = 0;
1241                         f->mt[3].y = 0;
1242                         f->fingers = 3;
1243                 } else {
1244                         f->fingers = 4;
1245                 }
1246                 break;
1247
1248         case SS4_PACKET_ID_IDLE:
1249         default:
1250                 memset(f, 0, sizeof(struct alps_fields));
1251                 break;
1252         }
1253
1254         f->left = !!(SS4_BTN_V2(p) & 0x01);
1255         if (!(priv->flags & ALPS_BUTTONPAD)) {
1256                 f->right = !!(SS4_BTN_V2(p) & 0x02);
1257                 f->middle = !!(SS4_BTN_V2(p) & 0x04);
1258         }
1259
1260         return 0;
1261 }
1262
1263 static void alps_process_packet_ss4_v2(struct psmouse *psmouse)
1264 {
1265         struct alps_data *priv = psmouse->private;
1266         unsigned char *packet = psmouse->packet;
1267         struct input_dev *dev = psmouse->dev;
1268         struct alps_fields *f = &priv->f;
1269
1270         memset(f, 0, sizeof(struct alps_fields));
1271         priv->decode_fields(f, packet, psmouse);
1272         if (priv->multi_packet) {
1273                 /*
1274                  * Sometimes the first packet will indicate a multi-packet
1275                  * sequence, but sometimes the next multi-packet would not
1276                  * come. Check for this, and when it happens process the
1277                  * position packet as usual.
1278                  */
1279                 if (f->is_mp) {
1280                         /* Now process the 1st packet */
1281                         priv->decode_fields(f, priv->multi_data, psmouse);
1282                 } else {
1283                         priv->multi_packet = 0;
1284                 }
1285         }
1286
1287         /*
1288          * "f.is_mp" would always be '0' after merging the 1st and 2nd packet.
1289          * When it is set, it means 2nd packet comes without 1st packet come.
1290          */
1291         if (f->is_mp)
1292                 return;
1293
1294         /* Save the first packet */
1295         if (!priv->multi_packet && f->first_mp) {
1296                 priv->multi_packet = 1;
1297                 memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
1298                 return;
1299         }
1300
1301         priv->multi_packet = 0;
1302
1303         alps_report_mt_data(psmouse, (f->fingers <= 4) ? f->fingers : 4);
1304
1305         input_mt_report_finger_count(dev, f->fingers);
1306
1307         input_report_key(dev, BTN_LEFT, f->left);
1308         input_report_key(dev, BTN_RIGHT, f->right);
1309         input_report_key(dev, BTN_MIDDLE, f->middle);
1310
1311         input_report_abs(dev, ABS_PRESSURE, f->pressure);
1312         input_sync(dev);
1313 }
1314
1315 static bool alps_is_valid_package_ss4_v2(struct psmouse *psmouse)
1316 {
1317         if (psmouse->pktcnt == 4 && ((psmouse->packet[3] & 0x08) != 0x08))
1318                 return false;
1319         if (psmouse->pktcnt == 6 && ((psmouse->packet[5] & 0x10) != 0x0))
1320                 return false;
1321         return true;
1322 }
1323
1324 static DEFINE_MUTEX(alps_mutex);
1325
1326 static void alps_register_bare_ps2_mouse(struct work_struct *work)
1327 {
1328         struct alps_data *priv =
1329                 container_of(work, struct alps_data, dev3_register_work.work);
1330         struct psmouse *psmouse = priv->psmouse;
1331         struct input_dev *dev3;
1332         int error = 0;
1333
1334         mutex_lock(&alps_mutex);
1335
1336         if (priv->dev3)
1337                 goto out;
1338
1339         dev3 = input_allocate_device();
1340         if (!dev3) {
1341                 psmouse_err(psmouse, "failed to allocate secondary device\n");
1342                 error = -ENOMEM;
1343                 goto out;
1344         }
1345
1346         snprintf(priv->phys3, sizeof(priv->phys3), "%s/%s",
1347                  psmouse->ps2dev.serio->phys,
1348                  (priv->dev2 ? "input2" : "input1"));
1349         dev3->phys = priv->phys3;
1350
1351         /*
1352          * format of input device name is: "protocol vendor name"
1353          * see function psmouse_switch_protocol() in psmouse-base.c
1354          */
1355         dev3->name = "PS/2 ALPS Mouse";
1356
1357         dev3->id.bustype = BUS_I8042;
1358         dev3->id.vendor  = 0x0002;
1359         dev3->id.product = PSMOUSE_PS2;
1360         dev3->id.version = 0x0000;
1361         dev3->dev.parent = &psmouse->ps2dev.serio->dev;
1362
1363         input_set_capability(dev3, EV_REL, REL_X);
1364         input_set_capability(dev3, EV_REL, REL_Y);
1365         input_set_capability(dev3, EV_KEY, BTN_LEFT);
1366         input_set_capability(dev3, EV_KEY, BTN_RIGHT);
1367         input_set_capability(dev3, EV_KEY, BTN_MIDDLE);
1368
1369         __set_bit(INPUT_PROP_POINTER, dev3->propbit);
1370
1371         error = input_register_device(dev3);
1372         if (error) {
1373                 psmouse_err(psmouse,
1374                             "failed to register secondary device: %d\n",
1375                             error);
1376                 input_free_device(dev3);
1377                 goto out;
1378         }
1379
1380         priv->dev3 = dev3;
1381
1382 out:
1383         /*
1384          * Save the error code so that we can detect that we
1385          * already tried to create the device.
1386          */
1387         if (error)
1388                 priv->dev3 = ERR_PTR(error);
1389
1390         mutex_unlock(&alps_mutex);
1391 }
1392
1393 static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
1394                                         unsigned char packet[],
1395                                         bool report_buttons)
1396 {
1397         struct alps_data *priv = psmouse->private;
1398         struct input_dev *dev;
1399
1400         /* Figure out which device to use to report the bare packet */
1401         if (priv->proto_version == ALPS_PROTO_V2 &&
1402             (priv->flags & ALPS_DUALPOINT)) {
1403                 /* On V2 devices the DualPoint Stick reports bare packets */
1404                 dev = priv->dev2;
1405         } else if (unlikely(IS_ERR_OR_NULL(priv->dev3))) {
1406                 /* Register dev3 mouse if we received PS/2 packet first time */
1407                 if (!IS_ERR(priv->dev3))
1408                         psmouse_queue_work(psmouse, &priv->dev3_register_work,
1409                                            0);
1410                 return;
1411         } else {
1412                 dev = priv->dev3;
1413         }
1414
1415         if (report_buttons)
1416                 alps_report_buttons(dev, NULL,
1417                                 packet[0] & 1, packet[0] & 2, packet[0] & 4);
1418
1419         input_report_rel(dev, REL_X,
1420                 packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
1421         input_report_rel(dev, REL_Y,
1422                 packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
1423
1424         input_sync(dev);
1425 }
1426
1427 static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
1428 {
1429         struct alps_data *priv = psmouse->private;
1430
1431         if (psmouse->pktcnt < 6)
1432                 return PSMOUSE_GOOD_DATA;
1433
1434         if (psmouse->pktcnt == 6) {
1435                 /*
1436                  * Start a timer to flush the packet if it ends up last
1437                  * 6-byte packet in the stream. Timer needs to fire
1438                  * psmouse core times out itself. 20 ms should be enough
1439                  * to decide if we are getting more data or not.
1440                  */
1441                 mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
1442                 return PSMOUSE_GOOD_DATA;
1443         }
1444
1445         del_timer(&priv->timer);
1446
1447         if (psmouse->packet[6] & 0x80) {
1448
1449                 /*
1450                  * Highest bit is set - that means we either had
1451                  * complete ALPS packet and this is start of the
1452                  * next packet or we got garbage.
1453                  */
1454
1455                 if (((psmouse->packet[3] |
1456                       psmouse->packet[4] |
1457                       psmouse->packet[5]) & 0x80) ||
1458                     (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) {
1459                         psmouse_dbg(psmouse,
1460                                     "refusing packet %4ph (suspected interleaved ps/2)\n",
1461                                     psmouse->packet + 3);
1462                         return PSMOUSE_BAD_DATA;
1463                 }
1464
1465                 priv->process_packet(psmouse);
1466
1467                 /* Continue with the next packet */
1468                 psmouse->packet[0] = psmouse->packet[6];
1469                 psmouse->pktcnt = 1;
1470
1471         } else {
1472
1473                 /*
1474                  * High bit is 0 - that means that we indeed got a PS/2
1475                  * packet in the middle of ALPS packet.
1476                  *
1477                  * There is also possibility that we got 6-byte ALPS
1478                  * packet followed  by 3-byte packet from trackpoint. We
1479                  * can not distinguish between these 2 scenarios but
1480                  * because the latter is unlikely to happen in course of
1481                  * normal operation (user would need to press all
1482                  * buttons on the pad and start moving trackpoint
1483                  * without touching the pad surface) we assume former.
1484                  * Even if we are wrong the wost thing that would happen
1485                  * the cursor would jump but we should not get protocol
1486                  * de-synchronization.
1487                  */
1488
1489                 alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
1490                                             false);
1491
1492                 /*
1493                  * Continue with the standard ALPS protocol handling,
1494                  * but make sure we won't process it as an interleaved
1495                  * packet again, which may happen if all buttons are
1496                  * pressed. To avoid this let's reset the 4th bit which
1497                  * is normally 1.
1498                  */
1499                 psmouse->packet[3] = psmouse->packet[6] & 0xf7;
1500                 psmouse->pktcnt = 4;
1501         }
1502
1503         return PSMOUSE_GOOD_DATA;
1504 }
1505
1506 static void alps_flush_packet(unsigned long data)
1507 {
1508         struct psmouse *psmouse = (struct psmouse *)data;
1509         struct alps_data *priv = psmouse->private;
1510
1511         serio_pause_rx(psmouse->ps2dev.serio);
1512
1513         if (psmouse->pktcnt == psmouse->pktsize) {
1514
1515                 /*
1516                  * We did not any more data in reasonable amount of time.
1517                  * Validate the last 3 bytes and process as a standard
1518                  * ALPS packet.
1519                  */
1520                 if ((psmouse->packet[3] |
1521                      psmouse->packet[4] |
1522                      psmouse->packet[5]) & 0x80) {
1523                         psmouse_dbg(psmouse,
1524                                     "refusing packet %3ph (suspected interleaved ps/2)\n",
1525                                     psmouse->packet + 3);
1526                 } else {
1527                         priv->process_packet(psmouse);
1528                 }
1529                 psmouse->pktcnt = 0;
1530         }
1531
1532         serio_continue_rx(psmouse->ps2dev.serio);
1533 }
1534
1535 static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
1536 {
1537         struct alps_data *priv = psmouse->private;
1538
1539         /*
1540          * Check if we are dealing with a bare PS/2 packet, presumably from
1541          * a device connected to the external PS/2 port. Because bare PS/2
1542          * protocol does not have enough constant bits to self-synchronize
1543          * properly we only do this if the device is fully synchronized.
1544          * Can not distinguish V8's first byte from PS/2 packet's
1545          */
1546         if (priv->proto_version != ALPS_PROTO_V8 &&
1547             !psmouse->out_of_sync_cnt &&
1548             (psmouse->packet[0] & 0xc8) == 0x08) {
1549
1550                 if (psmouse->pktcnt == 3) {
1551                         alps_report_bare_ps2_packet(psmouse, psmouse->packet,
1552                                                     true);
1553                         return PSMOUSE_FULL_PACKET;
1554                 }
1555                 return PSMOUSE_GOOD_DATA;
1556         }
1557
1558         /* Check for PS/2 packet stuffed in the middle of ALPS packet. */
1559
1560         if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
1561             psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
1562                 return alps_handle_interleaved_ps2(psmouse);
1563         }
1564
1565         if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
1566                 psmouse_dbg(psmouse,
1567                             "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
1568                             psmouse->packet[0], priv->mask0, priv->byte0);
1569                 return PSMOUSE_BAD_DATA;
1570         }
1571
1572         /* Bytes 2 - pktsize should have 0 in the highest bit */
1573         if (priv->proto_version < ALPS_PROTO_V5 &&
1574             psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
1575             (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
1576                 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1577                             psmouse->pktcnt - 1,
1578                             psmouse->packet[psmouse->pktcnt - 1]);
1579
1580                 if (priv->proto_version == ALPS_PROTO_V3_RUSHMORE &&
1581                     psmouse->pktcnt == psmouse->pktsize) {
1582                         /*
1583                          * Some Dell boxes, such as Latitude E6440 or E7440
1584                          * with closed lid, quite often smash last byte of
1585                          * otherwise valid packet with 0xff. Given that the
1586                          * next packet is very likely to be valid let's
1587                          * report PSMOUSE_FULL_PACKET but not process data,
1588                          * rather than reporting PSMOUSE_BAD_DATA and
1589                          * filling the logs.
1590                          */
1591                         return PSMOUSE_FULL_PACKET;
1592                 }
1593
1594                 return PSMOUSE_BAD_DATA;
1595         }
1596
1597         if ((priv->proto_version == ALPS_PROTO_V7 &&
1598                         !alps_is_valid_package_v7(psmouse)) ||
1599             (priv->proto_version == ALPS_PROTO_V8 &&
1600                         !alps_is_valid_package_ss4_v2(psmouse))) {
1601                 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1602                             psmouse->pktcnt - 1,
1603                             psmouse->packet[psmouse->pktcnt - 1]);
1604                 return PSMOUSE_BAD_DATA;
1605         }
1606
1607         if (psmouse->pktcnt == psmouse->pktsize) {
1608                 priv->process_packet(psmouse);
1609                 return PSMOUSE_FULL_PACKET;
1610         }
1611
1612         return PSMOUSE_GOOD_DATA;
1613 }
1614
1615 static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
1616 {
1617         struct ps2dev *ps2dev = &psmouse->ps2dev;
1618         struct alps_data *priv = psmouse->private;
1619         int command;
1620         unsigned char *param;
1621         unsigned char dummy[4];
1622
1623         BUG_ON(nibble > 0xf);
1624
1625         command = priv->nibble_commands[nibble].command;
1626         param = (command & 0x0f00) ?
1627                 dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
1628
1629         if (ps2_command(ps2dev, param, command))
1630                 return -1;
1631
1632         return 0;
1633 }
1634
1635 static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
1636 {
1637         struct ps2dev *ps2dev = &psmouse->ps2dev;
1638         struct alps_data *priv = psmouse->private;
1639         int i, nibble;
1640
1641         if (ps2_command(ps2dev, NULL, priv->addr_command))
1642                 return -1;
1643
1644         for (i = 12; i >= 0; i -= 4) {
1645                 nibble = (addr >> i) & 0xf;
1646                 if (alps_command_mode_send_nibble(psmouse, nibble))
1647                         return -1;
1648         }
1649
1650         return 0;
1651 }
1652
1653 static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1654 {
1655         struct ps2dev *ps2dev = &psmouse->ps2dev;
1656         unsigned char param[4];
1657
1658         if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1659                 return -1;
1660
1661         /*
1662          * The address being read is returned in the first two bytes
1663          * of the result. Check that this address matches the expected
1664          * address.
1665          */
1666         if (addr != ((param[0] << 8) | param[1]))
1667                 return -1;
1668
1669         return param[2];
1670 }
1671
1672 static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1673 {
1674         if (alps_command_mode_set_addr(psmouse, addr))
1675                 return -1;
1676         return __alps_command_mode_read_reg(psmouse, addr);
1677 }
1678
1679 static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
1680 {
1681         if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
1682                 return -1;
1683         if (alps_command_mode_send_nibble(psmouse, value & 0xf))
1684                 return -1;
1685         return 0;
1686 }
1687
1688 static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
1689                                        u8 value)
1690 {
1691         if (alps_command_mode_set_addr(psmouse, addr))
1692                 return -1;
1693         return __alps_command_mode_write_reg(psmouse, value);
1694 }
1695
1696 static int alps_rpt_cmd(struct psmouse *psmouse, int init_command,
1697                         int repeated_command, unsigned char *param)
1698 {
1699         struct ps2dev *ps2dev = &psmouse->ps2dev;
1700
1701         param[0] = 0;
1702         if (init_command && ps2_command(ps2dev, param, init_command))
1703                 return -EIO;
1704
1705         if (ps2_command(ps2dev,  NULL, repeated_command) ||
1706             ps2_command(ps2dev,  NULL, repeated_command) ||
1707             ps2_command(ps2dev,  NULL, repeated_command))
1708                 return -EIO;
1709
1710         param[0] = param[1] = param[2] = 0xff;
1711         if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1712                 return -EIO;
1713
1714         psmouse_dbg(psmouse, "%2.2X report: %3ph\n",
1715                     repeated_command, param);
1716         return 0;
1717 }
1718
1719 static bool alps_check_valid_firmware_id(unsigned char id[])
1720 {
1721         if (id[0] == 0x73)
1722                 return true;
1723
1724         if (id[0] == 0x88 &&
1725             (id[1] == 0x07 ||
1726              id[1] == 0x08 ||
1727              (id[1] & 0xf0) == 0xb0 ||
1728              (id[1] & 0xf0) == 0xc0)) {
1729                 return true;
1730         }
1731
1732         return false;
1733 }
1734
1735 static int alps_enter_command_mode(struct psmouse *psmouse)
1736 {
1737         unsigned char param[4];
1738
1739         if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) {
1740                 psmouse_err(psmouse, "failed to enter command mode\n");
1741                 return -1;
1742         }
1743
1744         if (!alps_check_valid_firmware_id(param)) {
1745                 psmouse_dbg(psmouse,
1746                             "unknown response while entering command mode\n");
1747                 return -1;
1748         }
1749         return 0;
1750 }
1751
1752 static inline int alps_exit_command_mode(struct psmouse *psmouse)
1753 {
1754         struct ps2dev *ps2dev = &psmouse->ps2dev;
1755         if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
1756                 return -1;
1757         return 0;
1758 }
1759
1760 /*
1761  * For DualPoint devices select the device that should respond to
1762  * subsequent commands. It looks like glidepad is behind stickpointer,
1763  * I'd thought it would be other way around...
1764  */
1765 static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
1766 {
1767         struct ps2dev *ps2dev = &psmouse->ps2dev;
1768         int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
1769
1770         if (ps2_command(ps2dev, NULL, cmd) ||
1771             ps2_command(ps2dev, NULL, cmd) ||
1772             ps2_command(ps2dev, NULL, cmd) ||
1773             ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1774                 return -1;
1775
1776         /* we may get 3 more bytes, just ignore them */
1777         ps2_drain(ps2dev, 3, 100);
1778
1779         return 0;
1780 }
1781
1782 static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
1783 {
1784         struct ps2dev *ps2dev = &psmouse->ps2dev;
1785
1786         /* Try ALPS magic knock - 4 disable before enable */
1787         if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1788             ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1789             ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1790             ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1791             ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
1792                 return -1;
1793
1794         /*
1795          * Switch mouse to poll (remote) mode so motion data will not
1796          * get in our way
1797          */
1798         return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
1799 }
1800
1801 static int alps_monitor_mode_send_word(struct psmouse *psmouse, u16 word)
1802 {
1803         int i, nibble;
1804
1805         /*
1806          * b0-b11 are valid bits, send sequence is inverse.
1807          * e.g. when word = 0x0123, nibble send sequence is 3, 2, 1
1808          */
1809         for (i = 0; i <= 8; i += 4) {
1810                 nibble = (word >> i) & 0xf;
1811                 if (alps_command_mode_send_nibble(psmouse, nibble))
1812                         return -1;
1813         }
1814
1815         return 0;
1816 }
1817
1818 static int alps_monitor_mode_write_reg(struct psmouse *psmouse,
1819                                        u16 addr, u16 value)
1820 {
1821         struct ps2dev *ps2dev = &psmouse->ps2dev;
1822
1823         /* 0x0A0 is the command to write the word */
1824         if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE) ||
1825             alps_monitor_mode_send_word(psmouse, 0x0A0) ||
1826             alps_monitor_mode_send_word(psmouse, addr) ||
1827             alps_monitor_mode_send_word(psmouse, value) ||
1828             ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1829                 return -1;
1830
1831         return 0;
1832 }
1833
1834 static int alps_monitor_mode(struct psmouse *psmouse, bool enable)
1835 {
1836         struct ps2dev *ps2dev = &psmouse->ps2dev;
1837
1838         if (enable) {
1839                 /* EC E9 F5 F5 E7 E6 E7 E9 to enter monitor mode */
1840                 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
1841                     ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO) ||
1842                     ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1843                     ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1844                     ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1845                     ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1846                     ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1847                     ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO))
1848                         return -1;
1849         } else {
1850                 /* EC to exit monitor mode */
1851                 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP))
1852                         return -1;
1853         }
1854
1855         return 0;
1856 }
1857
1858 static int alps_absolute_mode_v6(struct psmouse *psmouse)
1859 {
1860         u16 reg_val = 0x181;
1861         int ret = -1;
1862
1863         /* enter monitor mode, to write the register */
1864         if (alps_monitor_mode(psmouse, true))
1865                 return -1;
1866
1867         ret = alps_monitor_mode_write_reg(psmouse, 0x000, reg_val);
1868
1869         if (alps_monitor_mode(psmouse, false))
1870                 ret = -1;
1871
1872         return ret;
1873 }
1874
1875 static int alps_get_status(struct psmouse *psmouse, char *param)
1876 {
1877         /* Get status: 0xF5 0xF5 0xF5 0xE9 */
1878         if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param))
1879                 return -1;
1880
1881         return 0;
1882 }
1883
1884 /*
1885  * Turn touchpad tapping on or off. The sequences are:
1886  * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
1887  * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
1888  * My guess that 0xE9 (GetInfo) is here as a sync point.
1889  * For models that also have stickpointer (DualPoints) its tapping
1890  * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
1891  * we don't fiddle with it.
1892  */
1893 static int alps_tap_mode(struct psmouse *psmouse, int enable)
1894 {
1895         struct ps2dev *ps2dev = &psmouse->ps2dev;
1896         int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
1897         unsigned char tap_arg = enable ? 0x0A : 0x00;
1898         unsigned char param[4];
1899
1900         if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
1901             ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1902             ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1903             ps2_command(ps2dev, &tap_arg, cmd))
1904                 return -1;
1905
1906         if (alps_get_status(psmouse, param))
1907                 return -1;
1908
1909         return 0;
1910 }
1911
1912 /*
1913  * alps_poll() - poll the touchpad for current motion packet.
1914  * Used in resync.
1915  */
1916 static int alps_poll(struct psmouse *psmouse)
1917 {
1918         struct alps_data *priv = psmouse->private;
1919         unsigned char buf[sizeof(psmouse->packet)];
1920         bool poll_failed;
1921
1922         if (priv->flags & ALPS_PASS)
1923                 alps_passthrough_mode_v2(psmouse, true);
1924
1925         poll_failed = ps2_command(&psmouse->ps2dev, buf,
1926                                   PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
1927
1928         if (priv->flags & ALPS_PASS)
1929                 alps_passthrough_mode_v2(psmouse, false);
1930
1931         if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
1932                 return -1;
1933
1934         if ((psmouse->badbyte & 0xc8) == 0x08) {
1935 /*
1936  * Poll the track stick ...
1937  */
1938                 if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
1939                         return -1;
1940         }
1941
1942         memcpy(psmouse->packet, buf, sizeof(buf));
1943         return 0;
1944 }
1945
1946 static int alps_hw_init_v1_v2(struct psmouse *psmouse)
1947 {
1948         struct alps_data *priv = psmouse->private;
1949
1950         if ((priv->flags & ALPS_PASS) &&
1951             alps_passthrough_mode_v2(psmouse, true)) {
1952                 return -1;
1953         }
1954
1955         if (alps_tap_mode(psmouse, true)) {
1956                 psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
1957                 return -1;
1958         }
1959
1960         if (alps_absolute_mode_v1_v2(psmouse)) {
1961                 psmouse_err(psmouse, "Failed to enable absolute mode\n");
1962                 return -1;
1963         }
1964
1965         if ((priv->flags & ALPS_PASS) &&
1966             alps_passthrough_mode_v2(psmouse, false)) {
1967                 return -1;
1968         }
1969
1970         /* ALPS needs stream mode, otherwise it won't report any data */
1971         if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
1972                 psmouse_err(psmouse, "Failed to enable stream mode\n");
1973                 return -1;
1974         }
1975
1976         return 0;
1977 }
1978
1979 static int alps_hw_init_v6(struct psmouse *psmouse)
1980 {
1981         unsigned char param[2] = {0xC8, 0x14};
1982
1983         /* Enter passthrough mode to let trackpoint enter 6byte raw mode */
1984         if (alps_passthrough_mode_v2(psmouse, true))
1985                 return -1;
1986
1987         if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1988             ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1989             ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1990             ps2_command(&psmouse->ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
1991             ps2_command(&psmouse->ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
1992                 return -1;
1993
1994         if (alps_passthrough_mode_v2(psmouse, false))
1995                 return -1;
1996
1997         if (alps_absolute_mode_v6(psmouse)) {
1998                 psmouse_err(psmouse, "Failed to enable absolute mode\n");
1999                 return -1;
2000         }
2001
2002         return 0;
2003 }
2004
2005 /*
2006  * Enable or disable passthrough mode to the trackstick.
2007  */
2008 static int alps_passthrough_mode_v3(struct psmouse *psmouse,
2009                                     int reg_base, bool enable)
2010 {
2011         int reg_val, ret = -1;
2012
2013         if (alps_enter_command_mode(psmouse))
2014                 return -1;
2015
2016         reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x0008);
2017         if (reg_val == -1)
2018                 goto error;
2019
2020         if (enable)
2021                 reg_val |= 0x01;
2022         else
2023                 reg_val &= ~0x01;
2024
2025         ret = __alps_command_mode_write_reg(psmouse, reg_val);
2026
2027 error:
2028         if (alps_exit_command_mode(psmouse))
2029                 ret = -1;
2030         return ret;
2031 }
2032
2033 /* Must be in command mode when calling this function */
2034 static int alps_absolute_mode_v3(struct psmouse *psmouse)
2035 {
2036         int reg_val;
2037
2038         reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
2039         if (reg_val == -1)
2040                 return -1;
2041
2042         reg_val |= 0x06;
2043         if (__alps_command_mode_write_reg(psmouse, reg_val))
2044                 return -1;
2045
2046         return 0;
2047 }
2048
2049 static int alps_probe_trackstick_v3(struct psmouse *psmouse, int reg_base)
2050 {
2051         int ret = -EIO, reg_val;
2052
2053         if (alps_enter_command_mode(psmouse))
2054                 goto error;
2055
2056         reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x08);
2057         if (reg_val == -1)
2058                 goto error;
2059
2060         /* bit 7: trackstick is present */
2061         ret = reg_val & 0x80 ? 0 : -ENODEV;
2062
2063 error:
2064         alps_exit_command_mode(psmouse);
2065         return ret;
2066 }
2067
2068 static int alps_setup_trackstick_v3(struct psmouse *psmouse, int reg_base)
2069 {
2070         struct ps2dev *ps2dev = &psmouse->ps2dev;
2071         int ret = 0;
2072         unsigned char param[4];
2073
2074         if (alps_passthrough_mode_v3(psmouse, reg_base, true))
2075                 return -EIO;
2076
2077         /*
2078          * E7 report for the trackstick
2079          *
2080          * There have been reports of failures to seem to trace back
2081          * to the above trackstick check failing. When these occur
2082          * this E7 report fails, so when that happens we continue
2083          * with the assumption that there isn't a trackstick after
2084          * all.
2085          */
2086         if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_SETSCALE21, param)) {
2087                 psmouse_warn(psmouse, "Failed to initialize trackstick (E7 report failed)\n");
2088                 ret = -ENODEV;
2089         } else {
2090                 psmouse_dbg(psmouse, "trackstick E7 report: %3ph\n", param);
2091
2092                 /*
2093                  * Not sure what this does, but it is absolutely
2094                  * essential. Without it, the touchpad does not
2095                  * work at all and the trackstick just emits normal
2096                  * PS/2 packets.
2097                  */
2098                 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2099                     ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2100                     ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
2101                     alps_command_mode_send_nibble(psmouse, 0x9) ||
2102                     alps_command_mode_send_nibble(psmouse, 0x4)) {
2103                         psmouse_err(psmouse,
2104                                     "Error sending magic E6 sequence\n");
2105                         ret = -EIO;
2106                         goto error;
2107                 }
2108
2109                 /*
2110                  * This ensures the trackstick packets are in the format
2111                  * supported by this driver. If bit 1 isn't set the packet
2112                  * format is different.
2113                  */
2114                 if (alps_enter_command_mode(psmouse) ||
2115                     alps_command_mode_write_reg(psmouse,
2116                                                 reg_base + 0x08, 0x82) ||
2117                     alps_exit_command_mode(psmouse))
2118                         ret = -EIO;
2119         }
2120
2121 error:
2122         if (alps_passthrough_mode_v3(psmouse, reg_base, false))
2123                 ret = -EIO;
2124
2125         return ret;
2126 }
2127
2128 static int alps_hw_init_v3(struct psmouse *psmouse)
2129 {
2130         struct ps2dev *ps2dev = &psmouse->ps2dev;
2131         int reg_val;
2132         unsigned char param[4];
2133
2134         reg_val = alps_probe_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE);
2135         if (reg_val == -EIO)
2136                 goto error;
2137
2138         if (reg_val == 0 &&
2139             alps_setup_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE) == -EIO)
2140                 goto error;
2141
2142         if (alps_enter_command_mode(psmouse) ||
2143             alps_absolute_mode_v3(psmouse)) {
2144                 psmouse_err(psmouse, "Failed to enter absolute mode\n");
2145                 goto error;
2146         }
2147
2148         reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
2149         if (reg_val == -1)
2150                 goto error;
2151         if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
2152                 goto error;
2153
2154         reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
2155         if (reg_val == -1)
2156                 goto error;
2157         if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
2158                 goto error;
2159
2160         if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
2161                 goto error;
2162         if (__alps_command_mode_write_reg(psmouse, 0x04))
2163                 goto error;
2164
2165         if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
2166                 goto error;
2167         if (__alps_command_mode_write_reg(psmouse, 0x03))
2168                 goto error;
2169
2170         if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
2171                 goto error;
2172         if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
2173                 goto error;
2174
2175         if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
2176                 goto error;
2177         if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
2178                 goto error;
2179
2180         alps_exit_command_mode(psmouse);
2181
2182         /* Set rate and enable data reporting */
2183         param[0] = 0x64;
2184         if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
2185             ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
2186                 psmouse_err(psmouse, "Failed to enable data reporting\n");
2187                 return -1;
2188         }
2189
2190         return 0;
2191
2192 error:
2193         /*
2194          * Leaving the touchpad in command mode will essentially render
2195          * it unusable until the machine reboots, so exit it here just
2196          * to be safe
2197          */
2198         alps_exit_command_mode(psmouse);
2199         return -1;
2200 }
2201
2202 static int alps_get_v3_v7_resolution(struct psmouse *psmouse, int reg_pitch)
2203 {
2204         int reg, x_pitch, y_pitch, x_electrode, y_electrode, x_phys, y_phys;
2205         struct alps_data *priv = psmouse->private;
2206
2207         reg = alps_command_mode_read_reg(psmouse, reg_pitch);
2208         if (reg < 0)
2209                 return reg;
2210
2211         x_pitch = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
2212         x_pitch = 50 + 2 * x_pitch; /* In 0.1 mm units */
2213
2214         y_pitch = (char)reg >> 4; /* sign extend upper 4 bits */
2215         y_pitch = 36 + 2 * y_pitch; /* In 0.1 mm units */
2216
2217         reg = alps_command_mode_read_reg(psmouse, reg_pitch + 1);
2218         if (reg < 0)
2219                 return reg;
2220
2221         x_electrode = (char)(reg << 4) >> 4; /* sign extend lower 4 bits */
2222         x_electrode = 17 + x_electrode;
2223
2224         y_electrode = (char)reg >> 4; /* sign extend upper 4 bits */
2225         y_electrode = 13 + y_electrode;
2226
2227         x_phys = x_pitch * (x_electrode - 1); /* In 0.1 mm units */
2228         y_phys = y_pitch * (y_electrode - 1); /* In 0.1 mm units */
2229
2230         priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
2231         priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
2232
2233         psmouse_dbg(psmouse,
2234                     "pitch %dx%d num-electrodes %dx%d physical size %dx%d mm res %dx%d\n",
2235                     x_pitch, y_pitch, x_electrode, y_electrode,
2236                     x_phys / 10, y_phys / 10, priv->x_res, priv->y_res);
2237
2238         return 0;
2239 }
2240
2241 static int alps_hw_init_rushmore_v3(struct psmouse *psmouse)
2242 {
2243         struct alps_data *priv = psmouse->private;
2244         struct ps2dev *ps2dev = &psmouse->ps2dev;
2245         int reg_val, ret = -1;
2246
2247         if (priv->flags & ALPS_DUALPOINT) {
2248                 reg_val = alps_setup_trackstick_v3(psmouse,
2249                                                    ALPS_REG_BASE_RUSHMORE);
2250                 if (reg_val == -EIO)
2251                         goto error;
2252         }
2253
2254         if (alps_enter_command_mode(psmouse) ||
2255             alps_command_mode_read_reg(psmouse, 0xc2d9) == -1 ||
2256             alps_command_mode_write_reg(psmouse, 0xc2cb, 0x00))
2257                 goto error;
2258
2259         if (alps_get_v3_v7_resolution(psmouse, 0xc2da))
2260                 goto error;
2261
2262         reg_val = alps_command_mode_read_reg(psmouse, 0xc2c6);
2263         if (reg_val == -1)
2264                 goto error;
2265         if (__alps_command_mode_write_reg(psmouse, reg_val & 0xfd))
2266                 goto error;
2267
2268         if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
2269                 goto error;
2270
2271         /* enter absolute mode */
2272         reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
2273         if (reg_val == -1)
2274                 goto error;
2275         if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
2276                 goto error;
2277
2278         alps_exit_command_mode(psmouse);
2279         return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2280
2281 error:
2282         alps_exit_command_mode(psmouse);
2283         return ret;
2284 }
2285
2286 /* Must be in command mode when calling this function */
2287 static int alps_absolute_mode_v4(struct psmouse *psmouse)
2288 {
2289         int reg_val;
2290
2291         reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
2292         if (reg_val == -1)
2293                 return -1;
2294
2295         reg_val |= 0x02;
2296         if (__alps_command_mode_write_reg(psmouse, reg_val))
2297                 return -1;
2298
2299         return 0;
2300 }
2301
2302 static int alps_hw_init_v4(struct psmouse *psmouse)
2303 {
2304         struct ps2dev *ps2dev = &psmouse->ps2dev;
2305         unsigned char param[4];
2306
2307         if (alps_enter_command_mode(psmouse))
2308                 goto error;
2309
2310         if (alps_absolute_mode_v4(psmouse)) {
2311                 psmouse_err(psmouse, "Failed to enter absolute mode\n");
2312                 goto error;
2313         }
2314
2315         if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
2316                 goto error;
2317
2318         if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
2319                 goto error;
2320
2321         if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
2322                 goto error;
2323
2324         if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
2325                 goto error;
2326
2327         if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
2328                 goto error;
2329
2330         if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
2331                 goto error;
2332
2333         if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
2334                 goto error;
2335
2336         if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
2337                 goto error;
2338
2339         alps_exit_command_mode(psmouse);
2340
2341         /*
2342          * This sequence changes the output from a 9-byte to an
2343          * 8-byte format. All the same data seems to be present,
2344          * just in a more compact format.
2345          */
2346         param[0] = 0xc8;
2347         param[1] = 0x64;
2348         param[2] = 0x50;
2349         if (ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2350             ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE) ||
2351             ps2_command(ps2dev, &param[2], PSMOUSE_CMD_SETRATE) ||
2352             ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
2353                 return -1;
2354
2355         /* Set rate and enable data reporting */
2356         param[0] = 0x64;
2357         if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
2358             ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
2359                 psmouse_err(psmouse, "Failed to enable data reporting\n");
2360                 return -1;
2361         }
2362
2363         return 0;
2364
2365 error:
2366         /*
2367          * Leaving the touchpad in command mode will essentially render
2368          * it unusable until the machine reboots, so exit it here just
2369          * to be safe
2370          */
2371         alps_exit_command_mode(psmouse);
2372         return -1;
2373 }
2374
2375 static int alps_get_otp_values_ss4_v2(struct psmouse *psmouse,
2376                                       unsigned char index, unsigned char otp[])
2377 {
2378         struct ps2dev *ps2dev = &psmouse->ps2dev;
2379
2380         switch (index) {
2381         case 0:
2382                 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)  ||
2383                     ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)  ||
2384                     ps2_command(ps2dev, otp, PSMOUSE_CMD_GETINFO))
2385                         return -1;
2386
2387                 break;
2388
2389         case 1:
2390                 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL)  ||
2391                     ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL)  ||
2392                     ps2_command(ps2dev, otp, PSMOUSE_CMD_GETINFO))
2393                         return -1;
2394
2395                 break;
2396         }
2397
2398         return 0;
2399 }
2400
2401 static int alps_update_device_area_ss4_v2(unsigned char otp[][4],
2402                                           struct alps_data *priv)
2403 {
2404         int num_x_electrode;
2405         int num_y_electrode;
2406         int x_pitch, y_pitch, x_phys, y_phys;
2407
2408         num_x_electrode = SS4_NUMSENSOR_XOFFSET + (otp[1][0] & 0x0F);
2409         num_y_electrode = SS4_NUMSENSOR_YOFFSET + ((otp[1][0] >> 4) & 0x0F);
2410
2411         priv->x_max = (num_x_electrode - 1) * SS4_COUNT_PER_ELECTRODE;
2412         priv->y_max = (num_y_electrode - 1) * SS4_COUNT_PER_ELECTRODE;
2413
2414         x_pitch = ((otp[1][2] >> 2) & 0x07) + SS4_MIN_PITCH_MM;
2415         y_pitch = ((otp[1][2] >> 5) & 0x07) + SS4_MIN_PITCH_MM;
2416
2417         x_phys = x_pitch * (num_x_electrode - 1); /* In 0.1 mm units */
2418         y_phys = y_pitch * (num_y_electrode - 1); /* In 0.1 mm units */
2419
2420         priv->x_res = priv->x_max * 10 / x_phys; /* units / mm */
2421         priv->y_res = priv->y_max * 10 / y_phys; /* units / mm */
2422
2423         return 0;
2424 }
2425
2426 static int alps_update_btn_info_ss4_v2(unsigned char otp[][4],
2427                                        struct alps_data *priv)
2428 {
2429         unsigned char is_btnless;
2430
2431         is_btnless = (otp[1][1] >> 3) & 0x01;
2432
2433         if (is_btnless)
2434                 priv->flags |= ALPS_BUTTONPAD;
2435
2436         return 0;
2437 }
2438
2439 static int alps_set_defaults_ss4_v2(struct psmouse *psmouse,
2440                                     struct alps_data *priv)
2441 {
2442         unsigned char otp[2][4];
2443
2444         memset(otp, 0, sizeof(otp));
2445
2446         if (alps_get_otp_values_ss4_v2(psmouse, 0, &otp[0][0]) ||
2447             alps_get_otp_values_ss4_v2(psmouse, 1, &otp[1][0]))
2448                 return -1;
2449
2450         alps_update_device_area_ss4_v2(otp, priv);
2451
2452         alps_update_btn_info_ss4_v2(otp, priv);
2453
2454         return 0;
2455 }
2456
2457 static int alps_dolphin_get_device_area(struct psmouse *psmouse,
2458                                         struct alps_data *priv)
2459 {
2460         struct ps2dev *ps2dev = &psmouse->ps2dev;
2461         unsigned char param[4] = {0};
2462         int num_x_electrode, num_y_electrode;
2463
2464         if (alps_enter_command_mode(psmouse))
2465                 return -1;
2466
2467         param[0] = 0x0a;
2468         if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
2469             ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2470             ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
2471             ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2472             ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE))
2473                 return -1;
2474
2475         if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
2476                 return -1;
2477
2478         /*
2479          * Dolphin's sensor line number is not fixed. It can be calculated
2480          * by adding the device's register value with DOLPHIN_PROFILE_X/YOFFSET.
2481          * Further more, we can get device's x_max and y_max by multiplying
2482          * sensor line number with DOLPHIN_COUNT_PER_ELECTRODE.
2483          *
2484          * e.g. When we get register's sensor_x = 11 & sensor_y = 8,
2485          *      real sensor line number X = 11 + 8 = 19, and
2486          *      real sensor line number Y = 8 + 1 = 9.
2487          *      So, x_max = (19 - 1) * 64 = 1152, and
2488          *          y_max = (9 - 1) * 64 = 512.
2489          */
2490         num_x_electrode = DOLPHIN_PROFILE_XOFFSET + (param[2] & 0x0F);
2491         num_y_electrode = DOLPHIN_PROFILE_YOFFSET + ((param[2] >> 4) & 0x0F);
2492         priv->x_bits = num_x_electrode;
2493         priv->y_bits = num_y_electrode;
2494         priv->x_max = (num_x_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2495         priv->y_max = (num_y_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
2496
2497         if (alps_exit_command_mode(psmouse))
2498                 return -1;
2499
2500         return 0;
2501 }
2502
2503 static int alps_hw_init_dolphin_v1(struct psmouse *psmouse)
2504 {
2505         struct ps2dev *ps2dev = &psmouse->ps2dev;
2506         unsigned char param[2];
2507
2508         /* This is dolphin "v1" as empirically defined by florin9doi */
2509         param[0] = 0x64;
2510         param[1] = 0x28;
2511
2512         if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2513             ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2514             ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
2515                 return -1;
2516
2517         return 0;
2518 }
2519
2520 static int alps_hw_init_v7(struct psmouse *psmouse)
2521 {
2522         struct ps2dev *ps2dev = &psmouse->ps2dev;
2523         int reg_val, ret = -1;
2524
2525         if (alps_enter_command_mode(psmouse) ||
2526             alps_command_mode_read_reg(psmouse, 0xc2d9) == -1)
2527                 goto error;
2528
2529         if (alps_get_v3_v7_resolution(psmouse, 0xc397))
2530                 goto error;
2531
2532         if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
2533                 goto error;
2534
2535         reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
2536         if (reg_val == -1)
2537                 goto error;
2538         if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
2539                 goto error;
2540
2541         alps_exit_command_mode(psmouse);
2542         return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2543
2544 error:
2545         alps_exit_command_mode(psmouse);
2546         return ret;
2547 }
2548
2549 static int alps_hw_init_ss4_v2(struct psmouse *psmouse)
2550 {
2551         struct ps2dev *ps2dev = &psmouse->ps2dev;
2552         char param[2] = {0x64, 0x28};
2553         int ret = -1;
2554
2555         /* enter absolute mode */
2556         if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2557             ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
2558             ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
2559             ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE)) {
2560                 goto error;
2561         }
2562
2563         /* T.B.D. Decread noise packet number, delete in the future */
2564         if (alps_exit_command_mode(psmouse) ||
2565             alps_enter_command_mode(psmouse) ||
2566             alps_command_mode_write_reg(psmouse, 0x001D, 0x20)) {
2567                 goto error;
2568         }
2569         alps_exit_command_mode(psmouse);
2570
2571         return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
2572
2573 error:
2574         alps_exit_command_mode(psmouse);
2575         return ret;
2576 }
2577
2578 static int alps_set_protocol(struct psmouse *psmouse,
2579                              struct alps_data *priv,
2580                              const struct alps_protocol_info *protocol)
2581 {
2582         psmouse->private = priv;
2583
2584         setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
2585
2586         priv->proto_version = protocol->version;
2587         priv->byte0 = protocol->byte0;
2588         priv->mask0 = protocol->mask0;
2589         priv->flags = protocol->flags;
2590
2591         priv->x_max = 2000;
2592         priv->y_max = 1400;
2593         priv->x_bits = 15;
2594         priv->y_bits = 11;
2595
2596         switch (priv->proto_version) {
2597         case ALPS_PROTO_V1:
2598         case ALPS_PROTO_V2:
2599                 priv->hw_init = alps_hw_init_v1_v2;
2600                 priv->process_packet = alps_process_packet_v1_v2;
2601                 priv->set_abs_params = alps_set_abs_params_st;
2602                 priv->x_max = 1023;
2603                 priv->y_max = 767;
2604                 break;
2605
2606         case ALPS_PROTO_V3:
2607                 priv->hw_init = alps_hw_init_v3;
2608                 priv->process_packet = alps_process_packet_v3;
2609                 priv->set_abs_params = alps_set_abs_params_mt;
2610                 priv->decode_fields = alps_decode_pinnacle;
2611                 priv->nibble_commands = alps_v3_nibble_commands;
2612                 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2613                 break;
2614
2615         case ALPS_PROTO_V3_RUSHMORE:
2616                 priv->hw_init = alps_hw_init_rushmore_v3;
2617                 priv->process_packet = alps_process_packet_v3;
2618                 priv->set_abs_params = alps_set_abs_params_mt;
2619                 priv->decode_fields = alps_decode_rushmore;
2620                 priv->nibble_commands = alps_v3_nibble_commands;
2621                 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2622                 priv->x_bits = 16;
2623                 priv->y_bits = 12;
2624
2625                 if (alps_probe_trackstick_v3(psmouse,
2626                                              ALPS_REG_BASE_RUSHMORE) < 0)
2627                         priv->flags &= ~ALPS_DUALPOINT;
2628
2629                 break;
2630
2631         case ALPS_PROTO_V4:
2632                 priv->hw_init = alps_hw_init_v4;
2633                 priv->process_packet = alps_process_packet_v4;
2634                 priv->set_abs_params = alps_set_abs_params_mt;
2635                 priv->nibble_commands = alps_v4_nibble_commands;
2636                 priv->addr_command = PSMOUSE_CMD_DISABLE;
2637                 break;
2638
2639         case ALPS_PROTO_V5:
2640                 priv->hw_init = alps_hw_init_dolphin_v1;
2641                 priv->process_packet = alps_process_touchpad_packet_v3_v5;
2642                 priv->decode_fields = alps_decode_dolphin;
2643                 priv->set_abs_params = alps_set_abs_params_mt;
2644                 priv->nibble_commands = alps_v3_nibble_commands;
2645                 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2646                 priv->x_bits = 23;
2647                 priv->y_bits = 12;
2648
2649                 if (alps_dolphin_get_device_area(psmouse, priv))
2650                         return -EIO;
2651
2652                 break;
2653
2654         case ALPS_PROTO_V6:
2655                 priv->hw_init = alps_hw_init_v6;
2656                 priv->process_packet = alps_process_packet_v6;
2657                 priv->set_abs_params = alps_set_abs_params_st;
2658                 priv->nibble_commands = alps_v6_nibble_commands;
2659                 priv->x_max = 2047;
2660                 priv->y_max = 1535;
2661                 break;
2662
2663         case ALPS_PROTO_V7:
2664                 priv->hw_init = alps_hw_init_v7;
2665                 priv->process_packet = alps_process_packet_v7;
2666                 priv->decode_fields = alps_decode_packet_v7;
2667                 priv->set_abs_params = alps_set_abs_params_v7;
2668                 priv->nibble_commands = alps_v3_nibble_commands;
2669                 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2670                 priv->x_max = 0xfff;
2671                 priv->y_max = 0x7ff;
2672
2673                 if (priv->fw_ver[1] != 0xba)
2674                         priv->flags |= ALPS_BUTTONPAD;
2675
2676                 break;
2677
2678         case ALPS_PROTO_V8:
2679                 priv->hw_init = alps_hw_init_ss4_v2;
2680                 priv->process_packet = alps_process_packet_ss4_v2;
2681                 priv->decode_fields = alps_decode_ss4_v2;
2682                 priv->set_abs_params = alps_set_abs_params_ss4_v2;
2683                 priv->nibble_commands = alps_v3_nibble_commands;
2684                 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
2685
2686                 if (alps_set_defaults_ss4_v2(psmouse, priv))
2687                         return -EIO;
2688
2689                 break;
2690         }
2691
2692         return 0;
2693 }
2694
2695 static const struct alps_protocol_info *alps_match_table(unsigned char *e7,
2696                                                          unsigned char *ec)
2697 {
2698         const struct alps_model_info *model;
2699         int i;
2700
2701         for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
2702                 model = &alps_model_data[i];
2703
2704                 if (!memcmp(e7, model->signature, sizeof(model->signature)) &&
2705                     (!model->command_mode_resp ||
2706                      model->command_mode_resp == ec[2])) {
2707
2708                         return &model->protocol_info;
2709                 }
2710         }
2711
2712         return NULL;
2713 }
2714
2715 static int alps_identify(struct psmouse *psmouse, struct alps_data *priv)
2716 {
2717         const struct alps_protocol_info *protocol;
2718         unsigned char e6[4], e7[4], ec[4];
2719         int error;
2720
2721         /*
2722          * First try "E6 report".
2723          * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
2724          * The bits 0-2 of the first byte will be 1s if some buttons are
2725          * pressed.
2726          */
2727         if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2728                          PSMOUSE_CMD_SETSCALE11, e6))
2729                 return -EIO;
2730
2731         if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100))
2732                 return -EINVAL;
2733
2734         /*
2735          * Now get the "E7" and "EC" reports.  These will uniquely identify
2736          * most ALPS touchpads.
2737          */
2738         if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2739                          PSMOUSE_CMD_SETSCALE21, e7) ||
2740             alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
2741                          PSMOUSE_CMD_RESET_WRAP, ec) ||
2742             alps_exit_command_mode(psmouse))
2743                 return -EIO;
2744
2745         protocol = alps_match_table(e7, ec);
2746         if (!protocol) {
2747                 if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0x50 &&
2748                            ec[0] == 0x73 && (ec[1] == 0x01 || ec[1] == 0x02)) {
2749                         protocol = &alps_v5_protocol_data;
2750                 } else if (ec[0] == 0x88 &&
2751                            ((ec[1] & 0xf0) == 0xb0 || (ec[1] & 0xf0) == 0xc0)) {
2752                         protocol = &alps_v7_protocol_data;
2753                 } else if (ec[0] == 0x88 && ec[1] == 0x08) {
2754                         protocol = &alps_v3_rushmore_data;
2755                 } else if (ec[0] == 0x88 && ec[1] == 0x07 &&
2756                            ec[2] >= 0x90 && ec[2] <= 0x9d) {
2757                         protocol = &alps_v3_protocol_data;
2758                 } else if (e7[0] == 0x73 && e7[1] == 0x03 &&
2759                            e7[2] == 0x14 && ec[1] == 0x02) {
2760                         protocol = &alps_v8_protocol_data;
2761                 } else {
2762                         psmouse_dbg(psmouse,
2763                                     "Likely not an ALPS touchpad: E7=%3ph, EC=%3ph\n", e7, ec);
2764                         return -EINVAL;
2765                 }
2766         }
2767
2768         if (priv) {
2769                 /* Save the Firmware version */
2770                 memcpy(priv->fw_ver, ec, 3);
2771                 error = alps_set_protocol(psmouse, priv, protocol);
2772                 if (error)
2773                         return error;
2774         }
2775
2776         return 0;
2777 }
2778
2779 static int alps_reconnect(struct psmouse *psmouse)
2780 {
2781         struct alps_data *priv = psmouse->private;
2782
2783         psmouse_reset(psmouse);
2784
2785         if (alps_identify(psmouse, priv) < 0)
2786                 return -1;
2787
2788         return priv->hw_init(psmouse);
2789 }
2790
2791 static void alps_disconnect(struct psmouse *psmouse)
2792 {
2793         struct alps_data *priv = psmouse->private;
2794
2795         psmouse_reset(psmouse);
2796         del_timer_sync(&priv->timer);
2797         if (priv->dev2)
2798                 input_unregister_device(priv->dev2);
2799         if (!IS_ERR_OR_NULL(priv->dev3))
2800                 input_unregister_device(priv->dev3);
2801         kfree(priv);
2802 }
2803
2804 static void alps_set_abs_params_st(struct alps_data *priv,
2805                                    struct input_dev *dev1)
2806 {
2807         input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
2808         input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
2809         input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
2810 }
2811
2812 static void alps_set_abs_params_mt_common(struct alps_data *priv,
2813                                           struct input_dev *dev1)
2814 {
2815         input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
2816         input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
2817
2818         input_abs_set_res(dev1, ABS_MT_POSITION_X, priv->x_res);
2819         input_abs_set_res(dev1, ABS_MT_POSITION_Y, priv->y_res);
2820
2821         set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
2822         set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
2823 }
2824
2825 static void alps_set_abs_params_mt(struct alps_data *priv,
2826                                    struct input_dev *dev1)
2827 {
2828         alps_set_abs_params_mt_common(priv, dev1);
2829         input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
2830
2831         input_mt_init_slots(dev1, MAX_TOUCHES,
2832                             INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
2833                                 INPUT_MT_SEMI_MT);
2834 }
2835
2836 static void alps_set_abs_params_v7(struct alps_data *priv,
2837                                    struct input_dev *dev1)
2838 {
2839         alps_set_abs_params_mt_common(priv, dev1);
2840         set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
2841
2842         input_mt_init_slots(dev1, MAX_TOUCHES,
2843                             INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
2844                                 INPUT_MT_TRACK);
2845
2846         set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
2847 }
2848
2849 static void alps_set_abs_params_ss4_v2(struct alps_data *priv,
2850                                        struct input_dev *dev1)
2851 {
2852         alps_set_abs_params_mt_common(priv, dev1);
2853         input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
2854         set_bit(BTN_TOOL_QUINTTAP, dev1->keybit);
2855
2856         input_mt_init_slots(dev1, MAX_TOUCHES,
2857                             INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
2858                                 INPUT_MT_TRACK);
2859 }
2860
2861 int alps_init(struct psmouse *psmouse)
2862 {
2863         struct alps_data *priv = psmouse->private;
2864         struct input_dev *dev1 = psmouse->dev;
2865         int error;
2866
2867         error = priv->hw_init(psmouse);
2868         if (error)
2869                 goto init_fail;
2870
2871         /*
2872          * Undo part of setup done for us by psmouse core since touchpad
2873          * is not a relative device.
2874          */
2875         __clear_bit(EV_REL, dev1->evbit);
2876         __clear_bit(REL_X, dev1->relbit);
2877         __clear_bit(REL_Y, dev1->relbit);
2878
2879         /*
2880          * Now set up our capabilities.
2881          */
2882         dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
2883         dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
2884         dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
2885         dev1->keybit[BIT_WORD(BTN_LEFT)] |=
2886                 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
2887
2888         dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
2889
2890         priv->set_abs_params(priv, dev1);
2891
2892         if (priv->flags & ALPS_WHEEL) {
2893                 dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
2894                 dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
2895         }
2896
2897         if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
2898                 dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
2899                 dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
2900         }
2901
2902         if (priv->flags & ALPS_FOUR_BUTTONS) {
2903                 dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
2904                 dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
2905                 dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
2906                 dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
2907         } else if (priv->flags & ALPS_BUTTONPAD) {
2908                 set_bit(INPUT_PROP_BUTTONPAD, dev1->propbit);
2909                 clear_bit(BTN_RIGHT, dev1->keybit);
2910         } else {
2911                 dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
2912         }
2913
2914         if (priv->flags & ALPS_DUALPOINT) {
2915                 struct input_dev *dev2;
2916
2917                 dev2 = input_allocate_device();
2918                 if (!dev2) {
2919                         psmouse_err(psmouse,
2920                                     "failed to allocate trackstick device\n");
2921                         error = -ENOMEM;
2922                         goto init_fail;
2923                 }
2924
2925                 snprintf(priv->phys2, sizeof(priv->phys2), "%s/input1",
2926                          psmouse->ps2dev.serio->phys);
2927                 dev2->phys = priv->phys2;
2928
2929                 /*
2930                  * format of input device name is: "protocol vendor name"
2931                  * see function psmouse_switch_protocol() in psmouse-base.c
2932                  */
2933                 dev2->name = "AlpsPS/2 ALPS DualPoint Stick";
2934
2935                 dev2->id.bustype = BUS_I8042;
2936                 dev2->id.vendor  = 0x0002;
2937                 dev2->id.product = PSMOUSE_ALPS;
2938                 dev2->id.version = priv->proto_version;
2939                 dev2->dev.parent = &psmouse->ps2dev.serio->dev;
2940
2941                 input_set_capability(dev2, EV_REL, REL_X);
2942                 input_set_capability(dev2, EV_REL, REL_Y);
2943                 input_set_capability(dev2, EV_KEY, BTN_LEFT);
2944                 input_set_capability(dev2, EV_KEY, BTN_RIGHT);
2945                 input_set_capability(dev2, EV_KEY, BTN_MIDDLE);
2946
2947                 __set_bit(INPUT_PROP_POINTER, dev2->propbit);
2948                 __set_bit(INPUT_PROP_POINTING_STICK, dev2->propbit);
2949
2950                 error = input_register_device(dev2);
2951                 if (error) {
2952                         psmouse_err(psmouse,
2953                                     "failed to register trackstick device: %d\n",
2954                                     error);
2955                         input_free_device(dev2);
2956                         goto init_fail;
2957                 }
2958
2959                 priv->dev2 = dev2;
2960         }
2961
2962         priv->psmouse = psmouse;
2963
2964         INIT_DELAYED_WORK(&priv->dev3_register_work,
2965                           alps_register_bare_ps2_mouse);
2966
2967         psmouse->protocol_handler = alps_process_byte;
2968         psmouse->poll = alps_poll;
2969         psmouse->disconnect = alps_disconnect;
2970         psmouse->reconnect = alps_reconnect;
2971         psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
2972
2973         /* We are having trouble resyncing ALPS touchpads so disable it for now */
2974         psmouse->resync_time = 0;
2975
2976         /* Allow 2 invalid packets without resetting device */
2977         psmouse->resetafter = psmouse->pktsize * 2;
2978
2979         return 0;
2980
2981 init_fail:
2982         psmouse_reset(psmouse);
2983         /*
2984          * Even though we did not allocate psmouse->private we do free
2985          * it here.
2986          */
2987         kfree(psmouse->private);
2988         psmouse->private = NULL;
2989         return error;
2990 }
2991
2992 int alps_detect(struct psmouse *psmouse, bool set_properties)
2993 {
2994         struct alps_data *priv;
2995         int error;
2996
2997         error = alps_identify(psmouse, NULL);
2998         if (error)
2999                 return error;
3000
3001         /*
3002          * Reset the device to make sure it is fully operational:
3003          * on some laptops, like certain Dell Latitudes, we may
3004          * fail to properly detect presence of trackstick if device
3005          * has not been reset.
3006          */
3007         psmouse_reset(psmouse);
3008
3009         priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
3010         if (!priv)
3011                 return -ENOMEM;
3012
3013         error = alps_identify(psmouse, priv);
3014         if (error) {
3015                 kfree(priv);
3016                 return error;
3017         }
3018
3019         if (set_properties) {
3020                 psmouse->vendor = "ALPS";
3021                 psmouse->name = priv->flags & ALPS_DUALPOINT ?
3022                                 "DualPoint TouchPad" : "GlidePoint";
3023                 psmouse->model = priv->proto_version;
3024         } else {
3025                 /*
3026                  * Destroy alps_data structure we allocated earlier since
3027                  * this was just a "trial run". Otherwise we'll keep it
3028                  * to be used by alps_init() which has to be called if
3029                  * we succeed and set_properties is true.
3030                  */
3031                 kfree(priv);
3032                 psmouse->private = NULL;
3033         }
3034
3035         return 0;
3036 }
3037