Subversion Repositories shark

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
846 giacomo 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 <linuxcomp.h>
19
 
20
#include <linux/config.h>
21
#include <linux/module.h>
22
#include <linux/devfs_fs_kernel.h>
23
#include <linux/spinlock.h>
24
#include <linux/errno.h>
25
 
26
#ifdef CONFIG_USB_DEBUG
27
        #define DEBUG
28
#else
29
        #undef DEBUG
30
#endif
31
#include <linux/usb.h>
32
 
33
#define MAX_USB_MINORS  256
34
static struct file_operations *usb_minors[MAX_USB_MINORS];
35
static spinlock_t minor_lock = SPIN_LOCK_UNLOCKED;
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 void release_usb_class_dev(struct class_device *class_dev)
72
{
73
        dbg("%s - %s", __FUNCTION__, class_dev->class_id);
74
        kfree(class_dev);
75
}
76
 
77
static struct class usb_class = {
78
        .name           = "usb",
79
        .release        = &release_usb_class_dev,
80
};
81
 
82
int usb_major_init(void)
83
{
84
        if (register_chrdev(USB_MAJOR, "usb", &usb_fops)) {
85
                err("unable to get major %d for usb devices", USB_MAJOR);
86
                return -EBUSY;
87
        }
88
 
89
        devfs_mk_dir("usb");
90
        class_register(&usb_class);
91
        return 0;
92
}
93
 
94
void usb_major_cleanup(void)
95
{
96
        class_unregister(&usb_class);
97
        devfs_remove("usb");
98
        unregister_chrdev(USB_MAJOR, "usb");
99
}
100
 
101
static ssize_t show_dev(struct class_device *class_dev, char *buf)
102
{
103
        int minor = (int)(long)class_get_devdata(class_dev);
104
        return print_dev_t(buf, MKDEV(USB_MAJOR, minor));
105
}
106
static CLASS_DEVICE_ATTR(dev, S_IRUGO, show_dev, NULL);
107
 
108
/**
109
 * usb_register_dev - register a USB device, and ask for a minor number
110
 * @intf: pointer to the usb_interface that is being registered
111
 * @class_driver: pointer to the usb_class_driver for this device
112
 *
113
 * This should be called by all USB drivers that use the USB major number.
114
 * If CONFIG_USB_DYNAMIC_MINORS is enabled, the minor number will be
115
 * dynamically allocated out of the list of available ones.  If it is not
116
 * enabled, the minor number will be based on the next available free minor,
117
 * starting at the class_driver->minor_base.
118
 *
119
 * This function also creates the devfs file for the usb device, if devfs
120
 * is enabled, and creates a usb class device in the sysfs tree.
121
 *
122
 * usb_deregister_dev() must be called when the driver is done with
123
 * the minor numbers given out by this function.
124
 *
125
 * Returns -EINVAL if something bad happens with trying to register a
126
 * device, and 0 on success.
127
 */
128
int usb_register_dev(struct usb_interface *intf,
129
                     struct usb_class_driver *class_driver)
130
{
131
        int retval = -EINVAL;
132
        int minor_base = class_driver->minor_base;
133
        int minor = 0;
134
        char name[BUS_ID_SIZE];
135
        struct class_device *class_dev;
136
        char *temp;
137
 
138
#ifdef CONFIG_USB_DYNAMIC_MINORS
139
        /*
140
         * We don't care what the device tries to start at, we want to start
141
         * at zero to pack the devices into the smallest available space with
142
         * no holes in the minor range.
143
         */
144
        minor_base = 0;
145
#endif
146
        intf->minor = -1;
147
 
148
        dbg ("looking for a minor, starting at %d", minor_base);
149
 
150
        if (class_driver->fops == NULL)
151
                goto exit;
152
 
153
        spin_lock (&minor_lock);
154
        for (minor = minor_base; minor < MAX_USB_MINORS; ++minor) {
155
                if (usb_minors[minor])
156
                        continue;
157
 
158
                usb_minors[minor] = class_driver->fops;
159
 
160
                retval = 0;
161
                break;
162
        }
163
        spin_unlock (&minor_lock);
164
 
165
        if (retval)
166
                goto exit;
167
 
168
        intf->minor = minor;
169
 
170
        /* handle the devfs registration */
171
        snprintf26(name, BUS_ID_SIZE, class_driver->name, minor - minor_base);
172
        devfs_mk_cdev(MKDEV(USB_MAJOR, minor), class_driver->mode, name);
173
 
174
        /* create a usb class device for this usb interface */
175
        class_dev = kmalloc(sizeof(*class_dev), GFP_KERNEL);
176
        if (class_dev) {
177
                memset(class_dev, 0x00, sizeof(struct class_device));
178
                class_dev->class = &usb_class;
179
                class_dev->dev = &intf->dev;
180
 
181
                temp = strrchr(name, '/');
182
                if (temp && (temp[1] != 0x00))
183
                        ++temp;
184
                else
185
                        temp = name;
186
                snprintf26(class_dev->class_id, BUS_ID_SIZE, "%s", temp);
187
                class_set_devdata(class_dev, (void *)(long)intf->minor);
188
                class_device_register(class_dev);
189
                class_device_create_file(class_dev, &class_device_attr_dev);
190
                intf->class_dev = class_dev;
191
        }
192
exit:
193
        return retval;
194
}
195
EXPORT_SYMBOL(usb_register_dev);
196
 
197
/**
198
 * usb_deregister_dev - deregister a USB device's dynamic minor.
199
 * @intf: pointer to the usb_interface that is being deregistered
200
 * @class_driver: pointer to the usb_class_driver for this device
201
 *
202
 * Used in conjunction with usb_register_dev().  This function is called
203
 * when the USB driver is finished with the minor numbers gotten from a
204
 * call to usb_register_dev() (usually when the device is disconnected
205
 * from the system.)
206
 *
207
 * This function also cleans up the devfs file for the usb device, if devfs
208
 * is enabled, and removes the usb class device from the sysfs tree.
209
 *
210
 * This should be called by all drivers that use the USB major number.
211
 */
212
void usb_deregister_dev(struct usb_interface *intf,
213
                        struct usb_class_driver *class_driver)
214
{
215
        int minor_base = class_driver->minor_base;
216
        char name[BUS_ID_SIZE];
217
 
218
#ifdef CONFIG_USB_DYNAMIC_MINORS
219
        minor_base = 0;
220
#endif
221
 
222
        if (intf->minor == -1)
223
                return;
224
 
225
        dbg ("removing %d minor", intf->minor);
226
 
227
        spin_lock (&minor_lock);
228
        usb_minors[intf->minor] = NULL;
229
        spin_unlock (&minor_lock);
230
 
231
        snprintf26(name, BUS_ID_SIZE, class_driver->name, intf->minor - minor_base);
232
        devfs_remove (name);
233
 
234
        if (intf->class_dev) {
235
                class_device_unregister(intf->class_dev);
236
                intf->class_dev = NULL;
237
        }
238
        intf->minor = -1;
239
}
240
EXPORT_SYMBOL(usb_deregister_dev);
241
 
242