blob: e26bd5e773ada0685329ec8ed65084ff72d7242d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Uwe Zeisbergerf30c2262006-10-03 23:01:26 +02002 * drivers/usb/core/file.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * (C) Copyright Linus Torvalds 1999
5 * (C) Copyright Johannes Erdfelt 1999-2001
6 * (C) Copyright Andreas Gal 1999
7 * (C) Copyright Gregory P. Smith 1999
8 * (C) Copyright Deti Fliegl 1999 (new USB architecture)
9 * (C) Copyright Randy Dunlap 2000
10 * (C) Copyright David Brownell 2000-2001 (kernel hotplug, usb_device_id,
Matthias Beyer469271f2013-10-10 23:41:27 +020011 * more docs, etc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * (C) Copyright Yggdrasil Computing, Inc. 2000
13 * (usb_device_id matching changes by Adam J. Richter)
14 * (C) Copyright Greg Kroah-Hartman 2002-2003
15 *
Greg Kroah-Hartmanb65fba32016-10-28 17:16:36 -040016 * Released under the GPLv2 only.
17 * SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 */
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/errno.h>
Alan Sternd4ead162007-05-22 11:46:41 -040022#include <linux/rwsem.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Andy Shevchenkof8868ed2015-12-10 16:49:14 +020024#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/usb.h>
26
Greg KH6d5e8252005-04-18 17:39:24 -070027#include "usb.h"
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#define MAX_USB_MINORS 256
Arjan van de Ven99ac48f2006-03-28 01:56:41 -080030static const struct file_operations *usb_minors[MAX_USB_MINORS];
Alan Sternd4ead162007-05-22 11:46:41 -040031static DECLARE_RWSEM(minor_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Matthias Beyer1335f2d2013-10-10 23:41:28 +020033static int usb_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 int err = -ENODEV;
Al Viroe84f9e52013-09-22 14:17:15 -040036 const struct file_operations *new_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Alan Sternd4ead162007-05-22 11:46:41 -040038 down_read(&minor_rwsem);
Al Viroe84f9e52013-09-22 14:17:15 -040039 new_fops = fops_get(usb_minors[iminor(inode)]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Al Viroe84f9e52013-09-22 14:17:15 -040041 if (!new_fops)
Alan Sternd4ead162007-05-22 11:46:41 -040042 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Al Viroe84f9e52013-09-22 14:17:15 -040044 replace_fops(file, new_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 /* Curiouser and curiouser... NULL ->open() as "no device" ? */
46 if (file->f_op->open)
Matthias Beyer469271f2013-10-10 23:41:27 +020047 err = file->f_op->open(inode, file);
Alan Sternd4ead162007-05-22 11:46:41 -040048 done:
49 up_read(&minor_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 return err;
51}
52
Luiz Fernando N. Capitulino066202d2006-08-05 20:37:11 -030053static const struct file_operations usb_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 .owner = THIS_MODULE,
55 .open = usb_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +020056 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -070057};
58
Greg Kroah-Hartman43104f12006-06-20 15:14:07 -070059static struct usb_class {
60 struct kref kref;
61 struct class *class;
62} *usb_class;
63
Al Viro2c9ede52011-07-23 20:24:48 -040064static char *usb_devnode(struct device *dev, umode_t *mode)
Kay Sieversf7a386c2009-04-30 15:23:42 +020065{
66 struct usb_class_driver *drv;
67
68 drv = dev_get_drvdata(dev);
Kay Sieverse454cea2009-09-18 23:01:12 +020069 if (!drv || !drv->devnode)
Kay Sieversf7a386c2009-04-30 15:23:42 +020070 return NULL;
Kay Sieverse454cea2009-09-18 23:01:12 +020071 return drv->devnode(dev, mode);
Kay Sieversf7a386c2009-04-30 15:23:42 +020072}
73
Greg Kroah-Hartman43104f12006-06-20 15:14:07 -070074static int init_usb_class(void)
75{
76 int result = 0;
77
78 if (usb_class != NULL) {
79 kref_get(&usb_class->kref);
80 goto exit;
81 }
82
83 usb_class = kmalloc(sizeof(*usb_class), GFP_KERNEL);
84 if (!usb_class) {
85 result = -ENOMEM;
86 goto exit;
87 }
88
89 kref_init(&usb_class->kref);
Greg Kroah-Hartman7e972432012-04-25 17:22:37 -070090 usb_class->class = class_create(THIS_MODULE, "usbmisc");
Greg Kroah-Hartman43104f12006-06-20 15:14:07 -070091 if (IS_ERR(usb_class->class)) {
Alexey Khoroshilovde5535f2013-06-06 01:27:15 +040092 result = PTR_ERR(usb_class->class);
Greg Kroah-Hartman69a85942008-08-14 09:37:34 -070093 printk(KERN_ERR "class_create failed for usb devices\n");
Greg Kroah-Hartman43104f12006-06-20 15:14:07 -070094 kfree(usb_class);
95 usb_class = NULL;
Dan Carpentered7487c2009-11-10 11:02:08 +020096 goto exit;
Greg Kroah-Hartman43104f12006-06-20 15:14:07 -070097 }
Kay Sieverse454cea2009-09-18 23:01:12 +020098 usb_class->class->devnode = usb_devnode;
Greg Kroah-Hartman43104f12006-06-20 15:14:07 -070099
100exit:
101 return result;
102}
103
104static void release_usb_class(struct kref *kref)
105{
106 /* Ok, we cheat as we know we only have one usb_class */
107 class_destroy(usb_class->class);
108 kfree(usb_class);
109 usb_class = NULL;
110}
111
112static void destroy_usb_class(void)
113{
114 if (usb_class)
115 kref_put(&usb_class->kref, release_usb_class);
116}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118int usb_major_init(void)
119{
120 int error;
121
122 error = register_chrdev(USB_MAJOR, "usb", &usb_fops);
Greg Kroah-Hartman43104f12006-06-20 15:14:07 -0700123 if (error)
Greg Kroah-Hartman69a85942008-08-14 09:37:34 -0700124 printk(KERN_ERR "Unable to get major %d for usb devices\n",
125 USB_MAJOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 return error;
128}
129
130void usb_major_cleanup(void)
131{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 unregister_chrdev(USB_MAJOR, "usb");
133}
134
135/**
136 * usb_register_dev - register a USB device, and ask for a minor number
137 * @intf: pointer to the usb_interface that is being registered
138 * @class_driver: pointer to the usb_class_driver for this device
139 *
140 * This should be called by all USB drivers that use the USB major number.
141 * If CONFIG_USB_DYNAMIC_MINORS is enabled, the minor number will be
142 * dynamically allocated out of the list of available ones. If it is not
143 * enabled, the minor number will be based on the next available free minor,
144 * starting at the class_driver->minor_base.
145 *
Greg Kroah-Hartmand6e5bcf2005-06-20 21:15:16 -0700146 * This function also creates a usb class device in the sysfs tree.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 *
148 * usb_deregister_dev() must be called when the driver is done with
149 * the minor numbers given out by this function.
150 *
Yacine Belkadi626f0902013-08-02 20:10:04 +0200151 * Return: -EINVAL if something bad happens with trying to register a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 * device, and 0 on success.
153 */
154int usb_register_dev(struct usb_interface *intf,
155 struct usb_class_driver *class_driver)
156{
Alan Stern0026e002010-09-21 15:01:53 -0400157 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 int minor_base = class_driver->minor_base;
Alan Stern0026e002010-09-21 15:01:53 -0400159 int minor;
Kay Sievers7071a3c2008-05-02 06:02:41 +0200160 char name[20];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162#ifdef CONFIG_USB_DYNAMIC_MINORS
Matthias Beyer469271f2013-10-10 23:41:27 +0200163 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 * We don't care what the device tries to start at, we want to start
165 * at zero to pack the devices into the smallest available space with
166 * no holes in the minor range.
167 */
168 minor_base = 0;
169#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 if (class_driver->fops == NULL)
Alan Stern0026e002010-09-21 15:01:53 -0400172 return -EINVAL;
173 if (intf->minor >= 0)
174 return -EADDRINUSE;
175
176 retval = init_usb_class();
177 if (retval)
178 return retval;
179
Greg Kroah-Hartman079d4402012-05-01 21:33:35 -0700180 dev_dbg(&intf->dev, "looking for a minor, starting at %d\n", minor_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Alan Sternd4ead162007-05-22 11:46:41 -0400182 down_write(&minor_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 for (minor = minor_base; minor < MAX_USB_MINORS; ++minor) {
184 if (usb_minors[minor])
185 continue;
186
187 usb_minors[minor] = class_driver->fops;
Alan Stern0026e002010-09-21 15:01:53 -0400188 intf->minor = minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 break;
190 }
Alan Sternd4ead162007-05-22 11:46:41 -0400191 up_write(&minor_rwsem);
Alan Stern0026e002010-09-21 15:01:53 -0400192 if (intf->minor < 0)
193 return -EXFULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 /* create a usb class device for this usb interface */
Kay Sievers7071a3c2008-05-02 06:02:41 +0200196 snprintf(name, sizeof(name), class_driver->name, minor - minor_base);
Greg Kroah-Hartmanb0b090e2008-07-21 20:03:34 -0700197 intf->usb_dev = device_create(usb_class->class, &intf->dev,
Kay Sieversf7a386c2009-04-30 15:23:42 +0200198 MKDEV(USB_MAJOR, minor), class_driver,
Andy Shevchenkof8868ed2015-12-10 16:49:14 +0200199 "%s", kbasename(name));
Greg Kroah-Hartman0873c762006-06-20 13:09:50 -0700200 if (IS_ERR(intf->usb_dev)) {
Alan Sternd4ead162007-05-22 11:46:41 -0400201 down_write(&minor_rwsem);
Alan Stern0026e002010-09-21 15:01:53 -0400202 usb_minors[minor] = NULL;
203 intf->minor = -1;
Alan Sternd4ead162007-05-22 11:46:41 -0400204 up_write(&minor_rwsem);
Greg Kroah-Hartman0873c762006-06-20 13:09:50 -0700205 retval = PTR_ERR(intf->usb_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 return retval;
208}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600209EXPORT_SYMBOL_GPL(usb_register_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211/**
212 * usb_deregister_dev - deregister a USB device's dynamic minor.
213 * @intf: pointer to the usb_interface that is being deregistered
214 * @class_driver: pointer to the usb_class_driver for this device
215 *
216 * Used in conjunction with usb_register_dev(). This function is called
217 * when the USB driver is finished with the minor numbers gotten from a
218 * call to usb_register_dev() (usually when the device is disconnected
219 * from the system.)
220 *
Greg Kroah-Hartmand6e5bcf2005-06-20 21:15:16 -0700221 * This function also removes the usb class device from the sysfs tree.
222 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 * This should be called by all drivers that use the USB major number.
224 */
225void usb_deregister_dev(struct usb_interface *intf,
226 struct usb_class_driver *class_driver)
227{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 if (intf->minor == -1)
229 return;
230
Greg Kroah-Hartman079d4402012-05-01 21:33:35 -0700231 dev_dbg(&intf->dev, "removing %d minor\n", intf->minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Alan Sternd4ead162007-05-22 11:46:41 -0400233 down_write(&minor_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 usb_minors[intf->minor] = NULL;
Alan Sternd4ead162007-05-22 11:46:41 -0400235 up_write(&minor_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Greg Kroah-Hartman0873c762006-06-20 13:09:50 -0700237 device_destroy(usb_class->class, MKDEV(USB_MAJOR, intf->minor));
238 intf->usb_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 intf->minor = -1;
Greg Kroah-Hartman43104f12006-06-20 15:14:07 -0700240 destroy_usb_class();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600242EXPORT_SYMBOL_GPL(usb_deregister_dev);