]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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> | |
7705d548 | 27 | #include <linux/dmi.h> |
fec6e525 | 28 | #include <linux/input/mt.h> |
1da177e4 LT |
29 | #include <linux/serio.h> |
30 | #include <linux/libps2.h> | |
5a0e3ad6 | 31 | #include <linux/slab.h> |
1da177e4 LT |
32 | #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. | |
83ba9ea8 DT |
39 | * Note that newer firmware allows querying device for maximum useable |
40 | * coordinates. | |
1da177e4 LT |
41 | */ |
42 | #define XMIN_NOMINAL 1472 | |
43 | #define XMAX_NOMINAL 5472 | |
44 | #define YMIN_NOMINAL 1408 | |
45 | #define YMAX_NOMINAL 4448 | |
46 | ||
6de58dd6 DK |
47 | /* |
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 | */ | |
52 | static int synaptics_invert_y(int y) | |
53 | { | |
54 | return YMAX_NOMINAL + YMIN_NOMINAL - y; | |
55 | } | |
56 | ||
55e3d922 | 57 | |
1da177e4 | 58 | /***************************************************************************** |
55e3d922 | 59 | * Stuff we need even when we do not want native Synaptics support |
1da177e4 LT |
60 | ****************************************************************************/ |
61 | ||
62 | /* | |
55e3d922 | 63 | * Set the synaptics touchpad mode byte by special commands |
1da177e4 | 64 | */ |
55e3d922 | 65 | static int synaptics_mode_cmd(struct psmouse *psmouse, unsigned char mode) |
1da177e4 | 66 | { |
55e3d922 AS |
67 | unsigned char param[1]; |
68 | ||
69 | if (psmouse_sliced_command(psmouse, mode)) | |
1da177e4 | 70 | return -1; |
55e3d922 AS |
71 | param[0] = SYN_PS_SET_MODE2; |
72 | if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_SETRATE)) | |
1da177e4 LT |
73 | return -1; |
74 | return 0; | |
75 | } | |
76 | ||
b7802c5c | 77 | int synaptics_detect(struct psmouse *psmouse, bool set_properties) |
55e3d922 AS |
78 | { |
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 | ||
101 | void 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 | ||
1da177e4 | 113 | /* |
55e3d922 | 114 | * Send a command to the synpatics touchpad by special commands |
1da177e4 | 115 | */ |
55e3d922 | 116 | static int synaptics_send_cmd(struct psmouse *psmouse, unsigned char c, unsigned char *param) |
1da177e4 | 117 | { |
55e3d922 | 118 | if (psmouse_sliced_command(psmouse, c)) |
1da177e4 | 119 | return -1; |
55e3d922 | 120 | if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_GETINFO)) |
1da177e4 LT |
121 | return -1; |
122 | return 0; | |
123 | } | |
124 | ||
125 | /* | |
126 | * Read the model-id bytes from the touchpad | |
127 | * see also SYN_MODEL_* macros | |
128 | */ | |
129 | static 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 | */ | |
144 | static 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]; | |
5f57d67d TI |
152 | priv->ext_cap = priv->ext_cap_0c = 0; |
153 | ||
3619b8fe DT |
154 | /* |
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) { | |
1da177e4 | 159 | return -1; |
3619b8fe | 160 | } |
1da177e4 LT |
161 | |
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)) { | |
170 | printk(KERN_ERR "Synaptics claims to have extended capabilities," | |
5f57d67d | 171 | " but I'm not able to read them.\n"); |
1da177e4 LT |
172 | } 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 | } | |
5f57d67d TI |
183 | |
184 | if (SYN_EXT_CAP_REQUESTS(priv->capabilities) >= 4) { | |
185 | if (synaptics_send_cmd(psmouse, SYN_QUE_EXT_CAPAB_0C, cap)) { | |
186 | printk(KERN_ERR "Synaptics claims to have extended capability 0x0c," | |
187 | " but I'm not able to read it.\n"); | |
188 | } else { | |
189 | priv->ext_cap_0c = (cap[0] << 16) | (cap[1] << 8) | cap[2]; | |
190 | } | |
191 | } | |
192 | ||
1da177e4 LT |
193 | return 0; |
194 | } | |
195 | ||
196 | /* | |
197 | * Identify Touchpad | |
198 | * See also the SYN_ID_* macros | |
199 | */ | |
200 | static 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 | ||
ec20a022 | 213 | /* |
83ba9ea8 | 214 | * Read touchpad resolution and maximum reported coordinates |
ec20a022 TS |
215 | * Resolution is left zero if touchpad does not support the query |
216 | */ | |
217 | static int synaptics_resolution(struct psmouse *psmouse) | |
218 | { | |
219 | struct synaptics_data *priv = psmouse->private; | |
a66413fb | 220 | unsigned char resp[3]; |
ec20a022 TS |
221 | |
222 | if (SYN_ID_MAJOR(priv->identity) < 4) | |
bbddd199 | 223 | return 0; |
ec20a022 | 224 | |
a66413fb DT |
225 | 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 */ | |
83ba9ea8 DT |
229 | } |
230 | } | |
ec20a022 | 231 | |
83ba9ea8 DT |
232 | if (SYN_EXT_CAP_REQUESTS(priv->capabilities) >= 5 && |
233 | SYN_CAP_MAX_DIMENSIONS(priv->ext_cap_0c)) { | |
a66413fb DT |
234 | if (synaptics_send_cmd(psmouse, SYN_QUE_EXT_MAX_COORDS, resp)) { |
235 | printk(KERN_ERR "Synaptics claims to have max coordinates" | |
236 | " query, but I'm not able to read it.\n"); | |
237 | } else { | |
238 | 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)) { | |
246 | printk(KERN_ERR "Synaptics claims to have min coordinates" | |
247 | " query, but I'm not able to read it.\n"); | |
83ba9ea8 | 248 | } else { |
a66413fb DT |
249 | priv->x_min = (resp[0] << 5) | ((resp[1] & 0x0f) << 1); |
250 | priv->y_min = (resp[2] << 5) | ((resp[1] & 0xf0) >> 3); | |
83ba9ea8 | 251 | } |
ec20a022 TS |
252 | } |
253 | ||
254 | return 0; | |
255 | } | |
256 | ||
1da177e4 LT |
257 | static int synaptics_query_hardware(struct psmouse *psmouse) |
258 | { | |
1da177e4 LT |
259 | if (synaptics_identify(psmouse)) |
260 | return -1; | |
261 | if (synaptics_model_id(psmouse)) | |
262 | return -1; | |
263 | if (synaptics_capability(psmouse)) | |
264 | return -1; | |
ec20a022 TS |
265 | if (synaptics_resolution(psmouse)) |
266 | return -1; | |
1da177e4 LT |
267 | |
268 | return 0; | |
269 | } | |
270 | ||
271 | static int synaptics_set_absolute_mode(struct psmouse *psmouse) | |
272 | { | |
273 | struct synaptics_data *priv = psmouse->private; | |
274 | ||
275 | priv->mode = SYN_BIT_ABSOLUTE_MODE; | |
276 | if (SYN_ID_MAJOR(priv->identity) >= 4) | |
277 | priv->mode |= SYN_BIT_DISABLE_GESTURE; | |
278 | if (SYN_CAP_EXTENDED(priv->capabilities)) | |
279 | priv->mode |= SYN_BIT_W_MODE; | |
280 | ||
281 | if (synaptics_mode_cmd(psmouse, priv->mode)) | |
282 | return -1; | |
283 | ||
284 | return 0; | |
285 | } | |
286 | ||
287 | static void synaptics_set_rate(struct psmouse *psmouse, unsigned int rate) | |
288 | { | |
289 | struct synaptics_data *priv = psmouse->private; | |
290 | ||
291 | if (rate >= 80) { | |
292 | priv->mode |= SYN_BIT_HIGH_RATE; | |
293 | psmouse->rate = 80; | |
294 | } else { | |
295 | priv->mode &= ~SYN_BIT_HIGH_RATE; | |
296 | psmouse->rate = 40; | |
297 | } | |
298 | ||
299 | synaptics_mode_cmd(psmouse, priv->mode); | |
300 | } | |
301 | ||
fec6e525 HR |
302 | static int synaptics_set_advanced_gesture_mode(struct psmouse *psmouse) |
303 | { | |
304 | static unsigned char param = 0xc8; | |
305 | struct synaptics_data *priv = psmouse->private; | |
306 | ||
3cdfee9e DK |
307 | if (!(SYN_CAP_ADV_GESTURE(priv->ext_cap_0c) || |
308 | SYN_CAP_IMAGE_SENSOR(priv->ext_cap_0c))) | |
fec6e525 HR |
309 | return 0; |
310 | ||
311 | if (psmouse_sliced_command(psmouse, SYN_QUE_MODEL)) | |
312 | return -1; | |
313 | if (ps2_command(&psmouse->ps2dev, ¶m, PSMOUSE_CMD_SETRATE)) | |
314 | return -1; | |
315 | ||
316 | /* Advanced gesture mode also sends multi finger data */ | |
317 | priv->capabilities |= BIT(1); | |
318 | ||
319 | return 0; | |
320 | } | |
321 | ||
1da177e4 LT |
322 | /***************************************************************************** |
323 | * Synaptics pass-through PS/2 port support | |
324 | ****************************************************************************/ | |
325 | static int synaptics_pt_write(struct serio *serio, unsigned char c) | |
326 | { | |
327 | struct psmouse *parent = serio_get_drvdata(serio->parent); | |
328 | char rate_param = SYN_PS_CLIENT_CMD; /* indicates that we want pass-through port */ | |
329 | ||
330 | if (psmouse_sliced_command(parent, c)) | |
331 | return -1; | |
332 | if (ps2_command(&parent->ps2dev, &rate_param, PSMOUSE_CMD_SETRATE)) | |
333 | return -1; | |
334 | return 0; | |
335 | } | |
336 | ||
a8b3c0f5 DT |
337 | static int synaptics_pt_start(struct serio *serio) |
338 | { | |
339 | struct psmouse *parent = serio_get_drvdata(serio->parent); | |
340 | struct synaptics_data *priv = parent->private; | |
341 | ||
342 | serio_pause_rx(parent->ps2dev.serio); | |
343 | priv->pt_port = serio; | |
344 | serio_continue_rx(parent->ps2dev.serio); | |
345 | ||
346 | return 0; | |
347 | } | |
348 | ||
349 | static void synaptics_pt_stop(struct serio *serio) | |
350 | { | |
351 | struct psmouse *parent = serio_get_drvdata(serio->parent); | |
352 | struct synaptics_data *priv = parent->private; | |
353 | ||
354 | serio_pause_rx(parent->ps2dev.serio); | |
355 | priv->pt_port = NULL; | |
356 | serio_continue_rx(parent->ps2dev.serio); | |
357 | } | |
358 | ||
359 | static int synaptics_is_pt_packet(unsigned char *buf) | |
1da177e4 LT |
360 | { |
361 | return (buf[0] & 0xFC) == 0x84 && (buf[3] & 0xCC) == 0xC4; | |
362 | } | |
363 | ||
364 | static void synaptics_pass_pt_packet(struct serio *ptport, unsigned char *packet) | |
365 | { | |
366 | struct psmouse *child = serio_get_drvdata(ptport); | |
367 | ||
368 | if (child && child->state == PSMOUSE_ACTIVATED) { | |
7d12e780 DH |
369 | serio_interrupt(ptport, packet[1], 0); |
370 | serio_interrupt(ptport, packet[4], 0); | |
371 | serio_interrupt(ptport, packet[5], 0); | |
33fdfa97 | 372 | if (child->pktsize == 4) |
7d12e780 | 373 | serio_interrupt(ptport, packet[2], 0); |
1da177e4 | 374 | } else |
7d12e780 | 375 | serio_interrupt(ptport, packet[1], 0); |
1da177e4 LT |
376 | } |
377 | ||
378 | static void synaptics_pt_activate(struct psmouse *psmouse) | |
379 | { | |
1da177e4 | 380 | struct synaptics_data *priv = psmouse->private; |
a8b3c0f5 | 381 | struct psmouse *child = serio_get_drvdata(priv->pt_port); |
1da177e4 LT |
382 | |
383 | /* adjust the touchpad to child's choice of protocol */ | |
384 | if (child) { | |
33fdfa97 | 385 | if (child->pktsize == 4) |
1da177e4 LT |
386 | priv->mode |= SYN_BIT_FOUR_BYTE_CLIENT; |
387 | else | |
388 | priv->mode &= ~SYN_BIT_FOUR_BYTE_CLIENT; | |
389 | ||
390 | if (synaptics_mode_cmd(psmouse, priv->mode)) | |
391 | printk(KERN_INFO "synaptics: failed to switch guest protocol\n"); | |
392 | } | |
393 | } | |
394 | ||
395 | static void synaptics_pt_create(struct psmouse *psmouse) | |
396 | { | |
397 | struct serio *serio; | |
398 | ||
b39787a9 | 399 | serio = kzalloc(sizeof(struct serio), GFP_KERNEL); |
1da177e4 LT |
400 | if (!serio) { |
401 | printk(KERN_ERR "synaptics: not enough memory to allocate pass-through port\n"); | |
402 | return; | |
403 | } | |
404 | ||
1da177e4 LT |
405 | serio->id.type = SERIO_PS_PSTHRU; |
406 | strlcpy(serio->name, "Synaptics pass-through", sizeof(serio->name)); | |
407 | strlcpy(serio->phys, "synaptics-pt/serio0", sizeof(serio->name)); | |
408 | serio->write = synaptics_pt_write; | |
a8b3c0f5 DT |
409 | serio->start = synaptics_pt_start; |
410 | serio->stop = synaptics_pt_stop; | |
1da177e4 LT |
411 | serio->parent = psmouse->ps2dev.serio; |
412 | ||
413 | psmouse->pt_activate = synaptics_pt_activate; | |
414 | ||
415 | printk(KERN_INFO "serio: %s port at %s\n", serio->name, psmouse->phys); | |
416 | serio_register_port(serio); | |
417 | } | |
418 | ||
419 | /***************************************************************************** | |
420 | * Functions to interpret the absolute mode packets | |
421 | ****************************************************************************/ | |
422 | ||
a6ca40c1 DK |
423 | static void synaptics_mt_state_set(struct synaptics_mt_state *state, int count, |
424 | int sgm, int agm) | |
425 | { | |
426 | state->count = count; | |
427 | state->sgm = sgm; | |
428 | state->agm = agm; | |
429 | } | |
430 | ||
7afdb842 | 431 | static void synaptics_parse_agm(const unsigned char buf[], |
a6ca40c1 DK |
432 | struct synaptics_data *priv, |
433 | struct synaptics_hw_state *hw) | |
7afdb842 DK |
434 | { |
435 | struct synaptics_hw_state *agm = &priv->agm; | |
a6ca40c1 DK |
436 | int agm_packet_type; |
437 | ||
438 | agm_packet_type = (buf[5] & 0x30) >> 4; | |
439 | switch (agm_packet_type) { | |
440 | case 1: | |
441 | /* Gesture packet: (x, y, z) half resolution */ | |
442 | agm->w = hw->w; | |
443 | agm->x = (((buf[4] & 0x0f) << 8) | buf[1]) << 1; | |
444 | agm->y = (((buf[4] & 0xf0) << 4) | buf[2]) << 1; | |
445 | agm->z = ((buf[3] & 0x30) | (buf[5] & 0x0f)) << 1; | |
446 | break; | |
447 | ||
448 | case 2: | |
449 | /* AGM-CONTACT packet: (count, sgm, agm) */ | |
450 | synaptics_mt_state_set(&agm->mt_state, buf[1], buf[2], buf[4]); | |
451 | break; | |
452 | ||
453 | default: | |
454 | break; | |
455 | } | |
4dc772d2 DK |
456 | |
457 | /* Record that at least one AGM has been received since last SGM */ | |
458 | priv->agm_pending = true; | |
7afdb842 DK |
459 | } |
460 | ||
fec6e525 HR |
461 | static int synaptics_parse_hw_state(const unsigned char buf[], |
462 | struct synaptics_data *priv, | |
463 | struct synaptics_hw_state *hw) | |
1da177e4 LT |
464 | { |
465 | memset(hw, 0, sizeof(struct synaptics_hw_state)); | |
466 | ||
467 | if (SYN_MODEL_NEWABS(priv->model_id)) { | |
1da177e4 LT |
468 | hw->w = (((buf[0] & 0x30) >> 2) | |
469 | ((buf[0] & 0x04) >> 1) | | |
470 | ((buf[3] & 0x04) >> 2)); | |
471 | ||
472 | hw->left = (buf[0] & 0x01) ? 1 : 0; | |
473 | hw->right = (buf[0] & 0x02) ? 1 : 0; | |
474 | ||
5f57d67d TI |
475 | if (SYN_CAP_CLICKPAD(priv->ext_cap_0c)) { |
476 | /* | |
477 | * Clickpad's button is transmitted as middle button, | |
478 | * however, since it is primary button, we will report | |
479 | * it as BTN_LEFT. | |
480 | */ | |
481 | hw->left = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0; | |
482 | ||
483 | } else if (SYN_CAP_MIDDLE_BUTTON(priv->capabilities)) { | |
1da177e4 LT |
484 | hw->middle = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0; |
485 | if (hw->w == 2) | |
486 | hw->scroll = (signed char)(buf[1]); | |
487 | } | |
488 | ||
489 | if (SYN_CAP_FOUR_BUTTON(priv->capabilities)) { | |
490 | hw->up = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0; | |
491 | hw->down = ((buf[0] ^ buf[3]) & 0x02) ? 1 : 0; | |
492 | } | |
493 | ||
3cdfee9e DK |
494 | if ((SYN_CAP_ADV_GESTURE(priv->ext_cap_0c) || |
495 | SYN_CAP_IMAGE_SENSOR(priv->ext_cap_0c)) && | |
496 | hw->w == 2) { | |
a6ca40c1 | 497 | synaptics_parse_agm(buf, priv, hw); |
28d5fd86 DK |
498 | return 1; |
499 | } | |
500 | ||
501 | hw->x = (((buf[3] & 0x10) << 8) | | |
502 | ((buf[1] & 0x0f) << 8) | | |
503 | buf[4]); | |
504 | hw->y = (((buf[3] & 0x20) << 7) | | |
505 | ((buf[1] & 0xf0) << 4) | | |
506 | buf[5]); | |
507 | hw->z = buf[2]; | |
508 | ||
1da177e4 LT |
509 | if (SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap) && |
510 | ((buf[0] ^ buf[3]) & 0x02)) { | |
511 | switch (SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap) & ~0x01) { | |
512 | default: | |
513 | /* | |
514 | * if nExtBtn is greater than 8 it should be | |
515 | * considered invalid and treated as 0 | |
516 | */ | |
517 | break; | |
518 | case 8: | |
519 | hw->ext_buttons |= ((buf[5] & 0x08)) ? 0x80 : 0; | |
520 | hw->ext_buttons |= ((buf[4] & 0x08)) ? 0x40 : 0; | |
521 | case 6: | |
522 | hw->ext_buttons |= ((buf[5] & 0x04)) ? 0x20 : 0; | |
523 | hw->ext_buttons |= ((buf[4] & 0x04)) ? 0x10 : 0; | |
524 | case 4: | |
525 | hw->ext_buttons |= ((buf[5] & 0x02)) ? 0x08 : 0; | |
526 | hw->ext_buttons |= ((buf[4] & 0x02)) ? 0x04 : 0; | |
527 | case 2: | |
528 | hw->ext_buttons |= ((buf[5] & 0x01)) ? 0x02 : 0; | |
529 | hw->ext_buttons |= ((buf[4] & 0x01)) ? 0x01 : 0; | |
530 | } | |
531 | } | |
532 | } else { | |
533 | hw->x = (((buf[1] & 0x1f) << 8) | buf[2]); | |
534 | hw->y = (((buf[4] & 0x1f) << 8) | buf[5]); | |
535 | ||
536 | hw->z = (((buf[0] & 0x30) << 2) | (buf[3] & 0x3F)); | |
537 | hw->w = (((buf[1] & 0x80) >> 4) | ((buf[0] & 0x04) >> 1)); | |
538 | ||
539 | hw->left = (buf[0] & 0x01) ? 1 : 0; | |
540 | hw->right = (buf[0] & 0x02) ? 1 : 0; | |
541 | } | |
fec6e525 HR |
542 | |
543 | return 0; | |
544 | } | |
545 | ||
bea9f0ff DK |
546 | static void synaptics_report_semi_mt_slot(struct input_dev *dev, int slot, |
547 | bool active, int x, int y) | |
fec6e525 HR |
548 | { |
549 | input_mt_slot(dev, slot); | |
550 | input_mt_report_slot_state(dev, MT_TOOL_FINGER, active); | |
551 | if (active) { | |
552 | input_report_abs(dev, ABS_MT_POSITION_X, x); | |
6de58dd6 | 553 | input_report_abs(dev, ABS_MT_POSITION_Y, synaptics_invert_y(y)); |
fec6e525 HR |
554 | } |
555 | } | |
556 | ||
557 | static void synaptics_report_semi_mt_data(struct input_dev *dev, | |
558 | const struct synaptics_hw_state *a, | |
559 | const struct synaptics_hw_state *b, | |
560 | int num_fingers) | |
561 | { | |
562 | if (num_fingers >= 2) { | |
bea9f0ff DK |
563 | synaptics_report_semi_mt_slot(dev, 0, true, min(a->x, b->x), |
564 | min(a->y, b->y)); | |
565 | synaptics_report_semi_mt_slot(dev, 1, true, max(a->x, b->x), | |
566 | max(a->y, b->y)); | |
fec6e525 | 567 | } else if (num_fingers == 1) { |
bea9f0ff DK |
568 | synaptics_report_semi_mt_slot(dev, 0, true, a->x, a->y); |
569 | synaptics_report_semi_mt_slot(dev, 1, false, 0, 0); | |
fec6e525 | 570 | } else { |
bea9f0ff DK |
571 | synaptics_report_semi_mt_slot(dev, 0, false, 0, 0); |
572 | synaptics_report_semi_mt_slot(dev, 1, false, 0, 0); | |
fec6e525 | 573 | } |
1da177e4 LT |
574 | } |
575 | ||
3cdfee9e DK |
576 | static void synaptics_report_buttons(struct psmouse *psmouse, |
577 | const struct synaptics_hw_state *hw) | |
578 | { | |
579 | struct input_dev *dev = psmouse->dev; | |
580 | struct synaptics_data *priv = psmouse->private; | |
581 | int i; | |
582 | ||
583 | input_report_key(dev, BTN_LEFT, hw->left); | |
584 | input_report_key(dev, BTN_RIGHT, hw->right); | |
585 | ||
586 | if (SYN_CAP_MIDDLE_BUTTON(priv->capabilities)) | |
587 | input_report_key(dev, BTN_MIDDLE, hw->middle); | |
588 | ||
589 | if (SYN_CAP_FOUR_BUTTON(priv->capabilities)) { | |
590 | input_report_key(dev, BTN_FORWARD, hw->up); | |
591 | input_report_key(dev, BTN_BACK, hw->down); | |
592 | } | |
593 | ||
594 | for (i = 0; i < SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap); i++) | |
595 | input_report_key(dev, BTN_0 + i, hw->ext_buttons & (1 << i)); | |
596 | } | |
597 | ||
598 | static void synaptics_report_slot(struct input_dev *dev, int slot, | |
599 | const struct synaptics_hw_state *hw) | |
600 | { | |
601 | input_mt_slot(dev, slot); | |
602 | input_mt_report_slot_state(dev, MT_TOOL_FINGER, (hw != NULL)); | |
603 | if (!hw) | |
604 | return; | |
605 | ||
606 | input_report_abs(dev, ABS_MT_POSITION_X, hw->x); | |
607 | input_report_abs(dev, ABS_MT_POSITION_Y, synaptics_invert_y(hw->y)); | |
608 | input_report_abs(dev, ABS_MT_PRESSURE, hw->z); | |
609 | } | |
610 | ||
611 | static void synaptics_report_mt_data(struct psmouse *psmouse, | |
4dc772d2 | 612 | struct synaptics_mt_state *mt_state, |
3cdfee9e DK |
613 | const struct synaptics_hw_state *sgm) |
614 | { | |
615 | struct input_dev *dev = psmouse->dev; | |
616 | struct synaptics_data *priv = psmouse->private; | |
617 | struct synaptics_hw_state *agm = &priv->agm; | |
4dc772d2 | 618 | struct synaptics_mt_state *old = &priv->mt_state; |
3cdfee9e | 619 | |
4dc772d2 | 620 | switch (mt_state->count) { |
3cdfee9e DK |
621 | case 0: |
622 | synaptics_report_slot(dev, 0, NULL); | |
623 | synaptics_report_slot(dev, 1, NULL); | |
624 | break; | |
625 | case 1: | |
4dc772d2 DK |
626 | if (mt_state->sgm == -1) { |
627 | synaptics_report_slot(dev, 0, NULL); | |
628 | synaptics_report_slot(dev, 1, NULL); | |
629 | } else if (mt_state->sgm == 0) { | |
630 | synaptics_report_slot(dev, 0, sgm); | |
631 | synaptics_report_slot(dev, 1, NULL); | |
632 | } else { | |
633 | synaptics_report_slot(dev, 0, NULL); | |
634 | synaptics_report_slot(dev, 1, sgm); | |
635 | } | |
3cdfee9e | 636 | break; |
4dc772d2 DK |
637 | default: |
638 | /* | |
639 | * If the finger slot contained in SGM is valid, and either | |
640 | * hasn't changed, or is new, then report SGM in MTB slot 0. | |
641 | * Otherwise, empty MTB slot 0. | |
642 | */ | |
643 | if (mt_state->sgm != -1 && | |
644 | (mt_state->sgm == old->sgm || old->sgm == -1)) | |
645 | synaptics_report_slot(dev, 0, sgm); | |
646 | else | |
647 | synaptics_report_slot(dev, 0, NULL); | |
648 | ||
649 | /* | |
650 | * If the finger slot contained in AGM is valid, and either | |
651 | * hasn't changed, or is new, then report AGM in MTB slot 1. | |
652 | * Otherwise, empty MTB slot 1. | |
653 | */ | |
654 | if (mt_state->agm != -1 && | |
655 | (mt_state->agm == old->agm || old->agm == -1)) | |
656 | synaptics_report_slot(dev, 1, agm); | |
657 | else | |
658 | synaptics_report_slot(dev, 1, NULL); | |
3cdfee9e DK |
659 | break; |
660 | } | |
661 | ||
662 | /* Don't use active slot count to generate BTN_TOOL events. */ | |
663 | input_mt_report_pointer_emulation(dev, false); | |
664 | ||
665 | /* Send the number of fingers reported by touchpad itself. */ | |
4dc772d2 | 666 | input_mt_report_finger_count(dev, mt_state->count); |
3cdfee9e DK |
667 | |
668 | synaptics_report_buttons(psmouse, sgm); | |
669 | ||
670 | input_sync(dev); | |
671 | } | |
672 | ||
4dc772d2 DK |
673 | /* Handle case where mt_state->count = 0 */ |
674 | static void synaptics_image_sensor_0f(struct synaptics_data *priv, | |
675 | struct synaptics_mt_state *mt_state) | |
676 | { | |
677 | synaptics_mt_state_set(mt_state, 0, -1, -1); | |
678 | priv->mt_state_lost = false; | |
679 | } | |
680 | ||
681 | /* Handle case where mt_state->count = 1 */ | |
682 | static void synaptics_image_sensor_1f(struct synaptics_data *priv, | |
683 | struct synaptics_mt_state *mt_state) | |
684 | { | |
685 | struct synaptics_hw_state *agm = &priv->agm; | |
686 | struct synaptics_mt_state *old = &priv->mt_state; | |
687 | ||
688 | /* | |
689 | * If the last AGM was (0,0,0), and there is only one finger left, | |
690 | * then we absolutely know that SGM contains slot 0, and all other | |
691 | * fingers have been removed. | |
692 | */ | |
693 | if (priv->agm_pending && agm->z == 0) { | |
694 | synaptics_mt_state_set(mt_state, 1, 0, -1); | |
695 | priv->mt_state_lost = false; | |
696 | return; | |
697 | } | |
698 | ||
699 | switch (old->count) { | |
700 | case 0: | |
701 | synaptics_mt_state_set(mt_state, 1, 0, -1); | |
702 | break; | |
703 | case 1: | |
704 | /* | |
705 | * If mt_state_lost, then the previous transition was 3->1, | |
706 | * and SGM now contains either slot 0 or 1, but we don't know | |
707 | * which. So, we just assume that the SGM now contains slot 1. | |
708 | * | |
709 | * If pending AGM and either: | |
710 | * (a) the previous SGM slot contains slot 0, or | |
711 | * (b) there was no SGM slot | |
712 | * then, the SGM now contains slot 1 | |
713 | * | |
714 | * Case (a) happens with very rapid "drum roll" gestures, where | |
715 | * slot 0 finger is lifted and a new slot 1 finger touches | |
716 | * within one reporting interval. | |
717 | * | |
718 | * Case (b) happens if initially two or more fingers tap | |
719 | * briefly, and all but one lift before the end of the first | |
720 | * reporting interval. | |
721 | * | |
722 | * (In both these cases, slot 0 will becomes empty, so SGM | |
723 | * contains slot 1 with the new finger) | |
724 | * | |
725 | * Else, if there was no previous SGM, it now contains slot 0. | |
726 | * | |
727 | * Otherwise, SGM still contains the same slot. | |
728 | */ | |
729 | if (priv->mt_state_lost || | |
730 | (priv->agm_pending && old->sgm <= 0)) | |
731 | synaptics_mt_state_set(mt_state, 1, 1, -1); | |
732 | else if (old->sgm == -1) | |
733 | synaptics_mt_state_set(mt_state, 1, 0, -1); | |
734 | break; | |
735 | case 2: | |
736 | /* | |
737 | * If mt_state_lost, we don't know which finger SGM contains. | |
738 | * | |
739 | * So, report 1 finger, but with both slots empty. | |
740 | * We will use slot 1 on subsequent 1->1 | |
741 | */ | |
742 | if (priv->mt_state_lost) { | |
743 | synaptics_mt_state_set(mt_state, 1, -1, -1); | |
744 | break; | |
745 | } | |
746 | /* | |
747 | * Since the last AGM was NOT (0,0,0), it was the finger in | |
748 | * slot 0 that has been removed. | |
749 | * So, SGM now contains previous AGM's slot, and AGM is now | |
750 | * empty. | |
751 | */ | |
752 | synaptics_mt_state_set(mt_state, 1, old->agm, -1); | |
753 | break; | |
754 | case 3: | |
755 | /* | |
756 | * Since last AGM was not (0,0,0), we don't know which finger | |
757 | * is left. | |
758 | * | |
759 | * So, report 1 finger, but with both slots empty. | |
760 | * We will use slot 1 on subsequent 1->1 | |
761 | */ | |
762 | synaptics_mt_state_set(mt_state, 1, -1, -1); | |
763 | priv->mt_state_lost = true; | |
764 | break; | |
6b4b49fe DK |
765 | case 4: |
766 | case 5: | |
767 | /* mt_state was updated by AGM-CONTACT packet */ | |
768 | break; | |
4dc772d2 DK |
769 | } |
770 | } | |
771 | ||
772 | /* Handle case where mt_state->count = 2 */ | |
773 | static void synaptics_image_sensor_2f(struct synaptics_data *priv, | |
774 | struct synaptics_mt_state *mt_state) | |
775 | { | |
776 | struct synaptics_mt_state *old = &priv->mt_state; | |
777 | ||
778 | switch (old->count) { | |
779 | case 0: | |
780 | synaptics_mt_state_set(mt_state, 2, 0, 1); | |
781 | break; | |
782 | case 1: | |
783 | /* | |
784 | * If previous SGM contained slot 1 or higher, SGM now contains | |
785 | * slot 0 (the newly touching finger) and AGM contains SGM's | |
786 | * previous slot. | |
787 | * | |
788 | * Otherwise, SGM still contains slot 0 and AGM now contains | |
789 | * slot 1. | |
790 | */ | |
791 | if (old->sgm >= 1) | |
792 | synaptics_mt_state_set(mt_state, 2, 0, old->sgm); | |
793 | else | |
794 | synaptics_mt_state_set(mt_state, 2, 0, 1); | |
795 | break; | |
796 | case 2: | |
797 | /* | |
798 | * If mt_state_lost, SGM now contains either finger 1 or 2, but | |
799 | * we don't know which. | |
800 | * So, we just assume that the SGM contains slot 0 and AGM 1. | |
801 | */ | |
802 | if (priv->mt_state_lost) | |
803 | synaptics_mt_state_set(mt_state, 2, 0, 1); | |
804 | /* | |
805 | * Otherwise, use the same mt_state, since it either hasn't | |
806 | * changed, or was updated by a recently received AGM-CONTACT | |
807 | * packet. | |
808 | */ | |
809 | break; | |
810 | case 3: | |
811 | /* | |
812 | * 3->2 transitions have two unsolvable problems: | |
813 | * 1) no indication is given which finger was removed | |
814 | * 2) no way to tell if agm packet was for finger 3 | |
815 | * before 3->2, or finger 2 after 3->2. | |
816 | * | |
817 | * So, report 2 fingers, but empty all slots. | |
818 | * We will guess slots [0,1] on subsequent 2->2. | |
819 | */ | |
820 | synaptics_mt_state_set(mt_state, 2, -1, -1); | |
821 | priv->mt_state_lost = true; | |
822 | break; | |
6b4b49fe DK |
823 | case 4: |
824 | case 5: | |
825 | /* mt_state was updated by AGM-CONTACT packet */ | |
826 | break; | |
4dc772d2 DK |
827 | } |
828 | } | |
829 | ||
830 | /* Handle case where mt_state->count = 3 */ | |
831 | static void synaptics_image_sensor_3f(struct synaptics_data *priv, | |
832 | struct synaptics_mt_state *mt_state) | |
833 | { | |
834 | struct synaptics_mt_state *old = &priv->mt_state; | |
835 | ||
836 | switch (old->count) { | |
837 | case 0: | |
838 | synaptics_mt_state_set(mt_state, 3, 0, 2); | |
839 | break; | |
840 | case 1: | |
841 | /* | |
842 | * If previous SGM contained slot 2 or higher, SGM now contains | |
843 | * slot 0 (one of the newly touching fingers) and AGM contains | |
844 | * SGM's previous slot. | |
845 | * | |
846 | * Otherwise, SGM now contains slot 0 and AGM contains slot 2. | |
847 | */ | |
848 | if (old->sgm >= 2) | |
849 | synaptics_mt_state_set(mt_state, 3, 0, old->sgm); | |
850 | else | |
851 | synaptics_mt_state_set(mt_state, 3, 0, 2); | |
852 | break; | |
853 | case 2: | |
6b4b49fe DK |
854 | /* |
855 | * If the AGM previously contained slot 3 or higher, then the | |
856 | * newly touching finger is in the lowest available slot. | |
857 | * | |
858 | * If SGM was previously 1 or higher, then the new SGM is | |
859 | * now slot 0 (with a new finger), otherwise, the new finger | |
860 | * is now in a hidden slot between 0 and AGM's slot. | |
861 | * | |
862 | * In all such cases, the SGM now contains slot 0, and the AGM | |
863 | * continues to contain the same slot as before. | |
864 | */ | |
865 | if (old->agm >= 3) { | |
866 | synaptics_mt_state_set(mt_state, 3, 0, old->agm); | |
867 | break; | |
868 | } | |
869 | ||
4dc772d2 DK |
870 | /* |
871 | * After some 3->1 and all 3->2 transitions, we lose track | |
872 | * of which slot is reported by SGM and AGM. | |
873 | * | |
874 | * For 2->3 in this state, report 3 fingers, but empty all | |
875 | * slots, and we will guess (0,2) on a subsequent 0->3. | |
876 | * | |
877 | * To userspace, the resulting transition will look like: | |
878 | * 2:[0,1] -> 3:[-1,-1] -> 3:[0,2] | |
879 | */ | |
880 | if (priv->mt_state_lost) { | |
881 | synaptics_mt_state_set(mt_state, 3, -1, -1); | |
882 | break; | |
883 | } | |
884 | ||
885 | /* | |
886 | * If the (SGM,AGM) really previously contained slots (0, 1), | |
887 | * then we cannot know what slot was just reported by the AGM, | |
888 | * because the 2->3 transition can occur either before or after | |
889 | * the AGM packet. Thus, this most recent AGM could contain | |
890 | * either the same old slot 1 or the new slot 2. | |
891 | * Subsequent AGMs will be reporting slot 2. | |
892 | * | |
893 | * To userspace, the resulting transition will look like: | |
894 | * 2:[0,1] -> 3:[0,-1] -> 3:[0,2] | |
895 | */ | |
896 | synaptics_mt_state_set(mt_state, 3, 0, -1); | |
897 | break; | |
898 | case 3: | |
899 | /* | |
900 | * If, for whatever reason, the previous agm was invalid, | |
901 | * Assume SGM now contains slot 0, AGM now contains slot 2. | |
902 | */ | |
903 | if (old->agm <= 2) | |
904 | synaptics_mt_state_set(mt_state, 3, 0, 2); | |
905 | /* | |
906 | * mt_state either hasn't changed, or was updated by a recently | |
907 | * received AGM-CONTACT packet. | |
908 | */ | |
909 | break; | |
6b4b49fe DK |
910 | |
911 | case 4: | |
912 | case 5: | |
913 | /* mt_state was updated by AGM-CONTACT packet */ | |
914 | break; | |
4dc772d2 DK |
915 | } |
916 | } | |
917 | ||
6b4b49fe DK |
918 | /* Handle case where mt_state->count = 4, or = 5 */ |
919 | static void synaptics_image_sensor_45f(struct synaptics_data *priv, | |
920 | struct synaptics_mt_state *mt_state) | |
921 | { | |
922 | /* mt_state was updated correctly by AGM-CONTACT packet */ | |
923 | priv->mt_state_lost = false; | |
924 | } | |
925 | ||
3cdfee9e DK |
926 | static void synaptics_image_sensor_process(struct psmouse *psmouse, |
927 | struct synaptics_hw_state *sgm) | |
928 | { | |
4dc772d2 DK |
929 | struct synaptics_data *priv = psmouse->private; |
930 | struct synaptics_hw_state *agm = &priv->agm; | |
931 | struct synaptics_mt_state mt_state; | |
3cdfee9e | 932 | |
4dc772d2 DK |
933 | /* Initialize using current mt_state (as updated by last agm) */ |
934 | mt_state = agm->mt_state; | |
935 | ||
936 | /* | |
937 | * Update mt_state using the new finger count and current mt_state. | |
938 | */ | |
3cdfee9e | 939 | if (sgm->z == 0) |
4dc772d2 | 940 | synaptics_image_sensor_0f(priv, &mt_state); |
3cdfee9e | 941 | else if (sgm->w >= 4) |
4dc772d2 | 942 | synaptics_image_sensor_1f(priv, &mt_state); |
3cdfee9e | 943 | else if (sgm->w == 0) |
4dc772d2 | 944 | synaptics_image_sensor_2f(priv, &mt_state); |
6b4b49fe | 945 | else if (sgm->w == 1 && mt_state.count <= 3) |
4dc772d2 | 946 | synaptics_image_sensor_3f(priv, &mt_state); |
6b4b49fe DK |
947 | else |
948 | synaptics_image_sensor_45f(priv, &mt_state); | |
3cdfee9e DK |
949 | |
950 | /* Send resulting input events to user space */ | |
4dc772d2 DK |
951 | synaptics_report_mt_data(psmouse, &mt_state, sgm); |
952 | ||
953 | /* Store updated mt_state */ | |
954 | priv->mt_state = agm->mt_state = mt_state; | |
955 | priv->agm_pending = false; | |
3cdfee9e DK |
956 | } |
957 | ||
1da177e4 LT |
958 | /* |
959 | * called for each full received packet from the touchpad | |
960 | */ | |
961 | static void synaptics_process_packet(struct psmouse *psmouse) | |
962 | { | |
2e5b636b | 963 | struct input_dev *dev = psmouse->dev; |
1da177e4 LT |
964 | struct synaptics_data *priv = psmouse->private; |
965 | struct synaptics_hw_state hw; | |
966 | int num_fingers; | |
967 | int finger_width; | |
1da177e4 | 968 | |
fec6e525 HR |
969 | if (synaptics_parse_hw_state(psmouse->packet, priv, &hw)) |
970 | return; | |
1da177e4 | 971 | |
3cdfee9e DK |
972 | if (SYN_CAP_IMAGE_SENSOR(priv->ext_cap_0c)) { |
973 | synaptics_image_sensor_process(psmouse, &hw); | |
974 | return; | |
975 | } | |
976 | ||
1da177e4 LT |
977 | if (hw.scroll) { |
978 | priv->scroll += hw.scroll; | |
979 | ||
980 | while (priv->scroll >= 4) { | |
981 | input_report_key(dev, BTN_BACK, !hw.down); | |
982 | input_sync(dev); | |
983 | input_report_key(dev, BTN_BACK, hw.down); | |
984 | input_sync(dev); | |
985 | priv->scroll -= 4; | |
986 | } | |
987 | while (priv->scroll <= -4) { | |
988 | input_report_key(dev, BTN_FORWARD, !hw.up); | |
989 | input_sync(dev); | |
990 | input_report_key(dev, BTN_FORWARD, hw.up); | |
991 | input_sync(dev); | |
992 | priv->scroll += 4; | |
993 | } | |
994 | return; | |
995 | } | |
996 | ||
4f56ce92 | 997 | if (hw.z > 0 && hw.x > 1) { |
1da177e4 LT |
998 | num_fingers = 1; |
999 | finger_width = 5; | |
1000 | if (SYN_CAP_EXTENDED(priv->capabilities)) { | |
1001 | switch (hw.w) { | |
1002 | case 0 ... 1: | |
1003 | if (SYN_CAP_MULTIFINGER(priv->capabilities)) | |
1004 | num_fingers = hw.w + 2; | |
1005 | break; | |
1006 | case 2: | |
1007 | if (SYN_MODEL_PEN(priv->model_id)) | |
1008 | ; /* Nothing, treat a pen as a single finger */ | |
1009 | break; | |
1010 | case 4 ... 15: | |
1011 | if (SYN_CAP_PALMDETECT(priv->capabilities)) | |
1012 | finger_width = hw.w; | |
1013 | break; | |
1014 | } | |
1015 | } | |
1016 | } else { | |
1017 | num_fingers = 0; | |
1018 | finger_width = 0; | |
1019 | } | |
1020 | ||
fec6e525 | 1021 | if (SYN_CAP_ADV_GESTURE(priv->ext_cap_0c)) |
7afdb842 DK |
1022 | synaptics_report_semi_mt_data(dev, &hw, &priv->agm, |
1023 | num_fingers); | |
fec6e525 | 1024 | |
1da177e4 LT |
1025 | /* Post events |
1026 | * BTN_TOUCH has to be first as mousedev relies on it when doing | |
1027 | * absolute -> relative conversion | |
1028 | */ | |
1029 | if (hw.z > 30) input_report_key(dev, BTN_TOUCH, 1); | |
1030 | if (hw.z < 25) input_report_key(dev, BTN_TOUCH, 0); | |
1031 | ||
4f56ce92 | 1032 | if (num_fingers > 0) { |
1da177e4 | 1033 | input_report_abs(dev, ABS_X, hw.x); |
6de58dd6 | 1034 | input_report_abs(dev, ABS_Y, synaptics_invert_y(hw.y)); |
1da177e4 LT |
1035 | } |
1036 | input_report_abs(dev, ABS_PRESSURE, hw.z); | |
1037 | ||
2a8e7710 CB |
1038 | if (SYN_CAP_PALMDETECT(priv->capabilities)) |
1039 | input_report_abs(dev, ABS_TOOL_WIDTH, finger_width); | |
1040 | ||
1da177e4 | 1041 | input_report_key(dev, BTN_TOOL_FINGER, num_fingers == 1); |
e42b6646 PH |
1042 | if (SYN_CAP_MULTIFINGER(priv->capabilities)) { |
1043 | input_report_key(dev, BTN_TOOL_DOUBLETAP, num_fingers == 2); | |
1044 | input_report_key(dev, BTN_TOOL_TRIPLETAP, num_fingers == 3); | |
1045 | } | |
1046 | ||
3cdfee9e | 1047 | synaptics_report_buttons(psmouse, &hw); |
1da177e4 LT |
1048 | |
1049 | input_sync(dev); | |
1050 | } | |
1051 | ||
1052 | static int synaptics_validate_byte(unsigned char packet[], int idx, unsigned char pkt_type) | |
1053 | { | |
e38de678 HD |
1054 | static const unsigned char newabs_mask[] = { 0xC8, 0x00, 0x00, 0xC8, 0x00 }; |
1055 | static const unsigned char newabs_rel_mask[] = { 0xC0, 0x00, 0x00, 0xC0, 0x00 }; | |
1056 | static const unsigned char newabs_rslt[] = { 0x80, 0x00, 0x00, 0xC0, 0x00 }; | |
1057 | static const unsigned char oldabs_mask[] = { 0xC0, 0x60, 0x00, 0xC0, 0x60 }; | |
1058 | static const unsigned char oldabs_rslt[] = { 0xC0, 0x00, 0x00, 0x80, 0x00 }; | |
1da177e4 LT |
1059 | |
1060 | if (idx < 0 || idx > 4) | |
1061 | return 0; | |
1062 | ||
1063 | switch (pkt_type) { | |
1da177e4 | 1064 | |
a62f0d27 DT |
1065 | case SYN_NEWABS: |
1066 | case SYN_NEWABS_RELAXED: | |
1067 | return (packet[idx] & newabs_rel_mask[idx]) == newabs_rslt[idx]; | |
1da177e4 | 1068 | |
a62f0d27 DT |
1069 | case SYN_NEWABS_STRICT: |
1070 | return (packet[idx] & newabs_mask[idx]) == newabs_rslt[idx]; | |
1da177e4 | 1071 | |
a62f0d27 DT |
1072 | case SYN_OLDABS: |
1073 | return (packet[idx] & oldabs_mask[idx]) == oldabs_rslt[idx]; | |
1074 | ||
1075 | default: | |
1076 | printk(KERN_ERR "synaptics: unknown packet type %d\n", pkt_type); | |
1077 | return 0; | |
1da177e4 LT |
1078 | } |
1079 | } | |
1080 | ||
1081 | static unsigned char synaptics_detect_pkt_type(struct psmouse *psmouse) | |
1082 | { | |
1083 | int i; | |
1084 | ||
1085 | for (i = 0; i < 5; i++) | |
1086 | if (!synaptics_validate_byte(psmouse->packet, i, SYN_NEWABS_STRICT)) { | |
1087 | printk(KERN_INFO "synaptics: using relaxed packet validation\n"); | |
1088 | return SYN_NEWABS_RELAXED; | |
1089 | } | |
1090 | ||
1091 | return SYN_NEWABS_STRICT; | |
1092 | } | |
1093 | ||
7d12e780 | 1094 | static psmouse_ret_t synaptics_process_byte(struct psmouse *psmouse) |
1da177e4 | 1095 | { |
1da177e4 LT |
1096 | struct synaptics_data *priv = psmouse->private; |
1097 | ||
1da177e4 LT |
1098 | if (psmouse->pktcnt >= 6) { /* Full packet received */ |
1099 | if (unlikely(priv->pkt_type == SYN_NEWABS)) | |
1100 | priv->pkt_type = synaptics_detect_pkt_type(psmouse); | |
1101 | ||
a8b3c0f5 DT |
1102 | if (SYN_CAP_PASS_THROUGH(priv->capabilities) && |
1103 | synaptics_is_pt_packet(psmouse->packet)) { | |
1104 | if (priv->pt_port) | |
1105 | synaptics_pass_pt_packet(priv->pt_port, psmouse->packet); | |
1da177e4 LT |
1106 | } else |
1107 | synaptics_process_packet(psmouse); | |
1108 | ||
1109 | return PSMOUSE_FULL_PACKET; | |
1110 | } | |
1111 | ||
1112 | return synaptics_validate_byte(psmouse->packet, psmouse->pktcnt - 1, priv->pkt_type) ? | |
1113 | PSMOUSE_GOOD_DATA : PSMOUSE_BAD_DATA; | |
1114 | } | |
1115 | ||
1116 | /***************************************************************************** | |
1117 | * Driver initialization/cleanup functions | |
1118 | ****************************************************************************/ | |
85615476 DK |
1119 | static void set_abs_position_params(struct input_dev *dev, |
1120 | struct synaptics_data *priv, int x_code, | |
1121 | int y_code) | |
1da177e4 | 1122 | { |
85615476 DK |
1123 | int x_min = priv->x_min ?: XMIN_NOMINAL; |
1124 | int x_max = priv->x_max ?: XMAX_NOMINAL; | |
1125 | int y_min = priv->y_min ?: YMIN_NOMINAL; | |
1126 | int y_max = priv->y_max ?: YMAX_NOMINAL; | |
a9f0b79e DK |
1127 | int fuzz = SYN_CAP_REDUCED_FILTERING(priv->ext_cap_0c) ? |
1128 | SYN_REDUCED_FILTER_FUZZ : 0; | |
1da177e4 | 1129 | |
85615476 DK |
1130 | input_set_abs_params(dev, x_code, x_min, x_max, fuzz, 0); |
1131 | input_set_abs_params(dev, y_code, y_min, y_max, fuzz, 0); | |
1132 | input_abs_set_res(dev, x_code, priv->x_res); | |
1133 | input_abs_set_res(dev, y_code, priv->y_res); | |
1134 | } | |
1135 | ||
1136 | static void set_input_params(struct input_dev *dev, struct synaptics_data *priv) | |
1137 | { | |
1138 | int i; | |
1139 | ||
c14890a8 HR |
1140 | __set_bit(INPUT_PROP_POINTER, dev->propbit); |
1141 | ||
b7802c5c | 1142 | __set_bit(EV_ABS, dev->evbit); |
85615476 | 1143 | set_abs_position_params(dev, priv, ABS_X, ABS_Y); |
1da177e4 | 1144 | input_set_abs_params(dev, ABS_PRESSURE, 0, 255, 0, 0); |
2a8e7710 | 1145 | |
3cdfee9e DK |
1146 | if (SYN_CAP_IMAGE_SENSOR(priv->ext_cap_0c)) { |
1147 | input_mt_init_slots(dev, 2); | |
1148 | set_abs_position_params(dev, priv, ABS_MT_POSITION_X, | |
1149 | ABS_MT_POSITION_Y); | |
1150 | /* Image sensors can report per-contact pressure */ | |
1151 | input_set_abs_params(dev, ABS_MT_PRESSURE, 0, 255, 0, 0); | |
6b4b49fe DK |
1152 | |
1153 | /* Image sensors can signal 4 and 5 finger clicks */ | |
1154 | __set_bit(BTN_TOOL_QUADTAP, dev->keybit); | |
1155 | __set_bit(BTN_TOOL_QUINTTAP, dev->keybit); | |
3cdfee9e DK |
1156 | } else if (SYN_CAP_ADV_GESTURE(priv->ext_cap_0c)) { |
1157 | /* Non-image sensors with AGM use semi-mt */ | |
fec6e525 HR |
1158 | __set_bit(INPUT_PROP_SEMI_MT, dev->propbit); |
1159 | input_mt_init_slots(dev, 2); | |
85615476 DK |
1160 | set_abs_position_params(dev, priv, ABS_MT_POSITION_X, |
1161 | ABS_MT_POSITION_Y); | |
fec6e525 HR |
1162 | } |
1163 | ||
2a8e7710 | 1164 | if (SYN_CAP_PALMDETECT(priv->capabilities)) |
58fb0218 | 1165 | input_set_abs_params(dev, ABS_TOOL_WIDTH, 0, 15, 0, 0); |
1da177e4 | 1166 | |
b7802c5c DT |
1167 | __set_bit(EV_KEY, dev->evbit); |
1168 | __set_bit(BTN_TOUCH, dev->keybit); | |
1169 | __set_bit(BTN_TOOL_FINGER, dev->keybit); | |
1170 | __set_bit(BTN_LEFT, dev->keybit); | |
1171 | __set_bit(BTN_RIGHT, dev->keybit); | |
1da177e4 | 1172 | |
e42b6646 | 1173 | if (SYN_CAP_MULTIFINGER(priv->capabilities)) { |
b7802c5c DT |
1174 | __set_bit(BTN_TOOL_DOUBLETAP, dev->keybit); |
1175 | __set_bit(BTN_TOOL_TRIPLETAP, dev->keybit); | |
e42b6646 PH |
1176 | } |
1177 | ||
1da177e4 | 1178 | if (SYN_CAP_MIDDLE_BUTTON(priv->capabilities)) |
b7802c5c | 1179 | __set_bit(BTN_MIDDLE, dev->keybit); |
1da177e4 LT |
1180 | |
1181 | if (SYN_CAP_FOUR_BUTTON(priv->capabilities) || | |
1182 | SYN_CAP_MIDDLE_BUTTON(priv->capabilities)) { | |
b7802c5c DT |
1183 | __set_bit(BTN_FORWARD, dev->keybit); |
1184 | __set_bit(BTN_BACK, dev->keybit); | |
1da177e4 LT |
1185 | } |
1186 | ||
1187 | for (i = 0; i < SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap); i++) | |
b7802c5c | 1188 | __set_bit(BTN_0 + i, dev->keybit); |
1da177e4 | 1189 | |
b7802c5c DT |
1190 | __clear_bit(EV_REL, dev->evbit); |
1191 | __clear_bit(REL_X, dev->relbit); | |
1192 | __clear_bit(REL_Y, dev->relbit); | |
ec20a022 | 1193 | |
5f57d67d | 1194 | if (SYN_CAP_CLICKPAD(priv->ext_cap_0c)) { |
c14890a8 | 1195 | __set_bit(INPUT_PROP_BUTTONPAD, dev->propbit); |
5f57d67d TI |
1196 | /* Clickpads report only left button */ |
1197 | __clear_bit(BTN_RIGHT, dev->keybit); | |
1198 | __clear_bit(BTN_MIDDLE, dev->keybit); | |
1199 | } | |
1da177e4 LT |
1200 | } |
1201 | ||
1da177e4 LT |
1202 | static void synaptics_disconnect(struct psmouse *psmouse) |
1203 | { | |
1204 | synaptics_reset(psmouse); | |
1205 | kfree(psmouse->private); | |
1206 | psmouse->private = NULL; | |
1207 | } | |
1208 | ||
1209 | static int synaptics_reconnect(struct psmouse *psmouse) | |
1210 | { | |
1211 | struct synaptics_data *priv = psmouse->private; | |
1212 | struct synaptics_data old_priv = *priv; | |
c63fe0a4 APF |
1213 | int retry = 0; |
1214 | int error; | |
1da177e4 | 1215 | |
c63fe0a4 APF |
1216 | do { |
1217 | psmouse_reset(psmouse); | |
1218 | error = synaptics_detect(psmouse, 0); | |
1219 | } while (error && ++retry < 3); | |
4d368456 | 1220 | |
c63fe0a4 | 1221 | if (error) |
1da177e4 LT |
1222 | return -1; |
1223 | ||
c63fe0a4 APF |
1224 | if (retry > 1) |
1225 | printk(KERN_DEBUG "Synaptics reconnected after %d tries\n", | |
1226 | retry); | |
1227 | ||
1da177e4 LT |
1228 | if (synaptics_query_hardware(psmouse)) { |
1229 | printk(KERN_ERR "Unable to query Synaptics hardware.\n"); | |
1230 | return -1; | |
1231 | } | |
1232 | ||
1da177e4 LT |
1233 | if (synaptics_set_absolute_mode(psmouse)) { |
1234 | printk(KERN_ERR "Unable to initialize Synaptics hardware.\n"); | |
1235 | return -1; | |
1236 | } | |
1237 | ||
fec6e525 HR |
1238 | if (synaptics_set_advanced_gesture_mode(psmouse)) { |
1239 | printk(KERN_ERR "Advanced gesture mode reconnect failed.\n"); | |
1240 | return -1; | |
1241 | } | |
1242 | ||
baddf589 APF |
1243 | if (old_priv.identity != priv->identity || |
1244 | old_priv.model_id != priv->model_id || | |
1245 | old_priv.capabilities != priv->capabilities || | |
1246 | old_priv.ext_cap != priv->ext_cap) { | |
1247 | printk(KERN_ERR "Synaptics hardware appears to be different: " | |
1248 | "id(%ld-%ld), model(%ld-%ld), caps(%lx-%lx), ext(%lx-%lx).\n", | |
1249 | old_priv.identity, priv->identity, | |
1250 | old_priv.model_id, priv->model_id, | |
1251 | old_priv.capabilities, priv->capabilities, | |
1252 | old_priv.ext_cap, priv->ext_cap); | |
1253 | return -1; | |
1254 | } | |
1255 | ||
1da177e4 LT |
1256 | return 0; |
1257 | } | |
1258 | ||
7705d548 DT |
1259 | static bool impaired_toshiba_kbc; |
1260 | ||
1261 | static const struct dmi_system_id __initconst toshiba_dmi_table[] = { | |
1262 | #if defined(CONFIG_DMI) && defined(CONFIG_X86) | |
1da177e4 | 1263 | { |
9961e259 | 1264 | /* Toshiba Satellite */ |
1da177e4 LT |
1265 | .matches = { |
1266 | DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"), | |
53a2670c | 1267 | DMI_MATCH(DMI_PRODUCT_NAME, "Satellite"), |
1da177e4 LT |
1268 | }, |
1269 | }, | |
9ba5eaaf | 1270 | { |
9961e259 | 1271 | /* Toshiba Dynabook */ |
9ba5eaaf SH |
1272 | .matches = { |
1273 | DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"), | |
53a2670c RT |
1274 | DMI_MATCH(DMI_PRODUCT_NAME, "dynabook"), |
1275 | }, | |
1276 | }, | |
1277 | { | |
9961e259 | 1278 | /* Toshiba Portege M300 */ |
53a2670c RT |
1279 | .matches = { |
1280 | DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"), | |
1281 | DMI_MATCH(DMI_PRODUCT_NAME, "PORTEGE M300"), | |
9ba5eaaf | 1282 | }, |
5f5eeff4 DT |
1283 | |
1284 | }, | |
1285 | { | |
9961e259 | 1286 | /* Toshiba Portege M300 */ |
5f5eeff4 DT |
1287 | .matches = { |
1288 | DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"), | |
1289 | DMI_MATCH(DMI_PRODUCT_NAME, "Portable PC"), | |
1290 | DMI_MATCH(DMI_PRODUCT_VERSION, "Version 1.0"), | |
1291 | }, | |
1292 | ||
9ba5eaaf | 1293 | }, |
1da177e4 | 1294 | #endif |
70874867 | 1295 | { } |
7705d548 DT |
1296 | }; |
1297 | ||
ef8313bb AS |
1298 | static bool broken_olpc_ec; |
1299 | ||
1300 | static const struct dmi_system_id __initconst olpc_dmi_table[] = { | |
1301 | #if defined(CONFIG_DMI) && defined(CONFIG_OLPC) | |
1302 | { | |
1303 | /* OLPC XO-1 or XO-1.5 */ | |
1304 | .matches = { | |
1305 | DMI_MATCH(DMI_SYS_VENDOR, "OLPC"), | |
1306 | DMI_MATCH(DMI_PRODUCT_NAME, "XO"), | |
1307 | }, | |
1308 | }, | |
ef8313bb | 1309 | #endif |
70874867 | 1310 | { } |
ef8313bb AS |
1311 | }; |
1312 | ||
7705d548 DT |
1313 | void __init synaptics_module_init(void) |
1314 | { | |
1315 | impaired_toshiba_kbc = dmi_check_system(toshiba_dmi_table); | |
ef8313bb | 1316 | broken_olpc_ec = dmi_check_system(olpc_dmi_table); |
7705d548 | 1317 | } |
1da177e4 LT |
1318 | |
1319 | int synaptics_init(struct psmouse *psmouse) | |
1320 | { | |
1321 | struct synaptics_data *priv; | |
1322 | ||
ef8313bb AS |
1323 | /* |
1324 | * The OLPC XO has issues with Synaptics' absolute mode; similarly to | |
1325 | * the HGPK, it quickly degrades and the hardware becomes jumpy and | |
1326 | * overly sensitive. Not only that, but the constant packet spew | |
1327 | * (even at a lowered 40pps rate) overloads the EC such that key | |
1328 | * presses on the keyboard are missed. Given all of that, don't | |
1329 | * even attempt to use Synaptics mode. Relative mode seems to work | |
1330 | * just fine. | |
1331 | */ | |
1332 | if (broken_olpc_ec) { | |
1333 | printk(KERN_INFO "synaptics: OLPC XO detected, not enabling Synaptics protocol.\n"); | |
1334 | return -ENODEV; | |
1335 | } | |
1336 | ||
b39787a9 | 1337 | psmouse->private = priv = kzalloc(sizeof(struct synaptics_data), GFP_KERNEL); |
1da177e4 | 1338 | if (!priv) |
6792cbbb | 1339 | return -ENOMEM; |
1da177e4 | 1340 | |
4d368456 AW |
1341 | psmouse_reset(psmouse); |
1342 | ||
1da177e4 LT |
1343 | if (synaptics_query_hardware(psmouse)) { |
1344 | printk(KERN_ERR "Unable to query Synaptics hardware.\n"); | |
1345 | goto init_fail; | |
1346 | } | |
1347 | ||
1348 | if (synaptics_set_absolute_mode(psmouse)) { | |
1349 | printk(KERN_ERR "Unable to initialize Synaptics hardware.\n"); | |
1350 | goto init_fail; | |
1351 | } | |
1352 | ||
fec6e525 HR |
1353 | if (synaptics_set_advanced_gesture_mode(psmouse)) { |
1354 | printk(KERN_ERR "Advanced gesture mode init failed.\n"); | |
1355 | goto init_fail; | |
1356 | } | |
1357 | ||
1da177e4 LT |
1358 | priv->pkt_type = SYN_MODEL_NEWABS(priv->model_id) ? SYN_NEWABS : SYN_OLDABS; |
1359 | ||
5f57d67d | 1360 | printk(KERN_INFO "Synaptics Touchpad, model: %ld, fw: %ld.%ld, id: %#lx, caps: %#lx/%#lx/%#lx\n", |
409b7506 DT |
1361 | SYN_ID_MODEL(priv->identity), |
1362 | SYN_ID_MAJOR(priv->identity), SYN_ID_MINOR(priv->identity), | |
5f57d67d | 1363 | priv->model_id, priv->capabilities, priv->ext_cap, priv->ext_cap_0c); |
409b7506 | 1364 | |
2e5b636b | 1365 | set_input_params(psmouse->dev, priv); |
1da177e4 | 1366 | |
887cc127 DT |
1367 | /* |
1368 | * Encode touchpad model so that it can be used to set | |
1369 | * input device->id.version and be visible to userspace. | |
1370 | * Because version is __u16 we have to drop something. | |
1371 | * Hardware info bits seem to be good candidates as they | |
1372 | * are documented to be for Synaptics corp. internal use. | |
1373 | */ | |
1374 | psmouse->model = ((priv->model_id & 0x00ff0000) >> 8) | | |
1375 | (priv->model_id & 0x000000ff); | |
1376 | ||
1da177e4 LT |
1377 | psmouse->protocol_handler = synaptics_process_byte; |
1378 | psmouse->set_rate = synaptics_set_rate; | |
1379 | psmouse->disconnect = synaptics_disconnect; | |
1380 | psmouse->reconnect = synaptics_reconnect; | |
a1cec061 | 1381 | psmouse->cleanup = synaptics_reset; |
1da177e4 | 1382 | psmouse->pktsize = 6; |
f0d5c6f4 DT |
1383 | /* Synaptics can usually stay in sync without extra help */ |
1384 | psmouse->resync_time = 0; | |
1da177e4 LT |
1385 | |
1386 | if (SYN_CAP_PASS_THROUGH(priv->capabilities)) | |
1387 | synaptics_pt_create(psmouse); | |
1388 | ||
1da177e4 LT |
1389 | /* |
1390 | * Toshiba's KBC seems to have trouble handling data from | |
7ee99161 AS |
1391 | * Synaptics at full rate. Switch to a lower rate (roughly |
1392 | * the same rate as a standard PS/2 mouse). | |
1da177e4 | 1393 | */ |
7705d548 | 1394 | if (psmouse->rate >= 80 && impaired_toshiba_kbc) { |
9ba5eaaf SH |
1395 | printk(KERN_INFO "synaptics: Toshiba %s detected, limiting rate to 40pps.\n", |
1396 | dmi_get_system_info(DMI_PRODUCT_NAME)); | |
1da177e4 LT |
1397 | psmouse->rate = 40; |
1398 | } | |
1da177e4 LT |
1399 | |
1400 | return 0; | |
1401 | ||
1402 | init_fail: | |
1403 | kfree(priv); | |
1404 | return -1; | |
1405 | } | |
1406 | ||
e4e6efd2 DD |
1407 | bool synaptics_supported(void) |
1408 | { | |
1409 | return true; | |
1410 | } | |
1411 | ||
55e3d922 AS |
1412 | #else /* CONFIG_MOUSE_PS2_SYNAPTICS */ |
1413 | ||
7705d548 DT |
1414 | void __init synaptics_module_init(void) |
1415 | { | |
1416 | } | |
1417 | ||
55e3d922 AS |
1418 | int synaptics_init(struct psmouse *psmouse) |
1419 | { | |
1420 | return -ENOSYS; | |
1421 | } | |
1422 | ||
e4e6efd2 DD |
1423 | bool synaptics_supported(void) |
1424 | { | |
1425 | return false; | |
1426 | } | |
1427 | ||
55e3d922 | 1428 | #endif /* CONFIG_MOUSE_PS2_SYNAPTICS */ |