xref: /linux/drivers/usb/core/file.c (revision 2c9ede55ecec58099b72e4bb8eab719f32f72c31)
11da177e4SLinus Torvalds /*
2f30c2269SUwe Zeisberger  * drivers/usb/core/file.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * (C) Copyright Linus Torvalds 1999
51da177e4SLinus Torvalds  * (C) Copyright Johannes Erdfelt 1999-2001
61da177e4SLinus Torvalds  * (C) Copyright Andreas Gal 1999
71da177e4SLinus Torvalds  * (C) Copyright Gregory P. Smith 1999
81da177e4SLinus Torvalds  * (C) Copyright Deti Fliegl 1999 (new USB architecture)
91da177e4SLinus Torvalds  * (C) Copyright Randy Dunlap 2000
101da177e4SLinus Torvalds  * (C) Copyright David Brownell 2000-2001 (kernel hotplug, usb_device_id,
111da177e4SLinus Torvalds  	more docs, etc)
121da177e4SLinus Torvalds  * (C) Copyright Yggdrasil Computing, Inc. 2000
131da177e4SLinus Torvalds  *     (usb_device_id matching changes by Adam J. Richter)
141da177e4SLinus Torvalds  * (C) Copyright Greg Kroah-Hartman 2002-2003
151da177e4SLinus Torvalds  *
161da177e4SLinus Torvalds  */
171da177e4SLinus Torvalds 
181da177e4SLinus Torvalds #include <linux/module.h>
191da177e4SLinus Torvalds #include <linux/errno.h>
20d4ead16fSAlan Stern #include <linux/rwsem.h>
215a0e3ad6STejun Heo #include <linux/slab.h>
221da177e4SLinus Torvalds #include <linux/usb.h>
231da177e4SLinus Torvalds 
246d5e8254SGreg KH #include "usb.h"
256d5e8254SGreg KH 
261da177e4SLinus Torvalds #define MAX_USB_MINORS	256
2799ac48f5SArjan van de Ven static const struct file_operations *usb_minors[MAX_USB_MINORS];
28d4ead16fSAlan Stern static DECLARE_RWSEM(minor_rwsem);
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds static int usb_open(struct inode * inode, struct file * file)
311da177e4SLinus Torvalds {
321da177e4SLinus Torvalds 	int minor = iminor(inode);
3399ac48f5SArjan van de Ven 	const struct file_operations *c;
341da177e4SLinus Torvalds 	int err = -ENODEV;
3599ac48f5SArjan van de Ven 	const struct file_operations *old_fops, *new_fops = NULL;
361da177e4SLinus Torvalds 
37d4ead16fSAlan Stern 	down_read(&minor_rwsem);
381da177e4SLinus Torvalds 	c = usb_minors[minor];
391da177e4SLinus Torvalds 
40d4ead16fSAlan Stern 	if (!c || !(new_fops = fops_get(c)))
41d4ead16fSAlan Stern 		goto done;
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds 	old_fops = file->f_op;
441da177e4SLinus Torvalds 	file->f_op = new_fops;
451da177e4SLinus Torvalds 	/* Curiouser and curiouser... NULL ->open() as "no device" ? */
461da177e4SLinus Torvalds 	if (file->f_op->open)
471da177e4SLinus Torvalds 		err = file->f_op->open(inode,file);
481da177e4SLinus Torvalds 	if (err) {
491da177e4SLinus Torvalds 		fops_put(file->f_op);
501da177e4SLinus Torvalds 		file->f_op = fops_get(old_fops);
511da177e4SLinus Torvalds 	}
521da177e4SLinus Torvalds 	fops_put(old_fops);
53d4ead16fSAlan Stern  done:
54d4ead16fSAlan Stern 	up_read(&minor_rwsem);
551da177e4SLinus Torvalds 	return err;
561da177e4SLinus Torvalds }
571da177e4SLinus Torvalds 
58066202ddSLuiz Fernando N. Capitulino static const struct file_operations usb_fops = {
591da177e4SLinus Torvalds 	.owner =	THIS_MODULE,
601da177e4SLinus Torvalds 	.open =		usb_open,
616038f373SArnd Bergmann 	.llseek =	noop_llseek,
621da177e4SLinus Torvalds };
631da177e4SLinus Torvalds 
6443104f1dSGreg Kroah-Hartman static struct usb_class {
6543104f1dSGreg Kroah-Hartman 	struct kref kref;
6643104f1dSGreg Kroah-Hartman 	struct class *class;
6743104f1dSGreg Kroah-Hartman } *usb_class;
6843104f1dSGreg Kroah-Hartman 
69*2c9ede55SAl Viro static char *usb_devnode(struct device *dev, umode_t *mode)
70f7a386c5SKay Sievers {
71f7a386c5SKay Sievers 	struct usb_class_driver *drv;
72f7a386c5SKay Sievers 
73f7a386c5SKay Sievers 	drv = dev_get_drvdata(dev);
74e454cea2SKay Sievers 	if (!drv || !drv->devnode)
75f7a386c5SKay Sievers 		return NULL;
76e454cea2SKay Sievers 	return drv->devnode(dev, mode);
77f7a386c5SKay Sievers }
78f7a386c5SKay Sievers 
7943104f1dSGreg Kroah-Hartman static int init_usb_class(void)
8043104f1dSGreg Kroah-Hartman {
8143104f1dSGreg Kroah-Hartman 	int result = 0;
8243104f1dSGreg Kroah-Hartman 
8343104f1dSGreg Kroah-Hartman 	if (usb_class != NULL) {
8443104f1dSGreg Kroah-Hartman 		kref_get(&usb_class->kref);
8543104f1dSGreg Kroah-Hartman 		goto exit;
8643104f1dSGreg Kroah-Hartman 	}
8743104f1dSGreg Kroah-Hartman 
8843104f1dSGreg Kroah-Hartman 	usb_class = kmalloc(sizeof(*usb_class), GFP_KERNEL);
8943104f1dSGreg Kroah-Hartman 	if (!usb_class) {
9043104f1dSGreg Kroah-Hartman 		result = -ENOMEM;
9143104f1dSGreg Kroah-Hartman 		goto exit;
9243104f1dSGreg Kroah-Hartman 	}
9343104f1dSGreg Kroah-Hartman 
9443104f1dSGreg Kroah-Hartman 	kref_init(&usb_class->kref);
9543104f1dSGreg Kroah-Hartman 	usb_class->class = class_create(THIS_MODULE, "usb");
9643104f1dSGreg Kroah-Hartman 	if (IS_ERR(usb_class->class)) {
9743104f1dSGreg Kroah-Hartman 		result = IS_ERR(usb_class->class);
9869a85942SGreg Kroah-Hartman 		printk(KERN_ERR "class_create failed for usb devices\n");
9943104f1dSGreg Kroah-Hartman 		kfree(usb_class);
10043104f1dSGreg Kroah-Hartman 		usb_class = NULL;
101ed7487c2SDan Carpenter 		goto exit;
10243104f1dSGreg Kroah-Hartman 	}
103e454cea2SKay Sievers 	usb_class->class->devnode = usb_devnode;
10443104f1dSGreg Kroah-Hartman 
10543104f1dSGreg Kroah-Hartman exit:
10643104f1dSGreg Kroah-Hartman 	return result;
10743104f1dSGreg Kroah-Hartman }
10843104f1dSGreg Kroah-Hartman 
10943104f1dSGreg Kroah-Hartman static void release_usb_class(struct kref *kref)
11043104f1dSGreg Kroah-Hartman {
11143104f1dSGreg Kroah-Hartman 	/* Ok, we cheat as we know we only have one usb_class */
11243104f1dSGreg Kroah-Hartman 	class_destroy(usb_class->class);
11343104f1dSGreg Kroah-Hartman 	kfree(usb_class);
11443104f1dSGreg Kroah-Hartman 	usb_class = NULL;
11543104f1dSGreg Kroah-Hartman }
11643104f1dSGreg Kroah-Hartman 
11743104f1dSGreg Kroah-Hartman static void destroy_usb_class(void)
11843104f1dSGreg Kroah-Hartman {
11943104f1dSGreg Kroah-Hartman 	if (usb_class)
12043104f1dSGreg Kroah-Hartman 		kref_put(&usb_class->kref, release_usb_class);
12143104f1dSGreg Kroah-Hartman }
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds int usb_major_init(void)
1241da177e4SLinus Torvalds {
1251da177e4SLinus Torvalds 	int error;
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds 	error = register_chrdev(USB_MAJOR, "usb", &usb_fops);
12843104f1dSGreg Kroah-Hartman 	if (error)
12969a85942SGreg Kroah-Hartman 		printk(KERN_ERR "Unable to get major %d for usb devices\n",
13069a85942SGreg Kroah-Hartman 		       USB_MAJOR);
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds 	return error;
1331da177e4SLinus Torvalds }
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds void usb_major_cleanup(void)
1361da177e4SLinus Torvalds {
1371da177e4SLinus Torvalds 	unregister_chrdev(USB_MAJOR, "usb");
1381da177e4SLinus Torvalds }
1391da177e4SLinus Torvalds 
1401da177e4SLinus Torvalds /**
1411da177e4SLinus Torvalds  * usb_register_dev - register a USB device, and ask for a minor number
1421da177e4SLinus Torvalds  * @intf: pointer to the usb_interface that is being registered
1431da177e4SLinus Torvalds  * @class_driver: pointer to the usb_class_driver for this device
1441da177e4SLinus Torvalds  *
1451da177e4SLinus Torvalds  * This should be called by all USB drivers that use the USB major number.
1461da177e4SLinus Torvalds  * If CONFIG_USB_DYNAMIC_MINORS is enabled, the minor number will be
1471da177e4SLinus Torvalds  * dynamically allocated out of the list of available ones.  If it is not
1481da177e4SLinus Torvalds  * enabled, the minor number will be based on the next available free minor,
1491da177e4SLinus Torvalds  * starting at the class_driver->minor_base.
1501da177e4SLinus Torvalds  *
151d6e5bcf4SGreg Kroah-Hartman  * This function also creates a usb class device in the sysfs tree.
1521da177e4SLinus Torvalds  *
1531da177e4SLinus Torvalds  * usb_deregister_dev() must be called when the driver is done with
1541da177e4SLinus Torvalds  * the minor numbers given out by this function.
1551da177e4SLinus Torvalds  *
1561da177e4SLinus Torvalds  * Returns -EINVAL if something bad happens with trying to register a
1571da177e4SLinus Torvalds  * device, and 0 on success.
1581da177e4SLinus Torvalds  */
1591da177e4SLinus Torvalds int usb_register_dev(struct usb_interface *intf,
1601da177e4SLinus Torvalds 		     struct usb_class_driver *class_driver)
1611da177e4SLinus Torvalds {
1620026e005SAlan Stern 	int retval;
1631da177e4SLinus Torvalds 	int minor_base = class_driver->minor_base;
1640026e005SAlan Stern 	int minor;
1657071a3ceSKay Sievers 	char name[20];
1661da177e4SLinus Torvalds 	char *temp;
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds #ifdef CONFIG_USB_DYNAMIC_MINORS
1691da177e4SLinus Torvalds 	/*
1701da177e4SLinus Torvalds 	 * We don't care what the device tries to start at, we want to start
1711da177e4SLinus Torvalds 	 * at zero to pack the devices into the smallest available space with
1721da177e4SLinus Torvalds 	 * no holes in the minor range.
1731da177e4SLinus Torvalds 	 */
1741da177e4SLinus Torvalds 	minor_base = 0;
1751da177e4SLinus Torvalds #endif
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds 	if (class_driver->fops == NULL)
1780026e005SAlan Stern 		return -EINVAL;
1790026e005SAlan Stern 	if (intf->minor >= 0)
1800026e005SAlan Stern 		return -EADDRINUSE;
1810026e005SAlan Stern 
1820026e005SAlan Stern 	retval = init_usb_class();
1830026e005SAlan Stern 	if (retval)
1840026e005SAlan Stern 		return retval;
1850026e005SAlan Stern 
1860026e005SAlan Stern 	dev_dbg(&intf->dev, "looking for a minor, starting at %d", minor_base);
1871da177e4SLinus Torvalds 
188d4ead16fSAlan Stern 	down_write(&minor_rwsem);
1891da177e4SLinus Torvalds 	for (minor = minor_base; minor < MAX_USB_MINORS; ++minor) {
1901da177e4SLinus Torvalds 		if (usb_minors[minor])
1911da177e4SLinus Torvalds 			continue;
1921da177e4SLinus Torvalds 
1931da177e4SLinus Torvalds 		usb_minors[minor] = class_driver->fops;
1940026e005SAlan Stern 		intf->minor = minor;
1951da177e4SLinus Torvalds 		break;
1961da177e4SLinus Torvalds 	}
197d4ead16fSAlan Stern 	up_write(&minor_rwsem);
1980026e005SAlan Stern 	if (intf->minor < 0)
1990026e005SAlan Stern 		return -EXFULL;
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds 	/* create a usb class device for this usb interface */
2027071a3ceSKay Sievers 	snprintf(name, sizeof(name), class_driver->name, minor - minor_base);
2031da177e4SLinus Torvalds 	temp = strrchr(name, '/');
2047071a3ceSKay Sievers 	if (temp && (temp[1] != '\0'))
2051da177e4SLinus Torvalds 		++temp;
2061da177e4SLinus Torvalds 	else
2071da177e4SLinus Torvalds 		temp = name;
208b0b090e5SGreg Kroah-Hartman 	intf->usb_dev = device_create(usb_class->class, &intf->dev,
209f7a386c5SKay Sievers 				      MKDEV(USB_MAJOR, minor), class_driver,
210bc00bc92SGreg Kroah-Hartman 				      "%s", temp);
2110873c764SGreg Kroah-Hartman 	if (IS_ERR(intf->usb_dev)) {
212d4ead16fSAlan Stern 		down_write(&minor_rwsem);
2130026e005SAlan Stern 		usb_minors[minor] = NULL;
2140026e005SAlan Stern 		intf->minor = -1;
215d4ead16fSAlan Stern 		up_write(&minor_rwsem);
2160873c764SGreg Kroah-Hartman 		retval = PTR_ERR(intf->usb_dev);
2171da177e4SLinus Torvalds 	}
2181da177e4SLinus Torvalds 	return retval;
2191da177e4SLinus Torvalds }
220782e70c6SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(usb_register_dev);
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds /**
2231da177e4SLinus Torvalds  * usb_deregister_dev - deregister a USB device's dynamic minor.
2241da177e4SLinus Torvalds  * @intf: pointer to the usb_interface that is being deregistered
2251da177e4SLinus Torvalds  * @class_driver: pointer to the usb_class_driver for this device
2261da177e4SLinus Torvalds  *
2271da177e4SLinus Torvalds  * Used in conjunction with usb_register_dev().  This function is called
2281da177e4SLinus Torvalds  * when the USB driver is finished with the minor numbers gotten from a
2291da177e4SLinus Torvalds  * call to usb_register_dev() (usually when the device is disconnected
2301da177e4SLinus Torvalds  * from the system.)
2311da177e4SLinus Torvalds  *
232d6e5bcf4SGreg Kroah-Hartman  * This function also removes the usb class device from the sysfs tree.
2331da177e4SLinus Torvalds  *
2341da177e4SLinus Torvalds  * This should be called by all drivers that use the USB major number.
2351da177e4SLinus Torvalds  */
2361da177e4SLinus Torvalds void usb_deregister_dev(struct usb_interface *intf,
2371da177e4SLinus Torvalds 			struct usb_class_driver *class_driver)
2381da177e4SLinus Torvalds {
2391da177e4SLinus Torvalds 	if (intf->minor == -1)
2401da177e4SLinus Torvalds 		return;
2411da177e4SLinus Torvalds 
2421da177e4SLinus Torvalds 	dbg ("removing %d minor", intf->minor);
2431da177e4SLinus Torvalds 
244d4ead16fSAlan Stern 	down_write(&minor_rwsem);
2451da177e4SLinus Torvalds 	usb_minors[intf->minor] = NULL;
246d4ead16fSAlan Stern 	up_write(&minor_rwsem);
2471da177e4SLinus Torvalds 
2480873c764SGreg Kroah-Hartman 	device_destroy(usb_class->class, MKDEV(USB_MAJOR, intf->minor));
2490873c764SGreg Kroah-Hartman 	intf->usb_dev = NULL;
2501da177e4SLinus Torvalds 	intf->minor = -1;
25143104f1dSGreg Kroah-Hartman 	destroy_usb_class();
2521da177e4SLinus Torvalds }
253782e70c6SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(usb_deregister_dev);
254