设备模型的基础---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 ...
随机推荐
- HDOJ 5009 Paint Pearls
Dicripntion Lee has a string of n pearls. In the beginning, all the pearls have no color. He plans t ...
- 【树状数组】bzoj2789 [Poi2012]Letters
处理数组A,A[i]表示字符串a的第i个字符排序后应去的位置,队列可. 对于多次出现的字符,其在a中的顺序和在b中的顺序应该是一一对应的. #include<cstdio> #includ ...
- [CF911D]Inversion Counting
题目大意: 给你一个数列,翻转其中一个区间,问每次翻转过后逆序对个数的奇偶性. 思路: 首先树状数组求出一开始的奇偶性,然后考虑每次翻转对答案的贡献. 对于整个区间,我们可以把翻转转化成若干次交换. ...
- 5.4 集合的排序(Java学习笔记)(Collections.sort(),及Arrays.sort()底层分析)
1.Comparable接口 这个接口顾名思义就是用于排序的,如果要对某些对象进行排序,那么该对象所在的类必须实现 Comparabld接口.Comparable接口只有一个方法CompareTo() ...
- 微服务之SpringCloud实战(一):SpringCloud简介
什么是微服务架构 微服务架构就是系统架构设计的一种风格,它主旨将一个独立的系统,拆分成各个微服务,各个微服务独立运行,他们之间通过Http的Restful API进行通信,拆分出来的微服务是根据原系统 ...
- Problem G: 零起点学算法27——等级分制度
#include<stdio.h> int main() { int a,b; while(scanf("%d %d",&a,&b)!=EOF) +a* ...
- 十. 图形界面(GUI)设计11.对话框
对话框是为了人机对话过程提供交互模式的工具.应用程序通过对话框,或给用户提供信息,或从用户获得信息.对话框是一个临时窗口,可以在其中放置用于得到用户输入的控件.在Swing中,有两个对话框类,它们是J ...
- (转)Android Eclipse 代码混淆
为了防止自己的劳动成果被别人窃取,混淆代码能有效防止被反编译 1. 大家也许都注意到新建一个工程会看到项目下边有这样proguard-project.txt一个文件,这个对混淆代码很重要,如果你不小心 ...
- sql获取汉字的拼音首字母的函数
ql获取汉字的拼音首字母 if exists (select * from sysobjects where id = object_id(N'[fn_ChineseToSpell]') and ...
- iOS -- 解决iOS11中navigationBar上使用initWithCustomView按钮图片错位 frame无效
在iOS11上当使用如下代码设置时 UIButton *shareButton = [UIButton buttonWithType:(UIButtonTypeCustom)]; shareButto ...