2 * Synaptics TouchPad PS/2 mouse driver
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.
8 * 2003 Peter Osterlund <petero2@telia.com>
9 * Ported to 2.5 input device infrastructure.
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)
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)
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.
23 * Trademarks are the property of their respective owners.
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>
34 #include "synaptics.h"
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
47 #define XMIN_NOMINAL 1472
48 #define XMAX_NOMINAL 5472
49 #define YMIN_NOMINAL 1408
50 #define YMAX_NOMINAL 4448
52 /* Size in bits of absolute position values reported by the hardware */
53 #define ABS_POS_BITS 13
56 * Any position values from the hardware above the following limits are
57 * treated as "wrapped around negative" values that have been truncated to
58 * the 13-bit reporting range of the hardware. These are just reasonable
59 * guesses and can be adjusted if hardware is found that operates outside
60 * of these parameters.
62 #define X_MAX_POSITIVE (((1 << ABS_POS_BITS) + XMAX) / 2)
63 #define Y_MAX_POSITIVE (((1 << ABS_POS_BITS) + YMAX) / 2)
66 * Synaptics touchpads report the y coordinate from bottom to top, which is
67 * opposite from what userspace expects.
68 * This function is used to invert y before reporting.
70 static int synaptics_invert_y(int y)
72 return YMAX_NOMINAL + YMIN_NOMINAL - y;
76 /*****************************************************************************
77 * Stuff we need even when we do not want native Synaptics support
78 ****************************************************************************/
81 * Set the synaptics touchpad mode byte by special commands
83 static int synaptics_mode_cmd(struct psmouse *psmouse, unsigned char mode)
85 unsigned char param[1];
87 if (psmouse_sliced_command(psmouse, mode))
89 param[0] = SYN_PS_SET_MODE2;
90 if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_SETRATE))
95 int synaptics_detect(struct psmouse *psmouse, bool set_properties)
97 struct ps2dev *ps2dev = &psmouse->ps2dev;
98 unsigned char param[4];
102 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
103 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
104 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
105 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
106 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO);
108 if (param[1] != 0x47)
111 if (set_properties) {
112 psmouse->vendor = "Synaptics";
113 psmouse->name = "TouchPad";
119 void synaptics_reset(struct psmouse *psmouse)
121 /* reset touchpad back to relative mode, gestures enabled */
122 synaptics_mode_cmd(psmouse, 0);
125 #ifdef CONFIG_MOUSE_PS2_SYNAPTICS
127 /*****************************************************************************
128 * Synaptics communications functions
129 ****************************************************************************/
132 * Send a command to the synpatics touchpad by special commands
134 static int synaptics_send_cmd(struct psmouse *psmouse, unsigned char c, unsigned char *param)
136 if (psmouse_sliced_command(psmouse, c))
138 if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_GETINFO))
144 * Read the model-id bytes from the touchpad
145 * see also SYN_MODEL_* macros
147 static int synaptics_model_id(struct psmouse *psmouse)
149 struct synaptics_data *priv = psmouse->private;
152 if (synaptics_send_cmd(psmouse, SYN_QUE_MODEL, mi))
154 priv->model_id = (mi[0]<<16) | (mi[1]<<8) | mi[2];
159 * Read the capability-bits from the touchpad
160 * see also the SYN_CAP_* macros
162 static int synaptics_capability(struct psmouse *psmouse)
164 struct synaptics_data *priv = psmouse->private;
165 unsigned char cap[3];
167 if (synaptics_send_cmd(psmouse, SYN_QUE_CAPABILITIES, cap))
169 priv->capabilities = (cap[0] << 16) | (cap[1] << 8) | cap[2];
170 priv->ext_cap = priv->ext_cap_0c = 0;
173 * Older firmwares had submodel ID fixed to 0x47
175 if (SYN_ID_FULL(priv->identity) < 0x705 &&
176 SYN_CAP_SUBMODEL_ID(priv->capabilities) != 0x47) {
181 * Unless capExtended is set the rest of the flags should be ignored
183 if (!SYN_CAP_EXTENDED(priv->capabilities))
184 priv->capabilities = 0;
186 if (SYN_EXT_CAP_REQUESTS(priv->capabilities) >= 1) {
187 if (synaptics_send_cmd(psmouse, SYN_QUE_EXT_CAPAB, cap)) {
188 psmouse_warn(psmouse,
189 "device claims to have extended capabilities, but I'm not able to read them.\n");
191 priv->ext_cap = (cap[0] << 16) | (cap[1] << 8) | cap[2];
194 * if nExtBtn is greater than 8 it should be considered
195 * invalid and treated as 0
197 if (SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap) > 8)
198 priv->ext_cap &= 0xff0fff;
202 if (SYN_EXT_CAP_REQUESTS(priv->capabilities) >= 4) {
203 if (synaptics_send_cmd(psmouse, SYN_QUE_EXT_CAPAB_0C, cap)) {
204 psmouse_warn(psmouse,
205 "device claims to have extended capability 0x0c, but I'm not able to read it.\n");
207 priv->ext_cap_0c = (cap[0] << 16) | (cap[1] << 8) | cap[2];
216 * See also the SYN_ID_* macros
218 static int synaptics_identify(struct psmouse *psmouse)
220 struct synaptics_data *priv = psmouse->private;
223 if (synaptics_send_cmd(psmouse, SYN_QUE_IDENTIFY, id))
225 priv->identity = (id[0]<<16) | (id[1]<<8) | id[2];
226 if (SYN_ID_IS_SYNAPTICS(priv->identity))
232 * Read touchpad resolution and maximum reported coordinates
233 * Resolution is left zero if touchpad does not support the query
235 static int synaptics_resolution(struct psmouse *psmouse)
237 struct synaptics_data *priv = psmouse->private;
238 unsigned char resp[3];
240 if (SYN_ID_MAJOR(priv->identity) < 4)
243 if (synaptics_send_cmd(psmouse, SYN_QUE_RESOLUTION, resp) == 0) {
244 if (resp[0] != 0 && (resp[1] & 0x80) && resp[2] != 0) {
245 priv->x_res = resp[0]; /* x resolution in units/mm */
246 priv->y_res = resp[2]; /* y resolution in units/mm */
250 if (SYN_EXT_CAP_REQUESTS(priv->capabilities) >= 5 &&
251 SYN_CAP_MAX_DIMENSIONS(priv->ext_cap_0c)) {
252 if (synaptics_send_cmd(psmouse, SYN_QUE_EXT_MAX_COORDS, resp)) {
253 psmouse_warn(psmouse,
254 "device claims to have max coordinates query, but I'm not able to read it.\n");
256 priv->x_max = (resp[0] << 5) | ((resp[1] & 0x0f) << 1);
257 priv->y_max = (resp[2] << 5) | ((resp[1] & 0xf0) >> 3);
261 if (SYN_EXT_CAP_REQUESTS(priv->capabilities) >= 7 &&
262 SYN_CAP_MIN_DIMENSIONS(priv->ext_cap_0c)) {
263 if (synaptics_send_cmd(psmouse, SYN_QUE_EXT_MIN_COORDS, resp)) {
264 psmouse_warn(psmouse,
265 "device claims to have min coordinates query, but I'm not able to read it.\n");
267 priv->x_min = (resp[0] << 5) | ((resp[1] & 0x0f) << 1);
268 priv->y_min = (resp[2] << 5) | ((resp[1] & 0xf0) >> 3);
275 static int synaptics_query_hardware(struct psmouse *psmouse)
277 if (synaptics_identify(psmouse))
279 if (synaptics_model_id(psmouse))
281 if (synaptics_capability(psmouse))
283 if (synaptics_resolution(psmouse))
289 static int synaptics_set_absolute_mode(struct psmouse *psmouse)
291 struct synaptics_data *priv = psmouse->private;
293 priv->mode = SYN_BIT_ABSOLUTE_MODE;
294 if (SYN_ID_MAJOR(priv->identity) >= 4)
295 priv->mode |= SYN_BIT_DISABLE_GESTURE;
296 if (SYN_CAP_EXTENDED(priv->capabilities))
297 priv->mode |= SYN_BIT_W_MODE;
299 if (synaptics_mode_cmd(psmouse, priv->mode))
305 static void synaptics_set_rate(struct psmouse *psmouse, unsigned int rate)
307 struct synaptics_data *priv = psmouse->private;
310 priv->mode |= SYN_BIT_HIGH_RATE;
313 priv->mode &= ~SYN_BIT_HIGH_RATE;
317 synaptics_mode_cmd(psmouse, priv->mode);
320 static int synaptics_set_advanced_gesture_mode(struct psmouse *psmouse)
322 static unsigned char param = 0xc8;
323 struct synaptics_data *priv = psmouse->private;
325 if (!(SYN_CAP_ADV_GESTURE(priv->ext_cap_0c) ||
326 SYN_CAP_IMAGE_SENSOR(priv->ext_cap_0c)))
329 if (psmouse_sliced_command(psmouse, SYN_QUE_MODEL))
331 if (ps2_command(&psmouse->ps2dev, ¶m, PSMOUSE_CMD_SETRATE))
334 /* Advanced gesture mode also sends multi finger data */
335 priv->capabilities |= BIT(1);
340 /*****************************************************************************
341 * Synaptics pass-through PS/2 port support
342 ****************************************************************************/
343 static int synaptics_pt_write(struct serio *serio, unsigned char c)
345 struct psmouse *parent = serio_get_drvdata(serio->parent);
346 char rate_param = SYN_PS_CLIENT_CMD; /* indicates that we want pass-through port */
348 if (psmouse_sliced_command(parent, c))
350 if (ps2_command(&parent->ps2dev, &rate_param, PSMOUSE_CMD_SETRATE))
355 static int synaptics_pt_start(struct serio *serio)
357 struct psmouse *parent = serio_get_drvdata(serio->parent);
358 struct synaptics_data *priv = parent->private;
360 serio_pause_rx(parent->ps2dev.serio);
361 priv->pt_port = serio;
362 serio_continue_rx(parent->ps2dev.serio);
367 static void synaptics_pt_stop(struct serio *serio)
369 struct psmouse *parent = serio_get_drvdata(serio->parent);
370 struct synaptics_data *priv = parent->private;
372 serio_pause_rx(parent->ps2dev.serio);
373 priv->pt_port = NULL;
374 serio_continue_rx(parent->ps2dev.serio);
377 static int synaptics_is_pt_packet(unsigned char *buf)
379 return (buf[0] & 0xFC) == 0x84 && (buf[3] & 0xCC) == 0xC4;
382 static void synaptics_pass_pt_packet(struct serio *ptport, unsigned char *packet)
384 struct psmouse *child = serio_get_drvdata(ptport);
386 if (child && child->state == PSMOUSE_ACTIVATED) {
387 serio_interrupt(ptport, packet[1], 0);
388 serio_interrupt(ptport, packet[4], 0);
389 serio_interrupt(ptport, packet[5], 0);
390 if (child->pktsize == 4)
391 serio_interrupt(ptport, packet[2], 0);
393 serio_interrupt(ptport, packet[1], 0);
396 static void synaptics_pt_activate(struct psmouse *psmouse)
398 struct synaptics_data *priv = psmouse->private;
399 struct psmouse *child = serio_get_drvdata(priv->pt_port);
401 /* adjust the touchpad to child's choice of protocol */
403 if (child->pktsize == 4)
404 priv->mode |= SYN_BIT_FOUR_BYTE_CLIENT;
406 priv->mode &= ~SYN_BIT_FOUR_BYTE_CLIENT;
408 if (synaptics_mode_cmd(psmouse, priv->mode))
409 psmouse_warn(psmouse,
410 "failed to switch guest protocol\n");
414 static void synaptics_pt_create(struct psmouse *psmouse)
418 serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
421 "not enough memory for pass-through port\n");
425 serio->id.type = SERIO_PS_PSTHRU;
426 strlcpy(serio->name, "Synaptics pass-through", sizeof(serio->name));
427 strlcpy(serio->phys, "synaptics-pt/serio0", sizeof(serio->name));
428 serio->write = synaptics_pt_write;
429 serio->start = synaptics_pt_start;
430 serio->stop = synaptics_pt_stop;
431 serio->parent = psmouse->ps2dev.serio;
433 psmouse->pt_activate = synaptics_pt_activate;
435 psmouse_info(psmouse, "serio: %s port at %s\n",
436 serio->name, psmouse->phys);
437 serio_register_port(serio);
440 /*****************************************************************************
441 * Functions to interpret the absolute mode packets
442 ****************************************************************************/
444 static void synaptics_mt_state_set(struct synaptics_mt_state *state, int count,
447 state->count = count;
452 static void synaptics_parse_agm(const unsigned char buf[],
453 struct synaptics_data *priv,
454 struct synaptics_hw_state *hw)
456 struct synaptics_hw_state *agm = &priv->agm;
459 agm_packet_type = (buf[5] & 0x30) >> 4;
460 switch (agm_packet_type) {
462 /* Gesture packet: (x, y, z) half resolution */
464 agm->x = (((buf[4] & 0x0f) << 8) | buf[1]) << 1;
465 agm->y = (((buf[4] & 0xf0) << 4) | buf[2]) << 1;
466 agm->z = ((buf[3] & 0x30) | (buf[5] & 0x0f)) << 1;
470 /* AGM-CONTACT packet: (count, sgm, agm) */
471 synaptics_mt_state_set(&agm->mt_state, buf[1], buf[2], buf[4]);
478 /* Record that at least one AGM has been received since last SGM */
479 priv->agm_pending = true;
482 static int synaptics_parse_hw_state(const unsigned char buf[],
483 struct synaptics_data *priv,
484 struct synaptics_hw_state *hw)
486 memset(hw, 0, sizeof(struct synaptics_hw_state));
488 if (SYN_MODEL_NEWABS(priv->model_id)) {
489 hw->w = (((buf[0] & 0x30) >> 2) |
490 ((buf[0] & 0x04) >> 1) |
491 ((buf[3] & 0x04) >> 2));
493 hw->left = (buf[0] & 0x01) ? 1 : 0;
494 hw->right = (buf[0] & 0x02) ? 1 : 0;
496 if (SYN_CAP_CLICKPAD(priv->ext_cap_0c)) {
498 * Clickpad's button is transmitted as middle button,
499 * however, since it is primary button, we will report
502 hw->left = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0;
504 } else if (SYN_CAP_MIDDLE_BUTTON(priv->capabilities)) {
505 hw->middle = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0;
507 hw->scroll = (signed char)(buf[1]);
510 if (SYN_CAP_FOUR_BUTTON(priv->capabilities)) {
511 hw->up = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0;
512 hw->down = ((buf[0] ^ buf[3]) & 0x02) ? 1 : 0;
515 if ((SYN_CAP_ADV_GESTURE(priv->ext_cap_0c) ||
516 SYN_CAP_IMAGE_SENSOR(priv->ext_cap_0c)) &&
518 synaptics_parse_agm(buf, priv, hw);
522 hw->x = (((buf[3] & 0x10) << 8) |
523 ((buf[1] & 0x0f) << 8) |
525 hw->y = (((buf[3] & 0x20) << 7) |
526 ((buf[1] & 0xf0) << 4) |
530 if (SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap) &&
531 ((buf[0] ^ buf[3]) & 0x02)) {
532 switch (SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap) & ~0x01) {
535 * if nExtBtn is greater than 8 it should be
536 * considered invalid and treated as 0
540 hw->ext_buttons |= ((buf[5] & 0x08)) ? 0x80 : 0;
541 hw->ext_buttons |= ((buf[4] & 0x08)) ? 0x40 : 0;
543 hw->ext_buttons |= ((buf[5] & 0x04)) ? 0x20 : 0;
544 hw->ext_buttons |= ((buf[4] & 0x04)) ? 0x10 : 0;
546 hw->ext_buttons |= ((buf[5] & 0x02)) ? 0x08 : 0;
547 hw->ext_buttons |= ((buf[4] & 0x02)) ? 0x04 : 0;
549 hw->ext_buttons |= ((buf[5] & 0x01)) ? 0x02 : 0;
550 hw->ext_buttons |= ((buf[4] & 0x01)) ? 0x01 : 0;
554 hw->x = (((buf[1] & 0x1f) << 8) | buf[2]);
555 hw->y = (((buf[4] & 0x1f) << 8) | buf[5]);
557 hw->z = (((buf[0] & 0x30) << 2) | (buf[3] & 0x3F));
558 hw->w = (((buf[1] & 0x80) >> 4) | ((buf[0] & 0x04) >> 1));
560 hw->left = (buf[0] & 0x01) ? 1 : 0;
561 hw->right = (buf[0] & 0x02) ? 1 : 0;
564 /* Convert wrap-around values to negative */
565 if (hw->x > X_MAX_POSITIVE)
566 hw->x -= 1 << ABS_POS_BITS;
567 if (hw->y > Y_MAX_POSITIVE)
568 hw->y -= 1 << ABS_POS_BITS;
573 static void synaptics_report_semi_mt_slot(struct input_dev *dev, int slot,
574 bool active, int x, int y)
576 input_mt_slot(dev, slot);
577 input_mt_report_slot_state(dev, MT_TOOL_FINGER, active);
579 input_report_abs(dev, ABS_MT_POSITION_X, x);
580 input_report_abs(dev, ABS_MT_POSITION_Y, synaptics_invert_y(y));
584 static void synaptics_report_semi_mt_data(struct input_dev *dev,
585 const struct synaptics_hw_state *a,
586 const struct synaptics_hw_state *b,
589 if (num_fingers >= 2) {
590 synaptics_report_semi_mt_slot(dev, 0, true, min(a->x, b->x),
592 synaptics_report_semi_mt_slot(dev, 1, true, max(a->x, b->x),
594 } else if (num_fingers == 1) {
595 synaptics_report_semi_mt_slot(dev, 0, true, a->x, a->y);
596 synaptics_report_semi_mt_slot(dev, 1, false, 0, 0);
598 synaptics_report_semi_mt_slot(dev, 0, false, 0, 0);
599 synaptics_report_semi_mt_slot(dev, 1, false, 0, 0);
603 static void synaptics_report_buttons(struct psmouse *psmouse,
604 const struct synaptics_hw_state *hw)
606 struct input_dev *dev = psmouse->dev;
607 struct synaptics_data *priv = psmouse->private;
610 input_report_key(dev, BTN_LEFT, hw->left);
611 input_report_key(dev, BTN_RIGHT, hw->right);
613 if (SYN_CAP_MIDDLE_BUTTON(priv->capabilities))
614 input_report_key(dev, BTN_MIDDLE, hw->middle);
616 if (SYN_CAP_FOUR_BUTTON(priv->capabilities)) {
617 input_report_key(dev, BTN_FORWARD, hw->up);
618 input_report_key(dev, BTN_BACK, hw->down);
621 for (i = 0; i < SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap); i++)
622 input_report_key(dev, BTN_0 + i, hw->ext_buttons & (1 << i));
625 static void synaptics_report_slot(struct input_dev *dev, int slot,
626 const struct synaptics_hw_state *hw)
628 input_mt_slot(dev, slot);
629 input_mt_report_slot_state(dev, MT_TOOL_FINGER, (hw != NULL));
633 input_report_abs(dev, ABS_MT_POSITION_X, hw->x);
634 input_report_abs(dev, ABS_MT_POSITION_Y, synaptics_invert_y(hw->y));
635 input_report_abs(dev, ABS_MT_PRESSURE, hw->z);
638 static void synaptics_report_mt_data(struct psmouse *psmouse,
639 struct synaptics_mt_state *mt_state,
640 const struct synaptics_hw_state *sgm)
642 struct input_dev *dev = psmouse->dev;
643 struct synaptics_data *priv = psmouse->private;
644 struct synaptics_hw_state *agm = &priv->agm;
645 struct synaptics_mt_state *old = &priv->mt_state;
647 switch (mt_state->count) {
649 synaptics_report_slot(dev, 0, NULL);
650 synaptics_report_slot(dev, 1, NULL);
653 if (mt_state->sgm == -1) {
654 synaptics_report_slot(dev, 0, NULL);
655 synaptics_report_slot(dev, 1, NULL);
656 } else if (mt_state->sgm == 0) {
657 synaptics_report_slot(dev, 0, sgm);
658 synaptics_report_slot(dev, 1, NULL);
660 synaptics_report_slot(dev, 0, NULL);
661 synaptics_report_slot(dev, 1, sgm);
666 * If the finger slot contained in SGM is valid, and either
667 * hasn't changed, or is new, then report SGM in MTB slot 0.
668 * Otherwise, empty MTB slot 0.
670 if (mt_state->sgm != -1 &&
671 (mt_state->sgm == old->sgm || old->sgm == -1))
672 synaptics_report_slot(dev, 0, sgm);
674 synaptics_report_slot(dev, 0, NULL);
677 * If the finger slot contained in AGM is valid, and either
678 * hasn't changed, or is new, then report AGM in MTB slot 1.
679 * Otherwise, empty MTB slot 1.
681 if (mt_state->agm != -1 &&
682 (mt_state->agm == old->agm || old->agm == -1))
683 synaptics_report_slot(dev, 1, agm);
685 synaptics_report_slot(dev, 1, NULL);
689 /* Don't use active slot count to generate BTN_TOOL events. */
690 input_mt_report_pointer_emulation(dev, false);
692 /* Send the number of fingers reported by touchpad itself. */
693 input_mt_report_finger_count(dev, mt_state->count);
695 synaptics_report_buttons(psmouse, sgm);
700 /* Handle case where mt_state->count = 0 */
701 static void synaptics_image_sensor_0f(struct synaptics_data *priv,
702 struct synaptics_mt_state *mt_state)
704 synaptics_mt_state_set(mt_state, 0, -1, -1);
705 priv->mt_state_lost = false;
708 /* Handle case where mt_state->count = 1 */
709 static void synaptics_image_sensor_1f(struct synaptics_data *priv,
710 struct synaptics_mt_state *mt_state)
712 struct synaptics_hw_state *agm = &priv->agm;
713 struct synaptics_mt_state *old = &priv->mt_state;
716 * If the last AGM was (0,0,0), and there is only one finger left,
717 * then we absolutely know that SGM contains slot 0, and all other
718 * fingers have been removed.
720 if (priv->agm_pending && agm->z == 0) {
721 synaptics_mt_state_set(mt_state, 1, 0, -1);
722 priv->mt_state_lost = false;
726 switch (old->count) {
728 synaptics_mt_state_set(mt_state, 1, 0, -1);
732 * If mt_state_lost, then the previous transition was 3->1,
733 * and SGM now contains either slot 0 or 1, but we don't know
734 * which. So, we just assume that the SGM now contains slot 1.
736 * If pending AGM and either:
737 * (a) the previous SGM slot contains slot 0, or
738 * (b) there was no SGM slot
739 * then, the SGM now contains slot 1
741 * Case (a) happens with very rapid "drum roll" gestures, where
742 * slot 0 finger is lifted and a new slot 1 finger touches
743 * within one reporting interval.
745 * Case (b) happens if initially two or more fingers tap
746 * briefly, and all but one lift before the end of the first
747 * reporting interval.
749 * (In both these cases, slot 0 will becomes empty, so SGM
750 * contains slot 1 with the new finger)
752 * Else, if there was no previous SGM, it now contains slot 0.
754 * Otherwise, SGM still contains the same slot.
756 if (priv->mt_state_lost ||
757 (priv->agm_pending && old->sgm <= 0))
758 synaptics_mt_state_set(mt_state, 1, 1, -1);
759 else if (old->sgm == -1)
760 synaptics_mt_state_set(mt_state, 1, 0, -1);
764 * If mt_state_lost, we don't know which finger SGM contains.
766 * So, report 1 finger, but with both slots empty.
767 * We will use slot 1 on subsequent 1->1
769 if (priv->mt_state_lost) {
770 synaptics_mt_state_set(mt_state, 1, -1, -1);
774 * Since the last AGM was NOT (0,0,0), it was the finger in
775 * slot 0 that has been removed.
776 * So, SGM now contains previous AGM's slot, and AGM is now
779 synaptics_mt_state_set(mt_state, 1, old->agm, -1);
783 * Since last AGM was not (0,0,0), we don't know which finger
786 * So, report 1 finger, but with both slots empty.
787 * We will use slot 1 on subsequent 1->1
789 synaptics_mt_state_set(mt_state, 1, -1, -1);
790 priv->mt_state_lost = true;
794 /* mt_state was updated by AGM-CONTACT packet */
799 /* Handle case where mt_state->count = 2 */
800 static void synaptics_image_sensor_2f(struct synaptics_data *priv,
801 struct synaptics_mt_state *mt_state)
803 struct synaptics_mt_state *old = &priv->mt_state;
805 switch (old->count) {
807 synaptics_mt_state_set(mt_state, 2, 0, 1);
811 * If previous SGM contained slot 1 or higher, SGM now contains
812 * slot 0 (the newly touching finger) and AGM contains SGM's
815 * Otherwise, SGM still contains slot 0 and AGM now contains
819 synaptics_mt_state_set(mt_state, 2, 0, old->sgm);
821 synaptics_mt_state_set(mt_state, 2, 0, 1);
825 * If mt_state_lost, SGM now contains either finger 1 or 2, but
826 * we don't know which.
827 * So, we just assume that the SGM contains slot 0 and AGM 1.
829 if (priv->mt_state_lost)
830 synaptics_mt_state_set(mt_state, 2, 0, 1);
832 * Otherwise, use the same mt_state, since it either hasn't
833 * changed, or was updated by a recently received AGM-CONTACT
839 * 3->2 transitions have two unsolvable problems:
840 * 1) no indication is given which finger was removed
841 * 2) no way to tell if agm packet was for finger 3
842 * before 3->2, or finger 2 after 3->2.
844 * So, report 2 fingers, but empty all slots.
845 * We will guess slots [0,1] on subsequent 2->2.
847 synaptics_mt_state_set(mt_state, 2, -1, -1);
848 priv->mt_state_lost = true;
852 /* mt_state was updated by AGM-CONTACT packet */
857 /* Handle case where mt_state->count = 3 */
858 static void synaptics_image_sensor_3f(struct synaptics_data *priv,
859 struct synaptics_mt_state *mt_state)
861 struct synaptics_mt_state *old = &priv->mt_state;
863 switch (old->count) {
865 synaptics_mt_state_set(mt_state, 3, 0, 2);
869 * If previous SGM contained slot 2 or higher, SGM now contains
870 * slot 0 (one of the newly touching fingers) and AGM contains
871 * SGM's previous slot.
873 * Otherwise, SGM now contains slot 0 and AGM contains slot 2.
876 synaptics_mt_state_set(mt_state, 3, 0, old->sgm);
878 synaptics_mt_state_set(mt_state, 3, 0, 2);
882 * If the AGM previously contained slot 3 or higher, then the
883 * newly touching finger is in the lowest available slot.
885 * If SGM was previously 1 or higher, then the new SGM is
886 * now slot 0 (with a new finger), otherwise, the new finger
887 * is now in a hidden slot between 0 and AGM's slot.
889 * In all such cases, the SGM now contains slot 0, and the AGM
890 * continues to contain the same slot as before.
893 synaptics_mt_state_set(mt_state, 3, 0, old->agm);
898 * After some 3->1 and all 3->2 transitions, we lose track
899 * of which slot is reported by SGM and AGM.
901 * For 2->3 in this state, report 3 fingers, but empty all
902 * slots, and we will guess (0,2) on a subsequent 0->3.
904 * To userspace, the resulting transition will look like:
905 * 2:[0,1] -> 3:[-1,-1] -> 3:[0,2]
907 if (priv->mt_state_lost) {
908 synaptics_mt_state_set(mt_state, 3, -1, -1);
913 * If the (SGM,AGM) really previously contained slots (0, 1),
914 * then we cannot know what slot was just reported by the AGM,
915 * because the 2->3 transition can occur either before or after
916 * the AGM packet. Thus, this most recent AGM could contain
917 * either the same old slot 1 or the new slot 2.
918 * Subsequent AGMs will be reporting slot 2.
920 * To userspace, the resulting transition will look like:
921 * 2:[0,1] -> 3:[0,-1] -> 3:[0,2]
923 synaptics_mt_state_set(mt_state, 3, 0, -1);
927 * If, for whatever reason, the previous agm was invalid,
928 * Assume SGM now contains slot 0, AGM now contains slot 2.
931 synaptics_mt_state_set(mt_state, 3, 0, 2);
933 * mt_state either hasn't changed, or was updated by a recently
934 * received AGM-CONTACT packet.
940 /* mt_state was updated by AGM-CONTACT packet */
945 /* Handle case where mt_state->count = 4, or = 5 */
946 static void synaptics_image_sensor_45f(struct synaptics_data *priv,
947 struct synaptics_mt_state *mt_state)
949 /* mt_state was updated correctly by AGM-CONTACT packet */
950 priv->mt_state_lost = false;
953 static void synaptics_image_sensor_process(struct psmouse *psmouse,
954 struct synaptics_hw_state *sgm)
956 struct synaptics_data *priv = psmouse->private;
957 struct synaptics_hw_state *agm = &priv->agm;
958 struct synaptics_mt_state mt_state;
960 /* Initialize using current mt_state (as updated by last agm) */
961 mt_state = agm->mt_state;
964 * Update mt_state using the new finger count and current mt_state.
967 synaptics_image_sensor_0f(priv, &mt_state);
968 else if (sgm->w >= 4)
969 synaptics_image_sensor_1f(priv, &mt_state);
970 else if (sgm->w == 0)
971 synaptics_image_sensor_2f(priv, &mt_state);
972 else if (sgm->w == 1 && mt_state.count <= 3)
973 synaptics_image_sensor_3f(priv, &mt_state);
975 synaptics_image_sensor_45f(priv, &mt_state);
977 /* Send resulting input events to user space */
978 synaptics_report_mt_data(psmouse, &mt_state, sgm);
980 /* Store updated mt_state */
981 priv->mt_state = agm->mt_state = mt_state;
982 priv->agm_pending = false;
986 * called for each full received packet from the touchpad
988 static void synaptics_process_packet(struct psmouse *psmouse)
990 struct input_dev *dev = psmouse->dev;
991 struct synaptics_data *priv = psmouse->private;
992 struct synaptics_hw_state hw;
996 if (synaptics_parse_hw_state(psmouse->packet, priv, &hw))
999 if (SYN_CAP_IMAGE_SENSOR(priv->ext_cap_0c)) {
1000 synaptics_image_sensor_process(psmouse, &hw);
1005 priv->scroll += hw.scroll;
1007 while (priv->scroll >= 4) {
1008 input_report_key(dev, BTN_BACK, !hw.down);
1010 input_report_key(dev, BTN_BACK, hw.down);
1014 while (priv->scroll <= -4) {
1015 input_report_key(dev, BTN_FORWARD, !hw.up);
1017 input_report_key(dev, BTN_FORWARD, hw.up);
1024 if (hw.z > 0 && hw.x > 1) {
1027 if (SYN_CAP_EXTENDED(priv->capabilities)) {
1030 if (SYN_CAP_MULTIFINGER(priv->capabilities))
1031 num_fingers = hw.w + 2;
1034 if (SYN_MODEL_PEN(priv->model_id))
1035 ; /* Nothing, treat a pen as a single finger */
1038 if (SYN_CAP_PALMDETECT(priv->capabilities))
1039 finger_width = hw.w;
1048 if (SYN_CAP_ADV_GESTURE(priv->ext_cap_0c))
1049 synaptics_report_semi_mt_data(dev, &hw, &priv->agm,
1053 * BTN_TOUCH has to be first as mousedev relies on it when doing
1054 * absolute -> relative conversion
1056 if (hw.z > 30) input_report_key(dev, BTN_TOUCH, 1);
1057 if (hw.z < 25) input_report_key(dev, BTN_TOUCH, 0);
1059 if (num_fingers > 0) {
1060 input_report_abs(dev, ABS_X, hw.x);
1061 input_report_abs(dev, ABS_Y, synaptics_invert_y(hw.y));
1063 input_report_abs(dev, ABS_PRESSURE, hw.z);
1065 if (SYN_CAP_PALMDETECT(priv->capabilities))
1066 input_report_abs(dev, ABS_TOOL_WIDTH, finger_width);
1068 input_report_key(dev, BTN_TOOL_FINGER, num_fingers == 1);
1069 if (SYN_CAP_MULTIFINGER(priv->capabilities)) {
1070 input_report_key(dev, BTN_TOOL_DOUBLETAP, num_fingers == 2);
1071 input_report_key(dev, BTN_TOOL_TRIPLETAP, num_fingers == 3);
1074 synaptics_report_buttons(psmouse, &hw);
1079 static int synaptics_validate_byte(struct psmouse *psmouse,
1080 int idx, unsigned char pkt_type)
1082 static const unsigned char newabs_mask[] = { 0xC8, 0x00, 0x00, 0xC8, 0x00 };
1083 static const unsigned char newabs_rel_mask[] = { 0xC0, 0x00, 0x00, 0xC0, 0x00 };
1084 static const unsigned char newabs_rslt[] = { 0x80, 0x00, 0x00, 0xC0, 0x00 };
1085 static const unsigned char oldabs_mask[] = { 0xC0, 0x60, 0x00, 0xC0, 0x60 };
1086 static const unsigned char oldabs_rslt[] = { 0xC0, 0x00, 0x00, 0x80, 0x00 };
1087 const char *packet = psmouse->packet;
1089 if (idx < 0 || idx > 4)
1095 case SYN_NEWABS_RELAXED:
1096 return (packet[idx] & newabs_rel_mask[idx]) == newabs_rslt[idx];
1098 case SYN_NEWABS_STRICT:
1099 return (packet[idx] & newabs_mask[idx]) == newabs_rslt[idx];
1102 return (packet[idx] & oldabs_mask[idx]) == oldabs_rslt[idx];
1105 psmouse_err(psmouse, "unknown packet type %d\n", pkt_type);
1110 static unsigned char synaptics_detect_pkt_type(struct psmouse *psmouse)
1114 for (i = 0; i < 5; i++)
1115 if (!synaptics_validate_byte(psmouse, i, SYN_NEWABS_STRICT)) {
1116 psmouse_info(psmouse, "using relaxed packet validation\n");
1117 return SYN_NEWABS_RELAXED;
1120 return SYN_NEWABS_STRICT;
1123 static psmouse_ret_t synaptics_process_byte(struct psmouse *psmouse)
1125 struct synaptics_data *priv = psmouse->private;
1127 if (psmouse->pktcnt >= 6) { /* Full packet received */
1128 if (unlikely(priv->pkt_type == SYN_NEWABS))
1129 priv->pkt_type = synaptics_detect_pkt_type(psmouse);
1131 if (SYN_CAP_PASS_THROUGH(priv->capabilities) &&
1132 synaptics_is_pt_packet(psmouse->packet)) {
1134 synaptics_pass_pt_packet(priv->pt_port, psmouse->packet);
1136 synaptics_process_packet(psmouse);
1138 return PSMOUSE_FULL_PACKET;
1141 return synaptics_validate_byte(psmouse, psmouse->pktcnt - 1, priv->pkt_type) ?
1142 PSMOUSE_GOOD_DATA : PSMOUSE_BAD_DATA;
1145 /*****************************************************************************
1146 * Driver initialization/cleanup functions
1147 ****************************************************************************/
1148 static void set_abs_position_params(struct input_dev *dev,
1149 struct synaptics_data *priv, int x_code,
1152 int x_min = priv->x_min ?: XMIN_NOMINAL;
1153 int x_max = priv->x_max ?: XMAX_NOMINAL;
1154 int y_min = priv->y_min ?: YMIN_NOMINAL;
1155 int y_max = priv->y_max ?: YMAX_NOMINAL;
1156 int fuzz = SYN_CAP_REDUCED_FILTERING(priv->ext_cap_0c) ?
1157 SYN_REDUCED_FILTER_FUZZ : 0;
1159 input_set_abs_params(dev, x_code, x_min, x_max, fuzz, 0);
1160 input_set_abs_params(dev, y_code, y_min, y_max, fuzz, 0);
1161 input_abs_set_res(dev, x_code, priv->x_res);
1162 input_abs_set_res(dev, y_code, priv->y_res);
1165 static void set_input_params(struct input_dev *dev, struct synaptics_data *priv)
1169 __set_bit(INPUT_PROP_POINTER, dev->propbit);
1171 __set_bit(EV_ABS, dev->evbit);
1172 set_abs_position_params(dev, priv, ABS_X, ABS_Y);
1173 input_set_abs_params(dev, ABS_PRESSURE, 0, 255, 0, 0);
1175 if (SYN_CAP_IMAGE_SENSOR(priv->ext_cap_0c)) {
1176 input_mt_init_slots(dev, 2);
1177 set_abs_position_params(dev, priv, ABS_MT_POSITION_X,
1179 /* Image sensors can report per-contact pressure */
1180 input_set_abs_params(dev, ABS_MT_PRESSURE, 0, 255, 0, 0);
1182 /* Image sensors can signal 4 and 5 finger clicks */
1183 __set_bit(BTN_TOOL_QUADTAP, dev->keybit);
1184 __set_bit(BTN_TOOL_QUINTTAP, dev->keybit);
1185 } else if (SYN_CAP_ADV_GESTURE(priv->ext_cap_0c)) {
1186 /* Non-image sensors with AGM use semi-mt */
1187 __set_bit(INPUT_PROP_SEMI_MT, dev->propbit);
1188 input_mt_init_slots(dev, 2);
1189 set_abs_position_params(dev, priv, ABS_MT_POSITION_X,
1193 if (SYN_CAP_PALMDETECT(priv->capabilities))
1194 input_set_abs_params(dev, ABS_TOOL_WIDTH, 0, 15, 0, 0);
1196 __set_bit(EV_KEY, dev->evbit);
1197 __set_bit(BTN_TOUCH, dev->keybit);
1198 __set_bit(BTN_TOOL_FINGER, dev->keybit);
1199 __set_bit(BTN_LEFT, dev->keybit);
1200 __set_bit(BTN_RIGHT, dev->keybit);
1202 if (SYN_CAP_MULTIFINGER(priv->capabilities)) {
1203 __set_bit(BTN_TOOL_DOUBLETAP, dev->keybit);
1204 __set_bit(BTN_TOOL_TRIPLETAP, dev->keybit);
1207 if (SYN_CAP_MIDDLE_BUTTON(priv->capabilities))
1208 __set_bit(BTN_MIDDLE, dev->keybit);
1210 if (SYN_CAP_FOUR_BUTTON(priv->capabilities) ||
1211 SYN_CAP_MIDDLE_BUTTON(priv->capabilities)) {
1212 __set_bit(BTN_FORWARD, dev->keybit);
1213 __set_bit(BTN_BACK, dev->keybit);
1216 for (i = 0; i < SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap); i++)
1217 __set_bit(BTN_0 + i, dev->keybit);
1219 __clear_bit(EV_REL, dev->evbit);
1220 __clear_bit(REL_X, dev->relbit);
1221 __clear_bit(REL_Y, dev->relbit);
1223 if (SYN_CAP_CLICKPAD(priv->ext_cap_0c)) {
1224 __set_bit(INPUT_PROP_BUTTONPAD, dev->propbit);
1225 /* Clickpads report only left button */
1226 __clear_bit(BTN_RIGHT, dev->keybit);
1227 __clear_bit(BTN_MIDDLE, dev->keybit);
1231 static void synaptics_disconnect(struct psmouse *psmouse)
1233 synaptics_reset(psmouse);
1234 kfree(psmouse->private);
1235 psmouse->private = NULL;
1238 static int synaptics_reconnect(struct psmouse *psmouse)
1240 struct synaptics_data *priv = psmouse->private;
1241 struct synaptics_data old_priv = *priv;
1246 psmouse_reset(psmouse);
1249 * On some boxes, right after resuming, the touchpad
1250 * needs some time to finish initializing (I assume
1251 * it needs time to calibrate) and start responding
1252 * to Synaptics-specific queries, so let's wait a
1257 error = synaptics_detect(psmouse, 0);
1258 } while (error && ++retry < 3);
1264 psmouse_dbg(psmouse, "reconnected after %d tries\n", retry);
1266 if (synaptics_query_hardware(psmouse)) {
1267 psmouse_err(psmouse, "Unable to query device.\n");
1271 if (synaptics_set_absolute_mode(psmouse)) {
1272 psmouse_err(psmouse, "Unable to initialize device.\n");
1276 if (synaptics_set_advanced_gesture_mode(psmouse)) {
1277 psmouse_err(psmouse,
1278 "Advanced gesture mode reconnect failed.\n");
1282 if (old_priv.identity != priv->identity ||
1283 old_priv.model_id != priv->model_id ||
1284 old_priv.capabilities != priv->capabilities ||
1285 old_priv.ext_cap != priv->ext_cap) {
1286 psmouse_err(psmouse,
1287 "hardware appears to be different: id(%ld-%ld), model(%ld-%ld), caps(%lx-%lx), ext(%lx-%lx).\n",
1288 old_priv.identity, priv->identity,
1289 old_priv.model_id, priv->model_id,
1290 old_priv.capabilities, priv->capabilities,
1291 old_priv.ext_cap, priv->ext_cap);
1298 static bool impaired_toshiba_kbc;
1300 static const struct dmi_system_id __initconst toshiba_dmi_table[] = {
1301 #if defined(CONFIG_DMI) && defined(CONFIG_X86)
1303 /* Toshiba Satellite */
1305 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1306 DMI_MATCH(DMI_PRODUCT_NAME, "Satellite"),
1310 /* Toshiba Dynabook */
1312 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1313 DMI_MATCH(DMI_PRODUCT_NAME, "dynabook"),
1317 /* Toshiba Portege M300 */
1319 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1320 DMI_MATCH(DMI_PRODUCT_NAME, "PORTEGE M300"),
1325 /* Toshiba Portege M300 */
1327 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1328 DMI_MATCH(DMI_PRODUCT_NAME, "Portable PC"),
1329 DMI_MATCH(DMI_PRODUCT_VERSION, "Version 1.0"),
1337 static bool broken_olpc_ec;
1339 static const struct dmi_system_id __initconst olpc_dmi_table[] = {
1340 #if defined(CONFIG_DMI) && defined(CONFIG_OLPC)
1342 /* OLPC XO-1 or XO-1.5 */
1344 DMI_MATCH(DMI_SYS_VENDOR, "OLPC"),
1345 DMI_MATCH(DMI_PRODUCT_NAME, "XO"),
1352 void __init synaptics_module_init(void)
1354 impaired_toshiba_kbc = dmi_check_system(toshiba_dmi_table);
1355 broken_olpc_ec = dmi_check_system(olpc_dmi_table);
1358 int synaptics_init(struct psmouse *psmouse)
1360 struct synaptics_data *priv;
1363 * The OLPC XO has issues with Synaptics' absolute mode; similarly to
1364 * the HGPK, it quickly degrades and the hardware becomes jumpy and
1365 * overly sensitive. Not only that, but the constant packet spew
1366 * (even at a lowered 40pps rate) overloads the EC such that key
1367 * presses on the keyboard are missed. Given all of that, don't
1368 * even attempt to use Synaptics mode. Relative mode seems to work
1371 if (broken_olpc_ec) {
1372 psmouse_info(psmouse,
1373 "OLPC XO detected, not enabling Synaptics protocol.\n");
1377 psmouse->private = priv = kzalloc(sizeof(struct synaptics_data), GFP_KERNEL);
1381 psmouse_reset(psmouse);
1383 if (synaptics_query_hardware(psmouse)) {
1384 psmouse_err(psmouse, "Unable to query device.\n");
1388 if (synaptics_set_absolute_mode(psmouse)) {
1389 psmouse_err(psmouse, "Unable to initialize device.\n");
1393 if (synaptics_set_advanced_gesture_mode(psmouse)) {
1394 psmouse_err(psmouse, "Advanced gesture mode init failed.\n");
1398 priv->pkt_type = SYN_MODEL_NEWABS(priv->model_id) ? SYN_NEWABS : SYN_OLDABS;
1400 psmouse_info(psmouse,
1401 "Touchpad model: %ld, fw: %ld.%ld, id: %#lx, caps: %#lx/%#lx/%#lx\n",
1402 SYN_ID_MODEL(priv->identity),
1403 SYN_ID_MAJOR(priv->identity), SYN_ID_MINOR(priv->identity),
1405 priv->capabilities, priv->ext_cap, priv->ext_cap_0c);
1407 set_input_params(psmouse->dev, priv);
1410 * Encode touchpad model so that it can be used to set
1411 * input device->id.version and be visible to userspace.
1412 * Because version is __u16 we have to drop something.
1413 * Hardware info bits seem to be good candidates as they
1414 * are documented to be for Synaptics corp. internal use.
1416 psmouse->model = ((priv->model_id & 0x00ff0000) >> 8) |
1417 (priv->model_id & 0x000000ff);
1419 psmouse->protocol_handler = synaptics_process_byte;
1420 psmouse->set_rate = synaptics_set_rate;
1421 psmouse->disconnect = synaptics_disconnect;
1422 psmouse->reconnect = synaptics_reconnect;
1423 psmouse->cleanup = synaptics_reset;
1424 psmouse->pktsize = 6;
1425 /* Synaptics can usually stay in sync without extra help */
1426 psmouse->resync_time = 0;
1428 if (SYN_CAP_PASS_THROUGH(priv->capabilities))
1429 synaptics_pt_create(psmouse);
1432 * Toshiba's KBC seems to have trouble handling data from
1433 * Synaptics at full rate. Switch to a lower rate (roughly
1434 * the same rate as a standard PS/2 mouse).
1436 if (psmouse->rate >= 80 && impaired_toshiba_kbc) {
1437 psmouse_info(psmouse,
1438 "Toshiba %s detected, limiting rate to 40pps.\n",
1439 dmi_get_system_info(DMI_PRODUCT_NAME));
1450 bool synaptics_supported(void)
1455 #else /* CONFIG_MOUSE_PS2_SYNAPTICS */
1457 void __init synaptics_module_init(void)
1461 int synaptics_init(struct psmouse *psmouse)
1466 bool synaptics_supported(void)
1471 #endif /* CONFIG_MOUSE_PS2_SYNAPTICS */