Greg Kroah-Hartman | 40efcb0 | 2007-11-27 11:28:26 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 23 | static int foo; |
| 24 | static int baz; |
| 25 | static int bar; |
| 26 | |
| 27 | /* |
| 28 | * The "foo" file where a static variable is read from and written to. |
| 29 | */ |
| 30 | static ssize_t foo_show(struct kobject *kobj, struct kobj_attribute *attr, |
| 31 | char *buf) |
| 32 | { |
| 33 | return sprintf(buf, "%d\n", foo); |
| 34 | } |
| 35 | |
| 36 | static ssize_t foo_store(struct kobject *kobj, struct kobj_attribute *attr, |
| 37 | const char *buf, size_t count) |
| 38 | { |
Rastislav Barlik | 5fd637e | 2014-12-17 21:14:48 +0100 | [diff] [blame] | 39 | int ret; |
| 40 | |
| 41 | ret = kstrtoint(buf, 10, &foo); |
| 42 | if (ret < 0) |
| 43 | return ret; |
| 44 | |
Greg Kroah-Hartman | 40efcb0 | 2007-11-27 11:28:26 -0800 | [diff] [blame] | 45 | return count; |
| 46 | } |
| 47 | |
Rusty Russell | de51098 | 2014-05-14 10:33:50 +0930 | [diff] [blame] | 48 | /* Sysfs attributes cannot be world-writable. */ |
Greg Kroah-Hartman | 40efcb0 | 2007-11-27 11:28:26 -0800 | [diff] [blame] | 49 | static struct kobj_attribute foo_attribute = |
Rusty Russell | de51098 | 2014-05-14 10:33:50 +0930 | [diff] [blame] | 50 | __ATTR(foo, 0664, foo_show, foo_store); |
Greg Kroah-Hartman | 40efcb0 | 2007-11-27 11:28:26 -0800 | [diff] [blame] | 51 | |
| 52 | /* |
Radu Voicilas | a115bc0 | 2009-12-12 01:06:09 -0800 | [diff] [blame] | 53 | * More complex function where we determine which variable is being accessed by |
Greg Kroah-Hartman | 40efcb0 | 2007-11-27 11:28:26 -0800 | [diff] [blame] | 54 | * looking at the attribute for the "baz" and "bar" files. |
| 55 | */ |
| 56 | static ssize_t b_show(struct kobject *kobj, struct kobj_attribute *attr, |
| 57 | char *buf) |
| 58 | { |
| 59 | int var; |
| 60 | |
| 61 | if (strcmp(attr->attr.name, "baz") == 0) |
| 62 | var = baz; |
| 63 | else |
| 64 | var = bar; |
| 65 | return sprintf(buf, "%d\n", var); |
| 66 | } |
| 67 | |
| 68 | static ssize_t b_store(struct kobject *kobj, struct kobj_attribute *attr, |
| 69 | const char *buf, size_t count) |
| 70 | { |
Rastislav Barlik | 5fd637e | 2014-12-17 21:14:48 +0100 | [diff] [blame] | 71 | int var, ret; |
Greg Kroah-Hartman | 40efcb0 | 2007-11-27 11:28:26 -0800 | [diff] [blame] | 72 | |
Rastislav Barlik | 5fd637e | 2014-12-17 21:14:48 +0100 | [diff] [blame] | 73 | ret = kstrtoint(buf, 10, &var); |
| 74 | if (ret < 0) |
| 75 | return ret; |
| 76 | |
Greg Kroah-Hartman | 40efcb0 | 2007-11-27 11:28:26 -0800 | [diff] [blame] | 77 | if (strcmp(attr->attr.name, "baz") == 0) |
| 78 | baz = var; |
| 79 | else |
| 80 | bar = var; |
| 81 | return count; |
| 82 | } |
| 83 | |
| 84 | static struct kobj_attribute baz_attribute = |
Rusty Russell | de51098 | 2014-05-14 10:33:50 +0930 | [diff] [blame] | 85 | __ATTR(baz, 0664, b_show, b_store); |
Greg Kroah-Hartman | 40efcb0 | 2007-11-27 11:28:26 -0800 | [diff] [blame] | 86 | static struct kobj_attribute bar_attribute = |
Rusty Russell | de51098 | 2014-05-14 10:33:50 +0930 | [diff] [blame] | 87 | __ATTR(bar, 0664, b_show, b_store); |
Greg Kroah-Hartman | 40efcb0 | 2007-11-27 11:28:26 -0800 | [diff] [blame] | 88 | |
| 89 | |
| 90 | /* |
Radu Voicilas | a115bc0 | 2009-12-12 01:06:09 -0800 | [diff] [blame] | 91 | * Create a group of attributes so that we can create and destroy them all |
Greg Kroah-Hartman | 40efcb0 | 2007-11-27 11:28:26 -0800 | [diff] [blame] | 92 | * at once. |
| 93 | */ |
| 94 | static struct attribute *attrs[] = { |
| 95 | &foo_attribute.attr, |
| 96 | &baz_attribute.attr, |
| 97 | &bar_attribute.attr, |
| 98 | NULL, /* need to NULL terminate the list of attributes */ |
| 99 | }; |
| 100 | |
| 101 | /* |
| 102 | * An unnamed attribute group will put all of the attributes directly in |
| 103 | * the kobject directory. If we specify a name, a subdirectory will be |
| 104 | * created for the attributes with the directory being the name of the |
| 105 | * attribute group. |
| 106 | */ |
| 107 | static struct attribute_group attr_group = { |
| 108 | .attrs = attrs, |
| 109 | }; |
| 110 | |
| 111 | static struct kobject *example_kobj; |
| 112 | |
Qinghuang Feng | 7ec7fb3 | 2009-01-06 14:40:52 -0800 | [diff] [blame] | 113 | static int __init example_init(void) |
Greg Kroah-Hartman | 40efcb0 | 2007-11-27 11:28:26 -0800 | [diff] [blame] | 114 | { |
| 115 | int retval; |
| 116 | |
| 117 | /* |
| 118 | * Create a simple kobject with the name of "kobject_example", |
| 119 | * located under /sys/kernel/ |
| 120 | * |
| 121 | * As this is a simple directory, no uevent will be sent to |
| 122 | * userspace. That is why this function should not be used for |
| 123 | * any type of dynamic kobjects, where the name and number are |
| 124 | * not known ahead of time. |
| 125 | */ |
| 126 | example_kobj = kobject_create_and_add("kobject_example", kernel_kobj); |
| 127 | if (!example_kobj) |
| 128 | return -ENOMEM; |
| 129 | |
| 130 | /* Create the files associated with this kobject */ |
| 131 | retval = sysfs_create_group(example_kobj, &attr_group); |
| 132 | if (retval) |
| 133 | kobject_put(example_kobj); |
| 134 | |
| 135 | return retval; |
| 136 | } |
| 137 | |
Qinghuang Feng | 7ec7fb3 | 2009-01-06 14:40:52 -0800 | [diff] [blame] | 138 | static void __exit example_exit(void) |
Greg Kroah-Hartman | 40efcb0 | 2007-11-27 11:28:26 -0800 | [diff] [blame] | 139 | { |
| 140 | kobject_put(example_kobj); |
| 141 | } |
| 142 | |
| 143 | module_init(example_init); |
| 144 | module_exit(example_exit); |
Greg Kroah-Hartman | 07afb6a | 2015-03-25 13:41:42 +0100 | [diff] [blame] | 145 | MODULE_LICENSE("GPL v2"); |
Greg Kroah-Hartman | 40efcb0 | 2007-11-27 11:28:26 -0800 | [diff] [blame] | 146 | MODULE_AUTHOR("Greg Kroah-Hartman <greg@kroah.com>"); |