3.驱动程序结构

3.1一个设备驱动程序模块的基本框架

设备驱动程序流程图

在系统内部,I/O设备的存取通过一组固定的入口点来进行,入口点也可以理解为设备的句柄,就是对设备进行操作的基本函数。字符型设备驱动程序提供如下几个入口点:

—  open入口点。打开设备准备I/O操作。对字符设备文件进行打开操作,都会调用设备的open入口点。open子程序必须对将要进行的I/O操作做好必 要的准备工作,如清除缓冲区等。如果设备是独占的,即同一时刻只能有一个程序访问此设备,则open子程序必须设置一些标志以表示设备处于忙状态。

—  close入口点。关闭一个设备。当最后一次使用设备完成后,调用close子程序。独占设备必须标记设备方可再次使用。

—  read入口点。从设备上读数据。对于有缓冲区的I/O操作,一般是从缓冲区里读数据。对字符设备文件进行读操作将调用read子程序。

—  write入口点。往设备上写数据。对于有缓冲区的I/O操作,一般是把数据写入缓冲区里。对字符设备文件进行写操作将调用write子程序。

—  ioctl入口点。执行读、写之外的操作。

—       select入口点。检查设备,看数据是否可读或设备是否可用于写数据。select系统调用在检查与设备文件相关的文件描述符时使用select入口点。

3.1. file_operations结构体

struct file_operations {

structmodule *owner;

loff_t(*llseek) (struct file *, loff_t, int);

ssize_t(*read) (struct file *, char *, size_t, loff_t *);

ssize_t(*write) (struct file *, const char *, size_t, loff_t *);

int(*readdir) (struct file *, void *, filldir_t);

unsignedint (*poll) (struct file *, struct poll_table_struct *);

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

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

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

int(*flush) (struct file *);

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

int(*fsync) (struct file *, struct dentry *, int datasync);

int(*fasync) (int, struct file *, int);

int (*lock)(struct file *, int, struct file_lock *);

ssize_t(*readv) (struct file *, const struct iovec *, unsigned long, loff_t *);

ssize_t(*writev) (struct file *, const struct iovec *, unsigned long, loff_t *);

ssize_t(*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);

unsignedlong (*get_unmapped_area)(

struct file*,

unsignedlong,

unsignedlong,

unsignedlong,

unsignedlong

);

};

—  lseek,移动文件指针的位置,只能用于可以随机存取的设备。

—  read,进行读操作,buf为存放读取结果的缓冲区,count为所要读取的数据长度。

—  write,进行写操作,与read类似。

—  select,进行选择操作。

—  ioctl,进行读、写以外的其他操作。

—  mmap,用于把设备的内容映射到地址空间,一般只有块设备驱动程序使用。

—  open,打开设备进行I/O操作。返回0表示成功,返回负数表示失败。

—  release,即close操作

3.2.inode{}和file{}结构体

inode数据结构体提供关于特别设备文件的信息。file结构体主要是与文件系统对应的设备驱动程序使用。

struct file主要用于与文件系统相关的设备驱动程序,可提供关于被打开的文件的信息,定义如下:

struct file {

struct list_head   f_list;

struct dentry              *f_dentry;

struct vfsmount        *f_vfsmnt;

struct file_operations  *f_op;

atomic_t            f_count;

unsigned int              f_flags;

mode_t                     f_mode;

loff_t                 f_pos;

unsigned long           f_reada,f_ramax, f_raend, f_ralen, f_rawin;

struct fown_struct      f_owner;

unsigned int              f_uid,f_gid;

int                     f_error;

unsigned long            f_version;

/* needed for tty driver, and maybe others */

void                  *private_data;

/* preallocated helper kiobuf to speedup O_DIRECT */

struct kiobuf              *f_iobuf;

long                  f_iobuf_lock;

};

在用户自己的驱动程序中,首先要根据驱动程序的功能,完成file_operations结构中函数的实现。不需要的函数接口可以直接在 file_operations结构中初始化为NULL。file_operations中的变量会在驱动程序初始化时,注册到系统内部。每个进程对设备 的操作,都会根据主次设备号,转换成对file_operations结构的访问。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值