1、
設備模型的上層建築由總線(bus) 、設備(device)、 驅動(device_driver)這3個數據結構構成,設備模型表示了它們之間的連接關系。
在設備模型中,所有的設備都通過總線連接。總線可以是物理存在的,也可以是虛擬的。比如內部的platform總線。
設備是連接到某條物理或虛擬總線上的對象。可能是真正的物理對象,也可能的是虛擬對象。
驅動是用來和設備通信的軟件程序。驅動可以從設備獲得數據,也可以把相應數據發給設備進行處理。
2、數據結構
(1)、總線
struct bus_type {
const char *name;總線類型的名稱
struct bus_attribute*bus_attrs;
struct device_attribute*dev_attrs;
struct driver_attribute*drv_attrs;
int (*match)(struct device *dev, struct device_driver *drv);設備和驅動能否對應,就是有該總線的match方式決定。不同總線的match方式不一樣。
int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
int (*probe)(struct device *dev);
int (*remove)(struct device *dev);
void (*shutdown)(struct device *dev);
int (*suspend)(struct device *dev, pm_message_t state);
int (*suspend_late)(struct device *dev, pm_message_t state);
int (*resume_early)(struct device *dev);
int (*resume)(struct device *dev);
struct pm_ext_ops *pm;
struct bus_type_private *p;
};
現在如上數據結構和書中講的有所不同,只不過有包裝了一層數據結構struct bus_type_private *p,源碼如下:
/**
* struct bus_type_private - structure to hold the private to the driver core portions of the bus_type structure.
*
* @subsys - the struct kset that defines this bus. This is the main kobject
subsys描述該總線的子系統,它連接到全局量kset bus_subsys中。
* @drivers_kset - the list of drivers associated with this bus
該總線系統裡所有驅動的集合
* @devices_kset - the list of devices associated with this bus
該總線系統裡所有設備的集合
* @klist_devices - the klist to iterate over the @devices_kset
該總線裡的設備用klist指針連成一個鏈表
* @klist_drivers - the klist to iterate over the @drivers_kset
驅動鏈表
* @bus_notifier - the bus notifier list for anything that cares about things
* on this bus.
* @bus - pointer back to the struct bus_type that this structure is associated
* with.
*
* This structure is the one that is the actual kobject allowing struct
* bus_type to be statically allocated safely. Nothing outside of the driver
* core should ever touch these fields.
*/
struct bus_type_private {
struct kset subsys;
struct kset *drivers_kset;
struct kset *devices_kset;
struct klist klist_devices;
struct klist klist_drivers;
struct blocking_notifier_head bus_notifier;
unsigned int drivers_autoprobe:1;
struct bus_type *bus;
};
在sysfs文件系統中,我們可以清晰地看到它們之間的聯系。kset bus_subsys對應於/sys/bus這個目錄。每個bus_type對象都對應/sys/bus目錄下的一個子目錄,如PCI類型對應於/sys/bus/pci。
在每個這樣的目錄下都存在兩個子目錄:devices和drivers。