溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

linux或android怎么添加文件系統(tǒng)的屬性接口

發(fā)布時(shí)間:2022-04-07 16:53:09 來(lái)源:億速云 閱讀:145 作者:iii 欄目:編程語(yǔ)言

這篇文章主要介紹“l(fā)inux或android怎么添加文件系統(tǒng)的屬性接口”的相關(guān)知識(shí),小編通過(guò)實(shí)際案例向大家展示操作過(guò)程,操作方法簡(jiǎn)單快捷,實(shí)用性強(qiáng),希望這篇“l(fā)inux或android怎么添加文件系統(tǒng)的屬性接口”文章能幫助大家解決問(wèn)題。

第一種:

1、添加關(guān)鍵頭文件:

#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、在已經(jīng)存在驅(qū)動(dòng)文件中搜索"device_attr"關(guān)鍵字,如果存在,直接參考已經(jīng)存在的方法添加一個(gè)即可,如下:

unsigned int gpio134_otgid = 134; //定義全局變量
static unsigned int otgid_status = 1;
…

3、定義文件系統(tǒng)的讀寫(xiě)函數(shù):

//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的一致性,第一個(gè)參數(shù)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()函數(shù)中定義針對(duì)具體gpio管腳的請(qǐng)求和初始化

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){ //有自定義初始化狀態(tài)就添加上這個(gè)判斷,沒(méi)有就不需要添加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()函數(shù)中添加資源的釋放

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
 ........................................................
}

第二種方法:

在要添加驅(qū)動(dòng)文件中沒(méi)有搜索"device_attr"關(guān)鍵字的情況,如添加音頻功放打開(kāi)和關(guān)閉的控制接口:
1、添加關(guān)鍵頭文件:

#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、定義全局變量和定義打開(kāi)和關(guān)閉的接口并組織屬性數(shù)組:

// add zhaojr gpio63 for close or speaker pa enable
struct 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__);
 //功放打開(kāi)的時(shí)序
 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_value gpio63 speaker pa disable \n", __func__);
 //功放關(guān)閉的時(shí)序
 gpio_set_value(gpio63_spk_pa_gpio,0);
 udelay(600);
 //sdm660_cdc->ext_spk_pa_set = false; 
 }
 return count;
}
static ssize_t spkpaon_show(struct device *dev,struct device_attribute *attr, char *buf) 
{ 
 return sprintf(buf, "%d\n",spkpa_gpio_enable);
} 
static device_attr(spkpaon, 0664, spkpaon_show, spkpaon_store);
static struct attribute *spkpa_attributes[] = {
 &dev_attr_spkpaon.attr,
 null
};
static const struct attribute_group apkpa_attr_group = {
 .attrs = spkpa_attributes,
 null
};
//end zhaojr add

3、在probe()函數(shù)中添加文件系統(tǒng)屬性接口的注冊(cè):
在注冊(cè)的時(shí)候并不需要對(duì)功放進(jìn)行初始化,所以probe()函數(shù)中并沒(méi)有對(duì)sdm660_cdc->spk_pa_gpio(gpio63),只操作了請(qǐng)求。具體的請(qǐng)求操作請(qǐng)參考:msm8953 audio部分的ear和speaker輸出的聲音配置中的音頻部分的

vendor/qcom/opensource/audio-kernel/asoc/codecs/sdm660_cdc/msm-analog-cdc.c文件操作

static int msm_anlg_cdc_probe(struct platform_device *pdev)
{
 int ret = 0;
 struct sdm660_cdc_priv *sdm660_cdc = null;
 struct sdm660_cdc_pdata *pdata;
 int adsp_state;
 ..................................
 dev_set_drvdata(&pdev->dev, sdm660_cdc);
 //kangting add
 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_gpio not specified\n",__func__);
 else{
 pr_err("%s, sdm660_cdc->spk_pa_gpio is %d\n",__func__,sdm660_cdc->spk_pa_gpio);
 ret = gpio_request(sdm660_cdc->spk_pa_gpio, "spk_pa");
 if (ret) {
 pr_err("request spk_pa_gpio failed, ret=%d\n",ret);
 gpio_free(sdm660_cdc->spk_pa_gpio);
 }
 }
 //kangting end
 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_codec failed with error %d\n",
 __func__, ret);
 goto err_supplies;
 }
 blocking_init_notifier_head(&sdm660_cdc->notifier);
 blocking_init_notifier_head(&sdm660_cdc->notifier_mbhc);
 //add by zhaojr
 gpio63_spk_pa_gpio = sdm660_cdc->spk_pa_gpio; //將設(shè)備樹(shù)種定義的io口號(hào)獲取進(jìn)來(lái)
 spk_pa_kobj = kobject_create_and_add("spk_pa", null); //創(chuàng)建/sys/spk_pa/目錄
 ret = sysfs_create_group(spk_pa_kobj, &apkpa_attr_group); //創(chuàng)建/sys/class/spk_pa/spkpaon節(jié)點(diǎn)
 if (ret)
 dev_err(&pdev->dev,"%s:sysfs_create_group failed with error\n",__func__);
 //end zhaojr add 
 ....................................

4、在remove函數(shù)中釋放資源

static int msm_anlg_cdc_remove(struct platform_device *pdev)
{
 struct sdm660_cdc_priv *sdm660_cdc = dev_get_drvdata(&pdev->dev);
 struct sdm660_cdc_pdata *pdata = sdm660_cdc->dev->platform_data;
 int count;
 //add by zhaojr //釋放資源
 gpio_free(sdm660_cdc->spk_pa_gpio);
 kobject_put(spk_pa_kobj); //關(guān)鍵函數(shù)
 sysfs_remove_group(spk_pa_kobj, &apkpa_attr_group); //關(guān)鍵函數(shù)
 //end zhaojr add
 for (count = 0; count < sdm660_cdc->child_count &&
 count < anlg_cdc_child_devices_max; count++)
 platform_device_unregister(
 sdm660_cdc->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);
 return 0;
}

關(guān)于“l(fā)inux或android怎么添加文件系統(tǒng)的屬性接口”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí),可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會(huì)為大家更新不同的知識(shí)點(diǎn)。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI