treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / input / mouse / synaptics.c
blob1ae6f8bba9ae15acc09f175ffb682a38a1ba19ba
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Synaptics TouchPad PS/2 mouse driver
5 * 2003 Dmitry Torokhov <dtor@mail.ru>
6 * Added support for pass-through port. Special thanks to Peter Berg Larsen
7 * for explaining various Synaptics quirks.
9 * 2003 Peter Osterlund <petero2@telia.com>
10 * Ported to 2.5 input device infrastructure.
12 * Copyright (C) 2001 Stefan Gmeiner <riddlebox@freesurf.ch>
13 * start merging tpconfig and gpm code to a xfree-input module
14 * adding some changes and extensions (ex. 3rd and 4th button)
16 * Copyright (c) 1997 C. Scott Ananian <cananian@alumni.priceton.edu>
17 * Copyright (c) 1998-2000 Bruce Kalk <kall@compass.com>
18 * code for the special synaptics commands (from the tpconfig-source)
20 * Trademarks are the property of their respective owners.
23 #include <linux/module.h>
24 #include <linux/delay.h>
25 #include <linux/dmi.h>
26 #include <linux/input/mt.h>
27 #include <linux/serio.h>
28 #include <linux/libps2.h>
29 #include <linux/rmi.h>
30 #include <linux/i2c.h>
31 #include <linux/slab.h>
32 #include "psmouse.h"
33 #include "synaptics.h"
36 * The x/y limits are taken from the Synaptics TouchPad interfacing Guide,
37 * section 2.3.2, which says that they should be valid regardless of the
38 * actual size of the sensor.
39 * Note that newer firmware allows querying device for maximum useable
40 * coordinates.
42 #define XMIN 0
43 #define XMAX 6143
44 #define YMIN 0
45 #define YMAX 6143
46 #define XMIN_NOMINAL 1472
47 #define XMAX_NOMINAL 5472
48 #define YMIN_NOMINAL 1408
49 #define YMAX_NOMINAL 4448
51 /* Size in bits of absolute position values reported by the hardware */
52 #define ABS_POS_BITS 13
55 * These values should represent the absolute maximum value that will
56 * be reported for a positive position value. Some Synaptics firmware
57 * uses this value to indicate a finger near the edge of the touchpad
58 * whose precise position cannot be determined.
60 * At least one touchpad is known to report positions in excess of this
61 * value which are actually negative values truncated to the 13-bit
62 * reporting range. These values have never been observed to be lower
63 * than 8184 (i.e. -8), so we treat all values greater than 8176 as
64 * negative and any other value as positive.
66 #define X_MAX_POSITIVE 8176
67 #define Y_MAX_POSITIVE 8176
69 /* maximum ABS_MT_POSITION displacement (in mm) */
70 #define DMAX 10
72 /*****************************************************************************
73 * Stuff we need even when we do not want native Synaptics support
74 ****************************************************************************/
77 * Set the synaptics touchpad mode byte by special commands
79 static int synaptics_mode_cmd(struct psmouse *psmouse, u8 mode)
81 u8 param[1];
82 int error;
84 error = ps2_sliced_command(&psmouse->ps2dev, mode);
85 if (error)
86 return error;
88 param[0] = SYN_PS_SET_MODE2;
89 error = ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_SETRATE);
90 if (error)
91 return error;
93 return 0;
96 int synaptics_detect(struct psmouse *psmouse, bool set_properties)
98 struct ps2dev *ps2dev = &psmouse->ps2dev;
99 u8 param[4] = { 0 };
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);
107 if (param[1] != 0x47)
108 return -ENODEV;
110 if (set_properties) {
111 psmouse->vendor = "Synaptics";
112 psmouse->name = "TouchPad";
115 return 0;
118 void synaptics_reset(struct psmouse *psmouse)
120 /* reset touchpad back to relative mode, gestures enabled */
121 synaptics_mode_cmd(psmouse, 0);
124 #if defined(CONFIG_MOUSE_PS2_SYNAPTICS) || \
125 defined(CONFIG_MOUSE_PS2_SYNAPTICS_SMBUS)
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
165 static const char * const smbus_pnp_ids[] = {
166 /* all of the topbuttonpad_pnp_ids are valid, we just add some extras */
167 "LEN0048", /* X1 Carbon 3 */
168 "LEN0046", /* X250 */
169 "LEN004a", /* W541 */
170 "LEN005b", /* P50 */
171 "LEN005e", /* T560 */
172 "LEN0071", /* T480 */
173 "LEN0072", /* X1 Carbon Gen 5 (2017) - Elan/ALPS trackpoint */
174 "LEN0073", /* X1 Carbon G5 (Elantech) */
175 "LEN0091", /* X1 Carbon 6 */
176 "LEN0092", /* X1 Carbon 6 */
177 "LEN0093", /* T480 */
178 "LEN0096", /* X280 */
179 "LEN0097", /* X280 -> ALPS trackpoint */
180 "LEN009b", /* T580 */
181 "LEN200f", /* T450s */
182 "LEN2054", /* E480 */
183 "LEN2055", /* E580 */
184 "SYN3052", /* HP EliteBook 840 G4 */
185 "SYN3221", /* HP 15-ay000 */
186 "SYN323d", /* HP Spectre X360 13-w013dx */
187 NULL
190 static const char * const forcepad_pnp_ids[] = {
191 "SYN300D",
192 "SYN3014",
193 NULL
197 * Send a command to the synaptics touchpad by special commands
199 static int synaptics_send_cmd(struct psmouse *psmouse, u8 cmd, u8 *param)
201 int error;
203 error = ps2_sliced_command(&psmouse->ps2dev, cmd);
204 if (error)
205 return error;
207 error = ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_GETINFO);
208 if (error)
209 return error;
211 return 0;
214 static int synaptics_query_int(struct psmouse *psmouse, u8 query_cmd, u32 *val)
216 int error;
217 union {
218 __be32 be_val;
219 char buf[4];
220 } resp = { 0 };
222 error = synaptics_send_cmd(psmouse, query_cmd, resp.buf + 1);
223 if (error)
224 return error;
226 *val = be32_to_cpu(resp.be_val);
227 return 0;
231 * Identify Touchpad
232 * See also the SYN_ID_* macros
234 static int synaptics_identify(struct psmouse *psmouse,
235 struct synaptics_device_info *info)
237 int error;
239 error = synaptics_query_int(psmouse, SYN_QUE_IDENTIFY, &info->identity);
240 if (error)
241 return error;
243 return SYN_ID_IS_SYNAPTICS(info->identity) ? 0 : -ENXIO;
247 * Read the model-id bytes from the touchpad
248 * see also SYN_MODEL_* macros
250 static int synaptics_model_id(struct psmouse *psmouse,
251 struct synaptics_device_info *info)
253 return synaptics_query_int(psmouse, SYN_QUE_MODEL, &info->model_id);
257 * Read the firmware id from the touchpad
259 static int synaptics_firmware_id(struct psmouse *psmouse,
260 struct synaptics_device_info *info)
262 return synaptics_query_int(psmouse, SYN_QUE_FIRMWARE_ID,
263 &info->firmware_id);
267 * Read the board id and the "More Extended Queries" from the touchpad
268 * The board id is encoded in the "QUERY MODES" response
270 static int synaptics_query_modes(struct psmouse *psmouse,
271 struct synaptics_device_info *info)
273 u8 bid[3];
274 int error;
276 /* firmwares prior 7.5 have no board_id encoded */
277 if (SYN_ID_FULL(info->identity) < 0x705)
278 return 0;
280 error = synaptics_send_cmd(psmouse, SYN_QUE_MODES, bid);
281 if (error)
282 return error;
284 info->board_id = ((bid[0] & 0xfc) << 6) | bid[1];
286 if (SYN_MEXT_CAP_BIT(bid[0]))
287 return synaptics_query_int(psmouse, SYN_QUE_MEXT_CAPAB_10,
288 &info->ext_cap_10);
290 return 0;
294 * Read the capability-bits from the touchpad
295 * see also the SYN_CAP_* macros
297 static int synaptics_capability(struct psmouse *psmouse,
298 struct synaptics_device_info *info)
300 int error;
302 error = synaptics_query_int(psmouse, SYN_QUE_CAPABILITIES,
303 &info->capabilities);
304 if (error)
305 return error;
307 info->ext_cap = info->ext_cap_0c = 0;
310 * Older firmwares had submodel ID fixed to 0x47
312 if (SYN_ID_FULL(info->identity) < 0x705 &&
313 SYN_CAP_SUBMODEL_ID(info->capabilities) != 0x47) {
314 return -ENXIO;
318 * Unless capExtended is set the rest of the flags should be ignored
320 if (!SYN_CAP_EXTENDED(info->capabilities))
321 info->capabilities = 0;
323 if (SYN_EXT_CAP_REQUESTS(info->capabilities) >= 1) {
324 error = synaptics_query_int(psmouse, SYN_QUE_EXT_CAPAB,
325 &info->ext_cap);
326 if (error) {
327 psmouse_warn(psmouse,
328 "device claims to have extended capabilities, but I'm not able to read them.\n");
329 } else {
331 * if nExtBtn is greater than 8 it should be considered
332 * invalid and treated as 0
334 if (SYN_CAP_MULTI_BUTTON_NO(info->ext_cap) > 8)
335 info->ext_cap &= ~SYN_CAP_MB_MASK;
339 if (SYN_EXT_CAP_REQUESTS(info->capabilities) >= 4) {
340 error = synaptics_query_int(psmouse, SYN_QUE_EXT_CAPAB_0C,
341 &info->ext_cap_0c);
342 if (error)
343 psmouse_warn(psmouse,
344 "device claims to have extended capability 0x0c, but I'm not able to read it.\n");
347 return 0;
351 * Read touchpad resolution and maximum reported coordinates
352 * Resolution is left zero if touchpad does not support the query
354 static int synaptics_resolution(struct psmouse *psmouse,
355 struct synaptics_device_info *info)
357 u8 resp[3];
358 int error;
360 if (SYN_ID_MAJOR(info->identity) < 4)
361 return 0;
363 error = synaptics_send_cmd(psmouse, SYN_QUE_RESOLUTION, resp);
364 if (!error) {
365 if (resp[0] != 0 && (resp[1] & 0x80) && resp[2] != 0) {
366 info->x_res = resp[0]; /* x resolution in units/mm */
367 info->y_res = resp[2]; /* y resolution in units/mm */
371 if (SYN_EXT_CAP_REQUESTS(info->capabilities) >= 5 &&
372 SYN_CAP_MAX_DIMENSIONS(info->ext_cap_0c)) {
373 error = synaptics_send_cmd(psmouse,
374 SYN_QUE_EXT_MAX_COORDS, resp);
375 if (error) {
376 psmouse_warn(psmouse,
377 "device claims to have max coordinates query, but I'm not able to read it.\n");
378 } else {
379 info->x_max = (resp[0] << 5) | ((resp[1] & 0x0f) << 1);
380 info->y_max = (resp[2] << 5) | ((resp[1] & 0xf0) >> 3);
381 psmouse_info(psmouse,
382 "queried max coordinates: x [..%d], y [..%d]\n",
383 info->x_max, info->y_max);
387 if (SYN_CAP_MIN_DIMENSIONS(info->ext_cap_0c) &&
388 (SYN_EXT_CAP_REQUESTS(info->capabilities) >= 7 ||
390 * Firmware v8.1 does not report proper number of extended
391 * capabilities, but has been proven to report correct min
392 * coordinates.
394 SYN_ID_FULL(info->identity) == 0x801)) {
395 error = synaptics_send_cmd(psmouse,
396 SYN_QUE_EXT_MIN_COORDS, resp);
397 if (error) {
398 psmouse_warn(psmouse,
399 "device claims to have min coordinates query, but I'm not able to read it.\n");
400 } else {
401 info->x_min = (resp[0] << 5) | ((resp[1] & 0x0f) << 1);
402 info->y_min = (resp[2] << 5) | ((resp[1] & 0xf0) >> 3);
403 psmouse_info(psmouse,
404 "queried min coordinates: x [%d..], y [%d..]\n",
405 info->x_min, info->y_min);
409 return 0;
412 static int synaptics_query_hardware(struct psmouse *psmouse,
413 struct synaptics_device_info *info)
415 int error;
417 memset(info, 0, sizeof(*info));
419 error = synaptics_identify(psmouse, info);
420 if (error)
421 return error;
423 error = synaptics_model_id(psmouse, info);
424 if (error)
425 return error;
427 error = synaptics_firmware_id(psmouse, info);
428 if (error)
429 return error;
431 error = synaptics_query_modes(psmouse, info);
432 if (error)
433 return error;
435 error = synaptics_capability(psmouse, info);
436 if (error)
437 return error;
439 error = synaptics_resolution(psmouse, info);
440 if (error)
441 return error;
443 return 0;
446 #endif /* CONFIG_MOUSE_PS2_SYNAPTICS || CONFIG_MOUSE_PS2_SYNAPTICS_SMBUS */
448 #ifdef CONFIG_MOUSE_PS2_SYNAPTICS
450 static bool cr48_profile_sensor;
452 #define ANY_BOARD_ID 0
453 struct min_max_quirk {
454 const char * const *pnp_ids;
455 struct {
456 u32 min, max;
457 } board_id;
458 u32 x_min, x_max, y_min, y_max;
461 static const struct min_max_quirk min_max_pnpid_table[] = {
463 (const char * const []){"LEN0033", NULL},
464 {ANY_BOARD_ID, ANY_BOARD_ID},
465 1024, 5052, 2258, 4832
468 (const char * const []){"LEN0042", NULL},
469 {ANY_BOARD_ID, ANY_BOARD_ID},
470 1232, 5710, 1156, 4696
473 (const char * const []){"LEN0034", "LEN0036", "LEN0037",
474 "LEN0039", "LEN2002", "LEN2004",
475 NULL},
476 {ANY_BOARD_ID, 2961},
477 1024, 5112, 2024, 4832
480 (const char * const []){"LEN2000", NULL},
481 {ANY_BOARD_ID, ANY_BOARD_ID},
482 1024, 5113, 2021, 4832
485 (const char * const []){"LEN2001", NULL},
486 {ANY_BOARD_ID, ANY_BOARD_ID},
487 1024, 5022, 2508, 4832
490 (const char * const []){"LEN2006", NULL},
491 {2691, 2691},
492 1024, 5045, 2457, 4832
495 (const char * const []){"LEN2006", NULL},
496 {ANY_BOARD_ID, ANY_BOARD_ID},
497 1264, 5675, 1171, 4688
502 /*****************************************************************************
503 * Synaptics communications functions
504 ****************************************************************************/
507 * Synaptics touchpads report the y coordinate from bottom to top, which is
508 * opposite from what userspace expects.
509 * This function is used to invert y before reporting.
511 static int synaptics_invert_y(int y)
513 return YMAX_NOMINAL + YMIN_NOMINAL - y;
517 * Apply quirk(s) if the hardware matches
519 static void synaptics_apply_quirks(struct psmouse *psmouse,
520 struct synaptics_device_info *info)
522 int i;
524 for (i = 0; min_max_pnpid_table[i].pnp_ids; i++) {
525 if (!psmouse_matches_pnp_id(psmouse,
526 min_max_pnpid_table[i].pnp_ids))
527 continue;
529 if (min_max_pnpid_table[i].board_id.min != ANY_BOARD_ID &&
530 info->board_id < min_max_pnpid_table[i].board_id.min)
531 continue;
533 if (min_max_pnpid_table[i].board_id.max != ANY_BOARD_ID &&
534 info->board_id > min_max_pnpid_table[i].board_id.max)
535 continue;
537 info->x_min = min_max_pnpid_table[i].x_min;
538 info->x_max = min_max_pnpid_table[i].x_max;
539 info->y_min = min_max_pnpid_table[i].y_min;
540 info->y_max = min_max_pnpid_table[i].y_max;
541 psmouse_info(psmouse,
542 "quirked min/max coordinates: x [%d..%d], y [%d..%d]\n",
543 info->x_min, info->x_max,
544 info->y_min, info->y_max);
545 break;
549 static bool synaptics_has_agm(struct synaptics_data *priv)
551 return (SYN_CAP_ADV_GESTURE(priv->info.ext_cap_0c) ||
552 SYN_CAP_IMAGE_SENSOR(priv->info.ext_cap_0c));
555 static int synaptics_set_advanced_gesture_mode(struct psmouse *psmouse)
557 static u8 param = 0xc8;
558 int error;
560 error = ps2_sliced_command(&psmouse->ps2dev, SYN_QUE_MODEL);
561 if (error)
562 return error;
564 error = ps2_command(&psmouse->ps2dev, &param, PSMOUSE_CMD_SETRATE);
565 if (error)
566 return error;
568 return 0;
571 static int synaptics_set_mode(struct psmouse *psmouse)
573 struct synaptics_data *priv = psmouse->private;
574 int error;
576 priv->mode = 0;
577 if (priv->absolute_mode)
578 priv->mode |= SYN_BIT_ABSOLUTE_MODE;
579 if (priv->disable_gesture)
580 priv->mode |= SYN_BIT_DISABLE_GESTURE;
581 if (psmouse->rate >= 80)
582 priv->mode |= SYN_BIT_HIGH_RATE;
583 if (SYN_CAP_EXTENDED(priv->info.capabilities))
584 priv->mode |= SYN_BIT_W_MODE;
586 error = synaptics_mode_cmd(psmouse, priv->mode);
587 if (error)
588 return error;
590 if (priv->absolute_mode && synaptics_has_agm(priv)) {
591 error = synaptics_set_advanced_gesture_mode(psmouse);
592 if (error) {
593 psmouse_err(psmouse,
594 "Advanced gesture mode init failed: %d\n",
595 error);
596 return error;
600 return 0;
603 static void synaptics_set_rate(struct psmouse *psmouse, unsigned int rate)
605 struct synaptics_data *priv = psmouse->private;
607 if (rate >= 80) {
608 priv->mode |= SYN_BIT_HIGH_RATE;
609 psmouse->rate = 80;
610 } else {
611 priv->mode &= ~SYN_BIT_HIGH_RATE;
612 psmouse->rate = 40;
615 synaptics_mode_cmd(psmouse, priv->mode);
618 /*****************************************************************************
619 * Synaptics pass-through PS/2 port support
620 ****************************************************************************/
621 static int synaptics_pt_write(struct serio *serio, u8 c)
623 struct psmouse *parent = serio_get_drvdata(serio->parent);
624 u8 rate_param = SYN_PS_CLIENT_CMD; /* indicates that we want pass-through port */
625 int error;
627 error = ps2_sliced_command(&parent->ps2dev, c);
628 if (error)
629 return error;
631 error = ps2_command(&parent->ps2dev, &rate_param, PSMOUSE_CMD_SETRATE);
632 if (error)
633 return error;
635 return 0;
638 static int synaptics_pt_start(struct serio *serio)
640 struct psmouse *parent = serio_get_drvdata(serio->parent);
641 struct synaptics_data *priv = parent->private;
643 serio_pause_rx(parent->ps2dev.serio);
644 priv->pt_port = serio;
645 serio_continue_rx(parent->ps2dev.serio);
647 return 0;
650 static void synaptics_pt_stop(struct serio *serio)
652 struct psmouse *parent = serio_get_drvdata(serio->parent);
653 struct synaptics_data *priv = parent->private;
655 serio_pause_rx(parent->ps2dev.serio);
656 priv->pt_port = NULL;
657 serio_continue_rx(parent->ps2dev.serio);
660 static int synaptics_is_pt_packet(u8 *buf)
662 return (buf[0] & 0xFC) == 0x84 && (buf[3] & 0xCC) == 0xC4;
665 static void synaptics_pass_pt_packet(struct serio *ptport, u8 *packet)
667 struct psmouse *child = serio_get_drvdata(ptport);
669 if (child && child->state == PSMOUSE_ACTIVATED) {
670 serio_interrupt(ptport, packet[1], 0);
671 serio_interrupt(ptport, packet[4], 0);
672 serio_interrupt(ptport, packet[5], 0);
673 if (child->pktsize == 4)
674 serio_interrupt(ptport, packet[2], 0);
675 } else {
676 serio_interrupt(ptport, packet[1], 0);
680 static void synaptics_pt_activate(struct psmouse *psmouse)
682 struct synaptics_data *priv = psmouse->private;
683 struct psmouse *child = serio_get_drvdata(priv->pt_port);
685 /* adjust the touchpad to child's choice of protocol */
686 if (child) {
687 if (child->pktsize == 4)
688 priv->mode |= SYN_BIT_FOUR_BYTE_CLIENT;
689 else
690 priv->mode &= ~SYN_BIT_FOUR_BYTE_CLIENT;
692 if (synaptics_mode_cmd(psmouse, priv->mode))
693 psmouse_warn(psmouse,
694 "failed to switch guest protocol\n");
698 static void synaptics_pt_create(struct psmouse *psmouse)
700 struct serio *serio;
702 serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
703 if (!serio) {
704 psmouse_err(psmouse,
705 "not enough memory for pass-through port\n");
706 return;
709 serio->id.type = SERIO_PS_PSTHRU;
710 strlcpy(serio->name, "Synaptics pass-through", sizeof(serio->name));
711 strlcpy(serio->phys, "synaptics-pt/serio0", sizeof(serio->phys));
712 serio->write = synaptics_pt_write;
713 serio->start = synaptics_pt_start;
714 serio->stop = synaptics_pt_stop;
715 serio->parent = psmouse->ps2dev.serio;
717 psmouse->pt_activate = synaptics_pt_activate;
719 psmouse_info(psmouse, "serio: %s port at %s\n",
720 serio->name, psmouse->phys);
721 serio_register_port(serio);
724 /*****************************************************************************
725 * Functions to interpret the absolute mode packets
726 ****************************************************************************/
728 static void synaptics_parse_agm(const u8 buf[],
729 struct synaptics_data *priv,
730 struct synaptics_hw_state *hw)
732 struct synaptics_hw_state *agm = &priv->agm;
733 int agm_packet_type;
735 agm_packet_type = (buf[5] & 0x30) >> 4;
736 switch (agm_packet_type) {
737 case 1:
738 /* Gesture packet: (x, y, z) half resolution */
739 agm->w = hw->w;
740 agm->x = (((buf[4] & 0x0f) << 8) | buf[1]) << 1;
741 agm->y = (((buf[4] & 0xf0) << 4) | buf[2]) << 1;
742 agm->z = ((buf[3] & 0x30) | (buf[5] & 0x0f)) << 1;
743 break;
745 case 2:
746 /* AGM-CONTACT packet: we are only interested in the count */
747 priv->agm_count = buf[1];
748 break;
750 default:
751 break;
755 static void synaptics_parse_ext_buttons(const u8 buf[],
756 struct synaptics_data *priv,
757 struct synaptics_hw_state *hw)
759 unsigned int ext_bits =
760 (SYN_CAP_MULTI_BUTTON_NO(priv->info.ext_cap) + 1) >> 1;
761 unsigned int ext_mask = GENMASK(ext_bits - 1, 0);
763 hw->ext_buttons = buf[4] & ext_mask;
764 hw->ext_buttons |= (buf[5] & ext_mask) << ext_bits;
767 static int synaptics_parse_hw_state(const u8 buf[],
768 struct synaptics_data *priv,
769 struct synaptics_hw_state *hw)
771 memset(hw, 0, sizeof(struct synaptics_hw_state));
773 if (SYN_MODEL_NEWABS(priv->info.model_id)) {
774 hw->w = (((buf[0] & 0x30) >> 2) |
775 ((buf[0] & 0x04) >> 1) |
776 ((buf[3] & 0x04) >> 2));
778 if (synaptics_has_agm(priv) && hw->w == 2) {
779 synaptics_parse_agm(buf, priv, hw);
780 return 1;
783 hw->x = (((buf[3] & 0x10) << 8) |
784 ((buf[1] & 0x0f) << 8) |
785 buf[4]);
786 hw->y = (((buf[3] & 0x20) << 7) |
787 ((buf[1] & 0xf0) << 4) |
788 buf[5]);
789 hw->z = buf[2];
791 hw->left = (buf[0] & 0x01) ? 1 : 0;
792 hw->right = (buf[0] & 0x02) ? 1 : 0;
794 if (priv->is_forcepad) {
796 * ForcePads, like Clickpads, use middle button
797 * bits to report primary button clicks.
798 * Unfortunately they report primary button not
799 * only when user presses on the pad above certain
800 * threshold, but also when there are more than one
801 * finger on the touchpad, which interferes with
802 * out multi-finger gestures.
804 if (hw->z == 0) {
805 /* No contacts */
806 priv->press = priv->report_press = false;
807 } else if (hw->w >= 4 && ((buf[0] ^ buf[3]) & 0x01)) {
809 * Single-finger touch with pressure above
810 * the threshold. If pressure stays long
811 * enough, we'll start reporting primary
812 * button. We rely on the device continuing
813 * sending data even if finger does not
814 * move.
816 if (!priv->press) {
817 priv->press_start = jiffies;
818 priv->press = true;
819 } else if (time_after(jiffies,
820 priv->press_start +
821 msecs_to_jiffies(50))) {
822 priv->report_press = true;
824 } else {
825 priv->press = false;
828 hw->left = priv->report_press;
830 } else if (SYN_CAP_CLICKPAD(priv->info.ext_cap_0c)) {
832 * Clickpad's button is transmitted as middle button,
833 * however, since it is primary button, we will report
834 * it as BTN_LEFT.
836 hw->left = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0;
838 } else if (SYN_CAP_MIDDLE_BUTTON(priv->info.capabilities)) {
839 hw->middle = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0;
840 if (hw->w == 2)
841 hw->scroll = (s8)buf[1];
844 if (SYN_CAP_FOUR_BUTTON(priv->info.capabilities)) {
845 hw->up = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0;
846 hw->down = ((buf[0] ^ buf[3]) & 0x02) ? 1 : 0;
849 if (SYN_CAP_MULTI_BUTTON_NO(priv->info.ext_cap) > 0 &&
850 ((buf[0] ^ buf[3]) & 0x02)) {
851 synaptics_parse_ext_buttons(buf, priv, hw);
853 } else {
854 hw->x = (((buf[1] & 0x1f) << 8) | buf[2]);
855 hw->y = (((buf[4] & 0x1f) << 8) | buf[5]);
857 hw->z = (((buf[0] & 0x30) << 2) | (buf[3] & 0x3F));
858 hw->w = (((buf[1] & 0x80) >> 4) | ((buf[0] & 0x04) >> 1));
860 hw->left = (buf[0] & 0x01) ? 1 : 0;
861 hw->right = (buf[0] & 0x02) ? 1 : 0;
865 * Convert wrap-around values to negative. (X|Y)_MAX_POSITIVE
866 * is used by some firmware to indicate a finger at the edge of
867 * the touchpad whose precise position cannot be determined, so
868 * convert these values to the maximum axis value.
870 if (hw->x > X_MAX_POSITIVE)
871 hw->x -= 1 << ABS_POS_BITS;
872 else if (hw->x == X_MAX_POSITIVE)
873 hw->x = XMAX;
875 if (hw->y > Y_MAX_POSITIVE)
876 hw->y -= 1 << ABS_POS_BITS;
877 else if (hw->y == Y_MAX_POSITIVE)
878 hw->y = YMAX;
880 return 0;
883 static void synaptics_report_semi_mt_slot(struct input_dev *dev, int slot,
884 bool active, int x, int y)
886 input_mt_slot(dev, slot);
887 input_mt_report_slot_state(dev, MT_TOOL_FINGER, active);
888 if (active) {
889 input_report_abs(dev, ABS_MT_POSITION_X, x);
890 input_report_abs(dev, ABS_MT_POSITION_Y, synaptics_invert_y(y));
894 static void synaptics_report_semi_mt_data(struct input_dev *dev,
895 const struct synaptics_hw_state *a,
896 const struct synaptics_hw_state *b,
897 int num_fingers)
899 if (num_fingers >= 2) {
900 synaptics_report_semi_mt_slot(dev, 0, true, min(a->x, b->x),
901 min(a->y, b->y));
902 synaptics_report_semi_mt_slot(dev, 1, true, max(a->x, b->x),
903 max(a->y, b->y));
904 } else if (num_fingers == 1) {
905 synaptics_report_semi_mt_slot(dev, 0, true, a->x, a->y);
906 synaptics_report_semi_mt_slot(dev, 1, false, 0, 0);
907 } else {
908 synaptics_report_semi_mt_slot(dev, 0, false, 0, 0);
909 synaptics_report_semi_mt_slot(dev, 1, false, 0, 0);
913 static void synaptics_report_ext_buttons(struct psmouse *psmouse,
914 const struct synaptics_hw_state *hw)
916 struct input_dev *dev = psmouse->dev;
917 struct synaptics_data *priv = psmouse->private;
918 int ext_bits = (SYN_CAP_MULTI_BUTTON_NO(priv->info.ext_cap) + 1) >> 1;
919 int i;
921 if (!SYN_CAP_MULTI_BUTTON_NO(priv->info.ext_cap))
922 return;
924 /* Bug in FW 8.1 & 8.2, buttons are reported only when ExtBit is 1 */
925 if ((SYN_ID_FULL(priv->info.identity) == 0x801 ||
926 SYN_ID_FULL(priv->info.identity) == 0x802) &&
927 !((psmouse->packet[0] ^ psmouse->packet[3]) & 0x02))
928 return;
930 if (!SYN_CAP_EXT_BUTTONS_STICK(priv->info.ext_cap_10)) {
931 for (i = 0; i < ext_bits; i++) {
932 input_report_key(dev, BTN_0 + 2 * i,
933 hw->ext_buttons & BIT(i));
934 input_report_key(dev, BTN_1 + 2 * i,
935 hw->ext_buttons & BIT(i + ext_bits));
937 return;
941 * This generation of touchpads has the trackstick buttons
942 * physically wired to the touchpad. Re-route them through
943 * the pass-through interface.
945 if (priv->pt_port) {
946 u8 pt_buttons;
948 /* The trackstick expects at most 3 buttons */
949 pt_buttons = SYN_EXT_BUTTON_STICK_L(hw->ext_buttons) |
950 SYN_EXT_BUTTON_STICK_R(hw->ext_buttons) << 1 |
951 SYN_EXT_BUTTON_STICK_M(hw->ext_buttons) << 2;
953 serio_interrupt(priv->pt_port,
954 PSMOUSE_OOB_EXTRA_BTNS, SERIO_OOB_DATA);
955 serio_interrupt(priv->pt_port, pt_buttons, SERIO_OOB_DATA);
959 static void synaptics_report_buttons(struct psmouse *psmouse,
960 const struct synaptics_hw_state *hw)
962 struct input_dev *dev = psmouse->dev;
963 struct synaptics_data *priv = psmouse->private;
965 input_report_key(dev, BTN_LEFT, hw->left);
966 input_report_key(dev, BTN_RIGHT, hw->right);
968 if (SYN_CAP_MIDDLE_BUTTON(priv->info.capabilities))
969 input_report_key(dev, BTN_MIDDLE, hw->middle);
971 if (SYN_CAP_FOUR_BUTTON(priv->info.capabilities)) {
972 input_report_key(dev, BTN_FORWARD, hw->up);
973 input_report_key(dev, BTN_BACK, hw->down);
976 synaptics_report_ext_buttons(psmouse, hw);
979 static void synaptics_report_mt_data(struct psmouse *psmouse,
980 const struct synaptics_hw_state *sgm,
981 int num_fingers)
983 struct input_dev *dev = psmouse->dev;
984 struct synaptics_data *priv = psmouse->private;
985 const struct synaptics_hw_state *hw[2] = { sgm, &priv->agm };
986 struct input_mt_pos pos[2];
987 int slot[2], nsemi, i;
989 nsemi = clamp_val(num_fingers, 0, 2);
991 for (i = 0; i < nsemi; i++) {
992 pos[i].x = hw[i]->x;
993 pos[i].y = synaptics_invert_y(hw[i]->y);
996 input_mt_assign_slots(dev, slot, pos, nsemi, DMAX * priv->info.x_res);
998 for (i = 0; i < nsemi; i++) {
999 input_mt_slot(dev, slot[i]);
1000 input_mt_report_slot_state(dev, MT_TOOL_FINGER, true);
1001 input_report_abs(dev, ABS_MT_POSITION_X, pos[i].x);
1002 input_report_abs(dev, ABS_MT_POSITION_Y, pos[i].y);
1003 input_report_abs(dev, ABS_MT_PRESSURE, hw[i]->z);
1006 input_mt_drop_unused(dev);
1008 /* Don't use active slot count to generate BTN_TOOL events. */
1009 input_mt_report_pointer_emulation(dev, false);
1011 /* Send the number of fingers reported by touchpad itself. */
1012 input_mt_report_finger_count(dev, num_fingers);
1014 synaptics_report_buttons(psmouse, sgm);
1016 input_sync(dev);
1019 static void synaptics_image_sensor_process(struct psmouse *psmouse,
1020 struct synaptics_hw_state *sgm)
1022 struct synaptics_data *priv = psmouse->private;
1023 int num_fingers;
1026 * Update mt_state using the new finger count and current mt_state.
1028 if (sgm->z == 0)
1029 num_fingers = 0;
1030 else if (sgm->w >= 4)
1031 num_fingers = 1;
1032 else if (sgm->w == 0)
1033 num_fingers = 2;
1034 else if (sgm->w == 1)
1035 num_fingers = priv->agm_count ? priv->agm_count : 3;
1036 else
1037 num_fingers = 4;
1039 /* Send resulting input events to user space */
1040 synaptics_report_mt_data(psmouse, sgm, num_fingers);
1043 static bool synaptics_has_multifinger(struct synaptics_data *priv)
1045 if (SYN_CAP_MULTIFINGER(priv->info.capabilities))
1046 return true;
1048 /* Advanced gesture mode also sends multi finger data */
1049 return synaptics_has_agm(priv);
1053 * called for each full received packet from the touchpad
1055 static void synaptics_process_packet(struct psmouse *psmouse)
1057 struct input_dev *dev = psmouse->dev;
1058 struct synaptics_data *priv = psmouse->private;
1059 struct synaptics_device_info *info = &priv->info;
1060 struct synaptics_hw_state hw;
1061 int num_fingers;
1062 int finger_width;
1064 if (synaptics_parse_hw_state(psmouse->packet, priv, &hw))
1065 return;
1067 if (SYN_CAP_IMAGE_SENSOR(info->ext_cap_0c)) {
1068 synaptics_image_sensor_process(psmouse, &hw);
1069 return;
1072 if (hw.scroll) {
1073 priv->scroll += hw.scroll;
1075 while (priv->scroll >= 4) {
1076 input_report_key(dev, BTN_BACK, !hw.down);
1077 input_sync(dev);
1078 input_report_key(dev, BTN_BACK, hw.down);
1079 input_sync(dev);
1080 priv->scroll -= 4;
1082 while (priv->scroll <= -4) {
1083 input_report_key(dev, BTN_FORWARD, !hw.up);
1084 input_sync(dev);
1085 input_report_key(dev, BTN_FORWARD, hw.up);
1086 input_sync(dev);
1087 priv->scroll += 4;
1089 return;
1092 if (hw.z > 0 && hw.x > 1) {
1093 num_fingers = 1;
1094 finger_width = 5;
1095 if (SYN_CAP_EXTENDED(info->capabilities)) {
1096 switch (hw.w) {
1097 case 0 ... 1:
1098 if (synaptics_has_multifinger(priv))
1099 num_fingers = hw.w + 2;
1100 break;
1101 case 2:
1102 if (SYN_MODEL_PEN(info->model_id))
1103 ; /* Nothing, treat a pen as a single finger */
1104 break;
1105 case 4 ... 15:
1106 if (SYN_CAP_PALMDETECT(info->capabilities))
1107 finger_width = hw.w;
1108 break;
1111 } else {
1112 num_fingers = 0;
1113 finger_width = 0;
1116 if (cr48_profile_sensor) {
1117 synaptics_report_mt_data(psmouse, &hw, num_fingers);
1118 return;
1121 if (SYN_CAP_ADV_GESTURE(info->ext_cap_0c))
1122 synaptics_report_semi_mt_data(dev, &hw, &priv->agm,
1123 num_fingers);
1125 /* Post events
1126 * BTN_TOUCH has to be first as mousedev relies on it when doing
1127 * absolute -> relative conversion
1129 if (hw.z > 30) input_report_key(dev, BTN_TOUCH, 1);
1130 if (hw.z < 25) input_report_key(dev, BTN_TOUCH, 0);
1132 if (num_fingers > 0) {
1133 input_report_abs(dev, ABS_X, hw.x);
1134 input_report_abs(dev, ABS_Y, synaptics_invert_y(hw.y));
1136 input_report_abs(dev, ABS_PRESSURE, hw.z);
1138 if (SYN_CAP_PALMDETECT(info->capabilities))
1139 input_report_abs(dev, ABS_TOOL_WIDTH, finger_width);
1141 input_report_key(dev, BTN_TOOL_FINGER, num_fingers == 1);
1142 if (synaptics_has_multifinger(priv)) {
1143 input_report_key(dev, BTN_TOOL_DOUBLETAP, num_fingers == 2);
1144 input_report_key(dev, BTN_TOOL_TRIPLETAP, num_fingers == 3);
1147 synaptics_report_buttons(psmouse, &hw);
1149 input_sync(dev);
1152 static bool synaptics_validate_byte(struct psmouse *psmouse,
1153 int idx, enum synaptics_pkt_type pkt_type)
1155 static const u8 newabs_mask[] = { 0xC8, 0x00, 0x00, 0xC8, 0x00 };
1156 static const u8 newabs_rel_mask[] = { 0xC0, 0x00, 0x00, 0xC0, 0x00 };
1157 static const u8 newabs_rslt[] = { 0x80, 0x00, 0x00, 0xC0, 0x00 };
1158 static const u8 oldabs_mask[] = { 0xC0, 0x60, 0x00, 0xC0, 0x60 };
1159 static const u8 oldabs_rslt[] = { 0xC0, 0x00, 0x00, 0x80, 0x00 };
1160 const u8 *packet = psmouse->packet;
1162 if (idx < 0 || idx > 4)
1163 return false;
1165 switch (pkt_type) {
1167 case SYN_NEWABS:
1168 case SYN_NEWABS_RELAXED:
1169 return (packet[idx] & newabs_rel_mask[idx]) == newabs_rslt[idx];
1171 case SYN_NEWABS_STRICT:
1172 return (packet[idx] & newabs_mask[idx]) == newabs_rslt[idx];
1174 case SYN_OLDABS:
1175 return (packet[idx] & oldabs_mask[idx]) == oldabs_rslt[idx];
1177 default:
1178 psmouse_err(psmouse, "unknown packet type %d\n", pkt_type);
1179 return false;
1183 static enum synaptics_pkt_type
1184 synaptics_detect_pkt_type(struct psmouse *psmouse)
1186 int i;
1188 for (i = 0; i < 5; i++) {
1189 if (!synaptics_validate_byte(psmouse, i, SYN_NEWABS_STRICT)) {
1190 psmouse_info(psmouse, "using relaxed packet validation\n");
1191 return SYN_NEWABS_RELAXED;
1195 return SYN_NEWABS_STRICT;
1198 static psmouse_ret_t synaptics_process_byte(struct psmouse *psmouse)
1200 struct synaptics_data *priv = psmouse->private;
1202 if (psmouse->pktcnt >= 6) { /* Full packet received */
1203 if (unlikely(priv->pkt_type == SYN_NEWABS))
1204 priv->pkt_type = synaptics_detect_pkt_type(psmouse);
1206 if (SYN_CAP_PASS_THROUGH(priv->info.capabilities) &&
1207 synaptics_is_pt_packet(psmouse->packet)) {
1208 if (priv->pt_port)
1209 synaptics_pass_pt_packet(priv->pt_port,
1210 psmouse->packet);
1211 } else
1212 synaptics_process_packet(psmouse);
1214 return PSMOUSE_FULL_PACKET;
1217 return synaptics_validate_byte(psmouse, psmouse->pktcnt - 1, priv->pkt_type) ?
1218 PSMOUSE_GOOD_DATA : PSMOUSE_BAD_DATA;
1221 /*****************************************************************************
1222 * Driver initialization/cleanup functions
1223 ****************************************************************************/
1224 static void set_abs_position_params(struct input_dev *dev,
1225 struct synaptics_device_info *info,
1226 int x_code, int y_code)
1228 int x_min = info->x_min ?: XMIN_NOMINAL;
1229 int x_max = info->x_max ?: XMAX_NOMINAL;
1230 int y_min = info->y_min ?: YMIN_NOMINAL;
1231 int y_max = info->y_max ?: YMAX_NOMINAL;
1232 int fuzz = SYN_CAP_REDUCED_FILTERING(info->ext_cap_0c) ?
1233 SYN_REDUCED_FILTER_FUZZ : 0;
1235 input_set_abs_params(dev, x_code, x_min, x_max, fuzz, 0);
1236 input_set_abs_params(dev, y_code, y_min, y_max, fuzz, 0);
1237 input_abs_set_res(dev, x_code, info->x_res);
1238 input_abs_set_res(dev, y_code, info->y_res);
1241 static int set_input_params(struct psmouse *psmouse,
1242 struct synaptics_data *priv)
1244 struct input_dev *dev = psmouse->dev;
1245 struct synaptics_device_info *info = &priv->info;
1246 int i;
1247 int error;
1249 /* Reset default psmouse capabilities */
1250 __clear_bit(EV_REL, dev->evbit);
1251 bitmap_zero(dev->relbit, REL_CNT);
1252 bitmap_zero(dev->keybit, KEY_CNT);
1254 /* Things that apply to both modes */
1255 __set_bit(INPUT_PROP_POINTER, dev->propbit);
1257 input_set_capability(dev, EV_KEY, BTN_LEFT);
1259 /* Clickpads report only left button */
1260 if (!SYN_CAP_CLICKPAD(info->ext_cap_0c)) {
1261 input_set_capability(dev, EV_KEY, BTN_RIGHT);
1262 if (SYN_CAP_MIDDLE_BUTTON(info->capabilities))
1263 input_set_capability(dev, EV_KEY, BTN_MIDDLE);
1266 if (!priv->absolute_mode) {
1267 /* Relative mode */
1268 input_set_capability(dev, EV_REL, REL_X);
1269 input_set_capability(dev, EV_REL, REL_Y);
1270 return 0;
1273 /* Absolute mode */
1274 set_abs_position_params(dev, &priv->info, ABS_X, ABS_Y);
1275 input_set_abs_params(dev, ABS_PRESSURE, 0, 255, 0, 0);
1277 if (cr48_profile_sensor)
1278 input_set_abs_params(dev, ABS_MT_PRESSURE, 0, 255, 0, 0);
1280 if (SYN_CAP_IMAGE_SENSOR(info->ext_cap_0c)) {
1281 set_abs_position_params(dev, info,
1282 ABS_MT_POSITION_X, ABS_MT_POSITION_Y);
1283 /* Image sensors can report per-contact pressure */
1284 input_set_abs_params(dev, ABS_MT_PRESSURE, 0, 255, 0, 0);
1286 error = input_mt_init_slots(dev, 2,
1287 INPUT_MT_POINTER | INPUT_MT_TRACK);
1288 if (error)
1289 return error;
1291 /* Image sensors can signal 4 and 5 finger clicks */
1292 input_set_capability(dev, EV_KEY, BTN_TOOL_QUADTAP);
1293 input_set_capability(dev, EV_KEY, BTN_TOOL_QUINTTAP);
1294 } else if (SYN_CAP_ADV_GESTURE(info->ext_cap_0c)) {
1295 set_abs_position_params(dev, info,
1296 ABS_MT_POSITION_X, ABS_MT_POSITION_Y);
1298 * Profile sensor in CR-48 tracks contacts reasonably well,
1299 * other non-image sensors with AGM use semi-mt.
1301 error = input_mt_init_slots(dev, 2,
1302 INPUT_MT_POINTER |
1303 (cr48_profile_sensor ?
1304 INPUT_MT_TRACK :
1305 INPUT_MT_SEMI_MT));
1306 if (error)
1307 return error;
1310 * For semi-mt devices we send ABS_X/Y ourselves instead of
1311 * input_mt_report_pointer_emulation. But
1312 * input_mt_init_slots() resets the fuzz to 0, leading to a
1313 * filtered ABS_MT_POSITION_X but an unfiltered ABS_X
1314 * position. Let's re-initialize ABS_X/Y here.
1316 if (!cr48_profile_sensor)
1317 set_abs_position_params(dev, &priv->info, ABS_X, ABS_Y);
1320 if (SYN_CAP_PALMDETECT(info->capabilities))
1321 input_set_abs_params(dev, ABS_TOOL_WIDTH, 0, 15, 0, 0);
1323 input_set_capability(dev, EV_KEY, BTN_TOUCH);
1324 input_set_capability(dev, EV_KEY, BTN_TOOL_FINGER);
1326 if (synaptics_has_multifinger(priv)) {
1327 input_set_capability(dev, EV_KEY, BTN_TOOL_DOUBLETAP);
1328 input_set_capability(dev, EV_KEY, BTN_TOOL_TRIPLETAP);
1331 if (SYN_CAP_FOUR_BUTTON(info->capabilities) ||
1332 SYN_CAP_MIDDLE_BUTTON(info->capabilities)) {
1333 input_set_capability(dev, EV_KEY, BTN_FORWARD);
1334 input_set_capability(dev, EV_KEY, BTN_BACK);
1337 if (!SYN_CAP_EXT_BUTTONS_STICK(info->ext_cap_10))
1338 for (i = 0; i < SYN_CAP_MULTI_BUTTON_NO(info->ext_cap); i++)
1339 input_set_capability(dev, EV_KEY, BTN_0 + i);
1341 if (SYN_CAP_CLICKPAD(info->ext_cap_0c)) {
1342 __set_bit(INPUT_PROP_BUTTONPAD, dev->propbit);
1343 if (psmouse_matches_pnp_id(psmouse, topbuttonpad_pnp_ids) &&
1344 !SYN_CAP_EXT_BUTTONS_STICK(info->ext_cap_10))
1345 __set_bit(INPUT_PROP_TOPBUTTONPAD, dev->propbit);
1348 return 0;
1351 static ssize_t synaptics_show_disable_gesture(struct psmouse *psmouse,
1352 void *data, char *buf)
1354 struct synaptics_data *priv = psmouse->private;
1356 return sprintf(buf, "%c\n", priv->disable_gesture ? '1' : '0');
1359 static ssize_t synaptics_set_disable_gesture(struct psmouse *psmouse,
1360 void *data, const char *buf,
1361 size_t len)
1363 struct synaptics_data *priv = psmouse->private;
1364 unsigned int value;
1365 int err;
1367 err = kstrtouint(buf, 10, &value);
1368 if (err)
1369 return err;
1371 if (value > 1)
1372 return -EINVAL;
1374 if (value == priv->disable_gesture)
1375 return len;
1377 priv->disable_gesture = value;
1378 if (value)
1379 priv->mode |= SYN_BIT_DISABLE_GESTURE;
1380 else
1381 priv->mode &= ~SYN_BIT_DISABLE_GESTURE;
1383 if (synaptics_mode_cmd(psmouse, priv->mode))
1384 return -EIO;
1386 return len;
1389 PSMOUSE_DEFINE_ATTR(disable_gesture, S_IWUSR | S_IRUGO, NULL,
1390 synaptics_show_disable_gesture,
1391 synaptics_set_disable_gesture);
1393 static void synaptics_disconnect(struct psmouse *psmouse)
1395 struct synaptics_data *priv = psmouse->private;
1398 * We might have left a breadcrumb when trying to
1399 * set up SMbus companion.
1401 psmouse_smbus_cleanup(psmouse);
1403 if (!priv->absolute_mode &&
1404 SYN_ID_DISGEST_SUPPORTED(priv->info.identity))
1405 device_remove_file(&psmouse->ps2dev.serio->dev,
1406 &psmouse_attr_disable_gesture.dattr);
1408 synaptics_reset(psmouse);
1409 kfree(priv);
1410 psmouse->private = NULL;
1413 static int synaptics_reconnect(struct psmouse *psmouse)
1415 struct synaptics_data *priv = psmouse->private;
1416 struct synaptics_device_info info;
1417 u8 param[2];
1418 int retry = 0;
1419 int error;
1421 do {
1422 psmouse_reset(psmouse);
1423 if (retry) {
1425 * On some boxes, right after resuming, the touchpad
1426 * needs some time to finish initializing (I assume
1427 * it needs time to calibrate) and start responding
1428 * to Synaptics-specific queries, so let's wait a
1429 * bit.
1431 ssleep(1);
1433 ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_GETID);
1434 error = synaptics_detect(psmouse, 0);
1435 } while (error && ++retry < 3);
1437 if (error)
1438 return error;
1440 if (retry > 1)
1441 psmouse_dbg(psmouse, "reconnected after %d tries\n", retry);
1443 error = synaptics_query_hardware(psmouse, &info);
1444 if (error) {
1445 psmouse_err(psmouse, "Unable to query device.\n");
1446 return error;
1449 error = synaptics_set_mode(psmouse);
1450 if (error) {
1451 psmouse_err(psmouse, "Unable to initialize device.\n");
1452 return error;
1455 if (info.identity != priv->info.identity ||
1456 info.model_id != priv->info.model_id ||
1457 info.capabilities != priv->info.capabilities ||
1458 info.ext_cap != priv->info.ext_cap) {
1459 psmouse_err(psmouse,
1460 "hardware appears to be different: id(%u-%u), model(%u-%u), caps(%x-%x), ext(%x-%x).\n",
1461 priv->info.identity, info.identity,
1462 priv->info.model_id, info.model_id,
1463 priv->info.capabilities, info.capabilities,
1464 priv->info.ext_cap, info.ext_cap);
1465 return -ENXIO;
1468 return 0;
1471 static bool impaired_toshiba_kbc;
1473 static const struct dmi_system_id toshiba_dmi_table[] __initconst = {
1474 #if defined(CONFIG_DMI) && defined(CONFIG_X86)
1476 /* Toshiba Satellite */
1477 .matches = {
1478 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1479 DMI_MATCH(DMI_PRODUCT_NAME, "Satellite"),
1483 /* Toshiba Dynabook */
1484 .matches = {
1485 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1486 DMI_MATCH(DMI_PRODUCT_NAME, "dynabook"),
1490 /* Toshiba Portege M300 */
1491 .matches = {
1492 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1493 DMI_MATCH(DMI_PRODUCT_NAME, "PORTEGE M300"),
1498 /* Toshiba Portege M300 */
1499 .matches = {
1500 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1501 DMI_MATCH(DMI_PRODUCT_NAME, "Portable PC"),
1502 DMI_MATCH(DMI_PRODUCT_VERSION, "Version 1.0"),
1506 #endif
1510 static bool broken_olpc_ec;
1512 static const struct dmi_system_id olpc_dmi_table[] __initconst = {
1513 #if defined(CONFIG_DMI) && defined(CONFIG_OLPC)
1515 /* OLPC XO-1 or XO-1.5 */
1516 .matches = {
1517 DMI_MATCH(DMI_SYS_VENDOR, "OLPC"),
1518 DMI_MATCH(DMI_PRODUCT_NAME, "XO"),
1521 #endif
1525 static const struct dmi_system_id __initconst cr48_dmi_table[] = {
1526 #if defined(CONFIG_DMI) && defined(CONFIG_X86)
1528 /* Cr-48 Chromebook (Codename Mario) */
1529 .matches = {
1530 DMI_MATCH(DMI_SYS_VENDOR, "IEC"),
1531 DMI_MATCH(DMI_PRODUCT_NAME, "Mario"),
1534 #endif
1538 void __init synaptics_module_init(void)
1540 impaired_toshiba_kbc = dmi_check_system(toshiba_dmi_table);
1541 broken_olpc_ec = dmi_check_system(olpc_dmi_table);
1542 cr48_profile_sensor = dmi_check_system(cr48_dmi_table);
1545 static int synaptics_init_ps2(struct psmouse *psmouse,
1546 struct synaptics_device_info *info,
1547 bool absolute_mode)
1549 struct synaptics_data *priv;
1550 int err;
1552 synaptics_apply_quirks(psmouse, info);
1554 psmouse->private = priv = kzalloc(sizeof(struct synaptics_data), GFP_KERNEL);
1555 if (!priv)
1556 return -ENOMEM;
1558 priv->info = *info;
1559 priv->absolute_mode = absolute_mode;
1560 if (SYN_ID_DISGEST_SUPPORTED(info->identity))
1561 priv->disable_gesture = true;
1564 * Unfortunately ForcePad capability is not exported over PS/2,
1565 * so we have to resort to checking PNP IDs.
1567 priv->is_forcepad = psmouse_matches_pnp_id(psmouse, forcepad_pnp_ids);
1569 err = synaptics_set_mode(psmouse);
1570 if (err) {
1571 psmouse_err(psmouse, "Unable to initialize device.\n");
1572 goto init_fail;
1575 priv->pkt_type = SYN_MODEL_NEWABS(info->model_id) ?
1576 SYN_NEWABS : SYN_OLDABS;
1578 psmouse_info(psmouse,
1579 "Touchpad model: %lu, fw: %lu.%lu, id: %#x, caps: %#x/%#x/%#x/%#x, board id: %u, fw id: %u\n",
1580 SYN_ID_MODEL(info->identity),
1581 SYN_ID_MAJOR(info->identity), SYN_ID_MINOR(info->identity),
1582 info->model_id,
1583 info->capabilities, info->ext_cap, info->ext_cap_0c,
1584 info->ext_cap_10, info->board_id, info->firmware_id);
1586 err = set_input_params(psmouse, priv);
1587 if (err) {
1588 psmouse_err(psmouse,
1589 "failed to set up capabilities: %d\n", err);
1590 goto init_fail;
1594 * Encode touchpad model so that it can be used to set
1595 * input device->id.version and be visible to userspace.
1596 * Because version is __u16 we have to drop something.
1597 * Hardware info bits seem to be good candidates as they
1598 * are documented to be for Synaptics corp. internal use.
1600 psmouse->model = ((info->model_id & 0x00ff0000) >> 8) |
1601 (info->model_id & 0x000000ff);
1603 if (absolute_mode) {
1604 psmouse->protocol_handler = synaptics_process_byte;
1605 psmouse->pktsize = 6;
1606 } else {
1607 /* Relative mode follows standard PS/2 mouse protocol */
1608 psmouse->protocol_handler = psmouse_process_byte;
1609 psmouse->pktsize = 3;
1612 psmouse->set_rate = synaptics_set_rate;
1613 psmouse->disconnect = synaptics_disconnect;
1614 psmouse->reconnect = synaptics_reconnect;
1615 psmouse->cleanup = synaptics_reset;
1616 /* Synaptics can usually stay in sync without extra help */
1617 psmouse->resync_time = 0;
1619 if (SYN_CAP_PASS_THROUGH(info->capabilities))
1620 synaptics_pt_create(psmouse);
1623 * Toshiba's KBC seems to have trouble handling data from
1624 * Synaptics at full rate. Switch to a lower rate (roughly
1625 * the same rate as a standard PS/2 mouse).
1627 if (psmouse->rate >= 80 && impaired_toshiba_kbc) {
1628 psmouse_info(psmouse,
1629 "Toshiba %s detected, limiting rate to 40pps.\n",
1630 dmi_get_system_info(DMI_PRODUCT_NAME));
1631 psmouse->rate = 40;
1634 if (!priv->absolute_mode && SYN_ID_DISGEST_SUPPORTED(info->identity)) {
1635 err = device_create_file(&psmouse->ps2dev.serio->dev,
1636 &psmouse_attr_disable_gesture.dattr);
1637 if (err) {
1638 psmouse_err(psmouse,
1639 "Failed to create disable_gesture attribute (%d)",
1640 err);
1641 goto init_fail;
1645 return 0;
1647 init_fail:
1648 kfree(priv);
1649 return err;
1652 static int __synaptics_init(struct psmouse *psmouse, bool absolute_mode)
1654 struct synaptics_device_info info;
1655 int error;
1657 psmouse_reset(psmouse);
1659 error = synaptics_query_hardware(psmouse, &info);
1660 if (error) {
1661 psmouse_err(psmouse, "Unable to query device: %d\n", error);
1662 return error;
1665 return synaptics_init_ps2(psmouse, &info, absolute_mode);
1668 int synaptics_init_absolute(struct psmouse *psmouse)
1670 return __synaptics_init(psmouse, true);
1673 int synaptics_init_relative(struct psmouse *psmouse)
1675 return __synaptics_init(psmouse, false);
1678 static int synaptics_setup_ps2(struct psmouse *psmouse,
1679 struct synaptics_device_info *info)
1681 bool absolute_mode = true;
1682 int error;
1685 * The OLPC XO has issues with Synaptics' absolute mode; the constant
1686 * packet spew overloads the EC such that key presses on the keyboard
1687 * are missed. Given that, don't even attempt to use Absolute mode.
1688 * Relative mode seems to work just fine.
1690 if (broken_olpc_ec) {
1691 psmouse_info(psmouse,
1692 "OLPC XO detected, forcing relative protocol.\n");
1693 absolute_mode = false;
1696 error = synaptics_init_ps2(psmouse, info, absolute_mode);
1697 if (error)
1698 return error;
1700 return absolute_mode ? PSMOUSE_SYNAPTICS : PSMOUSE_SYNAPTICS_RELATIVE;
1703 #else /* CONFIG_MOUSE_PS2_SYNAPTICS */
1705 void __init synaptics_module_init(void)
1709 static int __maybe_unused
1710 synaptics_setup_ps2(struct psmouse *psmouse,
1711 struct synaptics_device_info *info)
1713 return -ENOSYS;
1716 #endif /* CONFIG_MOUSE_PS2_SYNAPTICS */
1718 #ifdef CONFIG_MOUSE_PS2_SYNAPTICS_SMBUS
1721 * The newest Synaptics device can use a secondary bus (called InterTouch) which
1722 * provides a better bandwidth and allow a better control of the touchpads.
1723 * This is used to decide if we need to use this bus or not.
1725 enum {
1726 SYNAPTICS_INTERTOUCH_NOT_SET = -1,
1727 SYNAPTICS_INTERTOUCH_OFF,
1728 SYNAPTICS_INTERTOUCH_ON,
1731 static int synaptics_intertouch = IS_ENABLED(CONFIG_RMI4_SMB) ?
1732 SYNAPTICS_INTERTOUCH_NOT_SET : SYNAPTICS_INTERTOUCH_OFF;
1733 module_param_named(synaptics_intertouch, synaptics_intertouch, int, 0644);
1734 MODULE_PARM_DESC(synaptics_intertouch, "Use a secondary bus for the Synaptics device.");
1736 static int synaptics_create_intertouch(struct psmouse *psmouse,
1737 struct synaptics_device_info *info,
1738 bool leave_breadcrumbs)
1740 bool topbuttonpad =
1741 psmouse_matches_pnp_id(psmouse, topbuttonpad_pnp_ids) &&
1742 !SYN_CAP_EXT_BUTTONS_STICK(info->ext_cap_10);
1743 const struct rmi_device_platform_data pdata = {
1744 .sensor_pdata = {
1745 .sensor_type = rmi_sensor_touchpad,
1746 .axis_align.flip_y = true,
1747 .kernel_tracking = false,
1748 .topbuttonpad = topbuttonpad,
1750 .f30_data = {
1751 .buttonpad = SYN_CAP_CLICKPAD(info->ext_cap_0c),
1752 .trackstick_buttons =
1753 !!SYN_CAP_EXT_BUTTONS_STICK(info->ext_cap_10),
1756 const struct i2c_board_info intertouch_board = {
1757 I2C_BOARD_INFO("rmi4_smbus", 0x2c),
1758 .flags = I2C_CLIENT_HOST_NOTIFY,
1761 return psmouse_smbus_init(psmouse, &intertouch_board,
1762 &pdata, sizeof(pdata), true,
1763 leave_breadcrumbs);
1767 * synaptics_setup_intertouch - called once the PS/2 devices are enumerated
1768 * and decides to instantiate a SMBus InterTouch device.
1770 static int synaptics_setup_intertouch(struct psmouse *psmouse,
1771 struct synaptics_device_info *info,
1772 bool leave_breadcrumbs)
1774 int error;
1776 if (synaptics_intertouch == SYNAPTICS_INTERTOUCH_OFF)
1777 return -ENXIO;
1779 if (synaptics_intertouch == SYNAPTICS_INTERTOUCH_NOT_SET) {
1780 if (!psmouse_matches_pnp_id(psmouse, topbuttonpad_pnp_ids) &&
1781 !psmouse_matches_pnp_id(psmouse, smbus_pnp_ids)) {
1783 if (!psmouse_matches_pnp_id(psmouse, forcepad_pnp_ids))
1784 psmouse_info(psmouse,
1785 "Your touchpad (%s) says it can support a different bus. "
1786 "If i2c-hid and hid-rmi are not used, you might want to try setting psmouse.synaptics_intertouch to 1 and report this to linux-input@vger.kernel.org.\n",
1787 psmouse->ps2dev.serio->firmware_id);
1789 return -ENXIO;
1793 psmouse_info(psmouse, "Trying to set up SMBus access\n");
1795 error = synaptics_create_intertouch(psmouse, info, leave_breadcrumbs);
1796 if (error) {
1797 if (error == -EAGAIN)
1798 psmouse_info(psmouse, "SMbus companion is not ready yet\n");
1799 else
1800 psmouse_err(psmouse, "unable to create intertouch device\n");
1802 return error;
1805 return 0;
1808 int synaptics_init_smbus(struct psmouse *psmouse)
1810 struct synaptics_device_info info;
1811 int error;
1813 psmouse_reset(psmouse);
1815 error = synaptics_query_hardware(psmouse, &info);
1816 if (error) {
1817 psmouse_err(psmouse, "Unable to query device: %d\n", error);
1818 return error;
1821 if (!SYN_CAP_INTERTOUCH(info.ext_cap_0c))
1822 return -ENXIO;
1824 return synaptics_create_intertouch(psmouse, &info, false);
1827 #else /* CONFIG_MOUSE_PS2_SYNAPTICS_SMBUS */
1829 static int __maybe_unused
1830 synaptics_setup_intertouch(struct psmouse *psmouse,
1831 struct synaptics_device_info *info,
1832 bool leave_breadcrumbs)
1834 return -ENOSYS;
1837 int synaptics_init_smbus(struct psmouse *psmouse)
1839 return -ENOSYS;
1842 #endif /* CONFIG_MOUSE_PS2_SYNAPTICS_SMBUS */
1844 #if defined(CONFIG_MOUSE_PS2_SYNAPTICS) || \
1845 defined(CONFIG_MOUSE_PS2_SYNAPTICS_SMBUS)
1847 int synaptics_init(struct psmouse *psmouse)
1849 struct synaptics_device_info info;
1850 int error;
1851 int retval;
1853 psmouse_reset(psmouse);
1855 error = synaptics_query_hardware(psmouse, &info);
1856 if (error) {
1857 psmouse_err(psmouse, "Unable to query device: %d\n", error);
1858 return error;
1861 if (SYN_CAP_INTERTOUCH(info.ext_cap_0c)) {
1862 if ((!IS_ENABLED(CONFIG_RMI4_SMB) ||
1863 !IS_ENABLED(CONFIG_MOUSE_PS2_SYNAPTICS_SMBUS)) &&
1864 /* Forcepads need F21, which is not ready */
1865 !psmouse_matches_pnp_id(psmouse, forcepad_pnp_ids)) {
1866 psmouse_warn(psmouse,
1867 "The touchpad can support a better bus than the too old PS/2 protocol. "
1868 "Make sure MOUSE_PS2_SYNAPTICS_SMBUS and RMI4_SMB are enabled to get a better touchpad experience.\n");
1871 error = synaptics_setup_intertouch(psmouse, &info, true);
1872 if (!error)
1873 return PSMOUSE_SYNAPTICS_SMBUS;
1876 retval = synaptics_setup_ps2(psmouse, &info);
1877 if (retval < 0) {
1879 * Not using any flavor of Synaptics support, so clean up
1880 * SMbus breadcrumbs, if any.
1882 psmouse_smbus_cleanup(psmouse);
1885 return retval;
1888 #else /* CONFIG_MOUSE_PS2_SYNAPTICS || CONFIG_MOUSE_PS2_SYNAPTICS_SMBUS */
1890 int synaptics_init(struct psmouse *psmouse)
1892 return -ENOSYS;
1895 #endif /* CONFIG_MOUSE_PS2_SYNAPTICS || CONFIG_MOUSE_PS2_SYNAPTICS_SMBUS */