xref: /linux/drivers/base/bus.c (revision 37e98d9bedb50644654fd196e38acad49903fadc)
1989d42e8SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * bus.c - bus driver management
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Copyright (c) 2002-3 Patrick Mochel
61da177e4SLinus Torvalds  * Copyright (c) 2002-3 Open Source Development Labs
7e5dd1278SGreg Kroah-Hartman  * Copyright (c) 2007 Greg Kroah-Hartman <gregkh@suse.de>
8e5dd1278SGreg Kroah-Hartman  * Copyright (c) 2007 Novell Inc.
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
11765230b5SDmitry Torokhov #include <linux/async.h>
125aee2bf2SGreg Kroah-Hartman #include <linux/device/bus.h>
131da177e4SLinus Torvalds #include <linux/device.h>
141da177e4SLinus Torvalds #include <linux/module.h>
151da177e4SLinus Torvalds #include <linux/errno.h>
165a0e3ad6STejun Heo #include <linux/slab.h>
171da177e4SLinus Torvalds #include <linux/init.h>
181da177e4SLinus Torvalds #include <linux/string.h>
19ca22e56dSKay Sievers #include <linux/mutex.h>
2063967685SGreg Kroah-Hartman #include <linux/sysfs.h>
211da177e4SLinus Torvalds #include "base.h"
221da177e4SLinus Torvalds #include "power/power.h"
231da177e4SLinus Torvalds 
24ca22e56dSKay Sievers /* /sys/devices/system */
2597ec448aSH Hartley Sweeten static struct kset *system_kset;
26ca22e56dSKay Sievers 
271da177e4SLinus Torvalds #define to_bus_attr(_attr) container_of(_attr, struct bus_attribute, attr)
281da177e4SLinus Torvalds 
291da177e4SLinus Torvalds /*
301da177e4SLinus Torvalds  * sysfs bindings for drivers
311da177e4SLinus Torvalds  */
321da177e4SLinus Torvalds 
331da177e4SLinus Torvalds #define to_drv_attr(_attr) container_of(_attr, struct driver_attribute, attr)
341da177e4SLinus Torvalds 
354f4b3743SDaniel Vetter #define DRIVER_ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) \
364f4b3743SDaniel Vetter 	struct driver_attribute driver_attr_##_name =		\
374f4b3743SDaniel Vetter 		__ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store)
381da177e4SLinus Torvalds 
39b8c5cec2SKay Sievers static int __must_check bus_rescan_devices_helper(struct device *dev,
40b8c5cec2SKay Sievers 						void *data);
41b8c5cec2SKay Sievers 
425901d014SGreg Kroah-Hartman static struct bus_type *bus_get(struct bus_type *bus)
435901d014SGreg Kroah-Hartman {
44c6f7e72aSGreg Kroah-Hartman 	if (bus) {
45c6f7e72aSGreg Kroah-Hartman 		kset_get(&bus->p->subsys);
46c6f7e72aSGreg Kroah-Hartman 		return bus;
47c6f7e72aSGreg Kroah-Hartman 	}
48c6f7e72aSGreg Kroah-Hartman 	return NULL;
495901d014SGreg Kroah-Hartman }
505901d014SGreg Kroah-Hartman 
51fc1ede58SGreg Kroah-Hartman static void bus_put(struct bus_type *bus)
52fc1ede58SGreg Kroah-Hartman {
53c6f7e72aSGreg Kroah-Hartman 	if (bus)
54c6f7e72aSGreg Kroah-Hartman 		kset_put(&bus->p->subsys);
55fc1ede58SGreg Kroah-Hartman }
56fc1ede58SGreg Kroah-Hartman 
574a3ad20cSGreg Kroah-Hartman static ssize_t drv_attr_show(struct kobject *kobj, struct attribute *attr,
584a3ad20cSGreg Kroah-Hartman 			     char *buf)
591da177e4SLinus Torvalds {
601da177e4SLinus Torvalds 	struct driver_attribute *drv_attr = to_drv_attr(attr);
61e5dd1278SGreg Kroah-Hartman 	struct driver_private *drv_priv = to_driver(kobj);
624a0c20bfSDmitry Torokhov 	ssize_t ret = -EIO;
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds 	if (drv_attr->show)
65e5dd1278SGreg Kroah-Hartman 		ret = drv_attr->show(drv_priv->driver, buf);
661da177e4SLinus Torvalds 	return ret;
671da177e4SLinus Torvalds }
681da177e4SLinus Torvalds 
694a3ad20cSGreg Kroah-Hartman static ssize_t drv_attr_store(struct kobject *kobj, struct attribute *attr,
701da177e4SLinus Torvalds 			      const char *buf, size_t count)
711da177e4SLinus Torvalds {
721da177e4SLinus Torvalds 	struct driver_attribute *drv_attr = to_drv_attr(attr);
73e5dd1278SGreg Kroah-Hartman 	struct driver_private *drv_priv = to_driver(kobj);
744a0c20bfSDmitry Torokhov 	ssize_t ret = -EIO;
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds 	if (drv_attr->store)
77e5dd1278SGreg Kroah-Hartman 		ret = drv_attr->store(drv_priv->driver, buf, count);
781da177e4SLinus Torvalds 	return ret;
791da177e4SLinus Torvalds }
801da177e4SLinus Torvalds 
8152cf25d0SEmese Revfy static const struct sysfs_ops driver_sysfs_ops = {
821da177e4SLinus Torvalds 	.show	= drv_attr_show,
831da177e4SLinus Torvalds 	.store	= drv_attr_store,
841da177e4SLinus Torvalds };
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds static void driver_release(struct kobject *kobj)
871da177e4SLinus Torvalds {
88e5dd1278SGreg Kroah-Hartman 	struct driver_private *drv_priv = to_driver(kobj);
89e5dd1278SGreg Kroah-Hartman 
902b3a302aSHarvey Harrison 	pr_debug("driver: '%s': %s\n", kobject_name(kobj), __func__);
91e5dd1278SGreg Kroah-Hartman 	kfree(drv_priv);
921da177e4SLinus Torvalds }
931da177e4SLinus Torvalds 
94a1148fb0SGreg Kroah-Hartman static struct kobj_type driver_ktype = {
951da177e4SLinus Torvalds 	.sysfs_ops	= &driver_sysfs_ops,
961da177e4SLinus Torvalds 	.release	= driver_release,
971da177e4SLinus Torvalds };
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds /*
1001da177e4SLinus Torvalds  * sysfs bindings for buses
1011da177e4SLinus Torvalds  */
1024a3ad20cSGreg Kroah-Hartman static ssize_t bus_attr_show(struct kobject *kobj, struct attribute *attr,
1034a3ad20cSGreg Kroah-Hartman 			     char *buf)
1041da177e4SLinus Torvalds {
1051da177e4SLinus Torvalds 	struct bus_attribute *bus_attr = to_bus_attr(attr);
1066b6e39a6SKay Sievers 	struct subsys_private *subsys_priv = to_subsys_private(kobj);
1071da177e4SLinus Torvalds 	ssize_t ret = 0;
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds 	if (bus_attr->show)
1106b6e39a6SKay Sievers 		ret = bus_attr->show(subsys_priv->bus, buf);
1111da177e4SLinus Torvalds 	return ret;
1121da177e4SLinus Torvalds }
1131da177e4SLinus Torvalds 
1144a3ad20cSGreg Kroah-Hartman static ssize_t bus_attr_store(struct kobject *kobj, struct attribute *attr,
1151da177e4SLinus Torvalds 			      const char *buf, size_t count)
1161da177e4SLinus Torvalds {
1171da177e4SLinus Torvalds 	struct bus_attribute *bus_attr = to_bus_attr(attr);
1186b6e39a6SKay Sievers 	struct subsys_private *subsys_priv = to_subsys_private(kobj);
1191da177e4SLinus Torvalds 	ssize_t ret = 0;
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds 	if (bus_attr->store)
1226b6e39a6SKay Sievers 		ret = bus_attr->store(subsys_priv->bus, buf, count);
1231da177e4SLinus Torvalds 	return ret;
1241da177e4SLinus Torvalds }
1251da177e4SLinus Torvalds 
12652cf25d0SEmese Revfy static const struct sysfs_ops bus_sysfs_ops = {
1271da177e4SLinus Torvalds 	.show	= bus_attr_show,
1281da177e4SLinus Torvalds 	.store	= bus_attr_store,
1291da177e4SLinus Torvalds };
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds int bus_create_file(struct bus_type *bus, struct bus_attribute *attr)
1321da177e4SLinus Torvalds {
1331da177e4SLinus Torvalds 	int error;
1345901d014SGreg Kroah-Hartman 	if (bus_get(bus)) {
135c6f7e72aSGreg Kroah-Hartman 		error = sysfs_create_file(&bus->p->subsys.kobj, &attr->attr);
136fc1ede58SGreg Kroah-Hartman 		bus_put(bus);
1371da177e4SLinus Torvalds 	} else
1381da177e4SLinus Torvalds 		error = -EINVAL;
1391da177e4SLinus Torvalds 	return error;
1401da177e4SLinus Torvalds }
1414a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_create_file);
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds void bus_remove_file(struct bus_type *bus, struct bus_attribute *attr)
1441da177e4SLinus Torvalds {
1455901d014SGreg Kroah-Hartman 	if (bus_get(bus)) {
146c6f7e72aSGreg Kroah-Hartman 		sysfs_remove_file(&bus->p->subsys.kobj, &attr->attr);
147fc1ede58SGreg Kroah-Hartman 		bus_put(bus);
1481da177e4SLinus Torvalds 	}
1491da177e4SLinus Torvalds }
1504a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_remove_file);
1511da177e4SLinus Torvalds 
152174be70bSBart Van Assche static void bus_release(struct kobject *kobj)
153174be70bSBart Van Assche {
154371fd7a2SGeliang Tang 	struct subsys_private *priv = to_subsys_private(kobj);
155174be70bSBart Van Assche 	struct bus_type *bus = priv->bus;
156174be70bSBart Van Assche 
157*37e98d9bSGreg Kroah-Hartman 	lockdep_unregister_key(&priv->lock_key);
158174be70bSBart Van Assche 	kfree(priv);
159174be70bSBart Van Assche 	bus->p = NULL;
160174be70bSBart Van Assche }
161174be70bSBart Van Assche 
16280f03e34SKay Sievers static struct kobj_type bus_ktype = {
1631da177e4SLinus Torvalds 	.sysfs_ops	= &bus_sysfs_ops,
164174be70bSBart Van Assche 	.release	= bus_release,
1651da177e4SLinus Torvalds };
1661da177e4SLinus Torvalds 
167c45a88bbSGreg Kroah-Hartman static int bus_uevent_filter(const struct kobject *kobj)
16880f03e34SKay Sievers {
169ee6d3dd4SWedson Almeida Filho 	const struct kobj_type *ktype = get_ktype(kobj);
17080f03e34SKay Sievers 
17180f03e34SKay Sievers 	if (ktype == &bus_ktype)
17280f03e34SKay Sievers 		return 1;
17380f03e34SKay Sievers 	return 0;
17480f03e34SKay Sievers }
17580f03e34SKay Sievers 
1769cd43611SEmese Revfy static const struct kset_uevent_ops bus_uevent_ops = {
17780f03e34SKay Sievers 	.filter = bus_uevent_filter,
17880f03e34SKay Sievers };
17980f03e34SKay Sievers 
18059a54833SGreg Kroah-Hartman static struct kset *bus_kset;
1811da177e4SLinus Torvalds 
1822b08c8d0SAlan Stern /* Manually detach a device from its associated driver. */
1832581c9ccSGreg Kroah-Hartman static ssize_t unbind_store(struct device_driver *drv, const char *buf,
1842581c9ccSGreg Kroah-Hartman 			    size_t count)
185151ef38fSGreg Kroah-Hartman {
1865901d014SGreg Kroah-Hartman 	struct bus_type *bus = bus_get(drv->bus);
187151ef38fSGreg Kroah-Hartman 	struct device *dev;
188151ef38fSGreg Kroah-Hartman 	int err = -ENODEV;
189151ef38fSGreg Kroah-Hartman 
1901f9ffc04SGreg Kroah-Hartman 	dev = bus_find_device_by_name(bus, NULL, buf);
1912b08c8d0SAlan Stern 	if (dev && dev->driver == drv) {
192ed88747cSAlexander Duyck 		device_driver_detach(dev);
193151ef38fSGreg Kroah-Hartman 		err = count;
194151ef38fSGreg Kroah-Hartman 	}
1952b08c8d0SAlan Stern 	put_device(dev);
196fc1ede58SGreg Kroah-Hartman 	bus_put(bus);
197151ef38fSGreg Kroah-Hartman 	return err;
198151ef38fSGreg Kroah-Hartman }
19916b0dd40SJinchao Wang static DRIVER_ATTR_IGNORE_LOCKDEP(unbind, 0200, NULL, unbind_store);
200151ef38fSGreg Kroah-Hartman 
201afdce75fSGreg Kroah-Hartman /*
202afdce75fSGreg Kroah-Hartman  * Manually attach a device to a driver.
203afdce75fSGreg Kroah-Hartman  * Note: the driver must want to bind to the device,
204afdce75fSGreg Kroah-Hartman  * it is not possible to override the driver's id table.
205afdce75fSGreg Kroah-Hartman  */
2062581c9ccSGreg Kroah-Hartman static ssize_t bind_store(struct device_driver *drv, const char *buf,
2072581c9ccSGreg Kroah-Hartman 			  size_t count)
208afdce75fSGreg Kroah-Hartman {
2095901d014SGreg Kroah-Hartman 	struct bus_type *bus = bus_get(drv->bus);
210afdce75fSGreg Kroah-Hartman 	struct device *dev;
211afdce75fSGreg Kroah-Hartman 	int err = -ENODEV;
212afdce75fSGreg Kroah-Hartman 
2131f9ffc04SGreg Kroah-Hartman 	dev = bus_find_device_by_name(bus, NULL, buf);
214204db60cSJason Gunthorpe 	if (dev && driver_match_device(drv, dev)) {
215ed88747cSAlexander Duyck 		err = device_driver_attach(drv, dev);
216ef6dcbddSChristoph Hellwig 		if (!err) {
2174a3ad20cSGreg Kroah-Hartman 			/* success */
21837225401SRyan Wilson 			err = count;
219afdce75fSGreg Kroah-Hartman 		}
2204a3ad20cSGreg Kroah-Hartman 	}
2212b08c8d0SAlan Stern 	put_device(dev);
222fc1ede58SGreg Kroah-Hartman 	bus_put(bus);
223afdce75fSGreg Kroah-Hartman 	return err;
224afdce75fSGreg Kroah-Hartman }
22516b0dd40SJinchao Wang static DRIVER_ATTR_IGNORE_LOCKDEP(bind, 0200, NULL, bind_store);
226afdce75fSGreg Kroah-Hartman 
2272e7189b6SGreg Kroah-Hartman static ssize_t drivers_autoprobe_show(struct bus_type *bus, char *buf)
228b8c5cec2SKay Sievers {
229948b3edbSJoe Perches 	return sysfs_emit(buf, "%d\n", bus->p->drivers_autoprobe);
230b8c5cec2SKay Sievers }
231b8c5cec2SKay Sievers 
2322e7189b6SGreg Kroah-Hartman static ssize_t drivers_autoprobe_store(struct bus_type *bus,
233b8c5cec2SKay Sievers 				       const char *buf, size_t count)
234b8c5cec2SKay Sievers {
235b8c5cec2SKay Sievers 	if (buf[0] == '0')
236c6f7e72aSGreg Kroah-Hartman 		bus->p->drivers_autoprobe = 0;
237b8c5cec2SKay Sievers 	else
238c6f7e72aSGreg Kroah-Hartman 		bus->p->drivers_autoprobe = 1;
239b8c5cec2SKay Sievers 	return count;
240b8c5cec2SKay Sievers }
241b8c5cec2SKay Sievers 
2422e7189b6SGreg Kroah-Hartman static ssize_t drivers_probe_store(struct bus_type *bus,
243b8c5cec2SKay Sievers 				   const char *buf, size_t count)
244b8c5cec2SKay Sievers {
245b8c5cec2SKay Sievers 	struct device *dev;
2460372ffb3SAlex Williamson 	int err = -EINVAL;
247b8c5cec2SKay Sievers 
2481f9ffc04SGreg Kroah-Hartman 	dev = bus_find_device_by_name(bus, NULL, buf);
249b8c5cec2SKay Sievers 	if (!dev)
250b8c5cec2SKay Sievers 		return -ENODEV;
2510372ffb3SAlex Williamson 	if (bus_rescan_devices_helper(dev, NULL) == 0)
2520372ffb3SAlex Williamson 		err = count;
2530372ffb3SAlex Williamson 	put_device(dev);
2540372ffb3SAlex Williamson 	return err;
255b8c5cec2SKay Sievers }
256151ef38fSGreg Kroah-Hartman 
257465c7a3aSmochel@digitalimplant.org static struct device *next_device(struct klist_iter *i)
258465c7a3aSmochel@digitalimplant.org {
259465c7a3aSmochel@digitalimplant.org 	struct klist_node *n = klist_next(i);
260ae1b4171SGreg Kroah-Hartman 	struct device *dev = NULL;
261ae1b4171SGreg Kroah-Hartman 	struct device_private *dev_prv;
262ae1b4171SGreg Kroah-Hartman 
263ae1b4171SGreg Kroah-Hartman 	if (n) {
264ae1b4171SGreg Kroah-Hartman 		dev_prv = to_device_private_bus(n);
265ae1b4171SGreg Kroah-Hartman 		dev = dev_prv->device;
266ae1b4171SGreg Kroah-Hartman 	}
267ae1b4171SGreg Kroah-Hartman 	return dev;
268465c7a3aSmochel@digitalimplant.org }
269465c7a3aSmochel@digitalimplant.org 
2701da177e4SLinus Torvalds /**
2711da177e4SLinus Torvalds  * bus_for_each_dev - device iterator.
2721da177e4SLinus Torvalds  * @bus: bus type.
2731da177e4SLinus Torvalds  * @start: device to start iterating from.
2741da177e4SLinus Torvalds  * @data: data for the callback.
2751da177e4SLinus Torvalds  * @fn: function to be called for each device.
2761da177e4SLinus Torvalds  *
2771da177e4SLinus Torvalds  * Iterate over @bus's list of devices, and call @fn for each,
2781da177e4SLinus Torvalds  * passing it @data. If @start is not NULL, we use that device to
2791da177e4SLinus Torvalds  * begin iterating from.
2801da177e4SLinus Torvalds  *
2811da177e4SLinus Torvalds  * We check the return of @fn each time. If it returns anything
2821da177e4SLinus Torvalds  * other than 0, we break out and return that value.
2831da177e4SLinus Torvalds  *
2841da177e4SLinus Torvalds  * NOTE: The device that returns a non-zero value is not retained
2851da177e4SLinus Torvalds  * in any way, nor is its refcount incremented. If the caller needs
2860fa1b0a1SAlex Chiang  * to retain this data, it should do so, and increment the reference
2871da177e4SLinus Torvalds  * count in the supplied callback.
2881da177e4SLinus Torvalds  */
2891da177e4SLinus Torvalds int bus_for_each_dev(struct bus_type *bus, struct device *start,
2901da177e4SLinus Torvalds 		     void *data, int (*fn)(struct device *, void *))
2911da177e4SLinus Torvalds {
292465c7a3aSmochel@digitalimplant.org 	struct klist_iter i;
293465c7a3aSmochel@digitalimplant.org 	struct device *dev;
294465c7a3aSmochel@digitalimplant.org 	int error = 0;
2951da177e4SLinus Torvalds 
2964fa3e78bSBjorn Helgaas 	if (!bus || !bus->p)
297465c7a3aSmochel@digitalimplant.org 		return -EINVAL;
298465c7a3aSmochel@digitalimplant.org 
2997cd9c9bbSGreg Kroah-Hartman 	klist_iter_init_node(&bus->p->klist_devices, &i,
300ae1b4171SGreg Kroah-Hartman 			     (start ? &start->p->knode_bus : NULL));
30193ead7c9SGimcuan Hui 	while (!error && (dev = next_device(&i)))
302465c7a3aSmochel@digitalimplant.org 		error = fn(dev, data);
303465c7a3aSmochel@digitalimplant.org 	klist_iter_exit(&i);
304465c7a3aSmochel@digitalimplant.org 	return error;
3051da177e4SLinus Torvalds }
3064a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_for_each_dev);
3071da177e4SLinus Torvalds 
3080edb5860SCornelia Huck /**
3090edb5860SCornelia Huck  * bus_find_device - device iterator for locating a particular device.
3100edb5860SCornelia Huck  * @bus: bus type
3110edb5860SCornelia Huck  * @start: Device to begin with
3120edb5860SCornelia Huck  * @data: Data to pass to match function
3130edb5860SCornelia Huck  * @match: Callback function to check device
3140edb5860SCornelia Huck  *
3150edb5860SCornelia Huck  * This is similar to the bus_for_each_dev() function above, but it
3160edb5860SCornelia Huck  * returns a reference to a device that is 'found' for later use, as
3170edb5860SCornelia Huck  * determined by the @match callback.
3180edb5860SCornelia Huck  *
3190edb5860SCornelia Huck  * The callback should return 0 if the device doesn't match and non-zero
3200edb5860SCornelia Huck  * if it does.  If the callback returns non-zero, this function will
3210edb5860SCornelia Huck  * return to the caller and not iterate over any more devices.
3220edb5860SCornelia Huck  */
3230edb5860SCornelia Huck struct device *bus_find_device(struct bus_type *bus,
324418e3ea1SSuzuki K Poulose 			       struct device *start, const void *data,
325418e3ea1SSuzuki K Poulose 			       int (*match)(struct device *dev, const void *data))
3260edb5860SCornelia Huck {
3270edb5860SCornelia Huck 	struct klist_iter i;
3280edb5860SCornelia Huck 	struct device *dev;
3290edb5860SCornelia Huck 
3304fa3e78bSBjorn Helgaas 	if (!bus || !bus->p)
3310edb5860SCornelia Huck 		return NULL;
3320edb5860SCornelia Huck 
3337cd9c9bbSGreg Kroah-Hartman 	klist_iter_init_node(&bus->p->klist_devices, &i,
3347cd9c9bbSGreg Kroah-Hartman 			     (start ? &start->p->knode_bus : NULL));
3350edb5860SCornelia Huck 	while ((dev = next_device(&i)))
3360edb5860SCornelia Huck 		if (match(dev, data) && get_device(dev))
3370edb5860SCornelia Huck 			break;
3380edb5860SCornelia Huck 	klist_iter_exit(&i);
3390edb5860SCornelia Huck 	return dev;
3400edb5860SCornelia Huck }
3414a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_find_device);
34238fdac3cSmochel@digitalimplant.org 
34338fdac3cSmochel@digitalimplant.org static struct device_driver *next_driver(struct klist_iter *i)
34438fdac3cSmochel@digitalimplant.org {
34538fdac3cSmochel@digitalimplant.org 	struct klist_node *n = klist_next(i);
346e5dd1278SGreg Kroah-Hartman 	struct driver_private *drv_priv;
347e5dd1278SGreg Kroah-Hartman 
348e5dd1278SGreg Kroah-Hartman 	if (n) {
349e5dd1278SGreg Kroah-Hartman 		drv_priv = container_of(n, struct driver_private, knode_bus);
350e5dd1278SGreg Kroah-Hartman 		return drv_priv->driver;
351e5dd1278SGreg Kroah-Hartman 	}
352e5dd1278SGreg Kroah-Hartman 	return NULL;
35338fdac3cSmochel@digitalimplant.org }
35438fdac3cSmochel@digitalimplant.org 
3551da177e4SLinus Torvalds /**
3561da177e4SLinus Torvalds  * bus_for_each_drv - driver iterator
3571da177e4SLinus Torvalds  * @bus: bus we're dealing with.
3581da177e4SLinus Torvalds  * @start: driver to start iterating on.
3591da177e4SLinus Torvalds  * @data: data to pass to the callback.
3601da177e4SLinus Torvalds  * @fn: function to call for each driver.
3611da177e4SLinus Torvalds  *
3621da177e4SLinus Torvalds  * This is nearly identical to the device iterator above.
3631da177e4SLinus Torvalds  * We iterate over each driver that belongs to @bus, and call
3641da177e4SLinus Torvalds  * @fn for each. If @fn returns anything but 0, we break out
3651da177e4SLinus Torvalds  * and return it. If @start is not NULL, we use it as the head
3661da177e4SLinus Torvalds  * of the list.
3671da177e4SLinus Torvalds  *
3681da177e4SLinus Torvalds  * NOTE: we don't return the driver that returns a non-zero
3691da177e4SLinus Torvalds  * value, nor do we leave the reference count incremented for that
3701da177e4SLinus Torvalds  * driver. If the caller needs to know that info, it must set it
3711da177e4SLinus Torvalds  * in the callback. It must also be sure to increment the refcount
3721da177e4SLinus Torvalds  * so it doesn't disappear before returning to the caller.
3731da177e4SLinus Torvalds  */
3741da177e4SLinus Torvalds int bus_for_each_drv(struct bus_type *bus, struct device_driver *start,
3751da177e4SLinus Torvalds 		     void *data, int (*fn)(struct device_driver *, void *))
3761da177e4SLinus Torvalds {
37738fdac3cSmochel@digitalimplant.org 	struct klist_iter i;
37838fdac3cSmochel@digitalimplant.org 	struct device_driver *drv;
37938fdac3cSmochel@digitalimplant.org 	int error = 0;
3801da177e4SLinus Torvalds 
38138fdac3cSmochel@digitalimplant.org 	if (!bus)
38238fdac3cSmochel@digitalimplant.org 		return -EINVAL;
38338fdac3cSmochel@digitalimplant.org 
3847cd9c9bbSGreg Kroah-Hartman 	klist_iter_init_node(&bus->p->klist_drivers, &i,
385e5dd1278SGreg Kroah-Hartman 			     start ? &start->p->knode_bus : NULL);
38638fdac3cSmochel@digitalimplant.org 	while ((drv = next_driver(&i)) && !error)
38738fdac3cSmochel@digitalimplant.org 		error = fn(drv, data);
38838fdac3cSmochel@digitalimplant.org 	klist_iter_exit(&i);
38938fdac3cSmochel@digitalimplant.org 	return error;
3901da177e4SLinus Torvalds }
3914a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_for_each_drv);
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds /**
3941da177e4SLinus Torvalds  * bus_add_device - add device to bus
3951da177e4SLinus Torvalds  * @dev: device being added
3961da177e4SLinus Torvalds  *
3972023c610SAlan Stern  * - Add device's bus attributes.
3982023c610SAlan Stern  * - Create links to device's bus.
3991da177e4SLinus Torvalds  * - Add the device to its bus's list of devices.
4001da177e4SLinus Torvalds  */
4011da177e4SLinus Torvalds int bus_add_device(struct device *dev)
4021da177e4SLinus Torvalds {
4035901d014SGreg Kroah-Hartman 	struct bus_type *bus = bus_get(dev->bus);
4041da177e4SLinus Torvalds 	int error = 0;
4051da177e4SLinus Torvalds 
4061da177e4SLinus Torvalds 	if (bus) {
4071e0b2cf9SKay Sievers 		pr_debug("bus: '%s': add device %s\n", bus->name, dev_name(dev));
408fa6fdb33SGreg Kroah-Hartman 		error = device_add_groups(dev, bus->dev_groups);
409fa6fdb33SGreg Kroah-Hartman 		if (error)
410b46c7337SGreg Kroah-Hartman 			goto out_put;
411c6f7e72aSGreg Kroah-Hartman 		error = sysfs_create_link(&bus->p->devices_kset->kobj,
4121e0b2cf9SKay Sievers 						&dev->kobj, dev_name(dev));
413f86db396SAndrew Morton 		if (error)
4141c34203aSJunjie Mao 			goto out_groups;
415f86db396SAndrew Morton 		error = sysfs_create_link(&dev->kobj,
416c6f7e72aSGreg Kroah-Hartman 				&dev->bus->p->subsys.kobj, "subsystem");
417f86db396SAndrew Morton 		if (error)
418513e7337SCornelia Huck 			goto out_subsys;
4192023c610SAlan Stern 		klist_add_tail(&dev->p->knode_bus, &bus->p->klist_devices);
4201da177e4SLinus Torvalds 	}
421513e7337SCornelia Huck 	return 0;
422513e7337SCornelia Huck 
423513e7337SCornelia Huck out_subsys:
4241e0b2cf9SKay Sievers 	sysfs_remove_link(&bus->p->devices_kset->kobj, dev_name(dev));
425fa6fdb33SGreg Kroah-Hartman out_groups:
426fa6fdb33SGreg Kroah-Hartman 	device_remove_groups(dev, bus->dev_groups);
427513e7337SCornelia Huck out_put:
428fc1ede58SGreg Kroah-Hartman 	bus_put(dev->bus);
4291da177e4SLinus Torvalds 	return error;
4301da177e4SLinus Torvalds }
4311da177e4SLinus Torvalds 
4321da177e4SLinus Torvalds /**
4332023c610SAlan Stern  * bus_probe_device - probe drivers for a new device
4342023c610SAlan Stern  * @dev: device to probe
43553877d06SKay Sievers  *
4362023c610SAlan Stern  * - Automatically probe for a driver if the bus allows it.
43753877d06SKay Sievers  */
4382023c610SAlan Stern void bus_probe_device(struct device *dev)
43953877d06SKay Sievers {
44053877d06SKay Sievers 	struct bus_type *bus = dev->bus;
441ca22e56dSKay Sievers 	struct subsys_interface *sif;
44253877d06SKay Sievers 
443ca22e56dSKay Sievers 	if (!bus)
444ca22e56dSKay Sievers 		return;
445ca22e56dSKay Sievers 
446765230b5SDmitry Torokhov 	if (bus->p->drivers_autoprobe)
447765230b5SDmitry Torokhov 		device_initial_probe(dev);
448ca22e56dSKay Sievers 
449ca22e56dSKay Sievers 	mutex_lock(&bus->p->mutex);
450ca22e56dSKay Sievers 	list_for_each_entry(sif, &bus->p->interfaces, node)
451ca22e56dSKay Sievers 		if (sif->add_dev)
452ca22e56dSKay Sievers 			sif->add_dev(dev, sif);
453ca22e56dSKay Sievers 	mutex_unlock(&bus->p->mutex);
454f86db396SAndrew Morton }
45553877d06SKay Sievers 
45653877d06SKay Sievers /**
4571da177e4SLinus Torvalds  * bus_remove_device - remove device from bus
4581da177e4SLinus Torvalds  * @dev: device to be removed
4591da177e4SLinus Torvalds  *
460ca22e56dSKay Sievers  * - Remove device from all interfaces.
461ca22e56dSKay Sievers  * - Remove symlink from bus' directory.
4621da177e4SLinus Torvalds  * - Delete device from bus's list.
4631da177e4SLinus Torvalds  * - Detach from its driver.
4641da177e4SLinus Torvalds  * - Drop reference taken in bus_add_device().
4651da177e4SLinus Torvalds  */
4661da177e4SLinus Torvalds void bus_remove_device(struct device *dev)
4671da177e4SLinus Torvalds {
468ca22e56dSKay Sievers 	struct bus_type *bus = dev->bus;
469ca22e56dSKay Sievers 	struct subsys_interface *sif;
470ca22e56dSKay Sievers 
471ca22e56dSKay Sievers 	if (!bus)
472ca22e56dSKay Sievers 		return;
473ca22e56dSKay Sievers 
474ca22e56dSKay Sievers 	mutex_lock(&bus->p->mutex);
475ca22e56dSKay Sievers 	list_for_each_entry(sif, &bus->p->interfaces, node)
476ca22e56dSKay Sievers 		if (sif->remove_dev)
477ca22e56dSKay Sievers 			sif->remove_dev(dev, sif);
478ca22e56dSKay Sievers 	mutex_unlock(&bus->p->mutex);
479ca22e56dSKay Sievers 
480b9d9c82bSKay Sievers 	sysfs_remove_link(&dev->kobj, "subsystem");
4814a3ad20cSGreg Kroah-Hartman 	sysfs_remove_link(&dev->bus->p->devices_kset->kobj,
4821e0b2cf9SKay Sievers 			  dev_name(dev));
483fa6fdb33SGreg Kroah-Hartman 	device_remove_groups(dev, dev->bus->dev_groups);
484ae1b4171SGreg Kroah-Hartman 	if (klist_node_attached(&dev->p->knode_bus))
485ae1b4171SGreg Kroah-Hartman 		klist_del(&dev->p->knode_bus);
4863f62e570SGreg Kroah-Hartman 
4874a3ad20cSGreg Kroah-Hartman 	pr_debug("bus: '%s': remove device %s\n",
4881e0b2cf9SKay Sievers 		 dev->bus->name, dev_name(dev));
4891da177e4SLinus Torvalds 	device_release_driver(dev);
490fc1ede58SGreg Kroah-Hartman 	bus_put(dev->bus);
4911da177e4SLinus Torvalds }
4921da177e4SLinus Torvalds 
493f86db396SAndrew Morton static int __must_check add_bind_files(struct device_driver *drv)
494874c6241SGreg Kroah-Hartman {
495f86db396SAndrew Morton 	int ret;
496f86db396SAndrew Morton 
497f86db396SAndrew Morton 	ret = driver_create_file(drv, &driver_attr_unbind);
498f86db396SAndrew Morton 	if (ret == 0) {
499f86db396SAndrew Morton 		ret = driver_create_file(drv, &driver_attr_bind);
500f86db396SAndrew Morton 		if (ret)
501f86db396SAndrew Morton 			driver_remove_file(drv, &driver_attr_unbind);
502f86db396SAndrew Morton 	}
503f86db396SAndrew Morton 	return ret;
504874c6241SGreg Kroah-Hartman }
505874c6241SGreg Kroah-Hartman 
506874c6241SGreg Kroah-Hartman static void remove_bind_files(struct device_driver *drv)
507874c6241SGreg Kroah-Hartman {
508874c6241SGreg Kroah-Hartman 	driver_remove_file(drv, &driver_attr_bind);
509874c6241SGreg Kroah-Hartman 	driver_remove_file(drv, &driver_attr_unbind);
510874c6241SGreg Kroah-Hartman }
511b8c5cec2SKay Sievers 
5122e7189b6SGreg Kroah-Hartman static BUS_ATTR_WO(drivers_probe);
5132e7189b6SGreg Kroah-Hartman static BUS_ATTR_RW(drivers_autoprobe);
5148380770cSKay Sievers 
515b8c5cec2SKay Sievers static int add_probe_files(struct bus_type *bus)
516b8c5cec2SKay Sievers {
517b8c5cec2SKay Sievers 	int retval;
518b8c5cec2SKay Sievers 
5198380770cSKay Sievers 	retval = bus_create_file(bus, &bus_attr_drivers_probe);
520b8c5cec2SKay Sievers 	if (retval)
521b8c5cec2SKay Sievers 		goto out;
522b8c5cec2SKay Sievers 
5238380770cSKay Sievers 	retval = bus_create_file(bus, &bus_attr_drivers_autoprobe);
524b8c5cec2SKay Sievers 	if (retval)
5258380770cSKay Sievers 		bus_remove_file(bus, &bus_attr_drivers_probe);
526b8c5cec2SKay Sievers out:
527b8c5cec2SKay Sievers 	return retval;
528b8c5cec2SKay Sievers }
529b8c5cec2SKay Sievers 
530b8c5cec2SKay Sievers static void remove_probe_files(struct bus_type *bus)
531b8c5cec2SKay Sievers {
5328380770cSKay Sievers 	bus_remove_file(bus, &bus_attr_drivers_autoprobe);
5338380770cSKay Sievers 	bus_remove_file(bus, &bus_attr_drivers_probe);
534b8c5cec2SKay Sievers }
5351da177e4SLinus Torvalds 
5362581c9ccSGreg Kroah-Hartman static ssize_t uevent_store(struct device_driver *drv, const char *buf,
5372581c9ccSGreg Kroah-Hartman 			    size_t count)
5387ac1cf4aSKay Sievers {
539df44b479SPeter Rajnoha 	int rc;
540df44b479SPeter Rajnoha 
541df44b479SPeter Rajnoha 	rc = kobject_synth_uevent(&drv->p->kobj, buf, count);
542df44b479SPeter Rajnoha 	return rc ? rc : count;
5437ac1cf4aSKay Sievers }
5442581c9ccSGreg Kroah-Hartman static DRIVER_ATTR_WO(uevent);
5457ac1cf4aSKay Sievers 
5461da177e4SLinus Torvalds /**
5471da177e4SLinus Torvalds  * bus_add_driver - Add a driver to the bus.
5481da177e4SLinus Torvalds  * @drv: driver.
5491da177e4SLinus Torvalds  */
5501da177e4SLinus Torvalds int bus_add_driver(struct device_driver *drv)
5511da177e4SLinus Torvalds {
552e5dd1278SGreg Kroah-Hartman 	struct bus_type *bus;
553e5dd1278SGreg Kroah-Hartman 	struct driver_private *priv;
5541da177e4SLinus Torvalds 	int error = 0;
5551da177e4SLinus Torvalds 
556e5dd1278SGreg Kroah-Hartman 	bus = bus_get(drv->bus);
557d9fd4d3bSJeff Garzik 	if (!bus)
5584f6e1945SGreg Kroah-Hartman 		return -EINVAL;
559d9fd4d3bSJeff Garzik 
5607dc72b28SGreg Kroah-Hartman 	pr_debug("bus: '%s': add driver %s\n", bus->name, drv->name);
561e5dd1278SGreg Kroah-Hartman 
562e5dd1278SGreg Kroah-Hartman 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
56307634464SCornelia Huck 	if (!priv) {
56407634464SCornelia Huck 		error = -ENOMEM;
56507634464SCornelia Huck 		goto out_put_bus;
56607634464SCornelia Huck 	}
567e5dd1278SGreg Kroah-Hartman 	klist_init(&priv->klist_devices, NULL, NULL);
568e5dd1278SGreg Kroah-Hartman 	priv->driver = drv;
569e5dd1278SGreg Kroah-Hartman 	drv->p = priv;
570c8e90d82SGreg Kroah-Hartman 	priv->kobj.kset = bus->p->drivers_kset;
571c8e90d82SGreg Kroah-Hartman 	error = kobject_init_and_add(&priv->kobj, &driver_ktype, NULL,
572c8e90d82SGreg Kroah-Hartman 				     "%s", drv->name);
573dc0afa83SCornelia Huck 	if (error)
57407634464SCornelia Huck 		goto out_unregister;
5751da177e4SLinus Torvalds 
576190888acSMing Lei 	klist_add_tail(&priv->knode_bus, &bus->p->klist_drivers);
577c6f7e72aSGreg Kroah-Hartman 	if (drv->bus->p->drivers_autoprobe) {
578f86db396SAndrew Morton 		error = driver_attach(drv);
579f86db396SAndrew Morton 		if (error)
580310862e5SSchspa Shi 			goto out_del_list;
581b8c5cec2SKay Sievers 	}
5821da177e4SLinus Torvalds 	module_add_driver(drv->owner, drv);
5831da177e4SLinus Torvalds 
5847ac1cf4aSKay Sievers 	error = driver_create_file(drv, &driver_attr_uevent);
5857ac1cf4aSKay Sievers 	if (error) {
5867ac1cf4aSKay Sievers 		printk(KERN_ERR "%s: uevent attr (%s) failed\n",
5872b3a302aSHarvey Harrison 			__func__, drv->name);
5887ac1cf4aSKay Sievers 	}
589e18945b1SGreg Kroah-Hartman 	error = driver_add_groups(drv, bus->drv_groups);
590f86db396SAndrew Morton 	if (error) {
591f86db396SAndrew Morton 		/* How the hell do we get out of this pickle? Give up */
5924044b2fcSJoe Pater 		printk(KERN_ERR "%s: driver_add_groups(%s) failed\n",
593ed0617b5SGreg Kroah-Hartman 			__func__, drv->name);
594e18945b1SGreg Kroah-Hartman 	}
5951a6f2a75SDmitry Torokhov 
5961a6f2a75SDmitry Torokhov 	if (!drv->suppress_bind_attrs) {
597f86db396SAndrew Morton 		error = add_bind_files(drv);
598f86db396SAndrew Morton 		if (error) {
599f86db396SAndrew Morton 			/* Ditto */
600f86db396SAndrew Morton 			printk(KERN_ERR "%s: add_bind_files(%s) failed\n",
6012b3a302aSHarvey Harrison 				__func__, drv->name);
602f86db396SAndrew Morton 		}
6031a6f2a75SDmitry Torokhov 	}
604d9fd4d3bSJeff Garzik 
6055c8563d7SKay Sievers 	return 0;
6061a6f2a75SDmitry Torokhov 
607310862e5SSchspa Shi out_del_list:
608310862e5SSchspa Shi 	klist_del(&priv->knode_bus);
609f86db396SAndrew Morton out_unregister:
61099b28f1bSPhil Carmody 	kobject_put(&priv->kobj);
6110f9b011dSChristophe JAILLET 	/* drv->p is freed in driver_release()  */
6125c8563d7SKay Sievers 	drv->p = NULL;
613f86db396SAndrew Morton out_put_bus:
614fc1ede58SGreg Kroah-Hartman 	bus_put(bus);
615f86db396SAndrew Morton 	return error;
6161da177e4SLinus Torvalds }
6171da177e4SLinus Torvalds 
6181da177e4SLinus Torvalds /**
6191da177e4SLinus Torvalds  * bus_remove_driver - delete driver from bus's knowledge.
6201da177e4SLinus Torvalds  * @drv: driver.
6211da177e4SLinus Torvalds  *
6221da177e4SLinus Torvalds  * Detach the driver from the devices it controls, and remove
6231da177e4SLinus Torvalds  * it from its bus's list of drivers. Finally, we drop the reference
6241da177e4SLinus Torvalds  * to the bus we took in bus_add_driver().
6251da177e4SLinus Torvalds  */
6261da177e4SLinus Torvalds void bus_remove_driver(struct device_driver *drv)
6271da177e4SLinus Torvalds {
628d9fd4d3bSJeff Garzik 	if (!drv->bus)
629d9fd4d3bSJeff Garzik 		return;
630d9fd4d3bSJeff Garzik 
6311a6f2a75SDmitry Torokhov 	if (!drv->suppress_bind_attrs)
632874c6241SGreg Kroah-Hartman 		remove_bind_files(drv);
633ed0617b5SGreg Kroah-Hartman 	driver_remove_groups(drv, drv->bus->drv_groups);
6347ac1cf4aSKay Sievers 	driver_remove_file(drv, &driver_attr_uevent);
635e5dd1278SGreg Kroah-Hartman 	klist_remove(&drv->p->knode_bus);
6367dc72b28SGreg Kroah-Hartman 	pr_debug("bus: '%s': remove driver %s\n", drv->bus->name, drv->name);
6371da177e4SLinus Torvalds 	driver_detach(drv);
6381da177e4SLinus Torvalds 	module_remove_driver(drv);
639c10997f6SGreg Kroah-Hartman 	kobject_put(&drv->p->kobj);
640fc1ede58SGreg Kroah-Hartman 	bus_put(drv->bus);
6411da177e4SLinus Torvalds }
6421da177e4SLinus Torvalds 
6431da177e4SLinus Torvalds /* Helper for bus_rescan_devices's iter */
644f86db396SAndrew Morton static int __must_check bus_rescan_devices_helper(struct device *dev,
645f86db396SAndrew Morton 						  void *data)
6461da177e4SLinus Torvalds {
647f86db396SAndrew Morton 	int ret = 0;
648f86db396SAndrew Morton 
649bf74ad5bSAlan Stern 	if (!dev->driver) {
6508c97a46aSMartin Liu 		if (dev->parent && dev->bus->need_parent_lock)
6518e9394ceSGreg Kroah-Hartman 			device_lock(dev->parent);
652f86db396SAndrew Morton 		ret = device_attach(dev);
6538c97a46aSMartin Liu 		if (dev->parent && dev->bus->need_parent_lock)
6548e9394ceSGreg Kroah-Hartman 			device_unlock(dev->parent);
655bf74ad5bSAlan Stern 	}
656f86db396SAndrew Morton 	return ret < 0 ? ret : 0;
6571da177e4SLinus Torvalds }
6581da177e4SLinus Torvalds 
6591da177e4SLinus Torvalds /**
6601da177e4SLinus Torvalds  * bus_rescan_devices - rescan devices on the bus for possible drivers
6611da177e4SLinus Torvalds  * @bus: the bus to scan.
6621da177e4SLinus Torvalds  *
6631da177e4SLinus Torvalds  * This function will look for devices on the bus with no driver
66423d3d602SGreg Kroah-Hartman  * attached and rescan it against existing drivers to see if it matches
66523d3d602SGreg Kroah-Hartman  * any by calling device_attach() for the unbound devices.
6661da177e4SLinus Torvalds  */
667f86db396SAndrew Morton int bus_rescan_devices(struct bus_type *bus)
6681da177e4SLinus Torvalds {
669f86db396SAndrew Morton 	return bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper);
6701da177e4SLinus Torvalds }
6714a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_rescan_devices);
6721da177e4SLinus Torvalds 
673e935d5daSMoore, Eric /**
674e935d5daSMoore, Eric  * device_reprobe - remove driver for a device and probe for a new driver
675e935d5daSMoore, Eric  * @dev: the device to reprobe
676e935d5daSMoore, Eric  *
677e935d5daSMoore, Eric  * This function detaches the attached driver (if any) for the given
678e935d5daSMoore, Eric  * device and restarts the driver probing process.  It is intended
679e935d5daSMoore, Eric  * to use if probing criteria changed during a devices lifetime and
680e935d5daSMoore, Eric  * driver attachment should change accordingly.
681e935d5daSMoore, Eric  */
682f86db396SAndrew Morton int device_reprobe(struct device *dev)
683e935d5daSMoore, Eric {
684ed88747cSAlexander Duyck 	if (dev->driver)
685ed88747cSAlexander Duyck 		device_driver_detach(dev);
686f86db396SAndrew Morton 	return bus_rescan_devices_helper(dev, NULL);
687e935d5daSMoore, Eric }
688e935d5daSMoore, Eric EXPORT_SYMBOL_GPL(device_reprobe);
6891da177e4SLinus Torvalds 
69012478ba0SGreg Kroah-Hartman static int bus_add_groups(struct bus_type *bus,
69112478ba0SGreg Kroah-Hartman 			  const struct attribute_group **groups)
69212478ba0SGreg Kroah-Hartman {
6933e9b2baeSGreg Kroah-Hartman 	return sysfs_create_groups(&bus->p->subsys.kobj, groups);
69412478ba0SGreg Kroah-Hartman }
69512478ba0SGreg Kroah-Hartman 
69612478ba0SGreg Kroah-Hartman static void bus_remove_groups(struct bus_type *bus,
69712478ba0SGreg Kroah-Hartman 			      const struct attribute_group **groups)
69812478ba0SGreg Kroah-Hartman {
6993e9b2baeSGreg Kroah-Hartman 	sysfs_remove_groups(&bus->p->subsys.kobj, groups);
70012478ba0SGreg Kroah-Hartman }
70112478ba0SGreg Kroah-Hartman 
70234bb61f9SJames Bottomley static void klist_devices_get(struct klist_node *n)
70334bb61f9SJames Bottomley {
704ae1b4171SGreg Kroah-Hartman 	struct device_private *dev_prv = to_device_private_bus(n);
705ae1b4171SGreg Kroah-Hartman 	struct device *dev = dev_prv->device;
70634bb61f9SJames Bottomley 
70734bb61f9SJames Bottomley 	get_device(dev);
70834bb61f9SJames Bottomley }
70934bb61f9SJames Bottomley 
71034bb61f9SJames Bottomley static void klist_devices_put(struct klist_node *n)
71134bb61f9SJames Bottomley {
712ae1b4171SGreg Kroah-Hartman 	struct device_private *dev_prv = to_device_private_bus(n);
713ae1b4171SGreg Kroah-Hartman 	struct device *dev = dev_prv->device;
71434bb61f9SJames Bottomley 
71534bb61f9SJames Bottomley 	put_device(dev);
71634bb61f9SJames Bottomley }
71734bb61f9SJames Bottomley 
7187ac1cf4aSKay Sievers static ssize_t bus_uevent_store(struct bus_type *bus,
7197ac1cf4aSKay Sievers 				const char *buf, size_t count)
7207ac1cf4aSKay Sievers {
721df44b479SPeter Rajnoha 	int rc;
722df44b479SPeter Rajnoha 
723df44b479SPeter Rajnoha 	rc = kobject_synth_uevent(&bus->p->subsys.kobj, buf, count);
724df44b479SPeter Rajnoha 	return rc ? rc : count;
7257ac1cf4aSKay Sievers }
726a4723041SGreg Kroah-Hartman /*
727a4723041SGreg Kroah-Hartman  * "open code" the old BUS_ATTR() macro here.  We want to use BUS_ATTR_WO()
728a4723041SGreg Kroah-Hartman  * here, but can not use it as earlier in the file we have
729a4723041SGreg Kroah-Hartman  * DEVICE_ATTR_WO(uevent), which would cause a clash with the with the store
730a4723041SGreg Kroah-Hartman  * function name.
731a4723041SGreg Kroah-Hartman  */
73216b0dd40SJinchao Wang static struct bus_attribute bus_attr_uevent = __ATTR(uevent, 0200, NULL,
733a4723041SGreg Kroah-Hartman 						     bus_uevent_store);
7347ac1cf4aSKay Sievers 
7351da177e4SLinus Torvalds /**
736be871b7eSMichal Hocko  * bus_register - register a driver-core subsystem
73778d79559SRandy Dunlap  * @bus: bus to register
7381da177e4SLinus Torvalds  *
73978d79559SRandy Dunlap  * Once we have that, we register the bus with the kobject
7401da177e4SLinus Torvalds  * infrastructure, then register the children subsystems it has:
741ca22e56dSKay Sievers  * the devices and drivers that belong to the subsystem.
7421da177e4SLinus Torvalds  */
743be871b7eSMichal Hocko int bus_register(struct bus_type *bus)
7441da177e4SLinus Torvalds {
7451da177e4SLinus Torvalds 	int retval;
7466b6e39a6SKay Sievers 	struct subsys_private *priv;
747*37e98d9bSGreg Kroah-Hartman 	struct lock_class_key *key;
7481da177e4SLinus Torvalds 
7496b6e39a6SKay Sievers 	priv = kzalloc(sizeof(struct subsys_private), GFP_KERNEL);
750c6f7e72aSGreg Kroah-Hartman 	if (!priv)
751c6f7e72aSGreg Kroah-Hartman 		return -ENOMEM;
752116af378SBenjamin Herrenschmidt 
753c6f7e72aSGreg Kroah-Hartman 	priv->bus = bus;
754c6f7e72aSGreg Kroah-Hartman 	bus->p = priv;
755c6f7e72aSGreg Kroah-Hartman 
756c6f7e72aSGreg Kroah-Hartman 	BLOCKING_INIT_NOTIFIER_HEAD(&priv->bus_notifier);
757c6f7e72aSGreg Kroah-Hartman 
758c6f7e72aSGreg Kroah-Hartman 	retval = kobject_set_name(&priv->subsys.kobj, "%s", bus->name);
7591da177e4SLinus Torvalds 	if (retval)
7601da177e4SLinus Torvalds 		goto out;
7611da177e4SLinus Torvalds 
762c6f7e72aSGreg Kroah-Hartman 	priv->subsys.kobj.kset = bus_kset;
763c6f7e72aSGreg Kroah-Hartman 	priv->subsys.kobj.ktype = &bus_ktype;
764c6f7e72aSGreg Kroah-Hartman 	priv->drivers_autoprobe = 1;
765d6b05b84SGreg Kroah-Hartman 
766c6f7e72aSGreg Kroah-Hartman 	retval = kset_register(&priv->subsys);
7671da177e4SLinus Torvalds 	if (retval)
7681da177e4SLinus Torvalds 		goto out;
7691da177e4SLinus Torvalds 
7707ac1cf4aSKay Sievers 	retval = bus_create_file(bus, &bus_attr_uevent);
7717ac1cf4aSKay Sievers 	if (retval)
7727ac1cf4aSKay Sievers 		goto bus_uevent_fail;
7737ac1cf4aSKay Sievers 
774c6f7e72aSGreg Kroah-Hartman 	priv->devices_kset = kset_create_and_add("devices", NULL,
775c6f7e72aSGreg Kroah-Hartman 						 &priv->subsys.kobj);
776c6f7e72aSGreg Kroah-Hartman 	if (!priv->devices_kset) {
7773d899596SGreg Kroah-Hartman 		retval = -ENOMEM;
7781da177e4SLinus Torvalds 		goto bus_devices_fail;
7793d899596SGreg Kroah-Hartman 	}
7801da177e4SLinus Torvalds 
781c6f7e72aSGreg Kroah-Hartman 	priv->drivers_kset = kset_create_and_add("drivers", NULL,
782c6f7e72aSGreg Kroah-Hartman 						 &priv->subsys.kobj);
783c6f7e72aSGreg Kroah-Hartman 	if (!priv->drivers_kset) {
7846dcec251SGreg Kroah-Hartman 		retval = -ENOMEM;
7851da177e4SLinus Torvalds 		goto bus_drivers_fail;
7866dcec251SGreg Kroah-Hartman 	}
787465c7a3aSmochel@digitalimplant.org 
788ca22e56dSKay Sievers 	INIT_LIST_HEAD(&priv->interfaces);
789*37e98d9bSGreg Kroah-Hartman 	key = &priv->lock_key;
790*37e98d9bSGreg Kroah-Hartman 	lockdep_register_key(key);
791ca22e56dSKay Sievers 	__mutex_init(&priv->mutex, "subsys mutex", key);
792c6f7e72aSGreg Kroah-Hartman 	klist_init(&priv->klist_devices, klist_devices_get, klist_devices_put);
793c6f7e72aSGreg Kroah-Hartman 	klist_init(&priv->klist_drivers, NULL, NULL);
794b8c5cec2SKay Sievers 
795b8c5cec2SKay Sievers 	retval = add_probe_files(bus);
796b8c5cec2SKay Sievers 	if (retval)
797b8c5cec2SKay Sievers 		goto bus_probe_files_fail;
798b8c5cec2SKay Sievers 
79912478ba0SGreg Kroah-Hartman 	retval = bus_add_groups(bus, bus->bus_groups);
80012478ba0SGreg Kroah-Hartman 	if (retval)
80112478ba0SGreg Kroah-Hartman 		goto bus_groups_fail;
8021da177e4SLinus Torvalds 
8037dc72b28SGreg Kroah-Hartman 	pr_debug("bus: '%s': registered\n", bus->name);
8041da177e4SLinus Torvalds 	return 0;
8051da177e4SLinus Torvalds 
80612478ba0SGreg Kroah-Hartman bus_groups_fail:
807b8c5cec2SKay Sievers 	remove_probe_files(bus);
808b8c5cec2SKay Sievers bus_probe_files_fail:
809c6f7e72aSGreg Kroah-Hartman 	kset_unregister(bus->p->drivers_kset);
8101da177e4SLinus Torvalds bus_drivers_fail:
811c6f7e72aSGreg Kroah-Hartman 	kset_unregister(bus->p->devices_kset);
8121da177e4SLinus Torvalds bus_devices_fail:
8137ac1cf4aSKay Sievers 	bus_remove_file(bus, &bus_attr_uevent);
8147ac1cf4aSKay Sievers bus_uevent_fail:
815c6f7e72aSGreg Kroah-Hartman 	kset_unregister(&bus->p->subsys);
8161da177e4SLinus Torvalds out:
817600c20f3SJike Song 	kfree(bus->p);
818f48f3febSDave Young 	bus->p = NULL;
8191da177e4SLinus Torvalds 	return retval;
8201da177e4SLinus Torvalds }
821be871b7eSMichal Hocko EXPORT_SYMBOL_GPL(bus_register);
8221da177e4SLinus Torvalds 
8231da177e4SLinus Torvalds /**
8241da177e4SLinus Torvalds  * bus_unregister - remove a bus from the system
8251da177e4SLinus Torvalds  * @bus: bus.
8261da177e4SLinus Torvalds  *
8271da177e4SLinus Torvalds  * Unregister the child subsystems and the bus itself.
828fc1ede58SGreg Kroah-Hartman  * Finally, we call bus_put() to release the refcount
8291da177e4SLinus Torvalds  */
8301da177e4SLinus Torvalds void bus_unregister(struct bus_type *bus)
8311da177e4SLinus Torvalds {
8327dc72b28SGreg Kroah-Hartman 	pr_debug("bus: '%s': unregistering\n", bus->name);
833ca22e56dSKay Sievers 	if (bus->dev_root)
834ca22e56dSKay Sievers 		device_unregister(bus->dev_root);
83512478ba0SGreg Kroah-Hartman 	bus_remove_groups(bus, bus->bus_groups);
836b8c5cec2SKay Sievers 	remove_probe_files(bus);
837c6f7e72aSGreg Kroah-Hartman 	kset_unregister(bus->p->drivers_kset);
838c6f7e72aSGreg Kroah-Hartman 	kset_unregister(bus->p->devices_kset);
8397ac1cf4aSKay Sievers 	bus_remove_file(bus, &bus_attr_uevent);
840c6f7e72aSGreg Kroah-Hartman 	kset_unregister(&bus->p->subsys);
8411da177e4SLinus Torvalds }
8424a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_unregister);
8431da177e4SLinus Torvalds 
844116af378SBenjamin Herrenschmidt int bus_register_notifier(struct bus_type *bus, struct notifier_block *nb)
845116af378SBenjamin Herrenschmidt {
846c6f7e72aSGreg Kroah-Hartman 	return blocking_notifier_chain_register(&bus->p->bus_notifier, nb);
847116af378SBenjamin Herrenschmidt }
848116af378SBenjamin Herrenschmidt EXPORT_SYMBOL_GPL(bus_register_notifier);
849116af378SBenjamin Herrenschmidt 
850116af378SBenjamin Herrenschmidt int bus_unregister_notifier(struct bus_type *bus, struct notifier_block *nb)
851116af378SBenjamin Herrenschmidt {
852c6f7e72aSGreg Kroah-Hartman 	return blocking_notifier_chain_unregister(&bus->p->bus_notifier, nb);
853116af378SBenjamin Herrenschmidt }
854116af378SBenjamin Herrenschmidt EXPORT_SYMBOL_GPL(bus_unregister_notifier);
855116af378SBenjamin Herrenschmidt 
856ed9f9181SGreg Kroah-Hartman void bus_notify(struct device *dev, enum bus_notifier_event value)
857ed9f9181SGreg Kroah-Hartman {
858ed9f9181SGreg Kroah-Hartman 	struct bus_type *bus = dev->bus;
859ed9f9181SGreg Kroah-Hartman 
860ed9f9181SGreg Kroah-Hartman 	if (bus)
861ed9f9181SGreg Kroah-Hartman 		blocking_notifier_call_chain(&bus->p->bus_notifier, value, dev);
862ed9f9181SGreg Kroah-Hartman }
863ed9f9181SGreg Kroah-Hartman 
8640fed80f7SGreg Kroah-Hartman struct kset *bus_get_kset(struct bus_type *bus)
8650fed80f7SGreg Kroah-Hartman {
866c6f7e72aSGreg Kroah-Hartman 	return &bus->p->subsys;
8670fed80f7SGreg Kroah-Hartman }
8680fed80f7SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_get_kset);
8690fed80f7SGreg Kroah-Hartman 
8708afbb427SGreg Kroah-Hartman static struct klist *bus_get_device_klist(struct bus_type *bus)
871b249072eSGreg Kroah-Hartman {
872c6f7e72aSGreg Kroah-Hartman 	return &bus->p->klist_devices;
873b249072eSGreg Kroah-Hartman }
874b249072eSGreg Kroah-Hartman 
87599178b03SGreg Kroah-Hartman /*
876dca25ebdSRobert P. J. Day  * Yes, this forcibly breaks the klist abstraction temporarily.  It
87799178b03SGreg Kroah-Hartman  * just wants to sort the klist, not change reference counts and
87899178b03SGreg Kroah-Hartman  * take/drop locks rapidly in the process.  It does all this while
87999178b03SGreg Kroah-Hartman  * holding the lock for the list, so objects can't otherwise be
88099178b03SGreg Kroah-Hartman  * added/removed while we're swizzling.
88199178b03SGreg Kroah-Hartman  */
88299178b03SGreg Kroah-Hartman static void device_insertion_sort_klist(struct device *a, struct list_head *list,
88399178b03SGreg Kroah-Hartman 					int (*compare)(const struct device *a,
88499178b03SGreg Kroah-Hartman 							const struct device *b))
88599178b03SGreg Kroah-Hartman {
88699178b03SGreg Kroah-Hartman 	struct klist_node *n;
887ae1b4171SGreg Kroah-Hartman 	struct device_private *dev_prv;
88899178b03SGreg Kroah-Hartman 	struct device *b;
88999178b03SGreg Kroah-Hartman 
8904c62785eSGeliang Tang 	list_for_each_entry(n, list, n_node) {
891ae1b4171SGreg Kroah-Hartman 		dev_prv = to_device_private_bus(n);
892ae1b4171SGreg Kroah-Hartman 		b = dev_prv->device;
89399178b03SGreg Kroah-Hartman 		if (compare(a, b) <= 0) {
894ae1b4171SGreg Kroah-Hartman 			list_move_tail(&a->p->knode_bus.n_node,
895ae1b4171SGreg Kroah-Hartman 				       &b->p->knode_bus.n_node);
89699178b03SGreg Kroah-Hartman 			return;
89799178b03SGreg Kroah-Hartman 		}
89899178b03SGreg Kroah-Hartman 	}
899ae1b4171SGreg Kroah-Hartman 	list_move_tail(&a->p->knode_bus.n_node, list);
90099178b03SGreg Kroah-Hartman }
90199178b03SGreg Kroah-Hartman 
90299178b03SGreg Kroah-Hartman void bus_sort_breadthfirst(struct bus_type *bus,
90399178b03SGreg Kroah-Hartman 			   int (*compare)(const struct device *a,
90499178b03SGreg Kroah-Hartman 					  const struct device *b))
90599178b03SGreg Kroah-Hartman {
90699178b03SGreg Kroah-Hartman 	LIST_HEAD(sorted_devices);
9074c62785eSGeliang Tang 	struct klist_node *n, *tmp;
908ae1b4171SGreg Kroah-Hartman 	struct device_private *dev_prv;
90999178b03SGreg Kroah-Hartman 	struct device *dev;
91099178b03SGreg Kroah-Hartman 	struct klist *device_klist;
91199178b03SGreg Kroah-Hartman 
91299178b03SGreg Kroah-Hartman 	device_klist = bus_get_device_klist(bus);
91399178b03SGreg Kroah-Hartman 
91499178b03SGreg Kroah-Hartman 	spin_lock(&device_klist->k_lock);
9154c62785eSGeliang Tang 	list_for_each_entry_safe(n, tmp, &device_klist->k_list, n_node) {
916ae1b4171SGreg Kroah-Hartman 		dev_prv = to_device_private_bus(n);
917ae1b4171SGreg Kroah-Hartman 		dev = dev_prv->device;
91899178b03SGreg Kroah-Hartman 		device_insertion_sort_klist(dev, &sorted_devices, compare);
91999178b03SGreg Kroah-Hartman 	}
92099178b03SGreg Kroah-Hartman 	list_splice(&sorted_devices, &device_klist->k_list);
92199178b03SGreg Kroah-Hartman 	spin_unlock(&device_klist->k_lock);
92299178b03SGreg Kroah-Hartman }
92399178b03SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_sort_breadthfirst);
92499178b03SGreg Kroah-Hartman 
925b0a8a59aSGreg Kroah-Hartman struct subsys_dev_iter {
926b0a8a59aSGreg Kroah-Hartman 	struct klist_iter		ki;
927b0a8a59aSGreg Kroah-Hartman 	const struct device_type	*type;
928b0a8a59aSGreg Kroah-Hartman };
929b0a8a59aSGreg Kroah-Hartman 
930ca22e56dSKay Sievers /**
931ca22e56dSKay Sievers  * subsys_dev_iter_init - initialize subsys device iterator
932ca22e56dSKay Sievers  * @iter: subsys iterator to initialize
933ca22e56dSKay Sievers  * @subsys: the subsys we wanna iterate over
934ca22e56dSKay Sievers  * @start: the device to start iterating from, if any
935ca22e56dSKay Sievers  * @type: device_type of the devices to iterate over, NULL for all
936ca22e56dSKay Sievers  *
937ca22e56dSKay Sievers  * Initialize subsys iterator @iter such that it iterates over devices
938ca22e56dSKay Sievers  * of @subsys.  If @start is set, the list iteration will start there,
939ca22e56dSKay Sievers  * otherwise if it is NULL, the iteration starts at the beginning of
940ca22e56dSKay Sievers  * the list.
941ca22e56dSKay Sievers  */
9422e45fc55SGreg Kroah-Hartman static void subsys_dev_iter_init(struct subsys_dev_iter *iter, struct bus_type *subsys,
943ca22e56dSKay Sievers 				 struct device *start, const struct device_type *type)
944ca22e56dSKay Sievers {
945ca22e56dSKay Sievers 	struct klist_node *start_knode = NULL;
946ca22e56dSKay Sievers 
947ca22e56dSKay Sievers 	if (start)
948ca22e56dSKay Sievers 		start_knode = &start->p->knode_bus;
9497cd9c9bbSGreg Kroah-Hartman 	klist_iter_init_node(&subsys->p->klist_devices, &iter->ki, start_knode);
950ca22e56dSKay Sievers 	iter->type = type;
951ca22e56dSKay Sievers }
952ca22e56dSKay Sievers 
953ca22e56dSKay Sievers /**
954ca22e56dSKay Sievers  * subsys_dev_iter_next - iterate to the next device
955ca22e56dSKay Sievers  * @iter: subsys iterator to proceed
956ca22e56dSKay Sievers  *
957ca22e56dSKay Sievers  * Proceed @iter to the next device and return it.  Returns NULL if
958ca22e56dSKay Sievers  * iteration is complete.
959ca22e56dSKay Sievers  *
960ca22e56dSKay Sievers  * The returned device is referenced and won't be released till
961ca22e56dSKay Sievers  * iterator is proceed to the next device or exited.  The caller is
962ca22e56dSKay Sievers  * free to do whatever it wants to do with the device including
963ca22e56dSKay Sievers  * calling back into subsys code.
964ca22e56dSKay Sievers  */
96538cdadefSGreg Kroah-Hartman static struct device *subsys_dev_iter_next(struct subsys_dev_iter *iter)
966ca22e56dSKay Sievers {
967ca22e56dSKay Sievers 	struct klist_node *knode;
968ca22e56dSKay Sievers 	struct device *dev;
969ca22e56dSKay Sievers 
970ca22e56dSKay Sievers 	for (;;) {
971ca22e56dSKay Sievers 		knode = klist_next(&iter->ki);
972ca22e56dSKay Sievers 		if (!knode)
973ca22e56dSKay Sievers 			return NULL;
974371fd7a2SGeliang Tang 		dev = to_device_private_bus(knode)->device;
975ca22e56dSKay Sievers 		if (!iter->type || iter->type == dev->type)
976ca22e56dSKay Sievers 			return dev;
977ca22e56dSKay Sievers 	}
978ca22e56dSKay Sievers }
979ca22e56dSKay Sievers 
980ca22e56dSKay Sievers /**
981ca22e56dSKay Sievers  * subsys_dev_iter_exit - finish iteration
982ca22e56dSKay Sievers  * @iter: subsys iterator to finish
983ca22e56dSKay Sievers  *
984ca22e56dSKay Sievers  * Finish an iteration.  Always call this function after iteration is
985ca22e56dSKay Sievers  * complete whether the iteration ran till the end or not.
986ca22e56dSKay Sievers  */
987af6d0743SGreg Kroah-Hartman static void subsys_dev_iter_exit(struct subsys_dev_iter *iter)
988ca22e56dSKay Sievers {
989ca22e56dSKay Sievers 	klist_iter_exit(&iter->ki);
990ca22e56dSKay Sievers }
991ca22e56dSKay Sievers 
992ca22e56dSKay Sievers int subsys_interface_register(struct subsys_interface *sif)
993ca22e56dSKay Sievers {
994ca22e56dSKay Sievers 	struct bus_type *subsys;
995ca22e56dSKay Sievers 	struct subsys_dev_iter iter;
996ca22e56dSKay Sievers 	struct device *dev;
997ca22e56dSKay Sievers 
998ca22e56dSKay Sievers 	if (!sif || !sif->subsys)
999ca22e56dSKay Sievers 		return -ENODEV;
1000ca22e56dSKay Sievers 
1001ca22e56dSKay Sievers 	subsys = bus_get(sif->subsys);
1002ca22e56dSKay Sievers 	if (!subsys)
1003ca22e56dSKay Sievers 		return -EINVAL;
1004ca22e56dSKay Sievers 
1005ca22e56dSKay Sievers 	mutex_lock(&subsys->p->mutex);
1006ca22e56dSKay Sievers 	list_add_tail(&sif->node, &subsys->p->interfaces);
1007ca22e56dSKay Sievers 	if (sif->add_dev) {
1008ca22e56dSKay Sievers 		subsys_dev_iter_init(&iter, subsys, NULL, NULL);
1009ca22e56dSKay Sievers 		while ((dev = subsys_dev_iter_next(&iter)))
1010ca22e56dSKay Sievers 			sif->add_dev(dev, sif);
1011ca22e56dSKay Sievers 		subsys_dev_iter_exit(&iter);
1012ca22e56dSKay Sievers 	}
1013ca22e56dSKay Sievers 	mutex_unlock(&subsys->p->mutex);
1014ca22e56dSKay Sievers 
1015ca22e56dSKay Sievers 	return 0;
1016ca22e56dSKay Sievers }
1017ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_interface_register);
1018ca22e56dSKay Sievers 
1019ca22e56dSKay Sievers void subsys_interface_unregister(struct subsys_interface *sif)
1020ca22e56dSKay Sievers {
10212b31594aSJonghwan Choi 	struct bus_type *subsys;
1022ca22e56dSKay Sievers 	struct subsys_dev_iter iter;
1023ca22e56dSKay Sievers 	struct device *dev;
1024ca22e56dSKay Sievers 
10252b31594aSJonghwan Choi 	if (!sif || !sif->subsys)
1026ca22e56dSKay Sievers 		return;
1027ca22e56dSKay Sievers 
10282b31594aSJonghwan Choi 	subsys = sif->subsys;
10292b31594aSJonghwan Choi 
1030ca22e56dSKay Sievers 	mutex_lock(&subsys->p->mutex);
1031ca22e56dSKay Sievers 	list_del_init(&sif->node);
1032ca22e56dSKay Sievers 	if (sif->remove_dev) {
1033ca22e56dSKay Sievers 		subsys_dev_iter_init(&iter, subsys, NULL, NULL);
1034ca22e56dSKay Sievers 		while ((dev = subsys_dev_iter_next(&iter)))
1035ca22e56dSKay Sievers 			sif->remove_dev(dev, sif);
1036ca22e56dSKay Sievers 		subsys_dev_iter_exit(&iter);
1037ca22e56dSKay Sievers 	}
1038ca22e56dSKay Sievers 	mutex_unlock(&subsys->p->mutex);
1039ca22e56dSKay Sievers 
1040ca22e56dSKay Sievers 	bus_put(subsys);
1041ca22e56dSKay Sievers }
1042ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_interface_unregister);
1043ca22e56dSKay Sievers 
1044ca22e56dSKay Sievers static void system_root_device_release(struct device *dev)
1045ca22e56dSKay Sievers {
1046ca22e56dSKay Sievers 	kfree(dev);
1047ca22e56dSKay Sievers }
1048d73ce004STejun Heo 
1049d73ce004STejun Heo static int subsys_register(struct bus_type *subsys,
1050d73ce004STejun Heo 			   const struct attribute_group **groups,
1051d73ce004STejun Heo 			   struct kobject *parent_of_root)
1052d73ce004STejun Heo {
1053d73ce004STejun Heo 	struct device *dev;
1054d73ce004STejun Heo 	int err;
1055d73ce004STejun Heo 
1056d73ce004STejun Heo 	err = bus_register(subsys);
1057d73ce004STejun Heo 	if (err < 0)
1058d73ce004STejun Heo 		return err;
1059d73ce004STejun Heo 
1060d73ce004STejun Heo 	dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1061d73ce004STejun Heo 	if (!dev) {
1062d73ce004STejun Heo 		err = -ENOMEM;
1063d73ce004STejun Heo 		goto err_dev;
1064d73ce004STejun Heo 	}
1065d73ce004STejun Heo 
1066d73ce004STejun Heo 	err = dev_set_name(dev, "%s", subsys->name);
1067d73ce004STejun Heo 	if (err < 0)
1068d73ce004STejun Heo 		goto err_name;
1069d73ce004STejun Heo 
1070d73ce004STejun Heo 	dev->kobj.parent = parent_of_root;
1071d73ce004STejun Heo 	dev->groups = groups;
1072d73ce004STejun Heo 	dev->release = system_root_device_release;
1073d73ce004STejun Heo 
1074d73ce004STejun Heo 	err = device_register(dev);
1075d73ce004STejun Heo 	if (err < 0)
1076d73ce004STejun Heo 		goto err_dev_reg;
1077d73ce004STejun Heo 
1078d73ce004STejun Heo 	subsys->dev_root = dev;
1079d73ce004STejun Heo 	return 0;
1080d73ce004STejun Heo 
1081d73ce004STejun Heo err_dev_reg:
1082d73ce004STejun Heo 	put_device(dev);
1083d73ce004STejun Heo 	dev = NULL;
1084d73ce004STejun Heo err_name:
1085d73ce004STejun Heo 	kfree(dev);
1086d73ce004STejun Heo err_dev:
1087d73ce004STejun Heo 	bus_unregister(subsys);
1088d73ce004STejun Heo 	return err;
1089d73ce004STejun Heo }
1090d73ce004STejun Heo 
1091ca22e56dSKay Sievers /**
1092ca22e56dSKay Sievers  * subsys_system_register - register a subsystem at /sys/devices/system/
109378d79559SRandy Dunlap  * @subsys: system subsystem
109478d79559SRandy Dunlap  * @groups: default attributes for the root device
1095ca22e56dSKay Sievers  *
1096ca22e56dSKay Sievers  * All 'system' subsystems have a /sys/devices/system/<name> root device
1097ca22e56dSKay Sievers  * with the name of the subsystem. The root device can carry subsystem-
1098ca22e56dSKay Sievers  * wide attributes. All registered devices are below this single root
1099ca22e56dSKay Sievers  * device and are named after the subsystem with a simple enumeration
1100e227867fSMasanari Iida  * number appended. The registered devices are not explicitly named;
1101ca22e56dSKay Sievers  * only 'id' in the device needs to be set.
1102ca22e56dSKay Sievers  *
1103ca22e56dSKay Sievers  * Do not use this interface for anything new, it exists for compatibility
1104ca22e56dSKay Sievers  * with bad ideas only. New subsystems should use plain subsystems; and
1105ca22e56dSKay Sievers  * add the subsystem-wide attributes should be added to the subsystem
1106ca22e56dSKay Sievers  * directory itself and not some create fake root-device placed in
1107ca22e56dSKay Sievers  * /sys/devices/system/<name>.
1108ca22e56dSKay Sievers  */
1109ca22e56dSKay Sievers int subsys_system_register(struct bus_type *subsys,
1110ca22e56dSKay Sievers 			   const struct attribute_group **groups)
1111ca22e56dSKay Sievers {
1112d73ce004STejun Heo 	return subsys_register(subsys, groups, &system_kset->kobj);
1113ca22e56dSKay Sievers }
1114ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_system_register);
1115ca22e56dSKay Sievers 
1116d73ce004STejun Heo /**
1117d73ce004STejun Heo  * subsys_virtual_register - register a subsystem at /sys/devices/virtual/
1118d73ce004STejun Heo  * @subsys: virtual subsystem
1119d73ce004STejun Heo  * @groups: default attributes for the root device
1120d73ce004STejun Heo  *
1121d73ce004STejun Heo  * All 'virtual' subsystems have a /sys/devices/system/<name> root device
1122d73ce004STejun Heo  * with the name of the subystem.  The root device can carry subsystem-wide
1123d73ce004STejun Heo  * attributes.  All registered devices are below this single root device.
1124d73ce004STejun Heo  * There's no restriction on device naming.  This is for kernel software
1125d73ce004STejun Heo  * constructs which need sysfs interface.
1126d73ce004STejun Heo  */
1127d73ce004STejun Heo int subsys_virtual_register(struct bus_type *subsys,
1128d73ce004STejun Heo 			    const struct attribute_group **groups)
1129d73ce004STejun Heo {
1130d73ce004STejun Heo 	struct kobject *virtual_dir;
1131d73ce004STejun Heo 
1132d73ce004STejun Heo 	virtual_dir = virtual_device_parent(NULL);
1133d73ce004STejun Heo 	if (!virtual_dir)
1134d73ce004STejun Heo 		return -ENOMEM;
1135d73ce004STejun Heo 
1136d73ce004STejun Heo 	return subsys_register(subsys, groups, virtual_dir);
1137d73ce004STejun Heo }
11381c04fc35SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(subsys_virtual_register);
1139d73ce004STejun Heo 
11401da177e4SLinus Torvalds int __init buses_init(void)
11411da177e4SLinus Torvalds {
114259a54833SGreg Kroah-Hartman 	bus_kset = kset_create_and_add("bus", &bus_uevent_ops, NULL);
114359a54833SGreg Kroah-Hartman 	if (!bus_kset)
114459a54833SGreg Kroah-Hartman 		return -ENOMEM;
1145ca22e56dSKay Sievers 
1146ca22e56dSKay Sievers 	system_kset = kset_create_and_add("system", NULL, &devices_kset->kobj);
1147ca22e56dSKay Sievers 	if (!system_kset)
1148ca22e56dSKay Sievers 		return -ENOMEM;
1149ca22e56dSKay Sievers 
115059a54833SGreg Kroah-Hartman 	return 0;
11511da177e4SLinus Torvalds }
1152