blob: 32b1363f7ace54a38a56d181df2a48d1fec27011 [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 Forsheeba37c702012-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 Forsheeba37c702012-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 Forsheead4751d2012-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 Forsheeba37c702012-07-24 23:54:11 -070066 */
Seth Forsheead4751d2012-09-28 10:29:21 -070067#define X_MAX_POSITIVE 8176
68#define Y_MAX_POSITIVE 8176
Seth Forsheeba37c702012-07-24 23:54:11 -070069
Daniel Kurtz6de58dd2011-08-23 23:00:24 -070070/*
71 * Synaptics touchpads report the y coordinate from bottom to top, which is
72 * opposite from what userspace expects.
73 * This function is used to invert y before reporting.
74 */
75static int synaptics_invert_y(int y)
76{
77 return YMAX_NOMINAL + YMIN_NOMINAL - y;
78}
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Andres Salomon55e3d922007-03-10 01:39:54 -050081/*****************************************************************************
82 * Stuff we need even when we do not want native Synaptics support
83 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85/*
86 * Set the synaptics touchpad mode byte by special commands
87 */
88static int synaptics_mode_cmd(struct psmouse *psmouse, unsigned char mode)
89{
90 unsigned char param[1];
91
92 if (psmouse_sliced_command(psmouse, mode))
93 return -1;
94 param[0] = SYN_PS_SET_MODE2;
95 if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_SETRATE))
96 return -1;
97 return 0;
98}
99
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700100int synaptics_detect(struct psmouse *psmouse, bool set_properties)
Andres Salomon55e3d922007-03-10 01:39:54 -0500101{
102 struct ps2dev *ps2dev = &psmouse->ps2dev;
103 unsigned char param[4];
104
105 param[0] = 0;
106
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_SETRES);
111 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO);
112
113 if (param[1] != 0x47)
114 return -ENODEV;
115
116 if (set_properties) {
117 psmouse->vendor = "Synaptics";
118 psmouse->name = "TouchPad";
119 }
120
121 return 0;
122}
123
124void synaptics_reset(struct psmouse *psmouse)
125{
126 /* reset touchpad back to relative mode, gestures enabled */
127 synaptics_mode_cmd(psmouse, 0);
128}
129
130#ifdef CONFIG_MOUSE_PS2_SYNAPTICS
131
132/*****************************************************************************
133 * Synaptics communications functions
134 ****************************************************************************/
135
136/*
137 * Send a command to the synpatics touchpad by special commands
138 */
139static int synaptics_send_cmd(struct psmouse *psmouse, unsigned char c, unsigned char *param)
140{
141 if (psmouse_sliced_command(psmouse, c))
142 return -1;
143 if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_GETINFO))
144 return -1;
145 return 0;
146}
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148/*
149 * Read the model-id bytes from the touchpad
150 * see also SYN_MODEL_* macros
151 */
152static int synaptics_model_id(struct psmouse *psmouse)
153{
154 struct synaptics_data *priv = psmouse->private;
155 unsigned char mi[3];
156
157 if (synaptics_send_cmd(psmouse, SYN_QUE_MODEL, mi))
158 return -1;
159 priv->model_id = (mi[0]<<16) | (mi[1]<<8) | mi[2];
160 return 0;
161}
162
163/*
164 * Read the capability-bits from the touchpad
165 * see also the SYN_CAP_* macros
166 */
167static int synaptics_capability(struct psmouse *psmouse)
168{
169 struct synaptics_data *priv = psmouse->private;
170 unsigned char cap[3];
171
172 if (synaptics_send_cmd(psmouse, SYN_QUE_CAPABILITIES, cap))
173 return -1;
174 priv->capabilities = (cap[0] << 16) | (cap[1] << 8) | cap[2];
Takashi Iwai5f57d672010-04-19 10:37:21 -0700175 priv->ext_cap = priv->ext_cap_0c = 0;
176
Dmitry Torokhov3619b8f2010-07-21 00:01:19 -0700177 /*
178 * Older firmwares had submodel ID fixed to 0x47
179 */
180 if (SYN_ID_FULL(priv->identity) < 0x705 &&
181 SYN_CAP_SUBMODEL_ID(priv->capabilities) != 0x47) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 return -1;
Dmitry Torokhov3619b8f2010-07-21 00:01:19 -0700183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185 /*
186 * Unless capExtended is set the rest of the flags should be ignored
187 */
188 if (!SYN_CAP_EXTENDED(priv->capabilities))
189 priv->capabilities = 0;
190
191 if (SYN_EXT_CAP_REQUESTS(priv->capabilities) >= 1) {
192 if (synaptics_send_cmd(psmouse, SYN_QUE_EXT_CAPAB, cap)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700193 psmouse_warn(psmouse,
194 "device claims to have extended capabilities, but I'm not able to read them.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 } else {
196 priv->ext_cap = (cap[0] << 16) | (cap[1] << 8) | cap[2];
197
198 /*
199 * if nExtBtn is greater than 8 it should be considered
200 * invalid and treated as 0
201 */
202 if (SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap) > 8)
203 priv->ext_cap &= 0xff0fff;
204 }
205 }
Takashi Iwai5f57d672010-04-19 10:37:21 -0700206
207 if (SYN_EXT_CAP_REQUESTS(priv->capabilities) >= 4) {
208 if (synaptics_send_cmd(psmouse, SYN_QUE_EXT_CAPAB_0C, cap)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700209 psmouse_warn(psmouse,
210 "device claims to have extended capability 0x0c, but I'm not able to read it.\n");
Takashi Iwai5f57d672010-04-19 10:37:21 -0700211 } else {
212 priv->ext_cap_0c = (cap[0] << 16) | (cap[1] << 8) | cap[2];
213 }
214 }
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 return 0;
217}
218
219/*
220 * Identify Touchpad
221 * See also the SYN_ID_* macros
222 */
223static int synaptics_identify(struct psmouse *psmouse)
224{
225 struct synaptics_data *priv = psmouse->private;
226 unsigned char id[3];
227
228 if (synaptics_send_cmd(psmouse, SYN_QUE_IDENTIFY, id))
229 return -1;
230 priv->identity = (id[0]<<16) | (id[1]<<8) | id[2];
231 if (SYN_ID_IS_SYNAPTICS(priv->identity))
232 return 0;
233 return -1;
234}
235
Tero Saarniec20a022009-06-10 23:27:24 -0700236/*
Dmitry Torokhov83ba9ea2010-05-10 23:06:52 -0700237 * Read touchpad resolution and maximum reported coordinates
Tero Saarniec20a022009-06-10 23:27:24 -0700238 * Resolution is left zero if touchpad does not support the query
239 */
Benjamin Tissoires969ba042014-03-28 00:43:00 -0700240
241static const int *quirk_min_max;
242
Tero Saarniec20a022009-06-10 23:27:24 -0700243static int synaptics_resolution(struct psmouse *psmouse)
244{
245 struct synaptics_data *priv = psmouse->private;
Dmitry Torokhova66413f2011-07-09 12:32:56 -0700246 unsigned char resp[3];
Tero Saarniec20a022009-06-10 23:27:24 -0700247
Benjamin Tissoires969ba042014-03-28 00:43:00 -0700248 if (quirk_min_max) {
249 priv->x_min = quirk_min_max[0];
250 priv->x_max = quirk_min_max[1];
251 priv->y_min = quirk_min_max[2];
252 priv->y_max = quirk_min_max[3];
253 return 0;
254 }
255
Tero Saarniec20a022009-06-10 23:27:24 -0700256 if (SYN_ID_MAJOR(priv->identity) < 4)
Takashi Iwaibbddd192010-07-14 09:32:46 -0700257 return 0;
Tero Saarniec20a022009-06-10 23:27:24 -0700258
Dmitry Torokhova66413f2011-07-09 12:32:56 -0700259 if (synaptics_send_cmd(psmouse, SYN_QUE_RESOLUTION, resp) == 0) {
260 if (resp[0] != 0 && (resp[1] & 0x80) && resp[2] != 0) {
261 priv->x_res = resp[0]; /* x resolution in units/mm */
262 priv->y_res = resp[2]; /* y resolution in units/mm */
Dmitry Torokhov83ba9ea2010-05-10 23:06:52 -0700263 }
264 }
Tero Saarniec20a022009-06-10 23:27:24 -0700265
Dmitry Torokhov83ba9ea2010-05-10 23:06:52 -0700266 if (SYN_EXT_CAP_REQUESTS(priv->capabilities) >= 5 &&
267 SYN_CAP_MAX_DIMENSIONS(priv->ext_cap_0c)) {
Dmitry Torokhova66413f2011-07-09 12:32:56 -0700268 if (synaptics_send_cmd(psmouse, SYN_QUE_EXT_MAX_COORDS, resp)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700269 psmouse_warn(psmouse,
270 "device claims to have max coordinates query, but I'm not able to read it.\n");
Dmitry Torokhov83ba9ea2010-05-10 23:06:52 -0700271 } else {
Dmitry Torokhova66413f2011-07-09 12:32:56 -0700272 priv->x_max = (resp[0] << 5) | ((resp[1] & 0x0f) << 1);
273 priv->y_max = (resp[2] << 5) | ((resp[1] & 0xf0) >> 3);
274 }
275 }
276
277 if (SYN_EXT_CAP_REQUESTS(priv->capabilities) >= 7 &&
278 SYN_CAP_MIN_DIMENSIONS(priv->ext_cap_0c)) {
279 if (synaptics_send_cmd(psmouse, SYN_QUE_EXT_MIN_COORDS, resp)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700280 psmouse_warn(psmouse,
281 "device claims to have min coordinates query, but I'm not able to read it.\n");
Dmitry Torokhova66413f2011-07-09 12:32:56 -0700282 } else {
283 priv->x_min = (resp[0] << 5) | ((resp[1] & 0x0f) << 1);
284 priv->y_min = (resp[2] << 5) | ((resp[1] & 0xf0) >> 3);
Dmitry Torokhov83ba9ea2010-05-10 23:06:52 -0700285 }
Tero Saarniec20a022009-06-10 23:27:24 -0700286 }
287
288 return 0;
289}
290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291static int synaptics_query_hardware(struct psmouse *psmouse)
292{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 if (synaptics_identify(psmouse))
294 return -1;
295 if (synaptics_model_id(psmouse))
296 return -1;
297 if (synaptics_capability(psmouse))
298 return -1;
Tero Saarniec20a022009-06-10 23:27:24 -0700299 if (synaptics_resolution(psmouse))
300 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 return 0;
303}
304
Daniel Drake7968a5d2011-11-08 00:00:35 -0800305static int synaptics_set_advanced_gesture_mode(struct psmouse *psmouse)
306{
307 static unsigned char param = 0xc8;
308 struct synaptics_data *priv = psmouse->private;
309
Benjamin Herrenschmidt899c6122012-04-20 22:34:49 -0700310 if (!(SYN_CAP_ADV_GESTURE(priv->ext_cap_0c) ||
311 SYN_CAP_IMAGE_SENSOR(priv->ext_cap_0c)))
Daniel Drake7968a5d2011-11-08 00:00:35 -0800312 return 0;
313
314 if (psmouse_sliced_command(psmouse, SYN_QUE_MODEL))
315 return -1;
316
317 if (ps2_command(&psmouse->ps2dev, &param, PSMOUSE_CMD_SETRATE))
318 return -1;
319
320 /* Advanced gesture mode also sends multi finger data */
321 priv->capabilities |= BIT(1);
322
323 return 0;
324}
325
326static int synaptics_set_mode(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
328 struct synaptics_data *priv = psmouse->private;
329
Daniel Drake7968a5d2011-11-08 00:00:35 -0800330 priv->mode = 0;
331 if (priv->absolute_mode)
332 priv->mode |= SYN_BIT_ABSOLUTE_MODE;
333 if (priv->disable_gesture)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 priv->mode |= SYN_BIT_DISABLE_GESTURE;
Daniel Drake7968a5d2011-11-08 00:00:35 -0800335 if (psmouse->rate >= 80)
336 priv->mode |= SYN_BIT_HIGH_RATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 if (SYN_CAP_EXTENDED(priv->capabilities))
338 priv->mode |= SYN_BIT_W_MODE;
339
340 if (synaptics_mode_cmd(psmouse, priv->mode))
341 return -1;
342
Daniel Drake7968a5d2011-11-08 00:00:35 -0800343 if (priv->absolute_mode &&
344 synaptics_set_advanced_gesture_mode(psmouse)) {
345 psmouse_err(psmouse, "Advanced gesture mode init failed.\n");
346 return -1;
347 }
348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 return 0;
350}
351
352static void synaptics_set_rate(struct psmouse *psmouse, unsigned int rate)
353{
354 struct synaptics_data *priv = psmouse->private;
355
356 if (rate >= 80) {
357 priv->mode |= SYN_BIT_HIGH_RATE;
358 psmouse->rate = 80;
359 } else {
360 priv->mode &= ~SYN_BIT_HIGH_RATE;
361 psmouse->rate = 40;
362 }
363
364 synaptics_mode_cmd(psmouse, priv->mode);
365}
366
367/*****************************************************************************
368 * Synaptics pass-through PS/2 port support
369 ****************************************************************************/
370static int synaptics_pt_write(struct serio *serio, unsigned char c)
371{
372 struct psmouse *parent = serio_get_drvdata(serio->parent);
373 char rate_param = SYN_PS_CLIENT_CMD; /* indicates that we want pass-through port */
374
375 if (psmouse_sliced_command(parent, c))
376 return -1;
377 if (ps2_command(&parent->ps2dev, &rate_param, PSMOUSE_CMD_SETRATE))
378 return -1;
379 return 0;
380}
381
Dmitry Torokhova8b3c0f2010-10-04 21:46:10 -0700382static int synaptics_pt_start(struct serio *serio)
383{
384 struct psmouse *parent = serio_get_drvdata(serio->parent);
385 struct synaptics_data *priv = parent->private;
386
387 serio_pause_rx(parent->ps2dev.serio);
388 priv->pt_port = serio;
389 serio_continue_rx(parent->ps2dev.serio);
390
391 return 0;
392}
393
394static void synaptics_pt_stop(struct serio *serio)
395{
396 struct psmouse *parent = serio_get_drvdata(serio->parent);
397 struct synaptics_data *priv = parent->private;
398
399 serio_pause_rx(parent->ps2dev.serio);
400 priv->pt_port = NULL;
401 serio_continue_rx(parent->ps2dev.serio);
402}
403
404static int synaptics_is_pt_packet(unsigned char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
406 return (buf[0] & 0xFC) == 0x84 && (buf[3] & 0xCC) == 0xC4;
407}
408
409static void synaptics_pass_pt_packet(struct serio *ptport, unsigned char *packet)
410{
411 struct psmouse *child = serio_get_drvdata(ptport);
412
413 if (child && child->state == PSMOUSE_ACTIVATED) {
David Howells7d12e782006-10-05 14:55:46 +0100414 serio_interrupt(ptport, packet[1], 0);
415 serio_interrupt(ptport, packet[4], 0);
416 serio_interrupt(ptport, packet[5], 0);
Sergey Vlasov33fdfa92005-07-24 00:53:32 -0500417 if (child->pktsize == 4)
David Howells7d12e782006-10-05 14:55:46 +0100418 serio_interrupt(ptport, packet[2], 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 } else
David Howells7d12e782006-10-05 14:55:46 +0100420 serio_interrupt(ptport, packet[1], 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421}
422
423static void synaptics_pt_activate(struct psmouse *psmouse)
424{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 struct synaptics_data *priv = psmouse->private;
Dmitry Torokhova8b3c0f2010-10-04 21:46:10 -0700426 struct psmouse *child = serio_get_drvdata(priv->pt_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 /* adjust the touchpad to child's choice of protocol */
429 if (child) {
Sergey Vlasov33fdfa92005-07-24 00:53:32 -0500430 if (child->pktsize == 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 priv->mode |= SYN_BIT_FOUR_BYTE_CLIENT;
432 else
433 priv->mode &= ~SYN_BIT_FOUR_BYTE_CLIENT;
434
435 if (synaptics_mode_cmd(psmouse, priv->mode))
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700436 psmouse_warn(psmouse,
437 "failed to switch guest protocol\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 }
439}
440
441static void synaptics_pt_create(struct psmouse *psmouse)
442{
443 struct serio *serio;
444
Eric Sesterhennb39787a2006-03-14 00:09:16 -0500445 serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 if (!serio) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700447 psmouse_err(psmouse,
448 "not enough memory for pass-through port\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 return;
450 }
451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 serio->id.type = SERIO_PS_PSTHRU;
453 strlcpy(serio->name, "Synaptics pass-through", sizeof(serio->name));
454 strlcpy(serio->phys, "synaptics-pt/serio0", sizeof(serio->name));
455 serio->write = synaptics_pt_write;
Dmitry Torokhova8b3c0f2010-10-04 21:46:10 -0700456 serio->start = synaptics_pt_start;
457 serio->stop = synaptics_pt_stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 serio->parent = psmouse->ps2dev.serio;
459
460 psmouse->pt_activate = synaptics_pt_activate;
461
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700462 psmouse_info(psmouse, "serio: %s port at %s\n",
463 serio->name, psmouse->phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 serio_register_port(serio);
465}
466
467/*****************************************************************************
468 * Functions to interpret the absolute mode packets
469 ****************************************************************************/
470
Daniel Kurtza6ca40c2011-08-23 23:02:31 -0700471static void synaptics_mt_state_set(struct synaptics_mt_state *state, int count,
472 int sgm, int agm)
473{
474 state->count = count;
475 state->sgm = sgm;
476 state->agm = agm;
477}
478
Daniel Kurtz7afdb842011-08-23 23:00:33 -0700479static void synaptics_parse_agm(const unsigned char buf[],
Daniel Kurtza6ca40c2011-08-23 23:02:31 -0700480 struct synaptics_data *priv,
481 struct synaptics_hw_state *hw)
Daniel Kurtz7afdb842011-08-23 23:00:33 -0700482{
483 struct synaptics_hw_state *agm = &priv->agm;
Daniel Kurtza6ca40c2011-08-23 23:02:31 -0700484 int agm_packet_type;
Daniel Kurtz7afdb842011-08-23 23:00:33 -0700485
Daniel Kurtza6ca40c2011-08-23 23:02:31 -0700486 agm_packet_type = (buf[5] & 0x30) >> 4;
487 switch (agm_packet_type) {
488 case 1:
489 /* Gesture packet: (x, y, z) half resolution */
490 agm->w = hw->w;
491 agm->x = (((buf[4] & 0x0f) << 8) | buf[1]) << 1;
492 agm->y = (((buf[4] & 0xf0) << 4) | buf[2]) << 1;
493 agm->z = ((buf[3] & 0x30) | (buf[5] & 0x0f)) << 1;
494 break;
495
496 case 2:
497 /* AGM-CONTACT packet: (count, sgm, agm) */
498 synaptics_mt_state_set(&agm->mt_state, buf[1], buf[2], buf[4]);
499 break;
500
501 default:
502 break;
503 }
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700504
505 /* Record that at least one AGM has been received since last SGM */
506 priv->agm_pending = true;
Daniel Kurtz7afdb842011-08-23 23:00:33 -0700507}
508
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100509static int synaptics_parse_hw_state(const unsigned char buf[],
510 struct synaptics_data *priv,
511 struct synaptics_hw_state *hw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
513 memset(hw, 0, sizeof(struct synaptics_hw_state));
514
515 if (SYN_MODEL_NEWABS(priv->model_id)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 hw->w = (((buf[0] & 0x30) >> 2) |
517 ((buf[0] & 0x04) >> 1) |
518 ((buf[3] & 0x04) >> 2));
519
Dmitry Torokhov386ba132014-08-30 13:51:06 -0700520 if ((SYN_CAP_ADV_GESTURE(priv->ext_cap_0c) ||
521 SYN_CAP_IMAGE_SENSOR(priv->ext_cap_0c)) &&
522 hw->w == 2) {
523 synaptics_parse_agm(buf, priv, hw);
524 return 1;
525 }
526
527 hw->x = (((buf[3] & 0x10) << 8) |
528 ((buf[1] & 0x0f) << 8) |
529 buf[4]);
530 hw->y = (((buf[3] & 0x20) << 7) |
531 ((buf[1] & 0xf0) << 4) |
532 buf[5]);
533 hw->z = buf[2];
534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 hw->left = (buf[0] & 0x01) ? 1 : 0;
536 hw->right = (buf[0] & 0x02) ? 1 : 0;
537
Dmitry Torokhov386ba132014-08-30 13:51:06 -0700538 if (SYN_CAP_FORCEPAD(priv->ext_cap_0c)) {
539 /*
540 * ForcePads, like Clickpads, use middle button
541 * bits to report primary button clicks.
542 * Unfortunately they report primary button not
543 * only when user presses on the pad above certain
544 * threshold, but also when there are more than one
545 * finger on the touchpad, which interferes with
546 * out multi-finger gestures.
547 */
548 if (hw->z == 0) {
549 /* No contacts */
550 priv->press = priv->report_press = false;
551 } else if (hw->w >= 4 && ((buf[0] ^ buf[3]) & 0x01)) {
552 /*
553 * Single-finger touch with pressure above
554 * the threshold. If pressure stays long
555 * enough, we'll start reporting primary
556 * button. We rely on the device continuing
557 * sending data even if finger does not
558 * move.
559 */
560 if (!priv->press) {
561 priv->press_start = jiffies;
562 priv->press = true;
563 } else if (time_after(jiffies,
564 priv->press_start +
565 msecs_to_jiffies(50))) {
566 priv->report_press = true;
567 }
568 } else {
569 priv->press = false;
570 }
571
572 hw->left = priv->report_press;
573
574 } else if (SYN_CAP_CLICKPAD(priv->ext_cap_0c)) {
Takashi Iwai5f57d672010-04-19 10:37:21 -0700575 /*
576 * Clickpad's button is transmitted as middle button,
577 * however, since it is primary button, we will report
578 * it as BTN_LEFT.
579 */
580 hw->left = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0;
581
582 } else if (SYN_CAP_MIDDLE_BUTTON(priv->capabilities)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 hw->middle = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0;
584 if (hw->w == 2)
585 hw->scroll = (signed char)(buf[1]);
586 }
587
588 if (SYN_CAP_FOUR_BUTTON(priv->capabilities)) {
589 hw->up = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0;
590 hw->down = ((buf[0] ^ buf[3]) & 0x02) ? 1 : 0;
591 }
592
593 if (SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap) &&
594 ((buf[0] ^ buf[3]) & 0x02)) {
595 switch (SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap) & ~0x01) {
596 default:
597 /*
598 * if nExtBtn is greater than 8 it should be
599 * considered invalid and treated as 0
600 */
601 break;
602 case 8:
603 hw->ext_buttons |= ((buf[5] & 0x08)) ? 0x80 : 0;
604 hw->ext_buttons |= ((buf[4] & 0x08)) ? 0x40 : 0;
605 case 6:
606 hw->ext_buttons |= ((buf[5] & 0x04)) ? 0x20 : 0;
607 hw->ext_buttons |= ((buf[4] & 0x04)) ? 0x10 : 0;
608 case 4:
609 hw->ext_buttons |= ((buf[5] & 0x02)) ? 0x08 : 0;
610 hw->ext_buttons |= ((buf[4] & 0x02)) ? 0x04 : 0;
611 case 2:
612 hw->ext_buttons |= ((buf[5] & 0x01)) ? 0x02 : 0;
613 hw->ext_buttons |= ((buf[4] & 0x01)) ? 0x01 : 0;
614 }
615 }
616 } else {
617 hw->x = (((buf[1] & 0x1f) << 8) | buf[2]);
618 hw->y = (((buf[4] & 0x1f) << 8) | buf[5]);
619
620 hw->z = (((buf[0] & 0x30) << 2) | (buf[3] & 0x3F));
621 hw->w = (((buf[1] & 0x80) >> 4) | ((buf[0] & 0x04) >> 1));
622
623 hw->left = (buf[0] & 0x01) ? 1 : 0;
624 hw->right = (buf[0] & 0x02) ? 1 : 0;
625 }
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100626
Seth Forsheead4751d2012-09-28 10:29:21 -0700627 /*
628 * Convert wrap-around values to negative. (X|Y)_MAX_POSITIVE
629 * is used by some firmware to indicate a finger at the edge of
630 * the touchpad whose precise position cannot be determined, so
631 * convert these values to the maximum axis value.
632 */
Seth Forsheeba37c702012-07-24 23:54:11 -0700633 if (hw->x > X_MAX_POSITIVE)
634 hw->x -= 1 << ABS_POS_BITS;
Seth Forsheead4751d2012-09-28 10:29:21 -0700635 else if (hw->x == X_MAX_POSITIVE)
636 hw->x = XMAX;
637
Seth Forsheeba37c702012-07-24 23:54:11 -0700638 if (hw->y > Y_MAX_POSITIVE)
639 hw->y -= 1 << ABS_POS_BITS;
Seth Forsheead4751d2012-09-28 10:29:21 -0700640 else if (hw->y == Y_MAX_POSITIVE)
641 hw->y = YMAX;
Seth Forsheeba37c702012-07-24 23:54:11 -0700642
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100643 return 0;
644}
645
Daniel Kurtzbea9f0f2011-07-06 22:42:52 -0700646static void synaptics_report_semi_mt_slot(struct input_dev *dev, int slot,
647 bool active, int x, int y)
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100648{
649 input_mt_slot(dev, slot);
650 input_mt_report_slot_state(dev, MT_TOOL_FINGER, active);
651 if (active) {
652 input_report_abs(dev, ABS_MT_POSITION_X, x);
Daniel Kurtz6de58dd2011-08-23 23:00:24 -0700653 input_report_abs(dev, ABS_MT_POSITION_Y, synaptics_invert_y(y));
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100654 }
655}
656
657static void synaptics_report_semi_mt_data(struct input_dev *dev,
658 const struct synaptics_hw_state *a,
659 const struct synaptics_hw_state *b,
660 int num_fingers)
661{
662 if (num_fingers >= 2) {
Daniel Kurtzbea9f0f2011-07-06 22:42:52 -0700663 synaptics_report_semi_mt_slot(dev, 0, true, min(a->x, b->x),
664 min(a->y, b->y));
665 synaptics_report_semi_mt_slot(dev, 1, true, max(a->x, b->x),
666 max(a->y, b->y));
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100667 } else if (num_fingers == 1) {
Daniel Kurtzbea9f0f2011-07-06 22:42:52 -0700668 synaptics_report_semi_mt_slot(dev, 0, true, a->x, a->y);
669 synaptics_report_semi_mt_slot(dev, 1, false, 0, 0);
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100670 } else {
Daniel Kurtzbea9f0f2011-07-06 22:42:52 -0700671 synaptics_report_semi_mt_slot(dev, 0, false, 0, 0);
672 synaptics_report_semi_mt_slot(dev, 1, false, 0, 0);
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674}
675
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700676static void synaptics_report_buttons(struct psmouse *psmouse,
677 const struct synaptics_hw_state *hw)
678{
679 struct input_dev *dev = psmouse->dev;
680 struct synaptics_data *priv = psmouse->private;
681 int i;
682
683 input_report_key(dev, BTN_LEFT, hw->left);
684 input_report_key(dev, BTN_RIGHT, hw->right);
685
686 if (SYN_CAP_MIDDLE_BUTTON(priv->capabilities))
687 input_report_key(dev, BTN_MIDDLE, hw->middle);
688
689 if (SYN_CAP_FOUR_BUTTON(priv->capabilities)) {
690 input_report_key(dev, BTN_FORWARD, hw->up);
691 input_report_key(dev, BTN_BACK, hw->down);
692 }
693
694 for (i = 0; i < SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap); i++)
695 input_report_key(dev, BTN_0 + i, hw->ext_buttons & (1 << i));
696}
697
698static void synaptics_report_slot(struct input_dev *dev, int slot,
699 const struct synaptics_hw_state *hw)
700{
701 input_mt_slot(dev, slot);
702 input_mt_report_slot_state(dev, MT_TOOL_FINGER, (hw != NULL));
703 if (!hw)
704 return;
705
706 input_report_abs(dev, ABS_MT_POSITION_X, hw->x);
707 input_report_abs(dev, ABS_MT_POSITION_Y, synaptics_invert_y(hw->y));
708 input_report_abs(dev, ABS_MT_PRESSURE, hw->z);
709}
710
711static void synaptics_report_mt_data(struct psmouse *psmouse,
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700712 struct synaptics_mt_state *mt_state,
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700713 const struct synaptics_hw_state *sgm)
714{
715 struct input_dev *dev = psmouse->dev;
716 struct synaptics_data *priv = psmouse->private;
717 struct synaptics_hw_state *agm = &priv->agm;
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700718 struct synaptics_mt_state *old = &priv->mt_state;
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700719
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700720 switch (mt_state->count) {
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700721 case 0:
722 synaptics_report_slot(dev, 0, NULL);
723 synaptics_report_slot(dev, 1, NULL);
724 break;
725 case 1:
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700726 if (mt_state->sgm == -1) {
727 synaptics_report_slot(dev, 0, NULL);
728 synaptics_report_slot(dev, 1, NULL);
729 } else if (mt_state->sgm == 0) {
730 synaptics_report_slot(dev, 0, sgm);
731 synaptics_report_slot(dev, 1, NULL);
732 } else {
733 synaptics_report_slot(dev, 0, NULL);
734 synaptics_report_slot(dev, 1, sgm);
735 }
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700736 break;
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700737 default:
738 /*
739 * If the finger slot contained in SGM is valid, and either
740 * hasn't changed, or is new, then report SGM in MTB slot 0.
741 * Otherwise, empty MTB slot 0.
742 */
743 if (mt_state->sgm != -1 &&
744 (mt_state->sgm == old->sgm || old->sgm == -1))
745 synaptics_report_slot(dev, 0, sgm);
746 else
747 synaptics_report_slot(dev, 0, NULL);
748
749 /*
750 * If the finger slot contained in AGM is valid, and either
751 * hasn't changed, or is new, then report AGM in MTB slot 1.
752 * Otherwise, empty MTB slot 1.
753 */
754 if (mt_state->agm != -1 &&
755 (mt_state->agm == old->agm || old->agm == -1))
756 synaptics_report_slot(dev, 1, agm);
757 else
758 synaptics_report_slot(dev, 1, NULL);
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700759 break;
760 }
761
762 /* Don't use active slot count to generate BTN_TOOL events. */
763 input_mt_report_pointer_emulation(dev, false);
764
765 /* Send the number of fingers reported by touchpad itself. */
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700766 input_mt_report_finger_count(dev, mt_state->count);
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700767
768 synaptics_report_buttons(psmouse, sgm);
769
770 input_sync(dev);
771}
772
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700773/* Handle case where mt_state->count = 0 */
774static void synaptics_image_sensor_0f(struct synaptics_data *priv,
775 struct synaptics_mt_state *mt_state)
776{
777 synaptics_mt_state_set(mt_state, 0, -1, -1);
778 priv->mt_state_lost = false;
779}
780
781/* Handle case where mt_state->count = 1 */
782static void synaptics_image_sensor_1f(struct synaptics_data *priv,
783 struct synaptics_mt_state *mt_state)
784{
785 struct synaptics_hw_state *agm = &priv->agm;
786 struct synaptics_mt_state *old = &priv->mt_state;
787
788 /*
789 * If the last AGM was (0,0,0), and there is only one finger left,
790 * then we absolutely know that SGM contains slot 0, and all other
791 * fingers have been removed.
792 */
793 if (priv->agm_pending && agm->z == 0) {
794 synaptics_mt_state_set(mt_state, 1, 0, -1);
795 priv->mt_state_lost = false;
796 return;
797 }
798
799 switch (old->count) {
800 case 0:
801 synaptics_mt_state_set(mt_state, 1, 0, -1);
802 break;
803 case 1:
804 /*
805 * If mt_state_lost, then the previous transition was 3->1,
806 * and SGM now contains either slot 0 or 1, but we don't know
807 * which. So, we just assume that the SGM now contains slot 1.
808 *
809 * If pending AGM and either:
810 * (a) the previous SGM slot contains slot 0, or
811 * (b) there was no SGM slot
812 * then, the SGM now contains slot 1
813 *
814 * Case (a) happens with very rapid "drum roll" gestures, where
815 * slot 0 finger is lifted and a new slot 1 finger touches
816 * within one reporting interval.
817 *
818 * Case (b) happens if initially two or more fingers tap
819 * briefly, and all but one lift before the end of the first
820 * reporting interval.
821 *
822 * (In both these cases, slot 0 will becomes empty, so SGM
823 * contains slot 1 with the new finger)
824 *
825 * Else, if there was no previous SGM, it now contains slot 0.
826 *
827 * Otherwise, SGM still contains the same slot.
828 */
829 if (priv->mt_state_lost ||
830 (priv->agm_pending && old->sgm <= 0))
831 synaptics_mt_state_set(mt_state, 1, 1, -1);
832 else if (old->sgm == -1)
833 synaptics_mt_state_set(mt_state, 1, 0, -1);
834 break;
835 case 2:
836 /*
837 * If mt_state_lost, we don't know which finger SGM contains.
838 *
839 * So, report 1 finger, but with both slots empty.
840 * We will use slot 1 on subsequent 1->1
841 */
842 if (priv->mt_state_lost) {
843 synaptics_mt_state_set(mt_state, 1, -1, -1);
844 break;
845 }
846 /*
847 * Since the last AGM was NOT (0,0,0), it was the finger in
848 * slot 0 that has been removed.
849 * So, SGM now contains previous AGM's slot, and AGM is now
850 * empty.
851 */
852 synaptics_mt_state_set(mt_state, 1, old->agm, -1);
853 break;
854 case 3:
855 /*
856 * Since last AGM was not (0,0,0), we don't know which finger
857 * is left.
858 *
859 * So, report 1 finger, but with both slots empty.
860 * We will use slot 1 on subsequent 1->1
861 */
862 synaptics_mt_state_set(mt_state, 1, -1, -1);
863 priv->mt_state_lost = true;
864 break;
Daniel Kurtz6b4b49f2011-08-23 23:02:56 -0700865 case 4:
866 case 5:
867 /* mt_state was updated by AGM-CONTACT packet */
868 break;
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700869 }
870}
871
872/* Handle case where mt_state->count = 2 */
873static void synaptics_image_sensor_2f(struct synaptics_data *priv,
874 struct synaptics_mt_state *mt_state)
875{
876 struct synaptics_mt_state *old = &priv->mt_state;
877
878 switch (old->count) {
879 case 0:
880 synaptics_mt_state_set(mt_state, 2, 0, 1);
881 break;
882 case 1:
883 /*
884 * If previous SGM contained slot 1 or higher, SGM now contains
885 * slot 0 (the newly touching finger) and AGM contains SGM's
886 * previous slot.
887 *
888 * Otherwise, SGM still contains slot 0 and AGM now contains
889 * slot 1.
890 */
891 if (old->sgm >= 1)
892 synaptics_mt_state_set(mt_state, 2, 0, old->sgm);
893 else
894 synaptics_mt_state_set(mt_state, 2, 0, 1);
895 break;
896 case 2:
897 /*
898 * If mt_state_lost, SGM now contains either finger 1 or 2, but
899 * we don't know which.
900 * So, we just assume that the SGM contains slot 0 and AGM 1.
901 */
902 if (priv->mt_state_lost)
903 synaptics_mt_state_set(mt_state, 2, 0, 1);
904 /*
905 * Otherwise, use the same mt_state, since it either hasn't
906 * changed, or was updated by a recently received AGM-CONTACT
907 * packet.
908 */
909 break;
910 case 3:
911 /*
912 * 3->2 transitions have two unsolvable problems:
913 * 1) no indication is given which finger was removed
914 * 2) no way to tell if agm packet was for finger 3
915 * before 3->2, or finger 2 after 3->2.
916 *
917 * So, report 2 fingers, but empty all slots.
918 * We will guess slots [0,1] on subsequent 2->2.
919 */
920 synaptics_mt_state_set(mt_state, 2, -1, -1);
921 priv->mt_state_lost = true;
922 break;
Daniel Kurtz6b4b49f2011-08-23 23:02:56 -0700923 case 4:
924 case 5:
925 /* mt_state was updated by AGM-CONTACT packet */
926 break;
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700927 }
928}
929
930/* Handle case where mt_state->count = 3 */
931static void synaptics_image_sensor_3f(struct synaptics_data *priv,
932 struct synaptics_mt_state *mt_state)
933{
934 struct synaptics_mt_state *old = &priv->mt_state;
935
936 switch (old->count) {
937 case 0:
938 synaptics_mt_state_set(mt_state, 3, 0, 2);
939 break;
940 case 1:
941 /*
942 * If previous SGM contained slot 2 or higher, SGM now contains
943 * slot 0 (one of the newly touching fingers) and AGM contains
944 * SGM's previous slot.
945 *
946 * Otherwise, SGM now contains slot 0 and AGM contains slot 2.
947 */
948 if (old->sgm >= 2)
949 synaptics_mt_state_set(mt_state, 3, 0, old->sgm);
950 else
951 synaptics_mt_state_set(mt_state, 3, 0, 2);
952 break;
953 case 2:
954 /*
Daniel Kurtz6b4b49f2011-08-23 23:02:56 -0700955 * If the AGM previously contained slot 3 or higher, then the
956 * newly touching finger is in the lowest available slot.
957 *
958 * If SGM was previously 1 or higher, then the new SGM is
959 * now slot 0 (with a new finger), otherwise, the new finger
960 * is now in a hidden slot between 0 and AGM's slot.
961 *
962 * In all such cases, the SGM now contains slot 0, and the AGM
963 * continues to contain the same slot as before.
964 */
965 if (old->agm >= 3) {
966 synaptics_mt_state_set(mt_state, 3, 0, old->agm);
967 break;
968 }
969
970 /*
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700971 * After some 3->1 and all 3->2 transitions, we lose track
972 * of which slot is reported by SGM and AGM.
973 *
974 * For 2->3 in this state, report 3 fingers, but empty all
975 * slots, and we will guess (0,2) on a subsequent 0->3.
976 *
977 * To userspace, the resulting transition will look like:
978 * 2:[0,1] -> 3:[-1,-1] -> 3:[0,2]
979 */
980 if (priv->mt_state_lost) {
981 synaptics_mt_state_set(mt_state, 3, -1, -1);
982 break;
983 }
984
985 /*
986 * If the (SGM,AGM) really previously contained slots (0, 1),
987 * then we cannot know what slot was just reported by the AGM,
988 * because the 2->3 transition can occur either before or after
989 * the AGM packet. Thus, this most recent AGM could contain
990 * either the same old slot 1 or the new slot 2.
991 * Subsequent AGMs will be reporting slot 2.
992 *
993 * To userspace, the resulting transition will look like:
994 * 2:[0,1] -> 3:[0,-1] -> 3:[0,2]
995 */
996 synaptics_mt_state_set(mt_state, 3, 0, -1);
997 break;
998 case 3:
999 /*
1000 * If, for whatever reason, the previous agm was invalid,
1001 * Assume SGM now contains slot 0, AGM now contains slot 2.
1002 */
1003 if (old->agm <= 2)
1004 synaptics_mt_state_set(mt_state, 3, 0, 2);
1005 /*
1006 * mt_state either hasn't changed, or was updated by a recently
1007 * received AGM-CONTACT packet.
1008 */
1009 break;
Daniel Kurtz6b4b49f2011-08-23 23:02:56 -07001010
1011 case 4:
1012 case 5:
1013 /* mt_state was updated by AGM-CONTACT packet */
1014 break;
Daniel Kurtz4dc772d2011-08-23 23:02:40 -07001015 }
1016}
1017
Daniel Kurtz6b4b49f2011-08-23 23:02:56 -07001018/* Handle case where mt_state->count = 4, or = 5 */
1019static void synaptics_image_sensor_45f(struct synaptics_data *priv,
1020 struct synaptics_mt_state *mt_state)
1021{
1022 /* mt_state was updated correctly by AGM-CONTACT packet */
1023 priv->mt_state_lost = false;
1024}
1025
Daniel Kurtz3cdfee92011-08-23 23:02:25 -07001026static void synaptics_image_sensor_process(struct psmouse *psmouse,
1027 struct synaptics_hw_state *sgm)
1028{
Daniel Kurtz4dc772d2011-08-23 23:02:40 -07001029 struct synaptics_data *priv = psmouse->private;
1030 struct synaptics_hw_state *agm = &priv->agm;
1031 struct synaptics_mt_state mt_state;
Daniel Kurtz3cdfee92011-08-23 23:02:25 -07001032
Daniel Kurtz4dc772d2011-08-23 23:02:40 -07001033 /* Initialize using current mt_state (as updated by last agm) */
1034 mt_state = agm->mt_state;
1035
1036 /*
1037 * Update mt_state using the new finger count and current mt_state.
1038 */
Daniel Kurtz3cdfee92011-08-23 23:02:25 -07001039 if (sgm->z == 0)
Daniel Kurtz4dc772d2011-08-23 23:02:40 -07001040 synaptics_image_sensor_0f(priv, &mt_state);
Daniel Kurtz3cdfee92011-08-23 23:02:25 -07001041 else if (sgm->w >= 4)
Daniel Kurtz4dc772d2011-08-23 23:02:40 -07001042 synaptics_image_sensor_1f(priv, &mt_state);
Daniel Kurtz3cdfee92011-08-23 23:02:25 -07001043 else if (sgm->w == 0)
Daniel Kurtz4dc772d2011-08-23 23:02:40 -07001044 synaptics_image_sensor_2f(priv, &mt_state);
Daniel Kurtz6b4b49f2011-08-23 23:02:56 -07001045 else if (sgm->w == 1 && mt_state.count <= 3)
Daniel Kurtz4dc772d2011-08-23 23:02:40 -07001046 synaptics_image_sensor_3f(priv, &mt_state);
Daniel Kurtz6b4b49f2011-08-23 23:02:56 -07001047 else
1048 synaptics_image_sensor_45f(priv, &mt_state);
Daniel Kurtz3cdfee92011-08-23 23:02:25 -07001049
1050 /* Send resulting input events to user space */
Daniel Kurtz4dc772d2011-08-23 23:02:40 -07001051 synaptics_report_mt_data(psmouse, &mt_state, sgm);
1052
1053 /* Store updated mt_state */
1054 priv->mt_state = agm->mt_state = mt_state;
1055 priv->agm_pending = false;
Daniel Kurtz3cdfee92011-08-23 23:02:25 -07001056}
1057
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058/*
1059 * called for each full received packet from the touchpad
1060 */
1061static void synaptics_process_packet(struct psmouse *psmouse)
1062{
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001063 struct input_dev *dev = psmouse->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 struct synaptics_data *priv = psmouse->private;
1065 struct synaptics_hw_state hw;
1066 int num_fingers;
1067 int finger_width;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Henrik Rydbergfec6e522010-12-21 18:11:25 +01001069 if (synaptics_parse_hw_state(psmouse->packet, priv, &hw))
1070 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
Daniel Kurtz3cdfee92011-08-23 23:02:25 -07001072 if (SYN_CAP_IMAGE_SENSOR(priv->ext_cap_0c)) {
1073 synaptics_image_sensor_process(psmouse, &hw);
1074 return;
1075 }
1076
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 if (hw.scroll) {
1078 priv->scroll += hw.scroll;
1079
1080 while (priv->scroll >= 4) {
1081 input_report_key(dev, BTN_BACK, !hw.down);
1082 input_sync(dev);
1083 input_report_key(dev, BTN_BACK, hw.down);
1084 input_sync(dev);
1085 priv->scroll -= 4;
1086 }
1087 while (priv->scroll <= -4) {
1088 input_report_key(dev, BTN_FORWARD, !hw.up);
1089 input_sync(dev);
1090 input_report_key(dev, BTN_FORWARD, hw.up);
1091 input_sync(dev);
1092 priv->scroll += 4;
1093 }
1094 return;
1095 }
1096
Henrik Rydberg4f56ce92010-12-18 15:42:30 +01001097 if (hw.z > 0 && hw.x > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 num_fingers = 1;
1099 finger_width = 5;
1100 if (SYN_CAP_EXTENDED(priv->capabilities)) {
1101 switch (hw.w) {
1102 case 0 ... 1:
1103 if (SYN_CAP_MULTIFINGER(priv->capabilities))
1104 num_fingers = hw.w + 2;
1105 break;
1106 case 2:
1107 if (SYN_MODEL_PEN(priv->model_id))
1108 ; /* Nothing, treat a pen as a single finger */
1109 break;
1110 case 4 ... 15:
1111 if (SYN_CAP_PALMDETECT(priv->capabilities))
1112 finger_width = hw.w;
1113 break;
1114 }
1115 }
1116 } else {
1117 num_fingers = 0;
1118 finger_width = 0;
1119 }
1120
Henrik Rydbergfec6e522010-12-21 18:11:25 +01001121 if (SYN_CAP_ADV_GESTURE(priv->ext_cap_0c))
Daniel Kurtz7afdb842011-08-23 23:00:33 -07001122 synaptics_report_semi_mt_data(dev, &hw, &priv->agm,
1123 num_fingers);
Henrik Rydbergfec6e522010-12-21 18:11:25 +01001124
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 /* Post events
1126 * BTN_TOUCH has to be first as mousedev relies on it when doing
1127 * absolute -> relative conversion
1128 */
1129 if (hw.z > 30) input_report_key(dev, BTN_TOUCH, 1);
1130 if (hw.z < 25) input_report_key(dev, BTN_TOUCH, 0);
1131
Henrik Rydberg4f56ce92010-12-18 15:42:30 +01001132 if (num_fingers > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 input_report_abs(dev, ABS_X, hw.x);
Daniel Kurtz6de58dd2011-08-23 23:00:24 -07001134 input_report_abs(dev, ABS_Y, synaptics_invert_y(hw.y));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 }
1136 input_report_abs(dev, ABS_PRESSURE, hw.z);
1137
Chris Bagwell2a8e7712010-07-19 09:06:15 -07001138 if (SYN_CAP_PALMDETECT(priv->capabilities))
1139 input_report_abs(dev, ABS_TOOL_WIDTH, finger_width);
1140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 input_report_key(dev, BTN_TOOL_FINGER, num_fingers == 1);
Peter Hutterere42b6642008-11-20 15:24:42 -05001142 if (SYN_CAP_MULTIFINGER(priv->capabilities)) {
1143 input_report_key(dev, BTN_TOOL_DOUBLETAP, num_fingers == 2);
1144 input_report_key(dev, BTN_TOOL_TRIPLETAP, num_fingers == 3);
1145 }
1146
Daniel Kurtz3cdfee92011-08-23 23:02:25 -07001147 synaptics_report_buttons(psmouse, &hw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
1149 input_sync(dev);
1150}
1151
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001152static int synaptics_validate_byte(struct psmouse *psmouse,
1153 int idx, unsigned char pkt_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154{
Helge Dellere38de672006-09-10 21:54:39 -04001155 static const unsigned char newabs_mask[] = { 0xC8, 0x00, 0x00, 0xC8, 0x00 };
1156 static const unsigned char newabs_rel_mask[] = { 0xC0, 0x00, 0x00, 0xC0, 0x00 };
1157 static const unsigned char newabs_rslt[] = { 0x80, 0x00, 0x00, 0xC0, 0x00 };
1158 static const unsigned char oldabs_mask[] = { 0xC0, 0x60, 0x00, 0xC0, 0x60 };
1159 static const unsigned char oldabs_rslt[] = { 0xC0, 0x00, 0x00, 0x80, 0x00 };
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001160 const char *packet = psmouse->packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
1162 if (idx < 0 || idx > 4)
1163 return 0;
1164
1165 switch (pkt_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Dmitry Torokhova62f0d22010-05-19 10:39:17 -07001167 case SYN_NEWABS:
1168 case SYN_NEWABS_RELAXED:
1169 return (packet[idx] & newabs_rel_mask[idx]) == newabs_rslt[idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
Dmitry Torokhova62f0d22010-05-19 10:39:17 -07001171 case SYN_NEWABS_STRICT:
1172 return (packet[idx] & newabs_mask[idx]) == newabs_rslt[idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Dmitry Torokhova62f0d22010-05-19 10:39:17 -07001174 case SYN_OLDABS:
1175 return (packet[idx] & oldabs_mask[idx]) == oldabs_rslt[idx];
1176
1177 default:
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001178 psmouse_err(psmouse, "unknown packet type %d\n", pkt_type);
Dmitry Torokhova62f0d22010-05-19 10:39:17 -07001179 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 }
1181}
1182
1183static unsigned char synaptics_detect_pkt_type(struct psmouse *psmouse)
1184{
1185 int i;
1186
1187 for (i = 0; i < 5; i++)
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001188 if (!synaptics_validate_byte(psmouse, i, SYN_NEWABS_STRICT)) {
1189 psmouse_info(psmouse, "using relaxed packet validation\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 return SYN_NEWABS_RELAXED;
1191 }
1192
1193 return SYN_NEWABS_STRICT;
1194}
1195
David Howells7d12e782006-10-05 14:55:46 +01001196static psmouse_ret_t synaptics_process_byte(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 struct synaptics_data *priv = psmouse->private;
1199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 if (psmouse->pktcnt >= 6) { /* Full packet received */
1201 if (unlikely(priv->pkt_type == SYN_NEWABS))
1202 priv->pkt_type = synaptics_detect_pkt_type(psmouse);
1203
Dmitry Torokhova8b3c0f2010-10-04 21:46:10 -07001204 if (SYN_CAP_PASS_THROUGH(priv->capabilities) &&
1205 synaptics_is_pt_packet(psmouse->packet)) {
1206 if (priv->pt_port)
1207 synaptics_pass_pt_packet(priv->pt_port, psmouse->packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 } else
1209 synaptics_process_packet(psmouse);
1210
1211 return PSMOUSE_FULL_PACKET;
1212 }
1213
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001214 return synaptics_validate_byte(psmouse, psmouse->pktcnt - 1, priv->pkt_type) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 PSMOUSE_GOOD_DATA : PSMOUSE_BAD_DATA;
1216}
1217
1218/*****************************************************************************
1219 * Driver initialization/cleanup functions
1220 ****************************************************************************/
Daniel Kurtz85615472011-08-23 23:00:41 -07001221static void set_abs_position_params(struct input_dev *dev,
1222 struct synaptics_data *priv, int x_code,
1223 int y_code)
1224{
1225 int x_min = priv->x_min ?: XMIN_NOMINAL;
1226 int x_max = priv->x_max ?: XMAX_NOMINAL;
1227 int y_min = priv->y_min ?: YMIN_NOMINAL;
1228 int y_max = priv->y_max ?: YMAX_NOMINAL;
1229 int fuzz = SYN_CAP_REDUCED_FILTERING(priv->ext_cap_0c) ?
1230 SYN_REDUCED_FILTER_FUZZ : 0;
1231
1232 input_set_abs_params(dev, x_code, x_min, x_max, fuzz, 0);
1233 input_set_abs_params(dev, y_code, y_min, y_max, fuzz, 0);
1234 input_abs_set_res(dev, x_code, priv->x_res);
1235 input_abs_set_res(dev, y_code, priv->y_res);
1236}
1237
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238static void set_input_params(struct input_dev *dev, struct synaptics_data *priv)
1239{
1240 int i;
1241
Daniel Drake7968a5d2011-11-08 00:00:35 -08001242 /* Things that apply to both modes */
Henrik Rydbergc14890a82010-12-16 09:52:23 +01001243 __set_bit(INPUT_PROP_POINTER, dev->propbit);
Daniel Drake7968a5d2011-11-08 00:00:35 -08001244 __set_bit(EV_KEY, dev->evbit);
1245 __set_bit(BTN_LEFT, dev->keybit);
1246 __set_bit(BTN_RIGHT, dev->keybit);
Henrik Rydbergc14890a82010-12-16 09:52:23 +01001247
Daniel Drake7968a5d2011-11-08 00:00:35 -08001248 if (SYN_CAP_MIDDLE_BUTTON(priv->capabilities))
1249 __set_bit(BTN_MIDDLE, dev->keybit);
1250
1251 if (!priv->absolute_mode) {
1252 /* Relative mode */
1253 __set_bit(EV_REL, dev->evbit);
1254 __set_bit(REL_X, dev->relbit);
1255 __set_bit(REL_Y, dev->relbit);
1256 return;
1257 }
1258
1259 /* Absolute mode */
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001260 __set_bit(EV_ABS, dev->evbit);
Daniel Kurtz85615472011-08-23 23:00:41 -07001261 set_abs_position_params(dev, priv, ABS_X, ABS_Y);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 input_set_abs_params(dev, ABS_PRESSURE, 0, 255, 0, 0);
Chris Bagwell2a8e7712010-07-19 09:06:15 -07001263
Daniel Kurtz3cdfee92011-08-23 23:02:25 -07001264 if (SYN_CAP_IMAGE_SENSOR(priv->ext_cap_0c)) {
1265 input_mt_init_slots(dev, 2);
1266 set_abs_position_params(dev, priv, ABS_MT_POSITION_X,
1267 ABS_MT_POSITION_Y);
1268 /* Image sensors can report per-contact pressure */
1269 input_set_abs_params(dev, ABS_MT_PRESSURE, 0, 255, 0, 0);
Daniel Kurtz6b4b49f2011-08-23 23:02:56 -07001270
1271 /* Image sensors can signal 4 and 5 finger clicks */
1272 __set_bit(BTN_TOOL_QUADTAP, dev->keybit);
1273 __set_bit(BTN_TOOL_QUINTTAP, dev->keybit);
Daniel Kurtz3cdfee92011-08-23 23:02:25 -07001274 } else if (SYN_CAP_ADV_GESTURE(priv->ext_cap_0c)) {
1275 /* Non-image sensors with AGM use semi-mt */
Henrik Rydbergfec6e522010-12-21 18:11:25 +01001276 __set_bit(INPUT_PROP_SEMI_MT, dev->propbit);
1277 input_mt_init_slots(dev, 2);
Daniel Kurtz85615472011-08-23 23:00:41 -07001278 set_abs_position_params(dev, priv, ABS_MT_POSITION_X,
1279 ABS_MT_POSITION_Y);
Henrik Rydbergfec6e522010-12-21 18:11:25 +01001280 }
1281
Chris Bagwell2a8e7712010-07-19 09:06:15 -07001282 if (SYN_CAP_PALMDETECT(priv->capabilities))
Chris Bagwell58fb0212010-07-19 09:06:15 -07001283 input_set_abs_params(dev, ABS_TOOL_WIDTH, 0, 15, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001285 __set_bit(BTN_TOUCH, dev->keybit);
1286 __set_bit(BTN_TOOL_FINGER, dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
Peter Hutterere42b6642008-11-20 15:24:42 -05001288 if (SYN_CAP_MULTIFINGER(priv->capabilities)) {
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001289 __set_bit(BTN_TOOL_DOUBLETAP, dev->keybit);
1290 __set_bit(BTN_TOOL_TRIPLETAP, dev->keybit);
Peter Hutterere42b6642008-11-20 15:24:42 -05001291 }
1292
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 if (SYN_CAP_FOUR_BUTTON(priv->capabilities) ||
1294 SYN_CAP_MIDDLE_BUTTON(priv->capabilities)) {
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001295 __set_bit(BTN_FORWARD, dev->keybit);
1296 __set_bit(BTN_BACK, dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 }
1298
1299 for (i = 0; i < SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap); i++)
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001300 __set_bit(BTN_0 + i, dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001302 __clear_bit(EV_REL, dev->evbit);
1303 __clear_bit(REL_X, dev->relbit);
1304 __clear_bit(REL_Y, dev->relbit);
Tero Saarniec20a022009-06-10 23:27:24 -07001305
Takashi Iwai5f57d672010-04-19 10:37:21 -07001306 if (SYN_CAP_CLICKPAD(priv->ext_cap_0c)) {
Henrik Rydbergc14890a82010-12-16 09:52:23 +01001307 __set_bit(INPUT_PROP_BUTTONPAD, dev->propbit);
Takashi Iwai5f57d672010-04-19 10:37:21 -07001308 /* Clickpads report only left button */
1309 __clear_bit(BTN_RIGHT, dev->keybit);
1310 __clear_bit(BTN_MIDDLE, dev->keybit);
1311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312}
1313
Daniel Drake7968a5d2011-11-08 00:00:35 -08001314static ssize_t synaptics_show_disable_gesture(struct psmouse *psmouse,
1315 void *data, char *buf)
1316{
1317 struct synaptics_data *priv = psmouse->private;
1318
1319 return sprintf(buf, "%c\n", priv->disable_gesture ? '1' : '0');
1320}
1321
1322static ssize_t synaptics_set_disable_gesture(struct psmouse *psmouse,
1323 void *data, const char *buf,
1324 size_t len)
1325{
1326 struct synaptics_data *priv = psmouse->private;
1327 unsigned int value;
1328 int err;
1329
1330 err = kstrtouint(buf, 10, &value);
1331 if (err)
1332 return err;
1333
1334 if (value > 1)
1335 return -EINVAL;
1336
1337 if (value == priv->disable_gesture)
1338 return len;
1339
1340 priv->disable_gesture = value;
1341 if (value)
1342 priv->mode |= SYN_BIT_DISABLE_GESTURE;
1343 else
1344 priv->mode &= ~SYN_BIT_DISABLE_GESTURE;
1345
1346 if (synaptics_mode_cmd(psmouse, priv->mode))
1347 return -EIO;
1348
1349 return len;
1350}
1351
1352PSMOUSE_DEFINE_ATTR(disable_gesture, S_IWUSR | S_IRUGO, NULL,
1353 synaptics_show_disable_gesture,
1354 synaptics_set_disable_gesture);
1355
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356static void synaptics_disconnect(struct psmouse *psmouse)
1357{
Daniel Drake7968a5d2011-11-08 00:00:35 -08001358 struct synaptics_data *priv = psmouse->private;
1359
1360 if (!priv->absolute_mode && SYN_ID_DISGEST_SUPPORTED(priv->identity))
1361 device_remove_file(&psmouse->ps2dev.serio->dev,
1362 &psmouse_attr_disable_gesture.dattr);
1363
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 synaptics_reset(psmouse);
Daniel Drake7968a5d2011-11-08 00:00:35 -08001365 kfree(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 psmouse->private = NULL;
1367}
1368
1369static int synaptics_reconnect(struct psmouse *psmouse)
1370{
1371 struct synaptics_data *priv = psmouse->private;
1372 struct synaptics_data old_priv = *priv;
Alexandre Peixoto Ferreirac63fe0a2011-01-28 22:05:14 -08001373 int retry = 0;
1374 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Alexandre Peixoto Ferreirac63fe0a2011-01-28 22:05:14 -08001376 do {
1377 psmouse_reset(psmouse);
Dmitry Torokhov85214782011-12-12 00:05:53 -08001378 if (retry) {
1379 /*
1380 * On some boxes, right after resuming, the touchpad
1381 * needs some time to finish initializing (I assume
1382 * it needs time to calibrate) and start responding
1383 * to Synaptics-specific queries, so let's wait a
1384 * bit.
1385 */
1386 ssleep(1);
1387 }
Alexandre Peixoto Ferreirac63fe0a2011-01-28 22:05:14 -08001388 error = synaptics_detect(psmouse, 0);
1389 } while (error && ++retry < 3);
Andy Whitcroft4d368452009-02-28 12:51:01 -08001390
Alexandre Peixoto Ferreirac63fe0a2011-01-28 22:05:14 -08001391 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 return -1;
1393
Alexandre Peixoto Ferreirac63fe0a2011-01-28 22:05:14 -08001394 if (retry > 1)
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001395 psmouse_dbg(psmouse, "reconnected after %d tries\n", retry);
Alexandre Peixoto Ferreirac63fe0a2011-01-28 22:05:14 -08001396
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 if (synaptics_query_hardware(psmouse)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001398 psmouse_err(psmouse, "Unable to query device.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 return -1;
1400 }
1401
Daniel Drake7968a5d2011-11-08 00:00:35 -08001402 if (synaptics_set_mode(psmouse)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001403 psmouse_err(psmouse, "Unable to initialize device.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 return -1;
1405 }
1406
Alexandre Peixoto Ferreirabaddf582011-01-28 22:05:14 -08001407 if (old_priv.identity != priv->identity ||
1408 old_priv.model_id != priv->model_id ||
1409 old_priv.capabilities != priv->capabilities ||
1410 old_priv.ext_cap != priv->ext_cap) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001411 psmouse_err(psmouse,
1412 "hardware appears to be different: id(%ld-%ld), model(%ld-%ld), caps(%lx-%lx), ext(%lx-%lx).\n",
1413 old_priv.identity, priv->identity,
1414 old_priv.model_id, priv->model_id,
1415 old_priv.capabilities, priv->capabilities,
1416 old_priv.ext_cap, priv->ext_cap);
Alexandre Peixoto Ferreirabaddf582011-01-28 22:05:14 -08001417 return -1;
1418 }
1419
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 return 0;
1421}
1422
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001423static bool impaired_toshiba_kbc;
1424
1425static const struct dmi_system_id __initconst toshiba_dmi_table[] = {
1426#if defined(CONFIG_DMI) && defined(CONFIG_X86)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 {
Dmitry Torokhov9961e252009-12-04 10:24:20 -08001428 /* Toshiba Satellite */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 .matches = {
1430 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
Richard Thrippleton53a26702006-04-02 00:10:18 -05001431 DMI_MATCH(DMI_PRODUCT_NAME, "Satellite"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 },
1433 },
Simon Horman9ba5eaa2005-07-11 01:07:20 -05001434 {
Dmitry Torokhov9961e252009-12-04 10:24:20 -08001435 /* Toshiba Dynabook */
Simon Horman9ba5eaa2005-07-11 01:07:20 -05001436 .matches = {
1437 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
Richard Thrippleton53a26702006-04-02 00:10:18 -05001438 DMI_MATCH(DMI_PRODUCT_NAME, "dynabook"),
1439 },
1440 },
1441 {
Dmitry Torokhov9961e252009-12-04 10:24:20 -08001442 /* Toshiba Portege M300 */
Richard Thrippleton53a26702006-04-02 00:10:18 -05001443 .matches = {
1444 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1445 DMI_MATCH(DMI_PRODUCT_NAME, "PORTEGE M300"),
Simon Horman9ba5eaa2005-07-11 01:07:20 -05001446 },
Dmitry Torokhov5f5eeff2009-10-12 21:35:00 -07001447
1448 },
1449 {
Dmitry Torokhov9961e252009-12-04 10:24:20 -08001450 /* Toshiba Portege M300 */
Dmitry Torokhov5f5eeff2009-10-12 21:35:00 -07001451 .matches = {
1452 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1453 DMI_MATCH(DMI_PRODUCT_NAME, "Portable PC"),
1454 DMI_MATCH(DMI_PRODUCT_VERSION, "Version 1.0"),
1455 },
1456
Simon Horman9ba5eaa2005-07-11 01:07:20 -05001457 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458#endif
Jan Beulich70874862011-03-31 00:01:58 -07001459 { }
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001460};
1461
Andres Salomonef8313b2010-12-23 01:19:38 -08001462static bool broken_olpc_ec;
1463
1464static const struct dmi_system_id __initconst olpc_dmi_table[] = {
1465#if defined(CONFIG_DMI) && defined(CONFIG_OLPC)
1466 {
1467 /* OLPC XO-1 or XO-1.5 */
1468 .matches = {
1469 DMI_MATCH(DMI_SYS_VENDOR, "OLPC"),
1470 DMI_MATCH(DMI_PRODUCT_NAME, "XO"),
1471 },
1472 },
Andres Salomonef8313b2010-12-23 01:19:38 -08001473#endif
Jan Beulich70874862011-03-31 00:01:58 -07001474 { }
Andres Salomonef8313b2010-12-23 01:19:38 -08001475};
1476
Benjamin Tissoires969ba042014-03-28 00:43:00 -07001477static const struct dmi_system_id min_max_dmi_table[] __initconst = {
1478#if defined(CONFIG_DMI)
1479 {
1480 /* Lenovo ThinkPad Helix */
1481 .matches = {
1482 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1483 DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad Helix"),
1484 },
1485 .driver_data = (int []){1024, 5052, 2258, 4832},
1486 },
1487 {
Hans de Goede3fcaab02014-03-28 01:01:38 -07001488 /* Lenovo ThinkPad X240 */
1489 .matches = {
1490 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1491 DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad X240"),
1492 },
1493 .driver_data = (int []){1232, 5710, 1156, 4696},
1494 },
1495 {
Benjamin Tissoires969ba042014-03-28 00:43:00 -07001496 /* Lenovo ThinkPad T440s */
1497 .matches = {
1498 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1499 DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad T440"),
1500 },
1501 .driver_data = (int []){1024, 5112, 2024, 4832},
1502 },
1503 {
1504 /* Lenovo ThinkPad T540p */
1505 .matches = {
1506 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1507 DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad T540"),
1508 },
1509 .driver_data = (int []){1024, 5056, 2058, 4832},
1510 },
1511#endif
1512 { }
1513};
1514
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001515void __init synaptics_module_init(void)
1516{
Benjamin Tissoires969ba042014-03-28 00:43:00 -07001517 const struct dmi_system_id *min_max_dmi;
1518
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001519 impaired_toshiba_kbc = dmi_check_system(toshiba_dmi_table);
Andres Salomonef8313b2010-12-23 01:19:38 -08001520 broken_olpc_ec = dmi_check_system(olpc_dmi_table);
Benjamin Tissoires969ba042014-03-28 00:43:00 -07001521
1522 min_max_dmi = dmi_first_match(min_max_dmi_table);
1523 if (min_max_dmi)
1524 quirk_min_max = min_max_dmi->driver_data;
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001525}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
Daniel Drake7968a5d2011-11-08 00:00:35 -08001527static int __synaptics_init(struct psmouse *psmouse, bool absolute_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528{
1529 struct synaptics_data *priv;
Daniel Drake7968a5d2011-11-08 00:00:35 -08001530 int err = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
Andres Salomonef8313b2010-12-23 01:19:38 -08001532 /*
Daniel Drake83551c02011-11-11 16:05:04 -08001533 * The OLPC XO has issues with Synaptics' absolute mode; the constant
1534 * packet spew overloads the EC such that key presses on the keyboard
1535 * are missed. Given that, don't even attempt to use Absolute mode.
1536 * Relative mode seems to work just fine.
Andres Salomonef8313b2010-12-23 01:19:38 -08001537 */
Daniel Drake83551c02011-11-11 16:05:04 -08001538 if (absolute_mode && broken_olpc_ec) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001539 psmouse_info(psmouse,
1540 "OLPC XO detected, not enabling Synaptics protocol.\n");
Andres Salomonef8313b2010-12-23 01:19:38 -08001541 return -ENODEV;
1542 }
1543
Eric Sesterhennb39787a2006-03-14 00:09:16 -05001544 psmouse->private = priv = kzalloc(sizeof(struct synaptics_data), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 if (!priv)
Davidlohr Bueso6792cbb2010-09-29 18:53:35 -07001546 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
Andy Whitcroft4d368452009-02-28 12:51:01 -08001548 psmouse_reset(psmouse);
1549
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 if (synaptics_query_hardware(psmouse)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001551 psmouse_err(psmouse, "Unable to query device.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 goto init_fail;
1553 }
1554
Daniel Drake7968a5d2011-11-08 00:00:35 -08001555 priv->absolute_mode = absolute_mode;
1556 if (SYN_ID_DISGEST_SUPPORTED(priv->identity))
1557 priv->disable_gesture = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
Daniel Drake7968a5d2011-11-08 00:00:35 -08001559 if (synaptics_set_mode(psmouse)) {
1560 psmouse_err(psmouse, "Unable to initialize device.\n");
Henrik Rydbergfec6e522010-12-21 18:11:25 +01001561 goto init_fail;
1562 }
1563
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 priv->pkt_type = SYN_MODEL_NEWABS(priv->model_id) ? SYN_NEWABS : SYN_OLDABS;
1565
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001566 psmouse_info(psmouse,
1567 "Touchpad model: %ld, fw: %ld.%ld, id: %#lx, caps: %#lx/%#lx/%#lx\n",
1568 SYN_ID_MODEL(priv->identity),
1569 SYN_ID_MAJOR(priv->identity), SYN_ID_MINOR(priv->identity),
1570 priv->model_id,
1571 priv->capabilities, priv->ext_cap, priv->ext_cap_0c);
Dmitry Torokhov409b7502005-05-28 02:12:18 -05001572
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001573 set_input_params(psmouse->dev, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
Dmitry Torokhov887cc122007-04-12 01:30:41 -04001575 /*
1576 * Encode touchpad model so that it can be used to set
1577 * input device->id.version and be visible to userspace.
1578 * Because version is __u16 we have to drop something.
1579 * Hardware info bits seem to be good candidates as they
1580 * are documented to be for Synaptics corp. internal use.
1581 */
1582 psmouse->model = ((priv->model_id & 0x00ff0000) >> 8) |
1583 (priv->model_id & 0x000000ff);
1584
Daniel Drake7968a5d2011-11-08 00:00:35 -08001585 if (absolute_mode) {
1586 psmouse->protocol_handler = synaptics_process_byte;
1587 psmouse->pktsize = 6;
1588 } else {
1589 /* Relative mode follows standard PS/2 mouse protocol */
1590 psmouse->protocol_handler = psmouse_process_byte;
1591 psmouse->pktsize = 3;
1592 }
1593
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 psmouse->set_rate = synaptics_set_rate;
1595 psmouse->disconnect = synaptics_disconnect;
1596 psmouse->reconnect = synaptics_reconnect;
Dmitry Torokhova1cec062007-02-18 01:40:24 -05001597 psmouse->cleanup = synaptics_reset;
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001598 /* Synaptics can usually stay in sync without extra help */
1599 psmouse->resync_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
1601 if (SYN_CAP_PASS_THROUGH(priv->capabilities))
1602 synaptics_pt_create(psmouse);
1603
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 /*
1605 * Toshiba's KBC seems to have trouble handling data from
Andres Salomon7ee99162010-12-23 01:18:28 -08001606 * Synaptics at full rate. Switch to a lower rate (roughly
1607 * the same rate as a standard PS/2 mouse).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 */
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001609 if (psmouse->rate >= 80 && impaired_toshiba_kbc) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001610 psmouse_info(psmouse,
1611 "Toshiba %s detected, limiting rate to 40pps.\n",
1612 dmi_get_system_info(DMI_PRODUCT_NAME));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 psmouse->rate = 40;
1614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
Daniel Drake7968a5d2011-11-08 00:00:35 -08001616 if (!priv->absolute_mode && SYN_ID_DISGEST_SUPPORTED(priv->identity)) {
1617 err = device_create_file(&psmouse->ps2dev.serio->dev,
1618 &psmouse_attr_disable_gesture.dattr);
1619 if (err) {
1620 psmouse_err(psmouse,
1621 "Failed to create disable_gesture attribute (%d)",
1622 err);
1623 goto init_fail;
1624 }
1625 }
1626
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 return 0;
1628
1629 init_fail:
1630 kfree(priv);
Daniel Drake7968a5d2011-11-08 00:00:35 -08001631 return err;
1632}
1633
1634int synaptics_init(struct psmouse *psmouse)
1635{
1636 return __synaptics_init(psmouse, true);
1637}
1638
1639int synaptics_init_relative(struct psmouse *psmouse)
1640{
1641 return __synaptics_init(psmouse, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642}
1643
Daniel Drakee4e6efd2010-01-07 01:52:39 -08001644bool synaptics_supported(void)
1645{
1646 return true;
1647}
1648
Andres Salomon55e3d922007-03-10 01:39:54 -05001649#else /* CONFIG_MOUSE_PS2_SYNAPTICS */
1650
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001651void __init synaptics_module_init(void)
1652{
1653}
1654
Andres Salomon55e3d922007-03-10 01:39:54 -05001655int synaptics_init(struct psmouse *psmouse)
1656{
1657 return -ENOSYS;
1658}
1659
Daniel Drakee4e6efd2010-01-07 01:52:39 -08001660bool synaptics_supported(void)
1661{
1662 return false;
1663}
1664
Andres Salomon55e3d922007-03-10 01:39:54 -05001665#endif /* CONFIG_MOUSE_PS2_SYNAPTICS */