xref: /linux/drivers/base/bus.c (revision 3514faca19a6fdc209734431c509631ea92b094e)
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 
335901d014SGreg Kroah-Hartman static struct bus_type *bus_get(struct bus_type *bus)
345901d014SGreg Kroah-Hartman {
355901d014SGreg Kroah-Hartman 	return bus ? container_of(kset_get(&bus->subsys),
365901d014SGreg Kroah-Hartman 				struct bus_type, subsys) : NULL;
375901d014SGreg Kroah-Hartman }
385901d014SGreg Kroah-Hartman 
39fc1ede58SGreg Kroah-Hartman static void bus_put(struct bus_type *bus)
40fc1ede58SGreg Kroah-Hartman {
41fc1ede58SGreg Kroah-Hartman 	kset_put(&bus->subsys);
42fc1ede58SGreg Kroah-Hartman }
43fc1ede58SGreg Kroah-Hartman 
441da177e4SLinus Torvalds static ssize_t
451da177e4SLinus Torvalds drv_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
461da177e4SLinus Torvalds {
471da177e4SLinus Torvalds 	struct driver_attribute * drv_attr = to_drv_attr(attr);
481da177e4SLinus Torvalds 	struct device_driver * drv = to_driver(kobj);
494a0c20bfSDmitry Torokhov 	ssize_t ret = -EIO;
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds 	if (drv_attr->show)
521da177e4SLinus Torvalds 		ret = drv_attr->show(drv, buf);
531da177e4SLinus Torvalds 	return ret;
541da177e4SLinus Torvalds }
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds static ssize_t
571da177e4SLinus Torvalds drv_attr_store(struct kobject * kobj, struct attribute * attr,
581da177e4SLinus Torvalds 	       const char * buf, size_t count)
591da177e4SLinus Torvalds {
601da177e4SLinus Torvalds 	struct driver_attribute * drv_attr = to_drv_attr(attr);
611da177e4SLinus Torvalds 	struct device_driver * drv = to_driver(kobj);
624a0c20bfSDmitry Torokhov 	ssize_t ret = -EIO;
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds 	if (drv_attr->store)
651da177e4SLinus Torvalds 		ret = drv_attr->store(drv, buf, count);
661da177e4SLinus Torvalds 	return ret;
671da177e4SLinus Torvalds }
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds static struct sysfs_ops driver_sysfs_ops = {
701da177e4SLinus Torvalds 	.show	= drv_attr_show,
711da177e4SLinus Torvalds 	.store	= drv_attr_store,
721da177e4SLinus Torvalds };
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds static void driver_release(struct kobject * kobj)
761da177e4SLinus Torvalds {
7774e9f5faSGreg Kroah-Hartman 	/*
7874e9f5faSGreg Kroah-Hartman 	 * Yes this is an empty release function, it is this way because struct
7974e9f5faSGreg Kroah-Hartman 	 * device is always a static object, not a dynamic one.  Yes, this is
8074e9f5faSGreg Kroah-Hartman 	 * not nice and bad, but remember, drivers are code, reference counted
8174e9f5faSGreg Kroah-Hartman 	 * by the module count, not a device, which is really data.  And yes,
8274e9f5faSGreg Kroah-Hartman 	 * in the future I do want to have all drivers be created dynamically,
8374e9f5faSGreg Kroah-Hartman 	 * and am working toward that goal, but it will take a bit longer...
8474e9f5faSGreg Kroah-Hartman 	 *
8574e9f5faSGreg Kroah-Hartman 	 * But do not let this example give _anyone_ the idea that they can
8674e9f5faSGreg Kroah-Hartman 	 * create a release function without any code in it at all, to do that
8774e9f5faSGreg Kroah-Hartman 	 * is almost always wrong.  If you have any questions about this,
8874e9f5faSGreg Kroah-Hartman 	 * please send an email to <greg@kroah.com>
8974e9f5faSGreg Kroah-Hartman 	 */
901da177e4SLinus Torvalds }
911da177e4SLinus Torvalds 
92a1148fb0SGreg Kroah-Hartman static struct kobj_type driver_ktype = {
931da177e4SLinus Torvalds 	.sysfs_ops	= &driver_sysfs_ops,
941da177e4SLinus Torvalds 	.release	= driver_release,
951da177e4SLinus Torvalds };
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds /*
991da177e4SLinus Torvalds  * sysfs bindings for buses
1001da177e4SLinus Torvalds  */
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds 
1031da177e4SLinus Torvalds static ssize_t
1041da177e4SLinus Torvalds bus_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
1051da177e4SLinus Torvalds {
1061da177e4SLinus Torvalds 	struct bus_attribute * bus_attr = to_bus_attr(attr);
1071da177e4SLinus Torvalds 	struct bus_type * bus = to_bus(kobj);
1081da177e4SLinus Torvalds 	ssize_t ret = 0;
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds 	if (bus_attr->show)
1111da177e4SLinus Torvalds 		ret = bus_attr->show(bus, buf);
1121da177e4SLinus Torvalds 	return ret;
1131da177e4SLinus Torvalds }
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds static ssize_t
1161da177e4SLinus Torvalds bus_attr_store(struct kobject * kobj, struct attribute * attr,
1171da177e4SLinus Torvalds 	       const char * buf, size_t count)
1181da177e4SLinus Torvalds {
1191da177e4SLinus Torvalds 	struct bus_attribute * bus_attr = to_bus_attr(attr);
1201da177e4SLinus Torvalds 	struct bus_type * bus = to_bus(kobj);
1211da177e4SLinus Torvalds 	ssize_t ret = 0;
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds 	if (bus_attr->store)
1241da177e4SLinus Torvalds 		ret = bus_attr->store(bus, buf, count);
1251da177e4SLinus Torvalds 	return ret;
1261da177e4SLinus Torvalds }
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds static struct sysfs_ops bus_sysfs_ops = {
1291da177e4SLinus Torvalds 	.show	= bus_attr_show,
1301da177e4SLinus Torvalds 	.store	= bus_attr_store,
1311da177e4SLinus Torvalds };
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds int bus_create_file(struct bus_type * bus, struct bus_attribute * attr)
1341da177e4SLinus Torvalds {
1351da177e4SLinus Torvalds 	int error;
1365901d014SGreg Kroah-Hartman 	if (bus_get(bus)) {
137823bccfcSGreg Kroah-Hartman 		error = sysfs_create_file(&bus->subsys.kobj, &attr->attr);
138fc1ede58SGreg Kroah-Hartman 		bus_put(bus);
1391da177e4SLinus Torvalds 	} else
1401da177e4SLinus Torvalds 		error = -EINVAL;
1411da177e4SLinus Torvalds 	return error;
1421da177e4SLinus Torvalds }
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds void bus_remove_file(struct bus_type * bus, struct bus_attribute * attr)
1451da177e4SLinus Torvalds {
1465901d014SGreg Kroah-Hartman 	if (bus_get(bus)) {
147823bccfcSGreg Kroah-Hartman 		sysfs_remove_file(&bus->subsys.kobj, &attr->attr);
148fc1ede58SGreg Kroah-Hartman 		bus_put(bus);
1491da177e4SLinus Torvalds 	}
1501da177e4SLinus Torvalds }
1511da177e4SLinus Torvalds 
15280f03e34SKay Sievers static struct kobj_type bus_ktype = {
1531da177e4SLinus Torvalds 	.sysfs_ops	= &bus_sysfs_ops,
1541da177e4SLinus Torvalds };
1551da177e4SLinus Torvalds 
15680f03e34SKay Sievers static int bus_uevent_filter(struct kset *kset, struct kobject *kobj)
15780f03e34SKay Sievers {
15880f03e34SKay Sievers 	struct kobj_type *ktype = get_ktype(kobj);
15980f03e34SKay Sievers 
16080f03e34SKay Sievers 	if (ktype == &bus_ktype)
16180f03e34SKay Sievers 		return 1;
16280f03e34SKay Sievers 	return 0;
16380f03e34SKay Sievers }
16480f03e34SKay Sievers 
16580f03e34SKay Sievers static struct kset_uevent_ops bus_uevent_ops = {
16680f03e34SKay Sievers 	.filter = bus_uevent_filter,
16780f03e34SKay Sievers };
16880f03e34SKay Sievers 
169*3514facaSGreg Kroah-Hartman static decl_subsys(bus, &bus_uevent_ops);
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds 
1722139bdd5SRussell King #ifdef CONFIG_HOTPLUG
1732b08c8d0SAlan Stern /* Manually detach a device from its associated driver. */
174151ef38fSGreg Kroah-Hartman static int driver_helper(struct device *dev, void *data)
175151ef38fSGreg Kroah-Hartman {
176151ef38fSGreg Kroah-Hartman 	const char *name = data;
177151ef38fSGreg Kroah-Hartman 
178151ef38fSGreg Kroah-Hartman 	if (strcmp(name, dev->bus_id) == 0)
179151ef38fSGreg Kroah-Hartman 		return 1;
180151ef38fSGreg Kroah-Hartman 	return 0;
181151ef38fSGreg Kroah-Hartman }
182151ef38fSGreg Kroah-Hartman 
183151ef38fSGreg Kroah-Hartman static ssize_t driver_unbind(struct device_driver *drv,
184151ef38fSGreg Kroah-Hartman 			     const char *buf, size_t count)
185151ef38fSGreg Kroah-Hartman {
1865901d014SGreg Kroah-Hartman 	struct bus_type *bus = bus_get(drv->bus);
187151ef38fSGreg Kroah-Hartman 	struct device *dev;
188151ef38fSGreg Kroah-Hartman 	int err = -ENODEV;
189151ef38fSGreg Kroah-Hartman 
190151ef38fSGreg Kroah-Hartman 	dev = bus_find_device(bus, NULL, (void *)buf, driver_helper);
1912b08c8d0SAlan Stern 	if (dev && dev->driver == drv) {
192bf74ad5bSAlan Stern 		if (dev->parent)	/* Needed for USB */
193bf74ad5bSAlan Stern 			down(&dev->parent->sem);
194151ef38fSGreg Kroah-Hartman 		device_release_driver(dev);
195bf74ad5bSAlan Stern 		if (dev->parent)
196bf74ad5bSAlan Stern 			up(&dev->parent->sem);
197151ef38fSGreg Kroah-Hartman 		err = count;
198151ef38fSGreg Kroah-Hartman 	}
1992b08c8d0SAlan Stern 	put_device(dev);
200fc1ede58SGreg Kroah-Hartman 	bus_put(bus);
201151ef38fSGreg Kroah-Hartman 	return err;
202151ef38fSGreg Kroah-Hartman }
203151ef38fSGreg Kroah-Hartman static DRIVER_ATTR(unbind, S_IWUSR, NULL, driver_unbind);
204151ef38fSGreg Kroah-Hartman 
205afdce75fSGreg Kroah-Hartman /*
206afdce75fSGreg Kroah-Hartman  * Manually attach a device to a driver.
207afdce75fSGreg Kroah-Hartman  * Note: the driver must want to bind to the device,
208afdce75fSGreg Kroah-Hartman  * it is not possible to override the driver's id table.
209afdce75fSGreg Kroah-Hartman  */
210afdce75fSGreg Kroah-Hartman static ssize_t driver_bind(struct device_driver *drv,
211afdce75fSGreg Kroah-Hartman 			   const char *buf, size_t count)
212afdce75fSGreg Kroah-Hartman {
2135901d014SGreg Kroah-Hartman 	struct bus_type *bus = bus_get(drv->bus);
214afdce75fSGreg Kroah-Hartman 	struct device *dev;
215afdce75fSGreg Kroah-Hartman 	int err = -ENODEV;
216afdce75fSGreg Kroah-Hartman 
217afdce75fSGreg Kroah-Hartman 	dev = bus_find_device(bus, NULL, (void *)buf, driver_helper);
2182b08c8d0SAlan Stern 	if (dev && dev->driver == NULL) {
219bf74ad5bSAlan Stern 		if (dev->parent)	/* Needed for USB */
220bf74ad5bSAlan Stern 			down(&dev->parent->sem);
221afdce75fSGreg Kroah-Hartman 		down(&dev->sem);
222afdce75fSGreg Kroah-Hartman 		err = driver_probe_device(drv, dev);
223afdce75fSGreg Kroah-Hartman 		up(&dev->sem);
224bf74ad5bSAlan Stern 		if (dev->parent)
225bf74ad5bSAlan Stern 			up(&dev->parent->sem);
22637225401SRyan Wilson 
22737225401SRyan Wilson 		if (err > 0) 		/* success */
22837225401SRyan Wilson 			err = count;
22937225401SRyan Wilson 		else if (err == 0)	/* driver didn't accept device */
23037225401SRyan Wilson 			err = -ENODEV;
231afdce75fSGreg Kroah-Hartman 	}
2322b08c8d0SAlan Stern 	put_device(dev);
233fc1ede58SGreg Kroah-Hartman 	bus_put(bus);
234afdce75fSGreg Kroah-Hartman 	return err;
235afdce75fSGreg Kroah-Hartman }
236afdce75fSGreg Kroah-Hartman static DRIVER_ATTR(bind, S_IWUSR, NULL, driver_bind);
237afdce75fSGreg Kroah-Hartman 
238b8c5cec2SKay Sievers static ssize_t show_drivers_autoprobe(struct bus_type *bus, char *buf)
239b8c5cec2SKay Sievers {
240b8c5cec2SKay Sievers 	return sprintf(buf, "%d\n", bus->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')
247b8c5cec2SKay Sievers 		bus->drivers_autoprobe = 0;
248b8c5cec2SKay Sievers 	else
249b8c5cec2SKay Sievers 		bus->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;
257b8c5cec2SKay Sievers 
258b8c5cec2SKay Sievers 	dev = bus_find_device(bus, NULL, (void *)buf, driver_helper);
259b8c5cec2SKay Sievers 	if (!dev)
260b8c5cec2SKay Sievers 		return -ENODEV;
261b8c5cec2SKay Sievers 	if (bus_rescan_devices_helper(dev, NULL) != 0)
262b8c5cec2SKay Sievers 		return -EINVAL;
263b8c5cec2SKay Sievers 	return count;
264b8c5cec2SKay Sievers }
2652139bdd5SRussell King #endif
266151ef38fSGreg Kroah-Hartman 
267465c7a3aSmochel@digitalimplant.org static struct device * next_device(struct klist_iter * i)
268465c7a3aSmochel@digitalimplant.org {
269465c7a3aSmochel@digitalimplant.org 	struct klist_node * n = klist_next(i);
270465c7a3aSmochel@digitalimplant.org 	return n ? container_of(n, struct device, knode_bus) : NULL;
271465c7a3aSmochel@digitalimplant.org }
272465c7a3aSmochel@digitalimplant.org 
2731da177e4SLinus Torvalds /**
2741da177e4SLinus Torvalds  *	bus_for_each_dev - device iterator.
2751da177e4SLinus Torvalds  *	@bus:	bus type.
2761da177e4SLinus Torvalds  *	@start:	device to start iterating from.
2771da177e4SLinus Torvalds  *	@data:	data for the callback.
2781da177e4SLinus Torvalds  *	@fn:	function to be called for each device.
2791da177e4SLinus Torvalds  *
2801da177e4SLinus Torvalds  *	Iterate over @bus's list of devices, and call @fn for each,
2811da177e4SLinus Torvalds  *	passing it @data. If @start is not NULL, we use that device to
2821da177e4SLinus Torvalds  *	begin iterating from.
2831da177e4SLinus Torvalds  *
2841da177e4SLinus Torvalds  *	We check the return of @fn each time. If it returns anything
2851da177e4SLinus Torvalds  *	other than 0, we break out and return that value.
2861da177e4SLinus Torvalds  *
2871da177e4SLinus Torvalds  *	NOTE: The device that returns a non-zero value is not retained
2881da177e4SLinus Torvalds  *	in any way, nor is its refcount incremented. If the caller needs
2891da177e4SLinus Torvalds  *	to retain this data, it should do, and increment the reference
2901da177e4SLinus Torvalds  *	count in the supplied callback.
2911da177e4SLinus Torvalds  */
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds int bus_for_each_dev(struct bus_type * bus, struct device * start,
2941da177e4SLinus Torvalds 		     void * data, int (*fn)(struct device *, void *))
2951da177e4SLinus Torvalds {
296465c7a3aSmochel@digitalimplant.org 	struct klist_iter i;
297465c7a3aSmochel@digitalimplant.org 	struct device * dev;
298465c7a3aSmochel@digitalimplant.org 	int error = 0;
2991da177e4SLinus Torvalds 
300465c7a3aSmochel@digitalimplant.org 	if (!bus)
301465c7a3aSmochel@digitalimplant.org 		return -EINVAL;
302465c7a3aSmochel@digitalimplant.org 
303465c7a3aSmochel@digitalimplant.org 	klist_iter_init_node(&bus->klist_devices, &i,
304465c7a3aSmochel@digitalimplant.org 			     (start ? &start->knode_bus : NULL));
305465c7a3aSmochel@digitalimplant.org 	while ((dev = next_device(&i)) && !error)
306465c7a3aSmochel@digitalimplant.org 		error = fn(dev, data);
307465c7a3aSmochel@digitalimplant.org 	klist_iter_exit(&i);
308465c7a3aSmochel@digitalimplant.org 	return error;
3091da177e4SLinus Torvalds }
3101da177e4SLinus Torvalds 
3110edb5860SCornelia Huck /**
3120edb5860SCornelia Huck  * bus_find_device - device iterator for locating a particular device.
3130edb5860SCornelia Huck  * @bus: bus type
3140edb5860SCornelia Huck  * @start: Device to begin with
3150edb5860SCornelia Huck  * @data: Data to pass to match function
3160edb5860SCornelia Huck  * @match: Callback function to check device
3170edb5860SCornelia Huck  *
3180edb5860SCornelia Huck  * This is similar to the bus_for_each_dev() function above, but it
3190edb5860SCornelia Huck  * returns a reference to a device that is 'found' for later use, as
3200edb5860SCornelia Huck  * determined by the @match callback.
3210edb5860SCornelia Huck  *
3220edb5860SCornelia Huck  * The callback should return 0 if the device doesn't match and non-zero
3230edb5860SCornelia Huck  * if it does.  If the callback returns non-zero, this function will
3240edb5860SCornelia Huck  * return to the caller and not iterate over any more devices.
3250edb5860SCornelia Huck  */
3260edb5860SCornelia Huck struct device * bus_find_device(struct bus_type *bus,
3270edb5860SCornelia Huck 				struct device *start, void *data,
3280edb5860SCornelia Huck 				int (*match)(struct device *, void *))
3290edb5860SCornelia Huck {
3300edb5860SCornelia Huck 	struct klist_iter i;
3310edb5860SCornelia Huck 	struct device *dev;
3320edb5860SCornelia Huck 
3330edb5860SCornelia Huck 	if (!bus)
3340edb5860SCornelia Huck 		return NULL;
3350edb5860SCornelia Huck 
3360edb5860SCornelia Huck 	klist_iter_init_node(&bus->klist_devices, &i,
3370edb5860SCornelia Huck 			     (start ? &start->knode_bus : NULL));
3380edb5860SCornelia Huck 	while ((dev = next_device(&i)))
3390edb5860SCornelia Huck 		if (match(dev, data) && get_device(dev))
3400edb5860SCornelia Huck 			break;
3410edb5860SCornelia Huck 	klist_iter_exit(&i);
3420edb5860SCornelia Huck 	return dev;
3430edb5860SCornelia Huck }
34438fdac3cSmochel@digitalimplant.org 
34538fdac3cSmochel@digitalimplant.org 
34638fdac3cSmochel@digitalimplant.org static struct device_driver * next_driver(struct klist_iter * i)
34738fdac3cSmochel@digitalimplant.org {
34838fdac3cSmochel@digitalimplant.org 	struct klist_node * n = klist_next(i);
34938fdac3cSmochel@digitalimplant.org 	return n ? container_of(n, struct device_driver, knode_bus) : NULL;
35038fdac3cSmochel@digitalimplant.org }
35138fdac3cSmochel@digitalimplant.org 
3521da177e4SLinus Torvalds /**
3531da177e4SLinus Torvalds  *	bus_for_each_drv - driver iterator
3541da177e4SLinus Torvalds  *	@bus:	bus we're dealing with.
3551da177e4SLinus Torvalds  *	@start:	driver to start iterating on.
3561da177e4SLinus Torvalds  *	@data:	data to pass to the callback.
3571da177e4SLinus Torvalds  *	@fn:	function to call for each driver.
3581da177e4SLinus Torvalds  *
3591da177e4SLinus Torvalds  *	This is nearly identical to the device iterator above.
3601da177e4SLinus Torvalds  *	We iterate over each driver that belongs to @bus, and call
3611da177e4SLinus Torvalds  *	@fn for each. If @fn returns anything but 0, we break out
3621da177e4SLinus Torvalds  *	and return it. If @start is not NULL, we use it as the head
3631da177e4SLinus Torvalds  *	of the list.
3641da177e4SLinus Torvalds  *
3651da177e4SLinus Torvalds  *	NOTE: we don't return the driver that returns a non-zero
3661da177e4SLinus Torvalds  *	value, nor do we leave the reference count incremented for that
3671da177e4SLinus Torvalds  *	driver. If the caller needs to know that info, it must set it
3681da177e4SLinus Torvalds  *	in the callback. It must also be sure to increment the refcount
3691da177e4SLinus Torvalds  *	so it doesn't disappear before returning to the caller.
3701da177e4SLinus Torvalds  */
3711da177e4SLinus Torvalds 
3721da177e4SLinus Torvalds int bus_for_each_drv(struct bus_type * bus, struct device_driver * start,
3731da177e4SLinus Torvalds 		     void * data, int (*fn)(struct device_driver *, void *))
3741da177e4SLinus Torvalds {
37538fdac3cSmochel@digitalimplant.org 	struct klist_iter i;
37638fdac3cSmochel@digitalimplant.org 	struct device_driver * drv;
37738fdac3cSmochel@digitalimplant.org 	int error = 0;
3781da177e4SLinus Torvalds 
37938fdac3cSmochel@digitalimplant.org 	if (!bus)
38038fdac3cSmochel@digitalimplant.org 		return -EINVAL;
38138fdac3cSmochel@digitalimplant.org 
38238fdac3cSmochel@digitalimplant.org 	klist_iter_init_node(&bus->klist_drivers, &i,
38338fdac3cSmochel@digitalimplant.org 			     start ? &start->knode_bus : NULL);
38438fdac3cSmochel@digitalimplant.org 	while ((drv = next_driver(&i)) && !error)
38538fdac3cSmochel@digitalimplant.org 		error = fn(drv, data);
38638fdac3cSmochel@digitalimplant.org 	klist_iter_exit(&i);
38738fdac3cSmochel@digitalimplant.org 	return error;
3881da177e4SLinus Torvalds }
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds static int device_add_attrs(struct bus_type *bus, struct device *dev)
3911da177e4SLinus Torvalds {
3921da177e4SLinus Torvalds 	int error = 0;
3931da177e4SLinus Torvalds 	int i;
3941da177e4SLinus Torvalds 
3954aca67e5SAndrew Morton 	if (!bus->dev_attrs)
3964aca67e5SAndrew Morton 		return 0;
3974aca67e5SAndrew Morton 
3981da177e4SLinus Torvalds 	for (i = 0; attr_name(bus->dev_attrs[i]); i++) {
3991da177e4SLinus Torvalds 		error = device_create_file(dev,&bus->dev_attrs[i]);
4004aca67e5SAndrew Morton 		if (error) {
4011da177e4SLinus Torvalds 			while (--i >= 0)
4021da177e4SLinus Torvalds 				device_remove_file(dev, &bus->dev_attrs[i]);
4034aca67e5SAndrew Morton 			break;
4041da177e4SLinus Torvalds 		}
4054aca67e5SAndrew Morton 	}
4064aca67e5SAndrew Morton 	return error;
4074aca67e5SAndrew Morton }
4081da177e4SLinus Torvalds 
4091da177e4SLinus Torvalds static void device_remove_attrs(struct bus_type * bus, struct device * dev)
4101da177e4SLinus Torvalds {
4111da177e4SLinus Torvalds 	int i;
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds 	if (bus->dev_attrs) {
4141da177e4SLinus Torvalds 		for (i = 0; attr_name(bus->dev_attrs[i]); i++)
4151da177e4SLinus Torvalds 			device_remove_file(dev,&bus->dev_attrs[i]);
4161da177e4SLinus Torvalds 	}
4171da177e4SLinus Torvalds }
4181da177e4SLinus Torvalds 
419b9cafc7dSKay Sievers #ifdef CONFIG_SYSFS_DEPRECATED
420b9cafc7dSKay Sievers static int make_deprecated_bus_links(struct device *dev)
421b9cafc7dSKay Sievers {
422b9cafc7dSKay Sievers 	return sysfs_create_link(&dev->kobj,
423823bccfcSGreg Kroah-Hartman 				 &dev->bus->subsys.kobj, "bus");
424b9cafc7dSKay Sievers }
425b9cafc7dSKay Sievers 
426b9cafc7dSKay Sievers static void remove_deprecated_bus_links(struct device *dev)
427b9cafc7dSKay Sievers {
428b9cafc7dSKay Sievers 	sysfs_remove_link(&dev->kobj, "bus");
429b9cafc7dSKay Sievers }
430b9cafc7dSKay Sievers #else
431b9cafc7dSKay Sievers static inline int make_deprecated_bus_links(struct device *dev) { return 0; }
432b9cafc7dSKay Sievers static inline void remove_deprecated_bus_links(struct device *dev) { }
433b9cafc7dSKay Sievers #endif
4341da177e4SLinus Torvalds 
4351da177e4SLinus Torvalds /**
4361da177e4SLinus Torvalds  *	bus_add_device - add device to bus
4371da177e4SLinus Torvalds  *	@dev:	device being added
4381da177e4SLinus Torvalds  *
4391da177e4SLinus Torvalds  *	- Add the device to its bus's list of devices.
44053877d06SKay Sievers  *	- Create link to device's bus.
4411da177e4SLinus Torvalds  */
4421da177e4SLinus Torvalds int bus_add_device(struct device * dev)
4431da177e4SLinus Torvalds {
4445901d014SGreg Kroah-Hartman 	struct bus_type * bus = bus_get(dev->bus);
4451da177e4SLinus Torvalds 	int error = 0;
4461da177e4SLinus Torvalds 
4471da177e4SLinus Torvalds 	if (bus) {
4481da177e4SLinus Torvalds 		pr_debug("bus %s: add device %s\n", bus->name, dev->bus_id);
449ca2b94baSHannes Reinecke 		error = device_add_attrs(bus, dev);
450f86db396SAndrew Morton 		if (error)
451513e7337SCornelia Huck 			goto out_put;
452f86db396SAndrew Morton 		error = sysfs_create_link(&bus->devices.kobj,
453f86db396SAndrew Morton 						&dev->kobj, dev->bus_id);
454f86db396SAndrew Morton 		if (error)
455513e7337SCornelia Huck 			goto out_id;
456f86db396SAndrew Morton 		error = sysfs_create_link(&dev->kobj,
457823bccfcSGreg Kroah-Hartman 				&dev->bus->subsys.kobj, "subsystem");
458f86db396SAndrew Morton 		if (error)
459513e7337SCornelia Huck 			goto out_subsys;
460b9cafc7dSKay Sievers 		error = make_deprecated_bus_links(dev);
461513e7337SCornelia Huck 		if (error)
462513e7337SCornelia Huck 			goto out_deprecated;
4631da177e4SLinus Torvalds 	}
464513e7337SCornelia Huck 	return 0;
465513e7337SCornelia Huck 
466513e7337SCornelia Huck out_deprecated:
467513e7337SCornelia Huck 	sysfs_remove_link(&dev->kobj, "subsystem");
468513e7337SCornelia Huck out_subsys:
469513e7337SCornelia Huck 	sysfs_remove_link(&bus->devices.kobj, dev->bus_id);
470513e7337SCornelia Huck out_id:
471513e7337SCornelia Huck 	device_remove_attrs(bus, dev);
472513e7337SCornelia Huck out_put:
473fc1ede58SGreg Kroah-Hartman 	bus_put(dev->bus);
4741da177e4SLinus Torvalds 	return error;
4751da177e4SLinus Torvalds }
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds /**
47853877d06SKay Sievers  *	bus_attach_device - add device to bus
47953877d06SKay Sievers  *	@dev:	device tried to attach to a driver
48053877d06SKay Sievers  *
481f2eaae19SAlan Stern  *	- Add device to bus's list of devices.
48253877d06SKay Sievers  *	- Try to attach to driver.
48353877d06SKay Sievers  */
484c6a46696SCornelia Huck void bus_attach_device(struct device * dev)
48553877d06SKay Sievers {
48653877d06SKay Sievers 	struct bus_type *bus = dev->bus;
487f86db396SAndrew Morton 	int ret = 0;
48853877d06SKay Sievers 
48953877d06SKay Sievers 	if (bus) {
490f2eaae19SAlan Stern 		dev->is_registered = 1;
491b8c5cec2SKay Sievers 		if (bus->drivers_autoprobe)
492f86db396SAndrew Morton 			ret = device_attach(dev);
493c6a46696SCornelia Huck 		WARN_ON(ret < 0);
494c6a46696SCornelia Huck 		if (ret >= 0)
49553877d06SKay Sievers 			klist_add_tail(&dev->knode_bus, &bus->klist_devices);
496c6a46696SCornelia Huck 		else
497f2eaae19SAlan Stern 			dev->is_registered = 0;
49853877d06SKay Sievers 	}
499f86db396SAndrew Morton }
50053877d06SKay Sievers 
50153877d06SKay Sievers /**
5021da177e4SLinus Torvalds  *	bus_remove_device - remove device from bus
5031da177e4SLinus Torvalds  *	@dev:	device to be removed
5041da177e4SLinus Torvalds  *
5051da177e4SLinus Torvalds  *	- Remove symlink from bus's directory.
5061da177e4SLinus Torvalds  *	- Delete device from bus's list.
5071da177e4SLinus Torvalds  *	- Detach from its driver.
5081da177e4SLinus Torvalds  *	- Drop reference taken in bus_add_device().
5091da177e4SLinus Torvalds  */
5101da177e4SLinus Torvalds void bus_remove_device(struct device * dev)
5111da177e4SLinus Torvalds {
5121da177e4SLinus Torvalds 	if (dev->bus) {
513b9d9c82bSKay Sievers 		sysfs_remove_link(&dev->kobj, "subsystem");
514b9cafc7dSKay Sievers 		remove_deprecated_bus_links(dev);
5151da177e4SLinus Torvalds 		sysfs_remove_link(&dev->bus->devices.kobj, dev->bus_id);
5161da177e4SLinus Torvalds 		device_remove_attrs(dev->bus, dev);
517f70fa629SAlan Stern 		if (dev->is_registered) {
518f2eaae19SAlan Stern 			dev->is_registered = 0;
519f2eaae19SAlan Stern 			klist_del(&dev->knode_bus);
520f70fa629SAlan Stern 		}
5211da177e4SLinus Torvalds 		pr_debug("bus %s: remove device %s\n", dev->bus->name, dev->bus_id);
5221da177e4SLinus Torvalds 		device_release_driver(dev);
523fc1ede58SGreg Kroah-Hartman 		bus_put(dev->bus);
5241da177e4SLinus Torvalds 	}
5251da177e4SLinus Torvalds }
5261da177e4SLinus Torvalds 
5271da177e4SLinus Torvalds static int driver_add_attrs(struct bus_type * bus, struct device_driver * drv)
5281da177e4SLinus Torvalds {
5291da177e4SLinus Torvalds 	int error = 0;
5301da177e4SLinus Torvalds 	int i;
5311da177e4SLinus Torvalds 
5321da177e4SLinus Torvalds 	if (bus->drv_attrs) {
5331da177e4SLinus Torvalds 		for (i = 0; attr_name(bus->drv_attrs[i]); i++) {
5341da177e4SLinus Torvalds 			error = driver_create_file(drv, &bus->drv_attrs[i]);
5351da177e4SLinus Torvalds 			if (error)
5361da177e4SLinus Torvalds 				goto Err;
5371da177e4SLinus Torvalds 		}
5381da177e4SLinus Torvalds 	}
5391da177e4SLinus Torvalds  Done:
5401da177e4SLinus Torvalds 	return error;
5411da177e4SLinus Torvalds  Err:
5421da177e4SLinus Torvalds 	while (--i >= 0)
5431da177e4SLinus Torvalds 		driver_remove_file(drv, &bus->drv_attrs[i]);
5441da177e4SLinus Torvalds 	goto Done;
5451da177e4SLinus Torvalds }
5461da177e4SLinus Torvalds 
5471da177e4SLinus Torvalds 
5481da177e4SLinus Torvalds static void driver_remove_attrs(struct bus_type * bus, struct device_driver * drv)
5491da177e4SLinus Torvalds {
5501da177e4SLinus Torvalds 	int i;
5511da177e4SLinus Torvalds 
5521da177e4SLinus Torvalds 	if (bus->drv_attrs) {
5531da177e4SLinus Torvalds 		for (i = 0; attr_name(bus->drv_attrs[i]); i++)
5541da177e4SLinus Torvalds 			driver_remove_file(drv, &bus->drv_attrs[i]);
5551da177e4SLinus Torvalds 	}
5561da177e4SLinus Torvalds }
5571da177e4SLinus Torvalds 
558874c6241SGreg Kroah-Hartman #ifdef CONFIG_HOTPLUG
559874c6241SGreg Kroah-Hartman /*
560874c6241SGreg Kroah-Hartman  * Thanks to drivers making their tables __devinit, we can't allow manual
561874c6241SGreg Kroah-Hartman  * bind and unbind from userspace unless CONFIG_HOTPLUG is enabled.
562874c6241SGreg Kroah-Hartman  */
563f86db396SAndrew Morton static int __must_check add_bind_files(struct device_driver *drv)
564874c6241SGreg Kroah-Hartman {
565f86db396SAndrew Morton 	int ret;
566f86db396SAndrew Morton 
567f86db396SAndrew Morton 	ret = driver_create_file(drv, &driver_attr_unbind);
568f86db396SAndrew Morton 	if (ret == 0) {
569f86db396SAndrew Morton 		ret = driver_create_file(drv, &driver_attr_bind);
570f86db396SAndrew Morton 		if (ret)
571f86db396SAndrew Morton 			driver_remove_file(drv, &driver_attr_unbind);
572f86db396SAndrew Morton 	}
573f86db396SAndrew Morton 	return ret;
574874c6241SGreg Kroah-Hartman }
575874c6241SGreg Kroah-Hartman 
576874c6241SGreg Kroah-Hartman static void remove_bind_files(struct device_driver *drv)
577874c6241SGreg Kroah-Hartman {
578874c6241SGreg Kroah-Hartman 	driver_remove_file(drv, &driver_attr_bind);
579874c6241SGreg Kroah-Hartman 	driver_remove_file(drv, &driver_attr_unbind);
580874c6241SGreg Kroah-Hartman }
581b8c5cec2SKay Sievers 
5828380770cSKay Sievers static BUS_ATTR(drivers_probe, S_IWUSR, NULL, store_drivers_probe);
5838380770cSKay Sievers static BUS_ATTR(drivers_autoprobe, S_IWUSR | S_IRUGO,
5848380770cSKay Sievers 		show_drivers_autoprobe, store_drivers_autoprobe);
5858380770cSKay Sievers 
586b8c5cec2SKay Sievers static int add_probe_files(struct bus_type *bus)
587b8c5cec2SKay Sievers {
588b8c5cec2SKay Sievers 	int retval;
589b8c5cec2SKay Sievers 
5908380770cSKay Sievers 	retval = bus_create_file(bus, &bus_attr_drivers_probe);
591b8c5cec2SKay Sievers 	if (retval)
592b8c5cec2SKay Sievers 		goto out;
593b8c5cec2SKay Sievers 
5948380770cSKay Sievers 	retval = bus_create_file(bus, &bus_attr_drivers_autoprobe);
595b8c5cec2SKay Sievers 	if (retval)
5968380770cSKay Sievers 		bus_remove_file(bus, &bus_attr_drivers_probe);
597b8c5cec2SKay Sievers out:
598b8c5cec2SKay Sievers 	return retval;
599b8c5cec2SKay Sievers }
600b8c5cec2SKay Sievers 
601b8c5cec2SKay Sievers static void remove_probe_files(struct bus_type *bus)
602b8c5cec2SKay Sievers {
6038380770cSKay Sievers 	bus_remove_file(bus, &bus_attr_drivers_autoprobe);
6048380770cSKay Sievers 	bus_remove_file(bus, &bus_attr_drivers_probe);
605b8c5cec2SKay Sievers }
606874c6241SGreg Kroah-Hartman #else
60735acfdd7SYoichi Yuasa static inline int add_bind_files(struct device_driver *drv) { return 0; }
608874c6241SGreg Kroah-Hartman static inline void remove_bind_files(struct device_driver *drv) {}
609b8c5cec2SKay Sievers static inline int add_probe_files(struct bus_type *bus) { return 0; }
610b8c5cec2SKay Sievers static inline void remove_probe_files(struct bus_type *bus) {}
611874c6241SGreg Kroah-Hartman #endif
6121da177e4SLinus Torvalds 
6137ac1cf4aSKay Sievers static ssize_t driver_uevent_store(struct device_driver *drv,
6147ac1cf4aSKay Sievers 				   const char *buf, size_t count)
6157ac1cf4aSKay Sievers {
6167ac1cf4aSKay Sievers 	enum kobject_action action;
6177ac1cf4aSKay Sievers 
6187ac1cf4aSKay Sievers 	if (kobject_action_type(buf, count, &action) == 0)
6197ac1cf4aSKay Sievers 		kobject_uevent(&drv->kobj, action);
6207ac1cf4aSKay Sievers 	return count;
6217ac1cf4aSKay Sievers }
6227ac1cf4aSKay Sievers static DRIVER_ATTR(uevent, S_IWUSR, NULL, driver_uevent_store);
6237ac1cf4aSKay Sievers 
6241da177e4SLinus Torvalds /**
6251da177e4SLinus Torvalds  *	bus_add_driver - Add a driver to the bus.
6261da177e4SLinus Torvalds  *	@drv:	driver.
6271da177e4SLinus Torvalds  *
6281da177e4SLinus Torvalds  */
6291da177e4SLinus Torvalds int bus_add_driver(struct device_driver *drv)
6301da177e4SLinus Torvalds {
6315901d014SGreg Kroah-Hartman 	struct bus_type * bus = bus_get(drv->bus);
6321da177e4SLinus Torvalds 	int error = 0;
6331da177e4SLinus Torvalds 
634d9fd4d3bSJeff Garzik 	if (!bus)
6354f6e1945SGreg Kroah-Hartman 		return -EINVAL;
636d9fd4d3bSJeff Garzik 
6371da177e4SLinus Torvalds 	pr_debug("bus %s: add driver %s\n", bus->name, drv->name);
6381da177e4SLinus Torvalds 	error = kobject_set_name(&drv->kobj, "%s", drv->name);
639f86db396SAndrew Morton 	if (error)
640f86db396SAndrew Morton 		goto out_put_bus;
6411da177e4SLinus Torvalds 	drv->kobj.kset = &bus->drivers;
642*3514facaSGreg Kroah-Hartman 	drv->kobj.ktype = &driver_ktype;
643dc0afa83SCornelia Huck 	error = kobject_register(&drv->kobj);
644dc0afa83SCornelia Huck 	if (error)
645f86db396SAndrew Morton 		goto out_put_bus;
6461da177e4SLinus Torvalds 
647b8c5cec2SKay Sievers 	if (drv->bus->drivers_autoprobe) {
648f86db396SAndrew Morton 		error = driver_attach(drv);
649f86db396SAndrew Morton 		if (error)
650f86db396SAndrew Morton 			goto out_unregister;
651b8c5cec2SKay Sievers 	}
652d856f1e3SJames Bottomley 	klist_add_tail(&drv->knode_bus, &bus->klist_drivers);
6531da177e4SLinus Torvalds 	module_add_driver(drv->owner, drv);
6541da177e4SLinus Torvalds 
6557ac1cf4aSKay Sievers 	error = driver_create_file(drv, &driver_attr_uevent);
6567ac1cf4aSKay Sievers 	if (error) {
6577ac1cf4aSKay Sievers 		printk(KERN_ERR "%s: uevent attr (%s) failed\n",
6587ac1cf4aSKay Sievers 			__FUNCTION__, drv->name);
6597ac1cf4aSKay Sievers 	}
660f86db396SAndrew Morton 	error = driver_add_attrs(bus, drv);
661f86db396SAndrew Morton 	if (error) {
662f86db396SAndrew Morton 		/* How the hell do we get out of this pickle? Give up */
663f86db396SAndrew Morton 		printk(KERN_ERR "%s: driver_add_attrs(%s) failed\n",
664f86db396SAndrew Morton 			__FUNCTION__, drv->name);
665f86db396SAndrew Morton 	}
666f86db396SAndrew Morton 	error = add_bind_files(drv);
667f86db396SAndrew Morton 	if (error) {
668f86db396SAndrew Morton 		/* Ditto */
669f86db396SAndrew Morton 		printk(KERN_ERR "%s: add_bind_files(%s) failed\n",
670f86db396SAndrew Morton 			__FUNCTION__, drv->name);
671f86db396SAndrew Morton 	}
672d9fd4d3bSJeff Garzik 
6731da177e4SLinus Torvalds 	return error;
674f86db396SAndrew Morton out_unregister:
675f86db396SAndrew Morton 	kobject_unregister(&drv->kobj);
676f86db396SAndrew Morton out_put_bus:
677fc1ede58SGreg Kroah-Hartman 	bus_put(bus);
678f86db396SAndrew Morton 	return error;
6791da177e4SLinus Torvalds }
6801da177e4SLinus Torvalds 
6811da177e4SLinus Torvalds /**
6821da177e4SLinus Torvalds  *	bus_remove_driver - delete driver from bus's knowledge.
6831da177e4SLinus Torvalds  *	@drv:	driver.
6841da177e4SLinus Torvalds  *
6851da177e4SLinus Torvalds  *	Detach the driver from the devices it controls, and remove
6861da177e4SLinus Torvalds  *	it from its bus's list of drivers. Finally, we drop the reference
6871da177e4SLinus Torvalds  *	to the bus we took in bus_add_driver().
6881da177e4SLinus Torvalds  */
6891da177e4SLinus Torvalds 
6901da177e4SLinus Torvalds void bus_remove_driver(struct device_driver * drv)
6911da177e4SLinus Torvalds {
692d9fd4d3bSJeff Garzik 	if (!drv->bus)
693d9fd4d3bSJeff Garzik 		return;
694d9fd4d3bSJeff Garzik 
695874c6241SGreg Kroah-Hartman 	remove_bind_files(drv);
6961da177e4SLinus Torvalds 	driver_remove_attrs(drv->bus, drv);
6977ac1cf4aSKay Sievers 	driver_remove_file(drv, &driver_attr_uevent);
69838fdac3cSmochel@digitalimplant.org 	klist_remove(&drv->knode_bus);
6991da177e4SLinus Torvalds 	pr_debug("bus %s: remove driver %s\n", drv->bus->name, drv->name);
7001da177e4SLinus Torvalds 	driver_detach(drv);
7011da177e4SLinus Torvalds 	module_remove_driver(drv);
7021da177e4SLinus Torvalds 	kobject_unregister(&drv->kobj);
703fc1ede58SGreg Kroah-Hartman 	bus_put(drv->bus);
7041da177e4SLinus Torvalds }
7051da177e4SLinus Torvalds 
7061da177e4SLinus Torvalds 
7071da177e4SLinus Torvalds /* Helper for bus_rescan_devices's iter */
708f86db396SAndrew Morton static int __must_check bus_rescan_devices_helper(struct device *dev,
709f86db396SAndrew Morton 						void *data)
7101da177e4SLinus Torvalds {
711f86db396SAndrew Morton 	int ret = 0;
712f86db396SAndrew Morton 
713bf74ad5bSAlan Stern 	if (!dev->driver) {
714bf74ad5bSAlan Stern 		if (dev->parent)	/* Needed for USB */
715bf74ad5bSAlan Stern 			down(&dev->parent->sem);
716f86db396SAndrew Morton 		ret = device_attach(dev);
717bf74ad5bSAlan Stern 		if (dev->parent)
718bf74ad5bSAlan Stern 			up(&dev->parent->sem);
719bf74ad5bSAlan Stern 	}
720f86db396SAndrew Morton 	return ret < 0 ? ret : 0;
7211da177e4SLinus Torvalds }
7221da177e4SLinus Torvalds 
7231da177e4SLinus Torvalds /**
7241da177e4SLinus Torvalds  * bus_rescan_devices - rescan devices on the bus for possible drivers
7251da177e4SLinus Torvalds  * @bus: the bus to scan.
7261da177e4SLinus Torvalds  *
7271da177e4SLinus Torvalds  * This function will look for devices on the bus with no driver
72823d3d602SGreg Kroah-Hartman  * attached and rescan it against existing drivers to see if it matches
72923d3d602SGreg Kroah-Hartman  * any by calling device_attach() for the unbound devices.
7301da177e4SLinus Torvalds  */
731f86db396SAndrew Morton int bus_rescan_devices(struct bus_type * bus)
7321da177e4SLinus Torvalds {
733f86db396SAndrew Morton 	return bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper);
7341da177e4SLinus Torvalds }
7351da177e4SLinus Torvalds 
736e935d5daSMoore, Eric /**
737e935d5daSMoore, Eric  * device_reprobe - remove driver for a device and probe for a new driver
738e935d5daSMoore, Eric  * @dev: the device to reprobe
739e935d5daSMoore, Eric  *
740e935d5daSMoore, Eric  * This function detaches the attached driver (if any) for the given
741e935d5daSMoore, Eric  * device and restarts the driver probing process.  It is intended
742e935d5daSMoore, Eric  * to use if probing criteria changed during a devices lifetime and
743e935d5daSMoore, Eric  * driver attachment should change accordingly.
744e935d5daSMoore, Eric  */
745f86db396SAndrew Morton int device_reprobe(struct device *dev)
746e935d5daSMoore, Eric {
747e935d5daSMoore, Eric 	if (dev->driver) {
748e935d5daSMoore, Eric 		if (dev->parent)        /* Needed for USB */
749e935d5daSMoore, Eric 			down(&dev->parent->sem);
750e935d5daSMoore, Eric 		device_release_driver(dev);
751e935d5daSMoore, Eric 		if (dev->parent)
752e935d5daSMoore, Eric 			up(&dev->parent->sem);
753e935d5daSMoore, Eric 	}
754f86db396SAndrew Morton 	return bus_rescan_devices_helper(dev, NULL);
755e935d5daSMoore, Eric }
756e935d5daSMoore, Eric EXPORT_SYMBOL_GPL(device_reprobe);
7571da177e4SLinus Torvalds 
7581da177e4SLinus Torvalds /**
7591da177e4SLinus Torvalds  *	find_bus - locate bus by name.
7601da177e4SLinus Torvalds  *	@name:	name of bus.
7611da177e4SLinus Torvalds  *
7621da177e4SLinus Torvalds  *	Call kset_find_obj() to iterate over list of buses to
7631da177e4SLinus Torvalds  *	find a bus by name. Return bus if found.
7641da177e4SLinus Torvalds  *
7651da177e4SLinus Torvalds  *	Note that kset_find_obj increments bus' reference count.
7661da177e4SLinus Torvalds  */
7677e4ef085SAdrian Bunk #if 0
7681da177e4SLinus Torvalds struct bus_type * find_bus(char * name)
7691da177e4SLinus Torvalds {
7701da177e4SLinus Torvalds 	struct kobject * k = kset_find_obj(&bus_subsys.kset, name);
7711da177e4SLinus Torvalds 	return k ? to_bus(k) : NULL;
7721da177e4SLinus Torvalds }
7737e4ef085SAdrian Bunk #endif  /*  0  */
7741da177e4SLinus Torvalds 
7751da177e4SLinus Torvalds 
7761da177e4SLinus Torvalds /**
7771da177e4SLinus Torvalds  *	bus_add_attrs - Add default attributes for this bus.
7781da177e4SLinus Torvalds  *	@bus:	Bus that has just been registered.
7791da177e4SLinus Torvalds  */
7801da177e4SLinus Torvalds 
7811da177e4SLinus Torvalds static int bus_add_attrs(struct bus_type * bus)
7821da177e4SLinus Torvalds {
7831da177e4SLinus Torvalds 	int error = 0;
7841da177e4SLinus Torvalds 	int i;
7851da177e4SLinus Torvalds 
7861da177e4SLinus Torvalds 	if (bus->bus_attrs) {
7871da177e4SLinus Torvalds 		for (i = 0; attr_name(bus->bus_attrs[i]); i++) {
788dc0afa83SCornelia Huck 			error = bus_create_file(bus,&bus->bus_attrs[i]);
789dc0afa83SCornelia Huck 			if (error)
7901da177e4SLinus Torvalds 				goto Err;
7911da177e4SLinus Torvalds 		}
7921da177e4SLinus Torvalds 	}
7931da177e4SLinus Torvalds  Done:
7941da177e4SLinus Torvalds 	return error;
7951da177e4SLinus Torvalds  Err:
7961da177e4SLinus Torvalds 	while (--i >= 0)
7971da177e4SLinus Torvalds 		bus_remove_file(bus,&bus->bus_attrs[i]);
7981da177e4SLinus Torvalds 	goto Done;
7991da177e4SLinus Torvalds }
8001da177e4SLinus Torvalds 
8011da177e4SLinus Torvalds static void bus_remove_attrs(struct bus_type * bus)
8021da177e4SLinus Torvalds {
8031da177e4SLinus Torvalds 	int i;
8041da177e4SLinus Torvalds 
8051da177e4SLinus Torvalds 	if (bus->bus_attrs) {
8061da177e4SLinus Torvalds 		for (i = 0; attr_name(bus->bus_attrs[i]); i++)
8071da177e4SLinus Torvalds 			bus_remove_file(bus,&bus->bus_attrs[i]);
8081da177e4SLinus Torvalds 	}
8091da177e4SLinus Torvalds }
8101da177e4SLinus Torvalds 
81134bb61f9SJames Bottomley static void klist_devices_get(struct klist_node *n)
81234bb61f9SJames Bottomley {
81334bb61f9SJames Bottomley 	struct device *dev = container_of(n, struct device, knode_bus);
81434bb61f9SJames Bottomley 
81534bb61f9SJames Bottomley 	get_device(dev);
81634bb61f9SJames Bottomley }
81734bb61f9SJames Bottomley 
81834bb61f9SJames Bottomley static void klist_devices_put(struct klist_node *n)
81934bb61f9SJames Bottomley {
82034bb61f9SJames Bottomley 	struct device *dev = container_of(n, struct device, knode_bus);
82134bb61f9SJames Bottomley 
82234bb61f9SJames Bottomley 	put_device(dev);
82334bb61f9SJames Bottomley }
82434bb61f9SJames Bottomley 
8257ac1cf4aSKay Sievers static ssize_t bus_uevent_store(struct bus_type *bus,
8267ac1cf4aSKay Sievers 				const char *buf, size_t count)
8277ac1cf4aSKay Sievers {
8287ac1cf4aSKay Sievers 	enum kobject_action action;
8297ac1cf4aSKay Sievers 
8307ac1cf4aSKay Sievers 	if (kobject_action_type(buf, count, &action) == 0)
8317ac1cf4aSKay Sievers 		kobject_uevent(&bus->subsys.kobj, action);
8327ac1cf4aSKay Sievers 	return count;
8337ac1cf4aSKay Sievers }
8347ac1cf4aSKay Sievers static BUS_ATTR(uevent, S_IWUSR, NULL, bus_uevent_store);
8357ac1cf4aSKay Sievers 
8361da177e4SLinus Torvalds /**
8371da177e4SLinus Torvalds  *	bus_register - register a bus with the system.
8381da177e4SLinus Torvalds  *	@bus:	bus.
8391da177e4SLinus Torvalds  *
8401da177e4SLinus Torvalds  *	Once we have that, we registered the bus with the kobject
8411da177e4SLinus Torvalds  *	infrastructure, then register the children subsystems it has:
8421da177e4SLinus Torvalds  *	the devices and drivers that belong to the bus.
8431da177e4SLinus Torvalds  */
8441da177e4SLinus Torvalds int bus_register(struct bus_type * bus)
8451da177e4SLinus Torvalds {
8461da177e4SLinus Torvalds 	int retval;
8471da177e4SLinus Torvalds 
848116af378SBenjamin Herrenschmidt 	BLOCKING_INIT_NOTIFIER_HEAD(&bus->bus_notifier);
849116af378SBenjamin Herrenschmidt 
850823bccfcSGreg Kroah-Hartman 	retval = kobject_set_name(&bus->subsys.kobj, "%s", bus->name);
8511da177e4SLinus Torvalds 	if (retval)
8521da177e4SLinus Torvalds 		goto out;
8531da177e4SLinus Torvalds 
854d6b05b84SGreg Kroah-Hartman 	bus->subsys.kobj.kset = &bus_subsys;
855*3514facaSGreg Kroah-Hartman 	bus->subsys.kobj.ktype = &bus_ktype;
856d6b05b84SGreg Kroah-Hartman 
8571da177e4SLinus Torvalds 	retval = subsystem_register(&bus->subsys);
8581da177e4SLinus Torvalds 	if (retval)
8591da177e4SLinus Torvalds 		goto out;
8601da177e4SLinus Torvalds 
8617ac1cf4aSKay Sievers 	retval = bus_create_file(bus, &bus_attr_uevent);
8627ac1cf4aSKay Sievers 	if (retval)
8637ac1cf4aSKay Sievers 		goto bus_uevent_fail;
8647ac1cf4aSKay Sievers 
8651da177e4SLinus Torvalds 	kobject_set_name(&bus->devices.kobj, "devices");
866823bccfcSGreg Kroah-Hartman 	bus->devices.kobj.parent = &bus->subsys.kobj;
8671da177e4SLinus Torvalds 	retval = kset_register(&bus->devices);
8681da177e4SLinus Torvalds 	if (retval)
8691da177e4SLinus Torvalds 		goto bus_devices_fail;
8701da177e4SLinus Torvalds 
8711da177e4SLinus Torvalds 	kobject_set_name(&bus->drivers.kobj, "drivers");
872823bccfcSGreg Kroah-Hartman 	bus->drivers.kobj.parent = &bus->subsys.kobj;
8731da177e4SLinus Torvalds 	retval = kset_register(&bus->drivers);
8741da177e4SLinus Torvalds 	if (retval)
8751da177e4SLinus Torvalds 		goto bus_drivers_fail;
876465c7a3aSmochel@digitalimplant.org 
87734bb61f9SJames Bottomley 	klist_init(&bus->klist_devices, klist_devices_get, klist_devices_put);
87881107bf5SAlan Stern 	klist_init(&bus->klist_drivers, NULL, NULL);
879b8c5cec2SKay Sievers 
880b8c5cec2SKay Sievers 	bus->drivers_autoprobe = 1;
881b8c5cec2SKay Sievers 	retval = add_probe_files(bus);
882b8c5cec2SKay Sievers 	if (retval)
883b8c5cec2SKay Sievers 		goto bus_probe_files_fail;
884b8c5cec2SKay Sievers 
8851bb6881aSCornelia Huck 	retval = bus_add_attrs(bus);
8861bb6881aSCornelia Huck 	if (retval)
8871bb6881aSCornelia Huck 		goto bus_attrs_fail;
8881da177e4SLinus Torvalds 
8891da177e4SLinus Torvalds 	pr_debug("bus type '%s' registered\n", bus->name);
8901da177e4SLinus Torvalds 	return 0;
8911da177e4SLinus Torvalds 
8921bb6881aSCornelia Huck bus_attrs_fail:
893b8c5cec2SKay Sievers 	remove_probe_files(bus);
894b8c5cec2SKay Sievers bus_probe_files_fail:
8951bb6881aSCornelia Huck 	kset_unregister(&bus->drivers);
8961da177e4SLinus Torvalds bus_drivers_fail:
8971da177e4SLinus Torvalds 	kset_unregister(&bus->devices);
8981da177e4SLinus Torvalds bus_devices_fail:
8997ac1cf4aSKay Sievers 	bus_remove_file(bus, &bus_attr_uevent);
9007ac1cf4aSKay Sievers bus_uevent_fail:
9011da177e4SLinus Torvalds 	subsystem_unregister(&bus->subsys);
9021da177e4SLinus Torvalds out:
9031da177e4SLinus Torvalds 	return retval;
9041da177e4SLinus Torvalds }
9051da177e4SLinus Torvalds 
9061da177e4SLinus Torvalds /**
9071da177e4SLinus Torvalds  *	bus_unregister - remove a bus from the system
9081da177e4SLinus Torvalds  *	@bus:	bus.
9091da177e4SLinus Torvalds  *
9101da177e4SLinus Torvalds  *	Unregister the child subsystems and the bus itself.
911fc1ede58SGreg Kroah-Hartman  *	Finally, we call bus_put() to release the refcount
9121da177e4SLinus Torvalds  */
9131da177e4SLinus Torvalds void bus_unregister(struct bus_type * bus)
9141da177e4SLinus Torvalds {
9151da177e4SLinus Torvalds 	pr_debug("bus %s: unregistering\n", bus->name);
9161da177e4SLinus Torvalds 	bus_remove_attrs(bus);
917b8c5cec2SKay Sievers 	remove_probe_files(bus);
9181da177e4SLinus Torvalds 	kset_unregister(&bus->drivers);
9191da177e4SLinus Torvalds 	kset_unregister(&bus->devices);
9207ac1cf4aSKay Sievers 	bus_remove_file(bus, &bus_attr_uevent);
9211da177e4SLinus Torvalds 	subsystem_unregister(&bus->subsys);
9221da177e4SLinus Torvalds }
9231da177e4SLinus Torvalds 
924116af378SBenjamin Herrenschmidt int bus_register_notifier(struct bus_type *bus, struct notifier_block *nb)
925116af378SBenjamin Herrenschmidt {
926116af378SBenjamin Herrenschmidt 	return blocking_notifier_chain_register(&bus->bus_notifier, nb);
927116af378SBenjamin Herrenschmidt }
928116af378SBenjamin Herrenschmidt EXPORT_SYMBOL_GPL(bus_register_notifier);
929116af378SBenjamin Herrenschmidt 
930116af378SBenjamin Herrenschmidt int bus_unregister_notifier(struct bus_type *bus, struct notifier_block *nb)
931116af378SBenjamin Herrenschmidt {
932116af378SBenjamin Herrenschmidt 	return blocking_notifier_chain_unregister(&bus->bus_notifier, nb);
933116af378SBenjamin Herrenschmidt }
934116af378SBenjamin Herrenschmidt EXPORT_SYMBOL_GPL(bus_unregister_notifier);
935116af378SBenjamin Herrenschmidt 
9361da177e4SLinus Torvalds int __init buses_init(void)
9371da177e4SLinus Torvalds {
9381da177e4SLinus Torvalds 	return subsystem_register(&bus_subsys);
9391da177e4SLinus Torvalds }
9401da177e4SLinus Torvalds 
9411da177e4SLinus Torvalds 
9421da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(bus_for_each_dev);
9430edb5860SCornelia Huck EXPORT_SYMBOL_GPL(bus_find_device);
9441da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(bus_for_each_drv);
9451da177e4SLinus Torvalds 
9461da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(bus_register);
9471da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(bus_unregister);
9481da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(bus_rescan_devices);
9491da177e4SLinus Torvalds 
9501da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(bus_create_file);
9511da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(bus_remove_file);
952