【Linux 驱动】设备驱动程序再理解

学习设备驱动编程也有一段时间了,也写过了几个驱动程序,因此有对设备驱动程序有了一些新的理解和认识,总结一下。学习设备驱动编程也有一段时间了,也写过了几个驱动程序,因此有对设备驱动程序有了一些新的理解和认识,总结一下。

★什么是驱动程序

刚开始学习设备驱动程序的时候,产生了许多的问题。什么是驱动程序?驱动程序是干嘛的?它是如何工作的?它又是如何跟操作系统联系起来的?一系列的问题,现在有些地方还是不一定清楚,但是相比起刚开始的那个阶段,感觉自己还是清楚了很多。

设备驱动程序说白了(实质)就是为应用程序提供一组操作硬件设备的接口。驱动程序是可以直接操作硬件上的资源,例如GPIO的各个寄存器,从而控制GPIO的方向(输出或是输入)、引脚的电平高低、中断等等。驱动程序所做的事情大部分与直接在51单片机或是AVR单片机写的程序的功能相似,与它们不同的是驱动程序向上是为操作系统提供统一的接口的。

字符设备驱动控制LED灯 

Linux下PCI设备驱动程序之注册详解

裸机驱动与Linux设备驱动的区别

Linux设备驱动开发详解(第2版)源代码 下载

Linux设备驱动开发详解(第2版)高清PDF

★file_operations结构体

驱动设备有很多种,那么驱动它们的程序也会各有不同,那么驱动程序怎样向上提供统一的接口呢?最关键的就是这个结构体,它定义在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);
};

通过这个结构体将驱动程序中操作硬件资源的函数链接起来。那么当操作系统知道了这个结构体之后,也就是知道了这个驱动程序。那么操作系统如何知道这个结构体呢?

其实,Linux下设备大概可以分为两种:字符设备、块设备。内核中用2个全局变量存放这两类驱动程序:

字符设备重要的结构体:

static struct char_device_struct {
 struct char_device_struct *next;
 unsigned int major;
 unsigned int baseminor;
 int minorct;
 char name[64];
 struct file_operations *fops;
 struct cdev *cdev;  /* will die */
} *chrdevs[CHRDEV_MAJOR_HASH_SIZE];

当驱动程序调用register_chrdev(unsigned int major, const char * name, struct file_operations *fops)注册函数就是将file_operations结构体存放到数组chrdevs[CHRDEV_MAJOR_HASH_SIZE]数组中,数组下标就是驱动主设备号,这样驱动程序就能和内核有了关联。

★设备文件

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/64ccc57450212741a4b564f9f02c8eb7.html