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