]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - drivers/base/bus.c
Driver core: add bus_sort_breadthfirst() function
[linux-2.6-omap-h63xx.git] / drivers / base / bus.c
index 055989e94799eff3fb6e0b44823835257e8feb28..5aee1c0169eafed102c699281b5bb4875fefd20c 100644 (file)
@@ -79,7 +79,7 @@ static void driver_release(struct kobject *kobj)
 {
        struct driver_private *drv_priv = to_driver(kobj);
 
-       pr_debug("driver: '%s': %s\n", kobject_name(kobj), __FUNCTION__);
+       pr_debug("driver: '%s': %s\n", kobject_name(kobj), __func__);
        kfree(drv_priv);
 }
 
@@ -333,9 +333,7 @@ static int match_name(struct device *dev, void *data)
 {
        const char *name = data;
 
-       if (strcmp(name, dev->bus_id) == 0)
-               return 1;
-       return 0;
+       return sysfs_streq(name, dev->bus_id);
 }
 
 /**
@@ -505,14 +503,11 @@ void bus_attach_device(struct device *dev)
        int ret = 0;
 
        if (bus) {
-               dev->is_registered = 1;
                if (bus->p->drivers_autoprobe)
                        ret = device_attach(dev);
                WARN_ON(ret < 0);
                if (ret >= 0)
                        klist_add_tail(&dev->knode_bus, &bus->p->klist_devices);
-               else
-                       dev->is_registered = 0;
        }
 }
 
@@ -533,10 +528,9 @@ void bus_remove_device(struct device *dev)
                sysfs_remove_link(&dev->bus->p->devices_kset->kobj,
                                  dev->bus_id);
                device_remove_attrs(dev->bus, dev);
-               if (dev->is_registered) {
-                       dev->is_registered = 0;
+               if (klist_node_attached(&dev->knode_bus))
                        klist_del(&dev->knode_bus);
-               }
+
                pr_debug("bus: '%s': remove device %s\n",
                         dev->bus->name, dev->bus_id);
                device_release_driver(dev);
@@ -658,9 +652,10 @@ int bus_add_driver(struct device_driver *drv)
        pr_debug("bus: '%s': add driver %s\n", bus->name, drv->name);
 
        priv = kzalloc(sizeof(*priv), GFP_KERNEL);
-       if (!priv)
-               return -ENOMEM;
-
+       if (!priv) {
+               error = -ENOMEM;
+               goto out_put_bus;
+       }
        klist_init(&priv->klist_devices, NULL, NULL);
        priv->driver = drv;
        drv->p = priv;
@@ -668,7 +663,7 @@ int bus_add_driver(struct device_driver *drv)
        error = kobject_init_and_add(&priv->kobj, &driver_ktype, NULL,
                                     "%s", drv->name);
        if (error)
-               goto out_put_bus;
+               goto out_unregister;
 
        if (drv->bus->p->drivers_autoprobe) {
                error = driver_attach(drv);
@@ -681,19 +676,19 @@ int bus_add_driver(struct device_driver *drv)
        error = driver_create_file(drv, &driver_attr_uevent);
        if (error) {
                printk(KERN_ERR "%s: uevent attr (%s) failed\n",
-                       __FUNCTION__, drv->name);
+                       __func__, drv->name);
        }
        error = driver_add_attrs(bus, drv);
        if (error) {
                /* How the hell do we get out of this pickle? Give up */
                printk(KERN_ERR "%s: driver_add_attrs(%s) failed\n",
-                       __FUNCTION__, drv->name);
+                       __func__, drv->name);
        }
        error = add_bind_files(drv);
        if (error) {
                /* Ditto */
                printk(KERN_ERR "%s: add_bind_files(%s) failed\n",
-                       __FUNCTION__, drv->name);
+                       __func__, drv->name);
        }
 
        kobject_uevent(&priv->kobj, KOBJ_ADD);
@@ -985,6 +980,56 @@ struct klist *bus_get_device_klist(struct bus_type *bus)
 }
 EXPORT_SYMBOL_GPL(bus_get_device_klist);
 
+/*
+ * Yes, this forcably breaks the klist abstraction temporarily.  It
+ * just wants to sort the klist, not change reference counts and
+ * take/drop locks rapidly in the process.  It does all this while
+ * holding the lock for the list, so objects can't otherwise be
+ * added/removed while we're swizzling.
+ */
+static void device_insertion_sort_klist(struct device *a, struct list_head *list,
+                                       int (*compare)(const struct device *a,
+                                                       const struct device *b))
+{
+       struct list_head *pos;
+       struct klist_node *n;
+       struct device *b;
+
+       list_for_each(pos, list) {
+               n = container_of(pos, struct klist_node, n_node);
+               b = container_of(n, struct device, knode_bus);
+               if (compare(a, b) <= 0) {
+                       list_move_tail(&a->knode_bus.n_node,
+                                      &b->knode_bus.n_node);
+                       return;
+               }
+       }
+       list_move_tail(&a->knode_bus.n_node, list);
+}
+
+void bus_sort_breadthfirst(struct bus_type *bus,
+                          int (*compare)(const struct device *a,
+                                         const struct device *b))
+{
+       LIST_HEAD(sorted_devices);
+       struct list_head *pos, *tmp;
+       struct klist_node *n;
+       struct device *dev;
+       struct klist *device_klist;
+
+       device_klist = bus_get_device_klist(bus);
+
+       spin_lock(&device_klist->k_lock);
+       list_for_each_safe(pos, tmp, &device_klist->k_list) {
+               n = container_of(pos, struct klist_node, n_node);
+               dev = container_of(n, struct device, knode_bus);
+               device_insertion_sort_klist(dev, &sorted_devices, compare);
+       }
+       list_splice(&sorted_devices, &device_klist->k_list);
+       spin_unlock(&device_klist->k_lock);
+}
+EXPORT_SYMBOL_GPL(bus_sort_breadthfirst);
+
 int __init buses_init(void)
 {
        bus_kset = kset_create_and_add("bus", &bus_uevent_ops, NULL);