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