xref: /linux/drivers/base/bus.c (revision 8380770c842faef3001e44662953d64ad9a93663)
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
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * This file is released under the GPLv2
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds #include <linux/device.h>
121da177e4SLinus Torvalds #include <linux/module.h>
131da177e4SLinus Torvalds #include <linux/errno.h>
141da177e4SLinus Torvalds #include <linux/init.h>
151da177e4SLinus Torvalds #include <linux/string.h>
161da177e4SLinus Torvalds #include "base.h"
171da177e4SLinus Torvalds #include "power/power.h"
181da177e4SLinus Torvalds 
191da177e4SLinus Torvalds #define to_bus_attr(_attr) container_of(_attr, struct bus_attribute, attr)
20823bccfcSGreg Kroah-Hartman #define to_bus(obj) container_of(obj, struct bus_type, subsys.kobj)
211da177e4SLinus Torvalds 
221da177e4SLinus Torvalds /*
231da177e4SLinus Torvalds  * sysfs bindings for drivers
241da177e4SLinus Torvalds  */
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds #define to_drv_attr(_attr) container_of(_attr, struct driver_attribute, attr)
271da177e4SLinus Torvalds #define to_driver(obj) container_of(obj, struct device_driver, kobj)
281da177e4SLinus Torvalds 
291da177e4SLinus Torvalds 
30b8c5cec2SKay Sievers static int __must_check bus_rescan_devices_helper(struct device *dev,
31b8c5cec2SKay Sievers 						void *data);
32b8c5cec2SKay Sievers 
331da177e4SLinus Torvalds static ssize_t
341da177e4SLinus Torvalds drv_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
351da177e4SLinus Torvalds {
361da177e4SLinus Torvalds 	struct driver_attribute * drv_attr = to_drv_attr(attr);
371da177e4SLinus Torvalds 	struct device_driver * drv = to_driver(kobj);
384a0c20bfSDmitry Torokhov 	ssize_t ret = -EIO;
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds 	if (drv_attr->show)
411da177e4SLinus Torvalds 		ret = drv_attr->show(drv, buf);
421da177e4SLinus Torvalds 	return ret;
431da177e4SLinus Torvalds }
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds static ssize_t
461da177e4SLinus Torvalds drv_attr_store(struct kobject * kobj, struct attribute * attr,
471da177e4SLinus Torvalds 	       const char * buf, size_t count)
481da177e4SLinus Torvalds {
491da177e4SLinus Torvalds 	struct driver_attribute * drv_attr = to_drv_attr(attr);
501da177e4SLinus Torvalds 	struct device_driver * drv = to_driver(kobj);
514a0c20bfSDmitry Torokhov 	ssize_t ret = -EIO;
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds 	if (drv_attr->store)
541da177e4SLinus Torvalds 		ret = drv_attr->store(drv, buf, count);
551da177e4SLinus Torvalds 	return ret;
561da177e4SLinus Torvalds }
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds static struct sysfs_ops driver_sysfs_ops = {
591da177e4SLinus Torvalds 	.show	= drv_attr_show,
601da177e4SLinus Torvalds 	.store	= drv_attr_store,
611da177e4SLinus Torvalds };
621da177e4SLinus Torvalds 
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds static void driver_release(struct kobject * kobj)
651da177e4SLinus Torvalds {
6674e9f5faSGreg Kroah-Hartman 	/*
6774e9f5faSGreg Kroah-Hartman 	 * Yes this is an empty release function, it is this way because struct
6874e9f5faSGreg Kroah-Hartman 	 * device is always a static object, not a dynamic one.  Yes, this is
6974e9f5faSGreg Kroah-Hartman 	 * not nice and bad, but remember, drivers are code, reference counted
7074e9f5faSGreg Kroah-Hartman 	 * by the module count, not a device, which is really data.  And yes,
7174e9f5faSGreg Kroah-Hartman 	 * in the future I do want to have all drivers be created dynamically,
7274e9f5faSGreg Kroah-Hartman 	 * and am working toward that goal, but it will take a bit longer...
7374e9f5faSGreg Kroah-Hartman 	 *
7474e9f5faSGreg Kroah-Hartman 	 * But do not let this example give _anyone_ the idea that they can
7574e9f5faSGreg Kroah-Hartman 	 * create a release function without any code in it at all, to do that
7674e9f5faSGreg Kroah-Hartman 	 * is almost always wrong.  If you have any questions about this,
7774e9f5faSGreg Kroah-Hartman 	 * please send an email to <greg@kroah.com>
7874e9f5faSGreg Kroah-Hartman 	 */
791da177e4SLinus Torvalds }
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds static struct kobj_type ktype_driver = {
821da177e4SLinus Torvalds 	.sysfs_ops	= &driver_sysfs_ops,
831da177e4SLinus Torvalds 	.release	= driver_release,
841da177e4SLinus Torvalds };
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds /*
881da177e4SLinus Torvalds  * sysfs bindings for buses
891da177e4SLinus Torvalds  */
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds static ssize_t
931da177e4SLinus Torvalds bus_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
941da177e4SLinus Torvalds {
951da177e4SLinus Torvalds 	struct bus_attribute * bus_attr = to_bus_attr(attr);
961da177e4SLinus Torvalds 	struct bus_type * bus = to_bus(kobj);
971da177e4SLinus Torvalds 	ssize_t ret = 0;
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds 	if (bus_attr->show)
1001da177e4SLinus Torvalds 		ret = bus_attr->show(bus, buf);
1011da177e4SLinus Torvalds 	return ret;
1021da177e4SLinus Torvalds }
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds static ssize_t
1051da177e4SLinus Torvalds bus_attr_store(struct kobject * kobj, struct attribute * attr,
1061da177e4SLinus Torvalds 	       const char * buf, size_t count)
1071da177e4SLinus Torvalds {
1081da177e4SLinus Torvalds 	struct bus_attribute * bus_attr = to_bus_attr(attr);
1091da177e4SLinus Torvalds 	struct bus_type * bus = to_bus(kobj);
1101da177e4SLinus Torvalds 	ssize_t ret = 0;
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds 	if (bus_attr->store)
1131da177e4SLinus Torvalds 		ret = bus_attr->store(bus, buf, count);
1141da177e4SLinus Torvalds 	return ret;
1151da177e4SLinus Torvalds }
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds static struct sysfs_ops bus_sysfs_ops = {
1181da177e4SLinus Torvalds 	.show	= bus_attr_show,
1191da177e4SLinus Torvalds 	.store	= bus_attr_store,
1201da177e4SLinus Torvalds };
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds int bus_create_file(struct bus_type * bus, struct bus_attribute * attr)
1231da177e4SLinus Torvalds {
1241da177e4SLinus Torvalds 	int error;
1251da177e4SLinus Torvalds 	if (get_bus(bus)) {
126823bccfcSGreg Kroah-Hartman 		error = sysfs_create_file(&bus->subsys.kobj, &attr->attr);
1271da177e4SLinus Torvalds 		put_bus(bus);
1281da177e4SLinus Torvalds 	} else
1291da177e4SLinus Torvalds 		error = -EINVAL;
1301da177e4SLinus Torvalds 	return error;
1311da177e4SLinus Torvalds }
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds void bus_remove_file(struct bus_type * bus, struct bus_attribute * attr)
1341da177e4SLinus Torvalds {
1351da177e4SLinus Torvalds 	if (get_bus(bus)) {
136823bccfcSGreg Kroah-Hartman 		sysfs_remove_file(&bus->subsys.kobj, &attr->attr);
1371da177e4SLinus Torvalds 		put_bus(bus);
1381da177e4SLinus Torvalds 	}
1391da177e4SLinus Torvalds }
1401da177e4SLinus Torvalds 
14180f03e34SKay Sievers static struct kobj_type bus_ktype = {
1421da177e4SLinus Torvalds 	.sysfs_ops	= &bus_sysfs_ops,
1431da177e4SLinus Torvalds };
1441da177e4SLinus Torvalds 
14580f03e34SKay Sievers static int bus_uevent_filter(struct kset *kset, struct kobject *kobj)
14680f03e34SKay Sievers {
14780f03e34SKay Sievers 	struct kobj_type *ktype = get_ktype(kobj);
14880f03e34SKay Sievers 
14980f03e34SKay Sievers 	if (ktype == &bus_ktype)
15080f03e34SKay Sievers 		return 1;
15180f03e34SKay Sievers 	return 0;
15280f03e34SKay Sievers }
15380f03e34SKay Sievers 
15480f03e34SKay Sievers static struct kset_uevent_ops bus_uevent_ops = {
15580f03e34SKay Sievers 	.filter = bus_uevent_filter,
15680f03e34SKay Sievers };
15780f03e34SKay Sievers 
15880f03e34SKay Sievers static decl_subsys(bus, &bus_ktype, &bus_uevent_ops);
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds 
1612139bdd5SRussell King #ifdef CONFIG_HOTPLUG
1622b08c8d0SAlan Stern /* Manually detach a device from its associated driver. */
163151ef38fSGreg Kroah-Hartman static int driver_helper(struct device *dev, void *data)
164151ef38fSGreg Kroah-Hartman {
165151ef38fSGreg Kroah-Hartman 	const char *name = data;
166151ef38fSGreg Kroah-Hartman 
167151ef38fSGreg Kroah-Hartman 	if (strcmp(name, dev->bus_id) == 0)
168151ef38fSGreg Kroah-Hartman 		return 1;
169151ef38fSGreg Kroah-Hartman 	return 0;
170151ef38fSGreg Kroah-Hartman }
171151ef38fSGreg Kroah-Hartman 
172151ef38fSGreg Kroah-Hartman static ssize_t driver_unbind(struct device_driver *drv,
173151ef38fSGreg Kroah-Hartman 			     const char *buf, size_t count)
174151ef38fSGreg Kroah-Hartman {
175151ef38fSGreg Kroah-Hartman 	struct bus_type *bus = get_bus(drv->bus);
176151ef38fSGreg Kroah-Hartman 	struct device *dev;
177151ef38fSGreg Kroah-Hartman 	int err = -ENODEV;
178151ef38fSGreg Kroah-Hartman 
179151ef38fSGreg Kroah-Hartman 	dev = bus_find_device(bus, NULL, (void *)buf, driver_helper);
1802b08c8d0SAlan Stern 	if (dev && dev->driver == drv) {
181bf74ad5bSAlan Stern 		if (dev->parent)	/* Needed for USB */
182bf74ad5bSAlan Stern 			down(&dev->parent->sem);
183151ef38fSGreg Kroah-Hartman 		device_release_driver(dev);
184bf74ad5bSAlan Stern 		if (dev->parent)
185bf74ad5bSAlan Stern 			up(&dev->parent->sem);
186151ef38fSGreg Kroah-Hartman 		err = count;
187151ef38fSGreg Kroah-Hartman 	}
1882b08c8d0SAlan Stern 	put_device(dev);
1892b08c8d0SAlan Stern 	put_bus(bus);
190151ef38fSGreg Kroah-Hartman 	return err;
191151ef38fSGreg Kroah-Hartman }
192151ef38fSGreg Kroah-Hartman static DRIVER_ATTR(unbind, S_IWUSR, NULL, driver_unbind);
193151ef38fSGreg Kroah-Hartman 
194afdce75fSGreg Kroah-Hartman /*
195afdce75fSGreg Kroah-Hartman  * Manually attach a device to a driver.
196afdce75fSGreg Kroah-Hartman  * Note: the driver must want to bind to the device,
197afdce75fSGreg Kroah-Hartman  * it is not possible to override the driver's id table.
198afdce75fSGreg Kroah-Hartman  */
199afdce75fSGreg Kroah-Hartman static ssize_t driver_bind(struct device_driver *drv,
200afdce75fSGreg Kroah-Hartman 			   const char *buf, size_t count)
201afdce75fSGreg Kroah-Hartman {
202afdce75fSGreg Kroah-Hartman 	struct bus_type *bus = get_bus(drv->bus);
203afdce75fSGreg Kroah-Hartman 	struct device *dev;
204afdce75fSGreg Kroah-Hartman 	int err = -ENODEV;
205afdce75fSGreg Kroah-Hartman 
206afdce75fSGreg Kroah-Hartman 	dev = bus_find_device(bus, NULL, (void *)buf, driver_helper);
2072b08c8d0SAlan Stern 	if (dev && dev->driver == NULL) {
208bf74ad5bSAlan Stern 		if (dev->parent)	/* Needed for USB */
209bf74ad5bSAlan Stern 			down(&dev->parent->sem);
210afdce75fSGreg Kroah-Hartman 		down(&dev->sem);
211afdce75fSGreg Kroah-Hartman 		err = driver_probe_device(drv, dev);
212afdce75fSGreg Kroah-Hartman 		up(&dev->sem);
213bf74ad5bSAlan Stern 		if (dev->parent)
214bf74ad5bSAlan Stern 			up(&dev->parent->sem);
21537225401SRyan Wilson 
21637225401SRyan Wilson 		if (err > 0) 		/* success */
21737225401SRyan Wilson 			err = count;
21837225401SRyan Wilson 		else if (err == 0)	/* driver didn't accept device */
21937225401SRyan Wilson 			err = -ENODEV;
220afdce75fSGreg Kroah-Hartman 	}
2212b08c8d0SAlan Stern 	put_device(dev);
2222b08c8d0SAlan Stern 	put_bus(bus);
223afdce75fSGreg Kroah-Hartman 	return err;
224afdce75fSGreg Kroah-Hartman }
225afdce75fSGreg Kroah-Hartman static DRIVER_ATTR(bind, S_IWUSR, NULL, driver_bind);
226afdce75fSGreg Kroah-Hartman 
227b8c5cec2SKay Sievers static ssize_t show_drivers_autoprobe(struct bus_type *bus, char *buf)
228b8c5cec2SKay Sievers {
229b8c5cec2SKay Sievers 	return sprintf(buf, "%d\n", bus->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')
236b8c5cec2SKay Sievers 		bus->drivers_autoprobe = 0;
237b8c5cec2SKay Sievers 	else
238b8c5cec2SKay Sievers 		bus->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 
247b8c5cec2SKay Sievers 	dev = bus_find_device(bus, NULL, (void *)buf, driver_helper);
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 }
2542139bdd5SRussell King #endif
255151ef38fSGreg Kroah-Hartman 
256465c7a3aSmochel@digitalimplant.org static struct device * next_device(struct klist_iter * i)
257465c7a3aSmochel@digitalimplant.org {
258465c7a3aSmochel@digitalimplant.org 	struct klist_node * n = klist_next(i);
259465c7a3aSmochel@digitalimplant.org 	return n ? container_of(n, struct device, knode_bus) : NULL;
260465c7a3aSmochel@digitalimplant.org }
261465c7a3aSmochel@digitalimplant.org 
2621da177e4SLinus Torvalds /**
2631da177e4SLinus Torvalds  *	bus_for_each_dev - device iterator.
2641da177e4SLinus Torvalds  *	@bus:	bus type.
2651da177e4SLinus Torvalds  *	@start:	device to start iterating from.
2661da177e4SLinus Torvalds  *	@data:	data for the callback.
2671da177e4SLinus Torvalds  *	@fn:	function to be called for each device.
2681da177e4SLinus Torvalds  *
2691da177e4SLinus Torvalds  *	Iterate over @bus's list of devices, and call @fn for each,
2701da177e4SLinus Torvalds  *	passing it @data. If @start is not NULL, we use that device to
2711da177e4SLinus Torvalds  *	begin iterating from.
2721da177e4SLinus Torvalds  *
2731da177e4SLinus Torvalds  *	We check the return of @fn each time. If it returns anything
2741da177e4SLinus Torvalds  *	other than 0, we break out and return that value.
2751da177e4SLinus Torvalds  *
2761da177e4SLinus Torvalds  *	NOTE: The device that returns a non-zero value is not retained
2771da177e4SLinus Torvalds  *	in any way, nor is its refcount incremented. If the caller needs
2781da177e4SLinus Torvalds  *	to retain this data, it should do, and increment the reference
2791da177e4SLinus Torvalds  *	count in the supplied callback.
2801da177e4SLinus Torvalds  */
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds int bus_for_each_dev(struct bus_type * bus, struct device * start,
2831da177e4SLinus Torvalds 		     void * data, int (*fn)(struct device *, void *))
2841da177e4SLinus Torvalds {
285465c7a3aSmochel@digitalimplant.org 	struct klist_iter i;
286465c7a3aSmochel@digitalimplant.org 	struct device * dev;
287465c7a3aSmochel@digitalimplant.org 	int error = 0;
2881da177e4SLinus Torvalds 
289465c7a3aSmochel@digitalimplant.org 	if (!bus)
290465c7a3aSmochel@digitalimplant.org 		return -EINVAL;
291465c7a3aSmochel@digitalimplant.org 
292465c7a3aSmochel@digitalimplant.org 	klist_iter_init_node(&bus->klist_devices, &i,
293465c7a3aSmochel@digitalimplant.org 			     (start ? &start->knode_bus : NULL));
294465c7a3aSmochel@digitalimplant.org 	while ((dev = next_device(&i)) && !error)
295465c7a3aSmochel@digitalimplant.org 		error = fn(dev, data);
296465c7a3aSmochel@digitalimplant.org 	klist_iter_exit(&i);
297465c7a3aSmochel@digitalimplant.org 	return error;
2981da177e4SLinus Torvalds }
2991da177e4SLinus Torvalds 
3000edb5860SCornelia Huck /**
3010edb5860SCornelia Huck  * bus_find_device - device iterator for locating a particular device.
3020edb5860SCornelia Huck  * @bus: bus type
3030edb5860SCornelia Huck  * @start: Device to begin with
3040edb5860SCornelia Huck  * @data: Data to pass to match function
3050edb5860SCornelia Huck  * @match: Callback function to check device
3060edb5860SCornelia Huck  *
3070edb5860SCornelia Huck  * This is similar to the bus_for_each_dev() function above, but it
3080edb5860SCornelia Huck  * returns a reference to a device that is 'found' for later use, as
3090edb5860SCornelia Huck  * determined by the @match callback.
3100edb5860SCornelia Huck  *
3110edb5860SCornelia Huck  * The callback should return 0 if the device doesn't match and non-zero
3120edb5860SCornelia Huck  * if it does.  If the callback returns non-zero, this function will
3130edb5860SCornelia Huck  * return to the caller and not iterate over any more devices.
3140edb5860SCornelia Huck  */
3150edb5860SCornelia Huck struct device * bus_find_device(struct bus_type *bus,
3160edb5860SCornelia Huck 				struct device *start, void *data,
3170edb5860SCornelia Huck 				int (*match)(struct device *, void *))
3180edb5860SCornelia Huck {
3190edb5860SCornelia Huck 	struct klist_iter i;
3200edb5860SCornelia Huck 	struct device *dev;
3210edb5860SCornelia Huck 
3220edb5860SCornelia Huck 	if (!bus)
3230edb5860SCornelia Huck 		return NULL;
3240edb5860SCornelia Huck 
3250edb5860SCornelia Huck 	klist_iter_init_node(&bus->klist_devices, &i,
3260edb5860SCornelia Huck 			     (start ? &start->knode_bus : NULL));
3270edb5860SCornelia Huck 	while ((dev = next_device(&i)))
3280edb5860SCornelia Huck 		if (match(dev, data) && get_device(dev))
3290edb5860SCornelia Huck 			break;
3300edb5860SCornelia Huck 	klist_iter_exit(&i);
3310edb5860SCornelia Huck 	return dev;
3320edb5860SCornelia Huck }
33338fdac3cSmochel@digitalimplant.org 
33438fdac3cSmochel@digitalimplant.org 
33538fdac3cSmochel@digitalimplant.org static struct device_driver * next_driver(struct klist_iter * i)
33638fdac3cSmochel@digitalimplant.org {
33738fdac3cSmochel@digitalimplant.org 	struct klist_node * n = klist_next(i);
33838fdac3cSmochel@digitalimplant.org 	return n ? container_of(n, struct device_driver, knode_bus) : NULL;
33938fdac3cSmochel@digitalimplant.org }
34038fdac3cSmochel@digitalimplant.org 
3411da177e4SLinus Torvalds /**
3421da177e4SLinus Torvalds  *	bus_for_each_drv - driver iterator
3431da177e4SLinus Torvalds  *	@bus:	bus we're dealing with.
3441da177e4SLinus Torvalds  *	@start:	driver to start iterating on.
3451da177e4SLinus Torvalds  *	@data:	data to pass to the callback.
3461da177e4SLinus Torvalds  *	@fn:	function to call for each driver.
3471da177e4SLinus Torvalds  *
3481da177e4SLinus Torvalds  *	This is nearly identical to the device iterator above.
3491da177e4SLinus Torvalds  *	We iterate over each driver that belongs to @bus, and call
3501da177e4SLinus Torvalds  *	@fn for each. If @fn returns anything but 0, we break out
3511da177e4SLinus Torvalds  *	and return it. If @start is not NULL, we use it as the head
3521da177e4SLinus Torvalds  *	of the list.
3531da177e4SLinus Torvalds  *
3541da177e4SLinus Torvalds  *	NOTE: we don't return the driver that returns a non-zero
3551da177e4SLinus Torvalds  *	value, nor do we leave the reference count incremented for that
3561da177e4SLinus Torvalds  *	driver. If the caller needs to know that info, it must set it
3571da177e4SLinus Torvalds  *	in the callback. It must also be sure to increment the refcount
3581da177e4SLinus Torvalds  *	so it doesn't disappear before returning to the caller.
3591da177e4SLinus Torvalds  */
3601da177e4SLinus Torvalds 
3611da177e4SLinus Torvalds int bus_for_each_drv(struct bus_type * bus, struct device_driver * start,
3621da177e4SLinus Torvalds 		     void * data, int (*fn)(struct device_driver *, void *))
3631da177e4SLinus Torvalds {
36438fdac3cSmochel@digitalimplant.org 	struct klist_iter i;
36538fdac3cSmochel@digitalimplant.org 	struct device_driver * drv;
36638fdac3cSmochel@digitalimplant.org 	int error = 0;
3671da177e4SLinus Torvalds 
36838fdac3cSmochel@digitalimplant.org 	if (!bus)
36938fdac3cSmochel@digitalimplant.org 		return -EINVAL;
37038fdac3cSmochel@digitalimplant.org 
37138fdac3cSmochel@digitalimplant.org 	klist_iter_init_node(&bus->klist_drivers, &i,
37238fdac3cSmochel@digitalimplant.org 			     start ? &start->knode_bus : NULL);
37338fdac3cSmochel@digitalimplant.org 	while ((drv = next_driver(&i)) && !error)
37438fdac3cSmochel@digitalimplant.org 		error = fn(drv, data);
37538fdac3cSmochel@digitalimplant.org 	klist_iter_exit(&i);
37638fdac3cSmochel@digitalimplant.org 	return error;
3771da177e4SLinus Torvalds }
3781da177e4SLinus Torvalds 
3791da177e4SLinus Torvalds static int device_add_attrs(struct bus_type *bus, struct device *dev)
3801da177e4SLinus Torvalds {
3811da177e4SLinus Torvalds 	int error = 0;
3821da177e4SLinus Torvalds 	int i;
3831da177e4SLinus Torvalds 
3844aca67e5SAndrew Morton 	if (!bus->dev_attrs)
3854aca67e5SAndrew Morton 		return 0;
3864aca67e5SAndrew Morton 
3871da177e4SLinus Torvalds 	for (i = 0; attr_name(bus->dev_attrs[i]); i++) {
3881da177e4SLinus Torvalds 		error = device_create_file(dev,&bus->dev_attrs[i]);
3894aca67e5SAndrew Morton 		if (error) {
3901da177e4SLinus Torvalds 			while (--i >= 0)
3911da177e4SLinus Torvalds 				device_remove_file(dev, &bus->dev_attrs[i]);
3924aca67e5SAndrew Morton 			break;
3931da177e4SLinus Torvalds 		}
3944aca67e5SAndrew Morton 	}
3954aca67e5SAndrew Morton 	return error;
3964aca67e5SAndrew Morton }
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds static void device_remove_attrs(struct bus_type * bus, struct device * dev)
3991da177e4SLinus Torvalds {
4001da177e4SLinus Torvalds 	int i;
4011da177e4SLinus Torvalds 
4021da177e4SLinus Torvalds 	if (bus->dev_attrs) {
4031da177e4SLinus Torvalds 		for (i = 0; attr_name(bus->dev_attrs[i]); i++)
4041da177e4SLinus Torvalds 			device_remove_file(dev,&bus->dev_attrs[i]);
4051da177e4SLinus Torvalds 	}
4061da177e4SLinus Torvalds }
4071da177e4SLinus Torvalds 
408b9cafc7dSKay Sievers #ifdef CONFIG_SYSFS_DEPRECATED
409b9cafc7dSKay Sievers static int make_deprecated_bus_links(struct device *dev)
410b9cafc7dSKay Sievers {
411b9cafc7dSKay Sievers 	return sysfs_create_link(&dev->kobj,
412823bccfcSGreg Kroah-Hartman 				 &dev->bus->subsys.kobj, "bus");
413b9cafc7dSKay Sievers }
414b9cafc7dSKay Sievers 
415b9cafc7dSKay Sievers static void remove_deprecated_bus_links(struct device *dev)
416b9cafc7dSKay Sievers {
417b9cafc7dSKay Sievers 	sysfs_remove_link(&dev->kobj, "bus");
418b9cafc7dSKay Sievers }
419b9cafc7dSKay Sievers #else
420b9cafc7dSKay Sievers static inline int make_deprecated_bus_links(struct device *dev) { return 0; }
421b9cafc7dSKay Sievers static inline void remove_deprecated_bus_links(struct device *dev) { }
422b9cafc7dSKay Sievers #endif
4231da177e4SLinus Torvalds 
4241da177e4SLinus Torvalds /**
4251da177e4SLinus Torvalds  *	bus_add_device - add device to bus
4261da177e4SLinus Torvalds  *	@dev:	device being added
4271da177e4SLinus Torvalds  *
4281da177e4SLinus Torvalds  *	- Add the device to its bus's list of devices.
42953877d06SKay Sievers  *	- Create link to device's bus.
4301da177e4SLinus Torvalds  */
4311da177e4SLinus Torvalds int bus_add_device(struct device * dev)
4321da177e4SLinus Torvalds {
4331da177e4SLinus Torvalds 	struct bus_type * bus = get_bus(dev->bus);
4341da177e4SLinus Torvalds 	int error = 0;
4351da177e4SLinus Torvalds 
4361da177e4SLinus Torvalds 	if (bus) {
4371da177e4SLinus Torvalds 		pr_debug("bus %s: add device %s\n", bus->name, dev->bus_id);
438ca2b94baSHannes Reinecke 		error = device_add_attrs(bus, dev);
439f86db396SAndrew Morton 		if (error)
440513e7337SCornelia Huck 			goto out_put;
441f86db396SAndrew Morton 		error = sysfs_create_link(&bus->devices.kobj,
442f86db396SAndrew Morton 						&dev->kobj, dev->bus_id);
443f86db396SAndrew Morton 		if (error)
444513e7337SCornelia Huck 			goto out_id;
445f86db396SAndrew Morton 		error = sysfs_create_link(&dev->kobj,
446823bccfcSGreg Kroah-Hartman 				&dev->bus->subsys.kobj, "subsystem");
447f86db396SAndrew Morton 		if (error)
448513e7337SCornelia Huck 			goto out_subsys;
449b9cafc7dSKay Sievers 		error = make_deprecated_bus_links(dev);
450513e7337SCornelia Huck 		if (error)
451513e7337SCornelia Huck 			goto out_deprecated;
4521da177e4SLinus Torvalds 	}
453513e7337SCornelia Huck 	return 0;
454513e7337SCornelia Huck 
455513e7337SCornelia Huck out_deprecated:
456513e7337SCornelia Huck 	sysfs_remove_link(&dev->kobj, "subsystem");
457513e7337SCornelia Huck out_subsys:
458513e7337SCornelia Huck 	sysfs_remove_link(&bus->devices.kobj, dev->bus_id);
459513e7337SCornelia Huck out_id:
460513e7337SCornelia Huck 	device_remove_attrs(bus, dev);
461513e7337SCornelia Huck out_put:
462513e7337SCornelia Huck 	put_bus(dev->bus);
4631da177e4SLinus Torvalds 	return error;
4641da177e4SLinus Torvalds }
4651da177e4SLinus Torvalds 
4661da177e4SLinus Torvalds /**
46753877d06SKay Sievers  *	bus_attach_device - add device to bus
46853877d06SKay Sievers  *	@dev:	device tried to attach to a driver
46953877d06SKay Sievers  *
470f2eaae19SAlan Stern  *	- Add device to bus's list of devices.
47153877d06SKay Sievers  *	- Try to attach to driver.
47253877d06SKay Sievers  */
473c6a46696SCornelia Huck void bus_attach_device(struct device * dev)
47453877d06SKay Sievers {
47553877d06SKay Sievers 	struct bus_type *bus = dev->bus;
476f86db396SAndrew Morton 	int ret = 0;
47753877d06SKay Sievers 
47853877d06SKay Sievers 	if (bus) {
479f2eaae19SAlan Stern 		dev->is_registered = 1;
480b8c5cec2SKay Sievers 		if (bus->drivers_autoprobe)
481f86db396SAndrew Morton 			ret = device_attach(dev);
482c6a46696SCornelia Huck 		WARN_ON(ret < 0);
483c6a46696SCornelia Huck 		if (ret >= 0)
48453877d06SKay Sievers 			klist_add_tail(&dev->knode_bus, &bus->klist_devices);
485c6a46696SCornelia Huck 		else
486f2eaae19SAlan Stern 			dev->is_registered = 0;
48753877d06SKay Sievers 	}
488f86db396SAndrew Morton }
48953877d06SKay Sievers 
49053877d06SKay Sievers /**
4911da177e4SLinus Torvalds  *	bus_remove_device - remove device from bus
4921da177e4SLinus Torvalds  *	@dev:	device to be removed
4931da177e4SLinus Torvalds  *
4941da177e4SLinus Torvalds  *	- Remove symlink from bus's directory.
4951da177e4SLinus Torvalds  *	- Delete device from bus's list.
4961da177e4SLinus Torvalds  *	- Detach from its driver.
4971da177e4SLinus Torvalds  *	- Drop reference taken in bus_add_device().
4981da177e4SLinus Torvalds  */
4991da177e4SLinus Torvalds void bus_remove_device(struct device * dev)
5001da177e4SLinus Torvalds {
5011da177e4SLinus Torvalds 	if (dev->bus) {
502b9d9c82bSKay Sievers 		sysfs_remove_link(&dev->kobj, "subsystem");
503b9cafc7dSKay Sievers 		remove_deprecated_bus_links(dev);
5041da177e4SLinus Torvalds 		sysfs_remove_link(&dev->bus->devices.kobj, dev->bus_id);
5051da177e4SLinus Torvalds 		device_remove_attrs(dev->bus, dev);
506f70fa629SAlan Stern 		if (dev->is_registered) {
507f2eaae19SAlan Stern 			dev->is_registered = 0;
508f2eaae19SAlan Stern 			klist_del(&dev->knode_bus);
509f70fa629SAlan Stern 		}
5101da177e4SLinus Torvalds 		pr_debug("bus %s: remove device %s\n", dev->bus->name, dev->bus_id);
5111da177e4SLinus Torvalds 		device_release_driver(dev);
5121da177e4SLinus Torvalds 		put_bus(dev->bus);
5131da177e4SLinus Torvalds 	}
5141da177e4SLinus Torvalds }
5151da177e4SLinus Torvalds 
5161da177e4SLinus Torvalds static int driver_add_attrs(struct bus_type * bus, struct device_driver * drv)
5171da177e4SLinus Torvalds {
5181da177e4SLinus Torvalds 	int error = 0;
5191da177e4SLinus Torvalds 	int i;
5201da177e4SLinus Torvalds 
5211da177e4SLinus Torvalds 	if (bus->drv_attrs) {
5221da177e4SLinus Torvalds 		for (i = 0; attr_name(bus->drv_attrs[i]); i++) {
5231da177e4SLinus Torvalds 			error = driver_create_file(drv, &bus->drv_attrs[i]);
5241da177e4SLinus Torvalds 			if (error)
5251da177e4SLinus Torvalds 				goto Err;
5261da177e4SLinus Torvalds 		}
5271da177e4SLinus Torvalds 	}
5281da177e4SLinus Torvalds  Done:
5291da177e4SLinus Torvalds 	return error;
5301da177e4SLinus Torvalds  Err:
5311da177e4SLinus Torvalds 	while (--i >= 0)
5321da177e4SLinus Torvalds 		driver_remove_file(drv, &bus->drv_attrs[i]);
5331da177e4SLinus Torvalds 	goto Done;
5341da177e4SLinus Torvalds }
5351da177e4SLinus Torvalds 
5361da177e4SLinus Torvalds 
5371da177e4SLinus Torvalds static void driver_remove_attrs(struct bus_type * bus, struct device_driver * drv)
5381da177e4SLinus Torvalds {
5391da177e4SLinus Torvalds 	int i;
5401da177e4SLinus Torvalds 
5411da177e4SLinus Torvalds 	if (bus->drv_attrs) {
5421da177e4SLinus Torvalds 		for (i = 0; attr_name(bus->drv_attrs[i]); i++)
5431da177e4SLinus Torvalds 			driver_remove_file(drv, &bus->drv_attrs[i]);
5441da177e4SLinus Torvalds 	}
5451da177e4SLinus Torvalds }
5461da177e4SLinus Torvalds 
547874c6241SGreg Kroah-Hartman #ifdef CONFIG_HOTPLUG
548874c6241SGreg Kroah-Hartman /*
549874c6241SGreg Kroah-Hartman  * Thanks to drivers making their tables __devinit, we can't allow manual
550874c6241SGreg Kroah-Hartman  * bind and unbind from userspace unless CONFIG_HOTPLUG is enabled.
551874c6241SGreg Kroah-Hartman  */
552f86db396SAndrew Morton static int __must_check add_bind_files(struct device_driver *drv)
553874c6241SGreg Kroah-Hartman {
554f86db396SAndrew Morton 	int ret;
555f86db396SAndrew Morton 
556f86db396SAndrew Morton 	ret = driver_create_file(drv, &driver_attr_unbind);
557f86db396SAndrew Morton 	if (ret == 0) {
558f86db396SAndrew Morton 		ret = driver_create_file(drv, &driver_attr_bind);
559f86db396SAndrew Morton 		if (ret)
560f86db396SAndrew Morton 			driver_remove_file(drv, &driver_attr_unbind);
561f86db396SAndrew Morton 	}
562f86db396SAndrew Morton 	return ret;
563874c6241SGreg Kroah-Hartman }
564874c6241SGreg Kroah-Hartman 
565874c6241SGreg Kroah-Hartman static void remove_bind_files(struct device_driver *drv)
566874c6241SGreg Kroah-Hartman {
567874c6241SGreg Kroah-Hartman 	driver_remove_file(drv, &driver_attr_bind);
568874c6241SGreg Kroah-Hartman 	driver_remove_file(drv, &driver_attr_unbind);
569874c6241SGreg Kroah-Hartman }
570b8c5cec2SKay Sievers 
571*8380770cSKay Sievers static BUS_ATTR(drivers_probe, S_IWUSR, NULL, store_drivers_probe);
572*8380770cSKay Sievers static BUS_ATTR(drivers_autoprobe, S_IWUSR | S_IRUGO,
573*8380770cSKay Sievers 		show_drivers_autoprobe, store_drivers_autoprobe);
574*8380770cSKay Sievers 
575b8c5cec2SKay Sievers static int add_probe_files(struct bus_type *bus)
576b8c5cec2SKay Sievers {
577b8c5cec2SKay Sievers 	int retval;
578b8c5cec2SKay Sievers 
579*8380770cSKay Sievers 	retval = bus_create_file(bus, &bus_attr_drivers_probe);
580b8c5cec2SKay Sievers 	if (retval)
581b8c5cec2SKay Sievers 		goto out;
582b8c5cec2SKay Sievers 
583*8380770cSKay Sievers 	retval = bus_create_file(bus, &bus_attr_drivers_autoprobe);
584b8c5cec2SKay Sievers 	if (retval)
585*8380770cSKay Sievers 		bus_remove_file(bus, &bus_attr_drivers_probe);
586b8c5cec2SKay Sievers out:
587b8c5cec2SKay Sievers 	return retval;
588b8c5cec2SKay Sievers }
589b8c5cec2SKay Sievers 
590b8c5cec2SKay Sievers static void remove_probe_files(struct bus_type *bus)
591b8c5cec2SKay Sievers {
592*8380770cSKay Sievers 	bus_remove_file(bus, &bus_attr_drivers_autoprobe);
593*8380770cSKay Sievers 	bus_remove_file(bus, &bus_attr_drivers_probe);
594b8c5cec2SKay Sievers }
595874c6241SGreg Kroah-Hartman #else
59635acfdd7SYoichi Yuasa static inline int add_bind_files(struct device_driver *drv) { return 0; }
597874c6241SGreg Kroah-Hartman static inline void remove_bind_files(struct device_driver *drv) {}
598b8c5cec2SKay Sievers static inline int add_probe_files(struct bus_type *bus) { return 0; }
599b8c5cec2SKay Sievers static inline void remove_probe_files(struct bus_type *bus) {}
600874c6241SGreg Kroah-Hartman #endif
6011da177e4SLinus Torvalds 
6021da177e4SLinus Torvalds /**
6031da177e4SLinus Torvalds  *	bus_add_driver - Add a driver to the bus.
6041da177e4SLinus Torvalds  *	@drv:	driver.
6051da177e4SLinus Torvalds  *
6061da177e4SLinus Torvalds  */
6071da177e4SLinus Torvalds int bus_add_driver(struct device_driver *drv)
6081da177e4SLinus Torvalds {
6091da177e4SLinus Torvalds 	struct bus_type * bus = get_bus(drv->bus);
6101da177e4SLinus Torvalds 	int error = 0;
6111da177e4SLinus Torvalds 
612d9fd4d3bSJeff Garzik 	if (!bus)
6134f6e1945SGreg Kroah-Hartman 		return -EINVAL;
614d9fd4d3bSJeff Garzik 
6151da177e4SLinus Torvalds 	pr_debug("bus %s: add driver %s\n", bus->name, drv->name);
6161da177e4SLinus Torvalds 	error = kobject_set_name(&drv->kobj, "%s", drv->name);
617f86db396SAndrew Morton 	if (error)
618f86db396SAndrew Morton 		goto out_put_bus;
6191da177e4SLinus Torvalds 	drv->kobj.kset = &bus->drivers;
620dc0afa83SCornelia Huck 	error = kobject_register(&drv->kobj);
621dc0afa83SCornelia Huck 	if (error)
622f86db396SAndrew Morton 		goto out_put_bus;
6231da177e4SLinus Torvalds 
624b8c5cec2SKay Sievers 	if (drv->bus->drivers_autoprobe) {
625f86db396SAndrew Morton 		error = driver_attach(drv);
626f86db396SAndrew Morton 		if (error)
627f86db396SAndrew Morton 			goto out_unregister;
628b8c5cec2SKay Sievers 	}
629d856f1e3SJames Bottomley 	klist_add_tail(&drv->knode_bus, &bus->klist_drivers);
6301da177e4SLinus Torvalds 	module_add_driver(drv->owner, drv);
6311da177e4SLinus Torvalds 
632f86db396SAndrew Morton 	error = driver_add_attrs(bus, drv);
633f86db396SAndrew Morton 	if (error) {
634f86db396SAndrew Morton 		/* How the hell do we get out of this pickle? Give up */
635f86db396SAndrew Morton 		printk(KERN_ERR "%s: driver_add_attrs(%s) failed\n",
636f86db396SAndrew Morton 			__FUNCTION__, drv->name);
637f86db396SAndrew Morton 	}
638f86db396SAndrew Morton 	error = add_bind_files(drv);
639f86db396SAndrew Morton 	if (error) {
640f86db396SAndrew Morton 		/* Ditto */
641f86db396SAndrew Morton 		printk(KERN_ERR "%s: add_bind_files(%s) failed\n",
642f86db396SAndrew Morton 			__FUNCTION__, drv->name);
643f86db396SAndrew Morton 	}
644d9fd4d3bSJeff Garzik 
6451da177e4SLinus Torvalds 	return error;
646f86db396SAndrew Morton out_unregister:
647f86db396SAndrew Morton 	kobject_unregister(&drv->kobj);
648f86db396SAndrew Morton out_put_bus:
649f86db396SAndrew Morton 	put_bus(bus);
650f86db396SAndrew Morton 	return error;
6511da177e4SLinus Torvalds }
6521da177e4SLinus Torvalds 
6531da177e4SLinus Torvalds /**
6541da177e4SLinus Torvalds  *	bus_remove_driver - delete driver from bus's knowledge.
6551da177e4SLinus Torvalds  *	@drv:	driver.
6561da177e4SLinus Torvalds  *
6571da177e4SLinus Torvalds  *	Detach the driver from the devices it controls, and remove
6581da177e4SLinus Torvalds  *	it from its bus's list of drivers. Finally, we drop the reference
6591da177e4SLinus Torvalds  *	to the bus we took in bus_add_driver().
6601da177e4SLinus Torvalds  */
6611da177e4SLinus Torvalds 
6621da177e4SLinus Torvalds void bus_remove_driver(struct device_driver * drv)
6631da177e4SLinus Torvalds {
664d9fd4d3bSJeff Garzik 	if (!drv->bus)
665d9fd4d3bSJeff Garzik 		return;
666d9fd4d3bSJeff Garzik 
667874c6241SGreg Kroah-Hartman 	remove_bind_files(drv);
6681da177e4SLinus Torvalds 	driver_remove_attrs(drv->bus, drv);
66938fdac3cSmochel@digitalimplant.org 	klist_remove(&drv->knode_bus);
6701da177e4SLinus Torvalds 	pr_debug("bus %s: remove driver %s\n", drv->bus->name, drv->name);
6711da177e4SLinus Torvalds 	driver_detach(drv);
6721da177e4SLinus Torvalds 	module_remove_driver(drv);
6731da177e4SLinus Torvalds 	kobject_unregister(&drv->kobj);
6741da177e4SLinus Torvalds 	put_bus(drv->bus);
6751da177e4SLinus Torvalds }
6761da177e4SLinus Torvalds 
6771da177e4SLinus Torvalds 
6781da177e4SLinus Torvalds /* Helper for bus_rescan_devices's iter */
679f86db396SAndrew Morton static int __must_check bus_rescan_devices_helper(struct device *dev,
680f86db396SAndrew Morton 						void *data)
6811da177e4SLinus Torvalds {
682f86db396SAndrew Morton 	int ret = 0;
683f86db396SAndrew Morton 
684bf74ad5bSAlan Stern 	if (!dev->driver) {
685bf74ad5bSAlan Stern 		if (dev->parent)	/* Needed for USB */
686bf74ad5bSAlan Stern 			down(&dev->parent->sem);
687f86db396SAndrew Morton 		ret = device_attach(dev);
688bf74ad5bSAlan Stern 		if (dev->parent)
689bf74ad5bSAlan Stern 			up(&dev->parent->sem);
690bf74ad5bSAlan Stern 	}
691f86db396SAndrew Morton 	return ret < 0 ? ret : 0;
6921da177e4SLinus Torvalds }
6931da177e4SLinus Torvalds 
6941da177e4SLinus Torvalds /**
6951da177e4SLinus Torvalds  * bus_rescan_devices - rescan devices on the bus for possible drivers
6961da177e4SLinus Torvalds  * @bus: the bus to scan.
6971da177e4SLinus Torvalds  *
6981da177e4SLinus Torvalds  * This function will look for devices on the bus with no driver
69923d3d602SGreg Kroah-Hartman  * attached and rescan it against existing drivers to see if it matches
70023d3d602SGreg Kroah-Hartman  * any by calling device_attach() for the unbound devices.
7011da177e4SLinus Torvalds  */
702f86db396SAndrew Morton int bus_rescan_devices(struct bus_type * bus)
7031da177e4SLinus Torvalds {
704f86db396SAndrew Morton 	return bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper);
7051da177e4SLinus Torvalds }
7061da177e4SLinus Torvalds 
707e935d5daSMoore, Eric /**
708e935d5daSMoore, Eric  * device_reprobe - remove driver for a device and probe for a new driver
709e935d5daSMoore, Eric  * @dev: the device to reprobe
710e935d5daSMoore, Eric  *
711e935d5daSMoore, Eric  * This function detaches the attached driver (if any) for the given
712e935d5daSMoore, Eric  * device and restarts the driver probing process.  It is intended
713e935d5daSMoore, Eric  * to use if probing criteria changed during a devices lifetime and
714e935d5daSMoore, Eric  * driver attachment should change accordingly.
715e935d5daSMoore, Eric  */
716f86db396SAndrew Morton int device_reprobe(struct device *dev)
717e935d5daSMoore, Eric {
718e935d5daSMoore, Eric 	if (dev->driver) {
719e935d5daSMoore, Eric 		if (dev->parent)        /* Needed for USB */
720e935d5daSMoore, Eric 			down(&dev->parent->sem);
721e935d5daSMoore, Eric 		device_release_driver(dev);
722e935d5daSMoore, Eric 		if (dev->parent)
723e935d5daSMoore, Eric 			up(&dev->parent->sem);
724e935d5daSMoore, Eric 	}
725f86db396SAndrew Morton 	return bus_rescan_devices_helper(dev, NULL);
726e935d5daSMoore, Eric }
727e935d5daSMoore, Eric EXPORT_SYMBOL_GPL(device_reprobe);
7281da177e4SLinus Torvalds 
7291da177e4SLinus Torvalds struct bus_type *get_bus(struct bus_type *bus)
7301da177e4SLinus Torvalds {
731f86db396SAndrew Morton 	return bus ? container_of(subsys_get(&bus->subsys),
732f86db396SAndrew Morton 				struct bus_type, subsys) : NULL;
7331da177e4SLinus Torvalds }
7341da177e4SLinus Torvalds 
7351da177e4SLinus Torvalds void put_bus(struct bus_type * bus)
7361da177e4SLinus Torvalds {
7371da177e4SLinus Torvalds 	subsys_put(&bus->subsys);
7381da177e4SLinus Torvalds }
7391da177e4SLinus Torvalds 
7401da177e4SLinus Torvalds 
7411da177e4SLinus Torvalds /**
7421da177e4SLinus Torvalds  *	find_bus - locate bus by name.
7431da177e4SLinus Torvalds  *	@name:	name of bus.
7441da177e4SLinus Torvalds  *
7451da177e4SLinus Torvalds  *	Call kset_find_obj() to iterate over list of buses to
7461da177e4SLinus Torvalds  *	find a bus by name. Return bus if found.
7471da177e4SLinus Torvalds  *
7481da177e4SLinus Torvalds  *	Note that kset_find_obj increments bus' reference count.
7491da177e4SLinus Torvalds  */
7507e4ef085SAdrian Bunk #if 0
7511da177e4SLinus Torvalds struct bus_type * find_bus(char * name)
7521da177e4SLinus Torvalds {
7531da177e4SLinus Torvalds 	struct kobject * k = kset_find_obj(&bus_subsys.kset, name);
7541da177e4SLinus Torvalds 	return k ? to_bus(k) : NULL;
7551da177e4SLinus Torvalds }
7567e4ef085SAdrian Bunk #endif  /*  0  */
7571da177e4SLinus Torvalds 
7581da177e4SLinus Torvalds 
7591da177e4SLinus Torvalds /**
7601da177e4SLinus Torvalds  *	bus_add_attrs - Add default attributes for this bus.
7611da177e4SLinus Torvalds  *	@bus:	Bus that has just been registered.
7621da177e4SLinus Torvalds  */
7631da177e4SLinus Torvalds 
7641da177e4SLinus Torvalds static int bus_add_attrs(struct bus_type * bus)
7651da177e4SLinus Torvalds {
7661da177e4SLinus Torvalds 	int error = 0;
7671da177e4SLinus Torvalds 	int i;
7681da177e4SLinus Torvalds 
7691da177e4SLinus Torvalds 	if (bus->bus_attrs) {
7701da177e4SLinus Torvalds 		for (i = 0; attr_name(bus->bus_attrs[i]); i++) {
771dc0afa83SCornelia Huck 			error = bus_create_file(bus,&bus->bus_attrs[i]);
772dc0afa83SCornelia Huck 			if (error)
7731da177e4SLinus Torvalds 				goto Err;
7741da177e4SLinus Torvalds 		}
7751da177e4SLinus Torvalds 	}
7761da177e4SLinus Torvalds  Done:
7771da177e4SLinus Torvalds 	return error;
7781da177e4SLinus Torvalds  Err:
7791da177e4SLinus Torvalds 	while (--i >= 0)
7801da177e4SLinus Torvalds 		bus_remove_file(bus,&bus->bus_attrs[i]);
7811da177e4SLinus Torvalds 	goto Done;
7821da177e4SLinus Torvalds }
7831da177e4SLinus Torvalds 
7841da177e4SLinus Torvalds static void bus_remove_attrs(struct bus_type * bus)
7851da177e4SLinus Torvalds {
7861da177e4SLinus Torvalds 	int i;
7871da177e4SLinus Torvalds 
7881da177e4SLinus Torvalds 	if (bus->bus_attrs) {
7891da177e4SLinus Torvalds 		for (i = 0; attr_name(bus->bus_attrs[i]); i++)
7901da177e4SLinus Torvalds 			bus_remove_file(bus,&bus->bus_attrs[i]);
7911da177e4SLinus Torvalds 	}
7921da177e4SLinus Torvalds }
7931da177e4SLinus Torvalds 
79434bb61f9SJames Bottomley static void klist_devices_get(struct klist_node *n)
79534bb61f9SJames Bottomley {
79634bb61f9SJames Bottomley 	struct device *dev = container_of(n, struct device, knode_bus);
79734bb61f9SJames Bottomley 
79834bb61f9SJames Bottomley 	get_device(dev);
79934bb61f9SJames Bottomley }
80034bb61f9SJames Bottomley 
80134bb61f9SJames Bottomley static void klist_devices_put(struct klist_node *n)
80234bb61f9SJames Bottomley {
80334bb61f9SJames Bottomley 	struct device *dev = container_of(n, struct device, knode_bus);
80434bb61f9SJames Bottomley 
80534bb61f9SJames Bottomley 	put_device(dev);
80634bb61f9SJames Bottomley }
80734bb61f9SJames Bottomley 
8081da177e4SLinus Torvalds /**
8091da177e4SLinus Torvalds  *	bus_register - register a bus with the system.
8101da177e4SLinus Torvalds  *	@bus:	bus.
8111da177e4SLinus Torvalds  *
8121da177e4SLinus Torvalds  *	Once we have that, we registered the bus with the kobject
8131da177e4SLinus Torvalds  *	infrastructure, then register the children subsystems it has:
8141da177e4SLinus Torvalds  *	the devices and drivers that belong to the bus.
8151da177e4SLinus Torvalds  */
8161da177e4SLinus Torvalds int bus_register(struct bus_type * bus)
8171da177e4SLinus Torvalds {
8181da177e4SLinus Torvalds 	int retval;
8191da177e4SLinus Torvalds 
820116af378SBenjamin Herrenschmidt 	BLOCKING_INIT_NOTIFIER_HEAD(&bus->bus_notifier);
821116af378SBenjamin Herrenschmidt 
822823bccfcSGreg Kroah-Hartman 	retval = kobject_set_name(&bus->subsys.kobj, "%s", bus->name);
8231da177e4SLinus Torvalds 	if (retval)
8241da177e4SLinus Torvalds 		goto out;
8251da177e4SLinus Torvalds 
8261da177e4SLinus Torvalds 	subsys_set_kset(bus, bus_subsys);
8271da177e4SLinus Torvalds 	retval = subsystem_register(&bus->subsys);
8281da177e4SLinus Torvalds 	if (retval)
8291da177e4SLinus Torvalds 		goto out;
8301da177e4SLinus Torvalds 
8311da177e4SLinus Torvalds 	kobject_set_name(&bus->devices.kobj, "devices");
832823bccfcSGreg Kroah-Hartman 	bus->devices.kobj.parent = &bus->subsys.kobj;
8331da177e4SLinus Torvalds 	retval = kset_register(&bus->devices);
8341da177e4SLinus Torvalds 	if (retval)
8351da177e4SLinus Torvalds 		goto bus_devices_fail;
8361da177e4SLinus Torvalds 
8371da177e4SLinus Torvalds 	kobject_set_name(&bus->drivers.kobj, "drivers");
838823bccfcSGreg Kroah-Hartman 	bus->drivers.kobj.parent = &bus->subsys.kobj;
8391da177e4SLinus Torvalds 	bus->drivers.ktype = &ktype_driver;
8401da177e4SLinus Torvalds 	retval = kset_register(&bus->drivers);
8411da177e4SLinus Torvalds 	if (retval)
8421da177e4SLinus Torvalds 		goto bus_drivers_fail;
843465c7a3aSmochel@digitalimplant.org 
84434bb61f9SJames Bottomley 	klist_init(&bus->klist_devices, klist_devices_get, klist_devices_put);
84581107bf5SAlan Stern 	klist_init(&bus->klist_drivers, NULL, NULL);
846b8c5cec2SKay Sievers 
847b8c5cec2SKay Sievers 	bus->drivers_autoprobe = 1;
848b8c5cec2SKay Sievers 	retval = add_probe_files(bus);
849b8c5cec2SKay Sievers 	if (retval)
850b8c5cec2SKay Sievers 		goto bus_probe_files_fail;
851b8c5cec2SKay Sievers 
8521bb6881aSCornelia Huck 	retval = bus_add_attrs(bus);
8531bb6881aSCornelia Huck 	if (retval)
8541bb6881aSCornelia Huck 		goto bus_attrs_fail;
8551da177e4SLinus Torvalds 
8561da177e4SLinus Torvalds 	pr_debug("bus type '%s' registered\n", bus->name);
8571da177e4SLinus Torvalds 	return 0;
8581da177e4SLinus Torvalds 
8591bb6881aSCornelia Huck bus_attrs_fail:
860b8c5cec2SKay Sievers 	remove_probe_files(bus);
861b8c5cec2SKay Sievers bus_probe_files_fail:
8621bb6881aSCornelia Huck 	kset_unregister(&bus->drivers);
8631da177e4SLinus Torvalds bus_drivers_fail:
8641da177e4SLinus Torvalds 	kset_unregister(&bus->devices);
8651da177e4SLinus Torvalds bus_devices_fail:
8661da177e4SLinus Torvalds 	subsystem_unregister(&bus->subsys);
8671da177e4SLinus Torvalds out:
8681da177e4SLinus Torvalds 	return retval;
8691da177e4SLinus Torvalds }
8701da177e4SLinus Torvalds 
8711da177e4SLinus Torvalds /**
8721da177e4SLinus Torvalds  *	bus_unregister - remove a bus from the system
8731da177e4SLinus Torvalds  *	@bus:	bus.
8741da177e4SLinus Torvalds  *
8751da177e4SLinus Torvalds  *	Unregister the child subsystems and the bus itself.
8761da177e4SLinus Torvalds  *	Finally, we call put_bus() to release the refcount
8771da177e4SLinus Torvalds  */
8781da177e4SLinus Torvalds void bus_unregister(struct bus_type * bus)
8791da177e4SLinus Torvalds {
8801da177e4SLinus Torvalds 	pr_debug("bus %s: unregistering\n", bus->name);
8811da177e4SLinus Torvalds 	bus_remove_attrs(bus);
882b8c5cec2SKay Sievers 	remove_probe_files(bus);
8831da177e4SLinus Torvalds 	kset_unregister(&bus->drivers);
8841da177e4SLinus Torvalds 	kset_unregister(&bus->devices);
8851da177e4SLinus Torvalds 	subsystem_unregister(&bus->subsys);
8861da177e4SLinus Torvalds }
8871da177e4SLinus Torvalds 
888116af378SBenjamin Herrenschmidt int bus_register_notifier(struct bus_type *bus, struct notifier_block *nb)
889116af378SBenjamin Herrenschmidt {
890116af378SBenjamin Herrenschmidt 	return blocking_notifier_chain_register(&bus->bus_notifier, nb);
891116af378SBenjamin Herrenschmidt }
892116af378SBenjamin Herrenschmidt EXPORT_SYMBOL_GPL(bus_register_notifier);
893116af378SBenjamin Herrenschmidt 
894116af378SBenjamin Herrenschmidt int bus_unregister_notifier(struct bus_type *bus, struct notifier_block *nb)
895116af378SBenjamin Herrenschmidt {
896116af378SBenjamin Herrenschmidt 	return blocking_notifier_chain_unregister(&bus->bus_notifier, nb);
897116af378SBenjamin Herrenschmidt }
898116af378SBenjamin Herrenschmidt EXPORT_SYMBOL_GPL(bus_unregister_notifier);
899116af378SBenjamin Herrenschmidt 
9001da177e4SLinus Torvalds int __init buses_init(void)
9011da177e4SLinus Torvalds {
9021da177e4SLinus Torvalds 	return subsystem_register(&bus_subsys);
9031da177e4SLinus Torvalds }
9041da177e4SLinus Torvalds 
9051da177e4SLinus Torvalds 
9061da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(bus_for_each_dev);
9070edb5860SCornelia Huck EXPORT_SYMBOL_GPL(bus_find_device);
9081da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(bus_for_each_drv);
9091da177e4SLinus Torvalds 
9101da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(bus_register);
9111da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(bus_unregister);
9121da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(bus_rescan_devices);
9131da177e4SLinus Torvalds 
9141da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(bus_create_file);
9151da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(bus_remove_file);
916