📄 program-device-driver.html
字号:
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="Author" content="Edward Fu">
<meta name="GENERATOR" content="Mozilla/4.05 [zh-CN] (X11; I; Linux 2.1.127 i686) [Netscape]">
<title>Freesoft Linux FAQ -- LINUX下的设备驱动程序</title>
</head>
<body>
发信人: dfbb (赵无忌), 信区:
Linux
<br>标 题: LINUX下的设备驱动程序
<br>发信站: BBS 水木清华站 (Sat Dec 13 15:54:36 1997)
<br>
<br>
<br>发信人: olly (剑胆琴心), 信区: Linux
<br>标 题: LINUX下的设备驱动程序
<br>发信站: BBS 水木清华站 (Sat Jun 28 14:00:08 1997)
<br>
<br>
<br>
<br> 以下是本人毕业论文的一部分,是关于Linux下设备驱动程序的编写的。
<br>请各位同志们指正。
<br>
<br>
<br>
<br>
<br>
<br>三、UNIX系统下的设备驱动程序
<br>3.1、UNIX下设备驱动程序的基本结构
<br>在UNIX系统里,对用户程序而言,设备驱动程序隐藏了设备的具体细节,
<br>对各种不同设备提供了一致的接口,一般来说是把设备映射为一个特殊的设备文
<br>件,用户程序可以象对其它文件一样对此设备文件进行操作。UNIX对硬件设备
<br>支持两个标准接口:块特别设备文件和字符特别设备文件,通过块(字符)特别
<br>设备文件存取的设备称为块(字符)设备或具有块(字符)设备接口。
<br>块设备接口仅支持面向块的I/O操作,所有I/O操作都通过在内核地址空间
<br>中的I/O缓冲区进行,它可以支持几乎任意长度和任意位置上的I/O请求,即提
<br>供随机存取的功能。
<br>字符设备接口支持面向字符的I/O操作,它不经过系统的快速缓存,所以它
<br>们负责管理自己的缓冲区结构。字符设备接口只支持顺序存取的功能,一般不能
<br>进行任意长度的I/O请求,而是限制I/O请求的长度必须是设备要求的基本块长
<br>的倍数。显然,本程序所驱动的串行卡只能提供顺序存取的功能,属于是字符设
<br>备,因此后面的讨论在两种设备有所区别时都只涉及字符型设备接口。
<br>设备由一个主设备号和一个次设备号标识。主设备号唯一标识了设备类型,
<br>即设备驱动程序类型,它是块设备表或字符设备表中设备表项的索引。次设备号
<br>仅由设备驱动程序解释,一般用于识别在若干可能的硬件设备中,I/O请求所涉
<br>及到的那个设备。
<br>设备驱动程序可以分为三个主要组成部分:
<br>(1) 自动配置和初始化子程序,负责检测所要驱动的硬件设备是否存在和是否
<br>能正常工作。如果该设备正常,则对这个设备及其相关的、设备驱动程序
<br>需要的软件状态进行初始化。这部分驱动程序仅在初始化的时候被调用一
<br>次。
<br>(2) 服务于I/O请求的子程序,又称为驱动程序的上半部分。调用这部分是由
<br>于系统调用的结果。这部分程序在执行的时候,系统仍认为是和进行调用
<br>的进程属于同一个进程,只是由用户态变成了核心态,具有进行此系统调
<br>用的用户程序的运行环境,因此可以在其中调用sleep()等与进程运行环
<br>境有关的函数。
<br>(3) 中断服务子程序,又称为驱动程序的下半部分。在UNIX系统中,并不是
<br>直接从中断向量表中调用设备驱动程序的中断服务子程序,而是由UNIX
<br>系统来接收硬件中断,再由系统调用中断服务子程序。中断可以产生在任
<br>何一个进程运行的时候,因此在中断服务程序被调用的时候,不能依赖于
<br>任何进程的状态,也就不能调用任何与进程运行环境有关的函数。因为设
<br>备驱动程序一般支持同一类型的若干设备,所以一般在系统调用中断服务
<br>子程序的时候,都带有一个或多个参数,以唯一标识请求服务的设备。
<br>在系统内部,I/O设备的存取通过一组固定的入口点来进行,这组入口点是
<br>由每个设备的设备驱动程序提供的。一般来说,字符型设备驱动程序能够提供如
<br>下几个入口点:
<br>(1) open入口点。打开设备准备I/O操作。对字符特别设备文件进行打开操
<br>作,都会调用设备的open入口点。open子程序必须对将要进行的I/O
<br>操作做好必要的准备工作,如清除缓冲区等。如果设备是独占的,即同一
<br>时刻只能有一个程序访问此设备,则open子程序必须设置一些标志以表
<br>示设备处于忙状态。
<br>(2) close入口点。关闭一个设备。当最后一次使用设备终结后,调用close
<br>子程序。独占设备必须标记设备可再次使用。
<br>(3) read入口点。从设备上读数据。对于有缓冲区的I/O操作,一般是从缓
<br>冲区里读数据。对字符特别设备文件进行读操作将调用read子程序。
<br>(4) write入口点。往设备上写数据。对于有缓冲区的I/O操作,一般是把数
<br>据写入缓冲区里。对字符特别设备文件进行写操作将调用write子程序。
<br>(5) ioctl入口点。执行读、写之外的操作。
<br>(6) select入口点。检查设备,看数据是否可读或设备是否可用于写数据。
<br>select系统调用在检查与设备特别文件相关的文件描述符时使用select入口点。
<br>如果设备驱动程序没有提供上述入口点中的某一个,系统会用缺省的子程序
<br>来代替。对于不同的系统,也还有一些其它的入口点。
<br>
<br>3.2、LINUX系统下的设备驱动程序
<br>具体到LINUX系统里,设备驱动程序所提供的这组入口点由一个结构来向系
<br>统进行说明,此结构定义为:
<br>#include <linux/fs.h>
<br>struct file_operations {
<br> int (*lseek)(struct inode
*inode,struct file *filp,
<br>
off_t off,int pos);
<br> int (*read)(struct inode
*inode,struct file *filp,
<br>
char *buf, int count);
<br> int (*write)(struct inode
*inode,struct file *filp,
<br>
char *buf,int count);
<br> int (*readdir)(struct inode
*inode,struct file *filp,
<br>
struct dirent *dirent,int count);
<br> int (*select)(struct inode
*inode,struct file *filp,
<br>
int sel_type,select_table *wait);
<br> int (*ioctl) (struct inode
*inode,struct file *filp,
<br>
unsigned int cmd,unsigned int arg);
<br> int (*mmap) (void);
<br>
<br> int (*open) (struct inode
*inode, struct file *filp);
<br> void (*release) (struct
inode *inode, struct file *filp);
<br> int (*fsync) (struct inode
*inode, struct file *filp);
<br>};
<br>其中,struct inode提供了关于特别设备文件/dev/driver(假设此设备名
<br>为driver)的信息,它的定义为:
<br>#include <linux/fs.h>
<br>struct inode {
<br> dev_t
i_dev;
<br> unsigned long
i_ino; /* Inode number */
<br> umode_t
i_mode; /* Mode of the file */
<br> nlink_t
i_nlink;
<br> uid_t
i_uid;
<br> gid_t
i_gid;
<br> dev_t
i_rdev; /* Device major and minor numbers*/
<br> off_t
i_size;
<br> time_t
i_atime;
<br> time_t
i_mtime;
<br> time_t
i_ctime;
<br> unsigned long
i_blksize;
<br> unsigned long
i_blocks;
<br> struct inode_operations
* i_op;
<br> struct super_block * i_sb;
<br> struct wait_queue * i_wait;
<br> struct file_lock * i_flock;
<br> struct vm_area_struct *
i_mmap;
<br> struct inode * i_next, *
i_prev;
<br> struct inode * i_hash_next,
* i_hash_prev;
<br> struct inode * i_bound_to,
* i_bound_by;
<br> unsigned short i_count;
<br> unsigned short i_flags;
/* Mount flags (see fs.h) */
<br> unsigned char i_lock;
<br> unsigned char i_dirt;
<br> unsigned char i_pipe;
<br> unsigned char i_mount;
<br> unsigned char i_seek;
<br> unsigned char i_update;
<br> union {
<br>
struct pipe_inode_info pipe_i;
<br>
struct minix_inode_info minix_i;
<br>
struct ext_inode_info ext_i;
<br>
struct msdos_inode_info msdos_i;
<br>
struct iso_inode_info isofs_i;
<br>
struct nfs_inode_info nfs_i;
<br> } u;
<br>};
<br>
<br>struct file主要用于与文件系统对应的设备驱动程序使用。当然,其它设
<br>备驱动程序也可以使用它。它提供关于被打开的文件的信息,定义为:
<br>#include <linux/fs.h>
<br>struct file {
<br> mode_t f_mode;
<br> dev_t f_rdev;
/* needed for /dev/tty */
<br> off_t f_pos;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -