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