144bfe16eSGreg Kroah-Hartman /* 244bfe16eSGreg Kroah-Hartman * Sample kset and ktype implementation 344bfe16eSGreg Kroah-Hartman * 444bfe16eSGreg Kroah-Hartman * Copyright (C) 2004-2007 Greg Kroah-Hartman <greg@kroah.com> 544bfe16eSGreg Kroah-Hartman * Copyright (C) 2007 Novell Inc. 644bfe16eSGreg Kroah-Hartman * 744bfe16eSGreg Kroah-Hartman * Released under the GPL version 2 only. 844bfe16eSGreg Kroah-Hartman * 944bfe16eSGreg Kroah-Hartman */ 1044bfe16eSGreg Kroah-Hartman #include <linux/kobject.h> 1144bfe16eSGreg Kroah-Hartman #include <linux/string.h> 1244bfe16eSGreg Kroah-Hartman #include <linux/sysfs.h> 135a0e3ad6STejun Heo #include <linux/slab.h> 1444bfe16eSGreg Kroah-Hartman #include <linux/module.h> 1544bfe16eSGreg Kroah-Hartman #include <linux/init.h> 1644bfe16eSGreg Kroah-Hartman 1744bfe16eSGreg Kroah-Hartman /* 1844bfe16eSGreg Kroah-Hartman * This module shows how to create a kset in sysfs called 1944bfe16eSGreg Kroah-Hartman * /sys/kernel/kset-example 2044bfe16eSGreg Kroah-Hartman * Then tree kobjects are created and assigned to this kset, "foo", "baz", 2144bfe16eSGreg Kroah-Hartman * and "bar". In those kobjects, attributes of the same name are also 2244bfe16eSGreg Kroah-Hartman * created and if an integer is written to these files, it can be later 2344bfe16eSGreg Kroah-Hartman * read out of it. 2444bfe16eSGreg Kroah-Hartman */ 2544bfe16eSGreg Kroah-Hartman 2644bfe16eSGreg Kroah-Hartman 2744bfe16eSGreg Kroah-Hartman /* 2844bfe16eSGreg Kroah-Hartman * This is our "object" that we will create a few of and register them with 2944bfe16eSGreg Kroah-Hartman * sysfs. 3044bfe16eSGreg Kroah-Hartman */ 3144bfe16eSGreg Kroah-Hartman struct foo_obj { 3244bfe16eSGreg Kroah-Hartman struct kobject kobj; 3344bfe16eSGreg Kroah-Hartman int foo; 3444bfe16eSGreg Kroah-Hartman int baz; 3544bfe16eSGreg Kroah-Hartman int bar; 3644bfe16eSGreg Kroah-Hartman }; 3744bfe16eSGreg Kroah-Hartman #define to_foo_obj(x) container_of(x, struct foo_obj, kobj) 3844bfe16eSGreg Kroah-Hartman 3944bfe16eSGreg Kroah-Hartman /* a custom attribute that works just for a struct foo_obj. */ 4044bfe16eSGreg Kroah-Hartman struct foo_attribute { 4144bfe16eSGreg Kroah-Hartman struct attribute attr; 4244bfe16eSGreg Kroah-Hartman ssize_t (*show)(struct foo_obj *foo, struct foo_attribute *attr, char *buf); 4344bfe16eSGreg Kroah-Hartman ssize_t (*store)(struct foo_obj *foo, struct foo_attribute *attr, const char *buf, size_t count); 4444bfe16eSGreg Kroah-Hartman }; 4544bfe16eSGreg Kroah-Hartman #define to_foo_attr(x) container_of(x, struct foo_attribute, attr) 4644bfe16eSGreg Kroah-Hartman 4744bfe16eSGreg Kroah-Hartman /* 4844bfe16eSGreg Kroah-Hartman * The default show function that must be passed to sysfs. This will be 4944bfe16eSGreg Kroah-Hartman * called by sysfs for whenever a show function is called by the user on a 5044bfe16eSGreg Kroah-Hartman * sysfs file associated with the kobjects we have registered. We need to 5144bfe16eSGreg Kroah-Hartman * transpose back from a "default" kobject to our custom struct foo_obj and 5244bfe16eSGreg Kroah-Hartman * then call the show function for that specific object. 5344bfe16eSGreg Kroah-Hartman */ 5444bfe16eSGreg Kroah-Hartman static ssize_t foo_attr_show(struct kobject *kobj, 5544bfe16eSGreg Kroah-Hartman struct attribute *attr, 5644bfe16eSGreg Kroah-Hartman char *buf) 5744bfe16eSGreg Kroah-Hartman { 5844bfe16eSGreg Kroah-Hartman struct foo_attribute *attribute; 5944bfe16eSGreg Kroah-Hartman struct foo_obj *foo; 6044bfe16eSGreg Kroah-Hartman 6144bfe16eSGreg Kroah-Hartman attribute = to_foo_attr(attr); 6244bfe16eSGreg Kroah-Hartman foo = to_foo_obj(kobj); 6344bfe16eSGreg Kroah-Hartman 6444bfe16eSGreg Kroah-Hartman if (!attribute->show) 6544bfe16eSGreg Kroah-Hartman return -EIO; 6644bfe16eSGreg Kroah-Hartman 6744bfe16eSGreg Kroah-Hartman return attribute->show(foo, attribute, buf); 6844bfe16eSGreg Kroah-Hartman } 6944bfe16eSGreg Kroah-Hartman 7044bfe16eSGreg Kroah-Hartman /* 7144bfe16eSGreg Kroah-Hartman * Just like the default show function above, but this one is for when the 7244bfe16eSGreg Kroah-Hartman * sysfs "store" is requested (when a value is written to a file.) 7344bfe16eSGreg Kroah-Hartman */ 7444bfe16eSGreg Kroah-Hartman static ssize_t foo_attr_store(struct kobject *kobj, 7544bfe16eSGreg Kroah-Hartman struct attribute *attr, 7644bfe16eSGreg Kroah-Hartman const char *buf, size_t len) 7744bfe16eSGreg Kroah-Hartman { 7844bfe16eSGreg Kroah-Hartman struct foo_attribute *attribute; 7944bfe16eSGreg Kroah-Hartman struct foo_obj *foo; 8044bfe16eSGreg Kroah-Hartman 8144bfe16eSGreg Kroah-Hartman attribute = to_foo_attr(attr); 8244bfe16eSGreg Kroah-Hartman foo = to_foo_obj(kobj); 8344bfe16eSGreg Kroah-Hartman 8444bfe16eSGreg Kroah-Hartman if (!attribute->store) 8544bfe16eSGreg Kroah-Hartman return -EIO; 8644bfe16eSGreg Kroah-Hartman 8744bfe16eSGreg Kroah-Hartman return attribute->store(foo, attribute, buf, len); 8844bfe16eSGreg Kroah-Hartman } 8944bfe16eSGreg Kroah-Hartman 9044bfe16eSGreg Kroah-Hartman /* Our custom sysfs_ops that we will associate with our ktype later on */ 9152cf25d0SEmese Revfy static const struct sysfs_ops foo_sysfs_ops = { 9244bfe16eSGreg Kroah-Hartman .show = foo_attr_show, 9344bfe16eSGreg Kroah-Hartman .store = foo_attr_store, 9444bfe16eSGreg Kroah-Hartman }; 9544bfe16eSGreg Kroah-Hartman 9644bfe16eSGreg Kroah-Hartman /* 9744bfe16eSGreg Kroah-Hartman * The release function for our object. This is REQUIRED by the kernel to 9844bfe16eSGreg Kroah-Hartman * have. We free the memory held in our object here. 9944bfe16eSGreg Kroah-Hartman * 10044bfe16eSGreg Kroah-Hartman * NEVER try to get away with just a "blank" release function to try to be 10144bfe16eSGreg Kroah-Hartman * smarter than the kernel. Turns out, no one ever is... 10244bfe16eSGreg Kroah-Hartman */ 10344bfe16eSGreg Kroah-Hartman static void foo_release(struct kobject *kobj) 10444bfe16eSGreg Kroah-Hartman { 10544bfe16eSGreg Kroah-Hartman struct foo_obj *foo; 10644bfe16eSGreg Kroah-Hartman 10744bfe16eSGreg Kroah-Hartman foo = to_foo_obj(kobj); 10844bfe16eSGreg Kroah-Hartman kfree(foo); 10944bfe16eSGreg Kroah-Hartman } 11044bfe16eSGreg Kroah-Hartman 11144bfe16eSGreg Kroah-Hartman /* 11244bfe16eSGreg Kroah-Hartman * The "foo" file where the .foo variable is read from and written to. 11344bfe16eSGreg Kroah-Hartman */ 11444bfe16eSGreg Kroah-Hartman static ssize_t foo_show(struct foo_obj *foo_obj, struct foo_attribute *attr, 11544bfe16eSGreg Kroah-Hartman char *buf) 11644bfe16eSGreg Kroah-Hartman { 11744bfe16eSGreg Kroah-Hartman return sprintf(buf, "%d\n", foo_obj->foo); 11844bfe16eSGreg Kroah-Hartman } 11944bfe16eSGreg Kroah-Hartman 12044bfe16eSGreg Kroah-Hartman static ssize_t foo_store(struct foo_obj *foo_obj, struct foo_attribute *attr, 12144bfe16eSGreg Kroah-Hartman const char *buf, size_t count) 12244bfe16eSGreg Kroah-Hartman { 12344bfe16eSGreg Kroah-Hartman sscanf(buf, "%du", &foo_obj->foo); 12444bfe16eSGreg Kroah-Hartman return count; 12544bfe16eSGreg Kroah-Hartman } 12644bfe16eSGreg Kroah-Hartman 12744bfe16eSGreg Kroah-Hartman static struct foo_attribute foo_attribute = 12844bfe16eSGreg Kroah-Hartman __ATTR(foo, 0666, foo_show, foo_store); 12944bfe16eSGreg Kroah-Hartman 13044bfe16eSGreg Kroah-Hartman /* 13120ef9f46SRadu Voicilas * More complex function where we determine which variable is being accessed by 13244bfe16eSGreg Kroah-Hartman * looking at the attribute for the "baz" and "bar" files. 13344bfe16eSGreg Kroah-Hartman */ 13444bfe16eSGreg Kroah-Hartman static ssize_t b_show(struct foo_obj *foo_obj, struct foo_attribute *attr, 13544bfe16eSGreg Kroah-Hartman char *buf) 13644bfe16eSGreg Kroah-Hartman { 13744bfe16eSGreg Kroah-Hartman int var; 13844bfe16eSGreg Kroah-Hartman 13944bfe16eSGreg Kroah-Hartman if (strcmp(attr->attr.name, "baz") == 0) 14044bfe16eSGreg Kroah-Hartman var = foo_obj->baz; 14144bfe16eSGreg Kroah-Hartman else 14244bfe16eSGreg Kroah-Hartman var = foo_obj->bar; 14344bfe16eSGreg Kroah-Hartman return sprintf(buf, "%d\n", var); 14444bfe16eSGreg Kroah-Hartman } 14544bfe16eSGreg Kroah-Hartman 14644bfe16eSGreg Kroah-Hartman static ssize_t b_store(struct foo_obj *foo_obj, struct foo_attribute *attr, 14744bfe16eSGreg Kroah-Hartman const char *buf, size_t count) 14844bfe16eSGreg Kroah-Hartman { 14944bfe16eSGreg Kroah-Hartman int var; 15044bfe16eSGreg Kroah-Hartman 15144bfe16eSGreg Kroah-Hartman sscanf(buf, "%du", &var); 15244bfe16eSGreg Kroah-Hartman if (strcmp(attr->attr.name, "baz") == 0) 15344bfe16eSGreg Kroah-Hartman foo_obj->baz = var; 15444bfe16eSGreg Kroah-Hartman else 15544bfe16eSGreg Kroah-Hartman foo_obj->bar = var; 15644bfe16eSGreg Kroah-Hartman return count; 15744bfe16eSGreg Kroah-Hartman } 15844bfe16eSGreg Kroah-Hartman 15944bfe16eSGreg Kroah-Hartman static struct foo_attribute baz_attribute = 16044bfe16eSGreg Kroah-Hartman __ATTR(baz, 0666, b_show, b_store); 16144bfe16eSGreg Kroah-Hartman static struct foo_attribute bar_attribute = 16244bfe16eSGreg Kroah-Hartman __ATTR(bar, 0666, b_show, b_store); 16344bfe16eSGreg Kroah-Hartman 16444bfe16eSGreg Kroah-Hartman /* 16520ef9f46SRadu Voicilas * Create a group of attributes so that we can create and destroy them all 16644bfe16eSGreg Kroah-Hartman * at once. 16744bfe16eSGreg Kroah-Hartman */ 16844bfe16eSGreg Kroah-Hartman static struct attribute *foo_default_attrs[] = { 16944bfe16eSGreg Kroah-Hartman &foo_attribute.attr, 17044bfe16eSGreg Kroah-Hartman &baz_attribute.attr, 17144bfe16eSGreg Kroah-Hartman &bar_attribute.attr, 17244bfe16eSGreg Kroah-Hartman NULL, /* need to NULL terminate the list of attributes */ 17344bfe16eSGreg Kroah-Hartman }; 17444bfe16eSGreg Kroah-Hartman 17544bfe16eSGreg Kroah-Hartman /* 17644bfe16eSGreg Kroah-Hartman * Our own ktype for our kobjects. Here we specify our sysfs ops, the 17744bfe16eSGreg Kroah-Hartman * release function, and the set of default attributes we want created 17844bfe16eSGreg Kroah-Hartman * whenever a kobject of this type is registered with the kernel. 17944bfe16eSGreg Kroah-Hartman */ 18044bfe16eSGreg Kroah-Hartman static struct kobj_type foo_ktype = { 18144bfe16eSGreg Kroah-Hartman .sysfs_ops = &foo_sysfs_ops, 18244bfe16eSGreg Kroah-Hartman .release = foo_release, 18344bfe16eSGreg Kroah-Hartman .default_attrs = foo_default_attrs, 18444bfe16eSGreg Kroah-Hartman }; 18544bfe16eSGreg Kroah-Hartman 18644bfe16eSGreg Kroah-Hartman static struct kset *example_kset; 18744bfe16eSGreg Kroah-Hartman static struct foo_obj *foo_obj; 18844bfe16eSGreg Kroah-Hartman static struct foo_obj *bar_obj; 18944bfe16eSGreg Kroah-Hartman static struct foo_obj *baz_obj; 19044bfe16eSGreg Kroah-Hartman 19144bfe16eSGreg Kroah-Hartman static struct foo_obj *create_foo_obj(const char *name) 19244bfe16eSGreg Kroah-Hartman { 19344bfe16eSGreg Kroah-Hartman struct foo_obj *foo; 19444bfe16eSGreg Kroah-Hartman int retval; 19544bfe16eSGreg Kroah-Hartman 19644bfe16eSGreg Kroah-Hartman /* allocate the memory for the whole object */ 19744bfe16eSGreg Kroah-Hartman foo = kzalloc(sizeof(*foo), GFP_KERNEL); 19844bfe16eSGreg Kroah-Hartman if (!foo) 19944bfe16eSGreg Kroah-Hartman return NULL; 20044bfe16eSGreg Kroah-Hartman 20144bfe16eSGreg Kroah-Hartman /* 20244bfe16eSGreg Kroah-Hartman * As we have a kset for this kobject, we need to set it before calling 20344bfe16eSGreg Kroah-Hartman * the kobject core. 20444bfe16eSGreg Kroah-Hartman */ 20544bfe16eSGreg Kroah-Hartman foo->kobj.kset = example_kset; 20644bfe16eSGreg Kroah-Hartman 20744bfe16eSGreg Kroah-Hartman /* 20844bfe16eSGreg Kroah-Hartman * Initialize and add the kobject to the kernel. All the default files 20944bfe16eSGreg Kroah-Hartman * will be created here. As we have already specified a kset for this 21044bfe16eSGreg Kroah-Hartman * kobject, we don't have to set a parent for the kobject, the kobject 21144bfe16eSGreg Kroah-Hartman * will be placed beneath that kset automatically. 21244bfe16eSGreg Kroah-Hartman */ 21344bfe16eSGreg Kroah-Hartman retval = kobject_init_and_add(&foo->kobj, &foo_ktype, NULL, "%s", name); 21444bfe16eSGreg Kroah-Hartman if (retval) { 215185000fcSLi Zefan kobject_put(&foo->kobj); 21644bfe16eSGreg Kroah-Hartman return NULL; 21744bfe16eSGreg Kroah-Hartman } 21844bfe16eSGreg Kroah-Hartman 21944bfe16eSGreg Kroah-Hartman /* 22044bfe16eSGreg Kroah-Hartman * We are always responsible for sending the uevent that the kobject 22144bfe16eSGreg Kroah-Hartman * was added to the system. 22244bfe16eSGreg Kroah-Hartman */ 22344bfe16eSGreg Kroah-Hartman kobject_uevent(&foo->kobj, KOBJ_ADD); 22444bfe16eSGreg Kroah-Hartman 22544bfe16eSGreg Kroah-Hartman return foo; 22644bfe16eSGreg Kroah-Hartman } 22744bfe16eSGreg Kroah-Hartman 22844bfe16eSGreg Kroah-Hartman static void destroy_foo_obj(struct foo_obj *foo) 22944bfe16eSGreg Kroah-Hartman { 23044bfe16eSGreg Kroah-Hartman kobject_put(&foo->kobj); 23144bfe16eSGreg Kroah-Hartman } 23244bfe16eSGreg Kroah-Hartman 2337ec7fb39SQinghuang Feng static int __init example_init(void) 23444bfe16eSGreg Kroah-Hartman { 23544bfe16eSGreg Kroah-Hartman /* 23644bfe16eSGreg Kroah-Hartman * Create a kset with the name of "kset_example", 23744bfe16eSGreg Kroah-Hartman * located under /sys/kernel/ 23844bfe16eSGreg Kroah-Hartman */ 23944bfe16eSGreg Kroah-Hartman example_kset = kset_create_and_add("kset_example", NULL, kernel_kobj); 24044bfe16eSGreg Kroah-Hartman if (!example_kset) 24144bfe16eSGreg Kroah-Hartman return -ENOMEM; 24244bfe16eSGreg Kroah-Hartman 24344bfe16eSGreg Kroah-Hartman /* 24444bfe16eSGreg Kroah-Hartman * Create three objects and register them with our kset 24544bfe16eSGreg Kroah-Hartman */ 24644bfe16eSGreg Kroah-Hartman foo_obj = create_foo_obj("foo"); 24744bfe16eSGreg Kroah-Hartman if (!foo_obj) 24844bfe16eSGreg Kroah-Hartman goto foo_error; 24944bfe16eSGreg Kroah-Hartman 25044bfe16eSGreg Kroah-Hartman bar_obj = create_foo_obj("bar"); 25144bfe16eSGreg Kroah-Hartman if (!bar_obj) 25244bfe16eSGreg Kroah-Hartman goto bar_error; 25344bfe16eSGreg Kroah-Hartman 25444bfe16eSGreg Kroah-Hartman baz_obj = create_foo_obj("baz"); 25544bfe16eSGreg Kroah-Hartman if (!baz_obj) 25644bfe16eSGreg Kroah-Hartman goto baz_error; 25744bfe16eSGreg Kroah-Hartman 25844bfe16eSGreg Kroah-Hartman return 0; 25944bfe16eSGreg Kroah-Hartman 26044bfe16eSGreg Kroah-Hartman baz_error: 26144bfe16eSGreg Kroah-Hartman destroy_foo_obj(bar_obj); 26244bfe16eSGreg Kroah-Hartman bar_error: 26344bfe16eSGreg Kroah-Hartman destroy_foo_obj(foo_obj); 26444bfe16eSGreg Kroah-Hartman foo_error: 265*e756bc56SBjorn Helgaas kset_unregister(example_kset); 26644bfe16eSGreg Kroah-Hartman return -EINVAL; 26744bfe16eSGreg Kroah-Hartman } 26844bfe16eSGreg Kroah-Hartman 2697ec7fb39SQinghuang Feng static void __exit example_exit(void) 27044bfe16eSGreg Kroah-Hartman { 27144bfe16eSGreg Kroah-Hartman destroy_foo_obj(baz_obj); 27244bfe16eSGreg Kroah-Hartman destroy_foo_obj(bar_obj); 27344bfe16eSGreg Kroah-Hartman destroy_foo_obj(foo_obj); 27444bfe16eSGreg Kroah-Hartman kset_unregister(example_kset); 27544bfe16eSGreg Kroah-Hartman } 27644bfe16eSGreg Kroah-Hartman 27744bfe16eSGreg Kroah-Hartman module_init(example_init); 27844bfe16eSGreg Kroah-Hartman module_exit(example_exit); 27944bfe16eSGreg Kroah-Hartman MODULE_LICENSE("GPL"); 28044bfe16eSGreg Kroah-Hartman MODULE_AUTHOR("Greg Kroah-Hartman <greg@kroah.com>"); 281