设备驱动程序

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

设备驱动程序2设备驱动程序的作用•设备驱动程序是一个软件层,该软件层使硬件设备响应预定义好的编程接口,我们已经熟悉这些接口,它由一组控制设备的函数(open,read,ioctl等等)组成,这些函数的实际实现由设备驱动程序全权负责。•设备驱动程序(应该只是)为系统的其它部分提供各种使用设备的能力,使用设备的方法应该由应用程序决定。3structfile_operations{structmodule*owner;loff_t(*llseek)(structfile*,loff_t,int);ssize_t(*read)(structfile*,char*,size_t,loff_t*);ssize_t(*write)(structfile*,constchar*,size_t,loff_t*);int(*readdir)(structfile*,void*,filldir_t);unsignedint(*poll)(structfile*,structpoll_table_struct*);int(*ioctl)(structinode*,structfile*,unsignedint,unsignedlong);int(*mmap)(structfile*,structvm_area_struct*);int(*open)(structinode*,structfile*);int(*flush)(structfile*);int(*release)(structinode*,structfile*);int(*fsync)(structfile*,structdentry*,intdatasync);int(*fasync)(int,structfile*,int);int(*lock)(structfile*,int,structfile_lock*);ssize_t(*readv)(structfile*,conststructiovec*,unsignedlong,loff_t*);ssize_t(*writev)(structfile*,conststructiovec*,unsignedlong,loff_t*);ssize_t(*sendpage)(structfile*,structpage*,int,size_t,loff_t*,int);unsignedlong(*get_unmapped_area)(structfile*,unsignedlong,unsignedlong,unsignedlong,unsignedlong);};structfile_operationsinclude/linux/fs.h设备驱动程序则编写设备驱动程序的主要工作就是编写如上子函数,并填充file_operations的各个域一个最简单字符驱动程序,由下面7个函数和1个结构体就可组成。Open(),Release,staticintmy_open(structinode*inode,structfile*filp){设备打开时的操作…}staticintmy_release(structinode*inode,structfile*filp){设备关闭时的操作…}staticintmy_write(structfile*file,constchar*buffer,size_tcount,loff_t*ppos){设备写入时的操作…}staticintmy_read(structfile*file,constchar*buffer,size_tcount,loff_t*ppos){设备读取时的操作…}()Write(),Read()Ioctl()Init(),Exit()Structfile_operationstaticint__initmy_init(void){初始化硬件,注册设备,创建设备节点…}staticvoid__exitmy_exit(void){删除设备节点,注销设备…}{设备的控制操作……}Staticintmy_ioctl(structinode*inode,structfile*filp,unsignedintcmd,unsignedlongarg)staticstructfile_operationsmy_fops={对文件操作结构体成员定义初始值…}7在操作系统中的位置•设备驱动程序是内核代码的一部分。•驱动程序的地址空间是内核的地址空间(copy_to_user函数等)。•驱动程序的代码直接对设备硬件(实际是设备的各种寄存器)进行控制(实际就是读写操作)。•应用程序通过操作系统的系统调用执行相应的驱动程序函数。中断则直接执行相应的中断程序代码。•设备驱动程序的file_operations结构体的地址被注册到内核中的设备链表中。•块设备和字符设备以设备文件的方式建立在文件系统中的/dev目录下,而且每个设备都有一个主设备号和一个次设备号。设备号•主设备号•驱动程序在初始化时,会注册它的驱动及对应主设备号到系统中,这样当应用程序访问设备节点时,系统就知道它所访问的驱动程序了。你可以通过/proc/devices文件来查看驱动系统设备的主设备号。•次设备号•驱动程序遍历设备时,每发现一个它能驱动的设备,就创建一个设备对象,并为其分配一个次设备号以区分不同的设备。这样当应用程序访问设备节点时驱动程序就可以根据次设备号知道它说访问的设备了。9ls-l/devcrw-r-----1rootroot1,1Jan100:00memcrw-r-----1rootroot1,2Jan100:00kmemcrw-rw-rw-1rootroot1,3Jan100:00nullcrw-r-----1rootroot1,4Jan100:00portcrw-rw-rw-1rootroot1,5Jan100:00zerocrw-rw-rw-1rootroot1,7Jan100:00fullcrw-r--r--1rootroot1,8Jan100:00randomcrw-r--r--1rootroot1,9Jan100:00urandomcrw-rw-rw-1rootroot5,0Jan100:00ttycrw-------1rootroot5,1Jan100:00consolecrw-rw-rw-1rootroot5,2Jan100:00ptmxdrwxr-xr-x1rootroot0Jan100:00ptydrwxr-xr-x2rootroot0Jan100:00ptsdrwxr-xr-x1rootroot0Jan100:00rddrwxr-xr-x1rootroot0Jan100:00mtddrwxr-xr-x1rootroot0Jan100:00mtdblockcrw-------1rootroot4,64Jan100:15ttyS0crw-------1rootroot4,65Jan100:00ttyS1crw-------1rootroot4,66Jan100:00ttyS2crw-------1rootroot4,67Jan100:00ttyS3crw-------1rootroot4,68Jan100:00ttyS4drwxr-xr-x1rootroot0Jan100:00miscc:字符设备b:块设备主设备号次设备号10设备驱动程序源代码的基本结构/**驱动程序简单说明:*驱动程序的作用:这是一个字符设备驱动程序的基本框架结构*被驱动设备的简单描述:将使用AT91RM9200的PB端口为例进行说明*一些特殊的考虑等:如PB21作为可以产生中断的输入引脚(本例未实现)*版本,创建日期,作者等:1.0版,2006年1月6日*/#ifndef__KERNEL__#define__KERNEL__#endif#ifndefMODULE#defineMODULE#endif#includelinux/config.h#includelinux/module.h...#includeasm/arch/hardware.h表明这个模块将用于内核,也可以在编译时通过–D选项指定,如gcc–D__KERNEL__。参见Makefile。内核头文件,需要根据具体驱动程序和用到的内核模块确定。表明这个驱动程序将以模块的方式编译和使用,也可以在编译时通过–D选项指定,如gcc–DMODULE。参见Makefile。11/**驱动程序中使用的各种函数的原型声明。标准的作法是将函数原型声明*放在一个头文件中,然后在该文件开始处使用#include引用,并在该*文件中定义。**这里我们将函数的声明和定义放在一起。所以下面的代码既是函数的声明,*也是函数的定义。*/staticssize_tspioc_read(structfile*filp,char*buffsize_tcnt,loof_t*off){/*这里是read函数的代码*/returnret;}staticssize_tspioc_write(structfile*filp,char*buffsize_tcnt,loff_t*off){/*这里是write函数的代码*/returnret;}12staticintspioc_ioctl(structinode*inode,structfile*filpunsignedintcmd,unsignedlongarg){/*这里是ioctl函数的代码,它的一般格式为一个switch分支语句*switch(cmd){*caseCMD1:*...*break;*...*caseCMDn:*...*break*default:*...*break;*}*/returnret;}ioctl()函数用于控制驱动程序本身的一些特性和参数,如设定驱动程序使用的缓冲区的大小,设定串行通讯的速率等。13staticintspioc_open(structinode*inode,structfile*filp){/*这里是open函数的代码*/returnret;}staticintspioc_close(structinode*inode,structfile*filp){/*这里是close函数的代码*/returnret;}上述5个函数,既read(),write(),ioctl(),open(),close(),是一个字符设备驱动程序最基本的需要由驱动程序的作者完成的函数。这5个函数将对应于相应的5个系统调用:read()-spioc_read()write()-spioc_write()ioctl()-spioc_ioctl()open()-spioc_open()close()-spioc_close()系统调用驱动程序函数14staticstructfile_operationsspioc_fops={read:spioc_read,write:spioc_write,ioctl:spioc_ioctl,open:spioc_open,release:spioc_close,};file_operations是一个结构体类型,定义在include/linux/fs.h中。上述代码定义了一个file_operations类型的结构体spioc_fops,并将其中的一些成员赋了初值。由于spioc_fops是一个静态变量,所以其他成员的初值是“零”。结构体spioc_fops将作为一个参数在注册一个设备驱动程序时传递给内核。内核使用设备链表维护各种注册的设备。不同类型的设备使用不同的链表。15staticint__initspioc_init(void){/*设备初始化代码等*/if(register_chrdev(SPIOC_MAJOR,“spioc”,&spioc_fops)){printk(KERN_ERR“spioc.c:unabletoregister”“thedevicewithmajor

1 / 27
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功