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)
22 #define to_bus_attr(_attr) container_of(_attr, struct bus_attribute, attr)
23 #define to_bus(obj) container_of(obj, struct bus_type, subsys.kset.kobj)
26 * sysfs bindings for drivers
29 #define to_drv_attr(_attr) container_of(_attr, struct driver_attribute, attr)
30 #define to_driver(obj) container_of(obj, struct device_driver, kobj)
34 drv_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
36 struct driver_attribute * drv_attr = to_drv_attr(attr);
37 struct device_driver * drv = to_driver(kobj);
41 ret = drv_attr->show(drv, buf);
46 drv_attr_store(struct kobject * kobj, struct attribute * attr,
47 const char * buf, size_t count)
49 struct driver_attribute * drv_attr = to_drv_attr(attr);
50 struct device_driver * drv = to_driver(kobj);
54 ret = drv_attr->store(drv, buf, count);
58 static struct sysfs_ops driver_sysfs_ops = {
59 .show = drv_attr_show,
60 .store = drv_attr_store,
64 static void driver_release(struct kobject * kobj)
66 struct device_driver * drv = to_driver(kobj);
67 complete(&drv->unloaded);
70 static struct kobj_type ktype_driver = {
71 .sysfs_ops = &driver_sysfs_ops,
72 .release = driver_release,
77 * sysfs bindings for buses
82 bus_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
84 struct bus_attribute * bus_attr = to_bus_attr(attr);
85 struct bus_type * bus = to_bus(kobj);
89 ret = bus_attr->show(bus, buf);
94 bus_attr_store(struct kobject * kobj, struct attribute * attr,
95 const char * buf, size_t count)
97 struct bus_attribute * bus_attr = to_bus_attr(attr);
98 struct bus_type * bus = to_bus(kobj);
102 ret = bus_attr->store(bus, buf, count);
106 static struct sysfs_ops bus_sysfs_ops = {
107 .show = bus_attr_show,
108 .store = bus_attr_store,
111 int bus_create_file(struct bus_type * bus, struct bus_attribute * attr)
115 error = sysfs_create_file(&bus->subsys.kset.kobj, &attr->attr);
122 void bus_remove_file(struct bus_type * bus, struct bus_attribute * attr)
125 sysfs_remove_file(&bus->subsys.kset.kobj, &attr->attr);
130 static struct kobj_type ktype_bus = {
131 .sysfs_ops = &bus_sysfs_ops,
135 decl_subsys(bus, &ktype_bus, NULL);
138 static int __bus_for_each_drv(struct bus_type *bus, struct device_driver *start,
139 void * data, int (*fn)(struct device_driver *, void *))
141 struct list_head *head;
142 struct device_driver *drv;
145 if (!(bus = get_bus(bus)))
148 head = &bus->drivers.list;
149 drv = list_prepare_entry(start, head, kobj.entry);
150 list_for_each_entry_continue(drv, head, kobj.entry) {
152 error = fn(drv, data);
162 static struct device * next_device(struct klist_iter * i)
164 struct klist_node * n = klist_next(i);
165 return n ? container_of(n, struct device, knode_bus) : NULL;
169 * bus_for_each_dev - device iterator.
171 * @start: device to start iterating from.
172 * @data: data for the callback.
173 * @fn: function to be called for each device.
175 * Iterate over @bus's list of devices, and call @fn for each,
176 * passing it @data. If @start is not NULL, we use that device to
177 * begin iterating from.
179 * We check the return of @fn each time. If it returns anything
180 * other than 0, we break out and return that value.
182 * NOTE: The device that returns a non-zero value is not retained
183 * in any way, nor is its refcount incremented. If the caller needs
184 * to retain this data, it should do, and increment the reference
185 * count in the supplied callback.
188 int bus_for_each_dev(struct bus_type * bus, struct device * start,
189 void * data, int (*fn)(struct device *, void *))
198 klist_iter_init_node(&bus->klist_devices, &i,
199 (start ? &start->knode_bus : NULL));
200 while ((dev = next_device(&i)) && !error)
201 error = fn(dev, data);
207 * bus_for_each_drv - driver iterator
208 * @bus: bus we're dealing with.
209 * @start: driver to start iterating on.
210 * @data: data to pass to the callback.
211 * @fn: function to call for each driver.
213 * This is nearly identical to the device iterator above.
214 * We iterate over each driver that belongs to @bus, and call
215 * @fn for each. If @fn returns anything but 0, we break out
216 * and return it. If @start is not NULL, we use it as the head
219 * NOTE: we don't return the driver that returns a non-zero
220 * value, nor do we leave the reference count incremented for that
221 * driver. If the caller needs to know that info, it must set it
222 * in the callback. It must also be sure to increment the refcount
223 * so it doesn't disappear before returning to the caller.
226 int bus_for_each_drv(struct bus_type * bus, struct device_driver * start,
227 void * data, int (*fn)(struct device_driver *, void *))
231 down_read(&bus->subsys.rwsem);
232 ret = __bus_for_each_drv(bus, start, data, fn);
233 up_read(&bus->subsys.rwsem);
237 static int device_add_attrs(struct bus_type * bus, struct device * dev)
242 if (bus->dev_attrs) {
243 for (i = 0; attr_name(bus->dev_attrs[i]); i++) {
244 error = device_create_file(dev,&bus->dev_attrs[i]);
253 device_remove_file(dev,&bus->dev_attrs[i]);
258 static void device_remove_attrs(struct bus_type * bus, struct device * dev)
262 if (bus->dev_attrs) {
263 for (i = 0; attr_name(bus->dev_attrs[i]); i++)
264 device_remove_file(dev,&bus->dev_attrs[i]);
270 * bus_add_device - add device to bus
271 * @dev: device being added
273 * - Add the device to its bus's list of devices.
274 * - Try to attach to driver.
275 * - Create link to device's physical location.
277 int bus_add_device(struct device * dev)
279 struct bus_type * bus = get_bus(dev->bus);
283 down_write(&dev->bus->subsys.rwsem);
284 pr_debug("bus %s: add device %s\n", bus->name, dev->bus_id);
285 list_add_tail(&dev->bus_list, &dev->bus->devices.list);
287 up_write(&dev->bus->subsys.rwsem);
288 klist_add_tail(&bus->klist_devices, &dev->knode_bus);
289 device_add_attrs(bus, dev);
290 sysfs_create_link(&bus->devices.kobj, &dev->kobj, dev->bus_id);
291 sysfs_create_link(&dev->kobj, &dev->bus->subsys.kset.kobj, "bus");
297 * bus_remove_device - remove device from bus
298 * @dev: device to be removed
300 * - Remove symlink from bus's directory.
301 * - Delete device from bus's list.
302 * - Detach from its driver.
303 * - Drop reference taken in bus_add_device().
305 void bus_remove_device(struct device * dev)
308 sysfs_remove_link(&dev->kobj, "bus");
309 sysfs_remove_link(&dev->bus->devices.kobj, dev->bus_id);
310 device_remove_attrs(dev->bus, dev);
311 klist_remove(&dev->knode_bus);
312 down_write(&dev->bus->subsys.rwsem);
313 pr_debug("bus %s: remove device %s\n", dev->bus->name, dev->bus_id);
314 device_release_driver(dev);
315 list_del_init(&dev->bus_list);
316 up_write(&dev->bus->subsys.rwsem);
321 static int driver_add_attrs(struct bus_type * bus, struct device_driver * drv)
326 if (bus->drv_attrs) {
327 for (i = 0; attr_name(bus->drv_attrs[i]); i++) {
328 error = driver_create_file(drv, &bus->drv_attrs[i]);
337 driver_remove_file(drv, &bus->drv_attrs[i]);
342 static void driver_remove_attrs(struct bus_type * bus, struct device_driver * drv)
346 if (bus->drv_attrs) {
347 for (i = 0; attr_name(bus->drv_attrs[i]); i++)
348 driver_remove_file(drv, &bus->drv_attrs[i]);
354 * bus_add_driver - Add a driver to the bus.
358 int bus_add_driver(struct device_driver * drv)
360 struct bus_type * bus = get_bus(drv->bus);
364 pr_debug("bus %s: add driver %s\n", bus->name, drv->name);
365 error = kobject_set_name(&drv->kobj, "%s", drv->name);
370 drv->kobj.kset = &bus->drivers;
371 if ((error = kobject_register(&drv->kobj))) {
376 down_write(&bus->subsys.rwsem);
378 up_write(&bus->subsys.rwsem);
379 module_add_driver(drv->owner, drv);
381 driver_add_attrs(bus, drv);
388 * bus_remove_driver - delete driver from bus's knowledge.
391 * Detach the driver from the devices it controls, and remove
392 * it from its bus's list of drivers. Finally, we drop the reference
393 * to the bus we took in bus_add_driver().
396 void bus_remove_driver(struct device_driver * drv)
399 driver_remove_attrs(drv->bus, drv);
400 down_write(&drv->bus->subsys.rwsem);
401 pr_debug("bus %s: remove driver %s\n", drv->bus->name, drv->name);
403 up_write(&drv->bus->subsys.rwsem);
404 module_remove_driver(drv);
405 kobject_unregister(&drv->kobj);
411 /* Helper for bus_rescan_devices's iter */
412 static int bus_rescan_devices_helper(struct device *dev, void *data)
416 if (!dev->driver && device_attach(dev))
424 * bus_rescan_devices - rescan devices on the bus for possible drivers
425 * @bus: the bus to scan.
427 * This function will look for devices on the bus with no driver
428 * attached and rescan it against existing drivers to see if it
429 * matches any. Calls device_attach(). Returns the number of devices
430 * that were sucessfully bound to a driver.
432 int bus_rescan_devices(struct bus_type * bus)
436 bus_for_each_dev(bus, NULL, &count, bus_rescan_devices_helper);
442 struct bus_type * get_bus(struct bus_type * bus)
444 return bus ? container_of(subsys_get(&bus->subsys), struct bus_type, subsys) : NULL;
447 void put_bus(struct bus_type * bus)
449 subsys_put(&bus->subsys);
454 * find_bus - locate bus by name.
455 * @name: name of bus.
457 * Call kset_find_obj() to iterate over list of buses to
458 * find a bus by name. Return bus if found.
460 * Note that kset_find_obj increments bus' reference count.
463 struct bus_type * find_bus(char * name)
465 struct kobject * k = kset_find_obj(&bus_subsys.kset, name);
466 return k ? to_bus(k) : NULL;
471 * bus_add_attrs - Add default attributes for this bus.
472 * @bus: Bus that has just been registered.
475 static int bus_add_attrs(struct bus_type * bus)
480 if (bus->bus_attrs) {
481 for (i = 0; attr_name(bus->bus_attrs[i]); i++) {
482 if ((error = bus_create_file(bus,&bus->bus_attrs[i])))
490 bus_remove_file(bus,&bus->bus_attrs[i]);
494 static void bus_remove_attrs(struct bus_type * bus)
498 if (bus->bus_attrs) {
499 for (i = 0; attr_name(bus->bus_attrs[i]); i++)
500 bus_remove_file(bus,&bus->bus_attrs[i]);
505 * bus_register - register a bus with the system.
508 * Once we have that, we registered the bus with the kobject
509 * infrastructure, then register the children subsystems it has:
510 * the devices and drivers that belong to the bus.
512 int bus_register(struct bus_type * bus)
516 retval = kobject_set_name(&bus->subsys.kset.kobj, "%s", bus->name);
520 subsys_set_kset(bus, bus_subsys);
521 retval = subsystem_register(&bus->subsys);
525 kobject_set_name(&bus->devices.kobj, "devices");
526 bus->devices.subsys = &bus->subsys;
527 retval = kset_register(&bus->devices);
529 goto bus_devices_fail;
531 kobject_set_name(&bus->drivers.kobj, "drivers");
532 bus->drivers.subsys = &bus->subsys;
533 bus->drivers.ktype = &ktype_driver;
534 retval = kset_register(&bus->drivers);
536 goto bus_drivers_fail;
538 klist_init(&bus->klist_devices);
541 pr_debug("bus type '%s' registered\n", bus->name);
545 kset_unregister(&bus->devices);
547 subsystem_unregister(&bus->subsys);
554 * bus_unregister - remove a bus from the system
557 * Unregister the child subsystems and the bus itself.
558 * Finally, we call put_bus() to release the refcount
560 void bus_unregister(struct bus_type * bus)
562 pr_debug("bus %s: unregistering\n", bus->name);
563 bus_remove_attrs(bus);
564 kset_unregister(&bus->drivers);
565 kset_unregister(&bus->devices);
566 subsystem_unregister(&bus->subsys);
569 int __init buses_init(void)
571 return subsystem_register(&bus_subsys);
575 EXPORT_SYMBOL_GPL(bus_for_each_dev);
576 EXPORT_SYMBOL_GPL(bus_for_each_drv);
578 EXPORT_SYMBOL_GPL(bus_add_device);
579 EXPORT_SYMBOL_GPL(bus_remove_device);
580 EXPORT_SYMBOL_GPL(bus_register);
581 EXPORT_SYMBOL_GPL(bus_unregister);
582 EXPORT_SYMBOL_GPL(bus_rescan_devices);
583 EXPORT_SYMBOL_GPL(get_bus);
584 EXPORT_SYMBOL_GPL(put_bus);
585 EXPORT_SYMBOL_GPL(find_bus);
587 EXPORT_SYMBOL_GPL(bus_create_file);
588 EXPORT_SYMBOL_GPL(bus_remove_file);