4 * Copyright (C) 1991, 1992 Linus Torvalds
7 #include <linux/config.h>
8 #include <linux/init.h>
10 #include <linux/slab.h>
11 #include <linux/string.h>
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>
19 #include <linux/kobject.h>
20 #include <linux/kobj_map.h>
21 #include <linux/cdev.h>
24 #include <linux/kmod.h>
27 static struct kobj_map *cdev_map;
29 #define MAX_PROBE_HASH 255 /* random */
31 static DECLARE_MUTEX(chrdevs_lock);
33 static struct char_device_struct {
34 struct char_device_struct *next;
36 unsigned int baseminor;
39 struct file_operations *fops;
40 struct cdev *cdev; /* will die */
41 } *chrdevs[MAX_PROBE_HASH];
43 /* index in the above */
44 static inline int major_to_index(int major)
46 return major % MAX_PROBE_HASH;
51 struct char_device_struct *cd;
54 void *get_next_chrdev(void *dev)
56 struct chrdev_info *info;
59 info = kmalloc(sizeof(*info), GFP_KERNEL);
63 info->cd = chrdevs[info->index];
70 while (info->index < ARRAY_SIZE(chrdevs)) {
72 info->cd = info->cd->next;
76 * No devices on this chain, move to the next
79 info->cd = (info->index < ARRAY_SIZE(chrdevs)) ?
80 chrdevs[info->index] : NULL;
89 void *acquire_chrdev_list(void)
92 return get_next_chrdev(NULL);
95 void release_chrdev_list(void *dev)
102 int count_chrdev_list(void)
104 struct char_device_struct *cd;
109 for (i = 0; i < ARRAY_SIZE(chrdevs) ; i++) {
110 for (cd = chrdevs[i]; cd; cd = cd->next)
117 int get_chrdev_info(void *dev, int *major, char **name)
119 struct chrdev_info *info = dev;
121 if (info->cd == NULL)
124 *major = info->cd->major;
125 *name = info->cd->name;
130 * Register a single major with a specified minor range.
132 * If major == 0 this functions will dynamically allocate a major and return
135 * If major > 0 this function will attempt to reserve the passed range of
136 * minors and will return zero on success.
138 * Returns a -ve errno on failure.
140 static struct char_device_struct *
141 __register_chrdev_region(unsigned int major, unsigned int baseminor,
142 int minorct, const char *name)
144 struct char_device_struct *cd, **cp;
148 cd = kmalloc(sizeof(struct char_device_struct), GFP_KERNEL);
150 return ERR_PTR(-ENOMEM);
152 memset(cd, 0, sizeof(struct char_device_struct));
158 for (i = ARRAY_SIZE(chrdevs)-1; i > 0; i--) {
159 if (chrdevs[i] == NULL)
172 cd->baseminor = baseminor;
173 cd->minorct = minorct;
174 strncpy(cd->name,name, 64);
176 i = major_to_index(major);
178 for (cp = &chrdevs[i]; *cp; cp = &(*cp)->next)
179 if ((*cp)->major > major ||
180 ((*cp)->major == major && (*cp)->baseminor >= baseminor))
182 if (*cp && (*cp)->major == major &&
183 (*cp)->baseminor < baseminor + minorct) {
197 static struct char_device_struct *
198 __unregister_chrdev_region(unsigned major, unsigned baseminor, int minorct)
200 struct char_device_struct *cd = NULL, **cp;
201 int i = major_to_index(major);
204 for (cp = &chrdevs[i]; *cp; cp = &(*cp)->next)
205 if ((*cp)->major == major &&
206 (*cp)->baseminor == baseminor &&
207 (*cp)->minorct == minorct)
217 int register_chrdev_region(dev_t from, unsigned count, const char *name)
219 struct char_device_struct *cd;
220 dev_t to = from + count;
223 for (n = from; n < to; n = next) {
224 next = MKDEV(MAJOR(n)+1, 0);
227 cd = __register_chrdev_region(MAJOR(n), MINOR(n),
235 for (n = from; n < to; n = next) {
236 next = MKDEV(MAJOR(n)+1, 0);
237 kfree(__unregister_chrdev_region(MAJOR(n), MINOR(n), next - n));
242 int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count,
245 struct char_device_struct *cd;
246 cd = __register_chrdev_region(0, baseminor, count, name);
249 *dev = MKDEV(cd->major, cd->baseminor);
253 int register_chrdev(unsigned int major, const char *name,
254 struct file_operations *fops)
256 struct char_device_struct *cd;
261 cd = __register_chrdev_region(major, 0, 256, name);
269 cdev->owner = fops->owner;
271 kobject_set_name(&cdev->kobj, "%s", name);
272 for (s = strchr(kobject_name(&cdev->kobj),'/'); s; s = strchr(s, '/'))
275 err = cdev_add(cdev, MKDEV(cd->major, 0), 256);
281 return major ? 0 : cd->major;
283 kobject_put(&cdev->kobj);
285 kfree(__unregister_chrdev_region(cd->major, 0, 256));
289 void unregister_chrdev_region(dev_t from, unsigned count)
291 dev_t to = from + count;
294 for (n = from; n < to; n = next) {
295 next = MKDEV(MAJOR(n)+1, 0);
298 kfree(__unregister_chrdev_region(MAJOR(n), MINOR(n), next - n));
302 int unregister_chrdev(unsigned int major, const char *name)
304 struct char_device_struct *cd;
305 cd = __unregister_chrdev_region(major, 0, 256);
312 static DEFINE_SPINLOCK(cdev_lock);
314 static struct kobject *cdev_get(struct cdev *p)
316 struct module *owner = p->owner;
317 struct kobject *kobj;
319 if (owner && !try_module_get(owner))
321 kobj = kobject_get(&p->kobj);
327 void cdev_put(struct cdev *p)
330 struct module *owner = p->owner;
331 kobject_put(&p->kobj);
337 * Called every time a character special file is opened
339 int chrdev_open(struct inode * inode, struct file * filp)
342 struct cdev *new = NULL;
345 spin_lock(&cdev_lock);
348 struct kobject *kobj;
350 spin_unlock(&cdev_lock);
351 kobj = kobj_lookup(cdev_map, inode->i_rdev, &idx);
354 new = container_of(kobj, struct cdev, kobj);
355 spin_lock(&cdev_lock);
358 inode->i_cdev = p = new;
359 inode->i_cindex = idx;
360 list_add(&inode->i_devices, &p->list);
362 } else if (!cdev_get(p))
364 } else if (!cdev_get(p))
366 spin_unlock(&cdev_lock);
370 filp->f_op = fops_get(p->ops);
375 if (filp->f_op->open) {
377 ret = filp->f_op->open(inode,filp);
385 void cd_forget(struct inode *inode)
387 spin_lock(&cdev_lock);
388 list_del_init(&inode->i_devices);
389 inode->i_cdev = NULL;
390 spin_unlock(&cdev_lock);
393 static void cdev_purge(struct cdev *cdev)
395 spin_lock(&cdev_lock);
396 while (!list_empty(&cdev->list)) {
398 inode = container_of(cdev->list.next, struct inode, i_devices);
399 list_del_init(&inode->i_devices);
400 inode->i_cdev = NULL;
402 spin_unlock(&cdev_lock);
406 * Dummy default file-operations: the only thing this does
407 * is contain the open that then fills in the correct operations
408 * depending on the special file...
410 struct file_operations def_chr_fops = {
414 static struct kobject *exact_match(dev_t dev, int *part, void *data)
416 struct cdev *p = data;
420 static int exact_lock(dev_t dev, void *data)
422 struct cdev *p = data;
423 return cdev_get(p) ? 0 : -1;
426 int cdev_add(struct cdev *p, dev_t dev, unsigned count)
430 return kobj_map(cdev_map, dev, count, NULL, exact_match, exact_lock, p);
433 static void cdev_unmap(dev_t dev, unsigned count)
435 kobj_unmap(cdev_map, dev, count);
438 void cdev_del(struct cdev *p)
440 cdev_unmap(p->dev, p->count);
441 kobject_put(&p->kobj);
445 static void cdev_default_release(struct kobject *kobj)
447 struct cdev *p = container_of(kobj, struct cdev, kobj);
451 static void cdev_dynamic_release(struct kobject *kobj)
453 struct cdev *p = container_of(kobj, struct cdev, kobj);
458 static struct kobj_type ktype_cdev_default = {
459 .release = cdev_default_release,
462 static struct kobj_type ktype_cdev_dynamic = {
463 .release = cdev_dynamic_release,
466 struct cdev *cdev_alloc(void)
468 struct cdev *p = kmalloc(sizeof(struct cdev), GFP_KERNEL);
470 memset(p, 0, sizeof(struct cdev));
471 p->kobj.ktype = &ktype_cdev_dynamic;
472 INIT_LIST_HEAD(&p->list);
473 kobject_init(&p->kobj);
478 void cdev_init(struct cdev *cdev, struct file_operations *fops)
480 memset(cdev, 0, sizeof *cdev);
481 INIT_LIST_HEAD(&cdev->list);
482 cdev->kobj.ktype = &ktype_cdev_default;
483 kobject_init(&cdev->kobj);
487 static struct kobject *base_probe(dev_t dev, int *part, void *data)
489 if (request_module("char-major-%d-%d", MAJOR(dev), MINOR(dev)) > 0)
490 /* Make old-style 2.4 aliases work */
491 request_module("char-major-%d", MAJOR(dev));
495 void __init chrdev_init(void)
497 cdev_map = kobj_map_init(base_probe, &chrdevs_lock);
501 /* Let modules do char dev stuff */
502 EXPORT_SYMBOL(register_chrdev_region);
503 EXPORT_SYMBOL(unregister_chrdev_region);
504 EXPORT_SYMBOL(alloc_chrdev_region);
505 EXPORT_SYMBOL(cdev_init);
506 EXPORT_SYMBOL(cdev_alloc);
507 EXPORT_SYMBOL(cdev_del);
508 EXPORT_SYMBOL(cdev_add);
509 EXPORT_SYMBOL(register_chrdev);
510 EXPORT_SYMBOL(unregister_chrdev);