Chinaunix首页 | 论坛 | 博客
  • 博客访问: 177138
  • 博文数量: 43
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 451
  • 用 户 组: 普通用户
  • 注册时间: 2014-06-28 09:10
文章分类
文章存档

2016年(43)

我的朋友

分类: LINUX

2016-05-09 23:25:03


点击(此处)折叠或打开

  1. #include <linux/device.h>
  2. #include <linux/module.h>
  3. #include <linux/kernel.h>
  4. #include <linux/init.h>
  5. #include <linux/string.h>
  6. #include <linux/sysfs.h>
  7. #include <linux/stat.h>
  8.   
  9. MODULE_AUTHOR("David Xie");
  10. MODULE_LICENSE("Dual BSD/GPL");

  11.  

  12. /*声明release、show、store函数*/


  13. void obj_test_release(struct kobject *kobject);
  14. ssize_t kobj_test_show(struct kobject *kobject, struct attribute *attr,char *buf);
  15. ssize_t kobj_test_store(struct kobject *kobject,struct attribute *attr,const char *buf, size_t count);

  16. /*对应于kobject的目录下的一个文件,Name成员就是文件名*/
  17. struct attribute test_attr = {
  18.         .name = "kobj_config",
  19.         .mode = S_IRWXUGO,
  20. };
  21.   
  22. static struct attribute *def_attrs[] = {
  23.         &test_attr,
  24.         NULL,
  25. };
  26.   
  27. //kobject对象的操作
  28. struct sysfs_ops obj_test_sysops =
  29. {
  30.         .show = kobj_test_show,
  31.         .store = kobj_test_store,
  32. };
  33.  
  34. /*定义kobject对象的一些属性及对应的操作*/
  35. struct kobj_type ktype =
  36. {
  37.         .release = obj_test_release,
  38.         .sysfs_ops = &obj_test_sysops,
  39.         .default_attrs = def_attrs,
  40. };

  41. /*release方法释放该kobject对象*/
  42. void obj_test_release(struct kobject *kobject)
  43. {
  44.         printk("<0>""eric_test: release .\n");
  45. }

  46. /*当读文件时执行的操作*/
  47. ssize_t kobj_test_show(struct kobject *kobject, struct attribute *attr,char *buf)
  48. {
  49.         printk("<0>""have show.\n");
  50.         printk("<0>""attrname:%s.\n", attr->name);
  51.         sprintf(buf,"%s\n",attr->name);
  52.         return strlen(attr->name)+2;
  53. }

  54. /*当写文件时执行的操作*/
  55. ssize_t kobj_test_store(struct kobject *kobject,struct attribute *attr,const char *buf, size_t count)
  56. {
  57.         printk("<0>""havestore\n");
  58.         printk("<0>""write: %s\n",buf);
  59.         return count;
  60. }
  61.   
  62. struct kobject kobj;//声明kobject对象
  63. struct kobject kobj_child;//声明kobject对象
  64.  
  65. /*
  66.  struct kset_uevent_ops uevent_ops
  67.  {
  68.     
  69.  }
  70. */
  71. struct kset *hbx_kset = NULL;
  72. struct kset *hbx_driver = NULL;

  73. static int kobj_test_init(void)
  74. {
  75.         printk("<0>""kboject test init.\n");
  76.         //kobject_init_and_add(&kobj,&ktype,NULL,"kobject_testd");//初始化kobject对象kobj,并将其注册到linux系统
  77.         //kobject_init_and_add(&kobj_child,&ktype,&kobj,"kobject_test_child");//初始化kobject对象kobj,并将其注册到linux系统
  78.         hbx_kset = kset_create_and_add("hbx", NULL, NULL);
  79.         hbx_driver = kset_create_and_add("hbx_driver", NULL, &(hbx_kset->kobj));
  80.         
  81.         kobject_init_and_add(&kobj,&ktype,&(hbx_driver->kobj),"kobject_testd");//初始化kobject对象kobj,并将其注册到linux系统
  82.         kobject_init_and_add(&kobj_child,&ktype,&kobj,"kobject_test_child");
  83.         return 0;
  84. }
  85.   
  86. static void kobj_test_exit(void)
  87. {
  88.         printk("<0>""kobject test exit.\n");
  89.         kobject_del(&kobj);
  90.         kset_unregister(hbx_driver);
  91.         kset_unregister(hbx_kset);        
  92. }
  93.   
  94. module_init(kobj_test_init);

  95. module_exit(kobj_test_exit);

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