USB: class: cdc-acm: clean up urb->status usage
[safe/jmp/linux-2.6] / drivers / usb / image / mdc800.c
index 5791723..36502a0 100644 (file)
@@ -23,7 +23,7 @@
  *
  *
  * The driver brings the USB functions of the MDC800 to Linux.
- * To use the Camera you must support the USB Protocoll of the camera
+ * To use the Camera you must support the USB Protocol of the camera
  * to the Kernel Node.
  * The Driver uses a misc device Node. Create it with :
  * mknod /dev/mustek c 180 32
@@ -94,8 +94,8 @@
 #include <linux/init.h>
 #include <linux/slab.h>
 #include <linux/module.h>
-#include <linux/smp_lock.h>
 #include <linux/wait.h>
+#include <linux/mutex.h>
 
 #include <linux/usb.h>
 #include <linux/fs.h>
@@ -169,7 +169,7 @@ struct mdc800_data
        int                     out_count;      // Bytes in the buffer
 
        int                     open;           // Camera device open ?
-       struct semaphore        io_lock;        // IO -lock
+       struct mutex            io_lock;        // IO -lock
 
        char                    in [8];         // Command Input Buffer
        int                     in_count;
@@ -279,7 +279,7 @@ static int mdc800_isReady (char *ch)
 /*
  * USB IRQ Handler for InputLine
  */
-static void mdc800_usb_irq (struct urb *urb, struct pt_regs *res)
+static void mdc800_usb_irq (struct urb *urb)
 {
        int data_received=0, wake_up;
        unsigned char* b=urb->transfer_buffer;
@@ -373,7 +373,7 @@ static int mdc800_usb_waitForIRQ (int mode, int msec)
 /*
  * The write_urb callback function
  */
-static void mdc800_usb_write_notify (struct urb *urb, struct pt_regs *res)
+static void mdc800_usb_write_notify (struct urb *urb)
 {
        struct mdc800_data* mdc800=urb->context;
 
@@ -393,7 +393,7 @@ static void mdc800_usb_write_notify (struct urb *urb, struct pt_regs *res)
 /*
  * The download_urb callback function
  */
-static void mdc800_usb_download_notify (struct urb *urb, struct pt_regs *res)
+static void mdc800_usb_download_notify (struct urb *urb)
 {
        struct mdc800_data* mdc800=urb->context;
 
@@ -423,11 +423,10 @@ static void mdc800_usb_download_notify (struct urb *urb, struct pt_regs *res)
  ***************************************************************************/
 
 static struct usb_driver mdc800_usb_driver;
-static struct file_operations mdc800_device_ops;
+static const struct file_operations mdc800_device_ops;
 static struct usb_class_driver mdc800_class = {
-       .name =         "usb/mdc800%d",
+       .name =         "mdc800%d",
        .fops =         &mdc800_device_ops,
-       .mode =         S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
        .minor_base =   MDC800_DEVICE_MINOR_BASE,
 };
 
@@ -498,7 +497,7 @@ static int mdc800_usb_probe (struct usb_interface *intf,
 
        info ("Found Mustek MDC800 on USB.");
 
-       down (&mdc800->io_lock);
+       mutex_lock(&mdc800->io_lock);
 
        retval = usb_register_dev(intf, &mdc800_class);
        if (retval) {
@@ -543,7 +542,7 @@ static int mdc800_usb_probe (struct usb_interface *intf,
 
        mdc800->state=READY;
 
-       up (&mdc800->io_lock);
+       mutex_unlock(&mdc800->io_lock);
        
        usb_set_intfdata(intf, mdc800);
        return 0;
@@ -565,11 +564,15 @@ static void mdc800_usb_disconnect (struct usb_interface *intf)
 
                usb_deregister_dev(intf, &mdc800_class);
 
+               /* must be under lock to make sure no URB
+                  is submitted after usb_kill_urb() */
+               mutex_lock(&mdc800->io_lock);
                mdc800->state=NOT_CONNECTED;
 
                usb_kill_urb(mdc800->irq_urb);
                usb_kill_urb(mdc800->write_urb);
                usb_kill_urb(mdc800->download_urb);
+               mutex_unlock(&mdc800->io_lock);
 
                mdc800->dev = NULL;
                usb_set_intfdata(intf, NULL);
@@ -621,7 +624,7 @@ static int mdc800_device_open (struct inode* inode, struct file *file)
        int retval=0;
        int errn=0;
 
-       down (&mdc800->io_lock);
+       mutex_lock(&mdc800->io_lock);
        
        if (mdc800->state == NOT_CONNECTED)
        {
@@ -657,7 +660,7 @@ static int mdc800_device_open (struct inode* inode, struct file *file)
        dbg ("Mustek MDC800 device opened.");
 
 error_out:
-       up (&mdc800->io_lock);
+       mutex_unlock(&mdc800->io_lock);
        return errn;
 }
 
@@ -670,7 +673,7 @@ static int mdc800_device_release (struct inode* inode, struct file *file)
        int retval=0;
        dbg ("Mustek MDC800 device closed.");
 
-       down (&mdc800->io_lock);
+       mutex_lock(&mdc800->io_lock);
        if (mdc800->open && (mdc800->state != NOT_CONNECTED))
        {
                usb_kill_urb(mdc800->irq_urb);
@@ -683,7 +686,7 @@ static int mdc800_device_release (struct inode* inode, struct file *file)
                retval=-EIO;
        }
 
-       up(&mdc800->io_lock);
+       mutex_unlock(&mdc800->io_lock);
        return retval;
 }
 
@@ -696,21 +699,21 @@ static ssize_t mdc800_device_read (struct file *file, char __user *buf, size_t l
        size_t left=len, sts=len; /* single transfer size */
        char __user *ptr = buf;
 
-       down (&mdc800->io_lock);
+       mutex_lock(&mdc800->io_lock);
        if (mdc800->state == NOT_CONNECTED)
        {
-               up (&mdc800->io_lock);
+               mutex_unlock(&mdc800->io_lock);
                return -EBUSY;
        }
        if (mdc800->state == WORKING)
        {
                warn ("Illegal State \"working\" reached during read ?!");
-               up (&mdc800->io_lock);
+               mutex_unlock(&mdc800->io_lock);
                return -EBUSY;
        }
        if (!mdc800->open)
        {
-               up (&mdc800->io_lock);
+               mutex_unlock(&mdc800->io_lock);
                return -EBUSY;
        }
 
@@ -718,7 +721,7 @@ static ssize_t mdc800_device_read (struct file *file, char __user *buf, size_t l
        {
                if (signal_pending (current)) 
                {
-                       up (&mdc800->io_lock);
+                       mutex_unlock(&mdc800->io_lock);
                        return -EINTR;
                }
 
@@ -737,7 +740,7 @@ static ssize_t mdc800_device_read (struct file *file, char __user *buf, size_t l
                                if (usb_submit_urb (mdc800->download_urb, GFP_KERNEL))
                                {
                                        err ("Can't submit download urb (status=%i)",mdc800->download_urb->status);
-                                       up (&mdc800->io_lock);
+                                       mutex_unlock(&mdc800->io_lock);
                                        return len-left;
                                }
                                wait_event_timeout(mdc800->download_wait, mdc800->downloaded,
@@ -746,14 +749,14 @@ static ssize_t mdc800_device_read (struct file *file, char __user *buf, size_t l
                                if (mdc800->download_urb->status != 0)
                                {
                                        err ("request download-bytes fails (status=%i)",mdc800->download_urb->status);
-                                       up (&mdc800->io_lock);
+                                       mutex_unlock(&mdc800->io_lock);
                                        return len-left;
                                }
                        }
                        else
                        {
                                /* No more bytes -> that's an error*/
-                               up (&mdc800->io_lock);
+                               mutex_unlock(&mdc800->io_lock);
                                return -EIO;
                        }
                }
@@ -762,7 +765,7 @@ static ssize_t mdc800_device_read (struct file *file, char __user *buf, size_t l
                        /* Copy Bytes */
                        if (copy_to_user(ptr, &mdc800->out [mdc800->out_ptr],
                                                sts)) {
-                               up(&mdc800->io_lock);
+                               mutex_unlock(&mdc800->io_lock);
                                return -EFAULT;
                        }
                        ptr+=sts;
@@ -771,7 +774,7 @@ static ssize_t mdc800_device_read (struct file *file, char __user *buf, size_t l
                }
        }
 
-       up (&mdc800->io_lock);
+       mutex_unlock(&mdc800->io_lock);
        return len-left;
 }
 
@@ -786,15 +789,15 @@ static ssize_t mdc800_device_write (struct file *file, const char __user *buf, s
 {
        size_t i=0;
 
-       down (&mdc800->io_lock);
+       mutex_lock(&mdc800->io_lock);
        if (mdc800->state != READY)
        {
-               up (&mdc800->io_lock);
+               mutex_unlock(&mdc800->io_lock);
                return -EBUSY;
        }
        if (!mdc800->open )
        {
-               up (&mdc800->io_lock);
+               mutex_unlock(&mdc800->io_lock);
                return -EBUSY;
        }
 
@@ -803,13 +806,13 @@ static ssize_t mdc800_device_write (struct file *file, const char __user *buf, s
                unsigned char c;
                if (signal_pending (current)) 
                {
-                       up (&mdc800->io_lock);
+                       mutex_unlock(&mdc800->io_lock);
                        return -EINTR;
                }
                
                if(get_user(c, buf+i))
                {
-                       up(&mdc800->io_lock);
+                       mutex_unlock(&mdc800->io_lock);
                        return -EFAULT;
                }
 
@@ -830,7 +833,7 @@ static ssize_t mdc800_device_write (struct file *file, const char __user *buf, s
                }
                else
                {
-                       up (&mdc800->io_lock);
+                       mutex_unlock(&mdc800->io_lock);
                        return -EIO;
                }
 
@@ -842,7 +845,7 @@ static ssize_t mdc800_device_write (struct file *file, const char __user *buf, s
                        if (mdc800_usb_waitForIRQ (0,TO_GET_READY))
                        {
                                err ("Camera didn't get ready.\n");
-                               up (&mdc800->io_lock);
+                               mutex_unlock(&mdc800->io_lock);
                                return -EIO;
                        }
 
@@ -854,7 +857,7 @@ static ssize_t mdc800_device_write (struct file *file, const char __user *buf, s
                        if (usb_submit_urb (mdc800->write_urb, GFP_KERNEL))
                        {
                                err ("submitting write urb fails (status=%i)", mdc800->write_urb->status);
-                               up (&mdc800->io_lock);
+                               mutex_unlock(&mdc800->io_lock);
                                return -EIO;
                        }
                        wait_event_timeout(mdc800->write_wait, mdc800->written, TO_WRITE_GET_READY*HZ/1000);
@@ -862,7 +865,7 @@ static ssize_t mdc800_device_write (struct file *file, const char __user *buf, s
                        if (mdc800->state == WORKING)
                        {
                                usb_kill_urb(mdc800->write_urb);
-                               up (&mdc800->io_lock);
+                               mutex_unlock(&mdc800->io_lock);
                                return -EIO;
                        }
 
@@ -874,7 +877,7 @@ static ssize_t mdc800_device_write (struct file *file, const char __user *buf, s
                                        {
                                                err ("call 0x07 before 0x05,0x3e");
                                                mdc800->state=READY;
-                                               up (&mdc800->io_lock);
+                                               mutex_unlock(&mdc800->io_lock);
                                                return -EIO;
                                        }
                                        mdc800->pic_len=-1;
@@ -893,7 +896,7 @@ static ssize_t mdc800_device_write (struct file *file, const char __user *buf, s
                                                if (mdc800_usb_waitForIRQ (1,TO_READ_FROM_IRQ))
                                                {
                                                        err ("requesting answer from irq fails");
-                                                       up (&mdc800->io_lock);
+                                                       mutex_unlock(&mdc800->io_lock);
                                                        return -EIO;
                                                }
 
@@ -921,7 +924,7 @@ static ssize_t mdc800_device_write (struct file *file, const char __user *buf, s
                                                if (mdc800_usb_waitForIRQ (0,TO_DEFAULT_COMMAND))
                                                {
                                                        err ("Command Timeout.");
-                                                       up (&mdc800->io_lock);
+                                                       mutex_unlock(&mdc800->io_lock);
                                                        return -EIO;
                                                }
                                        }
@@ -931,7 +934,7 @@ static ssize_t mdc800_device_write (struct file *file, const char __user *buf, s
                }
                i++;
        }
-       up (&mdc800->io_lock);
+       mutex_unlock(&mdc800->io_lock);
        return i;
 }
 
@@ -941,7 +944,7 @@ static ssize_t mdc800_device_write (struct file *file, const char __user *buf, s
 ****************************************************************************/
 
 /* File Operations of this drivers */
-static struct file_operations mdc800_device_ops =
+static const struct file_operations mdc800_device_ops =
 {
        .owner =        THIS_MODULE,
        .read =         mdc800_device_read,
@@ -963,7 +966,6 @@ MODULE_DEVICE_TABLE (usb, mdc800_table);
  */
 static struct usb_driver mdc800_usb_driver =
 {
-       .owner =        THIS_MODULE,
        .name =         "mdc800",
        .probe =        mdc800_usb_probe,
        .disconnect =   mdc800_usb_disconnect,
@@ -976,19 +978,17 @@ static struct usb_driver mdc800_usb_driver =
        Init and Cleanup this driver (Main Functions)
 *************************************************************************/
 
-#define try(A)           if (!(A)) goto cleanup_on_fail;
-
 static int __init usb_mdc800_init (void)
 {
        int retval = -ENODEV;
        /* Allocate Memory */
-       try (mdc800=kmalloc (sizeof (struct mdc800_data), GFP_KERNEL));
+       mdc800=kzalloc (sizeof (struct mdc800_data), GFP_KERNEL);
+       if (!mdc800)
+               goto cleanup_on_fail;
 
-       memset(mdc800, 0, sizeof(struct mdc800_data));
        mdc800->dev = NULL;
-       mdc800->open=0;
        mdc800->state=NOT_CONNECTED;
-       init_MUTEX (&mdc800->io_lock);
+       mutex_init (&mdc800->io_lock);
 
        init_waitqueue_head (&mdc800->irq_wait);
        init_waitqueue_head (&mdc800->write_wait);
@@ -998,13 +998,25 @@ static int __init usb_mdc800_init (void)
        mdc800->downloaded = 0;
        mdc800->written = 0;
 
-       try (mdc800->irq_urb_buffer=kmalloc (8, GFP_KERNEL));
-       try (mdc800->write_urb_buffer=kmalloc (8, GFP_KERNEL));
-       try (mdc800->download_urb_buffer=kmalloc (64, GFP_KERNEL));
+       mdc800->irq_urb_buffer=kmalloc (8, GFP_KERNEL);
+       if (!mdc800->irq_urb_buffer)
+               goto cleanup_on_fail;
+       mdc800->write_urb_buffer=kmalloc (8, GFP_KERNEL);
+       if (!mdc800->write_urb_buffer)
+               goto cleanup_on_fail;
+       mdc800->download_urb_buffer=kmalloc (64, GFP_KERNEL);
+       if (!mdc800->download_urb_buffer)
+               goto cleanup_on_fail;
 
-       try (mdc800->irq_urb=usb_alloc_urb (0, GFP_KERNEL));
-       try (mdc800->download_urb=usb_alloc_urb (0, GFP_KERNEL));
-       try (mdc800->write_urb=usb_alloc_urb (0, GFP_KERNEL));
+       mdc800->irq_urb=usb_alloc_urb (0, GFP_KERNEL);
+       if (!mdc800->irq_urb)
+               goto cleanup_on_fail;
+       mdc800->download_urb=usb_alloc_urb (0, GFP_KERNEL);
+       if (!mdc800->download_urb)
+               goto cleanup_on_fail;
+       mdc800->write_urb=usb_alloc_urb (0, GFP_KERNEL);
+       if (!mdc800->write_urb)
+               goto cleanup_on_fail;
 
        /* Register the driver */
        retval = usb_register(&mdc800_usb_driver);