18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Sample kset and ktype implementation 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2004-2007 Greg Kroah-Hartman <greg@kroah.com> 68c2ecf20Sopenharmony_ci * Copyright (C) 2007 Novell Inc. 78c2ecf20Sopenharmony_ci */ 88c2ecf20Sopenharmony_ci#include <linux/kobject.h> 98c2ecf20Sopenharmony_ci#include <linux/string.h> 108c2ecf20Sopenharmony_ci#include <linux/sysfs.h> 118c2ecf20Sopenharmony_ci#include <linux/slab.h> 128c2ecf20Sopenharmony_ci#include <linux/module.h> 138c2ecf20Sopenharmony_ci#include <linux/init.h> 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_ci/* 168c2ecf20Sopenharmony_ci * This module shows how to create a kset in sysfs called 178c2ecf20Sopenharmony_ci * /sys/kernel/kset-example 188c2ecf20Sopenharmony_ci * Then tree kobjects are created and assigned to this kset, "foo", "baz", 198c2ecf20Sopenharmony_ci * and "bar". In those kobjects, attributes of the same name are also 208c2ecf20Sopenharmony_ci * created and if an integer is written to these files, it can be later 218c2ecf20Sopenharmony_ci * read out of it. 228c2ecf20Sopenharmony_ci */ 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_ci/* 268c2ecf20Sopenharmony_ci * This is our "object" that we will create a few of and register them with 278c2ecf20Sopenharmony_ci * sysfs. 288c2ecf20Sopenharmony_ci */ 298c2ecf20Sopenharmony_cistruct foo_obj { 308c2ecf20Sopenharmony_ci struct kobject kobj; 318c2ecf20Sopenharmony_ci int foo; 328c2ecf20Sopenharmony_ci int baz; 338c2ecf20Sopenharmony_ci int bar; 348c2ecf20Sopenharmony_ci}; 358c2ecf20Sopenharmony_ci#define to_foo_obj(x) container_of(x, struct foo_obj, kobj) 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_ci/* a custom attribute that works just for a struct foo_obj. */ 388c2ecf20Sopenharmony_cistruct foo_attribute { 398c2ecf20Sopenharmony_ci struct attribute attr; 408c2ecf20Sopenharmony_ci ssize_t (*show)(struct foo_obj *foo, struct foo_attribute *attr, char *buf); 418c2ecf20Sopenharmony_ci ssize_t (*store)(struct foo_obj *foo, struct foo_attribute *attr, const char *buf, size_t count); 428c2ecf20Sopenharmony_ci}; 438c2ecf20Sopenharmony_ci#define to_foo_attr(x) container_of(x, struct foo_attribute, attr) 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_ci/* 468c2ecf20Sopenharmony_ci * The default show function that must be passed to sysfs. This will be 478c2ecf20Sopenharmony_ci * called by sysfs for whenever a show function is called by the user on a 488c2ecf20Sopenharmony_ci * sysfs file associated with the kobjects we have registered. We need to 498c2ecf20Sopenharmony_ci * transpose back from a "default" kobject to our custom struct foo_obj and 508c2ecf20Sopenharmony_ci * then call the show function for that specific object. 518c2ecf20Sopenharmony_ci */ 528c2ecf20Sopenharmony_cistatic ssize_t foo_attr_show(struct kobject *kobj, 538c2ecf20Sopenharmony_ci struct attribute *attr, 548c2ecf20Sopenharmony_ci char *buf) 558c2ecf20Sopenharmony_ci{ 568c2ecf20Sopenharmony_ci struct foo_attribute *attribute; 578c2ecf20Sopenharmony_ci struct foo_obj *foo; 588c2ecf20Sopenharmony_ci 598c2ecf20Sopenharmony_ci attribute = to_foo_attr(attr); 608c2ecf20Sopenharmony_ci foo = to_foo_obj(kobj); 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_ci if (!attribute->show) 638c2ecf20Sopenharmony_ci return -EIO; 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_ci return attribute->show(foo, attribute, buf); 668c2ecf20Sopenharmony_ci} 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_ci/* 698c2ecf20Sopenharmony_ci * Just like the default show function above, but this one is for when the 708c2ecf20Sopenharmony_ci * sysfs "store" is requested (when a value is written to a file.) 718c2ecf20Sopenharmony_ci */ 728c2ecf20Sopenharmony_cistatic ssize_t foo_attr_store(struct kobject *kobj, 738c2ecf20Sopenharmony_ci struct attribute *attr, 748c2ecf20Sopenharmony_ci const char *buf, size_t len) 758c2ecf20Sopenharmony_ci{ 768c2ecf20Sopenharmony_ci struct foo_attribute *attribute; 778c2ecf20Sopenharmony_ci struct foo_obj *foo; 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_ci attribute = to_foo_attr(attr); 808c2ecf20Sopenharmony_ci foo = to_foo_obj(kobj); 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_ci if (!attribute->store) 838c2ecf20Sopenharmony_ci return -EIO; 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_ci return attribute->store(foo, attribute, buf, len); 868c2ecf20Sopenharmony_ci} 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ci/* Our custom sysfs_ops that we will associate with our ktype later on */ 898c2ecf20Sopenharmony_cistatic const struct sysfs_ops foo_sysfs_ops = { 908c2ecf20Sopenharmony_ci .show = foo_attr_show, 918c2ecf20Sopenharmony_ci .store = foo_attr_store, 928c2ecf20Sopenharmony_ci}; 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_ci/* 958c2ecf20Sopenharmony_ci * The release function for our object. This is REQUIRED by the kernel to 968c2ecf20Sopenharmony_ci * have. We free the memory held in our object here. 978c2ecf20Sopenharmony_ci * 988c2ecf20Sopenharmony_ci * NEVER try to get away with just a "blank" release function to try to be 998c2ecf20Sopenharmony_ci * smarter than the kernel. Turns out, no one ever is... 1008c2ecf20Sopenharmony_ci */ 1018c2ecf20Sopenharmony_cistatic void foo_release(struct kobject *kobj) 1028c2ecf20Sopenharmony_ci{ 1038c2ecf20Sopenharmony_ci struct foo_obj *foo; 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_ci foo = to_foo_obj(kobj); 1068c2ecf20Sopenharmony_ci kfree(foo); 1078c2ecf20Sopenharmony_ci} 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_ci/* 1108c2ecf20Sopenharmony_ci * The "foo" file where the .foo variable is read from and written to. 1118c2ecf20Sopenharmony_ci */ 1128c2ecf20Sopenharmony_cistatic ssize_t foo_show(struct foo_obj *foo_obj, struct foo_attribute *attr, 1138c2ecf20Sopenharmony_ci char *buf) 1148c2ecf20Sopenharmony_ci{ 1158c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", foo_obj->foo); 1168c2ecf20Sopenharmony_ci} 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_cistatic ssize_t foo_store(struct foo_obj *foo_obj, struct foo_attribute *attr, 1198c2ecf20Sopenharmony_ci const char *buf, size_t count) 1208c2ecf20Sopenharmony_ci{ 1218c2ecf20Sopenharmony_ci int ret; 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_ci ret = kstrtoint(buf, 10, &foo_obj->foo); 1248c2ecf20Sopenharmony_ci if (ret < 0) 1258c2ecf20Sopenharmony_ci return ret; 1268c2ecf20Sopenharmony_ci 1278c2ecf20Sopenharmony_ci return count; 1288c2ecf20Sopenharmony_ci} 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_ci/* Sysfs attributes cannot be world-writable. */ 1318c2ecf20Sopenharmony_cistatic struct foo_attribute foo_attribute = 1328c2ecf20Sopenharmony_ci __ATTR(foo, 0664, foo_show, foo_store); 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_ci/* 1358c2ecf20Sopenharmony_ci * More complex function where we determine which variable is being accessed by 1368c2ecf20Sopenharmony_ci * looking at the attribute for the "baz" and "bar" files. 1378c2ecf20Sopenharmony_ci */ 1388c2ecf20Sopenharmony_cistatic ssize_t b_show(struct foo_obj *foo_obj, struct foo_attribute *attr, 1398c2ecf20Sopenharmony_ci char *buf) 1408c2ecf20Sopenharmony_ci{ 1418c2ecf20Sopenharmony_ci int var; 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_ci if (strcmp(attr->attr.name, "baz") == 0) 1448c2ecf20Sopenharmony_ci var = foo_obj->baz; 1458c2ecf20Sopenharmony_ci else 1468c2ecf20Sopenharmony_ci var = foo_obj->bar; 1478c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", var); 1488c2ecf20Sopenharmony_ci} 1498c2ecf20Sopenharmony_ci 1508c2ecf20Sopenharmony_cistatic ssize_t b_store(struct foo_obj *foo_obj, struct foo_attribute *attr, 1518c2ecf20Sopenharmony_ci const char *buf, size_t count) 1528c2ecf20Sopenharmony_ci{ 1538c2ecf20Sopenharmony_ci int var, ret; 1548c2ecf20Sopenharmony_ci 1558c2ecf20Sopenharmony_ci ret = kstrtoint(buf, 10, &var); 1568c2ecf20Sopenharmony_ci if (ret < 0) 1578c2ecf20Sopenharmony_ci return ret; 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_ci if (strcmp(attr->attr.name, "baz") == 0) 1608c2ecf20Sopenharmony_ci foo_obj->baz = var; 1618c2ecf20Sopenharmony_ci else 1628c2ecf20Sopenharmony_ci foo_obj->bar = var; 1638c2ecf20Sopenharmony_ci return count; 1648c2ecf20Sopenharmony_ci} 1658c2ecf20Sopenharmony_ci 1668c2ecf20Sopenharmony_cistatic struct foo_attribute baz_attribute = 1678c2ecf20Sopenharmony_ci __ATTR(baz, 0664, b_show, b_store); 1688c2ecf20Sopenharmony_cistatic struct foo_attribute bar_attribute = 1698c2ecf20Sopenharmony_ci __ATTR(bar, 0664, b_show, b_store); 1708c2ecf20Sopenharmony_ci 1718c2ecf20Sopenharmony_ci/* 1728c2ecf20Sopenharmony_ci * Create a group of attributes so that we can create and destroy them all 1738c2ecf20Sopenharmony_ci * at once. 1748c2ecf20Sopenharmony_ci */ 1758c2ecf20Sopenharmony_cistatic struct attribute *foo_default_attrs[] = { 1768c2ecf20Sopenharmony_ci &foo_attribute.attr, 1778c2ecf20Sopenharmony_ci &baz_attribute.attr, 1788c2ecf20Sopenharmony_ci &bar_attribute.attr, 1798c2ecf20Sopenharmony_ci NULL, /* need to NULL terminate the list of attributes */ 1808c2ecf20Sopenharmony_ci}; 1818c2ecf20Sopenharmony_ciATTRIBUTE_GROUPS(foo_default); 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_ci/* 1848c2ecf20Sopenharmony_ci * Our own ktype for our kobjects. Here we specify our sysfs ops, the 1858c2ecf20Sopenharmony_ci * release function, and the set of default attributes we want created 1868c2ecf20Sopenharmony_ci * whenever a kobject of this type is registered with the kernel. 1878c2ecf20Sopenharmony_ci */ 1888c2ecf20Sopenharmony_cistatic struct kobj_type foo_ktype = { 1898c2ecf20Sopenharmony_ci .sysfs_ops = &foo_sysfs_ops, 1908c2ecf20Sopenharmony_ci .release = foo_release, 1918c2ecf20Sopenharmony_ci .default_groups = foo_default_groups, 1928c2ecf20Sopenharmony_ci}; 1938c2ecf20Sopenharmony_ci 1948c2ecf20Sopenharmony_cistatic struct kset *example_kset; 1958c2ecf20Sopenharmony_cistatic struct foo_obj *foo_obj; 1968c2ecf20Sopenharmony_cistatic struct foo_obj *bar_obj; 1978c2ecf20Sopenharmony_cistatic struct foo_obj *baz_obj; 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_cistatic struct foo_obj *create_foo_obj(const char *name) 2008c2ecf20Sopenharmony_ci{ 2018c2ecf20Sopenharmony_ci struct foo_obj *foo; 2028c2ecf20Sopenharmony_ci int retval; 2038c2ecf20Sopenharmony_ci 2048c2ecf20Sopenharmony_ci /* allocate the memory for the whole object */ 2058c2ecf20Sopenharmony_ci foo = kzalloc(sizeof(*foo), GFP_KERNEL); 2068c2ecf20Sopenharmony_ci if (!foo) 2078c2ecf20Sopenharmony_ci return NULL; 2088c2ecf20Sopenharmony_ci 2098c2ecf20Sopenharmony_ci /* 2108c2ecf20Sopenharmony_ci * As we have a kset for this kobject, we need to set it before calling 2118c2ecf20Sopenharmony_ci * the kobject core. 2128c2ecf20Sopenharmony_ci */ 2138c2ecf20Sopenharmony_ci foo->kobj.kset = example_kset; 2148c2ecf20Sopenharmony_ci 2158c2ecf20Sopenharmony_ci /* 2168c2ecf20Sopenharmony_ci * Initialize and add the kobject to the kernel. All the default files 2178c2ecf20Sopenharmony_ci * will be created here. As we have already specified a kset for this 2188c2ecf20Sopenharmony_ci * kobject, we don't have to set a parent for the kobject, the kobject 2198c2ecf20Sopenharmony_ci * will be placed beneath that kset automatically. 2208c2ecf20Sopenharmony_ci */ 2218c2ecf20Sopenharmony_ci retval = kobject_init_and_add(&foo->kobj, &foo_ktype, NULL, "%s", name); 2228c2ecf20Sopenharmony_ci if (retval) { 2238c2ecf20Sopenharmony_ci kobject_put(&foo->kobj); 2248c2ecf20Sopenharmony_ci return NULL; 2258c2ecf20Sopenharmony_ci } 2268c2ecf20Sopenharmony_ci 2278c2ecf20Sopenharmony_ci /* 2288c2ecf20Sopenharmony_ci * We are always responsible for sending the uevent that the kobject 2298c2ecf20Sopenharmony_ci * was added to the system. 2308c2ecf20Sopenharmony_ci */ 2318c2ecf20Sopenharmony_ci kobject_uevent(&foo->kobj, KOBJ_ADD); 2328c2ecf20Sopenharmony_ci 2338c2ecf20Sopenharmony_ci return foo; 2348c2ecf20Sopenharmony_ci} 2358c2ecf20Sopenharmony_ci 2368c2ecf20Sopenharmony_cistatic void destroy_foo_obj(struct foo_obj *foo) 2378c2ecf20Sopenharmony_ci{ 2388c2ecf20Sopenharmony_ci kobject_put(&foo->kobj); 2398c2ecf20Sopenharmony_ci} 2408c2ecf20Sopenharmony_ci 2418c2ecf20Sopenharmony_cistatic int __init example_init(void) 2428c2ecf20Sopenharmony_ci{ 2438c2ecf20Sopenharmony_ci /* 2448c2ecf20Sopenharmony_ci * Create a kset with the name of "kset_example", 2458c2ecf20Sopenharmony_ci * located under /sys/kernel/ 2468c2ecf20Sopenharmony_ci */ 2478c2ecf20Sopenharmony_ci example_kset = kset_create_and_add("kset_example", NULL, kernel_kobj); 2488c2ecf20Sopenharmony_ci if (!example_kset) 2498c2ecf20Sopenharmony_ci return -ENOMEM; 2508c2ecf20Sopenharmony_ci 2518c2ecf20Sopenharmony_ci /* 2528c2ecf20Sopenharmony_ci * Create three objects and register them with our kset 2538c2ecf20Sopenharmony_ci */ 2548c2ecf20Sopenharmony_ci foo_obj = create_foo_obj("foo"); 2558c2ecf20Sopenharmony_ci if (!foo_obj) 2568c2ecf20Sopenharmony_ci goto foo_error; 2578c2ecf20Sopenharmony_ci 2588c2ecf20Sopenharmony_ci bar_obj = create_foo_obj("bar"); 2598c2ecf20Sopenharmony_ci if (!bar_obj) 2608c2ecf20Sopenharmony_ci goto bar_error; 2618c2ecf20Sopenharmony_ci 2628c2ecf20Sopenharmony_ci baz_obj = create_foo_obj("baz"); 2638c2ecf20Sopenharmony_ci if (!baz_obj) 2648c2ecf20Sopenharmony_ci goto baz_error; 2658c2ecf20Sopenharmony_ci 2668c2ecf20Sopenharmony_ci return 0; 2678c2ecf20Sopenharmony_ci 2688c2ecf20Sopenharmony_cibaz_error: 2698c2ecf20Sopenharmony_ci destroy_foo_obj(bar_obj); 2708c2ecf20Sopenharmony_cibar_error: 2718c2ecf20Sopenharmony_ci destroy_foo_obj(foo_obj); 2728c2ecf20Sopenharmony_cifoo_error: 2738c2ecf20Sopenharmony_ci kset_unregister(example_kset); 2748c2ecf20Sopenharmony_ci return -EINVAL; 2758c2ecf20Sopenharmony_ci} 2768c2ecf20Sopenharmony_ci 2778c2ecf20Sopenharmony_cistatic void __exit example_exit(void) 2788c2ecf20Sopenharmony_ci{ 2798c2ecf20Sopenharmony_ci destroy_foo_obj(baz_obj); 2808c2ecf20Sopenharmony_ci destroy_foo_obj(bar_obj); 2818c2ecf20Sopenharmony_ci destroy_foo_obj(foo_obj); 2828c2ecf20Sopenharmony_ci kset_unregister(example_kset); 2838c2ecf20Sopenharmony_ci} 2848c2ecf20Sopenharmony_ci 2858c2ecf20Sopenharmony_cimodule_init(example_init); 2868c2ecf20Sopenharmony_cimodule_exit(example_exit); 2878c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2"); 2888c2ecf20Sopenharmony_ciMODULE_AUTHOR("Greg Kroah-Hartman <greg@kroah.com>"); 289