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