xref: /linux/drivers/usb/usb-skeleton.c (revision 441b62c1edb986827154768d89bbac0ba779984f)
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 */
60758f7e16SOliver 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",
91*441b62c1SHarvey Harrison 		     __func__, 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 
105758f7e16SOliver Neukum 	/* lock the device to allow correctly handling errors
106758f7e16SOliver Neukum 	 * in resumption */
107758f7e16SOliver Neukum 	mutex_lock(&dev->io_mutex);
108758f7e16SOliver Neukum 
109758f7e16SOliver Neukum 	if (!dev->open_count++) {
1105b064708SOliver Neukum 		retval = usb_autopm_get_interface(interface);
1115b064708SOliver Neukum 			if (retval) {
112758f7e16SOliver Neukum 				dev->open_count--;
113758f7e16SOliver Neukum 				mutex_unlock(&dev->io_mutex);
1145b064708SOliver Neukum 				kref_put(&dev->kref, skel_delete);
1155b064708SOliver Neukum 				goto exit;
1165b064708SOliver Neukum 			}
117758f7e16SOliver Neukum 	} /* else { //uncomment this block if you want exclusive open
118758f7e16SOliver Neukum 		retval = -EBUSY;
119758f7e16SOliver Neukum 		dev->open_count--;
120758f7e16SOliver Neukum 		mutex_unlock(&dev->io_mutex);
121758f7e16SOliver Neukum 		kref_put(&dev->kref, skel_delete);
122758f7e16SOliver Neukum 		goto exit;
123758f7e16SOliver Neukum 	} */
124758f7e16SOliver 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;
128f7294055SMark Gross 	mutex_unlock(&dev->io_mutex);
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds exit:
1311da177e4SLinus Torvalds 	return retval;
1321da177e4SLinus Torvalds }
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds static int skel_release(struct inode *inode, struct file *file)
1351da177e4SLinus Torvalds {
1361da177e4SLinus Torvalds 	struct usb_skel *dev;
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds 	dev = (struct usb_skel *)file->private_data;
1391da177e4SLinus Torvalds 	if (dev == NULL)
1401da177e4SLinus Torvalds 		return -ENODEV;
1411da177e4SLinus Torvalds 
14201d883d4SAlan Stern 	/* allow the device to be autosuspended */
14301d883d4SAlan Stern 	mutex_lock(&dev->io_mutex);
144758f7e16SOliver Neukum 	if (!--dev->open_count && dev->interface)
14501d883d4SAlan Stern 		usb_autopm_put_interface(dev->interface);
14601d883d4SAlan Stern 	mutex_unlock(&dev->io_mutex);
14701d883d4SAlan Stern 
1481da177e4SLinus Torvalds 	/* decrement the count on our device */
1491da177e4SLinus Torvalds 	kref_put(&dev->kref, skel_delete);
1501da177e4SLinus Torvalds 	return 0;
1511da177e4SLinus Torvalds }
1521da177e4SLinus Torvalds 
153403dfb58SOliver Neukum static int skel_flush(struct file *file, fl_owner_t id)
154403dfb58SOliver Neukum {
155403dfb58SOliver Neukum 	struct usb_skel *dev;
156403dfb58SOliver Neukum 	int res;
157403dfb58SOliver Neukum 
158403dfb58SOliver Neukum 	dev = (struct usb_skel *)file->private_data;
159403dfb58SOliver Neukum 	if (dev == NULL)
160403dfb58SOliver Neukum 		return -ENODEV;
161403dfb58SOliver Neukum 
162403dfb58SOliver Neukum 	/* wait for io to stop */
163403dfb58SOliver Neukum 	mutex_lock(&dev->io_mutex);
164403dfb58SOliver Neukum 	skel_draw_down(dev);
165403dfb58SOliver Neukum 
166403dfb58SOliver Neukum 	/* read out errors, leave subsequent opens a clean slate */
167403dfb58SOliver Neukum 	spin_lock_irq(&dev->err_lock);
168403dfb58SOliver Neukum 	res = dev->errors ? (dev->errors == -EPIPE ? -EPIPE : -EIO) : 0;
169403dfb58SOliver Neukum 	dev->errors = 0;
170403dfb58SOliver Neukum 	spin_unlock_irq(&dev->err_lock);
171403dfb58SOliver Neukum 
172403dfb58SOliver Neukum 	mutex_unlock(&dev->io_mutex);
173403dfb58SOliver Neukum 
174403dfb58SOliver Neukum 	return res;
175403dfb58SOliver Neukum }
176403dfb58SOliver Neukum 
1771da177e4SLinus Torvalds static ssize_t skel_read(struct file *file, char *buffer, size_t count, loff_t *ppos)
1781da177e4SLinus Torvalds {
1791da177e4SLinus Torvalds 	struct usb_skel *dev;
180c0704541SLuiz Fernando N. Capitulino 	int retval;
1811da177e4SLinus Torvalds 	int bytes_read;
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds 	dev = (struct usb_skel *)file->private_data;
1841da177e4SLinus Torvalds 
185121e287cSAlan Stern 	mutex_lock(&dev->io_mutex);
186121e287cSAlan Stern 	if (!dev->interface) {		/* disconnect() was called */
187121e287cSAlan Stern 		retval = -ENODEV;
188121e287cSAlan Stern 		goto exit;
189121e287cSAlan Stern 	}
190121e287cSAlan Stern 
1911da177e4SLinus Torvalds 	/* do a blocking bulk read to get data from the device */
1921da177e4SLinus Torvalds 	retval = usb_bulk_msg(dev->udev,
1931da177e4SLinus Torvalds 			      usb_rcvbulkpipe(dev->udev, dev->bulk_in_endpointAddr),
1941da177e4SLinus Torvalds 			      dev->bulk_in_buffer,
1951da177e4SLinus Torvalds 			      min(dev->bulk_in_size, count),
1961da177e4SLinus Torvalds 			      &bytes_read, 10000);
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds 	/* if the read was successful, copy the data to userspace */
1991da177e4SLinus Torvalds 	if (!retval) {
2001da177e4SLinus Torvalds 		if (copy_to_user(buffer, dev->bulk_in_buffer, bytes_read))
2011da177e4SLinus Torvalds 			retval = -EFAULT;
2021da177e4SLinus Torvalds 		else
2031da177e4SLinus Torvalds 			retval = bytes_read;
2041da177e4SLinus Torvalds 	}
2051da177e4SLinus Torvalds 
206121e287cSAlan Stern exit:
207121e287cSAlan Stern 	mutex_unlock(&dev->io_mutex);
2081da177e4SLinus Torvalds 	return retval;
2091da177e4SLinus Torvalds }
2101da177e4SLinus Torvalds 
2117d12e780SDavid Howells static void skel_write_bulk_callback(struct urb *urb)
2121da177e4SLinus Torvalds {
2131da177e4SLinus Torvalds 	struct usb_skel *dev;
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds 	dev = (struct usb_skel *)urb->context;
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds 	/* sync/async unlink faults aren't errors */
218403dfb58SOliver Neukum 	if (urb->status) {
219403dfb58SOliver Neukum 		if(!(urb->status == -ENOENT ||
2201da177e4SLinus Torvalds 		    urb->status == -ECONNRESET ||
221403dfb58SOliver Neukum 		    urb->status == -ESHUTDOWN))
222c0704541SLuiz Fernando N. Capitulino 			err("%s - nonzero write bulk status received: %d",
223*441b62c1SHarvey Harrison 			    __func__, urb->status);
224403dfb58SOliver Neukum 
225403dfb58SOliver Neukum 		spin_lock(&dev->err_lock);
226403dfb58SOliver Neukum 		dev->errors = urb->status;
227403dfb58SOliver Neukum 		spin_unlock(&dev->err_lock);
2281da177e4SLinus Torvalds 	}
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds 	/* free up our allocated buffer */
2311da177e4SLinus Torvalds 	usb_buffer_free(urb->dev, urb->transfer_buffer_length,
2321da177e4SLinus Torvalds 			urb->transfer_buffer, urb->transfer_dma);
233ff906518SOliver Neukum 	up(&dev->limit_sem);
2341da177e4SLinus Torvalds }
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds static ssize_t skel_write(struct file *file, const char *user_buffer, size_t count, loff_t *ppos)
2371da177e4SLinus Torvalds {
2381da177e4SLinus Torvalds 	struct usb_skel *dev;
2391da177e4SLinus Torvalds 	int retval = 0;
2401da177e4SLinus Torvalds 	struct urb *urb = NULL;
2411da177e4SLinus Torvalds 	char *buf = NULL;
242c8dd7709SSam Bishop 	size_t writesize = min(count, (size_t)MAX_TRANSFER);
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds 	dev = (struct usb_skel *)file->private_data;
2451da177e4SLinus Torvalds 
2461da177e4SLinus Torvalds 	/* verify that we actually have some data to write */
2471da177e4SLinus Torvalds 	if (count == 0)
2481da177e4SLinus Torvalds 		goto exit;
2491da177e4SLinus Torvalds 
250ff906518SOliver Neukum 	/* limit the number of URBs in flight to stop a user from using up all RAM */
251c8dd7709SSam Bishop 	if (down_interruptible(&dev->limit_sem)) {
252c8dd7709SSam Bishop 		retval = -ERESTARTSYS;
253c8dd7709SSam Bishop 		goto exit;
254c8dd7709SSam Bishop 	}
255ff906518SOliver Neukum 
256403dfb58SOliver Neukum 	spin_lock_irq(&dev->err_lock);
257403dfb58SOliver Neukum 	if ((retval = dev->errors) < 0) {
258403dfb58SOliver Neukum 		/* any error is reported once */
259403dfb58SOliver Neukum 		dev->errors = 0;
260403dfb58SOliver Neukum 		/* to preserve notifications about reset */
261403dfb58SOliver Neukum 		retval = (retval == -EPIPE) ? retval : -EIO;
262403dfb58SOliver Neukum 	}
263403dfb58SOliver Neukum 	spin_unlock_irq(&dev->err_lock);
264403dfb58SOliver Neukum 	if (retval < 0)
265403dfb58SOliver Neukum 		goto error;
266403dfb58SOliver Neukum 
2671da177e4SLinus Torvalds 	/* create a urb, and a buffer for it, and copy the data to the urb */
2681da177e4SLinus Torvalds 	urb = usb_alloc_urb(0, GFP_KERNEL);
2691da177e4SLinus Torvalds 	if (!urb) {
2701da177e4SLinus Torvalds 		retval = -ENOMEM;
2711da177e4SLinus Torvalds 		goto error;
2721da177e4SLinus Torvalds 	}
2731da177e4SLinus Torvalds 
274ff906518SOliver Neukum 	buf = usb_buffer_alloc(dev->udev, writesize, GFP_KERNEL, &urb->transfer_dma);
2751da177e4SLinus Torvalds 	if (!buf) {
2761da177e4SLinus Torvalds 		retval = -ENOMEM;
2771da177e4SLinus Torvalds 		goto error;
2781da177e4SLinus Torvalds 	}
2791da177e4SLinus Torvalds 
280ff906518SOliver Neukum 	if (copy_from_user(buf, user_buffer, writesize)) {
2811da177e4SLinus Torvalds 		retval = -EFAULT;
2821da177e4SLinus Torvalds 		goto error;
2831da177e4SLinus Torvalds 	}
2841da177e4SLinus Torvalds 
285ba35e02bSOliver Neukum 	/* this lock makes sure we don't submit URBs to gone devices */
286ba35e02bSOliver Neukum 	mutex_lock(&dev->io_mutex);
287ba35e02bSOliver Neukum 	if (!dev->interface) {		/* disconnect() was called */
288ba35e02bSOliver Neukum 		mutex_unlock(&dev->io_mutex);
289ba35e02bSOliver Neukum 		retval = -ENODEV;
290ba35e02bSOliver Neukum 		goto error;
291ba35e02bSOliver Neukum 	}
292ba35e02bSOliver Neukum 
2931da177e4SLinus Torvalds 	/* initialize the urb properly */
2941da177e4SLinus Torvalds 	usb_fill_bulk_urb(urb, dev->udev,
2951da177e4SLinus Torvalds 			  usb_sndbulkpipe(dev->udev, dev->bulk_out_endpointAddr),
296ff906518SOliver Neukum 			  buf, writesize, skel_write_bulk_callback, dev);
2971da177e4SLinus Torvalds 	urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
298403dfb58SOliver Neukum 	usb_anchor_urb(urb, &dev->submitted);
2991da177e4SLinus Torvalds 
3001da177e4SLinus Torvalds 	/* send the data out the bulk port */
3011da177e4SLinus Torvalds 	retval = usb_submit_urb(urb, GFP_KERNEL);
302ba35e02bSOliver Neukum 	mutex_unlock(&dev->io_mutex);
3031da177e4SLinus Torvalds 	if (retval) {
304*441b62c1SHarvey Harrison 		err("%s - failed submitting write urb, error %d", __func__, retval);
305403dfb58SOliver Neukum 		goto error_unanchor;
3061da177e4SLinus Torvalds 	}
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds 	/* release our reference to this urb, the USB core will eventually free it entirely */
3091da177e4SLinus Torvalds 	usb_free_urb(urb);
3101da177e4SLinus Torvalds 
311ba35e02bSOliver Neukum 
312ff906518SOliver Neukum 	return writesize;
3131da177e4SLinus Torvalds 
314403dfb58SOliver Neukum error_unanchor:
315403dfb58SOliver Neukum 	usb_unanchor_urb(urb);
3161da177e4SLinus Torvalds error:
317121e287cSAlan Stern 	if (urb) {
318ff906518SOliver Neukum 		usb_buffer_free(dev->udev, writesize, buf, urb->transfer_dma);
3191da177e4SLinus Torvalds 		usb_free_urb(urb);
320121e287cSAlan Stern 	}
321ff906518SOliver Neukum 	up(&dev->limit_sem);
322121e287cSAlan Stern 
323121e287cSAlan Stern exit:
3241da177e4SLinus Torvalds 	return retval;
3251da177e4SLinus Torvalds }
3261da177e4SLinus Torvalds 
327066202ddSLuiz Fernando N. Capitulino static const struct file_operations skel_fops = {
3281da177e4SLinus Torvalds 	.owner =	THIS_MODULE,
3291da177e4SLinus Torvalds 	.read =		skel_read,
3301da177e4SLinus Torvalds 	.write =	skel_write,
3311da177e4SLinus Torvalds 	.open =		skel_open,
3321da177e4SLinus Torvalds 	.release =	skel_release,
333403dfb58SOliver Neukum 	.flush =	skel_flush,
3341da177e4SLinus Torvalds };
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds /*
3371da177e4SLinus Torvalds  * usb class driver info in order to get a minor number from the usb core,
338595b14cbSGreg Kroah-Hartman  * and to have the device registered with the driver core
3391da177e4SLinus Torvalds  */
3401da177e4SLinus Torvalds static struct usb_class_driver skel_class = {
341d6e5bcf4SGreg Kroah-Hartman 	.name =		"skel%d",
3421da177e4SLinus Torvalds 	.fops =		&skel_fops,
3431da177e4SLinus Torvalds 	.minor_base =	USB_SKEL_MINOR_BASE,
3441da177e4SLinus Torvalds };
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds static int skel_probe(struct usb_interface *interface, const struct usb_device_id *id)
3471da177e4SLinus Torvalds {
348c0704541SLuiz Fernando N. Capitulino 	struct usb_skel *dev;
3491da177e4SLinus Torvalds 	struct usb_host_interface *iface_desc;
3501da177e4SLinus Torvalds 	struct usb_endpoint_descriptor *endpoint;
3511da177e4SLinus Torvalds 	size_t buffer_size;
3521da177e4SLinus Torvalds 	int i;
3531da177e4SLinus Torvalds 	int retval = -ENOMEM;
3541da177e4SLinus Torvalds 
3551da177e4SLinus Torvalds 	/* allocate memory for our device state and initialize it */
356ff906518SOliver Neukum 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
357c0704541SLuiz Fernando N. Capitulino 	if (!dev) {
3581da177e4SLinus Torvalds 		err("Out of memory");
3591da177e4SLinus Torvalds 		goto error;
3601da177e4SLinus Torvalds 	}
3611da177e4SLinus Torvalds 	kref_init(&dev->kref);
362ff906518SOliver Neukum 	sema_init(&dev->limit_sem, WRITES_IN_FLIGHT);
363121e287cSAlan Stern 	mutex_init(&dev->io_mutex);
364403dfb58SOliver Neukum 	spin_lock_init(&dev->err_lock);
365403dfb58SOliver Neukum 	init_usb_anchor(&dev->submitted);
3661da177e4SLinus Torvalds 
3671da177e4SLinus Torvalds 	dev->udev = usb_get_dev(interface_to_usbdev(interface));
3681da177e4SLinus Torvalds 	dev->interface = interface;
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds 	/* set up the endpoint information */
3711da177e4SLinus Torvalds 	/* use only the first bulk-in and bulk-out endpoints */
3721da177e4SLinus Torvalds 	iface_desc = interface->cur_altsetting;
3731da177e4SLinus Torvalds 	for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
3741da177e4SLinus Torvalds 		endpoint = &iface_desc->endpoint[i].desc;
3751da177e4SLinus Torvalds 
3761da177e4SLinus Torvalds 		if (!dev->bulk_in_endpointAddr &&
377c0704541SLuiz Fernando N. Capitulino 		    usb_endpoint_is_bulk_in(endpoint)) {
3781da177e4SLinus Torvalds 			/* we found a bulk in endpoint */
3791da177e4SLinus Torvalds 			buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
3801da177e4SLinus Torvalds 			dev->bulk_in_size = buffer_size;
3811da177e4SLinus Torvalds 			dev->bulk_in_endpointAddr = endpoint->bEndpointAddress;
3821da177e4SLinus Torvalds 			dev->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
3831da177e4SLinus Torvalds 			if (!dev->bulk_in_buffer) {
3841da177e4SLinus Torvalds 				err("Could not allocate bulk_in_buffer");
3851da177e4SLinus Torvalds 				goto error;
3861da177e4SLinus Torvalds 			}
3871da177e4SLinus Torvalds 		}
3881da177e4SLinus Torvalds 
3891da177e4SLinus Torvalds 		if (!dev->bulk_out_endpointAddr &&
390c0704541SLuiz Fernando N. Capitulino 		    usb_endpoint_is_bulk_out(endpoint)) {
3911da177e4SLinus Torvalds 			/* we found a bulk out endpoint */
3921da177e4SLinus Torvalds 			dev->bulk_out_endpointAddr = endpoint->bEndpointAddress;
3931da177e4SLinus Torvalds 		}
3941da177e4SLinus Torvalds 	}
3951da177e4SLinus Torvalds 	if (!(dev->bulk_in_endpointAddr && dev->bulk_out_endpointAddr)) {
3961da177e4SLinus Torvalds 		err("Could not find both bulk-in and bulk-out endpoints");
3971da177e4SLinus Torvalds 		goto error;
3981da177e4SLinus Torvalds 	}
3991da177e4SLinus Torvalds 
4001da177e4SLinus Torvalds 	/* save our data pointer in this interface device */
4011da177e4SLinus Torvalds 	usb_set_intfdata(interface, dev);
4021da177e4SLinus Torvalds 
4031da177e4SLinus Torvalds 	/* we can register the device now, as it is ready */
4041da177e4SLinus Torvalds 	retval = usb_register_dev(interface, &skel_class);
4051da177e4SLinus Torvalds 	if (retval) {
4061da177e4SLinus Torvalds 		/* something prevented us from registering this driver */
4071da177e4SLinus Torvalds 		err("Not able to get a minor for this device.");
4081da177e4SLinus Torvalds 		usb_set_intfdata(interface, NULL);
4091da177e4SLinus Torvalds 		goto error;
4101da177e4SLinus Torvalds 	}
4111da177e4SLinus Torvalds 
4121da177e4SLinus Torvalds 	/* let the user know what node this device is now attached to */
4131da177e4SLinus Torvalds 	info("USB Skeleton device now attached to USBSkel-%d", interface->minor);
4141da177e4SLinus Torvalds 	return 0;
4151da177e4SLinus Torvalds 
4161da177e4SLinus Torvalds error:
4171da177e4SLinus Torvalds 	if (dev)
418ba35e02bSOliver Neukum 		/* this frees allocated memory */
4191da177e4SLinus Torvalds 		kref_put(&dev->kref, skel_delete);
4201da177e4SLinus Torvalds 	return retval;
4211da177e4SLinus Torvalds }
4221da177e4SLinus Torvalds 
4231da177e4SLinus Torvalds static void skel_disconnect(struct usb_interface *interface)
4241da177e4SLinus Torvalds {
4251da177e4SLinus Torvalds 	struct usb_skel *dev;
4261da177e4SLinus Torvalds 	int minor = interface->minor;
4271da177e4SLinus Torvalds 
4281da177e4SLinus Torvalds 	dev = usb_get_intfdata(interface);
4291da177e4SLinus Torvalds 	usb_set_intfdata(interface, NULL);
4301da177e4SLinus Torvalds 
4311da177e4SLinus Torvalds 	/* give back our minor */
4321da177e4SLinus Torvalds 	usb_deregister_dev(interface, &skel_class);
4331da177e4SLinus Torvalds 
434121e287cSAlan Stern 	/* prevent more I/O from starting */
435121e287cSAlan Stern 	mutex_lock(&dev->io_mutex);
436121e287cSAlan Stern 	dev->interface = NULL;
437121e287cSAlan Stern 	mutex_unlock(&dev->io_mutex);
438121e287cSAlan Stern 
439e73c7247SOliver Neukum 	usb_kill_anchored_urbs(&dev->submitted);
440e73c7247SOliver Neukum 
4411da177e4SLinus Torvalds 	/* decrement our usage count */
4421da177e4SLinus Torvalds 	kref_put(&dev->kref, skel_delete);
4431da177e4SLinus Torvalds 
4441da177e4SLinus Torvalds 	info("USB Skeleton #%d now disconnected", minor);
4451da177e4SLinus Torvalds }
4461da177e4SLinus Torvalds 
447403dfb58SOliver Neukum static void skel_draw_down(struct usb_skel *dev)
448403dfb58SOliver Neukum {
449403dfb58SOliver Neukum 	int time;
450403dfb58SOliver Neukum 
451403dfb58SOliver Neukum 	time = usb_wait_anchor_empty_timeout(&dev->submitted, 1000);
452403dfb58SOliver Neukum 	if (!time)
453403dfb58SOliver Neukum 		usb_kill_anchored_urbs(&dev->submitted);
454403dfb58SOliver Neukum }
455403dfb58SOliver Neukum 
456758f7e16SOliver Neukum static int skel_suspend(struct usb_interface *intf, pm_message_t message)
457758f7e16SOliver Neukum {
458758f7e16SOliver Neukum 	struct usb_skel *dev = usb_get_intfdata(intf);
459758f7e16SOliver Neukum 
460758f7e16SOliver Neukum 	if (!dev)
461758f7e16SOliver Neukum 		return 0;
462758f7e16SOliver Neukum 	skel_draw_down(dev);
463758f7e16SOliver Neukum 	return 0;
464758f7e16SOliver Neukum }
465758f7e16SOliver Neukum 
466758f7e16SOliver Neukum static int skel_resume (struct usb_interface *intf)
467758f7e16SOliver Neukum {
468758f7e16SOliver Neukum 	return 0;
469758f7e16SOliver Neukum }
470758f7e16SOliver Neukum 
47187d093e2SOliver Neukum static int skel_pre_reset(struct usb_interface *intf)
47287d093e2SOliver Neukum {
47387d093e2SOliver Neukum 	struct usb_skel *dev = usb_get_intfdata(intf);
47487d093e2SOliver Neukum 
47587d093e2SOliver Neukum 	mutex_lock(&dev->io_mutex);
47687d093e2SOliver Neukum 	skel_draw_down(dev);
47787d093e2SOliver Neukum 
47887d093e2SOliver Neukum 	return 0;
47987d093e2SOliver Neukum }
48087d093e2SOliver Neukum 
48187d093e2SOliver Neukum static int skel_post_reset(struct usb_interface *intf)
48287d093e2SOliver Neukum {
48387d093e2SOliver Neukum 	struct usb_skel *dev = usb_get_intfdata(intf);
48487d093e2SOliver Neukum 
48587d093e2SOliver Neukum 	/* we are sure no URBs are active - no locking needed */
48687d093e2SOliver Neukum 	dev->errors = -EPIPE;
48787d093e2SOliver Neukum 	mutex_unlock(&dev->io_mutex);
48887d093e2SOliver Neukum 
48987d093e2SOliver Neukum 	return 0;
49087d093e2SOliver Neukum }
49187d093e2SOliver Neukum 
4921da177e4SLinus Torvalds static struct usb_driver skel_driver = {
4931da177e4SLinus Torvalds 	.name =		"skeleton",
4941da177e4SLinus Torvalds 	.probe =	skel_probe,
4951da177e4SLinus Torvalds 	.disconnect =	skel_disconnect,
496758f7e16SOliver Neukum 	.suspend =	skel_suspend,
497758f7e16SOliver Neukum 	.resume =	skel_resume,
49887d093e2SOliver Neukum 	.pre_reset =	skel_pre_reset,
49987d093e2SOliver Neukum 	.post_reset =	skel_post_reset,
5001da177e4SLinus Torvalds 	.id_table =	skel_table,
501ba35e02bSOliver Neukum 	.supports_autosuspend = 1,
5021da177e4SLinus Torvalds };
5031da177e4SLinus Torvalds 
5041da177e4SLinus Torvalds static int __init usb_skel_init(void)
5051da177e4SLinus Torvalds {
5061da177e4SLinus Torvalds 	int result;
5071da177e4SLinus Torvalds 
5081da177e4SLinus Torvalds 	/* register this driver with the USB subsystem */
5091da177e4SLinus Torvalds 	result = usb_register(&skel_driver);
5101da177e4SLinus Torvalds 	if (result)
5111da177e4SLinus Torvalds 		err("usb_register failed. Error number %d", result);
5121da177e4SLinus Torvalds 
5131da177e4SLinus Torvalds 	return result;
5141da177e4SLinus Torvalds }
5151da177e4SLinus Torvalds 
5161da177e4SLinus Torvalds static void __exit usb_skel_exit(void)
5171da177e4SLinus Torvalds {
5181da177e4SLinus Torvalds 	/* deregister this driver with the USB subsystem */
5191da177e4SLinus Torvalds 	usb_deregister(&skel_driver);
5201da177e4SLinus Torvalds }
5211da177e4SLinus Torvalds 
5221da177e4SLinus Torvalds module_init(usb_skel_init);
5231da177e4SLinus Torvalds module_exit(usb_skel_exit);
5241da177e4SLinus Torvalds 
5251da177e4SLinus Torvalds MODULE_LICENSE("GPL");
526