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