]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - drivers/usb/core/file.c
Merge with /pub/scm/linux/kernel/git/torvalds/linux-2.6.git
[mirror_ubuntu-zesty-kernel.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/spinlock.h>
21 #include <linux/errno.h>
22
23 #ifdef CONFIG_USB_DEBUG
24 #define DEBUG
25 #else
26 #undef DEBUG
27 #endif
28 #include <linux/usb.h>
29
30 #include "usb.h"
31
32 #define MAX_USB_MINORS 256
33 static struct file_operations *usb_minors[MAX_USB_MINORS];
34 static DEFINE_SPINLOCK(minor_lock);
35
36 static int usb_open(struct inode * inode, struct file * file)
37 {
38 int minor = iminor(inode);
39 struct file_operations *c;
40 int err = -ENODEV;
41 struct file_operations *old_fops, *new_fops = NULL;
42
43 spin_lock (&minor_lock);
44 c = usb_minors[minor];
45
46 if (!c || !(new_fops = fops_get(c))) {
47 spin_unlock(&minor_lock);
48 return err;
49 }
50 spin_unlock(&minor_lock);
51
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);
60 }
61 fops_put(old_fops);
62 return err;
63 }
64
65 static struct file_operations usb_fops = {
66 .owner = THIS_MODULE,
67 .open = usb_open,
68 };
69
70 static struct class *usb_class;
71
72 int usb_major_init(void)
73 {
74 int error;
75
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;
80 }
81
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;
88 }
89
90 out:
91 return error;
92 }
93
94 void usb_major_cleanup(void)
95 {
96 class_destroy(usb_class);
97 unregister_chrdev(USB_MAJOR, "usb");
98 }
99
100 /**
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
104 *
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.
110 *
111 * This function also creates a usb class device in the sysfs tree.
112 *
113 * usb_deregister_dev() must be called when the driver is done with
114 * the minor numbers given out by this function.
115 *
116 * Returns -EINVAL if something bad happens with trying to register a
117 * device, and 0 on success.
118 */
119 int usb_register_dev(struct usb_interface *intf,
120 struct usb_class_driver *class_driver)
121 {
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;
127
128 #ifdef CONFIG_USB_DYNAMIC_MINORS
129 /*
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.
133 */
134 minor_base = 0;
135 #endif
136 intf->minor = -1;
137
138 dbg ("looking for a minor, starting at %d", minor_base);
139
140 if (class_driver->fops == NULL)
141 goto exit;
142
143 spin_lock (&minor_lock);
144 for (minor = minor_base; minor < MAX_USB_MINORS; ++minor) {
145 if (usb_minors[minor])
146 continue;
147
148 usb_minors[minor] = class_driver->fops;
149
150 retval = 0;
151 break;
152 }
153 spin_unlock (&minor_lock);
154
155 if (retval)
156 goto exit;
157
158 intf->minor = minor;
159
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);
175 }
176 exit:
177 return retval;
178 }
179 EXPORT_SYMBOL(usb_register_dev);
180
181 /**
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
185 *
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.)
190 *
191 * This function also removes the usb class device from the sysfs tree.
192 *
193 * This should be called by all drivers that use the USB major number.
194 */
195 void usb_deregister_dev(struct usb_interface *intf,
196 struct usb_class_driver *class_driver)
197 {
198 int minor_base = class_driver->minor_base;
199 char name[BUS_ID_SIZE];
200
201 #ifdef CONFIG_USB_DYNAMIC_MINORS
202 minor_base = 0;
203 #endif
204
205 if (intf->minor == -1)
206 return;
207
208 dbg ("removing %d minor", intf->minor);
209
210 spin_lock (&minor_lock);
211 usb_minors[intf->minor] = NULL;
212 spin_unlock (&minor_lock);
213
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;
218 }
219 EXPORT_SYMBOL(usb_deregister_dev);
220
221