668d715018bbb39390dad3db5fd852685c4cf129
[linux-flexiantxendom0-natty.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/dmi.h>
19 #include <linux/slab.h>
20 #include <linux/input.h>
21 #include <linux/serio.h>
22 #include <linux/libps2.h>
23
24 #include "psmouse.h"
25 #include "alps.h"
26
27 #undef DEBUG
28 #ifdef DEBUG
29 #define dbg(format, arg...) printk(KERN_INFO "alps.c: " format "\n", ## arg)
30 #else
31 #define dbg(format, arg...) do {} while (0)
32 #endif
33
34 #define ALPS_OLDPROTO           0x01    /* old style input */
35 #define ALPS_DUALPOINT          0x02    /* touchpad has trackstick */
36 #define ALPS_PASS               0x04    /* device has a pass-through port */
37
38 #define ALPS_WHEEL              0x08    /* hardware wheel present */
39 #define ALPS_FW_BK_1            0x10    /* front & back buttons present */
40 #define ALPS_FW_BK_2            0x20    /* front & back buttons present */
41 #define ALPS_FOUR_BUTTONS       0x40    /* 4 direction button present */
42 #define ALPS_PS2_INTERLEAVED    0x80    /* 3-byte PS/2 packet interleaved with
43                                            6-byte ALPS packet */
44
45 static const struct alps_model_info alps_model_data[] = {
46         { { 0x32, 0x02, 0x14 }, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* Toshiba Salellite Pro M10 */
47         { { 0x33, 0x02, 0x0a }, 0x88, 0xf8, ALPS_OLDPROTO },              /* UMAX-530T */
48         { { 0x53, 0x02, 0x0a }, 0xf8, 0xf8, 0 },
49         { { 0x53, 0x02, 0x14 }, 0xf8, 0xf8, 0 },
50         { { 0x60, 0x03, 0xc8 }, 0xf8, 0xf8, 0 },                          /* HP ze1115 */
51         { { 0x63, 0x02, 0x0a }, 0xf8, 0xf8, 0 },
52         { { 0x63, 0x02, 0x14 }, 0xf8, 0xf8, 0 },
53         { { 0x63, 0x02, 0x28 }, 0xf8, 0xf8, ALPS_FW_BK_2 },               /* Fujitsu Siemens S6010 */
54         { { 0x63, 0x02, 0x3c }, 0x8f, 0x8f, ALPS_WHEEL },                 /* Toshiba Satellite S2400-103 */
55         { { 0x63, 0x02, 0x50 }, 0xef, 0xef, ALPS_FW_BK_1 },               /* NEC Versa L320 */
56         { { 0x63, 0x02, 0x64 }, 0xf8, 0xf8, 0 },
57         { { 0x63, 0x03, 0xc8 }, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* Dell Latitude D800 */
58         { { 0x73, 0x00, 0x0a }, 0xf8, 0xf8, ALPS_DUALPOINT },             /* ThinkPad R61 8918-5QG */
59         { { 0x73, 0x02, 0x0a }, 0xf8, 0xf8, 0 },
60         { { 0x73, 0x02, 0x14 }, 0xf8, 0xf8, ALPS_FW_BK_2 },               /* Ahtec Laptop */
61         { { 0x20, 0x02, 0x0e }, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* XXX */
62         { { 0x22, 0x02, 0x0a }, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT },
63         { { 0x22, 0x02, 0x14 }, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT }, /* Dell Latitude D600 */
64         /* Dell Latitude E5500, E6400, E6500, Precision M4400 */
65         { { 0x62, 0x02, 0x14 }, 0xcf, 0xcf,
66                 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED },
67         /* Dell Precision 4500 */
68         { { 0x73, 0x02, 0x64 }, 0x80, 0x80, 0 },
69         { { 0x73, 0x02, 0x50 }, 0xcf, 0xcf, ALPS_FOUR_BUTTONS },          /* Dell Vostro 1400 */
70         { { 0x52, 0x01, 0x14 }, 0xff, 0xff,
71                 ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED },      /* Toshiba Tecra A11-11L */
72 };
73
74 /*
75  * XXX - this entry is suspicious. First byte has zero lower nibble,
76  * which is what a normal mouse would report. Also, the value 0x0e
77  * isn't valid per PS/2 spec.
78  */
79
80 /*
81  * PS/2 packet format
82  *
83  * byte 0:  0    0 YSGN XSGN    1    M    R    L
84  * byte 1: X7   X6   X5   X4   X3   X2   X1   X0
85  * byte 2: Y7   Y6   Y5   Y4   Y3   Y2   Y1   Y0
86  *
87  * Note that the device never signals overflow condition.
88  *
89  * ALPS absolute Mode - new format
90  *
91  * byte 0:  1    ?    ?    ?    1    ?    ?    ?
92  * byte 1:  0   x6   x5   x4   x3   x2   x1   x0
93  * byte 2:  0  x10   x9   x8   x7    ?  fin  ges
94  * byte 3:  0   y9   y8   y7    1    M    R    L
95  * byte 4:  0   y6   y5   y4   y3   y2   y1   y0
96  * byte 5:  0   z6   z5   z4   z3   z2   z1   z0
97  *
98  * Dualpoint device -- interleaved packet format
99  *
100  * byte 0:    1    1    0    0    1    1    1    1
101  * byte 1:    0   x6   x5   x4   x3   x2   x1   x0
102  * byte 2:    0  x10   x9   x8   x7    0  fin  ges
103  * byte 3:    0    0 YSGN XSGN    1    1    1    1
104  * byte 4:   X7   X6   X5   X4   X3   X2   X1   X0
105  * byte 5:   Y7   Y6   Y5   Y4   Y3   Y2   Y1   Y0
106  * byte 6:    0   y9   y8   y7    1    m    r    l
107  * byte 7:    0   y6   y5   y4   y3   y2   y1   y0
108  * byte 8:    0   z6   z5   z4   z3   z2   z1   z0
109  *
110  * CAPITALS = stick, miniscules = touchpad
111  *
112  * ?'s can have different meanings on different models,
113  * such as wheel rotation, extra buttons, stick buttons
114  * on a dualpoint, etc.
115  */
116
117 static const struct dmi_system_id dell_quirk_table[] = {
118         {
119                 .matches = {
120                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
121                         DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
122                 },
123         },
124         {
125                 .matches = {
126                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
127                         DMI_MATCH(DMI_CHASSIS_TYPE, "9"),
128                 },
129         },
130         { }
131 };
132
133 int alps_model_quirk_enabled;
134 static void dell_e2_model_init(void *data);
135
136  /* Magic Sequence to enable Intellimouse Mode on Dell E2 Touchpads*/
137
138 static int dell_e2_setup_intellimouse_mode(void *data)
139 {
140         struct psmouse *psmouse = (struct psmouse *)(data);
141         struct ps2dev *ps2dev = &psmouse->ps2dev;
142         unsigned char param[] = {0, 0, 0};
143
144         if (!dmi_check_system(dell_quirk_table))
145                 return -1;
146
147         if (ps2_command(ps2dev, param, 0x00f5) ||
148                 ps2_command(ps2dev, param, 0x00ea) ||
149                 ps2_command(ps2dev, param, 0x00ec) ||
150                 ps2_command(ps2dev, param, 0x00ec) ||
151                 ps2_command(ps2dev, param, 0x00ec) ||
152                 ps2_command(ps2dev, param, 0x03e9))
153                         return -1;
154
155         dbg("alps:dell_e2_setup: param[0]: %x,"
156                 "param[1]: %x, param[2]: %x\n", param[0],
157                                         param[1], param[2]);
158  /* Check for supported model to continue */
159
160         if (!((param[0] == 0x88) && (param[1] == 0x07)
161                 && ((param[2] == 0x9D) || (param[2] == 0x9B))))
162                 return -1;
163
164         if (ps2_command(ps2dev, NULL, 0x00ec) ||
165                 ps2_command(ps2dev, NULL, 0x00f0) ||
166                 ps2_command(ps2dev, NULL, 0x00f0) ||
167                 ps2_command(ps2dev, NULL, 0x00f0) ||
168                 ps2_command(ps2dev, NULL, 0x00f3) ||
169                 ps2_command(ps2dev, NULL, 0x0028) ||
170                 ps2_command(ps2dev, NULL, 0x00f0) ||
171                 ps2_command(ps2dev, NULL, 0x00f6) ||
172                 ps2_command(ps2dev, NULL, 0x00ea) ||
173                 ps2_command(ps2dev, NULL, 0x00f4))
174                         return -1;
175
176         return 0;
177 }
178
179 static const struct alps_model_quirk alps_model_init_quirk_tbl[] = {
180
181         { {0x73, 0x02, 0x64}, dell_e2_setup_intellimouse_mode, dell_e2_model_init }
182 };
183
184 static int alps_model_hw_init_quirk(struct psmouse *psmouse,
185                                         const struct alps_model_info *model)
186 {
187         int rc = 1;
188         int i;
189
190         for (i = 0; i < ARRAY_SIZE(alps_model_init_quirk_tbl); i++) {
191                 if (!memcmp(model->signature,
192                         alps_model_init_quirk_tbl[i].signature,
193                             sizeof(alps_model_init_quirk_tbl[i].signature))) {
194                                 rc = alps_model_init_quirk_tbl[i].alps_model_quirk_hw_init(psmouse);
195                                 alps_model_quirk_enabled = i + 1;
196                                 break;
197                 }
198         }
199
200         return rc;
201 }
202
203 static bool alps_is_valid_first_byte(const struct alps_model_info *model,
204                                      unsigned char data)
205 {
206         return (data & model->mask0) == model->byte0;
207 }
208
209 static void alps_report_buttons(struct psmouse *psmouse,
210                                 struct input_dev *dev1, struct input_dev *dev2,
211                                 int left, int right, int middle)
212 {
213         struct input_dev *dev;
214
215         /*
216          * If shared button has already been reported on the
217          * other device (dev2) then this event should be also
218          * sent through that device.
219          */
220         dev = test_bit(BTN_LEFT, dev2->key) ? dev2 : dev1;
221         input_report_key(dev, BTN_LEFT, left);
222
223         dev = test_bit(BTN_RIGHT, dev2->key) ? dev2 : dev1;
224         input_report_key(dev, BTN_RIGHT, right);
225
226         dev = test_bit(BTN_MIDDLE, dev2->key) ? dev2 : dev1;
227         input_report_key(dev, BTN_MIDDLE, middle);
228
229         /*
230          * Sync the _other_ device now, we'll do the first
231          * device later once we report the rest of the events.
232          */
233         input_sync(dev2);
234 }
235
236 static void alps_process_packet(struct psmouse *psmouse)
237 {
238         struct alps_data *priv = psmouse->private;
239         const struct alps_model_info *model = priv->i;
240         unsigned char *packet = psmouse->packet;
241         struct input_dev *dev = psmouse->dev;
242         struct input_dev *dev2 = priv->dev2;
243         int x, y, z, ges, fin, left, right, middle;
244         int back = 0, forward = 0;
245
246         if (model->flags & ALPS_OLDPROTO) {
247                 left = packet[2] & 0x10;
248                 right = packet[2] & 0x08;
249                 middle = 0;
250                 x = packet[1] | ((packet[0] & 0x07) << 7);
251                 y = packet[4] | ((packet[3] & 0x07) << 7);
252                 z = packet[5];
253         } else {
254                 left = packet[3] & 1;
255                 right = packet[3] & 2;
256                 middle = packet[3] & 4;
257                 x = packet[1] | ((packet[2] & 0x78) << (7 - 3));
258                 y = packet[4] | ((packet[3] & 0x70) << (7 - 4));
259                 z = packet[5];
260         }
261
262         if (model->flags & ALPS_FW_BK_1) {
263                 back = packet[0] & 0x10;
264                 forward = packet[2] & 4;
265         }
266
267         if (model->flags & ALPS_FW_BK_2) {
268                 back = packet[3] & 4;
269                 forward = packet[2] & 4;
270                 if ((middle = forward && back))
271                         forward = back = 0;
272         }
273
274         ges = packet[2] & 1;
275         fin = packet[2] & 2;
276
277         if ((model->flags & ALPS_DUALPOINT) && z == 127) {
278                 input_report_rel(dev2, REL_X,  (x > 383 ? (x - 768) : x));
279                 input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y));
280
281                 alps_report_buttons(psmouse, dev2, dev, left, right, middle);
282
283                 input_sync(dev2);
284                 return;
285         }
286
287         alps_report_buttons(psmouse, dev, dev2, left, right, middle);
288
289         /* Convert hardware tap to a reasonable Z value */
290         if (ges && !fin)
291                 z = 40;
292
293         /*
294          * A "tap and drag" operation is reported by the hardware as a transition
295          * from (!fin && ges) to (fin && ges). This should be translated to the
296          * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
297          */
298         if (ges && fin && !priv->prev_fin) {
299                 input_report_abs(dev, ABS_X, x);
300                 input_report_abs(dev, ABS_Y, y);
301                 input_report_abs(dev, ABS_PRESSURE, 0);
302                 input_report_key(dev, BTN_TOOL_FINGER, 0);
303                 input_sync(dev);
304         }
305         priv->prev_fin = fin;
306
307         if (z > 30)
308                 input_report_key(dev, BTN_TOUCH, 1);
309         if (z < 25)
310                 input_report_key(dev, BTN_TOUCH, 0);
311
312         if (z > 0) {
313                 input_report_abs(dev, ABS_X, x);
314                 input_report_abs(dev, ABS_Y, y);
315         }
316
317         input_report_abs(dev, ABS_PRESSURE, z);
318         input_report_key(dev, BTN_TOOL_FINGER, z > 0);
319
320         if (model->flags & ALPS_WHEEL)
321                 input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
322
323         if (model->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
324                 input_report_key(dev, BTN_FORWARD, forward);
325                 input_report_key(dev, BTN_BACK, back);
326         }
327
328         if (model->flags & ALPS_FOUR_BUTTONS) {
329                 input_report_key(dev, BTN_0, packet[2] & 4);
330                 input_report_key(dev, BTN_1, packet[0] & 0x10);
331                 input_report_key(dev, BTN_2, packet[3] & 4);
332                 input_report_key(dev, BTN_3, packet[0] & 0x20);
333         }
334
335         input_sync(dev);
336 }
337
338 static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
339                                         unsigned char packet[],
340                                         bool report_buttons)
341 {
342         struct alps_data *priv = psmouse->private;
343         struct input_dev *dev2 = priv->dev2;
344
345         if (report_buttons)
346                 alps_report_buttons(psmouse, dev2, psmouse->dev,
347                                 packet[0] & 1, packet[0] & 2, packet[0] & 4);
348
349         input_report_rel(dev2, REL_X,
350                 packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
351         input_report_rel(dev2, REL_Y,
352                 packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
353
354         input_sync(dev2);
355 }
356
357 static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
358 {
359         struct alps_data *priv = psmouse->private;
360
361         if (psmouse->pktcnt < 6)
362                 return PSMOUSE_GOOD_DATA;
363
364         if (psmouse->pktcnt == 6) {
365                 /*
366                  * Start a timer to flush the packet if it ends up last
367                  * 6-byte packet in the stream. Timer needs to fire
368                  * psmouse core times out itself. 20 ms should be enough
369                  * to decide if we are getting more data or not.
370                  */
371                 mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
372                 return PSMOUSE_GOOD_DATA;
373         }
374
375         del_timer(&priv->timer);
376
377         if (psmouse->packet[6] & 0x80) {
378
379                 /*
380                  * Highest bit is set - that means we either had
381                  * complete ALPS packet and this is start of the
382                  * next packet or we got garbage.
383                  */
384
385                 if (((psmouse->packet[3] |
386                       psmouse->packet[4] |
387                       psmouse->packet[5]) & 0x80) ||
388                     (!alps_is_valid_first_byte(priv->i, psmouse->packet[6]))) {
389                         dbg("refusing packet %x %x %x %x "
390                             "(suspected interleaved ps/2)\n",
391                             psmouse->packet[3], psmouse->packet[4],
392                             psmouse->packet[5], psmouse->packet[6]);
393                         return PSMOUSE_BAD_DATA;
394                 }
395
396                 alps_process_packet(psmouse);
397
398                 /* Continue with the next packet */
399                 psmouse->packet[0] = psmouse->packet[6];
400                 psmouse->pktcnt = 1;
401
402         } else {
403
404                 /*
405                  * High bit is 0 - that means that we indeed got a PS/2
406                  * packet in the middle of ALPS packet.
407                  *
408                  * There is also possibility that we got 6-byte ALPS
409                  * packet followed  by 3-byte packet from trackpoint. We
410                  * can not distinguish between these 2 scenarios but
411                  * becase the latter is unlikely to happen in course of
412                  * normal operation (user would need to press all
413                  * buttons on the pad and start moving trackpoint
414                  * without touching the pad surface) we assume former.
415                  * Even if we are wrong the wost thing that would happen
416                  * the cursor would jump but we should not get protocol
417                  * desynchronization.
418                  */
419
420                 alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
421                                             false);
422
423                 /*
424                  * Continue with the standard ALPS protocol handling,
425                  * but make sure we won't process it as an interleaved
426                  * packet again, which may happen if all buttons are
427                  * pressed. To avoid this let's reset the 4th bit which
428                  * is normally 1.
429                  */
430                 psmouse->packet[3] = psmouse->packet[6] & 0xf7;
431                 psmouse->pktcnt = 4;
432         }
433
434         return PSMOUSE_GOOD_DATA;
435 }
436
437 static void alps_flush_packet(unsigned long data)
438 {
439         struct psmouse *psmouse = (struct psmouse *)data;
440
441         serio_pause_rx(psmouse->ps2dev.serio);
442
443         if (psmouse->pktcnt == 6) {
444
445                 /*
446                  * We did not any more data in reasonable amount of time.
447                  * Validate the last 3 bytes and process as a standard
448                  * ALPS packet.
449                  */
450                 if ((psmouse->packet[3] |
451                      psmouse->packet[4] |
452                      psmouse->packet[5]) & 0x80) {
453                         dbg("refusing packet %x %x %x "
454                             "(suspected interleaved ps/2)\n",
455                             psmouse->packet[3], psmouse->packet[4],
456                             psmouse->packet[5]);
457                 } else {
458                         alps_process_packet(psmouse);
459                 }
460                 psmouse->pktcnt = 0;
461         }
462
463         serio_continue_rx(psmouse->ps2dev.serio);
464 }
465
466 static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
467 {
468         struct alps_data *priv = psmouse->private;
469         const struct alps_model_info *model = priv->i;
470
471         if ((psmouse->packet[0] & 0xc8) == 0x08) { /* PS/2 packet */
472                 if (psmouse->pktcnt == 3) {
473                         alps_report_bare_ps2_packet(psmouse, psmouse->packet,
474                                                     true);
475                         return PSMOUSE_FULL_PACKET;
476                 }
477                 return PSMOUSE_GOOD_DATA;
478         }
479
480         /* Check for PS/2 packet stuffed in the middle of ALPS packet. */
481
482         if ((model->flags & ALPS_PS2_INTERLEAVED) &&
483             psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
484                 return alps_handle_interleaved_ps2(psmouse);
485         }
486
487         if (!alps_is_valid_first_byte(model, psmouse->packet[0])) {
488                 dbg("refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
489                     psmouse->packet[0], model->mask0, model->byte0);
490                 return PSMOUSE_BAD_DATA;
491         }
492
493         /* Bytes 2 - 6 should have 0 in the highest bit */
494         if (psmouse->pktcnt >= 2 && psmouse->pktcnt <= 6 &&
495             (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
496                 dbg("refusing packet[%i] = %x\n",
497                     psmouse->pktcnt - 1, psmouse->packet[psmouse->pktcnt - 1]);
498                 return PSMOUSE_BAD_DATA;
499         }
500
501         if (psmouse->pktcnt == 6) {
502                 alps_process_packet(psmouse);
503                 return PSMOUSE_FULL_PACKET;
504         }
505
506         return PSMOUSE_GOOD_DATA;
507 }
508
509 static const struct alps_model_info *alps_get_model(struct psmouse *psmouse, int *version)
510 {
511         struct ps2dev *ps2dev = &psmouse->ps2dev;
512         static const unsigned char rates[] = { 0, 10, 20, 40, 60, 80, 100, 200 };
513         unsigned char param[4];
514         int i;
515
516         /*
517          * First try "E6 report".
518          * ALPS should return 0,0,10 or 0,0,100
519          */
520         param[0] = 0;
521         if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES) ||
522             ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE11) ||
523             ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE11) ||
524             ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE11))
525                 return NULL;
526
527         param[0] = param[1] = param[2] = 0xff;
528         if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
529                 return NULL;
530
531         dbg("E6 report: %2.2x %2.2x %2.2x", param[0], param[1], param[2]);
532
533         if (param[0] != 0 || param[1] != 0 || (param[2] != 10 && param[2] != 100))
534                 return NULL;
535
536         /*
537          * Now try "E7 report". Allowed responses are in
538          * alps_model_data[].signature
539          */
540         param[0] = 0;
541         if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES) ||
542             ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE21) ||
543             ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE21) ||
544             ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE21))
545                 return NULL;
546
547         param[0] = param[1] = param[2] = 0xff;
548         if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
549                 return NULL;
550
551         dbg("E7 report: %2.2x %2.2x %2.2x", param[0], param[1], param[2]);
552
553         if (version) {
554                 for (i = 0; i < ARRAY_SIZE(rates) && param[2] != rates[i]; i++)
555                         /* empty */;
556                 *version = (param[0] << 8) | (param[1] << 4) | i;
557         }
558
559         for (i = 0; i < ARRAY_SIZE(alps_model_data); i++)
560                 if (!memcmp(param, alps_model_data[i].signature,
561                             sizeof(alps_model_data[i].signature)))
562                         return alps_model_data + i;
563
564         return NULL;
565 }
566
567 /*
568  * For DualPoint devices select the device that should respond to
569  * subsequent commands. It looks like glidepad is behind stickpointer,
570  * I'd thought it would be other way around...
571  */
572 static int alps_passthrough_mode(struct psmouse *psmouse, bool enable)
573 {
574         struct ps2dev *ps2dev = &psmouse->ps2dev;
575         int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
576
577         if (ps2_command(ps2dev, NULL, cmd) ||
578             ps2_command(ps2dev, NULL, cmd) ||
579             ps2_command(ps2dev, NULL, cmd) ||
580             ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
581                 return -1;
582
583         /* we may get 3 more bytes, just ignore them */
584         ps2_drain(ps2dev, 3, 100);
585
586         return 0;
587 }
588
589 static int alps_absolute_mode(struct psmouse *psmouse)
590 {
591         struct ps2dev *ps2dev = &psmouse->ps2dev;
592
593         /* Try ALPS magic knock - 4 disable before enable */
594         if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
595             ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
596             ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
597             ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
598             ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
599                 return -1;
600
601         /*
602          * Switch mouse to poll (remote) mode so motion data will not
603          * get in our way
604          */
605         return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
606 }
607
608 static int alps_get_status(struct psmouse *psmouse, char *param)
609 {
610         struct ps2dev *ps2dev = &psmouse->ps2dev;
611
612         /* Get status: 0xF5 0xF5 0xF5 0xE9 */
613         if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
614             ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
615             ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
616             ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
617                 return -1;
618
619         dbg("Status: %2.2x %2.2x %2.2x", param[0], param[1], param[2]);
620
621         return 0;
622 }
623
624 /*
625  * Turn touchpad tapping on or off. The sequences are:
626  * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
627  * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
628  * My guess that 0xE9 (GetInfo) is here as a sync point.
629  * For models that also have stickpointer (DualPoints) its tapping
630  * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
631  * we don't fiddle with it.
632  */
633 static int alps_tap_mode(struct psmouse *psmouse, int enable)
634 {
635         struct ps2dev *ps2dev = &psmouse->ps2dev;
636         int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
637         unsigned char tap_arg = enable ? 0x0A : 0x00;
638         unsigned char param[4];
639
640         if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
641             ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
642             ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
643             ps2_command(ps2dev, &tap_arg, cmd))
644                 return -1;
645
646         if (alps_get_status(psmouse, param))
647                 return -1;
648
649         return 0;
650 }
651
652 /*
653  * alps_poll() - poll the touchpad for current motion packet.
654  * Used in resync.
655  */
656 static int alps_poll(struct psmouse *psmouse)
657 {
658         struct alps_data *priv = psmouse->private;
659         unsigned char buf[6];
660         bool poll_failed;
661
662         if (priv->i->flags & ALPS_PASS)
663                 alps_passthrough_mode(psmouse, true);
664
665         poll_failed = ps2_command(&psmouse->ps2dev, buf,
666                                   PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
667
668         if (priv->i->flags & ALPS_PASS)
669                 alps_passthrough_mode(psmouse, false);
670
671         if (poll_failed || (buf[0] & priv->i->mask0) != priv->i->byte0)
672                 return -1;
673
674         if ((psmouse->badbyte & 0xc8) == 0x08) {
675 /*
676  * Poll the track stick ...
677  */
678                 if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
679                         return -1;
680         }
681
682         memcpy(psmouse->packet, buf, sizeof(buf));
683         return 0;
684 }
685
686 static int alps_hw_init(struct psmouse *psmouse)
687 {
688         struct alps_data *priv = psmouse->private;
689         const struct alps_model_info *model = priv->i;
690
691         if (alps_model_hw_init_quirk(psmouse, model)) {
692                 alps_model_quirk_enabled = 0;
693         }
694
695         if ((model->flags & ALPS_PASS) &&
696             alps_passthrough_mode(psmouse, true)) {
697                 return -1;
698         }
699
700         if (alps_tap_mode(psmouse, true)) {
701                 printk(KERN_WARNING "alps.c: Failed to enable hardware tapping\n");
702                 return -1;
703         }
704
705         if (alps_absolute_mode(psmouse)) {
706                 printk(KERN_ERR "alps.c: Failed to enable absolute mode\n");
707                 return -1;
708         }
709
710         if ((model->flags & ALPS_PASS) &&
711             alps_passthrough_mode(psmouse, false)) {
712                 return -1;
713         }
714
715         /* ALPS needs stream mode, otherwise it won't report any data */
716         if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
717                 printk(KERN_ERR "alps.c: Failed to enable stream mode\n");
718                 return -1;
719         }
720
721         return 0;
722 }
723
724 static int alps_reconnect(struct psmouse *psmouse)
725 {
726         const struct alps_model_info *model;
727
728         psmouse_reset(psmouse);
729
730         model = alps_get_model(psmouse, NULL);
731         if (!model)
732                 return -1;
733
734         return alps_hw_init(psmouse);
735 }
736
737 static void alps_disconnect(struct psmouse *psmouse)
738 {
739         struct alps_data *priv = psmouse->private;
740
741         psmouse_reset(psmouse);
742         del_timer_sync(&priv->timer);
743         input_unregister_device(priv->dev2);
744         kfree(priv);
745 }
746
747 static void dell_e2_model_init(void *data)
748 {
749         struct psmouse *psmouse = (struct psmouse *)(data);
750         struct input_dev *dev = psmouse->dev;
751
752         __set_bit(BTN_MIDDLE, dev->keybit);
753         __set_bit(REL_WHEEL, dev->relbit);
754
755         psmouse->pktsize = 4;
756         psmouse->type = PSMOUSE_IMPS;
757         psmouse->disconnect = alps_disconnect;
758         psmouse->reconnect = alps_reconnect;
759
760 }
761
762 int alps_init(struct psmouse *psmouse)
763 {
764         struct alps_data *priv;
765         const struct alps_model_info *model;
766         struct input_dev *dev1 = psmouse->dev, *dev2;
767         int version;
768
769         priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
770         dev2 = input_allocate_device();
771         if (!priv || !dev2)
772                 goto init_fail;
773
774         priv->dev2 = dev2;
775         setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
776
777         psmouse->private = priv;
778
779         model = alps_get_model(psmouse, &version);
780         if (!model)
781                 goto init_fail;
782
783         priv->i = model;
784
785         if (alps_hw_init(psmouse))
786                 goto init_fail;
787
788         if (alps_model_quirk_enabled) {
789                 printk(KERN_WARNING "alps.c: Enabled hardware quirk, falling back to psmouse-core\n");
790                 alps_model_init_quirk_tbl[alps_model_quirk_enabled-1].alps_model_quirk_init(psmouse);
791                 return 0;
792         }
793
794         /*
795          * Undo part of setup done for us by psmouse core since touchpad
796          * is not a relative device.
797          */
798         __clear_bit(EV_REL, dev1->evbit);
799         __clear_bit(REL_X, dev1->relbit);
800         __clear_bit(REL_Y, dev1->relbit);
801
802         /*
803          * Now set up our capabilities.
804          */
805         dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
806         dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
807         dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
808         dev1->keybit[BIT_WORD(BTN_LEFT)] |=
809                 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
810
811         dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
812         input_set_abs_params(dev1, ABS_X, 0, 1023, 0, 0);
813         input_set_abs_params(dev1, ABS_Y, 0, 767, 0, 0);
814         input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
815
816         if (model->flags & ALPS_WHEEL) {
817                 dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
818                 dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
819         }
820
821         if (model->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
822                 dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
823                 dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
824         }
825
826         if (model->flags & ALPS_FOUR_BUTTONS) {
827                 dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
828                 dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
829                 dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
830                 dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
831         } else {
832                 dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
833         }
834
835         snprintf(priv->phys, sizeof(priv->phys), "%s/input1", psmouse->ps2dev.serio->phys);
836         dev2->phys = priv->phys;
837         dev2->name = (model->flags & ALPS_DUALPOINT) ? "DualPoint Stick" : "PS/2 Mouse";
838         dev2->id.bustype = BUS_I8042;
839         dev2->id.vendor  = 0x0002;
840         dev2->id.product = PSMOUSE_ALPS;
841         dev2->id.version = 0x0000;
842         dev2->dev.parent = &psmouse->ps2dev.serio->dev;
843
844         dev2->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
845         dev2->relbit[BIT_WORD(REL_X)] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
846         dev2->keybit[BIT_WORD(BTN_LEFT)] =
847                 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_MIDDLE) | BIT_MASK(BTN_RIGHT);
848
849         if (input_register_device(priv->dev2))
850                 goto init_fail;
851
852         psmouse->protocol_handler = alps_process_byte;
853         psmouse->poll = alps_poll;
854         psmouse->disconnect = alps_disconnect;
855         psmouse->reconnect = alps_reconnect;
856         psmouse->pktsize = 6;
857
858         /* We are having trouble resyncing ALPS touchpads so disable it for now */
859         psmouse->resync_time = 0;
860
861         return 0;
862
863 init_fail:
864         psmouse_reset(psmouse);
865         input_free_device(dev2);
866         kfree(priv);
867         psmouse->private = NULL;
868         return -1;
869 }
870
871 int alps_detect(struct psmouse *psmouse, bool set_properties)
872 {
873         int version;
874         const struct alps_model_info *model;
875
876         model = alps_get_model(psmouse, &version);
877         if (!model)
878                 return -1;
879
880         if (set_properties) {
881                 psmouse->vendor = "ALPS";
882                 psmouse->name = model->flags & ALPS_DUALPOINT ?
883                                 "DualPoint TouchPad" : "GlidePoint";
884                 psmouse->model = version;
885         }
886         return 0;
887 }
888