11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * bus.c - bus driver management 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (c) 2002-3 Patrick Mochel 51da177e4SLinus Torvalds * Copyright (c) 2002-3 Open Source Development Labs 6e5dd1278SGreg Kroah-Hartman * Copyright (c) 2007 Greg Kroah-Hartman <gregkh@suse.de> 7e5dd1278SGreg Kroah-Hartman * Copyright (c) 2007 Novell Inc. 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * This file is released under the GPLv2 101da177e4SLinus Torvalds * 111da177e4SLinus Torvalds */ 121da177e4SLinus Torvalds 13765230b5SDmitry Torokhov #include <linux/async.h> 141da177e4SLinus Torvalds #include <linux/device.h> 151da177e4SLinus Torvalds #include <linux/module.h> 161da177e4SLinus Torvalds #include <linux/errno.h> 175a0e3ad6STejun Heo #include <linux/slab.h> 181da177e4SLinus Torvalds #include <linux/init.h> 191da177e4SLinus Torvalds #include <linux/string.h> 20ca22e56dSKay Sievers #include <linux/mutex.h> 2163967685SGreg Kroah-Hartman #include <linux/sysfs.h> 221da177e4SLinus Torvalds #include "base.h" 231da177e4SLinus Torvalds #include "power/power.h" 241da177e4SLinus Torvalds 25ca22e56dSKay Sievers /* /sys/devices/system */ 2697ec448aSH Hartley Sweeten static struct kset *system_kset; 27ca22e56dSKay Sievers 281da177e4SLinus Torvalds #define to_bus_attr(_attr) container_of(_attr, struct bus_attribute, attr) 291da177e4SLinus Torvalds 301da177e4SLinus Torvalds /* 311da177e4SLinus Torvalds * sysfs bindings for drivers 321da177e4SLinus Torvalds */ 331da177e4SLinus Torvalds 341da177e4SLinus Torvalds #define to_drv_attr(_attr) container_of(_attr, struct driver_attribute, attr) 351da177e4SLinus Torvalds 361da177e4SLinus Torvalds 37b8c5cec2SKay Sievers static int __must_check bus_rescan_devices_helper(struct device *dev, 38b8c5cec2SKay Sievers void *data); 39b8c5cec2SKay Sievers 405901d014SGreg Kroah-Hartman static struct bus_type *bus_get(struct bus_type *bus) 415901d014SGreg Kroah-Hartman { 42c6f7e72aSGreg Kroah-Hartman if (bus) { 43c6f7e72aSGreg Kroah-Hartman kset_get(&bus->p->subsys); 44c6f7e72aSGreg Kroah-Hartman return bus; 45c6f7e72aSGreg Kroah-Hartman } 46c6f7e72aSGreg Kroah-Hartman return NULL; 475901d014SGreg Kroah-Hartman } 485901d014SGreg Kroah-Hartman 49fc1ede58SGreg Kroah-Hartman static void bus_put(struct bus_type *bus) 50fc1ede58SGreg Kroah-Hartman { 51c6f7e72aSGreg Kroah-Hartman if (bus) 52c6f7e72aSGreg Kroah-Hartman kset_put(&bus->p->subsys); 53fc1ede58SGreg Kroah-Hartman } 54fc1ede58SGreg Kroah-Hartman 554a3ad20cSGreg Kroah-Hartman static ssize_t drv_attr_show(struct kobject *kobj, struct attribute *attr, 564a3ad20cSGreg Kroah-Hartman char *buf) 571da177e4SLinus Torvalds { 581da177e4SLinus Torvalds struct driver_attribute *drv_attr = to_drv_attr(attr); 59e5dd1278SGreg Kroah-Hartman struct driver_private *drv_priv = to_driver(kobj); 604a0c20bfSDmitry Torokhov ssize_t ret = -EIO; 611da177e4SLinus Torvalds 621da177e4SLinus Torvalds if (drv_attr->show) 63e5dd1278SGreg Kroah-Hartman ret = drv_attr->show(drv_priv->driver, buf); 641da177e4SLinus Torvalds return ret; 651da177e4SLinus Torvalds } 661da177e4SLinus Torvalds 674a3ad20cSGreg Kroah-Hartman static ssize_t drv_attr_store(struct kobject *kobj, struct attribute *attr, 681da177e4SLinus Torvalds const char *buf, size_t count) 691da177e4SLinus Torvalds { 701da177e4SLinus Torvalds struct driver_attribute *drv_attr = to_drv_attr(attr); 71e5dd1278SGreg Kroah-Hartman struct driver_private *drv_priv = to_driver(kobj); 724a0c20bfSDmitry Torokhov ssize_t ret = -EIO; 731da177e4SLinus Torvalds 741da177e4SLinus Torvalds if (drv_attr->store) 75e5dd1278SGreg Kroah-Hartman ret = drv_attr->store(drv_priv->driver, buf, count); 761da177e4SLinus Torvalds return ret; 771da177e4SLinus Torvalds } 781da177e4SLinus Torvalds 7952cf25d0SEmese Revfy static const struct sysfs_ops driver_sysfs_ops = { 801da177e4SLinus Torvalds .show = drv_attr_show, 811da177e4SLinus Torvalds .store = drv_attr_store, 821da177e4SLinus Torvalds }; 831da177e4SLinus Torvalds 841da177e4SLinus Torvalds static void driver_release(struct kobject *kobj) 851da177e4SLinus Torvalds { 86e5dd1278SGreg Kroah-Hartman struct driver_private *drv_priv = to_driver(kobj); 87e5dd1278SGreg Kroah-Hartman 882b3a302aSHarvey Harrison pr_debug("driver: '%s': %s\n", kobject_name(kobj), __func__); 89e5dd1278SGreg Kroah-Hartman kfree(drv_priv); 901da177e4SLinus Torvalds } 911da177e4SLinus Torvalds 92a1148fb0SGreg Kroah-Hartman static struct kobj_type driver_ktype = { 931da177e4SLinus Torvalds .sysfs_ops = &driver_sysfs_ops, 941da177e4SLinus Torvalds .release = driver_release, 951da177e4SLinus Torvalds }; 961da177e4SLinus Torvalds 971da177e4SLinus Torvalds /* 981da177e4SLinus Torvalds * sysfs bindings for buses 991da177e4SLinus Torvalds */ 1004a3ad20cSGreg Kroah-Hartman static ssize_t bus_attr_show(struct kobject *kobj, struct attribute *attr, 1014a3ad20cSGreg Kroah-Hartman char *buf) 1021da177e4SLinus Torvalds { 1031da177e4SLinus Torvalds struct bus_attribute *bus_attr = to_bus_attr(attr); 1046b6e39a6SKay Sievers struct subsys_private *subsys_priv = to_subsys_private(kobj); 1051da177e4SLinus Torvalds ssize_t ret = 0; 1061da177e4SLinus Torvalds 1071da177e4SLinus Torvalds if (bus_attr->show) 1086b6e39a6SKay Sievers ret = bus_attr->show(subsys_priv->bus, buf); 1091da177e4SLinus Torvalds return ret; 1101da177e4SLinus Torvalds } 1111da177e4SLinus Torvalds 1124a3ad20cSGreg Kroah-Hartman static ssize_t bus_attr_store(struct kobject *kobj, struct attribute *attr, 1131da177e4SLinus Torvalds const char *buf, size_t count) 1141da177e4SLinus Torvalds { 1151da177e4SLinus Torvalds struct bus_attribute *bus_attr = to_bus_attr(attr); 1166b6e39a6SKay Sievers struct subsys_private *subsys_priv = to_subsys_private(kobj); 1171da177e4SLinus Torvalds ssize_t ret = 0; 1181da177e4SLinus Torvalds 1191da177e4SLinus Torvalds if (bus_attr->store) 1206b6e39a6SKay Sievers ret = bus_attr->store(subsys_priv->bus, buf, count); 1211da177e4SLinus Torvalds return ret; 1221da177e4SLinus Torvalds } 1231da177e4SLinus Torvalds 12452cf25d0SEmese Revfy static const struct sysfs_ops bus_sysfs_ops = { 1251da177e4SLinus Torvalds .show = bus_attr_show, 1261da177e4SLinus Torvalds .store = bus_attr_store, 1271da177e4SLinus Torvalds }; 1281da177e4SLinus Torvalds 1291da177e4SLinus Torvalds int bus_create_file(struct bus_type *bus, struct bus_attribute *attr) 1301da177e4SLinus Torvalds { 1311da177e4SLinus Torvalds int error; 1325901d014SGreg Kroah-Hartman if (bus_get(bus)) { 133c6f7e72aSGreg Kroah-Hartman error = sysfs_create_file(&bus->p->subsys.kobj, &attr->attr); 134fc1ede58SGreg Kroah-Hartman bus_put(bus); 1351da177e4SLinus Torvalds } else 1361da177e4SLinus Torvalds error = -EINVAL; 1371da177e4SLinus Torvalds return error; 1381da177e4SLinus Torvalds } 1394a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_create_file); 1401da177e4SLinus Torvalds 1411da177e4SLinus Torvalds void bus_remove_file(struct bus_type *bus, struct bus_attribute *attr) 1421da177e4SLinus Torvalds { 1435901d014SGreg Kroah-Hartman if (bus_get(bus)) { 144c6f7e72aSGreg Kroah-Hartman sysfs_remove_file(&bus->p->subsys.kobj, &attr->attr); 145fc1ede58SGreg Kroah-Hartman bus_put(bus); 1461da177e4SLinus Torvalds } 1471da177e4SLinus Torvalds } 1484a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_remove_file); 1491da177e4SLinus Torvalds 150174be70bSBart Van Assche static void bus_release(struct kobject *kobj) 151174be70bSBart Van Assche { 152371fd7a2SGeliang Tang struct subsys_private *priv = to_subsys_private(kobj); 153174be70bSBart Van Assche struct bus_type *bus = priv->bus; 154174be70bSBart Van Assche 155174be70bSBart Van Assche kfree(priv); 156174be70bSBart Van Assche bus->p = NULL; 157174be70bSBart Van Assche } 158174be70bSBart Van Assche 15980f03e34SKay Sievers static struct kobj_type bus_ktype = { 1601da177e4SLinus Torvalds .sysfs_ops = &bus_sysfs_ops, 161174be70bSBart Van Assche .release = bus_release, 1621da177e4SLinus Torvalds }; 1631da177e4SLinus Torvalds 16480f03e34SKay Sievers static int bus_uevent_filter(struct kset *kset, struct kobject *kobj) 16580f03e34SKay Sievers { 16680f03e34SKay Sievers struct kobj_type *ktype = get_ktype(kobj); 16780f03e34SKay Sievers 16880f03e34SKay Sievers if (ktype == &bus_ktype) 16980f03e34SKay Sievers return 1; 17080f03e34SKay Sievers return 0; 17180f03e34SKay Sievers } 17280f03e34SKay Sievers 1739cd43611SEmese Revfy static const struct kset_uevent_ops bus_uevent_ops = { 17480f03e34SKay Sievers .filter = bus_uevent_filter, 17580f03e34SKay Sievers }; 17680f03e34SKay Sievers 17759a54833SGreg Kroah-Hartman static struct kset *bus_kset; 1781da177e4SLinus Torvalds 1792b08c8d0SAlan Stern /* Manually detach a device from its associated driver. */ 1802581c9ccSGreg Kroah-Hartman static ssize_t unbind_store(struct device_driver *drv, const char *buf, 1812581c9ccSGreg Kroah-Hartman size_t count) 182151ef38fSGreg Kroah-Hartman { 1835901d014SGreg Kroah-Hartman struct bus_type *bus = bus_get(drv->bus); 184151ef38fSGreg Kroah-Hartman struct device *dev; 185151ef38fSGreg Kroah-Hartman int err = -ENODEV; 186151ef38fSGreg Kroah-Hartman 1871f9ffc04SGreg Kroah-Hartman dev = bus_find_device_by_name(bus, NULL, buf); 1882b08c8d0SAlan Stern if (dev && dev->driver == drv) { 189bf74ad5bSAlan Stern if (dev->parent) /* Needed for USB */ 1908e9394ceSGreg Kroah-Hartman device_lock(dev->parent); 191151ef38fSGreg Kroah-Hartman device_release_driver(dev); 192bf74ad5bSAlan Stern if (dev->parent) 1938e9394ceSGreg Kroah-Hartman device_unlock(dev->parent); 194151ef38fSGreg Kroah-Hartman err = count; 195151ef38fSGreg Kroah-Hartman } 1962b08c8d0SAlan Stern put_device(dev); 197fc1ede58SGreg Kroah-Hartman bus_put(bus); 198151ef38fSGreg Kroah-Hartman return err; 199151ef38fSGreg Kroah-Hartman } 2002581c9ccSGreg Kroah-Hartman static DRIVER_ATTR_WO(unbind); 201151ef38fSGreg Kroah-Hartman 202afdce75fSGreg Kroah-Hartman /* 203afdce75fSGreg Kroah-Hartman * Manually attach a device to a driver. 204afdce75fSGreg Kroah-Hartman * Note: the driver must want to bind to the device, 205afdce75fSGreg Kroah-Hartman * it is not possible to override the driver's id table. 206afdce75fSGreg Kroah-Hartman */ 2072581c9ccSGreg Kroah-Hartman static ssize_t bind_store(struct device_driver *drv, const char *buf, 2082581c9ccSGreg Kroah-Hartman size_t count) 209afdce75fSGreg Kroah-Hartman { 2105901d014SGreg Kroah-Hartman struct bus_type *bus = bus_get(drv->bus); 211afdce75fSGreg Kroah-Hartman struct device *dev; 212afdce75fSGreg Kroah-Hartman int err = -ENODEV; 213afdce75fSGreg Kroah-Hartman 2141f9ffc04SGreg Kroah-Hartman dev = bus_find_device_by_name(bus, NULL, buf); 21549b420a1SMing Lei if (dev && dev->driver == NULL && driver_match_device(drv, dev)) { 216bf74ad5bSAlan Stern if (dev->parent) /* Needed for USB */ 2178e9394ceSGreg Kroah-Hartman device_lock(dev->parent); 2188e9394ceSGreg Kroah-Hartman device_lock(dev); 219afdce75fSGreg Kroah-Hartman err = driver_probe_device(drv, dev); 2208e9394ceSGreg Kroah-Hartman device_unlock(dev); 221bf74ad5bSAlan Stern if (dev->parent) 2228e9394ceSGreg Kroah-Hartman device_unlock(dev->parent); 22337225401SRyan Wilson 2244a3ad20cSGreg Kroah-Hartman if (err > 0) { 2254a3ad20cSGreg Kroah-Hartman /* success */ 22637225401SRyan Wilson err = count; 2274a3ad20cSGreg Kroah-Hartman } else if (err == 0) { 2284a3ad20cSGreg Kroah-Hartman /* driver didn't accept device */ 22937225401SRyan Wilson err = -ENODEV; 230afdce75fSGreg Kroah-Hartman } 2314a3ad20cSGreg Kroah-Hartman } 2322b08c8d0SAlan Stern put_device(dev); 233fc1ede58SGreg Kroah-Hartman bus_put(bus); 234afdce75fSGreg Kroah-Hartman return err; 235afdce75fSGreg Kroah-Hartman } 2362581c9ccSGreg Kroah-Hartman static DRIVER_ATTR_WO(bind); 237afdce75fSGreg Kroah-Hartman 238b8c5cec2SKay Sievers static ssize_t show_drivers_autoprobe(struct bus_type *bus, char *buf) 239b8c5cec2SKay Sievers { 240c6f7e72aSGreg Kroah-Hartman return sprintf(buf, "%d\n", bus->p->drivers_autoprobe); 241b8c5cec2SKay Sievers } 242b8c5cec2SKay Sievers 243b8c5cec2SKay Sievers static ssize_t store_drivers_autoprobe(struct bus_type *bus, 244b8c5cec2SKay Sievers const char *buf, size_t count) 245b8c5cec2SKay Sievers { 246b8c5cec2SKay Sievers if (buf[0] == '0') 247c6f7e72aSGreg Kroah-Hartman bus->p->drivers_autoprobe = 0; 248b8c5cec2SKay Sievers else 249c6f7e72aSGreg Kroah-Hartman bus->p->drivers_autoprobe = 1; 250b8c5cec2SKay Sievers return count; 251b8c5cec2SKay Sievers } 252b8c5cec2SKay Sievers 253b8c5cec2SKay Sievers static ssize_t store_drivers_probe(struct bus_type *bus, 254b8c5cec2SKay Sievers const char *buf, size_t count) 255b8c5cec2SKay Sievers { 256b8c5cec2SKay Sievers struct device *dev; 2570372ffb3SAlex Williamson int err = -EINVAL; 258b8c5cec2SKay Sievers 2591f9ffc04SGreg Kroah-Hartman dev = bus_find_device_by_name(bus, NULL, buf); 260b8c5cec2SKay Sievers if (!dev) 261b8c5cec2SKay Sievers return -ENODEV; 2620372ffb3SAlex Williamson if (bus_rescan_devices_helper(dev, NULL) == 0) 2630372ffb3SAlex Williamson err = count; 2640372ffb3SAlex Williamson put_device(dev); 2650372ffb3SAlex Williamson return err; 266b8c5cec2SKay Sievers } 267151ef38fSGreg Kroah-Hartman 268465c7a3aSmochel@digitalimplant.org static struct device *next_device(struct klist_iter *i) 269465c7a3aSmochel@digitalimplant.org { 270465c7a3aSmochel@digitalimplant.org struct klist_node *n = klist_next(i); 271ae1b4171SGreg Kroah-Hartman struct device *dev = NULL; 272ae1b4171SGreg Kroah-Hartman struct device_private *dev_prv; 273ae1b4171SGreg Kroah-Hartman 274ae1b4171SGreg Kroah-Hartman if (n) { 275ae1b4171SGreg Kroah-Hartman dev_prv = to_device_private_bus(n); 276ae1b4171SGreg Kroah-Hartman dev = dev_prv->device; 277ae1b4171SGreg Kroah-Hartman } 278ae1b4171SGreg Kroah-Hartman return dev; 279465c7a3aSmochel@digitalimplant.org } 280465c7a3aSmochel@digitalimplant.org 2811da177e4SLinus Torvalds /** 2821da177e4SLinus Torvalds * bus_for_each_dev - device iterator. 2831da177e4SLinus Torvalds * @bus: bus type. 2841da177e4SLinus Torvalds * @start: device to start iterating from. 2851da177e4SLinus Torvalds * @data: data for the callback. 2861da177e4SLinus Torvalds * @fn: function to be called for each device. 2871da177e4SLinus Torvalds * 2881da177e4SLinus Torvalds * Iterate over @bus's list of devices, and call @fn for each, 2891da177e4SLinus Torvalds * passing it @data. If @start is not NULL, we use that device to 2901da177e4SLinus Torvalds * begin iterating from. 2911da177e4SLinus Torvalds * 2921da177e4SLinus Torvalds * We check the return of @fn each time. If it returns anything 2931da177e4SLinus Torvalds * other than 0, we break out and return that value. 2941da177e4SLinus Torvalds * 2951da177e4SLinus Torvalds * NOTE: The device that returns a non-zero value is not retained 2961da177e4SLinus Torvalds * in any way, nor is its refcount incremented. If the caller needs 2970fa1b0a1SAlex Chiang * to retain this data, it should do so, and increment the reference 2981da177e4SLinus Torvalds * count in the supplied callback. 2991da177e4SLinus Torvalds */ 3001da177e4SLinus Torvalds int bus_for_each_dev(struct bus_type *bus, struct device *start, 3011da177e4SLinus Torvalds void *data, int (*fn)(struct device *, void *)) 3021da177e4SLinus Torvalds { 303465c7a3aSmochel@digitalimplant.org struct klist_iter i; 304465c7a3aSmochel@digitalimplant.org struct device *dev; 305465c7a3aSmochel@digitalimplant.org int error = 0; 3061da177e4SLinus Torvalds 3074fa3e78bSBjorn Helgaas if (!bus || !bus->p) 308465c7a3aSmochel@digitalimplant.org return -EINVAL; 309465c7a3aSmochel@digitalimplant.org 3107cd9c9bbSGreg Kroah-Hartman klist_iter_init_node(&bus->p->klist_devices, &i, 311ae1b4171SGreg Kroah-Hartman (start ? &start->p->knode_bus : NULL)); 312465c7a3aSmochel@digitalimplant.org while ((dev = next_device(&i)) && !error) 313465c7a3aSmochel@digitalimplant.org error = fn(dev, data); 314465c7a3aSmochel@digitalimplant.org klist_iter_exit(&i); 315465c7a3aSmochel@digitalimplant.org return error; 3161da177e4SLinus Torvalds } 3174a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_for_each_dev); 3181da177e4SLinus Torvalds 3190edb5860SCornelia Huck /** 3200edb5860SCornelia Huck * bus_find_device - device iterator for locating a particular device. 3210edb5860SCornelia Huck * @bus: bus type 3220edb5860SCornelia Huck * @start: Device to begin with 3230edb5860SCornelia Huck * @data: Data to pass to match function 3240edb5860SCornelia Huck * @match: Callback function to check device 3250edb5860SCornelia Huck * 3260edb5860SCornelia Huck * This is similar to the bus_for_each_dev() function above, but it 3270edb5860SCornelia Huck * returns a reference to a device that is 'found' for later use, as 3280edb5860SCornelia Huck * determined by the @match callback. 3290edb5860SCornelia Huck * 3300edb5860SCornelia Huck * The callback should return 0 if the device doesn't match and non-zero 3310edb5860SCornelia Huck * if it does. If the callback returns non-zero, this function will 3320edb5860SCornelia Huck * return to the caller and not iterate over any more devices. 3330edb5860SCornelia Huck */ 3340edb5860SCornelia Huck struct device *bus_find_device(struct bus_type *bus, 3350edb5860SCornelia Huck struct device *start, void *data, 3364a3ad20cSGreg Kroah-Hartman int (*match)(struct device *dev, void *data)) 3370edb5860SCornelia Huck { 3380edb5860SCornelia Huck struct klist_iter i; 3390edb5860SCornelia Huck struct device *dev; 3400edb5860SCornelia Huck 3414fa3e78bSBjorn Helgaas if (!bus || !bus->p) 3420edb5860SCornelia Huck return NULL; 3430edb5860SCornelia Huck 3447cd9c9bbSGreg Kroah-Hartman klist_iter_init_node(&bus->p->klist_devices, &i, 3457cd9c9bbSGreg Kroah-Hartman (start ? &start->p->knode_bus : NULL)); 3460edb5860SCornelia Huck while ((dev = next_device(&i))) 3470edb5860SCornelia Huck if (match(dev, data) && get_device(dev)) 3480edb5860SCornelia Huck break; 3490edb5860SCornelia Huck klist_iter_exit(&i); 3500edb5860SCornelia Huck return dev; 3510edb5860SCornelia Huck } 3524a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_find_device); 35338fdac3cSmochel@digitalimplant.org 3541f9ffc04SGreg Kroah-Hartman static int match_name(struct device *dev, void *data) 3551f9ffc04SGreg Kroah-Hartman { 3561f9ffc04SGreg Kroah-Hartman const char *name = data; 3571f9ffc04SGreg Kroah-Hartman 3581e0b2cf9SKay Sievers return sysfs_streq(name, dev_name(dev)); 3591f9ffc04SGreg Kroah-Hartman } 3601f9ffc04SGreg Kroah-Hartman 3611f9ffc04SGreg Kroah-Hartman /** 3621f9ffc04SGreg Kroah-Hartman * bus_find_device_by_name - device iterator for locating a particular device of a specific name 3631f9ffc04SGreg Kroah-Hartman * @bus: bus type 3641f9ffc04SGreg Kroah-Hartman * @start: Device to begin with 3651f9ffc04SGreg Kroah-Hartman * @name: name of the device to match 3661f9ffc04SGreg Kroah-Hartman * 3671f9ffc04SGreg Kroah-Hartman * This is similar to the bus_find_device() function above, but it handles 3681f9ffc04SGreg Kroah-Hartman * searching by a name automatically, no need to write another strcmp matching 3691f9ffc04SGreg Kroah-Hartman * function. 3701f9ffc04SGreg Kroah-Hartman */ 3711f9ffc04SGreg Kroah-Hartman struct device *bus_find_device_by_name(struct bus_type *bus, 3721f9ffc04SGreg Kroah-Hartman struct device *start, const char *name) 3731f9ffc04SGreg Kroah-Hartman { 3741f9ffc04SGreg Kroah-Hartman return bus_find_device(bus, start, (void *)name, match_name); 3751f9ffc04SGreg Kroah-Hartman } 3761f9ffc04SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_find_device_by_name); 3771f9ffc04SGreg Kroah-Hartman 378ca22e56dSKay Sievers /** 379ca22e56dSKay Sievers * subsys_find_device_by_id - find a device with a specific enumeration number 380ca22e56dSKay Sievers * @subsys: subsystem 381ca22e56dSKay Sievers * @id: index 'id' in struct device 382ca22e56dSKay Sievers * @hint: device to check first 383ca22e56dSKay Sievers * 384ca22e56dSKay Sievers * Check the hint's next object and if it is a match return it directly, 385ca22e56dSKay Sievers * otherwise, fall back to a full list search. Either way a reference for 386ca22e56dSKay Sievers * the returned object is taken. 387ca22e56dSKay Sievers */ 388ca22e56dSKay Sievers struct device *subsys_find_device_by_id(struct bus_type *subsys, unsigned int id, 389ca22e56dSKay Sievers struct device *hint) 390ca22e56dSKay Sievers { 391ca22e56dSKay Sievers struct klist_iter i; 392ca22e56dSKay Sievers struct device *dev; 393ca22e56dSKay Sievers 394ca22e56dSKay Sievers if (!subsys) 395ca22e56dSKay Sievers return NULL; 396ca22e56dSKay Sievers 397ca22e56dSKay Sievers if (hint) { 3987cd9c9bbSGreg Kroah-Hartman klist_iter_init_node(&subsys->p->klist_devices, &i, &hint->p->knode_bus); 399ca22e56dSKay Sievers dev = next_device(&i); 400ca22e56dSKay Sievers if (dev && dev->id == id && get_device(dev)) { 401ca22e56dSKay Sievers klist_iter_exit(&i); 402ca22e56dSKay Sievers return dev; 403ca22e56dSKay Sievers } 404ca22e56dSKay Sievers klist_iter_exit(&i); 405ca22e56dSKay Sievers } 406ca22e56dSKay Sievers 407ca22e56dSKay Sievers klist_iter_init_node(&subsys->p->klist_devices, &i, NULL); 408ca22e56dSKay Sievers while ((dev = next_device(&i))) { 409ca22e56dSKay Sievers if (dev->id == id && get_device(dev)) { 410ca22e56dSKay Sievers klist_iter_exit(&i); 411ca22e56dSKay Sievers return dev; 412ca22e56dSKay Sievers } 413ca22e56dSKay Sievers } 414ca22e56dSKay Sievers klist_iter_exit(&i); 415ca22e56dSKay Sievers return NULL; 416ca22e56dSKay Sievers } 417ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_find_device_by_id); 418ca22e56dSKay Sievers 41938fdac3cSmochel@digitalimplant.org static struct device_driver *next_driver(struct klist_iter *i) 42038fdac3cSmochel@digitalimplant.org { 42138fdac3cSmochel@digitalimplant.org struct klist_node *n = klist_next(i); 422e5dd1278SGreg Kroah-Hartman struct driver_private *drv_priv; 423e5dd1278SGreg Kroah-Hartman 424e5dd1278SGreg Kroah-Hartman if (n) { 425e5dd1278SGreg Kroah-Hartman drv_priv = container_of(n, struct driver_private, knode_bus); 426e5dd1278SGreg Kroah-Hartman return drv_priv->driver; 427e5dd1278SGreg Kroah-Hartman } 428e5dd1278SGreg Kroah-Hartman return NULL; 42938fdac3cSmochel@digitalimplant.org } 43038fdac3cSmochel@digitalimplant.org 4311da177e4SLinus Torvalds /** 4321da177e4SLinus Torvalds * bus_for_each_drv - driver iterator 4331da177e4SLinus Torvalds * @bus: bus we're dealing with. 4341da177e4SLinus Torvalds * @start: driver to start iterating on. 4351da177e4SLinus Torvalds * @data: data to pass to the callback. 4361da177e4SLinus Torvalds * @fn: function to call for each driver. 4371da177e4SLinus Torvalds * 4381da177e4SLinus Torvalds * This is nearly identical to the device iterator above. 4391da177e4SLinus Torvalds * We iterate over each driver that belongs to @bus, and call 4401da177e4SLinus Torvalds * @fn for each. If @fn returns anything but 0, we break out 4411da177e4SLinus Torvalds * and return it. If @start is not NULL, we use it as the head 4421da177e4SLinus Torvalds * of the list. 4431da177e4SLinus Torvalds * 4441da177e4SLinus Torvalds * NOTE: we don't return the driver that returns a non-zero 4451da177e4SLinus Torvalds * value, nor do we leave the reference count incremented for that 4461da177e4SLinus Torvalds * driver. If the caller needs to know that info, it must set it 4471da177e4SLinus Torvalds * in the callback. It must also be sure to increment the refcount 4481da177e4SLinus Torvalds * so it doesn't disappear before returning to the caller. 4491da177e4SLinus Torvalds */ 4501da177e4SLinus Torvalds int bus_for_each_drv(struct bus_type *bus, struct device_driver *start, 4511da177e4SLinus Torvalds void *data, int (*fn)(struct device_driver *, void *)) 4521da177e4SLinus Torvalds { 45338fdac3cSmochel@digitalimplant.org struct klist_iter i; 45438fdac3cSmochel@digitalimplant.org struct device_driver *drv; 45538fdac3cSmochel@digitalimplant.org int error = 0; 4561da177e4SLinus Torvalds 45738fdac3cSmochel@digitalimplant.org if (!bus) 45838fdac3cSmochel@digitalimplant.org return -EINVAL; 45938fdac3cSmochel@digitalimplant.org 4607cd9c9bbSGreg Kroah-Hartman klist_iter_init_node(&bus->p->klist_drivers, &i, 461e5dd1278SGreg Kroah-Hartman start ? &start->p->knode_bus : NULL); 46238fdac3cSmochel@digitalimplant.org while ((drv = next_driver(&i)) && !error) 46338fdac3cSmochel@digitalimplant.org error = fn(drv, data); 46438fdac3cSmochel@digitalimplant.org klist_iter_exit(&i); 46538fdac3cSmochel@digitalimplant.org return error; 4661da177e4SLinus Torvalds } 4674a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_for_each_drv); 4681da177e4SLinus Torvalds 4691da177e4SLinus Torvalds /** 4701da177e4SLinus Torvalds * bus_add_device - add device to bus 4711da177e4SLinus Torvalds * @dev: device being added 4721da177e4SLinus Torvalds * 4732023c610SAlan Stern * - Add device's bus attributes. 4742023c610SAlan Stern * - Create links to device's bus. 4751da177e4SLinus Torvalds * - Add the device to its bus's list of devices. 4761da177e4SLinus Torvalds */ 4771da177e4SLinus Torvalds int bus_add_device(struct device *dev) 4781da177e4SLinus Torvalds { 4795901d014SGreg Kroah-Hartman struct bus_type *bus = bus_get(dev->bus); 4801da177e4SLinus Torvalds int error = 0; 4811da177e4SLinus Torvalds 4821da177e4SLinus Torvalds if (bus) { 4831e0b2cf9SKay Sievers pr_debug("bus: '%s': add device %s\n", bus->name, dev_name(dev)); 484fa6fdb33SGreg Kroah-Hartman error = device_add_groups(dev, bus->dev_groups); 485fa6fdb33SGreg Kroah-Hartman if (error) 486*b46c7337SGreg Kroah-Hartman goto out_put; 487c6f7e72aSGreg Kroah-Hartman error = sysfs_create_link(&bus->p->devices_kset->kobj, 4881e0b2cf9SKay Sievers &dev->kobj, dev_name(dev)); 489f86db396SAndrew Morton if (error) 4901c34203aSJunjie Mao goto out_groups; 491f86db396SAndrew Morton error = sysfs_create_link(&dev->kobj, 492c6f7e72aSGreg Kroah-Hartman &dev->bus->p->subsys.kobj, "subsystem"); 493f86db396SAndrew Morton if (error) 494513e7337SCornelia Huck goto out_subsys; 4952023c610SAlan Stern klist_add_tail(&dev->p->knode_bus, &bus->p->klist_devices); 4961da177e4SLinus Torvalds } 497513e7337SCornelia Huck return 0; 498513e7337SCornelia Huck 499513e7337SCornelia Huck out_subsys: 5001e0b2cf9SKay Sievers sysfs_remove_link(&bus->p->devices_kset->kobj, dev_name(dev)); 501fa6fdb33SGreg Kroah-Hartman out_groups: 502fa6fdb33SGreg Kroah-Hartman device_remove_groups(dev, bus->dev_groups); 503513e7337SCornelia Huck out_put: 504fc1ede58SGreg Kroah-Hartman bus_put(dev->bus); 5051da177e4SLinus Torvalds return error; 5061da177e4SLinus Torvalds } 5071da177e4SLinus Torvalds 5081da177e4SLinus Torvalds /** 5092023c610SAlan Stern * bus_probe_device - probe drivers for a new device 5102023c610SAlan Stern * @dev: device to probe 51153877d06SKay Sievers * 5122023c610SAlan Stern * - Automatically probe for a driver if the bus allows it. 51353877d06SKay Sievers */ 5142023c610SAlan Stern void bus_probe_device(struct device *dev) 51553877d06SKay Sievers { 51653877d06SKay Sievers struct bus_type *bus = dev->bus; 517ca22e56dSKay Sievers struct subsys_interface *sif; 51853877d06SKay Sievers 519ca22e56dSKay Sievers if (!bus) 520ca22e56dSKay Sievers return; 521ca22e56dSKay Sievers 522765230b5SDmitry Torokhov if (bus->p->drivers_autoprobe) 523765230b5SDmitry Torokhov device_initial_probe(dev); 524ca22e56dSKay Sievers 525ca22e56dSKay Sievers mutex_lock(&bus->p->mutex); 526ca22e56dSKay Sievers list_for_each_entry(sif, &bus->p->interfaces, node) 527ca22e56dSKay Sievers if (sif->add_dev) 528ca22e56dSKay Sievers sif->add_dev(dev, sif); 529ca22e56dSKay Sievers mutex_unlock(&bus->p->mutex); 530f86db396SAndrew Morton } 53153877d06SKay Sievers 53253877d06SKay Sievers /** 5331da177e4SLinus Torvalds * bus_remove_device - remove device from bus 5341da177e4SLinus Torvalds * @dev: device to be removed 5351da177e4SLinus Torvalds * 536ca22e56dSKay Sievers * - Remove device from all interfaces. 537ca22e56dSKay Sievers * - Remove symlink from bus' directory. 5381da177e4SLinus Torvalds * - Delete device from bus's list. 5391da177e4SLinus Torvalds * - Detach from its driver. 5401da177e4SLinus Torvalds * - Drop reference taken in bus_add_device(). 5411da177e4SLinus Torvalds */ 5421da177e4SLinus Torvalds void bus_remove_device(struct device *dev) 5431da177e4SLinus Torvalds { 544ca22e56dSKay Sievers struct bus_type *bus = dev->bus; 545ca22e56dSKay Sievers struct subsys_interface *sif; 546ca22e56dSKay Sievers 547ca22e56dSKay Sievers if (!bus) 548ca22e56dSKay Sievers return; 549ca22e56dSKay Sievers 550ca22e56dSKay Sievers mutex_lock(&bus->p->mutex); 551ca22e56dSKay Sievers list_for_each_entry(sif, &bus->p->interfaces, node) 552ca22e56dSKay Sievers if (sif->remove_dev) 553ca22e56dSKay Sievers sif->remove_dev(dev, sif); 554ca22e56dSKay Sievers mutex_unlock(&bus->p->mutex); 555ca22e56dSKay Sievers 556b9d9c82bSKay Sievers sysfs_remove_link(&dev->kobj, "subsystem"); 5574a3ad20cSGreg Kroah-Hartman sysfs_remove_link(&dev->bus->p->devices_kset->kobj, 5581e0b2cf9SKay Sievers dev_name(dev)); 559fa6fdb33SGreg Kroah-Hartman device_remove_groups(dev, dev->bus->dev_groups); 560ae1b4171SGreg Kroah-Hartman if (klist_node_attached(&dev->p->knode_bus)) 561ae1b4171SGreg Kroah-Hartman klist_del(&dev->p->knode_bus); 5623f62e570SGreg Kroah-Hartman 5634a3ad20cSGreg Kroah-Hartman pr_debug("bus: '%s': remove device %s\n", 5641e0b2cf9SKay Sievers dev->bus->name, dev_name(dev)); 5651da177e4SLinus Torvalds device_release_driver(dev); 566fc1ede58SGreg Kroah-Hartman bus_put(dev->bus); 5671da177e4SLinus Torvalds } 5681da177e4SLinus Torvalds 569f86db396SAndrew Morton static int __must_check add_bind_files(struct device_driver *drv) 570874c6241SGreg Kroah-Hartman { 571f86db396SAndrew Morton int ret; 572f86db396SAndrew Morton 573f86db396SAndrew Morton ret = driver_create_file(drv, &driver_attr_unbind); 574f86db396SAndrew Morton if (ret == 0) { 575f86db396SAndrew Morton ret = driver_create_file(drv, &driver_attr_bind); 576f86db396SAndrew Morton if (ret) 577f86db396SAndrew Morton driver_remove_file(drv, &driver_attr_unbind); 578f86db396SAndrew Morton } 579f86db396SAndrew Morton return ret; 580874c6241SGreg Kroah-Hartman } 581874c6241SGreg Kroah-Hartman 582874c6241SGreg Kroah-Hartman static void remove_bind_files(struct device_driver *drv) 583874c6241SGreg Kroah-Hartman { 584874c6241SGreg Kroah-Hartman driver_remove_file(drv, &driver_attr_bind); 585874c6241SGreg Kroah-Hartman driver_remove_file(drv, &driver_attr_unbind); 586874c6241SGreg Kroah-Hartman } 587b8c5cec2SKay Sievers 5888380770cSKay Sievers static BUS_ATTR(drivers_probe, S_IWUSR, NULL, store_drivers_probe); 5898380770cSKay Sievers static BUS_ATTR(drivers_autoprobe, S_IWUSR | S_IRUGO, 5908380770cSKay Sievers show_drivers_autoprobe, store_drivers_autoprobe); 5918380770cSKay Sievers 592b8c5cec2SKay Sievers static int add_probe_files(struct bus_type *bus) 593b8c5cec2SKay Sievers { 594b8c5cec2SKay Sievers int retval; 595b8c5cec2SKay Sievers 5968380770cSKay Sievers retval = bus_create_file(bus, &bus_attr_drivers_probe); 597b8c5cec2SKay Sievers if (retval) 598b8c5cec2SKay Sievers goto out; 599b8c5cec2SKay Sievers 6008380770cSKay Sievers retval = bus_create_file(bus, &bus_attr_drivers_autoprobe); 601b8c5cec2SKay Sievers if (retval) 6028380770cSKay Sievers bus_remove_file(bus, &bus_attr_drivers_probe); 603b8c5cec2SKay Sievers out: 604b8c5cec2SKay Sievers return retval; 605b8c5cec2SKay Sievers } 606b8c5cec2SKay Sievers 607b8c5cec2SKay Sievers static void remove_probe_files(struct bus_type *bus) 608b8c5cec2SKay Sievers { 6098380770cSKay Sievers bus_remove_file(bus, &bus_attr_drivers_autoprobe); 6108380770cSKay Sievers bus_remove_file(bus, &bus_attr_drivers_probe); 611b8c5cec2SKay Sievers } 6121da177e4SLinus Torvalds 6132581c9ccSGreg Kroah-Hartman static ssize_t uevent_store(struct device_driver *drv, const char *buf, 6142581c9ccSGreg Kroah-Hartman size_t count) 6157ac1cf4aSKay Sievers { 616f36776faSPeter Rajnoha kobject_synth_uevent(&drv->p->kobj, buf, count); 6177ac1cf4aSKay Sievers return count; 6187ac1cf4aSKay Sievers } 6192581c9ccSGreg Kroah-Hartman static DRIVER_ATTR_WO(uevent); 6207ac1cf4aSKay Sievers 621765230b5SDmitry Torokhov static void driver_attach_async(void *_drv, async_cookie_t cookie) 622765230b5SDmitry Torokhov { 623765230b5SDmitry Torokhov struct device_driver *drv = _drv; 624765230b5SDmitry Torokhov int ret; 625765230b5SDmitry Torokhov 626765230b5SDmitry Torokhov ret = driver_attach(drv); 627765230b5SDmitry Torokhov 628765230b5SDmitry Torokhov pr_debug("bus: '%s': driver %s async attach completed: %d\n", 629765230b5SDmitry Torokhov drv->bus->name, drv->name, ret); 630765230b5SDmitry Torokhov } 631765230b5SDmitry Torokhov 6321da177e4SLinus Torvalds /** 6331da177e4SLinus Torvalds * bus_add_driver - Add a driver to the bus. 6341da177e4SLinus Torvalds * @drv: driver. 6351da177e4SLinus Torvalds */ 6361da177e4SLinus Torvalds int bus_add_driver(struct device_driver *drv) 6371da177e4SLinus Torvalds { 638e5dd1278SGreg Kroah-Hartman struct bus_type *bus; 639e5dd1278SGreg Kroah-Hartman struct driver_private *priv; 6401da177e4SLinus Torvalds int error = 0; 6411da177e4SLinus Torvalds 642e5dd1278SGreg Kroah-Hartman bus = bus_get(drv->bus); 643d9fd4d3bSJeff Garzik if (!bus) 6444f6e1945SGreg Kroah-Hartman return -EINVAL; 645d9fd4d3bSJeff Garzik 6467dc72b28SGreg Kroah-Hartman pr_debug("bus: '%s': add driver %s\n", bus->name, drv->name); 647e5dd1278SGreg Kroah-Hartman 648e5dd1278SGreg Kroah-Hartman priv = kzalloc(sizeof(*priv), GFP_KERNEL); 64907634464SCornelia Huck if (!priv) { 65007634464SCornelia Huck error = -ENOMEM; 65107634464SCornelia Huck goto out_put_bus; 65207634464SCornelia Huck } 653e5dd1278SGreg Kroah-Hartman klist_init(&priv->klist_devices, NULL, NULL); 654e5dd1278SGreg Kroah-Hartman priv->driver = drv; 655e5dd1278SGreg Kroah-Hartman drv->p = priv; 656c8e90d82SGreg Kroah-Hartman priv->kobj.kset = bus->p->drivers_kset; 657c8e90d82SGreg Kroah-Hartman error = kobject_init_and_add(&priv->kobj, &driver_ktype, NULL, 658c8e90d82SGreg Kroah-Hartman "%s", drv->name); 659dc0afa83SCornelia Huck if (error) 66007634464SCornelia Huck goto out_unregister; 6611da177e4SLinus Torvalds 662190888acSMing Lei klist_add_tail(&priv->knode_bus, &bus->p->klist_drivers); 663c6f7e72aSGreg Kroah-Hartman if (drv->bus->p->drivers_autoprobe) { 664765230b5SDmitry Torokhov if (driver_allows_async_probing(drv)) { 665765230b5SDmitry Torokhov pr_debug("bus: '%s': probing driver %s asynchronously\n", 666765230b5SDmitry Torokhov drv->bus->name, drv->name); 667765230b5SDmitry Torokhov async_schedule(driver_attach_async, drv); 668765230b5SDmitry Torokhov } else { 669f86db396SAndrew Morton error = driver_attach(drv); 670f86db396SAndrew Morton if (error) 671f86db396SAndrew Morton goto out_unregister; 672b8c5cec2SKay Sievers } 673765230b5SDmitry Torokhov } 6741da177e4SLinus Torvalds module_add_driver(drv->owner, drv); 6751da177e4SLinus Torvalds 6767ac1cf4aSKay Sievers error = driver_create_file(drv, &driver_attr_uevent); 6777ac1cf4aSKay Sievers if (error) { 6787ac1cf4aSKay Sievers printk(KERN_ERR "%s: uevent attr (%s) failed\n", 6792b3a302aSHarvey Harrison __func__, drv->name); 6807ac1cf4aSKay Sievers } 681e18945b1SGreg Kroah-Hartman error = driver_add_groups(drv, bus->drv_groups); 682f86db396SAndrew Morton if (error) { 683f86db396SAndrew Morton /* How the hell do we get out of this pickle? Give up */ 684ed0617b5SGreg Kroah-Hartman printk(KERN_ERR "%s: driver_create_groups(%s) failed\n", 685ed0617b5SGreg Kroah-Hartman __func__, drv->name); 686e18945b1SGreg Kroah-Hartman } 6871a6f2a75SDmitry Torokhov 6881a6f2a75SDmitry Torokhov if (!drv->suppress_bind_attrs) { 689f86db396SAndrew Morton error = add_bind_files(drv); 690f86db396SAndrew Morton if (error) { 691f86db396SAndrew Morton /* Ditto */ 692f86db396SAndrew Morton printk(KERN_ERR "%s: add_bind_files(%s) failed\n", 6932b3a302aSHarvey Harrison __func__, drv->name); 694f86db396SAndrew Morton } 6951a6f2a75SDmitry Torokhov } 696d9fd4d3bSJeff Garzik 6975c8563d7SKay Sievers return 0; 6981a6f2a75SDmitry Torokhov 699f86db396SAndrew Morton out_unregister: 70099b28f1bSPhil Carmody kobject_put(&priv->kobj); 7015c8563d7SKay Sievers kfree(drv->p); 7025c8563d7SKay Sievers drv->p = NULL; 703f86db396SAndrew Morton out_put_bus: 704fc1ede58SGreg Kroah-Hartman bus_put(bus); 705f86db396SAndrew Morton return error; 7061da177e4SLinus Torvalds } 7071da177e4SLinus Torvalds 7081da177e4SLinus Torvalds /** 7091da177e4SLinus Torvalds * bus_remove_driver - delete driver from bus's knowledge. 7101da177e4SLinus Torvalds * @drv: driver. 7111da177e4SLinus Torvalds * 7121da177e4SLinus Torvalds * Detach the driver from the devices it controls, and remove 7131da177e4SLinus Torvalds * it from its bus's list of drivers. Finally, we drop the reference 7141da177e4SLinus Torvalds * to the bus we took in bus_add_driver(). 7151da177e4SLinus Torvalds */ 7161da177e4SLinus Torvalds void bus_remove_driver(struct device_driver *drv) 7171da177e4SLinus Torvalds { 718d9fd4d3bSJeff Garzik if (!drv->bus) 719d9fd4d3bSJeff Garzik return; 720d9fd4d3bSJeff Garzik 7211a6f2a75SDmitry Torokhov if (!drv->suppress_bind_attrs) 722874c6241SGreg Kroah-Hartman remove_bind_files(drv); 723ed0617b5SGreg Kroah-Hartman driver_remove_groups(drv, drv->bus->drv_groups); 7247ac1cf4aSKay Sievers driver_remove_file(drv, &driver_attr_uevent); 725e5dd1278SGreg Kroah-Hartman klist_remove(&drv->p->knode_bus); 7267dc72b28SGreg Kroah-Hartman pr_debug("bus: '%s': remove driver %s\n", drv->bus->name, drv->name); 7271da177e4SLinus Torvalds driver_detach(drv); 7281da177e4SLinus Torvalds module_remove_driver(drv); 729c10997f6SGreg Kroah-Hartman kobject_put(&drv->p->kobj); 730fc1ede58SGreg Kroah-Hartman bus_put(drv->bus); 7311da177e4SLinus Torvalds } 7321da177e4SLinus Torvalds 7331da177e4SLinus Torvalds /* Helper for bus_rescan_devices's iter */ 734f86db396SAndrew Morton static int __must_check bus_rescan_devices_helper(struct device *dev, 735f86db396SAndrew Morton void *data) 7361da177e4SLinus Torvalds { 737f86db396SAndrew Morton int ret = 0; 738f86db396SAndrew Morton 739bf74ad5bSAlan Stern if (!dev->driver) { 740bf74ad5bSAlan Stern if (dev->parent) /* Needed for USB */ 7418e9394ceSGreg Kroah-Hartman device_lock(dev->parent); 742f86db396SAndrew Morton ret = device_attach(dev); 743bf74ad5bSAlan Stern if (dev->parent) 7448e9394ceSGreg Kroah-Hartman device_unlock(dev->parent); 745bf74ad5bSAlan Stern } 746f86db396SAndrew Morton return ret < 0 ? ret : 0; 7471da177e4SLinus Torvalds } 7481da177e4SLinus Torvalds 7491da177e4SLinus Torvalds /** 7501da177e4SLinus Torvalds * bus_rescan_devices - rescan devices on the bus for possible drivers 7511da177e4SLinus Torvalds * @bus: the bus to scan. 7521da177e4SLinus Torvalds * 7531da177e4SLinus Torvalds * This function will look for devices on the bus with no driver 75423d3d602SGreg Kroah-Hartman * attached and rescan it against existing drivers to see if it matches 75523d3d602SGreg Kroah-Hartman * any by calling device_attach() for the unbound devices. 7561da177e4SLinus Torvalds */ 757f86db396SAndrew Morton int bus_rescan_devices(struct bus_type *bus) 7581da177e4SLinus Torvalds { 759f86db396SAndrew Morton return bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper); 7601da177e4SLinus Torvalds } 7614a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_rescan_devices); 7621da177e4SLinus Torvalds 763e935d5daSMoore, Eric /** 764e935d5daSMoore, Eric * device_reprobe - remove driver for a device and probe for a new driver 765e935d5daSMoore, Eric * @dev: the device to reprobe 766e935d5daSMoore, Eric * 767e935d5daSMoore, Eric * This function detaches the attached driver (if any) for the given 768e935d5daSMoore, Eric * device and restarts the driver probing process. It is intended 769e935d5daSMoore, Eric * to use if probing criteria changed during a devices lifetime and 770e935d5daSMoore, Eric * driver attachment should change accordingly. 771e935d5daSMoore, Eric */ 772f86db396SAndrew Morton int device_reprobe(struct device *dev) 773e935d5daSMoore, Eric { 774e935d5daSMoore, Eric if (dev->driver) { 775e935d5daSMoore, Eric if (dev->parent) /* Needed for USB */ 7768e9394ceSGreg Kroah-Hartman device_lock(dev->parent); 777e935d5daSMoore, Eric device_release_driver(dev); 778e935d5daSMoore, Eric if (dev->parent) 7798e9394ceSGreg Kroah-Hartman device_unlock(dev->parent); 780e935d5daSMoore, Eric } 781f86db396SAndrew Morton return bus_rescan_devices_helper(dev, NULL); 782e935d5daSMoore, Eric } 783e935d5daSMoore, Eric EXPORT_SYMBOL_GPL(device_reprobe); 7841da177e4SLinus Torvalds 7851da177e4SLinus Torvalds /** 7861da177e4SLinus Torvalds * find_bus - locate bus by name. 7871da177e4SLinus Torvalds * @name: name of bus. 7881da177e4SLinus Torvalds * 7891da177e4SLinus Torvalds * Call kset_find_obj() to iterate over list of buses to 7901da177e4SLinus Torvalds * find a bus by name. Return bus if found. 7911da177e4SLinus Torvalds * 7921da177e4SLinus Torvalds * Note that kset_find_obj increments bus' reference count. 7931da177e4SLinus Torvalds */ 7947e4ef085SAdrian Bunk #if 0 7951da177e4SLinus Torvalds struct bus_type *find_bus(char *name) 7961da177e4SLinus Torvalds { 79759a54833SGreg Kroah-Hartman struct kobject *k = kset_find_obj(bus_kset, name); 7981da177e4SLinus Torvalds return k ? to_bus(k) : NULL; 7991da177e4SLinus Torvalds } 8007e4ef085SAdrian Bunk #endif /* 0 */ 8011da177e4SLinus Torvalds 80212478ba0SGreg Kroah-Hartman static int bus_add_groups(struct bus_type *bus, 80312478ba0SGreg Kroah-Hartman const struct attribute_group **groups) 80412478ba0SGreg Kroah-Hartman { 8053e9b2baeSGreg Kroah-Hartman return sysfs_create_groups(&bus->p->subsys.kobj, groups); 80612478ba0SGreg Kroah-Hartman } 80712478ba0SGreg Kroah-Hartman 80812478ba0SGreg Kroah-Hartman static void bus_remove_groups(struct bus_type *bus, 80912478ba0SGreg Kroah-Hartman const struct attribute_group **groups) 81012478ba0SGreg Kroah-Hartman { 8113e9b2baeSGreg Kroah-Hartman sysfs_remove_groups(&bus->p->subsys.kobj, groups); 81212478ba0SGreg Kroah-Hartman } 81312478ba0SGreg Kroah-Hartman 81434bb61f9SJames Bottomley static void klist_devices_get(struct klist_node *n) 81534bb61f9SJames Bottomley { 816ae1b4171SGreg Kroah-Hartman struct device_private *dev_prv = to_device_private_bus(n); 817ae1b4171SGreg Kroah-Hartman struct device *dev = dev_prv->device; 81834bb61f9SJames Bottomley 81934bb61f9SJames Bottomley get_device(dev); 82034bb61f9SJames Bottomley } 82134bb61f9SJames Bottomley 82234bb61f9SJames Bottomley static void klist_devices_put(struct klist_node *n) 82334bb61f9SJames Bottomley { 824ae1b4171SGreg Kroah-Hartman struct device_private *dev_prv = to_device_private_bus(n); 825ae1b4171SGreg Kroah-Hartman struct device *dev = dev_prv->device; 82634bb61f9SJames Bottomley 82734bb61f9SJames Bottomley put_device(dev); 82834bb61f9SJames Bottomley } 82934bb61f9SJames Bottomley 8307ac1cf4aSKay Sievers static ssize_t bus_uevent_store(struct bus_type *bus, 8317ac1cf4aSKay Sievers const char *buf, size_t count) 8327ac1cf4aSKay Sievers { 833f36776faSPeter Rajnoha kobject_synth_uevent(&bus->p->subsys.kobj, buf, count); 8347ac1cf4aSKay Sievers return count; 8357ac1cf4aSKay Sievers } 8367ac1cf4aSKay Sievers static BUS_ATTR(uevent, S_IWUSR, NULL, bus_uevent_store); 8377ac1cf4aSKay Sievers 8381da177e4SLinus Torvalds /** 839be871b7eSMichal Hocko * bus_register - register a driver-core subsystem 84078d79559SRandy Dunlap * @bus: bus to register 8411da177e4SLinus Torvalds * 84278d79559SRandy Dunlap * Once we have that, we register the bus with the kobject 8431da177e4SLinus Torvalds * infrastructure, then register the children subsystems it has: 844ca22e56dSKay Sievers * the devices and drivers that belong to the subsystem. 8451da177e4SLinus Torvalds */ 846be871b7eSMichal Hocko int bus_register(struct bus_type *bus) 8471da177e4SLinus Torvalds { 8481da177e4SLinus Torvalds int retval; 8496b6e39a6SKay Sievers struct subsys_private *priv; 850be871b7eSMichal Hocko struct lock_class_key *key = &bus->lock_key; 8511da177e4SLinus Torvalds 8526b6e39a6SKay Sievers priv = kzalloc(sizeof(struct subsys_private), GFP_KERNEL); 853c6f7e72aSGreg Kroah-Hartman if (!priv) 854c6f7e72aSGreg Kroah-Hartman return -ENOMEM; 855116af378SBenjamin Herrenschmidt 856c6f7e72aSGreg Kroah-Hartman priv->bus = bus; 857c6f7e72aSGreg Kroah-Hartman bus->p = priv; 858c6f7e72aSGreg Kroah-Hartman 859c6f7e72aSGreg Kroah-Hartman BLOCKING_INIT_NOTIFIER_HEAD(&priv->bus_notifier); 860c6f7e72aSGreg Kroah-Hartman 861c6f7e72aSGreg Kroah-Hartman retval = kobject_set_name(&priv->subsys.kobj, "%s", bus->name); 8621da177e4SLinus Torvalds if (retval) 8631da177e4SLinus Torvalds goto out; 8641da177e4SLinus Torvalds 865c6f7e72aSGreg Kroah-Hartman priv->subsys.kobj.kset = bus_kset; 866c6f7e72aSGreg Kroah-Hartman priv->subsys.kobj.ktype = &bus_ktype; 867c6f7e72aSGreg Kroah-Hartman priv->drivers_autoprobe = 1; 868d6b05b84SGreg Kroah-Hartman 869c6f7e72aSGreg Kroah-Hartman retval = kset_register(&priv->subsys); 8701da177e4SLinus Torvalds if (retval) 8711da177e4SLinus Torvalds goto out; 8721da177e4SLinus Torvalds 8737ac1cf4aSKay Sievers retval = bus_create_file(bus, &bus_attr_uevent); 8747ac1cf4aSKay Sievers if (retval) 8757ac1cf4aSKay Sievers goto bus_uevent_fail; 8767ac1cf4aSKay Sievers 877c6f7e72aSGreg Kroah-Hartman priv->devices_kset = kset_create_and_add("devices", NULL, 878c6f7e72aSGreg Kroah-Hartman &priv->subsys.kobj); 879c6f7e72aSGreg Kroah-Hartman if (!priv->devices_kset) { 8803d899596SGreg Kroah-Hartman retval = -ENOMEM; 8811da177e4SLinus Torvalds goto bus_devices_fail; 8823d899596SGreg Kroah-Hartman } 8831da177e4SLinus Torvalds 884c6f7e72aSGreg Kroah-Hartman priv->drivers_kset = kset_create_and_add("drivers", NULL, 885c6f7e72aSGreg Kroah-Hartman &priv->subsys.kobj); 886c6f7e72aSGreg Kroah-Hartman if (!priv->drivers_kset) { 8876dcec251SGreg Kroah-Hartman retval = -ENOMEM; 8881da177e4SLinus Torvalds goto bus_drivers_fail; 8896dcec251SGreg Kroah-Hartman } 890465c7a3aSmochel@digitalimplant.org 891ca22e56dSKay Sievers INIT_LIST_HEAD(&priv->interfaces); 892ca22e56dSKay Sievers __mutex_init(&priv->mutex, "subsys mutex", key); 893c6f7e72aSGreg Kroah-Hartman klist_init(&priv->klist_devices, klist_devices_get, klist_devices_put); 894c6f7e72aSGreg Kroah-Hartman klist_init(&priv->klist_drivers, NULL, NULL); 895b8c5cec2SKay Sievers 896b8c5cec2SKay Sievers retval = add_probe_files(bus); 897b8c5cec2SKay Sievers if (retval) 898b8c5cec2SKay Sievers goto bus_probe_files_fail; 899b8c5cec2SKay Sievers 90012478ba0SGreg Kroah-Hartman retval = bus_add_groups(bus, bus->bus_groups); 90112478ba0SGreg Kroah-Hartman if (retval) 90212478ba0SGreg Kroah-Hartman goto bus_groups_fail; 9031da177e4SLinus Torvalds 9047dc72b28SGreg Kroah-Hartman pr_debug("bus: '%s': registered\n", bus->name); 9051da177e4SLinus Torvalds return 0; 9061da177e4SLinus Torvalds 90712478ba0SGreg Kroah-Hartman bus_groups_fail: 908b8c5cec2SKay Sievers remove_probe_files(bus); 909b8c5cec2SKay Sievers bus_probe_files_fail: 910c6f7e72aSGreg Kroah-Hartman kset_unregister(bus->p->drivers_kset); 9111da177e4SLinus Torvalds bus_drivers_fail: 912c6f7e72aSGreg Kroah-Hartman kset_unregister(bus->p->devices_kset); 9131da177e4SLinus Torvalds bus_devices_fail: 9147ac1cf4aSKay Sievers bus_remove_file(bus, &bus_attr_uevent); 9157ac1cf4aSKay Sievers bus_uevent_fail: 916c6f7e72aSGreg Kroah-Hartman kset_unregister(&bus->p->subsys); 9171da177e4SLinus Torvalds out: 918600c20f3SJike Song kfree(bus->p); 919f48f3febSDave Young bus->p = NULL; 9201da177e4SLinus Torvalds return retval; 9211da177e4SLinus Torvalds } 922be871b7eSMichal Hocko EXPORT_SYMBOL_GPL(bus_register); 9231da177e4SLinus Torvalds 9241da177e4SLinus Torvalds /** 9251da177e4SLinus Torvalds * bus_unregister - remove a bus from the system 9261da177e4SLinus Torvalds * @bus: bus. 9271da177e4SLinus Torvalds * 9281da177e4SLinus Torvalds * Unregister the child subsystems and the bus itself. 929fc1ede58SGreg Kroah-Hartman * Finally, we call bus_put() to release the refcount 9301da177e4SLinus Torvalds */ 9311da177e4SLinus Torvalds void bus_unregister(struct bus_type *bus) 9321da177e4SLinus Torvalds { 9337dc72b28SGreg Kroah-Hartman pr_debug("bus: '%s': unregistering\n", bus->name); 934ca22e56dSKay Sievers if (bus->dev_root) 935ca22e56dSKay Sievers device_unregister(bus->dev_root); 93612478ba0SGreg Kroah-Hartman bus_remove_groups(bus, bus->bus_groups); 937b8c5cec2SKay Sievers remove_probe_files(bus); 938c6f7e72aSGreg Kroah-Hartman kset_unregister(bus->p->drivers_kset); 939c6f7e72aSGreg Kroah-Hartman kset_unregister(bus->p->devices_kset); 9407ac1cf4aSKay Sievers bus_remove_file(bus, &bus_attr_uevent); 941c6f7e72aSGreg Kroah-Hartman kset_unregister(&bus->p->subsys); 9421da177e4SLinus Torvalds } 9434a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_unregister); 9441da177e4SLinus Torvalds 945116af378SBenjamin Herrenschmidt int bus_register_notifier(struct bus_type *bus, struct notifier_block *nb) 946116af378SBenjamin Herrenschmidt { 947c6f7e72aSGreg Kroah-Hartman return blocking_notifier_chain_register(&bus->p->bus_notifier, nb); 948116af378SBenjamin Herrenschmidt } 949116af378SBenjamin Herrenschmidt EXPORT_SYMBOL_GPL(bus_register_notifier); 950116af378SBenjamin Herrenschmidt 951116af378SBenjamin Herrenschmidt int bus_unregister_notifier(struct bus_type *bus, struct notifier_block *nb) 952116af378SBenjamin Herrenschmidt { 953c6f7e72aSGreg Kroah-Hartman return blocking_notifier_chain_unregister(&bus->p->bus_notifier, nb); 954116af378SBenjamin Herrenschmidt } 955116af378SBenjamin Herrenschmidt EXPORT_SYMBOL_GPL(bus_unregister_notifier); 956116af378SBenjamin Herrenschmidt 9570fed80f7SGreg Kroah-Hartman struct kset *bus_get_kset(struct bus_type *bus) 9580fed80f7SGreg Kroah-Hartman { 959c6f7e72aSGreg Kroah-Hartman return &bus->p->subsys; 9600fed80f7SGreg Kroah-Hartman } 9610fed80f7SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_get_kset); 9620fed80f7SGreg Kroah-Hartman 963b249072eSGreg Kroah-Hartman struct klist *bus_get_device_klist(struct bus_type *bus) 964b249072eSGreg Kroah-Hartman { 965c6f7e72aSGreg Kroah-Hartman return &bus->p->klist_devices; 966b249072eSGreg Kroah-Hartman } 967b249072eSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_get_device_klist); 968b249072eSGreg Kroah-Hartman 96999178b03SGreg Kroah-Hartman /* 970dca25ebdSRobert P. J. Day * Yes, this forcibly breaks the klist abstraction temporarily. It 97199178b03SGreg Kroah-Hartman * just wants to sort the klist, not change reference counts and 97299178b03SGreg Kroah-Hartman * take/drop locks rapidly in the process. It does all this while 97399178b03SGreg Kroah-Hartman * holding the lock for the list, so objects can't otherwise be 97499178b03SGreg Kroah-Hartman * added/removed while we're swizzling. 97599178b03SGreg Kroah-Hartman */ 97699178b03SGreg Kroah-Hartman static void device_insertion_sort_klist(struct device *a, struct list_head *list, 97799178b03SGreg Kroah-Hartman int (*compare)(const struct device *a, 97899178b03SGreg Kroah-Hartman const struct device *b)) 97999178b03SGreg Kroah-Hartman { 98099178b03SGreg Kroah-Hartman struct klist_node *n; 981ae1b4171SGreg Kroah-Hartman struct device_private *dev_prv; 98299178b03SGreg Kroah-Hartman struct device *b; 98399178b03SGreg Kroah-Hartman 9844c62785eSGeliang Tang list_for_each_entry(n, list, n_node) { 985ae1b4171SGreg Kroah-Hartman dev_prv = to_device_private_bus(n); 986ae1b4171SGreg Kroah-Hartman b = dev_prv->device; 98799178b03SGreg Kroah-Hartman if (compare(a, b) <= 0) { 988ae1b4171SGreg Kroah-Hartman list_move_tail(&a->p->knode_bus.n_node, 989ae1b4171SGreg Kroah-Hartman &b->p->knode_bus.n_node); 99099178b03SGreg Kroah-Hartman return; 99199178b03SGreg Kroah-Hartman } 99299178b03SGreg Kroah-Hartman } 993ae1b4171SGreg Kroah-Hartman list_move_tail(&a->p->knode_bus.n_node, list); 99499178b03SGreg Kroah-Hartman } 99599178b03SGreg Kroah-Hartman 99699178b03SGreg Kroah-Hartman void bus_sort_breadthfirst(struct bus_type *bus, 99799178b03SGreg Kroah-Hartman int (*compare)(const struct device *a, 99899178b03SGreg Kroah-Hartman const struct device *b)) 99999178b03SGreg Kroah-Hartman { 100099178b03SGreg Kroah-Hartman LIST_HEAD(sorted_devices); 10014c62785eSGeliang Tang struct klist_node *n, *tmp; 1002ae1b4171SGreg Kroah-Hartman struct device_private *dev_prv; 100399178b03SGreg Kroah-Hartman struct device *dev; 100499178b03SGreg Kroah-Hartman struct klist *device_klist; 100599178b03SGreg Kroah-Hartman 100699178b03SGreg Kroah-Hartman device_klist = bus_get_device_klist(bus); 100799178b03SGreg Kroah-Hartman 100899178b03SGreg Kroah-Hartman spin_lock(&device_klist->k_lock); 10094c62785eSGeliang Tang list_for_each_entry_safe(n, tmp, &device_klist->k_list, n_node) { 1010ae1b4171SGreg Kroah-Hartman dev_prv = to_device_private_bus(n); 1011ae1b4171SGreg Kroah-Hartman dev = dev_prv->device; 101299178b03SGreg Kroah-Hartman device_insertion_sort_klist(dev, &sorted_devices, compare); 101399178b03SGreg Kroah-Hartman } 101499178b03SGreg Kroah-Hartman list_splice(&sorted_devices, &device_klist->k_list); 101599178b03SGreg Kroah-Hartman spin_unlock(&device_klist->k_lock); 101699178b03SGreg Kroah-Hartman } 101799178b03SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_sort_breadthfirst); 101899178b03SGreg Kroah-Hartman 1019ca22e56dSKay Sievers /** 1020ca22e56dSKay Sievers * subsys_dev_iter_init - initialize subsys device iterator 1021ca22e56dSKay Sievers * @iter: subsys iterator to initialize 1022ca22e56dSKay Sievers * @subsys: the subsys we wanna iterate over 1023ca22e56dSKay Sievers * @start: the device to start iterating from, if any 1024ca22e56dSKay Sievers * @type: device_type of the devices to iterate over, NULL for all 1025ca22e56dSKay Sievers * 1026ca22e56dSKay Sievers * Initialize subsys iterator @iter such that it iterates over devices 1027ca22e56dSKay Sievers * of @subsys. If @start is set, the list iteration will start there, 1028ca22e56dSKay Sievers * otherwise if it is NULL, the iteration starts at the beginning of 1029ca22e56dSKay Sievers * the list. 1030ca22e56dSKay Sievers */ 10317cd9c9bbSGreg Kroah-Hartman void subsys_dev_iter_init(struct subsys_dev_iter *iter, struct bus_type *subsys, 1032ca22e56dSKay Sievers struct device *start, const struct device_type *type) 1033ca22e56dSKay Sievers { 1034ca22e56dSKay Sievers struct klist_node *start_knode = NULL; 1035ca22e56dSKay Sievers 1036ca22e56dSKay Sievers if (start) 1037ca22e56dSKay Sievers start_knode = &start->p->knode_bus; 10387cd9c9bbSGreg Kroah-Hartman klist_iter_init_node(&subsys->p->klist_devices, &iter->ki, start_knode); 1039ca22e56dSKay Sievers iter->type = type; 1040ca22e56dSKay Sievers } 1041ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_dev_iter_init); 1042ca22e56dSKay Sievers 1043ca22e56dSKay Sievers /** 1044ca22e56dSKay Sievers * subsys_dev_iter_next - iterate to the next device 1045ca22e56dSKay Sievers * @iter: subsys iterator to proceed 1046ca22e56dSKay Sievers * 1047ca22e56dSKay Sievers * Proceed @iter to the next device and return it. Returns NULL if 1048ca22e56dSKay Sievers * iteration is complete. 1049ca22e56dSKay Sievers * 1050ca22e56dSKay Sievers * The returned device is referenced and won't be released till 1051ca22e56dSKay Sievers * iterator is proceed to the next device or exited. The caller is 1052ca22e56dSKay Sievers * free to do whatever it wants to do with the device including 1053ca22e56dSKay Sievers * calling back into subsys code. 1054ca22e56dSKay Sievers */ 1055ca22e56dSKay Sievers struct device *subsys_dev_iter_next(struct subsys_dev_iter *iter) 1056ca22e56dSKay Sievers { 1057ca22e56dSKay Sievers struct klist_node *knode; 1058ca22e56dSKay Sievers struct device *dev; 1059ca22e56dSKay Sievers 1060ca22e56dSKay Sievers for (;;) { 1061ca22e56dSKay Sievers knode = klist_next(&iter->ki); 1062ca22e56dSKay Sievers if (!knode) 1063ca22e56dSKay Sievers return NULL; 1064371fd7a2SGeliang Tang dev = to_device_private_bus(knode)->device; 1065ca22e56dSKay Sievers if (!iter->type || iter->type == dev->type) 1066ca22e56dSKay Sievers return dev; 1067ca22e56dSKay Sievers } 1068ca22e56dSKay Sievers } 1069ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_dev_iter_next); 1070ca22e56dSKay Sievers 1071ca22e56dSKay Sievers /** 1072ca22e56dSKay Sievers * subsys_dev_iter_exit - finish iteration 1073ca22e56dSKay Sievers * @iter: subsys iterator to finish 1074ca22e56dSKay Sievers * 1075ca22e56dSKay Sievers * Finish an iteration. Always call this function after iteration is 1076ca22e56dSKay Sievers * complete whether the iteration ran till the end or not. 1077ca22e56dSKay Sievers */ 1078ca22e56dSKay Sievers void subsys_dev_iter_exit(struct subsys_dev_iter *iter) 1079ca22e56dSKay Sievers { 1080ca22e56dSKay Sievers klist_iter_exit(&iter->ki); 1081ca22e56dSKay Sievers } 1082ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_dev_iter_exit); 1083ca22e56dSKay Sievers 1084ca22e56dSKay Sievers int subsys_interface_register(struct subsys_interface *sif) 1085ca22e56dSKay Sievers { 1086ca22e56dSKay Sievers struct bus_type *subsys; 1087ca22e56dSKay Sievers struct subsys_dev_iter iter; 1088ca22e56dSKay Sievers struct device *dev; 1089ca22e56dSKay Sievers 1090ca22e56dSKay Sievers if (!sif || !sif->subsys) 1091ca22e56dSKay Sievers return -ENODEV; 1092ca22e56dSKay Sievers 1093ca22e56dSKay Sievers subsys = bus_get(sif->subsys); 1094ca22e56dSKay Sievers if (!subsys) 1095ca22e56dSKay Sievers return -EINVAL; 1096ca22e56dSKay Sievers 1097ca22e56dSKay Sievers mutex_lock(&subsys->p->mutex); 1098ca22e56dSKay Sievers list_add_tail(&sif->node, &subsys->p->interfaces); 1099ca22e56dSKay Sievers if (sif->add_dev) { 1100ca22e56dSKay Sievers subsys_dev_iter_init(&iter, subsys, NULL, NULL); 1101ca22e56dSKay Sievers while ((dev = subsys_dev_iter_next(&iter))) 1102ca22e56dSKay Sievers sif->add_dev(dev, sif); 1103ca22e56dSKay Sievers subsys_dev_iter_exit(&iter); 1104ca22e56dSKay Sievers } 1105ca22e56dSKay Sievers mutex_unlock(&subsys->p->mutex); 1106ca22e56dSKay Sievers 1107ca22e56dSKay Sievers return 0; 1108ca22e56dSKay Sievers } 1109ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_interface_register); 1110ca22e56dSKay Sievers 1111ca22e56dSKay Sievers void subsys_interface_unregister(struct subsys_interface *sif) 1112ca22e56dSKay Sievers { 11132b31594aSJonghwan Choi struct bus_type *subsys; 1114ca22e56dSKay Sievers struct subsys_dev_iter iter; 1115ca22e56dSKay Sievers struct device *dev; 1116ca22e56dSKay Sievers 11172b31594aSJonghwan Choi if (!sif || !sif->subsys) 1118ca22e56dSKay Sievers return; 1119ca22e56dSKay Sievers 11202b31594aSJonghwan Choi subsys = sif->subsys; 11212b31594aSJonghwan Choi 1122ca22e56dSKay Sievers mutex_lock(&subsys->p->mutex); 1123ca22e56dSKay Sievers list_del_init(&sif->node); 1124ca22e56dSKay Sievers if (sif->remove_dev) { 1125ca22e56dSKay Sievers subsys_dev_iter_init(&iter, subsys, NULL, NULL); 1126ca22e56dSKay Sievers while ((dev = subsys_dev_iter_next(&iter))) 1127ca22e56dSKay Sievers sif->remove_dev(dev, sif); 1128ca22e56dSKay Sievers subsys_dev_iter_exit(&iter); 1129ca22e56dSKay Sievers } 1130ca22e56dSKay Sievers mutex_unlock(&subsys->p->mutex); 1131ca22e56dSKay Sievers 1132ca22e56dSKay Sievers bus_put(subsys); 1133ca22e56dSKay Sievers } 1134ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_interface_unregister); 1135ca22e56dSKay Sievers 1136ca22e56dSKay Sievers static void system_root_device_release(struct device *dev) 1137ca22e56dSKay Sievers { 1138ca22e56dSKay Sievers kfree(dev); 1139ca22e56dSKay Sievers } 1140d73ce004STejun Heo 1141d73ce004STejun Heo static int subsys_register(struct bus_type *subsys, 1142d73ce004STejun Heo const struct attribute_group **groups, 1143d73ce004STejun Heo struct kobject *parent_of_root) 1144d73ce004STejun Heo { 1145d73ce004STejun Heo struct device *dev; 1146d73ce004STejun Heo int err; 1147d73ce004STejun Heo 1148d73ce004STejun Heo err = bus_register(subsys); 1149d73ce004STejun Heo if (err < 0) 1150d73ce004STejun Heo return err; 1151d73ce004STejun Heo 1152d73ce004STejun Heo dev = kzalloc(sizeof(struct device), GFP_KERNEL); 1153d73ce004STejun Heo if (!dev) { 1154d73ce004STejun Heo err = -ENOMEM; 1155d73ce004STejun Heo goto err_dev; 1156d73ce004STejun Heo } 1157d73ce004STejun Heo 1158d73ce004STejun Heo err = dev_set_name(dev, "%s", subsys->name); 1159d73ce004STejun Heo if (err < 0) 1160d73ce004STejun Heo goto err_name; 1161d73ce004STejun Heo 1162d73ce004STejun Heo dev->kobj.parent = parent_of_root; 1163d73ce004STejun Heo dev->groups = groups; 1164d73ce004STejun Heo dev->release = system_root_device_release; 1165d73ce004STejun Heo 1166d73ce004STejun Heo err = device_register(dev); 1167d73ce004STejun Heo if (err < 0) 1168d73ce004STejun Heo goto err_dev_reg; 1169d73ce004STejun Heo 1170d73ce004STejun Heo subsys->dev_root = dev; 1171d73ce004STejun Heo return 0; 1172d73ce004STejun Heo 1173d73ce004STejun Heo err_dev_reg: 1174d73ce004STejun Heo put_device(dev); 1175d73ce004STejun Heo dev = NULL; 1176d73ce004STejun Heo err_name: 1177d73ce004STejun Heo kfree(dev); 1178d73ce004STejun Heo err_dev: 1179d73ce004STejun Heo bus_unregister(subsys); 1180d73ce004STejun Heo return err; 1181d73ce004STejun Heo } 1182d73ce004STejun Heo 1183ca22e56dSKay Sievers /** 1184ca22e56dSKay Sievers * subsys_system_register - register a subsystem at /sys/devices/system/ 118578d79559SRandy Dunlap * @subsys: system subsystem 118678d79559SRandy Dunlap * @groups: default attributes for the root device 1187ca22e56dSKay Sievers * 1188ca22e56dSKay Sievers * All 'system' subsystems have a /sys/devices/system/<name> root device 1189ca22e56dSKay Sievers * with the name of the subsystem. The root device can carry subsystem- 1190ca22e56dSKay Sievers * wide attributes. All registered devices are below this single root 1191ca22e56dSKay Sievers * device and are named after the subsystem with a simple enumeration 1192e227867fSMasanari Iida * number appended. The registered devices are not explicitly named; 1193ca22e56dSKay Sievers * only 'id' in the device needs to be set. 1194ca22e56dSKay Sievers * 1195ca22e56dSKay Sievers * Do not use this interface for anything new, it exists for compatibility 1196ca22e56dSKay Sievers * with bad ideas only. New subsystems should use plain subsystems; and 1197ca22e56dSKay Sievers * add the subsystem-wide attributes should be added to the subsystem 1198ca22e56dSKay Sievers * directory itself and not some create fake root-device placed in 1199ca22e56dSKay Sievers * /sys/devices/system/<name>. 1200ca22e56dSKay Sievers */ 1201ca22e56dSKay Sievers int subsys_system_register(struct bus_type *subsys, 1202ca22e56dSKay Sievers const struct attribute_group **groups) 1203ca22e56dSKay Sievers { 1204d73ce004STejun Heo return subsys_register(subsys, groups, &system_kset->kobj); 1205ca22e56dSKay Sievers } 1206ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_system_register); 1207ca22e56dSKay Sievers 1208d73ce004STejun Heo /** 1209d73ce004STejun Heo * subsys_virtual_register - register a subsystem at /sys/devices/virtual/ 1210d73ce004STejun Heo * @subsys: virtual subsystem 1211d73ce004STejun Heo * @groups: default attributes for the root device 1212d73ce004STejun Heo * 1213d73ce004STejun Heo * All 'virtual' subsystems have a /sys/devices/system/<name> root device 1214d73ce004STejun Heo * with the name of the subystem. The root device can carry subsystem-wide 1215d73ce004STejun Heo * attributes. All registered devices are below this single root device. 1216d73ce004STejun Heo * There's no restriction on device naming. This is for kernel software 1217d73ce004STejun Heo * constructs which need sysfs interface. 1218d73ce004STejun Heo */ 1219d73ce004STejun Heo int subsys_virtual_register(struct bus_type *subsys, 1220d73ce004STejun Heo const struct attribute_group **groups) 1221d73ce004STejun Heo { 1222d73ce004STejun Heo struct kobject *virtual_dir; 1223d73ce004STejun Heo 1224d73ce004STejun Heo virtual_dir = virtual_device_parent(NULL); 1225d73ce004STejun Heo if (!virtual_dir) 1226d73ce004STejun Heo return -ENOMEM; 1227d73ce004STejun Heo 1228d73ce004STejun Heo return subsys_register(subsys, groups, virtual_dir); 1229d73ce004STejun Heo } 12301c04fc35SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(subsys_virtual_register); 1231d73ce004STejun Heo 12321da177e4SLinus Torvalds int __init buses_init(void) 12331da177e4SLinus Torvalds { 123459a54833SGreg Kroah-Hartman bus_kset = kset_create_and_add("bus", &bus_uevent_ops, NULL); 123559a54833SGreg Kroah-Hartman if (!bus_kset) 123659a54833SGreg Kroah-Hartman return -ENOMEM; 1237ca22e56dSKay Sievers 1238ca22e56dSKay Sievers system_kset = kset_create_and_add("system", NULL, &devices_kset->kobj); 1239ca22e56dSKay Sievers if (!system_kset) 1240ca22e56dSKay Sievers return -ENOMEM; 1241ca22e56dSKay Sievers 124259a54833SGreg Kroah-Hartman return 0; 12431da177e4SLinus Torvalds } 1244