xref: /linux/drivers/base/bus.c (revision e18945b159a1cdbc031f1d3b0b7e515a33bdcbf7)
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>
2063967685SGreg Kroah-Hartman #include <linux/sysfs.h>
211da177e4SLinus Torvalds #include "base.h"
221da177e4SLinus Torvalds #include "power/power.h"
231da177e4SLinus Torvalds 
24ca22e56dSKay Sievers /* /sys/devices/system */
2597ec448aSH Hartley Sweeten static struct kset *system_kset;
26ca22e56dSKay Sievers 
271da177e4SLinus Torvalds #define to_bus_attr(_attr) container_of(_attr, struct bus_attribute, attr)
281da177e4SLinus Torvalds 
291da177e4SLinus Torvalds /*
301da177e4SLinus Torvalds  * sysfs bindings for drivers
311da177e4SLinus Torvalds  */
321da177e4SLinus Torvalds 
331da177e4SLinus Torvalds #define to_drv_attr(_attr) container_of(_attr, struct driver_attribute, attr)
341da177e4SLinus Torvalds 
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 
1682b08c8d0SAlan Stern /* Manually detach a device from its associated driver. */
1692581c9ccSGreg Kroah-Hartman static ssize_t unbind_store(struct device_driver *drv, const char *buf,
1702581c9ccSGreg Kroah-Hartman 			    size_t count)
171151ef38fSGreg Kroah-Hartman {
1725901d014SGreg Kroah-Hartman 	struct bus_type *bus = bus_get(drv->bus);
173151ef38fSGreg Kroah-Hartman 	struct device *dev;
174151ef38fSGreg Kroah-Hartman 	int err = -ENODEV;
175151ef38fSGreg Kroah-Hartman 
1761f9ffc04SGreg Kroah-Hartman 	dev = bus_find_device_by_name(bus, NULL, buf);
1772b08c8d0SAlan Stern 	if (dev && dev->driver == drv) {
178bf74ad5bSAlan Stern 		if (dev->parent)	/* Needed for USB */
1798e9394ceSGreg Kroah-Hartman 			device_lock(dev->parent);
180151ef38fSGreg Kroah-Hartman 		device_release_driver(dev);
181bf74ad5bSAlan Stern 		if (dev->parent)
1828e9394ceSGreg Kroah-Hartman 			device_unlock(dev->parent);
183151ef38fSGreg Kroah-Hartman 		err = count;
184151ef38fSGreg Kroah-Hartman 	}
1852b08c8d0SAlan Stern 	put_device(dev);
186fc1ede58SGreg Kroah-Hartman 	bus_put(bus);
187151ef38fSGreg Kroah-Hartman 	return err;
188151ef38fSGreg Kroah-Hartman }
1892581c9ccSGreg Kroah-Hartman static DRIVER_ATTR_WO(unbind);
190151ef38fSGreg Kroah-Hartman 
191afdce75fSGreg Kroah-Hartman /*
192afdce75fSGreg Kroah-Hartman  * Manually attach a device to a driver.
193afdce75fSGreg Kroah-Hartman  * Note: the driver must want to bind to the device,
194afdce75fSGreg Kroah-Hartman  * it is not possible to override the driver's id table.
195afdce75fSGreg Kroah-Hartman  */
1962581c9ccSGreg Kroah-Hartman static ssize_t bind_store(struct device_driver *drv, const char *buf,
1972581c9ccSGreg Kroah-Hartman 			  size_t count)
198afdce75fSGreg Kroah-Hartman {
1995901d014SGreg Kroah-Hartman 	struct bus_type *bus = bus_get(drv->bus);
200afdce75fSGreg Kroah-Hartman 	struct device *dev;
201afdce75fSGreg Kroah-Hartman 	int err = -ENODEV;
202afdce75fSGreg Kroah-Hartman 
2031f9ffc04SGreg Kroah-Hartman 	dev = bus_find_device_by_name(bus, NULL, buf);
20449b420a1SMing Lei 	if (dev && dev->driver == NULL && driver_match_device(drv, dev)) {
205bf74ad5bSAlan Stern 		if (dev->parent)	/* Needed for USB */
2068e9394ceSGreg Kroah-Hartman 			device_lock(dev->parent);
2078e9394ceSGreg Kroah-Hartman 		device_lock(dev);
208afdce75fSGreg Kroah-Hartman 		err = driver_probe_device(drv, dev);
2098e9394ceSGreg Kroah-Hartman 		device_unlock(dev);
210bf74ad5bSAlan Stern 		if (dev->parent)
2118e9394ceSGreg Kroah-Hartman 			device_unlock(dev->parent);
21237225401SRyan Wilson 
2134a3ad20cSGreg Kroah-Hartman 		if (err > 0) {
2144a3ad20cSGreg Kroah-Hartman 			/* success */
21537225401SRyan Wilson 			err = count;
2164a3ad20cSGreg Kroah-Hartman 		} else if (err == 0) {
2174a3ad20cSGreg Kroah-Hartman 			/* driver didn't accept device */
21837225401SRyan Wilson 			err = -ENODEV;
219afdce75fSGreg Kroah-Hartman 		}
2204a3ad20cSGreg Kroah-Hartman 	}
2212b08c8d0SAlan Stern 	put_device(dev);
222fc1ede58SGreg Kroah-Hartman 	bus_put(bus);
223afdce75fSGreg Kroah-Hartman 	return err;
224afdce75fSGreg Kroah-Hartman }
2252581c9ccSGreg Kroah-Hartman static DRIVER_ATTR_WO(bind);
226afdce75fSGreg Kroah-Hartman 
227b8c5cec2SKay Sievers static ssize_t show_drivers_autoprobe(struct bus_type *bus, char *buf)
228b8c5cec2SKay Sievers {
229c6f7e72aSGreg Kroah-Hartman 	return sprintf(buf, "%d\n", bus->p->drivers_autoprobe);
230b8c5cec2SKay Sievers }
231b8c5cec2SKay Sievers 
232b8c5cec2SKay Sievers static ssize_t store_drivers_autoprobe(struct bus_type *bus,
233b8c5cec2SKay Sievers 				       const char *buf, size_t count)
234b8c5cec2SKay Sievers {
235b8c5cec2SKay Sievers 	if (buf[0] == '0')
236c6f7e72aSGreg Kroah-Hartman 		bus->p->drivers_autoprobe = 0;
237b8c5cec2SKay Sievers 	else
238c6f7e72aSGreg Kroah-Hartman 		bus->p->drivers_autoprobe = 1;
239b8c5cec2SKay Sievers 	return count;
240b8c5cec2SKay Sievers }
241b8c5cec2SKay Sievers 
242b8c5cec2SKay Sievers static ssize_t store_drivers_probe(struct bus_type *bus,
243b8c5cec2SKay Sievers 				   const char *buf, size_t count)
244b8c5cec2SKay Sievers {
245b8c5cec2SKay Sievers 	struct device *dev;
246b8c5cec2SKay Sievers 
2471f9ffc04SGreg Kroah-Hartman 	dev = bus_find_device_by_name(bus, NULL, buf);
248b8c5cec2SKay Sievers 	if (!dev)
249b8c5cec2SKay Sievers 		return -ENODEV;
250b8c5cec2SKay Sievers 	if (bus_rescan_devices_helper(dev, NULL) != 0)
251b8c5cec2SKay Sievers 		return -EINVAL;
252b8c5cec2SKay Sievers 	return count;
253b8c5cec2SKay Sievers }
254151ef38fSGreg Kroah-Hartman 
255465c7a3aSmochel@digitalimplant.org static struct device *next_device(struct klist_iter *i)
256465c7a3aSmochel@digitalimplant.org {
257465c7a3aSmochel@digitalimplant.org 	struct klist_node *n = klist_next(i);
258ae1b4171SGreg Kroah-Hartman 	struct device *dev = NULL;
259ae1b4171SGreg Kroah-Hartman 	struct device_private *dev_prv;
260ae1b4171SGreg Kroah-Hartman 
261ae1b4171SGreg Kroah-Hartman 	if (n) {
262ae1b4171SGreg Kroah-Hartman 		dev_prv = to_device_private_bus(n);
263ae1b4171SGreg Kroah-Hartman 		dev = dev_prv->device;
264ae1b4171SGreg Kroah-Hartman 	}
265ae1b4171SGreg Kroah-Hartman 	return dev;
266465c7a3aSmochel@digitalimplant.org }
267465c7a3aSmochel@digitalimplant.org 
2681da177e4SLinus Torvalds /**
2691da177e4SLinus Torvalds  * bus_for_each_dev - device iterator.
2701da177e4SLinus Torvalds  * @bus: bus type.
2711da177e4SLinus Torvalds  * @start: device to start iterating from.
2721da177e4SLinus Torvalds  * @data: data for the callback.
2731da177e4SLinus Torvalds  * @fn: function to be called for each device.
2741da177e4SLinus Torvalds  *
2751da177e4SLinus Torvalds  * Iterate over @bus's list of devices, and call @fn for each,
2761da177e4SLinus Torvalds  * passing it @data. If @start is not NULL, we use that device to
2771da177e4SLinus Torvalds  * begin iterating from.
2781da177e4SLinus Torvalds  *
2791da177e4SLinus Torvalds  * We check the return of @fn each time. If it returns anything
2801da177e4SLinus Torvalds  * other than 0, we break out and return that value.
2811da177e4SLinus Torvalds  *
2821da177e4SLinus Torvalds  * NOTE: The device that returns a non-zero value is not retained
2831da177e4SLinus Torvalds  * in any way, nor is its refcount incremented. If the caller needs
2840fa1b0a1SAlex Chiang  * to retain this data, it should do so, and increment the reference
2851da177e4SLinus Torvalds  * count in the supplied callback.
2861da177e4SLinus Torvalds  */
2871da177e4SLinus Torvalds int bus_for_each_dev(struct bus_type *bus, struct device *start,
2881da177e4SLinus Torvalds 		     void *data, int (*fn)(struct device *, void *))
2891da177e4SLinus Torvalds {
290465c7a3aSmochel@digitalimplant.org 	struct klist_iter i;
291465c7a3aSmochel@digitalimplant.org 	struct device *dev;
292465c7a3aSmochel@digitalimplant.org 	int error = 0;
2931da177e4SLinus Torvalds 
2944fa3e78bSBjorn Helgaas 	if (!bus || !bus->p)
295465c7a3aSmochel@digitalimplant.org 		return -EINVAL;
296465c7a3aSmochel@digitalimplant.org 
2977cd9c9bbSGreg Kroah-Hartman 	klist_iter_init_node(&bus->p->klist_devices, &i,
298ae1b4171SGreg Kroah-Hartman 			     (start ? &start->p->knode_bus : NULL));
299465c7a3aSmochel@digitalimplant.org 	while ((dev = next_device(&i)) && !error)
300465c7a3aSmochel@digitalimplant.org 		error = fn(dev, data);
301465c7a3aSmochel@digitalimplant.org 	klist_iter_exit(&i);
302465c7a3aSmochel@digitalimplant.org 	return error;
3031da177e4SLinus Torvalds }
3044a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_for_each_dev);
3051da177e4SLinus Torvalds 
3060edb5860SCornelia Huck /**
3070edb5860SCornelia Huck  * bus_find_device - device iterator for locating a particular device.
3080edb5860SCornelia Huck  * @bus: bus type
3090edb5860SCornelia Huck  * @start: Device to begin with
3100edb5860SCornelia Huck  * @data: Data to pass to match function
3110edb5860SCornelia Huck  * @match: Callback function to check device
3120edb5860SCornelia Huck  *
3130edb5860SCornelia Huck  * This is similar to the bus_for_each_dev() function above, but it
3140edb5860SCornelia Huck  * returns a reference to a device that is 'found' for later use, as
3150edb5860SCornelia Huck  * determined by the @match callback.
3160edb5860SCornelia Huck  *
3170edb5860SCornelia Huck  * The callback should return 0 if the device doesn't match and non-zero
3180edb5860SCornelia Huck  * if it does.  If the callback returns non-zero, this function will
3190edb5860SCornelia Huck  * return to the caller and not iterate over any more devices.
3200edb5860SCornelia Huck  */
3210edb5860SCornelia Huck struct device *bus_find_device(struct bus_type *bus,
3220edb5860SCornelia Huck 			       struct device *start, void *data,
3234a3ad20cSGreg Kroah-Hartman 			       int (*match)(struct device *dev, void *data))
3240edb5860SCornelia Huck {
3250edb5860SCornelia Huck 	struct klist_iter i;
3260edb5860SCornelia Huck 	struct device *dev;
3270edb5860SCornelia Huck 
3284fa3e78bSBjorn Helgaas 	if (!bus || !bus->p)
3290edb5860SCornelia Huck 		return NULL;
3300edb5860SCornelia Huck 
3317cd9c9bbSGreg Kroah-Hartman 	klist_iter_init_node(&bus->p->klist_devices, &i,
3327cd9c9bbSGreg Kroah-Hartman 			     (start ? &start->p->knode_bus : NULL));
3330edb5860SCornelia Huck 	while ((dev = next_device(&i)))
3340edb5860SCornelia Huck 		if (match(dev, data) && get_device(dev))
3350edb5860SCornelia Huck 			break;
3360edb5860SCornelia Huck 	klist_iter_exit(&i);
3370edb5860SCornelia Huck 	return dev;
3380edb5860SCornelia Huck }
3394a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_find_device);
34038fdac3cSmochel@digitalimplant.org 
3411f9ffc04SGreg Kroah-Hartman static int match_name(struct device *dev, void *data)
3421f9ffc04SGreg Kroah-Hartman {
3431f9ffc04SGreg Kroah-Hartman 	const char *name = data;
3441f9ffc04SGreg Kroah-Hartman 
3451e0b2cf9SKay Sievers 	return sysfs_streq(name, dev_name(dev));
3461f9ffc04SGreg Kroah-Hartman }
3471f9ffc04SGreg Kroah-Hartman 
3481f9ffc04SGreg Kroah-Hartman /**
3491f9ffc04SGreg Kroah-Hartman  * bus_find_device_by_name - device iterator for locating a particular device of a specific name
3501f9ffc04SGreg Kroah-Hartman  * @bus: bus type
3511f9ffc04SGreg Kroah-Hartman  * @start: Device to begin with
3521f9ffc04SGreg Kroah-Hartman  * @name: name of the device to match
3531f9ffc04SGreg Kroah-Hartman  *
3541f9ffc04SGreg Kroah-Hartman  * This is similar to the bus_find_device() function above, but it handles
3551f9ffc04SGreg Kroah-Hartman  * searching by a name automatically, no need to write another strcmp matching
3561f9ffc04SGreg Kroah-Hartman  * function.
3571f9ffc04SGreg Kroah-Hartman  */
3581f9ffc04SGreg Kroah-Hartman struct device *bus_find_device_by_name(struct bus_type *bus,
3591f9ffc04SGreg Kroah-Hartman 				       struct device *start, const char *name)
3601f9ffc04SGreg Kroah-Hartman {
3611f9ffc04SGreg Kroah-Hartman 	return bus_find_device(bus, start, (void *)name, match_name);
3621f9ffc04SGreg Kroah-Hartman }
3631f9ffc04SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_find_device_by_name);
3641f9ffc04SGreg Kroah-Hartman 
365ca22e56dSKay Sievers /**
366ca22e56dSKay Sievers  * subsys_find_device_by_id - find a device with a specific enumeration number
367ca22e56dSKay Sievers  * @subsys: subsystem
368ca22e56dSKay Sievers  * @id: index 'id' in struct device
369ca22e56dSKay Sievers  * @hint: device to check first
370ca22e56dSKay Sievers  *
371ca22e56dSKay Sievers  * Check the hint's next object and if it is a match return it directly,
372ca22e56dSKay Sievers  * otherwise, fall back to a full list search. Either way a reference for
373ca22e56dSKay Sievers  * the returned object is taken.
374ca22e56dSKay Sievers  */
375ca22e56dSKay Sievers struct device *subsys_find_device_by_id(struct bus_type *subsys, unsigned int id,
376ca22e56dSKay Sievers 					struct device *hint)
377ca22e56dSKay Sievers {
378ca22e56dSKay Sievers 	struct klist_iter i;
379ca22e56dSKay Sievers 	struct device *dev;
380ca22e56dSKay Sievers 
381ca22e56dSKay Sievers 	if (!subsys)
382ca22e56dSKay Sievers 		return NULL;
383ca22e56dSKay Sievers 
384ca22e56dSKay Sievers 	if (hint) {
3857cd9c9bbSGreg Kroah-Hartman 		klist_iter_init_node(&subsys->p->klist_devices, &i, &hint->p->knode_bus);
386ca22e56dSKay Sievers 		dev = next_device(&i);
387ca22e56dSKay Sievers 		if (dev && dev->id == id && get_device(dev)) {
388ca22e56dSKay Sievers 			klist_iter_exit(&i);
389ca22e56dSKay Sievers 			return dev;
390ca22e56dSKay Sievers 		}
391ca22e56dSKay Sievers 		klist_iter_exit(&i);
392ca22e56dSKay Sievers 	}
393ca22e56dSKay Sievers 
394ca22e56dSKay Sievers 	klist_iter_init_node(&subsys->p->klist_devices, &i, NULL);
395ca22e56dSKay Sievers 	while ((dev = next_device(&i))) {
396ca22e56dSKay Sievers 		if (dev->id == id && get_device(dev)) {
397ca22e56dSKay Sievers 			klist_iter_exit(&i);
398ca22e56dSKay Sievers 			return dev;
399ca22e56dSKay Sievers 		}
400ca22e56dSKay Sievers 	}
401ca22e56dSKay Sievers 	klist_iter_exit(&i);
402ca22e56dSKay Sievers 	return NULL;
403ca22e56dSKay Sievers }
404ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_find_device_by_id);
405ca22e56dSKay Sievers 
40638fdac3cSmochel@digitalimplant.org static struct device_driver *next_driver(struct klist_iter *i)
40738fdac3cSmochel@digitalimplant.org {
40838fdac3cSmochel@digitalimplant.org 	struct klist_node *n = klist_next(i);
409e5dd1278SGreg Kroah-Hartman 	struct driver_private *drv_priv;
410e5dd1278SGreg Kroah-Hartman 
411e5dd1278SGreg Kroah-Hartman 	if (n) {
412e5dd1278SGreg Kroah-Hartman 		drv_priv = container_of(n, struct driver_private, knode_bus);
413e5dd1278SGreg Kroah-Hartman 		return drv_priv->driver;
414e5dd1278SGreg Kroah-Hartman 	}
415e5dd1278SGreg Kroah-Hartman 	return NULL;
41638fdac3cSmochel@digitalimplant.org }
41738fdac3cSmochel@digitalimplant.org 
4181da177e4SLinus Torvalds /**
4191da177e4SLinus Torvalds  * bus_for_each_drv - driver iterator
4201da177e4SLinus Torvalds  * @bus: bus we're dealing with.
4211da177e4SLinus Torvalds  * @start: driver to start iterating on.
4221da177e4SLinus Torvalds  * @data: data to pass to the callback.
4231da177e4SLinus Torvalds  * @fn: function to call for each driver.
4241da177e4SLinus Torvalds  *
4251da177e4SLinus Torvalds  * This is nearly identical to the device iterator above.
4261da177e4SLinus Torvalds  * We iterate over each driver that belongs to @bus, and call
4271da177e4SLinus Torvalds  * @fn for each. If @fn returns anything but 0, we break out
4281da177e4SLinus Torvalds  * and return it. If @start is not NULL, we use it as the head
4291da177e4SLinus Torvalds  * of the list.
4301da177e4SLinus Torvalds  *
4311da177e4SLinus Torvalds  * NOTE: we don't return the driver that returns a non-zero
4321da177e4SLinus Torvalds  * value, nor do we leave the reference count incremented for that
4331da177e4SLinus Torvalds  * driver. If the caller needs to know that info, it must set it
4341da177e4SLinus Torvalds  * in the callback. It must also be sure to increment the refcount
4351da177e4SLinus Torvalds  * so it doesn't disappear before returning to the caller.
4361da177e4SLinus Torvalds  */
4371da177e4SLinus Torvalds int bus_for_each_drv(struct bus_type *bus, struct device_driver *start,
4381da177e4SLinus Torvalds 		     void *data, int (*fn)(struct device_driver *, void *))
4391da177e4SLinus Torvalds {
44038fdac3cSmochel@digitalimplant.org 	struct klist_iter i;
44138fdac3cSmochel@digitalimplant.org 	struct device_driver *drv;
44238fdac3cSmochel@digitalimplant.org 	int error = 0;
4431da177e4SLinus Torvalds 
44438fdac3cSmochel@digitalimplant.org 	if (!bus)
44538fdac3cSmochel@digitalimplant.org 		return -EINVAL;
44638fdac3cSmochel@digitalimplant.org 
4477cd9c9bbSGreg Kroah-Hartman 	klist_iter_init_node(&bus->p->klist_drivers, &i,
448e5dd1278SGreg Kroah-Hartman 			     start ? &start->p->knode_bus : NULL);
44938fdac3cSmochel@digitalimplant.org 	while ((drv = next_driver(&i)) && !error)
45038fdac3cSmochel@digitalimplant.org 		error = fn(drv, data);
45138fdac3cSmochel@digitalimplant.org 	klist_iter_exit(&i);
45238fdac3cSmochel@digitalimplant.org 	return error;
4531da177e4SLinus Torvalds }
4544a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_for_each_drv);
4551da177e4SLinus Torvalds 
4561da177e4SLinus Torvalds static int device_add_attrs(struct bus_type *bus, struct device *dev)
4571da177e4SLinus Torvalds {
4581da177e4SLinus Torvalds 	int error = 0;
4591da177e4SLinus Torvalds 	int i;
4601da177e4SLinus Torvalds 
4614aca67e5SAndrew Morton 	if (!bus->dev_attrs)
4624aca67e5SAndrew Morton 		return 0;
4634aca67e5SAndrew Morton 
4643e1026b3SGreg Kroah-Hartman 	for (i = 0; bus->dev_attrs[i].attr.name; i++) {
4651da177e4SLinus Torvalds 		error = device_create_file(dev, &bus->dev_attrs[i]);
4664aca67e5SAndrew Morton 		if (error) {
4671da177e4SLinus Torvalds 			while (--i >= 0)
4681da177e4SLinus Torvalds 				device_remove_file(dev, &bus->dev_attrs[i]);
4694aca67e5SAndrew Morton 			break;
4701da177e4SLinus Torvalds 		}
4714aca67e5SAndrew Morton 	}
4724aca67e5SAndrew Morton 	return error;
4734aca67e5SAndrew Morton }
4741da177e4SLinus Torvalds 
4751da177e4SLinus Torvalds static void device_remove_attrs(struct bus_type *bus, struct device *dev)
4761da177e4SLinus Torvalds {
4771da177e4SLinus Torvalds 	int i;
4781da177e4SLinus Torvalds 
4791da177e4SLinus Torvalds 	if (bus->dev_attrs) {
4803e1026b3SGreg Kroah-Hartman 		for (i = 0; bus->dev_attrs[i].attr.name; i++)
4811da177e4SLinus Torvalds 			device_remove_file(dev, &bus->dev_attrs[i]);
4821da177e4SLinus Torvalds 	}
4831da177e4SLinus Torvalds }
4841da177e4SLinus Torvalds 
4851da177e4SLinus Torvalds /**
4861da177e4SLinus Torvalds  * bus_add_device - add device to bus
4871da177e4SLinus Torvalds  * @dev: device being added
4881da177e4SLinus Torvalds  *
4892023c610SAlan Stern  * - Add device's bus attributes.
4902023c610SAlan Stern  * - Create links to device's bus.
4911da177e4SLinus Torvalds  * - Add the device to its bus's list of devices.
4921da177e4SLinus Torvalds  */
4931da177e4SLinus Torvalds int bus_add_device(struct device *dev)
4941da177e4SLinus Torvalds {
4955901d014SGreg Kroah-Hartman 	struct bus_type *bus = bus_get(dev->bus);
4961da177e4SLinus Torvalds 	int error = 0;
4971da177e4SLinus Torvalds 
4981da177e4SLinus Torvalds 	if (bus) {
4991e0b2cf9SKay Sievers 		pr_debug("bus: '%s': add device %s\n", bus->name, dev_name(dev));
500ca2b94baSHannes Reinecke 		error = device_add_attrs(bus, dev);
501f86db396SAndrew Morton 		if (error)
502513e7337SCornelia Huck 			goto out_put;
503fa6fdb33SGreg Kroah-Hartman 		error = device_add_groups(dev, bus->dev_groups);
504fa6fdb33SGreg Kroah-Hartman 		if (error)
505fa6fdb33SGreg Kroah-Hartman 			goto out_groups;
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));
520fa6fdb33SGreg Kroah-Hartman out_groups:
521fa6fdb33SGreg Kroah-Hartman 	device_remove_groups(dev, bus->dev_groups);
522513e7337SCornelia Huck out_id:
523513e7337SCornelia Huck 	device_remove_attrs(bus, dev);
524513e7337SCornelia Huck out_put:
525fc1ede58SGreg Kroah-Hartman 	bus_put(dev->bus);
5261da177e4SLinus Torvalds 	return error;
5271da177e4SLinus Torvalds }
5281da177e4SLinus Torvalds 
5291da177e4SLinus Torvalds /**
5302023c610SAlan Stern  * bus_probe_device - probe drivers for a new device
5312023c610SAlan Stern  * @dev: device to probe
53253877d06SKay Sievers  *
5332023c610SAlan Stern  * - Automatically probe for a driver if the bus allows it.
53453877d06SKay Sievers  */
5352023c610SAlan Stern void bus_probe_device(struct device *dev)
53653877d06SKay Sievers {
53753877d06SKay Sievers 	struct bus_type *bus = dev->bus;
538ca22e56dSKay Sievers 	struct subsys_interface *sif;
5392023c610SAlan Stern 	int ret;
54053877d06SKay Sievers 
541ca22e56dSKay Sievers 	if (!bus)
542ca22e56dSKay Sievers 		return;
543ca22e56dSKay Sievers 
544ca22e56dSKay Sievers 	if (bus->p->drivers_autoprobe) {
545f86db396SAndrew Morton 		ret = device_attach(dev);
546c6a46696SCornelia Huck 		WARN_ON(ret < 0);
54753877d06SKay Sievers 	}
548ca22e56dSKay Sievers 
549ca22e56dSKay Sievers 	mutex_lock(&bus->p->mutex);
550ca22e56dSKay Sievers 	list_for_each_entry(sif, &bus->p->interfaces, node)
551ca22e56dSKay Sievers 		if (sif->add_dev)
552ca22e56dSKay Sievers 			sif->add_dev(dev, sif);
553ca22e56dSKay Sievers 	mutex_unlock(&bus->p->mutex);
554f86db396SAndrew Morton }
55553877d06SKay Sievers 
55653877d06SKay Sievers /**
5571da177e4SLinus Torvalds  * bus_remove_device - remove device from bus
5581da177e4SLinus Torvalds  * @dev: device to be removed
5591da177e4SLinus Torvalds  *
560ca22e56dSKay Sievers  * - Remove device from all interfaces.
561ca22e56dSKay Sievers  * - Remove symlink from bus' directory.
5621da177e4SLinus Torvalds  * - Delete device from bus's list.
5631da177e4SLinus Torvalds  * - Detach from its driver.
5641da177e4SLinus Torvalds  * - Drop reference taken in bus_add_device().
5651da177e4SLinus Torvalds  */
5661da177e4SLinus Torvalds void bus_remove_device(struct device *dev)
5671da177e4SLinus Torvalds {
568ca22e56dSKay Sievers 	struct bus_type *bus = dev->bus;
569ca22e56dSKay Sievers 	struct subsys_interface *sif;
570ca22e56dSKay Sievers 
571ca22e56dSKay Sievers 	if (!bus)
572ca22e56dSKay Sievers 		return;
573ca22e56dSKay Sievers 
574ca22e56dSKay Sievers 	mutex_lock(&bus->p->mutex);
575ca22e56dSKay Sievers 	list_for_each_entry(sif, &bus->p->interfaces, node)
576ca22e56dSKay Sievers 		if (sif->remove_dev)
577ca22e56dSKay Sievers 			sif->remove_dev(dev, sif);
578ca22e56dSKay Sievers 	mutex_unlock(&bus->p->mutex);
579ca22e56dSKay Sievers 
580b9d9c82bSKay Sievers 	sysfs_remove_link(&dev->kobj, "subsystem");
5814a3ad20cSGreg Kroah-Hartman 	sysfs_remove_link(&dev->bus->p->devices_kset->kobj,
5821e0b2cf9SKay Sievers 			  dev_name(dev));
5831da177e4SLinus Torvalds 	device_remove_attrs(dev->bus, dev);
584fa6fdb33SGreg Kroah-Hartman 	device_remove_groups(dev, dev->bus->dev_groups);
585ae1b4171SGreg Kroah-Hartman 	if (klist_node_attached(&dev->p->knode_bus))
586ae1b4171SGreg Kroah-Hartman 		klist_del(&dev->p->knode_bus);
5873f62e570SGreg Kroah-Hartman 
5884a3ad20cSGreg Kroah-Hartman 	pr_debug("bus: '%s': remove device %s\n",
5891e0b2cf9SKay Sievers 		 dev->bus->name, dev_name(dev));
5901da177e4SLinus Torvalds 	device_release_driver(dev);
591fc1ede58SGreg Kroah-Hartman 	bus_put(dev->bus);
5921da177e4SLinus Torvalds }
5931da177e4SLinus Torvalds 
594f86db396SAndrew Morton static int __must_check add_bind_files(struct device_driver *drv)
595874c6241SGreg Kroah-Hartman {
596f86db396SAndrew Morton 	int ret;
597f86db396SAndrew Morton 
598f86db396SAndrew Morton 	ret = driver_create_file(drv, &driver_attr_unbind);
599f86db396SAndrew Morton 	if (ret == 0) {
600f86db396SAndrew Morton 		ret = driver_create_file(drv, &driver_attr_bind);
601f86db396SAndrew Morton 		if (ret)
602f86db396SAndrew Morton 			driver_remove_file(drv, &driver_attr_unbind);
603f86db396SAndrew Morton 	}
604f86db396SAndrew Morton 	return ret;
605874c6241SGreg Kroah-Hartman }
606874c6241SGreg Kroah-Hartman 
607874c6241SGreg Kroah-Hartman static void remove_bind_files(struct device_driver *drv)
608874c6241SGreg Kroah-Hartman {
609874c6241SGreg Kroah-Hartman 	driver_remove_file(drv, &driver_attr_bind);
610874c6241SGreg Kroah-Hartman 	driver_remove_file(drv, &driver_attr_unbind);
611874c6241SGreg Kroah-Hartman }
612b8c5cec2SKay Sievers 
6138380770cSKay Sievers static BUS_ATTR(drivers_probe, S_IWUSR, NULL, store_drivers_probe);
6148380770cSKay Sievers static BUS_ATTR(drivers_autoprobe, S_IWUSR | S_IRUGO,
6158380770cSKay Sievers 		show_drivers_autoprobe, store_drivers_autoprobe);
6168380770cSKay Sievers 
617b8c5cec2SKay Sievers static int add_probe_files(struct bus_type *bus)
618b8c5cec2SKay Sievers {
619b8c5cec2SKay Sievers 	int retval;
620b8c5cec2SKay Sievers 
6218380770cSKay Sievers 	retval = bus_create_file(bus, &bus_attr_drivers_probe);
622b8c5cec2SKay Sievers 	if (retval)
623b8c5cec2SKay Sievers 		goto out;
624b8c5cec2SKay Sievers 
6258380770cSKay Sievers 	retval = bus_create_file(bus, &bus_attr_drivers_autoprobe);
626b8c5cec2SKay Sievers 	if (retval)
6278380770cSKay Sievers 		bus_remove_file(bus, &bus_attr_drivers_probe);
628b8c5cec2SKay Sievers out:
629b8c5cec2SKay Sievers 	return retval;
630b8c5cec2SKay Sievers }
631b8c5cec2SKay Sievers 
632b8c5cec2SKay Sievers static void remove_probe_files(struct bus_type *bus)
633b8c5cec2SKay Sievers {
6348380770cSKay Sievers 	bus_remove_file(bus, &bus_attr_drivers_autoprobe);
6358380770cSKay Sievers 	bus_remove_file(bus, &bus_attr_drivers_probe);
636b8c5cec2SKay Sievers }
6371da177e4SLinus Torvalds 
6382581c9ccSGreg Kroah-Hartman static ssize_t uevent_store(struct device_driver *drv, const char *buf,
6392581c9ccSGreg Kroah-Hartman 			    size_t count)
6407ac1cf4aSKay Sievers {
6417ac1cf4aSKay Sievers 	enum kobject_action action;
6427ac1cf4aSKay Sievers 
6437ac1cf4aSKay Sievers 	if (kobject_action_type(buf, count, &action) == 0)
644e5dd1278SGreg Kroah-Hartman 		kobject_uevent(&drv->p->kobj, action);
6457ac1cf4aSKay Sievers 	return count;
6467ac1cf4aSKay Sievers }
6472581c9ccSGreg Kroah-Hartman static DRIVER_ATTR_WO(uevent);
6487ac1cf4aSKay Sievers 
6491da177e4SLinus Torvalds /**
6501da177e4SLinus Torvalds  * bus_add_driver - Add a driver to the bus.
6511da177e4SLinus Torvalds  * @drv: driver.
6521da177e4SLinus Torvalds  */
6531da177e4SLinus Torvalds int bus_add_driver(struct device_driver *drv)
6541da177e4SLinus Torvalds {
655e5dd1278SGreg Kroah-Hartman 	struct bus_type *bus;
656e5dd1278SGreg Kroah-Hartman 	struct driver_private *priv;
6571da177e4SLinus Torvalds 	int error = 0;
6581da177e4SLinus Torvalds 
659e5dd1278SGreg Kroah-Hartman 	bus = bus_get(drv->bus);
660d9fd4d3bSJeff Garzik 	if (!bus)
6614f6e1945SGreg Kroah-Hartman 		return -EINVAL;
662d9fd4d3bSJeff Garzik 
6637dc72b28SGreg Kroah-Hartman 	pr_debug("bus: '%s': add driver %s\n", bus->name, drv->name);
664e5dd1278SGreg Kroah-Hartman 
665e5dd1278SGreg Kroah-Hartman 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
66607634464SCornelia Huck 	if (!priv) {
66707634464SCornelia Huck 		error = -ENOMEM;
66807634464SCornelia Huck 		goto out_put_bus;
66907634464SCornelia Huck 	}
670e5dd1278SGreg Kroah-Hartman 	klist_init(&priv->klist_devices, NULL, NULL);
671e5dd1278SGreg Kroah-Hartman 	priv->driver = drv;
672e5dd1278SGreg Kroah-Hartman 	drv->p = priv;
673c8e90d82SGreg Kroah-Hartman 	priv->kobj.kset = bus->p->drivers_kset;
674c8e90d82SGreg Kroah-Hartman 	error = kobject_init_and_add(&priv->kobj, &driver_ktype, NULL,
675c8e90d82SGreg Kroah-Hartman 				     "%s", drv->name);
676dc0afa83SCornelia Huck 	if (error)
67707634464SCornelia Huck 		goto out_unregister;
6781da177e4SLinus Torvalds 
679190888acSMing Lei 	klist_add_tail(&priv->knode_bus, &bus->p->klist_drivers);
680c6f7e72aSGreg Kroah-Hartman 	if (drv->bus->p->drivers_autoprobe) {
681f86db396SAndrew Morton 		error = driver_attach(drv);
682f86db396SAndrew Morton 		if (error)
683f86db396SAndrew Morton 			goto out_unregister;
684b8c5cec2SKay Sievers 	}
6851da177e4SLinus Torvalds 	module_add_driver(drv->owner, drv);
6861da177e4SLinus Torvalds 
6877ac1cf4aSKay Sievers 	error = driver_create_file(drv, &driver_attr_uevent);
6887ac1cf4aSKay Sievers 	if (error) {
6897ac1cf4aSKay Sievers 		printk(KERN_ERR "%s: uevent attr (%s) failed\n",
6902b3a302aSHarvey Harrison 			__func__, drv->name);
6917ac1cf4aSKay Sievers 	}
692*e18945b1SGreg Kroah-Hartman 	error = driver_add_groups(drv, bus->drv_groups);
693f86db396SAndrew Morton 	if (error) {
694f86db396SAndrew Morton 		/* How the hell do we get out of this pickle? Give up */
695ed0617b5SGreg Kroah-Hartman 		printk(KERN_ERR "%s: driver_create_groups(%s) failed\n",
696ed0617b5SGreg Kroah-Hartman 			__func__, drv->name);
697*e18945b1SGreg Kroah-Hartman 	}
6981a6f2a75SDmitry Torokhov 
6991a6f2a75SDmitry Torokhov 	if (!drv->suppress_bind_attrs) {
700f86db396SAndrew Morton 		error = add_bind_files(drv);
701f86db396SAndrew Morton 		if (error) {
702f86db396SAndrew Morton 			/* Ditto */
703f86db396SAndrew Morton 			printk(KERN_ERR "%s: add_bind_files(%s) failed\n",
7042b3a302aSHarvey Harrison 				__func__, drv->name);
705f86db396SAndrew Morton 		}
7061a6f2a75SDmitry Torokhov 	}
707d9fd4d3bSJeff Garzik 
7085c8563d7SKay Sievers 	return 0;
7091a6f2a75SDmitry Torokhov 
710f86db396SAndrew Morton out_unregister:
71199b28f1bSPhil Carmody 	kobject_put(&priv->kobj);
7125c8563d7SKay Sievers 	kfree(drv->p);
7135c8563d7SKay Sievers 	drv->p = NULL;
714f86db396SAndrew Morton out_put_bus:
715fc1ede58SGreg Kroah-Hartman 	bus_put(bus);
716f86db396SAndrew Morton 	return error;
7171da177e4SLinus Torvalds }
7181da177e4SLinus Torvalds 
7191da177e4SLinus Torvalds /**
7201da177e4SLinus Torvalds  * bus_remove_driver - delete driver from bus's knowledge.
7211da177e4SLinus Torvalds  * @drv: driver.
7221da177e4SLinus Torvalds  *
7231da177e4SLinus Torvalds  * Detach the driver from the devices it controls, and remove
7241da177e4SLinus Torvalds  * it from its bus's list of drivers. Finally, we drop the reference
7251da177e4SLinus Torvalds  * to the bus we took in bus_add_driver().
7261da177e4SLinus Torvalds  */
7271da177e4SLinus Torvalds void bus_remove_driver(struct device_driver *drv)
7281da177e4SLinus Torvalds {
729d9fd4d3bSJeff Garzik 	if (!drv->bus)
730d9fd4d3bSJeff Garzik 		return;
731d9fd4d3bSJeff Garzik 
7321a6f2a75SDmitry Torokhov 	if (!drv->suppress_bind_attrs)
733874c6241SGreg Kroah-Hartman 		remove_bind_files(drv);
734ed0617b5SGreg Kroah-Hartman 	driver_remove_groups(drv, drv->bus->drv_groups);
7357ac1cf4aSKay Sievers 	driver_remove_file(drv, &driver_attr_uevent);
736e5dd1278SGreg Kroah-Hartman 	klist_remove(&drv->p->knode_bus);
7377dc72b28SGreg Kroah-Hartman 	pr_debug("bus: '%s': remove driver %s\n", drv->bus->name, drv->name);
7381da177e4SLinus Torvalds 	driver_detach(drv);
7391da177e4SLinus Torvalds 	module_remove_driver(drv);
740c10997f6SGreg Kroah-Hartman 	kobject_put(&drv->p->kobj);
741fc1ede58SGreg Kroah-Hartman 	bus_put(drv->bus);
7421da177e4SLinus Torvalds }
7431da177e4SLinus Torvalds 
7441da177e4SLinus Torvalds /* Helper for bus_rescan_devices's iter */
745f86db396SAndrew Morton static int __must_check bus_rescan_devices_helper(struct device *dev,
746f86db396SAndrew Morton 						  void *data)
7471da177e4SLinus Torvalds {
748f86db396SAndrew Morton 	int ret = 0;
749f86db396SAndrew Morton 
750bf74ad5bSAlan Stern 	if (!dev->driver) {
751bf74ad5bSAlan Stern 		if (dev->parent)	/* Needed for USB */
7528e9394ceSGreg Kroah-Hartman 			device_lock(dev->parent);
753f86db396SAndrew Morton 		ret = device_attach(dev);
754bf74ad5bSAlan Stern 		if (dev->parent)
7558e9394ceSGreg Kroah-Hartman 			device_unlock(dev->parent);
756bf74ad5bSAlan Stern 	}
757f86db396SAndrew Morton 	return ret < 0 ? ret : 0;
7581da177e4SLinus Torvalds }
7591da177e4SLinus Torvalds 
7601da177e4SLinus Torvalds /**
7611da177e4SLinus Torvalds  * bus_rescan_devices - rescan devices on the bus for possible drivers
7621da177e4SLinus Torvalds  * @bus: the bus to scan.
7631da177e4SLinus Torvalds  *
7641da177e4SLinus Torvalds  * This function will look for devices on the bus with no driver
76523d3d602SGreg Kroah-Hartman  * attached and rescan it against existing drivers to see if it matches
76623d3d602SGreg Kroah-Hartman  * any by calling device_attach() for the unbound devices.
7671da177e4SLinus Torvalds  */
768f86db396SAndrew Morton int bus_rescan_devices(struct bus_type *bus)
7691da177e4SLinus Torvalds {
770f86db396SAndrew Morton 	return bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper);
7711da177e4SLinus Torvalds }
7724a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_rescan_devices);
7731da177e4SLinus Torvalds 
774e935d5daSMoore, Eric /**
775e935d5daSMoore, Eric  * device_reprobe - remove driver for a device and probe for a new driver
776e935d5daSMoore, Eric  * @dev: the device to reprobe
777e935d5daSMoore, Eric  *
778e935d5daSMoore, Eric  * This function detaches the attached driver (if any) for the given
779e935d5daSMoore, Eric  * device and restarts the driver probing process.  It is intended
780e935d5daSMoore, Eric  * to use if probing criteria changed during a devices lifetime and
781e935d5daSMoore, Eric  * driver attachment should change accordingly.
782e935d5daSMoore, Eric  */
783f86db396SAndrew Morton int device_reprobe(struct device *dev)
784e935d5daSMoore, Eric {
785e935d5daSMoore, Eric 	if (dev->driver) {
786e935d5daSMoore, Eric 		if (dev->parent)        /* Needed for USB */
7878e9394ceSGreg Kroah-Hartman 			device_lock(dev->parent);
788e935d5daSMoore, Eric 		device_release_driver(dev);
789e935d5daSMoore, Eric 		if (dev->parent)
7908e9394ceSGreg Kroah-Hartman 			device_unlock(dev->parent);
791e935d5daSMoore, Eric 	}
792f86db396SAndrew Morton 	return bus_rescan_devices_helper(dev, NULL);
793e935d5daSMoore, Eric }
794e935d5daSMoore, Eric EXPORT_SYMBOL_GPL(device_reprobe);
7951da177e4SLinus Torvalds 
7961da177e4SLinus Torvalds /**
7971da177e4SLinus Torvalds  * find_bus - locate bus by name.
7981da177e4SLinus Torvalds  * @name: name of bus.
7991da177e4SLinus Torvalds  *
8001da177e4SLinus Torvalds  * Call kset_find_obj() to iterate over list of buses to
8011da177e4SLinus Torvalds  * find a bus by name. Return bus if found.
8021da177e4SLinus Torvalds  *
8031da177e4SLinus Torvalds  * Note that kset_find_obj increments bus' reference count.
8041da177e4SLinus Torvalds  */
8057e4ef085SAdrian Bunk #if 0
8061da177e4SLinus Torvalds struct bus_type *find_bus(char *name)
8071da177e4SLinus Torvalds {
80859a54833SGreg Kroah-Hartman 	struct kobject *k = kset_find_obj(bus_kset, name);
8091da177e4SLinus Torvalds 	return k ? to_bus(k) : NULL;
8101da177e4SLinus Torvalds }
8117e4ef085SAdrian Bunk #endif  /*  0  */
8121da177e4SLinus Torvalds 
81312478ba0SGreg Kroah-Hartman static int bus_add_groups(struct bus_type *bus,
81412478ba0SGreg Kroah-Hartman 			  const struct attribute_group **groups)
81512478ba0SGreg Kroah-Hartman {
8163e9b2baeSGreg Kroah-Hartman 	return sysfs_create_groups(&bus->p->subsys.kobj, groups);
81712478ba0SGreg Kroah-Hartman }
81812478ba0SGreg Kroah-Hartman 
81912478ba0SGreg Kroah-Hartman static void bus_remove_groups(struct bus_type *bus,
82012478ba0SGreg Kroah-Hartman 			      const struct attribute_group **groups)
82112478ba0SGreg Kroah-Hartman {
8223e9b2baeSGreg Kroah-Hartman 	sysfs_remove_groups(&bus->p->subsys.kobj, groups);
82312478ba0SGreg Kroah-Hartman }
82412478ba0SGreg Kroah-Hartman 
82534bb61f9SJames Bottomley static void klist_devices_get(struct klist_node *n)
82634bb61f9SJames Bottomley {
827ae1b4171SGreg Kroah-Hartman 	struct device_private *dev_prv = to_device_private_bus(n);
828ae1b4171SGreg Kroah-Hartman 	struct device *dev = dev_prv->device;
82934bb61f9SJames Bottomley 
83034bb61f9SJames Bottomley 	get_device(dev);
83134bb61f9SJames Bottomley }
83234bb61f9SJames Bottomley 
83334bb61f9SJames Bottomley static void klist_devices_put(struct klist_node *n)
83434bb61f9SJames Bottomley {
835ae1b4171SGreg Kroah-Hartman 	struct device_private *dev_prv = to_device_private_bus(n);
836ae1b4171SGreg Kroah-Hartman 	struct device *dev = dev_prv->device;
83734bb61f9SJames Bottomley 
83834bb61f9SJames Bottomley 	put_device(dev);
83934bb61f9SJames Bottomley }
84034bb61f9SJames Bottomley 
8417ac1cf4aSKay Sievers static ssize_t bus_uevent_store(struct bus_type *bus,
8427ac1cf4aSKay Sievers 				const char *buf, size_t count)
8437ac1cf4aSKay Sievers {
8447ac1cf4aSKay Sievers 	enum kobject_action action;
8457ac1cf4aSKay Sievers 
8467ac1cf4aSKay Sievers 	if (kobject_action_type(buf, count, &action) == 0)
847c6f7e72aSGreg Kroah-Hartman 		kobject_uevent(&bus->p->subsys.kobj, action);
8487ac1cf4aSKay Sievers 	return count;
8497ac1cf4aSKay Sievers }
8507ac1cf4aSKay Sievers static BUS_ATTR(uevent, S_IWUSR, NULL, bus_uevent_store);
8517ac1cf4aSKay Sievers 
8521da177e4SLinus Torvalds /**
853be871b7eSMichal Hocko  * bus_register - register a driver-core subsystem
85478d79559SRandy Dunlap  * @bus: bus to register
8551da177e4SLinus Torvalds  *
85678d79559SRandy Dunlap  * Once we have that, we register the bus with the kobject
8571da177e4SLinus Torvalds  * infrastructure, then register the children subsystems it has:
858ca22e56dSKay Sievers  * the devices and drivers that belong to the subsystem.
8591da177e4SLinus Torvalds  */
860be871b7eSMichal Hocko int bus_register(struct bus_type *bus)
8611da177e4SLinus Torvalds {
8621da177e4SLinus Torvalds 	int retval;
8636b6e39a6SKay Sievers 	struct subsys_private *priv;
864be871b7eSMichal Hocko 	struct lock_class_key *key = &bus->lock_key;
8651da177e4SLinus Torvalds 
8666b6e39a6SKay Sievers 	priv = kzalloc(sizeof(struct subsys_private), GFP_KERNEL);
867c6f7e72aSGreg Kroah-Hartman 	if (!priv)
868c6f7e72aSGreg Kroah-Hartman 		return -ENOMEM;
869116af378SBenjamin Herrenschmidt 
870c6f7e72aSGreg Kroah-Hartman 	priv->bus = bus;
871c6f7e72aSGreg Kroah-Hartman 	bus->p = priv;
872c6f7e72aSGreg Kroah-Hartman 
873c6f7e72aSGreg Kroah-Hartman 	BLOCKING_INIT_NOTIFIER_HEAD(&priv->bus_notifier);
874c6f7e72aSGreg Kroah-Hartman 
875c6f7e72aSGreg Kroah-Hartman 	retval = kobject_set_name(&priv->subsys.kobj, "%s", bus->name);
8761da177e4SLinus Torvalds 	if (retval)
8771da177e4SLinus Torvalds 		goto out;
8781da177e4SLinus Torvalds 
879c6f7e72aSGreg Kroah-Hartman 	priv->subsys.kobj.kset = bus_kset;
880c6f7e72aSGreg Kroah-Hartman 	priv->subsys.kobj.ktype = &bus_ktype;
881c6f7e72aSGreg Kroah-Hartman 	priv->drivers_autoprobe = 1;
882d6b05b84SGreg Kroah-Hartman 
883c6f7e72aSGreg Kroah-Hartman 	retval = kset_register(&priv->subsys);
8841da177e4SLinus Torvalds 	if (retval)
8851da177e4SLinus Torvalds 		goto out;
8861da177e4SLinus Torvalds 
8877ac1cf4aSKay Sievers 	retval = bus_create_file(bus, &bus_attr_uevent);
8887ac1cf4aSKay Sievers 	if (retval)
8897ac1cf4aSKay Sievers 		goto bus_uevent_fail;
8907ac1cf4aSKay Sievers 
891c6f7e72aSGreg Kroah-Hartman 	priv->devices_kset = kset_create_and_add("devices", NULL,
892c6f7e72aSGreg Kroah-Hartman 						 &priv->subsys.kobj);
893c6f7e72aSGreg Kroah-Hartman 	if (!priv->devices_kset) {
8943d899596SGreg Kroah-Hartman 		retval = -ENOMEM;
8951da177e4SLinus Torvalds 		goto bus_devices_fail;
8963d899596SGreg Kroah-Hartman 	}
8971da177e4SLinus Torvalds 
898c6f7e72aSGreg Kroah-Hartman 	priv->drivers_kset = kset_create_and_add("drivers", NULL,
899c6f7e72aSGreg Kroah-Hartman 						 &priv->subsys.kobj);
900c6f7e72aSGreg Kroah-Hartman 	if (!priv->drivers_kset) {
9016dcec251SGreg Kroah-Hartman 		retval = -ENOMEM;
9021da177e4SLinus Torvalds 		goto bus_drivers_fail;
9036dcec251SGreg Kroah-Hartman 	}
904465c7a3aSmochel@digitalimplant.org 
905ca22e56dSKay Sievers 	INIT_LIST_HEAD(&priv->interfaces);
906ca22e56dSKay Sievers 	__mutex_init(&priv->mutex, "subsys mutex", key);
907c6f7e72aSGreg Kroah-Hartman 	klist_init(&priv->klist_devices, klist_devices_get, klist_devices_put);
908c6f7e72aSGreg Kroah-Hartman 	klist_init(&priv->klist_drivers, NULL, NULL);
909b8c5cec2SKay Sievers 
910b8c5cec2SKay Sievers 	retval = add_probe_files(bus);
911b8c5cec2SKay Sievers 	if (retval)
912b8c5cec2SKay Sievers 		goto bus_probe_files_fail;
913b8c5cec2SKay Sievers 
91412478ba0SGreg Kroah-Hartman 	retval = bus_add_groups(bus, bus->bus_groups);
91512478ba0SGreg Kroah-Hartman 	if (retval)
91612478ba0SGreg Kroah-Hartman 		goto bus_groups_fail;
9171da177e4SLinus Torvalds 
9187dc72b28SGreg Kroah-Hartman 	pr_debug("bus: '%s': registered\n", bus->name);
9191da177e4SLinus Torvalds 	return 0;
9201da177e4SLinus Torvalds 
92112478ba0SGreg Kroah-Hartman bus_groups_fail:
922b8c5cec2SKay Sievers 	remove_probe_files(bus);
923b8c5cec2SKay Sievers bus_probe_files_fail:
924c6f7e72aSGreg Kroah-Hartman 	kset_unregister(bus->p->drivers_kset);
9251da177e4SLinus Torvalds bus_drivers_fail:
926c6f7e72aSGreg Kroah-Hartman 	kset_unregister(bus->p->devices_kset);
9271da177e4SLinus Torvalds bus_devices_fail:
9287ac1cf4aSKay Sievers 	bus_remove_file(bus, &bus_attr_uevent);
9297ac1cf4aSKay Sievers bus_uevent_fail:
930c6f7e72aSGreg Kroah-Hartman 	kset_unregister(&bus->p->subsys);
9311da177e4SLinus Torvalds out:
932600c20f3SJike Song 	kfree(bus->p);
933f48f3febSDave Young 	bus->p = NULL;
9341da177e4SLinus Torvalds 	return retval;
9351da177e4SLinus Torvalds }
936be871b7eSMichal Hocko EXPORT_SYMBOL_GPL(bus_register);
9371da177e4SLinus Torvalds 
9381da177e4SLinus Torvalds /**
9391da177e4SLinus Torvalds  * bus_unregister - remove a bus from the system
9401da177e4SLinus Torvalds  * @bus: bus.
9411da177e4SLinus Torvalds  *
9421da177e4SLinus Torvalds  * Unregister the child subsystems and the bus itself.
943fc1ede58SGreg Kroah-Hartman  * Finally, we call bus_put() to release the refcount
9441da177e4SLinus Torvalds  */
9451da177e4SLinus Torvalds void bus_unregister(struct bus_type *bus)
9461da177e4SLinus Torvalds {
9477dc72b28SGreg Kroah-Hartman 	pr_debug("bus: '%s': unregistering\n", bus->name);
948ca22e56dSKay Sievers 	if (bus->dev_root)
949ca22e56dSKay Sievers 		device_unregister(bus->dev_root);
95012478ba0SGreg Kroah-Hartman 	bus_remove_groups(bus, bus->bus_groups);
951b8c5cec2SKay Sievers 	remove_probe_files(bus);
952c6f7e72aSGreg Kroah-Hartman 	kset_unregister(bus->p->drivers_kset);
953c6f7e72aSGreg Kroah-Hartman 	kset_unregister(bus->p->devices_kset);
9547ac1cf4aSKay Sievers 	bus_remove_file(bus, &bus_attr_uevent);
955c6f7e72aSGreg Kroah-Hartman 	kset_unregister(&bus->p->subsys);
956c6f7e72aSGreg Kroah-Hartman 	kfree(bus->p);
957f48f3febSDave Young 	bus->p = NULL;
9581da177e4SLinus Torvalds }
9594a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_unregister);
9601da177e4SLinus Torvalds 
961116af378SBenjamin Herrenschmidt int bus_register_notifier(struct bus_type *bus, struct notifier_block *nb)
962116af378SBenjamin Herrenschmidt {
963c6f7e72aSGreg Kroah-Hartman 	return blocking_notifier_chain_register(&bus->p->bus_notifier, nb);
964116af378SBenjamin Herrenschmidt }
965116af378SBenjamin Herrenschmidt EXPORT_SYMBOL_GPL(bus_register_notifier);
966116af378SBenjamin Herrenschmidt 
967116af378SBenjamin Herrenschmidt int bus_unregister_notifier(struct bus_type *bus, struct notifier_block *nb)
968116af378SBenjamin Herrenschmidt {
969c6f7e72aSGreg Kroah-Hartman 	return blocking_notifier_chain_unregister(&bus->p->bus_notifier, nb);
970116af378SBenjamin Herrenschmidt }
971116af378SBenjamin Herrenschmidt EXPORT_SYMBOL_GPL(bus_unregister_notifier);
972116af378SBenjamin Herrenschmidt 
9730fed80f7SGreg Kroah-Hartman struct kset *bus_get_kset(struct bus_type *bus)
9740fed80f7SGreg Kroah-Hartman {
975c6f7e72aSGreg Kroah-Hartman 	return &bus->p->subsys;
9760fed80f7SGreg Kroah-Hartman }
9770fed80f7SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_get_kset);
9780fed80f7SGreg Kroah-Hartman 
979b249072eSGreg Kroah-Hartman struct klist *bus_get_device_klist(struct bus_type *bus)
980b249072eSGreg Kroah-Hartman {
981c6f7e72aSGreg Kroah-Hartman 	return &bus->p->klist_devices;
982b249072eSGreg Kroah-Hartman }
983b249072eSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_get_device_klist);
984b249072eSGreg Kroah-Hartman 
98599178b03SGreg Kroah-Hartman /*
986dca25ebdSRobert P. J. Day  * Yes, this forcibly breaks the klist abstraction temporarily.  It
98799178b03SGreg Kroah-Hartman  * just wants to sort the klist, not change reference counts and
98899178b03SGreg Kroah-Hartman  * take/drop locks rapidly in the process.  It does all this while
98999178b03SGreg Kroah-Hartman  * holding the lock for the list, so objects can't otherwise be
99099178b03SGreg Kroah-Hartman  * added/removed while we're swizzling.
99199178b03SGreg Kroah-Hartman  */
99299178b03SGreg Kroah-Hartman static void device_insertion_sort_klist(struct device *a, struct list_head *list,
99399178b03SGreg Kroah-Hartman 					int (*compare)(const struct device *a,
99499178b03SGreg Kroah-Hartman 							const struct device *b))
99599178b03SGreg Kroah-Hartman {
99699178b03SGreg Kroah-Hartman 	struct list_head *pos;
99799178b03SGreg Kroah-Hartman 	struct klist_node *n;
998ae1b4171SGreg Kroah-Hartman 	struct device_private *dev_prv;
99999178b03SGreg Kroah-Hartman 	struct device *b;
100099178b03SGreg Kroah-Hartman 
100199178b03SGreg Kroah-Hartman 	list_for_each(pos, list) {
100299178b03SGreg Kroah-Hartman 		n = container_of(pos, struct klist_node, n_node);
1003ae1b4171SGreg Kroah-Hartman 		dev_prv = to_device_private_bus(n);
1004ae1b4171SGreg Kroah-Hartman 		b = dev_prv->device;
100599178b03SGreg Kroah-Hartman 		if (compare(a, b) <= 0) {
1006ae1b4171SGreg Kroah-Hartman 			list_move_tail(&a->p->knode_bus.n_node,
1007ae1b4171SGreg Kroah-Hartman 				       &b->p->knode_bus.n_node);
100899178b03SGreg Kroah-Hartman 			return;
100999178b03SGreg Kroah-Hartman 		}
101099178b03SGreg Kroah-Hartman 	}
1011ae1b4171SGreg Kroah-Hartman 	list_move_tail(&a->p->knode_bus.n_node, list);
101299178b03SGreg Kroah-Hartman }
101399178b03SGreg Kroah-Hartman 
101499178b03SGreg Kroah-Hartman void bus_sort_breadthfirst(struct bus_type *bus,
101599178b03SGreg Kroah-Hartman 			   int (*compare)(const struct device *a,
101699178b03SGreg Kroah-Hartman 					  const struct device *b))
101799178b03SGreg Kroah-Hartman {
101899178b03SGreg Kroah-Hartman 	LIST_HEAD(sorted_devices);
101999178b03SGreg Kroah-Hartman 	struct list_head *pos, *tmp;
102099178b03SGreg Kroah-Hartman 	struct klist_node *n;
1021ae1b4171SGreg Kroah-Hartman 	struct device_private *dev_prv;
102299178b03SGreg Kroah-Hartman 	struct device *dev;
102399178b03SGreg Kroah-Hartman 	struct klist *device_klist;
102499178b03SGreg Kroah-Hartman 
102599178b03SGreg Kroah-Hartman 	device_klist = bus_get_device_klist(bus);
102699178b03SGreg Kroah-Hartman 
102799178b03SGreg Kroah-Hartman 	spin_lock(&device_klist->k_lock);
102899178b03SGreg Kroah-Hartman 	list_for_each_safe(pos, tmp, &device_klist->k_list) {
102999178b03SGreg Kroah-Hartman 		n = container_of(pos, struct klist_node, n_node);
1030ae1b4171SGreg Kroah-Hartman 		dev_prv = to_device_private_bus(n);
1031ae1b4171SGreg Kroah-Hartman 		dev = dev_prv->device;
103299178b03SGreg Kroah-Hartman 		device_insertion_sort_klist(dev, &sorted_devices, compare);
103399178b03SGreg Kroah-Hartman 	}
103499178b03SGreg Kroah-Hartman 	list_splice(&sorted_devices, &device_klist->k_list);
103599178b03SGreg Kroah-Hartman 	spin_unlock(&device_klist->k_lock);
103699178b03SGreg Kroah-Hartman }
103799178b03SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_sort_breadthfirst);
103899178b03SGreg Kroah-Hartman 
1039ca22e56dSKay Sievers /**
1040ca22e56dSKay Sievers  * subsys_dev_iter_init - initialize subsys device iterator
1041ca22e56dSKay Sievers  * @iter: subsys iterator to initialize
1042ca22e56dSKay Sievers  * @subsys: the subsys we wanna iterate over
1043ca22e56dSKay Sievers  * @start: the device to start iterating from, if any
1044ca22e56dSKay Sievers  * @type: device_type of the devices to iterate over, NULL for all
1045ca22e56dSKay Sievers  *
1046ca22e56dSKay Sievers  * Initialize subsys iterator @iter such that it iterates over devices
1047ca22e56dSKay Sievers  * of @subsys.  If @start is set, the list iteration will start there,
1048ca22e56dSKay Sievers  * otherwise if it is NULL, the iteration starts at the beginning of
1049ca22e56dSKay Sievers  * the list.
1050ca22e56dSKay Sievers  */
10517cd9c9bbSGreg Kroah-Hartman void subsys_dev_iter_init(struct subsys_dev_iter *iter, struct bus_type *subsys,
1052ca22e56dSKay Sievers 			  struct device *start, const struct device_type *type)
1053ca22e56dSKay Sievers {
1054ca22e56dSKay Sievers 	struct klist_node *start_knode = NULL;
1055ca22e56dSKay Sievers 
1056ca22e56dSKay Sievers 	if (start)
1057ca22e56dSKay Sievers 		start_knode = &start->p->knode_bus;
10587cd9c9bbSGreg Kroah-Hartman 	klist_iter_init_node(&subsys->p->klist_devices, &iter->ki, start_knode);
1059ca22e56dSKay Sievers 	iter->type = type;
1060ca22e56dSKay Sievers }
1061ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_dev_iter_init);
1062ca22e56dSKay Sievers 
1063ca22e56dSKay Sievers /**
1064ca22e56dSKay Sievers  * subsys_dev_iter_next - iterate to the next device
1065ca22e56dSKay Sievers  * @iter: subsys iterator to proceed
1066ca22e56dSKay Sievers  *
1067ca22e56dSKay Sievers  * Proceed @iter to the next device and return it.  Returns NULL if
1068ca22e56dSKay Sievers  * iteration is complete.
1069ca22e56dSKay Sievers  *
1070ca22e56dSKay Sievers  * The returned device is referenced and won't be released till
1071ca22e56dSKay Sievers  * iterator is proceed to the next device or exited.  The caller is
1072ca22e56dSKay Sievers  * free to do whatever it wants to do with the device including
1073ca22e56dSKay Sievers  * calling back into subsys code.
1074ca22e56dSKay Sievers  */
1075ca22e56dSKay Sievers struct device *subsys_dev_iter_next(struct subsys_dev_iter *iter)
1076ca22e56dSKay Sievers {
1077ca22e56dSKay Sievers 	struct klist_node *knode;
1078ca22e56dSKay Sievers 	struct device *dev;
1079ca22e56dSKay Sievers 
1080ca22e56dSKay Sievers 	for (;;) {
1081ca22e56dSKay Sievers 		knode = klist_next(&iter->ki);
1082ca22e56dSKay Sievers 		if (!knode)
1083ca22e56dSKay Sievers 			return NULL;
1084ca22e56dSKay Sievers 		dev = container_of(knode, struct device_private, knode_bus)->device;
1085ca22e56dSKay Sievers 		if (!iter->type || iter->type == dev->type)
1086ca22e56dSKay Sievers 			return dev;
1087ca22e56dSKay Sievers 	}
1088ca22e56dSKay Sievers }
1089ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_dev_iter_next);
1090ca22e56dSKay Sievers 
1091ca22e56dSKay Sievers /**
1092ca22e56dSKay Sievers  * subsys_dev_iter_exit - finish iteration
1093ca22e56dSKay Sievers  * @iter: subsys iterator to finish
1094ca22e56dSKay Sievers  *
1095ca22e56dSKay Sievers  * Finish an iteration.  Always call this function after iteration is
1096ca22e56dSKay Sievers  * complete whether the iteration ran till the end or not.
1097ca22e56dSKay Sievers  */
1098ca22e56dSKay Sievers void subsys_dev_iter_exit(struct subsys_dev_iter *iter)
1099ca22e56dSKay Sievers {
1100ca22e56dSKay Sievers 	klist_iter_exit(&iter->ki);
1101ca22e56dSKay Sievers }
1102ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_dev_iter_exit);
1103ca22e56dSKay Sievers 
1104ca22e56dSKay Sievers int subsys_interface_register(struct subsys_interface *sif)
1105ca22e56dSKay Sievers {
1106ca22e56dSKay Sievers 	struct bus_type *subsys;
1107ca22e56dSKay Sievers 	struct subsys_dev_iter iter;
1108ca22e56dSKay Sievers 	struct device *dev;
1109ca22e56dSKay Sievers 
1110ca22e56dSKay Sievers 	if (!sif || !sif->subsys)
1111ca22e56dSKay Sievers 		return -ENODEV;
1112ca22e56dSKay Sievers 
1113ca22e56dSKay Sievers 	subsys = bus_get(sif->subsys);
1114ca22e56dSKay Sievers 	if (!subsys)
1115ca22e56dSKay Sievers 		return -EINVAL;
1116ca22e56dSKay Sievers 
1117ca22e56dSKay Sievers 	mutex_lock(&subsys->p->mutex);
1118ca22e56dSKay Sievers 	list_add_tail(&sif->node, &subsys->p->interfaces);
1119ca22e56dSKay Sievers 	if (sif->add_dev) {
1120ca22e56dSKay Sievers 		subsys_dev_iter_init(&iter, subsys, NULL, NULL);
1121ca22e56dSKay Sievers 		while ((dev = subsys_dev_iter_next(&iter)))
1122ca22e56dSKay Sievers 			sif->add_dev(dev, sif);
1123ca22e56dSKay Sievers 		subsys_dev_iter_exit(&iter);
1124ca22e56dSKay Sievers 	}
1125ca22e56dSKay Sievers 	mutex_unlock(&subsys->p->mutex);
1126ca22e56dSKay Sievers 
1127ca22e56dSKay Sievers 	return 0;
1128ca22e56dSKay Sievers }
1129ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_interface_register);
1130ca22e56dSKay Sievers 
1131ca22e56dSKay Sievers void subsys_interface_unregister(struct subsys_interface *sif)
1132ca22e56dSKay Sievers {
11332b31594aSJonghwan Choi 	struct bus_type *subsys;
1134ca22e56dSKay Sievers 	struct subsys_dev_iter iter;
1135ca22e56dSKay Sievers 	struct device *dev;
1136ca22e56dSKay Sievers 
11372b31594aSJonghwan Choi 	if (!sif || !sif->subsys)
1138ca22e56dSKay Sievers 		return;
1139ca22e56dSKay Sievers 
11402b31594aSJonghwan Choi 	subsys = sif->subsys;
11412b31594aSJonghwan Choi 
1142ca22e56dSKay Sievers 	mutex_lock(&subsys->p->mutex);
1143ca22e56dSKay Sievers 	list_del_init(&sif->node);
1144ca22e56dSKay Sievers 	if (sif->remove_dev) {
1145ca22e56dSKay Sievers 		subsys_dev_iter_init(&iter, subsys, NULL, NULL);
1146ca22e56dSKay Sievers 		while ((dev = subsys_dev_iter_next(&iter)))
1147ca22e56dSKay Sievers 			sif->remove_dev(dev, sif);
1148ca22e56dSKay Sievers 		subsys_dev_iter_exit(&iter);
1149ca22e56dSKay Sievers 	}
1150ca22e56dSKay Sievers 	mutex_unlock(&subsys->p->mutex);
1151ca22e56dSKay Sievers 
1152ca22e56dSKay Sievers 	bus_put(subsys);
1153ca22e56dSKay Sievers }
1154ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_interface_unregister);
1155ca22e56dSKay Sievers 
1156ca22e56dSKay Sievers static void system_root_device_release(struct device *dev)
1157ca22e56dSKay Sievers {
1158ca22e56dSKay Sievers 	kfree(dev);
1159ca22e56dSKay Sievers }
1160d73ce004STejun Heo 
1161d73ce004STejun Heo static int subsys_register(struct bus_type *subsys,
1162d73ce004STejun Heo 			   const struct attribute_group **groups,
1163d73ce004STejun Heo 			   struct kobject *parent_of_root)
1164d73ce004STejun Heo {
1165d73ce004STejun Heo 	struct device *dev;
1166d73ce004STejun Heo 	int err;
1167d73ce004STejun Heo 
1168d73ce004STejun Heo 	err = bus_register(subsys);
1169d73ce004STejun Heo 	if (err < 0)
1170d73ce004STejun Heo 		return err;
1171d73ce004STejun Heo 
1172d73ce004STejun Heo 	dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1173d73ce004STejun Heo 	if (!dev) {
1174d73ce004STejun Heo 		err = -ENOMEM;
1175d73ce004STejun Heo 		goto err_dev;
1176d73ce004STejun Heo 	}
1177d73ce004STejun Heo 
1178d73ce004STejun Heo 	err = dev_set_name(dev, "%s", subsys->name);
1179d73ce004STejun Heo 	if (err < 0)
1180d73ce004STejun Heo 		goto err_name;
1181d73ce004STejun Heo 
1182d73ce004STejun Heo 	dev->kobj.parent = parent_of_root;
1183d73ce004STejun Heo 	dev->groups = groups;
1184d73ce004STejun Heo 	dev->release = system_root_device_release;
1185d73ce004STejun Heo 
1186d73ce004STejun Heo 	err = device_register(dev);
1187d73ce004STejun Heo 	if (err < 0)
1188d73ce004STejun Heo 		goto err_dev_reg;
1189d73ce004STejun Heo 
1190d73ce004STejun Heo 	subsys->dev_root = dev;
1191d73ce004STejun Heo 	return 0;
1192d73ce004STejun Heo 
1193d73ce004STejun Heo err_dev_reg:
1194d73ce004STejun Heo 	put_device(dev);
1195d73ce004STejun Heo 	dev = NULL;
1196d73ce004STejun Heo err_name:
1197d73ce004STejun Heo 	kfree(dev);
1198d73ce004STejun Heo err_dev:
1199d73ce004STejun Heo 	bus_unregister(subsys);
1200d73ce004STejun Heo 	return err;
1201d73ce004STejun Heo }
1202d73ce004STejun Heo 
1203ca22e56dSKay Sievers /**
1204ca22e56dSKay Sievers  * subsys_system_register - register a subsystem at /sys/devices/system/
120578d79559SRandy Dunlap  * @subsys: system subsystem
120678d79559SRandy Dunlap  * @groups: default attributes for the root device
1207ca22e56dSKay Sievers  *
1208ca22e56dSKay Sievers  * All 'system' subsystems have a /sys/devices/system/<name> root device
1209ca22e56dSKay Sievers  * with the name of the subsystem. The root device can carry subsystem-
1210ca22e56dSKay Sievers  * wide attributes. All registered devices are below this single root
1211ca22e56dSKay Sievers  * device and are named after the subsystem with a simple enumeration
1212ca22e56dSKay Sievers  * number appended. The registered devices are not explicitely named;
1213ca22e56dSKay Sievers  * only 'id' in the device needs to be set.
1214ca22e56dSKay Sievers  *
1215ca22e56dSKay Sievers  * Do not use this interface for anything new, it exists for compatibility
1216ca22e56dSKay Sievers  * with bad ideas only. New subsystems should use plain subsystems; and
1217ca22e56dSKay Sievers  * add the subsystem-wide attributes should be added to the subsystem
1218ca22e56dSKay Sievers  * directory itself and not some create fake root-device placed in
1219ca22e56dSKay Sievers  * /sys/devices/system/<name>.
1220ca22e56dSKay Sievers  */
1221ca22e56dSKay Sievers int subsys_system_register(struct bus_type *subsys,
1222ca22e56dSKay Sievers 			   const struct attribute_group **groups)
1223ca22e56dSKay Sievers {
1224d73ce004STejun Heo 	return subsys_register(subsys, groups, &system_kset->kobj);
1225ca22e56dSKay Sievers }
1226ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_system_register);
1227ca22e56dSKay Sievers 
1228d73ce004STejun Heo /**
1229d73ce004STejun Heo  * subsys_virtual_register - register a subsystem at /sys/devices/virtual/
1230d73ce004STejun Heo  * @subsys: virtual subsystem
1231d73ce004STejun Heo  * @groups: default attributes for the root device
1232d73ce004STejun Heo  *
1233d73ce004STejun Heo  * All 'virtual' subsystems have a /sys/devices/system/<name> root device
1234d73ce004STejun Heo  * with the name of the subystem.  The root device can carry subsystem-wide
1235d73ce004STejun Heo  * attributes.  All registered devices are below this single root device.
1236d73ce004STejun Heo  * There's no restriction on device naming.  This is for kernel software
1237d73ce004STejun Heo  * constructs which need sysfs interface.
1238d73ce004STejun Heo  */
1239d73ce004STejun Heo int subsys_virtual_register(struct bus_type *subsys,
1240d73ce004STejun Heo 			    const struct attribute_group **groups)
1241d73ce004STejun Heo {
1242d73ce004STejun Heo 	struct kobject *virtual_dir;
1243d73ce004STejun Heo 
1244d73ce004STejun Heo 	virtual_dir = virtual_device_parent(NULL);
1245d73ce004STejun Heo 	if (!virtual_dir)
1246d73ce004STejun Heo 		return -ENOMEM;
1247d73ce004STejun Heo 
1248d73ce004STejun Heo 	return subsys_register(subsys, groups, virtual_dir);
1249d73ce004STejun Heo }
12501c04fc35SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(subsys_virtual_register);
1251d73ce004STejun Heo 
12521da177e4SLinus Torvalds int __init buses_init(void)
12531da177e4SLinus Torvalds {
125459a54833SGreg Kroah-Hartman 	bus_kset = kset_create_and_add("bus", &bus_uevent_ops, NULL);
125559a54833SGreg Kroah-Hartman 	if (!bus_kset)
125659a54833SGreg Kroah-Hartman 		return -ENOMEM;
1257ca22e56dSKay Sievers 
1258ca22e56dSKay Sievers 	system_kset = kset_create_and_add("system", NULL, &devices_kset->kobj);
1259ca22e56dSKay Sievers 	if (!system_kset)
1260ca22e56dSKay Sievers 		return -ENOMEM;
1261ca22e56dSKay Sievers 
126259a54833SGreg Kroah-Hartman 	return 0;
12631da177e4SLinus Torvalds }
1264