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 struct bus_type *bus = priv->bus; 209174be70bSBart Van Assche 21037e98d9bSGreg Kroah-Hartman lockdep_unregister_key(&priv->lock_key); 211174be70bSBart Van Assche kfree(priv); 212174be70bSBart Van Assche bus->p = NULL; 213174be70bSBart Van Assche } 214174be70bSBart Van Assche 215c83d9ab4SThomas Weißschuh static const struct kobj_type bus_ktype = { 2161da177e4SLinus Torvalds .sysfs_ops = &bus_sysfs_ops, 217174be70bSBart Van Assche .release = bus_release, 2181da177e4SLinus Torvalds }; 2191da177e4SLinus Torvalds 220c45a88bbSGreg Kroah-Hartman static int bus_uevent_filter(const struct kobject *kobj) 22180f03e34SKay Sievers { 222ee6d3dd4SWedson Almeida Filho const struct kobj_type *ktype = get_ktype(kobj); 22380f03e34SKay Sievers 22480f03e34SKay Sievers if (ktype == &bus_ktype) 22580f03e34SKay Sievers return 1; 22680f03e34SKay Sievers return 0; 22780f03e34SKay Sievers } 22880f03e34SKay Sievers 2299cd43611SEmese Revfy static const struct kset_uevent_ops bus_uevent_ops = { 23080f03e34SKay Sievers .filter = bus_uevent_filter, 23180f03e34SKay Sievers }; 23280f03e34SKay Sievers 2332b08c8d0SAlan Stern /* Manually detach a device from its associated driver. */ 2342581c9ccSGreg Kroah-Hartman static ssize_t unbind_store(struct device_driver *drv, const char *buf, 2352581c9ccSGreg Kroah-Hartman size_t count) 236151ef38fSGreg Kroah-Hartman { 2375901d014SGreg Kroah-Hartman struct bus_type *bus = bus_get(drv->bus); 238151ef38fSGreg Kroah-Hartman struct device *dev; 239151ef38fSGreg Kroah-Hartman int err = -ENODEV; 240151ef38fSGreg Kroah-Hartman 2411f9ffc04SGreg Kroah-Hartman dev = bus_find_device_by_name(bus, NULL, buf); 2422b08c8d0SAlan Stern if (dev && dev->driver == drv) { 243ed88747cSAlexander Duyck device_driver_detach(dev); 244151ef38fSGreg Kroah-Hartman err = count; 245151ef38fSGreg Kroah-Hartman } 2462b08c8d0SAlan Stern put_device(dev); 247fc1ede58SGreg Kroah-Hartman bus_put(bus); 248151ef38fSGreg Kroah-Hartman return err; 249151ef38fSGreg Kroah-Hartman } 25016b0dd40SJinchao Wang static DRIVER_ATTR_IGNORE_LOCKDEP(unbind, 0200, NULL, unbind_store); 251151ef38fSGreg Kroah-Hartman 252afdce75fSGreg Kroah-Hartman /* 253afdce75fSGreg Kroah-Hartman * Manually attach a device to a driver. 254afdce75fSGreg Kroah-Hartman * Note: the driver must want to bind to the device, 255afdce75fSGreg Kroah-Hartman * it is not possible to override the driver's id table. 256afdce75fSGreg Kroah-Hartman */ 2572581c9ccSGreg Kroah-Hartman static ssize_t bind_store(struct device_driver *drv, const char *buf, 2582581c9ccSGreg Kroah-Hartman size_t count) 259afdce75fSGreg Kroah-Hartman { 2605901d014SGreg Kroah-Hartman struct bus_type *bus = bus_get(drv->bus); 261afdce75fSGreg Kroah-Hartman struct device *dev; 262afdce75fSGreg Kroah-Hartman int err = -ENODEV; 263afdce75fSGreg Kroah-Hartman 2641f9ffc04SGreg Kroah-Hartman dev = bus_find_device_by_name(bus, NULL, buf); 265204db60cSJason Gunthorpe if (dev && driver_match_device(drv, dev)) { 266ed88747cSAlexander Duyck err = device_driver_attach(drv, dev); 267ef6dcbddSChristoph Hellwig if (!err) { 2684a3ad20cSGreg Kroah-Hartman /* success */ 26937225401SRyan Wilson err = count; 270afdce75fSGreg Kroah-Hartman } 2714a3ad20cSGreg Kroah-Hartman } 2722b08c8d0SAlan Stern put_device(dev); 273fc1ede58SGreg Kroah-Hartman bus_put(bus); 274afdce75fSGreg Kroah-Hartman return err; 275afdce75fSGreg Kroah-Hartman } 27616b0dd40SJinchao Wang static DRIVER_ATTR_IGNORE_LOCKDEP(bind, 0200, NULL, bind_store); 277afdce75fSGreg Kroah-Hartman 2782e7189b6SGreg Kroah-Hartman static ssize_t drivers_autoprobe_show(struct bus_type *bus, char *buf) 279b8c5cec2SKay Sievers { 280*a00fdb98SGreg Kroah-Hartman struct subsys_private *sp = bus_to_subsys(bus); 281*a00fdb98SGreg Kroah-Hartman int ret; 282*a00fdb98SGreg Kroah-Hartman 283*a00fdb98SGreg Kroah-Hartman if (!sp) 284*a00fdb98SGreg Kroah-Hartman return -EINVAL; 285*a00fdb98SGreg Kroah-Hartman 286*a00fdb98SGreg Kroah-Hartman ret = sysfs_emit(buf, "%d\n", sp->drivers_autoprobe); 287*a00fdb98SGreg Kroah-Hartman subsys_put(sp); 288*a00fdb98SGreg Kroah-Hartman return ret; 289b8c5cec2SKay Sievers } 290b8c5cec2SKay Sievers 2912e7189b6SGreg Kroah-Hartman static ssize_t drivers_autoprobe_store(struct bus_type *bus, 292b8c5cec2SKay Sievers const char *buf, size_t count) 293b8c5cec2SKay Sievers { 294*a00fdb98SGreg Kroah-Hartman struct subsys_private *sp = bus_to_subsys(bus); 295*a00fdb98SGreg Kroah-Hartman 296*a00fdb98SGreg Kroah-Hartman if (!sp) 297*a00fdb98SGreg Kroah-Hartman return -EINVAL; 298*a00fdb98SGreg Kroah-Hartman 299b8c5cec2SKay Sievers if (buf[0] == '0') 300*a00fdb98SGreg Kroah-Hartman sp->drivers_autoprobe = 0; 301b8c5cec2SKay Sievers else 302*a00fdb98SGreg Kroah-Hartman sp->drivers_autoprobe = 1; 303*a00fdb98SGreg Kroah-Hartman 304*a00fdb98SGreg Kroah-Hartman subsys_put(sp); 305b8c5cec2SKay Sievers return count; 306b8c5cec2SKay Sievers } 307b8c5cec2SKay Sievers 3082e7189b6SGreg Kroah-Hartman static ssize_t drivers_probe_store(struct bus_type *bus, 309b8c5cec2SKay Sievers const char *buf, size_t count) 310b8c5cec2SKay Sievers { 311b8c5cec2SKay Sievers struct device *dev; 3120372ffb3SAlex Williamson int err = -EINVAL; 313b8c5cec2SKay Sievers 3141f9ffc04SGreg Kroah-Hartman dev = bus_find_device_by_name(bus, NULL, buf); 315b8c5cec2SKay Sievers if (!dev) 316b8c5cec2SKay Sievers return -ENODEV; 3170372ffb3SAlex Williamson if (bus_rescan_devices_helper(dev, NULL) == 0) 3180372ffb3SAlex Williamson err = count; 3190372ffb3SAlex Williamson put_device(dev); 3200372ffb3SAlex Williamson return err; 321b8c5cec2SKay Sievers } 322151ef38fSGreg Kroah-Hartman 323465c7a3aSmochel@digitalimplant.org static struct device *next_device(struct klist_iter *i) 324465c7a3aSmochel@digitalimplant.org { 325465c7a3aSmochel@digitalimplant.org struct klist_node *n = klist_next(i); 326ae1b4171SGreg Kroah-Hartman struct device *dev = NULL; 327ae1b4171SGreg Kroah-Hartman struct device_private *dev_prv; 328ae1b4171SGreg Kroah-Hartman 329ae1b4171SGreg Kroah-Hartman if (n) { 330ae1b4171SGreg Kroah-Hartman dev_prv = to_device_private_bus(n); 331ae1b4171SGreg Kroah-Hartman dev = dev_prv->device; 332ae1b4171SGreg Kroah-Hartman } 333ae1b4171SGreg Kroah-Hartman return dev; 334465c7a3aSmochel@digitalimplant.org } 335465c7a3aSmochel@digitalimplant.org 3361da177e4SLinus Torvalds /** 3371da177e4SLinus Torvalds * bus_for_each_dev - device iterator. 3381da177e4SLinus Torvalds * @bus: bus type. 3391da177e4SLinus Torvalds * @start: device to start iterating from. 3401da177e4SLinus Torvalds * @data: data for the callback. 3411da177e4SLinus Torvalds * @fn: function to be called for each device. 3421da177e4SLinus Torvalds * 3431da177e4SLinus Torvalds * Iterate over @bus's list of devices, and call @fn for each, 3441da177e4SLinus Torvalds * passing it @data. If @start is not NULL, we use that device to 3451da177e4SLinus Torvalds * begin iterating from. 3461da177e4SLinus Torvalds * 3471da177e4SLinus Torvalds * We check the return of @fn each time. If it returns anything 3481da177e4SLinus Torvalds * other than 0, we break out and return that value. 3491da177e4SLinus Torvalds * 3501da177e4SLinus Torvalds * NOTE: The device that returns a non-zero value is not retained 3511da177e4SLinus Torvalds * in any way, nor is its refcount incremented. If the caller needs 3520fa1b0a1SAlex Chiang * to retain this data, it should do so, and increment the reference 3531da177e4SLinus Torvalds * count in the supplied callback. 3541da177e4SLinus Torvalds */ 355e0766ea4SGreg Kroah-Hartman int bus_for_each_dev(const struct bus_type *bus, struct device *start, 3561da177e4SLinus Torvalds void *data, int (*fn)(struct device *, void *)) 3571da177e4SLinus Torvalds { 358465c7a3aSmochel@digitalimplant.org struct klist_iter i; 359465c7a3aSmochel@digitalimplant.org struct device *dev; 360465c7a3aSmochel@digitalimplant.org int error = 0; 3611da177e4SLinus Torvalds 3624fa3e78bSBjorn Helgaas if (!bus || !bus->p) 363465c7a3aSmochel@digitalimplant.org return -EINVAL; 364465c7a3aSmochel@digitalimplant.org 3657cd9c9bbSGreg Kroah-Hartman klist_iter_init_node(&bus->p->klist_devices, &i, 366ae1b4171SGreg Kroah-Hartman (start ? &start->p->knode_bus : NULL)); 36793ead7c9SGimcuan Hui while (!error && (dev = next_device(&i))) 368465c7a3aSmochel@digitalimplant.org error = fn(dev, data); 369465c7a3aSmochel@digitalimplant.org klist_iter_exit(&i); 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 { 3930edb5860SCornelia Huck struct klist_iter i; 3940edb5860SCornelia Huck struct device *dev; 3950edb5860SCornelia Huck 3964fa3e78bSBjorn Helgaas if (!bus || !bus->p) 3970edb5860SCornelia Huck return NULL; 3980edb5860SCornelia Huck 3997cd9c9bbSGreg Kroah-Hartman klist_iter_init_node(&bus->p->klist_devices, &i, 4007cd9c9bbSGreg Kroah-Hartman (start ? &start->p->knode_bus : NULL)); 4010edb5860SCornelia Huck while ((dev = next_device(&i))) 4020edb5860SCornelia Huck if (match(dev, data) && get_device(dev)) 4030edb5860SCornelia Huck break; 4040edb5860SCornelia Huck klist_iter_exit(&i); 4050edb5860SCornelia Huck return dev; 4060edb5860SCornelia Huck } 4074a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_find_device); 40838fdac3cSmochel@digitalimplant.org 40938fdac3cSmochel@digitalimplant.org static struct device_driver *next_driver(struct klist_iter *i) 41038fdac3cSmochel@digitalimplant.org { 41138fdac3cSmochel@digitalimplant.org struct klist_node *n = klist_next(i); 412e5dd1278SGreg Kroah-Hartman struct driver_private *drv_priv; 413e5dd1278SGreg Kroah-Hartman 414e5dd1278SGreg Kroah-Hartman if (n) { 415e5dd1278SGreg Kroah-Hartman drv_priv = container_of(n, struct driver_private, knode_bus); 416e5dd1278SGreg Kroah-Hartman return drv_priv->driver; 417e5dd1278SGreg Kroah-Hartman } 418e5dd1278SGreg Kroah-Hartman return NULL; 41938fdac3cSmochel@digitalimplant.org } 42038fdac3cSmochel@digitalimplant.org 4211da177e4SLinus Torvalds /** 4221da177e4SLinus Torvalds * bus_for_each_drv - driver iterator 4231da177e4SLinus Torvalds * @bus: bus we're dealing with. 4241da177e4SLinus Torvalds * @start: driver to start iterating on. 4251da177e4SLinus Torvalds * @data: data to pass to the callback. 4261da177e4SLinus Torvalds * @fn: function to call for each driver. 4271da177e4SLinus Torvalds * 4281da177e4SLinus Torvalds * This is nearly identical to the device iterator above. 4291da177e4SLinus Torvalds * We iterate over each driver that belongs to @bus, and call 4301da177e4SLinus Torvalds * @fn for each. If @fn returns anything but 0, we break out 4311da177e4SLinus Torvalds * and return it. If @start is not NULL, we use it as the head 4321da177e4SLinus Torvalds * of the list. 4331da177e4SLinus Torvalds * 4341da177e4SLinus Torvalds * NOTE: we don't return the driver that returns a non-zero 4351da177e4SLinus Torvalds * value, nor do we leave the reference count incremented for that 4361da177e4SLinus Torvalds * driver. If the caller needs to know that info, it must set it 4371da177e4SLinus Torvalds * in the callback. It must also be sure to increment the refcount 4381da177e4SLinus Torvalds * so it doesn't disappear before returning to the caller. 4391da177e4SLinus Torvalds */ 440e0766ea4SGreg Kroah-Hartman int bus_for_each_drv(const struct bus_type *bus, struct device_driver *start, 4411da177e4SLinus Torvalds void *data, int (*fn)(struct device_driver *, void *)) 4421da177e4SLinus Torvalds { 44338fdac3cSmochel@digitalimplant.org struct klist_iter i; 44438fdac3cSmochel@digitalimplant.org struct device_driver *drv; 44538fdac3cSmochel@digitalimplant.org int error = 0; 4461da177e4SLinus Torvalds 44738fdac3cSmochel@digitalimplant.org if (!bus) 44838fdac3cSmochel@digitalimplant.org return -EINVAL; 44938fdac3cSmochel@digitalimplant.org 4507cd9c9bbSGreg Kroah-Hartman klist_iter_init_node(&bus->p->klist_drivers, &i, 451e5dd1278SGreg Kroah-Hartman start ? &start->p->knode_bus : NULL); 45238fdac3cSmochel@digitalimplant.org while ((drv = next_driver(&i)) && !error) 45338fdac3cSmochel@digitalimplant.org error = fn(drv, data); 45438fdac3cSmochel@digitalimplant.org klist_iter_exit(&i); 45538fdac3cSmochel@digitalimplant.org return error; 4561da177e4SLinus Torvalds } 4574a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_for_each_drv); 4581da177e4SLinus Torvalds 4591da177e4SLinus Torvalds /** 4601da177e4SLinus Torvalds * bus_add_device - add device to bus 4611da177e4SLinus Torvalds * @dev: device being added 4621da177e4SLinus Torvalds * 4632023c610SAlan Stern * - Add device's bus attributes. 4642023c610SAlan Stern * - Create links to device's bus. 4651da177e4SLinus Torvalds * - Add the device to its bus's list of devices. 4661da177e4SLinus Torvalds */ 4671da177e4SLinus Torvalds int bus_add_device(struct device *dev) 4681da177e4SLinus Torvalds { 4695901d014SGreg Kroah-Hartman struct bus_type *bus = bus_get(dev->bus); 4701da177e4SLinus Torvalds int error = 0; 4711da177e4SLinus Torvalds 4721da177e4SLinus Torvalds if (bus) { 4731e0b2cf9SKay Sievers pr_debug("bus: '%s': add device %s\n", bus->name, dev_name(dev)); 474fa6fdb33SGreg Kroah-Hartman error = device_add_groups(dev, bus->dev_groups); 475fa6fdb33SGreg Kroah-Hartman if (error) 476b46c7337SGreg Kroah-Hartman goto out_put; 477c6f7e72aSGreg Kroah-Hartman error = sysfs_create_link(&bus->p->devices_kset->kobj, 4781e0b2cf9SKay Sievers &dev->kobj, dev_name(dev)); 479f86db396SAndrew Morton if (error) 4801c34203aSJunjie Mao goto out_groups; 481f86db396SAndrew Morton error = sysfs_create_link(&dev->kobj, 482c6f7e72aSGreg Kroah-Hartman &dev->bus->p->subsys.kobj, "subsystem"); 483f86db396SAndrew Morton if (error) 484513e7337SCornelia Huck goto out_subsys; 4852023c610SAlan Stern klist_add_tail(&dev->p->knode_bus, &bus->p->klist_devices); 4861da177e4SLinus Torvalds } 487513e7337SCornelia Huck return 0; 488513e7337SCornelia Huck 489513e7337SCornelia Huck out_subsys: 4901e0b2cf9SKay Sievers sysfs_remove_link(&bus->p->devices_kset->kobj, dev_name(dev)); 491fa6fdb33SGreg Kroah-Hartman out_groups: 492fa6fdb33SGreg Kroah-Hartman device_remove_groups(dev, bus->dev_groups); 493513e7337SCornelia Huck out_put: 494fc1ede58SGreg Kroah-Hartman bus_put(dev->bus); 4951da177e4SLinus Torvalds return error; 4961da177e4SLinus Torvalds } 4971da177e4SLinus Torvalds 4981da177e4SLinus Torvalds /** 4992023c610SAlan Stern * bus_probe_device - probe drivers for a new device 5002023c610SAlan Stern * @dev: device to probe 50153877d06SKay Sievers * 5022023c610SAlan Stern * - Automatically probe for a driver if the bus allows it. 50353877d06SKay Sievers */ 5042023c610SAlan Stern void bus_probe_device(struct device *dev) 50553877d06SKay Sievers { 50653877d06SKay Sievers struct bus_type *bus = dev->bus; 507ca22e56dSKay Sievers struct subsys_interface *sif; 50853877d06SKay Sievers 509ca22e56dSKay Sievers if (!bus) 510ca22e56dSKay Sievers return; 511ca22e56dSKay Sievers 512765230b5SDmitry Torokhov if (bus->p->drivers_autoprobe) 513765230b5SDmitry Torokhov device_initial_probe(dev); 514ca22e56dSKay Sievers 515ca22e56dSKay Sievers mutex_lock(&bus->p->mutex); 516ca22e56dSKay Sievers list_for_each_entry(sif, &bus->p->interfaces, node) 517ca22e56dSKay Sievers if (sif->add_dev) 518ca22e56dSKay Sievers sif->add_dev(dev, sif); 519ca22e56dSKay Sievers mutex_unlock(&bus->p->mutex); 520f86db396SAndrew Morton } 52153877d06SKay Sievers 52253877d06SKay Sievers /** 5231da177e4SLinus Torvalds * bus_remove_device - remove device from bus 5241da177e4SLinus Torvalds * @dev: device to be removed 5251da177e4SLinus Torvalds * 526ca22e56dSKay Sievers * - Remove device from all interfaces. 527ca22e56dSKay Sievers * - Remove symlink from bus' directory. 5281da177e4SLinus Torvalds * - Delete device from bus's list. 5291da177e4SLinus Torvalds * - Detach from its driver. 5301da177e4SLinus Torvalds * - Drop reference taken in bus_add_device(). 5311da177e4SLinus Torvalds */ 5321da177e4SLinus Torvalds void bus_remove_device(struct device *dev) 5331da177e4SLinus Torvalds { 534ca22e56dSKay Sievers struct bus_type *bus = dev->bus; 535ca22e56dSKay Sievers struct subsys_interface *sif; 536ca22e56dSKay Sievers 537ca22e56dSKay Sievers if (!bus) 538ca22e56dSKay Sievers return; 539ca22e56dSKay Sievers 540ca22e56dSKay Sievers mutex_lock(&bus->p->mutex); 541ca22e56dSKay Sievers list_for_each_entry(sif, &bus->p->interfaces, node) 542ca22e56dSKay Sievers if (sif->remove_dev) 543ca22e56dSKay Sievers sif->remove_dev(dev, sif); 544ca22e56dSKay Sievers mutex_unlock(&bus->p->mutex); 545ca22e56dSKay Sievers 546b9d9c82bSKay Sievers sysfs_remove_link(&dev->kobj, "subsystem"); 5474a3ad20cSGreg Kroah-Hartman sysfs_remove_link(&dev->bus->p->devices_kset->kobj, 5481e0b2cf9SKay Sievers dev_name(dev)); 549fa6fdb33SGreg Kroah-Hartman device_remove_groups(dev, dev->bus->dev_groups); 550ae1b4171SGreg Kroah-Hartman if (klist_node_attached(&dev->p->knode_bus)) 551ae1b4171SGreg Kroah-Hartman klist_del(&dev->p->knode_bus); 5523f62e570SGreg Kroah-Hartman 5534a3ad20cSGreg Kroah-Hartman pr_debug("bus: '%s': remove device %s\n", 5541e0b2cf9SKay Sievers dev->bus->name, dev_name(dev)); 5551da177e4SLinus Torvalds device_release_driver(dev); 556fc1ede58SGreg Kroah-Hartman bus_put(dev->bus); 5571da177e4SLinus Torvalds } 5581da177e4SLinus Torvalds 559f86db396SAndrew Morton static int __must_check add_bind_files(struct device_driver *drv) 560874c6241SGreg Kroah-Hartman { 561f86db396SAndrew Morton int ret; 562f86db396SAndrew Morton 563f86db396SAndrew Morton ret = driver_create_file(drv, &driver_attr_unbind); 564f86db396SAndrew Morton if (ret == 0) { 565f86db396SAndrew Morton ret = driver_create_file(drv, &driver_attr_bind); 566f86db396SAndrew Morton if (ret) 567f86db396SAndrew Morton driver_remove_file(drv, &driver_attr_unbind); 568f86db396SAndrew Morton } 569f86db396SAndrew Morton return ret; 570874c6241SGreg Kroah-Hartman } 571874c6241SGreg Kroah-Hartman 572874c6241SGreg Kroah-Hartman static void remove_bind_files(struct device_driver *drv) 573874c6241SGreg Kroah-Hartman { 574874c6241SGreg Kroah-Hartman driver_remove_file(drv, &driver_attr_bind); 575874c6241SGreg Kroah-Hartman driver_remove_file(drv, &driver_attr_unbind); 576874c6241SGreg Kroah-Hartman } 577b8c5cec2SKay Sievers 5782e7189b6SGreg Kroah-Hartman static BUS_ATTR_WO(drivers_probe); 5792e7189b6SGreg Kroah-Hartman static BUS_ATTR_RW(drivers_autoprobe); 5808380770cSKay Sievers 581b8c5cec2SKay Sievers static int add_probe_files(struct bus_type *bus) 582b8c5cec2SKay Sievers { 583b8c5cec2SKay Sievers int retval; 584b8c5cec2SKay Sievers 5858380770cSKay Sievers retval = bus_create_file(bus, &bus_attr_drivers_probe); 586b8c5cec2SKay Sievers if (retval) 587b8c5cec2SKay Sievers goto out; 588b8c5cec2SKay Sievers 5898380770cSKay Sievers retval = bus_create_file(bus, &bus_attr_drivers_autoprobe); 590b8c5cec2SKay Sievers if (retval) 5918380770cSKay Sievers bus_remove_file(bus, &bus_attr_drivers_probe); 592b8c5cec2SKay Sievers out: 593b8c5cec2SKay Sievers return retval; 594b8c5cec2SKay Sievers } 595b8c5cec2SKay Sievers 596b8c5cec2SKay Sievers static void remove_probe_files(struct bus_type *bus) 597b8c5cec2SKay Sievers { 5988380770cSKay Sievers bus_remove_file(bus, &bus_attr_drivers_autoprobe); 5998380770cSKay Sievers bus_remove_file(bus, &bus_attr_drivers_probe); 600b8c5cec2SKay Sievers } 6011da177e4SLinus Torvalds 6022581c9ccSGreg Kroah-Hartman static ssize_t uevent_store(struct device_driver *drv, const char *buf, 6032581c9ccSGreg Kroah-Hartman size_t count) 6047ac1cf4aSKay Sievers { 605df44b479SPeter Rajnoha int rc; 606df44b479SPeter Rajnoha 607df44b479SPeter Rajnoha rc = kobject_synth_uevent(&drv->p->kobj, buf, count); 608df44b479SPeter Rajnoha return rc ? rc : count; 6097ac1cf4aSKay Sievers } 6102581c9ccSGreg Kroah-Hartman static DRIVER_ATTR_WO(uevent); 6117ac1cf4aSKay Sievers 6121da177e4SLinus Torvalds /** 6131da177e4SLinus Torvalds * bus_add_driver - Add a driver to the bus. 6141da177e4SLinus Torvalds * @drv: driver. 6151da177e4SLinus Torvalds */ 6161da177e4SLinus Torvalds int bus_add_driver(struct device_driver *drv) 6171da177e4SLinus Torvalds { 618e5dd1278SGreg Kroah-Hartman struct bus_type *bus; 619e5dd1278SGreg Kroah-Hartman struct driver_private *priv; 6201da177e4SLinus Torvalds int error = 0; 6211da177e4SLinus Torvalds 622e5dd1278SGreg Kroah-Hartman bus = bus_get(drv->bus); 623d9fd4d3bSJeff Garzik if (!bus) 6244f6e1945SGreg Kroah-Hartman return -EINVAL; 625d9fd4d3bSJeff Garzik 6267dc72b28SGreg Kroah-Hartman pr_debug("bus: '%s': add driver %s\n", bus->name, drv->name); 627e5dd1278SGreg Kroah-Hartman 628e5dd1278SGreg Kroah-Hartman priv = kzalloc(sizeof(*priv), GFP_KERNEL); 62907634464SCornelia Huck if (!priv) { 63007634464SCornelia Huck error = -ENOMEM; 63107634464SCornelia Huck goto out_put_bus; 63207634464SCornelia Huck } 633e5dd1278SGreg Kroah-Hartman klist_init(&priv->klist_devices, NULL, NULL); 634e5dd1278SGreg Kroah-Hartman priv->driver = drv; 635e5dd1278SGreg Kroah-Hartman drv->p = priv; 636c8e90d82SGreg Kroah-Hartman priv->kobj.kset = bus->p->drivers_kset; 637c8e90d82SGreg Kroah-Hartman error = kobject_init_and_add(&priv->kobj, &driver_ktype, NULL, 638c8e90d82SGreg Kroah-Hartman "%s", drv->name); 639dc0afa83SCornelia Huck if (error) 64007634464SCornelia Huck goto out_unregister; 6411da177e4SLinus Torvalds 642190888acSMing Lei klist_add_tail(&priv->knode_bus, &bus->p->klist_drivers); 643c6f7e72aSGreg Kroah-Hartman if (drv->bus->p->drivers_autoprobe) { 644f86db396SAndrew Morton error = driver_attach(drv); 645f86db396SAndrew Morton if (error) 646310862e5SSchspa Shi goto out_del_list; 647b8c5cec2SKay Sievers } 6481da177e4SLinus Torvalds module_add_driver(drv->owner, drv); 6491da177e4SLinus Torvalds 6507ac1cf4aSKay Sievers error = driver_create_file(drv, &driver_attr_uevent); 6517ac1cf4aSKay Sievers if (error) { 6527ac1cf4aSKay Sievers printk(KERN_ERR "%s: uevent attr (%s) failed\n", 6532b3a302aSHarvey Harrison __func__, drv->name); 6547ac1cf4aSKay Sievers } 655e18945b1SGreg Kroah-Hartman error = driver_add_groups(drv, bus->drv_groups); 656f86db396SAndrew Morton if (error) { 657f86db396SAndrew Morton /* How the hell do we get out of this pickle? Give up */ 6584044b2fcSJoe Pater printk(KERN_ERR "%s: driver_add_groups(%s) failed\n", 659ed0617b5SGreg Kroah-Hartman __func__, drv->name); 660e18945b1SGreg Kroah-Hartman } 6611a6f2a75SDmitry Torokhov 6621a6f2a75SDmitry Torokhov if (!drv->suppress_bind_attrs) { 663f86db396SAndrew Morton error = add_bind_files(drv); 664f86db396SAndrew Morton if (error) { 665f86db396SAndrew Morton /* Ditto */ 666f86db396SAndrew Morton printk(KERN_ERR "%s: add_bind_files(%s) failed\n", 6672b3a302aSHarvey Harrison __func__, drv->name); 668f86db396SAndrew Morton } 6691a6f2a75SDmitry Torokhov } 670d9fd4d3bSJeff Garzik 6715c8563d7SKay Sievers return 0; 6721a6f2a75SDmitry Torokhov 673310862e5SSchspa Shi out_del_list: 674310862e5SSchspa Shi klist_del(&priv->knode_bus); 675f86db396SAndrew Morton out_unregister: 67699b28f1bSPhil Carmody kobject_put(&priv->kobj); 6770f9b011dSChristophe JAILLET /* drv->p is freed in driver_release() */ 6785c8563d7SKay Sievers drv->p = NULL; 679f86db396SAndrew Morton out_put_bus: 680fc1ede58SGreg Kroah-Hartman bus_put(bus); 681f86db396SAndrew Morton return error; 6821da177e4SLinus Torvalds } 6831da177e4SLinus Torvalds 6841da177e4SLinus Torvalds /** 6851da177e4SLinus Torvalds * bus_remove_driver - delete driver from bus's knowledge. 6861da177e4SLinus Torvalds * @drv: driver. 6871da177e4SLinus Torvalds * 6881da177e4SLinus Torvalds * Detach the driver from the devices it controls, and remove 6891da177e4SLinus Torvalds * it from its bus's list of drivers. Finally, we drop the reference 6901da177e4SLinus Torvalds * to the bus we took in bus_add_driver(). 6911da177e4SLinus Torvalds */ 6921da177e4SLinus Torvalds void bus_remove_driver(struct device_driver *drv) 6931da177e4SLinus Torvalds { 694d9fd4d3bSJeff Garzik if (!drv->bus) 695d9fd4d3bSJeff Garzik return; 696d9fd4d3bSJeff Garzik 6971a6f2a75SDmitry Torokhov if (!drv->suppress_bind_attrs) 698874c6241SGreg Kroah-Hartman remove_bind_files(drv); 699ed0617b5SGreg Kroah-Hartman driver_remove_groups(drv, drv->bus->drv_groups); 7007ac1cf4aSKay Sievers driver_remove_file(drv, &driver_attr_uevent); 701e5dd1278SGreg Kroah-Hartman klist_remove(&drv->p->knode_bus); 7027dc72b28SGreg Kroah-Hartman pr_debug("bus: '%s': remove driver %s\n", drv->bus->name, drv->name); 7031da177e4SLinus Torvalds driver_detach(drv); 7041da177e4SLinus Torvalds module_remove_driver(drv); 705c10997f6SGreg Kroah-Hartman kobject_put(&drv->p->kobj); 706fc1ede58SGreg Kroah-Hartman bus_put(drv->bus); 7071da177e4SLinus Torvalds } 7081da177e4SLinus Torvalds 7091da177e4SLinus Torvalds /* Helper for bus_rescan_devices's iter */ 710f86db396SAndrew Morton static int __must_check bus_rescan_devices_helper(struct device *dev, 711f86db396SAndrew Morton void *data) 7121da177e4SLinus Torvalds { 713f86db396SAndrew Morton int ret = 0; 714f86db396SAndrew Morton 715bf74ad5bSAlan Stern if (!dev->driver) { 7168c97a46aSMartin Liu if (dev->parent && dev->bus->need_parent_lock) 7178e9394ceSGreg Kroah-Hartman device_lock(dev->parent); 718f86db396SAndrew Morton ret = device_attach(dev); 7198c97a46aSMartin Liu if (dev->parent && dev->bus->need_parent_lock) 7208e9394ceSGreg Kroah-Hartman device_unlock(dev->parent); 721bf74ad5bSAlan Stern } 722f86db396SAndrew Morton return ret < 0 ? ret : 0; 7231da177e4SLinus Torvalds } 7241da177e4SLinus Torvalds 7251da177e4SLinus Torvalds /** 7261da177e4SLinus Torvalds * bus_rescan_devices - rescan devices on the bus for possible drivers 7271da177e4SLinus Torvalds * @bus: the bus to scan. 7281da177e4SLinus Torvalds * 7291da177e4SLinus Torvalds * This function will look for devices on the bus with no driver 73023d3d602SGreg Kroah-Hartman * attached and rescan it against existing drivers to see if it matches 73123d3d602SGreg Kroah-Hartman * any by calling device_attach() for the unbound devices. 7321da177e4SLinus Torvalds */ 733f86db396SAndrew Morton int bus_rescan_devices(struct bus_type *bus) 7341da177e4SLinus Torvalds { 735f86db396SAndrew Morton return bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper); 7361da177e4SLinus Torvalds } 7374a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_rescan_devices); 7381da177e4SLinus Torvalds 739e935d5daSMoore, Eric /** 740e935d5daSMoore, Eric * device_reprobe - remove driver for a device and probe for a new driver 741e935d5daSMoore, Eric * @dev: the device to reprobe 742e935d5daSMoore, Eric * 743e935d5daSMoore, Eric * This function detaches the attached driver (if any) for the given 744e935d5daSMoore, Eric * device and restarts the driver probing process. It is intended 745e935d5daSMoore, Eric * to use if probing criteria changed during a devices lifetime and 746e935d5daSMoore, Eric * driver attachment should change accordingly. 747e935d5daSMoore, Eric */ 748f86db396SAndrew Morton int device_reprobe(struct device *dev) 749e935d5daSMoore, Eric { 750ed88747cSAlexander Duyck if (dev->driver) 751ed88747cSAlexander Duyck device_driver_detach(dev); 752f86db396SAndrew Morton return bus_rescan_devices_helper(dev, NULL); 753e935d5daSMoore, Eric } 754e935d5daSMoore, Eric EXPORT_SYMBOL_GPL(device_reprobe); 7551da177e4SLinus Torvalds 75612478ba0SGreg Kroah-Hartman static int bus_add_groups(struct bus_type *bus, 75712478ba0SGreg Kroah-Hartman const struct attribute_group **groups) 75812478ba0SGreg Kroah-Hartman { 7593e9b2baeSGreg Kroah-Hartman return sysfs_create_groups(&bus->p->subsys.kobj, groups); 76012478ba0SGreg Kroah-Hartman } 76112478ba0SGreg Kroah-Hartman 76212478ba0SGreg Kroah-Hartman static void bus_remove_groups(struct bus_type *bus, 76312478ba0SGreg Kroah-Hartman const struct attribute_group **groups) 76412478ba0SGreg Kroah-Hartman { 7653e9b2baeSGreg Kroah-Hartman sysfs_remove_groups(&bus->p->subsys.kobj, groups); 76612478ba0SGreg Kroah-Hartman } 76712478ba0SGreg Kroah-Hartman 76834bb61f9SJames Bottomley static void klist_devices_get(struct klist_node *n) 76934bb61f9SJames Bottomley { 770ae1b4171SGreg Kroah-Hartman struct device_private *dev_prv = to_device_private_bus(n); 771ae1b4171SGreg Kroah-Hartman struct device *dev = dev_prv->device; 77234bb61f9SJames Bottomley 77334bb61f9SJames Bottomley get_device(dev); 77434bb61f9SJames Bottomley } 77534bb61f9SJames Bottomley 77634bb61f9SJames Bottomley static void klist_devices_put(struct klist_node *n) 77734bb61f9SJames Bottomley { 778ae1b4171SGreg Kroah-Hartman struct device_private *dev_prv = to_device_private_bus(n); 779ae1b4171SGreg Kroah-Hartman struct device *dev = dev_prv->device; 78034bb61f9SJames Bottomley 78134bb61f9SJames Bottomley put_device(dev); 78234bb61f9SJames Bottomley } 78334bb61f9SJames Bottomley 7847ac1cf4aSKay Sievers static ssize_t bus_uevent_store(struct bus_type *bus, 7857ac1cf4aSKay Sievers const char *buf, size_t count) 7867ac1cf4aSKay Sievers { 787*a00fdb98SGreg Kroah-Hartman struct subsys_private *sp = bus_to_subsys(bus); 788*a00fdb98SGreg Kroah-Hartman int ret; 789df44b479SPeter Rajnoha 790*a00fdb98SGreg Kroah-Hartman if (!sp) 791*a00fdb98SGreg Kroah-Hartman return -EINVAL; 792*a00fdb98SGreg Kroah-Hartman 793*a00fdb98SGreg Kroah-Hartman ret = kobject_synth_uevent(&sp->subsys.kobj, buf, count); 794*a00fdb98SGreg Kroah-Hartman subsys_put(sp); 795*a00fdb98SGreg Kroah-Hartman 796*a00fdb98SGreg Kroah-Hartman if (ret) 797*a00fdb98SGreg Kroah-Hartman return ret; 798*a00fdb98SGreg Kroah-Hartman return count; 7997ac1cf4aSKay Sievers } 800a4723041SGreg Kroah-Hartman /* 801a4723041SGreg Kroah-Hartman * "open code" the old BUS_ATTR() macro here. We want to use BUS_ATTR_WO() 802a4723041SGreg Kroah-Hartman * here, but can not use it as earlier in the file we have 803a4723041SGreg Kroah-Hartman * DEVICE_ATTR_WO(uevent), which would cause a clash with the with the store 804a4723041SGreg Kroah-Hartman * function name. 805a4723041SGreg Kroah-Hartman */ 80616b0dd40SJinchao Wang static struct bus_attribute bus_attr_uevent = __ATTR(uevent, 0200, NULL, 807a4723041SGreg Kroah-Hartman bus_uevent_store); 8087ac1cf4aSKay Sievers 8091da177e4SLinus Torvalds /** 810be871b7eSMichal Hocko * bus_register - register a driver-core subsystem 81178d79559SRandy Dunlap * @bus: bus to register 8121da177e4SLinus Torvalds * 81378d79559SRandy Dunlap * Once we have that, we register the bus with the kobject 8141da177e4SLinus Torvalds * infrastructure, then register the children subsystems it has: 815ca22e56dSKay Sievers * the devices and drivers that belong to the subsystem. 8161da177e4SLinus Torvalds */ 817be871b7eSMichal Hocko int bus_register(struct bus_type *bus) 8181da177e4SLinus Torvalds { 8191da177e4SLinus Torvalds int retval; 8206b6e39a6SKay Sievers struct subsys_private *priv; 82137e98d9bSGreg Kroah-Hartman struct lock_class_key *key; 8221da177e4SLinus Torvalds 8236b6e39a6SKay Sievers priv = kzalloc(sizeof(struct subsys_private), GFP_KERNEL); 824c6f7e72aSGreg Kroah-Hartman if (!priv) 825c6f7e72aSGreg Kroah-Hartman return -ENOMEM; 826116af378SBenjamin Herrenschmidt 827c6f7e72aSGreg Kroah-Hartman priv->bus = bus; 828c6f7e72aSGreg Kroah-Hartman bus->p = priv; 829c6f7e72aSGreg Kroah-Hartman 830c6f7e72aSGreg Kroah-Hartman BLOCKING_INIT_NOTIFIER_HEAD(&priv->bus_notifier); 831c6f7e72aSGreg Kroah-Hartman 832c6f7e72aSGreg Kroah-Hartman retval = kobject_set_name(&priv->subsys.kobj, "%s", bus->name); 8331da177e4SLinus Torvalds if (retval) 8341da177e4SLinus Torvalds goto out; 8351da177e4SLinus Torvalds 836c6f7e72aSGreg Kroah-Hartman priv->subsys.kobj.kset = bus_kset; 837c6f7e72aSGreg Kroah-Hartman priv->subsys.kobj.ktype = &bus_ktype; 838c6f7e72aSGreg Kroah-Hartman priv->drivers_autoprobe = 1; 839d6b05b84SGreg Kroah-Hartman 840c6f7e72aSGreg Kroah-Hartman retval = kset_register(&priv->subsys); 8411da177e4SLinus Torvalds if (retval) 8421da177e4SLinus Torvalds goto out; 8431da177e4SLinus Torvalds 8447ac1cf4aSKay Sievers retval = bus_create_file(bus, &bus_attr_uevent); 8457ac1cf4aSKay Sievers if (retval) 8467ac1cf4aSKay Sievers goto bus_uevent_fail; 8477ac1cf4aSKay Sievers 848c6f7e72aSGreg Kroah-Hartman priv->devices_kset = kset_create_and_add("devices", NULL, 849c6f7e72aSGreg Kroah-Hartman &priv->subsys.kobj); 850c6f7e72aSGreg Kroah-Hartman if (!priv->devices_kset) { 8513d899596SGreg Kroah-Hartman retval = -ENOMEM; 8521da177e4SLinus Torvalds goto bus_devices_fail; 8533d899596SGreg Kroah-Hartman } 8541da177e4SLinus Torvalds 855c6f7e72aSGreg Kroah-Hartman priv->drivers_kset = kset_create_and_add("drivers", NULL, 856c6f7e72aSGreg Kroah-Hartman &priv->subsys.kobj); 857c6f7e72aSGreg Kroah-Hartman if (!priv->drivers_kset) { 8586dcec251SGreg Kroah-Hartman retval = -ENOMEM; 8591da177e4SLinus Torvalds goto bus_drivers_fail; 8606dcec251SGreg Kroah-Hartman } 861465c7a3aSmochel@digitalimplant.org 862ca22e56dSKay Sievers INIT_LIST_HEAD(&priv->interfaces); 86337e98d9bSGreg Kroah-Hartman key = &priv->lock_key; 86437e98d9bSGreg Kroah-Hartman lockdep_register_key(key); 865ca22e56dSKay Sievers __mutex_init(&priv->mutex, "subsys mutex", key); 866c6f7e72aSGreg Kroah-Hartman klist_init(&priv->klist_devices, klist_devices_get, klist_devices_put); 867c6f7e72aSGreg Kroah-Hartman klist_init(&priv->klist_drivers, NULL, NULL); 868b8c5cec2SKay Sievers 869b8c5cec2SKay Sievers retval = add_probe_files(bus); 870b8c5cec2SKay Sievers if (retval) 871b8c5cec2SKay Sievers goto bus_probe_files_fail; 872b8c5cec2SKay Sievers 87312478ba0SGreg Kroah-Hartman retval = bus_add_groups(bus, bus->bus_groups); 87412478ba0SGreg Kroah-Hartman if (retval) 87512478ba0SGreg Kroah-Hartman goto bus_groups_fail; 8761da177e4SLinus Torvalds 8777dc72b28SGreg Kroah-Hartman pr_debug("bus: '%s': registered\n", bus->name); 8781da177e4SLinus Torvalds return 0; 8791da177e4SLinus Torvalds 88012478ba0SGreg Kroah-Hartman bus_groups_fail: 881b8c5cec2SKay Sievers remove_probe_files(bus); 882b8c5cec2SKay Sievers bus_probe_files_fail: 883c6f7e72aSGreg Kroah-Hartman kset_unregister(bus->p->drivers_kset); 8841da177e4SLinus Torvalds bus_drivers_fail: 885c6f7e72aSGreg Kroah-Hartman kset_unregister(bus->p->devices_kset); 8861da177e4SLinus Torvalds bus_devices_fail: 8877ac1cf4aSKay Sievers bus_remove_file(bus, &bus_attr_uevent); 8887ac1cf4aSKay Sievers bus_uevent_fail: 889c6f7e72aSGreg Kroah-Hartman kset_unregister(&bus->p->subsys); 8901da177e4SLinus Torvalds out: 891600c20f3SJike Song kfree(bus->p); 892f48f3febSDave Young bus->p = NULL; 8931da177e4SLinus Torvalds return retval; 8941da177e4SLinus Torvalds } 895be871b7eSMichal Hocko EXPORT_SYMBOL_GPL(bus_register); 8961da177e4SLinus Torvalds 8971da177e4SLinus Torvalds /** 8981da177e4SLinus Torvalds * bus_unregister - remove a bus from the system 8991da177e4SLinus Torvalds * @bus: bus. 9001da177e4SLinus Torvalds * 9011da177e4SLinus Torvalds * Unregister the child subsystems and the bus itself. 902fc1ede58SGreg Kroah-Hartman * Finally, we call bus_put() to release the refcount 9031da177e4SLinus Torvalds */ 9041da177e4SLinus Torvalds void bus_unregister(struct bus_type *bus) 9051da177e4SLinus Torvalds { 9067dc72b28SGreg Kroah-Hartman pr_debug("bus: '%s': unregistering\n", bus->name); 907ca22e56dSKay Sievers if (bus->dev_root) 908ca22e56dSKay Sievers device_unregister(bus->dev_root); 90912478ba0SGreg Kroah-Hartman bus_remove_groups(bus, bus->bus_groups); 910b8c5cec2SKay Sievers remove_probe_files(bus); 911c6f7e72aSGreg Kroah-Hartman kset_unregister(bus->p->drivers_kset); 912c6f7e72aSGreg Kroah-Hartman kset_unregister(bus->p->devices_kset); 9137ac1cf4aSKay Sievers bus_remove_file(bus, &bus_attr_uevent); 914c6f7e72aSGreg Kroah-Hartman kset_unregister(&bus->p->subsys); 9151da177e4SLinus Torvalds } 9164a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_unregister); 9171da177e4SLinus Torvalds 918116af378SBenjamin Herrenschmidt int bus_register_notifier(struct bus_type *bus, struct notifier_block *nb) 919116af378SBenjamin Herrenschmidt { 920c6f7e72aSGreg Kroah-Hartman return blocking_notifier_chain_register(&bus->p->bus_notifier, nb); 921116af378SBenjamin Herrenschmidt } 922116af378SBenjamin Herrenschmidt EXPORT_SYMBOL_GPL(bus_register_notifier); 923116af378SBenjamin Herrenschmidt 924116af378SBenjamin Herrenschmidt int bus_unregister_notifier(struct bus_type *bus, struct notifier_block *nb) 925116af378SBenjamin Herrenschmidt { 926c6f7e72aSGreg Kroah-Hartman return blocking_notifier_chain_unregister(&bus->p->bus_notifier, nb); 927116af378SBenjamin Herrenschmidt } 928116af378SBenjamin Herrenschmidt EXPORT_SYMBOL_GPL(bus_unregister_notifier); 929116af378SBenjamin Herrenschmidt 930ed9f9181SGreg Kroah-Hartman void bus_notify(struct device *dev, enum bus_notifier_event value) 931ed9f9181SGreg Kroah-Hartman { 932ed9f9181SGreg Kroah-Hartman struct bus_type *bus = dev->bus; 933ed9f9181SGreg Kroah-Hartman 934ed9f9181SGreg Kroah-Hartman if (bus) 935ed9f9181SGreg Kroah-Hartman blocking_notifier_call_chain(&bus->p->bus_notifier, value, dev); 936ed9f9181SGreg Kroah-Hartman } 937ed9f9181SGreg Kroah-Hartman 9380fed80f7SGreg Kroah-Hartman struct kset *bus_get_kset(struct bus_type *bus) 9390fed80f7SGreg Kroah-Hartman { 940c6f7e72aSGreg Kroah-Hartman return &bus->p->subsys; 9410fed80f7SGreg Kroah-Hartman } 9420fed80f7SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_get_kset); 9430fed80f7SGreg Kroah-Hartman 9448afbb427SGreg Kroah-Hartman static struct klist *bus_get_device_klist(struct bus_type *bus) 945b249072eSGreg Kroah-Hartman { 946c6f7e72aSGreg Kroah-Hartman return &bus->p->klist_devices; 947b249072eSGreg Kroah-Hartman } 948b249072eSGreg Kroah-Hartman 94999178b03SGreg Kroah-Hartman /* 950dca25ebdSRobert P. J. Day * Yes, this forcibly breaks the klist abstraction temporarily. It 95199178b03SGreg Kroah-Hartman * just wants to sort the klist, not change reference counts and 95299178b03SGreg Kroah-Hartman * take/drop locks rapidly in the process. It does all this while 95399178b03SGreg Kroah-Hartman * holding the lock for the list, so objects can't otherwise be 95499178b03SGreg Kroah-Hartman * added/removed while we're swizzling. 95599178b03SGreg Kroah-Hartman */ 95699178b03SGreg Kroah-Hartman static void device_insertion_sort_klist(struct device *a, struct list_head *list, 95799178b03SGreg Kroah-Hartman int (*compare)(const struct device *a, 95899178b03SGreg Kroah-Hartman const struct device *b)) 95999178b03SGreg Kroah-Hartman { 96099178b03SGreg Kroah-Hartman struct klist_node *n; 961ae1b4171SGreg Kroah-Hartman struct device_private *dev_prv; 96299178b03SGreg Kroah-Hartman struct device *b; 96399178b03SGreg Kroah-Hartman 9644c62785eSGeliang Tang list_for_each_entry(n, list, n_node) { 965ae1b4171SGreg Kroah-Hartman dev_prv = to_device_private_bus(n); 966ae1b4171SGreg Kroah-Hartman b = dev_prv->device; 96799178b03SGreg Kroah-Hartman if (compare(a, b) <= 0) { 968ae1b4171SGreg Kroah-Hartman list_move_tail(&a->p->knode_bus.n_node, 969ae1b4171SGreg Kroah-Hartman &b->p->knode_bus.n_node); 97099178b03SGreg Kroah-Hartman return; 97199178b03SGreg Kroah-Hartman } 97299178b03SGreg Kroah-Hartman } 973ae1b4171SGreg Kroah-Hartman list_move_tail(&a->p->knode_bus.n_node, list); 97499178b03SGreg Kroah-Hartman } 97599178b03SGreg Kroah-Hartman 97699178b03SGreg Kroah-Hartman void bus_sort_breadthfirst(struct bus_type *bus, 97799178b03SGreg Kroah-Hartman int (*compare)(const struct device *a, 97899178b03SGreg Kroah-Hartman const struct device *b)) 97999178b03SGreg Kroah-Hartman { 98099178b03SGreg Kroah-Hartman LIST_HEAD(sorted_devices); 9814c62785eSGeliang Tang struct klist_node *n, *tmp; 982ae1b4171SGreg Kroah-Hartman struct device_private *dev_prv; 98399178b03SGreg Kroah-Hartman struct device *dev; 98499178b03SGreg Kroah-Hartman struct klist *device_klist; 98599178b03SGreg Kroah-Hartman 98699178b03SGreg Kroah-Hartman device_klist = bus_get_device_klist(bus); 98799178b03SGreg Kroah-Hartman 98899178b03SGreg Kroah-Hartman spin_lock(&device_klist->k_lock); 9894c62785eSGeliang Tang list_for_each_entry_safe(n, tmp, &device_klist->k_list, n_node) { 990ae1b4171SGreg Kroah-Hartman dev_prv = to_device_private_bus(n); 991ae1b4171SGreg Kroah-Hartman dev = dev_prv->device; 99299178b03SGreg Kroah-Hartman device_insertion_sort_klist(dev, &sorted_devices, compare); 99399178b03SGreg Kroah-Hartman } 99499178b03SGreg Kroah-Hartman list_splice(&sorted_devices, &device_klist->k_list); 99599178b03SGreg Kroah-Hartman spin_unlock(&device_klist->k_lock); 99699178b03SGreg Kroah-Hartman } 99799178b03SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_sort_breadthfirst); 99899178b03SGreg Kroah-Hartman 999b0a8a59aSGreg Kroah-Hartman struct subsys_dev_iter { 1000b0a8a59aSGreg Kroah-Hartman struct klist_iter ki; 1001b0a8a59aSGreg Kroah-Hartman const struct device_type *type; 1002b0a8a59aSGreg Kroah-Hartman }; 1003b0a8a59aSGreg Kroah-Hartman 1004ca22e56dSKay Sievers /** 1005ca22e56dSKay Sievers * subsys_dev_iter_init - initialize subsys device iterator 1006ca22e56dSKay Sievers * @iter: subsys iterator to initialize 1007ca22e56dSKay Sievers * @subsys: the subsys we wanna iterate over 1008ca22e56dSKay Sievers * @start: the device to start iterating from, if any 1009ca22e56dSKay Sievers * @type: device_type of the devices to iterate over, NULL for all 1010ca22e56dSKay Sievers * 1011ca22e56dSKay Sievers * Initialize subsys iterator @iter such that it iterates over devices 1012ca22e56dSKay Sievers * of @subsys. If @start is set, the list iteration will start there, 1013ca22e56dSKay Sievers * otherwise if it is NULL, the iteration starts at the beginning of 1014ca22e56dSKay Sievers * the list. 1015ca22e56dSKay Sievers */ 10162e45fc55SGreg Kroah-Hartman static void subsys_dev_iter_init(struct subsys_dev_iter *iter, struct bus_type *subsys, 1017ca22e56dSKay Sievers struct device *start, const struct device_type *type) 1018ca22e56dSKay Sievers { 1019ca22e56dSKay Sievers struct klist_node *start_knode = NULL; 1020ca22e56dSKay Sievers 1021ca22e56dSKay Sievers if (start) 1022ca22e56dSKay Sievers start_knode = &start->p->knode_bus; 10237cd9c9bbSGreg Kroah-Hartman klist_iter_init_node(&subsys->p->klist_devices, &iter->ki, start_knode); 1024ca22e56dSKay Sievers iter->type = type; 1025ca22e56dSKay Sievers } 1026ca22e56dSKay Sievers 1027ca22e56dSKay Sievers /** 1028ca22e56dSKay Sievers * subsys_dev_iter_next - iterate to the next device 1029ca22e56dSKay Sievers * @iter: subsys iterator to proceed 1030ca22e56dSKay Sievers * 1031ca22e56dSKay Sievers * Proceed @iter to the next device and return it. Returns NULL if 1032ca22e56dSKay Sievers * iteration is complete. 1033ca22e56dSKay Sievers * 1034ca22e56dSKay Sievers * The returned device is referenced and won't be released till 1035ca22e56dSKay Sievers * iterator is proceed to the next device or exited. The caller is 1036ca22e56dSKay Sievers * free to do whatever it wants to do with the device including 1037ca22e56dSKay Sievers * calling back into subsys code. 1038ca22e56dSKay Sievers */ 103938cdadefSGreg Kroah-Hartman static struct device *subsys_dev_iter_next(struct subsys_dev_iter *iter) 1040ca22e56dSKay Sievers { 1041ca22e56dSKay Sievers struct klist_node *knode; 1042ca22e56dSKay Sievers struct device *dev; 1043ca22e56dSKay Sievers 1044ca22e56dSKay Sievers for (;;) { 1045ca22e56dSKay Sievers knode = klist_next(&iter->ki); 1046ca22e56dSKay Sievers if (!knode) 1047ca22e56dSKay Sievers return NULL; 1048371fd7a2SGeliang Tang dev = to_device_private_bus(knode)->device; 1049ca22e56dSKay Sievers if (!iter->type || iter->type == dev->type) 1050ca22e56dSKay Sievers return dev; 1051ca22e56dSKay Sievers } 1052ca22e56dSKay Sievers } 1053ca22e56dSKay Sievers 1054ca22e56dSKay Sievers /** 1055ca22e56dSKay Sievers * subsys_dev_iter_exit - finish iteration 1056ca22e56dSKay Sievers * @iter: subsys iterator to finish 1057ca22e56dSKay Sievers * 1058ca22e56dSKay Sievers * Finish an iteration. Always call this function after iteration is 1059ca22e56dSKay Sievers * complete whether the iteration ran till the end or not. 1060ca22e56dSKay Sievers */ 1061af6d0743SGreg Kroah-Hartman static void subsys_dev_iter_exit(struct subsys_dev_iter *iter) 1062ca22e56dSKay Sievers { 1063ca22e56dSKay Sievers klist_iter_exit(&iter->ki); 1064ca22e56dSKay Sievers } 1065ca22e56dSKay Sievers 1066ca22e56dSKay Sievers int subsys_interface_register(struct subsys_interface *sif) 1067ca22e56dSKay Sievers { 1068ca22e56dSKay Sievers struct bus_type *subsys; 1069ca22e56dSKay Sievers struct subsys_dev_iter iter; 1070ca22e56dSKay Sievers struct device *dev; 1071ca22e56dSKay Sievers 1072ca22e56dSKay Sievers if (!sif || !sif->subsys) 1073ca22e56dSKay Sievers return -ENODEV; 1074ca22e56dSKay Sievers 1075ca22e56dSKay Sievers subsys = bus_get(sif->subsys); 1076ca22e56dSKay Sievers if (!subsys) 1077ca22e56dSKay Sievers return -EINVAL; 1078ca22e56dSKay Sievers 1079ca22e56dSKay Sievers mutex_lock(&subsys->p->mutex); 1080ca22e56dSKay Sievers list_add_tail(&sif->node, &subsys->p->interfaces); 1081ca22e56dSKay Sievers if (sif->add_dev) { 1082ca22e56dSKay Sievers subsys_dev_iter_init(&iter, subsys, NULL, NULL); 1083ca22e56dSKay Sievers while ((dev = subsys_dev_iter_next(&iter))) 1084ca22e56dSKay Sievers sif->add_dev(dev, sif); 1085ca22e56dSKay Sievers subsys_dev_iter_exit(&iter); 1086ca22e56dSKay Sievers } 1087ca22e56dSKay Sievers mutex_unlock(&subsys->p->mutex); 1088ca22e56dSKay Sievers 1089ca22e56dSKay Sievers return 0; 1090ca22e56dSKay Sievers } 1091ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_interface_register); 1092ca22e56dSKay Sievers 1093ca22e56dSKay Sievers void subsys_interface_unregister(struct subsys_interface *sif) 1094ca22e56dSKay Sievers { 10952b31594aSJonghwan Choi struct bus_type *subsys; 1096ca22e56dSKay Sievers struct subsys_dev_iter iter; 1097ca22e56dSKay Sievers struct device *dev; 1098ca22e56dSKay Sievers 10992b31594aSJonghwan Choi if (!sif || !sif->subsys) 1100ca22e56dSKay Sievers return; 1101ca22e56dSKay Sievers 11022b31594aSJonghwan Choi subsys = sif->subsys; 11032b31594aSJonghwan Choi 1104ca22e56dSKay Sievers mutex_lock(&subsys->p->mutex); 1105ca22e56dSKay Sievers list_del_init(&sif->node); 1106ca22e56dSKay Sievers if (sif->remove_dev) { 1107ca22e56dSKay Sievers subsys_dev_iter_init(&iter, subsys, NULL, NULL); 1108ca22e56dSKay Sievers while ((dev = subsys_dev_iter_next(&iter))) 1109ca22e56dSKay Sievers sif->remove_dev(dev, sif); 1110ca22e56dSKay Sievers subsys_dev_iter_exit(&iter); 1111ca22e56dSKay Sievers } 1112ca22e56dSKay Sievers mutex_unlock(&subsys->p->mutex); 1113ca22e56dSKay Sievers 1114ca22e56dSKay Sievers bus_put(subsys); 1115ca22e56dSKay Sievers } 1116ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_interface_unregister); 1117ca22e56dSKay Sievers 1118ca22e56dSKay Sievers static void system_root_device_release(struct device *dev) 1119ca22e56dSKay Sievers { 1120ca22e56dSKay Sievers kfree(dev); 1121ca22e56dSKay Sievers } 1122d73ce004STejun Heo 1123d73ce004STejun Heo static int subsys_register(struct bus_type *subsys, 1124d73ce004STejun Heo const struct attribute_group **groups, 1125d73ce004STejun Heo struct kobject *parent_of_root) 1126d73ce004STejun Heo { 1127d73ce004STejun Heo struct device *dev; 1128d73ce004STejun Heo int err; 1129d73ce004STejun Heo 1130d73ce004STejun Heo err = bus_register(subsys); 1131d73ce004STejun Heo if (err < 0) 1132d73ce004STejun Heo return err; 1133d73ce004STejun Heo 1134d73ce004STejun Heo dev = kzalloc(sizeof(struct device), GFP_KERNEL); 1135d73ce004STejun Heo if (!dev) { 1136d73ce004STejun Heo err = -ENOMEM; 1137d73ce004STejun Heo goto err_dev; 1138d73ce004STejun Heo } 1139d73ce004STejun Heo 1140d73ce004STejun Heo err = dev_set_name(dev, "%s", subsys->name); 1141d73ce004STejun Heo if (err < 0) 1142d73ce004STejun Heo goto err_name; 1143d73ce004STejun Heo 1144d73ce004STejun Heo dev->kobj.parent = parent_of_root; 1145d73ce004STejun Heo dev->groups = groups; 1146d73ce004STejun Heo dev->release = system_root_device_release; 1147d73ce004STejun Heo 1148d73ce004STejun Heo err = device_register(dev); 1149d73ce004STejun Heo if (err < 0) 1150d73ce004STejun Heo goto err_dev_reg; 1151d73ce004STejun Heo 1152d73ce004STejun Heo subsys->dev_root = dev; 1153d73ce004STejun Heo return 0; 1154d73ce004STejun Heo 1155d73ce004STejun Heo err_dev_reg: 1156d73ce004STejun Heo put_device(dev); 1157d73ce004STejun Heo dev = NULL; 1158d73ce004STejun Heo err_name: 1159d73ce004STejun Heo kfree(dev); 1160d73ce004STejun Heo err_dev: 1161d73ce004STejun Heo bus_unregister(subsys); 1162d73ce004STejun Heo return err; 1163d73ce004STejun Heo } 1164d73ce004STejun Heo 1165ca22e56dSKay Sievers /** 1166ca22e56dSKay Sievers * subsys_system_register - register a subsystem at /sys/devices/system/ 116778d79559SRandy Dunlap * @subsys: system subsystem 116878d79559SRandy Dunlap * @groups: default attributes for the root device 1169ca22e56dSKay Sievers * 1170ca22e56dSKay Sievers * All 'system' subsystems have a /sys/devices/system/<name> root device 1171ca22e56dSKay Sievers * with the name of the subsystem. The root device can carry subsystem- 1172ca22e56dSKay Sievers * wide attributes. All registered devices are below this single root 1173ca22e56dSKay Sievers * device and are named after the subsystem with a simple enumeration 1174e227867fSMasanari Iida * number appended. The registered devices are not explicitly named; 1175ca22e56dSKay Sievers * only 'id' in the device needs to be set. 1176ca22e56dSKay Sievers * 1177ca22e56dSKay Sievers * Do not use this interface for anything new, it exists for compatibility 1178ca22e56dSKay Sievers * with bad ideas only. New subsystems should use plain subsystems; and 1179ca22e56dSKay Sievers * add the subsystem-wide attributes should be added to the subsystem 1180ca22e56dSKay Sievers * directory itself and not some create fake root-device placed in 1181ca22e56dSKay Sievers * /sys/devices/system/<name>. 1182ca22e56dSKay Sievers */ 1183ca22e56dSKay Sievers int subsys_system_register(struct bus_type *subsys, 1184ca22e56dSKay Sievers const struct attribute_group **groups) 1185ca22e56dSKay Sievers { 1186d73ce004STejun Heo return subsys_register(subsys, groups, &system_kset->kobj); 1187ca22e56dSKay Sievers } 1188ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_system_register); 1189ca22e56dSKay Sievers 1190d73ce004STejun Heo /** 1191d73ce004STejun Heo * subsys_virtual_register - register a subsystem at /sys/devices/virtual/ 1192d73ce004STejun Heo * @subsys: virtual subsystem 1193d73ce004STejun Heo * @groups: default attributes for the root device 1194d73ce004STejun Heo * 1195d73ce004STejun Heo * All 'virtual' subsystems have a /sys/devices/system/<name> root device 1196d73ce004STejun Heo * with the name of the subystem. The root device can carry subsystem-wide 1197d73ce004STejun Heo * attributes. All registered devices are below this single root device. 1198d73ce004STejun Heo * There's no restriction on device naming. This is for kernel software 1199d73ce004STejun Heo * constructs which need sysfs interface. 1200d73ce004STejun Heo */ 1201d73ce004STejun Heo int subsys_virtual_register(struct bus_type *subsys, 1202d73ce004STejun Heo const struct attribute_group **groups) 1203d73ce004STejun Heo { 1204d73ce004STejun Heo struct kobject *virtual_dir; 1205d73ce004STejun Heo 1206d73ce004STejun Heo virtual_dir = virtual_device_parent(NULL); 1207d73ce004STejun Heo if (!virtual_dir) 1208d73ce004STejun Heo return -ENOMEM; 1209d73ce004STejun Heo 1210d73ce004STejun Heo return subsys_register(subsys, groups, virtual_dir); 1211d73ce004STejun Heo } 12121c04fc35SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(subsys_virtual_register); 1213d73ce004STejun Heo 12141da177e4SLinus Torvalds int __init buses_init(void) 12151da177e4SLinus Torvalds { 121659a54833SGreg Kroah-Hartman bus_kset = kset_create_and_add("bus", &bus_uevent_ops, NULL); 121759a54833SGreg Kroah-Hartman if (!bus_kset) 121859a54833SGreg Kroah-Hartman return -ENOMEM; 1219ca22e56dSKay Sievers 1220ca22e56dSKay Sievers system_kset = kset_create_and_add("system", NULL, &devices_kset->kobj); 1221ca22e56dSKay Sievers if (!system_kset) 1222ca22e56dSKay Sievers return -ENOMEM; 1223ca22e56dSKay Sievers 122459a54833SGreg Kroah-Hartman return 0; 12251da177e4SLinus Torvalds } 1226