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