include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[safe/jmp/linux-2.6] / drivers / base / driver.c
index a35f041..b631f7c 100644 (file)
 #include <linux/device.h>
 #include <linux/module.h>
 #include <linux/errno.h>
+#include <linux/slab.h>
 #include <linux/string.h>
 #include "base.h"
 
-#define to_dev(node) container_of(node, struct device, driver_list)
-
-
 static struct device *next_device(struct klist_iter *i)
 {
        struct klist_node *n = klist_next(i);
-       return n ? container_of(n, struct device, knode_driver) : NULL;
+       struct device *dev = NULL;
+       struct device_private *dev_prv;
+
+       if (n) {
+               dev_prv = to_device_private_driver(n);
+               dev = dev_prv->device;
+       }
+       return dev;
 }
 
 /**
@@ -45,7 +50,7 @@ int driver_for_each_device(struct device_driver *drv, struct device *start,
                return -EINVAL;
 
        klist_iter_init_node(&drv->p->klist_devices, &i,
-                            start ? &start->knode_driver : NULL);
+                            start ? &start->p->knode_driver : NULL);
        while ((dev = next_device(&i)) && !error)
                error = fn(dev, data);
        klist_iter_exit(&i);
@@ -79,7 +84,7 @@ struct device *driver_find_device(struct device_driver *drv,
                return NULL;
 
        klist_iter_init_node(&drv->p->klist_devices, &i,
-                            (start ? &start->knode_driver : NULL));
+                            (start ? &start->p->knode_driver : NULL));
        while ((dev = next_device(&i)))
                if (match(dev, data) && get_device(dev))
                        break;
@@ -94,13 +99,12 @@ EXPORT_SYMBOL_GPL(driver_find_device);
  * @attr: driver attribute descriptor.
  */
 int driver_create_file(struct device_driver *drv,
-                      struct driver_attribute *attr)
+                      const struct driver_attribute *attr)
 {
        int error;
-       if (get_driver(drv)) {
+       if (drv)
                error = sysfs_create_file(&drv->p->kobj, &attr->attr);
-               put_driver(drv);
-       } else
+       else
                error = -EINVAL;
        return error;
 }
@@ -112,17 +116,18 @@ EXPORT_SYMBOL_GPL(driver_create_file);
  * @attr: driver attribute descriptor.
  */
 void driver_remove_file(struct device_driver *drv,
-                       struct driver_attribute *attr)
+                       const struct driver_attribute *attr)
 {
-       if (get_driver(drv)) {
+       if (drv)
                sysfs_remove_file(&drv->p->kobj, &attr->attr);
-               put_driver(drv);
-       }
 }
 EXPORT_SYMBOL_GPL(driver_remove_file);
 
 /**
  * driver_add_kobj - add a kobject below the specified driver
+ * @drv: requesting device driver
+ * @kobj: kobject to add below this driver
+ * @fmt: format string that names the kobject
  *
  * You really don't want to do this, this is only here due to one looney
  * iseries driver, go poke those developers if you are annoyed about
@@ -133,6 +138,7 @@ int driver_add_kobj(struct device_driver *drv, struct kobject *kobj,
 {
        va_list args;
        char *name;
+       int ret;
 
        va_start(args, fmt);
        name = kvasprintf(GFP_KERNEL, fmt, args);
@@ -141,7 +147,9 @@ int driver_add_kobj(struct device_driver *drv, struct kobject *kobj,
        if (!name)
                return -ENOMEM;
 
-       return kobject_add(kobj, &drv->p->kobj, "%s", name);
+       ret = kobject_add(kobj, &drv->p->kobj, "%s", name);
+       kfree(name);
+       return ret;
 }
 EXPORT_SYMBOL_GPL(driver_add_kobj);
 
@@ -174,7 +182,7 @@ void put_driver(struct device_driver *drv)
 EXPORT_SYMBOL_GPL(put_driver);
 
 static int driver_add_groups(struct device_driver *drv,
-                            struct attribute_group **groups)
+                            const struct attribute_group **groups)
 {
        int error = 0;
        int i;
@@ -194,7 +202,7 @@ static int driver_add_groups(struct device_driver *drv,
 }
 
 static void driver_remove_groups(struct device_driver *drv,
-                                struct attribute_group **groups)
+                                const struct attribute_group **groups)
 {
        int i;
 
@@ -214,12 +222,24 @@ static void driver_remove_groups(struct device_driver *drv,
 int driver_register(struct device_driver *drv)
 {
        int ret;
+       struct device_driver *other;
+
+       BUG_ON(!drv->bus->p);
 
        if ((drv->bus->probe && drv->probe) ||
            (drv->bus->remove && drv->remove) ||
            (drv->bus->shutdown && drv->shutdown))
                printk(KERN_WARNING "Driver '%s' needs updating - please use "
                        "bus_type methods\n", drv->name);
+
+       other = driver_find(drv->name, drv->bus);
+       if (other) {
+               put_driver(other);
+               printk(KERN_ERR "Error: Driver '%s' is already registered, "
+                       "aborting...\n", drv->name);
+               return -EBUSY;
+       }
+
        ret = bus_add_driver(drv);
        if (ret)
                return ret;
@@ -238,6 +258,10 @@ EXPORT_SYMBOL_GPL(driver_register);
  */
 void driver_unregister(struct device_driver *drv)
 {
+       if (!drv || !drv->p) {
+               WARN(1, "Unexpected driver unregister!\n");
+               return;
+       }
        driver_remove_groups(drv, drv->groups);
        bus_remove_driver(drv);
 }