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. 91da177e4SLinus Torvalds */ 101da177e4SLinus Torvalds 11765230b5SDmitry Torokhov #include <linux/async.h> 125aee2bf2SGreg Kroah-Hartman #include <linux/device/bus.h> 131da177e4SLinus Torvalds #include <linux/device.h> 141da177e4SLinus Torvalds #include <linux/module.h> 151da177e4SLinus Torvalds #include <linux/errno.h> 165a0e3ad6STejun Heo #include <linux/slab.h> 171da177e4SLinus Torvalds #include <linux/init.h> 181da177e4SLinus Torvalds #include <linux/string.h> 19ca22e56dSKay Sievers #include <linux/mutex.h> 2063967685SGreg Kroah-Hartman #include <linux/sysfs.h> 211da177e4SLinus Torvalds #include "base.h" 221da177e4SLinus Torvalds #include "power/power.h" 231da177e4SLinus Torvalds 24ca22e56dSKay Sievers /* /sys/devices/system */ 2597ec448aSH Hartley Sweeten static struct kset *system_kset; 26ca22e56dSKay Sievers 27273afac6SGreg Kroah-Hartman /* /sys/bus */ 28273afac6SGreg Kroah-Hartman static struct kset *bus_kset; 29273afac6SGreg Kroah-Hartman 301da177e4SLinus Torvalds #define to_bus_attr(_attr) container_of(_attr, struct bus_attribute, attr) 311da177e4SLinus Torvalds 321da177e4SLinus Torvalds /* 331da177e4SLinus Torvalds * sysfs bindings for drivers 341da177e4SLinus Torvalds */ 351da177e4SLinus Torvalds 361da177e4SLinus Torvalds #define to_drv_attr(_attr) container_of(_attr, struct driver_attribute, attr) 371da177e4SLinus Torvalds 384f4b3743SDaniel Vetter #define DRIVER_ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) \ 394f4b3743SDaniel Vetter struct driver_attribute driver_attr_##_name = \ 404f4b3743SDaniel Vetter __ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) 411da177e4SLinus Torvalds 42b8c5cec2SKay Sievers static int __must_check bus_rescan_devices_helper(struct device *dev, 43b8c5cec2SKay Sievers void *data); 44b8c5cec2SKay Sievers 45273afac6SGreg Kroah-Hartman /** 46273afac6SGreg Kroah-Hartman * bus_to_subsys - Turn a struct bus_type into a struct subsys_private 47273afac6SGreg Kroah-Hartman * 48273afac6SGreg Kroah-Hartman * @bus: pointer to the struct bus_type to look up 49273afac6SGreg Kroah-Hartman * 50273afac6SGreg Kroah-Hartman * The driver core internals needs to work on the subsys_private structure, not 51273afac6SGreg Kroah-Hartman * the external struct bus_type pointer. This function walks the list of 52273afac6SGreg Kroah-Hartman * registered busses in the system and finds the matching one and returns the 53273afac6SGreg Kroah-Hartman * internal struct subsys_private that relates to that bus. 54273afac6SGreg Kroah-Hartman * 55273afac6SGreg Kroah-Hartman * Note, the reference count of the return value is INCREMENTED if it is not 56273afac6SGreg Kroah-Hartman * NULL. A call to subsys_put() must be done when finished with the pointer in 57273afac6SGreg Kroah-Hartman * order for it to be properly freed. 58273afac6SGreg Kroah-Hartman */ 59273afac6SGreg Kroah-Hartman static struct subsys_private *bus_to_subsys(const struct bus_type *bus) 60273afac6SGreg Kroah-Hartman { 61273afac6SGreg Kroah-Hartman struct subsys_private *sp = NULL; 62273afac6SGreg Kroah-Hartman struct kobject *kobj; 63273afac6SGreg Kroah-Hartman 64273afac6SGreg Kroah-Hartman if (!bus) 65273afac6SGreg Kroah-Hartman return NULL; 66273afac6SGreg Kroah-Hartman 67273afac6SGreg Kroah-Hartman spin_lock(&bus_kset->list_lock); 68273afac6SGreg Kroah-Hartman 69273afac6SGreg Kroah-Hartman if (list_empty(&bus_kset->list)) 70273afac6SGreg Kroah-Hartman goto done; 71273afac6SGreg Kroah-Hartman 72273afac6SGreg Kroah-Hartman list_for_each_entry(kobj, &bus_kset->list, entry) { 73273afac6SGreg Kroah-Hartman struct kset *kset = container_of(kobj, struct kset, kobj); 74273afac6SGreg Kroah-Hartman 75273afac6SGreg Kroah-Hartman sp = container_of_const(kset, struct subsys_private, subsys); 76273afac6SGreg Kroah-Hartman if (sp->bus == bus) 77273afac6SGreg Kroah-Hartman goto done; 78273afac6SGreg Kroah-Hartman } 79273afac6SGreg Kroah-Hartman sp = NULL; 80273afac6SGreg Kroah-Hartman done: 81273afac6SGreg Kroah-Hartman sp = subsys_get(sp); 82273afac6SGreg Kroah-Hartman spin_unlock(&bus_kset->list_lock); 83273afac6SGreg Kroah-Hartman return sp; 84273afac6SGreg Kroah-Hartman } 85273afac6SGreg Kroah-Hartman 865901d014SGreg Kroah-Hartman static struct bus_type *bus_get(struct bus_type *bus) 875901d014SGreg Kroah-Hartman { 88273afac6SGreg Kroah-Hartman struct subsys_private *sp = bus_to_subsys(bus); 89273afac6SGreg Kroah-Hartman 90273afac6SGreg Kroah-Hartman if (sp) 91c6f7e72aSGreg Kroah-Hartman return bus; 92c6f7e72aSGreg Kroah-Hartman return NULL; 935901d014SGreg Kroah-Hartman } 945901d014SGreg Kroah-Hartman 95273afac6SGreg Kroah-Hartman static void bus_put(const struct bus_type *bus) 96fc1ede58SGreg Kroah-Hartman { 97273afac6SGreg Kroah-Hartman struct subsys_private *sp = bus_to_subsys(bus); 98273afac6SGreg Kroah-Hartman 99273afac6SGreg Kroah-Hartman /* two puts are required as the call to bus_to_subsys incremented it again */ 100273afac6SGreg Kroah-Hartman subsys_put(sp); 101273afac6SGreg Kroah-Hartman subsys_put(sp); 102fc1ede58SGreg Kroah-Hartman } 103fc1ede58SGreg Kroah-Hartman 1044a3ad20cSGreg Kroah-Hartman static ssize_t drv_attr_show(struct kobject *kobj, struct attribute *attr, 1054a3ad20cSGreg Kroah-Hartman char *buf) 1061da177e4SLinus Torvalds { 1071da177e4SLinus Torvalds struct driver_attribute *drv_attr = to_drv_attr(attr); 108e5dd1278SGreg Kroah-Hartman struct driver_private *drv_priv = to_driver(kobj); 1094a0c20bfSDmitry Torokhov ssize_t ret = -EIO; 1101da177e4SLinus Torvalds 1111da177e4SLinus Torvalds if (drv_attr->show) 112e5dd1278SGreg Kroah-Hartman ret = drv_attr->show(drv_priv->driver, buf); 1131da177e4SLinus Torvalds return ret; 1141da177e4SLinus Torvalds } 1151da177e4SLinus Torvalds 1164a3ad20cSGreg Kroah-Hartman static ssize_t drv_attr_store(struct kobject *kobj, struct attribute *attr, 1171da177e4SLinus Torvalds const char *buf, size_t count) 1181da177e4SLinus Torvalds { 1191da177e4SLinus Torvalds struct driver_attribute *drv_attr = to_drv_attr(attr); 120e5dd1278SGreg Kroah-Hartman struct driver_private *drv_priv = to_driver(kobj); 1214a0c20bfSDmitry Torokhov ssize_t ret = -EIO; 1221da177e4SLinus Torvalds 1231da177e4SLinus Torvalds if (drv_attr->store) 124e5dd1278SGreg Kroah-Hartman ret = drv_attr->store(drv_priv->driver, buf, count); 1251da177e4SLinus Torvalds return ret; 1261da177e4SLinus Torvalds } 1271da177e4SLinus Torvalds 12852cf25d0SEmese Revfy static const struct sysfs_ops driver_sysfs_ops = { 1291da177e4SLinus Torvalds .show = drv_attr_show, 1301da177e4SLinus Torvalds .store = drv_attr_store, 1311da177e4SLinus Torvalds }; 1321da177e4SLinus Torvalds 1331da177e4SLinus Torvalds static void driver_release(struct kobject *kobj) 1341da177e4SLinus Torvalds { 135e5dd1278SGreg Kroah-Hartman struct driver_private *drv_priv = to_driver(kobj); 136e5dd1278SGreg Kroah-Hartman 1372b3a302aSHarvey Harrison pr_debug("driver: '%s': %s\n", kobject_name(kobj), __func__); 138e5dd1278SGreg Kroah-Hartman kfree(drv_priv); 1391da177e4SLinus Torvalds } 1401da177e4SLinus Torvalds 141c83d9ab4SThomas Weißschuh static const struct kobj_type driver_ktype = { 1421da177e4SLinus Torvalds .sysfs_ops = &driver_sysfs_ops, 1431da177e4SLinus Torvalds .release = driver_release, 1441da177e4SLinus Torvalds }; 1451da177e4SLinus Torvalds 1461da177e4SLinus Torvalds /* 1471da177e4SLinus Torvalds * sysfs bindings for buses 1481da177e4SLinus Torvalds */ 1494a3ad20cSGreg Kroah-Hartman static ssize_t bus_attr_show(struct kobject *kobj, struct attribute *attr, 1504a3ad20cSGreg Kroah-Hartman char *buf) 1511da177e4SLinus Torvalds { 1521da177e4SLinus Torvalds struct bus_attribute *bus_attr = to_bus_attr(attr); 1536b6e39a6SKay Sievers struct subsys_private *subsys_priv = to_subsys_private(kobj); 1541da177e4SLinus Torvalds ssize_t ret = 0; 1551da177e4SLinus Torvalds 1561da177e4SLinus Torvalds if (bus_attr->show) 1576b6e39a6SKay Sievers ret = bus_attr->show(subsys_priv->bus, buf); 1581da177e4SLinus Torvalds return ret; 1591da177e4SLinus Torvalds } 1601da177e4SLinus Torvalds 1614a3ad20cSGreg Kroah-Hartman static ssize_t bus_attr_store(struct kobject *kobj, struct attribute *attr, 1621da177e4SLinus Torvalds const char *buf, size_t count) 1631da177e4SLinus Torvalds { 1641da177e4SLinus Torvalds struct bus_attribute *bus_attr = to_bus_attr(attr); 1656b6e39a6SKay Sievers struct subsys_private *subsys_priv = to_subsys_private(kobj); 1661da177e4SLinus Torvalds ssize_t ret = 0; 1671da177e4SLinus Torvalds 1681da177e4SLinus Torvalds if (bus_attr->store) 1696b6e39a6SKay Sievers ret = bus_attr->store(subsys_priv->bus, buf, count); 1701da177e4SLinus Torvalds return ret; 1711da177e4SLinus Torvalds } 1721da177e4SLinus Torvalds 17352cf25d0SEmese Revfy static const struct sysfs_ops bus_sysfs_ops = { 1741da177e4SLinus Torvalds .show = bus_attr_show, 1751da177e4SLinus Torvalds .store = bus_attr_store, 1761da177e4SLinus Torvalds }; 1771da177e4SLinus Torvalds 1780396f286SGreg Kroah-Hartman int bus_create_file(const struct bus_type *bus, struct bus_attribute *attr) 1791da177e4SLinus Torvalds { 1800396f286SGreg Kroah-Hartman struct subsys_private *sp = bus_to_subsys(bus); 1811da177e4SLinus Torvalds int error; 1820396f286SGreg Kroah-Hartman 1830396f286SGreg Kroah-Hartman if (!sp) 1840396f286SGreg Kroah-Hartman return -EINVAL; 1850396f286SGreg Kroah-Hartman 1860396f286SGreg Kroah-Hartman error = sysfs_create_file(&sp->subsys.kobj, &attr->attr); 1870396f286SGreg Kroah-Hartman 1880396f286SGreg Kroah-Hartman subsys_put(sp); 1891da177e4SLinus Torvalds return error; 1901da177e4SLinus Torvalds } 1914a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_create_file); 1921da177e4SLinus Torvalds 1930396f286SGreg Kroah-Hartman void bus_remove_file(const struct bus_type *bus, struct bus_attribute *attr) 1941da177e4SLinus Torvalds { 1950396f286SGreg Kroah-Hartman struct subsys_private *sp = bus_to_subsys(bus); 1960396f286SGreg Kroah-Hartman 1970396f286SGreg Kroah-Hartman if (!sp) 1980396f286SGreg Kroah-Hartman return; 1990396f286SGreg Kroah-Hartman 2000396f286SGreg Kroah-Hartman sysfs_remove_file(&sp->subsys.kobj, &attr->attr); 2010396f286SGreg Kroah-Hartman subsys_put(sp); 2021da177e4SLinus Torvalds } 2034a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_remove_file); 2041da177e4SLinus Torvalds 205174be70bSBart Van Assche static void bus_release(struct kobject *kobj) 206174be70bSBart Van Assche { 207371fd7a2SGeliang Tang struct subsys_private *priv = to_subsys_private(kobj); 208174be70bSBart Van Assche 20937e98d9bSGreg Kroah-Hartman lockdep_unregister_key(&priv->lock_key); 210174be70bSBart Van Assche kfree(priv); 211174be70bSBart Van Assche } 212174be70bSBart Van Assche 213c83d9ab4SThomas Weißschuh static const struct kobj_type bus_ktype = { 2141da177e4SLinus Torvalds .sysfs_ops = &bus_sysfs_ops, 215174be70bSBart Van Assche .release = bus_release, 2161da177e4SLinus Torvalds }; 2171da177e4SLinus Torvalds 218c45a88bbSGreg Kroah-Hartman static int bus_uevent_filter(const struct kobject *kobj) 21980f03e34SKay Sievers { 220ee6d3dd4SWedson Almeida Filho const struct kobj_type *ktype = get_ktype(kobj); 22180f03e34SKay Sievers 22280f03e34SKay Sievers if (ktype == &bus_ktype) 22380f03e34SKay Sievers return 1; 22480f03e34SKay Sievers return 0; 22580f03e34SKay Sievers } 22680f03e34SKay Sievers 2279cd43611SEmese Revfy static const struct kset_uevent_ops bus_uevent_ops = { 22880f03e34SKay Sievers .filter = bus_uevent_filter, 22980f03e34SKay Sievers }; 23080f03e34SKay Sievers 2312b08c8d0SAlan Stern /* Manually detach a device from its associated driver. */ 2322581c9ccSGreg Kroah-Hartman static ssize_t unbind_store(struct device_driver *drv, const char *buf, 2332581c9ccSGreg Kroah-Hartman size_t count) 234151ef38fSGreg Kroah-Hartman { 2355901d014SGreg Kroah-Hartman struct bus_type *bus = bus_get(drv->bus); 236151ef38fSGreg Kroah-Hartman struct device *dev; 237151ef38fSGreg Kroah-Hartman int err = -ENODEV; 238151ef38fSGreg Kroah-Hartman 2391f9ffc04SGreg Kroah-Hartman dev = bus_find_device_by_name(bus, NULL, buf); 2402b08c8d0SAlan Stern if (dev && dev->driver == drv) { 241ed88747cSAlexander Duyck device_driver_detach(dev); 242151ef38fSGreg Kroah-Hartman err = count; 243151ef38fSGreg Kroah-Hartman } 2442b08c8d0SAlan Stern put_device(dev); 245fc1ede58SGreg Kroah-Hartman bus_put(bus); 246151ef38fSGreg Kroah-Hartman return err; 247151ef38fSGreg Kroah-Hartman } 24816b0dd40SJinchao Wang static DRIVER_ATTR_IGNORE_LOCKDEP(unbind, 0200, NULL, unbind_store); 249151ef38fSGreg Kroah-Hartman 250afdce75fSGreg Kroah-Hartman /* 251afdce75fSGreg Kroah-Hartman * Manually attach a device to a driver. 252afdce75fSGreg Kroah-Hartman * Note: the driver must want to bind to the device, 253afdce75fSGreg Kroah-Hartman * it is not possible to override the driver's id table. 254afdce75fSGreg Kroah-Hartman */ 2552581c9ccSGreg Kroah-Hartman static ssize_t bind_store(struct device_driver *drv, const char *buf, 2562581c9ccSGreg Kroah-Hartman size_t count) 257afdce75fSGreg Kroah-Hartman { 2585901d014SGreg Kroah-Hartman struct bus_type *bus = bus_get(drv->bus); 259afdce75fSGreg Kroah-Hartman struct device *dev; 260afdce75fSGreg Kroah-Hartman int err = -ENODEV; 261afdce75fSGreg Kroah-Hartman 2621f9ffc04SGreg Kroah-Hartman dev = bus_find_device_by_name(bus, NULL, buf); 263204db60cSJason Gunthorpe if (dev && driver_match_device(drv, dev)) { 264ed88747cSAlexander Duyck err = device_driver_attach(drv, dev); 265ef6dcbddSChristoph Hellwig if (!err) { 2664a3ad20cSGreg Kroah-Hartman /* success */ 26737225401SRyan Wilson err = count; 268afdce75fSGreg Kroah-Hartman } 2694a3ad20cSGreg Kroah-Hartman } 2702b08c8d0SAlan Stern put_device(dev); 271fc1ede58SGreg Kroah-Hartman bus_put(bus); 272afdce75fSGreg Kroah-Hartman return err; 273afdce75fSGreg Kroah-Hartman } 27416b0dd40SJinchao Wang static DRIVER_ATTR_IGNORE_LOCKDEP(bind, 0200, NULL, bind_store); 275afdce75fSGreg Kroah-Hartman 2762e7189b6SGreg Kroah-Hartman static ssize_t drivers_autoprobe_show(struct bus_type *bus, char *buf) 277b8c5cec2SKay Sievers { 278a00fdb98SGreg Kroah-Hartman struct subsys_private *sp = bus_to_subsys(bus); 279a00fdb98SGreg Kroah-Hartman int ret; 280a00fdb98SGreg Kroah-Hartman 281a00fdb98SGreg Kroah-Hartman if (!sp) 282a00fdb98SGreg Kroah-Hartman return -EINVAL; 283a00fdb98SGreg Kroah-Hartman 284a00fdb98SGreg Kroah-Hartman ret = sysfs_emit(buf, "%d\n", sp->drivers_autoprobe); 285a00fdb98SGreg Kroah-Hartman subsys_put(sp); 286a00fdb98SGreg Kroah-Hartman return ret; 287b8c5cec2SKay Sievers } 288b8c5cec2SKay Sievers 2892e7189b6SGreg Kroah-Hartman static ssize_t drivers_autoprobe_store(struct bus_type *bus, 290b8c5cec2SKay Sievers const char *buf, size_t count) 291b8c5cec2SKay Sievers { 292a00fdb98SGreg Kroah-Hartman struct subsys_private *sp = bus_to_subsys(bus); 293a00fdb98SGreg Kroah-Hartman 294a00fdb98SGreg Kroah-Hartman if (!sp) 295a00fdb98SGreg Kroah-Hartman return -EINVAL; 296a00fdb98SGreg Kroah-Hartman 297b8c5cec2SKay Sievers if (buf[0] == '0') 298a00fdb98SGreg Kroah-Hartman sp->drivers_autoprobe = 0; 299b8c5cec2SKay Sievers else 300a00fdb98SGreg Kroah-Hartman sp->drivers_autoprobe = 1; 301a00fdb98SGreg Kroah-Hartman 302a00fdb98SGreg Kroah-Hartman subsys_put(sp); 303b8c5cec2SKay Sievers return count; 304b8c5cec2SKay Sievers } 305b8c5cec2SKay Sievers 3062e7189b6SGreg Kroah-Hartman static ssize_t drivers_probe_store(struct bus_type *bus, 307b8c5cec2SKay Sievers const char *buf, size_t count) 308b8c5cec2SKay Sievers { 309b8c5cec2SKay Sievers struct device *dev; 3100372ffb3SAlex Williamson int err = -EINVAL; 311b8c5cec2SKay Sievers 3121f9ffc04SGreg Kroah-Hartman dev = bus_find_device_by_name(bus, NULL, buf); 313b8c5cec2SKay Sievers if (!dev) 314b8c5cec2SKay Sievers return -ENODEV; 3150372ffb3SAlex Williamson if (bus_rescan_devices_helper(dev, NULL) == 0) 3160372ffb3SAlex Williamson err = count; 3170372ffb3SAlex Williamson put_device(dev); 3180372ffb3SAlex Williamson return err; 319b8c5cec2SKay Sievers } 320151ef38fSGreg Kroah-Hartman 321465c7a3aSmochel@digitalimplant.org static struct device *next_device(struct klist_iter *i) 322465c7a3aSmochel@digitalimplant.org { 323465c7a3aSmochel@digitalimplant.org struct klist_node *n = klist_next(i); 324ae1b4171SGreg Kroah-Hartman struct device *dev = NULL; 325ae1b4171SGreg Kroah-Hartman struct device_private *dev_prv; 326ae1b4171SGreg Kroah-Hartman 327ae1b4171SGreg Kroah-Hartman if (n) { 328ae1b4171SGreg Kroah-Hartman dev_prv = to_device_private_bus(n); 329ae1b4171SGreg Kroah-Hartman dev = dev_prv->device; 330ae1b4171SGreg Kroah-Hartman } 331ae1b4171SGreg Kroah-Hartman return dev; 332465c7a3aSmochel@digitalimplant.org } 333465c7a3aSmochel@digitalimplant.org 3341da177e4SLinus Torvalds /** 3351da177e4SLinus Torvalds * bus_for_each_dev - device iterator. 3361da177e4SLinus Torvalds * @bus: bus type. 3371da177e4SLinus Torvalds * @start: device to start iterating from. 3381da177e4SLinus Torvalds * @data: data for the callback. 3391da177e4SLinus Torvalds * @fn: function to be called for each device. 3401da177e4SLinus Torvalds * 3411da177e4SLinus Torvalds * Iterate over @bus's list of devices, and call @fn for each, 3421da177e4SLinus Torvalds * passing it @data. If @start is not NULL, we use that device to 3431da177e4SLinus Torvalds * begin iterating from. 3441da177e4SLinus Torvalds * 3451da177e4SLinus Torvalds * We check the return of @fn each time. If it returns anything 3461da177e4SLinus Torvalds * other than 0, we break out and return that value. 3471da177e4SLinus Torvalds * 3481da177e4SLinus Torvalds * NOTE: The device that returns a non-zero value is not retained 3491da177e4SLinus Torvalds * in any way, nor is its refcount incremented. If the caller needs 3500fa1b0a1SAlex Chiang * to retain this data, it should do so, and increment the reference 3511da177e4SLinus Torvalds * count in the supplied callback. 3521da177e4SLinus Torvalds */ 353e0766ea4SGreg Kroah-Hartman int bus_for_each_dev(const struct bus_type *bus, struct device *start, 3541da177e4SLinus Torvalds void *data, int (*fn)(struct device *, void *)) 3551da177e4SLinus Torvalds { 35683b9148dSGreg Kroah-Hartman struct subsys_private *sp = bus_to_subsys(bus); 357465c7a3aSmochel@digitalimplant.org struct klist_iter i; 358465c7a3aSmochel@digitalimplant.org struct device *dev; 359465c7a3aSmochel@digitalimplant.org int error = 0; 3601da177e4SLinus Torvalds 36183b9148dSGreg Kroah-Hartman if (!sp) 362465c7a3aSmochel@digitalimplant.org return -EINVAL; 363465c7a3aSmochel@digitalimplant.org 36483b9148dSGreg Kroah-Hartman klist_iter_init_node(&sp->klist_devices, &i, 365ae1b4171SGreg Kroah-Hartman (start ? &start->p->knode_bus : NULL)); 36693ead7c9SGimcuan Hui while (!error && (dev = next_device(&i))) 367465c7a3aSmochel@digitalimplant.org error = fn(dev, data); 368465c7a3aSmochel@digitalimplant.org klist_iter_exit(&i); 36983b9148dSGreg Kroah-Hartman subsys_put(sp); 370465c7a3aSmochel@digitalimplant.org return error; 3711da177e4SLinus Torvalds } 3724a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_for_each_dev); 3731da177e4SLinus Torvalds 3740edb5860SCornelia Huck /** 3750edb5860SCornelia Huck * bus_find_device - device iterator for locating a particular device. 3760edb5860SCornelia Huck * @bus: bus type 3770edb5860SCornelia Huck * @start: Device to begin with 3780edb5860SCornelia Huck * @data: Data to pass to match function 3790edb5860SCornelia Huck * @match: Callback function to check device 3800edb5860SCornelia Huck * 3810edb5860SCornelia Huck * This is similar to the bus_for_each_dev() function above, but it 3820edb5860SCornelia Huck * returns a reference to a device that is 'found' for later use, as 3830edb5860SCornelia Huck * determined by the @match callback. 3840edb5860SCornelia Huck * 3850edb5860SCornelia Huck * The callback should return 0 if the device doesn't match and non-zero 3860edb5860SCornelia Huck * if it does. If the callback returns non-zero, this function will 3870edb5860SCornelia Huck * return to the caller and not iterate over any more devices. 3880edb5860SCornelia Huck */ 389e0766ea4SGreg Kroah-Hartman struct device *bus_find_device(const struct bus_type *bus, 390418e3ea1SSuzuki K Poulose struct device *start, const void *data, 391418e3ea1SSuzuki K Poulose int (*match)(struct device *dev, const void *data)) 3920edb5860SCornelia Huck { 39383b9148dSGreg Kroah-Hartman struct subsys_private *sp = bus_to_subsys(bus); 3940edb5860SCornelia Huck struct klist_iter i; 3950edb5860SCornelia Huck struct device *dev; 3960edb5860SCornelia Huck 39783b9148dSGreg Kroah-Hartman if (!sp) 3980edb5860SCornelia Huck return NULL; 3990edb5860SCornelia Huck 40083b9148dSGreg Kroah-Hartman klist_iter_init_node(&sp->klist_devices, &i, 4017cd9c9bbSGreg Kroah-Hartman (start ? &start->p->knode_bus : NULL)); 4020edb5860SCornelia Huck while ((dev = next_device(&i))) 4030edb5860SCornelia Huck if (match(dev, data) && get_device(dev)) 4040edb5860SCornelia Huck break; 4050edb5860SCornelia Huck klist_iter_exit(&i); 40683b9148dSGreg Kroah-Hartman subsys_put(sp); 4070edb5860SCornelia Huck return dev; 4080edb5860SCornelia Huck } 4094a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_find_device); 41038fdac3cSmochel@digitalimplant.org 41138fdac3cSmochel@digitalimplant.org static struct device_driver *next_driver(struct klist_iter *i) 41238fdac3cSmochel@digitalimplant.org { 41338fdac3cSmochel@digitalimplant.org struct klist_node *n = klist_next(i); 414e5dd1278SGreg Kroah-Hartman struct driver_private *drv_priv; 415e5dd1278SGreg Kroah-Hartman 416e5dd1278SGreg Kroah-Hartman if (n) { 417e5dd1278SGreg Kroah-Hartman drv_priv = container_of(n, struct driver_private, knode_bus); 418e5dd1278SGreg Kroah-Hartman return drv_priv->driver; 419e5dd1278SGreg Kroah-Hartman } 420e5dd1278SGreg Kroah-Hartman return NULL; 42138fdac3cSmochel@digitalimplant.org } 42238fdac3cSmochel@digitalimplant.org 4231da177e4SLinus Torvalds /** 4241da177e4SLinus Torvalds * bus_for_each_drv - driver iterator 4251da177e4SLinus Torvalds * @bus: bus we're dealing with. 4261da177e4SLinus Torvalds * @start: driver to start iterating on. 4271da177e4SLinus Torvalds * @data: data to pass to the callback. 4281da177e4SLinus Torvalds * @fn: function to call for each driver. 4291da177e4SLinus Torvalds * 4301da177e4SLinus Torvalds * This is nearly identical to the device iterator above. 4311da177e4SLinus Torvalds * We iterate over each driver that belongs to @bus, and call 4321da177e4SLinus Torvalds * @fn for each. If @fn returns anything but 0, we break out 4331da177e4SLinus Torvalds * and return it. If @start is not NULL, we use it as the head 4341da177e4SLinus Torvalds * of the list. 4351da177e4SLinus Torvalds * 4361da177e4SLinus Torvalds * NOTE: we don't return the driver that returns a non-zero 4371da177e4SLinus Torvalds * value, nor do we leave the reference count incremented for that 4381da177e4SLinus Torvalds * driver. If the caller needs to know that info, it must set it 4391da177e4SLinus Torvalds * in the callback. It must also be sure to increment the refcount 4401da177e4SLinus Torvalds * so it doesn't disappear before returning to the caller. 4411da177e4SLinus Torvalds */ 442e0766ea4SGreg Kroah-Hartman int bus_for_each_drv(const struct bus_type *bus, struct device_driver *start, 4431da177e4SLinus Torvalds void *data, int (*fn)(struct device_driver *, void *)) 4441da177e4SLinus Torvalds { 44583b9148dSGreg Kroah-Hartman struct subsys_private *sp = bus_to_subsys(bus); 44638fdac3cSmochel@digitalimplant.org struct klist_iter i; 44738fdac3cSmochel@digitalimplant.org struct device_driver *drv; 44838fdac3cSmochel@digitalimplant.org int error = 0; 4491da177e4SLinus Torvalds 45083b9148dSGreg Kroah-Hartman if (!sp) 45138fdac3cSmochel@digitalimplant.org return -EINVAL; 45238fdac3cSmochel@digitalimplant.org 45383b9148dSGreg Kroah-Hartman klist_iter_init_node(&sp->klist_drivers, &i, 454e5dd1278SGreg Kroah-Hartman start ? &start->p->knode_bus : NULL); 45538fdac3cSmochel@digitalimplant.org while ((drv = next_driver(&i)) && !error) 45638fdac3cSmochel@digitalimplant.org error = fn(drv, data); 45738fdac3cSmochel@digitalimplant.org klist_iter_exit(&i); 45883b9148dSGreg Kroah-Hartman subsys_put(sp); 45938fdac3cSmochel@digitalimplant.org return error; 4601da177e4SLinus Torvalds } 4614a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_for_each_drv); 4621da177e4SLinus Torvalds 4631da177e4SLinus Torvalds /** 4641da177e4SLinus Torvalds * bus_add_device - add device to bus 4651da177e4SLinus Torvalds * @dev: device being added 4661da177e4SLinus Torvalds * 4672023c610SAlan Stern * - Add device's bus attributes. 4682023c610SAlan Stern * - Create links to device's bus. 4691da177e4SLinus Torvalds * - Add the device to its bus's list of devices. 4701da177e4SLinus Torvalds */ 4711da177e4SLinus Torvalds int bus_add_device(struct device *dev) 4721da177e4SLinus Torvalds { 4735221b82dSGreg Kroah-Hartman struct subsys_private *sp = bus_to_subsys(dev->bus); 4745221b82dSGreg Kroah-Hartman int error; 4751da177e4SLinus Torvalds 4765221b82dSGreg Kroah-Hartman if (!sp) { 4775221b82dSGreg Kroah-Hartman /* 4785221b82dSGreg Kroah-Hartman * This is a normal operation for many devices that do not 4795221b82dSGreg Kroah-Hartman * have a bus assigned to them, just say that all went 4805221b82dSGreg Kroah-Hartman * well. 4815221b82dSGreg Kroah-Hartman */ 4825221b82dSGreg Kroah-Hartman return 0; 4835221b82dSGreg Kroah-Hartman } 4845221b82dSGreg Kroah-Hartman 4855221b82dSGreg Kroah-Hartman /* 4865221b82dSGreg Kroah-Hartman * Reference in sp is now incremented and will be dropped when 4875221b82dSGreg Kroah-Hartman * the device is removed from the bus 4885221b82dSGreg Kroah-Hartman */ 4895221b82dSGreg Kroah-Hartman 4905221b82dSGreg Kroah-Hartman pr_debug("bus: '%s': add device %s\n", sp->bus->name, dev_name(dev)); 4915221b82dSGreg Kroah-Hartman 4925221b82dSGreg Kroah-Hartman error = device_add_groups(dev, sp->bus->dev_groups); 493fa6fdb33SGreg Kroah-Hartman if (error) 494b46c7337SGreg Kroah-Hartman goto out_put; 4955221b82dSGreg Kroah-Hartman 4965221b82dSGreg Kroah-Hartman error = sysfs_create_link(&sp->devices_kset->kobj, &dev->kobj, dev_name(dev)); 497f86db396SAndrew Morton if (error) 4981c34203aSJunjie Mao goto out_groups; 4995221b82dSGreg Kroah-Hartman 5005221b82dSGreg Kroah-Hartman error = sysfs_create_link(&dev->kobj, &sp->subsys.kobj, "subsystem"); 501f86db396SAndrew Morton if (error) 502513e7337SCornelia Huck goto out_subsys; 5035221b82dSGreg Kroah-Hartman 5045221b82dSGreg Kroah-Hartman klist_add_tail(&dev->p->knode_bus, &sp->klist_devices); 505513e7337SCornelia Huck return 0; 506513e7337SCornelia Huck 507513e7337SCornelia Huck out_subsys: 5085221b82dSGreg Kroah-Hartman sysfs_remove_link(&sp->devices_kset->kobj, dev_name(dev)); 509fa6fdb33SGreg Kroah-Hartman out_groups: 5105221b82dSGreg Kroah-Hartman device_remove_groups(dev, sp->bus->dev_groups); 511513e7337SCornelia Huck out_put: 5125221b82dSGreg Kroah-Hartman subsys_put(sp); 5131da177e4SLinus Torvalds return error; 5141da177e4SLinus Torvalds } 5151da177e4SLinus Torvalds 5161da177e4SLinus Torvalds /** 5172023c610SAlan Stern * bus_probe_device - probe drivers for a new device 5182023c610SAlan Stern * @dev: device to probe 51953877d06SKay Sievers * 5202023c610SAlan Stern * - Automatically probe for a driver if the bus allows it. 52153877d06SKay Sievers */ 5222023c610SAlan Stern void bus_probe_device(struct device *dev) 52353877d06SKay Sievers { 5245221b82dSGreg Kroah-Hartman struct subsys_private *sp = bus_to_subsys(dev->bus); 525ca22e56dSKay Sievers struct subsys_interface *sif; 52653877d06SKay Sievers 5275221b82dSGreg Kroah-Hartman if (!sp) 528ca22e56dSKay Sievers return; 529ca22e56dSKay Sievers 5305221b82dSGreg Kroah-Hartman if (sp->drivers_autoprobe) 531765230b5SDmitry Torokhov device_initial_probe(dev); 532ca22e56dSKay Sievers 5335221b82dSGreg Kroah-Hartman mutex_lock(&sp->mutex); 5345221b82dSGreg Kroah-Hartman list_for_each_entry(sif, &sp->interfaces, node) 535ca22e56dSKay Sievers if (sif->add_dev) 536ca22e56dSKay Sievers sif->add_dev(dev, sif); 5375221b82dSGreg Kroah-Hartman mutex_unlock(&sp->mutex); 5385221b82dSGreg Kroah-Hartman subsys_put(sp); 539f86db396SAndrew Morton } 54053877d06SKay Sievers 54153877d06SKay Sievers /** 5421da177e4SLinus Torvalds * bus_remove_device - remove device from bus 5431da177e4SLinus Torvalds * @dev: device to be removed 5441da177e4SLinus Torvalds * 545ca22e56dSKay Sievers * - Remove device from all interfaces. 546ca22e56dSKay Sievers * - Remove symlink from bus' directory. 5471da177e4SLinus Torvalds * - Delete device from bus's list. 5481da177e4SLinus Torvalds * - Detach from its driver. 5491da177e4SLinus Torvalds * - Drop reference taken in bus_add_device(). 5501da177e4SLinus Torvalds */ 5511da177e4SLinus Torvalds void bus_remove_device(struct device *dev) 5521da177e4SLinus Torvalds { 5535221b82dSGreg Kroah-Hartman struct subsys_private *sp = bus_to_subsys(dev->bus); 554ca22e56dSKay Sievers struct subsys_interface *sif; 555ca22e56dSKay Sievers 5565221b82dSGreg Kroah-Hartman if (!sp) 557ca22e56dSKay Sievers return; 558ca22e56dSKay Sievers 5595221b82dSGreg Kroah-Hartman mutex_lock(&sp->mutex); 5605221b82dSGreg Kroah-Hartman list_for_each_entry(sif, &sp->interfaces, node) 561ca22e56dSKay Sievers if (sif->remove_dev) 562ca22e56dSKay Sievers sif->remove_dev(dev, sif); 5635221b82dSGreg Kroah-Hartman mutex_unlock(&sp->mutex); 564ca22e56dSKay Sievers 565b9d9c82bSKay Sievers sysfs_remove_link(&dev->kobj, "subsystem"); 5665221b82dSGreg Kroah-Hartman sysfs_remove_link(&sp->devices_kset->kobj, dev_name(dev)); 567fa6fdb33SGreg Kroah-Hartman device_remove_groups(dev, dev->bus->dev_groups); 568ae1b4171SGreg Kroah-Hartman if (klist_node_attached(&dev->p->knode_bus)) 569ae1b4171SGreg Kroah-Hartman klist_del(&dev->p->knode_bus); 5703f62e570SGreg Kroah-Hartman 5714a3ad20cSGreg Kroah-Hartman pr_debug("bus: '%s': remove device %s\n", 5721e0b2cf9SKay Sievers dev->bus->name, dev_name(dev)); 5731da177e4SLinus Torvalds device_release_driver(dev); 5745221b82dSGreg Kroah-Hartman 5755221b82dSGreg Kroah-Hartman /* 5765221b82dSGreg Kroah-Hartman * Decrement the reference count twice, once for the bus_to_subsys() 5775221b82dSGreg Kroah-Hartman * call in the start of this function, and the second one from the 5785221b82dSGreg Kroah-Hartman * reference increment in bus_add_device() 5795221b82dSGreg Kroah-Hartman */ 5805221b82dSGreg Kroah-Hartman subsys_put(sp); 5815221b82dSGreg Kroah-Hartman subsys_put(sp); 5821da177e4SLinus Torvalds } 5831da177e4SLinus Torvalds 584f86db396SAndrew Morton static int __must_check add_bind_files(struct device_driver *drv) 585874c6241SGreg Kroah-Hartman { 586f86db396SAndrew Morton int ret; 587f86db396SAndrew Morton 588f86db396SAndrew Morton ret = driver_create_file(drv, &driver_attr_unbind); 589f86db396SAndrew Morton if (ret == 0) { 590f86db396SAndrew Morton ret = driver_create_file(drv, &driver_attr_bind); 591f86db396SAndrew Morton if (ret) 592f86db396SAndrew Morton driver_remove_file(drv, &driver_attr_unbind); 593f86db396SAndrew Morton } 594f86db396SAndrew Morton return ret; 595874c6241SGreg Kroah-Hartman } 596874c6241SGreg Kroah-Hartman 597874c6241SGreg Kroah-Hartman static void remove_bind_files(struct device_driver *drv) 598874c6241SGreg Kroah-Hartman { 599874c6241SGreg Kroah-Hartman driver_remove_file(drv, &driver_attr_bind); 600874c6241SGreg Kroah-Hartman driver_remove_file(drv, &driver_attr_unbind); 601874c6241SGreg Kroah-Hartman } 602b8c5cec2SKay Sievers 6032e7189b6SGreg Kroah-Hartman static BUS_ATTR_WO(drivers_probe); 6042e7189b6SGreg Kroah-Hartman static BUS_ATTR_RW(drivers_autoprobe); 6058380770cSKay Sievers 606*4dd1f3f8SGreg Kroah-Hartman static int add_probe_files(const struct bus_type *bus) 607b8c5cec2SKay Sievers { 608b8c5cec2SKay Sievers int retval; 609b8c5cec2SKay Sievers 6108380770cSKay Sievers retval = bus_create_file(bus, &bus_attr_drivers_probe); 611b8c5cec2SKay Sievers if (retval) 612b8c5cec2SKay Sievers goto out; 613b8c5cec2SKay Sievers 6148380770cSKay Sievers retval = bus_create_file(bus, &bus_attr_drivers_autoprobe); 615b8c5cec2SKay Sievers if (retval) 6168380770cSKay Sievers bus_remove_file(bus, &bus_attr_drivers_probe); 617b8c5cec2SKay Sievers out: 618b8c5cec2SKay Sievers return retval; 619b8c5cec2SKay Sievers } 620b8c5cec2SKay Sievers 621*4dd1f3f8SGreg Kroah-Hartman static void remove_probe_files(const struct bus_type *bus) 622b8c5cec2SKay Sievers { 6238380770cSKay Sievers bus_remove_file(bus, &bus_attr_drivers_autoprobe); 6248380770cSKay Sievers bus_remove_file(bus, &bus_attr_drivers_probe); 625b8c5cec2SKay Sievers } 6261da177e4SLinus Torvalds 6272581c9ccSGreg Kroah-Hartman static ssize_t uevent_store(struct device_driver *drv, const char *buf, 6282581c9ccSGreg Kroah-Hartman size_t count) 6297ac1cf4aSKay Sievers { 630df44b479SPeter Rajnoha int rc; 631df44b479SPeter Rajnoha 632df44b479SPeter Rajnoha rc = kobject_synth_uevent(&drv->p->kobj, buf, count); 633df44b479SPeter Rajnoha return rc ? rc : count; 6347ac1cf4aSKay Sievers } 6352581c9ccSGreg Kroah-Hartman static DRIVER_ATTR_WO(uevent); 6367ac1cf4aSKay Sievers 6371da177e4SLinus Torvalds /** 6381da177e4SLinus Torvalds * bus_add_driver - Add a driver to the bus. 6391da177e4SLinus Torvalds * @drv: driver. 6401da177e4SLinus Torvalds */ 6411da177e4SLinus Torvalds int bus_add_driver(struct device_driver *drv) 6421da177e4SLinus Torvalds { 643e4f05682SGreg Kroah-Hartman struct subsys_private *sp = bus_to_subsys(drv->bus); 644e5dd1278SGreg Kroah-Hartman struct driver_private *priv; 6451da177e4SLinus Torvalds int error = 0; 6461da177e4SLinus Torvalds 647e4f05682SGreg Kroah-Hartman if (!sp) 6484f6e1945SGreg Kroah-Hartman return -EINVAL; 649d9fd4d3bSJeff Garzik 650e4f05682SGreg Kroah-Hartman /* 651e4f05682SGreg Kroah-Hartman * Reference in sp is now incremented and will be dropped when 652e4f05682SGreg Kroah-Hartman * the driver is removed from the bus 653e4f05682SGreg Kroah-Hartman */ 654e4f05682SGreg Kroah-Hartman pr_debug("bus: '%s': add driver %s\n", sp->bus->name, drv->name); 655e5dd1278SGreg Kroah-Hartman 656e5dd1278SGreg Kroah-Hartman priv = kzalloc(sizeof(*priv), GFP_KERNEL); 65707634464SCornelia Huck if (!priv) { 65807634464SCornelia Huck error = -ENOMEM; 65907634464SCornelia Huck goto out_put_bus; 66007634464SCornelia Huck } 661e5dd1278SGreg Kroah-Hartman klist_init(&priv->klist_devices, NULL, NULL); 662e5dd1278SGreg Kroah-Hartman priv->driver = drv; 663e5dd1278SGreg Kroah-Hartman drv->p = priv; 664e4f05682SGreg Kroah-Hartman priv->kobj.kset = sp->drivers_kset; 665c8e90d82SGreg Kroah-Hartman error = kobject_init_and_add(&priv->kobj, &driver_ktype, NULL, 666c8e90d82SGreg Kroah-Hartman "%s", drv->name); 667dc0afa83SCornelia Huck if (error) 66807634464SCornelia Huck goto out_unregister; 6691da177e4SLinus Torvalds 670e4f05682SGreg Kroah-Hartman klist_add_tail(&priv->knode_bus, &sp->klist_drivers); 671e4f05682SGreg Kroah-Hartman if (sp->drivers_autoprobe) { 672f86db396SAndrew Morton error = driver_attach(drv); 673f86db396SAndrew Morton if (error) 674310862e5SSchspa Shi goto out_del_list; 675b8c5cec2SKay Sievers } 6761da177e4SLinus Torvalds module_add_driver(drv->owner, drv); 6771da177e4SLinus Torvalds 6787ac1cf4aSKay Sievers error = driver_create_file(drv, &driver_attr_uevent); 6797ac1cf4aSKay Sievers if (error) { 6807ac1cf4aSKay Sievers printk(KERN_ERR "%s: uevent attr (%s) failed\n", 6812b3a302aSHarvey Harrison __func__, drv->name); 6827ac1cf4aSKay Sievers } 683e4f05682SGreg Kroah-Hartman error = driver_add_groups(drv, sp->bus->drv_groups); 684f86db396SAndrew Morton if (error) { 685f86db396SAndrew Morton /* How the hell do we get out of this pickle? Give up */ 6864044b2fcSJoe Pater printk(KERN_ERR "%s: driver_add_groups(%s) failed\n", 687ed0617b5SGreg Kroah-Hartman __func__, drv->name); 688e18945b1SGreg Kroah-Hartman } 6891a6f2a75SDmitry Torokhov 6901a6f2a75SDmitry Torokhov if (!drv->suppress_bind_attrs) { 691f86db396SAndrew Morton error = add_bind_files(drv); 692f86db396SAndrew Morton if (error) { 693f86db396SAndrew Morton /* Ditto */ 694f86db396SAndrew Morton printk(KERN_ERR "%s: add_bind_files(%s) failed\n", 6952b3a302aSHarvey Harrison __func__, drv->name); 696f86db396SAndrew Morton } 6971a6f2a75SDmitry Torokhov } 698d9fd4d3bSJeff Garzik 6995c8563d7SKay Sievers return 0; 7001a6f2a75SDmitry Torokhov 701310862e5SSchspa Shi out_del_list: 702310862e5SSchspa Shi klist_del(&priv->knode_bus); 703f86db396SAndrew Morton out_unregister: 70499b28f1bSPhil Carmody kobject_put(&priv->kobj); 7050f9b011dSChristophe JAILLET /* drv->p is freed in driver_release() */ 7065c8563d7SKay Sievers drv->p = NULL; 707f86db396SAndrew Morton out_put_bus: 708e4f05682SGreg Kroah-Hartman subsys_put(sp); 709f86db396SAndrew Morton return error; 7101da177e4SLinus Torvalds } 7111da177e4SLinus Torvalds 7121da177e4SLinus Torvalds /** 7131da177e4SLinus Torvalds * bus_remove_driver - delete driver from bus's knowledge. 7141da177e4SLinus Torvalds * @drv: driver. 7151da177e4SLinus Torvalds * 7161da177e4SLinus Torvalds * Detach the driver from the devices it controls, and remove 7171da177e4SLinus Torvalds * it from its bus's list of drivers. Finally, we drop the reference 7181da177e4SLinus Torvalds * to the bus we took in bus_add_driver(). 7191da177e4SLinus Torvalds */ 7201da177e4SLinus Torvalds void bus_remove_driver(struct device_driver *drv) 7211da177e4SLinus Torvalds { 722e4f05682SGreg Kroah-Hartman struct subsys_private *sp = bus_to_subsys(drv->bus); 723e4f05682SGreg Kroah-Hartman 724e4f05682SGreg Kroah-Hartman if (!sp) 725d9fd4d3bSJeff Garzik return; 726d9fd4d3bSJeff Garzik 727e4f05682SGreg Kroah-Hartman pr_debug("bus: '%s': remove driver %s\n", sp->bus->name, drv->name); 728e4f05682SGreg Kroah-Hartman 7291a6f2a75SDmitry Torokhov if (!drv->suppress_bind_attrs) 730874c6241SGreg Kroah-Hartman remove_bind_files(drv); 731e4f05682SGreg Kroah-Hartman driver_remove_groups(drv, sp->bus->drv_groups); 7327ac1cf4aSKay Sievers driver_remove_file(drv, &driver_attr_uevent); 733e5dd1278SGreg Kroah-Hartman klist_remove(&drv->p->knode_bus); 7341da177e4SLinus Torvalds driver_detach(drv); 7351da177e4SLinus Torvalds module_remove_driver(drv); 736c10997f6SGreg Kroah-Hartman kobject_put(&drv->p->kobj); 737e4f05682SGreg Kroah-Hartman 738e4f05682SGreg Kroah-Hartman /* 739e4f05682SGreg Kroah-Hartman * Decrement the reference count twice, once for the bus_to_subsys() 740e4f05682SGreg Kroah-Hartman * call in the start of this function, and the second one from the 741e4f05682SGreg Kroah-Hartman * reference increment in bus_add_driver() 742e4f05682SGreg Kroah-Hartman */ 743e4f05682SGreg Kroah-Hartman subsys_put(sp); 744e4f05682SGreg Kroah-Hartman subsys_put(sp); 7451da177e4SLinus Torvalds } 7461da177e4SLinus Torvalds 7471da177e4SLinus Torvalds /* Helper for bus_rescan_devices's iter */ 748f86db396SAndrew Morton static int __must_check bus_rescan_devices_helper(struct device *dev, 749f86db396SAndrew Morton void *data) 7501da177e4SLinus Torvalds { 751f86db396SAndrew Morton int ret = 0; 752f86db396SAndrew Morton 753bf74ad5bSAlan Stern if (!dev->driver) { 7548c97a46aSMartin Liu if (dev->parent && dev->bus->need_parent_lock) 7558e9394ceSGreg Kroah-Hartman device_lock(dev->parent); 756f86db396SAndrew Morton ret = device_attach(dev); 7578c97a46aSMartin Liu if (dev->parent && dev->bus->need_parent_lock) 7588e9394ceSGreg Kroah-Hartman device_unlock(dev->parent); 759bf74ad5bSAlan Stern } 760f86db396SAndrew Morton return ret < 0 ? ret : 0; 7611da177e4SLinus Torvalds } 7621da177e4SLinus Torvalds 7631da177e4SLinus Torvalds /** 7641da177e4SLinus Torvalds * bus_rescan_devices - rescan devices on the bus for possible drivers 7651da177e4SLinus Torvalds * @bus: the bus to scan. 7661da177e4SLinus Torvalds * 7671da177e4SLinus Torvalds * This function will look for devices on the bus with no driver 76823d3d602SGreg Kroah-Hartman * attached and rescan it against existing drivers to see if it matches 76923d3d602SGreg Kroah-Hartman * any by calling device_attach() for the unbound devices. 7701da177e4SLinus Torvalds */ 771f86db396SAndrew Morton int bus_rescan_devices(struct bus_type *bus) 7721da177e4SLinus Torvalds { 773f86db396SAndrew Morton return bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper); 7741da177e4SLinus Torvalds } 7754a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_rescan_devices); 7761da177e4SLinus Torvalds 777e935d5daSMoore, Eric /** 778e935d5daSMoore, Eric * device_reprobe - remove driver for a device and probe for a new driver 779e935d5daSMoore, Eric * @dev: the device to reprobe 780e935d5daSMoore, Eric * 781e935d5daSMoore, Eric * This function detaches the attached driver (if any) for the given 782e935d5daSMoore, Eric * device and restarts the driver probing process. It is intended 783e935d5daSMoore, Eric * to use if probing criteria changed during a devices lifetime and 784e935d5daSMoore, Eric * driver attachment should change accordingly. 785e935d5daSMoore, Eric */ 786f86db396SAndrew Morton int device_reprobe(struct device *dev) 787e935d5daSMoore, Eric { 788ed88747cSAlexander Duyck if (dev->driver) 789ed88747cSAlexander Duyck device_driver_detach(dev); 790f86db396SAndrew Morton return bus_rescan_devices_helper(dev, NULL); 791e935d5daSMoore, Eric } 792e935d5daSMoore, Eric EXPORT_SYMBOL_GPL(device_reprobe); 7931da177e4SLinus Torvalds 79434bb61f9SJames Bottomley static void klist_devices_get(struct klist_node *n) 79534bb61f9SJames Bottomley { 796ae1b4171SGreg Kroah-Hartman struct device_private *dev_prv = to_device_private_bus(n); 797ae1b4171SGreg Kroah-Hartman struct device *dev = dev_prv->device; 79834bb61f9SJames Bottomley 79934bb61f9SJames Bottomley get_device(dev); 80034bb61f9SJames Bottomley } 80134bb61f9SJames Bottomley 80234bb61f9SJames Bottomley static void klist_devices_put(struct klist_node *n) 80334bb61f9SJames Bottomley { 804ae1b4171SGreg Kroah-Hartman struct device_private *dev_prv = to_device_private_bus(n); 805ae1b4171SGreg Kroah-Hartman struct device *dev = dev_prv->device; 80634bb61f9SJames Bottomley 80734bb61f9SJames Bottomley put_device(dev); 80834bb61f9SJames Bottomley } 80934bb61f9SJames Bottomley 8107ac1cf4aSKay Sievers static ssize_t bus_uevent_store(struct bus_type *bus, 8117ac1cf4aSKay Sievers const char *buf, size_t count) 8127ac1cf4aSKay Sievers { 813a00fdb98SGreg Kroah-Hartman struct subsys_private *sp = bus_to_subsys(bus); 814a00fdb98SGreg Kroah-Hartman int ret; 815df44b479SPeter Rajnoha 816a00fdb98SGreg Kroah-Hartman if (!sp) 817a00fdb98SGreg Kroah-Hartman return -EINVAL; 818a00fdb98SGreg Kroah-Hartman 819a00fdb98SGreg Kroah-Hartman ret = kobject_synth_uevent(&sp->subsys.kobj, buf, count); 820a00fdb98SGreg Kroah-Hartman subsys_put(sp); 821a00fdb98SGreg Kroah-Hartman 822a00fdb98SGreg Kroah-Hartman if (ret) 823a00fdb98SGreg Kroah-Hartman return ret; 824a00fdb98SGreg Kroah-Hartman return count; 8257ac1cf4aSKay Sievers } 826a4723041SGreg Kroah-Hartman /* 827a4723041SGreg Kroah-Hartman * "open code" the old BUS_ATTR() macro here. We want to use BUS_ATTR_WO() 828a4723041SGreg Kroah-Hartman * here, but can not use it as earlier in the file we have 829a4723041SGreg Kroah-Hartman * DEVICE_ATTR_WO(uevent), which would cause a clash with the with the store 830a4723041SGreg Kroah-Hartman * function name. 831a4723041SGreg Kroah-Hartman */ 83216b0dd40SJinchao Wang static struct bus_attribute bus_attr_uevent = __ATTR(uevent, 0200, NULL, 833a4723041SGreg Kroah-Hartman bus_uevent_store); 8347ac1cf4aSKay Sievers 8351da177e4SLinus Torvalds /** 836be871b7eSMichal Hocko * bus_register - register a driver-core subsystem 83778d79559SRandy Dunlap * @bus: bus to register 8381da177e4SLinus Torvalds * 83978d79559SRandy Dunlap * Once we have that, we register the bus with the kobject 8401da177e4SLinus Torvalds * infrastructure, then register the children subsystems it has: 841ca22e56dSKay Sievers * the devices and drivers that belong to the subsystem. 8421da177e4SLinus Torvalds */ 843be871b7eSMichal Hocko int bus_register(struct bus_type *bus) 8441da177e4SLinus Torvalds { 8451da177e4SLinus Torvalds int retval; 8466b6e39a6SKay Sievers struct subsys_private *priv; 8473465e2e4SGreg Kroah-Hartman struct kobject *bus_kobj; 84837e98d9bSGreg Kroah-Hartman struct lock_class_key *key; 8491da177e4SLinus Torvalds 8506b6e39a6SKay Sievers priv = kzalloc(sizeof(struct subsys_private), GFP_KERNEL); 851c6f7e72aSGreg Kroah-Hartman if (!priv) 852c6f7e72aSGreg Kroah-Hartman return -ENOMEM; 853116af378SBenjamin Herrenschmidt 854c6f7e72aSGreg Kroah-Hartman priv->bus = bus; 855c6f7e72aSGreg Kroah-Hartman 856c6f7e72aSGreg Kroah-Hartman BLOCKING_INIT_NOTIFIER_HEAD(&priv->bus_notifier); 857c6f7e72aSGreg Kroah-Hartman 8583465e2e4SGreg Kroah-Hartman bus_kobj = &priv->subsys.kobj; 8593465e2e4SGreg Kroah-Hartman retval = kobject_set_name(bus_kobj, "%s", bus->name); 8601da177e4SLinus Torvalds if (retval) 8611da177e4SLinus Torvalds goto out; 8621da177e4SLinus Torvalds 8633465e2e4SGreg Kroah-Hartman bus_kobj->kset = bus_kset; 8643465e2e4SGreg Kroah-Hartman bus_kobj->ktype = &bus_ktype; 865c6f7e72aSGreg Kroah-Hartman priv->drivers_autoprobe = 1; 866d6b05b84SGreg Kroah-Hartman 867c6f7e72aSGreg Kroah-Hartman retval = kset_register(&priv->subsys); 8681da177e4SLinus Torvalds if (retval) 8691da177e4SLinus Torvalds goto out; 8701da177e4SLinus Torvalds 8717ac1cf4aSKay Sievers retval = bus_create_file(bus, &bus_attr_uevent); 8727ac1cf4aSKay Sievers if (retval) 8737ac1cf4aSKay Sievers goto bus_uevent_fail; 8747ac1cf4aSKay Sievers 8753465e2e4SGreg Kroah-Hartman priv->devices_kset = kset_create_and_add("devices", NULL, bus_kobj); 876c6f7e72aSGreg Kroah-Hartman if (!priv->devices_kset) { 8773d899596SGreg Kroah-Hartman retval = -ENOMEM; 8781da177e4SLinus Torvalds goto bus_devices_fail; 8793d899596SGreg Kroah-Hartman } 8801da177e4SLinus Torvalds 8813465e2e4SGreg Kroah-Hartman priv->drivers_kset = kset_create_and_add("drivers", NULL, bus_kobj); 882c6f7e72aSGreg Kroah-Hartman if (!priv->drivers_kset) { 8836dcec251SGreg Kroah-Hartman retval = -ENOMEM; 8841da177e4SLinus Torvalds goto bus_drivers_fail; 8856dcec251SGreg Kroah-Hartman } 886465c7a3aSmochel@digitalimplant.org 887ca22e56dSKay Sievers INIT_LIST_HEAD(&priv->interfaces); 88837e98d9bSGreg Kroah-Hartman key = &priv->lock_key; 88937e98d9bSGreg Kroah-Hartman lockdep_register_key(key); 890ca22e56dSKay Sievers __mutex_init(&priv->mutex, "subsys mutex", key); 891c6f7e72aSGreg Kroah-Hartman klist_init(&priv->klist_devices, klist_devices_get, klist_devices_put); 892c6f7e72aSGreg Kroah-Hartman klist_init(&priv->klist_drivers, NULL, NULL); 893b8c5cec2SKay Sievers 894b8c5cec2SKay Sievers retval = add_probe_files(bus); 895b8c5cec2SKay Sievers if (retval) 896b8c5cec2SKay Sievers goto bus_probe_files_fail; 897b8c5cec2SKay Sievers 8983465e2e4SGreg Kroah-Hartman retval = sysfs_create_groups(bus_kobj, bus->bus_groups); 89912478ba0SGreg Kroah-Hartman if (retval) 90012478ba0SGreg Kroah-Hartman goto bus_groups_fail; 9011da177e4SLinus Torvalds 9027dc72b28SGreg Kroah-Hartman pr_debug("bus: '%s': registered\n", bus->name); 9031da177e4SLinus Torvalds return 0; 9041da177e4SLinus Torvalds 90512478ba0SGreg Kroah-Hartman bus_groups_fail: 906b8c5cec2SKay Sievers remove_probe_files(bus); 907b8c5cec2SKay Sievers bus_probe_files_fail: 9083465e2e4SGreg Kroah-Hartman kset_unregister(priv->drivers_kset); 9091da177e4SLinus Torvalds bus_drivers_fail: 9103465e2e4SGreg Kroah-Hartman kset_unregister(priv->devices_kset); 9111da177e4SLinus Torvalds bus_devices_fail: 9127ac1cf4aSKay Sievers bus_remove_file(bus, &bus_attr_uevent); 9137ac1cf4aSKay Sievers bus_uevent_fail: 9143465e2e4SGreg Kroah-Hartman kset_unregister(&priv->subsys); 9151da177e4SLinus Torvalds out: 9163465e2e4SGreg Kroah-Hartman kfree(priv); 9171da177e4SLinus Torvalds return retval; 9181da177e4SLinus Torvalds } 919be871b7eSMichal Hocko EXPORT_SYMBOL_GPL(bus_register); 9201da177e4SLinus Torvalds 9211da177e4SLinus Torvalds /** 9221da177e4SLinus Torvalds * bus_unregister - remove a bus from the system 9231da177e4SLinus Torvalds * @bus: bus. 9241da177e4SLinus Torvalds * 9251da177e4SLinus Torvalds * Unregister the child subsystems and the bus itself. 926fc1ede58SGreg Kroah-Hartman * Finally, we call bus_put() to release the refcount 9271da177e4SLinus Torvalds */ 9281da177e4SLinus Torvalds void bus_unregister(struct bus_type *bus) 9291da177e4SLinus Torvalds { 9303465e2e4SGreg Kroah-Hartman struct subsys_private *sp = bus_to_subsys(bus); 9313465e2e4SGreg Kroah-Hartman struct kobject *bus_kobj; 9323465e2e4SGreg Kroah-Hartman 9333465e2e4SGreg Kroah-Hartman if (!sp) 9343465e2e4SGreg Kroah-Hartman return; 9353465e2e4SGreg Kroah-Hartman 9367dc72b28SGreg Kroah-Hartman pr_debug("bus: '%s': unregistering\n", bus->name); 937ca22e56dSKay Sievers if (bus->dev_root) 938ca22e56dSKay Sievers device_unregister(bus->dev_root); 9393465e2e4SGreg Kroah-Hartman 9403465e2e4SGreg Kroah-Hartman bus_kobj = &sp->subsys.kobj; 9413465e2e4SGreg Kroah-Hartman sysfs_remove_groups(bus_kobj, bus->bus_groups); 942b8c5cec2SKay Sievers remove_probe_files(bus); 9437ac1cf4aSKay Sievers bus_remove_file(bus, &bus_attr_uevent); 9443465e2e4SGreg Kroah-Hartman 9453465e2e4SGreg Kroah-Hartman kset_unregister(sp->drivers_kset); 9463465e2e4SGreg Kroah-Hartman kset_unregister(sp->devices_kset); 9473465e2e4SGreg Kroah-Hartman kset_unregister(&sp->subsys); 9483465e2e4SGreg Kroah-Hartman subsys_put(sp); 9491da177e4SLinus Torvalds } 9504a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_unregister); 9511da177e4SLinus Torvalds 952bc8b7931SGreg Kroah-Hartman int bus_register_notifier(const struct bus_type *bus, struct notifier_block *nb) 953116af378SBenjamin Herrenschmidt { 95432a8121aSGreg Kroah-Hartman struct subsys_private *sp = bus_to_subsys(bus); 95532a8121aSGreg Kroah-Hartman int retval; 95632a8121aSGreg Kroah-Hartman 95732a8121aSGreg Kroah-Hartman if (!sp) 95832a8121aSGreg Kroah-Hartman return -EINVAL; 95932a8121aSGreg Kroah-Hartman 96032a8121aSGreg Kroah-Hartman retval = blocking_notifier_chain_register(&sp->bus_notifier, nb); 96132a8121aSGreg Kroah-Hartman subsys_put(sp); 96232a8121aSGreg Kroah-Hartman return retval; 963116af378SBenjamin Herrenschmidt } 964116af378SBenjamin Herrenschmidt EXPORT_SYMBOL_GPL(bus_register_notifier); 965116af378SBenjamin Herrenschmidt 966bc8b7931SGreg Kroah-Hartman int bus_unregister_notifier(const struct bus_type *bus, struct notifier_block *nb) 967116af378SBenjamin Herrenschmidt { 96832a8121aSGreg Kroah-Hartman struct subsys_private *sp = bus_to_subsys(bus); 96932a8121aSGreg Kroah-Hartman int retval; 97032a8121aSGreg Kroah-Hartman 97132a8121aSGreg Kroah-Hartman if (!sp) 97232a8121aSGreg Kroah-Hartman return -EINVAL; 97332a8121aSGreg Kroah-Hartman retval = blocking_notifier_chain_unregister(&sp->bus_notifier, nb); 97432a8121aSGreg Kroah-Hartman subsys_put(sp); 97532a8121aSGreg Kroah-Hartman return retval; 976116af378SBenjamin Herrenschmidt } 977116af378SBenjamin Herrenschmidt EXPORT_SYMBOL_GPL(bus_unregister_notifier); 978116af378SBenjamin Herrenschmidt 979ed9f9181SGreg Kroah-Hartman void bus_notify(struct device *dev, enum bus_notifier_event value) 980ed9f9181SGreg Kroah-Hartman { 98132a8121aSGreg Kroah-Hartman struct subsys_private *sp = bus_to_subsys(dev->bus); 982ed9f9181SGreg Kroah-Hartman 98332a8121aSGreg Kroah-Hartman if (!sp) 98432a8121aSGreg Kroah-Hartman return; 98532a8121aSGreg Kroah-Hartman 98632a8121aSGreg Kroah-Hartman blocking_notifier_call_chain(&sp->bus_notifier, value, dev); 98732a8121aSGreg Kroah-Hartman subsys_put(sp); 988ed9f9181SGreg Kroah-Hartman } 989ed9f9181SGreg Kroah-Hartman 990f91482beSGreg Kroah-Hartman struct kset *bus_get_kset(const struct bus_type *bus) 9910fed80f7SGreg Kroah-Hartman { 992beea7892SGreg Kroah-Hartman struct subsys_private *sp = bus_to_subsys(bus); 993beea7892SGreg Kroah-Hartman struct kset *kset; 994beea7892SGreg Kroah-Hartman 995beea7892SGreg Kroah-Hartman if (!sp) 996beea7892SGreg Kroah-Hartman return NULL; 997beea7892SGreg Kroah-Hartman 998beea7892SGreg Kroah-Hartman kset = &sp->subsys; 999beea7892SGreg Kroah-Hartman subsys_put(sp); 1000beea7892SGreg Kroah-Hartman 1001beea7892SGreg Kroah-Hartman return kset; 10020fed80f7SGreg Kroah-Hartman } 10030fed80f7SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_get_kset); 10040fed80f7SGreg Kroah-Hartman 100599178b03SGreg Kroah-Hartman /* 1006dca25ebdSRobert P. J. Day * Yes, this forcibly breaks the klist abstraction temporarily. It 100799178b03SGreg Kroah-Hartman * just wants to sort the klist, not change reference counts and 100899178b03SGreg Kroah-Hartman * take/drop locks rapidly in the process. It does all this while 100999178b03SGreg Kroah-Hartman * holding the lock for the list, so objects can't otherwise be 101099178b03SGreg Kroah-Hartman * added/removed while we're swizzling. 101199178b03SGreg Kroah-Hartman */ 101299178b03SGreg Kroah-Hartman static void device_insertion_sort_klist(struct device *a, struct list_head *list, 101399178b03SGreg Kroah-Hartman int (*compare)(const struct device *a, 101499178b03SGreg Kroah-Hartman const struct device *b)) 101599178b03SGreg Kroah-Hartman { 101699178b03SGreg Kroah-Hartman struct klist_node *n; 1017ae1b4171SGreg Kroah-Hartman struct device_private *dev_prv; 101899178b03SGreg Kroah-Hartman struct device *b; 101999178b03SGreg Kroah-Hartman 10204c62785eSGeliang Tang list_for_each_entry(n, list, n_node) { 1021ae1b4171SGreg Kroah-Hartman dev_prv = to_device_private_bus(n); 1022ae1b4171SGreg Kroah-Hartman b = dev_prv->device; 102399178b03SGreg Kroah-Hartman if (compare(a, b) <= 0) { 1024ae1b4171SGreg Kroah-Hartman list_move_tail(&a->p->knode_bus.n_node, 1025ae1b4171SGreg Kroah-Hartman &b->p->knode_bus.n_node); 102699178b03SGreg Kroah-Hartman return; 102799178b03SGreg Kroah-Hartman } 102899178b03SGreg Kroah-Hartman } 1029ae1b4171SGreg Kroah-Hartman list_move_tail(&a->p->knode_bus.n_node, list); 103099178b03SGreg Kroah-Hartman } 103199178b03SGreg Kroah-Hartman 103299178b03SGreg Kroah-Hartman void bus_sort_breadthfirst(struct bus_type *bus, 103399178b03SGreg Kroah-Hartman int (*compare)(const struct device *a, 103499178b03SGreg Kroah-Hartman const struct device *b)) 103599178b03SGreg Kroah-Hartman { 1036b5aaecb8SGreg Kroah-Hartman struct subsys_private *sp = bus_to_subsys(bus); 103799178b03SGreg Kroah-Hartman LIST_HEAD(sorted_devices); 10384c62785eSGeliang Tang struct klist_node *n, *tmp; 1039ae1b4171SGreg Kroah-Hartman struct device_private *dev_prv; 104099178b03SGreg Kroah-Hartman struct device *dev; 104199178b03SGreg Kroah-Hartman struct klist *device_klist; 104299178b03SGreg Kroah-Hartman 1043b5aaecb8SGreg Kroah-Hartman if (!sp) 1044b5aaecb8SGreg Kroah-Hartman return; 1045b5aaecb8SGreg Kroah-Hartman device_klist = &sp->klist_devices; 104699178b03SGreg Kroah-Hartman 104799178b03SGreg Kroah-Hartman spin_lock(&device_klist->k_lock); 10484c62785eSGeliang Tang list_for_each_entry_safe(n, tmp, &device_klist->k_list, n_node) { 1049ae1b4171SGreg Kroah-Hartman dev_prv = to_device_private_bus(n); 1050ae1b4171SGreg Kroah-Hartman dev = dev_prv->device; 105199178b03SGreg Kroah-Hartman device_insertion_sort_klist(dev, &sorted_devices, compare); 105299178b03SGreg Kroah-Hartman } 105399178b03SGreg Kroah-Hartman list_splice(&sorted_devices, &device_klist->k_list); 105499178b03SGreg Kroah-Hartman spin_unlock(&device_klist->k_lock); 1055b5aaecb8SGreg Kroah-Hartman subsys_put(sp); 105699178b03SGreg Kroah-Hartman } 105799178b03SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_sort_breadthfirst); 105899178b03SGreg Kroah-Hartman 1059b0a8a59aSGreg Kroah-Hartman struct subsys_dev_iter { 1060b0a8a59aSGreg Kroah-Hartman struct klist_iter ki; 1061b0a8a59aSGreg Kroah-Hartman const struct device_type *type; 1062b0a8a59aSGreg Kroah-Hartman }; 1063b0a8a59aSGreg Kroah-Hartman 1064ca22e56dSKay Sievers /** 1065ca22e56dSKay Sievers * subsys_dev_iter_init - initialize subsys device iterator 1066ca22e56dSKay Sievers * @iter: subsys iterator to initialize 1067adac0375SGreg Kroah-Hartman * @sp: the subsys private (i.e. bus) we wanna iterate over 1068ca22e56dSKay Sievers * @start: the device to start iterating from, if any 1069ca22e56dSKay Sievers * @type: device_type of the devices to iterate over, NULL for all 1070ca22e56dSKay Sievers * 1071ca22e56dSKay Sievers * Initialize subsys iterator @iter such that it iterates over devices 1072ca22e56dSKay Sievers * of @subsys. If @start is set, the list iteration will start there, 1073ca22e56dSKay Sievers * otherwise if it is NULL, the iteration starts at the beginning of 1074ca22e56dSKay Sievers * the list. 1075ca22e56dSKay Sievers */ 1076adac0375SGreg Kroah-Hartman static void subsys_dev_iter_init(struct subsys_dev_iter *iter, struct subsys_private *sp, 1077ca22e56dSKay Sievers struct device *start, const struct device_type *type) 1078ca22e56dSKay Sievers { 1079ca22e56dSKay Sievers struct klist_node *start_knode = NULL; 1080ca22e56dSKay Sievers 1081ca22e56dSKay Sievers if (start) 1082ca22e56dSKay Sievers start_knode = &start->p->knode_bus; 1083adac0375SGreg Kroah-Hartman klist_iter_init_node(&sp->klist_devices, &iter->ki, start_knode); 1084ca22e56dSKay Sievers iter->type = type; 1085ca22e56dSKay Sievers } 1086ca22e56dSKay Sievers 1087ca22e56dSKay Sievers /** 1088ca22e56dSKay Sievers * subsys_dev_iter_next - iterate to the next device 1089ca22e56dSKay Sievers * @iter: subsys iterator to proceed 1090ca22e56dSKay Sievers * 1091ca22e56dSKay Sievers * Proceed @iter to the next device and return it. Returns NULL if 1092ca22e56dSKay Sievers * iteration is complete. 1093ca22e56dSKay Sievers * 1094ca22e56dSKay Sievers * The returned device is referenced and won't be released till 1095ca22e56dSKay Sievers * iterator is proceed to the next device or exited. The caller is 1096ca22e56dSKay Sievers * free to do whatever it wants to do with the device including 1097ca22e56dSKay Sievers * calling back into subsys code. 1098ca22e56dSKay Sievers */ 109938cdadefSGreg Kroah-Hartman static struct device *subsys_dev_iter_next(struct subsys_dev_iter *iter) 1100ca22e56dSKay Sievers { 1101ca22e56dSKay Sievers struct klist_node *knode; 1102ca22e56dSKay Sievers struct device *dev; 1103ca22e56dSKay Sievers 1104ca22e56dSKay Sievers for (;;) { 1105ca22e56dSKay Sievers knode = klist_next(&iter->ki); 1106ca22e56dSKay Sievers if (!knode) 1107ca22e56dSKay Sievers return NULL; 1108371fd7a2SGeliang Tang dev = to_device_private_bus(knode)->device; 1109ca22e56dSKay Sievers if (!iter->type || iter->type == dev->type) 1110ca22e56dSKay Sievers return dev; 1111ca22e56dSKay Sievers } 1112ca22e56dSKay Sievers } 1113ca22e56dSKay Sievers 1114ca22e56dSKay Sievers /** 1115ca22e56dSKay Sievers * subsys_dev_iter_exit - finish iteration 1116ca22e56dSKay Sievers * @iter: subsys iterator to finish 1117ca22e56dSKay Sievers * 1118ca22e56dSKay Sievers * Finish an iteration. Always call this function after iteration is 1119ca22e56dSKay Sievers * complete whether the iteration ran till the end or not. 1120ca22e56dSKay Sievers */ 1121af6d0743SGreg Kroah-Hartman static void subsys_dev_iter_exit(struct subsys_dev_iter *iter) 1122ca22e56dSKay Sievers { 1123ca22e56dSKay Sievers klist_iter_exit(&iter->ki); 1124ca22e56dSKay Sievers } 1125ca22e56dSKay Sievers 1126ca22e56dSKay Sievers int subsys_interface_register(struct subsys_interface *sif) 1127ca22e56dSKay Sievers { 1128adac0375SGreg Kroah-Hartman struct subsys_private *sp; 1129ca22e56dSKay Sievers struct subsys_dev_iter iter; 1130ca22e56dSKay Sievers struct device *dev; 1131ca22e56dSKay Sievers 1132ca22e56dSKay Sievers if (!sif || !sif->subsys) 1133ca22e56dSKay Sievers return -ENODEV; 1134ca22e56dSKay Sievers 1135adac0375SGreg Kroah-Hartman sp = bus_to_subsys(sif->subsys); 1136adac0375SGreg Kroah-Hartman if (!sp) 1137ca22e56dSKay Sievers return -EINVAL; 1138ca22e56dSKay Sievers 1139adac0375SGreg Kroah-Hartman /* 1140adac0375SGreg Kroah-Hartman * Reference in sp is now incremented and will be dropped when 1141adac0375SGreg Kroah-Hartman * the interface is removed from the bus 1142adac0375SGreg Kroah-Hartman */ 1143adac0375SGreg Kroah-Hartman 1144adac0375SGreg Kroah-Hartman mutex_lock(&sp->mutex); 1145adac0375SGreg Kroah-Hartman list_add_tail(&sif->node, &sp->interfaces); 1146ca22e56dSKay Sievers if (sif->add_dev) { 1147adac0375SGreg Kroah-Hartman subsys_dev_iter_init(&iter, sp, NULL, NULL); 1148ca22e56dSKay Sievers while ((dev = subsys_dev_iter_next(&iter))) 1149ca22e56dSKay Sievers sif->add_dev(dev, sif); 1150ca22e56dSKay Sievers subsys_dev_iter_exit(&iter); 1151ca22e56dSKay Sievers } 1152adac0375SGreg Kroah-Hartman mutex_unlock(&sp->mutex); 1153ca22e56dSKay Sievers 1154ca22e56dSKay Sievers return 0; 1155ca22e56dSKay Sievers } 1156ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_interface_register); 1157ca22e56dSKay Sievers 1158ca22e56dSKay Sievers void subsys_interface_unregister(struct subsys_interface *sif) 1159ca22e56dSKay Sievers { 1160adac0375SGreg Kroah-Hartman struct subsys_private *sp; 1161ca22e56dSKay Sievers struct subsys_dev_iter iter; 1162ca22e56dSKay Sievers struct device *dev; 1163ca22e56dSKay Sievers 11642b31594aSJonghwan Choi if (!sif || !sif->subsys) 1165ca22e56dSKay Sievers return; 1166ca22e56dSKay Sievers 1167adac0375SGreg Kroah-Hartman sp = bus_to_subsys(sif->subsys); 1168adac0375SGreg Kroah-Hartman if (!sp) 1169adac0375SGreg Kroah-Hartman return; 11702b31594aSJonghwan Choi 1171adac0375SGreg Kroah-Hartman mutex_lock(&sp->mutex); 1172ca22e56dSKay Sievers list_del_init(&sif->node); 1173ca22e56dSKay Sievers if (sif->remove_dev) { 1174adac0375SGreg Kroah-Hartman subsys_dev_iter_init(&iter, sp, NULL, NULL); 1175ca22e56dSKay Sievers while ((dev = subsys_dev_iter_next(&iter))) 1176ca22e56dSKay Sievers sif->remove_dev(dev, sif); 1177ca22e56dSKay Sievers subsys_dev_iter_exit(&iter); 1178ca22e56dSKay Sievers } 1179adac0375SGreg Kroah-Hartman mutex_unlock(&sp->mutex); 1180ca22e56dSKay Sievers 1181adac0375SGreg Kroah-Hartman /* 1182adac0375SGreg Kroah-Hartman * Decrement the reference count twice, once for the bus_to_subsys() 1183adac0375SGreg Kroah-Hartman * call in the start of this function, and the second one from the 1184adac0375SGreg Kroah-Hartman * reference increment in subsys_interface_register() 1185adac0375SGreg Kroah-Hartman */ 1186adac0375SGreg Kroah-Hartman subsys_put(sp); 1187adac0375SGreg Kroah-Hartman subsys_put(sp); 1188ca22e56dSKay Sievers } 1189ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_interface_unregister); 1190ca22e56dSKay Sievers 1191ca22e56dSKay Sievers static void system_root_device_release(struct device *dev) 1192ca22e56dSKay Sievers { 1193ca22e56dSKay Sievers kfree(dev); 1194ca22e56dSKay Sievers } 1195d73ce004STejun Heo 1196d73ce004STejun Heo static int subsys_register(struct bus_type *subsys, 1197d73ce004STejun Heo const struct attribute_group **groups, 1198d73ce004STejun Heo struct kobject *parent_of_root) 1199d73ce004STejun Heo { 1200d73ce004STejun Heo struct device *dev; 1201d73ce004STejun Heo int err; 1202d73ce004STejun Heo 1203d73ce004STejun Heo err = bus_register(subsys); 1204d73ce004STejun Heo if (err < 0) 1205d73ce004STejun Heo return err; 1206d73ce004STejun Heo 1207d73ce004STejun Heo dev = kzalloc(sizeof(struct device), GFP_KERNEL); 1208d73ce004STejun Heo if (!dev) { 1209d73ce004STejun Heo err = -ENOMEM; 1210d73ce004STejun Heo goto err_dev; 1211d73ce004STejun Heo } 1212d73ce004STejun Heo 1213d73ce004STejun Heo err = dev_set_name(dev, "%s", subsys->name); 1214d73ce004STejun Heo if (err < 0) 1215d73ce004STejun Heo goto err_name; 1216d73ce004STejun Heo 1217d73ce004STejun Heo dev->kobj.parent = parent_of_root; 1218d73ce004STejun Heo dev->groups = groups; 1219d73ce004STejun Heo dev->release = system_root_device_release; 1220d73ce004STejun Heo 1221d73ce004STejun Heo err = device_register(dev); 1222d73ce004STejun Heo if (err < 0) 1223d73ce004STejun Heo goto err_dev_reg; 1224d73ce004STejun Heo 1225d73ce004STejun Heo subsys->dev_root = dev; 1226d73ce004STejun Heo return 0; 1227d73ce004STejun Heo 1228d73ce004STejun Heo err_dev_reg: 1229d73ce004STejun Heo put_device(dev); 1230d73ce004STejun Heo dev = NULL; 1231d73ce004STejun Heo err_name: 1232d73ce004STejun Heo kfree(dev); 1233d73ce004STejun Heo err_dev: 1234d73ce004STejun Heo bus_unregister(subsys); 1235d73ce004STejun Heo return err; 1236d73ce004STejun Heo } 1237d73ce004STejun Heo 1238ca22e56dSKay Sievers /** 1239ca22e56dSKay Sievers * subsys_system_register - register a subsystem at /sys/devices/system/ 124078d79559SRandy Dunlap * @subsys: system subsystem 124178d79559SRandy Dunlap * @groups: default attributes for the root device 1242ca22e56dSKay Sievers * 1243ca22e56dSKay Sievers * All 'system' subsystems have a /sys/devices/system/<name> root device 1244ca22e56dSKay Sievers * with the name of the subsystem. The root device can carry subsystem- 1245ca22e56dSKay Sievers * wide attributes. All registered devices are below this single root 1246ca22e56dSKay Sievers * device and are named after the subsystem with a simple enumeration 1247e227867fSMasanari Iida * number appended. The registered devices are not explicitly named; 1248ca22e56dSKay Sievers * only 'id' in the device needs to be set. 1249ca22e56dSKay Sievers * 1250ca22e56dSKay Sievers * Do not use this interface for anything new, it exists for compatibility 1251ca22e56dSKay Sievers * with bad ideas only. New subsystems should use plain subsystems; and 1252ca22e56dSKay Sievers * add the subsystem-wide attributes should be added to the subsystem 1253ca22e56dSKay Sievers * directory itself and not some create fake root-device placed in 1254ca22e56dSKay Sievers * /sys/devices/system/<name>. 1255ca22e56dSKay Sievers */ 1256ca22e56dSKay Sievers int subsys_system_register(struct bus_type *subsys, 1257ca22e56dSKay Sievers const struct attribute_group **groups) 1258ca22e56dSKay Sievers { 1259d73ce004STejun Heo return subsys_register(subsys, groups, &system_kset->kobj); 1260ca22e56dSKay Sievers } 1261ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_system_register); 1262ca22e56dSKay Sievers 1263d73ce004STejun Heo /** 1264d73ce004STejun Heo * subsys_virtual_register - register a subsystem at /sys/devices/virtual/ 1265d73ce004STejun Heo * @subsys: virtual subsystem 1266d73ce004STejun Heo * @groups: default attributes for the root device 1267d73ce004STejun Heo * 1268d73ce004STejun Heo * All 'virtual' subsystems have a /sys/devices/system/<name> root device 1269d73ce004STejun Heo * with the name of the subystem. The root device can carry subsystem-wide 1270d73ce004STejun Heo * attributes. All registered devices are below this single root device. 1271d73ce004STejun Heo * There's no restriction on device naming. This is for kernel software 1272d73ce004STejun Heo * constructs which need sysfs interface. 1273d73ce004STejun Heo */ 1274d73ce004STejun Heo int subsys_virtual_register(struct bus_type *subsys, 1275d73ce004STejun Heo const struct attribute_group **groups) 1276d73ce004STejun Heo { 1277d73ce004STejun Heo struct kobject *virtual_dir; 1278d73ce004STejun Heo 1279d73ce004STejun Heo virtual_dir = virtual_device_parent(NULL); 1280d73ce004STejun Heo if (!virtual_dir) 1281d73ce004STejun Heo return -ENOMEM; 1282d73ce004STejun Heo 1283d73ce004STejun Heo return subsys_register(subsys, groups, virtual_dir); 1284d73ce004STejun Heo } 12851c04fc35SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(subsys_virtual_register); 1286d73ce004STejun Heo 1287adc18506SGreg Kroah-Hartman /** 1288adc18506SGreg Kroah-Hartman * driver_find - locate driver on a bus by its name. 1289adc18506SGreg Kroah-Hartman * @name: name of the driver. 1290adc18506SGreg Kroah-Hartman * @bus: bus to scan for the driver. 1291adc18506SGreg Kroah-Hartman * 1292adc18506SGreg Kroah-Hartman * Call kset_find_obj() to iterate over list of drivers on 1293adc18506SGreg Kroah-Hartman * a bus to find driver by name. Return driver if found. 1294adc18506SGreg Kroah-Hartman * 1295adc18506SGreg Kroah-Hartman * This routine provides no locking to prevent the driver it returns 1296adc18506SGreg Kroah-Hartman * from being unregistered or unloaded while the caller is using it. 1297adc18506SGreg Kroah-Hartman * The caller is responsible for preventing this. 1298adc18506SGreg Kroah-Hartman */ 1299adc18506SGreg Kroah-Hartman struct device_driver *driver_find(const char *name, struct bus_type *bus) 1300adc18506SGreg Kroah-Hartman { 1301fb451966SGreg Kroah-Hartman struct subsys_private *sp = bus_to_subsys(bus); 1302fb451966SGreg Kroah-Hartman struct kobject *k; 1303adc18506SGreg Kroah-Hartman struct driver_private *priv; 1304adc18506SGreg Kroah-Hartman 1305fb451966SGreg Kroah-Hartman if (!sp) 1306fb451966SGreg Kroah-Hartman return NULL; 1307fb451966SGreg Kroah-Hartman 1308fb451966SGreg Kroah-Hartman k = kset_find_obj(sp->drivers_kset, name); 1309fb451966SGreg Kroah-Hartman subsys_put(sp); 1310fb451966SGreg Kroah-Hartman if (!k) 1311fb451966SGreg Kroah-Hartman return NULL; 1312fb451966SGreg Kroah-Hartman 1313fb451966SGreg Kroah-Hartman priv = to_driver(k); 1314fb451966SGreg Kroah-Hartman 1315adc18506SGreg Kroah-Hartman /* Drop reference added by kset_find_obj() */ 1316adc18506SGreg Kroah-Hartman kobject_put(k); 1317adc18506SGreg Kroah-Hartman return priv->driver; 1318adc18506SGreg Kroah-Hartman } 1319adc18506SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(driver_find); 1320adc18506SGreg Kroah-Hartman 132163b823d7SGreg Kroah-Hartman /* 132263b823d7SGreg Kroah-Hartman * Warning, the value could go to "removed" instantly after calling this function, so be very 132363b823d7SGreg Kroah-Hartman * careful when calling it... 132463b823d7SGreg Kroah-Hartman */ 132563b823d7SGreg Kroah-Hartman bool bus_is_registered(const struct bus_type *bus) 132663b823d7SGreg Kroah-Hartman { 132763b823d7SGreg Kroah-Hartman struct subsys_private *sp = bus_to_subsys(bus); 132863b823d7SGreg Kroah-Hartman bool is_initialized = false; 132963b823d7SGreg Kroah-Hartman 133063b823d7SGreg Kroah-Hartman if (sp) { 133163b823d7SGreg Kroah-Hartman is_initialized = true; 133263b823d7SGreg Kroah-Hartman subsys_put(sp); 133363b823d7SGreg Kroah-Hartman } 133463b823d7SGreg Kroah-Hartman return is_initialized; 133563b823d7SGreg Kroah-Hartman } 133663b823d7SGreg Kroah-Hartman 13371da177e4SLinus Torvalds int __init buses_init(void) 13381da177e4SLinus Torvalds { 133959a54833SGreg Kroah-Hartman bus_kset = kset_create_and_add("bus", &bus_uevent_ops, NULL); 134059a54833SGreg Kroah-Hartman if (!bus_kset) 134159a54833SGreg Kroah-Hartman return -ENOMEM; 1342ca22e56dSKay Sievers 1343ca22e56dSKay Sievers system_kset = kset_create_and_add("system", NULL, &devices_kset->kobj); 1344ca22e56dSKay Sievers if (!system_kset) 1345ca22e56dSKay Sievers return -ENOMEM; 1346ca22e56dSKay Sievers 134759a54833SGreg Kroah-Hartman return 0; 13481da177e4SLinus Torvalds } 1349