建立设备属性文件(device_create_file)

1. 涉及的内容

1)  int device_create_file(struct device *, struct device_attribute *);  在/sys/devices/xxx/目录下创建device属性文件

2) void device_remove_file(struct device *, struct device_attribute *); 移除/sys/devices/xxx/目录下的device属性文件

3) DEVICE_ATTR宏创建一个名为dev_attr_##_name的属性结构体,dev_attr_##_name用于device_create_file

#include 

#define DEVICE_ATTR(_name, _mode, _show, _store) \
        struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)

4) 初始化device_attribute结构体的宏

#include 

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

5)struct device_attribute的定义

#include 

/* 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);
};

2、创建流程

a、DEVICE_ATTR里面的_name, _mode, _show, _store四个参数实例化。

_name表示名字,在sys中代表为属性

_mode表示属性操作的权限,比如0664

_show表示属性的读取函数,在用户空间读取属性值

_store表示属性的写入函数,在用户空间写入属性值

b、属性结构体准备完毕,device_create_file创建属性文件

c、device_remove_file删除属性文件

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(嵌入式驱动,设备属性文件,驱动,sysfs)