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