blob: 886e9054999aa37dfa1adb44cacf4fbb2a761dc8 [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{
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700232 return sprintf(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 */
Greg Kroah-Hartmaned0617b2013-08-08 15:22:56 -0700636 printk(KERN_ERR "%s: driver_create_groups(%s) failed\n",
637 __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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800733 * find_bus - locate bus by name.
734 * @name: name of bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800736 * Call kset_find_obj() to iterate over list of buses to
737 * find a bus by name. Return bus if found.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800739 * Note that kset_find_obj increments bus' reference count.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 */
Adrian Bunk7e4ef082006-06-26 22:26:56 +0200741#if 0
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800742struct bus_type *find_bus(char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800744 struct kobject *k = kset_find_obj(bus_kset, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 return k ? to_bus(k) : NULL;
746}
Adrian Bunk7e4ef082006-06-26 22:26:56 +0200747#endif /* 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Greg Kroah-Hartman12478ba2013-08-08 15:22:57 -0700749static int bus_add_groups(struct bus_type *bus,
750 const struct attribute_group **groups)
751{
Greg Kroah-Hartman3e9b2ba2013-08-21 13:47:50 -0700752 return sysfs_create_groups(&bus->p->subsys.kobj, groups);
Greg Kroah-Hartman12478ba2013-08-08 15:22:57 -0700753}
754
755static void bus_remove_groups(struct bus_type *bus,
756 const struct attribute_group **groups)
757{
Greg Kroah-Hartman3e9b2ba2013-08-21 13:47:50 -0700758 sysfs_remove_groups(&bus->p->subsys.kobj, groups);
Greg Kroah-Hartman12478ba2013-08-08 15:22:57 -0700759}
760
James Bottomley34bb61f2005-09-06 16:56:51 -0700761static void klist_devices_get(struct klist_node *n)
762{
Greg Kroah-Hartmanae1b4172008-12-16 12:26:21 -0800763 struct device_private *dev_prv = to_device_private_bus(n);
764 struct device *dev = dev_prv->device;
James Bottomley34bb61f2005-09-06 16:56:51 -0700765
766 get_device(dev);
767}
768
769static void klist_devices_put(struct klist_node *n)
770{
Greg Kroah-Hartmanae1b4172008-12-16 12:26:21 -0800771 struct device_private *dev_prv = to_device_private_bus(n);
772 struct device *dev = dev_prv->device;
James Bottomley34bb61f2005-09-06 16:56:51 -0700773
774 put_device(dev);
775}
776
Kay Sievers7ac1cf42007-08-12 20:43:55 +0200777static ssize_t bus_uevent_store(struct bus_type *bus,
778 const char *buf, size_t count)
779{
Peter Rajnohadf44b472018-12-05 12:27:44 +0100780 int rc;
781
782 rc = kobject_synth_uevent(&bus->p->subsys.kobj, buf, count);
783 return rc ? rc : count;
Kay Sievers7ac1cf42007-08-12 20:43:55 +0200784}
Greg Kroah-Hartmana4723042018-10-29 16:31:26 +0100785/*
786 * "open code" the old BUS_ATTR() macro here. We want to use BUS_ATTR_WO()
787 * here, but can not use it as earlier in the file we have
788 * DEVICE_ATTR_WO(uevent), which would cause a clash with the with the store
789 * function name.
790 */
791static struct bus_attribute bus_attr_uevent = __ATTR(uevent, S_IWUSR, NULL,
792 bus_uevent_store);
Kay Sievers7ac1cf42007-08-12 20:43:55 +0200793
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794/**
Michal Hockobe871b72013-03-12 17:21:19 +0100795 * bus_register - register a driver-core subsystem
Randy Dunlap78d79552012-01-21 11:02:28 -0800796 * @bus: bus to register
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 *
Randy Dunlap78d79552012-01-21 11:02:28 -0800798 * Once we have that, we register the bus with the kobject
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800799 * infrastructure, then register the children subsystems it has:
Kay Sieversca22e562011-12-14 14:29:38 -0800800 * the devices and drivers that belong to the subsystem.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 */
Michal Hockobe871b72013-03-12 17:21:19 +0100802int bus_register(struct bus_type *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803{
804 int retval;
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100805 struct subsys_private *priv;
Michal Hockobe871b72013-03-12 17:21:19 +0100806 struct lock_class_key *key = &bus->lock_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Kay Sievers6b6e39a2010-11-15 23:13:18 +0100808 priv = kzalloc(sizeof(struct subsys_private), GFP_KERNEL);
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700809 if (!priv)
810 return -ENOMEM;
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +1000811
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700812 priv->bus = bus;
813 bus->p = priv;
814
815 BLOCKING_INIT_NOTIFIER_HEAD(&priv->bus_notifier);
816
817 retval = kobject_set_name(&priv->subsys.kobj, "%s", bus->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 if (retval)
819 goto out;
820
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700821 priv->subsys.kobj.kset = bus_kset;
822 priv->subsys.kobj.ktype = &bus_ktype;
823 priv->drivers_autoprobe = 1;
Greg Kroah-Hartmand6b05b82007-09-12 15:06:57 -0700824
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700825 retval = kset_register(&priv->subsys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 if (retval)
827 goto out;
828
Kay Sievers7ac1cf42007-08-12 20:43:55 +0200829 retval = bus_create_file(bus, &bus_attr_uevent);
830 if (retval)
831 goto bus_uevent_fail;
832
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700833 priv->devices_kset = kset_create_and_add("devices", NULL,
834 &priv->subsys.kobj);
835 if (!priv->devices_kset) {
Greg Kroah-Hartman3d899592007-11-01 13:31:26 -0700836 retval = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 goto bus_devices_fail;
Greg Kroah-Hartman3d899592007-11-01 13:31:26 -0700838 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700840 priv->drivers_kset = kset_create_and_add("drivers", NULL,
841 &priv->subsys.kobj);
842 if (!priv->drivers_kset) {
Greg Kroah-Hartman6dcec252007-11-01 13:31:26 -0700843 retval = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 goto bus_drivers_fail;
Greg Kroah-Hartman6dcec252007-11-01 13:31:26 -0700845 }
mochel@digitalimplant.org465c7a32005-03-21 11:49:14 -0800846
Kay Sieversca22e562011-12-14 14:29:38 -0800847 INIT_LIST_HEAD(&priv->interfaces);
848 __mutex_init(&priv->mutex, "subsys mutex", key);
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700849 klist_init(&priv->klist_devices, klist_devices_get, klist_devices_put);
850 klist_init(&priv->klist_drivers, NULL, NULL);
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100851
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100852 retval = add_probe_files(bus);
853 if (retval)
854 goto bus_probe_files_fail;
855
Greg Kroah-Hartman12478ba2013-08-08 15:22:57 -0700856 retval = bus_add_groups(bus, bus->bus_groups);
857 if (retval)
858 goto bus_groups_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800860 pr_debug("bus: '%s': registered\n", bus->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 return 0;
862
Greg Kroah-Hartman12478ba2013-08-08 15:22:57 -0700863bus_groups_fail:
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100864 remove_probe_files(bus);
865bus_probe_files_fail:
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700866 kset_unregister(bus->p->drivers_kset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867bus_drivers_fail:
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700868 kset_unregister(bus->p->devices_kset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869bus_devices_fail:
Kay Sievers7ac1cf42007-08-12 20:43:55 +0200870 bus_remove_file(bus, &bus_attr_uevent);
871bus_uevent_fail:
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700872 kset_unregister(&bus->p->subsys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873out:
Jike Song600c20f2010-07-15 17:43:54 +0800874 kfree(bus->p);
Dave Youngf48f3fe2009-02-14 21:23:22 +0800875 bus->p = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 return retval;
877}
Michal Hockobe871b72013-03-12 17:21:19 +0100878EXPORT_SYMBOL_GPL(bus_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800881 * bus_unregister - remove a bus from the system
882 * @bus: bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800884 * Unregister the child subsystems and the bus itself.
885 * Finally, we call bus_put() to release the refcount
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800887void bus_unregister(struct bus_type *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800889 pr_debug("bus: '%s': unregistering\n", bus->name);
Kay Sieversca22e562011-12-14 14:29:38 -0800890 if (bus->dev_root)
891 device_unregister(bus->dev_root);
Greg Kroah-Hartman12478ba2013-08-08 15:22:57 -0700892 bus_remove_groups(bus, bus->bus_groups);
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100893 remove_probe_files(bus);
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700894 kset_unregister(bus->p->drivers_kset);
895 kset_unregister(bus->p->devices_kset);
Kay Sievers7ac1cf42007-08-12 20:43:55 +0200896 bus_remove_file(bus, &bus_attr_uevent);
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700897 kset_unregister(&bus->p->subsys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898}
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800899EXPORT_SYMBOL_GPL(bus_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +1000901int bus_register_notifier(struct bus_type *bus, struct notifier_block *nb)
902{
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700903 return blocking_notifier_chain_register(&bus->p->bus_notifier, nb);
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +1000904}
905EXPORT_SYMBOL_GPL(bus_register_notifier);
906
907int bus_unregister_notifier(struct bus_type *bus, struct notifier_block *nb)
908{
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700909 return blocking_notifier_chain_unregister(&bus->p->bus_notifier, nb);
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +1000910}
911EXPORT_SYMBOL_GPL(bus_unregister_notifier);
912
Greg Kroah-Hartman0fed80f2007-11-01 19:41:16 -0700913struct kset *bus_get_kset(struct bus_type *bus)
914{
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700915 return &bus->p->subsys;
Greg Kroah-Hartman0fed80f2007-11-01 19:41:16 -0700916}
917EXPORT_SYMBOL_GPL(bus_get_kset);
918
Greg Kroah-Hartmanb2490722007-11-01 19:41:16 -0700919struct klist *bus_get_device_klist(struct bus_type *bus)
920{
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700921 return &bus->p->klist_devices;
Greg Kroah-Hartmanb2490722007-11-01 19:41:16 -0700922}
923EXPORT_SYMBOL_GPL(bus_get_device_klist);
924
Greg Kroah-Hartman99178b02008-08-26 11:00:57 -0500925/*
Robert P. J. Daydca25eb2010-11-21 08:15:01 -0500926 * Yes, this forcibly breaks the klist abstraction temporarily. It
Greg Kroah-Hartman99178b02008-08-26 11:00:57 -0500927 * just wants to sort the klist, not change reference counts and
928 * take/drop locks rapidly in the process. It does all this while
929 * holding the lock for the list, so objects can't otherwise be
930 * added/removed while we're swizzling.
931 */
932static void device_insertion_sort_klist(struct device *a, struct list_head *list,
933 int (*compare)(const struct device *a,
934 const struct device *b))
935{
Greg Kroah-Hartman99178b02008-08-26 11:00:57 -0500936 struct klist_node *n;
Greg Kroah-Hartmanae1b4172008-12-16 12:26:21 -0800937 struct device_private *dev_prv;
Greg Kroah-Hartman99178b02008-08-26 11:00:57 -0500938 struct device *b;
939
Geliang Tang4c627852016-01-05 23:03:37 +0800940 list_for_each_entry(n, list, n_node) {
Greg Kroah-Hartmanae1b4172008-12-16 12:26:21 -0800941 dev_prv = to_device_private_bus(n);
942 b = dev_prv->device;
Greg Kroah-Hartman99178b02008-08-26 11:00:57 -0500943 if (compare(a, b) <= 0) {
Greg Kroah-Hartmanae1b4172008-12-16 12:26:21 -0800944 list_move_tail(&a->p->knode_bus.n_node,
945 &b->p->knode_bus.n_node);
Greg Kroah-Hartman99178b02008-08-26 11:00:57 -0500946 return;
947 }
948 }
Greg Kroah-Hartmanae1b4172008-12-16 12:26:21 -0800949 list_move_tail(&a->p->knode_bus.n_node, list);
Greg Kroah-Hartman99178b02008-08-26 11:00:57 -0500950}
951
952void bus_sort_breadthfirst(struct bus_type *bus,
953 int (*compare)(const struct device *a,
954 const struct device *b))
955{
956 LIST_HEAD(sorted_devices);
Geliang Tang4c627852016-01-05 23:03:37 +0800957 struct klist_node *n, *tmp;
Greg Kroah-Hartmanae1b4172008-12-16 12:26:21 -0800958 struct device_private *dev_prv;
Greg Kroah-Hartman99178b02008-08-26 11:00:57 -0500959 struct device *dev;
960 struct klist *device_klist;
961
962 device_klist = bus_get_device_klist(bus);
963
964 spin_lock(&device_klist->k_lock);
Geliang Tang4c627852016-01-05 23:03:37 +0800965 list_for_each_entry_safe(n, tmp, &device_klist->k_list, n_node) {
Greg Kroah-Hartmanae1b4172008-12-16 12:26:21 -0800966 dev_prv = to_device_private_bus(n);
967 dev = dev_prv->device;
Greg Kroah-Hartman99178b02008-08-26 11:00:57 -0500968 device_insertion_sort_klist(dev, &sorted_devices, compare);
969 }
970 list_splice(&sorted_devices, &device_klist->k_list);
971 spin_unlock(&device_klist->k_lock);
972}
973EXPORT_SYMBOL_GPL(bus_sort_breadthfirst);
974
Kay Sieversca22e562011-12-14 14:29:38 -0800975/**
976 * subsys_dev_iter_init - initialize subsys device iterator
977 * @iter: subsys iterator to initialize
978 * @subsys: the subsys we wanna iterate over
979 * @start: the device to start iterating from, if any
980 * @type: device_type of the devices to iterate over, NULL for all
981 *
982 * Initialize subsys iterator @iter such that it iterates over devices
983 * of @subsys. If @start is set, the list iteration will start there,
984 * otherwise if it is NULL, the iteration starts at the beginning of
985 * the list.
986 */
Greg Kroah-Hartman7cd9c9b2012-04-19 19:17:30 -0700987void subsys_dev_iter_init(struct subsys_dev_iter *iter, struct bus_type *subsys,
988 struct device *start, const struct device_type *type)
Kay Sieversca22e562011-12-14 14:29:38 -0800989{
990 struct klist_node *start_knode = NULL;
991
992 if (start)
993 start_knode = &start->p->knode_bus;
Greg Kroah-Hartman7cd9c9b2012-04-19 19:17:30 -0700994 klist_iter_init_node(&subsys->p->klist_devices, &iter->ki, start_knode);
995 iter->type = type;
Kay Sieversca22e562011-12-14 14:29:38 -0800996}
997EXPORT_SYMBOL_GPL(subsys_dev_iter_init);
998
999/**
1000 * subsys_dev_iter_next - iterate to the next device
1001 * @iter: subsys iterator to proceed
1002 *
1003 * Proceed @iter to the next device and return it. Returns NULL if
1004 * iteration is complete.
1005 *
1006 * The returned device is referenced and won't be released till
1007 * iterator is proceed to the next device or exited. The caller is
1008 * free to do whatever it wants to do with the device including
1009 * calling back into subsys code.
1010 */
1011struct device *subsys_dev_iter_next(struct subsys_dev_iter *iter)
1012{
1013 struct klist_node *knode;
1014 struct device *dev;
1015
1016 for (;;) {
1017 knode = klist_next(&iter->ki);
1018 if (!knode)
1019 return NULL;
Geliang Tang371fd7a2016-01-05 23:03:38 +08001020 dev = to_device_private_bus(knode)->device;
Kay Sieversca22e562011-12-14 14:29:38 -08001021 if (!iter->type || iter->type == dev->type)
1022 return dev;
1023 }
1024}
1025EXPORT_SYMBOL_GPL(subsys_dev_iter_next);
1026
1027/**
1028 * subsys_dev_iter_exit - finish iteration
1029 * @iter: subsys iterator to finish
1030 *
1031 * Finish an iteration. Always call this function after iteration is
1032 * complete whether the iteration ran till the end or not.
1033 */
1034void subsys_dev_iter_exit(struct subsys_dev_iter *iter)
1035{
1036 klist_iter_exit(&iter->ki);
1037}
1038EXPORT_SYMBOL_GPL(subsys_dev_iter_exit);
1039
1040int subsys_interface_register(struct subsys_interface *sif)
1041{
1042 struct bus_type *subsys;
1043 struct subsys_dev_iter iter;
1044 struct device *dev;
1045
1046 if (!sif || !sif->subsys)
1047 return -ENODEV;
1048
1049 subsys = bus_get(sif->subsys);
1050 if (!subsys)
1051 return -EINVAL;
1052
1053 mutex_lock(&subsys->p->mutex);
1054 list_add_tail(&sif->node, &subsys->p->interfaces);
1055 if (sif->add_dev) {
1056 subsys_dev_iter_init(&iter, subsys, NULL, NULL);
1057 while ((dev = subsys_dev_iter_next(&iter)))
1058 sif->add_dev(dev, sif);
1059 subsys_dev_iter_exit(&iter);
1060 }
1061 mutex_unlock(&subsys->p->mutex);
1062
1063 return 0;
1064}
1065EXPORT_SYMBOL_GPL(subsys_interface_register);
1066
1067void subsys_interface_unregister(struct subsys_interface *sif)
1068{
Jonghwan Choi2b315942012-01-14 11:06:03 +09001069 struct bus_type *subsys;
Kay Sieversca22e562011-12-14 14:29:38 -08001070 struct subsys_dev_iter iter;
1071 struct device *dev;
1072
Jonghwan Choi2b315942012-01-14 11:06:03 +09001073 if (!sif || !sif->subsys)
Kay Sieversca22e562011-12-14 14:29:38 -08001074 return;
1075
Jonghwan Choi2b315942012-01-14 11:06:03 +09001076 subsys = sif->subsys;
1077
Kay Sieversca22e562011-12-14 14:29:38 -08001078 mutex_lock(&subsys->p->mutex);
1079 list_del_init(&sif->node);
1080 if (sif->remove_dev) {
1081 subsys_dev_iter_init(&iter, subsys, NULL, NULL);
1082 while ((dev = subsys_dev_iter_next(&iter)))
1083 sif->remove_dev(dev, sif);
1084 subsys_dev_iter_exit(&iter);
1085 }
1086 mutex_unlock(&subsys->p->mutex);
1087
1088 bus_put(subsys);
1089}
1090EXPORT_SYMBOL_GPL(subsys_interface_unregister);
1091
1092static void system_root_device_release(struct device *dev)
1093{
1094 kfree(dev);
1095}
Tejun Heod73ce002013-03-12 11:30:05 -07001096
1097static int subsys_register(struct bus_type *subsys,
1098 const struct attribute_group **groups,
1099 struct kobject *parent_of_root)
1100{
1101 struct device *dev;
1102 int err;
1103
1104 err = bus_register(subsys);
1105 if (err < 0)
1106 return err;
1107
1108 dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1109 if (!dev) {
1110 err = -ENOMEM;
1111 goto err_dev;
1112 }
1113
1114 err = dev_set_name(dev, "%s", subsys->name);
1115 if (err < 0)
1116 goto err_name;
1117
1118 dev->kobj.parent = parent_of_root;
1119 dev->groups = groups;
1120 dev->release = system_root_device_release;
1121
1122 err = device_register(dev);
1123 if (err < 0)
1124 goto err_dev_reg;
1125
1126 subsys->dev_root = dev;
1127 return 0;
1128
1129err_dev_reg:
1130 put_device(dev);
1131 dev = NULL;
1132err_name:
1133 kfree(dev);
1134err_dev:
1135 bus_unregister(subsys);
1136 return err;
1137}
1138
Kay Sieversca22e562011-12-14 14:29:38 -08001139/**
1140 * subsys_system_register - register a subsystem at /sys/devices/system/
Randy Dunlap78d79552012-01-21 11:02:28 -08001141 * @subsys: system subsystem
1142 * @groups: default attributes for the root device
Kay Sieversca22e562011-12-14 14:29:38 -08001143 *
1144 * All 'system' subsystems have a /sys/devices/system/<name> root device
1145 * with the name of the subsystem. The root device can carry subsystem-
1146 * wide attributes. All registered devices are below this single root
1147 * device and are named after the subsystem with a simple enumeration
Masanari Iidae2278672014-02-18 22:54:36 +09001148 * number appended. The registered devices are not explicitly named;
Kay Sieversca22e562011-12-14 14:29:38 -08001149 * only 'id' in the device needs to be set.
1150 *
1151 * Do not use this interface for anything new, it exists for compatibility
1152 * with bad ideas only. New subsystems should use plain subsystems; and
1153 * add the subsystem-wide attributes should be added to the subsystem
1154 * directory itself and not some create fake root-device placed in
1155 * /sys/devices/system/<name>.
1156 */
1157int subsys_system_register(struct bus_type *subsys,
1158 const struct attribute_group **groups)
1159{
Tejun Heod73ce002013-03-12 11:30:05 -07001160 return subsys_register(subsys, groups, &system_kset->kobj);
Kay Sieversca22e562011-12-14 14:29:38 -08001161}
1162EXPORT_SYMBOL_GPL(subsys_system_register);
1163
Tejun Heod73ce002013-03-12 11:30:05 -07001164/**
1165 * subsys_virtual_register - register a subsystem at /sys/devices/virtual/
1166 * @subsys: virtual subsystem
1167 * @groups: default attributes for the root device
1168 *
1169 * All 'virtual' subsystems have a /sys/devices/system/<name> root device
1170 * with the name of the subystem. The root device can carry subsystem-wide
1171 * attributes. All registered devices are below this single root device.
1172 * There's no restriction on device naming. This is for kernel software
1173 * constructs which need sysfs interface.
1174 */
1175int subsys_virtual_register(struct bus_type *subsys,
1176 const struct attribute_group **groups)
1177{
1178 struct kobject *virtual_dir;
1179
1180 virtual_dir = virtual_device_parent(NULL);
1181 if (!virtual_dir)
1182 return -ENOMEM;
1183
1184 return subsys_register(subsys, groups, virtual_dir);
1185}
Greg Kroah-Hartman1c04fc32013-05-10 09:14:04 -07001186EXPORT_SYMBOL_GPL(subsys_virtual_register);
Tejun Heod73ce002013-03-12 11:30:05 -07001187
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188int __init buses_init(void)
1189{
Greg Kroah-Hartman59a54832007-10-29 23:22:26 -05001190 bus_kset = kset_create_and_add("bus", &bus_uevent_ops, NULL);
1191 if (!bus_kset)
1192 return -ENOMEM;
Kay Sieversca22e562011-12-14 14:29:38 -08001193
1194 system_kset = kset_create_and_add("system", NULL, &devices_kset->kobj);
1195 if (!system_kset)
1196 return -ENOMEM;
1197
Greg Kroah-Hartman59a54832007-10-29 23:22:26 -05001198 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199}