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