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