Re: [PATCH] exporting capability code/name pairs (try #6.1)

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
From: Kohei KaiGai
Date: Tuesday, February 19, 2008 - 11:19 pm

>> Could you also modify the documentation and the sample code to use this

[3/3] Add a new example of kobject/attribute

The attached patch can provide a new exmple to use kobject and attribute.
The _show() and _store() method can refer/store the private data field of
kobj_attribute structure to know what entries are refered by users.
It will make easier to share a single _show()/_store() method with several
entries.

Signed-off-by: KaiGai Kohei <kaigai@ak.jp.nec.com>
--
 samples/kobject/kobject-example.c |   32 ++++++++++++++++++++++++++++++++
 1 files changed, 32 insertions(+), 0 deletions(-)

diff --git a/samples/kobject/kobject-example.c b/samples/kobject/kobject-example.c
index 08d0d3f..f99d734 100644
--- a/samples/kobject/kobject-example.c
+++ b/samples/kobject/kobject-example.c
@@ -77,6 +77,35 @@ static struct kobj_attribute baz_attribute =
 static struct kobj_attribute bar_attribute =
 	__ATTR(bar, 0666, b_show, b_store);

+/*
+ * You can store a private data within 'data' field of kobj_attribute.
+ * It enables to share a single _show() or _store() method with several
+ * entries.
+ */
+static ssize_t integer_show(struct kobject *kobj,
+			    struct kobj_attribute *attr,
+			    char *buf)
+{
+	return scnprintf(buf, PAGE_SIZE, "%d\n", (int) attr->data);
+}
+
+static ssize_t integer_store(struct kobject *kobj,
+			     struct kobj_attribute *attr,
+			     const char *buf, size_t count)
+{
+	int code;
+
+	sscanf(buf, "%du", &code);
+	attr->data = (void *) code;
+	return count;
+}
+
+static struct kobj_attribute hoge_attribute =
+	__ATTR_DATA(hoge, 0666, integer_show, integer_store, 123);
+static struct kobj_attribute piyo_attribute =
+	__ATTR_DATA(piyo, 0666, integer_show, integer_store, 456);
+static struct kobj_attribute fuga_attribute =
+	__ATTR_DATA(fuga, 0444, integer_show, NULL, 789);

 /*
  * Create a group of attributes so that we can create and destory them all
@@ -86,6 +115,9 @@ static struct attribute *attrs[] = {
 	&foo_attribute.attr,
 	&baz_attribute.attr,
 	&bar_attribute.attr,
+	&hoge_attribute.attr,
+	&piyo_attribute.attr,
+	&fuga_attribute.attr,
 	NULL,	/* need to NULL terminate the list of attributes */
 };

-- 
OSS Platform Development Division, NEC
KaiGai Kohei <kaigai@ak.jp.nec.com>
--
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
Re: [PATCH 1/3] exporting capability code/name pairs (try 2nd), Andrew G. Morgan, (Fri Jan 25, 12:32 am)
[PATCH] exporting capability code/name pairs (try #4), Kohei KaiGai, (Fri Feb 8, 2:42 am)
Re: [PATCH] exporting capability code/name pairs (try #4), Andrew G. Morgan, (Fri Feb 8, 9:48 am)
Re: [PATCH] exporting capability code/name pairs (try #4), Alexey Dobriyan, (Fri Feb 8, 12:23 pm)
Re: [PATCH] exporting capability code/name pairs (try #4), Serge E. Hallyn, (Tue Feb 12, 11:08 am)
Re: [PATCH] exporting capability code/name pairs (try #4), Alexey Dobriyan, (Tue Feb 12, 2:58 pm)
[PATCH] exporting capability code/name pairs (try #5), Kohei KaiGai, (Thu Feb 14, 6:38 pm)
Re: [PATCH] exporting capability code/name pairs (try #5.1), Serge E. Hallyn, (Fri Feb 15, 11:38 am)
Re: [PATCH] exporting capability code/name pairs (try #5.1), Serge E. Hallyn, (Mon Feb 18, 8:15 am)
[PATCH] exporting capability code/name pairs (try #6), Kohei KaiGai, (Tue Feb 19, 9:38 pm)
[PATCH] exporting capability code/name pairs (try #6), Kohei KaiGai, (Tue Feb 19, 9:39 pm)
Re: [PATCH] exporting capability code/name pairs (try #6), Kohei KaiGai, (Tue Feb 19, 10:38 pm)
Re: [PATCH] exporting capability code/name pairs (try #6.1), Kohei KaiGai, (Tue Feb 19, 11:19 pm)