xref: /linux/drivers/base/bus.c (revision 4044b2fcfb2048a256529ecbd869b43713982006)
1989d42e8SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * bus.c - bus driver management
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Copyright (c) 2002-3 Patrick Mochel
61da177e4SLinus Torvalds  * Copyright (c) 2002-3 Open Source Development Labs
7e5dd1278SGreg Kroah-Hartman  * Copyright (c) 2007 Greg Kroah-Hartman <gregkh@suse.de>
8e5dd1278SGreg Kroah-Hartman  * Copyright (c) 2007 Novell Inc.
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
11765230b5SDmitry Torokhov #include <linux/async.h>
125aee2bf2SGreg Kroah-Hartman #include <linux/device/bus.h>
131da177e4SLinus Torvalds #include <linux/device.h>
141da177e4SLinus Torvalds #include <linux/module.h>
151da177e4SLinus Torvalds #include <linux/errno.h>
165a0e3ad6STejun Heo #include <linux/slab.h>
171da177e4SLinus Torvalds #include <linux/init.h>
181da177e4SLinus Torvalds #include <linux/string.h>
19ca22e56dSKay Sievers #include <linux/mutex.h>
2063967685SGreg Kroah-Hartman #include <linux/sysfs.h>
211da177e4SLinus Torvalds #include "base.h"
221da177e4SLinus Torvalds #include "power/power.h"
231da177e4SLinus Torvalds 
24ca22e56dSKay Sievers /* /sys/devices/system */
2597ec448aSH Hartley Sweeten static struct kset *system_kset;
26ca22e56dSKay Sievers 
271da177e4SLinus Torvalds #define to_bus_attr(_attr) container_of(_attr, struct bus_attribute, attr)
281da177e4SLinus Torvalds 
291da177e4SLinus Torvalds /*
301da177e4SLinus Torvalds  * sysfs bindings for drivers
311da177e4SLinus Torvalds  */
321da177e4SLinus Torvalds 
331da177e4SLinus Torvalds #define to_drv_attr(_attr) container_of(_attr, struct driver_attribute, attr)
341da177e4SLinus Torvalds 
354f4b3743SDaniel Vetter #define DRIVER_ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) \
364f4b3743SDaniel Vetter 	struct driver_attribute driver_attr_##_name =		\
374f4b3743SDaniel Vetter 		__ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store)
381da177e4SLinus Torvalds 
39b8c5cec2SKay Sievers static int __must_check bus_rescan_devices_helper(struct device *dev,
40b8c5cec2SKay Sievers 						void *data);
41b8c5cec2SKay Sievers 
425901d014SGreg Kroah-Hartman static struct bus_type *bus_get(struct bus_type *bus)
435901d014SGreg Kroah-Hartman {
44c6f7e72aSGreg Kroah-Hartman 	if (bus) {
45c6f7e72aSGreg Kroah-Hartman 		kset_get(&bus->p->subsys);
46c6f7e72aSGreg Kroah-Hartman 		return bus;
47c6f7e72aSGreg Kroah-Hartman 	}
48c6f7e72aSGreg Kroah-Hartman 	return NULL;
495901d014SGreg Kroah-Hartman }
505901d014SGreg Kroah-Hartman 
51fc1ede58SGreg Kroah-Hartman static void bus_put(struct bus_type *bus)
52fc1ede58SGreg Kroah-Hartman {
53c6f7e72aSGreg Kroah-Hartman 	if (bus)
54c6f7e72aSGreg Kroah-Hartman 		kset_put(&bus->p->subsys);
55fc1ede58SGreg Kroah-Hartman }
56fc1ede58SGreg Kroah-Hartman 
574a3ad20cSGreg Kroah-Hartman static ssize_t drv_attr_show(struct kobject *kobj, struct attribute *attr,
584a3ad20cSGreg Kroah-Hartman 			     char *buf)
591da177e4SLinus Torvalds {
601da177e4SLinus Torvalds 	struct driver_attribute *drv_attr = to_drv_attr(attr);
61e5dd1278SGreg Kroah-Hartman 	struct driver_private *drv_priv = to_driver(kobj);
624a0c20bfSDmitry Torokhov 	ssize_t ret = -EIO;
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds 	if (drv_attr->show)
65e5dd1278SGreg Kroah-Hartman 		ret = drv_attr->show(drv_priv->driver, buf);
661da177e4SLinus Torvalds 	return ret;
671da177e4SLinus Torvalds }
681da177e4SLinus Torvalds 
694a3ad20cSGreg Kroah-Hartman static ssize_t drv_attr_store(struct kobject *kobj, struct attribute *attr,
701da177e4SLinus Torvalds 			      const char *buf, size_t count)
711da177e4SLinus Torvalds {
721da177e4SLinus Torvalds 	struct driver_attribute *drv_attr = to_drv_attr(attr);
73e5dd1278SGreg Kroah-Hartman 	struct driver_private *drv_priv = to_driver(kobj);
744a0c20bfSDmitry Torokhov 	ssize_t ret = -EIO;
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds 	if (drv_attr->store)
77e5dd1278SGreg Kroah-Hartman 		ret = drv_attr->store(drv_priv->driver, buf, count);
781da177e4SLinus Torvalds 	return ret;
791da177e4SLinus Torvalds }
801da177e4SLinus Torvalds 
8152cf25d0SEmese Revfy static const struct sysfs_ops driver_sysfs_ops = {
821da177e4SLinus Torvalds 	.show	= drv_attr_show,
831da177e4SLinus Torvalds 	.store	= drv_attr_store,
841da177e4SLinus Torvalds };
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds static void driver_release(struct kobject *kobj)
871da177e4SLinus Torvalds {
88e5dd1278SGreg Kroah-Hartman 	struct driver_private *drv_priv = to_driver(kobj);
89e5dd1278SGreg Kroah-Hartman 
902b3a302aSHarvey Harrison 	pr_debug("driver: '%s': %s\n", kobject_name(kobj), __func__);
91e5dd1278SGreg Kroah-Hartman 	kfree(drv_priv);
921da177e4SLinus Torvalds }
931da177e4SLinus Torvalds 
94a1148fb0SGreg Kroah-Hartman static struct kobj_type driver_ktype = {
951da177e4SLinus Torvalds 	.sysfs_ops	= &driver_sysfs_ops,
961da177e4SLinus Torvalds 	.release	= driver_release,
971da177e4SLinus Torvalds };
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds /*
1001da177e4SLinus Torvalds  * sysfs bindings for buses
1011da177e4SLinus Torvalds  */
1024a3ad20cSGreg Kroah-Hartman static ssize_t bus_attr_show(struct kobject *kobj, struct attribute *attr,
1034a3ad20cSGreg Kroah-Hartman 			     char *buf)
1041da177e4SLinus Torvalds {
1051da177e4SLinus Torvalds 	struct bus_attribute *bus_attr = to_bus_attr(attr);
1066b6e39a6SKay Sievers 	struct subsys_private *subsys_priv = to_subsys_private(kobj);
1071da177e4SLinus Torvalds 	ssize_t ret = 0;
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds 	if (bus_attr->show)
1106b6e39a6SKay Sievers 		ret = bus_attr->show(subsys_priv->bus, buf);
1111da177e4SLinus Torvalds 	return ret;
1121da177e4SLinus Torvalds }
1131da177e4SLinus Torvalds 
1144a3ad20cSGreg Kroah-Hartman static ssize_t bus_attr_store(struct kobject *kobj, struct attribute *attr,
1151da177e4SLinus Torvalds 			      const char *buf, size_t count)
1161da177e4SLinus Torvalds {
1171da177e4SLinus Torvalds 	struct bus_attribute *bus_attr = to_bus_attr(attr);
1186b6e39a6SKay Sievers 	struct subsys_private *subsys_priv = to_subsys_private(kobj);
1191da177e4SLinus Torvalds 	ssize_t ret = 0;
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds 	if (bus_attr->store)
1226b6e39a6SKay Sievers 		ret = bus_attr->store(subsys_priv->bus, buf, count);
1231da177e4SLinus Torvalds 	return ret;
1241da177e4SLinus Torvalds }
1251da177e4SLinus Torvalds 
12652cf25d0SEmese Revfy static const struct sysfs_ops bus_sysfs_ops = {
1271da177e4SLinus Torvalds 	.show	= bus_attr_show,
1281da177e4SLinus Torvalds 	.store	= bus_attr_store,
1291da177e4SLinus Torvalds };
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds int bus_create_file(struct bus_type *bus, struct bus_attribute *attr)
1321da177e4SLinus Torvalds {
1331da177e4SLinus Torvalds 	int error;
1345901d014SGreg Kroah-Hartman 	if (bus_get(bus)) {
135c6f7e72aSGreg Kroah-Hartman 		error = sysfs_create_file(&bus->p->subsys.kobj, &attr->attr);
136fc1ede58SGreg Kroah-Hartman 		bus_put(bus);
1371da177e4SLinus Torvalds 	} else
1381da177e4SLinus Torvalds 		error = -EINVAL;
1391da177e4SLinus Torvalds 	return error;
1401da177e4SLinus Torvalds }
1414a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_create_file);
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds void bus_remove_file(struct bus_type *bus, struct bus_attribute *attr)
1441da177e4SLinus Torvalds {
1455901d014SGreg Kroah-Hartman 	if (bus_get(bus)) {
146c6f7e72aSGreg Kroah-Hartman 		sysfs_remove_file(&bus->p->subsys.kobj, &attr->attr);
147fc1ede58SGreg Kroah-Hartman 		bus_put(bus);
1481da177e4SLinus Torvalds 	}
1491da177e4SLinus Torvalds }
1504a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_remove_file);
1511da177e4SLinus Torvalds 
152174be70bSBart Van Assche static void bus_release(struct kobject *kobj)
153174be70bSBart Van Assche {
154371fd7a2SGeliang Tang 	struct subsys_private *priv = to_subsys_private(kobj);
155174be70bSBart Van Assche 	struct bus_type *bus = priv->bus;
156174be70bSBart Van Assche 
157174be70bSBart Van Assche 	kfree(priv);
158174be70bSBart Van Assche 	bus->p = NULL;
159174be70bSBart Van Assche }
160174be70bSBart Van Assche 
16180f03e34SKay Sievers static struct kobj_type bus_ktype = {
1621da177e4SLinus Torvalds 	.sysfs_ops	= &bus_sysfs_ops,
163174be70bSBart Van Assche 	.release	= bus_release,
1641da177e4SLinus Torvalds };
1651da177e4SLinus Torvalds 
16680f03e34SKay Sievers static int bus_uevent_filter(struct kset *kset, struct kobject *kobj)
16780f03e34SKay Sievers {
16880f03e34SKay Sievers 	struct kobj_type *ktype = get_ktype(kobj);
16980f03e34SKay Sievers 
17080f03e34SKay Sievers 	if (ktype == &bus_ktype)
17180f03e34SKay Sievers 		return 1;
17280f03e34SKay Sievers 	return 0;
17380f03e34SKay Sievers }
17480f03e34SKay Sievers 
1759cd43611SEmese Revfy static const struct kset_uevent_ops bus_uevent_ops = {
17680f03e34SKay Sievers 	.filter = bus_uevent_filter,
17780f03e34SKay Sievers };
17880f03e34SKay Sievers 
17959a54833SGreg Kroah-Hartman static struct kset *bus_kset;
1801da177e4SLinus Torvalds 
1812b08c8d0SAlan Stern /* Manually detach a device from its associated driver. */
1822581c9ccSGreg Kroah-Hartman static ssize_t unbind_store(struct device_driver *drv, const char *buf,
1832581c9ccSGreg Kroah-Hartman 			    size_t count)
184151ef38fSGreg Kroah-Hartman {
1855901d014SGreg Kroah-Hartman 	struct bus_type *bus = bus_get(drv->bus);
186151ef38fSGreg Kroah-Hartman 	struct device *dev;
187151ef38fSGreg Kroah-Hartman 	int err = -ENODEV;
188151ef38fSGreg Kroah-Hartman 
1891f9ffc04SGreg Kroah-Hartman 	dev = bus_find_device_by_name(bus, NULL, buf);
1902b08c8d0SAlan Stern 	if (dev && dev->driver == drv) {
191ed88747cSAlexander Duyck 		device_driver_detach(dev);
192151ef38fSGreg Kroah-Hartman 		err = count;
193151ef38fSGreg Kroah-Hartman 	}
1942b08c8d0SAlan Stern 	put_device(dev);
195fc1ede58SGreg Kroah-Hartman 	bus_put(bus);
196151ef38fSGreg Kroah-Hartman 	return err;
197151ef38fSGreg Kroah-Hartman }
1984f4b3743SDaniel Vetter static DRIVER_ATTR_IGNORE_LOCKDEP(unbind, S_IWUSR, NULL, unbind_store);
199151ef38fSGreg Kroah-Hartman 
200afdce75fSGreg Kroah-Hartman /*
201afdce75fSGreg Kroah-Hartman  * Manually attach a device to a driver.
202afdce75fSGreg Kroah-Hartman  * Note: the driver must want to bind to the device,
203afdce75fSGreg Kroah-Hartman  * it is not possible to override the driver's id table.
204afdce75fSGreg Kroah-Hartman  */
2052581c9ccSGreg Kroah-Hartman static ssize_t bind_store(struct device_driver *drv, const char *buf,
2062581c9ccSGreg Kroah-Hartman 			  size_t count)
207afdce75fSGreg Kroah-Hartman {
2085901d014SGreg Kroah-Hartman 	struct bus_type *bus = bus_get(drv->bus);
209afdce75fSGreg Kroah-Hartman 	struct device *dev;
210afdce75fSGreg Kroah-Hartman 	int err = -ENODEV;
211afdce75fSGreg Kroah-Hartman 
2121f9ffc04SGreg Kroah-Hartman 	dev = bus_find_device_by_name(bus, NULL, buf);
21349b420a1SMing Lei 	if (dev && dev->driver == NULL && driver_match_device(drv, dev)) {
214ed88747cSAlexander Duyck 		err = device_driver_attach(drv, dev);
21537225401SRyan Wilson 
2164a3ad20cSGreg Kroah-Hartman 		if (err > 0) {
2174a3ad20cSGreg Kroah-Hartman 			/* success */
21837225401SRyan Wilson 			err = count;
2194a3ad20cSGreg Kroah-Hartman 		} else if (err == 0) {
2204a3ad20cSGreg Kroah-Hartman 			/* driver didn't accept device */
22137225401SRyan Wilson 			err = -ENODEV;
222afdce75fSGreg Kroah-Hartman 		}
2234a3ad20cSGreg Kroah-Hartman 	}
2242b08c8d0SAlan Stern 	put_device(dev);
225fc1ede58SGreg Kroah-Hartman 	bus_put(bus);
226afdce75fSGreg Kroah-Hartman 	return err;
227afdce75fSGreg Kroah-Hartman }
2284f4b3743SDaniel Vetter static DRIVER_ATTR_IGNORE_LOCKDEP(bind, S_IWUSR, NULL, bind_store);
229afdce75fSGreg Kroah-Hartman 
2302e7189b6SGreg Kroah-Hartman static ssize_t drivers_autoprobe_show(struct bus_type *bus, char *buf)
231b8c5cec2SKay Sievers {
232948b3edbSJoe Perches 	return sysfs_emit(buf, "%d\n", bus->p->drivers_autoprobe);
233b8c5cec2SKay Sievers }
234b8c5cec2SKay Sievers 
2352e7189b6SGreg Kroah-Hartman static ssize_t drivers_autoprobe_store(struct bus_type *bus,
236b8c5cec2SKay Sievers 				       const char *buf, size_t count)
237b8c5cec2SKay Sievers {
238b8c5cec2SKay Sievers 	if (buf[0] == '0')
239c6f7e72aSGreg Kroah-Hartman 		bus->p->drivers_autoprobe = 0;
240b8c5cec2SKay Sievers 	else
241c6f7e72aSGreg Kroah-Hartman 		bus->p->drivers_autoprobe = 1;
242b8c5cec2SKay Sievers 	return count;
243b8c5cec2SKay Sievers }
244b8c5cec2SKay Sievers 
2452e7189b6SGreg Kroah-Hartman static ssize_t drivers_probe_store(struct bus_type *bus,
246b8c5cec2SKay Sievers 				   const char *buf, size_t count)
247b8c5cec2SKay Sievers {
248b8c5cec2SKay Sievers 	struct device *dev;
2490372ffb3SAlex Williamson 	int err = -EINVAL;
250b8c5cec2SKay Sievers 
2511f9ffc04SGreg Kroah-Hartman 	dev = bus_find_device_by_name(bus, NULL, buf);
252b8c5cec2SKay Sievers 	if (!dev)
253b8c5cec2SKay Sievers 		return -ENODEV;
2540372ffb3SAlex Williamson 	if (bus_rescan_devices_helper(dev, NULL) == 0)
2550372ffb3SAlex Williamson 		err = count;
2560372ffb3SAlex Williamson 	put_device(dev);
2570372ffb3SAlex Williamson 	return err;
258b8c5cec2SKay Sievers }
259151ef38fSGreg Kroah-Hartman 
260465c7a3aSmochel@digitalimplant.org static struct device *next_device(struct klist_iter *i)
261465c7a3aSmochel@digitalimplant.org {
262465c7a3aSmochel@digitalimplant.org 	struct klist_node *n = klist_next(i);
263ae1b4171SGreg Kroah-Hartman 	struct device *dev = NULL;
264ae1b4171SGreg Kroah-Hartman 	struct device_private *dev_prv;
265ae1b4171SGreg Kroah-Hartman 
266ae1b4171SGreg Kroah-Hartman 	if (n) {
267ae1b4171SGreg Kroah-Hartman 		dev_prv = to_device_private_bus(n);
268ae1b4171SGreg Kroah-Hartman 		dev = dev_prv->device;
269ae1b4171SGreg Kroah-Hartman 	}
270ae1b4171SGreg Kroah-Hartman 	return dev;
271465c7a3aSmochel@digitalimplant.org }
272465c7a3aSmochel@digitalimplant.org 
2731da177e4SLinus Torvalds /**
2741da177e4SLinus Torvalds  * bus_for_each_dev - device iterator.
2751da177e4SLinus Torvalds  * @bus: bus type.
2761da177e4SLinus Torvalds  * @start: device to start iterating from.
2771da177e4SLinus Torvalds  * @data: data for the callback.
2781da177e4SLinus Torvalds  * @fn: function to be called for each device.
2791da177e4SLinus Torvalds  *
2801da177e4SLinus Torvalds  * Iterate over @bus's list of devices, and call @fn for each,
2811da177e4SLinus Torvalds  * passing it @data. If @start is not NULL, we use that device to
2821da177e4SLinus Torvalds  * begin iterating from.
2831da177e4SLinus Torvalds  *
2841da177e4SLinus Torvalds  * We check the return of @fn each time. If it returns anything
2851da177e4SLinus Torvalds  * other than 0, we break out and return that value.
2861da177e4SLinus Torvalds  *
2871da177e4SLinus Torvalds  * NOTE: The device that returns a non-zero value is not retained
2881da177e4SLinus Torvalds  * in any way, nor is its refcount incremented. If the caller needs
2890fa1b0a1SAlex Chiang  * to retain this data, it should do so, and increment the reference
2901da177e4SLinus Torvalds  * count in the supplied callback.
2911da177e4SLinus Torvalds  */
2921da177e4SLinus Torvalds int bus_for_each_dev(struct bus_type *bus, struct device *start,
2931da177e4SLinus Torvalds 		     void *data, int (*fn)(struct device *, void *))
2941da177e4SLinus Torvalds {
295465c7a3aSmochel@digitalimplant.org 	struct klist_iter i;
296465c7a3aSmochel@digitalimplant.org 	struct device *dev;
297465c7a3aSmochel@digitalimplant.org 	int error = 0;
2981da177e4SLinus Torvalds 
2994fa3e78bSBjorn Helgaas 	if (!bus || !bus->p)
300465c7a3aSmochel@digitalimplant.org 		return -EINVAL;
301465c7a3aSmochel@digitalimplant.org 
3027cd9c9bbSGreg Kroah-Hartman 	klist_iter_init_node(&bus->p->klist_devices, &i,
303ae1b4171SGreg Kroah-Hartman 			     (start ? &start->p->knode_bus : NULL));
30493ead7c9SGimcuan Hui 	while (!error && (dev = next_device(&i)))
305465c7a3aSmochel@digitalimplant.org 		error = fn(dev, data);
306465c7a3aSmochel@digitalimplant.org 	klist_iter_exit(&i);
307465c7a3aSmochel@digitalimplant.org 	return error;
3081da177e4SLinus Torvalds }
3094a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_for_each_dev);
3101da177e4SLinus Torvalds 
3110edb5860SCornelia Huck /**
3120edb5860SCornelia Huck  * bus_find_device - device iterator for locating a particular device.
3130edb5860SCornelia Huck  * @bus: bus type
3140edb5860SCornelia Huck  * @start: Device to begin with
3150edb5860SCornelia Huck  * @data: Data to pass to match function
3160edb5860SCornelia Huck  * @match: Callback function to check device
3170edb5860SCornelia Huck  *
3180edb5860SCornelia Huck  * This is similar to the bus_for_each_dev() function above, but it
3190edb5860SCornelia Huck  * returns a reference to a device that is 'found' for later use, as
3200edb5860SCornelia Huck  * determined by the @match callback.
3210edb5860SCornelia Huck  *
3220edb5860SCornelia Huck  * The callback should return 0 if the device doesn't match and non-zero
3230edb5860SCornelia Huck  * if it does.  If the callback returns non-zero, this function will
3240edb5860SCornelia Huck  * return to the caller and not iterate over any more devices.
3250edb5860SCornelia Huck  */
3260edb5860SCornelia Huck struct device *bus_find_device(struct bus_type *bus,
327418e3ea1SSuzuki K Poulose 			       struct device *start, const void *data,
328418e3ea1SSuzuki K Poulose 			       int (*match)(struct device *dev, const void *data))
3290edb5860SCornelia Huck {
3300edb5860SCornelia Huck 	struct klist_iter i;
3310edb5860SCornelia Huck 	struct device *dev;
3320edb5860SCornelia Huck 
3334fa3e78bSBjorn Helgaas 	if (!bus || !bus->p)
3340edb5860SCornelia Huck 		return NULL;
3350edb5860SCornelia Huck 
3367cd9c9bbSGreg Kroah-Hartman 	klist_iter_init_node(&bus->p->klist_devices, &i,
3377cd9c9bbSGreg Kroah-Hartman 			     (start ? &start->p->knode_bus : NULL));
3380edb5860SCornelia Huck 	while ((dev = next_device(&i)))
3390edb5860SCornelia Huck 		if (match(dev, data) && get_device(dev))
3400edb5860SCornelia Huck 			break;
3410edb5860SCornelia Huck 	klist_iter_exit(&i);
3420edb5860SCornelia Huck 	return dev;
3430edb5860SCornelia Huck }
3444a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_find_device);
34538fdac3cSmochel@digitalimplant.org 
346ca22e56dSKay Sievers /**
347ca22e56dSKay Sievers  * subsys_find_device_by_id - find a device with a specific enumeration number
348ca22e56dSKay Sievers  * @subsys: subsystem
349ca22e56dSKay Sievers  * @id: index 'id' in struct device
350ca22e56dSKay Sievers  * @hint: device to check first
351ca22e56dSKay Sievers  *
352ca22e56dSKay Sievers  * Check the hint's next object and if it is a match return it directly,
353ca22e56dSKay Sievers  * otherwise, fall back to a full list search. Either way a reference for
354ca22e56dSKay Sievers  * the returned object is taken.
355ca22e56dSKay Sievers  */
356ca22e56dSKay Sievers struct device *subsys_find_device_by_id(struct bus_type *subsys, unsigned int id,
357ca22e56dSKay Sievers 					struct device *hint)
358ca22e56dSKay Sievers {
359ca22e56dSKay Sievers 	struct klist_iter i;
360ca22e56dSKay Sievers 	struct device *dev;
361ca22e56dSKay Sievers 
362ca22e56dSKay Sievers 	if (!subsys)
363ca22e56dSKay Sievers 		return NULL;
364ca22e56dSKay Sievers 
365ca22e56dSKay Sievers 	if (hint) {
3667cd9c9bbSGreg Kroah-Hartman 		klist_iter_init_node(&subsys->p->klist_devices, &i, &hint->p->knode_bus);
367ca22e56dSKay Sievers 		dev = next_device(&i);
368ca22e56dSKay Sievers 		if (dev && dev->id == id && get_device(dev)) {
369ca22e56dSKay Sievers 			klist_iter_exit(&i);
370ca22e56dSKay Sievers 			return dev;
371ca22e56dSKay Sievers 		}
372ca22e56dSKay Sievers 		klist_iter_exit(&i);
373ca22e56dSKay Sievers 	}
374ca22e56dSKay Sievers 
375ca22e56dSKay Sievers 	klist_iter_init_node(&subsys->p->klist_devices, &i, NULL);
376ca22e56dSKay Sievers 	while ((dev = next_device(&i))) {
377ca22e56dSKay Sievers 		if (dev->id == id && get_device(dev)) {
378ca22e56dSKay Sievers 			klist_iter_exit(&i);
379ca22e56dSKay Sievers 			return dev;
380ca22e56dSKay Sievers 		}
381ca22e56dSKay Sievers 	}
382ca22e56dSKay Sievers 	klist_iter_exit(&i);
383ca22e56dSKay Sievers 	return NULL;
384ca22e56dSKay Sievers }
385ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_find_device_by_id);
386ca22e56dSKay Sievers 
38738fdac3cSmochel@digitalimplant.org static struct device_driver *next_driver(struct klist_iter *i)
38838fdac3cSmochel@digitalimplant.org {
38938fdac3cSmochel@digitalimplant.org 	struct klist_node *n = klist_next(i);
390e5dd1278SGreg Kroah-Hartman 	struct driver_private *drv_priv;
391e5dd1278SGreg Kroah-Hartman 
392e5dd1278SGreg Kroah-Hartman 	if (n) {
393e5dd1278SGreg Kroah-Hartman 		drv_priv = container_of(n, struct driver_private, knode_bus);
394e5dd1278SGreg Kroah-Hartman 		return drv_priv->driver;
395e5dd1278SGreg Kroah-Hartman 	}
396e5dd1278SGreg Kroah-Hartman 	return NULL;
39738fdac3cSmochel@digitalimplant.org }
39838fdac3cSmochel@digitalimplant.org 
3991da177e4SLinus Torvalds /**
4001da177e4SLinus Torvalds  * bus_for_each_drv - driver iterator
4011da177e4SLinus Torvalds  * @bus: bus we're dealing with.
4021da177e4SLinus Torvalds  * @start: driver to start iterating on.
4031da177e4SLinus Torvalds  * @data: data to pass to the callback.
4041da177e4SLinus Torvalds  * @fn: function to call for each driver.
4051da177e4SLinus Torvalds  *
4061da177e4SLinus Torvalds  * This is nearly identical to the device iterator above.
4071da177e4SLinus Torvalds  * We iterate over each driver that belongs to @bus, and call
4081da177e4SLinus Torvalds  * @fn for each. If @fn returns anything but 0, we break out
4091da177e4SLinus Torvalds  * and return it. If @start is not NULL, we use it as the head
4101da177e4SLinus Torvalds  * of the list.
4111da177e4SLinus Torvalds  *
4121da177e4SLinus Torvalds  * NOTE: we don't return the driver that returns a non-zero
4131da177e4SLinus Torvalds  * value, nor do we leave the reference count incremented for that
4141da177e4SLinus Torvalds  * driver. If the caller needs to know that info, it must set it
4151da177e4SLinus Torvalds  * in the callback. It must also be sure to increment the refcount
4161da177e4SLinus Torvalds  * so it doesn't disappear before returning to the caller.
4171da177e4SLinus Torvalds  */
4181da177e4SLinus Torvalds int bus_for_each_drv(struct bus_type *bus, struct device_driver *start,
4191da177e4SLinus Torvalds 		     void *data, int (*fn)(struct device_driver *, void *))
4201da177e4SLinus Torvalds {
42138fdac3cSmochel@digitalimplant.org 	struct klist_iter i;
42238fdac3cSmochel@digitalimplant.org 	struct device_driver *drv;
42338fdac3cSmochel@digitalimplant.org 	int error = 0;
4241da177e4SLinus Torvalds 
42538fdac3cSmochel@digitalimplant.org 	if (!bus)
42638fdac3cSmochel@digitalimplant.org 		return -EINVAL;
42738fdac3cSmochel@digitalimplant.org 
4287cd9c9bbSGreg Kroah-Hartman 	klist_iter_init_node(&bus->p->klist_drivers, &i,
429e5dd1278SGreg Kroah-Hartman 			     start ? &start->p->knode_bus : NULL);
43038fdac3cSmochel@digitalimplant.org 	while ((drv = next_driver(&i)) && !error)
43138fdac3cSmochel@digitalimplant.org 		error = fn(drv, data);
43238fdac3cSmochel@digitalimplant.org 	klist_iter_exit(&i);
43338fdac3cSmochel@digitalimplant.org 	return error;
4341da177e4SLinus Torvalds }
4354a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_for_each_drv);
4361da177e4SLinus Torvalds 
4371da177e4SLinus Torvalds /**
4381da177e4SLinus Torvalds  * bus_add_device - add device to bus
4391da177e4SLinus Torvalds  * @dev: device being added
4401da177e4SLinus Torvalds  *
4412023c610SAlan Stern  * - Add device's bus attributes.
4422023c610SAlan Stern  * - Create links to device's bus.
4431da177e4SLinus Torvalds  * - Add the device to its bus's list of devices.
4441da177e4SLinus Torvalds  */
4451da177e4SLinus Torvalds int bus_add_device(struct device *dev)
4461da177e4SLinus Torvalds {
4475901d014SGreg Kroah-Hartman 	struct bus_type *bus = bus_get(dev->bus);
4481da177e4SLinus Torvalds 	int error = 0;
4491da177e4SLinus Torvalds 
4501da177e4SLinus Torvalds 	if (bus) {
4511e0b2cf9SKay Sievers 		pr_debug("bus: '%s': add device %s\n", bus->name, dev_name(dev));
452fa6fdb33SGreg Kroah-Hartman 		error = device_add_groups(dev, bus->dev_groups);
453fa6fdb33SGreg Kroah-Hartman 		if (error)
454b46c7337SGreg Kroah-Hartman 			goto out_put;
455c6f7e72aSGreg Kroah-Hartman 		error = sysfs_create_link(&bus->p->devices_kset->kobj,
4561e0b2cf9SKay Sievers 						&dev->kobj, dev_name(dev));
457f86db396SAndrew Morton 		if (error)
4581c34203aSJunjie Mao 			goto out_groups;
459f86db396SAndrew Morton 		error = sysfs_create_link(&dev->kobj,
460c6f7e72aSGreg Kroah-Hartman 				&dev->bus->p->subsys.kobj, "subsystem");
461f86db396SAndrew Morton 		if (error)
462513e7337SCornelia Huck 			goto out_subsys;
4632023c610SAlan Stern 		klist_add_tail(&dev->p->knode_bus, &bus->p->klist_devices);
4641da177e4SLinus Torvalds 	}
465513e7337SCornelia Huck 	return 0;
466513e7337SCornelia Huck 
467513e7337SCornelia Huck out_subsys:
4681e0b2cf9SKay Sievers 	sysfs_remove_link(&bus->p->devices_kset->kobj, dev_name(dev));
469fa6fdb33SGreg Kroah-Hartman out_groups:
470fa6fdb33SGreg Kroah-Hartman 	device_remove_groups(dev, bus->dev_groups);
471513e7337SCornelia Huck out_put:
472fc1ede58SGreg Kroah-Hartman 	bus_put(dev->bus);
4731da177e4SLinus Torvalds 	return error;
4741da177e4SLinus Torvalds }
4751da177e4SLinus Torvalds 
4761da177e4SLinus Torvalds /**
4772023c610SAlan Stern  * bus_probe_device - probe drivers for a new device
4782023c610SAlan Stern  * @dev: device to probe
47953877d06SKay Sievers  *
4802023c610SAlan Stern  * - Automatically probe for a driver if the bus allows it.
48153877d06SKay Sievers  */
4822023c610SAlan Stern void bus_probe_device(struct device *dev)
48353877d06SKay Sievers {
48453877d06SKay Sievers 	struct bus_type *bus = dev->bus;
485ca22e56dSKay Sievers 	struct subsys_interface *sif;
48653877d06SKay Sievers 
487ca22e56dSKay Sievers 	if (!bus)
488ca22e56dSKay Sievers 		return;
489ca22e56dSKay Sievers 
490765230b5SDmitry Torokhov 	if (bus->p->drivers_autoprobe)
491765230b5SDmitry Torokhov 		device_initial_probe(dev);
492ca22e56dSKay Sievers 
493ca22e56dSKay Sievers 	mutex_lock(&bus->p->mutex);
494ca22e56dSKay Sievers 	list_for_each_entry(sif, &bus->p->interfaces, node)
495ca22e56dSKay Sievers 		if (sif->add_dev)
496ca22e56dSKay Sievers 			sif->add_dev(dev, sif);
497ca22e56dSKay Sievers 	mutex_unlock(&bus->p->mutex);
498f86db396SAndrew Morton }
49953877d06SKay Sievers 
50053877d06SKay Sievers /**
5011da177e4SLinus Torvalds  * bus_remove_device - remove device from bus
5021da177e4SLinus Torvalds  * @dev: device to be removed
5031da177e4SLinus Torvalds  *
504ca22e56dSKay Sievers  * - Remove device from all interfaces.
505ca22e56dSKay Sievers  * - Remove symlink from bus' directory.
5061da177e4SLinus Torvalds  * - Delete device from bus's list.
5071da177e4SLinus Torvalds  * - Detach from its driver.
5081da177e4SLinus Torvalds  * - Drop reference taken in bus_add_device().
5091da177e4SLinus Torvalds  */
5101da177e4SLinus Torvalds void bus_remove_device(struct device *dev)
5111da177e4SLinus Torvalds {
512ca22e56dSKay Sievers 	struct bus_type *bus = dev->bus;
513ca22e56dSKay Sievers 	struct subsys_interface *sif;
514ca22e56dSKay Sievers 
515ca22e56dSKay Sievers 	if (!bus)
516ca22e56dSKay Sievers 		return;
517ca22e56dSKay Sievers 
518ca22e56dSKay Sievers 	mutex_lock(&bus->p->mutex);
519ca22e56dSKay Sievers 	list_for_each_entry(sif, &bus->p->interfaces, node)
520ca22e56dSKay Sievers 		if (sif->remove_dev)
521ca22e56dSKay Sievers 			sif->remove_dev(dev, sif);
522ca22e56dSKay Sievers 	mutex_unlock(&bus->p->mutex);
523ca22e56dSKay Sievers 
524b9d9c82bSKay Sievers 	sysfs_remove_link(&dev->kobj, "subsystem");
5254a3ad20cSGreg Kroah-Hartman 	sysfs_remove_link(&dev->bus->p->devices_kset->kobj,
5261e0b2cf9SKay Sievers 			  dev_name(dev));
527fa6fdb33SGreg Kroah-Hartman 	device_remove_groups(dev, dev->bus->dev_groups);
528ae1b4171SGreg Kroah-Hartman 	if (klist_node_attached(&dev->p->knode_bus))
529ae1b4171SGreg Kroah-Hartman 		klist_del(&dev->p->knode_bus);
5303f62e570SGreg Kroah-Hartman 
5314a3ad20cSGreg Kroah-Hartman 	pr_debug("bus: '%s': remove device %s\n",
5321e0b2cf9SKay Sievers 		 dev->bus->name, dev_name(dev));
5331da177e4SLinus Torvalds 	device_release_driver(dev);
534fc1ede58SGreg Kroah-Hartman 	bus_put(dev->bus);
5351da177e4SLinus Torvalds }
5361da177e4SLinus Torvalds 
537f86db396SAndrew Morton static int __must_check add_bind_files(struct device_driver *drv)
538874c6241SGreg Kroah-Hartman {
539f86db396SAndrew Morton 	int ret;
540f86db396SAndrew Morton 
541f86db396SAndrew Morton 	ret = driver_create_file(drv, &driver_attr_unbind);
542f86db396SAndrew Morton 	if (ret == 0) {
543f86db396SAndrew Morton 		ret = driver_create_file(drv, &driver_attr_bind);
544f86db396SAndrew Morton 		if (ret)
545f86db396SAndrew Morton 			driver_remove_file(drv, &driver_attr_unbind);
546f86db396SAndrew Morton 	}
547f86db396SAndrew Morton 	return ret;
548874c6241SGreg Kroah-Hartman }
549874c6241SGreg Kroah-Hartman 
550874c6241SGreg Kroah-Hartman static void remove_bind_files(struct device_driver *drv)
551874c6241SGreg Kroah-Hartman {
552874c6241SGreg Kroah-Hartman 	driver_remove_file(drv, &driver_attr_bind);
553874c6241SGreg Kroah-Hartman 	driver_remove_file(drv, &driver_attr_unbind);
554874c6241SGreg Kroah-Hartman }
555b8c5cec2SKay Sievers 
5562e7189b6SGreg Kroah-Hartman static BUS_ATTR_WO(drivers_probe);
5572e7189b6SGreg Kroah-Hartman static BUS_ATTR_RW(drivers_autoprobe);
5588380770cSKay Sievers 
559b8c5cec2SKay Sievers static int add_probe_files(struct bus_type *bus)
560b8c5cec2SKay Sievers {
561b8c5cec2SKay Sievers 	int retval;
562b8c5cec2SKay Sievers 
5638380770cSKay Sievers 	retval = bus_create_file(bus, &bus_attr_drivers_probe);
564b8c5cec2SKay Sievers 	if (retval)
565b8c5cec2SKay Sievers 		goto out;
566b8c5cec2SKay Sievers 
5678380770cSKay Sievers 	retval = bus_create_file(bus, &bus_attr_drivers_autoprobe);
568b8c5cec2SKay Sievers 	if (retval)
5698380770cSKay Sievers 		bus_remove_file(bus, &bus_attr_drivers_probe);
570b8c5cec2SKay Sievers out:
571b8c5cec2SKay Sievers 	return retval;
572b8c5cec2SKay Sievers }
573b8c5cec2SKay Sievers 
574b8c5cec2SKay Sievers static void remove_probe_files(struct bus_type *bus)
575b8c5cec2SKay Sievers {
5768380770cSKay Sievers 	bus_remove_file(bus, &bus_attr_drivers_autoprobe);
5778380770cSKay Sievers 	bus_remove_file(bus, &bus_attr_drivers_probe);
578b8c5cec2SKay Sievers }
5791da177e4SLinus Torvalds 
5802581c9ccSGreg Kroah-Hartman static ssize_t uevent_store(struct device_driver *drv, const char *buf,
5812581c9ccSGreg Kroah-Hartman 			    size_t count)
5827ac1cf4aSKay Sievers {
583df44b479SPeter Rajnoha 	int rc;
584df44b479SPeter Rajnoha 
585df44b479SPeter Rajnoha 	rc = kobject_synth_uevent(&drv->p->kobj, buf, count);
586df44b479SPeter Rajnoha 	return rc ? rc : count;
5877ac1cf4aSKay Sievers }
5882581c9ccSGreg Kroah-Hartman static DRIVER_ATTR_WO(uevent);
5897ac1cf4aSKay Sievers 
5901da177e4SLinus Torvalds /**
5911da177e4SLinus Torvalds  * bus_add_driver - Add a driver to the bus.
5921da177e4SLinus Torvalds  * @drv: driver.
5931da177e4SLinus Torvalds  */
5941da177e4SLinus Torvalds int bus_add_driver(struct device_driver *drv)
5951da177e4SLinus Torvalds {
596e5dd1278SGreg Kroah-Hartman 	struct bus_type *bus;
597e5dd1278SGreg Kroah-Hartman 	struct driver_private *priv;
5981da177e4SLinus Torvalds 	int error = 0;
5991da177e4SLinus Torvalds 
600e5dd1278SGreg Kroah-Hartman 	bus = bus_get(drv->bus);
601d9fd4d3bSJeff Garzik 	if (!bus)
6024f6e1945SGreg Kroah-Hartman 		return -EINVAL;
603d9fd4d3bSJeff Garzik 
6047dc72b28SGreg Kroah-Hartman 	pr_debug("bus: '%s': add driver %s\n", bus->name, drv->name);
605e5dd1278SGreg Kroah-Hartman 
606e5dd1278SGreg Kroah-Hartman 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
60707634464SCornelia Huck 	if (!priv) {
60807634464SCornelia Huck 		error = -ENOMEM;
60907634464SCornelia Huck 		goto out_put_bus;
61007634464SCornelia Huck 	}
611e5dd1278SGreg Kroah-Hartman 	klist_init(&priv->klist_devices, NULL, NULL);
612e5dd1278SGreg Kroah-Hartman 	priv->driver = drv;
613e5dd1278SGreg Kroah-Hartman 	drv->p = priv;
614c8e90d82SGreg Kroah-Hartman 	priv->kobj.kset = bus->p->drivers_kset;
615c8e90d82SGreg Kroah-Hartman 	error = kobject_init_and_add(&priv->kobj, &driver_ktype, NULL,
616c8e90d82SGreg Kroah-Hartman 				     "%s", drv->name);
617dc0afa83SCornelia Huck 	if (error)
61807634464SCornelia Huck 		goto out_unregister;
6191da177e4SLinus Torvalds 
620190888acSMing Lei 	klist_add_tail(&priv->knode_bus, &bus->p->klist_drivers);
621c6f7e72aSGreg Kroah-Hartman 	if (drv->bus->p->drivers_autoprobe) {
622f86db396SAndrew Morton 		error = driver_attach(drv);
623f86db396SAndrew Morton 		if (error)
624f86db396SAndrew Morton 			goto out_unregister;
625b8c5cec2SKay Sievers 	}
6261da177e4SLinus Torvalds 	module_add_driver(drv->owner, drv);
6271da177e4SLinus Torvalds 
6287ac1cf4aSKay Sievers 	error = driver_create_file(drv, &driver_attr_uevent);
6297ac1cf4aSKay Sievers 	if (error) {
6307ac1cf4aSKay Sievers 		printk(KERN_ERR "%s: uevent attr (%s) failed\n",
6312b3a302aSHarvey Harrison 			__func__, drv->name);
6327ac1cf4aSKay Sievers 	}
633e18945b1SGreg Kroah-Hartman 	error = driver_add_groups(drv, bus->drv_groups);
634f86db396SAndrew Morton 	if (error) {
635f86db396SAndrew Morton 		/* How the hell do we get out of this pickle? Give up */
636*4044b2fcSJoe Pater 		printk(KERN_ERR "%s: driver_add_groups(%s) failed\n",
637ed0617b5SGreg Kroah-Hartman 			__func__, drv->name);
638e18945b1SGreg Kroah-Hartman 	}
6391a6f2a75SDmitry Torokhov 
6401a6f2a75SDmitry Torokhov 	if (!drv->suppress_bind_attrs) {
641f86db396SAndrew Morton 		error = add_bind_files(drv);
642f86db396SAndrew Morton 		if (error) {
643f86db396SAndrew Morton 			/* Ditto */
644f86db396SAndrew Morton 			printk(KERN_ERR "%s: add_bind_files(%s) failed\n",
6452b3a302aSHarvey Harrison 				__func__, drv->name);
646f86db396SAndrew Morton 		}
6471a6f2a75SDmitry Torokhov 	}
648d9fd4d3bSJeff Garzik 
6495c8563d7SKay Sievers 	return 0;
6501a6f2a75SDmitry Torokhov 
651f86db396SAndrew Morton out_unregister:
65299b28f1bSPhil Carmody 	kobject_put(&priv->kobj);
6530f9b011dSChristophe JAILLET 	/* drv->p is freed in driver_release()  */
6545c8563d7SKay Sievers 	drv->p = NULL;
655f86db396SAndrew Morton out_put_bus:
656fc1ede58SGreg Kroah-Hartman 	bus_put(bus);
657f86db396SAndrew Morton 	return error;
6581da177e4SLinus Torvalds }
6591da177e4SLinus Torvalds 
6601da177e4SLinus Torvalds /**
6611da177e4SLinus Torvalds  * bus_remove_driver - delete driver from bus's knowledge.
6621da177e4SLinus Torvalds  * @drv: driver.
6631da177e4SLinus Torvalds  *
6641da177e4SLinus Torvalds  * Detach the driver from the devices it controls, and remove
6651da177e4SLinus Torvalds  * it from its bus's list of drivers. Finally, we drop the reference
6661da177e4SLinus Torvalds  * to the bus we took in bus_add_driver().
6671da177e4SLinus Torvalds  */
6681da177e4SLinus Torvalds void bus_remove_driver(struct device_driver *drv)
6691da177e4SLinus Torvalds {
670d9fd4d3bSJeff Garzik 	if (!drv->bus)
671d9fd4d3bSJeff Garzik 		return;
672d9fd4d3bSJeff Garzik 
6731a6f2a75SDmitry Torokhov 	if (!drv->suppress_bind_attrs)
674874c6241SGreg Kroah-Hartman 		remove_bind_files(drv);
675ed0617b5SGreg Kroah-Hartman 	driver_remove_groups(drv, drv->bus->drv_groups);
6767ac1cf4aSKay Sievers 	driver_remove_file(drv, &driver_attr_uevent);
677e5dd1278SGreg Kroah-Hartman 	klist_remove(&drv->p->knode_bus);
6787dc72b28SGreg Kroah-Hartman 	pr_debug("bus: '%s': remove driver %s\n", drv->bus->name, drv->name);
6791da177e4SLinus Torvalds 	driver_detach(drv);
6801da177e4SLinus Torvalds 	module_remove_driver(drv);
681c10997f6SGreg Kroah-Hartman 	kobject_put(&drv->p->kobj);
682fc1ede58SGreg Kroah-Hartman 	bus_put(drv->bus);
6831da177e4SLinus Torvalds }
6841da177e4SLinus Torvalds 
6851da177e4SLinus Torvalds /* Helper for bus_rescan_devices's iter */
686f86db396SAndrew Morton static int __must_check bus_rescan_devices_helper(struct device *dev,
687f86db396SAndrew Morton 						  void *data)
6881da177e4SLinus Torvalds {
689f86db396SAndrew Morton 	int ret = 0;
690f86db396SAndrew Morton 
691bf74ad5bSAlan Stern 	if (!dev->driver) {
6928c97a46aSMartin Liu 		if (dev->parent && dev->bus->need_parent_lock)
6938e9394ceSGreg Kroah-Hartman 			device_lock(dev->parent);
694f86db396SAndrew Morton 		ret = device_attach(dev);
6958c97a46aSMartin Liu 		if (dev->parent && dev->bus->need_parent_lock)
6968e9394ceSGreg Kroah-Hartman 			device_unlock(dev->parent);
697bf74ad5bSAlan Stern 	}
698f86db396SAndrew Morton 	return ret < 0 ? ret : 0;
6991da177e4SLinus Torvalds }
7001da177e4SLinus Torvalds 
7011da177e4SLinus Torvalds /**
7021da177e4SLinus Torvalds  * bus_rescan_devices - rescan devices on the bus for possible drivers
7031da177e4SLinus Torvalds  * @bus: the bus to scan.
7041da177e4SLinus Torvalds  *
7051da177e4SLinus Torvalds  * This function will look for devices on the bus with no driver
70623d3d602SGreg Kroah-Hartman  * attached and rescan it against existing drivers to see if it matches
70723d3d602SGreg Kroah-Hartman  * any by calling device_attach() for the unbound devices.
7081da177e4SLinus Torvalds  */
709f86db396SAndrew Morton int bus_rescan_devices(struct bus_type *bus)
7101da177e4SLinus Torvalds {
711f86db396SAndrew Morton 	return bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper);
7121da177e4SLinus Torvalds }
7134a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_rescan_devices);
7141da177e4SLinus Torvalds 
715e935d5daSMoore, Eric /**
716e935d5daSMoore, Eric  * device_reprobe - remove driver for a device and probe for a new driver
717e935d5daSMoore, Eric  * @dev: the device to reprobe
718e935d5daSMoore, Eric  *
719e935d5daSMoore, Eric  * This function detaches the attached driver (if any) for the given
720e935d5daSMoore, Eric  * device and restarts the driver probing process.  It is intended
721e935d5daSMoore, Eric  * to use if probing criteria changed during a devices lifetime and
722e935d5daSMoore, Eric  * driver attachment should change accordingly.
723e935d5daSMoore, Eric  */
724f86db396SAndrew Morton int device_reprobe(struct device *dev)
725e935d5daSMoore, Eric {
726ed88747cSAlexander Duyck 	if (dev->driver)
727ed88747cSAlexander Duyck 		device_driver_detach(dev);
728f86db396SAndrew Morton 	return bus_rescan_devices_helper(dev, NULL);
729e935d5daSMoore, Eric }
730e935d5daSMoore, Eric EXPORT_SYMBOL_GPL(device_reprobe);
7311da177e4SLinus Torvalds 
73212478ba0SGreg Kroah-Hartman static int bus_add_groups(struct bus_type *bus,
73312478ba0SGreg Kroah-Hartman 			  const struct attribute_group **groups)
73412478ba0SGreg Kroah-Hartman {
7353e9b2baeSGreg Kroah-Hartman 	return sysfs_create_groups(&bus->p->subsys.kobj, groups);
73612478ba0SGreg Kroah-Hartman }
73712478ba0SGreg Kroah-Hartman 
73812478ba0SGreg Kroah-Hartman static void bus_remove_groups(struct bus_type *bus,
73912478ba0SGreg Kroah-Hartman 			      const struct attribute_group **groups)
74012478ba0SGreg Kroah-Hartman {
7413e9b2baeSGreg Kroah-Hartman 	sysfs_remove_groups(&bus->p->subsys.kobj, groups);
74212478ba0SGreg Kroah-Hartman }
74312478ba0SGreg Kroah-Hartman 
74434bb61f9SJames Bottomley static void klist_devices_get(struct klist_node *n)
74534bb61f9SJames Bottomley {
746ae1b4171SGreg Kroah-Hartman 	struct device_private *dev_prv = to_device_private_bus(n);
747ae1b4171SGreg Kroah-Hartman 	struct device *dev = dev_prv->device;
74834bb61f9SJames Bottomley 
74934bb61f9SJames Bottomley 	get_device(dev);
75034bb61f9SJames Bottomley }
75134bb61f9SJames Bottomley 
75234bb61f9SJames Bottomley static void klist_devices_put(struct klist_node *n)
75334bb61f9SJames Bottomley {
754ae1b4171SGreg Kroah-Hartman 	struct device_private *dev_prv = to_device_private_bus(n);
755ae1b4171SGreg Kroah-Hartman 	struct device *dev = dev_prv->device;
75634bb61f9SJames Bottomley 
75734bb61f9SJames Bottomley 	put_device(dev);
75834bb61f9SJames Bottomley }
75934bb61f9SJames Bottomley 
7607ac1cf4aSKay Sievers static ssize_t bus_uevent_store(struct bus_type *bus,
7617ac1cf4aSKay Sievers 				const char *buf, size_t count)
7627ac1cf4aSKay Sievers {
763df44b479SPeter Rajnoha 	int rc;
764df44b479SPeter Rajnoha 
765df44b479SPeter Rajnoha 	rc = kobject_synth_uevent(&bus->p->subsys.kobj, buf, count);
766df44b479SPeter Rajnoha 	return rc ? rc : count;
7677ac1cf4aSKay Sievers }
768a4723041SGreg Kroah-Hartman /*
769a4723041SGreg Kroah-Hartman  * "open code" the old BUS_ATTR() macro here.  We want to use BUS_ATTR_WO()
770a4723041SGreg Kroah-Hartman  * here, but can not use it as earlier in the file we have
771a4723041SGreg Kroah-Hartman  * DEVICE_ATTR_WO(uevent), which would cause a clash with the with the store
772a4723041SGreg Kroah-Hartman  * function name.
773a4723041SGreg Kroah-Hartman  */
774a4723041SGreg Kroah-Hartman static struct bus_attribute bus_attr_uevent = __ATTR(uevent, S_IWUSR, NULL,
775a4723041SGreg Kroah-Hartman 						     bus_uevent_store);
7767ac1cf4aSKay Sievers 
7771da177e4SLinus Torvalds /**
778be871b7eSMichal Hocko  * bus_register - register a driver-core subsystem
77978d79559SRandy Dunlap  * @bus: bus to register
7801da177e4SLinus Torvalds  *
78178d79559SRandy Dunlap  * Once we have that, we register the bus with the kobject
7821da177e4SLinus Torvalds  * infrastructure, then register the children subsystems it has:
783ca22e56dSKay Sievers  * the devices and drivers that belong to the subsystem.
7841da177e4SLinus Torvalds  */
785be871b7eSMichal Hocko int bus_register(struct bus_type *bus)
7861da177e4SLinus Torvalds {
7871da177e4SLinus Torvalds 	int retval;
7886b6e39a6SKay Sievers 	struct subsys_private *priv;
789be871b7eSMichal Hocko 	struct lock_class_key *key = &bus->lock_key;
7901da177e4SLinus Torvalds 
7916b6e39a6SKay Sievers 	priv = kzalloc(sizeof(struct subsys_private), GFP_KERNEL);
792c6f7e72aSGreg Kroah-Hartman 	if (!priv)
793c6f7e72aSGreg Kroah-Hartman 		return -ENOMEM;
794116af378SBenjamin Herrenschmidt 
795c6f7e72aSGreg Kroah-Hartman 	priv->bus = bus;
796c6f7e72aSGreg Kroah-Hartman 	bus->p = priv;
797c6f7e72aSGreg Kroah-Hartman 
798c6f7e72aSGreg Kroah-Hartman 	BLOCKING_INIT_NOTIFIER_HEAD(&priv->bus_notifier);
799c6f7e72aSGreg Kroah-Hartman 
800c6f7e72aSGreg Kroah-Hartman 	retval = kobject_set_name(&priv->subsys.kobj, "%s", bus->name);
8011da177e4SLinus Torvalds 	if (retval)
8021da177e4SLinus Torvalds 		goto out;
8031da177e4SLinus Torvalds 
804c6f7e72aSGreg Kroah-Hartman 	priv->subsys.kobj.kset = bus_kset;
805c6f7e72aSGreg Kroah-Hartman 	priv->subsys.kobj.ktype = &bus_ktype;
806c6f7e72aSGreg Kroah-Hartman 	priv->drivers_autoprobe = 1;
807d6b05b84SGreg Kroah-Hartman 
808c6f7e72aSGreg Kroah-Hartman 	retval = kset_register(&priv->subsys);
8091da177e4SLinus Torvalds 	if (retval)
8101da177e4SLinus Torvalds 		goto out;
8111da177e4SLinus Torvalds 
8127ac1cf4aSKay Sievers 	retval = bus_create_file(bus, &bus_attr_uevent);
8137ac1cf4aSKay Sievers 	if (retval)
8147ac1cf4aSKay Sievers 		goto bus_uevent_fail;
8157ac1cf4aSKay Sievers 
816c6f7e72aSGreg Kroah-Hartman 	priv->devices_kset = kset_create_and_add("devices", NULL,
817c6f7e72aSGreg Kroah-Hartman 						 &priv->subsys.kobj);
818c6f7e72aSGreg Kroah-Hartman 	if (!priv->devices_kset) {
8193d899596SGreg Kroah-Hartman 		retval = -ENOMEM;
8201da177e4SLinus Torvalds 		goto bus_devices_fail;
8213d899596SGreg Kroah-Hartman 	}
8221da177e4SLinus Torvalds 
823c6f7e72aSGreg Kroah-Hartman 	priv->drivers_kset = kset_create_and_add("drivers", NULL,
824c6f7e72aSGreg Kroah-Hartman 						 &priv->subsys.kobj);
825c6f7e72aSGreg Kroah-Hartman 	if (!priv->drivers_kset) {
8266dcec251SGreg Kroah-Hartman 		retval = -ENOMEM;
8271da177e4SLinus Torvalds 		goto bus_drivers_fail;
8286dcec251SGreg Kroah-Hartman 	}
829465c7a3aSmochel@digitalimplant.org 
830ca22e56dSKay Sievers 	INIT_LIST_HEAD(&priv->interfaces);
831ca22e56dSKay Sievers 	__mutex_init(&priv->mutex, "subsys mutex", key);
832c6f7e72aSGreg Kroah-Hartman 	klist_init(&priv->klist_devices, klist_devices_get, klist_devices_put);
833c6f7e72aSGreg Kroah-Hartman 	klist_init(&priv->klist_drivers, NULL, NULL);
834b8c5cec2SKay Sievers 
835b8c5cec2SKay Sievers 	retval = add_probe_files(bus);
836b8c5cec2SKay Sievers 	if (retval)
837b8c5cec2SKay Sievers 		goto bus_probe_files_fail;
838b8c5cec2SKay Sievers 
83912478ba0SGreg Kroah-Hartman 	retval = bus_add_groups(bus, bus->bus_groups);
84012478ba0SGreg Kroah-Hartman 	if (retval)
84112478ba0SGreg Kroah-Hartman 		goto bus_groups_fail;
8421da177e4SLinus Torvalds 
8437dc72b28SGreg Kroah-Hartman 	pr_debug("bus: '%s': registered\n", bus->name);
8441da177e4SLinus Torvalds 	return 0;
8451da177e4SLinus Torvalds 
84612478ba0SGreg Kroah-Hartman bus_groups_fail:
847b8c5cec2SKay Sievers 	remove_probe_files(bus);
848b8c5cec2SKay Sievers bus_probe_files_fail:
849c6f7e72aSGreg Kroah-Hartman 	kset_unregister(bus->p->drivers_kset);
8501da177e4SLinus Torvalds bus_drivers_fail:
851c6f7e72aSGreg Kroah-Hartman 	kset_unregister(bus->p->devices_kset);
8521da177e4SLinus Torvalds bus_devices_fail:
8537ac1cf4aSKay Sievers 	bus_remove_file(bus, &bus_attr_uevent);
8547ac1cf4aSKay Sievers bus_uevent_fail:
855c6f7e72aSGreg Kroah-Hartman 	kset_unregister(&bus->p->subsys);
8561da177e4SLinus Torvalds out:
857600c20f3SJike Song 	kfree(bus->p);
858f48f3febSDave Young 	bus->p = NULL;
8591da177e4SLinus Torvalds 	return retval;
8601da177e4SLinus Torvalds }
861be871b7eSMichal Hocko EXPORT_SYMBOL_GPL(bus_register);
8621da177e4SLinus Torvalds 
8631da177e4SLinus Torvalds /**
8641da177e4SLinus Torvalds  * bus_unregister - remove a bus from the system
8651da177e4SLinus Torvalds  * @bus: bus.
8661da177e4SLinus Torvalds  *
8671da177e4SLinus Torvalds  * Unregister the child subsystems and the bus itself.
868fc1ede58SGreg Kroah-Hartman  * Finally, we call bus_put() to release the refcount
8691da177e4SLinus Torvalds  */
8701da177e4SLinus Torvalds void bus_unregister(struct bus_type *bus)
8711da177e4SLinus Torvalds {
8727dc72b28SGreg Kroah-Hartman 	pr_debug("bus: '%s': unregistering\n", bus->name);
873ca22e56dSKay Sievers 	if (bus->dev_root)
874ca22e56dSKay Sievers 		device_unregister(bus->dev_root);
87512478ba0SGreg Kroah-Hartman 	bus_remove_groups(bus, bus->bus_groups);
876b8c5cec2SKay Sievers 	remove_probe_files(bus);
877c6f7e72aSGreg Kroah-Hartman 	kset_unregister(bus->p->drivers_kset);
878c6f7e72aSGreg Kroah-Hartman 	kset_unregister(bus->p->devices_kset);
8797ac1cf4aSKay Sievers 	bus_remove_file(bus, &bus_attr_uevent);
880c6f7e72aSGreg Kroah-Hartman 	kset_unregister(&bus->p->subsys);
8811da177e4SLinus Torvalds }
8824a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_unregister);
8831da177e4SLinus Torvalds 
884116af378SBenjamin Herrenschmidt int bus_register_notifier(struct bus_type *bus, struct notifier_block *nb)
885116af378SBenjamin Herrenschmidt {
886c6f7e72aSGreg Kroah-Hartman 	return blocking_notifier_chain_register(&bus->p->bus_notifier, nb);
887116af378SBenjamin Herrenschmidt }
888116af378SBenjamin Herrenschmidt EXPORT_SYMBOL_GPL(bus_register_notifier);
889116af378SBenjamin Herrenschmidt 
890116af378SBenjamin Herrenschmidt int bus_unregister_notifier(struct bus_type *bus, struct notifier_block *nb)
891116af378SBenjamin Herrenschmidt {
892c6f7e72aSGreg Kroah-Hartman 	return blocking_notifier_chain_unregister(&bus->p->bus_notifier, nb);
893116af378SBenjamin Herrenschmidt }
894116af378SBenjamin Herrenschmidt EXPORT_SYMBOL_GPL(bus_unregister_notifier);
895116af378SBenjamin Herrenschmidt 
8960fed80f7SGreg Kroah-Hartman struct kset *bus_get_kset(struct bus_type *bus)
8970fed80f7SGreg Kroah-Hartman {
898c6f7e72aSGreg Kroah-Hartman 	return &bus->p->subsys;
8990fed80f7SGreg Kroah-Hartman }
9000fed80f7SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_get_kset);
9010fed80f7SGreg Kroah-Hartman 
902b249072eSGreg Kroah-Hartman struct klist *bus_get_device_klist(struct bus_type *bus)
903b249072eSGreg Kroah-Hartman {
904c6f7e72aSGreg Kroah-Hartman 	return &bus->p->klist_devices;
905b249072eSGreg Kroah-Hartman }
906b249072eSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_get_device_klist);
907b249072eSGreg Kroah-Hartman 
90899178b03SGreg Kroah-Hartman /*
909dca25ebdSRobert P. J. Day  * Yes, this forcibly breaks the klist abstraction temporarily.  It
91099178b03SGreg Kroah-Hartman  * just wants to sort the klist, not change reference counts and
91199178b03SGreg Kroah-Hartman  * take/drop locks rapidly in the process.  It does all this while
91299178b03SGreg Kroah-Hartman  * holding the lock for the list, so objects can't otherwise be
91399178b03SGreg Kroah-Hartman  * added/removed while we're swizzling.
91499178b03SGreg Kroah-Hartman  */
91599178b03SGreg Kroah-Hartman static void device_insertion_sort_klist(struct device *a, struct list_head *list,
91699178b03SGreg Kroah-Hartman 					int (*compare)(const struct device *a,
91799178b03SGreg Kroah-Hartman 							const struct device *b))
91899178b03SGreg Kroah-Hartman {
91999178b03SGreg Kroah-Hartman 	struct klist_node *n;
920ae1b4171SGreg Kroah-Hartman 	struct device_private *dev_prv;
92199178b03SGreg Kroah-Hartman 	struct device *b;
92299178b03SGreg Kroah-Hartman 
9234c62785eSGeliang Tang 	list_for_each_entry(n, list, n_node) {
924ae1b4171SGreg Kroah-Hartman 		dev_prv = to_device_private_bus(n);
925ae1b4171SGreg Kroah-Hartman 		b = dev_prv->device;
92699178b03SGreg Kroah-Hartman 		if (compare(a, b) <= 0) {
927ae1b4171SGreg Kroah-Hartman 			list_move_tail(&a->p->knode_bus.n_node,
928ae1b4171SGreg Kroah-Hartman 				       &b->p->knode_bus.n_node);
92999178b03SGreg Kroah-Hartman 			return;
93099178b03SGreg Kroah-Hartman 		}
93199178b03SGreg Kroah-Hartman 	}
932ae1b4171SGreg Kroah-Hartman 	list_move_tail(&a->p->knode_bus.n_node, list);
93399178b03SGreg Kroah-Hartman }
93499178b03SGreg Kroah-Hartman 
93599178b03SGreg Kroah-Hartman void bus_sort_breadthfirst(struct bus_type *bus,
93699178b03SGreg Kroah-Hartman 			   int (*compare)(const struct device *a,
93799178b03SGreg Kroah-Hartman 					  const struct device *b))
93899178b03SGreg Kroah-Hartman {
93999178b03SGreg Kroah-Hartman 	LIST_HEAD(sorted_devices);
9404c62785eSGeliang Tang 	struct klist_node *n, *tmp;
941ae1b4171SGreg Kroah-Hartman 	struct device_private *dev_prv;
94299178b03SGreg Kroah-Hartman 	struct device *dev;
94399178b03SGreg Kroah-Hartman 	struct klist *device_klist;
94499178b03SGreg Kroah-Hartman 
94599178b03SGreg Kroah-Hartman 	device_klist = bus_get_device_klist(bus);
94699178b03SGreg Kroah-Hartman 
94799178b03SGreg Kroah-Hartman 	spin_lock(&device_klist->k_lock);
9484c62785eSGeliang Tang 	list_for_each_entry_safe(n, tmp, &device_klist->k_list, n_node) {
949ae1b4171SGreg Kroah-Hartman 		dev_prv = to_device_private_bus(n);
950ae1b4171SGreg Kroah-Hartman 		dev = dev_prv->device;
95199178b03SGreg Kroah-Hartman 		device_insertion_sort_klist(dev, &sorted_devices, compare);
95299178b03SGreg Kroah-Hartman 	}
95399178b03SGreg Kroah-Hartman 	list_splice(&sorted_devices, &device_klist->k_list);
95499178b03SGreg Kroah-Hartman 	spin_unlock(&device_klist->k_lock);
95599178b03SGreg Kroah-Hartman }
95699178b03SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_sort_breadthfirst);
95799178b03SGreg Kroah-Hartman 
958ca22e56dSKay Sievers /**
959ca22e56dSKay Sievers  * subsys_dev_iter_init - initialize subsys device iterator
960ca22e56dSKay Sievers  * @iter: subsys iterator to initialize
961ca22e56dSKay Sievers  * @subsys: the subsys we wanna iterate over
962ca22e56dSKay Sievers  * @start: the device to start iterating from, if any
963ca22e56dSKay Sievers  * @type: device_type of the devices to iterate over, NULL for all
964ca22e56dSKay Sievers  *
965ca22e56dSKay Sievers  * Initialize subsys iterator @iter such that it iterates over devices
966ca22e56dSKay Sievers  * of @subsys.  If @start is set, the list iteration will start there,
967ca22e56dSKay Sievers  * otherwise if it is NULL, the iteration starts at the beginning of
968ca22e56dSKay Sievers  * the list.
969ca22e56dSKay Sievers  */
9707cd9c9bbSGreg Kroah-Hartman void subsys_dev_iter_init(struct subsys_dev_iter *iter, struct bus_type *subsys,
971ca22e56dSKay Sievers 			  struct device *start, const struct device_type *type)
972ca22e56dSKay Sievers {
973ca22e56dSKay Sievers 	struct klist_node *start_knode = NULL;
974ca22e56dSKay Sievers 
975ca22e56dSKay Sievers 	if (start)
976ca22e56dSKay Sievers 		start_knode = &start->p->knode_bus;
9777cd9c9bbSGreg Kroah-Hartman 	klist_iter_init_node(&subsys->p->klist_devices, &iter->ki, start_knode);
978ca22e56dSKay Sievers 	iter->type = type;
979ca22e56dSKay Sievers }
980ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_dev_iter_init);
981ca22e56dSKay Sievers 
982ca22e56dSKay Sievers /**
983ca22e56dSKay Sievers  * subsys_dev_iter_next - iterate to the next device
984ca22e56dSKay Sievers  * @iter: subsys iterator to proceed
985ca22e56dSKay Sievers  *
986ca22e56dSKay Sievers  * Proceed @iter to the next device and return it.  Returns NULL if
987ca22e56dSKay Sievers  * iteration is complete.
988ca22e56dSKay Sievers  *
989ca22e56dSKay Sievers  * The returned device is referenced and won't be released till
990ca22e56dSKay Sievers  * iterator is proceed to the next device or exited.  The caller is
991ca22e56dSKay Sievers  * free to do whatever it wants to do with the device including
992ca22e56dSKay Sievers  * calling back into subsys code.
993ca22e56dSKay Sievers  */
994ca22e56dSKay Sievers struct device *subsys_dev_iter_next(struct subsys_dev_iter *iter)
995ca22e56dSKay Sievers {
996ca22e56dSKay Sievers 	struct klist_node *knode;
997ca22e56dSKay Sievers 	struct device *dev;
998ca22e56dSKay Sievers 
999ca22e56dSKay Sievers 	for (;;) {
1000ca22e56dSKay Sievers 		knode = klist_next(&iter->ki);
1001ca22e56dSKay Sievers 		if (!knode)
1002ca22e56dSKay Sievers 			return NULL;
1003371fd7a2SGeliang Tang 		dev = to_device_private_bus(knode)->device;
1004ca22e56dSKay Sievers 		if (!iter->type || iter->type == dev->type)
1005ca22e56dSKay Sievers 			return dev;
1006ca22e56dSKay Sievers 	}
1007ca22e56dSKay Sievers }
1008ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_dev_iter_next);
1009ca22e56dSKay Sievers 
1010ca22e56dSKay Sievers /**
1011ca22e56dSKay Sievers  * subsys_dev_iter_exit - finish iteration
1012ca22e56dSKay Sievers  * @iter: subsys iterator to finish
1013ca22e56dSKay Sievers  *
1014ca22e56dSKay Sievers  * Finish an iteration.  Always call this function after iteration is
1015ca22e56dSKay Sievers  * complete whether the iteration ran till the end or not.
1016ca22e56dSKay Sievers  */
1017ca22e56dSKay Sievers void subsys_dev_iter_exit(struct subsys_dev_iter *iter)
1018ca22e56dSKay Sievers {
1019ca22e56dSKay Sievers 	klist_iter_exit(&iter->ki);
1020ca22e56dSKay Sievers }
1021ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_dev_iter_exit);
1022ca22e56dSKay Sievers 
1023ca22e56dSKay Sievers int subsys_interface_register(struct subsys_interface *sif)
1024ca22e56dSKay Sievers {
1025ca22e56dSKay Sievers 	struct bus_type *subsys;
1026ca22e56dSKay Sievers 	struct subsys_dev_iter iter;
1027ca22e56dSKay Sievers 	struct device *dev;
1028ca22e56dSKay Sievers 
1029ca22e56dSKay Sievers 	if (!sif || !sif->subsys)
1030ca22e56dSKay Sievers 		return -ENODEV;
1031ca22e56dSKay Sievers 
1032ca22e56dSKay Sievers 	subsys = bus_get(sif->subsys);
1033ca22e56dSKay Sievers 	if (!subsys)
1034ca22e56dSKay Sievers 		return -EINVAL;
1035ca22e56dSKay Sievers 
1036ca22e56dSKay Sievers 	mutex_lock(&subsys->p->mutex);
1037ca22e56dSKay Sievers 	list_add_tail(&sif->node, &subsys->p->interfaces);
1038ca22e56dSKay Sievers 	if (sif->add_dev) {
1039ca22e56dSKay Sievers 		subsys_dev_iter_init(&iter, subsys, NULL, NULL);
1040ca22e56dSKay Sievers 		while ((dev = subsys_dev_iter_next(&iter)))
1041ca22e56dSKay Sievers 			sif->add_dev(dev, sif);
1042ca22e56dSKay Sievers 		subsys_dev_iter_exit(&iter);
1043ca22e56dSKay Sievers 	}
1044ca22e56dSKay Sievers 	mutex_unlock(&subsys->p->mutex);
1045ca22e56dSKay Sievers 
1046ca22e56dSKay Sievers 	return 0;
1047ca22e56dSKay Sievers }
1048ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_interface_register);
1049ca22e56dSKay Sievers 
1050ca22e56dSKay Sievers void subsys_interface_unregister(struct subsys_interface *sif)
1051ca22e56dSKay Sievers {
10522b31594aSJonghwan Choi 	struct bus_type *subsys;
1053ca22e56dSKay Sievers 	struct subsys_dev_iter iter;
1054ca22e56dSKay Sievers 	struct device *dev;
1055ca22e56dSKay Sievers 
10562b31594aSJonghwan Choi 	if (!sif || !sif->subsys)
1057ca22e56dSKay Sievers 		return;
1058ca22e56dSKay Sievers 
10592b31594aSJonghwan Choi 	subsys = sif->subsys;
10602b31594aSJonghwan Choi 
1061ca22e56dSKay Sievers 	mutex_lock(&subsys->p->mutex);
1062ca22e56dSKay Sievers 	list_del_init(&sif->node);
1063ca22e56dSKay Sievers 	if (sif->remove_dev) {
1064ca22e56dSKay Sievers 		subsys_dev_iter_init(&iter, subsys, NULL, NULL);
1065ca22e56dSKay Sievers 		while ((dev = subsys_dev_iter_next(&iter)))
1066ca22e56dSKay Sievers 			sif->remove_dev(dev, sif);
1067ca22e56dSKay Sievers 		subsys_dev_iter_exit(&iter);
1068ca22e56dSKay Sievers 	}
1069ca22e56dSKay Sievers 	mutex_unlock(&subsys->p->mutex);
1070ca22e56dSKay Sievers 
1071ca22e56dSKay Sievers 	bus_put(subsys);
1072ca22e56dSKay Sievers }
1073ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_interface_unregister);
1074ca22e56dSKay Sievers 
1075ca22e56dSKay Sievers static void system_root_device_release(struct device *dev)
1076ca22e56dSKay Sievers {
1077ca22e56dSKay Sievers 	kfree(dev);
1078ca22e56dSKay Sievers }
1079d73ce004STejun Heo 
1080d73ce004STejun Heo static int subsys_register(struct bus_type *subsys,
1081d73ce004STejun Heo 			   const struct attribute_group **groups,
1082d73ce004STejun Heo 			   struct kobject *parent_of_root)
1083d73ce004STejun Heo {
1084d73ce004STejun Heo 	struct device *dev;
1085d73ce004STejun Heo 	int err;
1086d73ce004STejun Heo 
1087d73ce004STejun Heo 	err = bus_register(subsys);
1088d73ce004STejun Heo 	if (err < 0)
1089d73ce004STejun Heo 		return err;
1090d73ce004STejun Heo 
1091d73ce004STejun Heo 	dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1092d73ce004STejun Heo 	if (!dev) {
1093d73ce004STejun Heo 		err = -ENOMEM;
1094d73ce004STejun Heo 		goto err_dev;
1095d73ce004STejun Heo 	}
1096d73ce004STejun Heo 
1097d73ce004STejun Heo 	err = dev_set_name(dev, "%s", subsys->name);
1098d73ce004STejun Heo 	if (err < 0)
1099d73ce004STejun Heo 		goto err_name;
1100d73ce004STejun Heo 
1101d73ce004STejun Heo 	dev->kobj.parent = parent_of_root;
1102d73ce004STejun Heo 	dev->groups = groups;
1103d73ce004STejun Heo 	dev->release = system_root_device_release;
1104d73ce004STejun Heo 
1105d73ce004STejun Heo 	err = device_register(dev);
1106d73ce004STejun Heo 	if (err < 0)
1107d73ce004STejun Heo 		goto err_dev_reg;
1108d73ce004STejun Heo 
1109d73ce004STejun Heo 	subsys->dev_root = dev;
1110d73ce004STejun Heo 	return 0;
1111d73ce004STejun Heo 
1112d73ce004STejun Heo err_dev_reg:
1113d73ce004STejun Heo 	put_device(dev);
1114d73ce004STejun Heo 	dev = NULL;
1115d73ce004STejun Heo err_name:
1116d73ce004STejun Heo 	kfree(dev);
1117d73ce004STejun Heo err_dev:
1118d73ce004STejun Heo 	bus_unregister(subsys);
1119d73ce004STejun Heo 	return err;
1120d73ce004STejun Heo }
1121d73ce004STejun Heo 
1122ca22e56dSKay Sievers /**
1123ca22e56dSKay Sievers  * subsys_system_register - register a subsystem at /sys/devices/system/
112478d79559SRandy Dunlap  * @subsys: system subsystem
112578d79559SRandy Dunlap  * @groups: default attributes for the root device
1126ca22e56dSKay Sievers  *
1127ca22e56dSKay Sievers  * All 'system' subsystems have a /sys/devices/system/<name> root device
1128ca22e56dSKay Sievers  * with the name of the subsystem. The root device can carry subsystem-
1129ca22e56dSKay Sievers  * wide attributes. All registered devices are below this single root
1130ca22e56dSKay Sievers  * device and are named after the subsystem with a simple enumeration
1131e227867fSMasanari Iida  * number appended. The registered devices are not explicitly named;
1132ca22e56dSKay Sievers  * only 'id' in the device needs to be set.
1133ca22e56dSKay Sievers  *
1134ca22e56dSKay Sievers  * Do not use this interface for anything new, it exists for compatibility
1135ca22e56dSKay Sievers  * with bad ideas only. New subsystems should use plain subsystems; and
1136ca22e56dSKay Sievers  * add the subsystem-wide attributes should be added to the subsystem
1137ca22e56dSKay Sievers  * directory itself and not some create fake root-device placed in
1138ca22e56dSKay Sievers  * /sys/devices/system/<name>.
1139ca22e56dSKay Sievers  */
1140ca22e56dSKay Sievers int subsys_system_register(struct bus_type *subsys,
1141ca22e56dSKay Sievers 			   const struct attribute_group **groups)
1142ca22e56dSKay Sievers {
1143d73ce004STejun Heo 	return subsys_register(subsys, groups, &system_kset->kobj);
1144ca22e56dSKay Sievers }
1145ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_system_register);
1146ca22e56dSKay Sievers 
1147d73ce004STejun Heo /**
1148d73ce004STejun Heo  * subsys_virtual_register - register a subsystem at /sys/devices/virtual/
1149d73ce004STejun Heo  * @subsys: virtual subsystem
1150d73ce004STejun Heo  * @groups: default attributes for the root device
1151d73ce004STejun Heo  *
1152d73ce004STejun Heo  * All 'virtual' subsystems have a /sys/devices/system/<name> root device
1153d73ce004STejun Heo  * with the name of the subystem.  The root device can carry subsystem-wide
1154d73ce004STejun Heo  * attributes.  All registered devices are below this single root device.
1155d73ce004STejun Heo  * There's no restriction on device naming.  This is for kernel software
1156d73ce004STejun Heo  * constructs which need sysfs interface.
1157d73ce004STejun Heo  */
1158d73ce004STejun Heo int subsys_virtual_register(struct bus_type *subsys,
1159d73ce004STejun Heo 			    const struct attribute_group **groups)
1160d73ce004STejun Heo {
1161d73ce004STejun Heo 	struct kobject *virtual_dir;
1162d73ce004STejun Heo 
1163d73ce004STejun Heo 	virtual_dir = virtual_device_parent(NULL);
1164d73ce004STejun Heo 	if (!virtual_dir)
1165d73ce004STejun Heo 		return -ENOMEM;
1166d73ce004STejun Heo 
1167d73ce004STejun Heo 	return subsys_register(subsys, groups, virtual_dir);
1168d73ce004STejun Heo }
11691c04fc35SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(subsys_virtual_register);
1170d73ce004STejun Heo 
11711da177e4SLinus Torvalds int __init buses_init(void)
11721da177e4SLinus Torvalds {
117359a54833SGreg Kroah-Hartman 	bus_kset = kset_create_and_add("bus", &bus_uevent_ops, NULL);
117459a54833SGreg Kroah-Hartman 	if (!bus_kset)
117559a54833SGreg Kroah-Hartman 		return -ENOMEM;
1176ca22e56dSKay Sievers 
1177ca22e56dSKay Sievers 	system_kset = kset_create_and_add("system", NULL, &devices_kset->kobj);
1178ca22e56dSKay Sievers 	if (!system_kset)
1179ca22e56dSKay Sievers 		return -ENOMEM;
1180ca22e56dSKay Sievers 
118159a54833SGreg Kroah-Hartman 	return 0;
11821da177e4SLinus Torvalds }
1183