目录
一、字符设备驱动框架
字符设备驱动的编写主要就是驱动对应的open、close、read…其实就是file_operations结构体的成员变量的实现。
应用程序运行在用户空间,而 Linux 驱动属于内核的一部分,因此驱动运行于内核空间。当我们在用户空间想要实现对内核的操作,比如使用 open 函数打开/dev/led 这个驱动,因为用户空间不能直接对内核进行操作,因此必须使用一个叫做“系统调用”的方法来实现从用户空间“陷入”到内核空间,这样才能实现对底层驱动的操作。open、close、write 和 read 等这些函数是由 C 库提供的,在 Linux 系统中,系统调用作为 C 库的一部分。当我们调用 open 函数的时候流程如下图:
程序员重点关注的是应用程序和具体的驱动,应用程序使用到的函数在具体驱动程序中都有与之对应的函数,比如应用程序中调用了 open 这个函数,那么在驱动程序中也得有一个名为 open 的函数。每一个系统调用,在驱动中都有与之对应的一个驱动函数,在 Linux 内核文件 include/linux/fs.h 中有个叫做 file_operations 的结构体,此结构体就是 Linux 内核驱动操作函数集合,内容如下:
struct file_operations {
struct module *owner;
loff_t (*llseek) (struct file *, loff_t, int);
ssize_t (*read) (struct file *, char __user *, size_t, loff_t*);
ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
ssize_t (*read_iter) (struct kiocb *, struct iov_iter *);
ssize_t (*write_iter) (struct kiocb *, struct iov_iter *);
int (*iterate) (struct file *, struct dir_context *);
unsigned int (*poll) (struct file *, struct poll_table_struct*);
long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
int (*mmap) (struct file *, struct vm_area_struct *);
int (*mremap)(struct file *, struct vm_area_struct *);
int (*open) (struct inode *, struct file *);
int (*flush) (struct file *, fl_owner_t id);
int (*release) (struct inode *, struct file *);
int (*fsync) (struct file *, loff_t, loff_t, int datasync);
int (*aio_fsync) (struct kiocb *, int datasync);
int (*fasync) (int, struct file *, int);
int (*lock) (struct file *, int, struct file_lock *);
ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
int (*check_flags)(int);
int (*flock) (struct file *, int, struct file_lock *);
ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
int (*setlease)(struct file *, long, struct file_lock **, void**);
long (*fallocate)(struct file *file, int mode, loff_t offset, loff_t len);
void (*show_fdinfo)(struct seq_file *m, struct file *f);
#ifndef CONFIG_MMU
unsigned (*mmap_capabilities)(struct file *);
#endif
};
简单介绍一下 file_operation 结构体中比较重要的、常用的函数:
- owner 拥有该结构体的模块的指针,一般设置为 THIS_MODULE。
- llseek 函数用于修改文件当前的读写位置。
- read 函数用于读取设备文件。
- write 函数用于向设备文件写入(发送)数据。
- poll 是个轮询函数,用于查询设备是否可以进行非阻塞的读写。
- unlocked_ioctl 函数提供对于设备的控制功能,与应用程序中的 ioctl 函数对应。
- compat_ioctl 函数与 unlocked_ioctl 函数功能一样,区别在于在 64 位系统上,32 位的应用程序调用将会使用此函数。在 32 位的系统上运行 32 位的应用程序调用的是unlocked_ioctl。
- mmap 函数用于将将设备的内存映射到进程空间中(也就是用户空间),一般帧缓冲设备会使用此函数,比如 LCD 驱动的显存,将帧缓冲(LCD 显存)映射到用户空间中以后应用程序就可以直接操作显存了,这样就不用在用户空间和内核空间之间来回复制。
- open 函数用于打开设备文件。
- release 函数用于释放(关闭)设备文件,与应用程序中的 close 函数对应。
- fasync 函数用于刷新待处理的数据,用于将缓冲区中的数据刷新到磁盘中。
- aio_fsync 函数与 fasync 函数的功能类似,只是 aio_fsync 是异步刷新待处理的数据。
二、驱动模块的加载和卸载
- 第一种方法:Linux驱动程序可以直接把它编译到内核里面去,这样当Linux内核启动的时候就会自动运行驱动程序,编译到内核里面去之后驱动就包含在ZImage里面了。
- 第二种方法:可以把Linux驱动编译成模块即:.ko结尾的文件,在Linux内核启动之后使用“insmod”命令加载驱动模块。
模块有加载和卸载两种操作,我们在编写驱动的时候需要注册这两种操作函数:
- 注册模块加载函数:module_init(xxx_init);
- 注册模块卸载函数:module_exit(xxx_exit);
编写驱动的时候的注意事项
- 1、编译驱动的时候需要用到Linux 内核源码,因此需要解压缩Linux内核源码,编译Linux 内核源码。得到zImage和.dtb。需要使用编译后得到的zImage和.dtb启动系统。
三、字符设备的注册于注销
- 1、我们需要向系统注册一个字符设备,使用函数register_chrdev
- 2、卸载驱动的时候需要注销掉前面注册的字符设备,使用函数unregister_chrdev来注销字符设备(与register_chrdev成对出现)
四、设备号
- Linux内核使用dev_t
typedef unsigned int __u32;
typedef __u32 __kernel_dev_t;
typedef __kernel_dev_t dev_t;
- 设备号分为两个部分:高12位是主设备号(0~4056),低20位是次设备号(1048576)。设备号的内核宏操作在:/include/linux/kdev_t.h
#define MINORBITS 20
#define MINORMASK ((1u << MINORBITS) - 1)
#define MAJOR(dev) ((unsigned int) ((dev) >> MINORBITS))
#define MINOR(dev) ((unsigned int) ((dev) & MINORMASK))
#define mkdev(ma,mi) (((ma) << MINORBITS) | (mi))
Linux下使用命令:cat /proc/devices
查看系统内所有设备:第一列是主设备号 ,第二列是设备名
五、file_operations的具体实现
六、驱动代码工程
Linux下使用命令:cat /proc/devices
查看系统内所有设备:第一列是主设备号 ,第二列是设备名
查看空余的主设备号:
chrdevbase.c
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#define CHRDEVBASE_MAJOR 200 // 主设备号
#define CHRDEVBASE_NAME "chrdevbase" // 名字
static int chrdevbase_open(struct inode *inode, struct file *filp)
{
printk("[func] chrdevbase_open");
return 0;
}
static int chrdevbase_release(struct inode *inode, struct file *filp)
{
printk("[func] chrdevbase_release");
return 0;
}
static ssize_t chrdevbase_read(struct file *filp, __user char *buf,
size_t count, loff_t *ppos)
{
printk("[func] chrdevbase_read");
return 0;
}
static ssize_t chrdevbase_write(struct file *filp, __user char *buf,
size_t count, loff_t *ppos)
{
printk("[func] chrdevbase_write");
return 0;
}
// 字符设备 操作集合
static struct file_operations chrdevbase_fops = {
.owner = THIS_MODULE,
.open = chrdevbase_open,
.release = chrdevbase_release,
.read = chrdevbase_read,
.write = chrdevbase_write,
};
static int __init chrdevbase_init(void)
{
int ret = 0;
printk("[func] chrdevbase_init\r\n");
// 注册字符设备
ret = register_chrdev(CHRDEVBASE_MAJOR, CHRDEVBASE_NAME, &chrdevbase_fops);
if(ret < 0)
{
printk("[error] chrdevbase_init failed!\r\n");
}
return 0;
}
static void __exit chrdevbase_exit(void)
{
printk("[func] chrdevbase_exit\r\n");
unregister_chrdev(CHRDEVBASE_MAJOR, CHRDEVBASE_NAME);
}
/**
* 模块入口与出口
**/
module_init(chrdevbase_init); // 入口
module_exit(chrdevbase_exit); // 出口
MODULE_LICENSE("GPL");
MODULE_AUTHOR("liefyuan");
Makefile
KERNELDIR := /home/liefyuan/linux/linux-imx-rel_imx_4.1.15_2.1.0_ga_alientek
CURRENT_PATH := $(shell pwd)
obj-m := chrdevbase.o
build : kernel_modules
kernel_modules:
$(MAKE) -C $(KERNELDIR) M=$(CURRENT_PATH) modules
clean:
$(MAKE) -C $(KERNELDIR) M=$(CURRENT_PATH) clean
.vscode 文件夹–vscode头文件目录配置文件夹
setting.json
{
"search.exclude": {
"**/node_modules": true,
"**/bower_components": true,
"**/*.o":true,
"**/*.su":true,
"**/*.cmd":true,
"Documentation":true,
},
"files.exclude": {
"**/.git": true,
"**/.svn": true,
"**/.hg": true,
"**/CVS": true,
"**/.DS_Store": true,
"**/*.o":true,
"**/*.su":true,
"**/*.cmd":true,
"Documentation":true,
}
}
c_cpp_properties.json
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/home/liefyuan/linux/linux-imx-rel_imx_4.1.15_2.1.0_ga_alientek/include",
"/home/liefyuan/linux/linux-imx-rel_imx_4.1.15_2.1.0_ga_alientek/arch/arm/include",
"/home/liefyuan/linux/linux-imx-rel_imx_4.1.15_2.1.0_ga_alientek/arch/arm/include/generated/"
],
"defines": [],
"compilerPath": "/usr/bin/clang",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
七、Linux基本应用程序开发
以上的是一个驱动工程,它最终会被编译成一个.ko文件,放到一个固定的文件夹里面。使用的时候需要编写一个应用程序–这就是传说中的Linux应用开发。
chrdevbaseAPP.c