xref: /linux/drivers/usb/core/file.c (revision a1c613ae4c322ddd58d5a8539dbfba2a0380a8c0)
1aa1f3bb5SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
3f30c2269SUwe Zeisberger  * drivers/usb/core/file.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * (C) Copyright Linus Torvalds 1999
61da177e4SLinus Torvalds  * (C) Copyright Johannes Erdfelt 1999-2001
71da177e4SLinus Torvalds  * (C) Copyright Andreas Gal 1999
81da177e4SLinus Torvalds  * (C) Copyright Gregory P. Smith 1999
91da177e4SLinus Torvalds  * (C) Copyright Deti Fliegl 1999 (new USB architecture)
101da177e4SLinus Torvalds  * (C) Copyright Randy Dunlap 2000
111da177e4SLinus Torvalds  * (C) Copyright David Brownell 2000-2001 (kernel hotplug, usb_device_id,
12469271f8SMatthias Beyer  *	more docs, etc)
131da177e4SLinus Torvalds  * (C) Copyright Yggdrasil Computing, Inc. 2000
141da177e4SLinus Torvalds  *     (usb_device_id matching changes by Adam J. Richter)
151da177e4SLinus Torvalds  * (C) Copyright Greg Kroah-Hartman 2002-2003
161da177e4SLinus Torvalds  *
17b65fba3dSGreg Kroah-Hartman  * Released under the GPLv2 only.
181da177e4SLinus Torvalds  */
191da177e4SLinus Torvalds 
201da177e4SLinus Torvalds #include <linux/module.h>
211da177e4SLinus Torvalds #include <linux/errno.h>
22d4ead16fSAlan Stern #include <linux/rwsem.h>
235a0e3ad6STejun Heo #include <linux/slab.h>
24f8868ed0SAndy Shevchenko #include <linux/string.h>
251da177e4SLinus Torvalds #include <linux/usb.h>
261da177e4SLinus Torvalds 
276d5e8254SGreg KH #include "usb.h"
286d5e8254SGreg KH 
291da177e4SLinus Torvalds #define MAX_USB_MINORS	256
3099ac48f5SArjan van de Ven static const struct file_operations *usb_minors[MAX_USB_MINORS];
31d4ead16fSAlan Stern static DECLARE_RWSEM(minor_rwsem);
321da177e4SLinus Torvalds 
usb_open(struct inode * inode,struct file * file)331da177e4SLinus Torvalds static int usb_open(struct inode *inode, struct file *file)
341da177e4SLinus Torvalds {
351da177e4SLinus Torvalds 	int err = -ENODEV;
36e84f9e57SAl Viro 	const struct file_operations *new_fops;
371da177e4SLinus Torvalds 
38d4ead16fSAlan Stern 	down_read(&minor_rwsem);
39e84f9e57SAl Viro 	new_fops = fops_get(usb_minors[iminor(inode)]);
401da177e4SLinus Torvalds 
41e84f9e57SAl Viro 	if (!new_fops)
42d4ead16fSAlan Stern 		goto done;
431da177e4SLinus Torvalds 
44e84f9e57SAl Viro 	replace_fops(file, 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);
48d4ead16fSAlan Stern  done:
49d4ead16fSAlan Stern 	up_read(&minor_rwsem);
501da177e4SLinus Torvalds 	return err;
511da177e4SLinus Torvalds }
521da177e4SLinus Torvalds 
53066202ddSLuiz Fernando N. Capitulino static const struct file_operations usb_fops = {
541da177e4SLinus Torvalds 	.owner =	THIS_MODULE,
551da177e4SLinus Torvalds 	.open =		usb_open,
566038f373SArnd Bergmann 	.llseek =	noop_llseek,
571da177e4SLinus Torvalds };
581da177e4SLinus Torvalds 
usb_devnode(const struct device * dev,umode_t * mode)59ff62b8e6SGreg Kroah-Hartman static char *usb_devnode(const struct device *dev, umode_t *mode)
60f7a386c5SKay Sievers {
61f7a386c5SKay Sievers 	struct usb_class_driver *drv;
62f7a386c5SKay Sievers 
63f7a386c5SKay Sievers 	drv = dev_get_drvdata(dev);
64e454cea2SKay Sievers 	if (!drv || !drv->devnode)
65f7a386c5SKay Sievers 		return NULL;
66e454cea2SKay Sievers 	return drv->devnode(dev, mode);
67f7a386c5SKay Sievers }
68f7a386c5SKay Sievers 
69*015fbddeSIvan Orlov const struct class usbmisc_class = {
70*015fbddeSIvan Orlov 	.name		= "usbmisc",
71*015fbddeSIvan Orlov 	.devnode	= usb_devnode,
72*015fbddeSIvan Orlov };
731da177e4SLinus Torvalds 
usb_major_init(void)741da177e4SLinus Torvalds int usb_major_init(void)
751da177e4SLinus Torvalds {
761da177e4SLinus Torvalds 	int error;
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds 	error = register_chrdev(USB_MAJOR, "usb", &usb_fops);
7943104f1dSGreg Kroah-Hartman 	if (error)
8069a85942SGreg Kroah-Hartman 		printk(KERN_ERR "Unable to get major %d for usb devices\n",
8169a85942SGreg Kroah-Hartman 		       USB_MAJOR);
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds 	return error;
841da177e4SLinus Torvalds }
851da177e4SLinus Torvalds 
usb_major_cleanup(void)861da177e4SLinus Torvalds void usb_major_cleanup(void)
871da177e4SLinus Torvalds {
881da177e4SLinus Torvalds 	unregister_chrdev(USB_MAJOR, "usb");
891da177e4SLinus Torvalds }
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds /**
921da177e4SLinus Torvalds  * usb_register_dev - register a USB device, and ask for a minor number
931da177e4SLinus Torvalds  * @intf: pointer to the usb_interface that is being registered
941da177e4SLinus Torvalds  * @class_driver: pointer to the usb_class_driver for this device
951da177e4SLinus Torvalds  *
961da177e4SLinus Torvalds  * This should be called by all USB drivers that use the USB major number.
971da177e4SLinus Torvalds  * If CONFIG_USB_DYNAMIC_MINORS is enabled, the minor number will be
981da177e4SLinus Torvalds  * dynamically allocated out of the list of available ones.  If it is not
991da177e4SLinus Torvalds  * enabled, the minor number will be based on the next available free minor,
1001da177e4SLinus Torvalds  * starting at the class_driver->minor_base.
1011da177e4SLinus Torvalds  *
102d6e5bcf4SGreg Kroah-Hartman  * This function also creates a usb class device in the sysfs tree.
1031da177e4SLinus Torvalds  *
1041da177e4SLinus Torvalds  * usb_deregister_dev() must be called when the driver is done with
1051da177e4SLinus Torvalds  * the minor numbers given out by this function.
1061da177e4SLinus Torvalds  *
107626f090cSYacine Belkadi  * Return: -EINVAL if something bad happens with trying to register a
1081da177e4SLinus Torvalds  * device, and 0 on success.
1091da177e4SLinus Torvalds  */
usb_register_dev(struct usb_interface * intf,struct usb_class_driver * class_driver)1101da177e4SLinus Torvalds int usb_register_dev(struct usb_interface *intf,
1111da177e4SLinus Torvalds 		     struct usb_class_driver *class_driver)
1121da177e4SLinus Torvalds {
113*015fbddeSIvan Orlov 	int retval = 0;
1141da177e4SLinus Torvalds 	int minor_base = class_driver->minor_base;
1150026e005SAlan Stern 	int minor;
1167071a3ceSKay Sievers 	char name[20];
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds #ifdef CONFIG_USB_DYNAMIC_MINORS
1191da177e4SLinus Torvalds 	/*
1201da177e4SLinus Torvalds 	 * We don't care what the device tries to start at, we want to start
1211da177e4SLinus Torvalds 	 * at zero to pack the devices into the smallest available space with
1221da177e4SLinus Torvalds 	 * no holes in the minor range.
1231da177e4SLinus Torvalds 	 */
1241da177e4SLinus Torvalds 	minor_base = 0;
1251da177e4SLinus Torvalds #endif
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds 	if (class_driver->fops == NULL)
1280026e005SAlan Stern 		return -EINVAL;
1290026e005SAlan Stern 	if (intf->minor >= 0)
1300026e005SAlan Stern 		return -EADDRINUSE;
1310026e005SAlan Stern 
132079d440eSGreg Kroah-Hartman 	dev_dbg(&intf->dev, "looking for a minor, starting at %d\n", minor_base);
1331da177e4SLinus Torvalds 
134d4ead16fSAlan Stern 	down_write(&minor_rwsem);
1351da177e4SLinus Torvalds 	for (minor = minor_base; minor < MAX_USB_MINORS; ++minor) {
1361da177e4SLinus Torvalds 		if (usb_minors[minor])
1371da177e4SLinus Torvalds 			continue;
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds 		usb_minors[minor] = class_driver->fops;
1400026e005SAlan Stern 		intf->minor = minor;
1411da177e4SLinus Torvalds 		break;
1421da177e4SLinus Torvalds 	}
143303911cfSAlan Stern 	if (intf->minor < 0) {
144d4ead16fSAlan Stern 		up_write(&minor_rwsem);
1450026e005SAlan Stern 		return -EXFULL;
146303911cfSAlan Stern 	}
1471da177e4SLinus Torvalds 
1481da177e4SLinus Torvalds 	/* create a usb class device for this usb interface */
1497071a3ceSKay Sievers 	snprintf(name, sizeof(name), class_driver->name, minor - minor_base);
150*015fbddeSIvan Orlov 	intf->usb_dev = device_create(&usbmisc_class, &intf->dev,
151f7a386c5SKay Sievers 				      MKDEV(USB_MAJOR, minor), class_driver,
152f8868ed0SAndy Shevchenko 				      "%s", kbasename(name));
1530873c764SGreg Kroah-Hartman 	if (IS_ERR(intf->usb_dev)) {
1540026e005SAlan Stern 		usb_minors[minor] = NULL;
1550026e005SAlan Stern 		intf->minor = -1;
1560873c764SGreg Kroah-Hartman 		retval = PTR_ERR(intf->usb_dev);
1571da177e4SLinus Torvalds 	}
158303911cfSAlan Stern 	up_write(&minor_rwsem);
1591da177e4SLinus Torvalds 	return retval;
1601da177e4SLinus Torvalds }
161782e70c6SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(usb_register_dev);
1621da177e4SLinus Torvalds 
1631da177e4SLinus Torvalds /**
1641da177e4SLinus Torvalds  * usb_deregister_dev - deregister a USB device's dynamic minor.
1651da177e4SLinus Torvalds  * @intf: pointer to the usb_interface that is being deregistered
1661da177e4SLinus Torvalds  * @class_driver: pointer to the usb_class_driver for this device
1671da177e4SLinus Torvalds  *
1681da177e4SLinus Torvalds  * Used in conjunction with usb_register_dev().  This function is called
1691da177e4SLinus Torvalds  * when the USB driver is finished with the minor numbers gotten from a
1701da177e4SLinus Torvalds  * call to usb_register_dev() (usually when the device is disconnected
1711da177e4SLinus Torvalds  * from the system.)
1721da177e4SLinus Torvalds  *
173d6e5bcf4SGreg Kroah-Hartman  * This function also removes the usb class device from the sysfs tree.
1741da177e4SLinus Torvalds  *
1751da177e4SLinus Torvalds  * This should be called by all drivers that use the USB major number.
1761da177e4SLinus Torvalds  */
usb_deregister_dev(struct usb_interface * intf,struct usb_class_driver * class_driver)1771da177e4SLinus Torvalds void usb_deregister_dev(struct usb_interface *intf,
1781da177e4SLinus Torvalds 			struct usb_class_driver *class_driver)
1791da177e4SLinus Torvalds {
1801da177e4SLinus Torvalds 	if (intf->minor == -1)
1811da177e4SLinus Torvalds 		return;
1821da177e4SLinus Torvalds 
183079d440eSGreg Kroah-Hartman 	dev_dbg(&intf->dev, "removing %d minor\n", intf->minor);
184*015fbddeSIvan Orlov 	device_destroy(&usbmisc_class, MKDEV(USB_MAJOR, intf->minor));
1851da177e4SLinus Torvalds 
186d4ead16fSAlan Stern 	down_write(&minor_rwsem);
1871da177e4SLinus Torvalds 	usb_minors[intf->minor] = NULL;
188d4ead16fSAlan Stern 	up_write(&minor_rwsem);
1891da177e4SLinus Torvalds 
1900873c764SGreg Kroah-Hartman 	intf->usb_dev = NULL;
1911da177e4SLinus Torvalds 	intf->minor = -1;
1921da177e4SLinus Torvalds }
193782e70c6SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(usb_deregister_dev);
194