/sys 下文件生成:
1. 定义属性,如: /kernel/power/power.h
#define power_attr(_name) \
static struct kobj_attribute _name##_attr = { \
.attr = { \
.name = __stringify(_name), \
.mode = 0644, \
}, \
.show = _name##_show, \
.store = _name##_store, \
}
power_attr(state); // 定义state属性
2. 如果是多个,可以生成属性组,如:/kernel/power/main.c
static struct attribute * g[] = {
&state_attr.attr,
#ifdef CONFIG_PM_TRACE
&pm_trace_attr.attr,
#endif
#ifdef CONFIG_PM_SLEEP
&pm_async_attr.attr,
#ifdef CONFIG_PM_DEBUG
&pm_test_attr.attr,
#endif
#endif
NULL,
};
static struct attribute_group attr_group = {
.attrs = g,
};
3. 定义操作读写/sys下文件的函数:
static ssize_t state_show(struct kobject *kobj, struct kobj_attribute *attr,
char *buf)
{
...................
return (s - buf);
}
static ssize_t state_store(struct kobject *kobj, struct kobj_attribute *attr,
const char *buf, size_t n)
{
....................
Exit:
return error ? error : n;
}
4. 创建文件:
static int __init pm_init(void)
{
int error = pm_start_workqueue();
if (error)
return error;
power_kobj = kobject_create_and_add("power", NULL);
if (!power_kobj)
return -ENOMEM;
return sysfs_create_group(power_kobj, &attr_group);
}
/proc 下文件的生成方法类似:
1. 定义属性写属性组:
static DEVICE_ATTR(in0_input, S_IRUGO, show_volt, NULL);
static DEVICE_ATTR(curr1_input, S_IRUGO, show_curr, NULL);
static struct attribute *zl6100_attributes[] = {
&dev_attr_in0_input.attr,
&dev_attr_curr1_input.attr,
NULL
};
static const struct attribute_group zl6100_group = {
.attrs = zl6100_attributes,
};
2. 定义读写文件的函数:
static int show_volt(struct device *dev, struct device_attribute *attr,
char *buf)
{
.....................
return sprintf(buf, "%d\n", volt);
}
static int show_curr(struct device *dev, struct device_attribute *attr,
char *buf)
{
....................
return sprintf(buf, "%d\n", cur);
}
3. 创建文件:
err = sysfs_create_group(&client->dev.kobj, &zl6100_group);
阅读(2206) | 评论(0) | 转发(0) |