Linux驱动程序刚接触,虽然不是很清楚,但是总归是慢慢学习的过程。我的环境是Fedora14虚拟机。内核版本是2.6.38.1,其中的实现过程存在很多的问题,主要是因为很多的内核函数发生了较大的差别.其中最大的可能是ioctl以及互信息量的实现。这两个的问题也使得我们在驱动设计过程中出现很多的疑惑和问题。
接上一部分,继续总结:
主要包括几个重要的结构体、并发控制、以及ioctl的实现。在驱动的设计过程主要涉及3个重要的结构体。struct file_operations,struct inode,struct file.
struct file_operations主要是涉及一些文件操作的函数,其本质上就是一个函数指针的集合,包含了文件操作的各种函数声明,可能与应用程序设计中的相应函数只在参数上存在一定的差别。但是在2.6.36版本以后,其中的内容发生了较大的变化,主要设计了ioctl的相关操作。
- 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 (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
- ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
- int (*readdir) (struct file *, void *, filldir_t);
- unsigned int (*poll) (struct file *, struct poll_table_struct *);
- /*新添加的函数,同时去掉了ioctl的函数,同时返回值也发生了变化*/
- 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 *, 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 **);
- long (*fallocate)(struct file *file, int mode, loff_t offset,
- loff_t len);
- };
long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
是最近添加进来的函数,为了实现原来的ioctl函数,同时参数以及返回值都发生了较大的变化,这也是为什么在2.6.36版本以后的内核中使用ioctl函数会报错的原因。unlocked_ioctl函数通常用来实现原来的ioctl函数,而compat_ioctl函数则用来实现一些兼容版本的ioctl问题。返回值由原来的int变为long型,也是需要注意的。在ioctl中,第一个参数是struct inode,2.6.36以后的版本将不能直接访问到inode参数,只能间接的访问,具体的访问方法后面在总结。
在驱动实现过程中主要包括对各个需要实现函数的赋值,但是open函数不能赋值与否,都会默认打开,如果不赋值,则默认该设备一直打开。其他的函数不赋值,即表示不实现该方法。常用的复制方法如下:
- /*添加该模块的基本文件操作支持*/
- static const struct file_operations mem_fops =
- {
- /*结尾不是分号,注意其中的差别*/
- .owner = THIS_MODULE,
- .llseek = mem_llseek,
- .read = mem_read,
- .write = mem_write,
- .open = mem_open,
- .release = mem_release,
- /*添加新的操作支持*/
- .unlocked_ioctl = mem_ioctl,
- };
需要注意的是后面不再是分号,而是逗号。其中的mem_read、mem_write等是函数的具体实现过程。.owner表示该结构体属于那个,当然就是THIS_MODULE,表示这个模块。
struct inode表示的是一个文件的索引,该结构是每一个具体的物理文件(保存在存储器中的实体文件)的索引,一个文件对应一个唯一的struct inode,其中表明了文件的大小,文件的类型,文件的时间等参数,结构体中每一个参数都能表示某一个文件的特性,通过inode就能表示文件的所有信息。
- struct inode {
- /* RCU path lookup touches following: */
- umode_t i_mode;
- /*使用者的id*/
- uid_t i_uid;
- /*使用者的组的id*/
- gid_t i_gid;
- const struct inode_operations *i_op;
- struct super_block *i_sb;
- spinlock_t i_lock; /* i_blocks, i_bytes, maybe i_size */
- unsigned int i_flags;
- struct mutex i_mutex;
- /*状态标志*/
- unsigned long i_state;
- unsigned long dirtied_when; /* jiffies of first dirtying */
- struct hlist_node i_hash;
- struct list_head i_wb_list; /* backing dev IO list */
- struct list_head i_lru; /* inode LRU list */
- struct list_head i_sb_list;
-
- union {
- struct list_head i_dentry;
- struct rcu_head i_rcu;
- };
- unsigned long i_ino;
-
- /*引用次数,当这个数为0时,release函数才能完成*/
- atomic_t i_count;
- unsigned int i_nlink;
- /*设备文件的设备号*/
- dev_t i_rdev;
- unsigned int i_blkbits;
- u64 i_version;
- /*文件偏移量*/
- loff_t i_size;
- #ifdef __NEED_I_SIZE_ORDERED
- seqcount_t i_size_seqcount;
- #endif
- /*文件的时间参数,包括三种时间*/
- struct timespec i_atime;
- struct timespec i_mtime;
- struct timespec i_ctime;
- blkcnt_t i_blocks;
- unsigned short i_bytes;
- struct rw_semaphore i_alloc_sem;
- const struct file_operations *i_fop; /* former ->i_op->default_file_ops */
- struct file_lock *i_flock;
-
- /*文件的备份地址空间*/
- struct address_space *i_mapping;
- /*设备地址空间*/
- struct address_space i_data;
- #ifdef CONFIG_QUOTA
- struct dquot *i_dquot[MAXQUOTAS];
- #endif
- struct list_head i_devices;
-
- /*说明了三种不同的驱动类型*/
- union {
- struct pipe_inode_info *i_pipe;
- struct block_device *i_bdev;
- struct cdev *i_cdev;
- };
- __u32 i_generation;
- #ifdef CONFIG_FSNOTIFY
- __u32 i_fsnotify_mask; /* all events this inode cares about */
- struct hlist_head i_fsnotify_marks;
- #endif
- #ifdef CONFIG_IMA
- /* protected by i_lock */
- unsigned int i_readcount; /* struct files open RO */
- #endif
- /*写者使用次数*/
- atomic_t i_writecount;
- #ifdef CONFIG_SECURITY
- void *i_security;
- #endif
- #ifdef CONFIG_FS_POSIX_ACL
- struct posix_acl *i_acl;
- struct posix_acl *i_default_acl;
- #endif
- void *i_private; /* fs or device private pointer */
- };
驱动程序设计过程中通常采用i_rdev判断设备文件的设备号。
struct file是指文件对象,表示进程中打开的文件,一个物理文件只有一个inode,但是可以被打开很多次,因此可以存在很多struct file结构体。
- struct file {
- /*
- * fu_list becomes invalid after file_free is called and queued via
- * fu_rcuhead for RCU freeing
- */
- union {
- struct list_head fu_list;
- struct rcu_head fu_rcuhead;
- } f_u;
-
- /*文件的路径*/
- struct path f_path;
- #define f_dentry f_path.dentry
- #define f_vfsmnt f_path.mnt
- /*该文件支持的操作集合*/
- const struct file_operations *f_op;
- spinlock_t f_lock; /* f_ep_links, f_flags, no IRQ */
- #ifdef CONFIG_SMP
- int f_sb_list_cpu;
- #endif
- /*文件对象的使用次数*/
- atomic_long_t f_count;
- unsigned int f_flags;
- fmode_t f_mode;
- loff_t f_pos;
- struct fown_struct f_owner;
- const struct cred *f_cred;
- struct file_ra_state f_ra;
- u64 f_version;
- #ifdef CONFIG_SECURITY
- void *f_security;
- #endif
- /* needed for tty driver, and maybe others */
- /*通常用来指向具体的数据或者设备文件,实现操作*/
- void *private_data;
- #ifdef CONFIG_EPOLL
- /* Used by fs/eventpoll.c to link all the hooks to this file */
- struct list_head f_ep_links;
- #endif /* #ifdef CONFIG_EPOLL */
- struct address_space *f_mapping;
- #ifdef CONFIG_DEBUG_WRITECOUNT
- unsigned long f_mnt_write_state;
- #endif
- };
访问inode主要是通过这两个机构体之间的管理型。
- struct path {
struct vfsmount *mnt;
struct dentry *dentry;
};
- struct dentry {
- /* RCU lookup touched fields */
- unsigned int d_flags; /* protected by d_lock */
- seqcount_t d_seq; /* per dentry seqlock */
- struct hlist_bl_node d_hash; /* lookup hash list */
- struct dentry *d_parent; /* parent directory */
- struct qstr d_name;
- struct inode *d_inode; /* Where the name belongs to - NULL is
- * negative */
- unsigned char d_iname[DNAME_INLINE_LEN]; /* small names */
- /* Ref lookup also touches following */
- unsigned int d_count; /* protected by d_lock */
- spinlock_t d_lock; /* per dentry lock */
- const struct dentry_operations *d_op;
- struct super_block *d_sb; /* The root of the dentry tree */
- unsigned long d_time; /* used by d_revalidate */
- void *d_fsdata; /* fs-specific data */
- struct list_head d_lru; /* LRU list */
- /*
- * d_child and d_rcu can share memory
- */
- union {
- struct list_head d_child; /* child of parent list */
- struct rcu_head d_rcu;
- } d_u;
- struct list_head d_subdirs; /* our children */
- struct list_head d_alias; /* inode alias list */
- };
其中的可以通过struct file间接的访问物理文件的struct inode,具体的实现是filp->f_path.entry->d_inode,这个过程也就实现了将inode和file结构体之间的联系。
上面的几个主要的结构体是驱动实现过程中最重要的几个。具体的意义还要联系起来分析。
阅读(690) | 评论(0) | 转发(0) |