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>
23 #include <linux/mutex.h>
26 #include "synaptics.h"
27 #include "logips2pp.h"
30 #include "trackpoint.h"
31 #include "touchkit_ps2.h"
33 #define DRIVER_DESC "PS/2 mouse driver"
35 MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
36 MODULE_DESCRIPTION(DRIVER_DESC);
37 MODULE_LICENSE("GPL");
39 static unsigned int psmouse_max_proto = PSMOUSE_AUTO;
40 static int psmouse_set_maxproto(const char *val, struct kernel_param *kp);
41 static int psmouse_get_maxproto(char *buffer, struct kernel_param *kp);
42 #define param_check_proto_abbrev(name, p) __param_check(name, p, unsigned int)
43 #define param_set_proto_abbrev psmouse_set_maxproto
44 #define param_get_proto_abbrev psmouse_get_maxproto
45 module_param_named(proto, psmouse_max_proto, proto_abbrev, 0644);
46 MODULE_PARM_DESC(proto, "Highest protocol extension to probe (bare, imps, exps, any). Useful for KVM switches.");
48 static unsigned int psmouse_resolution = 200;
49 module_param_named(resolution, psmouse_resolution, uint, 0644);
50 MODULE_PARM_DESC(resolution, "Resolution, in dpi.");
52 static unsigned int psmouse_rate = 100;
53 module_param_named(rate, psmouse_rate, uint, 0644);
54 MODULE_PARM_DESC(rate, "Report rate, in reports per second.");
56 static unsigned int psmouse_smartscroll = 1;
57 module_param_named(smartscroll, psmouse_smartscroll, bool, 0644);
58 MODULE_PARM_DESC(smartscroll, "Logitech Smartscroll autorepeat, 1 = enabled (default), 0 = disabled.");
60 static unsigned int psmouse_resetafter = 5;
61 module_param_named(resetafter, psmouse_resetafter, uint, 0644);
62 MODULE_PARM_DESC(resetafter, "Reset device after so many bad packets (0 = never).");
64 static unsigned int psmouse_resync_time;
65 module_param_named(resync_time, psmouse_resync_time, uint, 0644);
66 MODULE_PARM_DESC(resync_time, "How long can mouse stay idle before forcing resync (in seconds, 0 = never).");
68 PSMOUSE_DEFINE_ATTR(protocol, S_IWUSR | S_IRUGO,
70 psmouse_attr_show_protocol, psmouse_attr_set_protocol);
71 PSMOUSE_DEFINE_ATTR(rate, S_IWUSR | S_IRUGO,
72 (void *) offsetof(struct psmouse, rate),
73 psmouse_show_int_attr, psmouse_attr_set_rate);
74 PSMOUSE_DEFINE_ATTR(resolution, S_IWUSR | S_IRUGO,
75 (void *) offsetof(struct psmouse, resolution),
76 psmouse_show_int_attr, psmouse_attr_set_resolution);
77 PSMOUSE_DEFINE_ATTR(resetafter, S_IWUSR | S_IRUGO,
78 (void *) offsetof(struct psmouse, resetafter),
79 psmouse_show_int_attr, psmouse_set_int_attr);
80 PSMOUSE_DEFINE_ATTR(resync_time, S_IWUSR | S_IRUGO,
81 (void *) offsetof(struct psmouse, resync_time),
82 psmouse_show_int_attr, psmouse_set_int_attr);
84 static struct attribute *psmouse_attributes[] = {
85 &psmouse_attr_protocol.dattr.attr,
86 &psmouse_attr_rate.dattr.attr,
87 &psmouse_attr_resolution.dattr.attr,
88 &psmouse_attr_resetafter.dattr.attr,
89 &psmouse_attr_resync_time.dattr.attr,
93 static struct attribute_group psmouse_attribute_group = {
94 .attrs = psmouse_attributes,
98 * psmouse_mutex protects all operations changing state of mouse
99 * (connecting, disconnecting, changing rate or resolution via
100 * sysfs). We could use a per-device semaphore but since there
101 * rarely more than one PS/2 mouse connected and since semaphore
102 * is taken in "slow" paths it is not worth it.
104 static DEFINE_MUTEX(psmouse_mutex);
106 static struct workqueue_struct *kpsmoused_wq;
108 struct psmouse_protocol {
109 enum psmouse_type type;
113 int (*detect)(struct psmouse *, int);
114 int (*init)(struct psmouse *);
118 * psmouse_process_byte() analyzes the PS/2 data stream and reports
119 * relevant events to the input module once full packet has arrived.
122 static psmouse_ret_t psmouse_process_byte(struct psmouse *psmouse)
124 struct input_dev *dev = psmouse->dev;
125 unsigned char *packet = psmouse->packet;
127 if (psmouse->pktcnt < psmouse->pktsize)
128 return PSMOUSE_GOOD_DATA;
131 * Full packet accumulated, process it
135 * Scroll wheel on IntelliMice, scroll buttons on NetMice
138 if (psmouse->type == PSMOUSE_IMPS || psmouse->type == PSMOUSE_GENPS)
139 input_report_rel(dev, REL_WHEEL, -(signed char) packet[3]);
142 * Scroll wheel and buttons on IntelliMouse Explorer
145 if (psmouse->type == PSMOUSE_IMEX) {
146 switch (packet[3] & 0xC0) {
147 case 0x80: /* vertical scroll on IntelliMouse Explorer 4.0 */
148 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31));
150 case 0x40: /* horizontal scroll on IntelliMouse Explorer 4.0 */
151 input_report_rel(dev, REL_HWHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31));
155 input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 8) - (int) (packet[3] & 7));
156 input_report_key(dev, BTN_SIDE, (packet[3] >> 4) & 1);
157 input_report_key(dev, BTN_EXTRA, (packet[3] >> 5) & 1);
163 * Extra buttons on Genius NewNet 3D
166 if (psmouse->type == PSMOUSE_GENPS) {
167 input_report_key(dev, BTN_SIDE, (packet[0] >> 6) & 1);
168 input_report_key(dev, BTN_EXTRA, (packet[0] >> 7) & 1);
172 * Extra button on ThinkingMouse
174 if (psmouse->type == PSMOUSE_THINKPS) {
175 input_report_key(dev, BTN_EXTRA, (packet[0] >> 3) & 1);
176 /* Without this bit of weirdness moving up gives wildly high Y changes. */
177 packet[1] |= (packet[0] & 0x40) << 1;
181 * Cortron PS2 Trackball reports SIDE button on the 4th bit of the first
184 if (psmouse->type == PSMOUSE_CORTRON) {
185 input_report_key(dev, BTN_SIDE, (packet[0] >> 3) & 1);
193 input_report_key(dev, BTN_LEFT, packet[0] & 1);
194 input_report_key(dev, BTN_MIDDLE, (packet[0] >> 2) & 1);
195 input_report_key(dev, BTN_RIGHT, (packet[0] >> 1) & 1);
197 input_report_rel(dev, REL_X, packet[1] ? (int) packet[1] - (int) ((packet[0] << 4) & 0x100) : 0);
198 input_report_rel(dev, REL_Y, packet[2] ? (int) ((packet[0] << 3) & 0x100) - (int) packet[2] : 0);
202 return PSMOUSE_FULL_PACKET;
206 * __psmouse_set_state() sets new psmouse state and resets all flags.
209 static inline void __psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
211 psmouse->state = new_state;
212 psmouse->pktcnt = psmouse->out_of_sync = 0;
213 psmouse->ps2dev.flags = 0;
214 psmouse->last = jiffies;
219 * psmouse_set_state() sets new psmouse state and resets all flags and
220 * counters while holding serio lock so fighting with interrupt handler
224 static void psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
226 serio_pause_rx(psmouse->ps2dev.serio);
227 __psmouse_set_state(psmouse, new_state);
228 serio_continue_rx(psmouse->ps2dev.serio);
232 * psmouse_handle_byte() processes one byte of the input data stream
233 * by calling corresponding protocol handler.
236 static int psmouse_handle_byte(struct psmouse *psmouse)
238 psmouse_ret_t rc = psmouse->protocol_handler(psmouse);
241 case PSMOUSE_BAD_DATA:
242 if (psmouse->state == PSMOUSE_ACTIVATED) {
243 printk(KERN_WARNING "psmouse.c: %s at %s lost sync at byte %d\n",
244 psmouse->name, psmouse->phys, psmouse->pktcnt);
245 if (++psmouse->out_of_sync == psmouse->resetafter) {
246 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
247 printk(KERN_NOTICE "psmouse.c: issuing reconnect request\n");
248 serio_reconnect(psmouse->ps2dev.serio);
255 case PSMOUSE_FULL_PACKET:
257 if (psmouse->out_of_sync) {
258 psmouse->out_of_sync = 0;
259 printk(KERN_NOTICE "psmouse.c: %s at %s - driver resynched.\n",
260 psmouse->name, psmouse->phys);
264 case PSMOUSE_GOOD_DATA:
271 * psmouse_interrupt() handles incoming characters, either passing them
272 * for normal processing or gathering them as command response.
275 static irqreturn_t psmouse_interrupt(struct serio *serio,
276 unsigned char data, unsigned int flags)
278 struct psmouse *psmouse = serio_get_drvdata(serio);
280 if (psmouse->state == PSMOUSE_IGNORE)
283 if (flags & (SERIO_PARITY|SERIO_TIMEOUT)) {
284 if (psmouse->state == PSMOUSE_ACTIVATED)
285 printk(KERN_WARNING "psmouse.c: bad data from KBC -%s%s\n",
286 flags & SERIO_TIMEOUT ? " timeout" : "",
287 flags & SERIO_PARITY ? " bad parity" : "");
288 ps2_cmd_aborted(&psmouse->ps2dev);
292 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_ACK))
293 if (ps2_handle_ack(&psmouse->ps2dev, data))
296 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_CMD))
297 if (ps2_handle_response(&psmouse->ps2dev, data))
300 if (psmouse->state <= PSMOUSE_RESYNCING)
303 if (psmouse->state == PSMOUSE_ACTIVATED &&
304 psmouse->pktcnt && time_after(jiffies, psmouse->last + HZ/2)) {
305 printk(KERN_INFO "psmouse.c: %s at %s lost synchronization, throwing %d bytes away.\n",
306 psmouse->name, psmouse->phys, psmouse->pktcnt);
307 psmouse->badbyte = psmouse->packet[0];
308 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
309 queue_work(kpsmoused_wq, &psmouse->resync_work);
313 psmouse->packet[psmouse->pktcnt++] = data;
315 * Check if this is a new device announcement (0xAA 0x00)
317 if (unlikely(psmouse->packet[0] == PSMOUSE_RET_BAT && psmouse->pktcnt <= 2)) {
318 if (psmouse->pktcnt == 1) {
319 psmouse->last = jiffies;
323 if (psmouse->packet[1] == PSMOUSE_RET_ID) {
324 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
325 serio_reconnect(serio);
329 * Not a new device, try processing first byte normally
332 if (psmouse_handle_byte(psmouse))
335 psmouse->packet[psmouse->pktcnt++] = data;
339 * See if we need to force resync because mouse was idle for too long
341 if (psmouse->state == PSMOUSE_ACTIVATED &&
342 psmouse->pktcnt == 1 && psmouse->resync_time &&
343 time_after(jiffies, psmouse->last + psmouse->resync_time * HZ)) {
344 psmouse->badbyte = psmouse->packet[0];
345 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
346 queue_work(kpsmoused_wq, &psmouse->resync_work);
350 psmouse->last = jiffies;
351 psmouse_handle_byte(psmouse);
359 * psmouse_sliced_command() sends an extended PS/2 command to the mouse
360 * using sliced syntax, understood by advanced devices, such as Logitech
361 * or Synaptics touchpads. The command is encoded as:
362 * 0xE6 0xE8 rr 0xE8 ss 0xE8 tt 0xE8 uu where (rr*64)+(ss*16)+(tt*4)+uu
365 int psmouse_sliced_command(struct psmouse *psmouse, unsigned char command)
369 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11))
372 for (i = 6; i >= 0; i -= 2) {
373 unsigned char d = (command >> i) & 3;
374 if (ps2_command(&psmouse->ps2dev, &d, PSMOUSE_CMD_SETRES))
383 * psmouse_reset() resets the mouse into power-on state.
385 int psmouse_reset(struct psmouse *psmouse)
387 unsigned char param[2];
389 if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_RESET_BAT))
392 if (param[0] != PSMOUSE_RET_BAT && param[1] != PSMOUSE_RET_ID)
400 * Genius NetMouse magic init.
402 static int genius_detect(struct psmouse *psmouse, int set_properties)
404 struct ps2dev *ps2dev = &psmouse->ps2dev;
405 unsigned char param[4];
408 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
409 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
410 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
411 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
412 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO);
414 if (param[0] != 0x00 || param[1] != 0x33 || param[2] != 0x55)
417 if (set_properties) {
418 set_bit(BTN_EXTRA, psmouse->dev->keybit);
419 set_bit(BTN_SIDE, psmouse->dev->keybit);
420 set_bit(REL_WHEEL, psmouse->dev->relbit);
422 psmouse->vendor = "Genius";
423 psmouse->name = "Mouse";
424 psmouse->pktsize = 4;
431 * IntelliMouse magic init.
433 static int intellimouse_detect(struct psmouse *psmouse, int set_properties)
435 struct ps2dev *ps2dev = &psmouse->ps2dev;
436 unsigned char param[2];
439 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
441 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
443 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
444 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
449 if (set_properties) {
450 set_bit(BTN_MIDDLE, psmouse->dev->keybit);
451 set_bit(REL_WHEEL, psmouse->dev->relbit);
453 if (!psmouse->vendor) psmouse->vendor = "Generic";
454 if (!psmouse->name) psmouse->name = "Wheel Mouse";
455 psmouse->pktsize = 4;
462 * Try IntelliMouse/Explorer magic init.
464 static int im_explorer_detect(struct psmouse *psmouse, int set_properties)
466 struct ps2dev *ps2dev = &psmouse->ps2dev;
467 unsigned char param[2];
469 intellimouse_detect(psmouse, 0);
472 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
474 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
476 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
477 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
482 /* Magic to enable horizontal scrolling on IntelliMouse 4.0 */
484 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
486 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
488 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
490 if (set_properties) {
491 set_bit(BTN_MIDDLE, psmouse->dev->keybit);
492 set_bit(REL_WHEEL, psmouse->dev->relbit);
493 set_bit(REL_HWHEEL, psmouse->dev->relbit);
494 set_bit(BTN_SIDE, psmouse->dev->keybit);
495 set_bit(BTN_EXTRA, psmouse->dev->keybit);
497 if (!psmouse->vendor) psmouse->vendor = "Generic";
498 if (!psmouse->name) psmouse->name = "Explorer Mouse";
499 psmouse->pktsize = 4;
506 * Kensington ThinkingMouse / ExpertMouse magic init.
508 static int thinking_detect(struct psmouse *psmouse, int set_properties)
510 struct ps2dev *ps2dev = &psmouse->ps2dev;
511 unsigned char param[2];
512 static const unsigned char seq[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
516 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
518 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
519 for (i = 0; i < ARRAY_SIZE(seq); i++) {
521 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
523 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
528 if (set_properties) {
529 set_bit(BTN_EXTRA, psmouse->dev->keybit);
531 psmouse->vendor = "Kensington";
532 psmouse->name = "ThinkingMouse";
539 * Bare PS/2 protocol "detection". Always succeeds.
541 static int ps2bare_detect(struct psmouse *psmouse, int set_properties)
543 if (set_properties) {
544 if (!psmouse->vendor) psmouse->vendor = "Generic";
545 if (!psmouse->name) psmouse->name = "Mouse";
552 * Cortron PS/2 protocol detection. There's no special way to detect it, so it
553 * must be forced by sysfs protocol writing.
555 static int cortron_detect(struct psmouse *psmouse, int set_properties)
557 if (set_properties) {
558 psmouse->vendor = "Cortron";
559 psmouse->name = "PS/2 Trackball";
560 set_bit(BTN_SIDE, psmouse->dev->keybit);
567 * psmouse_extensions() probes for any extensions to the basic PS/2 protocol
568 * the mouse may have.
571 static int psmouse_extensions(struct psmouse *psmouse,
572 unsigned int max_proto, int set_properties)
574 int synaptics_hardware = 0;
577 * We always check for lifebook because it does not disturb mouse
578 * (it only checks DMI information).
580 if (lifebook_detect(psmouse, set_properties) == 0) {
581 if (max_proto > PSMOUSE_IMEX) {
582 if (!set_properties || lifebook_init(psmouse) == 0)
583 return PSMOUSE_LIFEBOOK;
588 * Try Kensington ThinkingMouse (we try first, because synaptics probe
589 * upsets the thinkingmouse).
592 if (max_proto > PSMOUSE_IMEX && thinking_detect(psmouse, set_properties) == 0)
593 return PSMOUSE_THINKPS;
596 * Try Synaptics TouchPad. Note that probing is done even if Synaptics protocol
597 * support is disabled in config - we need to know if it is synaptics so we
598 * can reset it properly after probing for intellimouse.
600 if (max_proto > PSMOUSE_PS2 && synaptics_detect(psmouse, set_properties) == 0) {
601 synaptics_hardware = 1;
603 if (max_proto > PSMOUSE_IMEX) {
604 if (!set_properties || synaptics_init(psmouse) == 0)
605 return PSMOUSE_SYNAPTICS;
607 * Some Synaptics touchpads can emulate extended protocols (like IMPS/2).
608 * Unfortunately Logitech/Genius probes confuse some firmware versions so
609 * we'll have to skip them.
611 max_proto = PSMOUSE_IMEX;
614 * Make sure that touchpad is in relative mode, gestures (taps) are enabled
616 synaptics_reset(psmouse);
622 if (max_proto > PSMOUSE_IMEX) {
623 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
624 if (alps_detect(psmouse, set_properties) == 0) {
625 if (!set_properties || alps_init(psmouse) == 0)
628 * Init failed, try basic relative protocols
630 max_proto = PSMOUSE_IMEX;
634 if (max_proto > PSMOUSE_IMEX) {
636 if (genius_detect(psmouse, set_properties) == 0)
637 return PSMOUSE_GENPS;
639 if (ps2pp_init(psmouse, set_properties) == 0)
640 return PSMOUSE_PS2PP;
642 if (trackpoint_detect(psmouse, set_properties) == 0)
643 return PSMOUSE_TRACKPOINT;
645 if (touchkit_ps2_detect(psmouse, set_properties) == 0)
646 return PSMOUSE_TOUCHKIT_PS2;
650 * Reset to defaults in case the device got confused by extended
651 * protocol probes. Note that we follow up with full reset because
652 * some mice put themselves to sleep when they see PSMOUSE_RESET_DIS.
654 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
655 psmouse_reset(psmouse);
657 if (max_proto >= PSMOUSE_IMEX && im_explorer_detect(psmouse, set_properties) == 0)
660 if (max_proto >= PSMOUSE_IMPS && intellimouse_detect(psmouse, set_properties) == 0)
664 * Okay, all failed, we have a standard mouse here. The number of the buttons
665 * is still a question, though. We assume 3.
667 ps2bare_detect(psmouse, set_properties);
669 if (synaptics_hardware) {
671 * We detected Synaptics hardware but it did not respond to IMPS/2 probes.
672 * We need to reset the touchpad because if there is a track point on the
673 * pass through port it could get disabled while probing for protocol
676 psmouse_reset(psmouse);
682 static const struct psmouse_protocol psmouse_protocols[] = {
688 .detect = ps2bare_detect,
690 #ifdef CONFIG_MOUSE_PS2_LOGIPS2PP
692 .type = PSMOUSE_PS2PP,
695 .detect = ps2pp_init,
699 .type = PSMOUSE_THINKPS,
702 .detect = thinking_detect,
705 .type = PSMOUSE_GENPS,
708 .detect = genius_detect,
711 .type = PSMOUSE_IMPS,
715 .detect = intellimouse_detect,
718 .type = PSMOUSE_IMEX,
722 .detect = im_explorer_detect,
724 #ifdef CONFIG_MOUSE_PS2_SYNAPTICS
726 .type = PSMOUSE_SYNAPTICS,
728 .alias = "synaptics",
729 .detect = synaptics_detect,
730 .init = synaptics_init,
733 #ifdef CONFIG_MOUSE_PS2_ALPS
735 .type = PSMOUSE_ALPS,
738 .detect = alps_detect,
742 #ifdef CONFIG_MOUSE_PS2_LIFEBOOK
744 .type = PSMOUSE_LIFEBOOK,
747 .init = lifebook_init,
750 #ifdef CONFIG_MOUSE_PS2_TRACKPOINT
752 .type = PSMOUSE_TRACKPOINT,
754 .alias = "trackpoint",
755 .detect = trackpoint_detect,
758 #ifdef CONFIG_MOUSE_PS2_TOUCHKIT
760 .type = PSMOUSE_TOUCHKIT_PS2,
761 .name = "touchkitPS/2",
763 .detect = touchkit_ps2_detect,
767 .type = PSMOUSE_CORTRON,
768 .name = "CortronPS/2",
770 .detect = cortron_detect,
773 .type = PSMOUSE_AUTO,
780 static const struct psmouse_protocol *psmouse_protocol_by_type(enum psmouse_type type)
784 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++)
785 if (psmouse_protocols[i].type == type)
786 return &psmouse_protocols[i];
789 return &psmouse_protocols[0];
792 static const struct psmouse_protocol *psmouse_protocol_by_name(const char *name, size_t len)
794 const struct psmouse_protocol *p;
797 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++) {
798 p = &psmouse_protocols[i];
800 if ((strlen(p->name) == len && !strncmp(p->name, name, len)) ||
801 (strlen(p->alias) == len && !strncmp(p->alias, name, len)))
802 return &psmouse_protocols[i];
810 * psmouse_probe() probes for a PS/2 mouse.
813 static int psmouse_probe(struct psmouse *psmouse)
815 struct ps2dev *ps2dev = &psmouse->ps2dev;
816 unsigned char param[2];
819 * First, we check if it's a mouse. It should send 0x00 or 0x03
820 * in case of an IntelliMouse in 4-byte mode or 0x04 for IM Explorer.
821 * Sunrex K8561 IR Keyboard/Mouse reports 0xff on second and subsequent
822 * ID queries, probably due to a firmware bug.
826 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
829 if (param[0] != 0x00 && param[0] != 0x03 &&
830 param[0] != 0x04 && param[0] != 0xff)
834 * Then we reset and disable the mouse so that it doesn't generate events.
837 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_DIS))
838 printk(KERN_WARNING "psmouse.c: Failed to reset mouse on %s\n", ps2dev->serio->phys);
844 * Here we set the mouse resolution.
847 void psmouse_set_resolution(struct psmouse *psmouse, unsigned int resolution)
849 static const unsigned char params[] = { 0, 1, 2, 2, 3 };
852 if (resolution == 0 || resolution > 200)
855 p = params[resolution / 50];
856 ps2_command(&psmouse->ps2dev, &p, PSMOUSE_CMD_SETRES);
857 psmouse->resolution = 25 << p;
861 * Here we set the mouse report rate.
864 static void psmouse_set_rate(struct psmouse *psmouse, unsigned int rate)
866 static const unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10, 0 };
870 while (rates[i] > rate) i++;
872 ps2_command(&psmouse->ps2dev, &r, PSMOUSE_CMD_SETRATE);
877 * psmouse_initialize() initializes the mouse to a sane state.
880 static void psmouse_initialize(struct psmouse *psmouse)
883 * We set the mouse report rate, resolution and scaling.
886 if (psmouse_max_proto != PSMOUSE_PS2) {
887 psmouse->set_rate(psmouse, psmouse->rate);
888 psmouse->set_resolution(psmouse, psmouse->resolution);
889 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
894 * psmouse_activate() enables the mouse so that we get motion reports from it.
897 static void psmouse_activate(struct psmouse *psmouse)
899 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE))
900 printk(KERN_WARNING "psmouse.c: Failed to enable mouse on %s\n",
901 psmouse->ps2dev.serio->phys);
903 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
908 * psmouse_deactivate() puts the mouse into poll mode so that we don't get motion
909 * reports from it unless we explicitly request it.
912 static void psmouse_deactivate(struct psmouse *psmouse)
914 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE))
915 printk(KERN_WARNING "psmouse.c: Failed to deactivate mouse on %s\n",
916 psmouse->ps2dev.serio->phys);
918 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
922 * psmouse_poll() - default poll hanlder. Everyone except for ALPS uses it.
925 static int psmouse_poll(struct psmouse *psmouse)
927 return ps2_command(&psmouse->ps2dev, psmouse->packet,
928 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8));
933 * psmouse_resync() attempts to re-validate current protocol.
936 static void psmouse_resync(struct work_struct *work)
938 struct psmouse *parent = NULL, *psmouse =
939 container_of(work, struct psmouse, resync_work);
940 struct serio *serio = psmouse->ps2dev.serio;
941 psmouse_ret_t rc = PSMOUSE_GOOD_DATA;
942 int failed = 0, enabled = 0;
945 mutex_lock(&psmouse_mutex);
947 if (psmouse->state != PSMOUSE_RESYNCING)
950 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
951 parent = serio_get_drvdata(serio->parent);
952 psmouse_deactivate(parent);
956 * Some mice don't ACK commands sent while they are in the middle of
957 * transmitting motion packet. To avoid delay we use ps2_sendbyte()
958 * instead of ps2_command() which would wait for 200ms for an ACK
959 * that may never come.
960 * As an additional quirk ALPS touchpads may not only forget to ACK
961 * disable command but will stop reporting taps, so if we see that
962 * mouse at least once ACKs disable we will do full reconnect if ACK
965 psmouse->num_resyncs++;
967 if (ps2_sendbyte(&psmouse->ps2dev, PSMOUSE_CMD_DISABLE, 20)) {
968 if (psmouse->num_resyncs < 3 || psmouse->acks_disable_command)
971 psmouse->acks_disable_command = 1;
974 * Poll the mouse. If it was reset the packet will be shorter than
975 * psmouse->pktsize and ps2_command will fail. We do not expect and
976 * do not handle scenario when mouse "upgrades" its protocol while
977 * disconnected since it would require additional delay. If we ever
978 * see a mouse that does it we'll adjust the code.
981 if (psmouse->poll(psmouse))
984 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
985 for (i = 0; i < psmouse->pktsize; i++) {
987 rc = psmouse->protocol_handler(psmouse);
988 if (rc != PSMOUSE_GOOD_DATA)
991 if (rc != PSMOUSE_FULL_PACKET)
993 psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
997 * Now try to enable mouse. We try to do that even if poll failed and also
998 * repeat our attempts 5 times, otherwise we may be left out with disabled
1001 for (i = 0; i < 5; i++) {
1002 if (!ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1010 printk(KERN_WARNING "psmouse.c: failed to re-enable mouse on %s\n",
1011 psmouse->ps2dev.serio->phys);
1016 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1017 printk(KERN_INFO "psmouse.c: resync failed, issuing reconnect request\n");
1018 serio_reconnect(serio);
1020 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
1023 psmouse_activate(parent);
1025 mutex_unlock(&psmouse_mutex);
1029 * psmouse_cleanup() resets the mouse into power-on state.
1032 static void psmouse_cleanup(struct serio *serio)
1034 struct psmouse *psmouse = serio_get_drvdata(serio);
1035 struct psmouse *parent = NULL;
1037 mutex_lock(&psmouse_mutex);
1039 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1040 parent = serio_get_drvdata(serio->parent);
1041 psmouse_deactivate(parent);
1044 psmouse_deactivate(psmouse);
1046 if (psmouse->cleanup)
1047 psmouse->cleanup(psmouse);
1049 psmouse_reset(psmouse);
1052 * Some boxes, such as HP nx7400, get terribly confused if mouse
1053 * is not fully enabled before suspending/shutting down.
1055 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE);
1058 if (parent->pt_deactivate)
1059 parent->pt_deactivate(parent);
1061 psmouse_activate(parent);
1064 mutex_unlock(&psmouse_mutex);
1068 * psmouse_disconnect() closes and frees.
1071 static void psmouse_disconnect(struct serio *serio)
1073 struct psmouse *psmouse, *parent = NULL;
1075 psmouse = serio_get_drvdata(serio);
1077 sysfs_remove_group(&serio->dev.kobj, &psmouse_attribute_group);
1079 mutex_lock(&psmouse_mutex);
1081 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1083 /* make sure we don't have a resync in progress */
1084 mutex_unlock(&psmouse_mutex);
1085 flush_workqueue(kpsmoused_wq);
1086 mutex_lock(&psmouse_mutex);
1088 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1089 parent = serio_get_drvdata(serio->parent);
1090 psmouse_deactivate(parent);
1093 if (psmouse->disconnect)
1094 psmouse->disconnect(psmouse);
1096 if (parent && parent->pt_deactivate)
1097 parent->pt_deactivate(parent);
1099 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1102 serio_set_drvdata(serio, NULL);
1103 input_unregister_device(psmouse->dev);
1107 psmouse_activate(parent);
1109 mutex_unlock(&psmouse_mutex);
1112 static int psmouse_switch_protocol(struct psmouse *psmouse, const struct psmouse_protocol *proto)
1114 struct input_dev *input_dev = psmouse->dev;
1116 input_dev->dev.parent = &psmouse->ps2dev.serio->dev;
1118 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
1119 input_dev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_LEFT) |
1120 BIT_MASK(BTN_MIDDLE) | BIT_MASK(BTN_RIGHT);
1121 input_dev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
1123 psmouse->set_rate = psmouse_set_rate;
1124 psmouse->set_resolution = psmouse_set_resolution;
1125 psmouse->poll = psmouse_poll;
1126 psmouse->protocol_handler = psmouse_process_byte;
1127 psmouse->pktsize = 3;
1129 if (proto && (proto->detect || proto->init)) {
1130 if (proto->detect && proto->detect(psmouse, 1) < 0)
1133 if (proto->init && proto->init(psmouse) < 0)
1136 psmouse->type = proto->type;
1139 psmouse->type = psmouse_extensions(psmouse, psmouse_max_proto, 1);
1142 * If mouse's packet size is 3 there is no point in polling the
1143 * device in hopes to detect protocol reset - we won't get less
1144 * than 3 bytes response anyhow.
1146 if (psmouse->pktsize == 3)
1147 psmouse->resync_time = 0;
1150 * Some smart KVMs fake response to POLL command returning just
1151 * 3 bytes and messing up our resync logic, so if initial poll
1152 * fails we won't try polling the device anymore. Hopefully
1153 * such KVM will maintain initially selected protocol.
1155 if (psmouse->resync_time && psmouse->poll(psmouse))
1156 psmouse->resync_time = 0;
1158 snprintf(psmouse->devname, sizeof(psmouse->devname), "%s %s %s",
1159 psmouse_protocol_by_type(psmouse->type)->name, psmouse->vendor, psmouse->name);
1161 input_dev->name = psmouse->devname;
1162 input_dev->phys = psmouse->phys;
1163 input_dev->id.bustype = BUS_I8042;
1164 input_dev->id.vendor = 0x0002;
1165 input_dev->id.product = psmouse->type;
1166 input_dev->id.version = psmouse->model;
1172 * psmouse_connect() is a callback from the serio module when
1173 * an unhandled serio port is found.
1175 static int psmouse_connect(struct serio *serio, struct serio_driver *drv)
1177 struct psmouse *psmouse, *parent = NULL;
1178 struct input_dev *input_dev;
1179 int retval = 0, error = -ENOMEM;
1181 mutex_lock(&psmouse_mutex);
1184 * If this is a pass-through port deactivate parent so the device
1185 * connected to this port can be successfully identified
1187 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1188 parent = serio_get_drvdata(serio->parent);
1189 psmouse_deactivate(parent);
1192 psmouse = kzalloc(sizeof(struct psmouse), GFP_KERNEL);
1193 input_dev = input_allocate_device();
1194 if (!psmouse || !input_dev)
1197 ps2_init(&psmouse->ps2dev, serio);
1198 INIT_WORK(&psmouse->resync_work, psmouse_resync);
1199 psmouse->dev = input_dev;
1200 snprintf(psmouse->phys, sizeof(psmouse->phys), "%s/input0", serio->phys);
1202 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1204 serio_set_drvdata(serio, psmouse);
1206 error = serio_open(serio, drv);
1208 goto err_clear_drvdata;
1210 if (psmouse_probe(psmouse) < 0) {
1212 goto err_close_serio;
1215 psmouse->rate = psmouse_rate;
1216 psmouse->resolution = psmouse_resolution;
1217 psmouse->resetafter = psmouse_resetafter;
1218 psmouse->resync_time = parent ? 0 : psmouse_resync_time;
1219 psmouse->smartscroll = psmouse_smartscroll;
1221 psmouse_switch_protocol(psmouse, NULL);
1223 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1224 psmouse_initialize(psmouse);
1226 error = input_register_device(psmouse->dev);
1228 goto err_protocol_disconnect;
1230 if (parent && parent->pt_activate)
1231 parent->pt_activate(parent);
1233 error = sysfs_create_group(&serio->dev.kobj, &psmouse_attribute_group);
1235 goto err_pt_deactivate;
1237 psmouse_activate(psmouse);
1240 /* If this is a pass-through port the parent needs to be re-activated */
1242 psmouse_activate(parent);
1244 mutex_unlock(&psmouse_mutex);
1248 if (parent && parent->pt_deactivate)
1249 parent->pt_deactivate(parent);
1250 input_unregister_device(psmouse->dev);
1251 input_dev = NULL; /* so we don't try to free it below */
1252 err_protocol_disconnect:
1253 if (psmouse->disconnect)
1254 psmouse->disconnect(psmouse);
1255 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1259 serio_set_drvdata(serio, NULL);
1261 input_free_device(input_dev);
1269 static int psmouse_reconnect(struct serio *serio)
1271 struct psmouse *psmouse = serio_get_drvdata(serio);
1272 struct psmouse *parent = NULL;
1273 struct serio_driver *drv = serio->drv;
1276 if (!drv || !psmouse) {
1277 printk(KERN_DEBUG "psmouse: reconnect request, but serio is disconnected, ignoring...\n");
1281 mutex_lock(&psmouse_mutex);
1283 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1284 parent = serio_get_drvdata(serio->parent);
1285 psmouse_deactivate(parent);
1288 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1290 if (psmouse->reconnect) {
1291 if (psmouse->reconnect(psmouse))
1293 } else if (psmouse_probe(psmouse) < 0 ||
1294 psmouse->type != psmouse_extensions(psmouse, psmouse_max_proto, 0))
1297 /* ok, the device type (and capabilities) match the old one,
1298 * we can continue using it, complete intialization
1300 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1302 psmouse_initialize(psmouse);
1304 if (parent && parent->pt_activate)
1305 parent->pt_activate(parent);
1307 psmouse_activate(psmouse);
1311 /* If this is a pass-through port the parent waits to be activated */
1313 psmouse_activate(parent);
1315 mutex_unlock(&psmouse_mutex);
1319 static struct serio_device_id psmouse_serio_ids[] = {
1327 .type = SERIO_PS_PSTHRU,
1335 MODULE_DEVICE_TABLE(serio, psmouse_serio_ids);
1337 static struct serio_driver psmouse_drv = {
1341 .description = DRIVER_DESC,
1342 .id_table = psmouse_serio_ids,
1343 .interrupt = psmouse_interrupt,
1344 .connect = psmouse_connect,
1345 .reconnect = psmouse_reconnect,
1346 .disconnect = psmouse_disconnect,
1347 .cleanup = psmouse_cleanup,
1350 ssize_t psmouse_attr_show_helper(struct device *dev, struct device_attribute *devattr,
1353 struct serio *serio = to_serio_port(dev);
1354 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1355 struct psmouse *psmouse;
1358 retval = serio_pin_driver(serio);
1362 if (serio->drv != &psmouse_drv) {
1367 psmouse = serio_get_drvdata(serio);
1369 retval = attr->show(psmouse, attr->data, buf);
1372 serio_unpin_driver(serio);
1376 ssize_t psmouse_attr_set_helper(struct device *dev, struct device_attribute *devattr,
1377 const char *buf, size_t count)
1379 struct serio *serio = to_serio_port(dev);
1380 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1381 struct psmouse *psmouse, *parent = NULL;
1384 retval = serio_pin_driver(serio);
1388 if (serio->drv != &psmouse_drv) {
1393 retval = mutex_lock_interruptible(&psmouse_mutex);
1397 psmouse = serio_get_drvdata(serio);
1399 if (psmouse->state == PSMOUSE_IGNORE) {
1404 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1405 parent = serio_get_drvdata(serio->parent);
1406 psmouse_deactivate(parent);
1409 psmouse_deactivate(psmouse);
1411 retval = attr->set(psmouse, attr->data, buf, count);
1413 if (retval != -ENODEV)
1414 psmouse_activate(psmouse);
1417 psmouse_activate(parent);
1420 mutex_unlock(&psmouse_mutex);
1422 serio_unpin_driver(serio);
1426 static ssize_t psmouse_show_int_attr(struct psmouse *psmouse, void *offset, char *buf)
1428 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
1430 return sprintf(buf, "%u\n", *field);
1433 static ssize_t psmouse_set_int_attr(struct psmouse *psmouse, void *offset, const char *buf, size_t count)
1435 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
1436 unsigned long value;
1439 value = simple_strtoul(buf, &rest, 10);
1443 if ((unsigned int)value != value)
1451 static ssize_t psmouse_attr_show_protocol(struct psmouse *psmouse, void *data, char *buf)
1453 return sprintf(buf, "%s\n", psmouse_protocol_by_type(psmouse->type)->name);
1456 static ssize_t psmouse_attr_set_protocol(struct psmouse *psmouse, void *data, const char *buf, size_t count)
1458 struct serio *serio = psmouse->ps2dev.serio;
1459 struct psmouse *parent = NULL;
1460 struct input_dev *old_dev, *new_dev;
1461 const struct psmouse_protocol *proto, *old_proto;
1465 proto = psmouse_protocol_by_name(buf, count);
1469 if (psmouse->type == proto->type)
1472 new_dev = input_allocate_device();
1476 while (serio->child) {
1478 printk(KERN_WARNING "psmouse: failed to destroy child port, protocol change aborted.\n");
1479 input_free_device(new_dev);
1483 mutex_unlock(&psmouse_mutex);
1484 serio_unpin_driver(serio);
1485 serio_unregister_child_port(serio);
1486 serio_pin_driver_uninterruptible(serio);
1487 mutex_lock(&psmouse_mutex);
1489 if (serio->drv != &psmouse_drv) {
1490 input_free_device(new_dev);
1494 if (psmouse->type == proto->type) {
1495 input_free_device(new_dev);
1496 return count; /* switched by other thread */
1500 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1501 parent = serio_get_drvdata(serio->parent);
1502 if (parent->pt_deactivate)
1503 parent->pt_deactivate(parent);
1506 old_dev = psmouse->dev;
1507 old_proto = psmouse_protocol_by_type(psmouse->type);
1509 if (psmouse->disconnect)
1510 psmouse->disconnect(psmouse);
1512 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1514 psmouse->dev = new_dev;
1515 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1517 if (psmouse_switch_protocol(psmouse, proto) < 0) {
1518 psmouse_reset(psmouse);
1519 /* default to PSMOUSE_PS2 */
1520 psmouse_switch_protocol(psmouse, &psmouse_protocols[0]);
1523 psmouse_initialize(psmouse);
1524 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1526 error = input_register_device(psmouse->dev);
1528 if (psmouse->disconnect)
1529 psmouse->disconnect(psmouse);
1531 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1532 input_free_device(new_dev);
1533 psmouse->dev = old_dev;
1534 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1535 psmouse_switch_protocol(psmouse, old_proto);
1536 psmouse_initialize(psmouse);
1537 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1542 input_unregister_device(old_dev);
1544 if (parent && parent->pt_activate)
1545 parent->pt_activate(parent);
1550 static ssize_t psmouse_attr_set_rate(struct psmouse *psmouse, void *data, const char *buf, size_t count)
1552 unsigned long value;
1555 value = simple_strtoul(buf, &rest, 10);
1559 psmouse->set_rate(psmouse, value);
1563 static ssize_t psmouse_attr_set_resolution(struct psmouse *psmouse, void *data, const char *buf, size_t count)
1565 unsigned long value;
1568 value = simple_strtoul(buf, &rest, 10);
1572 psmouse->set_resolution(psmouse, value);
1577 static int psmouse_set_maxproto(const char *val, struct kernel_param *kp)
1579 const struct psmouse_protocol *proto;
1584 proto = psmouse_protocol_by_name(val, strlen(val));
1586 if (!proto || !proto->maxproto)
1589 *((unsigned int *)kp->arg) = proto->type;
1594 static int psmouse_get_maxproto(char *buffer, struct kernel_param *kp)
1596 int type = *((unsigned int *)kp->arg);
1598 return sprintf(buffer, "%s\n", psmouse_protocol_by_type(type)->name);
1601 static int __init psmouse_init(void)
1605 kpsmoused_wq = create_singlethread_workqueue("kpsmoused");
1606 if (!kpsmoused_wq) {
1607 printk(KERN_ERR "psmouse: failed to create kpsmoused workqueue\n");
1611 err = serio_register_driver(&psmouse_drv);
1613 destroy_workqueue(kpsmoused_wq);
1618 static void __exit psmouse_exit(void)
1620 serio_unregister_driver(&psmouse_drv);
1621 destroy_workqueue(kpsmoused_wq);
1624 module_init(psmouse_init);
1625 module_exit(psmouse_exit);