一步步写驱动---cdev
来源:互联网 发布:中小型企业erp软件 编辑:程序博客网 时间:2024/06/12 19:39
前面写到如何向系统申请一个设备号,设备号就像我们的身份证号一样,号本身并没有什么特殊的意义,只有把这个号和人对应才有意义,通用设备号也需要和一个特殊的东西对于,这就是cdev, cdev是linux下抽象出来的一个用来描述一个字符设备的结构体,在linux下定义如下:
struct cdev { struct kobject kobj; struct module *owner; const struct file_operations *ops; struct list_head list; dev_t dev; unsigned int count;};结构体中有几个成员事我们写驱动的时候必须关心的:
dev 类型是dev_t,也就是我们的设备号
ops是一个同样也是一个结构体并且是一个字符驱动实现的主体,字符驱动通常需要和应用程序交互,在学linux系统编程的时候,都会讲到linux 应用程序通过系统调用陷入到内核空间,从而执行内核代码,而驱动作为内核的一部分同样也是需要在内核空间执行的,ops也就是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 *, structpoll_table_struct *); long (*unlocked_ioctl) (struct file *, unsigned int,unsigned long); long (*compat_ioctl) (struct file *, unsigned int, unsignedlong); 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 *, loff_t, loff_t, 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 *, unsignedlong, 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 *, structfile *, loff_t *, size_t, unsigned int); ssize_t (*splice_read)(struct file *, loff_t *, structpipe_inode_info *, size_t, unsigned int); int (*setlease)(struct file *, long, struct file_lock **); long (*fallocate)(struct file *file, int mode, loff_toffset, loff_t len);};
我们在驱动中要做的事情就是申请一个cdev并把cdev注册到系统中去,操作cdev的函数有:
void cdev_init(structcdev *, const struct file_operations *);struct cdev*cdev_alloc(void);int cdev_add(struct cdev*, dev_t, unsigned);void cdev_del(struct cdev*);
1、 cdev的定义
cdev的定义有两种方式一种是:
struct cdev cdev;
另外一种是:
strcut cdev cdev;cdev = cdev_alloc();2、 cdev的初始化
cdev_init 实现cdev的初始化,主要的工作是将我们定义好的file_operaionts与cdev关联起来,file_operations的实现根据实际需求来实现,后面详细介绍。
3、 cdev的注册
cdev_add实现cdev的注册,linux内核里维护了一个cdev_map的表,所谓cdev的注册就是把我们的cdev注册到cdev_map表上,cdev_map表结构如图:
4、 设备的删除
cdev_del 将我们的cdev从cdev_map中移除。
- 一步步写驱动---cdev
- 一步步写驱动--模块
- 一步步写驱动--module_init/module_exit
- 一步步写驱动--设备号
- 一步步写网卡驱动(-)
- 一步步写网卡驱动(二)
- 一步步写网卡驱动(三)
- 驱动之cdev
- 驱动之cdev
- cdev方式注册驱动
- Linux设备驱动----之一 cdev
- Linux设备驱动----之一 cdev
- Linux 设备驱动--- struct cdev
- linux字符设备驱动 cdev
- cdev设备驱动的使用
- 一步步写 CMOS 驱动模块 <ELDD 学习笔记>
- 设备驱动-----字符驱动globalmem(cdev)
- cdev
- 编译错误syntax error : missing ';' before 'type'原因探寻(VS 编译C文件)
- Android动画效果translate、scale、alpha、rotate详解
- 程序员只能吃青春饭?接下来只有一条出路?
- 事务小结
- BNU 0817 B. Alice and Bob
- 一步步写驱动---cdev
- Xcode改成不用MainWindow.xib 和 RootViewController.xib 的方法
- RHEL6.3 安装PPTP VPN Client
- 编码20年的老程序员分享所积累的20条编程经验
- 代码托管
- openvpn 在centos5.5下的配置
- Extjs EditorGridPanel 动态追加一行
- vb中的过程与函数
- python multiprocessing