1989d42e8SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * bus.c - bus driver management 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Copyright (c) 2002-3 Patrick Mochel 61da177e4SLinus Torvalds * Copyright (c) 2002-3 Open Source Development Labs 7e5dd1278SGreg Kroah-Hartman * Copyright (c) 2007 Greg Kroah-Hartman <gregkh@suse.de> 8e5dd1278SGreg Kroah-Hartman * Copyright (c) 2007 Novell Inc. 9ccfc901fSGreg Kroah-Hartman * Copyright (c) 2023 Greg Kroah-Hartman <gregkh@linuxfoundation.org> 101da177e4SLinus Torvalds */ 111da177e4SLinus Torvalds 12765230b5SDmitry Torokhov #include <linux/async.h> 135aee2bf2SGreg Kroah-Hartman #include <linux/device/bus.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 28273afac6SGreg Kroah-Hartman /* /sys/bus */ 29273afac6SGreg Kroah-Hartman static struct kset *bus_kset; 30273afac6SGreg Kroah-Hartman 311da177e4SLinus Torvalds #define to_bus_attr(_attr) container_of(_attr, struct bus_attribute, attr) 321da177e4SLinus Torvalds 331da177e4SLinus Torvalds /* 341da177e4SLinus Torvalds * sysfs bindings for drivers 351da177e4SLinus Torvalds */ 361da177e4SLinus Torvalds 371da177e4SLinus Torvalds #define to_drv_attr(_attr) container_of(_attr, struct driver_attribute, attr) 381da177e4SLinus Torvalds 394f4b3743SDaniel Vetter #define DRIVER_ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) \ 404f4b3743SDaniel Vetter struct driver_attribute driver_attr_##_name = \ 414f4b3743SDaniel Vetter __ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) 421da177e4SLinus Torvalds 43b8c5cec2SKay Sievers static int __must_check bus_rescan_devices_helper(struct device *dev, 44b8c5cec2SKay Sievers void *data); 45b8c5cec2SKay Sievers 46273afac6SGreg Kroah-Hartman /** 47273afac6SGreg Kroah-Hartman * bus_to_subsys - Turn a struct bus_type into a struct subsys_private 48273afac6SGreg Kroah-Hartman * 49273afac6SGreg Kroah-Hartman * @bus: pointer to the struct bus_type to look up 50273afac6SGreg Kroah-Hartman * 51273afac6SGreg Kroah-Hartman * The driver core internals needs to work on the subsys_private structure, not 52273afac6SGreg Kroah-Hartman * the external struct bus_type pointer. This function walks the list of 53273afac6SGreg Kroah-Hartman * registered busses in the system and finds the matching one and returns the 54273afac6SGreg Kroah-Hartman * internal struct subsys_private that relates to that bus. 55273afac6SGreg Kroah-Hartman * 56273afac6SGreg Kroah-Hartman * Note, the reference count of the return value is INCREMENTED if it is not 57273afac6SGreg Kroah-Hartman * NULL. A call to subsys_put() must be done when finished with the pointer in 58273afac6SGreg Kroah-Hartman * order for it to be properly freed. 59273afac6SGreg Kroah-Hartman */ 60273afac6SGreg Kroah-Hartman static struct subsys_private *bus_to_subsys(const struct bus_type *bus) 61273afac6SGreg Kroah-Hartman { 62273afac6SGreg Kroah-Hartman struct subsys_private *sp = NULL; 63273afac6SGreg Kroah-Hartman struct kobject *kobj; 64273afac6SGreg Kroah-Hartman 65e8b812b3SGeert Uytterhoeven if (!bus || !bus_kset) 66273afac6SGreg Kroah-Hartman return NULL; 67273afac6SGreg Kroah-Hartman 68273afac6SGreg Kroah-Hartman spin_lock(&bus_kset->list_lock); 69273afac6SGreg Kroah-Hartman 70273afac6SGreg Kroah-Hartman if (list_empty(&bus_kset->list)) 71273afac6SGreg Kroah-Hartman goto done; 72273afac6SGreg Kroah-Hartman 73273afac6SGreg Kroah-Hartman list_for_each_entry(kobj, &bus_kset->list, entry) { 74273afac6SGreg Kroah-Hartman struct kset *kset = container_of(kobj, struct kset, kobj); 75273afac6SGreg Kroah-Hartman 76273afac6SGreg Kroah-Hartman sp = container_of_const(kset, struct subsys_private, subsys); 77273afac6SGreg Kroah-Hartman if (sp->bus == bus) 78273afac6SGreg Kroah-Hartman goto done; 79273afac6SGreg Kroah-Hartman } 80273afac6SGreg Kroah-Hartman sp = NULL; 81273afac6SGreg Kroah-Hartman done: 82273afac6SGreg Kroah-Hartman sp = subsys_get(sp); 83273afac6SGreg Kroah-Hartman spin_unlock(&bus_kset->list_lock); 84273afac6SGreg Kroah-Hartman return sp; 85273afac6SGreg Kroah-Hartman } 86273afac6SGreg Kroah-Hartman 8738370c4eSGreg Kroah-Hartman static const struct bus_type *bus_get(const struct bus_type *bus) 885901d014SGreg Kroah-Hartman { 89273afac6SGreg Kroah-Hartman struct subsys_private *sp = bus_to_subsys(bus); 90273afac6SGreg Kroah-Hartman 91273afac6SGreg Kroah-Hartman if (sp) 92c6f7e72aSGreg Kroah-Hartman return bus; 93c6f7e72aSGreg Kroah-Hartman return NULL; 945901d014SGreg Kroah-Hartman } 955901d014SGreg Kroah-Hartman 96273afac6SGreg Kroah-Hartman static void bus_put(const struct bus_type *bus) 97fc1ede58SGreg Kroah-Hartman { 98273afac6SGreg Kroah-Hartman struct subsys_private *sp = bus_to_subsys(bus); 99273afac6SGreg Kroah-Hartman 100273afac6SGreg Kroah-Hartman /* two puts are required as the call to bus_to_subsys incremented it again */ 101273afac6SGreg Kroah-Hartman subsys_put(sp); 102273afac6SGreg Kroah-Hartman subsys_put(sp); 103fc1ede58SGreg Kroah-Hartman } 104fc1ede58SGreg Kroah-Hartman 1054a3ad20cSGreg Kroah-Hartman static ssize_t drv_attr_show(struct kobject *kobj, struct attribute *attr, 1064a3ad20cSGreg Kroah-Hartman char *buf) 1071da177e4SLinus Torvalds { 1081da177e4SLinus Torvalds struct driver_attribute *drv_attr = to_drv_attr(attr); 109e5dd1278SGreg Kroah-Hartman struct driver_private *drv_priv = to_driver(kobj); 1104a0c20bfSDmitry Torokhov ssize_t ret = -EIO; 1111da177e4SLinus Torvalds 1121da177e4SLinus Torvalds if (drv_attr->show) 113e5dd1278SGreg Kroah-Hartman ret = drv_attr->show(drv_priv->driver, buf); 1141da177e4SLinus Torvalds return ret; 1151da177e4SLinus Torvalds } 1161da177e4SLinus Torvalds 1174a3ad20cSGreg Kroah-Hartman static ssize_t drv_attr_store(struct kobject *kobj, struct attribute *attr, 1181da177e4SLinus Torvalds const char *buf, size_t count) 1191da177e4SLinus Torvalds { 1201da177e4SLinus Torvalds struct driver_attribute *drv_attr = to_drv_attr(attr); 121e5dd1278SGreg Kroah-Hartman struct driver_private *drv_priv = to_driver(kobj); 1224a0c20bfSDmitry Torokhov ssize_t ret = -EIO; 1231da177e4SLinus Torvalds 1241da177e4SLinus Torvalds if (drv_attr->store) 125e5dd1278SGreg Kroah-Hartman ret = drv_attr->store(drv_priv->driver, buf, count); 1261da177e4SLinus Torvalds return ret; 1271da177e4SLinus Torvalds } 1281da177e4SLinus Torvalds 12952cf25d0SEmese Revfy static const struct sysfs_ops driver_sysfs_ops = { 1301da177e4SLinus Torvalds .show = drv_attr_show, 1311da177e4SLinus Torvalds .store = drv_attr_store, 1321da177e4SLinus Torvalds }; 1331da177e4SLinus Torvalds 1341da177e4SLinus Torvalds static void driver_release(struct kobject *kobj) 1351da177e4SLinus Torvalds { 136e5dd1278SGreg Kroah-Hartman struct driver_private *drv_priv = to_driver(kobj); 137e5dd1278SGreg Kroah-Hartman 1382b3a302aSHarvey Harrison pr_debug("driver: '%s': %s\n", kobject_name(kobj), __func__); 139e5dd1278SGreg Kroah-Hartman kfree(drv_priv); 1401da177e4SLinus Torvalds } 1411da177e4SLinus Torvalds 142c83d9ab4SThomas Weißschuh static const struct kobj_type driver_ktype = { 1431da177e4SLinus Torvalds .sysfs_ops = &driver_sysfs_ops, 1441da177e4SLinus Torvalds .release = driver_release, 1451da177e4SLinus Torvalds }; 1461da177e4SLinus Torvalds 1471da177e4SLinus Torvalds /* 1481da177e4SLinus Torvalds * sysfs bindings for buses 1491da177e4SLinus Torvalds */ 1504a3ad20cSGreg Kroah-Hartman static ssize_t bus_attr_show(struct kobject *kobj, struct attribute *attr, 1514a3ad20cSGreg Kroah-Hartman char *buf) 1521da177e4SLinus Torvalds { 1531da177e4SLinus Torvalds struct bus_attribute *bus_attr = to_bus_attr(attr); 1546b6e39a6SKay Sievers struct subsys_private *subsys_priv = to_subsys_private(kobj); 155c0fd973cSZijun Hu /* return -EIO for reading a bus attribute without show() */ 156c0fd973cSZijun Hu ssize_t ret = -EIO; 1571da177e4SLinus Torvalds 1581da177e4SLinus Torvalds if (bus_attr->show) 1596b6e39a6SKay Sievers ret = bus_attr->show(subsys_priv->bus, buf); 1601da177e4SLinus Torvalds return ret; 1611da177e4SLinus Torvalds } 1621da177e4SLinus Torvalds 1634a3ad20cSGreg Kroah-Hartman static ssize_t bus_attr_store(struct kobject *kobj, struct attribute *attr, 1641da177e4SLinus Torvalds const char *buf, size_t count) 1651da177e4SLinus Torvalds { 1661da177e4SLinus Torvalds struct bus_attribute *bus_attr = to_bus_attr(attr); 1676b6e39a6SKay Sievers struct subsys_private *subsys_priv = to_subsys_private(kobj); 168c0fd973cSZijun Hu /* return -EIO for writing a bus attribute without store() */ 169c0fd973cSZijun Hu ssize_t ret = -EIO; 1701da177e4SLinus Torvalds 1711da177e4SLinus Torvalds if (bus_attr->store) 1726b6e39a6SKay Sievers ret = bus_attr->store(subsys_priv->bus, buf, count); 1731da177e4SLinus Torvalds return ret; 1741da177e4SLinus Torvalds } 1751da177e4SLinus Torvalds 17652cf25d0SEmese Revfy static const struct sysfs_ops bus_sysfs_ops = { 1771da177e4SLinus Torvalds .show = bus_attr_show, 1781da177e4SLinus Torvalds .store = bus_attr_store, 1791da177e4SLinus Torvalds }; 1801da177e4SLinus Torvalds 1810396f286SGreg Kroah-Hartman int bus_create_file(const struct bus_type *bus, struct bus_attribute *attr) 1821da177e4SLinus Torvalds { 1830396f286SGreg Kroah-Hartman struct subsys_private *sp = bus_to_subsys(bus); 1841da177e4SLinus Torvalds int error; 1850396f286SGreg Kroah-Hartman 1860396f286SGreg Kroah-Hartman if (!sp) 1870396f286SGreg Kroah-Hartman return -EINVAL; 1880396f286SGreg Kroah-Hartman 1890396f286SGreg Kroah-Hartman error = sysfs_create_file(&sp->subsys.kobj, &attr->attr); 1900396f286SGreg Kroah-Hartman 1910396f286SGreg Kroah-Hartman subsys_put(sp); 1921da177e4SLinus Torvalds return error; 1931da177e4SLinus Torvalds } 1944a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_create_file); 1951da177e4SLinus Torvalds 1960396f286SGreg Kroah-Hartman void bus_remove_file(const struct bus_type *bus, struct bus_attribute *attr) 1971da177e4SLinus Torvalds { 1980396f286SGreg Kroah-Hartman struct subsys_private *sp = bus_to_subsys(bus); 1990396f286SGreg Kroah-Hartman 2000396f286SGreg Kroah-Hartman if (!sp) 2010396f286SGreg Kroah-Hartman return; 2020396f286SGreg Kroah-Hartman 2030396f286SGreg Kroah-Hartman sysfs_remove_file(&sp->subsys.kobj, &attr->attr); 2040396f286SGreg Kroah-Hartman subsys_put(sp); 2051da177e4SLinus Torvalds } 2064a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_remove_file); 2071da177e4SLinus Torvalds 208174be70bSBart Van Assche static void bus_release(struct kobject *kobj) 209174be70bSBart Van Assche { 210371fd7a2SGeliang Tang struct subsys_private *priv = to_subsys_private(kobj); 211174be70bSBart Van Assche 21237e98d9bSGreg Kroah-Hartman lockdep_unregister_key(&priv->lock_key); 213174be70bSBart Van Assche kfree(priv); 214174be70bSBart Van Assche } 215174be70bSBart Van Assche 216c83d9ab4SThomas Weißschuh static const struct kobj_type bus_ktype = { 2171da177e4SLinus Torvalds .sysfs_ops = &bus_sysfs_ops, 218174be70bSBart Van Assche .release = bus_release, 2191da177e4SLinus Torvalds }; 2201da177e4SLinus Torvalds 221c45a88bbSGreg Kroah-Hartman static int bus_uevent_filter(const struct kobject *kobj) 22280f03e34SKay Sievers { 223ee6d3dd4SWedson Almeida Filho const struct kobj_type *ktype = get_ktype(kobj); 22480f03e34SKay Sievers 22580f03e34SKay Sievers if (ktype == &bus_ktype) 22680f03e34SKay Sievers return 1; 22780f03e34SKay Sievers return 0; 22880f03e34SKay Sievers } 22980f03e34SKay Sievers 2309cd43611SEmese Revfy static const struct kset_uevent_ops bus_uevent_ops = { 23180f03e34SKay Sievers .filter = bus_uevent_filter, 23280f03e34SKay Sievers }; 23380f03e34SKay Sievers 2342b08c8d0SAlan Stern /* Manually detach a device from its associated driver. */ 2352581c9ccSGreg Kroah-Hartman static ssize_t unbind_store(struct device_driver *drv, const char *buf, 2362581c9ccSGreg Kroah-Hartman size_t count) 237151ef38fSGreg Kroah-Hartman { 23838370c4eSGreg Kroah-Hartman const struct bus_type *bus = bus_get(drv->bus); 239151ef38fSGreg Kroah-Hartman struct device *dev; 240151ef38fSGreg Kroah-Hartman int err = -ENODEV; 241151ef38fSGreg Kroah-Hartman 2421f9ffc04SGreg Kroah-Hartman dev = bus_find_device_by_name(bus, NULL, buf); 2432b08c8d0SAlan Stern if (dev && dev->driver == drv) { 244ed88747cSAlexander Duyck device_driver_detach(dev); 245151ef38fSGreg Kroah-Hartman err = count; 246151ef38fSGreg Kroah-Hartman } 2472b08c8d0SAlan Stern put_device(dev); 248fc1ede58SGreg Kroah-Hartman bus_put(bus); 249151ef38fSGreg Kroah-Hartman return err; 250151ef38fSGreg Kroah-Hartman } 25116b0dd40SJinchao Wang static DRIVER_ATTR_IGNORE_LOCKDEP(unbind, 0200, NULL, unbind_store); 252151ef38fSGreg Kroah-Hartman 253afdce75fSGreg Kroah-Hartman /* 254afdce75fSGreg Kroah-Hartman * Manually attach a device to a driver. 255afdce75fSGreg Kroah-Hartman * Note: the driver must want to bind to the device, 256afdce75fSGreg Kroah-Hartman * it is not possible to override the driver's id table. 257afdce75fSGreg Kroah-Hartman */ 2582581c9ccSGreg Kroah-Hartman static ssize_t bind_store(struct device_driver *drv, const char *buf, 2592581c9ccSGreg Kroah-Hartman size_t count) 260afdce75fSGreg Kroah-Hartman { 26138370c4eSGreg Kroah-Hartman const struct bus_type *bus = bus_get(drv->bus); 262afdce75fSGreg Kroah-Hartman struct device *dev; 263afdce75fSGreg Kroah-Hartman int err = -ENODEV; 264afdce75fSGreg Kroah-Hartman 2651f9ffc04SGreg Kroah-Hartman dev = bus_find_device_by_name(bus, NULL, buf); 266204db60cSJason Gunthorpe if (dev && driver_match_device(drv, dev)) { 267ed88747cSAlexander Duyck err = device_driver_attach(drv, dev); 268ef6dcbddSChristoph Hellwig if (!err) { 2694a3ad20cSGreg Kroah-Hartman /* success */ 27037225401SRyan Wilson err = count; 271afdce75fSGreg Kroah-Hartman } 2724a3ad20cSGreg Kroah-Hartman } 2732b08c8d0SAlan Stern put_device(dev); 274fc1ede58SGreg Kroah-Hartman bus_put(bus); 275afdce75fSGreg Kroah-Hartman return err; 276afdce75fSGreg Kroah-Hartman } 27716b0dd40SJinchao Wang static DRIVER_ATTR_IGNORE_LOCKDEP(bind, 0200, NULL, bind_store); 278afdce75fSGreg Kroah-Hartman 27975cff725SGreg Kroah-Hartman static ssize_t drivers_autoprobe_show(const struct bus_type *bus, char *buf) 280b8c5cec2SKay Sievers { 281a00fdb98SGreg Kroah-Hartman struct subsys_private *sp = bus_to_subsys(bus); 282a00fdb98SGreg Kroah-Hartman int ret; 283a00fdb98SGreg Kroah-Hartman 284a00fdb98SGreg Kroah-Hartman if (!sp) 285a00fdb98SGreg Kroah-Hartman return -EINVAL; 286a00fdb98SGreg Kroah-Hartman 287a00fdb98SGreg Kroah-Hartman ret = sysfs_emit(buf, "%d\n", sp->drivers_autoprobe); 288a00fdb98SGreg Kroah-Hartman subsys_put(sp); 289a00fdb98SGreg Kroah-Hartman return ret; 290b8c5cec2SKay Sievers } 291b8c5cec2SKay Sievers 29275cff725SGreg Kroah-Hartman static ssize_t drivers_autoprobe_store(const struct bus_type *bus, 293b8c5cec2SKay Sievers const char *buf, size_t count) 294b8c5cec2SKay Sievers { 295a00fdb98SGreg Kroah-Hartman struct subsys_private *sp = bus_to_subsys(bus); 296a00fdb98SGreg Kroah-Hartman 297a00fdb98SGreg Kroah-Hartman if (!sp) 298a00fdb98SGreg Kroah-Hartman return -EINVAL; 299a00fdb98SGreg Kroah-Hartman 300b8c5cec2SKay Sievers if (buf[0] == '0') 301a00fdb98SGreg Kroah-Hartman sp->drivers_autoprobe = 0; 302b8c5cec2SKay Sievers else 303a00fdb98SGreg Kroah-Hartman sp->drivers_autoprobe = 1; 304a00fdb98SGreg Kroah-Hartman 305a00fdb98SGreg Kroah-Hartman subsys_put(sp); 306b8c5cec2SKay Sievers return count; 307b8c5cec2SKay Sievers } 308b8c5cec2SKay Sievers 30975cff725SGreg Kroah-Hartman static ssize_t drivers_probe_store(const struct bus_type *bus, 310b8c5cec2SKay Sievers const char *buf, size_t count) 311b8c5cec2SKay Sievers { 312b8c5cec2SKay Sievers struct device *dev; 3130372ffb3SAlex Williamson int err = -EINVAL; 314b8c5cec2SKay Sievers 3151f9ffc04SGreg Kroah-Hartman dev = bus_find_device_by_name(bus, NULL, buf); 316b8c5cec2SKay Sievers if (!dev) 317b8c5cec2SKay Sievers return -ENODEV; 3180372ffb3SAlex Williamson if (bus_rescan_devices_helper(dev, NULL) == 0) 3190372ffb3SAlex Williamson err = count; 3200372ffb3SAlex Williamson put_device(dev); 3210372ffb3SAlex Williamson return err; 322b8c5cec2SKay Sievers } 323151ef38fSGreg Kroah-Hartman 324465c7a3aSmochel@digitalimplant.org static struct device *next_device(struct klist_iter *i) 325465c7a3aSmochel@digitalimplant.org { 326465c7a3aSmochel@digitalimplant.org struct klist_node *n = klist_next(i); 327ae1b4171SGreg Kroah-Hartman struct device *dev = NULL; 328ae1b4171SGreg Kroah-Hartman struct device_private *dev_prv; 329ae1b4171SGreg Kroah-Hartman 330ae1b4171SGreg Kroah-Hartman if (n) { 331ae1b4171SGreg Kroah-Hartman dev_prv = to_device_private_bus(n); 332ae1b4171SGreg Kroah-Hartman dev = dev_prv->device; 333ae1b4171SGreg Kroah-Hartman } 334ae1b4171SGreg Kroah-Hartman return dev; 335465c7a3aSmochel@digitalimplant.org } 336465c7a3aSmochel@digitalimplant.org 3371da177e4SLinus Torvalds /** 3381da177e4SLinus Torvalds * bus_for_each_dev - device iterator. 3391da177e4SLinus Torvalds * @bus: bus type. 3401da177e4SLinus Torvalds * @start: device to start iterating from. 3411da177e4SLinus Torvalds * @data: data for the callback. 3421da177e4SLinus Torvalds * @fn: function to be called for each device. 3431da177e4SLinus Torvalds * 3441da177e4SLinus Torvalds * Iterate over @bus's list of devices, and call @fn for each, 3451da177e4SLinus Torvalds * passing it @data. If @start is not NULL, we use that device to 3461da177e4SLinus Torvalds * begin iterating from. 3471da177e4SLinus Torvalds * 3481da177e4SLinus Torvalds * We check the return of @fn each time. If it returns anything 3491da177e4SLinus Torvalds * other than 0, we break out and return that value. 3501da177e4SLinus Torvalds * 3511da177e4SLinus Torvalds * NOTE: The device that returns a non-zero value is not retained 3521da177e4SLinus Torvalds * in any way, nor is its refcount incremented. If the caller needs 3530fa1b0a1SAlex Chiang * to retain this data, it should do so, and increment the reference 3541da177e4SLinus Torvalds * count in the supplied callback. 3551da177e4SLinus Torvalds */ 356e0766ea4SGreg Kroah-Hartman int bus_for_each_dev(const struct bus_type *bus, struct device *start, 3571da177e4SLinus Torvalds void *data, int (*fn)(struct device *, void *)) 3581da177e4SLinus Torvalds { 35983b9148dSGreg Kroah-Hartman struct subsys_private *sp = bus_to_subsys(bus); 360465c7a3aSmochel@digitalimplant.org struct klist_iter i; 361465c7a3aSmochel@digitalimplant.org struct device *dev; 362465c7a3aSmochel@digitalimplant.org int error = 0; 3631da177e4SLinus Torvalds 36483b9148dSGreg Kroah-Hartman if (!sp) 365465c7a3aSmochel@digitalimplant.org return -EINVAL; 366465c7a3aSmochel@digitalimplant.org 36783b9148dSGreg Kroah-Hartman klist_iter_init_node(&sp->klist_devices, &i, 368ae1b4171SGreg Kroah-Hartman (start ? &start->p->knode_bus : NULL)); 36993ead7c9SGimcuan Hui while (!error && (dev = next_device(&i))) 370465c7a3aSmochel@digitalimplant.org error = fn(dev, data); 371465c7a3aSmochel@digitalimplant.org klist_iter_exit(&i); 37283b9148dSGreg Kroah-Hartman subsys_put(sp); 373465c7a3aSmochel@digitalimplant.org return error; 3741da177e4SLinus Torvalds } 3754a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_for_each_dev); 3761da177e4SLinus Torvalds 3770edb5860SCornelia Huck /** 3780edb5860SCornelia Huck * bus_find_device - device iterator for locating a particular device. 3790edb5860SCornelia Huck * @bus: bus type 3800edb5860SCornelia Huck * @start: Device to begin with 3810edb5860SCornelia Huck * @data: Data to pass to match function 3820edb5860SCornelia Huck * @match: Callback function to check device 3830edb5860SCornelia Huck * 3840edb5860SCornelia Huck * This is similar to the bus_for_each_dev() function above, but it 3850edb5860SCornelia Huck * returns a reference to a device that is 'found' for later use, as 3860edb5860SCornelia Huck * determined by the @match callback. 3870edb5860SCornelia Huck * 3880edb5860SCornelia Huck * The callback should return 0 if the device doesn't match and non-zero 3890edb5860SCornelia Huck * if it does. If the callback returns non-zero, this function will 3900edb5860SCornelia Huck * return to the caller and not iterate over any more devices. 3910edb5860SCornelia Huck */ 392e0766ea4SGreg Kroah-Hartman struct device *bus_find_device(const struct bus_type *bus, 393418e3ea1SSuzuki K Poulose struct device *start, const void *data, 394*b45ed06fSZijun Hu device_match_t match) 3950edb5860SCornelia Huck { 39683b9148dSGreg Kroah-Hartman struct subsys_private *sp = bus_to_subsys(bus); 3970edb5860SCornelia Huck struct klist_iter i; 3980edb5860SCornelia Huck struct device *dev; 3990edb5860SCornelia Huck 40083b9148dSGreg Kroah-Hartman if (!sp) 4010edb5860SCornelia Huck return NULL; 4020edb5860SCornelia Huck 40383b9148dSGreg Kroah-Hartman klist_iter_init_node(&sp->klist_devices, &i, 4047cd9c9bbSGreg Kroah-Hartman (start ? &start->p->knode_bus : NULL)); 4050edb5860SCornelia Huck while ((dev = next_device(&i))) 4060edb5860SCornelia Huck if (match(dev, data) && get_device(dev)) 4070edb5860SCornelia Huck break; 4080edb5860SCornelia Huck klist_iter_exit(&i); 40983b9148dSGreg Kroah-Hartman subsys_put(sp); 4100edb5860SCornelia Huck return dev; 4110edb5860SCornelia Huck } 4124a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_find_device); 41338fdac3cSmochel@digitalimplant.org 41438fdac3cSmochel@digitalimplant.org static struct device_driver *next_driver(struct klist_iter *i) 41538fdac3cSmochel@digitalimplant.org { 41638fdac3cSmochel@digitalimplant.org struct klist_node *n = klist_next(i); 417e5dd1278SGreg Kroah-Hartman struct driver_private *drv_priv; 418e5dd1278SGreg Kroah-Hartman 419e5dd1278SGreg Kroah-Hartman if (n) { 420e5dd1278SGreg Kroah-Hartman drv_priv = container_of(n, struct driver_private, knode_bus); 421e5dd1278SGreg Kroah-Hartman return drv_priv->driver; 422e5dd1278SGreg Kroah-Hartman } 423e5dd1278SGreg Kroah-Hartman return NULL; 42438fdac3cSmochel@digitalimplant.org } 42538fdac3cSmochel@digitalimplant.org 4261da177e4SLinus Torvalds /** 4271da177e4SLinus Torvalds * bus_for_each_drv - driver iterator 4281da177e4SLinus Torvalds * @bus: bus we're dealing with. 4291da177e4SLinus Torvalds * @start: driver to start iterating on. 4301da177e4SLinus Torvalds * @data: data to pass to the callback. 4311da177e4SLinus Torvalds * @fn: function to call for each driver. 4321da177e4SLinus Torvalds * 4331da177e4SLinus Torvalds * This is nearly identical to the device iterator above. 4341da177e4SLinus Torvalds * We iterate over each driver that belongs to @bus, and call 4351da177e4SLinus Torvalds * @fn for each. If @fn returns anything but 0, we break out 4361da177e4SLinus Torvalds * and return it. If @start is not NULL, we use it as the head 4371da177e4SLinus Torvalds * of the list. 4381da177e4SLinus Torvalds * 4391da177e4SLinus Torvalds * NOTE: we don't return the driver that returns a non-zero 4401da177e4SLinus Torvalds * value, nor do we leave the reference count incremented for that 4411da177e4SLinus Torvalds * driver. If the caller needs to know that info, it must set it 4421da177e4SLinus Torvalds * in the callback. It must also be sure to increment the refcount 4431da177e4SLinus Torvalds * so it doesn't disappear before returning to the caller. 4441da177e4SLinus Torvalds */ 445e0766ea4SGreg Kroah-Hartman int bus_for_each_drv(const struct bus_type *bus, struct device_driver *start, 4461da177e4SLinus Torvalds void *data, int (*fn)(struct device_driver *, void *)) 4471da177e4SLinus Torvalds { 44883b9148dSGreg Kroah-Hartman struct subsys_private *sp = bus_to_subsys(bus); 44938fdac3cSmochel@digitalimplant.org struct klist_iter i; 45038fdac3cSmochel@digitalimplant.org struct device_driver *drv; 45138fdac3cSmochel@digitalimplant.org int error = 0; 4521da177e4SLinus Torvalds 45383b9148dSGreg Kroah-Hartman if (!sp) 45438fdac3cSmochel@digitalimplant.org return -EINVAL; 45538fdac3cSmochel@digitalimplant.org 45683b9148dSGreg Kroah-Hartman klist_iter_init_node(&sp->klist_drivers, &i, 457e5dd1278SGreg Kroah-Hartman start ? &start->p->knode_bus : NULL); 45838fdac3cSmochel@digitalimplant.org while ((drv = next_driver(&i)) && !error) 45938fdac3cSmochel@digitalimplant.org error = fn(drv, data); 46038fdac3cSmochel@digitalimplant.org klist_iter_exit(&i); 46183b9148dSGreg Kroah-Hartman subsys_put(sp); 46238fdac3cSmochel@digitalimplant.org return error; 4631da177e4SLinus Torvalds } 4644a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_for_each_drv); 4651da177e4SLinus Torvalds 4661da177e4SLinus Torvalds /** 4671da177e4SLinus Torvalds * bus_add_device - add device to bus 4681da177e4SLinus Torvalds * @dev: device being added 4691da177e4SLinus Torvalds * 4702023c610SAlan Stern * - Add device's bus attributes. 4712023c610SAlan Stern * - Create links to device's bus. 4721da177e4SLinus Torvalds * - Add the device to its bus's list of devices. 4731da177e4SLinus Torvalds */ 4741da177e4SLinus Torvalds int bus_add_device(struct device *dev) 4751da177e4SLinus Torvalds { 4765221b82dSGreg Kroah-Hartman struct subsys_private *sp = bus_to_subsys(dev->bus); 4775221b82dSGreg Kroah-Hartman int error; 4781da177e4SLinus Torvalds 4795221b82dSGreg Kroah-Hartman if (!sp) { 4805221b82dSGreg Kroah-Hartman /* 4815221b82dSGreg Kroah-Hartman * This is a normal operation for many devices that do not 4825221b82dSGreg Kroah-Hartman * have a bus assigned to them, just say that all went 4835221b82dSGreg Kroah-Hartman * well. 4845221b82dSGreg Kroah-Hartman */ 4855221b82dSGreg Kroah-Hartman return 0; 4865221b82dSGreg Kroah-Hartman } 4875221b82dSGreg Kroah-Hartman 4885221b82dSGreg Kroah-Hartman /* 4895221b82dSGreg Kroah-Hartman * Reference in sp is now incremented and will be dropped when 4905221b82dSGreg Kroah-Hartman * the device is removed from the bus 4915221b82dSGreg Kroah-Hartman */ 4925221b82dSGreg Kroah-Hartman 4935221b82dSGreg Kroah-Hartman pr_debug("bus: '%s': add device %s\n", sp->bus->name, dev_name(dev)); 4945221b82dSGreg Kroah-Hartman 4955221b82dSGreg Kroah-Hartman error = device_add_groups(dev, sp->bus->dev_groups); 496fa6fdb33SGreg Kroah-Hartman if (error) 497b46c7337SGreg Kroah-Hartman goto out_put; 4985221b82dSGreg Kroah-Hartman 4995221b82dSGreg Kroah-Hartman error = sysfs_create_link(&sp->devices_kset->kobj, &dev->kobj, dev_name(dev)); 500f86db396SAndrew Morton if (error) 5011c34203aSJunjie Mao goto out_groups; 5025221b82dSGreg Kroah-Hartman 5035221b82dSGreg Kroah-Hartman error = sysfs_create_link(&dev->kobj, &sp->subsys.kobj, "subsystem"); 504f86db396SAndrew Morton if (error) 505513e7337SCornelia Huck goto out_subsys; 5065221b82dSGreg Kroah-Hartman 5075221b82dSGreg Kroah-Hartman klist_add_tail(&dev->p->knode_bus, &sp->klist_devices); 508513e7337SCornelia Huck return 0; 509513e7337SCornelia Huck 510513e7337SCornelia Huck out_subsys: 5115221b82dSGreg Kroah-Hartman sysfs_remove_link(&sp->devices_kset->kobj, dev_name(dev)); 512fa6fdb33SGreg Kroah-Hartman out_groups: 5135221b82dSGreg Kroah-Hartman device_remove_groups(dev, sp->bus->dev_groups); 514513e7337SCornelia Huck out_put: 5155221b82dSGreg Kroah-Hartman subsys_put(sp); 5161da177e4SLinus Torvalds return error; 5171da177e4SLinus Torvalds } 5181da177e4SLinus Torvalds 5191da177e4SLinus Torvalds /** 5202023c610SAlan Stern * bus_probe_device - probe drivers for a new device 5212023c610SAlan Stern * @dev: device to probe 52253877d06SKay Sievers * 5232023c610SAlan Stern * - Automatically probe for a driver if the bus allows it. 52453877d06SKay Sievers */ 5252023c610SAlan Stern void bus_probe_device(struct device *dev) 52653877d06SKay Sievers { 5275221b82dSGreg Kroah-Hartman struct subsys_private *sp = bus_to_subsys(dev->bus); 528ca22e56dSKay Sievers struct subsys_interface *sif; 52953877d06SKay Sievers 5305221b82dSGreg Kroah-Hartman if (!sp) 531ca22e56dSKay Sievers return; 532ca22e56dSKay Sievers 5335221b82dSGreg Kroah-Hartman if (sp->drivers_autoprobe) 534765230b5SDmitry Torokhov device_initial_probe(dev); 535ca22e56dSKay Sievers 5365221b82dSGreg Kroah-Hartman mutex_lock(&sp->mutex); 5375221b82dSGreg Kroah-Hartman list_for_each_entry(sif, &sp->interfaces, node) 538ca22e56dSKay Sievers if (sif->add_dev) 539ca22e56dSKay Sievers sif->add_dev(dev, sif); 5405221b82dSGreg Kroah-Hartman mutex_unlock(&sp->mutex); 5415221b82dSGreg Kroah-Hartman subsys_put(sp); 542f86db396SAndrew Morton } 54353877d06SKay Sievers 54453877d06SKay Sievers /** 5451da177e4SLinus Torvalds * bus_remove_device - remove device from bus 5461da177e4SLinus Torvalds * @dev: device to be removed 5471da177e4SLinus Torvalds * 548ca22e56dSKay Sievers * - Remove device from all interfaces. 549ca22e56dSKay Sievers * - Remove symlink from bus' directory. 5501da177e4SLinus Torvalds * - Delete device from bus's list. 5511da177e4SLinus Torvalds * - Detach from its driver. 5521da177e4SLinus Torvalds * - Drop reference taken in bus_add_device(). 5531da177e4SLinus Torvalds */ 5541da177e4SLinus Torvalds void bus_remove_device(struct device *dev) 5551da177e4SLinus Torvalds { 5565221b82dSGreg Kroah-Hartman struct subsys_private *sp = bus_to_subsys(dev->bus); 557ca22e56dSKay Sievers struct subsys_interface *sif; 558ca22e56dSKay Sievers 5595221b82dSGreg Kroah-Hartman if (!sp) 560ca22e56dSKay Sievers return; 561ca22e56dSKay Sievers 5625221b82dSGreg Kroah-Hartman mutex_lock(&sp->mutex); 5635221b82dSGreg Kroah-Hartman list_for_each_entry(sif, &sp->interfaces, node) 564ca22e56dSKay Sievers if (sif->remove_dev) 565ca22e56dSKay Sievers sif->remove_dev(dev, sif); 5665221b82dSGreg Kroah-Hartman mutex_unlock(&sp->mutex); 567ca22e56dSKay Sievers 568b9d9c82bSKay Sievers sysfs_remove_link(&dev->kobj, "subsystem"); 5695221b82dSGreg Kroah-Hartman sysfs_remove_link(&sp->devices_kset->kobj, dev_name(dev)); 570fa6fdb33SGreg Kroah-Hartman device_remove_groups(dev, dev->bus->dev_groups); 571ae1b4171SGreg Kroah-Hartman if (klist_node_attached(&dev->p->knode_bus)) 572ae1b4171SGreg Kroah-Hartman klist_del(&dev->p->knode_bus); 5733f62e570SGreg Kroah-Hartman 5744a3ad20cSGreg Kroah-Hartman pr_debug("bus: '%s': remove device %s\n", 5751e0b2cf9SKay Sievers dev->bus->name, dev_name(dev)); 5761da177e4SLinus Torvalds device_release_driver(dev); 5775221b82dSGreg Kroah-Hartman 5785221b82dSGreg Kroah-Hartman /* 5795221b82dSGreg Kroah-Hartman * Decrement the reference count twice, once for the bus_to_subsys() 5805221b82dSGreg Kroah-Hartman * call in the start of this function, and the second one from the 5815221b82dSGreg Kroah-Hartman * reference increment in bus_add_device() 5825221b82dSGreg Kroah-Hartman */ 5835221b82dSGreg Kroah-Hartman subsys_put(sp); 5845221b82dSGreg Kroah-Hartman subsys_put(sp); 5851da177e4SLinus Torvalds } 5861da177e4SLinus Torvalds 587f86db396SAndrew Morton static int __must_check add_bind_files(struct device_driver *drv) 588874c6241SGreg Kroah-Hartman { 589f86db396SAndrew Morton int ret; 590f86db396SAndrew Morton 591f86db396SAndrew Morton ret = driver_create_file(drv, &driver_attr_unbind); 592f86db396SAndrew Morton if (ret == 0) { 593f86db396SAndrew Morton ret = driver_create_file(drv, &driver_attr_bind); 594f86db396SAndrew Morton if (ret) 595f86db396SAndrew Morton driver_remove_file(drv, &driver_attr_unbind); 596f86db396SAndrew Morton } 597f86db396SAndrew Morton return ret; 598874c6241SGreg Kroah-Hartman } 599874c6241SGreg Kroah-Hartman 600874c6241SGreg Kroah-Hartman static void remove_bind_files(struct device_driver *drv) 601874c6241SGreg Kroah-Hartman { 602874c6241SGreg Kroah-Hartman driver_remove_file(drv, &driver_attr_bind); 603874c6241SGreg Kroah-Hartman driver_remove_file(drv, &driver_attr_unbind); 604874c6241SGreg Kroah-Hartman } 605b8c5cec2SKay Sievers 6062e7189b6SGreg Kroah-Hartman static BUS_ATTR_WO(drivers_probe); 6072e7189b6SGreg Kroah-Hartman static BUS_ATTR_RW(drivers_autoprobe); 6088380770cSKay Sievers 6094dd1f3f8SGreg Kroah-Hartman static int add_probe_files(const struct bus_type *bus) 610b8c5cec2SKay Sievers { 611b8c5cec2SKay Sievers int retval; 612b8c5cec2SKay Sievers 6138380770cSKay Sievers retval = bus_create_file(bus, &bus_attr_drivers_probe); 614b8c5cec2SKay Sievers if (retval) 615b8c5cec2SKay Sievers goto out; 616b8c5cec2SKay Sievers 6178380770cSKay Sievers retval = bus_create_file(bus, &bus_attr_drivers_autoprobe); 618b8c5cec2SKay Sievers if (retval) 6198380770cSKay Sievers bus_remove_file(bus, &bus_attr_drivers_probe); 620b8c5cec2SKay Sievers out: 621b8c5cec2SKay Sievers return retval; 622b8c5cec2SKay Sievers } 623b8c5cec2SKay Sievers 6244dd1f3f8SGreg Kroah-Hartman static void remove_probe_files(const struct bus_type *bus) 625b8c5cec2SKay Sievers { 6268380770cSKay Sievers bus_remove_file(bus, &bus_attr_drivers_autoprobe); 6278380770cSKay Sievers bus_remove_file(bus, &bus_attr_drivers_probe); 628b8c5cec2SKay Sievers } 6291da177e4SLinus Torvalds 6302581c9ccSGreg Kroah-Hartman static ssize_t uevent_store(struct device_driver *drv, const char *buf, 6312581c9ccSGreg Kroah-Hartman size_t count) 6327ac1cf4aSKay Sievers { 633df44b479SPeter Rajnoha int rc; 634df44b479SPeter Rajnoha 635df44b479SPeter Rajnoha rc = kobject_synth_uevent(&drv->p->kobj, buf, count); 636df44b479SPeter Rajnoha return rc ? rc : count; 6377ac1cf4aSKay Sievers } 6382581c9ccSGreg Kroah-Hartman static DRIVER_ATTR_WO(uevent); 6397ac1cf4aSKay Sievers 6401da177e4SLinus Torvalds /** 6411da177e4SLinus Torvalds * bus_add_driver - Add a driver to the bus. 6421da177e4SLinus Torvalds * @drv: driver. 6431da177e4SLinus Torvalds */ 6441da177e4SLinus Torvalds int bus_add_driver(struct device_driver *drv) 6451da177e4SLinus Torvalds { 646e4f05682SGreg Kroah-Hartman struct subsys_private *sp = bus_to_subsys(drv->bus); 647e5dd1278SGreg Kroah-Hartman struct driver_private *priv; 6481da177e4SLinus Torvalds int error = 0; 6491da177e4SLinus Torvalds 650e4f05682SGreg Kroah-Hartman if (!sp) 6514f6e1945SGreg Kroah-Hartman return -EINVAL; 652d9fd4d3bSJeff Garzik 653e4f05682SGreg Kroah-Hartman /* 654e4f05682SGreg Kroah-Hartman * Reference in sp is now incremented and will be dropped when 655e4f05682SGreg Kroah-Hartman * the driver is removed from the bus 656e4f05682SGreg Kroah-Hartman */ 657e4f05682SGreg Kroah-Hartman pr_debug("bus: '%s': add driver %s\n", sp->bus->name, drv->name); 658e5dd1278SGreg Kroah-Hartman 659e5dd1278SGreg Kroah-Hartman priv = kzalloc(sizeof(*priv), GFP_KERNEL); 66007634464SCornelia Huck if (!priv) { 66107634464SCornelia Huck error = -ENOMEM; 66207634464SCornelia Huck goto out_put_bus; 66307634464SCornelia Huck } 664e5dd1278SGreg Kroah-Hartman klist_init(&priv->klist_devices, NULL, NULL); 665e5dd1278SGreg Kroah-Hartman priv->driver = drv; 666e5dd1278SGreg Kroah-Hartman drv->p = priv; 667e4f05682SGreg Kroah-Hartman priv->kobj.kset = sp->drivers_kset; 668c8e90d82SGreg Kroah-Hartman error = kobject_init_and_add(&priv->kobj, &driver_ktype, NULL, 669c8e90d82SGreg Kroah-Hartman "%s", drv->name); 670dc0afa83SCornelia Huck if (error) 67107634464SCornelia Huck goto out_unregister; 6721da177e4SLinus Torvalds 673e4f05682SGreg Kroah-Hartman klist_add_tail(&priv->knode_bus, &sp->klist_drivers); 674e4f05682SGreg Kroah-Hartman if (sp->drivers_autoprobe) { 675f86db396SAndrew Morton error = driver_attach(drv); 676f86db396SAndrew Morton if (error) 677310862e5SSchspa Shi goto out_del_list; 678b8c5cec2SKay Sievers } 67985d2b0aaSArnd Bergmann error = module_add_driver(drv->owner, drv); 68085d2b0aaSArnd Bergmann if (error) { 68185d2b0aaSArnd Bergmann printk(KERN_ERR "%s: failed to create module links for %s\n", 68285d2b0aaSArnd Bergmann __func__, drv->name); 68385d2b0aaSArnd Bergmann goto out_detach; 68485d2b0aaSArnd Bergmann } 6851da177e4SLinus Torvalds 6867ac1cf4aSKay Sievers error = driver_create_file(drv, &driver_attr_uevent); 6877ac1cf4aSKay Sievers if (error) { 6887ac1cf4aSKay Sievers printk(KERN_ERR "%s: uevent attr (%s) failed\n", 6892b3a302aSHarvey Harrison __func__, drv->name); 6907ac1cf4aSKay Sievers } 691e4f05682SGreg Kroah-Hartman error = driver_add_groups(drv, sp->bus->drv_groups); 692f86db396SAndrew Morton if (error) { 693f86db396SAndrew Morton /* How the hell do we get out of this pickle? Give up */ 6944044b2fcSJoe Pater printk(KERN_ERR "%s: driver_add_groups(%s) failed\n", 695ed0617b5SGreg Kroah-Hartman __func__, drv->name); 696e18945b1SGreg Kroah-Hartman } 6971a6f2a75SDmitry Torokhov 6981a6f2a75SDmitry Torokhov if (!drv->suppress_bind_attrs) { 699f86db396SAndrew Morton error = add_bind_files(drv); 700f86db396SAndrew Morton if (error) { 701f86db396SAndrew Morton /* Ditto */ 702f86db396SAndrew Morton printk(KERN_ERR "%s: add_bind_files(%s) failed\n", 7032b3a302aSHarvey Harrison __func__, drv->name); 704f86db396SAndrew Morton } 7051a6f2a75SDmitry Torokhov } 706d9fd4d3bSJeff Garzik 7075c8563d7SKay Sievers return 0; 7081a6f2a75SDmitry Torokhov 70985d2b0aaSArnd Bergmann out_detach: 71085d2b0aaSArnd Bergmann driver_detach(drv); 711310862e5SSchspa Shi out_del_list: 712310862e5SSchspa Shi klist_del(&priv->knode_bus); 713f86db396SAndrew Morton out_unregister: 71499b28f1bSPhil Carmody kobject_put(&priv->kobj); 7150f9b011dSChristophe JAILLET /* drv->p is freed in driver_release() */ 7165c8563d7SKay Sievers drv->p = NULL; 717f86db396SAndrew Morton out_put_bus: 718e4f05682SGreg Kroah-Hartman subsys_put(sp); 719f86db396SAndrew Morton return error; 7201da177e4SLinus Torvalds } 7211da177e4SLinus Torvalds 7221da177e4SLinus Torvalds /** 7231da177e4SLinus Torvalds * bus_remove_driver - delete driver from bus's knowledge. 7241da177e4SLinus Torvalds * @drv: driver. 7251da177e4SLinus Torvalds * 7261da177e4SLinus Torvalds * Detach the driver from the devices it controls, and remove 7271da177e4SLinus Torvalds * it from its bus's list of drivers. Finally, we drop the reference 7281da177e4SLinus Torvalds * to the bus we took in bus_add_driver(). 7291da177e4SLinus Torvalds */ 7301da177e4SLinus Torvalds void bus_remove_driver(struct device_driver *drv) 7311da177e4SLinus Torvalds { 732e4f05682SGreg Kroah-Hartman struct subsys_private *sp = bus_to_subsys(drv->bus); 733e4f05682SGreg Kroah-Hartman 734e4f05682SGreg Kroah-Hartman if (!sp) 735d9fd4d3bSJeff Garzik return; 736d9fd4d3bSJeff Garzik 737e4f05682SGreg Kroah-Hartman pr_debug("bus: '%s': remove driver %s\n", sp->bus->name, drv->name); 738e4f05682SGreg Kroah-Hartman 7391a6f2a75SDmitry Torokhov if (!drv->suppress_bind_attrs) 740874c6241SGreg Kroah-Hartman remove_bind_files(drv); 741e4f05682SGreg Kroah-Hartman driver_remove_groups(drv, sp->bus->drv_groups); 7427ac1cf4aSKay Sievers driver_remove_file(drv, &driver_attr_uevent); 743e5dd1278SGreg Kroah-Hartman klist_remove(&drv->p->knode_bus); 7441da177e4SLinus Torvalds driver_detach(drv); 7451da177e4SLinus Torvalds module_remove_driver(drv); 746c10997f6SGreg Kroah-Hartman kobject_put(&drv->p->kobj); 747e4f05682SGreg Kroah-Hartman 748e4f05682SGreg Kroah-Hartman /* 749e4f05682SGreg Kroah-Hartman * Decrement the reference count twice, once for the bus_to_subsys() 750e4f05682SGreg Kroah-Hartman * call in the start of this function, and the second one from the 751e4f05682SGreg Kroah-Hartman * reference increment in bus_add_driver() 752e4f05682SGreg Kroah-Hartman */ 753e4f05682SGreg Kroah-Hartman subsys_put(sp); 754e4f05682SGreg Kroah-Hartman subsys_put(sp); 7551da177e4SLinus Torvalds } 7561da177e4SLinus Torvalds 7571da177e4SLinus Torvalds /* Helper for bus_rescan_devices's iter */ 758f86db396SAndrew Morton static int __must_check bus_rescan_devices_helper(struct device *dev, 759f86db396SAndrew Morton void *data) 7601da177e4SLinus Torvalds { 761f86db396SAndrew Morton int ret = 0; 762f86db396SAndrew Morton 763bf74ad5bSAlan Stern if (!dev->driver) { 7648c97a46aSMartin Liu if (dev->parent && dev->bus->need_parent_lock) 7658e9394ceSGreg Kroah-Hartman device_lock(dev->parent); 766f86db396SAndrew Morton ret = device_attach(dev); 7678c97a46aSMartin Liu if (dev->parent && dev->bus->need_parent_lock) 7688e9394ceSGreg Kroah-Hartman device_unlock(dev->parent); 769bf74ad5bSAlan Stern } 770f86db396SAndrew Morton return ret < 0 ? ret : 0; 7711da177e4SLinus Torvalds } 7721da177e4SLinus Torvalds 7731da177e4SLinus Torvalds /** 7741da177e4SLinus Torvalds * bus_rescan_devices - rescan devices on the bus for possible drivers 7751da177e4SLinus Torvalds * @bus: the bus to scan. 7761da177e4SLinus Torvalds * 7771da177e4SLinus Torvalds * This function will look for devices on the bus with no driver 77823d3d602SGreg Kroah-Hartman * attached and rescan it against existing drivers to see if it matches 77923d3d602SGreg Kroah-Hartman * any by calling device_attach() for the unbound devices. 7801da177e4SLinus Torvalds */ 7819622b9f2SGreg Kroah-Hartman int bus_rescan_devices(const struct bus_type *bus) 7821da177e4SLinus Torvalds { 783f86db396SAndrew Morton return bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper); 7841da177e4SLinus Torvalds } 7854a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_rescan_devices); 7861da177e4SLinus Torvalds 787e935d5daSMoore, Eric /** 788e935d5daSMoore, Eric * device_reprobe - remove driver for a device and probe for a new driver 789e935d5daSMoore, Eric * @dev: the device to reprobe 790e935d5daSMoore, Eric * 791e935d5daSMoore, Eric * This function detaches the attached driver (if any) for the given 792e935d5daSMoore, Eric * device and restarts the driver probing process. It is intended 793e935d5daSMoore, Eric * to use if probing criteria changed during a devices lifetime and 794e935d5daSMoore, Eric * driver attachment should change accordingly. 795e935d5daSMoore, Eric */ 796f86db396SAndrew Morton int device_reprobe(struct device *dev) 797e935d5daSMoore, Eric { 798ed88747cSAlexander Duyck if (dev->driver) 799ed88747cSAlexander Duyck device_driver_detach(dev); 800f86db396SAndrew Morton return bus_rescan_devices_helper(dev, NULL); 801e935d5daSMoore, Eric } 802e935d5daSMoore, Eric EXPORT_SYMBOL_GPL(device_reprobe); 8031da177e4SLinus Torvalds 80434bb61f9SJames Bottomley static void klist_devices_get(struct klist_node *n) 80534bb61f9SJames Bottomley { 806ae1b4171SGreg Kroah-Hartman struct device_private *dev_prv = to_device_private_bus(n); 807ae1b4171SGreg Kroah-Hartman struct device *dev = dev_prv->device; 80834bb61f9SJames Bottomley 80934bb61f9SJames Bottomley get_device(dev); 81034bb61f9SJames Bottomley } 81134bb61f9SJames Bottomley 81234bb61f9SJames Bottomley static void klist_devices_put(struct klist_node *n) 81334bb61f9SJames Bottomley { 814ae1b4171SGreg Kroah-Hartman struct device_private *dev_prv = to_device_private_bus(n); 815ae1b4171SGreg Kroah-Hartman struct device *dev = dev_prv->device; 81634bb61f9SJames Bottomley 81734bb61f9SJames Bottomley put_device(dev); 81834bb61f9SJames Bottomley } 81934bb61f9SJames Bottomley 82075cff725SGreg Kroah-Hartman static ssize_t bus_uevent_store(const struct bus_type *bus, 8217ac1cf4aSKay Sievers const char *buf, size_t count) 8227ac1cf4aSKay Sievers { 823a00fdb98SGreg Kroah-Hartman struct subsys_private *sp = bus_to_subsys(bus); 824a00fdb98SGreg Kroah-Hartman int ret; 825df44b479SPeter Rajnoha 826a00fdb98SGreg Kroah-Hartman if (!sp) 827a00fdb98SGreg Kroah-Hartman return -EINVAL; 828a00fdb98SGreg Kroah-Hartman 829a00fdb98SGreg Kroah-Hartman ret = kobject_synth_uevent(&sp->subsys.kobj, buf, count); 830a00fdb98SGreg Kroah-Hartman subsys_put(sp); 831a00fdb98SGreg Kroah-Hartman 832a00fdb98SGreg Kroah-Hartman if (ret) 833a00fdb98SGreg Kroah-Hartman return ret; 834a00fdb98SGreg Kroah-Hartman return count; 8357ac1cf4aSKay Sievers } 836a4723041SGreg Kroah-Hartman /* 837a4723041SGreg Kroah-Hartman * "open code" the old BUS_ATTR() macro here. We want to use BUS_ATTR_WO() 838a4723041SGreg Kroah-Hartman * here, but can not use it as earlier in the file we have 839a4723041SGreg Kroah-Hartman * DEVICE_ATTR_WO(uevent), which would cause a clash with the with the store 840a4723041SGreg Kroah-Hartman * function name. 841a4723041SGreg Kroah-Hartman */ 84216b0dd40SJinchao Wang static struct bus_attribute bus_attr_uevent = __ATTR(uevent, 0200, NULL, 843a4723041SGreg Kroah-Hartman bus_uevent_store); 8447ac1cf4aSKay Sievers 8451da177e4SLinus Torvalds /** 846be871b7eSMichal Hocko * bus_register - register a driver-core subsystem 84778d79559SRandy Dunlap * @bus: bus to register 8481da177e4SLinus Torvalds * 84978d79559SRandy Dunlap * Once we have that, we register the bus with the kobject 8501da177e4SLinus Torvalds * infrastructure, then register the children subsystems it has: 851ca22e56dSKay Sievers * the devices and drivers that belong to the subsystem. 8521da177e4SLinus Torvalds */ 85300c4a3c4SGreg Kroah-Hartman int bus_register(const struct bus_type *bus) 8541da177e4SLinus Torvalds { 8551da177e4SLinus Torvalds int retval; 8566b6e39a6SKay Sievers struct subsys_private *priv; 8573465e2e4SGreg Kroah-Hartman struct kobject *bus_kobj; 85837e98d9bSGreg Kroah-Hartman struct lock_class_key *key; 8591da177e4SLinus Torvalds 8606b6e39a6SKay Sievers priv = kzalloc(sizeof(struct subsys_private), GFP_KERNEL); 861c6f7e72aSGreg Kroah-Hartman if (!priv) 862c6f7e72aSGreg Kroah-Hartman return -ENOMEM; 863116af378SBenjamin Herrenschmidt 864c6f7e72aSGreg Kroah-Hartman priv->bus = bus; 865c6f7e72aSGreg Kroah-Hartman 866c6f7e72aSGreg Kroah-Hartman BLOCKING_INIT_NOTIFIER_HEAD(&priv->bus_notifier); 867c6f7e72aSGreg Kroah-Hartman 8683465e2e4SGreg Kroah-Hartman bus_kobj = &priv->subsys.kobj; 8693465e2e4SGreg Kroah-Hartman retval = kobject_set_name(bus_kobj, "%s", bus->name); 8701da177e4SLinus Torvalds if (retval) 8711da177e4SLinus Torvalds goto out; 8721da177e4SLinus Torvalds 8733465e2e4SGreg Kroah-Hartman bus_kobj->kset = bus_kset; 8743465e2e4SGreg Kroah-Hartman bus_kobj->ktype = &bus_ktype; 875c6f7e72aSGreg Kroah-Hartman priv->drivers_autoprobe = 1; 876d6b05b84SGreg Kroah-Hartman 877c6f7e72aSGreg Kroah-Hartman retval = kset_register(&priv->subsys); 8781da177e4SLinus Torvalds if (retval) 8791da177e4SLinus Torvalds goto out; 8801da177e4SLinus Torvalds 8817ac1cf4aSKay Sievers retval = bus_create_file(bus, &bus_attr_uevent); 8827ac1cf4aSKay Sievers if (retval) 8837ac1cf4aSKay Sievers goto bus_uevent_fail; 8847ac1cf4aSKay Sievers 8853465e2e4SGreg Kroah-Hartman priv->devices_kset = kset_create_and_add("devices", NULL, bus_kobj); 886c6f7e72aSGreg Kroah-Hartman if (!priv->devices_kset) { 8873d899596SGreg Kroah-Hartman retval = -ENOMEM; 8881da177e4SLinus Torvalds goto bus_devices_fail; 8893d899596SGreg Kroah-Hartman } 8901da177e4SLinus Torvalds 8913465e2e4SGreg Kroah-Hartman priv->drivers_kset = kset_create_and_add("drivers", NULL, bus_kobj); 892c6f7e72aSGreg Kroah-Hartman if (!priv->drivers_kset) { 8936dcec251SGreg Kroah-Hartman retval = -ENOMEM; 8941da177e4SLinus Torvalds goto bus_drivers_fail; 8956dcec251SGreg Kroah-Hartman } 896465c7a3aSmochel@digitalimplant.org 897ca22e56dSKay Sievers INIT_LIST_HEAD(&priv->interfaces); 89837e98d9bSGreg Kroah-Hartman key = &priv->lock_key; 89937e98d9bSGreg Kroah-Hartman lockdep_register_key(key); 900ca22e56dSKay Sievers __mutex_init(&priv->mutex, "subsys mutex", key); 901c6f7e72aSGreg Kroah-Hartman klist_init(&priv->klist_devices, klist_devices_get, klist_devices_put); 902c6f7e72aSGreg Kroah-Hartman klist_init(&priv->klist_drivers, NULL, NULL); 903b8c5cec2SKay Sievers 904b8c5cec2SKay Sievers retval = add_probe_files(bus); 905b8c5cec2SKay Sievers if (retval) 906b8c5cec2SKay Sievers goto bus_probe_files_fail; 907b8c5cec2SKay Sievers 9083465e2e4SGreg Kroah-Hartman retval = sysfs_create_groups(bus_kobj, bus->bus_groups); 90912478ba0SGreg Kroah-Hartman if (retval) 91012478ba0SGreg Kroah-Hartman goto bus_groups_fail; 9111da177e4SLinus Torvalds 9127dc72b28SGreg Kroah-Hartman pr_debug("bus: '%s': registered\n", bus->name); 9131da177e4SLinus Torvalds return 0; 9141da177e4SLinus Torvalds 91512478ba0SGreg Kroah-Hartman bus_groups_fail: 916b8c5cec2SKay Sievers remove_probe_files(bus); 917b8c5cec2SKay Sievers bus_probe_files_fail: 9183465e2e4SGreg Kroah-Hartman kset_unregister(priv->drivers_kset); 9191da177e4SLinus Torvalds bus_drivers_fail: 9203465e2e4SGreg Kroah-Hartman kset_unregister(priv->devices_kset); 9211da177e4SLinus Torvalds bus_devices_fail: 9227ac1cf4aSKay Sievers bus_remove_file(bus, &bus_attr_uevent); 9237ac1cf4aSKay Sievers bus_uevent_fail: 9243465e2e4SGreg Kroah-Hartman kset_unregister(&priv->subsys); 925bfa54a79SZijun Hu /* Above kset_unregister() will kfree @priv */ 926bfa54a79SZijun Hu priv = NULL; 9271da177e4SLinus Torvalds out: 9283465e2e4SGreg Kroah-Hartman kfree(priv); 9291da177e4SLinus Torvalds return retval; 9301da177e4SLinus Torvalds } 931be871b7eSMichal Hocko EXPORT_SYMBOL_GPL(bus_register); 9321da177e4SLinus Torvalds 9331da177e4SLinus Torvalds /** 9341da177e4SLinus Torvalds * bus_unregister - remove a bus from the system 9351da177e4SLinus Torvalds * @bus: bus. 9361da177e4SLinus Torvalds * 9371da177e4SLinus Torvalds * Unregister the child subsystems and the bus itself. 938fc1ede58SGreg Kroah-Hartman * Finally, we call bus_put() to release the refcount 9391da177e4SLinus Torvalds */ 940ad8685d0SGreg Kroah-Hartman void bus_unregister(const struct bus_type *bus) 9411da177e4SLinus Torvalds { 9423465e2e4SGreg Kroah-Hartman struct subsys_private *sp = bus_to_subsys(bus); 9433465e2e4SGreg Kroah-Hartman struct kobject *bus_kobj; 9443465e2e4SGreg Kroah-Hartman 9453465e2e4SGreg Kroah-Hartman if (!sp) 9463465e2e4SGreg Kroah-Hartman return; 9473465e2e4SGreg Kroah-Hartman 9487dc72b28SGreg Kroah-Hartman pr_debug("bus: '%s': unregistering\n", bus->name); 9499cc61e5fSGreg Kroah-Hartman if (sp->dev_root) 9509cc61e5fSGreg Kroah-Hartman device_unregister(sp->dev_root); 9513465e2e4SGreg Kroah-Hartman 9523465e2e4SGreg Kroah-Hartman bus_kobj = &sp->subsys.kobj; 9533465e2e4SGreg Kroah-Hartman sysfs_remove_groups(bus_kobj, bus->bus_groups); 954b8c5cec2SKay Sievers remove_probe_files(bus); 9557ac1cf4aSKay Sievers bus_remove_file(bus, &bus_attr_uevent); 9563465e2e4SGreg Kroah-Hartman 9573465e2e4SGreg Kroah-Hartman kset_unregister(sp->drivers_kset); 9583465e2e4SGreg Kroah-Hartman kset_unregister(sp->devices_kset); 9593465e2e4SGreg Kroah-Hartman kset_unregister(&sp->subsys); 9603465e2e4SGreg Kroah-Hartman subsys_put(sp); 9611da177e4SLinus Torvalds } 9624a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_unregister); 9631da177e4SLinus Torvalds 964bc8b7931SGreg Kroah-Hartman int bus_register_notifier(const struct bus_type *bus, struct notifier_block *nb) 965116af378SBenjamin Herrenschmidt { 96632a8121aSGreg Kroah-Hartman struct subsys_private *sp = bus_to_subsys(bus); 96732a8121aSGreg Kroah-Hartman int retval; 96832a8121aSGreg Kroah-Hartman 96932a8121aSGreg Kroah-Hartman if (!sp) 97032a8121aSGreg Kroah-Hartman return -EINVAL; 97132a8121aSGreg Kroah-Hartman 97232a8121aSGreg Kroah-Hartman retval = blocking_notifier_chain_register(&sp->bus_notifier, nb); 97332a8121aSGreg Kroah-Hartman subsys_put(sp); 97432a8121aSGreg Kroah-Hartman return retval; 975116af378SBenjamin Herrenschmidt } 976116af378SBenjamin Herrenschmidt EXPORT_SYMBOL_GPL(bus_register_notifier); 977116af378SBenjamin Herrenschmidt 978bc8b7931SGreg Kroah-Hartman int bus_unregister_notifier(const struct bus_type *bus, struct notifier_block *nb) 979116af378SBenjamin Herrenschmidt { 98032a8121aSGreg Kroah-Hartman struct subsys_private *sp = bus_to_subsys(bus); 98132a8121aSGreg Kroah-Hartman int retval; 98232a8121aSGreg Kroah-Hartman 98332a8121aSGreg Kroah-Hartman if (!sp) 98432a8121aSGreg Kroah-Hartman return -EINVAL; 98532a8121aSGreg Kroah-Hartman retval = blocking_notifier_chain_unregister(&sp->bus_notifier, nb); 98632a8121aSGreg Kroah-Hartman subsys_put(sp); 98732a8121aSGreg Kroah-Hartman return retval; 988116af378SBenjamin Herrenschmidt } 989116af378SBenjamin Herrenschmidt EXPORT_SYMBOL_GPL(bus_unregister_notifier); 990116af378SBenjamin Herrenschmidt 991ed9f9181SGreg Kroah-Hartman void bus_notify(struct device *dev, enum bus_notifier_event value) 992ed9f9181SGreg Kroah-Hartman { 99332a8121aSGreg Kroah-Hartman struct subsys_private *sp = bus_to_subsys(dev->bus); 994ed9f9181SGreg Kroah-Hartman 99532a8121aSGreg Kroah-Hartman if (!sp) 99632a8121aSGreg Kroah-Hartman return; 99732a8121aSGreg Kroah-Hartman 99832a8121aSGreg Kroah-Hartman blocking_notifier_call_chain(&sp->bus_notifier, value, dev); 99932a8121aSGreg Kroah-Hartman subsys_put(sp); 1000ed9f9181SGreg Kroah-Hartman } 1001ed9f9181SGreg Kroah-Hartman 1002f91482beSGreg Kroah-Hartman struct kset *bus_get_kset(const struct bus_type *bus) 10030fed80f7SGreg Kroah-Hartman { 1004beea7892SGreg Kroah-Hartman struct subsys_private *sp = bus_to_subsys(bus); 1005beea7892SGreg Kroah-Hartman struct kset *kset; 1006beea7892SGreg Kroah-Hartman 1007beea7892SGreg Kroah-Hartman if (!sp) 1008beea7892SGreg Kroah-Hartman return NULL; 1009beea7892SGreg Kroah-Hartman 1010beea7892SGreg Kroah-Hartman kset = &sp->subsys; 1011beea7892SGreg Kroah-Hartman subsys_put(sp); 1012beea7892SGreg Kroah-Hartman 1013beea7892SGreg Kroah-Hartman return kset; 10140fed80f7SGreg Kroah-Hartman } 10150fed80f7SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_get_kset); 10160fed80f7SGreg Kroah-Hartman 101799178b03SGreg Kroah-Hartman /* 1018dca25ebdSRobert P. J. Day * Yes, this forcibly breaks the klist abstraction temporarily. It 101999178b03SGreg Kroah-Hartman * just wants to sort the klist, not change reference counts and 102099178b03SGreg Kroah-Hartman * take/drop locks rapidly in the process. It does all this while 102199178b03SGreg Kroah-Hartman * holding the lock for the list, so objects can't otherwise be 102299178b03SGreg Kroah-Hartman * added/removed while we're swizzling. 102399178b03SGreg Kroah-Hartman */ 102499178b03SGreg Kroah-Hartman static void device_insertion_sort_klist(struct device *a, struct list_head *list, 102599178b03SGreg Kroah-Hartman int (*compare)(const struct device *a, 102699178b03SGreg Kroah-Hartman const struct device *b)) 102799178b03SGreg Kroah-Hartman { 102899178b03SGreg Kroah-Hartman struct klist_node *n; 1029ae1b4171SGreg Kroah-Hartman struct device_private *dev_prv; 103099178b03SGreg Kroah-Hartman struct device *b; 103199178b03SGreg Kroah-Hartman 10324c62785eSGeliang Tang list_for_each_entry(n, list, n_node) { 1033ae1b4171SGreg Kroah-Hartman dev_prv = to_device_private_bus(n); 1034ae1b4171SGreg Kroah-Hartman b = dev_prv->device; 103599178b03SGreg Kroah-Hartman if (compare(a, b) <= 0) { 1036ae1b4171SGreg Kroah-Hartman list_move_tail(&a->p->knode_bus.n_node, 1037ae1b4171SGreg Kroah-Hartman &b->p->knode_bus.n_node); 103899178b03SGreg Kroah-Hartman return; 103999178b03SGreg Kroah-Hartman } 104099178b03SGreg Kroah-Hartman } 1041ae1b4171SGreg Kroah-Hartman list_move_tail(&a->p->knode_bus.n_node, list); 104299178b03SGreg Kroah-Hartman } 104399178b03SGreg Kroah-Hartman 10445ae81209SGreg Kroah-Hartman void bus_sort_breadthfirst(const struct bus_type *bus, 104599178b03SGreg Kroah-Hartman int (*compare)(const struct device *a, 104699178b03SGreg Kroah-Hartman const struct device *b)) 104799178b03SGreg Kroah-Hartman { 1048b5aaecb8SGreg Kroah-Hartman struct subsys_private *sp = bus_to_subsys(bus); 104999178b03SGreg Kroah-Hartman LIST_HEAD(sorted_devices); 10504c62785eSGeliang Tang struct klist_node *n, *tmp; 1051ae1b4171SGreg Kroah-Hartman struct device_private *dev_prv; 105299178b03SGreg Kroah-Hartman struct device *dev; 105399178b03SGreg Kroah-Hartman struct klist *device_klist; 105499178b03SGreg Kroah-Hartman 1055b5aaecb8SGreg Kroah-Hartman if (!sp) 1056b5aaecb8SGreg Kroah-Hartman return; 1057b5aaecb8SGreg Kroah-Hartman device_klist = &sp->klist_devices; 105899178b03SGreg Kroah-Hartman 105999178b03SGreg Kroah-Hartman spin_lock(&device_klist->k_lock); 10604c62785eSGeliang Tang list_for_each_entry_safe(n, tmp, &device_klist->k_list, n_node) { 1061ae1b4171SGreg Kroah-Hartman dev_prv = to_device_private_bus(n); 1062ae1b4171SGreg Kroah-Hartman dev = dev_prv->device; 106399178b03SGreg Kroah-Hartman device_insertion_sort_klist(dev, &sorted_devices, compare); 106499178b03SGreg Kroah-Hartman } 106599178b03SGreg Kroah-Hartman list_splice(&sorted_devices, &device_klist->k_list); 106699178b03SGreg Kroah-Hartman spin_unlock(&device_klist->k_lock); 1067b5aaecb8SGreg Kroah-Hartman subsys_put(sp); 106899178b03SGreg Kroah-Hartman } 106999178b03SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_sort_breadthfirst); 107099178b03SGreg Kroah-Hartman 1071b0a8a59aSGreg Kroah-Hartman struct subsys_dev_iter { 1072b0a8a59aSGreg Kroah-Hartman struct klist_iter ki; 1073b0a8a59aSGreg Kroah-Hartman const struct device_type *type; 1074b0a8a59aSGreg Kroah-Hartman }; 1075b0a8a59aSGreg Kroah-Hartman 1076ca22e56dSKay Sievers /** 1077ca22e56dSKay Sievers * subsys_dev_iter_init - initialize subsys device iterator 1078ca22e56dSKay Sievers * @iter: subsys iterator to initialize 1079adac0375SGreg Kroah-Hartman * @sp: the subsys private (i.e. bus) we wanna iterate over 1080ca22e56dSKay Sievers * @start: the device to start iterating from, if any 1081ca22e56dSKay Sievers * @type: device_type of the devices to iterate over, NULL for all 1082ca22e56dSKay Sievers * 1083ca22e56dSKay Sievers * Initialize subsys iterator @iter such that it iterates over devices 1084ca22e56dSKay Sievers * of @subsys. If @start is set, the list iteration will start there, 1085ca22e56dSKay Sievers * otherwise if it is NULL, the iteration starts at the beginning of 1086ca22e56dSKay Sievers * the list. 1087ca22e56dSKay Sievers */ 1088adac0375SGreg Kroah-Hartman static void subsys_dev_iter_init(struct subsys_dev_iter *iter, struct subsys_private *sp, 1089ca22e56dSKay Sievers struct device *start, const struct device_type *type) 1090ca22e56dSKay Sievers { 1091ca22e56dSKay Sievers struct klist_node *start_knode = NULL; 1092ca22e56dSKay Sievers 1093ca22e56dSKay Sievers if (start) 1094ca22e56dSKay Sievers start_knode = &start->p->knode_bus; 1095adac0375SGreg Kroah-Hartman klist_iter_init_node(&sp->klist_devices, &iter->ki, start_knode); 1096ca22e56dSKay Sievers iter->type = type; 1097ca22e56dSKay Sievers } 1098ca22e56dSKay Sievers 1099ca22e56dSKay Sievers /** 1100ca22e56dSKay Sievers * subsys_dev_iter_next - iterate to the next device 1101ca22e56dSKay Sievers * @iter: subsys iterator to proceed 1102ca22e56dSKay Sievers * 1103ca22e56dSKay Sievers * Proceed @iter to the next device and return it. Returns NULL if 1104ca22e56dSKay Sievers * iteration is complete. 1105ca22e56dSKay Sievers * 1106ca22e56dSKay Sievers * The returned device is referenced and won't be released till 1107ca22e56dSKay Sievers * iterator is proceed to the next device or exited. The caller is 1108ca22e56dSKay Sievers * free to do whatever it wants to do with the device including 1109ca22e56dSKay Sievers * calling back into subsys code. 1110ca22e56dSKay Sievers */ 111138cdadefSGreg Kroah-Hartman static struct device *subsys_dev_iter_next(struct subsys_dev_iter *iter) 1112ca22e56dSKay Sievers { 1113ca22e56dSKay Sievers struct klist_node *knode; 1114ca22e56dSKay Sievers struct device *dev; 1115ca22e56dSKay Sievers 1116ca22e56dSKay Sievers for (;;) { 1117ca22e56dSKay Sievers knode = klist_next(&iter->ki); 1118ca22e56dSKay Sievers if (!knode) 1119ca22e56dSKay Sievers return NULL; 1120371fd7a2SGeliang Tang dev = to_device_private_bus(knode)->device; 1121ca22e56dSKay Sievers if (!iter->type || iter->type == dev->type) 1122ca22e56dSKay Sievers return dev; 1123ca22e56dSKay Sievers } 1124ca22e56dSKay Sievers } 1125ca22e56dSKay Sievers 1126ca22e56dSKay Sievers /** 1127ca22e56dSKay Sievers * subsys_dev_iter_exit - finish iteration 1128ca22e56dSKay Sievers * @iter: subsys iterator to finish 1129ca22e56dSKay Sievers * 1130ca22e56dSKay Sievers * Finish an iteration. Always call this function after iteration is 1131ca22e56dSKay Sievers * complete whether the iteration ran till the end or not. 1132ca22e56dSKay Sievers */ 1133af6d0743SGreg Kroah-Hartman static void subsys_dev_iter_exit(struct subsys_dev_iter *iter) 1134ca22e56dSKay Sievers { 1135ca22e56dSKay Sievers klist_iter_exit(&iter->ki); 1136ca22e56dSKay Sievers } 1137ca22e56dSKay Sievers 1138ca22e56dSKay Sievers int subsys_interface_register(struct subsys_interface *sif) 1139ca22e56dSKay Sievers { 1140adac0375SGreg Kroah-Hartman struct subsys_private *sp; 1141ca22e56dSKay Sievers struct subsys_dev_iter iter; 1142ca22e56dSKay Sievers struct device *dev; 1143ca22e56dSKay Sievers 1144ca22e56dSKay Sievers if (!sif || !sif->subsys) 1145ca22e56dSKay Sievers return -ENODEV; 1146ca22e56dSKay Sievers 1147adac0375SGreg Kroah-Hartman sp = bus_to_subsys(sif->subsys); 1148adac0375SGreg Kroah-Hartman if (!sp) 1149ca22e56dSKay Sievers return -EINVAL; 1150ca22e56dSKay Sievers 1151adac0375SGreg Kroah-Hartman /* 1152adac0375SGreg Kroah-Hartman * Reference in sp is now incremented and will be dropped when 1153adac0375SGreg Kroah-Hartman * the interface is removed from the bus 1154adac0375SGreg Kroah-Hartman */ 1155adac0375SGreg Kroah-Hartman 1156adac0375SGreg Kroah-Hartman mutex_lock(&sp->mutex); 1157adac0375SGreg Kroah-Hartman list_add_tail(&sif->node, &sp->interfaces); 1158ca22e56dSKay Sievers if (sif->add_dev) { 1159adac0375SGreg Kroah-Hartman subsys_dev_iter_init(&iter, sp, NULL, NULL); 1160ca22e56dSKay Sievers while ((dev = subsys_dev_iter_next(&iter))) 1161ca22e56dSKay Sievers sif->add_dev(dev, sif); 1162ca22e56dSKay Sievers subsys_dev_iter_exit(&iter); 1163ca22e56dSKay Sievers } 1164adac0375SGreg Kroah-Hartman mutex_unlock(&sp->mutex); 1165ca22e56dSKay Sievers 1166ca22e56dSKay Sievers return 0; 1167ca22e56dSKay Sievers } 1168ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_interface_register); 1169ca22e56dSKay Sievers 1170ca22e56dSKay Sievers void subsys_interface_unregister(struct subsys_interface *sif) 1171ca22e56dSKay Sievers { 1172adac0375SGreg Kroah-Hartman struct subsys_private *sp; 1173ca22e56dSKay Sievers struct subsys_dev_iter iter; 1174ca22e56dSKay Sievers struct device *dev; 1175ca22e56dSKay Sievers 11762b31594aSJonghwan Choi if (!sif || !sif->subsys) 1177ca22e56dSKay Sievers return; 1178ca22e56dSKay Sievers 1179adac0375SGreg Kroah-Hartman sp = bus_to_subsys(sif->subsys); 1180adac0375SGreg Kroah-Hartman if (!sp) 1181adac0375SGreg Kroah-Hartman return; 11822b31594aSJonghwan Choi 1183adac0375SGreg Kroah-Hartman mutex_lock(&sp->mutex); 1184ca22e56dSKay Sievers list_del_init(&sif->node); 1185ca22e56dSKay Sievers if (sif->remove_dev) { 1186adac0375SGreg Kroah-Hartman subsys_dev_iter_init(&iter, sp, NULL, NULL); 1187ca22e56dSKay Sievers while ((dev = subsys_dev_iter_next(&iter))) 1188ca22e56dSKay Sievers sif->remove_dev(dev, sif); 1189ca22e56dSKay Sievers subsys_dev_iter_exit(&iter); 1190ca22e56dSKay Sievers } 1191adac0375SGreg Kroah-Hartman mutex_unlock(&sp->mutex); 1192ca22e56dSKay Sievers 1193adac0375SGreg Kroah-Hartman /* 1194adac0375SGreg Kroah-Hartman * Decrement the reference count twice, once for the bus_to_subsys() 1195adac0375SGreg Kroah-Hartman * call in the start of this function, and the second one from the 1196adac0375SGreg Kroah-Hartman * reference increment in subsys_interface_register() 1197adac0375SGreg Kroah-Hartman */ 1198adac0375SGreg Kroah-Hartman subsys_put(sp); 1199adac0375SGreg Kroah-Hartman subsys_put(sp); 1200ca22e56dSKay Sievers } 1201ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_interface_unregister); 1202ca22e56dSKay Sievers 1203ca22e56dSKay Sievers static void system_root_device_release(struct device *dev) 1204ca22e56dSKay Sievers { 1205ca22e56dSKay Sievers kfree(dev); 1206ca22e56dSKay Sievers } 1207d73ce004STejun Heo 120832f78abeSGreg Kroah-Hartman static int subsys_register(const struct bus_type *subsys, 1209d73ce004STejun Heo const struct attribute_group **groups, 1210d73ce004STejun Heo struct kobject *parent_of_root) 1211d73ce004STejun Heo { 12129cc61e5fSGreg Kroah-Hartman struct subsys_private *sp; 1213d73ce004STejun Heo struct device *dev; 1214d73ce004STejun Heo int err; 1215d73ce004STejun Heo 1216d73ce004STejun Heo err = bus_register(subsys); 1217d73ce004STejun Heo if (err < 0) 1218d73ce004STejun Heo return err; 1219d73ce004STejun Heo 12209cc61e5fSGreg Kroah-Hartman sp = bus_to_subsys(subsys); 12219cc61e5fSGreg Kroah-Hartman if (!sp) { 12229cc61e5fSGreg Kroah-Hartman err = -EINVAL; 12239cc61e5fSGreg Kroah-Hartman goto err_sp; 12249cc61e5fSGreg Kroah-Hartman } 12259cc61e5fSGreg Kroah-Hartman 1226d73ce004STejun Heo dev = kzalloc(sizeof(struct device), GFP_KERNEL); 1227d73ce004STejun Heo if (!dev) { 1228d73ce004STejun Heo err = -ENOMEM; 1229d73ce004STejun Heo goto err_dev; 1230d73ce004STejun Heo } 1231d73ce004STejun Heo 1232d73ce004STejun Heo err = dev_set_name(dev, "%s", subsys->name); 1233d73ce004STejun Heo if (err < 0) 1234d73ce004STejun Heo goto err_name; 1235d73ce004STejun Heo 1236d73ce004STejun Heo dev->kobj.parent = parent_of_root; 1237d73ce004STejun Heo dev->groups = groups; 1238d73ce004STejun Heo dev->release = system_root_device_release; 1239d73ce004STejun Heo 1240d73ce004STejun Heo err = device_register(dev); 1241d73ce004STejun Heo if (err < 0) 1242d73ce004STejun Heo goto err_dev_reg; 1243d73ce004STejun Heo 12449cc61e5fSGreg Kroah-Hartman sp->dev_root = dev; 12459cc61e5fSGreg Kroah-Hartman subsys_put(sp); 1246d73ce004STejun Heo return 0; 1247d73ce004STejun Heo 1248d73ce004STejun Heo err_dev_reg: 1249d73ce004STejun Heo put_device(dev); 1250d73ce004STejun Heo dev = NULL; 1251d73ce004STejun Heo err_name: 1252d73ce004STejun Heo kfree(dev); 1253d73ce004STejun Heo err_dev: 12549cc61e5fSGreg Kroah-Hartman subsys_put(sp); 12559cc61e5fSGreg Kroah-Hartman err_sp: 1256d73ce004STejun Heo bus_unregister(subsys); 1257d73ce004STejun Heo return err; 1258d73ce004STejun Heo } 1259d73ce004STejun Heo 1260ca22e56dSKay Sievers /** 1261ca22e56dSKay Sievers * subsys_system_register - register a subsystem at /sys/devices/system/ 126278d79559SRandy Dunlap * @subsys: system subsystem 126378d79559SRandy Dunlap * @groups: default attributes for the root device 1264ca22e56dSKay Sievers * 1265ca22e56dSKay Sievers * All 'system' subsystems have a /sys/devices/system/<name> root device 1266ca22e56dSKay Sievers * with the name of the subsystem. The root device can carry subsystem- 1267ca22e56dSKay Sievers * wide attributes. All registered devices are below this single root 1268ca22e56dSKay Sievers * device and are named after the subsystem with a simple enumeration 1269e227867fSMasanari Iida * number appended. The registered devices are not explicitly named; 1270ca22e56dSKay Sievers * only 'id' in the device needs to be set. 1271ca22e56dSKay Sievers * 1272ca22e56dSKay Sievers * Do not use this interface for anything new, it exists for compatibility 1273ca22e56dSKay Sievers * with bad ideas only. New subsystems should use plain subsystems; and 1274ca22e56dSKay Sievers * add the subsystem-wide attributes should be added to the subsystem 1275ca22e56dSKay Sievers * directory itself and not some create fake root-device placed in 1276ca22e56dSKay Sievers * /sys/devices/system/<name>. 1277ca22e56dSKay Sievers */ 127832f78abeSGreg Kroah-Hartman int subsys_system_register(const struct bus_type *subsys, 1279ca22e56dSKay Sievers const struct attribute_group **groups) 1280ca22e56dSKay Sievers { 1281d73ce004STejun Heo return subsys_register(subsys, groups, &system_kset->kobj); 1282ca22e56dSKay Sievers } 1283ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_system_register); 1284ca22e56dSKay Sievers 1285d73ce004STejun Heo /** 1286d73ce004STejun Heo * subsys_virtual_register - register a subsystem at /sys/devices/virtual/ 1287d73ce004STejun Heo * @subsys: virtual subsystem 1288d73ce004STejun Heo * @groups: default attributes for the root device 1289d73ce004STejun Heo * 1290d73ce004STejun Heo * All 'virtual' subsystems have a /sys/devices/system/<name> root device 1291d73ce004STejun Heo * with the name of the subystem. The root device can carry subsystem-wide 1292d73ce004STejun Heo * attributes. All registered devices are below this single root device. 1293d73ce004STejun Heo * There's no restriction on device naming. This is for kernel software 1294d73ce004STejun Heo * constructs which need sysfs interface. 1295d73ce004STejun Heo */ 129632f78abeSGreg Kroah-Hartman int subsys_virtual_register(const struct bus_type *subsys, 1297d73ce004STejun Heo const struct attribute_group **groups) 1298d73ce004STejun Heo { 1299d73ce004STejun Heo struct kobject *virtual_dir; 1300d73ce004STejun Heo 13010314647dSZijun Hu virtual_dir = virtual_device_parent(); 1302d73ce004STejun Heo if (!virtual_dir) 1303d73ce004STejun Heo return -ENOMEM; 1304d73ce004STejun Heo 1305d73ce004STejun Heo return subsys_register(subsys, groups, virtual_dir); 1306d73ce004STejun Heo } 13071c04fc35SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(subsys_virtual_register); 1308d73ce004STejun Heo 1309adc18506SGreg Kroah-Hartman /** 1310adc18506SGreg Kroah-Hartman * driver_find - locate driver on a bus by its name. 1311adc18506SGreg Kroah-Hartman * @name: name of the driver. 1312adc18506SGreg Kroah-Hartman * @bus: bus to scan for the driver. 1313adc18506SGreg Kroah-Hartman * 1314adc18506SGreg Kroah-Hartman * Call kset_find_obj() to iterate over list of drivers on 1315adc18506SGreg Kroah-Hartman * a bus to find driver by name. Return driver if found. 1316adc18506SGreg Kroah-Hartman * 1317adc18506SGreg Kroah-Hartman * This routine provides no locking to prevent the driver it returns 1318adc18506SGreg Kroah-Hartman * from being unregistered or unloaded while the caller is using it. 1319adc18506SGreg Kroah-Hartman * The caller is responsible for preventing this. 1320adc18506SGreg Kroah-Hartman */ 13217c06be04SGreg Kroah-Hartman struct device_driver *driver_find(const char *name, const struct bus_type *bus) 1322adc18506SGreg Kroah-Hartman { 1323fb451966SGreg Kroah-Hartman struct subsys_private *sp = bus_to_subsys(bus); 1324fb451966SGreg Kroah-Hartman struct kobject *k; 1325adc18506SGreg Kroah-Hartman struct driver_private *priv; 1326adc18506SGreg Kroah-Hartman 1327fb451966SGreg Kroah-Hartman if (!sp) 1328fb451966SGreg Kroah-Hartman return NULL; 1329fb451966SGreg Kroah-Hartman 1330fb451966SGreg Kroah-Hartman k = kset_find_obj(sp->drivers_kset, name); 1331fb451966SGreg Kroah-Hartman subsys_put(sp); 1332fb451966SGreg Kroah-Hartman if (!k) 1333fb451966SGreg Kroah-Hartman return NULL; 1334fb451966SGreg Kroah-Hartman 1335fb451966SGreg Kroah-Hartman priv = to_driver(k); 1336fb451966SGreg Kroah-Hartman 1337adc18506SGreg Kroah-Hartman /* Drop reference added by kset_find_obj() */ 1338adc18506SGreg Kroah-Hartman kobject_put(k); 1339adc18506SGreg Kroah-Hartman return priv->driver; 1340adc18506SGreg Kroah-Hartman } 1341adc18506SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(driver_find); 1342adc18506SGreg Kroah-Hartman 134363b823d7SGreg Kroah-Hartman /* 134463b823d7SGreg Kroah-Hartman * Warning, the value could go to "removed" instantly after calling this function, so be very 134563b823d7SGreg Kroah-Hartman * careful when calling it... 134663b823d7SGreg Kroah-Hartman */ 134763b823d7SGreg Kroah-Hartman bool bus_is_registered(const struct bus_type *bus) 134863b823d7SGreg Kroah-Hartman { 134963b823d7SGreg Kroah-Hartman struct subsys_private *sp = bus_to_subsys(bus); 135063b823d7SGreg Kroah-Hartman bool is_initialized = false; 135163b823d7SGreg Kroah-Hartman 135263b823d7SGreg Kroah-Hartman if (sp) { 135363b823d7SGreg Kroah-Hartman is_initialized = true; 135463b823d7SGreg Kroah-Hartman subsys_put(sp); 135563b823d7SGreg Kroah-Hartman } 135663b823d7SGreg Kroah-Hartman return is_initialized; 135763b823d7SGreg Kroah-Hartman } 135863b823d7SGreg Kroah-Hartman 13598c99377eSGreg Kroah-Hartman /** 13608c99377eSGreg Kroah-Hartman * bus_get_dev_root - return a pointer to the "device root" of a bus 13618c99377eSGreg Kroah-Hartman * @bus: bus to return the device root of. 13628c99377eSGreg Kroah-Hartman * 13638c99377eSGreg Kroah-Hartman * If a bus has a "device root" structure, return it, WITH THE REFERENCE 13648c99377eSGreg Kroah-Hartman * COUNT INCREMENTED. 13658c99377eSGreg Kroah-Hartman * 13668c99377eSGreg Kroah-Hartman * Note, when finished with the device, a call to put_device() is required. 13678c99377eSGreg Kroah-Hartman * 13688c99377eSGreg Kroah-Hartman * If the device root is not present (or bus is not a valid pointer), NULL 13698c99377eSGreg Kroah-Hartman * will be returned. 13708c99377eSGreg Kroah-Hartman */ 13718c99377eSGreg Kroah-Hartman struct device *bus_get_dev_root(const struct bus_type *bus) 13728c99377eSGreg Kroah-Hartman { 13739cc61e5fSGreg Kroah-Hartman struct subsys_private *sp = bus_to_subsys(bus); 13749cc61e5fSGreg Kroah-Hartman struct device *dev_root; 13759cc61e5fSGreg Kroah-Hartman 13769cc61e5fSGreg Kroah-Hartman if (!sp) 13778c99377eSGreg Kroah-Hartman return NULL; 13789cc61e5fSGreg Kroah-Hartman 13799cc61e5fSGreg Kroah-Hartman dev_root = get_device(sp->dev_root); 13809cc61e5fSGreg Kroah-Hartman subsys_put(sp); 13819cc61e5fSGreg Kroah-Hartman return dev_root; 13828c99377eSGreg Kroah-Hartman } 13838c99377eSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_get_dev_root); 13848c99377eSGreg Kroah-Hartman 13851da177e4SLinus Torvalds int __init buses_init(void) 13861da177e4SLinus Torvalds { 138759a54833SGreg Kroah-Hartman bus_kset = kset_create_and_add("bus", &bus_uevent_ops, NULL); 138859a54833SGreg Kroah-Hartman if (!bus_kset) 138959a54833SGreg Kroah-Hartman return -ENOMEM; 1390ca22e56dSKay Sievers 1391ca22e56dSKay Sievers system_kset = kset_create_and_add("system", NULL, &devices_kset->kobj); 13922bdf3b83SZijun Hu if (!system_kset) { 13932bdf3b83SZijun Hu /* Do error handling here as devices_init() do */ 13942bdf3b83SZijun Hu kset_unregister(bus_kset); 13952bdf3b83SZijun Hu bus_kset = NULL; 13962bdf3b83SZijun Hu pr_err("%s: failed to create and add kset 'bus'\n", __func__); 1397ca22e56dSKay Sievers return -ENOMEM; 13982bdf3b83SZijun Hu } 1399ca22e56dSKay Sievers 140059a54833SGreg Kroah-Hartman return 0; 14011da177e4SLinus Torvalds } 1402