0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/kobject.h>
0009 #include <linux/string.h>
0010 #include <linux/sysfs.h>
0011 #include <linux/module.h>
0012 #include <linux/init.h>
0013
0014
0015
0016
0017
0018
0019
0020
0021 static int foo;
0022 static int baz;
0023 static int bar;
0024
0025
0026
0027
0028 static ssize_t foo_show(struct kobject *kobj, struct kobj_attribute *attr,
0029 char *buf)
0030 {
0031 return sysfs_emit(buf, "%d\n", foo);
0032 }
0033
0034 static ssize_t foo_store(struct kobject *kobj, struct kobj_attribute *attr,
0035 const char *buf, size_t count)
0036 {
0037 int ret;
0038
0039 ret = kstrtoint(buf, 10, &foo);
0040 if (ret < 0)
0041 return ret;
0042
0043 return count;
0044 }
0045
0046
0047 static struct kobj_attribute foo_attribute =
0048 __ATTR(foo, 0664, foo_show, foo_store);
0049
0050
0051
0052
0053
0054 static ssize_t b_show(struct kobject *kobj, struct kobj_attribute *attr,
0055 char *buf)
0056 {
0057 int var;
0058
0059 if (strcmp(attr->attr.name, "baz") == 0)
0060 var = baz;
0061 else
0062 var = bar;
0063 return sysfs_emit(buf, "%d\n", var);
0064 }
0065
0066 static ssize_t b_store(struct kobject *kobj, struct kobj_attribute *attr,
0067 const char *buf, size_t count)
0068 {
0069 int var, ret;
0070
0071 ret = kstrtoint(buf, 10, &var);
0072 if (ret < 0)
0073 return ret;
0074
0075 if (strcmp(attr->attr.name, "baz") == 0)
0076 baz = var;
0077 else
0078 bar = var;
0079 return count;
0080 }
0081
0082 static struct kobj_attribute baz_attribute =
0083 __ATTR(baz, 0664, b_show, b_store);
0084 static struct kobj_attribute bar_attribute =
0085 __ATTR(bar, 0664, b_show, b_store);
0086
0087
0088
0089
0090
0091
0092 static struct attribute *attrs[] = {
0093 &foo_attribute.attr,
0094 &baz_attribute.attr,
0095 &bar_attribute.attr,
0096 NULL,
0097 };
0098
0099
0100
0101
0102
0103
0104
0105 static struct attribute_group attr_group = {
0106 .attrs = attrs,
0107 };
0108
0109 static struct kobject *example_kobj;
0110
0111 static int __init example_init(void)
0112 {
0113 int retval;
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124 example_kobj = kobject_create_and_add("kobject_example", kernel_kobj);
0125 if (!example_kobj)
0126 return -ENOMEM;
0127
0128
0129 retval = sysfs_create_group(example_kobj, &attr_group);
0130 if (retval)
0131 kobject_put(example_kobj);
0132
0133 return retval;
0134 }
0135
0136 static void __exit example_exit(void)
0137 {
0138 kobject_put(example_kobj);
0139 }
0140
0141 module_init(example_init);
0142 module_exit(example_exit);
0143 MODULE_LICENSE("GPL v2");
0144 MODULE_AUTHOR("Greg Kroah-Hartman <greg@kroah.com>");