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(用户可读)等宏代替.
static ssize_t device_show(struct device *dev, struct device_attribute *attr, char *buf) //cat命令时,将会调用该函数 { returnbuf; }
static ssize_t device_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t len) //echo命令时,将会调用该函数.
{ returnlen; }static DEVICE_ATTR(my_device_test, S_IWUSR|S_IRUSR, device_show, device_store);
//定义一个名字为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 #include#include#include#include#include#include#include#include
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\n", 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);returnlen;
}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 intmajor;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");
原文:https://www.cnblogs.com/vino-yu/p/14118812.html