const: constify remaining dev_pm_ops
[safe/jmp/linux-2.6] / drivers / usb / usb-skeleton.c
index 59973ae..b1e579c 100644 (file)
@@ -18,7 +18,7 @@
 #include <linux/slab.h>
 #include <linux/module.h>
 #include <linux/kref.h>
-#include <asm/uaccess.h>
+#include <linux/uaccess.h>
 #include <linux/usb.h>
 #include <linux/mutex.h>
 
@@ -28,7 +28,7 @@
 #define USB_SKEL_PRODUCT_ID    0xfff0
 
 /* table of devices that work with this driver */
-static struct usb_device_id skel_table [] = {
+static struct usb_device_id skel_table[] = {
        { USB_DEVICE(USB_SKEL_VENDOR_ID, USB_SKEL_PRODUCT_ID) },
        { }                                     /* Terminating entry */
 };
@@ -52,15 +52,21 @@ struct usb_skel {
        struct usb_interface    *interface;             /* the interface for this device */
        struct semaphore        limit_sem;              /* limiting the number of writes in progress */
        struct usb_anchor       submitted;              /* in case we need to retract our submissions */
+       struct urb              *bulk_in_urb;           /* the urb to read data with */
        unsigned char           *bulk_in_buffer;        /* the buffer to receive data */
        size_t                  bulk_in_size;           /* the size of the receive buffer */
+       size_t                  bulk_in_filled;         /* number of bytes in the buffer */
+       size_t                  bulk_in_copied;         /* already copied to user space */
        __u8                    bulk_in_endpointAddr;   /* the address of the bulk in endpoint */
        __u8                    bulk_out_endpointAddr;  /* the address of the bulk out endpoint */
        int                     errors;                 /* the last request tanked */
        int                     open_count;             /* count the number of openers */
+       bool                    ongoing_read;           /* a read is going on */
+       bool                    processed_urb;          /* indicates we haven't processed the urb */
        spinlock_t              err_lock;               /* lock for errors */
        struct kref             kref;
        struct mutex            io_mutex;               /* synchronize I/O with disconnect */
+       struct completion       bulk_in_completion;     /* to wait for an ongoing read */
 };
 #define to_skel_dev(d) container_of(d, struct usb_skel, kref)
 
@@ -71,6 +77,7 @@ static void skel_delete(struct kref *kref)
 {
        struct usb_skel *dev = to_skel_dev(kref);
 
+       usb_free_urb(dev->bulk_in_urb);
        usb_put_dev(dev->udev);
        kfree(dev->bulk_in_buffer);
        kfree(dev);
@@ -87,8 +94,8 @@ static int skel_open(struct inode *inode, struct file *file)
 
        interface = usb_find_interface(&skel_driver, subminor);
        if (!interface) {
-               err ("%s - error, can't find device for minor %d",
-                    __FUNCTION__, subminor);
+               err("%s - error, can't find device for minor %d",
+                    __func__, subminor);
                retval = -ENODEV;
                goto exit;
        }
@@ -125,6 +132,7 @@ static int skel_open(struct inode *inode, struct file *file)
 
        /* save our object in the file's private structure */
        file->private_data = dev;
+       mutex_unlock(&dev->io_mutex);
 
 exit:
        return retval;
@@ -173,53 +181,205 @@ static int skel_flush(struct file *file, fl_owner_t id)
        return res;
 }
 
-static ssize_t skel_read(struct file *file, char *buffer, size_t count, loff_t *ppos)
+static void skel_read_bulk_callback(struct urb *urb)
 {
        struct usb_skel *dev;
-       int retval;
-       int bytes_read;
+
+       dev = urb->context;
+
+       spin_lock(&dev->err_lock);
+       /* sync/async unlink faults aren't errors */
+       if (urb->status) {
+               if (!(urb->status == -ENOENT ||
+                   urb->status == -ECONNRESET ||
+                   urb->status == -ESHUTDOWN))
+                       err("%s - nonzero write bulk status received: %d",
+                           __func__, urb->status);
+
+               dev->errors = urb->status;
+       } else {
+               dev->bulk_in_filled = urb->actual_length;
+       }
+       dev->ongoing_read = 0;
+       spin_unlock(&dev->err_lock);
+
+       complete(&dev->bulk_in_completion);
+}
+
+static int skel_do_read_io(struct usb_skel *dev, size_t count)
+{
+       int rv;
+
+       /* prepare a read */
+       usb_fill_bulk_urb(dev->bulk_in_urb,
+                       dev->udev,
+                       usb_rcvbulkpipe(dev->udev,
+                               dev->bulk_in_endpointAddr),
+                       dev->bulk_in_buffer,
+                       min(dev->bulk_in_size, count),
+                       skel_read_bulk_callback,
+                       dev);
+       /* tell everybody to leave the URB alone */
+       spin_lock_irq(&dev->err_lock);
+       dev->ongoing_read = 1;
+       spin_unlock_irq(&dev->err_lock);
+
+       /* do it */
+       rv = usb_submit_urb(dev->bulk_in_urb, GFP_KERNEL);
+       if (rv < 0) {
+               err("%s - failed submitting read urb, error %d",
+                       __func__, rv);
+               dev->bulk_in_filled = 0;
+               rv = (rv == -ENOMEM) ? rv : -EIO;
+               spin_lock_irq(&dev->err_lock);
+               dev->ongoing_read = 0;
+               spin_unlock_irq(&dev->err_lock);
+       }
+
+       return rv;
+}
+
+static ssize_t skel_read(struct file *file, char *buffer, size_t count,
+                        loff_t *ppos)
+{
+       struct usb_skel *dev;
+       int rv;
+       bool ongoing_io;
 
        dev = (struct usb_skel *)file->private_data;
 
-       mutex_lock(&dev->io_mutex);
+       /* if we cannot read at all, return EOF */
+       if (!dev->bulk_in_urb || !count)
+               return 0;
+
+       /* no concurrent readers */
+       rv = mutex_lock_interruptible(&dev->io_mutex);
+       if (rv < 0)
+               return rv;
+
        if (!dev->interface) {          /* disconnect() was called */
-               retval = -ENODEV;
+               rv = -ENODEV;
                goto exit;
        }
 
-       /* do a blocking bulk read to get data from the device */
-       retval = usb_bulk_msg(dev->udev,
-                             usb_rcvbulkpipe(dev->udev, dev->bulk_in_endpointAddr),
-                             dev->bulk_in_buffer,
-                             min(dev->bulk_in_size, count),
-                             &bytes_read, 10000);
-
-       /* if the read was successful, copy the data to userspace */
-       if (!retval) {
-               if (copy_to_user(buffer, dev->bulk_in_buffer, bytes_read))
-                       retval = -EFAULT;
-               else
-                       retval = bytes_read;
+       /* if IO is under way, we must not touch things */
+retry:
+       spin_lock_irq(&dev->err_lock);
+       ongoing_io = dev->ongoing_read;
+       spin_unlock_irq(&dev->err_lock);
+
+       if (ongoing_io) {
+               /* nonblocking IO shall not wait */
+               if (file->f_flags & O_NONBLOCK) {
+                       rv = -EAGAIN;
+                       goto exit;
+               }
+               /*
+                * IO may take forever
+                * hence wait in an interruptible state
+                */
+               rv = wait_for_completion_interruptible(&dev->bulk_in_completion);
+               if (rv < 0)
+                       goto exit;
+               /*
+                * by waiting we also semiprocessed the urb
+                * we must finish now
+                */
+               dev->bulk_in_copied = 0;
+               dev->processed_urb = 1;
        }
 
+       if (!dev->processed_urb) {
+               /*
+                * the URB hasn't been processed
+                * do it now
+                */
+               wait_for_completion(&dev->bulk_in_completion);
+               dev->bulk_in_copied = 0;
+               dev->processed_urb = 1;
+       }
+
+       /* errors must be reported */
+       rv = dev->errors;
+       if (rv < 0) {
+               /* any error is reported once */
+               dev->errors = 0;
+               /* to preserve notifications about reset */
+               rv = (rv == -EPIPE) ? rv : -EIO;
+               /* no data to deliver */
+               dev->bulk_in_filled = 0;
+               /* report it */
+               goto exit;
+       }
+
+       /*
+        * if the buffer is filled we may satisfy the read
+        * else we need to start IO
+        */
+
+       if (dev->bulk_in_filled) {
+               /* we had read data */
+               size_t available = dev->bulk_in_filled - dev->bulk_in_copied;
+               size_t chunk = min(available, count);
+
+               if (!available) {
+                       /*
+                        * all data has been used
+                        * actual IO needs to be done
+                        */
+                       rv = skel_do_read_io(dev, count);
+                       if (rv < 0)
+                               goto exit;
+                       else
+                               goto retry;
+               }
+               /*
+                * data is available
+                * chunk tells us how much shall be copied
+                */
+
+               if (copy_to_user(buffer,
+                                dev->bulk_in_buffer + dev->bulk_in_copied,
+                                chunk))
+                       rv = -EFAULT;
+               else
+                       rv = chunk;
+
+               dev->bulk_in_copied += chunk;
+
+               /*
+                * if we are asked for more than we have,
+                * we start IO but don't wait
+                */
+               if (available < count)
+                       skel_do_read_io(dev, count - chunk);
+       } else {
+               /* no data in the buffer */
+               rv = skel_do_read_io(dev, count);
+               if (rv < 0)
+                       goto exit;
+               else if (!(file->f_flags & O_NONBLOCK))
+                       goto retry;
+               rv = -EAGAIN;
+       }
 exit:
        mutex_unlock(&dev->io_mutex);
-       return retval;
+       return rv;
 }
 
 static void skel_write_bulk_callback(struct urb *urb)
 {
        struct usb_skel *dev;
 
-       dev = (struct usb_skel *)urb->context;
+       dev = urb->context;
 
        /* sync/async unlink faults aren't errors */
        if (urb->status) {
-               if(!(urb->status == -ENOENT ||
+               if (!(urb->status == -ENOENT ||
                    urb->status == -ECONNRESET ||
                    urb->status == -ESHUTDOWN))
                        err("%s - nonzero write bulk status received: %d",
-                           __FUNCTION__, urb->status);
+                           __func__, urb->status);
 
                spin_lock(&dev->err_lock);
                dev->errors = urb->status;
@@ -232,7 +392,8 @@ static void skel_write_bulk_callback(struct urb *urb)
        up(&dev->limit_sem);
 }
 
-static ssize_t skel_write(struct file *file, const char *user_buffer, size_t count, loff_t *ppos)
+static ssize_t skel_write(struct file *file, const char *user_buffer,
+                         size_t count, loff_t *ppos)
 {
        struct usb_skel *dev;
        int retval = 0;
@@ -246,14 +407,25 @@ static ssize_t skel_write(struct file *file, const char *user_buffer, size_t cou
        if (count == 0)
                goto exit;
 
-       /* limit the number of URBs in flight to stop a user from using up all RAM */
-       if (down_interruptible(&dev->limit_sem)) {
-               retval = -ERESTARTSYS;
-               goto exit;
+       /*
+        * limit the number of URBs in flight to stop a user from using up all
+        * RAM
+        */
+       if (!(file->f_flags & O_NONBLOCK)) {
+               if (down_interruptible(&dev->limit_sem)) {
+                       retval = -ERESTARTSYS;
+                       goto exit;
+               }
+       } else {
+               if (down_trylock(&dev->limit_sem)) {
+                       retval = -EAGAIN;
+                       goto exit;
+               }
        }
 
        spin_lock_irq(&dev->err_lock);
-       if ((retval = dev->errors) < 0) {
+       retval = dev->errors;
+       if (retval < 0) {
                /* any error is reported once */
                dev->errors = 0;
                /* to preserve notifications about reset */
@@ -270,7 +442,8 @@ static ssize_t skel_write(struct file *file, const char *user_buffer, size_t cou
                goto error;
        }
 
-       buf = usb_buffer_alloc(dev->udev, writesize, GFP_KERNEL, &urb->transfer_dma);
+       buf = usb_buffer_alloc(dev->udev, writesize, GFP_KERNEL,
+                              &urb->transfer_dma);
        if (!buf) {
                retval = -ENOMEM;
                goto error;
@@ -300,11 +473,15 @@ static ssize_t skel_write(struct file *file, const char *user_buffer, size_t cou
        retval = usb_submit_urb(urb, GFP_KERNEL);
        mutex_unlock(&dev->io_mutex);
        if (retval) {
-               err("%s - failed submitting write urb, error %d", __FUNCTION__, retval);
+               err("%s - failed submitting write urb, error %d", __func__,
+                   retval);
                goto error_unanchor;
        }
 
-       /* release our reference to this urb, the USB core will eventually free it entirely */
+       /*
+        * release our reference to this urb, the USB core will eventually free
+        * it entirely
+        */
        usb_free_urb(urb);
 
 
@@ -342,7 +519,8 @@ static struct usb_class_driver skel_class = {
        .minor_base =   USB_SKEL_MINOR_BASE,
 };
 
-static int skel_probe(struct usb_interface *interface, const struct usb_device_id *id)
+static int skel_probe(struct usb_interface *interface,
+                     const struct usb_device_id *id)
 {
        struct usb_skel *dev;
        struct usb_host_interface *iface_desc;
@@ -362,6 +540,7 @@ static int skel_probe(struct usb_interface *interface, const struct usb_device_i
        mutex_init(&dev->io_mutex);
        spin_lock_init(&dev->err_lock);
        init_usb_anchor(&dev->submitted);
+       init_completion(&dev->bulk_in_completion);
 
        dev->udev = usb_get_dev(interface_to_usbdev(interface));
        dev->interface = interface;
@@ -383,6 +562,11 @@ static int skel_probe(struct usb_interface *interface, const struct usb_device_i
                                err("Could not allocate bulk_in_buffer");
                                goto error;
                        }
+                       dev->bulk_in_urb = usb_alloc_urb(0, GFP_KERNEL);
+                       if (!dev->bulk_in_urb) {
+                               err("Could not allocate bulk_in_urb");
+                               goto error;
+                       }
                }
 
                if (!dev->bulk_out_endpointAddr &&
@@ -409,7 +593,9 @@ static int skel_probe(struct usb_interface *interface, const struct usb_device_i
        }
 
        /* let the user know what node this device is now attached to */
-       info("USB Skeleton device now attached to USBSkel-%d", interface->minor);
+       dev_info(&interface->dev,
+                "USB Skeleton device now attached to USBSkel-%d",
+                interface->minor);
        return 0;
 
 error:
@@ -440,7 +626,7 @@ static void skel_disconnect(struct usb_interface *interface)
        /* decrement our usage count */
        kref_put(&dev->kref, skel_delete);
 
-       info("USB Skeleton #%d now disconnected", minor);
+       dev_info(&interface->dev, "USB Skeleton #%d now disconnected", minor);
 }
 
 static void skel_draw_down(struct usb_skel *dev)
@@ -450,6 +636,7 @@ static void skel_draw_down(struct usb_skel *dev)
        time = usb_wait_anchor_empty_timeout(&dev->submitted, 1000);
        if (!time)
                usb_kill_anchored_urbs(&dev->submitted);
+       usb_kill_urb(dev->bulk_in_urb);
 }
 
 static int skel_suspend(struct usb_interface *intf, pm_message_t message)
@@ -462,17 +649,40 @@ static int skel_suspend(struct usb_interface *intf, pm_message_t message)
        return 0;
 }
 
-static int skel_resume (struct usb_interface *intf)
+static int skel_resume(struct usb_interface *intf)
 {
        return 0;
 }
 
+static int skel_pre_reset(struct usb_interface *intf)
+{
+       struct usb_skel *dev = usb_get_intfdata(intf);
+
+       mutex_lock(&dev->io_mutex);
+       skel_draw_down(dev);
+
+       return 0;
+}
+
+static int skel_post_reset(struct usb_interface *intf)
+{
+       struct usb_skel *dev = usb_get_intfdata(intf);
+
+       /* we are sure no URBs are active - no locking needed */
+       dev->errors = -EPIPE;
+       mutex_unlock(&dev->io_mutex);
+
+       return 0;
+}
+
 static struct usb_driver skel_driver = {
        .name =         "skeleton",
        .probe =        skel_probe,
        .disconnect =   skel_disconnect,
        .suspend =      skel_suspend,
        .resume =       skel_resume,
+       .pre_reset =    skel_pre_reset,
+       .post_reset =   skel_post_reset,
        .id_table =     skel_table,
        .supports_autosuspend = 1,
 };