Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7570672
  • 博文数量: 961
  • 博客积分: 15795
  • 博客等级: 上将
  • 技术积分: 16612
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-07 14:23
文章分类

全部博文(961)

文章存档

2016年(1)

2015年(61)

2014年(41)

2013年(51)

2012年(235)

2011年(391)

2010年(181)

分类: 嵌入式

2011-02-25 10:29:58

  1. /* 文件名:sysfs_test.c */
  2. /* 说明:内核集合与对象例程 */

  3. #include <linux/module.h>

  4. MODULE_LICENSE("GPL");

  5. #define SZ_STRING 30 /* 字符串的最大长度 */
  6. #define NR_KOBJS 3 /* 内核对象的个数 */

  7. /* 仿照内核的 kobj_attr_show 函数 */
  8. static ssize_t test_attr_show(struct kobject *kobj, struct attribute *attr,
  9.         char *buf)
  10. {
  11.     struct kobj_attribute *kattr;
  12.     ssize_t ret = -EIO;
  13.     kattr = container_of(attr, struct kobj_attribute, attr);
  14.     if (kattr->show)
  15.         ret = kattr->show(kobj, kattr, buf);
  16.     return ret;
  17. }

  18. /* 仿照内核的 kobj_attr_store 函数 */
  19. static ssize_t test_attr_store(struct kobject *kobj, struct attribute *attr,
  20.         const char *buf, size_t count)
  21. {
  22.     struct kobj_attribute *kattr;
  23.     ssize_t ret = -EIO;
  24.     kattr = container_of(attr, struct kobj_attribute, attr);
  25.     if (kattr->store)
  26.         ret = kattr->store(kobj, kattr, buf, count);
  27.     return ret;
  28. }

  29. /* 仿照内核的 kobj_sysfs_ops 变量 */
  30. static struct sysfs_ops test_sysfs_ops = {
  31.     .show    = test_attr_show,
  32.     .store    = test_attr_store,
  33. };

  34. /* 内核对象的释放函数 */
  35. static void test_release(struct kobject *kobj)
  36. {
  37.     kfree(kobj);
  38. }

  39. /* 属性 name 的 show 函数 */
  40. static ssize_t test_name_show(struct kobject *kobj,
  41.         struct kobj_attribute *attr, char *buf)
  42. {
  43.     return sprintf(buf, "%s\n", kobject_name(kobj));
  44. }

  45. /* 属性 name 的 store 函数,因为是只读属性,因此直接返回错误 */
  46. static ssize_t test_name_store(struct kobject *kobj,
  47.         struct kobj_attribute *attr, const char *buf, size_t len)
  48. {
  49.     return -EACCES; /* 错误号表示无权限 */
  50. }

  51. /* 属性 name 的定义 */
  52. static struct kobj_attribute test_attr_name =
  53.         __ATTR(name, S_IRUGO, test_name_show, test_name_store);

  54. /* 用于保存属性 string 对应的字符串 */
  55. static char test_string[SZ_STRING] = "test\n";

  56. /* 属性 string 的 show 函数 */
  57. static ssize_t test_string_show(struct kobject *kobj,
  58.         struct kobj_attribute *attr, char *buf)
  59. {
  60.     return sprintf(buf, test_string);
  61. }

  62. /* 属性 string 的 store 函数 */
  63. static ssize_t test_string_store(struct kobject *kobj,
  64.         struct kobj_attribute *attr, const char *buf, size_t len)
  65. {
  66.     if (len >= SZ_STRING) len = SZ_STRING-1; /* 限制写的长度 */
  67.     memcpy(test_string, buf, len); /* 复制数据 */
  68.     test_string[len] = '\0'; /* 字符串结尾 */
  69.     return len;
  70. }

  71. /* 属性 string 的定义 */
  72. static struct kobj_attribute test_attr_string =
  73.         __ATTR(string, S_IRUGO|S_IWUGO, test_string_show, test_string_store);

  74. /* 对象的默认属性 */
  75. static struct attribute *test_default_attr[] = {
  76.     &test_attr_name.attr,
  77.     &test_attr_string.attr,
  78.     NULL,
  79. };

  80. /* 对象的类型 */
  81. static struct kobj_type test_kobj_type = {
  82.     .release = test_release, /* 释放函数 */
  83.     .sysfs_ops = &test_sysfs_ops, /* 属性操作 */
  84.     .default_attrs = test_default_attr /* 默认属性 */
  85. };

  86. /* 用于保存动态创建的内核集合的指针 */
  87. static struct kset *kset;

  88. /* 模块的初始化函数 */
  89. static __init int sysfs_test_init(void)
  90. {
  91.     int i; /* 循环变量 */
  92.     int err; /* 用于暂存错误码 */
  93.     struct kobject *kobj, *next; /* 循环变量 */
  94.     pr_debug("sysfs_test_init: be called.\n");
  95.     /* 创建并注册内核集合,由于没有父对象,将成为顶级子系统 */
  96.     if ((kset = kset_create_and_add("test", NULL, NULL)) == NULL) {
  97.         pr_debug("sysfs_test_init: kset_create_and_add ERR!\n");
  98.         err = -ENOMEM;
  99.         goto kset_fail;
  100.     }
  101.     /* 为 kset 集合动态创建一个属性,使用 name 属性的定义 */
  102.     if ((err = sysfs_create_file(&kset->kobj, &test_attr_name.attr))) {
  103.         pr_debug("sysfs_test_init: sysfs_create_file ERR!\n");
  104.         goto sysfs_fail;
  105.     }
  106.     /* 注册 NR_KOBJS 个内核对象,都属于 kset 集合 */
  107.     for (i = 0; i < NR_KOBJS; i++) {
  108.         /* 分配内存 */
  109.         kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
  110.         if (!kobj) {
  111.             pr_debug("sysfs_test_init: kzalloc ERR!\n");
  112.             err = -ENOMEM;
  113.             goto kobject_fail;
  114.         }
  115.         /* 设置所属集合 */
  116.         kobj->kset = kset;
  117.         /* 初始化并注册 */
  118.         err = kobject_init_and_add(kobj, &test_kobj_type, NULL, "obj%d", i);
  119.         if (err) {
  120.             pr_debug("sysfs_test_init: kobject_register ERR = %d\n", err);
  121.             kfree(kobj);
  122.             goto kobject_fail;
  123.         }
  124.         /* 发送事件 */
  125.         kobject_uevent(kobj, KOBJ_ADD);
  126.     }
  127.     return 0;
  128. kobject_fail:
  129.     list_for_each_entry_safe(kobj, next, &kset->list, entry)
  130.         kobject_put(kobj);
  131. sysfs_fail:
  132.     kset_unregister(kset);
  133. kset_fail:
  134.     return err;
  135. }
  136. module_init(sysfs_test_init);

  137. /* 模块的退出函数 */
  138. static __exit void sysfs_test_exit(void)
  139. {
  140.     struct kobject *kobj, *next;
  141.     pr_debug("sysfs_test_exit: be called.\n");
  142.     /* 首先清理集合中的所有对象 */
  143.     list_for_each_entry_safe(kobj, next, &kset->list, entry)
  144.         kobject_put(kobj);
  145.     /* 然后注销集合 */
  146.     kset_unregister(kset);
  147. }
  148. module_exit(sysfs_test_exit);
  1. # 文件名:Makefile
  2. # 说明:内核集合与对象例程 Makefile
  3. # 模块名
  4. MOD_NAME := sysfs_test
  5. # 模块源码(不能出现与模块名相同的文件名)
  6. SRCS := \
  7. EXTRA_CFLAGS = -DDEBUG
  8. OBJS := $(SRCS:.c=.o)
  9. ifeq ($(KERNELRELEASE),)
  10. KERNELDIR ?= /lib/modules/$(shell uname -r)/build
  11. PWD := $(shell pwd)
  12. default:
  13. $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
  14. clean:
  15. -rm -f $(MOD_NAME).ko
  16. -rm -f $(MOD_NAME).o
  17. -rm -f $(OBJS)
  18. -rm -rf .tmp_versions
  19. -rm -f .*.cmd
  20. -rm -f *.mod.c
  21. -rm -f *.mod.o
  22. -rm -f Module.symvers
  23. -rm -f modules.order
  24. else
  25. $(MOD_NAME)-objs := $(OBJS)
  26. obj-m := $(MOD_NAME).o
  27. endif
阅读(1299) | 评论(0) | 转发(2) |
0

上一篇:获取 TID

下一篇:kobject

给主人留下些什么吧!~~