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