匯流排 裝置和驅動

2021-06-16 22:33:13 字數 3000 閱讀 4889

一、匯流排

linux中,使用struct bus_type表示一條匯流排,該結構定義在linux/device.h中,先只看其中的兩個字段,name表示匯流排的名字,比如usb、i2c、spi等,match方法用於匹配新增到該匯流排上的裝置和驅動,如果匹配成功,則呼叫驅動中的probe函式。註冊一條匯流排使用bus_register函式,原型如下:

int bus_register(struct bus_type *bus);

註冊成功可以在/sys/bus下看到該匯流排。登出使用bus_unregister函式,原型如下:

void bus_unregister(struct bus_type *bus);

匯流排操作例項如下:

#include #include #include #include module_license("gpl");

static int my_bus_match(struct device *dev, struct device_driver *drv)

struct bus_type my_bus_type = ;

export_symbol_gpl(my_bus_type);

static void my_bus_device_release(struct device *dev)

struct device my_bus_device = ;

export_symbol_gpl(my_bus_device);

static int __init my_bus_init(void)

ret = device_register(&my_bus_device);

if (ret)

return ret;

}static void __exit my_bus_exit(void)

module_init(my_bus_init);

module_exit(my_bus_exit);

這裡註冊了一條my_bus_type的匯流排,同時註冊了乙個裝置,該裝置為該匯流排裝置,可以在sys/devices下看到。該匯流排的match方法很簡單,就是比較裝置的名字和驅動的名字是否一樣。

二、裝置

裝置使用struct device來描述,其中init_name表示裝置的名字,需要注意的是,該字段在初始化完成之後被設定為null,所以以後都不能使用該欄位,需要獲取裝置的名字使用dev_name函式,例如上面的匯流排中就使用了該函式。bus表示匯流排,即該裝置掛載到什麼匯流排上的。註冊乙個裝置使用device_register函式,原型如下:

int device_register(struct device *dev);

登出裝置使用device_unregister函式,原型如下:

void device_unregister(struct device *dev);

裝置操作例項如下:

#include #include #include module_license("gpl");

static void my_device_release(struct device *dev)

extern struct bus_type my_bus_type;

extern struct device my_bus_device;

struct device my_device = ;

static int __init my_device_init(void)

return ret;

}static void __exit my_device_exit(void)

module_init(my_device_init);

module_exit(my_device_exit);

這裡還指定了parent成員為匯流排裝置,註冊成功之後將在匯流排裝置的目錄下看到該裝置。由於這裡使用了匯流排當中的my_bus_type和my_bus_device兩個成員,所以這裡需要使用extern宣告這兩個成員,同時還需記住的是在匯流排**當中必須使用export_symbol_gpl匯出這兩個符號,這樣才能其它地方使用這些符號,匯出符號還有乙個巨集export_symbol,其中export_symbol_gpl只能被gpl許可證下的模組使用。

三、驅動

驅動使用struct device_driver來描述,它也有個name欄位,這個欄位是可以一直使用的。同裝置一樣,也有個bus成員,含義同裝置一樣,都表示掛載到哪條匯流排上。驅動中有個probe和remove方法,其中probe方法將在匯流排匹配裝置和該驅動成功之後被呼叫,remove方法將在裝置或驅動解除安裝時被呼叫。註冊驅動使用driver_register函式,原型如下:

int driver_register(struct device_driver *drv);

登出驅動使用driver_unregister函式,原型如下:

void driver_unregister(struct device_driver *drv);

驅動操作例項如下:

#include #include #include module_license("gpl");

static int my_driver_probe(struct device *dev)

static int my_driver_remove(struct device *dev)

extern struct bus_type my_bus_type;

struct device_driver my_driver = ;

static int __init my_driver_init(void)

return ret;

}static void __exit my_driver_exit(void)

module_init(my_driver_init);

module_exit(my_driver_exit);

關於匯流排、裝置和驅動需要特別注意的是匯流排的match方法在匹配裝置和驅動成功之後呼叫驅動的probe方法,裝置或驅動移除時呼叫驅動的remove方法。

匯流排 裝置和驅動屬性檔案

一 匯流排屬性 匯流排屬性使用struct bus attribute來描述,linux給我們提供了乙個巨集,可以用來定義乙個匯流排屬性,那就是bus attr巨集,該巨集定義如下 define bus attr name,mode,show,store struct bus attribute b...

匯流排 裝置 驅動模型

裝置元素 匯流排,驅動,裝置 匯流排 處理器和裝置之間的通道,在裝置模型中,所有的裝置都通過匯流排相連,甚至是內部的虛擬 platform 匯流排 定時器,看門狗並沒有直接相連 在linux裝置模型中,匯流排由bus type結構表示,定義在 匯流排的註冊使用 bus register struct...

匯流排裝置驅動模型

匯流排裝置驅動模型 匯流排是主機和裝置之間的通道,由bus type 結構描述。int bus register struct bus type bus 匯流排的註冊,若成功,新的匯流排將被新增進系統,並可在 sysfs 的 sys bus 下看到。void bus unregister struc...