1. 一切皆文件
在linux系统中,无论是普通文件、设备文件,我们都是调用同一套接口:open、read、write、ioclt…
那么,最简单的写驱动程序的办法就是在内核中实现驱动版本的open、read、write、ioctl…等操作函数。
2. 写驱动程序的步骤
- 构造
file_operations
结构体,比如:struct file_operations fops;
a. 在里面填充open/read/write/ioctl
等成员函数;
/* 1. create file_operations */
static const struct file_operations hello_drv = {
.owner = THIS_MODULE,
.read = hello_read,
.write = hello_write,
.open = hello_open,
.release = hello_release,
};
b.在结构体外实现这些成员函数,以hello_open
为例:
static int hello_open (struct inode *node, struct file *filp)
{
printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
return 0;
}
- 注册
file_operations
结构体(告诉内核),这样我们就可以通过应用层的系统调用函数访问到驱动程序提供的接口;
int major = register_chrdev(0, "name", &fops);//major主设备号,用来表示驱动程序相同的一类设备
- 入口函数:调用:
register_chrdev
调用的是第2步的注册函数,在入口函数中注册file_operations
结构体
/* 2. register_chrdev */
/* 3. entry function */
static int hello_init(void)
{
major = register_chrdev(0, "100ask_hello", &hello_drv);
return 0;
}
- 出口函数:调用
unregister_chrdev
出口函数中实现入口函数的反操作,在入口函数中注册了file_operations
结构体,那么在出口函数就要反注册``file_operations```结构体
- 其他的辅助信息:
a.class_create/class_destroy
创建/销毁类;
b.device_create/device_destroy
创建/销毁设备节点;
3. hello驱动程序
根据上述的步骤,我们实现一个最简单的驱动程序:
#include <linux/mm.h>
#include <linux/module.h>
#include <linux/miscdevice.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>
#include <linux/mman.h>
#include <linux/random.h>
#include <linux/init.h>
#include <linux/raw.h>
#include <linux/tty.h>
#include <linux/capability.h>
#include <linux/ptrace.h>
#include <linux/device.h>
#include <linux/highmem.h>
#include <linux/backing-dev.h>
#include <linux/shmem_fs.h>
#include <linux/splice.h>
#include <linux/pfn.h>
#include <linux/export.h>
#include <linux/io.h>
#include <linux/uio.h>
#include <linux/uaccess.h>
static int major;
static int hello_open (struct inode *node, struct file *filp)
{
printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
return 0;
}
static ssize_t hello_read (struct file *filp, char __user *buf, size_t size, loff_t *offset)
{
printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
return size;
}
static ssize_t hello_write(struct file *filp, const char __user *buf, size_t size, loff_t *offset)
{
printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
return size;
}
static int hello_release (struct inode *node, struct file *filp)
{
printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
return 0;
}
/* 1. create file_operations */
static const struct file_operations hello_drv = {
.owner = THIS_MODULE,
.read = hello_read,
.write = hello_write,
.open = hello_open,
.release = hello_release,
};
/* 2. register_chrdev */
/* 3. entry function */
static int hello_init(void)
{
major = register_chrdev(0, "100ask_hello", &hello_drv);
return 0;
}
/* 4. exit function */
static void hello_exit(void)
{
unregister_chrdev(major, "100ask_hello");
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");
4. 测试
// 插入内核模块
insmod hello_drv.ko
//查看设备号,获得主设备号
cat /proc/devices
//添加设备节点
mknod /dev/xyz c 241 0
ls /dev/xyz
5. 小结
- 编写驱动时,选择相同的设备类型的内核源码进行参考。比如字符设备的驱动,在编写
hello
驱动程序的时候,就参考了Linux-5.4/drivers/char/mem.c
- 在编写驱动代码时,要善用工具。在Windos下,我们可以使用
Source Insight
。在Ubuntu环境中,我们可以搭建起vscode
环境,并使用clangd插件和工作区实现源码跳转; - 编写过程中,可以使用AI工具,比如:通义灵码插件、chatgpt等等;