Back to home page

OSCL-LXR

 
 

    


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