X-Git-Url: http://pilppa.org/gitweb/gitweb.cgi?a=blobdiff_plain;f=drivers%2Fbase%2Fbus.c;h=29f6af554e715ed408f0f23e8da838284dbc9dc7;hb=900b2b463dc6e65ec474d6880412c63c25b3aea9;hp=ab53832d57e5eff507b8e4552983446641f02d51;hpb=efda9452046bdd707b23a85b7846ec33548f84f1;p=linux-2.6-omap-h63xx.git diff --git a/drivers/base/bus.c b/drivers/base/bus.c index ab53832d57e..29f6af554e7 100644 --- a/drivers/base/bus.c +++ b/drivers/base/bus.c @@ -133,7 +133,7 @@ static struct kobj_type ktype_bus = { decl_subsys(bus, &ktype_bus, NULL); -/* Manually detach a device from it's associated driver. */ +/* Manually detach a device from its associated driver. */ static int driver_helper(struct device *dev, void *data) { const char *name = data; @@ -151,11 +151,16 @@ static ssize_t driver_unbind(struct device_driver *drv, int err = -ENODEV; dev = bus_find_device(bus, NULL, (void *)buf, driver_helper); - if ((dev) && - (dev->driver == drv)) { + if (dev && dev->driver == drv) { + if (dev->parent) /* Needed for USB */ + down(&dev->parent->sem); device_release_driver(dev); + if (dev->parent) + up(&dev->parent->sem); err = count; } + put_device(dev); + put_bus(bus); return err; } static DRIVER_ATTR(unbind, S_IWUSR, NULL, driver_unbind); @@ -173,16 +178,18 @@ static ssize_t driver_bind(struct device_driver *drv, int err = -ENODEV; dev = bus_find_device(bus, NULL, (void *)buf, driver_helper); - if ((dev) && - (dev->driver == NULL)) { + if (dev && dev->driver == NULL) { + if (dev->parent) /* Needed for USB */ + down(&dev->parent->sem); down(&dev->sem); err = driver_probe_device(drv, dev); up(&dev->sem); - put_device(dev); + if (dev->parent) + up(&dev->parent->sem); } - if (err) - return err; - return count; + put_device(dev); + put_bus(bus); + return err; } static DRIVER_ATTR(bind, S_IWUSR, NULL, driver_bind); @@ -358,7 +365,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); @@ -421,6 +428,26 @@ static void driver_remove_attrs(struct bus_type * bus, struct device_driver * dr } } +#ifdef CONFIG_HOTPLUG +/* + * Thanks to drivers making their tables __devinit, we can't allow manual + * bind and unbind from userspace unless CONFIG_HOTPLUG is enabled. + */ +static void add_bind_files(struct device_driver *drv) +{ + driver_create_file(drv, &driver_attr_unbind); + driver_create_file(drv, &driver_attr_bind); +} + +static void remove_bind_files(struct device_driver *drv) +{ + driver_remove_file(drv, &driver_attr_bind); + driver_remove_file(drv, &driver_attr_unbind); +} +#else +static inline void add_bind_files(struct device_driver *drv) {} +static inline void remove_bind_files(struct device_driver *drv) {} +#endif /** * bus_add_driver - Add a driver to the bus. @@ -446,12 +473,11 @@ 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); + add_bind_files(drv); } return error; } @@ -469,8 +495,7 @@ 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); + remove_bind_files(drv); driver_remove_attrs(drv->bus, drv); klist_remove(&drv->knode_bus); pr_debug("bus %s: remove driver %s\n", drv->bus->name, drv->name); @@ -485,8 +510,13 @@ 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) { - if (!dev->driver) + if (!dev->driver) { + if (dev->parent) /* Needed for USB */ + down(&dev->parent->sem); device_attach(dev); + if (dev->parent) + up(&dev->parent->sem); + } return 0; } @@ -566,6 +596,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. @@ -600,8 +660,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);