Linux-2.6.12-rc2
[linux-flexiantxendom0-natty.git] / drivers / usb / core / file.c
1 /*
2  * drivers/usb/file.c
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,
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
15  *
16  */
17
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>
23
24 #ifdef CONFIG_USB_DEBUG
25         #define DEBUG
26 #else
27         #undef DEBUG
28 #endif
29 #include <linux/usb.h>
30
31 #define MAX_USB_MINORS  256
32 static struct file_operations *usb_minors[MAX_USB_MINORS];
33 static DEFINE_SPINLOCK(minor_lock);
34
35 static int usb_open(struct inode * inode, struct file * file)
36 {
37         int minor = iminor(inode);
38         struct file_operations *c;
39         int err = -ENODEV;
40         struct file_operations *old_fops, *new_fops = NULL;
41
42         spin_lock (&minor_lock);
43         c = usb_minors[minor];
44
45         if (!c || !(new_fops = fops_get(c))) {
46                 spin_unlock(&minor_lock);
47                 return err;
48         }
49         spin_unlock(&minor_lock);
50
51         old_fops = file->f_op;
52         file->f_op = new_fops;
53         /* Curiouser and curiouser... NULL ->open() as "no device" ? */
54         if (file->f_op->open)
55                 err = file->f_op->open(inode,file);
56         if (err) {
57                 fops_put(file->f_op);
58                 file->f_op = fops_get(old_fops);
59         }
60         fops_put(old_fops);
61         return err;
62 }
63
64 static struct file_operations usb_fops = {
65         .owner =        THIS_MODULE,
66         .open =         usb_open,
67 };
68
69 static struct class_simple *usb_class;
70
71 int usb_major_init(void)
72 {
73         int error;
74
75         error = register_chrdev(USB_MAJOR, "usb", &usb_fops);
76         if (error) {
77                 err("unable to get major %d for usb devices", USB_MAJOR);
78                 goto out;
79         }
80
81         usb_class = class_simple_create(THIS_MODULE, "usb");
82         if (IS_ERR(usb_class)) {
83                 err("class_simple_create failed for usb devices");
84                 unregister_chrdev(USB_MAJOR, "usb");
85                 goto out;
86         }
87
88         devfs_mk_dir("usb");
89
90 out:
91         return error;
92 }
93
94 void usb_major_cleanup(void)
95 {
96         class_simple_destroy(usb_class);
97         devfs_remove("usb");
98         unregister_chrdev(USB_MAJOR, "usb");
99 }
100
101 /**
102  * usb_register_dev - register a USB device, and ask for a minor number
103  * @intf: pointer to the usb_interface that is being registered
104  * @class_driver: pointer to the usb_class_driver for this device
105  *
106  * This should be called by all USB drivers that use the USB major number.
107  * If CONFIG_USB_DYNAMIC_MINORS is enabled, the minor number will be
108  * dynamically allocated out of the list of available ones.  If it is not
109  * enabled, the minor number will be based on the next available free minor,
110  * starting at the class_driver->minor_base.
111  *
112  * This function also creates the devfs file for the usb device, if devfs
113  * is enabled, and creates a usb class device in the sysfs tree.
114  *
115  * usb_deregister_dev() must be called when the driver is done with
116  * the minor numbers given out by this function.
117  *
118  * Returns -EINVAL if something bad happens with trying to register a
119  * device, and 0 on success.
120  */
121 int usb_register_dev(struct usb_interface *intf,
122                      struct usb_class_driver *class_driver)
123 {
124         int retval = -EINVAL;
125         int minor_base = class_driver->minor_base;
126         int minor = 0;
127         char name[BUS_ID_SIZE];
128         char *temp;
129
130 #ifdef CONFIG_USB_DYNAMIC_MINORS
131         /* 
132          * We don't care what the device tries to start at, we want to start
133          * at zero to pack the devices into the smallest available space with
134          * no holes in the minor range.
135          */
136         minor_base = 0;
137 #endif
138         intf->minor = -1;
139
140         dbg ("looking for a minor, starting at %d", minor_base);
141
142         if (class_driver->fops == NULL)
143                 goto exit;
144
145         spin_lock (&minor_lock);
146         for (minor = minor_base; minor < MAX_USB_MINORS; ++minor) {
147                 if (usb_minors[minor])
148                         continue;
149
150                 usb_minors[minor] = class_driver->fops;
151
152                 retval = 0;
153                 break;
154         }
155         spin_unlock (&minor_lock);
156
157         if (retval)
158                 goto exit;
159
160         intf->minor = minor;
161
162         /* handle the devfs registration */
163         snprintf(name, BUS_ID_SIZE, class_driver->name, minor - minor_base);
164         devfs_mk_cdev(MKDEV(USB_MAJOR, minor), class_driver->mode, name);
165
166         /* create a usb class device for this usb interface */
167         temp = strrchr(name, '/');
168         if (temp && (temp[1] != 0x00))
169                 ++temp;
170         else
171                 temp = name;
172         intf->class_dev = class_simple_device_add(usb_class, MKDEV(USB_MAJOR, minor), &intf->dev, "%s", temp);
173         if (IS_ERR(intf->class_dev)) {
174                 spin_lock (&minor_lock);
175                 usb_minors[intf->minor] = NULL;
176                 spin_unlock (&minor_lock);
177                 devfs_remove (name);
178                 retval = PTR_ERR(intf->class_dev);
179         }
180 exit:
181         return retval;
182 }
183 EXPORT_SYMBOL(usb_register_dev);
184
185 /**
186  * usb_deregister_dev - deregister a USB device's dynamic minor.
187  * @intf: pointer to the usb_interface that is being deregistered
188  * @class_driver: pointer to the usb_class_driver for this device
189  *
190  * Used in conjunction with usb_register_dev().  This function is called
191  * when the USB driver is finished with the minor numbers gotten from a
192  * call to usb_register_dev() (usually when the device is disconnected
193  * from the system.)
194  *
195  * This function also cleans up the devfs file for the usb device, if devfs
196  * is enabled, and removes the usb class device from the sysfs tree.
197  * 
198  * This should be called by all drivers that use the USB major number.
199  */
200 void usb_deregister_dev(struct usb_interface *intf,
201                         struct usb_class_driver *class_driver)
202 {
203         int minor_base = class_driver->minor_base;
204         char name[BUS_ID_SIZE];
205
206 #ifdef CONFIG_USB_DYNAMIC_MINORS
207         minor_base = 0;
208 #endif
209
210         if (intf->minor == -1)
211                 return;
212
213         dbg ("removing %d minor", intf->minor);
214
215         spin_lock (&minor_lock);
216         usb_minors[intf->minor] = NULL;
217         spin_unlock (&minor_lock);
218
219         snprintf(name, BUS_ID_SIZE, class_driver->name, intf->minor - minor_base);
220         devfs_remove (name);
221         class_simple_device_remove(MKDEV(USB_MAJOR, intf->minor));
222         intf->class_dev = NULL;
223         intf->minor = -1;
224 }
225 EXPORT_SYMBOL(usb_deregister_dev);
226
227