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/slab.h>
14#include <linux/module.h>
15#include <linux/init.h>
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31struct foo_obj {
32 struct kobject kobj;
33 int foo;
34 int baz;
35 int bar;
36};
37#define to_foo_obj(x) container_of(x, struct foo_obj, kobj)
38
39
40struct foo_attribute {
41 struct attribute attr;
42 ssize_t (*show)(struct foo_obj *foo, struct foo_attribute *attr, char *buf);
43 ssize_t (*store)(struct foo_obj *foo, struct foo_attribute *attr, const char *buf, size_t count);
44};
45#define to_foo_attr(x) container_of(x, struct foo_attribute, attr)
46
47
48
49
50
51
52
53
54static ssize_t foo_attr_show(struct kobject *kobj,
55 struct attribute *attr,
56 char *buf)
57{
58 struct foo_attribute *attribute;
59 struct foo_obj *foo;
60
61 attribute = to_foo_attr(attr);
62 foo = to_foo_obj(kobj);
63
64 if (!attribute->show)
65 return -EIO;
66
67 return attribute->show(foo, attribute, buf);
68}
69
70
71
72
73
74static ssize_t foo_attr_store(struct kobject *kobj,
75 struct attribute *attr,
76 const char *buf, size_t len)
77{
78 struct foo_attribute *attribute;
79 struct foo_obj *foo;
80
81 attribute = to_foo_attr(attr);
82 foo = to_foo_obj(kobj);
83
84 if (!attribute->store)
85 return -EIO;
86
87 return attribute->store(foo, attribute, buf, len);
88}
89
90
91static const struct sysfs_ops foo_sysfs_ops = {
92 .show = foo_attr_show,
93 .store = foo_attr_store,
94};
95
96
97
98
99
100
101
102
103static void foo_release(struct kobject *kobj)
104{
105 struct foo_obj *foo;
106
107 foo = to_foo_obj(kobj);
108 kfree(foo);
109}
110
111
112
113
114static ssize_t foo_show(struct foo_obj *foo_obj, struct foo_attribute *attr,
115 char *buf)
116{
117 return sprintf(buf, "%d\n", foo_obj->foo);
118}
119
120static ssize_t foo_store(struct foo_obj *foo_obj, struct foo_attribute *attr,
121 const char *buf, size_t count)
122{
123 int ret;
124
125 ret = kstrtoint(buf, 10, &foo_obj->foo);
126 if (ret < 0)
127 return ret;
128
129 return count;
130}
131
132
133static struct foo_attribute foo_attribute =
134 __ATTR(foo, 0664, foo_show, foo_store);
135
136
137
138
139
140static ssize_t b_show(struct foo_obj *foo_obj, struct foo_attribute *attr,
141 char *buf)
142{
143 int var;
144
145 if (strcmp(attr->attr.name, "baz") == 0)
146 var = foo_obj->baz;
147 else
148 var = foo_obj->bar;
149 return sprintf(buf, "%d\n", var);
150}
151
152static ssize_t b_store(struct foo_obj *foo_obj, struct foo_attribute *attr,
153 const char *buf, size_t count)
154{
155 int var, ret;
156
157 ret = kstrtoint(buf, 10, &var);
158 if (ret < 0)
159 return ret;
160
161 if (strcmp(attr->attr.name, "baz") == 0)
162 foo_obj->baz = var;
163 else
164 foo_obj->bar = var;
165 return count;
166}
167
168static struct foo_attribute baz_attribute =
169 __ATTR(baz, 0664, b_show, b_store);
170static struct foo_attribute bar_attribute =
171 __ATTR(bar, 0664, b_show, b_store);
172
173
174
175
176
177static struct attribute *foo_default_attrs[] = {
178 &foo_attribute.attr,
179 &baz_attribute.attr,
180 &bar_attribute.attr,
181 NULL,
182};
183
184
185
186
187
188
189static struct kobj_type foo_ktype = {
190 .sysfs_ops = &foo_sysfs_ops,
191 .release = foo_release,
192 .default_attrs = foo_default_attrs,
193};
194
195static struct kset *example_kset;
196static struct foo_obj *foo_obj;
197static struct foo_obj *bar_obj;
198static struct foo_obj *baz_obj;
199
200static struct foo_obj *create_foo_obj(const char *name)
201{
202 struct foo_obj *foo;
203 int retval;
204
205
206 foo = kzalloc(sizeof(*foo), GFP_KERNEL);
207 if (!foo)
208 return NULL;
209
210
211
212
213
214 foo->kobj.kset = example_kset;
215
216
217
218
219
220
221
222 retval = kobject_init_and_add(&foo->kobj, &foo_ktype, NULL, "%s", name);
223 if (retval) {
224 kobject_put(&foo->kobj);
225 return NULL;
226 }
227
228
229
230
231
232 kobject_uevent(&foo->kobj, KOBJ_ADD);
233
234 return foo;
235}
236
237static void destroy_foo_obj(struct foo_obj *foo)
238{
239 kobject_put(&foo->kobj);
240}
241
242static int __init example_init(void)
243{
244
245
246
247
248 example_kset = kset_create_and_add("kset_example", NULL, kernel_kobj);
249 if (!example_kset)
250 return -ENOMEM;
251
252
253
254
255 foo_obj = create_foo_obj("foo");
256 if (!foo_obj)
257 goto foo_error;
258
259 bar_obj = create_foo_obj("bar");
260 if (!bar_obj)
261 goto bar_error;
262
263 baz_obj = create_foo_obj("baz");
264 if (!baz_obj)
265 goto baz_error;
266
267 return 0;
268
269baz_error:
270 destroy_foo_obj(bar_obj);
271bar_error:
272 destroy_foo_obj(foo_obj);
273foo_error:
274 kset_unregister(example_kset);
275 return -EINVAL;
276}
277
278static void __exit example_exit(void)
279{
280 destroy_foo_obj(baz_obj);
281 destroy_foo_obj(bar_obj);
282 destroy_foo_obj(foo_obj);
283 kset_unregister(example_kset);
284}
285
286module_init(example_init);
287module_exit(example_exit);
288MODULE_LICENSE("GPL v2");
289MODULE_AUTHOR("Greg Kroah-Hartman <greg@kroah.com>");
290