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 16680f03e34SKay Sievers static int bus_uevent_filter(struct kset *kset, struct kobject *kobj) 16780f03e34SKay Sievers { 16880f03e34SKay Sievers 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 } 1984f4b3743SDaniel Vetter static DRIVER_ATTR_IGNORE_LOCKDEP(unbind, S_IWUSR, 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); 215*ef6dcbddSChristoph 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 } 2244f4b3743SDaniel Vetter static DRIVER_ATTR_IGNORE_LOCKDEP(bind, S_IWUSR, 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 342ca22e56dSKay Sievers /** 343ca22e56dSKay Sievers * subsys_find_device_by_id - find a device with a specific enumeration number 344ca22e56dSKay Sievers * @subsys: subsystem 345ca22e56dSKay Sievers * @id: index 'id' in struct device 346ca22e56dSKay Sievers * @hint: device to check first 347ca22e56dSKay Sievers * 348ca22e56dSKay Sievers * Check the hint's next object and if it is a match return it directly, 349ca22e56dSKay Sievers * otherwise, fall back to a full list search. Either way a reference for 350ca22e56dSKay Sievers * the returned object is taken. 351ca22e56dSKay Sievers */ 352ca22e56dSKay Sievers struct device *subsys_find_device_by_id(struct bus_type *subsys, unsigned int id, 353ca22e56dSKay Sievers struct device *hint) 354ca22e56dSKay Sievers { 355ca22e56dSKay Sievers struct klist_iter i; 356ca22e56dSKay Sievers struct device *dev; 357ca22e56dSKay Sievers 358ca22e56dSKay Sievers if (!subsys) 359ca22e56dSKay Sievers return NULL; 360ca22e56dSKay Sievers 361ca22e56dSKay Sievers if (hint) { 3627cd9c9bbSGreg Kroah-Hartman klist_iter_init_node(&subsys->p->klist_devices, &i, &hint->p->knode_bus); 363ca22e56dSKay Sievers dev = next_device(&i); 364ca22e56dSKay Sievers if (dev && dev->id == id && get_device(dev)) { 365ca22e56dSKay Sievers klist_iter_exit(&i); 366ca22e56dSKay Sievers return dev; 367ca22e56dSKay Sievers } 368ca22e56dSKay Sievers klist_iter_exit(&i); 369ca22e56dSKay Sievers } 370ca22e56dSKay Sievers 371ca22e56dSKay Sievers klist_iter_init_node(&subsys->p->klist_devices, &i, NULL); 372ca22e56dSKay Sievers while ((dev = next_device(&i))) { 373ca22e56dSKay Sievers if (dev->id == id && get_device(dev)) { 374ca22e56dSKay Sievers klist_iter_exit(&i); 375ca22e56dSKay Sievers return dev; 376ca22e56dSKay Sievers } 377ca22e56dSKay Sievers } 378ca22e56dSKay Sievers klist_iter_exit(&i); 379ca22e56dSKay Sievers return NULL; 380ca22e56dSKay Sievers } 381ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_find_device_by_id); 382ca22e56dSKay Sievers 38338fdac3cSmochel@digitalimplant.org static struct device_driver *next_driver(struct klist_iter *i) 38438fdac3cSmochel@digitalimplant.org { 38538fdac3cSmochel@digitalimplant.org struct klist_node *n = klist_next(i); 386e5dd1278SGreg Kroah-Hartman struct driver_private *drv_priv; 387e5dd1278SGreg Kroah-Hartman 388e5dd1278SGreg Kroah-Hartman if (n) { 389e5dd1278SGreg Kroah-Hartman drv_priv = container_of(n, struct driver_private, knode_bus); 390e5dd1278SGreg Kroah-Hartman return drv_priv->driver; 391e5dd1278SGreg Kroah-Hartman } 392e5dd1278SGreg Kroah-Hartman return NULL; 39338fdac3cSmochel@digitalimplant.org } 39438fdac3cSmochel@digitalimplant.org 3951da177e4SLinus Torvalds /** 3961da177e4SLinus Torvalds * bus_for_each_drv - driver iterator 3971da177e4SLinus Torvalds * @bus: bus we're dealing with. 3981da177e4SLinus Torvalds * @start: driver to start iterating on. 3991da177e4SLinus Torvalds * @data: data to pass to the callback. 4001da177e4SLinus Torvalds * @fn: function to call for each driver. 4011da177e4SLinus Torvalds * 4021da177e4SLinus Torvalds * This is nearly identical to the device iterator above. 4031da177e4SLinus Torvalds * We iterate over each driver that belongs to @bus, and call 4041da177e4SLinus Torvalds * @fn for each. If @fn returns anything but 0, we break out 4051da177e4SLinus Torvalds * and return it. If @start is not NULL, we use it as the head 4061da177e4SLinus Torvalds * of the list. 4071da177e4SLinus Torvalds * 4081da177e4SLinus Torvalds * NOTE: we don't return the driver that returns a non-zero 4091da177e4SLinus Torvalds * value, nor do we leave the reference count incremented for that 4101da177e4SLinus Torvalds * driver. If the caller needs to know that info, it must set it 4111da177e4SLinus Torvalds * in the callback. It must also be sure to increment the refcount 4121da177e4SLinus Torvalds * so it doesn't disappear before returning to the caller. 4131da177e4SLinus Torvalds */ 4141da177e4SLinus Torvalds int bus_for_each_drv(struct bus_type *bus, struct device_driver *start, 4151da177e4SLinus Torvalds void *data, int (*fn)(struct device_driver *, void *)) 4161da177e4SLinus Torvalds { 41738fdac3cSmochel@digitalimplant.org struct klist_iter i; 41838fdac3cSmochel@digitalimplant.org struct device_driver *drv; 41938fdac3cSmochel@digitalimplant.org int error = 0; 4201da177e4SLinus Torvalds 42138fdac3cSmochel@digitalimplant.org if (!bus) 42238fdac3cSmochel@digitalimplant.org return -EINVAL; 42338fdac3cSmochel@digitalimplant.org 4247cd9c9bbSGreg Kroah-Hartman klist_iter_init_node(&bus->p->klist_drivers, &i, 425e5dd1278SGreg Kroah-Hartman start ? &start->p->knode_bus : NULL); 42638fdac3cSmochel@digitalimplant.org while ((drv = next_driver(&i)) && !error) 42738fdac3cSmochel@digitalimplant.org error = fn(drv, data); 42838fdac3cSmochel@digitalimplant.org klist_iter_exit(&i); 42938fdac3cSmochel@digitalimplant.org return error; 4301da177e4SLinus Torvalds } 4314a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_for_each_drv); 4321da177e4SLinus Torvalds 4331da177e4SLinus Torvalds /** 4341da177e4SLinus Torvalds * bus_add_device - add device to bus 4351da177e4SLinus Torvalds * @dev: device being added 4361da177e4SLinus Torvalds * 4372023c610SAlan Stern * - Add device's bus attributes. 4382023c610SAlan Stern * - Create links to device's bus. 4391da177e4SLinus Torvalds * - Add the device to its bus's list of devices. 4401da177e4SLinus Torvalds */ 4411da177e4SLinus Torvalds int bus_add_device(struct device *dev) 4421da177e4SLinus Torvalds { 4435901d014SGreg Kroah-Hartman struct bus_type *bus = bus_get(dev->bus); 4441da177e4SLinus Torvalds int error = 0; 4451da177e4SLinus Torvalds 4461da177e4SLinus Torvalds if (bus) { 4471e0b2cf9SKay Sievers pr_debug("bus: '%s': add device %s\n", bus->name, dev_name(dev)); 448fa6fdb33SGreg Kroah-Hartman error = device_add_groups(dev, bus->dev_groups); 449fa6fdb33SGreg Kroah-Hartman if (error) 450b46c7337SGreg Kroah-Hartman goto out_put; 451c6f7e72aSGreg Kroah-Hartman error = sysfs_create_link(&bus->p->devices_kset->kobj, 4521e0b2cf9SKay Sievers &dev->kobj, dev_name(dev)); 453f86db396SAndrew Morton if (error) 4541c34203aSJunjie Mao goto out_groups; 455f86db396SAndrew Morton error = sysfs_create_link(&dev->kobj, 456c6f7e72aSGreg Kroah-Hartman &dev->bus->p->subsys.kobj, "subsystem"); 457f86db396SAndrew Morton if (error) 458513e7337SCornelia Huck goto out_subsys; 4592023c610SAlan Stern klist_add_tail(&dev->p->knode_bus, &bus->p->klist_devices); 4601da177e4SLinus Torvalds } 461513e7337SCornelia Huck return 0; 462513e7337SCornelia Huck 463513e7337SCornelia Huck out_subsys: 4641e0b2cf9SKay Sievers sysfs_remove_link(&bus->p->devices_kset->kobj, dev_name(dev)); 465fa6fdb33SGreg Kroah-Hartman out_groups: 466fa6fdb33SGreg Kroah-Hartman device_remove_groups(dev, bus->dev_groups); 467513e7337SCornelia Huck out_put: 468fc1ede58SGreg Kroah-Hartman bus_put(dev->bus); 4691da177e4SLinus Torvalds return error; 4701da177e4SLinus Torvalds } 4711da177e4SLinus Torvalds 4721da177e4SLinus Torvalds /** 4732023c610SAlan Stern * bus_probe_device - probe drivers for a new device 4742023c610SAlan Stern * @dev: device to probe 47553877d06SKay Sievers * 4762023c610SAlan Stern * - Automatically probe for a driver if the bus allows it. 47753877d06SKay Sievers */ 4782023c610SAlan Stern void bus_probe_device(struct device *dev) 47953877d06SKay Sievers { 48053877d06SKay Sievers struct bus_type *bus = dev->bus; 481ca22e56dSKay Sievers struct subsys_interface *sif; 48253877d06SKay Sievers 483ca22e56dSKay Sievers if (!bus) 484ca22e56dSKay Sievers return; 485ca22e56dSKay Sievers 486765230b5SDmitry Torokhov if (bus->p->drivers_autoprobe) 487765230b5SDmitry Torokhov device_initial_probe(dev); 488ca22e56dSKay Sievers 489ca22e56dSKay Sievers mutex_lock(&bus->p->mutex); 490ca22e56dSKay Sievers list_for_each_entry(sif, &bus->p->interfaces, node) 491ca22e56dSKay Sievers if (sif->add_dev) 492ca22e56dSKay Sievers sif->add_dev(dev, sif); 493ca22e56dSKay Sievers mutex_unlock(&bus->p->mutex); 494f86db396SAndrew Morton } 49553877d06SKay Sievers 49653877d06SKay Sievers /** 4971da177e4SLinus Torvalds * bus_remove_device - remove device from bus 4981da177e4SLinus Torvalds * @dev: device to be removed 4991da177e4SLinus Torvalds * 500ca22e56dSKay Sievers * - Remove device from all interfaces. 501ca22e56dSKay Sievers * - Remove symlink from bus' directory. 5021da177e4SLinus Torvalds * - Delete device from bus's list. 5031da177e4SLinus Torvalds * - Detach from its driver. 5041da177e4SLinus Torvalds * - Drop reference taken in bus_add_device(). 5051da177e4SLinus Torvalds */ 5061da177e4SLinus Torvalds void bus_remove_device(struct device *dev) 5071da177e4SLinus Torvalds { 508ca22e56dSKay Sievers struct bus_type *bus = dev->bus; 509ca22e56dSKay Sievers struct subsys_interface *sif; 510ca22e56dSKay Sievers 511ca22e56dSKay Sievers if (!bus) 512ca22e56dSKay Sievers return; 513ca22e56dSKay Sievers 514ca22e56dSKay Sievers mutex_lock(&bus->p->mutex); 515ca22e56dSKay Sievers list_for_each_entry(sif, &bus->p->interfaces, node) 516ca22e56dSKay Sievers if (sif->remove_dev) 517ca22e56dSKay Sievers sif->remove_dev(dev, sif); 518ca22e56dSKay Sievers mutex_unlock(&bus->p->mutex); 519ca22e56dSKay Sievers 520b9d9c82bSKay Sievers sysfs_remove_link(&dev->kobj, "subsystem"); 5214a3ad20cSGreg Kroah-Hartman sysfs_remove_link(&dev->bus->p->devices_kset->kobj, 5221e0b2cf9SKay Sievers dev_name(dev)); 523fa6fdb33SGreg Kroah-Hartman device_remove_groups(dev, dev->bus->dev_groups); 524ae1b4171SGreg Kroah-Hartman if (klist_node_attached(&dev->p->knode_bus)) 525ae1b4171SGreg Kroah-Hartman klist_del(&dev->p->knode_bus); 5263f62e570SGreg Kroah-Hartman 5274a3ad20cSGreg Kroah-Hartman pr_debug("bus: '%s': remove device %s\n", 5281e0b2cf9SKay Sievers dev->bus->name, dev_name(dev)); 5291da177e4SLinus Torvalds device_release_driver(dev); 530fc1ede58SGreg Kroah-Hartman bus_put(dev->bus); 5311da177e4SLinus Torvalds } 5321da177e4SLinus Torvalds 533f86db396SAndrew Morton static int __must_check add_bind_files(struct device_driver *drv) 534874c6241SGreg Kroah-Hartman { 535f86db396SAndrew Morton int ret; 536f86db396SAndrew Morton 537f86db396SAndrew Morton ret = driver_create_file(drv, &driver_attr_unbind); 538f86db396SAndrew Morton if (ret == 0) { 539f86db396SAndrew Morton ret = driver_create_file(drv, &driver_attr_bind); 540f86db396SAndrew Morton if (ret) 541f86db396SAndrew Morton driver_remove_file(drv, &driver_attr_unbind); 542f86db396SAndrew Morton } 543f86db396SAndrew Morton return ret; 544874c6241SGreg Kroah-Hartman } 545874c6241SGreg Kroah-Hartman 546874c6241SGreg Kroah-Hartman static void remove_bind_files(struct device_driver *drv) 547874c6241SGreg Kroah-Hartman { 548874c6241SGreg Kroah-Hartman driver_remove_file(drv, &driver_attr_bind); 549874c6241SGreg Kroah-Hartman driver_remove_file(drv, &driver_attr_unbind); 550874c6241SGreg Kroah-Hartman } 551b8c5cec2SKay Sievers 5522e7189b6SGreg Kroah-Hartman static BUS_ATTR_WO(drivers_probe); 5532e7189b6SGreg Kroah-Hartman static BUS_ATTR_RW(drivers_autoprobe); 5548380770cSKay Sievers 555b8c5cec2SKay Sievers static int add_probe_files(struct bus_type *bus) 556b8c5cec2SKay Sievers { 557b8c5cec2SKay Sievers int retval; 558b8c5cec2SKay Sievers 5598380770cSKay Sievers retval = bus_create_file(bus, &bus_attr_drivers_probe); 560b8c5cec2SKay Sievers if (retval) 561b8c5cec2SKay Sievers goto out; 562b8c5cec2SKay Sievers 5638380770cSKay Sievers retval = bus_create_file(bus, &bus_attr_drivers_autoprobe); 564b8c5cec2SKay Sievers if (retval) 5658380770cSKay Sievers bus_remove_file(bus, &bus_attr_drivers_probe); 566b8c5cec2SKay Sievers out: 567b8c5cec2SKay Sievers return retval; 568b8c5cec2SKay Sievers } 569b8c5cec2SKay Sievers 570b8c5cec2SKay Sievers static void remove_probe_files(struct bus_type *bus) 571b8c5cec2SKay Sievers { 5728380770cSKay Sievers bus_remove_file(bus, &bus_attr_drivers_autoprobe); 5738380770cSKay Sievers bus_remove_file(bus, &bus_attr_drivers_probe); 574b8c5cec2SKay Sievers } 5751da177e4SLinus Torvalds 5762581c9ccSGreg Kroah-Hartman static ssize_t uevent_store(struct device_driver *drv, const char *buf, 5772581c9ccSGreg Kroah-Hartman size_t count) 5787ac1cf4aSKay Sievers { 579df44b479SPeter Rajnoha int rc; 580df44b479SPeter Rajnoha 581df44b479SPeter Rajnoha rc = kobject_synth_uevent(&drv->p->kobj, buf, count); 582df44b479SPeter Rajnoha return rc ? rc : count; 5837ac1cf4aSKay Sievers } 5842581c9ccSGreg Kroah-Hartman static DRIVER_ATTR_WO(uevent); 5857ac1cf4aSKay Sievers 5861da177e4SLinus Torvalds /** 5871da177e4SLinus Torvalds * bus_add_driver - Add a driver to the bus. 5881da177e4SLinus Torvalds * @drv: driver. 5891da177e4SLinus Torvalds */ 5901da177e4SLinus Torvalds int bus_add_driver(struct device_driver *drv) 5911da177e4SLinus Torvalds { 592e5dd1278SGreg Kroah-Hartman struct bus_type *bus; 593e5dd1278SGreg Kroah-Hartman struct driver_private *priv; 5941da177e4SLinus Torvalds int error = 0; 5951da177e4SLinus Torvalds 596e5dd1278SGreg Kroah-Hartman bus = bus_get(drv->bus); 597d9fd4d3bSJeff Garzik if (!bus) 5984f6e1945SGreg Kroah-Hartman return -EINVAL; 599d9fd4d3bSJeff Garzik 6007dc72b28SGreg Kroah-Hartman pr_debug("bus: '%s': add driver %s\n", bus->name, drv->name); 601e5dd1278SGreg Kroah-Hartman 602e5dd1278SGreg Kroah-Hartman priv = kzalloc(sizeof(*priv), GFP_KERNEL); 60307634464SCornelia Huck if (!priv) { 60407634464SCornelia Huck error = -ENOMEM; 60507634464SCornelia Huck goto out_put_bus; 60607634464SCornelia Huck } 607e5dd1278SGreg Kroah-Hartman klist_init(&priv->klist_devices, NULL, NULL); 608e5dd1278SGreg Kroah-Hartman priv->driver = drv; 609e5dd1278SGreg Kroah-Hartman drv->p = priv; 610c8e90d82SGreg Kroah-Hartman priv->kobj.kset = bus->p->drivers_kset; 611c8e90d82SGreg Kroah-Hartman error = kobject_init_and_add(&priv->kobj, &driver_ktype, NULL, 612c8e90d82SGreg Kroah-Hartman "%s", drv->name); 613dc0afa83SCornelia Huck if (error) 61407634464SCornelia Huck goto out_unregister; 6151da177e4SLinus Torvalds 616190888acSMing Lei klist_add_tail(&priv->knode_bus, &bus->p->klist_drivers); 617c6f7e72aSGreg Kroah-Hartman if (drv->bus->p->drivers_autoprobe) { 618f86db396SAndrew Morton error = driver_attach(drv); 619f86db396SAndrew Morton if (error) 620f86db396SAndrew Morton goto out_unregister; 621b8c5cec2SKay Sievers } 6221da177e4SLinus Torvalds module_add_driver(drv->owner, drv); 6231da177e4SLinus Torvalds 6247ac1cf4aSKay Sievers error = driver_create_file(drv, &driver_attr_uevent); 6257ac1cf4aSKay Sievers if (error) { 6267ac1cf4aSKay Sievers printk(KERN_ERR "%s: uevent attr (%s) failed\n", 6272b3a302aSHarvey Harrison __func__, drv->name); 6287ac1cf4aSKay Sievers } 629e18945b1SGreg Kroah-Hartman error = driver_add_groups(drv, bus->drv_groups); 630f86db396SAndrew Morton if (error) { 631f86db396SAndrew Morton /* How the hell do we get out of this pickle? Give up */ 6324044b2fcSJoe Pater printk(KERN_ERR "%s: driver_add_groups(%s) failed\n", 633ed0617b5SGreg Kroah-Hartman __func__, drv->name); 634e18945b1SGreg Kroah-Hartman } 6351a6f2a75SDmitry Torokhov 6361a6f2a75SDmitry Torokhov if (!drv->suppress_bind_attrs) { 637f86db396SAndrew Morton error = add_bind_files(drv); 638f86db396SAndrew Morton if (error) { 639f86db396SAndrew Morton /* Ditto */ 640f86db396SAndrew Morton printk(KERN_ERR "%s: add_bind_files(%s) failed\n", 6412b3a302aSHarvey Harrison __func__, drv->name); 642f86db396SAndrew Morton } 6431a6f2a75SDmitry Torokhov } 644d9fd4d3bSJeff Garzik 6455c8563d7SKay Sievers return 0; 6461a6f2a75SDmitry Torokhov 647f86db396SAndrew Morton out_unregister: 64899b28f1bSPhil Carmody kobject_put(&priv->kobj); 6490f9b011dSChristophe JAILLET /* drv->p is freed in driver_release() */ 6505c8563d7SKay Sievers drv->p = NULL; 651f86db396SAndrew Morton out_put_bus: 652fc1ede58SGreg Kroah-Hartman bus_put(bus); 653f86db396SAndrew Morton return error; 6541da177e4SLinus Torvalds } 6551da177e4SLinus Torvalds 6561da177e4SLinus Torvalds /** 6571da177e4SLinus Torvalds * bus_remove_driver - delete driver from bus's knowledge. 6581da177e4SLinus Torvalds * @drv: driver. 6591da177e4SLinus Torvalds * 6601da177e4SLinus Torvalds * Detach the driver from the devices it controls, and remove 6611da177e4SLinus Torvalds * it from its bus's list of drivers. Finally, we drop the reference 6621da177e4SLinus Torvalds * to the bus we took in bus_add_driver(). 6631da177e4SLinus Torvalds */ 6641da177e4SLinus Torvalds void bus_remove_driver(struct device_driver *drv) 6651da177e4SLinus Torvalds { 666d9fd4d3bSJeff Garzik if (!drv->bus) 667d9fd4d3bSJeff Garzik return; 668d9fd4d3bSJeff Garzik 6691a6f2a75SDmitry Torokhov if (!drv->suppress_bind_attrs) 670874c6241SGreg Kroah-Hartman remove_bind_files(drv); 671ed0617b5SGreg Kroah-Hartman driver_remove_groups(drv, drv->bus->drv_groups); 6727ac1cf4aSKay Sievers driver_remove_file(drv, &driver_attr_uevent); 673e5dd1278SGreg Kroah-Hartman klist_remove(&drv->p->knode_bus); 6747dc72b28SGreg Kroah-Hartman pr_debug("bus: '%s': remove driver %s\n", drv->bus->name, drv->name); 6751da177e4SLinus Torvalds driver_detach(drv); 6761da177e4SLinus Torvalds module_remove_driver(drv); 677c10997f6SGreg Kroah-Hartman kobject_put(&drv->p->kobj); 678fc1ede58SGreg Kroah-Hartman bus_put(drv->bus); 6791da177e4SLinus Torvalds } 6801da177e4SLinus Torvalds 6811da177e4SLinus Torvalds /* Helper for bus_rescan_devices's iter */ 682f86db396SAndrew Morton static int __must_check bus_rescan_devices_helper(struct device *dev, 683f86db396SAndrew Morton void *data) 6841da177e4SLinus Torvalds { 685f86db396SAndrew Morton int ret = 0; 686f86db396SAndrew Morton 687bf74ad5bSAlan Stern if (!dev->driver) { 6888c97a46aSMartin Liu if (dev->parent && dev->bus->need_parent_lock) 6898e9394ceSGreg Kroah-Hartman device_lock(dev->parent); 690f86db396SAndrew Morton ret = device_attach(dev); 6918c97a46aSMartin Liu if (dev->parent && dev->bus->need_parent_lock) 6928e9394ceSGreg Kroah-Hartman device_unlock(dev->parent); 693bf74ad5bSAlan Stern } 694f86db396SAndrew Morton return ret < 0 ? ret : 0; 6951da177e4SLinus Torvalds } 6961da177e4SLinus Torvalds 6971da177e4SLinus Torvalds /** 6981da177e4SLinus Torvalds * bus_rescan_devices - rescan devices on the bus for possible drivers 6991da177e4SLinus Torvalds * @bus: the bus to scan. 7001da177e4SLinus Torvalds * 7011da177e4SLinus Torvalds * This function will look for devices on the bus with no driver 70223d3d602SGreg Kroah-Hartman * attached and rescan it against existing drivers to see if it matches 70323d3d602SGreg Kroah-Hartman * any by calling device_attach() for the unbound devices. 7041da177e4SLinus Torvalds */ 705f86db396SAndrew Morton int bus_rescan_devices(struct bus_type *bus) 7061da177e4SLinus Torvalds { 707f86db396SAndrew Morton return bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper); 7081da177e4SLinus Torvalds } 7094a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_rescan_devices); 7101da177e4SLinus Torvalds 711e935d5daSMoore, Eric /** 712e935d5daSMoore, Eric * device_reprobe - remove driver for a device and probe for a new driver 713e935d5daSMoore, Eric * @dev: the device to reprobe 714e935d5daSMoore, Eric * 715e935d5daSMoore, Eric * This function detaches the attached driver (if any) for the given 716e935d5daSMoore, Eric * device and restarts the driver probing process. It is intended 717e935d5daSMoore, Eric * to use if probing criteria changed during a devices lifetime and 718e935d5daSMoore, Eric * driver attachment should change accordingly. 719e935d5daSMoore, Eric */ 720f86db396SAndrew Morton int device_reprobe(struct device *dev) 721e935d5daSMoore, Eric { 722ed88747cSAlexander Duyck if (dev->driver) 723ed88747cSAlexander Duyck device_driver_detach(dev); 724f86db396SAndrew Morton return bus_rescan_devices_helper(dev, NULL); 725e935d5daSMoore, Eric } 726e935d5daSMoore, Eric EXPORT_SYMBOL_GPL(device_reprobe); 7271da177e4SLinus Torvalds 72812478ba0SGreg Kroah-Hartman static int bus_add_groups(struct bus_type *bus, 72912478ba0SGreg Kroah-Hartman const struct attribute_group **groups) 73012478ba0SGreg Kroah-Hartman { 7313e9b2baeSGreg Kroah-Hartman return sysfs_create_groups(&bus->p->subsys.kobj, groups); 73212478ba0SGreg Kroah-Hartman } 73312478ba0SGreg Kroah-Hartman 73412478ba0SGreg Kroah-Hartman static void bus_remove_groups(struct bus_type *bus, 73512478ba0SGreg Kroah-Hartman const struct attribute_group **groups) 73612478ba0SGreg Kroah-Hartman { 7373e9b2baeSGreg Kroah-Hartman sysfs_remove_groups(&bus->p->subsys.kobj, groups); 73812478ba0SGreg Kroah-Hartman } 73912478ba0SGreg Kroah-Hartman 74034bb61f9SJames Bottomley static void klist_devices_get(struct klist_node *n) 74134bb61f9SJames Bottomley { 742ae1b4171SGreg Kroah-Hartman struct device_private *dev_prv = to_device_private_bus(n); 743ae1b4171SGreg Kroah-Hartman struct device *dev = dev_prv->device; 74434bb61f9SJames Bottomley 74534bb61f9SJames Bottomley get_device(dev); 74634bb61f9SJames Bottomley } 74734bb61f9SJames Bottomley 74834bb61f9SJames Bottomley static void klist_devices_put(struct klist_node *n) 74934bb61f9SJames Bottomley { 750ae1b4171SGreg Kroah-Hartman struct device_private *dev_prv = to_device_private_bus(n); 751ae1b4171SGreg Kroah-Hartman struct device *dev = dev_prv->device; 75234bb61f9SJames Bottomley 75334bb61f9SJames Bottomley put_device(dev); 75434bb61f9SJames Bottomley } 75534bb61f9SJames Bottomley 7567ac1cf4aSKay Sievers static ssize_t bus_uevent_store(struct bus_type *bus, 7577ac1cf4aSKay Sievers const char *buf, size_t count) 7587ac1cf4aSKay Sievers { 759df44b479SPeter Rajnoha int rc; 760df44b479SPeter Rajnoha 761df44b479SPeter Rajnoha rc = kobject_synth_uevent(&bus->p->subsys.kobj, buf, count); 762df44b479SPeter Rajnoha return rc ? rc : count; 7637ac1cf4aSKay Sievers } 764a4723041SGreg Kroah-Hartman /* 765a4723041SGreg Kroah-Hartman * "open code" the old BUS_ATTR() macro here. We want to use BUS_ATTR_WO() 766a4723041SGreg Kroah-Hartman * here, but can not use it as earlier in the file we have 767a4723041SGreg Kroah-Hartman * DEVICE_ATTR_WO(uevent), which would cause a clash with the with the store 768a4723041SGreg Kroah-Hartman * function name. 769a4723041SGreg Kroah-Hartman */ 770a4723041SGreg Kroah-Hartman static struct bus_attribute bus_attr_uevent = __ATTR(uevent, S_IWUSR, NULL, 771a4723041SGreg Kroah-Hartman bus_uevent_store); 7727ac1cf4aSKay Sievers 7731da177e4SLinus Torvalds /** 774be871b7eSMichal Hocko * bus_register - register a driver-core subsystem 77578d79559SRandy Dunlap * @bus: bus to register 7761da177e4SLinus Torvalds * 77778d79559SRandy Dunlap * Once we have that, we register the bus with the kobject 7781da177e4SLinus Torvalds * infrastructure, then register the children subsystems it has: 779ca22e56dSKay Sievers * the devices and drivers that belong to the subsystem. 7801da177e4SLinus Torvalds */ 781be871b7eSMichal Hocko int bus_register(struct bus_type *bus) 7821da177e4SLinus Torvalds { 7831da177e4SLinus Torvalds int retval; 7846b6e39a6SKay Sievers struct subsys_private *priv; 785be871b7eSMichal Hocko struct lock_class_key *key = &bus->lock_key; 7861da177e4SLinus Torvalds 7876b6e39a6SKay Sievers priv = kzalloc(sizeof(struct subsys_private), GFP_KERNEL); 788c6f7e72aSGreg Kroah-Hartman if (!priv) 789c6f7e72aSGreg Kroah-Hartman return -ENOMEM; 790116af378SBenjamin Herrenschmidt 791c6f7e72aSGreg Kroah-Hartman priv->bus = bus; 792c6f7e72aSGreg Kroah-Hartman bus->p = priv; 793c6f7e72aSGreg Kroah-Hartman 794c6f7e72aSGreg Kroah-Hartman BLOCKING_INIT_NOTIFIER_HEAD(&priv->bus_notifier); 795c6f7e72aSGreg Kroah-Hartman 796c6f7e72aSGreg Kroah-Hartman retval = kobject_set_name(&priv->subsys.kobj, "%s", bus->name); 7971da177e4SLinus Torvalds if (retval) 7981da177e4SLinus Torvalds goto out; 7991da177e4SLinus Torvalds 800c6f7e72aSGreg Kroah-Hartman priv->subsys.kobj.kset = bus_kset; 801c6f7e72aSGreg Kroah-Hartman priv->subsys.kobj.ktype = &bus_ktype; 802c6f7e72aSGreg Kroah-Hartman priv->drivers_autoprobe = 1; 803d6b05b84SGreg Kroah-Hartman 804c6f7e72aSGreg Kroah-Hartman retval = kset_register(&priv->subsys); 8051da177e4SLinus Torvalds if (retval) 8061da177e4SLinus Torvalds goto out; 8071da177e4SLinus Torvalds 8087ac1cf4aSKay Sievers retval = bus_create_file(bus, &bus_attr_uevent); 8097ac1cf4aSKay Sievers if (retval) 8107ac1cf4aSKay Sievers goto bus_uevent_fail; 8117ac1cf4aSKay Sievers 812c6f7e72aSGreg Kroah-Hartman priv->devices_kset = kset_create_and_add("devices", NULL, 813c6f7e72aSGreg Kroah-Hartman &priv->subsys.kobj); 814c6f7e72aSGreg Kroah-Hartman if (!priv->devices_kset) { 8153d899596SGreg Kroah-Hartman retval = -ENOMEM; 8161da177e4SLinus Torvalds goto bus_devices_fail; 8173d899596SGreg Kroah-Hartman } 8181da177e4SLinus Torvalds 819c6f7e72aSGreg Kroah-Hartman priv->drivers_kset = kset_create_and_add("drivers", NULL, 820c6f7e72aSGreg Kroah-Hartman &priv->subsys.kobj); 821c6f7e72aSGreg Kroah-Hartman if (!priv->drivers_kset) { 8226dcec251SGreg Kroah-Hartman retval = -ENOMEM; 8231da177e4SLinus Torvalds goto bus_drivers_fail; 8246dcec251SGreg Kroah-Hartman } 825465c7a3aSmochel@digitalimplant.org 826ca22e56dSKay Sievers INIT_LIST_HEAD(&priv->interfaces); 827ca22e56dSKay Sievers __mutex_init(&priv->mutex, "subsys mutex", key); 828c6f7e72aSGreg Kroah-Hartman klist_init(&priv->klist_devices, klist_devices_get, klist_devices_put); 829c6f7e72aSGreg Kroah-Hartman klist_init(&priv->klist_drivers, NULL, NULL); 830b8c5cec2SKay Sievers 831b8c5cec2SKay Sievers retval = add_probe_files(bus); 832b8c5cec2SKay Sievers if (retval) 833b8c5cec2SKay Sievers goto bus_probe_files_fail; 834b8c5cec2SKay Sievers 83512478ba0SGreg Kroah-Hartman retval = bus_add_groups(bus, bus->bus_groups); 83612478ba0SGreg Kroah-Hartman if (retval) 83712478ba0SGreg Kroah-Hartman goto bus_groups_fail; 8381da177e4SLinus Torvalds 8397dc72b28SGreg Kroah-Hartman pr_debug("bus: '%s': registered\n", bus->name); 8401da177e4SLinus Torvalds return 0; 8411da177e4SLinus Torvalds 84212478ba0SGreg Kroah-Hartman bus_groups_fail: 843b8c5cec2SKay Sievers remove_probe_files(bus); 844b8c5cec2SKay Sievers bus_probe_files_fail: 845c6f7e72aSGreg Kroah-Hartman kset_unregister(bus->p->drivers_kset); 8461da177e4SLinus Torvalds bus_drivers_fail: 847c6f7e72aSGreg Kroah-Hartman kset_unregister(bus->p->devices_kset); 8481da177e4SLinus Torvalds bus_devices_fail: 8497ac1cf4aSKay Sievers bus_remove_file(bus, &bus_attr_uevent); 8507ac1cf4aSKay Sievers bus_uevent_fail: 851c6f7e72aSGreg Kroah-Hartman kset_unregister(&bus->p->subsys); 8521da177e4SLinus Torvalds out: 853600c20f3SJike Song kfree(bus->p); 854f48f3febSDave Young bus->p = NULL; 8551da177e4SLinus Torvalds return retval; 8561da177e4SLinus Torvalds } 857be871b7eSMichal Hocko EXPORT_SYMBOL_GPL(bus_register); 8581da177e4SLinus Torvalds 8591da177e4SLinus Torvalds /** 8601da177e4SLinus Torvalds * bus_unregister - remove a bus from the system 8611da177e4SLinus Torvalds * @bus: bus. 8621da177e4SLinus Torvalds * 8631da177e4SLinus Torvalds * Unregister the child subsystems and the bus itself. 864fc1ede58SGreg Kroah-Hartman * Finally, we call bus_put() to release the refcount 8651da177e4SLinus Torvalds */ 8661da177e4SLinus Torvalds void bus_unregister(struct bus_type *bus) 8671da177e4SLinus Torvalds { 8687dc72b28SGreg Kroah-Hartman pr_debug("bus: '%s': unregistering\n", bus->name); 869ca22e56dSKay Sievers if (bus->dev_root) 870ca22e56dSKay Sievers device_unregister(bus->dev_root); 87112478ba0SGreg Kroah-Hartman bus_remove_groups(bus, bus->bus_groups); 872b8c5cec2SKay Sievers remove_probe_files(bus); 873c6f7e72aSGreg Kroah-Hartman kset_unregister(bus->p->drivers_kset); 874c6f7e72aSGreg Kroah-Hartman kset_unregister(bus->p->devices_kset); 8757ac1cf4aSKay Sievers bus_remove_file(bus, &bus_attr_uevent); 876c6f7e72aSGreg Kroah-Hartman kset_unregister(&bus->p->subsys); 8771da177e4SLinus Torvalds } 8784a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_unregister); 8791da177e4SLinus Torvalds 880116af378SBenjamin Herrenschmidt int bus_register_notifier(struct bus_type *bus, struct notifier_block *nb) 881116af378SBenjamin Herrenschmidt { 882c6f7e72aSGreg Kroah-Hartman return blocking_notifier_chain_register(&bus->p->bus_notifier, nb); 883116af378SBenjamin Herrenschmidt } 884116af378SBenjamin Herrenschmidt EXPORT_SYMBOL_GPL(bus_register_notifier); 885116af378SBenjamin Herrenschmidt 886116af378SBenjamin Herrenschmidt int bus_unregister_notifier(struct bus_type *bus, struct notifier_block *nb) 887116af378SBenjamin Herrenschmidt { 888c6f7e72aSGreg Kroah-Hartman return blocking_notifier_chain_unregister(&bus->p->bus_notifier, nb); 889116af378SBenjamin Herrenschmidt } 890116af378SBenjamin Herrenschmidt EXPORT_SYMBOL_GPL(bus_unregister_notifier); 891116af378SBenjamin Herrenschmidt 8920fed80f7SGreg Kroah-Hartman struct kset *bus_get_kset(struct bus_type *bus) 8930fed80f7SGreg Kroah-Hartman { 894c6f7e72aSGreg Kroah-Hartman return &bus->p->subsys; 8950fed80f7SGreg Kroah-Hartman } 8960fed80f7SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_get_kset); 8970fed80f7SGreg Kroah-Hartman 898b249072eSGreg Kroah-Hartman struct klist *bus_get_device_klist(struct bus_type *bus) 899b249072eSGreg Kroah-Hartman { 900c6f7e72aSGreg Kroah-Hartman return &bus->p->klist_devices; 901b249072eSGreg Kroah-Hartman } 902b249072eSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_get_device_klist); 903b249072eSGreg Kroah-Hartman 90499178b03SGreg Kroah-Hartman /* 905dca25ebdSRobert P. J. Day * Yes, this forcibly breaks the klist abstraction temporarily. It 90699178b03SGreg Kroah-Hartman * just wants to sort the klist, not change reference counts and 90799178b03SGreg Kroah-Hartman * take/drop locks rapidly in the process. It does all this while 90899178b03SGreg Kroah-Hartman * holding the lock for the list, so objects can't otherwise be 90999178b03SGreg Kroah-Hartman * added/removed while we're swizzling. 91099178b03SGreg Kroah-Hartman */ 91199178b03SGreg Kroah-Hartman static void device_insertion_sort_klist(struct device *a, struct list_head *list, 91299178b03SGreg Kroah-Hartman int (*compare)(const struct device *a, 91399178b03SGreg Kroah-Hartman const struct device *b)) 91499178b03SGreg Kroah-Hartman { 91599178b03SGreg Kroah-Hartman struct klist_node *n; 916ae1b4171SGreg Kroah-Hartman struct device_private *dev_prv; 91799178b03SGreg Kroah-Hartman struct device *b; 91899178b03SGreg Kroah-Hartman 9194c62785eSGeliang Tang list_for_each_entry(n, list, n_node) { 920ae1b4171SGreg Kroah-Hartman dev_prv = to_device_private_bus(n); 921ae1b4171SGreg Kroah-Hartman b = dev_prv->device; 92299178b03SGreg Kroah-Hartman if (compare(a, b) <= 0) { 923ae1b4171SGreg Kroah-Hartman list_move_tail(&a->p->knode_bus.n_node, 924ae1b4171SGreg Kroah-Hartman &b->p->knode_bus.n_node); 92599178b03SGreg Kroah-Hartman return; 92699178b03SGreg Kroah-Hartman } 92799178b03SGreg Kroah-Hartman } 928ae1b4171SGreg Kroah-Hartman list_move_tail(&a->p->knode_bus.n_node, list); 92999178b03SGreg Kroah-Hartman } 93099178b03SGreg Kroah-Hartman 93199178b03SGreg Kroah-Hartman void bus_sort_breadthfirst(struct bus_type *bus, 93299178b03SGreg Kroah-Hartman int (*compare)(const struct device *a, 93399178b03SGreg Kroah-Hartman const struct device *b)) 93499178b03SGreg Kroah-Hartman { 93599178b03SGreg Kroah-Hartman LIST_HEAD(sorted_devices); 9364c62785eSGeliang Tang struct klist_node *n, *tmp; 937ae1b4171SGreg Kroah-Hartman struct device_private *dev_prv; 93899178b03SGreg Kroah-Hartman struct device *dev; 93999178b03SGreg Kroah-Hartman struct klist *device_klist; 94099178b03SGreg Kroah-Hartman 94199178b03SGreg Kroah-Hartman device_klist = bus_get_device_klist(bus); 94299178b03SGreg Kroah-Hartman 94399178b03SGreg Kroah-Hartman spin_lock(&device_klist->k_lock); 9444c62785eSGeliang Tang list_for_each_entry_safe(n, tmp, &device_klist->k_list, n_node) { 945ae1b4171SGreg Kroah-Hartman dev_prv = to_device_private_bus(n); 946ae1b4171SGreg Kroah-Hartman dev = dev_prv->device; 94799178b03SGreg Kroah-Hartman device_insertion_sort_klist(dev, &sorted_devices, compare); 94899178b03SGreg Kroah-Hartman } 94999178b03SGreg Kroah-Hartman list_splice(&sorted_devices, &device_klist->k_list); 95099178b03SGreg Kroah-Hartman spin_unlock(&device_klist->k_lock); 95199178b03SGreg Kroah-Hartman } 95299178b03SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_sort_breadthfirst); 95399178b03SGreg Kroah-Hartman 954ca22e56dSKay Sievers /** 955ca22e56dSKay Sievers * subsys_dev_iter_init - initialize subsys device iterator 956ca22e56dSKay Sievers * @iter: subsys iterator to initialize 957ca22e56dSKay Sievers * @subsys: the subsys we wanna iterate over 958ca22e56dSKay Sievers * @start: the device to start iterating from, if any 959ca22e56dSKay Sievers * @type: device_type of the devices to iterate over, NULL for all 960ca22e56dSKay Sievers * 961ca22e56dSKay Sievers * Initialize subsys iterator @iter such that it iterates over devices 962ca22e56dSKay Sievers * of @subsys. If @start is set, the list iteration will start there, 963ca22e56dSKay Sievers * otherwise if it is NULL, the iteration starts at the beginning of 964ca22e56dSKay Sievers * the list. 965ca22e56dSKay Sievers */ 9667cd9c9bbSGreg Kroah-Hartman void subsys_dev_iter_init(struct subsys_dev_iter *iter, struct bus_type *subsys, 967ca22e56dSKay Sievers struct device *start, const struct device_type *type) 968ca22e56dSKay Sievers { 969ca22e56dSKay Sievers struct klist_node *start_knode = NULL; 970ca22e56dSKay Sievers 971ca22e56dSKay Sievers if (start) 972ca22e56dSKay Sievers start_knode = &start->p->knode_bus; 9737cd9c9bbSGreg Kroah-Hartman klist_iter_init_node(&subsys->p->klist_devices, &iter->ki, start_knode); 974ca22e56dSKay Sievers iter->type = type; 975ca22e56dSKay Sievers } 976ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_dev_iter_init); 977ca22e56dSKay Sievers 978ca22e56dSKay Sievers /** 979ca22e56dSKay Sievers * subsys_dev_iter_next - iterate to the next device 980ca22e56dSKay Sievers * @iter: subsys iterator to proceed 981ca22e56dSKay Sievers * 982ca22e56dSKay Sievers * Proceed @iter to the next device and return it. Returns NULL if 983ca22e56dSKay Sievers * iteration is complete. 984ca22e56dSKay Sievers * 985ca22e56dSKay Sievers * The returned device is referenced and won't be released till 986ca22e56dSKay Sievers * iterator is proceed to the next device or exited. The caller is 987ca22e56dSKay Sievers * free to do whatever it wants to do with the device including 988ca22e56dSKay Sievers * calling back into subsys code. 989ca22e56dSKay Sievers */ 990ca22e56dSKay Sievers struct device *subsys_dev_iter_next(struct subsys_dev_iter *iter) 991ca22e56dSKay Sievers { 992ca22e56dSKay Sievers struct klist_node *knode; 993ca22e56dSKay Sievers struct device *dev; 994ca22e56dSKay Sievers 995ca22e56dSKay Sievers for (;;) { 996ca22e56dSKay Sievers knode = klist_next(&iter->ki); 997ca22e56dSKay Sievers if (!knode) 998ca22e56dSKay Sievers return NULL; 999371fd7a2SGeliang Tang dev = to_device_private_bus(knode)->device; 1000ca22e56dSKay Sievers if (!iter->type || iter->type == dev->type) 1001ca22e56dSKay Sievers return dev; 1002ca22e56dSKay Sievers } 1003ca22e56dSKay Sievers } 1004ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_dev_iter_next); 1005ca22e56dSKay Sievers 1006ca22e56dSKay Sievers /** 1007ca22e56dSKay Sievers * subsys_dev_iter_exit - finish iteration 1008ca22e56dSKay Sievers * @iter: subsys iterator to finish 1009ca22e56dSKay Sievers * 1010ca22e56dSKay Sievers * Finish an iteration. Always call this function after iteration is 1011ca22e56dSKay Sievers * complete whether the iteration ran till the end or not. 1012ca22e56dSKay Sievers */ 1013ca22e56dSKay Sievers void subsys_dev_iter_exit(struct subsys_dev_iter *iter) 1014ca22e56dSKay Sievers { 1015ca22e56dSKay Sievers klist_iter_exit(&iter->ki); 1016ca22e56dSKay Sievers } 1017ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_dev_iter_exit); 1018ca22e56dSKay Sievers 1019ca22e56dSKay Sievers int subsys_interface_register(struct subsys_interface *sif) 1020ca22e56dSKay Sievers { 1021ca22e56dSKay Sievers struct bus_type *subsys; 1022ca22e56dSKay Sievers struct subsys_dev_iter iter; 1023ca22e56dSKay Sievers struct device *dev; 1024ca22e56dSKay Sievers 1025ca22e56dSKay Sievers if (!sif || !sif->subsys) 1026ca22e56dSKay Sievers return -ENODEV; 1027ca22e56dSKay Sievers 1028ca22e56dSKay Sievers subsys = bus_get(sif->subsys); 1029ca22e56dSKay Sievers if (!subsys) 1030ca22e56dSKay Sievers return -EINVAL; 1031ca22e56dSKay Sievers 1032ca22e56dSKay Sievers mutex_lock(&subsys->p->mutex); 1033ca22e56dSKay Sievers list_add_tail(&sif->node, &subsys->p->interfaces); 1034ca22e56dSKay Sievers if (sif->add_dev) { 1035ca22e56dSKay Sievers subsys_dev_iter_init(&iter, subsys, NULL, NULL); 1036ca22e56dSKay Sievers while ((dev = subsys_dev_iter_next(&iter))) 1037ca22e56dSKay Sievers sif->add_dev(dev, sif); 1038ca22e56dSKay Sievers subsys_dev_iter_exit(&iter); 1039ca22e56dSKay Sievers } 1040ca22e56dSKay Sievers mutex_unlock(&subsys->p->mutex); 1041ca22e56dSKay Sievers 1042ca22e56dSKay Sievers return 0; 1043ca22e56dSKay Sievers } 1044ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_interface_register); 1045ca22e56dSKay Sievers 1046ca22e56dSKay Sievers void subsys_interface_unregister(struct subsys_interface *sif) 1047ca22e56dSKay Sievers { 10482b31594aSJonghwan Choi struct bus_type *subsys; 1049ca22e56dSKay Sievers struct subsys_dev_iter iter; 1050ca22e56dSKay Sievers struct device *dev; 1051ca22e56dSKay Sievers 10522b31594aSJonghwan Choi if (!sif || !sif->subsys) 1053ca22e56dSKay Sievers return; 1054ca22e56dSKay Sievers 10552b31594aSJonghwan Choi subsys = sif->subsys; 10562b31594aSJonghwan Choi 1057ca22e56dSKay Sievers mutex_lock(&subsys->p->mutex); 1058ca22e56dSKay Sievers list_del_init(&sif->node); 1059ca22e56dSKay Sievers if (sif->remove_dev) { 1060ca22e56dSKay Sievers subsys_dev_iter_init(&iter, subsys, NULL, NULL); 1061ca22e56dSKay Sievers while ((dev = subsys_dev_iter_next(&iter))) 1062ca22e56dSKay Sievers sif->remove_dev(dev, sif); 1063ca22e56dSKay Sievers subsys_dev_iter_exit(&iter); 1064ca22e56dSKay Sievers } 1065ca22e56dSKay Sievers mutex_unlock(&subsys->p->mutex); 1066ca22e56dSKay Sievers 1067ca22e56dSKay Sievers bus_put(subsys); 1068ca22e56dSKay Sievers } 1069ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_interface_unregister); 1070ca22e56dSKay Sievers 1071ca22e56dSKay Sievers static void system_root_device_release(struct device *dev) 1072ca22e56dSKay Sievers { 1073ca22e56dSKay Sievers kfree(dev); 1074ca22e56dSKay Sievers } 1075d73ce004STejun Heo 1076d73ce004STejun Heo static int subsys_register(struct bus_type *subsys, 1077d73ce004STejun Heo const struct attribute_group **groups, 1078d73ce004STejun Heo struct kobject *parent_of_root) 1079d73ce004STejun Heo { 1080d73ce004STejun Heo struct device *dev; 1081d73ce004STejun Heo int err; 1082d73ce004STejun Heo 1083d73ce004STejun Heo err = bus_register(subsys); 1084d73ce004STejun Heo if (err < 0) 1085d73ce004STejun Heo return err; 1086d73ce004STejun Heo 1087d73ce004STejun Heo dev = kzalloc(sizeof(struct device), GFP_KERNEL); 1088d73ce004STejun Heo if (!dev) { 1089d73ce004STejun Heo err = -ENOMEM; 1090d73ce004STejun Heo goto err_dev; 1091d73ce004STejun Heo } 1092d73ce004STejun Heo 1093d73ce004STejun Heo err = dev_set_name(dev, "%s", subsys->name); 1094d73ce004STejun Heo if (err < 0) 1095d73ce004STejun Heo goto err_name; 1096d73ce004STejun Heo 1097d73ce004STejun Heo dev->kobj.parent = parent_of_root; 1098d73ce004STejun Heo dev->groups = groups; 1099d73ce004STejun Heo dev->release = system_root_device_release; 1100d73ce004STejun Heo 1101d73ce004STejun Heo err = device_register(dev); 1102d73ce004STejun Heo if (err < 0) 1103d73ce004STejun Heo goto err_dev_reg; 1104d73ce004STejun Heo 1105d73ce004STejun Heo subsys->dev_root = dev; 1106d73ce004STejun Heo return 0; 1107d73ce004STejun Heo 1108d73ce004STejun Heo err_dev_reg: 1109d73ce004STejun Heo put_device(dev); 1110d73ce004STejun Heo dev = NULL; 1111d73ce004STejun Heo err_name: 1112d73ce004STejun Heo kfree(dev); 1113d73ce004STejun Heo err_dev: 1114d73ce004STejun Heo bus_unregister(subsys); 1115d73ce004STejun Heo return err; 1116d73ce004STejun Heo } 1117d73ce004STejun Heo 1118ca22e56dSKay Sievers /** 1119ca22e56dSKay Sievers * subsys_system_register - register a subsystem at /sys/devices/system/ 112078d79559SRandy Dunlap * @subsys: system subsystem 112178d79559SRandy Dunlap * @groups: default attributes for the root device 1122ca22e56dSKay Sievers * 1123ca22e56dSKay Sievers * All 'system' subsystems have a /sys/devices/system/<name> root device 1124ca22e56dSKay Sievers * with the name of the subsystem. The root device can carry subsystem- 1125ca22e56dSKay Sievers * wide attributes. All registered devices are below this single root 1126ca22e56dSKay Sievers * device and are named after the subsystem with a simple enumeration 1127e227867fSMasanari Iida * number appended. The registered devices are not explicitly named; 1128ca22e56dSKay Sievers * only 'id' in the device needs to be set. 1129ca22e56dSKay Sievers * 1130ca22e56dSKay Sievers * Do not use this interface for anything new, it exists for compatibility 1131ca22e56dSKay Sievers * with bad ideas only. New subsystems should use plain subsystems; and 1132ca22e56dSKay Sievers * add the subsystem-wide attributes should be added to the subsystem 1133ca22e56dSKay Sievers * directory itself and not some create fake root-device placed in 1134ca22e56dSKay Sievers * /sys/devices/system/<name>. 1135ca22e56dSKay Sievers */ 1136ca22e56dSKay Sievers int subsys_system_register(struct bus_type *subsys, 1137ca22e56dSKay Sievers const struct attribute_group **groups) 1138ca22e56dSKay Sievers { 1139d73ce004STejun Heo return subsys_register(subsys, groups, &system_kset->kobj); 1140ca22e56dSKay Sievers } 1141ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_system_register); 1142ca22e56dSKay Sievers 1143d73ce004STejun Heo /** 1144d73ce004STejun Heo * subsys_virtual_register - register a subsystem at /sys/devices/virtual/ 1145d73ce004STejun Heo * @subsys: virtual subsystem 1146d73ce004STejun Heo * @groups: default attributes for the root device 1147d73ce004STejun Heo * 1148d73ce004STejun Heo * All 'virtual' subsystems have a /sys/devices/system/<name> root device 1149d73ce004STejun Heo * with the name of the subystem. The root device can carry subsystem-wide 1150d73ce004STejun Heo * attributes. All registered devices are below this single root device. 1151d73ce004STejun Heo * There's no restriction on device naming. This is for kernel software 1152d73ce004STejun Heo * constructs which need sysfs interface. 1153d73ce004STejun Heo */ 1154d73ce004STejun Heo int subsys_virtual_register(struct bus_type *subsys, 1155d73ce004STejun Heo const struct attribute_group **groups) 1156d73ce004STejun Heo { 1157d73ce004STejun Heo struct kobject *virtual_dir; 1158d73ce004STejun Heo 1159d73ce004STejun Heo virtual_dir = virtual_device_parent(NULL); 1160d73ce004STejun Heo if (!virtual_dir) 1161d73ce004STejun Heo return -ENOMEM; 1162d73ce004STejun Heo 1163d73ce004STejun Heo return subsys_register(subsys, groups, virtual_dir); 1164d73ce004STejun Heo } 11651c04fc35SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(subsys_virtual_register); 1166d73ce004STejun Heo 11671da177e4SLinus Torvalds int __init buses_init(void) 11681da177e4SLinus Torvalds { 116959a54833SGreg Kroah-Hartman bus_kset = kset_create_and_add("bus", &bus_uevent_ops, NULL); 117059a54833SGreg Kroah-Hartman if (!bus_kset) 117159a54833SGreg Kroah-Hartman return -ENOMEM; 1172ca22e56dSKay Sievers 1173ca22e56dSKay Sievers system_kset = kset_create_and_add("system", NULL, &devices_kset->kobj); 1174ca22e56dSKay Sievers if (!system_kset) 1175ca22e56dSKay Sievers return -ENOMEM; 1176ca22e56dSKay Sievers 117759a54833SGreg Kroah-Hartman return 0; 11781da177e4SLinus Torvalds } 1179