在sys目录创建/sys/zwx_demo/zwx_test_status 接口
int zwx_test_status = 0; //文件zwx_test_status的默认值
static struct kobject *zwx_test_kobj;
static ssize_t zwx_test_status_show(struct kobject *kobj, struct kobj_attribute *attr,
char *buf)
{
return sprintf(buf, "%d\n", zwx_test_status); //读节点zwx_test_status的值
}
static ssize_t zwx_test_status_store(struct kobject *kobj, struct kobj_attribute *attr,
const char *buf, size_t count)
{
char *after;
unsigned long status = simple_strtoul(buf, &after, 10);
zwx_test_status = (int)status; //写节点zwx_test_status的值
return count;
}
struct kobj_attribute zwx_test_status_attr = {
.attr = {"zwx_test_status", 0660},
.show = &zwx_test_status_show, //读节点zwx_test_status
.store = &zwx_test_status_store, //写节点zwx_test_status
};
static struct attribute * zwx_test_attr[] = {
&zwx_test_status_attr.attr, //节点zwx_test_status 的属性
NULL,
};
static struct attribute_group zwx_test_attr_group = {
.attrs = zwx_test_attr, //节点组zwx_test_attr_group,包含zwx_test_attr
};
在代码实现
zwx_test_kobj = kobject_create_and_add("zwx_demo", NULL); //在sys目录创建zwx_demo目录
printk("---zwx--- create zwx_demo node!\n");
if (zwx_test_kobj)
rc = sysfs_create_group(zwx_test_kobj, &zwx_test_attr_group); //在sys/zwx_demo/目录创建zwx_test_status 文件
root@android:/sys/zwx_demo # echo 2000 > zwx_test_status
root@android:/sys/zwx_demo # cat zwx_test_status
2000
root@android:/sys/zwx_demo #