本次练习实践开发板为飞凌IMX6UL开发板,教程资料参考韦东山老师的嵌入式教程
2.1 应用程序中打开文件在内核中表示
应用程序通过open打开文件,可以得到一个文件的句柄。在内核中通过struct file结构体表示,位于linux\fs.h中。open时传入的pathname、flags、mode以及读写文件的位置参数,保存在file结构体的f_paht、f_flags、f_mode、f_pos中。
2.2 设备文件中的操作struct file_operations
对于设备文件的打开、读写操作等保存在struct file的struct file_operations结构体f_op中。
2.3 如何编写驱动程序
- 确定主设备号(设置为0,由内核自动分配)
- 定义file_operations结构体
- 实现对应的drv_open、drv_read、drv_write等函数,填入file_operations结构体
- 使用register_chrdev注册file_operations到内核
- 定义注册驱动程序的入口函数
- 定义卸载驱动程序的出口函数,出口函数调用unregister_chrdev
- 提供设备信息,自动创建设备节点:class_create、device_create
2.4 LED驱动测试
hello_drv.c
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/miscdevice.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/mutex.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/stat.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/tty.h>
#include <linux/kmod.h>
#include <linux/gfp.h>
/
*
*4. 把file_operations结构体告诉内核:注册驱动程序
*5. 谁来注册驱动程序?得有一个入口函数:安装驱动程序时就会调用这个入口函数
*
*/
#define MIN(a, b) (a < b ? a : b)
//1. 确定主设备号
static int major = 0;
static char kernel_buf[1024];
static struct class *hello_class;
//3. 实现对应的open/write/read函数,填入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;
}
//2. 定义自己的file_operations结构体
static const 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__);
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__);
return -1;
}
major = register_chrdev(0,"hello",&hello_drv);
device_create(hello_class, NULL, MKDEV(major, 0), NULL, "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));
unregister_chrdev(major, "hello");
class_destroy(hello_class);
}
//7. 其他完善:提供设备信息,自动创建设备节点
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");
hello_drv_test.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
/*
* ./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;
}