`
lovecontry
  • 浏览: 1035374 次
文章分类
社区版块
存档分类
最新评论

Linux驱动编程 step-by-step (三)

 
阅读更多

字符设备中 重要的数据结构
大部分字符驱动设计三个重要的数据结构
<linux/fs.h>
structfile_operations
structfile
structinode

一、文件操作
在之前的一篇文章中已经有介绍了如何去生情字符设备设备号,但是没有做任何的工作,也就只能写一个不能工作的字符设备;
struct file_operations 结构域用来连接设备与操作,实现系统调用。
重要字段介绍:
structfile_operations{
structmodule*owner;//表示拥有这个结构模块的指针,几乎所有的驱动都会设置为THIS_MODULE<linux/module.h>
loff_t(*llseek)(structfile*,loff_t,int);//文件读写位置调整函数
ssize_t(*read)(structfile*,char__user*,size_t,loff_t*);//从设备读取数据
ssize_t(*write)(structfile*,constchar__user*,size_t,loff_t*);//项设备写入数据
unsignedint(*poll)(structfile*,structpoll_table_struct*);//查询文件描述符上的读取写入是否被阻塞
int(*mmap)(structfile*,structvm_area_struct*);//将设备内存映射到进程空间
int(*open)(structinode*,structfile*);//对应于打开设备
int(*release)(structinode*,structfile*);//对应于关闭一个设备
.
.
.
};
结构还有很多的操作因为还没有学会,就没有多做介绍,(ioctl函数操作在2.6.35之后就变成了unlocked_ioctl、compat_ioctl其实作用也不是很大 也没有做介绍,以后有机会再做介绍)
当open 与release置为NULL时 就以内核默认的方式进行打开或者关闭,并且不会出错,
当其他函数没有被定义时候,应用程序调用会出错。
下边是一个最重要的几个设备操作方法的定义。
structfile_operationssimple_fops={
.owner=THIS_MODULE,
.open=simple_open,
.release=simple_close,
.read=simple_read,
.write=simple_write,
.llseek=simple_llseek,
.poll=simple_poll,
.mmap=simple_mmap,
};

二、file结构
这里介绍的file结构并不是C语言中的FILE结构,两者没有任何关联,struct file只是一个内核的结构,每一个打开的文件,都会对应一个struct file结构,一个文件可以对应于不同的struct file结构
structfile{
structpathf_path;//文件位置
conststructfile_operations*f_op;.//文件操作符
spinlock_tf_lock;
atomic_long_tf_count;
unsignedintf_flags;//文件标识(O_NONBLOCK,O_RDONLY等由应用程序传入)
fmode_tf_mode;//文件模式可读可写
loff_tf_pos;//文件读写位置
structfown_structf_owner;
conststructcred*f_cred;
structfile_ra_statef_ra;
.
.
.
void*private_data;//mostimportant!!私有数据,驱动可以使用它指向任何的数据结构

};
三、inode结构
linux内核使用inode结构表示一个文件,与file不同,file可以理解为用来表示文件描述符的结构,一个文件可以对应很多的文件描述符,而最后只会指向同一个inode结构

structinode{
...
dev_ti_rdev;//保存设备号
union{
structpipe_inode_info*i_pipe;
structblock_device*i_bdev;
structcdev*i_cdev;//指向了structcdev结构
};
...
};

四、file 结构 与inode结构图解

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics