2 * Sample kset and ktype implementation
4 * Copyright (C) 2004-2007 Greg Kroah-Hartman <greg@kroah.com>
5 * Copyright (C) 2007 Novell Inc.
7 * Released under the GPL version 2 only.
10 #include <linux/kobject.h>
11 #include <linux/string.h>
12 #include <linux/sysfs.h>
13 #include <linux/module.h>
14 #include <linux/init.h>
17 * This module shows how to create a kset in sysfs called
18 * /sys/kernel/kset-example
19 * Then tree kobjects are created and assigned to this kset, "foo", "baz",
20 * and "bar". In those kobjects, attributes of the same name are also
21 * created and if an integer is written to these files, it can be later
27 * This is our "object" that we will create a few of and register them with
36 #define to_foo_obj(x) container_of(x, struct foo_obj, kobj)
38 /* a custom attribute that works just for a struct foo_obj. */
39 struct foo_attribute
{
40 struct attribute attr
;
41 ssize_t (*show
)(struct foo_obj
*foo
, struct foo_attribute
*attr
, char *buf
);
42 ssize_t (*store
)(struct foo_obj
*foo
, struct foo_attribute
*attr
, const char *buf
, size_t count
);
44 #define to_foo_attr(x) container_of(x, struct foo_attribute, attr)
47 * The default show function that must be passed to sysfs. This will be
48 * called by sysfs for whenever a show function is called by the user on a
49 * sysfs file associated with the kobjects we have registered. We need to
50 * transpose back from a "default" kobject to our custom struct foo_obj and
51 * then call the show function for that specific object.
53 static ssize_t
foo_attr_show(struct kobject
*kobj
,
54 struct attribute
*attr
,
57 struct foo_attribute
*attribute
;
60 attribute
= to_foo_attr(attr
);
61 foo
= to_foo_obj(kobj
);
66 return attribute
->show(foo
, attribute
, buf
);
70 * Just like the default show function above, but this one is for when the
71 * sysfs "store" is requested (when a value is written to a file.)
73 static ssize_t
foo_attr_store(struct kobject
*kobj
,
74 struct attribute
*attr
,
75 const char *buf
, size_t len
)
77 struct foo_attribute
*attribute
;
80 attribute
= to_foo_attr(attr
);
81 foo
= to_foo_obj(kobj
);
83 if (!attribute
->store
)
86 return attribute
->store(foo
, attribute
, buf
, len
);
89 /* Our custom sysfs_ops that we will associate with our ktype later on */
90 static struct sysfs_ops foo_sysfs_ops
= {
91 .show
= foo_attr_show
,
92 .store
= foo_attr_store
,
96 * The release function for our object. This is REQUIRED by the kernel to
97 * have. We free the memory held in our object here.
99 * NEVER try to get away with just a "blank" release function to try to be
100 * smarter than the kernel. Turns out, no one ever is...
102 static void foo_release(struct kobject
*kobj
)
106 foo
= to_foo_obj(kobj
);
111 * The "foo" file where the .foo variable is read from and written to.
113 static ssize_t
foo_show(struct foo_obj
*foo_obj
, struct foo_attribute
*attr
,
116 return sprintf(buf
, "%d\n", foo_obj
->foo
);
119 static ssize_t
foo_store(struct foo_obj
*foo_obj
, struct foo_attribute
*attr
,
120 const char *buf
, size_t count
)
122 sscanf(buf
, "%du", &foo_obj
->foo
);
126 static struct foo_attribute foo_attribute
=
127 __ATTR(foo
, 0666, foo_show
, foo_store
);
130 * More complex function where we determine which varible is being accessed by
131 * looking at the attribute for the "baz" and "bar" files.
133 static ssize_t
b_show(struct foo_obj
*foo_obj
, struct foo_attribute
*attr
,
138 if (strcmp(attr
->attr
.name
, "baz") == 0)
142 return sprintf(buf
, "%d\n", var
);
145 static ssize_t
b_store(struct foo_obj
*foo_obj
, struct foo_attribute
*attr
,
146 const char *buf
, size_t count
)
150 sscanf(buf
, "%du", &var
);
151 if (strcmp(attr
->attr
.name
, "baz") == 0)
158 static struct foo_attribute baz_attribute
=
159 __ATTR(baz
, 0666, b_show
, b_store
);
160 static struct foo_attribute bar_attribute
=
161 __ATTR(bar
, 0666, b_show
, b_store
);
164 * Create a group of attributes so that we can create and destory them all
167 static struct attribute
*foo_default_attrs
[] = {
171 NULL
, /* need to NULL terminate the list of attributes */
175 * Our own ktype for our kobjects. Here we specify our sysfs ops, the
176 * release function, and the set of default attributes we want created
177 * whenever a kobject of this type is registered with the kernel.
179 static struct kobj_type foo_ktype
= {
180 .sysfs_ops
= &foo_sysfs_ops
,
181 .release
= foo_release
,
182 .default_attrs
= foo_default_attrs
,
185 static struct kset
*example_kset
;
186 static struct foo_obj
*foo_obj
;
187 static struct foo_obj
*bar_obj
;
188 static struct foo_obj
*baz_obj
;
190 static struct foo_obj
*create_foo_obj(const char *name
)
195 /* allocate the memory for the whole object */
196 foo
= kzalloc(sizeof(*foo
), GFP_KERNEL
);
201 * As we have a kset for this kobject, we need to set it before calling
204 foo
->kobj
.kset
= example_kset
;
207 * Initialize and add the kobject to the kernel. All the default files
208 * will be created here. As we have already specified a kset for this
209 * kobject, we don't have to set a parent for the kobject, the kobject
210 * will be placed beneath that kset automatically.
212 retval
= kobject_init_and_add(&foo
->kobj
, &foo_ktype
, NULL
, "%s", name
);
214 kobject_put(&foo
->kobj
);
219 * We are always responsible for sending the uevent that the kobject
220 * was added to the system.
222 kobject_uevent(&foo
->kobj
, KOBJ_ADD
);
227 static void destroy_foo_obj(struct foo_obj
*foo
)
229 kobject_put(&foo
->kobj
);
232 static int example_init(void)
235 * Create a kset with the name of "kset_example",
236 * located under /sys/kernel/
238 example_kset
= kset_create_and_add("kset_example", NULL
, kernel_kobj
);
243 * Create three objects and register them with our kset
245 foo_obj
= create_foo_obj("foo");
249 bar_obj
= create_foo_obj("bar");
253 baz_obj
= create_foo_obj("baz");
260 destroy_foo_obj(bar_obj
);
262 destroy_foo_obj(foo_obj
);
267 static void example_exit(void)
269 destroy_foo_obj(baz_obj
);
270 destroy_foo_obj(bar_obj
);
271 destroy_foo_obj(foo_obj
);
272 kset_unregister(example_kset
);
275 module_init(example_init
);
276 module_exit(example_exit
);
277 MODULE_LICENSE("GPL");
278 MODULE_AUTHOR("Greg Kroah-Hartman <greg@kroah.com>");