Linux 字符设备操作函数

在Linux内核文件include/linux/fs.h中有个叫做file_operations的结构体,此结构体就是Linux字符设备操作函数集合,内容如下所示:

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 *); 
	int (*iterate_shared) (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 (*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 (*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 
	ssize_t (*copy_file_range)(struct file *, loff_t, struct file *, 	loff_t, size_t, unsigned int); 
	int (*clone_file_range)(struct file *, loff_t, struct file *, loff_t, 	u64); 
	ssize_t (*dedupe_file_range)(struct file *, u64, u64, struct file *, 	u64); 
} __randomize_layout;

简单介绍一下file_operation结构体中比较重要的、常用的函数:

  • owner拥有该结构体的模块的指针,一般设置为THIS_MODULE
  • llseek函数用于修改文件当前的读写位置;
  • read函数用于读取设备文件;
  • write函数用于向设备文件写入(发送)数据;
  • poll是个轮询函数,用于查询设备是否可以进行非阻塞的读写;
  • unlocked_ioctl函数提供对于设备的控制功能,与应用程序中的ioctl函数对应;
  • compat_ioctl函数与unlocked_ioctl函数功能一样,区别在于在64位系统上,32位的应用程序调用将会使用此函数。在32位的系统上运行32位的应用程序调用的是unlocked_ioctl
  • mmap函数用于将设备的内存映射到进程空间中(也就是用户空间),一般帧缓冲设备会使用此函数,比如LCD驱动的显存,将帧缓冲(LCD显存)映射到用户空间中以后应用程序就可以直接操作显存了,这样就不用在用户空间和内核空间之间来回复制;
  • open函数用于打开设备文件;
  • release函数用于释放(关闭)设备文件,与应用程序中的close函数对应;
  • fsync函数用于刷新待处理的数据,用于将缓冲区中的数据刷新到磁盘中;
  • fasync函数与fsync函数的功能类似,只是fasync是异步刷新待处理的数据。

我们在设备驱动开发中最主要的工作就是实现上面这些函数。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值