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> |
| 21 | #include <asm/uaccess.h> |
| 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 | |
| 30 | /* table of devices that work with this driver */ |
| 31 | static struct usb_device_id skel_table [] = { |
| 32 | { USB_DEVICE(USB_SKEL_VENDOR_ID, USB_SKEL_PRODUCT_ID) }, |
| 33 | { } /* Terminating entry */ |
| 34 | }; |
Luiz Fernando N. Capitulino | c070454 | 2006-08-14 22:44:29 -0300 | [diff] [blame] | 35 | MODULE_DEVICE_TABLE(usb, skel_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
| 37 | |
| 38 | /* Get a minor range for your devices from the usb maintainer */ |
| 39 | #define USB_SKEL_MINOR_BASE 192 |
| 40 | |
Oliver Neukum | ff90651 | 2005-12-21 19:27:29 +0100 | [diff] [blame] | 41 | /* 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] | 42 | #define MAX_TRANSFER (PAGE_SIZE - 512) |
Oliver Neukum | ba35e02 | 2007-03-01 14:31:02 +0100 | [diff] [blame] | 43 | /* MAX_TRANSFER is chosen so that the VM is not stressed by |
| 44 | allocations > PAGE_SIZE and the number of packets in a page |
| 45 | is an integer 512 is the largest possible packet on EHCI */ |
Oliver Neukum | ff90651 | 2005-12-21 19:27:29 +0100 | [diff] [blame] | 46 | #define WRITES_IN_FLIGHT 8 |
Oliver Neukum | ba35e02 | 2007-03-01 14:31:02 +0100 | [diff] [blame] | 47 | /* arbitrarily chosen */ |
Oliver Neukum | ff90651 | 2005-12-21 19:27:29 +0100 | [diff] [blame] | 48 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | /* Structure to hold all of our device specific stuff */ |
| 50 | struct usb_skel { |
Oliver Neukum | ba35e02 | 2007-03-01 14:31:02 +0100 | [diff] [blame] | 51 | struct usb_device *udev; /* the usb device for this device */ |
| 52 | struct usb_interface *interface; /* the interface for this device */ |
Oliver Neukum | ff90651 | 2005-12-21 19:27:29 +0100 | [diff] [blame] | 53 | struct semaphore limit_sem; /* limiting the number of writes in progress */ |
Oliver Neukum | 403dfb5 | 2007-05-25 13:42:56 +0200 | [diff] [blame] | 54 | struct usb_anchor submitted; /* in case we need to retract our submissions */ |
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 */ |
| 57 | __u8 bulk_in_endpointAddr; /* the address of the bulk in endpoint */ |
| 58 | __u8 bulk_out_endpointAddr; /* the address of the bulk out endpoint */ |
Oliver Neukum | 403dfb5 | 2007-05-25 13:42:56 +0200 | [diff] [blame] | 59 | int errors; /* the last request tanked */ |
Oliver Neukum | 758f7e16 | 2007-06-11 14:55:08 +0200 | [diff] [blame] | 60 | int open_count; /* count the number of openers */ |
Oliver Neukum | 403dfb5 | 2007-05-25 13:42:56 +0200 | [diff] [blame] | 61 | spinlock_t err_lock; /* lock for errors */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | struct kref kref; |
Alan Stern | 121e287 | 2006-07-01 22:06:36 -0400 | [diff] [blame] | 63 | struct mutex io_mutex; /* synchronize I/O with disconnect */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | }; |
| 65 | #define to_skel_dev(d) container_of(d, struct usb_skel, kref) |
| 66 | |
| 67 | static struct usb_driver skel_driver; |
Oliver Neukum | 403dfb5 | 2007-05-25 13:42:56 +0200 | [diff] [blame] | 68 | static void skel_draw_down(struct usb_skel *dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
| 70 | static void skel_delete(struct kref *kref) |
Luiz Fernando N. Capitulino | c070454 | 2006-08-14 22:44:29 -0300 | [diff] [blame] | 71 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | struct usb_skel *dev = to_skel_dev(kref); |
| 73 | |
| 74 | usb_put_dev(dev->udev); |
Luiz Fernando N. Capitulino | c070454 | 2006-08-14 22:44:29 -0300 | [diff] [blame] | 75 | kfree(dev->bulk_in_buffer); |
| 76 | kfree(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | static int skel_open(struct inode *inode, struct file *file) |
| 80 | { |
| 81 | struct usb_skel *dev; |
| 82 | struct usb_interface *interface; |
| 83 | int subminor; |
| 84 | int retval = 0; |
| 85 | |
| 86 | subminor = iminor(inode); |
| 87 | |
| 88 | interface = usb_find_interface(&skel_driver, subminor); |
| 89 | if (!interface) { |
| 90 | err ("%s - error, can't find device for minor %d", |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame^] | 91 | __func__, subminor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | retval = -ENODEV; |
| 93 | goto exit; |
| 94 | } |
| 95 | |
| 96 | dev = usb_get_intfdata(interface); |
| 97 | if (!dev) { |
| 98 | retval = -ENODEV; |
| 99 | goto exit; |
| 100 | } |
| 101 | |
| 102 | /* increment our usage count for the device */ |
| 103 | kref_get(&dev->kref); |
| 104 | |
Oliver Neukum | 758f7e16 | 2007-06-11 14:55:08 +0200 | [diff] [blame] | 105 | /* lock the device to allow correctly handling errors |
| 106 | * in resumption */ |
| 107 | mutex_lock(&dev->io_mutex); |
| 108 | |
| 109 | if (!dev->open_count++) { |
| 110 | retval = usb_autopm_get_interface(interface); |
| 111 | if (retval) { |
| 112 | dev->open_count--; |
| 113 | mutex_unlock(&dev->io_mutex); |
| 114 | kref_put(&dev->kref, skel_delete); |
| 115 | goto exit; |
| 116 | } |
| 117 | } /* else { //uncomment this block if you want exclusive open |
| 118 | retval = -EBUSY; |
| 119 | dev->open_count--; |
| 120 | mutex_unlock(&dev->io_mutex); |
Oliver Neukum | 5b06470 | 2007-02-08 15:42:53 +0100 | [diff] [blame] | 121 | kref_put(&dev->kref, skel_delete); |
| 122 | goto exit; |
Oliver Neukum | 758f7e16 | 2007-06-11 14:55:08 +0200 | [diff] [blame] | 123 | } */ |
| 124 | /* prevent the device from being autosuspended */ |
Oliver Neukum | 5b06470 | 2007-02-08 15:42:53 +0100 | [diff] [blame] | 125 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | /* save our object in the file's private structure */ |
| 127 | file->private_data = dev; |
Mark Gross | f729405 | 2007-09-24 09:28:14 -0700 | [diff] [blame] | 128 | mutex_unlock(&dev->io_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | |
| 130 | exit: |
| 131 | return retval; |
| 132 | } |
| 133 | |
| 134 | static int skel_release(struct inode *inode, struct file *file) |
| 135 | { |
| 136 | struct usb_skel *dev; |
| 137 | |
| 138 | dev = (struct usb_skel *)file->private_data; |
| 139 | if (dev == NULL) |
| 140 | return -ENODEV; |
| 141 | |
Alan Stern | 01d883d | 2006-08-30 15:47:18 -0400 | [diff] [blame] | 142 | /* allow the device to be autosuspended */ |
| 143 | mutex_lock(&dev->io_mutex); |
Oliver Neukum | 758f7e16 | 2007-06-11 14:55:08 +0200 | [diff] [blame] | 144 | if (!--dev->open_count && dev->interface) |
Alan Stern | 01d883d | 2006-08-30 15:47:18 -0400 | [diff] [blame] | 145 | usb_autopm_put_interface(dev->interface); |
| 146 | mutex_unlock(&dev->io_mutex); |
| 147 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | /* decrement the count on our device */ |
| 149 | kref_put(&dev->kref, skel_delete); |
| 150 | return 0; |
| 151 | } |
| 152 | |
Oliver Neukum | 403dfb5 | 2007-05-25 13:42:56 +0200 | [diff] [blame] | 153 | static int skel_flush(struct file *file, fl_owner_t id) |
| 154 | { |
| 155 | struct usb_skel *dev; |
| 156 | int res; |
| 157 | |
| 158 | dev = (struct usb_skel *)file->private_data; |
| 159 | if (dev == NULL) |
| 160 | return -ENODEV; |
| 161 | |
| 162 | /* wait for io to stop */ |
| 163 | mutex_lock(&dev->io_mutex); |
| 164 | skel_draw_down(dev); |
| 165 | |
| 166 | /* read out errors, leave subsequent opens a clean slate */ |
| 167 | spin_lock_irq(&dev->err_lock); |
| 168 | res = dev->errors ? (dev->errors == -EPIPE ? -EPIPE : -EIO) : 0; |
| 169 | dev->errors = 0; |
| 170 | spin_unlock_irq(&dev->err_lock); |
| 171 | |
| 172 | mutex_unlock(&dev->io_mutex); |
| 173 | |
| 174 | return res; |
| 175 | } |
| 176 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | static ssize_t skel_read(struct file *file, char *buffer, size_t count, loff_t *ppos) |
| 178 | { |
| 179 | struct usb_skel *dev; |
Luiz Fernando N. Capitulino | c070454 | 2006-08-14 22:44:29 -0300 | [diff] [blame] | 180 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | int bytes_read; |
| 182 | |
| 183 | dev = (struct usb_skel *)file->private_data; |
Alan Stern | 121e287 | 2006-07-01 22:06:36 -0400 | [diff] [blame] | 184 | |
| 185 | mutex_lock(&dev->io_mutex); |
| 186 | if (!dev->interface) { /* disconnect() was called */ |
| 187 | retval = -ENODEV; |
| 188 | goto exit; |
| 189 | } |
| 190 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | /* do a blocking bulk read to get data from the device */ |
| 192 | retval = usb_bulk_msg(dev->udev, |
| 193 | usb_rcvbulkpipe(dev->udev, dev->bulk_in_endpointAddr), |
| 194 | dev->bulk_in_buffer, |
| 195 | min(dev->bulk_in_size, count), |
| 196 | &bytes_read, 10000); |
| 197 | |
| 198 | /* if the read was successful, copy the data to userspace */ |
| 199 | if (!retval) { |
| 200 | if (copy_to_user(buffer, dev->bulk_in_buffer, bytes_read)) |
| 201 | retval = -EFAULT; |
| 202 | else |
| 203 | retval = bytes_read; |
| 204 | } |
| 205 | |
Alan Stern | 121e287 | 2006-07-01 22:06:36 -0400 | [diff] [blame] | 206 | exit: |
| 207 | mutex_unlock(&dev->io_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | return retval; |
| 209 | } |
| 210 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 211 | static void skel_write_bulk_callback(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | { |
| 213 | struct usb_skel *dev; |
| 214 | |
| 215 | dev = (struct usb_skel *)urb->context; |
| 216 | |
| 217 | /* sync/async unlink faults aren't errors */ |
Oliver Neukum | 403dfb5 | 2007-05-25 13:42:56 +0200 | [diff] [blame] | 218 | if (urb->status) { |
| 219 | if(!(urb->status == -ENOENT || |
| 220 | urb->status == -ECONNRESET || |
| 221 | urb->status == -ESHUTDOWN)) |
| 222 | err("%s - nonzero write bulk status received: %d", |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame^] | 223 | __func__, urb->status); |
Oliver Neukum | 403dfb5 | 2007-05-25 13:42:56 +0200 | [diff] [blame] | 224 | |
| 225 | spin_lock(&dev->err_lock); |
| 226 | dev->errors = urb->status; |
| 227 | spin_unlock(&dev->err_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | /* free up our allocated buffer */ |
Luiz Fernando N. Capitulino | c070454 | 2006-08-14 22:44:29 -0300 | [diff] [blame] | 231 | usb_buffer_free(urb->dev, urb->transfer_buffer_length, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | urb->transfer_buffer, urb->transfer_dma); |
Oliver Neukum | ff90651 | 2005-12-21 19:27:29 +0100 | [diff] [blame] | 233 | up(&dev->limit_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | static ssize_t skel_write(struct file *file, const char *user_buffer, size_t count, loff_t *ppos) |
| 237 | { |
| 238 | struct usb_skel *dev; |
| 239 | int retval = 0; |
| 240 | struct urb *urb = NULL; |
| 241 | char *buf = NULL; |
Sam Bishop | c8dd770 | 2005-12-22 17:11:02 -0700 | [diff] [blame] | 242 | size_t writesize = min(count, (size_t)MAX_TRANSFER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | |
| 244 | dev = (struct usb_skel *)file->private_data; |
| 245 | |
| 246 | /* verify that we actually have some data to write */ |
| 247 | if (count == 0) |
| 248 | goto exit; |
| 249 | |
Oliver Neukum | ff90651 | 2005-12-21 19:27:29 +0100 | [diff] [blame] | 250 | /* limit the number of URBs in flight to stop a user from using up all RAM */ |
Sam Bishop | c8dd770 | 2005-12-22 17:11:02 -0700 | [diff] [blame] | 251 | if (down_interruptible(&dev->limit_sem)) { |
| 252 | retval = -ERESTARTSYS; |
| 253 | goto exit; |
| 254 | } |
Oliver Neukum | ff90651 | 2005-12-21 19:27:29 +0100 | [diff] [blame] | 255 | |
Oliver Neukum | 403dfb5 | 2007-05-25 13:42:56 +0200 | [diff] [blame] | 256 | spin_lock_irq(&dev->err_lock); |
| 257 | if ((retval = dev->errors) < 0) { |
| 258 | /* any error is reported once */ |
| 259 | dev->errors = 0; |
| 260 | /* to preserve notifications about reset */ |
| 261 | retval = (retval == -EPIPE) ? retval : -EIO; |
| 262 | } |
| 263 | spin_unlock_irq(&dev->err_lock); |
| 264 | if (retval < 0) |
| 265 | goto error; |
| 266 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | /* create a urb, and a buffer for it, and copy the data to the urb */ |
| 268 | urb = usb_alloc_urb(0, GFP_KERNEL); |
| 269 | if (!urb) { |
| 270 | retval = -ENOMEM; |
| 271 | goto error; |
| 272 | } |
| 273 | |
Oliver Neukum | ff90651 | 2005-12-21 19:27:29 +0100 | [diff] [blame] | 274 | buf = usb_buffer_alloc(dev->udev, writesize, GFP_KERNEL, &urb->transfer_dma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | if (!buf) { |
| 276 | retval = -ENOMEM; |
| 277 | goto error; |
| 278 | } |
| 279 | |
Oliver Neukum | ff90651 | 2005-12-21 19:27:29 +0100 | [diff] [blame] | 280 | if (copy_from_user(buf, user_buffer, writesize)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | retval = -EFAULT; |
| 282 | goto error; |
| 283 | } |
| 284 | |
Oliver Neukum | ba35e02 | 2007-03-01 14:31:02 +0100 | [diff] [blame] | 285 | /* this lock makes sure we don't submit URBs to gone devices */ |
| 286 | mutex_lock(&dev->io_mutex); |
| 287 | if (!dev->interface) { /* disconnect() was called */ |
| 288 | mutex_unlock(&dev->io_mutex); |
| 289 | retval = -ENODEV; |
| 290 | goto error; |
| 291 | } |
| 292 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | /* initialize the urb properly */ |
| 294 | usb_fill_bulk_urb(urb, dev->udev, |
| 295 | usb_sndbulkpipe(dev->udev, dev->bulk_out_endpointAddr), |
Oliver Neukum | ff90651 | 2005-12-21 19:27:29 +0100 | [diff] [blame] | 296 | buf, writesize, skel_write_bulk_callback, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; |
Oliver Neukum | 403dfb5 | 2007-05-25 13:42:56 +0200 | [diff] [blame] | 298 | usb_anchor_urb(urb, &dev->submitted); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | |
| 300 | /* send the data out the bulk port */ |
| 301 | retval = usb_submit_urb(urb, GFP_KERNEL); |
Oliver Neukum | ba35e02 | 2007-03-01 14:31:02 +0100 | [diff] [blame] | 302 | mutex_unlock(&dev->io_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | if (retval) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame^] | 304 | err("%s - failed submitting write urb, error %d", __func__, retval); |
Oliver Neukum | 403dfb5 | 2007-05-25 13:42:56 +0200 | [diff] [blame] | 305 | goto error_unanchor; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | /* release our reference to this urb, the USB core will eventually free it entirely */ |
| 309 | usb_free_urb(urb); |
| 310 | |
Oliver Neukum | ba35e02 | 2007-03-01 14:31:02 +0100 | [diff] [blame] | 311 | |
Oliver Neukum | ff90651 | 2005-12-21 19:27:29 +0100 | [diff] [blame] | 312 | return writesize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | |
Oliver Neukum | 403dfb5 | 2007-05-25 13:42:56 +0200 | [diff] [blame] | 314 | error_unanchor: |
| 315 | usb_unanchor_urb(urb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | error: |
Alan Stern | 121e287 | 2006-07-01 22:06:36 -0400 | [diff] [blame] | 317 | if (urb) { |
| 318 | usb_buffer_free(dev->udev, writesize, buf, urb->transfer_dma); |
| 319 | usb_free_urb(urb); |
| 320 | } |
Oliver Neukum | ff90651 | 2005-12-21 19:27:29 +0100 | [diff] [blame] | 321 | up(&dev->limit_sem); |
Alan Stern | 121e287 | 2006-07-01 22:06:36 -0400 | [diff] [blame] | 322 | |
| 323 | exit: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | return retval; |
| 325 | } |
| 326 | |
Luiz Fernando N. Capitulino | 066202d | 2006-08-05 20:37:11 -0300 | [diff] [blame] | 327 | static const struct file_operations skel_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | .owner = THIS_MODULE, |
| 329 | .read = skel_read, |
| 330 | .write = skel_write, |
| 331 | .open = skel_open, |
| 332 | .release = skel_release, |
Oliver Neukum | 403dfb5 | 2007-05-25 13:42:56 +0200 | [diff] [blame] | 333 | .flush = skel_flush, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | }; |
| 335 | |
Luiz Fernando N. Capitulino | c070454 | 2006-08-14 22:44:29 -0300 | [diff] [blame] | 336 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | * 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] | 338 | * and to have the device registered with the driver core |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | */ |
| 340 | static struct usb_class_driver skel_class = { |
Greg Kroah-Hartman | d6e5bcf | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 341 | .name = "skel%d", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | .fops = &skel_fops, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | .minor_base = USB_SKEL_MINOR_BASE, |
| 344 | }; |
| 345 | |
| 346 | static int skel_probe(struct usb_interface *interface, const struct usb_device_id *id) |
| 347 | { |
Luiz Fernando N. Capitulino | c070454 | 2006-08-14 22:44:29 -0300 | [diff] [blame] | 348 | struct usb_skel *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | struct usb_host_interface *iface_desc; |
| 350 | struct usb_endpoint_descriptor *endpoint; |
| 351 | size_t buffer_size; |
| 352 | int i; |
| 353 | int retval = -ENOMEM; |
| 354 | |
| 355 | /* allocate memory for our device state and initialize it */ |
Oliver Neukum | ff90651 | 2005-12-21 19:27:29 +0100 | [diff] [blame] | 356 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
Luiz Fernando N. Capitulino | c070454 | 2006-08-14 22:44:29 -0300 | [diff] [blame] | 357 | if (!dev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | err("Out of memory"); |
| 359 | goto error; |
| 360 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | kref_init(&dev->kref); |
Oliver Neukum | ff90651 | 2005-12-21 19:27:29 +0100 | [diff] [blame] | 362 | sema_init(&dev->limit_sem, WRITES_IN_FLIGHT); |
Alan Stern | 121e287 | 2006-07-01 22:06:36 -0400 | [diff] [blame] | 363 | mutex_init(&dev->io_mutex); |
Oliver Neukum | 403dfb5 | 2007-05-25 13:42:56 +0200 | [diff] [blame] | 364 | spin_lock_init(&dev->err_lock); |
| 365 | init_usb_anchor(&dev->submitted); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | |
| 367 | dev->udev = usb_get_dev(interface_to_usbdev(interface)); |
| 368 | dev->interface = interface; |
| 369 | |
| 370 | /* set up the endpoint information */ |
| 371 | /* use only the first bulk-in and bulk-out endpoints */ |
| 372 | iface_desc = interface->cur_altsetting; |
| 373 | for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { |
| 374 | endpoint = &iface_desc->endpoint[i].desc; |
| 375 | |
| 376 | if (!dev->bulk_in_endpointAddr && |
Luiz Fernando N. Capitulino | c070454 | 2006-08-14 22:44:29 -0300 | [diff] [blame] | 377 | usb_endpoint_is_bulk_in(endpoint)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | /* we found a bulk in endpoint */ |
| 379 | buffer_size = le16_to_cpu(endpoint->wMaxPacketSize); |
| 380 | dev->bulk_in_size = buffer_size; |
| 381 | dev->bulk_in_endpointAddr = endpoint->bEndpointAddress; |
| 382 | dev->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL); |
| 383 | if (!dev->bulk_in_buffer) { |
| 384 | err("Could not allocate bulk_in_buffer"); |
| 385 | goto error; |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | if (!dev->bulk_out_endpointAddr && |
Luiz Fernando N. Capitulino | c070454 | 2006-08-14 22:44:29 -0300 | [diff] [blame] | 390 | usb_endpoint_is_bulk_out(endpoint)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | /* we found a bulk out endpoint */ |
| 392 | dev->bulk_out_endpointAddr = endpoint->bEndpointAddress; |
| 393 | } |
| 394 | } |
| 395 | if (!(dev->bulk_in_endpointAddr && dev->bulk_out_endpointAddr)) { |
| 396 | err("Could not find both bulk-in and bulk-out endpoints"); |
| 397 | goto error; |
| 398 | } |
| 399 | |
| 400 | /* save our data pointer in this interface device */ |
| 401 | usb_set_intfdata(interface, dev); |
| 402 | |
| 403 | /* we can register the device now, as it is ready */ |
| 404 | retval = usb_register_dev(interface, &skel_class); |
| 405 | if (retval) { |
| 406 | /* something prevented us from registering this driver */ |
| 407 | err("Not able to get a minor for this device."); |
| 408 | usb_set_intfdata(interface, NULL); |
| 409 | goto error; |
| 410 | } |
| 411 | |
| 412 | /* let the user know what node this device is now attached to */ |
| 413 | info("USB Skeleton device now attached to USBSkel-%d", interface->minor); |
| 414 | return 0; |
| 415 | |
| 416 | error: |
| 417 | if (dev) |
Oliver Neukum | ba35e02 | 2007-03-01 14:31:02 +0100 | [diff] [blame] | 418 | /* this frees allocated memory */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | kref_put(&dev->kref, skel_delete); |
| 420 | return retval; |
| 421 | } |
| 422 | |
| 423 | static void skel_disconnect(struct usb_interface *interface) |
| 424 | { |
| 425 | struct usb_skel *dev; |
| 426 | int minor = interface->minor; |
| 427 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | dev = usb_get_intfdata(interface); |
| 429 | usb_set_intfdata(interface, NULL); |
| 430 | |
| 431 | /* give back our minor */ |
| 432 | usb_deregister_dev(interface, &skel_class); |
| 433 | |
Alan Stern | 121e287 | 2006-07-01 22:06:36 -0400 | [diff] [blame] | 434 | /* prevent more I/O from starting */ |
| 435 | mutex_lock(&dev->io_mutex); |
| 436 | dev->interface = NULL; |
| 437 | mutex_unlock(&dev->io_mutex); |
| 438 | |
Oliver Neukum | e73c724 | 2007-06-11 14:54:02 +0200 | [diff] [blame] | 439 | usb_kill_anchored_urbs(&dev->submitted); |
| 440 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | /* decrement our usage count */ |
| 442 | kref_put(&dev->kref, skel_delete); |
| 443 | |
| 444 | info("USB Skeleton #%d now disconnected", minor); |
| 445 | } |
| 446 | |
Oliver Neukum | 403dfb5 | 2007-05-25 13:42:56 +0200 | [diff] [blame] | 447 | static void skel_draw_down(struct usb_skel *dev) |
| 448 | { |
| 449 | int time; |
| 450 | |
| 451 | time = usb_wait_anchor_empty_timeout(&dev->submitted, 1000); |
| 452 | if (!time) |
| 453 | usb_kill_anchored_urbs(&dev->submitted); |
| 454 | } |
| 455 | |
Oliver Neukum | 758f7e16 | 2007-06-11 14:55:08 +0200 | [diff] [blame] | 456 | static int skel_suspend(struct usb_interface *intf, pm_message_t message) |
| 457 | { |
| 458 | struct usb_skel *dev = usb_get_intfdata(intf); |
| 459 | |
| 460 | if (!dev) |
| 461 | return 0; |
| 462 | skel_draw_down(dev); |
| 463 | return 0; |
| 464 | } |
| 465 | |
| 466 | static int skel_resume (struct usb_interface *intf) |
| 467 | { |
| 468 | return 0; |
| 469 | } |
| 470 | |
Oliver Neukum | 87d093e | 2007-06-11 14:55:51 +0200 | [diff] [blame] | 471 | static int skel_pre_reset(struct usb_interface *intf) |
| 472 | { |
| 473 | struct usb_skel *dev = usb_get_intfdata(intf); |
| 474 | |
| 475 | mutex_lock(&dev->io_mutex); |
| 476 | skel_draw_down(dev); |
| 477 | |
| 478 | return 0; |
| 479 | } |
| 480 | |
| 481 | static int skel_post_reset(struct usb_interface *intf) |
| 482 | { |
| 483 | struct usb_skel *dev = usb_get_intfdata(intf); |
| 484 | |
| 485 | /* we are sure no URBs are active - no locking needed */ |
| 486 | dev->errors = -EPIPE; |
| 487 | mutex_unlock(&dev->io_mutex); |
| 488 | |
| 489 | return 0; |
| 490 | } |
| 491 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | static struct usb_driver skel_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | .name = "skeleton", |
| 494 | .probe = skel_probe, |
| 495 | .disconnect = skel_disconnect, |
Oliver Neukum | 758f7e16 | 2007-06-11 14:55:08 +0200 | [diff] [blame] | 496 | .suspend = skel_suspend, |
| 497 | .resume = skel_resume, |
Oliver Neukum | 87d093e | 2007-06-11 14:55:51 +0200 | [diff] [blame] | 498 | .pre_reset = skel_pre_reset, |
| 499 | .post_reset = skel_post_reset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | .id_table = skel_table, |
Oliver Neukum | ba35e02 | 2007-03-01 14:31:02 +0100 | [diff] [blame] | 501 | .supports_autosuspend = 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | }; |
| 503 | |
| 504 | static int __init usb_skel_init(void) |
| 505 | { |
| 506 | int result; |
| 507 | |
| 508 | /* register this driver with the USB subsystem */ |
| 509 | result = usb_register(&skel_driver); |
| 510 | if (result) |
| 511 | err("usb_register failed. Error number %d", result); |
| 512 | |
| 513 | return result; |
| 514 | } |
| 515 | |
| 516 | static void __exit usb_skel_exit(void) |
| 517 | { |
| 518 | /* deregister this driver with the USB subsystem */ |
| 519 | usb_deregister(&skel_driver); |
| 520 | } |
| 521 | |
Luiz Fernando N. Capitulino | c070454 | 2006-08-14 22:44:29 -0300 | [diff] [blame] | 522 | module_init(usb_skel_init); |
| 523 | module_exit(usb_skel_exit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | |
| 525 | MODULE_LICENSE("GPL"); |