Back to home page

LXR

 
 

    


0001 /*
0002  *  linux/fs/char_dev.c
0003  *
0004  *  Copyright (C) 1991, 1992  Linus Torvalds
0005  */
0006 
0007 #include <linux/init.h>
0008 #include <linux/fs.h>
0009 #include <linux/kdev_t.h>
0010 #include <linux/slab.h>
0011 #include <linux/string.h>
0012 
0013 #include <linux/major.h>
0014 #include <linux/errno.h>
0015 #include <linux/module.h>
0016 #include <linux/seq_file.h>
0017 
0018 #include <linux/kobject.h>
0019 #include <linux/kobj_map.h>
0020 #include <linux/cdev.h>
0021 #include <linux/mutex.h>
0022 #include <linux/backing-dev.h>
0023 #include <linux/tty.h>
0024 
0025 #include "internal.h"
0026 
0027 static struct kobj_map *cdev_map;
0028 
0029 static DEFINE_MUTEX(chrdevs_lock);
0030 
0031 static struct char_device_struct {
0032     struct char_device_struct *next;
0033     unsigned int major;
0034     unsigned int baseminor;
0035     int minorct;
0036     char name[64];
0037     struct cdev *cdev;      /* will die */
0038 } *chrdevs[CHRDEV_MAJOR_HASH_SIZE];
0039 
0040 /* index in the above */
0041 static inline int major_to_index(unsigned major)
0042 {
0043     return major % CHRDEV_MAJOR_HASH_SIZE;
0044 }
0045 
0046 #ifdef CONFIG_PROC_FS
0047 
0048 void chrdev_show(struct seq_file *f, off_t offset)
0049 {
0050     struct char_device_struct *cd;
0051 
0052     if (offset < CHRDEV_MAJOR_HASH_SIZE) {
0053         mutex_lock(&chrdevs_lock);
0054         for (cd = chrdevs[offset]; cd; cd = cd->next)
0055             seq_printf(f, "%3d %s\n", cd->major, cd->name);
0056         mutex_unlock(&chrdevs_lock);
0057     }
0058 }
0059 
0060 #endif /* CONFIG_PROC_FS */
0061 
0062 /*
0063  * Register a single major with a specified minor range.
0064  *
0065  * If major == 0 this functions will dynamically allocate a major and return
0066  * its number.
0067  *
0068  * If major > 0 this function will attempt to reserve the passed range of
0069  * minors and will return zero on success.
0070  *
0071  * Returns a -ve errno on failure.
0072  */
0073 static struct char_device_struct *
0074 __register_chrdev_region(unsigned int major, unsigned int baseminor,
0075                int minorct, const char *name)
0076 {
0077     struct char_device_struct *cd, **cp;
0078     int ret = 0;
0079     int i;
0080 
0081     cd = kzalloc(sizeof(struct char_device_struct), GFP_KERNEL);
0082     if (cd == NULL)
0083         return ERR_PTR(-ENOMEM);
0084 
0085     mutex_lock(&chrdevs_lock);
0086 
0087     /* temporary */
0088     if (major == 0) {
0089         for (i = ARRAY_SIZE(chrdevs)-1; i > 0; i--) {
0090             if (chrdevs[i] == NULL)
0091                 break;
0092         }
0093 
0094         if (i < CHRDEV_MAJOR_DYN_END)
0095             pr_warn("CHRDEV \"%s\" major number %d goes below the dynamic allocation range\n",
0096                 name, i);
0097 
0098         if (i == 0) {
0099             ret = -EBUSY;
0100             goto out;
0101         }
0102         major = i;
0103     }
0104 
0105     cd->major = major;
0106     cd->baseminor = baseminor;
0107     cd->minorct = minorct;
0108     strlcpy(cd->name, name, sizeof(cd->name));
0109 
0110     i = major_to_index(major);
0111 
0112     for (cp = &chrdevs[i]; *cp; cp = &(*cp)->next)
0113         if ((*cp)->major > major ||
0114             ((*cp)->major == major &&
0115              (((*cp)->baseminor >= baseminor) ||
0116               ((*cp)->baseminor + (*cp)->minorct > baseminor))))
0117             break;
0118 
0119     /* Check for overlapping minor ranges.  */
0120     if (*cp && (*cp)->major == major) {
0121         int old_min = (*cp)->baseminor;
0122         int old_max = (*cp)->baseminor + (*cp)->minorct - 1;
0123         int new_min = baseminor;
0124         int new_max = baseminor + minorct - 1;
0125 
0126         /* New driver overlaps from the left.  */
0127         if (new_max >= old_min && new_max <= old_max) {
0128             ret = -EBUSY;
0129             goto out;
0130         }
0131 
0132         /* New driver overlaps from the right.  */
0133         if (new_min <= old_max && new_min >= old_min) {
0134             ret = -EBUSY;
0135             goto out;
0136         }
0137     }
0138 
0139     cd->next = *cp;
0140     *cp = cd;
0141     mutex_unlock(&chrdevs_lock);
0142     return cd;
0143 out:
0144     mutex_unlock(&chrdevs_lock);
0145     kfree(cd);
0146     return ERR_PTR(ret);
0147 }
0148 
0149 static struct char_device_struct *
0150 __unregister_chrdev_region(unsigned major, unsigned baseminor, int minorct)
0151 {
0152     struct char_device_struct *cd = NULL, **cp;
0153     int i = major_to_index(major);
0154 
0155     mutex_lock(&chrdevs_lock);
0156     for (cp = &chrdevs[i]; *cp; cp = &(*cp)->next)
0157         if ((*cp)->major == major &&
0158             (*cp)->baseminor == baseminor &&
0159             (*cp)->minorct == minorct)
0160             break;
0161     if (*cp) {
0162         cd = *cp;
0163         *cp = cd->next;
0164     }
0165     mutex_unlock(&chrdevs_lock);
0166     return cd;
0167 }
0168 
0169 /**
0170  * register_chrdev_region() - register a range of device numbers
0171  * @from: the first in the desired range of device numbers; must include
0172  *        the major number.
0173  * @count: the number of consecutive device numbers required
0174  * @name: the name of the device or driver.
0175  *
0176  * Return value is zero on success, a negative error code on failure.
0177  */
0178 int register_chrdev_region(dev_t from, unsigned count, const char *name)
0179 {
0180     struct char_device_struct *cd;
0181     dev_t to = from + count;
0182     dev_t n, next;
0183 
0184     for (n = from; n < to; n = next) {
0185         next = MKDEV(MAJOR(n)+1, 0);
0186         if (next > to)
0187             next = to;
0188         cd = __register_chrdev_region(MAJOR(n), MINOR(n),
0189                    next - n, name);
0190         if (IS_ERR(cd))
0191             goto fail;
0192     }
0193     return 0;
0194 fail:
0195     to = n;
0196     for (n = from; n < to; n = next) {
0197         next = MKDEV(MAJOR(n)+1, 0);
0198         kfree(__unregister_chrdev_region(MAJOR(n), MINOR(n), next - n));
0199     }
0200     return PTR_ERR(cd);
0201 }
0202 
0203 /**
0204  * alloc_chrdev_region() - register a range of char device numbers
0205  * @dev: output parameter for first assigned number
0206  * @baseminor: first of the requested range of minor numbers
0207  * @count: the number of minor numbers required
0208  * @name: the name of the associated device or driver
0209  *
0210  * Allocates a range of char device numbers.  The major number will be
0211  * chosen dynamically, and returned (along with the first minor number)
0212  * in @dev.  Returns zero or a negative error code.
0213  */
0214 int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count,
0215             const char *name)
0216 {
0217     struct char_device_struct *cd;
0218     cd = __register_chrdev_region(0, baseminor, count, name);
0219     if (IS_ERR(cd))
0220         return PTR_ERR(cd);
0221     *dev = MKDEV(cd->major, cd->baseminor);
0222     return 0;
0223 }
0224 
0225 /**
0226  * __register_chrdev() - create and register a cdev occupying a range of minors
0227  * @major: major device number or 0 for dynamic allocation
0228  * @baseminor: first of the requested range of minor numbers
0229  * @count: the number of minor numbers required
0230  * @name: name of this range of devices
0231  * @fops: file operations associated with this devices
0232  *
0233  * If @major == 0 this functions will dynamically allocate a major and return
0234  * its number.
0235  *
0236  * If @major > 0 this function will attempt to reserve a device with the given
0237  * major number and will return zero on success.
0238  *
0239  * Returns a -ve errno on failure.
0240  *
0241  * The name of this device has nothing to do with the name of the device in
0242  * /dev. It only helps to keep track of the different owners of devices. If
0243  * your module name has only one type of devices it's ok to use e.g. the name
0244  * of the module here.
0245  */
0246 int __register_chrdev(unsigned int major, unsigned int baseminor,
0247               unsigned int count, const char *name,
0248               const struct file_operations *fops)
0249 {
0250     struct char_device_struct *cd;
0251     struct cdev *cdev;
0252     int err = -ENOMEM;
0253 
0254     cd = __register_chrdev_region(major, baseminor, count, name);
0255     if (IS_ERR(cd))
0256         return PTR_ERR(cd);
0257 
0258     cdev = cdev_alloc();
0259     if (!cdev)
0260         goto out2;
0261 
0262     cdev->owner = fops->owner;
0263     cdev->ops = fops;
0264     kobject_set_name(&cdev->kobj, "%s", name);
0265 
0266     err = cdev_add(cdev, MKDEV(cd->major, baseminor), count);
0267     if (err)
0268         goto out;
0269 
0270     cd->cdev = cdev;
0271 
0272     return major ? 0 : cd->major;
0273 out:
0274     kobject_put(&cdev->kobj);
0275 out2:
0276     kfree(__unregister_chrdev_region(cd->major, baseminor, count));
0277     return err;
0278 }
0279 
0280 /**
0281  * unregister_chrdev_region() - unregister a range of device numbers
0282  * @from: the first in the range of numbers to unregister
0283  * @count: the number of device numbers to unregister
0284  *
0285  * This function will unregister a range of @count device numbers,
0286  * starting with @from.  The caller should normally be the one who
0287  * allocated those numbers in the first place...
0288  */
0289 void unregister_chrdev_region(dev_t from, unsigned count)
0290 {
0291     dev_t to = from + count;
0292     dev_t n, next;
0293 
0294     for (n = from; n < to; n = next) {
0295         next = MKDEV(MAJOR(n)+1, 0);
0296         if (next > to)
0297             next = to;
0298         kfree(__unregister_chrdev_region(MAJOR(n), MINOR(n), next - n));
0299     }
0300 }
0301 
0302 /**
0303  * __unregister_chrdev - unregister and destroy a cdev
0304  * @major: major device number
0305  * @baseminor: first of the range of minor numbers
0306  * @count: the number of minor numbers this cdev is occupying
0307  * @name: name of this range of devices
0308  *
0309  * Unregister and destroy the cdev occupying the region described by
0310  * @major, @baseminor and @count.  This function undoes what
0311  * __register_chrdev() did.
0312  */
0313 void __unregister_chrdev(unsigned int major, unsigned int baseminor,
0314              unsigned int count, const char *name)
0315 {
0316     struct char_device_struct *cd;
0317 
0318     cd = __unregister_chrdev_region(major, baseminor, count);
0319     if (cd && cd->cdev)
0320         cdev_del(cd->cdev);
0321     kfree(cd);
0322 }
0323 
0324 static DEFINE_SPINLOCK(cdev_lock);
0325 
0326 static struct kobject *cdev_get(struct cdev *p)
0327 {
0328     struct module *owner = p->owner;
0329     struct kobject *kobj;
0330 
0331     if (owner && !try_module_get(owner))
0332         return NULL;
0333     kobj = kobject_get(&p->kobj);
0334     if (!kobj)
0335         module_put(owner);
0336     return kobj;
0337 }
0338 
0339 void cdev_put(struct cdev *p)
0340 {
0341     if (p) {
0342         struct module *owner = p->owner;
0343         kobject_put(&p->kobj);
0344         module_put(owner);
0345     }
0346 }
0347 
0348 /*
0349  * Called every time a character special file is opened
0350  */
0351 static int chrdev_open(struct inode *inode, struct file *filp)
0352 {
0353     const struct file_operations *fops;
0354     struct cdev *p;
0355     struct cdev *new = NULL;
0356     int ret = 0;
0357 
0358     spin_lock(&cdev_lock);
0359     p = inode->i_cdev;
0360     if (!p) {
0361         struct kobject *kobj;
0362         int idx;
0363         spin_unlock(&cdev_lock);
0364         kobj = kobj_lookup(cdev_map, inode->i_rdev, &idx);
0365         if (!kobj)
0366             return -ENXIO;
0367         new = container_of(kobj, struct cdev, kobj);
0368         spin_lock(&cdev_lock);
0369         /* Check i_cdev again in case somebody beat us to it while
0370            we dropped the lock. */
0371         p = inode->i_cdev;
0372         if (!p) {
0373             inode->i_cdev = p = new;
0374             list_add(&inode->i_devices, &p->list);
0375             new = NULL;
0376         } else if (!cdev_get(p))
0377             ret = -ENXIO;
0378     } else if (!cdev_get(p))
0379         ret = -ENXIO;
0380     spin_unlock(&cdev_lock);
0381     cdev_put(new);
0382     if (ret)
0383         return ret;
0384 
0385     ret = -ENXIO;
0386     fops = fops_get(p->ops);
0387     if (!fops)
0388         goto out_cdev_put;
0389 
0390     replace_fops(filp, fops);
0391     if (filp->f_op->open) {
0392         ret = filp->f_op->open(inode, filp);
0393         if (ret)
0394             goto out_cdev_put;
0395     }
0396 
0397     return 0;
0398 
0399  out_cdev_put:
0400     cdev_put(p);
0401     return ret;
0402 }
0403 
0404 void cd_forget(struct inode *inode)
0405 {
0406     spin_lock(&cdev_lock);
0407     list_del_init(&inode->i_devices);
0408     inode->i_cdev = NULL;
0409     inode->i_mapping = &inode->i_data;
0410     spin_unlock(&cdev_lock);
0411 }
0412 
0413 static void cdev_purge(struct cdev *cdev)
0414 {
0415     spin_lock(&cdev_lock);
0416     while (!list_empty(&cdev->list)) {
0417         struct inode *inode;
0418         inode = container_of(cdev->list.next, struct inode, i_devices);
0419         list_del_init(&inode->i_devices);
0420         inode->i_cdev = NULL;
0421     }
0422     spin_unlock(&cdev_lock);
0423 }
0424 
0425 /*
0426  * Dummy default file-operations: the only thing this does
0427  * is contain the open that then fills in the correct operations
0428  * depending on the special file...
0429  */
0430 const struct file_operations def_chr_fops = {
0431     .open = chrdev_open,
0432     .llseek = noop_llseek,
0433 };
0434 
0435 static struct kobject *exact_match(dev_t dev, int *part, void *data)
0436 {
0437     struct cdev *p = data;
0438     return &p->kobj;
0439 }
0440 
0441 static int exact_lock(dev_t dev, void *data)
0442 {
0443     struct cdev *p = data;
0444     return cdev_get(p) ? 0 : -1;
0445 }
0446 
0447 /**
0448  * cdev_add() - add a char device to the system
0449  * @p: the cdev structure for the device
0450  * @dev: the first device number for which this device is responsible
0451  * @count: the number of consecutive minor numbers corresponding to this
0452  *         device
0453  *
0454  * cdev_add() adds the device represented by @p to the system, making it
0455  * live immediately.  A negative error code is returned on failure.
0456  */
0457 int cdev_add(struct cdev *p, dev_t dev, unsigned count)
0458 {
0459     int error;
0460 
0461     p->dev = dev;
0462     p->count = count;
0463 
0464     error = kobj_map(cdev_map, dev, count, NULL,
0465              exact_match, exact_lock, p);
0466     if (error)
0467         return error;
0468 
0469     kobject_get(p->kobj.parent);
0470 
0471     return 0;
0472 }
0473 
0474 static void cdev_unmap(dev_t dev, unsigned count)
0475 {
0476     kobj_unmap(cdev_map, dev, count);
0477 }
0478 
0479 /**
0480  * cdev_del() - remove a cdev from the system
0481  * @p: the cdev structure to be removed
0482  *
0483  * cdev_del() removes @p from the system, possibly freeing the structure
0484  * itself.
0485  */
0486 void cdev_del(struct cdev *p)
0487 {
0488     cdev_unmap(p->dev, p->count);
0489     kobject_put(&p->kobj);
0490 }
0491 
0492 
0493 static void cdev_default_release(struct kobject *kobj)
0494 {
0495     struct cdev *p = container_of(kobj, struct cdev, kobj);
0496     struct kobject *parent = kobj->parent;
0497 
0498     cdev_purge(p);
0499     kobject_put(parent);
0500 }
0501 
0502 static void cdev_dynamic_release(struct kobject *kobj)
0503 {
0504     struct cdev *p = container_of(kobj, struct cdev, kobj);
0505     struct kobject *parent = kobj->parent;
0506 
0507     cdev_purge(p);
0508     kfree(p);
0509     kobject_put(parent);
0510 }
0511 
0512 static struct kobj_type ktype_cdev_default = {
0513     .release    = cdev_default_release,
0514 };
0515 
0516 static struct kobj_type ktype_cdev_dynamic = {
0517     .release    = cdev_dynamic_release,
0518 };
0519 
0520 /**
0521  * cdev_alloc() - allocate a cdev structure
0522  *
0523  * Allocates and returns a cdev structure, or NULL on failure.
0524  */
0525 struct cdev *cdev_alloc(void)
0526 {
0527     struct cdev *p = kzalloc(sizeof(struct cdev), GFP_KERNEL);
0528     if (p) {
0529         INIT_LIST_HEAD(&p->list);
0530         kobject_init(&p->kobj, &ktype_cdev_dynamic);
0531     }
0532     return p;
0533 }
0534 
0535 /**
0536  * cdev_init() - initialize a cdev structure
0537  * @cdev: the structure to initialize
0538  * @fops: the file_operations for this device
0539  *
0540  * Initializes @cdev, remembering @fops, making it ready to add to the
0541  * system with cdev_add().
0542  */
0543 void cdev_init(struct cdev *cdev, const struct file_operations *fops)
0544 {
0545     memset(cdev, 0, sizeof *cdev);
0546     INIT_LIST_HEAD(&cdev->list);
0547     kobject_init(&cdev->kobj, &ktype_cdev_default);
0548     cdev->ops = fops;
0549 }
0550 
0551 static struct kobject *base_probe(dev_t dev, int *part, void *data)
0552 {
0553     if (request_module("char-major-%d-%d", MAJOR(dev), MINOR(dev)) > 0)
0554         /* Make old-style 2.4 aliases work */
0555         request_module("char-major-%d", MAJOR(dev));
0556     return NULL;
0557 }
0558 
0559 void __init chrdev_init(void)
0560 {
0561     cdev_map = kobj_map_init(base_probe, &chrdevs_lock);
0562 }
0563 
0564 
0565 /* Let modules do char dev stuff */
0566 EXPORT_SYMBOL(register_chrdev_region);
0567 EXPORT_SYMBOL(unregister_chrdev_region);
0568 EXPORT_SYMBOL(alloc_chrdev_region);
0569 EXPORT_SYMBOL(cdev_init);
0570 EXPORT_SYMBOL(cdev_alloc);
0571 EXPORT_SYMBOL(cdev_del);
0572 EXPORT_SYMBOL(cdev_add);
0573 EXPORT_SYMBOL(__register_chrdev);
0574 EXPORT_SYMBOL(__unregister_chrdev);