2 * bus.c - bus driver management
4 * Copyright (c) 2002-3 Patrick Mochel
5 * Copyright (c) 2002-3 Open Source Development Labs
7 * This file is released under the GPLv2
11 #include <linux/config.h>
12 #include <linux/device.h>
13 #include <linux/module.h>
14 #include <linux/errno.h>
15 #include <linux/init.h>
16 #include <linux/string.h>
18 #include "power/power.h"
20 #define to_dev(node) container_of(node, struct device, bus_list)
21 #define to_drv(node) container_of(node, struct device_driver, kobj.entry)
23 #define to_bus_attr(_attr) container_of(_attr, struct bus_attribute, attr)
24 #define to_bus(obj) container_of(obj, struct bus_type, subsys.kset.kobj)
27 * sysfs bindings for drivers
30 #define to_drv_attr(_attr) container_of(_attr, struct driver_attribute, attr)
31 #define to_driver(obj) container_of(obj, struct device_driver, kobj)
35 drv_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
37 struct driver_attribute * drv_attr = to_drv_attr(attr);
38 struct device_driver * drv = to_driver(kobj);
42 ret = drv_attr->show(drv, buf);
47 drv_attr_store(struct kobject * kobj, struct attribute * attr,
48 const char * buf, size_t count)
50 struct driver_attribute * drv_attr = to_drv_attr(attr);
51 struct device_driver * drv = to_driver(kobj);
55 ret = drv_attr->store(drv, buf, count);
59 static struct sysfs_ops driver_sysfs_ops = {
60 .show = drv_attr_show,
61 .store = drv_attr_store,
65 static void driver_release(struct kobject * kobj)
67 struct device_driver * drv = to_driver(kobj);
68 complete(&drv->unloaded);
71 static struct kobj_type ktype_driver = {
72 .sysfs_ops = &driver_sysfs_ops,
73 .release = driver_release,
78 * sysfs bindings for buses
83 bus_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
85 struct bus_attribute * bus_attr = to_bus_attr(attr);
86 struct bus_type * bus = to_bus(kobj);
90 ret = bus_attr->show(bus, buf);
95 bus_attr_store(struct kobject * kobj, struct attribute * attr,
96 const char * buf, size_t count)
98 struct bus_attribute * bus_attr = to_bus_attr(attr);
99 struct bus_type * bus = to_bus(kobj);
103 ret = bus_attr->store(bus, buf, count);
107 static struct sysfs_ops bus_sysfs_ops = {
108 .show = bus_attr_show,
109 .store = bus_attr_store,
112 int bus_create_file(struct bus_type * bus, struct bus_attribute * attr)
116 error = sysfs_create_file(&bus->subsys.kset.kobj, &attr->attr);
123 void bus_remove_file(struct bus_type * bus, struct bus_attribute * attr)
126 sysfs_remove_file(&bus->subsys.kset.kobj, &attr->attr);
131 static struct kobj_type ktype_bus = {
132 .sysfs_ops = &bus_sysfs_ops,
136 decl_subsys(bus, &ktype_bus, NULL);
138 static int __bus_for_each_dev(struct bus_type *bus, struct device *start,
139 void *data, int (*fn)(struct device *, void *))
141 struct list_head *head;
145 if (!(bus = get_bus(bus)))
148 head = &bus->devices.list;
149 dev = list_prepare_entry(start, head, bus_list);
150 list_for_each_entry_continue(dev, head, bus_list) {
152 error = fn(dev, data);
161 static int __bus_for_each_drv(struct bus_type *bus, struct device_driver *start,
162 void * data, int (*fn)(struct device_driver *, void *))
164 struct list_head *head;
165 struct device_driver *drv;
168 if (!(bus = get_bus(bus)))
171 head = &bus->drivers.list;
172 drv = list_prepare_entry(start, head, kobj.entry);
173 list_for_each_entry_continue(drv, head, kobj.entry) {
175 error = fn(drv, data);
185 * bus_for_each_dev - device iterator.
187 * @start: device to start iterating from.
188 * @data: data for the callback.
189 * @fn: function to be called for each device.
191 * Iterate over @bus's list of devices, and call @fn for each,
192 * passing it @data. If @start is not NULL, we use that device to
193 * begin iterating from.
195 * We check the return of @fn each time. If it returns anything
196 * other than 0, we break out and return that value.
198 * NOTE: The device that returns a non-zero value is not retained
199 * in any way, nor is its refcount incremented. If the caller needs
200 * to retain this data, it should do, and increment the reference
201 * count in the supplied callback.
204 int bus_for_each_dev(struct bus_type * bus, struct device * start,
205 void * data, int (*fn)(struct device *, void *))
209 down_read(&bus->subsys.rwsem);
210 ret = __bus_for_each_dev(bus, start, data, fn);
211 up_read(&bus->subsys.rwsem);
216 * bus_for_each_drv - driver iterator
217 * @bus: bus we're dealing with.
218 * @start: driver to start iterating on.
219 * @data: data to pass to the callback.
220 * @fn: function to call for each driver.
222 * This is nearly identical to the device iterator above.
223 * We iterate over each driver that belongs to @bus, and call
224 * @fn for each. If @fn returns anything but 0, we break out
225 * and return it. If @start is not NULL, we use it as the head
228 * NOTE: we don't return the driver that returns a non-zero
229 * value, nor do we leave the reference count incremented for that
230 * driver. If the caller needs to know that info, it must set it
231 * in the callback. It must also be sure to increment the refcount
232 * so it doesn't disappear before returning to the caller.
235 int bus_for_each_drv(struct bus_type * bus, struct device_driver * start,
236 void * data, int (*fn)(struct device_driver *, void *))
240 down_read(&bus->subsys.rwsem);
241 ret = __bus_for_each_drv(bus, start, data, fn);
242 up_read(&bus->subsys.rwsem);
247 * device_bind_driver - bind a driver to one device.
250 * Allow manual attachment of a driver to a device.
251 * Caller must have already set @dev->driver.
253 * Note that this does not modify the bus reference count
254 * nor take the bus's rwsem. Please verify those are accounted
255 * for before calling this. (It is ok to call with no other effort
256 * from a driver's probe() method.)
259 void device_bind_driver(struct device * dev)
261 pr_debug("bound device '%s' to driver '%s'\n",
262 dev->bus_id, dev->driver->name);
263 list_add_tail(&dev->driver_list, &dev->driver->devices);
264 sysfs_create_link(&dev->driver->kobj, &dev->kobj,
265 kobject_name(&dev->kobj));
266 sysfs_create_link(&dev->kobj, &dev->driver->kobj, "driver");
271 * driver_probe_device - attempt to bind device & driver.
275 * First, we call the bus's match function, if one present, which
276 * should compare the device IDs the driver supports with the
277 * device IDs of the device. Note we don't do this ourselves
278 * because we don't know the format of the ID structures, nor what
279 * is to be considered a match and what is not.
281 * If we find a match, we call @drv->probe(@dev) if it exists, and
282 * call device_bind_driver() above.
284 int driver_probe_device(struct device_driver * drv, struct device * dev)
288 if (drv->bus->match && !drv->bus->match(dev, drv))
294 error = drv->probe(dev);
302 device_bind_driver(dev);
308 * device_attach - try to attach device to a driver.
311 * Walk the list of drivers that the bus has and call
312 * driver_probe_device() for each pair. If a compatible
313 * pair is found, break out and return.
315 int device_attach(struct device * dev)
317 struct bus_type * bus = dev->bus;
318 struct list_head * entry;
322 device_bind_driver(dev);
327 list_for_each(entry, &bus->drivers.list) {
328 struct device_driver * drv = to_drv(entry);
329 error = driver_probe_device(drv, dev);
331 /* success, driver matched */
333 if (error != -ENODEV && error != -ENXIO)
334 /* driver matched but the probe failed */
336 "%s: probe of %s failed with error %d\n",
337 drv->name, dev->bus_id, error);
346 * driver_attach - try to bind driver to devices.
349 * Walk the list of devices that the bus has on it and try to
350 * match the driver with each one. If driver_probe_device()
351 * returns 0 and the @dev->driver is set, we've found a
354 * Note that we ignore the -ENODEV error from driver_probe_device(),
355 * since it's perfectly valid for a driver not to bind to any devices.
357 void driver_attach(struct device_driver * drv)
359 struct bus_type * bus = drv->bus;
360 struct list_head * entry;
366 list_for_each(entry, &bus->devices.list) {
367 struct device * dev = container_of(entry, struct device, bus_list);
369 error = driver_probe_device(drv, dev);
370 if (error && (error != -ENODEV))
371 /* driver matched but the probe failed */
373 "%s: probe of %s failed with error %d\n",
374 drv->name, dev->bus_id, error);
381 * device_release_driver - manually detach device from driver.
384 * Manually detach device from driver.
385 * Note that this is called without incrementing the bus
386 * reference count nor taking the bus's rwsem. Be sure that
387 * those are accounted for before calling this function.
390 void device_release_driver(struct device * dev)
392 struct device_driver * drv;
397 sysfs_remove_link(&drv->kobj, kobject_name(&dev->kobj));
398 sysfs_remove_link(&dev->kobj, "driver");
399 list_del_init(&dev->driver_list);
409 * driver_detach - detach driver from all devices it controls.
413 static void driver_detach(struct device_driver * drv)
415 while (!list_empty(&drv->devices)) {
416 struct device * dev = container_of(drv->devices.next, struct device, driver_list);
417 device_release_driver(dev);
421 static int device_add_attrs(struct bus_type * bus, struct device * dev)
426 if (bus->dev_attrs) {
427 for (i = 0; attr_name(bus->dev_attrs[i]); i++) {
428 error = device_create_file(dev,&bus->dev_attrs[i]);
437 device_remove_file(dev,&bus->dev_attrs[i]);
442 static void device_remove_attrs(struct bus_type * bus, struct device * dev)
446 if (bus->dev_attrs) {
447 for (i = 0; attr_name(bus->dev_attrs[i]); i++)
448 device_remove_file(dev,&bus->dev_attrs[i]);
454 * bus_add_device - add device to bus
455 * @dev: device being added
457 * - Add the device to its bus's list of devices.
458 * - Try to attach to driver.
459 * - Create link to device's physical location.
461 int bus_add_device(struct device * dev)
463 struct bus_type * bus = get_bus(dev->bus);
467 down_write(&dev->bus->subsys.rwsem);
468 pr_debug("bus %s: add device %s\n", bus->name, dev->bus_id);
469 list_add_tail(&dev->bus_list, &dev->bus->devices.list);
471 up_write(&dev->bus->subsys.rwsem);
472 device_add_attrs(bus, dev);
473 sysfs_create_link(&bus->devices.kobj, &dev->kobj, dev->bus_id);
474 sysfs_create_link(&dev->kobj, &dev->bus->subsys.kset.kobj, "bus");
480 * bus_remove_device - remove device from bus
481 * @dev: device to be removed
483 * - Remove symlink from bus's directory.
484 * - Delete device from bus's list.
485 * - Detach from its driver.
486 * - Drop reference taken in bus_add_device().
488 void bus_remove_device(struct device * dev)
491 sysfs_remove_link(&dev->kobj, "bus");
492 sysfs_remove_link(&dev->bus->devices.kobj, dev->bus_id);
493 device_remove_attrs(dev->bus, dev);
494 down_write(&dev->bus->subsys.rwsem);
495 pr_debug("bus %s: remove device %s\n", dev->bus->name, dev->bus_id);
496 device_release_driver(dev);
497 list_del_init(&dev->bus_list);
498 up_write(&dev->bus->subsys.rwsem);
503 static int driver_add_attrs(struct bus_type * bus, struct device_driver * drv)
508 if (bus->drv_attrs) {
509 for (i = 0; attr_name(bus->drv_attrs[i]); i++) {
510 error = driver_create_file(drv, &bus->drv_attrs[i]);
519 driver_remove_file(drv, &bus->drv_attrs[i]);
524 static void driver_remove_attrs(struct bus_type * bus, struct device_driver * drv)
528 if (bus->drv_attrs) {
529 for (i = 0; attr_name(bus->drv_attrs[i]); i++)
530 driver_remove_file(drv, &bus->drv_attrs[i]);
536 * bus_add_driver - Add a driver to the bus.
540 int bus_add_driver(struct device_driver * drv)
542 struct bus_type * bus = get_bus(drv->bus);
546 pr_debug("bus %s: add driver %s\n", bus->name, drv->name);
547 error = kobject_set_name(&drv->kobj, "%s", drv->name);
552 drv->kobj.kset = &bus->drivers;
553 if ((error = kobject_register(&drv->kobj))) {
558 down_write(&bus->subsys.rwsem);
560 up_write(&bus->subsys.rwsem);
561 module_add_driver(drv->owner, drv);
563 driver_add_attrs(bus, drv);
570 * bus_remove_driver - delete driver from bus's knowledge.
573 * Detach the driver from the devices it controls, and remove
574 * it from its bus's list of drivers. Finally, we drop the reference
575 * to the bus we took in bus_add_driver().
578 void bus_remove_driver(struct device_driver * drv)
581 driver_remove_attrs(drv->bus, drv);
582 down_write(&drv->bus->subsys.rwsem);
583 pr_debug("bus %s: remove driver %s\n", drv->bus->name, drv->name);
585 up_write(&drv->bus->subsys.rwsem);
586 module_remove_driver(drv);
587 kobject_unregister(&drv->kobj);
593 /* Helper for bus_rescan_devices's iter */
594 static int bus_rescan_devices_helper(struct device *dev, void *data)
598 if (!dev->driver && device_attach(dev))
606 * bus_rescan_devices - rescan devices on the bus for possible drivers
607 * @bus: the bus to scan.
609 * This function will look for devices on the bus with no driver
610 * attached and rescan it against existing drivers to see if it
611 * matches any. Calls device_attach(). Returns the number of devices
612 * that were sucessfully bound to a driver.
614 int bus_rescan_devices(struct bus_type * bus)
618 down_write(&bus->subsys.rwsem);
619 __bus_for_each_dev(bus, NULL, &count, bus_rescan_devices_helper);
620 up_write(&bus->subsys.rwsem);
626 struct bus_type * get_bus(struct bus_type * bus)
628 return bus ? container_of(subsys_get(&bus->subsys), struct bus_type, subsys) : NULL;
631 void put_bus(struct bus_type * bus)
633 subsys_put(&bus->subsys);
638 * find_bus - locate bus by name.
639 * @name: name of bus.
641 * Call kset_find_obj() to iterate over list of buses to
642 * find a bus by name. Return bus if found.
644 * Note that kset_find_obj increments bus' reference count.
647 struct bus_type * find_bus(char * name)
649 struct kobject * k = kset_find_obj(&bus_subsys.kset, name);
650 return k ? to_bus(k) : NULL;
655 * bus_add_attrs - Add default attributes for this bus.
656 * @bus: Bus that has just been registered.
659 static int bus_add_attrs(struct bus_type * bus)
664 if (bus->bus_attrs) {
665 for (i = 0; attr_name(bus->bus_attrs[i]); i++) {
666 if ((error = bus_create_file(bus,&bus->bus_attrs[i])))
674 bus_remove_file(bus,&bus->bus_attrs[i]);
678 static void bus_remove_attrs(struct bus_type * bus)
682 if (bus->bus_attrs) {
683 for (i = 0; attr_name(bus->bus_attrs[i]); i++)
684 bus_remove_file(bus,&bus->bus_attrs[i]);
689 * bus_register - register a bus with the system.
692 * Once we have that, we registered the bus with the kobject
693 * infrastructure, then register the children subsystems it has:
694 * the devices and drivers that belong to the bus.
696 int bus_register(struct bus_type * bus)
700 retval = kobject_set_name(&bus->subsys.kset.kobj, "%s", bus->name);
704 subsys_set_kset(bus, bus_subsys);
705 retval = subsystem_register(&bus->subsys);
709 kobject_set_name(&bus->devices.kobj, "devices");
710 bus->devices.subsys = &bus->subsys;
711 retval = kset_register(&bus->devices);
713 goto bus_devices_fail;
715 kobject_set_name(&bus->drivers.kobj, "drivers");
716 bus->drivers.subsys = &bus->subsys;
717 bus->drivers.ktype = &ktype_driver;
718 retval = kset_register(&bus->drivers);
720 goto bus_drivers_fail;
723 pr_debug("bus type '%s' registered\n", bus->name);
727 kset_unregister(&bus->devices);
729 subsystem_unregister(&bus->subsys);
736 * bus_unregister - remove a bus from the system
739 * Unregister the child subsystems and the bus itself.
740 * Finally, we call put_bus() to release the refcount
742 void bus_unregister(struct bus_type * bus)
744 pr_debug("bus %s: unregistering\n", bus->name);
745 bus_remove_attrs(bus);
746 kset_unregister(&bus->drivers);
747 kset_unregister(&bus->devices);
748 subsystem_unregister(&bus->subsys);
751 int __init buses_init(void)
753 return subsystem_register(&bus_subsys);
757 EXPORT_SYMBOL_GPL(bus_for_each_dev);
758 EXPORT_SYMBOL_GPL(bus_for_each_drv);
760 EXPORT_SYMBOL_GPL(driver_probe_device);
761 EXPORT_SYMBOL_GPL(device_bind_driver);
762 EXPORT_SYMBOL_GPL(device_release_driver);
763 EXPORT_SYMBOL_GPL(device_attach);
764 EXPORT_SYMBOL_GPL(driver_attach);
766 EXPORT_SYMBOL_GPL(bus_add_device);
767 EXPORT_SYMBOL_GPL(bus_remove_device);
768 EXPORT_SYMBOL_GPL(bus_register);
769 EXPORT_SYMBOL_GPL(bus_unregister);
770 EXPORT_SYMBOL_GPL(bus_rescan_devices);
771 EXPORT_SYMBOL_GPL(get_bus);
772 EXPORT_SYMBOL_GPL(put_bus);
773 EXPORT_SYMBOL_GPL(find_bus);
775 EXPORT_SYMBOL_GPL(bus_create_file);
776 EXPORT_SYMBOL_GPL(bus_remove_file);