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/device.h>
12 #include <linux/module.h>
13 #include <linux/errno.h>
14 #include <linux/init.h>
15 #include <linux/string.h>
17 #include "power/power.h"
19 #define to_bus_attr(_attr) container_of(_attr, struct bus_attribute, attr)
20 #define to_bus(obj) container_of(obj, struct bus_type, subsys.kobj)
23 * sysfs bindings for drivers
26 #define to_drv_attr(_attr) container_of(_attr, struct driver_attribute, attr)
27 #define to_driver(obj) container_of(obj, struct device_driver, kobj)
30 static int __must_check bus_rescan_devices_helper(struct device *dev,
33 static struct bus_type *bus_get(struct bus_type *bus)
35 return bus ? container_of(kset_get(&bus->subsys),
36 struct bus_type, subsys) : NULL;
39 static void bus_put(struct bus_type *bus)
41 kset_put(&bus->subsys);
45 drv_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
47 struct driver_attribute * drv_attr = to_drv_attr(attr);
48 struct device_driver * drv = to_driver(kobj);
52 ret = drv_attr->show(drv, buf);
57 drv_attr_store(struct kobject * kobj, struct attribute * attr,
58 const char * buf, size_t count)
60 struct driver_attribute * drv_attr = to_drv_attr(attr);
61 struct device_driver * drv = to_driver(kobj);
65 ret = drv_attr->store(drv, buf, count);
69 static struct sysfs_ops driver_sysfs_ops = {
70 .show = drv_attr_show,
71 .store = drv_attr_store,
75 static void driver_release(struct kobject * kobj)
78 * Yes this is an empty release function, it is this way because struct
79 * device is always a static object, not a dynamic one. Yes, this is
80 * not nice and bad, but remember, drivers are code, reference counted
81 * by the module count, not a device, which is really data. And yes,
82 * in the future I do want to have all drivers be created dynamically,
83 * and am working toward that goal, but it will take a bit longer...
85 * But do not let this example give _anyone_ the idea that they can
86 * create a release function without any code in it at all, to do that
87 * is almost always wrong. If you have any questions about this,
88 * please send an email to <greg@kroah.com>
92 static struct kobj_type driver_ktype = {
93 .sysfs_ops = &driver_sysfs_ops,
94 .release = driver_release,
99 * sysfs bindings for buses
104 bus_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
106 struct bus_attribute * bus_attr = to_bus_attr(attr);
107 struct bus_type * bus = to_bus(kobj);
111 ret = bus_attr->show(bus, buf);
116 bus_attr_store(struct kobject * kobj, struct attribute * attr,
117 const char * buf, size_t count)
119 struct bus_attribute * bus_attr = to_bus_attr(attr);
120 struct bus_type * bus = to_bus(kobj);
124 ret = bus_attr->store(bus, buf, count);
128 static struct sysfs_ops bus_sysfs_ops = {
129 .show = bus_attr_show,
130 .store = bus_attr_store,
133 int bus_create_file(struct bus_type * bus, struct bus_attribute * attr)
137 error = sysfs_create_file(&bus->subsys.kobj, &attr->attr);
144 void bus_remove_file(struct bus_type * bus, struct bus_attribute * attr)
147 sysfs_remove_file(&bus->subsys.kobj, &attr->attr);
152 static struct kobj_type bus_ktype = {
153 .sysfs_ops = &bus_sysfs_ops,
156 static int bus_uevent_filter(struct kset *kset, struct kobject *kobj)
158 struct kobj_type *ktype = get_ktype(kobj);
160 if (ktype == &bus_ktype)
165 static struct kset_uevent_ops bus_uevent_ops = {
166 .filter = bus_uevent_filter,
169 static decl_subsys(bus, &bus_ktype, &bus_uevent_ops);
172 #ifdef CONFIG_HOTPLUG
173 /* Manually detach a device from its associated driver. */
174 static int driver_helper(struct device *dev, void *data)
176 const char *name = data;
178 if (strcmp(name, dev->bus_id) == 0)
183 static ssize_t driver_unbind(struct device_driver *drv,
184 const char *buf, size_t count)
186 struct bus_type *bus = bus_get(drv->bus);
190 dev = bus_find_device(bus, NULL, (void *)buf, driver_helper);
191 if (dev && dev->driver == drv) {
192 if (dev->parent) /* Needed for USB */
193 down(&dev->parent->sem);
194 device_release_driver(dev);
196 up(&dev->parent->sem);
203 static DRIVER_ATTR(unbind, S_IWUSR, NULL, driver_unbind);
206 * Manually attach a device to a driver.
207 * Note: the driver must want to bind to the device,
208 * it is not possible to override the driver's id table.
210 static ssize_t driver_bind(struct device_driver *drv,
211 const char *buf, size_t count)
213 struct bus_type *bus = bus_get(drv->bus);
217 dev = bus_find_device(bus, NULL, (void *)buf, driver_helper);
218 if (dev && dev->driver == NULL) {
219 if (dev->parent) /* Needed for USB */
220 down(&dev->parent->sem);
222 err = driver_probe_device(drv, dev);
225 up(&dev->parent->sem);
227 if (err > 0) /* success */
229 else if (err == 0) /* driver didn't accept device */
236 static DRIVER_ATTR(bind, S_IWUSR, NULL, driver_bind);
238 static ssize_t show_drivers_autoprobe(struct bus_type *bus, char *buf)
240 return sprintf(buf, "%d\n", bus->drivers_autoprobe);
243 static ssize_t store_drivers_autoprobe(struct bus_type *bus,
244 const char *buf, size_t count)
247 bus->drivers_autoprobe = 0;
249 bus->drivers_autoprobe = 1;
253 static ssize_t store_drivers_probe(struct bus_type *bus,
254 const char *buf, size_t count)
258 dev = bus_find_device(bus, NULL, (void *)buf, driver_helper);
261 if (bus_rescan_devices_helper(dev, NULL) != 0)
267 static struct device * next_device(struct klist_iter * i)
269 struct klist_node * n = klist_next(i);
270 return n ? container_of(n, struct device, knode_bus) : NULL;
274 * bus_for_each_dev - device iterator.
276 * @start: device to start iterating from.
277 * @data: data for the callback.
278 * @fn: function to be called for each device.
280 * Iterate over @bus's list of devices, and call @fn for each,
281 * passing it @data. If @start is not NULL, we use that device to
282 * begin iterating from.
284 * We check the return of @fn each time. If it returns anything
285 * other than 0, we break out and return that value.
287 * NOTE: The device that returns a non-zero value is not retained
288 * in any way, nor is its refcount incremented. If the caller needs
289 * to retain this data, it should do, and increment the reference
290 * count in the supplied callback.
293 int bus_for_each_dev(struct bus_type * bus, struct device * start,
294 void * data, int (*fn)(struct device *, void *))
303 klist_iter_init_node(&bus->klist_devices, &i,
304 (start ? &start->knode_bus : NULL));
305 while ((dev = next_device(&i)) && !error)
306 error = fn(dev, data);
312 * bus_find_device - device iterator for locating a particular device.
314 * @start: Device to begin with
315 * @data: Data to pass to match function
316 * @match: Callback function to check device
318 * This is similar to the bus_for_each_dev() function above, but it
319 * returns a reference to a device that is 'found' for later use, as
320 * determined by the @match callback.
322 * The callback should return 0 if the device doesn't match and non-zero
323 * if it does. If the callback returns non-zero, this function will
324 * return to the caller and not iterate over any more devices.
326 struct device * bus_find_device(struct bus_type *bus,
327 struct device *start, void *data,
328 int (*match)(struct device *, void *))
336 klist_iter_init_node(&bus->klist_devices, &i,
337 (start ? &start->knode_bus : NULL));
338 while ((dev = next_device(&i)))
339 if (match(dev, data) && get_device(dev))
346 static struct device_driver * next_driver(struct klist_iter * i)
348 struct klist_node * n = klist_next(i);
349 return n ? container_of(n, struct device_driver, knode_bus) : NULL;
353 * bus_for_each_drv - driver iterator
354 * @bus: bus we're dealing with.
355 * @start: driver to start iterating on.
356 * @data: data to pass to the callback.
357 * @fn: function to call for each driver.
359 * This is nearly identical to the device iterator above.
360 * We iterate over each driver that belongs to @bus, and call
361 * @fn for each. If @fn returns anything but 0, we break out
362 * and return it. If @start is not NULL, we use it as the head
365 * NOTE: we don't return the driver that returns a non-zero
366 * value, nor do we leave the reference count incremented for that
367 * driver. If the caller needs to know that info, it must set it
368 * in the callback. It must also be sure to increment the refcount
369 * so it doesn't disappear before returning to the caller.
372 int bus_for_each_drv(struct bus_type * bus, struct device_driver * start,
373 void * data, int (*fn)(struct device_driver *, void *))
376 struct device_driver * drv;
382 klist_iter_init_node(&bus->klist_drivers, &i,
383 start ? &start->knode_bus : NULL);
384 while ((drv = next_driver(&i)) && !error)
385 error = fn(drv, data);
390 static int device_add_attrs(struct bus_type *bus, struct device *dev)
398 for (i = 0; attr_name(bus->dev_attrs[i]); i++) {
399 error = device_create_file(dev,&bus->dev_attrs[i]);
402 device_remove_file(dev, &bus->dev_attrs[i]);
409 static void device_remove_attrs(struct bus_type * bus, struct device * dev)
413 if (bus->dev_attrs) {
414 for (i = 0; attr_name(bus->dev_attrs[i]); i++)
415 device_remove_file(dev,&bus->dev_attrs[i]);
419 #ifdef CONFIG_SYSFS_DEPRECATED
420 static int make_deprecated_bus_links(struct device *dev)
422 return sysfs_create_link(&dev->kobj,
423 &dev->bus->subsys.kobj, "bus");
426 static void remove_deprecated_bus_links(struct device *dev)
428 sysfs_remove_link(&dev->kobj, "bus");
431 static inline int make_deprecated_bus_links(struct device *dev) { return 0; }
432 static inline void remove_deprecated_bus_links(struct device *dev) { }
436 * bus_add_device - add device to bus
437 * @dev: device being added
439 * - Add the device to its bus's list of devices.
440 * - Create link to device's bus.
442 int bus_add_device(struct device * dev)
444 struct bus_type * bus = bus_get(dev->bus);
448 pr_debug("bus %s: add device %s\n", bus->name, dev->bus_id);
449 error = device_add_attrs(bus, dev);
452 error = sysfs_create_link(&bus->devices.kobj,
453 &dev->kobj, dev->bus_id);
456 error = sysfs_create_link(&dev->kobj,
457 &dev->bus->subsys.kobj, "subsystem");
460 error = make_deprecated_bus_links(dev);
467 sysfs_remove_link(&dev->kobj, "subsystem");
469 sysfs_remove_link(&bus->devices.kobj, dev->bus_id);
471 device_remove_attrs(bus, dev);
478 * bus_attach_device - add device to bus
479 * @dev: device tried to attach to a driver
481 * - Add device to bus's list of devices.
482 * - Try to attach to driver.
484 void bus_attach_device(struct device * dev)
486 struct bus_type *bus = dev->bus;
490 dev->is_registered = 1;
491 if (bus->drivers_autoprobe)
492 ret = device_attach(dev);
495 klist_add_tail(&dev->knode_bus, &bus->klist_devices);
497 dev->is_registered = 0;
502 * bus_remove_device - remove device from bus
503 * @dev: device to be removed
505 * - Remove symlink from bus's directory.
506 * - Delete device from bus's list.
507 * - Detach from its driver.
508 * - Drop reference taken in bus_add_device().
510 void bus_remove_device(struct device * dev)
513 sysfs_remove_link(&dev->kobj, "subsystem");
514 remove_deprecated_bus_links(dev);
515 sysfs_remove_link(&dev->bus->devices.kobj, dev->bus_id);
516 device_remove_attrs(dev->bus, dev);
517 if (dev->is_registered) {
518 dev->is_registered = 0;
519 klist_del(&dev->knode_bus);
521 pr_debug("bus %s: remove device %s\n", dev->bus->name, dev->bus_id);
522 device_release_driver(dev);
527 static int driver_add_attrs(struct bus_type * bus, struct device_driver * drv)
532 if (bus->drv_attrs) {
533 for (i = 0; attr_name(bus->drv_attrs[i]); i++) {
534 error = driver_create_file(drv, &bus->drv_attrs[i]);
543 driver_remove_file(drv, &bus->drv_attrs[i]);
548 static void driver_remove_attrs(struct bus_type * bus, struct device_driver * drv)
552 if (bus->drv_attrs) {
553 for (i = 0; attr_name(bus->drv_attrs[i]); i++)
554 driver_remove_file(drv, &bus->drv_attrs[i]);
558 #ifdef CONFIG_HOTPLUG
560 * Thanks to drivers making their tables __devinit, we can't allow manual
561 * bind and unbind from userspace unless CONFIG_HOTPLUG is enabled.
563 static int __must_check add_bind_files(struct device_driver *drv)
567 ret = driver_create_file(drv, &driver_attr_unbind);
569 ret = driver_create_file(drv, &driver_attr_bind);
571 driver_remove_file(drv, &driver_attr_unbind);
576 static void remove_bind_files(struct device_driver *drv)
578 driver_remove_file(drv, &driver_attr_bind);
579 driver_remove_file(drv, &driver_attr_unbind);
582 static BUS_ATTR(drivers_probe, S_IWUSR, NULL, store_drivers_probe);
583 static BUS_ATTR(drivers_autoprobe, S_IWUSR | S_IRUGO,
584 show_drivers_autoprobe, store_drivers_autoprobe);
586 static int add_probe_files(struct bus_type *bus)
590 retval = bus_create_file(bus, &bus_attr_drivers_probe);
594 retval = bus_create_file(bus, &bus_attr_drivers_autoprobe);
596 bus_remove_file(bus, &bus_attr_drivers_probe);
601 static void remove_probe_files(struct bus_type *bus)
603 bus_remove_file(bus, &bus_attr_drivers_autoprobe);
604 bus_remove_file(bus, &bus_attr_drivers_probe);
607 static inline int add_bind_files(struct device_driver *drv) { return 0; }
608 static inline void remove_bind_files(struct device_driver *drv) {}
609 static inline int add_probe_files(struct bus_type *bus) { return 0; }
610 static inline void remove_probe_files(struct bus_type *bus) {}
613 static ssize_t driver_uevent_store(struct device_driver *drv,
614 const char *buf, size_t count)
616 enum kobject_action action;
618 if (kobject_action_type(buf, count, &action) == 0)
619 kobject_uevent(&drv->kobj, action);
622 static DRIVER_ATTR(uevent, S_IWUSR, NULL, driver_uevent_store);
625 * bus_add_driver - Add a driver to the bus.
629 int bus_add_driver(struct device_driver *drv)
631 struct bus_type * bus = bus_get(drv->bus);
637 pr_debug("bus %s: add driver %s\n", bus->name, drv->name);
638 error = kobject_set_name(&drv->kobj, "%s", drv->name);
641 drv->kobj.kset = &bus->drivers;
642 error = kobject_register(&drv->kobj);
646 if (drv->bus->drivers_autoprobe) {
647 error = driver_attach(drv);
651 klist_add_tail(&drv->knode_bus, &bus->klist_drivers);
652 module_add_driver(drv->owner, drv);
654 error = driver_create_file(drv, &driver_attr_uevent);
656 printk(KERN_ERR "%s: uevent attr (%s) failed\n",
657 __FUNCTION__, drv->name);
659 error = driver_add_attrs(bus, drv);
661 /* How the hell do we get out of this pickle? Give up */
662 printk(KERN_ERR "%s: driver_add_attrs(%s) failed\n",
663 __FUNCTION__, drv->name);
665 error = add_bind_files(drv);
668 printk(KERN_ERR "%s: add_bind_files(%s) failed\n",
669 __FUNCTION__, drv->name);
674 kobject_unregister(&drv->kobj);
681 * bus_remove_driver - delete driver from bus's knowledge.
684 * Detach the driver from the devices it controls, and remove
685 * it from its bus's list of drivers. Finally, we drop the reference
686 * to the bus we took in bus_add_driver().
689 void bus_remove_driver(struct device_driver * drv)
694 remove_bind_files(drv);
695 driver_remove_attrs(drv->bus, drv);
696 driver_remove_file(drv, &driver_attr_uevent);
697 klist_remove(&drv->knode_bus);
698 pr_debug("bus %s: remove driver %s\n", drv->bus->name, drv->name);
700 module_remove_driver(drv);
701 kobject_unregister(&drv->kobj);
706 /* Helper for bus_rescan_devices's iter */
707 static int __must_check bus_rescan_devices_helper(struct device *dev,
713 if (dev->parent) /* Needed for USB */
714 down(&dev->parent->sem);
715 ret = device_attach(dev);
717 up(&dev->parent->sem);
719 return ret < 0 ? ret : 0;
723 * bus_rescan_devices - rescan devices on the bus for possible drivers
724 * @bus: the bus to scan.
726 * This function will look for devices on the bus with no driver
727 * attached and rescan it against existing drivers to see if it matches
728 * any by calling device_attach() for the unbound devices.
730 int bus_rescan_devices(struct bus_type * bus)
732 return bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper);
736 * device_reprobe - remove driver for a device and probe for a new driver
737 * @dev: the device to reprobe
739 * This function detaches the attached driver (if any) for the given
740 * device and restarts the driver probing process. It is intended
741 * to use if probing criteria changed during a devices lifetime and
742 * driver attachment should change accordingly.
744 int device_reprobe(struct device *dev)
747 if (dev->parent) /* Needed for USB */
748 down(&dev->parent->sem);
749 device_release_driver(dev);
751 up(&dev->parent->sem);
753 return bus_rescan_devices_helper(dev, NULL);
755 EXPORT_SYMBOL_GPL(device_reprobe);
758 * find_bus - locate bus by name.
759 * @name: name of bus.
761 * Call kset_find_obj() to iterate over list of buses to
762 * find a bus by name. Return bus if found.
764 * Note that kset_find_obj increments bus' reference count.
767 struct bus_type * find_bus(char * name)
769 struct kobject * k = kset_find_obj(&bus_subsys.kset, name);
770 return k ? to_bus(k) : NULL;
776 * bus_add_attrs - Add default attributes for this bus.
777 * @bus: Bus that has just been registered.
780 static int bus_add_attrs(struct bus_type * bus)
785 if (bus->bus_attrs) {
786 for (i = 0; attr_name(bus->bus_attrs[i]); i++) {
787 error = bus_create_file(bus,&bus->bus_attrs[i]);
796 bus_remove_file(bus,&bus->bus_attrs[i]);
800 static void bus_remove_attrs(struct bus_type * bus)
804 if (bus->bus_attrs) {
805 for (i = 0; attr_name(bus->bus_attrs[i]); i++)
806 bus_remove_file(bus,&bus->bus_attrs[i]);
810 static void klist_devices_get(struct klist_node *n)
812 struct device *dev = container_of(n, struct device, knode_bus);
817 static void klist_devices_put(struct klist_node *n)
819 struct device *dev = container_of(n, struct device, knode_bus);
824 static ssize_t bus_uevent_store(struct bus_type *bus,
825 const char *buf, size_t count)
827 enum kobject_action action;
829 if (kobject_action_type(buf, count, &action) == 0)
830 kobject_uevent(&bus->subsys.kobj, action);
833 static BUS_ATTR(uevent, S_IWUSR, NULL, bus_uevent_store);
836 * bus_register - register a bus with the system.
839 * Once we have that, we registered the bus with the kobject
840 * infrastructure, then register the children subsystems it has:
841 * the devices and drivers that belong to the bus.
843 int bus_register(struct bus_type * bus)
847 BLOCKING_INIT_NOTIFIER_HEAD(&bus->bus_notifier);
849 retval = kobject_set_name(&bus->subsys.kobj, "%s", bus->name);
853 bus->subsys.kobj.kset = &bus_subsys;
855 retval = subsystem_register(&bus->subsys);
859 retval = bus_create_file(bus, &bus_attr_uevent);
861 goto bus_uevent_fail;
863 kobject_set_name(&bus->devices.kobj, "devices");
864 bus->devices.kobj.parent = &bus->subsys.kobj;
865 retval = kset_register(&bus->devices);
867 goto bus_devices_fail;
869 kobject_set_name(&bus->drivers.kobj, "drivers");
870 bus->drivers.kobj.parent = &bus->subsys.kobj;
871 bus->drivers.ktype = &driver_ktype;
872 retval = kset_register(&bus->drivers);
874 goto bus_drivers_fail;
876 klist_init(&bus->klist_devices, klist_devices_get, klist_devices_put);
877 klist_init(&bus->klist_drivers, NULL, NULL);
879 bus->drivers_autoprobe = 1;
880 retval = add_probe_files(bus);
882 goto bus_probe_files_fail;
884 retval = bus_add_attrs(bus);
888 pr_debug("bus type '%s' registered\n", bus->name);
892 remove_probe_files(bus);
893 bus_probe_files_fail:
894 kset_unregister(&bus->drivers);
896 kset_unregister(&bus->devices);
898 bus_remove_file(bus, &bus_attr_uevent);
900 subsystem_unregister(&bus->subsys);
906 * bus_unregister - remove a bus from the system
909 * Unregister the child subsystems and the bus itself.
910 * Finally, we call bus_put() to release the refcount
912 void bus_unregister(struct bus_type * bus)
914 pr_debug("bus %s: unregistering\n", bus->name);
915 bus_remove_attrs(bus);
916 remove_probe_files(bus);
917 kset_unregister(&bus->drivers);
918 kset_unregister(&bus->devices);
919 bus_remove_file(bus, &bus_attr_uevent);
920 subsystem_unregister(&bus->subsys);
923 int bus_register_notifier(struct bus_type *bus, struct notifier_block *nb)
925 return blocking_notifier_chain_register(&bus->bus_notifier, nb);
927 EXPORT_SYMBOL_GPL(bus_register_notifier);
929 int bus_unregister_notifier(struct bus_type *bus, struct notifier_block *nb)
931 return blocking_notifier_chain_unregister(&bus->bus_notifier, nb);
933 EXPORT_SYMBOL_GPL(bus_unregister_notifier);
935 int __init buses_init(void)
937 return subsystem_register(&bus_subsys);
941 EXPORT_SYMBOL_GPL(bus_for_each_dev);
942 EXPORT_SYMBOL_GPL(bus_find_device);
943 EXPORT_SYMBOL_GPL(bus_for_each_drv);
945 EXPORT_SYMBOL_GPL(bus_register);
946 EXPORT_SYMBOL_GPL(bus_unregister);
947 EXPORT_SYMBOL_GPL(bus_rescan_devices);
949 EXPORT_SYMBOL_GPL(bus_create_file);
950 EXPORT_SYMBOL_GPL(bus_remove_file);