Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Sample kobject implementation
0004  *
0005  * Copyright (C) 2004-2007 Greg Kroah-Hartman <greg@kroah.com>
0006  * Copyright (C) 2007 Novell Inc.
0007  */
0008 #include <linux/kobject.h>
0009 #include <linux/string.h>
0010 #include <linux/sysfs.h>
0011 #include <linux/module.h>
0012 #include <linux/init.h>
0013 
0014 /*
0015  * This module shows how to create a simple subdirectory in sysfs called
0016  * /sys/kernel/kobject-example  In that directory, 3 files are created:
0017  * "foo", "baz", and "bar".  If an integer is written to these files, it can be
0018  * later read out of it.
0019  */
0020 
0021 static int foo;
0022 static int baz;
0023 static int bar;
0024 
0025 /*
0026  * The "foo" file where a static variable is read from and written to.
0027  */
0028 static ssize_t foo_show(struct kobject *kobj, struct kobj_attribute *attr,
0029             char *buf)
0030 {
0031     return sysfs_emit(buf, "%d\n", foo);
0032 }
0033 
0034 static ssize_t foo_store(struct kobject *kobj, struct kobj_attribute *attr,
0035              const char *buf, size_t count)
0036 {
0037     int ret;
0038 
0039     ret = kstrtoint(buf, 10, &foo);
0040     if (ret < 0)
0041         return ret;
0042 
0043     return count;
0044 }
0045 
0046 /* Sysfs attributes cannot be world-writable. */
0047 static struct kobj_attribute foo_attribute =
0048     __ATTR(foo, 0664, foo_show, foo_store);
0049 
0050 /*
0051  * More complex function where we determine which variable is being accessed by
0052  * looking at the attribute for the "baz" and "bar" files.
0053  */
0054 static ssize_t b_show(struct kobject *kobj, struct kobj_attribute *attr,
0055               char *buf)
0056 {
0057     int var;
0058 
0059     if (strcmp(attr->attr.name, "baz") == 0)
0060         var = baz;
0061     else
0062         var = bar;
0063     return sysfs_emit(buf, "%d\n", var);
0064 }
0065 
0066 static ssize_t b_store(struct kobject *kobj, struct kobj_attribute *attr,
0067                const char *buf, size_t count)
0068 {
0069     int var, ret;
0070 
0071     ret = kstrtoint(buf, 10, &var);
0072     if (ret < 0)
0073         return ret;
0074 
0075     if (strcmp(attr->attr.name, "baz") == 0)
0076         baz = var;
0077     else
0078         bar = var;
0079     return count;
0080 }
0081 
0082 static struct kobj_attribute baz_attribute =
0083     __ATTR(baz, 0664, b_show, b_store);
0084 static struct kobj_attribute bar_attribute =
0085     __ATTR(bar, 0664, b_show, b_store);
0086 
0087 
0088 /*
0089  * Create a group of attributes so that we can create and destroy them all
0090  * at once.
0091  */
0092 static struct attribute *attrs[] = {
0093     &foo_attribute.attr,
0094     &baz_attribute.attr,
0095     &bar_attribute.attr,
0096     NULL,   /* need to NULL terminate the list of attributes */
0097 };
0098 
0099 /*
0100  * An unnamed attribute group will put all of the attributes directly in
0101  * the kobject directory.  If we specify a name, a subdirectory will be
0102  * created for the attributes with the directory being the name of the
0103  * attribute group.
0104  */
0105 static struct attribute_group attr_group = {
0106     .attrs = attrs,
0107 };
0108 
0109 static struct kobject *example_kobj;
0110 
0111 static int __init example_init(void)
0112 {
0113     int retval;
0114 
0115     /*
0116      * Create a simple kobject with the name of "kobject_example",
0117      * located under /sys/kernel/
0118      *
0119      * As this is a simple directory, no uevent will be sent to
0120      * userspace.  That is why this function should not be used for
0121      * any type of dynamic kobjects, where the name and number are
0122      * not known ahead of time.
0123      */
0124     example_kobj = kobject_create_and_add("kobject_example", kernel_kobj);
0125     if (!example_kobj)
0126         return -ENOMEM;
0127 
0128     /* Create the files associated with this kobject */
0129     retval = sysfs_create_group(example_kobj, &attr_group);
0130     if (retval)
0131         kobject_put(example_kobj);
0132 
0133     return retval;
0134 }
0135 
0136 static void __exit example_exit(void)
0137 {
0138     kobject_put(example_kobj);
0139 }
0140 
0141 module_init(example_init);
0142 module_exit(example_exit);
0143 MODULE_LICENSE("GPL v2");
0144 MODULE_AUTHOR("Greg Kroah-Hartman <greg@kroah.com>");