Chinaunix首页 | 论坛 | 博客
  • 博客访问: 220002
  • 博文数量: 19
  • 博客积分: 757
  • 博客等级: 军士长
  • 技术积分: 320
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-08 07:55
个人简介

醉卧沙场君莫笑

文章分类

全部博文(19)

文章存档

2016年(5)

2015年(2)

2014年(3)

2013年(1)

2012年(5)

2011年(3)

分类: LINUX

2012-06-03 20:43:27

Makefile:

点击(此处)折叠或打开

  1. obj-$(CONFIG_SAMPLE_KOBJECT) += kobject-example.o kset-example.o
  2. ifneq ($(KERNELRELEASE),)
  3. obj-m := test.o
  4. else
  5. KDIR := /lib/modules/$(shell uname -r)/build
  6. PWD := $(shell pwd)
  7. endif
  8. default:
  9. $(MAKE) -C $(KDIR) M=$(PWD) modules

test.c: (来自内核)

点击(此处)折叠或打开

  1. /*
  2.  * Sample kobject implementation
  3.  *
  4.  * Copyright (C) 2004-2007 Greg Kroah-Hartman <greg@kroah.com>
  5.  * Copyright (C) 2007 Novell Inc.
  6.  *
  7.  * Released under the GPL version 2 only.
  8.  *
  9.  */
  10. #include <linux/kobject.h>
  11. #include <linux/string.h>
  12. #include <linux/sysfs.h>
  13. #include <linux/module.h>
  14. #include <linux/init.h>

  15. /*
  16.  * This module shows how to create a simple subdirectory in sysfs called
  17.  * /sys/kernel/kobject-example In that directory, 3 files are created:
  18.  * "foo", "baz", and "bar". If an integer is written to these files, it can be
  19.  * later read out of it.
  20.  */

  21. static int foo;
  22. static int baz;
  23. static int bar;

  24. /*
  25.  * The "foo" file where a static variable is read from and written to.
  26.  */
  27. static ssize_t foo_show(struct kobject *kobj, struct kobj_attribute *attr,
  28.             char *buf)
  29. {
  30.     return sprintf(buf, "%d\n", foo);
  31. }

  32. static ssize_t foo_store(struct kobject *kobj, struct kobj_attribute *attr,
  33.              const char *buf, size_t count)
  34. {
  35.     sscanf(buf, "%du", &foo);
  36.     return count;
  37. }

  38. static struct kobj_attribute foo_attribute =
  39.     __ATTR(foo, 0666, foo_show, foo_store);

  40. /*
  41.  * More complex function where we determine which variable is being accessed by
  42.  * looking at the attribute for the "baz" and "bar" files.
  43.  */
  44. static ssize_t b_show(struct kobject *kobj, struct kobj_attribute *attr,
  45.          char *buf)
  46. {
  47.     int var;

  48.     if (strcmp(attr->attr.name, "baz") == 0)
  49.         var = baz;
  50.     else
  51.         var = bar;
  52.     return sprintf(buf, "%d\n", var);
  53. }

  54. static ssize_t b_store(struct kobject *kobj, struct kobj_attribute *attr,
  55.          const char *buf, size_t count)
  56. {
  57.     int var;

  58.     sscanf(buf, "%du", &var);
  59.     if (strcmp(attr->attr.name, "baz") == 0)
  60.         baz = var;
  61.     else
  62.         bar = var;
  63.     return count;
  64. }

  65. static struct kobj_attribute baz_attribute =
  66.     __ATTR(baz, 0666, b_show, b_store);
  67. static struct kobj_attribute bar_attribute =
  68.     __ATTR(bar, 0666, b_show, b_store);


  69. /*
  70.  * Create a group of attributes so that we can create and destroy them all
  71.  * at once.
  72.  */
  73. static struct attribute *attrs[] = {
  74.     &foo_attribute.attr,
  75.     &baz_attribute.attr,
  76.     &bar_attribute.attr,
  77.     NULL,    /* need to NULL terminate the list of attributes */
  78. };

  79. /*
  80.  * An unnamed attribute group will put all of the attributes directly in
  81.  * the kobject directory. If we specify a name, a subdirectory will be
  82.  * created for the attributes with the directory being the name of the
  83.  * attribute group.
  84.  */
  85. static struct attribute_group attr_group = {
  86.     .attrs = attrs,
  87. };

  88. static struct kobject *example_kobj;

  89. static int __init example_init(void)
  90. {
  91.     int retval;

  92.     /*
  93.      * Create a simple kobject with the name of "kobject_example",
  94.      * located under /sys/kernel/
  95.      *
  96.      * As this is a simple directory, no uevent will be sent to
  97.      * userspace. That is why this function should not be used for
  98.      * any type of dynamic kobjects, where the name and number are
  99.      * not known ahead of time.
  100.      */
  101.     example_kobj = kobject_create_and_add("kobject_example", kernel_kobj);
  102.     if (!example_kobj)
  103.         return -ENOMEM;

  104.     /* Create the files associated with this kobject */
  105.     retval = sysfs_create_group(example_kobj, &attr_group);
  106.     if (retval)
  107.         kobject_put(example_kobj);

  108.     return retval;
  109. }

  110. static void __exit example_exit(void)
  111. {
  112.     kobject_put(example_kobj);
  113. }

  114. module_init(example_init);
  115. module_exit(example_exit);
  116. MODULE_LICENSE("GPL");
  117. MODULE_AUTHOR("Greg Kroah-Hartman ");

阅读(1917) | 评论(1) | 转发(1) |
0

上一篇:printk和printf

下一篇:用U盘安装XP

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

shirya09282012-10-20 11:05:35

你写的东西怎么都没人评论呢?哈哈,我知道了——都看不懂