Chinaunix首页 | 论坛 | 博客
  • 博客访问: 153302
  • 博文数量: 101
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 9
  • 用 户 组: 普通用户
  • 注册时间: 2016-06-17 08:11
文章分类
文章存档

2017年(91)

2016年(10)

我的朋友

分类: 嵌入式

2017-03-21 09:37:48

原文地址:linux设备模型kobject,kset 作者:andyhzw


点击(此处)折叠或打开

  1. #include<linux/device.h>
  2. #include<linux/module.h>
  3. #include<linux/kernel.h>
  4. #include<linux/stat.h>
  5. #include<linux/init.h>
  6. #include<linux/string.h>
  7. #include<linux/sysfs.h>
  8. #include<linux/kobject.h>

  9. MODULE_LICENSE("Dual BSD/GPL");
  10. MODULE_AUTHOR("SM");

  11. struct     kset    *set1;
  12. struct     kset    *set2;
  13. struct    kset    set3;
  14. struct     kobject    set4;


  15. int        kset_filter1(struct kset *kset,struct kobject *kobj)
  16. {
  17.     printk("filter 1 work !");
  18.     return 1;
  19. }

  20. const char * kset_name1(struct kset* kset,struct kobject *kobj)
  21. {
  22.     static    char buf[20];
  23.     printk("name 1\n");
  24.     printk("%s ",kobj->name);
  25.     sprintf(buf,"chage %s \n",kobj->name);
  26.     return buf;
  27. }


  28. /*
  29. <linux/kobject.h>
  30. #define UEVENT_NUM_ENVP            32    // number of env pointers
  31. #define UEVENT_BUFFER_SIZE        2048    // buffer for the variables
  32. struct kobj_uevent_env {
  33.     char *envp[UEVENT_NUM_ENVP];
  34.     int envp_idx;
  35.     char buf[UEVENT_BUFFER_SIZE];
  36.     int buflen;
  37. };
  38. */

  39. int    kset_uevent1(struct kset *kset,struct kobject *kobj,struct kobj_uevent_env *env)
  40. {
  41.     int        i = 0;
  42.     while(i < env->envp_idx)
  43.     {
  44.         printk("<1>envp[%d] = %s\n",i,env->envp[i]);
  45.         i++;
  46.     }
  47.     
  48.     return 0;
  49. }


  50. /*
  51. extern struct kset * __must_check kset_create_and_add(const char *name,
  52.                         const struct kset_uevent_ops *u,
  53.                         struct kobject *parent_kobj);
  54. */
  55. /*
  56. struct kset_uevent_ops {
  57.     int (* const filter)(struct kset *kset, struct kobject *kobj);
  58.     const char *(* const name)(struct kset *kset, struct kobject *kobj);
  59.     int (* const uevent)(struct kset *kset, struct kobject *kobj,
  60.          struct kobj_uevent_env *env);
  61. };
  62. */

  63. struct kset_uevent_ops kset_ops1 =
  64. {
  65.     .filter        = kset_filter1,
  66.     .name        = kset_name1,
  67.     .uevent        = kset_uevent1,
  68. };




  69. /*
  70. struct kobj_type {
  71.     void (*release)(struct kobject *kobj);
  72.     const struct sysfs_ops *sysfs_ops;
  73.     struct attribute **default_attrs;
  74.     const struct kobj_ns_type_operations *(*child_ns_type)(struct kobject *kobj);
  75.     const void *(*namespace)(struct kobject *kobj);
  76. };
  77. */

  78. /*
  79. struct attribute {
  80.     const char        *name;
  81.     mode_t            mode;
  82. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  83.     struct lock_class_key    *key;
  84.     struct lock_class_key    skey;
  85. #endif
  86. };
  87. */

  88. struct    attribute    attr1     =
  89. {
  90.     .name        = "attr_1",
  91.     .mode        = S_IRWXU,
  92. };

  93. struct    attribute    attr2    =
  94. {
  95.     .name        = "attr_2",
  96.     .mode        = S_IRUSR,
  97. };

  98. static    struct    attribute * attribute_set2[] =
  99. {
  100.     &attr1,
  101.     &attr2,
  102.     NULL,
  103. };


  104. ssize_t        set2_show(struct kobject *obj,struct attribute *attr,char *buf)
  105. {
  106.     sprintf(buf,"%s\n",attr->name);
  107.     return strlen(buf);
  108. }

  109. ssize_t        set2_store(struct kobject *kobj,struct attribute *attr,const char *buf,
  110.                         size_t    count)
  111. {
  112.     return count;
  113. }

  114. /*
  115. struct sysfs_ops {
  116.     ssize_t    (*show)(struct kobject *, struct attribute *,char *);
  117.     ssize_t    (*store)(struct kobject *,struct attribute *,const char *, size_t);
  118.     const void *(*namespace)(struct kobject *, const struct attribute *);
  119. };

  120. */
  121. struct     sysfs_ops    set2_ops =
  122. {
  123.     .show        = set2_show,
  124.     .store        = set2_store,
  125. };



  126. void    set2_release(struct kobject *obj)
  127. {
  128.     printk("release set2 \n");
  129. }



  130. struct     kobj_type        ktype =
  131. {
  132.     .release        = set2_release,
  133.     .sysfs_ops        = &set2_ops,
  134.     .default_attrs    = attribute_set2,
  135. };


  136. int        __init    sysfs_init(void)
  137. {
  138.     set1    = kset_create_and_add("set1",&kset_ops1,NULL);
  139.     set2    = kset_create_and_add("set2",&kset_ops1,&set1->kobj);

  140.     kobject_set_name(&set3.kobj,"set3");
  141.     set3.kobj.kset    = set1;
  142.     set3.kobj.ktype    = &ktype;
  143.     if(kset_register(&set3))
  144.         printk("set3 fail\n");
  145.     
  146.     set4.kset    = set1;
  147.     if(kobject_init_and_add(&set4,&ktype,NULL,"set4"))
  148.         printk("set4 fail\n");

  149.     printk("register success\n");

  150.     return 0;
  151. }



  152. void    __exit    sysfs_exit(void)
  153. {
  154.     kset_unregister(set1);
  155.     kset_unregister(set2);
  156.     kset_unregister(&set3);
  157.     kobject_del(&set4);
  158.     printk("unregister success\n");
  159. }

  160. module_init(sysfs_init);
  161. module_exit(sysfs_exit);


kset_create_and_add()

点击(此处)折叠或打开

  1. /**
  2.  * kset_create_and_add - create a struct kset dynamically and add it to sysfs
  3.  *
  4.  * @name: the name for the kset
  5.  * @uevent_ops: a struct kset_uevent_ops for the kset
  6.  * @parent_kobj: the parent kobject of this kset, if any.
  7.  *
  8.  * This function creates a kset structure dynamically and registers it
  9.  * with sysfs. When you are finished with this structure, call
  10.  * kset_unregister() and the structure will be dynamically freed when it
  11.  * is no longer being used.
  12.  *
  13.  * If the kset was not able to be created, NULL will be returned.
  14.  */
  15. struct kset *kset_create_and_add(const char *name,
  16.                  const struct kset_uevent_ops *uevent_ops,
  17.                  struct kobject *parent_kobj)
  18. {
  19.     struct kset *kset;
  20.     int error;

  21.     kset = kset_create(name, uevent_ops, parent_kobj);
  22.     if (!kset)
  23.         return NULL;
  24.     error = kset_register(kset);
  25.     if (error) {
  26.         kfree(kset);
  27.         return NULL;
  28.     }
  29.     return kset;
  30. }
  31. EXPORT_SYMBOL_GPL(kset_create_and_add);
待续。。。



阅读(629) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~