Merge branch 'work.misc-set_fs' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-block.git] / drivers / usb / core / file.c
CommitLineData
1da177e4 1/*
f30c2269 2 * drivers/usb/core/file.c
1da177e4
LT
3 *
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,
469271f8 11 * more docs, etc)
1da177e4
LT
12 * (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 *
b65fba3d
GKH
16 * Released under the GPLv2 only.
17 * SPDX-License-Identifier: GPL-2.0
1da177e4
LT
18 */
19
1da177e4 20#include <linux/module.h>
1da177e4 21#include <linux/errno.h>
d4ead16f 22#include <linux/rwsem.h>
5a0e3ad6 23#include <linux/slab.h>
f8868ed0 24#include <linux/string.h>
1da177e4
LT
25#include <linux/usb.h>
26
6d5e8254
GK
27#include "usb.h"
28
1da177e4 29#define MAX_USB_MINORS 256
99ac48f5 30static const struct file_operations *usb_minors[MAX_USB_MINORS];
d4ead16f 31static DECLARE_RWSEM(minor_rwsem);
2f86a96b 32static DEFINE_MUTEX(init_usb_class_mutex);
1da177e4 33
1335f2d2 34static int usb_open(struct inode *inode, struct file *file)
1da177e4 35{
1da177e4 36 int err = -ENODEV;
e84f9e57 37 const struct file_operations *new_fops;
1da177e4 38
d4ead16f 39 down_read(&minor_rwsem);
e84f9e57 40 new_fops = fops_get(usb_minors[iminor(inode)]);
1da177e4 41
e84f9e57 42 if (!new_fops)
d4ead16f 43 goto done;
1da177e4 44
e84f9e57 45 replace_fops(file, new_fops);
1da177e4
LT
46 /* Curiouser and curiouser... NULL ->open() as "no device" ? */
47 if (file->f_op->open)
469271f8 48 err = file->f_op->open(inode, file);
d4ead16f
AS
49 done:
50 up_read(&minor_rwsem);
1da177e4
LT
51 return err;
52}
53
066202dd 54static const struct file_operations usb_fops = {
1da177e4
LT
55 .owner = THIS_MODULE,
56 .open = usb_open,
6038f373 57 .llseek = noop_llseek,
1da177e4
LT
58};
59
43104f1d
GKH
60static struct usb_class {
61 struct kref kref;
62 struct class *class;
63} *usb_class;
1da177e4 64
2c9ede55 65static char *usb_devnode(struct device *dev, umode_t *mode)
f7a386c5
KS
66{
67 struct usb_class_driver *drv;
68
69 drv = dev_get_drvdata(dev);
e454cea2 70 if (!drv || !drv->devnode)
f7a386c5 71 return NULL;
e454cea2 72 return drv->devnode(dev, mode);
f7a386c5
KS
73}
74
43104f1d 75static int init_usb_class(void)
1da177e4 76{
43104f1d 77 int result = 0;
1da177e4 78
43104f1d
GKH
79 if (usb_class != NULL) {
80 kref_get(&usb_class->kref);
81 goto exit;
82 }
83
84 usb_class = kmalloc(sizeof(*usb_class), GFP_KERNEL);
85 if (!usb_class) {
86 result = -ENOMEM;
87 goto exit;
1da177e4
LT
88 }
89
43104f1d 90 kref_init(&usb_class->kref);
7e97243c 91 usb_class->class = class_create(THIS_MODULE, "usbmisc");
43104f1d 92 if (IS_ERR(usb_class->class)) {
de5535f5 93 result = PTR_ERR(usb_class->class);
69a85942 94 printk(KERN_ERR "class_create failed for usb devices\n");
43104f1d
GKH
95 kfree(usb_class);
96 usb_class = NULL;
ed7487c2 97 goto exit;
1da177e4 98 }
e454cea2 99 usb_class->class->devnode = usb_devnode;
1da177e4 100
43104f1d
GKH
101exit:
102 return result;
103}
104
105static void release_usb_class(struct kref *kref)
106{
107 /* Ok, we cheat as we know we only have one usb_class */
108 class_destroy(usb_class->class);
109 kfree(usb_class);
110 usb_class = NULL;
111}
112
113static void destroy_usb_class(void)
114{
2f86a96b
AK
115 mutex_lock(&init_usb_class_mutex);
116 kref_put(&usb_class->kref, release_usb_class);
117 mutex_unlock(&init_usb_class_mutex);
43104f1d
GKH
118}
119
120int usb_major_init(void)
121{
122 int error;
123
124 error = register_chrdev(USB_MAJOR, "usb", &usb_fops);
125 if (error)
69a85942
GKH
126 printk(KERN_ERR "Unable to get major %d for usb devices\n",
127 USB_MAJOR);
43104f1d 128
1da177e4
LT
129 return error;
130}
131
132void usb_major_cleanup(void)
133{
1da177e4
LT
134 unregister_chrdev(USB_MAJOR, "usb");
135}
136
137/**
138 * usb_register_dev - register a USB device, and ask for a minor number
139 * @intf: pointer to the usb_interface that is being registered
140 * @class_driver: pointer to the usb_class_driver for this device
141 *
142 * This should be called by all USB drivers that use the USB major number.
143 * If CONFIG_USB_DYNAMIC_MINORS is enabled, the minor number will be
144 * dynamically allocated out of the list of available ones. If it is not
145 * enabled, the minor number will be based on the next available free minor,
146 * starting at the class_driver->minor_base.
147 *
d6e5bcf4 148 * This function also creates a usb class device in the sysfs tree.
1da177e4
LT
149 *
150 * usb_deregister_dev() must be called when the driver is done with
151 * the minor numbers given out by this function.
152 *
626f090c 153 * Return: -EINVAL if something bad happens with trying to register a
1da177e4
LT
154 * device, and 0 on success.
155 */
156int usb_register_dev(struct usb_interface *intf,
157 struct usb_class_driver *class_driver)
158{
0026e005 159 int retval;
1da177e4 160 int minor_base = class_driver->minor_base;
0026e005 161 int minor;
7071a3ce 162 char name[20];
1da177e4
LT
163
164#ifdef CONFIG_USB_DYNAMIC_MINORS
469271f8 165 /*
1da177e4
LT
166 * We don't care what the device tries to start at, we want to start
167 * at zero to pack the devices into the smallest available space with
168 * no holes in the minor range.
169 */
170 minor_base = 0;
171#endif
1da177e4
LT
172
173 if (class_driver->fops == NULL)
0026e005
AS
174 return -EINVAL;
175 if (intf->minor >= 0)
176 return -EADDRINUSE;
177
2f86a96b 178 mutex_lock(&init_usb_class_mutex);
0026e005 179 retval = init_usb_class();
2f86a96b
AK
180 mutex_unlock(&init_usb_class_mutex);
181
0026e005
AS
182 if (retval)
183 return retval;
184
079d440e 185 dev_dbg(&intf->dev, "looking for a minor, starting at %d\n", minor_base);
1da177e4 186
d4ead16f 187 down_write(&minor_rwsem);
1da177e4
LT
188 for (minor = minor_base; minor < MAX_USB_MINORS; ++minor) {
189 if (usb_minors[minor])
190 continue;
191
192 usb_minors[minor] = class_driver->fops;
0026e005 193 intf->minor = minor;
1da177e4
LT
194 break;
195 }
d4ead16f 196 up_write(&minor_rwsem);
0026e005
AS
197 if (intf->minor < 0)
198 return -EXFULL;
1da177e4 199
1da177e4 200 /* create a usb class device for this usb interface */
7071a3ce 201 snprintf(name, sizeof(name), class_driver->name, minor - minor_base);
b0b090e5 202 intf->usb_dev = device_create(usb_class->class, &intf->dev,
f7a386c5 203 MKDEV(USB_MAJOR, minor), class_driver,
f8868ed0 204 "%s", kbasename(name));
0873c764 205 if (IS_ERR(intf->usb_dev)) {
d4ead16f 206 down_write(&minor_rwsem);
0026e005
AS
207 usb_minors[minor] = NULL;
208 intf->minor = -1;
d4ead16f 209 up_write(&minor_rwsem);
0873c764 210 retval = PTR_ERR(intf->usb_dev);
1da177e4 211 }
1da177e4
LT
212 return retval;
213}
782e70c6 214EXPORT_SYMBOL_GPL(usb_register_dev);
1da177e4
LT
215
216/**
217 * usb_deregister_dev - deregister a USB device's dynamic minor.
218 * @intf: pointer to the usb_interface that is being deregistered
219 * @class_driver: pointer to the usb_class_driver for this device
220 *
221 * Used in conjunction with usb_register_dev(). This function is called
222 * when the USB driver is finished with the minor numbers gotten from a
223 * call to usb_register_dev() (usually when the device is disconnected
224 * from the system.)
225 *
d6e5bcf4
GKH
226 * This function also removes the usb class device from the sysfs tree.
227 *
1da177e4
LT
228 * This should be called by all drivers that use the USB major number.
229 */
230void usb_deregister_dev(struct usb_interface *intf,
231 struct usb_class_driver *class_driver)
232{
1da177e4
LT
233 if (intf->minor == -1)
234 return;
235
079d440e 236 dev_dbg(&intf->dev, "removing %d minor\n", intf->minor);
1da177e4 237
d4ead16f 238 down_write(&minor_rwsem);
1da177e4 239 usb_minors[intf->minor] = NULL;
d4ead16f 240 up_write(&minor_rwsem);
1da177e4 241
0873c764
GKH
242 device_destroy(usb_class->class, MKDEV(USB_MAJOR, intf->minor));
243 intf->usb_dev = NULL;
1da177e4 244 intf->minor = -1;
43104f1d 245 destroy_usb_class();
1da177e4 246}
782e70c6 247EXPORT_SYMBOL_GPL(usb_deregister_dev);