新闻中心

EEPW首页 > 嵌入式系统 > 设计应用 > 嵌入式Linux设备驱动开发之:块设备驱动编程

嵌入式Linux设备驱动开发之:块设备驱动编程

作者:时间:2013-09-13来源:网络收藏

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

11.4块编程

块设备通常指一些需要以块(如512字节)的方式写入的设备,如IDE硬盘、SCSI硬盘、光驱等。它的驱动程序的编写过程与字符型程序的编写有很大的区别。

编程接口相对复杂,不如字符设备明晰易用。块设备驱动程序对整个系统的性能影响较大,速度和效率是设计块设备驱动程要重点考虑的问题。系统中使用缓冲区与访问请求的优化管理(合并与重新排序)来提高系统性能。

1.编程流程说明

块设备驱动程序的编写流程同字符设备驱动程序的编写流程很类似,也包括了注册和使用两部分。但与字符驱动设备所不同的是,块设备驱动程序包括一个request请求队列。它是当内核安排一次数据传输时在列表中的一个请求队列,以最大化系统性能为原则进行排序。在后面的读写操作时会详细讲解这个函数,图11.5为块设备驱动程序的流程图,请读者注意与字符设备驱动程序的区别。

图11.5块设备驱动程序流程图

2.重要数据结构

每个块设备物理实体由一个gendisk结构体来表示(在/linux/genhd.h>中定义),每个gendisk可以支持多个分区。

每个gendisk中包含了本物理实体的全部信息以及操作函数接口。整个块设备的注册过程是围绕gendisk来展开的。在驱动程序中需要初始化的gendisk的一些成员如下所示。

structgendisk

{

intmajor;/*主设备号*/

intfirst_minor;/*第一个次设备号*/

intminors;/*次设备号个数,一个块设备至少需要使用一个次设备号,而且块设

备的每个分区都需要一个次设备号,因此这个成员等于1,则表明该块

设备是不可被分区的,否则可以包含minors–1个分区。*/

chardisk_name[32];/*块设备名称,在/proc/partions中显示*/

structhd_struct**part;/*分区表*/

structblock_device_operations*fops;/*块设备操作接口,与字符设备的

file_operations结构对应*/

structrequest_queue*queue;/*I/O请求队列*/

void*private_data;/*指向驱动程序私有数据*/

sector_tcapacity;/*块设备可包含的扇区数*/

……/*其他省略*/

};

与字符设备驱动程序一样,块设备驱动程序也包含一个在linux/fs.h>中定义的block_device_operations结构,其定义如下所示。

structblock_device_operations

{

int(*open)(structinode*,structfile*);

int(*release)(structinode*,structfile*);

int(*ioctl)(structinode*,structfile*,unsigned,unsignedlong);

long(*unlocked_ioctl)(structfile*,unsigned,unsignedlong);

long(*compat_ioctl)(structfile*,unsigned,unsignedlong);

int(*direct_access)(structblock_device*,sector_t,unsignedlong*);

int(*media_changed)(structgendisk*);

int(*revalidate_disk)(structgendisk*);

int(*getgeo)(structblock_device*,structhd_geometry*);

structmodule*owner;

};

从该结构的定义中,可以看出块设备并不提供read()、write()等函数接口。对块设备的读写请求都是以异步方式发送到设备相关的request队列之中。

linux相关文章:linux教程



上一页 1 2 下一页

评论


相关推荐

技术专区

关闭