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