示例:Linux设备属性节点驱动,以及cat, echo操作

       在写Linux字符驱动的时候,经常涉及到一些驱动需要在/sys目录或子目录下创建,一个属性节点,以便与,不用查看驱动的版本信息,时间等等一些属性信息,以判断驱动程序加载的是否有误。

示例代码:

// drv_attr.c
#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";
//cat命令时,将会调用该函数
static ssize_t show_my_device(struct device *dev,
                  struct device_attribute *attr, char *buf)        
{
    return sprintf(buf, "%s\n", mybuf);
}

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

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");
         //创建mytest_device设备
         mydev = device_create(cls, 0, MKDEV(major,0),NULL,"mytest_device");    

    //在mytest_device设备目录下创建一个my_device_test属性文件
    if(sysfs_create_file(&(mydev->kobj), &dev_attr_my_device_test.attr)) {
        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");
# Makefile
ifneq ($(KERNELRELEASE),)
    obj-m := drv_attr.o
else
    KERNELDIR ?= /lib/modules/$(shell uname -r)/build
    PWD := $(shell pwd)
default:
    $(MAKE) -C $(KERNELDIR) M=$(PWD)  modules
endif
clean:  
    rm -rf *.mod.* *.o *.ko *.cmd *.symvers *.order 

编译:

        make

获取root权限,加载驱动:

        insmod drv_attr.ko

执行my_device_test节点进行cat echo操作:

        cd /sys/class/mytest_device/mytest_device/

        cat my_device_test  //显示123

        echo "abcdefg" > my_device_test  // 向驱动内写入abcdefg字符串

        cat my_device_test //显示abcdefg

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章