18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Sample kobject 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/module.h> 128c2ecf20Sopenharmony_ci#include <linux/init.h> 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_ci/* 158c2ecf20Sopenharmony_ci * This module shows how to create a simple subdirectory in sysfs called 168c2ecf20Sopenharmony_ci * /sys/kernel/kobject-example In that directory, 3 files are created: 178c2ecf20Sopenharmony_ci * "foo", "baz", and "bar". If an integer is written to these files, it can be 188c2ecf20Sopenharmony_ci * later read out of it. 198c2ecf20Sopenharmony_ci */ 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_cistatic int foo; 228c2ecf20Sopenharmony_cistatic int baz; 238c2ecf20Sopenharmony_cistatic int bar; 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_ci/* 268c2ecf20Sopenharmony_ci * The "foo" file where a static variable is read from and written to. 278c2ecf20Sopenharmony_ci */ 288c2ecf20Sopenharmony_cistatic ssize_t foo_show(struct kobject *kobj, struct kobj_attribute *attr, 298c2ecf20Sopenharmony_ci char *buf) 308c2ecf20Sopenharmony_ci{ 318c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", foo); 328c2ecf20Sopenharmony_ci} 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_cistatic ssize_t foo_store(struct kobject *kobj, struct kobj_attribute *attr, 358c2ecf20Sopenharmony_ci const char *buf, size_t count) 368c2ecf20Sopenharmony_ci{ 378c2ecf20Sopenharmony_ci int ret; 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_ci ret = kstrtoint(buf, 10, &foo); 408c2ecf20Sopenharmony_ci if (ret < 0) 418c2ecf20Sopenharmony_ci return ret; 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_ci return count; 448c2ecf20Sopenharmony_ci} 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_ci/* Sysfs attributes cannot be world-writable. */ 478c2ecf20Sopenharmony_cistatic struct kobj_attribute foo_attribute = 488c2ecf20Sopenharmony_ci __ATTR(foo, 0664, foo_show, foo_store); 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ci/* 518c2ecf20Sopenharmony_ci * More complex function where we determine which variable is being accessed by 528c2ecf20Sopenharmony_ci * looking at the attribute for the "baz" and "bar" files. 538c2ecf20Sopenharmony_ci */ 548c2ecf20Sopenharmony_cistatic ssize_t b_show(struct kobject *kobj, struct kobj_attribute *attr, 558c2ecf20Sopenharmony_ci char *buf) 568c2ecf20Sopenharmony_ci{ 578c2ecf20Sopenharmony_ci int var; 588c2ecf20Sopenharmony_ci 598c2ecf20Sopenharmony_ci if (strcmp(attr->attr.name, "baz") == 0) 608c2ecf20Sopenharmony_ci var = baz; 618c2ecf20Sopenharmony_ci else 628c2ecf20Sopenharmony_ci var = bar; 638c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", var); 648c2ecf20Sopenharmony_ci} 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_cistatic ssize_t b_store(struct kobject *kobj, struct kobj_attribute *attr, 678c2ecf20Sopenharmony_ci const char *buf, size_t count) 688c2ecf20Sopenharmony_ci{ 698c2ecf20Sopenharmony_ci int var, ret; 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_ci ret = kstrtoint(buf, 10, &var); 728c2ecf20Sopenharmony_ci if (ret < 0) 738c2ecf20Sopenharmony_ci return ret; 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ci if (strcmp(attr->attr.name, "baz") == 0) 768c2ecf20Sopenharmony_ci baz = var; 778c2ecf20Sopenharmony_ci else 788c2ecf20Sopenharmony_ci bar = var; 798c2ecf20Sopenharmony_ci return count; 808c2ecf20Sopenharmony_ci} 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_cistatic struct kobj_attribute baz_attribute = 838c2ecf20Sopenharmony_ci __ATTR(baz, 0664, b_show, b_store); 848c2ecf20Sopenharmony_cistatic struct kobj_attribute bar_attribute = 858c2ecf20Sopenharmony_ci __ATTR(bar, 0664, b_show, b_store); 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ci/* 898c2ecf20Sopenharmony_ci * Create a group of attributes so that we can create and destroy them all 908c2ecf20Sopenharmony_ci * at once. 918c2ecf20Sopenharmony_ci */ 928c2ecf20Sopenharmony_cistatic struct attribute *attrs[] = { 938c2ecf20Sopenharmony_ci &foo_attribute.attr, 948c2ecf20Sopenharmony_ci &baz_attribute.attr, 958c2ecf20Sopenharmony_ci &bar_attribute.attr, 968c2ecf20Sopenharmony_ci NULL, /* need to NULL terminate the list of attributes */ 978c2ecf20Sopenharmony_ci}; 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_ci/* 1008c2ecf20Sopenharmony_ci * An unnamed attribute group will put all of the attributes directly in 1018c2ecf20Sopenharmony_ci * the kobject directory. If we specify a name, a subdirectory will be 1028c2ecf20Sopenharmony_ci * created for the attributes with the directory being the name of the 1038c2ecf20Sopenharmony_ci * attribute group. 1048c2ecf20Sopenharmony_ci */ 1058c2ecf20Sopenharmony_cistatic struct attribute_group attr_group = { 1068c2ecf20Sopenharmony_ci .attrs = attrs, 1078c2ecf20Sopenharmony_ci}; 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_cistatic struct kobject *example_kobj; 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_cistatic int __init example_init(void) 1128c2ecf20Sopenharmony_ci{ 1138c2ecf20Sopenharmony_ci int retval; 1148c2ecf20Sopenharmony_ci 1158c2ecf20Sopenharmony_ci /* 1168c2ecf20Sopenharmony_ci * Create a simple kobject with the name of "kobject_example", 1178c2ecf20Sopenharmony_ci * located under /sys/kernel/ 1188c2ecf20Sopenharmony_ci * 1198c2ecf20Sopenharmony_ci * As this is a simple directory, no uevent will be sent to 1208c2ecf20Sopenharmony_ci * userspace. That is why this function should not be used for 1218c2ecf20Sopenharmony_ci * any type of dynamic kobjects, where the name and number are 1228c2ecf20Sopenharmony_ci * not known ahead of time. 1238c2ecf20Sopenharmony_ci */ 1248c2ecf20Sopenharmony_ci example_kobj = kobject_create_and_add("kobject_example", kernel_kobj); 1258c2ecf20Sopenharmony_ci if (!example_kobj) 1268c2ecf20Sopenharmony_ci return -ENOMEM; 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_ci /* Create the files associated with this kobject */ 1298c2ecf20Sopenharmony_ci retval = sysfs_create_group(example_kobj, &attr_group); 1308c2ecf20Sopenharmony_ci if (retval) 1318c2ecf20Sopenharmony_ci kobject_put(example_kobj); 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_ci return retval; 1348c2ecf20Sopenharmony_ci} 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_cistatic void __exit example_exit(void) 1378c2ecf20Sopenharmony_ci{ 1388c2ecf20Sopenharmony_ci kobject_put(example_kobj); 1398c2ecf20Sopenharmony_ci} 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_cimodule_init(example_init); 1428c2ecf20Sopenharmony_cimodule_exit(example_exit); 1438c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2"); 1448c2ecf20Sopenharmony_ciMODULE_AUTHOR("Greg Kroah-Hartman <greg@kroah.com>"); 145