驱动学习之调试驱动DEVICE_ATTR的原理及用法

1.首先来看下DEVICE_ATTR的 定义

#define DEVICE_ATTR(_name, _mode, _show, _store) \

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

_name:名称,也就是将在sys fs中生成的文件名称。
_mode:上述文件的访问权限,与普通文件相同,UGO的格式。
_show:显示函数,cat该文件时,此函数被调用。
_store:写函数,echo内容到该文件时,此函数被调用。


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

如对设备的使用        DEVICE_ATTR   

对驱动使用               DRIVER_ATTR

对总线使用               BUS_ATTR 

对类别 (class) 使用  CLASS_ATTR

这四个高级的宏来自于<include/linux/device.h> 

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


2.举例说明:

如果我们要实现

static DEVICE_ATTR(ftstpfwver, S_IRUGO|S_IWUSR, fts_tpfwver_show, fts_tpfwver_store);----------①


那就应该这样定义:

static struct attribute *fts_attributes[] = {-----------------------------------------------------②
&dev_attr_ftstpfwver.attr,
};

绿色的必须一样,前面要加上dev_attr_这个,规定的,不要问为什么。

然后还要:

static struct attribute_group fts_attribute_group = {-----------------------------------------③
.attrs = fts_attributes
};

再利用sysfs_create_group(&client->dev.kobj, &fts_attribute_group)---------------------------------------④

创建接口

紫色部分也应该一样。


通过以上④步骤就完成了,可以在adb shell 终端通过 echo和cat查看到接口的信息了。

原理:当我们将数据 echo 到接口中时,在上层实际上完成了一次 write 操作,对应到 kernel ,调用了驱动中的 “store”。同理,当我们cat 一个 接口时则会调用 “show” 。到这里,只是简单的建立了 android 层到 kernel 的桥梁,真正实现对硬件操作的,还是在 "show" 和 "store" 中完成的。用过proc文件系统的就知道,这个就和proc中的write和read一样的,以我的理解:proc有点老了,以后肯定会大量使用attribute,proc好比是Windows XP,attribute就像是Windows Seven。

3.那应该怎样去使用吗?

通过adb连接,在shell下输入以下:

   1.cd 到对应目录,我这上面是 cd /sys/devices/platform/fts_ts 

2.然后cat ftstpfwver此时可以读出该接口的信息,也就是执行fts_tpfwver_show这个函数

3.echo 01>ftstpfwver 这样就执行fts_tpfwver_store

猜你喜欢

转载自blog.csdn.net/xzx208/article/details/80632467
今日推荐