blob: ab5e447ec23871b66b46049129dfaafc6f3f287d [file] [log] [blame]
Greg Kroah-Hartman44bfe162007-11-27 11:28:26 -08001/*
2 * Sample kset and ktype implementation
3 *
4 * Copyright (C) 2004-2007 Greg Kroah-Hartman <greg@kroah.com>
5 * Copyright (C) 2007 Novell Inc.
6 *
7 * Released under the GPL version 2 only.
8 *
9 */
10#include <linux/kobject.h>
11#include <linux/string.h>
12#include <linux/sysfs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Greg Kroah-Hartman44bfe162007-11-27 11:28:26 -080014#include <linux/module.h>
15#include <linux/init.h>
16
17/*
18 * This module shows how to create a kset in sysfs called
19 * /sys/kernel/kset-example
20 * Then tree kobjects are created and assigned to this kset, "foo", "baz",
21 * and "bar". In those kobjects, attributes of the same name are also
22 * created and if an integer is written to these files, it can be later
23 * read out of it.
24 */
25
26
27/*
28 * This is our "object" that we will create a few of and register them with
29 * sysfs.
30 */
31struct foo_obj {
32 struct kobject kobj;
33 int foo;
34 int baz;
35 int bar;
36};
37#define to_foo_obj(x) container_of(x, struct foo_obj, kobj)
38
39/* a custom attribute that works just for a struct foo_obj. */
40struct foo_attribute {
41 struct attribute attr;
42 ssize_t (*show)(struct foo_obj *foo, struct foo_attribute *attr, char *buf);
43 ssize_t (*store)(struct foo_obj *foo, struct foo_attribute *attr, const char *buf, size_t count);
44};
45#define to_foo_attr(x) container_of(x, struct foo_attribute, attr)
46
47/*
48 * The default show function that must be passed to sysfs. This will be
49 * called by sysfs for whenever a show function is called by the user on a
50 * sysfs file associated with the kobjects we have registered. We need to
51 * transpose back from a "default" kobject to our custom struct foo_obj and
52 * then call the show function for that specific object.
53 */
54static ssize_t foo_attr_show(struct kobject *kobj,
55 struct attribute *attr,
56 char *buf)
57{
58 struct foo_attribute *attribute;
59 struct foo_obj *foo;
60
61 attribute = to_foo_attr(attr);
62 foo = to_foo_obj(kobj);
63
64 if (!attribute->show)
65 return -EIO;
66
67 return attribute->show(foo, attribute, buf);
68}
69
70/*
71 * Just like the default show function above, but this one is for when the
72 * sysfs "store" is requested (when a value is written to a file.)
73 */
74static ssize_t foo_attr_store(struct kobject *kobj,
75 struct attribute *attr,
76 const char *buf, size_t len)
77{
78 struct foo_attribute *attribute;
79 struct foo_obj *foo;
80
81 attribute = to_foo_attr(attr);
82 foo = to_foo_obj(kobj);
83
84 if (!attribute->store)
85 return -EIO;
86
87 return attribute->store(foo, attribute, buf, len);
88}
89
90/* Our custom sysfs_ops that we will associate with our ktype later on */
Emese Revfy52cf25d2010-01-19 02:58:23 +010091static const struct sysfs_ops foo_sysfs_ops = {
Greg Kroah-Hartman44bfe162007-11-27 11:28:26 -080092 .show = foo_attr_show,
93 .store = foo_attr_store,
94};
95
96/*
97 * The release function for our object. This is REQUIRED by the kernel to
98 * have. We free the memory held in our object here.
99 *
100 * NEVER try to get away with just a "blank" release function to try to be
101 * smarter than the kernel. Turns out, no one ever is...
102 */
103static void foo_release(struct kobject *kobj)
104{
105 struct foo_obj *foo;
106
107 foo = to_foo_obj(kobj);
108 kfree(foo);
109}
110
111/*
112 * The "foo" file where the .foo variable is read from and written to.
113 */
114static ssize_t foo_show(struct foo_obj *foo_obj, struct foo_attribute *attr,
115 char *buf)
116{
117 return sprintf(buf, "%d\n", foo_obj->foo);
118}
119
120static ssize_t foo_store(struct foo_obj *foo_obj, struct foo_attribute *attr,
121 const char *buf, size_t count)
122{
123 sscanf(buf, "%du", &foo_obj->foo);
124 return count;
125}
126
Rusty Russellde510982014-05-14 10:33:50 +0930127/* Sysfs attributes cannot be world-writable. */
Greg Kroah-Hartman44bfe162007-11-27 11:28:26 -0800128static struct foo_attribute foo_attribute =
Rusty Russellde510982014-05-14 10:33:50 +0930129 __ATTR(foo, 0664, foo_show, foo_store);
Greg Kroah-Hartman44bfe162007-11-27 11:28:26 -0800130
131/*
Radu Voicilas20ef9f42009-12-12 01:06:10 -0800132 * More complex function where we determine which variable is being accessed by
Greg Kroah-Hartman44bfe162007-11-27 11:28:26 -0800133 * looking at the attribute for the "baz" and "bar" files.
134 */
135static ssize_t b_show(struct foo_obj *foo_obj, struct foo_attribute *attr,
136 char *buf)
137{
138 int var;
139
140 if (strcmp(attr->attr.name, "baz") == 0)
141 var = foo_obj->baz;
142 else
143 var = foo_obj->bar;
144 return sprintf(buf, "%d\n", var);
145}
146
147static ssize_t b_store(struct foo_obj *foo_obj, struct foo_attribute *attr,
148 const char *buf, size_t count)
149{
150 int var;
151
152 sscanf(buf, "%du", &var);
153 if (strcmp(attr->attr.name, "baz") == 0)
154 foo_obj->baz = var;
155 else
156 foo_obj->bar = var;
157 return count;
158}
159
160static struct foo_attribute baz_attribute =
Rusty Russellde510982014-05-14 10:33:50 +0930161 __ATTR(baz, 0664, b_show, b_store);
Greg Kroah-Hartman44bfe162007-11-27 11:28:26 -0800162static struct foo_attribute bar_attribute =
Rusty Russellde510982014-05-14 10:33:50 +0930163 __ATTR(bar, 0664, b_show, b_store);
Greg Kroah-Hartman44bfe162007-11-27 11:28:26 -0800164
165/*
Radu Voicilas20ef9f42009-12-12 01:06:10 -0800166 * Create a group of attributes so that we can create and destroy them all
Greg Kroah-Hartman44bfe162007-11-27 11:28:26 -0800167 * at once.
168 */
169static struct attribute *foo_default_attrs[] = {
170 &foo_attribute.attr,
171 &baz_attribute.attr,
172 &bar_attribute.attr,
173 NULL, /* need to NULL terminate the list of attributes */
174};
175
176/*
177 * Our own ktype for our kobjects. Here we specify our sysfs ops, the
178 * release function, and the set of default attributes we want created
179 * whenever a kobject of this type is registered with the kernel.
180 */
181static struct kobj_type foo_ktype = {
182 .sysfs_ops = &foo_sysfs_ops,
183 .release = foo_release,
184 .default_attrs = foo_default_attrs,
185};
186
187static struct kset *example_kset;
188static struct foo_obj *foo_obj;
189static struct foo_obj *bar_obj;
190static struct foo_obj *baz_obj;
191
192static struct foo_obj *create_foo_obj(const char *name)
193{
194 struct foo_obj *foo;
195 int retval;
196
197 /* allocate the memory for the whole object */
198 foo = kzalloc(sizeof(*foo), GFP_KERNEL);
199 if (!foo)
200 return NULL;
201
202 /*
203 * As we have a kset for this kobject, we need to set it before calling
204 * the kobject core.
205 */
206 foo->kobj.kset = example_kset;
207
208 /*
209 * Initialize and add the kobject to the kernel. All the default files
210 * will be created here. As we have already specified a kset for this
211 * kobject, we don't have to set a parent for the kobject, the kobject
212 * will be placed beneath that kset automatically.
213 */
214 retval = kobject_init_and_add(&foo->kobj, &foo_ktype, NULL, "%s", name);
215 if (retval) {
Li Zefan185000f2008-06-13 11:09:16 +0800216 kobject_put(&foo->kobj);
Greg Kroah-Hartman44bfe162007-11-27 11:28:26 -0800217 return NULL;
218 }
219
220 /*
221 * We are always responsible for sending the uevent that the kobject
222 * was added to the system.
223 */
224 kobject_uevent(&foo->kobj, KOBJ_ADD);
225
226 return foo;
227}
228
229static void destroy_foo_obj(struct foo_obj *foo)
230{
231 kobject_put(&foo->kobj);
232}
233
Qinghuang Feng7ec7fb32009-01-06 14:40:52 -0800234static int __init example_init(void)
Greg Kroah-Hartman44bfe162007-11-27 11:28:26 -0800235{
236 /*
237 * Create a kset with the name of "kset_example",
238 * located under /sys/kernel/
239 */
240 example_kset = kset_create_and_add("kset_example", NULL, kernel_kobj);
241 if (!example_kset)
242 return -ENOMEM;
243
244 /*
245 * Create three objects and register them with our kset
246 */
247 foo_obj = create_foo_obj("foo");
248 if (!foo_obj)
249 goto foo_error;
250
251 bar_obj = create_foo_obj("bar");
252 if (!bar_obj)
253 goto bar_error;
254
255 baz_obj = create_foo_obj("baz");
256 if (!baz_obj)
257 goto baz_error;
258
259 return 0;
260
261baz_error:
262 destroy_foo_obj(bar_obj);
263bar_error:
264 destroy_foo_obj(foo_obj);
265foo_error:
Bjorn Helgaase756bc52013-10-08 14:20:24 -0600266 kset_unregister(example_kset);
Greg Kroah-Hartman44bfe162007-11-27 11:28:26 -0800267 return -EINVAL;
268}
269
Qinghuang Feng7ec7fb32009-01-06 14:40:52 -0800270static void __exit example_exit(void)
Greg Kroah-Hartman44bfe162007-11-27 11:28:26 -0800271{
272 destroy_foo_obj(baz_obj);
273 destroy_foo_obj(bar_obj);
274 destroy_foo_obj(foo_obj);
275 kset_unregister(example_kset);
276}
277
278module_init(example_init);
279module_exit(example_exit);
280MODULE_LICENSE("GPL");
281MODULE_AUTHOR("Greg Kroah-Hartman <greg@kroah.com>");