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