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