Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Luiz Fernando N. Capitulino | c070454 | 2006-08-14 22:44:29 -0300 | [diff] [blame] | 2 | * USB Skeleton driver - 2.2 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2001-2004 Greg Kroah-Hartman (greg@kroah.com) |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License as |
| 8 | * published by the Free Software Foundation, version 2. |
| 9 | * |
Luiz Fernando N. Capitulino | c070454 | 2006-08-14 22:44:29 -0300 | [diff] [blame] | 10 | * This driver is based on the 2.6.3 version of drivers/usb/usb-skeleton.c |
Alan Stern | 121e287 | 2006-07-01 22:06:36 -0400 | [diff] [blame] | 11 | * but has been rewritten to be easier to read and use. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | * |
| 13 | */ |
| 14 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/kernel.h> |
| 16 | #include <linux/errno.h> |
| 17 | #include <linux/init.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/kref.h> |
Greg Kroah-Hartman | 3ae9da1 | 2009-09-11 16:07:30 -0700 | [diff] [blame] | 21 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/usb.h> |
Alan Stern | 121e287 | 2006-07-01 22:06:36 -0400 | [diff] [blame] | 23 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
| 25 | |
| 26 | /* Define these values to match your devices */ |
| 27 | #define USB_SKEL_VENDOR_ID 0xfff0 |
| 28 | #define USB_SKEL_PRODUCT_ID 0xfff0 |
| 29 | |
Ming Lei | 26c71a7 | 2011-12-16 22:20:01 +0800 | [diff] [blame] | 30 | static DEFINE_MUTEX(skel_mutex); |
| 31 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | /* table of devices that work with this driver */ |
Németh Márton | 1bd4f29 | 2010-01-10 15:33:33 +0100 | [diff] [blame] | 33 | static const struct usb_device_id skel_table[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | { USB_DEVICE(USB_SKEL_VENDOR_ID, USB_SKEL_PRODUCT_ID) }, |
| 35 | { } /* Terminating entry */ |
| 36 | }; |
Luiz Fernando N. Capitulino | c070454 | 2006-08-14 22:44:29 -0300 | [diff] [blame] | 37 | MODULE_DEVICE_TABLE(usb, skel_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
| 39 | |
| 40 | /* Get a minor range for your devices from the usb maintainer */ |
| 41 | #define USB_SKEL_MINOR_BASE 192 |
| 42 | |
Oliver Neukum | ff90651 | 2005-12-21 19:27:29 +0100 | [diff] [blame] | 43 | /* our private defines. if this grows any larger, use your own .h file */ |
Luiz Fernando N. Capitulino | c070454 | 2006-08-14 22:44:29 -0300 | [diff] [blame] | 44 | #define MAX_TRANSFER (PAGE_SIZE - 512) |
Oliver Neukum | ba35e02 | 2007-03-01 14:31:02 +0100 | [diff] [blame] | 45 | /* MAX_TRANSFER is chosen so that the VM is not stressed by |
| 46 | allocations > PAGE_SIZE and the number of packets in a page |
| 47 | is an integer 512 is the largest possible packet on EHCI */ |
Oliver Neukum | ff90651 | 2005-12-21 19:27:29 +0100 | [diff] [blame] | 48 | #define WRITES_IN_FLIGHT 8 |
Oliver Neukum | ba35e02 | 2007-03-01 14:31:02 +0100 | [diff] [blame] | 49 | /* arbitrarily chosen */ |
Oliver Neukum | ff90651 | 2005-12-21 19:27:29 +0100 | [diff] [blame] | 50 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | /* Structure to hold all of our device specific stuff */ |
| 52 | struct usb_skel { |
Oliver Neukum | ba35e02 | 2007-03-01 14:31:02 +0100 | [diff] [blame] | 53 | struct usb_device *udev; /* the usb device for this device */ |
| 54 | struct usb_interface *interface; /* the interface for this device */ |
Oliver Neukum | ff90651 | 2005-12-21 19:27:29 +0100 | [diff] [blame] | 55 | struct semaphore limit_sem; /* limiting the number of writes in progress */ |
Oliver Neukum | 403dfb5 | 2007-05-25 13:42:56 +0200 | [diff] [blame] | 56 | struct usb_anchor submitted; /* in case we need to retract our submissions */ |
Oliver Neukum | e7389cc | 2009-09-09 17:06:53 +0200 | [diff] [blame] | 57 | struct urb *bulk_in_urb; /* the urb to read data with */ |
Luiz Fernando N. Capitulino | c070454 | 2006-08-14 22:44:29 -0300 | [diff] [blame] | 58 | unsigned char *bulk_in_buffer; /* the buffer to receive data */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | size_t bulk_in_size; /* the size of the receive buffer */ |
Oliver Neukum | e7389cc | 2009-09-09 17:06:53 +0200 | [diff] [blame] | 60 | size_t bulk_in_filled; /* number of bytes in the buffer */ |
| 61 | size_t bulk_in_copied; /* already copied to user space */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | __u8 bulk_in_endpointAddr; /* the address of the bulk in endpoint */ |
| 63 | __u8 bulk_out_endpointAddr; /* the address of the bulk out endpoint */ |
Oliver Neukum | 403dfb5 | 2007-05-25 13:42:56 +0200 | [diff] [blame] | 64 | int errors; /* the last request tanked */ |
Oliver Neukum | e7389cc | 2009-09-09 17:06:53 +0200 | [diff] [blame] | 65 | bool ongoing_read; /* a read is going on */ |
| 66 | bool processed_urb; /* indicates we haven't processed the urb */ |
Oliver Neukum | 403dfb5 | 2007-05-25 13:42:56 +0200 | [diff] [blame] | 67 | spinlock_t err_lock; /* lock for errors */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | struct kref kref; |
Alan Stern | 121e287 | 2006-07-01 22:06:36 -0400 | [diff] [blame] | 69 | struct mutex io_mutex; /* synchronize I/O with disconnect */ |
Oliver Neukum | e7389cc | 2009-09-09 17:06:53 +0200 | [diff] [blame] | 70 | struct completion bulk_in_completion; /* to wait for an ongoing read */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | }; |
| 72 | #define to_skel_dev(d) container_of(d, struct usb_skel, kref) |
| 73 | |
| 74 | static struct usb_driver skel_driver; |
Oliver Neukum | 403dfb5 | 2007-05-25 13:42:56 +0200 | [diff] [blame] | 75 | static void skel_draw_down(struct usb_skel *dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | |
| 77 | static void skel_delete(struct kref *kref) |
Luiz Fernando N. Capitulino | c070454 | 2006-08-14 22:44:29 -0300 | [diff] [blame] | 78 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | struct usb_skel *dev = to_skel_dev(kref); |
| 80 | |
Oliver Neukum | e7389cc | 2009-09-09 17:06:53 +0200 | [diff] [blame] | 81 | usb_free_urb(dev->bulk_in_urb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | usb_put_dev(dev->udev); |
Luiz Fernando N. Capitulino | c070454 | 2006-08-14 22:44:29 -0300 | [diff] [blame] | 83 | kfree(dev->bulk_in_buffer); |
| 84 | kfree(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | static int skel_open(struct inode *inode, struct file *file) |
| 88 | { |
| 89 | struct usb_skel *dev; |
| 90 | struct usb_interface *interface; |
| 91 | int subminor; |
| 92 | int retval = 0; |
| 93 | |
| 94 | subminor = iminor(inode); |
| 95 | |
| 96 | interface = usb_find_interface(&skel_driver, subminor); |
| 97 | if (!interface) { |
Greg Kroah-Hartman | 3ae9da1 | 2009-09-11 16:07:30 -0700 | [diff] [blame] | 98 | err("%s - error, can't find device for minor %d", |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 99 | __func__, subminor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | retval = -ENODEV; |
| 101 | goto exit; |
| 102 | } |
| 103 | |
Ming Lei | 26c71a7 | 2011-12-16 22:20:01 +0800 | [diff] [blame] | 104 | mutex_lock(&skel_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | dev = usb_get_intfdata(interface); |
| 106 | if (!dev) { |
Ming Lei | 26c71a7 | 2011-12-16 22:20:01 +0800 | [diff] [blame] | 107 | mutex_unlock(&skel_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | retval = -ENODEV; |
| 109 | goto exit; |
| 110 | } |
| 111 | |
| 112 | /* increment our usage count for the device */ |
| 113 | kref_get(&dev->kref); |
Ming Lei | 26c71a7 | 2011-12-16 22:20:01 +0800 | [diff] [blame] | 114 | mutex_unlock(&skel_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
Oliver Neukum | 758f7e16 | 2007-06-11 14:55:08 +0200 | [diff] [blame] | 116 | /* lock the device to allow correctly handling errors |
| 117 | * in resumption */ |
| 118 | mutex_lock(&dev->io_mutex); |
Ming Lei | 26c71a7 | 2011-12-16 22:20:01 +0800 | [diff] [blame] | 119 | if (!dev->interface) { |
| 120 | retval = -ENODEV; |
| 121 | goto out_err; |
| 122 | } |
Oliver Neukum | 758f7e16 | 2007-06-11 14:55:08 +0200 | [diff] [blame] | 123 | |
Ming Lei | e28dbb0 | 2011-12-16 22:20:44 +0800 | [diff] [blame^] | 124 | retval = usb_autopm_get_interface(interface); |
| 125 | if (retval) |
| 126 | goto out_err; |
Oliver Neukum | 5b06470 | 2007-02-08 15:42:53 +0100 | [diff] [blame] | 127 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | /* save our object in the file's private structure */ |
| 129 | file->private_data = dev; |
Ming Lei | 26c71a7 | 2011-12-16 22:20:01 +0800 | [diff] [blame] | 130 | |
| 131 | out_err: |
Mark Gross | f729405 | 2007-09-24 09:28:14 -0700 | [diff] [blame] | 132 | mutex_unlock(&dev->io_mutex); |
Ming Lei | 26c71a7 | 2011-12-16 22:20:01 +0800 | [diff] [blame] | 133 | if (retval) |
| 134 | kref_put(&dev->kref, skel_delete); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | |
| 136 | exit: |
| 137 | return retval; |
| 138 | } |
| 139 | |
| 140 | static int skel_release(struct inode *inode, struct file *file) |
| 141 | { |
| 142 | struct usb_skel *dev; |
| 143 | |
Joe Perches | e53e841 | 2010-07-12 13:50:13 -0700 | [diff] [blame] | 144 | dev = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | if (dev == NULL) |
| 146 | return -ENODEV; |
| 147 | |
Alan Stern | 01d883d | 2006-08-30 15:47:18 -0400 | [diff] [blame] | 148 | /* allow the device to be autosuspended */ |
| 149 | mutex_lock(&dev->io_mutex); |
Ming Lei | e28dbb0 | 2011-12-16 22:20:44 +0800 | [diff] [blame^] | 150 | if (dev->interface) |
Alan Stern | 01d883d | 2006-08-30 15:47:18 -0400 | [diff] [blame] | 151 | usb_autopm_put_interface(dev->interface); |
| 152 | mutex_unlock(&dev->io_mutex); |
| 153 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | /* decrement the count on our device */ |
| 155 | kref_put(&dev->kref, skel_delete); |
| 156 | return 0; |
| 157 | } |
| 158 | |
Oliver Neukum | 403dfb5 | 2007-05-25 13:42:56 +0200 | [diff] [blame] | 159 | static int skel_flush(struct file *file, fl_owner_t id) |
| 160 | { |
| 161 | struct usb_skel *dev; |
| 162 | int res; |
| 163 | |
Joe Perches | e53e841 | 2010-07-12 13:50:13 -0700 | [diff] [blame] | 164 | dev = file->private_data; |
Oliver Neukum | 403dfb5 | 2007-05-25 13:42:56 +0200 | [diff] [blame] | 165 | if (dev == NULL) |
| 166 | return -ENODEV; |
| 167 | |
| 168 | /* wait for io to stop */ |
| 169 | mutex_lock(&dev->io_mutex); |
| 170 | skel_draw_down(dev); |
| 171 | |
| 172 | /* read out errors, leave subsequent opens a clean slate */ |
| 173 | spin_lock_irq(&dev->err_lock); |
| 174 | res = dev->errors ? (dev->errors == -EPIPE ? -EPIPE : -EIO) : 0; |
| 175 | dev->errors = 0; |
| 176 | spin_unlock_irq(&dev->err_lock); |
| 177 | |
| 178 | mutex_unlock(&dev->io_mutex); |
| 179 | |
| 180 | return res; |
| 181 | } |
| 182 | |
Oliver Neukum | e7389cc | 2009-09-09 17:06:53 +0200 | [diff] [blame] | 183 | static void skel_read_bulk_callback(struct urb *urb) |
| 184 | { |
| 185 | struct usb_skel *dev; |
| 186 | |
| 187 | dev = urb->context; |
| 188 | |
| 189 | spin_lock(&dev->err_lock); |
| 190 | /* sync/async unlink faults aren't errors */ |
| 191 | if (urb->status) { |
Greg Kroah-Hartman | 3ae9da1 | 2009-09-11 16:07:30 -0700 | [diff] [blame] | 192 | if (!(urb->status == -ENOENT || |
Oliver Neukum | e7389cc | 2009-09-09 17:06:53 +0200 | [diff] [blame] | 193 | urb->status == -ECONNRESET || |
| 194 | urb->status == -ESHUTDOWN)) |
| 195 | err("%s - nonzero write bulk status received: %d", |
| 196 | __func__, urb->status); |
| 197 | |
| 198 | dev->errors = urb->status; |
| 199 | } else { |
| 200 | dev->bulk_in_filled = urb->actual_length; |
| 201 | } |
| 202 | dev->ongoing_read = 0; |
| 203 | spin_unlock(&dev->err_lock); |
| 204 | |
| 205 | complete(&dev->bulk_in_completion); |
| 206 | } |
| 207 | |
| 208 | static int skel_do_read_io(struct usb_skel *dev, size_t count) |
| 209 | { |
| 210 | int rv; |
| 211 | |
| 212 | /* prepare a read */ |
| 213 | usb_fill_bulk_urb(dev->bulk_in_urb, |
| 214 | dev->udev, |
| 215 | usb_rcvbulkpipe(dev->udev, |
| 216 | dev->bulk_in_endpointAddr), |
| 217 | dev->bulk_in_buffer, |
| 218 | min(dev->bulk_in_size, count), |
| 219 | skel_read_bulk_callback, |
| 220 | dev); |
| 221 | /* tell everybody to leave the URB alone */ |
| 222 | spin_lock_irq(&dev->err_lock); |
| 223 | dev->ongoing_read = 1; |
| 224 | spin_unlock_irq(&dev->err_lock); |
| 225 | |
| 226 | /* do it */ |
| 227 | rv = usb_submit_urb(dev->bulk_in_urb, GFP_KERNEL); |
| 228 | if (rv < 0) { |
| 229 | err("%s - failed submitting read urb, error %d", |
| 230 | __func__, rv); |
| 231 | dev->bulk_in_filled = 0; |
| 232 | rv = (rv == -ENOMEM) ? rv : -EIO; |
| 233 | spin_lock_irq(&dev->err_lock); |
| 234 | dev->ongoing_read = 0; |
| 235 | spin_unlock_irq(&dev->err_lock); |
| 236 | } |
| 237 | |
| 238 | return rv; |
| 239 | } |
| 240 | |
Greg Kroah-Hartman | 3ae9da1 | 2009-09-11 16:07:30 -0700 | [diff] [blame] | 241 | static ssize_t skel_read(struct file *file, char *buffer, size_t count, |
| 242 | loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | { |
| 244 | struct usb_skel *dev; |
Oliver Neukum | e7389cc | 2009-09-09 17:06:53 +0200 | [diff] [blame] | 245 | int rv; |
| 246 | bool ongoing_io; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | |
Joe Perches | e53e841 | 2010-07-12 13:50:13 -0700 | [diff] [blame] | 248 | dev = file->private_data; |
Alan Stern | 121e287 | 2006-07-01 22:06:36 -0400 | [diff] [blame] | 249 | |
Oliver Neukum | e7389cc | 2009-09-09 17:06:53 +0200 | [diff] [blame] | 250 | /* if we cannot read at all, return EOF */ |
| 251 | if (!dev->bulk_in_urb || !count) |
| 252 | return 0; |
| 253 | |
| 254 | /* no concurrent readers */ |
| 255 | rv = mutex_lock_interruptible(&dev->io_mutex); |
| 256 | if (rv < 0) |
| 257 | return rv; |
| 258 | |
Alan Stern | 121e287 | 2006-07-01 22:06:36 -0400 | [diff] [blame] | 259 | if (!dev->interface) { /* disconnect() was called */ |
Oliver Neukum | e7389cc | 2009-09-09 17:06:53 +0200 | [diff] [blame] | 260 | rv = -ENODEV; |
Alan Stern | 121e287 | 2006-07-01 22:06:36 -0400 | [diff] [blame] | 261 | goto exit; |
| 262 | } |
| 263 | |
Oliver Neukum | e7389cc | 2009-09-09 17:06:53 +0200 | [diff] [blame] | 264 | /* if IO is under way, we must not touch things */ |
| 265 | retry: |
| 266 | spin_lock_irq(&dev->err_lock); |
| 267 | ongoing_io = dev->ongoing_read; |
| 268 | spin_unlock_irq(&dev->err_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | |
Oliver Neukum | e7389cc | 2009-09-09 17:06:53 +0200 | [diff] [blame] | 270 | if (ongoing_io) { |
Oliver Neukum | 8cd0166 | 2009-09-09 17:08:50 +0200 | [diff] [blame] | 271 | /* nonblocking IO shall not wait */ |
| 272 | if (file->f_flags & O_NONBLOCK) { |
| 273 | rv = -EAGAIN; |
| 274 | goto exit; |
| 275 | } |
Oliver Neukum | e7389cc | 2009-09-09 17:06:53 +0200 | [diff] [blame] | 276 | /* |
| 277 | * IO may take forever |
| 278 | * hence wait in an interruptible state |
| 279 | */ |
| 280 | rv = wait_for_completion_interruptible(&dev->bulk_in_completion); |
| 281 | if (rv < 0) |
| 282 | goto exit; |
| 283 | /* |
| 284 | * by waiting we also semiprocessed the urb |
| 285 | * we must finish now |
| 286 | */ |
| 287 | dev->bulk_in_copied = 0; |
| 288 | dev->processed_urb = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | } |
| 290 | |
Oliver Neukum | e7389cc | 2009-09-09 17:06:53 +0200 | [diff] [blame] | 291 | if (!dev->processed_urb) { |
| 292 | /* |
| 293 | * the URB hasn't been processed |
| 294 | * do it now |
| 295 | */ |
| 296 | wait_for_completion(&dev->bulk_in_completion); |
| 297 | dev->bulk_in_copied = 0; |
| 298 | dev->processed_urb = 1; |
| 299 | } |
| 300 | |
| 301 | /* errors must be reported */ |
Greg Kroah-Hartman | 3ae9da1 | 2009-09-11 16:07:30 -0700 | [diff] [blame] | 302 | rv = dev->errors; |
| 303 | if (rv < 0) { |
Oliver Neukum | e7389cc | 2009-09-09 17:06:53 +0200 | [diff] [blame] | 304 | /* any error is reported once */ |
| 305 | dev->errors = 0; |
| 306 | /* to preserve notifications about reset */ |
| 307 | rv = (rv == -EPIPE) ? rv : -EIO; |
| 308 | /* no data to deliver */ |
| 309 | dev->bulk_in_filled = 0; |
| 310 | /* report it */ |
| 311 | goto exit; |
| 312 | } |
| 313 | |
| 314 | /* |
| 315 | * if the buffer is filled we may satisfy the read |
| 316 | * else we need to start IO |
| 317 | */ |
| 318 | |
| 319 | if (dev->bulk_in_filled) { |
| 320 | /* we had read data */ |
| 321 | size_t available = dev->bulk_in_filled - dev->bulk_in_copied; |
| 322 | size_t chunk = min(available, count); |
| 323 | |
| 324 | if (!available) { |
| 325 | /* |
| 326 | * all data has been used |
| 327 | * actual IO needs to be done |
| 328 | */ |
| 329 | rv = skel_do_read_io(dev, count); |
| 330 | if (rv < 0) |
| 331 | goto exit; |
| 332 | else |
| 333 | goto retry; |
| 334 | } |
| 335 | /* |
| 336 | * data is available |
| 337 | * chunk tells us how much shall be copied |
| 338 | */ |
| 339 | |
| 340 | if (copy_to_user(buffer, |
| 341 | dev->bulk_in_buffer + dev->bulk_in_copied, |
| 342 | chunk)) |
| 343 | rv = -EFAULT; |
| 344 | else |
| 345 | rv = chunk; |
| 346 | |
| 347 | dev->bulk_in_copied += chunk; |
| 348 | |
| 349 | /* |
| 350 | * if we are asked for more than we have, |
| 351 | * we start IO but don't wait |
| 352 | */ |
| 353 | if (available < count) |
| 354 | skel_do_read_io(dev, count - chunk); |
| 355 | } else { |
| 356 | /* no data in the buffer */ |
| 357 | rv = skel_do_read_io(dev, count); |
| 358 | if (rv < 0) |
| 359 | goto exit; |
Julia Lawall | 4de8405 | 2009-09-19 09:13:43 +0200 | [diff] [blame] | 360 | else if (!(file->f_flags & O_NONBLOCK)) |
Oliver Neukum | e7389cc | 2009-09-09 17:06:53 +0200 | [diff] [blame] | 361 | goto retry; |
Oliver Neukum | 8cd0166 | 2009-09-09 17:08:50 +0200 | [diff] [blame] | 362 | rv = -EAGAIN; |
Oliver Neukum | e7389cc | 2009-09-09 17:06:53 +0200 | [diff] [blame] | 363 | } |
Alan Stern | 121e287 | 2006-07-01 22:06:36 -0400 | [diff] [blame] | 364 | exit: |
| 365 | mutex_unlock(&dev->io_mutex); |
Oliver Neukum | e7389cc | 2009-09-09 17:06:53 +0200 | [diff] [blame] | 366 | return rv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | } |
| 368 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 369 | static void skel_write_bulk_callback(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | { |
| 371 | struct usb_skel *dev; |
| 372 | |
Ming Lei | cdc9779 | 2008-02-24 18:41:47 +0800 | [diff] [blame] | 373 | dev = urb->context; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | |
| 375 | /* sync/async unlink faults aren't errors */ |
Oliver Neukum | 403dfb5 | 2007-05-25 13:42:56 +0200 | [diff] [blame] | 376 | if (urb->status) { |
Greg Kroah-Hartman | 3ae9da1 | 2009-09-11 16:07:30 -0700 | [diff] [blame] | 377 | if (!(urb->status == -ENOENT || |
Oliver Neukum | 403dfb5 | 2007-05-25 13:42:56 +0200 | [diff] [blame] | 378 | urb->status == -ECONNRESET || |
| 379 | urb->status == -ESHUTDOWN)) |
| 380 | err("%s - nonzero write bulk status received: %d", |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 381 | __func__, urb->status); |
Oliver Neukum | 403dfb5 | 2007-05-25 13:42:56 +0200 | [diff] [blame] | 382 | |
| 383 | spin_lock(&dev->err_lock); |
| 384 | dev->errors = urb->status; |
| 385 | spin_unlock(&dev->err_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | /* free up our allocated buffer */ |
Daniel Mack | 997ea58 | 2010-04-12 13:17:25 +0200 | [diff] [blame] | 389 | usb_free_coherent(urb->dev, urb->transfer_buffer_length, |
| 390 | urb->transfer_buffer, urb->transfer_dma); |
Oliver Neukum | ff90651 | 2005-12-21 19:27:29 +0100 | [diff] [blame] | 391 | up(&dev->limit_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | } |
| 393 | |
Greg Kroah-Hartman | 3ae9da1 | 2009-09-11 16:07:30 -0700 | [diff] [blame] | 394 | static ssize_t skel_write(struct file *file, const char *user_buffer, |
| 395 | size_t count, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | { |
| 397 | struct usb_skel *dev; |
| 398 | int retval = 0; |
| 399 | struct urb *urb = NULL; |
| 400 | char *buf = NULL; |
Sam Bishop | c8dd770 | 2005-12-22 17:11:02 -0700 | [diff] [blame] | 401 | size_t writesize = min(count, (size_t)MAX_TRANSFER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | |
Joe Perches | e53e841 | 2010-07-12 13:50:13 -0700 | [diff] [blame] | 403 | dev = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | |
| 405 | /* verify that we actually have some data to write */ |
| 406 | if (count == 0) |
| 407 | goto exit; |
| 408 | |
Greg Kroah-Hartman | 3ae9da1 | 2009-09-11 16:07:30 -0700 | [diff] [blame] | 409 | /* |
| 410 | * limit the number of URBs in flight to stop a user from using up all |
| 411 | * RAM |
| 412 | */ |
Julia Lawall | 4de8405 | 2009-09-19 09:13:43 +0200 | [diff] [blame] | 413 | if (!(file->f_flags & O_NONBLOCK)) { |
Oliver Neukum | 7981998 | 2009-09-09 10:23:35 +0200 | [diff] [blame] | 414 | if (down_interruptible(&dev->limit_sem)) { |
| 415 | retval = -ERESTARTSYS; |
| 416 | goto exit; |
| 417 | } |
| 418 | } else { |
| 419 | if (down_trylock(&dev->limit_sem)) { |
| 420 | retval = -EAGAIN; |
| 421 | goto exit; |
| 422 | } |
Sam Bishop | c8dd770 | 2005-12-22 17:11:02 -0700 | [diff] [blame] | 423 | } |
Oliver Neukum | ff90651 | 2005-12-21 19:27:29 +0100 | [diff] [blame] | 424 | |
Oliver Neukum | 403dfb5 | 2007-05-25 13:42:56 +0200 | [diff] [blame] | 425 | spin_lock_irq(&dev->err_lock); |
Greg Kroah-Hartman | 3ae9da1 | 2009-09-11 16:07:30 -0700 | [diff] [blame] | 426 | retval = dev->errors; |
| 427 | if (retval < 0) { |
Oliver Neukum | 403dfb5 | 2007-05-25 13:42:56 +0200 | [diff] [blame] | 428 | /* any error is reported once */ |
| 429 | dev->errors = 0; |
| 430 | /* to preserve notifications about reset */ |
| 431 | retval = (retval == -EPIPE) ? retval : -EIO; |
| 432 | } |
| 433 | spin_unlock_irq(&dev->err_lock); |
| 434 | if (retval < 0) |
| 435 | goto error; |
| 436 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | /* create a urb, and a buffer for it, and copy the data to the urb */ |
| 438 | urb = usb_alloc_urb(0, GFP_KERNEL); |
| 439 | if (!urb) { |
| 440 | retval = -ENOMEM; |
| 441 | goto error; |
| 442 | } |
| 443 | |
Daniel Mack | 997ea58 | 2010-04-12 13:17:25 +0200 | [diff] [blame] | 444 | buf = usb_alloc_coherent(dev->udev, writesize, GFP_KERNEL, |
| 445 | &urb->transfer_dma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | if (!buf) { |
| 447 | retval = -ENOMEM; |
| 448 | goto error; |
| 449 | } |
| 450 | |
Oliver Neukum | ff90651 | 2005-12-21 19:27:29 +0100 | [diff] [blame] | 451 | if (copy_from_user(buf, user_buffer, writesize)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | retval = -EFAULT; |
| 453 | goto error; |
| 454 | } |
| 455 | |
Oliver Neukum | ba35e02 | 2007-03-01 14:31:02 +0100 | [diff] [blame] | 456 | /* this lock makes sure we don't submit URBs to gone devices */ |
| 457 | mutex_lock(&dev->io_mutex); |
| 458 | if (!dev->interface) { /* disconnect() was called */ |
| 459 | mutex_unlock(&dev->io_mutex); |
| 460 | retval = -ENODEV; |
| 461 | goto error; |
| 462 | } |
| 463 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | /* initialize the urb properly */ |
| 465 | usb_fill_bulk_urb(urb, dev->udev, |
| 466 | usb_sndbulkpipe(dev->udev, dev->bulk_out_endpointAddr), |
Oliver Neukum | ff90651 | 2005-12-21 19:27:29 +0100 | [diff] [blame] | 467 | buf, writesize, skel_write_bulk_callback, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; |
Oliver Neukum | 403dfb5 | 2007-05-25 13:42:56 +0200 | [diff] [blame] | 469 | usb_anchor_urb(urb, &dev->submitted); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | |
| 471 | /* send the data out the bulk port */ |
| 472 | retval = usb_submit_urb(urb, GFP_KERNEL); |
Oliver Neukum | ba35e02 | 2007-03-01 14:31:02 +0100 | [diff] [blame] | 473 | mutex_unlock(&dev->io_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | if (retval) { |
Greg Kroah-Hartman | 3ae9da1 | 2009-09-11 16:07:30 -0700 | [diff] [blame] | 475 | err("%s - failed submitting write urb, error %d", __func__, |
| 476 | retval); |
Oliver Neukum | 403dfb5 | 2007-05-25 13:42:56 +0200 | [diff] [blame] | 477 | goto error_unanchor; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | } |
| 479 | |
Greg Kroah-Hartman | 3ae9da1 | 2009-09-11 16:07:30 -0700 | [diff] [blame] | 480 | /* |
| 481 | * release our reference to this urb, the USB core will eventually free |
| 482 | * it entirely |
| 483 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | usb_free_urb(urb); |
| 485 | |
Oliver Neukum | ba35e02 | 2007-03-01 14:31:02 +0100 | [diff] [blame] | 486 | |
Oliver Neukum | ff90651 | 2005-12-21 19:27:29 +0100 | [diff] [blame] | 487 | return writesize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | |
Oliver Neukum | 403dfb5 | 2007-05-25 13:42:56 +0200 | [diff] [blame] | 489 | error_unanchor: |
| 490 | usb_unanchor_urb(urb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | error: |
Alan Stern | 121e287 | 2006-07-01 22:06:36 -0400 | [diff] [blame] | 492 | if (urb) { |
Daniel Mack | 997ea58 | 2010-04-12 13:17:25 +0200 | [diff] [blame] | 493 | usb_free_coherent(dev->udev, writesize, buf, urb->transfer_dma); |
Alan Stern | 121e287 | 2006-07-01 22:06:36 -0400 | [diff] [blame] | 494 | usb_free_urb(urb); |
| 495 | } |
Oliver Neukum | ff90651 | 2005-12-21 19:27:29 +0100 | [diff] [blame] | 496 | up(&dev->limit_sem); |
Alan Stern | 121e287 | 2006-07-01 22:06:36 -0400 | [diff] [blame] | 497 | |
| 498 | exit: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | return retval; |
| 500 | } |
| 501 | |
Luiz Fernando N. Capitulino | 066202d | 2006-08-05 20:37:11 -0300 | [diff] [blame] | 502 | static const struct file_operations skel_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | .owner = THIS_MODULE, |
| 504 | .read = skel_read, |
| 505 | .write = skel_write, |
| 506 | .open = skel_open, |
| 507 | .release = skel_release, |
Oliver Neukum | 403dfb5 | 2007-05-25 13:42:56 +0200 | [diff] [blame] | 508 | .flush = skel_flush, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 509 | .llseek = noop_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | }; |
| 511 | |
Luiz Fernando N. Capitulino | c070454 | 2006-08-14 22:44:29 -0300 | [diff] [blame] | 512 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | * usb class driver info in order to get a minor number from the usb core, |
Greg Kroah-Hartman | 595b14c | 2006-01-18 17:36:58 -0500 | [diff] [blame] | 514 | * and to have the device registered with the driver core |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | */ |
| 516 | static struct usb_class_driver skel_class = { |
Greg Kroah-Hartman | d6e5bcf | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 517 | .name = "skel%d", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | .fops = &skel_fops, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | .minor_base = USB_SKEL_MINOR_BASE, |
| 520 | }; |
| 521 | |
Greg Kroah-Hartman | 3ae9da1 | 2009-09-11 16:07:30 -0700 | [diff] [blame] | 522 | static int skel_probe(struct usb_interface *interface, |
| 523 | const struct usb_device_id *id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | { |
Luiz Fernando N. Capitulino | c070454 | 2006-08-14 22:44:29 -0300 | [diff] [blame] | 525 | struct usb_skel *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | struct usb_host_interface *iface_desc; |
| 527 | struct usb_endpoint_descriptor *endpoint; |
| 528 | size_t buffer_size; |
| 529 | int i; |
| 530 | int retval = -ENOMEM; |
| 531 | |
| 532 | /* allocate memory for our device state and initialize it */ |
Oliver Neukum | ff90651 | 2005-12-21 19:27:29 +0100 | [diff] [blame] | 533 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
Luiz Fernando N. Capitulino | c070454 | 2006-08-14 22:44:29 -0300 | [diff] [blame] | 534 | if (!dev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | err("Out of memory"); |
| 536 | goto error; |
| 537 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | kref_init(&dev->kref); |
Oliver Neukum | ff90651 | 2005-12-21 19:27:29 +0100 | [diff] [blame] | 539 | sema_init(&dev->limit_sem, WRITES_IN_FLIGHT); |
Alan Stern | 121e287 | 2006-07-01 22:06:36 -0400 | [diff] [blame] | 540 | mutex_init(&dev->io_mutex); |
Oliver Neukum | 403dfb5 | 2007-05-25 13:42:56 +0200 | [diff] [blame] | 541 | spin_lock_init(&dev->err_lock); |
| 542 | init_usb_anchor(&dev->submitted); |
Oliver Neukum | e7389cc | 2009-09-09 17:06:53 +0200 | [diff] [blame] | 543 | init_completion(&dev->bulk_in_completion); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | |
| 545 | dev->udev = usb_get_dev(interface_to_usbdev(interface)); |
| 546 | dev->interface = interface; |
| 547 | |
| 548 | /* set up the endpoint information */ |
| 549 | /* use only the first bulk-in and bulk-out endpoints */ |
| 550 | iface_desc = interface->cur_altsetting; |
| 551 | for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { |
| 552 | endpoint = &iface_desc->endpoint[i].desc; |
| 553 | |
| 554 | if (!dev->bulk_in_endpointAddr && |
Luiz Fernando N. Capitulino | c070454 | 2006-08-14 22:44:29 -0300 | [diff] [blame] | 555 | usb_endpoint_is_bulk_in(endpoint)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | /* we found a bulk in endpoint */ |
Kuninori Morimoto | 29cc889 | 2011-08-23 03:12:03 -0700 | [diff] [blame] | 557 | buffer_size = usb_endpoint_maxp(endpoint); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | dev->bulk_in_size = buffer_size; |
| 559 | dev->bulk_in_endpointAddr = endpoint->bEndpointAddress; |
| 560 | dev->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL); |
| 561 | if (!dev->bulk_in_buffer) { |
| 562 | err("Could not allocate bulk_in_buffer"); |
| 563 | goto error; |
| 564 | } |
Oliver Neukum | e7389cc | 2009-09-09 17:06:53 +0200 | [diff] [blame] | 565 | dev->bulk_in_urb = usb_alloc_urb(0, GFP_KERNEL); |
| 566 | if (!dev->bulk_in_urb) { |
| 567 | err("Could not allocate bulk_in_urb"); |
| 568 | goto error; |
| 569 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | if (!dev->bulk_out_endpointAddr && |
Luiz Fernando N. Capitulino | c070454 | 2006-08-14 22:44:29 -0300 | [diff] [blame] | 573 | usb_endpoint_is_bulk_out(endpoint)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | /* we found a bulk out endpoint */ |
| 575 | dev->bulk_out_endpointAddr = endpoint->bEndpointAddress; |
| 576 | } |
| 577 | } |
| 578 | if (!(dev->bulk_in_endpointAddr && dev->bulk_out_endpointAddr)) { |
| 579 | err("Could not find both bulk-in and bulk-out endpoints"); |
| 580 | goto error; |
| 581 | } |
| 582 | |
| 583 | /* save our data pointer in this interface device */ |
| 584 | usb_set_intfdata(interface, dev); |
| 585 | |
| 586 | /* we can register the device now, as it is ready */ |
| 587 | retval = usb_register_dev(interface, &skel_class); |
| 588 | if (retval) { |
| 589 | /* something prevented us from registering this driver */ |
| 590 | err("Not able to get a minor for this device."); |
| 591 | usb_set_intfdata(interface, NULL); |
| 592 | goto error; |
| 593 | } |
| 594 | |
| 595 | /* let the user know what node this device is now attached to */ |
Matt Kraai | a5f5ea2 | 2009-02-06 19:38:51 -0800 | [diff] [blame] | 596 | dev_info(&interface->dev, |
| 597 | "USB Skeleton device now attached to USBSkel-%d", |
| 598 | interface->minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | return 0; |
| 600 | |
| 601 | error: |
| 602 | if (dev) |
Oliver Neukum | ba35e02 | 2007-03-01 14:31:02 +0100 | [diff] [blame] | 603 | /* this frees allocated memory */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | kref_put(&dev->kref, skel_delete); |
| 605 | return retval; |
| 606 | } |
| 607 | |
| 608 | static void skel_disconnect(struct usb_interface *interface) |
| 609 | { |
| 610 | struct usb_skel *dev; |
| 611 | int minor = interface->minor; |
| 612 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | dev = usb_get_intfdata(interface); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | |
| 615 | /* give back our minor */ |
| 616 | usb_deregister_dev(interface, &skel_class); |
| 617 | |
Alan Stern | 121e287 | 2006-07-01 22:06:36 -0400 | [diff] [blame] | 618 | /* prevent more I/O from starting */ |
| 619 | mutex_lock(&dev->io_mutex); |
| 620 | dev->interface = NULL; |
| 621 | mutex_unlock(&dev->io_mutex); |
| 622 | |
Oliver Neukum | e73c724 | 2007-06-11 14:54:02 +0200 | [diff] [blame] | 623 | usb_kill_anchored_urbs(&dev->submitted); |
| 624 | |
Ming Lei | 26c71a7 | 2011-12-16 22:20:01 +0800 | [diff] [blame] | 625 | mutex_lock(&skel_mutex); |
| 626 | usb_set_intfdata(interface, NULL); |
| 627 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | /* decrement our usage count */ |
| 629 | kref_put(&dev->kref, skel_delete); |
Ming Lei | 26c71a7 | 2011-12-16 22:20:01 +0800 | [diff] [blame] | 630 | mutex_unlock(&skel_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | |
Matt Kraai | a5f5ea2 | 2009-02-06 19:38:51 -0800 | [diff] [blame] | 632 | dev_info(&interface->dev, "USB Skeleton #%d now disconnected", minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | } |
| 634 | |
Oliver Neukum | 403dfb5 | 2007-05-25 13:42:56 +0200 | [diff] [blame] | 635 | static void skel_draw_down(struct usb_skel *dev) |
| 636 | { |
| 637 | int time; |
| 638 | |
| 639 | time = usb_wait_anchor_empty_timeout(&dev->submitted, 1000); |
| 640 | if (!time) |
| 641 | usb_kill_anchored_urbs(&dev->submitted); |
Oliver Neukum | e7389cc | 2009-09-09 17:06:53 +0200 | [diff] [blame] | 642 | usb_kill_urb(dev->bulk_in_urb); |
Oliver Neukum | 403dfb5 | 2007-05-25 13:42:56 +0200 | [diff] [blame] | 643 | } |
| 644 | |
Oliver Neukum | 758f7e16 | 2007-06-11 14:55:08 +0200 | [diff] [blame] | 645 | static int skel_suspend(struct usb_interface *intf, pm_message_t message) |
| 646 | { |
| 647 | struct usb_skel *dev = usb_get_intfdata(intf); |
| 648 | |
| 649 | if (!dev) |
| 650 | return 0; |
| 651 | skel_draw_down(dev); |
| 652 | return 0; |
| 653 | } |
| 654 | |
Greg Kroah-Hartman | 3ae9da1 | 2009-09-11 16:07:30 -0700 | [diff] [blame] | 655 | static int skel_resume(struct usb_interface *intf) |
Oliver Neukum | 758f7e16 | 2007-06-11 14:55:08 +0200 | [diff] [blame] | 656 | { |
| 657 | return 0; |
| 658 | } |
| 659 | |
Oliver Neukum | 87d093e | 2007-06-11 14:55:51 +0200 | [diff] [blame] | 660 | static int skel_pre_reset(struct usb_interface *intf) |
| 661 | { |
| 662 | struct usb_skel *dev = usb_get_intfdata(intf); |
| 663 | |
| 664 | mutex_lock(&dev->io_mutex); |
| 665 | skel_draw_down(dev); |
| 666 | |
| 667 | return 0; |
| 668 | } |
| 669 | |
| 670 | static int skel_post_reset(struct usb_interface *intf) |
| 671 | { |
| 672 | struct usb_skel *dev = usb_get_intfdata(intf); |
| 673 | |
| 674 | /* we are sure no URBs are active - no locking needed */ |
| 675 | dev->errors = -EPIPE; |
| 676 | mutex_unlock(&dev->io_mutex); |
| 677 | |
| 678 | return 0; |
| 679 | } |
| 680 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | static struct usb_driver skel_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | .name = "skeleton", |
| 683 | .probe = skel_probe, |
| 684 | .disconnect = skel_disconnect, |
Oliver Neukum | 758f7e16 | 2007-06-11 14:55:08 +0200 | [diff] [blame] | 685 | .suspend = skel_suspend, |
| 686 | .resume = skel_resume, |
Oliver Neukum | 87d093e | 2007-06-11 14:55:51 +0200 | [diff] [blame] | 687 | .pre_reset = skel_pre_reset, |
| 688 | .post_reset = skel_post_reset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | .id_table = skel_table, |
Oliver Neukum | ba35e02 | 2007-03-01 14:31:02 +0100 | [diff] [blame] | 690 | .supports_autosuspend = 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | }; |
| 692 | |
| 693 | static int __init usb_skel_init(void) |
| 694 | { |
| 695 | int result; |
| 696 | |
| 697 | /* register this driver with the USB subsystem */ |
| 698 | result = usb_register(&skel_driver); |
| 699 | if (result) |
| 700 | err("usb_register failed. Error number %d", result); |
| 701 | |
| 702 | return result; |
| 703 | } |
| 704 | |
| 705 | static void __exit usb_skel_exit(void) |
| 706 | { |
| 707 | /* deregister this driver with the USB subsystem */ |
| 708 | usb_deregister(&skel_driver); |
| 709 | } |
| 710 | |
Luiz Fernando N. Capitulino | c070454 | 2006-08-14 22:44:29 -0300 | [diff] [blame] | 711 | module_init(usb_skel_init); |
| 712 | module_exit(usb_skel_exit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | |
| 714 | MODULE_LICENSE("GPL"); |