diff options
Diffstat (limited to 'samples/kobject')
-rw-r--r-- | samples/kobject/Makefile | 1 | ||||
-rw-r--r-- | samples/kobject/kobject-example.c | 137 |
2 files changed, 138 insertions, 0 deletions
diff --git a/samples/kobject/Makefile b/samples/kobject/Makefile new file mode 100644 index 000000000000..cce16e99fde5 --- /dev/null +++ b/samples/kobject/Makefile | |||
@@ -0,0 +1 @@ | |||
obj-$(CONFIG_SAMPLE_KOBJECT) += kobject-example.o | |||
diff --git a/samples/kobject/kobject-example.c b/samples/kobject/kobject-example.c new file mode 100644 index 000000000000..08d0d3ff3263 --- /dev/null +++ b/samples/kobject/kobject-example.c | |||
@@ -0,0 +1,137 @@ | |||
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 | /* | ||
17 | * This module shows how to create a simple subdirectory in sysfs called | ||
18 | * /sys/kernel/kobject-example In that directory, 3 files are created: | ||
19 | * "foo", "baz", and "bar". If an integer is written to these files, it can be | ||
20 | * later read out of it. | ||
21 | */ | ||
22 | |||
23 | static int foo; | ||
24 | static int baz; | ||
25 | static int bar; | ||
26 | |||
27 | /* | ||
28 | * The "foo" file where a static variable is read from and written to. | ||
29 | */ | ||
30 | static ssize_t foo_show(struct kobject *kobj, struct kobj_attribute *attr, | ||
31 | char *buf) | ||
32 | { | ||
33 | return sprintf(buf, "%d\n", foo); | ||
34 | } | ||
35 | |||
36 | static ssize_t foo_store(struct kobject *kobj, struct kobj_attribute *attr, | ||
37 | const char *buf, size_t count) | ||
38 | { | ||
39 | sscanf(buf, "%du", &foo); | ||
40 | return count; | ||
41 | } | ||
42 | |||
43 | static struct kobj_attribute foo_attribute = | ||
44 | __ATTR(foo, 0666, foo_show, foo_store); | ||
45 | |||
46 | /* | ||
47 | * More complex function where we determine which varible is being accessed by | ||
48 | * looking at the attribute for the "baz" and "bar" files. | ||
49 | */ | ||
50 | static ssize_t b_show(struct kobject *kobj, struct kobj_attribute *attr, | ||
51 | char *buf) | ||
52 | { | ||
53 | int var; | ||
54 | |||
55 | if (strcmp(attr->attr.name, "baz") == 0) | ||
56 | var = baz; | ||
57 | else | ||
58 | var = bar; | ||
59 | return sprintf(buf, "%d\n", var); | ||
60 | } | ||
61 | |||
62 | static ssize_t b_store(struct kobject *kobj, struct kobj_attribute *attr, | ||
63 | const char *buf, size_t count) | ||
64 | { | ||
65 | int var; | ||
66 | |||
67 | sscanf(buf, "%du", &var); | ||
68 | if (strcmp(attr->attr.name, "baz") == 0) | ||
69 | baz = var; | ||
70 | else | ||
71 | bar = var; | ||
72 | return count; | ||
73 | } | ||
74 | |||
75 | static struct kobj_attribute baz_attribute = | ||
76 | __ATTR(baz, 0666, b_show, b_store); | ||
77 | static struct kobj_attribute bar_attribute = | ||
78 | __ATTR(bar, 0666, b_show, b_store); | ||
79 | |||
80 | |||
81 | /* | ||
82 | * Create a group of attributes so that we can create and destory them all | ||
83 | * at once. | ||
84 | */ | ||
85 | static struct attribute *attrs[] = { | ||
86 | &foo_attribute.attr, | ||
87 | &baz_attribute.attr, | ||
88 | &bar_attribute.attr, | ||
89 | NULL, /* need to NULL terminate the list of attributes */ | ||
90 | }; | ||
91 | |||
92 | /* | ||
93 | * An unnamed attribute group will put all of the attributes directly in | ||
94 | * the kobject directory. If we specify a name, a subdirectory will be | ||
95 | * created for the attributes with the directory being the name of the | ||
96 | * attribute group. | ||
97 | */ | ||
98 | static struct attribute_group attr_group = { | ||
99 | .attrs = attrs, | ||
100 | }; | ||
101 | |||
102 | static struct kobject *example_kobj; | ||
103 | |||
104 | static int example_init(void) | ||
105 | { | ||
106 | int retval; | ||
107 | |||
108 | /* | ||
109 | * Create a simple kobject with the name of "kobject_example", | ||
110 | * located under /sys/kernel/ | ||
111 | * | ||
112 | * As this is a simple directory, no uevent will be sent to | ||
113 | * userspace. That is why this function should not be used for | ||
114 | * any type of dynamic kobjects, where the name and number are | ||
115 | * not known ahead of time. | ||
116 | */ | ||
117 | example_kobj = kobject_create_and_add("kobject_example", kernel_kobj); | ||
118 | if (!example_kobj) | ||
119 | return -ENOMEM; | ||
120 | |||
121 | /* Create the files associated with this kobject */ | ||
122 | retval = sysfs_create_group(example_kobj, &attr_group); | ||
123 | if (retval) | ||
124 | kobject_put(example_kobj); | ||
125 | |||
126 | return retval; | ||
127 | } | ||
128 | |||
129 | static void example_exit(void) | ||
130 | { | ||
131 | kobject_put(example_kobj); | ||
132 | } | ||
133 | |||
134 | module_init(example_init); | ||
135 | module_exit(example_exit); | ||
136 | MODULE_LICENSE("GPL"); | ||
137 | MODULE_AUTHOR("Greg Kroah-Hartman <greg@kroah.com>"); | ||