blob: ef9e0b8a9aa754b6b49e26b7b050c2a7bcf1f078 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Dmitry Torokhov85214782011-12-12 00:05:53 -080027#include <linux/delay.h>
Dmitry Torokhov7705d542009-12-03 23:21:14 -080028#include <linux/dmi.h>
Henrik Rydbergfec6e522010-12-21 18:11:25 +010029#include <linux/input/mt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/serio.h>
31#include <linux/libps2.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "psmouse.h"
34#include "synaptics.h"
35
36/*
37 * The x/y limits are taken from the Synaptics TouchPad interfacing Guide,
38 * section 2.3.2, which says that they should be valid regardless of the
39 * actual size of the sensor.
Dmitry Torokhov83ba9ea2010-05-10 23:06:52 -070040 * Note that newer firmware allows querying device for maximum useable
41 * coordinates.
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 */
Seth Forsheec03945062012-07-24 23:54:11 -070043#define XMIN 0
44#define XMAX 6143
45#define YMIN 0
46#define YMAX 6143
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#define XMIN_NOMINAL 1472
48#define XMAX_NOMINAL 5472
49#define YMIN_NOMINAL 1408
50#define YMAX_NOMINAL 4448
51
Seth Forsheec03945062012-07-24 23:54:11 -070052/* Size in bits of absolute position values reported by the hardware */
53#define ABS_POS_BITS 13
54
55/*
Seth Forshee824efd32012-09-28 10:29:21 -070056 * These values should represent the absolute maximum value that will
57 * be reported for a positive position value. Some Synaptics firmware
58 * uses this value to indicate a finger near the edge of the touchpad
59 * whose precise position cannot be determined.
60 *
61 * At least one touchpad is known to report positions in excess of this
62 * value which are actually negative values truncated to the 13-bit
63 * reporting range. These values have never been observed to be lower
64 * than 8184 (i.e. -8), so we treat all values greater than 8176 as
65 * negative and any other value as positive.
Seth Forsheec03945062012-07-24 23:54:11 -070066 */
Seth Forshee824efd32012-09-28 10:29:21 -070067#define X_MAX_POSITIVE 8176
68#define Y_MAX_POSITIVE 8176
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Andres Salomon55e3d922007-03-10 01:39:54 -050070/*****************************************************************************
71 * Stuff we need even when we do not want native Synaptics support
72 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74/*
75 * Set the synaptics touchpad mode byte by special commands
76 */
77static int synaptics_mode_cmd(struct psmouse *psmouse, unsigned char mode)
78{
79 unsigned char param[1];
80
81 if (psmouse_sliced_command(psmouse, mode))
82 return -1;
83 param[0] = SYN_PS_SET_MODE2;
84 if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_SETRATE))
85 return -1;
86 return 0;
87}
88
Dmitry Torokhovb7802c52009-09-09 19:13:20 -070089int synaptics_detect(struct psmouse *psmouse, bool set_properties)
Andres Salomon55e3d922007-03-10 01:39:54 -050090{
91 struct ps2dev *ps2dev = &psmouse->ps2dev;
92 unsigned char param[4];
93
94 param[0] = 0;
95
96 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
97 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
98 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
99 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
100 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO);
101
102 if (param[1] != 0x47)
103 return -ENODEV;
104
105 if (set_properties) {
106 psmouse->vendor = "Synaptics";
107 psmouse->name = "TouchPad";
108 }
109
110 return 0;
111}
112
113void synaptics_reset(struct psmouse *psmouse)
114{
115 /* reset touchpad back to relative mode, gestures enabled */
116 synaptics_mode_cmd(psmouse, 0);
117}
118
119#ifdef CONFIG_MOUSE_PS2_SYNAPTICS
Hans de Goede0f68f392014-05-19 22:54:09 -0700120struct min_max_quirk {
121 const char * const *pnp_ids;
122 int x_min, x_max, y_min, y_max;
123};
124
125static const struct min_max_quirk min_max_pnpid_table[] = {
126 {
127 (const char * const []){"LEN0033", NULL},
128 1024, 5052, 2258, 4832
129 },
130 {
131 (const char * const []){"LEN0035", "LEN0042", NULL},
132 1232, 5710, 1156, 4696
133 },
134 {
Hans de Goedee76aed92014-07-14 17:12:21 -0700135 (const char * const []){"LEN0034", "LEN0036", "LEN2002",
136 "LEN2004", NULL},
Hans de Goede0f68f392014-05-19 22:54:09 -0700137 1024, 5112, 2024, 4832
138 },
139 {
140 (const char * const []){"LEN2001", NULL},
141 1024, 5022, 2508, 4832
142 },
143 { }
144};
145
Hans de Goede43e19882014-04-19 22:26:41 -0700146/* This list has been kindly provided by Synaptics. */
147static const char * const topbuttonpad_pnp_ids[] = {
148 "LEN0017",
149 "LEN0018",
150 "LEN0019",
151 "LEN0023",
152 "LEN002A",
153 "LEN002B",
154 "LEN002C",
155 "LEN002D",
156 "LEN002E",
157 "LEN0033", /* Helix */
Hans de Goede0f68f392014-05-19 22:54:09 -0700158 "LEN0034", /* T431s, L440, L540, T540, W540, X1 Carbon 2nd */
Hans de Goede43e19882014-04-19 22:26:41 -0700159 "LEN0035", /* X240 */
160 "LEN0036", /* T440 */
161 "LEN0037",
162 "LEN0038",
163 "LEN0041",
164 "LEN0042", /* Yoga */
165 "LEN0045",
166 "LEN0046",
167 "LEN0047",
168 "LEN0048",
169 "LEN0049",
170 "LEN2000",
Hans de Goede0f68f392014-05-19 22:54:09 -0700171 "LEN2001", /* Edge E431 */
Hans de Goedee76aed92014-07-14 17:12:21 -0700172 "LEN2002", /* Edge E531 */
Hans de Goede43e19882014-04-19 22:26:41 -0700173 "LEN2003",
174 "LEN2004", /* L440 */
175 "LEN2005",
176 "LEN2006",
177 "LEN2007",
178 "LEN2008",
179 "LEN2009",
180 "LEN200A",
181 "LEN200B",
182 NULL
183};
Andres Salomon55e3d922007-03-10 01:39:54 -0500184
Hans de Goedee2f61102014-05-19 22:53:23 -0700185static bool matches_pnp_id(struct psmouse *psmouse, const char * const ids[])
186{
187 int i;
188
189 if (!strncmp(psmouse->ps2dev.serio->firmware_id, "PNP:", 4))
190 for (i = 0; ids[i]; i++)
191 if (strstr(psmouse->ps2dev.serio->firmware_id, ids[i]))
192 return true;
193
194 return false;
195}
196
Andres Salomon55e3d922007-03-10 01:39:54 -0500197/*****************************************************************************
198 * Synaptics communications functions
199 ****************************************************************************/
200
201/*
JJ Ding1a49a0a2012-05-10 22:32:00 -0700202 * Synaptics touchpads report the y coordinate from bottom to top, which is
203 * opposite from what userspace expects.
204 * This function is used to invert y before reporting.
205 */
206static int synaptics_invert_y(int y)
207{
208 return YMAX_NOMINAL + YMIN_NOMINAL - y;
209}
210
211/*
Andres Salomon55e3d922007-03-10 01:39:54 -0500212 * Send a command to the synpatics touchpad by special commands
213 */
214static int synaptics_send_cmd(struct psmouse *psmouse, unsigned char c, unsigned char *param)
215{
216 if (psmouse_sliced_command(psmouse, c))
217 return -1;
218 if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_GETINFO))
219 return -1;
220 return 0;
221}
222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223/*
224 * Read the model-id bytes from the touchpad
225 * see also SYN_MODEL_* macros
226 */
227static int synaptics_model_id(struct psmouse *psmouse)
228{
229 struct synaptics_data *priv = psmouse->private;
230 unsigned char mi[3];
231
232 if (synaptics_send_cmd(psmouse, SYN_QUE_MODEL, mi))
233 return -1;
234 priv->model_id = (mi[0]<<16) | (mi[1]<<8) | mi[2];
235 return 0;
236}
237
238/*
Daniel Kurtzc6bd9d42012-07-07 18:08:51 -0700239 * Read the board id from the touchpad
240 * The board id is encoded in the "QUERY MODES" response
241 */
242static int synaptics_board_id(struct psmouse *psmouse)
243{
244 struct synaptics_data *priv = psmouse->private;
245 unsigned char bid[3];
246
247 if (synaptics_send_cmd(psmouse, SYN_QUE_MODES, bid))
248 return -1;
249 priv->board_id = ((bid[0] & 0xfc) << 6) | bid[1];
250 return 0;
251}
252
253/*
254 * Read the firmware id from the touchpad
255 */
256static int synaptics_firmware_id(struct psmouse *psmouse)
257{
258 struct synaptics_data *priv = psmouse->private;
259 unsigned char fwid[3];
260
261 if (synaptics_send_cmd(psmouse, SYN_QUE_FIRMWARE_ID, fwid))
262 return -1;
263 priv->firmware_id = (fwid[0] << 16) | (fwid[1] << 8) | fwid[2];
264 return 0;
265}
266
267/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 * Read the capability-bits from the touchpad
269 * see also the SYN_CAP_* macros
270 */
271static int synaptics_capability(struct psmouse *psmouse)
272{
273 struct synaptics_data *priv = psmouse->private;
274 unsigned char cap[3];
275
276 if (synaptics_send_cmd(psmouse, SYN_QUE_CAPABILITIES, cap))
277 return -1;
278 priv->capabilities = (cap[0] << 16) | (cap[1] << 8) | cap[2];
Takashi Iwai5f57d672010-04-19 10:37:21 -0700279 priv->ext_cap = priv->ext_cap_0c = 0;
280
Dmitry Torokhov3619b8f2010-07-21 00:01:19 -0700281 /*
282 * Older firmwares had submodel ID fixed to 0x47
283 */
284 if (SYN_ID_FULL(priv->identity) < 0x705 &&
285 SYN_CAP_SUBMODEL_ID(priv->capabilities) != 0x47) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 return -1;
Dmitry Torokhov3619b8f2010-07-21 00:01:19 -0700287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289 /*
290 * Unless capExtended is set the rest of the flags should be ignored
291 */
292 if (!SYN_CAP_EXTENDED(priv->capabilities))
293 priv->capabilities = 0;
294
295 if (SYN_EXT_CAP_REQUESTS(priv->capabilities) >= 1) {
296 if (synaptics_send_cmd(psmouse, SYN_QUE_EXT_CAPAB, cap)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700297 psmouse_warn(psmouse,
298 "device claims to have extended capabilities, but I'm not able to read them.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 } else {
300 priv->ext_cap = (cap[0] << 16) | (cap[1] << 8) | cap[2];
301
302 /*
303 * if nExtBtn is greater than 8 it should be considered
304 * invalid and treated as 0
305 */
306 if (SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap) > 8)
307 priv->ext_cap &= 0xff0fff;
308 }
309 }
Takashi Iwai5f57d672010-04-19 10:37:21 -0700310
311 if (SYN_EXT_CAP_REQUESTS(priv->capabilities) >= 4) {
312 if (synaptics_send_cmd(psmouse, SYN_QUE_EXT_CAPAB_0C, cap)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700313 psmouse_warn(psmouse,
314 "device claims to have extended capability 0x0c, but I'm not able to read it.\n");
Takashi Iwai5f57d672010-04-19 10:37:21 -0700315 } else {
316 priv->ext_cap_0c = (cap[0] << 16) | (cap[1] << 8) | cap[2];
317 }
318 }
319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 return 0;
321}
322
323/*
324 * Identify Touchpad
325 * See also the SYN_ID_* macros
326 */
327static int synaptics_identify(struct psmouse *psmouse)
328{
329 struct synaptics_data *priv = psmouse->private;
330 unsigned char id[3];
331
332 if (synaptics_send_cmd(psmouse, SYN_QUE_IDENTIFY, id))
333 return -1;
334 priv->identity = (id[0]<<16) | (id[1]<<8) | id[2];
335 if (SYN_ID_IS_SYNAPTICS(priv->identity))
336 return 0;
337 return -1;
338}
339
Tero Saarniec20a022009-06-10 23:27:24 -0700340/*
Dmitry Torokhov83ba9ea2010-05-10 23:06:52 -0700341 * Read touchpad resolution and maximum reported coordinates
Tero Saarniec20a022009-06-10 23:27:24 -0700342 * Resolution is left zero if touchpad does not support the query
343 */
Benjamin Tissoires421e08c2014-03-28 00:43:00 -0700344
Tero Saarniec20a022009-06-10 23:27:24 -0700345static int synaptics_resolution(struct psmouse *psmouse)
346{
347 struct synaptics_data *priv = psmouse->private;
Dmitry Torokhova66413f2011-07-09 12:32:56 -0700348 unsigned char resp[3];
Hans de Goede0f68f392014-05-19 22:54:09 -0700349 int i;
Tero Saarniec20a022009-06-10 23:27:24 -0700350
351 if (SYN_ID_MAJOR(priv->identity) < 4)
Takashi Iwaibbddd192010-07-14 09:32:46 -0700352 return 0;
Tero Saarniec20a022009-06-10 23:27:24 -0700353
Dmitry Torokhova66413f2011-07-09 12:32:56 -0700354 if (synaptics_send_cmd(psmouse, SYN_QUE_RESOLUTION, resp) == 0) {
355 if (resp[0] != 0 && (resp[1] & 0x80) && resp[2] != 0) {
356 priv->x_res = resp[0]; /* x resolution in units/mm */
357 priv->y_res = resp[2]; /* y resolution in units/mm */
Dmitry Torokhov83ba9ea2010-05-10 23:06:52 -0700358 }
359 }
Tero Saarniec20a022009-06-10 23:27:24 -0700360
Benjamin Tissoiresd49cb7a2014-06-07 22:37:47 -0700361 for (i = 0; min_max_pnpid_table[i].pnp_ids; i++) {
362 if (matches_pnp_id(psmouse, min_max_pnpid_table[i].pnp_ids)) {
363 priv->x_min = min_max_pnpid_table[i].x_min;
364 priv->x_max = min_max_pnpid_table[i].x_max;
365 priv->y_min = min_max_pnpid_table[i].y_min;
366 priv->y_max = min_max_pnpid_table[i].y_max;
367 return 0;
368 }
369 }
370
Dmitry Torokhov83ba9ea2010-05-10 23:06:52 -0700371 if (SYN_EXT_CAP_REQUESTS(priv->capabilities) >= 5 &&
372 SYN_CAP_MAX_DIMENSIONS(priv->ext_cap_0c)) {
Dmitry Torokhova66413f2011-07-09 12:32:56 -0700373 if (synaptics_send_cmd(psmouse, SYN_QUE_EXT_MAX_COORDS, resp)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700374 psmouse_warn(psmouse,
375 "device claims to have max coordinates query, but I'm not able to read it.\n");
Dmitry Torokhov83ba9ea2010-05-10 23:06:52 -0700376 } else {
Dmitry Torokhova66413f2011-07-09 12:32:56 -0700377 priv->x_max = (resp[0] << 5) | ((resp[1] & 0x0f) << 1);
378 priv->y_max = (resp[2] << 5) | ((resp[1] & 0xf0) >> 3);
379 }
380 }
381
382 if (SYN_EXT_CAP_REQUESTS(priv->capabilities) >= 7 &&
383 SYN_CAP_MIN_DIMENSIONS(priv->ext_cap_0c)) {
384 if (synaptics_send_cmd(psmouse, SYN_QUE_EXT_MIN_COORDS, resp)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700385 psmouse_warn(psmouse,
386 "device claims to have min coordinates query, but I'm not able to read it.\n");
Dmitry Torokhova66413f2011-07-09 12:32:56 -0700387 } else {
388 priv->x_min = (resp[0] << 5) | ((resp[1] & 0x0f) << 1);
389 priv->y_min = (resp[2] << 5) | ((resp[1] & 0xf0) >> 3);
Dmitry Torokhov83ba9ea2010-05-10 23:06:52 -0700390 }
Tero Saarniec20a022009-06-10 23:27:24 -0700391 }
392
393 return 0;
394}
395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396static int synaptics_query_hardware(struct psmouse *psmouse)
397{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 if (synaptics_identify(psmouse))
399 return -1;
400 if (synaptics_model_id(psmouse))
401 return -1;
Daniel Kurtzc6bd9d42012-07-07 18:08:51 -0700402 if (synaptics_firmware_id(psmouse))
403 return -1;
404 if (synaptics_board_id(psmouse))
405 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 if (synaptics_capability(psmouse))
407 return -1;
Tero Saarniec20a022009-06-10 23:27:24 -0700408 if (synaptics_resolution(psmouse))
409 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 return 0;
412}
413
Daniel Drake7968a5d2011-11-08 00:00:35 -0800414static int synaptics_set_advanced_gesture_mode(struct psmouse *psmouse)
415{
416 static unsigned char param = 0xc8;
417 struct synaptics_data *priv = psmouse->private;
418
Benjamin Herrenschmidt899c6122012-04-20 22:34:49 -0700419 if (!(SYN_CAP_ADV_GESTURE(priv->ext_cap_0c) ||
420 SYN_CAP_IMAGE_SENSOR(priv->ext_cap_0c)))
Daniel Drake7968a5d2011-11-08 00:00:35 -0800421 return 0;
422
423 if (psmouse_sliced_command(psmouse, SYN_QUE_MODEL))
424 return -1;
425
426 if (ps2_command(&psmouse->ps2dev, &param, PSMOUSE_CMD_SETRATE))
427 return -1;
428
429 /* Advanced gesture mode also sends multi finger data */
430 priv->capabilities |= BIT(1);
431
432 return 0;
433}
434
435static int synaptics_set_mode(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
437 struct synaptics_data *priv = psmouse->private;
438
Daniel Drake7968a5d2011-11-08 00:00:35 -0800439 priv->mode = 0;
440 if (priv->absolute_mode)
441 priv->mode |= SYN_BIT_ABSOLUTE_MODE;
442 if (priv->disable_gesture)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 priv->mode |= SYN_BIT_DISABLE_GESTURE;
Daniel Drake7968a5d2011-11-08 00:00:35 -0800444 if (psmouse->rate >= 80)
445 priv->mode |= SYN_BIT_HIGH_RATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 if (SYN_CAP_EXTENDED(priv->capabilities))
447 priv->mode |= SYN_BIT_W_MODE;
448
449 if (synaptics_mode_cmd(psmouse, priv->mode))
450 return -1;
451
Daniel Drake7968a5d2011-11-08 00:00:35 -0800452 if (priv->absolute_mode &&
453 synaptics_set_advanced_gesture_mode(psmouse)) {
454 psmouse_err(psmouse, "Advanced gesture mode init failed.\n");
455 return -1;
456 }
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 return 0;
459}
460
461static void synaptics_set_rate(struct psmouse *psmouse, unsigned int rate)
462{
463 struct synaptics_data *priv = psmouse->private;
464
465 if (rate >= 80) {
466 priv->mode |= SYN_BIT_HIGH_RATE;
467 psmouse->rate = 80;
468 } else {
469 priv->mode &= ~SYN_BIT_HIGH_RATE;
470 psmouse->rate = 40;
471 }
472
473 synaptics_mode_cmd(psmouse, priv->mode);
474}
475
476/*****************************************************************************
477 * Synaptics pass-through PS/2 port support
478 ****************************************************************************/
479static int synaptics_pt_write(struct serio *serio, unsigned char c)
480{
481 struct psmouse *parent = serio_get_drvdata(serio->parent);
482 char rate_param = SYN_PS_CLIENT_CMD; /* indicates that we want pass-through port */
483
484 if (psmouse_sliced_command(parent, c))
485 return -1;
486 if (ps2_command(&parent->ps2dev, &rate_param, PSMOUSE_CMD_SETRATE))
487 return -1;
488 return 0;
489}
490
Dmitry Torokhova8b3c0f2010-10-04 21:46:10 -0700491static int synaptics_pt_start(struct serio *serio)
492{
493 struct psmouse *parent = serio_get_drvdata(serio->parent);
494 struct synaptics_data *priv = parent->private;
495
496 serio_pause_rx(parent->ps2dev.serio);
497 priv->pt_port = serio;
498 serio_continue_rx(parent->ps2dev.serio);
499
500 return 0;
501}
502
503static void synaptics_pt_stop(struct serio *serio)
504{
505 struct psmouse *parent = serio_get_drvdata(serio->parent);
506 struct synaptics_data *priv = parent->private;
507
508 serio_pause_rx(parent->ps2dev.serio);
509 priv->pt_port = NULL;
510 serio_continue_rx(parent->ps2dev.serio);
511}
512
513static int synaptics_is_pt_packet(unsigned char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
515 return (buf[0] & 0xFC) == 0x84 && (buf[3] & 0xCC) == 0xC4;
516}
517
518static void synaptics_pass_pt_packet(struct serio *ptport, unsigned char *packet)
519{
520 struct psmouse *child = serio_get_drvdata(ptport);
521
522 if (child && child->state == PSMOUSE_ACTIVATED) {
David Howells7d12e782006-10-05 14:55:46 +0100523 serio_interrupt(ptport, packet[1], 0);
524 serio_interrupt(ptport, packet[4], 0);
525 serio_interrupt(ptport, packet[5], 0);
Sergey Vlasov33fdfa92005-07-24 00:53:32 -0500526 if (child->pktsize == 4)
David Howells7d12e782006-10-05 14:55:46 +0100527 serio_interrupt(ptport, packet[2], 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 } else
David Howells7d12e782006-10-05 14:55:46 +0100529 serio_interrupt(ptport, packet[1], 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530}
531
532static void synaptics_pt_activate(struct psmouse *psmouse)
533{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 struct synaptics_data *priv = psmouse->private;
Dmitry Torokhova8b3c0f2010-10-04 21:46:10 -0700535 struct psmouse *child = serio_get_drvdata(priv->pt_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 /* adjust the touchpad to child's choice of protocol */
538 if (child) {
Sergey Vlasov33fdfa92005-07-24 00:53:32 -0500539 if (child->pktsize == 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 priv->mode |= SYN_BIT_FOUR_BYTE_CLIENT;
541 else
542 priv->mode &= ~SYN_BIT_FOUR_BYTE_CLIENT;
543
544 if (synaptics_mode_cmd(psmouse, priv->mode))
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700545 psmouse_warn(psmouse,
546 "failed to switch guest protocol\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 }
548}
549
550static void synaptics_pt_create(struct psmouse *psmouse)
551{
552 struct serio *serio;
553
Eric Sesterhennb39787a2006-03-14 00:09:16 -0500554 serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 if (!serio) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700556 psmouse_err(psmouse,
557 "not enough memory for pass-through port\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 return;
559 }
560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 serio->id.type = SERIO_PS_PSTHRU;
562 strlcpy(serio->name, "Synaptics pass-through", sizeof(serio->name));
563 strlcpy(serio->phys, "synaptics-pt/serio0", sizeof(serio->name));
564 serio->write = synaptics_pt_write;
Dmitry Torokhova8b3c0f2010-10-04 21:46:10 -0700565 serio->start = synaptics_pt_start;
566 serio->stop = synaptics_pt_stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 serio->parent = psmouse->ps2dev.serio;
568
569 psmouse->pt_activate = synaptics_pt_activate;
570
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700571 psmouse_info(psmouse, "serio: %s port at %s\n",
572 serio->name, psmouse->phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 serio_register_port(serio);
574}
575
576/*****************************************************************************
577 * Functions to interpret the absolute mode packets
578 ****************************************************************************/
579
Daniel Kurtza6ca40c2011-08-23 23:02:31 -0700580static void synaptics_mt_state_set(struct synaptics_mt_state *state, int count,
581 int sgm, int agm)
582{
583 state->count = count;
584 state->sgm = sgm;
585 state->agm = agm;
586}
587
Daniel Kurtz7afdb842011-08-23 23:00:33 -0700588static void synaptics_parse_agm(const unsigned char buf[],
Daniel Kurtza6ca40c2011-08-23 23:02:31 -0700589 struct synaptics_data *priv,
590 struct synaptics_hw_state *hw)
Daniel Kurtz7afdb842011-08-23 23:00:33 -0700591{
592 struct synaptics_hw_state *agm = &priv->agm;
Daniel Kurtza6ca40c2011-08-23 23:02:31 -0700593 int agm_packet_type;
Daniel Kurtz7afdb842011-08-23 23:00:33 -0700594
Daniel Kurtza6ca40c2011-08-23 23:02:31 -0700595 agm_packet_type = (buf[5] & 0x30) >> 4;
596 switch (agm_packet_type) {
597 case 1:
598 /* Gesture packet: (x, y, z) half resolution */
599 agm->w = hw->w;
600 agm->x = (((buf[4] & 0x0f) << 8) | buf[1]) << 1;
601 agm->y = (((buf[4] & 0xf0) << 4) | buf[2]) << 1;
602 agm->z = ((buf[3] & 0x30) | (buf[5] & 0x0f)) << 1;
603 break;
604
605 case 2:
606 /* AGM-CONTACT packet: (count, sgm, agm) */
607 synaptics_mt_state_set(&agm->mt_state, buf[1], buf[2], buf[4]);
608 break;
609
610 default:
611 break;
612 }
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700613
614 /* Record that at least one AGM has been received since last SGM */
615 priv->agm_pending = true;
Daniel Kurtz7afdb842011-08-23 23:00:33 -0700616}
617
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100618static int synaptics_parse_hw_state(const unsigned char buf[],
619 struct synaptics_data *priv,
620 struct synaptics_hw_state *hw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
622 memset(hw, 0, sizeof(struct synaptics_hw_state));
623
624 if (SYN_MODEL_NEWABS(priv->model_id)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 hw->w = (((buf[0] & 0x30) >> 2) |
626 ((buf[0] & 0x04) >> 1) |
627 ((buf[3] & 0x04) >> 2));
628
629 hw->left = (buf[0] & 0x01) ? 1 : 0;
630 hw->right = (buf[0] & 0x02) ? 1 : 0;
631
Takashi Iwai5f57d672010-04-19 10:37:21 -0700632 if (SYN_CAP_CLICKPAD(priv->ext_cap_0c)) {
633 /*
634 * Clickpad's button is transmitted as middle button,
635 * however, since it is primary button, we will report
636 * it as BTN_LEFT.
637 */
638 hw->left = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0;
639
640 } else if (SYN_CAP_MIDDLE_BUTTON(priv->capabilities)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 hw->middle = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0;
642 if (hw->w == 2)
643 hw->scroll = (signed char)(buf[1]);
644 }
645
646 if (SYN_CAP_FOUR_BUTTON(priv->capabilities)) {
647 hw->up = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0;
648 hw->down = ((buf[0] ^ buf[3]) & 0x02) ? 1 : 0;
649 }
650
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700651 if ((SYN_CAP_ADV_GESTURE(priv->ext_cap_0c) ||
652 SYN_CAP_IMAGE_SENSOR(priv->ext_cap_0c)) &&
653 hw->w == 2) {
Daniel Kurtza6ca40c2011-08-23 23:02:31 -0700654 synaptics_parse_agm(buf, priv, hw);
Daniel Kurtz28d5fd82011-07-06 22:57:39 -0700655 return 1;
656 }
657
658 hw->x = (((buf[3] & 0x10) << 8) |
659 ((buf[1] & 0x0f) << 8) |
660 buf[4]);
661 hw->y = (((buf[3] & 0x20) << 7) |
662 ((buf[1] & 0xf0) << 4) |
663 buf[5]);
664 hw->z = buf[2];
665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 if (SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap) &&
667 ((buf[0] ^ buf[3]) & 0x02)) {
668 switch (SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap) & ~0x01) {
669 default:
670 /*
671 * if nExtBtn is greater than 8 it should be
672 * considered invalid and treated as 0
673 */
674 break;
675 case 8:
676 hw->ext_buttons |= ((buf[5] & 0x08)) ? 0x80 : 0;
677 hw->ext_buttons |= ((buf[4] & 0x08)) ? 0x40 : 0;
678 case 6:
679 hw->ext_buttons |= ((buf[5] & 0x04)) ? 0x20 : 0;
680 hw->ext_buttons |= ((buf[4] & 0x04)) ? 0x10 : 0;
681 case 4:
682 hw->ext_buttons |= ((buf[5] & 0x02)) ? 0x08 : 0;
683 hw->ext_buttons |= ((buf[4] & 0x02)) ? 0x04 : 0;
684 case 2:
685 hw->ext_buttons |= ((buf[5] & 0x01)) ? 0x02 : 0;
686 hw->ext_buttons |= ((buf[4] & 0x01)) ? 0x01 : 0;
687 }
688 }
689 } else {
690 hw->x = (((buf[1] & 0x1f) << 8) | buf[2]);
691 hw->y = (((buf[4] & 0x1f) << 8) | buf[5]);
692
693 hw->z = (((buf[0] & 0x30) << 2) | (buf[3] & 0x3F));
694 hw->w = (((buf[1] & 0x80) >> 4) | ((buf[0] & 0x04) >> 1));
695
696 hw->left = (buf[0] & 0x01) ? 1 : 0;
697 hw->right = (buf[0] & 0x02) ? 1 : 0;
698 }
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100699
Seth Forshee824efd32012-09-28 10:29:21 -0700700 /*
701 * Convert wrap-around values to negative. (X|Y)_MAX_POSITIVE
702 * is used by some firmware to indicate a finger at the edge of
703 * the touchpad whose precise position cannot be determined, so
704 * convert these values to the maximum axis value.
705 */
Seth Forsheec03945062012-07-24 23:54:11 -0700706 if (hw->x > X_MAX_POSITIVE)
707 hw->x -= 1 << ABS_POS_BITS;
Seth Forshee824efd32012-09-28 10:29:21 -0700708 else if (hw->x == X_MAX_POSITIVE)
709 hw->x = XMAX;
710
Seth Forsheec03945062012-07-24 23:54:11 -0700711 if (hw->y > Y_MAX_POSITIVE)
712 hw->y -= 1 << ABS_POS_BITS;
Seth Forshee824efd32012-09-28 10:29:21 -0700713 else if (hw->y == Y_MAX_POSITIVE)
714 hw->y = YMAX;
Seth Forsheec03945062012-07-24 23:54:11 -0700715
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100716 return 0;
717}
718
Daniel Kurtzbea9f0f2011-07-06 22:42:52 -0700719static void synaptics_report_semi_mt_slot(struct input_dev *dev, int slot,
720 bool active, int x, int y)
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100721{
722 input_mt_slot(dev, slot);
723 input_mt_report_slot_state(dev, MT_TOOL_FINGER, active);
724 if (active) {
725 input_report_abs(dev, ABS_MT_POSITION_X, x);
Daniel Kurtz6de58dd2011-08-23 23:00:24 -0700726 input_report_abs(dev, ABS_MT_POSITION_Y, synaptics_invert_y(y));
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100727 }
728}
729
730static void synaptics_report_semi_mt_data(struct input_dev *dev,
731 const struct synaptics_hw_state *a,
732 const struct synaptics_hw_state *b,
733 int num_fingers)
734{
735 if (num_fingers >= 2) {
Daniel Kurtzbea9f0f2011-07-06 22:42:52 -0700736 synaptics_report_semi_mt_slot(dev, 0, true, min(a->x, b->x),
737 min(a->y, b->y));
738 synaptics_report_semi_mt_slot(dev, 1, true, max(a->x, b->x),
739 max(a->y, b->y));
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100740 } else if (num_fingers == 1) {
Daniel Kurtzbea9f0f2011-07-06 22:42:52 -0700741 synaptics_report_semi_mt_slot(dev, 0, true, a->x, a->y);
742 synaptics_report_semi_mt_slot(dev, 1, false, 0, 0);
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100743 } else {
Daniel Kurtzbea9f0f2011-07-06 22:42:52 -0700744 synaptics_report_semi_mt_slot(dev, 0, false, 0, 0);
745 synaptics_report_semi_mt_slot(dev, 1, false, 0, 0);
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747}
748
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700749static void synaptics_report_buttons(struct psmouse *psmouse,
750 const struct synaptics_hw_state *hw)
751{
752 struct input_dev *dev = psmouse->dev;
753 struct synaptics_data *priv = psmouse->private;
754 int i;
755
756 input_report_key(dev, BTN_LEFT, hw->left);
757 input_report_key(dev, BTN_RIGHT, hw->right);
758
759 if (SYN_CAP_MIDDLE_BUTTON(priv->capabilities))
760 input_report_key(dev, BTN_MIDDLE, hw->middle);
761
762 if (SYN_CAP_FOUR_BUTTON(priv->capabilities)) {
763 input_report_key(dev, BTN_FORWARD, hw->up);
764 input_report_key(dev, BTN_BACK, hw->down);
765 }
766
767 for (i = 0; i < SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap); i++)
768 input_report_key(dev, BTN_0 + i, hw->ext_buttons & (1 << i));
769}
770
771static void synaptics_report_slot(struct input_dev *dev, int slot,
772 const struct synaptics_hw_state *hw)
773{
774 input_mt_slot(dev, slot);
775 input_mt_report_slot_state(dev, MT_TOOL_FINGER, (hw != NULL));
776 if (!hw)
777 return;
778
779 input_report_abs(dev, ABS_MT_POSITION_X, hw->x);
780 input_report_abs(dev, ABS_MT_POSITION_Y, synaptics_invert_y(hw->y));
781 input_report_abs(dev, ABS_MT_PRESSURE, hw->z);
782}
783
784static void synaptics_report_mt_data(struct psmouse *psmouse,
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700785 struct synaptics_mt_state *mt_state,
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700786 const struct synaptics_hw_state *sgm)
787{
788 struct input_dev *dev = psmouse->dev;
789 struct synaptics_data *priv = psmouse->private;
790 struct synaptics_hw_state *agm = &priv->agm;
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700791 struct synaptics_mt_state *old = &priv->mt_state;
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700792
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700793 switch (mt_state->count) {
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700794 case 0:
795 synaptics_report_slot(dev, 0, NULL);
796 synaptics_report_slot(dev, 1, NULL);
797 break;
798 case 1:
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700799 if (mt_state->sgm == -1) {
800 synaptics_report_slot(dev, 0, NULL);
801 synaptics_report_slot(dev, 1, NULL);
802 } else if (mt_state->sgm == 0) {
803 synaptics_report_slot(dev, 0, sgm);
804 synaptics_report_slot(dev, 1, NULL);
805 } else {
806 synaptics_report_slot(dev, 0, NULL);
807 synaptics_report_slot(dev, 1, sgm);
808 }
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700809 break;
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700810 default:
811 /*
812 * If the finger slot contained in SGM is valid, and either
Daniel Kurtz48064bd2013-02-13 13:53:07 -0800813 * hasn't changed, or is new, or the old SGM has now moved to
814 * AGM, then report SGM in MTB slot 0.
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700815 * Otherwise, empty MTB slot 0.
816 */
817 if (mt_state->sgm != -1 &&
Daniel Kurtz48064bd2013-02-13 13:53:07 -0800818 (mt_state->sgm == old->sgm ||
819 old->sgm == -1 || mt_state->agm == old->sgm))
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700820 synaptics_report_slot(dev, 0, sgm);
821 else
822 synaptics_report_slot(dev, 0, NULL);
823
824 /*
825 * If the finger slot contained in AGM is valid, and either
826 * hasn't changed, or is new, then report AGM in MTB slot 1.
827 * Otherwise, empty MTB slot 1.
Daniel Kurtz48064bd2013-02-13 13:53:07 -0800828 *
829 * However, in the case where the AGM is new, make sure that
830 * that it is either the same as the old SGM, or there was no
831 * SGM.
832 *
833 * Otherwise, if the SGM was just 1, and the new AGM is 2, then
834 * the new AGM will keep the old SGM's tracking ID, which can
835 * cause apparent drumroll. This happens if in the following
836 * valid finger sequence:
837 *
838 * Action SGM AGM (MTB slot:Contact)
839 * 1. Touch contact 0 (0:0)
840 * 2. Touch contact 1 (0:0, 1:1)
841 * 3. Lift contact 0 (1:1)
842 * 4. Touch contacts 2,3 (0:2, 1:3)
843 *
844 * In step 4, contact 3, in AGM must not be given the same
845 * tracking ID as contact 1 had in step 3. To avoid this,
846 * the first agm with contact 3 is dropped and slot 1 is
847 * invalidated (tracking ID = -1).
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700848 */
849 if (mt_state->agm != -1 &&
Daniel Kurtz48064bd2013-02-13 13:53:07 -0800850 (mt_state->agm == old->agm ||
851 (old->agm == -1 &&
852 (old->sgm == -1 || mt_state->agm == old->sgm))))
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700853 synaptics_report_slot(dev, 1, agm);
854 else
855 synaptics_report_slot(dev, 1, NULL);
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700856 break;
857 }
858
859 /* Don't use active slot count to generate BTN_TOOL events. */
860 input_mt_report_pointer_emulation(dev, false);
861
862 /* Send the number of fingers reported by touchpad itself. */
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700863 input_mt_report_finger_count(dev, mt_state->count);
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700864
865 synaptics_report_buttons(psmouse, sgm);
866
867 input_sync(dev);
868}
869
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700870/* Handle case where mt_state->count = 0 */
871static void synaptics_image_sensor_0f(struct synaptics_data *priv,
872 struct synaptics_mt_state *mt_state)
873{
874 synaptics_mt_state_set(mt_state, 0, -1, -1);
875 priv->mt_state_lost = false;
876}
877
878/* Handle case where mt_state->count = 1 */
879static void synaptics_image_sensor_1f(struct synaptics_data *priv,
880 struct synaptics_mt_state *mt_state)
881{
882 struct synaptics_hw_state *agm = &priv->agm;
883 struct synaptics_mt_state *old = &priv->mt_state;
884
885 /*
886 * If the last AGM was (0,0,0), and there is only one finger left,
887 * then we absolutely know that SGM contains slot 0, and all other
888 * fingers have been removed.
889 */
890 if (priv->agm_pending && agm->z == 0) {
891 synaptics_mt_state_set(mt_state, 1, 0, -1);
892 priv->mt_state_lost = false;
893 return;
894 }
895
896 switch (old->count) {
897 case 0:
898 synaptics_mt_state_set(mt_state, 1, 0, -1);
899 break;
900 case 1:
901 /*
902 * If mt_state_lost, then the previous transition was 3->1,
903 * and SGM now contains either slot 0 or 1, but we don't know
904 * which. So, we just assume that the SGM now contains slot 1.
905 *
906 * If pending AGM and either:
907 * (a) the previous SGM slot contains slot 0, or
908 * (b) there was no SGM slot
909 * then, the SGM now contains slot 1
910 *
911 * Case (a) happens with very rapid "drum roll" gestures, where
912 * slot 0 finger is lifted and a new slot 1 finger touches
913 * within one reporting interval.
914 *
915 * Case (b) happens if initially two or more fingers tap
916 * briefly, and all but one lift before the end of the first
917 * reporting interval.
918 *
919 * (In both these cases, slot 0 will becomes empty, so SGM
920 * contains slot 1 with the new finger)
921 *
922 * Else, if there was no previous SGM, it now contains slot 0.
923 *
924 * Otherwise, SGM still contains the same slot.
925 */
926 if (priv->mt_state_lost ||
927 (priv->agm_pending && old->sgm <= 0))
928 synaptics_mt_state_set(mt_state, 1, 1, -1);
929 else if (old->sgm == -1)
930 synaptics_mt_state_set(mt_state, 1, 0, -1);
931 break;
932 case 2:
933 /*
934 * If mt_state_lost, we don't know which finger SGM contains.
935 *
936 * So, report 1 finger, but with both slots empty.
937 * We will use slot 1 on subsequent 1->1
938 */
939 if (priv->mt_state_lost) {
940 synaptics_mt_state_set(mt_state, 1, -1, -1);
941 break;
942 }
943 /*
944 * Since the last AGM was NOT (0,0,0), it was the finger in
945 * slot 0 that has been removed.
946 * So, SGM now contains previous AGM's slot, and AGM is now
947 * empty.
948 */
949 synaptics_mt_state_set(mt_state, 1, old->agm, -1);
950 break;
951 case 3:
952 /*
953 * Since last AGM was not (0,0,0), we don't know which finger
954 * is left.
955 *
956 * So, report 1 finger, but with both slots empty.
957 * We will use slot 1 on subsequent 1->1
958 */
959 synaptics_mt_state_set(mt_state, 1, -1, -1);
960 priv->mt_state_lost = true;
961 break;
Daniel Kurtz6b4b49f2011-08-23 23:02:56 -0700962 case 4:
963 case 5:
964 /* mt_state was updated by AGM-CONTACT packet */
965 break;
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700966 }
967}
968
969/* Handle case where mt_state->count = 2 */
970static void synaptics_image_sensor_2f(struct synaptics_data *priv,
971 struct synaptics_mt_state *mt_state)
972{
973 struct synaptics_mt_state *old = &priv->mt_state;
974
975 switch (old->count) {
976 case 0:
977 synaptics_mt_state_set(mt_state, 2, 0, 1);
978 break;
979 case 1:
980 /*
981 * If previous SGM contained slot 1 or higher, SGM now contains
982 * slot 0 (the newly touching finger) and AGM contains SGM's
983 * previous slot.
984 *
985 * Otherwise, SGM still contains slot 0 and AGM now contains
986 * slot 1.
987 */
988 if (old->sgm >= 1)
989 synaptics_mt_state_set(mt_state, 2, 0, old->sgm);
990 else
991 synaptics_mt_state_set(mt_state, 2, 0, 1);
992 break;
993 case 2:
994 /*
995 * If mt_state_lost, SGM now contains either finger 1 or 2, but
996 * we don't know which.
997 * So, we just assume that the SGM contains slot 0 and AGM 1.
998 */
999 if (priv->mt_state_lost)
1000 synaptics_mt_state_set(mt_state, 2, 0, 1);
1001 /*
1002 * Otherwise, use the same mt_state, since it either hasn't
1003 * changed, or was updated by a recently received AGM-CONTACT
1004 * packet.
1005 */
1006 break;
1007 case 3:
1008 /*
1009 * 3->2 transitions have two unsolvable problems:
1010 * 1) no indication is given which finger was removed
1011 * 2) no way to tell if agm packet was for finger 3
1012 * before 3->2, or finger 2 after 3->2.
1013 *
1014 * So, report 2 fingers, but empty all slots.
1015 * We will guess slots [0,1] on subsequent 2->2.
1016 */
1017 synaptics_mt_state_set(mt_state, 2, -1, -1);
1018 priv->mt_state_lost = true;
1019 break;
Daniel Kurtz6b4b49f2011-08-23 23:02:56 -07001020 case 4:
1021 case 5:
1022 /* mt_state was updated by AGM-CONTACT packet */
1023 break;
Daniel Kurtz4dc772d2011-08-23 23:02:40 -07001024 }
1025}
1026
1027/* Handle case where mt_state->count = 3 */
1028static void synaptics_image_sensor_3f(struct synaptics_data *priv,
1029 struct synaptics_mt_state *mt_state)
1030{
1031 struct synaptics_mt_state *old = &priv->mt_state;
1032
1033 switch (old->count) {
1034 case 0:
1035 synaptics_mt_state_set(mt_state, 3, 0, 2);
1036 break;
1037 case 1:
1038 /*
1039 * If previous SGM contained slot 2 or higher, SGM now contains
1040 * slot 0 (one of the newly touching fingers) and AGM contains
1041 * SGM's previous slot.
1042 *
1043 * Otherwise, SGM now contains slot 0 and AGM contains slot 2.
1044 */
1045 if (old->sgm >= 2)
1046 synaptics_mt_state_set(mt_state, 3, 0, old->sgm);
1047 else
1048 synaptics_mt_state_set(mt_state, 3, 0, 2);
1049 break;
1050 case 2:
1051 /*
Daniel Kurtz6b4b49f2011-08-23 23:02:56 -07001052 * If the AGM previously contained slot 3 or higher, then the
1053 * newly touching finger is in the lowest available slot.
1054 *
1055 * If SGM was previously 1 or higher, then the new SGM is
1056 * now slot 0 (with a new finger), otherwise, the new finger
1057 * is now in a hidden slot between 0 and AGM's slot.
1058 *
1059 * In all such cases, the SGM now contains slot 0, and the AGM
1060 * continues to contain the same slot as before.
1061 */
1062 if (old->agm >= 3) {
1063 synaptics_mt_state_set(mt_state, 3, 0, old->agm);
1064 break;
1065 }
1066
1067 /*
Daniel Kurtz4dc772d2011-08-23 23:02:40 -07001068 * After some 3->1 and all 3->2 transitions, we lose track
1069 * of which slot is reported by SGM and AGM.
1070 *
1071 * For 2->3 in this state, report 3 fingers, but empty all
1072 * slots, and we will guess (0,2) on a subsequent 0->3.
1073 *
1074 * To userspace, the resulting transition will look like:
1075 * 2:[0,1] -> 3:[-1,-1] -> 3:[0,2]
1076 */
1077 if (priv->mt_state_lost) {
1078 synaptics_mt_state_set(mt_state, 3, -1, -1);
1079 break;
1080 }
1081
1082 /*
1083 * If the (SGM,AGM) really previously contained slots (0, 1),
1084 * then we cannot know what slot was just reported by the AGM,
1085 * because the 2->3 transition can occur either before or after
1086 * the AGM packet. Thus, this most recent AGM could contain
1087 * either the same old slot 1 or the new slot 2.
1088 * Subsequent AGMs will be reporting slot 2.
1089 *
1090 * To userspace, the resulting transition will look like:
1091 * 2:[0,1] -> 3:[0,-1] -> 3:[0,2]
1092 */
1093 synaptics_mt_state_set(mt_state, 3, 0, -1);
1094 break;
1095 case 3:
1096 /*
1097 * If, for whatever reason, the previous agm was invalid,
1098 * Assume SGM now contains slot 0, AGM now contains slot 2.
1099 */
1100 if (old->agm <= 2)
1101 synaptics_mt_state_set(mt_state, 3, 0, 2);
1102 /*
1103 * mt_state either hasn't changed, or was updated by a recently
1104 * received AGM-CONTACT packet.
1105 */
1106 break;
Daniel Kurtz6b4b49f2011-08-23 23:02:56 -07001107
1108 case 4:
1109 case 5:
1110 /* mt_state was updated by AGM-CONTACT packet */
1111 break;
Daniel Kurtz4dc772d2011-08-23 23:02:40 -07001112 }
1113}
1114
Daniel Kurtz6b4b49f2011-08-23 23:02:56 -07001115/* Handle case where mt_state->count = 4, or = 5 */
1116static void synaptics_image_sensor_45f(struct synaptics_data *priv,
1117 struct synaptics_mt_state *mt_state)
1118{
1119 /* mt_state was updated correctly by AGM-CONTACT packet */
1120 priv->mt_state_lost = false;
1121}
1122
Daniel Kurtz3cdfee92011-08-23 23:02:25 -07001123static void synaptics_image_sensor_process(struct psmouse *psmouse,
1124 struct synaptics_hw_state *sgm)
1125{
Daniel Kurtz4dc772d2011-08-23 23:02:40 -07001126 struct synaptics_data *priv = psmouse->private;
1127 struct synaptics_hw_state *agm = &priv->agm;
1128 struct synaptics_mt_state mt_state;
Daniel Kurtz3cdfee92011-08-23 23:02:25 -07001129
Daniel Kurtz4dc772d2011-08-23 23:02:40 -07001130 /* Initialize using current mt_state (as updated by last agm) */
1131 mt_state = agm->mt_state;
1132
1133 /*
1134 * Update mt_state using the new finger count and current mt_state.
1135 */
Daniel Kurtz3cdfee92011-08-23 23:02:25 -07001136 if (sgm->z == 0)
Daniel Kurtz4dc772d2011-08-23 23:02:40 -07001137 synaptics_image_sensor_0f(priv, &mt_state);
Daniel Kurtz3cdfee92011-08-23 23:02:25 -07001138 else if (sgm->w >= 4)
Daniel Kurtz4dc772d2011-08-23 23:02:40 -07001139 synaptics_image_sensor_1f(priv, &mt_state);
Daniel Kurtz3cdfee92011-08-23 23:02:25 -07001140 else if (sgm->w == 0)
Daniel Kurtz4dc772d2011-08-23 23:02:40 -07001141 synaptics_image_sensor_2f(priv, &mt_state);
Daniel Kurtz6b4b49f2011-08-23 23:02:56 -07001142 else if (sgm->w == 1 && mt_state.count <= 3)
Daniel Kurtz4dc772d2011-08-23 23:02:40 -07001143 synaptics_image_sensor_3f(priv, &mt_state);
Daniel Kurtz6b4b49f2011-08-23 23:02:56 -07001144 else
1145 synaptics_image_sensor_45f(priv, &mt_state);
Daniel Kurtz3cdfee92011-08-23 23:02:25 -07001146
1147 /* Send resulting input events to user space */
Daniel Kurtz4dc772d2011-08-23 23:02:40 -07001148 synaptics_report_mt_data(psmouse, &mt_state, sgm);
1149
1150 /* Store updated mt_state */
1151 priv->mt_state = agm->mt_state = mt_state;
1152 priv->agm_pending = false;
Daniel Kurtz3cdfee92011-08-23 23:02:25 -07001153}
1154
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155/*
1156 * called for each full received packet from the touchpad
1157 */
1158static void synaptics_process_packet(struct psmouse *psmouse)
1159{
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001160 struct input_dev *dev = psmouse->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 struct synaptics_data *priv = psmouse->private;
1162 struct synaptics_hw_state hw;
1163 int num_fingers;
1164 int finger_width;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Henrik Rydbergfec6e522010-12-21 18:11:25 +01001166 if (synaptics_parse_hw_state(psmouse->packet, priv, &hw))
1167 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
Daniel Kurtz3cdfee92011-08-23 23:02:25 -07001169 if (SYN_CAP_IMAGE_SENSOR(priv->ext_cap_0c)) {
1170 synaptics_image_sensor_process(psmouse, &hw);
1171 return;
1172 }
1173
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 if (hw.scroll) {
1175 priv->scroll += hw.scroll;
1176
1177 while (priv->scroll >= 4) {
1178 input_report_key(dev, BTN_BACK, !hw.down);
1179 input_sync(dev);
1180 input_report_key(dev, BTN_BACK, hw.down);
1181 input_sync(dev);
1182 priv->scroll -= 4;
1183 }
1184 while (priv->scroll <= -4) {
1185 input_report_key(dev, BTN_FORWARD, !hw.up);
1186 input_sync(dev);
1187 input_report_key(dev, BTN_FORWARD, hw.up);
1188 input_sync(dev);
1189 priv->scroll += 4;
1190 }
1191 return;
1192 }
1193
Henrik Rydberg4f56ce92010-12-18 15:42:30 +01001194 if (hw.z > 0 && hw.x > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 num_fingers = 1;
1196 finger_width = 5;
1197 if (SYN_CAP_EXTENDED(priv->capabilities)) {
1198 switch (hw.w) {
1199 case 0 ... 1:
1200 if (SYN_CAP_MULTIFINGER(priv->capabilities))
1201 num_fingers = hw.w + 2;
1202 break;
1203 case 2:
1204 if (SYN_MODEL_PEN(priv->model_id))
1205 ; /* Nothing, treat a pen as a single finger */
1206 break;
1207 case 4 ... 15:
1208 if (SYN_CAP_PALMDETECT(priv->capabilities))
1209 finger_width = hw.w;
1210 break;
1211 }
1212 }
1213 } else {
1214 num_fingers = 0;
1215 finger_width = 0;
1216 }
1217
Henrik Rydbergfec6e522010-12-21 18:11:25 +01001218 if (SYN_CAP_ADV_GESTURE(priv->ext_cap_0c))
Daniel Kurtz7afdb842011-08-23 23:00:33 -07001219 synaptics_report_semi_mt_data(dev, &hw, &priv->agm,
1220 num_fingers);
Henrik Rydbergfec6e522010-12-21 18:11:25 +01001221
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 /* Post events
1223 * BTN_TOUCH has to be first as mousedev relies on it when doing
1224 * absolute -> relative conversion
1225 */
1226 if (hw.z > 30) input_report_key(dev, BTN_TOUCH, 1);
1227 if (hw.z < 25) input_report_key(dev, BTN_TOUCH, 0);
1228
Henrik Rydberg4f56ce92010-12-18 15:42:30 +01001229 if (num_fingers > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 input_report_abs(dev, ABS_X, hw.x);
Daniel Kurtz6de58dd2011-08-23 23:00:24 -07001231 input_report_abs(dev, ABS_Y, synaptics_invert_y(hw.y));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 }
1233 input_report_abs(dev, ABS_PRESSURE, hw.z);
1234
Chris Bagwell2a8e7712010-07-19 09:06:15 -07001235 if (SYN_CAP_PALMDETECT(priv->capabilities))
1236 input_report_abs(dev, ABS_TOOL_WIDTH, finger_width);
1237
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 input_report_key(dev, BTN_TOOL_FINGER, num_fingers == 1);
Peter Hutterere42b6642008-11-20 15:24:42 -05001239 if (SYN_CAP_MULTIFINGER(priv->capabilities)) {
1240 input_report_key(dev, BTN_TOOL_DOUBLETAP, num_fingers == 2);
1241 input_report_key(dev, BTN_TOOL_TRIPLETAP, num_fingers == 3);
1242 }
1243
Daniel Kurtz3cdfee92011-08-23 23:02:25 -07001244 synaptics_report_buttons(psmouse, &hw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
1246 input_sync(dev);
1247}
1248
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001249static int synaptics_validate_byte(struct psmouse *psmouse,
1250 int idx, unsigned char pkt_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251{
Helge Dellere38de672006-09-10 21:54:39 -04001252 static const unsigned char newabs_mask[] = { 0xC8, 0x00, 0x00, 0xC8, 0x00 };
1253 static const unsigned char newabs_rel_mask[] = { 0xC0, 0x00, 0x00, 0xC0, 0x00 };
1254 static const unsigned char newabs_rslt[] = { 0x80, 0x00, 0x00, 0xC0, 0x00 };
1255 static const unsigned char oldabs_mask[] = { 0xC0, 0x60, 0x00, 0xC0, 0x60 };
1256 static const unsigned char oldabs_rslt[] = { 0xC0, 0x00, 0x00, 0x80, 0x00 };
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001257 const char *packet = psmouse->packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
1259 if (idx < 0 || idx > 4)
1260 return 0;
1261
1262 switch (pkt_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Dmitry Torokhova62f0d22010-05-19 10:39:17 -07001264 case SYN_NEWABS:
1265 case SYN_NEWABS_RELAXED:
1266 return (packet[idx] & newabs_rel_mask[idx]) == newabs_rslt[idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Dmitry Torokhova62f0d22010-05-19 10:39:17 -07001268 case SYN_NEWABS_STRICT:
1269 return (packet[idx] & newabs_mask[idx]) == newabs_rslt[idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
Dmitry Torokhova62f0d22010-05-19 10:39:17 -07001271 case SYN_OLDABS:
1272 return (packet[idx] & oldabs_mask[idx]) == oldabs_rslt[idx];
1273
1274 default:
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001275 psmouse_err(psmouse, "unknown packet type %d\n", pkt_type);
Dmitry Torokhova62f0d22010-05-19 10:39:17 -07001276 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 }
1278}
1279
1280static unsigned char synaptics_detect_pkt_type(struct psmouse *psmouse)
1281{
1282 int i;
1283
1284 for (i = 0; i < 5; i++)
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001285 if (!synaptics_validate_byte(psmouse, i, SYN_NEWABS_STRICT)) {
1286 psmouse_info(psmouse, "using relaxed packet validation\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 return SYN_NEWABS_RELAXED;
1288 }
1289
1290 return SYN_NEWABS_STRICT;
1291}
1292
David Howells7d12e782006-10-05 14:55:46 +01001293static psmouse_ret_t synaptics_process_byte(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 struct synaptics_data *priv = psmouse->private;
1296
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 if (psmouse->pktcnt >= 6) { /* Full packet received */
1298 if (unlikely(priv->pkt_type == SYN_NEWABS))
1299 priv->pkt_type = synaptics_detect_pkt_type(psmouse);
1300
Dmitry Torokhova8b3c0f2010-10-04 21:46:10 -07001301 if (SYN_CAP_PASS_THROUGH(priv->capabilities) &&
1302 synaptics_is_pt_packet(psmouse->packet)) {
1303 if (priv->pt_port)
1304 synaptics_pass_pt_packet(priv->pt_port, psmouse->packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 } else
1306 synaptics_process_packet(psmouse);
1307
1308 return PSMOUSE_FULL_PACKET;
1309 }
1310
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001311 return synaptics_validate_byte(psmouse, psmouse->pktcnt - 1, priv->pkt_type) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 PSMOUSE_GOOD_DATA : PSMOUSE_BAD_DATA;
1313}
1314
1315/*****************************************************************************
1316 * Driver initialization/cleanup functions
1317 ****************************************************************************/
Daniel Kurtz85615472011-08-23 23:00:41 -07001318static void set_abs_position_params(struct input_dev *dev,
1319 struct synaptics_data *priv, int x_code,
1320 int y_code)
1321{
1322 int x_min = priv->x_min ?: XMIN_NOMINAL;
1323 int x_max = priv->x_max ?: XMAX_NOMINAL;
1324 int y_min = priv->y_min ?: YMIN_NOMINAL;
1325 int y_max = priv->y_max ?: YMAX_NOMINAL;
1326 int fuzz = SYN_CAP_REDUCED_FILTERING(priv->ext_cap_0c) ?
1327 SYN_REDUCED_FILTER_FUZZ : 0;
1328
1329 input_set_abs_params(dev, x_code, x_min, x_max, fuzz, 0);
1330 input_set_abs_params(dev, y_code, y_min, y_max, fuzz, 0);
1331 input_abs_set_res(dev, x_code, priv->x_res);
1332 input_abs_set_res(dev, y_code, priv->y_res);
1333}
1334
Hans de Goede43e19882014-04-19 22:26:41 -07001335static void set_input_params(struct psmouse *psmouse,
1336 struct synaptics_data *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337{
Hans de Goede43e19882014-04-19 22:26:41 -07001338 struct input_dev *dev = psmouse->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 int i;
1340
Daniel Drake7968a5d2011-11-08 00:00:35 -08001341 /* Things that apply to both modes */
Henrik Rydbergc14890a2010-12-16 09:52:23 +01001342 __set_bit(INPUT_PROP_POINTER, dev->propbit);
Daniel Drake7968a5d2011-11-08 00:00:35 -08001343 __set_bit(EV_KEY, dev->evbit);
1344 __set_bit(BTN_LEFT, dev->keybit);
1345 __set_bit(BTN_RIGHT, dev->keybit);
Henrik Rydbergc14890a2010-12-16 09:52:23 +01001346
Daniel Drake7968a5d2011-11-08 00:00:35 -08001347 if (SYN_CAP_MIDDLE_BUTTON(priv->capabilities))
1348 __set_bit(BTN_MIDDLE, dev->keybit);
1349
1350 if (!priv->absolute_mode) {
1351 /* Relative mode */
1352 __set_bit(EV_REL, dev->evbit);
1353 __set_bit(REL_X, dev->relbit);
1354 __set_bit(REL_Y, dev->relbit);
1355 return;
1356 }
1357
1358 /* Absolute mode */
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001359 __set_bit(EV_ABS, dev->evbit);
Daniel Kurtz85615472011-08-23 23:00:41 -07001360 set_abs_position_params(dev, priv, ABS_X, ABS_Y);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 input_set_abs_params(dev, ABS_PRESSURE, 0, 255, 0, 0);
Chris Bagwell2a8e7712010-07-19 09:06:15 -07001362
Daniel Kurtz3cdfee92011-08-23 23:02:25 -07001363 if (SYN_CAP_IMAGE_SENSOR(priv->ext_cap_0c)) {
Daniel Kurtz3cdfee92011-08-23 23:02:25 -07001364 set_abs_position_params(dev, priv, ABS_MT_POSITION_X,
1365 ABS_MT_POSITION_Y);
1366 /* Image sensors can report per-contact pressure */
1367 input_set_abs_params(dev, ABS_MT_PRESSURE, 0, 255, 0, 0);
Henrik Rydberg0b85bf72013-02-15 17:04:03 -08001368 input_mt_init_slots(dev, 2, INPUT_MT_POINTER);
Daniel Kurtz6b4b49f2011-08-23 23:02:56 -07001369
1370 /* Image sensors can signal 4 and 5 finger clicks */
1371 __set_bit(BTN_TOOL_QUADTAP, dev->keybit);
1372 __set_bit(BTN_TOOL_QUINTTAP, dev->keybit);
Daniel Kurtz3cdfee92011-08-23 23:02:25 -07001373 } else if (SYN_CAP_ADV_GESTURE(priv->ext_cap_0c)) {
1374 /* Non-image sensors with AGM use semi-mt */
Henrik Rydbergfec6e522010-12-21 18:11:25 +01001375 __set_bit(INPUT_PROP_SEMI_MT, dev->propbit);
Henrik Rydbergb4adbbe2012-08-11 22:07:55 +02001376 input_mt_init_slots(dev, 2, 0);
Daniel Kurtz85615472011-08-23 23:00:41 -07001377 set_abs_position_params(dev, priv, ABS_MT_POSITION_X,
1378 ABS_MT_POSITION_Y);
Henrik Rydbergfec6e522010-12-21 18:11:25 +01001379 }
1380
Chris Bagwell2a8e7712010-07-19 09:06:15 -07001381 if (SYN_CAP_PALMDETECT(priv->capabilities))
Chris Bagwell58fb0212010-07-19 09:06:15 -07001382 input_set_abs_params(dev, ABS_TOOL_WIDTH, 0, 15, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001384 __set_bit(BTN_TOUCH, dev->keybit);
1385 __set_bit(BTN_TOOL_FINGER, dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Peter Hutterere42b6642008-11-20 15:24:42 -05001387 if (SYN_CAP_MULTIFINGER(priv->capabilities)) {
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001388 __set_bit(BTN_TOOL_DOUBLETAP, dev->keybit);
1389 __set_bit(BTN_TOOL_TRIPLETAP, dev->keybit);
Peter Hutterere42b6642008-11-20 15:24:42 -05001390 }
1391
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 if (SYN_CAP_FOUR_BUTTON(priv->capabilities) ||
1393 SYN_CAP_MIDDLE_BUTTON(priv->capabilities)) {
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001394 __set_bit(BTN_FORWARD, dev->keybit);
1395 __set_bit(BTN_BACK, dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 }
1397
1398 for (i = 0; i < SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap); i++)
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001399 __set_bit(BTN_0 + i, dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001401 __clear_bit(EV_REL, dev->evbit);
1402 __clear_bit(REL_X, dev->relbit);
1403 __clear_bit(REL_Y, dev->relbit);
Tero Saarniec20a022009-06-10 23:27:24 -07001404
Takashi Iwai5f57d672010-04-19 10:37:21 -07001405 if (SYN_CAP_CLICKPAD(priv->ext_cap_0c)) {
Henrik Rydbergc14890a2010-12-16 09:52:23 +01001406 __set_bit(INPUT_PROP_BUTTONPAD, dev->propbit);
Hans de Goedee2f61102014-05-19 22:53:23 -07001407 if (matches_pnp_id(psmouse, topbuttonpad_pnp_ids))
1408 __set_bit(INPUT_PROP_TOPBUTTONPAD, dev->propbit);
Takashi Iwai5f57d672010-04-19 10:37:21 -07001409 /* Clickpads report only left button */
1410 __clear_bit(BTN_RIGHT, dev->keybit);
1411 __clear_bit(BTN_MIDDLE, dev->keybit);
1412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413}
1414
Daniel Drake7968a5d2011-11-08 00:00:35 -08001415static ssize_t synaptics_show_disable_gesture(struct psmouse *psmouse,
1416 void *data, char *buf)
1417{
1418 struct synaptics_data *priv = psmouse->private;
1419
1420 return sprintf(buf, "%c\n", priv->disable_gesture ? '1' : '0');
1421}
1422
1423static ssize_t synaptics_set_disable_gesture(struct psmouse *psmouse,
1424 void *data, const char *buf,
1425 size_t len)
1426{
1427 struct synaptics_data *priv = psmouse->private;
1428 unsigned int value;
1429 int err;
1430
1431 err = kstrtouint(buf, 10, &value);
1432 if (err)
1433 return err;
1434
1435 if (value > 1)
1436 return -EINVAL;
1437
1438 if (value == priv->disable_gesture)
1439 return len;
1440
1441 priv->disable_gesture = value;
1442 if (value)
1443 priv->mode |= SYN_BIT_DISABLE_GESTURE;
1444 else
1445 priv->mode &= ~SYN_BIT_DISABLE_GESTURE;
1446
1447 if (synaptics_mode_cmd(psmouse, priv->mode))
1448 return -EIO;
1449
1450 return len;
1451}
1452
1453PSMOUSE_DEFINE_ATTR(disable_gesture, S_IWUSR | S_IRUGO, NULL,
1454 synaptics_show_disable_gesture,
1455 synaptics_set_disable_gesture);
1456
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457static void synaptics_disconnect(struct psmouse *psmouse)
1458{
Daniel Drake7968a5d2011-11-08 00:00:35 -08001459 struct synaptics_data *priv = psmouse->private;
1460
1461 if (!priv->absolute_mode && SYN_ID_DISGEST_SUPPORTED(priv->identity))
1462 device_remove_file(&psmouse->ps2dev.serio->dev,
1463 &psmouse_attr_disable_gesture.dattr);
1464
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 synaptics_reset(psmouse);
Daniel Drake7968a5d2011-11-08 00:00:35 -08001466 kfree(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 psmouse->private = NULL;
1468}
1469
1470static int synaptics_reconnect(struct psmouse *psmouse)
1471{
1472 struct synaptics_data *priv = psmouse->private;
1473 struct synaptics_data old_priv = *priv;
Eric Miaoeeb06552013-06-04 09:30:55 -07001474 unsigned char param[2];
Alexandre Peixoto Ferreirac63fe0a2011-01-28 22:05:14 -08001475 int retry = 0;
1476 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Alexandre Peixoto Ferreirac63fe0a2011-01-28 22:05:14 -08001478 do {
1479 psmouse_reset(psmouse);
Dmitry Torokhov85214782011-12-12 00:05:53 -08001480 if (retry) {
1481 /*
1482 * On some boxes, right after resuming, the touchpad
1483 * needs some time to finish initializing (I assume
1484 * it needs time to calibrate) and start responding
1485 * to Synaptics-specific queries, so let's wait a
1486 * bit.
1487 */
1488 ssleep(1);
1489 }
Eric Miaoeeb06552013-06-04 09:30:55 -07001490 ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_GETID);
Alexandre Peixoto Ferreirac63fe0a2011-01-28 22:05:14 -08001491 error = synaptics_detect(psmouse, 0);
1492 } while (error && ++retry < 3);
Andy Whitcroft4d368452009-02-28 12:51:01 -08001493
Alexandre Peixoto Ferreirac63fe0a2011-01-28 22:05:14 -08001494 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 return -1;
1496
Alexandre Peixoto Ferreirac63fe0a2011-01-28 22:05:14 -08001497 if (retry > 1)
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001498 psmouse_dbg(psmouse, "reconnected after %d tries\n", retry);
Alexandre Peixoto Ferreirac63fe0a2011-01-28 22:05:14 -08001499
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 if (synaptics_query_hardware(psmouse)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001501 psmouse_err(psmouse, "Unable to query device.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 return -1;
1503 }
1504
Daniel Drake7968a5d2011-11-08 00:00:35 -08001505 if (synaptics_set_mode(psmouse)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001506 psmouse_err(psmouse, "Unable to initialize device.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 return -1;
1508 }
1509
Alexandre Peixoto Ferreirabaddf582011-01-28 22:05:14 -08001510 if (old_priv.identity != priv->identity ||
1511 old_priv.model_id != priv->model_id ||
1512 old_priv.capabilities != priv->capabilities ||
1513 old_priv.ext_cap != priv->ext_cap) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001514 psmouse_err(psmouse,
1515 "hardware appears to be different: id(%ld-%ld), model(%ld-%ld), caps(%lx-%lx), ext(%lx-%lx).\n",
1516 old_priv.identity, priv->identity,
1517 old_priv.model_id, priv->model_id,
1518 old_priv.capabilities, priv->capabilities,
1519 old_priv.ext_cap, priv->ext_cap);
Alexandre Peixoto Ferreirabaddf582011-01-28 22:05:14 -08001520 return -1;
1521 }
1522
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 return 0;
1524}
1525
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001526static bool impaired_toshiba_kbc;
1527
Sachin Kamatc9631562013-08-12 11:05:58 -07001528static const struct dmi_system_id toshiba_dmi_table[] __initconst = {
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001529#if defined(CONFIG_DMI) && defined(CONFIG_X86)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 {
Dmitry Torokhov9961e252009-12-04 10:24:20 -08001531 /* Toshiba Satellite */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 .matches = {
1533 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
Richard Thrippleton53a26702006-04-02 00:10:18 -05001534 DMI_MATCH(DMI_PRODUCT_NAME, "Satellite"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 },
1536 },
Simon Horman9ba5eaa2005-07-11 01:07:20 -05001537 {
Dmitry Torokhov9961e252009-12-04 10:24:20 -08001538 /* Toshiba Dynabook */
Simon Horman9ba5eaa2005-07-11 01:07:20 -05001539 .matches = {
1540 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
Richard Thrippleton53a26702006-04-02 00:10:18 -05001541 DMI_MATCH(DMI_PRODUCT_NAME, "dynabook"),
1542 },
1543 },
1544 {
Dmitry Torokhov9961e252009-12-04 10:24:20 -08001545 /* Toshiba Portege M300 */
Richard Thrippleton53a26702006-04-02 00:10:18 -05001546 .matches = {
1547 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1548 DMI_MATCH(DMI_PRODUCT_NAME, "PORTEGE M300"),
Simon Horman9ba5eaa2005-07-11 01:07:20 -05001549 },
Dmitry Torokhov5f5eeff2009-10-12 21:35:00 -07001550
1551 },
1552 {
Dmitry Torokhov9961e252009-12-04 10:24:20 -08001553 /* Toshiba Portege M300 */
Dmitry Torokhov5f5eeff2009-10-12 21:35:00 -07001554 .matches = {
1555 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1556 DMI_MATCH(DMI_PRODUCT_NAME, "Portable PC"),
1557 DMI_MATCH(DMI_PRODUCT_VERSION, "Version 1.0"),
1558 },
1559
Simon Horman9ba5eaa2005-07-11 01:07:20 -05001560 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561#endif
Jan Beulich70874862011-03-31 00:01:58 -07001562 { }
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001563};
1564
Andres Salomonef8313b2010-12-23 01:19:38 -08001565static bool broken_olpc_ec;
1566
Sachin Kamatc9631562013-08-12 11:05:58 -07001567static const struct dmi_system_id olpc_dmi_table[] __initconst = {
Andres Salomonef8313b2010-12-23 01:19:38 -08001568#if defined(CONFIG_DMI) && defined(CONFIG_OLPC)
1569 {
1570 /* OLPC XO-1 or XO-1.5 */
1571 .matches = {
1572 DMI_MATCH(DMI_SYS_VENDOR, "OLPC"),
1573 DMI_MATCH(DMI_PRODUCT_NAME, "XO"),
1574 },
1575 },
Andres Salomonef8313b2010-12-23 01:19:38 -08001576#endif
Jan Beulich70874862011-03-31 00:01:58 -07001577 { }
Andres Salomonef8313b2010-12-23 01:19:38 -08001578};
1579
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001580void __init synaptics_module_init(void)
1581{
1582 impaired_toshiba_kbc = dmi_check_system(toshiba_dmi_table);
Andres Salomonef8313b2010-12-23 01:19:38 -08001583 broken_olpc_ec = dmi_check_system(olpc_dmi_table);
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001584}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
Daniel Drake7968a5d2011-11-08 00:00:35 -08001586static int __synaptics_init(struct psmouse *psmouse, bool absolute_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587{
1588 struct synaptics_data *priv;
Daniel Drake7968a5d2011-11-08 00:00:35 -08001589 int err = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
Andres Salomonef8313b2010-12-23 01:19:38 -08001591 /*
Daniel Drake83551c02011-11-11 16:05:04 -08001592 * The OLPC XO has issues with Synaptics' absolute mode; the constant
1593 * packet spew overloads the EC such that key presses on the keyboard
1594 * are missed. Given that, don't even attempt to use Absolute mode.
1595 * Relative mode seems to work just fine.
Andres Salomonef8313b2010-12-23 01:19:38 -08001596 */
Daniel Drake83551c02011-11-11 16:05:04 -08001597 if (absolute_mode && broken_olpc_ec) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001598 psmouse_info(psmouse,
1599 "OLPC XO detected, not enabling Synaptics protocol.\n");
Andres Salomonef8313b2010-12-23 01:19:38 -08001600 return -ENODEV;
1601 }
1602
Eric Sesterhennb39787a2006-03-14 00:09:16 -05001603 psmouse->private = priv = kzalloc(sizeof(struct synaptics_data), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 if (!priv)
Davidlohr Bueso6792cbb2010-09-29 18:53:35 -07001605 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
Andy Whitcroft4d368452009-02-28 12:51:01 -08001607 psmouse_reset(psmouse);
1608
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 if (synaptics_query_hardware(psmouse)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001610 psmouse_err(psmouse, "Unable to query device.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 goto init_fail;
1612 }
1613
Daniel Drake7968a5d2011-11-08 00:00:35 -08001614 priv->absolute_mode = absolute_mode;
1615 if (SYN_ID_DISGEST_SUPPORTED(priv->identity))
1616 priv->disable_gesture = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
Daniel Drake7968a5d2011-11-08 00:00:35 -08001618 if (synaptics_set_mode(psmouse)) {
1619 psmouse_err(psmouse, "Unable to initialize device.\n");
Henrik Rydbergfec6e522010-12-21 18:11:25 +01001620 goto init_fail;
1621 }
1622
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 priv->pkt_type = SYN_MODEL_NEWABS(priv->model_id) ? SYN_NEWABS : SYN_OLDABS;
1624
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001625 psmouse_info(psmouse,
Daniel Kurtzc6bd9d42012-07-07 18:08:51 -07001626 "Touchpad model: %ld, fw: %ld.%ld, id: %#lx, caps: %#lx/%#lx/%#lx, board id: %lu, fw id: %lu\n",
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001627 SYN_ID_MODEL(priv->identity),
1628 SYN_ID_MAJOR(priv->identity), SYN_ID_MINOR(priv->identity),
1629 priv->model_id,
Daniel Kurtzc6bd9d42012-07-07 18:08:51 -07001630 priv->capabilities, priv->ext_cap, priv->ext_cap_0c,
1631 priv->board_id, priv->firmware_id);
Dmitry Torokhov409b7502005-05-28 02:12:18 -05001632
Hans de Goede43e19882014-04-19 22:26:41 -07001633 set_input_params(psmouse, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
Dmitry Torokhov887cc122007-04-12 01:30:41 -04001635 /*
1636 * Encode touchpad model so that it can be used to set
1637 * input device->id.version and be visible to userspace.
1638 * Because version is __u16 we have to drop something.
1639 * Hardware info bits seem to be good candidates as they
1640 * are documented to be for Synaptics corp. internal use.
1641 */
1642 psmouse->model = ((priv->model_id & 0x00ff0000) >> 8) |
1643 (priv->model_id & 0x000000ff);
1644
Daniel Drake7968a5d2011-11-08 00:00:35 -08001645 if (absolute_mode) {
1646 psmouse->protocol_handler = synaptics_process_byte;
1647 psmouse->pktsize = 6;
1648 } else {
1649 /* Relative mode follows standard PS/2 mouse protocol */
1650 psmouse->protocol_handler = psmouse_process_byte;
1651 psmouse->pktsize = 3;
1652 }
1653
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 psmouse->set_rate = synaptics_set_rate;
1655 psmouse->disconnect = synaptics_disconnect;
1656 psmouse->reconnect = synaptics_reconnect;
Dmitry Torokhova1cec062007-02-18 01:40:24 -05001657 psmouse->cleanup = synaptics_reset;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001658 /* Synaptics can usually stay in sync without extra help */
1659 psmouse->resync_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
1661 if (SYN_CAP_PASS_THROUGH(priv->capabilities))
1662 synaptics_pt_create(psmouse);
1663
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 /*
1665 * Toshiba's KBC seems to have trouble handling data from
Andres Salomon7ee99162010-12-23 01:18:28 -08001666 * Synaptics at full rate. Switch to a lower rate (roughly
1667 * the same rate as a standard PS/2 mouse).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 */
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001669 if (psmouse->rate >= 80 && impaired_toshiba_kbc) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001670 psmouse_info(psmouse,
1671 "Toshiba %s detected, limiting rate to 40pps.\n",
1672 dmi_get_system_info(DMI_PRODUCT_NAME));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 psmouse->rate = 40;
1674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
Daniel Drake7968a5d2011-11-08 00:00:35 -08001676 if (!priv->absolute_mode && SYN_ID_DISGEST_SUPPORTED(priv->identity)) {
1677 err = device_create_file(&psmouse->ps2dev.serio->dev,
1678 &psmouse_attr_disable_gesture.dattr);
1679 if (err) {
1680 psmouse_err(psmouse,
1681 "Failed to create disable_gesture attribute (%d)",
1682 err);
1683 goto init_fail;
1684 }
1685 }
1686
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 return 0;
1688
1689 init_fail:
1690 kfree(priv);
Daniel Drake7968a5d2011-11-08 00:00:35 -08001691 return err;
1692}
1693
1694int synaptics_init(struct psmouse *psmouse)
1695{
1696 return __synaptics_init(psmouse, true);
1697}
1698
1699int synaptics_init_relative(struct psmouse *psmouse)
1700{
1701 return __synaptics_init(psmouse, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702}
1703
Daniel Drakee4e6efd2010-01-07 01:52:39 -08001704bool synaptics_supported(void)
1705{
1706 return true;
1707}
1708
Andres Salomon55e3d922007-03-10 01:39:54 -05001709#else /* CONFIG_MOUSE_PS2_SYNAPTICS */
1710
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001711void __init synaptics_module_init(void)
1712{
1713}
1714
Andres Salomon55e3d922007-03-10 01:39:54 -05001715int synaptics_init(struct psmouse *psmouse)
1716{
1717 return -ENOSYS;
1718}
1719
Daniel Drakee4e6efd2010-01-07 01:52:39 -08001720bool synaptics_supported(void)
1721{
1722 return false;
1723}
1724
Andres Salomon55e3d922007-03-10 01:39:54 -05001725#endif /* CONFIG_MOUSE_PS2_SYNAPTICS */