Linux 內(nèi)核學(xué)習(xí)(9)---- Linux sysfs 文件系統(tǒng)(2)

struct device 類型

系統(tǒng)中的任一設(shè)備在設(shè)備模型中都由一個(gè) device 對(duì)象描述,其對(duì)應(yīng)的數(shù)據(jù)結(jié)構(gòu) struct device

struct device {
    struct kobject kobj;
    struct device       *parent;
    struct device_private   *p;
    const char      *init_name; /* initial name of the device */
    const struct device_type *type;
    struct bus_type *bus;       /* type of bus device is on */
    struct device_driver *driver;   /* which driver has allocated this device */
    void        *platform_data; /* Platform specific data, device core doesn't touch it */
    void        *driver_data;   /* Driver data, set and get with dev_set_drvdata/dev_get_drvdata */
    ....
    struct dev_links_info   links;
    struct dev_pm_info  power;
    struct dev_pm_domain    *pm_domain;
    const struct dma_map_ops *dma_ops;
    u64     *dma_mask;  /* dma mask (if dma'able device) */
    u64     coherent_dma_mask;
    u64     bus_dma_mask;   /* upstream dma_mask constraint */
    unsigned long   dma_pfn_offset;
    struct device_dma_parameters *dma_parms;
    ......
};

device 內(nèi)嵌一個(gè) kobject 對(duì)象,用于實(shí)現(xiàn)引用計(jì)數(shù)的管理并且通過(guò)它實(shí)現(xiàn) sysfs 的層級(jí)結(jié)構(gòu)
driver 域指向管理該設(shè)備的驅(qū)動(dòng)程序?qū)ο?,driver_data 是提供給驅(qū)動(dòng)程序的數(shù)據(jù)
bus 域描述設(shè)備連接的總線類型

內(nèi)核提供了相應(yīng)的函數(shù),操作device 對(duì)象:
device_reigster 函數(shù)將一個(gè)新的 device 對(duì)象插入設(shè)備模型,并且自動(dòng)在 /sys/devices 下創(chuàng)建一個(gè)新的目錄
device_unregister 完成相反的操作
device_reigster 中最終是調(diào)用 kobject_add 這些底層函數(shù),將 device 中包含的 kobject 注冊(cè)到整個(gè) kobject 的層級(jí)結(jié)構(gòu)中

sysfs 相關(guān)API

使用默認(rèn)目錄

sysfs 默認(rèn)掛載在 "sys" 目錄下,對(duì)于內(nèi)核態(tài)是 "kobject",對(duì)于用戶態(tài),映射在總線(bus) 對(duì)應(yīng)的目錄下,一般的設(shè)備驅(qū)動(dòng),我們使用的是 platform 虛擬總線,因此會(huì)映射在 /sys/platform/device/xxx 下
設(shè)備驅(qū)動(dòng)創(chuàng)建時(shí),就會(huì)創(chuàng)建該目錄,因此不需要手動(dòng)創(chuàng)建
在bus/devices 下創(chuàng)建文件:

static inline int sysfs_create_files(struct kobject *kobj, const struct attribute * const *attr)
自定義目錄

在 /sys 目錄下創(chuàng)建自定義目錄,創(chuàng)建目錄使用下面的接口:

struct kobject *kobject_create_and_add(const char *name, struct kobject *parent)
{
    struct kobject *kobj;
    int retval;

    kobj = kobject_create();
    if (!kobj)
        return NULL;

    retval = kobject_add(kobj, parent, "%s", name);
    if (retval) {
        pr_warn("%s: kobject_add error: %d\n", __func__, retval);
        kobject_put(kobj);
        kobj = NULL;
    }
    return kobj;
}

name 表示目錄名稱
parent 表示父目錄,NULL 為默認(rèn)在 /sys 目錄下創(chuàng)建

創(chuàng)建和釋放文件

內(nèi)核 kobject 中的 attribute 映射到用戶態(tài)就是文件,attribute 分為下面三類結(jié)構(gòu):

struct attribute {
    const char      *name;
    umode_t         mode;
    #ifdef CONFIG_DEBUG_LOCK_ALLOC
    bool            ignore_lockdep:1;
    struct lock_class_key   *key;
    struct lock_class_key   skey;
    #endif
};

struct attribute_group {
    const char      *name;
    umode_t         (*is_visible)(struct kobject *,
                          struct attribute *, int);
    umode_t         (*is_bin_visible)(struct kobject *,
                          struct bin_attribute *, int);
    struct attribute    **attrs;
    struct bin_attribute    **bin_attrs;
};

struct bin_attribute {
    struct attribute    attr;
    size_t          size;
    void            *private;
    ssize_t (*read)(struct file *, struct kobject *, struct bin_attribute *,
        char *, loff_t, size_t);
    ssize_t (*write)(struct file *, struct kobject *, struct bin_attribute *,
         char *, loff_t, size_t);
    int (*mmap)(struct file *, struct kobject *, struct bin_attribute *attr,
        struct vm_area_struct *vma);
};
  • struct attribute :屬性的通用結(jié)構(gòu),其他部分在使用時(shí)可以將 struct attribute 嵌入大到更大的屬性結(jié)構(gòu)中
  • struct bin_attribute:為二進(jìn)制屬性專門(mén)設(shè)計(jì),在 sysfs 中表現(xiàn)為二進(jìn)制文件,大多數(shù)設(shè)備參數(shù)的映射
  • struct attribute_group:提供一組屬性的集合,集中管理 attribute 和 bin_attribute

sysfs 提供了下面的接口,將屬性關(guān)聯(lián)到對(duì)應(yīng)的 kobject 上:

static inline int sysfs_create_files(struct kobject *kobj, const struct attribute * const *attr)
static inline int sysfs_create_file_ns(struct kobject *kobj, const struct attribute *attr, const void *ns)
int __must_check sysfs_create_bin_file(struct kobject *kobj, const struct bin_attribute *attr);
int __must_check sysfs_create_group(struct kobject *kobj, const struct attribute_group *grp);

struct attribute 包含了一個(gè) mode 屬性,即文件的訪問(wèn)權(quán)限,可讀,可寫(xiě),可執(zhí)行,可以用已經(jīng)定義的宏表示
(S_IWUSR | S_IRUGO),也可以用數(shù)字表示(0600)
宏的含義如下
S_IRUSR:用戶讀權(quán)限
S_IWUSR:用戶寫(xiě)權(quán)限
S_IRGRP:用戶組讀權(quán)限
S_IWGRP:用戶組寫(xiě)權(quán)限
S_IROTH:其他組都權(quán)限
S_IWOTH:其他組寫(xiě)權(quán)限

attribute 初始化宏

sysfs 接口提供了一組宏定義來(lái)初始化上面的 struct attibute 結(jié)構(gòu)

#define __ATTR(_name, _mode, _show, _store) {               \
    .attr = {.name = __stringify(_name),                \
         .mode = VERIFY_OCTAL_PERMISSIONS(_mode) },     \
    .show   = _show,                        \
    .store  = _store,                       \
}

#define __ATTR_PREALLOC(_name, _mode, _show, _store) {          \
    .attr = {.name = __stringify(_name),                \
         .mode = SYSFS_PREALLOC | VERIFY_OCTAL_PERMISSIONS(_mode) },\
    .show   = _show,                        \
    .store  = _store,                       \
}

#define __ATTR_RO(_name) {                      \
    .attr   = { .name = __stringify(_name), .mode = 0444 },     \
    .show   = _name##_show,                     \
}

#define __ATTR_RO_MODE(_name, _mode) {                  \
    .attr   = { .name = __stringify(_name),             \
            .mode = VERIFY_OCTAL_PERMISSIONS(_mode) },      \
    .show   = _name##_show,                     \
}

#define __ATTR_WO(_name) {                      \
    .attr   = { .name = __stringify(_name), .mode = 0200 },     \
    .store  = _name##_store,                    \
}
#define __ATTR_RW(_name) __ATTR(_name, 0644, _name##_show, _name##_store)
#define __ATTR_NULL { .attr = { .name = NULL } }
屬性的讀寫(xiě)方法
struct kobj_attribute {
    struct attribute attr;
    ssize_t (*show)(struct kobject *kobj, struct kobj_attribute *attr,
            char *buf);
    ssize_t (*store)(struct kobject *kobj, struct kobj_attribute *attr,
             const char *buf, size_t count);
};
  • show 函數(shù)在讀操作時(shí)被調(diào)用,它會(huì)拷貝 attr 提供的屬性值到 buffer 指定的緩沖區(qū)中,緩沖區(qū)的大小為 PAGE_SIZE 字節(jié),如果讀取成功,則返回實(shí)際寫(xiě)入 buffer 的字節(jié)數(shù),如果失敗,返回負(fù)的錯(cuò)誤碼
  • store 函數(shù)在寫(xiě)操作時(shí)被調(diào)用,它會(huì)從 buffer 中讀取 size 大小的字節(jié),并將其保存在 attr 所表示的屬性結(jié)構(gòu)體變量中,如果成功,則將返回實(shí)際從buffer中讀取的字節(jié)數(shù),如果失敗,返回負(fù)的錯(cuò)誤碼

device 中對(duì)sysfs的封裝

/* interface for exporting device attributes */
struct device_attribute {
    struct attribute    attr;
    ssize_t (*show)(struct device *dev, struct device_attribute *attr,
            char *buf);
    ssize_t (*store)(struct device *dev, struct device_attribute *attr,
             const char *buf, size_t count);
};

#define DEVICE_ATTR(_name, _mode, _show, _store) \
    struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)
#define DEVICE_ATTR_PREALLOC(_name, _mode, _show, _store) \
    struct device_attribute dev_attr_##_name = \
        __ATTR_PREALLOC(_name, _mode, _show, _store)
#define DEVICE_ATTR_RW(_name) \
    struct device_attribute dev_attr_##_name = __ATTR_RW(_name)
#define DEVICE_ATTR_RO(_name) \
    struct device_attribute dev_attr_##_name = __ATTR_RO(_name)
#define DEVICE_ATTR_WO(_name) \
    struct device_attribute dev_attr_##_name = __ATTR_WO(_name)

extern int device_create_file(struct device *device, const struct device_attribute *entry);
extern void device_remove_file(struct device *dev, const struct device_attribute *attr);

struct device 結(jié)構(gòu)中包含了 kobject 對(duì)象,相應(yīng)的 include/linux/Device.h 包含了 struct device_attribute 結(jié)構(gòu)體
定義對(duì)應(yīng)的 device_attribute 結(jié)構(gòu),使用 device_create_file 接口,就可以向 device 目錄下添加對(duì)應(yīng)的文件

DEVICE_ATTR 開(kāi)頭的宏是對(duì) __ATTR 宏的進(jìn)一步封裝

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容