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 271da177e4SLinus Torvalds #define to_bus_attr(_attr) container_of(_attr, struct bus_attribute, attr) 281da177e4SLinus Torvalds 291da177e4SLinus Torvalds /* 301da177e4SLinus Torvalds * sysfs bindings for drivers 311da177e4SLinus Torvalds */ 321da177e4SLinus Torvalds 331da177e4SLinus Torvalds #define to_drv_attr(_attr) container_of(_attr, struct driver_attribute, attr) 341da177e4SLinus Torvalds 354f4b3743SDaniel Vetter #define DRIVER_ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) \ 364f4b3743SDaniel Vetter struct driver_attribute driver_attr_##_name = \ 374f4b3743SDaniel Vetter __ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) 381da177e4SLinus Torvalds 39b8c5cec2SKay Sievers static int __must_check bus_rescan_devices_helper(struct device *dev, 40b8c5cec2SKay Sievers void *data); 41b8c5cec2SKay Sievers 425901d014SGreg Kroah-Hartman static struct bus_type *bus_get(struct bus_type *bus) 435901d014SGreg Kroah-Hartman { 44c6f7e72aSGreg Kroah-Hartman if (bus) { 45c6f7e72aSGreg Kroah-Hartman kset_get(&bus->p->subsys); 46c6f7e72aSGreg Kroah-Hartman return bus; 47c6f7e72aSGreg Kroah-Hartman } 48c6f7e72aSGreg Kroah-Hartman return NULL; 495901d014SGreg Kroah-Hartman } 505901d014SGreg Kroah-Hartman 51fc1ede58SGreg Kroah-Hartman static void bus_put(struct bus_type *bus) 52fc1ede58SGreg Kroah-Hartman { 53c6f7e72aSGreg Kroah-Hartman if (bus) 54c6f7e72aSGreg Kroah-Hartman kset_put(&bus->p->subsys); 55fc1ede58SGreg Kroah-Hartman } 56fc1ede58SGreg Kroah-Hartman 574a3ad20cSGreg Kroah-Hartman static ssize_t drv_attr_show(struct kobject *kobj, struct attribute *attr, 584a3ad20cSGreg Kroah-Hartman char *buf) 591da177e4SLinus Torvalds { 601da177e4SLinus Torvalds struct driver_attribute *drv_attr = to_drv_attr(attr); 61e5dd1278SGreg Kroah-Hartman struct driver_private *drv_priv = to_driver(kobj); 624a0c20bfSDmitry Torokhov ssize_t ret = -EIO; 631da177e4SLinus Torvalds 641da177e4SLinus Torvalds if (drv_attr->show) 65e5dd1278SGreg Kroah-Hartman ret = drv_attr->show(drv_priv->driver, buf); 661da177e4SLinus Torvalds return ret; 671da177e4SLinus Torvalds } 681da177e4SLinus Torvalds 694a3ad20cSGreg Kroah-Hartman static ssize_t drv_attr_store(struct kobject *kobj, struct attribute *attr, 701da177e4SLinus Torvalds const char *buf, size_t count) 711da177e4SLinus Torvalds { 721da177e4SLinus Torvalds struct driver_attribute *drv_attr = to_drv_attr(attr); 73e5dd1278SGreg Kroah-Hartman struct driver_private *drv_priv = to_driver(kobj); 744a0c20bfSDmitry Torokhov ssize_t ret = -EIO; 751da177e4SLinus Torvalds 761da177e4SLinus Torvalds if (drv_attr->store) 77e5dd1278SGreg Kroah-Hartman ret = drv_attr->store(drv_priv->driver, buf, count); 781da177e4SLinus Torvalds return ret; 791da177e4SLinus Torvalds } 801da177e4SLinus Torvalds 8152cf25d0SEmese Revfy static const struct sysfs_ops driver_sysfs_ops = { 821da177e4SLinus Torvalds .show = drv_attr_show, 831da177e4SLinus Torvalds .store = drv_attr_store, 841da177e4SLinus Torvalds }; 851da177e4SLinus Torvalds 861da177e4SLinus Torvalds static void driver_release(struct kobject *kobj) 871da177e4SLinus Torvalds { 88e5dd1278SGreg Kroah-Hartman struct driver_private *drv_priv = to_driver(kobj); 89e5dd1278SGreg Kroah-Hartman 902b3a302aSHarvey Harrison pr_debug("driver: '%s': %s\n", kobject_name(kobj), __func__); 91e5dd1278SGreg Kroah-Hartman kfree(drv_priv); 921da177e4SLinus Torvalds } 931da177e4SLinus Torvalds 94a1148fb0SGreg Kroah-Hartman static struct kobj_type driver_ktype = { 951da177e4SLinus Torvalds .sysfs_ops = &driver_sysfs_ops, 961da177e4SLinus Torvalds .release = driver_release, 971da177e4SLinus Torvalds }; 981da177e4SLinus Torvalds 991da177e4SLinus Torvalds /* 1001da177e4SLinus Torvalds * sysfs bindings for buses 1011da177e4SLinus Torvalds */ 1024a3ad20cSGreg Kroah-Hartman static ssize_t bus_attr_show(struct kobject *kobj, struct attribute *attr, 1034a3ad20cSGreg Kroah-Hartman char *buf) 1041da177e4SLinus Torvalds { 1051da177e4SLinus Torvalds struct bus_attribute *bus_attr = to_bus_attr(attr); 1066b6e39a6SKay Sievers struct subsys_private *subsys_priv = to_subsys_private(kobj); 1071da177e4SLinus Torvalds ssize_t ret = 0; 1081da177e4SLinus Torvalds 1091da177e4SLinus Torvalds if (bus_attr->show) 1106b6e39a6SKay Sievers ret = bus_attr->show(subsys_priv->bus, buf); 1111da177e4SLinus Torvalds return ret; 1121da177e4SLinus Torvalds } 1131da177e4SLinus Torvalds 1144a3ad20cSGreg Kroah-Hartman static ssize_t bus_attr_store(struct kobject *kobj, struct attribute *attr, 1151da177e4SLinus Torvalds const char *buf, size_t count) 1161da177e4SLinus Torvalds { 1171da177e4SLinus Torvalds struct bus_attribute *bus_attr = to_bus_attr(attr); 1186b6e39a6SKay Sievers struct subsys_private *subsys_priv = to_subsys_private(kobj); 1191da177e4SLinus Torvalds ssize_t ret = 0; 1201da177e4SLinus Torvalds 1211da177e4SLinus Torvalds if (bus_attr->store) 1226b6e39a6SKay Sievers ret = bus_attr->store(subsys_priv->bus, buf, count); 1231da177e4SLinus Torvalds return ret; 1241da177e4SLinus Torvalds } 1251da177e4SLinus Torvalds 12652cf25d0SEmese Revfy static const struct sysfs_ops bus_sysfs_ops = { 1271da177e4SLinus Torvalds .show = bus_attr_show, 1281da177e4SLinus Torvalds .store = bus_attr_store, 1291da177e4SLinus Torvalds }; 1301da177e4SLinus Torvalds 1311da177e4SLinus Torvalds int bus_create_file(struct bus_type *bus, struct bus_attribute *attr) 1321da177e4SLinus Torvalds { 1331da177e4SLinus Torvalds int error; 1345901d014SGreg Kroah-Hartman if (bus_get(bus)) { 135c6f7e72aSGreg Kroah-Hartman error = sysfs_create_file(&bus->p->subsys.kobj, &attr->attr); 136fc1ede58SGreg Kroah-Hartman bus_put(bus); 1371da177e4SLinus Torvalds } else 1381da177e4SLinus Torvalds error = -EINVAL; 1391da177e4SLinus Torvalds return error; 1401da177e4SLinus Torvalds } 1414a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_create_file); 1421da177e4SLinus Torvalds 1431da177e4SLinus Torvalds void bus_remove_file(struct bus_type *bus, struct bus_attribute *attr) 1441da177e4SLinus Torvalds { 1455901d014SGreg Kroah-Hartman if (bus_get(bus)) { 146c6f7e72aSGreg Kroah-Hartman sysfs_remove_file(&bus->p->subsys.kobj, &attr->attr); 147fc1ede58SGreg Kroah-Hartman bus_put(bus); 1481da177e4SLinus Torvalds } 1491da177e4SLinus Torvalds } 1504a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_remove_file); 1511da177e4SLinus Torvalds 152174be70bSBart Van Assche static void bus_release(struct kobject *kobj) 153174be70bSBart Van Assche { 154371fd7a2SGeliang Tang struct subsys_private *priv = to_subsys_private(kobj); 155174be70bSBart Van Assche struct bus_type *bus = priv->bus; 156174be70bSBart Van Assche 157174be70bSBart Van Assche kfree(priv); 158174be70bSBart Van Assche bus->p = NULL; 159174be70bSBart Van Assche } 160174be70bSBart Van Assche 16180f03e34SKay Sievers static struct kobj_type bus_ktype = { 1621da177e4SLinus Torvalds .sysfs_ops = &bus_sysfs_ops, 163174be70bSBart Van Assche .release = bus_release, 1641da177e4SLinus Torvalds }; 1651da177e4SLinus Torvalds 166c45a88bbSGreg Kroah-Hartman static int bus_uevent_filter(const struct kobject *kobj) 16780f03e34SKay Sievers { 168ee6d3dd4SWedson Almeida Filho const struct kobj_type *ktype = get_ktype(kobj); 16980f03e34SKay Sievers 17080f03e34SKay Sievers if (ktype == &bus_ktype) 17180f03e34SKay Sievers return 1; 17280f03e34SKay Sievers return 0; 17380f03e34SKay Sievers } 17480f03e34SKay Sievers 1759cd43611SEmese Revfy static const struct kset_uevent_ops bus_uevent_ops = { 17680f03e34SKay Sievers .filter = bus_uevent_filter, 17780f03e34SKay Sievers }; 17880f03e34SKay Sievers 17959a54833SGreg Kroah-Hartman static struct kset *bus_kset; 1801da177e4SLinus Torvalds 1812b08c8d0SAlan Stern /* Manually detach a device from its associated driver. */ 1822581c9ccSGreg Kroah-Hartman static ssize_t unbind_store(struct device_driver *drv, const char *buf, 1832581c9ccSGreg Kroah-Hartman size_t count) 184151ef38fSGreg Kroah-Hartman { 1855901d014SGreg Kroah-Hartman struct bus_type *bus = bus_get(drv->bus); 186151ef38fSGreg Kroah-Hartman struct device *dev; 187151ef38fSGreg Kroah-Hartman int err = -ENODEV; 188151ef38fSGreg Kroah-Hartman 1891f9ffc04SGreg Kroah-Hartman dev = bus_find_device_by_name(bus, NULL, buf); 1902b08c8d0SAlan Stern if (dev && dev->driver == drv) { 191ed88747cSAlexander Duyck device_driver_detach(dev); 192151ef38fSGreg Kroah-Hartman err = count; 193151ef38fSGreg Kroah-Hartman } 1942b08c8d0SAlan Stern put_device(dev); 195fc1ede58SGreg Kroah-Hartman bus_put(bus); 196151ef38fSGreg Kroah-Hartman return err; 197151ef38fSGreg Kroah-Hartman } 19816b0dd40SJinchao Wang static DRIVER_ATTR_IGNORE_LOCKDEP(unbind, 0200, NULL, unbind_store); 199151ef38fSGreg Kroah-Hartman 200afdce75fSGreg Kroah-Hartman /* 201afdce75fSGreg Kroah-Hartman * Manually attach a device to a driver. 202afdce75fSGreg Kroah-Hartman * Note: the driver must want to bind to the device, 203afdce75fSGreg Kroah-Hartman * it is not possible to override the driver's id table. 204afdce75fSGreg Kroah-Hartman */ 2052581c9ccSGreg Kroah-Hartman static ssize_t bind_store(struct device_driver *drv, const char *buf, 2062581c9ccSGreg Kroah-Hartman size_t count) 207afdce75fSGreg Kroah-Hartman { 2085901d014SGreg Kroah-Hartman struct bus_type *bus = bus_get(drv->bus); 209afdce75fSGreg Kroah-Hartman struct device *dev; 210afdce75fSGreg Kroah-Hartman int err = -ENODEV; 211afdce75fSGreg Kroah-Hartman 2121f9ffc04SGreg Kroah-Hartman dev = bus_find_device_by_name(bus, NULL, buf); 213204db60cSJason Gunthorpe if (dev && driver_match_device(drv, dev)) { 214ed88747cSAlexander Duyck err = device_driver_attach(drv, dev); 215ef6dcbddSChristoph Hellwig if (!err) { 2164a3ad20cSGreg Kroah-Hartman /* success */ 21737225401SRyan Wilson err = count; 218afdce75fSGreg Kroah-Hartman } 2194a3ad20cSGreg Kroah-Hartman } 2202b08c8d0SAlan Stern put_device(dev); 221fc1ede58SGreg Kroah-Hartman bus_put(bus); 222afdce75fSGreg Kroah-Hartman return err; 223afdce75fSGreg Kroah-Hartman } 22416b0dd40SJinchao Wang static DRIVER_ATTR_IGNORE_LOCKDEP(bind, 0200, NULL, bind_store); 225afdce75fSGreg Kroah-Hartman 2262e7189b6SGreg Kroah-Hartman static ssize_t drivers_autoprobe_show(struct bus_type *bus, char *buf) 227b8c5cec2SKay Sievers { 228948b3edbSJoe Perches return sysfs_emit(buf, "%d\n", bus->p->drivers_autoprobe); 229b8c5cec2SKay Sievers } 230b8c5cec2SKay Sievers 2312e7189b6SGreg Kroah-Hartman static ssize_t drivers_autoprobe_store(struct bus_type *bus, 232b8c5cec2SKay Sievers const char *buf, size_t count) 233b8c5cec2SKay Sievers { 234b8c5cec2SKay Sievers if (buf[0] == '0') 235c6f7e72aSGreg Kroah-Hartman bus->p->drivers_autoprobe = 0; 236b8c5cec2SKay Sievers else 237c6f7e72aSGreg Kroah-Hartman bus->p->drivers_autoprobe = 1; 238b8c5cec2SKay Sievers return count; 239b8c5cec2SKay Sievers } 240b8c5cec2SKay Sievers 2412e7189b6SGreg Kroah-Hartman static ssize_t drivers_probe_store(struct bus_type *bus, 242b8c5cec2SKay Sievers const char *buf, size_t count) 243b8c5cec2SKay Sievers { 244b8c5cec2SKay Sievers struct device *dev; 2450372ffb3SAlex Williamson int err = -EINVAL; 246b8c5cec2SKay Sievers 2471f9ffc04SGreg Kroah-Hartman dev = bus_find_device_by_name(bus, NULL, buf); 248b8c5cec2SKay Sievers if (!dev) 249b8c5cec2SKay Sievers return -ENODEV; 2500372ffb3SAlex Williamson if (bus_rescan_devices_helper(dev, NULL) == 0) 2510372ffb3SAlex Williamson err = count; 2520372ffb3SAlex Williamson put_device(dev); 2530372ffb3SAlex Williamson return err; 254b8c5cec2SKay Sievers } 255151ef38fSGreg Kroah-Hartman 256465c7a3aSmochel@digitalimplant.org static struct device *next_device(struct klist_iter *i) 257465c7a3aSmochel@digitalimplant.org { 258465c7a3aSmochel@digitalimplant.org struct klist_node *n = klist_next(i); 259ae1b4171SGreg Kroah-Hartman struct device *dev = NULL; 260ae1b4171SGreg Kroah-Hartman struct device_private *dev_prv; 261ae1b4171SGreg Kroah-Hartman 262ae1b4171SGreg Kroah-Hartman if (n) { 263ae1b4171SGreg Kroah-Hartman dev_prv = to_device_private_bus(n); 264ae1b4171SGreg Kroah-Hartman dev = dev_prv->device; 265ae1b4171SGreg Kroah-Hartman } 266ae1b4171SGreg Kroah-Hartman return dev; 267465c7a3aSmochel@digitalimplant.org } 268465c7a3aSmochel@digitalimplant.org 2691da177e4SLinus Torvalds /** 2701da177e4SLinus Torvalds * bus_for_each_dev - device iterator. 2711da177e4SLinus Torvalds * @bus: bus type. 2721da177e4SLinus Torvalds * @start: device to start iterating from. 2731da177e4SLinus Torvalds * @data: data for the callback. 2741da177e4SLinus Torvalds * @fn: function to be called for each device. 2751da177e4SLinus Torvalds * 2761da177e4SLinus Torvalds * Iterate over @bus's list of devices, and call @fn for each, 2771da177e4SLinus Torvalds * passing it @data. If @start is not NULL, we use that device to 2781da177e4SLinus Torvalds * begin iterating from. 2791da177e4SLinus Torvalds * 2801da177e4SLinus Torvalds * We check the return of @fn each time. If it returns anything 2811da177e4SLinus Torvalds * other than 0, we break out and return that value. 2821da177e4SLinus Torvalds * 2831da177e4SLinus Torvalds * NOTE: The device that returns a non-zero value is not retained 2841da177e4SLinus Torvalds * in any way, nor is its refcount incremented. If the caller needs 2850fa1b0a1SAlex Chiang * to retain this data, it should do so, and increment the reference 2861da177e4SLinus Torvalds * count in the supplied callback. 2871da177e4SLinus Torvalds */ 2881da177e4SLinus Torvalds int bus_for_each_dev(struct bus_type *bus, struct device *start, 2891da177e4SLinus Torvalds void *data, int (*fn)(struct device *, void *)) 2901da177e4SLinus Torvalds { 291465c7a3aSmochel@digitalimplant.org struct klist_iter i; 292465c7a3aSmochel@digitalimplant.org struct device *dev; 293465c7a3aSmochel@digitalimplant.org int error = 0; 2941da177e4SLinus Torvalds 2954fa3e78bSBjorn Helgaas if (!bus || !bus->p) 296465c7a3aSmochel@digitalimplant.org return -EINVAL; 297465c7a3aSmochel@digitalimplant.org 2987cd9c9bbSGreg Kroah-Hartman klist_iter_init_node(&bus->p->klist_devices, &i, 299ae1b4171SGreg Kroah-Hartman (start ? &start->p->knode_bus : NULL)); 30093ead7c9SGimcuan Hui while (!error && (dev = next_device(&i))) 301465c7a3aSmochel@digitalimplant.org error = fn(dev, data); 302465c7a3aSmochel@digitalimplant.org klist_iter_exit(&i); 303465c7a3aSmochel@digitalimplant.org return error; 3041da177e4SLinus Torvalds } 3054a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_for_each_dev); 3061da177e4SLinus Torvalds 3070edb5860SCornelia Huck /** 3080edb5860SCornelia Huck * bus_find_device - device iterator for locating a particular device. 3090edb5860SCornelia Huck * @bus: bus type 3100edb5860SCornelia Huck * @start: Device to begin with 3110edb5860SCornelia Huck * @data: Data to pass to match function 3120edb5860SCornelia Huck * @match: Callback function to check device 3130edb5860SCornelia Huck * 3140edb5860SCornelia Huck * This is similar to the bus_for_each_dev() function above, but it 3150edb5860SCornelia Huck * returns a reference to a device that is 'found' for later use, as 3160edb5860SCornelia Huck * determined by the @match callback. 3170edb5860SCornelia Huck * 3180edb5860SCornelia Huck * The callback should return 0 if the device doesn't match and non-zero 3190edb5860SCornelia Huck * if it does. If the callback returns non-zero, this function will 3200edb5860SCornelia Huck * return to the caller and not iterate over any more devices. 3210edb5860SCornelia Huck */ 3220edb5860SCornelia Huck struct device *bus_find_device(struct bus_type *bus, 323418e3ea1SSuzuki K Poulose struct device *start, const void *data, 324418e3ea1SSuzuki K Poulose int (*match)(struct device *dev, const void *data)) 3250edb5860SCornelia Huck { 3260edb5860SCornelia Huck struct klist_iter i; 3270edb5860SCornelia Huck struct device *dev; 3280edb5860SCornelia Huck 3294fa3e78bSBjorn Helgaas if (!bus || !bus->p) 3300edb5860SCornelia Huck return NULL; 3310edb5860SCornelia Huck 3327cd9c9bbSGreg Kroah-Hartman klist_iter_init_node(&bus->p->klist_devices, &i, 3337cd9c9bbSGreg Kroah-Hartman (start ? &start->p->knode_bus : NULL)); 3340edb5860SCornelia Huck while ((dev = next_device(&i))) 3350edb5860SCornelia Huck if (match(dev, data) && get_device(dev)) 3360edb5860SCornelia Huck break; 3370edb5860SCornelia Huck klist_iter_exit(&i); 3380edb5860SCornelia Huck return dev; 3390edb5860SCornelia Huck } 3404a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_find_device); 34138fdac3cSmochel@digitalimplant.org 34238fdac3cSmochel@digitalimplant.org static struct device_driver *next_driver(struct klist_iter *i) 34338fdac3cSmochel@digitalimplant.org { 34438fdac3cSmochel@digitalimplant.org struct klist_node *n = klist_next(i); 345e5dd1278SGreg Kroah-Hartman struct driver_private *drv_priv; 346e5dd1278SGreg Kroah-Hartman 347e5dd1278SGreg Kroah-Hartman if (n) { 348e5dd1278SGreg Kroah-Hartman drv_priv = container_of(n, struct driver_private, knode_bus); 349e5dd1278SGreg Kroah-Hartman return drv_priv->driver; 350e5dd1278SGreg Kroah-Hartman } 351e5dd1278SGreg Kroah-Hartman return NULL; 35238fdac3cSmochel@digitalimplant.org } 35338fdac3cSmochel@digitalimplant.org 3541da177e4SLinus Torvalds /** 3551da177e4SLinus Torvalds * bus_for_each_drv - driver iterator 3561da177e4SLinus Torvalds * @bus: bus we're dealing with. 3571da177e4SLinus Torvalds * @start: driver to start iterating on. 3581da177e4SLinus Torvalds * @data: data to pass to the callback. 3591da177e4SLinus Torvalds * @fn: function to call for each driver. 3601da177e4SLinus Torvalds * 3611da177e4SLinus Torvalds * This is nearly identical to the device iterator above. 3621da177e4SLinus Torvalds * We iterate over each driver that belongs to @bus, and call 3631da177e4SLinus Torvalds * @fn for each. If @fn returns anything but 0, we break out 3641da177e4SLinus Torvalds * and return it. If @start is not NULL, we use it as the head 3651da177e4SLinus Torvalds * of the list. 3661da177e4SLinus Torvalds * 3671da177e4SLinus Torvalds * NOTE: we don't return the driver that returns a non-zero 3681da177e4SLinus Torvalds * value, nor do we leave the reference count incremented for that 3691da177e4SLinus Torvalds * driver. If the caller needs to know that info, it must set it 3701da177e4SLinus Torvalds * in the callback. It must also be sure to increment the refcount 3711da177e4SLinus Torvalds * so it doesn't disappear before returning to the caller. 3721da177e4SLinus Torvalds */ 3731da177e4SLinus Torvalds int bus_for_each_drv(struct bus_type *bus, struct device_driver *start, 3741da177e4SLinus Torvalds void *data, int (*fn)(struct device_driver *, void *)) 3751da177e4SLinus Torvalds { 37638fdac3cSmochel@digitalimplant.org struct klist_iter i; 37738fdac3cSmochel@digitalimplant.org struct device_driver *drv; 37838fdac3cSmochel@digitalimplant.org int error = 0; 3791da177e4SLinus Torvalds 38038fdac3cSmochel@digitalimplant.org if (!bus) 38138fdac3cSmochel@digitalimplant.org return -EINVAL; 38238fdac3cSmochel@digitalimplant.org 3837cd9c9bbSGreg Kroah-Hartman klist_iter_init_node(&bus->p->klist_drivers, &i, 384e5dd1278SGreg Kroah-Hartman start ? &start->p->knode_bus : NULL); 38538fdac3cSmochel@digitalimplant.org while ((drv = next_driver(&i)) && !error) 38638fdac3cSmochel@digitalimplant.org error = fn(drv, data); 38738fdac3cSmochel@digitalimplant.org klist_iter_exit(&i); 38838fdac3cSmochel@digitalimplant.org return error; 3891da177e4SLinus Torvalds } 3904a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_for_each_drv); 3911da177e4SLinus Torvalds 3921da177e4SLinus Torvalds /** 3931da177e4SLinus Torvalds * bus_add_device - add device to bus 3941da177e4SLinus Torvalds * @dev: device being added 3951da177e4SLinus Torvalds * 3962023c610SAlan Stern * - Add device's bus attributes. 3972023c610SAlan Stern * - Create links to device's bus. 3981da177e4SLinus Torvalds * - Add the device to its bus's list of devices. 3991da177e4SLinus Torvalds */ 4001da177e4SLinus Torvalds int bus_add_device(struct device *dev) 4011da177e4SLinus Torvalds { 4025901d014SGreg Kroah-Hartman struct bus_type *bus = bus_get(dev->bus); 4031da177e4SLinus Torvalds int error = 0; 4041da177e4SLinus Torvalds 4051da177e4SLinus Torvalds if (bus) { 4061e0b2cf9SKay Sievers pr_debug("bus: '%s': add device %s\n", bus->name, dev_name(dev)); 407fa6fdb33SGreg Kroah-Hartman error = device_add_groups(dev, bus->dev_groups); 408fa6fdb33SGreg Kroah-Hartman if (error) 409b46c7337SGreg Kroah-Hartman goto out_put; 410c6f7e72aSGreg Kroah-Hartman error = sysfs_create_link(&bus->p->devices_kset->kobj, 4111e0b2cf9SKay Sievers &dev->kobj, dev_name(dev)); 412f86db396SAndrew Morton if (error) 4131c34203aSJunjie Mao goto out_groups; 414f86db396SAndrew Morton error = sysfs_create_link(&dev->kobj, 415c6f7e72aSGreg Kroah-Hartman &dev->bus->p->subsys.kobj, "subsystem"); 416f86db396SAndrew Morton if (error) 417513e7337SCornelia Huck goto out_subsys; 4182023c610SAlan Stern klist_add_tail(&dev->p->knode_bus, &bus->p->klist_devices); 4191da177e4SLinus Torvalds } 420513e7337SCornelia Huck return 0; 421513e7337SCornelia Huck 422513e7337SCornelia Huck out_subsys: 4231e0b2cf9SKay Sievers sysfs_remove_link(&bus->p->devices_kset->kobj, dev_name(dev)); 424fa6fdb33SGreg Kroah-Hartman out_groups: 425fa6fdb33SGreg Kroah-Hartman device_remove_groups(dev, bus->dev_groups); 426513e7337SCornelia Huck out_put: 427fc1ede58SGreg Kroah-Hartman bus_put(dev->bus); 4281da177e4SLinus Torvalds return error; 4291da177e4SLinus Torvalds } 4301da177e4SLinus Torvalds 4311da177e4SLinus Torvalds /** 4322023c610SAlan Stern * bus_probe_device - probe drivers for a new device 4332023c610SAlan Stern * @dev: device to probe 43453877d06SKay Sievers * 4352023c610SAlan Stern * - Automatically probe for a driver if the bus allows it. 43653877d06SKay Sievers */ 4372023c610SAlan Stern void bus_probe_device(struct device *dev) 43853877d06SKay Sievers { 43953877d06SKay Sievers struct bus_type *bus = dev->bus; 440ca22e56dSKay Sievers struct subsys_interface *sif; 44153877d06SKay Sievers 442ca22e56dSKay Sievers if (!bus) 443ca22e56dSKay Sievers return; 444ca22e56dSKay Sievers 445765230b5SDmitry Torokhov if (bus->p->drivers_autoprobe) 446765230b5SDmitry Torokhov device_initial_probe(dev); 447ca22e56dSKay Sievers 448ca22e56dSKay Sievers mutex_lock(&bus->p->mutex); 449ca22e56dSKay Sievers list_for_each_entry(sif, &bus->p->interfaces, node) 450ca22e56dSKay Sievers if (sif->add_dev) 451ca22e56dSKay Sievers sif->add_dev(dev, sif); 452ca22e56dSKay Sievers mutex_unlock(&bus->p->mutex); 453f86db396SAndrew Morton } 45453877d06SKay Sievers 45553877d06SKay Sievers /** 4561da177e4SLinus Torvalds * bus_remove_device - remove device from bus 4571da177e4SLinus Torvalds * @dev: device to be removed 4581da177e4SLinus Torvalds * 459ca22e56dSKay Sievers * - Remove device from all interfaces. 460ca22e56dSKay Sievers * - Remove symlink from bus' directory. 4611da177e4SLinus Torvalds * - Delete device from bus's list. 4621da177e4SLinus Torvalds * - Detach from its driver. 4631da177e4SLinus Torvalds * - Drop reference taken in bus_add_device(). 4641da177e4SLinus Torvalds */ 4651da177e4SLinus Torvalds void bus_remove_device(struct device *dev) 4661da177e4SLinus Torvalds { 467ca22e56dSKay Sievers struct bus_type *bus = dev->bus; 468ca22e56dSKay Sievers struct subsys_interface *sif; 469ca22e56dSKay Sievers 470ca22e56dSKay Sievers if (!bus) 471ca22e56dSKay Sievers return; 472ca22e56dSKay Sievers 473ca22e56dSKay Sievers mutex_lock(&bus->p->mutex); 474ca22e56dSKay Sievers list_for_each_entry(sif, &bus->p->interfaces, node) 475ca22e56dSKay Sievers if (sif->remove_dev) 476ca22e56dSKay Sievers sif->remove_dev(dev, sif); 477ca22e56dSKay Sievers mutex_unlock(&bus->p->mutex); 478ca22e56dSKay Sievers 479b9d9c82bSKay Sievers sysfs_remove_link(&dev->kobj, "subsystem"); 4804a3ad20cSGreg Kroah-Hartman sysfs_remove_link(&dev->bus->p->devices_kset->kobj, 4811e0b2cf9SKay Sievers dev_name(dev)); 482fa6fdb33SGreg Kroah-Hartman device_remove_groups(dev, dev->bus->dev_groups); 483ae1b4171SGreg Kroah-Hartman if (klist_node_attached(&dev->p->knode_bus)) 484ae1b4171SGreg Kroah-Hartman klist_del(&dev->p->knode_bus); 4853f62e570SGreg Kroah-Hartman 4864a3ad20cSGreg Kroah-Hartman pr_debug("bus: '%s': remove device %s\n", 4871e0b2cf9SKay Sievers dev->bus->name, dev_name(dev)); 4881da177e4SLinus Torvalds device_release_driver(dev); 489fc1ede58SGreg Kroah-Hartman bus_put(dev->bus); 4901da177e4SLinus Torvalds } 4911da177e4SLinus Torvalds 492f86db396SAndrew Morton static int __must_check add_bind_files(struct device_driver *drv) 493874c6241SGreg Kroah-Hartman { 494f86db396SAndrew Morton int ret; 495f86db396SAndrew Morton 496f86db396SAndrew Morton ret = driver_create_file(drv, &driver_attr_unbind); 497f86db396SAndrew Morton if (ret == 0) { 498f86db396SAndrew Morton ret = driver_create_file(drv, &driver_attr_bind); 499f86db396SAndrew Morton if (ret) 500f86db396SAndrew Morton driver_remove_file(drv, &driver_attr_unbind); 501f86db396SAndrew Morton } 502f86db396SAndrew Morton return ret; 503874c6241SGreg Kroah-Hartman } 504874c6241SGreg Kroah-Hartman 505874c6241SGreg Kroah-Hartman static void remove_bind_files(struct device_driver *drv) 506874c6241SGreg Kroah-Hartman { 507874c6241SGreg Kroah-Hartman driver_remove_file(drv, &driver_attr_bind); 508874c6241SGreg Kroah-Hartman driver_remove_file(drv, &driver_attr_unbind); 509874c6241SGreg Kroah-Hartman } 510b8c5cec2SKay Sievers 5112e7189b6SGreg Kroah-Hartman static BUS_ATTR_WO(drivers_probe); 5122e7189b6SGreg Kroah-Hartman static BUS_ATTR_RW(drivers_autoprobe); 5138380770cSKay Sievers 514b8c5cec2SKay Sievers static int add_probe_files(struct bus_type *bus) 515b8c5cec2SKay Sievers { 516b8c5cec2SKay Sievers int retval; 517b8c5cec2SKay Sievers 5188380770cSKay Sievers retval = bus_create_file(bus, &bus_attr_drivers_probe); 519b8c5cec2SKay Sievers if (retval) 520b8c5cec2SKay Sievers goto out; 521b8c5cec2SKay Sievers 5228380770cSKay Sievers retval = bus_create_file(bus, &bus_attr_drivers_autoprobe); 523b8c5cec2SKay Sievers if (retval) 5248380770cSKay Sievers bus_remove_file(bus, &bus_attr_drivers_probe); 525b8c5cec2SKay Sievers out: 526b8c5cec2SKay Sievers return retval; 527b8c5cec2SKay Sievers } 528b8c5cec2SKay Sievers 529b8c5cec2SKay Sievers static void remove_probe_files(struct bus_type *bus) 530b8c5cec2SKay Sievers { 5318380770cSKay Sievers bus_remove_file(bus, &bus_attr_drivers_autoprobe); 5328380770cSKay Sievers bus_remove_file(bus, &bus_attr_drivers_probe); 533b8c5cec2SKay Sievers } 5341da177e4SLinus Torvalds 5352581c9ccSGreg Kroah-Hartman static ssize_t uevent_store(struct device_driver *drv, const char *buf, 5362581c9ccSGreg Kroah-Hartman size_t count) 5377ac1cf4aSKay Sievers { 538df44b479SPeter Rajnoha int rc; 539df44b479SPeter Rajnoha 540df44b479SPeter Rajnoha rc = kobject_synth_uevent(&drv->p->kobj, buf, count); 541df44b479SPeter Rajnoha return rc ? rc : count; 5427ac1cf4aSKay Sievers } 5432581c9ccSGreg Kroah-Hartman static DRIVER_ATTR_WO(uevent); 5447ac1cf4aSKay Sievers 5451da177e4SLinus Torvalds /** 5461da177e4SLinus Torvalds * bus_add_driver - Add a driver to the bus. 5471da177e4SLinus Torvalds * @drv: driver. 5481da177e4SLinus Torvalds */ 5491da177e4SLinus Torvalds int bus_add_driver(struct device_driver *drv) 5501da177e4SLinus Torvalds { 551e5dd1278SGreg Kroah-Hartman struct bus_type *bus; 552e5dd1278SGreg Kroah-Hartman struct driver_private *priv; 5531da177e4SLinus Torvalds int error = 0; 5541da177e4SLinus Torvalds 555e5dd1278SGreg Kroah-Hartman bus = bus_get(drv->bus); 556d9fd4d3bSJeff Garzik if (!bus) 5574f6e1945SGreg Kroah-Hartman return -EINVAL; 558d9fd4d3bSJeff Garzik 5597dc72b28SGreg Kroah-Hartman pr_debug("bus: '%s': add driver %s\n", bus->name, drv->name); 560e5dd1278SGreg Kroah-Hartman 561e5dd1278SGreg Kroah-Hartman priv = kzalloc(sizeof(*priv), GFP_KERNEL); 56207634464SCornelia Huck if (!priv) { 56307634464SCornelia Huck error = -ENOMEM; 56407634464SCornelia Huck goto out_put_bus; 56507634464SCornelia Huck } 566e5dd1278SGreg Kroah-Hartman klist_init(&priv->klist_devices, NULL, NULL); 567e5dd1278SGreg Kroah-Hartman priv->driver = drv; 568e5dd1278SGreg Kroah-Hartman drv->p = priv; 569c8e90d82SGreg Kroah-Hartman priv->kobj.kset = bus->p->drivers_kset; 570c8e90d82SGreg Kroah-Hartman error = kobject_init_and_add(&priv->kobj, &driver_ktype, NULL, 571c8e90d82SGreg Kroah-Hartman "%s", drv->name); 572dc0afa83SCornelia Huck if (error) 57307634464SCornelia Huck goto out_unregister; 5741da177e4SLinus Torvalds 575190888acSMing Lei klist_add_tail(&priv->knode_bus, &bus->p->klist_drivers); 576c6f7e72aSGreg Kroah-Hartman if (drv->bus->p->drivers_autoprobe) { 577f86db396SAndrew Morton error = driver_attach(drv); 578f86db396SAndrew Morton if (error) 579310862e5SSchspa Shi goto out_del_list; 580b8c5cec2SKay Sievers } 5811da177e4SLinus Torvalds module_add_driver(drv->owner, drv); 5821da177e4SLinus Torvalds 5837ac1cf4aSKay Sievers error = driver_create_file(drv, &driver_attr_uevent); 5847ac1cf4aSKay Sievers if (error) { 5857ac1cf4aSKay Sievers printk(KERN_ERR "%s: uevent attr (%s) failed\n", 5862b3a302aSHarvey Harrison __func__, drv->name); 5877ac1cf4aSKay Sievers } 588e18945b1SGreg Kroah-Hartman error = driver_add_groups(drv, bus->drv_groups); 589f86db396SAndrew Morton if (error) { 590f86db396SAndrew Morton /* How the hell do we get out of this pickle? Give up */ 5914044b2fcSJoe Pater printk(KERN_ERR "%s: driver_add_groups(%s) failed\n", 592ed0617b5SGreg Kroah-Hartman __func__, drv->name); 593e18945b1SGreg Kroah-Hartman } 5941a6f2a75SDmitry Torokhov 5951a6f2a75SDmitry Torokhov if (!drv->suppress_bind_attrs) { 596f86db396SAndrew Morton error = add_bind_files(drv); 597f86db396SAndrew Morton if (error) { 598f86db396SAndrew Morton /* Ditto */ 599f86db396SAndrew Morton printk(KERN_ERR "%s: add_bind_files(%s) failed\n", 6002b3a302aSHarvey Harrison __func__, drv->name); 601f86db396SAndrew Morton } 6021a6f2a75SDmitry Torokhov } 603d9fd4d3bSJeff Garzik 6045c8563d7SKay Sievers return 0; 6051a6f2a75SDmitry Torokhov 606310862e5SSchspa Shi out_del_list: 607310862e5SSchspa Shi klist_del(&priv->knode_bus); 608f86db396SAndrew Morton out_unregister: 60999b28f1bSPhil Carmody kobject_put(&priv->kobj); 6100f9b011dSChristophe JAILLET /* drv->p is freed in driver_release() */ 6115c8563d7SKay Sievers drv->p = NULL; 612f86db396SAndrew Morton out_put_bus: 613fc1ede58SGreg Kroah-Hartman bus_put(bus); 614f86db396SAndrew Morton return error; 6151da177e4SLinus Torvalds } 6161da177e4SLinus Torvalds 6171da177e4SLinus Torvalds /** 6181da177e4SLinus Torvalds * bus_remove_driver - delete driver from bus's knowledge. 6191da177e4SLinus Torvalds * @drv: driver. 6201da177e4SLinus Torvalds * 6211da177e4SLinus Torvalds * Detach the driver from the devices it controls, and remove 6221da177e4SLinus Torvalds * it from its bus's list of drivers. Finally, we drop the reference 6231da177e4SLinus Torvalds * to the bus we took in bus_add_driver(). 6241da177e4SLinus Torvalds */ 6251da177e4SLinus Torvalds void bus_remove_driver(struct device_driver *drv) 6261da177e4SLinus Torvalds { 627d9fd4d3bSJeff Garzik if (!drv->bus) 628d9fd4d3bSJeff Garzik return; 629d9fd4d3bSJeff Garzik 6301a6f2a75SDmitry Torokhov if (!drv->suppress_bind_attrs) 631874c6241SGreg Kroah-Hartman remove_bind_files(drv); 632ed0617b5SGreg Kroah-Hartman driver_remove_groups(drv, drv->bus->drv_groups); 6337ac1cf4aSKay Sievers driver_remove_file(drv, &driver_attr_uevent); 634e5dd1278SGreg Kroah-Hartman klist_remove(&drv->p->knode_bus); 6357dc72b28SGreg Kroah-Hartman pr_debug("bus: '%s': remove driver %s\n", drv->bus->name, drv->name); 6361da177e4SLinus Torvalds driver_detach(drv); 6371da177e4SLinus Torvalds module_remove_driver(drv); 638c10997f6SGreg Kroah-Hartman kobject_put(&drv->p->kobj); 639fc1ede58SGreg Kroah-Hartman bus_put(drv->bus); 6401da177e4SLinus Torvalds } 6411da177e4SLinus Torvalds 6421da177e4SLinus Torvalds /* Helper for bus_rescan_devices's iter */ 643f86db396SAndrew Morton static int __must_check bus_rescan_devices_helper(struct device *dev, 644f86db396SAndrew Morton void *data) 6451da177e4SLinus Torvalds { 646f86db396SAndrew Morton int ret = 0; 647f86db396SAndrew Morton 648bf74ad5bSAlan Stern if (!dev->driver) { 6498c97a46aSMartin Liu if (dev->parent && dev->bus->need_parent_lock) 6508e9394ceSGreg Kroah-Hartman device_lock(dev->parent); 651f86db396SAndrew Morton ret = device_attach(dev); 6528c97a46aSMartin Liu if (dev->parent && dev->bus->need_parent_lock) 6538e9394ceSGreg Kroah-Hartman device_unlock(dev->parent); 654bf74ad5bSAlan Stern } 655f86db396SAndrew Morton return ret < 0 ? ret : 0; 6561da177e4SLinus Torvalds } 6571da177e4SLinus Torvalds 6581da177e4SLinus Torvalds /** 6591da177e4SLinus Torvalds * bus_rescan_devices - rescan devices on the bus for possible drivers 6601da177e4SLinus Torvalds * @bus: the bus to scan. 6611da177e4SLinus Torvalds * 6621da177e4SLinus Torvalds * This function will look for devices on the bus with no driver 66323d3d602SGreg Kroah-Hartman * attached and rescan it against existing drivers to see if it matches 66423d3d602SGreg Kroah-Hartman * any by calling device_attach() for the unbound devices. 6651da177e4SLinus Torvalds */ 666f86db396SAndrew Morton int bus_rescan_devices(struct bus_type *bus) 6671da177e4SLinus Torvalds { 668f86db396SAndrew Morton return bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper); 6691da177e4SLinus Torvalds } 6704a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_rescan_devices); 6711da177e4SLinus Torvalds 672e935d5daSMoore, Eric /** 673e935d5daSMoore, Eric * device_reprobe - remove driver for a device and probe for a new driver 674e935d5daSMoore, Eric * @dev: the device to reprobe 675e935d5daSMoore, Eric * 676e935d5daSMoore, Eric * This function detaches the attached driver (if any) for the given 677e935d5daSMoore, Eric * device and restarts the driver probing process. It is intended 678e935d5daSMoore, Eric * to use if probing criteria changed during a devices lifetime and 679e935d5daSMoore, Eric * driver attachment should change accordingly. 680e935d5daSMoore, Eric */ 681f86db396SAndrew Morton int device_reprobe(struct device *dev) 682e935d5daSMoore, Eric { 683ed88747cSAlexander Duyck if (dev->driver) 684ed88747cSAlexander Duyck device_driver_detach(dev); 685f86db396SAndrew Morton return bus_rescan_devices_helper(dev, NULL); 686e935d5daSMoore, Eric } 687e935d5daSMoore, Eric EXPORT_SYMBOL_GPL(device_reprobe); 6881da177e4SLinus Torvalds 68912478ba0SGreg Kroah-Hartman static int bus_add_groups(struct bus_type *bus, 69012478ba0SGreg Kroah-Hartman const struct attribute_group **groups) 69112478ba0SGreg Kroah-Hartman { 6923e9b2baeSGreg Kroah-Hartman return sysfs_create_groups(&bus->p->subsys.kobj, groups); 69312478ba0SGreg Kroah-Hartman } 69412478ba0SGreg Kroah-Hartman 69512478ba0SGreg Kroah-Hartman static void bus_remove_groups(struct bus_type *bus, 69612478ba0SGreg Kroah-Hartman const struct attribute_group **groups) 69712478ba0SGreg Kroah-Hartman { 6983e9b2baeSGreg Kroah-Hartman sysfs_remove_groups(&bus->p->subsys.kobj, groups); 69912478ba0SGreg Kroah-Hartman } 70012478ba0SGreg Kroah-Hartman 70134bb61f9SJames Bottomley static void klist_devices_get(struct klist_node *n) 70234bb61f9SJames Bottomley { 703ae1b4171SGreg Kroah-Hartman struct device_private *dev_prv = to_device_private_bus(n); 704ae1b4171SGreg Kroah-Hartman struct device *dev = dev_prv->device; 70534bb61f9SJames Bottomley 70634bb61f9SJames Bottomley get_device(dev); 70734bb61f9SJames Bottomley } 70834bb61f9SJames Bottomley 70934bb61f9SJames Bottomley static void klist_devices_put(struct klist_node *n) 71034bb61f9SJames Bottomley { 711ae1b4171SGreg Kroah-Hartman struct device_private *dev_prv = to_device_private_bus(n); 712ae1b4171SGreg Kroah-Hartman struct device *dev = dev_prv->device; 71334bb61f9SJames Bottomley 71434bb61f9SJames Bottomley put_device(dev); 71534bb61f9SJames Bottomley } 71634bb61f9SJames Bottomley 7177ac1cf4aSKay Sievers static ssize_t bus_uevent_store(struct bus_type *bus, 7187ac1cf4aSKay Sievers const char *buf, size_t count) 7197ac1cf4aSKay Sievers { 720df44b479SPeter Rajnoha int rc; 721df44b479SPeter Rajnoha 722df44b479SPeter Rajnoha rc = kobject_synth_uevent(&bus->p->subsys.kobj, buf, count); 723df44b479SPeter Rajnoha return rc ? rc : count; 7247ac1cf4aSKay Sievers } 725a4723041SGreg Kroah-Hartman /* 726a4723041SGreg Kroah-Hartman * "open code" the old BUS_ATTR() macro here. We want to use BUS_ATTR_WO() 727a4723041SGreg Kroah-Hartman * here, but can not use it as earlier in the file we have 728a4723041SGreg Kroah-Hartman * DEVICE_ATTR_WO(uevent), which would cause a clash with the with the store 729a4723041SGreg Kroah-Hartman * function name. 730a4723041SGreg Kroah-Hartman */ 73116b0dd40SJinchao Wang static struct bus_attribute bus_attr_uevent = __ATTR(uevent, 0200, NULL, 732a4723041SGreg Kroah-Hartman bus_uevent_store); 7337ac1cf4aSKay Sievers 7341da177e4SLinus Torvalds /** 735be871b7eSMichal Hocko * bus_register - register a driver-core subsystem 73678d79559SRandy Dunlap * @bus: bus to register 7371da177e4SLinus Torvalds * 73878d79559SRandy Dunlap * Once we have that, we register the bus with the kobject 7391da177e4SLinus Torvalds * infrastructure, then register the children subsystems it has: 740ca22e56dSKay Sievers * the devices and drivers that belong to the subsystem. 7411da177e4SLinus Torvalds */ 742be871b7eSMichal Hocko int bus_register(struct bus_type *bus) 7431da177e4SLinus Torvalds { 7441da177e4SLinus Torvalds int retval; 7456b6e39a6SKay Sievers struct subsys_private *priv; 746be871b7eSMichal Hocko struct lock_class_key *key = &bus->lock_key; 7471da177e4SLinus Torvalds 7486b6e39a6SKay Sievers priv = kzalloc(sizeof(struct subsys_private), GFP_KERNEL); 749c6f7e72aSGreg Kroah-Hartman if (!priv) 750c6f7e72aSGreg Kroah-Hartman return -ENOMEM; 751116af378SBenjamin Herrenschmidt 752c6f7e72aSGreg Kroah-Hartman priv->bus = bus; 753c6f7e72aSGreg Kroah-Hartman bus->p = priv; 754c6f7e72aSGreg Kroah-Hartman 755c6f7e72aSGreg Kroah-Hartman BLOCKING_INIT_NOTIFIER_HEAD(&priv->bus_notifier); 756c6f7e72aSGreg Kroah-Hartman 757c6f7e72aSGreg Kroah-Hartman retval = kobject_set_name(&priv->subsys.kobj, "%s", bus->name); 7581da177e4SLinus Torvalds if (retval) 7591da177e4SLinus Torvalds goto out; 7601da177e4SLinus Torvalds 761c6f7e72aSGreg Kroah-Hartman priv->subsys.kobj.kset = bus_kset; 762c6f7e72aSGreg Kroah-Hartman priv->subsys.kobj.ktype = &bus_ktype; 763c6f7e72aSGreg Kroah-Hartman priv->drivers_autoprobe = 1; 764d6b05b84SGreg Kroah-Hartman 765c6f7e72aSGreg Kroah-Hartman retval = kset_register(&priv->subsys); 7661da177e4SLinus Torvalds if (retval) 7671da177e4SLinus Torvalds goto out; 7681da177e4SLinus Torvalds 7697ac1cf4aSKay Sievers retval = bus_create_file(bus, &bus_attr_uevent); 7707ac1cf4aSKay Sievers if (retval) 7717ac1cf4aSKay Sievers goto bus_uevent_fail; 7727ac1cf4aSKay Sievers 773c6f7e72aSGreg Kroah-Hartman priv->devices_kset = kset_create_and_add("devices", NULL, 774c6f7e72aSGreg Kroah-Hartman &priv->subsys.kobj); 775c6f7e72aSGreg Kroah-Hartman if (!priv->devices_kset) { 7763d899596SGreg Kroah-Hartman retval = -ENOMEM; 7771da177e4SLinus Torvalds goto bus_devices_fail; 7783d899596SGreg Kroah-Hartman } 7791da177e4SLinus Torvalds 780c6f7e72aSGreg Kroah-Hartman priv->drivers_kset = kset_create_and_add("drivers", NULL, 781c6f7e72aSGreg Kroah-Hartman &priv->subsys.kobj); 782c6f7e72aSGreg Kroah-Hartman if (!priv->drivers_kset) { 7836dcec251SGreg Kroah-Hartman retval = -ENOMEM; 7841da177e4SLinus Torvalds goto bus_drivers_fail; 7856dcec251SGreg Kroah-Hartman } 786465c7a3aSmochel@digitalimplant.org 787ca22e56dSKay Sievers INIT_LIST_HEAD(&priv->interfaces); 788ca22e56dSKay Sievers __mutex_init(&priv->mutex, "subsys mutex", key); 789c6f7e72aSGreg Kroah-Hartman klist_init(&priv->klist_devices, klist_devices_get, klist_devices_put); 790c6f7e72aSGreg Kroah-Hartman klist_init(&priv->klist_drivers, NULL, NULL); 791b8c5cec2SKay Sievers 792b8c5cec2SKay Sievers retval = add_probe_files(bus); 793b8c5cec2SKay Sievers if (retval) 794b8c5cec2SKay Sievers goto bus_probe_files_fail; 795b8c5cec2SKay Sievers 79612478ba0SGreg Kroah-Hartman retval = bus_add_groups(bus, bus->bus_groups); 79712478ba0SGreg Kroah-Hartman if (retval) 79812478ba0SGreg Kroah-Hartman goto bus_groups_fail; 7991da177e4SLinus Torvalds 8007dc72b28SGreg Kroah-Hartman pr_debug("bus: '%s': registered\n", bus->name); 8011da177e4SLinus Torvalds return 0; 8021da177e4SLinus Torvalds 80312478ba0SGreg Kroah-Hartman bus_groups_fail: 804b8c5cec2SKay Sievers remove_probe_files(bus); 805b8c5cec2SKay Sievers bus_probe_files_fail: 806c6f7e72aSGreg Kroah-Hartman kset_unregister(bus->p->drivers_kset); 8071da177e4SLinus Torvalds bus_drivers_fail: 808c6f7e72aSGreg Kroah-Hartman kset_unregister(bus->p->devices_kset); 8091da177e4SLinus Torvalds bus_devices_fail: 8107ac1cf4aSKay Sievers bus_remove_file(bus, &bus_attr_uevent); 8117ac1cf4aSKay Sievers bus_uevent_fail: 812c6f7e72aSGreg Kroah-Hartman kset_unregister(&bus->p->subsys); 8131da177e4SLinus Torvalds out: 814600c20f3SJike Song kfree(bus->p); 815f48f3febSDave Young bus->p = NULL; 8161da177e4SLinus Torvalds return retval; 8171da177e4SLinus Torvalds } 818be871b7eSMichal Hocko EXPORT_SYMBOL_GPL(bus_register); 8191da177e4SLinus Torvalds 8201da177e4SLinus Torvalds /** 8211da177e4SLinus Torvalds * bus_unregister - remove a bus from the system 8221da177e4SLinus Torvalds * @bus: bus. 8231da177e4SLinus Torvalds * 8241da177e4SLinus Torvalds * Unregister the child subsystems and the bus itself. 825fc1ede58SGreg Kroah-Hartman * Finally, we call bus_put() to release the refcount 8261da177e4SLinus Torvalds */ 8271da177e4SLinus Torvalds void bus_unregister(struct bus_type *bus) 8281da177e4SLinus Torvalds { 8297dc72b28SGreg Kroah-Hartman pr_debug("bus: '%s': unregistering\n", bus->name); 830ca22e56dSKay Sievers if (bus->dev_root) 831ca22e56dSKay Sievers device_unregister(bus->dev_root); 83212478ba0SGreg Kroah-Hartman bus_remove_groups(bus, bus->bus_groups); 833b8c5cec2SKay Sievers remove_probe_files(bus); 834c6f7e72aSGreg Kroah-Hartman kset_unregister(bus->p->drivers_kset); 835c6f7e72aSGreg Kroah-Hartman kset_unregister(bus->p->devices_kset); 8367ac1cf4aSKay Sievers bus_remove_file(bus, &bus_attr_uevent); 837c6f7e72aSGreg Kroah-Hartman kset_unregister(&bus->p->subsys); 8381da177e4SLinus Torvalds } 8394a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_unregister); 8401da177e4SLinus Torvalds 841116af378SBenjamin Herrenschmidt int bus_register_notifier(struct bus_type *bus, struct notifier_block *nb) 842116af378SBenjamin Herrenschmidt { 843c6f7e72aSGreg Kroah-Hartman return blocking_notifier_chain_register(&bus->p->bus_notifier, nb); 844116af378SBenjamin Herrenschmidt } 845116af378SBenjamin Herrenschmidt EXPORT_SYMBOL_GPL(bus_register_notifier); 846116af378SBenjamin Herrenschmidt 847116af378SBenjamin Herrenschmidt int bus_unregister_notifier(struct bus_type *bus, struct notifier_block *nb) 848116af378SBenjamin Herrenschmidt { 849c6f7e72aSGreg Kroah-Hartman return blocking_notifier_chain_unregister(&bus->p->bus_notifier, nb); 850116af378SBenjamin Herrenschmidt } 851116af378SBenjamin Herrenschmidt EXPORT_SYMBOL_GPL(bus_unregister_notifier); 852116af378SBenjamin Herrenschmidt 853*ed9f9181SGreg Kroah-Hartman void bus_notify(struct device *dev, enum bus_notifier_event value) 854*ed9f9181SGreg Kroah-Hartman { 855*ed9f9181SGreg Kroah-Hartman struct bus_type *bus = dev->bus; 856*ed9f9181SGreg Kroah-Hartman 857*ed9f9181SGreg Kroah-Hartman if (bus) 858*ed9f9181SGreg Kroah-Hartman blocking_notifier_call_chain(&bus->p->bus_notifier, value, dev); 859*ed9f9181SGreg Kroah-Hartman } 860*ed9f9181SGreg Kroah-Hartman 8610fed80f7SGreg Kroah-Hartman struct kset *bus_get_kset(struct bus_type *bus) 8620fed80f7SGreg Kroah-Hartman { 863c6f7e72aSGreg Kroah-Hartman return &bus->p->subsys; 8640fed80f7SGreg Kroah-Hartman } 8650fed80f7SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_get_kset); 8660fed80f7SGreg Kroah-Hartman 8678afbb427SGreg Kroah-Hartman static struct klist *bus_get_device_klist(struct bus_type *bus) 868b249072eSGreg Kroah-Hartman { 869c6f7e72aSGreg Kroah-Hartman return &bus->p->klist_devices; 870b249072eSGreg Kroah-Hartman } 871b249072eSGreg Kroah-Hartman 87299178b03SGreg Kroah-Hartman /* 873dca25ebdSRobert P. J. Day * Yes, this forcibly breaks the klist abstraction temporarily. It 87499178b03SGreg Kroah-Hartman * just wants to sort the klist, not change reference counts and 87599178b03SGreg Kroah-Hartman * take/drop locks rapidly in the process. It does all this while 87699178b03SGreg Kroah-Hartman * holding the lock for the list, so objects can't otherwise be 87799178b03SGreg Kroah-Hartman * added/removed while we're swizzling. 87899178b03SGreg Kroah-Hartman */ 87999178b03SGreg Kroah-Hartman static void device_insertion_sort_klist(struct device *a, struct list_head *list, 88099178b03SGreg Kroah-Hartman int (*compare)(const struct device *a, 88199178b03SGreg Kroah-Hartman const struct device *b)) 88299178b03SGreg Kroah-Hartman { 88399178b03SGreg Kroah-Hartman struct klist_node *n; 884ae1b4171SGreg Kroah-Hartman struct device_private *dev_prv; 88599178b03SGreg Kroah-Hartman struct device *b; 88699178b03SGreg Kroah-Hartman 8874c62785eSGeliang Tang list_for_each_entry(n, list, n_node) { 888ae1b4171SGreg Kroah-Hartman dev_prv = to_device_private_bus(n); 889ae1b4171SGreg Kroah-Hartman b = dev_prv->device; 89099178b03SGreg Kroah-Hartman if (compare(a, b) <= 0) { 891ae1b4171SGreg Kroah-Hartman list_move_tail(&a->p->knode_bus.n_node, 892ae1b4171SGreg Kroah-Hartman &b->p->knode_bus.n_node); 89399178b03SGreg Kroah-Hartman return; 89499178b03SGreg Kroah-Hartman } 89599178b03SGreg Kroah-Hartman } 896ae1b4171SGreg Kroah-Hartman list_move_tail(&a->p->knode_bus.n_node, list); 89799178b03SGreg Kroah-Hartman } 89899178b03SGreg Kroah-Hartman 89999178b03SGreg Kroah-Hartman void bus_sort_breadthfirst(struct bus_type *bus, 90099178b03SGreg Kroah-Hartman int (*compare)(const struct device *a, 90199178b03SGreg Kroah-Hartman const struct device *b)) 90299178b03SGreg Kroah-Hartman { 90399178b03SGreg Kroah-Hartman LIST_HEAD(sorted_devices); 9044c62785eSGeliang Tang struct klist_node *n, *tmp; 905ae1b4171SGreg Kroah-Hartman struct device_private *dev_prv; 90699178b03SGreg Kroah-Hartman struct device *dev; 90799178b03SGreg Kroah-Hartman struct klist *device_klist; 90899178b03SGreg Kroah-Hartman 90999178b03SGreg Kroah-Hartman device_klist = bus_get_device_klist(bus); 91099178b03SGreg Kroah-Hartman 91199178b03SGreg Kroah-Hartman spin_lock(&device_klist->k_lock); 9124c62785eSGeliang Tang list_for_each_entry_safe(n, tmp, &device_klist->k_list, n_node) { 913ae1b4171SGreg Kroah-Hartman dev_prv = to_device_private_bus(n); 914ae1b4171SGreg Kroah-Hartman dev = dev_prv->device; 91599178b03SGreg Kroah-Hartman device_insertion_sort_klist(dev, &sorted_devices, compare); 91699178b03SGreg Kroah-Hartman } 91799178b03SGreg Kroah-Hartman list_splice(&sorted_devices, &device_klist->k_list); 91899178b03SGreg Kroah-Hartman spin_unlock(&device_klist->k_lock); 91999178b03SGreg Kroah-Hartman } 92099178b03SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_sort_breadthfirst); 92199178b03SGreg Kroah-Hartman 922b0a8a59aSGreg Kroah-Hartman struct subsys_dev_iter { 923b0a8a59aSGreg Kroah-Hartman struct klist_iter ki; 924b0a8a59aSGreg Kroah-Hartman const struct device_type *type; 925b0a8a59aSGreg Kroah-Hartman }; 926b0a8a59aSGreg Kroah-Hartman 927ca22e56dSKay Sievers /** 928ca22e56dSKay Sievers * subsys_dev_iter_init - initialize subsys device iterator 929ca22e56dSKay Sievers * @iter: subsys iterator to initialize 930ca22e56dSKay Sievers * @subsys: the subsys we wanna iterate over 931ca22e56dSKay Sievers * @start: the device to start iterating from, if any 932ca22e56dSKay Sievers * @type: device_type of the devices to iterate over, NULL for all 933ca22e56dSKay Sievers * 934ca22e56dSKay Sievers * Initialize subsys iterator @iter such that it iterates over devices 935ca22e56dSKay Sievers * of @subsys. If @start is set, the list iteration will start there, 936ca22e56dSKay Sievers * otherwise if it is NULL, the iteration starts at the beginning of 937ca22e56dSKay Sievers * the list. 938ca22e56dSKay Sievers */ 9392e45fc55SGreg Kroah-Hartman static void subsys_dev_iter_init(struct subsys_dev_iter *iter, struct bus_type *subsys, 940ca22e56dSKay Sievers struct device *start, const struct device_type *type) 941ca22e56dSKay Sievers { 942ca22e56dSKay Sievers struct klist_node *start_knode = NULL; 943ca22e56dSKay Sievers 944ca22e56dSKay Sievers if (start) 945ca22e56dSKay Sievers start_knode = &start->p->knode_bus; 9467cd9c9bbSGreg Kroah-Hartman klist_iter_init_node(&subsys->p->klist_devices, &iter->ki, start_knode); 947ca22e56dSKay Sievers iter->type = type; 948ca22e56dSKay Sievers } 949ca22e56dSKay Sievers 950ca22e56dSKay Sievers /** 951ca22e56dSKay Sievers * subsys_dev_iter_next - iterate to the next device 952ca22e56dSKay Sievers * @iter: subsys iterator to proceed 953ca22e56dSKay Sievers * 954ca22e56dSKay Sievers * Proceed @iter to the next device and return it. Returns NULL if 955ca22e56dSKay Sievers * iteration is complete. 956ca22e56dSKay Sievers * 957ca22e56dSKay Sievers * The returned device is referenced and won't be released till 958ca22e56dSKay Sievers * iterator is proceed to the next device or exited. The caller is 959ca22e56dSKay Sievers * free to do whatever it wants to do with the device including 960ca22e56dSKay Sievers * calling back into subsys code. 961ca22e56dSKay Sievers */ 96238cdadefSGreg Kroah-Hartman static struct device *subsys_dev_iter_next(struct subsys_dev_iter *iter) 963ca22e56dSKay Sievers { 964ca22e56dSKay Sievers struct klist_node *knode; 965ca22e56dSKay Sievers struct device *dev; 966ca22e56dSKay Sievers 967ca22e56dSKay Sievers for (;;) { 968ca22e56dSKay Sievers knode = klist_next(&iter->ki); 969ca22e56dSKay Sievers if (!knode) 970ca22e56dSKay Sievers return NULL; 971371fd7a2SGeliang Tang dev = to_device_private_bus(knode)->device; 972ca22e56dSKay Sievers if (!iter->type || iter->type == dev->type) 973ca22e56dSKay Sievers return dev; 974ca22e56dSKay Sievers } 975ca22e56dSKay Sievers } 976ca22e56dSKay Sievers 977ca22e56dSKay Sievers /** 978ca22e56dSKay Sievers * subsys_dev_iter_exit - finish iteration 979ca22e56dSKay Sievers * @iter: subsys iterator to finish 980ca22e56dSKay Sievers * 981ca22e56dSKay Sievers * Finish an iteration. Always call this function after iteration is 982ca22e56dSKay Sievers * complete whether the iteration ran till the end or not. 983ca22e56dSKay Sievers */ 984af6d0743SGreg Kroah-Hartman static void subsys_dev_iter_exit(struct subsys_dev_iter *iter) 985ca22e56dSKay Sievers { 986ca22e56dSKay Sievers klist_iter_exit(&iter->ki); 987ca22e56dSKay Sievers } 988ca22e56dSKay Sievers 989ca22e56dSKay Sievers int subsys_interface_register(struct subsys_interface *sif) 990ca22e56dSKay Sievers { 991ca22e56dSKay Sievers struct bus_type *subsys; 992ca22e56dSKay Sievers struct subsys_dev_iter iter; 993ca22e56dSKay Sievers struct device *dev; 994ca22e56dSKay Sievers 995ca22e56dSKay Sievers if (!sif || !sif->subsys) 996ca22e56dSKay Sievers return -ENODEV; 997ca22e56dSKay Sievers 998ca22e56dSKay Sievers subsys = bus_get(sif->subsys); 999ca22e56dSKay Sievers if (!subsys) 1000ca22e56dSKay Sievers return -EINVAL; 1001ca22e56dSKay Sievers 1002ca22e56dSKay Sievers mutex_lock(&subsys->p->mutex); 1003ca22e56dSKay Sievers list_add_tail(&sif->node, &subsys->p->interfaces); 1004ca22e56dSKay Sievers if (sif->add_dev) { 1005ca22e56dSKay Sievers subsys_dev_iter_init(&iter, subsys, NULL, NULL); 1006ca22e56dSKay Sievers while ((dev = subsys_dev_iter_next(&iter))) 1007ca22e56dSKay Sievers sif->add_dev(dev, sif); 1008ca22e56dSKay Sievers subsys_dev_iter_exit(&iter); 1009ca22e56dSKay Sievers } 1010ca22e56dSKay Sievers mutex_unlock(&subsys->p->mutex); 1011ca22e56dSKay Sievers 1012ca22e56dSKay Sievers return 0; 1013ca22e56dSKay Sievers } 1014ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_interface_register); 1015ca22e56dSKay Sievers 1016ca22e56dSKay Sievers void subsys_interface_unregister(struct subsys_interface *sif) 1017ca22e56dSKay Sievers { 10182b31594aSJonghwan Choi struct bus_type *subsys; 1019ca22e56dSKay Sievers struct subsys_dev_iter iter; 1020ca22e56dSKay Sievers struct device *dev; 1021ca22e56dSKay Sievers 10222b31594aSJonghwan Choi if (!sif || !sif->subsys) 1023ca22e56dSKay Sievers return; 1024ca22e56dSKay Sievers 10252b31594aSJonghwan Choi subsys = sif->subsys; 10262b31594aSJonghwan Choi 1027ca22e56dSKay Sievers mutex_lock(&subsys->p->mutex); 1028ca22e56dSKay Sievers list_del_init(&sif->node); 1029ca22e56dSKay Sievers if (sif->remove_dev) { 1030ca22e56dSKay Sievers subsys_dev_iter_init(&iter, subsys, NULL, NULL); 1031ca22e56dSKay Sievers while ((dev = subsys_dev_iter_next(&iter))) 1032ca22e56dSKay Sievers sif->remove_dev(dev, sif); 1033ca22e56dSKay Sievers subsys_dev_iter_exit(&iter); 1034ca22e56dSKay Sievers } 1035ca22e56dSKay Sievers mutex_unlock(&subsys->p->mutex); 1036ca22e56dSKay Sievers 1037ca22e56dSKay Sievers bus_put(subsys); 1038ca22e56dSKay Sievers } 1039ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_interface_unregister); 1040ca22e56dSKay Sievers 1041ca22e56dSKay Sievers static void system_root_device_release(struct device *dev) 1042ca22e56dSKay Sievers { 1043ca22e56dSKay Sievers kfree(dev); 1044ca22e56dSKay Sievers } 1045d73ce004STejun Heo 1046d73ce004STejun Heo static int subsys_register(struct bus_type *subsys, 1047d73ce004STejun Heo const struct attribute_group **groups, 1048d73ce004STejun Heo struct kobject *parent_of_root) 1049d73ce004STejun Heo { 1050d73ce004STejun Heo struct device *dev; 1051d73ce004STejun Heo int err; 1052d73ce004STejun Heo 1053d73ce004STejun Heo err = bus_register(subsys); 1054d73ce004STejun Heo if (err < 0) 1055d73ce004STejun Heo return err; 1056d73ce004STejun Heo 1057d73ce004STejun Heo dev = kzalloc(sizeof(struct device), GFP_KERNEL); 1058d73ce004STejun Heo if (!dev) { 1059d73ce004STejun Heo err = -ENOMEM; 1060d73ce004STejun Heo goto err_dev; 1061d73ce004STejun Heo } 1062d73ce004STejun Heo 1063d73ce004STejun Heo err = dev_set_name(dev, "%s", subsys->name); 1064d73ce004STejun Heo if (err < 0) 1065d73ce004STejun Heo goto err_name; 1066d73ce004STejun Heo 1067d73ce004STejun Heo dev->kobj.parent = parent_of_root; 1068d73ce004STejun Heo dev->groups = groups; 1069d73ce004STejun Heo dev->release = system_root_device_release; 1070d73ce004STejun Heo 1071d73ce004STejun Heo err = device_register(dev); 1072d73ce004STejun Heo if (err < 0) 1073d73ce004STejun Heo goto err_dev_reg; 1074d73ce004STejun Heo 1075d73ce004STejun Heo subsys->dev_root = dev; 1076d73ce004STejun Heo return 0; 1077d73ce004STejun Heo 1078d73ce004STejun Heo err_dev_reg: 1079d73ce004STejun Heo put_device(dev); 1080d73ce004STejun Heo dev = NULL; 1081d73ce004STejun Heo err_name: 1082d73ce004STejun Heo kfree(dev); 1083d73ce004STejun Heo err_dev: 1084d73ce004STejun Heo bus_unregister(subsys); 1085d73ce004STejun Heo return err; 1086d73ce004STejun Heo } 1087d73ce004STejun Heo 1088ca22e56dSKay Sievers /** 1089ca22e56dSKay Sievers * subsys_system_register - register a subsystem at /sys/devices/system/ 109078d79559SRandy Dunlap * @subsys: system subsystem 109178d79559SRandy Dunlap * @groups: default attributes for the root device 1092ca22e56dSKay Sievers * 1093ca22e56dSKay Sievers * All 'system' subsystems have a /sys/devices/system/<name> root device 1094ca22e56dSKay Sievers * with the name of the subsystem. The root device can carry subsystem- 1095ca22e56dSKay Sievers * wide attributes. All registered devices are below this single root 1096ca22e56dSKay Sievers * device and are named after the subsystem with a simple enumeration 1097e227867fSMasanari Iida * number appended. The registered devices are not explicitly named; 1098ca22e56dSKay Sievers * only 'id' in the device needs to be set. 1099ca22e56dSKay Sievers * 1100ca22e56dSKay Sievers * Do not use this interface for anything new, it exists for compatibility 1101ca22e56dSKay Sievers * with bad ideas only. New subsystems should use plain subsystems; and 1102ca22e56dSKay Sievers * add the subsystem-wide attributes should be added to the subsystem 1103ca22e56dSKay Sievers * directory itself and not some create fake root-device placed in 1104ca22e56dSKay Sievers * /sys/devices/system/<name>. 1105ca22e56dSKay Sievers */ 1106ca22e56dSKay Sievers int subsys_system_register(struct bus_type *subsys, 1107ca22e56dSKay Sievers const struct attribute_group **groups) 1108ca22e56dSKay Sievers { 1109d73ce004STejun Heo return subsys_register(subsys, groups, &system_kset->kobj); 1110ca22e56dSKay Sievers } 1111ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_system_register); 1112ca22e56dSKay Sievers 1113d73ce004STejun Heo /** 1114d73ce004STejun Heo * subsys_virtual_register - register a subsystem at /sys/devices/virtual/ 1115d73ce004STejun Heo * @subsys: virtual subsystem 1116d73ce004STejun Heo * @groups: default attributes for the root device 1117d73ce004STejun Heo * 1118d73ce004STejun Heo * All 'virtual' subsystems have a /sys/devices/system/<name> root device 1119d73ce004STejun Heo * with the name of the subystem. The root device can carry subsystem-wide 1120d73ce004STejun Heo * attributes. All registered devices are below this single root device. 1121d73ce004STejun Heo * There's no restriction on device naming. This is for kernel software 1122d73ce004STejun Heo * constructs which need sysfs interface. 1123d73ce004STejun Heo */ 1124d73ce004STejun Heo int subsys_virtual_register(struct bus_type *subsys, 1125d73ce004STejun Heo const struct attribute_group **groups) 1126d73ce004STejun Heo { 1127d73ce004STejun Heo struct kobject *virtual_dir; 1128d73ce004STejun Heo 1129d73ce004STejun Heo virtual_dir = virtual_device_parent(NULL); 1130d73ce004STejun Heo if (!virtual_dir) 1131d73ce004STejun Heo return -ENOMEM; 1132d73ce004STejun Heo 1133d73ce004STejun Heo return subsys_register(subsys, groups, virtual_dir); 1134d73ce004STejun Heo } 11351c04fc35SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(subsys_virtual_register); 1136d73ce004STejun Heo 11371da177e4SLinus Torvalds int __init buses_init(void) 11381da177e4SLinus Torvalds { 113959a54833SGreg Kroah-Hartman bus_kset = kset_create_and_add("bus", &bus_uevent_ops, NULL); 114059a54833SGreg Kroah-Hartman if (!bus_kset) 114159a54833SGreg Kroah-Hartman return -ENOMEM; 1142ca22e56dSKay Sievers 1143ca22e56dSKay Sievers system_kset = kset_create_and_add("system", NULL, &devices_kset->kobj); 1144ca22e56dSKay Sievers if (!system_kset) 1145ca22e56dSKay Sievers return -ENOMEM; 1146ca22e56dSKay Sievers 114759a54833SGreg Kroah-Hartman return 0; 11481da177e4SLinus Torvalds } 1149