xref: /linux/drivers/usb/core/file.c (revision 99ac48f54a91d02140c497edc31dc57d4bc5c85d)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * drivers/usb/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/config.h>
191da177e4SLinus Torvalds #include <linux/module.h>
201da177e4SLinus Torvalds #include <linux/spinlock.h>
211da177e4SLinus Torvalds #include <linux/errno.h>
221da177e4SLinus Torvalds #include <linux/usb.h>
231da177e4SLinus Torvalds 
246d5e8254SGreg KH #include "usb.h"
256d5e8254SGreg KH 
261da177e4SLinus Torvalds #define MAX_USB_MINORS	256
27*99ac48f5SArjan van de Ven static const struct file_operations *usb_minors[MAX_USB_MINORS];
281da177e4SLinus Torvalds static DEFINE_SPINLOCK(minor_lock);
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds static int usb_open(struct inode * inode, struct file * file)
311da177e4SLinus Torvalds {
321da177e4SLinus Torvalds 	int minor = iminor(inode);
33*99ac48f5SArjan van de Ven 	const struct file_operations *c;
341da177e4SLinus Torvalds 	int err = -ENODEV;
35*99ac48f5SArjan van de Ven 	const struct file_operations *old_fops, *new_fops = NULL;
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds 	spin_lock (&minor_lock);
381da177e4SLinus Torvalds 	c = usb_minors[minor];
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds 	if (!c || !(new_fops = fops_get(c))) {
411da177e4SLinus Torvalds 		spin_unlock(&minor_lock);
421da177e4SLinus Torvalds 		return err;
431da177e4SLinus Torvalds 	}
441da177e4SLinus Torvalds 	spin_unlock(&minor_lock);
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds 	old_fops = file->f_op;
471da177e4SLinus Torvalds 	file->f_op = new_fops;
481da177e4SLinus Torvalds 	/* Curiouser and curiouser... NULL ->open() as "no device" ? */
491da177e4SLinus Torvalds 	if (file->f_op->open)
501da177e4SLinus Torvalds 		err = file->f_op->open(inode,file);
511da177e4SLinus Torvalds 	if (err) {
521da177e4SLinus Torvalds 		fops_put(file->f_op);
531da177e4SLinus Torvalds 		file->f_op = fops_get(old_fops);
541da177e4SLinus Torvalds 	}
551da177e4SLinus Torvalds 	fops_put(old_fops);
561da177e4SLinus Torvalds 	return err;
571da177e4SLinus Torvalds }
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds static struct file_operations usb_fops = {
601da177e4SLinus Torvalds 	.owner =	THIS_MODULE,
611da177e4SLinus Torvalds 	.open =		usb_open,
621da177e4SLinus Torvalds };
631da177e4SLinus Torvalds 
6456b22935Sgregkh@suse.de static struct class *usb_class;
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds int usb_major_init(void)
671da177e4SLinus Torvalds {
681da177e4SLinus Torvalds 	int error;
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds 	error = register_chrdev(USB_MAJOR, "usb", &usb_fops);
711da177e4SLinus Torvalds 	if (error) {
721da177e4SLinus Torvalds 		err("unable to get major %d for usb devices", USB_MAJOR);
731da177e4SLinus Torvalds 		goto out;
741da177e4SLinus Torvalds 	}
751da177e4SLinus Torvalds 
7656b22935Sgregkh@suse.de 	usb_class = class_create(THIS_MODULE, "usb");
771da177e4SLinus Torvalds 	if (IS_ERR(usb_class)) {
785cebfb75SMark M. Hoffman 		error = PTR_ERR(usb_class);
7956b22935Sgregkh@suse.de 		err("class_create failed for usb devices");
801da177e4SLinus Torvalds 		unregister_chrdev(USB_MAJOR, "usb");
811da177e4SLinus Torvalds 		goto out;
821da177e4SLinus Torvalds 	}
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds out:
851da177e4SLinus Torvalds 	return error;
861da177e4SLinus Torvalds }
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds void usb_major_cleanup(void)
891da177e4SLinus Torvalds {
9056b22935Sgregkh@suse.de 	class_destroy(usb_class);
911da177e4SLinus Torvalds 	unregister_chrdev(USB_MAJOR, "usb");
921da177e4SLinus Torvalds }
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds /**
951da177e4SLinus Torvalds  * usb_register_dev - register a USB device, and ask for a minor number
961da177e4SLinus Torvalds  * @intf: pointer to the usb_interface that is being registered
971da177e4SLinus Torvalds  * @class_driver: pointer to the usb_class_driver for this device
981da177e4SLinus Torvalds  *
991da177e4SLinus Torvalds  * This should be called by all USB drivers that use the USB major number.
1001da177e4SLinus Torvalds  * If CONFIG_USB_DYNAMIC_MINORS is enabled, the minor number will be
1011da177e4SLinus Torvalds  * dynamically allocated out of the list of available ones.  If it is not
1021da177e4SLinus Torvalds  * enabled, the minor number will be based on the next available free minor,
1031da177e4SLinus Torvalds  * starting at the class_driver->minor_base.
1041da177e4SLinus Torvalds  *
105d6e5bcf4SGreg Kroah-Hartman  * This function also creates a usb class device in the sysfs tree.
1061da177e4SLinus Torvalds  *
1071da177e4SLinus Torvalds  * usb_deregister_dev() must be called when the driver is done with
1081da177e4SLinus Torvalds  * the minor numbers given out by this function.
1091da177e4SLinus Torvalds  *
1101da177e4SLinus Torvalds  * Returns -EINVAL if something bad happens with trying to register a
1111da177e4SLinus Torvalds  * device, and 0 on success.
1121da177e4SLinus Torvalds  */
1131da177e4SLinus Torvalds int usb_register_dev(struct usb_interface *intf,
1141da177e4SLinus Torvalds 		     struct usb_class_driver *class_driver)
1151da177e4SLinus Torvalds {
1161da177e4SLinus Torvalds 	int retval = -EINVAL;
1171da177e4SLinus Torvalds 	int minor_base = class_driver->minor_base;
1181da177e4SLinus Torvalds 	int minor = 0;
1191da177e4SLinus Torvalds 	char name[BUS_ID_SIZE];
1201da177e4SLinus Torvalds 	char *temp;
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds #ifdef CONFIG_USB_DYNAMIC_MINORS
1231da177e4SLinus Torvalds 	/*
1241da177e4SLinus Torvalds 	 * We don't care what the device tries to start at, we want to start
1251da177e4SLinus Torvalds 	 * at zero to pack the devices into the smallest available space with
1261da177e4SLinus Torvalds 	 * no holes in the minor range.
1271da177e4SLinus Torvalds 	 */
1281da177e4SLinus Torvalds 	minor_base = 0;
1291da177e4SLinus Torvalds #endif
1301da177e4SLinus Torvalds 	intf->minor = -1;
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds 	dbg ("looking for a minor, starting at %d", minor_base);
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds 	if (class_driver->fops == NULL)
1351da177e4SLinus Torvalds 		goto exit;
1361da177e4SLinus Torvalds 
1371da177e4SLinus Torvalds 	spin_lock (&minor_lock);
1381da177e4SLinus Torvalds 	for (minor = minor_base; minor < MAX_USB_MINORS; ++minor) {
1391da177e4SLinus Torvalds 		if (usb_minors[minor])
1401da177e4SLinus Torvalds 			continue;
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds 		usb_minors[minor] = class_driver->fops;
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds 		retval = 0;
1451da177e4SLinus Torvalds 		break;
1461da177e4SLinus Torvalds 	}
1471da177e4SLinus Torvalds 	spin_unlock (&minor_lock);
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds 	if (retval)
1501da177e4SLinus Torvalds 		goto exit;
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds 	intf->minor = minor;
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds 	/* create a usb class device for this usb interface */
155d6e5bcf4SGreg Kroah-Hartman 	snprintf(name, BUS_ID_SIZE, class_driver->name, minor - minor_base);
1561da177e4SLinus Torvalds 	temp = strrchr(name, '/');
1571da177e4SLinus Torvalds 	if (temp && (temp[1] != 0x00))
1581da177e4SLinus Torvalds 		++temp;
1591da177e4SLinus Torvalds 	else
1601da177e4SLinus Torvalds 		temp = name;
16153f46542SGreg Kroah-Hartman 	intf->class_dev = class_device_create(usb_class, NULL,
16253f46542SGreg Kroah-Hartman 					      MKDEV(USB_MAJOR, minor),
16353f46542SGreg Kroah-Hartman 					      &intf->dev, "%s", temp);
1641da177e4SLinus Torvalds 	if (IS_ERR(intf->class_dev)) {
1651da177e4SLinus Torvalds 		spin_lock (&minor_lock);
1661da177e4SLinus Torvalds 		usb_minors[intf->minor] = NULL;
1671da177e4SLinus Torvalds 		spin_unlock (&minor_lock);
1681da177e4SLinus Torvalds 		retval = PTR_ERR(intf->class_dev);
1691da177e4SLinus Torvalds 	}
1701da177e4SLinus Torvalds exit:
1711da177e4SLinus Torvalds 	return retval;
1721da177e4SLinus Torvalds }
1731da177e4SLinus Torvalds EXPORT_SYMBOL(usb_register_dev);
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds /**
1761da177e4SLinus Torvalds  * usb_deregister_dev - deregister a USB device's dynamic minor.
1771da177e4SLinus Torvalds  * @intf: pointer to the usb_interface that is being deregistered
1781da177e4SLinus Torvalds  * @class_driver: pointer to the usb_class_driver for this device
1791da177e4SLinus Torvalds  *
1801da177e4SLinus Torvalds  * Used in conjunction with usb_register_dev().  This function is called
1811da177e4SLinus Torvalds  * when the USB driver is finished with the minor numbers gotten from a
1821da177e4SLinus Torvalds  * call to usb_register_dev() (usually when the device is disconnected
1831da177e4SLinus Torvalds  * from the system.)
1841da177e4SLinus Torvalds  *
185d6e5bcf4SGreg Kroah-Hartman  * This function also removes the usb class device from the sysfs tree.
1861da177e4SLinus Torvalds  *
1871da177e4SLinus Torvalds  * This should be called by all drivers that use the USB major number.
1881da177e4SLinus Torvalds  */
1891da177e4SLinus Torvalds void usb_deregister_dev(struct usb_interface *intf,
1901da177e4SLinus Torvalds 			struct usb_class_driver *class_driver)
1911da177e4SLinus Torvalds {
1921da177e4SLinus Torvalds 	int minor_base = class_driver->minor_base;
1931da177e4SLinus Torvalds 	char name[BUS_ID_SIZE];
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds #ifdef CONFIG_USB_DYNAMIC_MINORS
1961da177e4SLinus Torvalds 	minor_base = 0;
1971da177e4SLinus Torvalds #endif
1981da177e4SLinus Torvalds 
1991da177e4SLinus Torvalds 	if (intf->minor == -1)
2001da177e4SLinus Torvalds 		return;
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds 	dbg ("removing %d minor", intf->minor);
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds 	spin_lock (&minor_lock);
2051da177e4SLinus Torvalds 	usb_minors[intf->minor] = NULL;
2061da177e4SLinus Torvalds 	spin_unlock (&minor_lock);
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds 	snprintf(name, BUS_ID_SIZE, class_driver->name, intf->minor - minor_base);
20956b22935Sgregkh@suse.de 	class_device_destroy(usb_class, MKDEV(USB_MAJOR, intf->minor));
2101da177e4SLinus Torvalds 	intf->class_dev = NULL;
2111da177e4SLinus Torvalds 	intf->minor = -1;
2121da177e4SLinus Torvalds }
2131da177e4SLinus Torvalds EXPORT_SYMBOL(usb_deregister_dev);
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds 
216