sysfs接口函数的建立_DEVICE_ATTR

http://www.cnblogs.com/superlcc/archive/2012/08/08/2628290.html


sysfs接口函数到建立_DEVICE_ATTR


最近在弄Sensor驱动,看过一个某厂家的成品驱动,里面实现的全都是sysfs接口,hal层利用sysfs生成的接口,对Sensor进行操作。

说道sysfs接口,就不得不提到函数宏 DEVICE_ATTR

原型是#define DEVICE_ATTR(_name, _mode, _show, _store) \

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

函数宏DEVICE_ATTR内封装的是__ATTR(_name,_mode,_show,_stroe)方法,_show表示的是读方法,_stroe表示的是写方法。

当然_ATTR不是独生子女,他还有一系列的姊妹__ATTR_RO宏只有读方法,__ATTR_NULL等等

如 对设备的使用  DEVICE_ATTR  ,对总线使用  BUS_ATTR  ,对驱动使用 DRIVER_ATTR  ,对类 别 (class) 使用  CLASS_ATTR,  这四个高级的宏来自于<include/linux/device.h> 

DEVICE_ATTR  宏声明有四个参数,分别是名称、权限位、读函数、写函数。其中读函数和写函数是读写功能函数的函数名。

如果你完成了DEVICE_ATTR函数宏的填充,下面就需要创建接口了

例如:

    static DEVICE_ATTR(polling, S_IRUGO | S_IWUSR, show_polling, set_polling);
    static struct attribute *dev_attrs[] = {
            &dev_attr_polling.attr,
            NULL,
    };

当你想要实现的接口名字是polling的时候,需要实现结构体struct attribute *dev_attrs[]

其中成员变量的名字必须是&dev_attr_polling.attr

然后再封装

    static struct attribute_group dev_attr_grp = {
            .attrs = dev_attrs,
    };

 

在利用sysfs_create_group(&pdev->dev.kobj, &dev_attr_grp);创建接口

通 过以上简单的三个步骤,就可以在adb shell 终端查看到接口了。当我们将数据 echo 到接口中时,在上层实际上完成了一次 write 操 作,对应到 kernel ,调用了驱动中的 “store”。同理,当我们cat 一个 接口时则会调用 “show” 。到这里,只是简单的建立 了 android 层到 kernel 的桥梁,真正实现对硬件操作的,还是在 "show" 和 "store" 中完成的。


######################################################################

以下来个例子:

#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/moduleparam.h>
#include <linux/slab.h>
#include <linux/types.h>
#include <linux/proc_fs.h>
#include <linux/fcntl.h>
#include <linux/uaccess.h>
#include <linux/gpio.h>
#include <linux/ioctl.h>
#include <linux/kobject.h>
#include <linux/sysfs.h>



MODULE_LICENSE("Dual BSD/GPL");

static int foo;

static ssize_t foo_show(struct kobject *kobject,struct kobj_attribute *attr,char *buf)
{
	printk("foo show\n");
	return sprintf(buf,"%d\n",foo);
}

static ssize_t foo_store(struct kobject *kobject,struct kobj_attribute *attr,const char *buf,size_t count)
{
	printk("foo store\n");
	return count;
}

static struct kobj_attribute foo_attribute = __ATTR(hellotest,0666,foo_show,foo_store);

static struct attribute *attrs[]={
		&foo_attribute.attr,
		NULL,
};

static struct attribute_group attr_group = {
	.attrs = attrs,
};

static struct kobject *example_kobj;

static int __init example_init(void)
{

int retval;

printk("example init\n");
example_kobj = kobject_create_and_add("hello",kernel_kobj);
if(!example_kobj)
return -1;

retval = sysfs_create_group(example_kobj,&attr_group);
if(retval)
	kobject_put(example_kobj);

	return retval;
}

static void __exit example_exit(void)
{
	kobject_put(example_kobj);
}

module_init(example_init);
module_exit(example_exit);



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值