设备模型的基础是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的更多相关文章

  1. Linux 设备模型之 (kobject、kset 和 Subsystem)(二)

    问题描写叙述:前文我们知道了/sys是包括内核和驱动的实施信息的,用户能够通过 /sys 这个接口.用户通过这个接口能够一览内核设备的全貌.本文将从Linux内核的角度来看一看这个设备模型是怎样构建的 ...

  2. Linux设备模型:基础篇

    linux提供了新的设备模型:总线(bus).设备(device).驱动(driver).其中总线是处理器与设备之间通道,在设备模型中,所有的设备都通过总线相连:设备是对于一个设备的详细信息描述,驱动 ...

  3. 设备模型之kobject,kset及其关系

    Linux2.6以后的设备驱动,都是在设备模型的基础上构建的,因此,要编写linux下的设备驱动程序,不论是usb设备,pci设备等,都需要了解设备模型. 设备模型的基础结构体主要是kobject,k ...

  4. 【原创】linux设备模型之kset/kobj/ktype分析

    背 景 Read the fucking source code! --By 鲁迅 A picture is worth a thousand words. --By 高尔基 说明: Kernel版本 ...

  5. linux设备模型_转

    建议原博文查看,效果更佳. 转自:http://www.cnblogs.com/wwang/category/269350.html Linux设备模型 (1) 随着计算机的周边外设越来越丰富,设备管 ...

  6. Linux设备模型 (2)

    上一篇文章<Linux设备模型 (1)>主要介绍了Linux设备模型在用户空间的接口sysfs,用户通过这个接口可以一览内核设备的全貌.本文将从Linux内核的角度来看一看这个设备模型是如 ...

  7. Linux设备模型之kobject

    阿辉原创,转载请注明出处 参考文档:LDD3-ch14.内核文档Documentation/kobject.txt,本文中使用到的代码均摘自Linux-3.4.75 ----------------- ...

  8. 内核设备模型从kobject到子系统

                                         内核设备模型 目的:表示设备和设备在系统中的拓扑关系 优点:1减少内核代码量,2可以统一查看所有设备状态和所连接的总线,3可以 ...

  9. Linux设备模型分析之kset(基于3.10.1内核)

    作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz 内核版本:3.10.1   一.kset结构定义 kset结构体定义在include/linux/kobject.h ...

随机推荐

  1. 【三维偏序】【分块】bzoj3262 陌上花开

    裸的三维偏序. 对x坐标排序,y.z坐标分块.复杂度O(n*sqrt(n*log(n))).代码很短. #include<cstdio> #include<cmath> #in ...

  2. 5.5(java学习笔记)TreeSet和TreeMap

    1.TreeMap TreeMap是可排序的Map类,使用这个类时,TreeMap会对存放的数据进行排序. 排序是根据key来排序的,排序规则是key实现comparable接口中的compareTo ...

  3. flask的配置设置的几种方式

     Flask的配置对象(config)是一个字典(dict)的子类(subclass),所以你可以把配置用键值对的方式存储进去. 1.一些重要的配置,可以设置在系统环境变量里,又或者放到某个服务器里, ...

  4. Difference between val() and text()

    .val() works on input elements (or any element with a value attribute?) and .text() will not work on ...

  5. Scala快学笔记(一)

    一,基本概念: 1,Scala是一种基于JVM的面向对象和函数式编程语言 2,基本类型:数值类型 ->:Byte,Short,Int,Long,Float,Double和布尔类型:Boolean ...

  6. Docker解析及轻量级PaaS平台演练(四)--Fig相关介绍

    本篇中将会使用开源工具Fig Fig是什么? 简单的说就是对Docker的封装,从而方便我们构建应用的运行环境 它所做的事情是协调Docker上的各个Container之间的联系,并通过服务发现的方式 ...

  7. idea Tomcat 部署 war和war exploded的区别

    idea Tomcat 部署 war和war exploded的区别 学习了:https://blog.csdn.net/linjpg/article/details/73322881 explode ...

  8. 倍福TwinCAT(贝福Beckhoff)应用教程12.3 TwinCAT控制松下伺服 NC进阶

    在前面一节,我们简单介绍了通过PLC+HMI实现完整控制松下伺服的上使能-运动,采集位置,速度等功能,这里我们会大量简化用到的贝福功能块(为了更加实用).首先依然是对单个轴的封装,我们之前的做法,例如 ...

  9. 微信小程序 - 回到自己位置(map)

    演示效果: 图片资源 index.js /** * 回到自己位置,在cover-image上绑定点击事件即可. */ clickcontrol(e) { let mpCtx = wx.createMa ...

  10. [转]SQL Server 性能调优(io)

      目录 诊断磁盘io问题 常见的磁盘问题 容量替代了性能 负载隔离配置有问题 分区对齐配置有问题 总结 关于io这一块,前面的东西如磁盘大小,磁盘带宽,随机读取写入,顺序读取写入,raid选择,DA ...