Search code examples
linux-kernellinux-device-driverkernel-moduledrivers

Use of count variable in register_chrdev_region


As of I understand, the signature of the register_chrdev_region is described as follows

extern int register_chrdev_region(dev_t firstmajor,unsigned int count,const char*dev_name);
//firstmajor: The major number requested for reservation of the dirver
//dev_name: Name of the device associated with the major number(for procfs and sysfs)
//count: Total number of contagious device numbes that is requested??

I don't get the usage of count arguement in the function (in alloc_chrdev_region as well). Please explain a simple use case of reserving contagious device numbers for the driver

Reference 3.2.2 in http://www.makelinux.net/ldd3/chp-3-sect-2


Solution

  • The comment said:

    /**     
     * alloc_chrdev_region() - register a range of char device numbers
     * @dev: output parameter for first assigned number
     * @baseminor: first of the requested range of minor numbers
     * @count: the number of minor numbers required
     * @name: the name of the associated device or driver
     *      
     * Allocates a range of char device numbers.  The major number will be
     * chosen dynamically, and returned (along with the first minor number)
     * in @dev.  Returns zero or a negative error code.
     */
    

    and you can find an example in fs/fuse/cuse.c:

    /* determine and reserve devt */
    devt = MKDEV(arg->dev_major, arg->dev_minor);
    if (!MAJOR(devt))
            rc = alloc_chrdev_region(&devt, MINOR(devt), 1, devinfo.name);
    else
            rc = register_chrdev_region(devt, 1, devinfo.name);
    if (rc) {
            printk(KERN_ERR "CUSE: failed to register chrdev region\n");
            goto err;
    }