blob: 9e383fdbaa00d2b55ff6f41773ce9b3df342e6cf [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.
Greg Kroah-Hartman40efcb02007-11-27 11:28:26 -08007 */
8#include <linux/kobject.h>
9#include <linux/string.h>
10#include <linux/sysfs.h>
11#include <linux/module.h>
12#include <linux/init.h>
13
14/*
15 * This module shows how to create a simple subdirectory in sysfs called
16 * /sys/kernel/kobject-example In that directory, 3 files are created:
17 * "foo", "baz", and "bar". If an integer is written to these files, it can be
18 * later read out of it.
19 */
20
21static int foo;
22static int baz;
23static int bar;
24
25/*
26 * The "foo" file where a static variable is read from and written to.
27 */
28static ssize_t foo_show(struct kobject *kobj, struct kobj_attribute *attr,
29 char *buf)
30{
31 return sprintf(buf, "%d\n", foo);
32}
33
34static ssize_t foo_store(struct kobject *kobj, struct kobj_attribute *attr,
35 const char *buf, size_t count)
36{
Rastislav Barlik5fd637e2014-12-17 21:14:48 +010037 int ret;
38
39 ret = kstrtoint(buf, 10, &foo);
40 if (ret < 0)
41 return ret;
42
Greg Kroah-Hartman40efcb02007-11-27 11:28:26 -080043 return count;
44}
45
Rusty Russellde510982014-05-14 10:33:50 +093046/* Sysfs attributes cannot be world-writable. */
Greg Kroah-Hartman40efcb02007-11-27 11:28:26 -080047static struct kobj_attribute foo_attribute =
Rusty Russellde510982014-05-14 10:33:50 +093048 __ATTR(foo, 0664, foo_show, foo_store);
Greg Kroah-Hartman40efcb02007-11-27 11:28:26 -080049
50/*
Radu Voicilasa115bc02009-12-12 01:06:09 -080051 * More complex function where we determine which variable is being accessed by
Greg Kroah-Hartman40efcb02007-11-27 11:28:26 -080052 * looking at the attribute for the "baz" and "bar" files.
53 */
54static ssize_t b_show(struct kobject *kobj, struct kobj_attribute *attr,
55 char *buf)
56{
57 int var;
58
59 if (strcmp(attr->attr.name, "baz") == 0)
60 var = baz;
61 else
62 var = bar;
63 return sprintf(buf, "%d\n", var);
64}
65
66static ssize_t b_store(struct kobject *kobj, struct kobj_attribute *attr,
67 const char *buf, size_t count)
68{
Rastislav Barlik5fd637e2014-12-17 21:14:48 +010069 int var, ret;
Greg Kroah-Hartman40efcb02007-11-27 11:28:26 -080070
Rastislav Barlik5fd637e2014-12-17 21:14:48 +010071 ret = kstrtoint(buf, 10, &var);
72 if (ret < 0)
73 return ret;
74
Greg Kroah-Hartman40efcb02007-11-27 11:28:26 -080075 if (strcmp(attr->attr.name, "baz") == 0)
76 baz = var;
77 else
78 bar = var;
79 return count;
80}
81
82static struct kobj_attribute baz_attribute =
Rusty Russellde510982014-05-14 10:33:50 +093083 __ATTR(baz, 0664, b_show, b_store);
Greg Kroah-Hartman40efcb02007-11-27 11:28:26 -080084static struct kobj_attribute bar_attribute =
Rusty Russellde510982014-05-14 10:33:50 +093085 __ATTR(bar, 0664, b_show, b_store);
Greg Kroah-Hartman40efcb02007-11-27 11:28:26 -080086
87
88/*
Radu Voicilasa115bc02009-12-12 01:06:09 -080089 * Create a group of attributes so that we can create and destroy them all
Greg Kroah-Hartman40efcb02007-11-27 11:28:26 -080090 * at once.
91 */
92static struct attribute *attrs[] = {
93 &foo_attribute.attr,
94 &baz_attribute.attr,
95 &bar_attribute.attr,
96 NULL, /* need to NULL terminate the list of attributes */
97};
98
99/*
100 * An unnamed attribute group will put all of the attributes directly in
101 * the kobject directory. If we specify a name, a subdirectory will be
102 * created for the attributes with the directory being the name of the
103 * attribute group.
104 */
105static struct attribute_group attr_group = {
106 .attrs = attrs,
107};
108
109static struct kobject *example_kobj;
110
Qinghuang Feng7ec7fb32009-01-06 14:40:52 -0800111static int __init example_init(void)
Greg Kroah-Hartman40efcb02007-11-27 11:28:26 -0800112{
113 int retval;
114
115 /*
116 * Create a simple kobject with the name of "kobject_example",
117 * located under /sys/kernel/
118 *
119 * As this is a simple directory, no uevent will be sent to
120 * userspace. That is why this function should not be used for
121 * any type of dynamic kobjects, where the name and number are
122 * not known ahead of time.
123 */
124 example_kobj = kobject_create_and_add("kobject_example", kernel_kobj);
125 if (!example_kobj)
126 return -ENOMEM;
127
128 /* Create the files associated with this kobject */
129 retval = sysfs_create_group(example_kobj, &attr_group);
130 if (retval)
131 kobject_put(example_kobj);
132
133 return retval;
134}
135
Qinghuang Feng7ec7fb32009-01-06 14:40:52 -0800136static void __exit example_exit(void)
Greg Kroah-Hartman40efcb02007-11-27 11:28:26 -0800137{
138 kobject_put(example_kobj);
139}
140
141module_init(example_init);
142module_exit(example_exit);
Greg Kroah-Hartman07afb6a2015-03-25 13:41:42 +0100143MODULE_LICENSE("GPL v2");
Greg Kroah-Hartman40efcb02007-11-27 11:28:26 -0800144MODULE_AUTHOR("Greg Kroah-Hartman <greg@kroah.com>");