11da177e4SLinus Torvalds /* 2c0704541SLuiz Fernando N. Capitulino * USB Skeleton driver - 2.2 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 2001-2004 Greg Kroah-Hartman (greg@kroah.com) 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 71da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License as 81da177e4SLinus Torvalds * published by the Free Software Foundation, version 2. 91da177e4SLinus Torvalds * 101da177e4SLinus Torvalds * This driver is based on the 2.6.3 version of drivers/usb/usb-skeleton.c 11121e287cSAlan Stern * but has been rewritten to be easier to read and use. 121da177e4SLinus Torvalds * 131da177e4SLinus Torvalds */ 141da177e4SLinus Torvalds 151da177e4SLinus Torvalds #include <linux/kernel.h> 161da177e4SLinus Torvalds #include <linux/errno.h> 171da177e4SLinus Torvalds #include <linux/init.h> 181da177e4SLinus Torvalds #include <linux/slab.h> 191da177e4SLinus Torvalds #include <linux/module.h> 201da177e4SLinus Torvalds #include <linux/kref.h> 211da177e4SLinus Torvalds #include <asm/uaccess.h> 221da177e4SLinus Torvalds #include <linux/usb.h> 23121e287cSAlan Stern #include <linux/mutex.h> 241da177e4SLinus Torvalds 251da177e4SLinus Torvalds 261da177e4SLinus Torvalds /* Define these values to match your devices */ 271da177e4SLinus Torvalds #define USB_SKEL_VENDOR_ID 0xfff0 281da177e4SLinus Torvalds #define USB_SKEL_PRODUCT_ID 0xfff0 291da177e4SLinus Torvalds 301da177e4SLinus Torvalds /* table of devices that work with this driver */ 311da177e4SLinus Torvalds static struct usb_device_id skel_table [] = { 321da177e4SLinus Torvalds { USB_DEVICE(USB_SKEL_VENDOR_ID, USB_SKEL_PRODUCT_ID) }, 331da177e4SLinus Torvalds { } /* Terminating entry */ 341da177e4SLinus Torvalds }; 351da177e4SLinus Torvalds MODULE_DEVICE_TABLE(usb, skel_table); 361da177e4SLinus Torvalds 371da177e4SLinus Torvalds 381da177e4SLinus Torvalds /* Get a minor range for your devices from the usb maintainer */ 391da177e4SLinus Torvalds #define USB_SKEL_MINOR_BASE 192 401da177e4SLinus Torvalds 41ff906518SOliver Neukum /* our private defines. if this grows any larger, use your own .h file */ 42ff906518SOliver Neukum #define MAX_TRANSFER (PAGE_SIZE - 512) 43ba35e02bSOliver Neukum /* MAX_TRANSFER is chosen so that the VM is not stressed by 44ba35e02bSOliver Neukum allocations > PAGE_SIZE and the number of packets in a page 45ba35e02bSOliver Neukum is an integer 512 is the largest possible packet on EHCI */ 46ff906518SOliver Neukum #define WRITES_IN_FLIGHT 8 47ba35e02bSOliver Neukum /* arbitrarily chosen */ 48ff906518SOliver Neukum 491da177e4SLinus Torvalds /* Structure to hold all of our device specific stuff */ 501da177e4SLinus Torvalds struct usb_skel { 51ba35e02bSOliver Neukum struct usb_device *udev; /* the usb device for this device */ 521da177e4SLinus Torvalds struct usb_interface *interface; /* the interface for this device */ 53ff906518SOliver Neukum struct semaphore limit_sem; /* limiting the number of writes in progress */ 54403dfb58SOliver Neukum struct usb_anchor submitted; /* in case we need to retract our submissions */ 551da177e4SLinus Torvalds unsigned char *bulk_in_buffer; /* the buffer to receive data */ 561da177e4SLinus Torvalds size_t bulk_in_size; /* the size of the receive buffer */ 571da177e4SLinus Torvalds __u8 bulk_in_endpointAddr; /* the address of the bulk in endpoint */ 581da177e4SLinus Torvalds __u8 bulk_out_endpointAddr; /* the address of the bulk out endpoint */ 59403dfb58SOliver Neukum int errors; /* the last request tanked */ 60*758f7e16SOliver Neukum int open_count; /* count the number of openers */ 61403dfb58SOliver Neukum spinlock_t err_lock; /* lock for errors */ 621da177e4SLinus Torvalds struct kref kref; 63121e287cSAlan Stern struct mutex io_mutex; /* synchronize I/O with disconnect */ 641da177e4SLinus Torvalds }; 651da177e4SLinus Torvalds #define to_skel_dev(d) container_of(d, struct usb_skel, kref) 661da177e4SLinus Torvalds 671da177e4SLinus Torvalds static struct usb_driver skel_driver; 68403dfb58SOliver Neukum static void skel_draw_down(struct usb_skel *dev); 691da177e4SLinus Torvalds 701da177e4SLinus Torvalds static void skel_delete(struct kref *kref) 711da177e4SLinus Torvalds { 721da177e4SLinus Torvalds struct usb_skel *dev = to_skel_dev(kref); 731da177e4SLinus Torvalds 741da177e4SLinus Torvalds usb_put_dev(dev->udev); 751da177e4SLinus Torvalds kfree(dev->bulk_in_buffer); 761da177e4SLinus Torvalds kfree(dev); 771da177e4SLinus Torvalds } 781da177e4SLinus Torvalds 791da177e4SLinus Torvalds static int skel_open(struct inode *inode, struct file *file) 801da177e4SLinus Torvalds { 811da177e4SLinus Torvalds struct usb_skel *dev; 821da177e4SLinus Torvalds struct usb_interface *interface; 831da177e4SLinus Torvalds int subminor; 841da177e4SLinus Torvalds int retval = 0; 851da177e4SLinus Torvalds 861da177e4SLinus Torvalds subminor = iminor(inode); 871da177e4SLinus Torvalds 881da177e4SLinus Torvalds interface = usb_find_interface(&skel_driver, subminor); 891da177e4SLinus Torvalds if (!interface) { 901da177e4SLinus Torvalds err ("%s - error, can't find device for minor %d", 911da177e4SLinus Torvalds __FUNCTION__, subminor); 921da177e4SLinus Torvalds retval = -ENODEV; 931da177e4SLinus Torvalds goto exit; 941da177e4SLinus Torvalds } 951da177e4SLinus Torvalds 961da177e4SLinus Torvalds dev = usb_get_intfdata(interface); 971da177e4SLinus Torvalds if (!dev) { 981da177e4SLinus Torvalds retval = -ENODEV; 991da177e4SLinus Torvalds goto exit; 1001da177e4SLinus Torvalds } 1011da177e4SLinus Torvalds 1021da177e4SLinus Torvalds /* increment our usage count for the device */ 1031da177e4SLinus Torvalds kref_get(&dev->kref); 1041da177e4SLinus Torvalds 105*758f7e16SOliver Neukum /* lock the device to allow correctly handling errors 106*758f7e16SOliver Neukum * in resumption */ 107*758f7e16SOliver Neukum mutex_lock(&dev->io_mutex); 108*758f7e16SOliver Neukum 109*758f7e16SOliver Neukum if (!dev->open_count++) { 1105b064708SOliver Neukum retval = usb_autopm_get_interface(interface); 1115b064708SOliver Neukum if (retval) { 112*758f7e16SOliver Neukum dev->open_count--; 113*758f7e16SOliver Neukum mutex_unlock(&dev->io_mutex); 1145b064708SOliver Neukum kref_put(&dev->kref, skel_delete); 1155b064708SOliver Neukum goto exit; 1165b064708SOliver Neukum } 117*758f7e16SOliver Neukum } /* else { //uncomment this block if you want exclusive open 118*758f7e16SOliver Neukum retval = -EBUSY; 119*758f7e16SOliver Neukum dev->open_count--; 120*758f7e16SOliver Neukum mutex_unlock(&dev->io_mutex); 121*758f7e16SOliver Neukum kref_put(&dev->kref, skel_delete); 122*758f7e16SOliver Neukum goto exit; 123*758f7e16SOliver Neukum } */ 124*758f7e16SOliver Neukum /* prevent the device from being autosuspended */ 1255b064708SOliver Neukum 1261da177e4SLinus Torvalds /* save our object in the file's private structure */ 1271da177e4SLinus Torvalds file->private_data = dev; 1281da177e4SLinus Torvalds 1291da177e4SLinus Torvalds exit: 1301da177e4SLinus Torvalds return retval; 1311da177e4SLinus Torvalds } 1321da177e4SLinus Torvalds 1331da177e4SLinus Torvalds static int skel_release(struct inode *inode, struct file *file) 1341da177e4SLinus Torvalds { 1351da177e4SLinus Torvalds struct usb_skel *dev; 1361da177e4SLinus Torvalds 1371da177e4SLinus Torvalds dev = (struct usb_skel *)file->private_data; 1381da177e4SLinus Torvalds if (dev == NULL) 1391da177e4SLinus Torvalds return -ENODEV; 1401da177e4SLinus Torvalds 14101d883d4SAlan Stern /* allow the device to be autosuspended */ 14201d883d4SAlan Stern mutex_lock(&dev->io_mutex); 143*758f7e16SOliver Neukum if (!--dev->open_count && dev->interface) 14401d883d4SAlan Stern usb_autopm_put_interface(dev->interface); 14501d883d4SAlan Stern mutex_unlock(&dev->io_mutex); 14601d883d4SAlan Stern 1471da177e4SLinus Torvalds /* decrement the count on our device */ 1481da177e4SLinus Torvalds kref_put(&dev->kref, skel_delete); 1491da177e4SLinus Torvalds return 0; 1501da177e4SLinus Torvalds } 1511da177e4SLinus Torvalds 152403dfb58SOliver Neukum static int skel_flush(struct file *file, fl_owner_t id) 153403dfb58SOliver Neukum { 154403dfb58SOliver Neukum struct usb_skel *dev; 155403dfb58SOliver Neukum int res; 156403dfb58SOliver Neukum 157403dfb58SOliver Neukum dev = (struct usb_skel *)file->private_data; 158403dfb58SOliver Neukum if (dev == NULL) 159403dfb58SOliver Neukum return -ENODEV; 160403dfb58SOliver Neukum 161403dfb58SOliver Neukum /* wait for io to stop */ 162403dfb58SOliver Neukum mutex_lock(&dev->io_mutex); 163403dfb58SOliver Neukum skel_draw_down(dev); 164403dfb58SOliver Neukum 165403dfb58SOliver Neukum /* read out errors, leave subsequent opens a clean slate */ 166403dfb58SOliver Neukum spin_lock_irq(&dev->err_lock); 167403dfb58SOliver Neukum res = dev->errors ? (dev->errors == -EPIPE ? -EPIPE : -EIO) : 0; 168403dfb58SOliver Neukum dev->errors = 0; 169403dfb58SOliver Neukum spin_unlock_irq(&dev->err_lock); 170403dfb58SOliver Neukum 171403dfb58SOliver Neukum mutex_unlock(&dev->io_mutex); 172403dfb58SOliver Neukum 173403dfb58SOliver Neukum return res; 174403dfb58SOliver Neukum } 175403dfb58SOliver Neukum 1761da177e4SLinus Torvalds static ssize_t skel_read(struct file *file, char *buffer, size_t count, loff_t *ppos) 1771da177e4SLinus Torvalds { 1781da177e4SLinus Torvalds struct usb_skel *dev; 179c0704541SLuiz Fernando N. Capitulino int retval; 1801da177e4SLinus Torvalds int bytes_read; 1811da177e4SLinus Torvalds 1821da177e4SLinus Torvalds dev = (struct usb_skel *)file->private_data; 1831da177e4SLinus Torvalds 184121e287cSAlan Stern mutex_lock(&dev->io_mutex); 185121e287cSAlan Stern if (!dev->interface) { /* disconnect() was called */ 186121e287cSAlan Stern retval = -ENODEV; 187121e287cSAlan Stern goto exit; 188121e287cSAlan Stern } 189121e287cSAlan Stern 1901da177e4SLinus Torvalds /* do a blocking bulk read to get data from the device */ 1911da177e4SLinus Torvalds retval = usb_bulk_msg(dev->udev, 1921da177e4SLinus Torvalds usb_rcvbulkpipe(dev->udev, dev->bulk_in_endpointAddr), 1931da177e4SLinus Torvalds dev->bulk_in_buffer, 1941da177e4SLinus Torvalds min(dev->bulk_in_size, count), 1951da177e4SLinus Torvalds &bytes_read, 10000); 1961da177e4SLinus Torvalds 1971da177e4SLinus Torvalds /* if the read was successful, copy the data to userspace */ 1981da177e4SLinus Torvalds if (!retval) { 1991da177e4SLinus Torvalds if (copy_to_user(buffer, dev->bulk_in_buffer, bytes_read)) 2001da177e4SLinus Torvalds retval = -EFAULT; 2011da177e4SLinus Torvalds else 2021da177e4SLinus Torvalds retval = bytes_read; 2031da177e4SLinus Torvalds } 2041da177e4SLinus Torvalds 205121e287cSAlan Stern exit: 206121e287cSAlan Stern mutex_unlock(&dev->io_mutex); 2071da177e4SLinus Torvalds return retval; 2081da177e4SLinus Torvalds } 2091da177e4SLinus Torvalds 2107d12e780SDavid Howells static void skel_write_bulk_callback(struct urb *urb) 2111da177e4SLinus Torvalds { 2121da177e4SLinus Torvalds struct usb_skel *dev; 2131da177e4SLinus Torvalds 2141da177e4SLinus Torvalds dev = (struct usb_skel *)urb->context; 2151da177e4SLinus Torvalds 2161da177e4SLinus Torvalds /* sync/async unlink faults aren't errors */ 217403dfb58SOliver Neukum if (urb->status) { 218403dfb58SOliver Neukum if(!(urb->status == -ENOENT || 2191da177e4SLinus Torvalds urb->status == -ECONNRESET || 220403dfb58SOliver Neukum urb->status == -ESHUTDOWN)) 221c0704541SLuiz Fernando N. Capitulino err("%s - nonzero write bulk status received: %d", 2221da177e4SLinus Torvalds __FUNCTION__, urb->status); 223403dfb58SOliver Neukum 224403dfb58SOliver Neukum spin_lock(&dev->err_lock); 225403dfb58SOliver Neukum dev->errors = urb->status; 226403dfb58SOliver Neukum spin_unlock(&dev->err_lock); 2271da177e4SLinus Torvalds } 2281da177e4SLinus Torvalds 2291da177e4SLinus Torvalds /* free up our allocated buffer */ 2301da177e4SLinus Torvalds usb_buffer_free(urb->dev, urb->transfer_buffer_length, 2311da177e4SLinus Torvalds urb->transfer_buffer, urb->transfer_dma); 232ff906518SOliver Neukum up(&dev->limit_sem); 2331da177e4SLinus Torvalds } 2341da177e4SLinus Torvalds 2351da177e4SLinus Torvalds static ssize_t skel_write(struct file *file, const char *user_buffer, size_t count, loff_t *ppos) 2361da177e4SLinus Torvalds { 2371da177e4SLinus Torvalds struct usb_skel *dev; 2381da177e4SLinus Torvalds int retval = 0; 2391da177e4SLinus Torvalds struct urb *urb = NULL; 2401da177e4SLinus Torvalds char *buf = NULL; 241c8dd7709SSam Bishop size_t writesize = min(count, (size_t)MAX_TRANSFER); 2421da177e4SLinus Torvalds 2431da177e4SLinus Torvalds dev = (struct usb_skel *)file->private_data; 2441da177e4SLinus Torvalds 2451da177e4SLinus Torvalds /* verify that we actually have some data to write */ 2461da177e4SLinus Torvalds if (count == 0) 2471da177e4SLinus Torvalds goto exit; 2481da177e4SLinus Torvalds 249ff906518SOliver Neukum /* limit the number of URBs in flight to stop a user from using up all RAM */ 250c8dd7709SSam Bishop if (down_interruptible(&dev->limit_sem)) { 251c8dd7709SSam Bishop retval = -ERESTARTSYS; 252c8dd7709SSam Bishop goto exit; 253c8dd7709SSam Bishop } 254ff906518SOliver Neukum 255403dfb58SOliver Neukum spin_lock_irq(&dev->err_lock); 256403dfb58SOliver Neukum if ((retval = dev->errors) < 0) { 257403dfb58SOliver Neukum /* any error is reported once */ 258403dfb58SOliver Neukum dev->errors = 0; 259403dfb58SOliver Neukum /* to preserve notifications about reset */ 260403dfb58SOliver Neukum retval = (retval == -EPIPE) ? retval : -EIO; 261403dfb58SOliver Neukum } 262403dfb58SOliver Neukum spin_unlock_irq(&dev->err_lock); 263403dfb58SOliver Neukum if (retval < 0) 264403dfb58SOliver Neukum goto error; 265403dfb58SOliver Neukum 2661da177e4SLinus Torvalds /* create a urb, and a buffer for it, and copy the data to the urb */ 2671da177e4SLinus Torvalds urb = usb_alloc_urb(0, GFP_KERNEL); 2681da177e4SLinus Torvalds if (!urb) { 2691da177e4SLinus Torvalds retval = -ENOMEM; 2701da177e4SLinus Torvalds goto error; 2711da177e4SLinus Torvalds } 2721da177e4SLinus Torvalds 273ff906518SOliver Neukum buf = usb_buffer_alloc(dev->udev, writesize, GFP_KERNEL, &urb->transfer_dma); 2741da177e4SLinus Torvalds if (!buf) { 2751da177e4SLinus Torvalds retval = -ENOMEM; 2761da177e4SLinus Torvalds goto error; 2771da177e4SLinus Torvalds } 2781da177e4SLinus Torvalds 279ff906518SOliver Neukum if (copy_from_user(buf, user_buffer, writesize)) { 2801da177e4SLinus Torvalds retval = -EFAULT; 2811da177e4SLinus Torvalds goto error; 2821da177e4SLinus Torvalds } 2831da177e4SLinus Torvalds 284ba35e02bSOliver Neukum /* this lock makes sure we don't submit URBs to gone devices */ 285ba35e02bSOliver Neukum mutex_lock(&dev->io_mutex); 286ba35e02bSOliver Neukum if (!dev->interface) { /* disconnect() was called */ 287ba35e02bSOliver Neukum mutex_unlock(&dev->io_mutex); 288ba35e02bSOliver Neukum retval = -ENODEV; 289ba35e02bSOliver Neukum goto error; 290ba35e02bSOliver Neukum } 291ba35e02bSOliver Neukum 2921da177e4SLinus Torvalds /* initialize the urb properly */ 2931da177e4SLinus Torvalds usb_fill_bulk_urb(urb, dev->udev, 2941da177e4SLinus Torvalds usb_sndbulkpipe(dev->udev, dev->bulk_out_endpointAddr), 295ff906518SOliver Neukum buf, writesize, skel_write_bulk_callback, dev); 2961da177e4SLinus Torvalds urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 297403dfb58SOliver Neukum usb_anchor_urb(urb, &dev->submitted); 2981da177e4SLinus Torvalds 2991da177e4SLinus Torvalds /* send the data out the bulk port */ 3001da177e4SLinus Torvalds retval = usb_submit_urb(urb, GFP_KERNEL); 301ba35e02bSOliver Neukum mutex_unlock(&dev->io_mutex); 3021da177e4SLinus Torvalds if (retval) { 3031da177e4SLinus Torvalds err("%s - failed submitting write urb, error %d", __FUNCTION__, retval); 304403dfb58SOliver Neukum goto error_unanchor; 3051da177e4SLinus Torvalds } 3061da177e4SLinus Torvalds 3071da177e4SLinus Torvalds /* release our reference to this urb, the USB core will eventually free it entirely */ 3081da177e4SLinus Torvalds usb_free_urb(urb); 3091da177e4SLinus Torvalds 310ba35e02bSOliver Neukum 311ff906518SOliver Neukum return writesize; 3121da177e4SLinus Torvalds 313403dfb58SOliver Neukum error_unanchor: 314403dfb58SOliver Neukum usb_unanchor_urb(urb); 3151da177e4SLinus Torvalds error: 316121e287cSAlan Stern if (urb) { 317ff906518SOliver Neukum usb_buffer_free(dev->udev, writesize, buf, urb->transfer_dma); 3181da177e4SLinus Torvalds usb_free_urb(urb); 319121e287cSAlan Stern } 320ff906518SOliver Neukum up(&dev->limit_sem); 321121e287cSAlan Stern 322121e287cSAlan Stern exit: 3231da177e4SLinus Torvalds return retval; 3241da177e4SLinus Torvalds } 3251da177e4SLinus Torvalds 326066202ddSLuiz Fernando N. Capitulino static const struct file_operations skel_fops = { 3271da177e4SLinus Torvalds .owner = THIS_MODULE, 3281da177e4SLinus Torvalds .read = skel_read, 3291da177e4SLinus Torvalds .write = skel_write, 3301da177e4SLinus Torvalds .open = skel_open, 3311da177e4SLinus Torvalds .release = skel_release, 332403dfb58SOliver Neukum .flush = skel_flush, 3331da177e4SLinus Torvalds }; 3341da177e4SLinus Torvalds 3351da177e4SLinus Torvalds /* 3361da177e4SLinus Torvalds * usb class driver info in order to get a minor number from the usb core, 337595b14cbSGreg Kroah-Hartman * and to have the device registered with the driver core 3381da177e4SLinus Torvalds */ 3391da177e4SLinus Torvalds static struct usb_class_driver skel_class = { 340d6e5bcf4SGreg Kroah-Hartman .name = "skel%d", 3411da177e4SLinus Torvalds .fops = &skel_fops, 3421da177e4SLinus Torvalds .minor_base = USB_SKEL_MINOR_BASE, 3431da177e4SLinus Torvalds }; 3441da177e4SLinus Torvalds 3451da177e4SLinus Torvalds static int skel_probe(struct usb_interface *interface, const struct usb_device_id *id) 3461da177e4SLinus Torvalds { 347c0704541SLuiz Fernando N. Capitulino struct usb_skel *dev; 3481da177e4SLinus Torvalds struct usb_host_interface *iface_desc; 3491da177e4SLinus Torvalds struct usb_endpoint_descriptor *endpoint; 3501da177e4SLinus Torvalds size_t buffer_size; 3511da177e4SLinus Torvalds int i; 3521da177e4SLinus Torvalds int retval = -ENOMEM; 3531da177e4SLinus Torvalds 3541da177e4SLinus Torvalds /* allocate memory for our device state and initialize it */ 355ff906518SOliver Neukum dev = kzalloc(sizeof(*dev), GFP_KERNEL); 356c0704541SLuiz Fernando N. Capitulino if (!dev) { 3571da177e4SLinus Torvalds err("Out of memory"); 3581da177e4SLinus Torvalds goto error; 3591da177e4SLinus Torvalds } 3601da177e4SLinus Torvalds kref_init(&dev->kref); 361ff906518SOliver Neukum sema_init(&dev->limit_sem, WRITES_IN_FLIGHT); 362121e287cSAlan Stern mutex_init(&dev->io_mutex); 363403dfb58SOliver Neukum spin_lock_init(&dev->err_lock); 364403dfb58SOliver Neukum init_usb_anchor(&dev->submitted); 3651da177e4SLinus Torvalds 3661da177e4SLinus Torvalds dev->udev = usb_get_dev(interface_to_usbdev(interface)); 3671da177e4SLinus Torvalds dev->interface = interface; 3681da177e4SLinus Torvalds 3691da177e4SLinus Torvalds /* set up the endpoint information */ 3701da177e4SLinus Torvalds /* use only the first bulk-in and bulk-out endpoints */ 3711da177e4SLinus Torvalds iface_desc = interface->cur_altsetting; 3721da177e4SLinus Torvalds for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { 3731da177e4SLinus Torvalds endpoint = &iface_desc->endpoint[i].desc; 3741da177e4SLinus Torvalds 3751da177e4SLinus Torvalds if (!dev->bulk_in_endpointAddr && 376c0704541SLuiz Fernando N. Capitulino usb_endpoint_is_bulk_in(endpoint)) { 3771da177e4SLinus Torvalds /* we found a bulk in endpoint */ 3781da177e4SLinus Torvalds buffer_size = le16_to_cpu(endpoint->wMaxPacketSize); 3791da177e4SLinus Torvalds dev->bulk_in_size = buffer_size; 3801da177e4SLinus Torvalds dev->bulk_in_endpointAddr = endpoint->bEndpointAddress; 3811da177e4SLinus Torvalds dev->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL); 3821da177e4SLinus Torvalds if (!dev->bulk_in_buffer) { 3831da177e4SLinus Torvalds err("Could not allocate bulk_in_buffer"); 3841da177e4SLinus Torvalds goto error; 3851da177e4SLinus Torvalds } 3861da177e4SLinus Torvalds } 3871da177e4SLinus Torvalds 3881da177e4SLinus Torvalds if (!dev->bulk_out_endpointAddr && 389c0704541SLuiz Fernando N. Capitulino usb_endpoint_is_bulk_out(endpoint)) { 3901da177e4SLinus Torvalds /* we found a bulk out endpoint */ 3911da177e4SLinus Torvalds dev->bulk_out_endpointAddr = endpoint->bEndpointAddress; 3921da177e4SLinus Torvalds } 3931da177e4SLinus Torvalds } 3941da177e4SLinus Torvalds if (!(dev->bulk_in_endpointAddr && dev->bulk_out_endpointAddr)) { 3951da177e4SLinus Torvalds err("Could not find both bulk-in and bulk-out endpoints"); 3961da177e4SLinus Torvalds goto error; 3971da177e4SLinus Torvalds } 3981da177e4SLinus Torvalds 3991da177e4SLinus Torvalds /* save our data pointer in this interface device */ 4001da177e4SLinus Torvalds usb_set_intfdata(interface, dev); 4011da177e4SLinus Torvalds 4021da177e4SLinus Torvalds /* we can register the device now, as it is ready */ 4031da177e4SLinus Torvalds retval = usb_register_dev(interface, &skel_class); 4041da177e4SLinus Torvalds if (retval) { 4051da177e4SLinus Torvalds /* something prevented us from registering this driver */ 4061da177e4SLinus Torvalds err("Not able to get a minor for this device."); 4071da177e4SLinus Torvalds usb_set_intfdata(interface, NULL); 4081da177e4SLinus Torvalds goto error; 4091da177e4SLinus Torvalds } 4101da177e4SLinus Torvalds 4111da177e4SLinus Torvalds /* let the user know what node this device is now attached to */ 4121da177e4SLinus Torvalds info("USB Skeleton device now attached to USBSkel-%d", interface->minor); 4131da177e4SLinus Torvalds return 0; 4141da177e4SLinus Torvalds 4151da177e4SLinus Torvalds error: 4161da177e4SLinus Torvalds if (dev) 417ba35e02bSOliver Neukum /* this frees allocated memory */ 4181da177e4SLinus Torvalds kref_put(&dev->kref, skel_delete); 4191da177e4SLinus Torvalds return retval; 4201da177e4SLinus Torvalds } 4211da177e4SLinus Torvalds 4221da177e4SLinus Torvalds static void skel_disconnect(struct usb_interface *interface) 4231da177e4SLinus Torvalds { 4241da177e4SLinus Torvalds struct usb_skel *dev; 4251da177e4SLinus Torvalds int minor = interface->minor; 4261da177e4SLinus Torvalds 4271da177e4SLinus Torvalds dev = usb_get_intfdata(interface); 4281da177e4SLinus Torvalds usb_set_intfdata(interface, NULL); 4291da177e4SLinus Torvalds 4301da177e4SLinus Torvalds /* give back our minor */ 4311da177e4SLinus Torvalds usb_deregister_dev(interface, &skel_class); 4321da177e4SLinus Torvalds 433121e287cSAlan Stern /* prevent more I/O from starting */ 434121e287cSAlan Stern mutex_lock(&dev->io_mutex); 435121e287cSAlan Stern dev->interface = NULL; 436121e287cSAlan Stern mutex_unlock(&dev->io_mutex); 437121e287cSAlan Stern 438e73c7247SOliver Neukum usb_kill_anchored_urbs(&dev->submitted); 439e73c7247SOliver Neukum 4401da177e4SLinus Torvalds /* decrement our usage count */ 4411da177e4SLinus Torvalds kref_put(&dev->kref, skel_delete); 4421da177e4SLinus Torvalds 4431da177e4SLinus Torvalds info("USB Skeleton #%d now disconnected", minor); 4441da177e4SLinus Torvalds } 4451da177e4SLinus Torvalds 446403dfb58SOliver Neukum static void skel_draw_down(struct usb_skel *dev) 447403dfb58SOliver Neukum { 448403dfb58SOliver Neukum int time; 449403dfb58SOliver Neukum 450403dfb58SOliver Neukum time = usb_wait_anchor_empty_timeout(&dev->submitted, 1000); 451403dfb58SOliver Neukum if (!time) 452403dfb58SOliver Neukum usb_kill_anchored_urbs(&dev->submitted); 453403dfb58SOliver Neukum } 454403dfb58SOliver Neukum 455*758f7e16SOliver Neukum static int skel_suspend(struct usb_interface *intf, pm_message_t message) 456*758f7e16SOliver Neukum { 457*758f7e16SOliver Neukum struct usb_skel *dev = usb_get_intfdata(intf); 458*758f7e16SOliver Neukum 459*758f7e16SOliver Neukum if (!dev) 460*758f7e16SOliver Neukum return 0; 461*758f7e16SOliver Neukum skel_draw_down(dev); 462*758f7e16SOliver Neukum return 0; 463*758f7e16SOliver Neukum } 464*758f7e16SOliver Neukum 465*758f7e16SOliver Neukum static int skel_resume (struct usb_interface *intf) 466*758f7e16SOliver Neukum { 467*758f7e16SOliver Neukum return 0; 468*758f7e16SOliver Neukum } 469*758f7e16SOliver Neukum 4701da177e4SLinus Torvalds static struct usb_driver skel_driver = { 4711da177e4SLinus Torvalds .name = "skeleton", 4721da177e4SLinus Torvalds .probe = skel_probe, 4731da177e4SLinus Torvalds .disconnect = skel_disconnect, 474*758f7e16SOliver Neukum .suspend = skel_suspend, 475*758f7e16SOliver Neukum .resume = skel_resume, 4761da177e4SLinus Torvalds .id_table = skel_table, 477ba35e02bSOliver Neukum .supports_autosuspend = 1, 4781da177e4SLinus Torvalds }; 4791da177e4SLinus Torvalds 4801da177e4SLinus Torvalds static int __init usb_skel_init(void) 4811da177e4SLinus Torvalds { 4821da177e4SLinus Torvalds int result; 4831da177e4SLinus Torvalds 4841da177e4SLinus Torvalds /* register this driver with the USB subsystem */ 4851da177e4SLinus Torvalds result = usb_register(&skel_driver); 4861da177e4SLinus Torvalds if (result) 4871da177e4SLinus Torvalds err("usb_register failed. Error number %d", result); 4881da177e4SLinus Torvalds 4891da177e4SLinus Torvalds return result; 4901da177e4SLinus Torvalds } 4911da177e4SLinus Torvalds 4921da177e4SLinus Torvalds static void __exit usb_skel_exit(void) 4931da177e4SLinus Torvalds { 4941da177e4SLinus Torvalds /* deregister this driver with the USB subsystem */ 4951da177e4SLinus Torvalds usb_deregister(&skel_driver); 4961da177e4SLinus Torvalds } 4971da177e4SLinus Torvalds 4981da177e4SLinus Torvalds module_init(usb_skel_init); 4991da177e4SLinus Torvalds module_exit(usb_skel_exit); 5001da177e4SLinus Torvalds 5011da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 502