blob: c1f7e9b65088de60c12b1349e970c0e48a1e71da [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/*
56 * Any position values from the hardware above the following limits are
57 * treated as "wrapped around negative" values that have been truncated to
58 * the 13-bit reporting range of the hardware. These are just reasonable
59 * guesses and can be adjusted if hardware is found that operates outside
60 * of these parameters.
61 */
62#define X_MAX_POSITIVE (((1 << ABS_POS_BITS) + XMAX) / 2)
63#define Y_MAX_POSITIVE (((1 << ABS_POS_BITS) + YMAX) / 2)
64
Daniel Kurtz6de58dd2011-08-23 23:00:24 -070065/*
66 * Synaptics touchpads report the y coordinate from bottom to top, which is
67 * opposite from what userspace expects.
68 * This function is used to invert y before reporting.
69 */
70static int synaptics_invert_y(int y)
71{
72 return YMAX_NOMINAL + YMIN_NOMINAL - y;
73}
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Andres Salomon55e3d922007-03-10 01:39:54 -050076/*****************************************************************************
77 * Stuff we need even when we do not want native Synaptics support
78 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80/*
81 * Set the synaptics touchpad mode byte by special commands
82 */
83static int synaptics_mode_cmd(struct psmouse *psmouse, unsigned char mode)
84{
85 unsigned char param[1];
86
87 if (psmouse_sliced_command(psmouse, mode))
88 return -1;
89 param[0] = SYN_PS_SET_MODE2;
90 if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_SETRATE))
91 return -1;
92 return 0;
93}
94
Dmitry Torokhovb7802c52009-09-09 19:13:20 -070095int synaptics_detect(struct psmouse *psmouse, bool set_properties)
Andres Salomon55e3d922007-03-10 01:39:54 -050096{
97 struct ps2dev *ps2dev = &psmouse->ps2dev;
98 unsigned char param[4];
99
100 param[0] = 0;
101
102 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
103 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
104 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
105 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
106 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO);
107
108 if (param[1] != 0x47)
109 return -ENODEV;
110
111 if (set_properties) {
112 psmouse->vendor = "Synaptics";
113 psmouse->name = "TouchPad";
114 }
115
116 return 0;
117}
118
119void synaptics_reset(struct psmouse *psmouse)
120{
121 /* reset touchpad back to relative mode, gestures enabled */
122 synaptics_mode_cmd(psmouse, 0);
123}
124
125#ifdef CONFIG_MOUSE_PS2_SYNAPTICS
126
127/*****************************************************************************
128 * Synaptics communications functions
129 ****************************************************************************/
130
131/*
132 * Send a command to the synpatics touchpad by special commands
133 */
134static int synaptics_send_cmd(struct psmouse *psmouse, unsigned char c, unsigned char *param)
135{
136 if (psmouse_sliced_command(psmouse, c))
137 return -1;
138 if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_GETINFO))
139 return -1;
140 return 0;
141}
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143/*
144 * Read the model-id bytes from the touchpad
145 * see also SYN_MODEL_* macros
146 */
147static int synaptics_model_id(struct psmouse *psmouse)
148{
149 struct synaptics_data *priv = psmouse->private;
150 unsigned char mi[3];
151
152 if (synaptics_send_cmd(psmouse, SYN_QUE_MODEL, mi))
153 return -1;
154 priv->model_id = (mi[0]<<16) | (mi[1]<<8) | mi[2];
155 return 0;
156}
157
158/*
159 * Read the capability-bits from the touchpad
160 * see also the SYN_CAP_* macros
161 */
162static int synaptics_capability(struct psmouse *psmouse)
163{
164 struct synaptics_data *priv = psmouse->private;
165 unsigned char cap[3];
166
167 if (synaptics_send_cmd(psmouse, SYN_QUE_CAPABILITIES, cap))
168 return -1;
169 priv->capabilities = (cap[0] << 16) | (cap[1] << 8) | cap[2];
Takashi Iwai5f57d672010-04-19 10:37:21 -0700170 priv->ext_cap = priv->ext_cap_0c = 0;
171
Dmitry Torokhov3619b8f2010-07-21 00:01:19 -0700172 /*
173 * Older firmwares had submodel ID fixed to 0x47
174 */
175 if (SYN_ID_FULL(priv->identity) < 0x705 &&
176 SYN_CAP_SUBMODEL_ID(priv->capabilities) != 0x47) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 return -1;
Dmitry Torokhov3619b8f2010-07-21 00:01:19 -0700178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 /*
181 * Unless capExtended is set the rest of the flags should be ignored
182 */
183 if (!SYN_CAP_EXTENDED(priv->capabilities))
184 priv->capabilities = 0;
185
186 if (SYN_EXT_CAP_REQUESTS(priv->capabilities) >= 1) {
187 if (synaptics_send_cmd(psmouse, SYN_QUE_EXT_CAPAB, cap)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700188 psmouse_warn(psmouse,
189 "device claims to have extended capabilities, but I'm not able to read them.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 } else {
191 priv->ext_cap = (cap[0] << 16) | (cap[1] << 8) | cap[2];
192
193 /*
194 * if nExtBtn is greater than 8 it should be considered
195 * invalid and treated as 0
196 */
197 if (SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap) > 8)
198 priv->ext_cap &= 0xff0fff;
199 }
200 }
Takashi Iwai5f57d672010-04-19 10:37:21 -0700201
202 if (SYN_EXT_CAP_REQUESTS(priv->capabilities) >= 4) {
203 if (synaptics_send_cmd(psmouse, SYN_QUE_EXT_CAPAB_0C, cap)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700204 psmouse_warn(psmouse,
205 "device claims to have extended capability 0x0c, but I'm not able to read it.\n");
Takashi Iwai5f57d672010-04-19 10:37:21 -0700206 } else {
207 priv->ext_cap_0c = (cap[0] << 16) | (cap[1] << 8) | cap[2];
208 }
209 }
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 return 0;
212}
213
214/*
215 * Identify Touchpad
216 * See also the SYN_ID_* macros
217 */
218static int synaptics_identify(struct psmouse *psmouse)
219{
220 struct synaptics_data *priv = psmouse->private;
221 unsigned char id[3];
222
223 if (synaptics_send_cmd(psmouse, SYN_QUE_IDENTIFY, id))
224 return -1;
225 priv->identity = (id[0]<<16) | (id[1]<<8) | id[2];
226 if (SYN_ID_IS_SYNAPTICS(priv->identity))
227 return 0;
228 return -1;
229}
230
Tero Saarniec20a022009-06-10 23:27:24 -0700231/*
Dmitry Torokhov83ba9ea2010-05-10 23:06:52 -0700232 * Read touchpad resolution and maximum reported coordinates
Tero Saarniec20a022009-06-10 23:27:24 -0700233 * Resolution is left zero if touchpad does not support the query
234 */
Benjamin Tissoires969ba042014-03-28 00:43:00 -0700235
236static const int *quirk_min_max;
237
Tero Saarniec20a022009-06-10 23:27:24 -0700238static int synaptics_resolution(struct psmouse *psmouse)
239{
240 struct synaptics_data *priv = psmouse->private;
Dmitry Torokhova66413f2011-07-09 12:32:56 -0700241 unsigned char resp[3];
Tero Saarniec20a022009-06-10 23:27:24 -0700242
Benjamin Tissoires969ba042014-03-28 00:43:00 -0700243 if (quirk_min_max) {
244 priv->x_min = quirk_min_max[0];
245 priv->x_max = quirk_min_max[1];
246 priv->y_min = quirk_min_max[2];
247 priv->y_max = quirk_min_max[3];
248 return 0;
249 }
250
Tero Saarniec20a022009-06-10 23:27:24 -0700251 if (SYN_ID_MAJOR(priv->identity) < 4)
Takashi Iwaibbddd192010-07-14 09:32:46 -0700252 return 0;
Tero Saarniec20a022009-06-10 23:27:24 -0700253
Dmitry Torokhova66413f2011-07-09 12:32:56 -0700254 if (synaptics_send_cmd(psmouse, SYN_QUE_RESOLUTION, resp) == 0) {
255 if (resp[0] != 0 && (resp[1] & 0x80) && resp[2] != 0) {
256 priv->x_res = resp[0]; /* x resolution in units/mm */
257 priv->y_res = resp[2]; /* y resolution in units/mm */
Dmitry Torokhov83ba9ea2010-05-10 23:06:52 -0700258 }
259 }
Tero Saarniec20a022009-06-10 23:27:24 -0700260
Dmitry Torokhov83ba9ea2010-05-10 23:06:52 -0700261 if (SYN_EXT_CAP_REQUESTS(priv->capabilities) >= 5 &&
262 SYN_CAP_MAX_DIMENSIONS(priv->ext_cap_0c)) {
Dmitry Torokhova66413f2011-07-09 12:32:56 -0700263 if (synaptics_send_cmd(psmouse, SYN_QUE_EXT_MAX_COORDS, resp)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700264 psmouse_warn(psmouse,
265 "device claims to have max coordinates query, but I'm not able to read it.\n");
Dmitry Torokhov83ba9ea2010-05-10 23:06:52 -0700266 } else {
Dmitry Torokhova66413f2011-07-09 12:32:56 -0700267 priv->x_max = (resp[0] << 5) | ((resp[1] & 0x0f) << 1);
268 priv->y_max = (resp[2] << 5) | ((resp[1] & 0xf0) >> 3);
269 }
270 }
271
272 if (SYN_EXT_CAP_REQUESTS(priv->capabilities) >= 7 &&
273 SYN_CAP_MIN_DIMENSIONS(priv->ext_cap_0c)) {
274 if (synaptics_send_cmd(psmouse, SYN_QUE_EXT_MIN_COORDS, resp)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700275 psmouse_warn(psmouse,
276 "device claims to have min coordinates query, but I'm not able to read it.\n");
Dmitry Torokhova66413f2011-07-09 12:32:56 -0700277 } else {
278 priv->x_min = (resp[0] << 5) | ((resp[1] & 0x0f) << 1);
279 priv->y_min = (resp[2] << 5) | ((resp[1] & 0xf0) >> 3);
Dmitry Torokhov83ba9ea2010-05-10 23:06:52 -0700280 }
Tero Saarniec20a022009-06-10 23:27:24 -0700281 }
282
283 return 0;
284}
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286static int synaptics_query_hardware(struct psmouse *psmouse)
287{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 if (synaptics_identify(psmouse))
289 return -1;
290 if (synaptics_model_id(psmouse))
291 return -1;
292 if (synaptics_capability(psmouse))
293 return -1;
Tero Saarniec20a022009-06-10 23:27:24 -0700294 if (synaptics_resolution(psmouse))
295 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297 return 0;
298}
299
Daniel Drake7968a5d2011-11-08 00:00:35 -0800300static int synaptics_set_advanced_gesture_mode(struct psmouse *psmouse)
301{
302 static unsigned char param = 0xc8;
303 struct synaptics_data *priv = psmouse->private;
304
Benjamin Herrenschmidt899c6122012-04-20 22:34:49 -0700305 if (!(SYN_CAP_ADV_GESTURE(priv->ext_cap_0c) ||
306 SYN_CAP_IMAGE_SENSOR(priv->ext_cap_0c)))
Daniel Drake7968a5d2011-11-08 00:00:35 -0800307 return 0;
308
309 if (psmouse_sliced_command(psmouse, SYN_QUE_MODEL))
310 return -1;
311
312 if (ps2_command(&psmouse->ps2dev, &param, PSMOUSE_CMD_SETRATE))
313 return -1;
314
315 /* Advanced gesture mode also sends multi finger data */
316 priv->capabilities |= BIT(1);
317
318 return 0;
319}
320
321static int synaptics_set_mode(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
323 struct synaptics_data *priv = psmouse->private;
324
Daniel Drake7968a5d2011-11-08 00:00:35 -0800325 priv->mode = 0;
326 if (priv->absolute_mode)
327 priv->mode |= SYN_BIT_ABSOLUTE_MODE;
328 if (priv->disable_gesture)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 priv->mode |= SYN_BIT_DISABLE_GESTURE;
Daniel Drake7968a5d2011-11-08 00:00:35 -0800330 if (psmouse->rate >= 80)
331 priv->mode |= SYN_BIT_HIGH_RATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 if (SYN_CAP_EXTENDED(priv->capabilities))
333 priv->mode |= SYN_BIT_W_MODE;
334
335 if (synaptics_mode_cmd(psmouse, priv->mode))
336 return -1;
337
Daniel Drake7968a5d2011-11-08 00:00:35 -0800338 if (priv->absolute_mode &&
339 synaptics_set_advanced_gesture_mode(psmouse)) {
340 psmouse_err(psmouse, "Advanced gesture mode init failed.\n");
341 return -1;
342 }
343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 return 0;
345}
346
347static void synaptics_set_rate(struct psmouse *psmouse, unsigned int rate)
348{
349 struct synaptics_data *priv = psmouse->private;
350
351 if (rate >= 80) {
352 priv->mode |= SYN_BIT_HIGH_RATE;
353 psmouse->rate = 80;
354 } else {
355 priv->mode &= ~SYN_BIT_HIGH_RATE;
356 psmouse->rate = 40;
357 }
358
359 synaptics_mode_cmd(psmouse, priv->mode);
360}
361
362/*****************************************************************************
363 * Synaptics pass-through PS/2 port support
364 ****************************************************************************/
365static int synaptics_pt_write(struct serio *serio, unsigned char c)
366{
367 struct psmouse *parent = serio_get_drvdata(serio->parent);
368 char rate_param = SYN_PS_CLIENT_CMD; /* indicates that we want pass-through port */
369
370 if (psmouse_sliced_command(parent, c))
371 return -1;
372 if (ps2_command(&parent->ps2dev, &rate_param, PSMOUSE_CMD_SETRATE))
373 return -1;
374 return 0;
375}
376
Dmitry Torokhova8b3c0f2010-10-04 21:46:10 -0700377static int synaptics_pt_start(struct serio *serio)
378{
379 struct psmouse *parent = serio_get_drvdata(serio->parent);
380 struct synaptics_data *priv = parent->private;
381
382 serio_pause_rx(parent->ps2dev.serio);
383 priv->pt_port = serio;
384 serio_continue_rx(parent->ps2dev.serio);
385
386 return 0;
387}
388
389static void synaptics_pt_stop(struct serio *serio)
390{
391 struct psmouse *parent = serio_get_drvdata(serio->parent);
392 struct synaptics_data *priv = parent->private;
393
394 serio_pause_rx(parent->ps2dev.serio);
395 priv->pt_port = NULL;
396 serio_continue_rx(parent->ps2dev.serio);
397}
398
399static int synaptics_is_pt_packet(unsigned char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
401 return (buf[0] & 0xFC) == 0x84 && (buf[3] & 0xCC) == 0xC4;
402}
403
404static void synaptics_pass_pt_packet(struct serio *ptport, unsigned char *packet)
405{
406 struct psmouse *child = serio_get_drvdata(ptport);
407
408 if (child && child->state == PSMOUSE_ACTIVATED) {
David Howells7d12e782006-10-05 14:55:46 +0100409 serio_interrupt(ptport, packet[1], 0);
410 serio_interrupt(ptport, packet[4], 0);
411 serio_interrupt(ptport, packet[5], 0);
Sergey Vlasov33fdfa92005-07-24 00:53:32 -0500412 if (child->pktsize == 4)
David Howells7d12e782006-10-05 14:55:46 +0100413 serio_interrupt(ptport, packet[2], 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 } else
David Howells7d12e782006-10-05 14:55:46 +0100415 serio_interrupt(ptport, packet[1], 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416}
417
418static void synaptics_pt_activate(struct psmouse *psmouse)
419{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 struct synaptics_data *priv = psmouse->private;
Dmitry Torokhova8b3c0f2010-10-04 21:46:10 -0700421 struct psmouse *child = serio_get_drvdata(priv->pt_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
423 /* adjust the touchpad to child's choice of protocol */
424 if (child) {
Sergey Vlasov33fdfa92005-07-24 00:53:32 -0500425 if (child->pktsize == 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 priv->mode |= SYN_BIT_FOUR_BYTE_CLIENT;
427 else
428 priv->mode &= ~SYN_BIT_FOUR_BYTE_CLIENT;
429
430 if (synaptics_mode_cmd(psmouse, priv->mode))
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700431 psmouse_warn(psmouse,
432 "failed to switch guest protocol\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 }
434}
435
436static void synaptics_pt_create(struct psmouse *psmouse)
437{
438 struct serio *serio;
439
Eric Sesterhennb39787a2006-03-14 00:09:16 -0500440 serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 if (!serio) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700442 psmouse_err(psmouse,
443 "not enough memory for pass-through port\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 return;
445 }
446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 serio->id.type = SERIO_PS_PSTHRU;
448 strlcpy(serio->name, "Synaptics pass-through", sizeof(serio->name));
449 strlcpy(serio->phys, "synaptics-pt/serio0", sizeof(serio->name));
450 serio->write = synaptics_pt_write;
Dmitry Torokhova8b3c0f2010-10-04 21:46:10 -0700451 serio->start = synaptics_pt_start;
452 serio->stop = synaptics_pt_stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 serio->parent = psmouse->ps2dev.serio;
454
455 psmouse->pt_activate = synaptics_pt_activate;
456
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700457 psmouse_info(psmouse, "serio: %s port at %s\n",
458 serio->name, psmouse->phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 serio_register_port(serio);
460}
461
462/*****************************************************************************
463 * Functions to interpret the absolute mode packets
464 ****************************************************************************/
465
Daniel Kurtza6ca40c2011-08-23 23:02:31 -0700466static void synaptics_mt_state_set(struct synaptics_mt_state *state, int count,
467 int sgm, int agm)
468{
469 state->count = count;
470 state->sgm = sgm;
471 state->agm = agm;
472}
473
Daniel Kurtz7afdb842011-08-23 23:00:33 -0700474static void synaptics_parse_agm(const unsigned char buf[],
Daniel Kurtza6ca40c2011-08-23 23:02:31 -0700475 struct synaptics_data *priv,
476 struct synaptics_hw_state *hw)
Daniel Kurtz7afdb842011-08-23 23:00:33 -0700477{
478 struct synaptics_hw_state *agm = &priv->agm;
Daniel Kurtza6ca40c2011-08-23 23:02:31 -0700479 int agm_packet_type;
Daniel Kurtz7afdb842011-08-23 23:00:33 -0700480
Daniel Kurtza6ca40c2011-08-23 23:02:31 -0700481 agm_packet_type = (buf[5] & 0x30) >> 4;
482 switch (agm_packet_type) {
483 case 1:
484 /* Gesture packet: (x, y, z) half resolution */
485 agm->w = hw->w;
486 agm->x = (((buf[4] & 0x0f) << 8) | buf[1]) << 1;
487 agm->y = (((buf[4] & 0xf0) << 4) | buf[2]) << 1;
488 agm->z = ((buf[3] & 0x30) | (buf[5] & 0x0f)) << 1;
489 break;
490
491 case 2:
492 /* AGM-CONTACT packet: (count, sgm, agm) */
493 synaptics_mt_state_set(&agm->mt_state, buf[1], buf[2], buf[4]);
494 break;
495
496 default:
497 break;
498 }
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700499
500 /* Record that at least one AGM has been received since last SGM */
501 priv->agm_pending = true;
Daniel Kurtz7afdb842011-08-23 23:00:33 -0700502}
503
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100504static int synaptics_parse_hw_state(const unsigned char buf[],
505 struct synaptics_data *priv,
506 struct synaptics_hw_state *hw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
508 memset(hw, 0, sizeof(struct synaptics_hw_state));
509
510 if (SYN_MODEL_NEWABS(priv->model_id)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 hw->w = (((buf[0] & 0x30) >> 2) |
512 ((buf[0] & 0x04) >> 1) |
513 ((buf[3] & 0x04) >> 2));
514
515 hw->left = (buf[0] & 0x01) ? 1 : 0;
516 hw->right = (buf[0] & 0x02) ? 1 : 0;
517
Takashi Iwai5f57d672010-04-19 10:37:21 -0700518 if (SYN_CAP_CLICKPAD(priv->ext_cap_0c)) {
519 /*
520 * Clickpad's button is transmitted as middle button,
521 * however, since it is primary button, we will report
522 * it as BTN_LEFT.
523 */
524 hw->left = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0;
525
526 } else if (SYN_CAP_MIDDLE_BUTTON(priv->capabilities)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 hw->middle = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0;
528 if (hw->w == 2)
529 hw->scroll = (signed char)(buf[1]);
530 }
531
532 if (SYN_CAP_FOUR_BUTTON(priv->capabilities)) {
533 hw->up = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0;
534 hw->down = ((buf[0] ^ buf[3]) & 0x02) ? 1 : 0;
535 }
536
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700537 if ((SYN_CAP_ADV_GESTURE(priv->ext_cap_0c) ||
538 SYN_CAP_IMAGE_SENSOR(priv->ext_cap_0c)) &&
539 hw->w == 2) {
Daniel Kurtza6ca40c2011-08-23 23:02:31 -0700540 synaptics_parse_agm(buf, priv, hw);
Daniel Kurtz28d5fd82011-07-06 22:57:39 -0700541 return 1;
542 }
543
544 hw->x = (((buf[3] & 0x10) << 8) |
545 ((buf[1] & 0x0f) << 8) |
546 buf[4]);
547 hw->y = (((buf[3] & 0x20) << 7) |
548 ((buf[1] & 0xf0) << 4) |
549 buf[5]);
550 hw->z = buf[2];
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 if (SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap) &&
553 ((buf[0] ^ buf[3]) & 0x02)) {
554 switch (SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap) & ~0x01) {
555 default:
556 /*
557 * if nExtBtn is greater than 8 it should be
558 * considered invalid and treated as 0
559 */
560 break;
561 case 8:
562 hw->ext_buttons |= ((buf[5] & 0x08)) ? 0x80 : 0;
563 hw->ext_buttons |= ((buf[4] & 0x08)) ? 0x40 : 0;
564 case 6:
565 hw->ext_buttons |= ((buf[5] & 0x04)) ? 0x20 : 0;
566 hw->ext_buttons |= ((buf[4] & 0x04)) ? 0x10 : 0;
567 case 4:
568 hw->ext_buttons |= ((buf[5] & 0x02)) ? 0x08 : 0;
569 hw->ext_buttons |= ((buf[4] & 0x02)) ? 0x04 : 0;
570 case 2:
571 hw->ext_buttons |= ((buf[5] & 0x01)) ? 0x02 : 0;
572 hw->ext_buttons |= ((buf[4] & 0x01)) ? 0x01 : 0;
573 }
574 }
575 } else {
576 hw->x = (((buf[1] & 0x1f) << 8) | buf[2]);
577 hw->y = (((buf[4] & 0x1f) << 8) | buf[5]);
578
579 hw->z = (((buf[0] & 0x30) << 2) | (buf[3] & 0x3F));
580 hw->w = (((buf[1] & 0x80) >> 4) | ((buf[0] & 0x04) >> 1));
581
582 hw->left = (buf[0] & 0x01) ? 1 : 0;
583 hw->right = (buf[0] & 0x02) ? 1 : 0;
584 }
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100585
Seth Forsheeba37c702012-07-24 23:54:11 -0700586 /* Convert wrap-around values to negative */
587 if (hw->x > X_MAX_POSITIVE)
588 hw->x -= 1 << ABS_POS_BITS;
589 if (hw->y > Y_MAX_POSITIVE)
590 hw->y -= 1 << ABS_POS_BITS;
591
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100592 return 0;
593}
594
Daniel Kurtzbea9f0f2011-07-06 22:42:52 -0700595static void synaptics_report_semi_mt_slot(struct input_dev *dev, int slot,
596 bool active, int x, int y)
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100597{
598 input_mt_slot(dev, slot);
599 input_mt_report_slot_state(dev, MT_TOOL_FINGER, active);
600 if (active) {
601 input_report_abs(dev, ABS_MT_POSITION_X, x);
Daniel Kurtz6de58dd2011-08-23 23:00:24 -0700602 input_report_abs(dev, ABS_MT_POSITION_Y, synaptics_invert_y(y));
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100603 }
604}
605
606static void synaptics_report_semi_mt_data(struct input_dev *dev,
607 const struct synaptics_hw_state *a,
608 const struct synaptics_hw_state *b,
609 int num_fingers)
610{
611 if (num_fingers >= 2) {
Daniel Kurtzbea9f0f2011-07-06 22:42:52 -0700612 synaptics_report_semi_mt_slot(dev, 0, true, min(a->x, b->x),
613 min(a->y, b->y));
614 synaptics_report_semi_mt_slot(dev, 1, true, max(a->x, b->x),
615 max(a->y, b->y));
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100616 } else if (num_fingers == 1) {
Daniel Kurtzbea9f0f2011-07-06 22:42:52 -0700617 synaptics_report_semi_mt_slot(dev, 0, true, a->x, a->y);
618 synaptics_report_semi_mt_slot(dev, 1, false, 0, 0);
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100619 } else {
Daniel Kurtzbea9f0f2011-07-06 22:42:52 -0700620 synaptics_report_semi_mt_slot(dev, 0, false, 0, 0);
621 synaptics_report_semi_mt_slot(dev, 1, false, 0, 0);
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100622 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623}
624
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700625static void synaptics_report_buttons(struct psmouse *psmouse,
626 const struct synaptics_hw_state *hw)
627{
628 struct input_dev *dev = psmouse->dev;
629 struct synaptics_data *priv = psmouse->private;
630 int i;
631
632 input_report_key(dev, BTN_LEFT, hw->left);
633 input_report_key(dev, BTN_RIGHT, hw->right);
634
635 if (SYN_CAP_MIDDLE_BUTTON(priv->capabilities))
636 input_report_key(dev, BTN_MIDDLE, hw->middle);
637
638 if (SYN_CAP_FOUR_BUTTON(priv->capabilities)) {
639 input_report_key(dev, BTN_FORWARD, hw->up);
640 input_report_key(dev, BTN_BACK, hw->down);
641 }
642
643 for (i = 0; i < SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap); i++)
644 input_report_key(dev, BTN_0 + i, hw->ext_buttons & (1 << i));
645}
646
647static void synaptics_report_slot(struct input_dev *dev, int slot,
648 const struct synaptics_hw_state *hw)
649{
650 input_mt_slot(dev, slot);
651 input_mt_report_slot_state(dev, MT_TOOL_FINGER, (hw != NULL));
652 if (!hw)
653 return;
654
655 input_report_abs(dev, ABS_MT_POSITION_X, hw->x);
656 input_report_abs(dev, ABS_MT_POSITION_Y, synaptics_invert_y(hw->y));
657 input_report_abs(dev, ABS_MT_PRESSURE, hw->z);
658}
659
660static void synaptics_report_mt_data(struct psmouse *psmouse,
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700661 struct synaptics_mt_state *mt_state,
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700662 const struct synaptics_hw_state *sgm)
663{
664 struct input_dev *dev = psmouse->dev;
665 struct synaptics_data *priv = psmouse->private;
666 struct synaptics_hw_state *agm = &priv->agm;
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700667 struct synaptics_mt_state *old = &priv->mt_state;
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700668
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700669 switch (mt_state->count) {
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700670 case 0:
671 synaptics_report_slot(dev, 0, NULL);
672 synaptics_report_slot(dev, 1, NULL);
673 break;
674 case 1:
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700675 if (mt_state->sgm == -1) {
676 synaptics_report_slot(dev, 0, NULL);
677 synaptics_report_slot(dev, 1, NULL);
678 } else if (mt_state->sgm == 0) {
679 synaptics_report_slot(dev, 0, sgm);
680 synaptics_report_slot(dev, 1, NULL);
681 } else {
682 synaptics_report_slot(dev, 0, NULL);
683 synaptics_report_slot(dev, 1, sgm);
684 }
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700685 break;
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700686 default:
687 /*
688 * If the finger slot contained in SGM is valid, and either
689 * hasn't changed, or is new, then report SGM in MTB slot 0.
690 * Otherwise, empty MTB slot 0.
691 */
692 if (mt_state->sgm != -1 &&
693 (mt_state->sgm == old->sgm || old->sgm == -1))
694 synaptics_report_slot(dev, 0, sgm);
695 else
696 synaptics_report_slot(dev, 0, NULL);
697
698 /*
699 * If the finger slot contained in AGM is valid, and either
700 * hasn't changed, or is new, then report AGM in MTB slot 1.
701 * Otherwise, empty MTB slot 1.
702 */
703 if (mt_state->agm != -1 &&
704 (mt_state->agm == old->agm || old->agm == -1))
705 synaptics_report_slot(dev, 1, agm);
706 else
707 synaptics_report_slot(dev, 1, NULL);
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700708 break;
709 }
710
711 /* Don't use active slot count to generate BTN_TOOL events. */
712 input_mt_report_pointer_emulation(dev, false);
713
714 /* Send the number of fingers reported by touchpad itself. */
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700715 input_mt_report_finger_count(dev, mt_state->count);
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700716
717 synaptics_report_buttons(psmouse, sgm);
718
719 input_sync(dev);
720}
721
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700722/* Handle case where mt_state->count = 0 */
723static void synaptics_image_sensor_0f(struct synaptics_data *priv,
724 struct synaptics_mt_state *mt_state)
725{
726 synaptics_mt_state_set(mt_state, 0, -1, -1);
727 priv->mt_state_lost = false;
728}
729
730/* Handle case where mt_state->count = 1 */
731static void synaptics_image_sensor_1f(struct synaptics_data *priv,
732 struct synaptics_mt_state *mt_state)
733{
734 struct synaptics_hw_state *agm = &priv->agm;
735 struct synaptics_mt_state *old = &priv->mt_state;
736
737 /*
738 * If the last AGM was (0,0,0), and there is only one finger left,
739 * then we absolutely know that SGM contains slot 0, and all other
740 * fingers have been removed.
741 */
742 if (priv->agm_pending && agm->z == 0) {
743 synaptics_mt_state_set(mt_state, 1, 0, -1);
744 priv->mt_state_lost = false;
745 return;
746 }
747
748 switch (old->count) {
749 case 0:
750 synaptics_mt_state_set(mt_state, 1, 0, -1);
751 break;
752 case 1:
753 /*
754 * If mt_state_lost, then the previous transition was 3->1,
755 * and SGM now contains either slot 0 or 1, but we don't know
756 * which. So, we just assume that the SGM now contains slot 1.
757 *
758 * If pending AGM and either:
759 * (a) the previous SGM slot contains slot 0, or
760 * (b) there was no SGM slot
761 * then, the SGM now contains slot 1
762 *
763 * Case (a) happens with very rapid "drum roll" gestures, where
764 * slot 0 finger is lifted and a new slot 1 finger touches
765 * within one reporting interval.
766 *
767 * Case (b) happens if initially two or more fingers tap
768 * briefly, and all but one lift before the end of the first
769 * reporting interval.
770 *
771 * (In both these cases, slot 0 will becomes empty, so SGM
772 * contains slot 1 with the new finger)
773 *
774 * Else, if there was no previous SGM, it now contains slot 0.
775 *
776 * Otherwise, SGM still contains the same slot.
777 */
778 if (priv->mt_state_lost ||
779 (priv->agm_pending && old->sgm <= 0))
780 synaptics_mt_state_set(mt_state, 1, 1, -1);
781 else if (old->sgm == -1)
782 synaptics_mt_state_set(mt_state, 1, 0, -1);
783 break;
784 case 2:
785 /*
786 * If mt_state_lost, we don't know which finger SGM contains.
787 *
788 * So, report 1 finger, but with both slots empty.
789 * We will use slot 1 on subsequent 1->1
790 */
791 if (priv->mt_state_lost) {
792 synaptics_mt_state_set(mt_state, 1, -1, -1);
793 break;
794 }
795 /*
796 * Since the last AGM was NOT (0,0,0), it was the finger in
797 * slot 0 that has been removed.
798 * So, SGM now contains previous AGM's slot, and AGM is now
799 * empty.
800 */
801 synaptics_mt_state_set(mt_state, 1, old->agm, -1);
802 break;
803 case 3:
804 /*
805 * Since last AGM was not (0,0,0), we don't know which finger
806 * is left.
807 *
808 * So, report 1 finger, but with both slots empty.
809 * We will use slot 1 on subsequent 1->1
810 */
811 synaptics_mt_state_set(mt_state, 1, -1, -1);
812 priv->mt_state_lost = true;
813 break;
Daniel Kurtz6b4b49f2011-08-23 23:02:56 -0700814 case 4:
815 case 5:
816 /* mt_state was updated by AGM-CONTACT packet */
817 break;
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700818 }
819}
820
821/* Handle case where mt_state->count = 2 */
822static void synaptics_image_sensor_2f(struct synaptics_data *priv,
823 struct synaptics_mt_state *mt_state)
824{
825 struct synaptics_mt_state *old = &priv->mt_state;
826
827 switch (old->count) {
828 case 0:
829 synaptics_mt_state_set(mt_state, 2, 0, 1);
830 break;
831 case 1:
832 /*
833 * If previous SGM contained slot 1 or higher, SGM now contains
834 * slot 0 (the newly touching finger) and AGM contains SGM's
835 * previous slot.
836 *
837 * Otherwise, SGM still contains slot 0 and AGM now contains
838 * slot 1.
839 */
840 if (old->sgm >= 1)
841 synaptics_mt_state_set(mt_state, 2, 0, old->sgm);
842 else
843 synaptics_mt_state_set(mt_state, 2, 0, 1);
844 break;
845 case 2:
846 /*
847 * If mt_state_lost, SGM now contains either finger 1 or 2, but
848 * we don't know which.
849 * So, we just assume that the SGM contains slot 0 and AGM 1.
850 */
851 if (priv->mt_state_lost)
852 synaptics_mt_state_set(mt_state, 2, 0, 1);
853 /*
854 * Otherwise, use the same mt_state, since it either hasn't
855 * changed, or was updated by a recently received AGM-CONTACT
856 * packet.
857 */
858 break;
859 case 3:
860 /*
861 * 3->2 transitions have two unsolvable problems:
862 * 1) no indication is given which finger was removed
863 * 2) no way to tell if agm packet was for finger 3
864 * before 3->2, or finger 2 after 3->2.
865 *
866 * So, report 2 fingers, but empty all slots.
867 * We will guess slots [0,1] on subsequent 2->2.
868 */
869 synaptics_mt_state_set(mt_state, 2, -1, -1);
870 priv->mt_state_lost = true;
871 break;
Daniel Kurtz6b4b49f2011-08-23 23:02:56 -0700872 case 4:
873 case 5:
874 /* mt_state was updated by AGM-CONTACT packet */
875 break;
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700876 }
877}
878
879/* Handle case where mt_state->count = 3 */
880static void synaptics_image_sensor_3f(struct synaptics_data *priv,
881 struct synaptics_mt_state *mt_state)
882{
883 struct synaptics_mt_state *old = &priv->mt_state;
884
885 switch (old->count) {
886 case 0:
887 synaptics_mt_state_set(mt_state, 3, 0, 2);
888 break;
889 case 1:
890 /*
891 * If previous SGM contained slot 2 or higher, SGM now contains
892 * slot 0 (one of the newly touching fingers) and AGM contains
893 * SGM's previous slot.
894 *
895 * Otherwise, SGM now contains slot 0 and AGM contains slot 2.
896 */
897 if (old->sgm >= 2)
898 synaptics_mt_state_set(mt_state, 3, 0, old->sgm);
899 else
900 synaptics_mt_state_set(mt_state, 3, 0, 2);
901 break;
902 case 2:
903 /*
Daniel Kurtz6b4b49f2011-08-23 23:02:56 -0700904 * If the AGM previously contained slot 3 or higher, then the
905 * newly touching finger is in the lowest available slot.
906 *
907 * If SGM was previously 1 or higher, then the new SGM is
908 * now slot 0 (with a new finger), otherwise, the new finger
909 * is now in a hidden slot between 0 and AGM's slot.
910 *
911 * In all such cases, the SGM now contains slot 0, and the AGM
912 * continues to contain the same slot as before.
913 */
914 if (old->agm >= 3) {
915 synaptics_mt_state_set(mt_state, 3, 0, old->agm);
916 break;
917 }
918
919 /*
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700920 * After some 3->1 and all 3->2 transitions, we lose track
921 * of which slot is reported by SGM and AGM.
922 *
923 * For 2->3 in this state, report 3 fingers, but empty all
924 * slots, and we will guess (0,2) on a subsequent 0->3.
925 *
926 * To userspace, the resulting transition will look like:
927 * 2:[0,1] -> 3:[-1,-1] -> 3:[0,2]
928 */
929 if (priv->mt_state_lost) {
930 synaptics_mt_state_set(mt_state, 3, -1, -1);
931 break;
932 }
933
934 /*
935 * If the (SGM,AGM) really previously contained slots (0, 1),
936 * then we cannot know what slot was just reported by the AGM,
937 * because the 2->3 transition can occur either before or after
938 * the AGM packet. Thus, this most recent AGM could contain
939 * either the same old slot 1 or the new slot 2.
940 * Subsequent AGMs will be reporting slot 2.
941 *
942 * To userspace, the resulting transition will look like:
943 * 2:[0,1] -> 3:[0,-1] -> 3:[0,2]
944 */
945 synaptics_mt_state_set(mt_state, 3, 0, -1);
946 break;
947 case 3:
948 /*
949 * If, for whatever reason, the previous agm was invalid,
950 * Assume SGM now contains slot 0, AGM now contains slot 2.
951 */
952 if (old->agm <= 2)
953 synaptics_mt_state_set(mt_state, 3, 0, 2);
954 /*
955 * mt_state either hasn't changed, or was updated by a recently
956 * received AGM-CONTACT packet.
957 */
958 break;
Daniel Kurtz6b4b49f2011-08-23 23:02:56 -0700959
960 case 4:
961 case 5:
962 /* mt_state was updated by AGM-CONTACT packet */
963 break;
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700964 }
965}
966
Daniel Kurtz6b4b49f2011-08-23 23:02:56 -0700967/* Handle case where mt_state->count = 4, or = 5 */
968static void synaptics_image_sensor_45f(struct synaptics_data *priv,
969 struct synaptics_mt_state *mt_state)
970{
971 /* mt_state was updated correctly by AGM-CONTACT packet */
972 priv->mt_state_lost = false;
973}
974
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700975static void synaptics_image_sensor_process(struct psmouse *psmouse,
976 struct synaptics_hw_state *sgm)
977{
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700978 struct synaptics_data *priv = psmouse->private;
979 struct synaptics_hw_state *agm = &priv->agm;
980 struct synaptics_mt_state mt_state;
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700981
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700982 /* Initialize using current mt_state (as updated by last agm) */
983 mt_state = agm->mt_state;
984
985 /*
986 * Update mt_state using the new finger count and current mt_state.
987 */
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700988 if (sgm->z == 0)
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700989 synaptics_image_sensor_0f(priv, &mt_state);
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700990 else if (sgm->w >= 4)
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700991 synaptics_image_sensor_1f(priv, &mt_state);
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700992 else if (sgm->w == 0)
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700993 synaptics_image_sensor_2f(priv, &mt_state);
Daniel Kurtz6b4b49f2011-08-23 23:02:56 -0700994 else if (sgm->w == 1 && mt_state.count <= 3)
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700995 synaptics_image_sensor_3f(priv, &mt_state);
Daniel Kurtz6b4b49f2011-08-23 23:02:56 -0700996 else
997 synaptics_image_sensor_45f(priv, &mt_state);
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700998
999 /* Send resulting input events to user space */
Daniel Kurtz4dc772d2011-08-23 23:02:40 -07001000 synaptics_report_mt_data(psmouse, &mt_state, sgm);
1001
1002 /* Store updated mt_state */
1003 priv->mt_state = agm->mt_state = mt_state;
1004 priv->agm_pending = false;
Daniel Kurtz3cdfee92011-08-23 23:02:25 -07001005}
1006
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007/*
1008 * called for each full received packet from the touchpad
1009 */
1010static void synaptics_process_packet(struct psmouse *psmouse)
1011{
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001012 struct input_dev *dev = psmouse->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 struct synaptics_data *priv = psmouse->private;
1014 struct synaptics_hw_state hw;
1015 int num_fingers;
1016 int finger_width;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Henrik Rydbergfec6e522010-12-21 18:11:25 +01001018 if (synaptics_parse_hw_state(psmouse->packet, priv, &hw))
1019 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Daniel Kurtz3cdfee92011-08-23 23:02:25 -07001021 if (SYN_CAP_IMAGE_SENSOR(priv->ext_cap_0c)) {
1022 synaptics_image_sensor_process(psmouse, &hw);
1023 return;
1024 }
1025
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 if (hw.scroll) {
1027 priv->scroll += hw.scroll;
1028
1029 while (priv->scroll >= 4) {
1030 input_report_key(dev, BTN_BACK, !hw.down);
1031 input_sync(dev);
1032 input_report_key(dev, BTN_BACK, hw.down);
1033 input_sync(dev);
1034 priv->scroll -= 4;
1035 }
1036 while (priv->scroll <= -4) {
1037 input_report_key(dev, BTN_FORWARD, !hw.up);
1038 input_sync(dev);
1039 input_report_key(dev, BTN_FORWARD, hw.up);
1040 input_sync(dev);
1041 priv->scroll += 4;
1042 }
1043 return;
1044 }
1045
Henrik Rydberg4f56ce92010-12-18 15:42:30 +01001046 if (hw.z > 0 && hw.x > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 num_fingers = 1;
1048 finger_width = 5;
1049 if (SYN_CAP_EXTENDED(priv->capabilities)) {
1050 switch (hw.w) {
1051 case 0 ... 1:
1052 if (SYN_CAP_MULTIFINGER(priv->capabilities))
1053 num_fingers = hw.w + 2;
1054 break;
1055 case 2:
1056 if (SYN_MODEL_PEN(priv->model_id))
1057 ; /* Nothing, treat a pen as a single finger */
1058 break;
1059 case 4 ... 15:
1060 if (SYN_CAP_PALMDETECT(priv->capabilities))
1061 finger_width = hw.w;
1062 break;
1063 }
1064 }
1065 } else {
1066 num_fingers = 0;
1067 finger_width = 0;
1068 }
1069
Henrik Rydbergfec6e522010-12-21 18:11:25 +01001070 if (SYN_CAP_ADV_GESTURE(priv->ext_cap_0c))
Daniel Kurtz7afdb842011-08-23 23:00:33 -07001071 synaptics_report_semi_mt_data(dev, &hw, &priv->agm,
1072 num_fingers);
Henrik Rydbergfec6e522010-12-21 18:11:25 +01001073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 /* Post events
1075 * BTN_TOUCH has to be first as mousedev relies on it when doing
1076 * absolute -> relative conversion
1077 */
1078 if (hw.z > 30) input_report_key(dev, BTN_TOUCH, 1);
1079 if (hw.z < 25) input_report_key(dev, BTN_TOUCH, 0);
1080
Henrik Rydberg4f56ce92010-12-18 15:42:30 +01001081 if (num_fingers > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 input_report_abs(dev, ABS_X, hw.x);
Daniel Kurtz6de58dd2011-08-23 23:00:24 -07001083 input_report_abs(dev, ABS_Y, synaptics_invert_y(hw.y));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 }
1085 input_report_abs(dev, ABS_PRESSURE, hw.z);
1086
Chris Bagwell2a8e7712010-07-19 09:06:15 -07001087 if (SYN_CAP_PALMDETECT(priv->capabilities))
1088 input_report_abs(dev, ABS_TOOL_WIDTH, finger_width);
1089
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 input_report_key(dev, BTN_TOOL_FINGER, num_fingers == 1);
Peter Hutterere42b6642008-11-20 15:24:42 -05001091 if (SYN_CAP_MULTIFINGER(priv->capabilities)) {
1092 input_report_key(dev, BTN_TOOL_DOUBLETAP, num_fingers == 2);
1093 input_report_key(dev, BTN_TOOL_TRIPLETAP, num_fingers == 3);
1094 }
1095
Daniel Kurtz3cdfee92011-08-23 23:02:25 -07001096 synaptics_report_buttons(psmouse, &hw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
1098 input_sync(dev);
1099}
1100
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001101static int synaptics_validate_byte(struct psmouse *psmouse,
1102 int idx, unsigned char pkt_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103{
Helge Dellere38de672006-09-10 21:54:39 -04001104 static const unsigned char newabs_mask[] = { 0xC8, 0x00, 0x00, 0xC8, 0x00 };
1105 static const unsigned char newabs_rel_mask[] = { 0xC0, 0x00, 0x00, 0xC0, 0x00 };
1106 static const unsigned char newabs_rslt[] = { 0x80, 0x00, 0x00, 0xC0, 0x00 };
1107 static const unsigned char oldabs_mask[] = { 0xC0, 0x60, 0x00, 0xC0, 0x60 };
1108 static const unsigned char oldabs_rslt[] = { 0xC0, 0x00, 0x00, 0x80, 0x00 };
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001109 const char *packet = psmouse->packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
1111 if (idx < 0 || idx > 4)
1112 return 0;
1113
1114 switch (pkt_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
Dmitry Torokhova62f0d22010-05-19 10:39:17 -07001116 case SYN_NEWABS:
1117 case SYN_NEWABS_RELAXED:
1118 return (packet[idx] & newabs_rel_mask[idx]) == newabs_rslt[idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
Dmitry Torokhova62f0d22010-05-19 10:39:17 -07001120 case SYN_NEWABS_STRICT:
1121 return (packet[idx] & newabs_mask[idx]) == newabs_rslt[idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
Dmitry Torokhova62f0d22010-05-19 10:39:17 -07001123 case SYN_OLDABS:
1124 return (packet[idx] & oldabs_mask[idx]) == oldabs_rslt[idx];
1125
1126 default:
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001127 psmouse_err(psmouse, "unknown packet type %d\n", pkt_type);
Dmitry Torokhova62f0d22010-05-19 10:39:17 -07001128 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 }
1130}
1131
1132static unsigned char synaptics_detect_pkt_type(struct psmouse *psmouse)
1133{
1134 int i;
1135
1136 for (i = 0; i < 5; i++)
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001137 if (!synaptics_validate_byte(psmouse, i, SYN_NEWABS_STRICT)) {
1138 psmouse_info(psmouse, "using relaxed packet validation\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 return SYN_NEWABS_RELAXED;
1140 }
1141
1142 return SYN_NEWABS_STRICT;
1143}
1144
David Howells7d12e782006-10-05 14:55:46 +01001145static psmouse_ret_t synaptics_process_byte(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 struct synaptics_data *priv = psmouse->private;
1148
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 if (psmouse->pktcnt >= 6) { /* Full packet received */
1150 if (unlikely(priv->pkt_type == SYN_NEWABS))
1151 priv->pkt_type = synaptics_detect_pkt_type(psmouse);
1152
Dmitry Torokhova8b3c0f2010-10-04 21:46:10 -07001153 if (SYN_CAP_PASS_THROUGH(priv->capabilities) &&
1154 synaptics_is_pt_packet(psmouse->packet)) {
1155 if (priv->pt_port)
1156 synaptics_pass_pt_packet(priv->pt_port, psmouse->packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 } else
1158 synaptics_process_packet(psmouse);
1159
1160 return PSMOUSE_FULL_PACKET;
1161 }
1162
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001163 return synaptics_validate_byte(psmouse, psmouse->pktcnt - 1, priv->pkt_type) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 PSMOUSE_GOOD_DATA : PSMOUSE_BAD_DATA;
1165}
1166
1167/*****************************************************************************
1168 * Driver initialization/cleanup functions
1169 ****************************************************************************/
Daniel Kurtz85615472011-08-23 23:00:41 -07001170static void set_abs_position_params(struct input_dev *dev,
1171 struct synaptics_data *priv, int x_code,
1172 int y_code)
1173{
1174 int x_min = priv->x_min ?: XMIN_NOMINAL;
1175 int x_max = priv->x_max ?: XMAX_NOMINAL;
1176 int y_min = priv->y_min ?: YMIN_NOMINAL;
1177 int y_max = priv->y_max ?: YMAX_NOMINAL;
1178 int fuzz = SYN_CAP_REDUCED_FILTERING(priv->ext_cap_0c) ?
1179 SYN_REDUCED_FILTER_FUZZ : 0;
1180
1181 input_set_abs_params(dev, x_code, x_min, x_max, fuzz, 0);
1182 input_set_abs_params(dev, y_code, y_min, y_max, fuzz, 0);
1183 input_abs_set_res(dev, x_code, priv->x_res);
1184 input_abs_set_res(dev, y_code, priv->y_res);
1185}
1186
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187static void set_input_params(struct input_dev *dev, struct synaptics_data *priv)
1188{
1189 int i;
1190
Daniel Drake7968a5d2011-11-08 00:00:35 -08001191 /* Things that apply to both modes */
Henrik Rydbergc14890a82010-12-16 09:52:23 +01001192 __set_bit(INPUT_PROP_POINTER, dev->propbit);
Daniel Drake7968a5d2011-11-08 00:00:35 -08001193 __set_bit(EV_KEY, dev->evbit);
1194 __set_bit(BTN_LEFT, dev->keybit);
1195 __set_bit(BTN_RIGHT, dev->keybit);
Henrik Rydbergc14890a82010-12-16 09:52:23 +01001196
Daniel Drake7968a5d2011-11-08 00:00:35 -08001197 if (SYN_CAP_MIDDLE_BUTTON(priv->capabilities))
1198 __set_bit(BTN_MIDDLE, dev->keybit);
1199
1200 if (!priv->absolute_mode) {
1201 /* Relative mode */
1202 __set_bit(EV_REL, dev->evbit);
1203 __set_bit(REL_X, dev->relbit);
1204 __set_bit(REL_Y, dev->relbit);
1205 return;
1206 }
1207
1208 /* Absolute mode */
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001209 __set_bit(EV_ABS, dev->evbit);
Daniel Kurtz85615472011-08-23 23:00:41 -07001210 set_abs_position_params(dev, priv, ABS_X, ABS_Y);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 input_set_abs_params(dev, ABS_PRESSURE, 0, 255, 0, 0);
Chris Bagwell2a8e7712010-07-19 09:06:15 -07001212
Daniel Kurtz3cdfee92011-08-23 23:02:25 -07001213 if (SYN_CAP_IMAGE_SENSOR(priv->ext_cap_0c)) {
1214 input_mt_init_slots(dev, 2);
1215 set_abs_position_params(dev, priv, ABS_MT_POSITION_X,
1216 ABS_MT_POSITION_Y);
1217 /* Image sensors can report per-contact pressure */
1218 input_set_abs_params(dev, ABS_MT_PRESSURE, 0, 255, 0, 0);
Daniel Kurtz6b4b49f2011-08-23 23:02:56 -07001219
1220 /* Image sensors can signal 4 and 5 finger clicks */
1221 __set_bit(BTN_TOOL_QUADTAP, dev->keybit);
1222 __set_bit(BTN_TOOL_QUINTTAP, dev->keybit);
Daniel Kurtz3cdfee92011-08-23 23:02:25 -07001223 } else if (SYN_CAP_ADV_GESTURE(priv->ext_cap_0c)) {
1224 /* Non-image sensors with AGM use semi-mt */
Henrik Rydbergfec6e522010-12-21 18:11:25 +01001225 __set_bit(INPUT_PROP_SEMI_MT, dev->propbit);
1226 input_mt_init_slots(dev, 2);
Daniel Kurtz85615472011-08-23 23:00:41 -07001227 set_abs_position_params(dev, priv, ABS_MT_POSITION_X,
1228 ABS_MT_POSITION_Y);
Henrik Rydbergfec6e522010-12-21 18:11:25 +01001229 }
1230
Chris Bagwell2a8e7712010-07-19 09:06:15 -07001231 if (SYN_CAP_PALMDETECT(priv->capabilities))
Chris Bagwell58fb0212010-07-19 09:06:15 -07001232 input_set_abs_params(dev, ABS_TOOL_WIDTH, 0, 15, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001234 __set_bit(BTN_TOUCH, dev->keybit);
1235 __set_bit(BTN_TOOL_FINGER, dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
Peter Hutterere42b6642008-11-20 15:24:42 -05001237 if (SYN_CAP_MULTIFINGER(priv->capabilities)) {
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001238 __set_bit(BTN_TOOL_DOUBLETAP, dev->keybit);
1239 __set_bit(BTN_TOOL_TRIPLETAP, dev->keybit);
Peter Hutterere42b6642008-11-20 15:24:42 -05001240 }
1241
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 if (SYN_CAP_FOUR_BUTTON(priv->capabilities) ||
1243 SYN_CAP_MIDDLE_BUTTON(priv->capabilities)) {
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001244 __set_bit(BTN_FORWARD, dev->keybit);
1245 __set_bit(BTN_BACK, dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 }
1247
1248 for (i = 0; i < SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap); i++)
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001249 __set_bit(BTN_0 + i, dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001251 __clear_bit(EV_REL, dev->evbit);
1252 __clear_bit(REL_X, dev->relbit);
1253 __clear_bit(REL_Y, dev->relbit);
Tero Saarniec20a022009-06-10 23:27:24 -07001254
Takashi Iwai5f57d672010-04-19 10:37:21 -07001255 if (SYN_CAP_CLICKPAD(priv->ext_cap_0c)) {
Henrik Rydbergc14890a82010-12-16 09:52:23 +01001256 __set_bit(INPUT_PROP_BUTTONPAD, dev->propbit);
Takashi Iwai5f57d672010-04-19 10:37:21 -07001257 /* Clickpads report only left button */
1258 __clear_bit(BTN_RIGHT, dev->keybit);
1259 __clear_bit(BTN_MIDDLE, dev->keybit);
1260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261}
1262
Daniel Drake7968a5d2011-11-08 00:00:35 -08001263static ssize_t synaptics_show_disable_gesture(struct psmouse *psmouse,
1264 void *data, char *buf)
1265{
1266 struct synaptics_data *priv = psmouse->private;
1267
1268 return sprintf(buf, "%c\n", priv->disable_gesture ? '1' : '0');
1269}
1270
1271static ssize_t synaptics_set_disable_gesture(struct psmouse *psmouse,
1272 void *data, const char *buf,
1273 size_t len)
1274{
1275 struct synaptics_data *priv = psmouse->private;
1276 unsigned int value;
1277 int err;
1278
1279 err = kstrtouint(buf, 10, &value);
1280 if (err)
1281 return err;
1282
1283 if (value > 1)
1284 return -EINVAL;
1285
1286 if (value == priv->disable_gesture)
1287 return len;
1288
1289 priv->disable_gesture = value;
1290 if (value)
1291 priv->mode |= SYN_BIT_DISABLE_GESTURE;
1292 else
1293 priv->mode &= ~SYN_BIT_DISABLE_GESTURE;
1294
1295 if (synaptics_mode_cmd(psmouse, priv->mode))
1296 return -EIO;
1297
1298 return len;
1299}
1300
1301PSMOUSE_DEFINE_ATTR(disable_gesture, S_IWUSR | S_IRUGO, NULL,
1302 synaptics_show_disable_gesture,
1303 synaptics_set_disable_gesture);
1304
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305static void synaptics_disconnect(struct psmouse *psmouse)
1306{
Daniel Drake7968a5d2011-11-08 00:00:35 -08001307 struct synaptics_data *priv = psmouse->private;
1308
1309 if (!priv->absolute_mode && SYN_ID_DISGEST_SUPPORTED(priv->identity))
1310 device_remove_file(&psmouse->ps2dev.serio->dev,
1311 &psmouse_attr_disable_gesture.dattr);
1312
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 synaptics_reset(psmouse);
Daniel Drake7968a5d2011-11-08 00:00:35 -08001314 kfree(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 psmouse->private = NULL;
1316}
1317
1318static int synaptics_reconnect(struct psmouse *psmouse)
1319{
1320 struct synaptics_data *priv = psmouse->private;
1321 struct synaptics_data old_priv = *priv;
Alexandre Peixoto Ferreirac63fe0a2011-01-28 22:05:14 -08001322 int retry = 0;
1323 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
Alexandre Peixoto Ferreirac63fe0a2011-01-28 22:05:14 -08001325 do {
1326 psmouse_reset(psmouse);
Dmitry Torokhov85214782011-12-12 00:05:53 -08001327 if (retry) {
1328 /*
1329 * On some boxes, right after resuming, the touchpad
1330 * needs some time to finish initializing (I assume
1331 * it needs time to calibrate) and start responding
1332 * to Synaptics-specific queries, so let's wait a
1333 * bit.
1334 */
1335 ssleep(1);
1336 }
Alexandre Peixoto Ferreirac63fe0a2011-01-28 22:05:14 -08001337 error = synaptics_detect(psmouse, 0);
1338 } while (error && ++retry < 3);
Andy Whitcroft4d368452009-02-28 12:51:01 -08001339
Alexandre Peixoto Ferreirac63fe0a2011-01-28 22:05:14 -08001340 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 return -1;
1342
Alexandre Peixoto Ferreirac63fe0a2011-01-28 22:05:14 -08001343 if (retry > 1)
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001344 psmouse_dbg(psmouse, "reconnected after %d tries\n", retry);
Alexandre Peixoto Ferreirac63fe0a2011-01-28 22:05:14 -08001345
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 if (synaptics_query_hardware(psmouse)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001347 psmouse_err(psmouse, "Unable to query device.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 return -1;
1349 }
1350
Daniel Drake7968a5d2011-11-08 00:00:35 -08001351 if (synaptics_set_mode(psmouse)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001352 psmouse_err(psmouse, "Unable to initialize device.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 return -1;
1354 }
1355
Alexandre Peixoto Ferreirabaddf582011-01-28 22:05:14 -08001356 if (old_priv.identity != priv->identity ||
1357 old_priv.model_id != priv->model_id ||
1358 old_priv.capabilities != priv->capabilities ||
1359 old_priv.ext_cap != priv->ext_cap) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001360 psmouse_err(psmouse,
1361 "hardware appears to be different: id(%ld-%ld), model(%ld-%ld), caps(%lx-%lx), ext(%lx-%lx).\n",
1362 old_priv.identity, priv->identity,
1363 old_priv.model_id, priv->model_id,
1364 old_priv.capabilities, priv->capabilities,
1365 old_priv.ext_cap, priv->ext_cap);
Alexandre Peixoto Ferreirabaddf582011-01-28 22:05:14 -08001366 return -1;
1367 }
1368
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 return 0;
1370}
1371
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001372static bool impaired_toshiba_kbc;
1373
1374static const struct dmi_system_id __initconst toshiba_dmi_table[] = {
1375#if defined(CONFIG_DMI) && defined(CONFIG_X86)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 {
Dmitry Torokhov9961e252009-12-04 10:24:20 -08001377 /* Toshiba Satellite */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 .matches = {
1379 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
Richard Thrippleton53a26702006-04-02 00:10:18 -05001380 DMI_MATCH(DMI_PRODUCT_NAME, "Satellite"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 },
1382 },
Simon Horman9ba5eaa2005-07-11 01:07:20 -05001383 {
Dmitry Torokhov9961e252009-12-04 10:24:20 -08001384 /* Toshiba Dynabook */
Simon Horman9ba5eaa2005-07-11 01:07:20 -05001385 .matches = {
1386 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
Richard Thrippleton53a26702006-04-02 00:10:18 -05001387 DMI_MATCH(DMI_PRODUCT_NAME, "dynabook"),
1388 },
1389 },
1390 {
Dmitry Torokhov9961e252009-12-04 10:24:20 -08001391 /* Toshiba Portege M300 */
Richard Thrippleton53a26702006-04-02 00:10:18 -05001392 .matches = {
1393 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1394 DMI_MATCH(DMI_PRODUCT_NAME, "PORTEGE M300"),
Simon Horman9ba5eaa2005-07-11 01:07:20 -05001395 },
Dmitry Torokhov5f5eeff2009-10-12 21:35:00 -07001396
1397 },
1398 {
Dmitry Torokhov9961e252009-12-04 10:24:20 -08001399 /* Toshiba Portege M300 */
Dmitry Torokhov5f5eeff2009-10-12 21:35:00 -07001400 .matches = {
1401 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1402 DMI_MATCH(DMI_PRODUCT_NAME, "Portable PC"),
1403 DMI_MATCH(DMI_PRODUCT_VERSION, "Version 1.0"),
1404 },
1405
Simon Horman9ba5eaa2005-07-11 01:07:20 -05001406 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407#endif
Jan Beulich70874862011-03-31 00:01:58 -07001408 { }
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001409};
1410
Andres Salomonef8313b2010-12-23 01:19:38 -08001411static bool broken_olpc_ec;
1412
1413static const struct dmi_system_id __initconst olpc_dmi_table[] = {
1414#if defined(CONFIG_DMI) && defined(CONFIG_OLPC)
1415 {
1416 /* OLPC XO-1 or XO-1.5 */
1417 .matches = {
1418 DMI_MATCH(DMI_SYS_VENDOR, "OLPC"),
1419 DMI_MATCH(DMI_PRODUCT_NAME, "XO"),
1420 },
1421 },
Andres Salomonef8313b2010-12-23 01:19:38 -08001422#endif
Jan Beulich70874862011-03-31 00:01:58 -07001423 { }
Andres Salomonef8313b2010-12-23 01:19:38 -08001424};
1425
Benjamin Tissoires969ba042014-03-28 00:43:00 -07001426static const struct dmi_system_id min_max_dmi_table[] __initconst = {
1427#if defined(CONFIG_DMI)
1428 {
1429 /* Lenovo ThinkPad Helix */
1430 .matches = {
1431 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1432 DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad Helix"),
1433 },
1434 .driver_data = (int []){1024, 5052, 2258, 4832},
1435 },
1436 {
Hans de Goede3fcaab02014-03-28 01:01:38 -07001437 /* Lenovo ThinkPad X240 */
1438 .matches = {
1439 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1440 DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad X240"),
1441 },
1442 .driver_data = (int []){1232, 5710, 1156, 4696},
1443 },
1444 {
Benjamin Tissoires969ba042014-03-28 00:43:00 -07001445 /* Lenovo ThinkPad T440s */
1446 .matches = {
1447 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1448 DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad T440"),
1449 },
1450 .driver_data = (int []){1024, 5112, 2024, 4832},
1451 },
1452 {
1453 /* Lenovo ThinkPad T540p */
1454 .matches = {
1455 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1456 DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad T540"),
1457 },
1458 .driver_data = (int []){1024, 5056, 2058, 4832},
1459 },
1460#endif
1461 { }
1462};
1463
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001464void __init synaptics_module_init(void)
1465{
Benjamin Tissoires969ba042014-03-28 00:43:00 -07001466 const struct dmi_system_id *min_max_dmi;
1467
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001468 impaired_toshiba_kbc = dmi_check_system(toshiba_dmi_table);
Andres Salomonef8313b2010-12-23 01:19:38 -08001469 broken_olpc_ec = dmi_check_system(olpc_dmi_table);
Benjamin Tissoires969ba042014-03-28 00:43:00 -07001470
1471 min_max_dmi = dmi_first_match(min_max_dmi_table);
1472 if (min_max_dmi)
1473 quirk_min_max = min_max_dmi->driver_data;
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001474}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
Daniel Drake7968a5d2011-11-08 00:00:35 -08001476static int __synaptics_init(struct psmouse *psmouse, bool absolute_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477{
1478 struct synaptics_data *priv;
Daniel Drake7968a5d2011-11-08 00:00:35 -08001479 int err = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Andres Salomonef8313b2010-12-23 01:19:38 -08001481 /*
Daniel Drake83551c02011-11-11 16:05:04 -08001482 * The OLPC XO has issues with Synaptics' absolute mode; the constant
1483 * packet spew overloads the EC such that key presses on the keyboard
1484 * are missed. Given that, don't even attempt to use Absolute mode.
1485 * Relative mode seems to work just fine.
Andres Salomonef8313b2010-12-23 01:19:38 -08001486 */
Daniel Drake83551c02011-11-11 16:05:04 -08001487 if (absolute_mode && broken_olpc_ec) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001488 psmouse_info(psmouse,
1489 "OLPC XO detected, not enabling Synaptics protocol.\n");
Andres Salomonef8313b2010-12-23 01:19:38 -08001490 return -ENODEV;
1491 }
1492
Eric Sesterhennb39787a2006-03-14 00:09:16 -05001493 psmouse->private = priv = kzalloc(sizeof(struct synaptics_data), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 if (!priv)
Davidlohr Bueso6792cbb2010-09-29 18:53:35 -07001495 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
Andy Whitcroft4d368452009-02-28 12:51:01 -08001497 psmouse_reset(psmouse);
1498
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 if (synaptics_query_hardware(psmouse)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001500 psmouse_err(psmouse, "Unable to query device.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 goto init_fail;
1502 }
1503
Daniel Drake7968a5d2011-11-08 00:00:35 -08001504 priv->absolute_mode = absolute_mode;
1505 if (SYN_ID_DISGEST_SUPPORTED(priv->identity))
1506 priv->disable_gesture = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Daniel Drake7968a5d2011-11-08 00:00:35 -08001508 if (synaptics_set_mode(psmouse)) {
1509 psmouse_err(psmouse, "Unable to initialize device.\n");
Henrik Rydbergfec6e522010-12-21 18:11:25 +01001510 goto init_fail;
1511 }
1512
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 priv->pkt_type = SYN_MODEL_NEWABS(priv->model_id) ? SYN_NEWABS : SYN_OLDABS;
1514
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001515 psmouse_info(psmouse,
1516 "Touchpad model: %ld, fw: %ld.%ld, id: %#lx, caps: %#lx/%#lx/%#lx\n",
1517 SYN_ID_MODEL(priv->identity),
1518 SYN_ID_MAJOR(priv->identity), SYN_ID_MINOR(priv->identity),
1519 priv->model_id,
1520 priv->capabilities, priv->ext_cap, priv->ext_cap_0c);
Dmitry Torokhov409b7502005-05-28 02:12:18 -05001521
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001522 set_input_params(psmouse->dev, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
Dmitry Torokhov887cc122007-04-12 01:30:41 -04001524 /*
1525 * Encode touchpad model so that it can be used to set
1526 * input device->id.version and be visible to userspace.
1527 * Because version is __u16 we have to drop something.
1528 * Hardware info bits seem to be good candidates as they
1529 * are documented to be for Synaptics corp. internal use.
1530 */
1531 psmouse->model = ((priv->model_id & 0x00ff0000) >> 8) |
1532 (priv->model_id & 0x000000ff);
1533
Daniel Drake7968a5d2011-11-08 00:00:35 -08001534 if (absolute_mode) {
1535 psmouse->protocol_handler = synaptics_process_byte;
1536 psmouse->pktsize = 6;
1537 } else {
1538 /* Relative mode follows standard PS/2 mouse protocol */
1539 psmouse->protocol_handler = psmouse_process_byte;
1540 psmouse->pktsize = 3;
1541 }
1542
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 psmouse->set_rate = synaptics_set_rate;
1544 psmouse->disconnect = synaptics_disconnect;
1545 psmouse->reconnect = synaptics_reconnect;
Dmitry Torokhova1cec062007-02-18 01:40:24 -05001546 psmouse->cleanup = synaptics_reset;
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001547 /* Synaptics can usually stay in sync without extra help */
1548 psmouse->resync_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
1550 if (SYN_CAP_PASS_THROUGH(priv->capabilities))
1551 synaptics_pt_create(psmouse);
1552
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 /*
1554 * Toshiba's KBC seems to have trouble handling data from
Andres Salomon7ee99162010-12-23 01:18:28 -08001555 * Synaptics at full rate. Switch to a lower rate (roughly
1556 * the same rate as a standard PS/2 mouse).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 */
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001558 if (psmouse->rate >= 80 && impaired_toshiba_kbc) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001559 psmouse_info(psmouse,
1560 "Toshiba %s detected, limiting rate to 40pps.\n",
1561 dmi_get_system_info(DMI_PRODUCT_NAME));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 psmouse->rate = 40;
1563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
Daniel Drake7968a5d2011-11-08 00:00:35 -08001565 if (!priv->absolute_mode && SYN_ID_DISGEST_SUPPORTED(priv->identity)) {
1566 err = device_create_file(&psmouse->ps2dev.serio->dev,
1567 &psmouse_attr_disable_gesture.dattr);
1568 if (err) {
1569 psmouse_err(psmouse,
1570 "Failed to create disable_gesture attribute (%d)",
1571 err);
1572 goto init_fail;
1573 }
1574 }
1575
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 return 0;
1577
1578 init_fail:
1579 kfree(priv);
Daniel Drake7968a5d2011-11-08 00:00:35 -08001580 return err;
1581}
1582
1583int synaptics_init(struct psmouse *psmouse)
1584{
1585 return __synaptics_init(psmouse, true);
1586}
1587
1588int synaptics_init_relative(struct psmouse *psmouse)
1589{
1590 return __synaptics_init(psmouse, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591}
1592
Daniel Drakee4e6efd2010-01-07 01:52:39 -08001593bool synaptics_supported(void)
1594{
1595 return true;
1596}
1597
Andres Salomon55e3d922007-03-10 01:39:54 -05001598#else /* CONFIG_MOUSE_PS2_SYNAPTICS */
1599
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001600void __init synaptics_module_init(void)
1601{
1602}
1603
Andres Salomon55e3d922007-03-10 01:39:54 -05001604int synaptics_init(struct psmouse *psmouse)
1605{
1606 return -ENOSYS;
1607}
1608
Daniel Drakee4e6efd2010-01-07 01:52:39 -08001609bool synaptics_supported(void)
1610{
1611 return false;
1612}
1613
Andres Salomon55e3d922007-03-10 01:39:54 -05001614#endif /* CONFIG_MOUSE_PS2_SYNAPTICS */