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