当前位置:Linux教程 - Linux综合 - LINUX下的设备驱动程序

LINUX下的设备驱动程序

  三、UNIX系统下的设备驱动程序      3.1、UNIX下设备驱动程序的基本结构      在UNIX系统里,对用户程序而言,设备驱动程序隐藏了设备的具体细节,对各种不同设备提供了一致的接口,一般来说是把设备映射为一个特殊的设备文件,用户程序可以象对其它文件一样对此设备文件进行操作。UNIX对硬件设备支持两个标准接口:块特别设备文件和字符特别设备文件,通过块(字符)特别设备文件存取的设备称为块(字符)设备或具有块(字符)设备接口。      块设备接口仅支持面向块的I/O操作,所有I/O操作都通过在内核地址空间中的I/O缓冲区进行,它可以支持几乎任意长度和任意位置上的I/O请求,即提供随机存取的功能。      字符设备接口支持面向字符的I/O操作,它不经过系统的快速缓存,所以它们负责管理自己的缓冲区结构。字符设备接口只支持顺序存取的功能,一般不能进行任意长度的I/O请求,而是限制I/O请求的长度必须是设备要求的基本块长的倍数。显然,本程序所驱动的串行卡只能提供顺序存取的功能,属于是字符设备,因此后面的讨论在两种设备有所区别时都只涉及字符型设备接口。      设备由一个主设备号和一个次设备号标识。主设备号唯一标识了设备类型,即设备驱动程序类型,它是块设备表或字符设备表中设备表项的索引。次设备号仅由设备驱动程序解释,一般用于识别在若干可能的硬件设备中,I/O请求所涉及到的那个设备。      设备驱动程序可以分为三个主要组成部分:      (1) 自动配置和初始化子程序,负责检测所要驱动的硬件设备是否存在和是否能正常工作。如果该设备正常,则对这个设备及其相关的、设备驱动程序需要的软件状态进行初始化。这部分驱动程序仅在初始化的时候被调用一次。      (2) 服务于I/O请求的子程序,又称为驱动程序的上半部分。调用这部分是由于系统调用的结果。这部分程序在执行的时候,系统仍认为是和进行调用的进程属于同一个进程,只是由用户态变成了核心态,具有进行此系统调用的用户程序的运行环境,因此可以在其中调用sleep()等与进程运行环境有关的函数。      (3) 中断服务子程序,又称为驱动程序的下半部分。在UNIX系统中,并不是直接从中断向量表中调用设备驱动程序的中断服务子程序,而是由UNIX系统来接收硬件中断,再由系统调用中断服务子程序。中断可以产生在任何一个进程运行的时候,因此在中断服务程序被调用的时候,不能依赖于任何进程的状态,也就不能调用任何与进程运行环境有关的函数。因为设备驱动程序一般支持同一类型的若干设备,所以一般在系统调用中断服务子程序的时候,都带有一个或多个参数,以唯一标识请求服务的设备。      在系统内部,I/O设备的存取通过一组固定的入口点来进行,这组入口点是由每个设备的设备驱动程序提供的。一般来说,字符型设备驱动程序能够提供如下几个入口点:      (1) open入口点。打开设备准备I/O操作。对字符特别设备文件进行打开操作,都会调用设备的open入口点。open子程序必须对将要进行的I/O操作做好必要的准备工作,如清除缓冲区等。如果设备是独占的,即同一 时刻只能有一个程序访问此设备,则open子程序必须设置一些标志以表示设备处于忙状态。      (2) close入口点。关闭一个设备。当最后一次使用设备终结后,调用close子程序。独占设备必须标记设备可再次使用。      (3) read入口点。从设备上读数据。对于有缓冲区的I/O操作,一般是从缓冲区里读数据。对字符特别设备文件进行读操作将调用read子程序。      (4) write入口点。往设备上写数据。对于有缓冲区的I/O操作,一般是把数据写入缓冲区里。对字符特别设备文件进行写操作将调用write子程序。      (5) ioctl入口点。执行读、写之外的操作。      (6) select入口点。检查设备,看数据是否可读或设备是否可用于写数据。select系统调用在检查与设备特别文件相关的文件描述符时使用select入口点。如果设备驱动程序没有提供上述入口点中的某一个,系统会用缺省的子程序来代替。对于不同的系统,也还有一些其它的入口点。      3.2、Linux系统下的设备驱动程序      具体到LINUX系统里,设备驱动程序所提供的这组入口点由一个结构来向系统进行说明,此结构定义为:    #include   strUCt file_operations {   int (*lseek)(struct inode *inode,struct file *filp,   off_t off,int pos);   int (*read)(struct inode *inode,struct file *filp,   char *buf, int count);   int (*write)(struct inode *inode,struct file *filp,   char *buf,int count);   int (*readdir)(struct inode *inode,struct file *filp,   struct dirent *dirent,int count);   int (*select)(struct inode *inode,struct file *filp,   int sel_type,select_table *wait);   int (*ioctl) (struct inode *inode,struct file *filp,   unsigned int cmd,unsigned int arg);   int (*mmap) (void);   int (*open) (struct inode *inode, struct file *filp);   void (*release) (struct inode *inode, struct file *filp);   int (*fsync) (struct inode *inode, struct file *filp);  };      其中,struct inode提供了关于特别设备文件/dev/driver(假设此设备名为driver)的信息,它的定义为:    #include   struct inode {   dev_t i_dev;   unsigned long i_ino; /* Inode number */   umode_t i_mode; /* Mode of the file */   nlink_t i_nlink;   uid_t i_uid;   gid_t i_gid;   dev_t i_rdev; /* Device major and minor numbers*/   off_t i_size;   time_t i_atime;   time_t i_mtime;   time_t i_ctime;   unsigned long i_blksize;   unsigned long i_blocks;   struct inode_operations * i_op;   struct super_block * i_sb;   struct wait_queue * i_wait;   struct file_lock * i_flock;   struct vm_area_struct * i_mmap;   struct inode * i_next, * i_prev;   struct inode * i_hash_next, * i_hash_prev;   struct inode * i_bound_to, * i_bound_by;   unsigned short i_count;   unsigned short i_flags; /* Mount flags (see fs.h) */   unsigned char i_lock;   unsigned char i_dirt;   unsigned char i_pipe;   unsigned char i_mount;   unsigned char i_seek;   unsigned char i_update;   union {   struct pipe_inode_info pipe_i;   struct minix_inode_info minix_i;   struct ext_inode_info ext_i;   struct msdos_inode_info msdos_i;   struct iso_inode_info isofs_i;   struct nfs_inode_info nfs_i;   } u;  };      struct file主要用于与文件系统对应的设备驱动程序使用。当然,其它设备驱动程序也可以使用它。它提供关于被打开的文件的信息,定义为:    #include   struct file {   mode_t f_mode;   dev_t f_rdev; /* needed for /dev/tty */   off_t f_pos; /* Curr. posn in file */   unsigned short f_flags; /* The flags arg passed to open */   unsigned short f_count; /* Number of opens on this file */   unsigned short f_reada;   struct inode *f_inode; /* pointer to the inode struct */   struct file_operations *f_op;/* pointer to the fops struct*/  };      在结构file_operations里,指出了设备驱动程序所提供的入口点位置,分别是:      (1) lseek,移动文件指针的位置,显然只能用于可以随机存取的设备。      (2) read,进行读操作,参数buf为存放读取结果的缓冲区,count为所要读取的数据长度。返回值为负表示读取操作发生错误,否则返回实际读取的字节数。对于字符型,要求读取的字节数和返回的实际读取字节数都必须是inode->i_blksize的的倍数。      (3) write,进行写操作,与read类似。      (4) readdir,取得下一个目录入口点,只有与文件系统相关的设备驱动程序才使用。      (5) selec,进行选择操作,如果驱动程序没有提供select入口,select操作将会认为设备已经准备好进行任何的I/O操作。      (6) ioctl,进行读、写以外的其它操作,参数cmd为自定义的的命令。      (7) mmap,
[1] [2] 下一页 

(出处:http://www.sheup.com)


上一页 [1] [2]