^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * bus.c - bus driver management
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (c) 2002-3 Patrick Mochel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (c) 2002-3 Open Source Development Labs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Copyright (c) 2007 Greg Kroah-Hartman <gregkh@suse.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Copyright (c) 2007 Novell Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/async.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/device/bus.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include "base.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include "power/power.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) /* /sys/devices/system */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) static struct kset *system_kset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #define to_bus_attr(_attr) container_of(_attr, struct bus_attribute, attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * sysfs bindings for drivers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #define to_drv_attr(_attr) container_of(_attr, struct driver_attribute, attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #define DRIVER_ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) struct driver_attribute driver_attr_##_name = \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) __ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) static int __must_check bus_rescan_devices_helper(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) void *data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) static struct bus_type *bus_get(struct bus_type *bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) if (bus) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) kset_get(&bus->p->subsys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) return bus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) static void bus_put(struct bus_type *bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) if (bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) kset_put(&bus->p->subsys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) static ssize_t drv_attr_show(struct kobject *kobj, struct attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) struct driver_attribute *drv_attr = to_drv_attr(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) struct driver_private *drv_priv = to_driver(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) ssize_t ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) if (drv_attr->show)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) ret = drv_attr->show(drv_priv->driver, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) static ssize_t drv_attr_store(struct kobject *kobj, struct attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) struct driver_attribute *drv_attr = to_drv_attr(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) struct driver_private *drv_priv = to_driver(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) ssize_t ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) if (drv_attr->store)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) ret = drv_attr->store(drv_priv->driver, buf, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) static const struct sysfs_ops driver_sysfs_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) .show = drv_attr_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) .store = drv_attr_store,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) static void driver_release(struct kobject *kobj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) struct driver_private *drv_priv = to_driver(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) pr_debug("driver: '%s': %s\n", kobject_name(kobj), __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) kfree(drv_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) static struct kobj_type driver_ktype = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) .sysfs_ops = &driver_sysfs_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) .release = driver_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * sysfs bindings for buses
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) static ssize_t bus_attr_show(struct kobject *kobj, struct attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) struct bus_attribute *bus_attr = to_bus_attr(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) struct subsys_private *subsys_priv = to_subsys_private(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) ssize_t ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) if (bus_attr->show)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) ret = bus_attr->show(subsys_priv->bus, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) static ssize_t bus_attr_store(struct kobject *kobj, struct attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) struct bus_attribute *bus_attr = to_bus_attr(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) struct subsys_private *subsys_priv = to_subsys_private(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) ssize_t ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) if (bus_attr->store)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) ret = bus_attr->store(subsys_priv->bus, buf, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) static const struct sysfs_ops bus_sysfs_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) .show = bus_attr_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) .store = bus_attr_store,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) int bus_create_file(struct bus_type *bus, struct bus_attribute *attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) if (bus_get(bus)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) error = sysfs_create_file(&bus->p->subsys.kobj, &attr->attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) bus_put(bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) error = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) EXPORT_SYMBOL_GPL(bus_create_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) void bus_remove_file(struct bus_type *bus, struct bus_attribute *attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) if (bus_get(bus)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) sysfs_remove_file(&bus->p->subsys.kobj, &attr->attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) bus_put(bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) EXPORT_SYMBOL_GPL(bus_remove_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) static void bus_release(struct kobject *kobj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) struct subsys_private *priv = to_subsys_private(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) struct bus_type *bus = priv->bus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) kfree(priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) bus->p = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) static struct kobj_type bus_ktype = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) .sysfs_ops = &bus_sysfs_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) .release = bus_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) static int bus_uevent_filter(struct kset *kset, struct kobject *kobj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) struct kobj_type *ktype = get_ktype(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) if (ktype == &bus_ktype)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) static const struct kset_uevent_ops bus_uevent_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) .filter = bus_uevent_filter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) static struct kset *bus_kset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) /* Manually detach a device from its associated driver. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) static ssize_t unbind_store(struct device_driver *drv, const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) struct bus_type *bus = bus_get(drv->bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) int err = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) dev = bus_find_device_by_name(bus, NULL, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) if (dev && dev->driver == drv) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) device_driver_detach(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) err = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) put_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) bus_put(bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) static DRIVER_ATTR_IGNORE_LOCKDEP(unbind, S_IWUSR, NULL, unbind_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) * Manually attach a device to a driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) * Note: the driver must want to bind to the device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) * it is not possible to override the driver's id table.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) static ssize_t bind_store(struct device_driver *drv, const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) struct bus_type *bus = bus_get(drv->bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) int err = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) dev = bus_find_device_by_name(bus, NULL, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) if (dev && dev->driver == NULL && driver_match_device(drv, dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) err = device_driver_attach(drv, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) if (err > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) /* success */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) err = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) } else if (err == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) /* driver didn't accept device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) err = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) put_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) bus_put(bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) static DRIVER_ATTR_IGNORE_LOCKDEP(bind, S_IWUSR, NULL, bind_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) static ssize_t drivers_autoprobe_show(struct bus_type *bus, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) return sysfs_emit(buf, "%d\n", bus->p->drivers_autoprobe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) static ssize_t drivers_autoprobe_store(struct bus_type *bus,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) if (buf[0] == '0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) bus->p->drivers_autoprobe = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) bus->p->drivers_autoprobe = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) static ssize_t drivers_probe_store(struct bus_type *bus,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) int err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) dev = bus_find_device_by_name(bus, NULL, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) if (bus_rescan_devices_helper(dev, NULL) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) err = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) put_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) static struct device *next_device(struct klist_iter *i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) struct klist_node *n = klist_next(i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) struct device *dev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) struct device_private *dev_prv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) if (n) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) dev_prv = to_device_private_bus(n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) dev = dev_prv->device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) return dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) * bus_for_each_dev - device iterator.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) * @bus: bus type.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) * @start: device to start iterating from.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) * @data: data for the callback.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) * @fn: function to be called for each device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) * Iterate over @bus's list of devices, and call @fn for each,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) * passing it @data. If @start is not NULL, we use that device to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) * begin iterating from.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) * We check the return of @fn each time. If it returns anything
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) * other than 0, we break out and return that value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) * NOTE: The device that returns a non-zero value is not retained
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) * in any way, nor is its refcount incremented. If the caller needs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) * to retain this data, it should do so, and increment the reference
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) * count in the supplied callback.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) int bus_for_each_dev(struct bus_type *bus, struct device *start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) void *data, int (*fn)(struct device *, void *))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) struct klist_iter i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) int error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) if (!bus || !bus->p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) klist_iter_init_node(&bus->p->klist_devices, &i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) (start ? &start->p->knode_bus : NULL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) while (!error && (dev = next_device(&i)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) error = fn(dev, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) klist_iter_exit(&i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) EXPORT_SYMBOL_GPL(bus_for_each_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) * bus_find_device - device iterator for locating a particular device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) * @bus: bus type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) * @start: Device to begin with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) * @data: Data to pass to match function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) * @match: Callback function to check device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) * This is similar to the bus_for_each_dev() function above, but it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) * returns a reference to a device that is 'found' for later use, as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) * determined by the @match callback.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) * The callback should return 0 if the device doesn't match and non-zero
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) * if it does. If the callback returns non-zero, this function will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) * return to the caller and not iterate over any more devices.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) struct device *bus_find_device(struct bus_type *bus,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) struct device *start, const void *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) int (*match)(struct device *dev, const void *data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) struct klist_iter i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) if (!bus || !bus->p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) klist_iter_init_node(&bus->p->klist_devices, &i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) (start ? &start->p->knode_bus : NULL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) while ((dev = next_device(&i)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) if (match(dev, data) && get_device(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) klist_iter_exit(&i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) return dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) EXPORT_SYMBOL_GPL(bus_find_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) * subsys_find_device_by_id - find a device with a specific enumeration number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) * @subsys: subsystem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) * @id: index 'id' in struct device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) * @hint: device to check first
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) * Check the hint's next object and if it is a match return it directly,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) * otherwise, fall back to a full list search. Either way a reference for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) * the returned object is taken.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) struct device *subsys_find_device_by_id(struct bus_type *subsys, unsigned int id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) struct device *hint)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) struct klist_iter i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) if (!subsys)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) if (hint) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) klist_iter_init_node(&subsys->p->klist_devices, &i, &hint->p->knode_bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) dev = next_device(&i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) if (dev && dev->id == id && get_device(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) klist_iter_exit(&i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) return dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) klist_iter_exit(&i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) klist_iter_init_node(&subsys->p->klist_devices, &i, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) while ((dev = next_device(&i))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) if (dev->id == id && get_device(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) klist_iter_exit(&i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) return dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) klist_iter_exit(&i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) EXPORT_SYMBOL_GPL(subsys_find_device_by_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) static struct device_driver *next_driver(struct klist_iter *i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) struct klist_node *n = klist_next(i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) struct driver_private *drv_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) if (n) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) drv_priv = container_of(n, struct driver_private, knode_bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) return drv_priv->driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) * bus_for_each_drv - driver iterator
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) * @bus: bus we're dealing with.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) * @start: driver to start iterating on.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) * @data: data to pass to the callback.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) * @fn: function to call for each driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) * This is nearly identical to the device iterator above.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) * We iterate over each driver that belongs to @bus, and call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) * @fn for each. If @fn returns anything but 0, we break out
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) * and return it. If @start is not NULL, we use it as the head
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) * of the list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) * NOTE: we don't return the driver that returns a non-zero
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) * value, nor do we leave the reference count incremented for that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) * driver. If the caller needs to know that info, it must set it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) * in the callback. It must also be sure to increment the refcount
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) * so it doesn't disappear before returning to the caller.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) int bus_for_each_drv(struct bus_type *bus, struct device_driver *start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) void *data, int (*fn)(struct device_driver *, void *))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) struct klist_iter i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) struct device_driver *drv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) int error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) if (!bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) klist_iter_init_node(&bus->p->klist_drivers, &i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) start ? &start->p->knode_bus : NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) while ((drv = next_driver(&i)) && !error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) error = fn(drv, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) klist_iter_exit(&i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) EXPORT_SYMBOL_GPL(bus_for_each_drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) * bus_add_device - add device to bus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) * @dev: device being added
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) * - Add device's bus attributes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) * - Create links to device's bus.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) * - Add the device to its bus's list of devices.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) int bus_add_device(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) struct bus_type *bus = bus_get(dev->bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) int error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) if (bus) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) pr_debug("bus: '%s': add device %s\n", bus->name, dev_name(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) error = device_add_groups(dev, bus->dev_groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) goto out_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) error = sysfs_create_link(&bus->p->devices_kset->kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) &dev->kobj, dev_name(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) goto out_groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) error = sysfs_create_link(&dev->kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) &dev->bus->p->subsys.kobj, "subsystem");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) goto out_subsys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) klist_add_tail(&dev->p->knode_bus, &bus->p->klist_devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) out_subsys:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) sysfs_remove_link(&bus->p->devices_kset->kobj, dev_name(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) out_groups:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) device_remove_groups(dev, bus->dev_groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) out_put:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) bus_put(dev->bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) * bus_probe_device - probe drivers for a new device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) * @dev: device to probe
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) * - Automatically probe for a driver if the bus allows it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) void bus_probe_device(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) struct bus_type *bus = dev->bus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) struct subsys_interface *sif;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) if (!bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) if (bus->p->drivers_autoprobe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) device_initial_probe(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) mutex_lock(&bus->p->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) list_for_each_entry(sif, &bus->p->interfaces, node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) if (sif->add_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) sif->add_dev(dev, sif);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) mutex_unlock(&bus->p->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) * bus_remove_device - remove device from bus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) * @dev: device to be removed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) * - Remove device from all interfaces.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) * - Remove symlink from bus' directory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) * - Delete device from bus's list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) * - Detach from its driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) * - Drop reference taken in bus_add_device().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) void bus_remove_device(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) struct bus_type *bus = dev->bus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) struct subsys_interface *sif;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) if (!bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) mutex_lock(&bus->p->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) list_for_each_entry(sif, &bus->p->interfaces, node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) if (sif->remove_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) sif->remove_dev(dev, sif);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) mutex_unlock(&bus->p->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) sysfs_remove_link(&dev->kobj, "subsystem");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) sysfs_remove_link(&dev->bus->p->devices_kset->kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) dev_name(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) device_remove_groups(dev, dev->bus->dev_groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) if (klist_node_attached(&dev->p->knode_bus))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) klist_del(&dev->p->knode_bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) pr_debug("bus: '%s': remove device %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) dev->bus->name, dev_name(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) device_release_driver(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) bus_put(dev->bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) static int __must_check add_bind_files(struct device_driver *drv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) ret = driver_create_file(drv, &driver_attr_unbind);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) if (ret == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) ret = driver_create_file(drv, &driver_attr_bind);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) driver_remove_file(drv, &driver_attr_unbind);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) static void remove_bind_files(struct device_driver *drv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) driver_remove_file(drv, &driver_attr_bind);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) driver_remove_file(drv, &driver_attr_unbind);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) static BUS_ATTR_WO(drivers_probe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) static BUS_ATTR_RW(drivers_autoprobe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) static int add_probe_files(struct bus_type *bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) retval = bus_create_file(bus, &bus_attr_drivers_probe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) retval = bus_create_file(bus, &bus_attr_drivers_autoprobe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) bus_remove_file(bus, &bus_attr_drivers_probe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) static void remove_probe_files(struct bus_type *bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) bus_remove_file(bus, &bus_attr_drivers_autoprobe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) bus_remove_file(bus, &bus_attr_drivers_probe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) static ssize_t uevent_store(struct device_driver *drv, const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) rc = kobject_synth_uevent(&drv->p->kobj, buf, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) return rc ? rc : count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) static DRIVER_ATTR_WO(uevent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) * bus_add_driver - Add a driver to the bus.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) * @drv: driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) int bus_add_driver(struct device_driver *drv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) struct bus_type *bus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) struct driver_private *priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) int error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) bus = bus_get(drv->bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) if (!bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) pr_debug("bus: '%s': add driver %s\n", bus->name, drv->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) priv = kzalloc(sizeof(*priv), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) if (!priv) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) error = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) goto out_put_bus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) klist_init(&priv->klist_devices, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) priv->driver = drv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) drv->p = priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) priv->kobj.kset = bus->p->drivers_kset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) error = kobject_init_and_add(&priv->kobj, &driver_ktype, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) "%s", drv->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) goto out_unregister;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) klist_add_tail(&priv->knode_bus, &bus->p->klist_drivers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) if (drv->bus->p->drivers_autoprobe) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) error = driver_attach(drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) goto out_unregister;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) module_add_driver(drv->owner, drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) error = driver_create_file(drv, &driver_attr_uevent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) printk(KERN_ERR "%s: uevent attr (%s) failed\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) __func__, drv->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) error = driver_add_groups(drv, bus->drv_groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) /* How the hell do we get out of this pickle? Give up */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) printk(KERN_ERR "%s: driver_create_groups(%s) failed\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) __func__, drv->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) if (!drv->suppress_bind_attrs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) error = add_bind_files(drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) /* Ditto */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) printk(KERN_ERR "%s: add_bind_files(%s) failed\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) __func__, drv->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) out_unregister:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) kobject_put(&priv->kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) /* drv->p is freed in driver_release() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) drv->p = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) out_put_bus:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) bus_put(bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) * bus_remove_driver - delete driver from bus's knowledge.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) * @drv: driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) * Detach the driver from the devices it controls, and remove
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) * it from its bus's list of drivers. Finally, we drop the reference
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) * to the bus we took in bus_add_driver().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) void bus_remove_driver(struct device_driver *drv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) if (!drv->bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) if (!drv->suppress_bind_attrs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) remove_bind_files(drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) driver_remove_groups(drv, drv->bus->drv_groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) driver_remove_file(drv, &driver_attr_uevent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) klist_remove(&drv->p->knode_bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) pr_debug("bus: '%s': remove driver %s\n", drv->bus->name, drv->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) driver_detach(drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) module_remove_driver(drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) kobject_put(&drv->p->kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) bus_put(drv->bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) /* Helper for bus_rescan_devices's iter */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) static int __must_check bus_rescan_devices_helper(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) if (!dev->driver) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) if (dev->parent && dev->bus->need_parent_lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) device_lock(dev->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) ret = device_attach(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) if (dev->parent && dev->bus->need_parent_lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) device_unlock(dev->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) return ret < 0 ? ret : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) * bus_rescan_devices - rescan devices on the bus for possible drivers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) * @bus: the bus to scan.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) * This function will look for devices on the bus with no driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) * attached and rescan it against existing drivers to see if it matches
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) * any by calling device_attach() for the unbound devices.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) int bus_rescan_devices(struct bus_type *bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) return bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) EXPORT_SYMBOL_GPL(bus_rescan_devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) * device_reprobe - remove driver for a device and probe for a new driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) * @dev: the device to reprobe
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) * This function detaches the attached driver (if any) for the given
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) * device and restarts the driver probing process. It is intended
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) * to use if probing criteria changed during a devices lifetime and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) * driver attachment should change accordingly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) int device_reprobe(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) if (dev->driver)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) device_driver_detach(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) return bus_rescan_devices_helper(dev, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) EXPORT_SYMBOL_GPL(device_reprobe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) * find_bus - locate bus by name.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) * @name: name of bus.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) * Call kset_find_obj() to iterate over list of buses to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) * find a bus by name. Return bus if found.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) * Note that kset_find_obj increments bus' reference count.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) #if 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) struct bus_type *find_bus(char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) struct kobject *k = kset_find_obj(bus_kset, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) return k ? to_bus(k) : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) #endif /* 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) static int bus_add_groups(struct bus_type *bus,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) const struct attribute_group **groups)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) return sysfs_create_groups(&bus->p->subsys.kobj, groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) static void bus_remove_groups(struct bus_type *bus,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) const struct attribute_group **groups)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) sysfs_remove_groups(&bus->p->subsys.kobj, groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) static void klist_devices_get(struct klist_node *n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) struct device_private *dev_prv = to_device_private_bus(n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) struct device *dev = dev_prv->device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) get_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) static void klist_devices_put(struct klist_node *n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) struct device_private *dev_prv = to_device_private_bus(n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) struct device *dev = dev_prv->device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) put_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) static ssize_t bus_uevent_store(struct bus_type *bus,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) rc = kobject_synth_uevent(&bus->p->subsys.kobj, buf, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) return rc ? rc : count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) * "open code" the old BUS_ATTR() macro here. We want to use BUS_ATTR_WO()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) * here, but can not use it as earlier in the file we have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) * DEVICE_ATTR_WO(uevent), which would cause a clash with the with the store
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) * function name.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) static struct bus_attribute bus_attr_uevent = __ATTR(uevent, S_IWUSR, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) bus_uevent_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) * bus_register - register a driver-core subsystem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) * @bus: bus to register
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) * Once we have that, we register the bus with the kobject
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) * infrastructure, then register the children subsystems it has:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) * the devices and drivers that belong to the subsystem.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) int bus_register(struct bus_type *bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) struct subsys_private *priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) struct lock_class_key *key = &bus->lock_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) priv = kzalloc(sizeof(struct subsys_private), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) if (!priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) priv->bus = bus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) bus->p = priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) BLOCKING_INIT_NOTIFIER_HEAD(&priv->bus_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) retval = kobject_set_name(&priv->subsys.kobj, "%s", bus->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) priv->subsys.kobj.kset = bus_kset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) priv->subsys.kobj.ktype = &bus_ktype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) priv->drivers_autoprobe = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) retval = kset_register(&priv->subsys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) retval = bus_create_file(bus, &bus_attr_uevent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) goto bus_uevent_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) priv->devices_kset = kset_create_and_add("devices", NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) &priv->subsys.kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) if (!priv->devices_kset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) retval = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) goto bus_devices_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) priv->drivers_kset = kset_create_and_add("drivers", NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) &priv->subsys.kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) if (!priv->drivers_kset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) retval = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) goto bus_drivers_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) INIT_LIST_HEAD(&priv->interfaces);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) __mutex_init(&priv->mutex, "subsys mutex", key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) klist_init(&priv->klist_devices, klist_devices_get, klist_devices_put);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) klist_init(&priv->klist_drivers, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) retval = add_probe_files(bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) goto bus_probe_files_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) retval = bus_add_groups(bus, bus->bus_groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) goto bus_groups_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) pr_debug("bus: '%s': registered\n", bus->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) bus_groups_fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) remove_probe_files(bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) bus_probe_files_fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) kset_unregister(bus->p->drivers_kset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) bus_drivers_fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) kset_unregister(bus->p->devices_kset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) bus_devices_fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) bus_remove_file(bus, &bus_attr_uevent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) bus_uevent_fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) kset_unregister(&bus->p->subsys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) kfree(bus->p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) bus->p = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) EXPORT_SYMBOL_GPL(bus_register);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) * bus_unregister - remove a bus from the system
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) * @bus: bus.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) * Unregister the child subsystems and the bus itself.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) * Finally, we call bus_put() to release the refcount
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) void bus_unregister(struct bus_type *bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) pr_debug("bus: '%s': unregistering\n", bus->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) if (bus->dev_root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) device_unregister(bus->dev_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) bus_remove_groups(bus, bus->bus_groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) remove_probe_files(bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) kset_unregister(bus->p->drivers_kset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) kset_unregister(bus->p->devices_kset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) bus_remove_file(bus, &bus_attr_uevent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) kset_unregister(&bus->p->subsys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) EXPORT_SYMBOL_GPL(bus_unregister);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) int bus_register_notifier(struct bus_type *bus, struct notifier_block *nb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) return blocking_notifier_chain_register(&bus->p->bus_notifier, nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) EXPORT_SYMBOL_GPL(bus_register_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) int bus_unregister_notifier(struct bus_type *bus, struct notifier_block *nb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) return blocking_notifier_chain_unregister(&bus->p->bus_notifier, nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) EXPORT_SYMBOL_GPL(bus_unregister_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) struct kset *bus_get_kset(struct bus_type *bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) return &bus->p->subsys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) EXPORT_SYMBOL_GPL(bus_get_kset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) struct klist *bus_get_device_klist(struct bus_type *bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) return &bus->p->klist_devices;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) EXPORT_SYMBOL_GPL(bus_get_device_klist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) * Yes, this forcibly breaks the klist abstraction temporarily. It
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) * just wants to sort the klist, not change reference counts and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) * take/drop locks rapidly in the process. It does all this while
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) * holding the lock for the list, so objects can't otherwise be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) * added/removed while we're swizzling.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) static void device_insertion_sort_klist(struct device *a, struct list_head *list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) int (*compare)(const struct device *a,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) const struct device *b))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) struct klist_node *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) struct device_private *dev_prv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) struct device *b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) list_for_each_entry(n, list, n_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) dev_prv = to_device_private_bus(n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) b = dev_prv->device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) if (compare(a, b) <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) list_move_tail(&a->p->knode_bus.n_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) &b->p->knode_bus.n_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) list_move_tail(&a->p->knode_bus.n_node, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) void bus_sort_breadthfirst(struct bus_type *bus,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) int (*compare)(const struct device *a,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) const struct device *b))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) LIST_HEAD(sorted_devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) struct klist_node *n, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) struct device_private *dev_prv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) struct klist *device_klist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) device_klist = bus_get_device_klist(bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) spin_lock(&device_klist->k_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) list_for_each_entry_safe(n, tmp, &device_klist->k_list, n_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) dev_prv = to_device_private_bus(n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) dev = dev_prv->device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) device_insertion_sort_klist(dev, &sorted_devices, compare);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) list_splice(&sorted_devices, &device_klist->k_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) spin_unlock(&device_klist->k_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) EXPORT_SYMBOL_GPL(bus_sort_breadthfirst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) * subsys_dev_iter_init - initialize subsys device iterator
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) * @iter: subsys iterator to initialize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) * @subsys: the subsys we wanna iterate over
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) * @start: the device to start iterating from, if any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) * @type: device_type of the devices to iterate over, NULL for all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) * Initialize subsys iterator @iter such that it iterates over devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) * of @subsys. If @start is set, the list iteration will start there,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) * otherwise if it is NULL, the iteration starts at the beginning of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) * the list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) void subsys_dev_iter_init(struct subsys_dev_iter *iter, struct bus_type *subsys,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) struct device *start, const struct device_type *type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) struct klist_node *start_knode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) if (start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) start_knode = &start->p->knode_bus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) klist_iter_init_node(&subsys->p->klist_devices, &iter->ki, start_knode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) iter->type = type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) EXPORT_SYMBOL_GPL(subsys_dev_iter_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) * subsys_dev_iter_next - iterate to the next device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) * @iter: subsys iterator to proceed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) * Proceed @iter to the next device and return it. Returns NULL if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) * iteration is complete.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) * The returned device is referenced and won't be released till
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) * iterator is proceed to the next device or exited. The caller is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) * free to do whatever it wants to do with the device including
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) * calling back into subsys code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) struct device *subsys_dev_iter_next(struct subsys_dev_iter *iter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) struct klist_node *knode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) knode = klist_next(&iter->ki);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) if (!knode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) dev = to_device_private_bus(knode)->device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) if (!iter->type || iter->type == dev->type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) return dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) EXPORT_SYMBOL_GPL(subsys_dev_iter_next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) * subsys_dev_iter_exit - finish iteration
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) * @iter: subsys iterator to finish
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) * Finish an iteration. Always call this function after iteration is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) * complete whether the iteration ran till the end or not.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) void subsys_dev_iter_exit(struct subsys_dev_iter *iter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) klist_iter_exit(&iter->ki);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) EXPORT_SYMBOL_GPL(subsys_dev_iter_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) int subsys_interface_register(struct subsys_interface *sif)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) struct bus_type *subsys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) struct subsys_dev_iter iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) if (!sif || !sif->subsys)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) subsys = bus_get(sif->subsys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) if (!subsys)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) mutex_lock(&subsys->p->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) list_add_tail(&sif->node, &subsys->p->interfaces);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) if (sif->add_dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) subsys_dev_iter_init(&iter, subsys, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) while ((dev = subsys_dev_iter_next(&iter)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) sif->add_dev(dev, sif);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) subsys_dev_iter_exit(&iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) mutex_unlock(&subsys->p->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) EXPORT_SYMBOL_GPL(subsys_interface_register);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) void subsys_interface_unregister(struct subsys_interface *sif)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) struct bus_type *subsys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) struct subsys_dev_iter iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) if (!sif || !sif->subsys)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) subsys = sif->subsys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) mutex_lock(&subsys->p->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) list_del_init(&sif->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) if (sif->remove_dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) subsys_dev_iter_init(&iter, subsys, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) while ((dev = subsys_dev_iter_next(&iter)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) sif->remove_dev(dev, sif);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) subsys_dev_iter_exit(&iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) mutex_unlock(&subsys->p->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) bus_put(subsys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) EXPORT_SYMBOL_GPL(subsys_interface_unregister);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) static void system_root_device_release(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) kfree(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) static int subsys_register(struct bus_type *subsys,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) const struct attribute_group **groups,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) struct kobject *parent_of_root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) err = bus_register(subsys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) dev = kzalloc(sizeof(struct device), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) if (!dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) goto err_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) err = dev_set_name(dev, "%s", subsys->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) goto err_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) dev->kobj.parent = parent_of_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) dev->groups = groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) dev->release = system_root_device_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) err = device_register(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) goto err_dev_reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) subsys->dev_root = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) err_dev_reg:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) put_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) dev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) err_name:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) kfree(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) err_dev:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) bus_unregister(subsys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) * subsys_system_register - register a subsystem at /sys/devices/system/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) * @subsys: system subsystem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) * @groups: default attributes for the root device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) * All 'system' subsystems have a /sys/devices/system/<name> root device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) * with the name of the subsystem. The root device can carry subsystem-
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) * wide attributes. All registered devices are below this single root
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) * device and are named after the subsystem with a simple enumeration
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) * number appended. The registered devices are not explicitly named;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) * only 'id' in the device needs to be set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) * Do not use this interface for anything new, it exists for compatibility
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) * with bad ideas only. New subsystems should use plain subsystems; and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) * add the subsystem-wide attributes should be added to the subsystem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) * directory itself and not some create fake root-device placed in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) * /sys/devices/system/<name>.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) int subsys_system_register(struct bus_type *subsys,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) const struct attribute_group **groups)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) return subsys_register(subsys, groups, &system_kset->kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) EXPORT_SYMBOL_GPL(subsys_system_register);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) * subsys_virtual_register - register a subsystem at /sys/devices/virtual/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) * @subsys: virtual subsystem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) * @groups: default attributes for the root device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) * All 'virtual' subsystems have a /sys/devices/system/<name> root device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) * with the name of the subystem. The root device can carry subsystem-wide
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) * attributes. All registered devices are below this single root device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) * There's no restriction on device naming. This is for kernel software
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) * constructs which need sysfs interface.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) int subsys_virtual_register(struct bus_type *subsys,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) const struct attribute_group **groups)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) struct kobject *virtual_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) virtual_dir = virtual_device_parent(NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) if (!virtual_dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) return subsys_register(subsys, groups, virtual_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) EXPORT_SYMBOL_GPL(subsys_virtual_register);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) int __init buses_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) bus_kset = kset_create_and_add("bus", &bus_uevent_ops, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) if (!bus_kset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) system_kset = kset_create_and_add("system", NULL, &devices_kset->kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) if (!system_kset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) }