标签:
1、 创建相应的kobj_attribute。本实验用到打开手电筒,关闭手电筒,打开闪关灯,关闭闪光灯。所以创建四个kobj_attribute。因为四个格式比较类似,只有功能函数实现功能不同,所以本文只以sysfs_torch为例进行说明。
static struct kobj_attribute sysfs_torch = __ATTR(torch,S_IRUGO,sysfs_torch,NULL)
<pre name="code" class="cpp">static struct kobj_attribute sysfs_off = __ATTR(off,S_IRUGO,sysfs_off,NULL)
kobj_attribute 定义
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);
}; #define __ATTR(_name, _mode, _show, _store) { .attr = {.name = __stringify(_name), .mode = _mode }, .show = _show, .store = _store, }2、创建相对应的功能函数 sysfs_torch static ssize_t sysfs_torch(struct kobject *kobj,struct kobj_attribute *attr, char *buf) {<span style="white-space:pre"> </span>实现手电筒功能代码。 }3、创建 struct attribute 结构体数组 和 struct attribute_group 对象。
static struct attribute *flash_sysfs[] = {
&sysfs_torch.attr,
&sysfs_off.attr,
&sysfs_flash.attr,
NULL,
} static struct attribute_group flash_attr_group = {
.attrs = flash_sysfs,
}4、编写init函数
init函数主要调用函数
sysfs_create_group(struct kobject *kobj,const struct attribute_group *grp);
传递进参数就可以实现使用sysfs控制闪光灯。
也可以自己创建一个struct kobject 赋值为空,并使用kobject_create_and_add(const char *name ,struct kobject *parent) 创建。把该返回值传递给sysfs_create_group
我实现的时候,直接在 platform_probe 中添加sysfs_create_group,根据此创建的命令,会保存在和你设备所在的devices目录下,如果不知道在那个目录下,可以使用命令
find ./ -name "你定义的名字" 进行查找。5、exit函数
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/li744831579/article/details/46691901