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