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>
18 #include <linux/seq_file.h>
20 #include <linux/kobject.h>
21 #include <linux/kobj_map.h>
22 #include <linux/cdev.h>
23 #include <linux/mutex.h>
26 #include <linux/kmod.h>
29 static struct kobj_map *cdev_map;
31 static DEFINE_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[CHRDEV_MAJOR_HASH_SIZE];
43 /* index in the above */
44 static inline int major_to_index(int major)
46 return major % CHRDEV_MAJOR_HASH_SIZE;
51 void chrdev_show(struct seq_file *f, off_t offset)
53 struct char_device_struct *cd;
55 if (offset < CHRDEV_MAJOR_HASH_SIZE) {
56 mutex_lock(&chrdevs_lock);
57 for (cd = chrdevs[offset]; cd; cd = cd->next)
58 seq_printf(f, "%3d %s\n", cd->major, cd->name);
59 mutex_unlock(&chrdevs_lock);
63 #endif /* CONFIG_PROC_FS */
66 * Register a single major with a specified minor range.
68 * If major == 0 this functions will dynamically allocate a major and return
71 * If major > 0 this function will attempt to reserve the passed range of
72 * minors and will return zero on success.
74 * Returns a -ve errno on failure.
76 static struct char_device_struct *
77 __register_chrdev_region(unsigned int major, unsigned int baseminor,
78 int minorct, const char *name)
80 struct char_device_struct *cd, **cp;
84 cd = kzalloc(sizeof(struct char_device_struct), GFP_KERNEL);
86 return ERR_PTR(-ENOMEM);
88 mutex_lock(&chrdevs_lock);
92 for (i = ARRAY_SIZE(chrdevs)-1; i > 0; i--) {
93 if (chrdevs[i] == NULL)
106 cd->baseminor = baseminor;
107 cd->minorct = minorct;
108 strncpy(cd->name,name, 64);
110 i = major_to_index(major);
112 for (cp = &chrdevs[i]; *cp; cp = &(*cp)->next)
113 if ((*cp)->major > major ||
114 ((*cp)->major == major && (*cp)->baseminor >= baseminor))
116 if (*cp && (*cp)->major == major &&
117 (*cp)->baseminor < baseminor + minorct) {
123 mutex_unlock(&chrdevs_lock);
126 mutex_unlock(&chrdevs_lock);
131 static struct char_device_struct *
132 __unregister_chrdev_region(unsigned major, unsigned baseminor, int minorct)
134 struct char_device_struct *cd = NULL, **cp;
135 int i = major_to_index(major);
137 mutex_lock(&chrdevs_lock);
138 for (cp = &chrdevs[i]; *cp; cp = &(*cp)->next)
139 if ((*cp)->major == major &&
140 (*cp)->baseminor == baseminor &&
141 (*cp)->minorct == minorct)
147 mutex_unlock(&chrdevs_lock);
151 int register_chrdev_region(dev_t from, unsigned count, const char *name)
153 struct char_device_struct *cd;
154 dev_t to = from + count;
157 for (n = from; n < to; n = next) {
158 next = MKDEV(MAJOR(n)+1, 0);
161 cd = __register_chrdev_region(MAJOR(n), MINOR(n),
169 for (n = from; n < to; n = next) {
170 next = MKDEV(MAJOR(n)+1, 0);
171 kfree(__unregister_chrdev_region(MAJOR(n), MINOR(n), next - n));
176 int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count,
179 struct char_device_struct *cd;
180 cd = __register_chrdev_region(0, baseminor, count, name);
183 *dev = MKDEV(cd->major, cd->baseminor);
187 int register_chrdev(unsigned int major, const char *name,
188 const struct file_operations *fops)
190 struct char_device_struct *cd;
195 cd = __register_chrdev_region(major, 0, 256, name);
203 cdev->owner = fops->owner;
205 kobject_set_name(&cdev->kobj, "%s", name);
206 for (s = strchr(kobject_name(&cdev->kobj),'/'); s; s = strchr(s, '/'))
209 err = cdev_add(cdev, MKDEV(cd->major, 0), 256);
215 return major ? 0 : cd->major;
217 kobject_put(&cdev->kobj);
219 kfree(__unregister_chrdev_region(cd->major, 0, 256));
223 void unregister_chrdev_region(dev_t from, unsigned count)
225 dev_t to = from + count;
228 for (n = from; n < to; n = next) {
229 next = MKDEV(MAJOR(n)+1, 0);
232 kfree(__unregister_chrdev_region(MAJOR(n), MINOR(n), next - n));
236 int unregister_chrdev(unsigned int major, const char *name)
238 struct char_device_struct *cd;
239 cd = __unregister_chrdev_region(major, 0, 256);
246 static DEFINE_SPINLOCK(cdev_lock);
248 static struct kobject *cdev_get(struct cdev *p)
250 struct module *owner = p->owner;
251 struct kobject *kobj;
253 if (owner && !try_module_get(owner))
255 kobj = kobject_get(&p->kobj);
261 void cdev_put(struct cdev *p)
264 struct module *owner = p->owner;
265 kobject_put(&p->kobj);
271 * Called every time a character special file is opened
273 int chrdev_open(struct inode * inode, struct file * filp)
276 struct cdev *new = NULL;
279 spin_lock(&cdev_lock);
282 struct kobject *kobj;
284 spin_unlock(&cdev_lock);
285 kobj = kobj_lookup(cdev_map, inode->i_rdev, &idx);
288 new = container_of(kobj, struct cdev, kobj);
289 spin_lock(&cdev_lock);
292 inode->i_cdev = p = new;
293 inode->i_cindex = idx;
294 list_add(&inode->i_devices, &p->list);
296 } else if (!cdev_get(p))
298 } else if (!cdev_get(p))
300 spin_unlock(&cdev_lock);
304 filp->f_op = fops_get(p->ops);
309 if (filp->f_op->open) {
311 ret = filp->f_op->open(inode,filp);
319 void cd_forget(struct inode *inode)
321 spin_lock(&cdev_lock);
322 list_del_init(&inode->i_devices);
323 inode->i_cdev = NULL;
324 spin_unlock(&cdev_lock);
327 static void cdev_purge(struct cdev *cdev)
329 spin_lock(&cdev_lock);
330 while (!list_empty(&cdev->list)) {
332 inode = container_of(cdev->list.next, struct inode, i_devices);
333 list_del_init(&inode->i_devices);
334 inode->i_cdev = NULL;
336 spin_unlock(&cdev_lock);
340 * Dummy default file-operations: the only thing this does
341 * is contain the open that then fills in the correct operations
342 * depending on the special file...
344 const struct file_operations def_chr_fops = {
348 static struct kobject *exact_match(dev_t dev, int *part, void *data)
350 struct cdev *p = data;
354 static int exact_lock(dev_t dev, void *data)
356 struct cdev *p = data;
357 return cdev_get(p) ? 0 : -1;
360 int cdev_add(struct cdev *p, dev_t dev, unsigned count)
364 return kobj_map(cdev_map, dev, count, NULL, exact_match, exact_lock, p);
367 static void cdev_unmap(dev_t dev, unsigned count)
369 kobj_unmap(cdev_map, dev, count);
372 void cdev_del(struct cdev *p)
374 cdev_unmap(p->dev, p->count);
375 kobject_put(&p->kobj);
379 static void cdev_default_release(struct kobject *kobj)
381 struct cdev *p = container_of(kobj, struct cdev, kobj);
385 static void cdev_dynamic_release(struct kobject *kobj)
387 struct cdev *p = container_of(kobj, struct cdev, kobj);
392 static struct kobj_type ktype_cdev_default = {
393 .release = cdev_default_release,
396 static struct kobj_type ktype_cdev_dynamic = {
397 .release = cdev_dynamic_release,
400 struct cdev *cdev_alloc(void)
402 struct cdev *p = kzalloc(sizeof(struct cdev), GFP_KERNEL);
404 p->kobj.ktype = &ktype_cdev_dynamic;
405 INIT_LIST_HEAD(&p->list);
406 kobject_init(&p->kobj);
411 void cdev_init(struct cdev *cdev, const struct file_operations *fops)
413 memset(cdev, 0, sizeof *cdev);
414 INIT_LIST_HEAD(&cdev->list);
415 cdev->kobj.ktype = &ktype_cdev_default;
416 kobject_init(&cdev->kobj);
420 static struct kobject *base_probe(dev_t dev, int *part, void *data)
422 if (request_module("char-major-%d-%d", MAJOR(dev), MINOR(dev)) > 0)
423 /* Make old-style 2.4 aliases work */
424 request_module("char-major-%d", MAJOR(dev));
428 void __init chrdev_init(void)
430 cdev_map = kobj_map_init(base_probe, &chrdevs_lock);
434 /* Let modules do char dev stuff */
435 EXPORT_SYMBOL(register_chrdev_region);
436 EXPORT_SYMBOL(unregister_chrdev_region);
437 EXPORT_SYMBOL(alloc_chrdev_region);
438 EXPORT_SYMBOL(cdev_init);
439 EXPORT_SYMBOL(cdev_alloc);
440 EXPORT_SYMBOL(cdev_del);
441 EXPORT_SYMBOL(cdev_add);
442 EXPORT_SYMBOL(register_chrdev);
443 EXPORT_SYMBOL(unregister_chrdev);