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