1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Sample kset and ktype implementation 4 * 5 * Copyright (C) 2004-2007 Greg Kroah-Hartman <greg@kroah.com> 6 * Copyright (C) 2007 Novell Inc. 7 * 8 * Released under the GPL version 2 only. 9 * 10 */ 11 #include <linux/kobject.h> 12 #include <linux/string.h> 13 #include <linux/sysfs.h> 14 #include <linux/slab.h> 15 #include <linux/module.h> 16 #include <linux/init.h> 17 18 /* 19 * This module shows how to create a kset in sysfs called 20 * /sys/kernel/kset-example 21 * Then tree kobjects are created and assigned to this kset, "foo", "baz", 22 * and "bar". In those kobjects, attributes of the same name are also 23 * created and if an integer is written to these files, it can be later 24 * read out of it. 25 */ 26 27 28 /* 29 * This is our "object" that we will create a few of and register them with 30 * sysfs. 31 */ 32 struct foo_obj { 33 struct kobject kobj; 34 int foo; 35 int baz; 36 int bar; 37 }; 38 #define to_foo_obj(x) container_of(x, struct foo_obj, kobj) 39 40 /* a custom attribute that works just for a struct foo_obj. */ 41 struct foo_attribute { 42 struct attribute attr; 43 ssize_t (*show)(struct foo_obj *foo, struct foo_attribute *attr, char *buf); 44 ssize_t (*store)(struct foo_obj *foo, struct foo_attribute *attr, const char *buf, size_t count); 45 }; 46 #define to_foo_attr(x) container_of(x, struct foo_attribute, attr) 47 48 /* 49 * The default show function that must be passed to sysfs. This will be 50 * called by sysfs for whenever a show function is called by the user on a 51 * sysfs file associated with the kobjects we have registered. We need to 52 * transpose back from a "default" kobject to our custom struct foo_obj and 53 * then call the show function for that specific object. 54 */ 55 static ssize_t foo_attr_show(struct kobject *kobj, 56 struct attribute *attr, 57 char *buf) 58 { 59 struct foo_attribute *attribute; 60 struct foo_obj *foo; 61 62 attribute = to_foo_attr(attr); 63 foo = to_foo_obj(kobj); 64 65 if (!attribute->show) 66 return -EIO; 67 68 return attribute->show(foo, attribute, buf); 69 } 70 71 /* 72 * Just like the default show function above, but this one is for when the 73 * sysfs "store" is requested (when a value is written to a file.) 74 */ 75 static ssize_t foo_attr_store(struct kobject *kobj, 76 struct attribute *attr, 77 const char *buf, size_t len) 78 { 79 struct foo_attribute *attribute; 80 struct foo_obj *foo; 81 82 attribute = to_foo_attr(attr); 83 foo = to_foo_obj(kobj); 84 85 if (!attribute->store) 86 return -EIO; 87 88 return attribute->store(foo, attribute, buf, len); 89 } 90 91 /* Our custom sysfs_ops that we will associate with our ktype later on */ 92 static const struct sysfs_ops foo_sysfs_ops = { 93 .show = foo_attr_show, 94 .store = foo_attr_store, 95 }; 96 97 /* 98 * The release function for our object. This is REQUIRED by the kernel to 99 * have. We free the memory held in our object here. 100 * 101 * NEVER try to get away with just a "blank" release function to try to be 102 * smarter than the kernel. Turns out, no one ever is... 103 */ 104 static void foo_release(struct kobject *kobj) 105 { 106 struct foo_obj *foo; 107 108 foo = to_foo_obj(kobj); 109 kfree(foo); 110 } 111 112 /* 113 * The "foo" file where the .foo variable is read from and written to. 114 */ 115 static ssize_t foo_show(struct foo_obj *foo_obj, struct foo_attribute *attr, 116 char *buf) 117 { 118 return sprintf(buf, "%d\n", foo_obj->foo); 119 } 120 121 static ssize_t foo_store(struct foo_obj *foo_obj, struct foo_attribute *attr, 122 const char *buf, size_t count) 123 { 124 int ret; 125 126 ret = kstrtoint(buf, 10, &foo_obj->foo); 127 if (ret < 0) 128 return ret; 129 130 return count; 131 } 132 133 /* Sysfs attributes cannot be world-writable. */ 134 static struct foo_attribute foo_attribute = 135 __ATTR(foo, 0664, foo_show, foo_store); 136 137 /* 138 * More complex function where we determine which variable is being accessed by 139 * looking at the attribute for the "baz" and "bar" files. 140 */ 141 static ssize_t b_show(struct foo_obj *foo_obj, struct foo_attribute *attr, 142 char *buf) 143 { 144 int var; 145 146 if (strcmp(attr->attr.name, "baz") == 0) 147 var = foo_obj->baz; 148 else 149 var = foo_obj->bar; 150 return sprintf(buf, "%d\n", var); 151 } 152 153 static ssize_t b_store(struct foo_obj *foo_obj, struct foo_attribute *attr, 154 const char *buf, size_t count) 155 { 156 int var, ret; 157 158 ret = kstrtoint(buf, 10, &var); 159 if (ret < 0) 160 return ret; 161 162 if (strcmp(attr->attr.name, "baz") == 0) 163 foo_obj->baz = var; 164 else 165 foo_obj->bar = var; 166 return count; 167 } 168 169 static struct foo_attribute baz_attribute = 170 __ATTR(baz, 0664, b_show, b_store); 171 static struct foo_attribute bar_attribute = 172 __ATTR(bar, 0664, b_show, b_store); 173 174 /* 175 * Create a group of attributes so that we can create and destroy them all 176 * at once. 177 */ 178 static struct attribute *foo_default_attrs[] = { 179 &foo_attribute.attr, 180 &baz_attribute.attr, 181 &bar_attribute.attr, 182 NULL, /* need to NULL terminate the list of attributes */ 183 }; 184 185 /* 186 * Our own ktype for our kobjects. Here we specify our sysfs ops, the 187 * release function, and the set of default attributes we want created 188 * whenever a kobject of this type is registered with the kernel. 189 */ 190 static struct kobj_type foo_ktype = { 191 .sysfs_ops = &foo_sysfs_ops, 192 .release = foo_release, 193 .default_attrs = foo_default_attrs, 194 }; 195 196 static struct kset *example_kset; 197 static struct foo_obj *foo_obj; 198 static struct foo_obj *bar_obj; 199 static struct foo_obj *baz_obj; 200 201 static struct foo_obj *create_foo_obj(const char *name) 202 { 203 struct foo_obj *foo; 204 int retval; 205 206 /* allocate the memory for the whole object */ 207 foo = kzalloc(sizeof(*foo), GFP_KERNEL); 208 if (!foo) 209 return NULL; 210 211 /* 212 * As we have a kset for this kobject, we need to set it before calling 213 * the kobject core. 214 */ 215 foo->kobj.kset = example_kset; 216 217 /* 218 * Initialize and add the kobject to the kernel. All the default files 219 * will be created here. As we have already specified a kset for this 220 * kobject, we don't have to set a parent for the kobject, the kobject 221 * will be placed beneath that kset automatically. 222 */ 223 retval = kobject_init_and_add(&foo->kobj, &foo_ktype, NULL, "%s", name); 224 if (retval) { 225 kobject_put(&foo->kobj); 226 return NULL; 227 } 228 229 /* 230 * We are always responsible for sending the uevent that the kobject 231 * was added to the system. 232 */ 233 kobject_uevent(&foo->kobj, KOBJ_ADD); 234 235 return foo; 236 } 237 238 static void destroy_foo_obj(struct foo_obj *foo) 239 { 240 kobject_put(&foo->kobj); 241 } 242 243 static int __init example_init(void) 244 { 245 /* 246 * Create a kset with the name of "kset_example", 247 * located under /sys/kernel/ 248 */ 249 example_kset = kset_create_and_add("kset_example", NULL, kernel_kobj); 250 if (!example_kset) 251 return -ENOMEM; 252 253 /* 254 * Create three objects and register them with our kset 255 */ 256 foo_obj = create_foo_obj("foo"); 257 if (!foo_obj) 258 goto foo_error; 259 260 bar_obj = create_foo_obj("bar"); 261 if (!bar_obj) 262 goto bar_error; 263 264 baz_obj = create_foo_obj("baz"); 265 if (!baz_obj) 266 goto baz_error; 267 268 return 0; 269 270 baz_error: 271 destroy_foo_obj(bar_obj); 272 bar_error: 273 destroy_foo_obj(foo_obj); 274 foo_error: 275 kset_unregister(example_kset); 276 return -EINVAL; 277 } 278 279 static void __exit example_exit(void) 280 { 281 destroy_foo_obj(baz_obj); 282 destroy_foo_obj(bar_obj); 283 destroy_foo_obj(foo_obj); 284 kset_unregister(example_kset); 285 } 286 287 module_init(example_init); 288 module_exit(example_exit); 289 MODULE_LICENSE("GPL v2"); 290 MODULE_AUTHOR("Greg Kroah-Hartman <greg@kroah.com>"); 291