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