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