linux设备驱动(2)--字符设备驱动框架

代码学习资料来源于:

第3.5讲 我的第一个Linux驱动-完善chrdevbase驱动_哔哩哔哩_bilibili

仅用于个人学习/复习,侵联删

1、字符设备驱动简介

字符设备是linux驱动中最基本的一类设备驱动,字符设备就是一个个字节,按照字节流来进行读写操作的设备,读写数据是分先后顺序的。比如我们最常见的点灯、按键、i2c、spi、lcd等都是字符设备,这些设备的驱动就叫做字符设备驱动。

linux内核文件 include/linux/fs.h中有个叫做file_operations的结构体,此结构体就是linux内核驱动操作函数集合。我们需要用到什么就用什么。常用的有open、write、read、release,poll,mmap等。

open:打开文件设备

read:读取文件设备

poll:轮询函数,用于查询设备是否可以进行非阻塞的读写

write:向设备文件写入设备

release:释放(关闭)文件设备,与应用程序中的close相对应

mmap:将设备的内存映射到进程空间中(也就是用户空间),一般帧缓冲设备会使用此函数,比如LCD驱动的显存,将帧缓冲映射到用户空间中以后应用程序就可以直接操作显存了,这样就可以不用在用户空间和内核空间之间来回复制。

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 *);
        int (*iterate_shared) (struct file *, struct dir_context *);
        __poll_t (*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 *);
        unsigned long mmap_supported_flags;
        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 (*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
        ssize_t (*copy_file_range)(struct file *, loff_t, struct file *,
                        loff_t, size_t, unsigned int);
        int (*clone_file_range)(struct file *, loff_t, struct file *, loff_t,
                        u64);
        int (*dedupe_file_range)(struct file *, loff_t, struct file *, loff_t,
                        u64);
        int (*fadvise)(struct file *, loff_t, loff_t, int);
} __randomize_layout;

2、内核模块的载入和卸载

linux驱动程序可以编译到kernel里面,也就是zImage,也可以编译为模块.ko。测试的时候只需要加载.ko文件即可。

__init修饰函数表示此函数仅在初始化阶段使用,使用后所占用的内存资源会释放

模块代码有两种方式,一是静态编译链接进入内核,在系统启动过程中进行初始化;二是编译成可动态加载的module,通过insmod动态加载重定位到内核。这两种方式可以在Makefile中通过obj-y或obj-m选项进行选择

在调试的时候可以使用obj-y,在提交代码的时候为了规范要使用obj-m。

关于module_init和module_exit两个宏参考:

Linux内核模块分析(module_init宏)_阿基米东的博客-CSDN博客_module_init

加载驱动模块:insmod drv.ko或者modprobe drv.ko

insmod和modprobe两者之间的区别:

insmod命令不能解决模块的依赖问题,比如drv.ko依赖first.ko模块,就必须先insmod first.ko模块,而modprobe就比较智能,会到指定的路径下去查找依赖

卸载驱动模块:rmmod drv.ko或者modprobe -r drv.ko

查看驱动模块:lsmod

3、内核打印函数

内核打印函数为printk,printk存在八个等级,具体可以查看include/linux/kern_levels.h

#define KERN_EMERG      KERN_SOH "0"    /* system is unusable */
#define KERN_ALERT      KERN_SOH "1"    /* action must be taken immediately */
#define KERN_CRIT       KERN_SOH "2"    /* critical conditions */
#define KERN_ERR        KERN_SOH "3"    /* error conditions */
#define KERN_WARNING    KERN_SOH "4"    /* warning conditions */
#define KERN_NOTICE     KERN_SOH "5"    /* normal but significant condition */
#define KERN_INFO       KERN_SOH "6"    /* informational */
#define KERN_DEBUG      KERN_SOH "7"    /* debug-level messages */

0的优先级最高,7的优先级最低,如果不设置消息级别,printk会选择默认级别default_message_loglevel,为7。

可以通过指令对系统内核打印的等级进行设置:

echo 1       4       1      7 > /proc/sys/kernel/printk

同样有一些printk的封装函数:

pr_xxx

除了直接使用 printk 加消息级别的方式,在 <linux/printk.h> 中还定义了 pr_notice、pr_info、pr_warn、pr_err 等接口。使用这些 pr_xxx 接口,就可以省去指定消息级别的麻烦。

#define pr_emerg(fmt, ...)     printk(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
#define pr_alert(fmt, ...)     printk(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
#define pr_crit(fmt, ...)      printk(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
#define pr_err(fmt, ...)       printk(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
#define pr_warning(fmt, ...)   printk(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
#define pr_warn pr_warning
#define pr_notice(fmt, ...)    printk(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
#define pr_info(fmt, ...)      printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)

#define pr_devel(fmt, ...)     printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
#define pr_debug(fmt, ...)     printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)

需要注意的是,其他 pr_XXX() 函数能无条件地打印,但 pr_debug() 却不能。因为默认情况下它不会被编译,除非定义了 DEBUG 或设定了 CONFIG_DYNAMIC_DEBUG。

dev_xxx

对于驱动程序,在 <linux/device.h> 里也提供了一些驱动模型诊断宏,例如 dev_err、dev_warn、dev_info 等等。使用它们,不仅可以按标记的消息级别打印,还会打印对应的设备和驱动信息,这对于驱动调试来说相当重要。

#define dev_emerg(dev, fmt, ...)  _dev_emerg(dev, dev_fmt(fmt), ##__VA_ARGS__)
#define dev_crit(dev, fmt, ...)   _dev_crit(dev, dev_fmt(fmt), ##__VA_ARGS__)
#define dev_alert(dev, fmt, ...)  _dev_alert(dev, dev_fmt(fmt), ##__VA_ARGS__)
#define dev_err(dev, fmt, ...)    _dev_err(dev, dev_fmt(fmt), ##__VA_ARGS__)
#define dev_warn(dev, fmt, ...)   _dev_warn(dev, dev_fmt(fmt), ##__VA_ARGS__)
#define dev_notice(dev, fmt, ...) _dev_notice(dev, dev_fmt(fmt), ##__VA_ARGS__)
#define dev_info(dev, fmt, ...)   _dev_info(dev, dev_fmt(fmt), ##__VA_ARGS__)

4、字符设备注册与注销

对于字符设备而言,当驱动模块加载成功以后需要注册字符设备,同样,卸载驱动模块的时候也需要注销掉字符设备。

// 注册字符设备函数
static inline int register_chrdev(unsigned int major, const char *name,
                                  const struct file_operations *fops)
{
        return __register_chrdev(major, 0, 256, name, fops);
}

// 注销字符设备函数
static inline void unregister_chrdev(unsigned int major, const char *name)
{
        __unregister_chrdev(major, 0, 256, name);
}

register_chrdev函数用于注册字符设备,此函数一共有三个参数,含义如下:

major:主设备号,linux下每一个设备都有一个设备号,设备号分为主设备号和次设备号两部分。

linux中主设备号表示某一个具体的驱动,次设备号表示使用这个驱动的各个设备,linux提供了一个名为dev_t的数据类型表示设备号,dev_t定义在include/linux/types.h里面,如下:

typedef u32 __kernel_dev_t;
typedef __kernel_dev_t          dev_t;

综合起来看,其实dev_t类型就是unsigned int类型,是一个无符号的int类型的数据。

32位的前10位为主设备号,低20位为次设备号,因此Linux系统中主设备号范围为0-4095,所以在选择主设备号时一定不要超过这个范围,在include/linux/kdev_t.h中提供了几个关于设备号的宏:

#define MINORBITS       20    // 次设备号位数
#define MINORMASK       ((1U << MINORBITS) - 1)    // 次设备号掩码

// 从设备号中获取主设备号,dev_t右移20位
#define MAJOR(dev)      ((unsigned int) ((dev) >> MINORBITS))
// 从设备号获取次设备号,取dev_t的低20位
#define MINOR(dev)      ((unsigned int) ((dev) & MINORMASK))
// 将给定的主设备号和次设备号组合成dev_t类型的设备号
#define MKDEV(ma,mi)    (((ma) << MINORBITS) | (mi))

可以使用cat /proc/devices命令来查看当前系统所有的设备,如下

可以查看到主设备号为50的没有使用,那么我们就可以用50的主设备号,例如:

register_chrdev(50, "chrdevbase",const struct file_operations *fops);

name:设备名称,指向一串字符串

fops:结构体file_operations类型指针,指向设备的操作函数集合变量

unregister_chrdev函数同理。

重要的几个结构体:

struct inode:静态文件

struct file:动态文件(打开的文件),file结构体里面有个叫做private_data的成员变量。

struct file_operations:描述设备的操作方法的

struct cdev:struct cdev结构体是用来描述一个字符设备的,每个字符设备都对应一个struct cdev结构体。

驱动中重要的三个结构体介绍:struct inode、struct file、struct file_operations_正在起飞的蜗牛的博客-CSDN博客

5、man使用

我们写完驱动程序之后,需要编写应用程序去测试所写的驱动程序是否正确。

man手册的调用如下(图片来源于网络):

比如open系统调用,我们就可以通过man 2 open来进行查看相关的头文件和注意事项

6、chrdevbase的驱动demo

进入 /dev 查看设备,以模块的名字进行命名的,我们可以先手动创建设备节点:

mknod /dev/chrdevbase c 200 0 (c表示字符设备,主设备号是200,次设备号是0)

chrdevbase.c

#include <linux/module.h>                                                                                                                                                                                   
#include <linux/init.h>
#include <linux/sched/signal.h>
#include <linux/device.h>
#include <linux/ioctl.h>
#include <linux/parport.h>
#include <linux/ctype.h>
#include <linux/poll.h>
#include <linux/slab.h>
#include <linux/major.h>
#include <linux/ppdev.h>
#include <linux/mutex.h>
#include <linux/uaccess.h>
#include <linux/compat.h>

#define CHRDEVBASE_MAJOR 200
#define CHRDEVBASE_NAME "chrdevbase"

static int chrdevbase_open(struct inode *inode, struct file *file) 
{
        printk("chrdevbase_open start\n");
        return 0;
}

static int chrdevbase_release(struct inode *inode, struct file *file)
{
        printk("chrdevbase_release start \n");
        return 0;
}

// count:读取的数据量大小
static ssize_t chrdevbase_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
{
        printk("chrdevbase_read start\n");
        return 0;
}

static ssize_t chrdevbase_write(struct file *filp, const char __user *buf, size_t count, loff_t *ppos)
{
        printk("chrdevbase_write start\n");
        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("chrdevbase__init \n");

        // 返回值小于0就申请失败了
        ret = register_chrdev(CHRDEVBASE_MAJOR, CHRDEVBASE_NAME, &chrdevbase_fops);
        if(ret < 0) {
                printk("chrdevbase__init failed!\n");
        }
        
        printk("chrdevbase__init start\n");
        return 0;
}

static void __exit chrdevbase__exit(void)
{
        unregister_chrdev(CHRDEVBASE_MAJOR, CHRDEVBASE_NAME); 
        printk("chrdevbase__exit end\n");
}

module_init(chrdevbase__init);
module_exit(chrdevbase__exit);

测试程序 chrdevbaseAPP.c

#include <sys/types.h>                                                                                                                                                                                      
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>

int main(int argc, int *argv[])
{
        char *filename;
        int fd; 
        int ret;
        filename = argv[1];
        char readbuf[100], writebuf[100];

        fd = open(filename, O_RDWR);
        if (fd < 0) {
                printf("open %s failed\n", filename);
                return -1; 
        }

        ret = read(fd, readbuf, 50);
        if (ret < 0) {
                printf("read %s failed\n", filename);
                return -1; 
        } else {
    
        }
    
        ret = write(fd, writebuf, 50);
        if (ret < 0) {
                printf("write %s failed\n", filename);
                return -1; 
        } else {
    
        }

        ret = close(fd);
        if (ret < 0) {
                printf("close %s failed\n", filename);
                return -1; 
        } else {
    
        }
    

        return 0;
}

执行测试:

./chrdevbaseAPP /dev/chrdevbase

7、chrdevbase驱动的完善

驱动给应用传递数据的时候需要用到copy_to_user,应用程序向驱动传递数据的时候则使用copy_from_user,定义分别如下:

static inline long copy_to_user(void __user volatile *to, const void *from,
                               unsigned long n)
                               
static inline long copy_from_user(void *to, const void __user volatile *from,
                                 unsigned long n)

返回参数:成功返回0,失败返回负数                                 

chrdevbase.c

#include <linux/module.h>
#include <linux/init.h>
#include <linux/sched/signal.h>
#include <linux/device.h>
#include <linux/ioctl.h>
#include <linux/parport.h>
#include <linux/ctype.h>
#include <linux/poll.h>
#include <linux/slab.h>
#include <linux/major.h>
#include <linux/ppdev.h>
#include <linux/mutex.h>
#include <linux/uaccess.h>
#include <linux/compat.h>
#include <linux/fs.h>
#include <linux/io.h>
                                                                                                                                                                                                            
#define CHRDEVBASE_MAJOR 200
#define CHRDEVBASE_NAME "chrdevbase"

static char readbuf[100];
static char writebuf[100];
static char kerneldata[] = {"kernel data!"};

static int chrdevbase_open(struct inode *inode, struct file *file) 
{
        printk("chrdevbase_open start\n");
        return 0;
}

static int chrdevbase_release(struct inode *inode, struct file *file)
{
        printk("chrdevbase_release start \n");
        return 0;
}

// count:读取的数据量大小
static ssize_t chrdevbase_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
{
        int ret = 0;

        memcpy(readbuf, kerneldata, sizeof(kerneldata));
        int ret = copy_to_user(buf, readbuf, count);
        if (ret == 0) {
                printk("copy to user success \n");
        } else {
        
        }

        return 0;
}

static ssize_t chrdevbase_write(struct file *filp, const char __user *buf, size_t count, loff_t *ppos)
{
        int ret = 0;
        ret = copy_from_user(writebuf, buf, count);
        if (ret == 0) {
                printk("copy from user success, recedata = %s \n", writebuf);
        } else {
        
        }

        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("chrdevbase__init \n");

        // 返回值小于0就申请失败了
        ret = register_chrdev(CHRDEVBASE_MAJOR, CHRDEVBASE_NAME, &chrdevbase_fops);
        if(ret < 0) {
                printk("chrdevbase__init failed!\n");
        }
        
        printk("chrdevbase__init start\n");
        return 0;
}

static void __exit chrdevbase__exit(void)
{
        unregister_chrdev(CHRDEVBASE_MAJOR, CHRDEVBASE_NAME); 
        printk("chrdevbase__exit end\n");
}

module_init(chrdevbase__init);
module_exit(chrdevbase__exit);             

chrdevbaseAPP.c

#include <sys/types.h>                                                                                                                                                                                      
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

/*
 * ./chrdevbaseAPP <filename> <1/2>
 * 1 读数据
 * 2 写数据
 * */ 

int main(int argc, int *argv[])
{
        char *filename;
        int fd;
        int ret;
        filename = argv[1];
        char readbuf[100], writebuf[100];

        if (argc < 3) {
                printf("usage error \n");
        }

        fd = open(filename, O_RDWR);
        if (fd < 0) {
                printf("open %s failed\n", filename);
                return -1;
        }

        if(atoi(argv[2]) == 1) {
                ret = read(fd, readbuf, 50);
                if (ret < 0) {
                        printf("read %s failed\n", filename);
                        return -1;
                } else {
                        printf("APP read data: %s \n", readbuf);                
                }
        }

        if(atoi(argv[2]) == 2) {
                memcpy(writebuf, usrdata, sizeof(userdata));
                ret = write(fd, writebuf, 50);
                if (ret < 0) {
                        printf("write %s failed\n", filename);
                        return -1;
                } else {
                        printf("APP write data: %s \n", readbuf);
                }
        }

        ret = close(fd);
        if (ret < 0) {
                printf("close %s failed\n", filename);
                return -1;
        }

        return 0;
}        

执行测试:

./chrdevbaseAPP /dev/chrdevbase 1

猜你喜欢

转载自blog.csdn.net/qq_58550520/article/details/129151803