Input: mice - handle errors when registering input devices
authorDmitry Torokhov <dtor@insightbb.com>
Mon, 6 Nov 2006 03:40:19 +0000 (22:40 -0500)
committerDmitry Torokhov <dtor@insightbb.com>
Mon, 6 Nov 2006 03:40:19 +0000 (22:40 -0500)
Signed-off-by: Dmitry Torokhov <dtor@mail.ru>
drivers/input/mouse/amimouse.c
drivers/input/mouse/inport.c
drivers/input/mouse/logibm.c
drivers/input/mouse/pc110pad.c
drivers/input/mouse/psmouse-base.c
drivers/input/mouse/rpcmouse.c
drivers/input/mouse/sermouse.c
drivers/input/mouse/vsxxxaa.c

index 599a7b2dc5191f7f057f0b4a2996a32c1c162d10..239a0e16d91abf215904584e72b3985c746e770b 100644 (file)
@@ -95,10 +95,13 @@ static void amimouse_close(struct input_dev *dev)
 
 static int __init amimouse_init(void)
 {
+       int err;
+
        if (!MACH_IS_AMIGA || !AMIGAHW_PRESENT(AMI_MOUSE))
                return -ENODEV;
 
-       if (!(amimouse_dev = input_allocate_device()))
+       amimouse_dev = input_allocate_device();
+       if (!amimouse_dev)
                return -ENOMEM;
 
        amimouse_dev->name = "Amiga mouse";
@@ -114,7 +117,11 @@ static int __init amimouse_init(void)
        amimouse_dev->open = amimouse_open;
        amimouse_dev->close = amimouse_close;
 
-       input_register_device(amimouse_dev);
+       err = input_register_device(amimouse_dev);
+       if (err) {
+               input_free_device(amimouse_dev);
+               return err;
+       }
 
        return 0;
 }
index e1252fa9a10788b88f38005b0eb78ebd3cfa41c3..13dd96785e397bf81d632993512e9457ae3e0053 100644 (file)
@@ -135,6 +135,7 @@ static void inport_close(struct input_dev *dev)
 static int __init inport_init(void)
 {
        unsigned char a, b, c;
+       int err;
 
        if (!request_region(INPORT_BASE, INPORT_EXTENT, "inport")) {
                printk(KERN_ERR "inport.c: Can't allocate ports at %#x\n", INPORT_BASE);
@@ -145,15 +146,16 @@ static int __init inport_init(void)
        b = inb(INPORT_SIGNATURE_PORT);
        c = inb(INPORT_SIGNATURE_PORT);
        if (a == b || a != c) {
-               release_region(INPORT_BASE, INPORT_EXTENT);
                printk(KERN_ERR "inport.c: Didn't find InPort mouse at %#x\n", INPORT_BASE);
-               return -ENODEV;
+               err = -ENODEV;
+               goto err_release_region;
        }
 
-       if (!(inport_dev = input_allocate_device())) {
+       inport_dev = input_allocate_device();
+       if (!inport_dev) {
                printk(KERN_ERR "inport.c: Not enough memory for input device\n");
-               release_region(INPORT_BASE, INPORT_EXTENT);
-               return -ENOMEM;
+               err = -ENOMEM;
+               goto err_release_region;
        }
 
        inport_dev->name = INPORT_NAME;
@@ -174,9 +176,18 @@ static int __init inport_init(void)
        outb(INPORT_REG_MODE, INPORT_CONTROL_PORT);
        outb(INPORT_MODE_BASE, INPORT_DATA_PORT);
 
-       input_register_device(inport_dev);
+       err = input_register_device(inport_dev);
+       if (err)
+               goto err_free_dev;
 
        return 0;
+
+ err_free_dev:
+       input_free_device(inport_dev);
+ err_release_region:
+       release_region(INPORT_BASE, INPORT_EXTENT);
+
+       return err;
 }
 
 static void __exit inport_exit(void)
index 8e9c2f3d69a860a7017e4459881043327bb174c3..db205995bffd08baf589ab5457c71ee8f640efe8 100644 (file)
@@ -124,6 +124,8 @@ static void logibm_close(struct input_dev *dev)
 
 static int __init logibm_init(void)
 {
+       int err;
+
        if (!request_region(LOGIBM_BASE, LOGIBM_EXTENT, "logibm")) {
                printk(KERN_ERR "logibm.c: Can't allocate ports at %#x\n", LOGIBM_BASE);
                return -EBUSY;
@@ -134,18 +136,19 @@ static int __init logibm_init(void)
        udelay(100);
 
        if (inb(LOGIBM_SIGNATURE_PORT) != LOGIBM_SIGNATURE_BYTE) {
-               release_region(LOGIBM_BASE, LOGIBM_EXTENT);
                printk(KERN_ERR "logibm.c: Didn't find Logitech busmouse at %#x\n", LOGIBM_BASE);
-               return -ENODEV;
+               err = -ENODEV;
+               goto err_release_region;
        }
 
        outb(LOGIBM_DEFAULT_MODE, LOGIBM_CONFIG_PORT);
        outb(LOGIBM_DISABLE_IRQ, LOGIBM_CONTROL_PORT);
 
-       if (!(logibm_dev = input_allocate_device())) {
+       logibm_dev = input_allocate_device();
+       if (!logibm_dev) {
                printk(KERN_ERR "logibm.c: Not enough memory for input device\n");
-               release_region(LOGIBM_BASE, LOGIBM_EXTENT);
-               return -ENOMEM;
+               err = -ENOMEM;
+               goto err_release_region;
        }
 
        logibm_dev->name = "Logitech bus mouse";
@@ -162,9 +165,18 @@ static int __init logibm_init(void)
        logibm_dev->open  = logibm_open;
        logibm_dev->close = logibm_close;
 
-       input_register_device(logibm_dev);
+       err = input_register_device(logibm_dev);
+       if (err)
+               goto err_free_dev;
 
        return 0;
+
+ err_free_dev:
+       input_free_device(logibm_dev);
+ err_release_region:
+       release_region(LOGIBM_BASE, LOGIBM_EXTENT);
+
+       return err;
 }
 
 static void __exit logibm_exit(void)
index 8c075aa7223b58978f61bd7924cb77b17f1f7237..f155c1fea04e3bd2e3181106886782011f3d64e7 100644 (file)
@@ -108,6 +108,7 @@ static int pc110pad_open(struct input_dev *dev)
 static int __init pc110pad_init(void)
 {
        struct pci_dev *dev;
+       int err;
 
        dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, NULL);
        if (dev) {
@@ -124,16 +125,16 @@ static int __init pc110pad_init(void)
        outb(PC110PAD_OFF, pc110pad_io + 2);
 
        if (request_irq(pc110pad_irq, pc110pad_interrupt, 0, "pc110pad", NULL)) {
-               release_region(pc110pad_io, 4);
                printk(KERN_ERR "pc110pad: Unable to get irq %d.\n", pc110pad_irq);
-               return -EBUSY;
+               err = -EBUSY;
+               goto err_release_region;
        }
 
-       if (!(pc110pad_dev = input_allocate_device())) {
-               free_irq(pc110pad_irq, NULL);
-               release_region(pc110pad_io, 4);
+       pc110pad_dev = input_allocate_device();
+       if (!pc110pad_dev) {
                printk(KERN_ERR "pc110pad: Not enough memory.\n");
-               return -ENOMEM;
+               err = -ENOMEM;
+               goto err_free_irq;
        }
 
        pc110pad_dev->name = "IBM PC110 TouchPad";
@@ -153,9 +154,20 @@ static int __init pc110pad_init(void)
        pc110pad_dev->open = pc110pad_open;
        pc110pad_dev->close = pc110pad_close;
 
-       input_register_device(pc110pad_dev);
+       err = input_register_device(pc110pad_dev);
+       if (err)
+               goto err_free_dev;
 
        return 0;
+
+ err_free_dev:
+       input_free_device(pc110pad_dev);
+ err_free_irq:
+       free_irq(pc110pad_irq, NULL);
+ err_release_region:
+       release_region(pc110pad_io, 4);
+
+       return err;
 }
 
 static void __exit pc110pad_exit(void)
index 6f9b2c7cc9c28903c05a51b682975c112996d129..9144df65e70384b9784a4a37f333be4569691bf1 100644 (file)
@@ -1102,7 +1102,7 @@ static int psmouse_connect(struct serio *serio, struct serio_driver *drv)
 {
        struct psmouse *psmouse, *parent = NULL;
        struct input_dev *input_dev;
-       int retval = -ENOMEM;
+       int retval = 0, error = -ENOMEM;
 
        mutex_lock(&psmouse_mutex);
 
@@ -1118,7 +1118,7 @@ static int psmouse_connect(struct serio *serio, struct serio_driver *drv)
        psmouse = kzalloc(sizeof(struct psmouse), GFP_KERNEL);
        input_dev = input_allocate_device();
        if (!psmouse || !input_dev)
-               goto out;
+               goto err_free;
 
        ps2_init(&psmouse->ps2dev, serio);
        INIT_WORK(&psmouse->resync_work, psmouse_resync, psmouse);
@@ -1129,14 +1129,13 @@ static int psmouse_connect(struct serio *serio, struct serio_driver *drv)
 
        serio_set_drvdata(serio, psmouse);
 
-       retval = serio_open(serio, drv);
-       if (retval)
-               goto out;
+       error = serio_open(serio, drv);
+       if (error)
+               goto err_clear_drvdata;
 
        if (psmouse_probe(psmouse) < 0) {
-               serio_close(serio);
-               retval = -ENODEV;
-               goto out;
+               error = -ENODEV;
+               goto err_close_serio;
        }
 
        psmouse->rate = psmouse_rate;
@@ -1150,30 +1149,44 @@ static int psmouse_connect(struct serio *serio, struct serio_driver *drv)
        psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
        psmouse_initialize(psmouse);
 
-       input_register_device(psmouse->dev);
+       error = input_register_device(psmouse->dev);
+       if (error)
+               goto err_protocol_disconnect;
 
        if (parent && parent->pt_activate)
                parent->pt_activate(parent);
 
-       sysfs_create_group(&serio->dev.kobj, &psmouse_attribute_group);
+       error = sysfs_create_group(&serio->dev.kobj, &psmouse_attribute_group);
+       if (error)
+               goto err_pt_deactivate;
 
        psmouse_activate(psmouse);
 
-       retval = 0;
-
-out:
-       if (retval) {
-               serio_set_drvdata(serio, NULL);
-               input_free_device(input_dev);
-               kfree(psmouse);
-       }
-
+ out:
        /* If this is a pass-through port the parent needs to be re-activated */
        if (parent)
                psmouse_activate(parent);
 
        mutex_unlock(&psmouse_mutex);
        return retval;
+
+ err_pt_deactivate:
+       if (parent && parent->pt_deactivate)
+               parent->pt_deactivate(parent);
+ err_protocol_disconnect:
+       if (psmouse->disconnect)
+               psmouse->disconnect(psmouse);
+       psmouse_set_state(psmouse, PSMOUSE_IGNORE);
+ err_close_serio:
+       serio_close(serio);
+ err_clear_drvdata:
+       serio_set_drvdata(serio, NULL);
+ err_free:
+       input_free_device(input_dev);
+       kfree(psmouse);
+
+       retval = error;
+       goto out;
 }
 
 
@@ -1365,17 +1378,20 @@ 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;
-       const struct psmouse_protocol *proto;
+       struct input_dev *old_dev, *new_dev;
+       const struct psmouse_protocol *proto, *old_proto;
+       int error;
        int retry = 0;
 
-       if (!(proto = psmouse_protocol_by_name(buf, count)))
+       proto = psmouse_protocol_by_name(buf, count);
+       if (!proto)
                return -EINVAL;
 
        if (psmouse->type == proto->type)
                return count;
 
-       if (!(new_dev = input_allocate_device()))
+       new_dev = input_allocate_device();
+       if (!new_dev)
                return -ENOMEM;
 
        while (serio->child) {
@@ -1408,11 +1424,13 @@ static ssize_t psmouse_attr_set_protocol(struct psmouse *psmouse, void *data, co
                        parent->pt_deactivate(parent);
        }
 
+       old_dev = psmouse->dev;
+       old_proto = psmouse_protocol_by_type(psmouse->type);
+
        if (psmouse->disconnect)
                psmouse->disconnect(psmouse);
 
        psmouse_set_state(psmouse, PSMOUSE_IGNORE);
-       input_unregister_device(psmouse->dev);
 
        psmouse->dev = new_dev;
        psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
@@ -1426,7 +1444,23 @@ static ssize_t psmouse_attr_set_protocol(struct psmouse *psmouse, void *data, co
        psmouse_initialize(psmouse);
        psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
 
-       input_register_device(psmouse->dev);
+       error = input_register_device(psmouse->dev);
+       if (error) {
+               if (psmouse->disconnect)
+                       psmouse->disconnect(psmouse);
+
+               psmouse_set_state(psmouse, PSMOUSE_IGNORE);
+               input_free_device(new_dev);
+               psmouse->dev = old_dev;
+               psmouse_set_state(psmouse, PSMOUSE_INITIALIZING);
+               psmouse_switch_protocol(psmouse, old_proto);
+               psmouse_initialize(psmouse);
+               psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
+
+               return error;
+       }
+
+       input_unregister_device(old_dev);
 
        if (parent && parent->pt_activate)
                parent->pt_activate(parent);
index ea0468569610a13065e432283612903dc582fd15..fbdcfd8eb4e9d9860a2f190d98dcbfb32e175c3d 100644 (file)
@@ -66,7 +66,10 @@ static irqreturn_t rpcmouse_irq(int irq, void *dev_id)
 
 static int __init rpcmouse_init(void)
 {
-       if (!(rpcmouse_dev = input_allocate_device()))
+       int err;
+
+       rpcmouse_dev = input_allocate_device();
+       if (!rpcmouse_dev)
                return -ENOMEM;
 
        rpcmouse_dev->name = "Acorn RiscPC Mouse";
@@ -85,13 +88,22 @@ static int __init rpcmouse_init(void)
 
        if (request_irq(IRQ_VSYNCPULSE, rpcmouse_irq, IRQF_SHARED, "rpcmouse", rpcmouse_dev)) {
                printk(KERN_ERR "rpcmouse: unable to allocate VSYNC interrupt\n");
-               input_free_device(rpcmouse_dev);
-               return -EBUSY;
+               err = -EBUSY;
+               goto err_free_dev;
        }
 
-       input_register_device(rpcmouse_dev);
+       err = input_register_device(rpcmouse_dev);
+       if (err)
+               goto err_free_irq;
 
        return 0;
+
+ err_free_irq:
+       free_irq(IRQ_VSYNCPULSE, rpcmouse_dev);
+ err_free_dev:
+       input_free_device(rpcmouse_dev);
+
+       return err;
 }
 
 static void __exit rpcmouse_exit(void)
index 2a272c5daf08b2284f4a6f24f10e70813c38c822..10b51e7f01f6923ebed79d511f5ba10e28351ec3 100644 (file)
@@ -246,7 +246,7 @@ static int sermouse_connect(struct serio *serio, struct serio_driver *drv)
        sermouse = kzalloc(sizeof(struct sermouse), GFP_KERNEL);
        input_dev = input_allocate_device();
        if (!sermouse || !input_dev)
-               goto fail;
+               goto fail1;
 
        sermouse->dev = input_dev;
        snprintf(sermouse->phys, sizeof(sermouse->phys), "%s/input0", serio->phys);
@@ -275,14 +275,17 @@ static int sermouse_connect(struct serio *serio, struct serio_driver *drv)
 
        err = serio_open(serio, drv);
        if (err)
-               goto fail;
+               goto fail2;
 
-       input_register_device(sermouse->dev);
+       err = input_register_device(sermouse->dev);
+       if (err)
+               goto fail3;
 
        return 0;
 
- fail: serio_set_drvdata(serio, NULL);
-       input_free_device(input_dev);
+ fail3:        serio_close(serio);
+ fail2:        serio_set_drvdata(serio, NULL);
+ fail1:        input_free_device(input_dev);
        kfree(sermouse);
        return err;
 }
index ffdb50eee93d6df28f0df7e7ac27fd4dca21dde0..ffd0d6624a8c726ed40913a22fb1cbd717ea264d 100644 (file)
@@ -497,7 +497,7 @@ vsxxxaa_connect (struct serio *serio, struct serio_driver *drv)
        mouse = kzalloc (sizeof (struct vsxxxaa), GFP_KERNEL);
        input_dev = input_allocate_device ();
        if (!mouse || !input_dev)
-               goto fail;
+               goto fail1;
 
        mouse->dev = input_dev;
        mouse->serio = serio;
@@ -527,7 +527,7 @@ vsxxxaa_connect (struct serio *serio, struct serio_driver *drv)
 
        err = serio_open (serio, drv);
        if (err)
-               goto fail;
+               goto fail2;
 
        /*
         * Request selftest. Standard packet format and differential
@@ -535,12 +535,15 @@ vsxxxaa_connect (struct serio *serio, struct serio_driver *drv)
         */
        serio->write (serio, 'T'); /* Test */
 
-       input_register_device (input_dev);
+       err = input_register_device (input_dev);
+       if (err)
+               goto fail3;
 
        return 0;
 
- fail: serio_set_drvdata (serio, NULL);
-       input_free_device (input_dev);
+ fail3:        serio_close (serio);
+ fail2:        serio_set_drvdata (serio, NULL);
+ fail1:        input_free_device (input_dev);
        kfree (mouse);
        return err;
 }