解析解析linux或或android添加文件系统的属性接口的方法添加文件系统的属性接口的方法
第一种:第一种:
1、添加关键头文件:
#include <linux/of_gpio.h>
#include <linux/gpio.h>
#include <linux/delay.h>
#include <linux/module.h>
#include <linux/types.h>
#include <linux/kobject.h>
2、在已经存在驱动文件中搜索”DEVICE_ATTR”关键字,如果存在,直接参考已经存在的方法添加一个即可,如下:
unsigned int Gpio134_OtgID = 134; //定义全局变量
static unsigned int otgid_status = 1;
…
3、定义文件系统的读写函数:
//add zhaojr gpio134 control OTG ID for host or device mode
static ssize_t setotgid_store(struct device *dev, struct device_attribute *attr,const char *buf, size_t count)
{
unsigned int ret=0;
pr_err("%s: ", __func__);
//ret = kstrtoint(buf, 10, &otgid_status);
ret = kstrtouint(buf, 10, &otgid_status);
//sscanf(buf, "%lu", &otgid_status);
if (ret < 0){
pr_err("%s::kstrtouint() failed ", __func__);
}
//sscanf(buf, "%d", &otgid_status);
pr_err("%s: otgid_status=%d ", __func__,otgid_status);
if(otgid_status > 0){
gpio_set_value(Gpio134_OtgID, 1);
}else{
gpio_set_value(Gpio134_OtgID, 0);
}
return count;
}
static ssize_t setotgid_show(struct device *dev,struct device_attribute *attr, char *buf)
{
pr_err("%s: ", __func__);
return sprintf(buf, "%d\n",otgid_status);
}
//static DEVICE_ATTR_RW(setotgid);
/*struct device_attribute dev_attr_setotgid = {
.attr = {.name ="setotgid",
.mode = 0664},
.show = setotgid_show,
.store = setotgid_store,
};*/
//setotgid的一致性,第一个参数setotgid和setotgid_show、setotgid_store前钻必须保持一致
static DEVICE_ATTR(setotgid, 0664, setotgid_show, setotgid_store);
//end zhaojr add
static struct device_attribute *android_usb_attributes[] = {
&dev_attr_state,
&dev_attr_setotgid, //setotgid跟DEVICE_ATTR定义的name必须保持一致
NULL
};
4、在probe()函数中定义针对具体GPIO管脚的请求和初始化
static int mdss_mdp_probe(struct platform_device *pdev)
{
....................................................................................
//zhaojr add for gpio134 to usb host or device mode
ret_status=gpio_request(Gpio134_OtgID, "Gpio134-OtgID");
if(ret_status<0){
pr_err("usb gadget configfs %s::Gpio134_OtgID gpio_request failed",__func__);
}
pr_err("android_device_create()::Gpio134_OtgID gpio_request OK");