xref: /linux/drivers/base/bus.c (revision 2b08c8d0468866f86da97f836c6ac14338cb81a9)
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/config.h>
121da177e4SLinus Torvalds #include <linux/device.h>
131da177e4SLinus Torvalds #include <linux/module.h>
141da177e4SLinus Torvalds #include <linux/errno.h>
151da177e4SLinus Torvalds #include <linux/init.h>
161da177e4SLinus Torvalds #include <linux/string.h>
171da177e4SLinus Torvalds #include "base.h"
181da177e4SLinus Torvalds #include "power/power.h"
191da177e4SLinus Torvalds 
201da177e4SLinus Torvalds #define to_bus_attr(_attr) container_of(_attr, struct bus_attribute, attr)
211da177e4SLinus Torvalds #define to_bus(obj) container_of(obj, struct bus_type, subsys.kset.kobj)
221da177e4SLinus Torvalds 
231da177e4SLinus Torvalds /*
241da177e4SLinus Torvalds  * sysfs bindings for drivers
251da177e4SLinus Torvalds  */
261da177e4SLinus Torvalds 
271da177e4SLinus Torvalds #define to_drv_attr(_attr) container_of(_attr, struct driver_attribute, attr)
281da177e4SLinus Torvalds #define to_driver(obj) container_of(obj, struct device_driver, kobj)
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds 
311da177e4SLinus Torvalds static ssize_t
321da177e4SLinus Torvalds drv_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
331da177e4SLinus Torvalds {
341da177e4SLinus Torvalds 	struct driver_attribute * drv_attr = to_drv_attr(attr);
351da177e4SLinus Torvalds 	struct device_driver * drv = to_driver(kobj);
364a0c20bfSDmitry Torokhov 	ssize_t ret = -EIO;
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds 	if (drv_attr->show)
391da177e4SLinus Torvalds 		ret = drv_attr->show(drv, buf);
401da177e4SLinus Torvalds 	return ret;
411da177e4SLinus Torvalds }
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds static ssize_t
441da177e4SLinus Torvalds drv_attr_store(struct kobject * kobj, struct attribute * attr,
451da177e4SLinus Torvalds 	       const char * buf, size_t count)
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->store)
521da177e4SLinus Torvalds 		ret = drv_attr->store(drv, buf, count);
531da177e4SLinus Torvalds 	return ret;
541da177e4SLinus Torvalds }
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds static struct sysfs_ops driver_sysfs_ops = {
571da177e4SLinus Torvalds 	.show	= drv_attr_show,
581da177e4SLinus Torvalds 	.store	= drv_attr_store,
591da177e4SLinus Torvalds };
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds static void driver_release(struct kobject * kobj)
631da177e4SLinus Torvalds {
641da177e4SLinus Torvalds 	struct device_driver * drv = to_driver(kobj);
651da177e4SLinus Torvalds 	complete(&drv->unloaded);
661da177e4SLinus Torvalds }
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds static struct kobj_type ktype_driver = {
691da177e4SLinus Torvalds 	.sysfs_ops	= &driver_sysfs_ops,
701da177e4SLinus Torvalds 	.release	= driver_release,
711da177e4SLinus Torvalds };
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds /*
751da177e4SLinus Torvalds  * sysfs bindings for buses
761da177e4SLinus Torvalds  */
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds static ssize_t
801da177e4SLinus Torvalds bus_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
811da177e4SLinus Torvalds {
821da177e4SLinus Torvalds 	struct bus_attribute * bus_attr = to_bus_attr(attr);
831da177e4SLinus Torvalds 	struct bus_type * bus = to_bus(kobj);
841da177e4SLinus Torvalds 	ssize_t ret = 0;
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds 	if (bus_attr->show)
871da177e4SLinus Torvalds 		ret = bus_attr->show(bus, buf);
881da177e4SLinus Torvalds 	return ret;
891da177e4SLinus Torvalds }
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds static ssize_t
921da177e4SLinus Torvalds bus_attr_store(struct kobject * kobj, struct attribute * attr,
931da177e4SLinus Torvalds 	       const char * buf, size_t count)
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->store)
1001da177e4SLinus Torvalds 		ret = bus_attr->store(bus, buf, count);
1011da177e4SLinus Torvalds 	return ret;
1021da177e4SLinus Torvalds }
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds static struct sysfs_ops bus_sysfs_ops = {
1051da177e4SLinus Torvalds 	.show	= bus_attr_show,
1061da177e4SLinus Torvalds 	.store	= bus_attr_store,
1071da177e4SLinus Torvalds };
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds int bus_create_file(struct bus_type * bus, struct bus_attribute * attr)
1101da177e4SLinus Torvalds {
1111da177e4SLinus Torvalds 	int error;
1121da177e4SLinus Torvalds 	if (get_bus(bus)) {
1131da177e4SLinus Torvalds 		error = sysfs_create_file(&bus->subsys.kset.kobj, &attr->attr);
1141da177e4SLinus Torvalds 		put_bus(bus);
1151da177e4SLinus Torvalds 	} else
1161da177e4SLinus Torvalds 		error = -EINVAL;
1171da177e4SLinus Torvalds 	return error;
1181da177e4SLinus Torvalds }
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds void bus_remove_file(struct bus_type * bus, struct bus_attribute * attr)
1211da177e4SLinus Torvalds {
1221da177e4SLinus Torvalds 	if (get_bus(bus)) {
1231da177e4SLinus Torvalds 		sysfs_remove_file(&bus->subsys.kset.kobj, &attr->attr);
1241da177e4SLinus Torvalds 		put_bus(bus);
1251da177e4SLinus Torvalds 	}
1261da177e4SLinus Torvalds }
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds static struct kobj_type ktype_bus = {
1291da177e4SLinus Torvalds 	.sysfs_ops	= &bus_sysfs_ops,
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds };
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds decl_subsys(bus, &ktype_bus, NULL);
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds 
136*2b08c8d0SAlan Stern /* Manually detach a device from its associated driver. */
137151ef38fSGreg Kroah-Hartman static int driver_helper(struct device *dev, void *data)
138151ef38fSGreg Kroah-Hartman {
139151ef38fSGreg Kroah-Hartman 	const char *name = data;
140151ef38fSGreg Kroah-Hartman 
141151ef38fSGreg Kroah-Hartman 	if (strcmp(name, dev->bus_id) == 0)
142151ef38fSGreg Kroah-Hartman 		return 1;
143151ef38fSGreg Kroah-Hartman 	return 0;
144151ef38fSGreg Kroah-Hartman }
145151ef38fSGreg Kroah-Hartman 
146151ef38fSGreg Kroah-Hartman static ssize_t driver_unbind(struct device_driver *drv,
147151ef38fSGreg Kroah-Hartman 			     const char *buf, size_t count)
148151ef38fSGreg Kroah-Hartman {
149151ef38fSGreg Kroah-Hartman 	struct bus_type *bus = get_bus(drv->bus);
150151ef38fSGreg Kroah-Hartman 	struct device *dev;
151151ef38fSGreg Kroah-Hartman 	int err = -ENODEV;
152151ef38fSGreg Kroah-Hartman 
153151ef38fSGreg Kroah-Hartman 	dev = bus_find_device(bus, NULL, (void *)buf, driver_helper);
154*2b08c8d0SAlan Stern 	if (dev && dev->driver == drv) {
155151ef38fSGreg Kroah-Hartman 		device_release_driver(dev);
156151ef38fSGreg Kroah-Hartman 		err = count;
157151ef38fSGreg Kroah-Hartman 	}
158*2b08c8d0SAlan Stern 	put_device(dev);
159*2b08c8d0SAlan Stern 	put_bus(bus);
160151ef38fSGreg Kroah-Hartman 	return err;
161151ef38fSGreg Kroah-Hartman }
162151ef38fSGreg Kroah-Hartman static DRIVER_ATTR(unbind, S_IWUSR, NULL, driver_unbind);
163151ef38fSGreg Kroah-Hartman 
164afdce75fSGreg Kroah-Hartman /*
165afdce75fSGreg Kroah-Hartman  * Manually attach a device to a driver.
166afdce75fSGreg Kroah-Hartman  * Note: the driver must want to bind to the device,
167afdce75fSGreg Kroah-Hartman  * it is not possible to override the driver's id table.
168afdce75fSGreg Kroah-Hartman  */
169afdce75fSGreg Kroah-Hartman static ssize_t driver_bind(struct device_driver *drv,
170afdce75fSGreg Kroah-Hartman 			   const char *buf, size_t count)
171afdce75fSGreg Kroah-Hartman {
172afdce75fSGreg Kroah-Hartman 	struct bus_type *bus = get_bus(drv->bus);
173afdce75fSGreg Kroah-Hartman 	struct device *dev;
174afdce75fSGreg Kroah-Hartman 	int err = -ENODEV;
175afdce75fSGreg Kroah-Hartman 
176afdce75fSGreg Kroah-Hartman 	dev = bus_find_device(bus, NULL, (void *)buf, driver_helper);
177*2b08c8d0SAlan Stern 	if (dev && dev->driver == NULL) {
178afdce75fSGreg Kroah-Hartman 		down(&dev->sem);
179afdce75fSGreg Kroah-Hartman 		err = driver_probe_device(drv, dev);
180afdce75fSGreg Kroah-Hartman 		up(&dev->sem);
181afdce75fSGreg Kroah-Hartman 	}
182*2b08c8d0SAlan Stern 	put_device(dev);
183*2b08c8d0SAlan Stern 	put_bus(bus);
184afdce75fSGreg Kroah-Hartman 	return err;
185afdce75fSGreg Kroah-Hartman }
186afdce75fSGreg Kroah-Hartman static DRIVER_ATTR(bind, S_IWUSR, NULL, driver_bind);
187afdce75fSGreg Kroah-Hartman 
188151ef38fSGreg Kroah-Hartman 
189465c7a3aSmochel@digitalimplant.org static struct device * next_device(struct klist_iter * i)
190465c7a3aSmochel@digitalimplant.org {
191465c7a3aSmochel@digitalimplant.org 	struct klist_node * n = klist_next(i);
192465c7a3aSmochel@digitalimplant.org 	return n ? container_of(n, struct device, knode_bus) : NULL;
193465c7a3aSmochel@digitalimplant.org }
194465c7a3aSmochel@digitalimplant.org 
1951da177e4SLinus Torvalds /**
1961da177e4SLinus Torvalds  *	bus_for_each_dev - device iterator.
1971da177e4SLinus Torvalds  *	@bus:	bus type.
1981da177e4SLinus Torvalds  *	@start:	device to start iterating from.
1991da177e4SLinus Torvalds  *	@data:	data for the callback.
2001da177e4SLinus Torvalds  *	@fn:	function to be called for each device.
2011da177e4SLinus Torvalds  *
2021da177e4SLinus Torvalds  *	Iterate over @bus's list of devices, and call @fn for each,
2031da177e4SLinus Torvalds  *	passing it @data. If @start is not NULL, we use that device to
2041da177e4SLinus Torvalds  *	begin iterating from.
2051da177e4SLinus Torvalds  *
2061da177e4SLinus Torvalds  *	We check the return of @fn each time. If it returns anything
2071da177e4SLinus Torvalds  *	other than 0, we break out and return that value.
2081da177e4SLinus Torvalds  *
2091da177e4SLinus Torvalds  *	NOTE: The device that returns a non-zero value is not retained
2101da177e4SLinus Torvalds  *	in any way, nor is its refcount incremented. If the caller needs
2111da177e4SLinus Torvalds  *	to retain this data, it should do, and increment the reference
2121da177e4SLinus Torvalds  *	count in the supplied callback.
2131da177e4SLinus Torvalds  */
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds int bus_for_each_dev(struct bus_type * bus, struct device * start,
2161da177e4SLinus Torvalds 		     void * data, int (*fn)(struct device *, void *))
2171da177e4SLinus Torvalds {
218465c7a3aSmochel@digitalimplant.org 	struct klist_iter i;
219465c7a3aSmochel@digitalimplant.org 	struct device * dev;
220465c7a3aSmochel@digitalimplant.org 	int error = 0;
2211da177e4SLinus Torvalds 
222465c7a3aSmochel@digitalimplant.org 	if (!bus)
223465c7a3aSmochel@digitalimplant.org 		return -EINVAL;
224465c7a3aSmochel@digitalimplant.org 
225465c7a3aSmochel@digitalimplant.org 	klist_iter_init_node(&bus->klist_devices, &i,
226465c7a3aSmochel@digitalimplant.org 			     (start ? &start->knode_bus : NULL));
227465c7a3aSmochel@digitalimplant.org 	while ((dev = next_device(&i)) && !error)
228465c7a3aSmochel@digitalimplant.org 		error = fn(dev, data);
229465c7a3aSmochel@digitalimplant.org 	klist_iter_exit(&i);
230465c7a3aSmochel@digitalimplant.org 	return error;
2311da177e4SLinus Torvalds }
2321da177e4SLinus Torvalds 
2330edb5860SCornelia Huck /**
2340edb5860SCornelia Huck  * bus_find_device - device iterator for locating a particular device.
2350edb5860SCornelia Huck  * @bus: bus type
2360edb5860SCornelia Huck  * @start: Device to begin with
2370edb5860SCornelia Huck  * @data: Data to pass to match function
2380edb5860SCornelia Huck  * @match: Callback function to check device
2390edb5860SCornelia Huck  *
2400edb5860SCornelia Huck  * This is similar to the bus_for_each_dev() function above, but it
2410edb5860SCornelia Huck  * returns a reference to a device that is 'found' for later use, as
2420edb5860SCornelia Huck  * determined by the @match callback.
2430edb5860SCornelia Huck  *
2440edb5860SCornelia Huck  * The callback should return 0 if the device doesn't match and non-zero
2450edb5860SCornelia Huck  * if it does.  If the callback returns non-zero, this function will
2460edb5860SCornelia Huck  * return to the caller and not iterate over any more devices.
2470edb5860SCornelia Huck  */
2480edb5860SCornelia Huck struct device * bus_find_device(struct bus_type *bus,
2490edb5860SCornelia Huck 				struct device *start, void *data,
2500edb5860SCornelia Huck 				int (*match)(struct device *, void *))
2510edb5860SCornelia Huck {
2520edb5860SCornelia Huck 	struct klist_iter i;
2530edb5860SCornelia Huck 	struct device *dev;
2540edb5860SCornelia Huck 
2550edb5860SCornelia Huck 	if (!bus)
2560edb5860SCornelia Huck 		return NULL;
2570edb5860SCornelia Huck 
2580edb5860SCornelia Huck 	klist_iter_init_node(&bus->klist_devices, &i,
2590edb5860SCornelia Huck 			     (start ? &start->knode_bus : NULL));
2600edb5860SCornelia Huck 	while ((dev = next_device(&i)))
2610edb5860SCornelia Huck 		if (match(dev, data) && get_device(dev))
2620edb5860SCornelia Huck 			break;
2630edb5860SCornelia Huck 	klist_iter_exit(&i);
2640edb5860SCornelia Huck 	return dev;
2650edb5860SCornelia Huck }
26638fdac3cSmochel@digitalimplant.org 
26738fdac3cSmochel@digitalimplant.org 
26838fdac3cSmochel@digitalimplant.org static struct device_driver * next_driver(struct klist_iter * i)
26938fdac3cSmochel@digitalimplant.org {
27038fdac3cSmochel@digitalimplant.org 	struct klist_node * n = klist_next(i);
27138fdac3cSmochel@digitalimplant.org 	return n ? container_of(n, struct device_driver, knode_bus) : NULL;
27238fdac3cSmochel@digitalimplant.org }
27338fdac3cSmochel@digitalimplant.org 
2741da177e4SLinus Torvalds /**
2751da177e4SLinus Torvalds  *	bus_for_each_drv - driver iterator
2761da177e4SLinus Torvalds  *	@bus:	bus we're dealing with.
2771da177e4SLinus Torvalds  *	@start:	driver to start iterating on.
2781da177e4SLinus Torvalds  *	@data:	data to pass to the callback.
2791da177e4SLinus Torvalds  *	@fn:	function to call for each driver.
2801da177e4SLinus Torvalds  *
2811da177e4SLinus Torvalds  *	This is nearly identical to the device iterator above.
2821da177e4SLinus Torvalds  *	We iterate over each driver that belongs to @bus, and call
2831da177e4SLinus Torvalds  *	@fn for each. If @fn returns anything but 0, we break out
2841da177e4SLinus Torvalds  *	and return it. If @start is not NULL, we use it as the head
2851da177e4SLinus Torvalds  *	of the list.
2861da177e4SLinus Torvalds  *
2871da177e4SLinus Torvalds  *	NOTE: we don't return the driver that returns a non-zero
2881da177e4SLinus Torvalds  *	value, nor do we leave the reference count incremented for that
2891da177e4SLinus Torvalds  *	driver. If the caller needs to know that info, it must set it
2901da177e4SLinus Torvalds  *	in the callback. It must also be sure to increment the refcount
2911da177e4SLinus Torvalds  *	so it doesn't disappear before returning to the caller.
2921da177e4SLinus Torvalds  */
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds int bus_for_each_drv(struct bus_type * bus, struct device_driver * start,
2951da177e4SLinus Torvalds 		     void * data, int (*fn)(struct device_driver *, void *))
2961da177e4SLinus Torvalds {
29738fdac3cSmochel@digitalimplant.org 	struct klist_iter i;
29838fdac3cSmochel@digitalimplant.org 	struct device_driver * drv;
29938fdac3cSmochel@digitalimplant.org 	int error = 0;
3001da177e4SLinus Torvalds 
30138fdac3cSmochel@digitalimplant.org 	if (!bus)
30238fdac3cSmochel@digitalimplant.org 		return -EINVAL;
30338fdac3cSmochel@digitalimplant.org 
30438fdac3cSmochel@digitalimplant.org 	klist_iter_init_node(&bus->klist_drivers, &i,
30538fdac3cSmochel@digitalimplant.org 			     start ? &start->knode_bus : NULL);
30638fdac3cSmochel@digitalimplant.org 	while ((drv = next_driver(&i)) && !error)
30738fdac3cSmochel@digitalimplant.org 		error = fn(drv, data);
30838fdac3cSmochel@digitalimplant.org 	klist_iter_exit(&i);
30938fdac3cSmochel@digitalimplant.org 	return error;
3101da177e4SLinus Torvalds }
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds static int device_add_attrs(struct bus_type * bus, struct device * dev)
3131da177e4SLinus Torvalds {
3141da177e4SLinus Torvalds 	int error = 0;
3151da177e4SLinus Torvalds 	int i;
3161da177e4SLinus Torvalds 
3171da177e4SLinus Torvalds 	if (bus->dev_attrs) {
3181da177e4SLinus Torvalds 		for (i = 0; attr_name(bus->dev_attrs[i]); i++) {
3191da177e4SLinus Torvalds 			error = device_create_file(dev,&bus->dev_attrs[i]);
3201da177e4SLinus Torvalds 			if (error)
3211da177e4SLinus Torvalds 				goto Err;
3221da177e4SLinus Torvalds 		}
3231da177e4SLinus Torvalds 	}
3241da177e4SLinus Torvalds  Done:
3251da177e4SLinus Torvalds 	return error;
3261da177e4SLinus Torvalds  Err:
3271da177e4SLinus Torvalds 	while (--i >= 0)
3281da177e4SLinus Torvalds 		device_remove_file(dev,&bus->dev_attrs[i]);
3291da177e4SLinus Torvalds 	goto Done;
3301da177e4SLinus Torvalds }
3311da177e4SLinus Torvalds 
3321da177e4SLinus Torvalds 
3331da177e4SLinus Torvalds static void device_remove_attrs(struct bus_type * bus, struct device * dev)
3341da177e4SLinus Torvalds {
3351da177e4SLinus Torvalds 	int i;
3361da177e4SLinus Torvalds 
3371da177e4SLinus Torvalds 	if (bus->dev_attrs) {
3381da177e4SLinus Torvalds 		for (i = 0; attr_name(bus->dev_attrs[i]); i++)
3391da177e4SLinus Torvalds 			device_remove_file(dev,&bus->dev_attrs[i]);
3401da177e4SLinus Torvalds 	}
3411da177e4SLinus Torvalds }
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds 
3441da177e4SLinus Torvalds /**
3451da177e4SLinus Torvalds  *	bus_add_device - add device to bus
3461da177e4SLinus Torvalds  *	@dev:	device being added
3471da177e4SLinus Torvalds  *
3481da177e4SLinus Torvalds  *	- Add the device to its bus's list of devices.
3491da177e4SLinus Torvalds  *	- Try to attach to driver.
3501da177e4SLinus Torvalds  *	- Create link to device's physical location.
3511da177e4SLinus Torvalds  */
3521da177e4SLinus Torvalds int bus_add_device(struct device * dev)
3531da177e4SLinus Torvalds {
3541da177e4SLinus Torvalds 	struct bus_type * bus = get_bus(dev->bus);
3551da177e4SLinus Torvalds 	int error = 0;
3561da177e4SLinus Torvalds 
3571da177e4SLinus Torvalds 	if (bus) {
3581da177e4SLinus Torvalds 		pr_debug("bus %s: add device %s\n", bus->name, dev->bus_id);
359d377e85bSGreg Kroah-Hartman 		device_attach(dev);
360d856f1e3SJames Bottomley 		klist_add_tail(&dev->knode_bus, &bus->klist_devices);
361ca2b94baSHannes Reinecke 		error = device_add_attrs(bus, dev);
362ca2b94baSHannes Reinecke 		if (!error) {
3631da177e4SLinus Torvalds 			sysfs_create_link(&bus->devices.kobj, &dev->kobj, dev->bus_id);
3641da177e4SLinus Torvalds 			sysfs_create_link(&dev->kobj, &dev->bus->subsys.kset.kobj, "bus");
3651da177e4SLinus Torvalds 		}
366ca2b94baSHannes Reinecke 	}
3671da177e4SLinus Torvalds 	return error;
3681da177e4SLinus Torvalds }
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds /**
3711da177e4SLinus Torvalds  *	bus_remove_device - remove device from bus
3721da177e4SLinus Torvalds  *	@dev:	device to be removed
3731da177e4SLinus Torvalds  *
3741da177e4SLinus Torvalds  *	- Remove symlink from bus's directory.
3751da177e4SLinus Torvalds  *	- Delete device from bus's list.
3761da177e4SLinus Torvalds  *	- Detach from its driver.
3771da177e4SLinus Torvalds  *	- Drop reference taken in bus_add_device().
3781da177e4SLinus Torvalds  */
3791da177e4SLinus Torvalds void bus_remove_device(struct device * dev)
3801da177e4SLinus Torvalds {
3811da177e4SLinus Torvalds 	if (dev->bus) {
3821da177e4SLinus Torvalds 		sysfs_remove_link(&dev->kobj, "bus");
3831da177e4SLinus Torvalds 		sysfs_remove_link(&dev->bus->devices.kobj, dev->bus_id);
3841da177e4SLinus Torvalds 		device_remove_attrs(dev->bus, dev);
385465c7a3aSmochel@digitalimplant.org 		klist_remove(&dev->knode_bus);
3861da177e4SLinus Torvalds 		pr_debug("bus %s: remove device %s\n", dev->bus->name, dev->bus_id);
3871da177e4SLinus Torvalds 		device_release_driver(dev);
3881da177e4SLinus Torvalds 		put_bus(dev->bus);
3891da177e4SLinus Torvalds 	}
3901da177e4SLinus Torvalds }
3911da177e4SLinus Torvalds 
3921da177e4SLinus Torvalds static int driver_add_attrs(struct bus_type * bus, struct device_driver * drv)
3931da177e4SLinus Torvalds {
3941da177e4SLinus Torvalds 	int error = 0;
3951da177e4SLinus Torvalds 	int i;
3961da177e4SLinus Torvalds 
3971da177e4SLinus Torvalds 	if (bus->drv_attrs) {
3981da177e4SLinus Torvalds 		for (i = 0; attr_name(bus->drv_attrs[i]); i++) {
3991da177e4SLinus Torvalds 			error = driver_create_file(drv, &bus->drv_attrs[i]);
4001da177e4SLinus Torvalds 			if (error)
4011da177e4SLinus Torvalds 				goto Err;
4021da177e4SLinus Torvalds 		}
4031da177e4SLinus Torvalds 	}
4041da177e4SLinus Torvalds  Done:
4051da177e4SLinus Torvalds 	return error;
4061da177e4SLinus Torvalds  Err:
4071da177e4SLinus Torvalds 	while (--i >= 0)
4081da177e4SLinus Torvalds 		driver_remove_file(drv, &bus->drv_attrs[i]);
4091da177e4SLinus Torvalds 	goto Done;
4101da177e4SLinus Torvalds }
4111da177e4SLinus Torvalds 
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds static void driver_remove_attrs(struct bus_type * bus, struct device_driver * drv)
4141da177e4SLinus Torvalds {
4151da177e4SLinus Torvalds 	int i;
4161da177e4SLinus Torvalds 
4171da177e4SLinus Torvalds 	if (bus->drv_attrs) {
4181da177e4SLinus Torvalds 		for (i = 0; attr_name(bus->drv_attrs[i]); i++)
4191da177e4SLinus Torvalds 			driver_remove_file(drv, &bus->drv_attrs[i]);
4201da177e4SLinus Torvalds 	}
4211da177e4SLinus Torvalds }
4221da177e4SLinus Torvalds 
4231da177e4SLinus Torvalds 
4241da177e4SLinus Torvalds /**
4251da177e4SLinus Torvalds  *	bus_add_driver - Add a driver to the bus.
4261da177e4SLinus Torvalds  *	@drv:	driver.
4271da177e4SLinus Torvalds  *
4281da177e4SLinus Torvalds  */
4291da177e4SLinus Torvalds int bus_add_driver(struct device_driver * drv)
4301da177e4SLinus Torvalds {
4311da177e4SLinus Torvalds 	struct bus_type * bus = get_bus(drv->bus);
4321da177e4SLinus Torvalds 	int error = 0;
4331da177e4SLinus Torvalds 
4341da177e4SLinus Torvalds 	if (bus) {
4351da177e4SLinus Torvalds 		pr_debug("bus %s: add driver %s\n", bus->name, drv->name);
4361da177e4SLinus Torvalds 		error = kobject_set_name(&drv->kobj, "%s", drv->name);
4371da177e4SLinus Torvalds 		if (error) {
4381da177e4SLinus Torvalds 			put_bus(bus);
4391da177e4SLinus Torvalds 			return error;
4401da177e4SLinus Torvalds 		}
4411da177e4SLinus Torvalds 		drv->kobj.kset = &bus->drivers;
4421da177e4SLinus Torvalds 		if ((error = kobject_register(&drv->kobj))) {
4431da177e4SLinus Torvalds 			put_bus(bus);
4441da177e4SLinus Torvalds 			return error;
4451da177e4SLinus Torvalds 		}
4461da177e4SLinus Torvalds 
4471da177e4SLinus Torvalds 		driver_attach(drv);
448d856f1e3SJames Bottomley 		klist_add_tail(&drv->knode_bus, &bus->klist_drivers);
4491da177e4SLinus Torvalds 		module_add_driver(drv->owner, drv);
4501da177e4SLinus Torvalds 
4511da177e4SLinus Torvalds 		driver_add_attrs(bus, drv);
452151ef38fSGreg Kroah-Hartman 		driver_create_file(drv, &driver_attr_unbind);
453afdce75fSGreg Kroah-Hartman 		driver_create_file(drv, &driver_attr_bind);
4541da177e4SLinus Torvalds 	}
4551da177e4SLinus Torvalds 	return error;
4561da177e4SLinus Torvalds }
4571da177e4SLinus Torvalds 
4581da177e4SLinus Torvalds 
4591da177e4SLinus Torvalds /**
4601da177e4SLinus Torvalds  *	bus_remove_driver - delete driver from bus's knowledge.
4611da177e4SLinus Torvalds  *	@drv:	driver.
4621da177e4SLinus Torvalds  *
4631da177e4SLinus Torvalds  *	Detach the driver from the devices it controls, and remove
4641da177e4SLinus Torvalds  *	it from its bus's list of drivers. Finally, we drop the reference
4651da177e4SLinus Torvalds  *	to the bus we took in bus_add_driver().
4661da177e4SLinus Torvalds  */
4671da177e4SLinus Torvalds 
4681da177e4SLinus Torvalds void bus_remove_driver(struct device_driver * drv)
4691da177e4SLinus Torvalds {
4701da177e4SLinus Torvalds 	if (drv->bus) {
471afdce75fSGreg Kroah-Hartman 		driver_remove_file(drv, &driver_attr_bind);
472151ef38fSGreg Kroah-Hartman 		driver_remove_file(drv, &driver_attr_unbind);
4731da177e4SLinus Torvalds 		driver_remove_attrs(drv->bus, drv);
47438fdac3cSmochel@digitalimplant.org 		klist_remove(&drv->knode_bus);
4751da177e4SLinus Torvalds 		pr_debug("bus %s: remove driver %s\n", drv->bus->name, drv->name);
4761da177e4SLinus Torvalds 		driver_detach(drv);
4771da177e4SLinus Torvalds 		module_remove_driver(drv);
4781da177e4SLinus Torvalds 		kobject_unregister(&drv->kobj);
4791da177e4SLinus Torvalds 		put_bus(drv->bus);
4801da177e4SLinus Torvalds 	}
4811da177e4SLinus Torvalds }
4821da177e4SLinus Torvalds 
4831da177e4SLinus Torvalds 
4841da177e4SLinus Torvalds /* Helper for bus_rescan_devices's iter */
4851da177e4SLinus Torvalds static int bus_rescan_devices_helper(struct device *dev, void *data)
4861da177e4SLinus Torvalds {
48723d3d602SGreg Kroah-Hartman 	if (!dev->driver)
48823d3d602SGreg Kroah-Hartman 		device_attach(dev);
4891da177e4SLinus Torvalds 	return 0;
4901da177e4SLinus Torvalds }
4911da177e4SLinus Torvalds 
4921da177e4SLinus Torvalds /**
4931da177e4SLinus Torvalds  * bus_rescan_devices - rescan devices on the bus for possible drivers
4941da177e4SLinus Torvalds  * @bus: the bus to scan.
4951da177e4SLinus Torvalds  *
4961da177e4SLinus Torvalds  * This function will look for devices on the bus with no driver
49723d3d602SGreg Kroah-Hartman  * attached and rescan it against existing drivers to see if it matches
49823d3d602SGreg Kroah-Hartman  * any by calling device_attach() for the unbound devices.
4991da177e4SLinus Torvalds  */
50023d3d602SGreg Kroah-Hartman void bus_rescan_devices(struct bus_type * bus)
5011da177e4SLinus Torvalds {
50223d3d602SGreg Kroah-Hartman 	bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper);
5031da177e4SLinus Torvalds }
5041da177e4SLinus Torvalds 
5051da177e4SLinus Torvalds 
5061da177e4SLinus Torvalds struct bus_type * get_bus(struct bus_type * bus)
5071da177e4SLinus Torvalds {
5081da177e4SLinus Torvalds 	return bus ? container_of(subsys_get(&bus->subsys), struct bus_type, subsys) : NULL;
5091da177e4SLinus Torvalds }
5101da177e4SLinus Torvalds 
5111da177e4SLinus Torvalds void put_bus(struct bus_type * bus)
5121da177e4SLinus Torvalds {
5131da177e4SLinus Torvalds 	subsys_put(&bus->subsys);
5141da177e4SLinus Torvalds }
5151da177e4SLinus Torvalds 
5161da177e4SLinus Torvalds 
5171da177e4SLinus Torvalds /**
5181da177e4SLinus Torvalds  *	find_bus - locate bus by name.
5191da177e4SLinus Torvalds  *	@name:	name of bus.
5201da177e4SLinus Torvalds  *
5211da177e4SLinus Torvalds  *	Call kset_find_obj() to iterate over list of buses to
5221da177e4SLinus Torvalds  *	find a bus by name. Return bus if found.
5231da177e4SLinus Torvalds  *
5241da177e4SLinus Torvalds  *	Note that kset_find_obj increments bus' reference count.
5251da177e4SLinus Torvalds  */
5261da177e4SLinus Torvalds 
5271da177e4SLinus Torvalds struct bus_type * find_bus(char * name)
5281da177e4SLinus Torvalds {
5291da177e4SLinus Torvalds 	struct kobject * k = kset_find_obj(&bus_subsys.kset, name);
5301da177e4SLinus Torvalds 	return k ? to_bus(k) : NULL;
5311da177e4SLinus Torvalds }
5321da177e4SLinus Torvalds 
5331da177e4SLinus Torvalds 
5341da177e4SLinus Torvalds /**
5351da177e4SLinus Torvalds  *	bus_add_attrs - Add default attributes for this bus.
5361da177e4SLinus Torvalds  *	@bus:	Bus that has just been registered.
5371da177e4SLinus Torvalds  */
5381da177e4SLinus Torvalds 
5391da177e4SLinus Torvalds static int bus_add_attrs(struct bus_type * bus)
5401da177e4SLinus Torvalds {
5411da177e4SLinus Torvalds 	int error = 0;
5421da177e4SLinus Torvalds 	int i;
5431da177e4SLinus Torvalds 
5441da177e4SLinus Torvalds 	if (bus->bus_attrs) {
5451da177e4SLinus Torvalds 		for (i = 0; attr_name(bus->bus_attrs[i]); i++) {
5461da177e4SLinus Torvalds 			if ((error = bus_create_file(bus,&bus->bus_attrs[i])))
5471da177e4SLinus Torvalds 				goto Err;
5481da177e4SLinus Torvalds 		}
5491da177e4SLinus Torvalds 	}
5501da177e4SLinus Torvalds  Done:
5511da177e4SLinus Torvalds 	return error;
5521da177e4SLinus Torvalds  Err:
5531da177e4SLinus Torvalds 	while (--i >= 0)
5541da177e4SLinus Torvalds 		bus_remove_file(bus,&bus->bus_attrs[i]);
5551da177e4SLinus Torvalds 	goto Done;
5561da177e4SLinus Torvalds }
5571da177e4SLinus Torvalds 
5581da177e4SLinus Torvalds static void bus_remove_attrs(struct bus_type * bus)
5591da177e4SLinus Torvalds {
5601da177e4SLinus Torvalds 	int i;
5611da177e4SLinus Torvalds 
5621da177e4SLinus Torvalds 	if (bus->bus_attrs) {
5631da177e4SLinus Torvalds 		for (i = 0; attr_name(bus->bus_attrs[i]); i++)
5641da177e4SLinus Torvalds 			bus_remove_file(bus,&bus->bus_attrs[i]);
5651da177e4SLinus Torvalds 	}
5661da177e4SLinus Torvalds }
5671da177e4SLinus Torvalds 
56834bb61f9SJames Bottomley static void klist_devices_get(struct klist_node *n)
56934bb61f9SJames Bottomley {
57034bb61f9SJames Bottomley 	struct device *dev = container_of(n, struct device, knode_bus);
57134bb61f9SJames Bottomley 
57234bb61f9SJames Bottomley 	get_device(dev);
57334bb61f9SJames Bottomley }
57434bb61f9SJames Bottomley 
57534bb61f9SJames Bottomley static void klist_devices_put(struct klist_node *n)
57634bb61f9SJames Bottomley {
57734bb61f9SJames Bottomley 	struct device *dev = container_of(n, struct device, knode_bus);
57834bb61f9SJames Bottomley 
57934bb61f9SJames Bottomley 	put_device(dev);
58034bb61f9SJames Bottomley }
58134bb61f9SJames Bottomley 
58234bb61f9SJames Bottomley static void klist_drivers_get(struct klist_node *n)
58334bb61f9SJames Bottomley {
58434bb61f9SJames Bottomley 	struct device_driver *drv = container_of(n, struct device_driver,
58534bb61f9SJames Bottomley 						 knode_bus);
58634bb61f9SJames Bottomley 
58734bb61f9SJames Bottomley 	get_driver(drv);
58834bb61f9SJames Bottomley }
58934bb61f9SJames Bottomley 
59034bb61f9SJames Bottomley static void klist_drivers_put(struct klist_node *n)
59134bb61f9SJames Bottomley {
59234bb61f9SJames Bottomley 	struct device_driver *drv = container_of(n, struct device_driver,
59334bb61f9SJames Bottomley 						 knode_bus);
59434bb61f9SJames Bottomley 
59534bb61f9SJames Bottomley 	put_driver(drv);
59634bb61f9SJames Bottomley }
59734bb61f9SJames Bottomley 
5981da177e4SLinus Torvalds /**
5991da177e4SLinus Torvalds  *	bus_register - register a bus with the system.
6001da177e4SLinus Torvalds  *	@bus:	bus.
6011da177e4SLinus Torvalds  *
6021da177e4SLinus Torvalds  *	Once we have that, we registered the bus with the kobject
6031da177e4SLinus Torvalds  *	infrastructure, then register the children subsystems it has:
6041da177e4SLinus Torvalds  *	the devices and drivers that belong to the bus.
6051da177e4SLinus Torvalds  */
6061da177e4SLinus Torvalds int bus_register(struct bus_type * bus)
6071da177e4SLinus Torvalds {
6081da177e4SLinus Torvalds 	int retval;
6091da177e4SLinus Torvalds 
6101da177e4SLinus Torvalds 	retval = kobject_set_name(&bus->subsys.kset.kobj, "%s", bus->name);
6111da177e4SLinus Torvalds 	if (retval)
6121da177e4SLinus Torvalds 		goto out;
6131da177e4SLinus Torvalds 
6141da177e4SLinus Torvalds 	subsys_set_kset(bus, bus_subsys);
6151da177e4SLinus Torvalds 	retval = subsystem_register(&bus->subsys);
6161da177e4SLinus Torvalds 	if (retval)
6171da177e4SLinus Torvalds 		goto out;
6181da177e4SLinus Torvalds 
6191da177e4SLinus Torvalds 	kobject_set_name(&bus->devices.kobj, "devices");
6201da177e4SLinus Torvalds 	bus->devices.subsys = &bus->subsys;
6211da177e4SLinus Torvalds 	retval = kset_register(&bus->devices);
6221da177e4SLinus Torvalds 	if (retval)
6231da177e4SLinus Torvalds 		goto bus_devices_fail;
6241da177e4SLinus Torvalds 
6251da177e4SLinus Torvalds 	kobject_set_name(&bus->drivers.kobj, "drivers");
6261da177e4SLinus Torvalds 	bus->drivers.subsys = &bus->subsys;
6271da177e4SLinus Torvalds 	bus->drivers.ktype = &ktype_driver;
6281da177e4SLinus Torvalds 	retval = kset_register(&bus->drivers);
6291da177e4SLinus Torvalds 	if (retval)
6301da177e4SLinus Torvalds 		goto bus_drivers_fail;
631465c7a3aSmochel@digitalimplant.org 
63234bb61f9SJames Bottomley 	klist_init(&bus->klist_devices, klist_devices_get, klist_devices_put);
63334bb61f9SJames Bottomley 	klist_init(&bus->klist_drivers, klist_drivers_get, klist_drivers_put);
6341da177e4SLinus Torvalds 	bus_add_attrs(bus);
6351da177e4SLinus Torvalds 
6361da177e4SLinus Torvalds 	pr_debug("bus type '%s' registered\n", bus->name);
6371da177e4SLinus Torvalds 	return 0;
6381da177e4SLinus Torvalds 
6391da177e4SLinus Torvalds bus_drivers_fail:
6401da177e4SLinus Torvalds 	kset_unregister(&bus->devices);
6411da177e4SLinus Torvalds bus_devices_fail:
6421da177e4SLinus Torvalds 	subsystem_unregister(&bus->subsys);
6431da177e4SLinus Torvalds out:
6441da177e4SLinus Torvalds 	return retval;
6451da177e4SLinus Torvalds }
6461da177e4SLinus Torvalds 
6471da177e4SLinus Torvalds 
6481da177e4SLinus Torvalds /**
6491da177e4SLinus Torvalds  *	bus_unregister - remove a bus from the system
6501da177e4SLinus Torvalds  *	@bus:	bus.
6511da177e4SLinus Torvalds  *
6521da177e4SLinus Torvalds  *	Unregister the child subsystems and the bus itself.
6531da177e4SLinus Torvalds  *	Finally, we call put_bus() to release the refcount
6541da177e4SLinus Torvalds  */
6551da177e4SLinus Torvalds void bus_unregister(struct bus_type * bus)
6561da177e4SLinus Torvalds {
6571da177e4SLinus Torvalds 	pr_debug("bus %s: unregistering\n", bus->name);
6581da177e4SLinus Torvalds 	bus_remove_attrs(bus);
6591da177e4SLinus Torvalds 	kset_unregister(&bus->drivers);
6601da177e4SLinus Torvalds 	kset_unregister(&bus->devices);
6611da177e4SLinus Torvalds 	subsystem_unregister(&bus->subsys);
6621da177e4SLinus Torvalds }
6631da177e4SLinus Torvalds 
6641da177e4SLinus Torvalds int __init buses_init(void)
6651da177e4SLinus Torvalds {
6661da177e4SLinus Torvalds 	return subsystem_register(&bus_subsys);
6671da177e4SLinus Torvalds }
6681da177e4SLinus Torvalds 
6691da177e4SLinus Torvalds 
6701da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(bus_for_each_dev);
6710edb5860SCornelia Huck EXPORT_SYMBOL_GPL(bus_find_device);
6721da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(bus_for_each_drv);
6731da177e4SLinus Torvalds 
6741da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(bus_add_device);
6751da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(bus_remove_device);
6761da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(bus_register);
6771da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(bus_unregister);
6781da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(bus_rescan_devices);
6791da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(get_bus);
6801da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(put_bus);
6811da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(find_bus);
6821da177e4SLinus Torvalds 
6831da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(bus_create_file);
6841da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(bus_remove_file);
685