Linux驱动开发file_operations结构体
Posted XXX_UUU_XXX
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux驱动开发file_operations结构体相关的知识,希望对你有一定的参考价值。
file_operations结构体是Linux内核驱动操作函数集合,在内核文件include/linux/fs.h中。根据具体驱动需求实现哪些函数。
字符设备驱动开发中常用的函数
owner
- 拥有该结构体的模块的指针,一般设置为THIS_MODULE
llseek
- 修改文件当前的读写位置
read
- 读取设备文件
write
- 向设备文件写入数据
poll
- 查询设备是否可以进行非阻塞的读写
unlocked_ioctl
- 提供对设备的控制功能,与应用程序中的ioctl函数对应
compat_ioctl
- 与unlocked_ioctl功能一样,不同的是64位系统中的32位的应用程序会调用compat_ioctl,32位系统中的32位的应用程序会调用unlocked_ioctl
mmap
- 将设备的内存映射到用户空间,一般帧缓冲设备会用mmap函数,如LCD驱动的显存,将LCD显存映射到用户空间后,应用程序就可以直接操作显存,不需要在用户空间和内核空间来回复制。
open
- 打开设备文件
release
- 释放设备文件
fasync
- 刷新待处理的数据,将缓冲区的数据刷新到磁盘中
aio_fsync
- 与fasync功能类似,aio_fsync是异步刷新待处理的数据
struct file_operations
struct module *owner;
loff_t (*llseek) (struct file *, loff_t, int);
ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
ssize_t (*read_iter) (struct kiocb *, struct iov_iter *);
ssize_t (*write_iter) (struct kiocb *, struct iov_iter *);
int (*iterate) (struct file *, struct dir_context *);
unsigned int (*poll) (struct file *, struct poll_table_struct *);
long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
int (*mmap) (struct file *, struct vm_area_struct *);
int (*mremap)(struct file *, struct vm_area_struct *);
int (*open) (struct inode *, struct file *);
int (*flush) (struct file *, fl_owner_t id);
int (*release) (struct inode *, struct file *);
int (*fsync) (struct file *, loff_t, loff_t, int datasync);
int (*aio_fsync) (struct kiocb *, int datasync);
int (*fasync) (int, struct file *, int);
int (*lock) (struct file *, int, struct file_lock *);
ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
int (*check_flags)(int);
int (*flock) (struct file *, int, struct file_lock *);
ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
int (*setlease)(struct file *, long, struct file_lock **, void **);
long (*fallocate)(struct file *file, int mode, loff_t offset,
loff_t len);
void (*show_fdinfo)(struct seq_file *m, struct file *f);
#ifndef CONFIG_MMU
unsigned (*mmap_capabilities)(struct file *);
#endif
;
以上是关于Linux驱动开发file_operations结构体的主要内容,如果未能解决你的问题,请参考以下文章
003_linux驱动之_file_operations函数