Linux Audio

Check our new training course

Loading...
v4.6
 
  1/*
  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>
 13#include <linux/slab.h>
 14#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 */
 91static const struct sysfs_ops foo_sysfs_ops = {
 92	.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	int ret;
124
125	ret = kstrtoint(buf, 10, &foo_obj->foo);
126	if (ret < 0)
127		return ret;
128
129	return count;
130}
131
132/* Sysfs attributes cannot be world-writable. */
133static struct foo_attribute foo_attribute =
134	__ATTR(foo, 0664, foo_show, foo_store);
135
136/*
137 * More complex function where we determine which variable is being accessed by
138 * looking at the attribute for the "baz" and "bar" files.
139 */
140static ssize_t b_show(struct foo_obj *foo_obj, struct foo_attribute *attr,
141		      char *buf)
142{
143	int var;
144
145	if (strcmp(attr->attr.name, "baz") == 0)
146		var = foo_obj->baz;
147	else
148		var = foo_obj->bar;
149	return sprintf(buf, "%d\n", var);
150}
151
152static ssize_t b_store(struct foo_obj *foo_obj, struct foo_attribute *attr,
153		       const char *buf, size_t count)
154{
155	int var, ret;
156
157	ret = kstrtoint(buf, 10, &var);
158	if (ret < 0)
159		return ret;
160
161	if (strcmp(attr->attr.name, "baz") == 0)
162		foo_obj->baz = var;
163	else
164		foo_obj->bar = var;
165	return count;
166}
167
168static struct foo_attribute baz_attribute =
169	__ATTR(baz, 0664, b_show, b_store);
170static struct foo_attribute bar_attribute =
171	__ATTR(bar, 0664, b_show, b_store);
172
173/*
174 * Create a group of attributes so that we can create and destroy them all
175 * at once.
176 */
177static struct attribute *foo_default_attrs[] = {
178	&foo_attribute.attr,
179	&baz_attribute.attr,
180	&bar_attribute.attr,
181	NULL,	/* need to NULL terminate the list of attributes */
182};
 
183
184/*
185 * Our own ktype for our kobjects.  Here we specify our sysfs ops, the
186 * release function, and the set of default attributes we want created
187 * whenever a kobject of this type is registered with the kernel.
188 */
189static struct kobj_type foo_ktype = {
190	.sysfs_ops = &foo_sysfs_ops,
191	.release = foo_release,
192	.default_attrs = foo_default_attrs,
193};
194
195static struct kset *example_kset;
196static struct foo_obj *foo_obj;
197static struct foo_obj *bar_obj;
198static struct foo_obj *baz_obj;
199
200static struct foo_obj *create_foo_obj(const char *name)
201{
202	struct foo_obj *foo;
203	int retval;
204
205	/* allocate the memory for the whole object */
206	foo = kzalloc(sizeof(*foo), GFP_KERNEL);
207	if (!foo)
208		return NULL;
209
210	/*
211	 * As we have a kset for this kobject, we need to set it before calling
212	 * the kobject core.
213	 */
214	foo->kobj.kset = example_kset;
215
216	/*
217	 * Initialize and add the kobject to the kernel.  All the default files
218	 * will be created here.  As we have already specified a kset for this
219	 * kobject, we don't have to set a parent for the kobject, the kobject
220	 * will be placed beneath that kset automatically.
221	 */
222	retval = kobject_init_and_add(&foo->kobj, &foo_ktype, NULL, "%s", name);
223	if (retval) {
224		kobject_put(&foo->kobj);
225		return NULL;
226	}
227
228	/*
229	 * We are always responsible for sending the uevent that the kobject
230	 * was added to the system.
231	 */
232	kobject_uevent(&foo->kobj, KOBJ_ADD);
233
234	return foo;
235}
236
237static void destroy_foo_obj(struct foo_obj *foo)
238{
239	kobject_put(&foo->kobj);
240}
241
242static int __init example_init(void)
243{
244	/*
245	 * Create a kset with the name of "kset_example",
246	 * located under /sys/kernel/
247	 */
248	example_kset = kset_create_and_add("kset_example", NULL, kernel_kobj);
249	if (!example_kset)
250		return -ENOMEM;
251
252	/*
253	 * Create three objects and register them with our kset
254	 */
255	foo_obj = create_foo_obj("foo");
256	if (!foo_obj)
257		goto foo_error;
258
259	bar_obj = create_foo_obj("bar");
260	if (!bar_obj)
261		goto bar_error;
262
263	baz_obj = create_foo_obj("baz");
264	if (!baz_obj)
265		goto baz_error;
266
267	return 0;
268
269baz_error:
270	destroy_foo_obj(bar_obj);
271bar_error:
272	destroy_foo_obj(foo_obj);
273foo_error:
274	kset_unregister(example_kset);
275	return -EINVAL;
276}
277
278static void __exit example_exit(void)
279{
280	destroy_foo_obj(baz_obj);
281	destroy_foo_obj(bar_obj);
282	destroy_foo_obj(foo_obj);
283	kset_unregister(example_kset);
284}
285
286module_init(example_init);
287module_exit(example_exit);
288MODULE_LICENSE("GPL v2");
289MODULE_AUTHOR("Greg Kroah-Hartman <greg@kroah.com>");
v5.9
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * Sample kset and ktype implementation
  4 *
  5 * Copyright (C) 2004-2007 Greg Kroah-Hartman <greg@kroah.com>
  6 * Copyright (C) 2007 Novell Inc.
 
 
 
  7 */
  8#include <linux/kobject.h>
  9#include <linux/string.h>
 10#include <linux/sysfs.h>
 11#include <linux/slab.h>
 12#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 */
 89static const struct sysfs_ops foo_sysfs_ops = {
 90	.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{
121	int ret;
122
123	ret = kstrtoint(buf, 10, &foo_obj->foo);
124	if (ret < 0)
125		return ret;
126
127	return count;
128}
129
130/* Sysfs attributes cannot be world-writable. */
131static struct foo_attribute foo_attribute =
132	__ATTR(foo, 0664, foo_show, foo_store);
133
134/*
135 * More complex function where we determine which variable is being accessed by
136 * 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{
153	int var, ret;
154
155	ret = kstrtoint(buf, 10, &var);
156	if (ret < 0)
157		return ret;
158
159	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 =
167	__ATTR(baz, 0664, b_show, b_store);
168static struct foo_attribute bar_attribute =
169	__ATTR(bar, 0664, b_show, b_store);
170
171/*
172 * Create a group of attributes so that we can create and destroy them all
173 * 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};
181ATTRIBUTE_GROUPS(foo_default);
182
183/*
184 * Our own ktype for our kobjects.  Here we specify our sysfs ops, the
185 * release function, and the set of default attributes we want created
186 * whenever a kobject of this type is registered with the kernel.
187 */
188static struct kobj_type foo_ktype = {
189	.sysfs_ops = &foo_sysfs_ops,
190	.release = foo_release,
191	.default_groups = foo_default_groups,
192};
193
194static struct kset *example_kset;
195static struct foo_obj *foo_obj;
196static struct foo_obj *bar_obj;
197static struct foo_obj *baz_obj;
198
199static struct foo_obj *create_foo_obj(const char *name)
200{
201	struct foo_obj *foo;
202	int retval;
203
204	/* allocate the memory for the whole object */
205	foo = kzalloc(sizeof(*foo), GFP_KERNEL);
206	if (!foo)
207		return NULL;
208
209	/*
210	 * As we have a kset for this kobject, we need to set it before calling
211	 * the kobject core.
212	 */
213	foo->kobj.kset = example_kset;
214
215	/*
216	 * Initialize and add the kobject to the kernel.  All the default files
217	 * will be created here.  As we have already specified a kset for this
218	 * kobject, we don't have to set a parent for the kobject, the kobject
219	 * will be placed beneath that kset automatically.
220	 */
221	retval = kobject_init_and_add(&foo->kobj, &foo_ktype, NULL, "%s", name);
222	if (retval) {
223		kobject_put(&foo->kobj);
224		return NULL;
225	}
226
227	/*
228	 * We are always responsible for sending the uevent that the kobject
229	 * was added to the system.
230	 */
231	kobject_uevent(&foo->kobj, KOBJ_ADD);
232
233	return foo;
234}
235
236static void destroy_foo_obj(struct foo_obj *foo)
237{
238	kobject_put(&foo->kobj);
239}
240
241static int __init example_init(void)
242{
243	/*
244	 * Create a kset with the name of "kset_example",
245	 * located under /sys/kernel/
246	 */
247	example_kset = kset_create_and_add("kset_example", NULL, kernel_kobj);
248	if (!example_kset)
249		return -ENOMEM;
250
251	/*
252	 * Create three objects and register them with our kset
253	 */
254	foo_obj = create_foo_obj("foo");
255	if (!foo_obj)
256		goto foo_error;
257
258	bar_obj = create_foo_obj("bar");
259	if (!bar_obj)
260		goto bar_error;
261
262	baz_obj = create_foo_obj("baz");
263	if (!baz_obj)
264		goto baz_error;
265
266	return 0;
267
268baz_error:
269	destroy_foo_obj(bar_obj);
270bar_error:
271	destroy_foo_obj(foo_obj);
272foo_error:
273	kset_unregister(example_kset);
274	return -EINVAL;
275}
276
277static void __exit example_exit(void)
278{
279	destroy_foo_obj(baz_obj);
280	destroy_foo_obj(bar_obj);
281	destroy_foo_obj(foo_obj);
282	kset_unregister(example_kset);
283}
284
285module_init(example_init);
286module_exit(example_exit);
287MODULE_LICENSE("GPL v2");
288MODULE_AUTHOR("Greg Kroah-Hartman <greg@kroah.com>");