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