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

2021-06-17 14:17:03 字數 2041 閱讀 1342

一、匯流排屬性

匯流排屬性使用struct bus_attribute來描述,linux給我們提供了乙個巨集,可以用來定義乙個匯流排屬性,那就是bus_attr巨集,該巨集定義如下:

#define bus_attr(_name, _mode, _show, _store)   \

struct bus_attribute bus_attr_##_name = __attr(_name, _mode, _show, _store)

其中_name為屬性的名字,_mode為屬性檔案訪問許可權,_show和_store是兩個方法,用於顯示和儲存一些資訊。建立匯流排屬性檔案使用bus_create_file,原型如下:

int bus_create_file(struct bus_type *, struct bus_attribute *);

刪除匯流排屬性檔案使用bus_remove_file函式,原型如下:

void bus_remove_file(struct bus_type *, struct bus_attribute *);

ldd上的乙個例子:

static char *version = "$revision: 1.9 $";

.../*

* export a ****** attribute.

*/static ssize_t show_bus_version(struct bus_type *bus, char *buf)

static bus_attr(version, s_irugo, show_bus_version, null);

static int __init ldd_bus_init(void)

二、裝置屬性

裝置屬性使用struct device_attribute來描述,定義裝置屬性也有乙個巨集device_attr,該巨集定義如下:

#define device_attr(_name, _mode, _show, _store) \

struct device_attribute dev_attr_##_name = __attr(_name, _mode, _show, _store)

巨集引數同匯流排屬性的巨集引數含義一樣,建立裝置屬性檔案使用device_create_file函式,原型如下:

int device_create_file(struct device *device, const struct device_attribute *entry);

刪除裝置屬性檔案使用函式device_remove_file函式,原型如下:

void device_remove_file(struct device *dev, const struct device_attribute *attr);

三、驅動屬性

驅動屬性使用struct driver_attribute來描述,定義驅動屬性也可以使用巨集driver_attr來完成,該巨集定義如下:

#define driver_attr(_name, _mode, _show, _store)        \

struct driver_attribute driver_attr_##_name =           \

__attr(_name, _mode, _show, _store)

建立驅動屬性檔案使用函式driver_create_file,原型如下:

int driver_create_file(struct device_driver *driver, const struct driver_attribute *attr);

刪除驅動屬性檔案使用函式driver_remove_file,原型如下:

void driver_remove_file(struct device_driver *driver, const struct driver_attribute *attr);

總結,不管是匯流排、還是裝置或者是驅動的屬性檔案最終都是呼叫的sysfs_create_file函式去建立的,刪除屬性檔案都是通過呼叫sysfs_remove_file去完成的,最終建立了sysfs這樣乙個基於ram的虛擬檔案系統。

匯流排 裝置和驅動

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

匯流排 裝置 驅動模型

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

匯流排裝置驅動模型

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