blob: e695308095ae8cbae7a9b131c93bd5a506b9c1dd [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#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
Greg KH6d5e8252005-04-18 17:39:24 -070030#include "usb.h"
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#define MAX_USB_MINORS 256
33static struct file_operations *usb_minors[MAX_USB_MINORS];
34static DEFINE_SPINLOCK(minor_lock);
35
36static 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
65static struct file_operations usb_fops = {
66 .owner = THIS_MODULE,
67 .open = usb_open,
68};
69
gregkh@suse.de56b22932005-03-23 10:01:41 -080070static struct class *usb_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72int 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
gregkh@suse.de56b22932005-03-23 10:01:41 -080082 usb_class = class_create(THIS_MODULE, "usb");
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 if (IS_ERR(usb_class)) {
Mark M. Hoffman5cebfb72005-05-02 23:35:45 -040084 error = PTR_ERR(usb_class);
gregkh@suse.de56b22932005-03-23 10:01:41 -080085 err("class_create failed for usb devices");
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 unregister_chrdev(USB_MAJOR, "usb");
87 goto out;
88 }
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090out:
91 return error;
92}
93
94void usb_major_cleanup(void)
95{
gregkh@suse.de56b22932005-03-23 10:01:41 -080096 class_destroy(usb_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 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 *
Greg Kroah-Hartmand6e5bcf2005-06-20 21:15:16 -0700111 * This function also creates a usb class device in the sysfs tree.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 *
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 */
119int 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 /* create a usb class device for this usb interface */
Greg Kroah-Hartmand6e5bcf2005-06-20 21:15:16 -0700161 snprintf(name, BUS_ID_SIZE, class_driver->name, minor - minor_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 temp = strrchr(name, '/');
163 if (temp && (temp[1] != 0x00))
164 ++temp;
165 else
166 temp = name;
Greg Kroah-Hartman53f46542005-10-27 22:25:43 -0700167 intf->class_dev = class_device_create(usb_class, NULL,
168 MKDEV(USB_MAJOR, minor),
169 &intf->dev, "%s", temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 if (IS_ERR(intf->class_dev)) {
171 spin_lock (&minor_lock);
172 usb_minors[intf->minor] = NULL;
173 spin_unlock (&minor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 retval = PTR_ERR(intf->class_dev);
175 }
176exit:
177 return retval;
178}
179EXPORT_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 *
Greg Kroah-Hartmand6e5bcf2005-06-20 21:15:16 -0700191 * This function also removes the usb class device from the sysfs tree.
192 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 * This should be called by all drivers that use the USB major number.
194 */
195void 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);
gregkh@suse.de56b22932005-03-23 10:01:41 -0800215 class_device_destroy(usb_class, MKDEV(USB_MAJOR, intf->minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 intf->class_dev = NULL;
217 intf->minor = -1;
218}
219EXPORT_SYMBOL(usb_deregister_dev);
220
221