如何将/dev/中的设备与实际的驱动程序关联起来
我试图了解设备驱动程序在 Linux 中的工作原理。
我有一个设备节点如下(主设备号89,设备名称i2c-0)
<前><代码> crw-r--r-- 1 0 0 89, 0 九月 29 01:36 /dev/i2c-0我有名为
i2c.ko
的 i2c 驱动程序,我将在启动期间执行insmod i2c.ko
。在驱动程序中,初始化期间将调用以下函数:
register_chrdev(89, "i2c", &i2chtv_fops)<0 // 不是 "i2c-0"
我的问题是:当用户调用 open("/dev/i2c-0", O_RDWR)
时,如何内核知道要使用哪个驱动程序?我注意到设备名称是 i2c-0
,但注册的设备名称是 i2c
。是否因为它们使用相同的主设备号,内核可以使用正确的驱动程序?
I am trying to understand how device driver works in linux.
I have a device node as follows (Major number 89, device name i2c-0)
crw-r--r-- 1 0 0 89, 0 Sep 29 01:36 /dev/i2c-0
I have the i2c driver with the name
i2c.ko
and I will doinsmod i2c.ko
during start up.And in the driver , following function will be called during initialization:
register_chrdev(89, "i2c", &i2chtv_fops)<0 // not "i2c-0"
My question is : When user call open("/dev/i2c-0", O_RDWR)
,how the kernel knows which driver to use ? I noticed the device name is i2c-0
but registered device name is i2c
. Is it because they are use the same major number that the kernel can use the correct driver?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
主设备号告诉您哪个驱动程序处理哪个设备文件。次要编号仅由驱动程序本身用来区分它正在运行的设备,以防驱动程序处理多个设备。
将驱动程序添加到系统意味着将其注册到内核。这与在模块初始化期间为其分配主编号是同义的。您可以使用 linux/fs.h 定义的 register_chrdev 函数来完成此操作。
其中 unsigned int Major 是您要请求的主设备号,const char *name 是设备的名称,它将出现在 /proc/devices 中,struct file_operations *fops 是指向驱动程序的 file_operations 表的指针。返回值为负数表示注册失败。请注意,我们没有将次要编号传递给 register_chrdev。那是因为内核不关心次设备号;只有我们的司机使用它。
来自此处
The major number tells you which driver handles which device file. The minor number is used only by the driver itself to differentiate which device it's operating on, just in case the driver handles more than one device.
Adding a driver to your system means registering it with the kernel. This is synonymous with assigning it a major number during the module's initialization. You do this by using the register_chrdev function, defined by linux/fs.h.
where unsigned int major is the major number you want to request, const char *name is the name of the device as it'll appear in /proc/devices and struct file_operations *fops is a pointer to the file_operations table for your driver. A negative return value means the registertration failed. Note that we didn't pass the minor number to register_chrdev. That's because the kernel doesn't care about the minor number; only our driver uses it.
From here
是的,主数字选择驱动程序,次数字选择“单位”(无论是什么;对于控制台驱动程序,它是不同的屏幕)。
您看到的
-0
是“单元”(如果您的系统中有多个 i2c 总线)。Yes, major numbers select the driver and minor numbers select "units" (whatever that may be; for the console driver, it's the different screens).
The
-0
you see is the "unit" (in case you have more than a single i2c bus in your system).