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