]> pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - drivers/base/bus.c
Merge master.kernel.org:/pub/scm/linux/kernel/git/davej/cpufreq
[linux-2.6-omap-h63xx.git] / drivers / base / bus.c
index c3fac7fd555e6e45debce0501c8643b36d58640e..03204bfd17afb3a64e940977474e411f74319535 100644 (file)
@@ -133,6 +133,62 @@ static struct kobj_type ktype_bus = {
 decl_subsys(bus, &ktype_bus, NULL);
 
 
+/* Manually detach a device from it's associated driver. */
+static int driver_helper(struct device *dev, void *data)
+{
+       const char *name = data;
+
+       if (strcmp(name, dev->bus_id) == 0)
+               return 1;
+       return 0;
+}
+
+static ssize_t driver_unbind(struct device_driver *drv,
+                            const char *buf, size_t count)
+{
+       struct bus_type *bus = get_bus(drv->bus);
+       struct device *dev;
+       int err = -ENODEV;
+
+       dev = bus_find_device(bus, NULL, (void *)buf, driver_helper);
+       if ((dev) &&
+           (dev->driver == drv)) {
+               device_release_driver(dev);
+               err = count;
+       }
+       if (err)
+               return err;
+       return count;
+}
+static DRIVER_ATTR(unbind, S_IWUSR, NULL, driver_unbind);
+
+/*
+ * Manually attach a device to a driver.
+ * Note: the driver must want to bind to the device,
+ * it is not possible to override the driver's id table.
+ */
+static ssize_t driver_bind(struct device_driver *drv,
+                          const char *buf, size_t count)
+{
+       struct bus_type *bus = get_bus(drv->bus);
+       struct device *dev;
+       int err = -ENODEV;
+
+       dev = bus_find_device(bus, NULL, (void *)buf, driver_helper);
+       if ((dev) &&
+           (dev->driver == NULL)) {
+               down(&dev->sem);
+               err = driver_probe_device(drv, dev);
+               up(&dev->sem);
+               put_device(dev);
+       }
+       if (err)
+               return err;
+       return count;
+}
+static DRIVER_ATTR(bind, S_IWUSR, NULL, driver_bind);
+
+
 static struct device * next_device(struct klist_iter * i)
 {
        struct klist_node * n = klist_next(i);
@@ -177,6 +233,39 @@ int bus_for_each_dev(struct bus_type * bus, struct device * start,
        return error;
 }
 
+/**
+ * bus_find_device - device iterator for locating a particular device.
+ * @bus: bus type
+ * @start: Device to begin with
+ * @data: Data to pass to match function
+ * @match: Callback function to check device
+ *
+ * This is similar to the bus_for_each_dev() 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 * bus_find_device(struct bus_type *bus,
+                               struct device *start, void *data,
+                               int (*match)(struct device *, void *))
+{
+       struct klist_iter i;
+       struct device *dev;
+
+       if (!bus)
+               return NULL;
+
+       klist_iter_init_node(&bus->klist_devices, &i,
+                            (start ? &start->knode_bus : NULL));
+       while ((dev = next_device(&i)))
+               if (match(dev, data) && get_device(dev))
+                       break;
+       klist_iter_exit(&i);
+       return dev;
+}
 
 
 static struct device_driver * next_driver(struct klist_iter * i)
@@ -271,7 +360,7 @@ int bus_add_device(struct device * dev)
        if (bus) {
                pr_debug("bus %s: add device %s\n", bus->name, dev->bus_id);
                device_attach(dev);
-               klist_add_tail(&bus->klist_devices, &dev->knode_bus);
+               klist_add_tail(&dev->knode_bus, &bus->klist_devices);
                error = device_add_attrs(bus, dev);
                if (!error) {
                        sysfs_create_link(&bus->devices.kobj, &dev->kobj, dev->bus_id);
@@ -359,10 +448,12 @@ int bus_add_driver(struct device_driver * drv)
                }
 
                driver_attach(drv);
-               klist_add_tail(&bus->klist_drivers, &drv->knode_bus);
+               klist_add_tail(&drv->knode_bus, &bus->klist_drivers);
                module_add_driver(drv->owner, drv);
 
                driver_add_attrs(bus, drv);
+               driver_create_file(drv, &driver_attr_unbind);
+               driver_create_file(drv, &driver_attr_bind);
        }
        return error;
 }
@@ -380,6 +471,8 @@ int bus_add_driver(struct device_driver * drv)
 void bus_remove_driver(struct device_driver * drv)
 {
        if (drv->bus) {
+               driver_remove_file(drv, &driver_attr_bind);
+               driver_remove_file(drv, &driver_attr_unbind);
                driver_remove_attrs(drv->bus, drv);
                klist_remove(&drv->knode_bus);
                pr_debug("bus %s: remove driver %s\n", drv->bus->name, drv->name);
@@ -394,31 +487,22 @@ void bus_remove_driver(struct device_driver * drv)
 /* Helper for bus_rescan_devices's iter */
 static int bus_rescan_devices_helper(struct device *dev, void *data)
 {
-       int *count = data;
-
-       if (!dev->driver && (device_attach(dev) > 0))
-               (*count)++;
-
+       if (!dev->driver)
+               device_attach(dev);
        return 0;
 }
 
-
 /**
- *     bus_rescan_devices - rescan devices on the bus for possible drivers
- *     @bus:   the bus to scan.
+ * bus_rescan_devices - rescan devices on the bus for possible drivers
+ * @bus: the bus to scan.
  *
- *     This function will look for devices on the bus with no driver
- *     attached and rescan it against existing drivers to see if it
- *     matches any. Calls device_attach(). Returns the number of devices
- *     that were sucessfully bound to a driver.
+ * This function will look for devices on the bus with no driver
+ * attached and rescan it against existing drivers to see if it matches
+ * any by calling device_attach() for the unbound devices.
  */
-int bus_rescan_devices(struct bus_type * bus)
+void bus_rescan_devices(struct bus_type * bus)
 {
-       int count = 0;
-
-       bus_for_each_dev(bus, NULL, &count, bus_rescan_devices_helper);
-
-       return count;
+       bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper);
 }
 
 
@@ -484,6 +568,36 @@ static void bus_remove_attrs(struct bus_type * bus)
        }
 }
 
+static void klist_devices_get(struct klist_node *n)
+{
+       struct device *dev = container_of(n, struct device, knode_bus);
+
+       get_device(dev);
+}
+
+static void klist_devices_put(struct klist_node *n)
+{
+       struct device *dev = container_of(n, struct device, knode_bus);
+
+       put_device(dev);
+}
+
+static void klist_drivers_get(struct klist_node *n)
+{
+       struct device_driver *drv = container_of(n, struct device_driver,
+                                                knode_bus);
+
+       get_driver(drv);
+}
+
+static void klist_drivers_put(struct klist_node *n)
+{
+       struct device_driver *drv = container_of(n, struct device_driver,
+                                                knode_bus);
+
+       put_driver(drv);
+}
+
 /**
  *     bus_register - register a bus with the system.
  *     @bus:   bus.
@@ -518,8 +632,8 @@ int bus_register(struct bus_type * bus)
        if (retval)
                goto bus_drivers_fail;
 
-       klist_init(&bus->klist_devices);
-       klist_init(&bus->klist_drivers);
+       klist_init(&bus->klist_devices, klist_devices_get, klist_devices_put);
+       klist_init(&bus->klist_drivers, klist_drivers_get, klist_drivers_put);
        bus_add_attrs(bus);
 
        pr_debug("bus type '%s' registered\n", bus->name);
@@ -557,6 +671,7 @@ int __init buses_init(void)
 
 
 EXPORT_SYMBOL_GPL(bus_for_each_dev);
+EXPORT_SYMBOL_GPL(bus_find_device);
 EXPORT_SYMBOL_GPL(bus_for_each_drv);
 
 EXPORT_SYMBOL_GPL(bus_add_device);