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