新闻中心

EEPW首页 > 嵌入式系统 > 设计应用 > 嵌入式Linux系统设备驱动程序的开发

嵌入式Linux系统设备驱动程序的开发

作者:时间:2012-05-08来源:网络收藏

引言

本文引用地址:http://www.eepw.com.cn/article/171494.htm

是一个遵循POSIX标准的免费操作。具有BSD和SYSV的扩展特性。与其他操作相比,以其可应用于多种硬件平台、内核高效稳定、源码开放、软件丰富、网络通信和文件管理机制完善等优良特性而正被作为研究热点,越来越多的研究人员采用平台来自己的产品。Linux在Linux内核源代码中占有很大比例,从2.0、2.2到 2.4版本的内核,源代码的长度日益增加,其实主要是在增加。

的编写

设备驱动程序是linux内核的一部分,是操作系统内核和机器硬件之间的接口,它由一组函数和一些私有数据组成,是连接应用程序与具体硬件的桥梁。Linux的一个基本特点是它对硬件设备的管理抽象化,系统中的每一个设备都用一个特殊的文件来表示。所有的硬件设备都像普通的文件一样看待,使用与操作系统相同的标准系统来进行打开、读写和关闭。

在Linux 操作系统下有3类主要的设备文件类型:块设备、字符设备、网络设备。字符设备是指存取时没有缓存的设备。可像文件一样访问字符设备,字符设备驱动程序负责实现这些行为。系统的控制台和并口就是字符设备的例子,它们可以很好地用“流”来描述。块设备是文件系统的宿主,如磁盘。 Linux允许像字符设备那样读取块设备——允许一次传输任意数目的字节。结果是,字符设备和块设备读取数方式一致。而网络设备不同于字符设备和块设备, 它面向的上一层不是文件系统而是网络协议层,是通过BSD套接口访问数据。与设备相对应的是三类设备驱动程序,字符设备驱动程序、块设备驱动程序、网络设备驱动程序。

字符设备驱动程序、块设备驱动程序与网络设备驱动程序的结构体是不同的。

在linux 源代码linux/ include / linux/ fs. h中定义了字符设备和块设备驱动程序中必须使用的file_operations结构,每个设备驱动都实现这个接口所定义的部分或全部函数。随着内核的不断升级, file_operations结构也越来越大,不同的版本的内核会稍有不同。file_operations定义如下:

struct file_operations{

int( * lseek) ( struct inode * , struct file * , off_t , int) ; int( *release) ( struct inode * , struct file * ) ;

int( * read) ( struct inode * , struct file * , char * , int) ; int( * fsync) ( struct inode *, struct file * ) ;

int( *write) ( struct inode * , struct file * , const char *, int) ; int( * fasync) ( struct inode * , struct file *, int) ;

int( * readdir) ( struct inode , struct file , void * , dilldir) ; int( *check_media_change) ( kdev_t dev) ;

int(*select) ( struct inode *, struct file * , int, select_table * ) ; int( * revalidate) ( kdev_t dev) ; };

int ( * ioctl) ( struct inode * , struct file *, unsigned int, unsigned long) ;

int( *mmap) ( struct inode * , struct file * , struct vm_area_struct * ) ;

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

应用程序只有通过对设备文件的open、release、read、write、ioctl等才能访问字符设备和块设备。用户自己定义好 file_operations结构后,编写出设备实际所需要的各操作函数,对于不需要的操作函数用NULL初始化,这些操作函数将被注册到内核,当应用程序对设备相应的设备文件进行文件操作时,内核会找到相应的操作函数,并进行调用。如果操作函数使用NULL,操作函数就进行默认处理。

对于字符设备而言,llseek( ),read( ),write(),ioctl( ),open( ),release( )这些函数是不可缺的;对于块设备,open( ),release( ),ioctl(),check_media_change( ),revalidate( )是不可缺少的。

网络设备结构体 net_device 定义在 includelinuxnetdevice.h 里,如下所示:

struct net_device

{

char name ; int (*init)(struct

net_device *dev);

unsigned short flags ; int (*open)

(struct net_device *dev);

unsigned long base_addr; int

(*stop)(struct net_device *dev)

unsigned int irq ; int

(*hard_start_xmit)(struct sk_buff *skb,

unsigned char dev_addr; struct

net_device *dev);

unsigned char addr_len; int

(*set_mac_address)( struct net_device

unsigned long trans_start; *dev,void* addr);

……

}

linux操作系统文章专题:linux操作系统详解(linux不再难懂)

上一页 1 2 3 下一页

评论


相关推荐

技术专区

关闭