]>
git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - drivers/usb/core/file.c
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/spinlock.h>
21 #include <linux/errno.h>
23 #ifdef CONFIG_USB_DEBUG
28 #include <linux/usb.h>
32 #define MAX_USB_MINORS 256
33 static struct file_operations
*usb_minors
[MAX_USB_MINORS
];
34 static DEFINE_SPINLOCK(minor_lock
);
36 static int usb_open(struct inode
* inode
, struct file
* file
)
38 int minor
= iminor(inode
);
39 struct file_operations
*c
;
41 struct file_operations
*old_fops
, *new_fops
= NULL
;
43 spin_lock (&minor_lock
);
44 c
= usb_minors
[minor
];
46 if (!c
|| !(new_fops
= fops_get(c
))) {
47 spin_unlock(&minor_lock
);
50 spin_unlock(&minor_lock
);
52 old_fops
= file
->f_op
;
53 file
->f_op
= new_fops
;
54 /* Curiouser and curiouser... NULL ->open() as "no device" ? */
56 err
= file
->f_op
->open(inode
,file
);
59 file
->f_op
= fops_get(old_fops
);
65 static struct file_operations usb_fops
= {
70 static struct class *usb_class
;
72 int usb_major_init(void)
76 error
= register_chrdev(USB_MAJOR
, "usb", &usb_fops
);
78 err("unable to get major %d for usb devices", USB_MAJOR
);
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");
94 void usb_major_cleanup(void)
96 class_destroy(usb_class
);
97 unregister_chrdev(USB_MAJOR
, "usb");
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
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.
111 * This function also creates a usb class device in the sysfs tree.
113 * usb_deregister_dev() must be called when the driver is done with
114 * the minor numbers given out by this function.
116 * Returns -EINVAL if something bad happens with trying to register a
117 * device, and 0 on success.
119 int usb_register_dev(struct usb_interface
*intf
,
120 struct usb_class_driver
*class_driver
)
122 int retval
= -EINVAL
;
123 int minor_base
= class_driver
->minor_base
;
125 char name
[BUS_ID_SIZE
];
128 #ifdef CONFIG_USB_DYNAMIC_MINORS
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.
138 dbg ("looking for a minor, starting at %d", minor_base
);
140 if (class_driver
->fops
== NULL
)
143 spin_lock (&minor_lock
);
144 for (minor
= minor_base
; minor
< MAX_USB_MINORS
; ++minor
) {
145 if (usb_minors
[minor
])
148 usb_minors
[minor
] = class_driver
->fops
;
153 spin_unlock (&minor_lock
);
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))
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
);
179 EXPORT_SYMBOL(usb_register_dev
);
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
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
191 * This function also removes the usb class device from the sysfs tree.
193 * This should be called by all drivers that use the USB major number.
195 void usb_deregister_dev(struct usb_interface
*intf
,
196 struct usb_class_driver
*class_driver
)
198 int minor_base
= class_driver
->minor_base
;
199 char name
[BUS_ID_SIZE
];
201 #ifdef CONFIG_USB_DYNAMIC_MINORS
205 if (intf
->minor
== -1)
208 dbg ("removing %d minor", intf
->minor
);
210 spin_lock (&minor_lock
);
211 usb_minors
[intf
->minor
] = NULL
;
212 spin_unlock (&minor_lock
);
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
;
219 EXPORT_SYMBOL(usb_deregister_dev
);