blob: 6d6198a3d52f2a21406ce5089fa318a518ae7780 [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -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>
27#include <linux/delay.h>
28#include <linux/dmi.h>
29#include <linux/input/mt.h>
30#include <linux/serio.h>
31#include <linux/libps2.h>
32#include <linux/slab.h>
33#include "psmouse.h"
34#include "synaptics.h"
35
36/*
37 * The x/y limits are taken from the Synaptics TouchPad interfacing Guide,
38 * section 2.3.2, which says that they should be valid regardless of the
39 * actual size of the sensor.
40 * Note that newer firmware allows querying device for maximum useable
41 * coordinates.
42 */
43#define XMIN 0
44#define XMAX 6143
45#define YMIN 0
46#define YMAX 6143
47#define XMIN_NOMINAL 1472
48#define XMAX_NOMINAL 5472
49#define YMIN_NOMINAL 1408
50#define YMAX_NOMINAL 4448
51
52/* Size in bits of absolute position values reported by the hardware */
53#define ABS_POS_BITS 13
54
55/*
56 * These values should represent the absolute maximum value that will
57 * be reported for a positive position value. Some Synaptics firmware
58 * uses this value to indicate a finger near the edge of the touchpad
59 * whose precise position cannot be determined.
60 *
61 * At least one touchpad is known to report positions in excess of this
62 * value which are actually negative values truncated to the 13-bit
63 * reporting range. These values have never been observed to be lower
64 * than 8184 (i.e. -8), so we treat all values greater than 8176 as
65 * negative and any other value as positive.
66 */
67#define X_MAX_POSITIVE 8176
68#define Y_MAX_POSITIVE 8176
69
70/*
71 * Synaptics touchpads report the y coordinate from bottom to top, which is
72 * opposite from what userspace expects.
73 * This function is used to invert y before reporting.
74 */
75static int synaptics_invert_y(int y)
76{
77 return YMAX_NOMINAL + YMIN_NOMINAL - y;
78}
79
80
81/*****************************************************************************
82 * Stuff we need even when we do not want native Synaptics support
83 ****************************************************************************/
84
85/*
86 * Set the synaptics touchpad mode byte by special commands
87 */
88static int synaptics_mode_cmd(struct psmouse *psmouse, unsigned char mode)
89{
90 unsigned char param[1];
91
92 if (psmouse_sliced_command(psmouse, mode))
93 return -1;
94 param[0] = SYN_PS_SET_MODE2;
95 if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_SETRATE))
96 return -1;
97 return 0;
98}
99
100int synaptics_detect(struct psmouse *psmouse, bool set_properties)
101{
102 struct ps2dev *ps2dev = &psmouse->ps2dev;
103 unsigned char param[4];
104
105 param[0] = 0;
106
107 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
108 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
109 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
110 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
111 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO);
112
113 if (param[1] != 0x47)
114 return -ENODEV;
115
116 if (set_properties) {
117 psmouse->vendor = "Synaptics";
118 psmouse->name = "TouchPad";
119 }
120
121 return 0;
122}
123
124void synaptics_reset(struct psmouse *psmouse)
125{
126 /* reset touchpad back to relative mode, gestures enabled */
127 synaptics_mode_cmd(psmouse, 0);
128}
129
130#ifdef CONFIG_MOUSE_PS2_SYNAPTICS
131
132/*****************************************************************************
133 * Synaptics communications functions
134 ****************************************************************************/
135
136/*
137 * Send a command to the synpatics touchpad by special commands
138 */
139static int synaptics_send_cmd(struct psmouse *psmouse, unsigned char c, unsigned char *param)
140{
141 if (psmouse_sliced_command(psmouse, c))
142 return -1;
143 if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_GETINFO))
144 return -1;
145 return 0;
146}
147
148/*
149 * Read the model-id bytes from the touchpad
150 * see also SYN_MODEL_* macros
151 */
152static int synaptics_model_id(struct psmouse *psmouse)
153{
154 struct synaptics_data *priv = psmouse->private;
155 unsigned char mi[3];
156
157 if (synaptics_send_cmd(psmouse, SYN_QUE_MODEL, mi))
158 return -1;
159 priv->model_id = (mi[0]<<16) | (mi[1]<<8) | mi[2];
160 return 0;
161}
162
163/*
164 * Read the capability-bits from the touchpad
165 * see also the SYN_CAP_* macros
166 */
167static int synaptics_capability(struct psmouse *psmouse)
168{
169 struct synaptics_data *priv = psmouse->private;
170 unsigned char cap[3];
171
172 if (synaptics_send_cmd(psmouse, SYN_QUE_CAPABILITIES, cap))
173 return -1;
174 priv->capabilities = (cap[0] << 16) | (cap[1] << 8) | cap[2];
175 priv->ext_cap = priv->ext_cap_0c = 0;
176
177 /*
178 * Older firmwares had submodel ID fixed to 0x47
179 */
180 if (SYN_ID_FULL(priv->identity) < 0x705 &&
181 SYN_CAP_SUBMODEL_ID(priv->capabilities) != 0x47) {
182 return -1;
183 }
184
185 /*
186 * Unless capExtended is set the rest of the flags should be ignored
187 */
188 if (!SYN_CAP_EXTENDED(priv->capabilities))
189 priv->capabilities = 0;
190
191 if (SYN_EXT_CAP_REQUESTS(priv->capabilities) >= 1) {
192 if (synaptics_send_cmd(psmouse, SYN_QUE_EXT_CAPAB, cap)) {
193 psmouse_warn(psmouse,
194 "device claims to have extended capabilities, but I'm not able to read them.\n");
195 } else {
196 priv->ext_cap = (cap[0] << 16) | (cap[1] << 8) | cap[2];
197
198 /*
199 * if nExtBtn is greater than 8 it should be considered
200 * invalid and treated as 0
201 */
202 if (SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap) > 8)
203 priv->ext_cap &= 0xff0fff;
204 }
205 }
206
207 if (SYN_EXT_CAP_REQUESTS(priv->capabilities) >= 4) {
208 if (synaptics_send_cmd(psmouse, SYN_QUE_EXT_CAPAB_0C, cap)) {
209 psmouse_warn(psmouse,
210 "device claims to have extended capability 0x0c, but I'm not able to read it.\n");
211 } else {
212 priv->ext_cap_0c = (cap[0] << 16) | (cap[1] << 8) | cap[2];
213 }
214 }
215
216 return 0;
217}
218
219/*
220 * Identify Touchpad
221 * See also the SYN_ID_* macros
222 */
223static int synaptics_identify(struct psmouse *psmouse)
224{
225 struct synaptics_data *priv = psmouse->private;
226 unsigned char id[3];
227
228 if (synaptics_send_cmd(psmouse, SYN_QUE_IDENTIFY, id))
229 return -1;
230 priv->identity = (id[0]<<16) | (id[1]<<8) | id[2];
231 if (SYN_ID_IS_SYNAPTICS(priv->identity))
232 return 0;
233 return -1;
234}
235
236/*
237 * Read touchpad resolution and maximum reported coordinates
238 * Resolution is left zero if touchpad does not support the query
239 */
240
241static const int *quirk_min_max;
242
243static int synaptics_resolution(struct psmouse *psmouse)
244{
245 struct synaptics_data *priv = psmouse->private;
246 unsigned char resp[3];
247
248 if (quirk_min_max) {
249 priv->x_min = quirk_min_max[0];
250 priv->x_max = quirk_min_max[1];
251 priv->y_min = quirk_min_max[2];
252 priv->y_max = quirk_min_max[3];
253 return 0;
254 }
255
256 if (SYN_ID_MAJOR(priv->identity) < 4)
257 return 0;
258
259 if (synaptics_send_cmd(psmouse, SYN_QUE_RESOLUTION, resp) == 0) {
260 if (resp[0] != 0 && (resp[1] & 0x80) && resp[2] != 0) {
261 priv->x_res = resp[0]; /* x resolution in units/mm */
262 priv->y_res = resp[2]; /* y resolution in units/mm */
263 }
264 }
265
266 if (SYN_EXT_CAP_REQUESTS(priv->capabilities) >= 5 &&
267 SYN_CAP_MAX_DIMENSIONS(priv->ext_cap_0c)) {
268 if (synaptics_send_cmd(psmouse, SYN_QUE_EXT_MAX_COORDS, resp)) {
269 psmouse_warn(psmouse,
270 "device claims to have max coordinates query, but I'm not able to read it.\n");
271 } else {
272 priv->x_max = (resp[0] << 5) | ((resp[1] & 0x0f) << 1);
273 priv->y_max = (resp[2] << 5) | ((resp[1] & 0xf0) >> 3);
274 }
275 }
276
277 if (SYN_CAP_MIN_DIMENSIONS(priv->ext_cap_0c) &&
278 (SYN_EXT_CAP_REQUESTS(priv->capabilities) >= 7 ||
279 /*
280 * Firmware v8.1 does not report proper number of extended
281 * capabilities, but has been proven to report correct min
282 * coordinates.
283 */
284 SYN_ID_FULL(priv->identity) == 0x801)) {
285 if (synaptics_send_cmd(psmouse, SYN_QUE_EXT_MIN_COORDS, resp)) {
286 psmouse_warn(psmouse,
287 "device claims to have min coordinates query, but I'm not able to read it.\n");
288 } else {
289 priv->x_min = (resp[0] << 5) | ((resp[1] & 0x0f) << 1);
290 priv->y_min = (resp[2] << 5) | ((resp[1] & 0xf0) >> 3);
291 }
292 }
293
294 return 0;
295}
296
297static int synaptics_query_hardware(struct psmouse *psmouse)
298{
299 if (synaptics_identify(psmouse))
300 return -1;
301 if (synaptics_model_id(psmouse))
302 return -1;
303 if (synaptics_capability(psmouse))
304 return -1;
305 if (synaptics_resolution(psmouse))
306 return -1;
307
308 return 0;
309}
310
311static int synaptics_set_advanced_gesture_mode(struct psmouse *psmouse)
312{
313 static unsigned char param = 0xc8;
314 struct synaptics_data *priv = psmouse->private;
315
316 if (!(SYN_CAP_ADV_GESTURE(priv->ext_cap_0c) ||
317 SYN_CAP_IMAGE_SENSOR(priv->ext_cap_0c)))
318 return 0;
319
320 if (psmouse_sliced_command(psmouse, SYN_QUE_MODEL))
321 return -1;
322
323 if (ps2_command(&psmouse->ps2dev, &param, PSMOUSE_CMD_SETRATE))
324 return -1;
325
326 /* Advanced gesture mode also sends multi finger data */
327 priv->capabilities |= BIT(1);
328
329 return 0;
330}
331
332static int synaptics_set_mode(struct psmouse *psmouse)
333{
334 struct synaptics_data *priv = psmouse->private;
335
336 priv->mode = 0;
337 if (priv->absolute_mode)
338 priv->mode |= SYN_BIT_ABSOLUTE_MODE;
339 if (priv->disable_gesture)
340 priv->mode |= SYN_BIT_DISABLE_GESTURE;
341 if (psmouse->rate >= 80)
342 priv->mode |= SYN_BIT_HIGH_RATE;
343 if (SYN_CAP_EXTENDED(priv->capabilities))
344 priv->mode |= SYN_BIT_W_MODE;
345
346 if (synaptics_mode_cmd(psmouse, priv->mode))
347 return -1;
348
349 if (priv->absolute_mode &&
350 synaptics_set_advanced_gesture_mode(psmouse)) {
351 psmouse_err(psmouse, "Advanced gesture mode init failed.\n");
352 return -1;
353 }
354
355 return 0;
356}
357
358static void synaptics_set_rate(struct psmouse *psmouse, unsigned int rate)
359{
360 struct synaptics_data *priv = psmouse->private;
361
362 if (rate >= 80) {
363 priv->mode |= SYN_BIT_HIGH_RATE;
364 psmouse->rate = 80;
365 } else {
366 priv->mode &= ~SYN_BIT_HIGH_RATE;
367 psmouse->rate = 40;
368 }
369
370 synaptics_mode_cmd(psmouse, priv->mode);
371}
372
373/*****************************************************************************
374 * Synaptics pass-through PS/2 port support
375 ****************************************************************************/
376static int synaptics_pt_write(struct serio *serio, unsigned char c)
377{
378 struct psmouse *parent = serio_get_drvdata(serio->parent);
379 char rate_param = SYN_PS_CLIENT_CMD; /* indicates that we want pass-through port */
380
381 if (psmouse_sliced_command(parent, c))
382 return -1;
383 if (ps2_command(&parent->ps2dev, &rate_param, PSMOUSE_CMD_SETRATE))
384 return -1;
385 return 0;
386}
387
388static int synaptics_pt_start(struct serio *serio)
389{
390 struct psmouse *parent = serio_get_drvdata(serio->parent);
391 struct synaptics_data *priv = parent->private;
392
393 serio_pause_rx(parent->ps2dev.serio);
394 priv->pt_port = serio;
395 serio_continue_rx(parent->ps2dev.serio);
396
397 return 0;
398}
399
400static void synaptics_pt_stop(struct serio *serio)
401{
402 struct psmouse *parent = serio_get_drvdata(serio->parent);
403 struct synaptics_data *priv = parent->private;
404
405 serio_pause_rx(parent->ps2dev.serio);
406 priv->pt_port = NULL;
407 serio_continue_rx(parent->ps2dev.serio);
408}
409
410static int synaptics_is_pt_packet(unsigned char *buf)
411{
412 return (buf[0] & 0xFC) == 0x84 && (buf[3] & 0xCC) == 0xC4;
413}
414
415static void synaptics_pass_pt_packet(struct serio *ptport, unsigned char *packet)
416{
417 struct psmouse *child = serio_get_drvdata(ptport);
418
419 if (child && child->state == PSMOUSE_ACTIVATED) {
420 serio_interrupt(ptport, packet[1], 0);
421 serio_interrupt(ptport, packet[4], 0);
422 serio_interrupt(ptport, packet[5], 0);
423 if (child->pktsize == 4)
424 serio_interrupt(ptport, packet[2], 0);
425 } else
426 serio_interrupt(ptport, packet[1], 0);
427}
428
429static void synaptics_pt_activate(struct psmouse *psmouse)
430{
431 struct synaptics_data *priv = psmouse->private;
432 struct psmouse *child = serio_get_drvdata(priv->pt_port);
433
434 /* adjust the touchpad to child's choice of protocol */
435 if (child) {
436 if (child->pktsize == 4)
437 priv->mode |= SYN_BIT_FOUR_BYTE_CLIENT;
438 else
439 priv->mode &= ~SYN_BIT_FOUR_BYTE_CLIENT;
440
441 if (synaptics_mode_cmd(psmouse, priv->mode))
442 psmouse_warn(psmouse,
443 "failed to switch guest protocol\n");
444 }
445}
446
447static void synaptics_pt_create(struct psmouse *psmouse)
448{
449 struct serio *serio;
450
451 serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
452 if (!serio) {
453 psmouse_err(psmouse,
454 "not enough memory for pass-through port\n");
455 return;
456 }
457
458 serio->id.type = SERIO_PS_PSTHRU;
459 strlcpy(serio->name, "Synaptics pass-through", sizeof(serio->name));
460 strlcpy(serio->phys, "synaptics-pt/serio0", sizeof(serio->name));
461 serio->write = synaptics_pt_write;
462 serio->start = synaptics_pt_start;
463 serio->stop = synaptics_pt_stop;
464 serio->parent = psmouse->ps2dev.serio;
465
466 psmouse->pt_activate = synaptics_pt_activate;
467
468 psmouse_info(psmouse, "serio: %s port at %s\n",
469 serio->name, psmouse->phys);
470 serio_register_port(serio);
471}
472
473/*****************************************************************************
474 * Functions to interpret the absolute mode packets
475 ****************************************************************************/
476
477static void synaptics_mt_state_set(struct synaptics_mt_state *state, int count,
478 int sgm, int agm)
479{
480 state->count = count;
481 state->sgm = sgm;
482 state->agm = agm;
483}
484
485static void synaptics_parse_agm(const unsigned char buf[],
486 struct synaptics_data *priv,
487 struct synaptics_hw_state *hw)
488{
489 struct synaptics_hw_state *agm = &priv->agm;
490 int agm_packet_type;
491
492 agm_packet_type = (buf[5] & 0x30) >> 4;
493 switch (agm_packet_type) {
494 case 1:
495 /* Gesture packet: (x, y, z) half resolution */
496 agm->w = hw->w;
497 agm->x = (((buf[4] & 0x0f) << 8) | buf[1]) << 1;
498 agm->y = (((buf[4] & 0xf0) << 4) | buf[2]) << 1;
499 agm->z = ((buf[3] & 0x30) | (buf[5] & 0x0f)) << 1;
500 break;
501
502 case 2:
503 /* AGM-CONTACT packet: (count, sgm, agm) */
504 synaptics_mt_state_set(&agm->mt_state, buf[1], buf[2], buf[4]);
505 break;
506
507 default:
508 break;
509 }
510
511 /* Record that at least one AGM has been received since last SGM */
512 priv->agm_pending = true;
513}
514
515static void synaptics_parse_ext_buttons(const unsigned char buf[],
516 struct synaptics_data *priv,
517 struct synaptics_hw_state *hw)
518{
519 unsigned int ext_bits =
520 (SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap) + 1) >> 1;
521 unsigned int ext_mask = (1U << ext_bits) - 1;
522
523 hw->ext_buttons = buf[4] & ext_mask;
524 hw->ext_buttons |= (buf[5] & ext_mask) << ext_bits;
525}
526
527static bool is_forcepad;
528
529static int synaptics_parse_hw_state(const unsigned char buf[],
530 struct synaptics_data *priv,
531 struct synaptics_hw_state *hw)
532{
533 memset(hw, 0, sizeof(struct synaptics_hw_state));
534
535 if (SYN_MODEL_NEWABS(priv->model_id)) {
536 hw->w = (((buf[0] & 0x30) >> 2) |
537 ((buf[0] & 0x04) >> 1) |
538 ((buf[3] & 0x04) >> 2));
539
540 if ((SYN_CAP_ADV_GESTURE(priv->ext_cap_0c) ||
541 SYN_CAP_IMAGE_SENSOR(priv->ext_cap_0c)) &&
542 hw->w == 2) {
543 synaptics_parse_agm(buf, priv, hw);
544 return 1;
545 }
546
547 hw->x = (((buf[3] & 0x10) << 8) |
548 ((buf[1] & 0x0f) << 8) |
549 buf[4]);
550 hw->y = (((buf[3] & 0x20) << 7) |
551 ((buf[1] & 0xf0) << 4) |
552 buf[5]);
553 hw->z = buf[2];
554
555 hw->left = (buf[0] & 0x01) ? 1 : 0;
556 hw->right = (buf[0] & 0x02) ? 1 : 0;
557
558 if (is_forcepad) {
559 /*
560 * ForcePads, like Clickpads, use middle button
561 * bits to report primary button clicks.
562 * Unfortunately they report primary button not
563 * only when user presses on the pad above certain
564 * threshold, but also when there are more than one
565 * finger on the touchpad, which interferes with
566 * out multi-finger gestures.
567 */
568 if (hw->z == 0) {
569 /* No contacts */
570 priv->press = priv->report_press = false;
571 } else if (hw->w >= 4 && ((buf[0] ^ buf[3]) & 0x01)) {
572 /*
573 * Single-finger touch with pressure above
574 * the threshold. If pressure stays long
575 * enough, we'll start reporting primary
576 * button. We rely on the device continuing
577 * sending data even if finger does not
578 * move.
579 */
580 if (!priv->press) {
581 priv->press_start = jiffies;
582 priv->press = true;
583 } else if (time_after(jiffies,
584 priv->press_start +
585 msecs_to_jiffies(50))) {
586 priv->report_press = true;
587 }
588 } else {
589 priv->press = false;
590 }
591
592 hw->left = priv->report_press;
593
594 } else if (SYN_CAP_CLICKPAD(priv->ext_cap_0c)) {
595 /*
596 * Clickpad's button is transmitted as middle button,
597 * however, since it is primary button, we will report
598 * it as BTN_LEFT.
599 */
600 hw->left = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0;
601
602 } else if (SYN_CAP_MIDDLE_BUTTON(priv->capabilities)) {
603 hw->middle = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0;
604 if (hw->w == 2)
605 hw->scroll = (signed char)(buf[1]);
606 }
607
608 if (SYN_CAP_FOUR_BUTTON(priv->capabilities)) {
609 hw->up = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0;
610 hw->down = ((buf[0] ^ buf[3]) & 0x02) ? 1 : 0;
611 }
612
613 if (SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap) > 0 &&
614 ((buf[0] ^ buf[3]) & 0x02)) {
615 synaptics_parse_ext_buttons(buf, priv, hw);
616 }
617 } else {
618 hw->x = (((buf[1] & 0x1f) << 8) | buf[2]);
619 hw->y = (((buf[4] & 0x1f) << 8) | buf[5]);
620
621 hw->z = (((buf[0] & 0x30) << 2) | (buf[3] & 0x3F));
622 hw->w = (((buf[1] & 0x80) >> 4) | ((buf[0] & 0x04) >> 1));
623
624 hw->left = (buf[0] & 0x01) ? 1 : 0;
625 hw->right = (buf[0] & 0x02) ? 1 : 0;
626 }
627
628 /*
629 * Convert wrap-around values to negative. (X|Y)_MAX_POSITIVE
630 * is used by some firmware to indicate a finger at the edge of
631 * the touchpad whose precise position cannot be determined, so
632 * convert these values to the maximum axis value.
633 */
634 if (hw->x > X_MAX_POSITIVE)
635 hw->x -= 1 << ABS_POS_BITS;
636 else if (hw->x == X_MAX_POSITIVE)
637 hw->x = XMAX;
638
639 if (hw->y > Y_MAX_POSITIVE)
640 hw->y -= 1 << ABS_POS_BITS;
641 else if (hw->y == Y_MAX_POSITIVE)
642 hw->y = YMAX;
643
644 return 0;
645}
646
647static void synaptics_report_semi_mt_slot(struct input_dev *dev, int slot,
648 bool active, int x, int y)
649{
650 input_mt_slot(dev, slot);
651 input_mt_report_slot_state(dev, MT_TOOL_FINGER, active);
652 if (active) {
653 input_report_abs(dev, ABS_MT_POSITION_X, x);
654 input_report_abs(dev, ABS_MT_POSITION_Y, synaptics_invert_y(y));
655 }
656}
657
658static void synaptics_report_semi_mt_data(struct input_dev *dev,
659 const struct synaptics_hw_state *a,
660 const struct synaptics_hw_state *b,
661 int num_fingers)
662{
663 if (num_fingers >= 2) {
664 synaptics_report_semi_mt_slot(dev, 0, true, min(a->x, b->x),
665 min(a->y, b->y));
666 synaptics_report_semi_mt_slot(dev, 1, true, max(a->x, b->x),
667 max(a->y, b->y));
668 } else if (num_fingers == 1) {
669 synaptics_report_semi_mt_slot(dev, 0, true, a->x, a->y);
670 synaptics_report_semi_mt_slot(dev, 1, false, 0, 0);
671 } else {
672 synaptics_report_semi_mt_slot(dev, 0, false, 0, 0);
673 synaptics_report_semi_mt_slot(dev, 1, false, 0, 0);
674 }
675}
676
677static void synaptics_report_ext_buttons(struct psmouse *psmouse,
678 const struct synaptics_hw_state *hw)
679{
680 struct input_dev *dev = psmouse->dev;
681 struct synaptics_data *priv = psmouse->private;
682 int ext_bits = (SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap) + 1) >> 1;
683 int i;
684
685 if (!SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap))
686 return;
687
688 /* Bug in FW 8.1, buttons are reported only when ExtBit is 1 */
689 if (SYN_ID_FULL(priv->identity) == 0x801 &&
690 !((psmouse->packet[0] ^ psmouse->packet[3]) & 0x02))
691 return;
692
693 for (i = 0; i < ext_bits; i++) {
694 input_report_key(dev, BTN_0 + 2 * i,
695 hw->ext_buttons & (1 << i));
696 input_report_key(dev, BTN_1 + 2 * i,
697 hw->ext_buttons & (1 << (i + ext_bits)));
698 }
699}
700
701static void synaptics_report_buttons(struct psmouse *psmouse,
702 const struct synaptics_hw_state *hw)
703{
704 struct input_dev *dev = psmouse->dev;
705 struct synaptics_data *priv = psmouse->private;
706
707 input_report_key(dev, BTN_LEFT, hw->left);
708 input_report_key(dev, BTN_RIGHT, hw->right);
709
710 if (SYN_CAP_MIDDLE_BUTTON(priv->capabilities))
711 input_report_key(dev, BTN_MIDDLE, hw->middle);
712
713 if (SYN_CAP_FOUR_BUTTON(priv->capabilities)) {
714 input_report_key(dev, BTN_FORWARD, hw->up);
715 input_report_key(dev, BTN_BACK, hw->down);
716 }
717
718 synaptics_report_ext_buttons(psmouse, hw);
719}
720
721static void synaptics_report_slot(struct input_dev *dev, int slot,
722 const struct synaptics_hw_state *hw)
723{
724 input_mt_slot(dev, slot);
725 input_mt_report_slot_state(dev, MT_TOOL_FINGER, (hw != NULL));
726 if (!hw)
727 return;
728
729 input_report_abs(dev, ABS_MT_POSITION_X, hw->x);
730 input_report_abs(dev, ABS_MT_POSITION_Y, synaptics_invert_y(hw->y));
731 input_report_abs(dev, ABS_MT_PRESSURE, hw->z);
732}
733
734static void synaptics_report_mt_data(struct psmouse *psmouse,
735 struct synaptics_mt_state *mt_state,
736 const struct synaptics_hw_state *sgm)
737{
738 struct input_dev *dev = psmouse->dev;
739 struct synaptics_data *priv = psmouse->private;
740 struct synaptics_hw_state *agm = &priv->agm;
741 struct synaptics_mt_state *old = &priv->mt_state;
742
743 switch (mt_state->count) {
744 case 0:
745 synaptics_report_slot(dev, 0, NULL);
746 synaptics_report_slot(dev, 1, NULL);
747 break;
748 case 1:
749 if (mt_state->sgm == -1) {
750 synaptics_report_slot(dev, 0, NULL);
751 synaptics_report_slot(dev, 1, NULL);
752 } else if (mt_state->sgm == 0) {
753 synaptics_report_slot(dev, 0, sgm);
754 synaptics_report_slot(dev, 1, NULL);
755 } else {
756 synaptics_report_slot(dev, 0, NULL);
757 synaptics_report_slot(dev, 1, sgm);
758 }
759 break;
760 default:
761 /*
762 * If the finger slot contained in SGM is valid, and either
763 * hasn't changed, or is new, then report SGM in MTB slot 0.
764 * Otherwise, empty MTB slot 0.
765 */
766 if (mt_state->sgm != -1 &&
767 (mt_state->sgm == old->sgm || old->sgm == -1))
768 synaptics_report_slot(dev, 0, sgm);
769 else
770 synaptics_report_slot(dev, 0, NULL);
771
772 /*
773 * If the finger slot contained in AGM is valid, and either
774 * hasn't changed, or is new, then report AGM in MTB slot 1.
775 * Otherwise, empty MTB slot 1.
776 */
777 if (mt_state->agm != -1 &&
778 (mt_state->agm == old->agm || old->agm == -1))
779 synaptics_report_slot(dev, 1, agm);
780 else
781 synaptics_report_slot(dev, 1, NULL);
782 break;
783 }
784
785 /* Don't use active slot count to generate BTN_TOOL events. */
786 input_mt_report_pointer_emulation(dev, false);
787
788 /* Send the number of fingers reported by touchpad itself. */
789 input_mt_report_finger_count(dev, mt_state->count);
790
791 synaptics_report_buttons(psmouse, sgm);
792
793 input_sync(dev);
794}
795
796/* Handle case where mt_state->count = 0 */
797static void synaptics_image_sensor_0f(struct synaptics_data *priv,
798 struct synaptics_mt_state *mt_state)
799{
800 synaptics_mt_state_set(mt_state, 0, -1, -1);
801 priv->mt_state_lost = false;
802}
803
804/* Handle case where mt_state->count = 1 */
805static void synaptics_image_sensor_1f(struct synaptics_data *priv,
806 struct synaptics_mt_state *mt_state)
807{
808 struct synaptics_hw_state *agm = &priv->agm;
809 struct synaptics_mt_state *old = &priv->mt_state;
810
811 /*
812 * If the last AGM was (0,0,0), and there is only one finger left,
813 * then we absolutely know that SGM contains slot 0, and all other
814 * fingers have been removed.
815 */
816 if (priv->agm_pending && agm->z == 0) {
817 synaptics_mt_state_set(mt_state, 1, 0, -1);
818 priv->mt_state_lost = false;
819 return;
820 }
821
822 switch (old->count) {
823 case 0:
824 synaptics_mt_state_set(mt_state, 1, 0, -1);
825 break;
826 case 1:
827 /*
828 * If mt_state_lost, then the previous transition was 3->1,
829 * and SGM now contains either slot 0 or 1, but we don't know
830 * which. So, we just assume that the SGM now contains slot 1.
831 *
832 * If pending AGM and either:
833 * (a) the previous SGM slot contains slot 0, or
834 * (b) there was no SGM slot
835 * then, the SGM now contains slot 1
836 *
837 * Case (a) happens with very rapid "drum roll" gestures, where
838 * slot 0 finger is lifted and a new slot 1 finger touches
839 * within one reporting interval.
840 *
841 * Case (b) happens if initially two or more fingers tap
842 * briefly, and all but one lift before the end of the first
843 * reporting interval.
844 *
845 * (In both these cases, slot 0 will becomes empty, so SGM
846 * contains slot 1 with the new finger)
847 *
848 * Else, if there was no previous SGM, it now contains slot 0.
849 *
850 * Otherwise, SGM still contains the same slot.
851 */
852 if (priv->mt_state_lost ||
853 (priv->agm_pending && old->sgm <= 0))
854 synaptics_mt_state_set(mt_state, 1, 1, -1);
855 else if (old->sgm == -1)
856 synaptics_mt_state_set(mt_state, 1, 0, -1);
857 break;
858 case 2:
859 /*
860 * If mt_state_lost, we don't know which finger SGM contains.
861 *
862 * So, report 1 finger, but with both slots empty.
863 * We will use slot 1 on subsequent 1->1
864 */
865 if (priv->mt_state_lost) {
866 synaptics_mt_state_set(mt_state, 1, -1, -1);
867 break;
868 }
869 /*
870 * Since the last AGM was NOT (0,0,0), it was the finger in
871 * slot 0 that has been removed.
872 * So, SGM now contains previous AGM's slot, and AGM is now
873 * empty.
874 */
875 synaptics_mt_state_set(mt_state, 1, old->agm, -1);
876 break;
877 case 3:
878 /*
879 * Since last AGM was not (0,0,0), we don't know which finger
880 * is left.
881 *
882 * So, report 1 finger, but with both slots empty.
883 * We will use slot 1 on subsequent 1->1
884 */
885 synaptics_mt_state_set(mt_state, 1, -1, -1);
886 priv->mt_state_lost = true;
887 break;
888 case 4:
889 case 5:
890 /* mt_state was updated by AGM-CONTACT packet */
891 break;
892 }
893}
894
895/* Handle case where mt_state->count = 2 */
896static void synaptics_image_sensor_2f(struct synaptics_data *priv,
897 struct synaptics_mt_state *mt_state)
898{
899 struct synaptics_mt_state *old = &priv->mt_state;
900
901 switch (old->count) {
902 case 0:
903 synaptics_mt_state_set(mt_state, 2, 0, 1);
904 break;
905 case 1:
906 /*
907 * If previous SGM contained slot 1 or higher, SGM now contains
908 * slot 0 (the newly touching finger) and AGM contains SGM's
909 * previous slot.
910 *
911 * Otherwise, SGM still contains slot 0 and AGM now contains
912 * slot 1.
913 */
914 if (old->sgm >= 1)
915 synaptics_mt_state_set(mt_state, 2, 0, old->sgm);
916 else
917 synaptics_mt_state_set(mt_state, 2, 0, 1);
918 break;
919 case 2:
920 /*
921 * If mt_state_lost, SGM now contains either finger 1 or 2, but
922 * we don't know which.
923 * So, we just assume that the SGM contains slot 0 and AGM 1.
924 */
925 if (priv->mt_state_lost)
926 synaptics_mt_state_set(mt_state, 2, 0, 1);
927 /*
928 * Otherwise, use the same mt_state, since it either hasn't
929 * changed, or was updated by a recently received AGM-CONTACT
930 * packet.
931 */
932 break;
933 case 3:
934 /*
935 * 3->2 transitions have two unsolvable problems:
936 * 1) no indication is given which finger was removed
937 * 2) no way to tell if agm packet was for finger 3
938 * before 3->2, or finger 2 after 3->2.
939 *
940 * So, report 2 fingers, but empty all slots.
941 * We will guess slots [0,1] on subsequent 2->2.
942 */
943 synaptics_mt_state_set(mt_state, 2, -1, -1);
944 priv->mt_state_lost = true;
945 break;
946 case 4:
947 case 5:
948 /* mt_state was updated by AGM-CONTACT packet */
949 break;
950 }
951}
952
953/* Handle case where mt_state->count = 3 */
954static void synaptics_image_sensor_3f(struct synaptics_data *priv,
955 struct synaptics_mt_state *mt_state)
956{
957 struct synaptics_mt_state *old = &priv->mt_state;
958
959 switch (old->count) {
960 case 0:
961 synaptics_mt_state_set(mt_state, 3, 0, 2);
962 break;
963 case 1:
964 /*
965 * If previous SGM contained slot 2 or higher, SGM now contains
966 * slot 0 (one of the newly touching fingers) and AGM contains
967 * SGM's previous slot.
968 *
969 * Otherwise, SGM now contains slot 0 and AGM contains slot 2.
970 */
971 if (old->sgm >= 2)
972 synaptics_mt_state_set(mt_state, 3, 0, old->sgm);
973 else
974 synaptics_mt_state_set(mt_state, 3, 0, 2);
975 break;
976 case 2:
977 /*
978 * If the AGM previously contained slot 3 or higher, then the
979 * newly touching finger is in the lowest available slot.
980 *
981 * If SGM was previously 1 or higher, then the new SGM is
982 * now slot 0 (with a new finger), otherwise, the new finger
983 * is now in a hidden slot between 0 and AGM's slot.
984 *
985 * In all such cases, the SGM now contains slot 0, and the AGM
986 * continues to contain the same slot as before.
987 */
988 if (old->agm >= 3) {
989 synaptics_mt_state_set(mt_state, 3, 0, old->agm);
990 break;
991 }
992
993 /*
994 * After some 3->1 and all 3->2 transitions, we lose track
995 * of which slot is reported by SGM and AGM.
996 *
997 * For 2->3 in this state, report 3 fingers, but empty all
998 * slots, and we will guess (0,2) on a subsequent 0->3.
999 *
1000 * To userspace, the resulting transition will look like:
1001 * 2:[0,1] -> 3:[-1,-1] -> 3:[0,2]
1002 */
1003 if (priv->mt_state_lost) {
1004 synaptics_mt_state_set(mt_state, 3, -1, -1);
1005 break;
1006 }
1007
1008 /*
1009 * If the (SGM,AGM) really previously contained slots (0, 1),
1010 * then we cannot know what slot was just reported by the AGM,
1011 * because the 2->3 transition can occur either before or after
1012 * the AGM packet. Thus, this most recent AGM could contain
1013 * either the same old slot 1 or the new slot 2.
1014 * Subsequent AGMs will be reporting slot 2.
1015 *
1016 * To userspace, the resulting transition will look like:
1017 * 2:[0,1] -> 3:[0,-1] -> 3:[0,2]
1018 */
1019 synaptics_mt_state_set(mt_state, 3, 0, -1);
1020 break;
1021 case 3:
1022 /*
1023 * If, for whatever reason, the previous agm was invalid,
1024 * Assume SGM now contains slot 0, AGM now contains slot 2.
1025 */
1026 if (old->agm <= 2)
1027 synaptics_mt_state_set(mt_state, 3, 0, 2);
1028 /*
1029 * mt_state either hasn't changed, or was updated by a recently
1030 * received AGM-CONTACT packet.
1031 */
1032 break;
1033
1034 case 4:
1035 case 5:
1036 /* mt_state was updated by AGM-CONTACT packet */
1037 break;
1038 }
1039}
1040
1041/* Handle case where mt_state->count = 4, or = 5 */
1042static void synaptics_image_sensor_45f(struct synaptics_data *priv,
1043 struct synaptics_mt_state *mt_state)
1044{
1045 /* mt_state was updated correctly by AGM-CONTACT packet */
1046 priv->mt_state_lost = false;
1047}
1048
1049static void synaptics_image_sensor_process(struct psmouse *psmouse,
1050 struct synaptics_hw_state *sgm)
1051{
1052 struct synaptics_data *priv = psmouse->private;
1053 struct synaptics_hw_state *agm = &priv->agm;
1054 struct synaptics_mt_state mt_state;
1055
1056 /* Initialize using current mt_state (as updated by last agm) */
1057 mt_state = agm->mt_state;
1058
1059 /*
1060 * Update mt_state using the new finger count and current mt_state.
1061 */
1062 if (sgm->z == 0)
1063 synaptics_image_sensor_0f(priv, &mt_state);
1064 else if (sgm->w >= 4)
1065 synaptics_image_sensor_1f(priv, &mt_state);
1066 else if (sgm->w == 0)
1067 synaptics_image_sensor_2f(priv, &mt_state);
1068 else if (sgm->w == 1 && mt_state.count <= 3)
1069 synaptics_image_sensor_3f(priv, &mt_state);
1070 else
1071 synaptics_image_sensor_45f(priv, &mt_state);
1072
1073 /* Send resulting input events to user space */
1074 synaptics_report_mt_data(psmouse, &mt_state, sgm);
1075
1076 /* Store updated mt_state */
1077 priv->mt_state = agm->mt_state = mt_state;
1078 priv->agm_pending = false;
1079}
1080
1081/*
1082 * called for each full received packet from the touchpad
1083 */
1084static void synaptics_process_packet(struct psmouse *psmouse)
1085{
1086 struct input_dev *dev = psmouse->dev;
1087 struct synaptics_data *priv = psmouse->private;
1088 struct synaptics_hw_state hw;
1089 int num_fingers;
1090 int finger_width;
1091
1092 if (synaptics_parse_hw_state(psmouse->packet, priv, &hw))
1093 return;
1094
1095 if (SYN_CAP_IMAGE_SENSOR(priv->ext_cap_0c)) {
1096 synaptics_image_sensor_process(psmouse, &hw);
1097 return;
1098 }
1099
1100 if (hw.scroll) {
1101 priv->scroll += hw.scroll;
1102
1103 while (priv->scroll >= 4) {
1104 input_report_key(dev, BTN_BACK, !hw.down);
1105 input_sync(dev);
1106 input_report_key(dev, BTN_BACK, hw.down);
1107 input_sync(dev);
1108 priv->scroll -= 4;
1109 }
1110 while (priv->scroll <= -4) {
1111 input_report_key(dev, BTN_FORWARD, !hw.up);
1112 input_sync(dev);
1113 input_report_key(dev, BTN_FORWARD, hw.up);
1114 input_sync(dev);
1115 priv->scroll += 4;
1116 }
1117 return;
1118 }
1119
1120 if (hw.z > 0 && hw.x > 1) {
1121 num_fingers = 1;
1122 finger_width = 5;
1123 if (SYN_CAP_EXTENDED(priv->capabilities)) {
1124 switch (hw.w) {
1125 case 0 ... 1:
1126 if (SYN_CAP_MULTIFINGER(priv->capabilities))
1127 num_fingers = hw.w + 2;
1128 break;
1129 case 2:
1130 if (SYN_MODEL_PEN(priv->model_id))
1131 ; /* Nothing, treat a pen as a single finger */
1132 break;
1133 case 4 ... 15:
1134 if (SYN_CAP_PALMDETECT(priv->capabilities))
1135 finger_width = hw.w;
1136 break;
1137 }
1138 }
1139 } else {
1140 num_fingers = 0;
1141 finger_width = 0;
1142 }
1143
1144 if (SYN_CAP_ADV_GESTURE(priv->ext_cap_0c))
1145 synaptics_report_semi_mt_data(dev, &hw, &priv->agm,
1146 num_fingers);
1147
1148 /* Post events
1149 * BTN_TOUCH has to be first as mousedev relies on it when doing
1150 * absolute -> relative conversion
1151 */
1152 if (hw.z > 30) input_report_key(dev, BTN_TOUCH, 1);
1153 if (hw.z < 25) input_report_key(dev, BTN_TOUCH, 0);
1154
1155 if (num_fingers > 0) {
1156 input_report_abs(dev, ABS_X, hw.x);
1157 input_report_abs(dev, ABS_Y, synaptics_invert_y(hw.y));
1158 }
1159 input_report_abs(dev, ABS_PRESSURE, hw.z);
1160
1161 if (SYN_CAP_PALMDETECT(priv->capabilities))
1162 input_report_abs(dev, ABS_TOOL_WIDTH, finger_width);
1163
1164 input_report_key(dev, BTN_TOOL_FINGER, num_fingers == 1);
1165 if (SYN_CAP_MULTIFINGER(priv->capabilities)) {
1166 input_report_key(dev, BTN_TOOL_DOUBLETAP, num_fingers == 2);
1167 input_report_key(dev, BTN_TOOL_TRIPLETAP, num_fingers == 3);
1168 }
1169
1170 synaptics_report_buttons(psmouse, &hw);
1171
1172 input_sync(dev);
1173}
1174
1175static int synaptics_validate_byte(struct psmouse *psmouse,
1176 int idx, unsigned char pkt_type)
1177{
1178 static const unsigned char newabs_mask[] = { 0xC8, 0x00, 0x00, 0xC8, 0x00 };
1179 static const unsigned char newabs_rel_mask[] = { 0xC0, 0x00, 0x00, 0xC0, 0x00 };
1180 static const unsigned char newabs_rslt[] = { 0x80, 0x00, 0x00, 0xC0, 0x00 };
1181 static const unsigned char oldabs_mask[] = { 0xC0, 0x60, 0x00, 0xC0, 0x60 };
1182 static const unsigned char oldabs_rslt[] = { 0xC0, 0x00, 0x00, 0x80, 0x00 };
1183 const char *packet = psmouse->packet;
1184
1185 if (idx < 0 || idx > 4)
1186 return 0;
1187
1188 switch (pkt_type) {
1189
1190 case SYN_NEWABS:
1191 case SYN_NEWABS_RELAXED:
1192 return (packet[idx] & newabs_rel_mask[idx]) == newabs_rslt[idx];
1193
1194 case SYN_NEWABS_STRICT:
1195 return (packet[idx] & newabs_mask[idx]) == newabs_rslt[idx];
1196
1197 case SYN_OLDABS:
1198 return (packet[idx] & oldabs_mask[idx]) == oldabs_rslt[idx];
1199
1200 default:
1201 psmouse_err(psmouse, "unknown packet type %d\n", pkt_type);
1202 return 0;
1203 }
1204}
1205
1206static unsigned char synaptics_detect_pkt_type(struct psmouse *psmouse)
1207{
1208 int i;
1209
1210 for (i = 0; i < 5; i++)
1211 if (!synaptics_validate_byte(psmouse, i, SYN_NEWABS_STRICT)) {
1212 psmouse_info(psmouse, "using relaxed packet validation\n");
1213 return SYN_NEWABS_RELAXED;
1214 }
1215
1216 return SYN_NEWABS_STRICT;
1217}
1218
1219static psmouse_ret_t synaptics_process_byte(struct psmouse *psmouse)
1220{
1221 struct synaptics_data *priv = psmouse->private;
1222
1223 if (psmouse->pktcnt >= 6) { /* Full packet received */
1224 if (unlikely(priv->pkt_type == SYN_NEWABS))
1225 priv->pkt_type = synaptics_detect_pkt_type(psmouse);
1226
1227 if (SYN_CAP_PASS_THROUGH(priv->capabilities) &&
1228 synaptics_is_pt_packet(psmouse->packet)) {
1229 if (priv->pt_port)
1230 synaptics_pass_pt_packet(priv->pt_port, psmouse->packet);
1231 } else
1232 synaptics_process_packet(psmouse);
1233
1234 return PSMOUSE_FULL_PACKET;
1235 }
1236
1237 return synaptics_validate_byte(psmouse, psmouse->pktcnt - 1, priv->pkt_type) ?
1238 PSMOUSE_GOOD_DATA : PSMOUSE_BAD_DATA;
1239}
1240
1241/*****************************************************************************
1242 * Driver initialization/cleanup functions
1243 ****************************************************************************/
1244static void set_abs_position_params(struct input_dev *dev,
1245 struct synaptics_data *priv, int x_code,
1246 int y_code)
1247{
1248 int x_min = priv->x_min ?: XMIN_NOMINAL;
1249 int x_max = priv->x_max ?: XMAX_NOMINAL;
1250 int y_min = priv->y_min ?: YMIN_NOMINAL;
1251 int y_max = priv->y_max ?: YMAX_NOMINAL;
1252 int fuzz = SYN_CAP_REDUCED_FILTERING(priv->ext_cap_0c) ?
1253 SYN_REDUCED_FILTER_FUZZ : 0;
1254
1255 input_set_abs_params(dev, x_code, x_min, x_max, fuzz, 0);
1256 input_set_abs_params(dev, y_code, y_min, y_max, fuzz, 0);
1257 input_abs_set_res(dev, x_code, priv->x_res);
1258 input_abs_set_res(dev, y_code, priv->y_res);
1259}
1260
1261static void set_input_params(struct input_dev *dev, struct synaptics_data *priv)
1262{
1263 int i;
1264
1265 /* Things that apply to both modes */
1266 __set_bit(INPUT_PROP_POINTER, dev->propbit);
1267 __set_bit(EV_KEY, dev->evbit);
1268 __set_bit(BTN_LEFT, dev->keybit);
1269 __set_bit(BTN_RIGHT, dev->keybit);
1270
1271 if (SYN_CAP_MIDDLE_BUTTON(priv->capabilities))
1272 __set_bit(BTN_MIDDLE, dev->keybit);
1273
1274 if (!priv->absolute_mode) {
1275 /* Relative mode */
1276 __set_bit(EV_REL, dev->evbit);
1277 __set_bit(REL_X, dev->relbit);
1278 __set_bit(REL_Y, dev->relbit);
1279 return;
1280 }
1281
1282 /* Absolute mode */
1283 __set_bit(EV_ABS, dev->evbit);
1284 set_abs_position_params(dev, priv, ABS_X, ABS_Y);
1285 input_set_abs_params(dev, ABS_PRESSURE, 0, 255, 0, 0);
1286
1287 if (SYN_CAP_IMAGE_SENSOR(priv->ext_cap_0c)) {
1288 input_mt_init_slots(dev, 2);
1289 set_abs_position_params(dev, priv, ABS_MT_POSITION_X,
1290 ABS_MT_POSITION_Y);
1291 /* Image sensors can report per-contact pressure */
1292 input_set_abs_params(dev, ABS_MT_PRESSURE, 0, 255, 0, 0);
1293
1294 /* Image sensors can signal 4 and 5 finger clicks */
1295 __set_bit(BTN_TOOL_QUADTAP, dev->keybit);
1296 __set_bit(BTN_TOOL_QUINTTAP, dev->keybit);
1297 } else if (SYN_CAP_ADV_GESTURE(priv->ext_cap_0c)) {
1298 /* Non-image sensors with AGM use semi-mt */
1299 __set_bit(INPUT_PROP_SEMI_MT, dev->propbit);
1300 input_mt_init_slots(dev, 2);
1301 set_abs_position_params(dev, priv, ABS_MT_POSITION_X,
1302 ABS_MT_POSITION_Y);
1303 }
1304
1305 if (SYN_CAP_PALMDETECT(priv->capabilities))
1306 input_set_abs_params(dev, ABS_TOOL_WIDTH, 0, 15, 0, 0);
1307
1308 __set_bit(BTN_TOUCH, dev->keybit);
1309 __set_bit(BTN_TOOL_FINGER, dev->keybit);
1310
1311 if (SYN_CAP_MULTIFINGER(priv->capabilities)) {
1312 __set_bit(BTN_TOOL_DOUBLETAP, dev->keybit);
1313 __set_bit(BTN_TOOL_TRIPLETAP, dev->keybit);
1314 }
1315
1316 if (SYN_CAP_FOUR_BUTTON(priv->capabilities) ||
1317 SYN_CAP_MIDDLE_BUTTON(priv->capabilities)) {
1318 __set_bit(BTN_FORWARD, dev->keybit);
1319 __set_bit(BTN_BACK, dev->keybit);
1320 }
1321
1322 for (i = 0; i < SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap); i++)
1323 __set_bit(BTN_0 + i, dev->keybit);
1324
1325 __clear_bit(EV_REL, dev->evbit);
1326 __clear_bit(REL_X, dev->relbit);
1327 __clear_bit(REL_Y, dev->relbit);
1328
1329 if (SYN_CAP_CLICKPAD(priv->ext_cap_0c)) {
1330 __set_bit(INPUT_PROP_BUTTONPAD, dev->propbit);
1331 /* Clickpads report only left button */
1332 __clear_bit(BTN_RIGHT, dev->keybit);
1333 __clear_bit(BTN_MIDDLE, dev->keybit);
1334 }
1335}
1336
1337static ssize_t synaptics_show_disable_gesture(struct psmouse *psmouse,
1338 void *data, char *buf)
1339{
1340 struct synaptics_data *priv = psmouse->private;
1341
1342 return sprintf(buf, "%c\n", priv->disable_gesture ? '1' : '0');
1343}
1344
1345static ssize_t synaptics_set_disable_gesture(struct psmouse *psmouse,
1346 void *data, const char *buf,
1347 size_t len)
1348{
1349 struct synaptics_data *priv = psmouse->private;
1350 unsigned int value;
1351 int err;
1352
1353 err = kstrtouint(buf, 10, &value);
1354 if (err)
1355 return err;
1356
1357 if (value > 1)
1358 return -EINVAL;
1359
1360 if (value == priv->disable_gesture)
1361 return len;
1362
1363 priv->disable_gesture = value;
1364 if (value)
1365 priv->mode |= SYN_BIT_DISABLE_GESTURE;
1366 else
1367 priv->mode &= ~SYN_BIT_DISABLE_GESTURE;
1368
1369 if (synaptics_mode_cmd(psmouse, priv->mode))
1370 return -EIO;
1371
1372 return len;
1373}
1374
1375PSMOUSE_DEFINE_ATTR(disable_gesture, S_IWUSR | S_IRUGO, NULL,
1376 synaptics_show_disable_gesture,
1377 synaptics_set_disable_gesture);
1378
1379static void synaptics_disconnect(struct psmouse *psmouse)
1380{
1381 struct synaptics_data *priv = psmouse->private;
1382
1383 if (!priv->absolute_mode && SYN_ID_DISGEST_SUPPORTED(priv->identity))
1384 device_remove_file(&psmouse->ps2dev.serio->dev,
1385 &psmouse_attr_disable_gesture.dattr);
1386
1387 synaptics_reset(psmouse);
1388 kfree(priv);
1389 psmouse->private = NULL;
1390}
1391
1392static int synaptics_reconnect(struct psmouse *psmouse)
1393{
1394 struct synaptics_data *priv = psmouse->private;
1395 struct synaptics_data old_priv = *priv;
1396 int retry = 0;
1397 int error;
1398
1399 do {
1400 psmouse_reset(psmouse);
1401 if (retry) {
1402 /*
1403 * On some boxes, right after resuming, the touchpad
1404 * needs some time to finish initializing (I assume
1405 * it needs time to calibrate) and start responding
1406 * to Synaptics-specific queries, so let's wait a
1407 * bit.
1408 */
1409 ssleep(1);
1410 }
1411 error = synaptics_detect(psmouse, 0);
1412 } while (error && ++retry < 3);
1413
1414 if (error)
1415 return -1;
1416
1417 if (retry > 1)
1418 psmouse_dbg(psmouse, "reconnected after %d tries\n", retry);
1419
1420 if (synaptics_query_hardware(psmouse)) {
1421 psmouse_err(psmouse, "Unable to query device.\n");
1422 return -1;
1423 }
1424
1425 if (synaptics_set_mode(psmouse)) {
1426 psmouse_err(psmouse, "Unable to initialize device.\n");
1427 return -1;
1428 }
1429
1430 if (old_priv.identity != priv->identity ||
1431 old_priv.model_id != priv->model_id ||
1432 old_priv.capabilities != priv->capabilities ||
1433 old_priv.ext_cap != priv->ext_cap) {
1434 psmouse_err(psmouse,
1435 "hardware appears to be different: id(%ld-%ld), model(%ld-%ld), caps(%lx-%lx), ext(%lx-%lx).\n",
1436 old_priv.identity, priv->identity,
1437 old_priv.model_id, priv->model_id,
1438 old_priv.capabilities, priv->capabilities,
1439 old_priv.ext_cap, priv->ext_cap);
1440 return -1;
1441 }
1442
1443 return 0;
1444}
1445
1446static bool impaired_toshiba_kbc;
1447
1448static const struct dmi_system_id __initconst toshiba_dmi_table[] = {
1449#if defined(CONFIG_DMI) && defined(CONFIG_X86)
1450 {
1451 /* Toshiba Satellite */
1452 .matches = {
1453 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1454 DMI_MATCH(DMI_PRODUCT_NAME, "Satellite"),
1455 },
1456 },
1457 {
1458 /* Toshiba Dynabook */
1459 .matches = {
1460 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1461 DMI_MATCH(DMI_PRODUCT_NAME, "dynabook"),
1462 },
1463 },
1464 {
1465 /* Toshiba Portege M300 */
1466 .matches = {
1467 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1468 DMI_MATCH(DMI_PRODUCT_NAME, "PORTEGE M300"),
1469 },
1470
1471 },
1472 {
1473 /* Toshiba Portege M300 */
1474 .matches = {
1475 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1476 DMI_MATCH(DMI_PRODUCT_NAME, "Portable PC"),
1477 DMI_MATCH(DMI_PRODUCT_VERSION, "Version 1.0"),
1478 },
1479
1480 },
1481#endif
1482 { }
1483};
1484
1485static bool broken_olpc_ec;
1486
1487static const struct dmi_system_id __initconst olpc_dmi_table[] = {
1488#if defined(CONFIG_DMI) && defined(CONFIG_OLPC)
1489 {
1490 /* OLPC XO-1 or XO-1.5 */
1491 .matches = {
1492 DMI_MATCH(DMI_SYS_VENDOR, "OLPC"),
1493 DMI_MATCH(DMI_PRODUCT_NAME, "XO"),
1494 },
1495 },
1496#endif
1497 { }
1498};
1499
1500static const struct dmi_system_id min_max_dmi_table[] __initconst = {
1501#if defined(CONFIG_DMI)
1502 {
1503 /* Lenovo ThinkPad Helix */
1504 .matches = {
1505 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1506 DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad Helix"),
1507 },
1508 .driver_data = (int []){1024, 5052, 2258, 4832},
1509 },
1510 {
1511 /* Lenovo ThinkPad X240 */
1512 .matches = {
1513 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1514 DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad X240"),
1515 },
1516 .driver_data = (int []){1232, 5710, 1156, 4696},
1517 },
1518 {
1519 /* Lenovo ThinkPad T440s */
1520 .matches = {
1521 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1522 DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad T440"),
1523 },
1524 .driver_data = (int []){1024, 5112, 2024, 4832},
1525 },
1526 {
1527 /* Lenovo ThinkPad T540p */
1528 .matches = {
1529 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1530 DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad T540"),
1531 },
1532 .driver_data = (int []){1024, 5056, 2058, 4832},
1533 },
1534#endif
1535 { }
1536};
1537
1538static const struct dmi_system_id forcepad_dmi_table[] __initconst = {
1539#if defined(CONFIG_DMI) && defined(CONFIG_X86)
1540 {
1541 .matches = {
1542 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
1543 DMI_MATCH(DMI_PRODUCT_NAME, "HP EliteBook Folio 1040 G1"),
1544 },
1545 },
1546#endif
1547 { }
1548};
1549
1550void __init synaptics_module_init(void)
1551{
1552 const struct dmi_system_id *min_max_dmi;
1553
1554 impaired_toshiba_kbc = dmi_check_system(toshiba_dmi_table);
1555 broken_olpc_ec = dmi_check_system(olpc_dmi_table);
1556
1557 min_max_dmi = dmi_first_match(min_max_dmi_table);
1558 if (min_max_dmi)
1559 quirk_min_max = min_max_dmi->driver_data;
1560
1561 /*
1562 * Unfortunately ForcePad capability is not exported over PS/2,
1563 * so we have to resort to checking DMI.
1564 */
1565 is_forcepad = dmi_check_system(forcepad_dmi_table);
1566}
1567
1568static int __synaptics_init(struct psmouse *psmouse, bool absolute_mode)
1569{
1570 struct synaptics_data *priv;
1571 int err = -1;
1572
1573 /*
1574 * The OLPC XO has issues with Synaptics' absolute mode; the constant
1575 * packet spew overloads the EC such that key presses on the keyboard
1576 * are missed. Given that, don't even attempt to use Absolute mode.
1577 * Relative mode seems to work just fine.
1578 */
1579 if (absolute_mode && broken_olpc_ec) {
1580 psmouse_info(psmouse,
1581 "OLPC XO detected, not enabling Synaptics protocol.\n");
1582 return -ENODEV;
1583 }
1584
1585 psmouse->private = priv = kzalloc(sizeof(struct synaptics_data), GFP_KERNEL);
1586 if (!priv)
1587 return -ENOMEM;
1588
1589 psmouse_reset(psmouse);
1590
1591 if (synaptics_query_hardware(psmouse)) {
1592 psmouse_err(psmouse, "Unable to query device.\n");
1593 goto init_fail;
1594 }
1595
1596 priv->absolute_mode = absolute_mode;
1597 if (SYN_ID_DISGEST_SUPPORTED(priv->identity))
1598 priv->disable_gesture = true;
1599
1600 if (synaptics_set_mode(psmouse)) {
1601 psmouse_err(psmouse, "Unable to initialize device.\n");
1602 goto init_fail;
1603 }
1604
1605 priv->pkt_type = SYN_MODEL_NEWABS(priv->model_id) ? SYN_NEWABS : SYN_OLDABS;
1606
1607 psmouse_info(psmouse,
1608 "Touchpad model: %ld, fw: %ld.%ld, id: %#lx, caps: %#lx/%#lx/%#lx\n",
1609 SYN_ID_MODEL(priv->identity),
1610 SYN_ID_MAJOR(priv->identity), SYN_ID_MINOR(priv->identity),
1611 priv->model_id,
1612 priv->capabilities, priv->ext_cap, priv->ext_cap_0c);
1613
1614 set_input_params(psmouse->dev, priv);
1615
1616 /*
1617 * Encode touchpad model so that it can be used to set
1618 * input device->id.version and be visible to userspace.
1619 * Because version is __u16 we have to drop something.
1620 * Hardware info bits seem to be good candidates as they
1621 * are documented to be for Synaptics corp. internal use.
1622 */
1623 psmouse->model = ((priv->model_id & 0x00ff0000) >> 8) |
1624 (priv->model_id & 0x000000ff);
1625
1626 if (absolute_mode) {
1627 psmouse->protocol_handler = synaptics_process_byte;
1628 psmouse->pktsize = 6;
1629 } else {
1630 /* Relative mode follows standard PS/2 mouse protocol */
1631 psmouse->protocol_handler = psmouse_process_byte;
1632 psmouse->pktsize = 3;
1633 }
1634
1635 psmouse->set_rate = synaptics_set_rate;
1636 psmouse->disconnect = synaptics_disconnect;
1637 psmouse->reconnect = synaptics_reconnect;
1638 psmouse->cleanup = synaptics_reset;
1639 /* Synaptics can usually stay in sync without extra help */
1640 psmouse->resync_time = 0;
1641
1642 if (SYN_CAP_PASS_THROUGH(priv->capabilities))
1643 synaptics_pt_create(psmouse);
1644
1645 /*
1646 * Toshiba's KBC seems to have trouble handling data from
1647 * Synaptics at full rate. Switch to a lower rate (roughly
1648 * the same rate as a standard PS/2 mouse).
1649 */
1650 if (psmouse->rate >= 80 && impaired_toshiba_kbc) {
1651 psmouse_info(psmouse,
1652 "Toshiba %s detected, limiting rate to 40pps.\n",
1653 dmi_get_system_info(DMI_PRODUCT_NAME));
1654 psmouse->rate = 40;
1655 }
1656
1657 if (!priv->absolute_mode && SYN_ID_DISGEST_SUPPORTED(priv->identity)) {
1658 err = device_create_file(&psmouse->ps2dev.serio->dev,
1659 &psmouse_attr_disable_gesture.dattr);
1660 if (err) {
1661 psmouse_err(psmouse,
1662 "Failed to create disable_gesture attribute (%d)",
1663 err);
1664 goto init_fail;
1665 }
1666 }
1667
1668 return 0;
1669
1670 init_fail:
1671 kfree(priv);
1672 return err;
1673}
1674
1675int synaptics_init(struct psmouse *psmouse)
1676{
1677 return __synaptics_init(psmouse, true);
1678}
1679
1680int synaptics_init_relative(struct psmouse *psmouse)
1681{
1682 return __synaptics_init(psmouse, false);
1683}
1684
1685bool synaptics_supported(void)
1686{
1687 return true;
1688}
1689
1690#else /* CONFIG_MOUSE_PS2_SYNAPTICS */
1691
1692void __init synaptics_module_init(void)
1693{
1694}
1695
1696int synaptics_init(struct psmouse *psmouse)
1697{
1698 return -ENOSYS;
1699}
1700
1701bool synaptics_supported(void)
1702{
1703 return false;
1704}
1705
1706#endif /* CONFIG_MOUSE_PS2_SYNAPTICS */