1 // SPDX-License-Identifier: GPL-2.0
3 * Sample kobject 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/module.h>
12 #include <linux/init.h>
15 * This module shows how to create a simple subdirectory in sysfs called
16 * /sys/kernel/kobject-example In that directory, 3 files are created:
17 * "foo", "baz", and "bar". If an integer is written to these files, it can be
18 * later read out of it.
26 * The "foo" file where a static variable is read from and written to.
28 static ssize_t foo_show(struct kobject *kobj, struct kobj_attribute *attr,
31 return sysfs_emit(buf, "%d\n", foo);
34 static ssize_t foo_store(struct kobject *kobj, struct kobj_attribute *attr,
35 const char *buf, size_t count)
39 ret = kstrtoint(buf, 10, &foo);
46 /* Sysfs attributes cannot be world-writable. */
47 static struct kobj_attribute foo_attribute =
48 __ATTR(foo, 0664, foo_show, foo_store);
51 * More complex function where we determine which variable is being accessed by
52 * looking at the attribute for the "baz" and "bar" files.
54 static ssize_t b_show(struct kobject *kobj, struct kobj_attribute *attr,
59 if (strcmp(attr->attr.name, "baz") == 0)
63 return sysfs_emit(buf, "%d\n", var);
66 static ssize_t b_store(struct kobject *kobj, struct kobj_attribute *attr,
67 const char *buf, size_t count)
71 ret = kstrtoint(buf, 10, &var);
75 if (strcmp(attr->attr.name, "baz") == 0)
82 static struct kobj_attribute baz_attribute =
83 __ATTR(baz, 0664, b_show, b_store);
84 static struct kobj_attribute bar_attribute =
85 __ATTR(bar, 0664, b_show, b_store);
89 * Create a group of attributes so that we can create and destroy them all
92 static struct attribute *attrs[] = {
96 NULL, /* need to NULL terminate the list of attributes */
100 * An unnamed attribute group will put all of the attributes directly in
101 * the kobject directory. If we specify a name, a subdirectory will be
102 * created for the attributes with the directory being the name of the
105 static struct attribute_group attr_group = {
109 static struct kobject *example_kobj;
111 static int __init example_init(void)
116 * Create a simple kobject with the name of "kobject_example",
117 * located under /sys/kernel/
119 * As this is a simple directory, no uevent will be sent to
120 * userspace. That is why this function should not be used for
121 * any type of dynamic kobjects, where the name and number are
122 * not known ahead of time.
124 example_kobj = kobject_create_and_add("kobject_example", kernel_kobj);
128 /* Create the files associated with this kobject */
129 retval = sysfs_create_group(example_kobj, &attr_group);
131 kobject_put(example_kobj);
136 static void __exit example_exit(void)
138 kobject_put(example_kobj);
141 module_init(example_init);
142 module_exit(example_exit);
143 MODULE_LICENSE("GPL v2");
144 MODULE_AUTHOR("Greg Kroah-Hartman <greg@kroah.com>");