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/input.h>
28 #include <linux/serio.h>
29 #include <linux/libps2.h>
31 #include "synaptics.h"
34 * The x/y limits are taken from the Synaptics TouchPad interfacing Guide,
35 * section 2.3.2, which says that they should be valid regardless of the
36 * actual size of the sensor.
38 #define XMIN_NOMINAL 1472
39 #define XMAX_NOMINAL 5472
40 #define YMIN_NOMINAL 1408
41 #define YMAX_NOMINAL 4448
43 /*****************************************************************************
44 * Synaptics communications functions
45 ****************************************************************************/
48 * Send a command to the synpatics touchpad by special commands
50 static int synaptics_send_cmd(struct psmouse
*psmouse
, unsigned char c
, unsigned char *param
)
52 if (psmouse_sliced_command(psmouse
, c
))
54 if (ps2_command(&psmouse
->ps2dev
, param
, PSMOUSE_CMD_GETINFO
))
60 * Set the synaptics touchpad mode byte by special commands
62 static int synaptics_mode_cmd(struct psmouse
*psmouse
, unsigned char mode
)
64 unsigned char param
[1];
66 if (psmouse_sliced_command(psmouse
, mode
))
68 param
[0] = SYN_PS_SET_MODE2
;
69 if (ps2_command(&psmouse
->ps2dev
, param
, PSMOUSE_CMD_SETRATE
))
75 * Read the model-id bytes from the touchpad
76 * see also SYN_MODEL_* macros
78 static int synaptics_model_id(struct psmouse
*psmouse
)
80 struct synaptics_data
*priv
= psmouse
->private;
83 if (synaptics_send_cmd(psmouse
, SYN_QUE_MODEL
, mi
))
85 priv
->model_id
= (mi
[0]<<16) | (mi
[1]<<8) | mi
[2];
90 * Read the capability-bits from the touchpad
91 * see also the SYN_CAP_* macros
93 static int synaptics_capability(struct psmouse
*psmouse
)
95 struct synaptics_data
*priv
= psmouse
->private;
98 if (synaptics_send_cmd(psmouse
, SYN_QUE_CAPABILITIES
, cap
))
100 priv
->capabilities
= (cap
[0] << 16) | (cap
[1] << 8) | cap
[2];
102 if (!SYN_CAP_VALID(priv
->capabilities
))
106 * Unless capExtended is set the rest of the flags should be ignored
108 if (!SYN_CAP_EXTENDED(priv
->capabilities
))
109 priv
->capabilities
= 0;
111 if (SYN_EXT_CAP_REQUESTS(priv
->capabilities
) >= 1) {
112 if (synaptics_send_cmd(psmouse
, SYN_QUE_EXT_CAPAB
, cap
)) {
113 printk(KERN_ERR
"Synaptics claims to have extended capabilities,"
114 " but I'm not able to read them.");
116 priv
->ext_cap
= (cap
[0] << 16) | (cap
[1] << 8) | cap
[2];
119 * if nExtBtn is greater than 8 it should be considered
120 * invalid and treated as 0
122 if (SYN_CAP_MULTI_BUTTON_NO(priv
->ext_cap
) > 8)
123 priv
->ext_cap
&= 0xff0fff;
131 * See also the SYN_ID_* macros
133 static int synaptics_identify(struct psmouse
*psmouse
)
135 struct synaptics_data
*priv
= psmouse
->private;
138 if (synaptics_send_cmd(psmouse
, SYN_QUE_IDENTIFY
, id
))
140 priv
->identity
= (id
[0]<<16) | (id
[1]<<8) | id
[2];
141 if (SYN_ID_IS_SYNAPTICS(priv
->identity
))
146 static int synaptics_query_hardware(struct psmouse
*psmouse
)
150 while ((retries
++ < 3) && psmouse_reset(psmouse
))
151 printk(KERN_ERR
"synaptics reset failed\n");
153 if (synaptics_identify(psmouse
))
155 if (synaptics_model_id(psmouse
))
157 if (synaptics_capability(psmouse
))
163 static int synaptics_set_absolute_mode(struct psmouse
*psmouse
)
165 struct synaptics_data
*priv
= psmouse
->private;
167 priv
->mode
= SYN_BIT_ABSOLUTE_MODE
;
168 if (SYN_ID_MAJOR(priv
->identity
) >= 4)
169 priv
->mode
|= SYN_BIT_DISABLE_GESTURE
;
170 if (SYN_CAP_EXTENDED(priv
->capabilities
))
171 priv
->mode
|= SYN_BIT_W_MODE
;
173 if (synaptics_mode_cmd(psmouse
, priv
->mode
))
179 static void synaptics_set_rate(struct psmouse
*psmouse
, unsigned int rate
)
181 struct synaptics_data
*priv
= psmouse
->private;
184 priv
->mode
|= SYN_BIT_HIGH_RATE
;
187 priv
->mode
&= ~SYN_BIT_HIGH_RATE
;
191 synaptics_mode_cmd(psmouse
, priv
->mode
);
194 /*****************************************************************************
195 * Synaptics pass-through PS/2 port support
196 ****************************************************************************/
197 static int synaptics_pt_write(struct serio
*serio
, unsigned char c
)
199 struct psmouse
*parent
= serio_get_drvdata(serio
->parent
);
200 char rate_param
= SYN_PS_CLIENT_CMD
; /* indicates that we want pass-through port */
202 if (psmouse_sliced_command(parent
, c
))
204 if (ps2_command(&parent
->ps2dev
, &rate_param
, PSMOUSE_CMD_SETRATE
))
209 static inline int synaptics_is_pt_packet(unsigned char *buf
)
211 return (buf
[0] & 0xFC) == 0x84 && (buf
[3] & 0xCC) == 0xC4;
214 static void synaptics_pass_pt_packet(struct serio
*ptport
, unsigned char *packet
)
216 struct psmouse
*child
= serio_get_drvdata(ptport
);
218 if (child
&& child
->state
== PSMOUSE_ACTIVATED
) {
219 serio_interrupt(ptport
, packet
[1], 0, NULL
);
220 serio_interrupt(ptport
, packet
[4], 0, NULL
);
221 serio_interrupt(ptport
, packet
[5], 0, NULL
);
222 if (child
->pktsize
== 4)
223 serio_interrupt(ptport
, packet
[2], 0, NULL
);
225 serio_interrupt(ptport
, packet
[1], 0, NULL
);
228 static void synaptics_pt_activate(struct psmouse
*psmouse
)
230 struct serio
*ptport
= psmouse
->ps2dev
.serio
->child
;
231 struct psmouse
*child
= serio_get_drvdata(ptport
);
232 struct synaptics_data
*priv
= psmouse
->private;
234 /* adjust the touchpad to child's choice of protocol */
236 if (child
->pktsize
== 4)
237 priv
->mode
|= SYN_BIT_FOUR_BYTE_CLIENT
;
239 priv
->mode
&= ~SYN_BIT_FOUR_BYTE_CLIENT
;
241 if (synaptics_mode_cmd(psmouse
, priv
->mode
))
242 printk(KERN_INFO
"synaptics: failed to switch guest protocol\n");
246 static void synaptics_pt_create(struct psmouse
*psmouse
)
250 serio
= kmalloc(sizeof(struct serio
), GFP_KERNEL
);
252 printk(KERN_ERR
"synaptics: not enough memory to allocate pass-through port\n");
256 memset(serio
, 0, sizeof(struct serio
));
258 serio
->id
.type
= SERIO_PS_PSTHRU
;
259 strlcpy(serio
->name
, "Synaptics pass-through", sizeof(serio
->name
));
260 strlcpy(serio
->phys
, "synaptics-pt/serio0", sizeof(serio
->name
));
261 serio
->write
= synaptics_pt_write
;
262 serio
->parent
= psmouse
->ps2dev
.serio
;
264 psmouse
->pt_activate
= synaptics_pt_activate
;
266 printk(KERN_INFO
"serio: %s port at %s\n", serio
->name
, psmouse
->phys
);
267 serio_register_port(serio
);
270 /*****************************************************************************
271 * Functions to interpret the absolute mode packets
272 ****************************************************************************/
274 static void synaptics_parse_hw_state(unsigned char buf
[], struct synaptics_data
*priv
, struct synaptics_hw_state
*hw
)
276 memset(hw
, 0, sizeof(struct synaptics_hw_state
));
278 if (SYN_MODEL_NEWABS(priv
->model_id
)) {
279 hw
->x
= (((buf
[3] & 0x10) << 8) |
280 ((buf
[1] & 0x0f) << 8) |
282 hw
->y
= (((buf
[3] & 0x20) << 7) |
283 ((buf
[1] & 0xf0) << 4) |
287 hw
->w
= (((buf
[0] & 0x30) >> 2) |
288 ((buf
[0] & 0x04) >> 1) |
289 ((buf
[3] & 0x04) >> 2));
291 hw
->left
= (buf
[0] & 0x01) ? 1 : 0;
292 hw
->right
= (buf
[0] & 0x02) ? 1 : 0;
294 if (SYN_CAP_MIDDLE_BUTTON(priv
->capabilities
)) {
295 hw
->middle
= ((buf
[0] ^ buf
[3]) & 0x01) ? 1 : 0;
297 hw
->scroll
= (signed char)(buf
[1]);
300 if (SYN_CAP_FOUR_BUTTON(priv
->capabilities
)) {
301 hw
->up
= ((buf
[0] ^ buf
[3]) & 0x01) ? 1 : 0;
302 hw
->down
= ((buf
[0] ^ buf
[3]) & 0x02) ? 1 : 0;
305 if (SYN_CAP_MULTI_BUTTON_NO(priv
->ext_cap
) &&
306 ((buf
[0] ^ buf
[3]) & 0x02)) {
307 switch (SYN_CAP_MULTI_BUTTON_NO(priv
->ext_cap
) & ~0x01) {
310 * if nExtBtn is greater than 8 it should be
311 * considered invalid and treated as 0
315 hw
->ext_buttons
|= ((buf
[5] & 0x08)) ? 0x80 : 0;
316 hw
->ext_buttons
|= ((buf
[4] & 0x08)) ? 0x40 : 0;
318 hw
->ext_buttons
|= ((buf
[5] & 0x04)) ? 0x20 : 0;
319 hw
->ext_buttons
|= ((buf
[4] & 0x04)) ? 0x10 : 0;
321 hw
->ext_buttons
|= ((buf
[5] & 0x02)) ? 0x08 : 0;
322 hw
->ext_buttons
|= ((buf
[4] & 0x02)) ? 0x04 : 0;
324 hw
->ext_buttons
|= ((buf
[5] & 0x01)) ? 0x02 : 0;
325 hw
->ext_buttons
|= ((buf
[4] & 0x01)) ? 0x01 : 0;
329 hw
->x
= (((buf
[1] & 0x1f) << 8) | buf
[2]);
330 hw
->y
= (((buf
[4] & 0x1f) << 8) | buf
[5]);
332 hw
->z
= (((buf
[0] & 0x30) << 2) | (buf
[3] & 0x3F));
333 hw
->w
= (((buf
[1] & 0x80) >> 4) | ((buf
[0] & 0x04) >> 1));
335 hw
->left
= (buf
[0] & 0x01) ? 1 : 0;
336 hw
->right
= (buf
[0] & 0x02) ? 1 : 0;
341 * called for each full received packet from the touchpad
343 static void synaptics_process_packet(struct psmouse
*psmouse
)
345 struct input_dev
*dev
= &psmouse
->dev
;
346 struct synaptics_data
*priv
= psmouse
->private;
347 struct synaptics_hw_state hw
;
352 synaptics_parse_hw_state(psmouse
->packet
, priv
, &hw
);
355 priv
->scroll
+= hw
.scroll
;
357 while (priv
->scroll
>= 4) {
358 input_report_key(dev
, BTN_BACK
, !hw
.down
);
360 input_report_key(dev
, BTN_BACK
, hw
.down
);
364 while (priv
->scroll
<= -4) {
365 input_report_key(dev
, BTN_FORWARD
, !hw
.up
);
367 input_report_key(dev
, BTN_FORWARD
, hw
.up
);
377 if (SYN_CAP_EXTENDED(priv
->capabilities
)) {
380 if (SYN_CAP_MULTIFINGER(priv
->capabilities
))
381 num_fingers
= hw
.w
+ 2;
384 if (SYN_MODEL_PEN(priv
->model_id
))
385 ; /* Nothing, treat a pen as a single finger */
388 if (SYN_CAP_PALMDETECT(priv
->capabilities
))
399 * BTN_TOUCH has to be first as mousedev relies on it when doing
400 * absolute -> relative conversion
402 if (hw
.z
> 30) input_report_key(dev
, BTN_TOUCH
, 1);
403 if (hw
.z
< 25) input_report_key(dev
, BTN_TOUCH
, 0);
406 input_report_abs(dev
, ABS_X
, hw
.x
);
407 input_report_abs(dev
, ABS_Y
, YMAX_NOMINAL
+ YMIN_NOMINAL
- hw
.y
);
409 input_report_abs(dev
, ABS_PRESSURE
, hw
.z
);
411 input_report_abs(dev
, ABS_TOOL_WIDTH
, finger_width
);
412 input_report_key(dev
, BTN_TOOL_FINGER
, num_fingers
== 1);
413 input_report_key(dev
, BTN_TOOL_DOUBLETAP
, num_fingers
== 2);
414 input_report_key(dev
, BTN_TOOL_TRIPLETAP
, num_fingers
== 3);
416 input_report_key(dev
, BTN_LEFT
, hw
.left
);
417 input_report_key(dev
, BTN_RIGHT
, hw
.right
);
419 if (SYN_CAP_MIDDLE_BUTTON(priv
->capabilities
))
420 input_report_key(dev
, BTN_MIDDLE
, hw
.middle
);
422 if (SYN_CAP_FOUR_BUTTON(priv
->capabilities
)) {
423 input_report_key(dev
, BTN_FORWARD
, hw
.up
);
424 input_report_key(dev
, BTN_BACK
, hw
.down
);
427 for (i
= 0; i
< SYN_CAP_MULTI_BUTTON_NO(priv
->ext_cap
); i
++)
428 input_report_key(dev
, BTN_0
+ i
, hw
.ext_buttons
& (1 << i
));
433 static int synaptics_validate_byte(unsigned char packet
[], int idx
, unsigned char pkt_type
)
435 static unsigned char newabs_mask
[] = { 0xC8, 0x00, 0x00, 0xC8, 0x00 };
436 static unsigned char newabs_rel_mask
[] = { 0xC0, 0x00, 0x00, 0xC0, 0x00 };
437 static unsigned char newabs_rslt
[] = { 0x80, 0x00, 0x00, 0xC0, 0x00 };
438 static unsigned char oldabs_mask
[] = { 0xC0, 0x60, 0x00, 0xC0, 0x60 };
439 static unsigned char oldabs_rslt
[] = { 0xC0, 0x00, 0x00, 0x80, 0x00 };
441 if (idx
< 0 || idx
> 4)
446 case SYN_NEWABS_RELAXED
:
447 return (packet
[idx
] & newabs_rel_mask
[idx
]) == newabs_rslt
[idx
];
449 case SYN_NEWABS_STRICT
:
450 return (packet
[idx
] & newabs_mask
[idx
]) == newabs_rslt
[idx
];
453 return (packet
[idx
] & oldabs_mask
[idx
]) == oldabs_rslt
[idx
];
456 printk(KERN_ERR
"synaptics: unknown packet type %d\n", pkt_type
);
461 static unsigned char synaptics_detect_pkt_type(struct psmouse
*psmouse
)
465 for (i
= 0; i
< 5; i
++)
466 if (!synaptics_validate_byte(psmouse
->packet
, i
, SYN_NEWABS_STRICT
)) {
467 printk(KERN_INFO
"synaptics: using relaxed packet validation\n");
468 return SYN_NEWABS_RELAXED
;
471 return SYN_NEWABS_STRICT
;
474 static psmouse_ret_t
synaptics_process_byte(struct psmouse
*psmouse
, struct pt_regs
*regs
)
476 struct input_dev
*dev
= &psmouse
->dev
;
477 struct synaptics_data
*priv
= psmouse
->private;
479 input_regs(dev
, regs
);
481 if (psmouse
->pktcnt
>= 6) { /* Full packet received */
482 if (unlikely(priv
->pkt_type
== SYN_NEWABS
))
483 priv
->pkt_type
= synaptics_detect_pkt_type(psmouse
);
485 if (SYN_CAP_PASS_THROUGH(priv
->capabilities
) && synaptics_is_pt_packet(psmouse
->packet
)) {
486 if (psmouse
->ps2dev
.serio
->child
)
487 synaptics_pass_pt_packet(psmouse
->ps2dev
.serio
->child
, psmouse
->packet
);
489 synaptics_process_packet(psmouse
);
491 return PSMOUSE_FULL_PACKET
;
494 return synaptics_validate_byte(psmouse
->packet
, psmouse
->pktcnt
- 1, priv
->pkt_type
) ?
495 PSMOUSE_GOOD_DATA
: PSMOUSE_BAD_DATA
;
498 /*****************************************************************************
499 * Driver initialization/cleanup functions
500 ****************************************************************************/
501 static void set_input_params(struct input_dev
*dev
, struct synaptics_data
*priv
)
505 set_bit(EV_ABS
, dev
->evbit
);
506 input_set_abs_params(dev
, ABS_X
, XMIN_NOMINAL
, XMAX_NOMINAL
, 0, 0);
507 input_set_abs_params(dev
, ABS_Y
, YMIN_NOMINAL
, YMAX_NOMINAL
, 0, 0);
508 input_set_abs_params(dev
, ABS_PRESSURE
, 0, 255, 0, 0);
509 set_bit(ABS_TOOL_WIDTH
, dev
->absbit
);
511 set_bit(EV_KEY
, dev
->evbit
);
512 set_bit(BTN_TOUCH
, dev
->keybit
);
513 set_bit(BTN_TOOL_FINGER
, dev
->keybit
);
514 set_bit(BTN_TOOL_DOUBLETAP
, dev
->keybit
);
515 set_bit(BTN_TOOL_TRIPLETAP
, dev
->keybit
);
517 set_bit(BTN_LEFT
, dev
->keybit
);
518 set_bit(BTN_RIGHT
, dev
->keybit
);
520 if (SYN_CAP_MIDDLE_BUTTON(priv
->capabilities
))
521 set_bit(BTN_MIDDLE
, dev
->keybit
);
523 if (SYN_CAP_FOUR_BUTTON(priv
->capabilities
) ||
524 SYN_CAP_MIDDLE_BUTTON(priv
->capabilities
)) {
525 set_bit(BTN_FORWARD
, dev
->keybit
);
526 set_bit(BTN_BACK
, dev
->keybit
);
529 for (i
= 0; i
< SYN_CAP_MULTI_BUTTON_NO(priv
->ext_cap
); i
++)
530 set_bit(BTN_0
+ i
, dev
->keybit
);
532 clear_bit(EV_REL
, dev
->evbit
);
533 clear_bit(REL_X
, dev
->relbit
);
534 clear_bit(REL_Y
, dev
->relbit
);
537 void synaptics_reset(struct psmouse
*psmouse
)
539 /* reset touchpad back to relative mode, gestures enabled */
540 synaptics_mode_cmd(psmouse
, 0);
543 static void synaptics_disconnect(struct psmouse
*psmouse
)
545 synaptics_reset(psmouse
);
546 kfree(psmouse
->private);
547 psmouse
->private = NULL
;
550 static int synaptics_reconnect(struct psmouse
*psmouse
)
552 struct synaptics_data
*priv
= psmouse
->private;
553 struct synaptics_data old_priv
= *priv
;
555 if (synaptics_detect(psmouse
, 0))
558 if (synaptics_query_hardware(psmouse
)) {
559 printk(KERN_ERR
"Unable to query Synaptics hardware.\n");
563 if (old_priv
.identity
!= priv
->identity
||
564 old_priv
.model_id
!= priv
->model_id
||
565 old_priv
.capabilities
!= priv
->capabilities
||
566 old_priv
.ext_cap
!= priv
->ext_cap
)
569 if (synaptics_set_absolute_mode(psmouse
)) {
570 printk(KERN_ERR
"Unable to initialize Synaptics hardware.\n");
577 int synaptics_detect(struct psmouse
*psmouse
, int set_properties
)
579 struct ps2dev
*ps2dev
= &psmouse
->ps2dev
;
580 unsigned char param
[4];
584 ps2_command(ps2dev
, param
, PSMOUSE_CMD_SETRES
);
585 ps2_command(ps2dev
, param
, PSMOUSE_CMD_SETRES
);
586 ps2_command(ps2dev
, param
, PSMOUSE_CMD_SETRES
);
587 ps2_command(ps2dev
, param
, PSMOUSE_CMD_SETRES
);
588 ps2_command(ps2dev
, param
, PSMOUSE_CMD_GETINFO
);
590 if (param
[1] != 0x47)
593 if (set_properties
) {
594 psmouse
->vendor
= "Synaptics";
595 psmouse
->name
= "TouchPad";
601 #if defined(__i386__)
602 #include <linux/dmi.h>
603 static struct dmi_system_id toshiba_dmi_table
[] = {
605 .ident
= "Toshiba Satellite",
607 DMI_MATCH(DMI_SYS_VENDOR
, "TOSHIBA"),
608 DMI_MATCH(DMI_PRODUCT_NAME
, "Satellite"),
612 .ident
= "Toshiba Dynabook",
614 DMI_MATCH(DMI_SYS_VENDOR
, "TOSHIBA"),
615 DMI_MATCH(DMI_PRODUCT_NAME
, "dynabook"),
622 int synaptics_init(struct psmouse
*psmouse
)
624 struct synaptics_data
*priv
;
626 psmouse
->private = priv
= kmalloc(sizeof(struct synaptics_data
), GFP_KERNEL
);
629 memset(priv
, 0, sizeof(struct synaptics_data
));
631 if (synaptics_query_hardware(psmouse
)) {
632 printk(KERN_ERR
"Unable to query Synaptics hardware.\n");
636 if (synaptics_set_absolute_mode(psmouse
)) {
637 printk(KERN_ERR
"Unable to initialize Synaptics hardware.\n");
641 priv
->pkt_type
= SYN_MODEL_NEWABS(priv
->model_id
) ? SYN_NEWABS
: SYN_OLDABS
;
643 printk(KERN_INFO
"Synaptics Touchpad, model: %ld, fw: %ld.%ld, id: %#lx, caps: %#lx/%#lx\n",
644 SYN_ID_MODEL(priv
->identity
),
645 SYN_ID_MAJOR(priv
->identity
), SYN_ID_MINOR(priv
->identity
),
646 priv
->model_id
, priv
->capabilities
, priv
->ext_cap
);
648 set_input_params(&psmouse
->dev
, priv
);
650 psmouse
->protocol_handler
= synaptics_process_byte
;
651 psmouse
->set_rate
= synaptics_set_rate
;
652 psmouse
->disconnect
= synaptics_disconnect
;
653 psmouse
->reconnect
= synaptics_reconnect
;
654 psmouse
->pktsize
= 6;
656 if (SYN_CAP_PASS_THROUGH(priv
->capabilities
))
657 synaptics_pt_create(psmouse
);
659 #if defined(__i386__)
661 * Toshiba's KBC seems to have trouble handling data from
662 * Synaptics as full rate, switch to lower rate which is roughly
663 * thye same as rate of standard PS/2 mouse.
665 if (psmouse
->rate
>= 80 && dmi_check_system(toshiba_dmi_table
)) {
666 printk(KERN_INFO
"synaptics: Toshiba %s detected, limiting rate to 40pps.\n",
667 dmi_get_system_info(DMI_PRODUCT_NAME
));