2 * Sample kobject implementation
4 * Copyright (C) 2004-2007 Greg Kroah-Hartman <greg@kroah.com>
5 * Copyright (C) 2007 Novell Inc.
7 * Released under the GPL version 2 only.
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>
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.
28 * The "foo" file where a static variable is read from and written to.
30 static ssize_t
foo_show(struct kobject
*kobj
, struct kobj_attribute
*attr
,
33 return sprintf(buf
, "%d\n", foo
);
36 static ssize_t
foo_store(struct kobject
*kobj
, struct kobj_attribute
*attr
,
37 const char *buf
, size_t count
)
39 sscanf(buf
, "%du", &foo
);
43 /* Sysfs attributes cannot be world-writable. */
44 static struct kobj_attribute foo_attribute
=
45 __ATTR(foo
, 0664, foo_show
, foo_store
);
48 * More complex function where we determine which variable is being accessed by
49 * looking at the attribute for the "baz" and "bar" files.
51 static ssize_t
b_show(struct kobject
*kobj
, struct kobj_attribute
*attr
,
56 if (strcmp(attr
->attr
.name
, "baz") == 0)
60 return sprintf(buf
, "%d\n", var
);
63 static ssize_t
b_store(struct kobject
*kobj
, struct kobj_attribute
*attr
,
64 const char *buf
, size_t count
)
68 sscanf(buf
, "%du", &var
);
69 if (strcmp(attr
->attr
.name
, "baz") == 0)
76 static struct kobj_attribute baz_attribute
=
77 __ATTR(baz
, 0664, b_show
, b_store
);
78 static struct kobj_attribute bar_attribute
=
79 __ATTR(bar
, 0664, b_show
, b_store
);
83 * Create a group of attributes so that we can create and destroy them all
86 static struct attribute
*attrs
[] = {
90 NULL
, /* need to NULL terminate the list of attributes */
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
99 static struct attribute_group attr_group
= {
103 static struct kobject
*example_kobj
;
105 static int __init
example_init(void)
110 * Create a simple kobject with the name of "kobject_example",
111 * located under /sys/kernel/
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.
118 example_kobj
= kobject_create_and_add("kobject_example", kernel_kobj
);
122 /* Create the files associated with this kobject */
123 retval
= sysfs_create_group(example_kobj
, &attr_group
);
125 kobject_put(example_kobj
);
130 static void __exit
example_exit(void)
132 kobject_put(example_kobj
);
135 module_init(example_init
);
136 module_exit(example_exit
);
137 MODULE_LICENSE("GPL");
138 MODULE_AUTHOR("Greg Kroah-Hartman <greg@kroah.com>");