SYSFS中屬性檔案的建立

2021-10-23 20:19:43 字數 1542 閱讀 6790

1、device中建立屬性檔案

(1)函式呼叫關係:

device_create_file

sysfs_create_file

(2)相關的資料結構:

1 struct attribute ;
1 struct device_attribute ;
(3) 相關的巨集定義

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

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

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

.show = _show,

.store = _store,

由此可知這個巨集定義就是定義了乙個 device_attribute 型別的變數並進行了初始化。

總結:我們如果需要給device新增屬性檔案,那麼我們可以通過device_create_file函式去新增,我們需要提供相應的device屬性檔案描述資訊,

也就是乙個device_attribute 結構體變數。

2、class建立屬性檔案

(1)函式呼叫關係

class_create_file

sysfs_create_file

(2)相關的資料結構

1 struct class_attribute ;
(3)相關的巨集定義

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

struct class_attribute class_attr_##_name = __attr(_name, _mode, _show, _store)

3、bus建立屬性檔案

(1)函式呼叫關係

bus_create_file

sysfs_create_file

(2)相關的資料結構

1 struct bus_attribute ;
(3)相關的巨集定義

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

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

4、一次建立多個屬性檔案的方法

上面說的那些函式一次只能建立乙個屬性檔案,其實在核心中還提供了另乙個可以一次建立多個屬性檔案的函式,所以既然是建立多個,那麼我們肯定是需要提供相應的陣列。

(1)一次建立多個device屬性

static int device_add_attributes(struct device *dev, struct device_attribute *attrs)

(2)一次建立多個bus屬性檔案

static int device_add_attrs(struct bus_type *bus, struct device *dev) 

sysfs系統 檔案和目錄的建立

sysfs是linux的特殊檔案系統,這個檔案系統主要作用是在使用者態展示裝置的資訊。linux計算機系統中,可以在根目錄下面找到sys目錄,這個目錄就是利用sysfs檔案系統進行建立的。開啟sys目錄,可以看到裝置的分類顯示,如下所示 yxf yxf pc sys ls block bus cla...

sysfs介面函式的建立 DEVICE ATTR

sysfs介面函式到建立 device attr 最近在弄sensor驅動,看過乙個某廠家的成品驅動,裡面實現的全都是sysfs介面,hal層利用sysfs生成的介面,對sensor進行操作。說道sysfs介面,就不得不提到函式巨集 device attr 原型是 define device att...

《驅動學習 sysfs檔案系統的編寫》

1.sysfs檔案系統 sysfs檔案系統可以把核心空間的資料 屬性 鏈結等輸出到使用者空間。反過來,使用者也可以通過sysfs檔案系統,往對應的核心空間傳遞資料。例如 echo 1 sys class gpio gpio64 value,就是改變gpio64的值。cat sys class gpi...