blob: 401328fd687d7d694f6a1270657ae5a10ae5a493 [file] [log] [blame]
Greg Kroah-Hartmand9d16e12017-11-07 17:30:05 +01001// SPDX-License-Identifier: GPL-2.0
Greg Kroah-Hartman44bfe162007-11-27 11:28:26 -08002/*
3 * Sample kset and ktype implementation
4 *
5 * Copyright (C) 2004-2007 Greg Kroah-Hartman <greg@kroah.com>
6 * Copyright (C) 2007 Novell Inc.
Greg Kroah-Hartman44bfe162007-11-27 11:28:26 -08007 */
8#include <linux/kobject.h>
9#include <linux/string.h>
10#include <linux/sysfs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/slab.h>
Greg Kroah-Hartman44bfe162007-11-27 11:28:26 -080012#include <linux/module.h>
13#include <linux/init.h>
14
15/*
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
21 * read out of it.
22 */
23
24
25/*
26 * This is our "object" that we will create a few of and register them with
27 * sysfs.
28 */
29struct foo_obj {
30 struct kobject kobj;
31 int foo;
32 int baz;
33 int bar;
34};
35#define to_foo_obj(x) container_of(x, struct foo_obj, kobj)
36
37/* a custom attribute that works just for a struct foo_obj. */
38struct 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);
42};
43#define to_foo_attr(x) container_of(x, struct foo_attribute, attr)
44
45/*
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.
51 */
52static ssize_t foo_attr_show(struct kobject *kobj,
53 struct attribute *attr,
54 char *buf)
55{
56 struct foo_attribute *attribute;
57 struct foo_obj *foo;
58
59 attribute = to_foo_attr(attr);
60 foo = to_foo_obj(kobj);
61
62 if (!attribute->show)
63 return -EIO;
64
65 return attribute->show(foo, attribute, buf);
66}
67
68/*
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.)
71 */
72static ssize_t foo_attr_store(struct kobject *kobj,
73 struct attribute *attr,
74 const char *buf, size_t len)
75{
76 struct foo_attribute *attribute;
77 struct foo_obj *foo;
78
79 attribute = to_foo_attr(attr);
80 foo = to_foo_obj(kobj);
81
82 if (!attribute->store)
83 return -EIO;
84
85 return attribute->store(foo, attribute, buf, len);
86}
87
88/* Our custom sysfs_ops that we will associate with our ktype later on */
Emese Revfy52cf25d2010-01-19 02:58:23 +010089static const struct sysfs_ops foo_sysfs_ops = {
Greg Kroah-Hartman44bfe162007-11-27 11:28:26 -080090 .show = foo_attr_show,
91 .store = foo_attr_store,
92};
93
94/*
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.
97 *
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...
100 */
101static void foo_release(struct kobject *kobj)
102{
103 struct foo_obj *foo;
104
105 foo = to_foo_obj(kobj);
106 kfree(foo);
107}
108
109/*
110 * The "foo" file where the .foo variable is read from and written to.
111 */
112static ssize_t foo_show(struct foo_obj *foo_obj, struct foo_attribute *attr,
113 char *buf)
114{
115 return sprintf(buf, "%d\n", foo_obj->foo);
116}
117
118static ssize_t foo_store(struct foo_obj *foo_obj, struct foo_attribute *attr,
119 const char *buf, size_t count)
120{
Rastislav Barlik5fd637e2014-12-17 21:14:48 +0100121 int ret;
122
123 ret = kstrtoint(buf, 10, &foo_obj->foo);
124 if (ret < 0)
125 return ret;
126
Greg Kroah-Hartman44bfe162007-11-27 11:28:26 -0800127 return count;
128}
129
Rusty Russellde510982014-05-14 10:33:50 +0930130/* Sysfs attributes cannot be world-writable. */
Greg Kroah-Hartman44bfe162007-11-27 11:28:26 -0800131static struct foo_attribute foo_attribute =
Rusty Russellde510982014-05-14 10:33:50 +0930132 __ATTR(foo, 0664, foo_show, foo_store);
Greg Kroah-Hartman44bfe162007-11-27 11:28:26 -0800133
134/*
Radu Voicilas20ef9f42009-12-12 01:06:10 -0800135 * More complex function where we determine which variable is being accessed by
Greg Kroah-Hartman44bfe162007-11-27 11:28:26 -0800136 * looking at the attribute for the "baz" and "bar" files.
137 */
138static ssize_t b_show(struct foo_obj *foo_obj, struct foo_attribute *attr,
139 char *buf)
140{
141 int var;
142
143 if (strcmp(attr->attr.name, "baz") == 0)
144 var = foo_obj->baz;
145 else
146 var = foo_obj->bar;
147 return sprintf(buf, "%d\n", var);
148}
149
150static ssize_t b_store(struct foo_obj *foo_obj, struct foo_attribute *attr,
151 const char *buf, size_t count)
152{
Rastislav Barlik5fd637e2014-12-17 21:14:48 +0100153 int var, ret;
Greg Kroah-Hartman44bfe162007-11-27 11:28:26 -0800154
Rastislav Barlik5fd637e2014-12-17 21:14:48 +0100155 ret = kstrtoint(buf, 10, &var);
156 if (ret < 0)
157 return ret;
158
Greg Kroah-Hartman44bfe162007-11-27 11:28:26 -0800159 if (strcmp(attr->attr.name, "baz") == 0)
160 foo_obj->baz = var;
161 else
162 foo_obj->bar = var;
163 return count;
164}
165
166static struct foo_attribute baz_attribute =
Rusty Russellde510982014-05-14 10:33:50 +0930167 __ATTR(baz, 0664, b_show, b_store);
Greg Kroah-Hartman44bfe162007-11-27 11:28:26 -0800168static struct foo_attribute bar_attribute =
Rusty Russellde510982014-05-14 10:33:50 +0930169 __ATTR(bar, 0664, b_show, b_store);
Greg Kroah-Hartman44bfe162007-11-27 11:28:26 -0800170
171/*
Radu Voicilas20ef9f42009-12-12 01:06:10 -0800172 * Create a group of attributes so that we can create and destroy them all
Greg Kroah-Hartman44bfe162007-11-27 11:28:26 -0800173 * at once.
174 */
175static struct attribute *foo_default_attrs[] = {
176 &foo_attribute.attr,
177 &baz_attribute.attr,
178 &bar_attribute.attr,
179 NULL, /* need to NULL terminate the list of attributes */
180};
181
182/*
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.
186 */
187static struct kobj_type foo_ktype = {
188 .sysfs_ops = &foo_sysfs_ops,
189 .release = foo_release,
190 .default_attrs = foo_default_attrs,
191};
192
193static struct kset *example_kset;
194static struct foo_obj *foo_obj;
195static struct foo_obj *bar_obj;
196static struct foo_obj *baz_obj;
197
198static struct foo_obj *create_foo_obj(const char *name)
199{
200 struct foo_obj *foo;
201 int retval;
202
203 /* allocate the memory for the whole object */
204 foo = kzalloc(sizeof(*foo), GFP_KERNEL);
205 if (!foo)
206 return NULL;
207
208 /*
209 * As we have a kset for this kobject, we need to set it before calling
210 * the kobject core.
211 */
212 foo->kobj.kset = example_kset;
213
214 /*
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.
219 */
220 retval = kobject_init_and_add(&foo->kobj, &foo_ktype, NULL, "%s", name);
221 if (retval) {
Li Zefan185000f2008-06-13 11:09:16 +0800222 kobject_put(&foo->kobj);
Greg Kroah-Hartman44bfe162007-11-27 11:28:26 -0800223 return NULL;
224 }
225
226 /*
227 * We are always responsible for sending the uevent that the kobject
228 * was added to the system.
229 */
230 kobject_uevent(&foo->kobj, KOBJ_ADD);
231
232 return foo;
233}
234
235static void destroy_foo_obj(struct foo_obj *foo)
236{
237 kobject_put(&foo->kobj);
238}
239
Qinghuang Feng7ec7fb32009-01-06 14:40:52 -0800240static int __init example_init(void)
Greg Kroah-Hartman44bfe162007-11-27 11:28:26 -0800241{
242 /*
243 * Create a kset with the name of "kset_example",
244 * located under /sys/kernel/
245 */
246 example_kset = kset_create_and_add("kset_example", NULL, kernel_kobj);
247 if (!example_kset)
248 return -ENOMEM;
249
250 /*
251 * Create three objects and register them with our kset
252 */
253 foo_obj = create_foo_obj("foo");
254 if (!foo_obj)
255 goto foo_error;
256
257 bar_obj = create_foo_obj("bar");
258 if (!bar_obj)
259 goto bar_error;
260
261 baz_obj = create_foo_obj("baz");
262 if (!baz_obj)
263 goto baz_error;
264
265 return 0;
266
267baz_error:
268 destroy_foo_obj(bar_obj);
269bar_error:
270 destroy_foo_obj(foo_obj);
271foo_error:
Bjorn Helgaase756bc52013-10-08 14:20:24 -0600272 kset_unregister(example_kset);
Greg Kroah-Hartman44bfe162007-11-27 11:28:26 -0800273 return -EINVAL;
274}
275
Qinghuang Feng7ec7fb32009-01-06 14:40:52 -0800276static void __exit example_exit(void)
Greg Kroah-Hartman44bfe162007-11-27 11:28:26 -0800277{
278 destroy_foo_obj(baz_obj);
279 destroy_foo_obj(bar_obj);
280 destroy_foo_obj(foo_obj);
281 kset_unregister(example_kset);
282}
283
284module_init(example_init);
285module_exit(example_exit);
Greg Kroah-Hartman07afb6ac2015-03-25 13:41:42 +0100286MODULE_LICENSE("GPL v2");
Greg Kroah-Hartman44bfe162007-11-27 11:28:26 -0800287MODULE_AUTHOR("Greg Kroah-Hartman <greg@kroah.com>");