字符设备开发你需要知道的

字符设备开发你需要知道的

cdev结构体

cdev结构体的作用就是用来描述一个字符设备的,我们来看看具体代码:

 struct cdev {
     struct kobject kobj;                    // 内嵌的kobject对象 
     struct module *owner;                   // 所属模块
     const struct file_operations *ops;      // 文件操作结构体
     struct list_head list;                  //linux内核所维护的链表指针
     dev_t dev;                      //设备号,31位,高12位主设备,低20是次设备
     unsigned int count;                     //设备数目
 };

一些宏和file_operations

老规矩,直接上代码,首先是宏的:

#define MINORBITS     20
#define MINORMASK     ((1U << MINORBITS) - 1) 
#define MAJOR(dev)    ((unsigned int) ((dev) >> MINORBITS))  //获得主设备号
#define MINOR(dev)    ((unsigned int) ((dev) & MINORMASK))   //获得此设备号
#define MKDEV(ma,mi)  (((ma) << MINORBITS) | (mi)) //由主次设备号得到设备号

然后是初始化函数cdev_init(),主要功能是初始化cdev成员,建立cdev和file_operations之间的连接,代码如下:

void cdev_init(struct cdev *cdev, const struct file_operations *fops)
{
    memset(cdev, 0, sizeof *cdev);
    INIT_LIST_HEAD(&cdev->list);
    kobject_init(&cdev->kobj, &ktype_cdev_default);
    cdev->ops = fops;
}

cdev_alloc()函数用于动态的申请一个cdev内存

struct cdev *cdev_alloc(void)
{
    struct cdev *p = kzalloc(sizeof(struct cdev), GFP_KERNEL);
    if (p) {
        INIT_LIST_HEAD(&p->list);
        kobject_init(&p->kobj, &ktype_cdev_dynamic);
    }
    return p;
}

分配和释放设备号函数分别是

int register_chrdev_region(dev_t from, unsigned count, const char *name)
//手动分配
int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count,
const char *name)
//自动分配

void unregister_chrdev_region(dev_t from, unsigned count) 
//注销

到这里基本的知识点就结束了,but,咱们再浅浅聊一下file_operations这个结构体吧

file_operations结构体中是字符设备驱动程序开发的主题内容,简而言之就是为Linux的应用层进行的open(),write(),read()等的底层实现,体积有点大,咱们就一起看
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 *, 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 *, 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);
	int (*show_fdinfo)(struct seq_file *m, struct file *f);
};

在下章节我会和大家一起试着在ubuntu里完成注册

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值