[PATCH] devfs: Remove the mode field from usb_class_driver as it's no longer needed
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / usb / core / file.c
blobe695308095ae8cbae7a9b131c93bd5a506b9c1dd
1 /*
2 * drivers/usb/file.c
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,
11 more docs, etc)
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/spinlock.h>
21 #include <linux/errno.h>
23 #ifdef CONFIG_USB_DEBUG
24 #define DEBUG
25 #else
26 #undef DEBUG
27 #endif
28 #include <linux/usb.h>
30 #include "usb.h"
32 #define MAX_USB_MINORS 256
33 static struct file_operations *usb_minors[MAX_USB_MINORS];
34 static DEFINE_SPINLOCK(minor_lock);
36 static int usb_open(struct inode * inode, struct file * file)
38 int minor = iminor(inode);
39 struct file_operations *c;
40 int err = -ENODEV;
41 struct file_operations *old_fops, *new_fops = NULL;
43 spin_lock (&minor_lock);
44 c = usb_minors[minor];
46 if (!c || !(new_fops = fops_get(c))) {
47 spin_unlock(&minor_lock);
48 return err;
50 spin_unlock(&minor_lock);
52 old_fops = file->f_op;
53 file->f_op = new_fops;
54 /* Curiouser and curiouser... NULL ->open() as "no device" ? */
55 if (file->f_op->open)
56 err = file->f_op->open(inode,file);
57 if (err) {
58 fops_put(file->f_op);
59 file->f_op = fops_get(old_fops);
61 fops_put(old_fops);
62 return err;
65 static struct file_operations usb_fops = {
66 .owner = THIS_MODULE,
67 .open = usb_open,
70 static struct class *usb_class;
72 int usb_major_init(void)
74 int error;
76 error = register_chrdev(USB_MAJOR, "usb", &usb_fops);
77 if (error) {
78 err("unable to get major %d for usb devices", USB_MAJOR);
79 goto out;
82 usb_class = class_create(THIS_MODULE, "usb");
83 if (IS_ERR(usb_class)) {
84 error = PTR_ERR(usb_class);
85 err("class_create failed for usb devices");
86 unregister_chrdev(USB_MAJOR, "usb");
87 goto out;
90 out:
91 return error;
94 void usb_major_cleanup(void)
96 class_destroy(usb_class);
97 unregister_chrdev(USB_MAJOR, "usb");
101 * usb_register_dev - register a USB device, and ask for a minor number
102 * @intf: pointer to the usb_interface that is being registered
103 * @class_driver: pointer to the usb_class_driver for this device
105 * This should be called by all USB drivers that use the USB major number.
106 * If CONFIG_USB_DYNAMIC_MINORS is enabled, the minor number will be
107 * dynamically allocated out of the list of available ones. If it is not
108 * enabled, the minor number will be based on the next available free minor,
109 * starting at the class_driver->minor_base.
111 * This function also creates a usb class device in the sysfs tree.
113 * usb_deregister_dev() must be called when the driver is done with
114 * the minor numbers given out by this function.
116 * Returns -EINVAL if something bad happens with trying to register a
117 * device, and 0 on success.
119 int usb_register_dev(struct usb_interface *intf,
120 struct usb_class_driver *class_driver)
122 int retval = -EINVAL;
123 int minor_base = class_driver->minor_base;
124 int minor = 0;
125 char name[BUS_ID_SIZE];
126 char *temp;
128 #ifdef CONFIG_USB_DYNAMIC_MINORS
130 * We don't care what the device tries to start at, we want to start
131 * at zero to pack the devices into the smallest available space with
132 * no holes in the minor range.
134 minor_base = 0;
135 #endif
136 intf->minor = -1;
138 dbg ("looking for a minor, starting at %d", minor_base);
140 if (class_driver->fops == NULL)
141 goto exit;
143 spin_lock (&minor_lock);
144 for (minor = minor_base; minor < MAX_USB_MINORS; ++minor) {
145 if (usb_minors[minor])
146 continue;
148 usb_minors[minor] = class_driver->fops;
150 retval = 0;
151 break;
153 spin_unlock (&minor_lock);
155 if (retval)
156 goto exit;
158 intf->minor = minor;
160 /* create a usb class device for this usb interface */
161 snprintf(name, BUS_ID_SIZE, class_driver->name, minor - minor_base);
162 temp = strrchr(name, '/');
163 if (temp && (temp[1] != 0x00))
164 ++temp;
165 else
166 temp = name;
167 intf->class_dev = class_device_create(usb_class, NULL,
168 MKDEV(USB_MAJOR, minor),
169 &intf->dev, "%s", temp);
170 if (IS_ERR(intf->class_dev)) {
171 spin_lock (&minor_lock);
172 usb_minors[intf->minor] = NULL;
173 spin_unlock (&minor_lock);
174 retval = PTR_ERR(intf->class_dev);
176 exit:
177 return retval;
179 EXPORT_SYMBOL(usb_register_dev);
182 * usb_deregister_dev - deregister a USB device's dynamic minor.
183 * @intf: pointer to the usb_interface that is being deregistered
184 * @class_driver: pointer to the usb_class_driver for this device
186 * Used in conjunction with usb_register_dev(). This function is called
187 * when the USB driver is finished with the minor numbers gotten from a
188 * call to usb_register_dev() (usually when the device is disconnected
189 * from the system.)
191 * This function also removes the usb class device from the sysfs tree.
193 * This should be called by all drivers that use the USB major number.
195 void usb_deregister_dev(struct usb_interface *intf,
196 struct usb_class_driver *class_driver)
198 int minor_base = class_driver->minor_base;
199 char name[BUS_ID_SIZE];
201 #ifdef CONFIG_USB_DYNAMIC_MINORS
202 minor_base = 0;
203 #endif
205 if (intf->minor == -1)
206 return;
208 dbg ("removing %d minor", intf->minor);
210 spin_lock (&minor_lock);
211 usb_minors[intf->minor] = NULL;
212 spin_unlock (&minor_lock);
214 snprintf(name, BUS_ID_SIZE, class_driver->name, intf->minor - minor_base);
215 class_device_destroy(usb_class, MKDEV(USB_MAJOR, intf->minor));
216 intf->class_dev = NULL;
217 intf->minor = -1;
219 EXPORT_SYMBOL(usb_deregister_dev);