xref: /linux/drivers/base/bus.c (revision a4723041857eaa35f189d237da769c4c63235544)
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>
121da177e4SLinus Torvalds #include <linux/device.h>
131da177e4SLinus Torvalds #include <linux/module.h>
141da177e4SLinus Torvalds #include <linux/errno.h>
155a0e3ad6STejun Heo #include <linux/slab.h>
161da177e4SLinus Torvalds #include <linux/init.h>
171da177e4SLinus Torvalds #include <linux/string.h>
18ca22e56dSKay Sievers #include <linux/mutex.h>
1963967685SGreg Kroah-Hartman #include <linux/sysfs.h>
201da177e4SLinus Torvalds #include "base.h"
211da177e4SLinus Torvalds #include "power/power.h"
221da177e4SLinus Torvalds 
23ca22e56dSKay Sievers /* /sys/devices/system */
2497ec448aSH Hartley Sweeten static struct kset *system_kset;
25ca22e56dSKay Sievers 
261da177e4SLinus Torvalds #define to_bus_attr(_attr) container_of(_attr, struct bus_attribute, attr)
271da177e4SLinus Torvalds 
281da177e4SLinus Torvalds /*
291da177e4SLinus Torvalds  * sysfs bindings for drivers
301da177e4SLinus Torvalds  */
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds #define to_drv_attr(_attr) container_of(_attr, struct driver_attribute, attr)
331da177e4SLinus Torvalds 
344f4b3743SDaniel Vetter #define DRIVER_ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) \
354f4b3743SDaniel Vetter 	struct driver_attribute driver_attr_##_name =		\
364f4b3743SDaniel Vetter 		__ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store)
371da177e4SLinus Torvalds 
38b8c5cec2SKay Sievers static int __must_check bus_rescan_devices_helper(struct device *dev,
39b8c5cec2SKay Sievers 						void *data);
40b8c5cec2SKay Sievers 
415901d014SGreg Kroah-Hartman static struct bus_type *bus_get(struct bus_type *bus)
425901d014SGreg Kroah-Hartman {
43c6f7e72aSGreg Kroah-Hartman 	if (bus) {
44c6f7e72aSGreg Kroah-Hartman 		kset_get(&bus->p->subsys);
45c6f7e72aSGreg Kroah-Hartman 		return bus;
46c6f7e72aSGreg Kroah-Hartman 	}
47c6f7e72aSGreg Kroah-Hartman 	return NULL;
485901d014SGreg Kroah-Hartman }
495901d014SGreg Kroah-Hartman 
50fc1ede58SGreg Kroah-Hartman static void bus_put(struct bus_type *bus)
51fc1ede58SGreg Kroah-Hartman {
52c6f7e72aSGreg Kroah-Hartman 	if (bus)
53c6f7e72aSGreg Kroah-Hartman 		kset_put(&bus->p->subsys);
54fc1ede58SGreg Kroah-Hartman }
55fc1ede58SGreg Kroah-Hartman 
564a3ad20cSGreg Kroah-Hartman static ssize_t drv_attr_show(struct kobject *kobj, struct attribute *attr,
574a3ad20cSGreg Kroah-Hartman 			     char *buf)
581da177e4SLinus Torvalds {
591da177e4SLinus Torvalds 	struct driver_attribute *drv_attr = to_drv_attr(attr);
60e5dd1278SGreg Kroah-Hartman 	struct driver_private *drv_priv = to_driver(kobj);
614a0c20bfSDmitry Torokhov 	ssize_t ret = -EIO;
621da177e4SLinus Torvalds 
631da177e4SLinus Torvalds 	if (drv_attr->show)
64e5dd1278SGreg Kroah-Hartman 		ret = drv_attr->show(drv_priv->driver, buf);
651da177e4SLinus Torvalds 	return ret;
661da177e4SLinus Torvalds }
671da177e4SLinus Torvalds 
684a3ad20cSGreg Kroah-Hartman static ssize_t drv_attr_store(struct kobject *kobj, struct attribute *attr,
691da177e4SLinus Torvalds 			      const char *buf, size_t count)
701da177e4SLinus Torvalds {
711da177e4SLinus Torvalds 	struct driver_attribute *drv_attr = to_drv_attr(attr);
72e5dd1278SGreg Kroah-Hartman 	struct driver_private *drv_priv = to_driver(kobj);
734a0c20bfSDmitry Torokhov 	ssize_t ret = -EIO;
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds 	if (drv_attr->store)
76e5dd1278SGreg Kroah-Hartman 		ret = drv_attr->store(drv_priv->driver, buf, count);
771da177e4SLinus Torvalds 	return ret;
781da177e4SLinus Torvalds }
791da177e4SLinus Torvalds 
8052cf25d0SEmese Revfy static const struct sysfs_ops driver_sysfs_ops = {
811da177e4SLinus Torvalds 	.show	= drv_attr_show,
821da177e4SLinus Torvalds 	.store	= drv_attr_store,
831da177e4SLinus Torvalds };
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds static void driver_release(struct kobject *kobj)
861da177e4SLinus Torvalds {
87e5dd1278SGreg Kroah-Hartman 	struct driver_private *drv_priv = to_driver(kobj);
88e5dd1278SGreg Kroah-Hartman 
892b3a302aSHarvey Harrison 	pr_debug("driver: '%s': %s\n", kobject_name(kobj), __func__);
90e5dd1278SGreg Kroah-Hartman 	kfree(drv_priv);
911da177e4SLinus Torvalds }
921da177e4SLinus Torvalds 
93a1148fb0SGreg Kroah-Hartman static struct kobj_type driver_ktype = {
941da177e4SLinus Torvalds 	.sysfs_ops	= &driver_sysfs_ops,
951da177e4SLinus Torvalds 	.release	= driver_release,
961da177e4SLinus Torvalds };
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds /*
991da177e4SLinus Torvalds  * sysfs bindings for buses
1001da177e4SLinus Torvalds  */
1014a3ad20cSGreg Kroah-Hartman static ssize_t bus_attr_show(struct kobject *kobj, struct attribute *attr,
1024a3ad20cSGreg Kroah-Hartman 			     char *buf)
1031da177e4SLinus Torvalds {
1041da177e4SLinus Torvalds 	struct bus_attribute *bus_attr = to_bus_attr(attr);
1056b6e39a6SKay Sievers 	struct subsys_private *subsys_priv = to_subsys_private(kobj);
1061da177e4SLinus Torvalds 	ssize_t ret = 0;
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds 	if (bus_attr->show)
1096b6e39a6SKay Sievers 		ret = bus_attr->show(subsys_priv->bus, buf);
1101da177e4SLinus Torvalds 	return ret;
1111da177e4SLinus Torvalds }
1121da177e4SLinus Torvalds 
1134a3ad20cSGreg Kroah-Hartman static ssize_t bus_attr_store(struct kobject *kobj, struct attribute *attr,
1141da177e4SLinus Torvalds 			      const char *buf, size_t count)
1151da177e4SLinus Torvalds {
1161da177e4SLinus Torvalds 	struct bus_attribute *bus_attr = to_bus_attr(attr);
1176b6e39a6SKay Sievers 	struct subsys_private *subsys_priv = to_subsys_private(kobj);
1181da177e4SLinus Torvalds 	ssize_t ret = 0;
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds 	if (bus_attr->store)
1216b6e39a6SKay Sievers 		ret = bus_attr->store(subsys_priv->bus, buf, count);
1221da177e4SLinus Torvalds 	return ret;
1231da177e4SLinus Torvalds }
1241da177e4SLinus Torvalds 
12552cf25d0SEmese Revfy static const struct sysfs_ops bus_sysfs_ops = {
1261da177e4SLinus Torvalds 	.show	= bus_attr_show,
1271da177e4SLinus Torvalds 	.store	= bus_attr_store,
1281da177e4SLinus Torvalds };
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds int bus_create_file(struct bus_type *bus, struct bus_attribute *attr)
1311da177e4SLinus Torvalds {
1321da177e4SLinus Torvalds 	int error;
1335901d014SGreg Kroah-Hartman 	if (bus_get(bus)) {
134c6f7e72aSGreg Kroah-Hartman 		error = sysfs_create_file(&bus->p->subsys.kobj, &attr->attr);
135fc1ede58SGreg Kroah-Hartman 		bus_put(bus);
1361da177e4SLinus Torvalds 	} else
1371da177e4SLinus Torvalds 		error = -EINVAL;
1381da177e4SLinus Torvalds 	return error;
1391da177e4SLinus Torvalds }
1404a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_create_file);
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds void bus_remove_file(struct bus_type *bus, struct bus_attribute *attr)
1431da177e4SLinus Torvalds {
1445901d014SGreg Kroah-Hartman 	if (bus_get(bus)) {
145c6f7e72aSGreg Kroah-Hartman 		sysfs_remove_file(&bus->p->subsys.kobj, &attr->attr);
146fc1ede58SGreg Kroah-Hartman 		bus_put(bus);
1471da177e4SLinus Torvalds 	}
1481da177e4SLinus Torvalds }
1494a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_remove_file);
1501da177e4SLinus Torvalds 
151174be70bSBart Van Assche static void bus_release(struct kobject *kobj)
152174be70bSBart Van Assche {
153371fd7a2SGeliang Tang 	struct subsys_private *priv = to_subsys_private(kobj);
154174be70bSBart Van Assche 	struct bus_type *bus = priv->bus;
155174be70bSBart Van Assche 
156174be70bSBart Van Assche 	kfree(priv);
157174be70bSBart Van Assche 	bus->p = NULL;
158174be70bSBart Van Assche }
159174be70bSBart Van Assche 
16080f03e34SKay Sievers static struct kobj_type bus_ktype = {
1611da177e4SLinus Torvalds 	.sysfs_ops	= &bus_sysfs_ops,
162174be70bSBart Van Assche 	.release	= bus_release,
1631da177e4SLinus Torvalds };
1641da177e4SLinus Torvalds 
16580f03e34SKay Sievers static int bus_uevent_filter(struct kset *kset, struct kobject *kobj)
16680f03e34SKay Sievers {
16780f03e34SKay Sievers 	struct kobj_type *ktype = get_ktype(kobj);
16880f03e34SKay Sievers 
16980f03e34SKay Sievers 	if (ktype == &bus_ktype)
17080f03e34SKay Sievers 		return 1;
17180f03e34SKay Sievers 	return 0;
17280f03e34SKay Sievers }
17380f03e34SKay Sievers 
1749cd43611SEmese Revfy static const struct kset_uevent_ops bus_uevent_ops = {
17580f03e34SKay Sievers 	.filter = bus_uevent_filter,
17680f03e34SKay Sievers };
17780f03e34SKay Sievers 
17859a54833SGreg Kroah-Hartman static struct kset *bus_kset;
1791da177e4SLinus Torvalds 
1802b08c8d0SAlan Stern /* Manually detach a device from its associated driver. */
1812581c9ccSGreg Kroah-Hartman static ssize_t unbind_store(struct device_driver *drv, const char *buf,
1822581c9ccSGreg Kroah-Hartman 			    size_t count)
183151ef38fSGreg Kroah-Hartman {
1845901d014SGreg Kroah-Hartman 	struct bus_type *bus = bus_get(drv->bus);
185151ef38fSGreg Kroah-Hartman 	struct device *dev;
186151ef38fSGreg Kroah-Hartman 	int err = -ENODEV;
187151ef38fSGreg Kroah-Hartman 
1881f9ffc04SGreg Kroah-Hartman 	dev = bus_find_device_by_name(bus, NULL, buf);
1892b08c8d0SAlan Stern 	if (dev && dev->driver == drv) {
1908c97a46aSMartin Liu 		if (dev->parent && dev->bus->need_parent_lock)
1918e9394ceSGreg Kroah-Hartman 			device_lock(dev->parent);
192151ef38fSGreg Kroah-Hartman 		device_release_driver(dev);
1938c97a46aSMartin Liu 		if (dev->parent && dev->bus->need_parent_lock)
1948e9394ceSGreg Kroah-Hartman 			device_unlock(dev->parent);
195151ef38fSGreg Kroah-Hartman 		err = count;
196151ef38fSGreg Kroah-Hartman 	}
1972b08c8d0SAlan Stern 	put_device(dev);
198fc1ede58SGreg Kroah-Hartman 	bus_put(bus);
199151ef38fSGreg Kroah-Hartman 	return err;
200151ef38fSGreg Kroah-Hartman }
2014f4b3743SDaniel Vetter static DRIVER_ATTR_IGNORE_LOCKDEP(unbind, S_IWUSR, NULL, unbind_store);
202151ef38fSGreg Kroah-Hartman 
203afdce75fSGreg Kroah-Hartman /*
204afdce75fSGreg Kroah-Hartman  * Manually attach a device to a driver.
205afdce75fSGreg Kroah-Hartman  * Note: the driver must want to bind to the device,
206afdce75fSGreg Kroah-Hartman  * it is not possible to override the driver's id table.
207afdce75fSGreg Kroah-Hartman  */
2082581c9ccSGreg Kroah-Hartman static ssize_t bind_store(struct device_driver *drv, const char *buf,
2092581c9ccSGreg Kroah-Hartman 			  size_t count)
210afdce75fSGreg Kroah-Hartman {
2115901d014SGreg Kroah-Hartman 	struct bus_type *bus = bus_get(drv->bus);
212afdce75fSGreg Kroah-Hartman 	struct device *dev;
213afdce75fSGreg Kroah-Hartman 	int err = -ENODEV;
214afdce75fSGreg Kroah-Hartman 
2151f9ffc04SGreg Kroah-Hartman 	dev = bus_find_device_by_name(bus, NULL, buf);
21649b420a1SMing Lei 	if (dev && dev->driver == NULL && driver_match_device(drv, dev)) {
2178c97a46aSMartin Liu 		if (dev->parent && bus->need_parent_lock)
2188e9394ceSGreg Kroah-Hartman 			device_lock(dev->parent);
2198e9394ceSGreg Kroah-Hartman 		device_lock(dev);
220afdce75fSGreg Kroah-Hartman 		err = driver_probe_device(drv, dev);
2218e9394ceSGreg Kroah-Hartman 		device_unlock(dev);
2228c97a46aSMartin Liu 		if (dev->parent && bus->need_parent_lock)
2238e9394ceSGreg Kroah-Hartman 			device_unlock(dev->parent);
22437225401SRyan Wilson 
2254a3ad20cSGreg Kroah-Hartman 		if (err > 0) {
2264a3ad20cSGreg Kroah-Hartman 			/* success */
22737225401SRyan Wilson 			err = count;
2284a3ad20cSGreg Kroah-Hartman 		} else if (err == 0) {
2294a3ad20cSGreg Kroah-Hartman 			/* driver didn't accept device */
23037225401SRyan Wilson 			err = -ENODEV;
231afdce75fSGreg Kroah-Hartman 		}
2324a3ad20cSGreg Kroah-Hartman 	}
2332b08c8d0SAlan Stern 	put_device(dev);
234fc1ede58SGreg Kroah-Hartman 	bus_put(bus);
235afdce75fSGreg Kroah-Hartman 	return err;
236afdce75fSGreg Kroah-Hartman }
2374f4b3743SDaniel Vetter static DRIVER_ATTR_IGNORE_LOCKDEP(bind, S_IWUSR, NULL, bind_store);
238afdce75fSGreg Kroah-Hartman 
2392e7189b6SGreg Kroah-Hartman static ssize_t drivers_autoprobe_show(struct bus_type *bus, char *buf)
240b8c5cec2SKay Sievers {
241c6f7e72aSGreg Kroah-Hartman 	return sprintf(buf, "%d\n", bus->p->drivers_autoprobe);
242b8c5cec2SKay Sievers }
243b8c5cec2SKay Sievers 
2442e7189b6SGreg Kroah-Hartman static ssize_t drivers_autoprobe_store(struct bus_type *bus,
245b8c5cec2SKay Sievers 				       const char *buf, size_t count)
246b8c5cec2SKay Sievers {
247b8c5cec2SKay Sievers 	if (buf[0] == '0')
248c6f7e72aSGreg Kroah-Hartman 		bus->p->drivers_autoprobe = 0;
249b8c5cec2SKay Sievers 	else
250c6f7e72aSGreg Kroah-Hartman 		bus->p->drivers_autoprobe = 1;
251b8c5cec2SKay Sievers 	return count;
252b8c5cec2SKay Sievers }
253b8c5cec2SKay Sievers 
2542e7189b6SGreg Kroah-Hartman static ssize_t drivers_probe_store(struct bus_type *bus,
255b8c5cec2SKay Sievers 				   const char *buf, size_t count)
256b8c5cec2SKay Sievers {
257b8c5cec2SKay Sievers 	struct device *dev;
2580372ffb3SAlex Williamson 	int err = -EINVAL;
259b8c5cec2SKay Sievers 
2601f9ffc04SGreg Kroah-Hartman 	dev = bus_find_device_by_name(bus, NULL, buf);
261b8c5cec2SKay Sievers 	if (!dev)
262b8c5cec2SKay Sievers 		return -ENODEV;
2630372ffb3SAlex Williamson 	if (bus_rescan_devices_helper(dev, NULL) == 0)
2640372ffb3SAlex Williamson 		err = count;
2650372ffb3SAlex Williamson 	put_device(dev);
2660372ffb3SAlex Williamson 	return err;
267b8c5cec2SKay Sievers }
268151ef38fSGreg Kroah-Hartman 
269465c7a3aSmochel@digitalimplant.org static struct device *next_device(struct klist_iter *i)
270465c7a3aSmochel@digitalimplant.org {
271465c7a3aSmochel@digitalimplant.org 	struct klist_node *n = klist_next(i);
272ae1b4171SGreg Kroah-Hartman 	struct device *dev = NULL;
273ae1b4171SGreg Kroah-Hartman 	struct device_private *dev_prv;
274ae1b4171SGreg Kroah-Hartman 
275ae1b4171SGreg Kroah-Hartman 	if (n) {
276ae1b4171SGreg Kroah-Hartman 		dev_prv = to_device_private_bus(n);
277ae1b4171SGreg Kroah-Hartman 		dev = dev_prv->device;
278ae1b4171SGreg Kroah-Hartman 	}
279ae1b4171SGreg Kroah-Hartman 	return dev;
280465c7a3aSmochel@digitalimplant.org }
281465c7a3aSmochel@digitalimplant.org 
2821da177e4SLinus Torvalds /**
2831da177e4SLinus Torvalds  * bus_for_each_dev - device iterator.
2841da177e4SLinus Torvalds  * @bus: bus type.
2851da177e4SLinus Torvalds  * @start: device to start iterating from.
2861da177e4SLinus Torvalds  * @data: data for the callback.
2871da177e4SLinus Torvalds  * @fn: function to be called for each device.
2881da177e4SLinus Torvalds  *
2891da177e4SLinus Torvalds  * Iterate over @bus's list of devices, and call @fn for each,
2901da177e4SLinus Torvalds  * passing it @data. If @start is not NULL, we use that device to
2911da177e4SLinus Torvalds  * begin iterating from.
2921da177e4SLinus Torvalds  *
2931da177e4SLinus Torvalds  * We check the return of @fn each time. If it returns anything
2941da177e4SLinus Torvalds  * other than 0, we break out and return that value.
2951da177e4SLinus Torvalds  *
2961da177e4SLinus Torvalds  * NOTE: The device that returns a non-zero value is not retained
2971da177e4SLinus Torvalds  * in any way, nor is its refcount incremented. If the caller needs
2980fa1b0a1SAlex Chiang  * to retain this data, it should do so, and increment the reference
2991da177e4SLinus Torvalds  * count in the supplied callback.
3001da177e4SLinus Torvalds  */
3011da177e4SLinus Torvalds int bus_for_each_dev(struct bus_type *bus, struct device *start,
3021da177e4SLinus Torvalds 		     void *data, int (*fn)(struct device *, void *))
3031da177e4SLinus Torvalds {
304465c7a3aSmochel@digitalimplant.org 	struct klist_iter i;
305465c7a3aSmochel@digitalimplant.org 	struct device *dev;
306465c7a3aSmochel@digitalimplant.org 	int error = 0;
3071da177e4SLinus Torvalds 
3084fa3e78bSBjorn Helgaas 	if (!bus || !bus->p)
309465c7a3aSmochel@digitalimplant.org 		return -EINVAL;
310465c7a3aSmochel@digitalimplant.org 
3117cd9c9bbSGreg Kroah-Hartman 	klist_iter_init_node(&bus->p->klist_devices, &i,
312ae1b4171SGreg Kroah-Hartman 			     (start ? &start->p->knode_bus : NULL));
31393ead7c9SGimcuan Hui 	while (!error && (dev = next_device(&i)))
314465c7a3aSmochel@digitalimplant.org 		error = fn(dev, data);
315465c7a3aSmochel@digitalimplant.org 	klist_iter_exit(&i);
316465c7a3aSmochel@digitalimplant.org 	return error;
3171da177e4SLinus Torvalds }
3184a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_for_each_dev);
3191da177e4SLinus Torvalds 
3200edb5860SCornelia Huck /**
3210edb5860SCornelia Huck  * bus_find_device - device iterator for locating a particular device.
3220edb5860SCornelia Huck  * @bus: bus type
3230edb5860SCornelia Huck  * @start: Device to begin with
3240edb5860SCornelia Huck  * @data: Data to pass to match function
3250edb5860SCornelia Huck  * @match: Callback function to check device
3260edb5860SCornelia Huck  *
3270edb5860SCornelia Huck  * This is similar to the bus_for_each_dev() function above, but it
3280edb5860SCornelia Huck  * returns a reference to a device that is 'found' for later use, as
3290edb5860SCornelia Huck  * determined by the @match callback.
3300edb5860SCornelia Huck  *
3310edb5860SCornelia Huck  * The callback should return 0 if the device doesn't match and non-zero
3320edb5860SCornelia Huck  * if it does.  If the callback returns non-zero, this function will
3330edb5860SCornelia Huck  * return to the caller and not iterate over any more devices.
3340edb5860SCornelia Huck  */
3350edb5860SCornelia Huck struct device *bus_find_device(struct bus_type *bus,
3360edb5860SCornelia Huck 			       struct device *start, void *data,
3374a3ad20cSGreg Kroah-Hartman 			       int (*match)(struct device *dev, void *data))
3380edb5860SCornelia Huck {
3390edb5860SCornelia Huck 	struct klist_iter i;
3400edb5860SCornelia Huck 	struct device *dev;
3410edb5860SCornelia Huck 
3424fa3e78bSBjorn Helgaas 	if (!bus || !bus->p)
3430edb5860SCornelia Huck 		return NULL;
3440edb5860SCornelia Huck 
3457cd9c9bbSGreg Kroah-Hartman 	klist_iter_init_node(&bus->p->klist_devices, &i,
3467cd9c9bbSGreg Kroah-Hartman 			     (start ? &start->p->knode_bus : NULL));
3470edb5860SCornelia Huck 	while ((dev = next_device(&i)))
3480edb5860SCornelia Huck 		if (match(dev, data) && get_device(dev))
3490edb5860SCornelia Huck 			break;
3500edb5860SCornelia Huck 	klist_iter_exit(&i);
3510edb5860SCornelia Huck 	return dev;
3520edb5860SCornelia Huck }
3534a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_find_device);
35438fdac3cSmochel@digitalimplant.org 
3551f9ffc04SGreg Kroah-Hartman static int match_name(struct device *dev, void *data)
3561f9ffc04SGreg Kroah-Hartman {
3571f9ffc04SGreg Kroah-Hartman 	const char *name = data;
3581f9ffc04SGreg Kroah-Hartman 
3591e0b2cf9SKay Sievers 	return sysfs_streq(name, dev_name(dev));
3601f9ffc04SGreg Kroah-Hartman }
3611f9ffc04SGreg Kroah-Hartman 
3621f9ffc04SGreg Kroah-Hartman /**
3631f9ffc04SGreg Kroah-Hartman  * bus_find_device_by_name - device iterator for locating a particular device of a specific name
3641f9ffc04SGreg Kroah-Hartman  * @bus: bus type
3651f9ffc04SGreg Kroah-Hartman  * @start: Device to begin with
3661f9ffc04SGreg Kroah-Hartman  * @name: name of the device to match
3671f9ffc04SGreg Kroah-Hartman  *
3681f9ffc04SGreg Kroah-Hartman  * This is similar to the bus_find_device() function above, but it handles
3691f9ffc04SGreg Kroah-Hartman  * searching by a name automatically, no need to write another strcmp matching
3701f9ffc04SGreg Kroah-Hartman  * function.
3711f9ffc04SGreg Kroah-Hartman  */
3721f9ffc04SGreg Kroah-Hartman struct device *bus_find_device_by_name(struct bus_type *bus,
3731f9ffc04SGreg Kroah-Hartman 				       struct device *start, const char *name)
3741f9ffc04SGreg Kroah-Hartman {
3751f9ffc04SGreg Kroah-Hartman 	return bus_find_device(bus, start, (void *)name, match_name);
3761f9ffc04SGreg Kroah-Hartman }
3771f9ffc04SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_find_device_by_name);
3781f9ffc04SGreg Kroah-Hartman 
379ca22e56dSKay Sievers /**
380ca22e56dSKay Sievers  * subsys_find_device_by_id - find a device with a specific enumeration number
381ca22e56dSKay Sievers  * @subsys: subsystem
382ca22e56dSKay Sievers  * @id: index 'id' in struct device
383ca22e56dSKay Sievers  * @hint: device to check first
384ca22e56dSKay Sievers  *
385ca22e56dSKay Sievers  * Check the hint's next object and if it is a match return it directly,
386ca22e56dSKay Sievers  * otherwise, fall back to a full list search. Either way a reference for
387ca22e56dSKay Sievers  * the returned object is taken.
388ca22e56dSKay Sievers  */
389ca22e56dSKay Sievers struct device *subsys_find_device_by_id(struct bus_type *subsys, unsigned int id,
390ca22e56dSKay Sievers 					struct device *hint)
391ca22e56dSKay Sievers {
392ca22e56dSKay Sievers 	struct klist_iter i;
393ca22e56dSKay Sievers 	struct device *dev;
394ca22e56dSKay Sievers 
395ca22e56dSKay Sievers 	if (!subsys)
396ca22e56dSKay Sievers 		return NULL;
397ca22e56dSKay Sievers 
398ca22e56dSKay Sievers 	if (hint) {
3997cd9c9bbSGreg Kroah-Hartman 		klist_iter_init_node(&subsys->p->klist_devices, &i, &hint->p->knode_bus);
400ca22e56dSKay Sievers 		dev = next_device(&i);
401ca22e56dSKay Sievers 		if (dev && dev->id == id && get_device(dev)) {
402ca22e56dSKay Sievers 			klist_iter_exit(&i);
403ca22e56dSKay Sievers 			return dev;
404ca22e56dSKay Sievers 		}
405ca22e56dSKay Sievers 		klist_iter_exit(&i);
406ca22e56dSKay Sievers 	}
407ca22e56dSKay Sievers 
408ca22e56dSKay Sievers 	klist_iter_init_node(&subsys->p->klist_devices, &i, NULL);
409ca22e56dSKay Sievers 	while ((dev = next_device(&i))) {
410ca22e56dSKay Sievers 		if (dev->id == id && get_device(dev)) {
411ca22e56dSKay Sievers 			klist_iter_exit(&i);
412ca22e56dSKay Sievers 			return dev;
413ca22e56dSKay Sievers 		}
414ca22e56dSKay Sievers 	}
415ca22e56dSKay Sievers 	klist_iter_exit(&i);
416ca22e56dSKay Sievers 	return NULL;
417ca22e56dSKay Sievers }
418ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_find_device_by_id);
419ca22e56dSKay Sievers 
42038fdac3cSmochel@digitalimplant.org static struct device_driver *next_driver(struct klist_iter *i)
42138fdac3cSmochel@digitalimplant.org {
42238fdac3cSmochel@digitalimplant.org 	struct klist_node *n = klist_next(i);
423e5dd1278SGreg Kroah-Hartman 	struct driver_private *drv_priv;
424e5dd1278SGreg Kroah-Hartman 
425e5dd1278SGreg Kroah-Hartman 	if (n) {
426e5dd1278SGreg Kroah-Hartman 		drv_priv = container_of(n, struct driver_private, knode_bus);
427e5dd1278SGreg Kroah-Hartman 		return drv_priv->driver;
428e5dd1278SGreg Kroah-Hartman 	}
429e5dd1278SGreg Kroah-Hartman 	return NULL;
43038fdac3cSmochel@digitalimplant.org }
43138fdac3cSmochel@digitalimplant.org 
4321da177e4SLinus Torvalds /**
4331da177e4SLinus Torvalds  * bus_for_each_drv - driver iterator
4341da177e4SLinus Torvalds  * @bus: bus we're dealing with.
4351da177e4SLinus Torvalds  * @start: driver to start iterating on.
4361da177e4SLinus Torvalds  * @data: data to pass to the callback.
4371da177e4SLinus Torvalds  * @fn: function to call for each driver.
4381da177e4SLinus Torvalds  *
4391da177e4SLinus Torvalds  * This is nearly identical to the device iterator above.
4401da177e4SLinus Torvalds  * We iterate over each driver that belongs to @bus, and call
4411da177e4SLinus Torvalds  * @fn for each. If @fn returns anything but 0, we break out
4421da177e4SLinus Torvalds  * and return it. If @start is not NULL, we use it as the head
4431da177e4SLinus Torvalds  * of the list.
4441da177e4SLinus Torvalds  *
4451da177e4SLinus Torvalds  * NOTE: we don't return the driver that returns a non-zero
4461da177e4SLinus Torvalds  * value, nor do we leave the reference count incremented for that
4471da177e4SLinus Torvalds  * driver. If the caller needs to know that info, it must set it
4481da177e4SLinus Torvalds  * in the callback. It must also be sure to increment the refcount
4491da177e4SLinus Torvalds  * so it doesn't disappear before returning to the caller.
4501da177e4SLinus Torvalds  */
4511da177e4SLinus Torvalds int bus_for_each_drv(struct bus_type *bus, struct device_driver *start,
4521da177e4SLinus Torvalds 		     void *data, int (*fn)(struct device_driver *, void *))
4531da177e4SLinus Torvalds {
45438fdac3cSmochel@digitalimplant.org 	struct klist_iter i;
45538fdac3cSmochel@digitalimplant.org 	struct device_driver *drv;
45638fdac3cSmochel@digitalimplant.org 	int error = 0;
4571da177e4SLinus Torvalds 
45838fdac3cSmochel@digitalimplant.org 	if (!bus)
45938fdac3cSmochel@digitalimplant.org 		return -EINVAL;
46038fdac3cSmochel@digitalimplant.org 
4617cd9c9bbSGreg Kroah-Hartman 	klist_iter_init_node(&bus->p->klist_drivers, &i,
462e5dd1278SGreg Kroah-Hartman 			     start ? &start->p->knode_bus : NULL);
46338fdac3cSmochel@digitalimplant.org 	while ((drv = next_driver(&i)) && !error)
46438fdac3cSmochel@digitalimplant.org 		error = fn(drv, data);
46538fdac3cSmochel@digitalimplant.org 	klist_iter_exit(&i);
46638fdac3cSmochel@digitalimplant.org 	return error;
4671da177e4SLinus Torvalds }
4684a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_for_each_drv);
4691da177e4SLinus Torvalds 
4701da177e4SLinus Torvalds /**
4711da177e4SLinus Torvalds  * bus_add_device - add device to bus
4721da177e4SLinus Torvalds  * @dev: device being added
4731da177e4SLinus Torvalds  *
4742023c610SAlan Stern  * - Add device's bus attributes.
4752023c610SAlan Stern  * - Create links to device's bus.
4761da177e4SLinus Torvalds  * - Add the device to its bus's list of devices.
4771da177e4SLinus Torvalds  */
4781da177e4SLinus Torvalds int bus_add_device(struct device *dev)
4791da177e4SLinus Torvalds {
4805901d014SGreg Kroah-Hartman 	struct bus_type *bus = bus_get(dev->bus);
4811da177e4SLinus Torvalds 	int error = 0;
4821da177e4SLinus Torvalds 
4831da177e4SLinus Torvalds 	if (bus) {
4841e0b2cf9SKay Sievers 		pr_debug("bus: '%s': add device %s\n", bus->name, dev_name(dev));
485fa6fdb33SGreg Kroah-Hartman 		error = device_add_groups(dev, bus->dev_groups);
486fa6fdb33SGreg Kroah-Hartman 		if (error)
487b46c7337SGreg Kroah-Hartman 			goto out_put;
488c6f7e72aSGreg Kroah-Hartman 		error = sysfs_create_link(&bus->p->devices_kset->kobj,
4891e0b2cf9SKay Sievers 						&dev->kobj, dev_name(dev));
490f86db396SAndrew Morton 		if (error)
4911c34203aSJunjie Mao 			goto out_groups;
492f86db396SAndrew Morton 		error = sysfs_create_link(&dev->kobj,
493c6f7e72aSGreg Kroah-Hartman 				&dev->bus->p->subsys.kobj, "subsystem");
494f86db396SAndrew Morton 		if (error)
495513e7337SCornelia Huck 			goto out_subsys;
4962023c610SAlan Stern 		klist_add_tail(&dev->p->knode_bus, &bus->p->klist_devices);
4971da177e4SLinus Torvalds 	}
498513e7337SCornelia Huck 	return 0;
499513e7337SCornelia Huck 
500513e7337SCornelia Huck out_subsys:
5011e0b2cf9SKay Sievers 	sysfs_remove_link(&bus->p->devices_kset->kobj, dev_name(dev));
502fa6fdb33SGreg Kroah-Hartman out_groups:
503fa6fdb33SGreg Kroah-Hartman 	device_remove_groups(dev, bus->dev_groups);
504513e7337SCornelia Huck out_put:
505fc1ede58SGreg Kroah-Hartman 	bus_put(dev->bus);
5061da177e4SLinus Torvalds 	return error;
5071da177e4SLinus Torvalds }
5081da177e4SLinus Torvalds 
5091da177e4SLinus Torvalds /**
5102023c610SAlan Stern  * bus_probe_device - probe drivers for a new device
5112023c610SAlan Stern  * @dev: device to probe
51253877d06SKay Sievers  *
5132023c610SAlan Stern  * - Automatically probe for a driver if the bus allows it.
51453877d06SKay Sievers  */
5152023c610SAlan Stern void bus_probe_device(struct device *dev)
51653877d06SKay Sievers {
51753877d06SKay Sievers 	struct bus_type *bus = dev->bus;
518ca22e56dSKay Sievers 	struct subsys_interface *sif;
51953877d06SKay Sievers 
520ca22e56dSKay Sievers 	if (!bus)
521ca22e56dSKay Sievers 		return;
522ca22e56dSKay Sievers 
523765230b5SDmitry Torokhov 	if (bus->p->drivers_autoprobe)
524765230b5SDmitry Torokhov 		device_initial_probe(dev);
525ca22e56dSKay Sievers 
526ca22e56dSKay Sievers 	mutex_lock(&bus->p->mutex);
527ca22e56dSKay Sievers 	list_for_each_entry(sif, &bus->p->interfaces, node)
528ca22e56dSKay Sievers 		if (sif->add_dev)
529ca22e56dSKay Sievers 			sif->add_dev(dev, sif);
530ca22e56dSKay Sievers 	mutex_unlock(&bus->p->mutex);
531f86db396SAndrew Morton }
53253877d06SKay Sievers 
53353877d06SKay Sievers /**
5341da177e4SLinus Torvalds  * bus_remove_device - remove device from bus
5351da177e4SLinus Torvalds  * @dev: device to be removed
5361da177e4SLinus Torvalds  *
537ca22e56dSKay Sievers  * - Remove device from all interfaces.
538ca22e56dSKay Sievers  * - Remove symlink from bus' directory.
5391da177e4SLinus Torvalds  * - Delete device from bus's list.
5401da177e4SLinus Torvalds  * - Detach from its driver.
5411da177e4SLinus Torvalds  * - Drop reference taken in bus_add_device().
5421da177e4SLinus Torvalds  */
5431da177e4SLinus Torvalds void bus_remove_device(struct device *dev)
5441da177e4SLinus Torvalds {
545ca22e56dSKay Sievers 	struct bus_type *bus = dev->bus;
546ca22e56dSKay Sievers 	struct subsys_interface *sif;
547ca22e56dSKay Sievers 
548ca22e56dSKay Sievers 	if (!bus)
549ca22e56dSKay Sievers 		return;
550ca22e56dSKay Sievers 
551ca22e56dSKay Sievers 	mutex_lock(&bus->p->mutex);
552ca22e56dSKay Sievers 	list_for_each_entry(sif, &bus->p->interfaces, node)
553ca22e56dSKay Sievers 		if (sif->remove_dev)
554ca22e56dSKay Sievers 			sif->remove_dev(dev, sif);
555ca22e56dSKay Sievers 	mutex_unlock(&bus->p->mutex);
556ca22e56dSKay Sievers 
557b9d9c82bSKay Sievers 	sysfs_remove_link(&dev->kobj, "subsystem");
5584a3ad20cSGreg Kroah-Hartman 	sysfs_remove_link(&dev->bus->p->devices_kset->kobj,
5591e0b2cf9SKay Sievers 			  dev_name(dev));
560fa6fdb33SGreg Kroah-Hartman 	device_remove_groups(dev, dev->bus->dev_groups);
561ae1b4171SGreg Kroah-Hartman 	if (klist_node_attached(&dev->p->knode_bus))
562ae1b4171SGreg Kroah-Hartman 		klist_del(&dev->p->knode_bus);
5633f62e570SGreg Kroah-Hartman 
5644a3ad20cSGreg Kroah-Hartman 	pr_debug("bus: '%s': remove device %s\n",
5651e0b2cf9SKay Sievers 		 dev->bus->name, dev_name(dev));
5661da177e4SLinus Torvalds 	device_release_driver(dev);
567fc1ede58SGreg Kroah-Hartman 	bus_put(dev->bus);
5681da177e4SLinus Torvalds }
5691da177e4SLinus Torvalds 
570f86db396SAndrew Morton static int __must_check add_bind_files(struct device_driver *drv)
571874c6241SGreg Kroah-Hartman {
572f86db396SAndrew Morton 	int ret;
573f86db396SAndrew Morton 
574f86db396SAndrew Morton 	ret = driver_create_file(drv, &driver_attr_unbind);
575f86db396SAndrew Morton 	if (ret == 0) {
576f86db396SAndrew Morton 		ret = driver_create_file(drv, &driver_attr_bind);
577f86db396SAndrew Morton 		if (ret)
578f86db396SAndrew Morton 			driver_remove_file(drv, &driver_attr_unbind);
579f86db396SAndrew Morton 	}
580f86db396SAndrew Morton 	return ret;
581874c6241SGreg Kroah-Hartman }
582874c6241SGreg Kroah-Hartman 
583874c6241SGreg Kroah-Hartman static void remove_bind_files(struct device_driver *drv)
584874c6241SGreg Kroah-Hartman {
585874c6241SGreg Kroah-Hartman 	driver_remove_file(drv, &driver_attr_bind);
586874c6241SGreg Kroah-Hartman 	driver_remove_file(drv, &driver_attr_unbind);
587874c6241SGreg Kroah-Hartman }
588b8c5cec2SKay Sievers 
5892e7189b6SGreg Kroah-Hartman static BUS_ATTR_WO(drivers_probe);
5902e7189b6SGreg Kroah-Hartman static BUS_ATTR_RW(drivers_autoprobe);
5918380770cSKay Sievers 
592b8c5cec2SKay Sievers static int add_probe_files(struct bus_type *bus)
593b8c5cec2SKay Sievers {
594b8c5cec2SKay Sievers 	int retval;
595b8c5cec2SKay Sievers 
5968380770cSKay Sievers 	retval = bus_create_file(bus, &bus_attr_drivers_probe);
597b8c5cec2SKay Sievers 	if (retval)
598b8c5cec2SKay Sievers 		goto out;
599b8c5cec2SKay Sievers 
6008380770cSKay Sievers 	retval = bus_create_file(bus, &bus_attr_drivers_autoprobe);
601b8c5cec2SKay Sievers 	if (retval)
6028380770cSKay Sievers 		bus_remove_file(bus, &bus_attr_drivers_probe);
603b8c5cec2SKay Sievers out:
604b8c5cec2SKay Sievers 	return retval;
605b8c5cec2SKay Sievers }
606b8c5cec2SKay Sievers 
607b8c5cec2SKay Sievers static void remove_probe_files(struct bus_type *bus)
608b8c5cec2SKay Sievers {
6098380770cSKay Sievers 	bus_remove_file(bus, &bus_attr_drivers_autoprobe);
6108380770cSKay Sievers 	bus_remove_file(bus, &bus_attr_drivers_probe);
611b8c5cec2SKay Sievers }
6121da177e4SLinus Torvalds 
6132581c9ccSGreg Kroah-Hartman static ssize_t uevent_store(struct device_driver *drv, const char *buf,
6142581c9ccSGreg Kroah-Hartman 			    size_t count)
6157ac1cf4aSKay Sievers {
616df44b479SPeter Rajnoha 	int rc;
617df44b479SPeter Rajnoha 
618df44b479SPeter Rajnoha 	rc = kobject_synth_uevent(&drv->p->kobj, buf, count);
619df44b479SPeter Rajnoha 	return rc ? rc : count;
6207ac1cf4aSKay Sievers }
6212581c9ccSGreg Kroah-Hartman static DRIVER_ATTR_WO(uevent);
6227ac1cf4aSKay Sievers 
623765230b5SDmitry Torokhov static void driver_attach_async(void *_drv, async_cookie_t cookie)
624765230b5SDmitry Torokhov {
625765230b5SDmitry Torokhov 	struct device_driver *drv = _drv;
626765230b5SDmitry Torokhov 	int ret;
627765230b5SDmitry Torokhov 
628765230b5SDmitry Torokhov 	ret = driver_attach(drv);
629765230b5SDmitry Torokhov 
630765230b5SDmitry Torokhov 	pr_debug("bus: '%s': driver %s async attach completed: %d\n",
631765230b5SDmitry Torokhov 		 drv->bus->name, drv->name, ret);
632765230b5SDmitry Torokhov }
633765230b5SDmitry Torokhov 
6341da177e4SLinus Torvalds /**
6351da177e4SLinus Torvalds  * bus_add_driver - Add a driver to the bus.
6361da177e4SLinus Torvalds  * @drv: driver.
6371da177e4SLinus Torvalds  */
6381da177e4SLinus Torvalds int bus_add_driver(struct device_driver *drv)
6391da177e4SLinus Torvalds {
640e5dd1278SGreg Kroah-Hartman 	struct bus_type *bus;
641e5dd1278SGreg Kroah-Hartman 	struct driver_private *priv;
6421da177e4SLinus Torvalds 	int error = 0;
6431da177e4SLinus Torvalds 
644e5dd1278SGreg Kroah-Hartman 	bus = bus_get(drv->bus);
645d9fd4d3bSJeff Garzik 	if (!bus)
6464f6e1945SGreg Kroah-Hartman 		return -EINVAL;
647d9fd4d3bSJeff Garzik 
6487dc72b28SGreg Kroah-Hartman 	pr_debug("bus: '%s': add driver %s\n", bus->name, drv->name);
649e5dd1278SGreg Kroah-Hartman 
650e5dd1278SGreg Kroah-Hartman 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
65107634464SCornelia Huck 	if (!priv) {
65207634464SCornelia Huck 		error = -ENOMEM;
65307634464SCornelia Huck 		goto out_put_bus;
65407634464SCornelia Huck 	}
655e5dd1278SGreg Kroah-Hartman 	klist_init(&priv->klist_devices, NULL, NULL);
656e5dd1278SGreg Kroah-Hartman 	priv->driver = drv;
657e5dd1278SGreg Kroah-Hartman 	drv->p = priv;
658c8e90d82SGreg Kroah-Hartman 	priv->kobj.kset = bus->p->drivers_kset;
659c8e90d82SGreg Kroah-Hartman 	error = kobject_init_and_add(&priv->kobj, &driver_ktype, NULL,
660c8e90d82SGreg Kroah-Hartman 				     "%s", drv->name);
661dc0afa83SCornelia Huck 	if (error)
66207634464SCornelia Huck 		goto out_unregister;
6631da177e4SLinus Torvalds 
664190888acSMing Lei 	klist_add_tail(&priv->knode_bus, &bus->p->klist_drivers);
665c6f7e72aSGreg Kroah-Hartman 	if (drv->bus->p->drivers_autoprobe) {
666765230b5SDmitry Torokhov 		if (driver_allows_async_probing(drv)) {
667765230b5SDmitry Torokhov 			pr_debug("bus: '%s': probing driver %s asynchronously\n",
668765230b5SDmitry Torokhov 				drv->bus->name, drv->name);
669765230b5SDmitry Torokhov 			async_schedule(driver_attach_async, drv);
670765230b5SDmitry Torokhov 		} else {
671f86db396SAndrew Morton 			error = driver_attach(drv);
672f86db396SAndrew Morton 			if (error)
673f86db396SAndrew Morton 				goto out_unregister;
674b8c5cec2SKay Sievers 		}
675765230b5SDmitry Torokhov 	}
6761da177e4SLinus Torvalds 	module_add_driver(drv->owner, drv);
6771da177e4SLinus Torvalds 
6787ac1cf4aSKay Sievers 	error = driver_create_file(drv, &driver_attr_uevent);
6797ac1cf4aSKay Sievers 	if (error) {
6807ac1cf4aSKay Sievers 		printk(KERN_ERR "%s: uevent attr (%s) failed\n",
6812b3a302aSHarvey Harrison 			__func__, drv->name);
6827ac1cf4aSKay Sievers 	}
683e18945b1SGreg Kroah-Hartman 	error = driver_add_groups(drv, bus->drv_groups);
684f86db396SAndrew Morton 	if (error) {
685f86db396SAndrew Morton 		/* How the hell do we get out of this pickle? Give up */
686ed0617b5SGreg Kroah-Hartman 		printk(KERN_ERR "%s: driver_create_groups(%s) failed\n",
687ed0617b5SGreg Kroah-Hartman 			__func__, drv->name);
688e18945b1SGreg Kroah-Hartman 	}
6891a6f2a75SDmitry Torokhov 
6901a6f2a75SDmitry Torokhov 	if (!drv->suppress_bind_attrs) {
691f86db396SAndrew Morton 		error = add_bind_files(drv);
692f86db396SAndrew Morton 		if (error) {
693f86db396SAndrew Morton 			/* Ditto */
694f86db396SAndrew Morton 			printk(KERN_ERR "%s: add_bind_files(%s) failed\n",
6952b3a302aSHarvey Harrison 				__func__, drv->name);
696f86db396SAndrew Morton 		}
6971a6f2a75SDmitry Torokhov 	}
698d9fd4d3bSJeff Garzik 
6995c8563d7SKay Sievers 	return 0;
7001a6f2a75SDmitry Torokhov 
701f86db396SAndrew Morton out_unregister:
70299b28f1bSPhil Carmody 	kobject_put(&priv->kobj);
7030f9b011dSChristophe JAILLET 	/* drv->p is freed in driver_release()  */
7045c8563d7SKay Sievers 	drv->p = NULL;
705f86db396SAndrew Morton out_put_bus:
706fc1ede58SGreg Kroah-Hartman 	bus_put(bus);
707f86db396SAndrew Morton 	return error;
7081da177e4SLinus Torvalds }
7091da177e4SLinus Torvalds 
7101da177e4SLinus Torvalds /**
7111da177e4SLinus Torvalds  * bus_remove_driver - delete driver from bus's knowledge.
7121da177e4SLinus Torvalds  * @drv: driver.
7131da177e4SLinus Torvalds  *
7141da177e4SLinus Torvalds  * Detach the driver from the devices it controls, and remove
7151da177e4SLinus Torvalds  * it from its bus's list of drivers. Finally, we drop the reference
7161da177e4SLinus Torvalds  * to the bus we took in bus_add_driver().
7171da177e4SLinus Torvalds  */
7181da177e4SLinus Torvalds void bus_remove_driver(struct device_driver *drv)
7191da177e4SLinus Torvalds {
720d9fd4d3bSJeff Garzik 	if (!drv->bus)
721d9fd4d3bSJeff Garzik 		return;
722d9fd4d3bSJeff Garzik 
7231a6f2a75SDmitry Torokhov 	if (!drv->suppress_bind_attrs)
724874c6241SGreg Kroah-Hartman 		remove_bind_files(drv);
725ed0617b5SGreg Kroah-Hartman 	driver_remove_groups(drv, drv->bus->drv_groups);
7267ac1cf4aSKay Sievers 	driver_remove_file(drv, &driver_attr_uevent);
727e5dd1278SGreg Kroah-Hartman 	klist_remove(&drv->p->knode_bus);
7287dc72b28SGreg Kroah-Hartman 	pr_debug("bus: '%s': remove driver %s\n", drv->bus->name, drv->name);
7291da177e4SLinus Torvalds 	driver_detach(drv);
7301da177e4SLinus Torvalds 	module_remove_driver(drv);
731c10997f6SGreg Kroah-Hartman 	kobject_put(&drv->p->kobj);
732fc1ede58SGreg Kroah-Hartman 	bus_put(drv->bus);
7331da177e4SLinus Torvalds }
7341da177e4SLinus Torvalds 
7351da177e4SLinus Torvalds /* Helper for bus_rescan_devices's iter */
736f86db396SAndrew Morton static int __must_check bus_rescan_devices_helper(struct device *dev,
737f86db396SAndrew Morton 						  void *data)
7381da177e4SLinus Torvalds {
739f86db396SAndrew Morton 	int ret = 0;
740f86db396SAndrew Morton 
741bf74ad5bSAlan Stern 	if (!dev->driver) {
7428c97a46aSMartin Liu 		if (dev->parent && dev->bus->need_parent_lock)
7438e9394ceSGreg Kroah-Hartman 			device_lock(dev->parent);
744f86db396SAndrew Morton 		ret = device_attach(dev);
7458c97a46aSMartin Liu 		if (dev->parent && dev->bus->need_parent_lock)
7468e9394ceSGreg Kroah-Hartman 			device_unlock(dev->parent);
747bf74ad5bSAlan Stern 	}
748f86db396SAndrew Morton 	return ret < 0 ? ret : 0;
7491da177e4SLinus Torvalds }
7501da177e4SLinus Torvalds 
7511da177e4SLinus Torvalds /**
7521da177e4SLinus Torvalds  * bus_rescan_devices - rescan devices on the bus for possible drivers
7531da177e4SLinus Torvalds  * @bus: the bus to scan.
7541da177e4SLinus Torvalds  *
7551da177e4SLinus Torvalds  * This function will look for devices on the bus with no driver
75623d3d602SGreg Kroah-Hartman  * attached and rescan it against existing drivers to see if it matches
75723d3d602SGreg Kroah-Hartman  * any by calling device_attach() for the unbound devices.
7581da177e4SLinus Torvalds  */
759f86db396SAndrew Morton int bus_rescan_devices(struct bus_type *bus)
7601da177e4SLinus Torvalds {
761f86db396SAndrew Morton 	return bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper);
7621da177e4SLinus Torvalds }
7634a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_rescan_devices);
7641da177e4SLinus Torvalds 
765e935d5daSMoore, Eric /**
766e935d5daSMoore, Eric  * device_reprobe - remove driver for a device and probe for a new driver
767e935d5daSMoore, Eric  * @dev: the device to reprobe
768e935d5daSMoore, Eric  *
769e935d5daSMoore, Eric  * This function detaches the attached driver (if any) for the given
770e935d5daSMoore, Eric  * device and restarts the driver probing process.  It is intended
771e935d5daSMoore, Eric  * to use if probing criteria changed during a devices lifetime and
772e935d5daSMoore, Eric  * driver attachment should change accordingly.
773e935d5daSMoore, Eric  */
774f86db396SAndrew Morton int device_reprobe(struct device *dev)
775e935d5daSMoore, Eric {
776e935d5daSMoore, Eric 	if (dev->driver) {
7778c97a46aSMartin Liu 		if (dev->parent && dev->bus->need_parent_lock)
7788e9394ceSGreg Kroah-Hartman 			device_lock(dev->parent);
779e935d5daSMoore, Eric 		device_release_driver(dev);
7808c97a46aSMartin Liu 		if (dev->parent && dev->bus->need_parent_lock)
7818e9394ceSGreg Kroah-Hartman 			device_unlock(dev->parent);
782e935d5daSMoore, Eric 	}
783f86db396SAndrew Morton 	return bus_rescan_devices_helper(dev, NULL);
784e935d5daSMoore, Eric }
785e935d5daSMoore, Eric EXPORT_SYMBOL_GPL(device_reprobe);
7861da177e4SLinus Torvalds 
7871da177e4SLinus Torvalds /**
7881da177e4SLinus Torvalds  * find_bus - locate bus by name.
7891da177e4SLinus Torvalds  * @name: name of bus.
7901da177e4SLinus Torvalds  *
7911da177e4SLinus Torvalds  * Call kset_find_obj() to iterate over list of buses to
7921da177e4SLinus Torvalds  * find a bus by name. Return bus if found.
7931da177e4SLinus Torvalds  *
7941da177e4SLinus Torvalds  * Note that kset_find_obj increments bus' reference count.
7951da177e4SLinus Torvalds  */
7967e4ef085SAdrian Bunk #if 0
7971da177e4SLinus Torvalds struct bus_type *find_bus(char *name)
7981da177e4SLinus Torvalds {
79959a54833SGreg Kroah-Hartman 	struct kobject *k = kset_find_obj(bus_kset, name);
8001da177e4SLinus Torvalds 	return k ? to_bus(k) : NULL;
8011da177e4SLinus Torvalds }
8027e4ef085SAdrian Bunk #endif  /*  0  */
8031da177e4SLinus Torvalds 
80412478ba0SGreg Kroah-Hartman static int bus_add_groups(struct bus_type *bus,
80512478ba0SGreg Kroah-Hartman 			  const struct attribute_group **groups)
80612478ba0SGreg Kroah-Hartman {
8073e9b2baeSGreg Kroah-Hartman 	return sysfs_create_groups(&bus->p->subsys.kobj, groups);
80812478ba0SGreg Kroah-Hartman }
80912478ba0SGreg Kroah-Hartman 
81012478ba0SGreg Kroah-Hartman static void bus_remove_groups(struct bus_type *bus,
81112478ba0SGreg Kroah-Hartman 			      const struct attribute_group **groups)
81212478ba0SGreg Kroah-Hartman {
8133e9b2baeSGreg Kroah-Hartman 	sysfs_remove_groups(&bus->p->subsys.kobj, groups);
81412478ba0SGreg Kroah-Hartman }
81512478ba0SGreg Kroah-Hartman 
81634bb61f9SJames Bottomley static void klist_devices_get(struct klist_node *n)
81734bb61f9SJames Bottomley {
818ae1b4171SGreg Kroah-Hartman 	struct device_private *dev_prv = to_device_private_bus(n);
819ae1b4171SGreg Kroah-Hartman 	struct device *dev = dev_prv->device;
82034bb61f9SJames Bottomley 
82134bb61f9SJames Bottomley 	get_device(dev);
82234bb61f9SJames Bottomley }
82334bb61f9SJames Bottomley 
82434bb61f9SJames Bottomley static void klist_devices_put(struct klist_node *n)
82534bb61f9SJames Bottomley {
826ae1b4171SGreg Kroah-Hartman 	struct device_private *dev_prv = to_device_private_bus(n);
827ae1b4171SGreg Kroah-Hartman 	struct device *dev = dev_prv->device;
82834bb61f9SJames Bottomley 
82934bb61f9SJames Bottomley 	put_device(dev);
83034bb61f9SJames Bottomley }
83134bb61f9SJames Bottomley 
8327ac1cf4aSKay Sievers static ssize_t bus_uevent_store(struct bus_type *bus,
8337ac1cf4aSKay Sievers 				const char *buf, size_t count)
8347ac1cf4aSKay Sievers {
835df44b479SPeter Rajnoha 	int rc;
836df44b479SPeter Rajnoha 
837df44b479SPeter Rajnoha 	rc = kobject_synth_uevent(&bus->p->subsys.kobj, buf, count);
838df44b479SPeter Rajnoha 	return rc ? rc : count;
8397ac1cf4aSKay Sievers }
840*a4723041SGreg Kroah-Hartman /*
841*a4723041SGreg Kroah-Hartman  * "open code" the old BUS_ATTR() macro here.  We want to use BUS_ATTR_WO()
842*a4723041SGreg Kroah-Hartman  * here, but can not use it as earlier in the file we have
843*a4723041SGreg Kroah-Hartman  * DEVICE_ATTR_WO(uevent), which would cause a clash with the with the store
844*a4723041SGreg Kroah-Hartman  * function name.
845*a4723041SGreg Kroah-Hartman  */
846*a4723041SGreg Kroah-Hartman static struct bus_attribute bus_attr_uevent = __ATTR(uevent, S_IWUSR, NULL,
847*a4723041SGreg Kroah-Hartman 						     bus_uevent_store);
8487ac1cf4aSKay Sievers 
8491da177e4SLinus Torvalds /**
850be871b7eSMichal Hocko  * bus_register - register a driver-core subsystem
85178d79559SRandy Dunlap  * @bus: bus to register
8521da177e4SLinus Torvalds  *
85378d79559SRandy Dunlap  * Once we have that, we register the bus with the kobject
8541da177e4SLinus Torvalds  * infrastructure, then register the children subsystems it has:
855ca22e56dSKay Sievers  * the devices and drivers that belong to the subsystem.
8561da177e4SLinus Torvalds  */
857be871b7eSMichal Hocko int bus_register(struct bus_type *bus)
8581da177e4SLinus Torvalds {
8591da177e4SLinus Torvalds 	int retval;
8606b6e39a6SKay Sievers 	struct subsys_private *priv;
861be871b7eSMichal Hocko 	struct lock_class_key *key = &bus->lock_key;
8621da177e4SLinus Torvalds 
8636b6e39a6SKay Sievers 	priv = kzalloc(sizeof(struct subsys_private), GFP_KERNEL);
864c6f7e72aSGreg Kroah-Hartman 	if (!priv)
865c6f7e72aSGreg Kroah-Hartman 		return -ENOMEM;
866116af378SBenjamin Herrenschmidt 
867c6f7e72aSGreg Kroah-Hartman 	priv->bus = bus;
868c6f7e72aSGreg Kroah-Hartman 	bus->p = priv;
869c6f7e72aSGreg Kroah-Hartman 
870c6f7e72aSGreg Kroah-Hartman 	BLOCKING_INIT_NOTIFIER_HEAD(&priv->bus_notifier);
871c6f7e72aSGreg Kroah-Hartman 
872c6f7e72aSGreg Kroah-Hartman 	retval = kobject_set_name(&priv->subsys.kobj, "%s", bus->name);
8731da177e4SLinus Torvalds 	if (retval)
8741da177e4SLinus Torvalds 		goto out;
8751da177e4SLinus Torvalds 
876c6f7e72aSGreg Kroah-Hartman 	priv->subsys.kobj.kset = bus_kset;
877c6f7e72aSGreg Kroah-Hartman 	priv->subsys.kobj.ktype = &bus_ktype;
878c6f7e72aSGreg Kroah-Hartman 	priv->drivers_autoprobe = 1;
879d6b05b84SGreg Kroah-Hartman 
880c6f7e72aSGreg Kroah-Hartman 	retval = kset_register(&priv->subsys);
8811da177e4SLinus Torvalds 	if (retval)
8821da177e4SLinus Torvalds 		goto out;
8831da177e4SLinus Torvalds 
8847ac1cf4aSKay Sievers 	retval = bus_create_file(bus, &bus_attr_uevent);
8857ac1cf4aSKay Sievers 	if (retval)
8867ac1cf4aSKay Sievers 		goto bus_uevent_fail;
8877ac1cf4aSKay Sievers 
888c6f7e72aSGreg Kroah-Hartman 	priv->devices_kset = kset_create_and_add("devices", NULL,
889c6f7e72aSGreg Kroah-Hartman 						 &priv->subsys.kobj);
890c6f7e72aSGreg Kroah-Hartman 	if (!priv->devices_kset) {
8913d899596SGreg Kroah-Hartman 		retval = -ENOMEM;
8921da177e4SLinus Torvalds 		goto bus_devices_fail;
8933d899596SGreg Kroah-Hartman 	}
8941da177e4SLinus Torvalds 
895c6f7e72aSGreg Kroah-Hartman 	priv->drivers_kset = kset_create_and_add("drivers", NULL,
896c6f7e72aSGreg Kroah-Hartman 						 &priv->subsys.kobj);
897c6f7e72aSGreg Kroah-Hartman 	if (!priv->drivers_kset) {
8986dcec251SGreg Kroah-Hartman 		retval = -ENOMEM;
8991da177e4SLinus Torvalds 		goto bus_drivers_fail;
9006dcec251SGreg Kroah-Hartman 	}
901465c7a3aSmochel@digitalimplant.org 
902ca22e56dSKay Sievers 	INIT_LIST_HEAD(&priv->interfaces);
903ca22e56dSKay Sievers 	__mutex_init(&priv->mutex, "subsys mutex", key);
904c6f7e72aSGreg Kroah-Hartman 	klist_init(&priv->klist_devices, klist_devices_get, klist_devices_put);
905c6f7e72aSGreg Kroah-Hartman 	klist_init(&priv->klist_drivers, NULL, NULL);
906b8c5cec2SKay Sievers 
907b8c5cec2SKay Sievers 	retval = add_probe_files(bus);
908b8c5cec2SKay Sievers 	if (retval)
909b8c5cec2SKay Sievers 		goto bus_probe_files_fail;
910b8c5cec2SKay Sievers 
91112478ba0SGreg Kroah-Hartman 	retval = bus_add_groups(bus, bus->bus_groups);
91212478ba0SGreg Kroah-Hartman 	if (retval)
91312478ba0SGreg Kroah-Hartman 		goto bus_groups_fail;
9141da177e4SLinus Torvalds 
9157dc72b28SGreg Kroah-Hartman 	pr_debug("bus: '%s': registered\n", bus->name);
9161da177e4SLinus Torvalds 	return 0;
9171da177e4SLinus Torvalds 
91812478ba0SGreg Kroah-Hartman bus_groups_fail:
919b8c5cec2SKay Sievers 	remove_probe_files(bus);
920b8c5cec2SKay Sievers bus_probe_files_fail:
921c6f7e72aSGreg Kroah-Hartman 	kset_unregister(bus->p->drivers_kset);
9221da177e4SLinus Torvalds bus_drivers_fail:
923c6f7e72aSGreg Kroah-Hartman 	kset_unregister(bus->p->devices_kset);
9241da177e4SLinus Torvalds bus_devices_fail:
9257ac1cf4aSKay Sievers 	bus_remove_file(bus, &bus_attr_uevent);
9267ac1cf4aSKay Sievers bus_uevent_fail:
927c6f7e72aSGreg Kroah-Hartman 	kset_unregister(&bus->p->subsys);
9281da177e4SLinus Torvalds out:
929600c20f3SJike Song 	kfree(bus->p);
930f48f3febSDave Young 	bus->p = NULL;
9311da177e4SLinus Torvalds 	return retval;
9321da177e4SLinus Torvalds }
933be871b7eSMichal Hocko EXPORT_SYMBOL_GPL(bus_register);
9341da177e4SLinus Torvalds 
9351da177e4SLinus Torvalds /**
9361da177e4SLinus Torvalds  * bus_unregister - remove a bus from the system
9371da177e4SLinus Torvalds  * @bus: bus.
9381da177e4SLinus Torvalds  *
9391da177e4SLinus Torvalds  * Unregister the child subsystems and the bus itself.
940fc1ede58SGreg Kroah-Hartman  * Finally, we call bus_put() to release the refcount
9411da177e4SLinus Torvalds  */
9421da177e4SLinus Torvalds void bus_unregister(struct bus_type *bus)
9431da177e4SLinus Torvalds {
9447dc72b28SGreg Kroah-Hartman 	pr_debug("bus: '%s': unregistering\n", bus->name);
945ca22e56dSKay Sievers 	if (bus->dev_root)
946ca22e56dSKay Sievers 		device_unregister(bus->dev_root);
94712478ba0SGreg Kroah-Hartman 	bus_remove_groups(bus, bus->bus_groups);
948b8c5cec2SKay Sievers 	remove_probe_files(bus);
949c6f7e72aSGreg Kroah-Hartman 	kset_unregister(bus->p->drivers_kset);
950c6f7e72aSGreg Kroah-Hartman 	kset_unregister(bus->p->devices_kset);
9517ac1cf4aSKay Sievers 	bus_remove_file(bus, &bus_attr_uevent);
952c6f7e72aSGreg Kroah-Hartman 	kset_unregister(&bus->p->subsys);
9531da177e4SLinus Torvalds }
9544a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_unregister);
9551da177e4SLinus Torvalds 
956116af378SBenjamin Herrenschmidt int bus_register_notifier(struct bus_type *bus, struct notifier_block *nb)
957116af378SBenjamin Herrenschmidt {
958c6f7e72aSGreg Kroah-Hartman 	return blocking_notifier_chain_register(&bus->p->bus_notifier, nb);
959116af378SBenjamin Herrenschmidt }
960116af378SBenjamin Herrenschmidt EXPORT_SYMBOL_GPL(bus_register_notifier);
961116af378SBenjamin Herrenschmidt 
962116af378SBenjamin Herrenschmidt int bus_unregister_notifier(struct bus_type *bus, struct notifier_block *nb)
963116af378SBenjamin Herrenschmidt {
964c6f7e72aSGreg Kroah-Hartman 	return blocking_notifier_chain_unregister(&bus->p->bus_notifier, nb);
965116af378SBenjamin Herrenschmidt }
966116af378SBenjamin Herrenschmidt EXPORT_SYMBOL_GPL(bus_unregister_notifier);
967116af378SBenjamin Herrenschmidt 
9680fed80f7SGreg Kroah-Hartman struct kset *bus_get_kset(struct bus_type *bus)
9690fed80f7SGreg Kroah-Hartman {
970c6f7e72aSGreg Kroah-Hartman 	return &bus->p->subsys;
9710fed80f7SGreg Kroah-Hartman }
9720fed80f7SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_get_kset);
9730fed80f7SGreg Kroah-Hartman 
974b249072eSGreg Kroah-Hartman struct klist *bus_get_device_klist(struct bus_type *bus)
975b249072eSGreg Kroah-Hartman {
976c6f7e72aSGreg Kroah-Hartman 	return &bus->p->klist_devices;
977b249072eSGreg Kroah-Hartman }
978b249072eSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_get_device_klist);
979b249072eSGreg Kroah-Hartman 
98099178b03SGreg Kroah-Hartman /*
981dca25ebdSRobert P. J. Day  * Yes, this forcibly breaks the klist abstraction temporarily.  It
98299178b03SGreg Kroah-Hartman  * just wants to sort the klist, not change reference counts and
98399178b03SGreg Kroah-Hartman  * take/drop locks rapidly in the process.  It does all this while
98499178b03SGreg Kroah-Hartman  * holding the lock for the list, so objects can't otherwise be
98599178b03SGreg Kroah-Hartman  * added/removed while we're swizzling.
98699178b03SGreg Kroah-Hartman  */
98799178b03SGreg Kroah-Hartman static void device_insertion_sort_klist(struct device *a, struct list_head *list,
98899178b03SGreg Kroah-Hartman 					int (*compare)(const struct device *a,
98999178b03SGreg Kroah-Hartman 							const struct device *b))
99099178b03SGreg Kroah-Hartman {
99199178b03SGreg Kroah-Hartman 	struct klist_node *n;
992ae1b4171SGreg Kroah-Hartman 	struct device_private *dev_prv;
99399178b03SGreg Kroah-Hartman 	struct device *b;
99499178b03SGreg Kroah-Hartman 
9954c62785eSGeliang Tang 	list_for_each_entry(n, list, n_node) {
996ae1b4171SGreg Kroah-Hartman 		dev_prv = to_device_private_bus(n);
997ae1b4171SGreg Kroah-Hartman 		b = dev_prv->device;
99899178b03SGreg Kroah-Hartman 		if (compare(a, b) <= 0) {
999ae1b4171SGreg Kroah-Hartman 			list_move_tail(&a->p->knode_bus.n_node,
1000ae1b4171SGreg Kroah-Hartman 				       &b->p->knode_bus.n_node);
100199178b03SGreg Kroah-Hartman 			return;
100299178b03SGreg Kroah-Hartman 		}
100399178b03SGreg Kroah-Hartman 	}
1004ae1b4171SGreg Kroah-Hartman 	list_move_tail(&a->p->knode_bus.n_node, list);
100599178b03SGreg Kroah-Hartman }
100699178b03SGreg Kroah-Hartman 
100799178b03SGreg Kroah-Hartman void bus_sort_breadthfirst(struct bus_type *bus,
100899178b03SGreg Kroah-Hartman 			   int (*compare)(const struct device *a,
100999178b03SGreg Kroah-Hartman 					  const struct device *b))
101099178b03SGreg Kroah-Hartman {
101199178b03SGreg Kroah-Hartman 	LIST_HEAD(sorted_devices);
10124c62785eSGeliang Tang 	struct klist_node *n, *tmp;
1013ae1b4171SGreg Kroah-Hartman 	struct device_private *dev_prv;
101499178b03SGreg Kroah-Hartman 	struct device *dev;
101599178b03SGreg Kroah-Hartman 	struct klist *device_klist;
101699178b03SGreg Kroah-Hartman 
101799178b03SGreg Kroah-Hartman 	device_klist = bus_get_device_klist(bus);
101899178b03SGreg Kroah-Hartman 
101999178b03SGreg Kroah-Hartman 	spin_lock(&device_klist->k_lock);
10204c62785eSGeliang Tang 	list_for_each_entry_safe(n, tmp, &device_klist->k_list, n_node) {
1021ae1b4171SGreg Kroah-Hartman 		dev_prv = to_device_private_bus(n);
1022ae1b4171SGreg Kroah-Hartman 		dev = dev_prv->device;
102399178b03SGreg Kroah-Hartman 		device_insertion_sort_klist(dev, &sorted_devices, compare);
102499178b03SGreg Kroah-Hartman 	}
102599178b03SGreg Kroah-Hartman 	list_splice(&sorted_devices, &device_klist->k_list);
102699178b03SGreg Kroah-Hartman 	spin_unlock(&device_klist->k_lock);
102799178b03SGreg Kroah-Hartman }
102899178b03SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_sort_breadthfirst);
102999178b03SGreg Kroah-Hartman 
1030ca22e56dSKay Sievers /**
1031ca22e56dSKay Sievers  * subsys_dev_iter_init - initialize subsys device iterator
1032ca22e56dSKay Sievers  * @iter: subsys iterator to initialize
1033ca22e56dSKay Sievers  * @subsys: the subsys we wanna iterate over
1034ca22e56dSKay Sievers  * @start: the device to start iterating from, if any
1035ca22e56dSKay Sievers  * @type: device_type of the devices to iterate over, NULL for all
1036ca22e56dSKay Sievers  *
1037ca22e56dSKay Sievers  * Initialize subsys iterator @iter such that it iterates over devices
1038ca22e56dSKay Sievers  * of @subsys.  If @start is set, the list iteration will start there,
1039ca22e56dSKay Sievers  * otherwise if it is NULL, the iteration starts at the beginning of
1040ca22e56dSKay Sievers  * the list.
1041ca22e56dSKay Sievers  */
10427cd9c9bbSGreg Kroah-Hartman void subsys_dev_iter_init(struct subsys_dev_iter *iter, struct bus_type *subsys,
1043ca22e56dSKay Sievers 			  struct device *start, const struct device_type *type)
1044ca22e56dSKay Sievers {
1045ca22e56dSKay Sievers 	struct klist_node *start_knode = NULL;
1046ca22e56dSKay Sievers 
1047ca22e56dSKay Sievers 	if (start)
1048ca22e56dSKay Sievers 		start_knode = &start->p->knode_bus;
10497cd9c9bbSGreg Kroah-Hartman 	klist_iter_init_node(&subsys->p->klist_devices, &iter->ki, start_knode);
1050ca22e56dSKay Sievers 	iter->type = type;
1051ca22e56dSKay Sievers }
1052ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_dev_iter_init);
1053ca22e56dSKay Sievers 
1054ca22e56dSKay Sievers /**
1055ca22e56dSKay Sievers  * subsys_dev_iter_next - iterate to the next device
1056ca22e56dSKay Sievers  * @iter: subsys iterator to proceed
1057ca22e56dSKay Sievers  *
1058ca22e56dSKay Sievers  * Proceed @iter to the next device and return it.  Returns NULL if
1059ca22e56dSKay Sievers  * iteration is complete.
1060ca22e56dSKay Sievers  *
1061ca22e56dSKay Sievers  * The returned device is referenced and won't be released till
1062ca22e56dSKay Sievers  * iterator is proceed to the next device or exited.  The caller is
1063ca22e56dSKay Sievers  * free to do whatever it wants to do with the device including
1064ca22e56dSKay Sievers  * calling back into subsys code.
1065ca22e56dSKay Sievers  */
1066ca22e56dSKay Sievers struct device *subsys_dev_iter_next(struct subsys_dev_iter *iter)
1067ca22e56dSKay Sievers {
1068ca22e56dSKay Sievers 	struct klist_node *knode;
1069ca22e56dSKay Sievers 	struct device *dev;
1070ca22e56dSKay Sievers 
1071ca22e56dSKay Sievers 	for (;;) {
1072ca22e56dSKay Sievers 		knode = klist_next(&iter->ki);
1073ca22e56dSKay Sievers 		if (!knode)
1074ca22e56dSKay Sievers 			return NULL;
1075371fd7a2SGeliang Tang 		dev = to_device_private_bus(knode)->device;
1076ca22e56dSKay Sievers 		if (!iter->type || iter->type == dev->type)
1077ca22e56dSKay Sievers 			return dev;
1078ca22e56dSKay Sievers 	}
1079ca22e56dSKay Sievers }
1080ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_dev_iter_next);
1081ca22e56dSKay Sievers 
1082ca22e56dSKay Sievers /**
1083ca22e56dSKay Sievers  * subsys_dev_iter_exit - finish iteration
1084ca22e56dSKay Sievers  * @iter: subsys iterator to finish
1085ca22e56dSKay Sievers  *
1086ca22e56dSKay Sievers  * Finish an iteration.  Always call this function after iteration is
1087ca22e56dSKay Sievers  * complete whether the iteration ran till the end or not.
1088ca22e56dSKay Sievers  */
1089ca22e56dSKay Sievers void subsys_dev_iter_exit(struct subsys_dev_iter *iter)
1090ca22e56dSKay Sievers {
1091ca22e56dSKay Sievers 	klist_iter_exit(&iter->ki);
1092ca22e56dSKay Sievers }
1093ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_dev_iter_exit);
1094ca22e56dSKay Sievers 
1095ca22e56dSKay Sievers int subsys_interface_register(struct subsys_interface *sif)
1096ca22e56dSKay Sievers {
1097ca22e56dSKay Sievers 	struct bus_type *subsys;
1098ca22e56dSKay Sievers 	struct subsys_dev_iter iter;
1099ca22e56dSKay Sievers 	struct device *dev;
1100ca22e56dSKay Sievers 
1101ca22e56dSKay Sievers 	if (!sif || !sif->subsys)
1102ca22e56dSKay Sievers 		return -ENODEV;
1103ca22e56dSKay Sievers 
1104ca22e56dSKay Sievers 	subsys = bus_get(sif->subsys);
1105ca22e56dSKay Sievers 	if (!subsys)
1106ca22e56dSKay Sievers 		return -EINVAL;
1107ca22e56dSKay Sievers 
1108ca22e56dSKay Sievers 	mutex_lock(&subsys->p->mutex);
1109ca22e56dSKay Sievers 	list_add_tail(&sif->node, &subsys->p->interfaces);
1110ca22e56dSKay Sievers 	if (sif->add_dev) {
1111ca22e56dSKay Sievers 		subsys_dev_iter_init(&iter, subsys, NULL, NULL);
1112ca22e56dSKay Sievers 		while ((dev = subsys_dev_iter_next(&iter)))
1113ca22e56dSKay Sievers 			sif->add_dev(dev, sif);
1114ca22e56dSKay Sievers 		subsys_dev_iter_exit(&iter);
1115ca22e56dSKay Sievers 	}
1116ca22e56dSKay Sievers 	mutex_unlock(&subsys->p->mutex);
1117ca22e56dSKay Sievers 
1118ca22e56dSKay Sievers 	return 0;
1119ca22e56dSKay Sievers }
1120ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_interface_register);
1121ca22e56dSKay Sievers 
1122ca22e56dSKay Sievers void subsys_interface_unregister(struct subsys_interface *sif)
1123ca22e56dSKay Sievers {
11242b31594aSJonghwan Choi 	struct bus_type *subsys;
1125ca22e56dSKay Sievers 	struct subsys_dev_iter iter;
1126ca22e56dSKay Sievers 	struct device *dev;
1127ca22e56dSKay Sievers 
11282b31594aSJonghwan Choi 	if (!sif || !sif->subsys)
1129ca22e56dSKay Sievers 		return;
1130ca22e56dSKay Sievers 
11312b31594aSJonghwan Choi 	subsys = sif->subsys;
11322b31594aSJonghwan Choi 
1133ca22e56dSKay Sievers 	mutex_lock(&subsys->p->mutex);
1134ca22e56dSKay Sievers 	list_del_init(&sif->node);
1135ca22e56dSKay Sievers 	if (sif->remove_dev) {
1136ca22e56dSKay Sievers 		subsys_dev_iter_init(&iter, subsys, NULL, NULL);
1137ca22e56dSKay Sievers 		while ((dev = subsys_dev_iter_next(&iter)))
1138ca22e56dSKay Sievers 			sif->remove_dev(dev, sif);
1139ca22e56dSKay Sievers 		subsys_dev_iter_exit(&iter);
1140ca22e56dSKay Sievers 	}
1141ca22e56dSKay Sievers 	mutex_unlock(&subsys->p->mutex);
1142ca22e56dSKay Sievers 
1143ca22e56dSKay Sievers 	bus_put(subsys);
1144ca22e56dSKay Sievers }
1145ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_interface_unregister);
1146ca22e56dSKay Sievers 
1147ca22e56dSKay Sievers static void system_root_device_release(struct device *dev)
1148ca22e56dSKay Sievers {
1149ca22e56dSKay Sievers 	kfree(dev);
1150ca22e56dSKay Sievers }
1151d73ce004STejun Heo 
1152d73ce004STejun Heo static int subsys_register(struct bus_type *subsys,
1153d73ce004STejun Heo 			   const struct attribute_group **groups,
1154d73ce004STejun Heo 			   struct kobject *parent_of_root)
1155d73ce004STejun Heo {
1156d73ce004STejun Heo 	struct device *dev;
1157d73ce004STejun Heo 	int err;
1158d73ce004STejun Heo 
1159d73ce004STejun Heo 	err = bus_register(subsys);
1160d73ce004STejun Heo 	if (err < 0)
1161d73ce004STejun Heo 		return err;
1162d73ce004STejun Heo 
1163d73ce004STejun Heo 	dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1164d73ce004STejun Heo 	if (!dev) {
1165d73ce004STejun Heo 		err = -ENOMEM;
1166d73ce004STejun Heo 		goto err_dev;
1167d73ce004STejun Heo 	}
1168d73ce004STejun Heo 
1169d73ce004STejun Heo 	err = dev_set_name(dev, "%s", subsys->name);
1170d73ce004STejun Heo 	if (err < 0)
1171d73ce004STejun Heo 		goto err_name;
1172d73ce004STejun Heo 
1173d73ce004STejun Heo 	dev->kobj.parent = parent_of_root;
1174d73ce004STejun Heo 	dev->groups = groups;
1175d73ce004STejun Heo 	dev->release = system_root_device_release;
1176d73ce004STejun Heo 
1177d73ce004STejun Heo 	err = device_register(dev);
1178d73ce004STejun Heo 	if (err < 0)
1179d73ce004STejun Heo 		goto err_dev_reg;
1180d73ce004STejun Heo 
1181d73ce004STejun Heo 	subsys->dev_root = dev;
1182d73ce004STejun Heo 	return 0;
1183d73ce004STejun Heo 
1184d73ce004STejun Heo err_dev_reg:
1185d73ce004STejun Heo 	put_device(dev);
1186d73ce004STejun Heo 	dev = NULL;
1187d73ce004STejun Heo err_name:
1188d73ce004STejun Heo 	kfree(dev);
1189d73ce004STejun Heo err_dev:
1190d73ce004STejun Heo 	bus_unregister(subsys);
1191d73ce004STejun Heo 	return err;
1192d73ce004STejun Heo }
1193d73ce004STejun Heo 
1194ca22e56dSKay Sievers /**
1195ca22e56dSKay Sievers  * subsys_system_register - register a subsystem at /sys/devices/system/
119678d79559SRandy Dunlap  * @subsys: system subsystem
119778d79559SRandy Dunlap  * @groups: default attributes for the root device
1198ca22e56dSKay Sievers  *
1199ca22e56dSKay Sievers  * All 'system' subsystems have a /sys/devices/system/<name> root device
1200ca22e56dSKay Sievers  * with the name of the subsystem. The root device can carry subsystem-
1201ca22e56dSKay Sievers  * wide attributes. All registered devices are below this single root
1202ca22e56dSKay Sievers  * device and are named after the subsystem with a simple enumeration
1203e227867fSMasanari Iida  * number appended. The registered devices are not explicitly named;
1204ca22e56dSKay Sievers  * only 'id' in the device needs to be set.
1205ca22e56dSKay Sievers  *
1206ca22e56dSKay Sievers  * Do not use this interface for anything new, it exists for compatibility
1207ca22e56dSKay Sievers  * with bad ideas only. New subsystems should use plain subsystems; and
1208ca22e56dSKay Sievers  * add the subsystem-wide attributes should be added to the subsystem
1209ca22e56dSKay Sievers  * directory itself and not some create fake root-device placed in
1210ca22e56dSKay Sievers  * /sys/devices/system/<name>.
1211ca22e56dSKay Sievers  */
1212ca22e56dSKay Sievers int subsys_system_register(struct bus_type *subsys,
1213ca22e56dSKay Sievers 			   const struct attribute_group **groups)
1214ca22e56dSKay Sievers {
1215d73ce004STejun Heo 	return subsys_register(subsys, groups, &system_kset->kobj);
1216ca22e56dSKay Sievers }
1217ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_system_register);
1218ca22e56dSKay Sievers 
1219d73ce004STejun Heo /**
1220d73ce004STejun Heo  * subsys_virtual_register - register a subsystem at /sys/devices/virtual/
1221d73ce004STejun Heo  * @subsys: virtual subsystem
1222d73ce004STejun Heo  * @groups: default attributes for the root device
1223d73ce004STejun Heo  *
1224d73ce004STejun Heo  * All 'virtual' subsystems have a /sys/devices/system/<name> root device
1225d73ce004STejun Heo  * with the name of the subystem.  The root device can carry subsystem-wide
1226d73ce004STejun Heo  * attributes.  All registered devices are below this single root device.
1227d73ce004STejun Heo  * There's no restriction on device naming.  This is for kernel software
1228d73ce004STejun Heo  * constructs which need sysfs interface.
1229d73ce004STejun Heo  */
1230d73ce004STejun Heo int subsys_virtual_register(struct bus_type *subsys,
1231d73ce004STejun Heo 			    const struct attribute_group **groups)
1232d73ce004STejun Heo {
1233d73ce004STejun Heo 	struct kobject *virtual_dir;
1234d73ce004STejun Heo 
1235d73ce004STejun Heo 	virtual_dir = virtual_device_parent(NULL);
1236d73ce004STejun Heo 	if (!virtual_dir)
1237d73ce004STejun Heo 		return -ENOMEM;
1238d73ce004STejun Heo 
1239d73ce004STejun Heo 	return subsys_register(subsys, groups, virtual_dir);
1240d73ce004STejun Heo }
12411c04fc35SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(subsys_virtual_register);
1242d73ce004STejun Heo 
12431da177e4SLinus Torvalds int __init buses_init(void)
12441da177e4SLinus Torvalds {
124559a54833SGreg Kroah-Hartman 	bus_kset = kset_create_and_add("bus", &bus_uevent_ops, NULL);
124659a54833SGreg Kroah-Hartman 	if (!bus_kset)
124759a54833SGreg Kroah-Hartman 		return -ENOMEM;
1248ca22e56dSKay Sievers 
1249ca22e56dSKay Sievers 	system_kset = kset_create_and_add("system", NULL, &devices_kset->kobj);
1250ca22e56dSKay Sievers 	if (!system_kset)
1251ca22e56dSKay Sievers 		return -ENOMEM;
1252ca22e56dSKay Sievers 
125359a54833SGreg Kroah-Hartman 	return 0;
12541da177e4SLinus Torvalds }
1255