blob: 3c54b9b0f65f16440ecc3d378439436626a6d313 [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
Benjamin Tissoires58fd9af2015-04-05 13:44:12 -070070/* maximum ABS_MT_POSITION displacement (in mm) */
71#define DMAX 10
72
Andres Salomon55e3d922007-03-10 01:39:54 -050073/*****************************************************************************
74 * Stuff we need even when we do not want native Synaptics support
75 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77/*
78 * Set the synaptics touchpad mode byte by special commands
79 */
80static int synaptics_mode_cmd(struct psmouse *psmouse, unsigned char mode)
81{
82 unsigned char param[1];
83
84 if (psmouse_sliced_command(psmouse, mode))
85 return -1;
86 param[0] = SYN_PS_SET_MODE2;
87 if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_SETRATE))
88 return -1;
89 return 0;
90}
91
Dmitry Torokhovb7802c52009-09-09 19:13:20 -070092int synaptics_detect(struct psmouse *psmouse, bool set_properties)
Andres Salomon55e3d922007-03-10 01:39:54 -050093{
94 struct ps2dev *ps2dev = &psmouse->ps2dev;
95 unsigned char param[4];
96
97 param[0] = 0;
98
99 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
100 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
101 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
102 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
103 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO);
104
105 if (param[1] != 0x47)
106 return -ENODEV;
107
108 if (set_properties) {
109 psmouse->vendor = "Synaptics";
110 psmouse->name = "TouchPad";
111 }
112
113 return 0;
114}
115
116void synaptics_reset(struct psmouse *psmouse)
117{
118 /* reset touchpad back to relative mode, gestures enabled */
119 synaptics_mode_cmd(psmouse, 0);
120}
121
122#ifdef CONFIG_MOUSE_PS2_SYNAPTICS
Henrik Rydberge08d9af2014-07-14 10:26:56 -0700123
124static bool cr48_profile_sensor;
125
Daniel Martin5b3089d2015-03-08 22:29:15 -0700126#define ANY_BOARD_ID 0
Hans de Goede0f68f392014-05-19 22:54:09 -0700127struct min_max_quirk {
128 const char * const *pnp_ids;
Daniel Martin5b3089d2015-03-08 22:29:15 -0700129 struct {
130 unsigned long int min, max;
131 } board_id;
Hans de Goede0f68f392014-05-19 22:54:09 -0700132 int x_min, x_max, y_min, y_max;
133};
134
135static const struct min_max_quirk min_max_pnpid_table[] = {
136 {
137 (const char * const []){"LEN0033", NULL},
Daniel Martin5b3089d2015-03-08 22:29:15 -0700138 {ANY_BOARD_ID, ANY_BOARD_ID},
Hans de Goede0f68f392014-05-19 22:54:09 -0700139 1024, 5052, 2258, 4832
140 },
141 {
Daniel Martinb05f4d12015-03-08 22:29:07 -0700142 (const char * const []){"LEN0042", NULL},
Daniel Martin5b3089d2015-03-08 22:29:15 -0700143 {ANY_BOARD_ID, ANY_BOARD_ID},
Hans de Goede0f68f392014-05-19 22:54:09 -0700144 1232, 5710, 1156, 4696
145 },
146 {
Peter Hutterer8543cf12015-01-19 16:29:25 -0800147 (const char * const []){"LEN0034", "LEN0036", "LEN0037",
148 "LEN0039", "LEN2002", "LEN2004",
149 NULL},
Benjamin Tissoires02e07492015-03-08 22:29:25 -0700150 {ANY_BOARD_ID, 2961},
Hans de Goede0f68f392014-05-19 22:54:09 -0700151 1024, 5112, 2024, 4832
152 },
153 {
154 (const char * const []){"LEN2001", NULL},
Daniel Martin5b3089d2015-03-08 22:29:15 -0700155 {ANY_BOARD_ID, ANY_BOARD_ID},
Hans de Goede0f68f392014-05-19 22:54:09 -0700156 1024, 5022, 2508, 4832
157 },
Ben Sagalbce4f9e2014-11-16 17:23:40 -0800158 {
159 (const char * const []){"LEN2006", NULL},
Daniel Martin5b3089d2015-03-08 22:29:15 -0700160 {ANY_BOARD_ID, ANY_BOARD_ID},
Ben Sagalbce4f9e2014-11-16 17:23:40 -0800161 1264, 5675, 1171, 4688
162 },
Hans de Goede0f68f392014-05-19 22:54:09 -0700163 { }
164};
165
Hans de Goede43e19882014-04-19 22:26:41 -0700166/* This list has been kindly provided by Synaptics. */
167static const char * const topbuttonpad_pnp_ids[] = {
168 "LEN0017",
169 "LEN0018",
170 "LEN0019",
171 "LEN0023",
172 "LEN002A",
173 "LEN002B",
174 "LEN002C",
175 "LEN002D",
176 "LEN002E",
177 "LEN0033", /* Helix */
Hans de Goede0f68f392014-05-19 22:54:09 -0700178 "LEN0034", /* T431s, L440, L540, T540, W540, X1 Carbon 2nd */
Hans de Goede43e19882014-04-19 22:26:41 -0700179 "LEN0035", /* X240 */
180 "LEN0036", /* T440 */
Peter Hutterer8543cf12015-01-19 16:29:25 -0800181 "LEN0037", /* X1 Carbon 2nd */
Hans de Goede43e19882014-04-19 22:26:41 -0700182 "LEN0038",
Takashi Iwaie4742b12014-11-06 09:27:11 -0800183 "LEN0039", /* T440s */
Hans de Goede43e19882014-04-19 22:26:41 -0700184 "LEN0041",
185 "LEN0042", /* Yoga */
186 "LEN0045",
Hans de Goede43e19882014-04-19 22:26:41 -0700187 "LEN0047",
Hans de Goede43e19882014-04-19 22:26:41 -0700188 "LEN0049",
189 "LEN2000",
Hans de Goede0f68f392014-05-19 22:54:09 -0700190 "LEN2001", /* Edge E431 */
Hans de Goedee76aed92014-07-14 17:12:21 -0700191 "LEN2002", /* Edge E531 */
Hans de Goede43e19882014-04-19 22:26:41 -0700192 "LEN2003",
193 "LEN2004", /* L440 */
194 "LEN2005",
195 "LEN2006",
196 "LEN2007",
197 "LEN2008",
198 "LEN2009",
199 "LEN200A",
200 "LEN200B",
201 NULL
202};
Andres Salomon55e3d922007-03-10 01:39:54 -0500203
Dmitry Torokhovde4e3742014-12-29 14:43:44 -0800204/* This list has been kindly provided by Synaptics. */
205static const char * const forcepad_pnp_ids[] = {
206 "SYN300D",
207 "SYN3014",
208 NULL
209};
210
Andres Salomon55e3d922007-03-10 01:39:54 -0500211/*****************************************************************************
212 * Synaptics communications functions
213 ****************************************************************************/
214
215/*
JJ Ding1a49a0a2012-05-10 22:32:00 -0700216 * Synaptics touchpads report the y coordinate from bottom to top, which is
217 * opposite from what userspace expects.
218 * This function is used to invert y before reporting.
219 */
220static int synaptics_invert_y(int y)
221{
222 return YMAX_NOMINAL + YMIN_NOMINAL - y;
223}
224
225/*
Andres Salomon55e3d922007-03-10 01:39:54 -0500226 * Send a command to the synpatics touchpad by special commands
227 */
228static int synaptics_send_cmd(struct psmouse *psmouse, unsigned char c, unsigned char *param)
229{
230 if (psmouse_sliced_command(psmouse, c))
231 return -1;
232 if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_GETINFO))
233 return -1;
234 return 0;
235}
236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237/*
238 * Read the model-id bytes from the touchpad
239 * see also SYN_MODEL_* macros
240 */
241static int synaptics_model_id(struct psmouse *psmouse)
242{
243 struct synaptics_data *priv = psmouse->private;
244 unsigned char mi[3];
245
246 if (synaptics_send_cmd(psmouse, SYN_QUE_MODEL, mi))
247 return -1;
248 priv->model_id = (mi[0]<<16) | (mi[1]<<8) | mi[2];
249 return 0;
250}
251
Benjamin Tissoires06aa3742015-03-08 22:34:03 -0700252static int synaptics_more_extended_queries(struct psmouse *psmouse)
253{
254 struct synaptics_data *priv = psmouse->private;
255 unsigned char buf[3];
256
257 if (synaptics_send_cmd(psmouse, SYN_QUE_MEXT_CAPAB_10, buf))
258 return -1;
259
260 priv->ext_cap_10 = (buf[0]<<16) | (buf[1]<<8) | buf[2];
261
262 return 0;
263}
264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265/*
Benjamin Tissoires06aa3742015-03-08 22:34:03 -0700266 * Read the board id and the "More Extended Queries" from the touchpad
Daniel Kurtzc6bd9d42012-07-07 18:08:51 -0700267 * The board id is encoded in the "QUERY MODES" response
268 */
Benjamin Tissoires06aa3742015-03-08 22:34:03 -0700269static int synaptics_query_modes(struct psmouse *psmouse)
Daniel Kurtzc6bd9d42012-07-07 18:08:51 -0700270{
271 struct synaptics_data *priv = psmouse->private;
272 unsigned char bid[3];
273
Benjamin Tissoiresb57a7122015-03-08 22:33:36 -0700274 /* firmwares prior 7.5 have no board_id encoded */
275 if (SYN_ID_FULL(priv->identity) < 0x705)
276 return 0;
277
Daniel Kurtzc6bd9d42012-07-07 18:08:51 -0700278 if (synaptics_send_cmd(psmouse, SYN_QUE_MODES, bid))
279 return -1;
280 priv->board_id = ((bid[0] & 0xfc) << 6) | bid[1];
Benjamin Tissoires06aa3742015-03-08 22:34:03 -0700281
282 if (SYN_MEXT_CAP_BIT(bid[0]))
283 return synaptics_more_extended_queries(psmouse);
284
Daniel Kurtzc6bd9d42012-07-07 18:08:51 -0700285 return 0;
286}
287
288/*
289 * Read the firmware id from the touchpad
290 */
291static int synaptics_firmware_id(struct psmouse *psmouse)
292{
293 struct synaptics_data *priv = psmouse->private;
294 unsigned char fwid[3];
295
296 if (synaptics_send_cmd(psmouse, SYN_QUE_FIRMWARE_ID, fwid))
297 return -1;
298 priv->firmware_id = (fwid[0] << 16) | (fwid[1] << 8) | fwid[2];
299 return 0;
300}
301
302/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 * Read the capability-bits from the touchpad
304 * see also the SYN_CAP_* macros
305 */
306static int synaptics_capability(struct psmouse *psmouse)
307{
308 struct synaptics_data *priv = psmouse->private;
309 unsigned char cap[3];
310
311 if (synaptics_send_cmd(psmouse, SYN_QUE_CAPABILITIES, cap))
312 return -1;
313 priv->capabilities = (cap[0] << 16) | (cap[1] << 8) | cap[2];
Takashi Iwai5f57d672010-04-19 10:37:21 -0700314 priv->ext_cap = priv->ext_cap_0c = 0;
315
Dmitry Torokhov3619b8f2010-07-21 00:01:19 -0700316 /*
317 * Older firmwares had submodel ID fixed to 0x47
318 */
319 if (SYN_ID_FULL(priv->identity) < 0x705 &&
320 SYN_CAP_SUBMODEL_ID(priv->capabilities) != 0x47) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 return -1;
Dmitry Torokhov3619b8f2010-07-21 00:01:19 -0700322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 /*
325 * Unless capExtended is set the rest of the flags should be ignored
326 */
327 if (!SYN_CAP_EXTENDED(priv->capabilities))
328 priv->capabilities = 0;
329
330 if (SYN_EXT_CAP_REQUESTS(priv->capabilities) >= 1) {
331 if (synaptics_send_cmd(psmouse, SYN_QUE_EXT_CAPAB, cap)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700332 psmouse_warn(psmouse,
333 "device claims to have extended capabilities, but I'm not able to read them.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 } else {
335 priv->ext_cap = (cap[0] << 16) | (cap[1] << 8) | cap[2];
336
337 /*
338 * if nExtBtn is greater than 8 it should be considered
339 * invalid and treated as 0
340 */
341 if (SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap) > 8)
342 priv->ext_cap &= 0xff0fff;
343 }
344 }
Takashi Iwai5f57d672010-04-19 10:37:21 -0700345
346 if (SYN_EXT_CAP_REQUESTS(priv->capabilities) >= 4) {
347 if (synaptics_send_cmd(psmouse, SYN_QUE_EXT_CAPAB_0C, cap)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700348 psmouse_warn(psmouse,
349 "device claims to have extended capability 0x0c, but I'm not able to read it.\n");
Takashi Iwai5f57d672010-04-19 10:37:21 -0700350 } else {
351 priv->ext_cap_0c = (cap[0] << 16) | (cap[1] << 8) | cap[2];
352 }
353 }
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 return 0;
356}
357
358/*
359 * Identify Touchpad
360 * See also the SYN_ID_* macros
361 */
362static int synaptics_identify(struct psmouse *psmouse)
363{
364 struct synaptics_data *priv = psmouse->private;
365 unsigned char id[3];
366
367 if (synaptics_send_cmd(psmouse, SYN_QUE_IDENTIFY, id))
368 return -1;
369 priv->identity = (id[0]<<16) | (id[1]<<8) | id[2];
370 if (SYN_ID_IS_SYNAPTICS(priv->identity))
371 return 0;
372 return -1;
373}
374
Tero Saarniec20a022009-06-10 23:27:24 -0700375/*
Dmitry Torokhov83ba9ea2010-05-10 23:06:52 -0700376 * Read touchpad resolution and maximum reported coordinates
Tero Saarniec20a022009-06-10 23:27:24 -0700377 * Resolution is left zero if touchpad does not support the query
378 */
Benjamin Tissoires421e08c2014-03-28 00:43:00 -0700379
Tero Saarniec20a022009-06-10 23:27:24 -0700380static int synaptics_resolution(struct psmouse *psmouse)
381{
382 struct synaptics_data *priv = psmouse->private;
Dmitry Torokhova66413f2011-07-09 12:32:56 -0700383 unsigned char resp[3];
Tero Saarniec20a022009-06-10 23:27:24 -0700384
385 if (SYN_ID_MAJOR(priv->identity) < 4)
Takashi Iwaibbddd192010-07-14 09:32:46 -0700386 return 0;
Tero Saarniec20a022009-06-10 23:27:24 -0700387
Dmitry Torokhova66413f2011-07-09 12:32:56 -0700388 if (synaptics_send_cmd(psmouse, SYN_QUE_RESOLUTION, resp) == 0) {
389 if (resp[0] != 0 && (resp[1] & 0x80) && resp[2] != 0) {
390 priv->x_res = resp[0]; /* x resolution in units/mm */
391 priv->y_res = resp[2]; /* y resolution in units/mm */
Dmitry Torokhov83ba9ea2010-05-10 23:06:52 -0700392 }
393 }
Tero Saarniec20a022009-06-10 23:27:24 -0700394
Dmitry Torokhov83ba9ea2010-05-10 23:06:52 -0700395 if (SYN_EXT_CAP_REQUESTS(priv->capabilities) >= 5 &&
396 SYN_CAP_MAX_DIMENSIONS(priv->ext_cap_0c)) {
Dmitry Torokhova66413f2011-07-09 12:32:56 -0700397 if (synaptics_send_cmd(psmouse, SYN_QUE_EXT_MAX_COORDS, resp)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700398 psmouse_warn(psmouse,
399 "device claims to have max coordinates query, but I'm not able to read it.\n");
Dmitry Torokhov83ba9ea2010-05-10 23:06:52 -0700400 } else {
Dmitry Torokhova66413f2011-07-09 12:32:56 -0700401 priv->x_max = (resp[0] << 5) | ((resp[1] & 0x0f) << 1);
402 priv->y_max = (resp[2] << 5) | ((resp[1] & 0xf0) >> 3);
Daniel Martin9aff6592015-03-08 22:28:29 -0700403 psmouse_info(psmouse,
404 "queried max coordinates: x [..%d], y [..%d]\n",
405 priv->x_max, priv->y_max);
Dmitry Torokhova66413f2011-07-09 12:32:56 -0700406 }
407 }
408
Daniel Martinac097932015-03-08 22:28:40 -0700409 if (SYN_CAP_MIN_DIMENSIONS(priv->ext_cap_0c) &&
410 (SYN_EXT_CAP_REQUESTS(priv->capabilities) >= 7 ||
411 /*
412 * Firmware v8.1 does not report proper number of extended
413 * capabilities, but has been proven to report correct min
414 * coordinates.
415 */
416 SYN_ID_FULL(priv->identity) == 0x801)) {
Dmitry Torokhova66413f2011-07-09 12:32:56 -0700417 if (synaptics_send_cmd(psmouse, SYN_QUE_EXT_MIN_COORDS, resp)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700418 psmouse_warn(psmouse,
419 "device claims to have min coordinates query, but I'm not able to read it.\n");
Dmitry Torokhova66413f2011-07-09 12:32:56 -0700420 } else {
421 priv->x_min = (resp[0] << 5) | ((resp[1] & 0x0f) << 1);
422 priv->y_min = (resp[2] << 5) | ((resp[1] & 0xf0) >> 3);
Daniel Martin9aff6592015-03-08 22:28:29 -0700423 psmouse_info(psmouse,
424 "queried min coordinates: x [%d..], y [%d..]\n",
425 priv->x_min, priv->y_min);
Dmitry Torokhov83ba9ea2010-05-10 23:06:52 -0700426 }
Tero Saarniec20a022009-06-10 23:27:24 -0700427 }
428
429 return 0;
430}
431
Daniel Martin8b04bab2015-03-08 22:27:37 -0700432/*
433 * Apply quirk(s) if the hardware matches
434 */
435
436static void synaptics_apply_quirks(struct psmouse *psmouse)
437{
438 struct synaptics_data *priv = psmouse->private;
439 int i;
440
441 for (i = 0; min_max_pnpid_table[i].pnp_ids; i++) {
Daniel Martin5b3089d2015-03-08 22:29:15 -0700442 if (!psmouse_matches_pnp_id(psmouse,
443 min_max_pnpid_table[i].pnp_ids))
444 continue;
445
446 if (min_max_pnpid_table[i].board_id.min != ANY_BOARD_ID &&
447 priv->board_id < min_max_pnpid_table[i].board_id.min)
448 continue;
449
450 if (min_max_pnpid_table[i].board_id.max != ANY_BOARD_ID &&
451 priv->board_id > min_max_pnpid_table[i].board_id.max)
452 continue;
453
454 priv->x_min = min_max_pnpid_table[i].x_min;
455 priv->x_max = min_max_pnpid_table[i].x_max;
456 priv->y_min = min_max_pnpid_table[i].y_min;
457 priv->y_max = min_max_pnpid_table[i].y_max;
458 psmouse_info(psmouse,
459 "quirked min/max coordinates: x [%d..%d], y [%d..%d]\n",
460 priv->x_min, priv->x_max,
461 priv->y_min, priv->y_max);
462 break;
Daniel Martin8b04bab2015-03-08 22:27:37 -0700463 }
464}
465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466static int synaptics_query_hardware(struct psmouse *psmouse)
467{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 if (synaptics_identify(psmouse))
469 return -1;
470 if (synaptics_model_id(psmouse))
471 return -1;
Daniel Kurtzc6bd9d42012-07-07 18:08:51 -0700472 if (synaptics_firmware_id(psmouse))
473 return -1;
Benjamin Tissoires06aa3742015-03-08 22:34:03 -0700474 if (synaptics_query_modes(psmouse))
Daniel Kurtzc6bd9d42012-07-07 18:08:51 -0700475 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 if (synaptics_capability(psmouse))
477 return -1;
Tero Saarniec20a022009-06-10 23:27:24 -0700478 if (synaptics_resolution(psmouse))
479 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Daniel Martin8b04bab2015-03-08 22:27:37 -0700481 synaptics_apply_quirks(psmouse);
482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 return 0;
484}
485
Daniel Drake7968a5d2011-11-08 00:00:35 -0800486static int synaptics_set_advanced_gesture_mode(struct psmouse *psmouse)
487{
488 static unsigned char param = 0xc8;
489 struct synaptics_data *priv = psmouse->private;
490
Benjamin Herrenschmidt899c6122012-04-20 22:34:49 -0700491 if (!(SYN_CAP_ADV_GESTURE(priv->ext_cap_0c) ||
492 SYN_CAP_IMAGE_SENSOR(priv->ext_cap_0c)))
Daniel Drake7968a5d2011-11-08 00:00:35 -0800493 return 0;
494
495 if (psmouse_sliced_command(psmouse, SYN_QUE_MODEL))
496 return -1;
497
498 if (ps2_command(&psmouse->ps2dev, &param, PSMOUSE_CMD_SETRATE))
499 return -1;
500
501 /* Advanced gesture mode also sends multi finger data */
502 priv->capabilities |= BIT(1);
503
504 return 0;
505}
506
507static int synaptics_set_mode(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
509 struct synaptics_data *priv = psmouse->private;
510
Daniel Drake7968a5d2011-11-08 00:00:35 -0800511 priv->mode = 0;
512 if (priv->absolute_mode)
513 priv->mode |= SYN_BIT_ABSOLUTE_MODE;
514 if (priv->disable_gesture)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 priv->mode |= SYN_BIT_DISABLE_GESTURE;
Daniel Drake7968a5d2011-11-08 00:00:35 -0800516 if (psmouse->rate >= 80)
517 priv->mode |= SYN_BIT_HIGH_RATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 if (SYN_CAP_EXTENDED(priv->capabilities))
519 priv->mode |= SYN_BIT_W_MODE;
520
521 if (synaptics_mode_cmd(psmouse, priv->mode))
522 return -1;
523
Daniel Drake7968a5d2011-11-08 00:00:35 -0800524 if (priv->absolute_mode &&
525 synaptics_set_advanced_gesture_mode(psmouse)) {
526 psmouse_err(psmouse, "Advanced gesture mode init failed.\n");
527 return -1;
528 }
529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 return 0;
531}
532
533static void synaptics_set_rate(struct psmouse *psmouse, unsigned int rate)
534{
535 struct synaptics_data *priv = psmouse->private;
536
537 if (rate >= 80) {
538 priv->mode |= SYN_BIT_HIGH_RATE;
539 psmouse->rate = 80;
540 } else {
541 priv->mode &= ~SYN_BIT_HIGH_RATE;
542 psmouse->rate = 40;
543 }
544
545 synaptics_mode_cmd(psmouse, priv->mode);
546}
547
548/*****************************************************************************
549 * Synaptics pass-through PS/2 port support
550 ****************************************************************************/
551static int synaptics_pt_write(struct serio *serio, unsigned char c)
552{
553 struct psmouse *parent = serio_get_drvdata(serio->parent);
554 char rate_param = SYN_PS_CLIENT_CMD; /* indicates that we want pass-through port */
555
556 if (psmouse_sliced_command(parent, c))
557 return -1;
558 if (ps2_command(&parent->ps2dev, &rate_param, PSMOUSE_CMD_SETRATE))
559 return -1;
560 return 0;
561}
562
Dmitry Torokhova8b3c0f2010-10-04 21:46:10 -0700563static int synaptics_pt_start(struct serio *serio)
564{
565 struct psmouse *parent = serio_get_drvdata(serio->parent);
566 struct synaptics_data *priv = parent->private;
567
568 serio_pause_rx(parent->ps2dev.serio);
569 priv->pt_port = serio;
570 serio_continue_rx(parent->ps2dev.serio);
571
572 return 0;
573}
574
575static void synaptics_pt_stop(struct serio *serio)
576{
577 struct psmouse *parent = serio_get_drvdata(serio->parent);
578 struct synaptics_data *priv = parent->private;
579
580 serio_pause_rx(parent->ps2dev.serio);
581 priv->pt_port = NULL;
582 serio_continue_rx(parent->ps2dev.serio);
583}
584
585static int synaptics_is_pt_packet(unsigned char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
587 return (buf[0] & 0xFC) == 0x84 && (buf[3] & 0xCC) == 0xC4;
588}
589
Benjamin Tissoirescdd9dc12015-03-08 22:35:41 -0700590static void synaptics_pass_pt_packet(struct psmouse *psmouse,
591 struct serio *ptport,
592 unsigned char *packet)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
Benjamin Tissoirescdd9dc12015-03-08 22:35:41 -0700594 struct synaptics_data *priv = psmouse->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 struct psmouse *child = serio_get_drvdata(ptport);
596
597 if (child && child->state == PSMOUSE_ACTIVATED) {
Benjamin Tissoirescdd9dc12015-03-08 22:35:41 -0700598 serio_interrupt(ptport, packet[1] | priv->pt_buttons, 0);
David Howells7d12e782006-10-05 14:55:46 +0100599 serio_interrupt(ptport, packet[4], 0);
600 serio_interrupt(ptport, packet[5], 0);
Sergey Vlasov33fdfa92005-07-24 00:53:32 -0500601 if (child->pktsize == 4)
David Howells7d12e782006-10-05 14:55:46 +0100602 serio_interrupt(ptport, packet[2], 0);
Benjamin Tissoirescdd9dc12015-03-08 22:35:41 -0700603 } else {
David Howells7d12e782006-10-05 14:55:46 +0100604 serio_interrupt(ptport, packet[1], 0);
Benjamin Tissoirescdd9dc12015-03-08 22:35:41 -0700605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606}
607
608static void synaptics_pt_activate(struct psmouse *psmouse)
609{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 struct synaptics_data *priv = psmouse->private;
Dmitry Torokhova8b3c0f2010-10-04 21:46:10 -0700611 struct psmouse *child = serio_get_drvdata(priv->pt_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613 /* adjust the touchpad to child's choice of protocol */
614 if (child) {
Sergey Vlasov33fdfa92005-07-24 00:53:32 -0500615 if (child->pktsize == 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 priv->mode |= SYN_BIT_FOUR_BYTE_CLIENT;
617 else
618 priv->mode &= ~SYN_BIT_FOUR_BYTE_CLIENT;
619
620 if (synaptics_mode_cmd(psmouse, priv->mode))
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700621 psmouse_warn(psmouse,
622 "failed to switch guest protocol\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 }
624}
625
626static void synaptics_pt_create(struct psmouse *psmouse)
627{
628 struct serio *serio;
629
Eric Sesterhennb39787a2006-03-14 00:09:16 -0500630 serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 if (!serio) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700632 psmouse_err(psmouse,
633 "not enough memory for pass-through port\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 return;
635 }
636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 serio->id.type = SERIO_PS_PSTHRU;
638 strlcpy(serio->name, "Synaptics pass-through", sizeof(serio->name));
639 strlcpy(serio->phys, "synaptics-pt/serio0", sizeof(serio->name));
640 serio->write = synaptics_pt_write;
Dmitry Torokhova8b3c0f2010-10-04 21:46:10 -0700641 serio->start = synaptics_pt_start;
642 serio->stop = synaptics_pt_stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 serio->parent = psmouse->ps2dev.serio;
644
645 psmouse->pt_activate = synaptics_pt_activate;
646
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700647 psmouse_info(psmouse, "serio: %s port at %s\n",
648 serio->name, psmouse->phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 serio_register_port(serio);
650}
651
652/*****************************************************************************
653 * Functions to interpret the absolute mode packets
654 ****************************************************************************/
655
Daniel Kurtz7afdb842011-08-23 23:00:33 -0700656static void synaptics_parse_agm(const unsigned char buf[],
Daniel Kurtza6ca40c2011-08-23 23:02:31 -0700657 struct synaptics_data *priv,
658 struct synaptics_hw_state *hw)
Daniel Kurtz7afdb842011-08-23 23:00:33 -0700659{
660 struct synaptics_hw_state *agm = &priv->agm;
Daniel Kurtza6ca40c2011-08-23 23:02:31 -0700661 int agm_packet_type;
Daniel Kurtz7afdb842011-08-23 23:00:33 -0700662
Daniel Kurtza6ca40c2011-08-23 23:02:31 -0700663 agm_packet_type = (buf[5] & 0x30) >> 4;
664 switch (agm_packet_type) {
665 case 1:
666 /* Gesture packet: (x, y, z) half resolution */
667 agm->w = hw->w;
668 agm->x = (((buf[4] & 0x0f) << 8) | buf[1]) << 1;
669 agm->y = (((buf[4] & 0xf0) << 4) | buf[2]) << 1;
670 agm->z = ((buf[3] & 0x30) | (buf[5] & 0x0f)) << 1;
671 break;
672
673 case 2:
Benjamin Tissoirese9e85202014-12-29 14:15:24 -0800674 /* AGM-CONTACT packet: we are only interested in the count */
675 priv->agm_count = buf[1];
Daniel Kurtza6ca40c2011-08-23 23:02:31 -0700676 break;
677
678 default:
679 break;
680 }
Daniel Kurtz7afdb842011-08-23 23:00:33 -0700681}
682
Dmitry Torokhovdc5465d2015-03-08 22:30:43 -0700683static void synaptics_parse_ext_buttons(const unsigned char buf[],
684 struct synaptics_data *priv,
685 struct synaptics_hw_state *hw)
686{
687 unsigned int ext_bits =
688 (SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap) + 1) >> 1;
689 unsigned int ext_mask = GENMASK(ext_bits - 1, 0);
690
691 hw->ext_buttons = buf[4] & ext_mask;
692 hw->ext_buttons |= (buf[5] & ext_mask) << ext_bits;
693}
694
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100695static int synaptics_parse_hw_state(const unsigned char buf[],
696 struct synaptics_data *priv,
697 struct synaptics_hw_state *hw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
699 memset(hw, 0, sizeof(struct synaptics_hw_state));
700
701 if (SYN_MODEL_NEWABS(priv->model_id)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 hw->w = (((buf[0] & 0x30) >> 2) |
703 ((buf[0] & 0x04) >> 1) |
704 ((buf[3] & 0x04) >> 2));
705
Dmitry Torokhov5715fc72014-08-30 13:51:06 -0700706 if ((SYN_CAP_ADV_GESTURE(priv->ext_cap_0c) ||
707 SYN_CAP_IMAGE_SENSOR(priv->ext_cap_0c)) &&
708 hw->w == 2) {
709 synaptics_parse_agm(buf, priv, hw);
710 return 1;
711 }
712
713 hw->x = (((buf[3] & 0x10) << 8) |
714 ((buf[1] & 0x0f) << 8) |
715 buf[4]);
716 hw->y = (((buf[3] & 0x20) << 7) |
717 ((buf[1] & 0xf0) << 4) |
718 buf[5]);
719 hw->z = buf[2];
720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 hw->left = (buf[0] & 0x01) ? 1 : 0;
722 hw->right = (buf[0] & 0x02) ? 1 : 0;
723
Dmitry Torokhovde4e3742014-12-29 14:43:44 -0800724 if (priv->is_forcepad) {
Dmitry Torokhov5715fc72014-08-30 13:51:06 -0700725 /*
726 * ForcePads, like Clickpads, use middle button
727 * bits to report primary button clicks.
728 * Unfortunately they report primary button not
729 * only when user presses on the pad above certain
730 * threshold, but also when there are more than one
731 * finger on the touchpad, which interferes with
732 * out multi-finger gestures.
733 */
734 if (hw->z == 0) {
735 /* No contacts */
736 priv->press = priv->report_press = false;
737 } else if (hw->w >= 4 && ((buf[0] ^ buf[3]) & 0x01)) {
738 /*
739 * Single-finger touch with pressure above
740 * the threshold. If pressure stays long
741 * enough, we'll start reporting primary
742 * button. We rely on the device continuing
743 * sending data even if finger does not
744 * move.
745 */
746 if (!priv->press) {
747 priv->press_start = jiffies;
748 priv->press = true;
749 } else if (time_after(jiffies,
750 priv->press_start +
751 msecs_to_jiffies(50))) {
752 priv->report_press = true;
753 }
754 } else {
755 priv->press = false;
756 }
757
758 hw->left = priv->report_press;
759
760 } else if (SYN_CAP_CLICKPAD(priv->ext_cap_0c)) {
Takashi Iwai5f57d672010-04-19 10:37:21 -0700761 /*
762 * Clickpad's button is transmitted as middle button,
763 * however, since it is primary button, we will report
764 * it as BTN_LEFT.
765 */
766 hw->left = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0;
767
768 } else if (SYN_CAP_MIDDLE_BUTTON(priv->capabilities)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 hw->middle = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0;
770 if (hw->w == 2)
771 hw->scroll = (signed char)(buf[1]);
772 }
773
774 if (SYN_CAP_FOUR_BUTTON(priv->capabilities)) {
775 hw->up = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0;
776 hw->down = ((buf[0] ^ buf[3]) & 0x02) ? 1 : 0;
777 }
778
Dmitry Torokhovdc5465d2015-03-08 22:30:43 -0700779 if (SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap) > 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 ((buf[0] ^ buf[3]) & 0x02)) {
Dmitry Torokhovdc5465d2015-03-08 22:30:43 -0700781 synaptics_parse_ext_buttons(buf, priv, hw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 }
783 } else {
784 hw->x = (((buf[1] & 0x1f) << 8) | buf[2]);
785 hw->y = (((buf[4] & 0x1f) << 8) | buf[5]);
786
787 hw->z = (((buf[0] & 0x30) << 2) | (buf[3] & 0x3F));
788 hw->w = (((buf[1] & 0x80) >> 4) | ((buf[0] & 0x04) >> 1));
789
790 hw->left = (buf[0] & 0x01) ? 1 : 0;
791 hw->right = (buf[0] & 0x02) ? 1 : 0;
792 }
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100793
Seth Forshee824efd32012-09-28 10:29:21 -0700794 /*
795 * Convert wrap-around values to negative. (X|Y)_MAX_POSITIVE
796 * is used by some firmware to indicate a finger at the edge of
797 * the touchpad whose precise position cannot be determined, so
798 * convert these values to the maximum axis value.
799 */
Seth Forsheec03945062012-07-24 23:54:11 -0700800 if (hw->x > X_MAX_POSITIVE)
801 hw->x -= 1 << ABS_POS_BITS;
Seth Forshee824efd32012-09-28 10:29:21 -0700802 else if (hw->x == X_MAX_POSITIVE)
803 hw->x = XMAX;
804
Seth Forsheec03945062012-07-24 23:54:11 -0700805 if (hw->y > Y_MAX_POSITIVE)
806 hw->y -= 1 << ABS_POS_BITS;
Seth Forshee824efd32012-09-28 10:29:21 -0700807 else if (hw->y == Y_MAX_POSITIVE)
808 hw->y = YMAX;
Seth Forsheec03945062012-07-24 23:54:11 -0700809
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100810 return 0;
811}
812
Daniel Kurtzbea9f0f2011-07-06 22:42:52 -0700813static void synaptics_report_semi_mt_slot(struct input_dev *dev, int slot,
814 bool active, int x, int y)
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100815{
816 input_mt_slot(dev, slot);
817 input_mt_report_slot_state(dev, MT_TOOL_FINGER, active);
818 if (active) {
819 input_report_abs(dev, ABS_MT_POSITION_X, x);
Daniel Kurtz6de58dd2011-08-23 23:00:24 -0700820 input_report_abs(dev, ABS_MT_POSITION_Y, synaptics_invert_y(y));
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100821 }
822}
823
824static void synaptics_report_semi_mt_data(struct input_dev *dev,
825 const struct synaptics_hw_state *a,
826 const struct synaptics_hw_state *b,
827 int num_fingers)
828{
829 if (num_fingers >= 2) {
Daniel Kurtzbea9f0f2011-07-06 22:42:52 -0700830 synaptics_report_semi_mt_slot(dev, 0, true, min(a->x, b->x),
831 min(a->y, b->y));
832 synaptics_report_semi_mt_slot(dev, 1, true, max(a->x, b->x),
833 max(a->y, b->y));
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100834 } else if (num_fingers == 1) {
Daniel Kurtzbea9f0f2011-07-06 22:42:52 -0700835 synaptics_report_semi_mt_slot(dev, 0, true, a->x, a->y);
836 synaptics_report_semi_mt_slot(dev, 1, false, 0, 0);
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100837 } else {
Daniel Kurtzbea9f0f2011-07-06 22:42:52 -0700838 synaptics_report_semi_mt_slot(dev, 0, false, 0, 0);
839 synaptics_report_semi_mt_slot(dev, 1, false, 0, 0);
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841}
842
Benjamin Tissoiresebc80842015-03-08 22:32:43 -0700843static void synaptics_report_ext_buttons(struct psmouse *psmouse,
844 const struct synaptics_hw_state *hw)
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700845{
846 struct input_dev *dev = psmouse->dev;
847 struct synaptics_data *priv = psmouse->private;
Dmitry Torokhovdc5465d2015-03-08 22:30:43 -0700848 int ext_bits = (SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap) + 1) >> 1;
Benjamin Tissoirescdd9dc12015-03-08 22:35:41 -0700849 char buf[6] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700850 int i;
851
Benjamin Tissoiresebc80842015-03-08 22:32:43 -0700852 if (!SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap))
853 return;
854
855 /* Bug in FW 8.1, buttons are reported only when ExtBit is 1 */
856 if (SYN_ID_FULL(priv->identity) == 0x801 &&
857 !((psmouse->packet[0] ^ psmouse->packet[3]) & 0x02))
858 return;
859
Benjamin Tissoirescdd9dc12015-03-08 22:35:41 -0700860 if (!SYN_CAP_EXT_BUTTONS_STICK(priv->ext_cap_10)) {
861 for (i = 0; i < ext_bits; i++) {
862 input_report_key(dev, BTN_0 + 2 * i,
863 hw->ext_buttons & (1 << i));
864 input_report_key(dev, BTN_1 + 2 * i,
865 hw->ext_buttons & (1 << (i + ext_bits)));
866 }
867 return;
Benjamin Tissoiresebc80842015-03-08 22:32:43 -0700868 }
Benjamin Tissoirescdd9dc12015-03-08 22:35:41 -0700869
870 /*
871 * This generation of touchpads has the trackstick buttons
872 * physically wired to the touchpad. Re-route them through
873 * the pass-through interface.
874 */
875 if (!priv->pt_port)
876 return;
877
878 /* The trackstick expects at most 3 buttons */
879 priv->pt_buttons = SYN_CAP_EXT_BUTTON_STICK_L(hw->ext_buttons) |
880 SYN_CAP_EXT_BUTTON_STICK_R(hw->ext_buttons) << 1 |
881 SYN_CAP_EXT_BUTTON_STICK_M(hw->ext_buttons) << 2;
882
883 synaptics_pass_pt_packet(psmouse, priv->pt_port, buf);
Benjamin Tissoiresebc80842015-03-08 22:32:43 -0700884}
885
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700886static void synaptics_report_buttons(struct psmouse *psmouse,
887 const struct synaptics_hw_state *hw)
888{
889 struct input_dev *dev = psmouse->dev;
890 struct synaptics_data *priv = psmouse->private;
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700891
892 input_report_key(dev, BTN_LEFT, hw->left);
893 input_report_key(dev, BTN_RIGHT, hw->right);
894
895 if (SYN_CAP_MIDDLE_BUTTON(priv->capabilities))
896 input_report_key(dev, BTN_MIDDLE, hw->middle);
897
898 if (SYN_CAP_FOUR_BUTTON(priv->capabilities)) {
899 input_report_key(dev, BTN_FORWARD, hw->up);
900 input_report_key(dev, BTN_BACK, hw->down);
901 }
902
Benjamin Tissoiresebc80842015-03-08 22:32:43 -0700903 synaptics_report_ext_buttons(psmouse, hw);
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700904}
905
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700906static void synaptics_report_mt_data(struct psmouse *psmouse,
Benjamin Tissoirese9e85202014-12-29 14:15:24 -0800907 const struct synaptics_hw_state *sgm,
908 int num_fingers)
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700909{
910 struct input_dev *dev = psmouse->dev;
911 struct synaptics_data *priv = psmouse->private;
Benjamin Tissoirese9e85202014-12-29 14:15:24 -0800912 const struct synaptics_hw_state *hw[2] = { sgm, &priv->agm };
913 struct input_mt_pos pos[2];
914 int slot[2], nsemi, i;
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700915
Benjamin Tissoirese9e85202014-12-29 14:15:24 -0800916 nsemi = clamp_val(num_fingers, 0, 2);
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700917
Benjamin Tissoirese9e85202014-12-29 14:15:24 -0800918 for (i = 0; i < nsemi; i++) {
919 pos[i].x = hw[i]->x;
920 pos[i].y = synaptics_invert_y(hw[i]->y);
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700921 }
922
Benjamin Tissoires58fd9af2015-04-05 13:44:12 -0700923 input_mt_assign_slots(dev, slot, pos, nsemi, DMAX * priv->x_res);
Benjamin Tissoirese9e85202014-12-29 14:15:24 -0800924
925 for (i = 0; i < nsemi; i++) {
926 input_mt_slot(dev, slot[i]);
927 input_mt_report_slot_state(dev, MT_TOOL_FINGER, true);
928 input_report_abs(dev, ABS_MT_POSITION_X, pos[i].x);
929 input_report_abs(dev, ABS_MT_POSITION_Y, pos[i].y);
930 input_report_abs(dev, ABS_MT_PRESSURE, hw[i]->z);
931 }
932
933 input_mt_drop_unused(dev);
934
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700935 /* Don't use active slot count to generate BTN_TOOL events. */
936 input_mt_report_pointer_emulation(dev, false);
937
938 /* Send the number of fingers reported by touchpad itself. */
Benjamin Tissoirese9e85202014-12-29 14:15:24 -0800939 input_mt_report_finger_count(dev, num_fingers);
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700940
941 synaptics_report_buttons(psmouse, sgm);
942
943 input_sync(dev);
944}
945
946static void synaptics_image_sensor_process(struct psmouse *psmouse,
947 struct synaptics_hw_state *sgm)
948{
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700949 struct synaptics_data *priv = psmouse->private;
Benjamin Tissoirese9e85202014-12-29 14:15:24 -0800950 int num_fingers;
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700951
952 /*
953 * Update mt_state using the new finger count and current mt_state.
954 */
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700955 if (sgm->z == 0)
Benjamin Tissoirese9e85202014-12-29 14:15:24 -0800956 num_fingers = 0;
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700957 else if (sgm->w >= 4)
Benjamin Tissoirese9e85202014-12-29 14:15:24 -0800958 num_fingers = 1;
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700959 else if (sgm->w == 0)
Benjamin Tissoirese9e85202014-12-29 14:15:24 -0800960 num_fingers = 2;
961 else if (sgm->w == 1)
962 num_fingers = priv->agm_count ? priv->agm_count : 3;
Daniel Kurtz6b4b49f2011-08-23 23:02:56 -0700963 else
Benjamin Tissoirese9e85202014-12-29 14:15:24 -0800964 num_fingers = 4;
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700965
966 /* Send resulting input events to user space */
Benjamin Tissoirese9e85202014-12-29 14:15:24 -0800967 synaptics_report_mt_data(psmouse, sgm, num_fingers);
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700968}
969
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970/*
971 * called for each full received packet from the touchpad
972 */
973static void synaptics_process_packet(struct psmouse *psmouse)
974{
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500975 struct input_dev *dev = psmouse->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 struct synaptics_data *priv = psmouse->private;
977 struct synaptics_hw_state hw;
978 int num_fingers;
979 int finger_width;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100981 if (synaptics_parse_hw_state(psmouse->packet, priv, &hw))
982 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700984 if (SYN_CAP_IMAGE_SENSOR(priv->ext_cap_0c)) {
985 synaptics_image_sensor_process(psmouse, &hw);
986 return;
987 }
988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 if (hw.scroll) {
990 priv->scroll += hw.scroll;
991
992 while (priv->scroll >= 4) {
993 input_report_key(dev, BTN_BACK, !hw.down);
994 input_sync(dev);
995 input_report_key(dev, BTN_BACK, hw.down);
996 input_sync(dev);
997 priv->scroll -= 4;
998 }
999 while (priv->scroll <= -4) {
1000 input_report_key(dev, BTN_FORWARD, !hw.up);
1001 input_sync(dev);
1002 input_report_key(dev, BTN_FORWARD, hw.up);
1003 input_sync(dev);
1004 priv->scroll += 4;
1005 }
1006 return;
1007 }
1008
Henrik Rydberg4f56ce92010-12-18 15:42:30 +01001009 if (hw.z > 0 && hw.x > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 num_fingers = 1;
1011 finger_width = 5;
1012 if (SYN_CAP_EXTENDED(priv->capabilities)) {
1013 switch (hw.w) {
1014 case 0 ... 1:
1015 if (SYN_CAP_MULTIFINGER(priv->capabilities))
1016 num_fingers = hw.w + 2;
1017 break;
1018 case 2:
1019 if (SYN_MODEL_PEN(priv->model_id))
1020 ; /* Nothing, treat a pen as a single finger */
1021 break;
1022 case 4 ... 15:
1023 if (SYN_CAP_PALMDETECT(priv->capabilities))
1024 finger_width = hw.w;
1025 break;
1026 }
1027 }
1028 } else {
1029 num_fingers = 0;
1030 finger_width = 0;
1031 }
1032
Henrik Rydberge08d9af2014-07-14 10:26:56 -07001033 if (cr48_profile_sensor) {
Benjamin Tissoiresaa104b12014-12-29 14:17:44 -08001034 synaptics_report_mt_data(psmouse, &hw, num_fingers);
Henrik Rydberge08d9af2014-07-14 10:26:56 -07001035 return;
1036 }
1037
Henrik Rydbergfec6e522010-12-21 18:11:25 +01001038 if (SYN_CAP_ADV_GESTURE(priv->ext_cap_0c))
Daniel Kurtz7afdb842011-08-23 23:00:33 -07001039 synaptics_report_semi_mt_data(dev, &hw, &priv->agm,
1040 num_fingers);
Henrik Rydbergfec6e522010-12-21 18:11:25 +01001041
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 /* Post events
1043 * BTN_TOUCH has to be first as mousedev relies on it when doing
1044 * absolute -> relative conversion
1045 */
1046 if (hw.z > 30) input_report_key(dev, BTN_TOUCH, 1);
1047 if (hw.z < 25) input_report_key(dev, BTN_TOUCH, 0);
1048
Henrik Rydberg4f56ce92010-12-18 15:42:30 +01001049 if (num_fingers > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 input_report_abs(dev, ABS_X, hw.x);
Daniel Kurtz6de58dd2011-08-23 23:00:24 -07001051 input_report_abs(dev, ABS_Y, synaptics_invert_y(hw.y));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 }
1053 input_report_abs(dev, ABS_PRESSURE, hw.z);
1054
Chris Bagwell2a8e7712010-07-19 09:06:15 -07001055 if (SYN_CAP_PALMDETECT(priv->capabilities))
1056 input_report_abs(dev, ABS_TOOL_WIDTH, finger_width);
1057
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 input_report_key(dev, BTN_TOOL_FINGER, num_fingers == 1);
Peter Hutterere42b6642008-11-20 15:24:42 -05001059 if (SYN_CAP_MULTIFINGER(priv->capabilities)) {
1060 input_report_key(dev, BTN_TOOL_DOUBLETAP, num_fingers == 2);
1061 input_report_key(dev, BTN_TOOL_TRIPLETAP, num_fingers == 3);
1062 }
1063
Daniel Kurtz3cdfee92011-08-23 23:02:25 -07001064 synaptics_report_buttons(psmouse, &hw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
1066 input_sync(dev);
1067}
1068
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001069static int synaptics_validate_byte(struct psmouse *psmouse,
1070 int idx, unsigned char pkt_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
Helge Dellere38de672006-09-10 21:54:39 -04001072 static const unsigned char newabs_mask[] = { 0xC8, 0x00, 0x00, 0xC8, 0x00 };
1073 static const unsigned char newabs_rel_mask[] = { 0xC0, 0x00, 0x00, 0xC0, 0x00 };
1074 static const unsigned char newabs_rslt[] = { 0x80, 0x00, 0x00, 0xC0, 0x00 };
1075 static const unsigned char oldabs_mask[] = { 0xC0, 0x60, 0x00, 0xC0, 0x60 };
1076 static const unsigned char oldabs_rslt[] = { 0xC0, 0x00, 0x00, 0x80, 0x00 };
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001077 const char *packet = psmouse->packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
1079 if (idx < 0 || idx > 4)
1080 return 0;
1081
1082 switch (pkt_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
Dmitry Torokhova62f0d22010-05-19 10:39:17 -07001084 case SYN_NEWABS:
1085 case SYN_NEWABS_RELAXED:
1086 return (packet[idx] & newabs_rel_mask[idx]) == newabs_rslt[idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Dmitry Torokhova62f0d22010-05-19 10:39:17 -07001088 case SYN_NEWABS_STRICT:
1089 return (packet[idx] & newabs_mask[idx]) == newabs_rslt[idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Dmitry Torokhova62f0d22010-05-19 10:39:17 -07001091 case SYN_OLDABS:
1092 return (packet[idx] & oldabs_mask[idx]) == oldabs_rslt[idx];
1093
1094 default:
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001095 psmouse_err(psmouse, "unknown packet type %d\n", pkt_type);
Dmitry Torokhova62f0d22010-05-19 10:39:17 -07001096 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 }
1098}
1099
1100static unsigned char synaptics_detect_pkt_type(struct psmouse *psmouse)
1101{
1102 int i;
1103
1104 for (i = 0; i < 5; i++)
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001105 if (!synaptics_validate_byte(psmouse, i, SYN_NEWABS_STRICT)) {
1106 psmouse_info(psmouse, "using relaxed packet validation\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 return SYN_NEWABS_RELAXED;
1108 }
1109
1110 return SYN_NEWABS_STRICT;
1111}
1112
David Howells7d12e782006-10-05 14:55:46 +01001113static psmouse_ret_t synaptics_process_byte(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 struct synaptics_data *priv = psmouse->private;
1116
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 if (psmouse->pktcnt >= 6) { /* Full packet received */
1118 if (unlikely(priv->pkt_type == SYN_NEWABS))
1119 priv->pkt_type = synaptics_detect_pkt_type(psmouse);
1120
Dmitry Torokhova8b3c0f2010-10-04 21:46:10 -07001121 if (SYN_CAP_PASS_THROUGH(priv->capabilities) &&
1122 synaptics_is_pt_packet(psmouse->packet)) {
1123 if (priv->pt_port)
Benjamin Tissoirescdd9dc12015-03-08 22:35:41 -07001124 synaptics_pass_pt_packet(psmouse, priv->pt_port,
1125 psmouse->packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 } else
1127 synaptics_process_packet(psmouse);
1128
1129 return PSMOUSE_FULL_PACKET;
1130 }
1131
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001132 return synaptics_validate_byte(psmouse, psmouse->pktcnt - 1, priv->pkt_type) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 PSMOUSE_GOOD_DATA : PSMOUSE_BAD_DATA;
1134}
1135
1136/*****************************************************************************
1137 * Driver initialization/cleanup functions
1138 ****************************************************************************/
Daniel Kurtz85615472011-08-23 23:00:41 -07001139static void set_abs_position_params(struct input_dev *dev,
1140 struct synaptics_data *priv, int x_code,
1141 int y_code)
1142{
1143 int x_min = priv->x_min ?: XMIN_NOMINAL;
1144 int x_max = priv->x_max ?: XMAX_NOMINAL;
1145 int y_min = priv->y_min ?: YMIN_NOMINAL;
1146 int y_max = priv->y_max ?: YMAX_NOMINAL;
1147 int fuzz = SYN_CAP_REDUCED_FILTERING(priv->ext_cap_0c) ?
1148 SYN_REDUCED_FILTER_FUZZ : 0;
1149
1150 input_set_abs_params(dev, x_code, x_min, x_max, fuzz, 0);
1151 input_set_abs_params(dev, y_code, y_min, y_max, fuzz, 0);
1152 input_abs_set_res(dev, x_code, priv->x_res);
1153 input_abs_set_res(dev, y_code, priv->y_res);
1154}
1155
Hans de Goede43e19882014-04-19 22:26:41 -07001156static void set_input_params(struct psmouse *psmouse,
1157 struct synaptics_data *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158{
Hans de Goede43e19882014-04-19 22:26:41 -07001159 struct input_dev *dev = psmouse->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 int i;
1161
Daniel Drake7968a5d2011-11-08 00:00:35 -08001162 /* Things that apply to both modes */
Henrik Rydbergc14890a2010-12-16 09:52:23 +01001163 __set_bit(INPUT_PROP_POINTER, dev->propbit);
Daniel Drake7968a5d2011-11-08 00:00:35 -08001164 __set_bit(EV_KEY, dev->evbit);
1165 __set_bit(BTN_LEFT, dev->keybit);
1166 __set_bit(BTN_RIGHT, dev->keybit);
Henrik Rydbergc14890a2010-12-16 09:52:23 +01001167
Daniel Drake7968a5d2011-11-08 00:00:35 -08001168 if (SYN_CAP_MIDDLE_BUTTON(priv->capabilities))
1169 __set_bit(BTN_MIDDLE, dev->keybit);
1170
1171 if (!priv->absolute_mode) {
1172 /* Relative mode */
1173 __set_bit(EV_REL, dev->evbit);
1174 __set_bit(REL_X, dev->relbit);
1175 __set_bit(REL_Y, dev->relbit);
1176 return;
1177 }
1178
1179 /* Absolute mode */
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001180 __set_bit(EV_ABS, dev->evbit);
Daniel Kurtz85615472011-08-23 23:00:41 -07001181 set_abs_position_params(dev, priv, ABS_X, ABS_Y);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 input_set_abs_params(dev, ABS_PRESSURE, 0, 255, 0, 0);
Chris Bagwell2a8e7712010-07-19 09:06:15 -07001183
Henrik Rydberge08d9af2014-07-14 10:26:56 -07001184 if (cr48_profile_sensor)
1185 input_set_abs_params(dev, ABS_MT_PRESSURE, 0, 255, 0, 0);
1186
Daniel Kurtz3cdfee92011-08-23 23:02:25 -07001187 if (SYN_CAP_IMAGE_SENSOR(priv->ext_cap_0c)) {
Daniel Kurtz3cdfee92011-08-23 23:02:25 -07001188 set_abs_position_params(dev, priv, ABS_MT_POSITION_X,
1189 ABS_MT_POSITION_Y);
1190 /* Image sensors can report per-contact pressure */
1191 input_set_abs_params(dev, ABS_MT_PRESSURE, 0, 255, 0, 0);
Benjamin Tissoirese9e85202014-12-29 14:15:24 -08001192 input_mt_init_slots(dev, 2, INPUT_MT_POINTER | INPUT_MT_TRACK);
Daniel Kurtz6b4b49f2011-08-23 23:02:56 -07001193
1194 /* Image sensors can signal 4 and 5 finger clicks */
1195 __set_bit(BTN_TOOL_QUADTAP, dev->keybit);
1196 __set_bit(BTN_TOOL_QUINTTAP, dev->keybit);
Daniel Kurtz3cdfee92011-08-23 23:02:25 -07001197 } else if (SYN_CAP_ADV_GESTURE(priv->ext_cap_0c)) {
Daniel Kurtz85615472011-08-23 23:00:41 -07001198 set_abs_position_params(dev, priv, ABS_MT_POSITION_X,
1199 ABS_MT_POSITION_Y);
Henrik Rydberge08d9af2014-07-14 10:26:56 -07001200 /*
1201 * Profile sensor in CR-48 tracks contacts reasonably well,
1202 * other non-image sensors with AGM use semi-mt.
1203 */
Dmitry Torokhovae841972014-07-25 17:12:12 -07001204 input_mt_init_slots(dev, 2,
Henrik Rydberge08d9af2014-07-14 10:26:56 -07001205 INPUT_MT_POINTER |
1206 (cr48_profile_sensor ?
1207 INPUT_MT_TRACK : INPUT_MT_SEMI_MT));
Henrik Rydbergfec6e522010-12-21 18:11:25 +01001208 }
1209
Chris Bagwell2a8e7712010-07-19 09:06:15 -07001210 if (SYN_CAP_PALMDETECT(priv->capabilities))
Chris Bagwell58fb0212010-07-19 09:06:15 -07001211 input_set_abs_params(dev, ABS_TOOL_WIDTH, 0, 15, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001213 __set_bit(BTN_TOUCH, dev->keybit);
1214 __set_bit(BTN_TOOL_FINGER, dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Peter Hutterere42b6642008-11-20 15:24:42 -05001216 if (SYN_CAP_MULTIFINGER(priv->capabilities)) {
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001217 __set_bit(BTN_TOOL_DOUBLETAP, dev->keybit);
1218 __set_bit(BTN_TOOL_TRIPLETAP, dev->keybit);
Peter Hutterere42b6642008-11-20 15:24:42 -05001219 }
1220
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 if (SYN_CAP_FOUR_BUTTON(priv->capabilities) ||
1222 SYN_CAP_MIDDLE_BUTTON(priv->capabilities)) {
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001223 __set_bit(BTN_FORWARD, dev->keybit);
1224 __set_bit(BTN_BACK, dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 }
1226
Benjamin Tissoirescdd9dc12015-03-08 22:35:41 -07001227 if (!SYN_CAP_EXT_BUTTONS_STICK(priv->ext_cap_10))
1228 for (i = 0; i < SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap); i++)
1229 __set_bit(BTN_0 + i, dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001231 __clear_bit(EV_REL, dev->evbit);
1232 __clear_bit(REL_X, dev->relbit);
1233 __clear_bit(REL_Y, dev->relbit);
Tero Saarniec20a022009-06-10 23:27:24 -07001234
Takashi Iwai5f57d672010-04-19 10:37:21 -07001235 if (SYN_CAP_CLICKPAD(priv->ext_cap_0c)) {
Henrik Rydbergc14890a2010-12-16 09:52:23 +01001236 __set_bit(INPUT_PROP_BUTTONPAD, dev->propbit);
Benjamin Tissoires3adde1f2015-03-08 22:34:50 -07001237 if (psmouse_matches_pnp_id(psmouse, topbuttonpad_pnp_ids) &&
1238 !SYN_CAP_EXT_BUTTONS_STICK(priv->ext_cap_10))
Hans de Goedee2f61102014-05-19 22:53:23 -07001239 __set_bit(INPUT_PROP_TOPBUTTONPAD, dev->propbit);
Takashi Iwai5f57d672010-04-19 10:37:21 -07001240 /* Clickpads report only left button */
1241 __clear_bit(BTN_RIGHT, dev->keybit);
1242 __clear_bit(BTN_MIDDLE, dev->keybit);
1243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244}
1245
Daniel Drake7968a5d2011-11-08 00:00:35 -08001246static ssize_t synaptics_show_disable_gesture(struct psmouse *psmouse,
1247 void *data, char *buf)
1248{
1249 struct synaptics_data *priv = psmouse->private;
1250
1251 return sprintf(buf, "%c\n", priv->disable_gesture ? '1' : '0');
1252}
1253
1254static ssize_t synaptics_set_disable_gesture(struct psmouse *psmouse,
1255 void *data, const char *buf,
1256 size_t len)
1257{
1258 struct synaptics_data *priv = psmouse->private;
1259 unsigned int value;
1260 int err;
1261
1262 err = kstrtouint(buf, 10, &value);
1263 if (err)
1264 return err;
1265
1266 if (value > 1)
1267 return -EINVAL;
1268
1269 if (value == priv->disable_gesture)
1270 return len;
1271
1272 priv->disable_gesture = value;
1273 if (value)
1274 priv->mode |= SYN_BIT_DISABLE_GESTURE;
1275 else
1276 priv->mode &= ~SYN_BIT_DISABLE_GESTURE;
1277
1278 if (synaptics_mode_cmd(psmouse, priv->mode))
1279 return -EIO;
1280
1281 return len;
1282}
1283
1284PSMOUSE_DEFINE_ATTR(disable_gesture, S_IWUSR | S_IRUGO, NULL,
1285 synaptics_show_disable_gesture,
1286 synaptics_set_disable_gesture);
1287
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288static void synaptics_disconnect(struct psmouse *psmouse)
1289{
Daniel Drake7968a5d2011-11-08 00:00:35 -08001290 struct synaptics_data *priv = psmouse->private;
1291
1292 if (!priv->absolute_mode && SYN_ID_DISGEST_SUPPORTED(priv->identity))
1293 device_remove_file(&psmouse->ps2dev.serio->dev,
1294 &psmouse_attr_disable_gesture.dattr);
1295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 synaptics_reset(psmouse);
Daniel Drake7968a5d2011-11-08 00:00:35 -08001297 kfree(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 psmouse->private = NULL;
1299}
1300
1301static int synaptics_reconnect(struct psmouse *psmouse)
1302{
1303 struct synaptics_data *priv = psmouse->private;
1304 struct synaptics_data old_priv = *priv;
Eric Miaoeeb06552013-06-04 09:30:55 -07001305 unsigned char param[2];
Alexandre Peixoto Ferreirac63fe0a2011-01-28 22:05:14 -08001306 int retry = 0;
1307 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
Alexandre Peixoto Ferreirac63fe0a2011-01-28 22:05:14 -08001309 do {
1310 psmouse_reset(psmouse);
Dmitry Torokhov85214782011-12-12 00:05:53 -08001311 if (retry) {
1312 /*
1313 * On some boxes, right after resuming, the touchpad
1314 * needs some time to finish initializing (I assume
1315 * it needs time to calibrate) and start responding
1316 * to Synaptics-specific queries, so let's wait a
1317 * bit.
1318 */
1319 ssleep(1);
1320 }
Eric Miaoeeb06552013-06-04 09:30:55 -07001321 ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_GETID);
Alexandre Peixoto Ferreirac63fe0a2011-01-28 22:05:14 -08001322 error = synaptics_detect(psmouse, 0);
1323 } while (error && ++retry < 3);
Andy Whitcroft4d368452009-02-28 12:51:01 -08001324
Alexandre Peixoto Ferreirac63fe0a2011-01-28 22:05:14 -08001325 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 return -1;
1327
Alexandre Peixoto Ferreirac63fe0a2011-01-28 22:05:14 -08001328 if (retry > 1)
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001329 psmouse_dbg(psmouse, "reconnected after %d tries\n", retry);
Alexandre Peixoto Ferreirac63fe0a2011-01-28 22:05:14 -08001330
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 if (synaptics_query_hardware(psmouse)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001332 psmouse_err(psmouse, "Unable to query device.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 return -1;
1334 }
1335
Daniel Drake7968a5d2011-11-08 00:00:35 -08001336 if (synaptics_set_mode(psmouse)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001337 psmouse_err(psmouse, "Unable to initialize device.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 return -1;
1339 }
1340
Alexandre Peixoto Ferreirabaddf582011-01-28 22:05:14 -08001341 if (old_priv.identity != priv->identity ||
1342 old_priv.model_id != priv->model_id ||
1343 old_priv.capabilities != priv->capabilities ||
1344 old_priv.ext_cap != priv->ext_cap) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001345 psmouse_err(psmouse,
1346 "hardware appears to be different: id(%ld-%ld), model(%ld-%ld), caps(%lx-%lx), ext(%lx-%lx).\n",
1347 old_priv.identity, priv->identity,
1348 old_priv.model_id, priv->model_id,
1349 old_priv.capabilities, priv->capabilities,
1350 old_priv.ext_cap, priv->ext_cap);
Alexandre Peixoto Ferreirabaddf582011-01-28 22:05:14 -08001351 return -1;
1352 }
1353
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 return 0;
1355}
1356
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001357static bool impaired_toshiba_kbc;
1358
Sachin Kamatc9631562013-08-12 11:05:58 -07001359static const struct dmi_system_id toshiba_dmi_table[] __initconst = {
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001360#if defined(CONFIG_DMI) && defined(CONFIG_X86)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 {
Dmitry Torokhov9961e252009-12-04 10:24:20 -08001362 /* Toshiba Satellite */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 .matches = {
1364 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
Richard Thrippleton53a26702006-04-02 00:10:18 -05001365 DMI_MATCH(DMI_PRODUCT_NAME, "Satellite"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 },
1367 },
Simon Horman9ba5eaa2005-07-11 01:07:20 -05001368 {
Dmitry Torokhov9961e252009-12-04 10:24:20 -08001369 /* Toshiba Dynabook */
Simon Horman9ba5eaa2005-07-11 01:07:20 -05001370 .matches = {
1371 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
Richard Thrippleton53a26702006-04-02 00:10:18 -05001372 DMI_MATCH(DMI_PRODUCT_NAME, "dynabook"),
1373 },
1374 },
1375 {
Dmitry Torokhov9961e252009-12-04 10:24:20 -08001376 /* Toshiba Portege M300 */
Richard Thrippleton53a26702006-04-02 00:10:18 -05001377 .matches = {
1378 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1379 DMI_MATCH(DMI_PRODUCT_NAME, "PORTEGE M300"),
Simon Horman9ba5eaa2005-07-11 01:07:20 -05001380 },
Dmitry Torokhov5f5eeff2009-10-12 21:35:00 -07001381
1382 },
1383 {
Dmitry Torokhov9961e252009-12-04 10:24:20 -08001384 /* Toshiba Portege M300 */
Dmitry Torokhov5f5eeff2009-10-12 21:35:00 -07001385 .matches = {
1386 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1387 DMI_MATCH(DMI_PRODUCT_NAME, "Portable PC"),
1388 DMI_MATCH(DMI_PRODUCT_VERSION, "Version 1.0"),
1389 },
1390
Simon Horman9ba5eaa2005-07-11 01:07:20 -05001391 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392#endif
Jan Beulich70874862011-03-31 00:01:58 -07001393 { }
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001394};
1395
Andres Salomonef8313b2010-12-23 01:19:38 -08001396static bool broken_olpc_ec;
1397
Sachin Kamatc9631562013-08-12 11:05:58 -07001398static const struct dmi_system_id olpc_dmi_table[] __initconst = {
Andres Salomonef8313b2010-12-23 01:19:38 -08001399#if defined(CONFIG_DMI) && defined(CONFIG_OLPC)
1400 {
1401 /* OLPC XO-1 or XO-1.5 */
1402 .matches = {
1403 DMI_MATCH(DMI_SYS_VENDOR, "OLPC"),
1404 DMI_MATCH(DMI_PRODUCT_NAME, "XO"),
1405 },
1406 },
Andres Salomonef8313b2010-12-23 01:19:38 -08001407#endif
Jan Beulich70874862011-03-31 00:01:58 -07001408 { }
Andres Salomonef8313b2010-12-23 01:19:38 -08001409};
1410
Henrik Rydberge08d9af2014-07-14 10:26:56 -07001411static const struct dmi_system_id __initconst cr48_dmi_table[] = {
1412#if defined(CONFIG_DMI) && defined(CONFIG_X86)
1413 {
1414 /* Cr-48 Chromebook (Codename Mario) */
1415 .matches = {
1416 DMI_MATCH(DMI_SYS_VENDOR, "IEC"),
1417 DMI_MATCH(DMI_PRODUCT_NAME, "Mario"),
1418 },
1419 },
1420#endif
1421 { }
1422};
1423
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001424void __init synaptics_module_init(void)
1425{
1426 impaired_toshiba_kbc = dmi_check_system(toshiba_dmi_table);
Andres Salomonef8313b2010-12-23 01:19:38 -08001427 broken_olpc_ec = dmi_check_system(olpc_dmi_table);
Henrik Rydberge08d9af2014-07-14 10:26:56 -07001428 cr48_profile_sensor = dmi_check_system(cr48_dmi_table);
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001429}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
Daniel Drake7968a5d2011-11-08 00:00:35 -08001431static int __synaptics_init(struct psmouse *psmouse, bool absolute_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432{
1433 struct synaptics_data *priv;
Daniel Drake7968a5d2011-11-08 00:00:35 -08001434 int err = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
Andres Salomonef8313b2010-12-23 01:19:38 -08001436 /*
Daniel Drake83551c02011-11-11 16:05:04 -08001437 * The OLPC XO has issues with Synaptics' absolute mode; the constant
1438 * packet spew overloads the EC such that key presses on the keyboard
1439 * are missed. Given that, don't even attempt to use Absolute mode.
1440 * Relative mode seems to work just fine.
Andres Salomonef8313b2010-12-23 01:19:38 -08001441 */
Daniel Drake83551c02011-11-11 16:05:04 -08001442 if (absolute_mode && broken_olpc_ec) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001443 psmouse_info(psmouse,
1444 "OLPC XO detected, not enabling Synaptics protocol.\n");
Andres Salomonef8313b2010-12-23 01:19:38 -08001445 return -ENODEV;
1446 }
1447
Eric Sesterhennb39787a2006-03-14 00:09:16 -05001448 psmouse->private = priv = kzalloc(sizeof(struct synaptics_data), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 if (!priv)
Davidlohr Bueso6792cbb2010-09-29 18:53:35 -07001450 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Andy Whitcroft4d368452009-02-28 12:51:01 -08001452 psmouse_reset(psmouse);
1453
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 if (synaptics_query_hardware(psmouse)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001455 psmouse_err(psmouse, "Unable to query device.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 goto init_fail;
1457 }
1458
Daniel Drake7968a5d2011-11-08 00:00:35 -08001459 priv->absolute_mode = absolute_mode;
1460 if (SYN_ID_DISGEST_SUPPORTED(priv->identity))
1461 priv->disable_gesture = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
Dmitry Torokhovde4e3742014-12-29 14:43:44 -08001463 /*
1464 * Unfortunately ForcePad capability is not exported over PS/2,
1465 * so we have to resort to checking PNP IDs.
1466 */
1467 priv->is_forcepad = psmouse_matches_pnp_id(psmouse, forcepad_pnp_ids);
1468
Daniel Drake7968a5d2011-11-08 00:00:35 -08001469 if (synaptics_set_mode(psmouse)) {
1470 psmouse_err(psmouse, "Unable to initialize device.\n");
Henrik Rydbergfec6e522010-12-21 18:11:25 +01001471 goto init_fail;
1472 }
1473
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 priv->pkt_type = SYN_MODEL_NEWABS(priv->model_id) ? SYN_NEWABS : SYN_OLDABS;
1475
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001476 psmouse_info(psmouse,
Daniel Kurtzc6bd9d42012-07-07 18:08:51 -07001477 "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 -07001478 SYN_ID_MODEL(priv->identity),
1479 SYN_ID_MAJOR(priv->identity), SYN_ID_MINOR(priv->identity),
1480 priv->model_id,
Daniel Kurtzc6bd9d42012-07-07 18:08:51 -07001481 priv->capabilities, priv->ext_cap, priv->ext_cap_0c,
1482 priv->board_id, priv->firmware_id);
Dmitry Torokhov409b7502005-05-28 02:12:18 -05001483
Hans de Goede43e19882014-04-19 22:26:41 -07001484 set_input_params(psmouse, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Dmitry Torokhov887cc122007-04-12 01:30:41 -04001486 /*
1487 * Encode touchpad model so that it can be used to set
1488 * input device->id.version and be visible to userspace.
1489 * Because version is __u16 we have to drop something.
1490 * Hardware info bits seem to be good candidates as they
1491 * are documented to be for Synaptics corp. internal use.
1492 */
1493 psmouse->model = ((priv->model_id & 0x00ff0000) >> 8) |
1494 (priv->model_id & 0x000000ff);
1495
Daniel Drake7968a5d2011-11-08 00:00:35 -08001496 if (absolute_mode) {
1497 psmouse->protocol_handler = synaptics_process_byte;
1498 psmouse->pktsize = 6;
1499 } else {
1500 /* Relative mode follows standard PS/2 mouse protocol */
1501 psmouse->protocol_handler = psmouse_process_byte;
1502 psmouse->pktsize = 3;
1503 }
1504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 psmouse->set_rate = synaptics_set_rate;
1506 psmouse->disconnect = synaptics_disconnect;
1507 psmouse->reconnect = synaptics_reconnect;
Dmitry Torokhova1cec062007-02-18 01:40:24 -05001508 psmouse->cleanup = synaptics_reset;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001509 /* Synaptics can usually stay in sync without extra help */
1510 psmouse->resync_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
1512 if (SYN_CAP_PASS_THROUGH(priv->capabilities))
1513 synaptics_pt_create(psmouse);
1514
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 /*
1516 * Toshiba's KBC seems to have trouble handling data from
Andres Salomon7ee99162010-12-23 01:18:28 -08001517 * Synaptics at full rate. Switch to a lower rate (roughly
1518 * the same rate as a standard PS/2 mouse).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 */
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001520 if (psmouse->rate >= 80 && impaired_toshiba_kbc) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001521 psmouse_info(psmouse,
1522 "Toshiba %s detected, limiting rate to 40pps.\n",
1523 dmi_get_system_info(DMI_PRODUCT_NAME));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 psmouse->rate = 40;
1525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
Daniel Drake7968a5d2011-11-08 00:00:35 -08001527 if (!priv->absolute_mode && SYN_ID_DISGEST_SUPPORTED(priv->identity)) {
1528 err = device_create_file(&psmouse->ps2dev.serio->dev,
1529 &psmouse_attr_disable_gesture.dattr);
1530 if (err) {
1531 psmouse_err(psmouse,
1532 "Failed to create disable_gesture attribute (%d)",
1533 err);
1534 goto init_fail;
1535 }
1536 }
1537
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 return 0;
1539
1540 init_fail:
1541 kfree(priv);
Daniel Drake7968a5d2011-11-08 00:00:35 -08001542 return err;
1543}
1544
1545int synaptics_init(struct psmouse *psmouse)
1546{
1547 return __synaptics_init(psmouse, true);
1548}
1549
1550int synaptics_init_relative(struct psmouse *psmouse)
1551{
1552 return __synaptics_init(psmouse, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553}
1554
Andres Salomon55e3d922007-03-10 01:39:54 -05001555#else /* CONFIG_MOUSE_PS2_SYNAPTICS */
1556
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001557void __init synaptics_module_init(void)
1558{
1559}
1560
Andres Salomon55e3d922007-03-10 01:39:54 -05001561int synaptics_init(struct psmouse *psmouse)
1562{
1563 return -ENOSYS;
1564}
1565
1566#endif /* CONFIG_MOUSE_PS2_SYNAPTICS */