]> pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - drivers/base/driver.c
Driver Core: add class iteration api
[linux-2.6-omap-h63xx.git] / drivers / base / driver.c
index 562600dd540a942a08b49277f06209b016fb8d07..94b697a9b4e0f9f38bc0df0d87ec7f0d8feda2e2 100644 (file)
@@ -3,6 +3,8 @@
  *
  * Copyright (c) 2002-3 Patrick Mochel
  * Copyright (c) 2002-3 Open Source Development Labs
+ * Copyright (c) 2007 Greg Kroah-Hartman <gregkh@suse.de>
+ * Copyright (c) 2007 Novell Inc.
  *
  * This file is released under the GPLv2
  *
@@ -15,7 +17,6 @@
 #include "base.h"
 
 #define to_dev(node) container_of(node, struct device, driver_list)
-#define to_drv(obj) container_of(obj, struct device_driver, kobj)
 
 
 static struct device * next_device(struct klist_iter * i)
@@ -44,7 +45,7 @@ int driver_for_each_device(struct device_driver * drv, struct device * start,
        if (!drv)
                return -EINVAL;
 
-       klist_iter_init_node(&drv->klist_devices, &i,
+       klist_iter_init_node(&drv->p->klist_devices, &i,
                             start ? &start->knode_driver : NULL);
        while ((dev = next_device(&i)) && !error)
                error = fn(dev, data);
@@ -80,7 +81,7 @@ struct device * driver_find_device(struct device_driver *drv,
        if (!drv)
                return NULL;
 
-       klist_iter_init_node(&drv->klist_devices, &i,
+       klist_iter_init_node(&drv->p->klist_devices, &i,
                             (start ? &start->knode_driver : NULL));
        while ((dev = next_device(&i)))
                if (match(dev, data) && get_device(dev))
@@ -100,7 +101,7 @@ int driver_create_file(struct device_driver * drv, struct driver_attribute * att
 {
        int error;
        if (get_driver(drv)) {
-               error = sysfs_create_file(&drv->kobj, &attr->attr);
+               error = sysfs_create_file(&drv->p->kobj, &attr->attr);
                put_driver(drv);
        } else
                error = -EINVAL;
@@ -117,19 +118,51 @@ int driver_create_file(struct device_driver * drv, struct driver_attribute * att
 void driver_remove_file(struct device_driver * drv, struct driver_attribute * attr)
 {
        if (get_driver(drv)) {
-               sysfs_remove_file(&drv->kobj, &attr->attr);
+               sysfs_remove_file(&drv->p->kobj, &attr->attr);
                put_driver(drv);
        }
 }
 
 
+/**
+ * driver_add_kobj - add a kobject below the specified driver
+ *
+ * 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
+ * this...
+ */
+int driver_add_kobj(struct device_driver *drv, struct kobject *kobj,
+                   const char *fmt, ...)
+{
+       va_list args;
+       char *name;
+
+       va_start(args, fmt);
+       name = kvasprintf(GFP_KERNEL, fmt, args);
+       va_end(args);
+
+       if (!name)
+               return -ENOMEM;
+
+       return kobject_add(kobj, &drv->p->kobj, "%s", name);
+}
+EXPORT_SYMBOL_GPL(driver_add_kobj);
+
 /**
  *     get_driver - increment driver reference count.
  *     @drv:   driver.
  */
 struct device_driver * get_driver(struct device_driver * drv)
 {
-       return drv ? to_drv(kobject_get(&drv->kobj)) : NULL;
+       if (drv) {
+               struct driver_private *priv;
+               struct kobject *kobj;
+
+               kobj = kobject_get(&drv->p->kobj);
+               priv = to_driver(kobj);
+               return priv->driver;
+       }
+       return NULL;
 }
 
 
@@ -139,23 +172,40 @@ struct device_driver * get_driver(struct device_driver * drv)
  */
 void put_driver(struct device_driver * drv)
 {
-       kobject_put(&drv->kobj);
+       kobject_put(&drv->p->kobj);
 }
 
-static void klist_devices_get(struct klist_node *n)
+static int driver_add_groups(struct device_driver *drv,
+                            struct attribute_group **groups)
 {
-       struct device *dev = container_of(n, struct device, knode_driver);
-
-       get_device(dev);
+       int error = 0;
+       int i;
+
+       if (groups) {
+               for (i = 0; groups[i]; i++) {
+                       error = sysfs_create_group(&drv->p->kobj, groups[i]);
+                       if (error) {
+                               while (--i >= 0)
+                                       sysfs_remove_group(&drv->p->kobj,
+                                                          groups[i]);
+                               break;
+                       }
+               }
+       }
+       return error;
 }
 
-static void klist_devices_put(struct klist_node *n)
+static void driver_remove_groups(struct device_driver *drv,
+                                struct attribute_group **groups)
 {
-       struct device *dev = container_of(n, struct device, knode_driver);
+       int i;
 
-       put_device(dev);
+       if (groups)
+               for (i = 0; groups[i]; i++)
+                       sysfs_remove_group(&drv->p->kobj, groups[i]);
 }
 
+
 /**
  *     driver_register - register driver with bus
  *     @drv:   driver to register
@@ -163,41 +213,36 @@ static void klist_devices_put(struct klist_node *n)
  *     We pass off most of the work to the bus_add_driver() call,
  *     since most of the things we have to do deal with the bus
  *     structures.
- *
- *     The one interesting aspect is that we setup @drv->unloaded
- *     as a completion that gets complete when the driver reference
- *     count reaches 0.
  */
 int driver_register(struct device_driver * drv)
 {
+       int ret;
+
        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);
        }
-       klist_init(&drv->klist_devices, klist_devices_get, klist_devices_put);
-       init_completion(&drv->unloaded);
-       return bus_add_driver(drv);
+       ret = bus_add_driver(drv);
+       if (ret)
+               return ret;
+       ret = driver_add_groups(drv, drv->groups);
+       if (ret)
+               bus_remove_driver(drv);
+       return ret;
 }
 
-
 /**
  *     driver_unregister - remove driver from system.
  *     @drv:   driver.
  *
  *     Again, we pass off most of the work to the bus-level call.
- *
- *     Though, once that is done, we wait until @drv->unloaded is completed.
- *     This will block until the driver refcount reaches 0, and it is
- *     released. Only modular drivers will call this function, and we
- *     have to guarantee that it won't complete, letting the driver
- *     unload until all references are gone.
  */
 
 void driver_unregister(struct device_driver * drv)
 {
+       driver_remove_groups(drv, drv->groups);
        bus_remove_driver(drv);
-       wait_for_completion(&drv->unloaded);
 }
 
 /**
@@ -212,9 +257,13 @@ void driver_unregister(struct device_driver * drv)
  */
 struct device_driver *driver_find(const char *name, struct bus_type *bus)
 {
-       struct kobject *k = kset_find_obj(&bus->drivers, name);
-       if (k)
-               return to_drv(k);
+       struct kobject *k = kset_find_obj(bus->p->drivers_kset, name);
+       struct driver_private *priv;
+
+       if (k) {
+               priv = to_driver(k);
+               return priv->driver;
+       }
        return NULL;
 }