xref: /linux/drivers/base/bus.c (revision 4fa3e78be7e985ca814ce2aa0c09cbee404efcf7)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * bus.c - bus driver management
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Copyright (c) 2002-3 Patrick Mochel
51da177e4SLinus Torvalds  * Copyright (c) 2002-3 Open Source Development Labs
6e5dd1278SGreg Kroah-Hartman  * Copyright (c) 2007 Greg Kroah-Hartman <gregkh@suse.de>
7e5dd1278SGreg Kroah-Hartman  * Copyright (c) 2007 Novell Inc.
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  * This file is released under the GPLv2
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  */
121da177e4SLinus Torvalds 
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>
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 
341da177e4SLinus Torvalds 
35b8c5cec2SKay Sievers static int __must_check bus_rescan_devices_helper(struct device *dev,
36b8c5cec2SKay Sievers 						void *data);
37b8c5cec2SKay Sievers 
385901d014SGreg Kroah-Hartman static struct bus_type *bus_get(struct bus_type *bus)
395901d014SGreg Kroah-Hartman {
40c6f7e72aSGreg Kroah-Hartman 	if (bus) {
41c6f7e72aSGreg Kroah-Hartman 		kset_get(&bus->p->subsys);
42c6f7e72aSGreg Kroah-Hartman 		return bus;
43c6f7e72aSGreg Kroah-Hartman 	}
44c6f7e72aSGreg Kroah-Hartman 	return NULL;
455901d014SGreg Kroah-Hartman }
465901d014SGreg Kroah-Hartman 
47fc1ede58SGreg Kroah-Hartman static void bus_put(struct bus_type *bus)
48fc1ede58SGreg Kroah-Hartman {
49c6f7e72aSGreg Kroah-Hartman 	if (bus)
50c6f7e72aSGreg Kroah-Hartman 		kset_put(&bus->p->subsys);
51fc1ede58SGreg Kroah-Hartman }
52fc1ede58SGreg Kroah-Hartman 
534a3ad20cSGreg Kroah-Hartman static ssize_t drv_attr_show(struct kobject *kobj, struct attribute *attr,
544a3ad20cSGreg Kroah-Hartman 			     char *buf)
551da177e4SLinus Torvalds {
561da177e4SLinus Torvalds 	struct driver_attribute *drv_attr = to_drv_attr(attr);
57e5dd1278SGreg Kroah-Hartman 	struct driver_private *drv_priv = to_driver(kobj);
584a0c20bfSDmitry Torokhov 	ssize_t ret = -EIO;
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds 	if (drv_attr->show)
61e5dd1278SGreg Kroah-Hartman 		ret = drv_attr->show(drv_priv->driver, buf);
621da177e4SLinus Torvalds 	return ret;
631da177e4SLinus Torvalds }
641da177e4SLinus Torvalds 
654a3ad20cSGreg Kroah-Hartman static ssize_t drv_attr_store(struct kobject *kobj, struct attribute *attr,
661da177e4SLinus Torvalds 			      const char *buf, size_t count)
671da177e4SLinus Torvalds {
681da177e4SLinus Torvalds 	struct driver_attribute *drv_attr = to_drv_attr(attr);
69e5dd1278SGreg Kroah-Hartman 	struct driver_private *drv_priv = to_driver(kobj);
704a0c20bfSDmitry Torokhov 	ssize_t ret = -EIO;
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds 	if (drv_attr->store)
73e5dd1278SGreg Kroah-Hartman 		ret = drv_attr->store(drv_priv->driver, buf, count);
741da177e4SLinus Torvalds 	return ret;
751da177e4SLinus Torvalds }
761da177e4SLinus Torvalds 
7752cf25d0SEmese Revfy static const struct sysfs_ops driver_sysfs_ops = {
781da177e4SLinus Torvalds 	.show	= drv_attr_show,
791da177e4SLinus Torvalds 	.store	= drv_attr_store,
801da177e4SLinus Torvalds };
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds static void driver_release(struct kobject *kobj)
831da177e4SLinus Torvalds {
84e5dd1278SGreg Kroah-Hartman 	struct driver_private *drv_priv = to_driver(kobj);
85e5dd1278SGreg Kroah-Hartman 
862b3a302aSHarvey Harrison 	pr_debug("driver: '%s': %s\n", kobject_name(kobj), __func__);
87e5dd1278SGreg Kroah-Hartman 	kfree(drv_priv);
881da177e4SLinus Torvalds }
891da177e4SLinus Torvalds 
90a1148fb0SGreg Kroah-Hartman static struct kobj_type driver_ktype = {
911da177e4SLinus Torvalds 	.sysfs_ops	= &driver_sysfs_ops,
921da177e4SLinus Torvalds 	.release	= driver_release,
931da177e4SLinus Torvalds };
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds /*
961da177e4SLinus Torvalds  * sysfs bindings for buses
971da177e4SLinus Torvalds  */
984a3ad20cSGreg Kroah-Hartman static ssize_t bus_attr_show(struct kobject *kobj, struct attribute *attr,
994a3ad20cSGreg Kroah-Hartman 			     char *buf)
1001da177e4SLinus Torvalds {
1011da177e4SLinus Torvalds 	struct bus_attribute *bus_attr = to_bus_attr(attr);
1026b6e39a6SKay Sievers 	struct subsys_private *subsys_priv = to_subsys_private(kobj);
1031da177e4SLinus Torvalds 	ssize_t ret = 0;
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds 	if (bus_attr->show)
1066b6e39a6SKay Sievers 		ret = bus_attr->show(subsys_priv->bus, buf);
1071da177e4SLinus Torvalds 	return ret;
1081da177e4SLinus Torvalds }
1091da177e4SLinus Torvalds 
1104a3ad20cSGreg Kroah-Hartman static ssize_t bus_attr_store(struct kobject *kobj, struct attribute *attr,
1111da177e4SLinus Torvalds 			      const char *buf, size_t count)
1121da177e4SLinus Torvalds {
1131da177e4SLinus Torvalds 	struct bus_attribute *bus_attr = to_bus_attr(attr);
1146b6e39a6SKay Sievers 	struct subsys_private *subsys_priv = to_subsys_private(kobj);
1151da177e4SLinus Torvalds 	ssize_t ret = 0;
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds 	if (bus_attr->store)
1186b6e39a6SKay Sievers 		ret = bus_attr->store(subsys_priv->bus, buf, count);
1191da177e4SLinus Torvalds 	return ret;
1201da177e4SLinus Torvalds }
1211da177e4SLinus Torvalds 
12252cf25d0SEmese Revfy static const struct sysfs_ops bus_sysfs_ops = {
1231da177e4SLinus Torvalds 	.show	= bus_attr_show,
1241da177e4SLinus Torvalds 	.store	= bus_attr_store,
1251da177e4SLinus Torvalds };
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds int bus_create_file(struct bus_type *bus, struct bus_attribute *attr)
1281da177e4SLinus Torvalds {
1291da177e4SLinus Torvalds 	int error;
1305901d014SGreg Kroah-Hartman 	if (bus_get(bus)) {
131c6f7e72aSGreg Kroah-Hartman 		error = sysfs_create_file(&bus->p->subsys.kobj, &attr->attr);
132fc1ede58SGreg Kroah-Hartman 		bus_put(bus);
1331da177e4SLinus Torvalds 	} else
1341da177e4SLinus Torvalds 		error = -EINVAL;
1351da177e4SLinus Torvalds 	return error;
1361da177e4SLinus Torvalds }
1374a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_create_file);
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds void bus_remove_file(struct bus_type *bus, struct bus_attribute *attr)
1401da177e4SLinus Torvalds {
1415901d014SGreg Kroah-Hartman 	if (bus_get(bus)) {
142c6f7e72aSGreg Kroah-Hartman 		sysfs_remove_file(&bus->p->subsys.kobj, &attr->attr);
143fc1ede58SGreg Kroah-Hartman 		bus_put(bus);
1441da177e4SLinus Torvalds 	}
1451da177e4SLinus Torvalds }
1464a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_remove_file);
1471da177e4SLinus Torvalds 
14880f03e34SKay Sievers static struct kobj_type bus_ktype = {
1491da177e4SLinus Torvalds 	.sysfs_ops	= &bus_sysfs_ops,
1501da177e4SLinus Torvalds };
1511da177e4SLinus Torvalds 
15280f03e34SKay Sievers static int bus_uevent_filter(struct kset *kset, struct kobject *kobj)
15380f03e34SKay Sievers {
15480f03e34SKay Sievers 	struct kobj_type *ktype = get_ktype(kobj);
15580f03e34SKay Sievers 
15680f03e34SKay Sievers 	if (ktype == &bus_ktype)
15780f03e34SKay Sievers 		return 1;
15880f03e34SKay Sievers 	return 0;
15980f03e34SKay Sievers }
16080f03e34SKay Sievers 
1619cd43611SEmese Revfy static const struct kset_uevent_ops bus_uevent_ops = {
16280f03e34SKay Sievers 	.filter = bus_uevent_filter,
16380f03e34SKay Sievers };
16480f03e34SKay Sievers 
16559a54833SGreg Kroah-Hartman static struct kset *bus_kset;
1661da177e4SLinus Torvalds 
1672b08c8d0SAlan Stern /* Manually detach a device from its associated driver. */
168151ef38fSGreg Kroah-Hartman static ssize_t driver_unbind(struct device_driver *drv,
169151ef38fSGreg Kroah-Hartman 			     const char *buf, size_t count)
170151ef38fSGreg Kroah-Hartman {
1715901d014SGreg Kroah-Hartman 	struct bus_type *bus = bus_get(drv->bus);
172151ef38fSGreg Kroah-Hartman 	struct device *dev;
173151ef38fSGreg Kroah-Hartman 	int err = -ENODEV;
174151ef38fSGreg Kroah-Hartman 
1751f9ffc04SGreg Kroah-Hartman 	dev = bus_find_device_by_name(bus, NULL, buf);
1762b08c8d0SAlan Stern 	if (dev && dev->driver == drv) {
177bf74ad5bSAlan Stern 		if (dev->parent)	/* Needed for USB */
1788e9394ceSGreg Kroah-Hartman 			device_lock(dev->parent);
179151ef38fSGreg Kroah-Hartman 		device_release_driver(dev);
180bf74ad5bSAlan Stern 		if (dev->parent)
1818e9394ceSGreg Kroah-Hartman 			device_unlock(dev->parent);
182151ef38fSGreg Kroah-Hartman 		err = count;
183151ef38fSGreg Kroah-Hartman 	}
1842b08c8d0SAlan Stern 	put_device(dev);
185fc1ede58SGreg Kroah-Hartman 	bus_put(bus);
186151ef38fSGreg Kroah-Hartman 	return err;
187151ef38fSGreg Kroah-Hartman }
188151ef38fSGreg Kroah-Hartman static DRIVER_ATTR(unbind, S_IWUSR, NULL, driver_unbind);
189151ef38fSGreg Kroah-Hartman 
190afdce75fSGreg Kroah-Hartman /*
191afdce75fSGreg Kroah-Hartman  * Manually attach a device to a driver.
192afdce75fSGreg Kroah-Hartman  * Note: the driver must want to bind to the device,
193afdce75fSGreg Kroah-Hartman  * it is not possible to override the driver's id table.
194afdce75fSGreg Kroah-Hartman  */
195afdce75fSGreg Kroah-Hartman static ssize_t driver_bind(struct device_driver *drv,
196afdce75fSGreg Kroah-Hartman 			   const char *buf, size_t count)
197afdce75fSGreg Kroah-Hartman {
1985901d014SGreg Kroah-Hartman 	struct bus_type *bus = bus_get(drv->bus);
199afdce75fSGreg Kroah-Hartman 	struct device *dev;
200afdce75fSGreg Kroah-Hartman 	int err = -ENODEV;
201afdce75fSGreg Kroah-Hartman 
2021f9ffc04SGreg Kroah-Hartman 	dev = bus_find_device_by_name(bus, NULL, buf);
20349b420a1SMing Lei 	if (dev && dev->driver == NULL && driver_match_device(drv, dev)) {
204bf74ad5bSAlan Stern 		if (dev->parent)	/* Needed for USB */
2058e9394ceSGreg Kroah-Hartman 			device_lock(dev->parent);
2068e9394ceSGreg Kroah-Hartman 		device_lock(dev);
207afdce75fSGreg Kroah-Hartman 		err = driver_probe_device(drv, dev);
2088e9394ceSGreg Kroah-Hartman 		device_unlock(dev);
209bf74ad5bSAlan Stern 		if (dev->parent)
2108e9394ceSGreg Kroah-Hartman 			device_unlock(dev->parent);
21137225401SRyan Wilson 
2124a3ad20cSGreg Kroah-Hartman 		if (err > 0) {
2134a3ad20cSGreg Kroah-Hartman 			/* success */
21437225401SRyan Wilson 			err = count;
2154a3ad20cSGreg Kroah-Hartman 		} else if (err == 0) {
2164a3ad20cSGreg Kroah-Hartman 			/* driver didn't accept device */
21737225401SRyan Wilson 			err = -ENODEV;
218afdce75fSGreg Kroah-Hartman 		}
2194a3ad20cSGreg Kroah-Hartman 	}
2202b08c8d0SAlan Stern 	put_device(dev);
221fc1ede58SGreg Kroah-Hartman 	bus_put(bus);
222afdce75fSGreg Kroah-Hartman 	return err;
223afdce75fSGreg Kroah-Hartman }
224afdce75fSGreg Kroah-Hartman static DRIVER_ATTR(bind, S_IWUSR, NULL, driver_bind);
225afdce75fSGreg Kroah-Hartman 
226b8c5cec2SKay Sievers static ssize_t show_drivers_autoprobe(struct bus_type *bus, char *buf)
227b8c5cec2SKay Sievers {
228c6f7e72aSGreg Kroah-Hartman 	return sprintf(buf, "%d\n", bus->p->drivers_autoprobe);
229b8c5cec2SKay Sievers }
230b8c5cec2SKay Sievers 
231b8c5cec2SKay Sievers static ssize_t store_drivers_autoprobe(struct bus_type *bus,
232b8c5cec2SKay Sievers 				       const char *buf, size_t count)
233b8c5cec2SKay Sievers {
234b8c5cec2SKay Sievers 	if (buf[0] == '0')
235c6f7e72aSGreg Kroah-Hartman 		bus->p->drivers_autoprobe = 0;
236b8c5cec2SKay Sievers 	else
237c6f7e72aSGreg Kroah-Hartman 		bus->p->drivers_autoprobe = 1;
238b8c5cec2SKay Sievers 	return count;
239b8c5cec2SKay Sievers }
240b8c5cec2SKay Sievers 
241b8c5cec2SKay Sievers static ssize_t store_drivers_probe(struct bus_type *bus,
242b8c5cec2SKay Sievers 				   const char *buf, size_t count)
243b8c5cec2SKay Sievers {
244b8c5cec2SKay Sievers 	struct device *dev;
245b8c5cec2SKay Sievers 
2461f9ffc04SGreg Kroah-Hartman 	dev = bus_find_device_by_name(bus, NULL, buf);
247b8c5cec2SKay Sievers 	if (!dev)
248b8c5cec2SKay Sievers 		return -ENODEV;
249b8c5cec2SKay Sievers 	if (bus_rescan_devices_helper(dev, NULL) != 0)
250b8c5cec2SKay Sievers 		return -EINVAL;
251b8c5cec2SKay Sievers 	return count;
252b8c5cec2SKay Sievers }
253151ef38fSGreg Kroah-Hartman 
254465c7a3aSmochel@digitalimplant.org static struct device *next_device(struct klist_iter *i)
255465c7a3aSmochel@digitalimplant.org {
256465c7a3aSmochel@digitalimplant.org 	struct klist_node *n = klist_next(i);
257ae1b4171SGreg Kroah-Hartman 	struct device *dev = NULL;
258ae1b4171SGreg Kroah-Hartman 	struct device_private *dev_prv;
259ae1b4171SGreg Kroah-Hartman 
260ae1b4171SGreg Kroah-Hartman 	if (n) {
261ae1b4171SGreg Kroah-Hartman 		dev_prv = to_device_private_bus(n);
262ae1b4171SGreg Kroah-Hartman 		dev = dev_prv->device;
263ae1b4171SGreg Kroah-Hartman 	}
264ae1b4171SGreg Kroah-Hartman 	return dev;
265465c7a3aSmochel@digitalimplant.org }
266465c7a3aSmochel@digitalimplant.org 
2671da177e4SLinus Torvalds /**
2681da177e4SLinus Torvalds  * bus_for_each_dev - device iterator.
2691da177e4SLinus Torvalds  * @bus: bus type.
2701da177e4SLinus Torvalds  * @start: device to start iterating from.
2711da177e4SLinus Torvalds  * @data: data for the callback.
2721da177e4SLinus Torvalds  * @fn: function to be called for each device.
2731da177e4SLinus Torvalds  *
2741da177e4SLinus Torvalds  * Iterate over @bus's list of devices, and call @fn for each,
2751da177e4SLinus Torvalds  * passing it @data. If @start is not NULL, we use that device to
2761da177e4SLinus Torvalds  * begin iterating from.
2771da177e4SLinus Torvalds  *
2781da177e4SLinus Torvalds  * We check the return of @fn each time. If it returns anything
2791da177e4SLinus Torvalds  * other than 0, we break out and return that value.
2801da177e4SLinus Torvalds  *
2811da177e4SLinus Torvalds  * NOTE: The device that returns a non-zero value is not retained
2821da177e4SLinus Torvalds  * in any way, nor is its refcount incremented. If the caller needs
2830fa1b0a1SAlex Chiang  * to retain this data, it should do so, and increment the reference
2841da177e4SLinus Torvalds  * count in the supplied callback.
2851da177e4SLinus Torvalds  */
2861da177e4SLinus Torvalds int bus_for_each_dev(struct bus_type *bus, struct device *start,
2871da177e4SLinus Torvalds 		     void *data, int (*fn)(struct device *, void *))
2881da177e4SLinus Torvalds {
289465c7a3aSmochel@digitalimplant.org 	struct klist_iter i;
290465c7a3aSmochel@digitalimplant.org 	struct device *dev;
291465c7a3aSmochel@digitalimplant.org 	int error = 0;
2921da177e4SLinus Torvalds 
293*4fa3e78bSBjorn Helgaas 	if (!bus || !bus->p)
294465c7a3aSmochel@digitalimplant.org 		return -EINVAL;
295465c7a3aSmochel@digitalimplant.org 
2967cd9c9bbSGreg Kroah-Hartman 	klist_iter_init_node(&bus->p->klist_devices, &i,
297ae1b4171SGreg Kroah-Hartman 			     (start ? &start->p->knode_bus : NULL));
298465c7a3aSmochel@digitalimplant.org 	while ((dev = next_device(&i)) && !error)
299465c7a3aSmochel@digitalimplant.org 		error = fn(dev, data);
300465c7a3aSmochel@digitalimplant.org 	klist_iter_exit(&i);
301465c7a3aSmochel@digitalimplant.org 	return error;
3021da177e4SLinus Torvalds }
3034a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_for_each_dev);
3041da177e4SLinus Torvalds 
3050edb5860SCornelia Huck /**
3060edb5860SCornelia Huck  * bus_find_device - device iterator for locating a particular device.
3070edb5860SCornelia Huck  * @bus: bus type
3080edb5860SCornelia Huck  * @start: Device to begin with
3090edb5860SCornelia Huck  * @data: Data to pass to match function
3100edb5860SCornelia Huck  * @match: Callback function to check device
3110edb5860SCornelia Huck  *
3120edb5860SCornelia Huck  * This is similar to the bus_for_each_dev() function above, but it
3130edb5860SCornelia Huck  * returns a reference to a device that is 'found' for later use, as
3140edb5860SCornelia Huck  * determined by the @match callback.
3150edb5860SCornelia Huck  *
3160edb5860SCornelia Huck  * The callback should return 0 if the device doesn't match and non-zero
3170edb5860SCornelia Huck  * if it does.  If the callback returns non-zero, this function will
3180edb5860SCornelia Huck  * return to the caller and not iterate over any more devices.
3190edb5860SCornelia Huck  */
3200edb5860SCornelia Huck struct device *bus_find_device(struct bus_type *bus,
3210edb5860SCornelia Huck 			       struct device *start, void *data,
3224a3ad20cSGreg Kroah-Hartman 			       int (*match)(struct device *dev, void *data))
3230edb5860SCornelia Huck {
3240edb5860SCornelia Huck 	struct klist_iter i;
3250edb5860SCornelia Huck 	struct device *dev;
3260edb5860SCornelia Huck 
327*4fa3e78bSBjorn Helgaas 	if (!bus || !bus->p)
3280edb5860SCornelia Huck 		return NULL;
3290edb5860SCornelia Huck 
3307cd9c9bbSGreg Kroah-Hartman 	klist_iter_init_node(&bus->p->klist_devices, &i,
3317cd9c9bbSGreg Kroah-Hartman 			     (start ? &start->p->knode_bus : NULL));
3320edb5860SCornelia Huck 	while ((dev = next_device(&i)))
3330edb5860SCornelia Huck 		if (match(dev, data) && get_device(dev))
3340edb5860SCornelia Huck 			break;
3350edb5860SCornelia Huck 	klist_iter_exit(&i);
3360edb5860SCornelia Huck 	return dev;
3370edb5860SCornelia Huck }
3384a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_find_device);
33938fdac3cSmochel@digitalimplant.org 
3401f9ffc04SGreg Kroah-Hartman static int match_name(struct device *dev, void *data)
3411f9ffc04SGreg Kroah-Hartman {
3421f9ffc04SGreg Kroah-Hartman 	const char *name = data;
3431f9ffc04SGreg Kroah-Hartman 
3441e0b2cf9SKay Sievers 	return sysfs_streq(name, dev_name(dev));
3451f9ffc04SGreg Kroah-Hartman }
3461f9ffc04SGreg Kroah-Hartman 
3471f9ffc04SGreg Kroah-Hartman /**
3481f9ffc04SGreg Kroah-Hartman  * bus_find_device_by_name - device iterator for locating a particular device of a specific name
3491f9ffc04SGreg Kroah-Hartman  * @bus: bus type
3501f9ffc04SGreg Kroah-Hartman  * @start: Device to begin with
3511f9ffc04SGreg Kroah-Hartman  * @name: name of the device to match
3521f9ffc04SGreg Kroah-Hartman  *
3531f9ffc04SGreg Kroah-Hartman  * This is similar to the bus_find_device() function above, but it handles
3541f9ffc04SGreg Kroah-Hartman  * searching by a name automatically, no need to write another strcmp matching
3551f9ffc04SGreg Kroah-Hartman  * function.
3561f9ffc04SGreg Kroah-Hartman  */
3571f9ffc04SGreg Kroah-Hartman struct device *bus_find_device_by_name(struct bus_type *bus,
3581f9ffc04SGreg Kroah-Hartman 				       struct device *start, const char *name)
3591f9ffc04SGreg Kroah-Hartman {
3601f9ffc04SGreg Kroah-Hartman 	return bus_find_device(bus, start, (void *)name, match_name);
3611f9ffc04SGreg Kroah-Hartman }
3621f9ffc04SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_find_device_by_name);
3631f9ffc04SGreg Kroah-Hartman 
364ca22e56dSKay Sievers /**
365ca22e56dSKay Sievers  * subsys_find_device_by_id - find a device with a specific enumeration number
366ca22e56dSKay Sievers  * @subsys: subsystem
367ca22e56dSKay Sievers  * @id: index 'id' in struct device
368ca22e56dSKay Sievers  * @hint: device to check first
369ca22e56dSKay Sievers  *
370ca22e56dSKay Sievers  * Check the hint's next object and if it is a match return it directly,
371ca22e56dSKay Sievers  * otherwise, fall back to a full list search. Either way a reference for
372ca22e56dSKay Sievers  * the returned object is taken.
373ca22e56dSKay Sievers  */
374ca22e56dSKay Sievers struct device *subsys_find_device_by_id(struct bus_type *subsys, unsigned int id,
375ca22e56dSKay Sievers 					struct device *hint)
376ca22e56dSKay Sievers {
377ca22e56dSKay Sievers 	struct klist_iter i;
378ca22e56dSKay Sievers 	struct device *dev;
379ca22e56dSKay Sievers 
380ca22e56dSKay Sievers 	if (!subsys)
381ca22e56dSKay Sievers 		return NULL;
382ca22e56dSKay Sievers 
383ca22e56dSKay Sievers 	if (hint) {
3847cd9c9bbSGreg Kroah-Hartman 		klist_iter_init_node(&subsys->p->klist_devices, &i, &hint->p->knode_bus);
385ca22e56dSKay Sievers 		dev = next_device(&i);
386ca22e56dSKay Sievers 		if (dev && dev->id == id && get_device(dev)) {
387ca22e56dSKay Sievers 			klist_iter_exit(&i);
388ca22e56dSKay Sievers 			return dev;
389ca22e56dSKay Sievers 		}
390ca22e56dSKay Sievers 		klist_iter_exit(&i);
391ca22e56dSKay Sievers 	}
392ca22e56dSKay Sievers 
393ca22e56dSKay Sievers 	klist_iter_init_node(&subsys->p->klist_devices, &i, NULL);
394ca22e56dSKay Sievers 	while ((dev = next_device(&i))) {
395ca22e56dSKay Sievers 		if (dev->id == id && get_device(dev)) {
396ca22e56dSKay Sievers 			klist_iter_exit(&i);
397ca22e56dSKay Sievers 			return dev;
398ca22e56dSKay Sievers 		}
399ca22e56dSKay Sievers 	}
400ca22e56dSKay Sievers 	klist_iter_exit(&i);
401ca22e56dSKay Sievers 	return NULL;
402ca22e56dSKay Sievers }
403ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_find_device_by_id);
404ca22e56dSKay Sievers 
40538fdac3cSmochel@digitalimplant.org static struct device_driver *next_driver(struct klist_iter *i)
40638fdac3cSmochel@digitalimplant.org {
40738fdac3cSmochel@digitalimplant.org 	struct klist_node *n = klist_next(i);
408e5dd1278SGreg Kroah-Hartman 	struct driver_private *drv_priv;
409e5dd1278SGreg Kroah-Hartman 
410e5dd1278SGreg Kroah-Hartman 	if (n) {
411e5dd1278SGreg Kroah-Hartman 		drv_priv = container_of(n, struct driver_private, knode_bus);
412e5dd1278SGreg Kroah-Hartman 		return drv_priv->driver;
413e5dd1278SGreg Kroah-Hartman 	}
414e5dd1278SGreg Kroah-Hartman 	return NULL;
41538fdac3cSmochel@digitalimplant.org }
41638fdac3cSmochel@digitalimplant.org 
4171da177e4SLinus Torvalds /**
4181da177e4SLinus Torvalds  * bus_for_each_drv - driver iterator
4191da177e4SLinus Torvalds  * @bus: bus we're dealing with.
4201da177e4SLinus Torvalds  * @start: driver to start iterating on.
4211da177e4SLinus Torvalds  * @data: data to pass to the callback.
4221da177e4SLinus Torvalds  * @fn: function to call for each driver.
4231da177e4SLinus Torvalds  *
4241da177e4SLinus Torvalds  * This is nearly identical to the device iterator above.
4251da177e4SLinus Torvalds  * We iterate over each driver that belongs to @bus, and call
4261da177e4SLinus Torvalds  * @fn for each. If @fn returns anything but 0, we break out
4271da177e4SLinus Torvalds  * and return it. If @start is not NULL, we use it as the head
4281da177e4SLinus Torvalds  * of the list.
4291da177e4SLinus Torvalds  *
4301da177e4SLinus Torvalds  * NOTE: we don't return the driver that returns a non-zero
4311da177e4SLinus Torvalds  * value, nor do we leave the reference count incremented for that
4321da177e4SLinus Torvalds  * driver. If the caller needs to know that info, it must set it
4331da177e4SLinus Torvalds  * in the callback. It must also be sure to increment the refcount
4341da177e4SLinus Torvalds  * so it doesn't disappear before returning to the caller.
4351da177e4SLinus Torvalds  */
4361da177e4SLinus Torvalds int bus_for_each_drv(struct bus_type *bus, struct device_driver *start,
4371da177e4SLinus Torvalds 		     void *data, int (*fn)(struct device_driver *, void *))
4381da177e4SLinus Torvalds {
43938fdac3cSmochel@digitalimplant.org 	struct klist_iter i;
44038fdac3cSmochel@digitalimplant.org 	struct device_driver *drv;
44138fdac3cSmochel@digitalimplant.org 	int error = 0;
4421da177e4SLinus Torvalds 
44338fdac3cSmochel@digitalimplant.org 	if (!bus)
44438fdac3cSmochel@digitalimplant.org 		return -EINVAL;
44538fdac3cSmochel@digitalimplant.org 
4467cd9c9bbSGreg Kroah-Hartman 	klist_iter_init_node(&bus->p->klist_drivers, &i,
447e5dd1278SGreg Kroah-Hartman 			     start ? &start->p->knode_bus : NULL);
44838fdac3cSmochel@digitalimplant.org 	while ((drv = next_driver(&i)) && !error)
44938fdac3cSmochel@digitalimplant.org 		error = fn(drv, data);
45038fdac3cSmochel@digitalimplant.org 	klist_iter_exit(&i);
45138fdac3cSmochel@digitalimplant.org 	return error;
4521da177e4SLinus Torvalds }
4534a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_for_each_drv);
4541da177e4SLinus Torvalds 
4551da177e4SLinus Torvalds static int device_add_attrs(struct bus_type *bus, struct device *dev)
4561da177e4SLinus Torvalds {
4571da177e4SLinus Torvalds 	int error = 0;
4581da177e4SLinus Torvalds 	int i;
4591da177e4SLinus Torvalds 
4604aca67e5SAndrew Morton 	if (!bus->dev_attrs)
4614aca67e5SAndrew Morton 		return 0;
4624aca67e5SAndrew Morton 
4631da177e4SLinus Torvalds 	for (i = 0; attr_name(bus->dev_attrs[i]); i++) {
4641da177e4SLinus Torvalds 		error = device_create_file(dev, &bus->dev_attrs[i]);
4654aca67e5SAndrew Morton 		if (error) {
4661da177e4SLinus Torvalds 			while (--i >= 0)
4671da177e4SLinus Torvalds 				device_remove_file(dev, &bus->dev_attrs[i]);
4684aca67e5SAndrew Morton 			break;
4691da177e4SLinus Torvalds 		}
4704aca67e5SAndrew Morton 	}
4714aca67e5SAndrew Morton 	return error;
4724aca67e5SAndrew Morton }
4731da177e4SLinus Torvalds 
4741da177e4SLinus Torvalds static void device_remove_attrs(struct bus_type *bus, struct device *dev)
4751da177e4SLinus Torvalds {
4761da177e4SLinus Torvalds 	int i;
4771da177e4SLinus Torvalds 
4781da177e4SLinus Torvalds 	if (bus->dev_attrs) {
4791da177e4SLinus Torvalds 		for (i = 0; attr_name(bus->dev_attrs[i]); i++)
4801da177e4SLinus Torvalds 			device_remove_file(dev, &bus->dev_attrs[i]);
4811da177e4SLinus Torvalds 	}
4821da177e4SLinus Torvalds }
4831da177e4SLinus Torvalds 
4841da177e4SLinus Torvalds /**
4851da177e4SLinus Torvalds  * bus_add_device - add device to bus
4861da177e4SLinus Torvalds  * @dev: device being added
4871da177e4SLinus Torvalds  *
4882023c610SAlan Stern  * - Add device's bus attributes.
4892023c610SAlan Stern  * - Create links to device's bus.
4901da177e4SLinus Torvalds  * - Add the device to its bus's list of devices.
4911da177e4SLinus Torvalds  */
4921da177e4SLinus Torvalds int bus_add_device(struct device *dev)
4931da177e4SLinus Torvalds {
4945901d014SGreg Kroah-Hartman 	struct bus_type *bus = bus_get(dev->bus);
4951da177e4SLinus Torvalds 	int error = 0;
4961da177e4SLinus Torvalds 
4971da177e4SLinus Torvalds 	if (bus) {
4981e0b2cf9SKay Sievers 		pr_debug("bus: '%s': add device %s\n", bus->name, dev_name(dev));
499ca2b94baSHannes Reinecke 		error = device_add_attrs(bus, dev);
500f86db396SAndrew Morton 		if (error)
501513e7337SCornelia Huck 			goto out_put;
502c6f7e72aSGreg Kroah-Hartman 		error = sysfs_create_link(&bus->p->devices_kset->kobj,
5031e0b2cf9SKay Sievers 						&dev->kobj, dev_name(dev));
504f86db396SAndrew Morton 		if (error)
505513e7337SCornelia Huck 			goto out_id;
506f86db396SAndrew Morton 		error = sysfs_create_link(&dev->kobj,
507c6f7e72aSGreg Kroah-Hartman 				&dev->bus->p->subsys.kobj, "subsystem");
508f86db396SAndrew Morton 		if (error)
509513e7337SCornelia Huck 			goto out_subsys;
5102023c610SAlan Stern 		klist_add_tail(&dev->p->knode_bus, &bus->p->klist_devices);
5111da177e4SLinus Torvalds 	}
512513e7337SCornelia Huck 	return 0;
513513e7337SCornelia Huck 
514513e7337SCornelia Huck out_subsys:
5151e0b2cf9SKay Sievers 	sysfs_remove_link(&bus->p->devices_kset->kobj, dev_name(dev));
516513e7337SCornelia Huck out_id:
517513e7337SCornelia Huck 	device_remove_attrs(bus, dev);
518513e7337SCornelia Huck out_put:
519fc1ede58SGreg Kroah-Hartman 	bus_put(dev->bus);
5201da177e4SLinus Torvalds 	return error;
5211da177e4SLinus Torvalds }
5221da177e4SLinus Torvalds 
5231da177e4SLinus Torvalds /**
5242023c610SAlan Stern  * bus_probe_device - probe drivers for a new device
5252023c610SAlan Stern  * @dev: device to probe
52653877d06SKay Sievers  *
5272023c610SAlan Stern  * - Automatically probe for a driver if the bus allows it.
52853877d06SKay Sievers  */
5292023c610SAlan Stern void bus_probe_device(struct device *dev)
53053877d06SKay Sievers {
53153877d06SKay Sievers 	struct bus_type *bus = dev->bus;
532ca22e56dSKay Sievers 	struct subsys_interface *sif;
5332023c610SAlan Stern 	int ret;
53453877d06SKay Sievers 
535ca22e56dSKay Sievers 	if (!bus)
536ca22e56dSKay Sievers 		return;
537ca22e56dSKay Sievers 
538ca22e56dSKay Sievers 	if (bus->p->drivers_autoprobe) {
539f86db396SAndrew Morton 		ret = device_attach(dev);
540c6a46696SCornelia Huck 		WARN_ON(ret < 0);
54153877d06SKay Sievers 	}
542ca22e56dSKay Sievers 
543ca22e56dSKay Sievers 	mutex_lock(&bus->p->mutex);
544ca22e56dSKay Sievers 	list_for_each_entry(sif, &bus->p->interfaces, node)
545ca22e56dSKay Sievers 		if (sif->add_dev)
546ca22e56dSKay Sievers 			sif->add_dev(dev, sif);
547ca22e56dSKay Sievers 	mutex_unlock(&bus->p->mutex);
548f86db396SAndrew Morton }
54953877d06SKay Sievers 
55053877d06SKay Sievers /**
5511da177e4SLinus Torvalds  * bus_remove_device - remove device from bus
5521da177e4SLinus Torvalds  * @dev: device to be removed
5531da177e4SLinus Torvalds  *
554ca22e56dSKay Sievers  * - Remove device from all interfaces.
555ca22e56dSKay Sievers  * - Remove symlink from bus' directory.
5561da177e4SLinus Torvalds  * - Delete device from bus's list.
5571da177e4SLinus Torvalds  * - Detach from its driver.
5581da177e4SLinus Torvalds  * - Drop reference taken in bus_add_device().
5591da177e4SLinus Torvalds  */
5601da177e4SLinus Torvalds void bus_remove_device(struct device *dev)
5611da177e4SLinus Torvalds {
562ca22e56dSKay Sievers 	struct bus_type *bus = dev->bus;
563ca22e56dSKay Sievers 	struct subsys_interface *sif;
564ca22e56dSKay Sievers 
565ca22e56dSKay Sievers 	if (!bus)
566ca22e56dSKay Sievers 		return;
567ca22e56dSKay Sievers 
568ca22e56dSKay Sievers 	mutex_lock(&bus->p->mutex);
569ca22e56dSKay Sievers 	list_for_each_entry(sif, &bus->p->interfaces, node)
570ca22e56dSKay Sievers 		if (sif->remove_dev)
571ca22e56dSKay Sievers 			sif->remove_dev(dev, sif);
572ca22e56dSKay Sievers 	mutex_unlock(&bus->p->mutex);
573ca22e56dSKay Sievers 
574b9d9c82bSKay Sievers 	sysfs_remove_link(&dev->kobj, "subsystem");
5754a3ad20cSGreg Kroah-Hartman 	sysfs_remove_link(&dev->bus->p->devices_kset->kobj,
5761e0b2cf9SKay Sievers 			  dev_name(dev));
5771da177e4SLinus Torvalds 	device_remove_attrs(dev->bus, dev);
578ae1b4171SGreg Kroah-Hartman 	if (klist_node_attached(&dev->p->knode_bus))
579ae1b4171SGreg Kroah-Hartman 		klist_del(&dev->p->knode_bus);
5803f62e570SGreg Kroah-Hartman 
5814a3ad20cSGreg Kroah-Hartman 	pr_debug("bus: '%s': remove device %s\n",
5821e0b2cf9SKay Sievers 		 dev->bus->name, dev_name(dev));
5831da177e4SLinus Torvalds 	device_release_driver(dev);
584fc1ede58SGreg Kroah-Hartman 	bus_put(dev->bus);
5851da177e4SLinus Torvalds }
5861da177e4SLinus Torvalds 
5871da177e4SLinus Torvalds static int driver_add_attrs(struct bus_type *bus, struct device_driver *drv)
5881da177e4SLinus Torvalds {
5891da177e4SLinus Torvalds 	int error = 0;
5901da177e4SLinus Torvalds 	int i;
5911da177e4SLinus Torvalds 
5921da177e4SLinus Torvalds 	if (bus->drv_attrs) {
5931da177e4SLinus Torvalds 		for (i = 0; attr_name(bus->drv_attrs[i]); i++) {
5941da177e4SLinus Torvalds 			error = driver_create_file(drv, &bus->drv_attrs[i]);
5951da177e4SLinus Torvalds 			if (error)
5964a3ad20cSGreg Kroah-Hartman 				goto err;
5971da177e4SLinus Torvalds 		}
5981da177e4SLinus Torvalds 	}
5994a3ad20cSGreg Kroah-Hartman done:
6001da177e4SLinus Torvalds 	return error;
6014a3ad20cSGreg Kroah-Hartman err:
6021da177e4SLinus Torvalds 	while (--i >= 0)
6031da177e4SLinus Torvalds 		driver_remove_file(drv, &bus->drv_attrs[i]);
6044a3ad20cSGreg Kroah-Hartman 	goto done;
6051da177e4SLinus Torvalds }
6061da177e4SLinus Torvalds 
6074a3ad20cSGreg Kroah-Hartman static void driver_remove_attrs(struct bus_type *bus,
6084a3ad20cSGreg Kroah-Hartman 				struct device_driver *drv)
6091da177e4SLinus Torvalds {
6101da177e4SLinus Torvalds 	int i;
6111da177e4SLinus Torvalds 
6121da177e4SLinus Torvalds 	if (bus->drv_attrs) {
6131da177e4SLinus Torvalds 		for (i = 0; attr_name(bus->drv_attrs[i]); i++)
6141da177e4SLinus Torvalds 			driver_remove_file(drv, &bus->drv_attrs[i]);
6151da177e4SLinus Torvalds 	}
6161da177e4SLinus Torvalds }
6171da177e4SLinus Torvalds 
618f86db396SAndrew Morton static int __must_check add_bind_files(struct device_driver *drv)
619874c6241SGreg Kroah-Hartman {
620f86db396SAndrew Morton 	int ret;
621f86db396SAndrew Morton 
622f86db396SAndrew Morton 	ret = driver_create_file(drv, &driver_attr_unbind);
623f86db396SAndrew Morton 	if (ret == 0) {
624f86db396SAndrew Morton 		ret = driver_create_file(drv, &driver_attr_bind);
625f86db396SAndrew Morton 		if (ret)
626f86db396SAndrew Morton 			driver_remove_file(drv, &driver_attr_unbind);
627f86db396SAndrew Morton 	}
628f86db396SAndrew Morton 	return ret;
629874c6241SGreg Kroah-Hartman }
630874c6241SGreg Kroah-Hartman 
631874c6241SGreg Kroah-Hartman static void remove_bind_files(struct device_driver *drv)
632874c6241SGreg Kroah-Hartman {
633874c6241SGreg Kroah-Hartman 	driver_remove_file(drv, &driver_attr_bind);
634874c6241SGreg Kroah-Hartman 	driver_remove_file(drv, &driver_attr_unbind);
635874c6241SGreg Kroah-Hartman }
636b8c5cec2SKay Sievers 
6378380770cSKay Sievers static BUS_ATTR(drivers_probe, S_IWUSR, NULL, store_drivers_probe);
6388380770cSKay Sievers static BUS_ATTR(drivers_autoprobe, S_IWUSR | S_IRUGO,
6398380770cSKay Sievers 		show_drivers_autoprobe, store_drivers_autoprobe);
6408380770cSKay Sievers 
641b8c5cec2SKay Sievers static int add_probe_files(struct bus_type *bus)
642b8c5cec2SKay Sievers {
643b8c5cec2SKay Sievers 	int retval;
644b8c5cec2SKay Sievers 
6458380770cSKay Sievers 	retval = bus_create_file(bus, &bus_attr_drivers_probe);
646b8c5cec2SKay Sievers 	if (retval)
647b8c5cec2SKay Sievers 		goto out;
648b8c5cec2SKay Sievers 
6498380770cSKay Sievers 	retval = bus_create_file(bus, &bus_attr_drivers_autoprobe);
650b8c5cec2SKay Sievers 	if (retval)
6518380770cSKay Sievers 		bus_remove_file(bus, &bus_attr_drivers_probe);
652b8c5cec2SKay Sievers out:
653b8c5cec2SKay Sievers 	return retval;
654b8c5cec2SKay Sievers }
655b8c5cec2SKay Sievers 
656b8c5cec2SKay Sievers static void remove_probe_files(struct bus_type *bus)
657b8c5cec2SKay Sievers {
6588380770cSKay Sievers 	bus_remove_file(bus, &bus_attr_drivers_autoprobe);
6598380770cSKay Sievers 	bus_remove_file(bus, &bus_attr_drivers_probe);
660b8c5cec2SKay Sievers }
6611da177e4SLinus Torvalds 
6627ac1cf4aSKay Sievers static ssize_t driver_uevent_store(struct device_driver *drv,
6637ac1cf4aSKay Sievers 				   const char *buf, size_t count)
6647ac1cf4aSKay Sievers {
6657ac1cf4aSKay Sievers 	enum kobject_action action;
6667ac1cf4aSKay Sievers 
6677ac1cf4aSKay Sievers 	if (kobject_action_type(buf, count, &action) == 0)
668e5dd1278SGreg Kroah-Hartman 		kobject_uevent(&drv->p->kobj, action);
6697ac1cf4aSKay Sievers 	return count;
6707ac1cf4aSKay Sievers }
6717ac1cf4aSKay Sievers static DRIVER_ATTR(uevent, S_IWUSR, NULL, driver_uevent_store);
6727ac1cf4aSKay Sievers 
6731da177e4SLinus Torvalds /**
6741da177e4SLinus Torvalds  * bus_add_driver - Add a driver to the bus.
6751da177e4SLinus Torvalds  * @drv: driver.
6761da177e4SLinus Torvalds  */
6771da177e4SLinus Torvalds int bus_add_driver(struct device_driver *drv)
6781da177e4SLinus Torvalds {
679e5dd1278SGreg Kroah-Hartman 	struct bus_type *bus;
680e5dd1278SGreg Kroah-Hartman 	struct driver_private *priv;
6811da177e4SLinus Torvalds 	int error = 0;
6821da177e4SLinus Torvalds 
683e5dd1278SGreg Kroah-Hartman 	bus = bus_get(drv->bus);
684d9fd4d3bSJeff Garzik 	if (!bus)
6854f6e1945SGreg Kroah-Hartman 		return -EINVAL;
686d9fd4d3bSJeff Garzik 
6877dc72b28SGreg Kroah-Hartman 	pr_debug("bus: '%s': add driver %s\n", bus->name, drv->name);
688e5dd1278SGreg Kroah-Hartman 
689e5dd1278SGreg Kroah-Hartman 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
69007634464SCornelia Huck 	if (!priv) {
69107634464SCornelia Huck 		error = -ENOMEM;
69207634464SCornelia Huck 		goto out_put_bus;
69307634464SCornelia Huck 	}
694e5dd1278SGreg Kroah-Hartman 	klist_init(&priv->klist_devices, NULL, NULL);
695e5dd1278SGreg Kroah-Hartman 	priv->driver = drv;
696e5dd1278SGreg Kroah-Hartman 	drv->p = priv;
697c8e90d82SGreg Kroah-Hartman 	priv->kobj.kset = bus->p->drivers_kset;
698c8e90d82SGreg Kroah-Hartman 	error = kobject_init_and_add(&priv->kobj, &driver_ktype, NULL,
699c8e90d82SGreg Kroah-Hartman 				     "%s", drv->name);
700dc0afa83SCornelia Huck 	if (error)
70107634464SCornelia Huck 		goto out_unregister;
7021da177e4SLinus Torvalds 
703190888acSMing Lei 	klist_add_tail(&priv->knode_bus, &bus->p->klist_drivers);
704c6f7e72aSGreg Kroah-Hartman 	if (drv->bus->p->drivers_autoprobe) {
705f86db396SAndrew Morton 		error = driver_attach(drv);
706f86db396SAndrew Morton 		if (error)
707f86db396SAndrew Morton 			goto out_unregister;
708b8c5cec2SKay Sievers 	}
7091da177e4SLinus Torvalds 	module_add_driver(drv->owner, drv);
7101da177e4SLinus Torvalds 
7117ac1cf4aSKay Sievers 	error = driver_create_file(drv, &driver_attr_uevent);
7127ac1cf4aSKay Sievers 	if (error) {
7137ac1cf4aSKay Sievers 		printk(KERN_ERR "%s: uevent attr (%s) failed\n",
7142b3a302aSHarvey Harrison 			__func__, drv->name);
7157ac1cf4aSKay Sievers 	}
716f86db396SAndrew Morton 	error = driver_add_attrs(bus, drv);
717f86db396SAndrew Morton 	if (error) {
718f86db396SAndrew Morton 		/* How the hell do we get out of this pickle? Give up */
719f86db396SAndrew Morton 		printk(KERN_ERR "%s: driver_add_attrs(%s) failed\n",
7202b3a302aSHarvey Harrison 			__func__, drv->name);
721f86db396SAndrew Morton 	}
7221a6f2a75SDmitry Torokhov 
7231a6f2a75SDmitry Torokhov 	if (!drv->suppress_bind_attrs) {
724f86db396SAndrew Morton 		error = add_bind_files(drv);
725f86db396SAndrew Morton 		if (error) {
726f86db396SAndrew Morton 			/* Ditto */
727f86db396SAndrew Morton 			printk(KERN_ERR "%s: add_bind_files(%s) failed\n",
7282b3a302aSHarvey Harrison 				__func__, drv->name);
729f86db396SAndrew Morton 		}
7301a6f2a75SDmitry Torokhov 	}
731d9fd4d3bSJeff Garzik 
7325c8563d7SKay Sievers 	return 0;
7331a6f2a75SDmitry Torokhov 
734f86db396SAndrew Morton out_unregister:
73599b28f1bSPhil Carmody 	kobject_put(&priv->kobj);
7365c8563d7SKay Sievers 	kfree(drv->p);
7375c8563d7SKay Sievers 	drv->p = NULL;
738f86db396SAndrew Morton out_put_bus:
739fc1ede58SGreg Kroah-Hartman 	bus_put(bus);
740f86db396SAndrew Morton 	return error;
7411da177e4SLinus Torvalds }
7421da177e4SLinus Torvalds 
7431da177e4SLinus Torvalds /**
7441da177e4SLinus Torvalds  * bus_remove_driver - delete driver from bus's knowledge.
7451da177e4SLinus Torvalds  * @drv: driver.
7461da177e4SLinus Torvalds  *
7471da177e4SLinus Torvalds  * Detach the driver from the devices it controls, and remove
7481da177e4SLinus Torvalds  * it from its bus's list of drivers. Finally, we drop the reference
7491da177e4SLinus Torvalds  * to the bus we took in bus_add_driver().
7501da177e4SLinus Torvalds  */
7511da177e4SLinus Torvalds void bus_remove_driver(struct device_driver *drv)
7521da177e4SLinus Torvalds {
753d9fd4d3bSJeff Garzik 	if (!drv->bus)
754d9fd4d3bSJeff Garzik 		return;
755d9fd4d3bSJeff Garzik 
7561a6f2a75SDmitry Torokhov 	if (!drv->suppress_bind_attrs)
757874c6241SGreg Kroah-Hartman 		remove_bind_files(drv);
7581da177e4SLinus Torvalds 	driver_remove_attrs(drv->bus, drv);
7597ac1cf4aSKay Sievers 	driver_remove_file(drv, &driver_attr_uevent);
760e5dd1278SGreg Kroah-Hartman 	klist_remove(&drv->p->knode_bus);
7617dc72b28SGreg Kroah-Hartman 	pr_debug("bus: '%s': remove driver %s\n", drv->bus->name, drv->name);
7621da177e4SLinus Torvalds 	driver_detach(drv);
7631da177e4SLinus Torvalds 	module_remove_driver(drv);
764c10997f6SGreg Kroah-Hartman 	kobject_put(&drv->p->kobj);
765fc1ede58SGreg Kroah-Hartman 	bus_put(drv->bus);
7661da177e4SLinus Torvalds }
7671da177e4SLinus Torvalds 
7681da177e4SLinus Torvalds /* Helper for bus_rescan_devices's iter */
769f86db396SAndrew Morton static int __must_check bus_rescan_devices_helper(struct device *dev,
770f86db396SAndrew Morton 						  void *data)
7711da177e4SLinus Torvalds {
772f86db396SAndrew Morton 	int ret = 0;
773f86db396SAndrew Morton 
774bf74ad5bSAlan Stern 	if (!dev->driver) {
775bf74ad5bSAlan Stern 		if (dev->parent)	/* Needed for USB */
7768e9394ceSGreg Kroah-Hartman 			device_lock(dev->parent);
777f86db396SAndrew Morton 		ret = device_attach(dev);
778bf74ad5bSAlan Stern 		if (dev->parent)
7798e9394ceSGreg Kroah-Hartman 			device_unlock(dev->parent);
780bf74ad5bSAlan Stern 	}
781f86db396SAndrew Morton 	return ret < 0 ? ret : 0;
7821da177e4SLinus Torvalds }
7831da177e4SLinus Torvalds 
7841da177e4SLinus Torvalds /**
7851da177e4SLinus Torvalds  * bus_rescan_devices - rescan devices on the bus for possible drivers
7861da177e4SLinus Torvalds  * @bus: the bus to scan.
7871da177e4SLinus Torvalds  *
7881da177e4SLinus Torvalds  * This function will look for devices on the bus with no driver
78923d3d602SGreg Kroah-Hartman  * attached and rescan it against existing drivers to see if it matches
79023d3d602SGreg Kroah-Hartman  * any by calling device_attach() for the unbound devices.
7911da177e4SLinus Torvalds  */
792f86db396SAndrew Morton int bus_rescan_devices(struct bus_type *bus)
7931da177e4SLinus Torvalds {
794f86db396SAndrew Morton 	return bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper);
7951da177e4SLinus Torvalds }
7964a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_rescan_devices);
7971da177e4SLinus Torvalds 
798e935d5daSMoore, Eric /**
799e935d5daSMoore, Eric  * device_reprobe - remove driver for a device and probe for a new driver
800e935d5daSMoore, Eric  * @dev: the device to reprobe
801e935d5daSMoore, Eric  *
802e935d5daSMoore, Eric  * This function detaches the attached driver (if any) for the given
803e935d5daSMoore, Eric  * device and restarts the driver probing process.  It is intended
804e935d5daSMoore, Eric  * to use if probing criteria changed during a devices lifetime and
805e935d5daSMoore, Eric  * driver attachment should change accordingly.
806e935d5daSMoore, Eric  */
807f86db396SAndrew Morton int device_reprobe(struct device *dev)
808e935d5daSMoore, Eric {
809e935d5daSMoore, Eric 	if (dev->driver) {
810e935d5daSMoore, Eric 		if (dev->parent)        /* Needed for USB */
8118e9394ceSGreg Kroah-Hartman 			device_lock(dev->parent);
812e935d5daSMoore, Eric 		device_release_driver(dev);
813e935d5daSMoore, Eric 		if (dev->parent)
8148e9394ceSGreg Kroah-Hartman 			device_unlock(dev->parent);
815e935d5daSMoore, Eric 	}
816f86db396SAndrew Morton 	return bus_rescan_devices_helper(dev, NULL);
817e935d5daSMoore, Eric }
818e935d5daSMoore, Eric EXPORT_SYMBOL_GPL(device_reprobe);
8191da177e4SLinus Torvalds 
8201da177e4SLinus Torvalds /**
8211da177e4SLinus Torvalds  * find_bus - locate bus by name.
8221da177e4SLinus Torvalds  * @name: name of bus.
8231da177e4SLinus Torvalds  *
8241da177e4SLinus Torvalds  * Call kset_find_obj() to iterate over list of buses to
8251da177e4SLinus Torvalds  * find a bus by name. Return bus if found.
8261da177e4SLinus Torvalds  *
8271da177e4SLinus Torvalds  * Note that kset_find_obj increments bus' reference count.
8281da177e4SLinus Torvalds  */
8297e4ef085SAdrian Bunk #if 0
8301da177e4SLinus Torvalds struct bus_type *find_bus(char *name)
8311da177e4SLinus Torvalds {
83259a54833SGreg Kroah-Hartman 	struct kobject *k = kset_find_obj(bus_kset, name);
8331da177e4SLinus Torvalds 	return k ? to_bus(k) : NULL;
8341da177e4SLinus Torvalds }
8357e4ef085SAdrian Bunk #endif  /*  0  */
8361da177e4SLinus Torvalds 
8371da177e4SLinus Torvalds 
8381da177e4SLinus Torvalds /**
8391da177e4SLinus Torvalds  * bus_add_attrs - Add default attributes for this bus.
8401da177e4SLinus Torvalds  * @bus: Bus that has just been registered.
8411da177e4SLinus Torvalds  */
8421da177e4SLinus Torvalds 
8431da177e4SLinus Torvalds static int bus_add_attrs(struct bus_type *bus)
8441da177e4SLinus Torvalds {
8451da177e4SLinus Torvalds 	int error = 0;
8461da177e4SLinus Torvalds 	int i;
8471da177e4SLinus Torvalds 
8481da177e4SLinus Torvalds 	if (bus->bus_attrs) {
8491da177e4SLinus Torvalds 		for (i = 0; attr_name(bus->bus_attrs[i]); i++) {
850dc0afa83SCornelia Huck 			error = bus_create_file(bus, &bus->bus_attrs[i]);
851dc0afa83SCornelia Huck 			if (error)
8524a3ad20cSGreg Kroah-Hartman 				goto err;
8531da177e4SLinus Torvalds 		}
8541da177e4SLinus Torvalds 	}
8554a3ad20cSGreg Kroah-Hartman done:
8561da177e4SLinus Torvalds 	return error;
8574a3ad20cSGreg Kroah-Hartman err:
8581da177e4SLinus Torvalds 	while (--i >= 0)
8591da177e4SLinus Torvalds 		bus_remove_file(bus, &bus->bus_attrs[i]);
8604a3ad20cSGreg Kroah-Hartman 	goto done;
8611da177e4SLinus Torvalds }
8621da177e4SLinus Torvalds 
8631da177e4SLinus Torvalds static void bus_remove_attrs(struct bus_type *bus)
8641da177e4SLinus Torvalds {
8651da177e4SLinus Torvalds 	int i;
8661da177e4SLinus Torvalds 
8671da177e4SLinus Torvalds 	if (bus->bus_attrs) {
8681da177e4SLinus Torvalds 		for (i = 0; attr_name(bus->bus_attrs[i]); i++)
8691da177e4SLinus Torvalds 			bus_remove_file(bus, &bus->bus_attrs[i]);
8701da177e4SLinus Torvalds 	}
8711da177e4SLinus Torvalds }
8721da177e4SLinus Torvalds 
87334bb61f9SJames Bottomley static void klist_devices_get(struct klist_node *n)
87434bb61f9SJames Bottomley {
875ae1b4171SGreg Kroah-Hartman 	struct device_private *dev_prv = to_device_private_bus(n);
876ae1b4171SGreg Kroah-Hartman 	struct device *dev = dev_prv->device;
87734bb61f9SJames Bottomley 
87834bb61f9SJames Bottomley 	get_device(dev);
87934bb61f9SJames Bottomley }
88034bb61f9SJames Bottomley 
88134bb61f9SJames Bottomley static void klist_devices_put(struct klist_node *n)
88234bb61f9SJames Bottomley {
883ae1b4171SGreg Kroah-Hartman 	struct device_private *dev_prv = to_device_private_bus(n);
884ae1b4171SGreg Kroah-Hartman 	struct device *dev = dev_prv->device;
88534bb61f9SJames Bottomley 
88634bb61f9SJames Bottomley 	put_device(dev);
88734bb61f9SJames Bottomley }
88834bb61f9SJames Bottomley 
8897ac1cf4aSKay Sievers static ssize_t bus_uevent_store(struct bus_type *bus,
8907ac1cf4aSKay Sievers 				const char *buf, size_t count)
8917ac1cf4aSKay Sievers {
8927ac1cf4aSKay Sievers 	enum kobject_action action;
8937ac1cf4aSKay Sievers 
8947ac1cf4aSKay Sievers 	if (kobject_action_type(buf, count, &action) == 0)
895c6f7e72aSGreg Kroah-Hartman 		kobject_uevent(&bus->p->subsys.kobj, action);
8967ac1cf4aSKay Sievers 	return count;
8977ac1cf4aSKay Sievers }
8987ac1cf4aSKay Sievers static BUS_ATTR(uevent, S_IWUSR, NULL, bus_uevent_store);
8997ac1cf4aSKay Sievers 
9001da177e4SLinus Torvalds /**
901ca22e56dSKay Sievers  * __bus_register - register a driver-core subsystem
90278d79559SRandy Dunlap  * @bus: bus to register
90378d79559SRandy Dunlap  * @key: lockdep class key
9041da177e4SLinus Torvalds  *
90578d79559SRandy Dunlap  * Once we have that, we register the bus with the kobject
9061da177e4SLinus Torvalds  * infrastructure, then register the children subsystems it has:
907ca22e56dSKay Sievers  * the devices and drivers that belong to the subsystem.
9081da177e4SLinus Torvalds  */
909ca22e56dSKay Sievers int __bus_register(struct bus_type *bus, struct lock_class_key *key)
9101da177e4SLinus Torvalds {
9111da177e4SLinus Torvalds 	int retval;
9126b6e39a6SKay Sievers 	struct subsys_private *priv;
9131da177e4SLinus Torvalds 
9146b6e39a6SKay Sievers 	priv = kzalloc(sizeof(struct subsys_private), GFP_KERNEL);
915c6f7e72aSGreg Kroah-Hartman 	if (!priv)
916c6f7e72aSGreg Kroah-Hartman 		return -ENOMEM;
917116af378SBenjamin Herrenschmidt 
918c6f7e72aSGreg Kroah-Hartman 	priv->bus = bus;
919c6f7e72aSGreg Kroah-Hartman 	bus->p = priv;
920c6f7e72aSGreg Kroah-Hartman 
921c6f7e72aSGreg Kroah-Hartman 	BLOCKING_INIT_NOTIFIER_HEAD(&priv->bus_notifier);
922c6f7e72aSGreg Kroah-Hartman 
923c6f7e72aSGreg Kroah-Hartman 	retval = kobject_set_name(&priv->subsys.kobj, "%s", bus->name);
9241da177e4SLinus Torvalds 	if (retval)
9251da177e4SLinus Torvalds 		goto out;
9261da177e4SLinus Torvalds 
927c6f7e72aSGreg Kroah-Hartman 	priv->subsys.kobj.kset = bus_kset;
928c6f7e72aSGreg Kroah-Hartman 	priv->subsys.kobj.ktype = &bus_ktype;
929c6f7e72aSGreg Kroah-Hartman 	priv->drivers_autoprobe = 1;
930d6b05b84SGreg Kroah-Hartman 
931c6f7e72aSGreg Kroah-Hartman 	retval = kset_register(&priv->subsys);
9321da177e4SLinus Torvalds 	if (retval)
9331da177e4SLinus Torvalds 		goto out;
9341da177e4SLinus Torvalds 
9357ac1cf4aSKay Sievers 	retval = bus_create_file(bus, &bus_attr_uevent);
9367ac1cf4aSKay Sievers 	if (retval)
9377ac1cf4aSKay Sievers 		goto bus_uevent_fail;
9387ac1cf4aSKay Sievers 
939c6f7e72aSGreg Kroah-Hartman 	priv->devices_kset = kset_create_and_add("devices", NULL,
940c6f7e72aSGreg Kroah-Hartman 						 &priv->subsys.kobj);
941c6f7e72aSGreg Kroah-Hartman 	if (!priv->devices_kset) {
9423d899596SGreg Kroah-Hartman 		retval = -ENOMEM;
9431da177e4SLinus Torvalds 		goto bus_devices_fail;
9443d899596SGreg Kroah-Hartman 	}
9451da177e4SLinus Torvalds 
946c6f7e72aSGreg Kroah-Hartman 	priv->drivers_kset = kset_create_and_add("drivers", NULL,
947c6f7e72aSGreg Kroah-Hartman 						 &priv->subsys.kobj);
948c6f7e72aSGreg Kroah-Hartman 	if (!priv->drivers_kset) {
9496dcec251SGreg Kroah-Hartman 		retval = -ENOMEM;
9501da177e4SLinus Torvalds 		goto bus_drivers_fail;
9516dcec251SGreg Kroah-Hartman 	}
952465c7a3aSmochel@digitalimplant.org 
953ca22e56dSKay Sievers 	INIT_LIST_HEAD(&priv->interfaces);
954ca22e56dSKay Sievers 	__mutex_init(&priv->mutex, "subsys mutex", key);
955c6f7e72aSGreg Kroah-Hartman 	klist_init(&priv->klist_devices, klist_devices_get, klist_devices_put);
956c6f7e72aSGreg Kroah-Hartman 	klist_init(&priv->klist_drivers, NULL, NULL);
957b8c5cec2SKay Sievers 
958b8c5cec2SKay Sievers 	retval = add_probe_files(bus);
959b8c5cec2SKay Sievers 	if (retval)
960b8c5cec2SKay Sievers 		goto bus_probe_files_fail;
961b8c5cec2SKay Sievers 
9621bb6881aSCornelia Huck 	retval = bus_add_attrs(bus);
9631bb6881aSCornelia Huck 	if (retval)
9641bb6881aSCornelia Huck 		goto bus_attrs_fail;
9651da177e4SLinus Torvalds 
9667dc72b28SGreg Kroah-Hartman 	pr_debug("bus: '%s': registered\n", bus->name);
9671da177e4SLinus Torvalds 	return 0;
9681da177e4SLinus Torvalds 
9691bb6881aSCornelia Huck bus_attrs_fail:
970b8c5cec2SKay Sievers 	remove_probe_files(bus);
971b8c5cec2SKay Sievers bus_probe_files_fail:
972c6f7e72aSGreg Kroah-Hartman 	kset_unregister(bus->p->drivers_kset);
9731da177e4SLinus Torvalds bus_drivers_fail:
974c6f7e72aSGreg Kroah-Hartman 	kset_unregister(bus->p->devices_kset);
9751da177e4SLinus Torvalds bus_devices_fail:
9767ac1cf4aSKay Sievers 	bus_remove_file(bus, &bus_attr_uevent);
9777ac1cf4aSKay Sievers bus_uevent_fail:
978c6f7e72aSGreg Kroah-Hartman 	kset_unregister(&bus->p->subsys);
9791da177e4SLinus Torvalds out:
980600c20f3SJike Song 	kfree(bus->p);
981f48f3febSDave Young 	bus->p = NULL;
9821da177e4SLinus Torvalds 	return retval;
9831da177e4SLinus Torvalds }
984ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(__bus_register);
9851da177e4SLinus Torvalds 
9861da177e4SLinus Torvalds /**
9871da177e4SLinus Torvalds  * bus_unregister - remove a bus from the system
9881da177e4SLinus Torvalds  * @bus: bus.
9891da177e4SLinus Torvalds  *
9901da177e4SLinus Torvalds  * Unregister the child subsystems and the bus itself.
991fc1ede58SGreg Kroah-Hartman  * Finally, we call bus_put() to release the refcount
9921da177e4SLinus Torvalds  */
9931da177e4SLinus Torvalds void bus_unregister(struct bus_type *bus)
9941da177e4SLinus Torvalds {
9957dc72b28SGreg Kroah-Hartman 	pr_debug("bus: '%s': unregistering\n", bus->name);
996ca22e56dSKay Sievers 	if (bus->dev_root)
997ca22e56dSKay Sievers 		device_unregister(bus->dev_root);
9981da177e4SLinus Torvalds 	bus_remove_attrs(bus);
999b8c5cec2SKay Sievers 	remove_probe_files(bus);
1000c6f7e72aSGreg Kroah-Hartman 	kset_unregister(bus->p->drivers_kset);
1001c6f7e72aSGreg Kroah-Hartman 	kset_unregister(bus->p->devices_kset);
10027ac1cf4aSKay Sievers 	bus_remove_file(bus, &bus_attr_uevent);
1003c6f7e72aSGreg Kroah-Hartman 	kset_unregister(&bus->p->subsys);
1004c6f7e72aSGreg Kroah-Hartman 	kfree(bus->p);
1005f48f3febSDave Young 	bus->p = NULL;
10061da177e4SLinus Torvalds }
10074a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_unregister);
10081da177e4SLinus Torvalds 
1009116af378SBenjamin Herrenschmidt int bus_register_notifier(struct bus_type *bus, struct notifier_block *nb)
1010116af378SBenjamin Herrenschmidt {
1011c6f7e72aSGreg Kroah-Hartman 	return blocking_notifier_chain_register(&bus->p->bus_notifier, nb);
1012116af378SBenjamin Herrenschmidt }
1013116af378SBenjamin Herrenschmidt EXPORT_SYMBOL_GPL(bus_register_notifier);
1014116af378SBenjamin Herrenschmidt 
1015116af378SBenjamin Herrenschmidt int bus_unregister_notifier(struct bus_type *bus, struct notifier_block *nb)
1016116af378SBenjamin Herrenschmidt {
1017c6f7e72aSGreg Kroah-Hartman 	return blocking_notifier_chain_unregister(&bus->p->bus_notifier, nb);
1018116af378SBenjamin Herrenschmidt }
1019116af378SBenjamin Herrenschmidt EXPORT_SYMBOL_GPL(bus_unregister_notifier);
1020116af378SBenjamin Herrenschmidt 
10210fed80f7SGreg Kroah-Hartman struct kset *bus_get_kset(struct bus_type *bus)
10220fed80f7SGreg Kroah-Hartman {
1023c6f7e72aSGreg Kroah-Hartman 	return &bus->p->subsys;
10240fed80f7SGreg Kroah-Hartman }
10250fed80f7SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_get_kset);
10260fed80f7SGreg Kroah-Hartman 
1027b249072eSGreg Kroah-Hartman struct klist *bus_get_device_klist(struct bus_type *bus)
1028b249072eSGreg Kroah-Hartman {
1029c6f7e72aSGreg Kroah-Hartman 	return &bus->p->klist_devices;
1030b249072eSGreg Kroah-Hartman }
1031b249072eSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_get_device_klist);
1032b249072eSGreg Kroah-Hartman 
103399178b03SGreg Kroah-Hartman /*
1034dca25ebdSRobert P. J. Day  * Yes, this forcibly breaks the klist abstraction temporarily.  It
103599178b03SGreg Kroah-Hartman  * just wants to sort the klist, not change reference counts and
103699178b03SGreg Kroah-Hartman  * take/drop locks rapidly in the process.  It does all this while
103799178b03SGreg Kroah-Hartman  * holding the lock for the list, so objects can't otherwise be
103899178b03SGreg Kroah-Hartman  * added/removed while we're swizzling.
103999178b03SGreg Kroah-Hartman  */
104099178b03SGreg Kroah-Hartman static void device_insertion_sort_klist(struct device *a, struct list_head *list,
104199178b03SGreg Kroah-Hartman 					int (*compare)(const struct device *a,
104299178b03SGreg Kroah-Hartman 							const struct device *b))
104399178b03SGreg Kroah-Hartman {
104499178b03SGreg Kroah-Hartman 	struct list_head *pos;
104599178b03SGreg Kroah-Hartman 	struct klist_node *n;
1046ae1b4171SGreg Kroah-Hartman 	struct device_private *dev_prv;
104799178b03SGreg Kroah-Hartman 	struct device *b;
104899178b03SGreg Kroah-Hartman 
104999178b03SGreg Kroah-Hartman 	list_for_each(pos, list) {
105099178b03SGreg Kroah-Hartman 		n = container_of(pos, struct klist_node, n_node);
1051ae1b4171SGreg Kroah-Hartman 		dev_prv = to_device_private_bus(n);
1052ae1b4171SGreg Kroah-Hartman 		b = dev_prv->device;
105399178b03SGreg Kroah-Hartman 		if (compare(a, b) <= 0) {
1054ae1b4171SGreg Kroah-Hartman 			list_move_tail(&a->p->knode_bus.n_node,
1055ae1b4171SGreg Kroah-Hartman 				       &b->p->knode_bus.n_node);
105699178b03SGreg Kroah-Hartman 			return;
105799178b03SGreg Kroah-Hartman 		}
105899178b03SGreg Kroah-Hartman 	}
1059ae1b4171SGreg Kroah-Hartman 	list_move_tail(&a->p->knode_bus.n_node, list);
106099178b03SGreg Kroah-Hartman }
106199178b03SGreg Kroah-Hartman 
106299178b03SGreg Kroah-Hartman void bus_sort_breadthfirst(struct bus_type *bus,
106399178b03SGreg Kroah-Hartman 			   int (*compare)(const struct device *a,
106499178b03SGreg Kroah-Hartman 					  const struct device *b))
106599178b03SGreg Kroah-Hartman {
106699178b03SGreg Kroah-Hartman 	LIST_HEAD(sorted_devices);
106799178b03SGreg Kroah-Hartman 	struct list_head *pos, *tmp;
106899178b03SGreg Kroah-Hartman 	struct klist_node *n;
1069ae1b4171SGreg Kroah-Hartman 	struct device_private *dev_prv;
107099178b03SGreg Kroah-Hartman 	struct device *dev;
107199178b03SGreg Kroah-Hartman 	struct klist *device_klist;
107299178b03SGreg Kroah-Hartman 
107399178b03SGreg Kroah-Hartman 	device_klist = bus_get_device_klist(bus);
107499178b03SGreg Kroah-Hartman 
107599178b03SGreg Kroah-Hartman 	spin_lock(&device_klist->k_lock);
107699178b03SGreg Kroah-Hartman 	list_for_each_safe(pos, tmp, &device_klist->k_list) {
107799178b03SGreg Kroah-Hartman 		n = container_of(pos, struct klist_node, n_node);
1078ae1b4171SGreg Kroah-Hartman 		dev_prv = to_device_private_bus(n);
1079ae1b4171SGreg Kroah-Hartman 		dev = dev_prv->device;
108099178b03SGreg Kroah-Hartman 		device_insertion_sort_klist(dev, &sorted_devices, compare);
108199178b03SGreg Kroah-Hartman 	}
108299178b03SGreg Kroah-Hartman 	list_splice(&sorted_devices, &device_klist->k_list);
108399178b03SGreg Kroah-Hartman 	spin_unlock(&device_klist->k_lock);
108499178b03SGreg Kroah-Hartman }
108599178b03SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_sort_breadthfirst);
108699178b03SGreg Kroah-Hartman 
1087ca22e56dSKay Sievers /**
1088ca22e56dSKay Sievers  * subsys_dev_iter_init - initialize subsys device iterator
1089ca22e56dSKay Sievers  * @iter: subsys iterator to initialize
1090ca22e56dSKay Sievers  * @subsys: the subsys we wanna iterate over
1091ca22e56dSKay Sievers  * @start: the device to start iterating from, if any
1092ca22e56dSKay Sievers  * @type: device_type of the devices to iterate over, NULL for all
1093ca22e56dSKay Sievers  *
1094ca22e56dSKay Sievers  * Initialize subsys iterator @iter such that it iterates over devices
1095ca22e56dSKay Sievers  * of @subsys.  If @start is set, the list iteration will start there,
1096ca22e56dSKay Sievers  * otherwise if it is NULL, the iteration starts at the beginning of
1097ca22e56dSKay Sievers  * the list.
1098ca22e56dSKay Sievers  */
10997cd9c9bbSGreg Kroah-Hartman void subsys_dev_iter_init(struct subsys_dev_iter *iter, struct bus_type *subsys,
1100ca22e56dSKay Sievers 			  struct device *start, const struct device_type *type)
1101ca22e56dSKay Sievers {
1102ca22e56dSKay Sievers 	struct klist_node *start_knode = NULL;
1103ca22e56dSKay Sievers 
1104ca22e56dSKay Sievers 	if (start)
1105ca22e56dSKay Sievers 		start_knode = &start->p->knode_bus;
11067cd9c9bbSGreg Kroah-Hartman 	klist_iter_init_node(&subsys->p->klist_devices, &iter->ki, start_knode);
1107ca22e56dSKay Sievers 	iter->type = type;
1108ca22e56dSKay Sievers }
1109ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_dev_iter_init);
1110ca22e56dSKay Sievers 
1111ca22e56dSKay Sievers /**
1112ca22e56dSKay Sievers  * subsys_dev_iter_next - iterate to the next device
1113ca22e56dSKay Sievers  * @iter: subsys iterator to proceed
1114ca22e56dSKay Sievers  *
1115ca22e56dSKay Sievers  * Proceed @iter to the next device and return it.  Returns NULL if
1116ca22e56dSKay Sievers  * iteration is complete.
1117ca22e56dSKay Sievers  *
1118ca22e56dSKay Sievers  * The returned device is referenced and won't be released till
1119ca22e56dSKay Sievers  * iterator is proceed to the next device or exited.  The caller is
1120ca22e56dSKay Sievers  * free to do whatever it wants to do with the device including
1121ca22e56dSKay Sievers  * calling back into subsys code.
1122ca22e56dSKay Sievers  */
1123ca22e56dSKay Sievers struct device *subsys_dev_iter_next(struct subsys_dev_iter *iter)
1124ca22e56dSKay Sievers {
1125ca22e56dSKay Sievers 	struct klist_node *knode;
1126ca22e56dSKay Sievers 	struct device *dev;
1127ca22e56dSKay Sievers 
1128ca22e56dSKay Sievers 	for (;;) {
1129ca22e56dSKay Sievers 		knode = klist_next(&iter->ki);
1130ca22e56dSKay Sievers 		if (!knode)
1131ca22e56dSKay Sievers 			return NULL;
1132ca22e56dSKay Sievers 		dev = container_of(knode, struct device_private, knode_bus)->device;
1133ca22e56dSKay Sievers 		if (!iter->type || iter->type == dev->type)
1134ca22e56dSKay Sievers 			return dev;
1135ca22e56dSKay Sievers 	}
1136ca22e56dSKay Sievers }
1137ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_dev_iter_next);
1138ca22e56dSKay Sievers 
1139ca22e56dSKay Sievers /**
1140ca22e56dSKay Sievers  * subsys_dev_iter_exit - finish iteration
1141ca22e56dSKay Sievers  * @iter: subsys iterator to finish
1142ca22e56dSKay Sievers  *
1143ca22e56dSKay Sievers  * Finish an iteration.  Always call this function after iteration is
1144ca22e56dSKay Sievers  * complete whether the iteration ran till the end or not.
1145ca22e56dSKay Sievers  */
1146ca22e56dSKay Sievers void subsys_dev_iter_exit(struct subsys_dev_iter *iter)
1147ca22e56dSKay Sievers {
1148ca22e56dSKay Sievers 	klist_iter_exit(&iter->ki);
1149ca22e56dSKay Sievers }
1150ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_dev_iter_exit);
1151ca22e56dSKay Sievers 
1152ca22e56dSKay Sievers int subsys_interface_register(struct subsys_interface *sif)
1153ca22e56dSKay Sievers {
1154ca22e56dSKay Sievers 	struct bus_type *subsys;
1155ca22e56dSKay Sievers 	struct subsys_dev_iter iter;
1156ca22e56dSKay Sievers 	struct device *dev;
1157ca22e56dSKay Sievers 
1158ca22e56dSKay Sievers 	if (!sif || !sif->subsys)
1159ca22e56dSKay Sievers 		return -ENODEV;
1160ca22e56dSKay Sievers 
1161ca22e56dSKay Sievers 	subsys = bus_get(sif->subsys);
1162ca22e56dSKay Sievers 	if (!subsys)
1163ca22e56dSKay Sievers 		return -EINVAL;
1164ca22e56dSKay Sievers 
1165ca22e56dSKay Sievers 	mutex_lock(&subsys->p->mutex);
1166ca22e56dSKay Sievers 	list_add_tail(&sif->node, &subsys->p->interfaces);
1167ca22e56dSKay Sievers 	if (sif->add_dev) {
1168ca22e56dSKay Sievers 		subsys_dev_iter_init(&iter, subsys, NULL, NULL);
1169ca22e56dSKay Sievers 		while ((dev = subsys_dev_iter_next(&iter)))
1170ca22e56dSKay Sievers 			sif->add_dev(dev, sif);
1171ca22e56dSKay Sievers 		subsys_dev_iter_exit(&iter);
1172ca22e56dSKay Sievers 	}
1173ca22e56dSKay Sievers 	mutex_unlock(&subsys->p->mutex);
1174ca22e56dSKay Sievers 
1175ca22e56dSKay Sievers 	return 0;
1176ca22e56dSKay Sievers }
1177ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_interface_register);
1178ca22e56dSKay Sievers 
1179ca22e56dSKay Sievers void subsys_interface_unregister(struct subsys_interface *sif)
1180ca22e56dSKay Sievers {
11812b31594aSJonghwan Choi 	struct bus_type *subsys;
1182ca22e56dSKay Sievers 	struct subsys_dev_iter iter;
1183ca22e56dSKay Sievers 	struct device *dev;
1184ca22e56dSKay Sievers 
11852b31594aSJonghwan Choi 	if (!sif || !sif->subsys)
1186ca22e56dSKay Sievers 		return;
1187ca22e56dSKay Sievers 
11882b31594aSJonghwan Choi 	subsys = sif->subsys;
11892b31594aSJonghwan Choi 
1190ca22e56dSKay Sievers 	mutex_lock(&subsys->p->mutex);
1191ca22e56dSKay Sievers 	list_del_init(&sif->node);
1192ca22e56dSKay Sievers 	if (sif->remove_dev) {
1193ca22e56dSKay Sievers 		subsys_dev_iter_init(&iter, subsys, NULL, NULL);
1194ca22e56dSKay Sievers 		while ((dev = subsys_dev_iter_next(&iter)))
1195ca22e56dSKay Sievers 			sif->remove_dev(dev, sif);
1196ca22e56dSKay Sievers 		subsys_dev_iter_exit(&iter);
1197ca22e56dSKay Sievers 	}
1198ca22e56dSKay Sievers 	mutex_unlock(&subsys->p->mutex);
1199ca22e56dSKay Sievers 
1200ca22e56dSKay Sievers 	bus_put(subsys);
1201ca22e56dSKay Sievers }
1202ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_interface_unregister);
1203ca22e56dSKay Sievers 
1204ca22e56dSKay Sievers static void system_root_device_release(struct device *dev)
1205ca22e56dSKay Sievers {
1206ca22e56dSKay Sievers 	kfree(dev);
1207ca22e56dSKay Sievers }
1208ca22e56dSKay Sievers /**
1209ca22e56dSKay Sievers  * subsys_system_register - register a subsystem at /sys/devices/system/
121078d79559SRandy Dunlap  * @subsys: system subsystem
121178d79559SRandy Dunlap  * @groups: default attributes for the root device
1212ca22e56dSKay Sievers  *
1213ca22e56dSKay Sievers  * All 'system' subsystems have a /sys/devices/system/<name> root device
1214ca22e56dSKay Sievers  * with the name of the subsystem. The root device can carry subsystem-
1215ca22e56dSKay Sievers  * wide attributes. All registered devices are below this single root
1216ca22e56dSKay Sievers  * device and are named after the subsystem with a simple enumeration
1217ca22e56dSKay Sievers  * number appended. The registered devices are not explicitely named;
1218ca22e56dSKay Sievers  * only 'id' in the device needs to be set.
1219ca22e56dSKay Sievers  *
1220ca22e56dSKay Sievers  * Do not use this interface for anything new, it exists for compatibility
1221ca22e56dSKay Sievers  * with bad ideas only. New subsystems should use plain subsystems; and
1222ca22e56dSKay Sievers  * add the subsystem-wide attributes should be added to the subsystem
1223ca22e56dSKay Sievers  * directory itself and not some create fake root-device placed in
1224ca22e56dSKay Sievers  * /sys/devices/system/<name>.
1225ca22e56dSKay Sievers  */
1226ca22e56dSKay Sievers int subsys_system_register(struct bus_type *subsys,
1227ca22e56dSKay Sievers 			   const struct attribute_group **groups)
1228ca22e56dSKay Sievers {
1229ca22e56dSKay Sievers 	struct device *dev;
1230ca22e56dSKay Sievers 	int err;
1231ca22e56dSKay Sievers 
1232ca22e56dSKay Sievers 	err = bus_register(subsys);
1233ca22e56dSKay Sievers 	if (err < 0)
1234ca22e56dSKay Sievers 		return err;
1235ca22e56dSKay Sievers 
1236ca22e56dSKay Sievers 	dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1237ca22e56dSKay Sievers 	if (!dev) {
1238ca22e56dSKay Sievers 		err = -ENOMEM;
1239ca22e56dSKay Sievers 		goto err_dev;
1240ca22e56dSKay Sievers 	}
1241ca22e56dSKay Sievers 
1242ca22e56dSKay Sievers 	err = dev_set_name(dev, "%s", subsys->name);
1243ca22e56dSKay Sievers 	if (err < 0)
1244ca22e56dSKay Sievers 		goto err_name;
1245ca22e56dSKay Sievers 
1246ca22e56dSKay Sievers 	dev->kobj.parent = &system_kset->kobj;
1247ca22e56dSKay Sievers 	dev->groups = groups;
1248ca22e56dSKay Sievers 	dev->release = system_root_device_release;
1249ca22e56dSKay Sievers 
1250ca22e56dSKay Sievers 	err = device_register(dev);
1251ca22e56dSKay Sievers 	if (err < 0)
1252ca22e56dSKay Sievers 		goto err_dev_reg;
1253ca22e56dSKay Sievers 
1254ca22e56dSKay Sievers 	subsys->dev_root = dev;
1255ca22e56dSKay Sievers 	return 0;
1256ca22e56dSKay Sievers 
1257ca22e56dSKay Sievers err_dev_reg:
1258ca22e56dSKay Sievers 	put_device(dev);
1259ca22e56dSKay Sievers 	dev = NULL;
1260ca22e56dSKay Sievers err_name:
1261ca22e56dSKay Sievers 	kfree(dev);
1262ca22e56dSKay Sievers err_dev:
1263ca22e56dSKay Sievers 	bus_unregister(subsys);
1264ca22e56dSKay Sievers 	return err;
1265ca22e56dSKay Sievers }
1266ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_system_register);
1267ca22e56dSKay Sievers 
12681da177e4SLinus Torvalds int __init buses_init(void)
12691da177e4SLinus Torvalds {
127059a54833SGreg Kroah-Hartman 	bus_kset = kset_create_and_add("bus", &bus_uevent_ops, NULL);
127159a54833SGreg Kroah-Hartman 	if (!bus_kset)
127259a54833SGreg Kroah-Hartman 		return -ENOMEM;
1273ca22e56dSKay Sievers 
1274ca22e56dSKay Sievers 	system_kset = kset_create_and_add("system", NULL, &devices_kset->kobj);
1275ca22e56dSKay Sievers 	if (!system_kset)
1276ca22e56dSKay Sievers 		return -ENOMEM;
1277ca22e56dSKay Sievers 
127859a54833SGreg Kroah-Hartman 	return 0;
12791da177e4SLinus Torvalds }
1280