linux设备驱动归纳总结(三):2.字符型设备的操作open、close、read、write

一、文件操作结构体file_operations
继续上次没讲完的问题,文件操作结构体到底是什么东西,为什么我注册了设备之后什么现象都没有?可以验证文件操作结构体的内容。
file_operations是一个函数指针的集合,用于存放我们定义的用于操作设备的函数的指针,如果我们不定义,它默认保留为NULL。
来个文件操作结构体的定义:
/*include/linux/fs.h*/
1310 struct file_operations {
1311 struct module *owner;
1312 loff_t (*llseek) (struct file *, loff_t, int);
1313 ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
1314 ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
1315 ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
1316 ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
1317 int (*readdir) (struct file *, void *, filldir_t);
1318 unsigned int (*poll) (struct file *, struct poll_table_struct *);
1319 int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);
1320 long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
1321 long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
1322 int (*mmap) (struct file *, struct vm_area_struct *);
1323 int (*open) (struct inode *, struct file *);
1324 int (*flush) (struct file *, fl_owner_t id);
1325 int (*release) (struct inode *, struct file *);
1326 int (*fsync) (struct file *, struct dentry *, int datasync);
1327 int (*aio_fsync) (struct kiocb *, int datasync);
1328 int (*fasync) (int, struct file *, int);
1329 int (*lock) (struct file *, int, struct file_lock *);
1330 ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
1331 unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
1332 int (*check_flags)(int);
1333 int (*flock) (struct file *, int, struct file_lock *);
1334 ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
1335 ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned i nt);
1336 int (*setlease)(struct file *, long, struct file_lock **);
1337 };
Tags: 

延伸阅读

最新评论

发表评论