blob: e2c2e1e2bd6f8b6d2b96a7eba7f17121d08f6bf8 [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 */
235static int synaptics_resolution(struct psmouse *psmouse)
236{
237 struct synaptics_data *priv = psmouse->private;
Dmitry Torokhova66413f2011-07-09 12:32:56 -0700238 unsigned char resp[3];
Tero Saarniec20a022009-06-10 23:27:24 -0700239
240 if (SYN_ID_MAJOR(priv->identity) < 4)
Takashi Iwaibbddd192010-07-14 09:32:46 -0700241 return 0;
Tero Saarniec20a022009-06-10 23:27:24 -0700242
Dmitry Torokhova66413f2011-07-09 12:32:56 -0700243 if (synaptics_send_cmd(psmouse, SYN_QUE_RESOLUTION, resp) == 0) {
244 if (resp[0] != 0 && (resp[1] & 0x80) && resp[2] != 0) {
245 priv->x_res = resp[0]; /* x resolution in units/mm */
246 priv->y_res = resp[2]; /* y resolution in units/mm */
Dmitry Torokhov83ba9ea2010-05-10 23:06:52 -0700247 }
248 }
Tero Saarniec20a022009-06-10 23:27:24 -0700249
Dmitry Torokhov83ba9ea2010-05-10 23:06:52 -0700250 if (SYN_EXT_CAP_REQUESTS(priv->capabilities) >= 5 &&
251 SYN_CAP_MAX_DIMENSIONS(priv->ext_cap_0c)) {
Dmitry Torokhova66413f2011-07-09 12:32:56 -0700252 if (synaptics_send_cmd(psmouse, SYN_QUE_EXT_MAX_COORDS, resp)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700253 psmouse_warn(psmouse,
254 "device claims to have max coordinates query, but I'm not able to read it.\n");
Dmitry Torokhov83ba9ea2010-05-10 23:06:52 -0700255 } else {
Dmitry Torokhova66413f2011-07-09 12:32:56 -0700256 priv->x_max = (resp[0] << 5) | ((resp[1] & 0x0f) << 1);
257 priv->y_max = (resp[2] << 5) | ((resp[1] & 0xf0) >> 3);
258 }
259 }
260
261 if (SYN_EXT_CAP_REQUESTS(priv->capabilities) >= 7 &&
262 SYN_CAP_MIN_DIMENSIONS(priv->ext_cap_0c)) {
263 if (synaptics_send_cmd(psmouse, SYN_QUE_EXT_MIN_COORDS, resp)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700264 psmouse_warn(psmouse,
265 "device claims to have min coordinates query, but I'm not able to read it.\n");
Dmitry Torokhova66413f2011-07-09 12:32:56 -0700266 } else {
267 priv->x_min = (resp[0] << 5) | ((resp[1] & 0x0f) << 1);
268 priv->y_min = (resp[2] << 5) | ((resp[1] & 0xf0) >> 3);
Dmitry Torokhov83ba9ea2010-05-10 23:06:52 -0700269 }
Tero Saarniec20a022009-06-10 23:27:24 -0700270 }
271
272 return 0;
273}
274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275static int synaptics_query_hardware(struct psmouse *psmouse)
276{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 if (synaptics_identify(psmouse))
278 return -1;
279 if (synaptics_model_id(psmouse))
280 return -1;
281 if (synaptics_capability(psmouse))
282 return -1;
Tero Saarniec20a022009-06-10 23:27:24 -0700283 if (synaptics_resolution(psmouse))
284 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 return 0;
287}
288
Daniel Drake7968a5d2011-11-08 00:00:35 -0800289static int synaptics_set_advanced_gesture_mode(struct psmouse *psmouse)
290{
291 static unsigned char param = 0xc8;
292 struct synaptics_data *priv = psmouse->private;
293
Benjamin Herrenschmidt899c6122012-04-20 22:34:49 -0700294 if (!(SYN_CAP_ADV_GESTURE(priv->ext_cap_0c) ||
295 SYN_CAP_IMAGE_SENSOR(priv->ext_cap_0c)))
Daniel Drake7968a5d2011-11-08 00:00:35 -0800296 return 0;
297
298 if (psmouse_sliced_command(psmouse, SYN_QUE_MODEL))
299 return -1;
300
301 if (ps2_command(&psmouse->ps2dev, &param, PSMOUSE_CMD_SETRATE))
302 return -1;
303
304 /* Advanced gesture mode also sends multi finger data */
305 priv->capabilities |= BIT(1);
306
307 return 0;
308}
309
310static int synaptics_set_mode(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
312 struct synaptics_data *priv = psmouse->private;
313
Daniel Drake7968a5d2011-11-08 00:00:35 -0800314 priv->mode = 0;
315 if (priv->absolute_mode)
316 priv->mode |= SYN_BIT_ABSOLUTE_MODE;
317 if (priv->disable_gesture)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 priv->mode |= SYN_BIT_DISABLE_GESTURE;
Daniel Drake7968a5d2011-11-08 00:00:35 -0800319 if (psmouse->rate >= 80)
320 priv->mode |= SYN_BIT_HIGH_RATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 if (SYN_CAP_EXTENDED(priv->capabilities))
322 priv->mode |= SYN_BIT_W_MODE;
323
324 if (synaptics_mode_cmd(psmouse, priv->mode))
325 return -1;
326
Daniel Drake7968a5d2011-11-08 00:00:35 -0800327 if (priv->absolute_mode &&
328 synaptics_set_advanced_gesture_mode(psmouse)) {
329 psmouse_err(psmouse, "Advanced gesture mode init failed.\n");
330 return -1;
331 }
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 return 0;
334}
335
336static void synaptics_set_rate(struct psmouse *psmouse, unsigned int rate)
337{
338 struct synaptics_data *priv = psmouse->private;
339
340 if (rate >= 80) {
341 priv->mode |= SYN_BIT_HIGH_RATE;
342 psmouse->rate = 80;
343 } else {
344 priv->mode &= ~SYN_BIT_HIGH_RATE;
345 psmouse->rate = 40;
346 }
347
348 synaptics_mode_cmd(psmouse, priv->mode);
349}
350
351/*****************************************************************************
352 * Synaptics pass-through PS/2 port support
353 ****************************************************************************/
354static int synaptics_pt_write(struct serio *serio, unsigned char c)
355{
356 struct psmouse *parent = serio_get_drvdata(serio->parent);
357 char rate_param = SYN_PS_CLIENT_CMD; /* indicates that we want pass-through port */
358
359 if (psmouse_sliced_command(parent, c))
360 return -1;
361 if (ps2_command(&parent->ps2dev, &rate_param, PSMOUSE_CMD_SETRATE))
362 return -1;
363 return 0;
364}
365
Dmitry Torokhova8b3c0f2010-10-04 21:46:10 -0700366static int synaptics_pt_start(struct serio *serio)
367{
368 struct psmouse *parent = serio_get_drvdata(serio->parent);
369 struct synaptics_data *priv = parent->private;
370
371 serio_pause_rx(parent->ps2dev.serio);
372 priv->pt_port = serio;
373 serio_continue_rx(parent->ps2dev.serio);
374
375 return 0;
376}
377
378static void synaptics_pt_stop(struct serio *serio)
379{
380 struct psmouse *parent = serio_get_drvdata(serio->parent);
381 struct synaptics_data *priv = parent->private;
382
383 serio_pause_rx(parent->ps2dev.serio);
384 priv->pt_port = NULL;
385 serio_continue_rx(parent->ps2dev.serio);
386}
387
388static int synaptics_is_pt_packet(unsigned char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
390 return (buf[0] & 0xFC) == 0x84 && (buf[3] & 0xCC) == 0xC4;
391}
392
393static void synaptics_pass_pt_packet(struct serio *ptport, unsigned char *packet)
394{
395 struct psmouse *child = serio_get_drvdata(ptport);
396
397 if (child && child->state == PSMOUSE_ACTIVATED) {
David Howells7d12e782006-10-05 14:55:46 +0100398 serio_interrupt(ptport, packet[1], 0);
399 serio_interrupt(ptport, packet[4], 0);
400 serio_interrupt(ptport, packet[5], 0);
Sergey Vlasov33fdfa92005-07-24 00:53:32 -0500401 if (child->pktsize == 4)
David Howells7d12e782006-10-05 14:55:46 +0100402 serio_interrupt(ptport, packet[2], 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 } else
David Howells7d12e782006-10-05 14:55:46 +0100404 serio_interrupt(ptport, packet[1], 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406
407static void synaptics_pt_activate(struct psmouse *psmouse)
408{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 struct synaptics_data *priv = psmouse->private;
Dmitry Torokhova8b3c0f2010-10-04 21:46:10 -0700410 struct psmouse *child = serio_get_drvdata(priv->pt_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412 /* adjust the touchpad to child's choice of protocol */
413 if (child) {
Sergey Vlasov33fdfa92005-07-24 00:53:32 -0500414 if (child->pktsize == 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 priv->mode |= SYN_BIT_FOUR_BYTE_CLIENT;
416 else
417 priv->mode &= ~SYN_BIT_FOUR_BYTE_CLIENT;
418
419 if (synaptics_mode_cmd(psmouse, priv->mode))
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700420 psmouse_warn(psmouse,
421 "failed to switch guest protocol\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 }
423}
424
425static void synaptics_pt_create(struct psmouse *psmouse)
426{
427 struct serio *serio;
428
Eric Sesterhennb39787a2006-03-14 00:09:16 -0500429 serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 if (!serio) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700431 psmouse_err(psmouse,
432 "not enough memory for pass-through port\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 return;
434 }
435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 serio->id.type = SERIO_PS_PSTHRU;
437 strlcpy(serio->name, "Synaptics pass-through", sizeof(serio->name));
438 strlcpy(serio->phys, "synaptics-pt/serio0", sizeof(serio->name));
439 serio->write = synaptics_pt_write;
Dmitry Torokhova8b3c0f2010-10-04 21:46:10 -0700440 serio->start = synaptics_pt_start;
441 serio->stop = synaptics_pt_stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 serio->parent = psmouse->ps2dev.serio;
443
444 psmouse->pt_activate = synaptics_pt_activate;
445
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700446 psmouse_info(psmouse, "serio: %s port at %s\n",
447 serio->name, psmouse->phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 serio_register_port(serio);
449}
450
451/*****************************************************************************
452 * Functions to interpret the absolute mode packets
453 ****************************************************************************/
454
Daniel Kurtza6ca40c2011-08-23 23:02:31 -0700455static void synaptics_mt_state_set(struct synaptics_mt_state *state, int count,
456 int sgm, int agm)
457{
458 state->count = count;
459 state->sgm = sgm;
460 state->agm = agm;
461}
462
Daniel Kurtz7afdb842011-08-23 23:00:33 -0700463static void synaptics_parse_agm(const unsigned char buf[],
Daniel Kurtza6ca40c2011-08-23 23:02:31 -0700464 struct synaptics_data *priv,
465 struct synaptics_hw_state *hw)
Daniel Kurtz7afdb842011-08-23 23:00:33 -0700466{
467 struct synaptics_hw_state *agm = &priv->agm;
Daniel Kurtza6ca40c2011-08-23 23:02:31 -0700468 int agm_packet_type;
Daniel Kurtz7afdb842011-08-23 23:00:33 -0700469
Daniel Kurtza6ca40c2011-08-23 23:02:31 -0700470 agm_packet_type = (buf[5] & 0x30) >> 4;
471 switch (agm_packet_type) {
472 case 1:
473 /* Gesture packet: (x, y, z) half resolution */
474 agm->w = hw->w;
475 agm->x = (((buf[4] & 0x0f) << 8) | buf[1]) << 1;
476 agm->y = (((buf[4] & 0xf0) << 4) | buf[2]) << 1;
477 agm->z = ((buf[3] & 0x30) | (buf[5] & 0x0f)) << 1;
478 break;
479
480 case 2:
481 /* AGM-CONTACT packet: (count, sgm, agm) */
482 synaptics_mt_state_set(&agm->mt_state, buf[1], buf[2], buf[4]);
483 break;
484
485 default:
486 break;
487 }
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700488
489 /* Record that at least one AGM has been received since last SGM */
490 priv->agm_pending = true;
Daniel Kurtz7afdb842011-08-23 23:00:33 -0700491}
492
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100493static int synaptics_parse_hw_state(const unsigned char buf[],
494 struct synaptics_data *priv,
495 struct synaptics_hw_state *hw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
497 memset(hw, 0, sizeof(struct synaptics_hw_state));
498
499 if (SYN_MODEL_NEWABS(priv->model_id)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 hw->w = (((buf[0] & 0x30) >> 2) |
501 ((buf[0] & 0x04) >> 1) |
502 ((buf[3] & 0x04) >> 2));
503
504 hw->left = (buf[0] & 0x01) ? 1 : 0;
505 hw->right = (buf[0] & 0x02) ? 1 : 0;
506
Takashi Iwai5f57d672010-04-19 10:37:21 -0700507 if (SYN_CAP_CLICKPAD(priv->ext_cap_0c)) {
508 /*
509 * Clickpad's button is transmitted as middle button,
510 * however, since it is primary button, we will report
511 * it as BTN_LEFT.
512 */
513 hw->left = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0;
514
515 } else if (SYN_CAP_MIDDLE_BUTTON(priv->capabilities)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 hw->middle = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0;
517 if (hw->w == 2)
518 hw->scroll = (signed char)(buf[1]);
519 }
520
521 if (SYN_CAP_FOUR_BUTTON(priv->capabilities)) {
522 hw->up = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0;
523 hw->down = ((buf[0] ^ buf[3]) & 0x02) ? 1 : 0;
524 }
525
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700526 if ((SYN_CAP_ADV_GESTURE(priv->ext_cap_0c) ||
527 SYN_CAP_IMAGE_SENSOR(priv->ext_cap_0c)) &&
528 hw->w == 2) {
Daniel Kurtza6ca40c2011-08-23 23:02:31 -0700529 synaptics_parse_agm(buf, priv, hw);
Daniel Kurtz28d5fd82011-07-06 22:57:39 -0700530 return 1;
531 }
532
533 hw->x = (((buf[3] & 0x10) << 8) |
534 ((buf[1] & 0x0f) << 8) |
535 buf[4]);
536 hw->y = (((buf[3] & 0x20) << 7) |
537 ((buf[1] & 0xf0) << 4) |
538 buf[5]);
539 hw->z = buf[2];
540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 if (SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap) &&
542 ((buf[0] ^ buf[3]) & 0x02)) {
543 switch (SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap) & ~0x01) {
544 default:
545 /*
546 * if nExtBtn is greater than 8 it should be
547 * considered invalid and treated as 0
548 */
549 break;
550 case 8:
551 hw->ext_buttons |= ((buf[5] & 0x08)) ? 0x80 : 0;
552 hw->ext_buttons |= ((buf[4] & 0x08)) ? 0x40 : 0;
553 case 6:
554 hw->ext_buttons |= ((buf[5] & 0x04)) ? 0x20 : 0;
555 hw->ext_buttons |= ((buf[4] & 0x04)) ? 0x10 : 0;
556 case 4:
557 hw->ext_buttons |= ((buf[5] & 0x02)) ? 0x08 : 0;
558 hw->ext_buttons |= ((buf[4] & 0x02)) ? 0x04 : 0;
559 case 2:
560 hw->ext_buttons |= ((buf[5] & 0x01)) ? 0x02 : 0;
561 hw->ext_buttons |= ((buf[4] & 0x01)) ? 0x01 : 0;
562 }
563 }
564 } else {
565 hw->x = (((buf[1] & 0x1f) << 8) | buf[2]);
566 hw->y = (((buf[4] & 0x1f) << 8) | buf[5]);
567
568 hw->z = (((buf[0] & 0x30) << 2) | (buf[3] & 0x3F));
569 hw->w = (((buf[1] & 0x80) >> 4) | ((buf[0] & 0x04) >> 1));
570
571 hw->left = (buf[0] & 0x01) ? 1 : 0;
572 hw->right = (buf[0] & 0x02) ? 1 : 0;
573 }
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100574
Seth Forsheeba37c702012-07-24 23:54:11 -0700575 /* Convert wrap-around values to negative */
576 if (hw->x > X_MAX_POSITIVE)
577 hw->x -= 1 << ABS_POS_BITS;
578 if (hw->y > Y_MAX_POSITIVE)
579 hw->y -= 1 << ABS_POS_BITS;
580
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100581 return 0;
582}
583
Daniel Kurtzbea9f0f2011-07-06 22:42:52 -0700584static void synaptics_report_semi_mt_slot(struct input_dev *dev, int slot,
585 bool active, int x, int y)
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100586{
587 input_mt_slot(dev, slot);
588 input_mt_report_slot_state(dev, MT_TOOL_FINGER, active);
589 if (active) {
590 input_report_abs(dev, ABS_MT_POSITION_X, x);
Daniel Kurtz6de58dd2011-08-23 23:00:24 -0700591 input_report_abs(dev, ABS_MT_POSITION_Y, synaptics_invert_y(y));
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100592 }
593}
594
595static void synaptics_report_semi_mt_data(struct input_dev *dev,
596 const struct synaptics_hw_state *a,
597 const struct synaptics_hw_state *b,
598 int num_fingers)
599{
600 if (num_fingers >= 2) {
Daniel Kurtzbea9f0f2011-07-06 22:42:52 -0700601 synaptics_report_semi_mt_slot(dev, 0, true, min(a->x, b->x),
602 min(a->y, b->y));
603 synaptics_report_semi_mt_slot(dev, 1, true, max(a->x, b->x),
604 max(a->y, b->y));
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100605 } else if (num_fingers == 1) {
Daniel Kurtzbea9f0f2011-07-06 22:42:52 -0700606 synaptics_report_semi_mt_slot(dev, 0, true, a->x, a->y);
607 synaptics_report_semi_mt_slot(dev, 1, false, 0, 0);
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100608 } else {
Daniel Kurtzbea9f0f2011-07-06 22:42:52 -0700609 synaptics_report_semi_mt_slot(dev, 0, false, 0, 0);
610 synaptics_report_semi_mt_slot(dev, 1, false, 0, 0);
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100611 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612}
613
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700614static void synaptics_report_buttons(struct psmouse *psmouse,
615 const struct synaptics_hw_state *hw)
616{
617 struct input_dev *dev = psmouse->dev;
618 struct synaptics_data *priv = psmouse->private;
619 int i;
620
621 input_report_key(dev, BTN_LEFT, hw->left);
622 input_report_key(dev, BTN_RIGHT, hw->right);
623
624 if (SYN_CAP_MIDDLE_BUTTON(priv->capabilities))
625 input_report_key(dev, BTN_MIDDLE, hw->middle);
626
627 if (SYN_CAP_FOUR_BUTTON(priv->capabilities)) {
628 input_report_key(dev, BTN_FORWARD, hw->up);
629 input_report_key(dev, BTN_BACK, hw->down);
630 }
631
632 for (i = 0; i < SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap); i++)
633 input_report_key(dev, BTN_0 + i, hw->ext_buttons & (1 << i));
634}
635
636static void synaptics_report_slot(struct input_dev *dev, int slot,
637 const struct synaptics_hw_state *hw)
638{
639 input_mt_slot(dev, slot);
640 input_mt_report_slot_state(dev, MT_TOOL_FINGER, (hw != NULL));
641 if (!hw)
642 return;
643
644 input_report_abs(dev, ABS_MT_POSITION_X, hw->x);
645 input_report_abs(dev, ABS_MT_POSITION_Y, synaptics_invert_y(hw->y));
646 input_report_abs(dev, ABS_MT_PRESSURE, hw->z);
647}
648
649static void synaptics_report_mt_data(struct psmouse *psmouse,
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700650 struct synaptics_mt_state *mt_state,
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700651 const struct synaptics_hw_state *sgm)
652{
653 struct input_dev *dev = psmouse->dev;
654 struct synaptics_data *priv = psmouse->private;
655 struct synaptics_hw_state *agm = &priv->agm;
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700656 struct synaptics_mt_state *old = &priv->mt_state;
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700657
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700658 switch (mt_state->count) {
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700659 case 0:
660 synaptics_report_slot(dev, 0, NULL);
661 synaptics_report_slot(dev, 1, NULL);
662 break;
663 case 1:
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700664 if (mt_state->sgm == -1) {
665 synaptics_report_slot(dev, 0, NULL);
666 synaptics_report_slot(dev, 1, NULL);
667 } else if (mt_state->sgm == 0) {
668 synaptics_report_slot(dev, 0, sgm);
669 synaptics_report_slot(dev, 1, NULL);
670 } else {
671 synaptics_report_slot(dev, 0, NULL);
672 synaptics_report_slot(dev, 1, sgm);
673 }
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700674 break;
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700675 default:
676 /*
677 * If the finger slot contained in SGM is valid, and either
678 * hasn't changed, or is new, then report SGM in MTB slot 0.
679 * Otherwise, empty MTB slot 0.
680 */
681 if (mt_state->sgm != -1 &&
682 (mt_state->sgm == old->sgm || old->sgm == -1))
683 synaptics_report_slot(dev, 0, sgm);
684 else
685 synaptics_report_slot(dev, 0, NULL);
686
687 /*
688 * If the finger slot contained in AGM is valid, and either
689 * hasn't changed, or is new, then report AGM in MTB slot 1.
690 * Otherwise, empty MTB slot 1.
691 */
692 if (mt_state->agm != -1 &&
693 (mt_state->agm == old->agm || old->agm == -1))
694 synaptics_report_slot(dev, 1, agm);
695 else
696 synaptics_report_slot(dev, 1, NULL);
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700697 break;
698 }
699
700 /* Don't use active slot count to generate BTN_TOOL events. */
701 input_mt_report_pointer_emulation(dev, false);
702
703 /* Send the number of fingers reported by touchpad itself. */
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700704 input_mt_report_finger_count(dev, mt_state->count);
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700705
706 synaptics_report_buttons(psmouse, sgm);
707
708 input_sync(dev);
709}
710
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700711/* Handle case where mt_state->count = 0 */
712static void synaptics_image_sensor_0f(struct synaptics_data *priv,
713 struct synaptics_mt_state *mt_state)
714{
715 synaptics_mt_state_set(mt_state, 0, -1, -1);
716 priv->mt_state_lost = false;
717}
718
719/* Handle case where mt_state->count = 1 */
720static void synaptics_image_sensor_1f(struct synaptics_data *priv,
721 struct synaptics_mt_state *mt_state)
722{
723 struct synaptics_hw_state *agm = &priv->agm;
724 struct synaptics_mt_state *old = &priv->mt_state;
725
726 /*
727 * If the last AGM was (0,0,0), and there is only one finger left,
728 * then we absolutely know that SGM contains slot 0, and all other
729 * fingers have been removed.
730 */
731 if (priv->agm_pending && agm->z == 0) {
732 synaptics_mt_state_set(mt_state, 1, 0, -1);
733 priv->mt_state_lost = false;
734 return;
735 }
736
737 switch (old->count) {
738 case 0:
739 synaptics_mt_state_set(mt_state, 1, 0, -1);
740 break;
741 case 1:
742 /*
743 * If mt_state_lost, then the previous transition was 3->1,
744 * and SGM now contains either slot 0 or 1, but we don't know
745 * which. So, we just assume that the SGM now contains slot 1.
746 *
747 * If pending AGM and either:
748 * (a) the previous SGM slot contains slot 0, or
749 * (b) there was no SGM slot
750 * then, the SGM now contains slot 1
751 *
752 * Case (a) happens with very rapid "drum roll" gestures, where
753 * slot 0 finger is lifted and a new slot 1 finger touches
754 * within one reporting interval.
755 *
756 * Case (b) happens if initially two or more fingers tap
757 * briefly, and all but one lift before the end of the first
758 * reporting interval.
759 *
760 * (In both these cases, slot 0 will becomes empty, so SGM
761 * contains slot 1 with the new finger)
762 *
763 * Else, if there was no previous SGM, it now contains slot 0.
764 *
765 * Otherwise, SGM still contains the same slot.
766 */
767 if (priv->mt_state_lost ||
768 (priv->agm_pending && old->sgm <= 0))
769 synaptics_mt_state_set(mt_state, 1, 1, -1);
770 else if (old->sgm == -1)
771 synaptics_mt_state_set(mt_state, 1, 0, -1);
772 break;
773 case 2:
774 /*
775 * If mt_state_lost, we don't know which finger SGM contains.
776 *
777 * So, report 1 finger, but with both slots empty.
778 * We will use slot 1 on subsequent 1->1
779 */
780 if (priv->mt_state_lost) {
781 synaptics_mt_state_set(mt_state, 1, -1, -1);
782 break;
783 }
784 /*
785 * Since the last AGM was NOT (0,0,0), it was the finger in
786 * slot 0 that has been removed.
787 * So, SGM now contains previous AGM's slot, and AGM is now
788 * empty.
789 */
790 synaptics_mt_state_set(mt_state, 1, old->agm, -1);
791 break;
792 case 3:
793 /*
794 * Since last AGM was not (0,0,0), we don't know which finger
795 * is left.
796 *
797 * So, report 1 finger, but with both slots empty.
798 * We will use slot 1 on subsequent 1->1
799 */
800 synaptics_mt_state_set(mt_state, 1, -1, -1);
801 priv->mt_state_lost = true;
802 break;
Daniel Kurtz6b4b49f2011-08-23 23:02:56 -0700803 case 4:
804 case 5:
805 /* mt_state was updated by AGM-CONTACT packet */
806 break;
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700807 }
808}
809
810/* Handle case where mt_state->count = 2 */
811static void synaptics_image_sensor_2f(struct synaptics_data *priv,
812 struct synaptics_mt_state *mt_state)
813{
814 struct synaptics_mt_state *old = &priv->mt_state;
815
816 switch (old->count) {
817 case 0:
818 synaptics_mt_state_set(mt_state, 2, 0, 1);
819 break;
820 case 1:
821 /*
822 * If previous SGM contained slot 1 or higher, SGM now contains
823 * slot 0 (the newly touching finger) and AGM contains SGM's
824 * previous slot.
825 *
826 * Otherwise, SGM still contains slot 0 and AGM now contains
827 * slot 1.
828 */
829 if (old->sgm >= 1)
830 synaptics_mt_state_set(mt_state, 2, 0, old->sgm);
831 else
832 synaptics_mt_state_set(mt_state, 2, 0, 1);
833 break;
834 case 2:
835 /*
836 * If mt_state_lost, SGM now contains either finger 1 or 2, but
837 * we don't know which.
838 * So, we just assume that the SGM contains slot 0 and AGM 1.
839 */
840 if (priv->mt_state_lost)
841 synaptics_mt_state_set(mt_state, 2, 0, 1);
842 /*
843 * Otherwise, use the same mt_state, since it either hasn't
844 * changed, or was updated by a recently received AGM-CONTACT
845 * packet.
846 */
847 break;
848 case 3:
849 /*
850 * 3->2 transitions have two unsolvable problems:
851 * 1) no indication is given which finger was removed
852 * 2) no way to tell if agm packet was for finger 3
853 * before 3->2, or finger 2 after 3->2.
854 *
855 * So, report 2 fingers, but empty all slots.
856 * We will guess slots [0,1] on subsequent 2->2.
857 */
858 synaptics_mt_state_set(mt_state, 2, -1, -1);
859 priv->mt_state_lost = true;
860 break;
Daniel Kurtz6b4b49f2011-08-23 23:02:56 -0700861 case 4:
862 case 5:
863 /* mt_state was updated by AGM-CONTACT packet */
864 break;
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700865 }
866}
867
868/* Handle case where mt_state->count = 3 */
869static void synaptics_image_sensor_3f(struct synaptics_data *priv,
870 struct synaptics_mt_state *mt_state)
871{
872 struct synaptics_mt_state *old = &priv->mt_state;
873
874 switch (old->count) {
875 case 0:
876 synaptics_mt_state_set(mt_state, 3, 0, 2);
877 break;
878 case 1:
879 /*
880 * If previous SGM contained slot 2 or higher, SGM now contains
881 * slot 0 (one of the newly touching fingers) and AGM contains
882 * SGM's previous slot.
883 *
884 * Otherwise, SGM now contains slot 0 and AGM contains slot 2.
885 */
886 if (old->sgm >= 2)
887 synaptics_mt_state_set(mt_state, 3, 0, old->sgm);
888 else
889 synaptics_mt_state_set(mt_state, 3, 0, 2);
890 break;
891 case 2:
892 /*
Daniel Kurtz6b4b49f2011-08-23 23:02:56 -0700893 * If the AGM previously contained slot 3 or higher, then the
894 * newly touching finger is in the lowest available slot.
895 *
896 * If SGM was previously 1 or higher, then the new SGM is
897 * now slot 0 (with a new finger), otherwise, the new finger
898 * is now in a hidden slot between 0 and AGM's slot.
899 *
900 * In all such cases, the SGM now contains slot 0, and the AGM
901 * continues to contain the same slot as before.
902 */
903 if (old->agm >= 3) {
904 synaptics_mt_state_set(mt_state, 3, 0, old->agm);
905 break;
906 }
907
908 /*
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700909 * After some 3->1 and all 3->2 transitions, we lose track
910 * of which slot is reported by SGM and AGM.
911 *
912 * For 2->3 in this state, report 3 fingers, but empty all
913 * slots, and we will guess (0,2) on a subsequent 0->3.
914 *
915 * To userspace, the resulting transition will look like:
916 * 2:[0,1] -> 3:[-1,-1] -> 3:[0,2]
917 */
918 if (priv->mt_state_lost) {
919 synaptics_mt_state_set(mt_state, 3, -1, -1);
920 break;
921 }
922
923 /*
924 * If the (SGM,AGM) really previously contained slots (0, 1),
925 * then we cannot know what slot was just reported by the AGM,
926 * because the 2->3 transition can occur either before or after
927 * the AGM packet. Thus, this most recent AGM could contain
928 * either the same old slot 1 or the new slot 2.
929 * Subsequent AGMs will be reporting slot 2.
930 *
931 * To userspace, the resulting transition will look like:
932 * 2:[0,1] -> 3:[0,-1] -> 3:[0,2]
933 */
934 synaptics_mt_state_set(mt_state, 3, 0, -1);
935 break;
936 case 3:
937 /*
938 * If, for whatever reason, the previous agm was invalid,
939 * Assume SGM now contains slot 0, AGM now contains slot 2.
940 */
941 if (old->agm <= 2)
942 synaptics_mt_state_set(mt_state, 3, 0, 2);
943 /*
944 * mt_state either hasn't changed, or was updated by a recently
945 * received AGM-CONTACT packet.
946 */
947 break;
Daniel Kurtz6b4b49f2011-08-23 23:02:56 -0700948
949 case 4:
950 case 5:
951 /* mt_state was updated by AGM-CONTACT packet */
952 break;
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700953 }
954}
955
Daniel Kurtz6b4b49f2011-08-23 23:02:56 -0700956/* Handle case where mt_state->count = 4, or = 5 */
957static void synaptics_image_sensor_45f(struct synaptics_data *priv,
958 struct synaptics_mt_state *mt_state)
959{
960 /* mt_state was updated correctly by AGM-CONTACT packet */
961 priv->mt_state_lost = false;
962}
963
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700964static void synaptics_image_sensor_process(struct psmouse *psmouse,
965 struct synaptics_hw_state *sgm)
966{
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700967 struct synaptics_data *priv = psmouse->private;
968 struct synaptics_hw_state *agm = &priv->agm;
969 struct synaptics_mt_state mt_state;
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700970
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700971 /* Initialize using current mt_state (as updated by last agm) */
972 mt_state = agm->mt_state;
973
974 /*
975 * Update mt_state using the new finger count and current mt_state.
976 */
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700977 if (sgm->z == 0)
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700978 synaptics_image_sensor_0f(priv, &mt_state);
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700979 else if (sgm->w >= 4)
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700980 synaptics_image_sensor_1f(priv, &mt_state);
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700981 else if (sgm->w == 0)
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700982 synaptics_image_sensor_2f(priv, &mt_state);
Daniel Kurtz6b4b49f2011-08-23 23:02:56 -0700983 else if (sgm->w == 1 && mt_state.count <= 3)
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700984 synaptics_image_sensor_3f(priv, &mt_state);
Daniel Kurtz6b4b49f2011-08-23 23:02:56 -0700985 else
986 synaptics_image_sensor_45f(priv, &mt_state);
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700987
988 /* Send resulting input events to user space */
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700989 synaptics_report_mt_data(psmouse, &mt_state, sgm);
990
991 /* Store updated mt_state */
992 priv->mt_state = agm->mt_state = mt_state;
993 priv->agm_pending = false;
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700994}
995
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996/*
997 * called for each full received packet from the touchpad
998 */
999static void synaptics_process_packet(struct psmouse *psmouse)
1000{
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001001 struct input_dev *dev = psmouse->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 struct synaptics_data *priv = psmouse->private;
1003 struct synaptics_hw_state hw;
1004 int num_fingers;
1005 int finger_width;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Henrik Rydbergfec6e522010-12-21 18:11:25 +01001007 if (synaptics_parse_hw_state(psmouse->packet, priv, &hw))
1008 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
Daniel Kurtz3cdfee92011-08-23 23:02:25 -07001010 if (SYN_CAP_IMAGE_SENSOR(priv->ext_cap_0c)) {
1011 synaptics_image_sensor_process(psmouse, &hw);
1012 return;
1013 }
1014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 if (hw.scroll) {
1016 priv->scroll += hw.scroll;
1017
1018 while (priv->scroll >= 4) {
1019 input_report_key(dev, BTN_BACK, !hw.down);
1020 input_sync(dev);
1021 input_report_key(dev, BTN_BACK, hw.down);
1022 input_sync(dev);
1023 priv->scroll -= 4;
1024 }
1025 while (priv->scroll <= -4) {
1026 input_report_key(dev, BTN_FORWARD, !hw.up);
1027 input_sync(dev);
1028 input_report_key(dev, BTN_FORWARD, hw.up);
1029 input_sync(dev);
1030 priv->scroll += 4;
1031 }
1032 return;
1033 }
1034
Henrik Rydberg4f56ce92010-12-18 15:42:30 +01001035 if (hw.z > 0 && hw.x > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 num_fingers = 1;
1037 finger_width = 5;
1038 if (SYN_CAP_EXTENDED(priv->capabilities)) {
1039 switch (hw.w) {
1040 case 0 ... 1:
1041 if (SYN_CAP_MULTIFINGER(priv->capabilities))
1042 num_fingers = hw.w + 2;
1043 break;
1044 case 2:
1045 if (SYN_MODEL_PEN(priv->model_id))
1046 ; /* Nothing, treat a pen as a single finger */
1047 break;
1048 case 4 ... 15:
1049 if (SYN_CAP_PALMDETECT(priv->capabilities))
1050 finger_width = hw.w;
1051 break;
1052 }
1053 }
1054 } else {
1055 num_fingers = 0;
1056 finger_width = 0;
1057 }
1058
Henrik Rydbergfec6e522010-12-21 18:11:25 +01001059 if (SYN_CAP_ADV_GESTURE(priv->ext_cap_0c))
Daniel Kurtz7afdb842011-08-23 23:00:33 -07001060 synaptics_report_semi_mt_data(dev, &hw, &priv->agm,
1061 num_fingers);
Henrik Rydbergfec6e522010-12-21 18:11:25 +01001062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 /* Post events
1064 * BTN_TOUCH has to be first as mousedev relies on it when doing
1065 * absolute -> relative conversion
1066 */
1067 if (hw.z > 30) input_report_key(dev, BTN_TOUCH, 1);
1068 if (hw.z < 25) input_report_key(dev, BTN_TOUCH, 0);
1069
Henrik Rydberg4f56ce92010-12-18 15:42:30 +01001070 if (num_fingers > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 input_report_abs(dev, ABS_X, hw.x);
Daniel Kurtz6de58dd2011-08-23 23:00:24 -07001072 input_report_abs(dev, ABS_Y, synaptics_invert_y(hw.y));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 }
1074 input_report_abs(dev, ABS_PRESSURE, hw.z);
1075
Chris Bagwell2a8e7712010-07-19 09:06:15 -07001076 if (SYN_CAP_PALMDETECT(priv->capabilities))
1077 input_report_abs(dev, ABS_TOOL_WIDTH, finger_width);
1078
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 input_report_key(dev, BTN_TOOL_FINGER, num_fingers == 1);
Peter Hutterere42b6642008-11-20 15:24:42 -05001080 if (SYN_CAP_MULTIFINGER(priv->capabilities)) {
1081 input_report_key(dev, BTN_TOOL_DOUBLETAP, num_fingers == 2);
1082 input_report_key(dev, BTN_TOOL_TRIPLETAP, num_fingers == 3);
1083 }
1084
Daniel Kurtz3cdfee92011-08-23 23:02:25 -07001085 synaptics_report_buttons(psmouse, &hw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
1087 input_sync(dev);
1088}
1089
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001090static int synaptics_validate_byte(struct psmouse *psmouse,
1091 int idx, unsigned char pkt_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
Helge Dellere38de672006-09-10 21:54:39 -04001093 static const unsigned char newabs_mask[] = { 0xC8, 0x00, 0x00, 0xC8, 0x00 };
1094 static const unsigned char newabs_rel_mask[] = { 0xC0, 0x00, 0x00, 0xC0, 0x00 };
1095 static const unsigned char newabs_rslt[] = { 0x80, 0x00, 0x00, 0xC0, 0x00 };
1096 static const unsigned char oldabs_mask[] = { 0xC0, 0x60, 0x00, 0xC0, 0x60 };
1097 static const unsigned char oldabs_rslt[] = { 0xC0, 0x00, 0x00, 0x80, 0x00 };
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001098 const char *packet = psmouse->packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
1100 if (idx < 0 || idx > 4)
1101 return 0;
1102
1103 switch (pkt_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Dmitry Torokhova62f0d22010-05-19 10:39:17 -07001105 case SYN_NEWABS:
1106 case SYN_NEWABS_RELAXED:
1107 return (packet[idx] & newabs_rel_mask[idx]) == newabs_rslt[idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Dmitry Torokhova62f0d22010-05-19 10:39:17 -07001109 case SYN_NEWABS_STRICT:
1110 return (packet[idx] & newabs_mask[idx]) == newabs_rslt[idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
Dmitry Torokhova62f0d22010-05-19 10:39:17 -07001112 case SYN_OLDABS:
1113 return (packet[idx] & oldabs_mask[idx]) == oldabs_rslt[idx];
1114
1115 default:
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001116 psmouse_err(psmouse, "unknown packet type %d\n", pkt_type);
Dmitry Torokhova62f0d22010-05-19 10:39:17 -07001117 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 }
1119}
1120
1121static unsigned char synaptics_detect_pkt_type(struct psmouse *psmouse)
1122{
1123 int i;
1124
1125 for (i = 0; i < 5; i++)
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001126 if (!synaptics_validate_byte(psmouse, i, SYN_NEWABS_STRICT)) {
1127 psmouse_info(psmouse, "using relaxed packet validation\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 return SYN_NEWABS_RELAXED;
1129 }
1130
1131 return SYN_NEWABS_STRICT;
1132}
1133
David Howells7d12e782006-10-05 14:55:46 +01001134static psmouse_ret_t synaptics_process_byte(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 struct synaptics_data *priv = psmouse->private;
1137
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 if (psmouse->pktcnt >= 6) { /* Full packet received */
1139 if (unlikely(priv->pkt_type == SYN_NEWABS))
1140 priv->pkt_type = synaptics_detect_pkt_type(psmouse);
1141
Dmitry Torokhova8b3c0f2010-10-04 21:46:10 -07001142 if (SYN_CAP_PASS_THROUGH(priv->capabilities) &&
1143 synaptics_is_pt_packet(psmouse->packet)) {
1144 if (priv->pt_port)
1145 synaptics_pass_pt_packet(priv->pt_port, psmouse->packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 } else
1147 synaptics_process_packet(psmouse);
1148
1149 return PSMOUSE_FULL_PACKET;
1150 }
1151
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001152 return synaptics_validate_byte(psmouse, psmouse->pktcnt - 1, priv->pkt_type) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 PSMOUSE_GOOD_DATA : PSMOUSE_BAD_DATA;
1154}
1155
1156/*****************************************************************************
1157 * Driver initialization/cleanup functions
1158 ****************************************************************************/
Daniel Kurtz85615472011-08-23 23:00:41 -07001159static void set_abs_position_params(struct input_dev *dev,
1160 struct synaptics_data *priv, int x_code,
1161 int y_code)
1162{
1163 int x_min = priv->x_min ?: XMIN_NOMINAL;
1164 int x_max = priv->x_max ?: XMAX_NOMINAL;
1165 int y_min = priv->y_min ?: YMIN_NOMINAL;
1166 int y_max = priv->y_max ?: YMAX_NOMINAL;
1167 int fuzz = SYN_CAP_REDUCED_FILTERING(priv->ext_cap_0c) ?
1168 SYN_REDUCED_FILTER_FUZZ : 0;
1169
1170 input_set_abs_params(dev, x_code, x_min, x_max, fuzz, 0);
1171 input_set_abs_params(dev, y_code, y_min, y_max, fuzz, 0);
1172 input_abs_set_res(dev, x_code, priv->x_res);
1173 input_abs_set_res(dev, y_code, priv->y_res);
1174}
1175
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176static void set_input_params(struct input_dev *dev, struct synaptics_data *priv)
1177{
1178 int i;
1179
Daniel Drake7968a5d2011-11-08 00:00:35 -08001180 /* Things that apply to both modes */
Henrik Rydbergc14890a82010-12-16 09:52:23 +01001181 __set_bit(INPUT_PROP_POINTER, dev->propbit);
Daniel Drake7968a5d2011-11-08 00:00:35 -08001182 __set_bit(EV_KEY, dev->evbit);
1183 __set_bit(BTN_LEFT, dev->keybit);
1184 __set_bit(BTN_RIGHT, dev->keybit);
Henrik Rydbergc14890a82010-12-16 09:52:23 +01001185
Daniel Drake7968a5d2011-11-08 00:00:35 -08001186 if (SYN_CAP_MIDDLE_BUTTON(priv->capabilities))
1187 __set_bit(BTN_MIDDLE, dev->keybit);
1188
1189 if (!priv->absolute_mode) {
1190 /* Relative mode */
1191 __set_bit(EV_REL, dev->evbit);
1192 __set_bit(REL_X, dev->relbit);
1193 __set_bit(REL_Y, dev->relbit);
1194 return;
1195 }
1196
1197 /* Absolute mode */
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001198 __set_bit(EV_ABS, dev->evbit);
Daniel Kurtz85615472011-08-23 23:00:41 -07001199 set_abs_position_params(dev, priv, ABS_X, ABS_Y);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 input_set_abs_params(dev, ABS_PRESSURE, 0, 255, 0, 0);
Chris Bagwell2a8e7712010-07-19 09:06:15 -07001201
Daniel Kurtz3cdfee92011-08-23 23:02:25 -07001202 if (SYN_CAP_IMAGE_SENSOR(priv->ext_cap_0c)) {
1203 input_mt_init_slots(dev, 2);
1204 set_abs_position_params(dev, priv, ABS_MT_POSITION_X,
1205 ABS_MT_POSITION_Y);
1206 /* Image sensors can report per-contact pressure */
1207 input_set_abs_params(dev, ABS_MT_PRESSURE, 0, 255, 0, 0);
Daniel Kurtz6b4b49f2011-08-23 23:02:56 -07001208
1209 /* Image sensors can signal 4 and 5 finger clicks */
1210 __set_bit(BTN_TOOL_QUADTAP, dev->keybit);
1211 __set_bit(BTN_TOOL_QUINTTAP, dev->keybit);
Daniel Kurtz3cdfee92011-08-23 23:02:25 -07001212 } else if (SYN_CAP_ADV_GESTURE(priv->ext_cap_0c)) {
1213 /* Non-image sensors with AGM use semi-mt */
Henrik Rydbergfec6e522010-12-21 18:11:25 +01001214 __set_bit(INPUT_PROP_SEMI_MT, dev->propbit);
1215 input_mt_init_slots(dev, 2);
Daniel Kurtz85615472011-08-23 23:00:41 -07001216 set_abs_position_params(dev, priv, ABS_MT_POSITION_X,
1217 ABS_MT_POSITION_Y);
Henrik Rydbergfec6e522010-12-21 18:11:25 +01001218 }
1219
Chris Bagwell2a8e7712010-07-19 09:06:15 -07001220 if (SYN_CAP_PALMDETECT(priv->capabilities))
Chris Bagwell58fb0212010-07-19 09:06:15 -07001221 input_set_abs_params(dev, ABS_TOOL_WIDTH, 0, 15, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001223 __set_bit(BTN_TOUCH, dev->keybit);
1224 __set_bit(BTN_TOOL_FINGER, dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Peter Hutterere42b6642008-11-20 15:24:42 -05001226 if (SYN_CAP_MULTIFINGER(priv->capabilities)) {
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001227 __set_bit(BTN_TOOL_DOUBLETAP, dev->keybit);
1228 __set_bit(BTN_TOOL_TRIPLETAP, dev->keybit);
Peter Hutterere42b6642008-11-20 15:24:42 -05001229 }
1230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 if (SYN_CAP_FOUR_BUTTON(priv->capabilities) ||
1232 SYN_CAP_MIDDLE_BUTTON(priv->capabilities)) {
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001233 __set_bit(BTN_FORWARD, dev->keybit);
1234 __set_bit(BTN_BACK, dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 }
1236
1237 for (i = 0; i < SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap); i++)
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001238 __set_bit(BTN_0 + i, dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001240 __clear_bit(EV_REL, dev->evbit);
1241 __clear_bit(REL_X, dev->relbit);
1242 __clear_bit(REL_Y, dev->relbit);
Tero Saarniec20a022009-06-10 23:27:24 -07001243
Takashi Iwai5f57d672010-04-19 10:37:21 -07001244 if (SYN_CAP_CLICKPAD(priv->ext_cap_0c)) {
Henrik Rydbergc14890a82010-12-16 09:52:23 +01001245 __set_bit(INPUT_PROP_BUTTONPAD, dev->propbit);
Takashi Iwai5f57d672010-04-19 10:37:21 -07001246 /* Clickpads report only left button */
1247 __clear_bit(BTN_RIGHT, dev->keybit);
1248 __clear_bit(BTN_MIDDLE, dev->keybit);
1249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250}
1251
Daniel Drake7968a5d2011-11-08 00:00:35 -08001252static ssize_t synaptics_show_disable_gesture(struct psmouse *psmouse,
1253 void *data, char *buf)
1254{
1255 struct synaptics_data *priv = psmouse->private;
1256
1257 return sprintf(buf, "%c\n", priv->disable_gesture ? '1' : '0');
1258}
1259
1260static ssize_t synaptics_set_disable_gesture(struct psmouse *psmouse,
1261 void *data, const char *buf,
1262 size_t len)
1263{
1264 struct synaptics_data *priv = psmouse->private;
1265 unsigned int value;
1266 int err;
1267
1268 err = kstrtouint(buf, 10, &value);
1269 if (err)
1270 return err;
1271
1272 if (value > 1)
1273 return -EINVAL;
1274
1275 if (value == priv->disable_gesture)
1276 return len;
1277
1278 priv->disable_gesture = value;
1279 if (value)
1280 priv->mode |= SYN_BIT_DISABLE_GESTURE;
1281 else
1282 priv->mode &= ~SYN_BIT_DISABLE_GESTURE;
1283
1284 if (synaptics_mode_cmd(psmouse, priv->mode))
1285 return -EIO;
1286
1287 return len;
1288}
1289
1290PSMOUSE_DEFINE_ATTR(disable_gesture, S_IWUSR | S_IRUGO, NULL,
1291 synaptics_show_disable_gesture,
1292 synaptics_set_disable_gesture);
1293
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294static void synaptics_disconnect(struct psmouse *psmouse)
1295{
Daniel Drake7968a5d2011-11-08 00:00:35 -08001296 struct synaptics_data *priv = psmouse->private;
1297
1298 if (!priv->absolute_mode && SYN_ID_DISGEST_SUPPORTED(priv->identity))
1299 device_remove_file(&psmouse->ps2dev.serio->dev,
1300 &psmouse_attr_disable_gesture.dattr);
1301
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 synaptics_reset(psmouse);
Daniel Drake7968a5d2011-11-08 00:00:35 -08001303 kfree(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 psmouse->private = NULL;
1305}
1306
1307static int synaptics_reconnect(struct psmouse *psmouse)
1308{
1309 struct synaptics_data *priv = psmouse->private;
1310 struct synaptics_data old_priv = *priv;
Alexandre Peixoto Ferreirac63fe0a2011-01-28 22:05:14 -08001311 int retry = 0;
1312 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
Alexandre Peixoto Ferreirac63fe0a2011-01-28 22:05:14 -08001314 do {
1315 psmouse_reset(psmouse);
Dmitry Torokhov85214782011-12-12 00:05:53 -08001316 if (retry) {
1317 /*
1318 * On some boxes, right after resuming, the touchpad
1319 * needs some time to finish initializing (I assume
1320 * it needs time to calibrate) and start responding
1321 * to Synaptics-specific queries, so let's wait a
1322 * bit.
1323 */
1324 ssleep(1);
1325 }
Alexandre Peixoto Ferreirac63fe0a2011-01-28 22:05:14 -08001326 error = synaptics_detect(psmouse, 0);
1327 } while (error && ++retry < 3);
Andy Whitcroft4d368452009-02-28 12:51:01 -08001328
Alexandre Peixoto Ferreirac63fe0a2011-01-28 22:05:14 -08001329 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 return -1;
1331
Alexandre Peixoto Ferreirac63fe0a2011-01-28 22:05:14 -08001332 if (retry > 1)
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001333 psmouse_dbg(psmouse, "reconnected after %d tries\n", retry);
Alexandre Peixoto Ferreirac63fe0a2011-01-28 22:05:14 -08001334
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 if (synaptics_query_hardware(psmouse)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001336 psmouse_err(psmouse, "Unable to query device.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 return -1;
1338 }
1339
Daniel Drake7968a5d2011-11-08 00:00:35 -08001340 if (synaptics_set_mode(psmouse)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001341 psmouse_err(psmouse, "Unable to initialize device.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 return -1;
1343 }
1344
Alexandre Peixoto Ferreirabaddf582011-01-28 22:05:14 -08001345 if (old_priv.identity != priv->identity ||
1346 old_priv.model_id != priv->model_id ||
1347 old_priv.capabilities != priv->capabilities ||
1348 old_priv.ext_cap != priv->ext_cap) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001349 psmouse_err(psmouse,
1350 "hardware appears to be different: id(%ld-%ld), model(%ld-%ld), caps(%lx-%lx), ext(%lx-%lx).\n",
1351 old_priv.identity, priv->identity,
1352 old_priv.model_id, priv->model_id,
1353 old_priv.capabilities, priv->capabilities,
1354 old_priv.ext_cap, priv->ext_cap);
Alexandre Peixoto Ferreirabaddf582011-01-28 22:05:14 -08001355 return -1;
1356 }
1357
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 return 0;
1359}
1360
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001361static bool impaired_toshiba_kbc;
1362
1363static const struct dmi_system_id __initconst toshiba_dmi_table[] = {
1364#if defined(CONFIG_DMI) && defined(CONFIG_X86)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 {
Dmitry Torokhov9961e252009-12-04 10:24:20 -08001366 /* Toshiba Satellite */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 .matches = {
1368 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
Richard Thrippleton53a26702006-04-02 00:10:18 -05001369 DMI_MATCH(DMI_PRODUCT_NAME, "Satellite"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 },
1371 },
Simon Horman9ba5eaa2005-07-11 01:07:20 -05001372 {
Dmitry Torokhov9961e252009-12-04 10:24:20 -08001373 /* Toshiba Dynabook */
Simon Horman9ba5eaa2005-07-11 01:07:20 -05001374 .matches = {
1375 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
Richard Thrippleton53a26702006-04-02 00:10:18 -05001376 DMI_MATCH(DMI_PRODUCT_NAME, "dynabook"),
1377 },
1378 },
1379 {
Dmitry Torokhov9961e252009-12-04 10:24:20 -08001380 /* Toshiba Portege M300 */
Richard Thrippleton53a26702006-04-02 00:10:18 -05001381 .matches = {
1382 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1383 DMI_MATCH(DMI_PRODUCT_NAME, "PORTEGE M300"),
Simon Horman9ba5eaa2005-07-11 01:07:20 -05001384 },
Dmitry Torokhov5f5eeff2009-10-12 21:35:00 -07001385
1386 },
1387 {
Dmitry Torokhov9961e252009-12-04 10:24:20 -08001388 /* Toshiba Portege M300 */
Dmitry Torokhov5f5eeff2009-10-12 21:35:00 -07001389 .matches = {
1390 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1391 DMI_MATCH(DMI_PRODUCT_NAME, "Portable PC"),
1392 DMI_MATCH(DMI_PRODUCT_VERSION, "Version 1.0"),
1393 },
1394
Simon Horman9ba5eaa2005-07-11 01:07:20 -05001395 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396#endif
Jan Beulich70874862011-03-31 00:01:58 -07001397 { }
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001398};
1399
Andres Salomonef8313b2010-12-23 01:19:38 -08001400static bool broken_olpc_ec;
1401
1402static const struct dmi_system_id __initconst olpc_dmi_table[] = {
1403#if defined(CONFIG_DMI) && defined(CONFIG_OLPC)
1404 {
1405 /* OLPC XO-1 or XO-1.5 */
1406 .matches = {
1407 DMI_MATCH(DMI_SYS_VENDOR, "OLPC"),
1408 DMI_MATCH(DMI_PRODUCT_NAME, "XO"),
1409 },
1410 },
Andres Salomonef8313b2010-12-23 01:19:38 -08001411#endif
Jan Beulich70874862011-03-31 00:01:58 -07001412 { }
Andres Salomonef8313b2010-12-23 01:19:38 -08001413};
1414
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001415void __init synaptics_module_init(void)
1416{
1417 impaired_toshiba_kbc = dmi_check_system(toshiba_dmi_table);
Andres Salomonef8313b2010-12-23 01:19:38 -08001418 broken_olpc_ec = dmi_check_system(olpc_dmi_table);
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001419}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
Daniel Drake7968a5d2011-11-08 00:00:35 -08001421static int __synaptics_init(struct psmouse *psmouse, bool absolute_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422{
1423 struct synaptics_data *priv;
Daniel Drake7968a5d2011-11-08 00:00:35 -08001424 int err = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
Andres Salomonef8313b2010-12-23 01:19:38 -08001426 /*
Daniel Drake83551c02011-11-11 16:05:04 -08001427 * The OLPC XO has issues with Synaptics' absolute mode; the constant
1428 * packet spew overloads the EC such that key presses on the keyboard
1429 * are missed. Given that, don't even attempt to use Absolute mode.
1430 * Relative mode seems to work just fine.
Andres Salomonef8313b2010-12-23 01:19:38 -08001431 */
Daniel Drake83551c02011-11-11 16:05:04 -08001432 if (absolute_mode && broken_olpc_ec) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001433 psmouse_info(psmouse,
1434 "OLPC XO detected, not enabling Synaptics protocol.\n");
Andres Salomonef8313b2010-12-23 01:19:38 -08001435 return -ENODEV;
1436 }
1437
Eric Sesterhennb39787a2006-03-14 00:09:16 -05001438 psmouse->private = priv = kzalloc(sizeof(struct synaptics_data), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 if (!priv)
Davidlohr Bueso6792cbb2010-09-29 18:53:35 -07001440 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Andy Whitcroft4d368452009-02-28 12:51:01 -08001442 psmouse_reset(psmouse);
1443
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 if (synaptics_query_hardware(psmouse)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001445 psmouse_err(psmouse, "Unable to query device.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 goto init_fail;
1447 }
1448
Daniel Drake7968a5d2011-11-08 00:00:35 -08001449 priv->absolute_mode = absolute_mode;
1450 if (SYN_ID_DISGEST_SUPPORTED(priv->identity))
1451 priv->disable_gesture = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Daniel Drake7968a5d2011-11-08 00:00:35 -08001453 if (synaptics_set_mode(psmouse)) {
1454 psmouse_err(psmouse, "Unable to initialize device.\n");
Henrik Rydbergfec6e522010-12-21 18:11:25 +01001455 goto init_fail;
1456 }
1457
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 priv->pkt_type = SYN_MODEL_NEWABS(priv->model_id) ? SYN_NEWABS : SYN_OLDABS;
1459
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001460 psmouse_info(psmouse,
1461 "Touchpad model: %ld, fw: %ld.%ld, id: %#lx, caps: %#lx/%#lx/%#lx\n",
1462 SYN_ID_MODEL(priv->identity),
1463 SYN_ID_MAJOR(priv->identity), SYN_ID_MINOR(priv->identity),
1464 priv->model_id,
1465 priv->capabilities, priv->ext_cap, priv->ext_cap_0c);
Dmitry Torokhov409b7502005-05-28 02:12:18 -05001466
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001467 set_input_params(psmouse->dev, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
Dmitry Torokhov887cc122007-04-12 01:30:41 -04001469 /*
1470 * Encode touchpad model so that it can be used to set
1471 * input device->id.version and be visible to userspace.
1472 * Because version is __u16 we have to drop something.
1473 * Hardware info bits seem to be good candidates as they
1474 * are documented to be for Synaptics corp. internal use.
1475 */
1476 psmouse->model = ((priv->model_id & 0x00ff0000) >> 8) |
1477 (priv->model_id & 0x000000ff);
1478
Daniel Drake7968a5d2011-11-08 00:00:35 -08001479 if (absolute_mode) {
1480 psmouse->protocol_handler = synaptics_process_byte;
1481 psmouse->pktsize = 6;
1482 } else {
1483 /* Relative mode follows standard PS/2 mouse protocol */
1484 psmouse->protocol_handler = psmouse_process_byte;
1485 psmouse->pktsize = 3;
1486 }
1487
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 psmouse->set_rate = synaptics_set_rate;
1489 psmouse->disconnect = synaptics_disconnect;
1490 psmouse->reconnect = synaptics_reconnect;
Dmitry Torokhova1cec062007-02-18 01:40:24 -05001491 psmouse->cleanup = synaptics_reset;
Dmitry Torokhovf0d5c6f2006-01-14 00:27:37 -05001492 /* Synaptics can usually stay in sync without extra help */
1493 psmouse->resync_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
1495 if (SYN_CAP_PASS_THROUGH(priv->capabilities))
1496 synaptics_pt_create(psmouse);
1497
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 /*
1499 * Toshiba's KBC seems to have trouble handling data from
Andres Salomon7ee99162010-12-23 01:18:28 -08001500 * Synaptics at full rate. Switch to a lower rate (roughly
1501 * the same rate as a standard PS/2 mouse).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 */
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001503 if (psmouse->rate >= 80 && impaired_toshiba_kbc) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001504 psmouse_info(psmouse,
1505 "Toshiba %s detected, limiting rate to 40pps.\n",
1506 dmi_get_system_info(DMI_PRODUCT_NAME));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 psmouse->rate = 40;
1508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
Daniel Drake7968a5d2011-11-08 00:00:35 -08001510 if (!priv->absolute_mode && SYN_ID_DISGEST_SUPPORTED(priv->identity)) {
1511 err = device_create_file(&psmouse->ps2dev.serio->dev,
1512 &psmouse_attr_disable_gesture.dattr);
1513 if (err) {
1514 psmouse_err(psmouse,
1515 "Failed to create disable_gesture attribute (%d)",
1516 err);
1517 goto init_fail;
1518 }
1519 }
1520
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 return 0;
1522
1523 init_fail:
1524 kfree(priv);
Daniel Drake7968a5d2011-11-08 00:00:35 -08001525 return err;
1526}
1527
1528int synaptics_init(struct psmouse *psmouse)
1529{
1530 return __synaptics_init(psmouse, true);
1531}
1532
1533int synaptics_init_relative(struct psmouse *psmouse)
1534{
1535 return __synaptics_init(psmouse, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536}
1537
Daniel Drakee4e6efd2010-01-07 01:52:39 -08001538bool synaptics_supported(void)
1539{
1540 return true;
1541}
1542
Andres Salomon55e3d922007-03-10 01:39:54 -05001543#else /* CONFIG_MOUSE_PS2_SYNAPTICS */
1544
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001545void __init synaptics_module_init(void)
1546{
1547}
1548
Andres Salomon55e3d922007-03-10 01:39:54 -05001549int synaptics_init(struct psmouse *psmouse)
1550{
1551 return -ENOSYS;
1552}
1553
Daniel Drakee4e6efd2010-01-07 01:52:39 -08001554bool synaptics_supported(void)
1555{
1556 return false;
1557}
1558
Andres Salomon55e3d922007-03-10 01:39:54 -05001559#endif /* CONFIG_MOUSE_PS2_SYNAPTICS */