I2C驅動體系結構一:驅動軟件概念與對應硬件的關係

一、概念:

1、設備:struct device:該數據結構是對物理設備的軟件抽象,比如I2C slave(對應i2c_client)和I2C 適配器(對應i2c_adapter)都是對應物理設備的軟件表達;

2、驅動:struct device_driver:該數據結構是對設備驅動的表達,從該數據結構的命名來看,它表達的意思是設備的驅動,從其回調函數成員變量來看,它實際是對struct device的管理(包括檢測、電源管理);但是並不包括對設備的功能性操作。可以說struct device_driver使用struct device實現了對具體物理設備的管理。

3、總線:struct bus_type:這個數據結構從名字看似乎很容易理解,總線,但是在計算機體系裏,總線這個詞本身就很抽象,到底什麼是計算機體系的總線,struct bus_type能否表達計算機系統中的總線呢?從kernel的註釋上看,struct bus_type是表示處理器與一個或多個設備之間的通道。從設備模型上看,所有設備都是通過“總線”進行連接。

* A bus is a channel between the processor and one or more devices. For the
 * purposes of the device model, all devices are connected via a bus, even if
 * it is an internal, virtual, "platform" bus. Buses can plug into each other.
 * A USB controller is usually a PCI device, for example. The device model
 * represents the actual connections between buses and the devices they control.
 * A bus is represented by the bus_type structure. It contains the name, the
 * default attributes, the bus' methods, PM operations, and the driver core's
 * private data.
 */

但是這裏如果完全把struct bus_type理解成物理意義上的連接關係會十分費解,因爲從物理上看,“總線”和“適配器”往往是說的一個意思;“物理設備”掛在到“適配器”下,從這個角度上看,“適配器”作爲“總線”的實際物理設備是合適的;但是從設備模型驅動上看,控制器往往是作爲一個“struct device”(如 struct i2c_adapter),並且也是掛在到“struct bus_type”下,所以從這個角度看,“struct bus_type”不能完全對應適配器,那麼"struct bus_type"到底怎麼理解呢?我認爲可以按照如下的角度理解:

1、將“適配器”作爲“總線”和“設備”兩種角色的集合;

2、"struct bus_type"的角色承擔上述“總線”的功能,包括對外提供probe、match等接口從總線的角度實現設備的檢測;

3、"struct bus_type"的角色還承擔"容器"的功能,"容納"了向該bus註冊的device及device_driver,至於device與device_driver的對應關係,由device和device_driver自身管理

二、is_a or has_a

C語言的語法並不完整清晰支持面向對象的設計,但是通過上下文理解和數據結構的組成形式,也可以從對象的角度進行一定程度的抽象分析。比如:

struct i2c_adapter/i2c_client {

......

struct device dev;

};

我們可以將struct i2c_adapter,struct i2c_client與struct device的關係理解爲 is_a 的關係,即,struct i2c_adapter/i2c_client是struct device的子類

struct bus_type:可以看到該數據結構的主體成員是多個回調函數成員,我們可以認爲struct bus_type是一個抽象結構,struct bus_type i2c_bus_type可以認爲是struct bus_type的實現類,因此也可以認爲 i2c_bus_type這個變量是struct bus_type的子類

struct bus_type中有一個struct subsys_private的指針結構,這是一個“容器”,記錄了“掛載”在該"bus"上的struct device及struct device_driver,在這個地方,可以認爲struct bus_type與struct device/struct device_driver是has_a的關係。

上圖是I2C設備模型數據結構純軟件上的關係圖

 

三、物理連接

I2C子系統,在物理上的連接實際十分明顯,如下:

上圖是硬件連接示意圖。

 

從上述兩個圖看,一個是軟件上的關係圖,一個是硬件上的關係圖,兩者很難和諧的統一起來。

設備模型軟件上的關係圖如何與硬件上的連接圖相映射,下一章再進行討論。

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章