aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorGreg Kroah-Hartman <gregkh@suse.de>2007-11-05 13:16:15 -0800
committerGreg Kroah-Hartman <gregkh@suse.de>2008-01-24 20:40:10 -0800
commit3f9e3ee9dc3605e5c593b5d708494571fb0d3970 (patch)
treeb5490463c14b919f88ec18833846a38cfb621aa3 /lib
parentb727c702896f88d2ff6c3e03bd011d7c3dffe3e1 (diff)
downloadlinaro-lsk-3f9e3ee9dc3605e5c593b5d708494571fb0d3970.tar.gz
kobject: add kobject_create_and_add function
This lets users create dynamic kobjects much easier. Cc: Kay Sievers <kay.sievers@vrfy.org> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
Diffstat (limited to 'lib')
-rw-r--r--lib/kobject.c81
1 files changed, 63 insertions, 18 deletions
diff --git a/lib/kobject.c b/lib/kobject.c
index 4fb27ba2880..98422a3eeff 100644
--- a/lib/kobject.c
+++ b/lib/kobject.c
@@ -619,18 +619,69 @@ void kobject_put(struct kobject * kobj)
kref_put(&kobj->kref, kobject_release);
}
-
-static void dir_release(struct kobject *kobj)
+static void dynamic_kobj_release(struct kobject *kobj)
{
+ pr_debug("%s: freeing %s\n", __FUNCTION__, kobject_name(kobj));
kfree(kobj);
}
-static struct kobj_type dir_ktype = {
- .release = dir_release,
- .sysfs_ops = NULL,
- .default_attrs = NULL,
+static struct kobj_type dynamic_kobj_ktype = {
+ .release = dynamic_kobj_release,
};
+/*
+ * kobject_create - create a struct kobject dynamically
+ *
+ * This function creates a kobject structure dynamically and sets it up
+ * to be a "dynamic" kobject with a default release function set up.
+ *
+ * If the kobject was not able to be created, NULL will be returned.
+ */
+static struct kobject *kobject_create(void)
+{
+ struct kobject *kobj;
+
+ kobj = kzalloc(sizeof(*kobj), GFP_KERNEL);
+ if (!kobj)
+ return NULL;
+
+ kobject_init_ng(kobj, &dynamic_kobj_ktype);
+ return kobj;
+}
+
+/**
+ * kobject_create_and_add - create a struct kobject dynamically and register it with sysfs
+ *
+ * @name: the name for the kset
+ * @parent: the parent kobject of this kobject, if any.
+ *
+ * This function creates a kset structure dynamically and registers it
+ * with sysfs. When you are finished with this structure, call
+ * kobject_unregister() and the structure will be dynamically freed when
+ * it is no longer being used.
+ *
+ * If the kobject was not able to be created, NULL will be returned.
+ */
+struct kobject *kobject_create_and_add(const char *name, struct kobject *parent)
+{
+ struct kobject *kobj;
+ int retval;
+
+ kobj = kobject_create();
+ if (!kobj)
+ return NULL;
+
+ retval = kobject_add_ng(kobj, parent, "%s", name);
+ if (retval) {
+ printk(KERN_WARNING "%s: kobject_add error: %d\n",
+ __FUNCTION__, retval);
+ kobject_put(kobj);
+ kobj = NULL;
+ }
+ return kobj;
+}
+EXPORT_SYMBOL_GPL(kobject_create_and_add);
+
/**
* kobject_kset_add_dir - add sub directory of object.
* @kset: kset the directory is belongs to.
@@ -645,23 +696,17 @@ struct kobject *kobject_kset_add_dir(struct kset *kset,
struct kobject *k;
int ret;
- if (!parent)
- return NULL;
-
- k = kzalloc(sizeof(*k), GFP_KERNEL);
+ k = kobject_create();
if (!k)
return NULL;
k->kset = kset;
- k->parent = parent;
- k->ktype = &dir_ktype;
- kobject_set_name(k, name);
- ret = kobject_register(k);
+ ret = kobject_add_ng(k, parent, "%s", name);
if (ret < 0) {
- printk(KERN_WARNING "%s: kobject_register error: %d\n",
+ printk(KERN_WARNING "%s: kobject_add error: %d\n",
__func__, ret);
- kobject_del(k);
- return NULL;
+ kobject_put(k);
+ k = NULL;
}
return k;
@@ -676,7 +721,7 @@ struct kobject *kobject_kset_add_dir(struct kset *kset,
*/
struct kobject *kobject_add_dir(struct kobject *parent, const char *name)
{
- return kobject_kset_add_dir(NULL, parent, name);
+ return kobject_create_and_add(name, parent);
}
/**