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