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,
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
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>
24 #ifdef CONFIG_USB_DEBUG
29 #include <linux/usb.h>
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;
35 static int usb_open(struct inode * inode, struct file * file)
37 int minor = iminor(inode);
38 struct file_operations *c;
40 struct file_operations *old_fops, *new_fops = NULL;
42 spin_lock (&minor_lock);
43 c = usb_minors[minor];
45 if (!c || !(new_fops = fops_get(c))) {
46 spin_unlock(&minor_lock);
49 spin_unlock(&minor_lock);
51 old_fops = file->f_op;
52 file->f_op = new_fops;
53 /* Curiouser and curiouser... NULL ->open() as "no device" ? */
55 err = file->f_op->open(inode,file);
58 file->f_op = fops_get(old_fops);
64 static struct file_operations usb_fops = {
69 static void release_usb_class_dev(struct class_device *class_dev)
71 dbg("%s - %s", __FUNCTION__, class_dev->class_id);
75 static struct class usb_class = {
77 .release = &release_usb_class_dev,
80 int usb_major_init(void)
82 if (register_chrdev(USB_MAJOR, "usb", &usb_fops)) {
83 err("unable to get major %d for usb devices", USB_MAJOR);
88 class_register(&usb_class);
92 void usb_major_cleanup(void)
94 class_unregister(&usb_class);
96 unregister_chrdev(USB_MAJOR, "usb");
99 static ssize_t show_dev(struct class_device *class_dev, char *buf)
101 int minor = (int)(long)class_get_devdata(class_dev);
102 return print_dev_t(buf, MKDEV(USB_MAJOR, minor));
104 static CLASS_DEVICE_ATTR(dev, S_IRUGO, show_dev, NULL);
107 * usb_register_dev - register a USB device, and ask for a minor number
108 * @intf: pointer to the usb_interface that is being registered
109 * @class_driver: pointer to the usb_class_driver for this device
111 * This should be called by all USB drivers that use the USB major number.
112 * If CONFIG_USB_DYNAMIC_MINORS is enabled, the minor number will be
113 * dynamically allocated out of the list of available ones. If it is not
114 * enabled, the minor number will be based on the next available free minor,
115 * starting at the class_driver->minor_base.
117 * This function also creates the devfs file for the usb device, if devfs
118 * is enabled, and creates a usb class device in the sysfs tree.
120 * usb_deregister_dev() must be called when the driver is done with
121 * the minor numbers given out by this function.
123 * Returns -EINVAL if something bad happens with trying to register a
124 * device, and 0 on success.
126 int usb_register_dev(struct usb_interface *intf,
127 struct usb_class_driver *class_driver)
129 int retval = -EINVAL;
130 int minor_base = class_driver->minor_base;
132 char name[BUS_ID_SIZE];
133 struct class_device *class_dev;
136 #ifdef CONFIG_USB_DYNAMIC_MINORS
138 * We don't care what the device tries to start at, we want to start
139 * at zero to pack the devices into the smallest available space with
140 * no holes in the minor range.
146 dbg ("looking for a minor, starting at %d", minor_base);
148 if (class_driver->fops == NULL)
151 spin_lock (&minor_lock);
152 for (minor = minor_base; minor < MAX_USB_MINORS; ++minor) {
153 if (usb_minors[minor])
156 usb_minors[minor] = class_driver->fops;
161 spin_unlock (&minor_lock);
168 /* handle the devfs registration */
169 snprintf(name, BUS_ID_SIZE, class_driver->name, minor - minor_base);
170 devfs_mk_cdev(MKDEV(USB_MAJOR, minor), class_driver->mode, name);
172 /* create a usb class device for this usb interface */
173 class_dev = kmalloc(sizeof(*class_dev), GFP_KERNEL);
175 memset(class_dev, 0x00, sizeof(struct class_device));
176 class_dev->class = &usb_class;
177 class_dev->dev = &intf->dev;
179 temp = strrchr(name, '/');
180 if (temp && (temp[1] != 0x00))
184 snprintf(class_dev->class_id, BUS_ID_SIZE, "%s", temp);
185 class_set_devdata(class_dev, (void *)(long)intf->minor);
186 class_device_register(class_dev);
187 class_device_create_file(class_dev, &class_device_attr_dev);
188 intf->class_dev = class_dev;
193 EXPORT_SYMBOL(usb_register_dev);
196 * usb_deregister_dev - deregister a USB device's dynamic minor.
197 * @intf: pointer to the usb_interface that is being deregistered
198 * @class_driver: pointer to the usb_class_driver for this device
200 * Used in conjunction with usb_register_dev(). This function is called
201 * when the USB driver is finished with the minor numbers gotten from a
202 * call to usb_register_dev() (usually when the device is disconnected
205 * This function also cleans up the devfs file for the usb device, if devfs
206 * is enabled, and removes the usb class device from the sysfs tree.
208 * This should be called by all drivers that use the USB major number.
210 void usb_deregister_dev(struct usb_interface *intf,
211 struct usb_class_driver *class_driver)
213 int minor_base = class_driver->minor_base;
214 char name[BUS_ID_SIZE];
216 #ifdef CONFIG_USB_DYNAMIC_MINORS
220 if (intf->minor == -1)
223 dbg ("removing %d minor", intf->minor);
225 spin_lock (&minor_lock);
226 usb_minors[intf->minor] = NULL;
227 spin_unlock (&minor_lock);
229 snprintf(name, BUS_ID_SIZE, class_driver->name, intf->minor - minor_base);
232 if (intf->class_dev) {
233 class_device_unregister(intf->class_dev);
234 intf->class_dev = NULL;
238 EXPORT_SYMBOL(usb_deregister_dev);