1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Sample kobject implementation
4  *
5  * Copyright (C) 2004-2007 Greg Kroah-Hartman <[email protected]>
6  * Copyright (C) 2007 Novell Inc.
7  *
8  * Released under the GPL version 2 only.
9  *
10  */
11 #include <linux/kobject.h>
12 #include <linux/string.h>
13 #include <linux/sysfs.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
16 
17 /*
18  * This module shows how to create a simple subdirectory in sysfs called
19  * /sys/kernel/kobject-example  In that directory, 3 files are created:
20  * "foo", "baz", and "bar".  If an integer is written to these files, it can be
21  * later read out of it.
22  */
23 
24 static int foo;
25 static int baz;
26 static int bar;
27 
28 /*
29  * The "foo" file where a static variable is read from and written to.
30  */
31 static ssize_t foo_show(struct kobject *kobj, struct kobj_attribute *attr,
32 			char *buf)
33 {
34 	return sprintf(buf, "%d\n", foo);
35 }
36 
37 static ssize_t foo_store(struct kobject *kobj, struct kobj_attribute *attr,
38 			 const char *buf, size_t count)
39 {
40 	int ret;
41 
42 	ret = kstrtoint(buf, 10, &foo);
43 	if (ret < 0)
44 		return ret;
45 
46 	return count;
47 }
48 
49 /* Sysfs attributes cannot be world-writable. */
50 static struct kobj_attribute foo_attribute =
51 	__ATTR(foo, 0664, foo_show, foo_store);
52 
53 /*
54  * More complex function where we determine which variable is being accessed by
55  * looking at the attribute for the "baz" and "bar" files.
56  */
57 static ssize_t b_show(struct kobject *kobj, struct kobj_attribute *attr,
58 		      char *buf)
59 {
60 	int var;
61 
62 	if (strcmp(attr->attr.name, "baz") == 0)
63 		var = baz;
64 	else
65 		var = bar;
66 	return sprintf(buf, "%d\n", var);
67 }
68 
69 static ssize_t b_store(struct kobject *kobj, struct kobj_attribute *attr,
70 		       const char *buf, size_t count)
71 {
72 	int var, ret;
73 
74 	ret = kstrtoint(buf, 10, &var);
75 	if (ret < 0)
76 		return ret;
77 
78 	if (strcmp(attr->attr.name, "baz") == 0)
79 		baz = var;
80 	else
81 		bar = var;
82 	return count;
83 }
84 
85 static struct kobj_attribute baz_attribute =
86 	__ATTR(baz, 0664, b_show, b_store);
87 static struct kobj_attribute bar_attribute =
88 	__ATTR(bar, 0664, b_show, b_store);
89 
90 
91 /*
92  * Create a group of attributes so that we can create and destroy them all
93  * at once.
94  */
95 static struct attribute *attrs[] = {
96 	&foo_attribute.attr,
97 	&baz_attribute.attr,
98 	&bar_attribute.attr,
99 	NULL,	/* need to NULL terminate the list of attributes */
100 };
101 
102 /*
103  * An unnamed attribute group will put all of the attributes directly in
104  * the kobject directory.  If we specify a name, a subdirectory will be
105  * created for the attributes with the directory being the name of the
106  * attribute group.
107  */
108 static struct attribute_group attr_group = {
109 	.attrs = attrs,
110 };
111 
112 static struct kobject *example_kobj;
113 
114 static int __init example_init(void)
115 {
116 	int retval;
117 
118 	/*
119 	 * Create a simple kobject with the name of "kobject_example",
120 	 * located under /sys/kernel/
121 	 *
122 	 * As this is a simple directory, no uevent will be sent to
123 	 * userspace.  That is why this function should not be used for
124 	 * any type of dynamic kobjects, where the name and number are
125 	 * not known ahead of time.
126 	 */
127 	example_kobj = kobject_create_and_add("kobject_example", kernel_kobj);
128 	if (!example_kobj)
129 		return -ENOMEM;
130 
131 	/* Create the files associated with this kobject */
132 	retval = sysfs_create_group(example_kobj, &attr_group);
133 	if (retval)
134 		kobject_put(example_kobj);
135 
136 	return retval;
137 }
138 
139 static void __exit example_exit(void)
140 {
141 	kobject_put(example_kobj);
142 }
143 
144 module_init(example_init);
145 module_exit(example_exit);
146 MODULE_LICENSE("GPL v2");
147 MODULE_AUTHOR("Greg Kroah-Hartman <[email protected]>");
148