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