美文网首页
Linux 内核学习(9)---- Linux sysfs 文件

Linux 内核学习(9)---- Linux sysfs 文件

作者: 特立独行的佩奇 | 来源:发表于2023-04-27 17:33 被阅读0次

struct device 类型

系统中的任一设备在设备模型中都由一个 device 对象描述,其对应的数据结构 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 内嵌一个 kobject 对象,用于实现引用计数的管理并且通过它实现 sysfs 的层级结构
driver 域指向管理该设备的驱动程序对象,driver_data 是提供给驱动程序的数据
bus 域描述设备连接的总线类型

内核提供了相应的函数,操作device 对象:
device_reigster 函数将一个新的 device 对象插入设备模型,并且自动在 /sys/devices 下创建一个新的目录
device_unregister 完成相反的操作
device_reigster 中最终是调用 kobject_add 这些底层函数,将 device 中包含的 kobject 注册到整个 kobject 的层级结构中

sysfs 相关API

使用默认目录

sysfs 默认挂载在 "sys" 目录下,对于内核态是 "kobject",对于用户态,映射在总线(bus) 对应的目录下,一般的设备驱动,我们使用的是 platform 虚拟总线,因此会映射在 /sys/platform/device/xxx 下
设备驱动创建时,就会创建该目录,因此不需要手动创建
在bus/devices 下创建文件:

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

在 /sys 目录下创建自定义目录,创建目录使用下面的接口:

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 为默认在 /sys 目录下创建

创建和释放文件

内核 kobject 中的 attribute 映射到用户态就是文件,attribute 分为下面三类结构:

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 :属性的通用结构,其他部分在使用时可以将 struct attribute 嵌入大到更大的属性结构中
  • struct bin_attribute:为二进制属性专门设计,在 sysfs 中表现为二进制文件,大多数设备参数的映射
  • struct attribute_group:提供一组属性的集合,集中管理 attribute 和 bin_attribute

sysfs 提供了下面的接口,将属性关联到对应的 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 包含了一个 mode 属性,即文件的访问权限,可读,可写,可执行,可以用已经定义的宏表示
(S_IWUSR | S_IRUGO),也可以用数字表示(0600)
宏的含义如下
S_IRUSR:用户读权限
S_IWUSR:用户写权限
S_IRGRP:用户组读权限
S_IWGRP:用户组写权限
S_IROTH:其他组都权限
S_IWOTH:其他组写权限

attribute 初始化宏

sysfs 接口提供了一组宏定义来初始化上面的 struct attibute 结构

#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 } }
属性的读写方法
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 函数在读操作时被调用,它会拷贝 attr 提供的属性值到 buffer 指定的缓冲区中,缓冲区的大小为 PAGE_SIZE 字节,如果读取成功,则返回实际写入 buffer 的字节数,如果失败,返回负的错误码
  • store 函数在写操作时被调用,它会从 buffer 中读取 size 大小的字节,并将其保存在 attr 所表示的属性结构体变量中,如果成功,则将返回实际从buffer中读取的字节数,如果失败,返回负的错误码

device 中对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 结构中包含了 kobject 对象,相应的 include/linux/Device.h 包含了 struct device_attribute 结构体
定义对应的 device_attribute 结构,使用 device_create_file 接口,就可以向 device 目录下添加对应的文件

DEVICE_ATTR 开头的宏是对 __ATTR 宏的进一步封装

相关文章

网友评论

      本文标题:Linux 内核学习(9)---- Linux sysfs 文件

      本文链接:https://www.haomeiwen.com/subject/myhmjdtx.html