Merge branch 'stable-3.2' into pandora-3.2
[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/delay.h>
28 #include <linux/dmi.h>
29 #include <linux/input/mt.h>
30 #include <linux/serio.h>
31 #include <linux/libps2.h>
32 #include <linux/slab.h>
33 #include "psmouse.h"
34 #include "synaptics.h"
35
36 /*
37  * The x/y limits are taken from the Synaptics TouchPad interfacing Guide,
38  * section 2.3.2, which says that they should be valid regardless of the
39  * actual size of the sensor.
40  * Note that newer firmware allows querying device for maximum useable
41  * coordinates.
42  */
43 #define XMIN 0
44 #define XMAX 6143
45 #define YMIN 0
46 #define YMAX 6143
47 #define XMIN_NOMINAL 1472
48 #define XMAX_NOMINAL 5472
49 #define YMIN_NOMINAL 1408
50 #define YMAX_NOMINAL 4448
51
52 /* Size in bits of absolute position values reported by the hardware */
53 #define ABS_POS_BITS 13
54
55 /*
56  * Any position values from the hardware above the following limits are
57  * treated as "wrapped around negative" values that have been truncated to
58  * the 13-bit reporting range of the hardware. These are just reasonable
59  * guesses and can be adjusted if hardware is found that operates outside
60  * of these parameters.
61  */
62 #define X_MAX_POSITIVE (((1 << ABS_POS_BITS) + XMAX) / 2)
63 #define Y_MAX_POSITIVE (((1 << ABS_POS_BITS) + YMAX) / 2)
64
65 /*
66  * Synaptics touchpads report the y coordinate from bottom to top, which is
67  * opposite from what userspace expects.
68  * This function is used to invert y before reporting.
69  */
70 static int synaptics_invert_y(int y)
71 {
72         return YMAX_NOMINAL + YMIN_NOMINAL - y;
73 }
74
75
76 /*****************************************************************************
77  *      Stuff we need even when we do not want native Synaptics support
78  ****************************************************************************/
79
80 /*
81  * Set the synaptics touchpad mode byte by special commands
82  */
83 static int synaptics_mode_cmd(struct psmouse *psmouse, unsigned char mode)
84 {
85         unsigned char param[1];
86
87         if (psmouse_sliced_command(psmouse, mode))
88                 return -1;
89         param[0] = SYN_PS_SET_MODE2;
90         if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_SETRATE))
91                 return -1;
92         return 0;
93 }
94
95 int synaptics_detect(struct psmouse *psmouse, bool set_properties)
96 {
97         struct ps2dev *ps2dev = &psmouse->ps2dev;
98         unsigned char param[4];
99
100         param[0] = 0;
101
102         ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
103         ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
104         ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
105         ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
106         ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO);
107
108         if (param[1] != 0x47)
109                 return -ENODEV;
110
111         if (set_properties) {
112                 psmouse->vendor = "Synaptics";
113                 psmouse->name = "TouchPad";
114         }
115
116         return 0;
117 }
118
119 void synaptics_reset(struct psmouse *psmouse)
120 {
121         /* reset touchpad back to relative mode, gestures enabled */
122         synaptics_mode_cmd(psmouse, 0);
123 }
124
125 #ifdef CONFIG_MOUSE_PS2_SYNAPTICS
126
127 /*****************************************************************************
128  *      Synaptics communications functions
129  ****************************************************************************/
130
131 /*
132  * Send a command to the synpatics touchpad by special commands
133  */
134 static int synaptics_send_cmd(struct psmouse *psmouse, unsigned char c, unsigned char *param)
135 {
136         if (psmouse_sliced_command(psmouse, c))
137                 return -1;
138         if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_GETINFO))
139                 return -1;
140         return 0;
141 }
142
143 /*
144  * Read the model-id bytes from the touchpad
145  * see also SYN_MODEL_* macros
146  */
147 static int synaptics_model_id(struct psmouse *psmouse)
148 {
149         struct synaptics_data *priv = psmouse->private;
150         unsigned char mi[3];
151
152         if (synaptics_send_cmd(psmouse, SYN_QUE_MODEL, mi))
153                 return -1;
154         priv->model_id = (mi[0]<<16) | (mi[1]<<8) | mi[2];
155         return 0;
156 }
157
158 /*
159  * Read the capability-bits from the touchpad
160  * see also the SYN_CAP_* macros
161  */
162 static int synaptics_capability(struct psmouse *psmouse)
163 {
164         struct synaptics_data *priv = psmouse->private;
165         unsigned char cap[3];
166
167         if (synaptics_send_cmd(psmouse, SYN_QUE_CAPABILITIES, cap))
168                 return -1;
169         priv->capabilities = (cap[0] << 16) | (cap[1] << 8) | cap[2];
170         priv->ext_cap = priv->ext_cap_0c = 0;
171
172         /*
173          * Older firmwares had submodel ID fixed to 0x47
174          */
175         if (SYN_ID_FULL(priv->identity) < 0x705 &&
176             SYN_CAP_SUBMODEL_ID(priv->capabilities) != 0x47) {
177                 return -1;
178         }
179
180         /*
181          * Unless capExtended is set the rest of the flags should be ignored
182          */
183         if (!SYN_CAP_EXTENDED(priv->capabilities))
184                 priv->capabilities = 0;
185
186         if (SYN_EXT_CAP_REQUESTS(priv->capabilities) >= 1) {
187                 if (synaptics_send_cmd(psmouse, SYN_QUE_EXT_CAPAB, cap)) {
188                         psmouse_warn(psmouse,
189                                      "device claims to have extended capabilities, but I'm not able to read them.\n");
190                 } else {
191                         priv->ext_cap = (cap[0] << 16) | (cap[1] << 8) | cap[2];
192
193                         /*
194                          * if nExtBtn is greater than 8 it should be considered
195                          * invalid and treated as 0
196                          */
197                         if (SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap) > 8)
198                                 priv->ext_cap &= 0xff0fff;
199                 }
200         }
201
202         if (SYN_EXT_CAP_REQUESTS(priv->capabilities) >= 4) {
203                 if (synaptics_send_cmd(psmouse, SYN_QUE_EXT_CAPAB_0C, cap)) {
204                         psmouse_warn(psmouse,
205                                      "device claims to have extended capability 0x0c, but I'm not able to read it.\n");
206                 } else {
207                         priv->ext_cap_0c = (cap[0] << 16) | (cap[1] << 8) | cap[2];
208                 }
209         }
210
211         return 0;
212 }
213
214 /*
215  * Identify Touchpad
216  * See also the SYN_ID_* macros
217  */
218 static int synaptics_identify(struct psmouse *psmouse)
219 {
220         struct synaptics_data *priv = psmouse->private;
221         unsigned char id[3];
222
223         if (synaptics_send_cmd(psmouse, SYN_QUE_IDENTIFY, id))
224                 return -1;
225         priv->identity = (id[0]<<16) | (id[1]<<8) | id[2];
226         if (SYN_ID_IS_SYNAPTICS(priv->identity))
227                 return 0;
228         return -1;
229 }
230
231 /*
232  * Read touchpad resolution and maximum reported coordinates
233  * Resolution is left zero if touchpad does not support the query
234  */
235 static int synaptics_resolution(struct psmouse *psmouse)
236 {
237         struct synaptics_data *priv = psmouse->private;
238         unsigned char resp[3];
239
240         if (SYN_ID_MAJOR(priv->identity) < 4)
241                 return 0;
242
243         if (synaptics_send_cmd(psmouse, SYN_QUE_RESOLUTION, resp) == 0) {
244                 if (resp[0] != 0 && (resp[1] & 0x80) && resp[2] != 0) {
245                         priv->x_res = resp[0]; /* x resolution in units/mm */
246                         priv->y_res = resp[2]; /* y resolution in units/mm */
247                 }
248         }
249
250         if (SYN_EXT_CAP_REQUESTS(priv->capabilities) >= 5 &&
251             SYN_CAP_MAX_DIMENSIONS(priv->ext_cap_0c)) {
252                 if (synaptics_send_cmd(psmouse, SYN_QUE_EXT_MAX_COORDS, resp)) {
253                         psmouse_warn(psmouse,
254                                      "device claims to have max coordinates query, but I'm not able to read it.\n");
255                 } else {
256                         priv->x_max = (resp[0] << 5) | ((resp[1] & 0x0f) << 1);
257                         priv->y_max = (resp[2] << 5) | ((resp[1] & 0xf0) >> 3);
258                 }
259         }
260
261         if (SYN_EXT_CAP_REQUESTS(priv->capabilities) >= 7 &&
262             SYN_CAP_MIN_DIMENSIONS(priv->ext_cap_0c)) {
263                 if (synaptics_send_cmd(psmouse, SYN_QUE_EXT_MIN_COORDS, resp)) {
264                         psmouse_warn(psmouse,
265                                      "device claims to have min coordinates query, but I'm not able to read it.\n");
266                 } else {
267                         priv->x_min = (resp[0] << 5) | ((resp[1] & 0x0f) << 1);
268                         priv->y_min = (resp[2] << 5) | ((resp[1] & 0xf0) >> 3);
269                 }
270         }
271
272         return 0;
273 }
274
275 static int synaptics_query_hardware(struct psmouse *psmouse)
276 {
277         if (synaptics_identify(psmouse))
278                 return -1;
279         if (synaptics_model_id(psmouse))
280                 return -1;
281         if (synaptics_capability(psmouse))
282                 return -1;
283         if (synaptics_resolution(psmouse))
284                 return -1;
285
286         return 0;
287 }
288
289 static int synaptics_set_absolute_mode(struct psmouse *psmouse)
290 {
291         struct synaptics_data *priv = psmouse->private;
292
293         priv->mode = SYN_BIT_ABSOLUTE_MODE;
294         if (SYN_ID_MAJOR(priv->identity) >= 4)
295                 priv->mode |= SYN_BIT_DISABLE_GESTURE;
296         if (SYN_CAP_EXTENDED(priv->capabilities))
297                 priv->mode |= SYN_BIT_W_MODE;
298
299         if (synaptics_mode_cmd(psmouse, priv->mode))
300                 return -1;
301
302         return 0;
303 }
304
305 static void synaptics_set_rate(struct psmouse *psmouse, unsigned int rate)
306 {
307         struct synaptics_data *priv = psmouse->private;
308
309         if (rate >= 80) {
310                 priv->mode |= SYN_BIT_HIGH_RATE;
311                 psmouse->rate = 80;
312         } else {
313                 priv->mode &= ~SYN_BIT_HIGH_RATE;
314                 psmouse->rate = 40;
315         }
316
317         synaptics_mode_cmd(psmouse, priv->mode);
318 }
319
320 static int synaptics_set_advanced_gesture_mode(struct psmouse *psmouse)
321 {
322         static unsigned char param = 0xc8;
323         struct synaptics_data *priv = psmouse->private;
324
325         if (!(SYN_CAP_ADV_GESTURE(priv->ext_cap_0c) ||
326                         SYN_CAP_IMAGE_SENSOR(priv->ext_cap_0c)))
327                 return 0;
328
329         if (psmouse_sliced_command(psmouse, SYN_QUE_MODEL))
330                 return -1;
331         if (ps2_command(&psmouse->ps2dev, &param, PSMOUSE_CMD_SETRATE))
332                 return -1;
333
334         /* Advanced gesture mode also sends multi finger data */
335         priv->capabilities |= BIT(1);
336
337         return 0;
338 }
339
340 /*****************************************************************************
341  *      Synaptics pass-through PS/2 port support
342  ****************************************************************************/
343 static int synaptics_pt_write(struct serio *serio, unsigned char c)
344 {
345         struct psmouse *parent = serio_get_drvdata(serio->parent);
346         char rate_param = SYN_PS_CLIENT_CMD; /* indicates that we want pass-through port */
347
348         if (psmouse_sliced_command(parent, c))
349                 return -1;
350         if (ps2_command(&parent->ps2dev, &rate_param, PSMOUSE_CMD_SETRATE))
351                 return -1;
352         return 0;
353 }
354
355 static int synaptics_pt_start(struct serio *serio)
356 {
357         struct psmouse *parent = serio_get_drvdata(serio->parent);
358         struct synaptics_data *priv = parent->private;
359
360         serio_pause_rx(parent->ps2dev.serio);
361         priv->pt_port = serio;
362         serio_continue_rx(parent->ps2dev.serio);
363
364         return 0;
365 }
366
367 static void synaptics_pt_stop(struct serio *serio)
368 {
369         struct psmouse *parent = serio_get_drvdata(serio->parent);
370         struct synaptics_data *priv = parent->private;
371
372         serio_pause_rx(parent->ps2dev.serio);
373         priv->pt_port = NULL;
374         serio_continue_rx(parent->ps2dev.serio);
375 }
376
377 static int synaptics_is_pt_packet(unsigned char *buf)
378 {
379         return (buf[0] & 0xFC) == 0x84 && (buf[3] & 0xCC) == 0xC4;
380 }
381
382 static void synaptics_pass_pt_packet(struct serio *ptport, unsigned char *packet)
383 {
384         struct psmouse *child = serio_get_drvdata(ptport);
385
386         if (child && child->state == PSMOUSE_ACTIVATED) {
387                 serio_interrupt(ptport, packet[1], 0);
388                 serio_interrupt(ptport, packet[4], 0);
389                 serio_interrupt(ptport, packet[5], 0);
390                 if (child->pktsize == 4)
391                         serio_interrupt(ptport, packet[2], 0);
392         } else
393                 serio_interrupt(ptport, packet[1], 0);
394 }
395
396 static void synaptics_pt_activate(struct psmouse *psmouse)
397 {
398         struct synaptics_data *priv = psmouse->private;
399         struct psmouse *child = serio_get_drvdata(priv->pt_port);
400
401         /* adjust the touchpad to child's choice of protocol */
402         if (child) {
403                 if (child->pktsize == 4)
404                         priv->mode |= SYN_BIT_FOUR_BYTE_CLIENT;
405                 else
406                         priv->mode &= ~SYN_BIT_FOUR_BYTE_CLIENT;
407
408                 if (synaptics_mode_cmd(psmouse, priv->mode))
409                         psmouse_warn(psmouse,
410                                      "failed to switch guest protocol\n");
411         }
412 }
413
414 static void synaptics_pt_create(struct psmouse *psmouse)
415 {
416         struct serio *serio;
417
418         serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
419         if (!serio) {
420                 psmouse_err(psmouse,
421                             "not enough memory for pass-through port\n");
422                 return;
423         }
424
425         serio->id.type = SERIO_PS_PSTHRU;
426         strlcpy(serio->name, "Synaptics pass-through", sizeof(serio->name));
427         strlcpy(serio->phys, "synaptics-pt/serio0", sizeof(serio->name));
428         serio->write = synaptics_pt_write;
429         serio->start = synaptics_pt_start;
430         serio->stop = synaptics_pt_stop;
431         serio->parent = psmouse->ps2dev.serio;
432
433         psmouse->pt_activate = synaptics_pt_activate;
434
435         psmouse_info(psmouse, "serio: %s port at %s\n",
436                      serio->name, psmouse->phys);
437         serio_register_port(serio);
438 }
439
440 /*****************************************************************************
441  *      Functions to interpret the absolute mode packets
442  ****************************************************************************/
443
444 static void synaptics_mt_state_set(struct synaptics_mt_state *state, int count,
445                                    int sgm, int agm)
446 {
447         state->count = count;
448         state->sgm = sgm;
449         state->agm = agm;
450 }
451
452 static void synaptics_parse_agm(const unsigned char buf[],
453                                 struct synaptics_data *priv,
454                                 struct synaptics_hw_state *hw)
455 {
456         struct synaptics_hw_state *agm = &priv->agm;
457         int agm_packet_type;
458
459         agm_packet_type = (buf[5] & 0x30) >> 4;
460         switch (agm_packet_type) {
461         case 1:
462                 /* Gesture packet: (x, y, z) half resolution */
463                 agm->w = hw->w;
464                 agm->x = (((buf[4] & 0x0f) << 8) | buf[1]) << 1;
465                 agm->y = (((buf[4] & 0xf0) << 4) | buf[2]) << 1;
466                 agm->z = ((buf[3] & 0x30) | (buf[5] & 0x0f)) << 1;
467                 break;
468
469         case 2:
470                 /* AGM-CONTACT packet: (count, sgm, agm) */
471                 synaptics_mt_state_set(&agm->mt_state, buf[1], buf[2], buf[4]);
472                 break;
473
474         default:
475                 break;
476         }
477
478         /* Record that at least one AGM has been received since last SGM */
479         priv->agm_pending = true;
480 }
481
482 static int synaptics_parse_hw_state(const unsigned char buf[],
483                                     struct synaptics_data *priv,
484                                     struct synaptics_hw_state *hw)
485 {
486         memset(hw, 0, sizeof(struct synaptics_hw_state));
487
488         if (SYN_MODEL_NEWABS(priv->model_id)) {
489                 hw->w = (((buf[0] & 0x30) >> 2) |
490                          ((buf[0] & 0x04) >> 1) |
491                          ((buf[3] & 0x04) >> 2));
492
493                 hw->left  = (buf[0] & 0x01) ? 1 : 0;
494                 hw->right = (buf[0] & 0x02) ? 1 : 0;
495
496                 if (SYN_CAP_CLICKPAD(priv->ext_cap_0c)) {
497                         /*
498                          * Clickpad's button is transmitted as middle button,
499                          * however, since it is primary button, we will report
500                          * it as BTN_LEFT.
501                          */
502                         hw->left = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0;
503
504                 } else if (SYN_CAP_MIDDLE_BUTTON(priv->capabilities)) {
505                         hw->middle = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0;
506                         if (hw->w == 2)
507                                 hw->scroll = (signed char)(buf[1]);
508                 }
509
510                 if (SYN_CAP_FOUR_BUTTON(priv->capabilities)) {
511                         hw->up   = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0;
512                         hw->down = ((buf[0] ^ buf[3]) & 0x02) ? 1 : 0;
513                 }
514
515                 if ((SYN_CAP_ADV_GESTURE(priv->ext_cap_0c) ||
516                         SYN_CAP_IMAGE_SENSOR(priv->ext_cap_0c)) &&
517                     hw->w == 2) {
518                         synaptics_parse_agm(buf, priv, hw);
519                         return 1;
520                 }
521
522                 hw->x = (((buf[3] & 0x10) << 8) |
523                          ((buf[1] & 0x0f) << 8) |
524                          buf[4]);
525                 hw->y = (((buf[3] & 0x20) << 7) |
526                          ((buf[1] & 0xf0) << 4) |
527                          buf[5]);
528                 hw->z = buf[2];
529
530                 if (SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap) &&
531                     ((buf[0] ^ buf[3]) & 0x02)) {
532                         switch (SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap) & ~0x01) {
533                         default:
534                                 /*
535                                  * if nExtBtn is greater than 8 it should be
536                                  * considered invalid and treated as 0
537                                  */
538                                 break;
539                         case 8:
540                                 hw->ext_buttons |= ((buf[5] & 0x08)) ? 0x80 : 0;
541                                 hw->ext_buttons |= ((buf[4] & 0x08)) ? 0x40 : 0;
542                         case 6:
543                                 hw->ext_buttons |= ((buf[5] & 0x04)) ? 0x20 : 0;
544                                 hw->ext_buttons |= ((buf[4] & 0x04)) ? 0x10 : 0;
545                         case 4:
546                                 hw->ext_buttons |= ((buf[5] & 0x02)) ? 0x08 : 0;
547                                 hw->ext_buttons |= ((buf[4] & 0x02)) ? 0x04 : 0;
548                         case 2:
549                                 hw->ext_buttons |= ((buf[5] & 0x01)) ? 0x02 : 0;
550                                 hw->ext_buttons |= ((buf[4] & 0x01)) ? 0x01 : 0;
551                         }
552                 }
553         } else {
554                 hw->x = (((buf[1] & 0x1f) << 8) | buf[2]);
555                 hw->y = (((buf[4] & 0x1f) << 8) | buf[5]);
556
557                 hw->z = (((buf[0] & 0x30) << 2) | (buf[3] & 0x3F));
558                 hw->w = (((buf[1] & 0x80) >> 4) | ((buf[0] & 0x04) >> 1));
559
560                 hw->left  = (buf[0] & 0x01) ? 1 : 0;
561                 hw->right = (buf[0] & 0x02) ? 1 : 0;
562         }
563
564         /* Convert wrap-around values to negative */
565         if (hw->x > X_MAX_POSITIVE)
566                 hw->x -= 1 << ABS_POS_BITS;
567         if (hw->y > Y_MAX_POSITIVE)
568                 hw->y -= 1 << ABS_POS_BITS;
569
570         return 0;
571 }
572
573 static void synaptics_report_semi_mt_slot(struct input_dev *dev, int slot,
574                                           bool active, int x, int y)
575 {
576         input_mt_slot(dev, slot);
577         input_mt_report_slot_state(dev, MT_TOOL_FINGER, active);
578         if (active) {
579                 input_report_abs(dev, ABS_MT_POSITION_X, x);
580                 input_report_abs(dev, ABS_MT_POSITION_Y, synaptics_invert_y(y));
581         }
582 }
583
584 static void synaptics_report_semi_mt_data(struct input_dev *dev,
585                                           const struct synaptics_hw_state *a,
586                                           const struct synaptics_hw_state *b,
587                                           int num_fingers)
588 {
589         if (num_fingers >= 2) {
590                 synaptics_report_semi_mt_slot(dev, 0, true, min(a->x, b->x),
591                                               min(a->y, b->y));
592                 synaptics_report_semi_mt_slot(dev, 1, true, max(a->x, b->x),
593                                               max(a->y, b->y));
594         } else if (num_fingers == 1) {
595                 synaptics_report_semi_mt_slot(dev, 0, true, a->x, a->y);
596                 synaptics_report_semi_mt_slot(dev, 1, false, 0, 0);
597         } else {
598                 synaptics_report_semi_mt_slot(dev, 0, false, 0, 0);
599                 synaptics_report_semi_mt_slot(dev, 1, false, 0, 0);
600         }
601 }
602
603 static void synaptics_report_buttons(struct psmouse *psmouse,
604                                      const struct synaptics_hw_state *hw)
605 {
606         struct input_dev *dev = psmouse->dev;
607         struct synaptics_data *priv = psmouse->private;
608         int i;
609
610         input_report_key(dev, BTN_LEFT, hw->left);
611         input_report_key(dev, BTN_RIGHT, hw->right);
612
613         if (SYN_CAP_MIDDLE_BUTTON(priv->capabilities))
614                 input_report_key(dev, BTN_MIDDLE, hw->middle);
615
616         if (SYN_CAP_FOUR_BUTTON(priv->capabilities)) {
617                 input_report_key(dev, BTN_FORWARD, hw->up);
618                 input_report_key(dev, BTN_BACK, hw->down);
619         }
620
621         for (i = 0; i < SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap); i++)
622                 input_report_key(dev, BTN_0 + i, hw->ext_buttons & (1 << i));
623 }
624
625 static void synaptics_report_slot(struct input_dev *dev, int slot,
626                                   const struct synaptics_hw_state *hw)
627 {
628         input_mt_slot(dev, slot);
629         input_mt_report_slot_state(dev, MT_TOOL_FINGER, (hw != NULL));
630         if (!hw)
631                 return;
632
633         input_report_abs(dev, ABS_MT_POSITION_X, hw->x);
634         input_report_abs(dev, ABS_MT_POSITION_Y, synaptics_invert_y(hw->y));
635         input_report_abs(dev, ABS_MT_PRESSURE, hw->z);
636 }
637
638 static void synaptics_report_mt_data(struct psmouse *psmouse,
639                                      struct synaptics_mt_state *mt_state,
640                                      const struct synaptics_hw_state *sgm)
641 {
642         struct input_dev *dev = psmouse->dev;
643         struct synaptics_data *priv = psmouse->private;
644         struct synaptics_hw_state *agm = &priv->agm;
645         struct synaptics_mt_state *old = &priv->mt_state;
646
647         switch (mt_state->count) {
648         case 0:
649                 synaptics_report_slot(dev, 0, NULL);
650                 synaptics_report_slot(dev, 1, NULL);
651                 break;
652         case 1:
653                 if (mt_state->sgm == -1) {
654                         synaptics_report_slot(dev, 0, NULL);
655                         synaptics_report_slot(dev, 1, NULL);
656                 } else if (mt_state->sgm == 0) {
657                         synaptics_report_slot(dev, 0, sgm);
658                         synaptics_report_slot(dev, 1, NULL);
659                 } else {
660                         synaptics_report_slot(dev, 0, NULL);
661                         synaptics_report_slot(dev, 1, sgm);
662                 }
663                 break;
664         default:
665                 /*
666                  * If the finger slot contained in SGM is valid, and either
667                  * hasn't changed, or is new, then report SGM in MTB slot 0.
668                  * Otherwise, empty MTB slot 0.
669                  */
670                 if (mt_state->sgm != -1 &&
671                     (mt_state->sgm == old->sgm || old->sgm == -1))
672                         synaptics_report_slot(dev, 0, sgm);
673                 else
674                         synaptics_report_slot(dev, 0, NULL);
675
676                 /*
677                  * If the finger slot contained in AGM is valid, and either
678                  * hasn't changed, or is new, then report AGM in MTB slot 1.
679                  * Otherwise, empty MTB slot 1.
680                  */
681                 if (mt_state->agm != -1 &&
682                     (mt_state->agm == old->agm || old->agm == -1))
683                         synaptics_report_slot(dev, 1, agm);
684                 else
685                         synaptics_report_slot(dev, 1, NULL);
686                 break;
687         }
688
689         /* Don't use active slot count to generate BTN_TOOL events. */
690         input_mt_report_pointer_emulation(dev, false);
691
692         /* Send the number of fingers reported by touchpad itself. */
693         input_mt_report_finger_count(dev, mt_state->count);
694
695         synaptics_report_buttons(psmouse, sgm);
696
697         input_sync(dev);
698 }
699
700 /* Handle case where mt_state->count = 0 */
701 static void synaptics_image_sensor_0f(struct synaptics_data *priv,
702                                       struct synaptics_mt_state *mt_state)
703 {
704         synaptics_mt_state_set(mt_state, 0, -1, -1);
705         priv->mt_state_lost = false;
706 }
707
708 /* Handle case where mt_state->count = 1 */
709 static void synaptics_image_sensor_1f(struct synaptics_data *priv,
710                                       struct synaptics_mt_state *mt_state)
711 {
712         struct synaptics_hw_state *agm = &priv->agm;
713         struct synaptics_mt_state *old = &priv->mt_state;
714
715         /*
716          * If the last AGM was (0,0,0), and there is only one finger left,
717          * then we absolutely know that SGM contains slot 0, and all other
718          * fingers have been removed.
719          */
720         if (priv->agm_pending && agm->z == 0) {
721                 synaptics_mt_state_set(mt_state, 1, 0, -1);
722                 priv->mt_state_lost = false;
723                 return;
724         }
725
726         switch (old->count) {
727         case 0:
728                 synaptics_mt_state_set(mt_state, 1, 0, -1);
729                 break;
730         case 1:
731                 /*
732                  * If mt_state_lost, then the previous transition was 3->1,
733                  * and SGM now contains either slot 0 or 1, but we don't know
734                  * which.  So, we just assume that the SGM now contains slot 1.
735                  *
736                  * If pending AGM and either:
737                  *   (a) the previous SGM slot contains slot 0, or
738                  *   (b) there was no SGM slot
739                  * then, the SGM now contains slot 1
740                  *
741                  * Case (a) happens with very rapid "drum roll" gestures, where
742                  * slot 0 finger is lifted and a new slot 1 finger touches
743                  * within one reporting interval.
744                  *
745                  * Case (b) happens if initially two or more fingers tap
746                  * briefly, and all but one lift before the end of the first
747                  * reporting interval.
748                  *
749                  * (In both these cases, slot 0 will becomes empty, so SGM
750                  * contains slot 1 with the new finger)
751                  *
752                  * Else, if there was no previous SGM, it now contains slot 0.
753                  *
754                  * Otherwise, SGM still contains the same slot.
755                  */
756                 if (priv->mt_state_lost ||
757                     (priv->agm_pending && old->sgm <= 0))
758                         synaptics_mt_state_set(mt_state, 1, 1, -1);
759                 else if (old->sgm == -1)
760                         synaptics_mt_state_set(mt_state, 1, 0, -1);
761                 break;
762         case 2:
763                 /*
764                  * If mt_state_lost, we don't know which finger SGM contains.
765                  *
766                  * So, report 1 finger, but with both slots empty.
767                  * We will use slot 1 on subsequent 1->1
768                  */
769                 if (priv->mt_state_lost) {
770                         synaptics_mt_state_set(mt_state, 1, -1, -1);
771                         break;
772                 }
773                 /*
774                  * Since the last AGM was NOT (0,0,0), it was the finger in
775                  * slot 0 that has been removed.
776                  * So, SGM now contains previous AGM's slot, and AGM is now
777                  * empty.
778                  */
779                 synaptics_mt_state_set(mt_state, 1, old->agm, -1);
780                 break;
781         case 3:
782                 /*
783                  * Since last AGM was not (0,0,0), we don't know which finger
784                  * is left.
785                  *
786                  * So, report 1 finger, but with both slots empty.
787                  * We will use slot 1 on subsequent 1->1
788                  */
789                 synaptics_mt_state_set(mt_state, 1, -1, -1);
790                 priv->mt_state_lost = true;
791                 break;
792         case 4:
793         case 5:
794                 /* mt_state was updated by AGM-CONTACT packet */
795                 break;
796         }
797 }
798
799 /* Handle case where mt_state->count = 2 */
800 static void synaptics_image_sensor_2f(struct synaptics_data *priv,
801                                       struct synaptics_mt_state *mt_state)
802 {
803         struct synaptics_mt_state *old = &priv->mt_state;
804
805         switch (old->count) {
806         case 0:
807                 synaptics_mt_state_set(mt_state, 2, 0, 1);
808                 break;
809         case 1:
810                 /*
811                  * If previous SGM contained slot 1 or higher, SGM now contains
812                  * slot 0 (the newly touching finger) and AGM contains SGM's
813                  * previous slot.
814                  *
815                  * Otherwise, SGM still contains slot 0 and AGM now contains
816                  * slot 1.
817                  */
818                 if (old->sgm >= 1)
819                         synaptics_mt_state_set(mt_state, 2, 0, old->sgm);
820                 else
821                         synaptics_mt_state_set(mt_state, 2, 0, 1);
822                 break;
823         case 2:
824                 /*
825                  * If mt_state_lost, SGM now contains either finger 1 or 2, but
826                  * we don't know which.
827                  * So, we just assume that the SGM contains slot 0 and AGM 1.
828                  */
829                 if (priv->mt_state_lost)
830                         synaptics_mt_state_set(mt_state, 2, 0, 1);
831                 /*
832                  * Otherwise, use the same mt_state, since it either hasn't
833                  * changed, or was updated by a recently received AGM-CONTACT
834                  * packet.
835                  */
836                 break;
837         case 3:
838                 /*
839                  * 3->2 transitions have two unsolvable problems:
840                  *  1) no indication is given which finger was removed
841                  *  2) no way to tell if agm packet was for finger 3
842                  *     before 3->2, or finger 2 after 3->2.
843                  *
844                  * So, report 2 fingers, but empty all slots.
845                  * We will guess slots [0,1] on subsequent 2->2.
846                  */
847                 synaptics_mt_state_set(mt_state, 2, -1, -1);
848                 priv->mt_state_lost = true;
849                 break;
850         case 4:
851         case 5:
852                 /* mt_state was updated by AGM-CONTACT packet */
853                 break;
854         }
855 }
856
857 /* Handle case where mt_state->count = 3 */
858 static void synaptics_image_sensor_3f(struct synaptics_data *priv,
859                                       struct synaptics_mt_state *mt_state)
860 {
861         struct synaptics_mt_state *old = &priv->mt_state;
862
863         switch (old->count) {
864         case 0:
865                 synaptics_mt_state_set(mt_state, 3, 0, 2);
866                 break;
867         case 1:
868                 /*
869                  * If previous SGM contained slot 2 or higher, SGM now contains
870                  * slot 0 (one of the newly touching fingers) and AGM contains
871                  * SGM's previous slot.
872                  *
873                  * Otherwise, SGM now contains slot 0 and AGM contains slot 2.
874                  */
875                 if (old->sgm >= 2)
876                         synaptics_mt_state_set(mt_state, 3, 0, old->sgm);
877                 else
878                         synaptics_mt_state_set(mt_state, 3, 0, 2);
879                 break;
880         case 2:
881                 /*
882                  * If the AGM previously contained slot 3 or higher, then the
883                  * newly touching finger is in the lowest available slot.
884                  *
885                  * If SGM was previously 1 or higher, then the new SGM is
886                  * now slot 0 (with a new finger), otherwise, the new finger
887                  * is now in a hidden slot between 0 and AGM's slot.
888                  *
889                  * In all such cases, the SGM now contains slot 0, and the AGM
890                  * continues to contain the same slot as before.
891                  */
892                 if (old->agm >= 3) {
893                         synaptics_mt_state_set(mt_state, 3, 0, old->agm);
894                         break;
895                 }
896
897                 /*
898                  * After some 3->1 and all 3->2 transitions, we lose track
899                  * of which slot is reported by SGM and AGM.
900                  *
901                  * For 2->3 in this state, report 3 fingers, but empty all
902                  * slots, and we will guess (0,2) on a subsequent 0->3.
903                  *
904                  * To userspace, the resulting transition will look like:
905                  *    2:[0,1] -> 3:[-1,-1] -> 3:[0,2]
906                  */
907                 if (priv->mt_state_lost) {
908                         synaptics_mt_state_set(mt_state, 3, -1, -1);
909                         break;
910                 }
911
912                 /*
913                  * If the (SGM,AGM) really previously contained slots (0, 1),
914                  * then we cannot know what slot was just reported by the AGM,
915                  * because the 2->3 transition can occur either before or after
916                  * the AGM packet. Thus, this most recent AGM could contain
917                  * either the same old slot 1 or the new slot 2.
918                  * Subsequent AGMs will be reporting slot 2.
919                  *
920                  * To userspace, the resulting transition will look like:
921                  *    2:[0,1] -> 3:[0,-1] -> 3:[0,2]
922                  */
923                 synaptics_mt_state_set(mt_state, 3, 0, -1);
924                 break;
925         case 3:
926                 /*
927                  * If, for whatever reason, the previous agm was invalid,
928                  * Assume SGM now contains slot 0, AGM now contains slot 2.
929                  */
930                 if (old->agm <= 2)
931                         synaptics_mt_state_set(mt_state, 3, 0, 2);
932                 /*
933                  * mt_state either hasn't changed, or was updated by a recently
934                  * received AGM-CONTACT packet.
935                  */
936                 break;
937
938         case 4:
939         case 5:
940                 /* mt_state was updated by AGM-CONTACT packet */
941                 break;
942         }
943 }
944
945 /* Handle case where mt_state->count = 4, or = 5 */
946 static void synaptics_image_sensor_45f(struct synaptics_data *priv,
947                                        struct synaptics_mt_state *mt_state)
948 {
949         /* mt_state was updated correctly by AGM-CONTACT packet */
950         priv->mt_state_lost = false;
951 }
952
953 static void synaptics_image_sensor_process(struct psmouse *psmouse,
954                                            struct synaptics_hw_state *sgm)
955 {
956         struct synaptics_data *priv = psmouse->private;
957         struct synaptics_hw_state *agm = &priv->agm;
958         struct synaptics_mt_state mt_state;
959
960         /* Initialize using current mt_state (as updated by last agm) */
961         mt_state = agm->mt_state;
962
963         /*
964          * Update mt_state using the new finger count and current mt_state.
965          */
966         if (sgm->z == 0)
967                 synaptics_image_sensor_0f(priv, &mt_state);
968         else if (sgm->w >= 4)
969                 synaptics_image_sensor_1f(priv, &mt_state);
970         else if (sgm->w == 0)
971                 synaptics_image_sensor_2f(priv, &mt_state);
972         else if (sgm->w == 1 && mt_state.count <= 3)
973                 synaptics_image_sensor_3f(priv, &mt_state);
974         else
975                 synaptics_image_sensor_45f(priv, &mt_state);
976
977         /* Send resulting input events to user space */
978         synaptics_report_mt_data(psmouse, &mt_state, sgm);
979
980         /* Store updated mt_state */
981         priv->mt_state = agm->mt_state = mt_state;
982         priv->agm_pending = false;
983 }
984
985 /*
986  *  called for each full received packet from the touchpad
987  */
988 static void synaptics_process_packet(struct psmouse *psmouse)
989 {
990         struct input_dev *dev = psmouse->dev;
991         struct synaptics_data *priv = psmouse->private;
992         struct synaptics_hw_state hw;
993         int num_fingers;
994         int finger_width;
995
996         if (synaptics_parse_hw_state(psmouse->packet, priv, &hw))
997                 return;
998
999         if (SYN_CAP_IMAGE_SENSOR(priv->ext_cap_0c)) {
1000                 synaptics_image_sensor_process(psmouse, &hw);
1001                 return;
1002         }
1003
1004         if (hw.scroll) {
1005                 priv->scroll += hw.scroll;
1006
1007                 while (priv->scroll >= 4) {
1008                         input_report_key(dev, BTN_BACK, !hw.down);
1009                         input_sync(dev);
1010                         input_report_key(dev, BTN_BACK, hw.down);
1011                         input_sync(dev);
1012                         priv->scroll -= 4;
1013                 }
1014                 while (priv->scroll <= -4) {
1015                         input_report_key(dev, BTN_FORWARD, !hw.up);
1016                         input_sync(dev);
1017                         input_report_key(dev, BTN_FORWARD, hw.up);
1018                         input_sync(dev);
1019                         priv->scroll += 4;
1020                 }
1021                 return;
1022         }
1023
1024         if (hw.z > 0 && hw.x > 1) {
1025                 num_fingers = 1;
1026                 finger_width = 5;
1027                 if (SYN_CAP_EXTENDED(priv->capabilities)) {
1028                         switch (hw.w) {
1029                         case 0 ... 1:
1030                                 if (SYN_CAP_MULTIFINGER(priv->capabilities))
1031                                         num_fingers = hw.w + 2;
1032                                 break;
1033                         case 2:
1034                                 if (SYN_MODEL_PEN(priv->model_id))
1035                                         ;   /* Nothing, treat a pen as a single finger */
1036                                 break;
1037                         case 4 ... 15:
1038                                 if (SYN_CAP_PALMDETECT(priv->capabilities))
1039                                         finger_width = hw.w;
1040                                 break;
1041                         }
1042                 }
1043         } else {
1044                 num_fingers = 0;
1045                 finger_width = 0;
1046         }
1047
1048         if (SYN_CAP_ADV_GESTURE(priv->ext_cap_0c))
1049                 synaptics_report_semi_mt_data(dev, &hw, &priv->agm,
1050                                               num_fingers);
1051
1052         /* Post events
1053          * BTN_TOUCH has to be first as mousedev relies on it when doing
1054          * absolute -> relative conversion
1055          */
1056         if (hw.z > 30) input_report_key(dev, BTN_TOUCH, 1);
1057         if (hw.z < 25) input_report_key(dev, BTN_TOUCH, 0);
1058
1059         if (num_fingers > 0) {
1060                 input_report_abs(dev, ABS_X, hw.x);
1061                 input_report_abs(dev, ABS_Y, synaptics_invert_y(hw.y));
1062         }
1063         input_report_abs(dev, ABS_PRESSURE, hw.z);
1064
1065         if (SYN_CAP_PALMDETECT(priv->capabilities))
1066                 input_report_abs(dev, ABS_TOOL_WIDTH, finger_width);
1067
1068         input_report_key(dev, BTN_TOOL_FINGER, num_fingers == 1);
1069         if (SYN_CAP_MULTIFINGER(priv->capabilities)) {
1070                 input_report_key(dev, BTN_TOOL_DOUBLETAP, num_fingers == 2);
1071                 input_report_key(dev, BTN_TOOL_TRIPLETAP, num_fingers == 3);
1072         }
1073
1074         synaptics_report_buttons(psmouse, &hw);
1075
1076         input_sync(dev);
1077 }
1078
1079 static int synaptics_validate_byte(struct psmouse *psmouse,
1080                                    int idx, unsigned char pkt_type)
1081 {
1082         static const unsigned char newabs_mask[]        = { 0xC8, 0x00, 0x00, 0xC8, 0x00 };
1083         static const unsigned char newabs_rel_mask[]    = { 0xC0, 0x00, 0x00, 0xC0, 0x00 };
1084         static const unsigned char newabs_rslt[]        = { 0x80, 0x00, 0x00, 0xC0, 0x00 };
1085         static const unsigned char oldabs_mask[]        = { 0xC0, 0x60, 0x00, 0xC0, 0x60 };
1086         static const unsigned char oldabs_rslt[]        = { 0xC0, 0x00, 0x00, 0x80, 0x00 };
1087         const char *packet = psmouse->packet;
1088
1089         if (idx < 0 || idx > 4)
1090                 return 0;
1091
1092         switch (pkt_type) {
1093
1094         case SYN_NEWABS:
1095         case SYN_NEWABS_RELAXED:
1096                 return (packet[idx] & newabs_rel_mask[idx]) == newabs_rslt[idx];
1097
1098         case SYN_NEWABS_STRICT:
1099                 return (packet[idx] & newabs_mask[idx]) == newabs_rslt[idx];
1100
1101         case SYN_OLDABS:
1102                 return (packet[idx] & oldabs_mask[idx]) == oldabs_rslt[idx];
1103
1104         default:
1105                 psmouse_err(psmouse, "unknown packet type %d\n", pkt_type);
1106                 return 0;
1107         }
1108 }
1109
1110 static unsigned char synaptics_detect_pkt_type(struct psmouse *psmouse)
1111 {
1112         int i;
1113
1114         for (i = 0; i < 5; i++)
1115                 if (!synaptics_validate_byte(psmouse, i, SYN_NEWABS_STRICT)) {
1116                         psmouse_info(psmouse, "using relaxed packet validation\n");
1117                         return SYN_NEWABS_RELAXED;
1118                 }
1119
1120         return SYN_NEWABS_STRICT;
1121 }
1122
1123 static psmouse_ret_t synaptics_process_byte(struct psmouse *psmouse)
1124 {
1125         struct synaptics_data *priv = psmouse->private;
1126
1127         if (psmouse->pktcnt >= 6) { /* Full packet received */
1128                 if (unlikely(priv->pkt_type == SYN_NEWABS))
1129                         priv->pkt_type = synaptics_detect_pkt_type(psmouse);
1130
1131                 if (SYN_CAP_PASS_THROUGH(priv->capabilities) &&
1132                     synaptics_is_pt_packet(psmouse->packet)) {
1133                         if (priv->pt_port)
1134                                 synaptics_pass_pt_packet(priv->pt_port, psmouse->packet);
1135                 } else
1136                         synaptics_process_packet(psmouse);
1137
1138                 return PSMOUSE_FULL_PACKET;
1139         }
1140
1141         return synaptics_validate_byte(psmouse, psmouse->pktcnt - 1, priv->pkt_type) ?
1142                 PSMOUSE_GOOD_DATA : PSMOUSE_BAD_DATA;
1143 }
1144
1145 /*****************************************************************************
1146  *      Driver initialization/cleanup functions
1147  ****************************************************************************/
1148 static void set_abs_position_params(struct input_dev *dev,
1149                                     struct synaptics_data *priv, int x_code,
1150                                     int y_code)
1151 {
1152         int x_min = priv->x_min ?: XMIN_NOMINAL;
1153         int x_max = priv->x_max ?: XMAX_NOMINAL;
1154         int y_min = priv->y_min ?: YMIN_NOMINAL;
1155         int y_max = priv->y_max ?: YMAX_NOMINAL;
1156         int fuzz = SYN_CAP_REDUCED_FILTERING(priv->ext_cap_0c) ?
1157                         SYN_REDUCED_FILTER_FUZZ : 0;
1158
1159         input_set_abs_params(dev, x_code, x_min, x_max, fuzz, 0);
1160         input_set_abs_params(dev, y_code, y_min, y_max, fuzz, 0);
1161         input_abs_set_res(dev, x_code, priv->x_res);
1162         input_abs_set_res(dev, y_code, priv->y_res);
1163 }
1164
1165 static void set_input_params(struct input_dev *dev, struct synaptics_data *priv)
1166 {
1167         int i;
1168
1169         __set_bit(INPUT_PROP_POINTER, dev->propbit);
1170
1171         __set_bit(EV_ABS, dev->evbit);
1172         set_abs_position_params(dev, priv, ABS_X, ABS_Y);
1173         input_set_abs_params(dev, ABS_PRESSURE, 0, 255, 0, 0);
1174
1175         if (SYN_CAP_IMAGE_SENSOR(priv->ext_cap_0c)) {
1176                 input_mt_init_slots(dev, 2);
1177                 set_abs_position_params(dev, priv, ABS_MT_POSITION_X,
1178                                         ABS_MT_POSITION_Y);
1179                 /* Image sensors can report per-contact pressure */
1180                 input_set_abs_params(dev, ABS_MT_PRESSURE, 0, 255, 0, 0);
1181
1182                 /* Image sensors can signal 4 and 5 finger clicks */
1183                 __set_bit(BTN_TOOL_QUADTAP, dev->keybit);
1184                 __set_bit(BTN_TOOL_QUINTTAP, dev->keybit);
1185         } else if (SYN_CAP_ADV_GESTURE(priv->ext_cap_0c)) {
1186                 /* Non-image sensors with AGM use semi-mt */
1187                 __set_bit(INPUT_PROP_SEMI_MT, dev->propbit);
1188                 input_mt_init_slots(dev, 2);
1189                 set_abs_position_params(dev, priv, ABS_MT_POSITION_X,
1190                                         ABS_MT_POSITION_Y);
1191         }
1192
1193         if (SYN_CAP_PALMDETECT(priv->capabilities))
1194                 input_set_abs_params(dev, ABS_TOOL_WIDTH, 0, 15, 0, 0);
1195
1196         __set_bit(EV_KEY, dev->evbit);
1197         __set_bit(BTN_TOUCH, dev->keybit);
1198         __set_bit(BTN_TOOL_FINGER, dev->keybit);
1199         __set_bit(BTN_LEFT, dev->keybit);
1200         __set_bit(BTN_RIGHT, dev->keybit);
1201
1202         if (SYN_CAP_MULTIFINGER(priv->capabilities)) {
1203                 __set_bit(BTN_TOOL_DOUBLETAP, dev->keybit);
1204                 __set_bit(BTN_TOOL_TRIPLETAP, dev->keybit);
1205         }
1206
1207         if (SYN_CAP_MIDDLE_BUTTON(priv->capabilities))
1208                 __set_bit(BTN_MIDDLE, dev->keybit);
1209
1210         if (SYN_CAP_FOUR_BUTTON(priv->capabilities) ||
1211             SYN_CAP_MIDDLE_BUTTON(priv->capabilities)) {
1212                 __set_bit(BTN_FORWARD, dev->keybit);
1213                 __set_bit(BTN_BACK, dev->keybit);
1214         }
1215
1216         for (i = 0; i < SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap); i++)
1217                 __set_bit(BTN_0 + i, dev->keybit);
1218
1219         __clear_bit(EV_REL, dev->evbit);
1220         __clear_bit(REL_X, dev->relbit);
1221         __clear_bit(REL_Y, dev->relbit);
1222
1223         if (SYN_CAP_CLICKPAD(priv->ext_cap_0c)) {
1224                 __set_bit(INPUT_PROP_BUTTONPAD, dev->propbit);
1225                 /* Clickpads report only left button */
1226                 __clear_bit(BTN_RIGHT, dev->keybit);
1227                 __clear_bit(BTN_MIDDLE, dev->keybit);
1228         }
1229 }
1230
1231 static void synaptics_disconnect(struct psmouse *psmouse)
1232 {
1233         synaptics_reset(psmouse);
1234         kfree(psmouse->private);
1235         psmouse->private = NULL;
1236 }
1237
1238 static int synaptics_reconnect(struct psmouse *psmouse)
1239 {
1240         struct synaptics_data *priv = psmouse->private;
1241         struct synaptics_data old_priv = *priv;
1242         int retry = 0;
1243         int error;
1244
1245         do {
1246                 psmouse_reset(psmouse);
1247                 if (retry) {
1248                         /*
1249                          * On some boxes, right after resuming, the touchpad
1250                          * needs some time to finish initializing (I assume
1251                          * it needs time to calibrate) and start responding
1252                          * to Synaptics-specific queries, so let's wait a
1253                          * bit.
1254                          */
1255                         ssleep(1);
1256                 }
1257                 error = synaptics_detect(psmouse, 0);
1258         } while (error && ++retry < 3);
1259
1260         if (error)
1261                 return -1;
1262
1263         if (retry > 1)
1264                 psmouse_dbg(psmouse, "reconnected after %d tries\n", retry);
1265
1266         if (synaptics_query_hardware(psmouse)) {
1267                 psmouse_err(psmouse, "Unable to query device.\n");
1268                 return -1;
1269         }
1270
1271         if (synaptics_set_absolute_mode(psmouse)) {
1272                 psmouse_err(psmouse, "Unable to initialize device.\n");
1273                 return -1;
1274         }
1275
1276         if (synaptics_set_advanced_gesture_mode(psmouse)) {
1277                 psmouse_err(psmouse,
1278                             "Advanced gesture mode reconnect failed.\n");
1279                 return -1;
1280         }
1281
1282         if (old_priv.identity != priv->identity ||
1283             old_priv.model_id != priv->model_id ||
1284             old_priv.capabilities != priv->capabilities ||
1285             old_priv.ext_cap != priv->ext_cap) {
1286                 psmouse_err(psmouse,
1287                             "hardware appears to be different: id(%ld-%ld), model(%ld-%ld), caps(%lx-%lx), ext(%lx-%lx).\n",
1288                             old_priv.identity, priv->identity,
1289                             old_priv.model_id, priv->model_id,
1290                             old_priv.capabilities, priv->capabilities,
1291                             old_priv.ext_cap, priv->ext_cap);
1292                 return -1;
1293         }
1294
1295         return 0;
1296 }
1297
1298 static bool impaired_toshiba_kbc;
1299
1300 static const struct dmi_system_id __initconst toshiba_dmi_table[] = {
1301 #if defined(CONFIG_DMI) && defined(CONFIG_X86)
1302         {
1303                 /* Toshiba Satellite */
1304                 .matches = {
1305                         DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1306                         DMI_MATCH(DMI_PRODUCT_NAME, "Satellite"),
1307                 },
1308         },
1309         {
1310                 /* Toshiba Dynabook */
1311                 .matches = {
1312                         DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1313                         DMI_MATCH(DMI_PRODUCT_NAME, "dynabook"),
1314                 },
1315         },
1316         {
1317                 /* Toshiba Portege M300 */
1318                 .matches = {
1319                         DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1320                         DMI_MATCH(DMI_PRODUCT_NAME, "PORTEGE M300"),
1321                 },
1322
1323         },
1324         {
1325                 /* Toshiba Portege M300 */
1326                 .matches = {
1327                         DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1328                         DMI_MATCH(DMI_PRODUCT_NAME, "Portable PC"),
1329                         DMI_MATCH(DMI_PRODUCT_VERSION, "Version 1.0"),
1330                 },
1331
1332         },
1333 #endif
1334         { }
1335 };
1336
1337 static bool broken_olpc_ec;
1338
1339 static const struct dmi_system_id __initconst olpc_dmi_table[] = {
1340 #if defined(CONFIG_DMI) && defined(CONFIG_OLPC)
1341         {
1342                 /* OLPC XO-1 or XO-1.5 */
1343                 .matches = {
1344                         DMI_MATCH(DMI_SYS_VENDOR, "OLPC"),
1345                         DMI_MATCH(DMI_PRODUCT_NAME, "XO"),
1346                 },
1347         },
1348 #endif
1349         { }
1350 };
1351
1352 void __init synaptics_module_init(void)
1353 {
1354         impaired_toshiba_kbc = dmi_check_system(toshiba_dmi_table);
1355         broken_olpc_ec = dmi_check_system(olpc_dmi_table);
1356 }
1357
1358 int synaptics_init(struct psmouse *psmouse)
1359 {
1360         struct synaptics_data *priv;
1361
1362         /*
1363          * The OLPC XO has issues with Synaptics' absolute mode; similarly to
1364          * the HGPK, it quickly degrades and the hardware becomes jumpy and
1365          * overly sensitive.  Not only that, but the constant packet spew
1366          * (even at a lowered 40pps rate) overloads the EC such that key
1367          * presses on the keyboard are missed.  Given all of that, don't
1368          * even attempt to use Synaptics mode.  Relative mode seems to work
1369          * just fine.
1370          */
1371         if (broken_olpc_ec) {
1372                 psmouse_info(psmouse,
1373                              "OLPC XO detected, not enabling Synaptics protocol.\n");
1374                 return -ENODEV;
1375         }
1376
1377         psmouse->private = priv = kzalloc(sizeof(struct synaptics_data), GFP_KERNEL);
1378         if (!priv)
1379                 return -ENOMEM;
1380
1381         psmouse_reset(psmouse);
1382
1383         if (synaptics_query_hardware(psmouse)) {
1384                 psmouse_err(psmouse, "Unable to query device.\n");
1385                 goto init_fail;
1386         }
1387
1388         if (synaptics_set_absolute_mode(psmouse)) {
1389                 psmouse_err(psmouse, "Unable to initialize device.\n");
1390                 goto init_fail;
1391         }
1392
1393         if (synaptics_set_advanced_gesture_mode(psmouse)) {
1394                 psmouse_err(psmouse, "Advanced gesture mode init failed.\n");
1395                 goto init_fail;
1396         }
1397
1398         priv->pkt_type = SYN_MODEL_NEWABS(priv->model_id) ? SYN_NEWABS : SYN_OLDABS;
1399
1400         psmouse_info(psmouse,
1401                      "Touchpad model: %ld, fw: %ld.%ld, id: %#lx, caps: %#lx/%#lx/%#lx\n",
1402                      SYN_ID_MODEL(priv->identity),
1403                      SYN_ID_MAJOR(priv->identity), SYN_ID_MINOR(priv->identity),
1404                      priv->model_id,
1405                      priv->capabilities, priv->ext_cap, priv->ext_cap_0c);
1406
1407         set_input_params(psmouse->dev, priv);
1408
1409         /*
1410          * Encode touchpad model so that it can be used to set
1411          * input device->id.version and be visible to userspace.
1412          * Because version is __u16 we have to drop something.
1413          * Hardware info bits seem to be good candidates as they
1414          * are documented to be for Synaptics corp. internal use.
1415          */
1416         psmouse->model = ((priv->model_id & 0x00ff0000) >> 8) |
1417                           (priv->model_id & 0x000000ff);
1418
1419         psmouse->protocol_handler = synaptics_process_byte;
1420         psmouse->set_rate = synaptics_set_rate;
1421         psmouse->disconnect = synaptics_disconnect;
1422         psmouse->reconnect = synaptics_reconnect;
1423         psmouse->cleanup = synaptics_reset;
1424         psmouse->pktsize = 6;
1425         /* Synaptics can usually stay in sync without extra help */
1426         psmouse->resync_time = 0;
1427
1428         if (SYN_CAP_PASS_THROUGH(priv->capabilities))
1429                 synaptics_pt_create(psmouse);
1430
1431         /*
1432          * Toshiba's KBC seems to have trouble handling data from
1433          * Synaptics at full rate.  Switch to a lower rate (roughly
1434          * the same rate as a standard PS/2 mouse).
1435          */
1436         if (psmouse->rate >= 80 && impaired_toshiba_kbc) {
1437                 psmouse_info(psmouse,
1438                              "Toshiba %s detected, limiting rate to 40pps.\n",
1439                              dmi_get_system_info(DMI_PRODUCT_NAME));
1440                 psmouse->rate = 40;
1441         }
1442
1443         return 0;
1444
1445  init_fail:
1446         kfree(priv);
1447         return -1;
1448 }
1449
1450 bool synaptics_supported(void)
1451 {
1452         return true;
1453 }
1454
1455 #else /* CONFIG_MOUSE_PS2_SYNAPTICS */
1456
1457 void __init synaptics_module_init(void)
1458 {
1459 }
1460
1461 int synaptics_init(struct psmouse *psmouse)
1462 {
1463         return -ENOSYS;
1464 }
1465
1466 bool synaptics_supported(void)
1467 {
1468         return false;
1469 }
1470
1471 #endif /* CONFIG_MOUSE_PS2_SYNAPTICS */