VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.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 spinlock_t minor_lock = SPIN_LOCK_UNLOCKED;
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 void release_usb_class_dev(struct class_device *class_dev)
70 {
71         dbg("%s - %s", __FUNCTION__, class_dev->class_id);
72         kfree(class_dev);
73 }
74
75 static struct class usb_class = {
76         .name           = "usb",
77         .release        = &release_usb_class_dev,
78 };
79
80 int usb_major_init(void)
81 {
82         int error;
83
84         error = register_chrdev(USB_MAJOR, "usb", &usb_fops);
85         if (error) {
86                 err("unable to get major %d for usb devices", USB_MAJOR);
87                 goto out;
88         }
89
90         error = class_register(&usb_class);
91         if (error) {
92                 err("class_register failed for usb devices");
93                 unregister_chrdev(USB_MAJOR, "usb");
94                 goto out;
95         }
96
97         devfs_mk_dir("usb");
98
99 out:
100         return error;
101 }
102
103 void usb_major_cleanup(void)
104 {
105         class_unregister(&usb_class);
106         devfs_remove("usb");
107         unregister_chrdev(USB_MAJOR, "usb");
108 }
109
110 static ssize_t show_dev(struct class_device *class_dev, char *buf)
111 {
112         int minor = (int)(long)class_get_devdata(class_dev);
113         return print_dev_t(buf, MKDEV(USB_MAJOR, minor));
114 }
115 static CLASS_DEVICE_ATTR(dev, S_IRUGO, show_dev, NULL);
116
117 /**
118  * usb_register_dev - register a USB device, and ask for a minor number
119  * @intf: pointer to the usb_interface that is being registered
120  * @class_driver: pointer to the usb_class_driver for this device
121  *
122  * This should be called by all USB drivers that use the USB major number.
123  * If CONFIG_USB_DYNAMIC_MINORS is enabled, the minor number will be
124  * dynamically allocated out of the list of available ones.  If it is not
125  * enabled, the minor number will be based on the next available free minor,
126  * starting at the class_driver->minor_base.
127  *
128  * This function also creates the devfs file for the usb device, if devfs
129  * is enabled, and creates a usb class device in the sysfs tree.
130  *
131  * usb_deregister_dev() must be called when the driver is done with
132  * the minor numbers given out by this function.
133  *
134  * Returns -EINVAL if something bad happens with trying to register a
135  * device, and 0 on success.
136  */
137 int usb_register_dev(struct usb_interface *intf,
138                      struct usb_class_driver *class_driver)
139 {
140         int retval = -EINVAL;
141         int minor_base = class_driver->minor_base;
142         int minor = 0;
143         char name[BUS_ID_SIZE];
144         struct class_device *class_dev;
145         char *temp;
146
147 #ifdef CONFIG_USB_DYNAMIC_MINORS
148         /* 
149          * We don't care what the device tries to start at, we want to start
150          * at zero to pack the devices into the smallest available space with
151          * no holes in the minor range.
152          */
153         minor_base = 0;
154 #endif
155         intf->minor = -1;
156
157         dbg ("looking for a minor, starting at %d", minor_base);
158
159         if (class_driver->fops == NULL)
160                 goto exit;
161
162         spin_lock (&minor_lock);
163         for (minor = minor_base; minor < MAX_USB_MINORS; ++minor) {
164                 if (usb_minors[minor])
165                         continue;
166
167                 usb_minors[minor] = class_driver->fops;
168
169                 retval = 0;
170                 break;
171         }
172         spin_unlock (&minor_lock);
173
174         if (retval)
175                 goto exit;
176
177         intf->minor = minor;
178
179         /* handle the devfs registration */
180         snprintf(name, BUS_ID_SIZE, class_driver->name, minor - minor_base);
181         devfs_mk_cdev(MKDEV(USB_MAJOR, minor), class_driver->mode, name);
182
183         /* create a usb class device for this usb interface */
184         class_dev = kmalloc(sizeof(*class_dev), GFP_KERNEL);
185         if (class_dev) {
186                 memset(class_dev, 0x00, sizeof(struct class_device));
187                 class_dev->class = &usb_class;
188                 class_dev->dev = &intf->dev;
189
190                 temp = strrchr(name, '/');
191                 if (temp && (temp[1] != 0x00))
192                         ++temp;
193                 else
194                         temp = name;
195                 snprintf(class_dev->class_id, BUS_ID_SIZE, "%s", temp);
196                 class_set_devdata(class_dev, (void *)(long)intf->minor);
197                 class_device_register(class_dev);
198                 class_device_create_file(class_dev, &class_device_attr_dev);
199                 intf->class_dev = class_dev;
200         }
201 exit:
202         return retval;
203 }
204 EXPORT_SYMBOL(usb_register_dev);
205
206 /**
207  * usb_deregister_dev - deregister a USB device's dynamic minor.
208  * @intf: pointer to the usb_interface that is being deregistered
209  * @class_driver: pointer to the usb_class_driver for this device
210  *
211  * Used in conjunction with usb_register_dev().  This function is called
212  * when the USB driver is finished with the minor numbers gotten from a
213  * call to usb_register_dev() (usually when the device is disconnected
214  * from the system.)
215  *
216  * This function also cleans up the devfs file for the usb device, if devfs
217  * is enabled, and removes the usb class device from the sysfs tree.
218  * 
219  * This should be called by all drivers that use the USB major number.
220  */
221 void usb_deregister_dev(struct usb_interface *intf,
222                         struct usb_class_driver *class_driver)
223 {
224         int minor_base = class_driver->minor_base;
225         char name[BUS_ID_SIZE];
226
227 #ifdef CONFIG_USB_DYNAMIC_MINORS
228         minor_base = 0;
229 #endif
230
231         if (intf->minor == -1)
232                 return;
233
234         dbg ("removing %d minor", intf->minor);
235
236         spin_lock (&minor_lock);
237         usb_minors[intf->minor] = NULL;
238         spin_unlock (&minor_lock);
239
240         snprintf(name, BUS_ID_SIZE, class_driver->name, intf->minor - minor_base);
241         devfs_remove (name);
242
243         if (intf->class_dev) {
244                 class_device_unregister(intf->class_dev);
245                 intf->class_dev = NULL;
246         }
247         intf->minor = -1;
248 }
249 EXPORT_SYMBOL(usb_deregister_dev);
250
251