linux里有一句很经典的话就,“linux一切皆文件”,写linux的驱动也跟文件脱不了关系。下面是一个字符驱动的简单运行的原理。
app--> (open, read, write, 来操作字符设备的文件)
|
----------------------------------------------------------------------------------------------------------------------------------------------------
内核层 open
cdev cdev cdev
open-> 内核里面的open
----------------------------------------------------|-------------|-----------|-------------------------------------------------------------------------
设备 设备 设备
=====================================================================================
cdev设备的注册函数
int register_chrdev(unsigned int major, const char *name, const struct file_operations *fops)
major:申请的主设备号
> 0 -> 申请这个指定的设备号
==0 -> 由系统自动分配
name :设备名
fops: 操作集
cdev设备的撤销函数
void unregister_chrdev(unsigned int major, const char *name)
major:撤销设备的设备号
name:撤销设备的设备名
struct file_operations 结构体
struct file_operations {
1639 struct module *owner; <---模块指针
1640 loff_t (*llseek) (struct file *, loff_t, int);
1641 ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
1642 ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
1643 ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
1644 ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
1645 int (*readdir) (struct file *, void *, filldir_t);
1646 unsigned int (*poll) (struct file *, struct poll_table_struct *);
1647 long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
1648 long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
1649 int (*mmap) (struct file *, struct vm_area_struct *);
1650 int (*open) (struct inode *, struct file *);
1651 int (*flush) (struct file *, fl_owner_t id);
1652 int (*release) (struct inode *, struct file *);
1653 int (*fsync) (struct file *, loff_t, loff_t, int datasync);
1654 int (*aio_fsync) (struct kiocb *, int datasync);
1655 int (*fasync) (int, struct file *, int);
1656 int (*lock) (struct file *, int, struct file_lock *);
1657 ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
1658 unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
1659 int (*check_flags)(int);
1660 int (*flock) (struct file *, int, struct file_lock *);
1661 ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
1662 ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
1663 int (*setlease)(struct file *, long, struct file_lock **);
1664 long (*fallocate)(struct file *file, int mode, loff_t offset,
1665 loff_t len);
1666 };