pandora: defconfig: update
[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         /*
376          * Check if we are dealing with a bare PS/2 packet, presumably from
377          * a device connected to the external PS/2 port. Because bare PS/2
378          * protocol does not have enough constant bits to self-synchronize
379          * properly we only do this if the device is fully synchronized.
380          */
381         if (!psmouse->out_of_sync_cnt && (psmouse->packet[0] & 0xc8) == 0x08) {
382                 if (psmouse->pktcnt == 3) {
383                         alps_report_bare_ps2_packet(psmouse, psmouse->packet,
384                                                     true);
385                         return PSMOUSE_FULL_PACKET;
386                 }
387                 return PSMOUSE_GOOD_DATA;
388         }
389
390         /* Check for PS/2 packet stuffed in the middle of ALPS packet. */
391
392         if ((model->flags & ALPS_PS2_INTERLEAVED) &&
393             psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
394                 return alps_handle_interleaved_ps2(psmouse);
395         }
396
397         if (!alps_is_valid_first_byte(model, psmouse->packet[0])) {
398                 psmouse_dbg(psmouse,
399                             "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
400                             psmouse->packet[0], model->mask0, model->byte0);
401                 return PSMOUSE_BAD_DATA;
402         }
403
404         /* Bytes 2 - 6 should have 0 in the highest bit */
405         if (psmouse->pktcnt >= 2 && psmouse->pktcnt <= 6 &&
406             (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
407                 psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
408                             psmouse->pktcnt - 1,
409                             psmouse->packet[psmouse->pktcnt - 1]);
410                 return PSMOUSE_BAD_DATA;
411         }
412
413         if (psmouse->pktcnt == 6) {
414                 alps_process_packet(psmouse);
415                 return PSMOUSE_FULL_PACKET;
416         }
417
418         return PSMOUSE_GOOD_DATA;
419 }
420
421 static const struct alps_model_info *alps_get_model(struct psmouse *psmouse, int *version)
422 {
423         struct ps2dev *ps2dev = &psmouse->ps2dev;
424         static const unsigned char rates[] = { 0, 10, 20, 40, 60, 80, 100, 200 };
425         unsigned char param[4];
426         int i;
427
428         /*
429          * First try "E6 report".
430          * ALPS should return 0,0,10 or 0,0,100 if no buttons are pressed.
431          * The bits 0-2 of the first byte will be 1s if some buttons are
432          * pressed.
433          */
434         param[0] = 0;
435         if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES) ||
436             ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE11) ||
437             ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE11) ||
438             ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE11))
439                 return NULL;
440
441         param[0] = param[1] = param[2] = 0xff;
442         if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
443                 return NULL;
444
445         psmouse_dbg(psmouse, "E6 report: %2.2x %2.2x %2.2x",
446                     param[0], param[1], param[2]);
447
448         if ((param[0] & 0xf8) != 0 || param[1] != 0 ||
449             (param[2] != 10 && param[2] != 100))
450                 return NULL;
451
452         /*
453          * Now try "E7 report". Allowed responses are in
454          * alps_model_data[].signature
455          */
456         param[0] = 0;
457         if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES) ||
458             ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE21) ||
459             ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE21) ||
460             ps2_command(ps2dev,  NULL, PSMOUSE_CMD_SETSCALE21))
461                 return NULL;
462
463         param[0] = param[1] = param[2] = 0xff;
464         if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
465                 return NULL;
466
467         psmouse_dbg(psmouse, "E7 report: %2.2x %2.2x %2.2x",
468                     param[0], param[1], param[2]);
469
470         if (version) {
471                 for (i = 0; i < ARRAY_SIZE(rates) && param[2] != rates[i]; i++)
472                         /* empty */;
473                 *version = (param[0] << 8) | (param[1] << 4) | i;
474         }
475
476         for (i = 0; i < ARRAY_SIZE(alps_model_data); i++)
477                 if (!memcmp(param, alps_model_data[i].signature,
478                             sizeof(alps_model_data[i].signature)))
479                         return alps_model_data + i;
480
481         return NULL;
482 }
483
484 /*
485  * For DualPoint devices select the device that should respond to
486  * subsequent commands. It looks like glidepad is behind stickpointer,
487  * I'd thought it would be other way around...
488  */
489 static int alps_passthrough_mode(struct psmouse *psmouse, bool enable)
490 {
491         struct ps2dev *ps2dev = &psmouse->ps2dev;
492         int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
493
494         if (ps2_command(ps2dev, NULL, cmd) ||
495             ps2_command(ps2dev, NULL, cmd) ||
496             ps2_command(ps2dev, NULL, cmd) ||
497             ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
498                 return -1;
499
500         /* we may get 3 more bytes, just ignore them */
501         ps2_drain(ps2dev, 3, 100);
502
503         return 0;
504 }
505
506 static int alps_absolute_mode(struct psmouse *psmouse)
507 {
508         struct ps2dev *ps2dev = &psmouse->ps2dev;
509
510         /* Try ALPS magic knock - 4 disable before enable */
511         if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
512             ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
513             ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
514             ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
515             ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
516                 return -1;
517
518         /*
519          * Switch mouse to poll (remote) mode so motion data will not
520          * get in our way
521          */
522         return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
523 }
524
525 static int alps_get_status(struct psmouse *psmouse, char *param)
526 {
527         struct ps2dev *ps2dev = &psmouse->ps2dev;
528
529         /* Get status: 0xF5 0xF5 0xF5 0xE9 */
530         if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
531             ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
532             ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
533             ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
534                 return -1;
535
536         psmouse_dbg(psmouse, "Status: %2.2x %2.2x %2.2x",
537                     param[0], param[1], param[2]);
538
539         return 0;
540 }
541
542 /*
543  * Turn touchpad tapping on or off. The sequences are:
544  * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
545  * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
546  * My guess that 0xE9 (GetInfo) is here as a sync point.
547  * For models that also have stickpointer (DualPoints) its tapping
548  * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
549  * we don't fiddle with it.
550  */
551 static int alps_tap_mode(struct psmouse *psmouse, int enable)
552 {
553         struct ps2dev *ps2dev = &psmouse->ps2dev;
554         int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
555         unsigned char tap_arg = enable ? 0x0A : 0x00;
556         unsigned char param[4];
557
558         if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
559             ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
560             ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
561             ps2_command(ps2dev, &tap_arg, cmd))
562                 return -1;
563
564         if (alps_get_status(psmouse, param))
565                 return -1;
566
567         return 0;
568 }
569
570 /*
571  * alps_poll() - poll the touchpad for current motion packet.
572  * Used in resync.
573  */
574 static int alps_poll(struct psmouse *psmouse)
575 {
576         struct alps_data *priv = psmouse->private;
577         unsigned char buf[6];
578         bool poll_failed;
579
580         if (priv->i->flags & ALPS_PASS)
581                 alps_passthrough_mode(psmouse, true);
582
583         poll_failed = ps2_command(&psmouse->ps2dev, buf,
584                                   PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
585
586         if (priv->i->flags & ALPS_PASS)
587                 alps_passthrough_mode(psmouse, false);
588
589         if (poll_failed || (buf[0] & priv->i->mask0) != priv->i->byte0)
590                 return -1;
591
592         if ((psmouse->badbyte & 0xc8) == 0x08) {
593 /*
594  * Poll the track stick ...
595  */
596                 if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
597                         return -1;
598         }
599
600         memcpy(psmouse->packet, buf, sizeof(buf));
601         return 0;
602 }
603
604 static int alps_hw_init(struct psmouse *psmouse)
605 {
606         struct alps_data *priv = psmouse->private;
607         const struct alps_model_info *model = priv->i;
608
609         if ((model->flags & ALPS_PASS) &&
610             alps_passthrough_mode(psmouse, true)) {
611                 return -1;
612         }
613
614         if (alps_tap_mode(psmouse, true)) {
615                 psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
616                 return -1;
617         }
618
619         if (alps_absolute_mode(psmouse)) {
620                 psmouse_err(psmouse, "Failed to enable absolute mode\n");
621                 return -1;
622         }
623
624         if ((model->flags & ALPS_PASS) &&
625             alps_passthrough_mode(psmouse, false)) {
626                 return -1;
627         }
628
629         /* ALPS needs stream mode, otherwise it won't report any data */
630         if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
631                 psmouse_err(psmouse, "Failed to enable stream mode\n");
632                 return -1;
633         }
634
635         return 0;
636 }
637
638 static int alps_reconnect(struct psmouse *psmouse)
639 {
640         const struct alps_model_info *model;
641
642         psmouse_reset(psmouse);
643
644         model = alps_get_model(psmouse, NULL);
645         if (!model)
646                 return -1;
647
648         return alps_hw_init(psmouse);
649 }
650
651 static void alps_disconnect(struct psmouse *psmouse)
652 {
653         struct alps_data *priv = psmouse->private;
654
655         psmouse_reset(psmouse);
656         del_timer_sync(&priv->timer);
657         input_unregister_device(priv->dev2);
658         kfree(priv);
659 }
660
661 int alps_init(struct psmouse *psmouse)
662 {
663         struct alps_data *priv;
664         const struct alps_model_info *model;
665         struct input_dev *dev1 = psmouse->dev, *dev2;
666         int version;
667
668         priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
669         dev2 = input_allocate_device();
670         if (!priv || !dev2)
671                 goto init_fail;
672
673         priv->dev2 = dev2;
674         setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
675
676         psmouse->private = priv;
677
678         model = alps_get_model(psmouse, &version);
679         if (!model)
680                 goto init_fail;
681
682         priv->i = model;
683
684         if (alps_hw_init(psmouse))
685                 goto init_fail;
686
687         /*
688          * Undo part of setup done for us by psmouse core since touchpad
689          * is not a relative device.
690          */
691         __clear_bit(EV_REL, dev1->evbit);
692         __clear_bit(REL_X, dev1->relbit);
693         __clear_bit(REL_Y, dev1->relbit);
694
695         /*
696          * Now set up our capabilities.
697          */
698         dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
699         dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
700         dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
701         dev1->keybit[BIT_WORD(BTN_LEFT)] |=
702                 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
703
704         dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
705         input_set_abs_params(dev1, ABS_X, 0, 1023, 0, 0);
706         input_set_abs_params(dev1, ABS_Y, 0, 767, 0, 0);
707         input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
708
709         if (model->flags & ALPS_WHEEL) {
710                 dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
711                 dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
712         }
713
714         if (model->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
715                 dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
716                 dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
717         }
718
719         if (model->flags & ALPS_FOUR_BUTTONS) {
720                 dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
721                 dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
722                 dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
723                 dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
724         } else {
725                 dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
726         }
727
728         snprintf(priv->phys, sizeof(priv->phys), "%s/input1", psmouse->ps2dev.serio->phys);
729         dev2->phys = priv->phys;
730         dev2->name = (model->flags & ALPS_DUALPOINT) ? "DualPoint Stick" : "PS/2 Mouse";
731         dev2->id.bustype = BUS_I8042;
732         dev2->id.vendor  = 0x0002;
733         dev2->id.product = PSMOUSE_ALPS;
734         dev2->id.version = 0x0000;
735         dev2->dev.parent = &psmouse->ps2dev.serio->dev;
736
737         dev2->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
738         dev2->relbit[BIT_WORD(REL_X)] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
739         dev2->keybit[BIT_WORD(BTN_LEFT)] =
740                 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_MIDDLE) | BIT_MASK(BTN_RIGHT);
741
742         if (input_register_device(priv->dev2))
743                 goto init_fail;
744
745         psmouse->protocol_handler = alps_process_byte;
746         psmouse->poll = alps_poll;
747         psmouse->disconnect = alps_disconnect;
748         psmouse->reconnect = alps_reconnect;
749         psmouse->pktsize = 6;
750
751         /* We are having trouble resyncing ALPS touchpads so disable it for now */
752         psmouse->resync_time = 0;
753
754         /* Allow 2 invalid packets without resetting device */
755         psmouse->resetafter = psmouse->pktsize * 2;
756
757         return 0;
758
759 init_fail:
760         psmouse_reset(psmouse);
761         input_free_device(dev2);
762         kfree(priv);
763         psmouse->private = NULL;
764         return -1;
765 }
766
767 int alps_detect(struct psmouse *psmouse, bool set_properties)
768 {
769         int version;
770         const struct alps_model_info *model;
771
772         model = alps_get_model(psmouse, &version);
773         if (!model)
774                 return -1;
775
776         if (set_properties) {
777                 psmouse->vendor = "ALPS";
778                 psmouse->name = model->flags & ALPS_DUALPOINT ?
779                                 "DualPoint TouchPad" : "GlidePoint";
780                 psmouse->model = version;
781         }
782         return 0;
783 }
784