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