162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Sample kset and ktype implementation
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (C) 2004-2007 Greg Kroah-Hartman <greg@kroah.com>
662306a36Sopenharmony_ci * Copyright (C) 2007 Novell Inc.
762306a36Sopenharmony_ci */
862306a36Sopenharmony_ci#include <linux/kobject.h>
962306a36Sopenharmony_ci#include <linux/string.h>
1062306a36Sopenharmony_ci#include <linux/sysfs.h>
1162306a36Sopenharmony_ci#include <linux/slab.h>
1262306a36Sopenharmony_ci#include <linux/module.h>
1362306a36Sopenharmony_ci#include <linux/init.h>
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ci/*
1662306a36Sopenharmony_ci * This module shows how to create a kset in sysfs called
1762306a36Sopenharmony_ci * /sys/kernel/kset-example
1862306a36Sopenharmony_ci * Then tree kobjects are created and assigned to this kset, "foo", "baz",
1962306a36Sopenharmony_ci * and "bar".  In those kobjects, attributes of the same name are also
2062306a36Sopenharmony_ci * created and if an integer is written to these files, it can be later
2162306a36Sopenharmony_ci * read out of it.
2262306a36Sopenharmony_ci */
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci/*
2662306a36Sopenharmony_ci * This is our "object" that we will create a few of and register them with
2762306a36Sopenharmony_ci * sysfs.
2862306a36Sopenharmony_ci */
2962306a36Sopenharmony_cistruct foo_obj {
3062306a36Sopenharmony_ci	struct kobject kobj;
3162306a36Sopenharmony_ci	int foo;
3262306a36Sopenharmony_ci	int baz;
3362306a36Sopenharmony_ci	int bar;
3462306a36Sopenharmony_ci};
3562306a36Sopenharmony_ci#define to_foo_obj(x) container_of(x, struct foo_obj, kobj)
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ci/* a custom attribute that works just for a struct foo_obj. */
3862306a36Sopenharmony_cistruct foo_attribute {
3962306a36Sopenharmony_ci	struct attribute attr;
4062306a36Sopenharmony_ci	ssize_t (*show)(struct foo_obj *foo, struct foo_attribute *attr, char *buf);
4162306a36Sopenharmony_ci	ssize_t (*store)(struct foo_obj *foo, struct foo_attribute *attr, const char *buf, size_t count);
4262306a36Sopenharmony_ci};
4362306a36Sopenharmony_ci#define to_foo_attr(x) container_of(x, struct foo_attribute, attr)
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_ci/*
4662306a36Sopenharmony_ci * The default show function that must be passed to sysfs.  This will be
4762306a36Sopenharmony_ci * called by sysfs for whenever a show function is called by the user on a
4862306a36Sopenharmony_ci * sysfs file associated with the kobjects we have registered.  We need to
4962306a36Sopenharmony_ci * transpose back from a "default" kobject to our custom struct foo_obj and
5062306a36Sopenharmony_ci * then call the show function for that specific object.
5162306a36Sopenharmony_ci */
5262306a36Sopenharmony_cistatic ssize_t foo_attr_show(struct kobject *kobj,
5362306a36Sopenharmony_ci			     struct attribute *attr,
5462306a36Sopenharmony_ci			     char *buf)
5562306a36Sopenharmony_ci{
5662306a36Sopenharmony_ci	struct foo_attribute *attribute;
5762306a36Sopenharmony_ci	struct foo_obj *foo;
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_ci	attribute = to_foo_attr(attr);
6062306a36Sopenharmony_ci	foo = to_foo_obj(kobj);
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ci	if (!attribute->show)
6362306a36Sopenharmony_ci		return -EIO;
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ci	return attribute->show(foo, attribute, buf);
6662306a36Sopenharmony_ci}
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci/*
6962306a36Sopenharmony_ci * Just like the default show function above, but this one is for when the
7062306a36Sopenharmony_ci * sysfs "store" is requested (when a value is written to a file.)
7162306a36Sopenharmony_ci */
7262306a36Sopenharmony_cistatic ssize_t foo_attr_store(struct kobject *kobj,
7362306a36Sopenharmony_ci			      struct attribute *attr,
7462306a36Sopenharmony_ci			      const char *buf, size_t len)
7562306a36Sopenharmony_ci{
7662306a36Sopenharmony_ci	struct foo_attribute *attribute;
7762306a36Sopenharmony_ci	struct foo_obj *foo;
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ci	attribute = to_foo_attr(attr);
8062306a36Sopenharmony_ci	foo = to_foo_obj(kobj);
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_ci	if (!attribute->store)
8362306a36Sopenharmony_ci		return -EIO;
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ci	return attribute->store(foo, attribute, buf, len);
8662306a36Sopenharmony_ci}
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci/* Our custom sysfs_ops that we will associate with our ktype later on */
8962306a36Sopenharmony_cistatic const struct sysfs_ops foo_sysfs_ops = {
9062306a36Sopenharmony_ci	.show = foo_attr_show,
9162306a36Sopenharmony_ci	.store = foo_attr_store,
9262306a36Sopenharmony_ci};
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ci/*
9562306a36Sopenharmony_ci * The release function for our object.  This is REQUIRED by the kernel to
9662306a36Sopenharmony_ci * have.  We free the memory held in our object here.
9762306a36Sopenharmony_ci *
9862306a36Sopenharmony_ci * NEVER try to get away with just a "blank" release function to try to be
9962306a36Sopenharmony_ci * smarter than the kernel.  Turns out, no one ever is...
10062306a36Sopenharmony_ci */
10162306a36Sopenharmony_cistatic void foo_release(struct kobject *kobj)
10262306a36Sopenharmony_ci{
10362306a36Sopenharmony_ci	struct foo_obj *foo;
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ci	foo = to_foo_obj(kobj);
10662306a36Sopenharmony_ci	kfree(foo);
10762306a36Sopenharmony_ci}
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_ci/*
11062306a36Sopenharmony_ci * The "foo" file where the .foo variable is read from and written to.
11162306a36Sopenharmony_ci */
11262306a36Sopenharmony_cistatic ssize_t foo_show(struct foo_obj *foo_obj, struct foo_attribute *attr,
11362306a36Sopenharmony_ci			char *buf)
11462306a36Sopenharmony_ci{
11562306a36Sopenharmony_ci	return sysfs_emit(buf, "%d\n", foo_obj->foo);
11662306a36Sopenharmony_ci}
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_cistatic ssize_t foo_store(struct foo_obj *foo_obj, struct foo_attribute *attr,
11962306a36Sopenharmony_ci			 const char *buf, size_t count)
12062306a36Sopenharmony_ci{
12162306a36Sopenharmony_ci	int ret;
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_ci	ret = kstrtoint(buf, 10, &foo_obj->foo);
12462306a36Sopenharmony_ci	if (ret < 0)
12562306a36Sopenharmony_ci		return ret;
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_ci	return count;
12862306a36Sopenharmony_ci}
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_ci/* Sysfs attributes cannot be world-writable. */
13162306a36Sopenharmony_cistatic struct foo_attribute foo_attribute =
13262306a36Sopenharmony_ci	__ATTR(foo, 0664, foo_show, foo_store);
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ci/*
13562306a36Sopenharmony_ci * More complex function where we determine which variable is being accessed by
13662306a36Sopenharmony_ci * looking at the attribute for the "baz" and "bar" files.
13762306a36Sopenharmony_ci */
13862306a36Sopenharmony_cistatic ssize_t b_show(struct foo_obj *foo_obj, struct foo_attribute *attr,
13962306a36Sopenharmony_ci		      char *buf)
14062306a36Sopenharmony_ci{
14162306a36Sopenharmony_ci	int var;
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ci	if (strcmp(attr->attr.name, "baz") == 0)
14462306a36Sopenharmony_ci		var = foo_obj->baz;
14562306a36Sopenharmony_ci	else
14662306a36Sopenharmony_ci		var = foo_obj->bar;
14762306a36Sopenharmony_ci	return sysfs_emit(buf, "%d\n", var);
14862306a36Sopenharmony_ci}
14962306a36Sopenharmony_ci
15062306a36Sopenharmony_cistatic ssize_t b_store(struct foo_obj *foo_obj, struct foo_attribute *attr,
15162306a36Sopenharmony_ci		       const char *buf, size_t count)
15262306a36Sopenharmony_ci{
15362306a36Sopenharmony_ci	int var, ret;
15462306a36Sopenharmony_ci
15562306a36Sopenharmony_ci	ret = kstrtoint(buf, 10, &var);
15662306a36Sopenharmony_ci	if (ret < 0)
15762306a36Sopenharmony_ci		return ret;
15862306a36Sopenharmony_ci
15962306a36Sopenharmony_ci	if (strcmp(attr->attr.name, "baz") == 0)
16062306a36Sopenharmony_ci		foo_obj->baz = var;
16162306a36Sopenharmony_ci	else
16262306a36Sopenharmony_ci		foo_obj->bar = var;
16362306a36Sopenharmony_ci	return count;
16462306a36Sopenharmony_ci}
16562306a36Sopenharmony_ci
16662306a36Sopenharmony_cistatic struct foo_attribute baz_attribute =
16762306a36Sopenharmony_ci	__ATTR(baz, 0664, b_show, b_store);
16862306a36Sopenharmony_cistatic struct foo_attribute bar_attribute =
16962306a36Sopenharmony_ci	__ATTR(bar, 0664, b_show, b_store);
17062306a36Sopenharmony_ci
17162306a36Sopenharmony_ci/*
17262306a36Sopenharmony_ci * Create a group of attributes so that we can create and destroy them all
17362306a36Sopenharmony_ci * at once.
17462306a36Sopenharmony_ci */
17562306a36Sopenharmony_cistatic struct attribute *foo_default_attrs[] = {
17662306a36Sopenharmony_ci	&foo_attribute.attr,
17762306a36Sopenharmony_ci	&baz_attribute.attr,
17862306a36Sopenharmony_ci	&bar_attribute.attr,
17962306a36Sopenharmony_ci	NULL,	/* need to NULL terminate the list of attributes */
18062306a36Sopenharmony_ci};
18162306a36Sopenharmony_ciATTRIBUTE_GROUPS(foo_default);
18262306a36Sopenharmony_ci
18362306a36Sopenharmony_ci/*
18462306a36Sopenharmony_ci * Our own ktype for our kobjects.  Here we specify our sysfs ops, the
18562306a36Sopenharmony_ci * release function, and the set of default attributes we want created
18662306a36Sopenharmony_ci * whenever a kobject of this type is registered with the kernel.
18762306a36Sopenharmony_ci */
18862306a36Sopenharmony_cistatic const struct kobj_type foo_ktype = {
18962306a36Sopenharmony_ci	.sysfs_ops = &foo_sysfs_ops,
19062306a36Sopenharmony_ci	.release = foo_release,
19162306a36Sopenharmony_ci	.default_groups = foo_default_groups,
19262306a36Sopenharmony_ci};
19362306a36Sopenharmony_ci
19462306a36Sopenharmony_cistatic struct kset *example_kset;
19562306a36Sopenharmony_cistatic struct foo_obj *foo_obj;
19662306a36Sopenharmony_cistatic struct foo_obj *bar_obj;
19762306a36Sopenharmony_cistatic struct foo_obj *baz_obj;
19862306a36Sopenharmony_ci
19962306a36Sopenharmony_cistatic struct foo_obj *create_foo_obj(const char *name)
20062306a36Sopenharmony_ci{
20162306a36Sopenharmony_ci	struct foo_obj *foo;
20262306a36Sopenharmony_ci	int retval;
20362306a36Sopenharmony_ci
20462306a36Sopenharmony_ci	/* allocate the memory for the whole object */
20562306a36Sopenharmony_ci	foo = kzalloc(sizeof(*foo), GFP_KERNEL);
20662306a36Sopenharmony_ci	if (!foo)
20762306a36Sopenharmony_ci		return NULL;
20862306a36Sopenharmony_ci
20962306a36Sopenharmony_ci	/*
21062306a36Sopenharmony_ci	 * As we have a kset for this kobject, we need to set it before calling
21162306a36Sopenharmony_ci	 * the kobject core.
21262306a36Sopenharmony_ci	 */
21362306a36Sopenharmony_ci	foo->kobj.kset = example_kset;
21462306a36Sopenharmony_ci
21562306a36Sopenharmony_ci	/*
21662306a36Sopenharmony_ci	 * Initialize and add the kobject to the kernel.  All the default files
21762306a36Sopenharmony_ci	 * will be created here.  As we have already specified a kset for this
21862306a36Sopenharmony_ci	 * kobject, we don't have to set a parent for the kobject, the kobject
21962306a36Sopenharmony_ci	 * will be placed beneath that kset automatically.
22062306a36Sopenharmony_ci	 */
22162306a36Sopenharmony_ci	retval = kobject_init_and_add(&foo->kobj, &foo_ktype, NULL, "%s", name);
22262306a36Sopenharmony_ci	if (retval) {
22362306a36Sopenharmony_ci		kobject_put(&foo->kobj);
22462306a36Sopenharmony_ci		return NULL;
22562306a36Sopenharmony_ci	}
22662306a36Sopenharmony_ci
22762306a36Sopenharmony_ci	/*
22862306a36Sopenharmony_ci	 * We are always responsible for sending the uevent that the kobject
22962306a36Sopenharmony_ci	 * was added to the system.
23062306a36Sopenharmony_ci	 */
23162306a36Sopenharmony_ci	kobject_uevent(&foo->kobj, KOBJ_ADD);
23262306a36Sopenharmony_ci
23362306a36Sopenharmony_ci	return foo;
23462306a36Sopenharmony_ci}
23562306a36Sopenharmony_ci
23662306a36Sopenharmony_cistatic void destroy_foo_obj(struct foo_obj *foo)
23762306a36Sopenharmony_ci{
23862306a36Sopenharmony_ci	kobject_put(&foo->kobj);
23962306a36Sopenharmony_ci}
24062306a36Sopenharmony_ci
24162306a36Sopenharmony_cistatic int __init example_init(void)
24262306a36Sopenharmony_ci{
24362306a36Sopenharmony_ci	/*
24462306a36Sopenharmony_ci	 * Create a kset with the name of "kset_example",
24562306a36Sopenharmony_ci	 * located under /sys/kernel/
24662306a36Sopenharmony_ci	 */
24762306a36Sopenharmony_ci	example_kset = kset_create_and_add("kset_example", NULL, kernel_kobj);
24862306a36Sopenharmony_ci	if (!example_kset)
24962306a36Sopenharmony_ci		return -ENOMEM;
25062306a36Sopenharmony_ci
25162306a36Sopenharmony_ci	/*
25262306a36Sopenharmony_ci	 * Create three objects and register them with our kset
25362306a36Sopenharmony_ci	 */
25462306a36Sopenharmony_ci	foo_obj = create_foo_obj("foo");
25562306a36Sopenharmony_ci	if (!foo_obj)
25662306a36Sopenharmony_ci		goto foo_error;
25762306a36Sopenharmony_ci
25862306a36Sopenharmony_ci	bar_obj = create_foo_obj("bar");
25962306a36Sopenharmony_ci	if (!bar_obj)
26062306a36Sopenharmony_ci		goto bar_error;
26162306a36Sopenharmony_ci
26262306a36Sopenharmony_ci	baz_obj = create_foo_obj("baz");
26362306a36Sopenharmony_ci	if (!baz_obj)
26462306a36Sopenharmony_ci		goto baz_error;
26562306a36Sopenharmony_ci
26662306a36Sopenharmony_ci	return 0;
26762306a36Sopenharmony_ci
26862306a36Sopenharmony_cibaz_error:
26962306a36Sopenharmony_ci	destroy_foo_obj(bar_obj);
27062306a36Sopenharmony_cibar_error:
27162306a36Sopenharmony_ci	destroy_foo_obj(foo_obj);
27262306a36Sopenharmony_cifoo_error:
27362306a36Sopenharmony_ci	kset_unregister(example_kset);
27462306a36Sopenharmony_ci	return -EINVAL;
27562306a36Sopenharmony_ci}
27662306a36Sopenharmony_ci
27762306a36Sopenharmony_cistatic void __exit example_exit(void)
27862306a36Sopenharmony_ci{
27962306a36Sopenharmony_ci	destroy_foo_obj(baz_obj);
28062306a36Sopenharmony_ci	destroy_foo_obj(bar_obj);
28162306a36Sopenharmony_ci	destroy_foo_obj(foo_obj);
28262306a36Sopenharmony_ci	kset_unregister(example_kset);
28362306a36Sopenharmony_ci}
28462306a36Sopenharmony_ci
28562306a36Sopenharmony_cimodule_init(example_init);
28662306a36Sopenharmony_cimodule_exit(example_exit);
28762306a36Sopenharmony_ciMODULE_LICENSE("GPL v2");
28862306a36Sopenharmony_ciMODULE_AUTHOR("Greg Kroah-Hartman <greg@kroah.com>");
289