WorkStruct: make allyesconfig
[safe/jmp/linux-2.6] / drivers / input / mouse / psmouse-base.c
index 6ee9999..52bb222 100644 (file)
@@ -20,6 +20,8 @@
 #include <linux/serio.h>
 #include <linux/init.h>
 #include <linux/libps2.h>
+#include <linux/mutex.h>
+
 #include "psmouse.h"
 #include "synaptics.h"
 #include "logips2pp.h"
@@ -54,10 +56,14 @@ static unsigned int psmouse_smartscroll = 1;
 module_param_named(smartscroll, psmouse_smartscroll, bool, 0644);
 MODULE_PARM_DESC(smartscroll, "Logitech Smartscroll autorepeat, 1 = enabled (default), 0 = disabled.");
 
-static unsigned int psmouse_resetafter;
+static unsigned int psmouse_resetafter = 5;
 module_param_named(resetafter, psmouse_resetafter, uint, 0644);
 MODULE_PARM_DESC(resetafter, "Reset device after so many bad packets (0 = never).");
 
+static unsigned int psmouse_resync_time;
+module_param_named(resync_time, psmouse_resync_time, uint, 0644);
+MODULE_PARM_DESC(resync_time, "How long can mouse stay idle before forcing resync (in seconds, 0 = never).");
+
 PSMOUSE_DEFINE_ATTR(protocol, S_IWUSR | S_IRUGO,
                        NULL,
                        psmouse_attr_show_protocol, psmouse_attr_set_protocol);
@@ -70,12 +76,16 @@ PSMOUSE_DEFINE_ATTR(resolution, S_IWUSR | S_IRUGO,
 PSMOUSE_DEFINE_ATTR(resetafter, S_IWUSR | S_IRUGO,
                        (void *) offsetof(struct psmouse, resetafter),
                        psmouse_show_int_attr, psmouse_set_int_attr);
+PSMOUSE_DEFINE_ATTR(resync_time, S_IWUSR | S_IRUGO,
+                       (void *) offsetof(struct psmouse, resync_time),
+                       psmouse_show_int_attr, psmouse_set_int_attr);
 
 static struct attribute *psmouse_attributes[] = {
        &psmouse_attr_protocol.dattr.attr,
        &psmouse_attr_rate.dattr.attr,
        &psmouse_attr_resolution.dattr.attr,
        &psmouse_attr_resetafter.dattr.attr,
+       &psmouse_attr_resync_time.dattr.attr,
        NULL
 };
 
@@ -90,18 +100,20 @@ __obsolete_setup("psmouse_resetafter=");
 __obsolete_setup("psmouse_rate=");
 
 /*
- * psmouse_sem protects all operations changing state of mouse
+ * psmouse_mutex protects all operations changing state of mouse
  * (connecting, disconnecting, changing rate or resolution via
  * sysfs). We could use a per-device semaphore but since there
  * rarely more than one PS/2 mouse connected and since semaphore
  * is taken in "slow" paths it is not worth it.
  */
-static DECLARE_MUTEX(psmouse_sem);
+static DEFINE_MUTEX(psmouse_mutex);
+
+static struct workqueue_struct *kpsmoused_wq;
 
 struct psmouse_protocol {
        enum psmouse_type type;
-       char *name;
-       char *alias;
+       const char *name;
+       const char *alias;
        int maxproto;
        int (*detect)(struct psmouse *, int);
        int (*init)(struct psmouse *);
@@ -112,7 +124,7 @@ struct psmouse_protocol {
  * relevant events to the input module once full packet has arrived.
  */
 
-static psmouse_ret_t psmouse_process_byte(struct psmouse *psmouse, struct pt_regs *regs)
+static psmouse_ret_t psmouse_process_byte(struct psmouse *psmouse)
 {
        struct input_dev *dev = psmouse->dev;
        unsigned char *packet = psmouse->packet;
@@ -124,8 +136,6 @@ static psmouse_ret_t psmouse_process_byte(struct psmouse *psmouse, struct pt_reg
  * Full packet accumulated, process it
  */
 
-       input_regs(dev, regs);
-
 /*
  * Scroll wheel on IntelliMice, scroll buttons on NetMice
  */
@@ -138,9 +148,20 @@ static psmouse_ret_t psmouse_process_byte(struct psmouse *psmouse, struct pt_reg
  */
 
        if (psmouse->type == PSMOUSE_IMEX) {
-               input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 8) - (int) (packet[3] & 7));
-               input_report_key(dev, BTN_SIDE, (packet[3] >> 4) & 1);
-               input_report_key(dev, BTN_EXTRA, (packet[3] >> 5) & 1);
+               switch (packet[3] & 0xC0) {
+                       case 0x80: /* vertical scroll on IntelliMouse Explorer 4.0 */
+                               input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31));
+                               break;
+                       case 0x40: /* horizontal scroll on IntelliMouse Explorer 4.0 */
+                               input_report_rel(dev, REL_HWHEEL, (int) (packet[3] & 32) - (int) (packet[3] & 31));
+                               break;
+                       case 0x00:
+                       case 0xC0:
+                               input_report_rel(dev, REL_WHEEL, (int) (packet[3] & 8) - (int) (packet[3] & 7));
+                               input_report_key(dev, BTN_SIDE, (packet[3] >> 4) & 1);
+                               input_report_key(dev, BTN_EXTRA, (packet[3] >> 5) & 1);
+                               break;
+               }
        }
 
 /*
@@ -178,15 +199,79 @@ static psmouse_ret_t psmouse_process_byte(struct psmouse *psmouse, struct pt_reg
 }
 
 /*
- * psmouse_interrupt() handles incoming characters, either gathering them into
- * packets or passing them to the command routine as command output.
+ * __psmouse_set_state() sets new psmouse state and resets all flags.
+ */
+
+static inline void __psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
+{
+       psmouse->state = new_state;
+       psmouse->pktcnt = psmouse->out_of_sync = 0;
+       psmouse->ps2dev.flags = 0;
+       psmouse->last = jiffies;
+}
+
+
+/*
+ * psmouse_set_state() sets new psmouse state and resets all flags and
+ * counters while holding serio lock so fighting with interrupt handler
+ * is not a concern.
+ */
+
+static void psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
+{
+       serio_pause_rx(psmouse->ps2dev.serio);
+       __psmouse_set_state(psmouse, new_state);
+       serio_continue_rx(psmouse->ps2dev.serio);
+}
+
+/*
+ * psmouse_handle_byte() processes one byte of the input data stream
+ * by calling corresponding protocol handler.
+ */
+
+static int psmouse_handle_byte(struct psmouse *psmouse)
+{
+       psmouse_ret_t rc = psmouse->protocol_handler(psmouse);
+
+       switch (rc) {
+               case PSMOUSE_BAD_DATA:
+                       if (psmouse->state == PSMOUSE_ACTIVATED) {
+                               printk(KERN_WARNING "psmouse.c: %s at %s lost sync at byte %d\n",
+                                       psmouse->name, psmouse->phys, psmouse->pktcnt);
+                               if (++psmouse->out_of_sync == psmouse->resetafter) {
+                                       __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
+                                       printk(KERN_NOTICE "psmouse.c: issuing reconnect request\n");
+                                       serio_reconnect(psmouse->ps2dev.serio);
+                                       return -1;
+                               }
+                       }
+                       psmouse->pktcnt = 0;
+                       break;
+
+               case PSMOUSE_FULL_PACKET:
+                       psmouse->pktcnt = 0;
+                       if (psmouse->out_of_sync) {
+                               psmouse->out_of_sync = 0;
+                               printk(KERN_NOTICE "psmouse.c: %s at %s - driver resynched.\n",
+                                       psmouse->name, psmouse->phys);
+                       }
+                       break;
+
+               case PSMOUSE_GOOD_DATA:
+                       break;
+       }
+       return 0;
+}
+
+/*
+ * psmouse_interrupt() handles incoming characters, either passing them
+ * for normal processing or gathering them as command response.
  */
 
 static irqreturn_t psmouse_interrupt(struct serio *serio,
-               unsigned char data, unsigned int flags, struct pt_regs *regs)
+               unsigned char data, unsigned int flags)
 {
        struct psmouse *psmouse = serio_get_drvdata(serio);
-       psmouse_ret_t rc;
 
        if (psmouse->state == PSMOUSE_IGNORE)
                goto out;
@@ -208,67 +293,60 @@ static irqreturn_t psmouse_interrupt(struct serio *serio,
                if  (ps2_handle_response(&psmouse->ps2dev, data))
                        goto out;
 
-       if (psmouse->state == PSMOUSE_INITIALIZING)
+       if (psmouse->state <= PSMOUSE_RESYNCING)
                goto out;
 
        if (psmouse->state == PSMOUSE_ACTIVATED &&
            psmouse->pktcnt && time_after(jiffies, psmouse->last + HZ/2)) {
-               printk(KERN_WARNING "psmouse.c: %s at %s lost synchronization, throwing %d bytes away.\n",
+               printk(KERN_INFO "psmouse.c: %s at %s lost synchronization, throwing %d bytes away.\n",
                       psmouse->name, psmouse->phys, psmouse->pktcnt);
-               psmouse->pktcnt = 0;
+               psmouse->badbyte = psmouse->packet[0];
+               __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
+               queue_work(kpsmoused_wq, &psmouse->resync_work);
+               goto out;
        }
 
-       psmouse->last = jiffies;
        psmouse->packet[psmouse->pktcnt++] = data;
-
-       if (psmouse->packet[0] == PSMOUSE_RET_BAT) {
-               if (psmouse->pktcnt == 1)
+/*
+ * Check if this is a new device announcement (0xAA 0x00)
+ */
+       if (unlikely(psmouse->packet[0] == PSMOUSE_RET_BAT && psmouse->pktcnt <= 2)) {
+               if (psmouse->pktcnt == 1) {
+                       psmouse->last = jiffies;
                        goto out;
-
-               if (psmouse->pktcnt == 2) {
-                       if (psmouse->packet[1] == PSMOUSE_RET_ID) {
-                               psmouse->state = PSMOUSE_IGNORE;
-                               serio_reconnect(serio);
-                               goto out;
-                       }
-                       if (psmouse->type == PSMOUSE_SYNAPTICS) {
-                               /* neither 0xAA nor 0x00 are valid first bytes
-                                * for a packet in absolute mode
-                                */
-                               psmouse->pktcnt = 0;
-                               goto out;
-                       }
                }
-       }
 
-       rc = psmouse->protocol_handler(psmouse, regs);
+               if (psmouse->packet[1] == PSMOUSE_RET_ID) {
+                       __psmouse_set_state(psmouse, PSMOUSE_IGNORE);
+                       serio_reconnect(serio);
+                       goto out;
+               }
+/*
+ * Not a new device, try processing first byte normally
+ */
+               psmouse->pktcnt = 1;
+               if (psmouse_handle_byte(psmouse))
+                       goto out;
 
-       switch (rc) {
-               case PSMOUSE_BAD_DATA:
-                       printk(KERN_WARNING "psmouse.c: %s at %s lost sync at byte %d\n",
-                               psmouse->name, psmouse->phys, psmouse->pktcnt);
-                       psmouse->pktcnt = 0;
+               psmouse->packet[psmouse->pktcnt++] = data;
+       }
 
-                       if (++psmouse->out_of_sync == psmouse->resetafter) {
-                               psmouse->state = PSMOUSE_IGNORE;
-                               printk(KERN_NOTICE "psmouse.c: issuing reconnect request\n");
-                               serio_reconnect(psmouse->ps2dev.serio);
-                       }
-                       break;
+/*
+ * See if we need to force resync because mouse was idle for too long
+ */
+       if (psmouse->state == PSMOUSE_ACTIVATED &&
+           psmouse->pktcnt == 1 && psmouse->resync_time &&
+           time_after(jiffies, psmouse->last + psmouse->resync_time * HZ)) {
+               psmouse->badbyte = psmouse->packet[0];
+               __psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
+               queue_work(kpsmoused_wq, &psmouse->resync_work);
+               goto out;
+       }
 
-               case PSMOUSE_FULL_PACKET:
-                       psmouse->pktcnt = 0;
-                       if (psmouse->out_of_sync) {
-                               psmouse->out_of_sync = 0;
-                               printk(KERN_NOTICE "psmouse.c: %s at %s - driver resynched.\n",
-                                       psmouse->name, psmouse->phys);
-                       }
-                       break;
+       psmouse->last = jiffies;
+       psmouse_handle_byte(psmouse);
 
-               case PSMOUSE_GOOD_DATA:
-                       break;
-       }
-out:
+ out:
        return IRQ_HANDLED;
 }
 
@@ -338,6 +416,7 @@ static int genius_detect(struct psmouse *psmouse, int set_properties)
                set_bit(REL_WHEEL, psmouse->dev->relbit);
 
                psmouse->vendor = "Genius";
+               psmouse->name = "Mouse";
                psmouse->pktsize = 4;
        }
 
@@ -396,9 +475,18 @@ static int im_explorer_detect(struct psmouse *psmouse, int set_properties)
        if (param[0] != 4)
                return -1;
 
+/* Magic to enable horizontal scrolling on IntelliMouse 4.0 */
+       param[0] = 200;
+       ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
+       param[0] =  80;
+       ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
+       param[0] =  40;
+       ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
+
        if (set_properties) {
                set_bit(BTN_MIDDLE, psmouse->dev->keybit);
                set_bit(REL_WHEEL, psmouse->dev->relbit);
+               set_bit(REL_HWHEEL, psmouse->dev->relbit);
                set_bit(BTN_SIDE, psmouse->dev->keybit);
                set_bit(BTN_EXTRA, psmouse->dev->keybit);
 
@@ -417,15 +505,17 @@ static int thinking_detect(struct psmouse *psmouse, int set_properties)
 {
        struct ps2dev *ps2dev = &psmouse->ps2dev;
        unsigned char param[2];
-       unsigned char seq[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20, 0 };
+       static const unsigned char seq[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
        int i;
 
        param[0] = 10;
        ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
        param[0] = 0;
        ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
-       for (i = 0; seq[i]; i++)
-               ps2_command(ps2dev, seq + i, PSMOUSE_CMD_SETRATE);
+       for (i = 0; i < ARRAY_SIZE(seq); i++) {
+               param[0] = seq[i];
+               ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
+       }
        ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
 
        if (param[0] != 2)
@@ -527,11 +617,15 @@ static int psmouse_extensions(struct psmouse *psmouse,
        if (max_proto > PSMOUSE_IMEX && ps2pp_init(psmouse, set_properties) == 0)
                return PSMOUSE_PS2PP;
 
+       if (max_proto > PSMOUSE_IMEX && trackpoint_detect(psmouse, set_properties) == 0)
+               return PSMOUSE_TRACKPOINT;
+
 /*
  * Reset to defaults in case the device got confused by extended
- * protocol probes.
+ * protocol probes. Note that we do full reset becuase some mice
+ * put themselves to sleep when see PSMOUSE_RESET_DIS.
  */
-       ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
+       psmouse_reset(psmouse);
 
        if (max_proto >= PSMOUSE_IMEX && im_explorer_detect(psmouse, set_properties) == 0)
                return PSMOUSE_IMEX;
@@ -540,12 +634,6 @@ static int psmouse_extensions(struct psmouse *psmouse,
                return PSMOUSE_IMPS;
 
 /*
- * Try to initialize the IBM TrackPoint
- */
-       if (max_proto > PSMOUSE_IMEX && trackpoint_detect(psmouse, set_properties) == 0)
-               return PSMOUSE_TRACKPOINT;
-
-/*
  * Okay, all failed, we have a standard mouse here. The number of the buttons
  * is still a question, though. We assume 3.
  */
@@ -559,13 +647,12 @@ static int psmouse_extensions(struct psmouse *psmouse,
  * extensions.
  */
                psmouse_reset(psmouse);
-               ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
        }
 
        return PSMOUSE_PS2;
 }
 
-static struct psmouse_protocol psmouse_protocols[] = {
+static const struct psmouse_protocol psmouse_protocols[] = {
        {
                .type           = PSMOUSE_PS2,
                .name           = "PS/2",
@@ -639,7 +726,7 @@ static struct psmouse_protocol psmouse_protocols[] = {
        },
 };
 
-static struct psmouse_protocol *psmouse_protocol_by_type(enum psmouse_type type)
+static const struct psmouse_protocol *psmouse_protocol_by_type(enum psmouse_type type)
 {
        int i;
 
@@ -651,9 +738,9 @@ static struct psmouse_protocol *psmouse_protocol_by_type(enum psmouse_type type)
        return &psmouse_protocols[0];
 }
 
-static struct psmouse_protocol *psmouse_protocol_by_name(const char *name, size_t len)
+static const struct psmouse_protocol *psmouse_protocol_by_name(const char *name, size_t len)
 {
-       struct psmouse_protocol *p;
+       const struct psmouse_protocol *p;
        int i;
 
        for (i = 0; i < ARRAY_SIZE(psmouse_protocols); i++) {
@@ -708,13 +795,15 @@ static int psmouse_probe(struct psmouse *psmouse)
 
 void psmouse_set_resolution(struct psmouse *psmouse, unsigned int resolution)
 {
-       unsigned char params[] = { 0, 1, 2, 2, 3 };
+       static const unsigned char params[] = { 0, 1, 2, 2, 3 };
+       unsigned char p;
 
        if (resolution == 0 || resolution > 200)
                resolution = 200;
 
-       ps2_command(&psmouse->ps2dev, &params[resolution / 50], PSMOUSE_CMD_SETRES);
-       psmouse->resolution = 25 << params[resolution / 50];
+       p = params[resolution / 50];
+       ps2_command(&psmouse->ps2dev, &p, PSMOUSE_CMD_SETRES);
+       psmouse->resolution = 25 << p;
 }
 
 /*
@@ -723,12 +812,14 @@ void psmouse_set_resolution(struct psmouse *psmouse, unsigned int resolution)
 
 static void psmouse_set_rate(struct psmouse *psmouse, unsigned int rate)
 {
-       unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10, 0 };
+       static const unsigned char rates[] = { 200, 100, 80, 60, 40, 20, 10, 0 };
+       unsigned char r;
        int i = 0;
 
        while (rates[i] > rate) i++;
-       ps2_command(&psmouse->ps2dev, &rates[i], PSMOUSE_CMD_SETRATE);
-       psmouse->rate = rates[i];
+       r = rates[i];
+       ps2_command(&psmouse->ps2dev, &r, PSMOUSE_CMD_SETRATE);
+       psmouse->rate = r;
 }
 
 /*
@@ -755,21 +846,6 @@ static void psmouse_initialize(struct psmouse *psmouse)
 }
 
 /*
- * psmouse_set_state() sets new psmouse state and resets all flags and
- * counters while holding serio lock so fighting with interrupt handler
- * is not a concern.
- */
-
-static void psmouse_set_state(struct psmouse *psmouse, enum psmouse_state new_state)
-{
-       serio_pause_rx(psmouse->ps2dev.serio);
-       psmouse->state = new_state;
-       psmouse->pktcnt = psmouse->out_of_sync = 0;
-       psmouse->ps2dev.flags = 0;
-       serio_continue_rx(psmouse->ps2dev.serio);
-}
-
-/*
  * psmouse_activate() enables the mouse so that we get motion reports from it.
  */
 
@@ -797,6 +873,112 @@ static void psmouse_deactivate(struct psmouse *psmouse)
        psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
 }
 
+/*
+ * psmouse_poll() - default poll hanlder. Everyone except for ALPS uses it.
+ */
+
+static int psmouse_poll(struct psmouse *psmouse)
+{
+       return ps2_command(&psmouse->ps2dev, psmouse->packet,
+                          PSMOUSE_CMD_POLL | (psmouse->pktsize << 8));
+}
+
+
+/*
+ * psmouse_resync() attempts to re-validate current protocol.
+ */
+
+static void psmouse_resync(struct work_struct *work)
+{
+       struct psmouse *parent = NULL, *psmouse =
+               container_of(work, struct psmouse, resync_work);
+       struct serio *serio = psmouse->ps2dev.serio;
+       psmouse_ret_t rc = PSMOUSE_GOOD_DATA;
+       int failed = 0, enabled = 0;
+       int i;
+
+       mutex_lock(&psmouse_mutex);
+
+       if (psmouse->state != PSMOUSE_RESYNCING)
+               goto out;
+
+       if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
+               parent = serio_get_drvdata(serio->parent);
+               psmouse_deactivate(parent);
+       }
+
+/*
+ * Some mice don't ACK commands sent while they are in the middle of
+ * transmitting motion packet. To avoid delay we use ps2_sendbyte()
+ * instead of ps2_command() which would wait for 200ms for an ACK
+ * that may never come.
+ * As an additional quirk ALPS touchpads may not only forget to ACK
+ * disable command but will stop reporting taps, so if we see that
+ * mouse at least once ACKs disable we will do full reconnect if ACK
+ * is missing.
+ */
+       psmouse->num_resyncs++;
+
+       if (ps2_sendbyte(&psmouse->ps2dev, PSMOUSE_CMD_DISABLE, 20)) {
+               if (psmouse->num_resyncs < 3 || psmouse->acks_disable_command)
+                       failed = 1;
+       } else
+               psmouse->acks_disable_command = 1;
+
+/*
+ * Poll the mouse. If it was reset the packet will be shorter than
+ * psmouse->pktsize and ps2_command will fail. We do not expect and
+ * do not handle scenario when mouse "upgrades" its protocol while
+ * disconnected since it would require additional delay. If we ever
+ * see a mouse that does it we'll adjust the code.
+ */
+       if (!failed) {
+               if (psmouse->poll(psmouse))
+                       failed = 1;
+               else {
+                       psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
+                       for (i = 0; i < psmouse->pktsize; i++) {
+                               psmouse->pktcnt++;
+                               rc = psmouse->protocol_handler(psmouse);
+                               if (rc != PSMOUSE_GOOD_DATA)
+                                       break;
+                       }
+                       if (rc != PSMOUSE_FULL_PACKET)
+                               failed = 1;
+                       psmouse_set_state(psmouse, PSMOUSE_RESYNCING);
+               }
+       }
+/*
+ * Now try to enable mouse. We try to do that even if poll failed and also
+ * repeat our attempts 5 times, otherwise we may be left out with disabled
+ * mouse.
+ */
+       for (i = 0; i < 5; i++) {
+               if (!ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_ENABLE)) {
+                       enabled = 1;
+                       break;
+               }
+               msleep(200);
+       }
+
+       if (!enabled) {
+               printk(KERN_WARNING "psmouse.c: failed to re-enable mouse on %s\n",
+                       psmouse->ps2dev.serio->phys);
+               failed = 1;
+       }
+
+       if (failed) {
+               psmouse_set_state(psmouse, PSMOUSE_IGNORE);
+               printk(KERN_INFO "psmouse.c: resync failed, issuing reconnect request\n");
+               serio_reconnect(serio);
+       } else
+               psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
+
+       if (parent)
+               psmouse_activate(parent);
+ out:
+       mutex_unlock(&psmouse_mutex);
+}
 
 /*
  * psmouse_cleanup() resets the mouse into power-on state.
@@ -821,10 +1003,15 @@ static void psmouse_disconnect(struct serio *serio)
 
        sysfs_remove_group(&serio->dev.kobj, &psmouse_attribute_group);
 
-       down(&psmouse_sem);
+       mutex_lock(&psmouse_mutex);
 
        psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
 
+       /* make sure we don't have a resync in progress */
+       mutex_unlock(&psmouse_mutex);
+       flush_workqueue(kpsmoused_wq);
+       mutex_lock(&psmouse_mutex);
+
        if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
                parent = serio_get_drvdata(serio->parent);
                psmouse_deactivate(parent);
@@ -846,10 +1033,10 @@ static void psmouse_disconnect(struct serio *serio)
        if (parent)
                psmouse_activate(parent);
 
-       up(&psmouse_sem);
+       mutex_unlock(&psmouse_mutex);
 }
 
-static int psmouse_switch_protocol(struct psmouse *psmouse, struct psmouse_protocol *proto)
+static int psmouse_switch_protocol(struct psmouse *psmouse, const struct psmouse_protocol *proto)
 {
        struct input_dev *input_dev = psmouse->dev;
 
@@ -862,6 +1049,7 @@ static int psmouse_switch_protocol(struct psmouse *psmouse, struct psmouse_proto
 
        psmouse->set_rate = psmouse_set_rate;
        psmouse->set_resolution = psmouse_set_resolution;
+       psmouse->poll = psmouse_poll;
        psmouse->protocol_handler = psmouse_process_byte;
        psmouse->pktsize = 3;
 
@@ -877,8 +1065,25 @@ static int psmouse_switch_protocol(struct psmouse *psmouse, struct psmouse_proto
        else
                psmouse->type = psmouse_extensions(psmouse, psmouse_max_proto, 1);
 
-       sprintf(psmouse->devname, "%s %s %s",
-               psmouse_protocol_by_type(psmouse->type)->name, psmouse->vendor, psmouse->name);
+       /*
+        * If mouse's packet size is 3 there is no point in polling the
+        * device in hopes to detect protocol reset - we won't get less
+        * than 3 bytes response anyhow.
+        */
+       if (psmouse->pktsize == 3)
+               psmouse->resync_time = 0;
+
+       /*
+        * Some smart KVMs fake response to POLL command returning just
+        * 3 bytes and messing up our resync logic, so if initial poll
+        * fails we won't try polling the device anymore. Hopefully
+        * such KVM will maintain initially selected protocol.
+        */
+       if (psmouse->resync_time && psmouse->poll(psmouse))
+               psmouse->resync_time = 0;
+
+       snprintf(psmouse->devname, sizeof(psmouse->devname), "%s %s %s",
+                psmouse_protocol_by_type(psmouse->type)->name, psmouse->vendor, psmouse->name);
 
        input_dev->name = psmouse->devname;
        input_dev->phys = psmouse->phys;
@@ -900,7 +1105,7 @@ static int psmouse_connect(struct serio *serio, struct serio_driver *drv)
        struct input_dev *input_dev;
        int retval = -ENOMEM;
 
-       down(&psmouse_sem);
+       mutex_lock(&psmouse_mutex);
 
        /*
         * If this is a pass-through port deactivate parent so the device
@@ -917,8 +1122,9 @@ static int psmouse_connect(struct serio *serio, struct serio_driver *drv)
                goto out;
 
        ps2_init(&psmouse->ps2dev, serio);
+       INIT_WORK(&psmouse->resync_work, psmouse_resync);
        psmouse->dev = input_dev;
-       sprintf(psmouse->phys, "%s/input0", serio->phys);
+       snprintf(psmouse->phys, sizeof(psmouse->phys), "%s/input0", serio->phys);
 
        psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
 
@@ -937,6 +1143,7 @@ static int psmouse_connect(struct serio *serio, struct serio_driver *drv)
        psmouse->rate = psmouse_rate;
        psmouse->resolution = psmouse_resolution;
        psmouse->resetafter = psmouse_resetafter;
+       psmouse->resync_time = parent ? 0 : psmouse_resync_time;
        psmouse->smartscroll = psmouse_smartscroll;
 
        psmouse_switch_protocol(psmouse, NULL);
@@ -966,7 +1173,7 @@ out:
        if (parent)
                psmouse_activate(parent);
 
-       up(&psmouse_sem);
+       mutex_unlock(&psmouse_mutex);
        return retval;
 }
 
@@ -983,7 +1190,7 @@ static int psmouse_reconnect(struct serio *serio)
                return -1;
        }
 
-       down(&psmouse_sem);
+       mutex_lock(&psmouse_mutex);
 
        if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
                parent = serio_get_drvdata(serio->parent);
@@ -1017,7 +1224,7 @@ out:
        if (parent)
                psmouse_activate(parent);
 
-       up(&psmouse_sem);
+       mutex_unlock(&psmouse_mutex);
        return rc;
 }
 
@@ -1095,7 +1302,7 @@ ssize_t psmouse_attr_set_helper(struct device *dev, struct device_attribute *dev
                goto out_unpin;
        }
 
-       retval = down_interruptible(&psmouse_sem);
+       retval = mutex_lock_interruptible(&psmouse_mutex);
        if (retval)
                goto out_unpin;
 
@@ -1103,7 +1310,7 @@ ssize_t psmouse_attr_set_helper(struct device *dev, struct device_attribute *dev
 
        if (psmouse->state == PSMOUSE_IGNORE) {
                retval = -ENODEV;
-               goto out_up;
+               goto out_unlock;
        }
 
        if (serio->parent && serio->id.type == SERIO_PS_PSTHRU) {
@@ -1121,8 +1328,8 @@ ssize_t psmouse_attr_set_helper(struct device *dev, struct device_attribute *dev
        if (parent)
                psmouse_activate(parent);
 
- out_up:
-       up(&psmouse_sem);
+ out_unlock:
+       mutex_unlock(&psmouse_mutex);
  out_unpin:
        serio_unpin_driver(serio);
        return retval;
@@ -1160,7 +1367,7 @@ static ssize_t psmouse_attr_set_protocol(struct psmouse *psmouse, void *data, co
        struct serio *serio = psmouse->ps2dev.serio;
        struct psmouse *parent = NULL;
        struct input_dev *new_dev;
-       struct psmouse_protocol *proto;
+       const struct psmouse_protocol *proto;
        int retry = 0;
 
        if (!(proto = psmouse_protocol_by_name(buf, count)))
@@ -1179,11 +1386,11 @@ static ssize_t psmouse_attr_set_protocol(struct psmouse *psmouse, void *data, co
                        return -EIO;
                }
 
-               up(&psmouse_sem);
+               mutex_unlock(&psmouse_mutex);
                serio_unpin_driver(serio);
                serio_unregister_child_port(serio);
                serio_pin_driver_uninterruptible(serio);
-               down(&psmouse_sem);
+               mutex_lock(&psmouse_mutex);
 
                if (serio->drv != &psmouse_drv) {
                        input_free_device(new_dev);
@@ -1257,7 +1464,7 @@ static ssize_t psmouse_attr_set_resolution(struct psmouse *psmouse, void *data,
 
 static int psmouse_set_maxproto(const char *val, struct kernel_param *kp)
 {
-       struct psmouse_protocol *proto;
+       const struct psmouse_protocol *proto;
 
        if (!val)
                return -EINVAL;
@@ -1281,13 +1488,21 @@ static int psmouse_get_maxproto(char *buffer, struct kernel_param *kp)
 
 static int __init psmouse_init(void)
 {
+       kpsmoused_wq = create_singlethread_workqueue("kpsmoused");
+       if (!kpsmoused_wq) {
+               printk(KERN_ERR "psmouse: failed to create kpsmoused workqueue\n");
+               return -ENOMEM;
+       }
+
        serio_register_driver(&psmouse_drv);
+
        return 0;
 }
 
 static void __exit psmouse_exit(void)
 {
        serio_unregister_driver(&psmouse_drv);
+       destroy_workqueue(kpsmoused_wq);
 }
 
 module_init(psmouse_init);