我想知道这两个函数之间的区别:
int register_chrdev_region(dev_t first,unsigned int count,char *name);
int alloc_chrdev_region(dev_t *dev,unsigned int firstminor,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有一个相关但不是技术上重复的问题.