[英]Why dev.bus is NULL in my device?
i'm trying to understand how linux device/driver model works and to do this i've written a little module. 我试图了解linux设备/驱动程序模型是如何工作的,为此,我编写了一个小模块。 This module is simple, retrieves a pointer to a struct net_device
(let's call it netdev) by the function dev_get_by_name(&init_net, "eth0")
. 这个模块很简单,通过函数dev_get_by_name(&init_net, "eth0")
检索指向struct net_device
的指针(我们将其称为dev_get_by_name(&init_net, "eth0")
。 Why the value of netdev->dev.bus
is NULL
? 为什么netdev->dev.bus
值为NULL
? Should that pointer represent the bus_type
structure on which my device is attached? 该指针是否应该代表我的设备所连接的bus_type
结构? The field netdev->parent->bus
is however not NULL
but it should represent the bus for eth controller...any explanation? 字段netdev->parent->bus
但是不是NULL
但是它应该代表eth控制器的总线...任何解释吗?
This is because your eth device, or better said its device "object" in the kernel, is not a bus and thus its bus value is left unitialized. 这是因为您的eth设备(或更确切地说是内核中的设备“对象”)不是总线,因此其总线值保持统一。 But its parent device usually is on a bus and it is sufficient that the parent device knows the bus it is on, since both device eventually are linked during the driver initialization. 但是其父设备通常在总线上,并且父设备知道它所在的总线就足够了,因为两个设备最终都在驱动程序初始化期间链接在一起。
Let's have a look at an example: here is what I have in sysfs for my eth0 device (notice the device field): 让我们看一个例子:这是我的eth0设备在sysfs中的内容(请注意设备字段):
$ ll /sys/class/net/eth0/
total 0
-r--r--r-- 1 root root 4096 May 20 11:10 address
-r--r--r-- 1 root root 4096 May 20 11:10 addr_len
-r--r--r-- 1 root root 4096 May 20 11:10 broadcast
-r--r--r-- 1 root root 4096 May 20 11:10 carrier
lrwxrwxrwx 1 root root 0 May 20 11:10 device -> ../../../devices/pci0000:00/0000:00:19.0
-r--r--r-- 1 root root 4096 May 20 11:10 dev_id
-r--r--r-- 1 root root 4096 May 20 11:10 dormant
-r--r--r-- 1 root root 4096 May 20 11:10 features
-rw-r--r-- 1 root root 4096 May 20 11:10 flags
-rw-r--r-- 1 root root 4096 May 20 11:10 ifalias
-r--r--r-- 1 root root 4096 May 20 11:10 ifindex
-r--r--r-- 1 root root 4096 May 20 11:10 iflink
-r--r--r-- 1 root root 4096 May 20 11:10 link_mode
-rw-r--r-- 1 root root 4096 May 20 11:10 mtu
-r--r--r-- 1 root root 4096 May 20 11:10 operstate
drwxr-xr-x 2 root root 0 May 20 11:10 power
drwxr-xr-x 2 root root 0 May 20 11:10 statistics
lrwxrwxrwx 1 root root 0 May 20 11:10 subsystem -> ../../net
-rw-r--r-- 1 root root 4096 May 20 11:10 tx_queue_len
-r--r--r-- 1 root root 4096 May 20 11:10 type
-rw-r--r-- 1 root root 4096 May 20 11:10 uevent
The link for the device is created from this code from the driver probe function, where netdev is the network device, and pdev the associated PCI device: 设备的链接是通过驱动程序探测功能的以下代码创建的,其中netdev是网络设备,而pdev是关联的PCI设备:
SET_NETDEV_DEV(netdev, &pdev->dev);
Which according to the documentation is: 根据文档,哪个是:
/* Set the sysfs physical device reference for the network logical device
* if set prior to registration will cause a symlink during initialization.
*/
#define SET_NETDEV_DEV(net, pdev) ((net)->dev.parent = (pdev))
And here is what I have in the corresponding PCI device, that was set by SET_NETDEV_DEV (where you can notice the bus field): 这是我在相应的PCI设备中所拥有的,这是由SET_NETDEV_DEV设置的(您可以在其中注意到总线字段):
$ ll /sys/devices/pci0000\:00/0000\:00\:19.0/
total 0
-rw-r--r-- 1 root root 4096 May 20 11:54 broken_parity_status
lrwxrwxrwx 1 root root 0 May 20 11:22 bus -> ../../../bus/pci
-r--r--r-- 1 root root 4096 May 20 11:07 class
-rw-r--r-- 1 root root 256 May 20 11:22 config
-r--r--r-- 1 root root 4096 May 20 11:54 device
lrwxrwxrwx 1 root root 0 May 20 11:22 driver -> ../../../bus/pci/drivers/e1000e
-rw------- 1 root root 4096 May 20 11:22 enable
-r--r--r-- 1 root root 4096 May 20 11:07 irq
-r--r--r-- 1 root root 4096 May 20 11:54 local_cpulist
-r--r--r-- 1 root root 4096 May 20 11:07 local_cpus
-r--r--r-- 1 root root 4096 May 20 11:22 modalias
-rw-r--r-- 1 root root 4096 May 20 11:22 msi_bus
lrwxrwxrwx 1 root root 0 May 20 11:22 net:eth0 -> ../../../class/net/eth0
drwxr-xr-x 2 root root 0 May 20 11:11 power
-r--r--r-- 1 root root 4096 May 20 11:22 resource
-rw------- 1 root root 131072 May 20 11:22 resource0
-rw------- 1 root root 4096 May 20 11:22 resource1
-rw------- 1 root root 32 May 20 11:22 resource2
lrwxrwxrwx 1 root root 0 May 20 11:22 subsystem -> ../../../bus/pci
-r--r--r-- 1 root root 4096 May 20 11:22 subsystem_device
-r--r--r-- 1 root root 4096 May 20 11:22 subsystem_vendor
-rw-r--r-- 1 root root 4096 May 20 11:22 uevent
-r--r--r-- 1 root root 4096 May 20 11:22 vendor
I hope this clarifies the situtation. 我希望这可以澄清这种情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.