1. 概述
总线(bus)是linux设备模型中抽象出来的一个概念,用于统一管理所有设备和驱动。在linux内核中,每个设备都会被挂载到相应的bus总线上,这个bus可以是对应真实的系统总线,如I2C总线,SPI总线等,也可以是虚拟总线,如platform bus。
在bus上,挂载有另外两个概念,设备(device)和驱动(device_driver)。
device即真实的物理设备,提供各种硬件资源和信息。device_driver提供用于读写访问device所需要的各种驱动方法。device和device_driver都通过注册挂载到bus上。
bus最主要的工作,就是维护device和device_driver的链表,并将挂载在bus上的device和device_driver进行匹配。当device或者device_driver注册到bus上时,都将调用bus的match函数,试图将注册的device和device_driver进行匹配。一旦匹配成功,意味着设备找到了相应的驱动,将调用probe函数,在probe中实现设备的各种初始化等工作。
2. 核心数据结构
/**
* struct bus_type - The bus type of the device
*
* @name: The name of the bus.
* @dev_name: Used for subsystems to enumerate devices like ("foo%u", dev->id).
* @dev_root: Default device to use as the parent.
* @dev_attrs: Default attributes of the devices on the bus.
* @bus_groups: Default attributes of the bus.
* @dev_groups: Default attributes of the devices on the bus.
* @drv_groups: Default attributes of the device drivers on the bus.
* @match: Called, perhaps multiple times, whenever a new device or driver
* is added for this bus. It should return a nonzero value if the
* given device can be handled by the given drive