先看使用的demo
static ssize_t ts_info_show(struct device *dev,struct device_attribute *attr, char *buf)
{
return 0;
}
static DEVICE_ATTR(ts_info, 0400, ts_info_show, NULL);
static struct attribute *g[] = {
&dev_attr_ts_info.attr,
NULL,
};
static const struct attribute_group gg = {
.attrs = g,
};
sysfs_create_group(&kobj,&gg);
kernel4.14/include/linux/device.h
#define DEVICE_ATTR(_name, _mode, _show, _store) \
struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)
kernel4.14/include/linux/sysfs.h
#define __ATTR(_name, _mode, _show, _store) { \
.attr = {.name = __stringify(_name), \
.mode = VERIFY_OCTAL_PERMISSIONS(_mode) }, \
.show = _show, \
.store = _store, \
}
比如这个static DEVICE_ATTR(ts_info, 0664, ts_info_show, NULL);
按上面的规则展开来就是
struct device_attribute dev_attr_ts_info={
.attr = {
.name = "ts_info",
.mode = 0400,
};
.show = ts_info_show,
.store = NULL,
}
kernel4.14/include/linux/device.h
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);
};
kernel4.14/include/linux/sysfs.h
struct attribute {
const char *name;
umode_t mode;
}
代码里调用
static struct attribute * g[] = {
&dev_attr_ts_info.attr,
NULL,
};