Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs-2.6
[pandora-kernel.git] / drivers / input / mouse / synaptics.c
1 /*
2  * Synaptics TouchPad PS/2 mouse driver
3  *
4  *   2003 Dmitry Torokhov <dtor@mail.ru>
5  *     Added support for pass-through port. Special thanks to Peter Berg Larsen
6  *     for explaining various Synaptics quirks.
7  *
8  *   2003 Peter Osterlund <petero2@telia.com>
9  *     Ported to 2.5 input device infrastructure.
10  *
11  *   Copyright (C) 2001 Stefan Gmeiner <riddlebox@freesurf.ch>
12  *     start merging tpconfig and gpm code to a xfree-input module
13  *     adding some changes and extensions (ex. 3rd and 4th button)
14  *
15  *   Copyright (c) 1997 C. Scott Ananian <cananian@alumni.priceton.edu>
16  *   Copyright (c) 1998-2000 Bruce Kalk <kall@compass.com>
17  *     code for the special synaptics commands (from the tpconfig-source)
18  *
19  * This program is free software; you can redistribute it and/or modify it
20  * under the terms of the GNU General Public License version 2 as published by
21  * the Free Software Foundation.
22  *
23  * Trademarks are the property of their respective owners.
24  */
25
26 #include <linux/module.h>
27 #include <linux/dmi.h>
28 #include <linux/input/mt.h>
29 #include <linux/serio.h>
30 #include <linux/libps2.h>
31 #include <linux/slab.h>
32 #include "psmouse.h"
33 #include "synaptics.h"
34
35 /*
36  * The x/y limits are taken from the Synaptics TouchPad interfacing Guide,
37  * section 2.3.2, which says that they should be valid regardless of the
38  * actual size of the sensor.
39  * Note that newer firmware allows querying device for maximum useable
40  * coordinates.
41  */
42 #define XMIN_NOMINAL 1472
43 #define XMAX_NOMINAL 5472
44 #define YMIN_NOMINAL 1408
45 #define YMAX_NOMINAL 4448
46
47
48 /*****************************************************************************
49  *      Stuff we need even when we do not want native Synaptics support
50  ****************************************************************************/
51
52 /*
53  * Set the synaptics touchpad mode byte by special commands
54  */
55 static int synaptics_mode_cmd(struct psmouse *psmouse, unsigned char mode)
56 {
57         unsigned char param[1];
58
59         if (psmouse_sliced_command(psmouse, mode))
60                 return -1;
61         param[0] = SYN_PS_SET_MODE2;
62         if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_SETRATE))
63                 return -1;
64         return 0;
65 }
66
67 int synaptics_detect(struct psmouse *psmouse, bool set_properties)
68 {
69         struct ps2dev *ps2dev = &psmouse->ps2dev;
70         unsigned char param[4];
71
72         param[0] = 0;
73
74         ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
75         ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
76         ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
77         ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
78         ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO);
79
80         if (param[1] != 0x47)
81                 return -ENODEV;
82
83         if (set_properties) {
84                 psmouse->vendor = "Synaptics";
85                 psmouse->name = "TouchPad";
86         }
87
88         return 0;
89 }
90
91 void synaptics_reset(struct psmouse *psmouse)
92 {
93         /* reset touchpad back to relative mode, gestures enabled */
94         synaptics_mode_cmd(psmouse, 0);
95 }
96
97 #ifdef CONFIG_MOUSE_PS2_SYNAPTICS
98
99 /*****************************************************************************
100  *      Synaptics communications functions
101  ****************************************************************************/
102
103 /*
104  * Send a command to the synpatics touchpad by special commands
105  */
106 static int synaptics_send_cmd(struct psmouse *psmouse, unsigned char c, unsigned char *param)
107 {
108         if (psmouse_sliced_command(psmouse, c))
109                 return -1;
110         if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_GETINFO))
111                 return -1;
112         return 0;
113 }
114
115 /*
116  * Read the model-id bytes from the touchpad
117  * see also SYN_MODEL_* macros
118  */
119 static int synaptics_model_id(struct psmouse *psmouse)
120 {
121         struct synaptics_data *priv = psmouse->private;
122         unsigned char mi[3];
123
124         if (synaptics_send_cmd(psmouse, SYN_QUE_MODEL, mi))
125                 return -1;
126         priv->model_id = (mi[0]<<16) | (mi[1]<<8) | mi[2];
127         return 0;
128 }
129
130 /*
131  * Read the capability-bits from the touchpad
132  * see also the SYN_CAP_* macros
133  */
134 static int synaptics_capability(struct psmouse *psmouse)
135 {
136         struct synaptics_data *priv = psmouse->private;
137         unsigned char cap[3];
138
139         if (synaptics_send_cmd(psmouse, SYN_QUE_CAPABILITIES, cap))
140                 return -1;
141         priv->capabilities = (cap[0] << 16) | (cap[1] << 8) | cap[2];
142         priv->ext_cap = priv->ext_cap_0c = 0;
143
144         /*
145          * Older firmwares had submodel ID fixed to 0x47
146          */
147         if (SYN_ID_FULL(priv->identity) < 0x705 &&
148             SYN_CAP_SUBMODEL_ID(priv->capabilities) != 0x47) {
149                 return -1;
150         }
151
152         /*
153          * Unless capExtended is set the rest of the flags should be ignored
154          */
155         if (!SYN_CAP_EXTENDED(priv->capabilities))
156                 priv->capabilities = 0;
157
158         if (SYN_EXT_CAP_REQUESTS(priv->capabilities) >= 1) {
159                 if (synaptics_send_cmd(psmouse, SYN_QUE_EXT_CAPAB, cap)) {
160                         printk(KERN_ERR "Synaptics claims to have extended capabilities,"
161                                " but I'm not able to read them.\n");
162                 } else {
163                         priv->ext_cap = (cap[0] << 16) | (cap[1] << 8) | cap[2];
164
165                         /*
166                          * if nExtBtn is greater than 8 it should be considered
167                          * invalid and treated as 0
168                          */
169                         if (SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap) > 8)
170                                 priv->ext_cap &= 0xff0fff;
171                 }
172         }
173
174         if (SYN_EXT_CAP_REQUESTS(priv->capabilities) >= 4) {
175                 if (synaptics_send_cmd(psmouse, SYN_QUE_EXT_CAPAB_0C, cap)) {
176                         printk(KERN_ERR "Synaptics claims to have extended capability 0x0c,"
177                                " but I'm not able to read it.\n");
178                 } else {
179                         priv->ext_cap_0c = (cap[0] << 16) | (cap[1] << 8) | cap[2];
180                 }
181         }
182
183         return 0;
184 }
185
186 /*
187  * Identify Touchpad
188  * See also the SYN_ID_* macros
189  */
190 static int synaptics_identify(struct psmouse *psmouse)
191 {
192         struct synaptics_data *priv = psmouse->private;
193         unsigned char id[3];
194
195         if (synaptics_send_cmd(psmouse, SYN_QUE_IDENTIFY, id))
196                 return -1;
197         priv->identity = (id[0]<<16) | (id[1]<<8) | id[2];
198         if (SYN_ID_IS_SYNAPTICS(priv->identity))
199                 return 0;
200         return -1;
201 }
202
203 /*
204  * Read touchpad resolution and maximum reported coordinates
205  * Resolution is left zero if touchpad does not support the query
206  */
207 static int synaptics_resolution(struct psmouse *psmouse)
208 {
209         struct synaptics_data *priv = psmouse->private;
210         unsigned char resp[3];
211
212         if (SYN_ID_MAJOR(priv->identity) < 4)
213                 return 0;
214
215         if (synaptics_send_cmd(psmouse, SYN_QUE_RESOLUTION, resp) == 0) {
216                 if (resp[0] != 0 && (resp[1] & 0x80) && resp[2] != 0) {
217                         priv->x_res = resp[0]; /* x resolution in units/mm */
218                         priv->y_res = resp[2]; /* y resolution in units/mm */
219                 }
220         }
221
222         if (SYN_EXT_CAP_REQUESTS(priv->capabilities) >= 5 &&
223             SYN_CAP_MAX_DIMENSIONS(priv->ext_cap_0c)) {
224                 if (synaptics_send_cmd(psmouse, SYN_QUE_EXT_MAX_COORDS, resp)) {
225                         printk(KERN_ERR "Synaptics claims to have max coordinates"
226                                " query, but I'm not able to read it.\n");
227                 } else {
228                         priv->x_max = (resp[0] << 5) | ((resp[1] & 0x0f) << 1);
229                         priv->y_max = (resp[2] << 5) | ((resp[1] & 0xf0) >> 3);
230                 }
231         }
232
233         if (SYN_EXT_CAP_REQUESTS(priv->capabilities) >= 7 &&
234             SYN_CAP_MIN_DIMENSIONS(priv->ext_cap_0c)) {
235                 if (synaptics_send_cmd(psmouse, SYN_QUE_EXT_MIN_COORDS, resp)) {
236                         printk(KERN_ERR "Synaptics claims to have min coordinates"
237                                " query, but I'm not able to read it.\n");
238                 } else {
239                         priv->x_min = (resp[0] << 5) | ((resp[1] & 0x0f) << 1);
240                         priv->y_min = (resp[2] << 5) | ((resp[1] & 0xf0) >> 3);
241                 }
242         }
243
244         return 0;
245 }
246
247 static int synaptics_query_hardware(struct psmouse *psmouse)
248 {
249         if (synaptics_identify(psmouse))
250                 return -1;
251         if (synaptics_model_id(psmouse))
252                 return -1;
253         if (synaptics_capability(psmouse))
254                 return -1;
255         if (synaptics_resolution(psmouse))
256                 return -1;
257
258         return 0;
259 }
260
261 static int synaptics_set_absolute_mode(struct psmouse *psmouse)
262 {
263         struct synaptics_data *priv = psmouse->private;
264
265         priv->mode = SYN_BIT_ABSOLUTE_MODE;
266         if (SYN_ID_MAJOR(priv->identity) >= 4)
267                 priv->mode |= SYN_BIT_DISABLE_GESTURE;
268         if (SYN_CAP_EXTENDED(priv->capabilities))
269                 priv->mode |= SYN_BIT_W_MODE;
270
271         if (synaptics_mode_cmd(psmouse, priv->mode))
272                 return -1;
273
274         return 0;
275 }
276
277 static void synaptics_set_rate(struct psmouse *psmouse, unsigned int rate)
278 {
279         struct synaptics_data *priv = psmouse->private;
280
281         if (rate >= 80) {
282                 priv->mode |= SYN_BIT_HIGH_RATE;
283                 psmouse->rate = 80;
284         } else {
285                 priv->mode &= ~SYN_BIT_HIGH_RATE;
286                 psmouse->rate = 40;
287         }
288
289         synaptics_mode_cmd(psmouse, priv->mode);
290 }
291
292 static int synaptics_set_advanced_gesture_mode(struct psmouse *psmouse)
293 {
294         static unsigned char param = 0xc8;
295         struct synaptics_data *priv = psmouse->private;
296
297         if (!SYN_CAP_ADV_GESTURE(priv->ext_cap_0c))
298                 return 0;
299
300         if (psmouse_sliced_command(psmouse, SYN_QUE_MODEL))
301                 return -1;
302         if (ps2_command(&psmouse->ps2dev, &param, PSMOUSE_CMD_SETRATE))
303                 return -1;
304
305         /* Advanced gesture mode also sends multi finger data */
306         priv->capabilities |= BIT(1);
307
308         return 0;
309 }
310
311 /*****************************************************************************
312  *      Synaptics pass-through PS/2 port support
313  ****************************************************************************/
314 static int synaptics_pt_write(struct serio *serio, unsigned char c)
315 {
316         struct psmouse *parent = serio_get_drvdata(serio->parent);
317         char rate_param = SYN_PS_CLIENT_CMD; /* indicates that we want pass-through port */
318
319         if (psmouse_sliced_command(parent, c))
320                 return -1;
321         if (ps2_command(&parent->ps2dev, &rate_param, PSMOUSE_CMD_SETRATE))
322                 return -1;
323         return 0;
324 }
325
326 static int synaptics_pt_start(struct serio *serio)
327 {
328         struct psmouse *parent = serio_get_drvdata(serio->parent);
329         struct synaptics_data *priv = parent->private;
330
331         serio_pause_rx(parent->ps2dev.serio);
332         priv->pt_port = serio;
333         serio_continue_rx(parent->ps2dev.serio);
334
335         return 0;
336 }
337
338 static void synaptics_pt_stop(struct serio *serio)
339 {
340         struct psmouse *parent = serio_get_drvdata(serio->parent);
341         struct synaptics_data *priv = parent->private;
342
343         serio_pause_rx(parent->ps2dev.serio);
344         priv->pt_port = NULL;
345         serio_continue_rx(parent->ps2dev.serio);
346 }
347
348 static int synaptics_is_pt_packet(unsigned char *buf)
349 {
350         return (buf[0] & 0xFC) == 0x84 && (buf[3] & 0xCC) == 0xC4;
351 }
352
353 static void synaptics_pass_pt_packet(struct serio *ptport, unsigned char *packet)
354 {
355         struct psmouse *child = serio_get_drvdata(ptport);
356
357         if (child && child->state == PSMOUSE_ACTIVATED) {
358                 serio_interrupt(ptport, packet[1], 0);
359                 serio_interrupt(ptport, packet[4], 0);
360                 serio_interrupt(ptport, packet[5], 0);
361                 if (child->pktsize == 4)
362                         serio_interrupt(ptport, packet[2], 0);
363         } else
364                 serio_interrupt(ptport, packet[1], 0);
365 }
366
367 static void synaptics_pt_activate(struct psmouse *psmouse)
368 {
369         struct synaptics_data *priv = psmouse->private;
370         struct psmouse *child = serio_get_drvdata(priv->pt_port);
371
372         /* adjust the touchpad to child's choice of protocol */
373         if (child) {
374                 if (child->pktsize == 4)
375                         priv->mode |= SYN_BIT_FOUR_BYTE_CLIENT;
376                 else
377                         priv->mode &= ~SYN_BIT_FOUR_BYTE_CLIENT;
378
379                 if (synaptics_mode_cmd(psmouse, priv->mode))
380                         printk(KERN_INFO "synaptics: failed to switch guest protocol\n");
381         }
382 }
383
384 static void synaptics_pt_create(struct psmouse *psmouse)
385 {
386         struct serio *serio;
387
388         serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
389         if (!serio) {
390                 printk(KERN_ERR "synaptics: not enough memory to allocate pass-through port\n");
391                 return;
392         }
393
394         serio->id.type = SERIO_PS_PSTHRU;
395         strlcpy(serio->name, "Synaptics pass-through", sizeof(serio->name));
396         strlcpy(serio->phys, "synaptics-pt/serio0", sizeof(serio->name));
397         serio->write = synaptics_pt_write;
398         serio->start = synaptics_pt_start;
399         serio->stop = synaptics_pt_stop;
400         serio->parent = psmouse->ps2dev.serio;
401
402         psmouse->pt_activate = synaptics_pt_activate;
403
404         printk(KERN_INFO "serio: %s port at %s\n", serio->name, psmouse->phys);
405         serio_register_port(serio);
406 }
407
408 /*****************************************************************************
409  *      Functions to interpret the absolute mode packets
410  ****************************************************************************/
411
412 static int synaptics_parse_hw_state(const unsigned char buf[],
413                                     struct synaptics_data *priv,
414                                     struct synaptics_hw_state *hw)
415 {
416         memset(hw, 0, sizeof(struct synaptics_hw_state));
417
418         if (SYN_MODEL_NEWABS(priv->model_id)) {
419                 hw->w = (((buf[0] & 0x30) >> 2) |
420                          ((buf[0] & 0x04) >> 1) |
421                          ((buf[3] & 0x04) >> 2));
422
423                 hw->left  = (buf[0] & 0x01) ? 1 : 0;
424                 hw->right = (buf[0] & 0x02) ? 1 : 0;
425
426                 if (SYN_CAP_CLICKPAD(priv->ext_cap_0c)) {
427                         /*
428                          * Clickpad's button is transmitted as middle button,
429                          * however, since it is primary button, we will report
430                          * it as BTN_LEFT.
431                          */
432                         hw->left = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0;
433
434                 } else if (SYN_CAP_MIDDLE_BUTTON(priv->capabilities)) {
435                         hw->middle = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0;
436                         if (hw->w == 2)
437                                 hw->scroll = (signed char)(buf[1]);
438                 }
439
440                 if (SYN_CAP_FOUR_BUTTON(priv->capabilities)) {
441                         hw->up   = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0;
442                         hw->down = ((buf[0] ^ buf[3]) & 0x02) ? 1 : 0;
443                 }
444
445                 if (SYN_CAP_ADV_GESTURE(priv->ext_cap_0c) && hw->w == 2) {
446                         /* Gesture packet: (x, y, z) at half resolution */
447                         priv->mt.x = (((buf[4] & 0x0f) << 8) | buf[1]) << 1;
448                         priv->mt.y = (((buf[4] & 0xf0) << 4) | buf[2]) << 1;
449                         priv->mt.z = ((buf[3] & 0x30) | (buf[5] & 0x0f)) << 1;
450                         return 1;
451                 }
452
453                 hw->x = (((buf[3] & 0x10) << 8) |
454                          ((buf[1] & 0x0f) << 8) |
455                          buf[4]);
456                 hw->y = (((buf[3] & 0x20) << 7) |
457                          ((buf[1] & 0xf0) << 4) |
458                          buf[5]);
459                 hw->z = buf[2];
460
461                 if (SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap) &&
462                     ((buf[0] ^ buf[3]) & 0x02)) {
463                         switch (SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap) & ~0x01) {
464                         default:
465                                 /*
466                                  * if nExtBtn is greater than 8 it should be
467                                  * considered invalid and treated as 0
468                                  */
469                                 break;
470                         case 8:
471                                 hw->ext_buttons |= ((buf[5] & 0x08)) ? 0x80 : 0;
472                                 hw->ext_buttons |= ((buf[4] & 0x08)) ? 0x40 : 0;
473                         case 6:
474                                 hw->ext_buttons |= ((buf[5] & 0x04)) ? 0x20 : 0;
475                                 hw->ext_buttons |= ((buf[4] & 0x04)) ? 0x10 : 0;
476                         case 4:
477                                 hw->ext_buttons |= ((buf[5] & 0x02)) ? 0x08 : 0;
478                                 hw->ext_buttons |= ((buf[4] & 0x02)) ? 0x04 : 0;
479                         case 2:
480                                 hw->ext_buttons |= ((buf[5] & 0x01)) ? 0x02 : 0;
481                                 hw->ext_buttons |= ((buf[4] & 0x01)) ? 0x01 : 0;
482                         }
483                 }
484         } else {
485                 hw->x = (((buf[1] & 0x1f) << 8) | buf[2]);
486                 hw->y = (((buf[4] & 0x1f) << 8) | buf[5]);
487
488                 hw->z = (((buf[0] & 0x30) << 2) | (buf[3] & 0x3F));
489                 hw->w = (((buf[1] & 0x80) >> 4) | ((buf[0] & 0x04) >> 1));
490
491                 hw->left  = (buf[0] & 0x01) ? 1 : 0;
492                 hw->right = (buf[0] & 0x02) ? 1 : 0;
493         }
494
495         return 0;
496 }
497
498 static void synaptics_report_semi_mt_slot(struct input_dev *dev, int slot,
499                                           bool active, int x, int y)
500 {
501         input_mt_slot(dev, slot);
502         input_mt_report_slot_state(dev, MT_TOOL_FINGER, active);
503         if (active) {
504                 input_report_abs(dev, ABS_MT_POSITION_X, x);
505                 input_report_abs(dev, ABS_MT_POSITION_Y,
506                                  YMAX_NOMINAL + YMIN_NOMINAL - y);
507         }
508 }
509
510 static void synaptics_report_semi_mt_data(struct input_dev *dev,
511                                           const struct synaptics_hw_state *a,
512                                           const struct synaptics_hw_state *b,
513                                           int num_fingers)
514 {
515         if (num_fingers >= 2) {
516                 synaptics_report_semi_mt_slot(dev, 0, true, min(a->x, b->x),
517                                               min(a->y, b->y));
518                 synaptics_report_semi_mt_slot(dev, 1, true, max(a->x, b->x),
519                                               max(a->y, b->y));
520         } else if (num_fingers == 1) {
521                 synaptics_report_semi_mt_slot(dev, 0, true, a->x, a->y);
522                 synaptics_report_semi_mt_slot(dev, 1, false, 0, 0);
523         } else {
524                 synaptics_report_semi_mt_slot(dev, 0, false, 0, 0);
525                 synaptics_report_semi_mt_slot(dev, 1, false, 0, 0);
526         }
527 }
528
529 /*
530  *  called for each full received packet from the touchpad
531  */
532 static void synaptics_process_packet(struct psmouse *psmouse)
533 {
534         struct input_dev *dev = psmouse->dev;
535         struct synaptics_data *priv = psmouse->private;
536         struct synaptics_hw_state hw;
537         int num_fingers;
538         int finger_width;
539         int i;
540
541         if (synaptics_parse_hw_state(psmouse->packet, priv, &hw))
542                 return;
543
544         if (hw.scroll) {
545                 priv->scroll += hw.scroll;
546
547                 while (priv->scroll >= 4) {
548                         input_report_key(dev, BTN_BACK, !hw.down);
549                         input_sync(dev);
550                         input_report_key(dev, BTN_BACK, hw.down);
551                         input_sync(dev);
552                         priv->scroll -= 4;
553                 }
554                 while (priv->scroll <= -4) {
555                         input_report_key(dev, BTN_FORWARD, !hw.up);
556                         input_sync(dev);
557                         input_report_key(dev, BTN_FORWARD, hw.up);
558                         input_sync(dev);
559                         priv->scroll += 4;
560                 }
561                 return;
562         }
563
564         if (hw.z > 0 && hw.x > 1) {
565                 num_fingers = 1;
566                 finger_width = 5;
567                 if (SYN_CAP_EXTENDED(priv->capabilities)) {
568                         switch (hw.w) {
569                         case 0 ... 1:
570                                 if (SYN_CAP_MULTIFINGER(priv->capabilities))
571                                         num_fingers = hw.w + 2;
572                                 break;
573                         case 2:
574                                 if (SYN_MODEL_PEN(priv->model_id))
575                                         ;   /* Nothing, treat a pen as a single finger */
576                                 break;
577                         case 4 ... 15:
578                                 if (SYN_CAP_PALMDETECT(priv->capabilities))
579                                         finger_width = hw.w;
580                                 break;
581                         }
582                 }
583         } else {
584                 num_fingers = 0;
585                 finger_width = 0;
586         }
587
588         if (SYN_CAP_ADV_GESTURE(priv->ext_cap_0c))
589                 synaptics_report_semi_mt_data(dev, &hw, &priv->mt, num_fingers);
590
591         /* Post events
592          * BTN_TOUCH has to be first as mousedev relies on it when doing
593          * absolute -> relative conversion
594          */
595         if (hw.z > 30) input_report_key(dev, BTN_TOUCH, 1);
596         if (hw.z < 25) input_report_key(dev, BTN_TOUCH, 0);
597
598         if (num_fingers > 0) {
599                 input_report_abs(dev, ABS_X, hw.x);
600                 input_report_abs(dev, ABS_Y, YMAX_NOMINAL + YMIN_NOMINAL - hw.y);
601         }
602         input_report_abs(dev, ABS_PRESSURE, hw.z);
603
604         if (SYN_CAP_PALMDETECT(priv->capabilities))
605                 input_report_abs(dev, ABS_TOOL_WIDTH, finger_width);
606
607         input_report_key(dev, BTN_TOOL_FINGER, num_fingers == 1);
608         input_report_key(dev, BTN_LEFT, hw.left);
609         input_report_key(dev, BTN_RIGHT, hw.right);
610
611         if (SYN_CAP_MULTIFINGER(priv->capabilities)) {
612                 input_report_key(dev, BTN_TOOL_DOUBLETAP, num_fingers == 2);
613                 input_report_key(dev, BTN_TOOL_TRIPLETAP, num_fingers == 3);
614         }
615
616         if (SYN_CAP_MIDDLE_BUTTON(priv->capabilities))
617                 input_report_key(dev, BTN_MIDDLE, hw.middle);
618
619         if (SYN_CAP_FOUR_BUTTON(priv->capabilities)) {
620                 input_report_key(dev, BTN_FORWARD, hw.up);
621                 input_report_key(dev, BTN_BACK, hw.down);
622         }
623
624         for (i = 0; i < SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap); i++)
625                 input_report_key(dev, BTN_0 + i, hw.ext_buttons & (1 << i));
626
627         input_sync(dev);
628 }
629
630 static int synaptics_validate_byte(unsigned char packet[], int idx, unsigned char pkt_type)
631 {
632         static const unsigned char newabs_mask[]        = { 0xC8, 0x00, 0x00, 0xC8, 0x00 };
633         static const unsigned char newabs_rel_mask[]    = { 0xC0, 0x00, 0x00, 0xC0, 0x00 };
634         static const unsigned char newabs_rslt[]        = { 0x80, 0x00, 0x00, 0xC0, 0x00 };
635         static const unsigned char oldabs_mask[]        = { 0xC0, 0x60, 0x00, 0xC0, 0x60 };
636         static const unsigned char oldabs_rslt[]        = { 0xC0, 0x00, 0x00, 0x80, 0x00 };
637
638         if (idx < 0 || idx > 4)
639                 return 0;
640
641         switch (pkt_type) {
642
643         case SYN_NEWABS:
644         case SYN_NEWABS_RELAXED:
645                 return (packet[idx] & newabs_rel_mask[idx]) == newabs_rslt[idx];
646
647         case SYN_NEWABS_STRICT:
648                 return (packet[idx] & newabs_mask[idx]) == newabs_rslt[idx];
649
650         case SYN_OLDABS:
651                 return (packet[idx] & oldabs_mask[idx]) == oldabs_rslt[idx];
652
653         default:
654                 printk(KERN_ERR "synaptics: unknown packet type %d\n", pkt_type);
655                 return 0;
656         }
657 }
658
659 static unsigned char synaptics_detect_pkt_type(struct psmouse *psmouse)
660 {
661         int i;
662
663         for (i = 0; i < 5; i++)
664                 if (!synaptics_validate_byte(psmouse->packet, i, SYN_NEWABS_STRICT)) {
665                         printk(KERN_INFO "synaptics: using relaxed packet validation\n");
666                         return SYN_NEWABS_RELAXED;
667                 }
668
669         return SYN_NEWABS_STRICT;
670 }
671
672 static psmouse_ret_t synaptics_process_byte(struct psmouse *psmouse)
673 {
674         struct synaptics_data *priv = psmouse->private;
675
676         if (psmouse->pktcnt >= 6) { /* Full packet received */
677                 if (unlikely(priv->pkt_type == SYN_NEWABS))
678                         priv->pkt_type = synaptics_detect_pkt_type(psmouse);
679
680                 if (SYN_CAP_PASS_THROUGH(priv->capabilities) &&
681                     synaptics_is_pt_packet(psmouse->packet)) {
682                         if (priv->pt_port)
683                                 synaptics_pass_pt_packet(priv->pt_port, psmouse->packet);
684                 } else
685                         synaptics_process_packet(psmouse);
686
687                 return PSMOUSE_FULL_PACKET;
688         }
689
690         return synaptics_validate_byte(psmouse->packet, psmouse->pktcnt - 1, priv->pkt_type) ?
691                 PSMOUSE_GOOD_DATA : PSMOUSE_BAD_DATA;
692 }
693
694 /*****************************************************************************
695  *      Driver initialization/cleanup functions
696  ****************************************************************************/
697 static void set_input_params(struct input_dev *dev, struct synaptics_data *priv)
698 {
699         int i;
700         int fuzz = SYN_CAP_REDUCED_FILTERING(priv->ext_cap_0c) ?
701                         SYN_REDUCED_FILTER_FUZZ : 0;
702
703         __set_bit(INPUT_PROP_POINTER, dev->propbit);
704
705         __set_bit(EV_ABS, dev->evbit);
706         input_set_abs_params(dev, ABS_X,
707                              priv->x_min ?: XMIN_NOMINAL,
708                              priv->x_max ?: XMAX_NOMINAL,
709                              fuzz, 0);
710         input_set_abs_params(dev, ABS_Y,
711                              priv->y_min ?: YMIN_NOMINAL,
712                              priv->y_max ?: YMAX_NOMINAL,
713                              fuzz, 0);
714         input_set_abs_params(dev, ABS_PRESSURE, 0, 255, 0, 0);
715
716         if (SYN_CAP_ADV_GESTURE(priv->ext_cap_0c)) {
717                 __set_bit(INPUT_PROP_SEMI_MT, dev->propbit);
718                 input_mt_init_slots(dev, 2);
719                 input_set_abs_params(dev, ABS_MT_POSITION_X,
720                                      priv->x_min ?: XMIN_NOMINAL,
721                                      priv->x_max ?: XMAX_NOMINAL,
722                                      fuzz, 0);
723                 input_set_abs_params(dev, ABS_MT_POSITION_Y,
724                                      priv->y_min ?: YMIN_NOMINAL,
725                                      priv->y_max ?: YMAX_NOMINAL,
726                                      fuzz, 0);
727
728                 input_abs_set_res(dev, ABS_MT_POSITION_X, priv->x_res);
729                 input_abs_set_res(dev, ABS_MT_POSITION_Y, priv->y_res);
730         }
731
732         if (SYN_CAP_PALMDETECT(priv->capabilities))
733                 input_set_abs_params(dev, ABS_TOOL_WIDTH, 0, 15, 0, 0);
734
735         __set_bit(EV_KEY, dev->evbit);
736         __set_bit(BTN_TOUCH, dev->keybit);
737         __set_bit(BTN_TOOL_FINGER, dev->keybit);
738         __set_bit(BTN_LEFT, dev->keybit);
739         __set_bit(BTN_RIGHT, dev->keybit);
740
741         if (SYN_CAP_MULTIFINGER(priv->capabilities)) {
742                 __set_bit(BTN_TOOL_DOUBLETAP, dev->keybit);
743                 __set_bit(BTN_TOOL_TRIPLETAP, dev->keybit);
744         }
745
746         if (SYN_CAP_MIDDLE_BUTTON(priv->capabilities))
747                 __set_bit(BTN_MIDDLE, dev->keybit);
748
749         if (SYN_CAP_FOUR_BUTTON(priv->capabilities) ||
750             SYN_CAP_MIDDLE_BUTTON(priv->capabilities)) {
751                 __set_bit(BTN_FORWARD, dev->keybit);
752                 __set_bit(BTN_BACK, dev->keybit);
753         }
754
755         for (i = 0; i < SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap); i++)
756                 __set_bit(BTN_0 + i, dev->keybit);
757
758         __clear_bit(EV_REL, dev->evbit);
759         __clear_bit(REL_X, dev->relbit);
760         __clear_bit(REL_Y, dev->relbit);
761
762         input_abs_set_res(dev, ABS_X, priv->x_res);
763         input_abs_set_res(dev, ABS_Y, priv->y_res);
764
765         if (SYN_CAP_CLICKPAD(priv->ext_cap_0c)) {
766                 __set_bit(INPUT_PROP_BUTTONPAD, dev->propbit);
767                 /* Clickpads report only left button */
768                 __clear_bit(BTN_RIGHT, dev->keybit);
769                 __clear_bit(BTN_MIDDLE, dev->keybit);
770         }
771 }
772
773 static void synaptics_disconnect(struct psmouse *psmouse)
774 {
775         synaptics_reset(psmouse);
776         kfree(psmouse->private);
777         psmouse->private = NULL;
778 }
779
780 static int synaptics_reconnect(struct psmouse *psmouse)
781 {
782         struct synaptics_data *priv = psmouse->private;
783         struct synaptics_data old_priv = *priv;
784         int retry = 0;
785         int error;
786
787         do {
788                 psmouse_reset(psmouse);
789                 error = synaptics_detect(psmouse, 0);
790         } while (error && ++retry < 3);
791
792         if (error)
793                 return -1;
794
795         if (retry > 1)
796                 printk(KERN_DEBUG "Synaptics reconnected after %d tries\n",
797                         retry);
798
799         if (synaptics_query_hardware(psmouse)) {
800                 printk(KERN_ERR "Unable to query Synaptics hardware.\n");
801                 return -1;
802         }
803
804         if (synaptics_set_absolute_mode(psmouse)) {
805                 printk(KERN_ERR "Unable to initialize Synaptics hardware.\n");
806                 return -1;
807         }
808
809         if (synaptics_set_advanced_gesture_mode(psmouse)) {
810                 printk(KERN_ERR "Advanced gesture mode reconnect failed.\n");
811                 return -1;
812         }
813
814         if (old_priv.identity != priv->identity ||
815             old_priv.model_id != priv->model_id ||
816             old_priv.capabilities != priv->capabilities ||
817             old_priv.ext_cap != priv->ext_cap) {
818                 printk(KERN_ERR "Synaptics hardware appears to be different: "
819                         "id(%ld-%ld), model(%ld-%ld), caps(%lx-%lx), ext(%lx-%lx).\n",
820                         old_priv.identity, priv->identity,
821                         old_priv.model_id, priv->model_id,
822                         old_priv.capabilities, priv->capabilities,
823                         old_priv.ext_cap, priv->ext_cap);
824                 return -1;
825         }
826
827         return 0;
828 }
829
830 static bool impaired_toshiba_kbc;
831
832 static const struct dmi_system_id __initconst toshiba_dmi_table[] = {
833 #if defined(CONFIG_DMI) && defined(CONFIG_X86)
834         {
835                 /* Toshiba Satellite */
836                 .matches = {
837                         DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
838                         DMI_MATCH(DMI_PRODUCT_NAME, "Satellite"),
839                 },
840         },
841         {
842                 /* Toshiba Dynabook */
843                 .matches = {
844                         DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
845                         DMI_MATCH(DMI_PRODUCT_NAME, "dynabook"),
846                 },
847         },
848         {
849                 /* Toshiba Portege M300 */
850                 .matches = {
851                         DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
852                         DMI_MATCH(DMI_PRODUCT_NAME, "PORTEGE M300"),
853                 },
854
855         },
856         {
857                 /* Toshiba Portege M300 */
858                 .matches = {
859                         DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
860                         DMI_MATCH(DMI_PRODUCT_NAME, "Portable PC"),
861                         DMI_MATCH(DMI_PRODUCT_VERSION, "Version 1.0"),
862                 },
863
864         },
865 #endif
866         { }
867 };
868
869 static bool broken_olpc_ec;
870
871 static const struct dmi_system_id __initconst olpc_dmi_table[] = {
872 #if defined(CONFIG_DMI) && defined(CONFIG_OLPC)
873         {
874                 /* OLPC XO-1 or XO-1.5 */
875                 .matches = {
876                         DMI_MATCH(DMI_SYS_VENDOR, "OLPC"),
877                         DMI_MATCH(DMI_PRODUCT_NAME, "XO"),
878                 },
879         },
880 #endif
881         { }
882 };
883
884 void __init synaptics_module_init(void)
885 {
886         impaired_toshiba_kbc = dmi_check_system(toshiba_dmi_table);
887         broken_olpc_ec = dmi_check_system(olpc_dmi_table);
888 }
889
890 int synaptics_init(struct psmouse *psmouse)
891 {
892         struct synaptics_data *priv;
893
894         /*
895          * The OLPC XO has issues with Synaptics' absolute mode; similarly to
896          * the HGPK, it quickly degrades and the hardware becomes jumpy and
897          * overly sensitive.  Not only that, but the constant packet spew
898          * (even at a lowered 40pps rate) overloads the EC such that key
899          * presses on the keyboard are missed.  Given all of that, don't
900          * even attempt to use Synaptics mode.  Relative mode seems to work
901          * just fine.
902          */
903         if (broken_olpc_ec) {
904                 printk(KERN_INFO "synaptics: OLPC XO detected, not enabling Synaptics protocol.\n");
905                 return -ENODEV;
906         }
907
908         psmouse->private = priv = kzalloc(sizeof(struct synaptics_data), GFP_KERNEL);
909         if (!priv)
910                 return -ENOMEM;
911
912         psmouse_reset(psmouse);
913
914         if (synaptics_query_hardware(psmouse)) {
915                 printk(KERN_ERR "Unable to query Synaptics hardware.\n");
916                 goto init_fail;
917         }
918
919         if (synaptics_set_absolute_mode(psmouse)) {
920                 printk(KERN_ERR "Unable to initialize Synaptics hardware.\n");
921                 goto init_fail;
922         }
923
924         if (synaptics_set_advanced_gesture_mode(psmouse)) {
925                 printk(KERN_ERR "Advanced gesture mode init failed.\n");
926                 goto init_fail;
927         }
928
929         priv->pkt_type = SYN_MODEL_NEWABS(priv->model_id) ? SYN_NEWABS : SYN_OLDABS;
930
931         printk(KERN_INFO "Synaptics Touchpad, model: %ld, fw: %ld.%ld, id: %#lx, caps: %#lx/%#lx/%#lx\n",
932                 SYN_ID_MODEL(priv->identity),
933                 SYN_ID_MAJOR(priv->identity), SYN_ID_MINOR(priv->identity),
934                 priv->model_id, priv->capabilities, priv->ext_cap, priv->ext_cap_0c);
935
936         set_input_params(psmouse->dev, priv);
937
938         /*
939          * Encode touchpad model so that it can be used to set
940          * input device->id.version and be visible to userspace.
941          * Because version is __u16 we have to drop something.
942          * Hardware info bits seem to be good candidates as they
943          * are documented to be for Synaptics corp. internal use.
944          */
945         psmouse->model = ((priv->model_id & 0x00ff0000) >> 8) |
946                           (priv->model_id & 0x000000ff);
947
948         psmouse->protocol_handler = synaptics_process_byte;
949         psmouse->set_rate = synaptics_set_rate;
950         psmouse->disconnect = synaptics_disconnect;
951         psmouse->reconnect = synaptics_reconnect;
952         psmouse->cleanup = synaptics_reset;
953         psmouse->pktsize = 6;
954         /* Synaptics can usually stay in sync without extra help */
955         psmouse->resync_time = 0;
956
957         if (SYN_CAP_PASS_THROUGH(priv->capabilities))
958                 synaptics_pt_create(psmouse);
959
960         /*
961          * Toshiba's KBC seems to have trouble handling data from
962          * Synaptics at full rate.  Switch to a lower rate (roughly
963          * the same rate as a standard PS/2 mouse).
964          */
965         if (psmouse->rate >= 80 && impaired_toshiba_kbc) {
966                 printk(KERN_INFO "synaptics: Toshiba %s detected, limiting rate to 40pps.\n",
967                         dmi_get_system_info(DMI_PRODUCT_NAME));
968                 psmouse->rate = 40;
969         }
970
971         return 0;
972
973  init_fail:
974         kfree(priv);
975         return -1;
976 }
977
978 bool synaptics_supported(void)
979 {
980         return true;
981 }
982
983 #else /* CONFIG_MOUSE_PS2_SYNAPTICS */
984
985 void __init synaptics_module_init(void)
986 {
987 }
988
989 int synaptics_init(struct psmouse *psmouse)
990 {
991         return -ENOSYS;
992 }
993
994 bool synaptics_supported(void)
995 {
996         return false;
997 }
998
999 #endif /* CONFIG_MOUSE_PS2_SYNAPTICS */