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