blob: 36d0c654ea6124c94f41851261b43c4f933c9e01 [file] [log] [blame]
Greg Kroah-Hartman989d42e2017-11-07 17:30:07 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * bus.c - bus driver management
4 *
5 * Copyright (c) 2002-3 Patrick Mochel
6 * Copyright (c) 2002-3 Open Source Development Labs
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -08007 * Copyright (c) 2007 Greg Kroah-Hartman <gregkh@suse.de>
8 * Copyright (c) 2007 Novell Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
Dmitry Torokhov765230b2015-03-30 16:20:04 -070011#include <linux/async.h>
Greg Kroah-Hartman5aee2bf2019-12-09 20:33:01 +010012#include <linux/device/bus.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/device.h>
14#include <linux/module.h>
15#include <linux/errno.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/init.h>
18#include <linux/string.h>
Kay Sieversca22e562011-12-14 14:29:38 -080019#include <linux/mutex.h>
Greg Kroah-Hartman63967682013-08-27 10:24:15 -070020#include <linux/sysfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "base.h"
22#include "power/power.h"
23
Kay Sieversca22e562011-12-14 14:29:38 -080024/* /sys/devices/system */
H Hartley Sweeten97ec4482012-04-16 18:14:10 -070025static struct kset *system_kset;
Kay Sieversca22e562011-12-14 14:29:38 -080026
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#define to_bus_attr(_attr) container_of(_attr, struct bus_attribute, attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29/*
30 * sysfs bindings for drivers
31 */
32
33#define to_drv_attr(_attr) container_of(_attr, struct driver_attribute, attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Daniel Vetter4f4b3742018-12-19 13:39:09 +010035#define DRIVER_ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) \
36 struct driver_attribute driver_attr_##_name = \
37 __ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Kay Sieversb8c5cec2007-02-16 17:33:36 +010039static int __must_check bus_rescan_devices_helper(struct device *dev,
40 void *data);
41
Greg Kroah-Hartman5901d012007-09-13 02:53:13 -070042static struct bus_type *bus_get(struct bus_type *bus)
43{
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -070044 if (bus) {
45 kset_get(&bus->p->subsys);
46 return bus;
47 }
48 return NULL;
Greg Kroah-Hartman5901d012007-09-13 02:53:13 -070049}
50
Greg Kroah-Hartmanfc1ede52007-09-13 02:53:13 -070051static void bus_put(struct bus_type *bus)
52{
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -070053 if (bus)
54 kset_put(&bus->p->subsys);
Greg Kroah-Hartmanfc1ede52007-09-13 02:53:13 -070055}
56
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080057static ssize_t drv_attr_show(struct kobject *kobj, struct attribute *attr,
58 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080060 struct driver_attribute *drv_attr = to_drv_attr(attr);
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -080061 struct driver_private *drv_priv = to_driver(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -050062 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64 if (drv_attr->show)
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -080065 ret = drv_attr->show(drv_priv->driver, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 return ret;
67}
68
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080069static ssize_t drv_attr_store(struct kobject *kobj, struct attribute *attr,
70 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080072 struct driver_attribute *drv_attr = to_drv_attr(attr);
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -080073 struct driver_private *drv_priv = to_driver(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -050074 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76 if (drv_attr->store)
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -080077 ret = drv_attr->store(drv_priv->driver, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 return ret;
79}
80
Emese Revfy52cf25d2010-01-19 02:58:23 +010081static const struct sysfs_ops driver_sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 .show = drv_attr_show,
83 .store = drv_attr_store,
84};
85
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -080086static void driver_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -080088 struct driver_private *drv_priv = to_driver(kobj);
89
Harvey Harrison2b3a3022008-03-04 16:41:05 -080090 pr_debug("driver: '%s': %s\n", kobject_name(kobj), __func__);
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -080091 kfree(drv_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092}
93
Greg Kroah-Hartmana1148fb2007-10-11 10:47:49 -060094static struct kobj_type driver_ktype = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 .sysfs_ops = &driver_sysfs_ops,
96 .release = driver_release,
97};
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099/*
100 * sysfs bindings for buses
101 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800102static ssize_t bus_attr_show(struct kobject *kobj, struct attribute *attr,
103 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800105 struct bus_attribute *bus_attr = to_bus_attr(attr);
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100106 struct subsys_private *subsys_priv = to_subsys_private(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 ssize_t ret = 0;
108
109 if (bus_attr->show)
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100110 ret = bus_attr->show(subsys_priv->bus, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 return ret;
112}
113
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800114static ssize_t bus_attr_store(struct kobject *kobj, struct attribute *attr,
115 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800117 struct bus_attribute *bus_attr = to_bus_attr(attr);
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100118 struct subsys_private *subsys_priv = to_subsys_private(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 ssize_t ret = 0;
120
121 if (bus_attr->store)
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100122 ret = bus_attr->store(subsys_priv->bus, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 return ret;
124}
125
Emese Revfy52cf25d2010-01-19 02:58:23 +0100126static const struct sysfs_ops bus_sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 .show = bus_attr_show,
128 .store = bus_attr_store,
129};
130
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800131int bus_create_file(struct bus_type *bus, struct bus_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
133 int error;
Greg Kroah-Hartman5901d012007-09-13 02:53:13 -0700134 if (bus_get(bus)) {
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700135 error = sysfs_create_file(&bus->p->subsys.kobj, &attr->attr);
Greg Kroah-Hartmanfc1ede52007-09-13 02:53:13 -0700136 bus_put(bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 } else
138 error = -EINVAL;
139 return error;
140}
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800141EXPORT_SYMBOL_GPL(bus_create_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800143void bus_remove_file(struct bus_type *bus, struct bus_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
Greg Kroah-Hartman5901d012007-09-13 02:53:13 -0700145 if (bus_get(bus)) {
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700146 sysfs_remove_file(&bus->p->subsys.kobj, &attr->attr);
Greg Kroah-Hartmanfc1ede52007-09-13 02:53:13 -0700147 bus_put(bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 }
149}
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800150EXPORT_SYMBOL_GPL(bus_remove_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Bart Van Assche174be702014-01-04 14:21:42 +0100152static void bus_release(struct kobject *kobj)
153{
Geliang Tang371fd7a2016-01-05 23:03:38 +0800154 struct subsys_private *priv = to_subsys_private(kobj);
Bart Van Assche174be702014-01-04 14:21:42 +0100155 struct bus_type *bus = priv->bus;
156
157 kfree(priv);
158 bus->p = NULL;
159}
160
Kay Sievers80f03e32007-05-26 11:21:36 +0200161static struct kobj_type bus_ktype = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 .sysfs_ops = &bus_sysfs_ops,
Bart Van Assche174be702014-01-04 14:21:42 +0100163 .release = bus_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164};
165
Kay Sievers80f03e32007-05-26 11:21:36 +0200166static int bus_uevent_filter(struct kset *kset, struct kobject *kobj)
167{
168 struct kobj_type *ktype = get_ktype(kobj);
169
170 if (ktype == &bus_ktype)
171 return 1;
172 return 0;
173}
174
Emese Revfy9cd43612009-12-31 14:52:51 +0100175static const struct kset_uevent_ops bus_uevent_ops = {
Kay Sievers80f03e32007-05-26 11:21:36 +0200176 .filter = bus_uevent_filter,
177};
178
Greg Kroah-Hartman59a54832007-10-29 23:22:26 -0500179static struct kset *bus_kset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Alan Stern2b08c8d2005-11-23 15:43:50 -0800181/* Manually detach a device from its associated driver. */
Greg Kroah-Hartman2581c9c2013-08-23 15:04:42 -0700182static ssize_t unbind_store(struct device_driver *drv, const char *buf,
183 size_t count)
Greg Kroah-Hartman151ef382005-06-22 16:09:05 -0700184{
Greg Kroah-Hartman5901d012007-09-13 02:53:13 -0700185 struct bus_type *bus = bus_get(drv->bus);
Greg Kroah-Hartman151ef382005-06-22 16:09:05 -0700186 struct device *dev;
187 int err = -ENODEV;
188
Greg Kroah-Hartman1f9ffc02008-01-27 10:29:20 -0800189 dev = bus_find_device_by_name(bus, NULL, buf);
Alan Stern2b08c8d2005-11-23 15:43:50 -0800190 if (dev && dev->driver == drv) {
Alexander Duycked887472019-01-22 10:39:16 -0800191 device_driver_detach(dev);
Greg Kroah-Hartman151ef382005-06-22 16:09:05 -0700192 err = count;
193 }
Alan Stern2b08c8d2005-11-23 15:43:50 -0800194 put_device(dev);
Greg Kroah-Hartmanfc1ede52007-09-13 02:53:13 -0700195 bus_put(bus);
Alan Stern2b08c8d2005-11-23 15:43:50 -0800196 return err;
Greg Kroah-Hartman151ef382005-06-22 16:09:05 -0700197}
Daniel Vetter4f4b3742018-12-19 13:39:09 +0100198static DRIVER_ATTR_IGNORE_LOCKDEP(unbind, S_IWUSR, NULL, unbind_store);
Greg Kroah-Hartman151ef382005-06-22 16:09:05 -0700199
Greg Kroah-Hartmanafdce752005-06-22 16:09:05 -0700200/*
201 * Manually attach a device to a driver.
202 * Note: the driver must want to bind to the device,
203 * it is not possible to override the driver's id table.
204 */
Greg Kroah-Hartman2581c9c2013-08-23 15:04:42 -0700205static ssize_t bind_store(struct device_driver *drv, const char *buf,
206 size_t count)
Greg Kroah-Hartmanafdce752005-06-22 16:09:05 -0700207{
Greg Kroah-Hartman5901d012007-09-13 02:53:13 -0700208 struct bus_type *bus = bus_get(drv->bus);
Greg Kroah-Hartmanafdce752005-06-22 16:09:05 -0700209 struct device *dev;
210 int err = -ENODEV;
211
Greg Kroah-Hartman1f9ffc02008-01-27 10:29:20 -0800212 dev = bus_find_device_by_name(bus, NULL, buf);
Ming Lei49b420a2009-01-21 23:27:47 +0800213 if (dev && dev->driver == NULL && driver_match_device(drv, dev)) {
Alexander Duycked887472019-01-22 10:39:16 -0800214 err = device_driver_attach(drv, dev);
Ryan Wilson37225402006-03-22 16:26:25 -0500215
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800216 if (err > 0) {
217 /* success */
Ryan Wilson37225402006-03-22 16:26:25 -0500218 err = count;
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800219 } else if (err == 0) {
220 /* driver didn't accept device */
Ryan Wilson37225402006-03-22 16:26:25 -0500221 err = -ENODEV;
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800222 }
Greg Kroah-Hartmanafdce752005-06-22 16:09:05 -0700223 }
Alan Stern2b08c8d2005-11-23 15:43:50 -0800224 put_device(dev);
Greg Kroah-Hartmanfc1ede52007-09-13 02:53:13 -0700225 bus_put(bus);
Alan Stern2b08c8d2005-11-23 15:43:50 -0800226 return err;
Greg Kroah-Hartmanafdce752005-06-22 16:09:05 -0700227}
Daniel Vetter4f4b3742018-12-19 13:39:09 +0100228static DRIVER_ATTR_IGNORE_LOCKDEP(bind, S_IWUSR, NULL, bind_store);
Greg Kroah-Hartmanafdce752005-06-22 16:09:05 -0700229
Greg Kroah-Hartman2e7189b2018-07-12 09:33:17 +0200230static ssize_t drivers_autoprobe_show(struct bus_type *bus, char *buf)
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100231{
Joe Perches948b3ed2020-09-16 13:40:42 -0700232 return sysfs_emit(buf, "%d\n", bus->p->drivers_autoprobe);
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100233}
234
Greg Kroah-Hartman2e7189b2018-07-12 09:33:17 +0200235static ssize_t drivers_autoprobe_store(struct bus_type *bus,
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100236 const char *buf, size_t count)
237{
238 if (buf[0] == '0')
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700239 bus->p->drivers_autoprobe = 0;
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100240 else
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700241 bus->p->drivers_autoprobe = 1;
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100242 return count;
243}
244
Greg Kroah-Hartman2e7189b2018-07-12 09:33:17 +0200245static ssize_t drivers_probe_store(struct bus_type *bus,
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100246 const char *buf, size_t count)
247{
248 struct device *dev;
Alex Williamson0372ffb2014-10-31 11:13:07 -0600249 int err = -EINVAL;
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100250
Greg Kroah-Hartman1f9ffc02008-01-27 10:29:20 -0800251 dev = bus_find_device_by_name(bus, NULL, buf);
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100252 if (!dev)
253 return -ENODEV;
Alex Williamson0372ffb2014-10-31 11:13:07 -0600254 if (bus_rescan_devices_helper(dev, NULL) == 0)
255 err = count;
256 put_device(dev);
257 return err;
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100258}
Greg Kroah-Hartman151ef382005-06-22 16:09:05 -0700259
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800260static struct device *next_device(struct klist_iter *i)
mochel@digitalimplant.org465c7a32005-03-21 11:49:14 -0800261{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800262 struct klist_node *n = klist_next(i);
Greg Kroah-Hartmanae1b4172008-12-16 12:26:21 -0800263 struct device *dev = NULL;
264 struct device_private *dev_prv;
265
266 if (n) {
267 dev_prv = to_device_private_bus(n);
268 dev = dev_prv->device;
269 }
270 return dev;
mochel@digitalimplant.org465c7a32005-03-21 11:49:14 -0800271}
272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800274 * bus_for_each_dev - device iterator.
275 * @bus: bus type.
276 * @start: device to start iterating from.
277 * @data: data for the callback.
278 * @fn: function to be called for each device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800280 * Iterate over @bus's list of devices, and call @fn for each,
281 * passing it @data. If @start is not NULL, we use that device to
282 * begin iterating from.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800284 * We check the return of @fn each time. If it returns anything
285 * other than 0, we break out and return that value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800287 * NOTE: The device that returns a non-zero value is not retained
288 * in any way, nor is its refcount incremented. If the caller needs
Alex Chiang0fa1b0a2009-05-14 23:15:22 +0200289 * to retain this data, it should do so, and increment the reference
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800290 * count in the supplied callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800292int bus_for_each_dev(struct bus_type *bus, struct device *start,
293 void *data, int (*fn)(struct device *, void *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
mochel@digitalimplant.org465c7a32005-03-21 11:49:14 -0800295 struct klist_iter i;
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800296 struct device *dev;
mochel@digitalimplant.org465c7a32005-03-21 11:49:14 -0800297 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Bjorn Helgaas4fa3e782013-01-29 16:44:27 -0700299 if (!bus || !bus->p)
mochel@digitalimplant.org465c7a32005-03-21 11:49:14 -0800300 return -EINVAL;
301
Greg Kroah-Hartman7cd9c9b2012-04-19 19:17:30 -0700302 klist_iter_init_node(&bus->p->klist_devices, &i,
303 (start ? &start->p->knode_bus : NULL));
Gimcuan Hui93ead7c2017-11-11 05:52:54 +0000304 while (!error && (dev = next_device(&i)))
Greg Kroah-Hartman7cd9c9b2012-04-19 19:17:30 -0700305 error = fn(dev, data);
306 klist_iter_exit(&i);
mochel@digitalimplant.org465c7a32005-03-21 11:49:14 -0800307 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308}
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800309EXPORT_SYMBOL_GPL(bus_for_each_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Cornelia Huck0edb5862005-06-22 16:59:51 +0200311/**
312 * bus_find_device - device iterator for locating a particular device.
313 * @bus: bus type
314 * @start: Device to begin with
315 * @data: Data to pass to match function
316 * @match: Callback function to check device
317 *
318 * This is similar to the bus_for_each_dev() function above, but it
319 * returns a reference to a device that is 'found' for later use, as
320 * determined by the @match callback.
321 *
322 * The callback should return 0 if the device doesn't match and non-zero
323 * if it does. If the callback returns non-zero, this function will
324 * return to the caller and not iterate over any more devices.
325 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800326struct device *bus_find_device(struct bus_type *bus,
Suzuki K Poulose418e3ea2019-06-14 18:53:59 +0100327 struct device *start, const void *data,
328 int (*match)(struct device *dev, const void *data))
Cornelia Huck0edb5862005-06-22 16:59:51 +0200329{
330 struct klist_iter i;
331 struct device *dev;
332
Bjorn Helgaas4fa3e782013-01-29 16:44:27 -0700333 if (!bus || !bus->p)
Cornelia Huck0edb5862005-06-22 16:59:51 +0200334 return NULL;
335
Greg Kroah-Hartman7cd9c9b2012-04-19 19:17:30 -0700336 klist_iter_init_node(&bus->p->klist_devices, &i,
337 (start ? &start->p->knode_bus : NULL));
Cornelia Huck0edb5862005-06-22 16:59:51 +0200338 while ((dev = next_device(&i)))
339 if (match(dev, data) && get_device(dev))
340 break;
341 klist_iter_exit(&i);
342 return dev;
343}
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800344EXPORT_SYMBOL_GPL(bus_find_device);
mochel@digitalimplant.org38fdac32005-03-21 12:00:18 -0800345
Kay Sieversca22e562011-12-14 14:29:38 -0800346/**
347 * subsys_find_device_by_id - find a device with a specific enumeration number
348 * @subsys: subsystem
349 * @id: index 'id' in struct device
350 * @hint: device to check first
351 *
352 * Check the hint's next object and if it is a match return it directly,
353 * otherwise, fall back to a full list search. Either way a reference for
354 * the returned object is taken.
355 */
356struct device *subsys_find_device_by_id(struct bus_type *subsys, unsigned int id,
357 struct device *hint)
358{
359 struct klist_iter i;
360 struct device *dev;
361
362 if (!subsys)
363 return NULL;
364
365 if (hint) {
Greg Kroah-Hartman7cd9c9b2012-04-19 19:17:30 -0700366 klist_iter_init_node(&subsys->p->klist_devices, &i, &hint->p->knode_bus);
Kay Sieversca22e562011-12-14 14:29:38 -0800367 dev = next_device(&i);
368 if (dev && dev->id == id && get_device(dev)) {
369 klist_iter_exit(&i);
370 return dev;
371 }
372 klist_iter_exit(&i);
373 }
374
375 klist_iter_init_node(&subsys->p->klist_devices, &i, NULL);
376 while ((dev = next_device(&i))) {
377 if (dev->id == id && get_device(dev)) {
378 klist_iter_exit(&i);
379 return dev;
380 }
381 }
382 klist_iter_exit(&i);
383 return NULL;
384}
385EXPORT_SYMBOL_GPL(subsys_find_device_by_id);
386
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800387static struct device_driver *next_driver(struct klist_iter *i)
mochel@digitalimplant.org38fdac32005-03-21 12:00:18 -0800388{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800389 struct klist_node *n = klist_next(i);
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -0800390 struct driver_private *drv_priv;
391
392 if (n) {
393 drv_priv = container_of(n, struct driver_private, knode_bus);
394 return drv_priv->driver;
395 }
396 return NULL;
mochel@digitalimplant.org38fdac32005-03-21 12:00:18 -0800397}
398
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800400 * bus_for_each_drv - driver iterator
401 * @bus: bus we're dealing with.
402 * @start: driver to start iterating on.
403 * @data: data to pass to the callback.
404 * @fn: function to call for each driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800406 * This is nearly identical to the device iterator above.
407 * We iterate over each driver that belongs to @bus, and call
408 * @fn for each. If @fn returns anything but 0, we break out
409 * and return it. If @start is not NULL, we use it as the head
410 * of the list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800412 * NOTE: we don't return the driver that returns a non-zero
413 * value, nor do we leave the reference count incremented for that
414 * driver. If the caller needs to know that info, it must set it
415 * in the callback. It must also be sure to increment the refcount
416 * so it doesn't disappear before returning to the caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800418int bus_for_each_drv(struct bus_type *bus, struct device_driver *start,
419 void *data, int (*fn)(struct device_driver *, void *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
mochel@digitalimplant.org38fdac32005-03-21 12:00:18 -0800421 struct klist_iter i;
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800422 struct device_driver *drv;
mochel@digitalimplant.org38fdac32005-03-21 12:00:18 -0800423 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
mochel@digitalimplant.org38fdac32005-03-21 12:00:18 -0800425 if (!bus)
426 return -EINVAL;
427
Greg Kroah-Hartman7cd9c9b2012-04-19 19:17:30 -0700428 klist_iter_init_node(&bus->p->klist_drivers, &i,
429 start ? &start->p->knode_bus : NULL);
430 while ((drv = next_driver(&i)) && !error)
431 error = fn(drv, data);
432 klist_iter_exit(&i);
mochel@digitalimplant.org38fdac32005-03-21 12:00:18 -0800433 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800435EXPORT_SYMBOL_GPL(bus_for_each_drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800438 * bus_add_device - add device to bus
439 * @dev: device being added
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 *
Alan Stern2023c612009-07-30 15:27:18 -0400441 * - Add device's bus attributes.
442 * - Create links to device's bus.
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800443 * - Add the device to its bus's list of devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800445int bus_add_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800447 struct bus_type *bus = bus_get(dev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 int error = 0;
449
450 if (bus) {
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100451 pr_debug("bus: '%s': add device %s\n", bus->name, dev_name(dev));
Greg Kroah-Hartmanfa6fdb32013-08-08 15:22:55 -0700452 error = device_add_groups(dev, bus->dev_groups);
453 if (error)
Greg Kroah-Hartmanb46c7332013-08-23 14:12:09 -0700454 goto out_put;
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700455 error = sysfs_create_link(&bus->p->devices_kset->kobj,
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100456 &dev->kobj, dev_name(dev));
Andrew Mortonf86db392006-08-14 22:43:20 -0700457 if (error)
Junjie Mao1c342032015-01-28 10:02:44 +0800458 goto out_groups;
Andrew Mortonf86db392006-08-14 22:43:20 -0700459 error = sysfs_create_link(&dev->kobj,
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700460 &dev->bus->p->subsys.kobj, "subsystem");
Andrew Mortonf86db392006-08-14 22:43:20 -0700461 if (error)
Cornelia Huck513e7332006-09-22 11:37:08 +0200462 goto out_subsys;
Alan Stern2023c612009-07-30 15:27:18 -0400463 klist_add_tail(&dev->p->knode_bus, &bus->p->klist_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 }
Cornelia Huck513e7332006-09-22 11:37:08 +0200465 return 0;
466
Cornelia Huck513e7332006-09-22 11:37:08 +0200467out_subsys:
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100468 sysfs_remove_link(&bus->p->devices_kset->kobj, dev_name(dev));
Greg Kroah-Hartmanfa6fdb32013-08-08 15:22:55 -0700469out_groups:
470 device_remove_groups(dev, bus->dev_groups);
Cornelia Huck513e7332006-09-22 11:37:08 +0200471out_put:
Greg Kroah-Hartmanfc1ede52007-09-13 02:53:13 -0700472 bus_put(dev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 return error;
474}
475
476/**
Alan Stern2023c612009-07-30 15:27:18 -0400477 * bus_probe_device - probe drivers for a new device
478 * @dev: device to probe
Kay Sievers53877d02006-04-04 20:42:26 +0200479 *
Alan Stern2023c612009-07-30 15:27:18 -0400480 * - Automatically probe for a driver if the bus allows it.
Kay Sievers53877d02006-04-04 20:42:26 +0200481 */
Alan Stern2023c612009-07-30 15:27:18 -0400482void bus_probe_device(struct device *dev)
Kay Sievers53877d02006-04-04 20:42:26 +0200483{
Andrew Mortonf86db392006-08-14 22:43:20 -0700484 struct bus_type *bus = dev->bus;
Kay Sieversca22e562011-12-14 14:29:38 -0800485 struct subsys_interface *sif;
Kay Sievers53877d02006-04-04 20:42:26 +0200486
Kay Sieversca22e562011-12-14 14:29:38 -0800487 if (!bus)
488 return;
489
Dmitry Torokhov765230b2015-03-30 16:20:04 -0700490 if (bus->p->drivers_autoprobe)
491 device_initial_probe(dev);
Kay Sieversca22e562011-12-14 14:29:38 -0800492
493 mutex_lock(&bus->p->mutex);
494 list_for_each_entry(sif, &bus->p->interfaces, node)
495 if (sif->add_dev)
496 sif->add_dev(dev, sif);
497 mutex_unlock(&bus->p->mutex);
Kay Sievers53877d02006-04-04 20:42:26 +0200498}
499
500/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800501 * bus_remove_device - remove device from bus
502 * @dev: device to be removed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 *
Kay Sieversca22e562011-12-14 14:29:38 -0800504 * - Remove device from all interfaces.
505 * - Remove symlink from bus' directory.
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800506 * - Delete device from bus's list.
507 * - Detach from its driver.
508 * - Drop reference taken in bus_add_device().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800510void bus_remove_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
Kay Sieversca22e562011-12-14 14:29:38 -0800512 struct bus_type *bus = dev->bus;
513 struct subsys_interface *sif;
Greg Kroah-Hartman3f62e572008-03-13 17:07:03 -0400514
Kay Sieversca22e562011-12-14 14:29:38 -0800515 if (!bus)
516 return;
517
518 mutex_lock(&bus->p->mutex);
519 list_for_each_entry(sif, &bus->p->interfaces, node)
520 if (sif->remove_dev)
521 sif->remove_dev(dev, sif);
522 mutex_unlock(&bus->p->mutex);
523
524 sysfs_remove_link(&dev->kobj, "subsystem");
525 sysfs_remove_link(&dev->bus->p->devices_kset->kobj,
526 dev_name(dev));
Greg Kroah-Hartmanfa6fdb32013-08-08 15:22:55 -0700527 device_remove_groups(dev, dev->bus->dev_groups);
Kay Sieversca22e562011-12-14 14:29:38 -0800528 if (klist_node_attached(&dev->p->knode_bus))
529 klist_del(&dev->p->knode_bus);
530
531 pr_debug("bus: '%s': remove device %s\n",
532 dev->bus->name, dev_name(dev));
533 device_release_driver(dev);
534 bus_put(dev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535}
536
Andrew Mortonf86db392006-08-14 22:43:20 -0700537static int __must_check add_bind_files(struct device_driver *drv)
Greg Kroah-Hartman874c6242005-12-13 15:17:34 -0800538{
Andrew Mortonf86db392006-08-14 22:43:20 -0700539 int ret;
540
541 ret = driver_create_file(drv, &driver_attr_unbind);
542 if (ret == 0) {
543 ret = driver_create_file(drv, &driver_attr_bind);
544 if (ret)
545 driver_remove_file(drv, &driver_attr_unbind);
546 }
547 return ret;
Greg Kroah-Hartman874c6242005-12-13 15:17:34 -0800548}
549
550static void remove_bind_files(struct device_driver *drv)
551{
552 driver_remove_file(drv, &driver_attr_bind);
553 driver_remove_file(drv, &driver_attr_unbind);
554}
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100555
Greg Kroah-Hartman2e7189b2018-07-12 09:33:17 +0200556static BUS_ATTR_WO(drivers_probe);
557static BUS_ATTR_RW(drivers_autoprobe);
Kay Sievers83807702007-07-30 02:28:56 +0200558
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100559static int add_probe_files(struct bus_type *bus)
560{
561 int retval;
562
Kay Sievers83807702007-07-30 02:28:56 +0200563 retval = bus_create_file(bus, &bus_attr_drivers_probe);
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100564 if (retval)
565 goto out;
566
Kay Sievers83807702007-07-30 02:28:56 +0200567 retval = bus_create_file(bus, &bus_attr_drivers_autoprobe);
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100568 if (retval)
Kay Sievers83807702007-07-30 02:28:56 +0200569 bus_remove_file(bus, &bus_attr_drivers_probe);
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100570out:
571 return retval;
572}
573
574static void remove_probe_files(struct bus_type *bus)
575{
Kay Sievers83807702007-07-30 02:28:56 +0200576 bus_remove_file(bus, &bus_attr_drivers_autoprobe);
577 bus_remove_file(bus, &bus_attr_drivers_probe);
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100578}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Greg Kroah-Hartman2581c9c2013-08-23 15:04:42 -0700580static ssize_t uevent_store(struct device_driver *drv, const char *buf,
581 size_t count)
Kay Sievers7ac1cf42007-08-12 20:43:55 +0200582{
Peter Rajnohadf44b472018-12-05 12:27:44 +0100583 int rc;
584
585 rc = kobject_synth_uevent(&drv->p->kobj, buf, count);
586 return rc ? rc : count;
Kay Sievers7ac1cf42007-08-12 20:43:55 +0200587}
Greg Kroah-Hartman2581c9c2013-08-23 15:04:42 -0700588static DRIVER_ATTR_WO(uevent);
Kay Sievers7ac1cf42007-08-12 20:43:55 +0200589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800591 * bus_add_driver - Add a driver to the bus.
592 * @drv: driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 */
Andrew Mortonf86db392006-08-14 22:43:20 -0700594int bus_add_driver(struct device_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -0800596 struct bus_type *bus;
597 struct driver_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 int error = 0;
599
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -0800600 bus = bus_get(drv->bus);
Jeff Garzikd9fd4d3b32006-10-04 07:48:03 -0400601 if (!bus)
Greg Kroah-Hartman4f6e1942007-04-20 11:29:52 -0700602 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800604 pr_debug("bus: '%s': add driver %s\n", bus->name, drv->name);
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -0800605
606 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Cornelia Huck07634462008-02-18 17:04:25 +0100607 if (!priv) {
608 error = -ENOMEM;
609 goto out_put_bus;
610 }
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -0800611 klist_init(&priv->klist_devices, NULL, NULL);
612 priv->driver = drv;
613 drv->p = priv;
Greg Kroah-Hartmanc8e90d82007-12-17 15:54:39 -0400614 priv->kobj.kset = bus->p->drivers_kset;
615 error = kobject_init_and_add(&priv->kobj, &driver_ktype, NULL,
616 "%s", drv->name);
Cornelia Huckdc0afa82007-07-09 11:39:18 -0700617 if (error)
Cornelia Huck07634462008-02-18 17:04:25 +0100618 goto out_unregister;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Ming Lei190888a2012-11-19 23:35:17 +0800620 klist_add_tail(&priv->knode_bus, &bus->p->klist_drivers);
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700621 if (drv->bus->p->drivers_autoprobe) {
Alexander Duyckef0ff682019-01-22 10:39:21 -0800622 error = driver_attach(drv);
623 if (error)
624 goto out_unregister;
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100625 }
Jeff Garzikd9fd4d3b32006-10-04 07:48:03 -0400626 module_add_driver(drv->owner, drv);
627
Kay Sievers7ac1cf42007-08-12 20:43:55 +0200628 error = driver_create_file(drv, &driver_attr_uevent);
629 if (error) {
630 printk(KERN_ERR "%s: uevent attr (%s) failed\n",
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800631 __func__, drv->name);
Kay Sievers7ac1cf42007-08-12 20:43:55 +0200632 }
Greg Kroah-Hartmane18945b2013-09-27 21:47:21 -0700633 error = driver_add_groups(drv, bus->drv_groups);
Jeff Garzikd9fd4d3b32006-10-04 07:48:03 -0400634 if (error) {
635 /* How the hell do we get out of this pickle? Give up */
Joe Pater4044b2f2021-01-10 14:54:43 +0000636 printk(KERN_ERR "%s: driver_add_groups(%s) failed\n",
Greg Kroah-Hartmaned0617b2013-08-08 15:22:56 -0700637 __func__, drv->name);
Greg Kroah-Hartmane18945b2013-09-27 21:47:21 -0700638 }
Dmitry Torokhov1a6f2a72009-10-12 20:17:41 -0700639
640 if (!drv->suppress_bind_attrs) {
641 error = add_bind_files(drv);
642 if (error) {
643 /* Ditto */
644 printk(KERN_ERR "%s: add_bind_files(%s) failed\n",
645 __func__, drv->name);
646 }
Jeff Garzikd9fd4d3b32006-10-04 07:48:03 -0400647 }
648
Kay Sievers5c8563d2009-05-28 14:24:07 -0700649 return 0;
Dmitry Torokhov1a6f2a72009-10-12 20:17:41 -0700650
Andrew Mortonf86db392006-08-14 22:43:20 -0700651out_unregister:
Phil Carmody99b28f12009-12-14 20:28:12 +0200652 kobject_put(&priv->kobj);
Christophe JAILLET0f9b0112017-08-29 21:23:49 +0200653 /* drv->p is freed in driver_release() */
Kay Sievers5c8563d2009-05-28 14:24:07 -0700654 drv->p = NULL;
Andrew Mortonf86db392006-08-14 22:43:20 -0700655out_put_bus:
Greg Kroah-Hartmanfc1ede52007-09-13 02:53:13 -0700656 bus_put(bus);
Andrew Mortonf86db392006-08-14 22:43:20 -0700657 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658}
659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800661 * bus_remove_driver - delete driver from bus's knowledge.
662 * @drv: driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800664 * Detach the driver from the devices it controls, and remove
665 * it from its bus's list of drivers. Finally, we drop the reference
666 * to the bus we took in bus_add_driver().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800668void bus_remove_driver(struct device_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
Jeff Garzikd9fd4d3b32006-10-04 07:48:03 -0400670 if (!drv->bus)
671 return;
672
Dmitry Torokhov1a6f2a72009-10-12 20:17:41 -0700673 if (!drv->suppress_bind_attrs)
674 remove_bind_files(drv);
Greg Kroah-Hartmaned0617b2013-08-08 15:22:56 -0700675 driver_remove_groups(drv, drv->bus->drv_groups);
Kay Sievers7ac1cf42007-08-12 20:43:55 +0200676 driver_remove_file(drv, &driver_attr_uevent);
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -0800677 klist_remove(&drv->p->knode_bus);
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800678 pr_debug("bus: '%s': remove driver %s\n", drv->bus->name, drv->name);
Jeff Garzikd9fd4d3b32006-10-04 07:48:03 -0400679 driver_detach(drv);
680 module_remove_driver(drv);
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -0800681 kobject_put(&drv->p->kobj);
Greg Kroah-Hartmanfc1ede52007-09-13 02:53:13 -0700682 bus_put(drv->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683}
684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685/* Helper for bus_rescan_devices's iter */
Andrew Mortonf86db392006-08-14 22:43:20 -0700686static int __must_check bus_rescan_devices_helper(struct device *dev,
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800687 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
Andrew Mortonf86db392006-08-14 22:43:20 -0700689 int ret = 0;
690
Alan Sternbf74ad52005-11-17 16:54:12 -0500691 if (!dev->driver) {
Martin Liu8c97a462018-05-31 00:31:36 +0800692 if (dev->parent && dev->bus->need_parent_lock)
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800693 device_lock(dev->parent);
Andrew Mortonf86db392006-08-14 22:43:20 -0700694 ret = device_attach(dev);
Martin Liu8c97a462018-05-31 00:31:36 +0800695 if (dev->parent && dev->bus->need_parent_lock)
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800696 device_unlock(dev->parent);
Alan Sternbf74ad52005-11-17 16:54:12 -0500697 }
Andrew Mortonf86db392006-08-14 22:43:20 -0700698 return ret < 0 ? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699}
700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701/**
Greg Kroah-Hartman23d3d602005-06-22 16:09:05 -0700702 * bus_rescan_devices - rescan devices on the bus for possible drivers
703 * @bus: the bus to scan.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 *
Greg Kroah-Hartman23d3d602005-06-22 16:09:05 -0700705 * This function will look for devices on the bus with no driver
706 * attached and rescan it against existing drivers to see if it matches
707 * any by calling device_attach() for the unbound devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800709int bus_rescan_devices(struct bus_type *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
Andrew Mortonf86db392006-08-14 22:43:20 -0700711 return bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712}
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800713EXPORT_SYMBOL_GPL(bus_rescan_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Moore, Erice935d5d2006-03-14 09:18:18 -0700715/**
716 * device_reprobe - remove driver for a device and probe for a new driver
717 * @dev: the device to reprobe
718 *
719 * This function detaches the attached driver (if any) for the given
720 * device and restarts the driver probing process. It is intended
721 * to use if probing criteria changed during a devices lifetime and
722 * driver attachment should change accordingly.
723 */
Andrew Mortonf86db392006-08-14 22:43:20 -0700724int device_reprobe(struct device *dev)
Moore, Erice935d5d2006-03-14 09:18:18 -0700725{
Alexander Duycked887472019-01-22 10:39:16 -0800726 if (dev->driver)
727 device_driver_detach(dev);
Andrew Mortonf86db392006-08-14 22:43:20 -0700728 return bus_rescan_devices_helper(dev, NULL);
Moore, Erice935d5d2006-03-14 09:18:18 -0700729}
730EXPORT_SYMBOL_GPL(device_reprobe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Greg Kroah-Hartman12478ba2013-08-08 15:22:57 -0700732static int bus_add_groups(struct bus_type *bus,
733 const struct attribute_group **groups)
734{
Greg Kroah-Hartman3e9b2ba2013-08-21 13:47:50 -0700735 return sysfs_create_groups(&bus->p->subsys.kobj, groups);
Greg Kroah-Hartman12478ba2013-08-08 15:22:57 -0700736}
737
738static void bus_remove_groups(struct bus_type *bus,
739 const struct attribute_group **groups)
740{
Greg Kroah-Hartman3e9b2ba2013-08-21 13:47:50 -0700741 sysfs_remove_groups(&bus->p->subsys.kobj, groups);
Greg Kroah-Hartman12478ba2013-08-08 15:22:57 -0700742}
743
James Bottomley34bb61f2005-09-06 16:56:51 -0700744static void klist_devices_get(struct klist_node *n)
745{
Greg Kroah-Hartmanae1b4172008-12-16 12:26:21 -0800746 struct device_private *dev_prv = to_device_private_bus(n);
747 struct device *dev = dev_prv->device;
James Bottomley34bb61f2005-09-06 16:56:51 -0700748
749 get_device(dev);
750}
751
752static void klist_devices_put(struct klist_node *n)
753{
Greg Kroah-Hartmanae1b4172008-12-16 12:26:21 -0800754 struct device_private *dev_prv = to_device_private_bus(n);
755 struct device *dev = dev_prv->device;
James Bottomley34bb61f2005-09-06 16:56:51 -0700756
757 put_device(dev);
758}
759
Kay Sievers7ac1cf42007-08-12 20:43:55 +0200760static ssize_t bus_uevent_store(struct bus_type *bus,
761 const char *buf, size_t count)
762{
Peter Rajnohadf44b472018-12-05 12:27:44 +0100763 int rc;
764
765 rc = kobject_synth_uevent(&bus->p->subsys.kobj, buf, count);
766 return rc ? rc : count;
Kay Sievers7ac1cf42007-08-12 20:43:55 +0200767}
Greg Kroah-Hartmana4723042018-10-29 16:31:26 +0100768/*
769 * "open code" the old BUS_ATTR() macro here. We want to use BUS_ATTR_WO()
770 * here, but can not use it as earlier in the file we have
771 * DEVICE_ATTR_WO(uevent), which would cause a clash with the with the store
772 * function name.
773 */
774static struct bus_attribute bus_attr_uevent = __ATTR(uevent, S_IWUSR, NULL,
775 bus_uevent_store);
Kay Sievers7ac1cf42007-08-12 20:43:55 +0200776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777/**
Michal Hockobe871b72013-03-12 17:21:19 +0100778 * bus_register - register a driver-core subsystem
Randy Dunlap78d79552012-01-21 11:02:28 -0800779 * @bus: bus to register
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 *
Randy Dunlap78d79552012-01-21 11:02:28 -0800781 * Once we have that, we register the bus with the kobject
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800782 * infrastructure, then register the children subsystems it has:
Kay Sieversca22e562011-12-14 14:29:38 -0800783 * the devices and drivers that belong to the subsystem.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 */
Michal Hockobe871b72013-03-12 17:21:19 +0100785int bus_register(struct bus_type *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
787 int retval;
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100788 struct subsys_private *priv;
Michal Hockobe871b72013-03-12 17:21:19 +0100789 struct lock_class_key *key = &bus->lock_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100791 priv = kzalloc(sizeof(struct subsys_private), GFP_KERNEL);
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700792 if (!priv)
793 return -ENOMEM;
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +1000794
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700795 priv->bus = bus;
796 bus->p = priv;
797
798 BLOCKING_INIT_NOTIFIER_HEAD(&priv->bus_notifier);
799
800 retval = kobject_set_name(&priv->subsys.kobj, "%s", bus->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 if (retval)
802 goto out;
803
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700804 priv->subsys.kobj.kset = bus_kset;
805 priv->subsys.kobj.ktype = &bus_ktype;
806 priv->drivers_autoprobe = 1;
Greg Kroah-Hartmand6b05b82007-09-12 15:06:57 -0700807
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700808 retval = kset_register(&priv->subsys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 if (retval)
810 goto out;
811
Kay Sievers7ac1cf42007-08-12 20:43:55 +0200812 retval = bus_create_file(bus, &bus_attr_uevent);
813 if (retval)
814 goto bus_uevent_fail;
815
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700816 priv->devices_kset = kset_create_and_add("devices", NULL,
817 &priv->subsys.kobj);
818 if (!priv->devices_kset) {
Greg Kroah-Hartman3d899592007-11-01 13:31:26 -0700819 retval = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 goto bus_devices_fail;
Greg Kroah-Hartman3d899592007-11-01 13:31:26 -0700821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700823 priv->drivers_kset = kset_create_and_add("drivers", NULL,
824 &priv->subsys.kobj);
825 if (!priv->drivers_kset) {
Greg Kroah-Hartman6dcec252007-11-01 13:31:26 -0700826 retval = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 goto bus_drivers_fail;
Greg Kroah-Hartman6dcec252007-11-01 13:31:26 -0700828 }
mochel@digitalimplant.org465c7a32005-03-21 11:49:14 -0800829
Kay Sieversca22e562011-12-14 14:29:38 -0800830 INIT_LIST_HEAD(&priv->interfaces);
831 __mutex_init(&priv->mutex, "subsys mutex", key);
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700832 klist_init(&priv->klist_devices, klist_devices_get, klist_devices_put);
833 klist_init(&priv->klist_drivers, NULL, NULL);
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100834
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100835 retval = add_probe_files(bus);
836 if (retval)
837 goto bus_probe_files_fail;
838
Greg Kroah-Hartman12478ba2013-08-08 15:22:57 -0700839 retval = bus_add_groups(bus, bus->bus_groups);
840 if (retval)
841 goto bus_groups_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800843 pr_debug("bus: '%s': registered\n", bus->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 return 0;
845
Greg Kroah-Hartman12478ba2013-08-08 15:22:57 -0700846bus_groups_fail:
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100847 remove_probe_files(bus);
848bus_probe_files_fail:
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700849 kset_unregister(bus->p->drivers_kset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850bus_drivers_fail:
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700851 kset_unregister(bus->p->devices_kset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852bus_devices_fail:
Kay Sievers7ac1cf42007-08-12 20:43:55 +0200853 bus_remove_file(bus, &bus_attr_uevent);
854bus_uevent_fail:
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700855 kset_unregister(&bus->p->subsys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856out:
Jike Song600c20f2010-07-15 17:43:54 +0800857 kfree(bus->p);
Dave Youngf48f3fe2009-02-14 21:23:22 +0800858 bus->p = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 return retval;
860}
Michal Hockobe871b72013-03-12 17:21:19 +0100861EXPORT_SYMBOL_GPL(bus_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800864 * bus_unregister - remove a bus from the system
865 * @bus: bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800867 * Unregister the child subsystems and the bus itself.
868 * Finally, we call bus_put() to release the refcount
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800870void bus_unregister(struct bus_type *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800872 pr_debug("bus: '%s': unregistering\n", bus->name);
Kay Sieversca22e562011-12-14 14:29:38 -0800873 if (bus->dev_root)
874 device_unregister(bus->dev_root);
Greg Kroah-Hartman12478ba2013-08-08 15:22:57 -0700875 bus_remove_groups(bus, bus->bus_groups);
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100876 remove_probe_files(bus);
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700877 kset_unregister(bus->p->drivers_kset);
878 kset_unregister(bus->p->devices_kset);
Kay Sievers7ac1cf42007-08-12 20:43:55 +0200879 bus_remove_file(bus, &bus_attr_uevent);
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700880 kset_unregister(&bus->p->subsys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881}
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800882EXPORT_SYMBOL_GPL(bus_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +1000884int bus_register_notifier(struct bus_type *bus, struct notifier_block *nb)
885{
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700886 return blocking_notifier_chain_register(&bus->p->bus_notifier, nb);
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +1000887}
888EXPORT_SYMBOL_GPL(bus_register_notifier);
889
890int bus_unregister_notifier(struct bus_type *bus, struct notifier_block *nb)
891{
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700892 return blocking_notifier_chain_unregister(&bus->p->bus_notifier, nb);
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +1000893}
894EXPORT_SYMBOL_GPL(bus_unregister_notifier);
895
Greg Kroah-Hartman0fed80f2007-11-01 19:41:16 -0700896struct kset *bus_get_kset(struct bus_type *bus)
897{
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700898 return &bus->p->subsys;
Greg Kroah-Hartman0fed80f2007-11-01 19:41:16 -0700899}
900EXPORT_SYMBOL_GPL(bus_get_kset);
901
Greg Kroah-Hartmanb2490722007-11-01 19:41:16 -0700902struct klist *bus_get_device_klist(struct bus_type *bus)
903{
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700904 return &bus->p->klist_devices;
Greg Kroah-Hartmanb2490722007-11-01 19:41:16 -0700905}
906EXPORT_SYMBOL_GPL(bus_get_device_klist);
907
Greg Kroah-Hartman99178b02008-08-26 11:00:57 -0500908/*
Robert P. J. Daydca25eb2010-11-21 08:15:01 -0500909 * Yes, this forcibly breaks the klist abstraction temporarily. It
Greg Kroah-Hartman99178b02008-08-26 11:00:57 -0500910 * just wants to sort the klist, not change reference counts and
911 * take/drop locks rapidly in the process. It does all this while
912 * holding the lock for the list, so objects can't otherwise be
913 * added/removed while we're swizzling.
914 */
915static void device_insertion_sort_klist(struct device *a, struct list_head *list,
916 int (*compare)(const struct device *a,
917 const struct device *b))
918{
Greg Kroah-Hartman99178b02008-08-26 11:00:57 -0500919 struct klist_node *n;
Greg Kroah-Hartmanae1b4172008-12-16 12:26:21 -0800920 struct device_private *dev_prv;
Greg Kroah-Hartman99178b02008-08-26 11:00:57 -0500921 struct device *b;
922
Geliang Tang4c627852016-01-05 23:03:37 +0800923 list_for_each_entry(n, list, n_node) {
Greg Kroah-Hartmanae1b4172008-12-16 12:26:21 -0800924 dev_prv = to_device_private_bus(n);
925 b = dev_prv->device;
Greg Kroah-Hartman99178b02008-08-26 11:00:57 -0500926 if (compare(a, b) <= 0) {
Greg Kroah-Hartmanae1b4172008-12-16 12:26:21 -0800927 list_move_tail(&a->p->knode_bus.n_node,
928 &b->p->knode_bus.n_node);
Greg Kroah-Hartman99178b02008-08-26 11:00:57 -0500929 return;
930 }
931 }
Greg Kroah-Hartmanae1b4172008-12-16 12:26:21 -0800932 list_move_tail(&a->p->knode_bus.n_node, list);
Greg Kroah-Hartman99178b02008-08-26 11:00:57 -0500933}
934
935void bus_sort_breadthfirst(struct bus_type *bus,
936 int (*compare)(const struct device *a,
937 const struct device *b))
938{
939 LIST_HEAD(sorted_devices);
Geliang Tang4c627852016-01-05 23:03:37 +0800940 struct klist_node *n, *tmp;
Greg Kroah-Hartmanae1b4172008-12-16 12:26:21 -0800941 struct device_private *dev_prv;
Greg Kroah-Hartman99178b02008-08-26 11:00:57 -0500942 struct device *dev;
943 struct klist *device_klist;
944
945 device_klist = bus_get_device_klist(bus);
946
947 spin_lock(&device_klist->k_lock);
Geliang Tang4c627852016-01-05 23:03:37 +0800948 list_for_each_entry_safe(n, tmp, &device_klist->k_list, n_node) {
Greg Kroah-Hartmanae1b4172008-12-16 12:26:21 -0800949 dev_prv = to_device_private_bus(n);
950 dev = dev_prv->device;
Greg Kroah-Hartman99178b02008-08-26 11:00:57 -0500951 device_insertion_sort_klist(dev, &sorted_devices, compare);
952 }
953 list_splice(&sorted_devices, &device_klist->k_list);
954 spin_unlock(&device_klist->k_lock);
955}
956EXPORT_SYMBOL_GPL(bus_sort_breadthfirst);
957
Kay Sieversca22e562011-12-14 14:29:38 -0800958/**
959 * subsys_dev_iter_init - initialize subsys device iterator
960 * @iter: subsys iterator to initialize
961 * @subsys: the subsys we wanna iterate over
962 * @start: the device to start iterating from, if any
963 * @type: device_type of the devices to iterate over, NULL for all
964 *
965 * Initialize subsys iterator @iter such that it iterates over devices
966 * of @subsys. If @start is set, the list iteration will start there,
967 * otherwise if it is NULL, the iteration starts at the beginning of
968 * the list.
969 */
Greg Kroah-Hartman7cd9c9b2012-04-19 19:17:30 -0700970void subsys_dev_iter_init(struct subsys_dev_iter *iter, struct bus_type *subsys,
971 struct device *start, const struct device_type *type)
Kay Sieversca22e562011-12-14 14:29:38 -0800972{
973 struct klist_node *start_knode = NULL;
974
975 if (start)
976 start_knode = &start->p->knode_bus;
Greg Kroah-Hartman7cd9c9b2012-04-19 19:17:30 -0700977 klist_iter_init_node(&subsys->p->klist_devices, &iter->ki, start_knode);
978 iter->type = type;
Kay Sieversca22e562011-12-14 14:29:38 -0800979}
980EXPORT_SYMBOL_GPL(subsys_dev_iter_init);
981
982/**
983 * subsys_dev_iter_next - iterate to the next device
984 * @iter: subsys iterator to proceed
985 *
986 * Proceed @iter to the next device and return it. Returns NULL if
987 * iteration is complete.
988 *
989 * The returned device is referenced and won't be released till
990 * iterator is proceed to the next device or exited. The caller is
991 * free to do whatever it wants to do with the device including
992 * calling back into subsys code.
993 */
994struct device *subsys_dev_iter_next(struct subsys_dev_iter *iter)
995{
996 struct klist_node *knode;
997 struct device *dev;
998
999 for (;;) {
1000 knode = klist_next(&iter->ki);
1001 if (!knode)
1002 return NULL;
Geliang Tang371fd7a2016-01-05 23:03:38 +08001003 dev = to_device_private_bus(knode)->device;
Kay Sieversca22e562011-12-14 14:29:38 -08001004 if (!iter->type || iter->type == dev->type)
1005 return dev;
1006 }
1007}
1008EXPORT_SYMBOL_GPL(subsys_dev_iter_next);
1009
1010/**
1011 * subsys_dev_iter_exit - finish iteration
1012 * @iter: subsys iterator to finish
1013 *
1014 * Finish an iteration. Always call this function after iteration is
1015 * complete whether the iteration ran till the end or not.
1016 */
1017void subsys_dev_iter_exit(struct subsys_dev_iter *iter)
1018{
1019 klist_iter_exit(&iter->ki);
1020}
1021EXPORT_SYMBOL_GPL(subsys_dev_iter_exit);
1022
1023int subsys_interface_register(struct subsys_interface *sif)
1024{
1025 struct bus_type *subsys;
1026 struct subsys_dev_iter iter;
1027 struct device *dev;
1028
1029 if (!sif || !sif->subsys)
1030 return -ENODEV;
1031
1032 subsys = bus_get(sif->subsys);
1033 if (!subsys)
1034 return -EINVAL;
1035
1036 mutex_lock(&subsys->p->mutex);
1037 list_add_tail(&sif->node, &subsys->p->interfaces);
1038 if (sif->add_dev) {
1039 subsys_dev_iter_init(&iter, subsys, NULL, NULL);
1040 while ((dev = subsys_dev_iter_next(&iter)))
1041 sif->add_dev(dev, sif);
1042 subsys_dev_iter_exit(&iter);
1043 }
1044 mutex_unlock(&subsys->p->mutex);
1045
1046 return 0;
1047}
1048EXPORT_SYMBOL_GPL(subsys_interface_register);
1049
1050void subsys_interface_unregister(struct subsys_interface *sif)
1051{
Jonghwan Choi2b315942012-01-14 11:06:03 +09001052 struct bus_type *subsys;
Kay Sieversca22e562011-12-14 14:29:38 -08001053 struct subsys_dev_iter iter;
1054 struct device *dev;
1055
Jonghwan Choi2b315942012-01-14 11:06:03 +09001056 if (!sif || !sif->subsys)
Kay Sieversca22e562011-12-14 14:29:38 -08001057 return;
1058
Jonghwan Choi2b315942012-01-14 11:06:03 +09001059 subsys = sif->subsys;
1060
Kay Sieversca22e562011-12-14 14:29:38 -08001061 mutex_lock(&subsys->p->mutex);
1062 list_del_init(&sif->node);
1063 if (sif->remove_dev) {
1064 subsys_dev_iter_init(&iter, subsys, NULL, NULL);
1065 while ((dev = subsys_dev_iter_next(&iter)))
1066 sif->remove_dev(dev, sif);
1067 subsys_dev_iter_exit(&iter);
1068 }
1069 mutex_unlock(&subsys->p->mutex);
1070
1071 bus_put(subsys);
1072}
1073EXPORT_SYMBOL_GPL(subsys_interface_unregister);
1074
1075static void system_root_device_release(struct device *dev)
1076{
1077 kfree(dev);
1078}
Tejun Heod73ce002013-03-12 11:30:05 -07001079
1080static int subsys_register(struct bus_type *subsys,
1081 const struct attribute_group **groups,
1082 struct kobject *parent_of_root)
1083{
1084 struct device *dev;
1085 int err;
1086
1087 err = bus_register(subsys);
1088 if (err < 0)
1089 return err;
1090
1091 dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1092 if (!dev) {
1093 err = -ENOMEM;
1094 goto err_dev;
1095 }
1096
1097 err = dev_set_name(dev, "%s", subsys->name);
1098 if (err < 0)
1099 goto err_name;
1100
1101 dev->kobj.parent = parent_of_root;
1102 dev->groups = groups;
1103 dev->release = system_root_device_release;
1104
1105 err = device_register(dev);
1106 if (err < 0)
1107 goto err_dev_reg;
1108
1109 subsys->dev_root = dev;
1110 return 0;
1111
1112err_dev_reg:
1113 put_device(dev);
1114 dev = NULL;
1115err_name:
1116 kfree(dev);
1117err_dev:
1118 bus_unregister(subsys);
1119 return err;
1120}
1121
Kay Sieversca22e562011-12-14 14:29:38 -08001122/**
1123 * subsys_system_register - register a subsystem at /sys/devices/system/
Randy Dunlap78d79552012-01-21 11:02:28 -08001124 * @subsys: system subsystem
1125 * @groups: default attributes for the root device
Kay Sieversca22e562011-12-14 14:29:38 -08001126 *
1127 * All 'system' subsystems have a /sys/devices/system/<name> root device
1128 * with the name of the subsystem. The root device can carry subsystem-
1129 * wide attributes. All registered devices are below this single root
1130 * device and are named after the subsystem with a simple enumeration
Masanari Iidae2278672014-02-18 22:54:36 +09001131 * number appended. The registered devices are not explicitly named;
Kay Sieversca22e562011-12-14 14:29:38 -08001132 * only 'id' in the device needs to be set.
1133 *
1134 * Do not use this interface for anything new, it exists for compatibility
1135 * with bad ideas only. New subsystems should use plain subsystems; and
1136 * add the subsystem-wide attributes should be added to the subsystem
1137 * directory itself and not some create fake root-device placed in
1138 * /sys/devices/system/<name>.
1139 */
1140int subsys_system_register(struct bus_type *subsys,
1141 const struct attribute_group **groups)
1142{
Tejun Heod73ce002013-03-12 11:30:05 -07001143 return subsys_register(subsys, groups, &system_kset->kobj);
Kay Sieversca22e562011-12-14 14:29:38 -08001144}
1145EXPORT_SYMBOL_GPL(subsys_system_register);
1146
Tejun Heod73ce002013-03-12 11:30:05 -07001147/**
1148 * subsys_virtual_register - register a subsystem at /sys/devices/virtual/
1149 * @subsys: virtual subsystem
1150 * @groups: default attributes for the root device
1151 *
1152 * All 'virtual' subsystems have a /sys/devices/system/<name> root device
1153 * with the name of the subystem. The root device can carry subsystem-wide
1154 * attributes. All registered devices are below this single root device.
1155 * There's no restriction on device naming. This is for kernel software
1156 * constructs which need sysfs interface.
1157 */
1158int subsys_virtual_register(struct bus_type *subsys,
1159 const struct attribute_group **groups)
1160{
1161 struct kobject *virtual_dir;
1162
1163 virtual_dir = virtual_device_parent(NULL);
1164 if (!virtual_dir)
1165 return -ENOMEM;
1166
1167 return subsys_register(subsys, groups, virtual_dir);
1168}
Greg Kroah-Hartman1c04fc32013-05-10 09:14:04 -07001169EXPORT_SYMBOL_GPL(subsys_virtual_register);
Tejun Heod73ce002013-03-12 11:30:05 -07001170
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171int __init buses_init(void)
1172{
Greg Kroah-Hartman59a54832007-10-29 23:22:26 -05001173 bus_kset = kset_create_and_add("bus", &bus_uevent_ops, NULL);
1174 if (!bus_kset)
1175 return -ENOMEM;
Kay Sieversca22e562011-12-14 14:29:38 -08001176
1177 system_kset = kset_create_and_add("system", NULL, &devices_kset->kobj);
1178 if (!system_kset)
1179 return -ENOMEM;
1180
Greg Kroah-Hartman59a54832007-10-29 23:22:26 -05001181 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182}