一、简介:
在Linux内核驱动中,字符设备是最基本的设备驱动。字符设备是能够像字节流(比如文件)一样被访问的设备,就是说对它的读写是以子为单位的。比如串口在进行收发数据时就是一个字节一个字节进行的。字符设备的驱动程序中实现了 open、close、read、wrie等系统调用,应用程序可以通过设备文件(比如dewAySAC0等)来访问字符设备。常见的串口、调制解调器都是字符设备。
二、重要的数据结构和函数
Linux操作系统将所有的设备(而不仅是存储器里的文件)都看成文件,以操作文件的方访问设备。应用程序不能直接操作硬件,而是使用统一的接口函数(如open、read、write等)调用硬件驱动程序。这组口被称为系统调用,在库函数中定义。
对于上述每个系统调用,驱动程序都有一个与之对应的函数。对于字符设备驱动程序而言,这些函数集合在一个file_operation的结构体中。在内核源码目录下\include\linux文件夹fs.h文件中定义。
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 (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
int (*readdir) (struct file *, void *, filldir_t);
unsigned int (*poll) (struct file *, struct poll_table_struct *);
int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);
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 (*open) (struct inode *, struct file *);
int (*flush) (struct file *, fl_owner_t id);
int (*release) (struct inode *, struct file *);
int (*fsync) (struct file *, struct dentry *, 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 (*sendfile) (struct file *, loff_t *, size_t, read_actor_t, void *);
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 (*dir_notify)(struct file *filp, unsigned long arg);
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);
};
当应用程序使用open函数打开某个设备时,设备驱动程序的file_operations结构中的open成员就会被调用;当应用程序使用read、write、ioctl等函数读写、控制设备时,驱动程序的file_operations 结构中的相应成员(read、write、ioctl等)就会被调用。从这个角度来说,编写字符设备驱动程序就是为具体硬件的file_operations结构编写各个函数实体(并不需要全部实现file_operations结构中的成员)。
因此:设备节点是上层应用和底层驱动的桥梁!
(1)设备文件有主/次设备号。
在Linux系统中,所有的资源都是作为文件管理的,设备驱动也不例外,设备驱动通常是作为一类特殊的文件存放在/dev目录下。查看/dev目录得到系统所有设备的列表如下:
这里仅列出了一部分文件,设备文件属性最开始的一个字符c表示该设备文件关联的是一个字符设备;b表示关联的是一个块设备。在文件列表的中间部分有两个数字,第一个数字称做主设备号,第二个数字称做次设备号。如tty的主设备号为5,此设备号为0。
(2)注册字符类设备号的两种方法
(3)模块初始化时,将主设备号与file_operations结构一起向内核注册。
驱动程序有一个初始化函数,在安装驱动程序时会调用它。在初始化函数中,会将驱动程序的file_operations结构连同其主设备号一起向内核进行注册。对于字符设备使用如下以下函数进行注册:
extern int register_chrdev(unsigned int major , const char * name ,const struct file_operations * fops);
参数major是主设备号,name是设备名称,fops是指向函数指针数组的结构指针,驱动程序的入口函数都包括在这个指针内部。该函数的返回值如果小于0,表示注册设备驱动失败,如果设置major为0,表示由内核动态分配主设备号,函数的返回值是主设备号。
当使用register_chardev()函数成功注册一个字符设备后,会在/proc/devices文件中显示出设备信息。
这样在打开一个设备的时候,内核会根据设备的主设备号得到设备驱动,并且把次设备号传递给驱动。Linux内核为所有设备都分配了主设备号,在编写驱动程序之前需要参考内核代码Documentation/devices.txt文件,确保使用的设备号没有被占用
三、开发字符设备的基本流程
1、设备号的申请与注销
note:
下面的方法已经逐渐废弃,因为指定了一个主设备号,会把这个主设备号所有的次设备号都占用,浪费资源!
2、注册字符类设备
- 定义一个cdev结构体
<include/linux/cdev.h>
struct cdev {
struct kobject kobj; //内嵌的内核对象.
struct module *owner; //该字符设备所在的内核模块的对象指针.
const struct file_operations *ops; //该结构描述了字符设备所能实现的方法,是极为关键的一个结构体.
struct list_head list; //用来将已经向内核注册的所有字符设备形成链表.
dev_t dev; //字符设备的设备号,由主设备号和次设备号构成.
unsigned int count; //隶属于同一主设备号的次设备号的个数.
};
- 初始化cdev结构体
void cdev_init(struct cdev *, const struct file_operations *);
该函数主要对struct cdev结构体做初始化, 最重要的就是建立cdev 和 file_operations之间的连接
- 向内核注册字符设备
int cdev_add(struct cdev *p, dev_t dev, unsigned count);
3、创建设备节点
- 方法一、使用命令创建设备节点
- 方法二、自动创建设备节点
创建类函数调用后,会在/sys/class/目录下找到自己创建的类
三、开发一个基本的字符设备驱动
建立一个名为GilobalChar的虚拟设备,设备内部只有一个全局变量供用户操作。设备提供了读函数读取全局变量的值并且返回给用户,写函数把用户设定的值写入全局变量。
3.1、编写模块代码
/* GlobalCharDev.c */
#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include </usr/src/linux-headers-4.15.0-60/include/linux/uaccess.h>
//copy_from_user copy_to_user
MODULE_LICENSE("GPL");
MODULE_AUTHOR("GongLei");
#define DEV_NAME "GlobalChar"
/*********函数声明*********/
static ssize_t GlobalRead(struct file *, char *, size_t, loff_t*);
static ssize_t GlobalWrite(struct file *, const char *, size_t, loff_t*);
static int char_major = 0;//主设备号为0,表示由内核自动分配主设备号
static int GlobalData = 0; // "GlobalChar" 设备的全局变量
//初始化字符设备驱动的file_operations结构体
struct file_operations globalchar_fops =
{
.read = GlobalRead,
.write = GlobalWrite
};
/* 模块初始化函数 执行insmod命令时候执行这个函数 */
static int __init GlobalChar_init(void)
{
int ret;
ret = register_chrdev(char_major, DEV_NAME, &globalchar_fops); // 注册设备驱动
if (ret<0)
{
printk(KERN_ALERT "GlobalChar Reg Fail!\n");
}
else
{
printk(KERN_ALERT "GlobalChar Reg Success!\n");
char_major = ret;
printk(KERN_ALERT "Major = %d\n", char_major);
}
return ret;
}
/* 模块卸载函数 执行rmmod命令时候执行这个函数 */
static void __exit GlobalChar_exit(void)
{
unregister_chrdev(char_major, DEV_NAME); // 注销设备驱动
return;
}
/* 设备驱动读函数 */
static ssize_t GlobalRead(struct file *filp, char *buf, size_t len, loff_t *off)
{
if (copy_to_user(buf, &GlobalData, sizeof(int)))
{ // 从内核空间复制GlobalData到用户空间
return -EFAULT;
}
return sizeof(int);
}
/* 设备驱动写函数 */
static ssize_t GlobalWrite(struct file *filp, const char *buf, size_t len, loff_t *off)
{
if (copy_from_user(&GlobalData, buf, sizeof(int)))
{ // 从用户空间复制GlobalData到内核空间
return -EFAULT;
}
return sizeof(int);
}
module_init(GlobalChar_init);
module_exit(GlobalChar_exit);
GlobalRead()函数读取全局变量GlobalData,并且把数据复制到用户空间,GlobalWrite()函数把用户空间的数值复制到全局变量GlobalData。在内核中操作数据要区分数据的来源,对于用户空间的数据要使用copy_from_user()函数复制,使用copy_to_user()函数回写,不能直接操作用户空间的数据,否则会产生内存访问错误。
static inline unsigned long _copy_from_user(void *to, const void __user *from, unsigned long n)
static inline unsigned long _copy_to_user(void __user *to, const void *from, unsigned long n)
3.2、编写Makefile代码
obj-m += GlobalCharDev.o
#ubuntu的内核源码树,如果要编译在ubuntu中安装的模块就打开这2个
KERN_VER = $(shell uname -r)
KERN_DIR = /lib/modules/$(KERN_VER)/build
all:
make -C $(KERN_DIR) M=`pwd` modules
.PHONY: clean
clean:
make -C $(KERN_DIR) M=`pwd` modules clean
3.3、编译模块文件得到内核模块GlobalCharDev.ko,并通过insmod命令加载
3.4、查看内核为该模块分配的主设备号(在/proc/devices文件中)
3.5、使用mknod命令建立一个设备文件
-m参数指定所有的用户可以访问
四、测试字符设备驱动
/* GlobalCharTest.c */
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#define DEV_NAME "/dev/GlobalChar"
int main()
{
int fd, num;
/* 打开设备文件 */
fd = open(DEV_NAME, O_RDWR, S_IRUSR | S_IWUSR);
if (fd<0) {
printf("Open Deivec Fail!\n");
return -1;
}
/* 读取当前设备数值 */
read(fd, &num, sizeof(int));
printf("The GlobalChar is %d\n", num);
printf("Please input a number written to GlobalChar: ");
scanf("%d", &num);
/* 写入新的数值 */
write(fd, &num, sizeof(int));
/* 重新读取设备数值 */
read(fd, &num, sizeof(int));
printf("The GlobalChar is %d\n", num);
close(fd);
return 0;
}
REF:
《ARM嵌入式linux系统开发详解》
《嵌入式linux应用开发完全手册-韦东山》