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