xref: /linux/drivers/base/bus.c (revision 4aca67e5f54bf6ee439b5bdbc77007a547ad5b43)
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)
201da177e4SLinus Torvalds #define to_bus(obj) container_of(obj, struct bus_type, subsys.kset.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 
301da177e4SLinus Torvalds static ssize_t
311da177e4SLinus Torvalds drv_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
321da177e4SLinus Torvalds {
331da177e4SLinus Torvalds 	struct driver_attribute * drv_attr = to_drv_attr(attr);
341da177e4SLinus Torvalds 	struct device_driver * drv = to_driver(kobj);
354a0c20bfSDmitry Torokhov 	ssize_t ret = -EIO;
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds 	if (drv_attr->show)
381da177e4SLinus Torvalds 		ret = drv_attr->show(drv, buf);
391da177e4SLinus Torvalds 	return ret;
401da177e4SLinus Torvalds }
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds static ssize_t
431da177e4SLinus Torvalds drv_attr_store(struct kobject * kobj, struct attribute * attr,
441da177e4SLinus Torvalds 	       const char * buf, size_t count)
451da177e4SLinus Torvalds {
461da177e4SLinus Torvalds 	struct driver_attribute * drv_attr = to_drv_attr(attr);
471da177e4SLinus Torvalds 	struct device_driver * drv = to_driver(kobj);
484a0c20bfSDmitry Torokhov 	ssize_t ret = -EIO;
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds 	if (drv_attr->store)
511da177e4SLinus Torvalds 		ret = drv_attr->store(drv, buf, count);
521da177e4SLinus Torvalds 	return ret;
531da177e4SLinus Torvalds }
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds static struct sysfs_ops driver_sysfs_ops = {
561da177e4SLinus Torvalds 	.show	= drv_attr_show,
571da177e4SLinus Torvalds 	.store	= drv_attr_store,
581da177e4SLinus Torvalds };
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds static void driver_release(struct kobject * kobj)
621da177e4SLinus Torvalds {
631da177e4SLinus Torvalds 	struct device_driver * drv = to_driver(kobj);
641da177e4SLinus Torvalds 	complete(&drv->unloaded);
651da177e4SLinus Torvalds }
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds static struct kobj_type ktype_driver = {
681da177e4SLinus Torvalds 	.sysfs_ops	= &driver_sysfs_ops,
691da177e4SLinus Torvalds 	.release	= driver_release,
701da177e4SLinus Torvalds };
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds /*
741da177e4SLinus Torvalds  * sysfs bindings for buses
751da177e4SLinus Torvalds  */
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds static ssize_t
791da177e4SLinus Torvalds bus_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
801da177e4SLinus Torvalds {
811da177e4SLinus Torvalds 	struct bus_attribute * bus_attr = to_bus_attr(attr);
821da177e4SLinus Torvalds 	struct bus_type * bus = to_bus(kobj);
831da177e4SLinus Torvalds 	ssize_t ret = 0;
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds 	if (bus_attr->show)
861da177e4SLinus Torvalds 		ret = bus_attr->show(bus, buf);
871da177e4SLinus Torvalds 	return ret;
881da177e4SLinus Torvalds }
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds static ssize_t
911da177e4SLinus Torvalds bus_attr_store(struct kobject * kobj, struct attribute * attr,
921da177e4SLinus Torvalds 	       const char * buf, size_t count)
931da177e4SLinus Torvalds {
941da177e4SLinus Torvalds 	struct bus_attribute * bus_attr = to_bus_attr(attr);
951da177e4SLinus Torvalds 	struct bus_type * bus = to_bus(kobj);
961da177e4SLinus Torvalds 	ssize_t ret = 0;
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds 	if (bus_attr->store)
991da177e4SLinus Torvalds 		ret = bus_attr->store(bus, buf, count);
1001da177e4SLinus Torvalds 	return ret;
1011da177e4SLinus Torvalds }
1021da177e4SLinus Torvalds 
1031da177e4SLinus Torvalds static struct sysfs_ops bus_sysfs_ops = {
1041da177e4SLinus Torvalds 	.show	= bus_attr_show,
1051da177e4SLinus Torvalds 	.store	= bus_attr_store,
1061da177e4SLinus Torvalds };
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds int bus_create_file(struct bus_type * bus, struct bus_attribute * attr)
1091da177e4SLinus Torvalds {
1101da177e4SLinus Torvalds 	int error;
1111da177e4SLinus Torvalds 	if (get_bus(bus)) {
1121da177e4SLinus Torvalds 		error = sysfs_create_file(&bus->subsys.kset.kobj, &attr->attr);
1131da177e4SLinus Torvalds 		put_bus(bus);
1141da177e4SLinus Torvalds 	} else
1151da177e4SLinus Torvalds 		error = -EINVAL;
1161da177e4SLinus Torvalds 	return error;
1171da177e4SLinus Torvalds }
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds void bus_remove_file(struct bus_type * bus, struct bus_attribute * attr)
1201da177e4SLinus Torvalds {
1211da177e4SLinus Torvalds 	if (get_bus(bus)) {
1221da177e4SLinus Torvalds 		sysfs_remove_file(&bus->subsys.kset.kobj, &attr->attr);
1231da177e4SLinus Torvalds 		put_bus(bus);
1241da177e4SLinus Torvalds 	}
1251da177e4SLinus Torvalds }
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds static struct kobj_type ktype_bus = {
1281da177e4SLinus Torvalds 	.sysfs_ops	= &bus_sysfs_ops,
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds };
1311da177e4SLinus Torvalds 
1327e4ef085SAdrian Bunk static decl_subsys(bus, &ktype_bus, NULL);
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds 
1352139bdd5SRussell King #ifdef CONFIG_HOTPLUG
1362139bdd5SRussell King 
1372b08c8d0SAlan Stern /* Manually detach a device from its associated driver. */
138151ef38fSGreg Kroah-Hartman static int driver_helper(struct device *dev, void *data)
139151ef38fSGreg Kroah-Hartman {
140151ef38fSGreg Kroah-Hartman 	const char *name = data;
141151ef38fSGreg Kroah-Hartman 
142151ef38fSGreg Kroah-Hartman 	if (strcmp(name, dev->bus_id) == 0)
143151ef38fSGreg Kroah-Hartman 		return 1;
144151ef38fSGreg Kroah-Hartman 	return 0;
145151ef38fSGreg Kroah-Hartman }
146151ef38fSGreg Kroah-Hartman 
147151ef38fSGreg Kroah-Hartman static ssize_t driver_unbind(struct device_driver *drv,
148151ef38fSGreg Kroah-Hartman 			     const char *buf, size_t count)
149151ef38fSGreg Kroah-Hartman {
150151ef38fSGreg Kroah-Hartman 	struct bus_type *bus = get_bus(drv->bus);
151151ef38fSGreg Kroah-Hartman 	struct device *dev;
152151ef38fSGreg Kroah-Hartman 	int err = -ENODEV;
153151ef38fSGreg Kroah-Hartman 
154151ef38fSGreg Kroah-Hartman 	dev = bus_find_device(bus, NULL, (void *)buf, driver_helper);
1552b08c8d0SAlan Stern 	if (dev && dev->driver == drv) {
156bf74ad5bSAlan Stern 		if (dev->parent)	/* Needed for USB */
157bf74ad5bSAlan Stern 			down(&dev->parent->sem);
158151ef38fSGreg Kroah-Hartman 		device_release_driver(dev);
159bf74ad5bSAlan Stern 		if (dev->parent)
160bf74ad5bSAlan Stern 			up(&dev->parent->sem);
161151ef38fSGreg Kroah-Hartman 		err = count;
162151ef38fSGreg Kroah-Hartman 	}
1632b08c8d0SAlan Stern 	put_device(dev);
1642b08c8d0SAlan Stern 	put_bus(bus);
165151ef38fSGreg Kroah-Hartman 	return err;
166151ef38fSGreg Kroah-Hartman }
167151ef38fSGreg Kroah-Hartman static DRIVER_ATTR(unbind, S_IWUSR, NULL, driver_unbind);
168151ef38fSGreg Kroah-Hartman 
169afdce75fSGreg Kroah-Hartman /*
170afdce75fSGreg Kroah-Hartman  * Manually attach a device to a driver.
171afdce75fSGreg Kroah-Hartman  * Note: the driver must want to bind to the device,
172afdce75fSGreg Kroah-Hartman  * it is not possible to override the driver's id table.
173afdce75fSGreg Kroah-Hartman  */
174afdce75fSGreg Kroah-Hartman static ssize_t driver_bind(struct device_driver *drv,
175afdce75fSGreg Kroah-Hartman 			   const char *buf, size_t count)
176afdce75fSGreg Kroah-Hartman {
177afdce75fSGreg Kroah-Hartman 	struct bus_type *bus = get_bus(drv->bus);
178afdce75fSGreg Kroah-Hartman 	struct device *dev;
179afdce75fSGreg Kroah-Hartman 	int err = -ENODEV;
180afdce75fSGreg Kroah-Hartman 
181afdce75fSGreg Kroah-Hartman 	dev = bus_find_device(bus, NULL, (void *)buf, driver_helper);
1822b08c8d0SAlan Stern 	if (dev && dev->driver == NULL) {
183bf74ad5bSAlan Stern 		if (dev->parent)	/* Needed for USB */
184bf74ad5bSAlan Stern 			down(&dev->parent->sem);
185afdce75fSGreg Kroah-Hartman 		down(&dev->sem);
186afdce75fSGreg Kroah-Hartman 		err = driver_probe_device(drv, dev);
187afdce75fSGreg Kroah-Hartman 		up(&dev->sem);
188bf74ad5bSAlan Stern 		if (dev->parent)
189bf74ad5bSAlan Stern 			up(&dev->parent->sem);
19037225401SRyan Wilson 
19137225401SRyan Wilson 		if (err > 0) 		/* success */
19237225401SRyan Wilson 			err = count;
19337225401SRyan Wilson 		else if (err == 0)	/* driver didn't accept device */
19437225401SRyan Wilson 			err = -ENODEV;
195afdce75fSGreg Kroah-Hartman 	}
1962b08c8d0SAlan Stern 	put_device(dev);
1972b08c8d0SAlan Stern 	put_bus(bus);
198afdce75fSGreg Kroah-Hartman 	return err;
199afdce75fSGreg Kroah-Hartman }
200afdce75fSGreg Kroah-Hartman static DRIVER_ATTR(bind, S_IWUSR, NULL, driver_bind);
201afdce75fSGreg Kroah-Hartman 
2022139bdd5SRussell King #endif
203151ef38fSGreg Kroah-Hartman 
204465c7a3aSmochel@digitalimplant.org static struct device * next_device(struct klist_iter * i)
205465c7a3aSmochel@digitalimplant.org {
206465c7a3aSmochel@digitalimplant.org 	struct klist_node * n = klist_next(i);
207465c7a3aSmochel@digitalimplant.org 	return n ? container_of(n, struct device, knode_bus) : NULL;
208465c7a3aSmochel@digitalimplant.org }
209465c7a3aSmochel@digitalimplant.org 
2101da177e4SLinus Torvalds /**
2111da177e4SLinus Torvalds  *	bus_for_each_dev - device iterator.
2121da177e4SLinus Torvalds  *	@bus:	bus type.
2131da177e4SLinus Torvalds  *	@start:	device to start iterating from.
2141da177e4SLinus Torvalds  *	@data:	data for the callback.
2151da177e4SLinus Torvalds  *	@fn:	function to be called for each device.
2161da177e4SLinus Torvalds  *
2171da177e4SLinus Torvalds  *	Iterate over @bus's list of devices, and call @fn for each,
2181da177e4SLinus Torvalds  *	passing it @data. If @start is not NULL, we use that device to
2191da177e4SLinus Torvalds  *	begin iterating from.
2201da177e4SLinus Torvalds  *
2211da177e4SLinus Torvalds  *	We check the return of @fn each time. If it returns anything
2221da177e4SLinus Torvalds  *	other than 0, we break out and return that value.
2231da177e4SLinus Torvalds  *
2241da177e4SLinus Torvalds  *	NOTE: The device that returns a non-zero value is not retained
2251da177e4SLinus Torvalds  *	in any way, nor is its refcount incremented. If the caller needs
2261da177e4SLinus Torvalds  *	to retain this data, it should do, and increment the reference
2271da177e4SLinus Torvalds  *	count in the supplied callback.
2281da177e4SLinus Torvalds  */
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds int bus_for_each_dev(struct bus_type * bus, struct device * start,
2311da177e4SLinus Torvalds 		     void * data, int (*fn)(struct device *, void *))
2321da177e4SLinus Torvalds {
233465c7a3aSmochel@digitalimplant.org 	struct klist_iter i;
234465c7a3aSmochel@digitalimplant.org 	struct device * dev;
235465c7a3aSmochel@digitalimplant.org 	int error = 0;
2361da177e4SLinus Torvalds 
237465c7a3aSmochel@digitalimplant.org 	if (!bus)
238465c7a3aSmochel@digitalimplant.org 		return -EINVAL;
239465c7a3aSmochel@digitalimplant.org 
240465c7a3aSmochel@digitalimplant.org 	klist_iter_init_node(&bus->klist_devices, &i,
241465c7a3aSmochel@digitalimplant.org 			     (start ? &start->knode_bus : NULL));
242465c7a3aSmochel@digitalimplant.org 	while ((dev = next_device(&i)) && !error)
243465c7a3aSmochel@digitalimplant.org 		error = fn(dev, data);
244465c7a3aSmochel@digitalimplant.org 	klist_iter_exit(&i);
245465c7a3aSmochel@digitalimplant.org 	return error;
2461da177e4SLinus Torvalds }
2471da177e4SLinus Torvalds 
2480edb5860SCornelia Huck /**
2490edb5860SCornelia Huck  * bus_find_device - device iterator for locating a particular device.
2500edb5860SCornelia Huck  * @bus: bus type
2510edb5860SCornelia Huck  * @start: Device to begin with
2520edb5860SCornelia Huck  * @data: Data to pass to match function
2530edb5860SCornelia Huck  * @match: Callback function to check device
2540edb5860SCornelia Huck  *
2550edb5860SCornelia Huck  * This is similar to the bus_for_each_dev() function above, but it
2560edb5860SCornelia Huck  * returns a reference to a device that is 'found' for later use, as
2570edb5860SCornelia Huck  * determined by the @match callback.
2580edb5860SCornelia Huck  *
2590edb5860SCornelia Huck  * The callback should return 0 if the device doesn't match and non-zero
2600edb5860SCornelia Huck  * if it does.  If the callback returns non-zero, this function will
2610edb5860SCornelia Huck  * return to the caller and not iterate over any more devices.
2620edb5860SCornelia Huck  */
2630edb5860SCornelia Huck struct device * bus_find_device(struct bus_type *bus,
2640edb5860SCornelia Huck 				struct device *start, void *data,
2650edb5860SCornelia Huck 				int (*match)(struct device *, void *))
2660edb5860SCornelia Huck {
2670edb5860SCornelia Huck 	struct klist_iter i;
2680edb5860SCornelia Huck 	struct device *dev;
2690edb5860SCornelia Huck 
2700edb5860SCornelia Huck 	if (!bus)
2710edb5860SCornelia Huck 		return NULL;
2720edb5860SCornelia Huck 
2730edb5860SCornelia Huck 	klist_iter_init_node(&bus->klist_devices, &i,
2740edb5860SCornelia Huck 			     (start ? &start->knode_bus : NULL));
2750edb5860SCornelia Huck 	while ((dev = next_device(&i)))
2760edb5860SCornelia Huck 		if (match(dev, data) && get_device(dev))
2770edb5860SCornelia Huck 			break;
2780edb5860SCornelia Huck 	klist_iter_exit(&i);
2790edb5860SCornelia Huck 	return dev;
2800edb5860SCornelia Huck }
28138fdac3cSmochel@digitalimplant.org 
28238fdac3cSmochel@digitalimplant.org 
28338fdac3cSmochel@digitalimplant.org static struct device_driver * next_driver(struct klist_iter * i)
28438fdac3cSmochel@digitalimplant.org {
28538fdac3cSmochel@digitalimplant.org 	struct klist_node * n = klist_next(i);
28638fdac3cSmochel@digitalimplant.org 	return n ? container_of(n, struct device_driver, knode_bus) : NULL;
28738fdac3cSmochel@digitalimplant.org }
28838fdac3cSmochel@digitalimplant.org 
2891da177e4SLinus Torvalds /**
2901da177e4SLinus Torvalds  *	bus_for_each_drv - driver iterator
2911da177e4SLinus Torvalds  *	@bus:	bus we're dealing with.
2921da177e4SLinus Torvalds  *	@start:	driver to start iterating on.
2931da177e4SLinus Torvalds  *	@data:	data to pass to the callback.
2941da177e4SLinus Torvalds  *	@fn:	function to call for each driver.
2951da177e4SLinus Torvalds  *
2961da177e4SLinus Torvalds  *	This is nearly identical to the device iterator above.
2971da177e4SLinus Torvalds  *	We iterate over each driver that belongs to @bus, and call
2981da177e4SLinus Torvalds  *	@fn for each. If @fn returns anything but 0, we break out
2991da177e4SLinus Torvalds  *	and return it. If @start is not NULL, we use it as the head
3001da177e4SLinus Torvalds  *	of the list.
3011da177e4SLinus Torvalds  *
3021da177e4SLinus Torvalds  *	NOTE: we don't return the driver that returns a non-zero
3031da177e4SLinus Torvalds  *	value, nor do we leave the reference count incremented for that
3041da177e4SLinus Torvalds  *	driver. If the caller needs to know that info, it must set it
3051da177e4SLinus Torvalds  *	in the callback. It must also be sure to increment the refcount
3061da177e4SLinus Torvalds  *	so it doesn't disappear before returning to the caller.
3071da177e4SLinus Torvalds  */
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds int bus_for_each_drv(struct bus_type * bus, struct device_driver * start,
3101da177e4SLinus Torvalds 		     void * data, int (*fn)(struct device_driver *, void *))
3111da177e4SLinus Torvalds {
31238fdac3cSmochel@digitalimplant.org 	struct klist_iter i;
31338fdac3cSmochel@digitalimplant.org 	struct device_driver * drv;
31438fdac3cSmochel@digitalimplant.org 	int error = 0;
3151da177e4SLinus Torvalds 
31638fdac3cSmochel@digitalimplant.org 	if (!bus)
31738fdac3cSmochel@digitalimplant.org 		return -EINVAL;
31838fdac3cSmochel@digitalimplant.org 
31938fdac3cSmochel@digitalimplant.org 	klist_iter_init_node(&bus->klist_drivers, &i,
32038fdac3cSmochel@digitalimplant.org 			     start ? &start->knode_bus : NULL);
32138fdac3cSmochel@digitalimplant.org 	while ((drv = next_driver(&i)) && !error)
32238fdac3cSmochel@digitalimplant.org 		error = fn(drv, data);
32338fdac3cSmochel@digitalimplant.org 	klist_iter_exit(&i);
32438fdac3cSmochel@digitalimplant.org 	return error;
3251da177e4SLinus Torvalds }
3261da177e4SLinus Torvalds 
3271da177e4SLinus Torvalds static int device_add_attrs(struct bus_type *bus, struct device *dev)
3281da177e4SLinus Torvalds {
3291da177e4SLinus Torvalds 	int error = 0;
3301da177e4SLinus Torvalds 	int i;
3311da177e4SLinus Torvalds 
332*4aca67e5SAndrew Morton 	if (!bus->dev_attrs)
333*4aca67e5SAndrew Morton 		return 0;
334*4aca67e5SAndrew Morton 
3351da177e4SLinus Torvalds 	for (i = 0; attr_name(bus->dev_attrs[i]); i++) {
3361da177e4SLinus Torvalds 		error = device_create_file(dev,&bus->dev_attrs[i]);
337*4aca67e5SAndrew Morton 		if (error) {
3381da177e4SLinus Torvalds 			while (--i >= 0)
3391da177e4SLinus Torvalds 				device_remove_file(dev, &bus->dev_attrs[i]);
340*4aca67e5SAndrew Morton 			break;
3411da177e4SLinus Torvalds 		}
342*4aca67e5SAndrew Morton 	}
343*4aca67e5SAndrew Morton 	return error;
344*4aca67e5SAndrew Morton }
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds static void device_remove_attrs(struct bus_type * bus, struct device * dev)
3471da177e4SLinus Torvalds {
3481da177e4SLinus Torvalds 	int i;
3491da177e4SLinus Torvalds 
3501da177e4SLinus Torvalds 	if (bus->dev_attrs) {
3511da177e4SLinus Torvalds 		for (i = 0; attr_name(bus->dev_attrs[i]); i++)
3521da177e4SLinus Torvalds 			device_remove_file(dev,&bus->dev_attrs[i]);
3531da177e4SLinus Torvalds 	}
3541da177e4SLinus Torvalds }
3551da177e4SLinus Torvalds 
356b9cafc7dSKay Sievers #ifdef CONFIG_SYSFS_DEPRECATED
357b9cafc7dSKay Sievers static int make_deprecated_bus_links(struct device *dev)
358b9cafc7dSKay Sievers {
359b9cafc7dSKay Sievers 	return sysfs_create_link(&dev->kobj,
360b9cafc7dSKay Sievers 				 &dev->bus->subsys.kset.kobj, "bus");
361b9cafc7dSKay Sievers }
362b9cafc7dSKay Sievers 
363b9cafc7dSKay Sievers static void remove_deprecated_bus_links(struct device *dev)
364b9cafc7dSKay Sievers {
365b9cafc7dSKay Sievers 	sysfs_remove_link(&dev->kobj, "bus");
366b9cafc7dSKay Sievers }
367b9cafc7dSKay Sievers #else
368b9cafc7dSKay Sievers static inline int make_deprecated_bus_links(struct device *dev) { return 0; }
369b9cafc7dSKay Sievers static inline void remove_deprecated_bus_links(struct device *dev) { }
370b9cafc7dSKay Sievers #endif
3711da177e4SLinus Torvalds 
3721da177e4SLinus Torvalds /**
3731da177e4SLinus Torvalds  *	bus_add_device - add device to bus
3741da177e4SLinus Torvalds  *	@dev:	device being added
3751da177e4SLinus Torvalds  *
3761da177e4SLinus Torvalds  *	- Add the device to its bus's list of devices.
37753877d06SKay Sievers  *	- Create link to device's bus.
3781da177e4SLinus Torvalds  */
3791da177e4SLinus Torvalds int bus_add_device(struct device * dev)
3801da177e4SLinus Torvalds {
3811da177e4SLinus Torvalds 	struct bus_type * bus = get_bus(dev->bus);
3821da177e4SLinus Torvalds 	int error = 0;
3831da177e4SLinus Torvalds 
3841da177e4SLinus Torvalds 	if (bus) {
3851da177e4SLinus Torvalds 		pr_debug("bus %s: add device %s\n", bus->name, dev->bus_id);
386ca2b94baSHannes Reinecke 		error = device_add_attrs(bus, dev);
387f86db396SAndrew Morton 		if (error)
388513e7337SCornelia Huck 			goto out_put;
389f86db396SAndrew Morton 		error = sysfs_create_link(&bus->devices.kobj,
390f86db396SAndrew Morton 						&dev->kobj, dev->bus_id);
391f86db396SAndrew Morton 		if (error)
392513e7337SCornelia Huck 			goto out_id;
393f86db396SAndrew Morton 		error = sysfs_create_link(&dev->kobj,
394f86db396SAndrew Morton 				&dev->bus->subsys.kset.kobj, "subsystem");
395f86db396SAndrew Morton 		if (error)
396513e7337SCornelia Huck 			goto out_subsys;
397b9cafc7dSKay Sievers 		error = make_deprecated_bus_links(dev);
398513e7337SCornelia Huck 		if (error)
399513e7337SCornelia Huck 			goto out_deprecated;
4001da177e4SLinus Torvalds 	}
401513e7337SCornelia Huck 	return 0;
402513e7337SCornelia Huck 
403513e7337SCornelia Huck out_deprecated:
404513e7337SCornelia Huck 	sysfs_remove_link(&dev->kobj, "subsystem");
405513e7337SCornelia Huck out_subsys:
406513e7337SCornelia Huck 	sysfs_remove_link(&bus->devices.kobj, dev->bus_id);
407513e7337SCornelia Huck out_id:
408513e7337SCornelia Huck 	device_remove_attrs(bus, dev);
409513e7337SCornelia Huck out_put:
410513e7337SCornelia Huck 	put_bus(dev->bus);
4111da177e4SLinus Torvalds 	return error;
4121da177e4SLinus Torvalds }
4131da177e4SLinus Torvalds 
4141da177e4SLinus Torvalds /**
41553877d06SKay Sievers  *	bus_attach_device - add device to bus
41653877d06SKay Sievers  *	@dev:	device tried to attach to a driver
41753877d06SKay Sievers  *
418f2eaae19SAlan Stern  *	- Add device to bus's list of devices.
41953877d06SKay Sievers  *	- Try to attach to driver.
42053877d06SKay Sievers  */
421f86db396SAndrew Morton int bus_attach_device(struct device * dev)
42253877d06SKay Sievers {
42353877d06SKay Sievers 	struct bus_type *bus = dev->bus;
424f86db396SAndrew Morton 	int ret = 0;
42553877d06SKay Sievers 
42653877d06SKay Sievers 	if (bus) {
427f2eaae19SAlan Stern 		dev->is_registered = 1;
428f86db396SAndrew Morton 		ret = device_attach(dev);
429f86db396SAndrew Morton 		if (ret >= 0) {
43053877d06SKay Sievers 			klist_add_tail(&dev->knode_bus, &bus->klist_devices);
431f86db396SAndrew Morton 			ret = 0;
432f2eaae19SAlan Stern 		} else
433f2eaae19SAlan Stern 			dev->is_registered = 0;
43453877d06SKay Sievers 	}
435f86db396SAndrew Morton 	return ret;
436f86db396SAndrew Morton }
43753877d06SKay Sievers 
43853877d06SKay Sievers /**
4391da177e4SLinus Torvalds  *	bus_remove_device - remove device from bus
4401da177e4SLinus Torvalds  *	@dev:	device to be removed
4411da177e4SLinus Torvalds  *
4421da177e4SLinus Torvalds  *	- Remove symlink from bus's directory.
4431da177e4SLinus Torvalds  *	- Delete device from bus's list.
4441da177e4SLinus Torvalds  *	- Detach from its driver.
4451da177e4SLinus Torvalds  *	- Drop reference taken in bus_add_device().
4461da177e4SLinus Torvalds  */
4471da177e4SLinus Torvalds void bus_remove_device(struct device * dev)
4481da177e4SLinus Torvalds {
4491da177e4SLinus Torvalds 	if (dev->bus) {
450b9d9c82bSKay Sievers 		sysfs_remove_link(&dev->kobj, "subsystem");
451b9cafc7dSKay Sievers 		remove_deprecated_bus_links(dev);
4521da177e4SLinus Torvalds 		sysfs_remove_link(&dev->bus->devices.kobj, dev->bus_id);
4531da177e4SLinus Torvalds 		device_remove_attrs(dev->bus, dev);
454f70fa629SAlan Stern 		if (dev->is_registered) {
455f2eaae19SAlan Stern 			dev->is_registered = 0;
456f2eaae19SAlan Stern 			klist_del(&dev->knode_bus);
457f70fa629SAlan Stern 		}
4581da177e4SLinus Torvalds 		pr_debug("bus %s: remove device %s\n", dev->bus->name, dev->bus_id);
4591da177e4SLinus Torvalds 		device_release_driver(dev);
4601da177e4SLinus Torvalds 		put_bus(dev->bus);
4611da177e4SLinus Torvalds 	}
4621da177e4SLinus Torvalds }
4631da177e4SLinus Torvalds 
4641da177e4SLinus Torvalds static int driver_add_attrs(struct bus_type * bus, struct device_driver * drv)
4651da177e4SLinus Torvalds {
4661da177e4SLinus Torvalds 	int error = 0;
4671da177e4SLinus Torvalds 	int i;
4681da177e4SLinus Torvalds 
4691da177e4SLinus Torvalds 	if (bus->drv_attrs) {
4701da177e4SLinus Torvalds 		for (i = 0; attr_name(bus->drv_attrs[i]); i++) {
4711da177e4SLinus Torvalds 			error = driver_create_file(drv, &bus->drv_attrs[i]);
4721da177e4SLinus Torvalds 			if (error)
4731da177e4SLinus Torvalds 				goto Err;
4741da177e4SLinus Torvalds 		}
4751da177e4SLinus Torvalds 	}
4761da177e4SLinus Torvalds  Done:
4771da177e4SLinus Torvalds 	return error;
4781da177e4SLinus Torvalds  Err:
4791da177e4SLinus Torvalds 	while (--i >= 0)
4801da177e4SLinus Torvalds 		driver_remove_file(drv, &bus->drv_attrs[i]);
4811da177e4SLinus Torvalds 	goto Done;
4821da177e4SLinus Torvalds }
4831da177e4SLinus Torvalds 
4841da177e4SLinus Torvalds 
4851da177e4SLinus Torvalds static void driver_remove_attrs(struct bus_type * bus, struct device_driver * drv)
4861da177e4SLinus Torvalds {
4871da177e4SLinus Torvalds 	int i;
4881da177e4SLinus Torvalds 
4891da177e4SLinus Torvalds 	if (bus->drv_attrs) {
4901da177e4SLinus Torvalds 		for (i = 0; attr_name(bus->drv_attrs[i]); i++)
4911da177e4SLinus Torvalds 			driver_remove_file(drv, &bus->drv_attrs[i]);
4921da177e4SLinus Torvalds 	}
4931da177e4SLinus Torvalds }
4941da177e4SLinus Torvalds 
495874c6241SGreg Kroah-Hartman #ifdef CONFIG_HOTPLUG
496874c6241SGreg Kroah-Hartman /*
497874c6241SGreg Kroah-Hartman  * Thanks to drivers making their tables __devinit, we can't allow manual
498874c6241SGreg Kroah-Hartman  * bind and unbind from userspace unless CONFIG_HOTPLUG is enabled.
499874c6241SGreg Kroah-Hartman  */
500f86db396SAndrew Morton static int __must_check add_bind_files(struct device_driver *drv)
501874c6241SGreg Kroah-Hartman {
502f86db396SAndrew Morton 	int ret;
503f86db396SAndrew Morton 
504f86db396SAndrew Morton 	ret = driver_create_file(drv, &driver_attr_unbind);
505f86db396SAndrew Morton 	if (ret == 0) {
506f86db396SAndrew Morton 		ret = driver_create_file(drv, &driver_attr_bind);
507f86db396SAndrew Morton 		if (ret)
508f86db396SAndrew Morton 			driver_remove_file(drv, &driver_attr_unbind);
509f86db396SAndrew Morton 	}
510f86db396SAndrew Morton 	return ret;
511874c6241SGreg Kroah-Hartman }
512874c6241SGreg Kroah-Hartman 
513874c6241SGreg Kroah-Hartman static void remove_bind_files(struct device_driver *drv)
514874c6241SGreg Kroah-Hartman {
515874c6241SGreg Kroah-Hartman 	driver_remove_file(drv, &driver_attr_bind);
516874c6241SGreg Kroah-Hartman 	driver_remove_file(drv, &driver_attr_unbind);
517874c6241SGreg Kroah-Hartman }
518874c6241SGreg Kroah-Hartman #else
51935acfdd7SYoichi Yuasa static inline int add_bind_files(struct device_driver *drv) { return 0; }
520874c6241SGreg Kroah-Hartman static inline void remove_bind_files(struct device_driver *drv) {}
521874c6241SGreg Kroah-Hartman #endif
5221da177e4SLinus Torvalds 
5231da177e4SLinus Torvalds /**
5241da177e4SLinus Torvalds  *	bus_add_driver - Add a driver to the bus.
5251da177e4SLinus Torvalds  *	@drv:	driver.
5261da177e4SLinus Torvalds  *
5271da177e4SLinus Torvalds  */
5281da177e4SLinus Torvalds int bus_add_driver(struct device_driver *drv)
5291da177e4SLinus Torvalds {
5301da177e4SLinus Torvalds 	struct bus_type * bus = get_bus(drv->bus);
5311da177e4SLinus Torvalds 	int error = 0;
5321da177e4SLinus Torvalds 
533d9fd4d3bSJeff Garzik 	if (!bus)
534d9fd4d3bSJeff Garzik 		return 0;
535d9fd4d3bSJeff Garzik 
5361da177e4SLinus Torvalds 	pr_debug("bus %s: add driver %s\n", bus->name, drv->name);
5371da177e4SLinus Torvalds 	error = kobject_set_name(&drv->kobj, "%s", drv->name);
538f86db396SAndrew Morton 	if (error)
539f86db396SAndrew Morton 		goto out_put_bus;
5401da177e4SLinus Torvalds 	drv->kobj.kset = &bus->drivers;
541f86db396SAndrew Morton 	if ((error = kobject_register(&drv->kobj)))
542f86db396SAndrew Morton 		goto out_put_bus;
5431da177e4SLinus Torvalds 
544f86db396SAndrew Morton 	error = driver_attach(drv);
545f86db396SAndrew Morton 	if (error)
546f86db396SAndrew Morton 		goto out_unregister;
547d856f1e3SJames Bottomley 	klist_add_tail(&drv->knode_bus, &bus->klist_drivers);
5481da177e4SLinus Torvalds 	module_add_driver(drv->owner, drv);
5491da177e4SLinus Torvalds 
550f86db396SAndrew Morton 	error = driver_add_attrs(bus, drv);
551f86db396SAndrew Morton 	if (error) {
552f86db396SAndrew Morton 		/* How the hell do we get out of this pickle? Give up */
553f86db396SAndrew Morton 		printk(KERN_ERR "%s: driver_add_attrs(%s) failed\n",
554f86db396SAndrew Morton 			__FUNCTION__, drv->name);
555f86db396SAndrew Morton 	}
556f86db396SAndrew Morton 	error = add_bind_files(drv);
557f86db396SAndrew Morton 	if (error) {
558f86db396SAndrew Morton 		/* Ditto */
559f86db396SAndrew Morton 		printk(KERN_ERR "%s: add_bind_files(%s) failed\n",
560f86db396SAndrew Morton 			__FUNCTION__, drv->name);
561f86db396SAndrew Morton 	}
562d9fd4d3bSJeff Garzik 
5631da177e4SLinus Torvalds 	return error;
564f86db396SAndrew Morton out_unregister:
565f86db396SAndrew Morton 	kobject_unregister(&drv->kobj);
566f86db396SAndrew Morton out_put_bus:
567f86db396SAndrew Morton 	put_bus(bus);
568f86db396SAndrew Morton 	return error;
5691da177e4SLinus Torvalds }
5701da177e4SLinus Torvalds 
5711da177e4SLinus Torvalds /**
5721da177e4SLinus Torvalds  *	bus_remove_driver - delete driver from bus's knowledge.
5731da177e4SLinus Torvalds  *	@drv:	driver.
5741da177e4SLinus Torvalds  *
5751da177e4SLinus Torvalds  *	Detach the driver from the devices it controls, and remove
5761da177e4SLinus Torvalds  *	it from its bus's list of drivers. Finally, we drop the reference
5771da177e4SLinus Torvalds  *	to the bus we took in bus_add_driver().
5781da177e4SLinus Torvalds  */
5791da177e4SLinus Torvalds 
5801da177e4SLinus Torvalds void bus_remove_driver(struct device_driver * drv)
5811da177e4SLinus Torvalds {
582d9fd4d3bSJeff Garzik 	if (!drv->bus)
583d9fd4d3bSJeff Garzik 		return;
584d9fd4d3bSJeff Garzik 
585874c6241SGreg Kroah-Hartman 	remove_bind_files(drv);
5861da177e4SLinus Torvalds 	driver_remove_attrs(drv->bus, drv);
58738fdac3cSmochel@digitalimplant.org 	klist_remove(&drv->knode_bus);
5881da177e4SLinus Torvalds 	pr_debug("bus %s: remove driver %s\n", drv->bus->name, drv->name);
5891da177e4SLinus Torvalds 	driver_detach(drv);
5901da177e4SLinus Torvalds 	module_remove_driver(drv);
5911da177e4SLinus Torvalds 	kobject_unregister(&drv->kobj);
5921da177e4SLinus Torvalds 	put_bus(drv->bus);
5931da177e4SLinus Torvalds }
5941da177e4SLinus Torvalds 
5951da177e4SLinus Torvalds 
5961da177e4SLinus Torvalds /* Helper for bus_rescan_devices's iter */
597f86db396SAndrew Morton static int __must_check bus_rescan_devices_helper(struct device *dev,
598f86db396SAndrew Morton 						void *data)
5991da177e4SLinus Torvalds {
600f86db396SAndrew Morton 	int ret = 0;
601f86db396SAndrew Morton 
602bf74ad5bSAlan Stern 	if (!dev->driver) {
603bf74ad5bSAlan Stern 		if (dev->parent)	/* Needed for USB */
604bf74ad5bSAlan Stern 			down(&dev->parent->sem);
605f86db396SAndrew Morton 		ret = device_attach(dev);
606bf74ad5bSAlan Stern 		if (dev->parent)
607bf74ad5bSAlan Stern 			up(&dev->parent->sem);
608f86db396SAndrew Morton 		if (ret > 0)
609f86db396SAndrew Morton 			ret = 0;
610bf74ad5bSAlan Stern 	}
611f86db396SAndrew Morton 	return ret < 0 ? ret : 0;
6121da177e4SLinus Torvalds }
6131da177e4SLinus Torvalds 
6141da177e4SLinus Torvalds /**
6151da177e4SLinus Torvalds  * bus_rescan_devices - rescan devices on the bus for possible drivers
6161da177e4SLinus Torvalds  * @bus: the bus to scan.
6171da177e4SLinus Torvalds  *
6181da177e4SLinus Torvalds  * This function will look for devices on the bus with no driver
61923d3d602SGreg Kroah-Hartman  * attached and rescan it against existing drivers to see if it matches
62023d3d602SGreg Kroah-Hartman  * any by calling device_attach() for the unbound devices.
6211da177e4SLinus Torvalds  */
622f86db396SAndrew Morton int bus_rescan_devices(struct bus_type * bus)
6231da177e4SLinus Torvalds {
624f86db396SAndrew Morton 	return bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper);
6251da177e4SLinus Torvalds }
6261da177e4SLinus Torvalds 
627e935d5daSMoore, Eric /**
628e935d5daSMoore, Eric  * device_reprobe - remove driver for a device and probe for a new driver
629e935d5daSMoore, Eric  * @dev: the device to reprobe
630e935d5daSMoore, Eric  *
631e935d5daSMoore, Eric  * This function detaches the attached driver (if any) for the given
632e935d5daSMoore, Eric  * device and restarts the driver probing process.  It is intended
633e935d5daSMoore, Eric  * to use if probing criteria changed during a devices lifetime and
634e935d5daSMoore, Eric  * driver attachment should change accordingly.
635e935d5daSMoore, Eric  */
636f86db396SAndrew Morton int device_reprobe(struct device *dev)
637e935d5daSMoore, Eric {
638e935d5daSMoore, Eric 	if (dev->driver) {
639e935d5daSMoore, Eric 		if (dev->parent)        /* Needed for USB */
640e935d5daSMoore, Eric 			down(&dev->parent->sem);
641e935d5daSMoore, Eric 		device_release_driver(dev);
642e935d5daSMoore, Eric 		if (dev->parent)
643e935d5daSMoore, Eric 			up(&dev->parent->sem);
644e935d5daSMoore, Eric 	}
645f86db396SAndrew Morton 	return bus_rescan_devices_helper(dev, NULL);
646e935d5daSMoore, Eric }
647e935d5daSMoore, Eric EXPORT_SYMBOL_GPL(device_reprobe);
6481da177e4SLinus Torvalds 
6491da177e4SLinus Torvalds struct bus_type *get_bus(struct bus_type *bus)
6501da177e4SLinus Torvalds {
651f86db396SAndrew Morton 	return bus ? container_of(subsys_get(&bus->subsys),
652f86db396SAndrew Morton 				struct bus_type, subsys) : NULL;
6531da177e4SLinus Torvalds }
6541da177e4SLinus Torvalds 
6551da177e4SLinus Torvalds void put_bus(struct bus_type * bus)
6561da177e4SLinus Torvalds {
6571da177e4SLinus Torvalds 	subsys_put(&bus->subsys);
6581da177e4SLinus Torvalds }
6591da177e4SLinus Torvalds 
6601da177e4SLinus Torvalds 
6611da177e4SLinus Torvalds /**
6621da177e4SLinus Torvalds  *	find_bus - locate bus by name.
6631da177e4SLinus Torvalds  *	@name:	name of bus.
6641da177e4SLinus Torvalds  *
6651da177e4SLinus Torvalds  *	Call kset_find_obj() to iterate over list of buses to
6661da177e4SLinus Torvalds  *	find a bus by name. Return bus if found.
6671da177e4SLinus Torvalds  *
6681da177e4SLinus Torvalds  *	Note that kset_find_obj increments bus' reference count.
6691da177e4SLinus Torvalds  */
6707e4ef085SAdrian Bunk #if 0
6711da177e4SLinus Torvalds struct bus_type * find_bus(char * name)
6721da177e4SLinus Torvalds {
6731da177e4SLinus Torvalds 	struct kobject * k = kset_find_obj(&bus_subsys.kset, name);
6741da177e4SLinus Torvalds 	return k ? to_bus(k) : NULL;
6751da177e4SLinus Torvalds }
6767e4ef085SAdrian Bunk #endif  /*  0  */
6771da177e4SLinus Torvalds 
6781da177e4SLinus Torvalds 
6791da177e4SLinus Torvalds /**
6801da177e4SLinus Torvalds  *	bus_add_attrs - Add default attributes for this bus.
6811da177e4SLinus Torvalds  *	@bus:	Bus that has just been registered.
6821da177e4SLinus Torvalds  */
6831da177e4SLinus Torvalds 
6841da177e4SLinus Torvalds static int bus_add_attrs(struct bus_type * bus)
6851da177e4SLinus Torvalds {
6861da177e4SLinus Torvalds 	int error = 0;
6871da177e4SLinus Torvalds 	int i;
6881da177e4SLinus Torvalds 
6891da177e4SLinus Torvalds 	if (bus->bus_attrs) {
6901da177e4SLinus Torvalds 		for (i = 0; attr_name(bus->bus_attrs[i]); i++) {
6911da177e4SLinus Torvalds 			if ((error = bus_create_file(bus,&bus->bus_attrs[i])))
6921da177e4SLinus Torvalds 				goto Err;
6931da177e4SLinus Torvalds 		}
6941da177e4SLinus Torvalds 	}
6951da177e4SLinus Torvalds  Done:
6961da177e4SLinus Torvalds 	return error;
6971da177e4SLinus Torvalds  Err:
6981da177e4SLinus Torvalds 	while (--i >= 0)
6991da177e4SLinus Torvalds 		bus_remove_file(bus,&bus->bus_attrs[i]);
7001da177e4SLinus Torvalds 	goto Done;
7011da177e4SLinus Torvalds }
7021da177e4SLinus Torvalds 
7031da177e4SLinus Torvalds static void bus_remove_attrs(struct bus_type * bus)
7041da177e4SLinus Torvalds {
7051da177e4SLinus Torvalds 	int i;
7061da177e4SLinus Torvalds 
7071da177e4SLinus Torvalds 	if (bus->bus_attrs) {
7081da177e4SLinus Torvalds 		for (i = 0; attr_name(bus->bus_attrs[i]); i++)
7091da177e4SLinus Torvalds 			bus_remove_file(bus,&bus->bus_attrs[i]);
7101da177e4SLinus Torvalds 	}
7111da177e4SLinus Torvalds }
7121da177e4SLinus Torvalds 
71334bb61f9SJames Bottomley static void klist_devices_get(struct klist_node *n)
71434bb61f9SJames Bottomley {
71534bb61f9SJames Bottomley 	struct device *dev = container_of(n, struct device, knode_bus);
71634bb61f9SJames Bottomley 
71734bb61f9SJames Bottomley 	get_device(dev);
71834bb61f9SJames Bottomley }
71934bb61f9SJames Bottomley 
72034bb61f9SJames Bottomley static void klist_devices_put(struct klist_node *n)
72134bb61f9SJames Bottomley {
72234bb61f9SJames Bottomley 	struct device *dev = container_of(n, struct device, knode_bus);
72334bb61f9SJames Bottomley 
72434bb61f9SJames Bottomley 	put_device(dev);
72534bb61f9SJames Bottomley }
72634bb61f9SJames Bottomley 
7271da177e4SLinus Torvalds /**
7281da177e4SLinus Torvalds  *	bus_register - register a bus with the system.
7291da177e4SLinus Torvalds  *	@bus:	bus.
7301da177e4SLinus Torvalds  *
7311da177e4SLinus Torvalds  *	Once we have that, we registered the bus with the kobject
7321da177e4SLinus Torvalds  *	infrastructure, then register the children subsystems it has:
7331da177e4SLinus Torvalds  *	the devices and drivers that belong to the bus.
7341da177e4SLinus Torvalds  */
7351da177e4SLinus Torvalds int bus_register(struct bus_type * bus)
7361da177e4SLinus Torvalds {
7371da177e4SLinus Torvalds 	int retval;
7381da177e4SLinus Torvalds 
739116af378SBenjamin Herrenschmidt 	BLOCKING_INIT_NOTIFIER_HEAD(&bus->bus_notifier);
740116af378SBenjamin Herrenschmidt 
7411da177e4SLinus Torvalds 	retval = kobject_set_name(&bus->subsys.kset.kobj, "%s", bus->name);
7421da177e4SLinus Torvalds 	if (retval)
7431da177e4SLinus Torvalds 		goto out;
7441da177e4SLinus Torvalds 
7451da177e4SLinus Torvalds 	subsys_set_kset(bus, bus_subsys);
7461da177e4SLinus Torvalds 	retval = subsystem_register(&bus->subsys);
7471da177e4SLinus Torvalds 	if (retval)
7481da177e4SLinus Torvalds 		goto out;
7491da177e4SLinus Torvalds 
7501da177e4SLinus Torvalds 	kobject_set_name(&bus->devices.kobj, "devices");
7511da177e4SLinus Torvalds 	bus->devices.subsys = &bus->subsys;
7521da177e4SLinus Torvalds 	retval = kset_register(&bus->devices);
7531da177e4SLinus Torvalds 	if (retval)
7541da177e4SLinus Torvalds 		goto bus_devices_fail;
7551da177e4SLinus Torvalds 
7561da177e4SLinus Torvalds 	kobject_set_name(&bus->drivers.kobj, "drivers");
7571da177e4SLinus Torvalds 	bus->drivers.subsys = &bus->subsys;
7581da177e4SLinus Torvalds 	bus->drivers.ktype = &ktype_driver;
7591da177e4SLinus Torvalds 	retval = kset_register(&bus->drivers);
7601da177e4SLinus Torvalds 	if (retval)
7611da177e4SLinus Torvalds 		goto bus_drivers_fail;
762465c7a3aSmochel@digitalimplant.org 
76334bb61f9SJames Bottomley 	klist_init(&bus->klist_devices, klist_devices_get, klist_devices_put);
76481107bf5SAlan Stern 	klist_init(&bus->klist_drivers, NULL, NULL);
7651bb6881aSCornelia Huck 	retval = bus_add_attrs(bus);
7661bb6881aSCornelia Huck 	if (retval)
7671bb6881aSCornelia Huck 		goto bus_attrs_fail;
7681da177e4SLinus Torvalds 
7691da177e4SLinus Torvalds 	pr_debug("bus type '%s' registered\n", bus->name);
7701da177e4SLinus Torvalds 	return 0;
7711da177e4SLinus Torvalds 
7721bb6881aSCornelia Huck bus_attrs_fail:
7731bb6881aSCornelia Huck 	kset_unregister(&bus->drivers);
7741da177e4SLinus Torvalds bus_drivers_fail:
7751da177e4SLinus Torvalds 	kset_unregister(&bus->devices);
7761da177e4SLinus Torvalds bus_devices_fail:
7771da177e4SLinus Torvalds 	subsystem_unregister(&bus->subsys);
7781da177e4SLinus Torvalds out:
7791da177e4SLinus Torvalds 	return retval;
7801da177e4SLinus Torvalds }
7811da177e4SLinus Torvalds 
7821da177e4SLinus Torvalds 
7831da177e4SLinus Torvalds /**
7841da177e4SLinus Torvalds  *	bus_unregister - remove a bus from the system
7851da177e4SLinus Torvalds  *	@bus:	bus.
7861da177e4SLinus Torvalds  *
7871da177e4SLinus Torvalds  *	Unregister the child subsystems and the bus itself.
7881da177e4SLinus Torvalds  *	Finally, we call put_bus() to release the refcount
7891da177e4SLinus Torvalds  */
7901da177e4SLinus Torvalds void bus_unregister(struct bus_type * bus)
7911da177e4SLinus Torvalds {
7921da177e4SLinus Torvalds 	pr_debug("bus %s: unregistering\n", bus->name);
7931da177e4SLinus Torvalds 	bus_remove_attrs(bus);
7941da177e4SLinus Torvalds 	kset_unregister(&bus->drivers);
7951da177e4SLinus Torvalds 	kset_unregister(&bus->devices);
7961da177e4SLinus Torvalds 	subsystem_unregister(&bus->subsys);
7971da177e4SLinus Torvalds }
7981da177e4SLinus Torvalds 
799116af378SBenjamin Herrenschmidt int bus_register_notifier(struct bus_type *bus, struct notifier_block *nb)
800116af378SBenjamin Herrenschmidt {
801116af378SBenjamin Herrenschmidt 	return blocking_notifier_chain_register(&bus->bus_notifier, nb);
802116af378SBenjamin Herrenschmidt }
803116af378SBenjamin Herrenschmidt EXPORT_SYMBOL_GPL(bus_register_notifier);
804116af378SBenjamin Herrenschmidt 
805116af378SBenjamin Herrenschmidt int bus_unregister_notifier(struct bus_type *bus, struct notifier_block *nb)
806116af378SBenjamin Herrenschmidt {
807116af378SBenjamin Herrenschmidt 	return blocking_notifier_chain_unregister(&bus->bus_notifier, nb);
808116af378SBenjamin Herrenschmidt }
809116af378SBenjamin Herrenschmidt EXPORT_SYMBOL_GPL(bus_unregister_notifier);
810116af378SBenjamin Herrenschmidt 
8111da177e4SLinus Torvalds int __init buses_init(void)
8121da177e4SLinus Torvalds {
8131da177e4SLinus Torvalds 	return subsystem_register(&bus_subsys);
8141da177e4SLinus Torvalds }
8151da177e4SLinus Torvalds 
8161da177e4SLinus Torvalds 
8171da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(bus_for_each_dev);
8180edb5860SCornelia Huck EXPORT_SYMBOL_GPL(bus_find_device);
8191da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(bus_for_each_drv);
8201da177e4SLinus Torvalds 
8211da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(bus_register);
8221da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(bus_unregister);
8231da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(bus_rescan_devices);
8241da177e4SLinus Torvalds 
8251da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(bus_create_file);
8261da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(bus_remove_file);
827