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,
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
18 #include <linux/config.h>
19 #include <linux/module.h>
20 #include <linux/devfs_fs_kernel.h>
21 #include <linux/spinlock.h>
22 #include <linux/errno.h>
24 #ifdef CONFIG_USB_DEBUG
29 #include <linux/usb.h>
33 #define MAX_USB_MINORS 256
34 static struct file_operations *usb_minors[MAX_USB_MINORS];
35 static DEFINE_SPINLOCK(minor_lock);
37 static int usb_open(struct inode * inode, struct file * file)
39 int minor = iminor(inode);
40 struct file_operations *c;
42 struct file_operations *old_fops, *new_fops = NULL;
44 spin_lock (&minor_lock);
45 c = usb_minors[minor];
47 if (!c || !(new_fops = fops_get(c))) {
48 spin_unlock(&minor_lock);
51 spin_unlock(&minor_lock);
53 old_fops = file->f_op;
54 file->f_op = new_fops;
55 /* Curiouser and curiouser... NULL ->open() as "no device" ? */
57 err = file->f_op->open(inode,file);
60 file->f_op = fops_get(old_fops);
66 static struct file_operations usb_fops = {
71 static struct class *usb_class;
73 int usb_major_init(void)
77 error = register_chrdev(USB_MAJOR, "usb", &usb_fops);
79 err("unable to get major %d for usb devices", USB_MAJOR);
83 usb_class = class_create(THIS_MODULE, "usb");
84 if (IS_ERR(usb_class)) {
85 error = PTR_ERR(usb_class);
86 err("class_create failed for usb devices");
87 unregister_chrdev(USB_MAJOR, "usb");
97 void usb_major_cleanup(void)
99 class_destroy(usb_class);
101 unregister_chrdev(USB_MAJOR, "usb");
105 * usb_register_dev - register a USB device, and ask for a minor number
106 * @intf: pointer to the usb_interface that is being registered
107 * @class_driver: pointer to the usb_class_driver for this device
109 * This should be called by all USB drivers that use the USB major number.
110 * If CONFIG_USB_DYNAMIC_MINORS is enabled, the minor number will be
111 * dynamically allocated out of the list of available ones. If it is not
112 * enabled, the minor number will be based on the next available free minor,
113 * starting at the class_driver->minor_base.
115 * This function also creates the devfs file for the usb device, if devfs
116 * is enabled, and creates a usb class device in the sysfs tree.
118 * usb_deregister_dev() must be called when the driver is done with
119 * the minor numbers given out by this function.
121 * Returns -EINVAL if something bad happens with trying to register a
122 * device, and 0 on success.
124 int usb_register_dev(struct usb_interface *intf,
125 struct usb_class_driver *class_driver)
127 int retval = -EINVAL;
128 int minor_base = class_driver->minor_base;
130 char name[BUS_ID_SIZE];
133 #ifdef CONFIG_USB_DYNAMIC_MINORS
135 * We don't care what the device tries to start at, we want to start
136 * at zero to pack the devices into the smallest available space with
137 * no holes in the minor range.
143 dbg ("looking for a minor, starting at %d", minor_base);
145 if (class_driver->fops == NULL)
148 spin_lock (&minor_lock);
149 for (minor = minor_base; minor < MAX_USB_MINORS; ++minor) {
150 if (usb_minors[minor])
153 usb_minors[minor] = class_driver->fops;
158 spin_unlock (&minor_lock);
165 /* handle the devfs registration */
166 snprintf(name, BUS_ID_SIZE, class_driver->name, minor - minor_base);
167 devfs_mk_cdev(MKDEV(USB_MAJOR, minor), class_driver->mode, name);
169 /* create a usb class device for this usb interface */
170 temp = strrchr(name, '/');
171 if (temp && (temp[1] != 0x00))
175 intf->class_dev = class_device_create(usb_class, MKDEV(USB_MAJOR, minor), &intf->dev, "%s", temp);
176 if (IS_ERR(intf->class_dev)) {
177 spin_lock (&minor_lock);
178 usb_minors[intf->minor] = NULL;
179 spin_unlock (&minor_lock);
181 retval = PTR_ERR(intf->class_dev);
186 EXPORT_SYMBOL(usb_register_dev);
189 * usb_deregister_dev - deregister a USB device's dynamic minor.
190 * @intf: pointer to the usb_interface that is being deregistered
191 * @class_driver: pointer to the usb_class_driver for this device
193 * Used in conjunction with usb_register_dev(). This function is called
194 * when the USB driver is finished with the minor numbers gotten from a
195 * call to usb_register_dev() (usually when the device is disconnected
198 * This function also cleans up the devfs file for the usb device, if devfs
199 * is enabled, and removes the usb class device from the sysfs tree.
201 * This should be called by all drivers that use the USB major number.
203 void usb_deregister_dev(struct usb_interface *intf,
204 struct usb_class_driver *class_driver)
206 int minor_base = class_driver->minor_base;
207 char name[BUS_ID_SIZE];
209 #ifdef CONFIG_USB_DYNAMIC_MINORS
213 if (intf->minor == -1)
216 dbg ("removing %d minor", intf->minor);
218 spin_lock (&minor_lock);
219 usb_minors[intf->minor] = NULL;
220 spin_unlock (&minor_lock);
222 snprintf(name, BUS_ID_SIZE, class_driver->name, intf->minor - minor_base);
224 class_device_destroy(usb_class, MKDEV(USB_MAJOR, intf->minor));
225 intf->class_dev = NULL;
228 EXPORT_SYMBOL(usb_deregister_dev);