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/slab.h>
17 #include <linux/interrupt.h>
18 #include <linux/input.h>
19 #include <linux/serio.h>
20 #include <linux/init.h>
21 #include <linux/libps2.h>
22 #include <linux/mutex.h>
25 #include "synaptics.h"
26 #include "logips2pp.h"
29 #include "trackpoint.h"
30 #include "touchkit_ps2.h"
32 #define DRIVER_DESC "PS/2 mouse driver"
34 MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
35 MODULE_DESCRIPTION(DRIVER_DESC);
36 MODULE_LICENSE("GPL");
38 static unsigned int psmouse_max_proto = PSMOUSE_AUTO;
39 static int psmouse_set_maxproto(const char *val, struct kernel_param *kp);
40 static int psmouse_get_maxproto(char *buffer, struct kernel_param *kp);
41 #define param_check_proto_abbrev(name, p) __param_check(name, p, unsigned int)
42 #define param_set_proto_abbrev psmouse_set_maxproto
43 #define param_get_proto_abbrev psmouse_get_maxproto
44 module_param_named(proto, psmouse_max_proto, proto_abbrev, 0644);
45 MODULE_PARM_DESC(proto, "Highest protocol extension to probe (bare, imps, exps, any). Useful for KVM switches.");
47 static unsigned int psmouse_resolution = 200;
48 module_param_named(resolution, psmouse_resolution, uint, 0644);
49 MODULE_PARM_DESC(resolution, "Resolution, in dpi.");
51 static unsigned int psmouse_rate = 100;
52 module_param_named(rate, psmouse_rate, uint, 0644);
53 MODULE_PARM_DESC(rate, "Report rate, in reports per second.");
55 static unsigned int psmouse_smartscroll = 1;
56 module_param_named(smartscroll, psmouse_smartscroll, bool, 0644);
57 MODULE_PARM_DESC(smartscroll, "Logitech Smartscroll autorepeat, 1 = enabled (default), 0 = disabled.");
59 static unsigned int psmouse_resetafter = 5;
60 module_param_named(resetafter, psmouse_resetafter, uint, 0644);
61 MODULE_PARM_DESC(resetafter, "Reset device after so many bad packets (0 = never).");
63 static unsigned int psmouse_resync_time;
64 module_param_named(resync_time, psmouse_resync_time, uint, 0644);
65 MODULE_PARM_DESC(resync_time, "How long can mouse stay idle before forcing resync (in seconds, 0 = never).");
67 PSMOUSE_DEFINE_ATTR(protocol, S_IWUSR | S_IRUGO,
69 psmouse_attr_show_protocol, psmouse_attr_set_protocol);
70 PSMOUSE_DEFINE_ATTR(rate, S_IWUSR | S_IRUGO,
71 (void *) offsetof(struct psmouse, rate),
72 psmouse_show_int_attr, psmouse_attr_set_rate);
73 PSMOUSE_DEFINE_ATTR(resolution, S_IWUSR | S_IRUGO,
74 (void *) offsetof(struct psmouse, resolution),
75 psmouse_show_int_attr, psmouse_attr_set_resolution);
76 PSMOUSE_DEFINE_ATTR(resetafter, S_IWUSR | S_IRUGO,
77 (void *) offsetof(struct psmouse, resetafter),
78 psmouse_show_int_attr, psmouse_set_int_attr);
79 PSMOUSE_DEFINE_ATTR(resync_time, S_IWUSR | S_IRUGO,
80 (void *) offsetof(struct psmouse, resync_time),
81 psmouse_show_int_attr, psmouse_set_int_attr);
83 static struct attribute *psmouse_attributes[] = {
84 &psmouse_attr_protocol.dattr.attr,
85 &psmouse_attr_rate.dattr.attr,
86 &psmouse_attr_resolution.dattr.attr,
87 &psmouse_attr_resetafter.dattr.attr,
88 &psmouse_attr_resync_time.dattr.attr,
92 static struct attribute_group psmouse_attribute_group = {
93 .attrs = psmouse_attributes,
97 * psmouse_mutex protects all operations changing state of mouse
98 * (connecting, disconnecting, changing rate or resolution via
99 * sysfs). We could use a per-device semaphore but since there
100 * rarely more than one PS/2 mouse connected and since semaphore
101 * is taken in "slow" paths it is not worth it.
103 static DEFINE_MUTEX(psmouse_mutex);
105 static struct workqueue_struct *kpsmoused_wq;
107 struct psmouse_protocol {
108 enum psmouse_type type;
112 int (*detect)(struct psmouse *, int);
113 int (*init)(struct psmouse *);
117 * psmouse_process_byte() analyzes the PS/2 data stream and reports
118 * relevant events to the input module once full packet has arrived.
121 static psmouse_ret_t psmouse_process_byte(struct psmouse *psmouse)
123 struct input_dev *dev = psmouse->dev;
124 unsigned char *packet = psmouse->packet;
126 if (psmouse->pktcnt < psmouse->pktsize)
127 return PSMOUSE_GOOD_DATA;
130 * Full packet accumulated, process it
134 * Scroll wheel on IntelliMice, scroll buttons on NetMice
137 if (psmouse->type == PSMOUSE_IMPS || psmouse->type == PSMOUSE_GENPS)
138 input_report_rel(dev, REL_WHEEL, -(signed char) packet[3]);
141 * Scroll wheel and buttons on IntelliMouse Explorer
144 if (psmouse->type == PSMOUSE_IMEX) {
145 switch (packet[3] & 0xC0) {
146 case 0x80: /* vertical scroll on IntelliMouse Explorer 4.0 */
147 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31));
149 case 0x40: /* horizontal scroll on IntelliMouse Explorer 4.0 */
150 input_report_rel(dev, REL_HWHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31));
154 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 8) - (int) (packet[3] & 7));
155 input_report_key(dev, BTN_SIDE, (packet[3] >> 4) & 1);
156 input_report_key(dev, BTN_EXTRA, (packet[3] >> 5) & 1);
162 * Extra buttons on Genius NewNet 3D
165 if (psmouse->type == PSMOUSE_GENPS) {
166 input_report_key(dev, BTN_SIDE, (packet[0] >> 6) & 1);
167 input_report_key(dev, BTN_EXTRA, (packet[0] >> 7) & 1);
171 * Extra button on ThinkingMouse
173 if (psmouse->type == PSMOUSE_THINKPS) {
174 input_report_key(dev, BTN_EXTRA, (packet[0] >> 3) & 1);
175 /* Without this bit of weirdness moving up gives wildly high Y changes. */
176 packet[1] |= (packet[0] & 0x40) << 1;
180 * Cortron PS2 Trackball reports SIDE button on the 4th bit of the first
183 if (psmouse->type == PSMOUSE_CORTRON) {
184 input_report_key(dev, BTN_SIDE, (packet[0] >> 3) & 1);
192 input_report_key(dev, BTN_LEFT, packet[0] & 1);
193 input_report_key(dev, BTN_MIDDLE, (packet[0] >> 2) & 1);
194 input_report_key(dev, BTN_RIGHT, (packet[0] >> 1) & 1);
196 input_report_rel(dev, REL_X, packet[1] ? (int) packet[1] - (int) ((packet[0] << 4) & 0x100) : 0);
197 input_report_rel(dev, REL_Y, packet[2] ? (int) ((packet[0] << 3) & 0x100) - (int) packet[2] : 0);
201 return PSMOUSE_FULL_PACKET;
205 * __psmouse_set_state() sets new psmouse state and resets all flags.
208 static inline void __psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
210 psmouse->state = new_state;
211 psmouse->pktcnt = psmouse->out_of_sync = 0;
212 psmouse->ps2dev.flags = 0;
213 psmouse->last = jiffies;
218 * psmouse_set_state() sets new psmouse state and resets all flags and
219 * counters while holding serio lock so fighting with interrupt handler
223 static void psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
225 serio_pause_rx(psmouse->ps2dev.serio);
226 __psmouse_set_state(psmouse, new_state);
227 serio_continue_rx(psmouse->ps2dev.serio);
231 * psmouse_handle_byte() processes one byte of the input data stream
232 * by calling corresponding protocol handler.
235 static int psmouse_handle_byte(struct psmouse *psmouse)
237 psmouse_ret_t rc = psmouse->protocol_handler(psmouse);
240 case PSMOUSE_BAD_DATA:
241 if (psmouse->state == PSMOUSE_ACTIVATED) {
242 printk(KERN_WARNING "psmouse.c: %s at %s lost sync at byte %d\n",
243 psmouse->name, psmouse->phys, psmouse->pktcnt);
244 if (++psmouse->out_of_sync == psmouse->resetafter) {
245 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
246 printk(KERN_NOTICE "psmouse.c: issuing reconnect request\n");
247 serio_reconnect(psmouse->ps2dev.serio);
254 case PSMOUSE_FULL_PACKET:
256 if (psmouse->out_of_sync) {
257 psmouse->out_of_sync = 0;
258 printk(KERN_NOTICE "psmouse.c: %s at %s - driver resynched.\n",
259 psmouse->name, psmouse->phys);
263 case PSMOUSE_GOOD_DATA:
270 * psmouse_interrupt() handles incoming characters, either passing them
271 * for normal processing or gathering them as command response.
274 static irqreturn_t psmouse_interrupt(struct serio *serio,
275 unsigned char data, unsigned int flags)
277 struct psmouse *psmouse = serio_get_drvdata(serio);
279 if (psmouse->state == PSMOUSE_IGNORE)
282 if (flags & (SERIO_PARITY|SERIO_TIMEOUT)) {
283 if (psmouse->state == PSMOUSE_ACTIVATED)
284 printk(KERN_WARNING "psmouse.c: bad data from KBC -%s%s\n",
285 flags & SERIO_TIMEOUT ? " timeout" : "",
286 flags & SERIO_PARITY ? " bad parity" : "");
287 ps2_cmd_aborted(&psmouse->ps2dev);
291 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_ACK))
292 if (ps2_handle_ack(&psmouse->ps2dev, data))
295 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_CMD))
296 if (ps2_handle_response(&psmouse->ps2dev, data))
299 if (psmouse->state <= PSMOUSE_RESYNCING)
302 if (psmouse->state == PSMOUSE_ACTIVATED &&
303 psmouse->pktcnt && time_after(jiffies, psmouse->last + HZ/2)) {
304 printk(KERN_INFO "psmouse.c: %s at %s lost synchronization, throwing %d bytes away.\n",
305 psmouse->name, psmouse->phys, psmouse->pktcnt);
306 psmouse->badbyte = psmouse->packet[0];
307 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
308 queue_work(kpsmoused_wq, &psmouse->resync_work);
312 psmouse->packet[psmouse->pktcnt++] = data;
314 * Check if this is a new device announcement (0xAA 0x00)
316 if (unlikely(psmouse->packet[0] == PSMOUSE_RET_BAT && psmouse->pktcnt <= 2)) {
317 if (psmouse->pktcnt == 1) {
318 psmouse->last = jiffies;
322 if (psmouse->packet[1] == PSMOUSE_RET_ID) {
323 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
324 serio_reconnect(serio);
328 * Not a new device, try processing first byte normally
331 if (psmouse_handle_byte(psmouse))
334 psmouse->packet[psmouse->pktcnt++] = data;
338 * See if we need to force resync because mouse was idle for too long
340 if (psmouse->state == PSMOUSE_ACTIVATED &&
341 psmouse->pktcnt == 1 && psmouse->resync_time &&
342 time_after(jiffies, psmouse->last + psmouse->resync_time * HZ)) {
343 psmouse->badbyte = psmouse->packet[0];
344 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
345 queue_work(kpsmoused_wq, &psmouse->resync_work);
349 psmouse->last = jiffies;
350 psmouse_handle_byte(psmouse);
358 * psmouse_sliced_command() sends an extended PS/2 command to the mouse
359 * using sliced syntax, understood by advanced devices, such as Logitech
360 * or Synaptics touchpads. The command is encoded as:
361 * 0xE6 0xE8 rr 0xE8 ss 0xE8 tt 0xE8 uu where (rr*64)+(ss*16)+(tt*4)+uu
364 int psmouse_sliced_command(struct psmouse *psmouse, unsigned char command)
368 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11))
371 for (i = 6; i >= 0; i -= 2) {
372 unsigned char d = (command >> i) & 3;
373 if (ps2_command(&psmouse->ps2dev, &d, PSMOUSE_CMD_SETRES))
382 * psmouse_reset() resets the mouse into power-on state.
384 int psmouse_reset(struct psmouse *psmouse)
386 unsigned char param[2];
388 if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_RESET_BAT))
391 if (param[0] != PSMOUSE_RET_BAT && param[1] != PSMOUSE_RET_ID)
399 * Genius NetMouse magic init.
401 static int genius_detect(struct psmouse *psmouse, int set_properties)
403 struct ps2dev *ps2dev = &psmouse->ps2dev;
404 unsigned char param[4];
407 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
408 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
409 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
410 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
411 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO);
413 if (param[0] != 0x00 || param[1] != 0x33 || param[2] != 0x55)
416 if (set_properties) {
417 set_bit(BTN_EXTRA, psmouse->dev->keybit);
418 set_bit(BTN_SIDE, psmouse->dev->keybit);
419 set_bit(REL_WHEEL, psmouse->dev->relbit);
421 psmouse->vendor = "Genius";
422 psmouse->name = "Mouse";
423 psmouse->pktsize = 4;
430 * IntelliMouse magic init.
432 static int intellimouse_detect(struct psmouse *psmouse, int set_properties)
434 struct ps2dev *ps2dev = &psmouse->ps2dev;
435 unsigned char param[2];
438 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
440 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
442 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
443 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
448 if (set_properties) {
449 set_bit(BTN_MIDDLE, psmouse->dev->keybit);
450 set_bit(REL_WHEEL, psmouse->dev->relbit);
452 if (!psmouse->vendor) psmouse->vendor = "Generic";
453 if (!psmouse->name) psmouse->name = "Wheel Mouse";
454 psmouse->pktsize = 4;
461 * Try IntelliMouse/Explorer magic init.
463 static int im_explorer_detect(struct psmouse *psmouse, int set_properties)
465 struct ps2dev *ps2dev = &psmouse->ps2dev;
466 unsigned char param[2];
468 intellimouse_detect(psmouse, 0);
471 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
473 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
475 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
476 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
481 /* Magic to enable horizontal scrolling on IntelliMouse 4.0 */
483 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
485 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
487 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
489 if (set_properties) {
490 set_bit(BTN_MIDDLE, psmouse->dev->keybit);
491 set_bit(REL_WHEEL, psmouse->dev->relbit);
492 set_bit(REL_HWHEEL, psmouse->dev->relbit);
493 set_bit(BTN_SIDE, psmouse->dev->keybit);
494 set_bit(BTN_EXTRA, psmouse->dev->keybit);
496 if (!psmouse->vendor) psmouse->vendor = "Generic";
497 if (!psmouse->name) psmouse->name = "Explorer Mouse";
498 psmouse->pktsize = 4;
505 * Kensington ThinkingMouse / ExpertMouse magic init.
507 static int thinking_detect(struct psmouse *psmouse, int set_properties)
509 struct ps2dev *ps2dev = &psmouse->ps2dev;
510 unsigned char param[2];
511 static const unsigned char seq[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
515 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
517 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
518 for (i = 0; i < ARRAY_SIZE(seq); i++) {
520 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
522 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
527 if (set_properties) {
528 set_bit(BTN_EXTRA, psmouse->dev->keybit);
530 psmouse->vendor = "Kensington";
531 psmouse->name = "ThinkingMouse";
538 * Bare PS/2 protocol "detection". Always succeeds.
540 static int ps2bare_detect(struct psmouse *psmouse, int set_properties)
542 if (set_properties) {
543 if (!psmouse->vendor) psmouse->vendor = "Generic";
544 if (!psmouse->name) psmouse->name = "Mouse";
551 * Cortron PS/2 protocol detection. There's no special way to detect it, so it
552 * must be forced by sysfs protocol writing.
554 static int cortron_detect(struct psmouse *psmouse, int set_properties)
556 if (set_properties) {
557 psmouse->vendor = "Cortron";
558 psmouse->name = "PS/2 Trackball";
559 set_bit(BTN_SIDE, psmouse->dev->keybit);
566 * psmouse_extensions() probes for any extensions to the basic PS/2 protocol
567 * the mouse may have.
570 static int psmouse_extensions(struct psmouse *psmouse,
571 unsigned int max_proto, int set_properties)
573 int synaptics_hardware = 0;
576 * We always check for lifebook because it does not disturb mouse
577 * (it only checks DMI information).
579 if (lifebook_detect(psmouse, set_properties) == 0) {
580 if (max_proto > PSMOUSE_IMEX) {
581 if (!set_properties || lifebook_init(psmouse) == 0)
582 return PSMOUSE_LIFEBOOK;
587 * Try Kensington ThinkingMouse (we try first, because synaptics probe
588 * upsets the thinkingmouse).
591 if (max_proto > PSMOUSE_IMEX && thinking_detect(psmouse, set_properties) == 0)
592 return PSMOUSE_THINKPS;
595 * Try Synaptics TouchPad. Note that probing is done even if Synaptics protocol
596 * support is disabled in config - we need to know if it is synaptics so we
597 * can reset it properly after probing for intellimouse.
599 if (max_proto > PSMOUSE_PS2 && synaptics_detect(psmouse, set_properties) == 0) {
600 synaptics_hardware = 1;
602 if (max_proto > PSMOUSE_IMEX) {
603 if (!set_properties || synaptics_init(psmouse) == 0)
604 return PSMOUSE_SYNAPTICS;
606 * Some Synaptics touchpads can emulate extended protocols (like IMPS/2).
607 * Unfortunately Logitech/Genius probes confuse some firmware versions so
608 * we'll have to skip them.
610 max_proto = PSMOUSE_IMEX;
613 * Make sure that touchpad is in relative mode, gestures (taps) are enabled
615 synaptics_reset(psmouse);
621 if (max_proto > PSMOUSE_IMEX) {
622 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
623 if (alps_detect(psmouse, set_properties) == 0) {
624 if (!set_properties || alps_init(psmouse) == 0)
627 * Init failed, try basic relative protocols
629 max_proto = PSMOUSE_IMEX;
633 if (max_proto > PSMOUSE_IMEX) {
635 if (genius_detect(psmouse, set_properties) == 0)
636 return PSMOUSE_GENPS;
638 if (ps2pp_init(psmouse, set_properties) == 0)
639 return PSMOUSE_PS2PP;
641 if (trackpoint_detect(psmouse, set_properties) == 0)
642 return PSMOUSE_TRACKPOINT;
644 if (touchkit_ps2_detect(psmouse, set_properties) == 0)
645 return PSMOUSE_TOUCHKIT_PS2;
649 * Reset to defaults in case the device got confused by extended
650 * protocol probes. Note that we follow up with full reset because
651 * some mice put themselves to sleep when they see PSMOUSE_RESET_DIS.
653 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
654 psmouse_reset(psmouse);
656 if (max_proto >= PSMOUSE_IMEX && im_explorer_detect(psmouse, set_properties) == 0)
659 if (max_proto >= PSMOUSE_IMPS && intellimouse_detect(psmouse, set_properties) == 0)
663 * Okay, all failed, we have a standard mouse here. The number of the buttons
664 * is still a question, though. We assume 3.
666 ps2bare_detect(psmouse, set_properties);
668 if (synaptics_hardware) {
670 * We detected Synaptics hardware but it did not respond to IMPS/2 probes.
671 * We need to reset the touchpad because if there is a track point on the
672 * pass through port it could get disabled while probing for protocol
675 psmouse_reset(psmouse);
681 static const struct psmouse_protocol psmouse_protocols[] = {
687 .detect = ps2bare_detect,
689 #ifdef CONFIG_MOUSE_PS2_LOGIPS2PP
691 .type = PSMOUSE_PS2PP,
694 .detect = ps2pp_init,
698 .type = PSMOUSE_THINKPS,
701 .detect = thinking_detect,
704 .type = PSMOUSE_GENPS,
707 .detect = genius_detect,
710 .type = PSMOUSE_IMPS,
714 .detect = intellimouse_detect,
717 .type = PSMOUSE_IMEX,
721 .detect = im_explorer_detect,
723 #ifdef CONFIG_MOUSE_PS2_SYNAPTICS
725 .type = PSMOUSE_SYNAPTICS,
727 .alias = "synaptics",
728 .detect = synaptics_detect,
729 .init = synaptics_init,
732 #ifdef CONFIG_MOUSE_PS2_ALPS
734 .type = PSMOUSE_ALPS,
737 .detect = alps_detect,
741 #ifdef CONFIG_MOUSE_PS2_LIFEBOOK
743 .type = PSMOUSE_LIFEBOOK,
746 .init = lifebook_init,
749 #ifdef CONFIG_MOUSE_PS2_TRACKPOINT
751 .type = PSMOUSE_TRACKPOINT,
753 .alias = "trackpoint",
754 .detect = trackpoint_detect,
757 #ifdef CONFIG_MOUSE_PS2_TOUCHKIT
759 .type = PSMOUSE_TOUCHKIT_PS2,
760 .name = "touchkitPS/2",
762 .detect = touchkit_ps2_detect,
766 .type = PSMOUSE_CORTRON,
767 .name = "CortronPS/2",
769 .detect = cortron_detect,
772 .type = PSMOUSE_AUTO,
779 static const struct psmouse_protocol *psmouse_protocol_by_type(enum psmouse_type type)
783 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++)
784 if (psmouse_protocols[i].type == type)
785 return &psmouse_protocols[i];
788 return &psmouse_protocols[0];
791 static const struct psmouse_protocol *psmouse_protocol_by_name(const char *name, size_t len)
793 const struct psmouse_protocol *p;
796 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++) {
797 p = &psmouse_protocols[i];
799 if ((strlen(p->name) == len && !strncmp(p->name, name, len)) ||
800 (strlen(p->alias) == len && !strncmp(p->alias, name, len)))
801 return &psmouse_protocols[i];
809 * psmouse_probe() probes for a PS/2 mouse.
812 static int psmouse_probe(struct psmouse *psmouse)
814 struct ps2dev *ps2dev = &psmouse->ps2dev;
815 unsigned char param[2];
818 * First, we check if it's a mouse. It should send 0x00 or 0x03
819 * in case of an IntelliMouse in 4-byte mode or 0x04 for IM Explorer.
820 * Sunrex K8561 IR Keyboard/Mouse reports 0xff on second and subsequent
821 * ID queries, probably due to a firmware bug.
825 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
828 if (param[0] != 0x00 && param[0] != 0x03 &&
829 param[0] != 0x04 && param[0] != 0xff)
833 * Then we reset and disable the mouse so that it doesn't generate events.
836 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_DIS))
837 printk(KERN_WARNING "psmouse.c: Failed to reset mouse on %s\n", ps2dev->serio->phys);
843 * Here we set the mouse resolution.
846 void psmouse_set_resolution(struct psmouse *psmouse, unsigned int resolution)
848 static const unsigned char params[] = { 0, 1, 2, 2, 3 };
851 if (resolution == 0 || resolution > 200)
854 p = params[resolution / 50];
855 ps2_command(&psmouse->ps2dev, &p, PSMOUSE_CMD_SETRES);
856 psmouse->resolution = 25 << p;
860 * Here we set the mouse report rate.
863 static void psmouse_set_rate(struct psmouse *psmouse, unsigned int rate)
865 static const unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10, 0 };
869 while (rates[i] > rate) i++;
871 ps2_command(&psmouse->ps2dev, &r, PSMOUSE_CMD_SETRATE);
876 * psmouse_initialize() initializes the mouse to a sane state.
879 static void psmouse_initialize(struct psmouse *psmouse)
882 * We set the mouse report rate, resolution and scaling.
885 if (psmouse_max_proto != PSMOUSE_PS2) {
886 psmouse->set_rate(psmouse, psmouse->rate);
887 psmouse->set_resolution(psmouse, psmouse->resolution);
888 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
893 * psmouse_activate() enables the mouse so that we get motion reports from it.
896 static void psmouse_activate(struct psmouse *psmouse)
898 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE))
899 printk(KERN_WARNING "psmouse.c: Failed to enable mouse on %s\n",
900 psmouse->ps2dev.serio->phys);
902 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
907 * psmouse_deactivate() puts the mouse into poll mode so that we don't get motion
908 * reports from it unless we explicitly request it.
911 static void psmouse_deactivate(struct psmouse *psmouse)
913 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE))
914 printk(KERN_WARNING "psmouse.c: Failed to deactivate mouse on %s\n",
915 psmouse->ps2dev.serio->phys);
917 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
921 * psmouse_poll() - default poll hanlder. Everyone except for ALPS uses it.
924 static int psmouse_poll(struct psmouse *psmouse)
926 return ps2_command(&psmouse->ps2dev, psmouse->packet,
927 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8));
932 * psmouse_resync() attempts to re-validate current protocol.
935 static void psmouse_resync(struct work_struct *work)
937 struct psmouse *parent = NULL, *psmouse =
938 container_of(work, struct psmouse, resync_work);
939 struct serio *serio = psmouse->ps2dev.serio;
940 psmouse_ret_t rc = PSMOUSE_GOOD_DATA;
941 int failed = 0, enabled = 0;
944 mutex_lock(&psmouse_mutex);
946 if (psmouse->state != PSMOUSE_RESYNCING)
949 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
950 parent = serio_get_drvdata(serio->parent);
951 psmouse_deactivate(parent);
955 * Some mice don't ACK commands sent while they are in the middle of
956 * transmitting motion packet. To avoid delay we use ps2_sendbyte()
957 * instead of ps2_command() which would wait for 200ms for an ACK
958 * that may never come.
959 * As an additional quirk ALPS touchpads may not only forget to ACK
960 * disable command but will stop reporting taps, so if we see that
961 * mouse at least once ACKs disable we will do full reconnect if ACK
964 psmouse->num_resyncs++;
966 if (ps2_sendbyte(&psmouse->ps2dev, PSMOUSE_CMD_DISABLE, 20)) {
967 if (psmouse->num_resyncs < 3 || psmouse->acks_disable_command)
970 psmouse->acks_disable_command = 1;
973 * Poll the mouse. If it was reset the packet will be shorter than
974 * psmouse->pktsize and ps2_command will fail. We do not expect and
975 * do not handle scenario when mouse "upgrades" its protocol while
976 * disconnected since it would require additional delay. If we ever
977 * see a mouse that does it we'll adjust the code.
980 if (psmouse->poll(psmouse))
983 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
984 for (i = 0; i < psmouse->pktsize; i++) {
986 rc = psmouse->protocol_handler(psmouse);
987 if (rc != PSMOUSE_GOOD_DATA)
990 if (rc != PSMOUSE_FULL_PACKET)
992 psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
996 * Now try to enable mouse. We try to do that even if poll failed and also
997 * repeat our attempts 5 times, otherwise we may be left out with disabled
1000 for (i = 0; i < 5; i++) {
1001 if (!ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1009 printk(KERN_WARNING "psmouse.c: failed to re-enable mouse on %s\n",
1010 psmouse->ps2dev.serio->phys);
1015 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1016 printk(KERN_INFO "psmouse.c: resync failed, issuing reconnect request\n");
1017 serio_reconnect(serio);
1019 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
1022 psmouse_activate(parent);
1024 mutex_unlock(&psmouse_mutex);
1028 * psmouse_cleanup() resets the mouse into power-on state.
1031 static void psmouse_cleanup(struct serio *serio)
1033 struct psmouse *psmouse = serio_get_drvdata(serio);
1034 struct psmouse *parent = NULL;
1036 mutex_lock(&psmouse_mutex);
1038 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1039 parent = serio_get_drvdata(serio->parent);
1040 psmouse_deactivate(parent);
1043 psmouse_deactivate(psmouse);
1045 if (psmouse->cleanup)
1046 psmouse->cleanup(psmouse);
1048 psmouse_reset(psmouse);
1051 * Some boxes, such as HP nx7400, get terribly confused if mouse
1052 * is not fully enabled before suspending/shutting down.
1054 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE);
1057 if (parent->pt_deactivate)
1058 parent->pt_deactivate(parent);
1060 psmouse_activate(parent);
1063 mutex_unlock(&psmouse_mutex);
1067 * psmouse_disconnect() closes and frees.
1070 static void psmouse_disconnect(struct serio *serio)
1072 struct psmouse *psmouse, *parent = NULL;
1074 psmouse = serio_get_drvdata(serio);
1076 sysfs_remove_group(&serio->dev.kobj, &psmouse_attribute_group);
1078 mutex_lock(&psmouse_mutex);
1080 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1082 /* make sure we don't have a resync in progress */
1083 mutex_unlock(&psmouse_mutex);
1084 flush_workqueue(kpsmoused_wq);
1085 mutex_lock(&psmouse_mutex);
1087 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1088 parent = serio_get_drvdata(serio->parent);
1089 psmouse_deactivate(parent);
1092 if (psmouse->disconnect)
1093 psmouse->disconnect(psmouse);
1095 if (parent && parent->pt_deactivate)
1096 parent->pt_deactivate(parent);
1098 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1101 serio_set_drvdata(serio, NULL);
1102 input_unregister_device(psmouse->dev);
1106 psmouse_activate(parent);
1108 mutex_unlock(&psmouse_mutex);
1111 static int psmouse_switch_protocol(struct psmouse *psmouse, const struct psmouse_protocol *proto)
1113 struct input_dev *input_dev = psmouse->dev;
1115 input_dev->dev.parent = &psmouse->ps2dev.serio->dev;
1117 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
1118 input_dev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_LEFT) |
1119 BIT_MASK(BTN_MIDDLE) | BIT_MASK(BTN_RIGHT);
1120 input_dev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
1122 psmouse->set_rate = psmouse_set_rate;
1123 psmouse->set_resolution = psmouse_set_resolution;
1124 psmouse->poll = psmouse_poll;
1125 psmouse->protocol_handler = psmouse_process_byte;
1126 psmouse->pktsize = 3;
1128 if (proto && (proto->detect || proto->init)) {
1129 if (proto->detect && proto->detect(psmouse, 1) < 0)
1132 if (proto->init && proto->init(psmouse) < 0)
1135 psmouse->type = proto->type;
1138 psmouse->type = psmouse_extensions(psmouse, psmouse_max_proto, 1);
1141 * If mouse's packet size is 3 there is no point in polling the
1142 * device in hopes to detect protocol reset - we won't get less
1143 * than 3 bytes response anyhow.
1145 if (psmouse->pktsize == 3)
1146 psmouse->resync_time = 0;
1149 * Some smart KVMs fake response to POLL command returning just
1150 * 3 bytes and messing up our resync logic, so if initial poll
1151 * fails we won't try polling the device anymore. Hopefully
1152 * such KVM will maintain initially selected protocol.
1154 if (psmouse->resync_time && psmouse->poll(psmouse))
1155 psmouse->resync_time = 0;
1157 snprintf(psmouse->devname, sizeof(psmouse->devname), "%s %s %s",
1158 psmouse_protocol_by_type(psmouse->type)->name, psmouse->vendor, psmouse->name);
1160 input_dev->name = psmouse->devname;
1161 input_dev->phys = psmouse->phys;
1162 input_dev->id.bustype = BUS_I8042;
1163 input_dev->id.vendor = 0x0002;
1164 input_dev->id.product = psmouse->type;
1165 input_dev->id.version = psmouse->model;
1171 * psmouse_connect() is a callback from the serio module when
1172 * an unhandled serio port is found.
1174 static int psmouse_connect(struct serio *serio, struct serio_driver *drv)
1176 struct psmouse *psmouse, *parent = NULL;
1177 struct input_dev *input_dev;
1178 int retval = 0, error = -ENOMEM;
1180 mutex_lock(&psmouse_mutex);
1183 * If this is a pass-through port deactivate parent so the device
1184 * connected to this port can be successfully identified
1186 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1187 parent = serio_get_drvdata(serio->parent);
1188 psmouse_deactivate(parent);
1191 psmouse = kzalloc(sizeof(struct psmouse), GFP_KERNEL);
1192 input_dev = input_allocate_device();
1193 if (!psmouse || !input_dev)
1196 ps2_init(&psmouse->ps2dev, serio);
1197 INIT_WORK(&psmouse->resync_work, psmouse_resync);
1198 psmouse->dev = input_dev;
1199 snprintf(psmouse->phys, sizeof(psmouse->phys), "%s/input0", serio->phys);
1201 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1203 serio_set_drvdata(serio, psmouse);
1205 error = serio_open(serio, drv);
1207 goto err_clear_drvdata;
1209 if (psmouse_probe(psmouse) < 0) {
1211 goto err_close_serio;
1214 psmouse->rate = psmouse_rate;
1215 psmouse->resolution = psmouse_resolution;
1216 psmouse->resetafter = psmouse_resetafter;
1217 psmouse->resync_time = parent ? 0 : psmouse_resync_time;
1218 psmouse->smartscroll = psmouse_smartscroll;
1220 psmouse_switch_protocol(psmouse, NULL);
1222 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1223 psmouse_initialize(psmouse);
1225 error = input_register_device(psmouse->dev);
1227 goto err_protocol_disconnect;
1229 if (parent && parent->pt_activate)
1230 parent->pt_activate(parent);
1232 error = sysfs_create_group(&serio->dev.kobj, &psmouse_attribute_group);
1234 goto err_pt_deactivate;
1236 psmouse_activate(psmouse);
1239 /* If this is a pass-through port the parent needs to be re-activated */
1241 psmouse_activate(parent);
1243 mutex_unlock(&psmouse_mutex);
1247 if (parent && parent->pt_deactivate)
1248 parent->pt_deactivate(parent);
1249 input_unregister_device(psmouse->dev);
1250 input_dev = NULL; /* so we don't try to free it below */
1251 err_protocol_disconnect:
1252 if (psmouse->disconnect)
1253 psmouse->disconnect(psmouse);
1254 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1258 serio_set_drvdata(serio, NULL);
1260 input_free_device(input_dev);
1268 static int psmouse_reconnect(struct serio *serio)
1270 struct psmouse *psmouse = serio_get_drvdata(serio);
1271 struct psmouse *parent = NULL;
1272 struct serio_driver *drv = serio->drv;
1275 if (!drv || !psmouse) {
1276 printk(KERN_DEBUG "psmouse: reconnect request, but serio is disconnected, ignoring...\n");
1280 mutex_lock(&psmouse_mutex);
1282 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1283 parent = serio_get_drvdata(serio->parent);
1284 psmouse_deactivate(parent);
1287 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1289 if (psmouse->reconnect) {
1290 if (psmouse->reconnect(psmouse))
1292 } else if (psmouse_probe(psmouse) < 0 ||
1293 psmouse->type != psmouse_extensions(psmouse, psmouse_max_proto, 0))
1296 /* ok, the device type (and capabilities) match the old one,
1297 * we can continue using it, complete intialization
1299 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1301 psmouse_initialize(psmouse);
1303 if (parent && parent->pt_activate)
1304 parent->pt_activate(parent);
1306 psmouse_activate(psmouse);
1310 /* If this is a pass-through port the parent waits to be activated */
1312 psmouse_activate(parent);
1314 mutex_unlock(&psmouse_mutex);
1318 static struct serio_device_id psmouse_serio_ids[] = {
1326 .type = SERIO_PS_PSTHRU,
1334 MODULE_DEVICE_TABLE(serio, psmouse_serio_ids);
1336 static struct serio_driver psmouse_drv = {
1340 .description = DRIVER_DESC,
1341 .id_table = psmouse_serio_ids,
1342 .interrupt = psmouse_interrupt,
1343 .connect = psmouse_connect,
1344 .reconnect = psmouse_reconnect,
1345 .disconnect = psmouse_disconnect,
1346 .cleanup = psmouse_cleanup,
1349 ssize_t psmouse_attr_show_helper(struct device *dev, struct device_attribute *devattr,
1352 struct serio *serio = to_serio_port(dev);
1353 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1354 struct psmouse *psmouse;
1357 retval = serio_pin_driver(serio);
1361 if (serio->drv != &psmouse_drv) {
1366 psmouse = serio_get_drvdata(serio);
1368 retval = attr->show(psmouse, attr->data, buf);
1371 serio_unpin_driver(serio);
1375 ssize_t psmouse_attr_set_helper(struct device *dev, struct device_attribute *devattr,
1376 const char *buf, size_t count)
1378 struct serio *serio = to_serio_port(dev);
1379 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1380 struct psmouse *psmouse, *parent = NULL;
1383 retval = serio_pin_driver(serio);
1387 if (serio->drv != &psmouse_drv) {
1392 retval = mutex_lock_interruptible(&psmouse_mutex);
1396 psmouse = serio_get_drvdata(serio);
1398 if (psmouse->state == PSMOUSE_IGNORE) {
1403 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1404 parent = serio_get_drvdata(serio->parent);
1405 psmouse_deactivate(parent);
1408 psmouse_deactivate(psmouse);
1410 retval = attr->set(psmouse, attr->data, buf, count);
1412 if (retval != -ENODEV)
1413 psmouse_activate(psmouse);
1416 psmouse_activate(parent);
1419 mutex_unlock(&psmouse_mutex);
1421 serio_unpin_driver(serio);
1425 static ssize_t psmouse_show_int_attr(struct psmouse *psmouse, void *offset, char *buf)
1427 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
1429 return sprintf(buf, "%u\n", *field);
1432 static ssize_t psmouse_set_int_attr(struct psmouse *psmouse, void *offset, const char *buf, size_t count)
1434 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
1435 unsigned long value;
1438 value = simple_strtoul(buf, &rest, 10);
1442 if ((unsigned int)value != value)
1450 static ssize_t psmouse_attr_show_protocol(struct psmouse *psmouse, void *data, char *buf)
1452 return sprintf(buf, "%s\n", psmouse_protocol_by_type(psmouse->type)->name);
1455 static ssize_t psmouse_attr_set_protocol(struct psmouse *psmouse, void *data, const char *buf, size_t count)
1457 struct serio *serio = psmouse->ps2dev.serio;
1458 struct psmouse *parent = NULL;
1459 struct input_dev *old_dev, *new_dev;
1460 const struct psmouse_protocol *proto, *old_proto;
1464 proto = psmouse_protocol_by_name(buf, count);
1468 if (psmouse->type == proto->type)
1471 new_dev = input_allocate_device();
1475 while (serio->child) {
1477 printk(KERN_WARNING "psmouse: failed to destroy child port, protocol change aborted.\n");
1478 input_free_device(new_dev);
1482 mutex_unlock(&psmouse_mutex);
1483 serio_unpin_driver(serio);
1484 serio_unregister_child_port(serio);
1485 serio_pin_driver_uninterruptible(serio);
1486 mutex_lock(&psmouse_mutex);
1488 if (serio->drv != &psmouse_drv) {
1489 input_free_device(new_dev);
1493 if (psmouse->type == proto->type) {
1494 input_free_device(new_dev);
1495 return count; /* switched by other thread */
1499 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1500 parent = serio_get_drvdata(serio->parent);
1501 if (parent->pt_deactivate)
1502 parent->pt_deactivate(parent);
1505 old_dev = psmouse->dev;
1506 old_proto = psmouse_protocol_by_type(psmouse->type);
1508 if (psmouse->disconnect)
1509 psmouse->disconnect(psmouse);
1511 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1513 psmouse->dev = new_dev;
1514 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1516 if (psmouse_switch_protocol(psmouse, proto) < 0) {
1517 psmouse_reset(psmouse);
1518 /* default to PSMOUSE_PS2 */
1519 psmouse_switch_protocol(psmouse, &psmouse_protocols[0]);
1522 psmouse_initialize(psmouse);
1523 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1525 error = input_register_device(psmouse->dev);
1527 if (psmouse->disconnect)
1528 psmouse->disconnect(psmouse);
1530 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1531 input_free_device(new_dev);
1532 psmouse->dev = old_dev;
1533 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1534 psmouse_switch_protocol(psmouse, old_proto);
1535 psmouse_initialize(psmouse);
1536 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1541 input_unregister_device(old_dev);
1543 if (parent && parent->pt_activate)
1544 parent->pt_activate(parent);
1549 static ssize_t psmouse_attr_set_rate(struct psmouse *psmouse, void *data, const char *buf, size_t count)
1551 unsigned long value;
1554 value = simple_strtoul(buf, &rest, 10);
1558 psmouse->set_rate(psmouse, value);
1562 static ssize_t psmouse_attr_set_resolution(struct psmouse *psmouse, void *data, const char *buf, size_t count)
1564 unsigned long value;
1567 value = simple_strtoul(buf, &rest, 10);
1571 psmouse->set_resolution(psmouse, value);
1576 static int psmouse_set_maxproto(const char *val, struct kernel_param *kp)
1578 const struct psmouse_protocol *proto;
1583 proto = psmouse_protocol_by_name(val, strlen(val));
1585 if (!proto || !proto->maxproto)
1588 *((unsigned int *)kp->arg) = proto->type;
1593 static int psmouse_get_maxproto(char *buffer, struct kernel_param *kp)
1595 int type = *((unsigned int *)kp->arg);
1597 return sprintf(buffer, "%s\n", psmouse_protocol_by_type(type)->name);
1600 static int __init psmouse_init(void)
1604 kpsmoused_wq = create_singlethread_workqueue("kpsmoused");
1605 if (!kpsmoused_wq) {
1606 printk(KERN_ERR "psmouse: failed to create kpsmoused workqueue\n");
1610 err = serio_register_driver(&psmouse_drv);
1612 destroy_workqueue(kpsmoused_wq);
1617 static void __exit psmouse_exit(void)
1619 serio_unregister_driver(&psmouse_drv);
1620 destroy_workqueue(kpsmoused_wq);
1623 module_init(psmouse_init);
1624 module_exit(psmouse_exit);