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,
};
通过以上简单的三个步骤,就可以在adb shell 终端查看到接口了。当我们将数据 echo 到接口中时,在上层实际上完成了一次 write 操作,对应到 kernel ,调用了驱动中的 “store”。同理,当我们cat 一个 接口时则会调用 “show” 。到这里,只是简单的建立了 android 层到 kernel 的桥梁,真正实现对硬件操作的,还是在 "show" 和 "store" 中完成的。
######################################################################
以下来个例子:
/*
* Sample kobject implementation
*
* Copyright (C) 2004-2007 Greg Kroah-Hartman <greg@kroah.com>
* Copyright (C) 2007 Novell Inc.
*
* Released under the GPL version 2 only.
*
*/
#include <linux/kobject.h>
#include <linux/string.h>
#include <linux/sysfs.h>
#include <linux/module.h>
#include <linux/init.h>
#include <asm/gpio.h>
#include <linux/delay.h>
/*
* This module shows how to create a simple subdirectory in sysfs called
* /sys/kernel/kobject-example In that directory, 3 files are created:
* "foo", "baz", and "bar". If an integer is written to these files, it can be
* later read out of it.
*/
static int foo;
/*
* The "foo" file where a static variable is read from and written to.
*/
static struct msm_gpio qup_i2c_gpios_io[] = {
{ GPIO_CFG(60, 0, GPIO_CFG_OUTPUT, GPIO_CFG_NO_PULL, GPIO_CFG_8MA),
"qup_scl" },
{ GPIO_CFG(61, 0, GPIO_CFG_INPUT, GPIO_CFG_NO_PULL, GPIO_CFG_8MA),
"qup_sda" },
{ GPIO_CFG(131, 0, GPIO_CFG_OUTPUT, GPIO_CFG_NO_PULL, GPIO_CFG_8MA),
"qup_scl" },
{ GPIO_CFG(132, 0, GPIO_CFG_INPUT, GPIO_CFG_NO_PULL, GPIO_CFG_8MA),
"qup_sda" },
};
static struct msm_gpio qup_i2c_gpios_hw[] = {
{ GPIO_CFG(60, 1, GPIO_CFG_INPUT, GPIO_CFG_NO_PULL, GPIO_CFG_8MA),
"qup_scl" },
{ GPIO_CFG(61, 1, GPIO_CFG_INPUT, GPIO_CFG_NO_PULL, GPIO_CFG_8MA),
"qup_sda" },
{ GPIO_CFG(131, 2, GPIO_CFG_INPUT, GPIO_CFG_NO_PULL, GPIO_CFG_8MA),
"qup_scl" },
{ GPIO_CFG(132, 2, GPIO_CFG_INPUT, GPIO_CFG_NO_PULL, GPIO_CFG_8MA),
"qup_sda" },
};
static void gsbi_qup_i2c_gpio_config(int adap_id, int config_type)
{
int rc;
if (adap_id < 0 || adap_id > 1)
return;
/* Each adapter gets 2 lines from the table */
if (config_type)
rc = msm_gpios_enable(&qup_i2c_gpios_hw[adap_id*2], 2);
else
rc = msm_gpios_enable(&qup_i2c_gpios_io[adap_id*2], 2);
if (rc < 0)
pr_err("QUP GPIO request/enable failed: %d\n", rc);
}
static ssize_t foo_show(struct kobject *kobj, struct kobj_attribute *attr,
char *buf)
{
return sprintf(buf, "%d\n", foo);
}
static ssize_t foo_store(struct kobject *kobj, struct kobj_attribute *attr,
const char *buf, size_t count)
{
int sda,scl;
int i;
sscanf(buf, "%du", &foo);
printk("foo = %d.\n",foo);
foo = foo -1;
if (foo < 0 || foo > 1)
{
printk("input foo error. foo=%d.\n",foo);
return 0;
}
sda = GPIO_PIN((&qup_i2c_gpios_hw[foo*2+1])->gpio_cfg);
scl = GPIO_PIN((&qup_i2c_gpios_hw[foo*2])->gpio_cfg);
printk("sda = %d.\n",sda);
printk("scl = %d.\n",scl);
gsbi_qup_i2c_gpio_config(foo,0);
for(i=0;i<9;i++)
{
if(gpio_get_value(sda))
{
printk("sda of i2c%d is high when %d pulse is output on scl.\n",foo,i);
break;
}
gpio_set_value(scl,0);
udelay(5);
gpio_set_value(scl,1);
udelay(5);
}
gsbi_qup_i2c_gpio_config(foo,1);
printk("finish.\n");
return count;
}
static struct kobj_attribute foo_attribute =
__ATTR(i2c_unlock, 0666, foo_show, foo_store);
/*
* Create a group of attributes so that we can create and destroy them all
* at once.
*/
static struct attribute *attrs[] = {
&foo_attribute.attr,
NULL,» /* need to NULL terminate the list of attributes */
};
/*
* An unnamed attribute group will put all of the attributes directly in
* the kobject directory. If we specify a name, a subdirectory will be
* created for the attributes with the directory being the name of the
* attribute group.
*/
static struct attribute_group attr_group = {
.attrs = attrs,
};
static struct kobject *example_kobj;
static int __init example_init(void)
{
int retval;
/*
* Create a simple kobject with the name of "kobject_example",
* located under /sys/kernel/
*
* As this is a simple directory, no uevent will be sent to
* userspace. That is why this function should not be used for
* any type of dynamic kobjects, where the name and number are
* not known ahead of time.
*/
example_kobj = kobject_create_and_add("i2c_recovery", kernel_kobj);
if (!example_kobj)
return -ENOMEM;
/* Create the files associated with this kobject */
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);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Wupeng");
sysfs接口函数的建立_DEVICE_ATTR相关推荐
- Linux内核编程接口函数
Linux内核编程接口函数 转载请注明出处: http://blog.csdn.net/drivelinux/article/details/8656280 字符设备相关函数 1.alloc_chrd ...
- 【嵌入式】Libmodbus源码分析(二)-常用接口函数分析
00. 目录 文章目录 00. 目录 01. modbus常用接口函数概述 02. modbus辅助接口函数 03. modbus功能接口函数 04. modbus数据处理 05. 附录 01. mo ...
- 【转】libpcap实现机制及接口函数
转自:libpcap实现机制及接口函数 - 简书 1.Libpcap 的工作原理 Libpcap的工作原理可以描述为,当一个数据包到达网卡时,通过网络分接口(即旁路机制)将数据包发给BPF过滤器,匹配 ...
- linux sht11驱动,SHT11.h下载 温湿度传感器SHT11的驱动程序,提供了外界调用接口函数...
/******************************************************************* *[文 件 名]:SHT11.h ...
- java中没有直接调用matlab的接口函数
java中没有直接调用matlab的接口函数,在通常的情况下我们是通过借用C++作为中间过程来实现的,使用C++主要是用c与matlab的结构实现c调用matlab的功能,将这些功能函数封装成dll文 ...
- 加密机PKCS#11国际加密标准解读-概念和常用接口函数(1)
PKCS#11是使用非常普遍的密码设备接口,在实际应用中,国密的密码设备应用接口规范GMT0018与之作用相同,在技术体系架构中处于类似的位置. 在密码产品的开发中,按照PKCS#11或者GMT001 ...
- Hook Com接口函数
标 题: [原创]COM接口函数通用Hook方法 作 者: zhangluduo 时 间: 2014-12-08,22:34:20 链 接: http://bbs.pediy.com/showthr ...
- WinCE中串口驱动及接口函数介绍(转载)
作者:ARM-WinCE 在WinCE中,串口驱动实际上就是一个流设备驱动,具体架构如图: 串口驱动本身分为MDD层和PDD层.MDD层对上层的Device Manager提供了标准的流设备驱动接口( ...
- python如何调用cpp文件的接口函数_C++中嵌入Python调用
python嵌入到C++中 把python嵌入的C++里面需要做一些步骤 安装python程序,这样才能使用python的头文件和库 在我们写的源文件中增加"Python.h"头文 ...
最新文章
- SQL Developer更改日期显示格式
- PMCAFF微课堂(已结束) | 典典养车新媒体负责人亲授:如何运营百万级企业服务号
- 做安全操作系统,这位技术老兵是认真的!
- 飞鸽传书整个逻辑盲写,不做测试
- 9.11两点间距离(1636050091)
- 查看Linux连接数
- 【备忘】Windows的命令行下设置网络代理
- TStrings的一些技巧(转)
- Android10加入APEX目的
- 所谓的日常 #10 - 勤王室馬騰舉義 報父仇曹操興師
- JQuery Tables 的应用(二)
- word写论文公式编号问题
- 菲尼克斯电源模块UNO-PS1AC24DC100W的组装
- 四阶龙格库塔方法求解一次常微分方程组
- 万人连麦的幕后技术详解
- 是不是选择任何一个方向,都会游向同一个宿命呢
- 浏览器:CSDN的浏览器助手使用推荐
- 图像超分:HAN(Single Image Super-Resolution via a Holistic Attention Network)
- Java中如何使用帮助文档(API)
- 1481: 考试排名(一)(结构体专题)
热门文章
- 我自横刀向天笑,我命由我不由天
- python多项式回归_在python中实现多项式回归
- 5187. 收集足够苹果的最小花园周长
- 超链接href属性_如何使用标签上的HREF属性制作HTML超链接
- 使用这些HTTP标头保护您的Web应用程序
- 终端打开后-bash_如何爵士化Bash终端-带有图片的分步指南
- python:单例模式--使用__new__(cls)实现
- ubuntu 18.04 vi里面方向键变成abcd 处理办法
- 如何使用Redis做MySQL的缓存
- Linux入门之常用命令(15) lsof