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> 121da177e4SLinus Torvalds #include <linux/device.h> 131da177e4SLinus Torvalds #include <linux/module.h> 141da177e4SLinus Torvalds #include <linux/errno.h> 155a0e3ad6STejun Heo #include <linux/slab.h> 161da177e4SLinus Torvalds #include <linux/init.h> 171da177e4SLinus Torvalds #include <linux/string.h> 18ca22e56dSKay Sievers #include <linux/mutex.h> 1963967685SGreg Kroah-Hartman #include <linux/sysfs.h> 201da177e4SLinus Torvalds #include "base.h" 211da177e4SLinus Torvalds #include "power/power.h" 221da177e4SLinus Torvalds 23ca22e56dSKay Sievers /* /sys/devices/system */ 2497ec448aSH Hartley Sweeten static struct kset *system_kset; 25ca22e56dSKay Sievers 261da177e4SLinus Torvalds #define to_bus_attr(_attr) container_of(_attr, struct bus_attribute, attr) 271da177e4SLinus Torvalds 281da177e4SLinus Torvalds /* 291da177e4SLinus Torvalds * sysfs bindings for drivers 301da177e4SLinus Torvalds */ 311da177e4SLinus Torvalds 321da177e4SLinus Torvalds #define to_drv_attr(_attr) container_of(_attr, struct driver_attribute, attr) 331da177e4SLinus Torvalds 34*4f4b3743SDaniel Vetter #define DRIVER_ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) \ 35*4f4b3743SDaniel Vetter struct driver_attribute driver_attr_##_name = \ 36*4f4b3743SDaniel Vetter __ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) 371da177e4SLinus Torvalds 38b8c5cec2SKay Sievers static int __must_check bus_rescan_devices_helper(struct device *dev, 39b8c5cec2SKay Sievers void *data); 40b8c5cec2SKay Sievers 415901d014SGreg Kroah-Hartman static struct bus_type *bus_get(struct bus_type *bus) 425901d014SGreg Kroah-Hartman { 43c6f7e72aSGreg Kroah-Hartman if (bus) { 44c6f7e72aSGreg Kroah-Hartman kset_get(&bus->p->subsys); 45c6f7e72aSGreg Kroah-Hartman return bus; 46c6f7e72aSGreg Kroah-Hartman } 47c6f7e72aSGreg Kroah-Hartman return NULL; 485901d014SGreg Kroah-Hartman } 495901d014SGreg Kroah-Hartman 50fc1ede58SGreg Kroah-Hartman static void bus_put(struct bus_type *bus) 51fc1ede58SGreg Kroah-Hartman { 52c6f7e72aSGreg Kroah-Hartman if (bus) 53c6f7e72aSGreg Kroah-Hartman kset_put(&bus->p->subsys); 54fc1ede58SGreg Kroah-Hartman } 55fc1ede58SGreg Kroah-Hartman 564a3ad20cSGreg Kroah-Hartman static ssize_t drv_attr_show(struct kobject *kobj, struct attribute *attr, 574a3ad20cSGreg Kroah-Hartman char *buf) 581da177e4SLinus Torvalds { 591da177e4SLinus Torvalds struct driver_attribute *drv_attr = to_drv_attr(attr); 60e5dd1278SGreg Kroah-Hartman struct driver_private *drv_priv = to_driver(kobj); 614a0c20bfSDmitry Torokhov ssize_t ret = -EIO; 621da177e4SLinus Torvalds 631da177e4SLinus Torvalds if (drv_attr->show) 64e5dd1278SGreg Kroah-Hartman ret = drv_attr->show(drv_priv->driver, buf); 651da177e4SLinus Torvalds return ret; 661da177e4SLinus Torvalds } 671da177e4SLinus Torvalds 684a3ad20cSGreg Kroah-Hartman static ssize_t drv_attr_store(struct kobject *kobj, struct attribute *attr, 691da177e4SLinus Torvalds const char *buf, size_t count) 701da177e4SLinus Torvalds { 711da177e4SLinus Torvalds struct driver_attribute *drv_attr = to_drv_attr(attr); 72e5dd1278SGreg Kroah-Hartman struct driver_private *drv_priv = to_driver(kobj); 734a0c20bfSDmitry Torokhov ssize_t ret = -EIO; 741da177e4SLinus Torvalds 751da177e4SLinus Torvalds if (drv_attr->store) 76e5dd1278SGreg Kroah-Hartman ret = drv_attr->store(drv_priv->driver, buf, count); 771da177e4SLinus Torvalds return ret; 781da177e4SLinus Torvalds } 791da177e4SLinus Torvalds 8052cf25d0SEmese Revfy static const struct sysfs_ops driver_sysfs_ops = { 811da177e4SLinus Torvalds .show = drv_attr_show, 821da177e4SLinus Torvalds .store = drv_attr_store, 831da177e4SLinus Torvalds }; 841da177e4SLinus Torvalds 851da177e4SLinus Torvalds static void driver_release(struct kobject *kobj) 861da177e4SLinus Torvalds { 87e5dd1278SGreg Kroah-Hartman struct driver_private *drv_priv = to_driver(kobj); 88e5dd1278SGreg Kroah-Hartman 892b3a302aSHarvey Harrison pr_debug("driver: '%s': %s\n", kobject_name(kobj), __func__); 90e5dd1278SGreg Kroah-Hartman kfree(drv_priv); 911da177e4SLinus Torvalds } 921da177e4SLinus Torvalds 93a1148fb0SGreg Kroah-Hartman static struct kobj_type driver_ktype = { 941da177e4SLinus Torvalds .sysfs_ops = &driver_sysfs_ops, 951da177e4SLinus Torvalds .release = driver_release, 961da177e4SLinus Torvalds }; 971da177e4SLinus Torvalds 981da177e4SLinus Torvalds /* 991da177e4SLinus Torvalds * sysfs bindings for buses 1001da177e4SLinus Torvalds */ 1014a3ad20cSGreg Kroah-Hartman static ssize_t bus_attr_show(struct kobject *kobj, struct attribute *attr, 1024a3ad20cSGreg Kroah-Hartman char *buf) 1031da177e4SLinus Torvalds { 1041da177e4SLinus Torvalds struct bus_attribute *bus_attr = to_bus_attr(attr); 1056b6e39a6SKay Sievers struct subsys_private *subsys_priv = to_subsys_private(kobj); 1061da177e4SLinus Torvalds ssize_t ret = 0; 1071da177e4SLinus Torvalds 1081da177e4SLinus Torvalds if (bus_attr->show) 1096b6e39a6SKay Sievers ret = bus_attr->show(subsys_priv->bus, buf); 1101da177e4SLinus Torvalds return ret; 1111da177e4SLinus Torvalds } 1121da177e4SLinus Torvalds 1134a3ad20cSGreg Kroah-Hartman static ssize_t bus_attr_store(struct kobject *kobj, struct attribute *attr, 1141da177e4SLinus Torvalds const char *buf, size_t count) 1151da177e4SLinus Torvalds { 1161da177e4SLinus Torvalds struct bus_attribute *bus_attr = to_bus_attr(attr); 1176b6e39a6SKay Sievers struct subsys_private *subsys_priv = to_subsys_private(kobj); 1181da177e4SLinus Torvalds ssize_t ret = 0; 1191da177e4SLinus Torvalds 1201da177e4SLinus Torvalds if (bus_attr->store) 1216b6e39a6SKay Sievers ret = bus_attr->store(subsys_priv->bus, buf, count); 1221da177e4SLinus Torvalds return ret; 1231da177e4SLinus Torvalds } 1241da177e4SLinus Torvalds 12552cf25d0SEmese Revfy static const struct sysfs_ops bus_sysfs_ops = { 1261da177e4SLinus Torvalds .show = bus_attr_show, 1271da177e4SLinus Torvalds .store = bus_attr_store, 1281da177e4SLinus Torvalds }; 1291da177e4SLinus Torvalds 1301da177e4SLinus Torvalds int bus_create_file(struct bus_type *bus, struct bus_attribute *attr) 1311da177e4SLinus Torvalds { 1321da177e4SLinus Torvalds int error; 1335901d014SGreg Kroah-Hartman if (bus_get(bus)) { 134c6f7e72aSGreg Kroah-Hartman error = sysfs_create_file(&bus->p->subsys.kobj, &attr->attr); 135fc1ede58SGreg Kroah-Hartman bus_put(bus); 1361da177e4SLinus Torvalds } else 1371da177e4SLinus Torvalds error = -EINVAL; 1381da177e4SLinus Torvalds return error; 1391da177e4SLinus Torvalds } 1404a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_create_file); 1411da177e4SLinus Torvalds 1421da177e4SLinus Torvalds void bus_remove_file(struct bus_type *bus, struct bus_attribute *attr) 1431da177e4SLinus Torvalds { 1445901d014SGreg Kroah-Hartman if (bus_get(bus)) { 145c6f7e72aSGreg Kroah-Hartman sysfs_remove_file(&bus->p->subsys.kobj, &attr->attr); 146fc1ede58SGreg Kroah-Hartman bus_put(bus); 1471da177e4SLinus Torvalds } 1481da177e4SLinus Torvalds } 1494a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_remove_file); 1501da177e4SLinus Torvalds 151174be70bSBart Van Assche static void bus_release(struct kobject *kobj) 152174be70bSBart Van Assche { 153371fd7a2SGeliang Tang struct subsys_private *priv = to_subsys_private(kobj); 154174be70bSBart Van Assche struct bus_type *bus = priv->bus; 155174be70bSBart Van Assche 156174be70bSBart Van Assche kfree(priv); 157174be70bSBart Van Assche bus->p = NULL; 158174be70bSBart Van Assche } 159174be70bSBart Van Assche 16080f03e34SKay Sievers static struct kobj_type bus_ktype = { 1611da177e4SLinus Torvalds .sysfs_ops = &bus_sysfs_ops, 162174be70bSBart Van Assche .release = bus_release, 1631da177e4SLinus Torvalds }; 1641da177e4SLinus Torvalds 16580f03e34SKay Sievers static int bus_uevent_filter(struct kset *kset, struct kobject *kobj) 16680f03e34SKay Sievers { 16780f03e34SKay Sievers struct kobj_type *ktype = get_ktype(kobj); 16880f03e34SKay Sievers 16980f03e34SKay Sievers if (ktype == &bus_ktype) 17080f03e34SKay Sievers return 1; 17180f03e34SKay Sievers return 0; 17280f03e34SKay Sievers } 17380f03e34SKay Sievers 1749cd43611SEmese Revfy static const struct kset_uevent_ops bus_uevent_ops = { 17580f03e34SKay Sievers .filter = bus_uevent_filter, 17680f03e34SKay Sievers }; 17780f03e34SKay Sievers 17859a54833SGreg Kroah-Hartman static struct kset *bus_kset; 1791da177e4SLinus Torvalds 1802b08c8d0SAlan Stern /* Manually detach a device from its associated driver. */ 1812581c9ccSGreg Kroah-Hartman static ssize_t unbind_store(struct device_driver *drv, const char *buf, 1822581c9ccSGreg Kroah-Hartman size_t count) 183151ef38fSGreg Kroah-Hartman { 1845901d014SGreg Kroah-Hartman struct bus_type *bus = bus_get(drv->bus); 185151ef38fSGreg Kroah-Hartman struct device *dev; 186151ef38fSGreg Kroah-Hartman int err = -ENODEV; 187151ef38fSGreg Kroah-Hartman 1881f9ffc04SGreg Kroah-Hartman dev = bus_find_device_by_name(bus, NULL, buf); 1892b08c8d0SAlan Stern if (dev && dev->driver == drv) { 1908c97a46aSMartin Liu if (dev->parent && dev->bus->need_parent_lock) 1918e9394ceSGreg Kroah-Hartman device_lock(dev->parent); 192151ef38fSGreg Kroah-Hartman device_release_driver(dev); 1938c97a46aSMartin Liu if (dev->parent && dev->bus->need_parent_lock) 1948e9394ceSGreg Kroah-Hartman device_unlock(dev->parent); 195151ef38fSGreg Kroah-Hartman err = count; 196151ef38fSGreg Kroah-Hartman } 1972b08c8d0SAlan Stern put_device(dev); 198fc1ede58SGreg Kroah-Hartman bus_put(bus); 199151ef38fSGreg Kroah-Hartman return err; 200151ef38fSGreg Kroah-Hartman } 201*4f4b3743SDaniel Vetter static DRIVER_ATTR_IGNORE_LOCKDEP(unbind, S_IWUSR, NULL, unbind_store); 202151ef38fSGreg Kroah-Hartman 203afdce75fSGreg Kroah-Hartman /* 204afdce75fSGreg Kroah-Hartman * Manually attach a device to a driver. 205afdce75fSGreg Kroah-Hartman * Note: the driver must want to bind to the device, 206afdce75fSGreg Kroah-Hartman * it is not possible to override the driver's id table. 207afdce75fSGreg Kroah-Hartman */ 2082581c9ccSGreg Kroah-Hartman static ssize_t bind_store(struct device_driver *drv, const char *buf, 2092581c9ccSGreg Kroah-Hartman size_t count) 210afdce75fSGreg Kroah-Hartman { 2115901d014SGreg Kroah-Hartman struct bus_type *bus = bus_get(drv->bus); 212afdce75fSGreg Kroah-Hartman struct device *dev; 213afdce75fSGreg Kroah-Hartman int err = -ENODEV; 214afdce75fSGreg Kroah-Hartman 2151f9ffc04SGreg Kroah-Hartman dev = bus_find_device_by_name(bus, NULL, buf); 21649b420a1SMing Lei if (dev && dev->driver == NULL && driver_match_device(drv, dev)) { 2178c97a46aSMartin Liu if (dev->parent && bus->need_parent_lock) 2188e9394ceSGreg Kroah-Hartman device_lock(dev->parent); 2198e9394ceSGreg Kroah-Hartman device_lock(dev); 220afdce75fSGreg Kroah-Hartman err = driver_probe_device(drv, dev); 2218e9394ceSGreg Kroah-Hartman device_unlock(dev); 2228c97a46aSMartin Liu if (dev->parent && bus->need_parent_lock) 2238e9394ceSGreg Kroah-Hartman device_unlock(dev->parent); 22437225401SRyan Wilson 2254a3ad20cSGreg Kroah-Hartman if (err > 0) { 2264a3ad20cSGreg Kroah-Hartman /* success */ 22737225401SRyan Wilson err = count; 2284a3ad20cSGreg Kroah-Hartman } else if (err == 0) { 2294a3ad20cSGreg Kroah-Hartman /* driver didn't accept device */ 23037225401SRyan Wilson err = -ENODEV; 231afdce75fSGreg Kroah-Hartman } 2324a3ad20cSGreg Kroah-Hartman } 2332b08c8d0SAlan Stern put_device(dev); 234fc1ede58SGreg Kroah-Hartman bus_put(bus); 235afdce75fSGreg Kroah-Hartman return err; 236afdce75fSGreg Kroah-Hartman } 237*4f4b3743SDaniel Vetter static DRIVER_ATTR_IGNORE_LOCKDEP(bind, S_IWUSR, NULL, bind_store); 238afdce75fSGreg Kroah-Hartman 239b8c5cec2SKay Sievers static ssize_t show_drivers_autoprobe(struct bus_type *bus, char *buf) 240b8c5cec2SKay Sievers { 241c6f7e72aSGreg Kroah-Hartman return sprintf(buf, "%d\n", bus->p->drivers_autoprobe); 242b8c5cec2SKay Sievers } 243b8c5cec2SKay Sievers 244b8c5cec2SKay Sievers static ssize_t store_drivers_autoprobe(struct bus_type *bus, 245b8c5cec2SKay Sievers const char *buf, size_t count) 246b8c5cec2SKay Sievers { 247b8c5cec2SKay Sievers if (buf[0] == '0') 248c6f7e72aSGreg Kroah-Hartman bus->p->drivers_autoprobe = 0; 249b8c5cec2SKay Sievers else 250c6f7e72aSGreg Kroah-Hartman bus->p->drivers_autoprobe = 1; 251b8c5cec2SKay Sievers return count; 252b8c5cec2SKay Sievers } 253b8c5cec2SKay Sievers 254b8c5cec2SKay Sievers static ssize_t store_drivers_probe(struct bus_type *bus, 255b8c5cec2SKay Sievers const char *buf, size_t count) 256b8c5cec2SKay Sievers { 257b8c5cec2SKay Sievers struct device *dev; 2580372ffb3SAlex Williamson int err = -EINVAL; 259b8c5cec2SKay Sievers 2601f9ffc04SGreg Kroah-Hartman dev = bus_find_device_by_name(bus, NULL, buf); 261b8c5cec2SKay Sievers if (!dev) 262b8c5cec2SKay Sievers return -ENODEV; 2630372ffb3SAlex Williamson if (bus_rescan_devices_helper(dev, NULL) == 0) 2640372ffb3SAlex Williamson err = count; 2650372ffb3SAlex Williamson put_device(dev); 2660372ffb3SAlex Williamson return err; 267b8c5cec2SKay Sievers } 268151ef38fSGreg Kroah-Hartman 269465c7a3aSmochel@digitalimplant.org static struct device *next_device(struct klist_iter *i) 270465c7a3aSmochel@digitalimplant.org { 271465c7a3aSmochel@digitalimplant.org struct klist_node *n = klist_next(i); 272ae1b4171SGreg Kroah-Hartman struct device *dev = NULL; 273ae1b4171SGreg Kroah-Hartman struct device_private *dev_prv; 274ae1b4171SGreg Kroah-Hartman 275ae1b4171SGreg Kroah-Hartman if (n) { 276ae1b4171SGreg Kroah-Hartman dev_prv = to_device_private_bus(n); 277ae1b4171SGreg Kroah-Hartman dev = dev_prv->device; 278ae1b4171SGreg Kroah-Hartman } 279ae1b4171SGreg Kroah-Hartman return dev; 280465c7a3aSmochel@digitalimplant.org } 281465c7a3aSmochel@digitalimplant.org 2821da177e4SLinus Torvalds /** 2831da177e4SLinus Torvalds * bus_for_each_dev - device iterator. 2841da177e4SLinus Torvalds * @bus: bus type. 2851da177e4SLinus Torvalds * @start: device to start iterating from. 2861da177e4SLinus Torvalds * @data: data for the callback. 2871da177e4SLinus Torvalds * @fn: function to be called for each device. 2881da177e4SLinus Torvalds * 2891da177e4SLinus Torvalds * Iterate over @bus's list of devices, and call @fn for each, 2901da177e4SLinus Torvalds * passing it @data. If @start is not NULL, we use that device to 2911da177e4SLinus Torvalds * begin iterating from. 2921da177e4SLinus Torvalds * 2931da177e4SLinus Torvalds * We check the return of @fn each time. If it returns anything 2941da177e4SLinus Torvalds * other than 0, we break out and return that value. 2951da177e4SLinus Torvalds * 2961da177e4SLinus Torvalds * NOTE: The device that returns a non-zero value is not retained 2971da177e4SLinus Torvalds * in any way, nor is its refcount incremented. If the caller needs 2980fa1b0a1SAlex Chiang * to retain this data, it should do so, and increment the reference 2991da177e4SLinus Torvalds * count in the supplied callback. 3001da177e4SLinus Torvalds */ 3011da177e4SLinus Torvalds int bus_for_each_dev(struct bus_type *bus, struct device *start, 3021da177e4SLinus Torvalds void *data, int (*fn)(struct device *, void *)) 3031da177e4SLinus Torvalds { 304465c7a3aSmochel@digitalimplant.org struct klist_iter i; 305465c7a3aSmochel@digitalimplant.org struct device *dev; 306465c7a3aSmochel@digitalimplant.org int error = 0; 3071da177e4SLinus Torvalds 3084fa3e78bSBjorn Helgaas if (!bus || !bus->p) 309465c7a3aSmochel@digitalimplant.org return -EINVAL; 310465c7a3aSmochel@digitalimplant.org 3117cd9c9bbSGreg Kroah-Hartman klist_iter_init_node(&bus->p->klist_devices, &i, 312ae1b4171SGreg Kroah-Hartman (start ? &start->p->knode_bus : NULL)); 31393ead7c9SGimcuan Hui while (!error && (dev = next_device(&i))) 314465c7a3aSmochel@digitalimplant.org error = fn(dev, data); 315465c7a3aSmochel@digitalimplant.org klist_iter_exit(&i); 316465c7a3aSmochel@digitalimplant.org return error; 3171da177e4SLinus Torvalds } 3184a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_for_each_dev); 3191da177e4SLinus Torvalds 3200edb5860SCornelia Huck /** 3210edb5860SCornelia Huck * bus_find_device - device iterator for locating a particular device. 3220edb5860SCornelia Huck * @bus: bus type 3230edb5860SCornelia Huck * @start: Device to begin with 3240edb5860SCornelia Huck * @data: Data to pass to match function 3250edb5860SCornelia Huck * @match: Callback function to check device 3260edb5860SCornelia Huck * 3270edb5860SCornelia Huck * This is similar to the bus_for_each_dev() function above, but it 3280edb5860SCornelia Huck * returns a reference to a device that is 'found' for later use, as 3290edb5860SCornelia Huck * determined by the @match callback. 3300edb5860SCornelia Huck * 3310edb5860SCornelia Huck * The callback should return 0 if the device doesn't match and non-zero 3320edb5860SCornelia Huck * if it does. If the callback returns non-zero, this function will 3330edb5860SCornelia Huck * return to the caller and not iterate over any more devices. 3340edb5860SCornelia Huck */ 3350edb5860SCornelia Huck struct device *bus_find_device(struct bus_type *bus, 3360edb5860SCornelia Huck struct device *start, void *data, 3374a3ad20cSGreg Kroah-Hartman int (*match)(struct device *dev, void *data)) 3380edb5860SCornelia Huck { 3390edb5860SCornelia Huck struct klist_iter i; 3400edb5860SCornelia Huck struct device *dev; 3410edb5860SCornelia Huck 3424fa3e78bSBjorn Helgaas if (!bus || !bus->p) 3430edb5860SCornelia Huck return NULL; 3440edb5860SCornelia Huck 3457cd9c9bbSGreg Kroah-Hartman klist_iter_init_node(&bus->p->klist_devices, &i, 3467cd9c9bbSGreg Kroah-Hartman (start ? &start->p->knode_bus : NULL)); 3470edb5860SCornelia Huck while ((dev = next_device(&i))) 3480edb5860SCornelia Huck if (match(dev, data) && get_device(dev)) 3490edb5860SCornelia Huck break; 3500edb5860SCornelia Huck klist_iter_exit(&i); 3510edb5860SCornelia Huck return dev; 3520edb5860SCornelia Huck } 3534a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_find_device); 35438fdac3cSmochel@digitalimplant.org 3551f9ffc04SGreg Kroah-Hartman static int match_name(struct device *dev, void *data) 3561f9ffc04SGreg Kroah-Hartman { 3571f9ffc04SGreg Kroah-Hartman const char *name = data; 3581f9ffc04SGreg Kroah-Hartman 3591e0b2cf9SKay Sievers return sysfs_streq(name, dev_name(dev)); 3601f9ffc04SGreg Kroah-Hartman } 3611f9ffc04SGreg Kroah-Hartman 3621f9ffc04SGreg Kroah-Hartman /** 3631f9ffc04SGreg Kroah-Hartman * bus_find_device_by_name - device iterator for locating a particular device of a specific name 3641f9ffc04SGreg Kroah-Hartman * @bus: bus type 3651f9ffc04SGreg Kroah-Hartman * @start: Device to begin with 3661f9ffc04SGreg Kroah-Hartman * @name: name of the device to match 3671f9ffc04SGreg Kroah-Hartman * 3681f9ffc04SGreg Kroah-Hartman * This is similar to the bus_find_device() function above, but it handles 3691f9ffc04SGreg Kroah-Hartman * searching by a name automatically, no need to write another strcmp matching 3701f9ffc04SGreg Kroah-Hartman * function. 3711f9ffc04SGreg Kroah-Hartman */ 3721f9ffc04SGreg Kroah-Hartman struct device *bus_find_device_by_name(struct bus_type *bus, 3731f9ffc04SGreg Kroah-Hartman struct device *start, const char *name) 3741f9ffc04SGreg Kroah-Hartman { 3751f9ffc04SGreg Kroah-Hartman return bus_find_device(bus, start, (void *)name, match_name); 3761f9ffc04SGreg Kroah-Hartman } 3771f9ffc04SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_find_device_by_name); 3781f9ffc04SGreg Kroah-Hartman 379ca22e56dSKay Sievers /** 380ca22e56dSKay Sievers * subsys_find_device_by_id - find a device with a specific enumeration number 381ca22e56dSKay Sievers * @subsys: subsystem 382ca22e56dSKay Sievers * @id: index 'id' in struct device 383ca22e56dSKay Sievers * @hint: device to check first 384ca22e56dSKay Sievers * 385ca22e56dSKay Sievers * Check the hint's next object and if it is a match return it directly, 386ca22e56dSKay Sievers * otherwise, fall back to a full list search. Either way a reference for 387ca22e56dSKay Sievers * the returned object is taken. 388ca22e56dSKay Sievers */ 389ca22e56dSKay Sievers struct device *subsys_find_device_by_id(struct bus_type *subsys, unsigned int id, 390ca22e56dSKay Sievers struct device *hint) 391ca22e56dSKay Sievers { 392ca22e56dSKay Sievers struct klist_iter i; 393ca22e56dSKay Sievers struct device *dev; 394ca22e56dSKay Sievers 395ca22e56dSKay Sievers if (!subsys) 396ca22e56dSKay Sievers return NULL; 397ca22e56dSKay Sievers 398ca22e56dSKay Sievers if (hint) { 3997cd9c9bbSGreg Kroah-Hartman klist_iter_init_node(&subsys->p->klist_devices, &i, &hint->p->knode_bus); 400ca22e56dSKay Sievers dev = next_device(&i); 401ca22e56dSKay Sievers if (dev && dev->id == id && get_device(dev)) { 402ca22e56dSKay Sievers klist_iter_exit(&i); 403ca22e56dSKay Sievers return dev; 404ca22e56dSKay Sievers } 405ca22e56dSKay Sievers klist_iter_exit(&i); 406ca22e56dSKay Sievers } 407ca22e56dSKay Sievers 408ca22e56dSKay Sievers klist_iter_init_node(&subsys->p->klist_devices, &i, NULL); 409ca22e56dSKay Sievers while ((dev = next_device(&i))) { 410ca22e56dSKay Sievers if (dev->id == id && get_device(dev)) { 411ca22e56dSKay Sievers klist_iter_exit(&i); 412ca22e56dSKay Sievers return dev; 413ca22e56dSKay Sievers } 414ca22e56dSKay Sievers } 415ca22e56dSKay Sievers klist_iter_exit(&i); 416ca22e56dSKay Sievers return NULL; 417ca22e56dSKay Sievers } 418ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_find_device_by_id); 419ca22e56dSKay Sievers 42038fdac3cSmochel@digitalimplant.org static struct device_driver *next_driver(struct klist_iter *i) 42138fdac3cSmochel@digitalimplant.org { 42238fdac3cSmochel@digitalimplant.org struct klist_node *n = klist_next(i); 423e5dd1278SGreg Kroah-Hartman struct driver_private *drv_priv; 424e5dd1278SGreg Kroah-Hartman 425e5dd1278SGreg Kroah-Hartman if (n) { 426e5dd1278SGreg Kroah-Hartman drv_priv = container_of(n, struct driver_private, knode_bus); 427e5dd1278SGreg Kroah-Hartman return drv_priv->driver; 428e5dd1278SGreg Kroah-Hartman } 429e5dd1278SGreg Kroah-Hartman return NULL; 43038fdac3cSmochel@digitalimplant.org } 43138fdac3cSmochel@digitalimplant.org 4321da177e4SLinus Torvalds /** 4331da177e4SLinus Torvalds * bus_for_each_drv - driver iterator 4341da177e4SLinus Torvalds * @bus: bus we're dealing with. 4351da177e4SLinus Torvalds * @start: driver to start iterating on. 4361da177e4SLinus Torvalds * @data: data to pass to the callback. 4371da177e4SLinus Torvalds * @fn: function to call for each driver. 4381da177e4SLinus Torvalds * 4391da177e4SLinus Torvalds * This is nearly identical to the device iterator above. 4401da177e4SLinus Torvalds * We iterate over each driver that belongs to @bus, and call 4411da177e4SLinus Torvalds * @fn for each. If @fn returns anything but 0, we break out 4421da177e4SLinus Torvalds * and return it. If @start is not NULL, we use it as the head 4431da177e4SLinus Torvalds * of the list. 4441da177e4SLinus Torvalds * 4451da177e4SLinus Torvalds * NOTE: we don't return the driver that returns a non-zero 4461da177e4SLinus Torvalds * value, nor do we leave the reference count incremented for that 4471da177e4SLinus Torvalds * driver. If the caller needs to know that info, it must set it 4481da177e4SLinus Torvalds * in the callback. It must also be sure to increment the refcount 4491da177e4SLinus Torvalds * so it doesn't disappear before returning to the caller. 4501da177e4SLinus Torvalds */ 4511da177e4SLinus Torvalds int bus_for_each_drv(struct bus_type *bus, struct device_driver *start, 4521da177e4SLinus Torvalds void *data, int (*fn)(struct device_driver *, void *)) 4531da177e4SLinus Torvalds { 45438fdac3cSmochel@digitalimplant.org struct klist_iter i; 45538fdac3cSmochel@digitalimplant.org struct device_driver *drv; 45638fdac3cSmochel@digitalimplant.org int error = 0; 4571da177e4SLinus Torvalds 45838fdac3cSmochel@digitalimplant.org if (!bus) 45938fdac3cSmochel@digitalimplant.org return -EINVAL; 46038fdac3cSmochel@digitalimplant.org 4617cd9c9bbSGreg Kroah-Hartman klist_iter_init_node(&bus->p->klist_drivers, &i, 462e5dd1278SGreg Kroah-Hartman start ? &start->p->knode_bus : NULL); 46338fdac3cSmochel@digitalimplant.org while ((drv = next_driver(&i)) && !error) 46438fdac3cSmochel@digitalimplant.org error = fn(drv, data); 46538fdac3cSmochel@digitalimplant.org klist_iter_exit(&i); 46638fdac3cSmochel@digitalimplant.org return error; 4671da177e4SLinus Torvalds } 4684a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_for_each_drv); 4691da177e4SLinus Torvalds 4701da177e4SLinus Torvalds /** 4711da177e4SLinus Torvalds * bus_add_device - add device to bus 4721da177e4SLinus Torvalds * @dev: device being added 4731da177e4SLinus Torvalds * 4742023c610SAlan Stern * - Add device's bus attributes. 4752023c610SAlan Stern * - Create links to device's bus. 4761da177e4SLinus Torvalds * - Add the device to its bus's list of devices. 4771da177e4SLinus Torvalds */ 4781da177e4SLinus Torvalds int bus_add_device(struct device *dev) 4791da177e4SLinus Torvalds { 4805901d014SGreg Kroah-Hartman struct bus_type *bus = bus_get(dev->bus); 4811da177e4SLinus Torvalds int error = 0; 4821da177e4SLinus Torvalds 4831da177e4SLinus Torvalds if (bus) { 4841e0b2cf9SKay Sievers pr_debug("bus: '%s': add device %s\n", bus->name, dev_name(dev)); 485fa6fdb33SGreg Kroah-Hartman error = device_add_groups(dev, bus->dev_groups); 486fa6fdb33SGreg Kroah-Hartman if (error) 487b46c7337SGreg Kroah-Hartman goto out_put; 488c6f7e72aSGreg Kroah-Hartman error = sysfs_create_link(&bus->p->devices_kset->kobj, 4891e0b2cf9SKay Sievers &dev->kobj, dev_name(dev)); 490f86db396SAndrew Morton if (error) 4911c34203aSJunjie Mao goto out_groups; 492f86db396SAndrew Morton error = sysfs_create_link(&dev->kobj, 493c6f7e72aSGreg Kroah-Hartman &dev->bus->p->subsys.kobj, "subsystem"); 494f86db396SAndrew Morton if (error) 495513e7337SCornelia Huck goto out_subsys; 4962023c610SAlan Stern klist_add_tail(&dev->p->knode_bus, &bus->p->klist_devices); 4971da177e4SLinus Torvalds } 498513e7337SCornelia Huck return 0; 499513e7337SCornelia Huck 500513e7337SCornelia Huck out_subsys: 5011e0b2cf9SKay Sievers sysfs_remove_link(&bus->p->devices_kset->kobj, dev_name(dev)); 502fa6fdb33SGreg Kroah-Hartman out_groups: 503fa6fdb33SGreg Kroah-Hartman device_remove_groups(dev, bus->dev_groups); 504513e7337SCornelia Huck out_put: 505fc1ede58SGreg Kroah-Hartman bus_put(dev->bus); 5061da177e4SLinus Torvalds return error; 5071da177e4SLinus Torvalds } 5081da177e4SLinus Torvalds 5091da177e4SLinus Torvalds /** 5102023c610SAlan Stern * bus_probe_device - probe drivers for a new device 5112023c610SAlan Stern * @dev: device to probe 51253877d06SKay Sievers * 5132023c610SAlan Stern * - Automatically probe for a driver if the bus allows it. 51453877d06SKay Sievers */ 5152023c610SAlan Stern void bus_probe_device(struct device *dev) 51653877d06SKay Sievers { 51753877d06SKay Sievers struct bus_type *bus = dev->bus; 518ca22e56dSKay Sievers struct subsys_interface *sif; 51953877d06SKay Sievers 520ca22e56dSKay Sievers if (!bus) 521ca22e56dSKay Sievers return; 522ca22e56dSKay Sievers 523765230b5SDmitry Torokhov if (bus->p->drivers_autoprobe) 524765230b5SDmitry Torokhov device_initial_probe(dev); 525ca22e56dSKay Sievers 526ca22e56dSKay Sievers mutex_lock(&bus->p->mutex); 527ca22e56dSKay Sievers list_for_each_entry(sif, &bus->p->interfaces, node) 528ca22e56dSKay Sievers if (sif->add_dev) 529ca22e56dSKay Sievers sif->add_dev(dev, sif); 530ca22e56dSKay Sievers mutex_unlock(&bus->p->mutex); 531f86db396SAndrew Morton } 53253877d06SKay Sievers 53353877d06SKay Sievers /** 5341da177e4SLinus Torvalds * bus_remove_device - remove device from bus 5351da177e4SLinus Torvalds * @dev: device to be removed 5361da177e4SLinus Torvalds * 537ca22e56dSKay Sievers * - Remove device from all interfaces. 538ca22e56dSKay Sievers * - Remove symlink from bus' directory. 5391da177e4SLinus Torvalds * - Delete device from bus's list. 5401da177e4SLinus Torvalds * - Detach from its driver. 5411da177e4SLinus Torvalds * - Drop reference taken in bus_add_device(). 5421da177e4SLinus Torvalds */ 5431da177e4SLinus Torvalds void bus_remove_device(struct device *dev) 5441da177e4SLinus Torvalds { 545ca22e56dSKay Sievers struct bus_type *bus = dev->bus; 546ca22e56dSKay Sievers struct subsys_interface *sif; 547ca22e56dSKay Sievers 548ca22e56dSKay Sievers if (!bus) 549ca22e56dSKay Sievers return; 550ca22e56dSKay Sievers 551ca22e56dSKay Sievers mutex_lock(&bus->p->mutex); 552ca22e56dSKay Sievers list_for_each_entry(sif, &bus->p->interfaces, node) 553ca22e56dSKay Sievers if (sif->remove_dev) 554ca22e56dSKay Sievers sif->remove_dev(dev, sif); 555ca22e56dSKay Sievers mutex_unlock(&bus->p->mutex); 556ca22e56dSKay Sievers 557b9d9c82bSKay Sievers sysfs_remove_link(&dev->kobj, "subsystem"); 5584a3ad20cSGreg Kroah-Hartman sysfs_remove_link(&dev->bus->p->devices_kset->kobj, 5591e0b2cf9SKay Sievers dev_name(dev)); 560fa6fdb33SGreg Kroah-Hartman device_remove_groups(dev, dev->bus->dev_groups); 561ae1b4171SGreg Kroah-Hartman if (klist_node_attached(&dev->p->knode_bus)) 562ae1b4171SGreg Kroah-Hartman klist_del(&dev->p->knode_bus); 5633f62e570SGreg Kroah-Hartman 5644a3ad20cSGreg Kroah-Hartman pr_debug("bus: '%s': remove device %s\n", 5651e0b2cf9SKay Sievers dev->bus->name, dev_name(dev)); 5661da177e4SLinus Torvalds device_release_driver(dev); 567fc1ede58SGreg Kroah-Hartman bus_put(dev->bus); 5681da177e4SLinus Torvalds } 5691da177e4SLinus Torvalds 570f86db396SAndrew Morton static int __must_check add_bind_files(struct device_driver *drv) 571874c6241SGreg Kroah-Hartman { 572f86db396SAndrew Morton int ret; 573f86db396SAndrew Morton 574f86db396SAndrew Morton ret = driver_create_file(drv, &driver_attr_unbind); 575f86db396SAndrew Morton if (ret == 0) { 576f86db396SAndrew Morton ret = driver_create_file(drv, &driver_attr_bind); 577f86db396SAndrew Morton if (ret) 578f86db396SAndrew Morton driver_remove_file(drv, &driver_attr_unbind); 579f86db396SAndrew Morton } 580f86db396SAndrew Morton return ret; 581874c6241SGreg Kroah-Hartman } 582874c6241SGreg Kroah-Hartman 583874c6241SGreg Kroah-Hartman static void remove_bind_files(struct device_driver *drv) 584874c6241SGreg Kroah-Hartman { 585874c6241SGreg Kroah-Hartman driver_remove_file(drv, &driver_attr_bind); 586874c6241SGreg Kroah-Hartman driver_remove_file(drv, &driver_attr_unbind); 587874c6241SGreg Kroah-Hartman } 588b8c5cec2SKay Sievers 5898380770cSKay Sievers static BUS_ATTR(drivers_probe, S_IWUSR, NULL, store_drivers_probe); 5908380770cSKay Sievers static BUS_ATTR(drivers_autoprobe, S_IWUSR | S_IRUGO, 5918380770cSKay Sievers show_drivers_autoprobe, store_drivers_autoprobe); 5928380770cSKay Sievers 593b8c5cec2SKay Sievers static int add_probe_files(struct bus_type *bus) 594b8c5cec2SKay Sievers { 595b8c5cec2SKay Sievers int retval; 596b8c5cec2SKay Sievers 5978380770cSKay Sievers retval = bus_create_file(bus, &bus_attr_drivers_probe); 598b8c5cec2SKay Sievers if (retval) 599b8c5cec2SKay Sievers goto out; 600b8c5cec2SKay Sievers 6018380770cSKay Sievers retval = bus_create_file(bus, &bus_attr_drivers_autoprobe); 602b8c5cec2SKay Sievers if (retval) 6038380770cSKay Sievers bus_remove_file(bus, &bus_attr_drivers_probe); 604b8c5cec2SKay Sievers out: 605b8c5cec2SKay Sievers return retval; 606b8c5cec2SKay Sievers } 607b8c5cec2SKay Sievers 608b8c5cec2SKay Sievers static void remove_probe_files(struct bus_type *bus) 609b8c5cec2SKay Sievers { 6108380770cSKay Sievers bus_remove_file(bus, &bus_attr_drivers_autoprobe); 6118380770cSKay Sievers bus_remove_file(bus, &bus_attr_drivers_probe); 612b8c5cec2SKay Sievers } 6131da177e4SLinus Torvalds 6142581c9ccSGreg Kroah-Hartman static ssize_t uevent_store(struct device_driver *drv, const char *buf, 6152581c9ccSGreg Kroah-Hartman size_t count) 6167ac1cf4aSKay Sievers { 617df44b479SPeter Rajnoha int rc; 618df44b479SPeter Rajnoha 619df44b479SPeter Rajnoha rc = kobject_synth_uevent(&drv->p->kobj, buf, count); 620df44b479SPeter Rajnoha return rc ? rc : count; 6217ac1cf4aSKay Sievers } 6222581c9ccSGreg Kroah-Hartman static DRIVER_ATTR_WO(uevent); 6237ac1cf4aSKay Sievers 624765230b5SDmitry Torokhov static void driver_attach_async(void *_drv, async_cookie_t cookie) 625765230b5SDmitry Torokhov { 626765230b5SDmitry Torokhov struct device_driver *drv = _drv; 627765230b5SDmitry Torokhov int ret; 628765230b5SDmitry Torokhov 629765230b5SDmitry Torokhov ret = driver_attach(drv); 630765230b5SDmitry Torokhov 631765230b5SDmitry Torokhov pr_debug("bus: '%s': driver %s async attach completed: %d\n", 632765230b5SDmitry Torokhov drv->bus->name, drv->name, ret); 633765230b5SDmitry Torokhov } 634765230b5SDmitry Torokhov 6351da177e4SLinus Torvalds /** 6361da177e4SLinus Torvalds * bus_add_driver - Add a driver to the bus. 6371da177e4SLinus Torvalds * @drv: driver. 6381da177e4SLinus Torvalds */ 6391da177e4SLinus Torvalds int bus_add_driver(struct device_driver *drv) 6401da177e4SLinus Torvalds { 641e5dd1278SGreg Kroah-Hartman struct bus_type *bus; 642e5dd1278SGreg Kroah-Hartman struct driver_private *priv; 6431da177e4SLinus Torvalds int error = 0; 6441da177e4SLinus Torvalds 645e5dd1278SGreg Kroah-Hartman bus = bus_get(drv->bus); 646d9fd4d3bSJeff Garzik if (!bus) 6474f6e1945SGreg Kroah-Hartman return -EINVAL; 648d9fd4d3bSJeff Garzik 6497dc72b28SGreg Kroah-Hartman pr_debug("bus: '%s': add driver %s\n", bus->name, drv->name); 650e5dd1278SGreg Kroah-Hartman 651e5dd1278SGreg Kroah-Hartman priv = kzalloc(sizeof(*priv), GFP_KERNEL); 65207634464SCornelia Huck if (!priv) { 65307634464SCornelia Huck error = -ENOMEM; 65407634464SCornelia Huck goto out_put_bus; 65507634464SCornelia Huck } 656e5dd1278SGreg Kroah-Hartman klist_init(&priv->klist_devices, NULL, NULL); 657e5dd1278SGreg Kroah-Hartman priv->driver = drv; 658e5dd1278SGreg Kroah-Hartman drv->p = priv; 659c8e90d82SGreg Kroah-Hartman priv->kobj.kset = bus->p->drivers_kset; 660c8e90d82SGreg Kroah-Hartman error = kobject_init_and_add(&priv->kobj, &driver_ktype, NULL, 661c8e90d82SGreg Kroah-Hartman "%s", drv->name); 662dc0afa83SCornelia Huck if (error) 66307634464SCornelia Huck goto out_unregister; 6641da177e4SLinus Torvalds 665190888acSMing Lei klist_add_tail(&priv->knode_bus, &bus->p->klist_drivers); 666c6f7e72aSGreg Kroah-Hartman if (drv->bus->p->drivers_autoprobe) { 667765230b5SDmitry Torokhov if (driver_allows_async_probing(drv)) { 668765230b5SDmitry Torokhov pr_debug("bus: '%s': probing driver %s asynchronously\n", 669765230b5SDmitry Torokhov drv->bus->name, drv->name); 670765230b5SDmitry Torokhov async_schedule(driver_attach_async, drv); 671765230b5SDmitry Torokhov } else { 672f86db396SAndrew Morton error = driver_attach(drv); 673f86db396SAndrew Morton if (error) 674f86db396SAndrew Morton goto out_unregister; 675b8c5cec2SKay Sievers } 676765230b5SDmitry Torokhov } 6771da177e4SLinus Torvalds module_add_driver(drv->owner, drv); 6781da177e4SLinus Torvalds 6797ac1cf4aSKay Sievers error = driver_create_file(drv, &driver_attr_uevent); 6807ac1cf4aSKay Sievers if (error) { 6817ac1cf4aSKay Sievers printk(KERN_ERR "%s: uevent attr (%s) failed\n", 6822b3a302aSHarvey Harrison __func__, drv->name); 6837ac1cf4aSKay Sievers } 684e18945b1SGreg Kroah-Hartman error = driver_add_groups(drv, bus->drv_groups); 685f86db396SAndrew Morton if (error) { 686f86db396SAndrew Morton /* How the hell do we get out of this pickle? Give up */ 687ed0617b5SGreg Kroah-Hartman printk(KERN_ERR "%s: driver_create_groups(%s) failed\n", 688ed0617b5SGreg Kroah-Hartman __func__, drv->name); 689e18945b1SGreg Kroah-Hartman } 6901a6f2a75SDmitry Torokhov 6911a6f2a75SDmitry Torokhov if (!drv->suppress_bind_attrs) { 692f86db396SAndrew Morton error = add_bind_files(drv); 693f86db396SAndrew Morton if (error) { 694f86db396SAndrew Morton /* Ditto */ 695f86db396SAndrew Morton printk(KERN_ERR "%s: add_bind_files(%s) failed\n", 6962b3a302aSHarvey Harrison __func__, drv->name); 697f86db396SAndrew Morton } 6981a6f2a75SDmitry Torokhov } 699d9fd4d3bSJeff Garzik 7005c8563d7SKay Sievers return 0; 7011a6f2a75SDmitry Torokhov 702f86db396SAndrew Morton out_unregister: 70399b28f1bSPhil Carmody kobject_put(&priv->kobj); 7040f9b011dSChristophe JAILLET /* drv->p is freed in driver_release() */ 7055c8563d7SKay Sievers drv->p = NULL; 706f86db396SAndrew Morton out_put_bus: 707fc1ede58SGreg Kroah-Hartman bus_put(bus); 708f86db396SAndrew Morton return error; 7091da177e4SLinus Torvalds } 7101da177e4SLinus Torvalds 7111da177e4SLinus Torvalds /** 7121da177e4SLinus Torvalds * bus_remove_driver - delete driver from bus's knowledge. 7131da177e4SLinus Torvalds * @drv: driver. 7141da177e4SLinus Torvalds * 7151da177e4SLinus Torvalds * Detach the driver from the devices it controls, and remove 7161da177e4SLinus Torvalds * it from its bus's list of drivers. Finally, we drop the reference 7171da177e4SLinus Torvalds * to the bus we took in bus_add_driver(). 7181da177e4SLinus Torvalds */ 7191da177e4SLinus Torvalds void bus_remove_driver(struct device_driver *drv) 7201da177e4SLinus Torvalds { 721d9fd4d3bSJeff Garzik if (!drv->bus) 722d9fd4d3bSJeff Garzik return; 723d9fd4d3bSJeff Garzik 7241a6f2a75SDmitry Torokhov if (!drv->suppress_bind_attrs) 725874c6241SGreg Kroah-Hartman remove_bind_files(drv); 726ed0617b5SGreg Kroah-Hartman driver_remove_groups(drv, drv->bus->drv_groups); 7277ac1cf4aSKay Sievers driver_remove_file(drv, &driver_attr_uevent); 728e5dd1278SGreg Kroah-Hartman klist_remove(&drv->p->knode_bus); 7297dc72b28SGreg Kroah-Hartman pr_debug("bus: '%s': remove driver %s\n", drv->bus->name, drv->name); 7301da177e4SLinus Torvalds driver_detach(drv); 7311da177e4SLinus Torvalds module_remove_driver(drv); 732c10997f6SGreg Kroah-Hartman kobject_put(&drv->p->kobj); 733fc1ede58SGreg Kroah-Hartman bus_put(drv->bus); 7341da177e4SLinus Torvalds } 7351da177e4SLinus Torvalds 7361da177e4SLinus Torvalds /* Helper for bus_rescan_devices's iter */ 737f86db396SAndrew Morton static int __must_check bus_rescan_devices_helper(struct device *dev, 738f86db396SAndrew Morton void *data) 7391da177e4SLinus Torvalds { 740f86db396SAndrew Morton int ret = 0; 741f86db396SAndrew Morton 742bf74ad5bSAlan Stern if (!dev->driver) { 7438c97a46aSMartin Liu if (dev->parent && dev->bus->need_parent_lock) 7448e9394ceSGreg Kroah-Hartman device_lock(dev->parent); 745f86db396SAndrew Morton ret = device_attach(dev); 7468c97a46aSMartin Liu if (dev->parent && dev->bus->need_parent_lock) 7478e9394ceSGreg Kroah-Hartman device_unlock(dev->parent); 748bf74ad5bSAlan Stern } 749f86db396SAndrew Morton return ret < 0 ? ret : 0; 7501da177e4SLinus Torvalds } 7511da177e4SLinus Torvalds 7521da177e4SLinus Torvalds /** 7531da177e4SLinus Torvalds * bus_rescan_devices - rescan devices on the bus for possible drivers 7541da177e4SLinus Torvalds * @bus: the bus to scan. 7551da177e4SLinus Torvalds * 7561da177e4SLinus Torvalds * This function will look for devices on the bus with no driver 75723d3d602SGreg Kroah-Hartman * attached and rescan it against existing drivers to see if it matches 75823d3d602SGreg Kroah-Hartman * any by calling device_attach() for the unbound devices. 7591da177e4SLinus Torvalds */ 760f86db396SAndrew Morton int bus_rescan_devices(struct bus_type *bus) 7611da177e4SLinus Torvalds { 762f86db396SAndrew Morton return bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper); 7631da177e4SLinus Torvalds } 7644a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_rescan_devices); 7651da177e4SLinus Torvalds 766e935d5daSMoore, Eric /** 767e935d5daSMoore, Eric * device_reprobe - remove driver for a device and probe for a new driver 768e935d5daSMoore, Eric * @dev: the device to reprobe 769e935d5daSMoore, Eric * 770e935d5daSMoore, Eric * This function detaches the attached driver (if any) for the given 771e935d5daSMoore, Eric * device and restarts the driver probing process. It is intended 772e935d5daSMoore, Eric * to use if probing criteria changed during a devices lifetime and 773e935d5daSMoore, Eric * driver attachment should change accordingly. 774e935d5daSMoore, Eric */ 775f86db396SAndrew Morton int device_reprobe(struct device *dev) 776e935d5daSMoore, Eric { 777e935d5daSMoore, Eric if (dev->driver) { 7788c97a46aSMartin Liu if (dev->parent && dev->bus->need_parent_lock) 7798e9394ceSGreg Kroah-Hartman device_lock(dev->parent); 780e935d5daSMoore, Eric device_release_driver(dev); 7818c97a46aSMartin Liu if (dev->parent && dev->bus->need_parent_lock) 7828e9394ceSGreg Kroah-Hartman device_unlock(dev->parent); 783e935d5daSMoore, Eric } 784f86db396SAndrew Morton return bus_rescan_devices_helper(dev, NULL); 785e935d5daSMoore, Eric } 786e935d5daSMoore, Eric EXPORT_SYMBOL_GPL(device_reprobe); 7871da177e4SLinus Torvalds 7881da177e4SLinus Torvalds /** 7891da177e4SLinus Torvalds * find_bus - locate bus by name. 7901da177e4SLinus Torvalds * @name: name of bus. 7911da177e4SLinus Torvalds * 7921da177e4SLinus Torvalds * Call kset_find_obj() to iterate over list of buses to 7931da177e4SLinus Torvalds * find a bus by name. Return bus if found. 7941da177e4SLinus Torvalds * 7951da177e4SLinus Torvalds * Note that kset_find_obj increments bus' reference count. 7961da177e4SLinus Torvalds */ 7977e4ef085SAdrian Bunk #if 0 7981da177e4SLinus Torvalds struct bus_type *find_bus(char *name) 7991da177e4SLinus Torvalds { 80059a54833SGreg Kroah-Hartman struct kobject *k = kset_find_obj(bus_kset, name); 8011da177e4SLinus Torvalds return k ? to_bus(k) : NULL; 8021da177e4SLinus Torvalds } 8037e4ef085SAdrian Bunk #endif /* 0 */ 8041da177e4SLinus Torvalds 80512478ba0SGreg Kroah-Hartman static int bus_add_groups(struct bus_type *bus, 80612478ba0SGreg Kroah-Hartman const struct attribute_group **groups) 80712478ba0SGreg Kroah-Hartman { 8083e9b2baeSGreg Kroah-Hartman return sysfs_create_groups(&bus->p->subsys.kobj, groups); 80912478ba0SGreg Kroah-Hartman } 81012478ba0SGreg Kroah-Hartman 81112478ba0SGreg Kroah-Hartman static void bus_remove_groups(struct bus_type *bus, 81212478ba0SGreg Kroah-Hartman const struct attribute_group **groups) 81312478ba0SGreg Kroah-Hartman { 8143e9b2baeSGreg Kroah-Hartman sysfs_remove_groups(&bus->p->subsys.kobj, groups); 81512478ba0SGreg Kroah-Hartman } 81612478ba0SGreg Kroah-Hartman 81734bb61f9SJames Bottomley static void klist_devices_get(struct klist_node *n) 81834bb61f9SJames Bottomley { 819ae1b4171SGreg Kroah-Hartman struct device_private *dev_prv = to_device_private_bus(n); 820ae1b4171SGreg Kroah-Hartman struct device *dev = dev_prv->device; 82134bb61f9SJames Bottomley 82234bb61f9SJames Bottomley get_device(dev); 82334bb61f9SJames Bottomley } 82434bb61f9SJames Bottomley 82534bb61f9SJames Bottomley static void klist_devices_put(struct klist_node *n) 82634bb61f9SJames Bottomley { 827ae1b4171SGreg Kroah-Hartman struct device_private *dev_prv = to_device_private_bus(n); 828ae1b4171SGreg Kroah-Hartman struct device *dev = dev_prv->device; 82934bb61f9SJames Bottomley 83034bb61f9SJames Bottomley put_device(dev); 83134bb61f9SJames Bottomley } 83234bb61f9SJames Bottomley 8337ac1cf4aSKay Sievers static ssize_t bus_uevent_store(struct bus_type *bus, 8347ac1cf4aSKay Sievers const char *buf, size_t count) 8357ac1cf4aSKay Sievers { 836df44b479SPeter Rajnoha int rc; 837df44b479SPeter Rajnoha 838df44b479SPeter Rajnoha rc = kobject_synth_uevent(&bus->p->subsys.kobj, buf, count); 839df44b479SPeter Rajnoha return rc ? rc : count; 8407ac1cf4aSKay Sievers } 8417ac1cf4aSKay Sievers static BUS_ATTR(uevent, S_IWUSR, NULL, bus_uevent_store); 8427ac1cf4aSKay Sievers 8431da177e4SLinus Torvalds /** 844be871b7eSMichal Hocko * bus_register - register a driver-core subsystem 84578d79559SRandy Dunlap * @bus: bus to register 8461da177e4SLinus Torvalds * 84778d79559SRandy Dunlap * Once we have that, we register the bus with the kobject 8481da177e4SLinus Torvalds * infrastructure, then register the children subsystems it has: 849ca22e56dSKay Sievers * the devices and drivers that belong to the subsystem. 8501da177e4SLinus Torvalds */ 851be871b7eSMichal Hocko int bus_register(struct bus_type *bus) 8521da177e4SLinus Torvalds { 8531da177e4SLinus Torvalds int retval; 8546b6e39a6SKay Sievers struct subsys_private *priv; 855be871b7eSMichal Hocko struct lock_class_key *key = &bus->lock_key; 8561da177e4SLinus Torvalds 8576b6e39a6SKay Sievers priv = kzalloc(sizeof(struct subsys_private), GFP_KERNEL); 858c6f7e72aSGreg Kroah-Hartman if (!priv) 859c6f7e72aSGreg Kroah-Hartman return -ENOMEM; 860116af378SBenjamin Herrenschmidt 861c6f7e72aSGreg Kroah-Hartman priv->bus = bus; 862c6f7e72aSGreg Kroah-Hartman bus->p = priv; 863c6f7e72aSGreg Kroah-Hartman 864c6f7e72aSGreg Kroah-Hartman BLOCKING_INIT_NOTIFIER_HEAD(&priv->bus_notifier); 865c6f7e72aSGreg Kroah-Hartman 866c6f7e72aSGreg Kroah-Hartman retval = kobject_set_name(&priv->subsys.kobj, "%s", bus->name); 8671da177e4SLinus Torvalds if (retval) 8681da177e4SLinus Torvalds goto out; 8691da177e4SLinus Torvalds 870c6f7e72aSGreg Kroah-Hartman priv->subsys.kobj.kset = bus_kset; 871c6f7e72aSGreg Kroah-Hartman priv->subsys.kobj.ktype = &bus_ktype; 872c6f7e72aSGreg Kroah-Hartman priv->drivers_autoprobe = 1; 873d6b05b84SGreg Kroah-Hartman 874c6f7e72aSGreg Kroah-Hartman retval = kset_register(&priv->subsys); 8751da177e4SLinus Torvalds if (retval) 8761da177e4SLinus Torvalds goto out; 8771da177e4SLinus Torvalds 8787ac1cf4aSKay Sievers retval = bus_create_file(bus, &bus_attr_uevent); 8797ac1cf4aSKay Sievers if (retval) 8807ac1cf4aSKay Sievers goto bus_uevent_fail; 8817ac1cf4aSKay Sievers 882c6f7e72aSGreg Kroah-Hartman priv->devices_kset = kset_create_and_add("devices", NULL, 883c6f7e72aSGreg Kroah-Hartman &priv->subsys.kobj); 884c6f7e72aSGreg Kroah-Hartman if (!priv->devices_kset) { 8853d899596SGreg Kroah-Hartman retval = -ENOMEM; 8861da177e4SLinus Torvalds goto bus_devices_fail; 8873d899596SGreg Kroah-Hartman } 8881da177e4SLinus Torvalds 889c6f7e72aSGreg Kroah-Hartman priv->drivers_kset = kset_create_and_add("drivers", NULL, 890c6f7e72aSGreg Kroah-Hartman &priv->subsys.kobj); 891c6f7e72aSGreg Kroah-Hartman if (!priv->drivers_kset) { 8926dcec251SGreg Kroah-Hartman retval = -ENOMEM; 8931da177e4SLinus Torvalds goto bus_drivers_fail; 8946dcec251SGreg Kroah-Hartman } 895465c7a3aSmochel@digitalimplant.org 896ca22e56dSKay Sievers INIT_LIST_HEAD(&priv->interfaces); 897ca22e56dSKay Sievers __mutex_init(&priv->mutex, "subsys mutex", key); 898c6f7e72aSGreg Kroah-Hartman klist_init(&priv->klist_devices, klist_devices_get, klist_devices_put); 899c6f7e72aSGreg Kroah-Hartman klist_init(&priv->klist_drivers, NULL, NULL); 900b8c5cec2SKay Sievers 901b8c5cec2SKay Sievers retval = add_probe_files(bus); 902b8c5cec2SKay Sievers if (retval) 903b8c5cec2SKay Sievers goto bus_probe_files_fail; 904b8c5cec2SKay Sievers 90512478ba0SGreg Kroah-Hartman retval = bus_add_groups(bus, bus->bus_groups); 90612478ba0SGreg Kroah-Hartman if (retval) 90712478ba0SGreg Kroah-Hartman goto bus_groups_fail; 9081da177e4SLinus Torvalds 9097dc72b28SGreg Kroah-Hartman pr_debug("bus: '%s': registered\n", bus->name); 9101da177e4SLinus Torvalds return 0; 9111da177e4SLinus Torvalds 91212478ba0SGreg Kroah-Hartman bus_groups_fail: 913b8c5cec2SKay Sievers remove_probe_files(bus); 914b8c5cec2SKay Sievers bus_probe_files_fail: 915c6f7e72aSGreg Kroah-Hartman kset_unregister(bus->p->drivers_kset); 9161da177e4SLinus Torvalds bus_drivers_fail: 917c6f7e72aSGreg Kroah-Hartman kset_unregister(bus->p->devices_kset); 9181da177e4SLinus Torvalds bus_devices_fail: 9197ac1cf4aSKay Sievers bus_remove_file(bus, &bus_attr_uevent); 9207ac1cf4aSKay Sievers bus_uevent_fail: 921c6f7e72aSGreg Kroah-Hartman kset_unregister(&bus->p->subsys); 9221da177e4SLinus Torvalds out: 923600c20f3SJike Song kfree(bus->p); 924f48f3febSDave Young bus->p = NULL; 9251da177e4SLinus Torvalds return retval; 9261da177e4SLinus Torvalds } 927be871b7eSMichal Hocko EXPORT_SYMBOL_GPL(bus_register); 9281da177e4SLinus Torvalds 9291da177e4SLinus Torvalds /** 9301da177e4SLinus Torvalds * bus_unregister - remove a bus from the system 9311da177e4SLinus Torvalds * @bus: bus. 9321da177e4SLinus Torvalds * 9331da177e4SLinus Torvalds * Unregister the child subsystems and the bus itself. 934fc1ede58SGreg Kroah-Hartman * Finally, we call bus_put() to release the refcount 9351da177e4SLinus Torvalds */ 9361da177e4SLinus Torvalds void bus_unregister(struct bus_type *bus) 9371da177e4SLinus Torvalds { 9387dc72b28SGreg Kroah-Hartman pr_debug("bus: '%s': unregistering\n", bus->name); 939ca22e56dSKay Sievers if (bus->dev_root) 940ca22e56dSKay Sievers device_unregister(bus->dev_root); 94112478ba0SGreg Kroah-Hartman bus_remove_groups(bus, bus->bus_groups); 942b8c5cec2SKay Sievers remove_probe_files(bus); 943c6f7e72aSGreg Kroah-Hartman kset_unregister(bus->p->drivers_kset); 944c6f7e72aSGreg Kroah-Hartman kset_unregister(bus->p->devices_kset); 9457ac1cf4aSKay Sievers bus_remove_file(bus, &bus_attr_uevent); 946c6f7e72aSGreg Kroah-Hartman kset_unregister(&bus->p->subsys); 9471da177e4SLinus Torvalds } 9484a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_unregister); 9491da177e4SLinus Torvalds 950116af378SBenjamin Herrenschmidt int bus_register_notifier(struct bus_type *bus, struct notifier_block *nb) 951116af378SBenjamin Herrenschmidt { 952c6f7e72aSGreg Kroah-Hartman return blocking_notifier_chain_register(&bus->p->bus_notifier, nb); 953116af378SBenjamin Herrenschmidt } 954116af378SBenjamin Herrenschmidt EXPORT_SYMBOL_GPL(bus_register_notifier); 955116af378SBenjamin Herrenschmidt 956116af378SBenjamin Herrenschmidt int bus_unregister_notifier(struct bus_type *bus, struct notifier_block *nb) 957116af378SBenjamin Herrenschmidt { 958c6f7e72aSGreg Kroah-Hartman return blocking_notifier_chain_unregister(&bus->p->bus_notifier, nb); 959116af378SBenjamin Herrenschmidt } 960116af378SBenjamin Herrenschmidt EXPORT_SYMBOL_GPL(bus_unregister_notifier); 961116af378SBenjamin Herrenschmidt 9620fed80f7SGreg Kroah-Hartman struct kset *bus_get_kset(struct bus_type *bus) 9630fed80f7SGreg Kroah-Hartman { 964c6f7e72aSGreg Kroah-Hartman return &bus->p->subsys; 9650fed80f7SGreg Kroah-Hartman } 9660fed80f7SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_get_kset); 9670fed80f7SGreg Kroah-Hartman 968b249072eSGreg Kroah-Hartman struct klist *bus_get_device_klist(struct bus_type *bus) 969b249072eSGreg Kroah-Hartman { 970c6f7e72aSGreg Kroah-Hartman return &bus->p->klist_devices; 971b249072eSGreg Kroah-Hartman } 972b249072eSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_get_device_klist); 973b249072eSGreg Kroah-Hartman 97499178b03SGreg Kroah-Hartman /* 975dca25ebdSRobert P. J. Day * Yes, this forcibly breaks the klist abstraction temporarily. It 97699178b03SGreg Kroah-Hartman * just wants to sort the klist, not change reference counts and 97799178b03SGreg Kroah-Hartman * take/drop locks rapidly in the process. It does all this while 97899178b03SGreg Kroah-Hartman * holding the lock for the list, so objects can't otherwise be 97999178b03SGreg Kroah-Hartman * added/removed while we're swizzling. 98099178b03SGreg Kroah-Hartman */ 98199178b03SGreg Kroah-Hartman static void device_insertion_sort_klist(struct device *a, struct list_head *list, 98299178b03SGreg Kroah-Hartman int (*compare)(const struct device *a, 98399178b03SGreg Kroah-Hartman const struct device *b)) 98499178b03SGreg Kroah-Hartman { 98599178b03SGreg Kroah-Hartman struct klist_node *n; 986ae1b4171SGreg Kroah-Hartman struct device_private *dev_prv; 98799178b03SGreg Kroah-Hartman struct device *b; 98899178b03SGreg Kroah-Hartman 9894c62785eSGeliang Tang list_for_each_entry(n, list, n_node) { 990ae1b4171SGreg Kroah-Hartman dev_prv = to_device_private_bus(n); 991ae1b4171SGreg Kroah-Hartman b = dev_prv->device; 99299178b03SGreg Kroah-Hartman if (compare(a, b) <= 0) { 993ae1b4171SGreg Kroah-Hartman list_move_tail(&a->p->knode_bus.n_node, 994ae1b4171SGreg Kroah-Hartman &b->p->knode_bus.n_node); 99599178b03SGreg Kroah-Hartman return; 99699178b03SGreg Kroah-Hartman } 99799178b03SGreg Kroah-Hartman } 998ae1b4171SGreg Kroah-Hartman list_move_tail(&a->p->knode_bus.n_node, list); 99999178b03SGreg Kroah-Hartman } 100099178b03SGreg Kroah-Hartman 100199178b03SGreg Kroah-Hartman void bus_sort_breadthfirst(struct bus_type *bus, 100299178b03SGreg Kroah-Hartman int (*compare)(const struct device *a, 100399178b03SGreg Kroah-Hartman const struct device *b)) 100499178b03SGreg Kroah-Hartman { 100599178b03SGreg Kroah-Hartman LIST_HEAD(sorted_devices); 10064c62785eSGeliang Tang struct klist_node *n, *tmp; 1007ae1b4171SGreg Kroah-Hartman struct device_private *dev_prv; 100899178b03SGreg Kroah-Hartman struct device *dev; 100999178b03SGreg Kroah-Hartman struct klist *device_klist; 101099178b03SGreg Kroah-Hartman 101199178b03SGreg Kroah-Hartman device_klist = bus_get_device_klist(bus); 101299178b03SGreg Kroah-Hartman 101399178b03SGreg Kroah-Hartman spin_lock(&device_klist->k_lock); 10144c62785eSGeliang Tang list_for_each_entry_safe(n, tmp, &device_klist->k_list, n_node) { 1015ae1b4171SGreg Kroah-Hartman dev_prv = to_device_private_bus(n); 1016ae1b4171SGreg Kroah-Hartman dev = dev_prv->device; 101799178b03SGreg Kroah-Hartman device_insertion_sort_klist(dev, &sorted_devices, compare); 101899178b03SGreg Kroah-Hartman } 101999178b03SGreg Kroah-Hartman list_splice(&sorted_devices, &device_klist->k_list); 102099178b03SGreg Kroah-Hartman spin_unlock(&device_klist->k_lock); 102199178b03SGreg Kroah-Hartman } 102299178b03SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_sort_breadthfirst); 102399178b03SGreg Kroah-Hartman 1024ca22e56dSKay Sievers /** 1025ca22e56dSKay Sievers * subsys_dev_iter_init - initialize subsys device iterator 1026ca22e56dSKay Sievers * @iter: subsys iterator to initialize 1027ca22e56dSKay Sievers * @subsys: the subsys we wanna iterate over 1028ca22e56dSKay Sievers * @start: the device to start iterating from, if any 1029ca22e56dSKay Sievers * @type: device_type of the devices to iterate over, NULL for all 1030ca22e56dSKay Sievers * 1031ca22e56dSKay Sievers * Initialize subsys iterator @iter such that it iterates over devices 1032ca22e56dSKay Sievers * of @subsys. If @start is set, the list iteration will start there, 1033ca22e56dSKay Sievers * otherwise if it is NULL, the iteration starts at the beginning of 1034ca22e56dSKay Sievers * the list. 1035ca22e56dSKay Sievers */ 10367cd9c9bbSGreg Kroah-Hartman void subsys_dev_iter_init(struct subsys_dev_iter *iter, struct bus_type *subsys, 1037ca22e56dSKay Sievers struct device *start, const struct device_type *type) 1038ca22e56dSKay Sievers { 1039ca22e56dSKay Sievers struct klist_node *start_knode = NULL; 1040ca22e56dSKay Sievers 1041ca22e56dSKay Sievers if (start) 1042ca22e56dSKay Sievers start_knode = &start->p->knode_bus; 10437cd9c9bbSGreg Kroah-Hartman klist_iter_init_node(&subsys->p->klist_devices, &iter->ki, start_knode); 1044ca22e56dSKay Sievers iter->type = type; 1045ca22e56dSKay Sievers } 1046ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_dev_iter_init); 1047ca22e56dSKay Sievers 1048ca22e56dSKay Sievers /** 1049ca22e56dSKay Sievers * subsys_dev_iter_next - iterate to the next device 1050ca22e56dSKay Sievers * @iter: subsys iterator to proceed 1051ca22e56dSKay Sievers * 1052ca22e56dSKay Sievers * Proceed @iter to the next device and return it. Returns NULL if 1053ca22e56dSKay Sievers * iteration is complete. 1054ca22e56dSKay Sievers * 1055ca22e56dSKay Sievers * The returned device is referenced and won't be released till 1056ca22e56dSKay Sievers * iterator is proceed to the next device or exited. The caller is 1057ca22e56dSKay Sievers * free to do whatever it wants to do with the device including 1058ca22e56dSKay Sievers * calling back into subsys code. 1059ca22e56dSKay Sievers */ 1060ca22e56dSKay Sievers struct device *subsys_dev_iter_next(struct subsys_dev_iter *iter) 1061ca22e56dSKay Sievers { 1062ca22e56dSKay Sievers struct klist_node *knode; 1063ca22e56dSKay Sievers struct device *dev; 1064ca22e56dSKay Sievers 1065ca22e56dSKay Sievers for (;;) { 1066ca22e56dSKay Sievers knode = klist_next(&iter->ki); 1067ca22e56dSKay Sievers if (!knode) 1068ca22e56dSKay Sievers return NULL; 1069371fd7a2SGeliang Tang dev = to_device_private_bus(knode)->device; 1070ca22e56dSKay Sievers if (!iter->type || iter->type == dev->type) 1071ca22e56dSKay Sievers return dev; 1072ca22e56dSKay Sievers } 1073ca22e56dSKay Sievers } 1074ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_dev_iter_next); 1075ca22e56dSKay Sievers 1076ca22e56dSKay Sievers /** 1077ca22e56dSKay Sievers * subsys_dev_iter_exit - finish iteration 1078ca22e56dSKay Sievers * @iter: subsys iterator to finish 1079ca22e56dSKay Sievers * 1080ca22e56dSKay Sievers * Finish an iteration. Always call this function after iteration is 1081ca22e56dSKay Sievers * complete whether the iteration ran till the end or not. 1082ca22e56dSKay Sievers */ 1083ca22e56dSKay Sievers void subsys_dev_iter_exit(struct subsys_dev_iter *iter) 1084ca22e56dSKay Sievers { 1085ca22e56dSKay Sievers klist_iter_exit(&iter->ki); 1086ca22e56dSKay Sievers } 1087ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_dev_iter_exit); 1088ca22e56dSKay Sievers 1089ca22e56dSKay Sievers int subsys_interface_register(struct subsys_interface *sif) 1090ca22e56dSKay Sievers { 1091ca22e56dSKay Sievers struct bus_type *subsys; 1092ca22e56dSKay Sievers struct subsys_dev_iter iter; 1093ca22e56dSKay Sievers struct device *dev; 1094ca22e56dSKay Sievers 1095ca22e56dSKay Sievers if (!sif || !sif->subsys) 1096ca22e56dSKay Sievers return -ENODEV; 1097ca22e56dSKay Sievers 1098ca22e56dSKay Sievers subsys = bus_get(sif->subsys); 1099ca22e56dSKay Sievers if (!subsys) 1100ca22e56dSKay Sievers return -EINVAL; 1101ca22e56dSKay Sievers 1102ca22e56dSKay Sievers mutex_lock(&subsys->p->mutex); 1103ca22e56dSKay Sievers list_add_tail(&sif->node, &subsys->p->interfaces); 1104ca22e56dSKay Sievers if (sif->add_dev) { 1105ca22e56dSKay Sievers subsys_dev_iter_init(&iter, subsys, NULL, NULL); 1106ca22e56dSKay Sievers while ((dev = subsys_dev_iter_next(&iter))) 1107ca22e56dSKay Sievers sif->add_dev(dev, sif); 1108ca22e56dSKay Sievers subsys_dev_iter_exit(&iter); 1109ca22e56dSKay Sievers } 1110ca22e56dSKay Sievers mutex_unlock(&subsys->p->mutex); 1111ca22e56dSKay Sievers 1112ca22e56dSKay Sievers return 0; 1113ca22e56dSKay Sievers } 1114ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_interface_register); 1115ca22e56dSKay Sievers 1116ca22e56dSKay Sievers void subsys_interface_unregister(struct subsys_interface *sif) 1117ca22e56dSKay Sievers { 11182b31594aSJonghwan Choi struct bus_type *subsys; 1119ca22e56dSKay Sievers struct subsys_dev_iter iter; 1120ca22e56dSKay Sievers struct device *dev; 1121ca22e56dSKay Sievers 11222b31594aSJonghwan Choi if (!sif || !sif->subsys) 1123ca22e56dSKay Sievers return; 1124ca22e56dSKay Sievers 11252b31594aSJonghwan Choi subsys = sif->subsys; 11262b31594aSJonghwan Choi 1127ca22e56dSKay Sievers mutex_lock(&subsys->p->mutex); 1128ca22e56dSKay Sievers list_del_init(&sif->node); 1129ca22e56dSKay Sievers if (sif->remove_dev) { 1130ca22e56dSKay Sievers subsys_dev_iter_init(&iter, subsys, NULL, NULL); 1131ca22e56dSKay Sievers while ((dev = subsys_dev_iter_next(&iter))) 1132ca22e56dSKay Sievers sif->remove_dev(dev, sif); 1133ca22e56dSKay Sievers subsys_dev_iter_exit(&iter); 1134ca22e56dSKay Sievers } 1135ca22e56dSKay Sievers mutex_unlock(&subsys->p->mutex); 1136ca22e56dSKay Sievers 1137ca22e56dSKay Sievers bus_put(subsys); 1138ca22e56dSKay Sievers } 1139ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_interface_unregister); 1140ca22e56dSKay Sievers 1141ca22e56dSKay Sievers static void system_root_device_release(struct device *dev) 1142ca22e56dSKay Sievers { 1143ca22e56dSKay Sievers kfree(dev); 1144ca22e56dSKay Sievers } 1145d73ce004STejun Heo 1146d73ce004STejun Heo static int subsys_register(struct bus_type *subsys, 1147d73ce004STejun Heo const struct attribute_group **groups, 1148d73ce004STejun Heo struct kobject *parent_of_root) 1149d73ce004STejun Heo { 1150d73ce004STejun Heo struct device *dev; 1151d73ce004STejun Heo int err; 1152d73ce004STejun Heo 1153d73ce004STejun Heo err = bus_register(subsys); 1154d73ce004STejun Heo if (err < 0) 1155d73ce004STejun Heo return err; 1156d73ce004STejun Heo 1157d73ce004STejun Heo dev = kzalloc(sizeof(struct device), GFP_KERNEL); 1158d73ce004STejun Heo if (!dev) { 1159d73ce004STejun Heo err = -ENOMEM; 1160d73ce004STejun Heo goto err_dev; 1161d73ce004STejun Heo } 1162d73ce004STejun Heo 1163d73ce004STejun Heo err = dev_set_name(dev, "%s", subsys->name); 1164d73ce004STejun Heo if (err < 0) 1165d73ce004STejun Heo goto err_name; 1166d73ce004STejun Heo 1167d73ce004STejun Heo dev->kobj.parent = parent_of_root; 1168d73ce004STejun Heo dev->groups = groups; 1169d73ce004STejun Heo dev->release = system_root_device_release; 1170d73ce004STejun Heo 1171d73ce004STejun Heo err = device_register(dev); 1172d73ce004STejun Heo if (err < 0) 1173d73ce004STejun Heo goto err_dev_reg; 1174d73ce004STejun Heo 1175d73ce004STejun Heo subsys->dev_root = dev; 1176d73ce004STejun Heo return 0; 1177d73ce004STejun Heo 1178d73ce004STejun Heo err_dev_reg: 1179d73ce004STejun Heo put_device(dev); 1180d73ce004STejun Heo dev = NULL; 1181d73ce004STejun Heo err_name: 1182d73ce004STejun Heo kfree(dev); 1183d73ce004STejun Heo err_dev: 1184d73ce004STejun Heo bus_unregister(subsys); 1185d73ce004STejun Heo return err; 1186d73ce004STejun Heo } 1187d73ce004STejun Heo 1188ca22e56dSKay Sievers /** 1189ca22e56dSKay Sievers * subsys_system_register - register a subsystem at /sys/devices/system/ 119078d79559SRandy Dunlap * @subsys: system subsystem 119178d79559SRandy Dunlap * @groups: default attributes for the root device 1192ca22e56dSKay Sievers * 1193ca22e56dSKay Sievers * All 'system' subsystems have a /sys/devices/system/<name> root device 1194ca22e56dSKay Sievers * with the name of the subsystem. The root device can carry subsystem- 1195ca22e56dSKay Sievers * wide attributes. All registered devices are below this single root 1196ca22e56dSKay Sievers * device and are named after the subsystem with a simple enumeration 1197e227867fSMasanari Iida * number appended. The registered devices are not explicitly named; 1198ca22e56dSKay Sievers * only 'id' in the device needs to be set. 1199ca22e56dSKay Sievers * 1200ca22e56dSKay Sievers * Do not use this interface for anything new, it exists for compatibility 1201ca22e56dSKay Sievers * with bad ideas only. New subsystems should use plain subsystems; and 1202ca22e56dSKay Sievers * add the subsystem-wide attributes should be added to the subsystem 1203ca22e56dSKay Sievers * directory itself and not some create fake root-device placed in 1204ca22e56dSKay Sievers * /sys/devices/system/<name>. 1205ca22e56dSKay Sievers */ 1206ca22e56dSKay Sievers int subsys_system_register(struct bus_type *subsys, 1207ca22e56dSKay Sievers const struct attribute_group **groups) 1208ca22e56dSKay Sievers { 1209d73ce004STejun Heo return subsys_register(subsys, groups, &system_kset->kobj); 1210ca22e56dSKay Sievers } 1211ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_system_register); 1212ca22e56dSKay Sievers 1213d73ce004STejun Heo /** 1214d73ce004STejun Heo * subsys_virtual_register - register a subsystem at /sys/devices/virtual/ 1215d73ce004STejun Heo * @subsys: virtual subsystem 1216d73ce004STejun Heo * @groups: default attributes for the root device 1217d73ce004STejun Heo * 1218d73ce004STejun Heo * All 'virtual' subsystems have a /sys/devices/system/<name> root device 1219d73ce004STejun Heo * with the name of the subystem. The root device can carry subsystem-wide 1220d73ce004STejun Heo * attributes. All registered devices are below this single root device. 1221d73ce004STejun Heo * There's no restriction on device naming. This is for kernel software 1222d73ce004STejun Heo * constructs which need sysfs interface. 1223d73ce004STejun Heo */ 1224d73ce004STejun Heo int subsys_virtual_register(struct bus_type *subsys, 1225d73ce004STejun Heo const struct attribute_group **groups) 1226d73ce004STejun Heo { 1227d73ce004STejun Heo struct kobject *virtual_dir; 1228d73ce004STejun Heo 1229d73ce004STejun Heo virtual_dir = virtual_device_parent(NULL); 1230d73ce004STejun Heo if (!virtual_dir) 1231d73ce004STejun Heo return -ENOMEM; 1232d73ce004STejun Heo 1233d73ce004STejun Heo return subsys_register(subsys, groups, virtual_dir); 1234d73ce004STejun Heo } 12351c04fc35SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(subsys_virtual_register); 1236d73ce004STejun Heo 12371da177e4SLinus Torvalds int __init buses_init(void) 12381da177e4SLinus Torvalds { 123959a54833SGreg Kroah-Hartman bus_kset = kset_create_and_add("bus", &bus_uevent_ops, NULL); 124059a54833SGreg Kroah-Hartman if (!bus_kset) 124159a54833SGreg Kroah-Hartman return -ENOMEM; 1242ca22e56dSKay Sievers 1243ca22e56dSKay Sievers system_kset = kset_create_and_add("system", NULL, &devices_kset->kobj); 1244ca22e56dSKay Sievers if (!system_kset) 1245ca22e56dSKay Sievers return -ENOMEM; 1246ca22e56dSKay Sievers 124759a54833SGreg Kroah-Hartman return 0; 12481da177e4SLinus Torvalds } 1249