4 * Copyright (C) 1991, 1992 Linus Torvalds
7 #include <linux/init.h>
9 #include <linux/slab.h>
10 #include <linux/string.h>
12 #include <linux/major.h>
13 #include <linux/errno.h>
14 #include <linux/module.h>
15 #include <linux/smp_lock.h>
16 #include <linux/seq_file.h>
18 #include <linux/kobject.h>
19 #include <linux/kobj_map.h>
20 #include <linux/cdev.h>
21 #include <linux/mutex.h>
24 #include <linux/kmod.h>
27 static struct kobj_map *cdev_map;
29 static DEFINE_MUTEX(chrdevs_lock);
31 static struct char_device_struct {
32 struct char_device_struct *next;
34 unsigned int baseminor;
37 struct file_operations *fops;
38 struct cdev *cdev; /* will die */
39 } *chrdevs[CHRDEV_MAJOR_HASH_SIZE];
41 /* index in the above */
42 static inline int major_to_index(int major)
44 return major % CHRDEV_MAJOR_HASH_SIZE;
49 void chrdev_show(struct seq_file *f, off_t offset)
51 struct char_device_struct *cd;
53 if (offset < CHRDEV_MAJOR_HASH_SIZE) {
54 mutex_lock(&chrdevs_lock);
55 for (cd = chrdevs[offset]; cd; cd = cd->next)
56 seq_printf(f, "%3d %s\n", cd->major, cd->name);
57 mutex_unlock(&chrdevs_lock);
61 #endif /* CONFIG_PROC_FS */
64 * Register a single major with a specified minor range.
66 * If major == 0 this functions will dynamically allocate a major and return
69 * If major > 0 this function will attempt to reserve the passed range of
70 * minors and will return zero on success.
72 * Returns a -ve errno on failure.
74 static struct char_device_struct *
75 __register_chrdev_region(unsigned int major, unsigned int baseminor,
76 int minorct, const char *name)
78 struct char_device_struct *cd, **cp;
82 cd = kzalloc(sizeof(struct char_device_struct), GFP_KERNEL);
84 return ERR_PTR(-ENOMEM);
86 mutex_lock(&chrdevs_lock);
90 for (i = ARRAY_SIZE(chrdevs)-1; i > 0; i--) {
91 if (chrdevs[i] == NULL)
104 cd->baseminor = baseminor;
105 cd->minorct = minorct;
106 strncpy(cd->name,name, 64);
108 i = major_to_index(major);
110 for (cp = &chrdevs[i]; *cp; cp = &(*cp)->next)
111 if ((*cp)->major > major ||
112 ((*cp)->major == major && (*cp)->baseminor >= baseminor))
114 if (*cp && (*cp)->major == major &&
115 (*cp)->baseminor < baseminor + minorct) {
121 mutex_unlock(&chrdevs_lock);
124 mutex_unlock(&chrdevs_lock);
129 static struct char_device_struct *
130 __unregister_chrdev_region(unsigned major, unsigned baseminor, int minorct)
132 struct char_device_struct *cd = NULL, **cp;
133 int i = major_to_index(major);
135 mutex_lock(&chrdevs_lock);
136 for (cp = &chrdevs[i]; *cp; cp = &(*cp)->next)
137 if ((*cp)->major == major &&
138 (*cp)->baseminor == baseminor &&
139 (*cp)->minorct == minorct)
145 mutex_unlock(&chrdevs_lock);
149 int register_chrdev_region(dev_t from, unsigned count, const char *name)
151 struct char_device_struct *cd;
152 dev_t to = from + count;
155 for (n = from; n < to; n = next) {
156 next = MKDEV(MAJOR(n)+1, 0);
159 cd = __register_chrdev_region(MAJOR(n), MINOR(n),
167 for (n = from; n < to; n = next) {
168 next = MKDEV(MAJOR(n)+1, 0);
169 kfree(__unregister_chrdev_region(MAJOR(n), MINOR(n), next - n));
174 int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count,
177 struct char_device_struct *cd;
178 cd = __register_chrdev_region(0, baseminor, count, name);
181 *dev = MKDEV(cd->major, cd->baseminor);
186 * register_chrdev() - Register a major number for character devices.
187 * @major: major device number or 0 for dynamic allocation
188 * @name: name of this range of devices
189 * @fops: file operations associated with this devices
191 * If @major == 0 this functions will dynamically allocate a major and return
194 * If @major > 0 this function will attempt to reserve a device with the given
195 * major number and will return zero on success.
197 * Returns a -ve errno on failure.
199 * The name of this device has nothing to do with the name of the device in
200 * /dev. It only helps to keep track of the different owners of devices. If
201 * your module name has only one type of devices it's ok to use e.g. the name
202 * of the module here.
204 * This function registers a range of 256 minor numbers. The first minor number
207 int register_chrdev(unsigned int major, const char *name,
208 const struct file_operations *fops)
210 struct char_device_struct *cd;
215 cd = __register_chrdev_region(major, 0, 256, name);
223 cdev->owner = fops->owner;
225 kobject_set_name(&cdev->kobj, "%s", name);
226 for (s = strchr(kobject_name(&cdev->kobj),'/'); s; s = strchr(s, '/'))
229 err = cdev_add(cdev, MKDEV(cd->major, 0), 256);
235 return major ? 0 : cd->major;
237 kobject_put(&cdev->kobj);
239 kfree(__unregister_chrdev_region(cd->major, 0, 256));
243 void unregister_chrdev_region(dev_t from, unsigned count)
245 dev_t to = from + count;
248 for (n = from; n < to; n = next) {
249 next = MKDEV(MAJOR(n)+1, 0);
252 kfree(__unregister_chrdev_region(MAJOR(n), MINOR(n), next - n));
256 int unregister_chrdev(unsigned int major, const char *name)
258 struct char_device_struct *cd;
259 cd = __unregister_chrdev_region(major, 0, 256);
266 static DEFINE_SPINLOCK(cdev_lock);
268 static struct kobject *cdev_get(struct cdev *p)
270 struct module *owner = p->owner;
271 struct kobject *kobj;
273 if (owner && !try_module_get(owner))
275 kobj = kobject_get(&p->kobj);
281 void cdev_put(struct cdev *p)
284 struct module *owner = p->owner;
285 kobject_put(&p->kobj);
291 * Called every time a character special file is opened
293 int chrdev_open(struct inode * inode, struct file * filp)
296 struct cdev *new = NULL;
299 spin_lock(&cdev_lock);
302 struct kobject *kobj;
304 spin_unlock(&cdev_lock);
305 kobj = kobj_lookup(cdev_map, inode->i_rdev, &idx);
308 new = container_of(kobj, struct cdev, kobj);
309 spin_lock(&cdev_lock);
312 inode->i_cdev = p = new;
313 inode->i_cindex = idx;
314 list_add(&inode->i_devices, &p->list);
316 } else if (!cdev_get(p))
318 } else if (!cdev_get(p))
320 spin_unlock(&cdev_lock);
324 filp->f_op = fops_get(p->ops);
329 if (filp->f_op->open) {
331 ret = filp->f_op->open(inode,filp);
339 void cd_forget(struct inode *inode)
341 spin_lock(&cdev_lock);
342 list_del_init(&inode->i_devices);
343 inode->i_cdev = NULL;
344 spin_unlock(&cdev_lock);
347 static void cdev_purge(struct cdev *cdev)
349 spin_lock(&cdev_lock);
350 while (!list_empty(&cdev->list)) {
352 inode = container_of(cdev->list.next, struct inode, i_devices);
353 list_del_init(&inode->i_devices);
354 inode->i_cdev = NULL;
356 spin_unlock(&cdev_lock);
360 * Dummy default file-operations: the only thing this does
361 * is contain the open that then fills in the correct operations
362 * depending on the special file...
364 const struct file_operations def_chr_fops = {
368 static struct kobject *exact_match(dev_t dev, int *part, void *data)
370 struct cdev *p = data;
374 static int exact_lock(dev_t dev, void *data)
376 struct cdev *p = data;
377 return cdev_get(p) ? 0 : -1;
380 int cdev_add(struct cdev *p, dev_t dev, unsigned count)
384 return kobj_map(cdev_map, dev, count, NULL, exact_match, exact_lock, p);
387 static void cdev_unmap(dev_t dev, unsigned count)
389 kobj_unmap(cdev_map, dev, count);
392 void cdev_del(struct cdev *p)
394 cdev_unmap(p->dev, p->count);
395 kobject_put(&p->kobj);
399 static void cdev_default_release(struct kobject *kobj)
401 struct cdev *p = container_of(kobj, struct cdev, kobj);
405 static void cdev_dynamic_release(struct kobject *kobj)
407 struct cdev *p = container_of(kobj, struct cdev, kobj);
412 static struct kobj_type ktype_cdev_default = {
413 .release = cdev_default_release,
416 static struct kobj_type ktype_cdev_dynamic = {
417 .release = cdev_dynamic_release,
420 struct cdev *cdev_alloc(void)
422 struct cdev *p = kzalloc(sizeof(struct cdev), GFP_KERNEL);
424 p->kobj.ktype = &ktype_cdev_dynamic;
425 INIT_LIST_HEAD(&p->list);
426 kobject_init(&p->kobj);
431 void cdev_init(struct cdev *cdev, const struct file_operations *fops)
433 memset(cdev, 0, sizeof *cdev);
434 INIT_LIST_HEAD(&cdev->list);
435 cdev->kobj.ktype = &ktype_cdev_default;
436 kobject_init(&cdev->kobj);
440 static struct kobject *base_probe(dev_t dev, int *part, void *data)
442 if (request_module("char-major-%d-%d", MAJOR(dev), MINOR(dev)) > 0)
443 /* Make old-style 2.4 aliases work */
444 request_module("char-major-%d", MAJOR(dev));
448 void __init chrdev_init(void)
450 cdev_map = kobj_map_init(base_probe, &chrdevs_lock);
454 /* Let modules do char dev stuff */
455 EXPORT_SYMBOL(register_chrdev_region);
456 EXPORT_SYMBOL(unregister_chrdev_region);
457 EXPORT_SYMBOL(alloc_chrdev_region);
458 EXPORT_SYMBOL(cdev_init);
459 EXPORT_SYMBOL(cdev_alloc);
460 EXPORT_SYMBOL(cdev_del);
461 EXPORT_SYMBOL(cdev_add);
462 EXPORT_SYMBOL(register_chrdev);
463 EXPORT_SYMBOL(unregister_chrdev);