xref: /linux/drivers/base/bus.c (revision ca22e56debc57b47c422b749c93217ba62644be2)
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>
19*ca22e56dSKay Sievers #include <linux/mutex.h>
201da177e4SLinus Torvalds #include "base.h"
211da177e4SLinus Torvalds #include "power/power.h"
221da177e4SLinus Torvalds 
23*ca22e56dSKay Sievers /* /sys/devices/system */
24*ca22e56dSKay Sievers /* FIXME: make static after drivers/base/sys.c is deleted */
25*ca22e56dSKay Sievers struct kset *system_kset;
26*ca22e56dSKay Sievers 
271da177e4SLinus Torvalds #define to_bus_attr(_attr) container_of(_attr, struct bus_attribute, attr)
281da177e4SLinus Torvalds 
291da177e4SLinus Torvalds /*
301da177e4SLinus Torvalds  * sysfs bindings for drivers
311da177e4SLinus Torvalds  */
321da177e4SLinus Torvalds 
331da177e4SLinus Torvalds #define to_drv_attr(_attr) container_of(_attr, struct driver_attribute, attr)
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds 
36b8c5cec2SKay Sievers static int __must_check bus_rescan_devices_helper(struct device *dev,
37b8c5cec2SKay Sievers 						void *data);
38b8c5cec2SKay Sievers 
395901d014SGreg Kroah-Hartman static struct bus_type *bus_get(struct bus_type *bus)
405901d014SGreg Kroah-Hartman {
41c6f7e72aSGreg Kroah-Hartman 	if (bus) {
42c6f7e72aSGreg Kroah-Hartman 		kset_get(&bus->p->subsys);
43c6f7e72aSGreg Kroah-Hartman 		return bus;
44c6f7e72aSGreg Kroah-Hartman 	}
45c6f7e72aSGreg Kroah-Hartman 	return NULL;
465901d014SGreg Kroah-Hartman }
475901d014SGreg Kroah-Hartman 
48fc1ede58SGreg Kroah-Hartman static void bus_put(struct bus_type *bus)
49fc1ede58SGreg Kroah-Hartman {
50c6f7e72aSGreg Kroah-Hartman 	if (bus)
51c6f7e72aSGreg Kroah-Hartman 		kset_put(&bus->p->subsys);
52fc1ede58SGreg Kroah-Hartman }
53fc1ede58SGreg Kroah-Hartman 
544a3ad20cSGreg Kroah-Hartman static ssize_t drv_attr_show(struct kobject *kobj, struct attribute *attr,
554a3ad20cSGreg Kroah-Hartman 			     char *buf)
561da177e4SLinus Torvalds {
571da177e4SLinus Torvalds 	struct driver_attribute *drv_attr = to_drv_attr(attr);
58e5dd1278SGreg Kroah-Hartman 	struct driver_private *drv_priv = to_driver(kobj);
594a0c20bfSDmitry Torokhov 	ssize_t ret = -EIO;
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds 	if (drv_attr->show)
62e5dd1278SGreg Kroah-Hartman 		ret = drv_attr->show(drv_priv->driver, buf);
631da177e4SLinus Torvalds 	return ret;
641da177e4SLinus Torvalds }
651da177e4SLinus Torvalds 
664a3ad20cSGreg Kroah-Hartman static ssize_t drv_attr_store(struct kobject *kobj, struct attribute *attr,
671da177e4SLinus Torvalds 			      const char *buf, size_t count)
681da177e4SLinus Torvalds {
691da177e4SLinus Torvalds 	struct driver_attribute *drv_attr = to_drv_attr(attr);
70e5dd1278SGreg Kroah-Hartman 	struct driver_private *drv_priv = to_driver(kobj);
714a0c20bfSDmitry Torokhov 	ssize_t ret = -EIO;
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds 	if (drv_attr->store)
74e5dd1278SGreg Kroah-Hartman 		ret = drv_attr->store(drv_priv->driver, buf, count);
751da177e4SLinus Torvalds 	return ret;
761da177e4SLinus Torvalds }
771da177e4SLinus Torvalds 
7852cf25d0SEmese Revfy static const struct sysfs_ops driver_sysfs_ops = {
791da177e4SLinus Torvalds 	.show	= drv_attr_show,
801da177e4SLinus Torvalds 	.store	= drv_attr_store,
811da177e4SLinus Torvalds };
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds static void driver_release(struct kobject *kobj)
841da177e4SLinus Torvalds {
85e5dd1278SGreg Kroah-Hartman 	struct driver_private *drv_priv = to_driver(kobj);
86e5dd1278SGreg Kroah-Hartman 
872b3a302aSHarvey Harrison 	pr_debug("driver: '%s': %s\n", kobject_name(kobj), __func__);
88e5dd1278SGreg Kroah-Hartman 	kfree(drv_priv);
891da177e4SLinus Torvalds }
901da177e4SLinus Torvalds 
91a1148fb0SGreg Kroah-Hartman static struct kobj_type driver_ktype = {
921da177e4SLinus Torvalds 	.sysfs_ops	= &driver_sysfs_ops,
931da177e4SLinus Torvalds 	.release	= driver_release,
941da177e4SLinus Torvalds };
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds /*
971da177e4SLinus Torvalds  * sysfs bindings for buses
981da177e4SLinus Torvalds  */
994a3ad20cSGreg Kroah-Hartman static ssize_t bus_attr_show(struct kobject *kobj, struct attribute *attr,
1004a3ad20cSGreg Kroah-Hartman 			     char *buf)
1011da177e4SLinus Torvalds {
1021da177e4SLinus Torvalds 	struct bus_attribute *bus_attr = to_bus_attr(attr);
1036b6e39a6SKay Sievers 	struct subsys_private *subsys_priv = to_subsys_private(kobj);
1041da177e4SLinus Torvalds 	ssize_t ret = 0;
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds 	if (bus_attr->show)
1076b6e39a6SKay Sievers 		ret = bus_attr->show(subsys_priv->bus, buf);
1081da177e4SLinus Torvalds 	return ret;
1091da177e4SLinus Torvalds }
1101da177e4SLinus Torvalds 
1114a3ad20cSGreg Kroah-Hartman static ssize_t bus_attr_store(struct kobject *kobj, struct attribute *attr,
1121da177e4SLinus Torvalds 			      const char *buf, size_t count)
1131da177e4SLinus Torvalds {
1141da177e4SLinus Torvalds 	struct bus_attribute *bus_attr = to_bus_attr(attr);
1156b6e39a6SKay Sievers 	struct subsys_private *subsys_priv = to_subsys_private(kobj);
1161da177e4SLinus Torvalds 	ssize_t ret = 0;
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds 	if (bus_attr->store)
1196b6e39a6SKay Sievers 		ret = bus_attr->store(subsys_priv->bus, buf, count);
1201da177e4SLinus Torvalds 	return ret;
1211da177e4SLinus Torvalds }
1221da177e4SLinus Torvalds 
12352cf25d0SEmese Revfy static const struct sysfs_ops bus_sysfs_ops = {
1241da177e4SLinus Torvalds 	.show	= bus_attr_show,
1251da177e4SLinus Torvalds 	.store	= bus_attr_store,
1261da177e4SLinus Torvalds };
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds int bus_create_file(struct bus_type *bus, struct bus_attribute *attr)
1291da177e4SLinus Torvalds {
1301da177e4SLinus Torvalds 	int error;
1315901d014SGreg Kroah-Hartman 	if (bus_get(bus)) {
132c6f7e72aSGreg Kroah-Hartman 		error = sysfs_create_file(&bus->p->subsys.kobj, &attr->attr);
133fc1ede58SGreg Kroah-Hartman 		bus_put(bus);
1341da177e4SLinus Torvalds 	} else
1351da177e4SLinus Torvalds 		error = -EINVAL;
1361da177e4SLinus Torvalds 	return error;
1371da177e4SLinus Torvalds }
1384a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_create_file);
1391da177e4SLinus Torvalds 
1401da177e4SLinus Torvalds void bus_remove_file(struct bus_type *bus, struct bus_attribute *attr)
1411da177e4SLinus Torvalds {
1425901d014SGreg Kroah-Hartman 	if (bus_get(bus)) {
143c6f7e72aSGreg Kroah-Hartman 		sysfs_remove_file(&bus->p->subsys.kobj, &attr->attr);
144fc1ede58SGreg Kroah-Hartman 		bus_put(bus);
1451da177e4SLinus Torvalds 	}
1461da177e4SLinus Torvalds }
1474a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_remove_file);
1481da177e4SLinus Torvalds 
14980f03e34SKay Sievers static struct kobj_type bus_ktype = {
1501da177e4SLinus Torvalds 	.sysfs_ops	= &bus_sysfs_ops,
1511da177e4SLinus Torvalds };
1521da177e4SLinus Torvalds 
15380f03e34SKay Sievers static int bus_uevent_filter(struct kset *kset, struct kobject *kobj)
15480f03e34SKay Sievers {
15580f03e34SKay Sievers 	struct kobj_type *ktype = get_ktype(kobj);
15680f03e34SKay Sievers 
15780f03e34SKay Sievers 	if (ktype == &bus_ktype)
15880f03e34SKay Sievers 		return 1;
15980f03e34SKay Sievers 	return 0;
16080f03e34SKay Sievers }
16180f03e34SKay Sievers 
1629cd43611SEmese Revfy static const struct kset_uevent_ops bus_uevent_ops = {
16380f03e34SKay Sievers 	.filter = bus_uevent_filter,
16480f03e34SKay Sievers };
16580f03e34SKay Sievers 
16659a54833SGreg Kroah-Hartman static struct kset *bus_kset;
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds 
1692139bdd5SRussell King #ifdef CONFIG_HOTPLUG
1702b08c8d0SAlan Stern /* Manually detach a device from its associated driver. */
171151ef38fSGreg Kroah-Hartman static ssize_t driver_unbind(struct device_driver *drv,
172151ef38fSGreg Kroah-Hartman 			     const char *buf, size_t count)
173151ef38fSGreg Kroah-Hartman {
1745901d014SGreg Kroah-Hartman 	struct bus_type *bus = bus_get(drv->bus);
175151ef38fSGreg Kroah-Hartman 	struct device *dev;
176151ef38fSGreg Kroah-Hartman 	int err = -ENODEV;
177151ef38fSGreg Kroah-Hartman 
1781f9ffc04SGreg Kroah-Hartman 	dev = bus_find_device_by_name(bus, NULL, buf);
1792b08c8d0SAlan Stern 	if (dev && dev->driver == drv) {
180bf74ad5bSAlan Stern 		if (dev->parent)	/* Needed for USB */
1818e9394ceSGreg Kroah-Hartman 			device_lock(dev->parent);
182151ef38fSGreg Kroah-Hartman 		device_release_driver(dev);
183bf74ad5bSAlan Stern 		if (dev->parent)
1848e9394ceSGreg Kroah-Hartman 			device_unlock(dev->parent);
185151ef38fSGreg Kroah-Hartman 		err = count;
186151ef38fSGreg Kroah-Hartman 	}
1872b08c8d0SAlan Stern 	put_device(dev);
188fc1ede58SGreg Kroah-Hartman 	bus_put(bus);
189151ef38fSGreg Kroah-Hartman 	return err;
190151ef38fSGreg Kroah-Hartman }
191151ef38fSGreg Kroah-Hartman static DRIVER_ATTR(unbind, S_IWUSR, NULL, driver_unbind);
192151ef38fSGreg Kroah-Hartman 
193afdce75fSGreg Kroah-Hartman /*
194afdce75fSGreg Kroah-Hartman  * Manually attach a device to a driver.
195afdce75fSGreg Kroah-Hartman  * Note: the driver must want to bind to the device,
196afdce75fSGreg Kroah-Hartman  * it is not possible to override the driver's id table.
197afdce75fSGreg Kroah-Hartman  */
198afdce75fSGreg Kroah-Hartman static ssize_t driver_bind(struct device_driver *drv,
199afdce75fSGreg Kroah-Hartman 			   const char *buf, size_t count)
200afdce75fSGreg Kroah-Hartman {
2015901d014SGreg Kroah-Hartman 	struct bus_type *bus = bus_get(drv->bus);
202afdce75fSGreg Kroah-Hartman 	struct device *dev;
203afdce75fSGreg Kroah-Hartman 	int err = -ENODEV;
204afdce75fSGreg Kroah-Hartman 
2051f9ffc04SGreg Kroah-Hartman 	dev = bus_find_device_by_name(bus, NULL, buf);
20649b420a1SMing Lei 	if (dev && dev->driver == NULL && driver_match_device(drv, dev)) {
207bf74ad5bSAlan Stern 		if (dev->parent)	/* Needed for USB */
2088e9394ceSGreg Kroah-Hartman 			device_lock(dev->parent);
2098e9394ceSGreg Kroah-Hartman 		device_lock(dev);
210afdce75fSGreg Kroah-Hartman 		err = driver_probe_device(drv, dev);
2118e9394ceSGreg Kroah-Hartman 		device_unlock(dev);
212bf74ad5bSAlan Stern 		if (dev->parent)
2138e9394ceSGreg Kroah-Hartman 			device_unlock(dev->parent);
21437225401SRyan Wilson 
2154a3ad20cSGreg Kroah-Hartman 		if (err > 0) {
2164a3ad20cSGreg Kroah-Hartman 			/* success */
21737225401SRyan Wilson 			err = count;
2184a3ad20cSGreg Kroah-Hartman 		} else if (err == 0) {
2194a3ad20cSGreg Kroah-Hartman 			/* driver didn't accept device */
22037225401SRyan Wilson 			err = -ENODEV;
221afdce75fSGreg Kroah-Hartman 		}
2224a3ad20cSGreg Kroah-Hartman 	}
2232b08c8d0SAlan Stern 	put_device(dev);
224fc1ede58SGreg Kroah-Hartman 	bus_put(bus);
225afdce75fSGreg Kroah-Hartman 	return err;
226afdce75fSGreg Kroah-Hartman }
227afdce75fSGreg Kroah-Hartman static DRIVER_ATTR(bind, S_IWUSR, NULL, driver_bind);
228afdce75fSGreg Kroah-Hartman 
229b8c5cec2SKay Sievers static ssize_t show_drivers_autoprobe(struct bus_type *bus, char *buf)
230b8c5cec2SKay Sievers {
231c6f7e72aSGreg Kroah-Hartman 	return sprintf(buf, "%d\n", bus->p->drivers_autoprobe);
232b8c5cec2SKay Sievers }
233b8c5cec2SKay Sievers 
234b8c5cec2SKay Sievers static ssize_t store_drivers_autoprobe(struct bus_type *bus,
235b8c5cec2SKay Sievers 				       const char *buf, size_t count)
236b8c5cec2SKay Sievers {
237b8c5cec2SKay Sievers 	if (buf[0] == '0')
238c6f7e72aSGreg Kroah-Hartman 		bus->p->drivers_autoprobe = 0;
239b8c5cec2SKay Sievers 	else
240c6f7e72aSGreg Kroah-Hartman 		bus->p->drivers_autoprobe = 1;
241b8c5cec2SKay Sievers 	return count;
242b8c5cec2SKay Sievers }
243b8c5cec2SKay Sievers 
244b8c5cec2SKay Sievers static ssize_t store_drivers_probe(struct bus_type *bus,
245b8c5cec2SKay Sievers 				   const char *buf, size_t count)
246b8c5cec2SKay Sievers {
247b8c5cec2SKay Sievers 	struct device *dev;
248b8c5cec2SKay Sievers 
2491f9ffc04SGreg Kroah-Hartman 	dev = bus_find_device_by_name(bus, NULL, buf);
250b8c5cec2SKay Sievers 	if (!dev)
251b8c5cec2SKay Sievers 		return -ENODEV;
252b8c5cec2SKay Sievers 	if (bus_rescan_devices_helper(dev, NULL) != 0)
253b8c5cec2SKay Sievers 		return -EINVAL;
254b8c5cec2SKay Sievers 	return count;
255b8c5cec2SKay Sievers }
2562139bdd5SRussell King #endif
257151ef38fSGreg Kroah-Hartman 
258465c7a3aSmochel@digitalimplant.org static struct device *next_device(struct klist_iter *i)
259465c7a3aSmochel@digitalimplant.org {
260465c7a3aSmochel@digitalimplant.org 	struct klist_node *n = klist_next(i);
261ae1b4171SGreg Kroah-Hartman 	struct device *dev = NULL;
262ae1b4171SGreg Kroah-Hartman 	struct device_private *dev_prv;
263ae1b4171SGreg Kroah-Hartman 
264ae1b4171SGreg Kroah-Hartman 	if (n) {
265ae1b4171SGreg Kroah-Hartman 		dev_prv = to_device_private_bus(n);
266ae1b4171SGreg Kroah-Hartman 		dev = dev_prv->device;
267ae1b4171SGreg Kroah-Hartman 	}
268ae1b4171SGreg Kroah-Hartman 	return dev;
269465c7a3aSmochel@digitalimplant.org }
270465c7a3aSmochel@digitalimplant.org 
2711da177e4SLinus Torvalds /**
2721da177e4SLinus Torvalds  * bus_for_each_dev - device iterator.
2731da177e4SLinus Torvalds  * @bus: bus type.
2741da177e4SLinus Torvalds  * @start: device to start iterating from.
2751da177e4SLinus Torvalds  * @data: data for the callback.
2761da177e4SLinus Torvalds  * @fn: function to be called for each device.
2771da177e4SLinus Torvalds  *
2781da177e4SLinus Torvalds  * Iterate over @bus's list of devices, and call @fn for each,
2791da177e4SLinus Torvalds  * passing it @data. If @start is not NULL, we use that device to
2801da177e4SLinus Torvalds  * begin iterating from.
2811da177e4SLinus Torvalds  *
2821da177e4SLinus Torvalds  * We check the return of @fn each time. If it returns anything
2831da177e4SLinus Torvalds  * other than 0, we break out and return that value.
2841da177e4SLinus Torvalds  *
2851da177e4SLinus Torvalds  * NOTE: The device that returns a non-zero value is not retained
2861da177e4SLinus Torvalds  * in any way, nor is its refcount incremented. If the caller needs
2870fa1b0a1SAlex Chiang  * to retain this data, it should do so, and increment the reference
2881da177e4SLinus Torvalds  * count in the supplied callback.
2891da177e4SLinus Torvalds  */
2901da177e4SLinus Torvalds int bus_for_each_dev(struct bus_type *bus, struct device *start,
2911da177e4SLinus Torvalds 		     void *data, int (*fn)(struct device *, void *))
2921da177e4SLinus Torvalds {
293465c7a3aSmochel@digitalimplant.org 	struct klist_iter i;
294465c7a3aSmochel@digitalimplant.org 	struct device *dev;
295465c7a3aSmochel@digitalimplant.org 	int error = 0;
2961da177e4SLinus Torvalds 
297465c7a3aSmochel@digitalimplant.org 	if (!bus)
298465c7a3aSmochel@digitalimplant.org 		return -EINVAL;
299465c7a3aSmochel@digitalimplant.org 
300c6f7e72aSGreg Kroah-Hartman 	klist_iter_init_node(&bus->p->klist_devices, &i,
301ae1b4171SGreg Kroah-Hartman 			     (start ? &start->p->knode_bus : NULL));
302465c7a3aSmochel@digitalimplant.org 	while ((dev = next_device(&i)) && !error)
303465c7a3aSmochel@digitalimplant.org 		error = fn(dev, data);
304465c7a3aSmochel@digitalimplant.org 	klist_iter_exit(&i);
305465c7a3aSmochel@digitalimplant.org 	return error;
3061da177e4SLinus Torvalds }
3074a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_for_each_dev);
3081da177e4SLinus Torvalds 
3090edb5860SCornelia Huck /**
3100edb5860SCornelia Huck  * bus_find_device - device iterator for locating a particular device.
3110edb5860SCornelia Huck  * @bus: bus type
3120edb5860SCornelia Huck  * @start: Device to begin with
3130edb5860SCornelia Huck  * @data: Data to pass to match function
3140edb5860SCornelia Huck  * @match: Callback function to check device
3150edb5860SCornelia Huck  *
3160edb5860SCornelia Huck  * This is similar to the bus_for_each_dev() function above, but it
3170edb5860SCornelia Huck  * returns a reference to a device that is 'found' for later use, as
3180edb5860SCornelia Huck  * determined by the @match callback.
3190edb5860SCornelia Huck  *
3200edb5860SCornelia Huck  * The callback should return 0 if the device doesn't match and non-zero
3210edb5860SCornelia Huck  * if it does.  If the callback returns non-zero, this function will
3220edb5860SCornelia Huck  * return to the caller and not iterate over any more devices.
3230edb5860SCornelia Huck  */
3240edb5860SCornelia Huck struct device *bus_find_device(struct bus_type *bus,
3250edb5860SCornelia Huck 			       struct device *start, void *data,
3264a3ad20cSGreg Kroah-Hartman 			       int (*match)(struct device *dev, void *data))
3270edb5860SCornelia Huck {
3280edb5860SCornelia Huck 	struct klist_iter i;
3290edb5860SCornelia Huck 	struct device *dev;
3300edb5860SCornelia Huck 
3310edb5860SCornelia Huck 	if (!bus)
3320edb5860SCornelia Huck 		return NULL;
3330edb5860SCornelia Huck 
334c6f7e72aSGreg Kroah-Hartman 	klist_iter_init_node(&bus->p->klist_devices, &i,
335ae1b4171SGreg Kroah-Hartman 			     (start ? &start->p->knode_bus : NULL));
3360edb5860SCornelia Huck 	while ((dev = next_device(&i)))
3370edb5860SCornelia Huck 		if (match(dev, data) && get_device(dev))
3380edb5860SCornelia Huck 			break;
3390edb5860SCornelia Huck 	klist_iter_exit(&i);
3400edb5860SCornelia Huck 	return dev;
3410edb5860SCornelia Huck }
3424a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_find_device);
34338fdac3cSmochel@digitalimplant.org 
3441f9ffc04SGreg Kroah-Hartman static int match_name(struct device *dev, void *data)
3451f9ffc04SGreg Kroah-Hartman {
3461f9ffc04SGreg Kroah-Hartman 	const char *name = data;
3471f9ffc04SGreg Kroah-Hartman 
3481e0b2cf9SKay Sievers 	return sysfs_streq(name, dev_name(dev));
3491f9ffc04SGreg Kroah-Hartman }
3501f9ffc04SGreg Kroah-Hartman 
3511f9ffc04SGreg Kroah-Hartman /**
3521f9ffc04SGreg Kroah-Hartman  * bus_find_device_by_name - device iterator for locating a particular device of a specific name
3531f9ffc04SGreg Kroah-Hartman  * @bus: bus type
3541f9ffc04SGreg Kroah-Hartman  * @start: Device to begin with
3551f9ffc04SGreg Kroah-Hartman  * @name: name of the device to match
3561f9ffc04SGreg Kroah-Hartman  *
3571f9ffc04SGreg Kroah-Hartman  * This is similar to the bus_find_device() function above, but it handles
3581f9ffc04SGreg Kroah-Hartman  * searching by a name automatically, no need to write another strcmp matching
3591f9ffc04SGreg Kroah-Hartman  * function.
3601f9ffc04SGreg Kroah-Hartman  */
3611f9ffc04SGreg Kroah-Hartman struct device *bus_find_device_by_name(struct bus_type *bus,
3621f9ffc04SGreg Kroah-Hartman 				       struct device *start, const char *name)
3631f9ffc04SGreg Kroah-Hartman {
3641f9ffc04SGreg Kroah-Hartman 	return bus_find_device(bus, start, (void *)name, match_name);
3651f9ffc04SGreg Kroah-Hartman }
3661f9ffc04SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_find_device_by_name);
3671f9ffc04SGreg Kroah-Hartman 
368*ca22e56dSKay Sievers /**
369*ca22e56dSKay Sievers  * subsys_find_device_by_id - find a device with a specific enumeration number
370*ca22e56dSKay Sievers  * @subsys: subsystem
371*ca22e56dSKay Sievers  * @id: index 'id' in struct device
372*ca22e56dSKay Sievers  * @hint: device to check first
373*ca22e56dSKay Sievers  *
374*ca22e56dSKay Sievers  * Check the hint's next object and if it is a match return it directly,
375*ca22e56dSKay Sievers  * otherwise, fall back to a full list search. Either way a reference for
376*ca22e56dSKay Sievers  * the returned object is taken.
377*ca22e56dSKay Sievers  */
378*ca22e56dSKay Sievers struct device *subsys_find_device_by_id(struct bus_type *subsys, unsigned int id,
379*ca22e56dSKay Sievers 					struct device *hint)
380*ca22e56dSKay Sievers {
381*ca22e56dSKay Sievers 	struct klist_iter i;
382*ca22e56dSKay Sievers 	struct device *dev;
383*ca22e56dSKay Sievers 
384*ca22e56dSKay Sievers 	if (!subsys)
385*ca22e56dSKay Sievers 		return NULL;
386*ca22e56dSKay Sievers 
387*ca22e56dSKay Sievers 	if (hint) {
388*ca22e56dSKay Sievers 		klist_iter_init_node(&subsys->p->klist_devices, &i, &hint->p->knode_bus);
389*ca22e56dSKay Sievers 		dev = next_device(&i);
390*ca22e56dSKay Sievers 		if (dev && dev->id == id && get_device(dev)) {
391*ca22e56dSKay Sievers 			klist_iter_exit(&i);
392*ca22e56dSKay Sievers 			return dev;
393*ca22e56dSKay Sievers 		}
394*ca22e56dSKay Sievers 		klist_iter_exit(&i);
395*ca22e56dSKay Sievers 	}
396*ca22e56dSKay Sievers 
397*ca22e56dSKay Sievers 	klist_iter_init_node(&subsys->p->klist_devices, &i, NULL);
398*ca22e56dSKay Sievers 	while ((dev = next_device(&i))) {
399*ca22e56dSKay Sievers 		if (dev->id == id && get_device(dev)) {
400*ca22e56dSKay Sievers 			klist_iter_exit(&i);
401*ca22e56dSKay Sievers 			return dev;
402*ca22e56dSKay Sievers 		}
403*ca22e56dSKay Sievers 	}
404*ca22e56dSKay Sievers 	klist_iter_exit(&i);
405*ca22e56dSKay Sievers 	return NULL;
406*ca22e56dSKay Sievers }
407*ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_find_device_by_id);
408*ca22e56dSKay Sievers 
40938fdac3cSmochel@digitalimplant.org static struct device_driver *next_driver(struct klist_iter *i)
41038fdac3cSmochel@digitalimplant.org {
41138fdac3cSmochel@digitalimplant.org 	struct klist_node *n = klist_next(i);
412e5dd1278SGreg Kroah-Hartman 	struct driver_private *drv_priv;
413e5dd1278SGreg Kroah-Hartman 
414e5dd1278SGreg Kroah-Hartman 	if (n) {
415e5dd1278SGreg Kroah-Hartman 		drv_priv = container_of(n, struct driver_private, knode_bus);
416e5dd1278SGreg Kroah-Hartman 		return drv_priv->driver;
417e5dd1278SGreg Kroah-Hartman 	}
418e5dd1278SGreg Kroah-Hartman 	return NULL;
41938fdac3cSmochel@digitalimplant.org }
42038fdac3cSmochel@digitalimplant.org 
4211da177e4SLinus Torvalds /**
4221da177e4SLinus Torvalds  * bus_for_each_drv - driver iterator
4231da177e4SLinus Torvalds  * @bus: bus we're dealing with.
4241da177e4SLinus Torvalds  * @start: driver to start iterating on.
4251da177e4SLinus Torvalds  * @data: data to pass to the callback.
4261da177e4SLinus Torvalds  * @fn: function to call for each driver.
4271da177e4SLinus Torvalds  *
4281da177e4SLinus Torvalds  * This is nearly identical to the device iterator above.
4291da177e4SLinus Torvalds  * We iterate over each driver that belongs to @bus, and call
4301da177e4SLinus Torvalds  * @fn for each. If @fn returns anything but 0, we break out
4311da177e4SLinus Torvalds  * and return it. If @start is not NULL, we use it as the head
4321da177e4SLinus Torvalds  * of the list.
4331da177e4SLinus Torvalds  *
4341da177e4SLinus Torvalds  * NOTE: we don't return the driver that returns a non-zero
4351da177e4SLinus Torvalds  * value, nor do we leave the reference count incremented for that
4361da177e4SLinus Torvalds  * driver. If the caller needs to know that info, it must set it
4371da177e4SLinus Torvalds  * in the callback. It must also be sure to increment the refcount
4381da177e4SLinus Torvalds  * so it doesn't disappear before returning to the caller.
4391da177e4SLinus Torvalds  */
4401da177e4SLinus Torvalds int bus_for_each_drv(struct bus_type *bus, struct device_driver *start,
4411da177e4SLinus Torvalds 		     void *data, int (*fn)(struct device_driver *, void *))
4421da177e4SLinus Torvalds {
44338fdac3cSmochel@digitalimplant.org 	struct klist_iter i;
44438fdac3cSmochel@digitalimplant.org 	struct device_driver *drv;
44538fdac3cSmochel@digitalimplant.org 	int error = 0;
4461da177e4SLinus Torvalds 
44738fdac3cSmochel@digitalimplant.org 	if (!bus)
44838fdac3cSmochel@digitalimplant.org 		return -EINVAL;
44938fdac3cSmochel@digitalimplant.org 
450c6f7e72aSGreg Kroah-Hartman 	klist_iter_init_node(&bus->p->klist_drivers, &i,
451e5dd1278SGreg Kroah-Hartman 			     start ? &start->p->knode_bus : NULL);
45238fdac3cSmochel@digitalimplant.org 	while ((drv = next_driver(&i)) && !error)
45338fdac3cSmochel@digitalimplant.org 		error = fn(drv, data);
45438fdac3cSmochel@digitalimplant.org 	klist_iter_exit(&i);
45538fdac3cSmochel@digitalimplant.org 	return error;
4561da177e4SLinus Torvalds }
4574a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_for_each_drv);
4581da177e4SLinus Torvalds 
4591da177e4SLinus Torvalds static int device_add_attrs(struct bus_type *bus, struct device *dev)
4601da177e4SLinus Torvalds {
4611da177e4SLinus Torvalds 	int error = 0;
4621da177e4SLinus Torvalds 	int i;
4631da177e4SLinus Torvalds 
4644aca67e5SAndrew Morton 	if (!bus->dev_attrs)
4654aca67e5SAndrew Morton 		return 0;
4664aca67e5SAndrew Morton 
4671da177e4SLinus Torvalds 	for (i = 0; attr_name(bus->dev_attrs[i]); i++) {
4681da177e4SLinus Torvalds 		error = device_create_file(dev, &bus->dev_attrs[i]);
4694aca67e5SAndrew Morton 		if (error) {
4701da177e4SLinus Torvalds 			while (--i >= 0)
4711da177e4SLinus Torvalds 				device_remove_file(dev, &bus->dev_attrs[i]);
4724aca67e5SAndrew Morton 			break;
4731da177e4SLinus Torvalds 		}
4744aca67e5SAndrew Morton 	}
4754aca67e5SAndrew Morton 	return error;
4764aca67e5SAndrew Morton }
4771da177e4SLinus Torvalds 
4781da177e4SLinus Torvalds static void device_remove_attrs(struct bus_type *bus, struct device *dev)
4791da177e4SLinus Torvalds {
4801da177e4SLinus Torvalds 	int i;
4811da177e4SLinus Torvalds 
4821da177e4SLinus Torvalds 	if (bus->dev_attrs) {
4831da177e4SLinus Torvalds 		for (i = 0; attr_name(bus->dev_attrs[i]); i++)
4841da177e4SLinus Torvalds 			device_remove_file(dev, &bus->dev_attrs[i]);
4851da177e4SLinus Torvalds 	}
4861da177e4SLinus Torvalds }
4871da177e4SLinus Torvalds 
4881da177e4SLinus Torvalds /**
4891da177e4SLinus Torvalds  * bus_add_device - add device to bus
4901da177e4SLinus Torvalds  * @dev: device being added
4911da177e4SLinus Torvalds  *
4922023c610SAlan Stern  * - Add device's bus attributes.
4932023c610SAlan Stern  * - Create links to device's bus.
4941da177e4SLinus Torvalds  * - Add the device to its bus's list of devices.
4951da177e4SLinus Torvalds  */
4961da177e4SLinus Torvalds int bus_add_device(struct device *dev)
4971da177e4SLinus Torvalds {
4985901d014SGreg Kroah-Hartman 	struct bus_type *bus = bus_get(dev->bus);
4991da177e4SLinus Torvalds 	int error = 0;
5001da177e4SLinus Torvalds 
5011da177e4SLinus Torvalds 	if (bus) {
5021e0b2cf9SKay Sievers 		pr_debug("bus: '%s': add device %s\n", bus->name, dev_name(dev));
503ca2b94baSHannes Reinecke 		error = device_add_attrs(bus, dev);
504f86db396SAndrew Morton 		if (error)
505513e7337SCornelia Huck 			goto out_put;
506c6f7e72aSGreg Kroah-Hartman 		error = sysfs_create_link(&bus->p->devices_kset->kobj,
5071e0b2cf9SKay Sievers 						&dev->kobj, dev_name(dev));
508f86db396SAndrew Morton 		if (error)
509513e7337SCornelia Huck 			goto out_id;
510f86db396SAndrew Morton 		error = sysfs_create_link(&dev->kobj,
511c6f7e72aSGreg Kroah-Hartman 				&dev->bus->p->subsys.kobj, "subsystem");
512f86db396SAndrew Morton 		if (error)
513513e7337SCornelia Huck 			goto out_subsys;
5142023c610SAlan Stern 		klist_add_tail(&dev->p->knode_bus, &bus->p->klist_devices);
5151da177e4SLinus Torvalds 	}
516513e7337SCornelia Huck 	return 0;
517513e7337SCornelia Huck 
518513e7337SCornelia Huck out_subsys:
5191e0b2cf9SKay Sievers 	sysfs_remove_link(&bus->p->devices_kset->kobj, dev_name(dev));
520513e7337SCornelia Huck out_id:
521513e7337SCornelia Huck 	device_remove_attrs(bus, dev);
522513e7337SCornelia Huck out_put:
523fc1ede58SGreg Kroah-Hartman 	bus_put(dev->bus);
5241da177e4SLinus Torvalds 	return error;
5251da177e4SLinus Torvalds }
5261da177e4SLinus Torvalds 
5271da177e4SLinus Torvalds /**
5282023c610SAlan Stern  * bus_probe_device - probe drivers for a new device
5292023c610SAlan Stern  * @dev: device to probe
53053877d06SKay Sievers  *
5312023c610SAlan Stern  * - Automatically probe for a driver if the bus allows it.
53253877d06SKay Sievers  */
5332023c610SAlan Stern void bus_probe_device(struct device *dev)
53453877d06SKay Sievers {
53553877d06SKay Sievers 	struct bus_type *bus = dev->bus;
536*ca22e56dSKay Sievers 	struct subsys_interface *sif;
5372023c610SAlan Stern 	int ret;
53853877d06SKay Sievers 
539*ca22e56dSKay Sievers 	if (!bus)
540*ca22e56dSKay Sievers 		return;
541*ca22e56dSKay Sievers 
542*ca22e56dSKay Sievers 	if (bus->p->drivers_autoprobe) {
543f86db396SAndrew Morton 		ret = device_attach(dev);
544c6a46696SCornelia Huck 		WARN_ON(ret < 0);
54553877d06SKay Sievers 	}
546*ca22e56dSKay Sievers 
547*ca22e56dSKay Sievers 	mutex_lock(&bus->p->mutex);
548*ca22e56dSKay Sievers 	list_for_each_entry(sif, &bus->p->interfaces, node)
549*ca22e56dSKay Sievers 		if (sif->add_dev)
550*ca22e56dSKay Sievers 			sif->add_dev(dev, sif);
551*ca22e56dSKay Sievers 	mutex_unlock(&bus->p->mutex);
552f86db396SAndrew Morton }
55353877d06SKay Sievers 
55453877d06SKay Sievers /**
5551da177e4SLinus Torvalds  * bus_remove_device - remove device from bus
5561da177e4SLinus Torvalds  * @dev: device to be removed
5571da177e4SLinus Torvalds  *
558*ca22e56dSKay Sievers  * - Remove device from all interfaces.
559*ca22e56dSKay Sievers  * - Remove symlink from bus' directory.
5601da177e4SLinus Torvalds  * - Delete device from bus's list.
5611da177e4SLinus Torvalds  * - Detach from its driver.
5621da177e4SLinus Torvalds  * - Drop reference taken in bus_add_device().
5631da177e4SLinus Torvalds  */
5641da177e4SLinus Torvalds void bus_remove_device(struct device *dev)
5651da177e4SLinus Torvalds {
566*ca22e56dSKay Sievers 	struct bus_type *bus = dev->bus;
567*ca22e56dSKay Sievers 	struct subsys_interface *sif;
568*ca22e56dSKay Sievers 
569*ca22e56dSKay Sievers 	if (!bus)
570*ca22e56dSKay Sievers 		return;
571*ca22e56dSKay Sievers 
572*ca22e56dSKay Sievers 	mutex_lock(&bus->p->mutex);
573*ca22e56dSKay Sievers 	list_for_each_entry(sif, &bus->p->interfaces, node)
574*ca22e56dSKay Sievers 		if (sif->remove_dev)
575*ca22e56dSKay Sievers 			sif->remove_dev(dev, sif);
576*ca22e56dSKay Sievers 	mutex_unlock(&bus->p->mutex);
577*ca22e56dSKay Sievers 
578b9d9c82bSKay Sievers 	sysfs_remove_link(&dev->kobj, "subsystem");
5794a3ad20cSGreg Kroah-Hartman 	sysfs_remove_link(&dev->bus->p->devices_kset->kobj,
5801e0b2cf9SKay Sievers 			  dev_name(dev));
5811da177e4SLinus Torvalds 	device_remove_attrs(dev->bus, dev);
582ae1b4171SGreg Kroah-Hartman 	if (klist_node_attached(&dev->p->knode_bus))
583ae1b4171SGreg Kroah-Hartman 		klist_del(&dev->p->knode_bus);
5843f62e570SGreg Kroah-Hartman 
5854a3ad20cSGreg Kroah-Hartman 	pr_debug("bus: '%s': remove device %s\n",
5861e0b2cf9SKay Sievers 		 dev->bus->name, dev_name(dev));
5871da177e4SLinus Torvalds 	device_release_driver(dev);
588fc1ede58SGreg Kroah-Hartman 	bus_put(dev->bus);
5891da177e4SLinus Torvalds }
5901da177e4SLinus Torvalds 
5911da177e4SLinus Torvalds static int driver_add_attrs(struct bus_type *bus, struct device_driver *drv)
5921da177e4SLinus Torvalds {
5931da177e4SLinus Torvalds 	int error = 0;
5941da177e4SLinus Torvalds 	int i;
5951da177e4SLinus Torvalds 
5961da177e4SLinus Torvalds 	if (bus->drv_attrs) {
5971da177e4SLinus Torvalds 		for (i = 0; attr_name(bus->drv_attrs[i]); i++) {
5981da177e4SLinus Torvalds 			error = driver_create_file(drv, &bus->drv_attrs[i]);
5991da177e4SLinus Torvalds 			if (error)
6004a3ad20cSGreg Kroah-Hartman 				goto err;
6011da177e4SLinus Torvalds 		}
6021da177e4SLinus Torvalds 	}
6034a3ad20cSGreg Kroah-Hartman done:
6041da177e4SLinus Torvalds 	return error;
6054a3ad20cSGreg Kroah-Hartman err:
6061da177e4SLinus Torvalds 	while (--i >= 0)
6071da177e4SLinus Torvalds 		driver_remove_file(drv, &bus->drv_attrs[i]);
6084a3ad20cSGreg Kroah-Hartman 	goto done;
6091da177e4SLinus Torvalds }
6101da177e4SLinus Torvalds 
6114a3ad20cSGreg Kroah-Hartman static void driver_remove_attrs(struct bus_type *bus,
6124a3ad20cSGreg Kroah-Hartman 				struct device_driver *drv)
6131da177e4SLinus Torvalds {
6141da177e4SLinus Torvalds 	int i;
6151da177e4SLinus Torvalds 
6161da177e4SLinus Torvalds 	if (bus->drv_attrs) {
6171da177e4SLinus Torvalds 		for (i = 0; attr_name(bus->drv_attrs[i]); i++)
6181da177e4SLinus Torvalds 			driver_remove_file(drv, &bus->drv_attrs[i]);
6191da177e4SLinus Torvalds 	}
6201da177e4SLinus Torvalds }
6211da177e4SLinus Torvalds 
622874c6241SGreg Kroah-Hartman #ifdef CONFIG_HOTPLUG
623874c6241SGreg Kroah-Hartman /*
624874c6241SGreg Kroah-Hartman  * Thanks to drivers making their tables __devinit, we can't allow manual
625874c6241SGreg Kroah-Hartman  * bind and unbind from userspace unless CONFIG_HOTPLUG is enabled.
626874c6241SGreg Kroah-Hartman  */
627f86db396SAndrew Morton static int __must_check add_bind_files(struct device_driver *drv)
628874c6241SGreg Kroah-Hartman {
629f86db396SAndrew Morton 	int ret;
630f86db396SAndrew Morton 
631f86db396SAndrew Morton 	ret = driver_create_file(drv, &driver_attr_unbind);
632f86db396SAndrew Morton 	if (ret == 0) {
633f86db396SAndrew Morton 		ret = driver_create_file(drv, &driver_attr_bind);
634f86db396SAndrew Morton 		if (ret)
635f86db396SAndrew Morton 			driver_remove_file(drv, &driver_attr_unbind);
636f86db396SAndrew Morton 	}
637f86db396SAndrew Morton 	return ret;
638874c6241SGreg Kroah-Hartman }
639874c6241SGreg Kroah-Hartman 
640874c6241SGreg Kroah-Hartman static void remove_bind_files(struct device_driver *drv)
641874c6241SGreg Kroah-Hartman {
642874c6241SGreg Kroah-Hartman 	driver_remove_file(drv, &driver_attr_bind);
643874c6241SGreg Kroah-Hartman 	driver_remove_file(drv, &driver_attr_unbind);
644874c6241SGreg Kroah-Hartman }
645b8c5cec2SKay Sievers 
6468380770cSKay Sievers static BUS_ATTR(drivers_probe, S_IWUSR, NULL, store_drivers_probe);
6478380770cSKay Sievers static BUS_ATTR(drivers_autoprobe, S_IWUSR | S_IRUGO,
6488380770cSKay Sievers 		show_drivers_autoprobe, store_drivers_autoprobe);
6498380770cSKay Sievers 
650b8c5cec2SKay Sievers static int add_probe_files(struct bus_type *bus)
651b8c5cec2SKay Sievers {
652b8c5cec2SKay Sievers 	int retval;
653b8c5cec2SKay Sievers 
6548380770cSKay Sievers 	retval = bus_create_file(bus, &bus_attr_drivers_probe);
655b8c5cec2SKay Sievers 	if (retval)
656b8c5cec2SKay Sievers 		goto out;
657b8c5cec2SKay Sievers 
6588380770cSKay Sievers 	retval = bus_create_file(bus, &bus_attr_drivers_autoprobe);
659b8c5cec2SKay Sievers 	if (retval)
6608380770cSKay Sievers 		bus_remove_file(bus, &bus_attr_drivers_probe);
661b8c5cec2SKay Sievers out:
662b8c5cec2SKay Sievers 	return retval;
663b8c5cec2SKay Sievers }
664b8c5cec2SKay Sievers 
665b8c5cec2SKay Sievers static void remove_probe_files(struct bus_type *bus)
666b8c5cec2SKay Sievers {
6678380770cSKay Sievers 	bus_remove_file(bus, &bus_attr_drivers_autoprobe);
6688380770cSKay Sievers 	bus_remove_file(bus, &bus_attr_drivers_probe);
669b8c5cec2SKay Sievers }
670874c6241SGreg Kroah-Hartman #else
67135acfdd7SYoichi Yuasa static inline int add_bind_files(struct device_driver *drv) { return 0; }
672874c6241SGreg Kroah-Hartman static inline void remove_bind_files(struct device_driver *drv) {}
673b8c5cec2SKay Sievers static inline int add_probe_files(struct bus_type *bus) { return 0; }
674b8c5cec2SKay Sievers static inline void remove_probe_files(struct bus_type *bus) {}
675874c6241SGreg Kroah-Hartman #endif
6761da177e4SLinus Torvalds 
6777ac1cf4aSKay Sievers static ssize_t driver_uevent_store(struct device_driver *drv,
6787ac1cf4aSKay Sievers 				   const char *buf, size_t count)
6797ac1cf4aSKay Sievers {
6807ac1cf4aSKay Sievers 	enum kobject_action action;
6817ac1cf4aSKay Sievers 
6827ac1cf4aSKay Sievers 	if (kobject_action_type(buf, count, &action) == 0)
683e5dd1278SGreg Kroah-Hartman 		kobject_uevent(&drv->p->kobj, action);
6847ac1cf4aSKay Sievers 	return count;
6857ac1cf4aSKay Sievers }
6867ac1cf4aSKay Sievers static DRIVER_ATTR(uevent, S_IWUSR, NULL, driver_uevent_store);
6877ac1cf4aSKay Sievers 
6881da177e4SLinus Torvalds /**
6891da177e4SLinus Torvalds  * bus_add_driver - Add a driver to the bus.
6901da177e4SLinus Torvalds  * @drv: driver.
6911da177e4SLinus Torvalds  */
6921da177e4SLinus Torvalds int bus_add_driver(struct device_driver *drv)
6931da177e4SLinus Torvalds {
694e5dd1278SGreg Kroah-Hartman 	struct bus_type *bus;
695e5dd1278SGreg Kroah-Hartman 	struct driver_private *priv;
6961da177e4SLinus Torvalds 	int error = 0;
6971da177e4SLinus Torvalds 
698e5dd1278SGreg Kroah-Hartman 	bus = bus_get(drv->bus);
699d9fd4d3bSJeff Garzik 	if (!bus)
7004f6e1945SGreg Kroah-Hartman 		return -EINVAL;
701d9fd4d3bSJeff Garzik 
7027dc72b28SGreg Kroah-Hartman 	pr_debug("bus: '%s': add driver %s\n", bus->name, drv->name);
703e5dd1278SGreg Kroah-Hartman 
704e5dd1278SGreg Kroah-Hartman 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
70507634464SCornelia Huck 	if (!priv) {
70607634464SCornelia Huck 		error = -ENOMEM;
70707634464SCornelia Huck 		goto out_put_bus;
70807634464SCornelia Huck 	}
709e5dd1278SGreg Kroah-Hartman 	klist_init(&priv->klist_devices, NULL, NULL);
710e5dd1278SGreg Kroah-Hartman 	priv->driver = drv;
711e5dd1278SGreg Kroah-Hartman 	drv->p = priv;
712c8e90d82SGreg Kroah-Hartman 	priv->kobj.kset = bus->p->drivers_kset;
713c8e90d82SGreg Kroah-Hartman 	error = kobject_init_and_add(&priv->kobj, &driver_ktype, NULL,
714c8e90d82SGreg Kroah-Hartman 				     "%s", drv->name);
715dc0afa83SCornelia Huck 	if (error)
71607634464SCornelia Huck 		goto out_unregister;
7171da177e4SLinus Torvalds 
718c6f7e72aSGreg Kroah-Hartman 	if (drv->bus->p->drivers_autoprobe) {
719f86db396SAndrew Morton 		error = driver_attach(drv);
720f86db396SAndrew Morton 		if (error)
721f86db396SAndrew Morton 			goto out_unregister;
722b8c5cec2SKay Sievers 	}
723e5dd1278SGreg Kroah-Hartman 	klist_add_tail(&priv->knode_bus, &bus->p->klist_drivers);
7241da177e4SLinus Torvalds 	module_add_driver(drv->owner, drv);
7251da177e4SLinus Torvalds 
7267ac1cf4aSKay Sievers 	error = driver_create_file(drv, &driver_attr_uevent);
7277ac1cf4aSKay Sievers 	if (error) {
7287ac1cf4aSKay Sievers 		printk(KERN_ERR "%s: uevent attr (%s) failed\n",
7292b3a302aSHarvey Harrison 			__func__, drv->name);
7307ac1cf4aSKay Sievers 	}
731f86db396SAndrew Morton 	error = driver_add_attrs(bus, drv);
732f86db396SAndrew Morton 	if (error) {
733f86db396SAndrew Morton 		/* How the hell do we get out of this pickle? Give up */
734f86db396SAndrew Morton 		printk(KERN_ERR "%s: driver_add_attrs(%s) failed\n",
7352b3a302aSHarvey Harrison 			__func__, drv->name);
736f86db396SAndrew Morton 	}
7371a6f2a75SDmitry Torokhov 
7381a6f2a75SDmitry Torokhov 	if (!drv->suppress_bind_attrs) {
739f86db396SAndrew Morton 		error = add_bind_files(drv);
740f86db396SAndrew Morton 		if (error) {
741f86db396SAndrew Morton 			/* Ditto */
742f86db396SAndrew Morton 			printk(KERN_ERR "%s: add_bind_files(%s) failed\n",
7432b3a302aSHarvey Harrison 				__func__, drv->name);
744f86db396SAndrew Morton 		}
7451a6f2a75SDmitry Torokhov 	}
746d9fd4d3bSJeff Garzik 
747c8e90d82SGreg Kroah-Hartman 	kobject_uevent(&priv->kobj, KOBJ_ADD);
7485c8563d7SKay Sievers 	return 0;
7491a6f2a75SDmitry Torokhov 
750f86db396SAndrew Morton out_unregister:
75199b28f1bSPhil Carmody 	kobject_put(&priv->kobj);
7525c8563d7SKay Sievers 	kfree(drv->p);
7535c8563d7SKay Sievers 	drv->p = NULL;
754f86db396SAndrew Morton out_put_bus:
755fc1ede58SGreg Kroah-Hartman 	bus_put(bus);
756f86db396SAndrew Morton 	return error;
7571da177e4SLinus Torvalds }
7581da177e4SLinus Torvalds 
7591da177e4SLinus Torvalds /**
7601da177e4SLinus Torvalds  * bus_remove_driver - delete driver from bus's knowledge.
7611da177e4SLinus Torvalds  * @drv: driver.
7621da177e4SLinus Torvalds  *
7631da177e4SLinus Torvalds  * Detach the driver from the devices it controls, and remove
7641da177e4SLinus Torvalds  * it from its bus's list of drivers. Finally, we drop the reference
7651da177e4SLinus Torvalds  * to the bus we took in bus_add_driver().
7661da177e4SLinus Torvalds  */
7671da177e4SLinus Torvalds void bus_remove_driver(struct device_driver *drv)
7681da177e4SLinus Torvalds {
769d9fd4d3bSJeff Garzik 	if (!drv->bus)
770d9fd4d3bSJeff Garzik 		return;
771d9fd4d3bSJeff Garzik 
7721a6f2a75SDmitry Torokhov 	if (!drv->suppress_bind_attrs)
773874c6241SGreg Kroah-Hartman 		remove_bind_files(drv);
7741da177e4SLinus Torvalds 	driver_remove_attrs(drv->bus, drv);
7757ac1cf4aSKay Sievers 	driver_remove_file(drv, &driver_attr_uevent);
776e5dd1278SGreg Kroah-Hartman 	klist_remove(&drv->p->knode_bus);
7777dc72b28SGreg Kroah-Hartman 	pr_debug("bus: '%s': remove driver %s\n", drv->bus->name, drv->name);
7781da177e4SLinus Torvalds 	driver_detach(drv);
7791da177e4SLinus Torvalds 	module_remove_driver(drv);
780c10997f6SGreg Kroah-Hartman 	kobject_put(&drv->p->kobj);
781fc1ede58SGreg Kroah-Hartman 	bus_put(drv->bus);
7821da177e4SLinus Torvalds }
7831da177e4SLinus Torvalds 
7841da177e4SLinus Torvalds /* Helper for bus_rescan_devices's iter */
785f86db396SAndrew Morton static int __must_check bus_rescan_devices_helper(struct device *dev,
786f86db396SAndrew Morton 						  void *data)
7871da177e4SLinus Torvalds {
788f86db396SAndrew Morton 	int ret = 0;
789f86db396SAndrew Morton 
790bf74ad5bSAlan Stern 	if (!dev->driver) {
791bf74ad5bSAlan Stern 		if (dev->parent)	/* Needed for USB */
7928e9394ceSGreg Kroah-Hartman 			device_lock(dev->parent);
793f86db396SAndrew Morton 		ret = device_attach(dev);
794bf74ad5bSAlan Stern 		if (dev->parent)
7958e9394ceSGreg Kroah-Hartman 			device_unlock(dev->parent);
796bf74ad5bSAlan Stern 	}
797f86db396SAndrew Morton 	return ret < 0 ? ret : 0;
7981da177e4SLinus Torvalds }
7991da177e4SLinus Torvalds 
8001da177e4SLinus Torvalds /**
8011da177e4SLinus Torvalds  * bus_rescan_devices - rescan devices on the bus for possible drivers
8021da177e4SLinus Torvalds  * @bus: the bus to scan.
8031da177e4SLinus Torvalds  *
8041da177e4SLinus Torvalds  * This function will look for devices on the bus with no driver
80523d3d602SGreg Kroah-Hartman  * attached and rescan it against existing drivers to see if it matches
80623d3d602SGreg Kroah-Hartman  * any by calling device_attach() for the unbound devices.
8071da177e4SLinus Torvalds  */
808f86db396SAndrew Morton int bus_rescan_devices(struct bus_type *bus)
8091da177e4SLinus Torvalds {
810f86db396SAndrew Morton 	return bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper);
8111da177e4SLinus Torvalds }
8124a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_rescan_devices);
8131da177e4SLinus Torvalds 
814e935d5daSMoore, Eric /**
815e935d5daSMoore, Eric  * device_reprobe - remove driver for a device and probe for a new driver
816e935d5daSMoore, Eric  * @dev: the device to reprobe
817e935d5daSMoore, Eric  *
818e935d5daSMoore, Eric  * This function detaches the attached driver (if any) for the given
819e935d5daSMoore, Eric  * device and restarts the driver probing process.  It is intended
820e935d5daSMoore, Eric  * to use if probing criteria changed during a devices lifetime and
821e935d5daSMoore, Eric  * driver attachment should change accordingly.
822e935d5daSMoore, Eric  */
823f86db396SAndrew Morton int device_reprobe(struct device *dev)
824e935d5daSMoore, Eric {
825e935d5daSMoore, Eric 	if (dev->driver) {
826e935d5daSMoore, Eric 		if (dev->parent)        /* Needed for USB */
8278e9394ceSGreg Kroah-Hartman 			device_lock(dev->parent);
828e935d5daSMoore, Eric 		device_release_driver(dev);
829e935d5daSMoore, Eric 		if (dev->parent)
8308e9394ceSGreg Kroah-Hartman 			device_unlock(dev->parent);
831e935d5daSMoore, Eric 	}
832f86db396SAndrew Morton 	return bus_rescan_devices_helper(dev, NULL);
833e935d5daSMoore, Eric }
834e935d5daSMoore, Eric EXPORT_SYMBOL_GPL(device_reprobe);
8351da177e4SLinus Torvalds 
8361da177e4SLinus Torvalds /**
8371da177e4SLinus Torvalds  * find_bus - locate bus by name.
8381da177e4SLinus Torvalds  * @name: name of bus.
8391da177e4SLinus Torvalds  *
8401da177e4SLinus Torvalds  * Call kset_find_obj() to iterate over list of buses to
8411da177e4SLinus Torvalds  * find a bus by name. Return bus if found.
8421da177e4SLinus Torvalds  *
8431da177e4SLinus Torvalds  * Note that kset_find_obj increments bus' reference count.
8441da177e4SLinus Torvalds  */
8457e4ef085SAdrian Bunk #if 0
8461da177e4SLinus Torvalds struct bus_type *find_bus(char *name)
8471da177e4SLinus Torvalds {
84859a54833SGreg Kroah-Hartman 	struct kobject *k = kset_find_obj(bus_kset, name);
8491da177e4SLinus Torvalds 	return k ? to_bus(k) : NULL;
8501da177e4SLinus Torvalds }
8517e4ef085SAdrian Bunk #endif  /*  0  */
8521da177e4SLinus Torvalds 
8531da177e4SLinus Torvalds 
8541da177e4SLinus Torvalds /**
8551da177e4SLinus Torvalds  * bus_add_attrs - Add default attributes for this bus.
8561da177e4SLinus Torvalds  * @bus: Bus that has just been registered.
8571da177e4SLinus Torvalds  */
8581da177e4SLinus Torvalds 
8591da177e4SLinus Torvalds static int bus_add_attrs(struct bus_type *bus)
8601da177e4SLinus Torvalds {
8611da177e4SLinus Torvalds 	int error = 0;
8621da177e4SLinus Torvalds 	int i;
8631da177e4SLinus Torvalds 
8641da177e4SLinus Torvalds 	if (bus->bus_attrs) {
8651da177e4SLinus Torvalds 		for (i = 0; attr_name(bus->bus_attrs[i]); i++) {
866dc0afa83SCornelia Huck 			error = bus_create_file(bus, &bus->bus_attrs[i]);
867dc0afa83SCornelia Huck 			if (error)
8684a3ad20cSGreg Kroah-Hartman 				goto err;
8691da177e4SLinus Torvalds 		}
8701da177e4SLinus Torvalds 	}
8714a3ad20cSGreg Kroah-Hartman done:
8721da177e4SLinus Torvalds 	return error;
8734a3ad20cSGreg Kroah-Hartman err:
8741da177e4SLinus Torvalds 	while (--i >= 0)
8751da177e4SLinus Torvalds 		bus_remove_file(bus, &bus->bus_attrs[i]);
8764a3ad20cSGreg Kroah-Hartman 	goto done;
8771da177e4SLinus Torvalds }
8781da177e4SLinus Torvalds 
8791da177e4SLinus Torvalds static void bus_remove_attrs(struct bus_type *bus)
8801da177e4SLinus Torvalds {
8811da177e4SLinus Torvalds 	int i;
8821da177e4SLinus Torvalds 
8831da177e4SLinus Torvalds 	if (bus->bus_attrs) {
8841da177e4SLinus Torvalds 		for (i = 0; attr_name(bus->bus_attrs[i]); i++)
8851da177e4SLinus Torvalds 			bus_remove_file(bus, &bus->bus_attrs[i]);
8861da177e4SLinus Torvalds 	}
8871da177e4SLinus Torvalds }
8881da177e4SLinus Torvalds 
88934bb61f9SJames Bottomley static void klist_devices_get(struct klist_node *n)
89034bb61f9SJames Bottomley {
891ae1b4171SGreg Kroah-Hartman 	struct device_private *dev_prv = to_device_private_bus(n);
892ae1b4171SGreg Kroah-Hartman 	struct device *dev = dev_prv->device;
89334bb61f9SJames Bottomley 
89434bb61f9SJames Bottomley 	get_device(dev);
89534bb61f9SJames Bottomley }
89634bb61f9SJames Bottomley 
89734bb61f9SJames Bottomley static void klist_devices_put(struct klist_node *n)
89834bb61f9SJames Bottomley {
899ae1b4171SGreg Kroah-Hartman 	struct device_private *dev_prv = to_device_private_bus(n);
900ae1b4171SGreg Kroah-Hartman 	struct device *dev = dev_prv->device;
90134bb61f9SJames Bottomley 
90234bb61f9SJames Bottomley 	put_device(dev);
90334bb61f9SJames Bottomley }
90434bb61f9SJames Bottomley 
9057ac1cf4aSKay Sievers static ssize_t bus_uevent_store(struct bus_type *bus,
9067ac1cf4aSKay Sievers 				const char *buf, size_t count)
9077ac1cf4aSKay Sievers {
9087ac1cf4aSKay Sievers 	enum kobject_action action;
9097ac1cf4aSKay Sievers 
9107ac1cf4aSKay Sievers 	if (kobject_action_type(buf, count, &action) == 0)
911c6f7e72aSGreg Kroah-Hartman 		kobject_uevent(&bus->p->subsys.kobj, action);
9127ac1cf4aSKay Sievers 	return count;
9137ac1cf4aSKay Sievers }
9147ac1cf4aSKay Sievers static BUS_ATTR(uevent, S_IWUSR, NULL, bus_uevent_store);
9157ac1cf4aSKay Sievers 
9161da177e4SLinus Torvalds /**
917*ca22e56dSKay Sievers  * __bus_register - register a driver-core subsystem
9181da177e4SLinus Torvalds  * @bus: bus.
9191da177e4SLinus Torvalds  *
9201da177e4SLinus Torvalds  * Once we have that, we registered the bus with the kobject
9211da177e4SLinus Torvalds  * infrastructure, then register the children subsystems it has:
922*ca22e56dSKay Sievers  * the devices and drivers that belong to the subsystem.
9231da177e4SLinus Torvalds  */
924*ca22e56dSKay Sievers int __bus_register(struct bus_type *bus, struct lock_class_key *key)
9251da177e4SLinus Torvalds {
9261da177e4SLinus Torvalds 	int retval;
9276b6e39a6SKay Sievers 	struct subsys_private *priv;
9281da177e4SLinus Torvalds 
9296b6e39a6SKay Sievers 	priv = kzalloc(sizeof(struct subsys_private), GFP_KERNEL);
930c6f7e72aSGreg Kroah-Hartman 	if (!priv)
931c6f7e72aSGreg Kroah-Hartman 		return -ENOMEM;
932116af378SBenjamin Herrenschmidt 
933c6f7e72aSGreg Kroah-Hartman 	priv->bus = bus;
934c6f7e72aSGreg Kroah-Hartman 	bus->p = priv;
935c6f7e72aSGreg Kroah-Hartman 
936c6f7e72aSGreg Kroah-Hartman 	BLOCKING_INIT_NOTIFIER_HEAD(&priv->bus_notifier);
937c6f7e72aSGreg Kroah-Hartman 
938c6f7e72aSGreg Kroah-Hartman 	retval = kobject_set_name(&priv->subsys.kobj, "%s", bus->name);
9391da177e4SLinus Torvalds 	if (retval)
9401da177e4SLinus Torvalds 		goto out;
9411da177e4SLinus Torvalds 
942c6f7e72aSGreg Kroah-Hartman 	priv->subsys.kobj.kset = bus_kset;
943c6f7e72aSGreg Kroah-Hartman 	priv->subsys.kobj.ktype = &bus_ktype;
944c6f7e72aSGreg Kroah-Hartman 	priv->drivers_autoprobe = 1;
945d6b05b84SGreg Kroah-Hartman 
946c6f7e72aSGreg Kroah-Hartman 	retval = kset_register(&priv->subsys);
9471da177e4SLinus Torvalds 	if (retval)
9481da177e4SLinus Torvalds 		goto out;
9491da177e4SLinus Torvalds 
9507ac1cf4aSKay Sievers 	retval = bus_create_file(bus, &bus_attr_uevent);
9517ac1cf4aSKay Sievers 	if (retval)
9527ac1cf4aSKay Sievers 		goto bus_uevent_fail;
9537ac1cf4aSKay Sievers 
954c6f7e72aSGreg Kroah-Hartman 	priv->devices_kset = kset_create_and_add("devices", NULL,
955c6f7e72aSGreg Kroah-Hartman 						 &priv->subsys.kobj);
956c6f7e72aSGreg Kroah-Hartman 	if (!priv->devices_kset) {
9573d899596SGreg Kroah-Hartman 		retval = -ENOMEM;
9581da177e4SLinus Torvalds 		goto bus_devices_fail;
9593d899596SGreg Kroah-Hartman 	}
9601da177e4SLinus Torvalds 
961c6f7e72aSGreg Kroah-Hartman 	priv->drivers_kset = kset_create_and_add("drivers", NULL,
962c6f7e72aSGreg Kroah-Hartman 						 &priv->subsys.kobj);
963c6f7e72aSGreg Kroah-Hartman 	if (!priv->drivers_kset) {
9646dcec251SGreg Kroah-Hartman 		retval = -ENOMEM;
9651da177e4SLinus Torvalds 		goto bus_drivers_fail;
9666dcec251SGreg Kroah-Hartman 	}
967465c7a3aSmochel@digitalimplant.org 
968*ca22e56dSKay Sievers 	INIT_LIST_HEAD(&priv->interfaces);
969*ca22e56dSKay Sievers 	__mutex_init(&priv->mutex, "subsys mutex", key);
970c6f7e72aSGreg Kroah-Hartman 	klist_init(&priv->klist_devices, klist_devices_get, klist_devices_put);
971c6f7e72aSGreg Kroah-Hartman 	klist_init(&priv->klist_drivers, NULL, NULL);
972b8c5cec2SKay Sievers 
973b8c5cec2SKay Sievers 	retval = add_probe_files(bus);
974b8c5cec2SKay Sievers 	if (retval)
975b8c5cec2SKay Sievers 		goto bus_probe_files_fail;
976b8c5cec2SKay Sievers 
9771bb6881aSCornelia Huck 	retval = bus_add_attrs(bus);
9781bb6881aSCornelia Huck 	if (retval)
9791bb6881aSCornelia Huck 		goto bus_attrs_fail;
9801da177e4SLinus Torvalds 
9817dc72b28SGreg Kroah-Hartman 	pr_debug("bus: '%s': registered\n", bus->name);
9821da177e4SLinus Torvalds 	return 0;
9831da177e4SLinus Torvalds 
9841bb6881aSCornelia Huck bus_attrs_fail:
985b8c5cec2SKay Sievers 	remove_probe_files(bus);
986b8c5cec2SKay Sievers bus_probe_files_fail:
987c6f7e72aSGreg Kroah-Hartman 	kset_unregister(bus->p->drivers_kset);
9881da177e4SLinus Torvalds bus_drivers_fail:
989c6f7e72aSGreg Kroah-Hartman 	kset_unregister(bus->p->devices_kset);
9901da177e4SLinus Torvalds bus_devices_fail:
9917ac1cf4aSKay Sievers 	bus_remove_file(bus, &bus_attr_uevent);
9927ac1cf4aSKay Sievers bus_uevent_fail:
993c6f7e72aSGreg Kroah-Hartman 	kset_unregister(&bus->p->subsys);
9941da177e4SLinus Torvalds out:
995600c20f3SJike Song 	kfree(bus->p);
996f48f3febSDave Young 	bus->p = NULL;
9971da177e4SLinus Torvalds 	return retval;
9981da177e4SLinus Torvalds }
999*ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(__bus_register);
10001da177e4SLinus Torvalds 
10011da177e4SLinus Torvalds /**
10021da177e4SLinus Torvalds  * bus_unregister - remove a bus from the system
10031da177e4SLinus Torvalds  * @bus: bus.
10041da177e4SLinus Torvalds  *
10051da177e4SLinus Torvalds  * Unregister the child subsystems and the bus itself.
1006fc1ede58SGreg Kroah-Hartman  * Finally, we call bus_put() to release the refcount
10071da177e4SLinus Torvalds  */
10081da177e4SLinus Torvalds void bus_unregister(struct bus_type *bus)
10091da177e4SLinus Torvalds {
10107dc72b28SGreg Kroah-Hartman 	pr_debug("bus: '%s': unregistering\n", bus->name);
1011*ca22e56dSKay Sievers 	if (bus->dev_root)
1012*ca22e56dSKay Sievers 		device_unregister(bus->dev_root);
10131da177e4SLinus Torvalds 	bus_remove_attrs(bus);
1014b8c5cec2SKay Sievers 	remove_probe_files(bus);
1015c6f7e72aSGreg Kroah-Hartman 	kset_unregister(bus->p->drivers_kset);
1016c6f7e72aSGreg Kroah-Hartman 	kset_unregister(bus->p->devices_kset);
10177ac1cf4aSKay Sievers 	bus_remove_file(bus, &bus_attr_uevent);
1018c6f7e72aSGreg Kroah-Hartman 	kset_unregister(&bus->p->subsys);
1019c6f7e72aSGreg Kroah-Hartman 	kfree(bus->p);
1020f48f3febSDave Young 	bus->p = NULL;
10211da177e4SLinus Torvalds }
10224a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_unregister);
10231da177e4SLinus Torvalds 
1024116af378SBenjamin Herrenschmidt int bus_register_notifier(struct bus_type *bus, struct notifier_block *nb)
1025116af378SBenjamin Herrenschmidt {
1026c6f7e72aSGreg Kroah-Hartman 	return blocking_notifier_chain_register(&bus->p->bus_notifier, nb);
1027116af378SBenjamin Herrenschmidt }
1028116af378SBenjamin Herrenschmidt EXPORT_SYMBOL_GPL(bus_register_notifier);
1029116af378SBenjamin Herrenschmidt 
1030116af378SBenjamin Herrenschmidt int bus_unregister_notifier(struct bus_type *bus, struct notifier_block *nb)
1031116af378SBenjamin Herrenschmidt {
1032c6f7e72aSGreg Kroah-Hartman 	return blocking_notifier_chain_unregister(&bus->p->bus_notifier, nb);
1033116af378SBenjamin Herrenschmidt }
1034116af378SBenjamin Herrenschmidt EXPORT_SYMBOL_GPL(bus_unregister_notifier);
1035116af378SBenjamin Herrenschmidt 
10360fed80f7SGreg Kroah-Hartman struct kset *bus_get_kset(struct bus_type *bus)
10370fed80f7SGreg Kroah-Hartman {
1038c6f7e72aSGreg Kroah-Hartman 	return &bus->p->subsys;
10390fed80f7SGreg Kroah-Hartman }
10400fed80f7SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_get_kset);
10410fed80f7SGreg Kroah-Hartman 
1042b249072eSGreg Kroah-Hartman struct klist *bus_get_device_klist(struct bus_type *bus)
1043b249072eSGreg Kroah-Hartman {
1044c6f7e72aSGreg Kroah-Hartman 	return &bus->p->klist_devices;
1045b249072eSGreg Kroah-Hartman }
1046b249072eSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_get_device_klist);
1047b249072eSGreg Kroah-Hartman 
104899178b03SGreg Kroah-Hartman /*
1049dca25ebdSRobert P. J. Day  * Yes, this forcibly breaks the klist abstraction temporarily.  It
105099178b03SGreg Kroah-Hartman  * just wants to sort the klist, not change reference counts and
105199178b03SGreg Kroah-Hartman  * take/drop locks rapidly in the process.  It does all this while
105299178b03SGreg Kroah-Hartman  * holding the lock for the list, so objects can't otherwise be
105399178b03SGreg Kroah-Hartman  * added/removed while we're swizzling.
105499178b03SGreg Kroah-Hartman  */
105599178b03SGreg Kroah-Hartman static void device_insertion_sort_klist(struct device *a, struct list_head *list,
105699178b03SGreg Kroah-Hartman 					int (*compare)(const struct device *a,
105799178b03SGreg Kroah-Hartman 							const struct device *b))
105899178b03SGreg Kroah-Hartman {
105999178b03SGreg Kroah-Hartman 	struct list_head *pos;
106099178b03SGreg Kroah-Hartman 	struct klist_node *n;
1061ae1b4171SGreg Kroah-Hartman 	struct device_private *dev_prv;
106299178b03SGreg Kroah-Hartman 	struct device *b;
106399178b03SGreg Kroah-Hartman 
106499178b03SGreg Kroah-Hartman 	list_for_each(pos, list) {
106599178b03SGreg Kroah-Hartman 		n = container_of(pos, struct klist_node, n_node);
1066ae1b4171SGreg Kroah-Hartman 		dev_prv = to_device_private_bus(n);
1067ae1b4171SGreg Kroah-Hartman 		b = dev_prv->device;
106899178b03SGreg Kroah-Hartman 		if (compare(a, b) <= 0) {
1069ae1b4171SGreg Kroah-Hartman 			list_move_tail(&a->p->knode_bus.n_node,
1070ae1b4171SGreg Kroah-Hartman 				       &b->p->knode_bus.n_node);
107199178b03SGreg Kroah-Hartman 			return;
107299178b03SGreg Kroah-Hartman 		}
107399178b03SGreg Kroah-Hartman 	}
1074ae1b4171SGreg Kroah-Hartman 	list_move_tail(&a->p->knode_bus.n_node, list);
107599178b03SGreg Kroah-Hartman }
107699178b03SGreg Kroah-Hartman 
107799178b03SGreg Kroah-Hartman void bus_sort_breadthfirst(struct bus_type *bus,
107899178b03SGreg Kroah-Hartman 			   int (*compare)(const struct device *a,
107999178b03SGreg Kroah-Hartman 					  const struct device *b))
108099178b03SGreg Kroah-Hartman {
108199178b03SGreg Kroah-Hartman 	LIST_HEAD(sorted_devices);
108299178b03SGreg Kroah-Hartman 	struct list_head *pos, *tmp;
108399178b03SGreg Kroah-Hartman 	struct klist_node *n;
1084ae1b4171SGreg Kroah-Hartman 	struct device_private *dev_prv;
108599178b03SGreg Kroah-Hartman 	struct device *dev;
108699178b03SGreg Kroah-Hartman 	struct klist *device_klist;
108799178b03SGreg Kroah-Hartman 
108899178b03SGreg Kroah-Hartman 	device_klist = bus_get_device_klist(bus);
108999178b03SGreg Kroah-Hartman 
109099178b03SGreg Kroah-Hartman 	spin_lock(&device_klist->k_lock);
109199178b03SGreg Kroah-Hartman 	list_for_each_safe(pos, tmp, &device_klist->k_list) {
109299178b03SGreg Kroah-Hartman 		n = container_of(pos, struct klist_node, n_node);
1093ae1b4171SGreg Kroah-Hartman 		dev_prv = to_device_private_bus(n);
1094ae1b4171SGreg Kroah-Hartman 		dev = dev_prv->device;
109599178b03SGreg Kroah-Hartman 		device_insertion_sort_klist(dev, &sorted_devices, compare);
109699178b03SGreg Kroah-Hartman 	}
109799178b03SGreg Kroah-Hartman 	list_splice(&sorted_devices, &device_klist->k_list);
109899178b03SGreg Kroah-Hartman 	spin_unlock(&device_klist->k_lock);
109999178b03SGreg Kroah-Hartman }
110099178b03SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_sort_breadthfirst);
110199178b03SGreg Kroah-Hartman 
1102*ca22e56dSKay Sievers /**
1103*ca22e56dSKay Sievers  * subsys_dev_iter_init - initialize subsys device iterator
1104*ca22e56dSKay Sievers  * @iter: subsys iterator to initialize
1105*ca22e56dSKay Sievers  * @subsys: the subsys we wanna iterate over
1106*ca22e56dSKay Sievers  * @start: the device to start iterating from, if any
1107*ca22e56dSKay Sievers  * @type: device_type of the devices to iterate over, NULL for all
1108*ca22e56dSKay Sievers  *
1109*ca22e56dSKay Sievers  * Initialize subsys iterator @iter such that it iterates over devices
1110*ca22e56dSKay Sievers  * of @subsys.  If @start is set, the list iteration will start there,
1111*ca22e56dSKay Sievers  * otherwise if it is NULL, the iteration starts at the beginning of
1112*ca22e56dSKay Sievers  * the list.
1113*ca22e56dSKay Sievers  */
1114*ca22e56dSKay Sievers void subsys_dev_iter_init(struct subsys_dev_iter *iter, struct bus_type *subsys,
1115*ca22e56dSKay Sievers 			  struct device *start, const struct device_type *type)
1116*ca22e56dSKay Sievers {
1117*ca22e56dSKay Sievers 	struct klist_node *start_knode = NULL;
1118*ca22e56dSKay Sievers 
1119*ca22e56dSKay Sievers 	if (start)
1120*ca22e56dSKay Sievers 		start_knode = &start->p->knode_bus;
1121*ca22e56dSKay Sievers 	klist_iter_init_node(&subsys->p->klist_devices, &iter->ki, start_knode);
1122*ca22e56dSKay Sievers 	iter->type = type;
1123*ca22e56dSKay Sievers }
1124*ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_dev_iter_init);
1125*ca22e56dSKay Sievers 
1126*ca22e56dSKay Sievers /**
1127*ca22e56dSKay Sievers  * subsys_dev_iter_next - iterate to the next device
1128*ca22e56dSKay Sievers  * @iter: subsys iterator to proceed
1129*ca22e56dSKay Sievers  *
1130*ca22e56dSKay Sievers  * Proceed @iter to the next device and return it.  Returns NULL if
1131*ca22e56dSKay Sievers  * iteration is complete.
1132*ca22e56dSKay Sievers  *
1133*ca22e56dSKay Sievers  * The returned device is referenced and won't be released till
1134*ca22e56dSKay Sievers  * iterator is proceed to the next device or exited.  The caller is
1135*ca22e56dSKay Sievers  * free to do whatever it wants to do with the device including
1136*ca22e56dSKay Sievers  * calling back into subsys code.
1137*ca22e56dSKay Sievers  */
1138*ca22e56dSKay Sievers struct device *subsys_dev_iter_next(struct subsys_dev_iter *iter)
1139*ca22e56dSKay Sievers {
1140*ca22e56dSKay Sievers 	struct klist_node *knode;
1141*ca22e56dSKay Sievers 	struct device *dev;
1142*ca22e56dSKay Sievers 
1143*ca22e56dSKay Sievers 	for (;;) {
1144*ca22e56dSKay Sievers 		knode = klist_next(&iter->ki);
1145*ca22e56dSKay Sievers 		if (!knode)
1146*ca22e56dSKay Sievers 			return NULL;
1147*ca22e56dSKay Sievers 		dev = container_of(knode, struct device_private, knode_bus)->device;
1148*ca22e56dSKay Sievers 		if (!iter->type || iter->type == dev->type)
1149*ca22e56dSKay Sievers 			return dev;
1150*ca22e56dSKay Sievers 	}
1151*ca22e56dSKay Sievers }
1152*ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_dev_iter_next);
1153*ca22e56dSKay Sievers 
1154*ca22e56dSKay Sievers /**
1155*ca22e56dSKay Sievers  * subsys_dev_iter_exit - finish iteration
1156*ca22e56dSKay Sievers  * @iter: subsys iterator to finish
1157*ca22e56dSKay Sievers  *
1158*ca22e56dSKay Sievers  * Finish an iteration.  Always call this function after iteration is
1159*ca22e56dSKay Sievers  * complete whether the iteration ran till the end or not.
1160*ca22e56dSKay Sievers  */
1161*ca22e56dSKay Sievers void subsys_dev_iter_exit(struct subsys_dev_iter *iter)
1162*ca22e56dSKay Sievers {
1163*ca22e56dSKay Sievers 	klist_iter_exit(&iter->ki);
1164*ca22e56dSKay Sievers }
1165*ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_dev_iter_exit);
1166*ca22e56dSKay Sievers 
1167*ca22e56dSKay Sievers int subsys_interface_register(struct subsys_interface *sif)
1168*ca22e56dSKay Sievers {
1169*ca22e56dSKay Sievers 	struct bus_type *subsys;
1170*ca22e56dSKay Sievers 	struct subsys_dev_iter iter;
1171*ca22e56dSKay Sievers 	struct device *dev;
1172*ca22e56dSKay Sievers 
1173*ca22e56dSKay Sievers 	if (!sif || !sif->subsys)
1174*ca22e56dSKay Sievers 		return -ENODEV;
1175*ca22e56dSKay Sievers 
1176*ca22e56dSKay Sievers 	subsys = bus_get(sif->subsys);
1177*ca22e56dSKay Sievers 	if (!subsys)
1178*ca22e56dSKay Sievers 		return -EINVAL;
1179*ca22e56dSKay Sievers 
1180*ca22e56dSKay Sievers 	mutex_lock(&subsys->p->mutex);
1181*ca22e56dSKay Sievers 	list_add_tail(&sif->node, &subsys->p->interfaces);
1182*ca22e56dSKay Sievers 	if (sif->add_dev) {
1183*ca22e56dSKay Sievers 		subsys_dev_iter_init(&iter, subsys, NULL, NULL);
1184*ca22e56dSKay Sievers 		while ((dev = subsys_dev_iter_next(&iter)))
1185*ca22e56dSKay Sievers 			sif->add_dev(dev, sif);
1186*ca22e56dSKay Sievers 		subsys_dev_iter_exit(&iter);
1187*ca22e56dSKay Sievers 	}
1188*ca22e56dSKay Sievers 	mutex_unlock(&subsys->p->mutex);
1189*ca22e56dSKay Sievers 
1190*ca22e56dSKay Sievers 	return 0;
1191*ca22e56dSKay Sievers }
1192*ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_interface_register);
1193*ca22e56dSKay Sievers 
1194*ca22e56dSKay Sievers void subsys_interface_unregister(struct subsys_interface *sif)
1195*ca22e56dSKay Sievers {
1196*ca22e56dSKay Sievers 	struct bus_type *subsys = sif->subsys;
1197*ca22e56dSKay Sievers 	struct subsys_dev_iter iter;
1198*ca22e56dSKay Sievers 	struct device *dev;
1199*ca22e56dSKay Sievers 
1200*ca22e56dSKay Sievers 	if (!sif)
1201*ca22e56dSKay Sievers 		return;
1202*ca22e56dSKay Sievers 
1203*ca22e56dSKay Sievers 	mutex_lock(&subsys->p->mutex);
1204*ca22e56dSKay Sievers 	list_del_init(&sif->node);
1205*ca22e56dSKay Sievers 	if (sif->remove_dev) {
1206*ca22e56dSKay Sievers 		subsys_dev_iter_init(&iter, subsys, NULL, NULL);
1207*ca22e56dSKay Sievers 		while ((dev = subsys_dev_iter_next(&iter)))
1208*ca22e56dSKay Sievers 			sif->remove_dev(dev, sif);
1209*ca22e56dSKay Sievers 		subsys_dev_iter_exit(&iter);
1210*ca22e56dSKay Sievers 	}
1211*ca22e56dSKay Sievers 	mutex_unlock(&subsys->p->mutex);
1212*ca22e56dSKay Sievers 
1213*ca22e56dSKay Sievers 	bus_put(subsys);
1214*ca22e56dSKay Sievers }
1215*ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_interface_unregister);
1216*ca22e56dSKay Sievers 
1217*ca22e56dSKay Sievers static void system_root_device_release(struct device *dev)
1218*ca22e56dSKay Sievers {
1219*ca22e56dSKay Sievers 	kfree(dev);
1220*ca22e56dSKay Sievers }
1221*ca22e56dSKay Sievers /**
1222*ca22e56dSKay Sievers  * subsys_system_register - register a subsystem at /sys/devices/system/
1223*ca22e56dSKay Sievers  * @subsys - system subsystem
1224*ca22e56dSKay Sievers  * @groups - default attributes for the root device
1225*ca22e56dSKay Sievers  *
1226*ca22e56dSKay Sievers  * All 'system' subsystems have a /sys/devices/system/<name> root device
1227*ca22e56dSKay Sievers  * with the name of the subsystem. The root device can carry subsystem-
1228*ca22e56dSKay Sievers  * wide attributes. All registered devices are below this single root
1229*ca22e56dSKay Sievers  * device and are named after the subsystem with a simple enumeration
1230*ca22e56dSKay Sievers  * number appended. The registered devices are not explicitely named;
1231*ca22e56dSKay Sievers  * only 'id' in the device needs to be set.
1232*ca22e56dSKay Sievers  *
1233*ca22e56dSKay Sievers  * Do not use this interface for anything new, it exists for compatibility
1234*ca22e56dSKay Sievers  * with bad ideas only. New subsystems should use plain subsystems; and
1235*ca22e56dSKay Sievers  * add the subsystem-wide attributes should be added to the subsystem
1236*ca22e56dSKay Sievers  * directory itself and not some create fake root-device placed in
1237*ca22e56dSKay Sievers  * /sys/devices/system/<name>.
1238*ca22e56dSKay Sievers  */
1239*ca22e56dSKay Sievers int subsys_system_register(struct bus_type *subsys,
1240*ca22e56dSKay Sievers 			   const struct attribute_group **groups)
1241*ca22e56dSKay Sievers {
1242*ca22e56dSKay Sievers 	struct device *dev;
1243*ca22e56dSKay Sievers 	int err;
1244*ca22e56dSKay Sievers 
1245*ca22e56dSKay Sievers 	err = bus_register(subsys);
1246*ca22e56dSKay Sievers 	if (err < 0)
1247*ca22e56dSKay Sievers 		return err;
1248*ca22e56dSKay Sievers 
1249*ca22e56dSKay Sievers 	dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1250*ca22e56dSKay Sievers 	if (!dev) {
1251*ca22e56dSKay Sievers 		err = -ENOMEM;
1252*ca22e56dSKay Sievers 		goto err_dev;
1253*ca22e56dSKay Sievers 	}
1254*ca22e56dSKay Sievers 
1255*ca22e56dSKay Sievers 	err = dev_set_name(dev, "%s", subsys->name);
1256*ca22e56dSKay Sievers 	if (err < 0)
1257*ca22e56dSKay Sievers 		goto err_name;
1258*ca22e56dSKay Sievers 
1259*ca22e56dSKay Sievers 	dev->kobj.parent = &system_kset->kobj;
1260*ca22e56dSKay Sievers 	dev->groups = groups;
1261*ca22e56dSKay Sievers 	dev->release = system_root_device_release;
1262*ca22e56dSKay Sievers 
1263*ca22e56dSKay Sievers 	err = device_register(dev);
1264*ca22e56dSKay Sievers 	if (err < 0)
1265*ca22e56dSKay Sievers 		goto err_dev_reg;
1266*ca22e56dSKay Sievers 
1267*ca22e56dSKay Sievers 	subsys->dev_root = dev;
1268*ca22e56dSKay Sievers 	return 0;
1269*ca22e56dSKay Sievers 
1270*ca22e56dSKay Sievers err_dev_reg:
1271*ca22e56dSKay Sievers 	put_device(dev);
1272*ca22e56dSKay Sievers 	dev = NULL;
1273*ca22e56dSKay Sievers err_name:
1274*ca22e56dSKay Sievers 	kfree(dev);
1275*ca22e56dSKay Sievers err_dev:
1276*ca22e56dSKay Sievers 	bus_unregister(subsys);
1277*ca22e56dSKay Sievers 	return err;
1278*ca22e56dSKay Sievers }
1279*ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_system_register);
1280*ca22e56dSKay Sievers 
12811da177e4SLinus Torvalds int __init buses_init(void)
12821da177e4SLinus Torvalds {
128359a54833SGreg Kroah-Hartman 	bus_kset = kset_create_and_add("bus", &bus_uevent_ops, NULL);
128459a54833SGreg Kroah-Hartman 	if (!bus_kset)
128559a54833SGreg Kroah-Hartman 		return -ENOMEM;
1286*ca22e56dSKay Sievers 
1287*ca22e56dSKay Sievers 	system_kset = kset_create_and_add("system", NULL, &devices_kset->kobj);
1288*ca22e56dSKay Sievers 	if (!system_kset)
1289*ca22e56dSKay Sievers 		return -ENOMEM;
1290*ca22e56dSKay Sievers 
129159a54833SGreg Kroah-Hartman 	return 0;
12921da177e4SLinus Torvalds }
1293