设备模型的基础---kobject,kset
设备模型的基础是kobject,kset,kobj_type。
kobject本身并没有什么意义,真正有用的地方在于嵌入了kobject的结构体(对象),kobject可以看成是一个最小单元,sysfs的层次结构中的元素都是由kobject构成。
kset与kobject的关系:
kset会包含一类的kobject对象(内核链表串起来),而这些对象对应在sysfs中就是同一级的各个子目录,每一个kobject对象有自己的属性,这些属性就对应于各自子目录的文件。
如此,就会构成一个有层次的组织结构。
kset本身也包含一个kobject,但这个kobject与kset内核链表list中的kobjects并没有什么关系。
kobject的两个主要作用:
1.引用计数
2.事件上报(kobject不加入某个kset是不会上报uevent的 http://www.cnblogs.com/black-mamba/p/5055683.html)
相关文档:
linux/Documentation/kobject.txt
Documentation/filesystems/sysfs.txt
Documentation/filesystems/sysfs-pci.txt
linux/include/linux/kobject.h
struct kset {
struct list_head list;//所有属于当前集合的kobject对象的链表
spinlock_t list_lock;
struct kobject kobj; //与集合中包含的各个kobject无关,只是用来管理kset对象本身
const struct kset_uevent_ops *uevent_ops;//用于将集合的状态信息传递给用户层。该机制有驱动程序的核心使用。
}; //描述内核对象的共同特性
struct kobj_type {
void (*release)(struct kobject *kobj);
const struct sysfs_ops *sysfs_ops;
struct attribute **default_attrs;
const struct kobj_ns_type_operations *(*child_ns_type)(struct kobject *kobj);
const void *(*namespace)(struct kobject *kobj);
}; //kobject直接嵌入到其他数据结构中,通过管理kobject即可以对包含kobject的对象进行管理。
//kobject包含在一个层次化的组织中。
struct kobject {
const char *name;//对象名称
struct list_head entry;
struct kobject *parent; //决定kobject出现在sysfs层次结构中的位置:如果存在父对象,那么需要在父对象对应的目录中新建一项;否则,将其放置到kobject所在的kset所属的kobject对应的目录中
struct kset *kset;//集合指针
struct kobj_type *ktype;
struct sysfs_dirent *sd;
struct kref kref; //引用计数
unsigned int state_initialized:;
unsigned int state_in_sysfs:;
unsigned int state_add_uevent_sent:;
unsigned int state_remove_uevent_sent:;
unsigned int uevent_suppress:;
}; struct kobj_attribute {
struct attribute attr;
ssize_t (*show)(struct kobject *kobj, struct kobj_attribute *attr,//用户空间读属性时,内核会调用该函数
char *buf);
ssize_t (*store)(struct kobject *kobj, struct kobj_attribute *attr,//用户空间写属性时,内核会调用该函数
const char *buf, size_t count);
}; /disk/A9/linux-3.5/include/linux/sysfs.h
struct attribute {
const char *name;//显示的文件名
umode_t mode;//文件的权限
#ifdef CONFIG_DEBUG_LOCK_ALLOC
bool ignore_lockdep:;
struct lock_class_key *key;
struct lock_class_key skey;
#endif
}; #define __ATTR(_name,_mode,_show,_store) { \
.attr = {.name = __stringify(_name), .mode = _mode }, \
.show = _show, \
.store = _store, \
} struct attribute_group {
const char *name;
umode_t (*is_visible)(struct kobject *,
struct attribute *, int);
struct attribute **attrs;
}; struct kobject *kobject_get(struct kobject *kobj) //引用+1
void kobject_put(struct kobject *kobj)//引用-1 void kobject_init(struct kobject *kobj, struct kobj_type *ktype)
初始化kobeject结构体 struct kobject *kobject_create(void)//非导出函数
创建一个kobject结构体,并用kobject_init初始化 int kobject_add(struct kobject *kobj, struct kobject *parent,
const char *fmt, ...)
注册一个kobject,在sysfs中生成相应的目录。
如果parent为空,则kobject的parent指向其kset的kobject;如果kset也没有分配,则kobject位于/sys下 struct kobject *kobject_create_and_add(const char *name, struct kobject *parent)
创建一个kobject并注册到sysfs,失败返回空。 int kobject_init_and_add(struct kobject *kobj, struct kobj_type *ktype,
struct kobject *parent, const char *fmt, ...)
初始化一个kobject并注册到sysfs,属性文件会自动生成。
Q1:用户空间调用什么接口是与show、store是相对应的?
linux/include/linux/kobject.h
/**
* struct kset - a set of kobjects of a specific type, belonging to a specific subsystem.
*
* A kset defines a group of kobjects. They can be individually
* different "types" but overall these kobjects all want to be grouped
* together and operated on in the same manner. ksets are used to
* define the attribute callbacks and other common events that happen to
* a kobject.
*
* @list: the list of all kobjects for this kset
* @list_lock: a lock for iterating over the kobjects
* @kobj: the embedded kobject for this kset (recursion, isn't it fun...)
* @uevent_ops: the set of uevent operations for this kset. These are
* called whenever a kobject has something happen to it so that the kset
* can add new environment variables, or filter out the uevents if so
* desired.
*/
struct kset {
struct list_head list;//所有属于当前集合的kobject对象的链表
spinlock_t list_lock;
struct kobject kobj; //与集合中包含的各个kobject无关,只是用来管理kset对象本身
const struct kset_uevent_ops *uevent_ops;//用于将集合的状态信息传递给用户层。该机制由驱动程序的核心使用。
}; /**
* kset_create - create a struct kset dynamically
*
* @name: the name for the kset
* @uevent_ops: a struct kset_uevent_ops for the kset
* @parent_kobj: the parent kobject of this kset, if any.
*
* This function creates a kset structure dynamically. This structure can
* then be registered with the system and show up in sysfs with a call to
* kset_register(). When you are finished with this structure, if
* kset_register() has been called, call kset_unregister() and the
* structure will be dynamically freed when it is no longer being used.
*
* If the kset was not able to be created, NULL will be returned.
*/
//非导出函数
static struct kset *kset_create(const char *name,
const struct kset_uevent_ops *uevent_ops,
struct kobject *parent_kobj)
/**
* kset_init - initialize a kset for use
* @k: kset
*/
void kset_init(struct kset *k) /**
* kset_register - initialize and add a kset.
* @k: kset.
*/
int kset_register(struct kset *k) /**
* kset_unregister - remove a kset.
* @k: kset.
*/
void kset_unregister(struct kset *k) /**
* kobject_uevent - notify userspace by sending an uevent
*
* @action: action that is happening
* @kobj: struct kobject that the action is happening to
*
* Returns 0 if kobject_uevent() is completed with success or the
* corresponding error when it fails.
*/
int kobject_uevent(struct kobject *kobj, enum kobject_action action) /**
* kset_create_and_add - create a struct kset dynamically and add it to sysfs
*
* @name: the name for the kset
* @uevent_ops: a struct kset_uevent_ops for the kset
* @parent_kobj: the parent kobject of this kset, if any.
*
* This function creates a kset structure dynamically and registers it
* with sysfs. When you are finished with this structure, call
* kset_unregister() and the structure will be dynamically freed when it
* is no longer being used.
*
* If the kset was not able to be created, NULL will be returned.
*/
struct kset *kset_create_and_add(const char *name,
const struct kset_uevent_ops *uevent_ops,
struct kobject *parent_kobj)
设备模型的基础---kobject,kset的更多相关文章
- Linux 设备模型之 (kobject、kset 和 Subsystem)(二)
问题描写叙述:前文我们知道了/sys是包括内核和驱动的实施信息的,用户能够通过 /sys 这个接口.用户通过这个接口能够一览内核设备的全貌.本文将从Linux内核的角度来看一看这个设备模型是怎样构建的 ...
- Linux设备模型:基础篇
linux提供了新的设备模型:总线(bus).设备(device).驱动(driver).其中总线是处理器与设备之间通道,在设备模型中,所有的设备都通过总线相连:设备是对于一个设备的详细信息描述,驱动 ...
- 设备模型之kobject,kset及其关系
Linux2.6以后的设备驱动,都是在设备模型的基础上构建的,因此,要编写linux下的设备驱动程序,不论是usb设备,pci设备等,都需要了解设备模型. 设备模型的基础结构体主要是kobject,k ...
- 【原创】linux设备模型之kset/kobj/ktype分析
背 景 Read the fucking source code! --By 鲁迅 A picture is worth a thousand words. --By 高尔基 说明: Kernel版本 ...
- linux设备模型_转
建议原博文查看,效果更佳. 转自:http://www.cnblogs.com/wwang/category/269350.html Linux设备模型 (1) 随着计算机的周边外设越来越丰富,设备管 ...
- Linux设备模型 (2)
上一篇文章<Linux设备模型 (1)>主要介绍了Linux设备模型在用户空间的接口sysfs,用户通过这个接口可以一览内核设备的全貌.本文将从Linux内核的角度来看一看这个设备模型是如 ...
- Linux设备模型之kobject
阿辉原创,转载请注明出处 参考文档:LDD3-ch14.内核文档Documentation/kobject.txt,本文中使用到的代码均摘自Linux-3.4.75 ----------------- ...
- 内核设备模型从kobject到子系统
内核设备模型 目的:表示设备和设备在系统中的拓扑关系 优点:1减少内核代码量,2可以统一查看所有设备状态和所连接的总线,3可以 ...
- Linux设备模型分析之kset(基于3.10.1内核)
作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz 内核版本:3.10.1 一.kset结构定义 kset结构体定义在include/linux/kobject.h ...
随机推荐
- [Codeforces 30D] Kings Problem
Brief Intro: 有n+1个点,其中n个点在X轴上,求从第k个点出发最短的汉密尔顿路径 Solution: 分类讨论+逐个枚举 设dist(i)是第i个点到n+1的距离 cal1(l,r)是n ...
- [CF986E]Prince's Problem
题意:给一棵带点权$w_i$的树,多次询问$(u,v,x)$,求出$\prod\limits_{i\in\text{path}(u,v)}(w_i,x)$ 因为是乘法,所以可以把路径询问拆成到根询问, ...
- 1.1(java学习笔记) 面向过程与面向对象
面向过程思考时,我们会先思考具体的步骤,第一步走什么,第二步做什么. 比如电脑新建文件夹,第一步:打开电脑 第二步:按下鼠标右键. 第三步:找到新建选项 第四步:点击新建选项下的文件夹 c语言是典型的 ...
- js 鼠标左键拖动滚动
鼠标左键拖动滚动 原作者: http://blog.csdn.net/lisatisfy/article/details/6606026 本文在源代码的基础上 增加支持水平滚动 的功能 html &l ...
- Kingdee Apusic 中间件有关资料
Kingdee Apusic 中间件有关资料: 1.官方网站:http://www.apusic.com 2.资料目录:http://www.apusic.com/dist 3.Apusic 8 资料 ...
- springBoot框架不同环境读取不同的配置文件
1. 3个配置文件(更多环境可以建多个): application.properties (公共配置文件) application-dev.properties (开发环境) applicatio ...
- ibatis中使用List作为传入参数的使用方法及 CDATA使用
ibatis中list做回参很简单,resultClass设为list中元素类型,dao层调用: (List)getSqlMapClientTemplate().queryForList(" ...
- LVS负载均衡之NAT模式部署
1.LVS的NAT模式介绍 参考自官网:http://www.linuxvirtualserver.org/zh/lvs3.html 由于IPv4中IP地址空间的日益紧张和安全方面的原因,很多网络使用 ...
- linux 局域网内文件传送
Linux scp命令用于Linux之间复制文件和目录,具体如何使用这里好好介绍一下,从本地复制到远程.从远程复制到本地是两种使用方式.这里有具体举例: ================== Linu ...
- Java 多线程之 synchronized 和 volatile 的比較
概述 在做多线程并发处理时,常常须要对资源进行可见性訪问和相互排斥同步操作.有时候,我们可能从前辈那里得知我们须要对资源进行 volatile 或是 synchronized 关键字修饰处理.但是,我 ...