Linux-驱动开发-系统调用流程
- ■ Linux-系统调用流程
- ■ Linux-file_operations 结构体
■ Linux-系统调用流程
■ Linux-file_operations 结构体
在 Linux 内核文件 include/linux/fs.h 中有个叫做 file_operations 的结构体,此结构体就是 Linux 内核驱动操作函数集合,
1588 struct file_operations {
1589 struct module *owner; // 拥有该结构体的模块的指针,一般设置为 THIS_MODULE
1590 loff_t (*llseek) (struct file *, loff_t, int); //llseek 函数用于修改文件当前的读写位置。
1591 ssize_t (*read) (struct file *, char __user *, size_t, loff_t*); // read 函数用于读取设备文件。
1592 ssize_t (*write) (struct file *, const char __user *, size_t,loff_t *); //write 函数用于向设备文件写入(发送)数据。
1593 ssize_t (*read_iter) (struct kiocb *, struct iov_iter *);
1594 ssize_t (*write_iter) (struct kiocb *, struct iov_iter *);
1595 int (*iterate) (struct file *, struct dir_context *);
1596 unsigned int (*poll) (struct file *, struct poll_table_struct*); //是个轮询函数,用于查询设备是否可以进行非阻塞的读写。
1597 long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long); //unlocked_ioctl 函数提供对于设备的控制功能,与应用程序中的 ioctl 函数对应。
1598 long (*compat_ioctl) (struct file *, unsigned int, unsigned long); //compat_ioctl 函数与 unlocked_ioctl 函数功能一样,区别在于在 64 位系统上,
32 位的应用程序调用将会使用此函数。在 32 位的系统上运行 32 位的应用程序调用的是
unlocked_ioctl。
1599 int (*mmap) (struct file *, struct vm_area_struct *); //mmap 函数用于将设备的内存映射到进程空间中(也就是用户空间),一般帧缓
冲设备会使用此函数,比如 LCD 驱动的显存,将帧缓冲(LCD 显存)映射到用户空间中以后应用
程序就可以直接操作显存了,这样就不用在用户空间和内核空间之间来回复制。
1600 int (*mremap)(struct file *, struct vm_area_struct *);
1601 int (*open) (struct inode *, struct file *); //open 函数用于打开设备文件。
1602 int (*flush) (struct file *, fl_owner_t id);
1603 int (*release) (struct inode *, struct file *); //release 函数用于释放(关闭)设备文件,与应用程序中的 close 函数对应。
1604 int (*fsync) (struct file *, loff_t, loff_t, int datasync); //fasync 函数用于刷新待处理的数据,用于将缓冲区中的数据刷新到磁盘中
1605 int (*aio_fsync) (struct kiocb *, int datasync); //aio_fsync 函数与 fasync 函数的功能类似,只是 aio_fsync 是异步刷新待处理的
数据。
1606 int (*fasync) (int, struct file *, int);
1607 int (*lock) (struct file *, int, struct file_lock *);
1608 ssize_t (*sendpage) (struct file *, struct page *, int, size_t,loff_t *, int);
1609 unsigned long (*get_unmapped_area)(struct file *, unsigned long,unsigned long, unsigned long, unsigned long);
1610 int (*check_flags)(int);
1611 int (*flock) (struct file *, int, struct file_lock *);
1612 ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,loff_t *, size_t, unsigned int);
1613 ssize_t (*splice_read)(struct file *, loff_t *, structpipe_inode_info *, size_t, unsigned int);
1614 int (*setlease)(struct file *, long, struct file_lock **, void**);
1615 long (*fallocate)(struct file *file, int mode, loff_t offset, loff_t len);
1617 void (*show_fdinfo)(struct seq_file *m, struct file *f);
1618 #ifndef CONFIG_MMU
1619 unsigned (*mmap_capabilities)(struct file *);
1620 #endif
1621 };
序号 | 描述 |
---|---|
1 | 驱动加载成功以后会在“/dev”目录下生成一个相应的文件 |
2 | 比如现在有个叫做/dev/led 的驱动文件,此文件是 led 灯的驱动文件。应用程序使用 open 函数来打开文件/dev/led,使用完成以后使用close 函数关闭/dev/led 这 个文件。 open和 close 就是打开和关闭 led 驱动的函数,如果要点亮或关闭 led |
3 | 应用程序运行在用户空间,而 Linux 驱动属于内核的一部分,因此驱动运行于内核空间 |
4 | |
5 | |
6 | |
7 | |
8 |