]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - drivers/usb/misc/adutux.c
USB: remove info() macro from usb/misc drivers
[linux-2.6-omap-h63xx.git] / drivers / usb / misc / adutux.c
index c567aa7a41ead3643de3c38ab4b286a3c13c283b..92f58ccc8fb3f6efb562329c61ff71ee0bdb2fd3 100644 (file)
@@ -79,12 +79,22 @@ MODULE_DEVICE_TABLE(usb, device_table);
 
 #define COMMAND_TIMEOUT        (2*HZ)  /* 60 second timeout for a command */
 
+/*
+ * The locking scheme is a vanilla 3-lock:
+ *   adu_device.buflock: A spinlock, covers what IRQs touch.
+ *   adutux_mutex:       A Static lock to cover open_count. It would also cover
+ *                       any globals, but we don't have them in 2.6.
+ *   adu_device.mtx:     A mutex to hold across sleepers like copy_from_user.
+ *                       It covers all of adu_device, except the open_count
+ *                       and what .buflock covers.
+ */
+
 /* Structure to hold all of our device specific stuff */
 struct adu_device {
-       struct mutex            mtx; /* locks this structure */
+       struct mutex            mtx;
        struct usb_device*      udev; /* save off the usb device pointer */
        struct usb_interface*   interface;
-       unsigned char           minor; /* the starting minor number for this device */
+       unsigned int            minor; /* the starting minor number for this device */
        char                    serial_number[8];
 
        int                     open_count; /* number of times this port has been opened */
@@ -107,8 +117,11 @@ struct adu_device {
        char*                   interrupt_out_buffer;
        struct usb_endpoint_descriptor* interrupt_out_endpoint;
        struct urb*             interrupt_out_urb;
+       int                     out_urb_finished;
 };
 
+static DEFINE_MUTEX(adutux_mutex);
+
 static struct usb_driver adu_driver;
 
 static void adu_debug_data(int level, const char *function, int size,
@@ -132,37 +145,39 @@ static void adu_debug_data(int level, const char *function, int size,
  */
 static void adu_abort_transfers(struct adu_device *dev)
 {
-       dbg(2," %s : enter", __FUNCTION__);
+       unsigned long flags;
 
-       if (dev == NULL) {
-               dbg(1," %s : dev is null", __FUNCTION__);
-               goto exit;
-       }
+       dbg(2," %s : enter", __func__);
 
        if (dev->udev == NULL) {
-               dbg(1," %s : udev is null", __FUNCTION__);
-               goto exit;
-       }
-
-       dbg(2," %s : udev state %d", __FUNCTION__, dev->udev->state);
-       if (dev->udev->state == USB_STATE_NOTATTACHED) {
-               dbg(1," %s : udev is not attached", __FUNCTION__);
+               dbg(1," %s : udev is null", __func__);
                goto exit;
        }
 
        /* shutdown transfer */
-       usb_unlink_urb(dev->interrupt_in_urb);
-       usb_unlink_urb(dev->interrupt_out_urb);
+
+       /* XXX Anchor these instead */
+       spin_lock_irqsave(&dev->buflock, flags);
+       if (!dev->read_urb_finished) {
+               spin_unlock_irqrestore(&dev->buflock, flags);
+               usb_kill_urb(dev->interrupt_in_urb);
+       } else
+               spin_unlock_irqrestore(&dev->buflock, flags);
+
+       spin_lock_irqsave(&dev->buflock, flags);
+       if (!dev->out_urb_finished) {
+               spin_unlock_irqrestore(&dev->buflock, flags);
+               usb_kill_urb(dev->interrupt_out_urb);
+       } else
+               spin_unlock_irqrestore(&dev->buflock, flags);
 
 exit:
-       dbg(2," %s : leave", __FUNCTION__);
+       dbg(2," %s : leave", __func__);
 }
 
 static void adu_delete(struct adu_device *dev)
 {
-       dbg(2, "%s enter", __FUNCTION__);
-
-       adu_abort_transfers(dev);
+       dbg(2, "%s enter", __func__);
 
        /* free data structures */
        usb_free_urb(dev->interrupt_in_urb);
@@ -173,7 +188,7 @@ static void adu_delete(struct adu_device *dev)
        kfree(dev->interrupt_out_buffer);
        kfree(dev);
 
-       dbg(2, "%s : leave", __FUNCTION__);
+       dbg(2, "%s : leave", __func__);
 }
 
 static void adu_interrupt_in_callback(struct urb *urb)
@@ -181,8 +196,8 @@ 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__, status);
-       adu_debug_data(5, __FUNCTION__, urb->actual_length,
+       dbg(4," %s : enter, status %d", __func__, status);
+       adu_debug_data(5, __func__, urb->actual_length,
                       urb->transfer_buffer);
 
        spin_lock(&dev->buflock);
@@ -191,7 +206,7 @@ static void adu_interrupt_in_callback(struct urb *urb)
                if ((status != -ENOENT) && (status != -ECONNRESET) &&
                        (status != -ESHUTDOWN)) {
                        dbg(1," %s : nonzero status received: %d",
-                           __FUNCTION__, status);
+                           __func__, status);
                }
                goto exit;
        }
@@ -205,10 +220,10 @@ static void adu_interrupt_in_callback(struct urb *urb)
                                dev->interrupt_in_buffer, urb->actual_length);
 
                        dev->read_buffer_length += urb->actual_length;
-                       dbg(2," %s reading  %d ", __FUNCTION__,
+                       dbg(2," %s reading  %d ", __func__,
                            urb->actual_length);
                } else {
-                       dbg(1," %s : read_buffer overflow", __FUNCTION__);
+                       dbg(1," %s : read_buffer overflow", __func__);
                }
        }
 
@@ -217,9 +232,9 @@ exit:
        spin_unlock(&dev->buflock);
        /* always wake up so we recover from errors */
        wake_up_interruptible(&dev->read_wait);
-       adu_debug_data(5, __FUNCTION__, urb->actual_length,
+       adu_debug_data(5, __func__, urb->actual_length,
                       urb->transfer_buffer);
-       dbg(4," %s : leave, status %d", __FUNCTION__, status);
+       dbg(4," %s : leave, status %d", __func__, status);
 }
 
 static void adu_interrupt_out_callback(struct urb *urb)
@@ -227,24 +242,27 @@ 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__, status);
-       adu_debug_data(5,__FUNCTION__, urb->actual_length, urb->transfer_buffer);
+       dbg(4," %s : enter, status %d", __func__, status);
+       adu_debug_data(5,__func__, urb->actual_length, urb->transfer_buffer);
 
        if (status != 0) {
                if ((status != -ENOENT) &&
                    (status != -ECONNRESET)) {
                        dbg(1, " %s :nonzero status received: %d",
-                           __FUNCTION__, status);
+                           __func__, status);
                }
                goto exit;
        }
 
-       wake_up_interruptible(&dev->write_wait);
+       spin_lock(&dev->buflock);
+       dev->out_urb_finished = 1;
+       wake_up(&dev->write_wait);
+       spin_unlock(&dev->buflock);
 exit:
 
-       adu_debug_data(5, __FUNCTION__, urb->actual_length,
+       adu_debug_data(5, __func__, urb->actual_length,
                       urb->transfer_buffer);
-       dbg(4," %s : leave, status %d", __FUNCTION__, status);
+       dbg(4," %s : leave, status %d", __func__, status);
 }
 
 static int adu_open(struct inode *inode, struct file *file)
@@ -252,127 +270,125 @@ static int adu_open(struct inode *inode, struct file *file)
        struct adu_device *dev = NULL;
        struct usb_interface *interface;
        int subminor;
-       int retval = 0;
+       int retval;
 
-       dbg(2,"%s : enter", __FUNCTION__);
+       dbg(2,"%s : enter", __func__);
 
        subminor = iminor(inode);
 
+       if ((retval = mutex_lock_interruptible(&adutux_mutex))) {
+               dbg(2, "%s : mutex lock failed", __func__);
+               goto exit_no_lock;
+       }
+
        interface = usb_find_interface(&adu_driver, subminor);
        if (!interface) {
                err("%s - error, can't find device for minor %d",
-                   __FUNCTION__, subminor);
+                   __func__, subminor);
                retval = -ENODEV;
                goto exit_no_device;
        }
 
        dev = usb_get_intfdata(interface);
-       if (!dev) {
+       if (!dev || !dev->udev) {
                retval = -ENODEV;
                goto exit_no_device;
        }
 
-       /* lock this device */
-       if ((retval = mutex_lock_interruptible(&dev->mtx))) {
-               dbg(2, "%s : mutex lock failed", __FUNCTION__);
+       /* check that nobody else is using the device */
+       if (dev->open_count) {
+               retval = -EBUSY;
                goto exit_no_device;
        }
 
-       /* increment our usage count for the device */
        ++dev->open_count;
-       dbg(2,"%s : open count %d", __FUNCTION__, dev->open_count);
+       dbg(2,"%s : open count %d", __func__, dev->open_count);
 
        /* save device in the file's private structure */
        file->private_data = dev;
 
-       if (dev->open_count == 1) {
-               /* initialize in direction */
-               dev->read_buffer_length = 0;
+       /* 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);
+       /* 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->read_urb_finished = 0;
+       if (usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL))
+               dev->read_urb_finished = 1;
+       /* we ignore failure */
+       /* end of fixup for first read */
 
-exit_no_device:
-       dbg(2,"%s : leave, return value %d ", __FUNCTION__, retval);
+       /* initialize out direction */
+       dev->out_urb_finished = 1;
+
+       retval = 0;
 
+exit_no_device:
+       mutex_unlock(&adutux_mutex);
+exit_no_lock:
+       dbg(2,"%s : leave, return value %d ", __func__, retval);
        return retval;
 }
 
-static int adu_release_internal(struct adu_device *dev)
+static void adu_release_internal(struct adu_device *dev)
 {
-       int retval = 0;
-
-       dbg(2," %s : enter", __FUNCTION__);
+       dbg(2," %s : enter", __func__);
 
        /* decrement our usage count for the device */
        --dev->open_count;
-       dbg(2," %s : open count %d", __FUNCTION__, dev->open_count);
+       dbg(2," %s : open count %d", __func__, dev->open_count);
        if (dev->open_count <= 0) {
                adu_abort_transfers(dev);
                dev->open_count = 0;
        }
 
-       dbg(2," %s : leave", __FUNCTION__);
-       return retval;
+       dbg(2," %s : leave", __func__);
 }
 
 static int adu_release(struct inode *inode, struct file *file)
 {
-       struct adu_device *dev = NULL;
+       struct adu_device *dev;
        int retval = 0;
 
-       dbg(2," %s : enter", __FUNCTION__);
+       dbg(2," %s : enter", __func__);
 
        if (file == NULL) {
-               dbg(1," %s : file is NULL", __FUNCTION__);
+               dbg(1," %s : file is NULL", __func__);
                retval = -ENODEV;
                goto exit;
        }
 
        dev = file->private_data;
-
        if (dev == NULL) {
-               dbg(1," %s : object is NULL", __FUNCTION__);
+               dbg(1," %s : object is NULL", __func__);
                retval = -ENODEV;
                goto exit;
        }
 
-       /* lock our device */
-       mutex_lock(&dev->mtx); /* not interruptible */
+       mutex_lock(&adutux_mutex); /* not interruptible */
 
        if (dev->open_count <= 0) {
-               dbg(1," %s : device not opened", __FUNCTION__);
+               dbg(1," %s : device not opened", __func__);
                retval = -ENODEV;
                goto exit;
        }
 
+       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);
+               if (!dev->open_count)   /* ... and we're the last user */
+                       adu_delete(dev);
        }
 
 exit:
-       if (dev)
-               mutex_unlock(&dev->mtx);
-       dbg(2," %s : leave, return value %d", __FUNCTION__, retval);
+       mutex_unlock(&adutux_mutex);
+       dbg(2," %s : leave, return value %d", __func__, retval);
        return retval;
 }
 
@@ -389,16 +405,16 @@ static ssize_t adu_read(struct file *file, __user char *buffer, size_t count,
        unsigned long flags;
        DECLARE_WAITQUEUE(wait, current);
 
-       dbg(2," %s : enter, count = %Zd, file=%p", __FUNCTION__, count, file);
+       dbg(2," %s : enter, count = %Zd, file=%p", __func__, count, file);
 
        dev = file->private_data;
-       dbg(2," %s : dev=%p", __FUNCTION__, dev);
-       /* lock this object */
+       dbg(2," %s : dev=%p", __func__, dev);
+
        if (mutex_lock_interruptible(&dev->mtx))
                return -ERESTARTSYS;
 
        /* verify that the device wasn't unplugged */
-       if (dev->udev == NULL || dev->minor == 0) {
+       if (dev->udev == NULL) {
                retval = -ENODEV;
                err("No device or device unplugged %d", retval);
                goto exit;
@@ -406,16 +422,16 @@ static ssize_t adu_read(struct file *file, __user char *buffer, size_t count,
 
        /* verify that some data was requested */
        if (count == 0) {
-               dbg(1," %s : read request of 0 bytes", __FUNCTION__);
+               dbg(1," %s : read request of 0 bytes", __func__);
                goto exit;
        }
 
        timeout = COMMAND_TIMEOUT;
-       dbg(2," %s : about to start looping", __FUNCTION__);
+       dbg(2," %s : about to start looping", __func__);
        while (bytes_to_read) {
                int data_in_secondary = dev->secondary_tail - dev->secondary_head;
                dbg(2," %s : while, data_in_secondary=%d, status=%d",
-                   __FUNCTION__, data_in_secondary,
+                   __func__, data_in_secondary,
                    dev->interrupt_in_urb->status);
 
                if (data_in_secondary) {
@@ -440,7 +456,7 @@ static ssize_t adu_read(struct file *file, __user char *buffer, size_t count,
                                /* we secure access to the primary */
                                char *tmp;
                                dbg(2," %s : swap, read_buffer_length = %d",
-                                   __FUNCTION__, dev->read_buffer_length);
+                                   __func__, dev->read_buffer_length);
                                tmp = dev->read_buffer_secondary;
                                dev->read_buffer_secondary = dev->read_buffer_primary;
                                dev->read_buffer_primary = tmp;
@@ -452,14 +468,15 @@ static ssize_t adu_read(struct file *file, __user char *buffer, size_t count,
                                should_submit = 1;
                        } else {
                                /* even the primary was empty - we may need to do IO */
-                               if (dev->interrupt_in_urb->status == -EINPROGRESS) {
+                               if (!dev->read_urb_finished) {
                                        /* somebody is doing IO */
                                        spin_unlock_irqrestore(&dev->buflock, flags);
-                                       dbg(2," %s : submitted already", __FUNCTION__);
+                                       dbg(2," %s : submitted already", __func__);
                                } else {
                                        /* we must initiate input */
-                                       dbg(2," %s : initiate input", __FUNCTION__);
+                                       dbg(2," %s : initiate input", __func__);
                                        dev->read_urb_finished = 0;
+                                       spin_unlock_irqrestore(&dev->buflock, flags);
 
                                        usb_fill_int_urb(dev->interrupt_in_urb,dev->udev,
                                                         usb_rcvintpipe(dev->udev,
@@ -469,16 +486,13 @@ 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_ATOMIC);
-                                       if (!retval) {
-                                               spin_unlock_irqrestore(&dev->buflock, flags);
-                                               dbg(2," %s : submitted OK", __FUNCTION__);
-                                       } else {
+                                       retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
+                                       if (retval) {
+                                               dev->read_urb_finished = 1;
                                                if (retval == -ENOMEM) {
                                                        retval = bytes_read ? bytes_read : -ENOMEM;
                                                }
-                                               spin_unlock_irqrestore(&dev->buflock, flags);
-                                               dbg(2," %s : submit failed", __FUNCTION__);
+                                               dbg(2," %s : submit failed", __func__);
                                                goto exit;
                                        }
                                }
@@ -486,20 +500,24 @@ static ssize_t adu_read(struct file *file, __user char *buffer, size_t count,
                                /* we wait for I/O to complete */
                                set_current_state(TASK_INTERRUPTIBLE);
                                add_wait_queue(&dev->read_wait, &wait);
-                               if (!dev->read_urb_finished)
+                               spin_lock_irqsave(&dev->buflock, flags);
+                               if (!dev->read_urb_finished) {
+                                       spin_unlock_irqrestore(&dev->buflock, flags);
                                        timeout = schedule_timeout(COMMAND_TIMEOUT);
-                               else
+                               } else {
+                                       spin_unlock_irqrestore(&dev->buflock, flags);
                                        set_current_state(TASK_RUNNING);
+                               }
                                remove_wait_queue(&dev->read_wait, &wait);
 
                                if (timeout <= 0) {
-                                       dbg(2," %s : timeout", __FUNCTION__);
+                                       dbg(2," %s : timeout", __func__);
                                        retval = bytes_read ? bytes_read : -ETIMEDOUT;
                                        goto exit;
                                }
 
                                if (signal_pending(current)) {
-                                       dbg(2," %s : signal pending", __FUNCTION__);
+                                       dbg(2," %s : signal pending", __func__);
                                        retval = bytes_read ? bytes_read : -EINTR;
                                        goto exit;
                                }
@@ -509,50 +527,54 @@ static ssize_t adu_read(struct file *file, __user char *buffer, size_t count,
 
        retval = bytes_read;
        /* if the primary buffer is empty then use it */
-       if (should_submit && !dev->interrupt_in_urb->status==-EINPROGRESS) {
+       spin_lock_irqsave(&dev->buflock, flags);
+       if (should_submit && dev->read_urb_finished) {
+               dev->read_urb_finished = 0;
+               spin_unlock_irqrestore(&dev->buflock, flags);
                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);
+                               dev->interrupt_in_buffer,
+                               le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize),
+                               adu_interrupt_in_callback,
+                               dev,
+                               dev->interrupt_in_endpoint->bInterval);
+               if (usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL) != 0)
+                       dev->read_urb_finished = 1;
                /* we ignore failure */
+       } else {
+               spin_unlock_irqrestore(&dev->buflock, flags);
        }
 
 exit:
        /* unlock the device */
        mutex_unlock(&dev->mtx);
 
-       dbg(2," %s : leave, return value %d", __FUNCTION__, retval);
+       dbg(2," %s : leave, return value %d", __func__, retval);
        return retval;
 }
 
 static ssize_t adu_write(struct file *file, const __user char *buffer,
                         size_t count, loff_t *ppos)
 {
+       DECLARE_WAITQUEUE(waita, current);
        struct adu_device *dev;
        size_t bytes_written = 0;
        size_t bytes_to_write;
        size_t buffer_size;
+       unsigned long flags;
        int retval;
-       int timeout = 0;
 
-       dbg(2," %s : enter, count = %Zd", __FUNCTION__, count);
+       dbg(2," %s : enter, count = %Zd", __func__, count);
 
        dev = file->private_data;
 
-       /* lock this object */
        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) {
+       if (dev->udev == NULL) {
                retval = -ENODEV;
                err("No device or device unplugged %d", retval);
                goto exit;
@@ -560,53 +582,48 @@ static ssize_t adu_write(struct file *file, const __user char *buffer,
 
        /* verify that we actually have some data to write */
        if (count == 0) {
-               dbg(1," %s : write request of 0 bytes", __FUNCTION__);
+               dbg(1," %s : write request of 0 bytes", __func__);
                goto exit;
        }
 
-
        while (count > 0) {
-               if (dev->interrupt_out_urb->status == -EINPROGRESS) {
-                       timeout = COMMAND_TIMEOUT;
+               add_wait_queue(&dev->write_wait, &waita);
+               set_current_state(TASK_INTERRUPTIBLE);
+               spin_lock_irqsave(&dev->buflock, flags);
+               if (!dev->out_urb_finished) {
+                       spin_unlock_irqrestore(&dev->buflock, flags);
 
-                       while (timeout > 0) {
-                               if (signal_pending(current)) {
-                               dbg(1," %s : interrupted", __FUNCTION__);
+                       mutex_unlock(&dev->mtx);
+                       if (signal_pending(current)) {
+                               dbg(1," %s : interrupted", __func__);
+                               set_current_state(TASK_RUNNING);
                                retval = -EINTR;
-                               goto exit;
+                               goto exit_onqueue;
                        }
-                       mutex_unlock(&dev->mtx);
-                       timeout = interruptible_sleep_on_timeout(&dev->write_wait, timeout);
+                       if (schedule_timeout(COMMAND_TIMEOUT) == 0) {
+                               dbg(1, "%s - command timed out.", __func__);
+                               retval = -ETIMEDOUT;
+                               goto exit_onqueue;
+                       }
+                       remove_wait_queue(&dev->write_wait, &waita);
                        retval = mutex_lock_interruptible(&dev->mtx);
                        if (retval) {
                                retval = bytes_written ? bytes_written : retval;
                                goto exit_nolock;
                        }
-                       if (timeout > 0) {
-                               break;
-                       }
-                       dbg(1," %s : interrupted timeout: %d", __FUNCTION__, timeout);
-               }
-
-
-               dbg(1," %s : final timeout: %d", __FUNCTION__, timeout);
-
-               if (timeout == 0) {
-                       dbg(1, "%s - command timed out.", __FUNCTION__);
-                       retval = -ETIMEDOUT;
-                       goto exit;
-               }
-
-               dbg(4," %s : in progress, count = %Zd", __FUNCTION__, count);
 
+                       dbg(4," %s : in progress, count = %Zd", __func__, count);
                } else {
-                       dbg(4," %s : sending, count = %Zd", __FUNCTION__, count);
+                       spin_unlock_irqrestore(&dev->buflock, flags);
+                       set_current_state(TASK_RUNNING);
+                       remove_wait_queue(&dev->write_wait, &waita);
+                       dbg(4," %s : sending, count = %Zd", __func__, count);
 
                        /* write the data into interrupt_out_buffer from userspace */
                        buffer_size = le16_to_cpu(dev->interrupt_out_endpoint->wMaxPacketSize);
                        bytes_to_write = count > buffer_size ? buffer_size : count;
                        dbg(4," %s : buffer_size = %Zd, count = %Zd, bytes_to_write = %Zd",
-                           __FUNCTION__, buffer_size, count, bytes_to_write);
+                           __func__, buffer_size, count, bytes_to_write);
 
                        if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write) != 0) {
                                retval = -EFAULT;
@@ -622,11 +639,12 @@ static ssize_t adu_write(struct file *file, const __user char *buffer,
                                bytes_to_write,
                                adu_interrupt_out_callback,
                                dev,
-                               dev->interrupt_in_endpoint->bInterval);
-                       /* dev->interrupt_in_urb->transfer_flags |= URB_ASYNC_UNLINK; */
+                               dev->interrupt_out_endpoint->bInterval);
                        dev->interrupt_out_urb->actual_length = bytes_to_write;
+                       dev->out_urb_finished = 0;
                        retval = usb_submit_urb(dev->interrupt_out_urb, GFP_KERNEL);
                        if (retval < 0) {
+                               dev->out_urb_finished = 1;
                                err("Couldn't submit interrupt_out_urb %d", retval);
                                goto exit;
                        }
@@ -637,16 +655,17 @@ static ssize_t adu_write(struct file *file, const __user char *buffer,
                        bytes_written += bytes_to_write;
                }
        }
-
-       retval = bytes_written;
+       mutex_unlock(&dev->mtx);
+       return bytes_written;
 
 exit:
-       /* unlock the device */
        mutex_unlock(&dev->mtx);
 exit_nolock:
+       dbg(2," %s : leave, return value %d", __func__, retval);
+       return retval;
 
-       dbg(2," %s : leave, return value %d", __FUNCTION__, retval);
-
+exit_onqueue:
+       remove_wait_queue(&dev->write_wait, &waita);
        return retval;
 }
 
@@ -687,7 +706,7 @@ static int adu_probe(struct usb_interface *interface,
        int out_end_size;
        int i;
 
-       dbg(2," %s : enter", __FUNCTION__);
+       dbg(2," %s : enter", __func__);
 
        if (udev == NULL) {
                dev_err(&interface->dev, "udev is NULL.\n");
@@ -788,7 +807,7 @@ static int adu_probe(struct usb_interface *interface,
                dev_err(&interface->dev, "Could not retrieve serial number\n");
                goto error;
        }
-       dbg(2," %s : serial_number=%s", __FUNCTION__, dev->serial_number);
+       dbg(2," %s : serial_number=%s", __func__, dev->serial_number);
 
        /* we can register the device now, as it is ready */
        usb_set_intfdata(interface, dev);
@@ -809,7 +828,7 @@ static int adu_probe(struct usb_interface *interface,
                 udev->descriptor.idProduct, dev->serial_number,
                 (dev->minor - ADU_MINOR_BASE));
 exit:
-       dbg(2," %s : leave, return value %p (dev)", __FUNCTION__, dev);
+       dbg(2," %s : leave, return value %p (dev)", __func__, dev);
 
        return retval;
 
@@ -828,33 +847,30 @@ static void adu_disconnect(struct usb_interface *interface)
        struct adu_device *dev;
        int minor;
 
-       dbg(2," %s : enter", __FUNCTION__);
+       dbg(2," %s : enter", __func__);
 
        dev = usb_get_intfdata(interface);
-       usb_set_intfdata(interface, NULL);
 
+       mutex_lock(&dev->mtx);  /* not interruptible */
+       dev->udev = NULL;       /* poison */
        minor = dev->minor;
-
-       /* give back our minor */
        usb_deregister_dev(interface, &adu_class);
-       dev->minor = 0;
+       mutex_unlock(&dev->mtx);
 
-       mutex_lock(&dev->mtx); /* not interruptible */
+       mutex_lock(&adutux_mutex);
+       usb_set_intfdata(interface, NULL);
 
        /* 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) {
-               mutex_unlock(&dev->mtx);
+       dbg(2," %s : open count %d", __func__, dev->open_count);
+       if (!dev->open_count)
                adu_delete(dev);
-       } else {
-               dev->udev = NULL;
-               mutex_unlock(&dev->mtx);
-       }
+
+       mutex_unlock(&adutux_mutex);
 
        dev_info(&interface->dev, "ADU device adutux%d now disconnected\n",
                 (minor - ADU_MINOR_BASE));
 
-       dbg(2," %s : leave", __FUNCTION__);
+       dbg(2," %s : leave", __func__);
 }
 
 /* usb specific object needed to register this driver with the usb subsystem */
@@ -869,7 +885,7 @@ static int __init adu_init(void)
 {
        int result;
 
-       dbg(2," %s : enter", __FUNCTION__);
+       dbg(2," %s : enter", __func__);
 
        /* register this driver with the USB subsystem */
        result = usb_register(&adu_driver);
@@ -879,21 +895,22 @@ static int __init adu_init(void)
                goto exit;
        }
 
-       info("adutux " DRIVER_DESC " " DRIVER_VERSION);
-       info("adutux is an experimental driver. Use at your own risk");
+       printk(KERN_INFO "adutux " DRIVER_DESC " " DRIVER_VERSION "\n");
+       printk(KERN_INFO "adutux is an experimental driver. "
+              "Use at your own risk\n");
 
 exit:
-       dbg(2," %s : leave, return value %d", __FUNCTION__, result);
+       dbg(2," %s : leave, return value %d", __func__, result);
 
        return result;
 }
 
 static void __exit adu_exit(void)
 {
-       dbg(2," %s : enter", __FUNCTION__);
+       dbg(2," %s : enter", __func__);
        /* deregister this driver with the USB subsystem */
        usb_deregister(&adu_driver);
-       dbg(2," %s : leave", __FUNCTION__);
+       dbg(2," %s : leave", __func__);
 }
 
 module_init(adu_init);