xref: /linux/drivers/usb/core/file.c (revision 303911cfc5b95d33687d9046133ff184cf5043ff)
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);
322f86a96bSAjay Kaher static DEFINE_MUTEX(init_usb_class_mutex);
331da177e4SLinus Torvalds 
341da177e4SLinus Torvalds static int usb_open(struct inode *inode, struct file *file)
351da177e4SLinus Torvalds {
361da177e4SLinus Torvalds 	int err = -ENODEV;
37e84f9e57SAl Viro 	const struct file_operations *new_fops;
381da177e4SLinus Torvalds 
39d4ead16fSAlan Stern 	down_read(&minor_rwsem);
40e84f9e57SAl Viro 	new_fops = fops_get(usb_minors[iminor(inode)]);
411da177e4SLinus Torvalds 
42e84f9e57SAl Viro 	if (!new_fops)
43d4ead16fSAlan Stern 		goto done;
441da177e4SLinus Torvalds 
45e84f9e57SAl Viro 	replace_fops(file, new_fops);
461da177e4SLinus Torvalds 	/* Curiouser and curiouser... NULL ->open() as "no device" ? */
471da177e4SLinus Torvalds 	if (file->f_op->open)
481da177e4SLinus Torvalds 		err = file->f_op->open(inode, file);
49d4ead16fSAlan Stern  done:
50d4ead16fSAlan Stern 	up_read(&minor_rwsem);
511da177e4SLinus Torvalds 	return err;
521da177e4SLinus Torvalds }
531da177e4SLinus Torvalds 
54066202ddSLuiz Fernando N. Capitulino static const struct file_operations usb_fops = {
551da177e4SLinus Torvalds 	.owner =	THIS_MODULE,
561da177e4SLinus Torvalds 	.open =		usb_open,
576038f373SArnd Bergmann 	.llseek =	noop_llseek,
581da177e4SLinus Torvalds };
591da177e4SLinus Torvalds 
6043104f1dSGreg Kroah-Hartman static struct usb_class {
6143104f1dSGreg Kroah-Hartman 	struct kref kref;
6243104f1dSGreg Kroah-Hartman 	struct class *class;
6343104f1dSGreg Kroah-Hartman } *usb_class;
6443104f1dSGreg Kroah-Hartman 
652c9ede55SAl Viro static char *usb_devnode(struct device *dev, umode_t *mode)
66f7a386c5SKay Sievers {
67f7a386c5SKay Sievers 	struct usb_class_driver *drv;
68f7a386c5SKay Sievers 
69f7a386c5SKay Sievers 	drv = dev_get_drvdata(dev);
70e454cea2SKay Sievers 	if (!drv || !drv->devnode)
71f7a386c5SKay Sievers 		return NULL;
72e454cea2SKay Sievers 	return drv->devnode(dev, mode);
73f7a386c5SKay Sievers }
74f7a386c5SKay Sievers 
7543104f1dSGreg Kroah-Hartman static int init_usb_class(void)
7643104f1dSGreg Kroah-Hartman {
7743104f1dSGreg Kroah-Hartman 	int result = 0;
7843104f1dSGreg Kroah-Hartman 
7943104f1dSGreg Kroah-Hartman 	if (usb_class != NULL) {
8043104f1dSGreg Kroah-Hartman 		kref_get(&usb_class->kref);
8143104f1dSGreg Kroah-Hartman 		goto exit;
8243104f1dSGreg Kroah-Hartman 	}
8343104f1dSGreg Kroah-Hartman 
8443104f1dSGreg Kroah-Hartman 	usb_class = kmalloc(sizeof(*usb_class), GFP_KERNEL);
8543104f1dSGreg Kroah-Hartman 	if (!usb_class) {
8643104f1dSGreg Kroah-Hartman 		result = -ENOMEM;
8743104f1dSGreg Kroah-Hartman 		goto exit;
8843104f1dSGreg Kroah-Hartman 	}
8943104f1dSGreg Kroah-Hartman 
9043104f1dSGreg Kroah-Hartman 	kref_init(&usb_class->kref);
917e97243cSGreg Kroah-Hartman 	usb_class->class = class_create(THIS_MODULE, "usbmisc");
9243104f1dSGreg Kroah-Hartman 	if (IS_ERR(usb_class->class)) {
93de5535f5SAlexey Khoroshilov 		result = PTR_ERR(usb_class->class);
9469a85942SGreg Kroah-Hartman 		printk(KERN_ERR "class_create failed for usb devices\n");
9543104f1dSGreg Kroah-Hartman 		kfree(usb_class);
9643104f1dSGreg Kroah-Hartman 		usb_class = NULL;
97ed7487c2SDan Carpenter 		goto exit;
9843104f1dSGreg Kroah-Hartman 	}
99e454cea2SKay Sievers 	usb_class->class->devnode = usb_devnode;
10043104f1dSGreg Kroah-Hartman 
10143104f1dSGreg Kroah-Hartman exit:
10243104f1dSGreg Kroah-Hartman 	return result;
10343104f1dSGreg Kroah-Hartman }
10443104f1dSGreg Kroah-Hartman 
10543104f1dSGreg Kroah-Hartman static void release_usb_class(struct kref *kref)
10643104f1dSGreg Kroah-Hartman {
10743104f1dSGreg Kroah-Hartman 	/* Ok, we cheat as we know we only have one usb_class */
10843104f1dSGreg Kroah-Hartman 	class_destroy(usb_class->class);
10943104f1dSGreg Kroah-Hartman 	kfree(usb_class);
11043104f1dSGreg Kroah-Hartman 	usb_class = NULL;
11143104f1dSGreg Kroah-Hartman }
11243104f1dSGreg Kroah-Hartman 
11343104f1dSGreg Kroah-Hartman static void destroy_usb_class(void)
11443104f1dSGreg Kroah-Hartman {
1152f86a96bSAjay Kaher 	mutex_lock(&init_usb_class_mutex);
11643104f1dSGreg Kroah-Hartman 	kref_put(&usb_class->kref, release_usb_class);
1172f86a96bSAjay Kaher 	mutex_unlock(&init_usb_class_mutex);
11843104f1dSGreg Kroah-Hartman }
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds int usb_major_init(void)
1211da177e4SLinus Torvalds {
1221da177e4SLinus Torvalds 	int error;
1231da177e4SLinus Torvalds 
1241da177e4SLinus Torvalds 	error = register_chrdev(USB_MAJOR, "usb", &usb_fops);
12543104f1dSGreg Kroah-Hartman 	if (error)
12669a85942SGreg Kroah-Hartman 		printk(KERN_ERR "Unable to get major %d for usb devices\n",
12769a85942SGreg Kroah-Hartman 		       USB_MAJOR);
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds 	return error;
1301da177e4SLinus Torvalds }
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds void usb_major_cleanup(void)
1331da177e4SLinus Torvalds {
1341da177e4SLinus Torvalds 	unregister_chrdev(USB_MAJOR, "usb");
1351da177e4SLinus Torvalds }
1361da177e4SLinus Torvalds 
1371da177e4SLinus Torvalds /**
1381da177e4SLinus Torvalds  * usb_register_dev - register a USB device, and ask for a minor number
1391da177e4SLinus Torvalds  * @intf: pointer to the usb_interface that is being registered
1401da177e4SLinus Torvalds  * @class_driver: pointer to the usb_class_driver for this device
1411da177e4SLinus Torvalds  *
1421da177e4SLinus Torvalds  * This should be called by all USB drivers that use the USB major number.
1431da177e4SLinus Torvalds  * If CONFIG_USB_DYNAMIC_MINORS is enabled, the minor number will be
1441da177e4SLinus Torvalds  * dynamically allocated out of the list of available ones.  If it is not
1451da177e4SLinus Torvalds  * enabled, the minor number will be based on the next available free minor,
1461da177e4SLinus Torvalds  * starting at the class_driver->minor_base.
1471da177e4SLinus Torvalds  *
148d6e5bcf4SGreg Kroah-Hartman  * This function also creates a usb class device in the sysfs tree.
1491da177e4SLinus Torvalds  *
1501da177e4SLinus Torvalds  * usb_deregister_dev() must be called when the driver is done with
1511da177e4SLinus Torvalds  * the minor numbers given out by this function.
1521da177e4SLinus Torvalds  *
153626f090cSYacine Belkadi  * Return: -EINVAL if something bad happens with trying to register a
1541da177e4SLinus Torvalds  * device, and 0 on success.
1551da177e4SLinus Torvalds  */
1561da177e4SLinus Torvalds int usb_register_dev(struct usb_interface *intf,
1571da177e4SLinus Torvalds 		     struct usb_class_driver *class_driver)
1581da177e4SLinus Torvalds {
1590026e005SAlan Stern 	int retval;
1601da177e4SLinus Torvalds 	int minor_base = class_driver->minor_base;
1610026e005SAlan Stern 	int minor;
1627071a3ceSKay Sievers 	char name[20];
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds #ifdef CONFIG_USB_DYNAMIC_MINORS
1651da177e4SLinus Torvalds 	/*
1661da177e4SLinus Torvalds 	 * We don't care what the device tries to start at, we want to start
1671da177e4SLinus Torvalds 	 * at zero to pack the devices into the smallest available space with
1681da177e4SLinus Torvalds 	 * no holes in the minor range.
1691da177e4SLinus Torvalds 	 */
1701da177e4SLinus Torvalds 	minor_base = 0;
1711da177e4SLinus Torvalds #endif
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds 	if (class_driver->fops == NULL)
1740026e005SAlan Stern 		return -EINVAL;
1750026e005SAlan Stern 	if (intf->minor >= 0)
1760026e005SAlan Stern 		return -EADDRINUSE;
1770026e005SAlan Stern 
1782f86a96bSAjay Kaher 	mutex_lock(&init_usb_class_mutex);
1790026e005SAlan Stern 	retval = init_usb_class();
1802f86a96bSAjay Kaher 	mutex_unlock(&init_usb_class_mutex);
1812f86a96bSAjay Kaher 
1820026e005SAlan Stern 	if (retval)
1830026e005SAlan Stern 		return retval;
1840026e005SAlan Stern 
185079d440eSGreg Kroah-Hartman 	dev_dbg(&intf->dev, "looking for a minor, starting at %d\n", minor_base);
1861da177e4SLinus Torvalds 
187d4ead16fSAlan Stern 	down_write(&minor_rwsem);
1881da177e4SLinus Torvalds 	for (minor = minor_base; minor < MAX_USB_MINORS; ++minor) {
1891da177e4SLinus Torvalds 		if (usb_minors[minor])
1901da177e4SLinus Torvalds 			continue;
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds 		usb_minors[minor] = class_driver->fops;
1930026e005SAlan Stern 		intf->minor = minor;
1941da177e4SLinus Torvalds 		break;
1951da177e4SLinus Torvalds 	}
196*303911cfSAlan Stern 	if (intf->minor < 0) {
197d4ead16fSAlan Stern 		up_write(&minor_rwsem);
1980026e005SAlan Stern 		return -EXFULL;
199*303911cfSAlan Stern 	}
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);
203b0b090e5SGreg Kroah-Hartman 	intf->usb_dev = device_create(usb_class->class, &intf->dev,
204f7a386c5SKay Sievers 				      MKDEV(USB_MAJOR, minor), class_driver,
205f8868ed0SAndy Shevchenko 				      "%s", kbasename(name));
2060873c764SGreg Kroah-Hartman 	if (IS_ERR(intf->usb_dev)) {
2070026e005SAlan Stern 		usb_minors[minor] = NULL;
2080026e005SAlan Stern 		intf->minor = -1;
2090873c764SGreg Kroah-Hartman 		retval = PTR_ERR(intf->usb_dev);
2101da177e4SLinus Torvalds 	}
211*303911cfSAlan Stern 	up_write(&minor_rwsem);
2121da177e4SLinus Torvalds 	return retval;
2131da177e4SLinus Torvalds }
214782e70c6SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(usb_register_dev);
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds /**
2171da177e4SLinus Torvalds  * usb_deregister_dev - deregister a USB device's dynamic minor.
2181da177e4SLinus Torvalds  * @intf: pointer to the usb_interface that is being deregistered
2191da177e4SLinus Torvalds  * @class_driver: pointer to the usb_class_driver for this device
2201da177e4SLinus Torvalds  *
2211da177e4SLinus Torvalds  * Used in conjunction with usb_register_dev().  This function is called
2221da177e4SLinus Torvalds  * when the USB driver is finished with the minor numbers gotten from a
2231da177e4SLinus Torvalds  * call to usb_register_dev() (usually when the device is disconnected
2241da177e4SLinus Torvalds  * from the system.)
2251da177e4SLinus Torvalds  *
226d6e5bcf4SGreg Kroah-Hartman  * This function also removes the usb class device from the sysfs tree.
2271da177e4SLinus Torvalds  *
2281da177e4SLinus Torvalds  * This should be called by all drivers that use the USB major number.
2291da177e4SLinus Torvalds  */
2301da177e4SLinus Torvalds void usb_deregister_dev(struct usb_interface *intf,
2311da177e4SLinus Torvalds 			struct usb_class_driver *class_driver)
2321da177e4SLinus Torvalds {
2331da177e4SLinus Torvalds 	if (intf->minor == -1)
2341da177e4SLinus Torvalds 		return;
2351da177e4SLinus Torvalds 
236079d440eSGreg Kroah-Hartman 	dev_dbg(&intf->dev, "removing %d minor\n", intf->minor);
237*303911cfSAlan Stern 	device_destroy(usb_class->class, MKDEV(USB_MAJOR, intf->minor));
2381da177e4SLinus Torvalds 
239d4ead16fSAlan Stern 	down_write(&minor_rwsem);
2401da177e4SLinus Torvalds 	usb_minors[intf->minor] = NULL;
241d4ead16fSAlan Stern 	up_write(&minor_rwsem);
2421da177e4SLinus Torvalds 
2430873c764SGreg Kroah-Hartman 	intf->usb_dev = NULL;
2441da177e4SLinus Torvalds 	intf->minor = -1;
24543104f1dSGreg Kroah-Hartman 	destroy_usb_class();
2461da177e4SLinus Torvalds }
247782e70c6SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(usb_deregister_dev);
248