Merge /spare/repo/linux-2.6/
[powerpc.git] / drivers / base / driver.c
index 484fed1..291c595 100644 (file)
 #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.
  *     @data:  Data to pass to the callback.
  *     @fn:    Function to call for each device.
  *
- *     Take the bus's rwsem and iterate over the @drv's list of devices,
- *     calling @fn for each one.
+ *     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 list_head * head;
+       struct klist_iter i;
        struct device * dev;
        int error = 0;
 
-       down_read(&drv->bus->subsys.rwsem);
-       head = &drv->devices;
-       dev = list_prepare_entry(start, head, driver_list);
-       list_for_each_entry_continue(dev, head, driver_list) {
-               get_device(dev);
+       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);
-               put_device(dev);
-               if (error)
-                       break;
-       }
-       up_read(&drv->bus->subsys.rwsem);
+       klist_iter_exit(&i);
        return error;
 }
 
-EXPORT_SYMBOL(driver_for_each_device);
+EXPORT_SYMBOL_GPL(driver_for_each_device);
+
+
+/**
+ * driver_find_device - device iterator for locating a particular device.
+ * @driver: 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.
@@ -120,7 +157,7 @@ void put_driver(struct device_driver * drv)
  */
 int driver_register(struct device_driver * drv)
 {
-       INIT_LIST_HEAD(&drv->devices);
+       klist_init(&drv->klist_devices);
        init_completion(&drv->unloaded);
        return bus_add_driver(drv);
 }