summaryrefslogtreecommitdiff
path: root/samples
diff options
context:
space:
mode:
authorGreg Kroah-Hartman <gregkh@suse.de>2007-11-27 11:28:26 -0800
committerGreg Kroah-Hartman <gregkh@suse.de>2008-01-24 20:40:41 -0800
commit40efcb05f213180b7cc8fd8d963377305f236c28 (patch)
tree84e74d8fe64f84b52b1023c963ebe215276d9f8e /samples
parent36d78d6c5b50ba945bbdee9bf1d8daac00154e02 (diff)
downloadlinux-next-40efcb05f213180b7cc8fd8d963377305f236c28.tar.gz
kobject: add sample code for how to use kobjects in a simple manner.
This is a simple kobject module, showing how to use kobj_attributes in basic and more complex ways. Cc: Kay Sievers <kay.sievers@vrfy.org> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
Diffstat (limited to 'samples')
-rw-r--r--samples/Kconfig10
-rw-r--r--samples/Makefile2
-rw-r--r--samples/kobject/Makefile1
-rw-r--r--samples/kobject/kobject-example.c137
4 files changed, 149 insertions, 1 deletions
diff --git a/samples/Kconfig b/samples/Kconfig
index 57bb2236952c..74d97cc24787 100644
--- a/samples/Kconfig
+++ b/samples/Kconfig
@@ -13,4 +13,14 @@ config SAMPLE_MARKERS
help
This build markers example modules.
+config SAMPLE_KOBJECT
+ tristate "Build kobject examples"
+ help
+ This config option will allow you to build a number of
+ different kobject sample modules showing how to use kobjects,
+ ksets, and ktypes properly.
+
+ If in doubt, say "N" here.
+
endif # SAMPLES
+
diff --git a/samples/Makefile b/samples/Makefile
index 5a4f0b6bcbed..8652d0f268ad 100644
--- a/samples/Makefile
+++ b/samples/Makefile
@@ -1,3 +1,3 @@
# Makefile for Linux samples code
-obj-$(CONFIG_SAMPLES) += markers/
+obj-$(CONFIG_SAMPLES) += markers/ kobject/
diff --git a/samples/kobject/Makefile b/samples/kobject/Makefile
new file mode 100644
index 000000000000..cce16e99fde5
--- /dev/null
+++ b/samples/kobject/Makefile
@@ -0,0 +1 @@
+obj-$(CONFIG_SAMPLE_KOBJECT) += kobject-example.o
diff --git a/samples/kobject/kobject-example.c b/samples/kobject/kobject-example.c
new file mode 100644
index 000000000000..08d0d3ff3263
--- /dev/null
+++ b/samples/kobject/kobject-example.c
@@ -0,0 +1,137 @@
+/*
+ * Sample kobject implementation
+ *
+ * Copyright (C) 2004-2007 Greg Kroah-Hartman <greg@kroah.com>
+ * Copyright (C) 2007 Novell Inc.
+ *
+ * Released under the GPL version 2 only.
+ *
+ */
+#include <linux/kobject.h>
+#include <linux/string.h>
+#include <linux/sysfs.h>
+#include <linux/module.h>
+#include <linux/init.h>
+
+/*
+ * This module shows how to create a simple subdirectory in sysfs called
+ * /sys/kernel/kobject-example In that directory, 3 files are created:
+ * "foo", "baz", and "bar". If an integer is written to these files, it can be
+ * later read out of it.
+ */
+
+static int foo;
+static int baz;
+static int bar;
+
+/*
+ * The "foo" file where a static variable is read from and written to.
+ */
+static ssize_t foo_show(struct kobject *kobj, struct kobj_attribute *attr,
+ char *buf)
+{
+ return sprintf(buf, "%d\n", foo);
+}
+
+static ssize_t foo_store(struct kobject *kobj, struct kobj_attribute *attr,
+ const char *buf, size_t count)
+{
+ sscanf(buf, "%du", &foo);
+ return count;
+}
+
+static struct kobj_attribute foo_attribute =
+ __ATTR(foo, 0666, foo_show, foo_store);
+
+/*
+ * More complex function where we determine which varible is being accessed by
+ * looking at the attribute for the "baz" and "bar" files.
+ */
+static ssize_t b_show(struct kobject *kobj, struct kobj_attribute *attr,
+ char *buf)
+{
+ int var;
+
+ if (strcmp(attr->attr.name, "baz") == 0)
+ var = baz;
+ else
+ var = bar;
+ return sprintf(buf, "%d\n", var);
+}
+
+static ssize_t b_store(struct kobject *kobj, struct kobj_attribute *attr,
+ const char *buf, size_t count)
+{
+ int var;
+
+ sscanf(buf, "%du", &var);
+ if (strcmp(attr->attr.name, "baz") == 0)
+ baz = var;
+ else
+ bar = var;
+ return count;
+}
+
+static struct kobj_attribute baz_attribute =
+ __ATTR(baz, 0666, b_show, b_store);
+static struct kobj_attribute bar_attribute =
+ __ATTR(bar, 0666, b_show, b_store);
+
+
+/*
+ * Create a group of attributes so that we can create and destory them all
+ * at once.
+ */
+static struct attribute *attrs[] = {
+ &foo_attribute.attr,
+ &baz_attribute.attr,
+ &bar_attribute.attr,
+ NULL, /* need to NULL terminate the list of attributes */
+};
+
+/*
+ * An unnamed attribute group will put all of the attributes directly in
+ * the kobject directory. If we specify a name, a subdirectory will be
+ * created for the attributes with the directory being the name of the
+ * attribute group.
+ */
+static struct attribute_group attr_group = {
+ .attrs = attrs,
+};
+
+static struct kobject *example_kobj;
+
+static int example_init(void)
+{
+ int retval;
+
+ /*
+ * Create a simple kobject with the name of "kobject_example",
+ * located under /sys/kernel/
+ *
+ * As this is a simple directory, no uevent will be sent to
+ * userspace. That is why this function should not be used for
+ * any type of dynamic kobjects, where the name and number are
+ * not known ahead of time.
+ */
+ example_kobj = kobject_create_and_add("kobject_example", kernel_kobj);
+ if (!example_kobj)
+ return -ENOMEM;
+
+ /* Create the files associated with this kobject */
+ retval = sysfs_create_group(example_kobj, &attr_group);
+ if (retval)
+ kobject_put(example_kobj);
+
+ return retval;
+}
+
+static void example_exit(void)
+{
+ kobject_put(example_kobj);
+}
+
+module_init(example_init);
+module_exit(example_exit);
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Greg Kroah-Hartman <greg@kroah.com>");