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