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