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