xref: /linux/drivers/base/bus.c (revision 0372ffb35d00288802265586a29c117911d02fb8)
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 
149174be70bSBart Van Assche static void bus_release(struct kobject *kobj)
150174be70bSBart Van Assche {
151174be70bSBart Van Assche 	struct subsys_private *priv =
152174be70bSBart Van Assche 		container_of(kobj, typeof(*priv), subsys.kobj);
153174be70bSBart Van Assche 	struct bus_type *bus = priv->bus;
154174be70bSBart Van Assche 
155174be70bSBart Van Assche 	kfree(priv);
156174be70bSBart Van Assche 	bus->p = NULL;
157174be70bSBart Van Assche }
158174be70bSBart Van Assche 
15980f03e34SKay Sievers static struct kobj_type bus_ktype = {
1601da177e4SLinus Torvalds 	.sysfs_ops	= &bus_sysfs_ops,
161174be70bSBart Van Assche 	.release	= bus_release,
1621da177e4SLinus Torvalds };
1631da177e4SLinus Torvalds 
16480f03e34SKay Sievers static int bus_uevent_filter(struct kset *kset, struct kobject *kobj)
16580f03e34SKay Sievers {
16680f03e34SKay Sievers 	struct kobj_type *ktype = get_ktype(kobj);
16780f03e34SKay Sievers 
16880f03e34SKay Sievers 	if (ktype == &bus_ktype)
16980f03e34SKay Sievers 		return 1;
17080f03e34SKay Sievers 	return 0;
17180f03e34SKay Sievers }
17280f03e34SKay Sievers 
1739cd43611SEmese Revfy static const struct kset_uevent_ops bus_uevent_ops = {
17480f03e34SKay Sievers 	.filter = bus_uevent_filter,
17580f03e34SKay Sievers };
17680f03e34SKay Sievers 
17759a54833SGreg Kroah-Hartman static struct kset *bus_kset;
1781da177e4SLinus Torvalds 
1792b08c8d0SAlan Stern /* Manually detach a device from its associated driver. */
1802581c9ccSGreg Kroah-Hartman static ssize_t unbind_store(struct device_driver *drv, const char *buf,
1812581c9ccSGreg Kroah-Hartman 			    size_t count)
182151ef38fSGreg Kroah-Hartman {
1835901d014SGreg Kroah-Hartman 	struct bus_type *bus = bus_get(drv->bus);
184151ef38fSGreg Kroah-Hartman 	struct device *dev;
185151ef38fSGreg Kroah-Hartman 	int err = -ENODEV;
186151ef38fSGreg Kroah-Hartman 
1871f9ffc04SGreg Kroah-Hartman 	dev = bus_find_device_by_name(bus, NULL, buf);
1882b08c8d0SAlan Stern 	if (dev && dev->driver == drv) {
189bf74ad5bSAlan Stern 		if (dev->parent)	/* Needed for USB */
1908e9394ceSGreg Kroah-Hartman 			device_lock(dev->parent);
191151ef38fSGreg Kroah-Hartman 		device_release_driver(dev);
192bf74ad5bSAlan Stern 		if (dev->parent)
1938e9394ceSGreg Kroah-Hartman 			device_unlock(dev->parent);
194151ef38fSGreg Kroah-Hartman 		err = count;
195151ef38fSGreg Kroah-Hartman 	}
1962b08c8d0SAlan Stern 	put_device(dev);
197fc1ede58SGreg Kroah-Hartman 	bus_put(bus);
198151ef38fSGreg Kroah-Hartman 	return err;
199151ef38fSGreg Kroah-Hartman }
2002581c9ccSGreg Kroah-Hartman static DRIVER_ATTR_WO(unbind);
201151ef38fSGreg Kroah-Hartman 
202afdce75fSGreg Kroah-Hartman /*
203afdce75fSGreg Kroah-Hartman  * Manually attach a device to a driver.
204afdce75fSGreg Kroah-Hartman  * Note: the driver must want to bind to the device,
205afdce75fSGreg Kroah-Hartman  * it is not possible to override the driver's id table.
206afdce75fSGreg Kroah-Hartman  */
2072581c9ccSGreg Kroah-Hartman static ssize_t bind_store(struct device_driver *drv, const char *buf,
2082581c9ccSGreg Kroah-Hartman 			  size_t count)
209afdce75fSGreg Kroah-Hartman {
2105901d014SGreg Kroah-Hartman 	struct bus_type *bus = bus_get(drv->bus);
211afdce75fSGreg Kroah-Hartman 	struct device *dev;
212afdce75fSGreg Kroah-Hartman 	int err = -ENODEV;
213afdce75fSGreg Kroah-Hartman 
2141f9ffc04SGreg Kroah-Hartman 	dev = bus_find_device_by_name(bus, NULL, buf);
21549b420a1SMing Lei 	if (dev && dev->driver == NULL && driver_match_device(drv, dev)) {
216bf74ad5bSAlan Stern 		if (dev->parent)	/* Needed for USB */
2178e9394ceSGreg Kroah-Hartman 			device_lock(dev->parent);
2188e9394ceSGreg Kroah-Hartman 		device_lock(dev);
219afdce75fSGreg Kroah-Hartman 		err = driver_probe_device(drv, dev);
2208e9394ceSGreg Kroah-Hartman 		device_unlock(dev);
221bf74ad5bSAlan Stern 		if (dev->parent)
2228e9394ceSGreg Kroah-Hartman 			device_unlock(dev->parent);
22337225401SRyan Wilson 
2244a3ad20cSGreg Kroah-Hartman 		if (err > 0) {
2254a3ad20cSGreg Kroah-Hartman 			/* success */
22637225401SRyan Wilson 			err = count;
2274a3ad20cSGreg Kroah-Hartman 		} else if (err == 0) {
2284a3ad20cSGreg Kroah-Hartman 			/* driver didn't accept device */
22937225401SRyan Wilson 			err = -ENODEV;
230afdce75fSGreg Kroah-Hartman 		}
2314a3ad20cSGreg Kroah-Hartman 	}
2322b08c8d0SAlan Stern 	put_device(dev);
233fc1ede58SGreg Kroah-Hartman 	bus_put(bus);
234afdce75fSGreg Kroah-Hartman 	return err;
235afdce75fSGreg Kroah-Hartman }
2362581c9ccSGreg Kroah-Hartman static DRIVER_ATTR_WO(bind);
237afdce75fSGreg Kroah-Hartman 
238b8c5cec2SKay Sievers static ssize_t show_drivers_autoprobe(struct bus_type *bus, char *buf)
239b8c5cec2SKay Sievers {
240c6f7e72aSGreg Kroah-Hartman 	return sprintf(buf, "%d\n", bus->p->drivers_autoprobe);
241b8c5cec2SKay Sievers }
242b8c5cec2SKay Sievers 
243b8c5cec2SKay Sievers static ssize_t store_drivers_autoprobe(struct bus_type *bus,
244b8c5cec2SKay Sievers 				       const char *buf, size_t count)
245b8c5cec2SKay Sievers {
246b8c5cec2SKay Sievers 	if (buf[0] == '0')
247c6f7e72aSGreg Kroah-Hartman 		bus->p->drivers_autoprobe = 0;
248b8c5cec2SKay Sievers 	else
249c6f7e72aSGreg Kroah-Hartman 		bus->p->drivers_autoprobe = 1;
250b8c5cec2SKay Sievers 	return count;
251b8c5cec2SKay Sievers }
252b8c5cec2SKay Sievers 
253b8c5cec2SKay Sievers static ssize_t store_drivers_probe(struct bus_type *bus,
254b8c5cec2SKay Sievers 				   const char *buf, size_t count)
255b8c5cec2SKay Sievers {
256b8c5cec2SKay Sievers 	struct device *dev;
257*0372ffb3SAlex Williamson 	int err = -EINVAL;
258b8c5cec2SKay Sievers 
2591f9ffc04SGreg Kroah-Hartman 	dev = bus_find_device_by_name(bus, NULL, buf);
260b8c5cec2SKay Sievers 	if (!dev)
261b8c5cec2SKay Sievers 		return -ENODEV;
262*0372ffb3SAlex Williamson 	if (bus_rescan_devices_helper(dev, NULL) == 0)
263*0372ffb3SAlex Williamson 		err = count;
264*0372ffb3SAlex Williamson 	put_device(dev);
265*0372ffb3SAlex Williamson 	return err;
266b8c5cec2SKay Sievers }
267151ef38fSGreg Kroah-Hartman 
268465c7a3aSmochel@digitalimplant.org static struct device *next_device(struct klist_iter *i)
269465c7a3aSmochel@digitalimplant.org {
270465c7a3aSmochel@digitalimplant.org 	struct klist_node *n = klist_next(i);
271ae1b4171SGreg Kroah-Hartman 	struct device *dev = NULL;
272ae1b4171SGreg Kroah-Hartman 	struct device_private *dev_prv;
273ae1b4171SGreg Kroah-Hartman 
274ae1b4171SGreg Kroah-Hartman 	if (n) {
275ae1b4171SGreg Kroah-Hartman 		dev_prv = to_device_private_bus(n);
276ae1b4171SGreg Kroah-Hartman 		dev = dev_prv->device;
277ae1b4171SGreg Kroah-Hartman 	}
278ae1b4171SGreg Kroah-Hartman 	return dev;
279465c7a3aSmochel@digitalimplant.org }
280465c7a3aSmochel@digitalimplant.org 
2811da177e4SLinus Torvalds /**
2821da177e4SLinus Torvalds  * bus_for_each_dev - device iterator.
2831da177e4SLinus Torvalds  * @bus: bus type.
2841da177e4SLinus Torvalds  * @start: device to start iterating from.
2851da177e4SLinus Torvalds  * @data: data for the callback.
2861da177e4SLinus Torvalds  * @fn: function to be called for each device.
2871da177e4SLinus Torvalds  *
2881da177e4SLinus Torvalds  * Iterate over @bus's list of devices, and call @fn for each,
2891da177e4SLinus Torvalds  * passing it @data. If @start is not NULL, we use that device to
2901da177e4SLinus Torvalds  * begin iterating from.
2911da177e4SLinus Torvalds  *
2921da177e4SLinus Torvalds  * We check the return of @fn each time. If it returns anything
2931da177e4SLinus Torvalds  * other than 0, we break out and return that value.
2941da177e4SLinus Torvalds  *
2951da177e4SLinus Torvalds  * NOTE: The device that returns a non-zero value is not retained
2961da177e4SLinus Torvalds  * in any way, nor is its refcount incremented. If the caller needs
2970fa1b0a1SAlex Chiang  * to retain this data, it should do so, and increment the reference
2981da177e4SLinus Torvalds  * count in the supplied callback.
2991da177e4SLinus Torvalds  */
3001da177e4SLinus Torvalds int bus_for_each_dev(struct bus_type *bus, struct device *start,
3011da177e4SLinus Torvalds 		     void *data, int (*fn)(struct device *, void *))
3021da177e4SLinus Torvalds {
303465c7a3aSmochel@digitalimplant.org 	struct klist_iter i;
304465c7a3aSmochel@digitalimplant.org 	struct device *dev;
305465c7a3aSmochel@digitalimplant.org 	int error = 0;
3061da177e4SLinus Torvalds 
3074fa3e78bSBjorn Helgaas 	if (!bus || !bus->p)
308465c7a3aSmochel@digitalimplant.org 		return -EINVAL;
309465c7a3aSmochel@digitalimplant.org 
3107cd9c9bbSGreg Kroah-Hartman 	klist_iter_init_node(&bus->p->klist_devices, &i,
311ae1b4171SGreg Kroah-Hartman 			     (start ? &start->p->knode_bus : NULL));
312465c7a3aSmochel@digitalimplant.org 	while ((dev = next_device(&i)) && !error)
313465c7a3aSmochel@digitalimplant.org 		error = fn(dev, data);
314465c7a3aSmochel@digitalimplant.org 	klist_iter_exit(&i);
315465c7a3aSmochel@digitalimplant.org 	return error;
3161da177e4SLinus Torvalds }
3174a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_for_each_dev);
3181da177e4SLinus Torvalds 
3190edb5860SCornelia Huck /**
3200edb5860SCornelia Huck  * bus_find_device - device iterator for locating a particular device.
3210edb5860SCornelia Huck  * @bus: bus type
3220edb5860SCornelia Huck  * @start: Device to begin with
3230edb5860SCornelia Huck  * @data: Data to pass to match function
3240edb5860SCornelia Huck  * @match: Callback function to check device
3250edb5860SCornelia Huck  *
3260edb5860SCornelia Huck  * This is similar to the bus_for_each_dev() function above, but it
3270edb5860SCornelia Huck  * returns a reference to a device that is 'found' for later use, as
3280edb5860SCornelia Huck  * determined by the @match callback.
3290edb5860SCornelia Huck  *
3300edb5860SCornelia Huck  * The callback should return 0 if the device doesn't match and non-zero
3310edb5860SCornelia Huck  * if it does.  If the callback returns non-zero, this function will
3320edb5860SCornelia Huck  * return to the caller and not iterate over any more devices.
3330edb5860SCornelia Huck  */
3340edb5860SCornelia Huck struct device *bus_find_device(struct bus_type *bus,
3350edb5860SCornelia Huck 			       struct device *start, void *data,
3364a3ad20cSGreg Kroah-Hartman 			       int (*match)(struct device *dev, void *data))
3370edb5860SCornelia Huck {
3380edb5860SCornelia Huck 	struct klist_iter i;
3390edb5860SCornelia Huck 	struct device *dev;
3400edb5860SCornelia Huck 
3414fa3e78bSBjorn Helgaas 	if (!bus || !bus->p)
3420edb5860SCornelia Huck 		return NULL;
3430edb5860SCornelia Huck 
3447cd9c9bbSGreg Kroah-Hartman 	klist_iter_init_node(&bus->p->klist_devices, &i,
3457cd9c9bbSGreg Kroah-Hartman 			     (start ? &start->p->knode_bus : NULL));
3460edb5860SCornelia Huck 	while ((dev = next_device(&i)))
3470edb5860SCornelia Huck 		if (match(dev, data) && get_device(dev))
3480edb5860SCornelia Huck 			break;
3490edb5860SCornelia Huck 	klist_iter_exit(&i);
3500edb5860SCornelia Huck 	return dev;
3510edb5860SCornelia Huck }
3524a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_find_device);
35338fdac3cSmochel@digitalimplant.org 
3541f9ffc04SGreg Kroah-Hartman static int match_name(struct device *dev, void *data)
3551f9ffc04SGreg Kroah-Hartman {
3561f9ffc04SGreg Kroah-Hartman 	const char *name = data;
3571f9ffc04SGreg Kroah-Hartman 
3581e0b2cf9SKay Sievers 	return sysfs_streq(name, dev_name(dev));
3591f9ffc04SGreg Kroah-Hartman }
3601f9ffc04SGreg Kroah-Hartman 
3611f9ffc04SGreg Kroah-Hartman /**
3621f9ffc04SGreg Kroah-Hartman  * bus_find_device_by_name - device iterator for locating a particular device of a specific name
3631f9ffc04SGreg Kroah-Hartman  * @bus: bus type
3641f9ffc04SGreg Kroah-Hartman  * @start: Device to begin with
3651f9ffc04SGreg Kroah-Hartman  * @name: name of the device to match
3661f9ffc04SGreg Kroah-Hartman  *
3671f9ffc04SGreg Kroah-Hartman  * This is similar to the bus_find_device() function above, but it handles
3681f9ffc04SGreg Kroah-Hartman  * searching by a name automatically, no need to write another strcmp matching
3691f9ffc04SGreg Kroah-Hartman  * function.
3701f9ffc04SGreg Kroah-Hartman  */
3711f9ffc04SGreg Kroah-Hartman struct device *bus_find_device_by_name(struct bus_type *bus,
3721f9ffc04SGreg Kroah-Hartman 				       struct device *start, const char *name)
3731f9ffc04SGreg Kroah-Hartman {
3741f9ffc04SGreg Kroah-Hartman 	return bus_find_device(bus, start, (void *)name, match_name);
3751f9ffc04SGreg Kroah-Hartman }
3761f9ffc04SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_find_device_by_name);
3771f9ffc04SGreg Kroah-Hartman 
378ca22e56dSKay Sievers /**
379ca22e56dSKay Sievers  * subsys_find_device_by_id - find a device with a specific enumeration number
380ca22e56dSKay Sievers  * @subsys: subsystem
381ca22e56dSKay Sievers  * @id: index 'id' in struct device
382ca22e56dSKay Sievers  * @hint: device to check first
383ca22e56dSKay Sievers  *
384ca22e56dSKay Sievers  * Check the hint's next object and if it is a match return it directly,
385ca22e56dSKay Sievers  * otherwise, fall back to a full list search. Either way a reference for
386ca22e56dSKay Sievers  * the returned object is taken.
387ca22e56dSKay Sievers  */
388ca22e56dSKay Sievers struct device *subsys_find_device_by_id(struct bus_type *subsys, unsigned int id,
389ca22e56dSKay Sievers 					struct device *hint)
390ca22e56dSKay Sievers {
391ca22e56dSKay Sievers 	struct klist_iter i;
392ca22e56dSKay Sievers 	struct device *dev;
393ca22e56dSKay Sievers 
394ca22e56dSKay Sievers 	if (!subsys)
395ca22e56dSKay Sievers 		return NULL;
396ca22e56dSKay Sievers 
397ca22e56dSKay Sievers 	if (hint) {
3987cd9c9bbSGreg Kroah-Hartman 		klist_iter_init_node(&subsys->p->klist_devices, &i, &hint->p->knode_bus);
399ca22e56dSKay Sievers 		dev = next_device(&i);
400ca22e56dSKay Sievers 		if (dev && dev->id == id && get_device(dev)) {
401ca22e56dSKay Sievers 			klist_iter_exit(&i);
402ca22e56dSKay Sievers 			return dev;
403ca22e56dSKay Sievers 		}
404ca22e56dSKay Sievers 		klist_iter_exit(&i);
405ca22e56dSKay Sievers 	}
406ca22e56dSKay Sievers 
407ca22e56dSKay Sievers 	klist_iter_init_node(&subsys->p->klist_devices, &i, NULL);
408ca22e56dSKay Sievers 	while ((dev = next_device(&i))) {
409ca22e56dSKay Sievers 		if (dev->id == id && get_device(dev)) {
410ca22e56dSKay Sievers 			klist_iter_exit(&i);
411ca22e56dSKay Sievers 			return dev;
412ca22e56dSKay Sievers 		}
413ca22e56dSKay Sievers 	}
414ca22e56dSKay Sievers 	klist_iter_exit(&i);
415ca22e56dSKay Sievers 	return NULL;
416ca22e56dSKay Sievers }
417ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_find_device_by_id);
418ca22e56dSKay Sievers 
41938fdac3cSmochel@digitalimplant.org static struct device_driver *next_driver(struct klist_iter *i)
42038fdac3cSmochel@digitalimplant.org {
42138fdac3cSmochel@digitalimplant.org 	struct klist_node *n = klist_next(i);
422e5dd1278SGreg Kroah-Hartman 	struct driver_private *drv_priv;
423e5dd1278SGreg Kroah-Hartman 
424e5dd1278SGreg Kroah-Hartman 	if (n) {
425e5dd1278SGreg Kroah-Hartman 		drv_priv = container_of(n, struct driver_private, knode_bus);
426e5dd1278SGreg Kroah-Hartman 		return drv_priv->driver;
427e5dd1278SGreg Kroah-Hartman 	}
428e5dd1278SGreg Kroah-Hartman 	return NULL;
42938fdac3cSmochel@digitalimplant.org }
43038fdac3cSmochel@digitalimplant.org 
4311da177e4SLinus Torvalds /**
4321da177e4SLinus Torvalds  * bus_for_each_drv - driver iterator
4331da177e4SLinus Torvalds  * @bus: bus we're dealing with.
4341da177e4SLinus Torvalds  * @start: driver to start iterating on.
4351da177e4SLinus Torvalds  * @data: data to pass to the callback.
4361da177e4SLinus Torvalds  * @fn: function to call for each driver.
4371da177e4SLinus Torvalds  *
4381da177e4SLinus Torvalds  * This is nearly identical to the device iterator above.
4391da177e4SLinus Torvalds  * We iterate over each driver that belongs to @bus, and call
4401da177e4SLinus Torvalds  * @fn for each. If @fn returns anything but 0, we break out
4411da177e4SLinus Torvalds  * and return it. If @start is not NULL, we use it as the head
4421da177e4SLinus Torvalds  * of the list.
4431da177e4SLinus Torvalds  *
4441da177e4SLinus Torvalds  * NOTE: we don't return the driver that returns a non-zero
4451da177e4SLinus Torvalds  * value, nor do we leave the reference count incremented for that
4461da177e4SLinus Torvalds  * driver. If the caller needs to know that info, it must set it
4471da177e4SLinus Torvalds  * in the callback. It must also be sure to increment the refcount
4481da177e4SLinus Torvalds  * so it doesn't disappear before returning to the caller.
4491da177e4SLinus Torvalds  */
4501da177e4SLinus Torvalds int bus_for_each_drv(struct bus_type *bus, struct device_driver *start,
4511da177e4SLinus Torvalds 		     void *data, int (*fn)(struct device_driver *, void *))
4521da177e4SLinus Torvalds {
45338fdac3cSmochel@digitalimplant.org 	struct klist_iter i;
45438fdac3cSmochel@digitalimplant.org 	struct device_driver *drv;
45538fdac3cSmochel@digitalimplant.org 	int error = 0;
4561da177e4SLinus Torvalds 
45738fdac3cSmochel@digitalimplant.org 	if (!bus)
45838fdac3cSmochel@digitalimplant.org 		return -EINVAL;
45938fdac3cSmochel@digitalimplant.org 
4607cd9c9bbSGreg Kroah-Hartman 	klist_iter_init_node(&bus->p->klist_drivers, &i,
461e5dd1278SGreg Kroah-Hartman 			     start ? &start->p->knode_bus : NULL);
46238fdac3cSmochel@digitalimplant.org 	while ((drv = next_driver(&i)) && !error)
46338fdac3cSmochel@digitalimplant.org 		error = fn(drv, data);
46438fdac3cSmochel@digitalimplant.org 	klist_iter_exit(&i);
46538fdac3cSmochel@digitalimplant.org 	return error;
4661da177e4SLinus Torvalds }
4674a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_for_each_drv);
4681da177e4SLinus Torvalds 
4691da177e4SLinus Torvalds static int device_add_attrs(struct bus_type *bus, struct device *dev)
4701da177e4SLinus Torvalds {
4711da177e4SLinus Torvalds 	int error = 0;
4721da177e4SLinus Torvalds 	int i;
4731da177e4SLinus Torvalds 
4744aca67e5SAndrew Morton 	if (!bus->dev_attrs)
4754aca67e5SAndrew Morton 		return 0;
4764aca67e5SAndrew Morton 
4773e1026b3SGreg Kroah-Hartman 	for (i = 0; bus->dev_attrs[i].attr.name; i++) {
4781da177e4SLinus Torvalds 		error = device_create_file(dev, &bus->dev_attrs[i]);
4794aca67e5SAndrew Morton 		if (error) {
4801da177e4SLinus Torvalds 			while (--i >= 0)
4811da177e4SLinus Torvalds 				device_remove_file(dev, &bus->dev_attrs[i]);
4824aca67e5SAndrew Morton 			break;
4831da177e4SLinus Torvalds 		}
4844aca67e5SAndrew Morton 	}
4854aca67e5SAndrew Morton 	return error;
4864aca67e5SAndrew Morton }
4871da177e4SLinus Torvalds 
4881da177e4SLinus Torvalds static void device_remove_attrs(struct bus_type *bus, struct device *dev)
4891da177e4SLinus Torvalds {
4901da177e4SLinus Torvalds 	int i;
4911da177e4SLinus Torvalds 
4921da177e4SLinus Torvalds 	if (bus->dev_attrs) {
4933e1026b3SGreg Kroah-Hartman 		for (i = 0; bus->dev_attrs[i].attr.name; i++)
4941da177e4SLinus Torvalds 			device_remove_file(dev, &bus->dev_attrs[i]);
4951da177e4SLinus Torvalds 	}
4961da177e4SLinus Torvalds }
4971da177e4SLinus Torvalds 
4981da177e4SLinus Torvalds /**
4991da177e4SLinus Torvalds  * bus_add_device - add device to bus
5001da177e4SLinus Torvalds  * @dev: device being added
5011da177e4SLinus Torvalds  *
5022023c610SAlan Stern  * - Add device's bus attributes.
5032023c610SAlan Stern  * - Create links to device's bus.
5041da177e4SLinus Torvalds  * - Add the device to its bus's list of devices.
5051da177e4SLinus Torvalds  */
5061da177e4SLinus Torvalds int bus_add_device(struct device *dev)
5071da177e4SLinus Torvalds {
5085901d014SGreg Kroah-Hartman 	struct bus_type *bus = bus_get(dev->bus);
5091da177e4SLinus Torvalds 	int error = 0;
5101da177e4SLinus Torvalds 
5111da177e4SLinus Torvalds 	if (bus) {
5121e0b2cf9SKay Sievers 		pr_debug("bus: '%s': add device %s\n", bus->name, dev_name(dev));
513ca2b94baSHannes Reinecke 		error = device_add_attrs(bus, dev);
514f86db396SAndrew Morton 		if (error)
515513e7337SCornelia Huck 			goto out_put;
516fa6fdb33SGreg Kroah-Hartman 		error = device_add_groups(dev, bus->dev_groups);
517fa6fdb33SGreg Kroah-Hartman 		if (error)
518fa6fdb33SGreg Kroah-Hartman 			goto out_groups;
519c6f7e72aSGreg Kroah-Hartman 		error = sysfs_create_link(&bus->p->devices_kset->kobj,
5201e0b2cf9SKay Sievers 						&dev->kobj, dev_name(dev));
521f86db396SAndrew Morton 		if (error)
522513e7337SCornelia Huck 			goto out_id;
523f86db396SAndrew Morton 		error = sysfs_create_link(&dev->kobj,
524c6f7e72aSGreg Kroah-Hartman 				&dev->bus->p->subsys.kobj, "subsystem");
525f86db396SAndrew Morton 		if (error)
526513e7337SCornelia Huck 			goto out_subsys;
5272023c610SAlan Stern 		klist_add_tail(&dev->p->knode_bus, &bus->p->klist_devices);
5281da177e4SLinus Torvalds 	}
529513e7337SCornelia Huck 	return 0;
530513e7337SCornelia Huck 
531513e7337SCornelia Huck out_subsys:
5321e0b2cf9SKay Sievers 	sysfs_remove_link(&bus->p->devices_kset->kobj, dev_name(dev));
533fa6fdb33SGreg Kroah-Hartman out_groups:
534fa6fdb33SGreg Kroah-Hartman 	device_remove_groups(dev, bus->dev_groups);
535513e7337SCornelia Huck out_id:
536513e7337SCornelia Huck 	device_remove_attrs(bus, dev);
537513e7337SCornelia Huck out_put:
538fc1ede58SGreg Kroah-Hartman 	bus_put(dev->bus);
5391da177e4SLinus Torvalds 	return error;
5401da177e4SLinus Torvalds }
5411da177e4SLinus Torvalds 
5421da177e4SLinus Torvalds /**
5432023c610SAlan Stern  * bus_probe_device - probe drivers for a new device
5442023c610SAlan Stern  * @dev: device to probe
54553877d06SKay Sievers  *
5462023c610SAlan Stern  * - Automatically probe for a driver if the bus allows it.
54753877d06SKay Sievers  */
5482023c610SAlan Stern void bus_probe_device(struct device *dev)
54953877d06SKay Sievers {
55053877d06SKay Sievers 	struct bus_type *bus = dev->bus;
551ca22e56dSKay Sievers 	struct subsys_interface *sif;
5522023c610SAlan Stern 	int ret;
55353877d06SKay Sievers 
554ca22e56dSKay Sievers 	if (!bus)
555ca22e56dSKay Sievers 		return;
556ca22e56dSKay Sievers 
557ca22e56dSKay Sievers 	if (bus->p->drivers_autoprobe) {
558f86db396SAndrew Morton 		ret = device_attach(dev);
559c6a46696SCornelia Huck 		WARN_ON(ret < 0);
56053877d06SKay Sievers 	}
561ca22e56dSKay Sievers 
562ca22e56dSKay Sievers 	mutex_lock(&bus->p->mutex);
563ca22e56dSKay Sievers 	list_for_each_entry(sif, &bus->p->interfaces, node)
564ca22e56dSKay Sievers 		if (sif->add_dev)
565ca22e56dSKay Sievers 			sif->add_dev(dev, sif);
566ca22e56dSKay Sievers 	mutex_unlock(&bus->p->mutex);
567f86db396SAndrew Morton }
56853877d06SKay Sievers 
56953877d06SKay Sievers /**
5701da177e4SLinus Torvalds  * bus_remove_device - remove device from bus
5711da177e4SLinus Torvalds  * @dev: device to be removed
5721da177e4SLinus Torvalds  *
573ca22e56dSKay Sievers  * - Remove device from all interfaces.
574ca22e56dSKay Sievers  * - Remove symlink from bus' directory.
5751da177e4SLinus Torvalds  * - Delete device from bus's list.
5761da177e4SLinus Torvalds  * - Detach from its driver.
5771da177e4SLinus Torvalds  * - Drop reference taken in bus_add_device().
5781da177e4SLinus Torvalds  */
5791da177e4SLinus Torvalds void bus_remove_device(struct device *dev)
5801da177e4SLinus Torvalds {
581ca22e56dSKay Sievers 	struct bus_type *bus = dev->bus;
582ca22e56dSKay Sievers 	struct subsys_interface *sif;
583ca22e56dSKay Sievers 
584ca22e56dSKay Sievers 	if (!bus)
585ca22e56dSKay Sievers 		return;
586ca22e56dSKay Sievers 
587ca22e56dSKay Sievers 	mutex_lock(&bus->p->mutex);
588ca22e56dSKay Sievers 	list_for_each_entry(sif, &bus->p->interfaces, node)
589ca22e56dSKay Sievers 		if (sif->remove_dev)
590ca22e56dSKay Sievers 			sif->remove_dev(dev, sif);
591ca22e56dSKay Sievers 	mutex_unlock(&bus->p->mutex);
592ca22e56dSKay Sievers 
593b9d9c82bSKay Sievers 	sysfs_remove_link(&dev->kobj, "subsystem");
5944a3ad20cSGreg Kroah-Hartman 	sysfs_remove_link(&dev->bus->p->devices_kset->kobj,
5951e0b2cf9SKay Sievers 			  dev_name(dev));
5961da177e4SLinus Torvalds 	device_remove_attrs(dev->bus, dev);
597fa6fdb33SGreg Kroah-Hartman 	device_remove_groups(dev, dev->bus->dev_groups);
598ae1b4171SGreg Kroah-Hartman 	if (klist_node_attached(&dev->p->knode_bus))
599ae1b4171SGreg Kroah-Hartman 		klist_del(&dev->p->knode_bus);
6003f62e570SGreg Kroah-Hartman 
6014a3ad20cSGreg Kroah-Hartman 	pr_debug("bus: '%s': remove device %s\n",
6021e0b2cf9SKay Sievers 		 dev->bus->name, dev_name(dev));
6031da177e4SLinus Torvalds 	device_release_driver(dev);
604fc1ede58SGreg Kroah-Hartman 	bus_put(dev->bus);
6051da177e4SLinus Torvalds }
6061da177e4SLinus Torvalds 
607f86db396SAndrew Morton static int __must_check add_bind_files(struct device_driver *drv)
608874c6241SGreg Kroah-Hartman {
609f86db396SAndrew Morton 	int ret;
610f86db396SAndrew Morton 
611f86db396SAndrew Morton 	ret = driver_create_file(drv, &driver_attr_unbind);
612f86db396SAndrew Morton 	if (ret == 0) {
613f86db396SAndrew Morton 		ret = driver_create_file(drv, &driver_attr_bind);
614f86db396SAndrew Morton 		if (ret)
615f86db396SAndrew Morton 			driver_remove_file(drv, &driver_attr_unbind);
616f86db396SAndrew Morton 	}
617f86db396SAndrew Morton 	return ret;
618874c6241SGreg Kroah-Hartman }
619874c6241SGreg Kroah-Hartman 
620874c6241SGreg Kroah-Hartman static void remove_bind_files(struct device_driver *drv)
621874c6241SGreg Kroah-Hartman {
622874c6241SGreg Kroah-Hartman 	driver_remove_file(drv, &driver_attr_bind);
623874c6241SGreg Kroah-Hartman 	driver_remove_file(drv, &driver_attr_unbind);
624874c6241SGreg Kroah-Hartman }
625b8c5cec2SKay Sievers 
6268380770cSKay Sievers static BUS_ATTR(drivers_probe, S_IWUSR, NULL, store_drivers_probe);
6278380770cSKay Sievers static BUS_ATTR(drivers_autoprobe, S_IWUSR | S_IRUGO,
6288380770cSKay Sievers 		show_drivers_autoprobe, store_drivers_autoprobe);
6298380770cSKay Sievers 
630b8c5cec2SKay Sievers static int add_probe_files(struct bus_type *bus)
631b8c5cec2SKay Sievers {
632b8c5cec2SKay Sievers 	int retval;
633b8c5cec2SKay Sievers 
6348380770cSKay Sievers 	retval = bus_create_file(bus, &bus_attr_drivers_probe);
635b8c5cec2SKay Sievers 	if (retval)
636b8c5cec2SKay Sievers 		goto out;
637b8c5cec2SKay Sievers 
6388380770cSKay Sievers 	retval = bus_create_file(bus, &bus_attr_drivers_autoprobe);
639b8c5cec2SKay Sievers 	if (retval)
6408380770cSKay Sievers 		bus_remove_file(bus, &bus_attr_drivers_probe);
641b8c5cec2SKay Sievers out:
642b8c5cec2SKay Sievers 	return retval;
643b8c5cec2SKay Sievers }
644b8c5cec2SKay Sievers 
645b8c5cec2SKay Sievers static void remove_probe_files(struct bus_type *bus)
646b8c5cec2SKay Sievers {
6478380770cSKay Sievers 	bus_remove_file(bus, &bus_attr_drivers_autoprobe);
6488380770cSKay Sievers 	bus_remove_file(bus, &bus_attr_drivers_probe);
649b8c5cec2SKay Sievers }
6501da177e4SLinus Torvalds 
6512581c9ccSGreg Kroah-Hartman static ssize_t uevent_store(struct device_driver *drv, const char *buf,
6522581c9ccSGreg Kroah-Hartman 			    size_t count)
6537ac1cf4aSKay Sievers {
6547ac1cf4aSKay Sievers 	enum kobject_action action;
6557ac1cf4aSKay Sievers 
6567ac1cf4aSKay Sievers 	if (kobject_action_type(buf, count, &action) == 0)
657e5dd1278SGreg Kroah-Hartman 		kobject_uevent(&drv->p->kobj, action);
6587ac1cf4aSKay Sievers 	return count;
6597ac1cf4aSKay Sievers }
6602581c9ccSGreg Kroah-Hartman static DRIVER_ATTR_WO(uevent);
6617ac1cf4aSKay Sievers 
6621da177e4SLinus Torvalds /**
6631da177e4SLinus Torvalds  * bus_add_driver - Add a driver to the bus.
6641da177e4SLinus Torvalds  * @drv: driver.
6651da177e4SLinus Torvalds  */
6661da177e4SLinus Torvalds int bus_add_driver(struct device_driver *drv)
6671da177e4SLinus Torvalds {
668e5dd1278SGreg Kroah-Hartman 	struct bus_type *bus;
669e5dd1278SGreg Kroah-Hartman 	struct driver_private *priv;
6701da177e4SLinus Torvalds 	int error = 0;
6711da177e4SLinus Torvalds 
672e5dd1278SGreg Kroah-Hartman 	bus = bus_get(drv->bus);
673d9fd4d3bSJeff Garzik 	if (!bus)
6744f6e1945SGreg Kroah-Hartman 		return -EINVAL;
675d9fd4d3bSJeff Garzik 
6767dc72b28SGreg Kroah-Hartman 	pr_debug("bus: '%s': add driver %s\n", bus->name, drv->name);
677e5dd1278SGreg Kroah-Hartman 
678e5dd1278SGreg Kroah-Hartman 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
67907634464SCornelia Huck 	if (!priv) {
68007634464SCornelia Huck 		error = -ENOMEM;
68107634464SCornelia Huck 		goto out_put_bus;
68207634464SCornelia Huck 	}
683e5dd1278SGreg Kroah-Hartman 	klist_init(&priv->klist_devices, NULL, NULL);
684e5dd1278SGreg Kroah-Hartman 	priv->driver = drv;
685e5dd1278SGreg Kroah-Hartman 	drv->p = priv;
686c8e90d82SGreg Kroah-Hartman 	priv->kobj.kset = bus->p->drivers_kset;
687c8e90d82SGreg Kroah-Hartman 	error = kobject_init_and_add(&priv->kobj, &driver_ktype, NULL,
688c8e90d82SGreg Kroah-Hartman 				     "%s", drv->name);
689dc0afa83SCornelia Huck 	if (error)
69007634464SCornelia Huck 		goto out_unregister;
6911da177e4SLinus Torvalds 
692190888acSMing Lei 	klist_add_tail(&priv->knode_bus, &bus->p->klist_drivers);
693c6f7e72aSGreg Kroah-Hartman 	if (drv->bus->p->drivers_autoprobe) {
694f86db396SAndrew Morton 		error = driver_attach(drv);
695f86db396SAndrew Morton 		if (error)
696f86db396SAndrew Morton 			goto out_unregister;
697b8c5cec2SKay Sievers 	}
6981da177e4SLinus Torvalds 	module_add_driver(drv->owner, drv);
6991da177e4SLinus Torvalds 
7007ac1cf4aSKay Sievers 	error = driver_create_file(drv, &driver_attr_uevent);
7017ac1cf4aSKay Sievers 	if (error) {
7027ac1cf4aSKay Sievers 		printk(KERN_ERR "%s: uevent attr (%s) failed\n",
7032b3a302aSHarvey Harrison 			__func__, drv->name);
7047ac1cf4aSKay Sievers 	}
705e18945b1SGreg Kroah-Hartman 	error = driver_add_groups(drv, bus->drv_groups);
706f86db396SAndrew Morton 	if (error) {
707f86db396SAndrew Morton 		/* How the hell do we get out of this pickle? Give up */
708ed0617b5SGreg Kroah-Hartman 		printk(KERN_ERR "%s: driver_create_groups(%s) failed\n",
709ed0617b5SGreg Kroah-Hartman 			__func__, drv->name);
710e18945b1SGreg Kroah-Hartman 	}
7111a6f2a75SDmitry Torokhov 
7121a6f2a75SDmitry Torokhov 	if (!drv->suppress_bind_attrs) {
713f86db396SAndrew Morton 		error = add_bind_files(drv);
714f86db396SAndrew Morton 		if (error) {
715f86db396SAndrew Morton 			/* Ditto */
716f86db396SAndrew Morton 			printk(KERN_ERR "%s: add_bind_files(%s) failed\n",
7172b3a302aSHarvey Harrison 				__func__, drv->name);
718f86db396SAndrew Morton 		}
7191a6f2a75SDmitry Torokhov 	}
720d9fd4d3bSJeff Garzik 
7215c8563d7SKay Sievers 	return 0;
7221a6f2a75SDmitry Torokhov 
723f86db396SAndrew Morton out_unregister:
72499b28f1bSPhil Carmody 	kobject_put(&priv->kobj);
7255c8563d7SKay Sievers 	kfree(drv->p);
7265c8563d7SKay Sievers 	drv->p = NULL;
727f86db396SAndrew Morton out_put_bus:
728fc1ede58SGreg Kroah-Hartman 	bus_put(bus);
729f86db396SAndrew Morton 	return error;
7301da177e4SLinus Torvalds }
7311da177e4SLinus Torvalds 
7321da177e4SLinus Torvalds /**
7331da177e4SLinus Torvalds  * bus_remove_driver - delete driver from bus's knowledge.
7341da177e4SLinus Torvalds  * @drv: driver.
7351da177e4SLinus Torvalds  *
7361da177e4SLinus Torvalds  * Detach the driver from the devices it controls, and remove
7371da177e4SLinus Torvalds  * it from its bus's list of drivers. Finally, we drop the reference
7381da177e4SLinus Torvalds  * to the bus we took in bus_add_driver().
7391da177e4SLinus Torvalds  */
7401da177e4SLinus Torvalds void bus_remove_driver(struct device_driver *drv)
7411da177e4SLinus Torvalds {
742d9fd4d3bSJeff Garzik 	if (!drv->bus)
743d9fd4d3bSJeff Garzik 		return;
744d9fd4d3bSJeff Garzik 
7451a6f2a75SDmitry Torokhov 	if (!drv->suppress_bind_attrs)
746874c6241SGreg Kroah-Hartman 		remove_bind_files(drv);
747ed0617b5SGreg Kroah-Hartman 	driver_remove_groups(drv, drv->bus->drv_groups);
7487ac1cf4aSKay Sievers 	driver_remove_file(drv, &driver_attr_uevent);
749e5dd1278SGreg Kroah-Hartman 	klist_remove(&drv->p->knode_bus);
7507dc72b28SGreg Kroah-Hartman 	pr_debug("bus: '%s': remove driver %s\n", drv->bus->name, drv->name);
7511da177e4SLinus Torvalds 	driver_detach(drv);
7521da177e4SLinus Torvalds 	module_remove_driver(drv);
753c10997f6SGreg Kroah-Hartman 	kobject_put(&drv->p->kobj);
754fc1ede58SGreg Kroah-Hartman 	bus_put(drv->bus);
7551da177e4SLinus Torvalds }
7561da177e4SLinus Torvalds 
7571da177e4SLinus Torvalds /* Helper for bus_rescan_devices's iter */
758f86db396SAndrew Morton static int __must_check bus_rescan_devices_helper(struct device *dev,
759f86db396SAndrew Morton 						  void *data)
7601da177e4SLinus Torvalds {
761f86db396SAndrew Morton 	int ret = 0;
762f86db396SAndrew Morton 
763bf74ad5bSAlan Stern 	if (!dev->driver) {
764bf74ad5bSAlan Stern 		if (dev->parent)	/* Needed for USB */
7658e9394ceSGreg Kroah-Hartman 			device_lock(dev->parent);
766f86db396SAndrew Morton 		ret = device_attach(dev);
767bf74ad5bSAlan Stern 		if (dev->parent)
7688e9394ceSGreg Kroah-Hartman 			device_unlock(dev->parent);
769bf74ad5bSAlan Stern 	}
770f86db396SAndrew Morton 	return ret < 0 ? ret : 0;
7711da177e4SLinus Torvalds }
7721da177e4SLinus Torvalds 
7731da177e4SLinus Torvalds /**
7741da177e4SLinus Torvalds  * bus_rescan_devices - rescan devices on the bus for possible drivers
7751da177e4SLinus Torvalds  * @bus: the bus to scan.
7761da177e4SLinus Torvalds  *
7771da177e4SLinus Torvalds  * This function will look for devices on the bus with no driver
77823d3d602SGreg Kroah-Hartman  * attached and rescan it against existing drivers to see if it matches
77923d3d602SGreg Kroah-Hartman  * any by calling device_attach() for the unbound devices.
7801da177e4SLinus Torvalds  */
781f86db396SAndrew Morton int bus_rescan_devices(struct bus_type *bus)
7821da177e4SLinus Torvalds {
783f86db396SAndrew Morton 	return bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper);
7841da177e4SLinus Torvalds }
7854a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_rescan_devices);
7861da177e4SLinus Torvalds 
787e935d5daSMoore, Eric /**
788e935d5daSMoore, Eric  * device_reprobe - remove driver for a device and probe for a new driver
789e935d5daSMoore, Eric  * @dev: the device to reprobe
790e935d5daSMoore, Eric  *
791e935d5daSMoore, Eric  * This function detaches the attached driver (if any) for the given
792e935d5daSMoore, Eric  * device and restarts the driver probing process.  It is intended
793e935d5daSMoore, Eric  * to use if probing criteria changed during a devices lifetime and
794e935d5daSMoore, Eric  * driver attachment should change accordingly.
795e935d5daSMoore, Eric  */
796f86db396SAndrew Morton int device_reprobe(struct device *dev)
797e935d5daSMoore, Eric {
798e935d5daSMoore, Eric 	if (dev->driver) {
799e935d5daSMoore, Eric 		if (dev->parent)        /* Needed for USB */
8008e9394ceSGreg Kroah-Hartman 			device_lock(dev->parent);
801e935d5daSMoore, Eric 		device_release_driver(dev);
802e935d5daSMoore, Eric 		if (dev->parent)
8038e9394ceSGreg Kroah-Hartman 			device_unlock(dev->parent);
804e935d5daSMoore, Eric 	}
805f86db396SAndrew Morton 	return bus_rescan_devices_helper(dev, NULL);
806e935d5daSMoore, Eric }
807e935d5daSMoore, Eric EXPORT_SYMBOL_GPL(device_reprobe);
8081da177e4SLinus Torvalds 
8091da177e4SLinus Torvalds /**
8101da177e4SLinus Torvalds  * find_bus - locate bus by name.
8111da177e4SLinus Torvalds  * @name: name of bus.
8121da177e4SLinus Torvalds  *
8131da177e4SLinus Torvalds  * Call kset_find_obj() to iterate over list of buses to
8141da177e4SLinus Torvalds  * find a bus by name. Return bus if found.
8151da177e4SLinus Torvalds  *
8161da177e4SLinus Torvalds  * Note that kset_find_obj increments bus' reference count.
8171da177e4SLinus Torvalds  */
8187e4ef085SAdrian Bunk #if 0
8191da177e4SLinus Torvalds struct bus_type *find_bus(char *name)
8201da177e4SLinus Torvalds {
82159a54833SGreg Kroah-Hartman 	struct kobject *k = kset_find_obj(bus_kset, name);
8221da177e4SLinus Torvalds 	return k ? to_bus(k) : NULL;
8231da177e4SLinus Torvalds }
8247e4ef085SAdrian Bunk #endif  /*  0  */
8251da177e4SLinus Torvalds 
82612478ba0SGreg Kroah-Hartman static int bus_add_groups(struct bus_type *bus,
82712478ba0SGreg Kroah-Hartman 			  const struct attribute_group **groups)
82812478ba0SGreg Kroah-Hartman {
8293e9b2baeSGreg Kroah-Hartman 	return sysfs_create_groups(&bus->p->subsys.kobj, groups);
83012478ba0SGreg Kroah-Hartman }
83112478ba0SGreg Kroah-Hartman 
83212478ba0SGreg Kroah-Hartman static void bus_remove_groups(struct bus_type *bus,
83312478ba0SGreg Kroah-Hartman 			      const struct attribute_group **groups)
83412478ba0SGreg Kroah-Hartman {
8353e9b2baeSGreg Kroah-Hartman 	sysfs_remove_groups(&bus->p->subsys.kobj, groups);
83612478ba0SGreg Kroah-Hartman }
83712478ba0SGreg Kroah-Hartman 
83834bb61f9SJames Bottomley static void klist_devices_get(struct klist_node *n)
83934bb61f9SJames Bottomley {
840ae1b4171SGreg Kroah-Hartman 	struct device_private *dev_prv = to_device_private_bus(n);
841ae1b4171SGreg Kroah-Hartman 	struct device *dev = dev_prv->device;
84234bb61f9SJames Bottomley 
84334bb61f9SJames Bottomley 	get_device(dev);
84434bb61f9SJames Bottomley }
84534bb61f9SJames Bottomley 
84634bb61f9SJames Bottomley static void klist_devices_put(struct klist_node *n)
84734bb61f9SJames Bottomley {
848ae1b4171SGreg Kroah-Hartman 	struct device_private *dev_prv = to_device_private_bus(n);
849ae1b4171SGreg Kroah-Hartman 	struct device *dev = dev_prv->device;
85034bb61f9SJames Bottomley 
85134bb61f9SJames Bottomley 	put_device(dev);
85234bb61f9SJames Bottomley }
85334bb61f9SJames Bottomley 
8547ac1cf4aSKay Sievers static ssize_t bus_uevent_store(struct bus_type *bus,
8557ac1cf4aSKay Sievers 				const char *buf, size_t count)
8567ac1cf4aSKay Sievers {
8577ac1cf4aSKay Sievers 	enum kobject_action action;
8587ac1cf4aSKay Sievers 
8597ac1cf4aSKay Sievers 	if (kobject_action_type(buf, count, &action) == 0)
860c6f7e72aSGreg Kroah-Hartman 		kobject_uevent(&bus->p->subsys.kobj, action);
8617ac1cf4aSKay Sievers 	return count;
8627ac1cf4aSKay Sievers }
8637ac1cf4aSKay Sievers static BUS_ATTR(uevent, S_IWUSR, NULL, bus_uevent_store);
8647ac1cf4aSKay Sievers 
8651da177e4SLinus Torvalds /**
866be871b7eSMichal Hocko  * bus_register - register a driver-core subsystem
86778d79559SRandy Dunlap  * @bus: bus to register
8681da177e4SLinus Torvalds  *
86978d79559SRandy Dunlap  * Once we have that, we register the bus with the kobject
8701da177e4SLinus Torvalds  * infrastructure, then register the children subsystems it has:
871ca22e56dSKay Sievers  * the devices and drivers that belong to the subsystem.
8721da177e4SLinus Torvalds  */
873be871b7eSMichal Hocko int bus_register(struct bus_type *bus)
8741da177e4SLinus Torvalds {
8751da177e4SLinus Torvalds 	int retval;
8766b6e39a6SKay Sievers 	struct subsys_private *priv;
877be871b7eSMichal Hocko 	struct lock_class_key *key = &bus->lock_key;
8781da177e4SLinus Torvalds 
8796b6e39a6SKay Sievers 	priv = kzalloc(sizeof(struct subsys_private), GFP_KERNEL);
880c6f7e72aSGreg Kroah-Hartman 	if (!priv)
881c6f7e72aSGreg Kroah-Hartman 		return -ENOMEM;
882116af378SBenjamin Herrenschmidt 
883c6f7e72aSGreg Kroah-Hartman 	priv->bus = bus;
884c6f7e72aSGreg Kroah-Hartman 	bus->p = priv;
885c6f7e72aSGreg Kroah-Hartman 
886c6f7e72aSGreg Kroah-Hartman 	BLOCKING_INIT_NOTIFIER_HEAD(&priv->bus_notifier);
887c6f7e72aSGreg Kroah-Hartman 
888c6f7e72aSGreg Kroah-Hartman 	retval = kobject_set_name(&priv->subsys.kobj, "%s", bus->name);
8891da177e4SLinus Torvalds 	if (retval)
8901da177e4SLinus Torvalds 		goto out;
8911da177e4SLinus Torvalds 
892c6f7e72aSGreg Kroah-Hartman 	priv->subsys.kobj.kset = bus_kset;
893c6f7e72aSGreg Kroah-Hartman 	priv->subsys.kobj.ktype = &bus_ktype;
894c6f7e72aSGreg Kroah-Hartman 	priv->drivers_autoprobe = 1;
895d6b05b84SGreg Kroah-Hartman 
896c6f7e72aSGreg Kroah-Hartman 	retval = kset_register(&priv->subsys);
8971da177e4SLinus Torvalds 	if (retval)
8981da177e4SLinus Torvalds 		goto out;
8991da177e4SLinus Torvalds 
9007ac1cf4aSKay Sievers 	retval = bus_create_file(bus, &bus_attr_uevent);
9017ac1cf4aSKay Sievers 	if (retval)
9027ac1cf4aSKay Sievers 		goto bus_uevent_fail;
9037ac1cf4aSKay Sievers 
904c6f7e72aSGreg Kroah-Hartman 	priv->devices_kset = kset_create_and_add("devices", NULL,
905c6f7e72aSGreg Kroah-Hartman 						 &priv->subsys.kobj);
906c6f7e72aSGreg Kroah-Hartman 	if (!priv->devices_kset) {
9073d899596SGreg Kroah-Hartman 		retval = -ENOMEM;
9081da177e4SLinus Torvalds 		goto bus_devices_fail;
9093d899596SGreg Kroah-Hartman 	}
9101da177e4SLinus Torvalds 
911c6f7e72aSGreg Kroah-Hartman 	priv->drivers_kset = kset_create_and_add("drivers", NULL,
912c6f7e72aSGreg Kroah-Hartman 						 &priv->subsys.kobj);
913c6f7e72aSGreg Kroah-Hartman 	if (!priv->drivers_kset) {
9146dcec251SGreg Kroah-Hartman 		retval = -ENOMEM;
9151da177e4SLinus Torvalds 		goto bus_drivers_fail;
9166dcec251SGreg Kroah-Hartman 	}
917465c7a3aSmochel@digitalimplant.org 
918ca22e56dSKay Sievers 	INIT_LIST_HEAD(&priv->interfaces);
919ca22e56dSKay Sievers 	__mutex_init(&priv->mutex, "subsys mutex", key);
920c6f7e72aSGreg Kroah-Hartman 	klist_init(&priv->klist_devices, klist_devices_get, klist_devices_put);
921c6f7e72aSGreg Kroah-Hartman 	klist_init(&priv->klist_drivers, NULL, NULL);
922b8c5cec2SKay Sievers 
923b8c5cec2SKay Sievers 	retval = add_probe_files(bus);
924b8c5cec2SKay Sievers 	if (retval)
925b8c5cec2SKay Sievers 		goto bus_probe_files_fail;
926b8c5cec2SKay Sievers 
92712478ba0SGreg Kroah-Hartman 	retval = bus_add_groups(bus, bus->bus_groups);
92812478ba0SGreg Kroah-Hartman 	if (retval)
92912478ba0SGreg Kroah-Hartman 		goto bus_groups_fail;
9301da177e4SLinus Torvalds 
9317dc72b28SGreg Kroah-Hartman 	pr_debug("bus: '%s': registered\n", bus->name);
9321da177e4SLinus Torvalds 	return 0;
9331da177e4SLinus Torvalds 
93412478ba0SGreg Kroah-Hartman bus_groups_fail:
935b8c5cec2SKay Sievers 	remove_probe_files(bus);
936b8c5cec2SKay Sievers bus_probe_files_fail:
937c6f7e72aSGreg Kroah-Hartman 	kset_unregister(bus->p->drivers_kset);
9381da177e4SLinus Torvalds bus_drivers_fail:
939c6f7e72aSGreg Kroah-Hartman 	kset_unregister(bus->p->devices_kset);
9401da177e4SLinus Torvalds bus_devices_fail:
9417ac1cf4aSKay Sievers 	bus_remove_file(bus, &bus_attr_uevent);
9427ac1cf4aSKay Sievers bus_uevent_fail:
943c6f7e72aSGreg Kroah-Hartman 	kset_unregister(&bus->p->subsys);
9441da177e4SLinus Torvalds out:
945600c20f3SJike Song 	kfree(bus->p);
946f48f3febSDave Young 	bus->p = NULL;
9471da177e4SLinus Torvalds 	return retval;
9481da177e4SLinus Torvalds }
949be871b7eSMichal Hocko EXPORT_SYMBOL_GPL(bus_register);
9501da177e4SLinus Torvalds 
9511da177e4SLinus Torvalds /**
9521da177e4SLinus Torvalds  * bus_unregister - remove a bus from the system
9531da177e4SLinus Torvalds  * @bus: bus.
9541da177e4SLinus Torvalds  *
9551da177e4SLinus Torvalds  * Unregister the child subsystems and the bus itself.
956fc1ede58SGreg Kroah-Hartman  * Finally, we call bus_put() to release the refcount
9571da177e4SLinus Torvalds  */
9581da177e4SLinus Torvalds void bus_unregister(struct bus_type *bus)
9591da177e4SLinus Torvalds {
9607dc72b28SGreg Kroah-Hartman 	pr_debug("bus: '%s': unregistering\n", bus->name);
961ca22e56dSKay Sievers 	if (bus->dev_root)
962ca22e56dSKay Sievers 		device_unregister(bus->dev_root);
96312478ba0SGreg Kroah-Hartman 	bus_remove_groups(bus, bus->bus_groups);
964b8c5cec2SKay Sievers 	remove_probe_files(bus);
965c6f7e72aSGreg Kroah-Hartman 	kset_unregister(bus->p->drivers_kset);
966c6f7e72aSGreg Kroah-Hartman 	kset_unregister(bus->p->devices_kset);
9677ac1cf4aSKay Sievers 	bus_remove_file(bus, &bus_attr_uevent);
968c6f7e72aSGreg Kroah-Hartman 	kset_unregister(&bus->p->subsys);
9691da177e4SLinus Torvalds }
9704a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_unregister);
9711da177e4SLinus Torvalds 
972116af378SBenjamin Herrenschmidt int bus_register_notifier(struct bus_type *bus, struct notifier_block *nb)
973116af378SBenjamin Herrenschmidt {
974c6f7e72aSGreg Kroah-Hartman 	return blocking_notifier_chain_register(&bus->p->bus_notifier, nb);
975116af378SBenjamin Herrenschmidt }
976116af378SBenjamin Herrenschmidt EXPORT_SYMBOL_GPL(bus_register_notifier);
977116af378SBenjamin Herrenschmidt 
978116af378SBenjamin Herrenschmidt int bus_unregister_notifier(struct bus_type *bus, struct notifier_block *nb)
979116af378SBenjamin Herrenschmidt {
980c6f7e72aSGreg Kroah-Hartman 	return blocking_notifier_chain_unregister(&bus->p->bus_notifier, nb);
981116af378SBenjamin Herrenschmidt }
982116af378SBenjamin Herrenschmidt EXPORT_SYMBOL_GPL(bus_unregister_notifier);
983116af378SBenjamin Herrenschmidt 
9840fed80f7SGreg Kroah-Hartman struct kset *bus_get_kset(struct bus_type *bus)
9850fed80f7SGreg Kroah-Hartman {
986c6f7e72aSGreg Kroah-Hartman 	return &bus->p->subsys;
9870fed80f7SGreg Kroah-Hartman }
9880fed80f7SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_get_kset);
9890fed80f7SGreg Kroah-Hartman 
990b249072eSGreg Kroah-Hartman struct klist *bus_get_device_klist(struct bus_type *bus)
991b249072eSGreg Kroah-Hartman {
992c6f7e72aSGreg Kroah-Hartman 	return &bus->p->klist_devices;
993b249072eSGreg Kroah-Hartman }
994b249072eSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_get_device_klist);
995b249072eSGreg Kroah-Hartman 
99699178b03SGreg Kroah-Hartman /*
997dca25ebdSRobert P. J. Day  * Yes, this forcibly breaks the klist abstraction temporarily.  It
99899178b03SGreg Kroah-Hartman  * just wants to sort the klist, not change reference counts and
99999178b03SGreg Kroah-Hartman  * take/drop locks rapidly in the process.  It does all this while
100099178b03SGreg Kroah-Hartman  * holding the lock for the list, so objects can't otherwise be
100199178b03SGreg Kroah-Hartman  * added/removed while we're swizzling.
100299178b03SGreg Kroah-Hartman  */
100399178b03SGreg Kroah-Hartman static void device_insertion_sort_klist(struct device *a, struct list_head *list,
100499178b03SGreg Kroah-Hartman 					int (*compare)(const struct device *a,
100599178b03SGreg Kroah-Hartman 							const struct device *b))
100699178b03SGreg Kroah-Hartman {
100799178b03SGreg Kroah-Hartman 	struct list_head *pos;
100899178b03SGreg Kroah-Hartman 	struct klist_node *n;
1009ae1b4171SGreg Kroah-Hartman 	struct device_private *dev_prv;
101099178b03SGreg Kroah-Hartman 	struct device *b;
101199178b03SGreg Kroah-Hartman 
101299178b03SGreg Kroah-Hartman 	list_for_each(pos, list) {
101399178b03SGreg Kroah-Hartman 		n = container_of(pos, struct klist_node, n_node);
1014ae1b4171SGreg Kroah-Hartman 		dev_prv = to_device_private_bus(n);
1015ae1b4171SGreg Kroah-Hartman 		b = dev_prv->device;
101699178b03SGreg Kroah-Hartman 		if (compare(a, b) <= 0) {
1017ae1b4171SGreg Kroah-Hartman 			list_move_tail(&a->p->knode_bus.n_node,
1018ae1b4171SGreg Kroah-Hartman 				       &b->p->knode_bus.n_node);
101999178b03SGreg Kroah-Hartman 			return;
102099178b03SGreg Kroah-Hartman 		}
102199178b03SGreg Kroah-Hartman 	}
1022ae1b4171SGreg Kroah-Hartman 	list_move_tail(&a->p->knode_bus.n_node, list);
102399178b03SGreg Kroah-Hartman }
102499178b03SGreg Kroah-Hartman 
102599178b03SGreg Kroah-Hartman void bus_sort_breadthfirst(struct bus_type *bus,
102699178b03SGreg Kroah-Hartman 			   int (*compare)(const struct device *a,
102799178b03SGreg Kroah-Hartman 					  const struct device *b))
102899178b03SGreg Kroah-Hartman {
102999178b03SGreg Kroah-Hartman 	LIST_HEAD(sorted_devices);
103099178b03SGreg Kroah-Hartman 	struct list_head *pos, *tmp;
103199178b03SGreg Kroah-Hartman 	struct klist_node *n;
1032ae1b4171SGreg Kroah-Hartman 	struct device_private *dev_prv;
103399178b03SGreg Kroah-Hartman 	struct device *dev;
103499178b03SGreg Kroah-Hartman 	struct klist *device_klist;
103599178b03SGreg Kroah-Hartman 
103699178b03SGreg Kroah-Hartman 	device_klist = bus_get_device_klist(bus);
103799178b03SGreg Kroah-Hartman 
103899178b03SGreg Kroah-Hartman 	spin_lock(&device_klist->k_lock);
103999178b03SGreg Kroah-Hartman 	list_for_each_safe(pos, tmp, &device_klist->k_list) {
104099178b03SGreg Kroah-Hartman 		n = container_of(pos, struct klist_node, n_node);
1041ae1b4171SGreg Kroah-Hartman 		dev_prv = to_device_private_bus(n);
1042ae1b4171SGreg Kroah-Hartman 		dev = dev_prv->device;
104399178b03SGreg Kroah-Hartman 		device_insertion_sort_klist(dev, &sorted_devices, compare);
104499178b03SGreg Kroah-Hartman 	}
104599178b03SGreg Kroah-Hartman 	list_splice(&sorted_devices, &device_klist->k_list);
104699178b03SGreg Kroah-Hartman 	spin_unlock(&device_klist->k_lock);
104799178b03SGreg Kroah-Hartman }
104899178b03SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_sort_breadthfirst);
104999178b03SGreg Kroah-Hartman 
1050ca22e56dSKay Sievers /**
1051ca22e56dSKay Sievers  * subsys_dev_iter_init - initialize subsys device iterator
1052ca22e56dSKay Sievers  * @iter: subsys iterator to initialize
1053ca22e56dSKay Sievers  * @subsys: the subsys we wanna iterate over
1054ca22e56dSKay Sievers  * @start: the device to start iterating from, if any
1055ca22e56dSKay Sievers  * @type: device_type of the devices to iterate over, NULL for all
1056ca22e56dSKay Sievers  *
1057ca22e56dSKay Sievers  * Initialize subsys iterator @iter such that it iterates over devices
1058ca22e56dSKay Sievers  * of @subsys.  If @start is set, the list iteration will start there,
1059ca22e56dSKay Sievers  * otherwise if it is NULL, the iteration starts at the beginning of
1060ca22e56dSKay Sievers  * the list.
1061ca22e56dSKay Sievers  */
10627cd9c9bbSGreg Kroah-Hartman void subsys_dev_iter_init(struct subsys_dev_iter *iter, struct bus_type *subsys,
1063ca22e56dSKay Sievers 			  struct device *start, const struct device_type *type)
1064ca22e56dSKay Sievers {
1065ca22e56dSKay Sievers 	struct klist_node *start_knode = NULL;
1066ca22e56dSKay Sievers 
1067ca22e56dSKay Sievers 	if (start)
1068ca22e56dSKay Sievers 		start_knode = &start->p->knode_bus;
10697cd9c9bbSGreg Kroah-Hartman 	klist_iter_init_node(&subsys->p->klist_devices, &iter->ki, start_knode);
1070ca22e56dSKay Sievers 	iter->type = type;
1071ca22e56dSKay Sievers }
1072ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_dev_iter_init);
1073ca22e56dSKay Sievers 
1074ca22e56dSKay Sievers /**
1075ca22e56dSKay Sievers  * subsys_dev_iter_next - iterate to the next device
1076ca22e56dSKay Sievers  * @iter: subsys iterator to proceed
1077ca22e56dSKay Sievers  *
1078ca22e56dSKay Sievers  * Proceed @iter to the next device and return it.  Returns NULL if
1079ca22e56dSKay Sievers  * iteration is complete.
1080ca22e56dSKay Sievers  *
1081ca22e56dSKay Sievers  * The returned device is referenced and won't be released till
1082ca22e56dSKay Sievers  * iterator is proceed to the next device or exited.  The caller is
1083ca22e56dSKay Sievers  * free to do whatever it wants to do with the device including
1084ca22e56dSKay Sievers  * calling back into subsys code.
1085ca22e56dSKay Sievers  */
1086ca22e56dSKay Sievers struct device *subsys_dev_iter_next(struct subsys_dev_iter *iter)
1087ca22e56dSKay Sievers {
1088ca22e56dSKay Sievers 	struct klist_node *knode;
1089ca22e56dSKay Sievers 	struct device *dev;
1090ca22e56dSKay Sievers 
1091ca22e56dSKay Sievers 	for (;;) {
1092ca22e56dSKay Sievers 		knode = klist_next(&iter->ki);
1093ca22e56dSKay Sievers 		if (!knode)
1094ca22e56dSKay Sievers 			return NULL;
1095ca22e56dSKay Sievers 		dev = container_of(knode, struct device_private, knode_bus)->device;
1096ca22e56dSKay Sievers 		if (!iter->type || iter->type == dev->type)
1097ca22e56dSKay Sievers 			return dev;
1098ca22e56dSKay Sievers 	}
1099ca22e56dSKay Sievers }
1100ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_dev_iter_next);
1101ca22e56dSKay Sievers 
1102ca22e56dSKay Sievers /**
1103ca22e56dSKay Sievers  * subsys_dev_iter_exit - finish iteration
1104ca22e56dSKay Sievers  * @iter: subsys iterator to finish
1105ca22e56dSKay Sievers  *
1106ca22e56dSKay Sievers  * Finish an iteration.  Always call this function after iteration is
1107ca22e56dSKay Sievers  * complete whether the iteration ran till the end or not.
1108ca22e56dSKay Sievers  */
1109ca22e56dSKay Sievers void subsys_dev_iter_exit(struct subsys_dev_iter *iter)
1110ca22e56dSKay Sievers {
1111ca22e56dSKay Sievers 	klist_iter_exit(&iter->ki);
1112ca22e56dSKay Sievers }
1113ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_dev_iter_exit);
1114ca22e56dSKay Sievers 
1115ca22e56dSKay Sievers int subsys_interface_register(struct subsys_interface *sif)
1116ca22e56dSKay Sievers {
1117ca22e56dSKay Sievers 	struct bus_type *subsys;
1118ca22e56dSKay Sievers 	struct subsys_dev_iter iter;
1119ca22e56dSKay Sievers 	struct device *dev;
1120ca22e56dSKay Sievers 
1121ca22e56dSKay Sievers 	if (!sif || !sif->subsys)
1122ca22e56dSKay Sievers 		return -ENODEV;
1123ca22e56dSKay Sievers 
1124ca22e56dSKay Sievers 	subsys = bus_get(sif->subsys);
1125ca22e56dSKay Sievers 	if (!subsys)
1126ca22e56dSKay Sievers 		return -EINVAL;
1127ca22e56dSKay Sievers 
1128ca22e56dSKay Sievers 	mutex_lock(&subsys->p->mutex);
1129ca22e56dSKay Sievers 	list_add_tail(&sif->node, &subsys->p->interfaces);
1130ca22e56dSKay Sievers 	if (sif->add_dev) {
1131ca22e56dSKay Sievers 		subsys_dev_iter_init(&iter, subsys, NULL, NULL);
1132ca22e56dSKay Sievers 		while ((dev = subsys_dev_iter_next(&iter)))
1133ca22e56dSKay Sievers 			sif->add_dev(dev, sif);
1134ca22e56dSKay Sievers 		subsys_dev_iter_exit(&iter);
1135ca22e56dSKay Sievers 	}
1136ca22e56dSKay Sievers 	mutex_unlock(&subsys->p->mutex);
1137ca22e56dSKay Sievers 
1138ca22e56dSKay Sievers 	return 0;
1139ca22e56dSKay Sievers }
1140ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_interface_register);
1141ca22e56dSKay Sievers 
1142ca22e56dSKay Sievers void subsys_interface_unregister(struct subsys_interface *sif)
1143ca22e56dSKay Sievers {
11442b31594aSJonghwan Choi 	struct bus_type *subsys;
1145ca22e56dSKay Sievers 	struct subsys_dev_iter iter;
1146ca22e56dSKay Sievers 	struct device *dev;
1147ca22e56dSKay Sievers 
11482b31594aSJonghwan Choi 	if (!sif || !sif->subsys)
1149ca22e56dSKay Sievers 		return;
1150ca22e56dSKay Sievers 
11512b31594aSJonghwan Choi 	subsys = sif->subsys;
11522b31594aSJonghwan Choi 
1153ca22e56dSKay Sievers 	mutex_lock(&subsys->p->mutex);
1154ca22e56dSKay Sievers 	list_del_init(&sif->node);
1155ca22e56dSKay Sievers 	if (sif->remove_dev) {
1156ca22e56dSKay Sievers 		subsys_dev_iter_init(&iter, subsys, NULL, NULL);
1157ca22e56dSKay Sievers 		while ((dev = subsys_dev_iter_next(&iter)))
1158ca22e56dSKay Sievers 			sif->remove_dev(dev, sif);
1159ca22e56dSKay Sievers 		subsys_dev_iter_exit(&iter);
1160ca22e56dSKay Sievers 	}
1161ca22e56dSKay Sievers 	mutex_unlock(&subsys->p->mutex);
1162ca22e56dSKay Sievers 
1163ca22e56dSKay Sievers 	bus_put(subsys);
1164ca22e56dSKay Sievers }
1165ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_interface_unregister);
1166ca22e56dSKay Sievers 
1167ca22e56dSKay Sievers static void system_root_device_release(struct device *dev)
1168ca22e56dSKay Sievers {
1169ca22e56dSKay Sievers 	kfree(dev);
1170ca22e56dSKay Sievers }
1171d73ce004STejun Heo 
1172d73ce004STejun Heo static int subsys_register(struct bus_type *subsys,
1173d73ce004STejun Heo 			   const struct attribute_group **groups,
1174d73ce004STejun Heo 			   struct kobject *parent_of_root)
1175d73ce004STejun Heo {
1176d73ce004STejun Heo 	struct device *dev;
1177d73ce004STejun Heo 	int err;
1178d73ce004STejun Heo 
1179d73ce004STejun Heo 	err = bus_register(subsys);
1180d73ce004STejun Heo 	if (err < 0)
1181d73ce004STejun Heo 		return err;
1182d73ce004STejun Heo 
1183d73ce004STejun Heo 	dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1184d73ce004STejun Heo 	if (!dev) {
1185d73ce004STejun Heo 		err = -ENOMEM;
1186d73ce004STejun Heo 		goto err_dev;
1187d73ce004STejun Heo 	}
1188d73ce004STejun Heo 
1189d73ce004STejun Heo 	err = dev_set_name(dev, "%s", subsys->name);
1190d73ce004STejun Heo 	if (err < 0)
1191d73ce004STejun Heo 		goto err_name;
1192d73ce004STejun Heo 
1193d73ce004STejun Heo 	dev->kobj.parent = parent_of_root;
1194d73ce004STejun Heo 	dev->groups = groups;
1195d73ce004STejun Heo 	dev->release = system_root_device_release;
1196d73ce004STejun Heo 
1197d73ce004STejun Heo 	err = device_register(dev);
1198d73ce004STejun Heo 	if (err < 0)
1199d73ce004STejun Heo 		goto err_dev_reg;
1200d73ce004STejun Heo 
1201d73ce004STejun Heo 	subsys->dev_root = dev;
1202d73ce004STejun Heo 	return 0;
1203d73ce004STejun Heo 
1204d73ce004STejun Heo err_dev_reg:
1205d73ce004STejun Heo 	put_device(dev);
1206d73ce004STejun Heo 	dev = NULL;
1207d73ce004STejun Heo err_name:
1208d73ce004STejun Heo 	kfree(dev);
1209d73ce004STejun Heo err_dev:
1210d73ce004STejun Heo 	bus_unregister(subsys);
1211d73ce004STejun Heo 	return err;
1212d73ce004STejun Heo }
1213d73ce004STejun Heo 
1214ca22e56dSKay Sievers /**
1215ca22e56dSKay Sievers  * subsys_system_register - register a subsystem at /sys/devices/system/
121678d79559SRandy Dunlap  * @subsys: system subsystem
121778d79559SRandy Dunlap  * @groups: default attributes for the root device
1218ca22e56dSKay Sievers  *
1219ca22e56dSKay Sievers  * All 'system' subsystems have a /sys/devices/system/<name> root device
1220ca22e56dSKay Sievers  * with the name of the subsystem. The root device can carry subsystem-
1221ca22e56dSKay Sievers  * wide attributes. All registered devices are below this single root
1222ca22e56dSKay Sievers  * device and are named after the subsystem with a simple enumeration
1223e227867fSMasanari Iida  * number appended. The registered devices are not explicitly named;
1224ca22e56dSKay Sievers  * only 'id' in the device needs to be set.
1225ca22e56dSKay Sievers  *
1226ca22e56dSKay Sievers  * Do not use this interface for anything new, it exists for compatibility
1227ca22e56dSKay Sievers  * with bad ideas only. New subsystems should use plain subsystems; and
1228ca22e56dSKay Sievers  * add the subsystem-wide attributes should be added to the subsystem
1229ca22e56dSKay Sievers  * directory itself and not some create fake root-device placed in
1230ca22e56dSKay Sievers  * /sys/devices/system/<name>.
1231ca22e56dSKay Sievers  */
1232ca22e56dSKay Sievers int subsys_system_register(struct bus_type *subsys,
1233ca22e56dSKay Sievers 			   const struct attribute_group **groups)
1234ca22e56dSKay Sievers {
1235d73ce004STejun Heo 	return subsys_register(subsys, groups, &system_kset->kobj);
1236ca22e56dSKay Sievers }
1237ca22e56dSKay Sievers EXPORT_SYMBOL_GPL(subsys_system_register);
1238ca22e56dSKay Sievers 
1239d73ce004STejun Heo /**
1240d73ce004STejun Heo  * subsys_virtual_register - register a subsystem at /sys/devices/virtual/
1241d73ce004STejun Heo  * @subsys: virtual subsystem
1242d73ce004STejun Heo  * @groups: default attributes for the root device
1243d73ce004STejun Heo  *
1244d73ce004STejun Heo  * All 'virtual' subsystems have a /sys/devices/system/<name> root device
1245d73ce004STejun Heo  * with the name of the subystem.  The root device can carry subsystem-wide
1246d73ce004STejun Heo  * attributes.  All registered devices are below this single root device.
1247d73ce004STejun Heo  * There's no restriction on device naming.  This is for kernel software
1248d73ce004STejun Heo  * constructs which need sysfs interface.
1249d73ce004STejun Heo  */
1250d73ce004STejun Heo int subsys_virtual_register(struct bus_type *subsys,
1251d73ce004STejun Heo 			    const struct attribute_group **groups)
1252d73ce004STejun Heo {
1253d73ce004STejun Heo 	struct kobject *virtual_dir;
1254d73ce004STejun Heo 
1255d73ce004STejun Heo 	virtual_dir = virtual_device_parent(NULL);
1256d73ce004STejun Heo 	if (!virtual_dir)
1257d73ce004STejun Heo 		return -ENOMEM;
1258d73ce004STejun Heo 
1259d73ce004STejun Heo 	return subsys_register(subsys, groups, virtual_dir);
1260d73ce004STejun Heo }
12611c04fc35SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(subsys_virtual_register);
1262d73ce004STejun Heo 
12631da177e4SLinus Torvalds int __init buses_init(void)
12641da177e4SLinus Torvalds {
126559a54833SGreg Kroah-Hartman 	bus_kset = kset_create_and_add("bus", &bus_uevent_ops, NULL);
126659a54833SGreg Kroah-Hartman 	if (!bus_kset)
126759a54833SGreg Kroah-Hartman 		return -ENOMEM;
1268ca22e56dSKay Sievers 
1269ca22e56dSKay Sievers 	system_kset = kset_create_and_add("system", NULL, &devices_kset->kobj);
1270ca22e56dSKay Sievers 	if (!system_kset)
1271ca22e56dSKay Sievers 		return -ENOMEM;
1272ca22e56dSKay Sievers 
127359a54833SGreg Kroah-Hartman 	return 0;
12741da177e4SLinus Torvalds }
1275