fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / drivers / base / driver.c
index 735bd17..1214cbd 100644 (file)
@@ -8,7 +8,6 @@
  *
  */
 
-#include <linux/config.h>
 #include <linux/device.h>
 #include <linux/module.h>
 #include <linux/errno.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)
+{
+       struct klist_node * n = klist_next(i);
+       return n ? container_of(n, struct device, knode_driver) : NULL;
+}
+
+/**
+ *     driver_for_each_device - Iterator for devices bound to a driver.
+ *     @drv:   Driver we're iterating.
+ *     @start: Device to begin with
+ *     @data:  Data to pass to the callback.
+ *     @fn:    Function to call for each device.
+ *
+ *     Iterate over the @drv's list of devices calling @fn for each one.
+ */
+
+int driver_for_each_device(struct device_driver * drv, struct device * start, 
+                          void * data, int (*fn)(struct device *, void *))
+{
+       struct klist_iter i;
+       struct device * dev;
+       int error = 0;
+
+       if (!drv)
+               return -EINVAL;
+
+       klist_iter_init_node(&drv->klist_devices, &i,
+                            start ? &start->knode_driver : NULL);
+       while ((dev = next_device(&i)) && !error)
+               error = fn(dev, data);
+       klist_iter_exit(&i);
+       return error;
+}
+
+EXPORT_SYMBOL_GPL(driver_for_each_device);
+
+
+/**
+ * driver_find_device - device iterator for locating a particular device.
+ * @drv: The device's driver
+ * @start: Device to begin with
+ * @data: Data to pass to match function
+ * @match: Callback function to check device
+ *
+ * This is similar to the driver_for_each_device() function above, but
+ * it returns a reference to a device that is 'found' for later use, as
+ * determined by the @match callback.
+ *
+ * The callback should return 0 if the device doesn't match and non-zero
+ * if it does.  If the callback returns non-zero, this function will
+ * return to the caller and not iterate over any more devices.
+ */
+struct device * driver_find_device(struct device_driver *drv,
+                                  struct device * start, void * data,
+                                  int (*match)(struct device *, void *))
+{
+       struct klist_iter i;
+       struct device *dev;
+
+       if (!drv)
+               return NULL;
+
+       klist_iter_init_node(&drv->klist_devices, &i,
+                            (start ? &start->knode_driver : NULL));
+       while ((dev = next_device(&i)))
+               if (match(dev, data) && get_device(dev))
+                       break;
+       klist_iter_exit(&i);
+       return dev;
+}
+EXPORT_SYMBOL_GPL(driver_find_device);
+
 /**
  *     driver_create_file - create sysfs file for driver.
  *     @drv:   driver.
@@ -70,7 +142,6 @@ void put_driver(struct device_driver * drv)
        kobject_put(&drv->kobj);
 }
 
-
 /**
  *     driver_register - register driver with bus
  *     @drv:   driver to register
@@ -79,14 +150,19 @@ void put_driver(struct device_driver * drv)
  *     since most of the things we have to do deal with the bus
  *     structures.
  *
- *     The one interesting aspect is that we initialize @drv->unload_sem
- *     to a locked state here. It will be unlocked when the driver
- *     reference count reaches 0.
+ *     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)
 {
-       INIT_LIST_HEAD(&drv->devices);
-       init_MUTEX_LOCKED(&drv->unload_sem);
+       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, NULL, NULL);
+       init_completion(&drv->unloaded);
        return bus_add_driver(drv);
 }
 
@@ -97,7 +173,7 @@ int driver_register(struct device_driver * drv)
  *
  *     Again, we pass off most of the work to the bus-level call.
  *
- *     Though, once that is done, we attempt to take @drv->unload_sem.
+ *     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
@@ -107,8 +183,7 @@ int driver_register(struct device_driver * drv)
 void driver_unregister(struct device_driver * drv)
 {
        bus_remove_driver(drv);
-       down(&drv->unload_sem);
-       up(&drv->unload_sem);
+       wait_for_completion(&drv->unloaded);
 }
 
 /**
@@ -129,11 +204,11 @@ struct device_driver *driver_find(const char *name, struct bus_type *bus)
        return NULL;
 }
 
-EXPORT_SYMBOL(driver_register);
-EXPORT_SYMBOL(driver_unregister);
-EXPORT_SYMBOL(get_driver);
-EXPORT_SYMBOL(put_driver);
-EXPORT_SYMBOL(driver_find);
+EXPORT_SYMBOL_GPL(driver_register);
+EXPORT_SYMBOL_GPL(driver_unregister);
+EXPORT_SYMBOL_GPL(get_driver);
+EXPORT_SYMBOL_GPL(put_driver);
+EXPORT_SYMBOL_GPL(driver_find);
 
-EXPORT_SYMBOL(driver_create_file);
-EXPORT_SYMBOL(driver_remove_file);
+EXPORT_SYMBOL_GPL(driver_create_file);
+EXPORT_SYMBOL_GPL(driver_remove_file);