嵌入式Linux字符设备驱动程序的主要数据结构
1)struct cdev:在内核中代表一个字符设备驱动(char device,cdev),每一个字符设备驱动都有一个struct cdev结构体变量与之对应,记录该设备驱动的相关信息,主要包括设备号dev_t dev和设备操作函数集const struct file_operations *ops。定义如下:
struct cdev { struct kobject kobj; struct module *owner; const struct file_operations *ops; struct list_head list; dev_t dev; unsigned int count;};
2)struct file_operations:定义了设备驱动程序与用来访问操作内核之间的接口,该结构体定义的函数指来访用来问实际的硬件设备,不同的硬件设备具有不同的读写函数。
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 *);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 **);};
3)struct inode:表示一个文件节点,记录一个文件固有的通用的信息,任何一个文件都有这个结构体变量。
struct inode {struct hlist_nodei_hash;struct list_headi_list;/* backing dev IO list */struct list_headi_sb_list;struct list_headi_dentry;unsigned longi_ino;atomic_ti_count;unsigned inti_nlink;uid_ti_uid;gid_ti_gid;dev_ti_rdev;unsigned inti_blkbits;u64i_version;loff_ti_size;#ifdef __NEED_I_SIZE_ORDEREDseqcount_ti_size_seqcount;#endifstruct timespeci_atime;struct timespeci_mtime;struct timespeci_ctime;blkcnt_ti_blocks;unsigned short i_bytes;umode_ti_mode;spinlock_ti_lock;/* i_blocks, i_bytes, maybe i_size */struct mutexi_mutex;struct rw_semaphorei_alloc_sem;const struct inode_operations*i_op;const struct file_operations*i_fop;/* former ->i_op->default_file_ops */struct super_block*i_sb;struct file_lock*i_flock;struct address_space*i_mapping;struct address_spacei_data;#ifdef CONFIG_QUOTAstruct dquot*i_dquot[MAXQUOTAS];#endifstruct list_headi_devices;union {struct pipe_inode_info*i_pipe;struct block_device*i_bdev;struct cdev*i_cdev;};
4)struct file:表示一个已经打开的文件,记录打开文件的属性,包括读写属性,linux中一切皆文件,但是文件一般分为磁盘文件和特殊文件,而这个特殊文件就是设备文件,其中所有的外围硬件设备都当作设备文件进行处理。
struct file {/* * fu_list becomes invalid after file_free is called and queued via * fu_rcuhead for RCU freeing */union {struct list_headfu_list;struct rcu_head fu_rcuhead;} f_u;struct pathf_path;#define f_dentryf_path.dentry#define f_vfsmntf_path.mntconst struct file_operations*f_op;spinlock_tf_lock; /* f_ep_links, f_flags, no IRQ */#ifdef CONFIG_SMPintf_sb_list_cpu;#endifatomic_long_tf_count;unsigned int f_flags;fmode_tf_mode;loff_tf_pos;struct fown_structf_owner;const struct cred*f_cred;struct file_ra_statef_ra;u64f_version;#ifdef CONFIG_SECURITYvoid*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_headf_ep_links;#endif /* #ifdef CONFIG_EPOLL */struct address_space*f_mapping;#ifdef CONFIG_DEBUG_WRITECOUNTunsigned long f_mnt_write_state;#endif};