1
2
3
4
5
6
7
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
18
19
20
21
22
23static int foo;
24static int baz;
25static int bar;
26
27
28
29
30static ssize_t foo_show(struct kobject *kobj, struct kobj_attribute *attr,
31 char *buf)
32{
33 return sprintf(buf, "%d\n", foo);
34}
35
36static ssize_t foo_store(struct kobject *kobj, struct kobj_attribute *attr,
37 const char *buf, size_t count)
38{
39 int ret;
40
41 ret = kstrtoint(buf, 10, &foo);
42 if (ret < 0)
43 return ret;
44
45 return count;
46}
47
48
49static struct kobj_attribute foo_attribute =
50 __ATTR(foo, 0664, foo_show, foo_store);
51
52
53
54
55
56static ssize_t b_show(struct kobject *kobj, struct kobj_attribute *attr,
57 char *buf)
58{
59 int var;
60
61 if (strcmp(attr->attr.name, "baz") == 0)
62 var = baz;
63 else
64 var = bar;
65 return sprintf(buf, "%d\n", var);
66}
67
68static ssize_t b_store(struct kobject *kobj, struct kobj_attribute *attr,
69 const char *buf, size_t count)
70{
71 int var, ret;
72
73 ret = kstrtoint(buf, 10, &var);
74 if (ret < 0)
75 return ret;
76
77 if (strcmp(attr->attr.name, "baz") == 0)
78 baz = var;
79 else
80 bar = var;
81 return count;
82}
83
84static struct kobj_attribute baz_attribute =
85 __ATTR(baz, 0664, b_show, b_store);
86static struct kobj_attribute bar_attribute =
87 __ATTR(bar, 0664, b_show, b_store);
88
89
90
91
92
93
94static struct attribute *attrs[] = {
95 &foo_attribute.attr,
96 &baz_attribute.attr,
97 &bar_attribute.attr,
98 NULL,
99};
100
101
102
103
104
105
106
107static struct attribute_group attr_group = {
108 .attrs = attrs,
109};
110
111static struct kobject *example_kobj;
112
113static int __init example_init(void)
114{
115 int retval;
116
117
118
119
120
121
122
123
124
125
126 example_kobj = kobject_create_and_add("kobject_example", kernel_kobj);
127 if (!example_kobj)
128 return -ENOMEM;
129
130
131 retval = sysfs_create_group(example_kobj, &attr_group);
132 if (retval)
133 kobject_put(example_kobj);
134
135 return retval;
136}
137
138static void __exit example_exit(void)
139{
140 kobject_put(example_kobj);
141}
142
143module_init(example_init);
144module_exit(example_exit);
145MODULE_LICENSE("GPL v2");
146MODULE_AUTHOR("Greg Kroah-Hartman <greg@kroah.com>");
147