zoukankan      html  css  js  c++  java
  • 字符设备驱动简介

    1.字符设备文件

    通过字符设备文件,应用程序可以使用相应的字符设备驱动程序来控制字符设备。创建字符设备文件的方法:

    • mknod /dev/文件名 c 主设备号 次设备号
    • 使用函数在驱动程序中创建

    2.设备描述结构

    字符设备在内核中使用struct cdev来描述。

    struct cdev {
    struct kobject kobj;
    struct module *owner;
    const struct file_operations *ops; //设备操作集
    struct list_head list;
    dev_t dev; //设备号
    unsigned int count; //设备数
    }
    

    2.1设备号的操作

    Linux内核中使用dev_t类型来定义设备号,dev_t这种类型其实质为32位的unsigned int,其中高12位为主设备号,低20位为次设备号.

    • 通过主设备号,次设备号,组合成dev_t类型
    dev_t dev = MKDEV(主设备号,次设备号)
    
    • 从dev_t中分解出主设备号
    主设备号 = MAJOR(dev_t dev)
    
    • 从dev_t中分解出次设备号
    次设备号=MINOR(dev_t dev)
    

    2.2设备号的分配

    2.2.1静态申请

    通过函数register_chrdev_region向内核申请使用。
    缺点:如果申请使用的设备号已经被内核中的其他驱动使用了,则申请失败。

    2.2.2动态分配

    使用alloc_chrdev_region由内核分配一个可用的主设备号。
    优点:因为内核知道哪些号已经被使用了,所以不会导致分配到已经被使用的号。

    2.3设备号的注销

    在驱动退出时,使用unregister_chrdev_region函数释放设备号。

    3.操作函数集

    struct file_operations {
            //它是一个指向拥有这个结构的模块的指针. 这个成员用来在它的操作还在被使用时阻止模块被卸载. 几乎所有时间中, 它被简单初始化为 THIS_MODULE
            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);
            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 (*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 **);
            long (*fallocate)(struct file *file, int mode, loff_t offset,
                              loff_t len);
            int (*show_fdinfo)(struct seq_file *m, struct file *f);
    };
    

    这里面的函数在驱动中不用全部实现,不支持的操作留置为NULL即可。eg.

    struct file_operations dev_fops = {
    .llseek = NULL,
    .read = dev_read,
    .write = dev_write,
    .ioctl = dev_ioctl,
    .open = dev_open,
    .release = dev_release,
    };
    

    4.描述结构

    4.1分配

    cdev变量的定义可以采用静态和动态两种办法
    ·静态分配struct cdev mdev;
    ·动态分配struct cdev *pdev = cdev_alloc();

    4.2初始化

    struct cdev的初始化使用cdev_init函数来完成。
    cdev_init(struct cdev *cdev, const struct file_operations *fops)
    参数:
    cdev: 待初始化的cdev结构
    fops: 设备对应的操作函数集

    4.3注册

    字符设备的注册使用cdev_add函数来完成。
    cdev_add(struct cdev *p, dev_t dev, unsigned count)
    参数:
    p: 待添加到内核的字符设备结构
    dev: 设备号
    count: 该类设备的设备个数

    5.设备操作

    5.1 open()

    open设备方法是驱动程序用来为以后的操作,完成初始化准备工作的。在大部分驱动程序中,open完成标明次设备号、启动设备两个工作。
    原型:

    int (*open) (struct inode *, struct file *)
    

    5.2 release()

    设备方法有时也称为close
    原型:

    int (*release) (struct inode *, struct file *)
    

    5.3 read()

    read设备方法通常完成2件事情:

    • 从设备中读取数据(属于硬件访问类操作)
    • 将读取到的数据返回给应用程序
      原型:
    ssize_t (*read) (struct file *filp, char __user *buff, size_t count, loff_t *offp)
    参数分析:
    filp:与字符设备文件关联的file结构指针, 由内核创建。
    buff : 从设备读取到的数据,需要保存到的位置。由read系统调用提供该参数。
    count: 请求传输的数据量,由read系统调用提供该参数。
    offp: 文件的读写位置,由内核从file结构中取出后,传递进来。
    

    5.4 write()

    原型:

    ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *)
    

    5.5 补充

    每一个存在于文件系统里面的文件都会关联一个inode 结构,该结构主要用来记录文件物理上的信息。因此, 它和代表打开文件的file结构是不同的。一个文件没有被打开时不会关联file结构,但是却会关联一个inode 结构。
    重要成员:
    dev_t i_rdev:设备号

    6.驱动注销

    从内核中卸载驱动程序的时候,需要使用cdev_del函数来完成字符设备的注销。
    原型:

    void cdev_del(struct cdev *);
    
  • 相关阅读:
    JZ-C-36
    JZ-C-35
    JZ-C-34
    JZ-C-33
    JZ-C-32
    JZ-C-31
    JZ-C-30
    JZ-C-29
    JZ-C-28
    JZ-C-27
  • 原文地址:https://www.cnblogs.com/boyiliushui/p/6664591.html
Copyright © 2011-2022 走看看