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