4 * Copyright (c) 1999-2002 Vojtech Pavlik
5 * Copyright (c) 2003-2004 Dmitry Torokhov
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License version 2 as published by
11 * the Free Software Foundation.
14 #include <linux/delay.h>
15 #include <linux/module.h>
16 #include <linux/moduleparam.h>
17 #include <linux/slab.h>
18 #include <linux/interrupt.h>
19 #include <linux/input.h>
20 #include <linux/serio.h>
21 #include <linux/init.h>
22 #include <linux/libps2.h>
24 #include "synaptics.h"
25 #include "logips2pp.h"
28 #include "trackpoint.h"
30 #define DRIVER_DESC "PS/2 mouse driver"
32 MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
33 MODULE_DESCRIPTION(DRIVER_DESC
);
34 MODULE_LICENSE("GPL");
36 static unsigned int psmouse_max_proto
= PSMOUSE_AUTO
;
37 static int psmouse_set_maxproto(const char *val
, struct kernel_param
*kp
);
38 static int psmouse_get_maxproto(char *buffer
, struct kernel_param
*kp
);
39 #define param_check_proto_abbrev(name, p) __param_check(name, p, unsigned int)
40 #define param_set_proto_abbrev psmouse_set_maxproto
41 #define param_get_proto_abbrev psmouse_get_maxproto
42 module_param_named(proto
, psmouse_max_proto
, proto_abbrev
, 0644);
43 MODULE_PARM_DESC(proto
, "Highest protocol extension to probe (bare, imps, exps, any). Useful for KVM switches.");
45 static unsigned int psmouse_resolution
= 200;
46 module_param_named(resolution
, psmouse_resolution
, uint
, 0644);
47 MODULE_PARM_DESC(resolution
, "Resolution, in dpi.");
49 static unsigned int psmouse_rate
= 100;
50 module_param_named(rate
, psmouse_rate
, uint
, 0644);
51 MODULE_PARM_DESC(rate
, "Report rate, in reports per second.");
53 static unsigned int psmouse_smartscroll
= 1;
54 module_param_named(smartscroll
, psmouse_smartscroll
, bool, 0644);
55 MODULE_PARM_DESC(smartscroll
, "Logitech Smartscroll autorepeat, 1 = enabled (default), 0 = disabled.");
57 static unsigned int psmouse_resetafter
;
58 module_param_named(resetafter
, psmouse_resetafter
, uint
, 0644);
59 MODULE_PARM_DESC(resetafter
, "Reset device after so many bad packets (0 = never).");
61 PSMOUSE_DEFINE_ATTR(protocol
, S_IWUSR
| S_IRUGO
,
63 psmouse_attr_show_protocol
, psmouse_attr_set_protocol
);
64 PSMOUSE_DEFINE_ATTR(rate
, S_IWUSR
| S_IRUGO
,
65 (void *) offsetof(struct psmouse
, rate
),
66 psmouse_show_int_attr
, psmouse_attr_set_rate
);
67 PSMOUSE_DEFINE_ATTR(resolution
, S_IWUSR
| S_IRUGO
,
68 (void *) offsetof(struct psmouse
, resolution
),
69 psmouse_show_int_attr
, psmouse_attr_set_resolution
);
70 PSMOUSE_DEFINE_ATTR(resetafter
, S_IWUSR
| S_IRUGO
,
71 (void *) offsetof(struct psmouse
, resetafter
),
72 psmouse_show_int_attr
, psmouse_set_int_attr
);
74 static struct attribute
*psmouse_attributes
[] = {
75 &psmouse_attr_protocol
.dattr
.attr
,
76 &psmouse_attr_rate
.dattr
.attr
,
77 &psmouse_attr_resolution
.dattr
.attr
,
78 &psmouse_attr_resetafter
.dattr
.attr
,
82 static struct attribute_group psmouse_attribute_group
= {
83 .attrs
= psmouse_attributes
,
86 __obsolete_setup("psmouse_noext");
87 __obsolete_setup("psmouse_resolution=");
88 __obsolete_setup("psmouse_smartscroll=");
89 __obsolete_setup("psmouse_resetafter=");
90 __obsolete_setup("psmouse_rate=");
93 * psmouse_sem protects all operations changing state of mouse
94 * (connecting, disconnecting, changing rate or resolution via
95 * sysfs). We could use a per-device semaphore but since there
96 * rarely more than one PS/2 mouse connected and since semaphore
97 * is taken in "slow" paths it is not worth it.
99 static DECLARE_MUTEX(psmouse_sem
);
101 struct psmouse_protocol
{
102 enum psmouse_type type
;
106 int (*detect
)(struct psmouse
*, int);
107 int (*init
)(struct psmouse
*);
111 * psmouse_process_byte() analyzes the PS/2 data stream and reports
112 * relevant events to the input module once full packet has arrived.
115 static psmouse_ret_t
psmouse_process_byte(struct psmouse
*psmouse
, struct pt_regs
*regs
)
117 struct input_dev
*dev
= psmouse
->dev
;
118 unsigned char *packet
= psmouse
->packet
;
120 if (psmouse
->pktcnt
< psmouse
->pktsize
)
121 return PSMOUSE_GOOD_DATA
;
124 * Full packet accumulated, process it
127 input_regs(dev
, regs
);
130 * Scroll wheel on IntelliMice, scroll buttons on NetMice
133 if (psmouse
->type
== PSMOUSE_IMPS
|| psmouse
->type
== PSMOUSE_GENPS
)
134 input_report_rel(dev
, REL_WHEEL
, -(signed char) packet
[3]);
137 * Scroll wheel and buttons on IntelliMouse Explorer
140 if (psmouse
->type
== PSMOUSE_IMEX
) {
141 input_report_rel(dev
, REL_WHEEL
, (int) (packet
[3] & 8) - (int) (packet
[3] & 7));
142 input_report_key(dev
, BTN_SIDE
, (packet
[3] >> 4) & 1);
143 input_report_key(dev
, BTN_EXTRA
, (packet
[3] >> 5) & 1);
147 * Extra buttons on Genius NewNet 3D
150 if (psmouse
->type
== PSMOUSE_GENPS
) {
151 input_report_key(dev
, BTN_SIDE
, (packet
[0] >> 6) & 1);
152 input_report_key(dev
, BTN_EXTRA
, (packet
[0] >> 7) & 1);
156 * Extra button on ThinkingMouse
158 if (psmouse
->type
== PSMOUSE_THINKPS
) {
159 input_report_key(dev
, BTN_EXTRA
, (packet
[0] >> 3) & 1);
160 /* Without this bit of weirdness moving up gives wildly high Y changes. */
161 packet
[1] |= (packet
[0] & 0x40) << 1;
168 input_report_key(dev
, BTN_LEFT
, packet
[0] & 1);
169 input_report_key(dev
, BTN_MIDDLE
, (packet
[0] >> 2) & 1);
170 input_report_key(dev
, BTN_RIGHT
, (packet
[0] >> 1) & 1);
172 input_report_rel(dev
, REL_X
, packet
[1] ? (int) packet
[1] - (int) ((packet
[0] << 4) & 0x100) : 0);
173 input_report_rel(dev
, REL_Y
, packet
[2] ? (int) ((packet
[0] << 3) & 0x100) - (int) packet
[2] : 0);
177 return PSMOUSE_FULL_PACKET
;
181 * psmouse_interrupt() handles incoming characters, either gathering them into
182 * packets or passing them to the command routine as command output.
185 static irqreturn_t
psmouse_interrupt(struct serio
*serio
,
186 unsigned char data
, unsigned int flags
, struct pt_regs
*regs
)
188 struct psmouse
*psmouse
= serio_get_drvdata(serio
);
191 if (psmouse
->state
== PSMOUSE_IGNORE
)
194 if (flags
& (SERIO_PARITY
|SERIO_TIMEOUT
)) {
195 if (psmouse
->state
== PSMOUSE_ACTIVATED
)
196 printk(KERN_WARNING
"psmouse.c: bad data from KBC -%s%s\n",
197 flags
& SERIO_TIMEOUT
? " timeout" : "",
198 flags
& SERIO_PARITY
? " bad parity" : "");
199 ps2_cmd_aborted(&psmouse
->ps2dev
);
203 if (unlikely(psmouse
->ps2dev
.flags
& PS2_FLAG_ACK
))
204 if (ps2_handle_ack(&psmouse
->ps2dev
, data
))
207 if (unlikely(psmouse
->ps2dev
.flags
& PS2_FLAG_CMD
))
208 if (ps2_handle_response(&psmouse
->ps2dev
, data
))
211 if (psmouse
->state
== PSMOUSE_INITIALIZING
)
214 if (psmouse
->state
== PSMOUSE_ACTIVATED
&&
215 psmouse
->pktcnt
&& time_after(jiffies
, psmouse
->last
+ HZ
/2)) {
216 printk(KERN_WARNING
"psmouse.c: %s at %s lost synchronization, throwing %d bytes away.\n",
217 psmouse
->name
, psmouse
->phys
, psmouse
->pktcnt
);
221 psmouse
->last
= jiffies
;
222 psmouse
->packet
[psmouse
->pktcnt
++] = data
;
224 if (psmouse
->packet
[0] == PSMOUSE_RET_BAT
) {
225 if (psmouse
->pktcnt
== 1)
228 if (psmouse
->pktcnt
== 2) {
229 if (psmouse
->packet
[1] == PSMOUSE_RET_ID
) {
230 psmouse
->state
= PSMOUSE_IGNORE
;
231 serio_reconnect(serio
);
234 if (psmouse
->type
== PSMOUSE_SYNAPTICS
) {
235 /* neither 0xAA nor 0x00 are valid first bytes
236 * for a packet in absolute mode
244 rc
= psmouse
->protocol_handler(psmouse
, regs
);
247 case PSMOUSE_BAD_DATA
:
248 printk(KERN_WARNING
"psmouse.c: %s at %s lost sync at byte %d\n",
249 psmouse
->name
, psmouse
->phys
, psmouse
->pktcnt
);
252 if (++psmouse
->out_of_sync
== psmouse
->resetafter
) {
253 psmouse
->state
= PSMOUSE_IGNORE
;
254 printk(KERN_NOTICE
"psmouse.c: issuing reconnect request\n");
255 serio_reconnect(psmouse
->ps2dev
.serio
);
259 case PSMOUSE_FULL_PACKET
:
261 if (psmouse
->out_of_sync
) {
262 psmouse
->out_of_sync
= 0;
263 printk(KERN_NOTICE
"psmouse.c: %s at %s - driver resynched.\n",
264 psmouse
->name
, psmouse
->phys
);
268 case PSMOUSE_GOOD_DATA
:
277 * psmouse_sliced_command() sends an extended PS/2 command to the mouse
278 * using sliced syntax, understood by advanced devices, such as Logitech
279 * or Synaptics touchpads. The command is encoded as:
280 * 0xE6 0xE8 rr 0xE8 ss 0xE8 tt 0xE8 uu where (rr*64)+(ss*16)+(tt*4)+uu
283 int psmouse_sliced_command(struct psmouse
*psmouse
, unsigned char command
)
287 if (ps2_command(&psmouse
->ps2dev
, NULL
, PSMOUSE_CMD_SETSCALE11
))
290 for (i
= 6; i
>= 0; i
-= 2) {
291 unsigned char d
= (command
>> i
) & 3;
292 if (ps2_command(&psmouse
->ps2dev
, &d
, PSMOUSE_CMD_SETRES
))
301 * psmouse_reset() resets the mouse into power-on state.
303 int psmouse_reset(struct psmouse
*psmouse
)
305 unsigned char param
[2];
307 if (ps2_command(&psmouse
->ps2dev
, param
, PSMOUSE_CMD_RESET_BAT
))
310 if (param
[0] != PSMOUSE_RET_BAT
&& param
[1] != PSMOUSE_RET_ID
)
318 * Genius NetMouse magic init.
320 static int genius_detect(struct psmouse
*psmouse
, int set_properties
)
322 struct ps2dev
*ps2dev
= &psmouse
->ps2dev
;
323 unsigned char param
[4];
326 ps2_command(ps2dev
, param
, PSMOUSE_CMD_SETRES
);
327 ps2_command(ps2dev
, NULL
, PSMOUSE_CMD_SETSCALE11
);
328 ps2_command(ps2dev
, NULL
, PSMOUSE_CMD_SETSCALE11
);
329 ps2_command(ps2dev
, NULL
, PSMOUSE_CMD_SETSCALE11
);
330 ps2_command(ps2dev
, param
, PSMOUSE_CMD_GETINFO
);
332 if (param
[0] != 0x00 || param
[1] != 0x33 || param
[2] != 0x55)
335 if (set_properties
) {
336 set_bit(BTN_EXTRA
, psmouse
->dev
->keybit
);
337 set_bit(BTN_SIDE
, psmouse
->dev
->keybit
);
338 set_bit(REL_WHEEL
, psmouse
->dev
->relbit
);
340 psmouse
->vendor
= "Genius";
341 psmouse
->pktsize
= 4;
348 * IntelliMouse magic init.
350 static int intellimouse_detect(struct psmouse
*psmouse
, int set_properties
)
352 struct ps2dev
*ps2dev
= &psmouse
->ps2dev
;
353 unsigned char param
[2];
356 ps2_command(ps2dev
, param
, PSMOUSE_CMD_SETRATE
);
358 ps2_command(ps2dev
, param
, PSMOUSE_CMD_SETRATE
);
360 ps2_command(ps2dev
, param
, PSMOUSE_CMD_SETRATE
);
361 ps2_command(ps2dev
, param
, PSMOUSE_CMD_GETID
);
366 if (set_properties
) {
367 set_bit(BTN_MIDDLE
, psmouse
->dev
->keybit
);
368 set_bit(REL_WHEEL
, psmouse
->dev
->relbit
);
370 if (!psmouse
->vendor
) psmouse
->vendor
= "Generic";
371 if (!psmouse
->name
) psmouse
->name
= "Wheel Mouse";
372 psmouse
->pktsize
= 4;
379 * Try IntelliMouse/Explorer magic init.
381 static int im_explorer_detect(struct psmouse
*psmouse
, int set_properties
)
383 struct ps2dev
*ps2dev
= &psmouse
->ps2dev
;
384 unsigned char param
[2];
386 intellimouse_detect(psmouse
, 0);
389 ps2_command(ps2dev
, param
, PSMOUSE_CMD_SETRATE
);
391 ps2_command(ps2dev
, param
, PSMOUSE_CMD_SETRATE
);
393 ps2_command(ps2dev
, param
, PSMOUSE_CMD_SETRATE
);
394 ps2_command(ps2dev
, param
, PSMOUSE_CMD_GETID
);
399 if (set_properties
) {
400 set_bit(BTN_MIDDLE
, psmouse
->dev
->keybit
);
401 set_bit(REL_WHEEL
, psmouse
->dev
->relbit
);
402 set_bit(BTN_SIDE
, psmouse
->dev
->keybit
);
403 set_bit(BTN_EXTRA
, psmouse
->dev
->keybit
);
405 if (!psmouse
->vendor
) psmouse
->vendor
= "Generic";
406 if (!psmouse
->name
) psmouse
->name
= "Explorer Mouse";
407 psmouse
->pktsize
= 4;
414 * Kensington ThinkingMouse / ExpertMouse magic init.
416 static int thinking_detect(struct psmouse
*psmouse
, int set_properties
)
418 struct ps2dev
*ps2dev
= &psmouse
->ps2dev
;
419 unsigned char param
[2];
420 unsigned char seq
[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20, 0 };
424 ps2_command(ps2dev
, param
, PSMOUSE_CMD_SETRATE
);
426 ps2_command(ps2dev
, param
, PSMOUSE_CMD_SETRES
);
427 for (i
= 0; seq
[i
]; i
++)
428 ps2_command(ps2dev
, seq
+ i
, PSMOUSE_CMD_SETRATE
);
429 ps2_command(ps2dev
, param
, PSMOUSE_CMD_GETID
);
434 if (set_properties
) {
435 set_bit(BTN_EXTRA
, psmouse
->dev
->keybit
);
437 psmouse
->vendor
= "Kensington";
438 psmouse
->name
= "ThinkingMouse";
445 * Bare PS/2 protocol "detection". Always succeeds.
447 static int ps2bare_detect(struct psmouse
*psmouse
, int set_properties
)
449 if (set_properties
) {
450 if (!psmouse
->vendor
) psmouse
->vendor
= "Generic";
451 if (!psmouse
->name
) psmouse
->name
= "Mouse";
459 * psmouse_extensions() probes for any extensions to the basic PS/2 protocol
460 * the mouse may have.
463 static int psmouse_extensions(struct psmouse
*psmouse
,
464 unsigned int max_proto
, int set_properties
)
466 int synaptics_hardware
= 0;
469 * We always check for lifebook because it does not disturb mouse
470 * (it only checks DMI information).
472 if (lifebook_detect(psmouse
, set_properties
) == 0) {
473 if (max_proto
> PSMOUSE_IMEX
) {
474 if (!set_properties
|| lifebook_init(psmouse
) == 0)
475 return PSMOUSE_LIFEBOOK
;
480 * Try Kensington ThinkingMouse (we try first, because synaptics probe
481 * upsets the thinkingmouse).
484 if (max_proto
> PSMOUSE_IMEX
&& thinking_detect(psmouse
, set_properties
) == 0)
485 return PSMOUSE_THINKPS
;
488 * Try Synaptics TouchPad
490 if (max_proto
> PSMOUSE_PS2
&& synaptics_detect(psmouse
, set_properties
) == 0) {
491 synaptics_hardware
= 1;
493 if (max_proto
> PSMOUSE_IMEX
) {
494 if (!set_properties
|| synaptics_init(psmouse
) == 0)
495 return PSMOUSE_SYNAPTICS
;
497 * Some Synaptics touchpads can emulate extended protocols (like IMPS/2).
498 * Unfortunately Logitech/Genius probes confuse some firmware versions so
499 * we'll have to skip them.
501 max_proto
= PSMOUSE_IMEX
;
504 * Make sure that touchpad is in relative mode, gestures (taps) are enabled
506 synaptics_reset(psmouse
);
512 if (max_proto
> PSMOUSE_IMEX
) {
513 ps2_command(&psmouse
->ps2dev
, NULL
, PSMOUSE_CMD_RESET_DIS
);
514 if (alps_detect(psmouse
, set_properties
) == 0) {
515 if (!set_properties
|| alps_init(psmouse
) == 0)
518 * Init failed, try basic relative protocols
520 max_proto
= PSMOUSE_IMEX
;
524 if (max_proto
> PSMOUSE_IMEX
&& genius_detect(psmouse
, set_properties
) == 0)
525 return PSMOUSE_GENPS
;
527 if (max_proto
> PSMOUSE_IMEX
&& ps2pp_init(psmouse
, set_properties
) == 0)
528 return PSMOUSE_PS2PP
;
531 * Reset to defaults in case the device got confused by extended
534 ps2_command(&psmouse
->ps2dev
, NULL
, PSMOUSE_CMD_RESET_DIS
);
536 if (max_proto
>= PSMOUSE_IMEX
&& im_explorer_detect(psmouse
, set_properties
) == 0)
539 if (max_proto
>= PSMOUSE_IMPS
&& intellimouse_detect(psmouse
, set_properties
) == 0)
543 * Try to initialize the IBM TrackPoint
545 if (max_proto
> PSMOUSE_IMEX
&& trackpoint_detect(psmouse
, set_properties
) == 0)
546 return PSMOUSE_TRACKPOINT
;
549 * Okay, all failed, we have a standard mouse here. The number of the buttons
550 * is still a question, though. We assume 3.
552 ps2bare_detect(psmouse
, set_properties
);
554 if (synaptics_hardware
) {
556 * We detected Synaptics hardware but it did not respond to IMPS/2 probes.
557 * We need to reset the touchpad because if there is a track point on the
558 * pass through port it could get disabled while probing for protocol
561 psmouse_reset(psmouse
);
562 ps2_command(&psmouse
->ps2dev
, NULL
, PSMOUSE_CMD_RESET_DIS
);
568 static struct psmouse_protocol psmouse_protocols
[] = {
574 .detect
= ps2bare_detect
,
577 .type
= PSMOUSE_PS2PP
,
580 .detect
= ps2pp_init
,
583 .type
= PSMOUSE_THINKPS
,
586 .detect
= thinking_detect
,
589 .type
= PSMOUSE_GENPS
,
592 .detect
= genius_detect
,
595 .type
= PSMOUSE_IMPS
,
599 .detect
= intellimouse_detect
,
602 .type
= PSMOUSE_IMEX
,
606 .detect
= im_explorer_detect
,
609 .type
= PSMOUSE_SYNAPTICS
,
611 .alias
= "synaptics",
612 .detect
= synaptics_detect
,
613 .init
= synaptics_init
,
616 .type
= PSMOUSE_ALPS
,
619 .detect
= alps_detect
,
623 .type
= PSMOUSE_LIFEBOOK
,
626 .init
= lifebook_init
,
629 .type
= PSMOUSE_TRACKPOINT
,
631 .alias
= "trackpoint",
632 .detect
= trackpoint_detect
,
635 .type
= PSMOUSE_AUTO
,
642 static struct psmouse_protocol
*psmouse_protocol_by_type(enum psmouse_type type
)
646 for (i
= 0; i
< ARRAY_SIZE(psmouse_protocols
); i
++)
647 if (psmouse_protocols
[i
].type
== type
)
648 return &psmouse_protocols
[i
];
651 return &psmouse_protocols
[0];
654 static struct psmouse_protocol
*psmouse_protocol_by_name(const char *name
, size_t len
)
656 struct psmouse_protocol
*p
;
659 for (i
= 0; i
< ARRAY_SIZE(psmouse_protocols
); i
++) {
660 p
= &psmouse_protocols
[i
];
662 if ((strlen(p
->name
) == len
&& !strncmp(p
->name
, name
, len
)) ||
663 (strlen(p
->alias
) == len
&& !strncmp(p
->alias
, name
, len
)))
664 return &psmouse_protocols
[i
];
672 * psmouse_probe() probes for a PS/2 mouse.
675 static int psmouse_probe(struct psmouse
*psmouse
)
677 struct ps2dev
*ps2dev
= &psmouse
->ps2dev
;
678 unsigned char param
[2];
681 * First, we check if it's a mouse. It should send 0x00 or 0x03
682 * in case of an IntelliMouse in 4-byte mode or 0x04 for IM Explorer.
683 * Sunrex K8561 IR Keyboard/Mouse reports 0xff on second and subsequent
684 * ID queries, probably due to a firmware bug.
688 if (ps2_command(ps2dev
, param
, PSMOUSE_CMD_GETID
))
691 if (param
[0] != 0x00 && param
[0] != 0x03 &&
692 param
[0] != 0x04 && param
[0] != 0xff)
696 * Then we reset and disable the mouse so that it doesn't generate events.
699 if (ps2_command(ps2dev
, NULL
, PSMOUSE_CMD_RESET_DIS
))
700 printk(KERN_WARNING
"psmouse.c: Failed to reset mouse on %s\n", ps2dev
->serio
->phys
);
706 * Here we set the mouse resolution.
709 void psmouse_set_resolution(struct psmouse
*psmouse
, unsigned int resolution
)
711 unsigned char params
[] = { 0, 1, 2, 2, 3 };
713 if (resolution
== 0 || resolution
> 200)
716 ps2_command(&psmouse
->ps2dev
, ¶ms
[resolution
/ 50], PSMOUSE_CMD_SETRES
);
717 psmouse
->resolution
= 25 << params
[resolution
/ 50];
721 * Here we set the mouse report rate.
724 static void psmouse_set_rate(struct psmouse
*psmouse
, unsigned int rate
)
726 unsigned char rates
[] = { 200, 100, 80, 60, 40, 20, 10, 0 };
729 while (rates
[i
] > rate
) i
++;
730 ps2_command(&psmouse
->ps2dev
, &rates
[i
], PSMOUSE_CMD_SETRATE
);
731 psmouse
->rate
= rates
[i
];
735 * psmouse_initialize() initializes the mouse to a sane state.
738 static void psmouse_initialize(struct psmouse
*psmouse
)
741 * We set the mouse into streaming mode.
744 ps2_command(&psmouse
->ps2dev
, NULL
, PSMOUSE_CMD_SETSTREAM
);
747 * We set the mouse report rate, resolution and scaling.
750 if (psmouse_max_proto
!= PSMOUSE_PS2
) {
751 psmouse
->set_rate(psmouse
, psmouse
->rate
);
752 psmouse
->set_resolution(psmouse
, psmouse
->resolution
);
753 ps2_command(&psmouse
->ps2dev
, NULL
, PSMOUSE_CMD_SETSCALE11
);
758 * psmouse_set_state() sets new psmouse state and resets all flags and
759 * counters while holding serio lock so fighting with interrupt handler
763 static void psmouse_set_state(struct psmouse
*psmouse
, enum psmouse_state new_state
)
765 serio_pause_rx(psmouse
->ps2dev
.serio
);
766 psmouse
->state
= new_state
;
767 psmouse
->pktcnt
= psmouse
->out_of_sync
= 0;
768 psmouse
->ps2dev
.flags
= 0;
769 serio_continue_rx(psmouse
->ps2dev
.serio
);
773 * psmouse_activate() enables the mouse so that we get motion reports from it.
776 static void psmouse_activate(struct psmouse
*psmouse
)
778 if (ps2_command(&psmouse
->ps2dev
, NULL
, PSMOUSE_CMD_ENABLE
))
779 printk(KERN_WARNING
"psmouse.c: Failed to enable mouse on %s\n",
780 psmouse
->ps2dev
.serio
->phys
);
782 psmouse_set_state(psmouse
, PSMOUSE_ACTIVATED
);
787 * psmouse_deactivate() puts the mouse into poll mode so that we don't get motion
788 * reports from it unless we explicitely request it.
791 static void psmouse_deactivate(struct psmouse
*psmouse
)
793 if (ps2_command(&psmouse
->ps2dev
, NULL
, PSMOUSE_CMD_DISABLE
))
794 printk(KERN_WARNING
"psmouse.c: Failed to deactivate mouse on %s\n",
795 psmouse
->ps2dev
.serio
->phys
);
797 psmouse_set_state(psmouse
, PSMOUSE_CMD_MODE
);
802 * psmouse_cleanup() resets the mouse into power-on state.
805 static void psmouse_cleanup(struct serio
*serio
)
807 struct psmouse
*psmouse
= serio_get_drvdata(serio
);
809 psmouse_reset(psmouse
);
813 * psmouse_disconnect() closes and frees.
816 static void psmouse_disconnect(struct serio
*serio
)
818 struct psmouse
*psmouse
, *parent
= NULL
;
820 psmouse
= serio_get_drvdata(serio
);
822 sysfs_remove_group(&serio
->dev
.kobj
, &psmouse_attribute_group
);
826 psmouse_set_state(psmouse
, PSMOUSE_CMD_MODE
);
828 if (serio
->parent
&& serio
->id
.type
== SERIO_PS_PSTHRU
) {
829 parent
= serio_get_drvdata(serio
->parent
);
830 psmouse_deactivate(parent
);
833 if (psmouse
->disconnect
)
834 psmouse
->disconnect(psmouse
);
836 if (parent
&& parent
->pt_deactivate
)
837 parent
->pt_deactivate(parent
);
839 psmouse_set_state(psmouse
, PSMOUSE_IGNORE
);
842 serio_set_drvdata(serio
, NULL
);
843 input_unregister_device(psmouse
->dev
);
847 psmouse_activate(parent
);
852 static int psmouse_switch_protocol(struct psmouse
*psmouse
, struct psmouse_protocol
*proto
)
854 struct input_dev
*input_dev
= psmouse
->dev
;
856 input_dev
->private = psmouse
;
857 input_dev
->cdev
.dev
= &psmouse
->ps2dev
.serio
->dev
;
859 input_dev
->evbit
[0] = BIT(EV_KEY
) | BIT(EV_REL
);
860 input_dev
->keybit
[LONG(BTN_MOUSE
)] = BIT(BTN_LEFT
) | BIT(BTN_MIDDLE
) | BIT(BTN_RIGHT
);
861 input_dev
->relbit
[0] = BIT(REL_X
) | BIT(REL_Y
);
863 psmouse
->set_rate
= psmouse_set_rate
;
864 psmouse
->set_resolution
= psmouse_set_resolution
;
865 psmouse
->protocol_handler
= psmouse_process_byte
;
866 psmouse
->pktsize
= 3;
868 if (proto
&& (proto
->detect
|| proto
->init
)) {
869 if (proto
->detect
&& proto
->detect(psmouse
, 1) < 0)
872 if (proto
->init
&& proto
->init(psmouse
) < 0)
875 psmouse
->type
= proto
->type
;
878 psmouse
->type
= psmouse_extensions(psmouse
, psmouse_max_proto
, 1);
880 sprintf(psmouse
->devname
, "%s %s %s",
881 psmouse_protocol_by_type(psmouse
->type
)->name
, psmouse
->vendor
, psmouse
->name
);
883 input_dev
->name
= psmouse
->devname
;
884 input_dev
->phys
= psmouse
->phys
;
885 input_dev
->id
.bustype
= BUS_I8042
;
886 input_dev
->id
.vendor
= 0x0002;
887 input_dev
->id
.product
= psmouse
->type
;
888 input_dev
->id
.version
= psmouse
->model
;
894 * psmouse_connect() is a callback from the serio module when
895 * an unhandled serio port is found.
897 static int psmouse_connect(struct serio
*serio
, struct serio_driver
*drv
)
899 struct psmouse
*psmouse
, *parent
= NULL
;
900 struct input_dev
*input_dev
;
901 int retval
= -ENOMEM
;
906 * If this is a pass-through port deactivate parent so the device
907 * connected to this port can be successfully identified
909 if (serio
->parent
&& serio
->id
.type
== SERIO_PS_PSTHRU
) {
910 parent
= serio_get_drvdata(serio
->parent
);
911 psmouse_deactivate(parent
);
914 psmouse
= kzalloc(sizeof(struct psmouse
), GFP_KERNEL
);
915 input_dev
= input_allocate_device();
916 if (!psmouse
|| !input_dev
)
919 ps2_init(&psmouse
->ps2dev
, serio
);
920 psmouse
->dev
= input_dev
;
921 sprintf(psmouse
->phys
, "%s/input0", serio
->phys
);
923 psmouse_set_state(psmouse
, PSMOUSE_INITIALIZING
);
925 serio_set_drvdata(serio
, psmouse
);
927 retval
= serio_open(serio
, drv
);
931 if (psmouse_probe(psmouse
) < 0) {
937 psmouse
->rate
= psmouse_rate
;
938 psmouse
->resolution
= psmouse_resolution
;
939 psmouse
->resetafter
= psmouse_resetafter
;
940 psmouse
->smartscroll
= psmouse_smartscroll
;
942 psmouse_switch_protocol(psmouse
, NULL
);
944 psmouse_set_state(psmouse
, PSMOUSE_CMD_MODE
);
945 psmouse_initialize(psmouse
);
947 input_register_device(psmouse
->dev
);
949 if (parent
&& parent
->pt_activate
)
950 parent
->pt_activate(parent
);
952 sysfs_create_group(&serio
->dev
.kobj
, &psmouse_attribute_group
);
954 psmouse_activate(psmouse
);
960 serio_set_drvdata(serio
, NULL
);
961 input_free_device(input_dev
);
965 /* If this is a pass-through port the parent needs to be re-activated */
967 psmouse_activate(parent
);
974 static int psmouse_reconnect(struct serio
*serio
)
976 struct psmouse
*psmouse
= serio_get_drvdata(serio
);
977 struct psmouse
*parent
= NULL
;
978 struct serio_driver
*drv
= serio
->drv
;
981 if (!drv
|| !psmouse
) {
982 printk(KERN_DEBUG
"psmouse: reconnect request, but serio is disconnected, ignoring...\n");
988 if (serio
->parent
&& serio
->id
.type
== SERIO_PS_PSTHRU
) {
989 parent
= serio_get_drvdata(serio
->parent
);
990 psmouse_deactivate(parent
);
993 psmouse_set_state(psmouse
, PSMOUSE_INITIALIZING
);
995 if (psmouse
->reconnect
) {
996 if (psmouse
->reconnect(psmouse
))
998 } else if (psmouse_probe(psmouse
) < 0 ||
999 psmouse
->type
!= psmouse_extensions(psmouse
, psmouse_max_proto
, 0))
1002 /* ok, the device type (and capabilities) match the old one,
1003 * we can continue using it, complete intialization
1005 psmouse_set_state(psmouse
, PSMOUSE_CMD_MODE
);
1007 psmouse_initialize(psmouse
);
1009 if (parent
&& parent
->pt_activate
)
1010 parent
->pt_activate(parent
);
1012 psmouse_activate(psmouse
);
1016 /* If this is a pass-through port the parent waits to be activated */
1018 psmouse_activate(parent
);
1024 static struct serio_device_id psmouse_serio_ids
[] = {
1032 .type
= SERIO_PS_PSTHRU
,
1040 MODULE_DEVICE_TABLE(serio
, psmouse_serio_ids
);
1042 static struct serio_driver psmouse_drv
= {
1046 .description
= DRIVER_DESC
,
1047 .id_table
= psmouse_serio_ids
,
1048 .interrupt
= psmouse_interrupt
,
1049 .connect
= psmouse_connect
,
1050 .reconnect
= psmouse_reconnect
,
1051 .disconnect
= psmouse_disconnect
,
1052 .cleanup
= psmouse_cleanup
,
1055 ssize_t
psmouse_attr_show_helper(struct device
*dev
, struct device_attribute
*devattr
,
1058 struct serio
*serio
= to_serio_port(dev
);
1059 struct psmouse_attribute
*attr
= to_psmouse_attr(devattr
);
1060 struct psmouse
*psmouse
;
1063 retval
= serio_pin_driver(serio
);
1067 if (serio
->drv
!= &psmouse_drv
) {
1072 psmouse
= serio_get_drvdata(serio
);
1074 retval
= attr
->show(psmouse
, attr
->data
, buf
);
1077 serio_unpin_driver(serio
);
1081 ssize_t
psmouse_attr_set_helper(struct device
*dev
, struct device_attribute
*devattr
,
1082 const char *buf
, size_t count
)
1084 struct serio
*serio
= to_serio_port(dev
);
1085 struct psmouse_attribute
*attr
= to_psmouse_attr(devattr
);
1086 struct psmouse
*psmouse
, *parent
= NULL
;
1089 retval
= serio_pin_driver(serio
);
1093 if (serio
->drv
!= &psmouse_drv
) {
1098 retval
= down_interruptible(&psmouse_sem
);
1102 psmouse
= serio_get_drvdata(serio
);
1104 if (psmouse
->state
== PSMOUSE_IGNORE
) {
1109 if (serio
->parent
&& serio
->id
.type
== SERIO_PS_PSTHRU
) {
1110 parent
= serio_get_drvdata(serio
->parent
);
1111 psmouse_deactivate(parent
);
1114 psmouse_deactivate(psmouse
);
1116 retval
= attr
->set(psmouse
, attr
->data
, buf
, count
);
1118 if (retval
!= -ENODEV
)
1119 psmouse_activate(psmouse
);
1122 psmouse_activate(parent
);
1127 serio_unpin_driver(serio
);
1131 static ssize_t
psmouse_show_int_attr(struct psmouse
*psmouse
, void *offset
, char *buf
)
1133 unsigned long *field
= (unsigned long *)((char *)psmouse
+ (size_t)offset
);
1135 return sprintf(buf
, "%lu\n", *field
);
1138 static ssize_t
psmouse_set_int_attr(struct psmouse
*psmouse
, void *offset
, const char *buf
, size_t count
)
1140 unsigned long *field
= (unsigned long *)((char *)psmouse
+ (size_t)offset
);
1141 unsigned long value
;
1144 value
= simple_strtoul(buf
, &rest
, 10);
1153 static ssize_t
psmouse_attr_show_protocol(struct psmouse
*psmouse
, void *data
, char *buf
)
1155 return sprintf(buf
, "%s\n", psmouse_protocol_by_type(psmouse
->type
)->name
);
1158 static ssize_t
psmouse_attr_set_protocol(struct psmouse
*psmouse
, void *data
, const char *buf
, size_t count
)
1160 struct serio
*serio
= psmouse
->ps2dev
.serio
;
1161 struct psmouse
*parent
= NULL
;
1162 struct input_dev
*new_dev
;
1163 struct psmouse_protocol
*proto
;
1166 if (!(proto
= psmouse_protocol_by_name(buf
, count
)))
1169 if (psmouse
->type
== proto
->type
)
1172 if (!(new_dev
= input_allocate_device()))
1175 while (serio
->child
) {
1177 printk(KERN_WARNING
"psmouse: failed to destroy child port, protocol change aborted.\n");
1178 input_free_device(new_dev
);
1183 serio_unpin_driver(serio
);
1184 serio_unregister_child_port(serio
);
1185 serio_pin_driver_uninterruptible(serio
);
1188 if (serio
->drv
!= &psmouse_drv
) {
1189 input_free_device(new_dev
);
1193 if (psmouse
->type
== proto
->type
) {
1194 input_free_device(new_dev
);
1195 return count
; /* switched by other thread */
1199 if (serio
->parent
&& serio
->id
.type
== SERIO_PS_PSTHRU
) {
1200 parent
= serio_get_drvdata(serio
->parent
);
1201 if (parent
->pt_deactivate
)
1202 parent
->pt_deactivate(parent
);
1205 if (psmouse
->disconnect
)
1206 psmouse
->disconnect(psmouse
);
1208 psmouse_set_state(psmouse
, PSMOUSE_IGNORE
);
1209 input_unregister_device(psmouse
->dev
);
1211 psmouse
->dev
= new_dev
;
1212 psmouse_set_state(psmouse
, PSMOUSE_INITIALIZING
);
1214 if (psmouse_switch_protocol(psmouse
, proto
) < 0) {
1215 psmouse_reset(psmouse
);
1216 /* default to PSMOUSE_PS2 */
1217 psmouse_switch_protocol(psmouse
, &psmouse_protocols
[0]);
1220 psmouse_initialize(psmouse
);
1221 psmouse_set_state(psmouse
, PSMOUSE_CMD_MODE
);
1223 input_register_device(psmouse
->dev
);
1225 if (parent
&& parent
->pt_activate
)
1226 parent
->pt_activate(parent
);
1231 static ssize_t
psmouse_attr_set_rate(struct psmouse
*psmouse
, void *data
, const char *buf
, size_t count
)
1233 unsigned long value
;
1236 value
= simple_strtoul(buf
, &rest
, 10);
1240 psmouse
->set_rate(psmouse
, value
);
1244 static ssize_t
psmouse_attr_set_resolution(struct psmouse
*psmouse
, void *data
, const char *buf
, size_t count
)
1246 unsigned long value
;
1249 value
= simple_strtoul(buf
, &rest
, 10);
1253 psmouse
->set_resolution(psmouse
, value
);
1258 static int psmouse_set_maxproto(const char *val
, struct kernel_param
*kp
)
1260 struct psmouse_protocol
*proto
;
1265 proto
= psmouse_protocol_by_name(val
, strlen(val
));
1267 if (!proto
|| !proto
->maxproto
)
1270 *((unsigned int *)kp
->arg
) = proto
->type
;
1275 static int psmouse_get_maxproto(char *buffer
, struct kernel_param
*kp
)
1277 int type
= *((unsigned int *)kp
->arg
);
1279 return sprintf(buffer
, "%s\n", psmouse_protocol_by_type(type
)->name
);
1282 static int __init
psmouse_init(void)
1284 serio_register_driver(&psmouse_drv
);
1288 static void __exit
psmouse_exit(void)
1290 serio_unregister_driver(&psmouse_drv
);
1293 module_init(psmouse_init
);
1294 module_exit(psmouse_exit
);