1 // SPDX-License-Identifier: GPL-2.0
3 * Sample kset and ktype implementation
5 * Copyright (C) 2004-2007 Greg Kroah-Hartman <greg@kroah.com>
6 * Copyright (C) 2007 Novell Inc.
8 #include <linux/kobject.h>
9 #include <linux/string.h>
10 #include <linux/sysfs.h>
11 #include <linux/slab.h>
12 #include <linux/module.h>
13 #include <linux/init.h>
16 * This module shows how to create a kset in sysfs called
17 * /sys/kernel/kset-example
18 * Then tree kobjects are created and assigned to this kset, "foo", "baz",
19 * and "bar". In those kobjects, attributes of the same name are also
20 * created and if an integer is written to these files, it can be later
26 * This is our "object" that we will create a few of and register them with
35 #define to_foo_obj(x) container_of(x, struct foo_obj, kobj)
37 /* a custom attribute that works just for a struct foo_obj. */
38 struct foo_attribute {
39 struct attribute attr;
40 ssize_t (*show)(struct foo_obj *foo, struct foo_attribute *attr, char *buf);
41 ssize_t (*store)(struct foo_obj *foo, struct foo_attribute *attr, const char *buf, size_t count);
43 #define to_foo_attr(x) container_of(x, struct foo_attribute, attr)
46 * The default show function that must be passed to sysfs. This will be
47 * called by sysfs for whenever a show function is called by the user on a
48 * sysfs file associated with the kobjects we have registered. We need to
49 * transpose back from a "default" kobject to our custom struct foo_obj and
50 * then call the show function for that specific object.
52 static ssize_t foo_attr_show(struct kobject *kobj,
53 struct attribute *attr,
56 struct foo_attribute *attribute;
59 attribute = to_foo_attr(attr);
60 foo = to_foo_obj(kobj);
65 return attribute->show(foo, attribute, buf);
69 * Just like the default show function above, but this one is for when the
70 * sysfs "store" is requested (when a value is written to a file.)
72 static ssize_t foo_attr_store(struct kobject *kobj,
73 struct attribute *attr,
74 const char *buf, size_t len)
76 struct foo_attribute *attribute;
79 attribute = to_foo_attr(attr);
80 foo = to_foo_obj(kobj);
82 if (!attribute->store)
85 return attribute->store(foo, attribute, buf, len);
88 /* Our custom sysfs_ops that we will associate with our ktype later on */
89 static const struct sysfs_ops foo_sysfs_ops = {
90 .show = foo_attr_show,
91 .store = foo_attr_store,
95 * The release function for our object. This is REQUIRED by the kernel to
96 * have. We free the memory held in our object here.
98 * NEVER try to get away with just a "blank" release function to try to be
99 * smarter than the kernel. Turns out, no one ever is...
101 static void foo_release(struct kobject *kobj)
105 foo = to_foo_obj(kobj);
110 * The "foo" file where the .foo variable is read from and written to.
112 static ssize_t foo_show(struct foo_obj *foo_obj, struct foo_attribute *attr,
115 return sprintf(buf, "%d\n", foo_obj->foo);
118 static ssize_t foo_store(struct foo_obj *foo_obj, struct foo_attribute *attr,
119 const char *buf, size_t count)
123 ret = kstrtoint(buf, 10, &foo_obj->foo);
130 /* Sysfs attributes cannot be world-writable. */
131 static struct foo_attribute foo_attribute =
132 __ATTR(foo, 0664, foo_show, foo_store);
135 * More complex function where we determine which variable is being accessed by
136 * looking at the attribute for the "baz" and "bar" files.
138 static ssize_t b_show(struct foo_obj *foo_obj, struct foo_attribute *attr,
143 if (strcmp(attr->attr.name, "baz") == 0)
147 return sprintf(buf, "%d\n", var);
150 static ssize_t b_store(struct foo_obj *foo_obj, struct foo_attribute *attr,
151 const char *buf, size_t count)
155 ret = kstrtoint(buf, 10, &var);
159 if (strcmp(attr->attr.name, "baz") == 0)
166 static struct foo_attribute baz_attribute =
167 __ATTR(baz, 0664, b_show, b_store);
168 static struct foo_attribute bar_attribute =
169 __ATTR(bar, 0664, b_show, b_store);
172 * Create a group of attributes so that we can create and destroy them all
175 static struct attribute *foo_default_attrs[] = {
179 NULL, /* need to NULL terminate the list of attributes */
183 * Our own ktype for our kobjects. Here we specify our sysfs ops, the
184 * release function, and the set of default attributes we want created
185 * whenever a kobject of this type is registered with the kernel.
187 static struct kobj_type foo_ktype = {
188 .sysfs_ops = &foo_sysfs_ops,
189 .release = foo_release,
190 .default_attrs = foo_default_attrs,
193 static struct kset *example_kset;
194 static struct foo_obj *foo_obj;
195 static struct foo_obj *bar_obj;
196 static struct foo_obj *baz_obj;
198 static struct foo_obj *create_foo_obj(const char *name)
203 /* allocate the memory for the whole object */
204 foo = kzalloc(sizeof(*foo), GFP_KERNEL);
209 * As we have a kset for this kobject, we need to set it before calling
212 foo->kobj.kset = example_kset;
215 * Initialize and add the kobject to the kernel. All the default files
216 * will be created here. As we have already specified a kset for this
217 * kobject, we don't have to set a parent for the kobject, the kobject
218 * will be placed beneath that kset automatically.
220 retval = kobject_init_and_add(&foo->kobj, &foo_ktype, NULL, "%s", name);
222 kobject_put(&foo->kobj);
227 * We are always responsible for sending the uevent that the kobject
228 * was added to the system.
230 kobject_uevent(&foo->kobj, KOBJ_ADD);
235 static void destroy_foo_obj(struct foo_obj *foo)
237 kobject_put(&foo->kobj);
240 static int __init example_init(void)
243 * Create a kset with the name of "kset_example",
244 * located under /sys/kernel/
246 example_kset = kset_create_and_add("kset_example", NULL, kernel_kobj);
251 * Create three objects and register them with our kset
253 foo_obj = create_foo_obj("foo");
257 bar_obj = create_foo_obj("bar");
261 baz_obj = create_foo_obj("baz");
268 destroy_foo_obj(bar_obj);
270 destroy_foo_obj(foo_obj);
272 kset_unregister(example_kset);
276 static void __exit example_exit(void)
278 destroy_foo_obj(baz_obj);
279 destroy_foo_obj(bar_obj);
280 destroy_foo_obj(foo_obj);
281 kset_unregister(example_kset);
284 module_init(example_init);
285 module_exit(example_exit);
286 MODULE_LICENSE("GPL v2");
287 MODULE_AUTHOR("Greg Kroah-Hartman <greg@kroah.com>");