xref: /linux/drivers/base/bus.c (revision 5a0e3ad6af8660be21ca98a971cd00f331318c05)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * bus.c - bus driver management
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Copyright (c) 2002-3 Patrick Mochel
51da177e4SLinus Torvalds  * Copyright (c) 2002-3 Open Source Development Labs
6e5dd1278SGreg Kroah-Hartman  * Copyright (c) 2007 Greg Kroah-Hartman <gregkh@suse.de>
7e5dd1278SGreg Kroah-Hartman  * Copyright (c) 2007 Novell Inc.
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  * This file is released under the GPLv2
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  */
121da177e4SLinus Torvalds 
131da177e4SLinus Torvalds #include <linux/device.h>
141da177e4SLinus Torvalds #include <linux/module.h>
151da177e4SLinus Torvalds #include <linux/errno.h>
16*5a0e3ad6STejun Heo #include <linux/slab.h>
171da177e4SLinus Torvalds #include <linux/init.h>
181da177e4SLinus Torvalds #include <linux/string.h>
191da177e4SLinus Torvalds #include "base.h"
201da177e4SLinus Torvalds #include "power/power.h"
211da177e4SLinus Torvalds 
221da177e4SLinus Torvalds #define to_bus_attr(_attr) container_of(_attr, struct bus_attribute, attr)
23c6f7e72aSGreg Kroah-Hartman #define to_bus(obj) container_of(obj, struct bus_type_private, subsys.kobj)
241da177e4SLinus Torvalds 
251da177e4SLinus Torvalds /*
261da177e4SLinus Torvalds  * sysfs bindings for drivers
271da177e4SLinus Torvalds  */
281da177e4SLinus Torvalds 
291da177e4SLinus Torvalds #define to_drv_attr(_attr) container_of(_attr, struct driver_attribute, attr)
301da177e4SLinus Torvalds 
311da177e4SLinus Torvalds 
32b8c5cec2SKay Sievers static int __must_check bus_rescan_devices_helper(struct device *dev,
33b8c5cec2SKay Sievers 						void *data);
34b8c5cec2SKay Sievers 
355901d014SGreg Kroah-Hartman static struct bus_type *bus_get(struct bus_type *bus)
365901d014SGreg Kroah-Hartman {
37c6f7e72aSGreg Kroah-Hartman 	if (bus) {
38c6f7e72aSGreg Kroah-Hartman 		kset_get(&bus->p->subsys);
39c6f7e72aSGreg Kroah-Hartman 		return bus;
40c6f7e72aSGreg Kroah-Hartman 	}
41c6f7e72aSGreg Kroah-Hartman 	return NULL;
425901d014SGreg Kroah-Hartman }
435901d014SGreg Kroah-Hartman 
44fc1ede58SGreg Kroah-Hartman static void bus_put(struct bus_type *bus)
45fc1ede58SGreg Kroah-Hartman {
46c6f7e72aSGreg Kroah-Hartman 	if (bus)
47c6f7e72aSGreg Kroah-Hartman 		kset_put(&bus->p->subsys);
48fc1ede58SGreg Kroah-Hartman }
49fc1ede58SGreg Kroah-Hartman 
504a3ad20cSGreg Kroah-Hartman static ssize_t drv_attr_show(struct kobject *kobj, struct attribute *attr,
514a3ad20cSGreg Kroah-Hartman 			     char *buf)
521da177e4SLinus Torvalds {
531da177e4SLinus Torvalds 	struct driver_attribute *drv_attr = to_drv_attr(attr);
54e5dd1278SGreg Kroah-Hartman 	struct driver_private *drv_priv = to_driver(kobj);
554a0c20bfSDmitry Torokhov 	ssize_t ret = -EIO;
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds 	if (drv_attr->show)
58e5dd1278SGreg Kroah-Hartman 		ret = drv_attr->show(drv_priv->driver, buf);
591da177e4SLinus Torvalds 	return ret;
601da177e4SLinus Torvalds }
611da177e4SLinus Torvalds 
624a3ad20cSGreg Kroah-Hartman static ssize_t drv_attr_store(struct kobject *kobj, struct attribute *attr,
631da177e4SLinus Torvalds 			      const char *buf, size_t count)
641da177e4SLinus Torvalds {
651da177e4SLinus Torvalds 	struct driver_attribute *drv_attr = to_drv_attr(attr);
66e5dd1278SGreg Kroah-Hartman 	struct driver_private *drv_priv = to_driver(kobj);
674a0c20bfSDmitry Torokhov 	ssize_t ret = -EIO;
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds 	if (drv_attr->store)
70e5dd1278SGreg Kroah-Hartman 		ret = drv_attr->store(drv_priv->driver, buf, count);
711da177e4SLinus Torvalds 	return ret;
721da177e4SLinus Torvalds }
731da177e4SLinus Torvalds 
7452cf25d0SEmese Revfy static const struct sysfs_ops driver_sysfs_ops = {
751da177e4SLinus Torvalds 	.show	= drv_attr_show,
761da177e4SLinus Torvalds 	.store	= drv_attr_store,
771da177e4SLinus Torvalds };
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds static void driver_release(struct kobject *kobj)
801da177e4SLinus Torvalds {
81e5dd1278SGreg Kroah-Hartman 	struct driver_private *drv_priv = to_driver(kobj);
82e5dd1278SGreg Kroah-Hartman 
832b3a302aSHarvey Harrison 	pr_debug("driver: '%s': %s\n", kobject_name(kobj), __func__);
84e5dd1278SGreg Kroah-Hartman 	kfree(drv_priv);
851da177e4SLinus Torvalds }
861da177e4SLinus Torvalds 
87a1148fb0SGreg Kroah-Hartman static struct kobj_type driver_ktype = {
881da177e4SLinus Torvalds 	.sysfs_ops	= &driver_sysfs_ops,
891da177e4SLinus Torvalds 	.release	= driver_release,
901da177e4SLinus Torvalds };
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds /*
931da177e4SLinus Torvalds  * sysfs bindings for buses
941da177e4SLinus Torvalds  */
954a3ad20cSGreg Kroah-Hartman static ssize_t bus_attr_show(struct kobject *kobj, struct attribute *attr,
964a3ad20cSGreg Kroah-Hartman 			     char *buf)
971da177e4SLinus Torvalds {
981da177e4SLinus Torvalds 	struct bus_attribute *bus_attr = to_bus_attr(attr);
99c6f7e72aSGreg Kroah-Hartman 	struct bus_type_private *bus_priv = to_bus(kobj);
1001da177e4SLinus Torvalds 	ssize_t ret = 0;
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds 	if (bus_attr->show)
103c6f7e72aSGreg Kroah-Hartman 		ret = bus_attr->show(bus_priv->bus, buf);
1041da177e4SLinus Torvalds 	return ret;
1051da177e4SLinus Torvalds }
1061da177e4SLinus Torvalds 
1074a3ad20cSGreg Kroah-Hartman static ssize_t bus_attr_store(struct kobject *kobj, struct attribute *attr,
1081da177e4SLinus Torvalds 			      const char *buf, size_t count)
1091da177e4SLinus Torvalds {
1101da177e4SLinus Torvalds 	struct bus_attribute *bus_attr = to_bus_attr(attr);
111c6f7e72aSGreg Kroah-Hartman 	struct bus_type_private *bus_priv = to_bus(kobj);
1121da177e4SLinus Torvalds 	ssize_t ret = 0;
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds 	if (bus_attr->store)
115c6f7e72aSGreg Kroah-Hartman 		ret = bus_attr->store(bus_priv->bus, buf, count);
1161da177e4SLinus Torvalds 	return ret;
1171da177e4SLinus Torvalds }
1181da177e4SLinus Torvalds 
11952cf25d0SEmese Revfy static const struct sysfs_ops bus_sysfs_ops = {
1201da177e4SLinus Torvalds 	.show	= bus_attr_show,
1211da177e4SLinus Torvalds 	.store	= bus_attr_store,
1221da177e4SLinus Torvalds };
1231da177e4SLinus Torvalds 
1241da177e4SLinus Torvalds int bus_create_file(struct bus_type *bus, struct bus_attribute *attr)
1251da177e4SLinus Torvalds {
1261da177e4SLinus Torvalds 	int error;
1275901d014SGreg Kroah-Hartman 	if (bus_get(bus)) {
128c6f7e72aSGreg Kroah-Hartman 		error = sysfs_create_file(&bus->p->subsys.kobj, &attr->attr);
129fc1ede58SGreg Kroah-Hartman 		bus_put(bus);
1301da177e4SLinus Torvalds 	} else
1311da177e4SLinus Torvalds 		error = -EINVAL;
1321da177e4SLinus Torvalds 	return error;
1331da177e4SLinus Torvalds }
1344a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_create_file);
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds void bus_remove_file(struct bus_type *bus, struct bus_attribute *attr)
1371da177e4SLinus Torvalds {
1385901d014SGreg Kroah-Hartman 	if (bus_get(bus)) {
139c6f7e72aSGreg Kroah-Hartman 		sysfs_remove_file(&bus->p->subsys.kobj, &attr->attr);
140fc1ede58SGreg Kroah-Hartman 		bus_put(bus);
1411da177e4SLinus Torvalds 	}
1421da177e4SLinus Torvalds }
1434a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_remove_file);
1441da177e4SLinus Torvalds 
14580f03e34SKay Sievers static struct kobj_type bus_ktype = {
1461da177e4SLinus Torvalds 	.sysfs_ops	= &bus_sysfs_ops,
1471da177e4SLinus Torvalds };
1481da177e4SLinus Torvalds 
14980f03e34SKay Sievers static int bus_uevent_filter(struct kset *kset, struct kobject *kobj)
15080f03e34SKay Sievers {
15180f03e34SKay Sievers 	struct kobj_type *ktype = get_ktype(kobj);
15280f03e34SKay Sievers 
15380f03e34SKay Sievers 	if (ktype == &bus_ktype)
15480f03e34SKay Sievers 		return 1;
15580f03e34SKay Sievers 	return 0;
15680f03e34SKay Sievers }
15780f03e34SKay Sievers 
1589cd43611SEmese Revfy static const struct kset_uevent_ops bus_uevent_ops = {
15980f03e34SKay Sievers 	.filter = bus_uevent_filter,
16080f03e34SKay Sievers };
16180f03e34SKay Sievers 
16259a54833SGreg Kroah-Hartman static struct kset *bus_kset;
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds 
1652139bdd5SRussell King #ifdef CONFIG_HOTPLUG
1662b08c8d0SAlan Stern /* Manually detach a device from its associated driver. */
167151ef38fSGreg Kroah-Hartman static ssize_t driver_unbind(struct device_driver *drv,
168151ef38fSGreg Kroah-Hartman 			     const char *buf, size_t count)
169151ef38fSGreg Kroah-Hartman {
1705901d014SGreg Kroah-Hartman 	struct bus_type *bus = bus_get(drv->bus);
171151ef38fSGreg Kroah-Hartman 	struct device *dev;
172151ef38fSGreg Kroah-Hartman 	int err = -ENODEV;
173151ef38fSGreg Kroah-Hartman 
1741f9ffc04SGreg Kroah-Hartman 	dev = bus_find_device_by_name(bus, NULL, buf);
1752b08c8d0SAlan Stern 	if (dev && dev->driver == drv) {
176bf74ad5bSAlan Stern 		if (dev->parent)	/* Needed for USB */
1778e9394ceSGreg Kroah-Hartman 			device_lock(dev->parent);
178151ef38fSGreg Kroah-Hartman 		device_release_driver(dev);
179bf74ad5bSAlan Stern 		if (dev->parent)
1808e9394ceSGreg Kroah-Hartman 			device_unlock(dev->parent);
181151ef38fSGreg Kroah-Hartman 		err = count;
182151ef38fSGreg Kroah-Hartman 	}
1832b08c8d0SAlan Stern 	put_device(dev);
184fc1ede58SGreg Kroah-Hartman 	bus_put(bus);
185151ef38fSGreg Kroah-Hartman 	return err;
186151ef38fSGreg Kroah-Hartman }
187151ef38fSGreg Kroah-Hartman static DRIVER_ATTR(unbind, S_IWUSR, NULL, driver_unbind);
188151ef38fSGreg Kroah-Hartman 
189afdce75fSGreg Kroah-Hartman /*
190afdce75fSGreg Kroah-Hartman  * Manually attach a device to a driver.
191afdce75fSGreg Kroah-Hartman  * Note: the driver must want to bind to the device,
192afdce75fSGreg Kroah-Hartman  * it is not possible to override the driver's id table.
193afdce75fSGreg Kroah-Hartman  */
194afdce75fSGreg Kroah-Hartman static ssize_t driver_bind(struct device_driver *drv,
195afdce75fSGreg Kroah-Hartman 			   const char *buf, size_t count)
196afdce75fSGreg Kroah-Hartman {
1975901d014SGreg Kroah-Hartman 	struct bus_type *bus = bus_get(drv->bus);
198afdce75fSGreg Kroah-Hartman 	struct device *dev;
199afdce75fSGreg Kroah-Hartman 	int err = -ENODEV;
200afdce75fSGreg Kroah-Hartman 
2011f9ffc04SGreg Kroah-Hartman 	dev = bus_find_device_by_name(bus, NULL, buf);
20249b420a1SMing Lei 	if (dev && dev->driver == NULL && driver_match_device(drv, dev)) {
203bf74ad5bSAlan Stern 		if (dev->parent)	/* Needed for USB */
2048e9394ceSGreg Kroah-Hartman 			device_lock(dev->parent);
2058e9394ceSGreg Kroah-Hartman 		device_lock(dev);
206afdce75fSGreg Kroah-Hartman 		err = driver_probe_device(drv, dev);
2078e9394ceSGreg Kroah-Hartman 		device_unlock(dev);
208bf74ad5bSAlan Stern 		if (dev->parent)
2098e9394ceSGreg Kroah-Hartman 			device_unlock(dev->parent);
21037225401SRyan Wilson 
2114a3ad20cSGreg Kroah-Hartman 		if (err > 0) {
2124a3ad20cSGreg Kroah-Hartman 			/* success */
21337225401SRyan Wilson 			err = count;
2144a3ad20cSGreg Kroah-Hartman 		} else if (err == 0) {
2154a3ad20cSGreg Kroah-Hartman 			/* driver didn't accept device */
21637225401SRyan Wilson 			err = -ENODEV;
217afdce75fSGreg Kroah-Hartman 		}
2184a3ad20cSGreg Kroah-Hartman 	}
2192b08c8d0SAlan Stern 	put_device(dev);
220fc1ede58SGreg Kroah-Hartman 	bus_put(bus);
221afdce75fSGreg Kroah-Hartman 	return err;
222afdce75fSGreg Kroah-Hartman }
223afdce75fSGreg Kroah-Hartman static DRIVER_ATTR(bind, S_IWUSR, NULL, driver_bind);
224afdce75fSGreg Kroah-Hartman 
225b8c5cec2SKay Sievers static ssize_t show_drivers_autoprobe(struct bus_type *bus, char *buf)
226b8c5cec2SKay Sievers {
227c6f7e72aSGreg Kroah-Hartman 	return sprintf(buf, "%d\n", bus->p->drivers_autoprobe);
228b8c5cec2SKay Sievers }
229b8c5cec2SKay Sievers 
230b8c5cec2SKay Sievers static ssize_t store_drivers_autoprobe(struct bus_type *bus,
231b8c5cec2SKay Sievers 				       const char *buf, size_t count)
232b8c5cec2SKay Sievers {
233b8c5cec2SKay Sievers 	if (buf[0] == '0')
234c6f7e72aSGreg Kroah-Hartman 		bus->p->drivers_autoprobe = 0;
235b8c5cec2SKay Sievers 	else
236c6f7e72aSGreg Kroah-Hartman 		bus->p->drivers_autoprobe = 1;
237b8c5cec2SKay Sievers 	return count;
238b8c5cec2SKay Sievers }
239b8c5cec2SKay Sievers 
240b8c5cec2SKay Sievers static ssize_t store_drivers_probe(struct bus_type *bus,
241b8c5cec2SKay Sievers 				   const char *buf, size_t count)
242b8c5cec2SKay Sievers {
243b8c5cec2SKay Sievers 	struct device *dev;
244b8c5cec2SKay Sievers 
2451f9ffc04SGreg Kroah-Hartman 	dev = bus_find_device_by_name(bus, NULL, buf);
246b8c5cec2SKay Sievers 	if (!dev)
247b8c5cec2SKay Sievers 		return -ENODEV;
248b8c5cec2SKay Sievers 	if (bus_rescan_devices_helper(dev, NULL) != 0)
249b8c5cec2SKay Sievers 		return -EINVAL;
250b8c5cec2SKay Sievers 	return count;
251b8c5cec2SKay Sievers }
2522139bdd5SRussell King #endif
253151ef38fSGreg Kroah-Hartman 
254465c7a3aSmochel@digitalimplant.org static struct device *next_device(struct klist_iter *i)
255465c7a3aSmochel@digitalimplant.org {
256465c7a3aSmochel@digitalimplant.org 	struct klist_node *n = klist_next(i);
257ae1b4171SGreg Kroah-Hartman 	struct device *dev = NULL;
258ae1b4171SGreg Kroah-Hartman 	struct device_private *dev_prv;
259ae1b4171SGreg Kroah-Hartman 
260ae1b4171SGreg Kroah-Hartman 	if (n) {
261ae1b4171SGreg Kroah-Hartman 		dev_prv = to_device_private_bus(n);
262ae1b4171SGreg Kroah-Hartman 		dev = dev_prv->device;
263ae1b4171SGreg Kroah-Hartman 	}
264ae1b4171SGreg Kroah-Hartman 	return dev;
265465c7a3aSmochel@digitalimplant.org }
266465c7a3aSmochel@digitalimplant.org 
2671da177e4SLinus Torvalds /**
2681da177e4SLinus Torvalds  * bus_for_each_dev - device iterator.
2691da177e4SLinus Torvalds  * @bus: bus type.
2701da177e4SLinus Torvalds  * @start: device to start iterating from.
2711da177e4SLinus Torvalds  * @data: data for the callback.
2721da177e4SLinus Torvalds  * @fn: function to be called for each device.
2731da177e4SLinus Torvalds  *
2741da177e4SLinus Torvalds  * Iterate over @bus's list of devices, and call @fn for each,
2751da177e4SLinus Torvalds  * passing it @data. If @start is not NULL, we use that device to
2761da177e4SLinus Torvalds  * begin iterating from.
2771da177e4SLinus Torvalds  *
2781da177e4SLinus Torvalds  * We check the return of @fn each time. If it returns anything
2791da177e4SLinus Torvalds  * other than 0, we break out and return that value.
2801da177e4SLinus Torvalds  *
2811da177e4SLinus Torvalds  * NOTE: The device that returns a non-zero value is not retained
2821da177e4SLinus Torvalds  * in any way, nor is its refcount incremented. If the caller needs
2830fa1b0a1SAlex Chiang  * to retain this data, it should do so, and increment the reference
2841da177e4SLinus Torvalds  * count in the supplied callback.
2851da177e4SLinus Torvalds  */
2861da177e4SLinus Torvalds int bus_for_each_dev(struct bus_type *bus, struct device *start,
2871da177e4SLinus Torvalds 		     void *data, int (*fn)(struct device *, void *))
2881da177e4SLinus Torvalds {
289465c7a3aSmochel@digitalimplant.org 	struct klist_iter i;
290465c7a3aSmochel@digitalimplant.org 	struct device *dev;
291465c7a3aSmochel@digitalimplant.org 	int error = 0;
2921da177e4SLinus Torvalds 
293465c7a3aSmochel@digitalimplant.org 	if (!bus)
294465c7a3aSmochel@digitalimplant.org 		return -EINVAL;
295465c7a3aSmochel@digitalimplant.org 
296c6f7e72aSGreg Kroah-Hartman 	klist_iter_init_node(&bus->p->klist_devices, &i,
297ae1b4171SGreg Kroah-Hartman 			     (start ? &start->p->knode_bus : NULL));
298465c7a3aSmochel@digitalimplant.org 	while ((dev = next_device(&i)) && !error)
299465c7a3aSmochel@digitalimplant.org 		error = fn(dev, data);
300465c7a3aSmochel@digitalimplant.org 	klist_iter_exit(&i);
301465c7a3aSmochel@digitalimplant.org 	return error;
3021da177e4SLinus Torvalds }
3034a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_for_each_dev);
3041da177e4SLinus Torvalds 
3050edb5860SCornelia Huck /**
3060edb5860SCornelia Huck  * bus_find_device - device iterator for locating a particular device.
3070edb5860SCornelia Huck  * @bus: bus type
3080edb5860SCornelia Huck  * @start: Device to begin with
3090edb5860SCornelia Huck  * @data: Data to pass to match function
3100edb5860SCornelia Huck  * @match: Callback function to check device
3110edb5860SCornelia Huck  *
3120edb5860SCornelia Huck  * This is similar to the bus_for_each_dev() function above, but it
3130edb5860SCornelia Huck  * returns a reference to a device that is 'found' for later use, as
3140edb5860SCornelia Huck  * determined by the @match callback.
3150edb5860SCornelia Huck  *
3160edb5860SCornelia Huck  * The callback should return 0 if the device doesn't match and non-zero
3170edb5860SCornelia Huck  * if it does.  If the callback returns non-zero, this function will
3180edb5860SCornelia Huck  * return to the caller and not iterate over any more devices.
3190edb5860SCornelia Huck  */
3200edb5860SCornelia Huck struct device *bus_find_device(struct bus_type *bus,
3210edb5860SCornelia Huck 			       struct device *start, void *data,
3224a3ad20cSGreg Kroah-Hartman 			       int (*match)(struct device *dev, void *data))
3230edb5860SCornelia Huck {
3240edb5860SCornelia Huck 	struct klist_iter i;
3250edb5860SCornelia Huck 	struct device *dev;
3260edb5860SCornelia Huck 
3270edb5860SCornelia Huck 	if (!bus)
3280edb5860SCornelia Huck 		return NULL;
3290edb5860SCornelia Huck 
330c6f7e72aSGreg Kroah-Hartman 	klist_iter_init_node(&bus->p->klist_devices, &i,
331ae1b4171SGreg Kroah-Hartman 			     (start ? &start->p->knode_bus : NULL));
3320edb5860SCornelia Huck 	while ((dev = next_device(&i)))
3330edb5860SCornelia Huck 		if (match(dev, data) && get_device(dev))
3340edb5860SCornelia Huck 			break;
3350edb5860SCornelia Huck 	klist_iter_exit(&i);
3360edb5860SCornelia Huck 	return dev;
3370edb5860SCornelia Huck }
3384a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_find_device);
33938fdac3cSmochel@digitalimplant.org 
3401f9ffc04SGreg Kroah-Hartman static int match_name(struct device *dev, void *data)
3411f9ffc04SGreg Kroah-Hartman {
3421f9ffc04SGreg Kroah-Hartman 	const char *name = data;
3431f9ffc04SGreg Kroah-Hartman 
3441e0b2cf9SKay Sievers 	return sysfs_streq(name, dev_name(dev));
3451f9ffc04SGreg Kroah-Hartman }
3461f9ffc04SGreg Kroah-Hartman 
3471f9ffc04SGreg Kroah-Hartman /**
3481f9ffc04SGreg Kroah-Hartman  * bus_find_device_by_name - device iterator for locating a particular device of a specific name
3491f9ffc04SGreg Kroah-Hartman  * @bus: bus type
3501f9ffc04SGreg Kroah-Hartman  * @start: Device to begin with
3511f9ffc04SGreg Kroah-Hartman  * @name: name of the device to match
3521f9ffc04SGreg Kroah-Hartman  *
3531f9ffc04SGreg Kroah-Hartman  * This is similar to the bus_find_device() function above, but it handles
3541f9ffc04SGreg Kroah-Hartman  * searching by a name automatically, no need to write another strcmp matching
3551f9ffc04SGreg Kroah-Hartman  * function.
3561f9ffc04SGreg Kroah-Hartman  */
3571f9ffc04SGreg Kroah-Hartman struct device *bus_find_device_by_name(struct bus_type *bus,
3581f9ffc04SGreg Kroah-Hartman 				       struct device *start, const char *name)
3591f9ffc04SGreg Kroah-Hartman {
3601f9ffc04SGreg Kroah-Hartman 	return bus_find_device(bus, start, (void *)name, match_name);
3611f9ffc04SGreg Kroah-Hartman }
3621f9ffc04SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_find_device_by_name);
3631f9ffc04SGreg Kroah-Hartman 
36438fdac3cSmochel@digitalimplant.org static struct device_driver *next_driver(struct klist_iter *i)
36538fdac3cSmochel@digitalimplant.org {
36638fdac3cSmochel@digitalimplant.org 	struct klist_node *n = klist_next(i);
367e5dd1278SGreg Kroah-Hartman 	struct driver_private *drv_priv;
368e5dd1278SGreg Kroah-Hartman 
369e5dd1278SGreg Kroah-Hartman 	if (n) {
370e5dd1278SGreg Kroah-Hartman 		drv_priv = container_of(n, struct driver_private, knode_bus);
371e5dd1278SGreg Kroah-Hartman 		return drv_priv->driver;
372e5dd1278SGreg Kroah-Hartman 	}
373e5dd1278SGreg Kroah-Hartman 	return NULL;
37438fdac3cSmochel@digitalimplant.org }
37538fdac3cSmochel@digitalimplant.org 
3761da177e4SLinus Torvalds /**
3771da177e4SLinus Torvalds  * bus_for_each_drv - driver iterator
3781da177e4SLinus Torvalds  * @bus: bus we're dealing with.
3791da177e4SLinus Torvalds  * @start: driver to start iterating on.
3801da177e4SLinus Torvalds  * @data: data to pass to the callback.
3811da177e4SLinus Torvalds  * @fn: function to call for each driver.
3821da177e4SLinus Torvalds  *
3831da177e4SLinus Torvalds  * This is nearly identical to the device iterator above.
3841da177e4SLinus Torvalds  * We iterate over each driver that belongs to @bus, and call
3851da177e4SLinus Torvalds  * @fn for each. If @fn returns anything but 0, we break out
3861da177e4SLinus Torvalds  * and return it. If @start is not NULL, we use it as the head
3871da177e4SLinus Torvalds  * of the list.
3881da177e4SLinus Torvalds  *
3891da177e4SLinus Torvalds  * NOTE: we don't return the driver that returns a non-zero
3901da177e4SLinus Torvalds  * value, nor do we leave the reference count incremented for that
3911da177e4SLinus Torvalds  * driver. If the caller needs to know that info, it must set it
3921da177e4SLinus Torvalds  * in the callback. It must also be sure to increment the refcount
3931da177e4SLinus Torvalds  * so it doesn't disappear before returning to the caller.
3941da177e4SLinus Torvalds  */
3951da177e4SLinus Torvalds int bus_for_each_drv(struct bus_type *bus, struct device_driver *start,
3961da177e4SLinus Torvalds 		     void *data, int (*fn)(struct device_driver *, void *))
3971da177e4SLinus Torvalds {
39838fdac3cSmochel@digitalimplant.org 	struct klist_iter i;
39938fdac3cSmochel@digitalimplant.org 	struct device_driver *drv;
40038fdac3cSmochel@digitalimplant.org 	int error = 0;
4011da177e4SLinus Torvalds 
40238fdac3cSmochel@digitalimplant.org 	if (!bus)
40338fdac3cSmochel@digitalimplant.org 		return -EINVAL;
40438fdac3cSmochel@digitalimplant.org 
405c6f7e72aSGreg Kroah-Hartman 	klist_iter_init_node(&bus->p->klist_drivers, &i,
406e5dd1278SGreg Kroah-Hartman 			     start ? &start->p->knode_bus : NULL);
40738fdac3cSmochel@digitalimplant.org 	while ((drv = next_driver(&i)) && !error)
40838fdac3cSmochel@digitalimplant.org 		error = fn(drv, data);
40938fdac3cSmochel@digitalimplant.org 	klist_iter_exit(&i);
41038fdac3cSmochel@digitalimplant.org 	return error;
4111da177e4SLinus Torvalds }
4124a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_for_each_drv);
4131da177e4SLinus Torvalds 
4141da177e4SLinus Torvalds static int device_add_attrs(struct bus_type *bus, struct device *dev)
4151da177e4SLinus Torvalds {
4161da177e4SLinus Torvalds 	int error = 0;
4171da177e4SLinus Torvalds 	int i;
4181da177e4SLinus Torvalds 
4194aca67e5SAndrew Morton 	if (!bus->dev_attrs)
4204aca67e5SAndrew Morton 		return 0;
4214aca67e5SAndrew Morton 
4221da177e4SLinus Torvalds 	for (i = 0; attr_name(bus->dev_attrs[i]); i++) {
4231da177e4SLinus Torvalds 		error = device_create_file(dev, &bus->dev_attrs[i]);
4244aca67e5SAndrew Morton 		if (error) {
4251da177e4SLinus Torvalds 			while (--i >= 0)
4261da177e4SLinus Torvalds 				device_remove_file(dev, &bus->dev_attrs[i]);
4274aca67e5SAndrew Morton 			break;
4281da177e4SLinus Torvalds 		}
4294aca67e5SAndrew Morton 	}
4304aca67e5SAndrew Morton 	return error;
4314aca67e5SAndrew Morton }
4321da177e4SLinus Torvalds 
4331da177e4SLinus Torvalds static void device_remove_attrs(struct bus_type *bus, struct device *dev)
4341da177e4SLinus Torvalds {
4351da177e4SLinus Torvalds 	int i;
4361da177e4SLinus Torvalds 
4371da177e4SLinus Torvalds 	if (bus->dev_attrs) {
4381da177e4SLinus Torvalds 		for (i = 0; attr_name(bus->dev_attrs[i]); i++)
4391da177e4SLinus Torvalds 			device_remove_file(dev, &bus->dev_attrs[i]);
4401da177e4SLinus Torvalds 	}
4411da177e4SLinus Torvalds }
4421da177e4SLinus Torvalds 
443b9cafc7dSKay Sievers #ifdef CONFIG_SYSFS_DEPRECATED
444b9cafc7dSKay Sievers static int make_deprecated_bus_links(struct device *dev)
445b9cafc7dSKay Sievers {
446b9cafc7dSKay Sievers 	return sysfs_create_link(&dev->kobj,
447c6f7e72aSGreg Kroah-Hartman 				 &dev->bus->p->subsys.kobj, "bus");
448b9cafc7dSKay Sievers }
449b9cafc7dSKay Sievers 
450b9cafc7dSKay Sievers static void remove_deprecated_bus_links(struct device *dev)
451b9cafc7dSKay Sievers {
452b9cafc7dSKay Sievers 	sysfs_remove_link(&dev->kobj, "bus");
453b9cafc7dSKay Sievers }
454b9cafc7dSKay Sievers #else
455b9cafc7dSKay Sievers static inline int make_deprecated_bus_links(struct device *dev) { return 0; }
456b9cafc7dSKay Sievers static inline void remove_deprecated_bus_links(struct device *dev) { }
457b9cafc7dSKay Sievers #endif
4581da177e4SLinus Torvalds 
4591da177e4SLinus Torvalds /**
4601da177e4SLinus Torvalds  * bus_add_device - add device to bus
4611da177e4SLinus Torvalds  * @dev: device being added
4621da177e4SLinus Torvalds  *
4632023c610SAlan Stern  * - Add device's bus attributes.
4642023c610SAlan Stern  * - Create links to device's bus.
4651da177e4SLinus Torvalds  * - Add the device to its bus's list of devices.
4661da177e4SLinus Torvalds  */
4671da177e4SLinus Torvalds int bus_add_device(struct device *dev)
4681da177e4SLinus Torvalds {
4695901d014SGreg Kroah-Hartman 	struct bus_type *bus = bus_get(dev->bus);
4701da177e4SLinus Torvalds 	int error = 0;
4711da177e4SLinus Torvalds 
4721da177e4SLinus Torvalds 	if (bus) {
4731e0b2cf9SKay Sievers 		pr_debug("bus: '%s': add device %s\n", bus->name, dev_name(dev));
474ca2b94baSHannes Reinecke 		error = device_add_attrs(bus, dev);
475f86db396SAndrew Morton 		if (error)
476513e7337SCornelia Huck 			goto out_put;
477c6f7e72aSGreg Kroah-Hartman 		error = sysfs_create_link(&bus->p->devices_kset->kobj,
4781e0b2cf9SKay Sievers 						&dev->kobj, dev_name(dev));
479f86db396SAndrew Morton 		if (error)
480513e7337SCornelia Huck 			goto out_id;
481f86db396SAndrew Morton 		error = sysfs_create_link(&dev->kobj,
482c6f7e72aSGreg Kroah-Hartman 				&dev->bus->p->subsys.kobj, "subsystem");
483f86db396SAndrew Morton 		if (error)
484513e7337SCornelia Huck 			goto out_subsys;
485b9cafc7dSKay Sievers 		error = make_deprecated_bus_links(dev);
486513e7337SCornelia Huck 		if (error)
487513e7337SCornelia Huck 			goto out_deprecated;
4882023c610SAlan Stern 		klist_add_tail(&dev->p->knode_bus, &bus->p->klist_devices);
4891da177e4SLinus Torvalds 	}
490513e7337SCornelia Huck 	return 0;
491513e7337SCornelia Huck 
492513e7337SCornelia Huck out_deprecated:
493513e7337SCornelia Huck 	sysfs_remove_link(&dev->kobj, "subsystem");
494513e7337SCornelia Huck out_subsys:
4951e0b2cf9SKay Sievers 	sysfs_remove_link(&bus->p->devices_kset->kobj, dev_name(dev));
496513e7337SCornelia Huck out_id:
497513e7337SCornelia Huck 	device_remove_attrs(bus, dev);
498513e7337SCornelia Huck out_put:
499fc1ede58SGreg Kroah-Hartman 	bus_put(dev->bus);
5001da177e4SLinus Torvalds 	return error;
5011da177e4SLinus Torvalds }
5021da177e4SLinus Torvalds 
5031da177e4SLinus Torvalds /**
5042023c610SAlan Stern  * bus_probe_device - probe drivers for a new device
5052023c610SAlan Stern  * @dev: device to probe
50653877d06SKay Sievers  *
5072023c610SAlan Stern  * - Automatically probe for a driver if the bus allows it.
50853877d06SKay Sievers  */
5092023c610SAlan Stern void bus_probe_device(struct device *dev)
51053877d06SKay Sievers {
51153877d06SKay Sievers 	struct bus_type *bus = dev->bus;
5122023c610SAlan Stern 	int ret;
51353877d06SKay Sievers 
5142023c610SAlan Stern 	if (bus && bus->p->drivers_autoprobe) {
515f86db396SAndrew Morton 		ret = device_attach(dev);
516c6a46696SCornelia Huck 		WARN_ON(ret < 0);
51753877d06SKay Sievers 	}
518f86db396SAndrew Morton }
51953877d06SKay Sievers 
52053877d06SKay Sievers /**
5211da177e4SLinus Torvalds  * bus_remove_device - remove device from bus
5221da177e4SLinus Torvalds  * @dev: device to be removed
5231da177e4SLinus Torvalds  *
5241da177e4SLinus Torvalds  * - Remove symlink from bus's directory.
5251da177e4SLinus Torvalds  * - Delete device from bus's list.
5261da177e4SLinus Torvalds  * - Detach from its driver.
5271da177e4SLinus Torvalds  * - Drop reference taken in bus_add_device().
5281da177e4SLinus Torvalds  */
5291da177e4SLinus Torvalds void bus_remove_device(struct device *dev)
5301da177e4SLinus Torvalds {
5311da177e4SLinus Torvalds 	if (dev->bus) {
532b9d9c82bSKay Sievers 		sysfs_remove_link(&dev->kobj, "subsystem");
533b9cafc7dSKay Sievers 		remove_deprecated_bus_links(dev);
5344a3ad20cSGreg Kroah-Hartman 		sysfs_remove_link(&dev->bus->p->devices_kset->kobj,
5351e0b2cf9SKay Sievers 				  dev_name(dev));
5361da177e4SLinus Torvalds 		device_remove_attrs(dev->bus, dev);
537ae1b4171SGreg Kroah-Hartman 		if (klist_node_attached(&dev->p->knode_bus))
538ae1b4171SGreg Kroah-Hartman 			klist_del(&dev->p->knode_bus);
5393f62e570SGreg Kroah-Hartman 
5404a3ad20cSGreg Kroah-Hartman 		pr_debug("bus: '%s': remove device %s\n",
5411e0b2cf9SKay Sievers 			 dev->bus->name, dev_name(dev));
5421da177e4SLinus Torvalds 		device_release_driver(dev);
543fc1ede58SGreg Kroah-Hartman 		bus_put(dev->bus);
5441da177e4SLinus Torvalds 	}
5451da177e4SLinus Torvalds }
5461da177e4SLinus Torvalds 
5471da177e4SLinus Torvalds static int driver_add_attrs(struct bus_type *bus, struct device_driver *drv)
5481da177e4SLinus Torvalds {
5491da177e4SLinus Torvalds 	int error = 0;
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 			error = driver_create_file(drv, &bus->drv_attrs[i]);
5551da177e4SLinus Torvalds 			if (error)
5564a3ad20cSGreg Kroah-Hartman 				goto err;
5571da177e4SLinus Torvalds 		}
5581da177e4SLinus Torvalds 	}
5594a3ad20cSGreg Kroah-Hartman done:
5601da177e4SLinus Torvalds 	return error;
5614a3ad20cSGreg Kroah-Hartman err:
5621da177e4SLinus Torvalds 	while (--i >= 0)
5631da177e4SLinus Torvalds 		driver_remove_file(drv, &bus->drv_attrs[i]);
5644a3ad20cSGreg Kroah-Hartman 	goto done;
5651da177e4SLinus Torvalds }
5661da177e4SLinus Torvalds 
5674a3ad20cSGreg Kroah-Hartman static void driver_remove_attrs(struct bus_type *bus,
5684a3ad20cSGreg Kroah-Hartman 				struct device_driver *drv)
5691da177e4SLinus Torvalds {
5701da177e4SLinus Torvalds 	int i;
5711da177e4SLinus Torvalds 
5721da177e4SLinus Torvalds 	if (bus->drv_attrs) {
5731da177e4SLinus Torvalds 		for (i = 0; attr_name(bus->drv_attrs[i]); i++)
5741da177e4SLinus Torvalds 			driver_remove_file(drv, &bus->drv_attrs[i]);
5751da177e4SLinus Torvalds 	}
5761da177e4SLinus Torvalds }
5771da177e4SLinus Torvalds 
578874c6241SGreg Kroah-Hartman #ifdef CONFIG_HOTPLUG
579874c6241SGreg Kroah-Hartman /*
580874c6241SGreg Kroah-Hartman  * Thanks to drivers making their tables __devinit, we can't allow manual
581874c6241SGreg Kroah-Hartman  * bind and unbind from userspace unless CONFIG_HOTPLUG is enabled.
582874c6241SGreg Kroah-Hartman  */
583f86db396SAndrew Morton static int __must_check add_bind_files(struct device_driver *drv)
584874c6241SGreg Kroah-Hartman {
585f86db396SAndrew Morton 	int ret;
586f86db396SAndrew Morton 
587f86db396SAndrew Morton 	ret = driver_create_file(drv, &driver_attr_unbind);
588f86db396SAndrew Morton 	if (ret == 0) {
589f86db396SAndrew Morton 		ret = driver_create_file(drv, &driver_attr_bind);
590f86db396SAndrew Morton 		if (ret)
591f86db396SAndrew Morton 			driver_remove_file(drv, &driver_attr_unbind);
592f86db396SAndrew Morton 	}
593f86db396SAndrew Morton 	return ret;
594874c6241SGreg Kroah-Hartman }
595874c6241SGreg Kroah-Hartman 
596874c6241SGreg Kroah-Hartman static void remove_bind_files(struct device_driver *drv)
597874c6241SGreg Kroah-Hartman {
598874c6241SGreg Kroah-Hartman 	driver_remove_file(drv, &driver_attr_bind);
599874c6241SGreg Kroah-Hartman 	driver_remove_file(drv, &driver_attr_unbind);
600874c6241SGreg Kroah-Hartman }
601b8c5cec2SKay Sievers 
6028380770cSKay Sievers static BUS_ATTR(drivers_probe, S_IWUSR, NULL, store_drivers_probe);
6038380770cSKay Sievers static BUS_ATTR(drivers_autoprobe, S_IWUSR | S_IRUGO,
6048380770cSKay Sievers 		show_drivers_autoprobe, store_drivers_autoprobe);
6058380770cSKay Sievers 
606b8c5cec2SKay Sievers static int add_probe_files(struct bus_type *bus)
607b8c5cec2SKay Sievers {
608b8c5cec2SKay Sievers 	int retval;
609b8c5cec2SKay Sievers 
6108380770cSKay Sievers 	retval = bus_create_file(bus, &bus_attr_drivers_probe);
611b8c5cec2SKay Sievers 	if (retval)
612b8c5cec2SKay Sievers 		goto out;
613b8c5cec2SKay Sievers 
6148380770cSKay Sievers 	retval = bus_create_file(bus, &bus_attr_drivers_autoprobe);
615b8c5cec2SKay Sievers 	if (retval)
6168380770cSKay Sievers 		bus_remove_file(bus, &bus_attr_drivers_probe);
617b8c5cec2SKay Sievers out:
618b8c5cec2SKay Sievers 	return retval;
619b8c5cec2SKay Sievers }
620b8c5cec2SKay Sievers 
621b8c5cec2SKay Sievers static void remove_probe_files(struct bus_type *bus)
622b8c5cec2SKay Sievers {
6238380770cSKay Sievers 	bus_remove_file(bus, &bus_attr_drivers_autoprobe);
6248380770cSKay Sievers 	bus_remove_file(bus, &bus_attr_drivers_probe);
625b8c5cec2SKay Sievers }
626874c6241SGreg Kroah-Hartman #else
62735acfdd7SYoichi Yuasa static inline int add_bind_files(struct device_driver *drv) { return 0; }
628874c6241SGreg Kroah-Hartman static inline void remove_bind_files(struct device_driver *drv) {}
629b8c5cec2SKay Sievers static inline int add_probe_files(struct bus_type *bus) { return 0; }
630b8c5cec2SKay Sievers static inline void remove_probe_files(struct bus_type *bus) {}
631874c6241SGreg Kroah-Hartman #endif
6321da177e4SLinus Torvalds 
6337ac1cf4aSKay Sievers static ssize_t driver_uevent_store(struct device_driver *drv,
6347ac1cf4aSKay Sievers 				   const char *buf, size_t count)
6357ac1cf4aSKay Sievers {
6367ac1cf4aSKay Sievers 	enum kobject_action action;
6377ac1cf4aSKay Sievers 
6387ac1cf4aSKay Sievers 	if (kobject_action_type(buf, count, &action) == 0)
639e5dd1278SGreg Kroah-Hartman 		kobject_uevent(&drv->p->kobj, action);
6407ac1cf4aSKay Sievers 	return count;
6417ac1cf4aSKay Sievers }
6427ac1cf4aSKay Sievers static DRIVER_ATTR(uevent, S_IWUSR, NULL, driver_uevent_store);
6437ac1cf4aSKay Sievers 
6441da177e4SLinus Torvalds /**
6451da177e4SLinus Torvalds  * bus_add_driver - Add a driver to the bus.
6461da177e4SLinus Torvalds  * @drv: driver.
6471da177e4SLinus Torvalds  */
6481da177e4SLinus Torvalds int bus_add_driver(struct device_driver *drv)
6491da177e4SLinus Torvalds {
650e5dd1278SGreg Kroah-Hartman 	struct bus_type *bus;
651e5dd1278SGreg Kroah-Hartman 	struct driver_private *priv;
6521da177e4SLinus Torvalds 	int error = 0;
6531da177e4SLinus Torvalds 
654e5dd1278SGreg Kroah-Hartman 	bus = bus_get(drv->bus);
655d9fd4d3bSJeff Garzik 	if (!bus)
6564f6e1945SGreg Kroah-Hartman 		return -EINVAL;
657d9fd4d3bSJeff Garzik 
6587dc72b28SGreg Kroah-Hartman 	pr_debug("bus: '%s': add driver %s\n", bus->name, drv->name);
659e5dd1278SGreg Kroah-Hartman 
660e5dd1278SGreg Kroah-Hartman 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
66107634464SCornelia Huck 	if (!priv) {
66207634464SCornelia Huck 		error = -ENOMEM;
66307634464SCornelia Huck 		goto out_put_bus;
66407634464SCornelia Huck 	}
665e5dd1278SGreg Kroah-Hartman 	klist_init(&priv->klist_devices, NULL, NULL);
666e5dd1278SGreg Kroah-Hartman 	priv->driver = drv;
667e5dd1278SGreg Kroah-Hartman 	drv->p = priv;
668c8e90d82SGreg Kroah-Hartman 	priv->kobj.kset = bus->p->drivers_kset;
669c8e90d82SGreg Kroah-Hartman 	error = kobject_init_and_add(&priv->kobj, &driver_ktype, NULL,
670c8e90d82SGreg Kroah-Hartman 				     "%s", drv->name);
671dc0afa83SCornelia Huck 	if (error)
67207634464SCornelia Huck 		goto out_unregister;
6731da177e4SLinus Torvalds 
674c6f7e72aSGreg Kroah-Hartman 	if (drv->bus->p->drivers_autoprobe) {
675f86db396SAndrew Morton 		error = driver_attach(drv);
676f86db396SAndrew Morton 		if (error)
677f86db396SAndrew Morton 			goto out_unregister;
678b8c5cec2SKay Sievers 	}
679e5dd1278SGreg Kroah-Hartman 	klist_add_tail(&priv->knode_bus, &bus->p->klist_drivers);
6801da177e4SLinus Torvalds 	module_add_driver(drv->owner, drv);
6811da177e4SLinus Torvalds 
6827ac1cf4aSKay Sievers 	error = driver_create_file(drv, &driver_attr_uevent);
6837ac1cf4aSKay Sievers 	if (error) {
6847ac1cf4aSKay Sievers 		printk(KERN_ERR "%s: uevent attr (%s) failed\n",
6852b3a302aSHarvey Harrison 			__func__, drv->name);
6867ac1cf4aSKay Sievers 	}
687f86db396SAndrew Morton 	error = driver_add_attrs(bus, drv);
688f86db396SAndrew Morton 	if (error) {
689f86db396SAndrew Morton 		/* How the hell do we get out of this pickle? Give up */
690f86db396SAndrew Morton 		printk(KERN_ERR "%s: driver_add_attrs(%s) failed\n",
6912b3a302aSHarvey Harrison 			__func__, drv->name);
692f86db396SAndrew Morton 	}
6931a6f2a75SDmitry Torokhov 
6941a6f2a75SDmitry Torokhov 	if (!drv->suppress_bind_attrs) {
695f86db396SAndrew Morton 		error = add_bind_files(drv);
696f86db396SAndrew Morton 		if (error) {
697f86db396SAndrew Morton 			/* Ditto */
698f86db396SAndrew Morton 			printk(KERN_ERR "%s: add_bind_files(%s) failed\n",
6992b3a302aSHarvey Harrison 				__func__, drv->name);
700f86db396SAndrew Morton 		}
7011a6f2a75SDmitry Torokhov 	}
702d9fd4d3bSJeff Garzik 
703c8e90d82SGreg Kroah-Hartman 	kobject_uevent(&priv->kobj, KOBJ_ADD);
7045c8563d7SKay Sievers 	return 0;
7051a6f2a75SDmitry Torokhov 
706f86db396SAndrew Morton out_unregister:
70799b28f1bSPhil Carmody 	kobject_put(&priv->kobj);
7085c8563d7SKay Sievers 	kfree(drv->p);
7095c8563d7SKay Sievers 	drv->p = NULL;
710f86db396SAndrew Morton out_put_bus:
711fc1ede58SGreg Kroah-Hartman 	bus_put(bus);
712f86db396SAndrew Morton 	return error;
7131da177e4SLinus Torvalds }
7141da177e4SLinus Torvalds 
7151da177e4SLinus Torvalds /**
7161da177e4SLinus Torvalds  * bus_remove_driver - delete driver from bus's knowledge.
7171da177e4SLinus Torvalds  * @drv: driver.
7181da177e4SLinus Torvalds  *
7191da177e4SLinus Torvalds  * Detach the driver from the devices it controls, and remove
7201da177e4SLinus Torvalds  * it from its bus's list of drivers. Finally, we drop the reference
7211da177e4SLinus Torvalds  * to the bus we took in bus_add_driver().
7221da177e4SLinus Torvalds  */
7231da177e4SLinus Torvalds void bus_remove_driver(struct device_driver *drv)
7241da177e4SLinus Torvalds {
725d9fd4d3bSJeff Garzik 	if (!drv->bus)
726d9fd4d3bSJeff Garzik 		return;
727d9fd4d3bSJeff Garzik 
7281a6f2a75SDmitry Torokhov 	if (!drv->suppress_bind_attrs)
729874c6241SGreg Kroah-Hartman 		remove_bind_files(drv);
7301da177e4SLinus Torvalds 	driver_remove_attrs(drv->bus, drv);
7317ac1cf4aSKay Sievers 	driver_remove_file(drv, &driver_attr_uevent);
732e5dd1278SGreg Kroah-Hartman 	klist_remove(&drv->p->knode_bus);
7337dc72b28SGreg Kroah-Hartman 	pr_debug("bus: '%s': remove driver %s\n", drv->bus->name, drv->name);
7341da177e4SLinus Torvalds 	driver_detach(drv);
7351da177e4SLinus Torvalds 	module_remove_driver(drv);
736c10997f6SGreg Kroah-Hartman 	kobject_put(&drv->p->kobj);
737fc1ede58SGreg Kroah-Hartman 	bus_put(drv->bus);
7381da177e4SLinus Torvalds }
7391da177e4SLinus Torvalds 
7401da177e4SLinus Torvalds /* Helper for bus_rescan_devices's iter */
741f86db396SAndrew Morton static int __must_check bus_rescan_devices_helper(struct device *dev,
742f86db396SAndrew Morton 						  void *data)
7431da177e4SLinus Torvalds {
744f86db396SAndrew Morton 	int ret = 0;
745f86db396SAndrew Morton 
746bf74ad5bSAlan Stern 	if (!dev->driver) {
747bf74ad5bSAlan Stern 		if (dev->parent)	/* Needed for USB */
7488e9394ceSGreg Kroah-Hartman 			device_lock(dev->parent);
749f86db396SAndrew Morton 		ret = device_attach(dev);
750bf74ad5bSAlan Stern 		if (dev->parent)
7518e9394ceSGreg Kroah-Hartman 			device_unlock(dev->parent);
752bf74ad5bSAlan Stern 	}
753f86db396SAndrew Morton 	return ret < 0 ? ret : 0;
7541da177e4SLinus Torvalds }
7551da177e4SLinus Torvalds 
7561da177e4SLinus Torvalds /**
7571da177e4SLinus Torvalds  * bus_rescan_devices - rescan devices on the bus for possible drivers
7581da177e4SLinus Torvalds  * @bus: the bus to scan.
7591da177e4SLinus Torvalds  *
7601da177e4SLinus Torvalds  * This function will look for devices on the bus with no driver
76123d3d602SGreg Kroah-Hartman  * attached and rescan it against existing drivers to see if it matches
76223d3d602SGreg Kroah-Hartman  * any by calling device_attach() for the unbound devices.
7631da177e4SLinus Torvalds  */
764f86db396SAndrew Morton int bus_rescan_devices(struct bus_type *bus)
7651da177e4SLinus Torvalds {
766f86db396SAndrew Morton 	return bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper);
7671da177e4SLinus Torvalds }
7684a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_rescan_devices);
7691da177e4SLinus Torvalds 
770e935d5daSMoore, Eric /**
771e935d5daSMoore, Eric  * device_reprobe - remove driver for a device and probe for a new driver
772e935d5daSMoore, Eric  * @dev: the device to reprobe
773e935d5daSMoore, Eric  *
774e935d5daSMoore, Eric  * This function detaches the attached driver (if any) for the given
775e935d5daSMoore, Eric  * device and restarts the driver probing process.  It is intended
776e935d5daSMoore, Eric  * to use if probing criteria changed during a devices lifetime and
777e935d5daSMoore, Eric  * driver attachment should change accordingly.
778e935d5daSMoore, Eric  */
779f86db396SAndrew Morton int device_reprobe(struct device *dev)
780e935d5daSMoore, Eric {
781e935d5daSMoore, Eric 	if (dev->driver) {
782e935d5daSMoore, Eric 		if (dev->parent)        /* Needed for USB */
7838e9394ceSGreg Kroah-Hartman 			device_lock(dev->parent);
784e935d5daSMoore, Eric 		device_release_driver(dev);
785e935d5daSMoore, Eric 		if (dev->parent)
7868e9394ceSGreg Kroah-Hartman 			device_unlock(dev->parent);
787e935d5daSMoore, Eric 	}
788f86db396SAndrew Morton 	return bus_rescan_devices_helper(dev, NULL);
789e935d5daSMoore, Eric }
790e935d5daSMoore, Eric EXPORT_SYMBOL_GPL(device_reprobe);
7911da177e4SLinus Torvalds 
7921da177e4SLinus Torvalds /**
7931da177e4SLinus Torvalds  * find_bus - locate bus by name.
7941da177e4SLinus Torvalds  * @name: name of bus.
7951da177e4SLinus Torvalds  *
7961da177e4SLinus Torvalds  * Call kset_find_obj() to iterate over list of buses to
7971da177e4SLinus Torvalds  * find a bus by name. Return bus if found.
7981da177e4SLinus Torvalds  *
7991da177e4SLinus Torvalds  * Note that kset_find_obj increments bus' reference count.
8001da177e4SLinus Torvalds  */
8017e4ef085SAdrian Bunk #if 0
8021da177e4SLinus Torvalds struct bus_type *find_bus(char *name)
8031da177e4SLinus Torvalds {
80459a54833SGreg Kroah-Hartman 	struct kobject *k = kset_find_obj(bus_kset, name);
8051da177e4SLinus Torvalds 	return k ? to_bus(k) : NULL;
8061da177e4SLinus Torvalds }
8077e4ef085SAdrian Bunk #endif  /*  0  */
8081da177e4SLinus Torvalds 
8091da177e4SLinus Torvalds 
8101da177e4SLinus Torvalds /**
8111da177e4SLinus Torvalds  * bus_add_attrs - Add default attributes for this bus.
8121da177e4SLinus Torvalds  * @bus: Bus that has just been registered.
8131da177e4SLinus Torvalds  */
8141da177e4SLinus Torvalds 
8151da177e4SLinus Torvalds static int bus_add_attrs(struct bus_type *bus)
8161da177e4SLinus Torvalds {
8171da177e4SLinus Torvalds 	int error = 0;
8181da177e4SLinus Torvalds 	int i;
8191da177e4SLinus Torvalds 
8201da177e4SLinus Torvalds 	if (bus->bus_attrs) {
8211da177e4SLinus Torvalds 		for (i = 0; attr_name(bus->bus_attrs[i]); i++) {
822dc0afa83SCornelia Huck 			error = bus_create_file(bus, &bus->bus_attrs[i]);
823dc0afa83SCornelia Huck 			if (error)
8244a3ad20cSGreg Kroah-Hartman 				goto err;
8251da177e4SLinus Torvalds 		}
8261da177e4SLinus Torvalds 	}
8274a3ad20cSGreg Kroah-Hartman done:
8281da177e4SLinus Torvalds 	return error;
8294a3ad20cSGreg Kroah-Hartman err:
8301da177e4SLinus Torvalds 	while (--i >= 0)
8311da177e4SLinus Torvalds 		bus_remove_file(bus, &bus->bus_attrs[i]);
8324a3ad20cSGreg Kroah-Hartman 	goto done;
8331da177e4SLinus Torvalds }
8341da177e4SLinus Torvalds 
8351da177e4SLinus Torvalds static void bus_remove_attrs(struct bus_type *bus)
8361da177e4SLinus Torvalds {
8371da177e4SLinus Torvalds 	int i;
8381da177e4SLinus Torvalds 
8391da177e4SLinus Torvalds 	if (bus->bus_attrs) {
8401da177e4SLinus Torvalds 		for (i = 0; attr_name(bus->bus_attrs[i]); i++)
8411da177e4SLinus Torvalds 			bus_remove_file(bus, &bus->bus_attrs[i]);
8421da177e4SLinus Torvalds 	}
8431da177e4SLinus Torvalds }
8441da177e4SLinus Torvalds 
84534bb61f9SJames Bottomley static void klist_devices_get(struct klist_node *n)
84634bb61f9SJames Bottomley {
847ae1b4171SGreg Kroah-Hartman 	struct device_private *dev_prv = to_device_private_bus(n);
848ae1b4171SGreg Kroah-Hartman 	struct device *dev = dev_prv->device;
84934bb61f9SJames Bottomley 
85034bb61f9SJames Bottomley 	get_device(dev);
85134bb61f9SJames Bottomley }
85234bb61f9SJames Bottomley 
85334bb61f9SJames Bottomley static void klist_devices_put(struct klist_node *n)
85434bb61f9SJames Bottomley {
855ae1b4171SGreg Kroah-Hartman 	struct device_private *dev_prv = to_device_private_bus(n);
856ae1b4171SGreg Kroah-Hartman 	struct device *dev = dev_prv->device;
85734bb61f9SJames Bottomley 
85834bb61f9SJames Bottomley 	put_device(dev);
85934bb61f9SJames Bottomley }
86034bb61f9SJames Bottomley 
8617ac1cf4aSKay Sievers static ssize_t bus_uevent_store(struct bus_type *bus,
8627ac1cf4aSKay Sievers 				const char *buf, size_t count)
8637ac1cf4aSKay Sievers {
8647ac1cf4aSKay Sievers 	enum kobject_action action;
8657ac1cf4aSKay Sievers 
8667ac1cf4aSKay Sievers 	if (kobject_action_type(buf, count, &action) == 0)
867c6f7e72aSGreg Kroah-Hartman 		kobject_uevent(&bus->p->subsys.kobj, action);
8687ac1cf4aSKay Sievers 	return count;
8697ac1cf4aSKay Sievers }
8707ac1cf4aSKay Sievers static BUS_ATTR(uevent, S_IWUSR, NULL, bus_uevent_store);
8717ac1cf4aSKay Sievers 
8721da177e4SLinus Torvalds /**
8731da177e4SLinus Torvalds  * bus_register - register a bus with the system.
8741da177e4SLinus Torvalds  * @bus: bus.
8751da177e4SLinus Torvalds  *
8761da177e4SLinus Torvalds  * Once we have that, we registered the bus with the kobject
8771da177e4SLinus Torvalds  * infrastructure, then register the children subsystems it has:
8781da177e4SLinus Torvalds  * the devices and drivers that belong to the bus.
8791da177e4SLinus Torvalds  */
8801da177e4SLinus Torvalds int bus_register(struct bus_type *bus)
8811da177e4SLinus Torvalds {
8821da177e4SLinus Torvalds 	int retval;
883c6f7e72aSGreg Kroah-Hartman 	struct bus_type_private *priv;
8841da177e4SLinus Torvalds 
885c6f7e72aSGreg Kroah-Hartman 	priv = kzalloc(sizeof(struct bus_type_private), GFP_KERNEL);
886c6f7e72aSGreg Kroah-Hartman 	if (!priv)
887c6f7e72aSGreg Kroah-Hartman 		return -ENOMEM;
888116af378SBenjamin Herrenschmidt 
889c6f7e72aSGreg Kroah-Hartman 	priv->bus = bus;
890c6f7e72aSGreg Kroah-Hartman 	bus->p = priv;
891c6f7e72aSGreg Kroah-Hartman 
892c6f7e72aSGreg Kroah-Hartman 	BLOCKING_INIT_NOTIFIER_HEAD(&priv->bus_notifier);
893c6f7e72aSGreg Kroah-Hartman 
894c6f7e72aSGreg Kroah-Hartman 	retval = kobject_set_name(&priv->subsys.kobj, "%s", bus->name);
8951da177e4SLinus Torvalds 	if (retval)
8961da177e4SLinus Torvalds 		goto out;
8971da177e4SLinus Torvalds 
898c6f7e72aSGreg Kroah-Hartman 	priv->subsys.kobj.kset = bus_kset;
899c6f7e72aSGreg Kroah-Hartman 	priv->subsys.kobj.ktype = &bus_ktype;
900c6f7e72aSGreg Kroah-Hartman 	priv->drivers_autoprobe = 1;
901d6b05b84SGreg Kroah-Hartman 
902c6f7e72aSGreg Kroah-Hartman 	retval = kset_register(&priv->subsys);
9031da177e4SLinus Torvalds 	if (retval)
9041da177e4SLinus Torvalds 		goto out;
9051da177e4SLinus Torvalds 
9067ac1cf4aSKay Sievers 	retval = bus_create_file(bus, &bus_attr_uevent);
9077ac1cf4aSKay Sievers 	if (retval)
9087ac1cf4aSKay Sievers 		goto bus_uevent_fail;
9097ac1cf4aSKay Sievers 
910c6f7e72aSGreg Kroah-Hartman 	priv->devices_kset = kset_create_and_add("devices", NULL,
911c6f7e72aSGreg Kroah-Hartman 						 &priv->subsys.kobj);
912c6f7e72aSGreg Kroah-Hartman 	if (!priv->devices_kset) {
9133d899596SGreg Kroah-Hartman 		retval = -ENOMEM;
9141da177e4SLinus Torvalds 		goto bus_devices_fail;
9153d899596SGreg Kroah-Hartman 	}
9161da177e4SLinus Torvalds 
917c6f7e72aSGreg Kroah-Hartman 	priv->drivers_kset = kset_create_and_add("drivers", NULL,
918c6f7e72aSGreg Kroah-Hartman 						 &priv->subsys.kobj);
919c6f7e72aSGreg Kroah-Hartman 	if (!priv->drivers_kset) {
9206dcec251SGreg Kroah-Hartman 		retval = -ENOMEM;
9211da177e4SLinus Torvalds 		goto bus_drivers_fail;
9226dcec251SGreg Kroah-Hartman 	}
923465c7a3aSmochel@digitalimplant.org 
924c6f7e72aSGreg Kroah-Hartman 	klist_init(&priv->klist_devices, klist_devices_get, klist_devices_put);
925c6f7e72aSGreg Kroah-Hartman 	klist_init(&priv->klist_drivers, NULL, NULL);
926b8c5cec2SKay Sievers 
927b8c5cec2SKay Sievers 	retval = add_probe_files(bus);
928b8c5cec2SKay Sievers 	if (retval)
929b8c5cec2SKay Sievers 		goto bus_probe_files_fail;
930b8c5cec2SKay Sievers 
9311bb6881aSCornelia Huck 	retval = bus_add_attrs(bus);
9321bb6881aSCornelia Huck 	if (retval)
9331bb6881aSCornelia Huck 		goto bus_attrs_fail;
9341da177e4SLinus Torvalds 
9357dc72b28SGreg Kroah-Hartman 	pr_debug("bus: '%s': registered\n", bus->name);
9361da177e4SLinus Torvalds 	return 0;
9371da177e4SLinus Torvalds 
9381bb6881aSCornelia Huck bus_attrs_fail:
939b8c5cec2SKay Sievers 	remove_probe_files(bus);
940b8c5cec2SKay Sievers bus_probe_files_fail:
941c6f7e72aSGreg Kroah-Hartman 	kset_unregister(bus->p->drivers_kset);
9421da177e4SLinus Torvalds bus_drivers_fail:
943c6f7e72aSGreg Kroah-Hartman 	kset_unregister(bus->p->devices_kset);
9441da177e4SLinus Torvalds bus_devices_fail:
9457ac1cf4aSKay Sievers 	bus_remove_file(bus, &bus_attr_uevent);
9467ac1cf4aSKay Sievers bus_uevent_fail:
947c6f7e72aSGreg Kroah-Hartman 	kset_unregister(&bus->p->subsys);
948c6f7e72aSGreg Kroah-Hartman 	kfree(bus->p);
9491da177e4SLinus Torvalds out:
950f48f3febSDave Young 	bus->p = NULL;
9511da177e4SLinus Torvalds 	return retval;
9521da177e4SLinus Torvalds }
9534a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_register);
9541da177e4SLinus Torvalds 
9551da177e4SLinus Torvalds /**
9561da177e4SLinus Torvalds  * bus_unregister - remove a bus from the system
9571da177e4SLinus Torvalds  * @bus: bus.
9581da177e4SLinus Torvalds  *
9591da177e4SLinus Torvalds  * Unregister the child subsystems and the bus itself.
960fc1ede58SGreg Kroah-Hartman  * Finally, we call bus_put() to release the refcount
9611da177e4SLinus Torvalds  */
9621da177e4SLinus Torvalds void bus_unregister(struct bus_type *bus)
9631da177e4SLinus Torvalds {
9647dc72b28SGreg Kroah-Hartman 	pr_debug("bus: '%s': unregistering\n", bus->name);
9651da177e4SLinus Torvalds 	bus_remove_attrs(bus);
966b8c5cec2SKay Sievers 	remove_probe_files(bus);
967c6f7e72aSGreg Kroah-Hartman 	kset_unregister(bus->p->drivers_kset);
968c6f7e72aSGreg Kroah-Hartman 	kset_unregister(bus->p->devices_kset);
9697ac1cf4aSKay Sievers 	bus_remove_file(bus, &bus_attr_uevent);
970c6f7e72aSGreg Kroah-Hartman 	kset_unregister(&bus->p->subsys);
971c6f7e72aSGreg Kroah-Hartman 	kfree(bus->p);
972f48f3febSDave Young 	bus->p = NULL;
9731da177e4SLinus Torvalds }
9744a3ad20cSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_unregister);
9751da177e4SLinus Torvalds 
976116af378SBenjamin Herrenschmidt int bus_register_notifier(struct bus_type *bus, struct notifier_block *nb)
977116af378SBenjamin Herrenschmidt {
978c6f7e72aSGreg Kroah-Hartman 	return blocking_notifier_chain_register(&bus->p->bus_notifier, nb);
979116af378SBenjamin Herrenschmidt }
980116af378SBenjamin Herrenschmidt EXPORT_SYMBOL_GPL(bus_register_notifier);
981116af378SBenjamin Herrenschmidt 
982116af378SBenjamin Herrenschmidt int bus_unregister_notifier(struct bus_type *bus, struct notifier_block *nb)
983116af378SBenjamin Herrenschmidt {
984c6f7e72aSGreg Kroah-Hartman 	return blocking_notifier_chain_unregister(&bus->p->bus_notifier, nb);
985116af378SBenjamin Herrenschmidt }
986116af378SBenjamin Herrenschmidt EXPORT_SYMBOL_GPL(bus_unregister_notifier);
987116af378SBenjamin Herrenschmidt 
9880fed80f7SGreg Kroah-Hartman struct kset *bus_get_kset(struct bus_type *bus)
9890fed80f7SGreg Kroah-Hartman {
990c6f7e72aSGreg Kroah-Hartman 	return &bus->p->subsys;
9910fed80f7SGreg Kroah-Hartman }
9920fed80f7SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_get_kset);
9930fed80f7SGreg Kroah-Hartman 
994b249072eSGreg Kroah-Hartman struct klist *bus_get_device_klist(struct bus_type *bus)
995b249072eSGreg Kroah-Hartman {
996c6f7e72aSGreg Kroah-Hartman 	return &bus->p->klist_devices;
997b249072eSGreg Kroah-Hartman }
998b249072eSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_get_device_klist);
999b249072eSGreg Kroah-Hartman 
100099178b03SGreg Kroah-Hartman /*
100199178b03SGreg Kroah-Hartman  * Yes, this forcably breaks the klist abstraction temporarily.  It
100299178b03SGreg Kroah-Hartman  * just wants to sort the klist, not change reference counts and
100399178b03SGreg Kroah-Hartman  * take/drop locks rapidly in the process.  It does all this while
100499178b03SGreg Kroah-Hartman  * holding the lock for the list, so objects can't otherwise be
100599178b03SGreg Kroah-Hartman  * added/removed while we're swizzling.
100699178b03SGreg Kroah-Hartman  */
100799178b03SGreg Kroah-Hartman static void device_insertion_sort_klist(struct device *a, struct list_head *list,
100899178b03SGreg Kroah-Hartman 					int (*compare)(const struct device *a,
100999178b03SGreg Kroah-Hartman 							const struct device *b))
101099178b03SGreg Kroah-Hartman {
101199178b03SGreg Kroah-Hartman 	struct list_head *pos;
101299178b03SGreg Kroah-Hartman 	struct klist_node *n;
1013ae1b4171SGreg Kroah-Hartman 	struct device_private *dev_prv;
101499178b03SGreg Kroah-Hartman 	struct device *b;
101599178b03SGreg Kroah-Hartman 
101699178b03SGreg Kroah-Hartman 	list_for_each(pos, list) {
101799178b03SGreg Kroah-Hartman 		n = container_of(pos, struct klist_node, n_node);
1018ae1b4171SGreg Kroah-Hartman 		dev_prv = to_device_private_bus(n);
1019ae1b4171SGreg Kroah-Hartman 		b = dev_prv->device;
102099178b03SGreg Kroah-Hartman 		if (compare(a, b) <= 0) {
1021ae1b4171SGreg Kroah-Hartman 			list_move_tail(&a->p->knode_bus.n_node,
1022ae1b4171SGreg Kroah-Hartman 				       &b->p->knode_bus.n_node);
102399178b03SGreg Kroah-Hartman 			return;
102499178b03SGreg Kroah-Hartman 		}
102599178b03SGreg Kroah-Hartman 	}
1026ae1b4171SGreg Kroah-Hartman 	list_move_tail(&a->p->knode_bus.n_node, list);
102799178b03SGreg Kroah-Hartman }
102899178b03SGreg Kroah-Hartman 
102999178b03SGreg Kroah-Hartman void bus_sort_breadthfirst(struct bus_type *bus,
103099178b03SGreg Kroah-Hartman 			   int (*compare)(const struct device *a,
103199178b03SGreg Kroah-Hartman 					  const struct device *b))
103299178b03SGreg Kroah-Hartman {
103399178b03SGreg Kroah-Hartman 	LIST_HEAD(sorted_devices);
103499178b03SGreg Kroah-Hartman 	struct list_head *pos, *tmp;
103599178b03SGreg Kroah-Hartman 	struct klist_node *n;
1036ae1b4171SGreg Kroah-Hartman 	struct device_private *dev_prv;
103799178b03SGreg Kroah-Hartman 	struct device *dev;
103899178b03SGreg Kroah-Hartman 	struct klist *device_klist;
103999178b03SGreg Kroah-Hartman 
104099178b03SGreg Kroah-Hartman 	device_klist = bus_get_device_klist(bus);
104199178b03SGreg Kroah-Hartman 
104299178b03SGreg Kroah-Hartman 	spin_lock(&device_klist->k_lock);
104399178b03SGreg Kroah-Hartman 	list_for_each_safe(pos, tmp, &device_klist->k_list) {
104499178b03SGreg Kroah-Hartman 		n = container_of(pos, struct klist_node, n_node);
1045ae1b4171SGreg Kroah-Hartman 		dev_prv = to_device_private_bus(n);
1046ae1b4171SGreg Kroah-Hartman 		dev = dev_prv->device;
104799178b03SGreg Kroah-Hartman 		device_insertion_sort_klist(dev, &sorted_devices, compare);
104899178b03SGreg Kroah-Hartman 	}
104999178b03SGreg Kroah-Hartman 	list_splice(&sorted_devices, &device_klist->k_list);
105099178b03SGreg Kroah-Hartman 	spin_unlock(&device_klist->k_lock);
105199178b03SGreg Kroah-Hartman }
105299178b03SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(bus_sort_breadthfirst);
105399178b03SGreg Kroah-Hartman 
10541da177e4SLinus Torvalds int __init buses_init(void)
10551da177e4SLinus Torvalds {
105659a54833SGreg Kroah-Hartman 	bus_kset = kset_create_and_add("bus", &bus_uevent_ops, NULL);
105759a54833SGreg Kroah-Hartman 	if (!bus_kset)
105859a54833SGreg Kroah-Hartman 		return -ENOMEM;
105959a54833SGreg Kroah-Hartman 	return 0;
10601da177e4SLinus Torvalds }
1061