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