Input: alps - process_bitmap: round down when spreading adjescent fingers over 2...
[cascardo/linux.git] / drivers / input / mouse / alps.c
1 /*
2  * ALPS touchpad PS/2 mouse driver
3  *
4  * Copyright (c) 2003 Neil Brown <neilb@cse.unsw.edu.au>
5  * Copyright (c) 2003-2005 Peter Osterlund <petero2@telia.com>
6  * Copyright (c) 2004 Dmitry Torokhov <dtor@mail.ru>
7  * Copyright (c) 2005 Vojtech Pavlik <vojtech@suse.cz>
8  * Copyright (c) 2009 Sebastian Kapfer <sebastian_kapfer@gmx.net>
9  *
10  * ALPS detection, tap switching and status querying info is taken from
11  * tpconfig utility (by C. Scott Ananian and Bruce Kall).
12  *
13  * This program is free software; you can redistribute it and/or modify it
14  * under the terms of the GNU General Public License version 2 as published by
15  * the Free Software Foundation.
16  */
17
18 #include <linux/slab.h>
19 #include <linux/input.h>
20 #include <linux/input/mt.h>
21 #include <linux/serio.h>
22 #include <linux/libps2.h>
23
24 #include "psmouse.h"
25 #include "alps.h"
26
27 /*
28  * Definitions for ALPS version 3 and 4 command mode protocol
29  */
30 #define ALPS_CMD_NIBBLE_10      0x01f2
31
32 #define ALPS_REG_BASE_RUSHMORE  0xc2c0
33 #define ALPS_REG_BASE_PINNACLE  0x0000
34
35 static const struct alps_nibble_commands alps_v3_nibble_commands[] = {
36         { PSMOUSE_CMD_SETPOLL,          0x00 }, /* 0 */
37         { PSMOUSE_CMD_RESET_DIS,        0x00 }, /* 1 */
38         { PSMOUSE_CMD_SETSCALE21,       0x00 }, /* 2 */
39         { PSMOUSE_CMD_SETRATE,          0x0a }, /* 3 */
40         { PSMOUSE_CMD_SETRATE,          0x14 }, /* 4 */
41         { PSMOUSE_CMD_SETRATE,          0x28 }, /* 5 */
42         { PSMOUSE_CMD_SETRATE,          0x3c }, /* 6 */
43         { PSMOUSE_CMD_SETRATE,          0x50 }, /* 7 */
44         { PSMOUSE_CMD_SETRATE,          0x64 }, /* 8 */
45         { PSMOUSE_CMD_SETRATE,          0xc8 }, /* 9 */
46         { ALPS_CMD_NIBBLE_10,           0x00 }, /* a */
47         { PSMOUSE_CMD_SETRES,           0x00 }, /* b */
48         { PSMOUSE_CMD_SETRES,           0x01 }, /* c */
49         { PSMOUSE_CMD_SETRES,           0x02 }, /* d */
50         { PSMOUSE_CMD_SETRES,           0x03 }, /* e */
51         { PSMOUSE_CMD_SETSCALE11,       0x00 }, /* f */
52 };
53
54 static const struct alps_nibble_commands alps_v4_nibble_commands[] = {
55         { PSMOUSE_CMD_ENABLE,           0x00 }, /* 0 */
56         { PSMOUSE_CMD_RESET_DIS,        0x00 }, /* 1 */
57         { PSMOUSE_CMD_SETSCALE21,       0x00 }, /* 2 */
58         { PSMOUSE_CMD_SETRATE,          0x0a }, /* 3 */
59         { PSMOUSE_CMD_SETRATE,          0x14 }, /* 4 */
60         { PSMOUSE_CMD_SETRATE,          0x28 }, /* 5 */
61         { PSMOUSE_CMD_SETRATE,          0x3c }, /* 6 */
62         { PSMOUSE_CMD_SETRATE,          0x50 }, /* 7 */
63         { PSMOUSE_CMD_SETRATE,          0x64 }, /* 8 */
64         { PSMOUSE_CMD_SETRATE,          0xc8 }, /* 9 */
65         { ALPS_CMD_NIBBLE_10,           0x00 }, /* a */
66         { PSMOUSE_CMD_SETRES,           0x00 }, /* b */
67         { PSMOUSE_CMD_SETRES,           0x01 }, /* c */
68         { PSMOUSE_CMD_SETRES,           0x02 }, /* d */
69         { PSMOUSE_CMD_SETRES,           0x03 }, /* e */
70         { PSMOUSE_CMD_SETSCALE11,       0x00 }, /* f */
71 };
72
73 static const struct alps_nibble_commands alps_v6_nibble_commands[] = {
74         { PSMOUSE_CMD_ENABLE,           0x00 }, /* 0 */
75         { PSMOUSE_CMD_SETRATE,          0x0a }, /* 1 */
76         { PSMOUSE_CMD_SETRATE,          0x14 }, /* 2 */
77         { PSMOUSE_CMD_SETRATE,          0x28 }, /* 3 */
78         { PSMOUSE_CMD_SETRATE,          0x3c }, /* 4 */
79         { PSMOUSE_CMD_SETRATE,          0x50 }, /* 5 */
80         { PSMOUSE_CMD_SETRATE,          0x64 }, /* 6 */
81         { PSMOUSE_CMD_SETRATE,          0xc8 }, /* 7 */
82         { PSMOUSE_CMD_GETID,            0x00 }, /* 8 */
83         { PSMOUSE_CMD_GETINFO,          0x00 }, /* 9 */
84         { PSMOUSE_CMD_SETRES,           0x00 }, /* a */
85         { PSMOUSE_CMD_SETRES,           0x01 }, /* b */
86         { PSMOUSE_CMD_SETRES,           0x02 }, /* c */
87         { PSMOUSE_CMD_SETRES,           0x03 }, /* d */
88         { PSMOUSE_CMD_SETSCALE21,       0x00 }, /* e */
89         { PSMOUSE_CMD_SETSCALE11,       0x00 }, /* f */
90 };
91
92
93 #define ALPS_DUALPOINT          0x02    /* touchpad has trackstick */
94 #define ALPS_PASS               0x04    /* device has a pass-through port */
95
96 #define ALPS_WHEEL              0x08    /* hardware wheel present */
97 #define ALPS_FW_BK_1            0x10    /* front & back buttons present */
98 #define ALPS_FW_BK_2            0x20    /* front & back buttons present */
99 #define ALPS_FOUR_BUTTONS       0x40    /* 4 direction button present */
100 #define ALPS_PS2_INTERLEAVED    0x80    /* 3-byte PS/2 packet interleaved with
101                                            6-byte ALPS packet */
102 #define ALPS_IS_RUSHMORE        0x100   /* device is a rushmore */
103
104 static const struct alps_model_info alps_model_data[] = {
105         { { 0x32, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT },  /* Toshiba Salellite Pro M10 */
106         { { 0x33, 0x02, 0x0a }, 0x00, ALPS_PROTO_V1, 0x88, 0xf8, 0 },                           /* UMAX-530T */
107         { { 0x53, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
108         { { 0x53, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
109         { { 0x60, 0x03, 0xc8 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },                           /* HP ze1115 */
110         { { 0x63, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
111         { { 0x63, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
112         { { 0x63, 0x02, 0x28 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 },                /* Fujitsu Siemens S6010 */
113         { { 0x63, 0x02, 0x3c }, 0x00, ALPS_PROTO_V2, 0x8f, 0x8f, ALPS_WHEEL },                  /* Toshiba Satellite S2400-103 */
114         { { 0x63, 0x02, 0x50 }, 0x00, ALPS_PROTO_V2, 0xef, 0xef, ALPS_FW_BK_1 },                /* NEC Versa L320 */
115         { { 0x63, 0x02, 0x64 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
116         { { 0x63, 0x03, 0xc8 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT },  /* Dell Latitude D800 */
117         { { 0x73, 0x00, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_DUALPOINT },              /* ThinkPad R61 8918-5QG */
118         { { 0x73, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, 0 },
119         { { 0x73, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_FW_BK_2 },                /* Ahtec Laptop */
120         { { 0x20, 0x02, 0x0e }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT },  /* XXX */
121         { { 0x22, 0x02, 0x0a }, 0x00, ALPS_PROTO_V2, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT },
122         { { 0x22, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT },  /* Dell Latitude D600 */
123         /* Dell Latitude E5500, E6400, E6500, Precision M4400 */
124         { { 0x62, 0x02, 0x14 }, 0x00, ALPS_PROTO_V2, 0xcf, 0xcf,
125                 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED },
126         { { 0x73, 0x00, 0x14 }, 0x00, ALPS_PROTO_V6, 0xff, 0xff, ALPS_DUALPOINT },              /* Dell XT2 */
127         { { 0x73, 0x02, 0x50 }, 0x00, ALPS_PROTO_V2, 0xcf, 0xcf, ALPS_FOUR_BUTTONS },           /* Dell Vostro 1400 */
128         { { 0x52, 0x01, 0x14 }, 0x00, ALPS_PROTO_V2, 0xff, 0xff,
129                 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED },                            /* Toshiba Tecra A11-11L */
130         { { 0x73, 0x02, 0x64 }, 0x8a, ALPS_PROTO_V4, 0x8f, 0x8f, 0 },
131 };
132
133 static void alps_set_abs_params_st(struct alps_data *priv,
134                                    struct input_dev *dev1);
135 static void alps_set_abs_params_mt(struct alps_data *priv,
136                                    struct input_dev *dev1);
137
138 /*
139  * XXX - this entry is suspicious. First byte has zero lower nibble,
140  * which is what a normal mouse would report. Also, the value 0x0e
141  * isn't valid per PS/2 spec.
142  */
143
144 /* Packet formats are described in Documentation/input/alps.txt */
145
146 static bool alps_is_valid_first_byte(struct alps_data *priv,
147                                      unsigned char data)
148 {
149         return (data & priv->mask0) == priv->byte0;
150 }
151
152 static void alps_report_buttons(struct psmouse *psmouse,
153                                 struct input_dev *dev1, struct input_dev *dev2,
154                                 int left, int right, int middle)
155 {
156         struct input_dev *dev;
157
158         /*
159          * If shared button has already been reported on the
160          * other device (dev2) then this event should be also
161          * sent through that device.
162          */
163         dev = test_bit(BTN_LEFT, dev2->key) ? dev2 : dev1;
164         input_report_key(dev, BTN_LEFT, left);
165
166         dev = test_bit(BTN_RIGHT, dev2->key) ? dev2 : dev1;
167         input_report_key(dev, BTN_RIGHT, right);
168
169         dev = test_bit(BTN_MIDDLE, dev2->key) ? dev2 : dev1;
170         input_report_key(dev, BTN_MIDDLE, middle);
171
172         /*
173          * Sync the _other_ device now, we'll do the first
174          * device later once we report the rest of the events.
175          */
176         input_sync(dev2);
177 }
178
179 static void alps_process_packet_v1_v2(struct psmouse *psmouse)
180 {
181         struct alps_data *priv = psmouse->private;
182         unsigned char *packet = psmouse->packet;
183         struct input_dev *dev = psmouse->dev;
184         struct input_dev *dev2 = priv->dev2;
185         int x, y, z, ges, fin, left, right, middle;
186         int back = 0, forward = 0;
187
188         if (priv->proto_version == ALPS_PROTO_V1) {
189                 left = packet[2] & 0x10;
190                 right = packet[2] & 0x08;
191                 middle = 0;
192                 x = packet[1] | ((packet[0] & 0x07) << 7);
193                 y = packet[4] | ((packet[3] & 0x07) << 7);
194                 z = packet[5];
195         } else {
196                 left = packet[3] & 1;
197                 right = packet[3] & 2;
198                 middle = packet[3] & 4;
199                 x = packet[1] | ((packet[2] & 0x78) << (7 - 3));
200                 y = packet[4] | ((packet[3] & 0x70) << (7 - 4));
201                 z = packet[5];
202         }
203
204         if (priv->flags & ALPS_FW_BK_1) {
205                 back = packet[0] & 0x10;
206                 forward = packet[2] & 4;
207         }
208
209         if (priv->flags & ALPS_FW_BK_2) {
210                 back = packet[3] & 4;
211                 forward = packet[2] & 4;
212                 if ((middle = forward && back))
213                         forward = back = 0;
214         }
215
216         ges = packet[2] & 1;
217         fin = packet[2] & 2;
218
219         if ((priv->flags & ALPS_DUALPOINT) && z == 127) {
220                 input_report_rel(dev2, REL_X,  (x > 383 ? (x - 768) : x));
221                 input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y));
222
223                 alps_report_buttons(psmouse, dev2, dev, left, right, middle);
224
225                 input_sync(dev2);
226                 return;
227         }
228
229         alps_report_buttons(psmouse, dev, dev2, left, right, middle);
230
231         /* Convert hardware tap to a reasonable Z value */
232         if (ges && !fin)
233                 z = 40;
234
235         /*
236          * A "tap and drag" operation is reported by the hardware as a transition
237          * from (!fin && ges) to (fin && ges). This should be translated to the
238          * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
239          */
240         if (ges && fin && !priv->prev_fin) {
241                 input_report_abs(dev, ABS_X, x);
242                 input_report_abs(dev, ABS_Y, y);
243                 input_report_abs(dev, ABS_PRESSURE, 0);
244                 input_report_key(dev, BTN_TOOL_FINGER, 0);
245                 input_sync(dev);
246         }
247         priv->prev_fin = fin;
248
249         if (z > 30)
250                 input_report_key(dev, BTN_TOUCH, 1);
251         if (z < 25)
252                 input_report_key(dev, BTN_TOUCH, 0);
253
254         if (z > 0) {
255                 input_report_abs(dev, ABS_X, x);
256                 input_report_abs(dev, ABS_Y, y);
257         }
258
259         input_report_abs(dev, ABS_PRESSURE, z);
260         input_report_key(dev, BTN_TOOL_FINGER, z > 0);
261
262         if (priv->flags & ALPS_WHEEL)
263                 input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
264
265         if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
266                 input_report_key(dev, BTN_FORWARD, forward);
267                 input_report_key(dev, BTN_BACK, back);
268         }
269
270         if (priv->flags & ALPS_FOUR_BUTTONS) {
271                 input_report_key(dev, BTN_0, packet[2] & 4);
272                 input_report_key(dev, BTN_1, packet[0] & 0x10);
273                 input_report_key(dev, BTN_2, packet[3] & 4);
274                 input_report_key(dev, BTN_3, packet[0] & 0x20);
275         }
276
277         input_sync(dev);
278 }
279
280 /*
281  * Process bitmap data for V5 protocols. Return value is null.
282  *
283  * The bitmaps don't have enough data to track fingers, so this function
284  * only generates points representing a bounding box of at most two contacts.
285  * These two points are returned in x1, y1, x2, and y2.
286  */
287 static void alps_process_bitmap_dolphin(struct alps_data *priv,
288                                         struct alps_fields *fields,
289                                         int *x1, int *y1, int *x2, int *y2)
290 {
291         int box_middle_x, box_middle_y;
292         unsigned int x_map, y_map;
293         unsigned char start_bit, end_bit;
294         unsigned char x_msb, x_lsb, y_msb, y_lsb;
295
296         x_map = fields->x_map;
297         y_map = fields->y_map;
298
299         if (!x_map || !y_map)
300                 return;
301
302         /* Get Most-significant and Least-significant bit */
303         x_msb = fls(x_map);
304         x_lsb = ffs(x_map);
305         y_msb = fls(y_map);
306         y_lsb = ffs(y_map);
307
308         /* Most-significant bit should never exceed max sensor line number */
309         if (x_msb > priv->x_bits || y_msb > priv->y_bits)
310                 return;
311
312         *x1 = *y1 = *x2 = *y2 = 0;
313
314         if (fields->fingers > 1) {
315                 start_bit = priv->x_bits - x_msb;
316                 end_bit = priv->x_bits - x_lsb;
317                 box_middle_x = (priv->x_max * (start_bit + end_bit)) /
318                                 (2 * (priv->x_bits - 1));
319
320                 start_bit = y_lsb - 1;
321                 end_bit = y_msb - 1;
322                 box_middle_y = (priv->y_max * (start_bit + end_bit)) /
323                                 (2 * (priv->y_bits - 1));
324                 *x1 = fields->x;
325                 *y1 = fields->y;
326                 *x2 = 2 * box_middle_x - *x1;
327                 *y2 = 2 * box_middle_y - *y1;
328         }
329 }
330
331 static void alps_get_bitmap_points(unsigned int map,
332                                    struct alps_bitmap_point *low,
333                                    struct alps_bitmap_point *high,
334                                    int *fingers)
335 {
336         struct alps_bitmap_point *point;
337         int i, bit, prev_bit = 0;
338
339         point = low;
340         for (i = 0; map != 0; i++, map >>= 1) {
341                 bit = map & 1;
342                 if (bit) {
343                         if (!prev_bit) {
344                                 point->start_bit = i;
345                                 point->num_bits = 0;
346                                 (*fingers)++;
347                         }
348                         point->num_bits++;
349                 } else {
350                         if (prev_bit)
351                                 point = high;
352                 }
353                 prev_bit = bit;
354         }
355 }
356
357 /*
358  * Process bitmap data from v3 and v4 protocols. Returns the number of
359  * fingers detected. A return value of 0 means at least one of the
360  * bitmaps was empty.
361  *
362  * The bitmaps don't have enough data to track fingers, so this function
363  * only generates points representing a bounding box of all contacts.
364  * These points are returned in x1, y1, x2, and y2 when the return value
365  * is greater than 0.
366  */
367 static int alps_process_bitmap(struct alps_data *priv,
368                                unsigned int x_map, unsigned int y_map,
369                                int *x1, int *y1, int *x2, int *y2)
370 {
371         int i, fingers_x = 0, fingers_y = 0, fingers;
372         struct alps_bitmap_point x_low = {0,}, x_high = {0,};
373         struct alps_bitmap_point y_low = {0,}, y_high = {0,};
374
375         if (!x_map || !y_map)
376                 return 0;
377
378         *x1 = *y1 = *x2 = *y2 = 0;
379
380         alps_get_bitmap_points(x_map, &x_low, &x_high, &fingers_x);
381         alps_get_bitmap_points(y_map, &y_low, &y_high, &fingers_y);
382
383         /*
384          * Fingers can overlap, so we use the maximum count of fingers
385          * on either axis as the finger count.
386          */
387         fingers = max(fingers_x, fingers_y);
388
389         /*
390          * If an axis reports only a single contact, we have overlapping or
391          * adjacent fingers. Divide the single contact between the two points.
392          */
393         if (fingers_x == 1) {
394                 i = (x_low.num_bits - 1) / 2;
395                 x_low.num_bits = x_low.num_bits - i;
396                 x_high.start_bit = x_low.start_bit + i;
397                 x_high.num_bits = max(i, 1);
398         }
399         if (fingers_y == 1) {
400                 i = (y_low.num_bits - 1) / 2;
401                 y_low.num_bits = y_low.num_bits - i;
402                 y_high.start_bit = y_low.start_bit + i;
403                 y_high.num_bits = max(i, 1);
404         }
405
406         *x1 = (priv->x_max * (2 * x_low.start_bit + x_low.num_bits - 1)) /
407               (2 * (priv->x_bits - 1));
408         *y1 = (priv->y_max * (2 * y_low.start_bit + y_low.num_bits - 1)) /
409               (2 * (priv->y_bits - 1));
410
411         *x2 = (priv->x_max *
412                (2 * x_high.start_bit + x_high.num_bits - 1)) /
413               (2 * (priv->x_bits - 1));
414         *y2 = (priv->y_max *
415                (2 * y_high.start_bit + y_high.num_bits - 1)) /
416               (2 * (priv->y_bits - 1));
417
418         /* y-bitmap order is reversed, except on rushmore */
419         if (!(priv->flags & ALPS_IS_RUSHMORE)) {
420                 *y1 = priv->y_max - *y1;
421                 *y2 = priv->y_max - *y2;
422         }
423
424         return fingers;
425 }
426
427 static void alps_set_slot(struct input_dev *dev, int slot, bool active,
428                           int x, int y)
429 {
430         input_mt_slot(dev, slot);
431         input_mt_report_slot_state(dev, MT_TOOL_FINGER, active);
432         if (active) {
433                 input_report_abs(dev, ABS_MT_POSITION_X, x);
434                 input_report_abs(dev, ABS_MT_POSITION_Y, y);
435         }
436 }
437
438 static void alps_report_semi_mt_data(struct input_dev *dev, int num_fingers,
439                                      int x1, int y1, int x2, int y2)
440 {
441         alps_set_slot(dev, 0, num_fingers != 0, x1, y1);
442         alps_set_slot(dev, 1, num_fingers == 2, x2, y2);
443 }
444
445 static void alps_process_trackstick_packet_v3(struct psmouse *psmouse)
446 {
447         struct alps_data *priv = psmouse->private;
448         unsigned char *packet = psmouse->packet;
449         struct input_dev *dev = priv->dev2;
450         int x, y, z, left, right, middle;
451
452         /* Sanity check packet */
453         if (!(packet[0] & 0x40)) {
454                 psmouse_dbg(psmouse, "Bad trackstick packet, discarding\n");
455                 return;
456         }
457
458         /*
459          * There's a special packet that seems to indicate the end
460          * of a stream of trackstick data. Filter these out.
461          */
462         if (packet[1] == 0x7f && packet[2] == 0x7f && packet[4] == 0x7f)
463                 return;
464
465         x = (s8)(((packet[0] & 0x20) << 2) | (packet[1] & 0x7f));
466         y = (s8)(((packet[0] & 0x10) << 3) | (packet[2] & 0x7f));
467         z = (packet[4] & 0x7c) >> 2;
468
469         /*
470          * The x and y values tend to be quite large, and when used
471          * alone the trackstick is difficult to use. Scale them down
472          * to compensate.
473          */
474         x /= 8;
475         y /= 8;
476
477         input_report_rel(dev, REL_X, x);
478         input_report_rel(dev, REL_Y, -y);
479
480         /*
481          * Most ALPS models report the trackstick buttons in the touchpad
482          * packets, but a few report them here. No reliable way has been
483          * found to differentiate between the models upfront, so we enable
484          * the quirk in response to seeing a button press in the trackstick
485          * packet.
486          */
487         left = packet[3] & 0x01;
488         right = packet[3] & 0x02;
489         middle = packet[3] & 0x04;
490
491         if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) &&
492             (left || right || middle))
493                 priv->quirks |= ALPS_QUIRK_TRACKSTICK_BUTTONS;
494
495         if (priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS) {
496                 input_report_key(dev, BTN_LEFT, left);
497                 input_report_key(dev, BTN_RIGHT, right);
498                 input_report_key(dev, BTN_MIDDLE, middle);
499         }
500
501         input_sync(dev);
502         return;
503 }
504
505 static void alps_decode_buttons_v3(struct alps_fields *f, unsigned char *p)
506 {
507         f->left = !!(p[3] & 0x01);
508         f->right = !!(p[3] & 0x02);
509         f->middle = !!(p[3] & 0x04);
510
511         f->ts_left = !!(p[3] & 0x10);
512         f->ts_right = !!(p[3] & 0x20);
513         f->ts_middle = !!(p[3] & 0x40);
514 }
515
516 static void alps_decode_pinnacle(struct alps_fields *f, unsigned char *p,
517                                  struct psmouse *psmouse)
518 {
519         f->first_mp = !!(p[4] & 0x40);
520         f->is_mp = !!(p[0] & 0x40);
521
522         f->fingers = (p[5] & 0x3) + 1;
523         f->x_map = ((p[4] & 0x7e) << 8) |
524                    ((p[1] & 0x7f) << 2) |
525                    ((p[0] & 0x30) >> 4);
526         f->y_map = ((p[3] & 0x70) << 4) |
527                    ((p[2] & 0x7f) << 1) |
528                    (p[4] & 0x01);
529
530         f->x = ((p[1] & 0x7f) << 4) | ((p[4] & 0x30) >> 2) |
531                ((p[0] & 0x30) >> 4);
532         f->y = ((p[2] & 0x7f) << 4) | (p[4] & 0x0f);
533         f->z = p[5] & 0x7f;
534
535         alps_decode_buttons_v3(f, p);
536 }
537
538 static void alps_decode_rushmore(struct alps_fields *f, unsigned char *p,
539                                  struct psmouse *psmouse)
540 {
541         alps_decode_pinnacle(f, p, psmouse);
542
543         /* Rushmore's packet decode has a bit difference with Pinnacle's */
544         f->is_mp = !!(p[5] & 0x40);
545         f->fingers = max((p[5] & 0x3), ((p[5] >> 2) & 0x3)) + 1;
546         f->x_map |= (p[5] & 0x10) << 11;
547         f->y_map |= (p[5] & 0x20) << 6;
548 }
549
550 static void alps_decode_dolphin(struct alps_fields *f, unsigned char *p,
551                                 struct psmouse *psmouse)
552 {
553         u64 palm_data = 0;
554         struct alps_data *priv = psmouse->private;
555
556         f->first_mp = !!(p[0] & 0x02);
557         f->is_mp = !!(p[0] & 0x20);
558
559         if (!f->is_mp) {
560                 f->x = ((p[1] & 0x7f) | ((p[4] & 0x0f) << 7));
561                 f->y = ((p[2] & 0x7f) | ((p[4] & 0xf0) << 3));
562                 f->z = (p[0] & 4) ? 0 : p[5] & 0x7f;
563                 alps_decode_buttons_v3(f, p);
564         } else {
565                 f->fingers = ((p[0] & 0x6) >> 1 |
566                      (p[0] & 0x10) >> 2);
567
568                 palm_data = (p[1] & 0x7f) |
569                             ((p[2] & 0x7f) << 7) |
570                             ((p[4] & 0x7f) << 14) |
571                             ((p[5] & 0x7f) << 21) |
572                             ((p[3] & 0x07) << 28) |
573                             (((u64)p[3] & 0x70) << 27) |
574                             (((u64)p[0] & 0x01) << 34);
575
576                 /* Y-profile is stored in P(0) to p(n-1), n = y_bits; */
577                 f->y_map = palm_data & (BIT(priv->y_bits) - 1);
578
579                 /* X-profile is stored in p(n) to p(n+m-1), m = x_bits; */
580                 f->x_map = (palm_data >> priv->y_bits) &
581                            (BIT(priv->x_bits) - 1);
582         }
583 }
584
585 static void alps_process_touchpad_packet_v3_v5(struct psmouse *psmouse)
586 {
587         struct alps_data *priv = psmouse->private;
588         unsigned char *packet = psmouse->packet;
589         struct input_dev *dev = psmouse->dev;
590         struct input_dev *dev2 = priv->dev2;
591         int x1 = 0, y1 = 0, x2 = 0, y2 = 0, fingers = 0;
592         struct alps_fields f = {0};
593
594         priv->decode_fields(&f, packet, psmouse);
595
596         /*
597          * There's no single feature of touchpad position and bitmap packets
598          * that can be used to distinguish between them. We rely on the fact
599          * that a bitmap packet should always follow a position packet with
600          * bit 6 of packet[4] set.
601          */
602         if (priv->multi_packet) {
603                 /*
604                  * Sometimes a position packet will indicate a multi-packet
605                  * sequence, but then what follows is another position
606                  * packet. Check for this, and when it happens process the
607                  * position packet as usual.
608                  */
609                 if (f.is_mp) {
610                         fingers = f.fingers;
611                         if (priv->proto_version == ALPS_PROTO_V3) {
612                                 if (alps_process_bitmap(priv, f.x_map,
613                                                         f.y_map, &x1, &y1,
614                                                         &x2, &y2) == 0)
615                                         fingers = 0; /* Use st data */
616
617                                 /* Now process position packet */
618                                 priv->decode_fields(&f, priv->multi_data,
619                                                     psmouse);
620                         } else {
621                                 /*
622                                  * Because Dolphin uses position packet's
623                                  * coordinate data as Pt1 and uses it to
624                                  * calculate Pt2, so we need to do position
625                                  * packet decode first.
626                                  */
627                                 priv->decode_fields(&f, priv->multi_data,
628                                                     psmouse);
629
630                                 /*
631                                  * Since Dolphin's finger number is reliable,
632                                  * there is no need to compare with bmap_fn.
633                                  */
634                                 alps_process_bitmap_dolphin(priv, &f, &x1, &y1,
635                                                             &x2, &y2);
636                         }
637                 } else {
638                         priv->multi_packet = 0;
639                 }
640         }
641
642         /*
643          * Bit 6 of byte 0 is not usually set in position packets. The only
644          * times it seems to be set is in situations where the data is
645          * suspect anyway, e.g. a palm resting flat on the touchpad. Given
646          * this combined with the fact that this bit is useful for filtering
647          * out misidentified bitmap packets, we reject anything with this
648          * bit set.
649          */
650         if (f.is_mp)
651                 return;
652
653         if (!priv->multi_packet && f.first_mp) {
654                 priv->multi_packet = 1;
655                 memcpy(priv->multi_data, packet, sizeof(priv->multi_data));
656                 return;
657         }
658
659         priv->multi_packet = 0;
660
661         /*
662          * Sometimes the hardware sends a single packet with z = 0
663          * in the middle of a stream. Real releases generate packets
664          * with x, y, and z all zero, so these seem to be flukes.
665          * Ignore them.
666          */
667         if (f.x && f.y && !f.z)
668                 return;
669
670         /*
671          * If we don't have MT data or the bitmaps were empty, we have
672          * to rely on ST data.
673          */
674         if (!fingers) {
675                 x1 = f.x;
676                 y1 = f.y;
677                 fingers = f.z > 0 ? 1 : 0;
678         }
679
680         if (f.z >= 64)
681                 input_report_key(dev, BTN_TOUCH, 1);
682         else
683                 input_report_key(dev, BTN_TOUCH, 0);
684
685         alps_report_semi_mt_data(dev, fingers, x1, y1, x2, y2);
686
687         input_mt_report_finger_count(dev, fingers);
688
689         input_report_key(dev, BTN_LEFT, f.left);
690         input_report_key(dev, BTN_RIGHT, f.right);
691         input_report_key(dev, BTN_MIDDLE, f.middle);
692
693         if (f.z > 0) {
694                 input_report_abs(dev, ABS_X, f.x);
695                 input_report_abs(dev, ABS_Y, f.y);
696         }
697         input_report_abs(dev, ABS_PRESSURE, f.z);
698
699         input_sync(dev);
700
701         if (!(priv->quirks & ALPS_QUIRK_TRACKSTICK_BUTTONS)) {
702                 input_report_key(dev2, BTN_LEFT, f.ts_left);
703                 input_report_key(dev2, BTN_RIGHT, f.ts_right);
704                 input_report_key(dev2, BTN_MIDDLE, f.ts_middle);
705                 input_sync(dev2);
706         }
707 }
708
709 static void alps_process_packet_v3(struct psmouse *psmouse)
710 {
711         unsigned char *packet = psmouse->packet;
712
713         /*
714          * v3 protocol packets come in three types, two representing
715          * touchpad data and one representing trackstick data.
716          * Trackstick packets seem to be distinguished by always
717          * having 0x3f in the last byte. This value has never been
718          * observed in the last byte of either of the other types
719          * of packets.
720          */
721         if (packet[5] == 0x3f) {
722                 alps_process_trackstick_packet_v3(psmouse);
723                 return;
724         }
725
726         alps_process_touchpad_packet_v3_v5(psmouse);
727 }
728
729 static void alps_process_packet_v6(struct psmouse *psmouse)
730 {
731         struct alps_data *priv = psmouse->private;
732         unsigned char *packet = psmouse->packet;
733         struct input_dev *dev = psmouse->dev;
734         struct input_dev *dev2 = priv->dev2;
735         int x, y, z, left, right, middle;
736
737         /*
738          * We can use Byte5 to distinguish if the packet is from Touchpad
739          * or Trackpoint.
740          * Touchpad:    0 - 0x7E
741          * Trackpoint:  0x7F
742          */
743         if (packet[5] == 0x7F) {
744                 /* It should be a DualPoint when received Trackpoint packet */
745                 if (!(priv->flags & ALPS_DUALPOINT))
746                         return;
747
748                 /* Trackpoint packet */
749                 x = packet[1] | ((packet[3] & 0x20) << 2);
750                 y = packet[2] | ((packet[3] & 0x40) << 1);
751                 z = packet[4];
752                 left = packet[3] & 0x01;
753                 right = packet[3] & 0x02;
754                 middle = packet[3] & 0x04;
755
756                 /* To prevent the cursor jump when finger lifted */
757                 if (x == 0x7F && y == 0x7F && z == 0x7F)
758                         x = y = z = 0;
759
760                 /* Divide 4 since trackpoint's speed is too fast */
761                 input_report_rel(dev2, REL_X, (char)x / 4);
762                 input_report_rel(dev2, REL_Y, -((char)y / 4));
763
764                 input_report_key(dev2, BTN_LEFT, left);
765                 input_report_key(dev2, BTN_RIGHT, right);
766                 input_report_key(dev2, BTN_MIDDLE, middle);
767
768                 input_sync(dev2);
769                 return;
770         }
771
772         /* Touchpad packet */
773         x = packet[1] | ((packet[3] & 0x78) << 4);
774         y = packet[2] | ((packet[4] & 0x78) << 4);
775         z = packet[5];
776         left = packet[3] & 0x01;
777         right = packet[3] & 0x02;
778
779         if (z > 30)
780                 input_report_key(dev, BTN_TOUCH, 1);
781         if (z < 25)
782                 input_report_key(dev, BTN_TOUCH, 0);
783
784         if (z > 0) {
785                 input_report_abs(dev, ABS_X, x);
786                 input_report_abs(dev, ABS_Y, y);
787         }
788
789         input_report_abs(dev, ABS_PRESSURE, z);
790         input_report_key(dev, BTN_TOOL_FINGER, z > 0);
791
792         /* v6 touchpad does not have middle button */
793         input_report_key(dev, BTN_LEFT, left);
794         input_report_key(dev, BTN_RIGHT, right);
795
796         input_sync(dev);
797 }
798
799 static void alps_process_packet_v4(struct psmouse *psmouse)
800 {
801         struct alps_data *priv = psmouse->private;
802         unsigned char *packet = psmouse->packet;
803         struct input_dev *dev = psmouse->dev;
804         int offset;
805         int x, y, z;
806         int left, right;
807         int x1, y1, x2, y2;
808         int fingers = 0;
809         unsigned int x_bitmap, y_bitmap;
810
811         /*
812          * v4 has a 6-byte encoding for bitmap data, but this data is
813          * broken up between 3 normal packets. Use priv->multi_packet to
814          * track our position in the bitmap packet.
815          */
816         if (packet[6] & 0x40) {
817                 /* sync, reset position */
818                 priv->multi_packet = 0;
819         }
820
821         if (WARN_ON_ONCE(priv->multi_packet > 2))
822                 return;
823
824         offset = 2 * priv->multi_packet;
825         priv->multi_data[offset] = packet[6];
826         priv->multi_data[offset + 1] = packet[7];
827
828         if (++priv->multi_packet > 2) {
829                 priv->multi_packet = 0;
830
831                 x_bitmap = ((priv->multi_data[2] & 0x1f) << 10) |
832                            ((priv->multi_data[3] & 0x60) << 3) |
833                            ((priv->multi_data[0] & 0x3f) << 2) |
834                            ((priv->multi_data[1] & 0x60) >> 5);
835                 y_bitmap = ((priv->multi_data[5] & 0x01) << 10) |
836                            ((priv->multi_data[3] & 0x1f) << 5) |
837                             (priv->multi_data[1] & 0x1f);
838
839                 fingers = alps_process_bitmap(priv, x_bitmap, y_bitmap,
840                                               &x1, &y1, &x2, &y2);
841
842                 /* Store MT data.*/
843                 priv->fingers = fingers;
844                 priv->x1 = x1;
845                 priv->x2 = x2;
846                 priv->y1 = y1;
847                 priv->y2 = y2;
848         }
849
850         left = packet[4] & 0x01;
851         right = packet[4] & 0x02;
852
853         x = ((packet[1] & 0x7f) << 4) | ((packet[3] & 0x30) >> 2) |
854             ((packet[0] & 0x30) >> 4);
855         y = ((packet[2] & 0x7f) << 4) | (packet[3] & 0x0f);
856         z = packet[5] & 0x7f;
857
858         /*
859          * If there were no contacts in the bitmap, use ST
860          * points in MT reports.
861          * If there were two contacts or more, report MT data.
862          */
863         if (priv->fingers < 2) {
864                 x1 = x;
865                 y1 = y;
866                 fingers = z > 0 ? 1 : 0;
867         } else {
868                 fingers = priv->fingers;
869                 x1 = priv->x1;
870                 x2 = priv->x2;
871                 y1 = priv->y1;
872                 y2 = priv->y2;
873         }
874
875         if (z >= 64)
876                 input_report_key(dev, BTN_TOUCH, 1);
877         else
878                 input_report_key(dev, BTN_TOUCH, 0);
879
880         alps_report_semi_mt_data(dev, fingers, x1, y1, x2, y2);
881
882         input_mt_report_finger_count(dev, fingers);
883
884         input_report_key(dev, BTN_LEFT, left);
885         input_report_key(dev, BTN_RIGHT, right);
886
887         if (z > 0) {
888                 input_report_abs(dev, ABS_X, x);
889                 input_report_abs(dev, ABS_Y, y);
890         }
891         input_report_abs(dev, ABS_PRESSURE, z);
892
893         input_sync(dev);
894 }
895
896 static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
897                                         unsigned char packet[],
898                                         bool report_buttons)
899 {
900         struct alps_data *priv = psmouse->private;
901         struct input_dev *dev2 = priv->dev2;
902
903         if (report_buttons)
904                 alps_report_buttons(psmouse, dev2, psmouse->dev,
905                                 packet[0] & 1, packet[0] & 2, packet[0] & 4);
906
907         input_report_rel(dev2, REL_X,
908                 packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
909         input_report_rel(dev2, REL_Y,
910                 packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
911
912         input_sync(dev2);
913 }
914
915 static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
916 {
917         struct alps_data *priv = psmouse->private;
918
919         if (psmouse->pktcnt < 6)
920                 return PSMOUSE_GOOD_DATA;
921
922         if (psmouse->pktcnt == 6) {
923                 /*
924                  * Start a timer to flush the packet if it ends up last
925                  * 6-byte packet in the stream. Timer needs to fire
926                  * psmouse core times out itself. 20 ms should be enough
927                  * to decide if we are getting more data or not.
928                  */
929                 mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
930                 return PSMOUSE_GOOD_DATA;
931         }
932
933         del_timer(&priv->timer);
934
935         if (psmouse->packet[6] & 0x80) {
936
937                 /*
938                  * Highest bit is set - that means we either had
939                  * complete ALPS packet and this is start of the
940                  * next packet or we got garbage.
941                  */
942
943                 if (((psmouse->packet[3] |
944                       psmouse->packet[4] |
945                       psmouse->packet[5]) & 0x80) ||
946                     (!alps_is_valid_first_byte(priv, psmouse->packet[6]))) {
947                         psmouse_dbg(psmouse,
948                                     "refusing packet %4ph (suspected interleaved ps/2)\n",
949                                     psmouse->packet + 3);
950                         return PSMOUSE_BAD_DATA;
951                 }
952
953                 priv->process_packet(psmouse);
954
955                 /* Continue with the next packet */
956                 psmouse->packet[0] = psmouse->packet[6];
957                 psmouse->pktcnt = 1;
958
959         } else {
960
961                 /*
962                  * High bit is 0 - that means that we indeed got a PS/2
963                  * packet in the middle of ALPS packet.
964                  *
965                  * There is also possibility that we got 6-byte ALPS
966                  * packet followed  by 3-byte packet from trackpoint. We
967                  * can not distinguish between these 2 scenarios but
968                  * because the latter is unlikely to happen in course of
969                  * normal operation (user would need to press all
970                  * buttons on the pad and start moving trackpoint
971                  * without touching the pad surface) we assume former.
972                  * Even if we are wrong the wost thing that would happen
973                  * the cursor would jump but we should not get protocol
974                  * de-synchronization.
975                  */
976
977                 alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
978                                             false);
979
980                 /*
981                  * Continue with the standard ALPS protocol handling,
982                  * but make sure we won't process it as an interleaved
983                  * packet again, which may happen if all buttons are
984                  * pressed. To avoid this let's reset the 4th bit which
985                  * is normally 1.
986                  */
987                 psmouse->packet[3] = psmouse->packet[6] & 0xf7;
988                 psmouse->pktcnt = 4;
989         }
990
991         return PSMOUSE_GOOD_DATA;
992 }
993
994 static void alps_flush_packet(unsigned long data)
995 {
996         struct psmouse *psmouse = (struct psmouse *)data;
997         struct alps_data *priv = psmouse->private;
998
999         serio_pause_rx(psmouse->ps2dev.serio);
1000
1001         if (psmouse->pktcnt == psmouse->pktsize) {
1002
1003                 /*
1004                  * We did not any more data in reasonable amount of time.
1005                  * Validate the last 3 bytes and process as a standard
1006                  * ALPS packet.
1007                  */
1008                 if ((psmouse->packet[3] |
1009                      psmouse->packet[4] |
1010                      psmouse->packet[5]) & 0x80) {
1011                         psmouse_dbg(psmouse,
1012                                     "refusing packet %3ph (suspected interleaved ps/2)\n",
1013                                     psmouse->packet + 3);
1014                 } else {
1015                         priv->process_packet(psmouse);
1016                 }
1017                 psmouse->pktcnt = 0;
1018         }
1019
1020         serio_continue_rx(psmouse->ps2dev.serio);
1021 }
1022
1023 static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
1024 {
1025         struct alps_data *priv = psmouse->private;
1026
1027         if ((psmouse->packet[0] & 0xc8) == 0x08) { /* PS/2 packet */
1028                 if (psmouse->pktcnt == 3) {
1029                         alps_report_bare_ps2_packet(psmouse, psmouse->packet,
1030                                                     true);
1031                         return PSMOUSE_FULL_PACKET;
1032                 }
1033                 return PSMOUSE_GOOD_DATA;
1034         }
1035
1036         /* Check for PS/2 packet stuffed in the middle of ALPS packet. */
1037
1038         if ((priv->flags & ALPS_PS2_INTERLEAVED) &&
1039             psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
1040                 return alps_handle_interleaved_ps2(psmouse);
1041         }
1042
1043         if (!alps_is_valid_first_byte(priv, psmouse->packet[0])) {
1044                 psmouse_dbg(psmouse,
1045                             "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
1046                             psmouse->packet[0], priv->mask0, priv->byte0);
1047                 return PSMOUSE_BAD_DATA;
1048         }
1049
1050         /* Bytes 2 - pktsize should have 0 in the highest bit */
1051         if ((priv->proto_version < ALPS_PROTO_V5) &&
1052             psmouse->pktcnt >= 2 && psmouse->pktcnt <= psmouse->pktsize &&
1053             (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
1054                 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
1055                             psmouse->pktcnt - 1,
1056                             psmouse->packet[psmouse->pktcnt - 1]);
1057                 return PSMOUSE_BAD_DATA;
1058         }
1059
1060         if (psmouse->pktcnt == psmouse->pktsize) {
1061                 priv->process_packet(psmouse);
1062                 return PSMOUSE_FULL_PACKET;
1063         }
1064
1065         return PSMOUSE_GOOD_DATA;
1066 }
1067
1068 static int alps_command_mode_send_nibble(struct psmouse *psmouse, int nibble)
1069 {
1070         struct ps2dev *ps2dev = &psmouse->ps2dev;
1071         struct alps_data *priv = psmouse->private;
1072         int command;
1073         unsigned char *param;
1074         unsigned char dummy[4];
1075
1076         BUG_ON(nibble > 0xf);
1077
1078         command = priv->nibble_commands[nibble].command;
1079         param = (command & 0x0f00) ?
1080                 dummy : (unsigned char *)&priv->nibble_commands[nibble].data;
1081
1082         if (ps2_command(ps2dev, param, command))
1083                 return -1;
1084
1085         return 0;
1086 }
1087
1088 static int alps_command_mode_set_addr(struct psmouse *psmouse, int addr)
1089 {
1090         struct ps2dev *ps2dev = &psmouse->ps2dev;
1091         struct alps_data *priv = psmouse->private;
1092         int i, nibble;
1093
1094         if (ps2_command(ps2dev, NULL, priv->addr_command))
1095                 return -1;
1096
1097         for (i = 12; i >= 0; i -= 4) {
1098                 nibble = (addr >> i) & 0xf;
1099                 if (alps_command_mode_send_nibble(psmouse, nibble))
1100                         return -1;
1101         }
1102
1103         return 0;
1104 }
1105
1106 static int __alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1107 {
1108         struct ps2dev *ps2dev = &psmouse->ps2dev;
1109         unsigned char param[4];
1110
1111         if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1112                 return -1;
1113
1114         /*
1115          * The address being read is returned in the first two bytes
1116          * of the result. Check that this address matches the expected
1117          * address.
1118          */
1119         if (addr != ((param[0] << 8) | param[1]))
1120                 return -1;
1121
1122         return param[2];
1123 }
1124
1125 static int alps_command_mode_read_reg(struct psmouse *psmouse, int addr)
1126 {
1127         if (alps_command_mode_set_addr(psmouse, addr))
1128                 return -1;
1129         return __alps_command_mode_read_reg(psmouse, addr);
1130 }
1131
1132 static int __alps_command_mode_write_reg(struct psmouse *psmouse, u8 value)
1133 {
1134         if (alps_command_mode_send_nibble(psmouse, (value >> 4) & 0xf))
1135                 return -1;
1136         if (alps_command_mode_send_nibble(psmouse, value & 0xf))
1137                 return -1;
1138         return 0;
1139 }
1140
1141 static int alps_command_mode_write_reg(struct psmouse *psmouse, int addr,
1142                                        u8 value)
1143 {
1144         if (alps_command_mode_set_addr(psmouse, addr))
1145                 return -1;
1146         return __alps_command_mode_write_reg(psmouse, value);
1147 }
1148
1149 static int alps_rpt_cmd(struct psmouse *psmouse, int init_command,
1150                         int repeated_command, unsigned char *param)
1151 {
1152         struct ps2dev *ps2dev = &psmouse->ps2dev;
1153
1154         param[0] = 0;
1155         if (init_command && ps2_command(ps2dev, param, init_command))
1156                 return -EIO;
1157
1158         if (ps2_command(ps2dev,  NULL, repeated_command) ||
1159             ps2_command(ps2dev,  NULL, repeated_command) ||
1160             ps2_command(ps2dev,  NULL, repeated_command))
1161                 return -EIO;
1162
1163         param[0] = param[1] = param[2] = 0xff;
1164         if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1165                 return -EIO;
1166
1167         psmouse_dbg(psmouse, "%2.2X report: %3ph\n",
1168                     repeated_command, param);
1169         return 0;
1170 }
1171
1172 static int alps_enter_command_mode(struct psmouse *psmouse)
1173 {
1174         unsigned char param[4];
1175
1176         if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_RESET_WRAP, param)) {
1177                 psmouse_err(psmouse, "failed to enter command mode\n");
1178                 return -1;
1179         }
1180
1181         if ((param[0] != 0x88 || (param[1] != 0x07 && param[1] != 0x08)) &&
1182             param[0] != 0x73) {
1183                 psmouse_dbg(psmouse,
1184                             "unknown response while entering command mode\n");
1185                 return -1;
1186         }
1187         return 0;
1188 }
1189
1190 static inline int alps_exit_command_mode(struct psmouse *psmouse)
1191 {
1192         struct ps2dev *ps2dev = &psmouse->ps2dev;
1193         if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM))
1194                 return -1;
1195         return 0;
1196 }
1197
1198 /*
1199  * For DualPoint devices select the device that should respond to
1200  * subsequent commands. It looks like glidepad is behind stickpointer,
1201  * I'd thought it would be other way around...
1202  */
1203 static int alps_passthrough_mode_v2(struct psmouse *psmouse, bool enable)
1204 {
1205         struct ps2dev *ps2dev = &psmouse->ps2dev;
1206         int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
1207
1208         if (ps2_command(ps2dev, NULL, cmd) ||
1209             ps2_command(ps2dev, NULL, cmd) ||
1210             ps2_command(ps2dev, NULL, cmd) ||
1211             ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1212                 return -1;
1213
1214         /* we may get 3 more bytes, just ignore them */
1215         ps2_drain(ps2dev, 3, 100);
1216
1217         return 0;
1218 }
1219
1220 static int alps_absolute_mode_v1_v2(struct psmouse *psmouse)
1221 {
1222         struct ps2dev *ps2dev = &psmouse->ps2dev;
1223
1224         /* Try ALPS magic knock - 4 disable before enable */
1225         if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1226             ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1227             ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1228             ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1229             ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
1230                 return -1;
1231
1232         /*
1233          * Switch mouse to poll (remote) mode so motion data will not
1234          * get in our way
1235          */
1236         return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
1237 }
1238
1239 static int alps_monitor_mode_send_word(struct psmouse *psmouse, u16 word)
1240 {
1241         int i, nibble;
1242
1243         /*
1244          * b0-b11 are valid bits, send sequence is inverse.
1245          * e.g. when word = 0x0123, nibble send sequence is 3, 2, 1
1246          */
1247         for (i = 0; i <= 8; i += 4) {
1248                 nibble = (word >> i) & 0xf;
1249                 if (alps_command_mode_send_nibble(psmouse, nibble))
1250                         return -1;
1251         }
1252
1253         return 0;
1254 }
1255
1256 static int alps_monitor_mode_write_reg(struct psmouse *psmouse,
1257                                        u16 addr, u16 value)
1258 {
1259         struct ps2dev *ps2dev = &psmouse->ps2dev;
1260
1261         /* 0x0A0 is the command to write the word */
1262         if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE) ||
1263             alps_monitor_mode_send_word(psmouse, 0x0A0) ||
1264             alps_monitor_mode_send_word(psmouse, addr) ||
1265             alps_monitor_mode_send_word(psmouse, value) ||
1266             ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
1267                 return -1;
1268
1269         return 0;
1270 }
1271
1272 static int alps_monitor_mode(struct psmouse *psmouse, bool enable)
1273 {
1274         struct ps2dev *ps2dev = &psmouse->ps2dev;
1275
1276         if (enable) {
1277                 /* EC E9 F5 F5 E7 E6 E7 E9 to enter monitor mode */
1278                 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
1279                     ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO) ||
1280                     ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1281                     ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1282                     ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1283                     ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1284                     ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
1285                     ps2_command(ps2dev, NULL, PSMOUSE_CMD_GETINFO))
1286                         return -1;
1287         } else {
1288                 /* EC to exit monitor mode */
1289                 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP))
1290                         return -1;
1291         }
1292
1293         return 0;
1294 }
1295
1296 static int alps_absolute_mode_v6(struct psmouse *psmouse)
1297 {
1298         u16 reg_val = 0x181;
1299         int ret = -1;
1300
1301         /* enter monitor mode, to write the register */
1302         if (alps_monitor_mode(psmouse, true))
1303                 return -1;
1304
1305         ret = alps_monitor_mode_write_reg(psmouse, 0x000, reg_val);
1306
1307         if (alps_monitor_mode(psmouse, false))
1308                 ret = -1;
1309
1310         return ret;
1311 }
1312
1313 static int alps_get_status(struct psmouse *psmouse, char *param)
1314 {
1315         /* Get status: 0xF5 0xF5 0xF5 0xE9 */
1316         if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_DISABLE, param))
1317                 return -1;
1318
1319         return 0;
1320 }
1321
1322 /*
1323  * Turn touchpad tapping on or off. The sequences are:
1324  * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
1325  * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
1326  * My guess that 0xE9 (GetInfo) is here as a sync point.
1327  * For models that also have stickpointer (DualPoints) its tapping
1328  * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
1329  * we don't fiddle with it.
1330  */
1331 static int alps_tap_mode(struct psmouse *psmouse, int enable)
1332 {
1333         struct ps2dev *ps2dev = &psmouse->ps2dev;
1334         int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
1335         unsigned char tap_arg = enable ? 0x0A : 0x00;
1336         unsigned char param[4];
1337
1338         if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
1339             ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1340             ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
1341             ps2_command(ps2dev, &tap_arg, cmd))
1342                 return -1;
1343
1344         if (alps_get_status(psmouse, param))
1345                 return -1;
1346
1347         return 0;
1348 }
1349
1350 /*
1351  * alps_poll() - poll the touchpad for current motion packet.
1352  * Used in resync.
1353  */
1354 static int alps_poll(struct psmouse *psmouse)
1355 {
1356         struct alps_data *priv = psmouse->private;
1357         unsigned char buf[sizeof(psmouse->packet)];
1358         bool poll_failed;
1359
1360         if (priv->flags & ALPS_PASS)
1361                 alps_passthrough_mode_v2(psmouse, true);
1362
1363         poll_failed = ps2_command(&psmouse->ps2dev, buf,
1364                                   PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
1365
1366         if (priv->flags & ALPS_PASS)
1367                 alps_passthrough_mode_v2(psmouse, false);
1368
1369         if (poll_failed || (buf[0] & priv->mask0) != priv->byte0)
1370                 return -1;
1371
1372         if ((psmouse->badbyte & 0xc8) == 0x08) {
1373 /*
1374  * Poll the track stick ...
1375  */
1376                 if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
1377                         return -1;
1378         }
1379
1380         memcpy(psmouse->packet, buf, sizeof(buf));
1381         return 0;
1382 }
1383
1384 static int alps_hw_init_v1_v2(struct psmouse *psmouse)
1385 {
1386         struct alps_data *priv = psmouse->private;
1387
1388         if ((priv->flags & ALPS_PASS) &&
1389             alps_passthrough_mode_v2(psmouse, true)) {
1390                 return -1;
1391         }
1392
1393         if (alps_tap_mode(psmouse, true)) {
1394                 psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
1395                 return -1;
1396         }
1397
1398         if (alps_absolute_mode_v1_v2(psmouse)) {
1399                 psmouse_err(psmouse, "Failed to enable absolute mode\n");
1400                 return -1;
1401         }
1402
1403         if ((priv->flags & ALPS_PASS) &&
1404             alps_passthrough_mode_v2(psmouse, false)) {
1405                 return -1;
1406         }
1407
1408         /* ALPS needs stream mode, otherwise it won't report any data */
1409         if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
1410                 psmouse_err(psmouse, "Failed to enable stream mode\n");
1411                 return -1;
1412         }
1413
1414         return 0;
1415 }
1416
1417 static int alps_hw_init_v6(struct psmouse *psmouse)
1418 {
1419         unsigned char param[2] = {0xC8, 0x14};
1420
1421         /* Enter passthrough mode to let trackpoint enter 6byte raw mode */
1422         if (alps_passthrough_mode_v2(psmouse, true))
1423                 return -1;
1424
1425         if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1426             ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1427             ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1428             ps2_command(&psmouse->ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
1429             ps2_command(&psmouse->ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
1430                 return -1;
1431
1432         if (alps_passthrough_mode_v2(psmouse, false))
1433                 return -1;
1434
1435         if (alps_absolute_mode_v6(psmouse)) {
1436                 psmouse_err(psmouse, "Failed to enable absolute mode\n");
1437                 return -1;
1438         }
1439
1440         return 0;
1441 }
1442
1443 /*
1444  * Enable or disable passthrough mode to the trackstick.
1445  */
1446 static int alps_passthrough_mode_v3(struct psmouse *psmouse,
1447                                     int reg_base, bool enable)
1448 {
1449         int reg_val, ret = -1;
1450
1451         if (alps_enter_command_mode(psmouse))
1452                 return -1;
1453
1454         reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x0008);
1455         if (reg_val == -1)
1456                 goto error;
1457
1458         if (enable)
1459                 reg_val |= 0x01;
1460         else
1461                 reg_val &= ~0x01;
1462
1463         ret = __alps_command_mode_write_reg(psmouse, reg_val);
1464
1465 error:
1466         if (alps_exit_command_mode(psmouse))
1467                 ret = -1;
1468         return ret;
1469 }
1470
1471 /* Must be in command mode when calling this function */
1472 static int alps_absolute_mode_v3(struct psmouse *psmouse)
1473 {
1474         int reg_val;
1475
1476         reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
1477         if (reg_val == -1)
1478                 return -1;
1479
1480         reg_val |= 0x06;
1481         if (__alps_command_mode_write_reg(psmouse, reg_val))
1482                 return -1;
1483
1484         return 0;
1485 }
1486
1487 static int alps_probe_trackstick_v3(struct psmouse *psmouse, int reg_base)
1488 {
1489         int ret = -EIO, reg_val;
1490
1491         if (alps_enter_command_mode(psmouse))
1492                 goto error;
1493
1494         reg_val = alps_command_mode_read_reg(psmouse, reg_base + 0x08);
1495         if (reg_val == -1)
1496                 goto error;
1497
1498         /* bit 7: trackstick is present */
1499         ret = reg_val & 0x80 ? 0 : -ENODEV;
1500
1501 error:
1502         alps_exit_command_mode(psmouse);
1503         return ret;
1504 }
1505
1506 static int alps_setup_trackstick_v3(struct psmouse *psmouse, int reg_base)
1507 {
1508         struct ps2dev *ps2dev = &psmouse->ps2dev;
1509         int ret = 0;
1510         unsigned char param[4];
1511
1512         if (alps_passthrough_mode_v3(psmouse, reg_base, true))
1513                 return -EIO;
1514
1515         /*
1516          * E7 report for the trackstick
1517          *
1518          * There have been reports of failures to seem to trace back
1519          * to the above trackstick check failing. When these occur
1520          * this E7 report fails, so when that happens we continue
1521          * with the assumption that there isn't a trackstick after
1522          * all.
1523          */
1524         if (alps_rpt_cmd(psmouse, 0, PSMOUSE_CMD_SETSCALE21, param)) {
1525                 psmouse_warn(psmouse, "trackstick E7 report failed\n");
1526                 ret = -ENODEV;
1527         } else {
1528                 psmouse_dbg(psmouse, "trackstick E7 report: %3ph\n", param);
1529
1530                 /*
1531                  * Not sure what this does, but it is absolutely
1532                  * essential. Without it, the touchpad does not
1533                  * work at all and the trackstick just emits normal
1534                  * PS/2 packets.
1535                  */
1536                 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1537                     ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1538                     ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
1539                     alps_command_mode_send_nibble(psmouse, 0x9) ||
1540                     alps_command_mode_send_nibble(psmouse, 0x4)) {
1541                         psmouse_err(psmouse,
1542                                     "Error sending magic E6 sequence\n");
1543                         ret = -EIO;
1544                         goto error;
1545                 }
1546
1547                 /*
1548                  * This ensures the trackstick packets are in the format
1549                  * supported by this driver. If bit 1 isn't set the packet
1550                  * format is different.
1551                  */
1552                 if (alps_enter_command_mode(psmouse) ||
1553                     alps_command_mode_write_reg(psmouse,
1554                                                 reg_base + 0x08, 0x82) ||
1555                     alps_exit_command_mode(psmouse))
1556                         ret = -EIO;
1557         }
1558
1559 error:
1560         if (alps_passthrough_mode_v3(psmouse, reg_base, false))
1561                 ret = -EIO;
1562
1563         return ret;
1564 }
1565
1566 static int alps_hw_init_v3(struct psmouse *psmouse)
1567 {
1568         struct ps2dev *ps2dev = &psmouse->ps2dev;
1569         int reg_val;
1570         unsigned char param[4];
1571
1572         reg_val = alps_probe_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE);
1573         if (reg_val == -EIO)
1574                 goto error;
1575
1576         if (reg_val == 0 &&
1577             alps_setup_trackstick_v3(psmouse, ALPS_REG_BASE_PINNACLE) == -EIO)
1578                 goto error;
1579
1580         if (alps_enter_command_mode(psmouse) ||
1581             alps_absolute_mode_v3(psmouse)) {
1582                 psmouse_err(psmouse, "Failed to enter absolute mode\n");
1583                 goto error;
1584         }
1585
1586         reg_val = alps_command_mode_read_reg(psmouse, 0x0006);
1587         if (reg_val == -1)
1588                 goto error;
1589         if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
1590                 goto error;
1591
1592         reg_val = alps_command_mode_read_reg(psmouse, 0x0007);
1593         if (reg_val == -1)
1594                 goto error;
1595         if (__alps_command_mode_write_reg(psmouse, reg_val | 0x01))
1596                 goto error;
1597
1598         if (alps_command_mode_read_reg(psmouse, 0x0144) == -1)
1599                 goto error;
1600         if (__alps_command_mode_write_reg(psmouse, 0x04))
1601                 goto error;
1602
1603         if (alps_command_mode_read_reg(psmouse, 0x0159) == -1)
1604                 goto error;
1605         if (__alps_command_mode_write_reg(psmouse, 0x03))
1606                 goto error;
1607
1608         if (alps_command_mode_read_reg(psmouse, 0x0163) == -1)
1609                 goto error;
1610         if (alps_command_mode_write_reg(psmouse, 0x0163, 0x03))
1611                 goto error;
1612
1613         if (alps_command_mode_read_reg(psmouse, 0x0162) == -1)
1614                 goto error;
1615         if (alps_command_mode_write_reg(psmouse, 0x0162, 0x04))
1616                 goto error;
1617
1618         alps_exit_command_mode(psmouse);
1619
1620         /* Set rate and enable data reporting */
1621         param[0] = 0x64;
1622         if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
1623             ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1624                 psmouse_err(psmouse, "Failed to enable data reporting\n");
1625                 return -1;
1626         }
1627
1628         return 0;
1629
1630 error:
1631         /*
1632          * Leaving the touchpad in command mode will essentially render
1633          * it unusable until the machine reboots, so exit it here just
1634          * to be safe
1635          */
1636         alps_exit_command_mode(psmouse);
1637         return -1;
1638 }
1639
1640 static int alps_hw_init_rushmore_v3(struct psmouse *psmouse)
1641 {
1642         struct alps_data *priv = psmouse->private;
1643         struct ps2dev *ps2dev = &psmouse->ps2dev;
1644         int reg_val, ret = -1;
1645
1646         if (priv->flags & ALPS_DUALPOINT) {
1647                 reg_val = alps_setup_trackstick_v3(psmouse,
1648                                                    ALPS_REG_BASE_RUSHMORE);
1649                 if (reg_val == -EIO)
1650                         goto error;
1651                 if (reg_val == -ENODEV)
1652                         priv->flags &= ~ALPS_DUALPOINT;
1653         }
1654
1655         if (alps_enter_command_mode(psmouse) ||
1656             alps_command_mode_read_reg(psmouse, 0xc2d9) == -1 ||
1657             alps_command_mode_write_reg(psmouse, 0xc2cb, 0x00))
1658                 goto error;
1659
1660         reg_val = alps_command_mode_read_reg(psmouse, 0xc2c6);
1661         if (reg_val == -1)
1662                 goto error;
1663         if (__alps_command_mode_write_reg(psmouse, reg_val & 0xfd))
1664                 goto error;
1665
1666         if (alps_command_mode_write_reg(psmouse, 0xc2c9, 0x64))
1667                 goto error;
1668
1669         /* enter absolute mode */
1670         reg_val = alps_command_mode_read_reg(psmouse, 0xc2c4);
1671         if (reg_val == -1)
1672                 goto error;
1673         if (__alps_command_mode_write_reg(psmouse, reg_val | 0x02))
1674                 goto error;
1675
1676         alps_exit_command_mode(psmouse);
1677         return ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
1678
1679 error:
1680         alps_exit_command_mode(psmouse);
1681         return ret;
1682 }
1683
1684 /* Must be in command mode when calling this function */
1685 static int alps_absolute_mode_v4(struct psmouse *psmouse)
1686 {
1687         int reg_val;
1688
1689         reg_val = alps_command_mode_read_reg(psmouse, 0x0004);
1690         if (reg_val == -1)
1691                 return -1;
1692
1693         reg_val |= 0x02;
1694         if (__alps_command_mode_write_reg(psmouse, reg_val))
1695                 return -1;
1696
1697         return 0;
1698 }
1699
1700 static int alps_hw_init_v4(struct psmouse *psmouse)
1701 {
1702         struct ps2dev *ps2dev = &psmouse->ps2dev;
1703         unsigned char param[4];
1704
1705         if (alps_enter_command_mode(psmouse))
1706                 goto error;
1707
1708         if (alps_absolute_mode_v4(psmouse)) {
1709                 psmouse_err(psmouse, "Failed to enter absolute mode\n");
1710                 goto error;
1711         }
1712
1713         if (alps_command_mode_write_reg(psmouse, 0x0007, 0x8c))
1714                 goto error;
1715
1716         if (alps_command_mode_write_reg(psmouse, 0x0149, 0x03))
1717                 goto error;
1718
1719         if (alps_command_mode_write_reg(psmouse, 0x0160, 0x03))
1720                 goto error;
1721
1722         if (alps_command_mode_write_reg(psmouse, 0x017f, 0x15))
1723                 goto error;
1724
1725         if (alps_command_mode_write_reg(psmouse, 0x0151, 0x01))
1726                 goto error;
1727
1728         if (alps_command_mode_write_reg(psmouse, 0x0168, 0x03))
1729                 goto error;
1730
1731         if (alps_command_mode_write_reg(psmouse, 0x014a, 0x03))
1732                 goto error;
1733
1734         if (alps_command_mode_write_reg(psmouse, 0x0161, 0x03))
1735                 goto error;
1736
1737         alps_exit_command_mode(psmouse);
1738
1739         /*
1740          * This sequence changes the output from a 9-byte to an
1741          * 8-byte format. All the same data seems to be present,
1742          * just in a more compact format.
1743          */
1744         param[0] = 0xc8;
1745         param[1] = 0x64;
1746         param[2] = 0x50;
1747         if (ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
1748             ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE) ||
1749             ps2_command(ps2dev, &param[2], PSMOUSE_CMD_SETRATE) ||
1750             ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
1751                 return -1;
1752
1753         /* Set rate and enable data reporting */
1754         param[0] = 0x64;
1755         if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE) ||
1756             ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1757                 psmouse_err(psmouse, "Failed to enable data reporting\n");
1758                 return -1;
1759         }
1760
1761         return 0;
1762
1763 error:
1764         /*
1765          * Leaving the touchpad in command mode will essentially render
1766          * it unusable until the machine reboots, so exit it here just
1767          * to be safe
1768          */
1769         alps_exit_command_mode(psmouse);
1770         return -1;
1771 }
1772
1773 static int alps_dolphin_get_device_area(struct psmouse *psmouse,
1774                                         struct alps_data *priv)
1775 {
1776         struct ps2dev *ps2dev = &psmouse->ps2dev;
1777         unsigned char param[4] = {0};
1778         int num_x_electrode, num_y_electrode;
1779
1780         if (alps_enter_command_mode(psmouse))
1781                 return -1;
1782
1783         param[0] = 0x0a;
1784         if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_WRAP) ||
1785             ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
1786             ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETPOLL) ||
1787             ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
1788             ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE))
1789                 return -1;
1790
1791         if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
1792                 return -1;
1793
1794         /*
1795          * Dolphin's sensor line number is not fixed. It can be calculated
1796          * by adding the device's register value with DOLPHIN_PROFILE_X/YOFFSET.
1797          * Further more, we can get device's x_max and y_max by multiplying
1798          * sensor line number with DOLPHIN_COUNT_PER_ELECTRODE.
1799          *
1800          * e.g. When we get register's sensor_x = 11 & sensor_y = 8,
1801          *      real sensor line number X = 11 + 8 = 19, and
1802          *      real sensor line number Y = 8 + 1 = 9.
1803          *      So, x_max = (19 - 1) * 64 = 1152, and
1804          *          y_max = (9 - 1) * 64 = 512.
1805          */
1806         num_x_electrode = DOLPHIN_PROFILE_XOFFSET + (param[2] & 0x0F);
1807         num_y_electrode = DOLPHIN_PROFILE_YOFFSET + ((param[2] >> 4) & 0x0F);
1808         priv->x_bits = num_x_electrode;
1809         priv->y_bits = num_y_electrode;
1810         priv->x_max = (num_x_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
1811         priv->y_max = (num_y_electrode - 1) * DOLPHIN_COUNT_PER_ELECTRODE;
1812
1813         if (alps_exit_command_mode(psmouse))
1814                 return -1;
1815
1816         return 0;
1817 }
1818
1819 static int alps_hw_init_dolphin_v1(struct psmouse *psmouse)
1820 {
1821         struct ps2dev *ps2dev = &psmouse->ps2dev;
1822         unsigned char param[2];
1823
1824         /* This is dolphin "v1" as empirically defined by florin9doi */
1825         param[0] = 0x64;
1826         param[1] = 0x28;
1827
1828         if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSTREAM) ||
1829             ps2_command(ps2dev, &param[0], PSMOUSE_CMD_SETRATE) ||
1830             ps2_command(ps2dev, &param[1], PSMOUSE_CMD_SETRATE))
1831                 return -1;
1832
1833         return 0;
1834 }
1835
1836 static void alps_set_defaults(struct alps_data *priv)
1837 {
1838         priv->byte0 = 0x8f;
1839         priv->mask0 = 0x8f;
1840         priv->flags = ALPS_DUALPOINT;
1841
1842         priv->x_max = 2000;
1843         priv->y_max = 1400;
1844         priv->x_bits = 15;
1845         priv->y_bits = 11;
1846
1847         switch (priv->proto_version) {
1848         case ALPS_PROTO_V1:
1849         case ALPS_PROTO_V2:
1850                 priv->hw_init = alps_hw_init_v1_v2;
1851                 priv->process_packet = alps_process_packet_v1_v2;
1852                 priv->set_abs_params = alps_set_abs_params_st;
1853                 priv->x_max = 1023;
1854                 priv->y_max = 767;
1855                 break;
1856         case ALPS_PROTO_V3:
1857                 priv->hw_init = alps_hw_init_v3;
1858                 priv->process_packet = alps_process_packet_v3;
1859                 priv->set_abs_params = alps_set_abs_params_mt;
1860                 priv->decode_fields = alps_decode_pinnacle;
1861                 priv->nibble_commands = alps_v3_nibble_commands;
1862                 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
1863                 break;
1864         case ALPS_PROTO_V4:
1865                 priv->hw_init = alps_hw_init_v4;
1866                 priv->process_packet = alps_process_packet_v4;
1867                 priv->set_abs_params = alps_set_abs_params_mt;
1868                 priv->nibble_commands = alps_v4_nibble_commands;
1869                 priv->addr_command = PSMOUSE_CMD_DISABLE;
1870                 break;
1871         case ALPS_PROTO_V5:
1872                 priv->hw_init = alps_hw_init_dolphin_v1;
1873                 priv->process_packet = alps_process_touchpad_packet_v3_v5;
1874                 priv->decode_fields = alps_decode_dolphin;
1875                 priv->set_abs_params = alps_set_abs_params_mt;
1876                 priv->nibble_commands = alps_v3_nibble_commands;
1877                 priv->addr_command = PSMOUSE_CMD_RESET_WRAP;
1878                 priv->byte0 = 0xc8;
1879                 priv->mask0 = 0xd8;
1880                 priv->flags = 0;
1881                 priv->x_max = 1360;
1882                 priv->y_max = 660;
1883                 priv->x_bits = 23;
1884                 priv->y_bits = 12;
1885                 break;
1886         case ALPS_PROTO_V6:
1887                 priv->hw_init = alps_hw_init_v6;
1888                 priv->process_packet = alps_process_packet_v6;
1889                 priv->set_abs_params = alps_set_abs_params_st;
1890                 priv->nibble_commands = alps_v6_nibble_commands;
1891                 priv->x_max = 2047;
1892                 priv->y_max = 1535;
1893                 break;
1894         }
1895 }
1896
1897 static int alps_match_table(struct psmouse *psmouse, struct alps_data *priv,
1898                             unsigned char *e7, unsigned char *ec)
1899 {
1900         const struct alps_model_info *model;
1901         int i;
1902
1903         for (i = 0; i < ARRAY_SIZE(alps_model_data); i++) {
1904                 model = &alps_model_data[i];
1905
1906                 if (!memcmp(e7, model->signature, sizeof(model->signature)) &&
1907                     (!model->command_mode_resp ||
1908                      model->command_mode_resp == ec[2])) {
1909
1910                         priv->proto_version = model->proto_version;
1911                         alps_set_defaults(priv);
1912
1913                         priv->flags = model->flags;
1914                         priv->byte0 = model->byte0;
1915                         priv->mask0 = model->mask0;
1916
1917                         return 0;
1918                 }
1919         }
1920
1921         return -EINVAL;
1922 }
1923
1924 static int alps_identify(struct psmouse *psmouse, struct alps_data *priv)
1925 {
1926         unsigned char e6[4], e7[4], ec[4];
1927
1928         /*
1929          * First try "E6 report".
1930          * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
1931          * The bits 0-2 of the first byte will be 1s if some buttons are
1932          * pressed.
1933          */
1934         if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
1935                          PSMOUSE_CMD_SETSCALE11, e6))
1936                 return -EIO;
1937
1938         if ((e6[0] & 0xf8) != 0 || e6[1] != 0 || (e6[2] != 10 && e6[2] != 100))
1939                 return -EINVAL;
1940
1941         /*
1942          * Now get the "E7" and "EC" reports.  These will uniquely identify
1943          * most ALPS touchpads.
1944          */
1945         if (alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
1946                          PSMOUSE_CMD_SETSCALE21, e7) ||
1947             alps_rpt_cmd(psmouse, PSMOUSE_CMD_SETRES,
1948                          PSMOUSE_CMD_RESET_WRAP, ec) ||
1949             alps_exit_command_mode(psmouse))
1950                 return -EIO;
1951
1952         if (alps_match_table(psmouse, priv, e7, ec) == 0) {
1953                 return 0;
1954         } else if (e7[0] == 0x73 && e7[1] == 0x03 && e7[2] == 0x50 &&
1955                    ec[0] == 0x73 && (ec[1] == 0x01 || ec[1] == 0x02)) {
1956                 priv->proto_version = ALPS_PROTO_V5;
1957                 alps_set_defaults(priv);
1958                 if (alps_dolphin_get_device_area(psmouse, priv))
1959                         return -EIO;
1960                 else
1961                         return 0;
1962         } else if (ec[0] == 0x88 && ec[1] == 0x08) {
1963                 priv->proto_version = ALPS_PROTO_V3;
1964                 alps_set_defaults(priv);
1965
1966                 priv->hw_init = alps_hw_init_rushmore_v3;
1967                 priv->decode_fields = alps_decode_rushmore;
1968                 priv->x_bits = 16;
1969                 priv->y_bits = 12;
1970                 priv->flags |= ALPS_IS_RUSHMORE;
1971
1972                 /* hack to make addr_command, nibble_command available */
1973                 psmouse->private = priv;
1974
1975                 if (alps_probe_trackstick_v3(psmouse, ALPS_REG_BASE_RUSHMORE))
1976                         priv->flags &= ~ALPS_DUALPOINT;
1977
1978                 return 0;
1979         } else if (ec[0] == 0x88 && ec[1] == 0x07 &&
1980                    ec[2] >= 0x90 && ec[2] <= 0x9d) {
1981                 priv->proto_version = ALPS_PROTO_V3;
1982                 alps_set_defaults(priv);
1983
1984                 return 0;
1985         }
1986
1987         psmouse_info(psmouse,
1988                      "Unknown ALPS touchpad: E7=%3ph, EC=%3ph\n", e7, ec);
1989
1990         return -EINVAL;
1991 }
1992
1993 static int alps_reconnect(struct psmouse *psmouse)
1994 {
1995         struct alps_data *priv = psmouse->private;
1996
1997         psmouse_reset(psmouse);
1998
1999         if (alps_identify(psmouse, priv) < 0)
2000                 return -1;
2001
2002         return priv->hw_init(psmouse);
2003 }
2004
2005 static void alps_disconnect(struct psmouse *psmouse)
2006 {
2007         struct alps_data *priv = psmouse->private;
2008
2009         psmouse_reset(psmouse);
2010         del_timer_sync(&priv->timer);
2011         input_unregister_device(priv->dev2);
2012         kfree(priv);
2013 }
2014
2015 static void alps_set_abs_params_st(struct alps_data *priv,
2016                                    struct input_dev *dev1)
2017 {
2018         input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
2019         input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
2020 }
2021
2022 static void alps_set_abs_params_mt(struct alps_data *priv,
2023                                    struct input_dev *dev1)
2024 {
2025         set_bit(INPUT_PROP_SEMI_MT, dev1->propbit);
2026         input_mt_init_slots(dev1, 2, 0);
2027         input_set_abs_params(dev1, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
2028         input_set_abs_params(dev1, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
2029
2030         set_bit(BTN_TOOL_DOUBLETAP, dev1->keybit);
2031         set_bit(BTN_TOOL_TRIPLETAP, dev1->keybit);
2032         set_bit(BTN_TOOL_QUADTAP, dev1->keybit);
2033
2034         input_set_abs_params(dev1, ABS_X, 0, priv->x_max, 0, 0);
2035         input_set_abs_params(dev1, ABS_Y, 0, priv->y_max, 0, 0);
2036 }
2037
2038 int alps_init(struct psmouse *psmouse)
2039 {
2040         struct alps_data *priv;
2041         struct input_dev *dev1 = psmouse->dev, *dev2;
2042
2043         priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
2044         dev2 = input_allocate_device();
2045         if (!priv || !dev2)
2046                 goto init_fail;
2047
2048         priv->dev2 = dev2;
2049         setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
2050
2051         psmouse->private = priv;
2052
2053         psmouse_reset(psmouse);
2054
2055         if (alps_identify(psmouse, priv) < 0)
2056                 goto init_fail;
2057
2058         if (priv->hw_init(psmouse))
2059                 goto init_fail;
2060
2061         /*
2062          * Undo part of setup done for us by psmouse core since touchpad
2063          * is not a relative device.
2064          */
2065         __clear_bit(EV_REL, dev1->evbit);
2066         __clear_bit(REL_X, dev1->relbit);
2067         __clear_bit(REL_Y, dev1->relbit);
2068
2069         /*
2070          * Now set up our capabilities.
2071          */
2072         dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
2073         dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
2074         dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
2075         dev1->keybit[BIT_WORD(BTN_LEFT)] |=
2076                 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
2077
2078         dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
2079
2080         priv->set_abs_params(priv, dev1);
2081         input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
2082
2083         if (priv->flags & ALPS_WHEEL) {
2084                 dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
2085                 dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
2086         }
2087
2088         if (priv->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
2089                 dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
2090                 dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
2091         }
2092
2093         if (priv->flags & ALPS_FOUR_BUTTONS) {
2094                 dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
2095                 dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
2096                 dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
2097                 dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
2098         } else {
2099                 dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
2100         }
2101
2102         snprintf(priv->phys, sizeof(priv->phys), "%s/input1", psmouse->ps2dev.serio->phys);
2103         dev2->phys = priv->phys;
2104         dev2->name = (priv->flags & ALPS_DUALPOINT) ?
2105                      "DualPoint Stick" : "ALPS PS/2 Device";
2106         dev2->id.bustype = BUS_I8042;
2107         dev2->id.vendor  = 0x0002;
2108         dev2->id.product = PSMOUSE_ALPS;
2109         dev2->id.version = 0x0000;
2110         dev2->dev.parent = &psmouse->ps2dev.serio->dev;
2111
2112         dev2->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
2113         dev2->relbit[BIT_WORD(REL_X)] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
2114         dev2->keybit[BIT_WORD(BTN_LEFT)] =
2115                 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_MIDDLE) | BIT_MASK(BTN_RIGHT);
2116
2117         if (input_register_device(priv->dev2))
2118                 goto init_fail;
2119
2120         psmouse->protocol_handler = alps_process_byte;
2121         psmouse->poll = alps_poll;
2122         psmouse->disconnect = alps_disconnect;
2123         psmouse->reconnect = alps_reconnect;
2124         psmouse->pktsize = priv->proto_version == ALPS_PROTO_V4 ? 8 : 6;
2125
2126         /* We are having trouble resyncing ALPS touchpads so disable it for now */
2127         psmouse->resync_time = 0;
2128
2129         return 0;
2130
2131 init_fail:
2132         psmouse_reset(psmouse);
2133         input_free_device(dev2);
2134         kfree(priv);
2135         psmouse->private = NULL;
2136         return -1;
2137 }
2138
2139 int alps_detect(struct psmouse *psmouse, bool set_properties)
2140 {
2141         struct alps_data dummy;
2142
2143         if (alps_identify(psmouse, &dummy) < 0)
2144                 return -1;
2145
2146         if (set_properties) {
2147                 psmouse->vendor = "ALPS";
2148                 psmouse->name = dummy.flags & ALPS_DUALPOINT ?
2149                                 "DualPoint TouchPad" : "GlidePoint";
2150                 psmouse->model = dummy.proto_version << 8;
2151         }
2152         return 0;
2153 }
2154