11 #include <linux/string.h>
13 #include <linux/slab.h>
14 #include <linux/module.h>
37 #define to_foo_obj(x) container_of(x, struct foo_obj, kobj)
45 #define to_foo_attr(x) container_of(x, struct foo_attribute, attr)
67 return attribute->
show(foo, attribute, buf);
76 const char *
buf,
size_t len)
84 if (!attribute->
store)
87 return attribute->
store(foo, attribute, buf, len);
91 static const struct sysfs_ops foo_sysfs_ops = {
92 .show = foo_attr_show,
93 .store = foo_attr_store,
103 static void foo_release(
struct kobject *kobj)
121 const char *buf,
size_t count)
128 __ATTR(foo, 0666, foo_show, foo_store);
143 return sprintf(buf,
"%d\n", var);
146 static ssize_t b_store(
struct foo_obj *foo_obj,
struct foo_attribute *attr,
147 const char *buf,
size_t count)
159 static struct foo_attribute baz_attribute =
160 __ATTR(baz, 0666, b_show, b_store);
161 static struct foo_attribute bar_attribute =
162 __ATTR(bar, 0666, b_show, b_store);
168 static struct attribute *foo_default_attrs[] = {
181 .sysfs_ops = &foo_sysfs_ops,
182 .release = foo_release,
183 .default_attrs = foo_default_attrs,
186 static struct kset *example_kset;
187 static struct foo_obj *foo_obj;
188 static struct foo_obj *bar_obj;
189 static struct foo_obj *baz_obj;
191 static struct foo_obj *create_foo_obj(
const char *
name)
205 foo->
kobj.kset = example_kset;
228 static void destroy_foo_obj(
struct foo_obj *foo)
233 static int __init example_init(
void)
246 foo_obj = create_foo_obj(
"foo");
250 bar_obj = create_foo_obj(
"bar");
254 baz_obj = create_foo_obj(
"baz");
261 destroy_foo_obj(bar_obj);
263 destroy_foo_obj(foo_obj);
268 static void __exit example_exit(
void)
270 destroy_foo_obj(baz_obj);
271 destroy_foo_obj(bar_obj);
272 destroy_foo_obj(foo_obj);