linux裝置模型之匯流排 裝置 和驅動
《Linux核心修煉之道》讀書筆記
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中。
該匯流排系統裡所有驅動的集合
* @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。
(2)、裝置驅動device_driver
struct device_driver {
const char
*name;驅動程式名稱
struct bus_type*bus;該驅動所管理的裝置掛接的匯流排型別
struct module*owner;
const char *mod_name;/*
used for built-in modules */
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 (*resume) (struct device *dev);
struct attribute_group **groups;
struct pm_ops *pm;電源管理
struct driver_private *p;
};
與匯流排類似
struct driver_private {
struct kobject kobj;
struct klist klist_devices;該驅動程式能操作的裝置連結串列
struct klist_node knode_bus;
struct module_kobject *mkobj;
struct device_driver *driver;
};
注:device_driver結構很少單獨使用,而通常將其嵌入到一個驅動的高層中。如platform驅動
struct platform_driver {
int (*probe)(struct platform_device *);
int (*remove)(struct platform_device *);
void (*shutdown)(struct platform_device *);
int (*suspend)(struct platform_device *, pm_message_t state);
int (*suspend_late)(struct platform_device *, pm_message_t state);
int (*resume_early)(struct platform_device *);
int (*resume)(struct platform_device *);
struct pm_ext_ops *pm;
struct device_driver driver;
};
(3)、device裝置
struct device {
struct klistklist_children;裝置列表中的孩子列表
struct klist_nodeknode_parent;/* node in sibling list */
struct klist_nodeknode_driver;
struct klist_nodeknode_bus;
分別是掛入parent 、驅動、匯流排連結串列中的指標
struct device*parent;
struct kobject kobj;
charbus_id[BUS_ID_SIZE];/*
position on parent bus */
const char*init_name; /* initial name of the device */
struct device_type*type;
unsigneduevent_suppress:1;
struct semaphoresem;/*
semaphore to synchronize calls to
* its driver.
*/
struct bus_type*bus;/* type of bus device is on */裝置所在匯流排
struct device_driver *driver;/* which driver has allocated this
device */該裝置的驅動程式
void*driver_data;/*
data private to the driver */
void*platform_data;/*
Platform specific data, device
core doesn't touch it */
struct dev_pm_infopower;
#ifdef CONFIG_NUMA
intnuma_node;/*
NUMA node this device is close to */
#endif
u64*dma_mask;/*
dma mask (if dma'able device) */
u64coherent_dma_mask;/* Like dma_mask, but for
alloc_coherent mappings as
not all hardware supports
64 bit addresses for consistent
allocations such descriptors. */
struct device_dma_parameters *dma_parms;
struct list_headdma_pools;/*
dma pools (if dma'ble) */
struct dma_coherent_mem*dma_mem; /* internal for coherent mem
override */
/* arch specific additions */
struct dev_archdataarchdata;
spinlock_tdevres_lock;
struct list_headdevres_head;
struct klist_nodeknode_class;
struct class*class;
dev_tdevt;/*
dev_t, creates the sysfs "dev" */
struct attribute_group**groups;/*
optional groups */
void(*release)(struct device *dev);
};
注:device也通常嵌入到一個更大的結構體中。如platform裝置
struct platform_device {
const char* name;
intid;
struct devicedev;
u32num_resources;
struct resource* resource;
};
最後比較匯流排、裝置和驅動的結構,我們發現,struct bus_type中有struct kset drivers和struct kset devices,同時struct device中有兩個成員struct bus_type*bus和struct device_driver *driver,struct device_driver中有兩個成員struct bus_type*bus和struct klist klist klist_devices.
4、
(1)、struct device中的bus表示這個裝置練到那個總線上,driver表示這個裝置的驅動是什麼,struct device_driver中的bus表示這個驅動屬於哪個匯流排,klist_devices表示這個驅動都支援哪些裝置,因為這裡device是複數,又是list,更因為一個驅動可以支援多個裝置,而一個裝置只能繫結一個驅動。當然,struct bus_type中的drivers和devices分別表示了這個匯流排擁有那些裝置和驅動。
(2)、匯流排中兩個連結串列的形成,核心每出現一個裝置都要向匯流排彙報,或者說註冊,每出現一個驅動,也要向匯流排註冊。
(3)、現在,device可以在任何時刻出現,而driver也可以在任何時刻被載入,所以,出現的情況就是,每當一個struct device誕生,就會去bus的derivers連結串列中尋找它的另一半,反之,每當一個struct device_driver誕生,它就會去bus的devices連結串列中尋找它的那些裝置。如果找到合適的,呼叫device_bind_driver繫結好。如果找不到,等待。