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"
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;
205 void psmouse_queue_work(struct psmouse *psmouse, struct delayed_work *work,
208 queue_delayed_work(kpsmoused_wq, work, delay);
212 * __psmouse_set_state() sets new psmouse state and resets all flags.
215 static inline void __psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
217 psmouse->state = new_state;
218 psmouse->pktcnt = psmouse->out_of_sync = 0;
219 psmouse->ps2dev.flags = 0;
220 psmouse->last = jiffies;
225 * psmouse_set_state() sets new psmouse state and resets all flags and
226 * counters while holding serio lock so fighting with interrupt handler
230 void psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
232 serio_pause_rx(psmouse->ps2dev.serio);
233 __psmouse_set_state(psmouse, new_state);
234 serio_continue_rx(psmouse->ps2dev.serio);
238 * psmouse_handle_byte() processes one byte of the input data stream
239 * by calling corresponding protocol handler.
242 static int psmouse_handle_byte(struct psmouse *psmouse)
244 psmouse_ret_t rc = psmouse->protocol_handler(psmouse);
247 case PSMOUSE_BAD_DATA:
248 if (psmouse->state == PSMOUSE_ACTIVATED) {
249 printk(KERN_WARNING "psmouse.c: %s at %s lost sync at byte %d\n",
250 psmouse->name, psmouse->phys, psmouse->pktcnt);
251 if (++psmouse->out_of_sync == psmouse->resetafter) {
252 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
253 printk(KERN_NOTICE "psmouse.c: issuing reconnect request\n");
254 serio_reconnect(psmouse->ps2dev.serio);
261 case PSMOUSE_FULL_PACKET:
263 if (psmouse->out_of_sync) {
264 psmouse->out_of_sync = 0;
265 printk(KERN_NOTICE "psmouse.c: %s at %s - driver resynched.\n",
266 psmouse->name, psmouse->phys);
270 case PSMOUSE_GOOD_DATA:
277 * psmouse_interrupt() handles incoming characters, either passing them
278 * for normal processing or gathering them as command response.
281 static irqreturn_t psmouse_interrupt(struct serio *serio,
282 unsigned char data, unsigned int flags)
284 struct psmouse *psmouse = serio_get_drvdata(serio);
286 if (psmouse->state == PSMOUSE_IGNORE)
289 if (flags & (SERIO_PARITY|SERIO_TIMEOUT)) {
290 if (psmouse->state == PSMOUSE_ACTIVATED)
291 printk(KERN_WARNING "psmouse.c: bad data from KBC -%s%s\n",
292 flags & SERIO_TIMEOUT ? " timeout" : "",
293 flags & SERIO_PARITY ? " bad parity" : "");
294 ps2_cmd_aborted(&psmouse->ps2dev);
298 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_ACK))
299 if (ps2_handle_ack(&psmouse->ps2dev, data))
302 if (unlikely(psmouse->ps2dev.flags & PS2_FLAG_CMD))
303 if (ps2_handle_response(&psmouse->ps2dev, data))
306 if (psmouse->state <= PSMOUSE_RESYNCING)
309 if (psmouse->state == PSMOUSE_ACTIVATED &&
310 psmouse->pktcnt && time_after(jiffies, psmouse->last + HZ/2)) {
311 printk(KERN_INFO "psmouse.c: %s at %s lost synchronization, throwing %d bytes away.\n",
312 psmouse->name, psmouse->phys, psmouse->pktcnt);
313 psmouse->badbyte = psmouse->packet[0];
314 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
315 psmouse_queue_work(psmouse, &psmouse->resync_work, 0);
319 psmouse->packet[psmouse->pktcnt++] = data;
321 * Check if this is a new device announcement (0xAA 0x00)
323 if (unlikely(psmouse->packet[0] == PSMOUSE_RET_BAT && psmouse->pktcnt <= 2)) {
324 if (psmouse->pktcnt == 1) {
325 psmouse->last = jiffies;
329 if (psmouse->packet[1] == PSMOUSE_RET_ID) {
330 __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
331 serio_reconnect(serio);
335 * Not a new device, try processing first byte normally
338 if (psmouse_handle_byte(psmouse))
341 psmouse->packet[psmouse->pktcnt++] = data;
345 * See if we need to force resync because mouse was idle for too long
347 if (psmouse->state == PSMOUSE_ACTIVATED &&
348 psmouse->pktcnt == 1 && psmouse->resync_time &&
349 time_after(jiffies, psmouse->last + psmouse->resync_time * HZ)) {
350 psmouse->badbyte = psmouse->packet[0];
351 __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
352 psmouse_queue_work(psmouse, &psmouse->resync_work, 0);
356 psmouse->last = jiffies;
357 psmouse_handle_byte(psmouse);
365 * psmouse_sliced_command() sends an extended PS/2 command to the mouse
366 * using sliced syntax, understood by advanced devices, such as Logitech
367 * or Synaptics touchpads. The command is encoded as:
368 * 0xE6 0xE8 rr 0xE8 ss 0xE8 tt 0xE8 uu where (rr*64)+(ss*16)+(tt*4)+uu
371 int psmouse_sliced_command(struct psmouse *psmouse, unsigned char command)
375 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11))
378 for (i = 6; i >= 0; i -= 2) {
379 unsigned char d = (command >> i) & 3;
380 if (ps2_command(&psmouse->ps2dev, &d, PSMOUSE_CMD_SETRES))
389 * psmouse_reset() resets the mouse into power-on state.
391 int psmouse_reset(struct psmouse *psmouse)
393 unsigned char param[2];
395 if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_RESET_BAT))
398 if (param[0] != PSMOUSE_RET_BAT && param[1] != PSMOUSE_RET_ID)
406 * Genius NetMouse magic init.
408 static int genius_detect(struct psmouse *psmouse, int set_properties)
410 struct ps2dev *ps2dev = &psmouse->ps2dev;
411 unsigned char param[4];
414 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
415 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
416 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
417 ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
418 ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO);
420 if (param[0] != 0x00 || param[1] != 0x33 || param[2] != 0x55)
423 if (set_properties) {
424 set_bit(BTN_EXTRA, psmouse->dev->keybit);
425 set_bit(BTN_SIDE, psmouse->dev->keybit);
426 set_bit(REL_WHEEL, psmouse->dev->relbit);
428 psmouse->vendor = "Genius";
429 psmouse->name = "Mouse";
430 psmouse->pktsize = 4;
437 * IntelliMouse magic init.
439 static int intellimouse_detect(struct psmouse *psmouse, int set_properties)
441 struct ps2dev *ps2dev = &psmouse->ps2dev;
442 unsigned char param[2];
445 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
447 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
449 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
450 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
455 if (set_properties) {
456 set_bit(BTN_MIDDLE, psmouse->dev->keybit);
457 set_bit(REL_WHEEL, psmouse->dev->relbit);
459 if (!psmouse->vendor) psmouse->vendor = "Generic";
460 if (!psmouse->name) psmouse->name = "Wheel Mouse";
461 psmouse->pktsize = 4;
468 * Try IntelliMouse/Explorer magic init.
470 static int im_explorer_detect(struct psmouse *psmouse, int set_properties)
472 struct ps2dev *ps2dev = &psmouse->ps2dev;
473 unsigned char param[2];
475 intellimouse_detect(psmouse, 0);
478 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
480 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
482 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
483 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
488 /* Magic to enable horizontal scrolling on IntelliMouse 4.0 */
490 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
492 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
494 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
496 if (set_properties) {
497 set_bit(BTN_MIDDLE, psmouse->dev->keybit);
498 set_bit(REL_WHEEL, psmouse->dev->relbit);
499 set_bit(REL_HWHEEL, psmouse->dev->relbit);
500 set_bit(BTN_SIDE, psmouse->dev->keybit);
501 set_bit(BTN_EXTRA, psmouse->dev->keybit);
503 if (!psmouse->vendor) psmouse->vendor = "Generic";
504 if (!psmouse->name) psmouse->name = "Explorer Mouse";
505 psmouse->pktsize = 4;
512 * Kensington ThinkingMouse / ExpertMouse magic init.
514 static int thinking_detect(struct psmouse *psmouse, int set_properties)
516 struct ps2dev *ps2dev = &psmouse->ps2dev;
517 unsigned char param[2];
518 static const unsigned char seq[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
522 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
524 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
525 for (i = 0; i < ARRAY_SIZE(seq); i++) {
527 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
529 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
534 if (set_properties) {
535 set_bit(BTN_EXTRA, psmouse->dev->keybit);
537 psmouse->vendor = "Kensington";
538 psmouse->name = "ThinkingMouse";
545 * Bare PS/2 protocol "detection". Always succeeds.
547 static int ps2bare_detect(struct psmouse *psmouse, int set_properties)
549 if (set_properties) {
550 if (!psmouse->vendor) psmouse->vendor = "Generic";
551 if (!psmouse->name) psmouse->name = "Mouse";
558 * Cortron PS/2 protocol detection. There's no special way to detect it, so it
559 * must be forced by sysfs protocol writing.
561 static int cortron_detect(struct psmouse *psmouse, int set_properties)
563 if (set_properties) {
564 psmouse->vendor = "Cortron";
565 psmouse->name = "PS/2 Trackball";
566 set_bit(BTN_SIDE, psmouse->dev->keybit);
573 * psmouse_extensions() probes for any extensions to the basic PS/2 protocol
574 * the mouse may have.
577 static int psmouse_extensions(struct psmouse *psmouse,
578 unsigned int max_proto, int set_properties)
580 int synaptics_hardware = 0;
583 * We always check for lifebook because it does not disturb mouse
584 * (it only checks DMI information).
586 if (lifebook_detect(psmouse, set_properties) == 0) {
587 if (max_proto > PSMOUSE_IMEX) {
588 if (!set_properties || lifebook_init(psmouse) == 0)
589 return PSMOUSE_LIFEBOOK;
594 * Try Kensington ThinkingMouse (we try first, because synaptics probe
595 * upsets the thinkingmouse).
598 if (max_proto > PSMOUSE_IMEX && thinking_detect(psmouse, set_properties) == 0)
599 return PSMOUSE_THINKPS;
602 * Try Synaptics TouchPad. Note that probing is done even if Synaptics protocol
603 * support is disabled in config - we need to know if it is synaptics so we
604 * can reset it properly after probing for intellimouse.
606 if (max_proto > PSMOUSE_PS2 && synaptics_detect(psmouse, set_properties) == 0) {
607 synaptics_hardware = 1;
609 if (max_proto > PSMOUSE_IMEX) {
610 if (!set_properties || synaptics_init(psmouse) == 0)
611 return PSMOUSE_SYNAPTICS;
613 * Some Synaptics touchpads can emulate extended protocols (like IMPS/2).
614 * Unfortunately Logitech/Genius probes confuse some firmware versions so
615 * we'll have to skip them.
617 max_proto = PSMOUSE_IMEX;
620 * Make sure that touchpad is in relative mode, gestures (taps) are enabled
622 synaptics_reset(psmouse);
628 if (max_proto > PSMOUSE_IMEX) {
629 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
630 if (alps_detect(psmouse, set_properties) == 0) {
631 if (!set_properties || alps_init(psmouse) == 0)
634 * Init failed, try basic relative protocols
636 max_proto = PSMOUSE_IMEX;
641 * Try OLPC HGPK touchpad.
643 if (max_proto > PSMOUSE_IMEX &&
644 hgpk_detect(psmouse, set_properties) == 0) {
645 if (!set_properties || hgpk_init(psmouse) == 0)
648 * Init failed, try basic relative protocols
650 max_proto = PSMOUSE_IMEX;
653 if (max_proto > PSMOUSE_IMEX) {
654 if (genius_detect(psmouse, set_properties) == 0)
655 return PSMOUSE_GENPS;
657 if (ps2pp_init(psmouse, set_properties) == 0)
658 return PSMOUSE_PS2PP;
660 if (trackpoint_detect(psmouse, set_properties) == 0)
661 return PSMOUSE_TRACKPOINT;
663 if (touchkit_ps2_detect(psmouse, set_properties) == 0)
664 return PSMOUSE_TOUCHKIT_PS2;
668 * Reset to defaults in case the device got confused by extended
669 * protocol probes. Note that we follow up with full reset because
670 * some mice put themselves to sleep when they see PSMOUSE_RESET_DIS.
672 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
673 psmouse_reset(psmouse);
675 if (max_proto >= PSMOUSE_IMEX && im_explorer_detect(psmouse, set_properties) == 0)
678 if (max_proto >= PSMOUSE_IMPS && intellimouse_detect(psmouse, set_properties) == 0)
682 * Okay, all failed, we have a standard mouse here. The number of the buttons
683 * is still a question, though. We assume 3.
685 ps2bare_detect(psmouse, set_properties);
687 if (synaptics_hardware) {
689 * We detected Synaptics hardware but it did not respond to IMPS/2 probes.
690 * We need to reset the touchpad because if there is a track point on the
691 * pass through port it could get disabled while probing for protocol
694 psmouse_reset(psmouse);
700 static const struct psmouse_protocol psmouse_protocols[] = {
706 .detect = ps2bare_detect,
708 #ifdef CONFIG_MOUSE_PS2_LOGIPS2PP
710 .type = PSMOUSE_PS2PP,
713 .detect = ps2pp_init,
717 .type = PSMOUSE_THINKPS,
720 .detect = thinking_detect,
723 .type = PSMOUSE_GENPS,
726 .detect = genius_detect,
729 .type = PSMOUSE_IMPS,
733 .detect = intellimouse_detect,
736 .type = PSMOUSE_IMEX,
740 .detect = im_explorer_detect,
742 #ifdef CONFIG_MOUSE_PS2_SYNAPTICS
744 .type = PSMOUSE_SYNAPTICS,
746 .alias = "synaptics",
747 .detect = synaptics_detect,
748 .init = synaptics_init,
751 #ifdef CONFIG_MOUSE_PS2_ALPS
753 .type = PSMOUSE_ALPS,
756 .detect = alps_detect,
760 #ifdef CONFIG_MOUSE_PS2_LIFEBOOK
762 .type = PSMOUSE_LIFEBOOK,
765 .init = lifebook_init,
768 #ifdef CONFIG_MOUSE_PS2_TRACKPOINT
770 .type = PSMOUSE_TRACKPOINT,
772 .alias = "trackpoint",
773 .detect = trackpoint_detect,
776 #ifdef CONFIG_MOUSE_PS2_TOUCHKIT
778 .type = PSMOUSE_TOUCHKIT_PS2,
779 .name = "touchkitPS/2",
781 .detect = touchkit_ps2_detect,
784 #ifdef CONFIG_MOUSE_PS2_OLPC
786 .type = PSMOUSE_HGPK,
789 .detect = hgpk_detect,
793 .type = PSMOUSE_CORTRON,
794 .name = "CortronPS/2",
796 .detect = cortron_detect,
799 .type = PSMOUSE_AUTO,
806 static const struct psmouse_protocol *psmouse_protocol_by_type(enum psmouse_type type)
810 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++)
811 if (psmouse_protocols[i].type == type)
812 return &psmouse_protocols[i];
815 return &psmouse_protocols[0];
818 static const struct psmouse_protocol *psmouse_protocol_by_name(const char *name, size_t len)
820 const struct psmouse_protocol *p;
823 for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++) {
824 p = &psmouse_protocols[i];
826 if ((strlen(p->name) == len && !strncmp(p->name, name, len)) ||
827 (strlen(p->alias) == len && !strncmp(p->alias, name, len)))
828 return &psmouse_protocols[i];
836 * psmouse_probe() probes for a PS/2 mouse.
839 static int psmouse_probe(struct psmouse *psmouse)
841 struct ps2dev *ps2dev = &psmouse->ps2dev;
842 unsigned char param[2];
845 * First, we check if it's a mouse. It should send 0x00 or 0x03
846 * in case of an IntelliMouse in 4-byte mode or 0x04 for IM Explorer.
847 * Sunrex K8561 IR Keyboard/Mouse reports 0xff on second and subsequent
848 * ID queries, probably due to a firmware bug.
852 if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETID))
855 if (param[0] != 0x00 && param[0] != 0x03 &&
856 param[0] != 0x04 && param[0] != 0xff)
860 * Then we reset and disable the mouse so that it doesn't generate events.
863 if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_RESET_DIS))
864 printk(KERN_WARNING "psmouse.c: Failed to reset mouse on %s\n", ps2dev->serio->phys);
870 * Here we set the mouse resolution.
873 void psmouse_set_resolution(struct psmouse *psmouse, unsigned int resolution)
875 static const unsigned char params[] = { 0, 1, 2, 2, 3 };
878 if (resolution == 0 || resolution > 200)
881 p = params[resolution / 50];
882 ps2_command(&psmouse->ps2dev, &p, PSMOUSE_CMD_SETRES);
883 psmouse->resolution = 25 << p;
887 * Here we set the mouse report rate.
890 static void psmouse_set_rate(struct psmouse *psmouse, unsigned int rate)
892 static const unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10, 0 };
896 while (rates[i] > rate) i++;
898 ps2_command(&psmouse->ps2dev, &r, PSMOUSE_CMD_SETRATE);
903 * psmouse_initialize() initializes the mouse to a sane state.
906 static void psmouse_initialize(struct psmouse *psmouse)
909 * We set the mouse report rate, resolution and scaling.
912 if (psmouse_max_proto != PSMOUSE_PS2) {
913 psmouse->set_rate(psmouse, psmouse->rate);
914 psmouse->set_resolution(psmouse, psmouse->resolution);
915 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSCALE11);
920 * psmouse_activate() enables the mouse so that we get motion reports from it.
923 static void psmouse_activate(struct psmouse *psmouse)
925 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE))
926 printk(KERN_WARNING "psmouse.c: Failed to enable mouse on %s\n",
927 psmouse->ps2dev.serio->phys);
929 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
934 * psmouse_deactivate() puts the mouse into poll mode so that we don't get motion
935 * reports from it unless we explicitly request it.
938 static void psmouse_deactivate(struct psmouse *psmouse)
940 if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_DISABLE))
941 printk(KERN_WARNING "psmouse.c: Failed to deactivate mouse on %s\n",
942 psmouse->ps2dev.serio->phys);
944 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
948 * psmouse_poll() - default poll hanlder. Everyone except for ALPS uses it.
951 static int psmouse_poll(struct psmouse *psmouse)
953 return ps2_command(&psmouse->ps2dev, psmouse->packet,
954 PSMOUSE_CMD_POLL | (psmouse->pktsize << 8));
959 * psmouse_resync() attempts to re-validate current protocol.
962 static void psmouse_resync(struct work_struct *work)
964 struct psmouse *parent = NULL, *psmouse =
965 container_of(work, struct psmouse, resync_work.work);
966 struct serio *serio = psmouse->ps2dev.serio;
967 psmouse_ret_t rc = PSMOUSE_GOOD_DATA;
968 int failed = 0, enabled = 0;
971 mutex_lock(&psmouse_mutex);
973 if (psmouse->state != PSMOUSE_RESYNCING)
976 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
977 parent = serio_get_drvdata(serio->parent);
978 psmouse_deactivate(parent);
982 * Some mice don't ACK commands sent while they are in the middle of
983 * transmitting motion packet. To avoid delay we use ps2_sendbyte()
984 * instead of ps2_command() which would wait for 200ms for an ACK
985 * that may never come.
986 * As an additional quirk ALPS touchpads may not only forget to ACK
987 * disable command but will stop reporting taps, so if we see that
988 * mouse at least once ACKs disable we will do full reconnect if ACK
991 psmouse->num_resyncs++;
993 if (ps2_sendbyte(&psmouse->ps2dev, PSMOUSE_CMD_DISABLE, 20)) {
994 if (psmouse->num_resyncs < 3 || psmouse->acks_disable_command)
997 psmouse->acks_disable_command = 1;
1000 * Poll the mouse. If it was reset the packet will be shorter than
1001 * psmouse->pktsize and ps2_command will fail. We do not expect and
1002 * do not handle scenario when mouse "upgrades" its protocol while
1003 * disconnected since it would require additional delay. If we ever
1004 * see a mouse that does it we'll adjust the code.
1007 if (psmouse->poll(psmouse))
1010 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1011 for (i = 0; i < psmouse->pktsize; i++) {
1013 rc = psmouse->protocol_handler(psmouse);
1014 if (rc != PSMOUSE_GOOD_DATA)
1017 if (rc != PSMOUSE_FULL_PACKET)
1019 psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
1023 * Now try to enable mouse. We try to do that even if poll failed and also
1024 * repeat our attempts 5 times, otherwise we may be left out with disabled
1027 for (i = 0; i < 5; i++) {
1028 if (!ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
1036 printk(KERN_WARNING "psmouse.c: failed to re-enable mouse on %s\n",
1037 psmouse->ps2dev.serio->phys);
1042 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1043 printk(KERN_INFO "psmouse.c: resync failed, issuing reconnect request\n");
1044 serio_reconnect(serio);
1046 psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
1049 psmouse_activate(parent);
1051 mutex_unlock(&psmouse_mutex);
1055 * psmouse_cleanup() resets the mouse into power-on state.
1058 static void psmouse_cleanup(struct serio *serio)
1060 struct psmouse *psmouse = serio_get_drvdata(serio);
1061 struct psmouse *parent = NULL;
1063 mutex_lock(&psmouse_mutex);
1065 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1066 parent = serio_get_drvdata(serio->parent);
1067 psmouse_deactivate(parent);
1070 psmouse_deactivate(psmouse);
1072 if (psmouse->cleanup)
1073 psmouse->cleanup(psmouse);
1075 psmouse_reset(psmouse);
1078 * Some boxes, such as HP nx7400, get terribly confused if mouse
1079 * is not fully enabled before suspending/shutting down.
1081 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE);
1084 if (parent->pt_deactivate)
1085 parent->pt_deactivate(parent);
1087 psmouse_activate(parent);
1090 mutex_unlock(&psmouse_mutex);
1094 * psmouse_disconnect() closes and frees.
1097 static void psmouse_disconnect(struct serio *serio)
1099 struct psmouse *psmouse, *parent = NULL;
1101 psmouse = serio_get_drvdata(serio);
1103 sysfs_remove_group(&serio->dev.kobj, &psmouse_attribute_group);
1105 mutex_lock(&psmouse_mutex);
1107 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1109 /* make sure we don't have a resync in progress */
1110 mutex_unlock(&psmouse_mutex);
1111 flush_workqueue(kpsmoused_wq);
1112 mutex_lock(&psmouse_mutex);
1114 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1115 parent = serio_get_drvdata(serio->parent);
1116 psmouse_deactivate(parent);
1119 if (psmouse->disconnect)
1120 psmouse->disconnect(psmouse);
1122 if (parent && parent->pt_deactivate)
1123 parent->pt_deactivate(parent);
1125 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1128 serio_set_drvdata(serio, NULL);
1129 input_unregister_device(psmouse->dev);
1133 psmouse_activate(parent);
1135 mutex_unlock(&psmouse_mutex);
1138 static int psmouse_switch_protocol(struct psmouse *psmouse, const struct psmouse_protocol *proto)
1140 struct input_dev *input_dev = psmouse->dev;
1142 input_dev->dev.parent = &psmouse->ps2dev.serio->dev;
1144 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
1145 input_dev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_LEFT) |
1146 BIT_MASK(BTN_MIDDLE) | BIT_MASK(BTN_RIGHT);
1147 input_dev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
1149 psmouse->set_rate = psmouse_set_rate;
1150 psmouse->set_resolution = psmouse_set_resolution;
1151 psmouse->poll = psmouse_poll;
1152 psmouse->protocol_handler = psmouse_process_byte;
1153 psmouse->pktsize = 3;
1155 if (proto && (proto->detect || proto->init)) {
1156 if (proto->detect && proto->detect(psmouse, 1) < 0)
1159 if (proto->init && proto->init(psmouse) < 0)
1162 psmouse->type = proto->type;
1165 psmouse->type = psmouse_extensions(psmouse, psmouse_max_proto, 1);
1168 * If mouse's packet size is 3 there is no point in polling the
1169 * device in hopes to detect protocol reset - we won't get less
1170 * than 3 bytes response anyhow.
1172 if (psmouse->pktsize == 3)
1173 psmouse->resync_time = 0;
1176 * Some smart KVMs fake response to POLL command returning just
1177 * 3 bytes and messing up our resync logic, so if initial poll
1178 * fails we won't try polling the device anymore. Hopefully
1179 * such KVM will maintain initially selected protocol.
1181 if (psmouse->resync_time && psmouse->poll(psmouse))
1182 psmouse->resync_time = 0;
1184 snprintf(psmouse->devname, sizeof(psmouse->devname), "%s %s %s",
1185 psmouse_protocol_by_type(psmouse->type)->name, psmouse->vendor, psmouse->name);
1187 input_dev->name = psmouse->devname;
1188 input_dev->phys = psmouse->phys;
1189 input_dev->id.bustype = BUS_I8042;
1190 input_dev->id.vendor = 0x0002;
1191 input_dev->id.product = psmouse->type;
1192 input_dev->id.version = psmouse->model;
1198 * psmouse_connect() is a callback from the serio module when
1199 * an unhandled serio port is found.
1201 static int psmouse_connect(struct serio *serio, struct serio_driver *drv)
1203 struct psmouse *psmouse, *parent = NULL;
1204 struct input_dev *input_dev;
1205 int retval = 0, error = -ENOMEM;
1207 mutex_lock(&psmouse_mutex);
1210 * If this is a pass-through port deactivate parent so the device
1211 * connected to this port can be successfully identified
1213 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1214 parent = serio_get_drvdata(serio->parent);
1215 psmouse_deactivate(parent);
1218 psmouse = kzalloc(sizeof(struct psmouse), GFP_KERNEL);
1219 input_dev = input_allocate_device();
1220 if (!psmouse || !input_dev)
1223 ps2_init(&psmouse->ps2dev, serio);
1224 INIT_DELAYED_WORK(&psmouse->resync_work, psmouse_resync);
1225 psmouse->dev = input_dev;
1226 snprintf(psmouse->phys, sizeof(psmouse->phys), "%s/input0", serio->phys);
1228 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1230 serio_set_drvdata(serio, psmouse);
1232 error = serio_open(serio, drv);
1234 goto err_clear_drvdata;
1236 if (psmouse_probe(psmouse) < 0) {
1238 goto err_close_serio;
1241 psmouse->rate = psmouse_rate;
1242 psmouse->resolution = psmouse_resolution;
1243 psmouse->resetafter = psmouse_resetafter;
1244 psmouse->resync_time = parent ? 0 : psmouse_resync_time;
1245 psmouse->smartscroll = psmouse_smartscroll;
1247 psmouse_switch_protocol(psmouse, NULL);
1249 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1250 psmouse_initialize(psmouse);
1252 error = input_register_device(psmouse->dev);
1254 goto err_protocol_disconnect;
1256 if (parent && parent->pt_activate)
1257 parent->pt_activate(parent);
1259 error = sysfs_create_group(&serio->dev.kobj, &psmouse_attribute_group);
1261 goto err_pt_deactivate;
1263 psmouse_activate(psmouse);
1266 /* If this is a pass-through port the parent needs to be re-activated */
1268 psmouse_activate(parent);
1270 mutex_unlock(&psmouse_mutex);
1274 if (parent && parent->pt_deactivate)
1275 parent->pt_deactivate(parent);
1276 input_unregister_device(psmouse->dev);
1277 input_dev = NULL; /* so we don't try to free it below */
1278 err_protocol_disconnect:
1279 if (psmouse->disconnect)
1280 psmouse->disconnect(psmouse);
1281 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1285 serio_set_drvdata(serio, NULL);
1287 input_free_device(input_dev);
1295 static int psmouse_reconnect(struct serio *serio)
1297 struct psmouse *psmouse = serio_get_drvdata(serio);
1298 struct psmouse *parent = NULL;
1299 struct serio_driver *drv = serio->drv;
1302 if (!drv || !psmouse) {
1303 printk(KERN_DEBUG "psmouse: reconnect request, but serio is disconnected, ignoring...\n");
1307 mutex_lock(&psmouse_mutex);
1309 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1310 parent = serio_get_drvdata(serio->parent);
1311 psmouse_deactivate(parent);
1314 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1316 if (psmouse->reconnect) {
1317 if (psmouse->reconnect(psmouse))
1319 } else if (psmouse_probe(psmouse) < 0 ||
1320 psmouse->type != psmouse_extensions(psmouse, psmouse_max_proto, 0))
1323 /* ok, the device type (and capabilities) match the old one,
1324 * we can continue using it, complete intialization
1326 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1328 psmouse_initialize(psmouse);
1330 if (parent && parent->pt_activate)
1331 parent->pt_activate(parent);
1333 psmouse_activate(psmouse);
1337 /* If this is a pass-through port the parent waits to be activated */
1339 psmouse_activate(parent);
1341 mutex_unlock(&psmouse_mutex);
1345 static struct serio_device_id psmouse_serio_ids[] = {
1353 .type = SERIO_PS_PSTHRU,
1361 MODULE_DEVICE_TABLE(serio, psmouse_serio_ids);
1363 static struct serio_driver psmouse_drv = {
1367 .description = DRIVER_DESC,
1368 .id_table = psmouse_serio_ids,
1369 .interrupt = psmouse_interrupt,
1370 .connect = psmouse_connect,
1371 .reconnect = psmouse_reconnect,
1372 .disconnect = psmouse_disconnect,
1373 .cleanup = psmouse_cleanup,
1376 ssize_t psmouse_attr_show_helper(struct device *dev, struct device_attribute *devattr,
1379 struct serio *serio = to_serio_port(dev);
1380 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1381 struct psmouse *psmouse;
1384 retval = serio_pin_driver(serio);
1388 if (serio->drv != &psmouse_drv) {
1393 psmouse = serio_get_drvdata(serio);
1395 retval = attr->show(psmouse, attr->data, buf);
1398 serio_unpin_driver(serio);
1402 ssize_t psmouse_attr_set_helper(struct device *dev, struct device_attribute *devattr,
1403 const char *buf, size_t count)
1405 struct serio *serio = to_serio_port(dev);
1406 struct psmouse_attribute *attr = to_psmouse_attr(devattr);
1407 struct psmouse *psmouse, *parent = NULL;
1410 retval = serio_pin_driver(serio);
1414 if (serio->drv != &psmouse_drv) {
1419 retval = mutex_lock_interruptible(&psmouse_mutex);
1423 psmouse = serio_get_drvdata(serio);
1425 if (attr->protect) {
1426 if (psmouse->state == PSMOUSE_IGNORE) {
1431 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1432 parent = serio_get_drvdata(serio->parent);
1433 psmouse_deactivate(parent);
1436 psmouse_deactivate(psmouse);
1439 retval = attr->set(psmouse, attr->data, buf, count);
1441 if (attr->protect) {
1442 if (retval != -ENODEV)
1443 psmouse_activate(psmouse);
1446 psmouse_activate(parent);
1450 mutex_unlock(&psmouse_mutex);
1452 serio_unpin_driver(serio);
1456 static ssize_t psmouse_show_int_attr(struct psmouse *psmouse, void *offset, char *buf)
1458 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
1460 return sprintf(buf, "%u\n", *field);
1463 static ssize_t psmouse_set_int_attr(struct psmouse *psmouse, void *offset, const char *buf, size_t count)
1465 unsigned int *field = (unsigned int *)((char *)psmouse + (size_t)offset);
1466 unsigned long value;
1468 if (strict_strtoul(buf, 10, &value))
1471 if ((unsigned int)value != value)
1479 static ssize_t psmouse_attr_show_protocol(struct psmouse *psmouse, void *data, char *buf)
1481 return sprintf(buf, "%s\n", psmouse_protocol_by_type(psmouse->type)->name);
1484 static ssize_t psmouse_attr_set_protocol(struct psmouse *psmouse, void *data, const char *buf, size_t count)
1486 struct serio *serio = psmouse->ps2dev.serio;
1487 struct psmouse *parent = NULL;
1488 struct input_dev *old_dev, *new_dev;
1489 const struct psmouse_protocol *proto, *old_proto;
1493 proto = psmouse_protocol_by_name(buf, count);
1497 if (psmouse->type == proto->type)
1500 new_dev = input_allocate_device();
1504 while (serio->child) {
1506 printk(KERN_WARNING "psmouse: failed to destroy child port, protocol change aborted.\n");
1507 input_free_device(new_dev);
1511 mutex_unlock(&psmouse_mutex);
1512 serio_unpin_driver(serio);
1513 serio_unregister_child_port(serio);
1514 serio_pin_driver_uninterruptible(serio);
1515 mutex_lock(&psmouse_mutex);
1517 if (serio->drv != &psmouse_drv) {
1518 input_free_device(new_dev);
1522 if (psmouse->type == proto->type) {
1523 input_free_device(new_dev);
1524 return count; /* switched by other thread */
1528 if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
1529 parent = serio_get_drvdata(serio->parent);
1530 if (parent->pt_deactivate)
1531 parent->pt_deactivate(parent);
1534 old_dev = psmouse->dev;
1535 old_proto = psmouse_protocol_by_type(psmouse->type);
1537 if (psmouse->disconnect)
1538 psmouse->disconnect(psmouse);
1540 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1542 psmouse->dev = new_dev;
1543 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1545 if (psmouse_switch_protocol(psmouse, proto) < 0) {
1546 psmouse_reset(psmouse);
1547 /* default to PSMOUSE_PS2 */
1548 psmouse_switch_protocol(psmouse, &psmouse_protocols[0]);
1551 psmouse_initialize(psmouse);
1552 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1554 error = input_register_device(psmouse->dev);
1556 if (psmouse->disconnect)
1557 psmouse->disconnect(psmouse);
1559 psmouse_set_state(psmouse, PSMOUSE_IGNORE);
1560 input_free_device(new_dev);
1561 psmouse->dev = old_dev;
1562 psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
1563 psmouse_switch_protocol(psmouse, old_proto);
1564 psmouse_initialize(psmouse);
1565 psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
1570 input_unregister_device(old_dev);
1572 if (parent && parent->pt_activate)
1573 parent->pt_activate(parent);
1578 static ssize_t psmouse_attr_set_rate(struct psmouse *psmouse, void *data, const char *buf, size_t count)
1580 unsigned long value;
1582 if (strict_strtoul(buf, 10, &value))
1585 psmouse->set_rate(psmouse, value);
1589 static ssize_t psmouse_attr_set_resolution(struct psmouse *psmouse, void *data, const char *buf, size_t count)
1591 unsigned long value;
1593 if (strict_strtoul(buf, 10, &value))
1596 psmouse->set_resolution(psmouse, value);
1601 static int psmouse_set_maxproto(const char *val, struct kernel_param *kp)
1603 const struct psmouse_protocol *proto;
1608 proto = psmouse_protocol_by_name(val, strlen(val));
1610 if (!proto || !proto->maxproto)
1613 *((unsigned int *)kp->arg) = proto->type;
1618 static int psmouse_get_maxproto(char *buffer, struct kernel_param *kp)
1620 int type = *((unsigned int *)kp->arg);
1622 return sprintf(buffer, "%s\n", psmouse_protocol_by_type(type)->name);
1625 static int __init psmouse_init(void)
1629 kpsmoused_wq = create_singlethread_workqueue("kpsmoused");
1630 if (!kpsmoused_wq) {
1631 printk(KERN_ERR "psmouse: failed to create kpsmoused workqueue\n");
1635 err = serio_register_driver(&psmouse_drv);
1637 destroy_workqueue(kpsmoused_wq);
1642 static void __exit psmouse_exit(void)
1644 serio_unregister_driver(&psmouse_drv);
1645 destroy_workqueue(kpsmoused_wq);
1648 module_init(psmouse_init);
1649 module_exit(psmouse_exit);