2 * bus.c - bus driver management
4 * Copyright (c) 2002-3 Patrick Mochel
5 * Copyright (c) 2002-3 Open Source Development Labs
6 * Copyright (c) 2007 Greg Kroah-Hartman <gregkh@suse.de>
7 * Copyright (c) 2007 Novell Inc.
9 * This file is released under the GPLv2
13 #include <linux/async.h>
14 #include <linux/device.h>
15 #include <linux/module.h>
16 #include <linux/errno.h>
17 #include <linux/slab.h>
18 #include <linux/init.h>
19 #include <linux/string.h>
20 #include <linux/mutex.h>
21 #include <linux/sysfs.h>
23 #include "power/power.h"
25 /* /sys/devices/system */
26 static struct kset *system_kset;
28 #define to_bus_attr(_attr) container_of(_attr, struct bus_attribute, attr)
31 * sysfs bindings for drivers
34 #define to_drv_attr(_attr) container_of(_attr, struct driver_attribute, attr)
36 #define DRIVER_ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) \
37 struct driver_attribute driver_attr_##_name = \
38 __ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store)
40 static int __must_check bus_rescan_devices_helper(struct device *dev,
43 static struct bus_type *bus_get(struct bus_type *bus)
46 kset_get(&bus->p->subsys);
52 static void bus_put(struct bus_type *bus)
55 kset_put(&bus->p->subsys);
58 static ssize_t drv_attr_show(struct kobject *kobj, struct attribute *attr,
61 struct driver_attribute *drv_attr = to_drv_attr(attr);
62 struct driver_private *drv_priv = to_driver(kobj);
66 ret = drv_attr->show(drv_priv->driver, buf);
70 static ssize_t drv_attr_store(struct kobject *kobj, struct attribute *attr,
71 const char *buf, size_t count)
73 struct driver_attribute *drv_attr = to_drv_attr(attr);
74 struct driver_private *drv_priv = to_driver(kobj);
78 ret = drv_attr->store(drv_priv->driver, buf, count);
82 static const struct sysfs_ops driver_sysfs_ops = {
83 .show = drv_attr_show,
84 .store = drv_attr_store,
87 static void driver_release(struct kobject *kobj)
89 struct driver_private *drv_priv = to_driver(kobj);
91 pr_debug("driver: '%s': %s\n", kobject_name(kobj), __func__);
95 static struct kobj_type driver_ktype = {
96 .sysfs_ops = &driver_sysfs_ops,
97 .release = driver_release,
101 * sysfs bindings for buses
103 static ssize_t bus_attr_show(struct kobject *kobj, struct attribute *attr,
106 struct bus_attribute *bus_attr = to_bus_attr(attr);
107 struct subsys_private *subsys_priv = to_subsys_private(kobj);
111 ret = bus_attr->show(subsys_priv->bus, buf);
115 static ssize_t bus_attr_store(struct kobject *kobj, struct attribute *attr,
116 const char *buf, size_t count)
118 struct bus_attribute *bus_attr = to_bus_attr(attr);
119 struct subsys_private *subsys_priv = to_subsys_private(kobj);
123 ret = bus_attr->store(subsys_priv->bus, buf, count);
127 static const struct sysfs_ops bus_sysfs_ops = {
128 .show = bus_attr_show,
129 .store = bus_attr_store,
132 int bus_create_file(struct bus_type *bus, struct bus_attribute *attr)
136 error = sysfs_create_file(&bus->p->subsys.kobj, &attr->attr);
142 EXPORT_SYMBOL_GPL(bus_create_file);
144 void bus_remove_file(struct bus_type *bus, struct bus_attribute *attr)
147 sysfs_remove_file(&bus->p->subsys.kobj, &attr->attr);
151 EXPORT_SYMBOL_GPL(bus_remove_file);
153 static void bus_release(struct kobject *kobj)
155 struct subsys_private *priv =
156 container_of(kobj, typeof(*priv), subsys.kobj);
157 struct bus_type *bus = priv->bus;
163 static struct kobj_type bus_ktype = {
164 .sysfs_ops = &bus_sysfs_ops,
165 .release = bus_release,
168 static int bus_uevent_filter(struct kset *kset, struct kobject *kobj)
170 struct kobj_type *ktype = get_ktype(kobj);
172 if (ktype == &bus_ktype)
177 static const struct kset_uevent_ops bus_uevent_ops = {
178 .filter = bus_uevent_filter,
181 static struct kset *bus_kset;
183 /* Manually detach a device from its associated driver. */
184 static ssize_t unbind_store(struct device_driver *drv, const char *buf,
187 struct bus_type *bus = bus_get(drv->bus);
191 dev = bus_find_device_by_name(bus, NULL, buf);
192 if (dev && dev->driver == drv) {
193 if (dev->parent) /* Needed for USB */
194 device_lock(dev->parent);
195 device_release_driver(dev);
197 device_unlock(dev->parent);
204 static DRIVER_ATTR_IGNORE_LOCKDEP(unbind, S_IWUSR, NULL, unbind_store);
207 * Manually attach a device to a driver.
208 * Note: the driver must want to bind to the device,
209 * it is not possible to override the driver's id table.
211 static ssize_t bind_store(struct device_driver *drv, const char *buf,
214 struct bus_type *bus = bus_get(drv->bus);
218 dev = bus_find_device_by_name(bus, NULL, buf);
219 if (dev && dev->driver == NULL && driver_match_device(drv, dev)) {
220 if (dev->parent) /* Needed for USB */
221 device_lock(dev->parent);
223 err = driver_probe_device(drv, dev);
226 device_unlock(dev->parent);
231 } else if (err == 0) {
232 /* driver didn't accept device */
240 static DRIVER_ATTR_IGNORE_LOCKDEP(bind, S_IWUSR, NULL, bind_store);
242 static ssize_t show_drivers_autoprobe(struct bus_type *bus, char *buf)
244 return sprintf(buf, "%d\n", bus->p->drivers_autoprobe);
247 static ssize_t store_drivers_autoprobe(struct bus_type *bus,
248 const char *buf, size_t count)
251 bus->p->drivers_autoprobe = 0;
253 bus->p->drivers_autoprobe = 1;
257 static ssize_t store_drivers_probe(struct bus_type *bus,
258 const char *buf, size_t count)
263 dev = bus_find_device_by_name(bus, NULL, buf);
266 if (bus_rescan_devices_helper(dev, NULL) == 0)
272 static struct device *next_device(struct klist_iter *i)
274 struct klist_node *n = klist_next(i);
275 struct device *dev = NULL;
276 struct device_private *dev_prv;
279 dev_prv = to_device_private_bus(n);
280 dev = dev_prv->device;
286 * bus_for_each_dev - device iterator.
288 * @start: device to start iterating from.
289 * @data: data for the callback.
290 * @fn: function to be called for each device.
292 * Iterate over @bus's list of devices, and call @fn for each,
293 * passing it @data. If @start is not NULL, we use that device to
294 * begin iterating from.
296 * We check the return of @fn each time. If it returns anything
297 * other than 0, we break out and return that value.
299 * NOTE: The device that returns a non-zero value is not retained
300 * in any way, nor is its refcount incremented. If the caller needs
301 * to retain this data, it should do so, and increment the reference
302 * count in the supplied callback.
304 int bus_for_each_dev(struct bus_type *bus, struct device *start,
305 void *data, int (*fn)(struct device *, void *))
314 klist_iter_init_node(&bus->p->klist_devices, &i,
315 (start ? &start->p->knode_bus : NULL));
316 while ((dev = next_device(&i)) && !error)
317 error = fn(dev, data);
321 EXPORT_SYMBOL_GPL(bus_for_each_dev);
324 * bus_find_device - device iterator for locating a particular device.
326 * @start: Device to begin with
327 * @data: Data to pass to match function
328 * @match: Callback function to check device
330 * This is similar to the bus_for_each_dev() function above, but it
331 * returns a reference to a device that is 'found' for later use, as
332 * determined by the @match callback.
334 * The callback should return 0 if the device doesn't match and non-zero
335 * if it does. If the callback returns non-zero, this function will
336 * return to the caller and not iterate over any more devices.
338 struct device *bus_find_device(struct bus_type *bus,
339 struct device *start, void *data,
340 int (*match)(struct device *dev, void *data))
348 klist_iter_init_node(&bus->p->klist_devices, &i,
349 (start ? &start->p->knode_bus : NULL));
350 while ((dev = next_device(&i)))
351 if (match(dev, data) && get_device(dev))
356 EXPORT_SYMBOL_GPL(bus_find_device);
358 static int match_name(struct device *dev, void *data)
360 const char *name = data;
362 return sysfs_streq(name, dev_name(dev));
366 * bus_find_device_by_name - device iterator for locating a particular device of a specific name
368 * @start: Device to begin with
369 * @name: name of the device to match
371 * This is similar to the bus_find_device() function above, but it handles
372 * searching by a name automatically, no need to write another strcmp matching
375 struct device *bus_find_device_by_name(struct bus_type *bus,
376 struct device *start, const char *name)
378 return bus_find_device(bus, start, (void *)name, match_name);
380 EXPORT_SYMBOL_GPL(bus_find_device_by_name);
383 * subsys_find_device_by_id - find a device with a specific enumeration number
385 * @id: index 'id' in struct device
386 * @hint: device to check first
388 * Check the hint's next object and if it is a match return it directly,
389 * otherwise, fall back to a full list search. Either way a reference for
390 * the returned object is taken.
392 struct device *subsys_find_device_by_id(struct bus_type *subsys, unsigned int id,
402 klist_iter_init_node(&subsys->p->klist_devices, &i, &hint->p->knode_bus);
403 dev = next_device(&i);
404 if (dev && dev->id == id && get_device(dev)) {
411 klist_iter_init_node(&subsys->p->klist_devices, &i, NULL);
412 while ((dev = next_device(&i))) {
413 if (dev->id == id && get_device(dev)) {
421 EXPORT_SYMBOL_GPL(subsys_find_device_by_id);
423 static struct device_driver *next_driver(struct klist_iter *i)
425 struct klist_node *n = klist_next(i);
426 struct driver_private *drv_priv;
429 drv_priv = container_of(n, struct driver_private, knode_bus);
430 return drv_priv->driver;
436 * bus_for_each_drv - driver iterator
437 * @bus: bus we're dealing with.
438 * @start: driver to start iterating on.
439 * @data: data to pass to the callback.
440 * @fn: function to call for each driver.
442 * This is nearly identical to the device iterator above.
443 * We iterate over each driver that belongs to @bus, and call
444 * @fn for each. If @fn returns anything but 0, we break out
445 * and return it. If @start is not NULL, we use it as the head
448 * NOTE: we don't return the driver that returns a non-zero
449 * value, nor do we leave the reference count incremented for that
450 * driver. If the caller needs to know that info, it must set it
451 * in the callback. It must also be sure to increment the refcount
452 * so it doesn't disappear before returning to the caller.
454 int bus_for_each_drv(struct bus_type *bus, struct device_driver *start,
455 void *data, int (*fn)(struct device_driver *, void *))
458 struct device_driver *drv;
464 klist_iter_init_node(&bus->p->klist_drivers, &i,
465 start ? &start->p->knode_bus : NULL);
466 while ((drv = next_driver(&i)) && !error)
467 error = fn(drv, data);
471 EXPORT_SYMBOL_GPL(bus_for_each_drv);
473 static int device_add_attrs(struct bus_type *bus, struct device *dev)
481 for (i = 0; bus->dev_attrs[i].attr.name; i++) {
482 error = device_create_file(dev, &bus->dev_attrs[i]);
485 device_remove_file(dev, &bus->dev_attrs[i]);
492 static void device_remove_attrs(struct bus_type *bus, struct device *dev)
496 if (bus->dev_attrs) {
497 for (i = 0; bus->dev_attrs[i].attr.name; i++)
498 device_remove_file(dev, &bus->dev_attrs[i]);
503 * bus_add_device - add device to bus
504 * @dev: device being added
506 * - Add device's bus attributes.
507 * - Create links to device's bus.
508 * - Add the device to its bus's list of devices.
510 int bus_add_device(struct device *dev)
512 struct bus_type *bus = bus_get(dev->bus);
516 pr_debug("bus: '%s': add device %s\n", bus->name, dev_name(dev));
517 error = device_add_attrs(bus, dev);
520 error = device_add_groups(dev, bus->dev_groups);
523 error = sysfs_create_link(&bus->p->devices_kset->kobj,
524 &dev->kobj, dev_name(dev));
527 error = sysfs_create_link(&dev->kobj,
528 &dev->bus->p->subsys.kobj, "subsystem");
531 klist_add_tail(&dev->p->knode_bus, &bus->p->klist_devices);
536 sysfs_remove_link(&bus->p->devices_kset->kobj, dev_name(dev));
538 device_remove_groups(dev, bus->dev_groups);
540 device_remove_attrs(bus, dev);
547 * bus_probe_device - probe drivers for a new device
548 * @dev: device to probe
550 * - Automatically probe for a driver if the bus allows it.
552 void bus_probe_device(struct device *dev)
554 struct bus_type *bus = dev->bus;
555 struct subsys_interface *sif;
560 if (bus->p->drivers_autoprobe)
561 device_initial_probe(dev);
563 mutex_lock(&bus->p->mutex);
564 list_for_each_entry(sif, &bus->p->interfaces, node)
566 sif->add_dev(dev, sif);
567 mutex_unlock(&bus->p->mutex);
571 * bus_remove_device - remove device from bus
572 * @dev: device to be removed
574 * - Remove device from all interfaces.
575 * - Remove symlink from bus' directory.
576 * - Delete device from bus's list.
577 * - Detach from its driver.
578 * - Drop reference taken in bus_add_device().
580 void bus_remove_device(struct device *dev)
582 struct bus_type *bus = dev->bus;
583 struct subsys_interface *sif;
588 mutex_lock(&bus->p->mutex);
589 list_for_each_entry(sif, &bus->p->interfaces, node)
591 sif->remove_dev(dev, sif);
592 mutex_unlock(&bus->p->mutex);
594 sysfs_remove_link(&dev->kobj, "subsystem");
595 sysfs_remove_link(&dev->bus->p->devices_kset->kobj,
597 device_remove_attrs(dev->bus, dev);
598 device_remove_groups(dev, dev->bus->dev_groups);
599 if (klist_node_attached(&dev->p->knode_bus))
600 klist_del(&dev->p->knode_bus);
602 pr_debug("bus: '%s': remove device %s\n",
603 dev->bus->name, dev_name(dev));
604 device_release_driver(dev);
608 static int __must_check add_bind_files(struct device_driver *drv)
612 ret = driver_create_file(drv, &driver_attr_unbind);
614 ret = driver_create_file(drv, &driver_attr_bind);
616 driver_remove_file(drv, &driver_attr_unbind);
621 static void remove_bind_files(struct device_driver *drv)
623 driver_remove_file(drv, &driver_attr_bind);
624 driver_remove_file(drv, &driver_attr_unbind);
627 static BUS_ATTR(drivers_probe, S_IWUSR, NULL, store_drivers_probe);
628 static BUS_ATTR(drivers_autoprobe, S_IWUSR | S_IRUGO,
629 show_drivers_autoprobe, store_drivers_autoprobe);
631 static int add_probe_files(struct bus_type *bus)
635 retval = bus_create_file(bus, &bus_attr_drivers_probe);
639 retval = bus_create_file(bus, &bus_attr_drivers_autoprobe);
641 bus_remove_file(bus, &bus_attr_drivers_probe);
646 static void remove_probe_files(struct bus_type *bus)
648 bus_remove_file(bus, &bus_attr_drivers_autoprobe);
649 bus_remove_file(bus, &bus_attr_drivers_probe);
652 static ssize_t uevent_store(struct device_driver *drv, const char *buf,
655 enum kobject_action action;
657 if (kobject_action_type(buf, count, &action) == 0)
658 kobject_uevent(&drv->p->kobj, action);
661 static DRIVER_ATTR_WO(uevent);
663 static void driver_attach_async(void *_drv, async_cookie_t cookie)
665 struct device_driver *drv = _drv;
668 ret = driver_attach(drv);
670 pr_debug("bus: '%s': driver %s async attach completed: %d\n",
671 drv->bus->name, drv->name, ret);
675 * bus_add_driver - Add a driver to the bus.
678 int bus_add_driver(struct device_driver *drv)
680 struct bus_type *bus;
681 struct driver_private *priv;
684 bus = bus_get(drv->bus);
688 pr_debug("bus: '%s': add driver %s\n", bus->name, drv->name);
690 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
695 klist_init(&priv->klist_devices, NULL, NULL);
698 priv->kobj.kset = bus->p->drivers_kset;
699 error = kobject_init_and_add(&priv->kobj, &driver_ktype, NULL,
704 klist_add_tail(&priv->knode_bus, &bus->p->klist_drivers);
705 if (drv->bus->p->drivers_autoprobe) {
706 if (driver_allows_async_probing(drv)) {
707 pr_debug("bus: '%s': probing driver %s asynchronously\n",
708 drv->bus->name, drv->name);
709 async_schedule(driver_attach_async, drv);
711 error = driver_attach(drv);
716 module_add_driver(drv->owner, drv);
718 error = driver_create_file(drv, &driver_attr_uevent);
720 printk(KERN_ERR "%s: uevent attr (%s) failed\n",
721 __func__, drv->name);
723 error = driver_add_groups(drv, bus->drv_groups);
725 /* How the hell do we get out of this pickle? Give up */
726 printk(KERN_ERR "%s: driver_create_groups(%s) failed\n",
727 __func__, drv->name);
730 if (!drv->suppress_bind_attrs) {
731 error = add_bind_files(drv);
734 printk(KERN_ERR "%s: add_bind_files(%s) failed\n",
735 __func__, drv->name);
742 kobject_put(&priv->kobj);
743 /* drv->p is freed in driver_release() */
751 * bus_remove_driver - delete driver from bus's knowledge.
754 * Detach the driver from the devices it controls, and remove
755 * it from its bus's list of drivers. Finally, we drop the reference
756 * to the bus we took in bus_add_driver().
758 void bus_remove_driver(struct device_driver *drv)
763 if (!drv->suppress_bind_attrs)
764 remove_bind_files(drv);
765 driver_remove_groups(drv, drv->bus->drv_groups);
766 driver_remove_file(drv, &driver_attr_uevent);
767 klist_remove(&drv->p->knode_bus);
768 pr_debug("bus: '%s': remove driver %s\n", drv->bus->name, drv->name);
770 module_remove_driver(drv);
771 kobject_put(&drv->p->kobj);
775 /* Helper for bus_rescan_devices's iter */
776 static int __must_check bus_rescan_devices_helper(struct device *dev,
782 if (dev->parent) /* Needed for USB */
783 device_lock(dev->parent);
784 ret = device_attach(dev);
786 device_unlock(dev->parent);
788 return ret < 0 ? ret : 0;
792 * bus_rescan_devices - rescan devices on the bus for possible drivers
793 * @bus: the bus to scan.
795 * This function will look for devices on the bus with no driver
796 * attached and rescan it against existing drivers to see if it matches
797 * any by calling device_attach() for the unbound devices.
799 int bus_rescan_devices(struct bus_type *bus)
801 return bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper);
803 EXPORT_SYMBOL_GPL(bus_rescan_devices);
806 * device_reprobe - remove driver for a device and probe for a new driver
807 * @dev: the device to reprobe
809 * This function detaches the attached driver (if any) for the given
810 * device and restarts the driver probing process. It is intended
811 * to use if probing criteria changed during a devices lifetime and
812 * driver attachment should change accordingly.
814 int device_reprobe(struct device *dev)
817 if (dev->parent) /* Needed for USB */
818 device_lock(dev->parent);
819 device_release_driver(dev);
821 device_unlock(dev->parent);
823 return bus_rescan_devices_helper(dev, NULL);
825 EXPORT_SYMBOL_GPL(device_reprobe);
828 * find_bus - locate bus by name.
829 * @name: name of bus.
831 * Call kset_find_obj() to iterate over list of buses to
832 * find a bus by name. Return bus if found.
834 * Note that kset_find_obj increments bus' reference count.
837 struct bus_type *find_bus(char *name)
839 struct kobject *k = kset_find_obj(bus_kset, name);
840 return k ? to_bus(k) : NULL;
844 static int bus_add_groups(struct bus_type *bus,
845 const struct attribute_group **groups)
847 return sysfs_create_groups(&bus->p->subsys.kobj, groups);
850 static void bus_remove_groups(struct bus_type *bus,
851 const struct attribute_group **groups)
853 sysfs_remove_groups(&bus->p->subsys.kobj, groups);
856 static void klist_devices_get(struct klist_node *n)
858 struct device_private *dev_prv = to_device_private_bus(n);
859 struct device *dev = dev_prv->device;
864 static void klist_devices_put(struct klist_node *n)
866 struct device_private *dev_prv = to_device_private_bus(n);
867 struct device *dev = dev_prv->device;
872 static ssize_t bus_uevent_store(struct bus_type *bus,
873 const char *buf, size_t count)
875 enum kobject_action action;
877 if (kobject_action_type(buf, count, &action) == 0)
878 kobject_uevent(&bus->p->subsys.kobj, action);
881 static BUS_ATTR(uevent, S_IWUSR, NULL, bus_uevent_store);
884 * bus_register - register a driver-core subsystem
885 * @bus: bus to register
887 * Once we have that, we register the bus with the kobject
888 * infrastructure, then register the children subsystems it has:
889 * the devices and drivers that belong to the subsystem.
891 int bus_register(struct bus_type *bus)
894 struct subsys_private *priv;
895 struct lock_class_key *key = &bus->lock_key;
897 priv = kzalloc(sizeof(struct subsys_private), GFP_KERNEL);
904 BLOCKING_INIT_NOTIFIER_HEAD(&priv->bus_notifier);
906 retval = kobject_set_name(&priv->subsys.kobj, "%s", bus->name);
910 priv->subsys.kobj.kset = bus_kset;
911 priv->subsys.kobj.ktype = &bus_ktype;
912 priv->drivers_autoprobe = 1;
914 retval = kset_register(&priv->subsys);
918 retval = bus_create_file(bus, &bus_attr_uevent);
920 goto bus_uevent_fail;
922 priv->devices_kset = kset_create_and_add("devices", NULL,
924 if (!priv->devices_kset) {
926 goto bus_devices_fail;
929 priv->drivers_kset = kset_create_and_add("drivers", NULL,
931 if (!priv->drivers_kset) {
933 goto bus_drivers_fail;
936 INIT_LIST_HEAD(&priv->interfaces);
937 __mutex_init(&priv->mutex, "subsys mutex", key);
938 klist_init(&priv->klist_devices, klist_devices_get, klist_devices_put);
939 klist_init(&priv->klist_drivers, NULL, NULL);
941 retval = add_probe_files(bus);
943 goto bus_probe_files_fail;
945 retval = bus_add_groups(bus, bus->bus_groups);
947 goto bus_groups_fail;
949 pr_debug("bus: '%s': registered\n", bus->name);
953 remove_probe_files(bus);
954 bus_probe_files_fail:
955 kset_unregister(bus->p->drivers_kset);
957 kset_unregister(bus->p->devices_kset);
959 bus_remove_file(bus, &bus_attr_uevent);
961 kset_unregister(&bus->p->subsys);
967 EXPORT_SYMBOL_GPL(bus_register);
970 * bus_unregister - remove a bus from the system
973 * Unregister the child subsystems and the bus itself.
974 * Finally, we call bus_put() to release the refcount
976 void bus_unregister(struct bus_type *bus)
978 pr_debug("bus: '%s': unregistering\n", bus->name);
980 device_unregister(bus->dev_root);
981 bus_remove_groups(bus, bus->bus_groups);
982 remove_probe_files(bus);
983 kset_unregister(bus->p->drivers_kset);
984 kset_unregister(bus->p->devices_kset);
985 bus_remove_file(bus, &bus_attr_uevent);
986 kset_unregister(&bus->p->subsys);
988 EXPORT_SYMBOL_GPL(bus_unregister);
990 int bus_register_notifier(struct bus_type *bus, struct notifier_block *nb)
992 return blocking_notifier_chain_register(&bus->p->bus_notifier, nb);
994 EXPORT_SYMBOL_GPL(bus_register_notifier);
996 int bus_unregister_notifier(struct bus_type *bus, struct notifier_block *nb)
998 return blocking_notifier_chain_unregister(&bus->p->bus_notifier, nb);
1000 EXPORT_SYMBOL_GPL(bus_unregister_notifier);
1002 struct kset *bus_get_kset(struct bus_type *bus)
1004 return &bus->p->subsys;
1006 EXPORT_SYMBOL_GPL(bus_get_kset);
1008 struct klist *bus_get_device_klist(struct bus_type *bus)
1010 return &bus->p->klist_devices;
1012 EXPORT_SYMBOL_GPL(bus_get_device_klist);
1015 * Yes, this forcibly breaks the klist abstraction temporarily. It
1016 * just wants to sort the klist, not change reference counts and
1017 * take/drop locks rapidly in the process. It does all this while
1018 * holding the lock for the list, so objects can't otherwise be
1019 * added/removed while we're swizzling.
1021 static void device_insertion_sort_klist(struct device *a, struct list_head *list,
1022 int (*compare)(const struct device *a,
1023 const struct device *b))
1025 struct list_head *pos;
1026 struct klist_node *n;
1027 struct device_private *dev_prv;
1030 list_for_each(pos, list) {
1031 n = container_of(pos, struct klist_node, n_node);
1032 dev_prv = to_device_private_bus(n);
1033 b = dev_prv->device;
1034 if (compare(a, b) <= 0) {
1035 list_move_tail(&a->p->knode_bus.n_node,
1036 &b->p->knode_bus.n_node);
1040 list_move_tail(&a->p->knode_bus.n_node, list);
1043 void bus_sort_breadthfirst(struct bus_type *bus,
1044 int (*compare)(const struct device *a,
1045 const struct device *b))
1047 LIST_HEAD(sorted_devices);
1048 struct list_head *pos, *tmp;
1049 struct klist_node *n;
1050 struct device_private *dev_prv;
1052 struct klist *device_klist;
1054 device_klist = bus_get_device_klist(bus);
1056 spin_lock(&device_klist->k_lock);
1057 list_for_each_safe(pos, tmp, &device_klist->k_list) {
1058 n = container_of(pos, struct klist_node, n_node);
1059 dev_prv = to_device_private_bus(n);
1060 dev = dev_prv->device;
1061 device_insertion_sort_klist(dev, &sorted_devices, compare);
1063 list_splice(&sorted_devices, &device_klist->k_list);
1064 spin_unlock(&device_klist->k_lock);
1066 EXPORT_SYMBOL_GPL(bus_sort_breadthfirst);
1069 * subsys_dev_iter_init - initialize subsys device iterator
1070 * @iter: subsys iterator to initialize
1071 * @subsys: the subsys we wanna iterate over
1072 * @start: the device to start iterating from, if any
1073 * @type: device_type of the devices to iterate over, NULL for all
1075 * Initialize subsys iterator @iter such that it iterates over devices
1076 * of @subsys. If @start is set, the list iteration will start there,
1077 * otherwise if it is NULL, the iteration starts at the beginning of
1080 void subsys_dev_iter_init(struct subsys_dev_iter *iter, struct bus_type *subsys,
1081 struct device *start, const struct device_type *type)
1083 struct klist_node *start_knode = NULL;
1086 start_knode = &start->p->knode_bus;
1087 klist_iter_init_node(&subsys->p->klist_devices, &iter->ki, start_knode);
1090 EXPORT_SYMBOL_GPL(subsys_dev_iter_init);
1093 * subsys_dev_iter_next - iterate to the next device
1094 * @iter: subsys iterator to proceed
1096 * Proceed @iter to the next device and return it. Returns NULL if
1097 * iteration is complete.
1099 * The returned device is referenced and won't be released till
1100 * iterator is proceed to the next device or exited. The caller is
1101 * free to do whatever it wants to do with the device including
1102 * calling back into subsys code.
1104 struct device *subsys_dev_iter_next(struct subsys_dev_iter *iter)
1106 struct klist_node *knode;
1110 knode = klist_next(&iter->ki);
1113 dev = container_of(knode, struct device_private, knode_bus)->device;
1114 if (!iter->type || iter->type == dev->type)
1118 EXPORT_SYMBOL_GPL(subsys_dev_iter_next);
1121 * subsys_dev_iter_exit - finish iteration
1122 * @iter: subsys iterator to finish
1124 * Finish an iteration. Always call this function after iteration is
1125 * complete whether the iteration ran till the end or not.
1127 void subsys_dev_iter_exit(struct subsys_dev_iter *iter)
1129 klist_iter_exit(&iter->ki);
1131 EXPORT_SYMBOL_GPL(subsys_dev_iter_exit);
1133 int subsys_interface_register(struct subsys_interface *sif)
1135 struct bus_type *subsys;
1136 struct subsys_dev_iter iter;
1139 if (!sif || !sif->subsys)
1142 subsys = bus_get(sif->subsys);
1146 mutex_lock(&subsys->p->mutex);
1147 list_add_tail(&sif->node, &subsys->p->interfaces);
1149 subsys_dev_iter_init(&iter, subsys, NULL, NULL);
1150 while ((dev = subsys_dev_iter_next(&iter)))
1151 sif->add_dev(dev, sif);
1152 subsys_dev_iter_exit(&iter);
1154 mutex_unlock(&subsys->p->mutex);
1158 EXPORT_SYMBOL_GPL(subsys_interface_register);
1160 void subsys_interface_unregister(struct subsys_interface *sif)
1162 struct bus_type *subsys;
1163 struct subsys_dev_iter iter;
1166 if (!sif || !sif->subsys)
1169 subsys = sif->subsys;
1171 mutex_lock(&subsys->p->mutex);
1172 list_del_init(&sif->node);
1173 if (sif->remove_dev) {
1174 subsys_dev_iter_init(&iter, subsys, NULL, NULL);
1175 while ((dev = subsys_dev_iter_next(&iter)))
1176 sif->remove_dev(dev, sif);
1177 subsys_dev_iter_exit(&iter);
1179 mutex_unlock(&subsys->p->mutex);
1183 EXPORT_SYMBOL_GPL(subsys_interface_unregister);
1185 static void system_root_device_release(struct device *dev)
1190 static int subsys_register(struct bus_type *subsys,
1191 const struct attribute_group **groups,
1192 struct kobject *parent_of_root)
1197 err = bus_register(subsys);
1201 dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1207 err = dev_set_name(dev, "%s", subsys->name);
1211 dev->kobj.parent = parent_of_root;
1212 dev->groups = groups;
1213 dev->release = system_root_device_release;
1215 err = device_register(dev);
1219 subsys->dev_root = dev;
1228 bus_unregister(subsys);
1233 * subsys_system_register - register a subsystem at /sys/devices/system/
1234 * @subsys: system subsystem
1235 * @groups: default attributes for the root device
1237 * All 'system' subsystems have a /sys/devices/system/<name> root device
1238 * with the name of the subsystem. The root device can carry subsystem-
1239 * wide attributes. All registered devices are below this single root
1240 * device and are named after the subsystem with a simple enumeration
1241 * number appended. The registered devices are not explicitly named;
1242 * only 'id' in the device needs to be set.
1244 * Do not use this interface for anything new, it exists for compatibility
1245 * with bad ideas only. New subsystems should use plain subsystems; and
1246 * add the subsystem-wide attributes should be added to the subsystem
1247 * directory itself and not some create fake root-device placed in
1248 * /sys/devices/system/<name>.
1250 int subsys_system_register(struct bus_type *subsys,
1251 const struct attribute_group **groups)
1253 return subsys_register(subsys, groups, &system_kset->kobj);
1255 EXPORT_SYMBOL_GPL(subsys_system_register);
1258 * subsys_virtual_register - register a subsystem at /sys/devices/virtual/
1259 * @subsys: virtual subsystem
1260 * @groups: default attributes for the root device
1262 * All 'virtual' subsystems have a /sys/devices/system/<name> root device
1263 * with the name of the subystem. The root device can carry subsystem-wide
1264 * attributes. All registered devices are below this single root device.
1265 * There's no restriction on device naming. This is for kernel software
1266 * constructs which need sysfs interface.
1268 int subsys_virtual_register(struct bus_type *subsys,
1269 const struct attribute_group **groups)
1271 struct kobject *virtual_dir;
1273 virtual_dir = virtual_device_parent(NULL);
1277 return subsys_register(subsys, groups, virtual_dir);
1279 EXPORT_SYMBOL_GPL(subsys_virtual_register);
1281 int __init buses_init(void)
1283 bus_kset = kset_create_and_add("bus", &bus_uevent_ops, NULL);
1287 system_kset = kset_create_and_add("system", NULL, &devices_kset->kobj);