2015年5月23日 星期六

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

透過driver attribute的讀寫,

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

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

要在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 %s\n",hello_adv_group[i]->attr.name);
}
}

如果還不是很明白,

可以下載範例程式來觀看

沒有留言: