解析linux或android添加文件系统的属性接口的方法

发布时间:2024-05-06 点击:69
服务器
第一种:
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: \\\\n, __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 \\\\n, __func__); } //sscanf(buf, %d, &otgid_status); pr_err(%s: otgid_status=%d \\\\n, __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: \\\\n, __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 addstatic 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\\\\n,__func__); } pr_err(android_device_create()::gpio134_otgid gpio_request ok\\\\n); gpio_direction_output(gpio134_otgid,1); if(otgid_status > 0){ //有自定义初始化状态就添加上这个判断,没有就不需要添加if else操作 pr_err(%s-gpio134_otgid pin set 1\\\\n, __func__); gpio_set_value(gpio134_otgid, 1); //msleep(5); }else{ pr_err(%s-gpio134_otgid pin set 0\\\\n, __func__); gpio_set_value(gpio134_otgid, 0); //msleep(5); } //end zhaojr add................................................................}5、在remove()函数中添加资源的释放
static int mdss_mdp_remove(struct platform_device *pdev){ struct mdss_data_type *mdata = platform_get_drvdata(pdev); if (!mdata) return -enodev; pr_err(%s\\\\n, __func__); gpio_free(gpio134_otgid); //zhaojr add free gpio otgid pin ........................................................}第二种方法:
在要添加驱动文件中没有搜索device_attr关键字的情况,如添加音频功放打开和关闭的控制接口:
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、定义全局变量和定义打开和关闭的接口并组织属性数组:
// add zhaojr gpio63 for close or speaker pa enablestruct kobject *spk_pa_kobj = null;unsigned int gpio63_spk_pa_gpio; //for speaker pa ic enable//extern unsigned int gpio63_spk_pa_gpio;static unsigned int spkpa_gpio_enable = 0;static ssize_t spkpaon_store(struct device *dev, struct device_attribute *attr,const char *buf, size_t count) { unsigned int ret=0; //ret = kstrtoint(buf, 10, &backlight_enable); ret = kstrtouint(buf, 10, &spkpa_gpio_enable); if (ret < 0){ pr_err(%s::kstrtouint() failed \\\\n, __func__); } pr_err(%s: spkpa_gpio_enable=%d \\\\n, __func__,spkpa_gpio_enable); if(spkpa_gpio_enable > 0){ //gpio_set_value(gpio63_spk_pa_gpio, 1); pr_err(%s: gpio_set_value gpio63 speaker pa enable \\\\n, __func__); //功放打开的时序 gpio_set_value(gpio63_spk_pa_gpio,0); udelay(8); gpio_set_value(gpio63_spk_pa_gpio,1); udelay(8); gpi

学生云服务器划算
阿里云服务器租赁和购买
账号变更公司-其他问题
云服务器1核1g秒杀价
这样做可以避免网站被降权
企业如何搭建网站 企业一般用什么云服务器比较好
阿里云1核与2核的服务器区别吗
刚买的阿里云服务器上不了网