在寫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