144bfe16eSGreg Kroah-Hartman /*
244bfe16eSGreg Kroah-Hartman  * Sample kset and ktype implementation
344bfe16eSGreg Kroah-Hartman  *
444bfe16eSGreg Kroah-Hartman  * Copyright (C) 2004-2007 Greg Kroah-Hartman <greg@kroah.com>
544bfe16eSGreg Kroah-Hartman  * Copyright (C) 2007 Novell Inc.
644bfe16eSGreg Kroah-Hartman  *
744bfe16eSGreg Kroah-Hartman  * Released under the GPL version 2 only.
844bfe16eSGreg Kroah-Hartman  *
944bfe16eSGreg Kroah-Hartman  */
1044bfe16eSGreg Kroah-Hartman #include <linux/kobject.h>
1144bfe16eSGreg Kroah-Hartman #include <linux/string.h>
1244bfe16eSGreg Kroah-Hartman #include <linux/sysfs.h>
135a0e3ad6STejun Heo #include <linux/slab.h>
1444bfe16eSGreg Kroah-Hartman #include <linux/module.h>
1544bfe16eSGreg Kroah-Hartman #include <linux/init.h>
1644bfe16eSGreg Kroah-Hartman 
1744bfe16eSGreg Kroah-Hartman /*
1844bfe16eSGreg Kroah-Hartman  * This module shows how to create a kset in sysfs called
1944bfe16eSGreg Kroah-Hartman  * /sys/kernel/kset-example
2044bfe16eSGreg Kroah-Hartman  * Then tree kobjects are created and assigned to this kset, "foo", "baz",
2144bfe16eSGreg Kroah-Hartman  * and "bar".  In those kobjects, attributes of the same name are also
2244bfe16eSGreg Kroah-Hartman  * created and if an integer is written to these files, it can be later
2344bfe16eSGreg Kroah-Hartman  * read out of it.
2444bfe16eSGreg Kroah-Hartman  */
2544bfe16eSGreg Kroah-Hartman 
2644bfe16eSGreg Kroah-Hartman 
2744bfe16eSGreg Kroah-Hartman /*
2844bfe16eSGreg Kroah-Hartman  * This is our "object" that we will create a few of and register them with
2944bfe16eSGreg Kroah-Hartman  * sysfs.
3044bfe16eSGreg Kroah-Hartman  */
3144bfe16eSGreg Kroah-Hartman struct foo_obj {
3244bfe16eSGreg Kroah-Hartman 	struct kobject kobj;
3344bfe16eSGreg Kroah-Hartman 	int foo;
3444bfe16eSGreg Kroah-Hartman 	int baz;
3544bfe16eSGreg Kroah-Hartman 	int bar;
3644bfe16eSGreg Kroah-Hartman };
3744bfe16eSGreg Kroah-Hartman #define to_foo_obj(x) container_of(x, struct foo_obj, kobj)
3844bfe16eSGreg Kroah-Hartman 
3944bfe16eSGreg Kroah-Hartman /* a custom attribute that works just for a struct foo_obj. */
4044bfe16eSGreg Kroah-Hartman struct foo_attribute {
4144bfe16eSGreg Kroah-Hartman 	struct attribute attr;
4244bfe16eSGreg Kroah-Hartman 	ssize_t (*show)(struct foo_obj *foo, struct foo_attribute *attr, char *buf);
4344bfe16eSGreg Kroah-Hartman 	ssize_t (*store)(struct foo_obj *foo, struct foo_attribute *attr, const char *buf, size_t count);
4444bfe16eSGreg Kroah-Hartman };
4544bfe16eSGreg Kroah-Hartman #define to_foo_attr(x) container_of(x, struct foo_attribute, attr)
4644bfe16eSGreg Kroah-Hartman 
4744bfe16eSGreg Kroah-Hartman /*
4844bfe16eSGreg Kroah-Hartman  * The default show function that must be passed to sysfs.  This will be
4944bfe16eSGreg Kroah-Hartman  * called by sysfs for whenever a show function is called by the user on a
5044bfe16eSGreg Kroah-Hartman  * sysfs file associated with the kobjects we have registered.  We need to
5144bfe16eSGreg Kroah-Hartman  * transpose back from a "default" kobject to our custom struct foo_obj and
5244bfe16eSGreg Kroah-Hartman  * then call the show function for that specific object.
5344bfe16eSGreg Kroah-Hartman  */
5444bfe16eSGreg Kroah-Hartman static ssize_t foo_attr_show(struct kobject *kobj,
5544bfe16eSGreg Kroah-Hartman 			     struct attribute *attr,
5644bfe16eSGreg Kroah-Hartman 			     char *buf)
5744bfe16eSGreg Kroah-Hartman {
5844bfe16eSGreg Kroah-Hartman 	struct foo_attribute *attribute;
5944bfe16eSGreg Kroah-Hartman 	struct foo_obj *foo;
6044bfe16eSGreg Kroah-Hartman 
6144bfe16eSGreg Kroah-Hartman 	attribute = to_foo_attr(attr);
6244bfe16eSGreg Kroah-Hartman 	foo = to_foo_obj(kobj);
6344bfe16eSGreg Kroah-Hartman 
6444bfe16eSGreg Kroah-Hartman 	if (!attribute->show)
6544bfe16eSGreg Kroah-Hartman 		return -EIO;
6644bfe16eSGreg Kroah-Hartman 
6744bfe16eSGreg Kroah-Hartman 	return attribute->show(foo, attribute, buf);
6844bfe16eSGreg Kroah-Hartman }
6944bfe16eSGreg Kroah-Hartman 
7044bfe16eSGreg Kroah-Hartman /*
7144bfe16eSGreg Kroah-Hartman  * Just like the default show function above, but this one is for when the
7244bfe16eSGreg Kroah-Hartman  * sysfs "store" is requested (when a value is written to a file.)
7344bfe16eSGreg Kroah-Hartman  */
7444bfe16eSGreg Kroah-Hartman static ssize_t foo_attr_store(struct kobject *kobj,
7544bfe16eSGreg Kroah-Hartman 			      struct attribute *attr,
7644bfe16eSGreg Kroah-Hartman 			      const char *buf, size_t len)
7744bfe16eSGreg Kroah-Hartman {
7844bfe16eSGreg Kroah-Hartman 	struct foo_attribute *attribute;
7944bfe16eSGreg Kroah-Hartman 	struct foo_obj *foo;
8044bfe16eSGreg Kroah-Hartman 
8144bfe16eSGreg Kroah-Hartman 	attribute = to_foo_attr(attr);
8244bfe16eSGreg Kroah-Hartman 	foo = to_foo_obj(kobj);
8344bfe16eSGreg Kroah-Hartman 
8444bfe16eSGreg Kroah-Hartman 	if (!attribute->store)
8544bfe16eSGreg Kroah-Hartman 		return -EIO;
8644bfe16eSGreg Kroah-Hartman 
8744bfe16eSGreg Kroah-Hartman 	return attribute->store(foo, attribute, buf, len);
8844bfe16eSGreg Kroah-Hartman }
8944bfe16eSGreg Kroah-Hartman 
9044bfe16eSGreg Kroah-Hartman /* Our custom sysfs_ops that we will associate with our ktype later on */
9152cf25d0SEmese Revfy static const struct sysfs_ops foo_sysfs_ops = {
9244bfe16eSGreg Kroah-Hartman 	.show = foo_attr_show,
9344bfe16eSGreg Kroah-Hartman 	.store = foo_attr_store,
9444bfe16eSGreg Kroah-Hartman };
9544bfe16eSGreg Kroah-Hartman 
9644bfe16eSGreg Kroah-Hartman /*
9744bfe16eSGreg Kroah-Hartman  * The release function for our object.  This is REQUIRED by the kernel to
9844bfe16eSGreg Kroah-Hartman  * have.  We free the memory held in our object here.
9944bfe16eSGreg Kroah-Hartman  *
10044bfe16eSGreg Kroah-Hartman  * NEVER try to get away with just a "blank" release function to try to be
10144bfe16eSGreg Kroah-Hartman  * smarter than the kernel.  Turns out, no one ever is...
10244bfe16eSGreg Kroah-Hartman  */
10344bfe16eSGreg Kroah-Hartman static void foo_release(struct kobject *kobj)
10444bfe16eSGreg Kroah-Hartman {
10544bfe16eSGreg Kroah-Hartman 	struct foo_obj *foo;
10644bfe16eSGreg Kroah-Hartman 
10744bfe16eSGreg Kroah-Hartman 	foo = to_foo_obj(kobj);
10844bfe16eSGreg Kroah-Hartman 	kfree(foo);
10944bfe16eSGreg Kroah-Hartman }
11044bfe16eSGreg Kroah-Hartman 
11144bfe16eSGreg Kroah-Hartman /*
11244bfe16eSGreg Kroah-Hartman  * The "foo" file where the .foo variable is read from and written to.
11344bfe16eSGreg Kroah-Hartman  */
11444bfe16eSGreg Kroah-Hartman static ssize_t foo_show(struct foo_obj *foo_obj, struct foo_attribute *attr,
11544bfe16eSGreg Kroah-Hartman 			char *buf)
11644bfe16eSGreg Kroah-Hartman {
11744bfe16eSGreg Kroah-Hartman 	return sprintf(buf, "%d\n", foo_obj->foo);
11844bfe16eSGreg Kroah-Hartman }
11944bfe16eSGreg Kroah-Hartman 
12044bfe16eSGreg Kroah-Hartman static ssize_t foo_store(struct foo_obj *foo_obj, struct foo_attribute *attr,
12144bfe16eSGreg Kroah-Hartman 			 const char *buf, size_t count)
12244bfe16eSGreg Kroah-Hartman {
1235fd637e7SRastislav Barlik 	int ret;
1245fd637e7SRastislav Barlik 
1255fd637e7SRastislav Barlik 	ret = kstrtoint(buf, 10, &foo_obj->foo);
1265fd637e7SRastislav Barlik 	if (ret < 0)
1275fd637e7SRastislav Barlik 		return ret;
1285fd637e7SRastislav Barlik 
12944bfe16eSGreg Kroah-Hartman 	return count;
13044bfe16eSGreg Kroah-Hartman }
13144bfe16eSGreg Kroah-Hartman 
132de510989SRusty Russell /* Sysfs attributes cannot be world-writable. */
13344bfe16eSGreg Kroah-Hartman static struct foo_attribute foo_attribute =
134de510989SRusty Russell 	__ATTR(foo, 0664, foo_show, foo_store);
13544bfe16eSGreg Kroah-Hartman 
13644bfe16eSGreg Kroah-Hartman /*
13720ef9f46SRadu Voicilas  * More complex function where we determine which variable is being accessed by
13844bfe16eSGreg Kroah-Hartman  * looking at the attribute for the "baz" and "bar" files.
13944bfe16eSGreg Kroah-Hartman  */
14044bfe16eSGreg Kroah-Hartman static ssize_t b_show(struct foo_obj *foo_obj, struct foo_attribute *attr,
14144bfe16eSGreg Kroah-Hartman 		      char *buf)
14244bfe16eSGreg Kroah-Hartman {
14344bfe16eSGreg Kroah-Hartman 	int var;
14444bfe16eSGreg Kroah-Hartman 
14544bfe16eSGreg Kroah-Hartman 	if (strcmp(attr->attr.name, "baz") == 0)
14644bfe16eSGreg Kroah-Hartman 		var = foo_obj->baz;
14744bfe16eSGreg Kroah-Hartman 	else
14844bfe16eSGreg Kroah-Hartman 		var = foo_obj->bar;
14944bfe16eSGreg Kroah-Hartman 	return sprintf(buf, "%d\n", var);
15044bfe16eSGreg Kroah-Hartman }
15144bfe16eSGreg Kroah-Hartman 
15244bfe16eSGreg Kroah-Hartman static ssize_t b_store(struct foo_obj *foo_obj, struct foo_attribute *attr,
15344bfe16eSGreg Kroah-Hartman 		       const char *buf, size_t count)
15444bfe16eSGreg Kroah-Hartman {
1555fd637e7SRastislav Barlik 	int var, ret;
15644bfe16eSGreg Kroah-Hartman 
1575fd637e7SRastislav Barlik 	ret = kstrtoint(buf, 10, &var);
1585fd637e7SRastislav Barlik 	if (ret < 0)
1595fd637e7SRastislav Barlik 		return ret;
1605fd637e7SRastislav Barlik 
16144bfe16eSGreg Kroah-Hartman 	if (strcmp(attr->attr.name, "baz") == 0)
16244bfe16eSGreg Kroah-Hartman 		foo_obj->baz = var;
16344bfe16eSGreg Kroah-Hartman 	else
16444bfe16eSGreg Kroah-Hartman 		foo_obj->bar = var;
16544bfe16eSGreg Kroah-Hartman 	return count;
16644bfe16eSGreg Kroah-Hartman }
16744bfe16eSGreg Kroah-Hartman 
16844bfe16eSGreg Kroah-Hartman static struct foo_attribute baz_attribute =
169de510989SRusty Russell 	__ATTR(baz, 0664, b_show, b_store);
17044bfe16eSGreg Kroah-Hartman static struct foo_attribute bar_attribute =
171de510989SRusty Russell 	__ATTR(bar, 0664, b_show, b_store);
17244bfe16eSGreg Kroah-Hartman 
17344bfe16eSGreg Kroah-Hartman /*
17420ef9f46SRadu Voicilas  * Create a group of attributes so that we can create and destroy them all
17544bfe16eSGreg Kroah-Hartman  * at once.
17644bfe16eSGreg Kroah-Hartman  */
17744bfe16eSGreg Kroah-Hartman static struct attribute *foo_default_attrs[] = {
17844bfe16eSGreg Kroah-Hartman 	&foo_attribute.attr,
17944bfe16eSGreg Kroah-Hartman 	&baz_attribute.attr,
18044bfe16eSGreg Kroah-Hartman 	&bar_attribute.attr,
18144bfe16eSGreg Kroah-Hartman 	NULL,	/* need to NULL terminate the list of attributes */
18244bfe16eSGreg Kroah-Hartman };
18344bfe16eSGreg Kroah-Hartman 
18444bfe16eSGreg Kroah-Hartman /*
18544bfe16eSGreg Kroah-Hartman  * Our own ktype for our kobjects.  Here we specify our sysfs ops, the
18644bfe16eSGreg Kroah-Hartman  * release function, and the set of default attributes we want created
18744bfe16eSGreg Kroah-Hartman  * whenever a kobject of this type is registered with the kernel.
18844bfe16eSGreg Kroah-Hartman  */
18944bfe16eSGreg Kroah-Hartman static struct kobj_type foo_ktype = {
19044bfe16eSGreg Kroah-Hartman 	.sysfs_ops = &foo_sysfs_ops,
19144bfe16eSGreg Kroah-Hartman 	.release = foo_release,
19244bfe16eSGreg Kroah-Hartman 	.default_attrs = foo_default_attrs,
19344bfe16eSGreg Kroah-Hartman };
19444bfe16eSGreg Kroah-Hartman 
19544bfe16eSGreg Kroah-Hartman static struct kset *example_kset;
19644bfe16eSGreg Kroah-Hartman static struct foo_obj *foo_obj;
19744bfe16eSGreg Kroah-Hartman static struct foo_obj *bar_obj;
19844bfe16eSGreg Kroah-Hartman static struct foo_obj *baz_obj;
19944bfe16eSGreg Kroah-Hartman 
20044bfe16eSGreg Kroah-Hartman static struct foo_obj *create_foo_obj(const char *name)
20144bfe16eSGreg Kroah-Hartman {
20244bfe16eSGreg Kroah-Hartman 	struct foo_obj *foo;
20344bfe16eSGreg Kroah-Hartman 	int retval;
20444bfe16eSGreg Kroah-Hartman 
20544bfe16eSGreg Kroah-Hartman 	/* allocate the memory for the whole object */
20644bfe16eSGreg Kroah-Hartman 	foo = kzalloc(sizeof(*foo), GFP_KERNEL);
20744bfe16eSGreg Kroah-Hartman 	if (!foo)
20844bfe16eSGreg Kroah-Hartman 		return NULL;
20944bfe16eSGreg Kroah-Hartman 
21044bfe16eSGreg Kroah-Hartman 	/*
21144bfe16eSGreg Kroah-Hartman 	 * As we have a kset for this kobject, we need to set it before calling
21244bfe16eSGreg Kroah-Hartman 	 * the kobject core.
21344bfe16eSGreg Kroah-Hartman 	 */
21444bfe16eSGreg Kroah-Hartman 	foo->kobj.kset = example_kset;
21544bfe16eSGreg Kroah-Hartman 
21644bfe16eSGreg Kroah-Hartman 	/*
21744bfe16eSGreg Kroah-Hartman 	 * Initialize and add the kobject to the kernel.  All the default files
21844bfe16eSGreg Kroah-Hartman 	 * will be created here.  As we have already specified a kset for this
21944bfe16eSGreg Kroah-Hartman 	 * kobject, we don't have to set a parent for the kobject, the kobject
22044bfe16eSGreg Kroah-Hartman 	 * will be placed beneath that kset automatically.
22144bfe16eSGreg Kroah-Hartman 	 */
22244bfe16eSGreg Kroah-Hartman 	retval = kobject_init_and_add(&foo->kobj, &foo_ktype, NULL, "%s", name);
22344bfe16eSGreg Kroah-Hartman 	if (retval) {
224185000fcSLi Zefan 		kobject_put(&foo->kobj);
22544bfe16eSGreg Kroah-Hartman 		return NULL;
22644bfe16eSGreg Kroah-Hartman 	}
22744bfe16eSGreg Kroah-Hartman 
22844bfe16eSGreg Kroah-Hartman 	/*
22944bfe16eSGreg Kroah-Hartman 	 * We are always responsible for sending the uevent that the kobject
23044bfe16eSGreg Kroah-Hartman 	 * was added to the system.
23144bfe16eSGreg Kroah-Hartman 	 */
23244bfe16eSGreg Kroah-Hartman 	kobject_uevent(&foo->kobj, KOBJ_ADD);
23344bfe16eSGreg Kroah-Hartman 
23444bfe16eSGreg Kroah-Hartman 	return foo;
23544bfe16eSGreg Kroah-Hartman }
23644bfe16eSGreg Kroah-Hartman 
23744bfe16eSGreg Kroah-Hartman static void destroy_foo_obj(struct foo_obj *foo)
23844bfe16eSGreg Kroah-Hartman {
23944bfe16eSGreg Kroah-Hartman 	kobject_put(&foo->kobj);
24044bfe16eSGreg Kroah-Hartman }
24144bfe16eSGreg Kroah-Hartman 
2427ec7fb39SQinghuang Feng static int __init example_init(void)
24344bfe16eSGreg Kroah-Hartman {
24444bfe16eSGreg Kroah-Hartman 	/*
24544bfe16eSGreg Kroah-Hartman 	 * Create a kset with the name of "kset_example",
24644bfe16eSGreg Kroah-Hartman 	 * located under /sys/kernel/
24744bfe16eSGreg Kroah-Hartman 	 */
24844bfe16eSGreg Kroah-Hartman 	example_kset = kset_create_and_add("kset_example", NULL, kernel_kobj);
24944bfe16eSGreg Kroah-Hartman 	if (!example_kset)
25044bfe16eSGreg Kroah-Hartman 		return -ENOMEM;
25144bfe16eSGreg Kroah-Hartman 
25244bfe16eSGreg Kroah-Hartman 	/*
25344bfe16eSGreg Kroah-Hartman 	 * Create three objects and register them with our kset
25444bfe16eSGreg Kroah-Hartman 	 */
25544bfe16eSGreg Kroah-Hartman 	foo_obj = create_foo_obj("foo");
25644bfe16eSGreg Kroah-Hartman 	if (!foo_obj)
25744bfe16eSGreg Kroah-Hartman 		goto foo_error;
25844bfe16eSGreg Kroah-Hartman 
25944bfe16eSGreg Kroah-Hartman 	bar_obj = create_foo_obj("bar");
26044bfe16eSGreg Kroah-Hartman 	if (!bar_obj)
26144bfe16eSGreg Kroah-Hartman 		goto bar_error;
26244bfe16eSGreg Kroah-Hartman 
26344bfe16eSGreg Kroah-Hartman 	baz_obj = create_foo_obj("baz");
26444bfe16eSGreg Kroah-Hartman 	if (!baz_obj)
26544bfe16eSGreg Kroah-Hartman 		goto baz_error;
26644bfe16eSGreg Kroah-Hartman 
26744bfe16eSGreg Kroah-Hartman 	return 0;
26844bfe16eSGreg Kroah-Hartman 
26944bfe16eSGreg Kroah-Hartman baz_error:
27044bfe16eSGreg Kroah-Hartman 	destroy_foo_obj(bar_obj);
27144bfe16eSGreg Kroah-Hartman bar_error:
27244bfe16eSGreg Kroah-Hartman 	destroy_foo_obj(foo_obj);
27344bfe16eSGreg Kroah-Hartman foo_error:
274e756bc56SBjorn Helgaas 	kset_unregister(example_kset);
27544bfe16eSGreg Kroah-Hartman 	return -EINVAL;
27644bfe16eSGreg Kroah-Hartman }
27744bfe16eSGreg Kroah-Hartman 
2787ec7fb39SQinghuang Feng static void __exit example_exit(void)
27944bfe16eSGreg Kroah-Hartman {
28044bfe16eSGreg Kroah-Hartman 	destroy_foo_obj(baz_obj);
28144bfe16eSGreg Kroah-Hartman 	destroy_foo_obj(bar_obj);
28244bfe16eSGreg Kroah-Hartman 	destroy_foo_obj(foo_obj);
28344bfe16eSGreg Kroah-Hartman 	kset_unregister(example_kset);
28444bfe16eSGreg Kroah-Hartman }
28544bfe16eSGreg Kroah-Hartman 
28644bfe16eSGreg Kroah-Hartman module_init(example_init);
28744bfe16eSGreg Kroah-Hartman module_exit(example_exit);
28807afb6acSGreg Kroah-Hartman MODULE_LICENSE("GPL v2");
28944bfe16eSGreg Kroah-Hartman MODULE_AUTHOR("Greg Kroah-Hartman <greg@kroah.com>");
290