我是靠谱客的博主 老实小虾米,最近开发中收集的这篇文章主要介绍Linux dev_attr 设备文件操作,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

1. 写法A:

static ssize_t xxx_show(struct device *dev,
        struct device_attribute *attr, char *buf)
{
	return sprintf(buf,"show somethingn");
}

static ssize_t xxx_store(struct device *dev,
        struct device_attribute *attr, const char *buf,
        size_t count)
{
#if 1 //字符串判断
	char on = *buf;
	if(on == '0'){
		printk("to do something An");
	}
	if(on == '1'){
		printk("to do something Bn");
	}
#else //字符转int 判断
    unsigned long value;
    int ret;
    ret = strict_strtoul(buf,10,&value);//strtoul(buf,10,&value);
    //或者 str 转 unsigned int
    //unsigned int cmd;
    //ret = kstrtouint(buf,10,&cmd);
    //
    if(ret){
        return ret;
    }
    switch((int)value){
        case 0:
            printk("to do something An");
            break;
        case 1:
            printk("to do something Bn");
            break;
        case 2:
            printk("to do something Cn");
            break;
        default:
            printk("inval param.")
    }
#endif
	return count;
}

static struct device_attribute xxx_dev_attr = {
    .attr = {
    .name = "xxx_state",
	.mode = S_IRWXU|S_IRWXG|S_IRWXO,
    },
    .show = xxx_show,
    .store = xxx_store,
};

static int xxx_probe(struct platform_device *pdev)
{
    int ret = 0;
    ...

    //创建设备操控文件节点
    ret = device_create_file(&(pdev->dev), &xxx_dev_attr);
	if (ret) {
		printk("[xxx_probe] sys file creation failedn");
	}
}

static int xxx_remove(struct platform_device *pdev)
{
    ...
    device_remove_file(&pdev->dev,&xxx_dev_attr);
	return 0;
}

2. 写法B:

static ssize_t xxx_show(struct device *dev,
        struct device_attribute *attr, char *buf)
{
	return sprintf(buf,"show somethingn");
}

static ssize_t xxx_store(struct device *dev,
        struct device_attribute *attr, const char *buf,
        size_t count)
{
#if 1 //字符串判断
	char on = *buf;
	if(on == '0'){
		printk("to do something An");
	}
	if(on == '1'){
		printk("to do something Bn");
	}
#else //字符转int 判断
    unsigned long value;
    int ret;
    ret = strict_strtoul(buf,10,&value)
    if(ret){
        return ret;
    }
    switch((int)value){
        case 0:
            printk("to do something An");
            break;
        case 1:
            printk("to do something Bn");
            break;
        case 2:
            printk("to do something Cn");
            break;
        default:
            printk("inval param.")
    }
#endif
	return count;
}

//DEVICE_ATTR声明
static DEVICE_ATTR(xxx_state,S_IRWXU|S_IRWXG|S_IRWXO,xxx_show,xxx_store);

static int xxx_probe(struct platform_device *pdev)
{
    int ret = 0;
    ...

    //创建设备操控文件节点
    ret = device_create_file(&(pdev->dev), &dev_attr_xxx_state);
	if (ret) {
		printk("[xxx_probe] sys file creation failedn");
	}
}

static int xxx_remove(struct platform_device *pdev)
{
    ...
    device_remove_file(&pdev->dev,&dev_attr_xxx_state);
	return 0;
}

 

最后

以上就是老实小虾米为你收集整理的Linux dev_attr 设备文件操作的全部内容,希望文章能够帮你解决Linux dev_attr 设备文件操作所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(56)

评论列表共有 0 条评论

立即
投稿
返回
顶部