博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
cdev简单解析
阅读量:6154 次
发布时间:2019-06-21

本文共 2790 字,大约阅读时间需要 9 分钟。

1. cdev是linux用来管理字符设备的结构体,其在内核中采用数组结构设计,这样系统中有多少个主设备号就约定了数组大小,此设备号采用链表管理,同一主设备号下可以有多个子设备。设备即文件,上层应用要访问设备,必须通过文件,cdev中包含file_operations结构体,该结构体就是驱动的文件操作集合。

(根据于说得)
2. cdev定义在include/linux/cdev.h中,如下。具体实现在fs/char_dev.c中。

#include 
#include
#include
struct file_operations;struct inode;struct module;struct cdev {struct kobject kobj;struct module *owner;const struct file_operations *ops;struct list_head list;dev_t dev;unsigned int count;};void cdev_init(struct cdev *, const struct file_operations *); struct cdev *cdev_alloc(void);void cdev_put(struct cdev *p);int cdev_add(struct cdev *, dev_t, unsigned);void cdev_del(struct cdev *);void cd_forget(struct inode *);extern struct backing_dev_info directly_mappable_cdev_bdi;
//fs/char_dev.c/*** cdev_add() - add a char device to the system* @p: the cdev structure for the device* @dev: the first device number for which this device is responsible* @count: the number of consecutive minor numbers corresponding to this * device** cdev_add() adds the device represented by @p to the system, making it* live immediately. A negative error code is returned on failure. */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);}

3. cdev函数

>>cdev定义有两种方式,struct cdev cdev;另外一种struct cdev *cdev; cdev=cdev_alloc();

一种静态声明定义,另一种动态分配。
>>cdev通过函数cdev_init()初始化,主要工作就是将file_operations和cdev关联起来。file_operations是字符驱动需要实现的主要内容。
>>cdev通过cdev_add()实现cdev的注册,所谓注册就是将cdev根据设备号(dev_t)添加到cdev数组(cdev_map)中供系统管理。
>>cdev通过cdev_del()将cdev从cdev_map中移除。
4. 设备号
申请设备号用如下两个函数:
alloc_chrdev_region() --自动分配设备号
register_chrdev_region() --分配已设定的设备号

/*** register_chrdev_region() - register a range of device numbers* @from: the first in the desired range of device numbers; must include* the major number.* @count: the number of consecutive device numbers required* @name: the name of the device or driver.** Return value is zero on success, a negative error code on failure.*/int register_chrdev_region(dev_t from, unsigned count, const char *name);/*** alloc_chrdev_region() - register a range of char device numbers* @dev: output parameter for first assigned number* @baseminor: first of the requested range of minor numbers* @count: the number of minor numbers required* @name: the name of the associated device or driver** Allocates a range of char device numbers. The major number will be* chosen dynamically, and returned (along with the first minor number)* in @dev. Returns zero or a negative error code.*/int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count,const char *name);

5. more

参考:

http://www.embedu.org/Column/Column552.htm
http://blog.chinaunix.net/uid-24517893-id-161446.html

转载地址:http://eqffa.baihongyu.com/

你可能感兴趣的文章
AndroidStudio中导入SlidingMenu报错解决方案
查看>>
修改GRUB2背景图片
查看>>
Ajax异步
查看>>
好记性不如烂笔杆-android学习笔记<十六> switcher和gallery
查看>>
JAVA GC
查看>>
3springboot:springboot配置文件(外部配置加载顺序、自动配置原理,@Conditional)
查看>>
前端第七天
查看>>
图解SSH原理及两种登录方法
查看>>
【总结整理】JQuery基础学习---样式篇
查看>>
查询个人站点的文章、分类和标签查询
查看>>
基础知识:数字、字符串、列表 的类型及内置方法
查看>>
JSP的隐式对象
查看>>
JS图片跟着鼠标跑效果
查看>>
[SCOI2005][BZOJ 1084]最大子矩阵
查看>>
学习笔记之Data Visualization
查看>>
Leetcode 3. Longest Substring Without Repeating Characters
查看>>
416. Partition Equal Subset Sum
查看>>
app内部H5测试点总结
查看>>
[TC13761]Mutalisk
查看>>
while()
查看>>