Linux那些事兒之我是Sysfs(3)裝置模型上層容器

來源:互聯網
上載者:User

§1 bus
系統中匯流排由struct bus_type描述,定義為:
struct bus_type {
char * name; 匯流排類型的名稱
struct subsystem subsys; 與該匯流排相關的subsystem
struct kset drivers; 所有與該匯流排相關的驅動程式集合
struct kset devices; 所有掛接在該匯流排上的裝置集合
struct bus attribute * bus_attrs; 匯流排屬性
struct device attribute * dev_attrs; 裝置屬性
struct driver attribute * drv_attrs; 驅動程式屬性
int (*match)(struct device * dev, struct device_driver * drv);
int (*hotplug) (struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size);
int (*suspend)(struct device * dev, u32 state);
int (*resume)(struct device * dev);
};
每個bus_type對象都內嵌一個subsystem對象,bus_subsys對象管理系統中所有匯流排類型的subsystem對象。每個bus_type對象都對應/sys/bus目錄下的一個子目錄,如PCI匯流排類型對應於/sys/bus/pci。在每個這樣的目錄下都存在兩個子目錄:devices和drivers(分別對應於bus type結構中的devices和drivers域)。其中devices子目錄描述串連在該匯流排上的所有裝置,而drivers目錄則描述與該匯流排關聯的所有驅動程式。與device_driver對象類似,bus_type結構還包含幾個函數(match()、hotplug()等)處理相應的熱插拔、即插即拔和電源管理事件。

§2 device
系統中的任一裝置在裝置模型中都由一個device對象描述,其對應的資料結構struct device
定義為:
struct device {
struct list_head g_list;
struct list_head node;
struct list_head bus_list;
struct list_head driver_list;
struct list_head children;
struct device *parent;
struct kobject kobj;
char bus_id[BUS_ID_SIZE];
struct bus_type *bus;
struct device_driver *driver;
void *driver_data;
/* Several fields omitted */
};
g_list 將該device對象掛接到全域裝置鏈表中,所有的device對象都包含在devices subsys中,並組織成階層。Node域將該對象掛接到其兄弟對象的鏈表中,而bus list則用於將串連到相同匯流排上的裝置群組織成鏈表,driver list則將同一驅動程式管理的所有裝置群組織為鏈表。此外,children域指向該device對象子物件鏈表頭,parent域則指向父物件。Device對象還內嵌一個kobject對象,用於引用計數管理並通過它實現裝置階層。Driver域指向管理該裝置的驅動程式對象,而driver data則是提供給驅動程式的資料。Bus域描述裝置所串連的
匯流排類型。

核心提供了相應的函數用於操作device對象。其中device_register()函數將一個新的device對象插入裝置模型,並自動在/sys/devices下建立一個對應的目錄。device_unregister()完成相反的操作,登出裝置對象。get_device()和put_device()分別增加與減少裝置對象的引用計數。通常device結構不單獨使用,而是包含在更大的結構中作為一個子結構使用,比如描述PCI裝置的struct pci_dev,還有我們ldd_dev,其中的dev域就是一個device對象。

§3. driver
系統中的每個驅動程式由一個device_driver對象描述,對應的資料結構定義為:
struct device_driver {
char *name; 裝置驅動程式的名稱
struct bus_type *bus; 該驅動所管理的裝置掛接的匯流排類型
struct kobject kobj; 內嵌kobject對象
struct list_head devices; 該驅動所管理的裝置鏈表頭
int (*probe)(struct device *dev); 指向裝置探測函數,用於探測裝置是否可以被該驅動程式管理
int (*remove)(struct device *dev); 用於刪除裝置的函數
/* some fields omitted*/
};
與device 結構類似,device_driver對象依靠內嵌的kobject對象實現引用計數管理和階層組織。核心提供類似的函數用於操作device_driver對象,如get_driver()增加引用計數,driver_register()用於向裝置模型插入新的driver對象,同時在sysfs檔案系統中建立對應的目錄。device_driver()結構還包括幾個函數,用於處理熱拔插、隨插即用和電源管理事件。 

可能你面對剛剛列舉出來的一些列資料結構,感到很苦惱,很莫名其妙。沒關係,我接下來講個例子您就明白了。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.