[Android]Linux kernel 開發基礎 (五)- driver attribute

首頁 >> google協作平臺 >> [Android]Linux kernel 開發基礎 (五)- driver attribute

透過driver attribute的讀寫,

可以讓使用者用讀寫檔案方式,直接跟driver溝通,

這在debug與其他driver之間互動提供了簡便的方法,

這方法就是今天要介紹的

Linux kernel 開發基礎 (五)- driver attribute

只需要兩個步驟:

1. 定義attribute本身:


static ssize_t hello_show(struct device *dev,struct device_attribute *attr, char *buf)
{

printk(“hello_show n”);

return 0;
}
static ssize_t hello_store(struct device *dev,struct device_attribute *attr,const char *buf, size_t size)
{
printk(“hello_store n”);
return -1;
}

static struct device_attribute hello_adv_setting = {
.attr = {
.name = “hello_adv”,
.mode = 0664,
},
.show = hello_show,
.store = hello_store,
};

static struct device_attribute *hello_adv_group[] = {
&hello_adv_setting,
// &hello_adv_setting2,

};

2. 在driver建立attribute檔案


for (i = 0; i < ARRAY_SIZE(hello_adv_group); i++) { ret = device_create_file(&pdev->dev,hello_adv_group[i]);
if (ret) {
dev_err(&pdev->dev, "failed: sysfs file %sn",hello_adv_group[i]->attr.name);
}
}

如果還不是很明白,

可以下載範例程式來觀看

 



================================
分享與讚美,是我們繼續打拼的原動力.
若文章對您有幫助,望請不吝按讚或分享.
或者對影片有興趣可以訂閱頻道接收通知
================================
YouTube 頻道
FB 粉絲專頁
================================

guangyaw

重點主題: 程式設計: Python , Django,Android 工具與軟體: Open edX,Linux工具,Blender教學 分享各地美景與產品使用心得,遊戲實況,甚至影視戲劇等, 您的訂閱就是頻道成長的原動力。 YouTube 頻道: https://youtube.com/xyawli

You may also like...

發表迴響