From: Greg KH <greg@kroah.com>
To: linux-kernel@vger.kernel.org
Cc: Kay Sievers <kay.sievers@vrfy.org>,
Alan Stern <stern@rowland.harvard.edu>,
Jonathan Corbet <corbet@lwn.net>,
Randy Dunlap <randy.dunlap@oracle.com>
Subject: [RFC] Sample kset/ktype/kobject implementation
Date: Tue, 27 Nov 2007 15:04:06 -0800 [thread overview]
Message-ID: <20071127230406.GD10038@kroah.com> (raw)
In-Reply-To: <20071127230252.GB10038@kroah.com>
/*
* Sample kset and ktype 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 kset in sysfs called
* /sys/kernel/kset-example
* Then tree kobjects are created and assigned to this kset, "foo", "baz",
* and "bar". In those kobjects, attributes of the same name are also
* created and if an integer is written to these files, it can be later
* read out of it.
*/
/*
* This is our "object" that we will create a few of and register them with
* sysfs.
*/
struct foo_obj {
struct kobject kobj;
int foo;
int baz;
int bar;
};
#define to_foo_obj(x) container_of(x, struct foo_obj, kobj)
/* a custom attribute that works just for a struct foo_obj. */
struct foo_attribute {
struct attribute attr;
ssize_t (*show)(struct foo_obj *foo, struct foo_attribute *attr, char *buf);
ssize_t (*store)(struct foo_obj *foo, struct foo_attribute *attr, const char *buf, size_t count);
};
#define to_foo_attr(x) container_of(x, struct foo_attribute, attr)
/*
* The default show function that must be passed to sysfs. This will be
* called by sysfs for whenever a show function is called by the user on a
* sysfs file associated with the kobjects we have registered. We need to
* transpose back from a "default" kobject to our custom struct foo_obj and
* then call the show function for that specific object.
*/
static ssize_t foo_attr_show(struct kobject *kobj,
struct attribute *attr,
char *buf)
{
struct foo_attribute *attribute;
struct foo_obj *foo;
attribute = to_foo_attr(attr);
foo = to_foo_obj(kobj);
if (!attribute->show)
return -EIO;
return attribute->show(foo, attribute, buf);
}
/*
* Just like the default show function above, but this one is for when the
* sysfs "store" is requested (when a value is written to a file.)
*/
static ssize_t foo_attr_store(struct kobject *kobj,
struct attribute *attr,
const char *buf, size_t len)
{
struct foo_attribute *attribute;
struct foo_obj *foo;
attribute = to_foo_attr(attr);
foo = to_foo_obj(kobj);
if (!attribute->store)
return -EIO;
return attribute->store(foo, attribute, buf, len);
}
/* Our custom sysfs_ops that we will associate with our ktype later on */
static struct sysfs_ops foo_sysfs_ops = {
.show = foo_attr_show,
.store = foo_attr_store,
};
/*
* The release function for our object. This is REQUIRED by the kernel to
* have. We free the memory held in our object here.
*
* NEVER try to get away with just a "blank" release function to try to be
* smarter than the kernel. Turns out, no one ever is...
*/
static void foo_release(struct kobject *kobj)
{
struct foo_obj *foo;
foo = to_foo_obj(kobj);
kfree(foo);
}
/*
* The "foo" file where the .foo variable is read from and written to.
*/
static ssize_t foo_show(struct foo_obj *foo_obj, struct foo_attribute *attr,
char *buf)
{
return sprintf(buf, "%d\n", foo_obj->foo);
}
static ssize_t foo_store(struct foo_obj *foo_obj, struct foo_attribute *attr,
const char *buf, size_t count)
{
sscanf(buf, "%du", &foo_obj->foo);
return count;
}
static struct foo_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 foo_obj *foo_obj, struct foo_attribute *attr,
char *buf)
{
int var;
if (strcmp(attr->attr.name, "baz") == 0)
var = foo_obj->baz;
else
var = foo_obj->bar;
return sprintf(buf, "%d\n", var);
}
static ssize_t b_store(struct foo_obj *foo_obj, struct foo_attribute *attr,
const char *buf, size_t count)
{
int var;
sscanf(buf, "%du", &var);
if (strcmp(attr->attr.name, "baz") == 0)
foo_obj->baz = var;
else
foo_obj->bar = var;
return count;
}
static struct foo_attribute baz_attribute =
__ATTR(baz, 0666, b_show, b_store);
static struct foo_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 *foo_default_attrs[] = {
&foo_attribute.attr,
&baz_attribute.attr,
&bar_attribute.attr,
NULL, /* need to NULL terminate the list of attributes */
};
/*
* Our own ktype for our kobjects. Here we specify our sysfs ops, the
* release function, and the set of default attributes we want created
* whenever a kobject of this type is registered with the kernel.
*/
static struct kobj_type foo_ktype = {
.sysfs_ops = &foo_sysfs_ops,
.release = foo_release,
.default_attrs = foo_default_attrs,
};
static struct kset *example_kset;
static struct foo_obj *foo_obj;
static struct foo_obj *bar_obj;
static struct foo_obj *baz_obj;
static struct foo_obj *create_foo_obj(const char *name)
{
struct foo_obj *foo;
int retval;
/* allocate the memory for the whole object */
foo = kzalloc(sizeof(*foo), GFP_KERNEL);
if (!foo)
return NULL;
/* initialize the kobject portion of the object properly */
kobject_set_name(&foo->kobj, "%s", name);
foo->kobj.kset = example_kset;
foo->kobj.ktype = &foo_ktype;
/*
* Register the kobject with the kernel, all the default files will
* be created here and the uevent will be sent out. If we were to
* call kobject_init() and then kobject_add() we would be
* responsible for sending out the initial KOBJ_ADD uevent.
*/
retval = kobject_register(&foo->kobj);
if (retval) {
kfree(foo);
return NULL;
}
return foo;
}
static void destroy_foo_obj(struct foo_obj *foo)
{
kobject_unregister(&foo->kobj);
}
static int example_init(void)
{
/*
* Create a kset with the name of "kset_example",
* located under /sys/kernel/
*/
example_kset = kset_create_and_register("kset_example", NULL, kernel_kobj);
if (!example_kset)
return -ENOMEM;
/*
* Create three objects and register them with our kset
*/
foo_obj = create_foo_obj("foo");
if (!foo_obj)
goto foo_error;
bar_obj = create_foo_obj("bar");
if (!bar_obj)
goto bar_error;
baz_obj = create_foo_obj("baz");
if (!baz_obj)
goto baz_error;
return 0;
baz_error:
destroy_foo_obj(bar_obj);
bar_error:
destroy_foo_obj(foo_obj);
foo_error:
return -EINVAL;
}
static void example_exit(void)
{
destroy_foo_obj(baz_obj);
destroy_foo_obj(bar_obj);
destroy_foo_obj(foo_obj);
kset_unregister(example_kset);
}
module_init(example_init);
module_exit(example_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Greg Kroah-Hartman <greg@kroah.com>");
next prev parent reply other threads:[~2007-11-27 23:05 UTC|newest]
Thread overview: 77+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-11-27 23:02 [RFC] New kobject/kset/ktype documentation and example code Greg KH
2007-11-27 23:03 ` [RFC] sample kobject implementation Greg KH
2007-11-27 23:04 ` Greg KH [this message]
2007-11-28 16:35 ` [RFC] Sample kset/ktype/kobject implementation Cornelia Huck
2007-11-29 6:11 ` Greg KH
2007-11-29 9:39 ` Cornelia Huck
2007-11-29 20:39 ` Greg KH
2007-11-29 22:11 ` Alan Stern
2007-11-30 5:07 ` Dave Young
2007-11-30 5:57 ` Dave Young
2007-11-30 14:51 ` Alan Stern
2007-11-30 6:41 ` Greg KH
2007-11-27 23:10 ` [RFC] New kobject/kset/ktype documentation and example code Kyle McMartin
2007-11-27 23:29 ` Greg KH
2007-11-27 23:21 ` Frans Pop
2007-11-28 3:50 ` Jonathan Corbet
2007-11-29 5:46 ` Greg KH
2007-11-28 9:01 ` Cornelia Huck
2007-11-28 12:35 ` Kay Sievers
2007-11-28 15:52 ` Cornelia Huck
2007-11-28 16:03 ` Kay Sievers
2007-11-28 16:09 ` Cornelia Huck
2007-11-28 17:06 ` Greg KH
2007-11-28 19:18 ` Alan Stern
2007-11-29 10:12 ` Cornelia Huck
2007-11-29 15:47 ` Alan Stern
2007-11-29 16:28 ` Cornelia Huck
2007-11-29 16:55 ` Alan Stern
2007-11-29 17:52 ` Cornelia Huck
2007-11-29 5:59 ` Greg KH
2007-11-28 11:45 ` Cornelia Huck
2007-11-28 12:23 ` Kay Sievers
2007-11-28 15:48 ` Cornelia Huck
2007-11-28 15:57 ` Kay Sievers
2007-11-28 16:12 ` Cornelia Huck
2007-11-28 16:36 ` Kay Sievers
2007-11-28 16:51 ` Cornelia Huck
2007-11-28 17:00 ` Kay Sievers
2007-11-29 6:08 ` Greg KH
2007-11-29 7:50 ` Kay Sievers
2007-11-29 9:35 ` Cornelia Huck
2007-11-29 10:53 ` Kay Sievers
2007-11-29 6:02 ` Greg KH
2007-11-29 6:04 ` Greg KH
2007-11-29 9:41 ` Cornelia Huck
2007-11-28 19:03 ` Alan Stern
2007-11-28 19:28 ` Kay Sievers
2007-11-28 19:36 ` Alan Stern
2007-11-28 19:46 ` Kay Sievers
2007-11-28 20:42 ` [PATCH] kobject: make sure kobj->ktype is set before kobject_init Alan Stern
2007-11-28 20:52 ` Kay Sievers
2007-11-28 21:45 ` Greg KH
2007-11-28 22:00 ` Alan Stern
2007-11-28 22:38 ` Greg KH
2007-11-29 10:05 ` Cornelia Huck
2007-11-29 10:59 ` Kay Sievers
2007-11-29 11:48 ` Cornelia Huck
2007-11-29 15:54 ` Alan Stern
2007-11-29 16:04 ` Kay Sievers
2007-11-29 16:21 ` Cornelia Huck
2007-11-29 21:53 ` kobject_init rewrite Greg KH
2007-11-29 21:54 ` Greg KH
2007-11-30 9:31 ` Cornelia Huck
2007-11-29 22:16 ` Alan Stern
2007-11-29 22:24 ` Greg KH
2007-11-29 17:06 ` [PATCH] kobject: make sure kobj->ktype is set before kobject_init Alan Stern
2007-11-29 17:17 ` Kay Sievers
2007-11-29 18:04 ` Alan Stern
2007-11-29 18:33 ` Kay Sievers
2007-11-29 19:05 ` Alan Stern
2007-11-29 19:51 ` Kay Sievers
2007-11-29 20:09 ` Alan Stern
2007-11-29 20:19 ` Kay Sievers
2007-11-29 20:26 ` Kay Sievers
2007-11-30 9:30 ` Cornelia Huck
2007-11-29 6:18 ` [RFC] New kobject/kset/ktype documentation and example code Greg KH
2007-11-29 15:42 ` Alan Stern
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20071127230406.GD10038@kroah.com \
--to=greg@kroah.com \
--cc=corbet@lwn.net \
--cc=kay.sievers@vrfy.org \
--cc=linux-kernel@vger.kernel.org \
--cc=randy.dunlap@oracle.com \
--cc=stern@rowland.harvard.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox