blob: 395ec9c5ae9a29297bc03c3738accf5cc9c0aade [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 Torokhov83ba9ea82010-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 Goede43e19882014-04-19 22:26:41 -0700120/* This list has been kindly provided by Synaptics. */
121static const char * const topbuttonpad_pnp_ids[] = {
122 "LEN0017",
123 "LEN0018",
124 "LEN0019",
125 "LEN0023",
126 "LEN002A",
127 "LEN002B",
128 "LEN002C",
129 "LEN002D",
130 "LEN002E",
131 "LEN0033", /* Helix */
132 "LEN0034", /* T431s, T540, X1 Carbon 2nd */
133 "LEN0035", /* X240 */
134 "LEN0036", /* T440 */
135 "LEN0037",
136 "LEN0038",
137 "LEN0041",
138 "LEN0042", /* Yoga */
139 "LEN0045",
140 "LEN0046",
141 "LEN0047",
142 "LEN0048",
143 "LEN0049",
144 "LEN2000",
145 "LEN2001",
146 "LEN2002",
147 "LEN2003",
148 "LEN2004", /* L440 */
149 "LEN2005",
150 "LEN2006",
151 "LEN2007",
152 "LEN2008",
153 "LEN2009",
154 "LEN200A",
155 "LEN200B",
156 NULL
157};
Andres Salomon55e3d922007-03-10 01:39:54 -0500158
Hans de Goedee2f61102014-05-19 22:53:23 -0700159static bool matches_pnp_id(struct psmouse *psmouse, const char * const ids[])
160{
161 int i;
162
163 if (!strncmp(psmouse->ps2dev.serio->firmware_id, "PNP:", 4))
164 for (i = 0; ids[i]; i++)
165 if (strstr(psmouse->ps2dev.serio->firmware_id, ids[i]))
166 return true;
167
168 return false;
169}
170
Andres Salomon55e3d922007-03-10 01:39:54 -0500171/*****************************************************************************
172 * Synaptics communications functions
173 ****************************************************************************/
174
175/*
JJ Ding1a49a0a2012-05-10 22:32:00 -0700176 * Synaptics touchpads report the y coordinate from bottom to top, which is
177 * opposite from what userspace expects.
178 * This function is used to invert y before reporting.
179 */
180static int synaptics_invert_y(int y)
181{
182 return YMAX_NOMINAL + YMIN_NOMINAL - y;
183}
184
185/*
Andres Salomon55e3d922007-03-10 01:39:54 -0500186 * Send a command to the synpatics touchpad by special commands
187 */
188static int synaptics_send_cmd(struct psmouse *psmouse, unsigned char c, unsigned char *param)
189{
190 if (psmouse_sliced_command(psmouse, c))
191 return -1;
192 if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_GETINFO))
193 return -1;
194 return 0;
195}
196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197/*
198 * Read the model-id bytes from the touchpad
199 * see also SYN_MODEL_* macros
200 */
201static int synaptics_model_id(struct psmouse *psmouse)
202{
203 struct synaptics_data *priv = psmouse->private;
204 unsigned char mi[3];
205
206 if (synaptics_send_cmd(psmouse, SYN_QUE_MODEL, mi))
207 return -1;
208 priv->model_id = (mi[0]<<16) | (mi[1]<<8) | mi[2];
209 return 0;
210}
211
212/*
Daniel Kurtzc6bd9d42012-07-07 18:08:51 -0700213 * Read the board id from the touchpad
214 * The board id is encoded in the "QUERY MODES" response
215 */
216static int synaptics_board_id(struct psmouse *psmouse)
217{
218 struct synaptics_data *priv = psmouse->private;
219 unsigned char bid[3];
220
221 if (synaptics_send_cmd(psmouse, SYN_QUE_MODES, bid))
222 return -1;
223 priv->board_id = ((bid[0] & 0xfc) << 6) | bid[1];
224 return 0;
225}
226
227/*
228 * Read the firmware id from the touchpad
229 */
230static int synaptics_firmware_id(struct psmouse *psmouse)
231{
232 struct synaptics_data *priv = psmouse->private;
233 unsigned char fwid[3];
234
235 if (synaptics_send_cmd(psmouse, SYN_QUE_FIRMWARE_ID, fwid))
236 return -1;
237 priv->firmware_id = (fwid[0] << 16) | (fwid[1] << 8) | fwid[2];
238 return 0;
239}
240
241/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 * Read the capability-bits from the touchpad
243 * see also the SYN_CAP_* macros
244 */
245static int synaptics_capability(struct psmouse *psmouse)
246{
247 struct synaptics_data *priv = psmouse->private;
248 unsigned char cap[3];
249
250 if (synaptics_send_cmd(psmouse, SYN_QUE_CAPABILITIES, cap))
251 return -1;
252 priv->capabilities = (cap[0] << 16) | (cap[1] << 8) | cap[2];
Takashi Iwai5f57d672010-04-19 10:37:21 -0700253 priv->ext_cap = priv->ext_cap_0c = 0;
254
Dmitry Torokhov3619b8f2010-07-21 00:01:19 -0700255 /*
256 * Older firmwares had submodel ID fixed to 0x47
257 */
258 if (SYN_ID_FULL(priv->identity) < 0x705 &&
259 SYN_CAP_SUBMODEL_ID(priv->capabilities) != 0x47) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 return -1;
Dmitry Torokhov3619b8f2010-07-21 00:01:19 -0700261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 /*
264 * Unless capExtended is set the rest of the flags should be ignored
265 */
266 if (!SYN_CAP_EXTENDED(priv->capabilities))
267 priv->capabilities = 0;
268
269 if (SYN_EXT_CAP_REQUESTS(priv->capabilities) >= 1) {
270 if (synaptics_send_cmd(psmouse, SYN_QUE_EXT_CAPAB, cap)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700271 psmouse_warn(psmouse,
272 "device claims to have extended capabilities, but I'm not able to read them.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 } else {
274 priv->ext_cap = (cap[0] << 16) | (cap[1] << 8) | cap[2];
275
276 /*
277 * if nExtBtn is greater than 8 it should be considered
278 * invalid and treated as 0
279 */
280 if (SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap) > 8)
281 priv->ext_cap &= 0xff0fff;
282 }
283 }
Takashi Iwai5f57d672010-04-19 10:37:21 -0700284
285 if (SYN_EXT_CAP_REQUESTS(priv->capabilities) >= 4) {
286 if (synaptics_send_cmd(psmouse, SYN_QUE_EXT_CAPAB_0C, cap)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700287 psmouse_warn(psmouse,
288 "device claims to have extended capability 0x0c, but I'm not able to read it.\n");
Takashi Iwai5f57d672010-04-19 10:37:21 -0700289 } else {
290 priv->ext_cap_0c = (cap[0] << 16) | (cap[1] << 8) | cap[2];
291 }
292 }
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 return 0;
295}
296
297/*
298 * Identify Touchpad
299 * See also the SYN_ID_* macros
300 */
301static int synaptics_identify(struct psmouse *psmouse)
302{
303 struct synaptics_data *priv = psmouse->private;
304 unsigned char id[3];
305
306 if (synaptics_send_cmd(psmouse, SYN_QUE_IDENTIFY, id))
307 return -1;
308 priv->identity = (id[0]<<16) | (id[1]<<8) | id[2];
309 if (SYN_ID_IS_SYNAPTICS(priv->identity))
310 return 0;
311 return -1;
312}
313
Tero Saarniec20a022009-06-10 23:27:24 -0700314/*
Dmitry Torokhov83ba9ea82010-05-10 23:06:52 -0700315 * Read touchpad resolution and maximum reported coordinates
Tero Saarniec20a022009-06-10 23:27:24 -0700316 * Resolution is left zero if touchpad does not support the query
317 */
Benjamin Tissoires421e08c2014-03-28 00:43:00 -0700318
319static const int *quirk_min_max;
320
Tero Saarniec20a022009-06-10 23:27:24 -0700321static int synaptics_resolution(struct psmouse *psmouse)
322{
323 struct synaptics_data *priv = psmouse->private;
Dmitry Torokhova66413f2011-07-09 12:32:56 -0700324 unsigned char resp[3];
Tero Saarniec20a022009-06-10 23:27:24 -0700325
Benjamin Tissoires421e08c2014-03-28 00:43:00 -0700326 if (quirk_min_max) {
327 priv->x_min = quirk_min_max[0];
328 priv->x_max = quirk_min_max[1];
329 priv->y_min = quirk_min_max[2];
330 priv->y_max = quirk_min_max[3];
331 return 0;
332 }
333
Tero Saarniec20a022009-06-10 23:27:24 -0700334 if (SYN_ID_MAJOR(priv->identity) < 4)
Takashi Iwaibbddd192010-07-14 09:32:46 -0700335 return 0;
Tero Saarniec20a022009-06-10 23:27:24 -0700336
Dmitry Torokhova66413f2011-07-09 12:32:56 -0700337 if (synaptics_send_cmd(psmouse, SYN_QUE_RESOLUTION, resp) == 0) {
338 if (resp[0] != 0 && (resp[1] & 0x80) && resp[2] != 0) {
339 priv->x_res = resp[0]; /* x resolution in units/mm */
340 priv->y_res = resp[2]; /* y resolution in units/mm */
Dmitry Torokhov83ba9ea82010-05-10 23:06:52 -0700341 }
342 }
Tero Saarniec20a022009-06-10 23:27:24 -0700343
Dmitry Torokhov83ba9ea82010-05-10 23:06:52 -0700344 if (SYN_EXT_CAP_REQUESTS(priv->capabilities) >= 5 &&
345 SYN_CAP_MAX_DIMENSIONS(priv->ext_cap_0c)) {
Dmitry Torokhova66413f2011-07-09 12:32:56 -0700346 if (synaptics_send_cmd(psmouse, SYN_QUE_EXT_MAX_COORDS, resp)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700347 psmouse_warn(psmouse,
348 "device claims to have max coordinates query, but I'm not able to read it.\n");
Dmitry Torokhov83ba9ea82010-05-10 23:06:52 -0700349 } else {
Dmitry Torokhova66413f2011-07-09 12:32:56 -0700350 priv->x_max = (resp[0] << 5) | ((resp[1] & 0x0f) << 1);
351 priv->y_max = (resp[2] << 5) | ((resp[1] & 0xf0) >> 3);
352 }
353 }
354
355 if (SYN_EXT_CAP_REQUESTS(priv->capabilities) >= 7 &&
356 SYN_CAP_MIN_DIMENSIONS(priv->ext_cap_0c)) {
357 if (synaptics_send_cmd(psmouse, SYN_QUE_EXT_MIN_COORDS, resp)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700358 psmouse_warn(psmouse,
359 "device claims to have min coordinates query, but I'm not able to read it.\n");
Dmitry Torokhova66413f2011-07-09 12:32:56 -0700360 } else {
361 priv->x_min = (resp[0] << 5) | ((resp[1] & 0x0f) << 1);
362 priv->y_min = (resp[2] << 5) | ((resp[1] & 0xf0) >> 3);
Dmitry Torokhov83ba9ea82010-05-10 23:06:52 -0700363 }
Tero Saarniec20a022009-06-10 23:27:24 -0700364 }
365
366 return 0;
367}
368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369static int synaptics_query_hardware(struct psmouse *psmouse)
370{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 if (synaptics_identify(psmouse))
372 return -1;
373 if (synaptics_model_id(psmouse))
374 return -1;
Daniel Kurtzc6bd9d42012-07-07 18:08:51 -0700375 if (synaptics_firmware_id(psmouse))
376 return -1;
377 if (synaptics_board_id(psmouse))
378 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 if (synaptics_capability(psmouse))
380 return -1;
Tero Saarniec20a022009-06-10 23:27:24 -0700381 if (synaptics_resolution(psmouse))
382 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384 return 0;
385}
386
Daniel Drake7968a5d2011-11-08 00:00:35 -0800387static int synaptics_set_advanced_gesture_mode(struct psmouse *psmouse)
388{
389 static unsigned char param = 0xc8;
390 struct synaptics_data *priv = psmouse->private;
391
Benjamin Herrenschmidt899c6122012-04-20 22:34:49 -0700392 if (!(SYN_CAP_ADV_GESTURE(priv->ext_cap_0c) ||
393 SYN_CAP_IMAGE_SENSOR(priv->ext_cap_0c)))
Daniel Drake7968a5d2011-11-08 00:00:35 -0800394 return 0;
395
396 if (psmouse_sliced_command(psmouse, SYN_QUE_MODEL))
397 return -1;
398
399 if (ps2_command(&psmouse->ps2dev, &param, PSMOUSE_CMD_SETRATE))
400 return -1;
401
402 /* Advanced gesture mode also sends multi finger data */
403 priv->capabilities |= BIT(1);
404
405 return 0;
406}
407
408static int synaptics_set_mode(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
410 struct synaptics_data *priv = psmouse->private;
411
Daniel Drake7968a5d2011-11-08 00:00:35 -0800412 priv->mode = 0;
413 if (priv->absolute_mode)
414 priv->mode |= SYN_BIT_ABSOLUTE_MODE;
415 if (priv->disable_gesture)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 priv->mode |= SYN_BIT_DISABLE_GESTURE;
Daniel Drake7968a5d2011-11-08 00:00:35 -0800417 if (psmouse->rate >= 80)
418 priv->mode |= SYN_BIT_HIGH_RATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 if (SYN_CAP_EXTENDED(priv->capabilities))
420 priv->mode |= SYN_BIT_W_MODE;
421
422 if (synaptics_mode_cmd(psmouse, priv->mode))
423 return -1;
424
Daniel Drake7968a5d2011-11-08 00:00:35 -0800425 if (priv->absolute_mode &&
426 synaptics_set_advanced_gesture_mode(psmouse)) {
427 psmouse_err(psmouse, "Advanced gesture mode init failed.\n");
428 return -1;
429 }
430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 return 0;
432}
433
434static void synaptics_set_rate(struct psmouse *psmouse, unsigned int rate)
435{
436 struct synaptics_data *priv = psmouse->private;
437
438 if (rate >= 80) {
439 priv->mode |= SYN_BIT_HIGH_RATE;
440 psmouse->rate = 80;
441 } else {
442 priv->mode &= ~SYN_BIT_HIGH_RATE;
443 psmouse->rate = 40;
444 }
445
446 synaptics_mode_cmd(psmouse, priv->mode);
447}
448
449/*****************************************************************************
450 * Synaptics pass-through PS/2 port support
451 ****************************************************************************/
452static int synaptics_pt_write(struct serio *serio, unsigned char c)
453{
454 struct psmouse *parent = serio_get_drvdata(serio->parent);
455 char rate_param = SYN_PS_CLIENT_CMD; /* indicates that we want pass-through port */
456
457 if (psmouse_sliced_command(parent, c))
458 return -1;
459 if (ps2_command(&parent->ps2dev, &rate_param, PSMOUSE_CMD_SETRATE))
460 return -1;
461 return 0;
462}
463
Dmitry Torokhova8b3c0f2010-10-04 21:46:10 -0700464static int synaptics_pt_start(struct serio *serio)
465{
466 struct psmouse *parent = serio_get_drvdata(serio->parent);
467 struct synaptics_data *priv = parent->private;
468
469 serio_pause_rx(parent->ps2dev.serio);
470 priv->pt_port = serio;
471 serio_continue_rx(parent->ps2dev.serio);
472
473 return 0;
474}
475
476static void synaptics_pt_stop(struct serio *serio)
477{
478 struct psmouse *parent = serio_get_drvdata(serio->parent);
479 struct synaptics_data *priv = parent->private;
480
481 serio_pause_rx(parent->ps2dev.serio);
482 priv->pt_port = NULL;
483 serio_continue_rx(parent->ps2dev.serio);
484}
485
486static int synaptics_is_pt_packet(unsigned char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
488 return (buf[0] & 0xFC) == 0x84 && (buf[3] & 0xCC) == 0xC4;
489}
490
491static void synaptics_pass_pt_packet(struct serio *ptport, unsigned char *packet)
492{
493 struct psmouse *child = serio_get_drvdata(ptport);
494
495 if (child && child->state == PSMOUSE_ACTIVATED) {
David Howells7d12e782006-10-05 14:55:46 +0100496 serio_interrupt(ptport, packet[1], 0);
497 serio_interrupt(ptport, packet[4], 0);
498 serio_interrupt(ptport, packet[5], 0);
Sergey Vlasov33fdfa92005-07-24 00:53:32 -0500499 if (child->pktsize == 4)
David Howells7d12e782006-10-05 14:55:46 +0100500 serio_interrupt(ptport, packet[2], 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 } else
David Howells7d12e782006-10-05 14:55:46 +0100502 serio_interrupt(ptport, packet[1], 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}
504
505static void synaptics_pt_activate(struct psmouse *psmouse)
506{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 struct synaptics_data *priv = psmouse->private;
Dmitry Torokhova8b3c0f2010-10-04 21:46:10 -0700508 struct psmouse *child = serio_get_drvdata(priv->pt_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
510 /* adjust the touchpad to child's choice of protocol */
511 if (child) {
Sergey Vlasov33fdfa92005-07-24 00:53:32 -0500512 if (child->pktsize == 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 priv->mode |= SYN_BIT_FOUR_BYTE_CLIENT;
514 else
515 priv->mode &= ~SYN_BIT_FOUR_BYTE_CLIENT;
516
517 if (synaptics_mode_cmd(psmouse, priv->mode))
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700518 psmouse_warn(psmouse,
519 "failed to switch guest protocol\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 }
521}
522
523static void synaptics_pt_create(struct psmouse *psmouse)
524{
525 struct serio *serio;
526
Eric Sesterhennb39787a2006-03-14 00:09:16 -0500527 serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 if (!serio) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700529 psmouse_err(psmouse,
530 "not enough memory for pass-through port\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 return;
532 }
533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 serio->id.type = SERIO_PS_PSTHRU;
535 strlcpy(serio->name, "Synaptics pass-through", sizeof(serio->name));
536 strlcpy(serio->phys, "synaptics-pt/serio0", sizeof(serio->name));
537 serio->write = synaptics_pt_write;
Dmitry Torokhova8b3c0f2010-10-04 21:46:10 -0700538 serio->start = synaptics_pt_start;
539 serio->stop = synaptics_pt_stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 serio->parent = psmouse->ps2dev.serio;
541
542 psmouse->pt_activate = synaptics_pt_activate;
543
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700544 psmouse_info(psmouse, "serio: %s port at %s\n",
545 serio->name, psmouse->phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 serio_register_port(serio);
547}
548
549/*****************************************************************************
550 * Functions to interpret the absolute mode packets
551 ****************************************************************************/
552
Daniel Kurtza6ca40c2011-08-23 23:02:31 -0700553static void synaptics_mt_state_set(struct synaptics_mt_state *state, int count,
554 int sgm, int agm)
555{
556 state->count = count;
557 state->sgm = sgm;
558 state->agm = agm;
559}
560
Daniel Kurtz7afdb842011-08-23 23:00:33 -0700561static void synaptics_parse_agm(const unsigned char buf[],
Daniel Kurtza6ca40c2011-08-23 23:02:31 -0700562 struct synaptics_data *priv,
563 struct synaptics_hw_state *hw)
Daniel Kurtz7afdb842011-08-23 23:00:33 -0700564{
565 struct synaptics_hw_state *agm = &priv->agm;
Daniel Kurtza6ca40c2011-08-23 23:02:31 -0700566 int agm_packet_type;
Daniel Kurtz7afdb842011-08-23 23:00:33 -0700567
Daniel Kurtza6ca40c2011-08-23 23:02:31 -0700568 agm_packet_type = (buf[5] & 0x30) >> 4;
569 switch (agm_packet_type) {
570 case 1:
571 /* Gesture packet: (x, y, z) half resolution */
572 agm->w = hw->w;
573 agm->x = (((buf[4] & 0x0f) << 8) | buf[1]) << 1;
574 agm->y = (((buf[4] & 0xf0) << 4) | buf[2]) << 1;
575 agm->z = ((buf[3] & 0x30) | (buf[5] & 0x0f)) << 1;
576 break;
577
578 case 2:
579 /* AGM-CONTACT packet: (count, sgm, agm) */
580 synaptics_mt_state_set(&agm->mt_state, buf[1], buf[2], buf[4]);
581 break;
582
583 default:
584 break;
585 }
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700586
587 /* Record that at least one AGM has been received since last SGM */
588 priv->agm_pending = true;
Daniel Kurtz7afdb842011-08-23 23:00:33 -0700589}
590
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100591static int synaptics_parse_hw_state(const unsigned char buf[],
592 struct synaptics_data *priv,
593 struct synaptics_hw_state *hw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
595 memset(hw, 0, sizeof(struct synaptics_hw_state));
596
597 if (SYN_MODEL_NEWABS(priv->model_id)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 hw->w = (((buf[0] & 0x30) >> 2) |
599 ((buf[0] & 0x04) >> 1) |
600 ((buf[3] & 0x04) >> 2));
601
602 hw->left = (buf[0] & 0x01) ? 1 : 0;
603 hw->right = (buf[0] & 0x02) ? 1 : 0;
604
Takashi Iwai5f57d672010-04-19 10:37:21 -0700605 if (SYN_CAP_CLICKPAD(priv->ext_cap_0c)) {
606 /*
607 * Clickpad's button is transmitted as middle button,
608 * however, since it is primary button, we will report
609 * it as BTN_LEFT.
610 */
611 hw->left = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0;
612
613 } else if (SYN_CAP_MIDDLE_BUTTON(priv->capabilities)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 hw->middle = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0;
615 if (hw->w == 2)
616 hw->scroll = (signed char)(buf[1]);
617 }
618
619 if (SYN_CAP_FOUR_BUTTON(priv->capabilities)) {
620 hw->up = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0;
621 hw->down = ((buf[0] ^ buf[3]) & 0x02) ? 1 : 0;
622 }
623
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700624 if ((SYN_CAP_ADV_GESTURE(priv->ext_cap_0c) ||
625 SYN_CAP_IMAGE_SENSOR(priv->ext_cap_0c)) &&
626 hw->w == 2) {
Daniel Kurtza6ca40c2011-08-23 23:02:31 -0700627 synaptics_parse_agm(buf, priv, hw);
Daniel Kurtz28d5fd82011-07-06 22:57:39 -0700628 return 1;
629 }
630
631 hw->x = (((buf[3] & 0x10) << 8) |
632 ((buf[1] & 0x0f) << 8) |
633 buf[4]);
634 hw->y = (((buf[3] & 0x20) << 7) |
635 ((buf[1] & 0xf0) << 4) |
636 buf[5]);
637 hw->z = buf[2];
638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 if (SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap) &&
640 ((buf[0] ^ buf[3]) & 0x02)) {
641 switch (SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap) & ~0x01) {
642 default:
643 /*
644 * if nExtBtn is greater than 8 it should be
645 * considered invalid and treated as 0
646 */
647 break;
648 case 8:
649 hw->ext_buttons |= ((buf[5] & 0x08)) ? 0x80 : 0;
650 hw->ext_buttons |= ((buf[4] & 0x08)) ? 0x40 : 0;
651 case 6:
652 hw->ext_buttons |= ((buf[5] & 0x04)) ? 0x20 : 0;
653 hw->ext_buttons |= ((buf[4] & 0x04)) ? 0x10 : 0;
654 case 4:
655 hw->ext_buttons |= ((buf[5] & 0x02)) ? 0x08 : 0;
656 hw->ext_buttons |= ((buf[4] & 0x02)) ? 0x04 : 0;
657 case 2:
658 hw->ext_buttons |= ((buf[5] & 0x01)) ? 0x02 : 0;
659 hw->ext_buttons |= ((buf[4] & 0x01)) ? 0x01 : 0;
660 }
661 }
662 } else {
663 hw->x = (((buf[1] & 0x1f) << 8) | buf[2]);
664 hw->y = (((buf[4] & 0x1f) << 8) | buf[5]);
665
666 hw->z = (((buf[0] & 0x30) << 2) | (buf[3] & 0x3F));
667 hw->w = (((buf[1] & 0x80) >> 4) | ((buf[0] & 0x04) >> 1));
668
669 hw->left = (buf[0] & 0x01) ? 1 : 0;
670 hw->right = (buf[0] & 0x02) ? 1 : 0;
671 }
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100672
Seth Forshee824efd32012-09-28 10:29:21 -0700673 /*
674 * Convert wrap-around values to negative. (X|Y)_MAX_POSITIVE
675 * is used by some firmware to indicate a finger at the edge of
676 * the touchpad whose precise position cannot be determined, so
677 * convert these values to the maximum axis value.
678 */
Seth Forsheec03945062012-07-24 23:54:11 -0700679 if (hw->x > X_MAX_POSITIVE)
680 hw->x -= 1 << ABS_POS_BITS;
Seth Forshee824efd32012-09-28 10:29:21 -0700681 else if (hw->x == X_MAX_POSITIVE)
682 hw->x = XMAX;
683
Seth Forsheec03945062012-07-24 23:54:11 -0700684 if (hw->y > Y_MAX_POSITIVE)
685 hw->y -= 1 << ABS_POS_BITS;
Seth Forshee824efd32012-09-28 10:29:21 -0700686 else if (hw->y == Y_MAX_POSITIVE)
687 hw->y = YMAX;
Seth Forsheec03945062012-07-24 23:54:11 -0700688
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100689 return 0;
690}
691
Daniel Kurtzbea9f0f2011-07-06 22:42:52 -0700692static void synaptics_report_semi_mt_slot(struct input_dev *dev, int slot,
693 bool active, int x, int y)
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100694{
695 input_mt_slot(dev, slot);
696 input_mt_report_slot_state(dev, MT_TOOL_FINGER, active);
697 if (active) {
698 input_report_abs(dev, ABS_MT_POSITION_X, x);
Daniel Kurtz6de58dd2011-08-23 23:00:24 -0700699 input_report_abs(dev, ABS_MT_POSITION_Y, synaptics_invert_y(y));
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100700 }
701}
702
703static void synaptics_report_semi_mt_data(struct input_dev *dev,
704 const struct synaptics_hw_state *a,
705 const struct synaptics_hw_state *b,
706 int num_fingers)
707{
708 if (num_fingers >= 2) {
Daniel Kurtzbea9f0f2011-07-06 22:42:52 -0700709 synaptics_report_semi_mt_slot(dev, 0, true, min(a->x, b->x),
710 min(a->y, b->y));
711 synaptics_report_semi_mt_slot(dev, 1, true, max(a->x, b->x),
712 max(a->y, b->y));
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100713 } else if (num_fingers == 1) {
Daniel Kurtzbea9f0f2011-07-06 22:42:52 -0700714 synaptics_report_semi_mt_slot(dev, 0, true, a->x, a->y);
715 synaptics_report_semi_mt_slot(dev, 1, false, 0, 0);
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100716 } else {
Daniel Kurtzbea9f0f2011-07-06 22:42:52 -0700717 synaptics_report_semi_mt_slot(dev, 0, false, 0, 0);
718 synaptics_report_semi_mt_slot(dev, 1, false, 0, 0);
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720}
721
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700722static void synaptics_report_buttons(struct psmouse *psmouse,
723 const struct synaptics_hw_state *hw)
724{
725 struct input_dev *dev = psmouse->dev;
726 struct synaptics_data *priv = psmouse->private;
727 int i;
728
729 input_report_key(dev, BTN_LEFT, hw->left);
730 input_report_key(dev, BTN_RIGHT, hw->right);
731
732 if (SYN_CAP_MIDDLE_BUTTON(priv->capabilities))
733 input_report_key(dev, BTN_MIDDLE, hw->middle);
734
735 if (SYN_CAP_FOUR_BUTTON(priv->capabilities)) {
736 input_report_key(dev, BTN_FORWARD, hw->up);
737 input_report_key(dev, BTN_BACK, hw->down);
738 }
739
740 for (i = 0; i < SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap); i++)
741 input_report_key(dev, BTN_0 + i, hw->ext_buttons & (1 << i));
742}
743
744static void synaptics_report_slot(struct input_dev *dev, int slot,
745 const struct synaptics_hw_state *hw)
746{
747 input_mt_slot(dev, slot);
748 input_mt_report_slot_state(dev, MT_TOOL_FINGER, (hw != NULL));
749 if (!hw)
750 return;
751
752 input_report_abs(dev, ABS_MT_POSITION_X, hw->x);
753 input_report_abs(dev, ABS_MT_POSITION_Y, synaptics_invert_y(hw->y));
754 input_report_abs(dev, ABS_MT_PRESSURE, hw->z);
755}
756
757static void synaptics_report_mt_data(struct psmouse *psmouse,
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700758 struct synaptics_mt_state *mt_state,
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700759 const struct synaptics_hw_state *sgm)
760{
761 struct input_dev *dev = psmouse->dev;
762 struct synaptics_data *priv = psmouse->private;
763 struct synaptics_hw_state *agm = &priv->agm;
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700764 struct synaptics_mt_state *old = &priv->mt_state;
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700765
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700766 switch (mt_state->count) {
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700767 case 0:
768 synaptics_report_slot(dev, 0, NULL);
769 synaptics_report_slot(dev, 1, NULL);
770 break;
771 case 1:
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700772 if (mt_state->sgm == -1) {
773 synaptics_report_slot(dev, 0, NULL);
774 synaptics_report_slot(dev, 1, NULL);
775 } else if (mt_state->sgm == 0) {
776 synaptics_report_slot(dev, 0, sgm);
777 synaptics_report_slot(dev, 1, NULL);
778 } else {
779 synaptics_report_slot(dev, 0, NULL);
780 synaptics_report_slot(dev, 1, sgm);
781 }
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700782 break;
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700783 default:
784 /*
785 * If the finger slot contained in SGM is valid, and either
Daniel Kurtz48064bd2013-02-13 13:53:07 -0800786 * hasn't changed, or is new, or the old SGM has now moved to
787 * AGM, then report SGM in MTB slot 0.
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700788 * Otherwise, empty MTB slot 0.
789 */
790 if (mt_state->sgm != -1 &&
Daniel Kurtz48064bd2013-02-13 13:53:07 -0800791 (mt_state->sgm == old->sgm ||
792 old->sgm == -1 || mt_state->agm == old->sgm))
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700793 synaptics_report_slot(dev, 0, sgm);
794 else
795 synaptics_report_slot(dev, 0, NULL);
796
797 /*
798 * If the finger slot contained in AGM is valid, and either
799 * hasn't changed, or is new, then report AGM in MTB slot 1.
800 * Otherwise, empty MTB slot 1.
Daniel Kurtz48064bd2013-02-13 13:53:07 -0800801 *
802 * However, in the case where the AGM is new, make sure that
803 * that it is either the same as the old SGM, or there was no
804 * SGM.
805 *
806 * Otherwise, if the SGM was just 1, and the new AGM is 2, then
807 * the new AGM will keep the old SGM's tracking ID, which can
808 * cause apparent drumroll. This happens if in the following
809 * valid finger sequence:
810 *
811 * Action SGM AGM (MTB slot:Contact)
812 * 1. Touch contact 0 (0:0)
813 * 2. Touch contact 1 (0:0, 1:1)
814 * 3. Lift contact 0 (1:1)
815 * 4. Touch contacts 2,3 (0:2, 1:3)
816 *
817 * In step 4, contact 3, in AGM must not be given the same
818 * tracking ID as contact 1 had in step 3. To avoid this,
819 * the first agm with contact 3 is dropped and slot 1 is
820 * invalidated (tracking ID = -1).
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700821 */
822 if (mt_state->agm != -1 &&
Daniel Kurtz48064bd2013-02-13 13:53:07 -0800823 (mt_state->agm == old->agm ||
824 (old->agm == -1 &&
825 (old->sgm == -1 || mt_state->agm == old->sgm))))
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700826 synaptics_report_slot(dev, 1, agm);
827 else
828 synaptics_report_slot(dev, 1, NULL);
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700829 break;
830 }
831
832 /* Don't use active slot count to generate BTN_TOOL events. */
833 input_mt_report_pointer_emulation(dev, false);
834
835 /* Send the number of fingers reported by touchpad itself. */
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700836 input_mt_report_finger_count(dev, mt_state->count);
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700837
838 synaptics_report_buttons(psmouse, sgm);
839
840 input_sync(dev);
841}
842
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700843/* Handle case where mt_state->count = 0 */
844static void synaptics_image_sensor_0f(struct synaptics_data *priv,
845 struct synaptics_mt_state *mt_state)
846{
847 synaptics_mt_state_set(mt_state, 0, -1, -1);
848 priv->mt_state_lost = false;
849}
850
851/* Handle case where mt_state->count = 1 */
852static void synaptics_image_sensor_1f(struct synaptics_data *priv,
853 struct synaptics_mt_state *mt_state)
854{
855 struct synaptics_hw_state *agm = &priv->agm;
856 struct synaptics_mt_state *old = &priv->mt_state;
857
858 /*
859 * If the last AGM was (0,0,0), and there is only one finger left,
860 * then we absolutely know that SGM contains slot 0, and all other
861 * fingers have been removed.
862 */
863 if (priv->agm_pending && agm->z == 0) {
864 synaptics_mt_state_set(mt_state, 1, 0, -1);
865 priv->mt_state_lost = false;
866 return;
867 }
868
869 switch (old->count) {
870 case 0:
871 synaptics_mt_state_set(mt_state, 1, 0, -1);
872 break;
873 case 1:
874 /*
875 * If mt_state_lost, then the previous transition was 3->1,
876 * and SGM now contains either slot 0 or 1, but we don't know
877 * which. So, we just assume that the SGM now contains slot 1.
878 *
879 * If pending AGM and either:
880 * (a) the previous SGM slot contains slot 0, or
881 * (b) there was no SGM slot
882 * then, the SGM now contains slot 1
883 *
884 * Case (a) happens with very rapid "drum roll" gestures, where
885 * slot 0 finger is lifted and a new slot 1 finger touches
886 * within one reporting interval.
887 *
888 * Case (b) happens if initially two or more fingers tap
889 * briefly, and all but one lift before the end of the first
890 * reporting interval.
891 *
892 * (In both these cases, slot 0 will becomes empty, so SGM
893 * contains slot 1 with the new finger)
894 *
895 * Else, if there was no previous SGM, it now contains slot 0.
896 *
897 * Otherwise, SGM still contains the same slot.
898 */
899 if (priv->mt_state_lost ||
900 (priv->agm_pending && old->sgm <= 0))
901 synaptics_mt_state_set(mt_state, 1, 1, -1);
902 else if (old->sgm == -1)
903 synaptics_mt_state_set(mt_state, 1, 0, -1);
904 break;
905 case 2:
906 /*
907 * If mt_state_lost, we don't know which finger SGM contains.
908 *
909 * So, report 1 finger, but with both slots empty.
910 * We will use slot 1 on subsequent 1->1
911 */
912 if (priv->mt_state_lost) {
913 synaptics_mt_state_set(mt_state, 1, -1, -1);
914 break;
915 }
916 /*
917 * Since the last AGM was NOT (0,0,0), it was the finger in
918 * slot 0 that has been removed.
919 * So, SGM now contains previous AGM's slot, and AGM is now
920 * empty.
921 */
922 synaptics_mt_state_set(mt_state, 1, old->agm, -1);
923 break;
924 case 3:
925 /*
926 * Since last AGM was not (0,0,0), we don't know which finger
927 * is left.
928 *
929 * So, report 1 finger, but with both slots empty.
930 * We will use slot 1 on subsequent 1->1
931 */
932 synaptics_mt_state_set(mt_state, 1, -1, -1);
933 priv->mt_state_lost = true;
934 break;
Daniel Kurtz6b4b49f2011-08-23 23:02:56 -0700935 case 4:
936 case 5:
937 /* mt_state was updated by AGM-CONTACT packet */
938 break;
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700939 }
940}
941
942/* Handle case where mt_state->count = 2 */
943static void synaptics_image_sensor_2f(struct synaptics_data *priv,
944 struct synaptics_mt_state *mt_state)
945{
946 struct synaptics_mt_state *old = &priv->mt_state;
947
948 switch (old->count) {
949 case 0:
950 synaptics_mt_state_set(mt_state, 2, 0, 1);
951 break;
952 case 1:
953 /*
954 * If previous SGM contained slot 1 or higher, SGM now contains
955 * slot 0 (the newly touching finger) and AGM contains SGM's
956 * previous slot.
957 *
958 * Otherwise, SGM still contains slot 0 and AGM now contains
959 * slot 1.
960 */
961 if (old->sgm >= 1)
962 synaptics_mt_state_set(mt_state, 2, 0, old->sgm);
963 else
964 synaptics_mt_state_set(mt_state, 2, 0, 1);
965 break;
966 case 2:
967 /*
968 * If mt_state_lost, SGM now contains either finger 1 or 2, but
969 * we don't know which.
970 * So, we just assume that the SGM contains slot 0 and AGM 1.
971 */
972 if (priv->mt_state_lost)
973 synaptics_mt_state_set(mt_state, 2, 0, 1);
974 /*
975 * Otherwise, use the same mt_state, since it either hasn't
976 * changed, or was updated by a recently received AGM-CONTACT
977 * packet.
978 */
979 break;
980 case 3:
981 /*
982 * 3->2 transitions have two unsolvable problems:
983 * 1) no indication is given which finger was removed
984 * 2) no way to tell if agm packet was for finger 3
985 * before 3->2, or finger 2 after 3->2.
986 *
987 * So, report 2 fingers, but empty all slots.
988 * We will guess slots [0,1] on subsequent 2->2.
989 */
990 synaptics_mt_state_set(mt_state, 2, -1, -1);
991 priv->mt_state_lost = true;
992 break;
Daniel Kurtz6b4b49f2011-08-23 23:02:56 -0700993 case 4:
994 case 5:
995 /* mt_state was updated by AGM-CONTACT packet */
996 break;
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700997 }
998}
999
1000/* Handle case where mt_state->count = 3 */
1001static void synaptics_image_sensor_3f(struct synaptics_data *priv,
1002 struct synaptics_mt_state *mt_state)
1003{
1004 struct synaptics_mt_state *old = &priv->mt_state;
1005
1006 switch (old->count) {
1007 case 0:
1008 synaptics_mt_state_set(mt_state, 3, 0, 2);
1009 break;
1010 case 1:
1011 /*
1012 * If previous SGM contained slot 2 or higher, SGM now contains
1013 * slot 0 (one of the newly touching fingers) and AGM contains
1014 * SGM's previous slot.
1015 *
1016 * Otherwise, SGM now contains slot 0 and AGM contains slot 2.
1017 */
1018 if (old->sgm >= 2)
1019 synaptics_mt_state_set(mt_state, 3, 0, old->sgm);
1020 else
1021 synaptics_mt_state_set(mt_state, 3, 0, 2);
1022 break;
1023 case 2:
1024 /*
Daniel Kurtz6b4b49f2011-08-23 23:02:56 -07001025 * If the AGM previously contained slot 3 or higher, then the
1026 * newly touching finger is in the lowest available slot.
1027 *
1028 * If SGM was previously 1 or higher, then the new SGM is
1029 * now slot 0 (with a new finger), otherwise, the new finger
1030 * is now in a hidden slot between 0 and AGM's slot.
1031 *
1032 * In all such cases, the SGM now contains slot 0, and the AGM
1033 * continues to contain the same slot as before.
1034 */
1035 if (old->agm >= 3) {
1036 synaptics_mt_state_set(mt_state, 3, 0, old->agm);
1037 break;
1038 }
1039
1040 /*
Daniel Kurtz4dc772d2011-08-23 23:02:40 -07001041 * After some 3->1 and all 3->2 transitions, we lose track
1042 * of which slot is reported by SGM and AGM.
1043 *
1044 * For 2->3 in this state, report 3 fingers, but empty all
1045 * slots, and we will guess (0,2) on a subsequent 0->3.
1046 *
1047 * To userspace, the resulting transition will look like:
1048 * 2:[0,1] -> 3:[-1,-1] -> 3:[0,2]
1049 */
1050 if (priv->mt_state_lost) {
1051 synaptics_mt_state_set(mt_state, 3, -1, -1);
1052 break;
1053 }
1054
1055 /*
1056 * If the (SGM,AGM) really previously contained slots (0, 1),
1057 * then we cannot know what slot was just reported by the AGM,
1058 * because the 2->3 transition can occur either before or after
1059 * the AGM packet. Thus, this most recent AGM could contain
1060 * either the same old slot 1 or the new slot 2.
1061 * Subsequent AGMs will be reporting slot 2.
1062 *
1063 * To userspace, the resulting transition will look like:
1064 * 2:[0,1] -> 3:[0,-1] -> 3:[0,2]
1065 */
1066 synaptics_mt_state_set(mt_state, 3, 0, -1);
1067 break;
1068 case 3:
1069 /*
1070 * If, for whatever reason, the previous agm was invalid,
1071 * Assume SGM now contains slot 0, AGM now contains slot 2.
1072 */
1073 if (old->agm <= 2)
1074 synaptics_mt_state_set(mt_state, 3, 0, 2);
1075 /*
1076 * mt_state either hasn't changed, or was updated by a recently
1077 * received AGM-CONTACT packet.
1078 */
1079 break;
Daniel Kurtz6b4b49f2011-08-23 23:02:56 -07001080
1081 case 4:
1082 case 5:
1083 /* mt_state was updated by AGM-CONTACT packet */
1084 break;
Daniel Kurtz4dc772d2011-08-23 23:02:40 -07001085 }
1086}
1087
Daniel Kurtz6b4b49f2011-08-23 23:02:56 -07001088/* Handle case where mt_state->count = 4, or = 5 */
1089static void synaptics_image_sensor_45f(struct synaptics_data *priv,
1090 struct synaptics_mt_state *mt_state)
1091{
1092 /* mt_state was updated correctly by AGM-CONTACT packet */
1093 priv->mt_state_lost = false;
1094}
1095
Daniel Kurtz3cdfee92011-08-23 23:02:25 -07001096static void synaptics_image_sensor_process(struct psmouse *psmouse,
1097 struct synaptics_hw_state *sgm)
1098{
Daniel Kurtz4dc772d2011-08-23 23:02:40 -07001099 struct synaptics_data *priv = psmouse->private;
1100 struct synaptics_hw_state *agm = &priv->agm;
1101 struct synaptics_mt_state mt_state;
Daniel Kurtz3cdfee92011-08-23 23:02:25 -07001102
Daniel Kurtz4dc772d2011-08-23 23:02:40 -07001103 /* Initialize using current mt_state (as updated by last agm) */
1104 mt_state = agm->mt_state;
1105
1106 /*
1107 * Update mt_state using the new finger count and current mt_state.
1108 */
Daniel Kurtz3cdfee92011-08-23 23:02:25 -07001109 if (sgm->z == 0)
Daniel Kurtz4dc772d2011-08-23 23:02:40 -07001110 synaptics_image_sensor_0f(priv, &mt_state);
Daniel Kurtz3cdfee92011-08-23 23:02:25 -07001111 else if (sgm->w >= 4)
Daniel Kurtz4dc772d2011-08-23 23:02:40 -07001112 synaptics_image_sensor_1f(priv, &mt_state);
Daniel Kurtz3cdfee92011-08-23 23:02:25 -07001113 else if (sgm->w == 0)
Daniel Kurtz4dc772d2011-08-23 23:02:40 -07001114 synaptics_image_sensor_2f(priv, &mt_state);
Daniel Kurtz6b4b49f2011-08-23 23:02:56 -07001115 else if (sgm->w == 1 && mt_state.count <= 3)
Daniel Kurtz4dc772d2011-08-23 23:02:40 -07001116 synaptics_image_sensor_3f(priv, &mt_state);
Daniel Kurtz6b4b49f2011-08-23 23:02:56 -07001117 else
1118 synaptics_image_sensor_45f(priv, &mt_state);
Daniel Kurtz3cdfee92011-08-23 23:02:25 -07001119
1120 /* Send resulting input events to user space */
Daniel Kurtz4dc772d2011-08-23 23:02:40 -07001121 synaptics_report_mt_data(psmouse, &mt_state, sgm);
1122
1123 /* Store updated mt_state */
1124 priv->mt_state = agm->mt_state = mt_state;
1125 priv->agm_pending = false;
Daniel Kurtz3cdfee92011-08-23 23:02:25 -07001126}
1127
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128/*
1129 * called for each full received packet from the touchpad
1130 */
1131static void synaptics_process_packet(struct psmouse *psmouse)
1132{
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001133 struct input_dev *dev = psmouse->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 struct synaptics_data *priv = psmouse->private;
1135 struct synaptics_hw_state hw;
1136 int num_fingers;
1137 int finger_width;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Henrik Rydbergfec6e522010-12-21 18:11:25 +01001139 if (synaptics_parse_hw_state(psmouse->packet, priv, &hw))
1140 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
Daniel Kurtz3cdfee92011-08-23 23:02:25 -07001142 if (SYN_CAP_IMAGE_SENSOR(priv->ext_cap_0c)) {
1143 synaptics_image_sensor_process(psmouse, &hw);
1144 return;
1145 }
1146
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 if (hw.scroll) {
1148 priv->scroll += hw.scroll;
1149
1150 while (priv->scroll >= 4) {
1151 input_report_key(dev, BTN_BACK, !hw.down);
1152 input_sync(dev);
1153 input_report_key(dev, BTN_BACK, hw.down);
1154 input_sync(dev);
1155 priv->scroll -= 4;
1156 }
1157 while (priv->scroll <= -4) {
1158 input_report_key(dev, BTN_FORWARD, !hw.up);
1159 input_sync(dev);
1160 input_report_key(dev, BTN_FORWARD, hw.up);
1161 input_sync(dev);
1162 priv->scroll += 4;
1163 }
1164 return;
1165 }
1166
Henrik Rydberg4f56ce92010-12-18 15:42:30 +01001167 if (hw.z > 0 && hw.x > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 num_fingers = 1;
1169 finger_width = 5;
1170 if (SYN_CAP_EXTENDED(priv->capabilities)) {
1171 switch (hw.w) {
1172 case 0 ... 1:
1173 if (SYN_CAP_MULTIFINGER(priv->capabilities))
1174 num_fingers = hw.w + 2;
1175 break;
1176 case 2:
1177 if (SYN_MODEL_PEN(priv->model_id))
1178 ; /* Nothing, treat a pen as a single finger */
1179 break;
1180 case 4 ... 15:
1181 if (SYN_CAP_PALMDETECT(priv->capabilities))
1182 finger_width = hw.w;
1183 break;
1184 }
1185 }
1186 } else {
1187 num_fingers = 0;
1188 finger_width = 0;
1189 }
1190
Henrik Rydbergfec6e522010-12-21 18:11:25 +01001191 if (SYN_CAP_ADV_GESTURE(priv->ext_cap_0c))
Daniel Kurtz7afdb842011-08-23 23:00:33 -07001192 synaptics_report_semi_mt_data(dev, &hw, &priv->agm,
1193 num_fingers);
Henrik Rydbergfec6e522010-12-21 18:11:25 +01001194
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 /* Post events
1196 * BTN_TOUCH has to be first as mousedev relies on it when doing
1197 * absolute -> relative conversion
1198 */
1199 if (hw.z > 30) input_report_key(dev, BTN_TOUCH, 1);
1200 if (hw.z < 25) input_report_key(dev, BTN_TOUCH, 0);
1201
Henrik Rydberg4f56ce92010-12-18 15:42:30 +01001202 if (num_fingers > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 input_report_abs(dev, ABS_X, hw.x);
Daniel Kurtz6de58dd2011-08-23 23:00:24 -07001204 input_report_abs(dev, ABS_Y, synaptics_invert_y(hw.y));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 }
1206 input_report_abs(dev, ABS_PRESSURE, hw.z);
1207
Chris Bagwell2a8e7712010-07-19 09:06:15 -07001208 if (SYN_CAP_PALMDETECT(priv->capabilities))
1209 input_report_abs(dev, ABS_TOOL_WIDTH, finger_width);
1210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 input_report_key(dev, BTN_TOOL_FINGER, num_fingers == 1);
Peter Hutterere42b6642008-11-20 15:24:42 -05001212 if (SYN_CAP_MULTIFINGER(priv->capabilities)) {
1213 input_report_key(dev, BTN_TOOL_DOUBLETAP, num_fingers == 2);
1214 input_report_key(dev, BTN_TOOL_TRIPLETAP, num_fingers == 3);
1215 }
1216
Daniel Kurtz3cdfee92011-08-23 23:02:25 -07001217 synaptics_report_buttons(psmouse, &hw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
1219 input_sync(dev);
1220}
1221
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001222static int synaptics_validate_byte(struct psmouse *psmouse,
1223 int idx, unsigned char pkt_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224{
Helge Dellere38de672006-09-10 21:54:39 -04001225 static const unsigned char newabs_mask[] = { 0xC8, 0x00, 0x00, 0xC8, 0x00 };
1226 static const unsigned char newabs_rel_mask[] = { 0xC0, 0x00, 0x00, 0xC0, 0x00 };
1227 static const unsigned char newabs_rslt[] = { 0x80, 0x00, 0x00, 0xC0, 0x00 };
1228 static const unsigned char oldabs_mask[] = { 0xC0, 0x60, 0x00, 0xC0, 0x60 };
1229 static const unsigned char oldabs_rslt[] = { 0xC0, 0x00, 0x00, 0x80, 0x00 };
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001230 const char *packet = psmouse->packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
1232 if (idx < 0 || idx > 4)
1233 return 0;
1234
1235 switch (pkt_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
Dmitry Torokhova62f0d22010-05-19 10:39:17 -07001237 case SYN_NEWABS:
1238 case SYN_NEWABS_RELAXED:
1239 return (packet[idx] & newabs_rel_mask[idx]) == newabs_rslt[idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
Dmitry Torokhova62f0d22010-05-19 10:39:17 -07001241 case SYN_NEWABS_STRICT:
1242 return (packet[idx] & newabs_mask[idx]) == newabs_rslt[idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
Dmitry Torokhova62f0d22010-05-19 10:39:17 -07001244 case SYN_OLDABS:
1245 return (packet[idx] & oldabs_mask[idx]) == oldabs_rslt[idx];
1246
1247 default:
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001248 psmouse_err(psmouse, "unknown packet type %d\n", pkt_type);
Dmitry Torokhova62f0d22010-05-19 10:39:17 -07001249 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 }
1251}
1252
1253static unsigned char synaptics_detect_pkt_type(struct psmouse *psmouse)
1254{
1255 int i;
1256
1257 for (i = 0; i < 5; i++)
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001258 if (!synaptics_validate_byte(psmouse, i, SYN_NEWABS_STRICT)) {
1259 psmouse_info(psmouse, "using relaxed packet validation\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 return SYN_NEWABS_RELAXED;
1261 }
1262
1263 return SYN_NEWABS_STRICT;
1264}
1265
David Howells7d12e782006-10-05 14:55:46 +01001266static psmouse_ret_t synaptics_process_byte(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 struct synaptics_data *priv = psmouse->private;
1269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 if (psmouse->pktcnt >= 6) { /* Full packet received */
1271 if (unlikely(priv->pkt_type == SYN_NEWABS))
1272 priv->pkt_type = synaptics_detect_pkt_type(psmouse);
1273
Dmitry Torokhova8b3c0f2010-10-04 21:46:10 -07001274 if (SYN_CAP_PASS_THROUGH(priv->capabilities) &&
1275 synaptics_is_pt_packet(psmouse->packet)) {
1276 if (priv->pt_port)
1277 synaptics_pass_pt_packet(priv->pt_port, psmouse->packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 } else
1279 synaptics_process_packet(psmouse);
1280
1281 return PSMOUSE_FULL_PACKET;
1282 }
1283
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001284 return synaptics_validate_byte(psmouse, psmouse->pktcnt - 1, priv->pkt_type) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 PSMOUSE_GOOD_DATA : PSMOUSE_BAD_DATA;
1286}
1287
1288/*****************************************************************************
1289 * Driver initialization/cleanup functions
1290 ****************************************************************************/
Daniel Kurtz85615472011-08-23 23:00:41 -07001291static void set_abs_position_params(struct input_dev *dev,
1292 struct synaptics_data *priv, int x_code,
1293 int y_code)
1294{
1295 int x_min = priv->x_min ?: XMIN_NOMINAL;
1296 int x_max = priv->x_max ?: XMAX_NOMINAL;
1297 int y_min = priv->y_min ?: YMIN_NOMINAL;
1298 int y_max = priv->y_max ?: YMAX_NOMINAL;
1299 int fuzz = SYN_CAP_REDUCED_FILTERING(priv->ext_cap_0c) ?
1300 SYN_REDUCED_FILTER_FUZZ : 0;
1301
1302 input_set_abs_params(dev, x_code, x_min, x_max, fuzz, 0);
1303 input_set_abs_params(dev, y_code, y_min, y_max, fuzz, 0);
1304 input_abs_set_res(dev, x_code, priv->x_res);
1305 input_abs_set_res(dev, y_code, priv->y_res);
1306}
1307
Hans de Goede43e19882014-04-19 22:26:41 -07001308static void set_input_params(struct psmouse *psmouse,
1309 struct synaptics_data *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310{
Hans de Goede43e19882014-04-19 22:26:41 -07001311 struct input_dev *dev = psmouse->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 int i;
1313
Daniel Drake7968a5d2011-11-08 00:00:35 -08001314 /* Things that apply to both modes */
Henrik Rydbergc14890a2010-12-16 09:52:23 +01001315 __set_bit(INPUT_PROP_POINTER, dev->propbit);
Daniel Drake7968a5d2011-11-08 00:00:35 -08001316 __set_bit(EV_KEY, dev->evbit);
1317 __set_bit(BTN_LEFT, dev->keybit);
1318 __set_bit(BTN_RIGHT, dev->keybit);
Henrik Rydbergc14890a2010-12-16 09:52:23 +01001319
Daniel Drake7968a5d2011-11-08 00:00:35 -08001320 if (SYN_CAP_MIDDLE_BUTTON(priv->capabilities))
1321 __set_bit(BTN_MIDDLE, dev->keybit);
1322
1323 if (!priv->absolute_mode) {
1324 /* Relative mode */
1325 __set_bit(EV_REL, dev->evbit);
1326 __set_bit(REL_X, dev->relbit);
1327 __set_bit(REL_Y, dev->relbit);
1328 return;
1329 }
1330
1331 /* Absolute mode */
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001332 __set_bit(EV_ABS, dev->evbit);
Daniel Kurtz85615472011-08-23 23:00:41 -07001333 set_abs_position_params(dev, priv, ABS_X, ABS_Y);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 input_set_abs_params(dev, ABS_PRESSURE, 0, 255, 0, 0);
Chris Bagwell2a8e7712010-07-19 09:06:15 -07001335
Daniel Kurtz3cdfee92011-08-23 23:02:25 -07001336 if (SYN_CAP_IMAGE_SENSOR(priv->ext_cap_0c)) {
Daniel Kurtz3cdfee92011-08-23 23:02:25 -07001337 set_abs_position_params(dev, priv, ABS_MT_POSITION_X,
1338 ABS_MT_POSITION_Y);
1339 /* Image sensors can report per-contact pressure */
1340 input_set_abs_params(dev, ABS_MT_PRESSURE, 0, 255, 0, 0);
Henrik Rydberg0b85bf72013-02-15 17:04:03 -08001341 input_mt_init_slots(dev, 2, INPUT_MT_POINTER);
Daniel Kurtz6b4b49f2011-08-23 23:02:56 -07001342
1343 /* Image sensors can signal 4 and 5 finger clicks */
1344 __set_bit(BTN_TOOL_QUADTAP, dev->keybit);
1345 __set_bit(BTN_TOOL_QUINTTAP, dev->keybit);
Daniel Kurtz3cdfee92011-08-23 23:02:25 -07001346 } else if (SYN_CAP_ADV_GESTURE(priv->ext_cap_0c)) {
1347 /* Non-image sensors with AGM use semi-mt */
Henrik Rydbergfec6e522010-12-21 18:11:25 +01001348 __set_bit(INPUT_PROP_SEMI_MT, dev->propbit);
Henrik Rydbergb4adbbe2012-08-11 22:07:55 +02001349 input_mt_init_slots(dev, 2, 0);
Daniel Kurtz85615472011-08-23 23:00:41 -07001350 set_abs_position_params(dev, priv, ABS_MT_POSITION_X,
1351 ABS_MT_POSITION_Y);
Henrik Rydbergfec6e522010-12-21 18:11:25 +01001352 }
1353
Chris Bagwell2a8e7712010-07-19 09:06:15 -07001354 if (SYN_CAP_PALMDETECT(priv->capabilities))
Chris Bagwell58fb0212010-07-19 09:06:15 -07001355 input_set_abs_params(dev, ABS_TOOL_WIDTH, 0, 15, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001357 __set_bit(BTN_TOUCH, dev->keybit);
1358 __set_bit(BTN_TOOL_FINGER, dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
Peter Hutterere42b6642008-11-20 15:24:42 -05001360 if (SYN_CAP_MULTIFINGER(priv->capabilities)) {
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001361 __set_bit(BTN_TOOL_DOUBLETAP, dev->keybit);
1362 __set_bit(BTN_TOOL_TRIPLETAP, dev->keybit);
Peter Hutterere42b6642008-11-20 15:24:42 -05001363 }
1364
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 if (SYN_CAP_FOUR_BUTTON(priv->capabilities) ||
1366 SYN_CAP_MIDDLE_BUTTON(priv->capabilities)) {
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001367 __set_bit(BTN_FORWARD, dev->keybit);
1368 __set_bit(BTN_BACK, dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 }
1370
1371 for (i = 0; i < SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap); i++)
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001372 __set_bit(BTN_0 + i, dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001374 __clear_bit(EV_REL, dev->evbit);
1375 __clear_bit(REL_X, dev->relbit);
1376 __clear_bit(REL_Y, dev->relbit);
Tero Saarniec20a022009-06-10 23:27:24 -07001377
Takashi Iwai5f57d672010-04-19 10:37:21 -07001378 if (SYN_CAP_CLICKPAD(priv->ext_cap_0c)) {
Henrik Rydbergc14890a2010-12-16 09:52:23 +01001379 __set_bit(INPUT_PROP_BUTTONPAD, dev->propbit);
Hans de Goedee2f61102014-05-19 22:53:23 -07001380 if (matches_pnp_id(psmouse, topbuttonpad_pnp_ids))
1381 __set_bit(INPUT_PROP_TOPBUTTONPAD, dev->propbit);
Takashi Iwai5f57d672010-04-19 10:37:21 -07001382 /* Clickpads report only left button */
1383 __clear_bit(BTN_RIGHT, dev->keybit);
1384 __clear_bit(BTN_MIDDLE, dev->keybit);
1385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386}
1387
Daniel Drake7968a5d2011-11-08 00:00:35 -08001388static ssize_t synaptics_show_disable_gesture(struct psmouse *psmouse,
1389 void *data, char *buf)
1390{
1391 struct synaptics_data *priv = psmouse->private;
1392
1393 return sprintf(buf, "%c\n", priv->disable_gesture ? '1' : '0');
1394}
1395
1396static ssize_t synaptics_set_disable_gesture(struct psmouse *psmouse,
1397 void *data, const char *buf,
1398 size_t len)
1399{
1400 struct synaptics_data *priv = psmouse->private;
1401 unsigned int value;
1402 int err;
1403
1404 err = kstrtouint(buf, 10, &value);
1405 if (err)
1406 return err;
1407
1408 if (value > 1)
1409 return -EINVAL;
1410
1411 if (value == priv->disable_gesture)
1412 return len;
1413
1414 priv->disable_gesture = value;
1415 if (value)
1416 priv->mode |= SYN_BIT_DISABLE_GESTURE;
1417 else
1418 priv->mode &= ~SYN_BIT_DISABLE_GESTURE;
1419
1420 if (synaptics_mode_cmd(psmouse, priv->mode))
1421 return -EIO;
1422
1423 return len;
1424}
1425
1426PSMOUSE_DEFINE_ATTR(disable_gesture, S_IWUSR | S_IRUGO, NULL,
1427 synaptics_show_disable_gesture,
1428 synaptics_set_disable_gesture);
1429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430static void synaptics_disconnect(struct psmouse *psmouse)
1431{
Daniel Drake7968a5d2011-11-08 00:00:35 -08001432 struct synaptics_data *priv = psmouse->private;
1433
1434 if (!priv->absolute_mode && SYN_ID_DISGEST_SUPPORTED(priv->identity))
1435 device_remove_file(&psmouse->ps2dev.serio->dev,
1436 &psmouse_attr_disable_gesture.dattr);
1437
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 synaptics_reset(psmouse);
Daniel Drake7968a5d2011-11-08 00:00:35 -08001439 kfree(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 psmouse->private = NULL;
1441}
1442
1443static int synaptics_reconnect(struct psmouse *psmouse)
1444{
1445 struct synaptics_data *priv = psmouse->private;
1446 struct synaptics_data old_priv = *priv;
Eric Miaoeeb06552013-06-04 09:30:55 -07001447 unsigned char param[2];
Alexandre Peixoto Ferreirac63fe0a2011-01-28 22:05:14 -08001448 int retry = 0;
1449 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Alexandre Peixoto Ferreirac63fe0a2011-01-28 22:05:14 -08001451 do {
1452 psmouse_reset(psmouse);
Dmitry Torokhov85214782011-12-12 00:05:53 -08001453 if (retry) {
1454 /*
1455 * On some boxes, right after resuming, the touchpad
1456 * needs some time to finish initializing (I assume
1457 * it needs time to calibrate) and start responding
1458 * to Synaptics-specific queries, so let's wait a
1459 * bit.
1460 */
1461 ssleep(1);
1462 }
Eric Miaoeeb06552013-06-04 09:30:55 -07001463 ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_GETID);
Alexandre Peixoto Ferreirac63fe0a2011-01-28 22:05:14 -08001464 error = synaptics_detect(psmouse, 0);
1465 } while (error && ++retry < 3);
Andy Whitcroft4d368452009-02-28 12:51:01 -08001466
Alexandre Peixoto Ferreirac63fe0a2011-01-28 22:05:14 -08001467 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 return -1;
1469
Alexandre Peixoto Ferreirac63fe0a2011-01-28 22:05:14 -08001470 if (retry > 1)
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001471 psmouse_dbg(psmouse, "reconnected after %d tries\n", retry);
Alexandre Peixoto Ferreirac63fe0a2011-01-28 22:05:14 -08001472
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 if (synaptics_query_hardware(psmouse)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001474 psmouse_err(psmouse, "Unable to query device.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 return -1;
1476 }
1477
Daniel Drake7968a5d2011-11-08 00:00:35 -08001478 if (synaptics_set_mode(psmouse)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001479 psmouse_err(psmouse, "Unable to initialize device.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 return -1;
1481 }
1482
Alexandre Peixoto Ferreirabaddf582011-01-28 22:05:14 -08001483 if (old_priv.identity != priv->identity ||
1484 old_priv.model_id != priv->model_id ||
1485 old_priv.capabilities != priv->capabilities ||
1486 old_priv.ext_cap != priv->ext_cap) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001487 psmouse_err(psmouse,
1488 "hardware appears to be different: id(%ld-%ld), model(%ld-%ld), caps(%lx-%lx), ext(%lx-%lx).\n",
1489 old_priv.identity, priv->identity,
1490 old_priv.model_id, priv->model_id,
1491 old_priv.capabilities, priv->capabilities,
1492 old_priv.ext_cap, priv->ext_cap);
Alexandre Peixoto Ferreirabaddf582011-01-28 22:05:14 -08001493 return -1;
1494 }
1495
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 return 0;
1497}
1498
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001499static bool impaired_toshiba_kbc;
1500
Sachin Kamatc9631562013-08-12 11:05:58 -07001501static const struct dmi_system_id toshiba_dmi_table[] __initconst = {
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001502#if defined(CONFIG_DMI) && defined(CONFIG_X86)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 {
Dmitry Torokhov9961e252009-12-04 10:24:20 -08001504 /* Toshiba Satellite */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 .matches = {
1506 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
Richard Thrippleton53a26702006-04-02 00:10:18 -05001507 DMI_MATCH(DMI_PRODUCT_NAME, "Satellite"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 },
1509 },
Simon Horman9ba5eaa2005-07-11 01:07:20 -05001510 {
Dmitry Torokhov9961e252009-12-04 10:24:20 -08001511 /* Toshiba Dynabook */
Simon Horman9ba5eaa2005-07-11 01:07:20 -05001512 .matches = {
1513 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
Richard Thrippleton53a26702006-04-02 00:10:18 -05001514 DMI_MATCH(DMI_PRODUCT_NAME, "dynabook"),
1515 },
1516 },
1517 {
Dmitry Torokhov9961e252009-12-04 10:24:20 -08001518 /* Toshiba Portege M300 */
Richard Thrippleton53a26702006-04-02 00:10:18 -05001519 .matches = {
1520 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1521 DMI_MATCH(DMI_PRODUCT_NAME, "PORTEGE M300"),
Simon Horman9ba5eaa2005-07-11 01:07:20 -05001522 },
Dmitry Torokhov5f5eeff2009-10-12 21:35:00 -07001523
1524 },
1525 {
Dmitry Torokhov9961e252009-12-04 10:24:20 -08001526 /* Toshiba Portege M300 */
Dmitry Torokhov5f5eeff2009-10-12 21:35:00 -07001527 .matches = {
1528 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1529 DMI_MATCH(DMI_PRODUCT_NAME, "Portable PC"),
1530 DMI_MATCH(DMI_PRODUCT_VERSION, "Version 1.0"),
1531 },
1532
Simon Horman9ba5eaa2005-07-11 01:07:20 -05001533 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534#endif
Jan Beulich70874862011-03-31 00:01:58 -07001535 { }
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001536};
1537
Andres Salomonef8313b2010-12-23 01:19:38 -08001538static bool broken_olpc_ec;
1539
Sachin Kamatc9631562013-08-12 11:05:58 -07001540static const struct dmi_system_id olpc_dmi_table[] __initconst = {
Andres Salomonef8313b2010-12-23 01:19:38 -08001541#if defined(CONFIG_DMI) && defined(CONFIG_OLPC)
1542 {
1543 /* OLPC XO-1 or XO-1.5 */
1544 .matches = {
1545 DMI_MATCH(DMI_SYS_VENDOR, "OLPC"),
1546 DMI_MATCH(DMI_PRODUCT_NAME, "XO"),
1547 },
1548 },
Andres Salomonef8313b2010-12-23 01:19:38 -08001549#endif
Jan Beulich70874862011-03-31 00:01:58 -07001550 { }
Andres Salomonef8313b2010-12-23 01:19:38 -08001551};
1552
Benjamin Tissoires421e08c2014-03-28 00:43:00 -07001553static const struct dmi_system_id min_max_dmi_table[] __initconst = {
1554#if defined(CONFIG_DMI)
1555 {
1556 /* Lenovo ThinkPad Helix */
1557 .matches = {
1558 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1559 DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad Helix"),
1560 },
1561 .driver_data = (int []){1024, 5052, 2258, 4832},
1562 },
1563 {
Hans de Goede8a0435d2014-03-28 01:01:38 -07001564 /* Lenovo ThinkPad X240 */
1565 .matches = {
1566 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1567 DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad X240"),
1568 },
1569 .driver_data = (int []){1232, 5710, 1156, 4696},
1570 },
1571 {
Hans de Goede27a38852014-04-23 13:02:35 -07001572 /* Lenovo ThinkPad Edge E431 */
1573 .matches = {
1574 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1575 DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad Edge E431"),
1576 },
1577 .driver_data = (int []){1024, 5022, 2508, 4832},
1578 },
1579 {
Hans de Goede46a29862014-04-19 22:31:18 -07001580 /* Lenovo ThinkPad T431s */
1581 .matches = {
1582 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1583 DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad T431"),
1584 },
1585 .driver_data = (int []){1024, 5112, 2024, 4832},
1586 },
1587 {
Benjamin Tissoires421e08c2014-03-28 00:43:00 -07001588 /* Lenovo ThinkPad T440s */
1589 .matches = {
1590 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1591 DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad T440"),
1592 },
1593 .driver_data = (int []){1024, 5112, 2024, 4832},
1594 },
1595 {
Hans de Goede46a29862014-04-19 22:31:18 -07001596 /* Lenovo ThinkPad L440 */
1597 .matches = {
1598 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1599 DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad L440"),
1600 },
1601 .driver_data = (int []){1024, 5112, 2024, 4832},
1602 },
1603 {
Benjamin Tissoires421e08c2014-03-28 00:43:00 -07001604 /* Lenovo ThinkPad T540p */
1605 .matches = {
1606 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1607 DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad T540"),
1608 },
Hans de Goede6d396ed2014-05-19 22:52:30 -07001609 .driver_data = (int []){1024, 5112, 2024, 4832},
Benjamin Tissoires421e08c2014-03-28 00:43:00 -07001610 },
Hans de Goede46a29862014-04-19 22:31:18 -07001611 {
1612 /* Lenovo ThinkPad L540 */
1613 .matches = {
1614 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1615 DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad L540"),
1616 },
1617 .driver_data = (int []){1024, 5112, 2024, 4832},
1618 },
1619 {
Hans de Goede0b5fe732014-05-14 11:10:40 -07001620 /* Lenovo ThinkPad W540 */
1621 .matches = {
1622 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1623 DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad W540"),
1624 },
1625 .driver_data = (int []){1024, 5112, 2024, 4832},
1626 },
1627 {
Hans de Goede46a29862014-04-19 22:31:18 -07001628 /* Lenovo Yoga S1 */
1629 .matches = {
1630 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1631 DMI_EXACT_MATCH(DMI_PRODUCT_VERSION,
1632 "ThinkPad S1 Yoga"),
1633 },
1634 .driver_data = (int []){1232, 5710, 1156, 4696},
1635 },
1636 {
1637 /* Lenovo ThinkPad X1 Carbon Haswell (3rd generation) */
1638 .matches = {
1639 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1640 DMI_MATCH(DMI_PRODUCT_VERSION,
1641 "ThinkPad X1 Carbon 2nd"),
1642 },
1643 .driver_data = (int []){1024, 5112, 2024, 4832},
1644 },
Benjamin Tissoires421e08c2014-03-28 00:43:00 -07001645#endif
1646 { }
1647};
1648
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001649void __init synaptics_module_init(void)
1650{
Benjamin Tissoires421e08c2014-03-28 00:43:00 -07001651 const struct dmi_system_id *min_max_dmi;
1652
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001653 impaired_toshiba_kbc = dmi_check_system(toshiba_dmi_table);
Andres Salomonef8313b2010-12-23 01:19:38 -08001654 broken_olpc_ec = dmi_check_system(olpc_dmi_table);
Benjamin Tissoires421e08c2014-03-28 00:43:00 -07001655
1656 min_max_dmi = dmi_first_match(min_max_dmi_table);
1657 if (min_max_dmi)
1658 quirk_min_max = min_max_dmi->driver_data;
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001659}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
Daniel Drake7968a5d2011-11-08 00:00:35 -08001661static int __synaptics_init(struct psmouse *psmouse, bool absolute_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662{
1663 struct synaptics_data *priv;
Daniel Drake7968a5d2011-11-08 00:00:35 -08001664 int err = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
Andres Salomonef8313b2010-12-23 01:19:38 -08001666 /*
Daniel Drake83551c02011-11-11 16:05:04 -08001667 * The OLPC XO has issues with Synaptics' absolute mode; the constant
1668 * packet spew overloads the EC such that key presses on the keyboard
1669 * are missed. Given that, don't even attempt to use Absolute mode.
1670 * Relative mode seems to work just fine.
Andres Salomonef8313b2010-12-23 01:19:38 -08001671 */
Daniel Drake83551c02011-11-11 16:05:04 -08001672 if (absolute_mode && broken_olpc_ec) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001673 psmouse_info(psmouse,
1674 "OLPC XO detected, not enabling Synaptics protocol.\n");
Andres Salomonef8313b2010-12-23 01:19:38 -08001675 return -ENODEV;
1676 }
1677
Eric Sesterhennb39787a2006-03-14 00:09:16 -05001678 psmouse->private = priv = kzalloc(sizeof(struct synaptics_data), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 if (!priv)
Davidlohr Bueso6792cbb2010-09-29 18:53:35 -07001680 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
Andy Whitcroft4d368452009-02-28 12:51:01 -08001682 psmouse_reset(psmouse);
1683
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 if (synaptics_query_hardware(psmouse)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001685 psmouse_err(psmouse, "Unable to query device.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 goto init_fail;
1687 }
1688
Daniel Drake7968a5d2011-11-08 00:00:35 -08001689 priv->absolute_mode = absolute_mode;
1690 if (SYN_ID_DISGEST_SUPPORTED(priv->identity))
1691 priv->disable_gesture = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
Daniel Drake7968a5d2011-11-08 00:00:35 -08001693 if (synaptics_set_mode(psmouse)) {
1694 psmouse_err(psmouse, "Unable to initialize device.\n");
Henrik Rydbergfec6e522010-12-21 18:11:25 +01001695 goto init_fail;
1696 }
1697
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 priv->pkt_type = SYN_MODEL_NEWABS(priv->model_id) ? SYN_NEWABS : SYN_OLDABS;
1699
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001700 psmouse_info(psmouse,
Daniel Kurtzc6bd9d42012-07-07 18:08:51 -07001701 "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 -07001702 SYN_ID_MODEL(priv->identity),
1703 SYN_ID_MAJOR(priv->identity), SYN_ID_MINOR(priv->identity),
1704 priv->model_id,
Daniel Kurtzc6bd9d42012-07-07 18:08:51 -07001705 priv->capabilities, priv->ext_cap, priv->ext_cap_0c,
1706 priv->board_id, priv->firmware_id);
Dmitry Torokhov409b7502005-05-28 02:12:18 -05001707
Hans de Goede43e19882014-04-19 22:26:41 -07001708 set_input_params(psmouse, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
Dmitry Torokhov887cc122007-04-12 01:30:41 -04001710 /*
1711 * Encode touchpad model so that it can be used to set
1712 * input device->id.version and be visible to userspace.
1713 * Because version is __u16 we have to drop something.
1714 * Hardware info bits seem to be good candidates as they
1715 * are documented to be for Synaptics corp. internal use.
1716 */
1717 psmouse->model = ((priv->model_id & 0x00ff0000) >> 8) |
1718 (priv->model_id & 0x000000ff);
1719
Daniel Drake7968a5d2011-11-08 00:00:35 -08001720 if (absolute_mode) {
1721 psmouse->protocol_handler = synaptics_process_byte;
1722 psmouse->pktsize = 6;
1723 } else {
1724 /* Relative mode follows standard PS/2 mouse protocol */
1725 psmouse->protocol_handler = psmouse_process_byte;
1726 psmouse->pktsize = 3;
1727 }
1728
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 psmouse->set_rate = synaptics_set_rate;
1730 psmouse->disconnect = synaptics_disconnect;
1731 psmouse->reconnect = synaptics_reconnect;
Dmitry Torokhova1cec062007-02-18 01:40:24 -05001732 psmouse->cleanup = synaptics_reset;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001733 /* Synaptics can usually stay in sync without extra help */
1734 psmouse->resync_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
1736 if (SYN_CAP_PASS_THROUGH(priv->capabilities))
1737 synaptics_pt_create(psmouse);
1738
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 /*
1740 * Toshiba's KBC seems to have trouble handling data from
Andres Salomon7ee99162010-12-23 01:18:28 -08001741 * Synaptics at full rate. Switch to a lower rate (roughly
1742 * the same rate as a standard PS/2 mouse).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 */
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001744 if (psmouse->rate >= 80 && impaired_toshiba_kbc) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001745 psmouse_info(psmouse,
1746 "Toshiba %s detected, limiting rate to 40pps.\n",
1747 dmi_get_system_info(DMI_PRODUCT_NAME));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 psmouse->rate = 40;
1749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
Daniel Drake7968a5d2011-11-08 00:00:35 -08001751 if (!priv->absolute_mode && SYN_ID_DISGEST_SUPPORTED(priv->identity)) {
1752 err = device_create_file(&psmouse->ps2dev.serio->dev,
1753 &psmouse_attr_disable_gesture.dattr);
1754 if (err) {
1755 psmouse_err(psmouse,
1756 "Failed to create disable_gesture attribute (%d)",
1757 err);
1758 goto init_fail;
1759 }
1760 }
1761
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 return 0;
1763
1764 init_fail:
1765 kfree(priv);
Daniel Drake7968a5d2011-11-08 00:00:35 -08001766 return err;
1767}
1768
1769int synaptics_init(struct psmouse *psmouse)
1770{
1771 return __synaptics_init(psmouse, true);
1772}
1773
1774int synaptics_init_relative(struct psmouse *psmouse)
1775{
1776 return __synaptics_init(psmouse, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777}
1778
Daniel Drakee4e6efd2010-01-07 01:52:39 -08001779bool synaptics_supported(void)
1780{
1781 return true;
1782}
1783
Andres Salomon55e3d922007-03-10 01:39:54 -05001784#else /* CONFIG_MOUSE_PS2_SYNAPTICS */
1785
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001786void __init synaptics_module_init(void)
1787{
1788}
1789
Andres Salomon55e3d922007-03-10 01:39:54 -05001790int synaptics_init(struct psmouse *psmouse)
1791{
1792 return -ENOSYS;
1793}
1794
Daniel Drakee4e6efd2010-01-07 01:52:39 -08001795bool synaptics_supported(void)
1796{
1797 return false;
1798}
1799
Andres Salomon55e3d922007-03-10 01:39:54 -05001800#endif /* CONFIG_MOUSE_PS2_SYNAPTICS */