【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

  • 释放设备文件

fsync

  • 刷新待处理的数据,将缓冲区的数据刷新到磁盘中

aio_fsync

  • 与fsync功能类似,aio_fsync是异步刷新待处理的数据

fasync

  • 异步通知使用,通知设备它的FASYNC标志的改变
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
};

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
`file_operations` 结构体Linux 内核中用来实现字符设备驱动的关键数据结构之一。它定义了一组函数指针,用于实现对字符设备的操作,包括打开设备、关闭设备、读取数据、写入数据、控制设备等等。具体来说,`file_operations` 结构体中定义的函数指针包括以下方法: - `open`: 打开设备时调用的方法。 - `release`: 关闭设备时调用的方法。 - `read`: 读取设备数据时调用的方法。 - `write`: 写入设备数据时调用的方法。 - `ioctl`: 控制设备时调用的方法。 - `mmap`: 内存映射时调用的方法。 这些方法实现了对字符设备的各种操作,可以根据具体的需求进行自定义实现。例如,`read` 方法可以从设备中读取数据并返回给应用程序,`write` 方法可以将应用程序传递的数据写入设备等等。在实现这些方法时,需要注意遵守一些规则和限制,例如不允许阻塞式操作等等。 需要注意的是,`file_operations` 结构体是在字符设备驱动中定义的,用于告诉内核如何操作设备。在注册字符设备时,需要将 `file_operations` 结构体作为参数传递给 `register_chrdev()` 函数。例如: ``` struct file_operations my_fops = { .owner = THIS_MODULE, .open = my_open, .release = my_release, .read = my_read, .write = my_write, .ioctl = my_ioctl, }; register_chrdev(my_major, "my_device", &my_fops); ``` 在上述代码中,`my_fops` 是自定义的 `file_operations` 结构体,包含了各种操作设备的方法。`register_chrdev()` 函数用于注册字符设备,并将 `my_fops` 作为参数传递给该函数。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

奶油芝士汉堡包

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值