]> pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - drivers/base/driver.c
Merge ../torvalds-2.6/
[linux-2.6-omap-h63xx.git] / drivers / base / driver.c
index 1b645886e9eba14046fbb0d62fd7051477ff43a5..ef3fe513e39855f60e2e73a771d558a1b91b1dab 100644 (file)
@@ -55,6 +55,41 @@ int driver_for_each_device(struct device_driver * drv, struct device * start,
 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.
  *     @drv:   driver.
@@ -107,6 +142,19 @@ void put_driver(struct device_driver * drv)
        kobject_put(&drv->kobj);
 }
 
+static void klist_devices_get(struct klist_node *n)
+{
+       struct device *dev = container_of(n, struct device, knode_driver);
+
+       get_device(dev);
+}
+
+static void klist_devices_put(struct klist_node *n)
+{
+       struct device *dev = container_of(n, struct device, knode_driver);
+
+       put_device(dev);
+}
 
 /**
  *     driver_register - register driver with bus
@@ -122,7 +170,7 @@ void put_driver(struct device_driver * drv)
  */
 int driver_register(struct device_driver * drv)
 {
-       klist_init(&drv->klist_devices);
+       klist_init(&drv->klist_devices, klist_devices_get, klist_devices_put);
        init_completion(&drv->unloaded);
        return bus_add_driver(drv);
 }