xref: /linux/drivers/usb/usb-skeleton.c (revision 066202dd48cf3296b6cc22b5fcf89aef33fa0efc)
11da177e4SLinus Torvalds /*
2121e287cSAlan Stern  * USB Skeleton driver - 2.1
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 )
43ff906518SOliver Neukum #define WRITES_IN_FLIGHT	8
44ff906518SOliver Neukum 
451da177e4SLinus Torvalds /* Structure to hold all of our device specific stuff */
461da177e4SLinus Torvalds struct usb_skel {
471da177e4SLinus Torvalds 	struct usb_device *	udev;			/* the usb device for this device */
481da177e4SLinus Torvalds 	struct usb_interface *	interface;		/* the interface for this device */
49ff906518SOliver Neukum 	struct semaphore	limit_sem;		/* limiting the number of writes in progress */
501da177e4SLinus Torvalds 	unsigned char *		bulk_in_buffer;		/* the buffer to receive data */
511da177e4SLinus Torvalds 	size_t			bulk_in_size;		/* the size of the receive buffer */
521da177e4SLinus Torvalds 	__u8			bulk_in_endpointAddr;	/* the address of the bulk in endpoint */
531da177e4SLinus Torvalds 	__u8			bulk_out_endpointAddr;	/* the address of the bulk out endpoint */
541da177e4SLinus Torvalds 	struct kref		kref;
55121e287cSAlan Stern 	struct mutex		io_mutex;		/* synchronize I/O with disconnect */
561da177e4SLinus Torvalds };
571da177e4SLinus Torvalds #define to_skel_dev(d) container_of(d, struct usb_skel, kref)
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds static struct usb_driver skel_driver;
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds static void skel_delete(struct kref *kref)
621da177e4SLinus Torvalds {
631da177e4SLinus Torvalds 	struct usb_skel *dev = to_skel_dev(kref);
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds 	usb_put_dev(dev->udev);
661da177e4SLinus Torvalds 	kfree (dev->bulk_in_buffer);
671da177e4SLinus Torvalds 	kfree (dev);
681da177e4SLinus Torvalds }
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds static int skel_open(struct inode *inode, struct file *file)
711da177e4SLinus Torvalds {
721da177e4SLinus Torvalds 	struct usb_skel *dev;
731da177e4SLinus Torvalds 	struct usb_interface *interface;
741da177e4SLinus Torvalds 	int subminor;
751da177e4SLinus Torvalds 	int retval = 0;
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds 	subminor = iminor(inode);
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds 	interface = usb_find_interface(&skel_driver, subminor);
801da177e4SLinus Torvalds 	if (!interface) {
811da177e4SLinus Torvalds 		err ("%s - error, can't find device for minor %d",
821da177e4SLinus Torvalds 		     __FUNCTION__, subminor);
831da177e4SLinus Torvalds 		retval = -ENODEV;
841da177e4SLinus Torvalds 		goto exit;
851da177e4SLinus Torvalds 	}
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds 	dev = usb_get_intfdata(interface);
881da177e4SLinus Torvalds 	if (!dev) {
891da177e4SLinus Torvalds 		retval = -ENODEV;
901da177e4SLinus Torvalds 		goto exit;
911da177e4SLinus Torvalds 	}
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds 	/* increment our usage count for the device */
941da177e4SLinus Torvalds 	kref_get(&dev->kref);
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds 	/* save our object in the file's private structure */
971da177e4SLinus Torvalds 	file->private_data = dev;
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds exit:
1001da177e4SLinus Torvalds 	return retval;
1011da177e4SLinus Torvalds }
1021da177e4SLinus Torvalds 
1031da177e4SLinus Torvalds static int skel_release(struct inode *inode, struct file *file)
1041da177e4SLinus Torvalds {
1051da177e4SLinus Torvalds 	struct usb_skel *dev;
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds 	dev = (struct usb_skel *)file->private_data;
1081da177e4SLinus Torvalds 	if (dev == NULL)
1091da177e4SLinus Torvalds 		return -ENODEV;
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds 	/* decrement the count on our device */
1121da177e4SLinus Torvalds 	kref_put(&dev->kref, skel_delete);
1131da177e4SLinus Torvalds 	return 0;
1141da177e4SLinus Torvalds }
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds static ssize_t skel_read(struct file *file, char *buffer, size_t count, loff_t *ppos)
1171da177e4SLinus Torvalds {
1181da177e4SLinus Torvalds 	struct usb_skel *dev;
1191da177e4SLinus Torvalds 	int retval = 0;
1201da177e4SLinus Torvalds 	int bytes_read;
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds 	dev = (struct usb_skel *)file->private_data;
1231da177e4SLinus Torvalds 
124121e287cSAlan Stern 	mutex_lock(&dev->io_mutex);
125121e287cSAlan Stern 	if (!dev->interface) {		/* disconnect() was called */
126121e287cSAlan Stern 		retval = -ENODEV;
127121e287cSAlan Stern 		goto exit;
128121e287cSAlan Stern 	}
129121e287cSAlan Stern 
1301da177e4SLinus Torvalds 	/* do a blocking bulk read to get data from the device */
1311da177e4SLinus Torvalds 	retval = usb_bulk_msg(dev->udev,
1321da177e4SLinus Torvalds 			      usb_rcvbulkpipe(dev->udev, dev->bulk_in_endpointAddr),
1331da177e4SLinus Torvalds 			      dev->bulk_in_buffer,
1341da177e4SLinus Torvalds 			      min(dev->bulk_in_size, count),
1351da177e4SLinus Torvalds 			      &bytes_read, 10000);
1361da177e4SLinus Torvalds 
1371da177e4SLinus Torvalds 	/* if the read was successful, copy the data to userspace */
1381da177e4SLinus Torvalds 	if (!retval) {
1391da177e4SLinus Torvalds 		if (copy_to_user(buffer, dev->bulk_in_buffer, bytes_read))
1401da177e4SLinus Torvalds 			retval = -EFAULT;
1411da177e4SLinus Torvalds 		else
1421da177e4SLinus Torvalds 			retval = bytes_read;
1431da177e4SLinus Torvalds 	}
1441da177e4SLinus Torvalds 
145121e287cSAlan Stern exit:
146121e287cSAlan Stern 	mutex_unlock(&dev->io_mutex);
1471da177e4SLinus Torvalds 	return retval;
1481da177e4SLinus Torvalds }
1491da177e4SLinus Torvalds 
1501da177e4SLinus Torvalds static void skel_write_bulk_callback(struct urb *urb, struct pt_regs *regs)
1511da177e4SLinus Torvalds {
1521da177e4SLinus Torvalds 	struct usb_skel *dev;
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds 	dev = (struct usb_skel *)urb->context;
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds 	/* sync/async unlink faults aren't errors */
1571da177e4SLinus Torvalds 	if (urb->status &&
1581da177e4SLinus Torvalds 	    !(urb->status == -ENOENT ||
1591da177e4SLinus Torvalds 	      urb->status == -ECONNRESET ||
1601da177e4SLinus Torvalds 	      urb->status == -ESHUTDOWN)) {
1611da177e4SLinus Torvalds 		dbg("%s - nonzero write bulk status received: %d",
1621da177e4SLinus Torvalds 		    __FUNCTION__, urb->status);
1631da177e4SLinus Torvalds 	}
1641da177e4SLinus Torvalds 
1651da177e4SLinus Torvalds 	/* free up our allocated buffer */
1661da177e4SLinus Torvalds 	usb_buffer_free(urb->dev, urb->transfer_buffer_length,
1671da177e4SLinus Torvalds 			urb->transfer_buffer, urb->transfer_dma);
168ff906518SOliver Neukum 	up(&dev->limit_sem);
1691da177e4SLinus Torvalds }
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds static ssize_t skel_write(struct file *file, const char *user_buffer, size_t count, loff_t *ppos)
1721da177e4SLinus Torvalds {
1731da177e4SLinus Torvalds 	struct usb_skel *dev;
1741da177e4SLinus Torvalds 	int retval = 0;
1751da177e4SLinus Torvalds 	struct urb *urb = NULL;
1761da177e4SLinus Torvalds 	char *buf = NULL;
177c8dd7709SSam Bishop 	size_t writesize = min(count, (size_t)MAX_TRANSFER);
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds 	dev = (struct usb_skel *)file->private_data;
1801da177e4SLinus Torvalds 
1811da177e4SLinus Torvalds 	/* verify that we actually have some data to write */
1821da177e4SLinus Torvalds 	if (count == 0)
1831da177e4SLinus Torvalds 		goto exit;
1841da177e4SLinus Torvalds 
185ff906518SOliver Neukum 	/* limit the number of URBs in flight to stop a user from using up all RAM */
186c8dd7709SSam Bishop 	if (down_interruptible(&dev->limit_sem)) {
187c8dd7709SSam Bishop 		retval = -ERESTARTSYS;
188c8dd7709SSam Bishop 		goto exit;
189c8dd7709SSam Bishop 	}
190ff906518SOliver Neukum 
191121e287cSAlan Stern 	mutex_lock(&dev->io_mutex);
192121e287cSAlan Stern 	if (!dev->interface) {		/* disconnect() was called */
193121e287cSAlan Stern 		retval = -ENODEV;
194121e287cSAlan Stern 		goto error;
195121e287cSAlan Stern 	}
196121e287cSAlan Stern 
1971da177e4SLinus Torvalds 	/* create a urb, and a buffer for it, and copy the data to the urb */
1981da177e4SLinus Torvalds 	urb = usb_alloc_urb(0, GFP_KERNEL);
1991da177e4SLinus Torvalds 	if (!urb) {
2001da177e4SLinus Torvalds 		retval = -ENOMEM;
2011da177e4SLinus Torvalds 		goto error;
2021da177e4SLinus Torvalds 	}
2031da177e4SLinus Torvalds 
204ff906518SOliver Neukum 	buf = usb_buffer_alloc(dev->udev, writesize, GFP_KERNEL, &urb->transfer_dma);
2051da177e4SLinus Torvalds 	if (!buf) {
2061da177e4SLinus Torvalds 		retval = -ENOMEM;
2071da177e4SLinus Torvalds 		goto error;
2081da177e4SLinus Torvalds 	}
2091da177e4SLinus Torvalds 
210ff906518SOliver Neukum 	if (copy_from_user(buf, user_buffer, writesize)) {
2111da177e4SLinus Torvalds 		retval = -EFAULT;
2121da177e4SLinus Torvalds 		goto error;
2131da177e4SLinus Torvalds 	}
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds 	/* initialize the urb properly */
2161da177e4SLinus Torvalds 	usb_fill_bulk_urb(urb, dev->udev,
2171da177e4SLinus Torvalds 			  usb_sndbulkpipe(dev->udev, dev->bulk_out_endpointAddr),
218ff906518SOliver Neukum 			  buf, writesize, skel_write_bulk_callback, dev);
2191da177e4SLinus Torvalds 	urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
2201da177e4SLinus Torvalds 
2211da177e4SLinus Torvalds 	/* send the data out the bulk port */
2221da177e4SLinus Torvalds 	retval = usb_submit_urb(urb, GFP_KERNEL);
2231da177e4SLinus Torvalds 	if (retval) {
2241da177e4SLinus Torvalds 		err("%s - failed submitting write urb, error %d", __FUNCTION__, retval);
2251da177e4SLinus Torvalds 		goto error;
2261da177e4SLinus Torvalds 	}
2271da177e4SLinus Torvalds 
2281da177e4SLinus Torvalds 	/* release our reference to this urb, the USB core will eventually free it entirely */
2291da177e4SLinus Torvalds 	usb_free_urb(urb);
2301da177e4SLinus Torvalds 
231121e287cSAlan Stern 	mutex_unlock(&dev->io_mutex);
232ff906518SOliver Neukum 	return writesize;
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds error:
235121e287cSAlan Stern 	if (urb) {
236ff906518SOliver Neukum 		usb_buffer_free(dev->udev, writesize, buf, urb->transfer_dma);
2371da177e4SLinus Torvalds 		usb_free_urb(urb);
238121e287cSAlan Stern 	}
239121e287cSAlan Stern 	mutex_unlock(&dev->io_mutex);
240ff906518SOliver Neukum 	up(&dev->limit_sem);
241121e287cSAlan Stern 
242121e287cSAlan Stern exit:
2431da177e4SLinus Torvalds 	return retval;
2441da177e4SLinus Torvalds }
2451da177e4SLinus Torvalds 
246*066202ddSLuiz Fernando N. Capitulino static const struct file_operations skel_fops = {
2471da177e4SLinus Torvalds 	.owner =	THIS_MODULE,
2481da177e4SLinus Torvalds 	.read =		skel_read,
2491da177e4SLinus Torvalds 	.write =	skel_write,
2501da177e4SLinus Torvalds 	.open =		skel_open,
2511da177e4SLinus Torvalds 	.release =	skel_release,
2521da177e4SLinus Torvalds };
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds /*
2551da177e4SLinus Torvalds  * usb class driver info in order to get a minor number from the usb core,
256595b14cbSGreg Kroah-Hartman  * and to have the device registered with the driver core
2571da177e4SLinus Torvalds  */
2581da177e4SLinus Torvalds static struct usb_class_driver skel_class = {
259d6e5bcf4SGreg Kroah-Hartman 	.name =		"skel%d",
2601da177e4SLinus Torvalds 	.fops =		&skel_fops,
2611da177e4SLinus Torvalds 	.minor_base =	USB_SKEL_MINOR_BASE,
2621da177e4SLinus Torvalds };
2631da177e4SLinus Torvalds 
2641da177e4SLinus Torvalds static int skel_probe(struct usb_interface *interface, const struct usb_device_id *id)
2651da177e4SLinus Torvalds {
2661da177e4SLinus Torvalds 	struct usb_skel *dev = NULL;
2671da177e4SLinus Torvalds 	struct usb_host_interface *iface_desc;
2681da177e4SLinus Torvalds 	struct usb_endpoint_descriptor *endpoint;
2691da177e4SLinus Torvalds 	size_t buffer_size;
2701da177e4SLinus Torvalds 	int i;
2711da177e4SLinus Torvalds 	int retval = -ENOMEM;
2721da177e4SLinus Torvalds 
2731da177e4SLinus Torvalds 	/* allocate memory for our device state and initialize it */
274ff906518SOliver Neukum 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2751da177e4SLinus Torvalds 	if (dev == NULL) {
2761da177e4SLinus Torvalds 		err("Out of memory");
2771da177e4SLinus Torvalds 		goto error;
2781da177e4SLinus Torvalds 	}
2791da177e4SLinus Torvalds 	kref_init(&dev->kref);
280ff906518SOliver Neukum 	sema_init(&dev->limit_sem, WRITES_IN_FLIGHT);
281121e287cSAlan Stern 	mutex_init(&dev->io_mutex);
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds 	dev->udev = usb_get_dev(interface_to_usbdev(interface));
2841da177e4SLinus Torvalds 	dev->interface = interface;
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds 	/* set up the endpoint information */
2871da177e4SLinus Torvalds 	/* use only the first bulk-in and bulk-out endpoints */
2881da177e4SLinus Torvalds 	iface_desc = interface->cur_altsetting;
2891da177e4SLinus Torvalds 	for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2901da177e4SLinus Torvalds 		endpoint = &iface_desc->endpoint[i].desc;
2911da177e4SLinus Torvalds 
2921da177e4SLinus Torvalds 		if (!dev->bulk_in_endpointAddr &&
2936b216df8SConger, Chris A 		    ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
2946b216df8SConger, Chris A 					== USB_DIR_IN) &&
2951da177e4SLinus Torvalds 		    ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
2961da177e4SLinus Torvalds 					== USB_ENDPOINT_XFER_BULK)) {
2971da177e4SLinus Torvalds 			/* we found a bulk in endpoint */
2981da177e4SLinus Torvalds 			buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
2991da177e4SLinus Torvalds 			dev->bulk_in_size = buffer_size;
3001da177e4SLinus Torvalds 			dev->bulk_in_endpointAddr = endpoint->bEndpointAddress;
3011da177e4SLinus Torvalds 			dev->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
3021da177e4SLinus Torvalds 			if (!dev->bulk_in_buffer) {
3031da177e4SLinus Torvalds 				err("Could not allocate bulk_in_buffer");
3041da177e4SLinus Torvalds 				goto error;
3051da177e4SLinus Torvalds 			}
3061da177e4SLinus Torvalds 		}
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds 		if (!dev->bulk_out_endpointAddr &&
3096b216df8SConger, Chris A 		    ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
3106b216df8SConger, Chris A 					== USB_DIR_OUT) &&
3111da177e4SLinus Torvalds 		    ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
3121da177e4SLinus Torvalds 					== USB_ENDPOINT_XFER_BULK)) {
3131da177e4SLinus Torvalds 			/* we found a bulk out endpoint */
3141da177e4SLinus Torvalds 			dev->bulk_out_endpointAddr = endpoint->bEndpointAddress;
3151da177e4SLinus Torvalds 		}
3161da177e4SLinus Torvalds 	}
3171da177e4SLinus Torvalds 	if (!(dev->bulk_in_endpointAddr && dev->bulk_out_endpointAddr)) {
3181da177e4SLinus Torvalds 		err("Could not find both bulk-in and bulk-out endpoints");
3191da177e4SLinus Torvalds 		goto error;
3201da177e4SLinus Torvalds 	}
3211da177e4SLinus Torvalds 
3221da177e4SLinus Torvalds 	/* save our data pointer in this interface device */
3231da177e4SLinus Torvalds 	usb_set_intfdata(interface, dev);
3241da177e4SLinus Torvalds 
3251da177e4SLinus Torvalds 	/* we can register the device now, as it is ready */
3261da177e4SLinus Torvalds 	retval = usb_register_dev(interface, &skel_class);
3271da177e4SLinus Torvalds 	if (retval) {
3281da177e4SLinus Torvalds 		/* something prevented us from registering this driver */
3291da177e4SLinus Torvalds 		err("Not able to get a minor for this device.");
3301da177e4SLinus Torvalds 		usb_set_intfdata(interface, NULL);
3311da177e4SLinus Torvalds 		goto error;
3321da177e4SLinus Torvalds 	}
3331da177e4SLinus Torvalds 
3341da177e4SLinus Torvalds 	/* let the user know what node this device is now attached to */
3351da177e4SLinus Torvalds 	info("USB Skeleton device now attached to USBSkel-%d", interface->minor);
3361da177e4SLinus Torvalds 	return 0;
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds error:
3391da177e4SLinus Torvalds 	if (dev)
3401da177e4SLinus Torvalds 		kref_put(&dev->kref, skel_delete);
3411da177e4SLinus Torvalds 	return retval;
3421da177e4SLinus Torvalds }
3431da177e4SLinus Torvalds 
3441da177e4SLinus Torvalds static void skel_disconnect(struct usb_interface *interface)
3451da177e4SLinus Torvalds {
3461da177e4SLinus Torvalds 	struct usb_skel *dev;
3471da177e4SLinus Torvalds 	int minor = interface->minor;
3481da177e4SLinus Torvalds 
3491da177e4SLinus Torvalds 	/* prevent skel_open() from racing skel_disconnect() */
3501da177e4SLinus Torvalds 	lock_kernel();
3511da177e4SLinus Torvalds 
3521da177e4SLinus Torvalds 	dev = usb_get_intfdata(interface);
3531da177e4SLinus Torvalds 	usb_set_intfdata(interface, NULL);
3541da177e4SLinus Torvalds 
3551da177e4SLinus Torvalds 	/* give back our minor */
3561da177e4SLinus Torvalds 	usb_deregister_dev(interface, &skel_class);
3571da177e4SLinus Torvalds 
358121e287cSAlan Stern 	/* prevent more I/O from starting */
359121e287cSAlan Stern 	mutex_lock(&dev->io_mutex);
360121e287cSAlan Stern 	dev->interface = NULL;
361121e287cSAlan Stern 	mutex_unlock(&dev->io_mutex);
362121e287cSAlan Stern 
3631da177e4SLinus Torvalds 	unlock_kernel();
3641da177e4SLinus Torvalds 
3651da177e4SLinus Torvalds 	/* decrement our usage count */
3661da177e4SLinus Torvalds 	kref_put(&dev->kref, skel_delete);
3671da177e4SLinus Torvalds 
3681da177e4SLinus Torvalds 	info("USB Skeleton #%d now disconnected", minor);
3691da177e4SLinus Torvalds }
3701da177e4SLinus Torvalds 
3711da177e4SLinus Torvalds static struct usb_driver skel_driver = {
3721da177e4SLinus Torvalds 	.name =		"skeleton",
3731da177e4SLinus Torvalds 	.probe =	skel_probe,
3741da177e4SLinus Torvalds 	.disconnect =	skel_disconnect,
3751da177e4SLinus Torvalds 	.id_table =	skel_table,
3761da177e4SLinus Torvalds };
3771da177e4SLinus Torvalds 
3781da177e4SLinus Torvalds static int __init usb_skel_init(void)
3791da177e4SLinus Torvalds {
3801da177e4SLinus Torvalds 	int result;
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds 	/* register this driver with the USB subsystem */
3831da177e4SLinus Torvalds 	result = usb_register(&skel_driver);
3841da177e4SLinus Torvalds 	if (result)
3851da177e4SLinus Torvalds 		err("usb_register failed. Error number %d", result);
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds 	return result;
3881da177e4SLinus Torvalds }
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds static void __exit usb_skel_exit(void)
3911da177e4SLinus Torvalds {
3921da177e4SLinus Torvalds 	/* deregister this driver with the USB subsystem */
3931da177e4SLinus Torvalds 	usb_deregister(&skel_driver);
3941da177e4SLinus Torvalds }
3951da177e4SLinus Torvalds 
3961da177e4SLinus Torvalds module_init (usb_skel_init);
3971da177e4SLinus Torvalds module_exit (usb_skel_exit);
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds MODULE_LICENSE("GPL");
400