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