blob: 6b9db57faa805348f0f8c04bac6f19becfb5fb9e [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>
Benjamin Tissoirese839ffa2017-03-02 14:13:53 -080032#include <linux/rmi.h>
33#include <linux/i2c.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "psmouse.h"
36#include "synaptics.h"
37
38/*
39 * The x/y limits are taken from the Synaptics TouchPad interfacing Guide,
40 * section 2.3.2, which says that they should be valid regardless of the
41 * actual size of the sensor.
Dmitry Torokhov83ba9ea82010-05-10 23:06:52 -070042 * Note that newer firmware allows querying device for maximum useable
43 * coordinates.
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 */
Seth Forsheec03945062012-07-24 23:54:11 -070045#define XMIN 0
46#define XMAX 6143
47#define YMIN 0
48#define YMAX 6143
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#define XMIN_NOMINAL 1472
50#define XMAX_NOMINAL 5472
51#define YMIN_NOMINAL 1408
52#define YMAX_NOMINAL 4448
53
Seth Forsheec03945062012-07-24 23:54:11 -070054/* Size in bits of absolute position values reported by the hardware */
55#define ABS_POS_BITS 13
56
57/*
Seth Forshee824efd32012-09-28 10:29:21 -070058 * These values should represent the absolute maximum value that will
59 * be reported for a positive position value. Some Synaptics firmware
60 * uses this value to indicate a finger near the edge of the touchpad
61 * whose precise position cannot be determined.
62 *
63 * At least one touchpad is known to report positions in excess of this
64 * value which are actually negative values truncated to the 13-bit
65 * reporting range. These values have never been observed to be lower
66 * than 8184 (i.e. -8), so we treat all values greater than 8176 as
67 * negative and any other value as positive.
Seth Forsheec03945062012-07-24 23:54:11 -070068 */
Seth Forshee824efd32012-09-28 10:29:21 -070069#define X_MAX_POSITIVE 8176
70#define Y_MAX_POSITIVE 8176
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Benjamin Tissoires58fd9af2015-04-05 13:44:12 -070072/* maximum ABS_MT_POSITION displacement (in mm) */
73#define DMAX 10
74
Andres Salomon55e3d922007-03-10 01:39:54 -050075/*****************************************************************************
76 * Stuff we need even when we do not want native Synaptics support
77 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79/*
80 * Set the synaptics touchpad mode byte by special commands
81 */
Dmitry Torokhovf6c44422017-03-24 11:20:38 -070082static int synaptics_mode_cmd(struct psmouse *psmouse, u8 mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
Dmitry Torokhovf6c44422017-03-24 11:20:38 -070084 u8 param[1];
Dmitry Torokhov212baf02017-03-23 18:38:14 -070085 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Dmitry Torokhov08be9542018-01-02 12:03:02 -080087 error = ps2_sliced_command(&psmouse->ps2dev, mode);
Dmitry Torokhov212baf02017-03-23 18:38:14 -070088 if (error)
89 return error;
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 param[0] = SYN_PS_SET_MODE2;
Dmitry Torokhov212baf02017-03-23 18:38:14 -070092 error = ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_SETRATE);
93 if (error)
94 return error;
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 return 0;
97}
98
Dmitry Torokhovb7802c52009-09-09 19:13:20 -070099int synaptics_detect(struct psmouse *psmouse, bool set_properties)
Andres Salomon55e3d922007-03-10 01:39:54 -0500100{
101 struct ps2dev *ps2dev = &psmouse->ps2dev;
Dmitry Torokhovf6c44422017-03-24 11:20:38 -0700102 u8 param[4];
Andres Salomon55e3d922007-03-10 01:39:54 -0500103
104 param[0] = 0;
105
106 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
107 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
108 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
109 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
110 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO);
111
112 if (param[1] != 0x47)
113 return -ENODEV;
114
115 if (set_properties) {
116 psmouse->vendor = "Synaptics";
117 psmouse->name = "TouchPad";
118 }
119
120 return 0;
121}
122
123void synaptics_reset(struct psmouse *psmouse)
124{
125 /* reset touchpad back to relative mode, gestures enabled */
126 synaptics_mode_cmd(psmouse, 0);
127}
128
Benjamin Tissoirese839ffa2017-03-02 14:13:53 -0800129#if defined(CONFIG_MOUSE_PS2_SYNAPTICS) || \
130 defined(CONFIG_MOUSE_PS2_SYNAPTICS_SMBUS)
Hans de Goede0f68f392014-05-19 22:54:09 -0700131
Hans de Goede43e19882014-04-19 22:26:41 -0700132/* This list has been kindly provided by Synaptics. */
133static const char * const topbuttonpad_pnp_ids[] = {
134 "LEN0017",
135 "LEN0018",
136 "LEN0019",
137 "LEN0023",
138 "LEN002A",
139 "LEN002B",
140 "LEN002C",
141 "LEN002D",
142 "LEN002E",
143 "LEN0033", /* Helix */
Hans de Goede0f68f392014-05-19 22:54:09 -0700144 "LEN0034", /* T431s, L440, L540, T540, W540, X1 Carbon 2nd */
Hans de Goede43e19882014-04-19 22:26:41 -0700145 "LEN0035", /* X240 */
146 "LEN0036", /* T440 */
Peter Hutterer8543cf12015-01-19 16:29:25 -0800147 "LEN0037", /* X1 Carbon 2nd */
Hans de Goede43e19882014-04-19 22:26:41 -0700148 "LEN0038",
Takashi Iwaie4742b12014-11-06 09:27:11 -0800149 "LEN0039", /* T440s */
Hans de Goede43e19882014-04-19 22:26:41 -0700150 "LEN0041",
151 "LEN0042", /* Yoga */
152 "LEN0045",
Hans de Goede43e19882014-04-19 22:26:41 -0700153 "LEN0047",
Hans de Goede43e19882014-04-19 22:26:41 -0700154 "LEN0049",
Peter Hutterer7f2ca8b2015-06-08 10:17:32 -0700155 "LEN2000", /* S540 */
Hans de Goede0f68f392014-05-19 22:54:09 -0700156 "LEN2001", /* Edge E431 */
Hans de Goedee76aed92014-07-14 17:12:21 -0700157 "LEN2002", /* Edge E531 */
Hans de Goede43e19882014-04-19 22:26:41 -0700158 "LEN2003",
159 "LEN2004", /* L440 */
160 "LEN2005",
Ramiro Morales98dc0702015-03-23 10:33:07 -0700161 "LEN2006", /* Edge E440/E540 */
Hans de Goede43e19882014-04-19 22:26:41 -0700162 "LEN2007",
163 "LEN2008",
164 "LEN2009",
165 "LEN200A",
166 "LEN200B",
167 NULL
168};
Andres Salomon55e3d922007-03-10 01:39:54 -0500169
Benjamin Tissoirese839ffa2017-03-02 14:13:53 -0800170static const char * const smbus_pnp_ids[] = {
171 /* all of the topbuttonpad_pnp_ids are valid, we just add some extras */
172 "LEN0048", /* X1 Carbon 3 */
173 "LEN0046", /* X250 */
174 "LEN004a", /* W541 */
Dmitry Torokhov9b207102017-08-18 12:08:13 -0700175 "LEN0072", /* X1 Carbon Gen 5 (2017) - Elan/ALPS trackpoint */
Edvard Holst15e2cff2018-02-03 11:46:15 -0800176 "LEN0073", /* X1 Carbon G5 (Elantech) */
Benjamin Tissoirese839ffa2017-03-02 14:13:53 -0800177 "LEN200f", /* T450s */
Dmitry Torokhovde4e3742014-12-29 14:43:44 -0800178 NULL
179};
180
Benjamin Tissoiresf4101ff2017-05-26 16:21:36 -0700181static const char * const forcepad_pnp_ids[] = {
182 "SYN300D",
183 "SYN3014",
184 NULL
185};
186
JJ Ding1a49a0a2012-05-10 22:32:00 -0700187/*
Andres Salomon55e3d922007-03-10 01:39:54 -0500188 * Send a command to the synpatics touchpad by special commands
189 */
Dmitry Torokhovf6c44422017-03-24 11:20:38 -0700190static int synaptics_send_cmd(struct psmouse *psmouse, u8 cmd, u8 *param)
Andres Salomon55e3d922007-03-10 01:39:54 -0500191{
Benjamin Tissoirese839ffa2017-03-02 14:13:53 -0800192 int error;
193
Dmitry Torokhov08be9542018-01-02 12:03:02 -0800194 error = ps2_sliced_command(&psmouse->ps2dev, cmd);
Benjamin Tissoirese839ffa2017-03-02 14:13:53 -0800195 if (error)
196 return error;
197
198 error = ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_GETINFO);
199 if (error)
200 return error;
201
Andres Salomon55e3d922007-03-10 01:39:54 -0500202 return 0;
203}
204
Dmitry Torokhov2c6ecbb2017-03-23 17:40:57 -0700205static int synaptics_query_int(struct psmouse *psmouse, u8 query_cmd, u32 *val)
206{
207 int error;
208 union {
209 __be32 be_val;
210 char buf[4];
211 } resp = { 0 };
212
213 error = synaptics_send_cmd(psmouse, query_cmd, resp.buf + 1);
214 if (error)
215 return error;
216
217 *val = be32_to_cpu(resp.be_val);
218 return 0;
219}
220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221/*
Benjamin Tissoirese839ffa2017-03-02 14:13:53 -0800222 * Identify Touchpad
223 * See also the SYN_ID_* macros
224 */
225static int synaptics_identify(struct psmouse *psmouse,
226 struct synaptics_device_info *info)
227{
Benjamin Tissoirese839ffa2017-03-02 14:13:53 -0800228 int error;
229
Dmitry Torokhov2c6ecbb2017-03-23 17:40:57 -0700230 error = synaptics_query_int(psmouse, SYN_QUE_IDENTIFY, &info->identity);
Benjamin Tissoirese839ffa2017-03-02 14:13:53 -0800231 if (error)
232 return error;
233
Benjamin Tissoirese839ffa2017-03-02 14:13:53 -0800234 return SYN_ID_IS_SYNAPTICS(info->identity) ? 0 : -ENXIO;
235}
236
237/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 * Read the model-id bytes from the touchpad
239 * see also SYN_MODEL_* macros
240 */
Dmitry Torokhov6c536942017-03-05 15:51:33 -0800241static int synaptics_model_id(struct psmouse *psmouse,
242 struct synaptics_device_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
Dmitry Torokhov2c6ecbb2017-03-23 17:40:57 -0700244 return synaptics_query_int(psmouse, SYN_QUE_MODEL, &info->model_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245}
246
Benjamin Tissoirese839ffa2017-03-02 14:13:53 -0800247/*
248 * Read the firmware id from the touchpad
249 */
250static int synaptics_firmware_id(struct psmouse *psmouse,
251 struct synaptics_device_info *info)
252{
Dmitry Torokhov2c6ecbb2017-03-23 17:40:57 -0700253 return synaptics_query_int(psmouse, SYN_QUE_FIRMWARE_ID,
254 &info->firmware_id);
Benjamin Tissoires06aa3742015-03-08 22:34:03 -0700255}
256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257/*
Benjamin Tissoires06aa3742015-03-08 22:34:03 -0700258 * Read the board id and the "More Extended Queries" from the touchpad
Daniel Kurtzc6bd9d42012-07-07 18:08:51 -0700259 * The board id is encoded in the "QUERY MODES" response
260 */
Dmitry Torokhov6c536942017-03-05 15:51:33 -0800261static int synaptics_query_modes(struct psmouse *psmouse,
262 struct synaptics_device_info *info)
Daniel Kurtzc6bd9d42012-07-07 18:08:51 -0700263{
Dmitry Torokhovf6c44422017-03-24 11:20:38 -0700264 u8 bid[3];
Dmitry Torokhov6c536942017-03-05 15:51:33 -0800265 int error;
Daniel Kurtzc6bd9d42012-07-07 18:08:51 -0700266
Benjamin Tissoiresb57a7122015-03-08 22:33:36 -0700267 /* firmwares prior 7.5 have no board_id encoded */
Dmitry Torokhov6c536942017-03-05 15:51:33 -0800268 if (SYN_ID_FULL(info->identity) < 0x705)
Benjamin Tissoiresb57a7122015-03-08 22:33:36 -0700269 return 0;
270
Dmitry Torokhov6c536942017-03-05 15:51:33 -0800271 error = synaptics_send_cmd(psmouse, SYN_QUE_MODES, bid);
272 if (error)
273 return error;
274
275 info->board_id = ((bid[0] & 0xfc) << 6) | bid[1];
Benjamin Tissoires06aa3742015-03-08 22:34:03 -0700276
277 if (SYN_MEXT_CAP_BIT(bid[0]))
Dmitry Torokhov2c6ecbb2017-03-23 17:40:57 -0700278 return synaptics_query_int(psmouse, SYN_QUE_MEXT_CAPAB_10,
279 &info->ext_cap_10);
Benjamin Tissoires06aa3742015-03-08 22:34:03 -0700280
Daniel Kurtzc6bd9d42012-07-07 18:08:51 -0700281 return 0;
282}
283
284/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 * Read the capability-bits from the touchpad
286 * see also the SYN_CAP_* macros
287 */
Dmitry Torokhov6c536942017-03-05 15:51:33 -0800288static int synaptics_capability(struct psmouse *psmouse,
289 struct synaptics_device_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
Dmitry Torokhov6c536942017-03-05 15:51:33 -0800291 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Dmitry Torokhov2c6ecbb2017-03-23 17:40:57 -0700293 error = synaptics_query_int(psmouse, SYN_QUE_CAPABILITIES,
294 &info->capabilities);
Dmitry Torokhov6c536942017-03-05 15:51:33 -0800295 if (error)
296 return error;
297
Dmitry Torokhov6c536942017-03-05 15:51:33 -0800298 info->ext_cap = info->ext_cap_0c = 0;
Takashi Iwai5f57d672010-04-19 10:37:21 -0700299
Dmitry Torokhov3619b8f2010-07-21 00:01:19 -0700300 /*
301 * Older firmwares had submodel ID fixed to 0x47
302 */
Dmitry Torokhov6c536942017-03-05 15:51:33 -0800303 if (SYN_ID_FULL(info->identity) < 0x705 &&
304 SYN_CAP_SUBMODEL_ID(info->capabilities) != 0x47) {
305 return -ENXIO;
Dmitry Torokhov3619b8f2010-07-21 00:01:19 -0700306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308 /*
309 * Unless capExtended is set the rest of the flags should be ignored
310 */
Dmitry Torokhov6c536942017-03-05 15:51:33 -0800311 if (!SYN_CAP_EXTENDED(info->capabilities))
312 info->capabilities = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Dmitry Torokhov6c536942017-03-05 15:51:33 -0800314 if (SYN_EXT_CAP_REQUESTS(info->capabilities) >= 1) {
Dmitry Torokhov2c6ecbb2017-03-23 17:40:57 -0700315 error = synaptics_query_int(psmouse, SYN_QUE_EXT_CAPAB,
316 &info->ext_cap);
317 if (error) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700318 psmouse_warn(psmouse,
319 "device claims to have extended capabilities, but I'm not able to read them.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 /*
322 * if nExtBtn is greater than 8 it should be considered
323 * invalid and treated as 0
324 */
Dmitry Torokhov6c536942017-03-05 15:51:33 -0800325 if (SYN_CAP_MULTI_BUTTON_NO(info->ext_cap) > 8)
Dmitry Torokhov2c6ecbb2017-03-23 17:40:57 -0700326 info->ext_cap &= ~SYN_CAP_MB_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 }
328 }
Takashi Iwai5f57d672010-04-19 10:37:21 -0700329
Dmitry Torokhov6c536942017-03-05 15:51:33 -0800330 if (SYN_EXT_CAP_REQUESTS(info->capabilities) >= 4) {
Dmitry Torokhov2c6ecbb2017-03-23 17:40:57 -0700331 error = synaptics_query_int(psmouse, SYN_QUE_EXT_CAPAB_0C,
332 &info->ext_cap_0c);
Dmitry Torokhov6c536942017-03-05 15:51:33 -0800333 if (error)
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700334 psmouse_warn(psmouse,
335 "device claims to have extended capability 0x0c, but I'm not able to read it.\n");
Takashi Iwai5f57d672010-04-19 10:37:21 -0700336 }
337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 return 0;
339}
340
341/*
Dmitry Torokhov83ba9ea82010-05-10 23:06:52 -0700342 * Read touchpad resolution and maximum reported coordinates
Tero Saarniec20a022009-06-10 23:27:24 -0700343 * Resolution is left zero if touchpad does not support the query
344 */
Dmitry Torokhov6c536942017-03-05 15:51:33 -0800345static int synaptics_resolution(struct psmouse *psmouse,
346 struct synaptics_device_info *info)
Tero Saarniec20a022009-06-10 23:27:24 -0700347{
Dmitry Torokhovf6c44422017-03-24 11:20:38 -0700348 u8 resp[3];
Dmitry Torokhov6c536942017-03-05 15:51:33 -0800349 int error;
Tero Saarniec20a022009-06-10 23:27:24 -0700350
Dmitry Torokhov6c536942017-03-05 15:51:33 -0800351 if (SYN_ID_MAJOR(info->identity) < 4)
Takashi Iwaibbddd192010-07-14 09:32:46 -0700352 return 0;
Tero Saarniec20a022009-06-10 23:27:24 -0700353
Dmitry Torokhov6c536942017-03-05 15:51:33 -0800354 error = synaptics_send_cmd(psmouse, SYN_QUE_RESOLUTION, resp);
355 if (!error) {
Dmitry Torokhova66413f2011-07-09 12:32:56 -0700356 if (resp[0] != 0 && (resp[1] & 0x80) && resp[2] != 0) {
Dmitry Torokhov6c536942017-03-05 15:51:33 -0800357 info->x_res = resp[0]; /* x resolution in units/mm */
358 info->y_res = resp[2]; /* y resolution in units/mm */
Dmitry Torokhov83ba9ea82010-05-10 23:06:52 -0700359 }
360 }
Tero Saarniec20a022009-06-10 23:27:24 -0700361
Dmitry Torokhov6c536942017-03-05 15:51:33 -0800362 if (SYN_EXT_CAP_REQUESTS(info->capabilities) >= 5 &&
363 SYN_CAP_MAX_DIMENSIONS(info->ext_cap_0c)) {
364 error = synaptics_send_cmd(psmouse,
365 SYN_QUE_EXT_MAX_COORDS, resp);
366 if (error) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700367 psmouse_warn(psmouse,
368 "device claims to have max coordinates query, but I'm not able to read it.\n");
Dmitry Torokhov83ba9ea82010-05-10 23:06:52 -0700369 } else {
Dmitry Torokhov6c536942017-03-05 15:51:33 -0800370 info->x_max = (resp[0] << 5) | ((resp[1] & 0x0f) << 1);
371 info->y_max = (resp[2] << 5) | ((resp[1] & 0xf0) >> 3);
Daniel Martin9aff6592015-03-08 22:28:29 -0700372 psmouse_info(psmouse,
373 "queried max coordinates: x [..%d], y [..%d]\n",
Dmitry Torokhov6c536942017-03-05 15:51:33 -0800374 info->x_max, info->y_max);
Dmitry Torokhova66413f2011-07-09 12:32:56 -0700375 }
376 }
377
Dmitry Torokhov6c536942017-03-05 15:51:33 -0800378 if (SYN_CAP_MIN_DIMENSIONS(info->ext_cap_0c) &&
379 (SYN_EXT_CAP_REQUESTS(info->capabilities) >= 7 ||
Daniel Martinac097932015-03-08 22:28:40 -0700380 /*
381 * Firmware v8.1 does not report proper number of extended
382 * capabilities, but has been proven to report correct min
383 * coordinates.
384 */
Dmitry Torokhov6c536942017-03-05 15:51:33 -0800385 SYN_ID_FULL(info->identity) == 0x801)) {
386 error = synaptics_send_cmd(psmouse,
387 SYN_QUE_EXT_MIN_COORDS, resp);
388 if (error) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700389 psmouse_warn(psmouse,
390 "device claims to have min coordinates query, but I'm not able to read it.\n");
Dmitry Torokhova66413f2011-07-09 12:32:56 -0700391 } else {
Dmitry Torokhov6c536942017-03-05 15:51:33 -0800392 info->x_min = (resp[0] << 5) | ((resp[1] & 0x0f) << 1);
393 info->y_min = (resp[2] << 5) | ((resp[1] & 0xf0) >> 3);
Daniel Martin9aff6592015-03-08 22:28:29 -0700394 psmouse_info(psmouse,
395 "queried min coordinates: x [%d..], y [%d..]\n",
Dmitry Torokhov6c536942017-03-05 15:51:33 -0800396 info->x_min, info->y_min);
Dmitry Torokhov83ba9ea82010-05-10 23:06:52 -0700397 }
Tero Saarniec20a022009-06-10 23:27:24 -0700398 }
399
400 return 0;
401}
402
Dmitry Torokhov6c536942017-03-05 15:51:33 -0800403static int synaptics_query_hardware(struct psmouse *psmouse,
404 struct synaptics_device_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
Dmitry Torokhov6c536942017-03-05 15:51:33 -0800406 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Eric Biggers275555112017-05-29 19:57:19 -0700408 memset(info, 0, sizeof(*info));
409
Dmitry Torokhov6c536942017-03-05 15:51:33 -0800410 error = synaptics_identify(psmouse, info);
411 if (error)
412 return error;
413
414 error = synaptics_model_id(psmouse, info);
415 if (error)
416 return error;
417
418 error = synaptics_firmware_id(psmouse, info);
419 if (error)
420 return error;
421
422 error = synaptics_query_modes(psmouse, info);
423 if (error)
424 return error;
425
426 error = synaptics_capability(psmouse, info);
427 if (error)
428 return error;
429
430 error = synaptics_resolution(psmouse, info);
431 if (error)
432 return error;
Daniel Martin8b04bab2015-03-08 22:27:37 -0700433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 return 0;
435}
436
Benjamin Tissoirese839ffa2017-03-02 14:13:53 -0800437#endif /* CONFIG_MOUSE_PS2_SYNAPTICS || CONFIG_MOUSE_PS2_SYNAPTICS_SMBUS */
438
439#ifdef CONFIG_MOUSE_PS2_SYNAPTICS
440
441static bool cr48_profile_sensor;
442
443#define ANY_BOARD_ID 0
444struct min_max_quirk {
445 const char * const *pnp_ids;
446 struct {
447 u32 min, max;
448 } board_id;
449 u32 x_min, x_max, y_min, y_max;
450};
451
452static const struct min_max_quirk min_max_pnpid_table[] = {
453 {
454 (const char * const []){"LEN0033", NULL},
455 {ANY_BOARD_ID, ANY_BOARD_ID},
456 1024, 5052, 2258, 4832
457 },
458 {
459 (const char * const []){"LEN0042", NULL},
460 {ANY_BOARD_ID, ANY_BOARD_ID},
461 1232, 5710, 1156, 4696
462 },
463 {
464 (const char * const []){"LEN0034", "LEN0036", "LEN0037",
465 "LEN0039", "LEN2002", "LEN2004",
466 NULL},
467 {ANY_BOARD_ID, 2961},
468 1024, 5112, 2024, 4832
469 },
470 {
471 (const char * const []){"LEN2000", NULL},
472 {ANY_BOARD_ID, ANY_BOARD_ID},
473 1024, 5113, 2021, 4832
474 },
475 {
476 (const char * const []){"LEN2001", NULL},
477 {ANY_BOARD_ID, ANY_BOARD_ID},
478 1024, 5022, 2508, 4832
479 },
480 {
481 (const char * const []){"LEN2006", NULL},
482 {2691, 2691},
483 1024, 5045, 2457, 4832
484 },
485 {
486 (const char * const []){"LEN2006", NULL},
487 {ANY_BOARD_ID, ANY_BOARD_ID},
488 1264, 5675, 1171, 4688
489 },
490 { }
491};
492
Benjamin Tissoirese839ffa2017-03-02 14:13:53 -0800493/*****************************************************************************
494 * Synaptics communications functions
495 ****************************************************************************/
496
497/*
498 * Synaptics touchpads report the y coordinate from bottom to top, which is
499 * opposite from what userspace expects.
500 * This function is used to invert y before reporting.
501 */
502static int synaptics_invert_y(int y)
503{
504 return YMAX_NOMINAL + YMIN_NOMINAL - y;
505}
506
507/*
508 * Apply quirk(s) if the hardware matches
509 */
510static void synaptics_apply_quirks(struct psmouse *psmouse,
511 struct synaptics_device_info *info)
512{
513 int i;
514
515 for (i = 0; min_max_pnpid_table[i].pnp_ids; i++) {
516 if (!psmouse_matches_pnp_id(psmouse,
517 min_max_pnpid_table[i].pnp_ids))
518 continue;
519
520 if (min_max_pnpid_table[i].board_id.min != ANY_BOARD_ID &&
521 info->board_id < min_max_pnpid_table[i].board_id.min)
522 continue;
523
524 if (min_max_pnpid_table[i].board_id.max != ANY_BOARD_ID &&
525 info->board_id > min_max_pnpid_table[i].board_id.max)
526 continue;
527
528 info->x_min = min_max_pnpid_table[i].x_min;
529 info->x_max = min_max_pnpid_table[i].x_max;
530 info->y_min = min_max_pnpid_table[i].y_min;
531 info->y_max = min_max_pnpid_table[i].y_max;
532 psmouse_info(psmouse,
533 "quirked min/max coordinates: x [%d..%d], y [%d..%d]\n",
534 info->x_min, info->x_max,
535 info->y_min, info->y_max);
536 break;
537 }
538}
539
Anthony Martin3f9db522017-08-28 10:26:12 -0700540static bool synaptics_has_agm(struct synaptics_data *priv)
541{
542 return (SYN_CAP_ADV_GESTURE(priv->info.ext_cap_0c) ||
543 SYN_CAP_IMAGE_SENSOR(priv->info.ext_cap_0c));
544}
545
Daniel Drake7968a5d2011-11-08 00:00:35 -0800546static int synaptics_set_advanced_gesture_mode(struct psmouse *psmouse)
547{
Dmitry Torokhovf6c44422017-03-24 11:20:38 -0700548 static u8 param = 0xc8;
Dmitry Torokhov212baf02017-03-23 18:38:14 -0700549 int error;
Daniel Drake7968a5d2011-11-08 00:00:35 -0800550
Dmitry Torokhov08be9542018-01-02 12:03:02 -0800551 error = ps2_sliced_command(&psmouse->ps2dev, SYN_QUE_MODEL);
Dmitry Torokhov212baf02017-03-23 18:38:14 -0700552 if (error)
553 return error;
Daniel Drake7968a5d2011-11-08 00:00:35 -0800554
Dmitry Torokhov212baf02017-03-23 18:38:14 -0700555 error = ps2_command(&psmouse->ps2dev, &param, PSMOUSE_CMD_SETRATE);
556 if (error)
557 return error;
Daniel Drake7968a5d2011-11-08 00:00:35 -0800558
Daniel Drake7968a5d2011-11-08 00:00:35 -0800559 return 0;
560}
561
562static int synaptics_set_mode(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
564 struct synaptics_data *priv = psmouse->private;
Dmitry Torokhov212baf02017-03-23 18:38:14 -0700565 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Daniel Drake7968a5d2011-11-08 00:00:35 -0800567 priv->mode = 0;
Dmitry Torokhov62d78462015-10-02 10:31:32 -0700568 if (priv->absolute_mode)
Daniel Drake7968a5d2011-11-08 00:00:35 -0800569 priv->mode |= SYN_BIT_ABSOLUTE_MODE;
Dmitry Torokhov62d78462015-10-02 10:31:32 -0700570 if (priv->disable_gesture)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 priv->mode |= SYN_BIT_DISABLE_GESTURE;
Daniel Drake7968a5d2011-11-08 00:00:35 -0800572 if (psmouse->rate >= 80)
573 priv->mode |= SYN_BIT_HIGH_RATE;
Dmitry Torokhov6c536942017-03-05 15:51:33 -0800574 if (SYN_CAP_EXTENDED(priv->info.capabilities))
Dmitry Torokhov62d78462015-10-02 10:31:32 -0700575 priv->mode |= SYN_BIT_W_MODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Dmitry Torokhov212baf02017-03-23 18:38:14 -0700577 error = synaptics_mode_cmd(psmouse, priv->mode);
578 if (error)
579 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Anthony Martin3f9db522017-08-28 10:26:12 -0700581 if (priv->absolute_mode && synaptics_has_agm(priv)) {
Dmitry Torokhov212baf02017-03-23 18:38:14 -0700582 error = synaptics_set_advanced_gesture_mode(psmouse);
583 if (error) {
584 psmouse_err(psmouse,
585 "Advanced gesture mode init failed: %d\n",
586 error);
587 return error;
588 }
Daniel Drake7968a5d2011-11-08 00:00:35 -0800589 }
590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 return 0;
592}
593
594static void synaptics_set_rate(struct psmouse *psmouse, unsigned int rate)
595{
596 struct synaptics_data *priv = psmouse->private;
597
598 if (rate >= 80) {
599 priv->mode |= SYN_BIT_HIGH_RATE;
600 psmouse->rate = 80;
601 } else {
602 priv->mode &= ~SYN_BIT_HIGH_RATE;
603 psmouse->rate = 40;
604 }
605
606 synaptics_mode_cmd(psmouse, priv->mode);
607}
608
609/*****************************************************************************
610 * Synaptics pass-through PS/2 port support
611 ****************************************************************************/
Dmitry Torokhovf6c44422017-03-24 11:20:38 -0700612static int synaptics_pt_write(struct serio *serio, u8 c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
614 struct psmouse *parent = serio_get_drvdata(serio->parent);
Dmitry Torokhov212baf02017-03-23 18:38:14 -0700615 u8 rate_param = SYN_PS_CLIENT_CMD; /* indicates that we want pass-through port */
616 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Dmitry Torokhov08be9542018-01-02 12:03:02 -0800618 error = ps2_sliced_command(&parent->ps2dev, c);
Dmitry Torokhov212baf02017-03-23 18:38:14 -0700619 if (error)
620 return error;
621
622 error = ps2_command(&parent->ps2dev, &rate_param, PSMOUSE_CMD_SETRATE);
623 if (error)
624 return error;
625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 return 0;
627}
628
Dmitry Torokhova8b3c0f2010-10-04 21:46:10 -0700629static int synaptics_pt_start(struct serio *serio)
630{
631 struct psmouse *parent = serio_get_drvdata(serio->parent);
632 struct synaptics_data *priv = parent->private;
633
634 serio_pause_rx(parent->ps2dev.serio);
635 priv->pt_port = serio;
636 serio_continue_rx(parent->ps2dev.serio);
637
638 return 0;
639}
640
641static void synaptics_pt_stop(struct serio *serio)
642{
643 struct psmouse *parent = serio_get_drvdata(serio->parent);
644 struct synaptics_data *priv = parent->private;
645
646 serio_pause_rx(parent->ps2dev.serio);
647 priv->pt_port = NULL;
648 serio_continue_rx(parent->ps2dev.serio);
649}
650
Dmitry Torokhovf6c44422017-03-24 11:20:38 -0700651static int synaptics_is_pt_packet(u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
653 return (buf[0] & 0xFC) == 0x84 && (buf[3] & 0xCC) == 0xC4;
654}
655
Dmitry Torokhovf6c44422017-03-24 11:20:38 -0700656static void synaptics_pass_pt_packet(struct serio *ptport, u8 *packet)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
658 struct psmouse *child = serio_get_drvdata(ptport);
659
660 if (child && child->state == PSMOUSE_ACTIVATED) {
Dmitry Torokhovbf23cfc2017-02-09 11:34:08 -0800661 serio_interrupt(ptport, packet[1], 0);
David Howells7d12e782006-10-05 14:55:46 +0100662 serio_interrupt(ptport, packet[4], 0);
663 serio_interrupt(ptport, packet[5], 0);
Sergey Vlasov33fdfa92005-07-24 00:53:32 -0500664 if (child->pktsize == 4)
David Howells7d12e782006-10-05 14:55:46 +0100665 serio_interrupt(ptport, packet[2], 0);
Benjamin Tissoirescdd9dc12015-03-08 22:35:41 -0700666 } else {
David Howells7d12e782006-10-05 14:55:46 +0100667 serio_interrupt(ptport, packet[1], 0);
Benjamin Tissoirescdd9dc12015-03-08 22:35:41 -0700668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669}
670
671static void synaptics_pt_activate(struct psmouse *psmouse)
672{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 struct synaptics_data *priv = psmouse->private;
Dmitry Torokhova8b3c0f2010-10-04 21:46:10 -0700674 struct psmouse *child = serio_get_drvdata(priv->pt_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
676 /* adjust the touchpad to child's choice of protocol */
677 if (child) {
Sergey Vlasov33fdfa92005-07-24 00:53:32 -0500678 if (child->pktsize == 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 priv->mode |= SYN_BIT_FOUR_BYTE_CLIENT;
680 else
681 priv->mode &= ~SYN_BIT_FOUR_BYTE_CLIENT;
682
683 if (synaptics_mode_cmd(psmouse, priv->mode))
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700684 psmouse_warn(psmouse,
685 "failed to switch guest protocol\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 }
687}
688
689static void synaptics_pt_create(struct psmouse *psmouse)
690{
691 struct serio *serio;
692
Eric Sesterhennb39787a2006-03-14 00:09:16 -0500693 serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 if (!serio) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700695 psmouse_err(psmouse,
696 "not enough memory for pass-through port\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 return;
698 }
699
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 serio->id.type = SERIO_PS_PSTHRU;
701 strlcpy(serio->name, "Synaptics pass-through", sizeof(serio->name));
702 strlcpy(serio->phys, "synaptics-pt/serio0", sizeof(serio->name));
703 serio->write = synaptics_pt_write;
Dmitry Torokhova8b3c0f2010-10-04 21:46:10 -0700704 serio->start = synaptics_pt_start;
705 serio->stop = synaptics_pt_stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 serio->parent = psmouse->ps2dev.serio;
707
708 psmouse->pt_activate = synaptics_pt_activate;
709
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700710 psmouse_info(psmouse, "serio: %s port at %s\n",
711 serio->name, psmouse->phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 serio_register_port(serio);
713}
714
715/*****************************************************************************
716 * Functions to interpret the absolute mode packets
717 ****************************************************************************/
718
Dmitry Torokhovf6c44422017-03-24 11:20:38 -0700719static void synaptics_parse_agm(const u8 buf[],
Daniel Kurtza6ca40c2011-08-23 23:02:31 -0700720 struct synaptics_data *priv,
721 struct synaptics_hw_state *hw)
Daniel Kurtz7afdb842011-08-23 23:00:33 -0700722{
723 struct synaptics_hw_state *agm = &priv->agm;
Daniel Kurtza6ca40c2011-08-23 23:02:31 -0700724 int agm_packet_type;
Daniel Kurtz7afdb842011-08-23 23:00:33 -0700725
Daniel Kurtza6ca40c2011-08-23 23:02:31 -0700726 agm_packet_type = (buf[5] & 0x30) >> 4;
727 switch (agm_packet_type) {
728 case 1:
729 /* Gesture packet: (x, y, z) half resolution */
730 agm->w = hw->w;
731 agm->x = (((buf[4] & 0x0f) << 8) | buf[1]) << 1;
732 agm->y = (((buf[4] & 0xf0) << 4) | buf[2]) << 1;
733 agm->z = ((buf[3] & 0x30) | (buf[5] & 0x0f)) << 1;
734 break;
735
736 case 2:
Benjamin Tissoirese9e85202014-12-29 14:15:24 -0800737 /* AGM-CONTACT packet: we are only interested in the count */
738 priv->agm_count = buf[1];
Daniel Kurtza6ca40c2011-08-23 23:02:31 -0700739 break;
740
741 default:
742 break;
743 }
Daniel Kurtz7afdb842011-08-23 23:00:33 -0700744}
745
Dmitry Torokhovf6c44422017-03-24 11:20:38 -0700746static void synaptics_parse_ext_buttons(const u8 buf[],
Dmitry Torokhovdc5465d2015-03-08 22:30:43 -0700747 struct synaptics_data *priv,
748 struct synaptics_hw_state *hw)
749{
750 unsigned int ext_bits =
Dmitry Torokhov6c536942017-03-05 15:51:33 -0800751 (SYN_CAP_MULTI_BUTTON_NO(priv->info.ext_cap) + 1) >> 1;
Dmitry Torokhovdc5465d2015-03-08 22:30:43 -0700752 unsigned int ext_mask = GENMASK(ext_bits - 1, 0);
753
754 hw->ext_buttons = buf[4] & ext_mask;
755 hw->ext_buttons |= (buf[5] & ext_mask) << ext_bits;
756}
757
Dmitry Torokhovf6c44422017-03-24 11:20:38 -0700758static int synaptics_parse_hw_state(const u8 buf[],
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100759 struct synaptics_data *priv,
760 struct synaptics_hw_state *hw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
762 memset(hw, 0, sizeof(struct synaptics_hw_state));
763
Dmitry Torokhov6c536942017-03-05 15:51:33 -0800764 if (SYN_MODEL_NEWABS(priv->info.model_id)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 hw->w = (((buf[0] & 0x30) >> 2) |
766 ((buf[0] & 0x04) >> 1) |
767 ((buf[3] & 0x04) >> 2));
768
Anthony Martin3f9db522017-08-28 10:26:12 -0700769 if (synaptics_has_agm(priv) && hw->w == 2) {
Dmitry Torokhov5715fc72014-08-30 13:51:06 -0700770 synaptics_parse_agm(buf, priv, hw);
771 return 1;
772 }
773
774 hw->x = (((buf[3] & 0x10) << 8) |
775 ((buf[1] & 0x0f) << 8) |
776 buf[4]);
777 hw->y = (((buf[3] & 0x20) << 7) |
778 ((buf[1] & 0xf0) << 4) |
779 buf[5]);
780 hw->z = buf[2];
781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 hw->left = (buf[0] & 0x01) ? 1 : 0;
783 hw->right = (buf[0] & 0x02) ? 1 : 0;
784
Dmitry Torokhovde4e3742014-12-29 14:43:44 -0800785 if (priv->is_forcepad) {
Dmitry Torokhov5715fc72014-08-30 13:51:06 -0700786 /*
787 * ForcePads, like Clickpads, use middle button
788 * bits to report primary button clicks.
789 * Unfortunately they report primary button not
790 * only when user presses on the pad above certain
791 * threshold, but also when there are more than one
792 * finger on the touchpad, which interferes with
793 * out multi-finger gestures.
794 */
795 if (hw->z == 0) {
796 /* No contacts */
797 priv->press = priv->report_press = false;
798 } else if (hw->w >= 4 && ((buf[0] ^ buf[3]) & 0x01)) {
799 /*
800 * Single-finger touch with pressure above
801 * the threshold. If pressure stays long
802 * enough, we'll start reporting primary
803 * button. We rely on the device continuing
804 * sending data even if finger does not
805 * move.
806 */
807 if (!priv->press) {
808 priv->press_start = jiffies;
809 priv->press = true;
810 } else if (time_after(jiffies,
811 priv->press_start +
812 msecs_to_jiffies(50))) {
813 priv->report_press = true;
814 }
815 } else {
816 priv->press = false;
817 }
818
819 hw->left = priv->report_press;
820
Dmitry Torokhov6c536942017-03-05 15:51:33 -0800821 } else if (SYN_CAP_CLICKPAD(priv->info.ext_cap_0c)) {
Takashi Iwai5f57d672010-04-19 10:37:21 -0700822 /*
823 * Clickpad's button is transmitted as middle button,
824 * however, since it is primary button, we will report
825 * it as BTN_LEFT.
826 */
827 hw->left = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0;
828
Dmitry Torokhov6c536942017-03-05 15:51:33 -0800829 } else if (SYN_CAP_MIDDLE_BUTTON(priv->info.capabilities)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 hw->middle = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0;
831 if (hw->w == 2)
Dmitry Torokhovf6c44422017-03-24 11:20:38 -0700832 hw->scroll = (s8)buf[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 }
834
Dmitry Torokhov6c536942017-03-05 15:51:33 -0800835 if (SYN_CAP_FOUR_BUTTON(priv->info.capabilities)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 hw->up = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0;
837 hw->down = ((buf[0] ^ buf[3]) & 0x02) ? 1 : 0;
838 }
839
Dmitry Torokhov6c536942017-03-05 15:51:33 -0800840 if (SYN_CAP_MULTI_BUTTON_NO(priv->info.ext_cap) > 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 ((buf[0] ^ buf[3]) & 0x02)) {
Dmitry Torokhovdc5465d2015-03-08 22:30:43 -0700842 synaptics_parse_ext_buttons(buf, priv, hw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 }
844 } else {
845 hw->x = (((buf[1] & 0x1f) << 8) | buf[2]);
846 hw->y = (((buf[4] & 0x1f) << 8) | buf[5]);
847
848 hw->z = (((buf[0] & 0x30) << 2) | (buf[3] & 0x3F));
849 hw->w = (((buf[1] & 0x80) >> 4) | ((buf[0] & 0x04) >> 1));
850
851 hw->left = (buf[0] & 0x01) ? 1 : 0;
852 hw->right = (buf[0] & 0x02) ? 1 : 0;
853 }
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100854
Seth Forshee824efd32012-09-28 10:29:21 -0700855 /*
856 * Convert wrap-around values to negative. (X|Y)_MAX_POSITIVE
857 * is used by some firmware to indicate a finger at the edge of
858 * the touchpad whose precise position cannot be determined, so
859 * convert these values to the maximum axis value.
860 */
Seth Forsheec03945062012-07-24 23:54:11 -0700861 if (hw->x > X_MAX_POSITIVE)
862 hw->x -= 1 << ABS_POS_BITS;
Seth Forshee824efd32012-09-28 10:29:21 -0700863 else if (hw->x == X_MAX_POSITIVE)
864 hw->x = XMAX;
865
Seth Forsheec03945062012-07-24 23:54:11 -0700866 if (hw->y > Y_MAX_POSITIVE)
867 hw->y -= 1 << ABS_POS_BITS;
Seth Forshee824efd32012-09-28 10:29:21 -0700868 else if (hw->y == Y_MAX_POSITIVE)
869 hw->y = YMAX;
Seth Forsheec03945062012-07-24 23:54:11 -0700870
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100871 return 0;
872}
873
Daniel Kurtzbea9f0f2011-07-06 22:42:52 -0700874static void synaptics_report_semi_mt_slot(struct input_dev *dev, int slot,
875 bool active, int x, int y)
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100876{
877 input_mt_slot(dev, slot);
878 input_mt_report_slot_state(dev, MT_TOOL_FINGER, active);
879 if (active) {
880 input_report_abs(dev, ABS_MT_POSITION_X, x);
Daniel Kurtz6de58dd2011-08-23 23:00:24 -0700881 input_report_abs(dev, ABS_MT_POSITION_Y, synaptics_invert_y(y));
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100882 }
883}
884
885static void synaptics_report_semi_mt_data(struct input_dev *dev,
886 const struct synaptics_hw_state *a,
887 const struct synaptics_hw_state *b,
888 int num_fingers)
889{
890 if (num_fingers >= 2) {
Daniel Kurtzbea9f0f2011-07-06 22:42:52 -0700891 synaptics_report_semi_mt_slot(dev, 0, true, min(a->x, b->x),
892 min(a->y, b->y));
893 synaptics_report_semi_mt_slot(dev, 1, true, max(a->x, b->x),
894 max(a->y, b->y));
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100895 } else if (num_fingers == 1) {
Daniel Kurtzbea9f0f2011-07-06 22:42:52 -0700896 synaptics_report_semi_mt_slot(dev, 0, true, a->x, a->y);
897 synaptics_report_semi_mt_slot(dev, 1, false, 0, 0);
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100898 } else {
Daniel Kurtzbea9f0f2011-07-06 22:42:52 -0700899 synaptics_report_semi_mt_slot(dev, 0, false, 0, 0);
900 synaptics_report_semi_mt_slot(dev, 1, false, 0, 0);
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902}
903
Benjamin Tissoiresebc80842015-03-08 22:32:43 -0700904static void synaptics_report_ext_buttons(struct psmouse *psmouse,
905 const struct synaptics_hw_state *hw)
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700906{
907 struct input_dev *dev = psmouse->dev;
908 struct synaptics_data *priv = psmouse->private;
Dmitry Torokhov6c536942017-03-05 15:51:33 -0800909 int ext_bits = (SYN_CAP_MULTI_BUTTON_NO(priv->info.ext_cap) + 1) >> 1;
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700910 int i;
911
Dmitry Torokhov6c536942017-03-05 15:51:33 -0800912 if (!SYN_CAP_MULTI_BUTTON_NO(priv->info.ext_cap))
Benjamin Tissoiresebc80842015-03-08 22:32:43 -0700913 return;
914
Benjamin Tissoires82be7882016-03-17 17:12:54 -0700915 /* Bug in FW 8.1 & 8.2, buttons are reported only when ExtBit is 1 */
Dmitry Torokhov6c536942017-03-05 15:51:33 -0800916 if ((SYN_ID_FULL(priv->info.identity) == 0x801 ||
917 SYN_ID_FULL(priv->info.identity) == 0x802) &&
Benjamin Tissoiresebc80842015-03-08 22:32:43 -0700918 !((psmouse->packet[0] ^ psmouse->packet[3]) & 0x02))
919 return;
920
Dmitry Torokhov6c536942017-03-05 15:51:33 -0800921 if (!SYN_CAP_EXT_BUTTONS_STICK(priv->info.ext_cap_10)) {
Benjamin Tissoirescdd9dc12015-03-08 22:35:41 -0700922 for (i = 0; i < ext_bits; i++) {
923 input_report_key(dev, BTN_0 + 2 * i,
Dmitry Torokhov991d29fe02017-03-23 14:56:06 -0700924 hw->ext_buttons & BIT(i));
Benjamin Tissoirescdd9dc12015-03-08 22:35:41 -0700925 input_report_key(dev, BTN_1 + 2 * i,
Dmitry Torokhov991d29fe02017-03-23 14:56:06 -0700926 hw->ext_buttons & BIT(i + ext_bits));
Benjamin Tissoirescdd9dc12015-03-08 22:35:41 -0700927 }
928 return;
Benjamin Tissoiresebc80842015-03-08 22:32:43 -0700929 }
Benjamin Tissoirescdd9dc12015-03-08 22:35:41 -0700930
931 /*
932 * This generation of touchpads has the trackstick buttons
933 * physically wired to the touchpad. Re-route them through
934 * the pass-through interface.
935 */
Dmitry Torokhovbf23cfc2017-02-09 11:34:08 -0800936 if (priv->pt_port) {
937 u8 pt_buttons;
Benjamin Tissoirescdd9dc12015-03-08 22:35:41 -0700938
Dmitry Torokhovbf23cfc2017-02-09 11:34:08 -0800939 /* The trackstick expects at most 3 buttons */
Dmitry Torokhov996b9ee2017-03-23 10:02:50 -0700940 pt_buttons = SYN_EXT_BUTTON_STICK_L(hw->ext_buttons) |
941 SYN_EXT_BUTTON_STICK_R(hw->ext_buttons) << 1 |
942 SYN_EXT_BUTTON_STICK_M(hw->ext_buttons) << 2;
Benjamin Tissoirescdd9dc12015-03-08 22:35:41 -0700943
Dmitry Torokhovbf23cfc2017-02-09 11:34:08 -0800944 serio_interrupt(priv->pt_port,
945 PSMOUSE_OOB_EXTRA_BTNS, SERIO_OOB_DATA);
946 serio_interrupt(priv->pt_port, pt_buttons, SERIO_OOB_DATA);
947 }
Benjamin Tissoiresebc80842015-03-08 22:32:43 -0700948}
949
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700950static void synaptics_report_buttons(struct psmouse *psmouse,
951 const struct synaptics_hw_state *hw)
952{
953 struct input_dev *dev = psmouse->dev;
954 struct synaptics_data *priv = psmouse->private;
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700955
956 input_report_key(dev, BTN_LEFT, hw->left);
957 input_report_key(dev, BTN_RIGHT, hw->right);
958
Dmitry Torokhov6c536942017-03-05 15:51:33 -0800959 if (SYN_CAP_MIDDLE_BUTTON(priv->info.capabilities))
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700960 input_report_key(dev, BTN_MIDDLE, hw->middle);
961
Dmitry Torokhov6c536942017-03-05 15:51:33 -0800962 if (SYN_CAP_FOUR_BUTTON(priv->info.capabilities)) {
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700963 input_report_key(dev, BTN_FORWARD, hw->up);
964 input_report_key(dev, BTN_BACK, hw->down);
965 }
966
Benjamin Tissoiresebc80842015-03-08 22:32:43 -0700967 synaptics_report_ext_buttons(psmouse, hw);
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700968}
969
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700970static void synaptics_report_mt_data(struct psmouse *psmouse,
Benjamin Tissoirese9e85202014-12-29 14:15:24 -0800971 const struct synaptics_hw_state *sgm,
972 int num_fingers)
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700973{
974 struct input_dev *dev = psmouse->dev;
975 struct synaptics_data *priv = psmouse->private;
Benjamin Tissoirese9e85202014-12-29 14:15:24 -0800976 const struct synaptics_hw_state *hw[2] = { sgm, &priv->agm };
977 struct input_mt_pos pos[2];
978 int slot[2], nsemi, i;
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700979
Benjamin Tissoirese9e85202014-12-29 14:15:24 -0800980 nsemi = clamp_val(num_fingers, 0, 2);
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700981
Benjamin Tissoirese9e85202014-12-29 14:15:24 -0800982 for (i = 0; i < nsemi; i++) {
983 pos[i].x = hw[i]->x;
984 pos[i].y = synaptics_invert_y(hw[i]->y);
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700985 }
986
Dmitry Torokhov6c536942017-03-05 15:51:33 -0800987 input_mt_assign_slots(dev, slot, pos, nsemi, DMAX * priv->info.x_res);
Benjamin Tissoirese9e85202014-12-29 14:15:24 -0800988
989 for (i = 0; i < nsemi; i++) {
990 input_mt_slot(dev, slot[i]);
991 input_mt_report_slot_state(dev, MT_TOOL_FINGER, true);
992 input_report_abs(dev, ABS_MT_POSITION_X, pos[i].x);
993 input_report_abs(dev, ABS_MT_POSITION_Y, pos[i].y);
994 input_report_abs(dev, ABS_MT_PRESSURE, hw[i]->z);
995 }
996
997 input_mt_drop_unused(dev);
998
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700999 /* Don't use active slot count to generate BTN_TOOL events. */
1000 input_mt_report_pointer_emulation(dev, false);
1001
1002 /* Send the number of fingers reported by touchpad itself. */
Benjamin Tissoirese9e85202014-12-29 14:15:24 -08001003 input_mt_report_finger_count(dev, num_fingers);
Daniel Kurtz3cdfee92011-08-23 23:02:25 -07001004
1005 synaptics_report_buttons(psmouse, sgm);
1006
1007 input_sync(dev);
1008}
1009
1010static void synaptics_image_sensor_process(struct psmouse *psmouse,
1011 struct synaptics_hw_state *sgm)
1012{
Daniel Kurtz4dc772d2011-08-23 23:02:40 -07001013 struct synaptics_data *priv = psmouse->private;
Benjamin Tissoirese9e85202014-12-29 14:15:24 -08001014 int num_fingers;
Daniel Kurtz4dc772d2011-08-23 23:02:40 -07001015
1016 /*
1017 * Update mt_state using the new finger count and current mt_state.
1018 */
Daniel Kurtz3cdfee92011-08-23 23:02:25 -07001019 if (sgm->z == 0)
Benjamin Tissoirese9e85202014-12-29 14:15:24 -08001020 num_fingers = 0;
Daniel Kurtz3cdfee92011-08-23 23:02:25 -07001021 else if (sgm->w >= 4)
Benjamin Tissoirese9e85202014-12-29 14:15:24 -08001022 num_fingers = 1;
Daniel Kurtz3cdfee92011-08-23 23:02:25 -07001023 else if (sgm->w == 0)
Benjamin Tissoirese9e85202014-12-29 14:15:24 -08001024 num_fingers = 2;
1025 else if (sgm->w == 1)
1026 num_fingers = priv->agm_count ? priv->agm_count : 3;
Daniel Kurtz6b4b49f2011-08-23 23:02:56 -07001027 else
Benjamin Tissoirese9e85202014-12-29 14:15:24 -08001028 num_fingers = 4;
Daniel Kurtz3cdfee92011-08-23 23:02:25 -07001029
1030 /* Send resulting input events to user space */
Benjamin Tissoirese9e85202014-12-29 14:15:24 -08001031 synaptics_report_mt_data(psmouse, sgm, num_fingers);
Daniel Kurtz3cdfee92011-08-23 23:02:25 -07001032}
1033
Anthony Martin3f9db522017-08-28 10:26:12 -07001034static bool synaptics_has_multifinger(struct synaptics_data *priv)
1035{
1036 if (SYN_CAP_MULTIFINGER(priv->info.capabilities))
1037 return true;
1038
1039 /* Advanced gesture mode also sends multi finger data */
1040 return synaptics_has_agm(priv);
1041}
1042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043/*
1044 * called for each full received packet from the touchpad
1045 */
1046static void synaptics_process_packet(struct psmouse *psmouse)
1047{
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001048 struct input_dev *dev = psmouse->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 struct synaptics_data *priv = psmouse->private;
Dmitry Torokhov6c536942017-03-05 15:51:33 -08001050 struct synaptics_device_info *info = &priv->info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 struct synaptics_hw_state hw;
1052 int num_fingers;
1053 int finger_width;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Henrik Rydbergfec6e522010-12-21 18:11:25 +01001055 if (synaptics_parse_hw_state(psmouse->packet, priv, &hw))
1056 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
Dmitry Torokhov6c536942017-03-05 15:51:33 -08001058 if (SYN_CAP_IMAGE_SENSOR(info->ext_cap_0c)) {
Daniel Kurtz3cdfee92011-08-23 23:02:25 -07001059 synaptics_image_sensor_process(psmouse, &hw);
1060 return;
1061 }
1062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 if (hw.scroll) {
1064 priv->scroll += hw.scroll;
1065
1066 while (priv->scroll >= 4) {
1067 input_report_key(dev, BTN_BACK, !hw.down);
1068 input_sync(dev);
1069 input_report_key(dev, BTN_BACK, hw.down);
1070 input_sync(dev);
1071 priv->scroll -= 4;
1072 }
1073 while (priv->scroll <= -4) {
1074 input_report_key(dev, BTN_FORWARD, !hw.up);
1075 input_sync(dev);
1076 input_report_key(dev, BTN_FORWARD, hw.up);
1077 input_sync(dev);
1078 priv->scroll += 4;
1079 }
1080 return;
1081 }
1082
Henrik Rydberg4f56ce92010-12-18 15:42:30 +01001083 if (hw.z > 0 && hw.x > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 num_fingers = 1;
1085 finger_width = 5;
Dmitry Torokhov6c536942017-03-05 15:51:33 -08001086 if (SYN_CAP_EXTENDED(info->capabilities)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 switch (hw.w) {
1088 case 0 ... 1:
Anthony Martin3f9db522017-08-28 10:26:12 -07001089 if (synaptics_has_multifinger(priv))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 num_fingers = hw.w + 2;
1091 break;
1092 case 2:
Dmitry Torokhov6c536942017-03-05 15:51:33 -08001093 if (SYN_MODEL_PEN(info->model_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 ; /* Nothing, treat a pen as a single finger */
1095 break;
1096 case 4 ... 15:
Dmitry Torokhov6c536942017-03-05 15:51:33 -08001097 if (SYN_CAP_PALMDETECT(info->capabilities))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 finger_width = hw.w;
1099 break;
1100 }
1101 }
1102 } else {
1103 num_fingers = 0;
1104 finger_width = 0;
1105 }
1106
Henrik Rydberge08d9af2014-07-14 10:26:56 -07001107 if (cr48_profile_sensor) {
Benjamin Tissoiresaa104b12014-12-29 14:17:44 -08001108 synaptics_report_mt_data(psmouse, &hw, num_fingers);
Henrik Rydberge08d9af2014-07-14 10:26:56 -07001109 return;
1110 }
1111
Dmitry Torokhov6c536942017-03-05 15:51:33 -08001112 if (SYN_CAP_ADV_GESTURE(info->ext_cap_0c))
Daniel Kurtz7afdb842011-08-23 23:00:33 -07001113 synaptics_report_semi_mt_data(dev, &hw, &priv->agm,
1114 num_fingers);
Henrik Rydbergfec6e522010-12-21 18:11:25 +01001115
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 /* Post events
1117 * BTN_TOUCH has to be first as mousedev relies on it when doing
1118 * absolute -> relative conversion
1119 */
1120 if (hw.z > 30) input_report_key(dev, BTN_TOUCH, 1);
1121 if (hw.z < 25) input_report_key(dev, BTN_TOUCH, 0);
1122
Henrik Rydberg4f56ce92010-12-18 15:42:30 +01001123 if (num_fingers > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 input_report_abs(dev, ABS_X, hw.x);
Daniel Kurtz6de58dd2011-08-23 23:00:24 -07001125 input_report_abs(dev, ABS_Y, synaptics_invert_y(hw.y));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 }
1127 input_report_abs(dev, ABS_PRESSURE, hw.z);
1128
Dmitry Torokhov6c536942017-03-05 15:51:33 -08001129 if (SYN_CAP_PALMDETECT(info->capabilities))
Chris Bagwell2a8e7712010-07-19 09:06:15 -07001130 input_report_abs(dev, ABS_TOOL_WIDTH, finger_width);
1131
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 input_report_key(dev, BTN_TOOL_FINGER, num_fingers == 1);
Anthony Martin3f9db522017-08-28 10:26:12 -07001133 if (synaptics_has_multifinger(priv)) {
Peter Hutterere42b6642008-11-20 15:24:42 -05001134 input_report_key(dev, BTN_TOOL_DOUBLETAP, num_fingers == 2);
1135 input_report_key(dev, BTN_TOOL_TRIPLETAP, num_fingers == 3);
1136 }
1137
Daniel Kurtz3cdfee92011-08-23 23:02:25 -07001138 synaptics_report_buttons(psmouse, &hw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
1140 input_sync(dev);
1141}
1142
Dmitry Torokhovf6c44422017-03-24 11:20:38 -07001143static bool synaptics_validate_byte(struct psmouse *psmouse,
1144 int idx, enum synaptics_pkt_type pkt_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
Dmitry Torokhovf6c44422017-03-24 11:20:38 -07001146 static const u8 newabs_mask[] = { 0xC8, 0x00, 0x00, 0xC8, 0x00 };
1147 static const u8 newabs_rel_mask[] = { 0xC0, 0x00, 0x00, 0xC0, 0x00 };
1148 static const u8 newabs_rslt[] = { 0x80, 0x00, 0x00, 0xC0, 0x00 };
1149 static const u8 oldabs_mask[] = { 0xC0, 0x60, 0x00, 0xC0, 0x60 };
1150 static const u8 oldabs_rslt[] = { 0xC0, 0x00, 0x00, 0x80, 0x00 };
1151 const u8 *packet = psmouse->packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
1153 if (idx < 0 || idx > 4)
Dmitry Torokhovf6c44422017-03-24 11:20:38 -07001154 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
1156 switch (pkt_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
Dmitry Torokhova62f0d22010-05-19 10:39:17 -07001158 case SYN_NEWABS:
1159 case SYN_NEWABS_RELAXED:
1160 return (packet[idx] & newabs_rel_mask[idx]) == newabs_rslt[idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
Dmitry Torokhova62f0d22010-05-19 10:39:17 -07001162 case SYN_NEWABS_STRICT:
1163 return (packet[idx] & newabs_mask[idx]) == newabs_rslt[idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Dmitry Torokhova62f0d22010-05-19 10:39:17 -07001165 case SYN_OLDABS:
1166 return (packet[idx] & oldabs_mask[idx]) == oldabs_rslt[idx];
1167
1168 default:
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001169 psmouse_err(psmouse, "unknown packet type %d\n", pkt_type);
Dmitry Torokhovf6c44422017-03-24 11:20:38 -07001170 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 }
1172}
1173
Dmitry Torokhovf6c44422017-03-24 11:20:38 -07001174static enum synaptics_pkt_type
1175synaptics_detect_pkt_type(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176{
1177 int i;
1178
Dmitry Torokhovf6c44422017-03-24 11:20:38 -07001179 for (i = 0; i < 5; i++) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001180 if (!synaptics_validate_byte(psmouse, i, SYN_NEWABS_STRICT)) {
1181 psmouse_info(psmouse, "using relaxed packet validation\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 return SYN_NEWABS_RELAXED;
1183 }
Dmitry Torokhovf6c44422017-03-24 11:20:38 -07001184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
1186 return SYN_NEWABS_STRICT;
1187}
1188
David Howells7d12e782006-10-05 14:55:46 +01001189static psmouse_ret_t synaptics_process_byte(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 struct synaptics_data *priv = psmouse->private;
1192
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 if (psmouse->pktcnt >= 6) { /* Full packet received */
1194 if (unlikely(priv->pkt_type == SYN_NEWABS))
1195 priv->pkt_type = synaptics_detect_pkt_type(psmouse);
1196
Dmitry Torokhov6c536942017-03-05 15:51:33 -08001197 if (SYN_CAP_PASS_THROUGH(priv->info.capabilities) &&
Dmitry Torokhova8b3c0f2010-10-04 21:46:10 -07001198 synaptics_is_pt_packet(psmouse->packet)) {
1199 if (priv->pt_port)
Dmitry Torokhovbf23cfc2017-02-09 11:34:08 -08001200 synaptics_pass_pt_packet(priv->pt_port,
Benjamin Tissoirescdd9dc12015-03-08 22:35:41 -07001201 psmouse->packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 } else
1203 synaptics_process_packet(psmouse);
1204
1205 return PSMOUSE_FULL_PACKET;
1206 }
1207
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001208 return synaptics_validate_byte(psmouse, psmouse->pktcnt - 1, priv->pkt_type) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 PSMOUSE_GOOD_DATA : PSMOUSE_BAD_DATA;
1210}
1211
1212/*****************************************************************************
1213 * Driver initialization/cleanup functions
1214 ****************************************************************************/
Daniel Kurtz85615472011-08-23 23:00:41 -07001215static void set_abs_position_params(struct input_dev *dev,
Dmitry Torokhov6c536942017-03-05 15:51:33 -08001216 struct synaptics_device_info *info,
1217 int x_code, int y_code)
Daniel Kurtz85615472011-08-23 23:00:41 -07001218{
Dmitry Torokhov6c536942017-03-05 15:51:33 -08001219 int x_min = info->x_min ?: XMIN_NOMINAL;
1220 int x_max = info->x_max ?: XMAX_NOMINAL;
1221 int y_min = info->y_min ?: YMIN_NOMINAL;
1222 int y_max = info->y_max ?: YMAX_NOMINAL;
1223 int fuzz = SYN_CAP_REDUCED_FILTERING(info->ext_cap_0c) ?
Daniel Kurtz85615472011-08-23 23:00:41 -07001224 SYN_REDUCED_FILTER_FUZZ : 0;
1225
1226 input_set_abs_params(dev, x_code, x_min, x_max, fuzz, 0);
1227 input_set_abs_params(dev, y_code, y_min, y_max, fuzz, 0);
Dmitry Torokhov6c536942017-03-05 15:51:33 -08001228 input_abs_set_res(dev, x_code, info->x_res);
1229 input_abs_set_res(dev, y_code, info->y_res);
Daniel Kurtz85615472011-08-23 23:00:41 -07001230}
1231
Dmitry Torokhovcdc24662017-04-03 16:54:04 -07001232static int set_input_params(struct psmouse *psmouse,
1233 struct synaptics_data *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234{
Hans de Goede43e19882014-04-19 22:26:41 -07001235 struct input_dev *dev = psmouse->dev;
Dmitry Torokhov6c536942017-03-05 15:51:33 -08001236 struct synaptics_device_info *info = &priv->info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 int i;
Dmitry Torokhovcdc24662017-04-03 16:54:04 -07001238 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
Dmitry Torokhov29aa6192017-04-03 16:37:24 -07001240 /* Reset default psmouse capabilities */
1241 __clear_bit(EV_REL, dev->evbit);
1242 bitmap_zero(dev->relbit, REL_CNT);
1243 bitmap_zero(dev->keybit, KEY_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
Daniel Drake7968a5d2011-11-08 00:00:35 -08001245 /* Things that apply to both modes */
Henrik Rydbergc14890a2010-12-16 09:52:23 +01001246 __set_bit(INPUT_PROP_POINTER, dev->propbit);
1247
Dmitry Torokhov29aa6192017-04-03 16:37:24 -07001248 input_set_capability(dev, EV_KEY, BTN_LEFT);
1249
1250 /* Clickpads report only left button */
1251 if (!SYN_CAP_CLICKPAD(info->ext_cap_0c)) {
1252 input_set_capability(dev, EV_KEY, BTN_RIGHT);
1253 if (SYN_CAP_MIDDLE_BUTTON(info->capabilities))
1254 input_set_capability(dev, EV_KEY, BTN_MIDDLE);
1255 }
Daniel Drake7968a5d2011-11-08 00:00:35 -08001256
1257 if (!priv->absolute_mode) {
1258 /* Relative mode */
Dmitry Torokhov29aa6192017-04-03 16:37:24 -07001259 input_set_capability(dev, EV_REL, REL_X);
1260 input_set_capability(dev, EV_REL, REL_Y);
Dmitry Torokhovcdc24662017-04-03 16:54:04 -07001261 return 0;
Daniel Drake7968a5d2011-11-08 00:00:35 -08001262 }
1263
1264 /* Absolute mode */
Dmitry Torokhov6c536942017-03-05 15:51:33 -08001265 set_abs_position_params(dev, &priv->info, ABS_X, ABS_Y);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 input_set_abs_params(dev, ABS_PRESSURE, 0, 255, 0, 0);
Chris Bagwell2a8e7712010-07-19 09:06:15 -07001267
Henrik Rydberge08d9af2014-07-14 10:26:56 -07001268 if (cr48_profile_sensor)
1269 input_set_abs_params(dev, ABS_MT_PRESSURE, 0, 255, 0, 0);
1270
Dmitry Torokhov6c536942017-03-05 15:51:33 -08001271 if (SYN_CAP_IMAGE_SENSOR(info->ext_cap_0c)) {
1272 set_abs_position_params(dev, info,
1273 ABS_MT_POSITION_X, ABS_MT_POSITION_Y);
Daniel Kurtz3cdfee92011-08-23 23:02:25 -07001274 /* Image sensors can report per-contact pressure */
1275 input_set_abs_params(dev, ABS_MT_PRESSURE, 0, 255, 0, 0);
Dmitry Torokhovcdc24662017-04-03 16:54:04 -07001276
1277 error = input_mt_init_slots(dev, 2,
1278 INPUT_MT_POINTER | INPUT_MT_TRACK);
1279 if (error)
1280 return error;
Daniel Kurtz6b4b49f2011-08-23 23:02:56 -07001281
1282 /* Image sensors can signal 4 and 5 finger clicks */
Dmitry Torokhov29aa6192017-04-03 16:37:24 -07001283 input_set_capability(dev, EV_KEY, BTN_TOOL_QUADTAP);
1284 input_set_capability(dev, EV_KEY, BTN_TOOL_QUINTTAP);
Dmitry Torokhov6c536942017-03-05 15:51:33 -08001285 } else if (SYN_CAP_ADV_GESTURE(info->ext_cap_0c)) {
1286 set_abs_position_params(dev, info,
1287 ABS_MT_POSITION_X, ABS_MT_POSITION_Y);
Henrik Rydberge08d9af2014-07-14 10:26:56 -07001288 /*
1289 * Profile sensor in CR-48 tracks contacts reasonably well,
1290 * other non-image sensors with AGM use semi-mt.
1291 */
Dmitry Torokhovcdc24662017-04-03 16:54:04 -07001292 error = input_mt_init_slots(dev, 2,
1293 INPUT_MT_POINTER |
1294 (cr48_profile_sensor ?
1295 INPUT_MT_TRACK :
1296 INPUT_MT_SEMI_MT));
1297 if (error)
1298 return error;
Peter Hutterer19eb4ed2018-01-16 15:20:58 -08001299
1300 /*
1301 * For semi-mt devices we send ABS_X/Y ourselves instead of
1302 * input_mt_report_pointer_emulation. But
1303 * input_mt_init_slots() resets the fuzz to 0, leading to a
1304 * filtered ABS_MT_POSITION_X but an unfiltered ABS_X
1305 * position. Let's re-initialize ABS_X/Y here.
1306 */
1307 if (!cr48_profile_sensor)
1308 set_abs_position_params(dev, &priv->info, ABS_X, ABS_Y);
Henrik Rydbergfec6e522010-12-21 18:11:25 +01001309 }
1310
Dmitry Torokhov6c536942017-03-05 15:51:33 -08001311 if (SYN_CAP_PALMDETECT(info->capabilities))
Chris Bagwell58fb0212010-07-19 09:06:15 -07001312 input_set_abs_params(dev, ABS_TOOL_WIDTH, 0, 15, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
Dmitry Torokhov29aa6192017-04-03 16:37:24 -07001314 input_set_capability(dev, EV_KEY, BTN_TOUCH);
1315 input_set_capability(dev, EV_KEY, BTN_TOOL_FINGER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
Anthony Martin3f9db522017-08-28 10:26:12 -07001317 if (synaptics_has_multifinger(priv)) {
Dmitry Torokhov29aa6192017-04-03 16:37:24 -07001318 input_set_capability(dev, EV_KEY, BTN_TOOL_DOUBLETAP);
1319 input_set_capability(dev, EV_KEY, BTN_TOOL_TRIPLETAP);
Peter Hutterere42b6642008-11-20 15:24:42 -05001320 }
1321
Dmitry Torokhov6c536942017-03-05 15:51:33 -08001322 if (SYN_CAP_FOUR_BUTTON(info->capabilities) ||
1323 SYN_CAP_MIDDLE_BUTTON(info->capabilities)) {
Dmitry Torokhov29aa6192017-04-03 16:37:24 -07001324 input_set_capability(dev, EV_KEY, BTN_FORWARD);
1325 input_set_capability(dev, EV_KEY, BTN_BACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 }
1327
Dmitry Torokhov6c536942017-03-05 15:51:33 -08001328 if (!SYN_CAP_EXT_BUTTONS_STICK(info->ext_cap_10))
1329 for (i = 0; i < SYN_CAP_MULTI_BUTTON_NO(info->ext_cap); i++)
Dmitry Torokhov29aa6192017-04-03 16:37:24 -07001330 input_set_capability(dev, EV_KEY, BTN_0 + i);
Tero Saarniec20a022009-06-10 23:27:24 -07001331
Dmitry Torokhov6c536942017-03-05 15:51:33 -08001332 if (SYN_CAP_CLICKPAD(info->ext_cap_0c)) {
Henrik Rydbergc14890a2010-12-16 09:52:23 +01001333 __set_bit(INPUT_PROP_BUTTONPAD, dev->propbit);
Benjamin Tissoires3adde1f2015-03-08 22:34:50 -07001334 if (psmouse_matches_pnp_id(psmouse, topbuttonpad_pnp_ids) &&
Dmitry Torokhov6c536942017-03-05 15:51:33 -08001335 !SYN_CAP_EXT_BUTTONS_STICK(info->ext_cap_10))
Hans de Goedee2f61102014-05-19 22:53:23 -07001336 __set_bit(INPUT_PROP_TOPBUTTONPAD, dev->propbit);
Takashi Iwai5f57d672010-04-19 10:37:21 -07001337 }
Dmitry Torokhovcdc24662017-04-03 16:54:04 -07001338
1339 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340}
1341
Daniel Drake7968a5d2011-11-08 00:00:35 -08001342static ssize_t synaptics_show_disable_gesture(struct psmouse *psmouse,
1343 void *data, char *buf)
1344{
1345 struct synaptics_data *priv = psmouse->private;
1346
1347 return sprintf(buf, "%c\n", priv->disable_gesture ? '1' : '0');
1348}
1349
1350static ssize_t synaptics_set_disable_gesture(struct psmouse *psmouse,
1351 void *data, const char *buf,
1352 size_t len)
1353{
1354 struct synaptics_data *priv = psmouse->private;
1355 unsigned int value;
1356 int err;
1357
1358 err = kstrtouint(buf, 10, &value);
1359 if (err)
1360 return err;
1361
1362 if (value > 1)
1363 return -EINVAL;
1364
1365 if (value == priv->disable_gesture)
1366 return len;
1367
1368 priv->disable_gesture = value;
1369 if (value)
1370 priv->mode |= SYN_BIT_DISABLE_GESTURE;
1371 else
1372 priv->mode &= ~SYN_BIT_DISABLE_GESTURE;
1373
1374 if (synaptics_mode_cmd(psmouse, priv->mode))
1375 return -EIO;
1376
1377 return len;
1378}
1379
1380PSMOUSE_DEFINE_ATTR(disable_gesture, S_IWUSR | S_IRUGO, NULL,
1381 synaptics_show_disable_gesture,
1382 synaptics_set_disable_gesture);
1383
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384static void synaptics_disconnect(struct psmouse *psmouse)
1385{
Daniel Drake7968a5d2011-11-08 00:00:35 -08001386 struct synaptics_data *priv = psmouse->private;
1387
Benjamin Tissoirese839ffa2017-03-02 14:13:53 -08001388 /*
1389 * We might have left a breadcrumb when trying to
1390 * set up SMbus companion.
1391 */
1392 psmouse_smbus_cleanup(psmouse);
1393
Dmitry Torokhov6c536942017-03-05 15:51:33 -08001394 if (!priv->absolute_mode &&
1395 SYN_ID_DISGEST_SUPPORTED(priv->info.identity))
Daniel Drake7968a5d2011-11-08 00:00:35 -08001396 device_remove_file(&psmouse->ps2dev.serio->dev,
1397 &psmouse_attr_disable_gesture.dattr);
1398
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 synaptics_reset(psmouse);
Daniel Drake7968a5d2011-11-08 00:00:35 -08001400 kfree(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 psmouse->private = NULL;
1402}
1403
1404static int synaptics_reconnect(struct psmouse *psmouse)
1405{
1406 struct synaptics_data *priv = psmouse->private;
Dmitry Torokhov6c536942017-03-05 15:51:33 -08001407 struct synaptics_device_info info;
Dmitry Torokhovf6c44422017-03-24 11:20:38 -07001408 u8 param[2];
Alexandre Peixoto Ferreirac63fe0a2011-01-28 22:05:14 -08001409 int retry = 0;
1410 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
Alexandre Peixoto Ferreirac63fe0a2011-01-28 22:05:14 -08001412 do {
1413 psmouse_reset(psmouse);
Dmitry Torokhov85214782011-12-12 00:05:53 -08001414 if (retry) {
1415 /*
1416 * On some boxes, right after resuming, the touchpad
1417 * needs some time to finish initializing (I assume
1418 * it needs time to calibrate) and start responding
1419 * to Synaptics-specific queries, so let's wait a
1420 * bit.
1421 */
1422 ssleep(1);
1423 }
Eric Miaoeeb06552013-06-04 09:30:55 -07001424 ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_GETID);
Alexandre Peixoto Ferreirac63fe0a2011-01-28 22:05:14 -08001425 error = synaptics_detect(psmouse, 0);
1426 } while (error && ++retry < 3);
Andy Whitcroft4d368452009-02-28 12:51:01 -08001427
Alexandre Peixoto Ferreirac63fe0a2011-01-28 22:05:14 -08001428 if (error)
Dmitry Torokhov212baf02017-03-23 18:38:14 -07001429 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
Alexandre Peixoto Ferreirac63fe0a2011-01-28 22:05:14 -08001431 if (retry > 1)
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001432 psmouse_dbg(psmouse, "reconnected after %d tries\n", retry);
Alexandre Peixoto Ferreirac63fe0a2011-01-28 22:05:14 -08001433
Dmitry Torokhov212baf02017-03-23 18:38:14 -07001434 error = synaptics_query_hardware(psmouse, &info);
1435 if (error) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001436 psmouse_err(psmouse, "Unable to query device.\n");
Dmitry Torokhov212baf02017-03-23 18:38:14 -07001437 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 }
1439
Dmitry Torokhov212baf02017-03-23 18:38:14 -07001440 error = synaptics_set_mode(psmouse);
1441 if (error) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001442 psmouse_err(psmouse, "Unable to initialize device.\n");
Dmitry Torokhov212baf02017-03-23 18:38:14 -07001443 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 }
1445
Dmitry Torokhov6c536942017-03-05 15:51:33 -08001446 if (info.identity != priv->info.identity ||
1447 info.model_id != priv->info.model_id ||
1448 info.capabilities != priv->info.capabilities ||
1449 info.ext_cap != priv->info.ext_cap) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001450 psmouse_err(psmouse,
Dmitry Torokhov6c536942017-03-05 15:51:33 -08001451 "hardware appears to be different: id(%u-%u), model(%u-%u), caps(%x-%x), ext(%x-%x).\n",
1452 priv->info.identity, info.identity,
1453 priv->info.model_id, info.model_id,
1454 priv->info.capabilities, info.capabilities,
1455 priv->info.ext_cap, info.ext_cap);
Dmitry Torokhov212baf02017-03-23 18:38:14 -07001456 return -ENXIO;
Alexandre Peixoto Ferreirabaddf582011-01-28 22:05:14 -08001457 }
1458
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 return 0;
1460}
1461
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001462static bool impaired_toshiba_kbc;
1463
Sachin Kamatc9631562013-08-12 11:05:58 -07001464static const struct dmi_system_id toshiba_dmi_table[] __initconst = {
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001465#if defined(CONFIG_DMI) && defined(CONFIG_X86)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 {
Dmitry Torokhov9961e252009-12-04 10:24:20 -08001467 /* Toshiba Satellite */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 .matches = {
1469 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
Richard Thrippleton53a26702006-04-02 00:10:18 -05001470 DMI_MATCH(DMI_PRODUCT_NAME, "Satellite"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 },
1472 },
Simon Horman9ba5eaa2005-07-11 01:07:20 -05001473 {
Dmitry Torokhov9961e252009-12-04 10:24:20 -08001474 /* Toshiba Dynabook */
Simon Horman9ba5eaa2005-07-11 01:07:20 -05001475 .matches = {
1476 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
Richard Thrippleton53a26702006-04-02 00:10:18 -05001477 DMI_MATCH(DMI_PRODUCT_NAME, "dynabook"),
1478 },
1479 },
1480 {
Dmitry Torokhov9961e252009-12-04 10:24:20 -08001481 /* Toshiba Portege M300 */
Richard Thrippleton53a26702006-04-02 00:10:18 -05001482 .matches = {
1483 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1484 DMI_MATCH(DMI_PRODUCT_NAME, "PORTEGE M300"),
Simon Horman9ba5eaa2005-07-11 01:07:20 -05001485 },
Dmitry Torokhov5f5eeff2009-10-12 21:35:00 -07001486
1487 },
1488 {
Dmitry Torokhov9961e252009-12-04 10:24:20 -08001489 /* Toshiba Portege M300 */
Dmitry Torokhov5f5eeff2009-10-12 21:35:00 -07001490 .matches = {
1491 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1492 DMI_MATCH(DMI_PRODUCT_NAME, "Portable PC"),
1493 DMI_MATCH(DMI_PRODUCT_VERSION, "Version 1.0"),
1494 },
1495
Simon Horman9ba5eaa2005-07-11 01:07:20 -05001496 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497#endif
Jan Beulich70874862011-03-31 00:01:58 -07001498 { }
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001499};
1500
Andres Salomonef8313b2010-12-23 01:19:38 -08001501static bool broken_olpc_ec;
1502
Sachin Kamatc9631562013-08-12 11:05:58 -07001503static const struct dmi_system_id olpc_dmi_table[] __initconst = {
Andres Salomonef8313b2010-12-23 01:19:38 -08001504#if defined(CONFIG_DMI) && defined(CONFIG_OLPC)
1505 {
1506 /* OLPC XO-1 or XO-1.5 */
1507 .matches = {
1508 DMI_MATCH(DMI_SYS_VENDOR, "OLPC"),
1509 DMI_MATCH(DMI_PRODUCT_NAME, "XO"),
1510 },
1511 },
Andres Salomonef8313b2010-12-23 01:19:38 -08001512#endif
Jan Beulich70874862011-03-31 00:01:58 -07001513 { }
Andres Salomonef8313b2010-12-23 01:19:38 -08001514};
1515
Henrik Rydberge08d9af2014-07-14 10:26:56 -07001516static const struct dmi_system_id __initconst cr48_dmi_table[] = {
1517#if defined(CONFIG_DMI) && defined(CONFIG_X86)
1518 {
1519 /* Cr-48 Chromebook (Codename Mario) */
1520 .matches = {
1521 DMI_MATCH(DMI_SYS_VENDOR, "IEC"),
1522 DMI_MATCH(DMI_PRODUCT_NAME, "Mario"),
1523 },
1524 },
1525#endif
1526 { }
1527};
1528
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001529void __init synaptics_module_init(void)
1530{
1531 impaired_toshiba_kbc = dmi_check_system(toshiba_dmi_table);
Andres Salomonef8313b2010-12-23 01:19:38 -08001532 broken_olpc_ec = dmi_check_system(olpc_dmi_table);
Henrik Rydberge08d9af2014-07-14 10:26:56 -07001533 cr48_profile_sensor = dmi_check_system(cr48_dmi_table);
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001534}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
Benjamin Tissoirese839ffa2017-03-02 14:13:53 -08001536static int synaptics_init_ps2(struct psmouse *psmouse,
1537 struct synaptics_device_info *info,
1538 bool absolute_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539{
1540 struct synaptics_data *priv;
Benjamin Tissoirese839ffa2017-03-02 14:13:53 -08001541 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
Benjamin Tissoirese839ffa2017-03-02 14:13:53 -08001543 synaptics_apply_quirks(psmouse, info);
Andres Salomonef8313b2010-12-23 01:19:38 -08001544
Eric Sesterhennb39787a2006-03-14 00:09:16 -05001545 psmouse->private = priv = kzalloc(sizeof(struct synaptics_data), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 if (!priv)
Davidlohr Bueso6792cbb2010-09-29 18:53:35 -07001547 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
Benjamin Tissoirese839ffa2017-03-02 14:13:53 -08001549 priv->info = *info;
Daniel Drake7968a5d2011-11-08 00:00:35 -08001550 priv->absolute_mode = absolute_mode;
Dmitry Torokhov6c536942017-03-05 15:51:33 -08001551 if (SYN_ID_DISGEST_SUPPORTED(info->identity))
Daniel Drake7968a5d2011-11-08 00:00:35 -08001552 priv->disable_gesture = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
Dmitry Torokhovde4e3742014-12-29 14:43:44 -08001554 /*
1555 * Unfortunately ForcePad capability is not exported over PS/2,
1556 * so we have to resort to checking PNP IDs.
1557 */
1558 priv->is_forcepad = psmouse_matches_pnp_id(psmouse, forcepad_pnp_ids);
1559
Benjamin Tissoirese839ffa2017-03-02 14:13:53 -08001560 err = synaptics_set_mode(psmouse);
1561 if (err) {
Daniel Drake7968a5d2011-11-08 00:00:35 -08001562 psmouse_err(psmouse, "Unable to initialize device.\n");
Henrik Rydbergfec6e522010-12-21 18:11:25 +01001563 goto init_fail;
1564 }
1565
Dmitry Torokhov6c536942017-03-05 15:51:33 -08001566 priv->pkt_type = SYN_MODEL_NEWABS(info->model_id) ?
1567 SYN_NEWABS : SYN_OLDABS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001569 psmouse_info(psmouse,
Dmitry Torokhov991d29fe02017-03-23 14:56:06 -07001570 "Touchpad model: %lu, fw: %lu.%lu, id: %#x, caps: %#x/%#x/%#x/%#x, board id: %u, fw id: %u\n",
Dmitry Torokhov6c536942017-03-05 15:51:33 -08001571 SYN_ID_MODEL(info->identity),
1572 SYN_ID_MAJOR(info->identity), SYN_ID_MINOR(info->identity),
1573 info->model_id,
1574 info->capabilities, info->ext_cap, info->ext_cap_0c,
1575 info->ext_cap_10, info->board_id, info->firmware_id);
Dmitry Torokhov409b7502005-05-28 02:12:18 -05001576
Dmitry Torokhovcdc24662017-04-03 16:54:04 -07001577 err = set_input_params(psmouse, priv);
1578 if (err) {
1579 psmouse_err(psmouse,
1580 "failed to set up capabilities: %d\n", err);
1581 goto init_fail;
1582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
Dmitry Torokhov887cc122007-04-12 01:30:41 -04001584 /*
1585 * Encode touchpad model so that it can be used to set
1586 * input device->id.version and be visible to userspace.
1587 * Because version is __u16 we have to drop something.
1588 * Hardware info bits seem to be good candidates as they
1589 * are documented to be for Synaptics corp. internal use.
1590 */
Dmitry Torokhov6c536942017-03-05 15:51:33 -08001591 psmouse->model = ((info->model_id & 0x00ff0000) >> 8) |
1592 (info->model_id & 0x000000ff);
Dmitry Torokhov887cc122007-04-12 01:30:41 -04001593
Daniel Drake7968a5d2011-11-08 00:00:35 -08001594 if (absolute_mode) {
1595 psmouse->protocol_handler = synaptics_process_byte;
1596 psmouse->pktsize = 6;
1597 } else {
1598 /* Relative mode follows standard PS/2 mouse protocol */
1599 psmouse->protocol_handler = psmouse_process_byte;
1600 psmouse->pktsize = 3;
1601 }
1602
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 psmouse->set_rate = synaptics_set_rate;
1604 psmouse->disconnect = synaptics_disconnect;
1605 psmouse->reconnect = synaptics_reconnect;
Dmitry Torokhova1cec062007-02-18 01:40:24 -05001606 psmouse->cleanup = synaptics_reset;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001607 /* Synaptics can usually stay in sync without extra help */
1608 psmouse->resync_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
Dmitry Torokhov6c536942017-03-05 15:51:33 -08001610 if (SYN_CAP_PASS_THROUGH(info->capabilities))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 synaptics_pt_create(psmouse);
1612
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 /*
1614 * Toshiba's KBC seems to have trouble handling data from
Andres Salomon7ee99162010-12-23 01:18:28 -08001615 * Synaptics at full rate. Switch to a lower rate (roughly
1616 * the same rate as a standard PS/2 mouse).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 */
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001618 if (psmouse->rate >= 80 && impaired_toshiba_kbc) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001619 psmouse_info(psmouse,
1620 "Toshiba %s detected, limiting rate to 40pps.\n",
1621 dmi_get_system_info(DMI_PRODUCT_NAME));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 psmouse->rate = 40;
1623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
Dmitry Torokhov6c536942017-03-05 15:51:33 -08001625 if (!priv->absolute_mode && SYN_ID_DISGEST_SUPPORTED(info->identity)) {
Daniel Drake7968a5d2011-11-08 00:00:35 -08001626 err = device_create_file(&psmouse->ps2dev.serio->dev,
1627 &psmouse_attr_disable_gesture.dattr);
1628 if (err) {
1629 psmouse_err(psmouse,
1630 "Failed to create disable_gesture attribute (%d)",
1631 err);
1632 goto init_fail;
1633 }
1634 }
1635
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 return 0;
1637
1638 init_fail:
1639 kfree(priv);
Daniel Drake7968a5d2011-11-08 00:00:35 -08001640 return err;
1641}
1642
Benjamin Tissoirese839ffa2017-03-02 14:13:53 -08001643static int __synaptics_init(struct psmouse *psmouse, bool absolute_mode)
1644{
1645 struct synaptics_device_info info;
1646 int error;
1647
1648 psmouse_reset(psmouse);
1649
1650 error = synaptics_query_hardware(psmouse, &info);
1651 if (error) {
1652 psmouse_err(psmouse, "Unable to query device: %d\n", error);
1653 return error;
1654 }
1655
1656 return synaptics_init_ps2(psmouse, &info, absolute_mode);
1657}
1658
1659int synaptics_init_absolute(struct psmouse *psmouse)
Daniel Drake7968a5d2011-11-08 00:00:35 -08001660{
1661 return __synaptics_init(psmouse, true);
1662}
1663
1664int synaptics_init_relative(struct psmouse *psmouse)
1665{
1666 return __synaptics_init(psmouse, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667}
1668
Benjamin Tissoirese839ffa2017-03-02 14:13:53 -08001669static int synaptics_setup_ps2(struct psmouse *psmouse,
1670 struct synaptics_device_info *info)
1671{
1672 bool absolute_mode = true;
1673 int error;
1674
1675 /*
1676 * The OLPC XO has issues with Synaptics' absolute mode; the constant
1677 * packet spew overloads the EC such that key presses on the keyboard
1678 * are missed. Given that, don't even attempt to use Absolute mode.
1679 * Relative mode seems to work just fine.
1680 */
1681 if (broken_olpc_ec) {
1682 psmouse_info(psmouse,
1683 "OLPC XO detected, forcing relative protocol.\n");
1684 absolute_mode = false;
1685 }
1686
1687 error = synaptics_init_ps2(psmouse, info, absolute_mode);
1688 if (error)
1689 return error;
1690
1691 return absolute_mode ? PSMOUSE_SYNAPTICS : PSMOUSE_SYNAPTICS_RELATIVE;
1692}
1693
Andres Salomon55e3d922007-03-10 01:39:54 -05001694#else /* CONFIG_MOUSE_PS2_SYNAPTICS */
1695
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001696void __init synaptics_module_init(void)
1697{
1698}
1699
Benjamin Tissoirese839ffa2017-03-02 14:13:53 -08001700static int __maybe_unused
1701synaptics_setup_ps2(struct psmouse *psmouse,
1702 struct synaptics_device_info *info)
Andres Salomon55e3d922007-03-10 01:39:54 -05001703{
1704 return -ENOSYS;
1705}
1706
1707#endif /* CONFIG_MOUSE_PS2_SYNAPTICS */
Benjamin Tissoirese839ffa2017-03-02 14:13:53 -08001708
1709#ifdef CONFIG_MOUSE_PS2_SYNAPTICS_SMBUS
1710
1711/*
1712 * The newest Synaptics device can use a secondary bus (called InterTouch) which
1713 * provides a better bandwidth and allow a better control of the touchpads.
1714 * This is used to decide if we need to use this bus or not.
1715 */
1716enum {
1717 SYNAPTICS_INTERTOUCH_NOT_SET = -1,
1718 SYNAPTICS_INTERTOUCH_OFF,
1719 SYNAPTICS_INTERTOUCH_ON,
1720};
1721
Benjamin Tissoiresf4947d72017-05-23 14:15:24 -07001722static int synaptics_intertouch = IS_ENABLED(CONFIG_RMI4_SMB) ?
1723 SYNAPTICS_INTERTOUCH_NOT_SET : SYNAPTICS_INTERTOUCH_OFF;
Benjamin Tissoirese839ffa2017-03-02 14:13:53 -08001724module_param_named(synaptics_intertouch, synaptics_intertouch, int, 0644);
1725MODULE_PARM_DESC(synaptics_intertouch, "Use a secondary bus for the Synaptics device.");
1726
1727static int synaptics_create_intertouch(struct psmouse *psmouse,
1728 struct synaptics_device_info *info,
1729 bool leave_breadcrumbs)
1730{
1731 bool topbuttonpad =
1732 psmouse_matches_pnp_id(psmouse, topbuttonpad_pnp_ids) &&
1733 !SYN_CAP_EXT_BUTTONS_STICK(info->ext_cap_10);
1734 const struct rmi_device_platform_data pdata = {
1735 .sensor_pdata = {
1736 .sensor_type = rmi_sensor_touchpad,
1737 .axis_align.flip_y = true,
Andrew Duggan2b302972017-10-09 20:51:05 -07001738 .kernel_tracking = false,
Benjamin Tissoirese839ffa2017-03-02 14:13:53 -08001739 .topbuttonpad = topbuttonpad,
1740 },
1741 .f30_data = {
1742 .buttonpad = SYN_CAP_CLICKPAD(info->ext_cap_0c),
1743 .trackstick_buttons =
1744 !!SYN_CAP_EXT_BUTTONS_STICK(info->ext_cap_10),
1745 },
1746 };
1747 const struct i2c_board_info intertouch_board = {
1748 I2C_BOARD_INFO("rmi4_smbus", 0x2c),
1749 .flags = I2C_CLIENT_HOST_NOTIFY,
1750 };
1751
1752 return psmouse_smbus_init(psmouse, &intertouch_board,
1753 &pdata, sizeof(pdata),
1754 leave_breadcrumbs);
1755}
1756
1757/**
1758 * synaptics_setup_intertouch - called once the PS/2 devices are enumerated
1759 * and decides to instantiate a SMBus InterTouch device.
1760 */
1761static int synaptics_setup_intertouch(struct psmouse *psmouse,
1762 struct synaptics_device_info *info,
1763 bool leave_breadcrumbs)
1764{
1765 int error;
1766
1767 if (synaptics_intertouch == SYNAPTICS_INTERTOUCH_OFF)
1768 return -ENXIO;
1769
1770 if (synaptics_intertouch == SYNAPTICS_INTERTOUCH_NOT_SET) {
1771 if (!psmouse_matches_pnp_id(psmouse, topbuttonpad_pnp_ids) &&
Benjamin Tissoires2fef8262017-05-26 16:51:19 -07001772 !psmouse_matches_pnp_id(psmouse, smbus_pnp_ids)) {
1773
1774 if (!psmouse_matches_pnp_id(psmouse, forcepad_pnp_ids))
1775 psmouse_info(psmouse,
1776 "Your touchpad (%s) says it can support a different bus. "
1777 "If i2c-hid and hid-rmi are not used, you might want to try setting psmouse.synaptics_intertouch to 1 and report this to linux-input@vger.kernel.org.\n",
1778 psmouse->ps2dev.serio->firmware_id);
1779
Benjamin Tissoirese839ffa2017-03-02 14:13:53 -08001780 return -ENXIO;
Benjamin Tissoires2fef8262017-05-26 16:51:19 -07001781 }
Benjamin Tissoirese839ffa2017-03-02 14:13:53 -08001782 }
1783
1784 psmouse_info(psmouse, "Trying to set up SMBus access\n");
1785
1786 error = synaptics_create_intertouch(psmouse, info, leave_breadcrumbs);
1787 if (error) {
1788 if (error == -EAGAIN)
1789 psmouse_info(psmouse, "SMbus companion is not ready yet\n");
1790 else
1791 psmouse_err(psmouse, "unable to create intertouch device\n");
1792
1793 return error;
1794 }
1795
1796 return 0;
1797}
1798
1799int synaptics_init_smbus(struct psmouse *psmouse)
1800{
1801 struct synaptics_device_info info;
1802 int error;
1803
1804 psmouse_reset(psmouse);
1805
1806 error = synaptics_query_hardware(psmouse, &info);
1807 if (error) {
1808 psmouse_err(psmouse, "Unable to query device: %d\n", error);
1809 return error;
1810 }
1811
1812 if (!SYN_CAP_INTERTOUCH(info.ext_cap_0c))
1813 return -ENXIO;
1814
1815 return synaptics_create_intertouch(psmouse, &info, false);
1816}
1817
1818#else /* CONFIG_MOUSE_PS2_SYNAPTICS_SMBUS */
1819
1820static int __maybe_unused
1821synaptics_setup_intertouch(struct psmouse *psmouse,
1822 struct synaptics_device_info *info,
1823 bool leave_breadcrumbs)
1824{
1825 return -ENOSYS;
1826}
1827
1828int synaptics_init_smbus(struct psmouse *psmouse)
1829{
1830 return -ENOSYS;
1831}
1832
1833#endif /* CONFIG_MOUSE_PS2_SYNAPTICS_SMBUS */
1834
1835#if defined(CONFIG_MOUSE_PS2_SYNAPTICS) || \
1836 defined(CONFIG_MOUSE_PS2_SYNAPTICS_SMBUS)
1837
1838int synaptics_init(struct psmouse *psmouse)
1839{
1840 struct synaptics_device_info info;
1841 int error;
1842 int retval;
1843
1844 psmouse_reset(psmouse);
1845
1846 error = synaptics_query_hardware(psmouse, &info);
1847 if (error) {
1848 psmouse_err(psmouse, "Unable to query device: %d\n", error);
1849 return error;
1850 }
1851
1852 if (SYN_CAP_INTERTOUCH(info.ext_cap_0c)) {
Benjamin Tissoiresf4101ff2017-05-26 16:21:36 -07001853 if ((!IS_ENABLED(CONFIG_RMI4_SMB) ||
1854 !IS_ENABLED(CONFIG_MOUSE_PS2_SYNAPTICS_SMBUS)) &&
1855 /* Forcepads need F21, which is not ready */
1856 !psmouse_matches_pnp_id(psmouse, forcepad_pnp_ids)) {
1857 psmouse_warn(psmouse,
1858 "The touchpad can support a better bus than the too old PS/2 protocol. "
1859 "Make sure MOUSE_PS2_SYNAPTICS_SMBUS and RMI4_SMB are enabled to get a better touchpad experience.\n");
1860 }
1861
Benjamin Tissoirese839ffa2017-03-02 14:13:53 -08001862 error = synaptics_setup_intertouch(psmouse, &info, true);
1863 if (!error)
1864 return PSMOUSE_SYNAPTICS_SMBUS;
1865 }
1866
1867 retval = synaptics_setup_ps2(psmouse, &info);
1868 if (retval < 0) {
1869 /*
1870 * Not using any flavor of Synaptics support, so clean up
1871 * SMbus breadcrumbs, if any.
1872 */
1873 psmouse_smbus_cleanup(psmouse);
1874 }
1875
1876 return retval;
1877}
1878
1879#else /* CONFIG_MOUSE_PS2_SYNAPTICS || CONFIG_MOUSE_PS2_SYNAPTICS_SMBUS */
1880
1881int synaptics_init(struct psmouse *psmouse)
1882{
1883 return -ENOSYS;
1884}
1885
1886#endif /* CONFIG_MOUSE_PS2_SYNAPTICS || CONFIG_MOUSE_PS2_SYNAPTICS_SMBUS */