天天看点

linux内核字符设备驱动相关的函数以及结构体

 1. struct cdev {

         struct kobject kobj;

         struct module *owner;

         const struct file_operations *ops;

         struct list_head list;

         dev_t dev;

         unsigned int count;

};//描述字符设备

2. void cdev_init(struct cdev *cdev, const struct file_operations *fops){

         memset(cdev, 0, sizeof *cdev);

         INIT_LIST_HEAD(&cdev->list);

         cdev->kobj.ktype = &ktype_cdev_default;

         kobject_init(&cdev->kobj);

         cdev->ops = fops;

}//用于初始化cdev的成员,并建立cdev和file_operations之间的链接

3. struct cdev *cdev_alloc(void){

         struct cdev *p = kzalloc(sizeof(struct cdev), GFP_KERNEL);

         if (p) {

                   p->kobj.ktype = &ktype_cdev_dynamic;

                   INIT_LIST_HEAD(&p->list);

                   kobject_init(&p->kobj);

         }

         return p;

}//用于动态申请一个cdev内存

4.int cdev_add(struct cdev *p, dev_t dev, unsigned count){

         p->dev = dev;

         p->count = count;

         return kobj_map(cdev_map, dev, count, NULL, exact_match, exact_lock, p);

}//添加(注册)一个cdev

5. void cdev_del(struct cdev *p){

         cdev_unmap(p->dev, p->count);

         kobject_put(&p->kobj);

}//删除(注销)一个cdev

6. MAJOR(dev_t dev)//得到主设备号

7.MINOR(dev_t dev)//得到次设备号

8. MKDEV(int major,int minor)//生成dev_t

9. int register_chrdev_region(dev_t from, unsigned count, const char *name){

         struct char_device_struct *cd;

         dev_t to = from + count;

         dev_t n, next;

         for (n = from; n < to; n = next) {

                   next = MKDEV(MAJOR(n)+1, 0);

                   if (next > to)

                            next = to;

                   cd = __register_chrdev_region(MAJOR(n), MINOR(n),

                                   next - n, name);

                   if (IS_ERR(cd))

                            goto fail;

         }

         return 0;

fail:

         to = n;

         for (n = from; n < to; n = next) {

                   next = MKDEV(MAJOR(n)+1, 0);

                   kfree(__unregister_chrdev_region(MAJOR(n), MINOR(n), next - n));

         }

         return PTR_ERR(cd);

}//向系统申请设备号,已知起始设备的设备号

10.int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count,

                            const char *name){

         struct char_device_struct *cd;

         cd = __register_chrdev_region(0, baseminor, count, name);

         if (IS_ERR(cd))

                   return PTR_ERR(cd);

         *dev = MKDEV(cd->major, cd->baseminor);

         return 0;

}//向系统申请设备号,设备号未知,向系统申请未被暂用的设备号

11.void unregister_chrdev_region(dev_t from, unsigned count){

         dev_t to = from + count;

         dev_t n, next;

         for (n = from; n < to; n = next) {

                   next = MKDEV(MAJOR(n)+1, 0);

                   if (next > to)

                            next = to;

                   kfree(__unregister_chrdev_region(MAJOR(n), MINOR(n), next - n));

         }

}//释放申请的设备号

12. 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 *);

         int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);

         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 *, struct dentry *, 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 (*sendfile) (struct file *, loff_t *, size_t, read_actor_t, void *);

         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 (*dir_notify)(struct file *filp, unsigned long arg);

         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);

};//结构体中的成员函数是字符设备驱动程序设计的主体内容

由于字符设备的上层没有磁盘文件系统,所以字符设备的file_operations成员函数就直接由设备驱动提供了,file_operations正是字符设备驱动的核心。

字符设备驱动模块加载

想要实现一个驱动模块的加载至少应该实现以下几个步骤:

1.       对设备进行初始化

2.       获取设备号

3.       添加(注册)设备

字符设备驱动模块卸载

驱动模块的卸载至少应该实现以下几个步骤:

1.       释放占用的设备号

2.       删除(注销)设备

继续阅读