linux device结构体,struct device结构体
一、定义:
linux/include/linux/device.h
struct device {
struct klist klist_children;
struct klist_node knode_parent; /* node in sibling list */
struct klist_node knode_driver;
struct klist_node knode_bus;
struct device *parent;
struct kobject kobj;
char bus_id[BUS_ID_SIZE]; /* position on parent bus */
struct device_type *type;
unsigned is_registered:1;
unsigned uevent_suppress:1;
struct semaphore sem; /* semaphore to synchronize calls to * its driver.*/
struct bus_type * bus; /* type of bus device is on */
struct device_driver *driver; /* which driver has allocated this device */
void *driver_data; /* data private to the driver */
void *platform_data; /* Platform specific data, device core doesn't touch it */
struct dev_pm_info power;
#ifdef CONFIG_NUMA
int numa_node; /* NUMA node this device is close to */
#endif
u64 *dma_mask; /* dma mask (if dma'able device) */
u64 coherent_dma_mask;/* Like dma_mask, but for alloc_coherent mappings as not all
hardware supports 64 bit addresses for consistent allocations such descriptors. */
struct list_head dma_pools; /* dma pools (if dma'ble) */
struct dma_coherent_mem *dma_mem; /* internal for coherent mem override */
/* arch specific additions */
struct dev_archdata archdata;
spinlock_t devres_lock;
struct list_head devres_head; /* class_device migration path */
struct list_head node;
struct class *class;
dev_t devt; /* dev_t, creates the sysfs "dev" */
struct attribute_group **groups; /* optional groups */
void (*release)(struct device * dev);
};
二、作用:
用于描述设备相关的信息设备之间的层次关系,以及设备与总线、驱动的关系。
三、详解:
1、struct klist klist_children;
struct klist被定义在linux/include/linux/klist.h中,原型是:
struct klist {
spinlock_t k_lock;
struct list_head k_list;
void (*get)(struct klist_node *);
void (*put)(struct klist_node *);
};
可见它是对struct list_head的扩展,在此它的作用是连接设备列表中的孩子列表。
2、struct klist_node knode_parent; /* node in sibling list */
struct klist_node被定义在linux/include/linux/klist.h,原型是:
struct klist_node {
struct klist * n_klist;
struct list_head n_node;
struct kref n_ref;
struct completion n_removed;
};
在此它的作用是表示它的兄弟节点。
3、struct klist_node knode_driver;
表示它的驱动节点。
4、struct klist_node knode_bus;
表示总线节点。
5、struct device *parent;
指向其父设备。
6、struct kobject kobj;
这里http://blog.chinaunix.net/u1/55599/showart.php?id=1086478有对kobject的解释,此处
它是内嵌的一个kobject对象。
7、char bus_id[BUS_ID_SIZE];
bus_id表示其在父总线上的位置。BUS_ID_SIZE被定义为:
#define KOBJ_NAME_LEN 20 /*linux/include/linux/kobject.h*/
#define BUS_ID_SIZE KOBJ_NAME_LEN /*linux/include/linux/device.h*/
所以表示位置的字符串长度不能超过20。
8、struct device_type *type;
被定义在/linux/include/linux/device.h中,原型是:
struct device_type {
const char *name;
struct attribute_group **groups;
int (*uevent)(struct device *dev, char **envp, int num_envp,char *buffer, int
buffer_size);
void (*release)(struct device *dev);
int (*suspend)(struct device * dev, pm_message_t state);
int (*resume)(struct device * dev);
};
device_type结构表示设备的类型。一个设备类或者总线可以包含不同类型的设备,例如“分区
”和“磁盘” , “鼠标”和“事件” 。device_type就可以标识某个设备类型和该设备的特有
信息,它就等同于kobject结构中的kobj_type一样。如果name数据成员被指定,那么uevent成员
函数就会把它包含在DEVTYPE变量中。
9、unsigned is_registered:1;
标识该设备是否已经被注册过。is_registered:1这样的形式表示is_registered这个变量只有一
位。在32位linux系统下,unsigned是4字节32位,而经过is_registered:1这样的限制后,变量
is_registered只有一位,其取值只能是1或者0,相当于声明了一个boolean类型的变量。在此种
用法中,后面指定数据宽度的值只能大于0且小于本身的数据宽度。
10、struct bus_type * bus;
指向所连接总线的指针。
11、struct device_driver *driver;
指向被分配到该设备的设备驱动。
12、u64 *dma_mask; /*指向设备DMA屏蔽字。*/
u64 coherent_dma_mask;/*设备一致性DMA的屏蔽字。*/
struct list_head dma_pools; /*聚集的DMA缓冲池。*/
struct dma_coherent_mem *dma_mem; /*指向设备所使用的一致性DMA存储器描述符的指针*/
13、spinlock_t devres_lock;
定义一个设备自旋锁,用于互斥访问设备。关于自旋锁的详细讲解参考:
14、void (*release)(struct device * dev);
释放设备描述符的回调函数。
四、操作:
linux内核系统了一系列完整的对device操作的函数。
1、其中device_register()函数用来将一个新的device对象插入设备模型。它在
linux/drivers/base/core.c中被实现:
int device_register(struct device *dev){ device_initialize(dev);
return device_add(dev);}
该函数首先是调用device_initialize()初始化device结构,具体是初始化嵌入的kobject结构
dev->kobj,初始化列表中的孩子列表kobj->klist_children,初始化DMA缓冲池dev->dma_pools,
初始化自旋锁dev->devres_lock等。接下来device_add()函数才真正将该device对象dev插入设
备模型中。device_add()函数首先是通过kboject_add()函数将它添加到kobject层次,再把它添
加都全局和兄弟链表中,最后添加到其他相关的子系统的驱动程序模型,完成device对象的注册
。
2、device_unregister()完成相反的过程:/linux/drivers/base/core.c
void device_unregister(struct device * dev){
pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id);
device_del(dev);
put_device(dev);
}
它会先以KERN_DEBUG级别打印注销设备的信息,然后才真正删除设备,减少设备对象的引用计数
。
3、get_device()和put_device()分别是增加和减少设备对象的引用计数。这两个函数都定义在
:/linux/drivers/base/core.c中。具体是应用在注册device对象时,device_add()函数会调用
get_device()增加对该device对象的引用计数。在注销设备对象时,device_unregister()函数
直接调用put_device()函数减少对该device对象的引用计数。
linux device结构体,struct device结构体相关推荐
- C/C++结构体struct 与结构体数组和枚举型enum的结合使用
C/C++结构体struct 与结构体数组和枚举型enum的结合使用 #include "stdafx.h" #include <string> #include &l ...
- Linux总线驱动-02: struct bus_type 结构体
http://blog.csdn.net/cppgp/article/details/6333359 本文测试系统为:Ubuntu 10.10 x86_64 2.6.35-24-generic 上节中 ...
- linux sock结构体,struct socket结构体详解
在内核中为什么要有struct socket结构体呢? struct socket结构体的作用是什么? 下面这个图,我觉得可以回答以上两个问题. 由这个图可知,内核中的进程可以通过使用struct ...
- 获取网络接口信息——ioctl()函数与结构体struct ifreq、 struct ifconf
http://blog.csdn.net/windeal3203/article/details/39320605 Linux 下 可以使用ioctl()函数 以及 结构体 struct ifreq ...
- struct timeval结构体 以及 gettimeofday()函数
一.struct timeval结构体 struct timeval结构体在time.h中的定义为: struct timeval { __time_t tv_sec; /* Secon ...
- struct timeval结构体 以及 gettimeofday()函数、struct timespec结构体
struct timeval结构体 struct timeval结构体在time.h中的定义为: struct timeval { __time_t tv_sec; /* Seconds. */ __ ...
- 【零基础学C语言】知识总结八:struct 结构体与 union 共用体
struct 结构体 struct即结构体,C程序中经常需要用相关的不同类型的数据来描述一个数据对象.例如,描述学生的综合信息时,需要使用学生的学号.姓名.性别等不同类型的数据时,像这种数据类型总是在 ...
- c语言结构体加联合,C语言:结构体和联合体(共用体)
结构体:struct 1.结构体变量的首地址能够被其最宽基本类型成员的大小所整除. 2.结构体每个成员相对于结构体首地址的偏移量(offset)都是成员的整数倍. 3.结构体的总大小为结构体最宽基本类 ...
- struct device结构体(2.6.23)
struct device结构体(2.6.23) 一.定义: linux/include/linux/device.h struct device { struct klist ...
最新文章
- 图论-欧拉路(UVA10054)(HDU1116)
- 考前自学系列·计算机组成原理·常见的数据寻址方式(地址码,操作数位置)
- 在命令行下进行Oracle用户解锁
- spring boot actuator服务监控与管理
- tdd干扰波形_LTE常见干扰排查(中国移动)
- [转]JavaScript构造函数及原型对象
- P6348 [PA2011]Journeys 线段树优化建图 区间连区间
- TensorFlow笔记(3) TensorBoard可视化
- Mac os更新系统后安装scrapy报错error: command ‘xcrun‘ failed with exit status 1
- oracle高压水位线,Oracle 高水位线详解(HWM)
- matlab的小波分析,Matlab下小波分析wavelet常用命令
- virtualenv 安装及使用[转]
- 用python分析拼多多_python:拼多多订单接口api
- robocode基本原理之坐标锁定
- 跟着小甲鱼学习C语言
- dreamware html中加入flv,Dreamweaver插入FLV文件技巧
- 使用IDEA生成DOC文档
- onedrive指定文件夹备份
- html各种弹出框和提示框
- 欢迎关注我的公众号《魔笛手CTO》,感谢大家的支持
热门文章
- python3.8.5 应用程序无法启动-macos python3.8.5 打开摄像头问题
- python游戏-练习项目19:使用python制作游戏(上)
- python主要用途-学习Python的三大主要用途
- python自带的shell是什么-python的shell是什么
- python 装饰器 参数-Python装饰器(4)带参数的装饰器
- python批量下载文件-python 从远程批量下载文件到本地
- python课程怎么样-python课程体系是怎么样的?
- python自学免费课堂-python自学——文件打开
- python自带库处理excel-python处理excel之第三方库openpyxl
- python真的好吗-python的缩进格式真的不好吗?