USB: always visit drivers/usb/misc/
[cascardo/linux.git] / drivers / usb / misc / adutux.c
index 75bfab9..5131cbf 100644 (file)
@@ -24,6 +24,7 @@
 #include <linux/slab.h>
 #include <linux/module.h>
 #include <linux/usb.h>
+#include <linux/mutex.h>
 #include <asm/uaccess.h>
 
 #ifdef CONFIG_USB_DEBUG
@@ -80,7 +81,7 @@ MODULE_DEVICE_TABLE(usb, device_table);
 
 /* Structure to hold all of our device specific stuff */
 struct adu_device {
-       struct semaphore        sem; /* locks this structure */
+       struct mutex            mtx; /* locks this structure */
        struct usb_device*      udev; /* save off the usb device pointer */
        struct usb_interface*   interface;
        unsigned char           minor; /* the starting minor number for this device */
@@ -108,8 +109,6 @@ struct adu_device {
        struct urb*             interrupt_out_urb;
 };
 
-/* prevent races between open() and disconnect */
-static DEFINE_MUTEX(disconnect_mutex);
 static struct usb_driver adu_driver;
 
 static void adu_debug_data(int level, const char *function, int size,
@@ -180,17 +179,19 @@ static void adu_delete(struct adu_device *dev)
 static void adu_interrupt_in_callback(struct urb *urb)
 {
        struct adu_device *dev = urb->context;
+       int status = urb->status;
 
-       dbg(4," %s : enter, status %d", __FUNCTION__, urb->status);
+       dbg(4," %s : enter, status %d", __FUNCTION__, status);
        adu_debug_data(5, __FUNCTION__, urb->actual_length,
                       urb->transfer_buffer);
 
        spin_lock(&dev->buflock);
 
-       if (urb->status != 0) {
-               if ((urb->status != -ENOENT) && (urb->status != -ECONNRESET)) {
+       if (status != 0) {
+               if ((status != -ENOENT) && (status != -ECONNRESET) &&
+                       (status != -ESHUTDOWN)) {
                        dbg(1," %s : nonzero status received: %d",
-                           __FUNCTION__, urb->status);
+                           __FUNCTION__, status);
                }
                goto exit;
        }
@@ -218,21 +219,22 @@ exit:
        wake_up_interruptible(&dev->read_wait);
        adu_debug_data(5, __FUNCTION__, urb->actual_length,
                       urb->transfer_buffer);
-       dbg(4," %s : leave, status %d", __FUNCTION__, urb->status);
+       dbg(4," %s : leave, status %d", __FUNCTION__, status);
 }
 
 static void adu_interrupt_out_callback(struct urb *urb)
 {
        struct adu_device *dev = urb->context;
+       int status = urb->status;
 
-       dbg(4," %s : enter, status %d", __FUNCTION__, urb->status);
+       dbg(4," %s : enter, status %d", __FUNCTION__, status);
        adu_debug_data(5,__FUNCTION__, urb->actual_length, urb->transfer_buffer);
 
-       if (urb->status != 0) {
-               if ((urb->status != -ENOENT) &&
-                   (urb->status != -ECONNRESET)) {
+       if (status != 0) {
+               if ((status != -ENOENT) &&
+                   (status != -ECONNRESET)) {
                        dbg(1, " %s :nonzero status received: %d",
-                           __FUNCTION__, urb->status);
+                           __FUNCTION__, status);
                }
                goto exit;
        }
@@ -242,7 +244,7 @@ exit:
 
        adu_debug_data(5, __FUNCTION__, urb->actual_length,
                       urb->transfer_buffer);
-       dbg(4," %s : leave, status %d", __FUNCTION__, urb->status);
+       dbg(4," %s : leave, status %d", __FUNCTION__, status);
 }
 
 static int adu_open(struct inode *inode, struct file *file)
@@ -256,8 +258,6 @@ static int adu_open(struct inode *inode, struct file *file)
 
        subminor = iminor(inode);
 
-       mutex_lock(&disconnect_mutex);
-
        interface = usb_find_interface(&adu_driver, subminor);
        if (!interface) {
                err("%s - error, can't find device for minor %d",
@@ -273,8 +273,8 @@ static int adu_open(struct inode *inode, struct file *file)
        }
 
        /* lock this device */
-       if ((retval = down_interruptible(&dev->sem))) {
-               dbg(2, "%s : sem down failed", __FUNCTION__);
+       if ((retval = mutex_lock_interruptible(&dev->mtx))) {
+               dbg(2, "%s : mutex lock failed", __FUNCTION__);
                goto exit_no_device;
        }
 
@@ -285,27 +285,27 @@ static int adu_open(struct inode *inode, struct file *file)
        /* save device in the file's private structure */
        file->private_data = dev;
 
-       /* initialize in direction */
-       dev->read_buffer_length = 0;
-
-       /* fixup first read by having urb waiting for it */
-       usb_fill_int_urb(dev->interrupt_in_urb,dev->udev,
-                        usb_rcvintpipe(dev->udev,
-                                       dev->interrupt_in_endpoint->bEndpointAddress),
-                        dev->interrupt_in_buffer,
-                        le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize),
-                        adu_interrupt_in_callback, dev,
-                        dev->interrupt_in_endpoint->bInterval);
-       /* dev->interrupt_in_urb->transfer_flags |= URB_ASYNC_UNLINK; */
-       dev->read_urb_finished = 0;
-       usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
-       /* we ignore failure */
-       /* end of fixup for first read */
-
-       up(&dev->sem);
+       if (dev->open_count == 1) {
+               /* initialize in direction */
+               dev->read_buffer_length = 0;
+
+               /* fixup first read by having urb waiting for it */
+               usb_fill_int_urb(dev->interrupt_in_urb,dev->udev,
+                                usb_rcvintpipe(dev->udev,
+                                               dev->interrupt_in_endpoint->bEndpointAddress),
+                                dev->interrupt_in_buffer,
+                                le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize),
+                                adu_interrupt_in_callback, dev,
+                                dev->interrupt_in_endpoint->bInterval);
+               /* dev->interrupt_in_urb->transfer_flags |= URB_ASYNC_UNLINK; */
+               dev->read_urb_finished = 0;
+               retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
+               if (retval)
+                       --dev->open_count;
+       }
+       mutex_unlock(&dev->mtx);
 
 exit_no_device:
-       mutex_unlock(&disconnect_mutex);
        dbg(2,"%s : leave, return value %d ", __FUNCTION__, retval);
 
        return retval;
@@ -317,12 +317,6 @@ static int adu_release_internal(struct adu_device *dev)
 
        dbg(2," %s : enter", __FUNCTION__);
 
-       if (dev->udev == NULL) {
-               /* the device was unplugged before the file was released */
-               adu_delete(dev);
-               goto exit;
-       }
-
        /* decrement our usage count for the device */
        --dev->open_count;
        dbg(2," %s : open count %d", __FUNCTION__, dev->open_count);
@@ -331,7 +325,6 @@ static int adu_release_internal(struct adu_device *dev)
                dev->open_count = 0;
        }
 
-exit:
        dbg(2," %s : leave", __FUNCTION__);
        return retval;
 }
@@ -358,7 +351,7 @@ static int adu_release(struct inode *inode, struct file *file)
        }
 
        /* lock our device */
-       down(&dev->sem); /* not interruptible */
+       mutex_lock(&dev->mtx); /* not interruptible */
 
        if (dev->open_count <= 0) {
                dbg(1," %s : device not opened", __FUNCTION__);
@@ -366,12 +359,19 @@ static int adu_release(struct inode *inode, struct file *file)
                goto exit;
        }
 
-       /* do the work */
-       retval = adu_release_internal(dev);
+       if (dev->udev == NULL) {
+               /* the device was unplugged before the file was released */
+               mutex_unlock(&dev->mtx);
+               adu_delete(dev);
+               dev = NULL;
+       } else {
+               /* do the work */
+               retval = adu_release_internal(dev);
+       }
 
 exit:
        if (dev)
-               up(&dev->sem);
+               mutex_unlock(&dev->mtx);
        dbg(2," %s : leave, return value %d", __FUNCTION__, retval);
        return retval;
 }
@@ -394,7 +394,7 @@ static ssize_t adu_read(struct file *file, __user char *buffer, size_t count,
        dev = file->private_data;
        dbg(2," %s : dev=%p", __FUNCTION__, dev);
        /* lock this object */
-       if (down_interruptible(&dev->sem))
+       if (mutex_lock_interruptible(&dev->mtx))
                return -ERESTARTSYS;
 
        /* verify that the device wasn't unplugged */
@@ -469,7 +469,7 @@ static ssize_t adu_read(struct file *file, __user char *buffer, size_t count,
                                                         adu_interrupt_in_callback,
                                                         dev,
                                                         dev->interrupt_in_endpoint->bInterval);
-                                       retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
+                                       retval = usb_submit_urb(dev->interrupt_in_urb, GFP_ATOMIC);
                                        if (!retval) {
                                                spin_unlock_irqrestore(&dev->buflock, flags);
                                                dbg(2," %s : submitted OK", __FUNCTION__);
@@ -526,7 +526,7 @@ static ssize_t adu_read(struct file *file, __user char *buffer, size_t count,
 
 exit:
        /* unlock the device */
-       up(&dev->sem);
+       mutex_unlock(&dev->mtx);
 
        dbg(2," %s : leave, return value %d", __FUNCTION__, retval);
        return retval;
@@ -539,7 +539,7 @@ static ssize_t adu_write(struct file *file, const __user char *buffer,
        size_t bytes_written = 0;
        size_t bytes_to_write;
        size_t buffer_size;
-       int retval = 0;
+       int retval;
        int timeout = 0;
 
        dbg(2," %s : enter, count = %Zd", __FUNCTION__, count);
@@ -547,7 +547,9 @@ static ssize_t adu_write(struct file *file, const __user char *buffer,
        dev = file->private_data;
 
        /* lock this object */
-       down_interruptible(&dev->sem);
+       retval = mutex_lock_interruptible(&dev->mtx);
+       if (retval)
+               goto exit_nolock;
 
        /* verify that the device wasn't unplugged */
        if (dev->udev == NULL || dev->minor == 0) {
@@ -573,9 +575,13 @@ static ssize_t adu_write(struct file *file, const __user char *buffer,
                                retval = -EINTR;
                                goto exit;
                        }
-                       up(&dev->sem);
+                       mutex_unlock(&dev->mtx);
                        timeout = interruptible_sleep_on_timeout(&dev->write_wait, timeout);
-                       down_interruptible(&dev->sem);
+                       retval = mutex_lock_interruptible(&dev->mtx);
+                       if (retval) {
+                               retval = bytes_written ? bytes_written : retval;
+                               goto exit_nolock;
+                       }
                        if (timeout > 0) {
                                break;
                        }
@@ -636,7 +642,8 @@ static ssize_t adu_write(struct file *file, const __user char *buffer,
 
 exit:
        /* unlock the device */
-       up(&dev->sem);
+       mutex_unlock(&dev->mtx);
+exit_nolock:
 
        dbg(2," %s : leave, return value %d", __FUNCTION__, retval);
 
@@ -695,7 +702,7 @@ static int adu_probe(struct usb_interface *interface,
                goto exit;
        }
 
-       init_MUTEX(&dev->sem);
+       mutex_init(&dev->mtx);
        spin_lock_init(&dev->buflock);
        dev->udev = udev;
        init_waitqueue_head(&dev->read_wait);
@@ -823,31 +830,27 @@ static void adu_disconnect(struct usb_interface *interface)
 
        dbg(2," %s : enter", __FUNCTION__);
 
-       mutex_lock(&disconnect_mutex); /* not interruptible */
-
        dev = usb_get_intfdata(interface);
        usb_set_intfdata(interface, NULL);
 
-       down(&dev->sem); /* not interruptible */
-
        minor = dev->minor;
 
        /* give back our minor */
        usb_deregister_dev(interface, &adu_class);
        dev->minor = 0;
 
+       mutex_lock(&dev->mtx); /* not interruptible */
+
        /* if the device is not opened, then we clean up right now */
        dbg(2," %s : open count %d", __FUNCTION__, dev->open_count);
        if (!dev->open_count) {
-               up(&dev->sem);
+               mutex_unlock(&dev->mtx);
                adu_delete(dev);
        } else {
                dev->udev = NULL;
-               up(&dev->sem);
+               mutex_unlock(&dev->mtx);
        }
 
-       mutex_unlock(&disconnect_mutex);
-
        dev_info(&interface->dev, "ADU device adutux%d now disconnected",
                 (minor - ADU_MINOR_BASE));