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 struct device * next_device(struct klist_iter * i)
140 struct klist_node * n = klist_next(i);
141 return n ? container_of(n, struct device, knode_bus) : NULL;
145 * bus_for_each_dev - device iterator.
147 * @start: device to start iterating from.
148 * @data: data for the callback.
149 * @fn: function to be called for each device.
151 * Iterate over @bus's list of devices, and call @fn for each,
152 * passing it @data. If @start is not NULL, we use that device to
153 * begin iterating from.
155 * We check the return of @fn each time. If it returns anything
156 * other than 0, we break out and return that value.
158 * NOTE: The device that returns a non-zero value is not retained
159 * in any way, nor is its refcount incremented. If the caller needs
160 * to retain this data, it should do, and increment the reference
161 * count in the supplied callback.
164 int bus_for_each_dev(struct bus_type * bus, struct device * start,
165 void * data, int (*fn)(struct device *, void *))
174 klist_iter_init_node(&bus->klist_devices, &i,
175 (start ? &start->knode_bus : NULL));
176 while ((dev = next_device(&i)) && !error)
177 error = fn(dev, data);
184 static struct device_driver * next_driver(struct klist_iter * i)
186 struct klist_node * n = klist_next(i);
187 return n ? container_of(n, struct device_driver, knode_bus) : NULL;
191 * bus_for_each_drv - driver iterator
192 * @bus: bus we're dealing with.
193 * @start: driver to start iterating on.
194 * @data: data to pass to the callback.
195 * @fn: function to call for each driver.
197 * This is nearly identical to the device iterator above.
198 * We iterate over each driver that belongs to @bus, and call
199 * @fn for each. If @fn returns anything but 0, we break out
200 * and return it. If @start is not NULL, we use it as the head
203 * NOTE: we don't return the driver that returns a non-zero
204 * value, nor do we leave the reference count incremented for that
205 * driver. If the caller needs to know that info, it must set it
206 * in the callback. It must also be sure to increment the refcount
207 * so it doesn't disappear before returning to the caller.
210 int bus_for_each_drv(struct bus_type * bus, struct device_driver * start,
211 void * data, int (*fn)(struct device_driver *, void *))
214 struct device_driver * drv;
220 klist_iter_init_node(&bus->klist_drivers, &i,
221 start ? &start->knode_bus : NULL);
222 while ((drv = next_driver(&i)) && !error)
223 error = fn(drv, data);
228 static int device_add_attrs(struct bus_type * bus, struct device * dev)
233 if (bus->dev_attrs) {
234 for (i = 0; attr_name(bus->dev_attrs[i]); i++) {
235 error = device_create_file(dev,&bus->dev_attrs[i]);
244 device_remove_file(dev,&bus->dev_attrs[i]);
249 static void device_remove_attrs(struct bus_type * bus, struct device * dev)
253 if (bus->dev_attrs) {
254 for (i = 0; attr_name(bus->dev_attrs[i]); i++)
255 device_remove_file(dev,&bus->dev_attrs[i]);
261 * bus_add_device - add device to bus
262 * @dev: device being added
264 * - Add the device to its bus's list of devices.
265 * - Try to attach to driver.
266 * - Create link to device's physical location.
268 int bus_add_device(struct device * dev)
270 struct bus_type * bus = get_bus(dev->bus);
274 down_write(&dev->bus->subsys.rwsem);
275 pr_debug("bus %s: add device %s\n", bus->name, dev->bus_id);
276 list_add_tail(&dev->bus_list, &dev->bus->devices.list);
278 up_write(&dev->bus->subsys.rwsem);
279 klist_add_tail(&bus->klist_devices, &dev->knode_bus);
280 device_add_attrs(bus, dev);
281 sysfs_create_link(&bus->devices.kobj, &dev->kobj, dev->bus_id);
282 sysfs_create_link(&dev->kobj, &dev->bus->subsys.kset.kobj, "bus");
288 * bus_remove_device - remove device from bus
289 * @dev: device to be removed
291 * - Remove symlink from bus's directory.
292 * - Delete device from bus's list.
293 * - Detach from its driver.
294 * - Drop reference taken in bus_add_device().
296 void bus_remove_device(struct device * dev)
299 sysfs_remove_link(&dev->kobj, "bus");
300 sysfs_remove_link(&dev->bus->devices.kobj, dev->bus_id);
301 device_remove_attrs(dev->bus, dev);
302 klist_remove(&dev->knode_bus);
303 down_write(&dev->bus->subsys.rwsem);
304 pr_debug("bus %s: remove device %s\n", dev->bus->name, dev->bus_id);
305 device_release_driver(dev);
306 list_del_init(&dev->bus_list);
307 up_write(&dev->bus->subsys.rwsem);
312 static int driver_add_attrs(struct bus_type * bus, struct device_driver * drv)
317 if (bus->drv_attrs) {
318 for (i = 0; attr_name(bus->drv_attrs[i]); i++) {
319 error = driver_create_file(drv, &bus->drv_attrs[i]);
328 driver_remove_file(drv, &bus->drv_attrs[i]);
333 static void driver_remove_attrs(struct bus_type * bus, struct device_driver * drv)
337 if (bus->drv_attrs) {
338 for (i = 0; attr_name(bus->drv_attrs[i]); i++)
339 driver_remove_file(drv, &bus->drv_attrs[i]);
345 * bus_add_driver - Add a driver to the bus.
349 int bus_add_driver(struct device_driver * drv)
351 struct bus_type * bus = get_bus(drv->bus);
355 pr_debug("bus %s: add driver %s\n", bus->name, drv->name);
356 error = kobject_set_name(&drv->kobj, "%s", drv->name);
361 drv->kobj.kset = &bus->drivers;
362 if ((error = kobject_register(&drv->kobj))) {
367 down_write(&bus->subsys.rwsem);
369 up_write(&bus->subsys.rwsem);
370 klist_add_tail(&bus->klist_drivers, &drv->knode_bus);
371 module_add_driver(drv->owner, drv);
373 driver_add_attrs(bus, drv);
380 * bus_remove_driver - delete driver from bus's knowledge.
383 * Detach the driver from the devices it controls, and remove
384 * it from its bus's list of drivers. Finally, we drop the reference
385 * to the bus we took in bus_add_driver().
388 void bus_remove_driver(struct device_driver * drv)
391 driver_remove_attrs(drv->bus, drv);
392 klist_remove(&drv->knode_bus);
393 down_write(&drv->bus->subsys.rwsem);
394 pr_debug("bus %s: remove driver %s\n", drv->bus->name, drv->name);
396 up_write(&drv->bus->subsys.rwsem);
397 module_remove_driver(drv);
398 kobject_unregister(&drv->kobj);
404 /* Helper for bus_rescan_devices's iter */
405 static int bus_rescan_devices_helper(struct device *dev, void *data)
409 if (!dev->driver && device_attach(dev))
417 * bus_rescan_devices - rescan devices on the bus for possible drivers
418 * @bus: the bus to scan.
420 * This function will look for devices on the bus with no driver
421 * attached and rescan it against existing drivers to see if it
422 * matches any. Calls device_attach(). Returns the number of devices
423 * that were sucessfully bound to a driver.
425 int bus_rescan_devices(struct bus_type * bus)
429 bus_for_each_dev(bus, NULL, &count, bus_rescan_devices_helper);
435 struct bus_type * get_bus(struct bus_type * bus)
437 return bus ? container_of(subsys_get(&bus->subsys), struct bus_type, subsys) : NULL;
440 void put_bus(struct bus_type * bus)
442 subsys_put(&bus->subsys);
447 * find_bus - locate bus by name.
448 * @name: name of bus.
450 * Call kset_find_obj() to iterate over list of buses to
451 * find a bus by name. Return bus if found.
453 * Note that kset_find_obj increments bus' reference count.
456 struct bus_type * find_bus(char * name)
458 struct kobject * k = kset_find_obj(&bus_subsys.kset, name);
459 return k ? to_bus(k) : NULL;
464 * bus_add_attrs - Add default attributes for this bus.
465 * @bus: Bus that has just been registered.
468 static int bus_add_attrs(struct bus_type * bus)
473 if (bus->bus_attrs) {
474 for (i = 0; attr_name(bus->bus_attrs[i]); i++) {
475 if ((error = bus_create_file(bus,&bus->bus_attrs[i])))
483 bus_remove_file(bus,&bus->bus_attrs[i]);
487 static void bus_remove_attrs(struct bus_type * bus)
491 if (bus->bus_attrs) {
492 for (i = 0; attr_name(bus->bus_attrs[i]); i++)
493 bus_remove_file(bus,&bus->bus_attrs[i]);
498 * bus_register - register a bus with the system.
501 * Once we have that, we registered the bus with the kobject
502 * infrastructure, then register the children subsystems it has:
503 * the devices and drivers that belong to the bus.
505 int bus_register(struct bus_type * bus)
509 retval = kobject_set_name(&bus->subsys.kset.kobj, "%s", bus->name);
513 subsys_set_kset(bus, bus_subsys);
514 retval = subsystem_register(&bus->subsys);
518 kobject_set_name(&bus->devices.kobj, "devices");
519 bus->devices.subsys = &bus->subsys;
520 retval = kset_register(&bus->devices);
522 goto bus_devices_fail;
524 kobject_set_name(&bus->drivers.kobj, "drivers");
525 bus->drivers.subsys = &bus->subsys;
526 bus->drivers.ktype = &ktype_driver;
527 retval = kset_register(&bus->drivers);
529 goto bus_drivers_fail;
531 klist_init(&bus->klist_devices);
532 klist_init(&bus->klist_drivers);
535 pr_debug("bus type '%s' registered\n", bus->name);
539 kset_unregister(&bus->devices);
541 subsystem_unregister(&bus->subsys);
548 * bus_unregister - remove a bus from the system
551 * Unregister the child subsystems and the bus itself.
552 * Finally, we call put_bus() to release the refcount
554 void bus_unregister(struct bus_type * bus)
556 pr_debug("bus %s: unregistering\n", bus->name);
557 bus_remove_attrs(bus);
558 kset_unregister(&bus->drivers);
559 kset_unregister(&bus->devices);
560 subsystem_unregister(&bus->subsys);
563 int __init buses_init(void)
565 return subsystem_register(&bus_subsys);
569 EXPORT_SYMBOL_GPL(bus_for_each_dev);
570 EXPORT_SYMBOL_GPL(bus_for_each_drv);
572 EXPORT_SYMBOL_GPL(bus_add_device);
573 EXPORT_SYMBOL_GPL(bus_remove_device);
574 EXPORT_SYMBOL_GPL(bus_register);
575 EXPORT_SYMBOL_GPL(bus_unregister);
576 EXPORT_SYMBOL_GPL(bus_rescan_devices);
577 EXPORT_SYMBOL_GPL(get_bus);
578 EXPORT_SYMBOL_GPL(put_bus);
579 EXPORT_SYMBOL_GPL(find_bus);
581 EXPORT_SYMBOL_GPL(bus_create_file);
582 EXPORT_SYMBOL_GPL(bus_remove_file);