应用程序调用驱动程序访问硬件过程
1. 打开设备文件 fd = open("/dev/hello", flag, mode)
1.1 根据文件名/dev/hello找到设备节点(目录项 = 文件名 +节点), struct inode 结构体
struct inode{
}
通过其中描述的信息可以知道接下来要操作的设备类型(字符设备还是块设备),还会分配一个struct file结构体。
1.2 根据struct inode结构体里面记录的设备号,可以找到对应的驱动函数,也就是驱动层file_operation结构体中的op
en,read,write等。
1.3 任务完成,VFS层会给应用返回一个文件描述符(fd)。这个fd是和struct file结构体对应的
通过fd可以将进程的task_struct数据结构和file结构挂钩起来。接下来上层应用程序就可以通过fd找到对应的struct
file,从而找到对应的驱动函数。
struct file {
struct path f_path,
struct inode *f_node,
const struct file_operations *f_op;
}
struct file_operations{
int (*open)(struct inode *, struct file* ),
ssize_t (*read)(struct file *, const char __user *, size_t, loff_t *);
ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *);
}
2. 向硬件写入数据 write(fd, buf, size)
2.1 应用程序调用write, 执行系统调用,触发swi异常,执行异常处理函数,识别应该调用驱动程序drv_write()
2.2 如何识别应该调用驱动程序drv_write()?
fd 对应 struct file 结构体,找到设备节点struct inode,
取出主设备号major,通过主设备号找到file_operations结构体,取出其中的write执行
字符设备驱动程序框架
1. 确定主设备号,也可以让内核分配
2. 定义自己的file_operations结构体
3. 实现对应的drv_open/drv_read/drv_write等函数,填入file_operations结构体
4. 把file_operations结构体告诉内核:register_chrdev
5. 谁来注册驱动程序啊?得有一个入口函数:安装驱动程序时,就会去调用这个入口函数
6. 有入口函数就应该有出口函数:卸载驱动程序时,出口函数调用unregister_chrdev
7. 其他完善:提供设备信息,自动创建设备节点:class_create, device_create
核心:填充struct file_operations结构体
hello 驱动程序(不涉及硬件)
功能:
APP调用write函数时,传入的数据保存在驱动中
APP调用read函数时,把驱动中保存的数据返回给APP
驱动程序中包含了很多头文件,这些头文件来自内核,不同的ARM板它的某些头文件可能不同。所以编译驱动程序时,需要
指定板子所用的内核的源码路径。要编译哪个文件?这也需要指定,设置obj-m变量即可
先设置好交叉编译工具链,编译好你的板子所用的内核,然后修改Makefile指定内核源码路径,最后即可执行make命令编
译驱动程序和测试程序。
驱动程序:
// 1. 确定主设备号
static int major = 0;
static char kernel_buf[1024];
static struct class *hello_class;
// 3. 实现对应的open/read/write等函数,填入file_operations结构体
static ssize_t hello_drv_read (struct file *file, char __user *buf, size_t size, loff_t *offset)
{
int err;
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
err = copy_to_user(buf, kernel_buf, MIN(1024, size));
return MIN(1024, size);
}
static ssize_t hello_drv_write (struct file *file, const char __user *buf, size_t size, loff_t *offset)
{
int err;
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
err = copy_from_user(kernel_buf, buf, MIN(1024, size));
return MIN(1024, size);
}
static int hello_drv_open (struct inode *node, struct file *file)
{
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
return 0;
}
static int hello_drv_close (struct inode *node, struct file *file)
{
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
return 0;
}
// 定义自己的file_operations结构体
static struct file_operations hello_drv = {
.owner = THIS_MODULE,
.open = hello_drv_open,
.read = hello_drv_read,
.write = hello_drv_write,
.release = hello_drv_close,
};
//4. 把file_operations结构体告诉内核:注册驱动程序
// 5. 谁来注册驱动程序啊?得有一个入口函数:安装驱动程序时,就会去调用这个入口函数
static int __init hello_init(void)
{
int err;
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
major = register_chrdev(0, "hello", &hello_drv); /* /dev/hello */
hello_class = class_create(THIS_MODULE, "hello_class");
err = PTR_ERR(hello_class);
if (IS_ERR(hello_class)) {
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
unregister_chrdev(major, "hello");
return -1;
}
device_create(hello_class, NULL, MKDEV(major, 0), NULL, "hello"); /* /dev/hello */
return 0;
}
// 6. 有入口函数就应该有出口函数:卸载驱动程序时,就会去调用这个出口函数
static void __exit hello_exit(void)
{
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
device_destroy(hello_class, MKDEV(major, 0));
class_destroy(hello_class);
unregister_chrdev(major, "hello");
}
// 7. 其他完善:提供设备信息,自动创建设备节点
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");
测试程序
/*
* ./hello_drv_test -w abc
* ./hello_drv_test -r
*/
int main(int argc, char **argv)
{
int fd;
char buf[1024];
int len;
// 1. 判断参数
if (argc < 2)
{
printf("Usage: %s -w <string>\n", argv[0]);
printf(" %s -r\n", argv[0]);
return -1;
}
//2. 打开文件
fd = open("/dev/hello", O_RDWR);
if (fd == -1)
{
printf("can not open file /dev/hello\n");
return -1;
}
//3. 写文件或读文件
if ((0 == strcmp(argv[1], "-w")) && (argc == 3))
{
len = strlen(argv[2]) + 1;
len = len < 1024 ? len : 1024;
write(fd, argv[2], len);
}
else
{
len = read(fd, buf, 1024);
buf[1023] = '\0';
printf("APP read : %s\n", buf);
}
close(fd);
return 0;
}