^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Sample kobject implementation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2004-2007 Greg Kroah-Hartman <greg@kroah.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (C) 2007 Novell Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/kobject.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * This module shows how to create a simple subdirectory in sysfs called
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * /sys/kernel/kobject-example In that directory, 3 files are created:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * "foo", "baz", and "bar". If an integer is written to these files, it can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * later read out of it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) static int foo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) static int baz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) static int bar;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * The "foo" file where a static variable is read from and written to.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) static ssize_t foo_show(struct kobject *kobj, struct kobj_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) return sprintf(buf, "%d\n", foo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) static ssize_t foo_store(struct kobject *kobj, struct kobj_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) ret = kstrtoint(buf, 10, &foo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) /* Sysfs attributes cannot be world-writable. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) static struct kobj_attribute foo_attribute =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) __ATTR(foo, 0664, foo_show, foo_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * More complex function where we determine which variable is being accessed by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * looking at the attribute for the "baz" and "bar" files.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) static ssize_t b_show(struct kobject *kobj, struct kobj_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) int var;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) if (strcmp(attr->attr.name, "baz") == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) var = baz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) var = bar;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) return sprintf(buf, "%d\n", var);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) static ssize_t b_store(struct kobject *kobj, struct kobj_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) int var, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) ret = kstrtoint(buf, 10, &var);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) if (strcmp(attr->attr.name, "baz") == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) baz = var;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) bar = var;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) static struct kobj_attribute baz_attribute =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) __ATTR(baz, 0664, b_show, b_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) static struct kobj_attribute bar_attribute =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) __ATTR(bar, 0664, b_show, b_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) * Create a group of attributes so that we can create and destroy them all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) * at once.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) static struct attribute *attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) &foo_attribute.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) &baz_attribute.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) &bar_attribute.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) NULL, /* need to NULL terminate the list of attributes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * An unnamed attribute group will put all of the attributes directly in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) * the kobject directory. If we specify a name, a subdirectory will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) * created for the attributes with the directory being the name of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) * attribute group.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) static struct attribute_group attr_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) .attrs = attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) static struct kobject *example_kobj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) static int __init example_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) * Create a simple kobject with the name of "kobject_example",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) * located under /sys/kernel/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) * As this is a simple directory, no uevent will be sent to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) * userspace. That is why this function should not be used for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) * any type of dynamic kobjects, where the name and number are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) * not known ahead of time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) example_kobj = kobject_create_and_add("kobject_example", kernel_kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) if (!example_kobj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) /* Create the files associated with this kobject */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) retval = sysfs_create_group(example_kobj, &attr_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) kobject_put(example_kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) static void __exit example_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) kobject_put(example_kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) module_init(example_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) module_exit(example_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) MODULE_LICENSE("GPL v2");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) MODULE_AUTHOR("Greg Kroah-Hartman <greg@kroah.com>");