From foo@baz Sun Jan 25 21:05:55 PST 2009 Date: Sun, 25 Jan 2009 21:05:55 -0800 To: Greg KH From: Greg Kroah-Hartman Subject: Driver core: add dev_init_and_add() Lots of times code calls device_initialize() and then device_add(), so put the two together to make it easier for people to remember what needs to be set up to do this properly. It also handles all of the error handling in one convenient place. Cc: Kay Sievers Signed-off-by: Greg Kroah-Hartman --- drivers/base/core.c | 30 ++++++++++++++++++++++++++++++ include/linux/device.h | 6 ++++++ 2 files changed, 36 insertions(+) --- a/drivers/base/core.c +++ b/drivers/base/core.c @@ -1370,6 +1370,36 @@ void root_device_unregister(struct devic } EXPORT_SYMBOL_GPL(root_device_unregister); +int device_init_and_add(struct device *dev, struct device *parent, + struct bus_type *bus, + void (*release)(struct device *dev), + const char *name, ...) +{ + va_list vargs; + int err; + + if (release == NULL) { + printk(KERN_ERR "%s must have a valid release function\n", + __func__); + WARN_ON(1); + return -EINVAL; + } + + dev->parent = parent; + dev->bus = bus; + dev->release = release; + + device_initialize(dev); + + va_start(vargs, name); + err = kobject_set_name_vargs(&dev->kobj, name, vargs); + va_end(vargs); + if (err) + return err; + + return device_add(dev); +} +EXPORT_SYMBOL_GPL(device_init_and_add); static void device_create_release(struct device *dev) { --- a/include/linux/device.h +++ b/include/linux/device.h @@ -547,6 +547,12 @@ extern int __must_check device_register( extern void device_unregister(struct device *dev); extern void device_initialize(struct device *dev); extern int __must_check device_add(struct device *dev); +extern int __must_check device_init_and_add(struct device *dev, + struct device *parent, + struct bus_type *bus, + void (*release)(struct device *dev), + const char *name, ...) + __attribute__((format(printf, 5, 6))); extern void device_del(struct device *dev); extern int device_for_each_child(struct device *dev, void *data, int (*fn)(struct device *dev, void *data));