This commit was manufactured by cvs2svn to create tag
[linux-2.6.git] / fs / char_dev.c
1 /*
2  *  linux/fs/char_dev.c
3  *
4  *  Copyright (C) 1991, 1992  Linus Torvalds
5  */
6
7 #include <linux/config.h>
8 #include <linux/init.h>
9 #include <linux/fs.h>
10 #include <linux/slab.h>
11 #include <linux/string.h>
12
13 #include <linux/major.h>
14 #include <linux/errno.h>
15 #include <linux/module.h>
16 #include <linux/smp_lock.h>
17 #include <linux/devfs_fs_kernel.h>
18
19 #include <linux/kobject.h>
20 #include <linux/kobj_map.h>
21 #include <linux/cdev.h>
22
23 #ifdef CONFIG_KMOD
24 #include <linux/kmod.h>
25 #endif
26
27 static struct kobj_map *cdev_map;
28
29 #define MAX_PROBE_HASH 255      /* random */
30
31 static rwlock_t chrdevs_lock = RW_LOCK_UNLOCKED;
32
33 static struct char_device_struct {
34         struct char_device_struct *next;
35         unsigned int major;
36         unsigned int baseminor;
37         int minorct;
38         const char *name;
39         struct file_operations *fops;
40         struct cdev *cdev;              /* will die */
41 } *chrdevs[MAX_PROBE_HASH];
42
43 /* index in the above */
44 static inline int major_to_index(int major)
45 {
46         return major % MAX_PROBE_HASH;
47 }
48
49 /* get char device names in somewhat random order */
50 int get_chrdev_list(char *page)
51 {
52         struct char_device_struct *cd;
53         int i, len;
54
55         len = sprintf(page, "Character devices:\n");
56
57         read_lock(&chrdevs_lock);
58         for (i = 0; i < ARRAY_SIZE(chrdevs) ; i++) {
59                 for (cd = chrdevs[i]; cd; cd = cd->next)
60                         len += sprintf(page+len, "%3d %s\n",
61                                        cd->major, cd->name);
62         }
63         read_unlock(&chrdevs_lock);
64
65         return len;
66 }
67
68 /*
69  * Register a single major with a specified minor range.
70  *
71  * If major == 0 this functions will dynamically allocate a major and return
72  * its number.
73  *
74  * If major > 0 this function will attempt to reserve the passed range of
75  * minors and will return zero on success.
76  *
77  * Returns a -ve errno on failure.
78  */
79 static struct char_device_struct *
80 __register_chrdev_region(unsigned int major, unsigned int baseminor,
81                            int minorct, const char *name)
82 {
83         struct char_device_struct *cd, **cp;
84         int ret = 0;
85         int i;
86
87         cd = kmalloc(sizeof(struct char_device_struct), GFP_KERNEL);
88         if (cd == NULL)
89                 return ERR_PTR(-ENOMEM);
90
91         memset(cd, 0, sizeof(struct char_device_struct));
92
93         write_lock_irq(&chrdevs_lock);
94
95         /* temporary */
96         if (major == 0) {
97                 for (i = ARRAY_SIZE(chrdevs)-1; i > 0; i--) {
98                         if (chrdevs[i] == NULL)
99                                 break;
100                 }
101
102                 if (i == 0) {
103                         ret = -EBUSY;
104                         goto out;
105                 }
106                 major = i;
107                 ret = major;
108         }
109
110         cd->major = major;
111         cd->baseminor = baseminor;
112         cd->minorct = minorct;
113         cd->name = name;
114
115         i = major_to_index(major);
116
117         for (cp = &chrdevs[i]; *cp; cp = &(*cp)->next)
118                 if ((*cp)->major > major ||
119                     ((*cp)->major == major && (*cp)->baseminor >= baseminor))
120                         break;
121         if (*cp && (*cp)->major == major &&
122             (*cp)->baseminor < baseminor + minorct) {
123                 ret = -EBUSY;
124                 goto out;
125         }
126         cd->next = *cp;
127         *cp = cd;
128         write_unlock_irq(&chrdevs_lock);
129         return cd;
130 out:
131         write_unlock_irq(&chrdevs_lock);
132         kfree(cd);
133         return ERR_PTR(ret);
134 }
135
136 static struct char_device_struct *
137 __unregister_chrdev_region(unsigned major, unsigned baseminor, int minorct)
138 {
139         struct char_device_struct *cd = NULL, **cp;
140         int i = major_to_index(major);
141
142         write_lock_irq(&chrdevs_lock);
143         for (cp = &chrdevs[i]; *cp; cp = &(*cp)->next)
144                 if ((*cp)->major == major &&
145                     (*cp)->baseminor == baseminor &&
146                     (*cp)->minorct == minorct)
147                         break;
148         if (*cp) {
149                 cd = *cp;
150                 *cp = cd->next;
151         }
152         write_unlock_irq(&chrdevs_lock);
153         return cd;
154 }
155
156 int register_chrdev_region(dev_t from, unsigned count, char *name)
157 {
158         struct char_device_struct *cd;
159         dev_t to = from + count;
160         dev_t n, next;
161
162         for (n = from; n < to; n = next) {
163                 next = MKDEV(MAJOR(n)+1, 0);
164                 if (next > to)
165                         next = to;
166                 cd = __register_chrdev_region(MAJOR(n), MINOR(n),
167                                next - n, name);
168                 if (IS_ERR(cd))
169                         goto fail;
170         }
171         return 0;
172 fail:
173         to = n;
174         for (n = from; n < to; n = next) {
175                 next = MKDEV(MAJOR(n)+1, 0);
176                 kfree(__unregister_chrdev_region(MAJOR(n), MINOR(n), next - n));
177         }
178         return PTR_ERR(cd);
179 }
180
181 int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count, char *name)
182 {
183         struct char_device_struct *cd;
184         cd = __register_chrdev_region(0, baseminor, count, name);
185         if (IS_ERR(cd))
186                 return PTR_ERR(cd);
187         *dev = MKDEV(cd->major, cd->baseminor);
188         return 0;
189 }
190
191 int register_chrdev(unsigned int major, const char *name,
192                     struct file_operations *fops)
193 {
194         struct char_device_struct *cd;
195         struct cdev *cdev;
196         char *s;
197         int err = -ENOMEM;
198
199         cd = __register_chrdev_region(major, 0, 256, name);
200         if (IS_ERR(cd))
201                 return PTR_ERR(cd);
202         
203         cdev = cdev_alloc();
204         if (!cdev)
205                 goto out2;
206
207         cdev->owner = fops->owner;
208         cdev->ops = fops;
209         strcpy(cdev->kobj.name, name);
210         for (s = strchr(cdev->kobj.name, '/'); s; s = strchr(s, '/'))
211                 *s = '!';
212                 
213         err = cdev_add(cdev, MKDEV(cd->major, 0), 256);
214         if (err)
215                 goto out;
216
217         cd->cdev = cdev;
218
219         return major ? 0 : cd->major;
220 out:
221         kobject_put(&cdev->kobj);
222 out2:
223         kfree(__unregister_chrdev_region(cd->major, 0, 256));
224         return err;
225 }
226
227 void unregister_chrdev_region(dev_t from, unsigned count)
228 {
229         dev_t to = from + count;
230         dev_t n, next;
231
232         for (n = from; n < to; n = next) {
233                 next = MKDEV(MAJOR(n)+1, 0);
234                 if (next > to)
235                         next = to;
236                 kfree(__unregister_chrdev_region(MAJOR(n), MINOR(n), next - n));
237         }
238 }
239
240 int unregister_chrdev(unsigned int major, const char *name)
241 {
242         struct char_device_struct *cd;
243         cd = __unregister_chrdev_region(major, 0, 256);
244         if (cd && cd->cdev)
245                 cdev_del(cd->cdev);
246         kfree(cd);
247         return 0;
248 }
249
250 static spinlock_t cdev_lock = SPIN_LOCK_UNLOCKED;
251
252 static struct kobject *cdev_get(struct cdev *p)
253 {
254         struct module *owner = p->owner;
255         struct kobject *kobj;
256
257         if (owner && !try_module_get(owner))
258                 return NULL;
259         kobj = kobject_get(&p->kobj);
260         if (!kobj)
261                 module_put(owner);
262         return kobj;
263 }
264
265 void cdev_put(struct cdev *p)
266 {
267         if (p) {
268                 kobject_put(&p->kobj);
269                 module_put(p->owner);
270         }
271 }
272
273 /*
274  * Called every time a character special file is opened
275  */
276 int chrdev_open(struct inode * inode, struct file * filp)
277 {
278         struct cdev *p;
279         struct cdev *new = NULL;
280         int ret = 0;
281
282         spin_lock(&cdev_lock);
283         p = inode->i_cdev;
284         if (!p) {
285                 struct kobject *kobj;
286                 int idx;
287                 spin_unlock(&cdev_lock);
288                 kobj = kobj_lookup(cdev_map, inode->i_rdev, &idx);
289                 if (!kobj)
290                         return -ENXIO;
291                 new = container_of(kobj, struct cdev, kobj);
292                 spin_lock(&cdev_lock);
293                 p = inode->i_cdev;
294                 if (!p) {
295                         inode->i_cdev = p = new;
296                         inode->i_cindex = idx;
297                         list_add(&inode->i_devices, &p->list);
298                         new = NULL;
299                 } else if (!cdev_get(p))
300                         ret = -ENXIO;
301         } else if (!cdev_get(p))
302                 ret = -ENXIO;
303         spin_unlock(&cdev_lock);
304         cdev_put(new);
305         if (ret)
306                 return ret;
307         filp->f_op = fops_get(p->ops);
308         if (!filp->f_op) {
309                 cdev_put(p);
310                 return -ENXIO;
311         }
312         if (filp->f_op->open) {
313                 lock_kernel();
314                 ret = filp->f_op->open(inode,filp);
315                 unlock_kernel();
316         }
317         if (ret)
318                 cdev_put(p);
319         return ret;
320 }
321
322 void cd_forget(struct inode *inode)
323 {
324         spin_lock(&cdev_lock);
325         list_del_init(&inode->i_devices);
326         inode->i_cdev = NULL;
327         spin_unlock(&cdev_lock);
328 }
329
330 void cdev_purge(struct cdev *cdev)
331 {
332         spin_lock(&cdev_lock);
333         while (!list_empty(&cdev->list)) {
334                 struct inode *inode;
335                 inode = container_of(cdev->list.next, struct inode, i_devices);
336                 list_del_init(&inode->i_devices);
337                 inode->i_cdev = NULL;
338         }
339         spin_unlock(&cdev_lock);
340 }
341
342 /*
343  * Dummy default file-operations: the only thing this does
344  * is contain the open that then fills in the correct operations
345  * depending on the special file...
346  */
347 struct file_operations def_chr_fops = {
348         .open = chrdev_open,
349 };
350
351 static struct kobject *exact_match(dev_t dev, int *part, void *data)
352 {
353         struct cdev *p = data;
354         return &p->kobj;
355 }
356
357 static int exact_lock(dev_t dev, void *data)
358 {
359         struct cdev *p = data;
360         return cdev_get(p) ? 0 : -1;
361 }
362
363 int cdev_add(struct cdev *p, dev_t dev, unsigned count)
364 {
365         p->dev = dev;
366         p->count = count;
367         return kobj_map(cdev_map, dev, count, NULL, exact_match, exact_lock, p);
368 }
369
370 static void cdev_unmap(dev_t dev, unsigned count)
371 {
372         kobj_unmap(cdev_map, dev, count);
373 }
374
375 void cdev_del(struct cdev *p)
376 {
377         cdev_unmap(p->dev, p->count);
378         kobject_put(&p->kobj);
379 }
380
381
382 static decl_subsys(cdev, NULL, NULL);
383
384 static void cdev_default_release(struct kobject *kobj)
385 {
386         struct cdev *p = container_of(kobj, struct cdev, kobj);
387         cdev_purge(p);
388 }
389
390 static void cdev_dynamic_release(struct kobject *kobj)
391 {
392         struct cdev *p = container_of(kobj, struct cdev, kobj);
393         cdev_purge(p);
394         kfree(p);
395 }
396
397 static struct kobj_type ktype_cdev_default = {
398         .release        = cdev_default_release,
399 };
400
401 static struct kobj_type ktype_cdev_dynamic = {
402         .release        = cdev_dynamic_release,
403 };
404
405 struct cdev *cdev_alloc(void)
406 {
407         struct cdev *p = kmalloc(sizeof(struct cdev), GFP_KERNEL);
408         if (p) {
409                 memset(p, 0, sizeof(struct cdev));
410                 p->kobj.ktype = &ktype_cdev_dynamic;
411                 INIT_LIST_HEAD(&p->list);
412                 kobject_init(&p->kobj);
413         }
414         return p;
415 }
416
417 void cdev_init(struct cdev *cdev, struct file_operations *fops)
418 {
419         INIT_LIST_HEAD(&cdev->list);
420         cdev->kobj.ktype = &ktype_cdev_default;
421         kobject_init(&cdev->kobj);
422         cdev->ops = fops;
423 }
424
425 static struct kobject *base_probe(dev_t dev, int *part, void *data)
426 {
427         if (request_module("char-major-%d-%d", MAJOR(dev), MINOR(dev)) > 0)
428                 /* Make old-style 2.4 aliases work */
429                 request_module("char-major-%d", MAJOR(dev));
430         return NULL;
431 }
432
433 void __init chrdev_init(void)
434 {
435 /*
436  * Keep cdev_subsys around because (and only because) the kobj_map code
437  * depends on the rwsem it contains.  We don't make it public in sysfs,
438  * however.
439  */
440         subsystem_init(&cdev_subsys);
441         cdev_map = kobj_map_init(base_probe, &cdev_subsys);
442 }
443
444
445 /* Let modules do char dev stuff */
446 EXPORT_SYMBOL(register_chrdev_region);
447 EXPORT_SYMBOL(unregister_chrdev_region);
448 EXPORT_SYMBOL(alloc_chrdev_region);
449 EXPORT_SYMBOL(cdev_init);
450 EXPORT_SYMBOL(cdev_alloc);
451 EXPORT_SYMBOL(cdev_del);
452 EXPORT_SYMBOL(cdev_add);
453 EXPORT_SYMBOL(register_chrdev);
454 EXPORT_SYMBOL(unregister_chrdev);