Path: blob/master/samples/kobject/kobject-example.c
10818 views
/*1* Sample kobject implementation2*3* Copyright (C) 2004-2007 Greg Kroah-Hartman <[email protected]>4* Copyright (C) 2007 Novell Inc.5*6* Released under the GPL version 2 only.7*8*/9#include <linux/kobject.h>10#include <linux/string.h>11#include <linux/sysfs.h>12#include <linux/module.h>13#include <linux/init.h>1415/*16* This module shows how to create a simple subdirectory in sysfs called17* /sys/kernel/kobject-example In that directory, 3 files are created:18* "foo", "baz", and "bar". If an integer is written to these files, it can be19* later read out of it.20*/2122static int foo;23static int baz;24static int bar;2526/*27* The "foo" file where a static variable is read from and written to.28*/29static ssize_t foo_show(struct kobject *kobj, struct kobj_attribute *attr,30char *buf)31{32return sprintf(buf, "%d\n", foo);33}3435static ssize_t foo_store(struct kobject *kobj, struct kobj_attribute *attr,36const char *buf, size_t count)37{38sscanf(buf, "%du", &foo);39return count;40}4142static struct kobj_attribute foo_attribute =43__ATTR(foo, 0666, foo_show, foo_store);4445/*46* More complex function where we determine which variable is being accessed by47* looking at the attribute for the "baz" and "bar" files.48*/49static ssize_t b_show(struct kobject *kobj, struct kobj_attribute *attr,50char *buf)51{52int var;5354if (strcmp(attr->attr.name, "baz") == 0)55var = baz;56else57var = bar;58return sprintf(buf, "%d\n", var);59}6061static ssize_t b_store(struct kobject *kobj, struct kobj_attribute *attr,62const char *buf, size_t count)63{64int var;6566sscanf(buf, "%du", &var);67if (strcmp(attr->attr.name, "baz") == 0)68baz = var;69else70bar = var;71return count;72}7374static struct kobj_attribute baz_attribute =75__ATTR(baz, 0666, b_show, b_store);76static struct kobj_attribute bar_attribute =77__ATTR(bar, 0666, b_show, b_store);787980/*81* Create a group of attributes so that we can create and destroy them all82* at once.83*/84static struct attribute *attrs[] = {85&foo_attribute.attr,86&baz_attribute.attr,87&bar_attribute.attr,88NULL, /* need to NULL terminate the list of attributes */89};9091/*92* An unnamed attribute group will put all of the attributes directly in93* the kobject directory. If we specify a name, a subdirectory will be94* created for the attributes with the directory being the name of the95* attribute group.96*/97static struct attribute_group attr_group = {98.attrs = attrs,99};100101static struct kobject *example_kobj;102103static int __init example_init(void)104{105int retval;106107/*108* Create a simple kobject with the name of "kobject_example",109* located under /sys/kernel/110*111* As this is a simple directory, no uevent will be sent to112* userspace. That is why this function should not be used for113* any type of dynamic kobjects, where the name and number are114* not known ahead of time.115*/116example_kobj = kobject_create_and_add("kobject_example", kernel_kobj);117if (!example_kobj)118return -ENOMEM;119120/* Create the files associated with this kobject */121retval = sysfs_create_group(example_kobj, &attr_group);122if (retval)123kobject_put(example_kobj);124125return retval;126}127128static void __exit example_exit(void)129{130kobject_put(example_kobj);131}132133module_init(example_init);134module_exit(example_exit);135MODULE_LICENSE("GPL");136MODULE_AUTHOR("Greg Kroah-Hartman <[email protected]>");137138139