blob: a3bb49cb691c8a2bc1f34b498c695f386685d985 [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 Torokhov7705d542009-12-03 23:21:14 -080027#include <linux/dmi.h>
Henrik Rydbergfec6e522010-12-21 18:11:25 +010028#include <linux/input/mt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/serio.h>
30#include <linux/libps2.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "psmouse.h"
33#include "synaptics.h"
34
35/*
36 * The x/y limits are taken from the Synaptics TouchPad interfacing Guide,
37 * section 2.3.2, which says that they should be valid regardless of the
38 * actual size of the sensor.
Dmitry Torokhov83ba9ea82010-05-10 23:06:52 -070039 * Note that newer firmware allows querying device for maximum useable
40 * coordinates.
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 */
42#define XMIN_NOMINAL 1472
43#define XMAX_NOMINAL 5472
44#define YMIN_NOMINAL 1408
45#define YMAX_NOMINAL 4448
46
Daniel Kurtz6de58dd2011-08-23 23:00:24 -070047/*
48 * Synaptics touchpads report the y coordinate from bottom to top, which is
49 * opposite from what userspace expects.
50 * This function is used to invert y before reporting.
51 */
52static int synaptics_invert_y(int y)
53{
54 return YMAX_NOMINAL + YMIN_NOMINAL - y;
55}
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Andres Salomon55e3d922007-03-10 01:39:54 -050058/*****************************************************************************
59 * Stuff we need even when we do not want native Synaptics support
60 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62/*
63 * Set the synaptics touchpad mode byte by special commands
64 */
65static int synaptics_mode_cmd(struct psmouse *psmouse, unsigned char mode)
66{
67 unsigned char param[1];
68
69 if (psmouse_sliced_command(psmouse, mode))
70 return -1;
71 param[0] = SYN_PS_SET_MODE2;
72 if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_SETRATE))
73 return -1;
74 return 0;
75}
76
Dmitry Torokhovb7802c52009-09-09 19:13:20 -070077int synaptics_detect(struct psmouse *psmouse, bool set_properties)
Andres Salomon55e3d922007-03-10 01:39:54 -050078{
79 struct ps2dev *ps2dev = &psmouse->ps2dev;
80 unsigned char param[4];
81
82 param[0] = 0;
83
84 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
85 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
86 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
87 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
88 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO);
89
90 if (param[1] != 0x47)
91 return -ENODEV;
92
93 if (set_properties) {
94 psmouse->vendor = "Synaptics";
95 psmouse->name = "TouchPad";
96 }
97
98 return 0;
99}
100
101void synaptics_reset(struct psmouse *psmouse)
102{
103 /* reset touchpad back to relative mode, gestures enabled */
104 synaptics_mode_cmd(psmouse, 0);
105}
106
107#ifdef CONFIG_MOUSE_PS2_SYNAPTICS
108
109/*****************************************************************************
110 * Synaptics communications functions
111 ****************************************************************************/
112
113/*
114 * Send a command to the synpatics touchpad by special commands
115 */
116static int synaptics_send_cmd(struct psmouse *psmouse, unsigned char c, unsigned char *param)
117{
118 if (psmouse_sliced_command(psmouse, c))
119 return -1;
120 if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_GETINFO))
121 return -1;
122 return 0;
123}
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125/*
126 * Read the model-id bytes from the touchpad
127 * see also SYN_MODEL_* macros
128 */
129static int synaptics_model_id(struct psmouse *psmouse)
130{
131 struct synaptics_data *priv = psmouse->private;
132 unsigned char mi[3];
133
134 if (synaptics_send_cmd(psmouse, SYN_QUE_MODEL, mi))
135 return -1;
136 priv->model_id = (mi[0]<<16) | (mi[1]<<8) | mi[2];
137 return 0;
138}
139
140/*
141 * Read the capability-bits from the touchpad
142 * see also the SYN_CAP_* macros
143 */
144static int synaptics_capability(struct psmouse *psmouse)
145{
146 struct synaptics_data *priv = psmouse->private;
147 unsigned char cap[3];
148
149 if (synaptics_send_cmd(psmouse, SYN_QUE_CAPABILITIES, cap))
150 return -1;
151 priv->capabilities = (cap[0] << 16) | (cap[1] << 8) | cap[2];
Takashi Iwai5f57d672010-04-19 10:37:21 -0700152 priv->ext_cap = priv->ext_cap_0c = 0;
153
Dmitry Torokhov3619b8f2010-07-21 00:01:19 -0700154 /*
155 * Older firmwares had submodel ID fixed to 0x47
156 */
157 if (SYN_ID_FULL(priv->identity) < 0x705 &&
158 SYN_CAP_SUBMODEL_ID(priv->capabilities) != 0x47) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 return -1;
Dmitry Torokhov3619b8f2010-07-21 00:01:19 -0700160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 /*
163 * Unless capExtended is set the rest of the flags should be ignored
164 */
165 if (!SYN_CAP_EXTENDED(priv->capabilities))
166 priv->capabilities = 0;
167
168 if (SYN_EXT_CAP_REQUESTS(priv->capabilities) >= 1) {
169 if (synaptics_send_cmd(psmouse, SYN_QUE_EXT_CAPAB, cap)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700170 psmouse_warn(psmouse,
171 "device claims to have extended capabilities, but I'm not able to read them.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 } else {
173 priv->ext_cap = (cap[0] << 16) | (cap[1] << 8) | cap[2];
174
175 /*
176 * if nExtBtn is greater than 8 it should be considered
177 * invalid and treated as 0
178 */
179 if (SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap) > 8)
180 priv->ext_cap &= 0xff0fff;
181 }
182 }
Takashi Iwai5f57d672010-04-19 10:37:21 -0700183
184 if (SYN_EXT_CAP_REQUESTS(priv->capabilities) >= 4) {
185 if (synaptics_send_cmd(psmouse, SYN_QUE_EXT_CAPAB_0C, cap)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700186 psmouse_warn(psmouse,
187 "device claims to have extended capability 0x0c, but I'm not able to read it.\n");
Takashi Iwai5f57d672010-04-19 10:37:21 -0700188 } else {
189 priv->ext_cap_0c = (cap[0] << 16) | (cap[1] << 8) | cap[2];
190 }
191 }
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 return 0;
194}
195
196/*
197 * Identify Touchpad
198 * See also the SYN_ID_* macros
199 */
200static int synaptics_identify(struct psmouse *psmouse)
201{
202 struct synaptics_data *priv = psmouse->private;
203 unsigned char id[3];
204
205 if (synaptics_send_cmd(psmouse, SYN_QUE_IDENTIFY, id))
206 return -1;
207 priv->identity = (id[0]<<16) | (id[1]<<8) | id[2];
208 if (SYN_ID_IS_SYNAPTICS(priv->identity))
209 return 0;
210 return -1;
211}
212
Tero Saarniec20a022009-06-10 23:27:24 -0700213/*
Dmitry Torokhov83ba9ea82010-05-10 23:06:52 -0700214 * Read touchpad resolution and maximum reported coordinates
Tero Saarniec20a022009-06-10 23:27:24 -0700215 * Resolution is left zero if touchpad does not support the query
216 */
217static int synaptics_resolution(struct psmouse *psmouse)
218{
219 struct synaptics_data *priv = psmouse->private;
Dmitry Torokhova66413f2011-07-09 12:32:56 -0700220 unsigned char resp[3];
Tero Saarniec20a022009-06-10 23:27:24 -0700221
222 if (SYN_ID_MAJOR(priv->identity) < 4)
Takashi Iwaibbddd192010-07-14 09:32:46 -0700223 return 0;
Tero Saarniec20a022009-06-10 23:27:24 -0700224
Dmitry Torokhova66413f2011-07-09 12:32:56 -0700225 if (synaptics_send_cmd(psmouse, SYN_QUE_RESOLUTION, resp) == 0) {
226 if (resp[0] != 0 && (resp[1] & 0x80) && resp[2] != 0) {
227 priv->x_res = resp[0]; /* x resolution in units/mm */
228 priv->y_res = resp[2]; /* y resolution in units/mm */
Dmitry Torokhov83ba9ea82010-05-10 23:06:52 -0700229 }
230 }
Tero Saarniec20a022009-06-10 23:27:24 -0700231
Dmitry Torokhov83ba9ea82010-05-10 23:06:52 -0700232 if (SYN_EXT_CAP_REQUESTS(priv->capabilities) >= 5 &&
233 SYN_CAP_MAX_DIMENSIONS(priv->ext_cap_0c)) {
Dmitry Torokhova66413f2011-07-09 12:32:56 -0700234 if (synaptics_send_cmd(psmouse, SYN_QUE_EXT_MAX_COORDS, resp)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700235 psmouse_warn(psmouse,
236 "device claims to have max coordinates query, but I'm not able to read it.\n");
Dmitry Torokhov83ba9ea82010-05-10 23:06:52 -0700237 } else {
Dmitry Torokhova66413f2011-07-09 12:32:56 -0700238 priv->x_max = (resp[0] << 5) | ((resp[1] & 0x0f) << 1);
239 priv->y_max = (resp[2] << 5) | ((resp[1] & 0xf0) >> 3);
240 }
241 }
242
243 if (SYN_EXT_CAP_REQUESTS(priv->capabilities) >= 7 &&
244 SYN_CAP_MIN_DIMENSIONS(priv->ext_cap_0c)) {
245 if (synaptics_send_cmd(psmouse, SYN_QUE_EXT_MIN_COORDS, resp)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700246 psmouse_warn(psmouse,
247 "device claims to have min coordinates query, but I'm not able to read it.\n");
Dmitry Torokhova66413f2011-07-09 12:32:56 -0700248 } else {
249 priv->x_min = (resp[0] << 5) | ((resp[1] & 0x0f) << 1);
250 priv->y_min = (resp[2] << 5) | ((resp[1] & 0xf0) >> 3);
Dmitry Torokhov83ba9ea82010-05-10 23:06:52 -0700251 }
Tero Saarniec20a022009-06-10 23:27:24 -0700252 }
253
254 return 0;
255}
256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257static int synaptics_query_hardware(struct psmouse *psmouse)
258{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 if (synaptics_identify(psmouse))
260 return -1;
261 if (synaptics_model_id(psmouse))
262 return -1;
263 if (synaptics_capability(psmouse))
264 return -1;
Tero Saarniec20a022009-06-10 23:27:24 -0700265 if (synaptics_resolution(psmouse))
266 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 return 0;
269}
270
Daniel Drake7968a5d2011-11-08 00:00:35 -0800271static int synaptics_set_advanced_gesture_mode(struct psmouse *psmouse)
272{
273 static unsigned char param = 0xc8;
274 struct synaptics_data *priv = psmouse->private;
275
276 if (!SYN_CAP_ADV_GESTURE(priv->ext_cap_0c))
277 return 0;
278
279 if (psmouse_sliced_command(psmouse, SYN_QUE_MODEL))
280 return -1;
281
282 if (ps2_command(&psmouse->ps2dev, &param, PSMOUSE_CMD_SETRATE))
283 return -1;
284
285 /* Advanced gesture mode also sends multi finger data */
286 priv->capabilities |= BIT(1);
287
288 return 0;
289}
290
291static int synaptics_set_mode(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
293 struct synaptics_data *priv = psmouse->private;
294
Daniel Drake7968a5d2011-11-08 00:00:35 -0800295 priv->mode = 0;
296 if (priv->absolute_mode)
297 priv->mode |= SYN_BIT_ABSOLUTE_MODE;
298 if (priv->disable_gesture)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 priv->mode |= SYN_BIT_DISABLE_GESTURE;
Daniel Drake7968a5d2011-11-08 00:00:35 -0800300 if (psmouse->rate >= 80)
301 priv->mode |= SYN_BIT_HIGH_RATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 if (SYN_CAP_EXTENDED(priv->capabilities))
303 priv->mode |= SYN_BIT_W_MODE;
304
305 if (synaptics_mode_cmd(psmouse, priv->mode))
306 return -1;
307
Daniel Drake7968a5d2011-11-08 00:00:35 -0800308 if (priv->absolute_mode &&
309 synaptics_set_advanced_gesture_mode(psmouse)) {
310 psmouse_err(psmouse, "Advanced gesture mode init failed.\n");
311 return -1;
312 }
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 return 0;
315}
316
317static void synaptics_set_rate(struct psmouse *psmouse, unsigned int rate)
318{
319 struct synaptics_data *priv = psmouse->private;
320
321 if (rate >= 80) {
322 priv->mode |= SYN_BIT_HIGH_RATE;
323 psmouse->rate = 80;
324 } else {
325 priv->mode &= ~SYN_BIT_HIGH_RATE;
326 psmouse->rate = 40;
327 }
328
329 synaptics_mode_cmd(psmouse, priv->mode);
330}
331
332/*****************************************************************************
333 * Synaptics pass-through PS/2 port support
334 ****************************************************************************/
335static int synaptics_pt_write(struct serio *serio, unsigned char c)
336{
337 struct psmouse *parent = serio_get_drvdata(serio->parent);
338 char rate_param = SYN_PS_CLIENT_CMD; /* indicates that we want pass-through port */
339
340 if (psmouse_sliced_command(parent, c))
341 return -1;
342 if (ps2_command(&parent->ps2dev, &rate_param, PSMOUSE_CMD_SETRATE))
343 return -1;
344 return 0;
345}
346
Dmitry Torokhova8b3c0f2010-10-04 21:46:10 -0700347static int synaptics_pt_start(struct serio *serio)
348{
349 struct psmouse *parent = serio_get_drvdata(serio->parent);
350 struct synaptics_data *priv = parent->private;
351
352 serio_pause_rx(parent->ps2dev.serio);
353 priv->pt_port = serio;
354 serio_continue_rx(parent->ps2dev.serio);
355
356 return 0;
357}
358
359static void synaptics_pt_stop(struct serio *serio)
360{
361 struct psmouse *parent = serio_get_drvdata(serio->parent);
362 struct synaptics_data *priv = parent->private;
363
364 serio_pause_rx(parent->ps2dev.serio);
365 priv->pt_port = NULL;
366 serio_continue_rx(parent->ps2dev.serio);
367}
368
369static int synaptics_is_pt_packet(unsigned char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
371 return (buf[0] & 0xFC) == 0x84 && (buf[3] & 0xCC) == 0xC4;
372}
373
374static void synaptics_pass_pt_packet(struct serio *ptport, unsigned char *packet)
375{
376 struct psmouse *child = serio_get_drvdata(ptport);
377
378 if (child && child->state == PSMOUSE_ACTIVATED) {
David Howells7d12e782006-10-05 14:55:46 +0100379 serio_interrupt(ptport, packet[1], 0);
380 serio_interrupt(ptport, packet[4], 0);
381 serio_interrupt(ptport, packet[5], 0);
Sergey Vlasov33fdfa92005-07-24 00:53:32 -0500382 if (child->pktsize == 4)
David Howells7d12e782006-10-05 14:55:46 +0100383 serio_interrupt(ptport, packet[2], 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 } else
David Howells7d12e782006-10-05 14:55:46 +0100385 serio_interrupt(ptport, packet[1], 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
387
388static void synaptics_pt_activate(struct psmouse *psmouse)
389{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 struct synaptics_data *priv = psmouse->private;
Dmitry Torokhova8b3c0f2010-10-04 21:46:10 -0700391 struct psmouse *child = serio_get_drvdata(priv->pt_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 /* adjust the touchpad to child's choice of protocol */
394 if (child) {
Sergey Vlasov33fdfa92005-07-24 00:53:32 -0500395 if (child->pktsize == 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 priv->mode |= SYN_BIT_FOUR_BYTE_CLIENT;
397 else
398 priv->mode &= ~SYN_BIT_FOUR_BYTE_CLIENT;
399
400 if (synaptics_mode_cmd(psmouse, priv->mode))
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700401 psmouse_warn(psmouse,
402 "failed to switch guest protocol\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 }
404}
405
406static void synaptics_pt_create(struct psmouse *psmouse)
407{
408 struct serio *serio;
409
Eric Sesterhennb39787a2006-03-14 00:09:16 -0500410 serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 if (!serio) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700412 psmouse_err(psmouse,
413 "not enough memory for pass-through port\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 return;
415 }
416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 serio->id.type = SERIO_PS_PSTHRU;
418 strlcpy(serio->name, "Synaptics pass-through", sizeof(serio->name));
419 strlcpy(serio->phys, "synaptics-pt/serio0", sizeof(serio->name));
420 serio->write = synaptics_pt_write;
Dmitry Torokhova8b3c0f2010-10-04 21:46:10 -0700421 serio->start = synaptics_pt_start;
422 serio->stop = synaptics_pt_stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 serio->parent = psmouse->ps2dev.serio;
424
425 psmouse->pt_activate = synaptics_pt_activate;
426
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700427 psmouse_info(psmouse, "serio: %s port at %s\n",
428 serio->name, psmouse->phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 serio_register_port(serio);
430}
431
432/*****************************************************************************
433 * Functions to interpret the absolute mode packets
434 ****************************************************************************/
435
Daniel Kurtza6ca40c2011-08-23 23:02:31 -0700436static void synaptics_mt_state_set(struct synaptics_mt_state *state, int count,
437 int sgm, int agm)
438{
439 state->count = count;
440 state->sgm = sgm;
441 state->agm = agm;
442}
443
Daniel Kurtz7afdb842011-08-23 23:00:33 -0700444static void synaptics_parse_agm(const unsigned char buf[],
Daniel Kurtza6ca40c2011-08-23 23:02:31 -0700445 struct synaptics_data *priv,
446 struct synaptics_hw_state *hw)
Daniel Kurtz7afdb842011-08-23 23:00:33 -0700447{
448 struct synaptics_hw_state *agm = &priv->agm;
Daniel Kurtza6ca40c2011-08-23 23:02:31 -0700449 int agm_packet_type;
Daniel Kurtz7afdb842011-08-23 23:00:33 -0700450
Daniel Kurtza6ca40c2011-08-23 23:02:31 -0700451 agm_packet_type = (buf[5] & 0x30) >> 4;
452 switch (agm_packet_type) {
453 case 1:
454 /* Gesture packet: (x, y, z) half resolution */
455 agm->w = hw->w;
456 agm->x = (((buf[4] & 0x0f) << 8) | buf[1]) << 1;
457 agm->y = (((buf[4] & 0xf0) << 4) | buf[2]) << 1;
458 agm->z = ((buf[3] & 0x30) | (buf[5] & 0x0f)) << 1;
459 break;
460
461 case 2:
462 /* AGM-CONTACT packet: (count, sgm, agm) */
463 synaptics_mt_state_set(&agm->mt_state, buf[1], buf[2], buf[4]);
464 break;
465
466 default:
467 break;
468 }
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700469
470 /* Record that at least one AGM has been received since last SGM */
471 priv->agm_pending = true;
Daniel Kurtz7afdb842011-08-23 23:00:33 -0700472}
473
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100474static int synaptics_parse_hw_state(const unsigned char buf[],
475 struct synaptics_data *priv,
476 struct synaptics_hw_state *hw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
478 memset(hw, 0, sizeof(struct synaptics_hw_state));
479
480 if (SYN_MODEL_NEWABS(priv->model_id)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 hw->w = (((buf[0] & 0x30) >> 2) |
482 ((buf[0] & 0x04) >> 1) |
483 ((buf[3] & 0x04) >> 2));
484
485 hw->left = (buf[0] & 0x01) ? 1 : 0;
486 hw->right = (buf[0] & 0x02) ? 1 : 0;
487
Takashi Iwai5f57d672010-04-19 10:37:21 -0700488 if (SYN_CAP_CLICKPAD(priv->ext_cap_0c)) {
489 /*
490 * Clickpad's button is transmitted as middle button,
491 * however, since it is primary button, we will report
492 * it as BTN_LEFT.
493 */
494 hw->left = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0;
495
496 } else if (SYN_CAP_MIDDLE_BUTTON(priv->capabilities)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 hw->middle = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0;
498 if (hw->w == 2)
499 hw->scroll = (signed char)(buf[1]);
500 }
501
502 if (SYN_CAP_FOUR_BUTTON(priv->capabilities)) {
503 hw->up = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0;
504 hw->down = ((buf[0] ^ buf[3]) & 0x02) ? 1 : 0;
505 }
506
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700507 if ((SYN_CAP_ADV_GESTURE(priv->ext_cap_0c) ||
508 SYN_CAP_IMAGE_SENSOR(priv->ext_cap_0c)) &&
509 hw->w == 2) {
Daniel Kurtza6ca40c2011-08-23 23:02:31 -0700510 synaptics_parse_agm(buf, priv, hw);
Daniel Kurtz28d5fd82011-07-06 22:57:39 -0700511 return 1;
512 }
513
514 hw->x = (((buf[3] & 0x10) << 8) |
515 ((buf[1] & 0x0f) << 8) |
516 buf[4]);
517 hw->y = (((buf[3] & 0x20) << 7) |
518 ((buf[1] & 0xf0) << 4) |
519 buf[5]);
520 hw->z = buf[2];
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 if (SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap) &&
523 ((buf[0] ^ buf[3]) & 0x02)) {
524 switch (SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap) & ~0x01) {
525 default:
526 /*
527 * if nExtBtn is greater than 8 it should be
528 * considered invalid and treated as 0
529 */
530 break;
531 case 8:
532 hw->ext_buttons |= ((buf[5] & 0x08)) ? 0x80 : 0;
533 hw->ext_buttons |= ((buf[4] & 0x08)) ? 0x40 : 0;
534 case 6:
535 hw->ext_buttons |= ((buf[5] & 0x04)) ? 0x20 : 0;
536 hw->ext_buttons |= ((buf[4] & 0x04)) ? 0x10 : 0;
537 case 4:
538 hw->ext_buttons |= ((buf[5] & 0x02)) ? 0x08 : 0;
539 hw->ext_buttons |= ((buf[4] & 0x02)) ? 0x04 : 0;
540 case 2:
541 hw->ext_buttons |= ((buf[5] & 0x01)) ? 0x02 : 0;
542 hw->ext_buttons |= ((buf[4] & 0x01)) ? 0x01 : 0;
543 }
544 }
545 } else {
546 hw->x = (((buf[1] & 0x1f) << 8) | buf[2]);
547 hw->y = (((buf[4] & 0x1f) << 8) | buf[5]);
548
549 hw->z = (((buf[0] & 0x30) << 2) | (buf[3] & 0x3F));
550 hw->w = (((buf[1] & 0x80) >> 4) | ((buf[0] & 0x04) >> 1));
551
552 hw->left = (buf[0] & 0x01) ? 1 : 0;
553 hw->right = (buf[0] & 0x02) ? 1 : 0;
554 }
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100555
556 return 0;
557}
558
Daniel Kurtzbea9f0f2011-07-06 22:42:52 -0700559static void synaptics_report_semi_mt_slot(struct input_dev *dev, int slot,
560 bool active, int x, int y)
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100561{
562 input_mt_slot(dev, slot);
563 input_mt_report_slot_state(dev, MT_TOOL_FINGER, active);
564 if (active) {
565 input_report_abs(dev, ABS_MT_POSITION_X, x);
Daniel Kurtz6de58dd2011-08-23 23:00:24 -0700566 input_report_abs(dev, ABS_MT_POSITION_Y, synaptics_invert_y(y));
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100567 }
568}
569
570static void synaptics_report_semi_mt_data(struct input_dev *dev,
571 const struct synaptics_hw_state *a,
572 const struct synaptics_hw_state *b,
573 int num_fingers)
574{
575 if (num_fingers >= 2) {
Daniel Kurtzbea9f0f2011-07-06 22:42:52 -0700576 synaptics_report_semi_mt_slot(dev, 0, true, min(a->x, b->x),
577 min(a->y, b->y));
578 synaptics_report_semi_mt_slot(dev, 1, true, max(a->x, b->x),
579 max(a->y, b->y));
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100580 } else if (num_fingers == 1) {
Daniel Kurtzbea9f0f2011-07-06 22:42:52 -0700581 synaptics_report_semi_mt_slot(dev, 0, true, a->x, a->y);
582 synaptics_report_semi_mt_slot(dev, 1, false, 0, 0);
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100583 } else {
Daniel Kurtzbea9f0f2011-07-06 22:42:52 -0700584 synaptics_report_semi_mt_slot(dev, 0, false, 0, 0);
585 synaptics_report_semi_mt_slot(dev, 1, false, 0, 0);
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587}
588
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700589static void synaptics_report_buttons(struct psmouse *psmouse,
590 const struct synaptics_hw_state *hw)
591{
592 struct input_dev *dev = psmouse->dev;
593 struct synaptics_data *priv = psmouse->private;
594 int i;
595
596 input_report_key(dev, BTN_LEFT, hw->left);
597 input_report_key(dev, BTN_RIGHT, hw->right);
598
599 if (SYN_CAP_MIDDLE_BUTTON(priv->capabilities))
600 input_report_key(dev, BTN_MIDDLE, hw->middle);
601
602 if (SYN_CAP_FOUR_BUTTON(priv->capabilities)) {
603 input_report_key(dev, BTN_FORWARD, hw->up);
604 input_report_key(dev, BTN_BACK, hw->down);
605 }
606
607 for (i = 0; i < SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap); i++)
608 input_report_key(dev, BTN_0 + i, hw->ext_buttons & (1 << i));
609}
610
611static void synaptics_report_slot(struct input_dev *dev, int slot,
612 const struct synaptics_hw_state *hw)
613{
614 input_mt_slot(dev, slot);
615 input_mt_report_slot_state(dev, MT_TOOL_FINGER, (hw != NULL));
616 if (!hw)
617 return;
618
619 input_report_abs(dev, ABS_MT_POSITION_X, hw->x);
620 input_report_abs(dev, ABS_MT_POSITION_Y, synaptics_invert_y(hw->y));
621 input_report_abs(dev, ABS_MT_PRESSURE, hw->z);
622}
623
624static void synaptics_report_mt_data(struct psmouse *psmouse,
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700625 struct synaptics_mt_state *mt_state,
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700626 const struct synaptics_hw_state *sgm)
627{
628 struct input_dev *dev = psmouse->dev;
629 struct synaptics_data *priv = psmouse->private;
630 struct synaptics_hw_state *agm = &priv->agm;
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700631 struct synaptics_mt_state *old = &priv->mt_state;
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700632
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700633 switch (mt_state->count) {
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700634 case 0:
635 synaptics_report_slot(dev, 0, NULL);
636 synaptics_report_slot(dev, 1, NULL);
637 break;
638 case 1:
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700639 if (mt_state->sgm == -1) {
640 synaptics_report_slot(dev, 0, NULL);
641 synaptics_report_slot(dev, 1, NULL);
642 } else if (mt_state->sgm == 0) {
643 synaptics_report_slot(dev, 0, sgm);
644 synaptics_report_slot(dev, 1, NULL);
645 } else {
646 synaptics_report_slot(dev, 0, NULL);
647 synaptics_report_slot(dev, 1, sgm);
648 }
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700649 break;
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700650 default:
651 /*
652 * If the finger slot contained in SGM is valid, and either
653 * hasn't changed, or is new, then report SGM in MTB slot 0.
654 * Otherwise, empty MTB slot 0.
655 */
656 if (mt_state->sgm != -1 &&
657 (mt_state->sgm == old->sgm || old->sgm == -1))
658 synaptics_report_slot(dev, 0, sgm);
659 else
660 synaptics_report_slot(dev, 0, NULL);
661
662 /*
663 * If the finger slot contained in AGM is valid, and either
664 * hasn't changed, or is new, then report AGM in MTB slot 1.
665 * Otherwise, empty MTB slot 1.
666 */
667 if (mt_state->agm != -1 &&
668 (mt_state->agm == old->agm || old->agm == -1))
669 synaptics_report_slot(dev, 1, agm);
670 else
671 synaptics_report_slot(dev, 1, NULL);
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700672 break;
673 }
674
675 /* Don't use active slot count to generate BTN_TOOL events. */
676 input_mt_report_pointer_emulation(dev, false);
677
678 /* Send the number of fingers reported by touchpad itself. */
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700679 input_mt_report_finger_count(dev, mt_state->count);
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700680
681 synaptics_report_buttons(psmouse, sgm);
682
683 input_sync(dev);
684}
685
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700686/* Handle case where mt_state->count = 0 */
687static void synaptics_image_sensor_0f(struct synaptics_data *priv,
688 struct synaptics_mt_state *mt_state)
689{
690 synaptics_mt_state_set(mt_state, 0, -1, -1);
691 priv->mt_state_lost = false;
692}
693
694/* Handle case where mt_state->count = 1 */
695static void synaptics_image_sensor_1f(struct synaptics_data *priv,
696 struct synaptics_mt_state *mt_state)
697{
698 struct synaptics_hw_state *agm = &priv->agm;
699 struct synaptics_mt_state *old = &priv->mt_state;
700
701 /*
702 * If the last AGM was (0,0,0), and there is only one finger left,
703 * then we absolutely know that SGM contains slot 0, and all other
704 * fingers have been removed.
705 */
706 if (priv->agm_pending && agm->z == 0) {
707 synaptics_mt_state_set(mt_state, 1, 0, -1);
708 priv->mt_state_lost = false;
709 return;
710 }
711
712 switch (old->count) {
713 case 0:
714 synaptics_mt_state_set(mt_state, 1, 0, -1);
715 break;
716 case 1:
717 /*
718 * If mt_state_lost, then the previous transition was 3->1,
719 * and SGM now contains either slot 0 or 1, but we don't know
720 * which. So, we just assume that the SGM now contains slot 1.
721 *
722 * If pending AGM and either:
723 * (a) the previous SGM slot contains slot 0, or
724 * (b) there was no SGM slot
725 * then, the SGM now contains slot 1
726 *
727 * Case (a) happens with very rapid "drum roll" gestures, where
728 * slot 0 finger is lifted and a new slot 1 finger touches
729 * within one reporting interval.
730 *
731 * Case (b) happens if initially two or more fingers tap
732 * briefly, and all but one lift before the end of the first
733 * reporting interval.
734 *
735 * (In both these cases, slot 0 will becomes empty, so SGM
736 * contains slot 1 with the new finger)
737 *
738 * Else, if there was no previous SGM, it now contains slot 0.
739 *
740 * Otherwise, SGM still contains the same slot.
741 */
742 if (priv->mt_state_lost ||
743 (priv->agm_pending && old->sgm <= 0))
744 synaptics_mt_state_set(mt_state, 1, 1, -1);
745 else if (old->sgm == -1)
746 synaptics_mt_state_set(mt_state, 1, 0, -1);
747 break;
748 case 2:
749 /*
750 * If mt_state_lost, we don't know which finger SGM contains.
751 *
752 * So, report 1 finger, but with both slots empty.
753 * We will use slot 1 on subsequent 1->1
754 */
755 if (priv->mt_state_lost) {
756 synaptics_mt_state_set(mt_state, 1, -1, -1);
757 break;
758 }
759 /*
760 * Since the last AGM was NOT (0,0,0), it was the finger in
761 * slot 0 that has been removed.
762 * So, SGM now contains previous AGM's slot, and AGM is now
763 * empty.
764 */
765 synaptics_mt_state_set(mt_state, 1, old->agm, -1);
766 break;
767 case 3:
768 /*
769 * Since last AGM was not (0,0,0), we don't know which finger
770 * is left.
771 *
772 * So, report 1 finger, but with both slots empty.
773 * We will use slot 1 on subsequent 1->1
774 */
775 synaptics_mt_state_set(mt_state, 1, -1, -1);
776 priv->mt_state_lost = true;
777 break;
Daniel Kurtz6b4b49f2011-08-23 23:02:56 -0700778 case 4:
779 case 5:
780 /* mt_state was updated by AGM-CONTACT packet */
781 break;
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700782 }
783}
784
785/* Handle case where mt_state->count = 2 */
786static void synaptics_image_sensor_2f(struct synaptics_data *priv,
787 struct synaptics_mt_state *mt_state)
788{
789 struct synaptics_mt_state *old = &priv->mt_state;
790
791 switch (old->count) {
792 case 0:
793 synaptics_mt_state_set(mt_state, 2, 0, 1);
794 break;
795 case 1:
796 /*
797 * If previous SGM contained slot 1 or higher, SGM now contains
798 * slot 0 (the newly touching finger) and AGM contains SGM's
799 * previous slot.
800 *
801 * Otherwise, SGM still contains slot 0 and AGM now contains
802 * slot 1.
803 */
804 if (old->sgm >= 1)
805 synaptics_mt_state_set(mt_state, 2, 0, old->sgm);
806 else
807 synaptics_mt_state_set(mt_state, 2, 0, 1);
808 break;
809 case 2:
810 /*
811 * If mt_state_lost, SGM now contains either finger 1 or 2, but
812 * we don't know which.
813 * So, we just assume that the SGM contains slot 0 and AGM 1.
814 */
815 if (priv->mt_state_lost)
816 synaptics_mt_state_set(mt_state, 2, 0, 1);
817 /*
818 * Otherwise, use the same mt_state, since it either hasn't
819 * changed, or was updated by a recently received AGM-CONTACT
820 * packet.
821 */
822 break;
823 case 3:
824 /*
825 * 3->2 transitions have two unsolvable problems:
826 * 1) no indication is given which finger was removed
827 * 2) no way to tell if agm packet was for finger 3
828 * before 3->2, or finger 2 after 3->2.
829 *
830 * So, report 2 fingers, but empty all slots.
831 * We will guess slots [0,1] on subsequent 2->2.
832 */
833 synaptics_mt_state_set(mt_state, 2, -1, -1);
834 priv->mt_state_lost = true;
835 break;
Daniel Kurtz6b4b49f2011-08-23 23:02:56 -0700836 case 4:
837 case 5:
838 /* mt_state was updated by AGM-CONTACT packet */
839 break;
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700840 }
841}
842
843/* Handle case where mt_state->count = 3 */
844static void synaptics_image_sensor_3f(struct synaptics_data *priv,
845 struct synaptics_mt_state *mt_state)
846{
847 struct synaptics_mt_state *old = &priv->mt_state;
848
849 switch (old->count) {
850 case 0:
851 synaptics_mt_state_set(mt_state, 3, 0, 2);
852 break;
853 case 1:
854 /*
855 * If previous SGM contained slot 2 or higher, SGM now contains
856 * slot 0 (one of the newly touching fingers) and AGM contains
857 * SGM's previous slot.
858 *
859 * Otherwise, SGM now contains slot 0 and AGM contains slot 2.
860 */
861 if (old->sgm >= 2)
862 synaptics_mt_state_set(mt_state, 3, 0, old->sgm);
863 else
864 synaptics_mt_state_set(mt_state, 3, 0, 2);
865 break;
866 case 2:
867 /*
Daniel Kurtz6b4b49f2011-08-23 23:02:56 -0700868 * If the AGM previously contained slot 3 or higher, then the
869 * newly touching finger is in the lowest available slot.
870 *
871 * If SGM was previously 1 or higher, then the new SGM is
872 * now slot 0 (with a new finger), otherwise, the new finger
873 * is now in a hidden slot between 0 and AGM's slot.
874 *
875 * In all such cases, the SGM now contains slot 0, and the AGM
876 * continues to contain the same slot as before.
877 */
878 if (old->agm >= 3) {
879 synaptics_mt_state_set(mt_state, 3, 0, old->agm);
880 break;
881 }
882
883 /*
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700884 * After some 3->1 and all 3->2 transitions, we lose track
885 * of which slot is reported by SGM and AGM.
886 *
887 * For 2->3 in this state, report 3 fingers, but empty all
888 * slots, and we will guess (0,2) on a subsequent 0->3.
889 *
890 * To userspace, the resulting transition will look like:
891 * 2:[0,1] -> 3:[-1,-1] -> 3:[0,2]
892 */
893 if (priv->mt_state_lost) {
894 synaptics_mt_state_set(mt_state, 3, -1, -1);
895 break;
896 }
897
898 /*
899 * If the (SGM,AGM) really previously contained slots (0, 1),
900 * then we cannot know what slot was just reported by the AGM,
901 * because the 2->3 transition can occur either before or after
902 * the AGM packet. Thus, this most recent AGM could contain
903 * either the same old slot 1 or the new slot 2.
904 * Subsequent AGMs will be reporting slot 2.
905 *
906 * To userspace, the resulting transition will look like:
907 * 2:[0,1] -> 3:[0,-1] -> 3:[0,2]
908 */
909 synaptics_mt_state_set(mt_state, 3, 0, -1);
910 break;
911 case 3:
912 /*
913 * If, for whatever reason, the previous agm was invalid,
914 * Assume SGM now contains slot 0, AGM now contains slot 2.
915 */
916 if (old->agm <= 2)
917 synaptics_mt_state_set(mt_state, 3, 0, 2);
918 /*
919 * mt_state either hasn't changed, or was updated by a recently
920 * received AGM-CONTACT packet.
921 */
922 break;
Daniel Kurtz6b4b49f2011-08-23 23:02:56 -0700923
924 case 4:
925 case 5:
926 /* mt_state was updated by AGM-CONTACT packet */
927 break;
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700928 }
929}
930
Daniel Kurtz6b4b49f2011-08-23 23:02:56 -0700931/* Handle case where mt_state->count = 4, or = 5 */
932static void synaptics_image_sensor_45f(struct synaptics_data *priv,
933 struct synaptics_mt_state *mt_state)
934{
935 /* mt_state was updated correctly by AGM-CONTACT packet */
936 priv->mt_state_lost = false;
937}
938
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700939static void synaptics_image_sensor_process(struct psmouse *psmouse,
940 struct synaptics_hw_state *sgm)
941{
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700942 struct synaptics_data *priv = psmouse->private;
943 struct synaptics_hw_state *agm = &priv->agm;
944 struct synaptics_mt_state mt_state;
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700945
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700946 /* Initialize using current mt_state (as updated by last agm) */
947 mt_state = agm->mt_state;
948
949 /*
950 * Update mt_state using the new finger count and current mt_state.
951 */
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700952 if (sgm->z == 0)
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700953 synaptics_image_sensor_0f(priv, &mt_state);
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700954 else if (sgm->w >= 4)
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700955 synaptics_image_sensor_1f(priv, &mt_state);
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700956 else if (sgm->w == 0)
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700957 synaptics_image_sensor_2f(priv, &mt_state);
Daniel Kurtz6b4b49f2011-08-23 23:02:56 -0700958 else if (sgm->w == 1 && mt_state.count <= 3)
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700959 synaptics_image_sensor_3f(priv, &mt_state);
Daniel Kurtz6b4b49f2011-08-23 23:02:56 -0700960 else
961 synaptics_image_sensor_45f(priv, &mt_state);
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700962
963 /* Send resulting input events to user space */
Daniel Kurtz4dc772d2011-08-23 23:02:40 -0700964 synaptics_report_mt_data(psmouse, &mt_state, sgm);
965
966 /* Store updated mt_state */
967 priv->mt_state = agm->mt_state = mt_state;
968 priv->agm_pending = false;
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700969}
970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971/*
972 * called for each full received packet from the touchpad
973 */
974static void synaptics_process_packet(struct psmouse *psmouse)
975{
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500976 struct input_dev *dev = psmouse->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 struct synaptics_data *priv = psmouse->private;
978 struct synaptics_hw_state hw;
979 int num_fingers;
980 int finger_width;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Henrik Rydbergfec6e522010-12-21 18:11:25 +0100982 if (synaptics_parse_hw_state(psmouse->packet, priv, &hw))
983 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Daniel Kurtz3cdfee92011-08-23 23:02:25 -0700985 if (SYN_CAP_IMAGE_SENSOR(priv->ext_cap_0c)) {
986 synaptics_image_sensor_process(psmouse, &hw);
987 return;
988 }
989
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 if (hw.scroll) {
991 priv->scroll += hw.scroll;
992
993 while (priv->scroll >= 4) {
994 input_report_key(dev, BTN_BACK, !hw.down);
995 input_sync(dev);
996 input_report_key(dev, BTN_BACK, hw.down);
997 input_sync(dev);
998 priv->scroll -= 4;
999 }
1000 while (priv->scroll <= -4) {
1001 input_report_key(dev, BTN_FORWARD, !hw.up);
1002 input_sync(dev);
1003 input_report_key(dev, BTN_FORWARD, hw.up);
1004 input_sync(dev);
1005 priv->scroll += 4;
1006 }
1007 return;
1008 }
1009
Henrik Rydberg4f56ce92010-12-18 15:42:30 +01001010 if (hw.z > 0 && hw.x > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 num_fingers = 1;
1012 finger_width = 5;
1013 if (SYN_CAP_EXTENDED(priv->capabilities)) {
1014 switch (hw.w) {
1015 case 0 ... 1:
1016 if (SYN_CAP_MULTIFINGER(priv->capabilities))
1017 num_fingers = hw.w + 2;
1018 break;
1019 case 2:
1020 if (SYN_MODEL_PEN(priv->model_id))
1021 ; /* Nothing, treat a pen as a single finger */
1022 break;
1023 case 4 ... 15:
1024 if (SYN_CAP_PALMDETECT(priv->capabilities))
1025 finger_width = hw.w;
1026 break;
1027 }
1028 }
1029 } else {
1030 num_fingers = 0;
1031 finger_width = 0;
1032 }
1033
Henrik Rydbergfec6e522010-12-21 18:11:25 +01001034 if (SYN_CAP_ADV_GESTURE(priv->ext_cap_0c))
Daniel Kurtz7afdb842011-08-23 23:00:33 -07001035 synaptics_report_semi_mt_data(dev, &hw, &priv->agm,
1036 num_fingers);
Henrik Rydbergfec6e522010-12-21 18:11:25 +01001037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 /* Post events
1039 * BTN_TOUCH has to be first as mousedev relies on it when doing
1040 * absolute -> relative conversion
1041 */
1042 if (hw.z > 30) input_report_key(dev, BTN_TOUCH, 1);
1043 if (hw.z < 25) input_report_key(dev, BTN_TOUCH, 0);
1044
Henrik Rydberg4f56ce92010-12-18 15:42:30 +01001045 if (num_fingers > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 input_report_abs(dev, ABS_X, hw.x);
Daniel Kurtz6de58dd2011-08-23 23:00:24 -07001047 input_report_abs(dev, ABS_Y, synaptics_invert_y(hw.y));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 }
1049 input_report_abs(dev, ABS_PRESSURE, hw.z);
1050
Chris Bagwell2a8e7712010-07-19 09:06:15 -07001051 if (SYN_CAP_PALMDETECT(priv->capabilities))
1052 input_report_abs(dev, ABS_TOOL_WIDTH, finger_width);
1053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 input_report_key(dev, BTN_TOOL_FINGER, num_fingers == 1);
Peter Hutterere42b6642008-11-20 15:24:42 -05001055 if (SYN_CAP_MULTIFINGER(priv->capabilities)) {
1056 input_report_key(dev, BTN_TOOL_DOUBLETAP, num_fingers == 2);
1057 input_report_key(dev, BTN_TOOL_TRIPLETAP, num_fingers == 3);
1058 }
1059
Daniel Kurtz3cdfee92011-08-23 23:02:25 -07001060 synaptics_report_buttons(psmouse, &hw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
1062 input_sync(dev);
1063}
1064
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001065static int synaptics_validate_byte(struct psmouse *psmouse,
1066 int idx, unsigned char pkt_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067{
Helge Dellere38de672006-09-10 21:54:39 -04001068 static const unsigned char newabs_mask[] = { 0xC8, 0x00, 0x00, 0xC8, 0x00 };
1069 static const unsigned char newabs_rel_mask[] = { 0xC0, 0x00, 0x00, 0xC0, 0x00 };
1070 static const unsigned char newabs_rslt[] = { 0x80, 0x00, 0x00, 0xC0, 0x00 };
1071 static const unsigned char oldabs_mask[] = { 0xC0, 0x60, 0x00, 0xC0, 0x60 };
1072 static const unsigned char oldabs_rslt[] = { 0xC0, 0x00, 0x00, 0x80, 0x00 };
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001073 const char *packet = psmouse->packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
1075 if (idx < 0 || idx > 4)
1076 return 0;
1077
1078 switch (pkt_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Dmitry Torokhova62f0d22010-05-19 10:39:17 -07001080 case SYN_NEWABS:
1081 case SYN_NEWABS_RELAXED:
1082 return (packet[idx] & newabs_rel_mask[idx]) == newabs_rslt[idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
Dmitry Torokhova62f0d22010-05-19 10:39:17 -07001084 case SYN_NEWABS_STRICT:
1085 return (packet[idx] & newabs_mask[idx]) == newabs_rslt[idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
Dmitry Torokhova62f0d22010-05-19 10:39:17 -07001087 case SYN_OLDABS:
1088 return (packet[idx] & oldabs_mask[idx]) == oldabs_rslt[idx];
1089
1090 default:
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001091 psmouse_err(psmouse, "unknown packet type %d\n", pkt_type);
Dmitry Torokhova62f0d22010-05-19 10:39:17 -07001092 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 }
1094}
1095
1096static unsigned char synaptics_detect_pkt_type(struct psmouse *psmouse)
1097{
1098 int i;
1099
1100 for (i = 0; i < 5; i++)
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001101 if (!synaptics_validate_byte(psmouse, i, SYN_NEWABS_STRICT)) {
1102 psmouse_info(psmouse, "using relaxed packet validation\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 return SYN_NEWABS_RELAXED;
1104 }
1105
1106 return SYN_NEWABS_STRICT;
1107}
1108
David Howells7d12e782006-10-05 14:55:46 +01001109static psmouse_ret_t synaptics_process_byte(struct psmouse *psmouse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 struct synaptics_data *priv = psmouse->private;
1112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 if (psmouse->pktcnt >= 6) { /* Full packet received */
1114 if (unlikely(priv->pkt_type == SYN_NEWABS))
1115 priv->pkt_type = synaptics_detect_pkt_type(psmouse);
1116
Dmitry Torokhova8b3c0f2010-10-04 21:46:10 -07001117 if (SYN_CAP_PASS_THROUGH(priv->capabilities) &&
1118 synaptics_is_pt_packet(psmouse->packet)) {
1119 if (priv->pt_port)
1120 synaptics_pass_pt_packet(priv->pt_port, psmouse->packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 } else
1122 synaptics_process_packet(psmouse);
1123
1124 return PSMOUSE_FULL_PACKET;
1125 }
1126
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001127 return synaptics_validate_byte(psmouse, psmouse->pktcnt - 1, priv->pkt_type) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 PSMOUSE_GOOD_DATA : PSMOUSE_BAD_DATA;
1129}
1130
1131/*****************************************************************************
1132 * Driver initialization/cleanup functions
1133 ****************************************************************************/
Daniel Kurtz85615472011-08-23 23:00:41 -07001134static void set_abs_position_params(struct input_dev *dev,
1135 struct synaptics_data *priv, int x_code,
1136 int y_code)
1137{
1138 int x_min = priv->x_min ?: XMIN_NOMINAL;
1139 int x_max = priv->x_max ?: XMAX_NOMINAL;
1140 int y_min = priv->y_min ?: YMIN_NOMINAL;
1141 int y_max = priv->y_max ?: YMAX_NOMINAL;
1142 int fuzz = SYN_CAP_REDUCED_FILTERING(priv->ext_cap_0c) ?
1143 SYN_REDUCED_FILTER_FUZZ : 0;
1144
1145 input_set_abs_params(dev, x_code, x_min, x_max, fuzz, 0);
1146 input_set_abs_params(dev, y_code, y_min, y_max, fuzz, 0);
1147 input_abs_set_res(dev, x_code, priv->x_res);
1148 input_abs_set_res(dev, y_code, priv->y_res);
1149}
1150
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151static void set_input_params(struct input_dev *dev, struct synaptics_data *priv)
1152{
1153 int i;
1154
Daniel Drake7968a5d2011-11-08 00:00:35 -08001155 /* Things that apply to both modes */
Henrik Rydbergc14890a2010-12-16 09:52:23 +01001156 __set_bit(INPUT_PROP_POINTER, dev->propbit);
Daniel Drake7968a5d2011-11-08 00:00:35 -08001157 __set_bit(EV_KEY, dev->evbit);
1158 __set_bit(BTN_LEFT, dev->keybit);
1159 __set_bit(BTN_RIGHT, dev->keybit);
Henrik Rydbergc14890a2010-12-16 09:52:23 +01001160
Daniel Drake7968a5d2011-11-08 00:00:35 -08001161 if (SYN_CAP_MIDDLE_BUTTON(priv->capabilities))
1162 __set_bit(BTN_MIDDLE, dev->keybit);
1163
1164 if (!priv->absolute_mode) {
1165 /* Relative mode */
1166 __set_bit(EV_REL, dev->evbit);
1167 __set_bit(REL_X, dev->relbit);
1168 __set_bit(REL_Y, dev->relbit);
1169 return;
1170 }
1171
1172 /* Absolute mode */
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001173 __set_bit(EV_ABS, dev->evbit);
Daniel Kurtz85615472011-08-23 23:00:41 -07001174 set_abs_position_params(dev, priv, ABS_X, ABS_Y);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 input_set_abs_params(dev, ABS_PRESSURE, 0, 255, 0, 0);
Chris Bagwell2a8e7712010-07-19 09:06:15 -07001176
Daniel Kurtz3cdfee92011-08-23 23:02:25 -07001177 if (SYN_CAP_IMAGE_SENSOR(priv->ext_cap_0c)) {
1178 input_mt_init_slots(dev, 2);
1179 set_abs_position_params(dev, priv, ABS_MT_POSITION_X,
1180 ABS_MT_POSITION_Y);
1181 /* Image sensors can report per-contact pressure */
1182 input_set_abs_params(dev, ABS_MT_PRESSURE, 0, 255, 0, 0);
Daniel Kurtz6b4b49f2011-08-23 23:02:56 -07001183
1184 /* Image sensors can signal 4 and 5 finger clicks */
1185 __set_bit(BTN_TOOL_QUADTAP, dev->keybit);
1186 __set_bit(BTN_TOOL_QUINTTAP, dev->keybit);
Daniel Kurtz3cdfee92011-08-23 23:02:25 -07001187 } else if (SYN_CAP_ADV_GESTURE(priv->ext_cap_0c)) {
1188 /* Non-image sensors with AGM use semi-mt */
Henrik Rydbergfec6e522010-12-21 18:11:25 +01001189 __set_bit(INPUT_PROP_SEMI_MT, dev->propbit);
1190 input_mt_init_slots(dev, 2);
Daniel Kurtz85615472011-08-23 23:00:41 -07001191 set_abs_position_params(dev, priv, ABS_MT_POSITION_X,
1192 ABS_MT_POSITION_Y);
Henrik Rydbergfec6e522010-12-21 18:11:25 +01001193 }
1194
Chris Bagwell2a8e7712010-07-19 09:06:15 -07001195 if (SYN_CAP_PALMDETECT(priv->capabilities))
Chris Bagwell58fb0212010-07-19 09:06:15 -07001196 input_set_abs_params(dev, ABS_TOOL_WIDTH, 0, 15, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001198 __set_bit(BTN_TOUCH, dev->keybit);
1199 __set_bit(BTN_TOOL_FINGER, dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Peter Hutterere42b6642008-11-20 15:24:42 -05001201 if (SYN_CAP_MULTIFINGER(priv->capabilities)) {
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001202 __set_bit(BTN_TOOL_DOUBLETAP, dev->keybit);
1203 __set_bit(BTN_TOOL_TRIPLETAP, dev->keybit);
Peter Hutterere42b6642008-11-20 15:24:42 -05001204 }
1205
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 if (SYN_CAP_FOUR_BUTTON(priv->capabilities) ||
1207 SYN_CAP_MIDDLE_BUTTON(priv->capabilities)) {
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001208 __set_bit(BTN_FORWARD, dev->keybit);
1209 __set_bit(BTN_BACK, dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 }
1211
1212 for (i = 0; i < SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap); i++)
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001213 __set_bit(BTN_0 + i, dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Dmitry Torokhovb7802c52009-09-09 19:13:20 -07001215 __clear_bit(EV_REL, dev->evbit);
1216 __clear_bit(REL_X, dev->relbit);
1217 __clear_bit(REL_Y, dev->relbit);
Tero Saarniec20a022009-06-10 23:27:24 -07001218
Takashi Iwai5f57d672010-04-19 10:37:21 -07001219 if (SYN_CAP_CLICKPAD(priv->ext_cap_0c)) {
Henrik Rydbergc14890a2010-12-16 09:52:23 +01001220 __set_bit(INPUT_PROP_BUTTONPAD, dev->propbit);
Takashi Iwai5f57d672010-04-19 10:37:21 -07001221 /* Clickpads report only left button */
1222 __clear_bit(BTN_RIGHT, dev->keybit);
1223 __clear_bit(BTN_MIDDLE, dev->keybit);
1224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225}
1226
Daniel Drake7968a5d2011-11-08 00:00:35 -08001227static ssize_t synaptics_show_disable_gesture(struct psmouse *psmouse,
1228 void *data, char *buf)
1229{
1230 struct synaptics_data *priv = psmouse->private;
1231
1232 return sprintf(buf, "%c\n", priv->disable_gesture ? '1' : '0');
1233}
1234
1235static ssize_t synaptics_set_disable_gesture(struct psmouse *psmouse,
1236 void *data, const char *buf,
1237 size_t len)
1238{
1239 struct synaptics_data *priv = psmouse->private;
1240 unsigned int value;
1241 int err;
1242
1243 err = kstrtouint(buf, 10, &value);
1244 if (err)
1245 return err;
1246
1247 if (value > 1)
1248 return -EINVAL;
1249
1250 if (value == priv->disable_gesture)
1251 return len;
1252
1253 priv->disable_gesture = value;
1254 if (value)
1255 priv->mode |= SYN_BIT_DISABLE_GESTURE;
1256 else
1257 priv->mode &= ~SYN_BIT_DISABLE_GESTURE;
1258
1259 if (synaptics_mode_cmd(psmouse, priv->mode))
1260 return -EIO;
1261
1262 return len;
1263}
1264
1265PSMOUSE_DEFINE_ATTR(disable_gesture, S_IWUSR | S_IRUGO, NULL,
1266 synaptics_show_disable_gesture,
1267 synaptics_set_disable_gesture);
1268
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269static void synaptics_disconnect(struct psmouse *psmouse)
1270{
Daniel Drake7968a5d2011-11-08 00:00:35 -08001271 struct synaptics_data *priv = psmouse->private;
1272
1273 if (!priv->absolute_mode && SYN_ID_DISGEST_SUPPORTED(priv->identity))
1274 device_remove_file(&psmouse->ps2dev.serio->dev,
1275 &psmouse_attr_disable_gesture.dattr);
1276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 synaptics_reset(psmouse);
Daniel Drake7968a5d2011-11-08 00:00:35 -08001278 kfree(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 psmouse->private = NULL;
1280}
1281
1282static int synaptics_reconnect(struct psmouse *psmouse)
1283{
1284 struct synaptics_data *priv = psmouse->private;
1285 struct synaptics_data old_priv = *priv;
Alexandre Peixoto Ferreirac63fe0a2011-01-28 22:05:14 -08001286 int retry = 0;
1287 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
Alexandre Peixoto Ferreirac63fe0a2011-01-28 22:05:14 -08001289 do {
1290 psmouse_reset(psmouse);
1291 error = synaptics_detect(psmouse, 0);
1292 } while (error && ++retry < 3);
Andy Whitcroft4d368452009-02-28 12:51:01 -08001293
Alexandre Peixoto Ferreirac63fe0a2011-01-28 22:05:14 -08001294 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 return -1;
1296
Alexandre Peixoto Ferreirac63fe0a2011-01-28 22:05:14 -08001297 if (retry > 1)
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001298 psmouse_dbg(psmouse, "reconnected after %d tries\n", retry);
Alexandre Peixoto Ferreirac63fe0a2011-01-28 22:05:14 -08001299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 if (synaptics_query_hardware(psmouse)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001301 psmouse_err(psmouse, "Unable to query device.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 return -1;
1303 }
1304
Daniel Drake7968a5d2011-11-08 00:00:35 -08001305 if (synaptics_set_mode(psmouse)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001306 psmouse_err(psmouse, "Unable to initialize device.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 return -1;
1308 }
1309
Alexandre Peixoto Ferreirabaddf582011-01-28 22:05:14 -08001310 if (old_priv.identity != priv->identity ||
1311 old_priv.model_id != priv->model_id ||
1312 old_priv.capabilities != priv->capabilities ||
1313 old_priv.ext_cap != priv->ext_cap) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001314 psmouse_err(psmouse,
1315 "hardware appears to be different: id(%ld-%ld), model(%ld-%ld), caps(%lx-%lx), ext(%lx-%lx).\n",
1316 old_priv.identity, priv->identity,
1317 old_priv.model_id, priv->model_id,
1318 old_priv.capabilities, priv->capabilities,
1319 old_priv.ext_cap, priv->ext_cap);
Alexandre Peixoto Ferreirabaddf582011-01-28 22:05:14 -08001320 return -1;
1321 }
1322
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 return 0;
1324}
1325
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001326static bool impaired_toshiba_kbc;
1327
1328static const struct dmi_system_id __initconst toshiba_dmi_table[] = {
1329#if defined(CONFIG_DMI) && defined(CONFIG_X86)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 {
Dmitry Torokhov9961e252009-12-04 10:24:20 -08001331 /* Toshiba Satellite */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 .matches = {
1333 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
Richard Thrippleton53a26702006-04-02 00:10:18 -05001334 DMI_MATCH(DMI_PRODUCT_NAME, "Satellite"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 },
1336 },
Simon Horman9ba5eaa2005-07-11 01:07:20 -05001337 {
Dmitry Torokhov9961e252009-12-04 10:24:20 -08001338 /* Toshiba Dynabook */
Simon Horman9ba5eaa2005-07-11 01:07:20 -05001339 .matches = {
1340 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
Richard Thrippleton53a26702006-04-02 00:10:18 -05001341 DMI_MATCH(DMI_PRODUCT_NAME, "dynabook"),
1342 },
1343 },
1344 {
Dmitry Torokhov9961e252009-12-04 10:24:20 -08001345 /* Toshiba Portege M300 */
Richard Thrippleton53a26702006-04-02 00:10:18 -05001346 .matches = {
1347 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1348 DMI_MATCH(DMI_PRODUCT_NAME, "PORTEGE M300"),
Simon Horman9ba5eaa2005-07-11 01:07:20 -05001349 },
Dmitry Torokhov5f5eeff2009-10-12 21:35:00 -07001350
1351 },
1352 {
Dmitry Torokhov9961e252009-12-04 10:24:20 -08001353 /* Toshiba Portege M300 */
Dmitry Torokhov5f5eeff2009-10-12 21:35:00 -07001354 .matches = {
1355 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1356 DMI_MATCH(DMI_PRODUCT_NAME, "Portable PC"),
1357 DMI_MATCH(DMI_PRODUCT_VERSION, "Version 1.0"),
1358 },
1359
Simon Horman9ba5eaa2005-07-11 01:07:20 -05001360 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361#endif
Jan Beulich70874862011-03-31 00:01:58 -07001362 { }
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001363};
1364
Andres Salomonef8313b2010-12-23 01:19:38 -08001365static bool broken_olpc_ec;
1366
1367static const struct dmi_system_id __initconst olpc_dmi_table[] = {
1368#if defined(CONFIG_DMI) && defined(CONFIG_OLPC)
1369 {
1370 /* OLPC XO-1 or XO-1.5 */
1371 .matches = {
1372 DMI_MATCH(DMI_SYS_VENDOR, "OLPC"),
1373 DMI_MATCH(DMI_PRODUCT_NAME, "XO"),
1374 },
1375 },
Andres Salomonef8313b2010-12-23 01:19:38 -08001376#endif
Jan Beulich70874862011-03-31 00:01:58 -07001377 { }
Andres Salomonef8313b2010-12-23 01:19:38 -08001378};
1379
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001380void __init synaptics_module_init(void)
1381{
1382 impaired_toshiba_kbc = dmi_check_system(toshiba_dmi_table);
Andres Salomonef8313b2010-12-23 01:19:38 -08001383 broken_olpc_ec = dmi_check_system(olpc_dmi_table);
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001384}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
Daniel Drake7968a5d2011-11-08 00:00:35 -08001386static int __synaptics_init(struct psmouse *psmouse, bool absolute_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387{
1388 struct synaptics_data *priv;
Daniel Drake7968a5d2011-11-08 00:00:35 -08001389 int err = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
Andres Salomonef8313b2010-12-23 01:19:38 -08001391 /*
1392 * The OLPC XO has issues with Synaptics' absolute mode; similarly to
1393 * the HGPK, it quickly degrades and the hardware becomes jumpy and
1394 * overly sensitive. Not only that, but the constant packet spew
1395 * (even at a lowered 40pps rate) overloads the EC such that key
1396 * presses on the keyboard are missed. Given all of that, don't
1397 * even attempt to use Synaptics mode. Relative mode seems to work
1398 * just fine.
1399 */
1400 if (broken_olpc_ec) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001401 psmouse_info(psmouse,
1402 "OLPC XO detected, not enabling Synaptics protocol.\n");
Andres Salomonef8313b2010-12-23 01:19:38 -08001403 return -ENODEV;
1404 }
1405
Eric Sesterhennb39787a2006-03-14 00:09:16 -05001406 psmouse->private = priv = kzalloc(sizeof(struct synaptics_data), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 if (!priv)
Davidlohr Bueso6792cbb2010-09-29 18:53:35 -07001408 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Andy Whitcroft4d368452009-02-28 12:51:01 -08001410 psmouse_reset(psmouse);
1411
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 if (synaptics_query_hardware(psmouse)) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001413 psmouse_err(psmouse, "Unable to query device.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 goto init_fail;
1415 }
1416
Daniel Drake7968a5d2011-11-08 00:00:35 -08001417 priv->absolute_mode = absolute_mode;
1418 if (SYN_ID_DISGEST_SUPPORTED(priv->identity))
1419 priv->disable_gesture = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
Daniel Drake7968a5d2011-11-08 00:00:35 -08001421 if (synaptics_set_mode(psmouse)) {
1422 psmouse_err(psmouse, "Unable to initialize device.\n");
Henrik Rydbergfec6e522010-12-21 18:11:25 +01001423 goto init_fail;
1424 }
1425
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 priv->pkt_type = SYN_MODEL_NEWABS(priv->model_id) ? SYN_NEWABS : SYN_OLDABS;
1427
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001428 psmouse_info(psmouse,
1429 "Touchpad model: %ld, fw: %ld.%ld, id: %#lx, caps: %#lx/%#lx/%#lx\n",
1430 SYN_ID_MODEL(priv->identity),
1431 SYN_ID_MAJOR(priv->identity), SYN_ID_MINOR(priv->identity),
1432 priv->model_id,
1433 priv->capabilities, priv->ext_cap, priv->ext_cap_0c);
Dmitry Torokhov409b7502005-05-28 02:12:18 -05001434
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -05001435 set_input_params(psmouse->dev, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
Dmitry Torokhov887cc122007-04-12 01:30:41 -04001437 /*
1438 * Encode touchpad model so that it can be used to set
1439 * input device->id.version and be visible to userspace.
1440 * Because version is __u16 we have to drop something.
1441 * Hardware info bits seem to be good candidates as they
1442 * are documented to be for Synaptics corp. internal use.
1443 */
1444 psmouse->model = ((priv->model_id & 0x00ff0000) >> 8) |
1445 (priv->model_id & 0x000000ff);
1446
Daniel Drake7968a5d2011-11-08 00:00:35 -08001447 if (absolute_mode) {
1448 psmouse->protocol_handler = synaptics_process_byte;
1449 psmouse->pktsize = 6;
1450 } else {
1451 /* Relative mode follows standard PS/2 mouse protocol */
1452 psmouse->protocol_handler = psmouse_process_byte;
1453 psmouse->pktsize = 3;
1454 }
1455
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 psmouse->set_rate = synaptics_set_rate;
1457 psmouse->disconnect = synaptics_disconnect;
1458 psmouse->reconnect = synaptics_reconnect;
Dmitry Torokhova1cec062007-02-18 01:40:24 -05001459 psmouse->cleanup = synaptics_reset;
Dmitry Torokhovf0d5c6f42006-01-14 00:27:37 -05001460 /* Synaptics can usually stay in sync without extra help */
1461 psmouse->resync_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
1463 if (SYN_CAP_PASS_THROUGH(priv->capabilities))
1464 synaptics_pt_create(psmouse);
1465
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 /*
1467 * Toshiba's KBC seems to have trouble handling data from
Andres Salomon7ee99162010-12-23 01:18:28 -08001468 * Synaptics at full rate. Switch to a lower rate (roughly
1469 * the same rate as a standard PS/2 mouse).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 */
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001471 if (psmouse->rate >= 80 && impaired_toshiba_kbc) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -07001472 psmouse_info(psmouse,
1473 "Toshiba %s detected, limiting rate to 40pps.\n",
1474 dmi_get_system_info(DMI_PRODUCT_NAME));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 psmouse->rate = 40;
1476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Daniel Drake7968a5d2011-11-08 00:00:35 -08001478 if (!priv->absolute_mode && SYN_ID_DISGEST_SUPPORTED(priv->identity)) {
1479 err = device_create_file(&psmouse->ps2dev.serio->dev,
1480 &psmouse_attr_disable_gesture.dattr);
1481 if (err) {
1482 psmouse_err(psmouse,
1483 "Failed to create disable_gesture attribute (%d)",
1484 err);
1485 goto init_fail;
1486 }
1487 }
1488
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 return 0;
1490
1491 init_fail:
1492 kfree(priv);
Daniel Drake7968a5d2011-11-08 00:00:35 -08001493 return err;
1494}
1495
1496int synaptics_init(struct psmouse *psmouse)
1497{
1498 return __synaptics_init(psmouse, true);
1499}
1500
1501int synaptics_init_relative(struct psmouse *psmouse)
1502{
1503 return __synaptics_init(psmouse, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504}
1505
Daniel Drakee4e6efd2010-01-07 01:52:39 -08001506bool synaptics_supported(void)
1507{
1508 return true;
1509}
1510
Andres Salomon55e3d922007-03-10 01:39:54 -05001511#else /* CONFIG_MOUSE_PS2_SYNAPTICS */
1512
Dmitry Torokhov7705d542009-12-03 23:21:14 -08001513void __init synaptics_module_init(void)
1514{
1515}
1516
Andres Salomon55e3d922007-03-10 01:39:54 -05001517int synaptics_init(struct psmouse *psmouse)
1518{
1519 return -ENOSYS;
1520}
1521
Daniel Drakee4e6efd2010-01-07 01:52:39 -08001522bool synaptics_supported(void)
1523{
1524 return false;
1525}
1526
Andres Salomon55e3d922007-03-10 01:39:54 -05001527#endif /* CONFIG_MOUSE_PS2_SYNAPTICS */