linux - register_chrdev_region 和 alloc_chrdev_region 分配设备号有什么区别?

标签 linux linux-kernel kernel device drivers

我想知道这两个函数的区别:

int register_chrdev_region(dev_t first, unsigned int count, char *name);

int alloc_chrdev_region(dev_t *dev, unsigned int firstminor, unsigned int count, char *name);

最佳答案

参见 here有关这两个功能的详细信息。

只有事先知道要从哪个主编号开始,注册才真正有用。通过注册,您告诉内核您想要什么设备号(开始的主要/次要编号和计数),它要么给你,要么不给你(取决于可用性)。

通过分配,您告诉内核您需要多少设备号(起始次设备号和计数),它会为您找到一个起始主设备号(如果可用的话)。

部分是为了避免与其他设备驱动程序发生冲突,最好使用分配函数,它会动态为您分配设备编号。

来自上面给出的链接:

Some major device numbers are statically assigned to the most common devices. A list of those devices can be found in Documentation/devices.txt within the kernel source tree. The chances of a static number having already been assigned for the use of your new driver are small, however, and new numbers are not being assigned. So, as a driver writer, you have a choice: you can simply pick a number that appears to be unused, or you can allocate major numbers in a dynamic manner.

Picking a number may work as long as the only user of your driver is you; once your driver is more widely deployed, a randomly picked major number will lead to conflicts and trouble.

Thus, for new drivers, we strongly suggest that you use dynamic allocation to obtain your major device number, rather than choosing a number randomly from the ones that are currently free. In other words, your drivers should almost certainly be using alloc_chrdev_region rather than register_chrdev_region.

The disadvantage of dynamic assignment is that you can't create the device nodes in advance, because the major number assigned to your module will vary. For normal use of the driver, this is hardly a problem, because once the number has been assigned, you can read it from /proc/devices.

有一个相关但技术上不重复的问题 here .

关于linux - register_chrdev_region 和 alloc_chrdev_region 分配设备号有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9835850/

相关文章:

linux - 删除包含数千个文件的大目录的最佳和最快方法是什么(在 ubuntu 中)

linux - 异常(exception)如下。 org.apache.flume.FlumeException : Unable to load source type in flume twitter analysis 异常

linux-kernel - register_wide_hw_breakpoint 持续触发处理程序回调

linux - 内核模式 clock_gettime()

linux - 如何更改页表条目以在 linux 中引发页面错误?

c++ - 客户端的socket编程

c - 我添加了 MAX7320 i2c 输出芯片。如何让内核为其加载驱动程序?

c - 多个/proc 条目的单个回调 - 找到调用者条目

Linux内核硬件断点

linux - 在 linux 启动时启动脚本