0

据我了解,的签名register_chrdev_region描述如下

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??

我没有count在函数中使用争论(alloc_chrdev_region同样)。请解释一个为驱动程序保留传染设备号的简单用例

参考http://www.makelinux.net/ldd3/chp-3-sect-2中的3.2.2

4

2 回答 2

3

Count 是分配给此主编号或此驱动程序的次编号的数量 在创建设备节点时,用户有责任创建具有允许的次编号的设备节点(它应该小于计数)

例如:如果我们创建第一个数字为 0 并计数为 2 的驱动程序,内核会为该驱动程序分配 0 到 2 个次要数字。但是用户仍然可以创建一个次要编号大于 2 的设备节点。如果次要编号大于 2,内核无法访问该设备节点的驱动程序,因为该次要编号未注册,因此无法打开该设备节点并抛出错误您想在该设备节点上执行文件操作。

于 2016-09-21T13:31:34.337 回答
2

评论说:

/**     
 * 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.
 */

您可以在 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;
}
于 2012-11-18T13:13:31.477 回答