Bus Types

Definition

  1. int bus_register(struct bus_type * bus);

Declaration

每一个内核的总线类型(PCI,USB),需要被定义静态 ,

  1. struct bus_type pci_bus_type = {
  2. .name = "pci",
  3. .match = pci_bus_match,
  4. };

然后在头文件中

  1. extern struct bus_type pci_bus_type;

Registration

当一个设备被初始化的时候,将会调用将调用bus_register

  1. include/linux/device.h:138:
  2. extern int __must_check bus_register(struct bus_type *bus);

这个函数将会初始化struct bus_type结构体。并将其插入总线类型的全局列表中。

回调

match(): Attaching Drivers to Devices

设备ID结构的格式以及用于比较它们的语义本质上是特定于总线的。驱动程序通常声明特定于总线的驱动程序结构中所支持的设备的设备ID数组。匹配回调的目的是使总线有机会通过比较驱动程序支持的设备ID与特定设备的设备ID来确定特定驱动程序是否支持特定设备,而不会牺牲特定于总线的功能或类型安全性。在总线上注册了驱动程序后,总线上的设备列表将被迭代,并为每个没有与之关联的驱动程序的设备调用match回调。

Device and Driver Lists

sysfs

根目录上的/sys文件夹,每条总线在总线目录中都有一个目录,

  1. [root@imx6ull:/sys/bus/usb]# ls
  2. devices drivers_autoprobe uevent
  3. drivers drivers_probe

每一个注册的设备会在对应的总线目录下的drivers中含有一个文件

  1. [root@imx6ull:/sys/bus/usb/drivers]# ls
  2. asix cdc_ncm pegasus usbhid
  3. ax88179_178a cdc_subset r8152 usblp
  4. bcm203x cdc_wdm rtl8150 usbserial

在该类型的总线上发现的每个设备都会在总线的设备目录中获得到物理层次结构中该设备目录的符号链接:

  1. [root@imx6ull:/sys/bus/usb/devices]# ls -al
  2. lrwxrwxrwx 1 root root 0 Jan 1 1970 1-0:1.0 -> ../../../devices/soc0/soc/2100000.aips-bus/2184200.usb/ci_hdrc.1/usb1/1-0:1.0
  3. lrwxrwxrwx 1 root root 0 Jan 1 1970 1-1 -> ../../../devices/soc0/soc/2100000.aips-bus/2184200.usb/ci_hdrc.1/usb1/1-1

Exporting Attributes

  1. struct bus_attribute {
  2. struct attribute attr;
  3. ssize_t (*show)(struct bus_type *, char * buf);
  4. ssize_t (*store)(struct bus_type *, const char * buf, size_t count);
  5. };

总线驱动程序可以使用BUS_ATTR_RW宏导出属性,该宏的作用类似于设备的DEVICE_ATTR_RW宏。例如,这样的定义:

  1. static BUS_ATTR_RW(debug);
  2. # 声明以后就可以添加删除属性了
  3. int bus_create_file(struct bus_type *, struct bus_attribute *);
  4. void bus_remove_file(struct bus_type *, struct bus_attribute *);