2017-11-03 09:18:41 +01:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2005-04-16 15:20:36 -07:00
|
|
|
/*
|
2006-10-03 23:01:26 +02:00
|
|
|
* drivers/usb/core/file.c
|
2005-04-16 15:20:36 -07:00
|
|
|
*
|
|
|
|
|
* (C) Copyright Linus Torvalds 1999
|
|
|
|
|
* (C) Copyright Johannes Erdfelt 1999-2001
|
|
|
|
|
* (C) Copyright Andreas Gal 1999
|
|
|
|
|
* (C) Copyright Gregory P. Smith 1999
|
|
|
|
|
* (C) Copyright Deti Fliegl 1999 (new USB architecture)
|
|
|
|
|
* (C) Copyright Randy Dunlap 2000
|
|
|
|
|
* (C) Copyright David Brownell 2000-2001 (kernel hotplug, usb_device_id,
|
2013-10-10 23:41:27 +02:00
|
|
|
* more docs, etc)
|
2005-04-16 15:20:36 -07:00
|
|
|
* (C) Copyright Yggdrasil Computing, Inc. 2000
|
|
|
|
|
* (usb_device_id matching changes by Adam J. Richter)
|
|
|
|
|
* (C) Copyright Greg Kroah-Hartman 2002-2003
|
|
|
|
|
*
|
2016-10-28 17:16:36 -04:00
|
|
|
* Released under the GPLv2 only.
|
2005-04-16 15:20:36 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
|
#include <linux/errno.h>
|
2007-05-22 11:46:41 -04:00
|
|
|
#include <linux/rwsem.h>
|
2010-03-24 17:04:11 +09:00
|
|
|
#include <linux/slab.h>
|
2015-12-10 16:49:14 +02:00
|
|
|
#include <linux/string.h>
|
2005-04-16 15:20:36 -07:00
|
|
|
#include <linux/usb.h>
|
|
|
|
|
|
2005-04-18 17:39:24 -07:00
|
|
|
#include "usb.h"
|
|
|
|
|
|
2005-04-16 15:20:36 -07:00
|
|
|
#define MAX_USB_MINORS 256
|
2006-03-28 01:56:41 -08:00
|
|
|
static const struct file_operations *usb_minors[MAX_USB_MINORS];
|
2007-05-22 11:46:41 -04:00
|
|
|
static DECLARE_RWSEM(minor_rwsem);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2013-10-10 23:41:28 +02:00
|
|
|
static int usb_open(struct inode *inode, struct file *file)
|
2005-04-16 15:20:36 -07:00
|
|
|
{
|
|
|
|
|
int err = -ENODEV;
|
2013-09-22 14:17:15 -04:00
|
|
|
const struct file_operations *new_fops;
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2007-05-22 11:46:41 -04:00
|
|
|
down_read(&minor_rwsem);
|
2013-09-22 14:17:15 -04:00
|
|
|
new_fops = fops_get(usb_minors[iminor(inode)]);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2013-09-22 14:17:15 -04:00
|
|
|
if (!new_fops)
|
2007-05-22 11:46:41 -04:00
|
|
|
goto done;
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2013-09-22 14:17:15 -04:00
|
|
|
replace_fops(file, new_fops);
|
2005-04-16 15:20:36 -07:00
|
|
|
/* Curiouser and curiouser... NULL ->open() as "no device" ? */
|
|
|
|
|
if (file->f_op->open)
|
2013-10-10 23:41:27 +02:00
|
|
|
err = file->f_op->open(inode, file);
|
2007-05-22 11:46:41 -04:00
|
|
|
done:
|
|
|
|
|
up_read(&minor_rwsem);
|
2005-04-16 15:20:36 -07:00
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
2006-08-05 20:37:11 -03:00
|
|
|
static const struct file_operations usb_fops = {
|
2005-04-16 15:20:36 -07:00
|
|
|
.owner = THIS_MODULE,
|
|
|
|
|
.open = usb_open,
|
2010-08-15 18:52:59 +02:00
|
|
|
.llseek = noop_llseek,
|
2005-04-16 15:20:36 -07:00
|
|
|
};
|
|
|
|
|
|
2022-11-23 13:25:20 +01:00
|
|
|
static char *usb_devnode(const struct device *dev, umode_t *mode)
|
2009-04-30 15:23:42 +02:00
|
|
|
{
|
|
|
|
|
struct usb_class_driver *drv;
|
|
|
|
|
|
|
|
|
|
drv = dev_get_drvdata(dev);
|
2009-09-18 23:01:12 +02:00
|
|
|
if (!drv || !drv->devnode)
|
2009-04-30 15:23:42 +02:00
|
|
|
return NULL;
|
2009-09-18 23:01:12 +02:00
|
|
|
return drv->devnode(dev, mode);
|
2009-04-30 15:23:42 +02:00
|
|
|
}
|
|
|
|
|
|
2023-06-22 00:25:14 +04:00
|
|
|
const struct class usbmisc_class = {
|
|
|
|
|
.name = "usbmisc",
|
|
|
|
|
.devnode = usb_devnode,
|
|
|
|
|
};
|
2005-04-16 15:20:36 -07:00
|
|
|
|
|
|
|
|
int usb_major_init(void)
|
|
|
|
|
{
|
|
|
|
|
int error;
|
|
|
|
|
|
|
|
|
|
error = register_chrdev(USB_MAJOR, "usb", &usb_fops);
|
2006-06-20 15:14:07 -07:00
|
|
|
if (error)
|
2008-08-14 09:37:34 -07:00
|
|
|
printk(KERN_ERR "Unable to get major %d for usb devices\n",
|
|
|
|
|
USB_MAJOR);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
|
|
|
|
return error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void usb_major_cleanup(void)
|
|
|
|
|
{
|
|
|
|
|
unregister_chrdev(USB_MAJOR, "usb");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* usb_register_dev - register a USB device, and ask for a minor number
|
|
|
|
|
* @intf: pointer to the usb_interface that is being registered
|
|
|
|
|
* @class_driver: pointer to the usb_class_driver for this device
|
|
|
|
|
*
|
|
|
|
|
* This should be called by all USB drivers that use the USB major number.
|
|
|
|
|
* If CONFIG_USB_DYNAMIC_MINORS is enabled, the minor number will be
|
|
|
|
|
* dynamically allocated out of the list of available ones. If it is not
|
|
|
|
|
* enabled, the minor number will be based on the next available free minor,
|
|
|
|
|
* starting at the class_driver->minor_base.
|
|
|
|
|
*
|
2005-06-20 21:15:16 -07:00
|
|
|
* This function also creates a usb class device in the sysfs tree.
|
2005-04-16 15:20:36 -07:00
|
|
|
*
|
|
|
|
|
* usb_deregister_dev() must be called when the driver is done with
|
|
|
|
|
* the minor numbers given out by this function.
|
|
|
|
|
*
|
2013-08-02 20:10:04 +02:00
|
|
|
* Return: -EINVAL if something bad happens with trying to register a
|
2005-04-16 15:20:36 -07:00
|
|
|
* device, and 0 on success.
|
|
|
|
|
*/
|
|
|
|
|
int usb_register_dev(struct usb_interface *intf,
|
|
|
|
|
struct usb_class_driver *class_driver)
|
|
|
|
|
{
|
2023-06-22 00:25:14 +04:00
|
|
|
int retval = 0;
|
2005-04-16 15:20:36 -07:00
|
|
|
int minor_base = class_driver->minor_base;
|
2010-09-21 15:01:53 -04:00
|
|
|
int minor;
|
2008-05-02 06:02:41 +02:00
|
|
|
char name[20];
|
2005-04-16 15:20:36 -07:00
|
|
|
|
|
|
|
|
#ifdef CONFIG_USB_DYNAMIC_MINORS
|
2013-10-10 23:41:27 +02:00
|
|
|
/*
|
2005-04-16 15:20:36 -07:00
|
|
|
* We don't care what the device tries to start at, we want to start
|
|
|
|
|
* at zero to pack the devices into the smallest available space with
|
|
|
|
|
* no holes in the minor range.
|
|
|
|
|
*/
|
|
|
|
|
minor_base = 0;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
if (class_driver->fops == NULL)
|
2010-09-21 15:01:53 -04:00
|
|
|
return -EINVAL;
|
|
|
|
|
if (intf->minor >= 0)
|
|
|
|
|
return -EADDRINUSE;
|
|
|
|
|
|
2012-05-01 21:33:35 -07:00
|
|
|
dev_dbg(&intf->dev, "looking for a minor, starting at %d\n", minor_base);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2007-05-22 11:46:41 -04:00
|
|
|
down_write(&minor_rwsem);
|
2005-04-16 15:20:36 -07:00
|
|
|
for (minor = minor_base; minor < MAX_USB_MINORS; ++minor) {
|
|
|
|
|
if (usb_minors[minor])
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
usb_minors[minor] = class_driver->fops;
|
2010-09-21 15:01:53 -04:00
|
|
|
intf->minor = minor;
|
2005-04-16 15:20:36 -07:00
|
|
|
break;
|
|
|
|
|
}
|
2019-08-12 16:11:07 -04:00
|
|
|
if (intf->minor < 0) {
|
|
|
|
|
up_write(&minor_rwsem);
|
2010-09-21 15:01:53 -04:00
|
|
|
return -EXFULL;
|
2019-08-12 16:11:07 -04:00
|
|
|
}
|
2005-04-16 15:20:36 -07:00
|
|
|
|
|
|
|
|
/* create a usb class device for this usb interface */
|
2008-05-02 06:02:41 +02:00
|
|
|
snprintf(name, sizeof(name), class_driver->name, minor - minor_base);
|
2023-06-22 00:25:14 +04:00
|
|
|
intf->usb_dev = device_create(&usbmisc_class, &intf->dev,
|
2009-04-30 15:23:42 +02:00
|
|
|
MKDEV(USB_MAJOR, minor), class_driver,
|
2015-12-10 16:49:14 +02:00
|
|
|
"%s", kbasename(name));
|
2006-06-20 13:09:50 -07:00
|
|
|
if (IS_ERR(intf->usb_dev)) {
|
2010-09-21 15:01:53 -04:00
|
|
|
usb_minors[minor] = NULL;
|
|
|
|
|
intf->minor = -1;
|
2006-06-20 13:09:50 -07:00
|
|
|
retval = PTR_ERR(intf->usb_dev);
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
2019-08-12 16:11:07 -04:00
|
|
|
up_write(&minor_rwsem);
|
2005-04-16 15:20:36 -07:00
|
|
|
return retval;
|
|
|
|
|
}
|
2008-01-25 11:12:21 -06:00
|
|
|
EXPORT_SYMBOL_GPL(usb_register_dev);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* usb_deregister_dev - deregister a USB device's dynamic minor.
|
|
|
|
|
* @intf: pointer to the usb_interface that is being deregistered
|
|
|
|
|
* @class_driver: pointer to the usb_class_driver for this device
|
|
|
|
|
*
|
|
|
|
|
* Used in conjunction with usb_register_dev(). This function is called
|
|
|
|
|
* when the USB driver is finished with the minor numbers gotten from a
|
|
|
|
|
* call to usb_register_dev() (usually when the device is disconnected
|
|
|
|
|
* from the system.)
|
|
|
|
|
*
|
2005-06-20 21:15:16 -07:00
|
|
|
* This function also removes the usb class device from the sysfs tree.
|
|
|
|
|
*
|
2005-04-16 15:20:36 -07:00
|
|
|
* This should be called by all drivers that use the USB major number.
|
|
|
|
|
*/
|
|
|
|
|
void usb_deregister_dev(struct usb_interface *intf,
|
|
|
|
|
struct usb_class_driver *class_driver)
|
|
|
|
|
{
|
|
|
|
|
if (intf->minor == -1)
|
|
|
|
|
return;
|
|
|
|
|
|
2012-05-01 21:33:35 -07:00
|
|
|
dev_dbg(&intf->dev, "removing %d minor\n", intf->minor);
|
2023-06-22 00:25:14 +04:00
|
|
|
device_destroy(&usbmisc_class, MKDEV(USB_MAJOR, intf->minor));
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2007-05-22 11:46:41 -04:00
|
|
|
down_write(&minor_rwsem);
|
2005-04-16 15:20:36 -07:00
|
|
|
usb_minors[intf->minor] = NULL;
|
2007-05-22 11:46:41 -04:00
|
|
|
up_write(&minor_rwsem);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2006-06-20 13:09:50 -07:00
|
|
|
intf->usb_dev = NULL;
|
2005-04-16 15:20:36 -07:00
|
|
|
intf->minor = -1;
|
|
|
|
|
}
|
2008-01-25 11:12:21 -06:00
|
|
|
EXPORT_SYMBOL_GPL(usb_deregister_dev);
|