鱼C论坛

 找回密码
 立即注册
查看: 2582|回复: 0

[技术交流] Linux设备驱动模型-Kobject

[复制链接]
发表于 2016-9-29 14:43:34 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 寒小咸 于 2016-9-29 14:50 编辑

概述
Kobject是linux设备驱动模型的基础,也是设备模型中抽象的一部分。如果想了解设备驱动模型就需要明白Kobject的构成或原理。linux内核为了兼容各种形形色色的设备,就需要对各种设备的共性进行抽象,抽象出一个基类,其余的设备只需要继承此基类就可以了。而此基类就是kobject,但是C语言没有面向对象语法,这时候就需要将此基类(Kobject)嵌入到具体的结构体中,从而就可以访问控制此设备的操作。通常驱动程序员很少使用到kobject结构及其相关接口,而是使用封装之后的更高层的接口函数。

Kobject结构体
  1. struct kobject {
  2.         const char                *name;
  3.         struct list_head        entry;
  4.         struct kobject                *parent;
  5.         struct kset                *kset;
  6.         struct kobj_type        *ktype;
  7.         struct kernfs_node        *sd;
  8.         struct kref                kref;
  9. #ifdef CONFIG_DEBUG_KOBJECT_RELEASE
  10.         struct delayed_work        release;
  11. #endif
  12.         unsigned int state_initialized:1;
  13.         unsigned int state_in_sysfs:1;
  14.         unsigned int state_add_uevent_sent:1;
  15.         unsigned int state_remove_uevent_sent:1;
  16.         unsigned int uevent_suppress:1;
  17. };
复制代码

name:   用来表示内核对象的名称,如果该内核对象加入到系统,那么它的name就会出现在sys目录下。
entry:    用来将一系列的内核对象kobject连接成链表
parent:  用来指向该内核对象的上层节点,从而可以实现内核对象的层次化结构
kset:      用来执行内核对象所属的kset。kset对象用来容纳一系列同类型的kobject
ktype:    用来定义该内核对象的sys文件系统的相关操作函数和属性。
sd:         用来表示该内核对象在sys文件系统中的目录项实例
kref:       其核心是原子操作变量,用来表示该内核对象的引用计数。
state_initialized: 用来表示该内核对象的初始化状态,1表示已经初始化,0表示未初始化。
state_in_sysfs:   用来表示该内核对象是否在sys中已经存在。
state_add_uevent_sent: 用来表示该内核对象是否向用户空间发送了ADD uevent事件
state_remove_uevent_sent:用来表示该内核对象是否向用户空间发送了Remove uevent事件
uevent_suppress: 用来表示该内核对象状态发生改变时,时候向用户空间发送uevent事件,1表示不发送。

kobject数据结构通常的用法就是嵌入到某一个对象的数据结构中,比如struct device结构
  1. struct device {
  2.         struct device                *parent;

  3.         struct device_private        *p;

  4.         struct kobject kobj;
  5.         const char                *init_name; /* initial name of the device */
  6.         const struct device_type *type;
复制代码


Kobject相关操作函数
kobject相关的操作函数一般驱动程序员是不会直接操作的。
1.  kobject_init
  1. void kobject_init(struct kobject *kobj, struct kobj_type *ktype)
  2. {
  3.         char *err_str;

  4.         if (!kobj) {                                         //合法性检测
  5.                 err_str = "invalid kobject pointer!";
  6.                 goto error;
  7.         }
  8.         if (!ktype) {                                       //kobject结构必须设置相应的属性
  9.                 err_str = "must have a ktype to be initialized properly!\n";
  10.                 goto error;
  11.         }
  12.         if (kobj->state_initialized) {                      //如果已经初始化,在打印log
  13.                 /* do not error out as sometimes we can recover */
  14.                 printk(KERN_ERR "kobject (%p): tried to init an initialized "
  15.                        "object, something is seriously wrong.\n", kobj);
  16.                 dump_stack();
  17.         }

  18.         kobject_init_internal(kobj);
  19.         kobj->ktype = ktype;                                       
  20.         return;

  21. error:
  22.         printk(KERN_ERR "kobject (%p): %s\n", kobj, err_str);
  23.         dump_stack();
  24. }
复制代码

除了ktype成员外,真正的初始化在kobject_init_internal函数中实现
  1. static void kobject_init_internal(struct kobject *kobj)
  2. {
  3.         if (!kobj)
  4.                 return;
  5.         kref_init(&kobj->kref);                         //原子变量初始化
  6.         INIT_LIST_HEAD(&kobj->entry);                   //初始化链表
  7.         kobj->state_in_sysfs = 0;                       //没有出现在sys目录中
  8.         kobj->state_add_uevent_sent = 0;                 
  9.         kobj->state_remove_uevent_sent = 0;
  10.         kobj->state_initialized = 1;                    //已经初始化
  11. }
复制代码


2.  kobject_set_name: 用于设置kobject中的name成员
  1. int kobject_set_name(struct kobject *kobj, const char *fmt, ...)
  2. {
  3.         va_list vargs;
  4.         int retval;

  5.         va_start(vargs, fmt);
  6.         retval = kobject_set_name_vargs(kobj, fmt, vargs);
  7.         va_end(vargs);

  8.         return retval;
  9. }
复制代码

如果kobject已经被加入到系统当中,就必须使用kobject_rename函数
  1. int kobject_set_name_vargs(struct kobject *kobj, const char *fmt,
  2.                                   va_list vargs)
  3. {
  4.         const char *old_name = kobj->name;
  5.         char *s;

  6.         if (kobj->name && !fmt)                    //名字存在返回
  7.                 return 0;

  8.         kobj->name = kvasprintf(GFP_KERNEL, fmt, vargs);  
  9.         if (!kobj->name) {
  10.                 kobj->name = old_name;
  11.                 return -ENOMEM;
  12.         }

  13.         /* ewww... some of these buggers have '/' in the name ... */
  14.         while ((s = strchr(kobj->name, '/')))   //除掉name中存在“/”
  15.                 s[0] = '!';

  16.         kfree(old_name);
  17.         return 0;
  18. }
复制代码


3. kobject_get(增加kobject的引用计数)
  1. /**
  2. * kobject_get - increment refcount for object.
  3. * @kobj: object.
  4. */
  5. struct kobject *kobject_get(struct kobject *kobj)
  6. {
  7.         if (kobj)
  8.                 kref_get(&kobj->kref);      //原子加1操作
  9.         return kobj;
  10. }
复制代码


4. kobject_put(减少kobject的引用计数)
  1. /**
  2. * kobject_put - decrement refcount for object.
  3. * @kobj: object.
  4. *
  5. * Decrement the refcount, and if 0, call kobject_cleanup().
  6. */
  7. void kobject_put(struct kobject *kobj)
  8. {
  9.         if (kobj) {
  10.                 if (!kobj->state_initialized)             //如果没有初始化,调用put函数,打印log
  11.                         WARN(1, KERN_WARNING "kobject: '%s' (%p): is not "
  12.                                "initialized, yet kobject_put() is being "
  13.                                "called.\n", kobject_name(kobj), kobj);
  14.                 kref_put(&kobj->kref, kobject_release);
  15.         }
  16. }
复制代码


5.  kobject_add(增加一个kobject到sys目录下)
  1. int kobject_add(struct kobject *kobj, struct kobject *parent,
  2.                 const char *fmt, ...)
  3. {
  4.         va_list args;
  5.         int retval;

  6.         if (!kobj)
  7.                 return -EINVAL;

  8.         if (!kobj->state_initialized) {       //没有初始化的kobject,返回错误
  9.                 printk(KERN_ERR "kobject '%s' (%p): tried to add an "
  10.                        "uninitialized object, something is seriously wrong.\n",
  11.                        kobject_name(kobj), kobj);
  12.                 dump_stack();
  13.                 return -EINVAL;
  14.         }
  15.         va_start(args, fmt);
  16.         retval = kobject_add_varg(kobj, parent, fmt, args);    //核心的实现代码
  17.         va_end(args);

  18.         return retval;
  19. }
  20. static int kobject_add_varg(struct kobject *kobj, struct kobject *parent,
  21.                             const char *fmt, va_list vargs)
  22. {
  23.         int retval;

  24.         retval = kobject_set_name_vargs(kobj, fmt, vargs);         //设置kobject的name
  25.         if (retval) {
  26.                 printk(KERN_ERR "kobject: can not set name properly!\n");
  27.                 return retval;
  28.         }
  29.         kobj->parent = parent;                   //设置parent成员
  30.         return kobject_add_internal(kobj);       //调用此函数进行进一步add操作
  31. }
  32. static int kobject_add_internal(struct kobject *kobj)
  33. {
  34.         int error = 0;
  35.         struct kobject *parent;

  36.         if (!kobj)
  37.                 return -ENOENT;

  38.         if (!kobj->name || !kobj->name[0]) {                  //尝试注册一个name为空的kobject
  39.                 WARN(1, "kobject: (%p): attempted to be registered with empty "
  40.                          "name!\n", kobj);
  41.                 return -EINVAL;
  42.         }

  43.         parent = kobject_get(kobj->parent);                  //kobj的parent的引用计数加1,返回parent

  44.         /* join kset if set, use it as parent if we do not already have one */
  45.         if (kobj->kset) {                           
  46.                 if (!parent)                                //在kset存在的前提下,如果kobj的parent节点为NULL,就将kobj的kset中的kobj设置为kobj的parent
  47.                         parent = kobject_get(&kobj->kset->kobj);
  48.                 kobj_kset_join(kobj);                       //添加kobj到kset的list链表中
  49.                 kobj->parent = parent;                      //设置kobj的parent
  50.         }

  51.         pr_debug("kobject: '%s' (%p): %s: parent: '%s', set: '%s'\n",
  52.                  kobject_name(kobj), kobj, __func__,
  53.                  parent ? kobject_name(parent) : "<NULL>",
  54.                  kobj->kset ? kobject_name(&kobj->kset->kobj) : "<NULL>");

  55.         error = create_dir(kobj);                          //在sys下创建目录
  56.         if (error) {
  57.                 kobj_kset_leave(kobj);
  58.                 kobject_put(parent);
  59.                 kobj->parent = NULL;

  60.                 /* be noisy on error issues */
  61.                 if (error == -EEXIST)
  62.                         WARN(1, "%s failed for %s with "
  63.                              "-EEXIST, don't try to register things with "
  64.                              "the same name in the same directory.\n",
  65.                              __func__, kobject_name(kobj));
  66.                 else
  67.                         WARN(1, "%s failed for %s (error: %d parent: %s)\n",
  68.                              __func__, kobject_name(kobj), error,
  69.                              parent ? kobject_name(parent) : "'none'");
  70.         } else
  71.                 kobj->state_in_sysfs = 1;                  //设置state_in_sysfs为1,代表obj已经初始化在sys中

  72.         return error;
  73. }
  74. 下来大概看看create_dir的过程,也就是创建目录的过程。
  75. static int create_dir(struct kobject *kobj)
  76. {
  77.         const struct kobj_ns_type_operations *ops;
  78.         int error;

  79.         error = sysfs_create_dir_ns(kobj, kobject_namespace(kobj));                     //为kobject创建目录
  80.         if (error)
  81.                 return error;

  82.         error = populate_dir(kobj);                                           //填充kobj的属性
  83.         if (error) {
  84.                 sysfs_remove_dir(kobj);
  85.                 return error;
  86.         }

  87.         /*
  88.          * @kobj->sd may be deleted by an ancestor going away.  Hold an
  89.          * extra reference so that it stays until @kobj is gone.
  90.          */
  91.         sysfs_get(kobj->sd);                                             //kobj->sd引用计数加1

  92.         /*
  93.          * If @kobj has ns_ops, its children need to be filtered based on
  94.          * their namespace tags.  Enable namespace support on @kobj->sd.
  95.          */
  96.         ops = kobj_child_ns_ops(kobj);                             //namespace的知识
  97.         if (ops) {
  98.                 BUG_ON(ops->type <= KOBJ_NS_TYPE_NONE);
  99.                 BUG_ON(ops->type >= KOBJ_NS_TYPES);
  100.                 BUG_ON(!kobj_ns_type_registered(ops->type));

  101.                 sysfs_enable_ns(kobj->sd);
  102.         }

  103.         return 0;
  104. }
  105. 主要是分析下sysfs_create_dir_ns函数的执行流程。
  106. /**
  107. * sysfs_create_dir_ns - create a directory for an object with a namespace tag
  108. * @kobj: object we're creating directory for
  109. * @ns: the namespace tag to use
  110. */
  111. int sysfs_create_dir_ns(struct kobject *kobj, const void *ns)
  112. {
  113.         struct kernfs_node *parent, *kn;

  114.         BUG_ON(!kobj);                            //如果执行到这里,kobj为NULL,就会panic

  115.         if (kobj->parent)           
  116.                 parent = kobj->parent->sd;         //如果存在parent就在父目录下创建
  117.         else
  118.                 parent = sysfs_root_kn;            //如果不存在parent,就在sys目录下创建

  119.         if (!parent)
  120.                 return -ENOENT;

  121.         kn = kernfs_create_dir_ns(parent, kobject_name(kobj),        //正真的创建函数,不是此节的重点,不再分析。
  122.                                   S_IRWXU | S_IRUGO | S_IXUGO, kobj, ns);
  123.         if (IS_ERR(kn)) {
  124.                 if (PTR_ERR(kn) == -EEXIST)
  125.                         sysfs_warn_dup(parent, kobject_name(kobj));
  126.                 return PTR_ERR(kn);
  127.         }

  128.         kobj->sd = kn;
  129.         return 0;
  130. }
复制代码


6.  kobject_init_and_add(kobject_init函数和kobject_add函数的合并)
7.  kobject_create(动态分析一个kobject)
  1. struct kobject *kobject_create(void)
  2. {
  3.         struct kobject *kobj;

  4.         kobj = kzalloc(sizeof(*kobj), GFP_KERNEL);            //分配一个kobject结构
  5.         if (!kobj)
  6.                 return NULL;

  7.         kobject_init(kobj, &dynamic_kobj_ktype);              //初始化kobject,kobj_type会在后面说到
  8.         return kobj;
  9. }
复制代码


8.  kobject_create_and_add(动态创建一个kobject然后注册到sys文件系统中,也就是kobject_create和kobject_add的结合体)
9.  kobject_del(从sys删除对应的kobj)
  1. void kobject_del(struct kobject *kobj)
  2. {
  3.         struct kernfs_node *sd;

  4.         if (!kobj)
  5.                 return;           //如果不存在,直接返回。

  6.         sd = kobj->sd;
  7.         sysfs_remove_dir(kobj);    //从sys目录下删除kobj
  8.         sysfs_put(sd);             //sd的引用技术减去1

  9.         kobj->state_in_sysfs = 0;   //表示没有在sys中初始化
  10.         kobj_kset_leave(kobj);      //如果存在于kset中,从kset中删除
  11.         kobject_put(kobj->parent);  //parent引用计数减1
  12.         kobj->parent = NULL;
  13. }
复制代码


举例
在sys目录下创建一个123_test的文件夹。
  1. #include <linux/module.h>
  2. #include <linux/kernel.h>
  3. #include <linux/kobject.h>
  4. #include <linux/sysfs.h>
  5. #include <linux/init.h>
  6.                                                                                                                
  7. static struct kobject *kobj;

  8. static int kobject_test_init(void)
  9. {

  10.     kobj = kobject_create_and_add("123_test", NULL);

  11.     return 0;
  12. }

  13. static void kobject_test_exit(void)
  14. {
  15.     kobject_del(kobj);
  16. }

  17. module_init(kobject_test_init);
  18. module_exit(kobject_test_exit);
  19. MODULE_LICENSE("GPL v2");
复制代码

测试结果如下:
root@test:/sys # ls -l
drwxr-xr-x root     root              2012-01-02 09:31 123_test
drwxr-xr-x root     root              2012-01-02 07:13 bcm-dhd
drwxr-xr-x root     root              2012-01-02 05:02 block
drwxr-xr-x root     root              2012-01-02 05:02 bus
drwxr-xr-x root     root              2012-01-02 05:02 class
....
接着在123_test目录下创建456_test目录,修改代码如下:
  1. #include <linux/module.h>                                                                                      
  2. #include <linux/kernel.h>
  3. #include <linux/kobject.h>
  4. #include <linux/sysfs.h>
  5. #include <linux/init.h>

  6. static struct kobject *kobj;
  7. static struct kobject *ckobj;

  8. static int kobject_test_init(void)
  9. {

  10.     kobj = kobject_create_and_add("123_test", NULL);
  11.     ckobj = kobject_create_and_add("456_test", kobj);

  12.     return 0;
  13. }

  14. static void kobject_test_exit(void)
  15. {
  16.     kobject_del(ckobj);
  17.     kobject_del(kobj);
  18. }

  19. module_init(kobject_test_init);
  20. module_exit(kobject_test_exit);
  21. MODULE_LICENSE("GPL v2");
复制代码


测试结果如下:
root@test:/sys/123_test # ls
456_test
而目前456_test目录下是不存在任何文件的,那是因为我们没有添加该obj的属性,此测试case将在下解完善。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-20 19:38

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表