DEVICE_ATTR实例分析
在內核中, sysfs 屬性一般是由 __ATTR 系列的宏來聲明的,如對設備的使用 DEVICE_ATTR ,對總線使用 BUS_ATTR ,對驅動使用 DRIVER_ATTR ,對類別(class)使用 CLASS_ATTR, 這四個高級的宏來自于 <include/linux/device.h>, 都是以更低層的來自 <include/linux/sysfs.h> 中的 __ATTR/__ATRR_RO 宏實現。
在adb?shell?終端查看到接口,當我們將數據?echo?到接口中時,在上層實際上完成了一次?write?操作,對應到?kernel?,調用了驅動中的?“store”。同理,當我們cat?一個?接口時則會調用?“show”?。到這里,只是簡單的建立了?android?層到?kernel?的橋梁,真正實現對硬件操作的,還是在?"show"?和?"store"?中完成的。
?
實現辦法,例一:
#include <linux/platform_device.h>
1)
static ssize_t rohm_proximity_show_debug(struct device* cd,struct device_attribute *attr, char* buf)
{
??? ssize_t ret = 0;
????
??? sprintf(buf, "ROHM Debug %d\n",debug_level);//將格式數據寫到緩沖里面,此處為buf
????
??? ret = strlen(buf) + 1;
??? return ret;
}
static ssize_t rohm_proximity_store_debug(struct device* cd, struct device_attribute *attr,
?????????????? const char* buf, size_t len)
{
??? unsigned long on_off = simple_strtoul(buf, NULL, 10);//解析字符串cp?中?8,10,16?進制數字???,返回值是解析的數字,endp?指向字符串起始處,base :進制
??? debug_level = on_off;
??? printk("%s: debug_level=%d\n",__func__, debug_level);
????
??? return len;
}
2)
static DEVICE_ATTR(debug, S_IRUGO | S_IWUSR, rohm_proximity_show_debug, rohm_proximity_store_debug);
?3)
static int rohm_proximity_create_sysfs(struct platform_device *client)
{
??? struct device *dev = &(client->dev);
??? int err = 0;
??? PS_DBG("%s\n", __func__);
??? if ((err = device_create_file(dev, &dev_attr_control)))
??????? goto err_out;
??? if ((err = device_create_file(dev, &dev_attr_debug)))
??????? goto err_out;
??? return 0;
err_out:
??? return err;
}
4)
static int rohm_proximity_probe(struct platform_device *pdev)
{
???????? rohm_proximity_create_sysfs(pdev);
}
?
實現辦法,例二:
1)
static ssize_t mc32x0_threshold_show(struct device *dev,
???????????????????? struct device_attribute *attr, char *buf)
{
#ifdef MC32X0_DEBUG
?????? printk("mcube %s\n",__FUNCTION__);
#endif
??? return sprintf(buf, "%d\n", mc32x0_get_threshold(dev));
}
static ssize_t mc32x0_threshold_store(struct device *dev,
????????????????????? struct device_attribute *attr,
????????????????????? const char *buf, size_t count)
{
??? unsigned long threshold;
#ifdef MC32X0_DEBUG
?????? printk("mcube %s\n",__FUNCTION__);
#endif
??? threshold = simple_strtoul(buf, NULL,10);
??????? if (threshold >= 0 && threshold <= ABSMAX_8G) {
??????? mc32x0_set_threshold(dev, threshold);
??????? }???
??? return count;
}
2)
static DEVICE_ATTR(threshold, 0666,??? mc32x0_threshold_show, mc32x0_threshold_store);
static struct attribute *mc32x0_attributes[] = {
??//? &dev_attr_enable.attr,
???// &dev_attr_delay.attr,
??//? &dev_attr_position.attr,
??? &dev_attr_threshold.attr,
};
3)
static struct attribute_group mc32x0_attribute_group = {
??? .attrs =?mc32x0_attributes
};
?
4)
static int mc32x0_probe(struct i2c_client *client, const struct i2c_device_id *id)
{
??? err = sysfs_create_group(&mc32x0->input->dev.kobj, &mc32x0_attribute_group);//.probe中生成
??? if (err < 0) {
??????? goto error_2;
??? }
??? //sysfs_remove_group(&mc32x0->input->dev.kobj, &mc32x0_attribute_group);//.remove中移除
?
}
adb下輸入:(以下節點為DEVICE_ATTR(debug,,)同為,位置在/sys/devices/platform/rohm_proximity/,在要/目錄下find . -name "debug"查找)
#cat debug???????? //此端口下會輸出"ROHM Debug XX",XX為此處值。注:另一adb端口cat /proc/kmsg不會顯示此字符
#echo 01>debug?//在另外一個adb端口下cat /proc/kmsg會顯示"rohm_proximity_store_debug: debug_level=1"。
注:echo 01>debug值為1,echo 1>debug值為0,echo 0>debug無效.
此時已給此端口寫1,再#cat debug,端口下會輸出"ROHM Debug 1"
?
參考文檔:http://blog.chinaunix.net/uid-26413351-id-3180609.html
總結
以上是生活随笔為你收集整理的DEVICE_ATTR实例分析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 虚拟机安装Ubuntu14.04打开Fi
- 下一篇: 内核驱动中常见的miscdevice、p