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