zoukankan      html  css  js  c++  java
  • 47.Linux-DEVICE_ATTR()介绍及使用示例

    1.介绍

    使用DEVICE_ATTR,可以实现驱动在sys目录自动创建文件,我们只需要实现show和store函数即可.

    然后在应用层就能通过cat和echo命令来对sys创建出来的文件进行读写驱动设备,实现交互.

    2.DEVICE_ATTR()宏定义

    DEVICE_ATTR()定义位于include/linux/device.h中,定义如下所示:

    #define DEVICE_ATTR(_name, _mode, _show, _store) 
        struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)

    其中_mode定义如下:

    • 400    拥有者能够读,其他任何人不能进行任何操作;
    • 644    拥有者都能够读,但只有拥有者可以编辑;
    • 660    拥有者和组用户都可读和写,其他人不能进行任何操作;
    • 664    所有人都可读,但只有拥有者和组用户可编辑;
    • 700    拥有者能够读、写和执行,其他用户不能任何操作;
    • 744    所有人都能读,但只有拥有者才能编辑和执行;
    • 755    所有人都能读和执行,但只有拥有者才能编辑;
    • 777    所有人都能读、写和执行(该设置通常不是好想法)。

    当然也可以用S_IWUSR(用户可写),S_IRUSR(用户可读)等宏代替.

    以下面DEVICE_ATTR()定义为例:

    static ssize_t show_my_device(struct device *dev,
                      struct device_attribute *attr, char *buf)   //cat命令时,将会调用该函数
    {
        return buf;
    }
    
    static ssize_t set_my_device(struct device *dev,
                     struct device_attribute *attr,
                     const char *buf, size_t len)   //echo命令时,将会调用该函数.
    {
        return len;
    }
    static DEVICE_ATTR(my_device_test, S_IWUSR|S_IRUSR, show_my_device, set_my_device);   //定义一个名字为my_device_test的设备属性文件

    最终将宏展开为:

    struct device_attribute dev_attr_my_device_test ={  
        .attr = {.name = "my_device_test", .mode = S_IWUSR|S_IRUSR },     
        .show    = show_my_device,                 
        .store    = set_my_device,             
    }

    然后再通过device_create_file()或者sysfs_create_file()便来创建上面my_device_test设备文件.

    3.使用示例

    示例代码如下:

    #include <board.h>
    #include <linux/module.h>
    #include <linux/init.h>
    #include <linux/platform_device.h>
    #include <linux/gpio.h>
    #include <linux/delay.h>
    #include <linux/regulator/consumer.h>
    #include <sound/soc.h>
    #include <sound/jack.h>
    
    
    static  char mybuf[100]="123";
    static ssize_t show_my_device(struct device *dev,
                      struct device_attribute *attr, char *buf)        //cat命令时,将会调用该函数
    {
        return sprintf(buf, "%s
    ", mybuf);
    }
    
    static ssize_t set_my_device(struct device *dev,
                     struct device_attribute *attr,
                     const char *buf, size_t len)        //echo命令时,将会调用该函数
    {
        sprintf(mybuf, "%s", buf);
        return len;
    }
    static DEVICE_ATTR(my_device_test, S_IWUSR|S_IRUSR, show_my_device, set_my_device);
                    //定义一个名字为my_device_test的设备属性文件
                    
    struct file_operations mytest_ops={
             .owner  = THIS_MODULE,
    };
    
    static int major;
    static struct class *cls;
    static int mytest_init(void)
    {
             struct device *mydev;   
             major=register_chrdev(0,"mytest", &mytest_ops);
             cls=class_create(THIS_MODULE, "mytest_class");
             mydev = device_create(cls, 0, MKDEV(major,0),NULL,"mytest_device");    //创建mytest_device设备    
        
        if(sysfs_create_file(&(mydev->kobj), &dev_attr_my_device_test.attr)){    //在mytest_device设备目录下创建一个my_device_test属性文件
                return -1;}
                
             return 0;
    }
    
    static void mytest_exit(void)
    {
             device_destroy(cls, MKDEV(major,0));
             class_destroy(cls);
             unregister_chrdev(major, "mytest");
    }
    
    module_init(mytest_init);
    module_exit(mytest_exit);
    MODULE_LICENSE("GPL");

    效果如下:

     以后就可以无需写测试程序来读写驱动了,是不是很方便

  • 相关阅读:
    Vue+Element-UI+jQuery+Layer+Camera+Easing的简单应用
    Vue(vue-cli脚手架)中使用layer.js做非模态弹窗(二)
    unicable
    grep 搜索目录时,怎样排除某些目录?
    Mali Midgard架构解析
    如何从GFP确定最后申请的内存来自哪个zone?
    内存申请 GFP_KERNEL GFP_ATOMIC
    Linux内核中的page migration和compaction机制简介
    IOVA/IOMMU
    memory zone spanned/present/managed区别
  • 原文地址:https://www.cnblogs.com/lifexy/p/9799778.html
Copyright © 2011-2022 走看看