




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Linux驱动开发庖丁解牛之三揭开字符设备驱动程序的面纱By:dreamice 2008-11-231写在前面的话 我们知道,在Linux设备驱动开发中,包括三大设备类:字符设备,块设备和网络设备。而字符设备,作为最简单的设备类,为此,我们将从最简单的字符设备开始,走进Linux驱动程序设计的神秘殿堂。我们已经踏上了真正的设备驱动开发的道路了!有志者,事竟成。付出越多,而上苍定会以同等的收获回馈于你,当然,最重要的一点是:我们必须走上正确的道路,做正确的付出。开始吧参考书目:Linux Device Driver第三版Understandin
2、g the linux kernel第三版Linux设备驱动开发详解2必备之“砖” 盖大楼,得预先准备好砖头。同样的道理,要写好驱动程序,我们也必须准备好自己的“砖头”,拿好这些砖头,便会真正如庖丁解牛般,游刃于Linux驱动程序设计的神奇艺术之中。在Linux的设计之初,曾提出:一切皆文件,如果一个东西不是文件,那就是进程。由此可见,文件的概念在Linux系统中可谓是根深蒂固,以至于它深入到对驱动程序的控制,这也是情理之中的事。下图描述了Linux系统中虚拟文件系统和进程之间的关系:图表 1进程和文件系统的关系在上图中
3、,我们看到了Process,File object,dentry object,inode object以及Sperblock object等概念。Process就是指一个特定的进程,而File obeject对应于进程打开的一个文件;dentry object描述了一个目录项;inode object则对应于磁盘上一个特定的文件;Sperblock object描述了文件系统的相关信息。从这个图中,可以看到进程到磁盘上一个文件实体的路径及对应关系。下面,我们一次看看这些实体结构在内核中的定义。2.1 File object File结构代表
4、一个打开的文件,系统中每个打开的文件,在内核空间都对应一个file结构。它由内核在调用open时创建,并传递给在该文件上操作的所有函数,直到最后的close函数。在文件的所有实例都被关闭以后,内核才会释放这个结构。在内核中,通常以filp来代表指向file结构的指针。File结构的详细定义如下:/linux/fs.h779 struct file 780 /*781 * fu_list becomes invalid after file_f
5、ree is called and queued via782 * fu_rcuhead for RCU freeing783 */784 union 785 struct list_head
6、; fu_list;786 struct rcu_head fu_rcuhead;787 f_u;788 struct path &
7、#160; f_path;789 #define f_dentry f_path.dentry790 #define f_vfsmnt f_path.mnt791 const struct file_operations *f_op; /与文件操作相关的函数指针结构792
8、 atomic_t f_count;793 unsigned int f_flags;794 mode_t
9、0; f_mode;795 loff_t f_pos;796 struct fown_struct f_owner;797
10、60; unsigned int f_uid, f_gid;798 struct file_ra_state f_ra;799800 u64
11、 f_version;801 #ifdef CONFIG_SECURITY802 void *f_security;803 #endif804 /* needed for tty driver, and maybe others *
12、/805 void *private_data;806807 #ifdef CONFIG_EPOLL808 /* Used by fs/eventpoll.c to link all the hooks to this file */809
13、160; struct list_head f_ep_links;810 spinlock_t f_ep_lock;811 #endif /* #ifdef CONFIG_EPOLL */812 struct
14、address_space *f_mapping;813 ;1166 /*1167 * NOTE:1168 * read, write, poll, fsync, readv, writev, unlocked_ioctl and compat_ioctl1169 * can be called without the big kernel lock held in all filesystems.1170 */1171 struct file_operations 1172
15、60; struct module *owner;1173 loff_t (*llseek) (struct file *, loff_t, int);1174 ssize_t (*read) (struct file *, char _user *, size_t, loff_t *);1175 ssiz
16、e_t (*write) (struct file *, const char _user *, size_t, loff_t *);1176 ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t);1177 ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsig
17、ned long, loff_t);1178 int (*readdir) (struct file *, void *, filldir_t);1179 unsigned int (*poll) (struct file *, struct poll_table_struct *);1180 int (*ioctl) (struct inode *, struct f
18、ile *, unsigned int, unsigned long);1181 long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);1182 long (*compat_ioctl) (struct file *, unsigned int, unsigned long);1183 i
19、nt (*mmap) (struct file *, struct vm_area_struct *);1184 int (*open) (struct inode *, struct file *);1185 int (*flush) (struct file *, fl_owner_t id);1186 int (*release) (struct inode *,
20、 struct file *);1187 int (*fsync) (struct file *, struct dentry *, int datasync);1188 int (*aio_fsync) (struct kiocb *, int datasync);1189 int (*fasync) (int, struct file *, int);1190
21、60; int (*lock) (struct file *, int, struct file_lock *);1191 ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);1192 unsigned long (*get_unmapped_area)(struct file
22、 *, unsigned long, unsigned long, unsigned long, unsigned long);1193 int (*check_flags)(int);1194 int (*dir_notify)(struct file *filp, unsigned long arg);1195 int (*flock) (struct file *
23、, int, struct file_lock *);1196 ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);1197 ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned
24、 int);1198 int (*setlease)(struct file *, long, struct file_lock *);1199 ;其中,蓝色字体标出部分,为与驱动程序最为密切的部分。由于很多书中都对这些结构体做了详细的阐述,这里就不再赘述了。2.2 inode object 内核用inode结构在内部表示文件,它和file结构的不同之处在于:file表示打开的文件描述符,对单个文件,可能有多个表示打开的文件描述符的file结构,
25、但他们都指向同一个inode结构。Inode结构的详细定义如下:593 struct inode 594 struct hlist_node i_hash;595 struct list_head i_list;596 struct list_h
26、ead i_sb_list;597 struct list_head i_dentry;598 unsigned long i_ino;599
27、60; atomic_t i_count;600 unsigned int i_nlink;601 uid_t &
28、#160; i_uid;602 gid_t i_gid;603 dev_t i_rd
29、ev;604 u64 i_version;605 loff_t i_size;606 #ifdef _NEED_I
30、_SIZE_ORDERED607 seqcount_t i_size_seqcount;608 #endif609 struct timespec i_atime;610
31、;struct timespec i_mtime;611 struct timespec i_ctime;612 unsigned int i_blkbits;613 &
32、#160; blkcnt_t i_blocks;614 unsigned short i_bytes;615 umode_t
33、 i_mode;616 spinlock_t i_lock; /* i_blocks, i_bytes, maybe i_size */617 struct mutex &
34、#160; i_mutex;618 struct rw_semaphore i_alloc_sem;619 const struct inode_operations *i_op;/inode操作函数集合620 const struct file_operations
35、60; *i_fop; /* former ->i_op->default_file_ops */621 struct super_block *i_sb;622 struct file_lock *i_flock;623 struct
36、 address_space *i_mapping;624 struct address_space i_data;625 #ifdef CONFIG_QUOTA626 struct dquot *i_dquotMAXQUOTAS;627 #endif628
37、; struct list_head i_devices;629 union 630 struct pipe_inode_info *i_pipe;631
38、60; struct block_device *i_bdev;632 struct cdev *i_cdev;633 634
39、60; int i_cindex;635636 _u32 i_generation;637638 #ifdef CONFIG_DNOTIFY639
40、160; unsigned long i_dnotify_mask; /* Directory notify events */640 struct dnotify_struct *i_dnotify; /* for directory notifications */641 #endif642643 #ifdef CONFIG_IN
41、OTIFY644 struct list_head inotify_watches; /* watches on this inode */645 struct mutex inotify_mutex; /* protects the watches
42、 list */646 #endif647648 unsigned long i_state;649 unsigned long dirtied_when; /* jiffies of first dirtying */65
43、0651 unsigned int i_flags;652653 atomic_t i_writecount;654 #ifdef CONFIG_SECURITY655
44、0; void *i_security;656 #endif657 void *i_private; /* fs or device priva
45、te pointer */658 ;2.3 Super block object Super block object对应于一个特定的文件系统,通常对应于存放在磁盘扇区中的文件系统超级块或文件系统控制块,而对于非基于文件系统的文件,他们会在使用现场创建超级块,并将其保存到内存中。一下是结构体的详细描述:981 struct super_block 982 struct list_head
46、0; s_list; /* Keep this first */983 dev_t s_dev; /* search index; _not_ kdev_t */984
47、; unsigned long s_blocksize;985 unsigned char s_blocksize_bits;986 unsigned char
48、; s_dirt;987 unsigned long long s_maxbytes; /* Max file size */988 struct file_system_type *s_type;989 const stru
49、ct super_operations *s_op;990 struct dquot_operations *dq_op;991 struct quotactl_ops *s_qcop;992 const struct export_operations *s_export_op;993
50、 unsigned long s_flags;994 unsigned long s_magic;995 struct dentry
51、 *s_root;996 struct rw_semaphore s_umount;997 struct mutex s_lock;998 int &
52、#160; s_count;999 int s_syncing;1000 int
53、60; s_need_sync_fs;1001 atomic_t s_active;1002 #ifdef CONFIG_SECURITY1003 void
54、0; *s_security;1004 #endif1005 struct xattr_handler *s_xattr;10061007 struct list_head s_inodes; &
55、#160; /* all inodes */1008 struct list_head s_dirty; /* dirty inodes */1009 struct list_head s_io;
56、160; /* parked for writeback */1010 struct list_head s_more_io; /* parked for more writeback */1011 struct hlist_head&
57、#160; s_anon; /* anonymous dentries for (nfs) exporting */1012 struct list_head s_files;10131014 struct block_device
58、 *s_bdev;1015 struct mtd_info *s_mtd;1016 struct list_head s_instances;1017 struct quota_info
59、 s_dquot; /* Diskquota specific options */10181019 int s_frozen;1020 wait_queu
60、e_head_t s_wait_unfrozen;10211022 char s_id32; /* Informational name */10231024 voi
61、d *s_fs_info; /* Filesystem private info */10251026 /*1027 * The next field is for VFS *only*. No filesystems
62、have any business1028 * even looking at it. You had been warned.1029 */1030 struct mutex s_vfs_rename_mutex; /* Kludge */10311032
63、; /* Granularity of c/m/atime in ns.1033 Cannot be worse than a second */1034 u32 s_time_gran;10351036
64、; /*1037 * Filesystem subtype. If non-empty the filesystem type field1038 * in /proc/mounts will be 'type.subtype'2.4 Identry object Linux中把目录也当作文件,为了方便查找操作,虚拟文件系统(VFS)引入了目录项的概念。每个dentry代表路径中一个特定部分。由于驱动程序很少涉及到dentry,所以在这里就不做描述了。3. 实例剖析Linux字符设备驱动程序 Linux的变化真的是太快了。当我们还在研读最新版的LDD3(基于内核)时,而实际上,它的驱动程序框架结构已经发生了很大的变化。当我还在投入的学习scull示例驱动程序的时候,我发现,对于编写一个字符设备驱动程序,已经有了新的变化。原本打算
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 学生能力培养策略计划
- 体育锻炼与健康促进方案计划
- 2025年腊八节幼儿园活动标准教案
- 胸腔积液的护理问题与护理措施
- 仓库服务创新的实践探索计划
- 创意写作社团创作训练计划
- 员工招聘管理专题培训
- 营销渠道策略(二)
- 胸部止血包扎护理
- 《3 劳动创造幸福》(教学设计)-2023-2024学年三年级下册综合实践活动皖教版
- 《走进汽车》课件
- 中国充电桩行业运营趋势及投资价值评估研究报告
- 2025年小红书品牌博主合作合同
- 2025年华能铜川照金煤电有限公司招聘笔试参考题库含答案解析
- 2025年危化企业安全教育培训计划
- 《HR的成长之路》课件
- 2025年山东浪潮集团有限公司招聘笔试参考题库含答案解析
- U8UAP开发手册资料
- GB 17681-2024危险化学品重大危险源安全监控技术规范
- 2018NFPA10便携式灭火器标准
- 桥梁桩基工程培训课件
评论
0/150
提交评论