解析linux或android添加文件系统的属性接口的方法
第一种:
1、添加关键头文件:
#include#include #include #include #include #include
2、在已经存在驱动文件中搜索"DEVICE_ATTR"关键字,如果存在,直接参考已经存在的方法添加一个即可,如下:
unsignedintGpio134_OtgID=134;//定义全局变量 staticunsignedintotgid_status=1; …
3、定义文件系统的读写函数:
//addzhaojrgpio134controlOTGIDforhostordevicemode staticssize_tsetotgid_store(structdevice*dev,structdevice_attribute*attr,constchar*buf,size_tcount) { unsignedintret=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); } returncount; } staticssize_tsetotgid_show(structdevice*dev,structdevice_attribute*attr,char*buf) { pr_err("%s:\n",__func__); returnsprintf(buf,"%d\n",otgid_status); } //staticDEVICE_ATTR_RW(setotgid); /*structdevice_attributedev_attr_setotgid={ .attr={.name="setotgid", .mode=0664}, .show=setotgid_show, .store=setotgid_store, };*/ //setotgid的一致性,第一个参数setotgid和setotgid_show、setotgid_store前钻必须保持一致 staticDEVICE_ATTR(setotgid,0664,setotgid_show,setotgid_store); //endzhaojradd staticstructdevice_attribute*android_usb_attributes[]={ &dev_attr_state, &dev_attr_setotgid,//setotgid跟DEVICE_ATTR定义的name必须保持一致 NULL };
4、在probe()函数中定义针对具体GPIO管脚的请求和初始化
staticintmdss_mdp_probe(structplatform_device*pdev) { .................................................................................... //zhaojraddforgpio134tousbhostordevicemode ret_status=gpio_request(Gpio134_OtgID,"Gpio134-OtgID"); if(ret_status<0){ pr_err("usbgadgetconfigfs%s::Gpio134_OtgIDgpio_requestfailed\n",__func__); } pr_err("android_device_create()::Gpio134_OtgIDgpio_requestOK\n"); gpio_direction_output(Gpio134_OtgID,1); if(otgid_status>0){//有自定义初始化状态就添加上这个判断,没有就不需要添加ifelse操作 pr_err("%s-Gpio134_OtgIDpinset1\n",__func__); gpio_set_value(Gpio134_OtgID,1); //msleep(5); }else{ pr_err("%s-Gpio134_OtgIDpinset0\n",__func__); gpio_set_value(Gpio134_OtgID,0); //msleep(5); } //endzhaojradd ................................................................ }
5、在remove()函数中添加资源的释放
staticintmdss_mdp_remove(structplatform_device*pdev) { structmdss_data_type*mdata=platform_get_drvdata(pdev); if(!mdata) return-ENODEV; pr_err("%s\n",__func__); gpio_free(Gpio134_OtgID);//zhaojraddfreegpiootgidpin ........................................................ }
第二种方法:
在要添加驱动文件中没有搜索"DEVICE_ATTR"关键字的情况,如添加音频功放打开和关闭的控制接口:
1、添加关键头文件:
#include#include #include #include #include #include
2、定义全局变量和定义打开和关闭的接口并组织属性数组:
//addzhaojrgpio63forcloseorspeakerpaenable structkobject*spk_pa_kobj=NULL; unsignedintgpio63_spk_pa_gpio;//forspeakerpaicenable //externunsignedintgpio63_spk_pa_gpio; staticunsignedintSpkPa_Gpio_Enable=0; staticssize_tspkpaon_store(structdevice*dev,structdevice_attribute*attr,constchar*buf,size_tcount) { unsignedintret=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_valuegpio63speakerpaenable\n",__func__); //功放打开的时序 gpio_set_value(gpio63_spk_pa_gpio,0); udelay(8); gpio_set_value(gpio63_spk_pa_gpio,1); udelay(8); gpio_set_value(gpio63_spk_pa_gpio,0); udelay(8); gpio_set_value(gpio63_spk_pa_gpio,1); //sdm660_cdc->ext_spk_pa_set=true; }else{ pr_err("%s:gpio_set_valuegpio63speakerpadisable\n",__func__); //功放关闭的时序 gpio_set_value(gpio63_spk_pa_gpio,0); udelay(600); //sdm660_cdc->ext_spk_pa_set=false; } returncount; } staticssize_tspkpaon_show(structdevice*dev,structdevice_attribute*attr,char*buf) { returnsprintf(buf,"%d\n",SpkPa_Gpio_Enable); } staticDEVICE_ATTR(spkpaon,0664,spkpaon_show,spkpaon_store); staticstructattribute*spkpa_attributes[]={ &dev_attr_spkpaon.attr, NULL }; staticconststructattribute_groupapkpa_attr_group={ .attrs=spkpa_attributes, NULL }; //endzhaojradd
3、在probe()函数中添加文件系统属性接口的注册:
在注册的时候并不需要对功放进行初始化,所以probe()函数中并没有对sdm660_cdc->spk_pa_gpio(GPIO63),只操作了请求。具体的请求操作请参考:msm8953audio部分的EAR和Speaker输出的声音配置中的音频部分的
vendor/qcom/opensource/audio-kernel/asoc/codecs/sdm660_cdc/msm-analog-cdc.c文件操作
staticintmsm_anlg_cdc_probe(structplatform_device*pdev) { intret=0; structsdm660_cdc_priv*sdm660_cdc=NULL; structsdm660_cdc_pdata*pdata; intadsp_state; .................................. dev_set_drvdata(&pdev->dev,sdm660_cdc); //kangtingadd sdm660_cdc->spk_pa_gpio=of_get_named_gpio(pdev->dev.of_node,"qcom,speaker-pa",0); if(!gpio_is_valid(sdm660_cdc->spk_pa_gpio)) pr_err("%s,sdm660_cdc->spk_pa_gpionotspecified\n",__func__); else{ pr_err("%s,sdm660_cdc->spk_pa_gpiois%d\n",__func__,sdm660_cdc->spk_pa_gpio); ret=gpio_request(sdm660_cdc->spk_pa_gpio,"spk_pa"); if(ret){ pr_err("requestspk_pa_gpiofailed,ret=%d\n",ret); gpio_free(sdm660_cdc->spk_pa_gpio); } } //kangtingend ret=snd_soc_register_codec(&pdev->dev, &soc_codec_dev_sdm660_cdc, msm_anlg_cdc_i2s_dai, ARRAY_SIZE(msm_anlg_cdc_i2s_dai)); if(ret){ dev_err(&pdev->dev, "%s:snd_soc_register_codecfailedwitherror%d\n", __func__,ret); gotoerr_supplies; } BLOCKING_INIT_NOTIFIER_HEAD(&sdm660_cdc->notifier); BLOCKING_INIT_NOTIFIER_HEAD(&sdm660_cdc->notifier_mbhc); //addbyzhaojr gpio63_spk_pa_gpio=sdm660_cdc->spk_pa_gpio;//将设备树种定义的IO口号获取进来 spk_pa_kobj=kobject_create_and_add("spk_pa",NULL);//创建/sys/spk_pa/目录 ret=sysfs_create_group(spk_pa_kobj,&apkpa_attr_group);//创建/sys/class/spk_pa/spkpaon节点 if(ret) dev_err(&pdev->dev,"%s:sysfs_create_groupfailedwitherror\n",__func__); //endzhaojradd ....................................
4、在remove函数中释放资源
staticintmsm_anlg_cdc_remove(structplatform_device*pdev) { structsdm660_cdc_priv*sdm660_cdc=dev_get_drvdata(&pdev->dev); structsdm660_cdc_pdata*pdata=sdm660_cdc->dev->platform_data; intcount; //addbyzhaojr//释放资源 gpio_free(sdm660_cdc->spk_pa_gpio); kobject_put(spk_pa_kobj);//关键函数 sysfs_remove_group(spk_pa_kobj,&apkpa_attr_group);//关键函数 //endzhaojradd for(count=0;countchild_count&& count pdev_child_devices[count]); snd_soc_unregister_codec(&pdev->dev); msm_anlg_cdc_disable_supplies(sdm660_cdc,pdata); wcd9xxx_spmi_irq_exit(); devm_kfree(&pdev->dev,sdm660_cdc); return0; }
总结
到此这篇关于解析linux或android添加文件系统的属性接口的方法的文章就介绍到这了,更多相关linux文件系统的属性接口内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。