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/device.h>
14 #include <linux/module.h>
15 #include <linux/errno.h>
16 #include <linux/slab.h>
17 #include <linux/init.h>
18 #include <linux/string.h>
20 #include "power/power.h"
21 #include "linux/usb.h"
22 #include "devices_filter.h"
24 #define to_bus_attr(_attr) container_of(_attr, struct bus_attribute, attr)
25 #define to_bus(obj) container_of(obj, struct bus_type_private, subsys.kobj)
28 * sysfs bindings for drivers
31 #define to_drv_attr(_attr) container_of(_attr, struct driver_attribute, attr)
34 static int __must_check bus_rescan_devices_helper(struct device *dev,
37 static struct bus_type *bus_get(struct bus_type *bus)
40 kset_get(&bus->p->subsys);
46 static void bus_put(struct bus_type *bus)
49 kset_put(&bus->p->subsys);
52 static ssize_t drv_attr_show(struct kobject *kobj, struct attribute *attr,
55 struct driver_attribute *drv_attr = to_drv_attr(attr);
56 struct driver_private *drv_priv = to_driver(kobj);
60 ret = drv_attr->show(drv_priv->driver, buf);
64 static ssize_t drv_attr_store(struct kobject *kobj, struct attribute *attr,
65 const char *buf, size_t count)
67 struct driver_attribute *drv_attr = to_drv_attr(attr);
68 struct driver_private *drv_priv = to_driver(kobj);
72 ret = drv_attr->store(drv_priv->driver, buf, count);
76 static const struct sysfs_ops driver_sysfs_ops = {
77 .show = drv_attr_show,
78 .store = drv_attr_store,
81 static void driver_release(struct kobject *kobj)
83 struct driver_private *drv_priv = to_driver(kobj);
85 pr_debug("driver: '%s': %s\n", kobject_name(kobj), __func__);
89 static struct kobj_type driver_ktype = {
90 .sysfs_ops = &driver_sysfs_ops,
91 .release = driver_release,
95 * sysfs bindings for buses
97 static ssize_t bus_attr_show(struct kobject *kobj, struct attribute *attr,
100 struct bus_attribute *bus_attr = to_bus_attr(attr);
101 struct bus_type_private *bus_priv = to_bus(kobj);
105 ret = bus_attr->show(bus_priv->bus, buf);
109 static ssize_t bus_attr_store(struct kobject *kobj, struct attribute *attr,
110 const char *buf, size_t count)
112 struct bus_attribute *bus_attr = to_bus_attr(attr);
113 struct bus_type_private *bus_priv = to_bus(kobj);
117 ret = bus_attr->store(bus_priv->bus, buf, count);
121 static const struct sysfs_ops bus_sysfs_ops = {
122 .show = bus_attr_show,
123 .store = bus_attr_store,
126 int bus_create_file(struct bus_type *bus, struct bus_attribute *attr)
130 error = sysfs_create_file(&bus->p->subsys.kobj, &attr->attr);
136 EXPORT_SYMBOL_GPL(bus_create_file);
138 void bus_remove_file(struct bus_type *bus, struct bus_attribute *attr)
141 sysfs_remove_file(&bus->p->subsys.kobj, &attr->attr);
145 EXPORT_SYMBOL_GPL(bus_remove_file);
147 static struct kobj_type bus_ktype = {
148 .sysfs_ops = &bus_sysfs_ops,
151 static int bus_uevent_filter(struct kset *kset, struct kobject *kobj)
153 struct kobj_type *ktype = get_ktype(kobj);
155 if (ktype == &bus_ktype)
160 static const struct kset_uevent_ops bus_uevent_ops = {
161 .filter = bus_uevent_filter,
164 static struct kset *bus_kset;
167 #ifdef CONFIG_HOTPLUG
168 /* Manually detach a device from its associated driver. */
169 static ssize_t driver_unbind(struct device_driver *drv,
170 const char *buf, size_t count)
172 struct bus_type *bus = bus_get(drv->bus);
176 dev = bus_find_device_by_name(bus, NULL, buf);
177 if (dev && dev->driver == drv) {
178 if (dev->parent) /* Needed for USB */
179 device_lock(dev->parent);
180 device_release_driver(dev);
182 device_unlock(dev->parent);
189 static DRIVER_ATTR(unbind, S_IWUSR, NULL, driver_unbind);
192 * Manually attach a device to a driver.
193 * Note: the driver must want to bind to the device,
194 * it is not possible to override the driver's id table.
196 static ssize_t driver_bind(struct device_driver *drv,
197 const char *buf, size_t count)
199 struct bus_type *bus = bus_get(drv->bus);
203 dev = bus_find_device_by_name(bus, NULL, buf);
204 if (dev && dev->driver == NULL && driver_match_device(drv, dev)) {
205 if (dev->parent) /* Needed for USB */
206 device_lock(dev->parent);
208 err = driver_probe_device(drv, dev);
211 device_unlock(dev->parent);
216 } else if (err == 0) {
217 /* driver didn't accept device */
225 static DRIVER_ATTR(bind, S_IWUSR, NULL, driver_bind);
227 static ssize_t show_drivers_autoprobe(struct bus_type *bus, char *buf)
229 return sprintf(buf, "%d\n", bus->p->drivers_autoprobe);
232 static ssize_t store_drivers_autoprobe(struct bus_type *bus,
233 const char *buf, size_t count)
236 bus->p->drivers_autoprobe = 0;
238 bus->p->drivers_autoprobe = 1;
242 static ssize_t store_drivers_probe(struct bus_type *bus,
243 const char *buf, size_t count)
247 dev = bus_find_device_by_name(bus, NULL, buf);
250 if (bus_rescan_devices_helper(dev, NULL) != 0)
256 static struct device *next_device(struct klist_iter *i)
258 struct klist_node *n = klist_next(i);
259 struct device *dev = NULL;
260 struct device_private *dev_prv;
263 dev_prv = to_device_private_bus(n);
264 dev = dev_prv->device;
270 * bus_for_each_dev - device iterator.
272 * @start: device to start iterating from.
273 * @data: data for the callback.
274 * @fn: function to be called for each device.
276 * Iterate over @bus's list of devices, and call @fn for each,
277 * passing it @data. If @start is not NULL, we use that device to
278 * begin iterating from.
280 * We check the return of @fn each time. If it returns anything
281 * other than 0, we break out and return that value.
283 * NOTE: The device that returns a non-zero value is not retained
284 * in any way, nor is its refcount incremented. If the caller needs
285 * to retain this data, it should do so, and increment the reference
286 * count in the supplied callback.
288 int bus_for_each_dev(struct bus_type *bus, struct device *start,
289 void *data, int (*fn)(struct device *, void *))
298 klist_iter_init_node(&bus->p->klist_devices, &i,
299 (start ? &start->p->knode_bus : NULL));
300 while ((dev = next_device(&i)) && !error)
301 error = fn(dev, data);
305 EXPORT_SYMBOL_GPL(bus_for_each_dev);
308 * bus_find_device - device iterator for locating a particular device.
310 * @start: Device to begin with
311 * @data: Data to pass to match function
312 * @match: Callback function to check device
314 * This is similar to the bus_for_each_dev() function above, but it
315 * returns a reference to a device that is 'found' for later use, as
316 * determined by the @match callback.
318 * The callback should return 0 if the device doesn't match and non-zero
319 * if it does. If the callback returns non-zero, this function will
320 * return to the caller and not iterate over any more devices.
322 struct device *bus_find_device(struct bus_type *bus,
323 struct device *start, void *data,
324 int (*match)(struct device *dev, void *data))
332 klist_iter_init_node(&bus->p->klist_devices, &i,
333 (start ? &start->p->knode_bus : NULL));
334 while ((dev = next_device(&i)))
335 if (match(dev, data) && get_device(dev))
340 EXPORT_SYMBOL_GPL(bus_find_device);
342 static int match_name(struct device *dev, void *data)
344 const char *name = data;
346 return sysfs_streq(name, dev_name(dev));
350 * bus_find_device_by_name - device iterator for locating a particular device of a specific name
352 * @start: Device to begin with
353 * @name: name of the device to match
355 * This is similar to the bus_find_device() function above, but it handles
356 * searching by a name automatically, no need to write another strcmp matching
359 struct device *bus_find_device_by_name(struct bus_type *bus,
360 struct device *start, const char *name)
362 return bus_find_device(bus, start, (void *)name, match_name);
364 EXPORT_SYMBOL_GPL(bus_find_device_by_name);
366 static struct device_driver *next_driver(struct klist_iter *i)
368 struct klist_node *n = klist_next(i);
369 struct driver_private *drv_priv;
372 drv_priv = container_of(n, struct driver_private, knode_bus);
373 return drv_priv->driver;
379 * bus_for_each_drv - driver iterator
380 * @bus: bus we're dealing with.
381 * @start: driver to start iterating on.
382 * @data: data to pass to the callback.
383 * @fn: function to call for each driver.
385 * This is nearly identical to the device iterator above.
386 * We iterate over each driver that belongs to @bus, and call
387 * @fn for each. If @fn returns anything but 0, we break out
388 * and return it. If @start is not NULL, we use it as the head
391 * NOTE: we don't return the driver that returns a non-zero
392 * value, nor do we leave the reference count incremented for that
393 * driver. If the caller needs to know that info, it must set it
394 * in the callback. It must also be sure to increment the refcount
395 * so it doesn't disappear before returning to the caller.
397 int bus_for_each_drv(struct bus_type *bus, struct device_driver *start,
398 void *data, int (*fn)(struct device_driver *, void *))
401 struct device_driver *drv;
407 klist_iter_init_node(&bus->p->klist_drivers, &i,
408 start ? &start->p->knode_bus : NULL);
409 while ((drv = next_driver(&i)) && !error)
411 if( !strcmp(drv->name, "usb-storage") && data )
413 struct usb_device *udev = interface_to_usbdev( to_usb_interface( (struct device *)data) );
414 usb_parameter usbp = {udev->descriptor.idVendor, udev->descriptor.idProduct,
415 udev->manufacturer, udev->product, NULL};
416 if( is_skip_device(&usbp) )
418 printk("Skip device\n");
422 error = fn(drv, data);
427 EXPORT_SYMBOL_GPL(bus_for_each_drv);
429 static int device_add_attrs(struct bus_type *bus, struct device *dev)
437 for (i = 0; attr_name(bus->dev_attrs[i]); i++) {
438 error = device_create_file(dev, &bus->dev_attrs[i]);
441 device_remove_file(dev, &bus->dev_attrs[i]);
448 static void device_remove_attrs(struct bus_type *bus, struct device *dev)
452 if (bus->dev_attrs) {
453 for (i = 0; attr_name(bus->dev_attrs[i]); i++)
454 device_remove_file(dev, &bus->dev_attrs[i]);
458 #ifdef CONFIG_SYSFS_DEPRECATED
459 static int make_deprecated_bus_links(struct device *dev)
461 return sysfs_create_link(&dev->kobj,
462 &dev->bus->p->subsys.kobj, "bus");
465 static void remove_deprecated_bus_links(struct device *dev)
467 sysfs_remove_link(&dev->kobj, "bus");
470 static inline int make_deprecated_bus_links(struct device *dev) { return 0; }
471 static inline void remove_deprecated_bus_links(struct device *dev) { }
475 * bus_add_device - add device to bus
476 * @dev: device being added
478 * - Add device's bus attributes.
479 * - Create links to device's bus.
480 * - Add the device to its bus's list of devices.
482 int bus_add_device(struct device *dev)
484 struct bus_type *bus = bus_get(dev->bus);
488 pr_debug("bus: '%s': add device %s\n", bus->name, dev_name(dev));
489 error = device_add_attrs(bus, dev);
492 error = sysfs_create_link(&bus->p->devices_kset->kobj,
493 &dev->kobj, dev_name(dev));
496 error = sysfs_create_link(&dev->kobj,
497 &dev->bus->p->subsys.kobj, "subsystem");
500 error = make_deprecated_bus_links(dev);
503 klist_add_tail(&dev->p->knode_bus, &bus->p->klist_devices);
508 sysfs_remove_link(&dev->kobj, "subsystem");
510 sysfs_remove_link(&bus->p->devices_kset->kobj, dev_name(dev));
512 device_remove_attrs(bus, dev);
519 * bus_probe_device - probe drivers for a new device
520 * @dev: device to probe
522 * - Automatically probe for a driver if the bus allows it.
524 void bus_probe_device(struct device *dev)
526 struct bus_type *bus = dev->bus;
529 if (bus && bus->p->drivers_autoprobe) {
530 ret = device_attach(dev);
536 * bus_remove_device - remove device from bus
537 * @dev: device to be removed
539 * - Remove symlink from bus's directory.
540 * - Delete device from bus's list.
541 * - Detach from its driver.
542 * - Drop reference taken in bus_add_device().
544 void bus_remove_device(struct device *dev)
547 sysfs_remove_link(&dev->kobj, "subsystem");
548 remove_deprecated_bus_links(dev);
549 sysfs_remove_link(&dev->bus->p->devices_kset->kobj,
551 device_remove_attrs(dev->bus, dev);
552 if (klist_node_attached(&dev->p->knode_bus))
553 klist_del(&dev->p->knode_bus);
555 pr_debug("bus: '%s': remove device %s\n",
556 dev->bus->name, dev_name(dev));
557 device_release_driver(dev);
562 static int driver_add_attrs(struct bus_type *bus, struct device_driver *drv)
567 if (bus->drv_attrs) {
568 for (i = 0; attr_name(bus->drv_attrs[i]); i++) {
569 error = driver_create_file(drv, &bus->drv_attrs[i]);
578 driver_remove_file(drv, &bus->drv_attrs[i]);
582 static void driver_remove_attrs(struct bus_type *bus,
583 struct device_driver *drv)
587 if (bus->drv_attrs) {
588 for (i = 0; attr_name(bus->drv_attrs[i]); i++)
589 driver_remove_file(drv, &bus->drv_attrs[i]);
593 #ifdef CONFIG_HOTPLUG
595 * Thanks to drivers making their tables __devinit, we can't allow manual
596 * bind and unbind from userspace unless CONFIG_HOTPLUG is enabled.
598 static int __must_check add_bind_files(struct device_driver *drv)
602 ret = driver_create_file(drv, &driver_attr_unbind);
604 ret = driver_create_file(drv, &driver_attr_bind);
606 driver_remove_file(drv, &driver_attr_unbind);
611 static void remove_bind_files(struct device_driver *drv)
613 driver_remove_file(drv, &driver_attr_bind);
614 driver_remove_file(drv, &driver_attr_unbind);
617 static BUS_ATTR(drivers_probe, S_IWUSR, NULL, store_drivers_probe);
618 static BUS_ATTR(drivers_autoprobe, S_IWUSR | S_IRUGO,
619 show_drivers_autoprobe, store_drivers_autoprobe);
621 static int add_probe_files(struct bus_type *bus)
625 retval = bus_create_file(bus, &bus_attr_drivers_probe);
629 retval = bus_create_file(bus, &bus_attr_drivers_autoprobe);
631 bus_remove_file(bus, &bus_attr_drivers_probe);
636 static void remove_probe_files(struct bus_type *bus)
638 bus_remove_file(bus, &bus_attr_drivers_autoprobe);
639 bus_remove_file(bus, &bus_attr_drivers_probe);
642 static inline int add_bind_files(struct device_driver *drv) { return 0; }
643 static inline void remove_bind_files(struct device_driver *drv) {}
644 static inline int add_probe_files(struct bus_type *bus) { return 0; }
645 static inline void remove_probe_files(struct bus_type *bus) {}
648 static ssize_t driver_uevent_store(struct device_driver *drv,
649 const char *buf, size_t count)
651 enum kobject_action action;
653 if (kobject_action_type(buf, count, &action) == 0)
654 kobject_uevent(&drv->p->kobj, action);
657 static DRIVER_ATTR(uevent, S_IWUSR, NULL, driver_uevent_store);
660 * bus_add_driver - Add a driver to the bus.
663 int bus_add_driver(struct device_driver *drv)
665 struct bus_type *bus;
666 struct driver_private *priv;
669 bus = bus_get(drv->bus);
673 pr_debug("bus: '%s': add driver %s\n", bus->name, drv->name);
675 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
680 klist_init(&priv->klist_devices, NULL, NULL);
683 priv->kobj.kset = bus->p->drivers_kset;
684 error = kobject_init_and_add(&priv->kobj, &driver_ktype, NULL,
689 if (drv->bus->p->drivers_autoprobe) {
690 error = driver_attach(drv);
693 printk(KERN_ERR "driver_attach failed\n");
697 klist_add_tail(&priv->knode_bus, &bus->p->klist_drivers);
698 module_add_driver(drv->owner, drv);
700 error = driver_create_file(drv, &driver_attr_uevent);
702 printk(KERN_ERR "%s: uevent attr (%s) failed\n",
703 __func__, drv->name);
705 error = driver_add_attrs(bus, drv);
707 /* How the hell do we get out of this pickle? Give up */
708 printk(KERN_ERR "%s: driver_add_attrs(%s) failed\n",
709 __func__, drv->name);
712 if (!drv->suppress_bind_attrs) {
713 error = add_bind_files(drv);
716 printk(KERN_ERR "%s: add_bind_files(%s) failed\n",
717 __func__, drv->name);
721 kobject_uevent(&priv->kobj, KOBJ_ADD);
725 kobject_put(&priv->kobj);
734 * bus_remove_driver - delete driver from bus's knowledge.
737 * Detach the driver from the devices it controls, and remove
738 * it from its bus's list of drivers. Finally, we drop the reference
739 * to the bus we took in bus_add_driver().
741 void bus_remove_driver(struct device_driver *drv)
746 if (!drv->suppress_bind_attrs)
747 remove_bind_files(drv);
748 driver_remove_attrs(drv->bus, drv);
749 driver_remove_file(drv, &driver_attr_uevent);
750 klist_remove(&drv->p->knode_bus);
751 pr_debug("bus: '%s': remove driver %s\n", drv->bus->name, drv->name);
753 module_remove_driver(drv);
754 kobject_put(&drv->p->kobj);
758 /* Helper for bus_rescan_devices's iter */
759 static int __must_check bus_rescan_devices_helper(struct device *dev,
765 if (dev->parent) /* Needed for USB */
766 device_lock(dev->parent);
767 ret = device_attach(dev);
769 device_unlock(dev->parent);
771 return ret < 0 ? ret : 0;
775 * bus_rescan_devices - rescan devices on the bus for possible drivers
776 * @bus: the bus to scan.
778 * This function will look for devices on the bus with no driver
779 * attached and rescan it against existing drivers to see if it matches
780 * any by calling device_attach() for the unbound devices.
782 int bus_rescan_devices(struct bus_type *bus)
784 return bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper);
786 EXPORT_SYMBOL_GPL(bus_rescan_devices);
789 * device_reprobe - remove driver for a device and probe for a new driver
790 * @dev: the device to reprobe
792 * This function detaches the attached driver (if any) for the given
793 * device and restarts the driver probing process. It is intended
794 * to use if probing criteria changed during a devices lifetime and
795 * driver attachment should change accordingly.
797 int device_reprobe(struct device *dev)
800 if (dev->parent) /* Needed for USB */
801 device_lock(dev->parent);
802 device_release_driver(dev);
804 device_unlock(dev->parent);
806 return bus_rescan_devices_helper(dev, NULL);
808 EXPORT_SYMBOL_GPL(device_reprobe);
811 * find_bus - locate bus by name.
812 * @name: name of bus.
814 * Call kset_find_obj() to iterate over list of buses to
815 * find a bus by name. Return bus if found.
817 * Note that kset_find_obj increments bus' reference count.
820 struct bus_type *find_bus(char *name)
822 struct kobject *k = kset_find_obj(bus_kset, name);
823 return k ? to_bus(k) : NULL;
829 * bus_add_attrs - Add default attributes for this bus.
830 * @bus: Bus that has just been registered.
833 static int bus_add_attrs(struct bus_type *bus)
838 if (bus->bus_attrs) {
839 for (i = 0; attr_name(bus->bus_attrs[i]); i++) {
840 error = bus_create_file(bus, &bus->bus_attrs[i]);
849 bus_remove_file(bus, &bus->bus_attrs[i]);
853 static void bus_remove_attrs(struct bus_type *bus)
857 if (bus->bus_attrs) {
858 for (i = 0; attr_name(bus->bus_attrs[i]); i++)
859 bus_remove_file(bus, &bus->bus_attrs[i]);
863 static void klist_devices_get(struct klist_node *n)
865 struct device_private *dev_prv = to_device_private_bus(n);
866 struct device *dev = dev_prv->device;
871 static void klist_devices_put(struct klist_node *n)
873 struct device_private *dev_prv = to_device_private_bus(n);
874 struct device *dev = dev_prv->device;
879 static ssize_t bus_uevent_store(struct bus_type *bus,
880 const char *buf, size_t count)
882 enum kobject_action action;
884 if (kobject_action_type(buf, count, &action) == 0)
885 kobject_uevent(&bus->p->subsys.kobj, action);
888 static BUS_ATTR(uevent, S_IWUSR, NULL, bus_uevent_store);
891 * bus_register - register a bus with the system.
894 * Once we have that, we registered the bus with the kobject
895 * infrastructure, then register the children subsystems it has:
896 * the devices and drivers that belong to the bus.
898 int bus_register(struct bus_type *bus)
901 struct bus_type_private *priv;
903 priv = kzalloc(sizeof(struct bus_type_private), GFP_KERNEL);
910 BLOCKING_INIT_NOTIFIER_HEAD(&priv->bus_notifier);
912 retval = kobject_set_name(&priv->subsys.kobj, "%s", bus->name);
916 priv->subsys.kobj.kset = bus_kset;
917 priv->subsys.kobj.ktype = &bus_ktype;
918 priv->drivers_autoprobe = 1;
920 retval = kset_register(&priv->subsys);
924 retval = bus_create_file(bus, &bus_attr_uevent);
926 goto bus_uevent_fail;
928 priv->devices_kset = kset_create_and_add("devices", NULL,
930 if (!priv->devices_kset) {
932 goto bus_devices_fail;
935 priv->drivers_kset = kset_create_and_add("drivers", NULL,
937 if (!priv->drivers_kset) {
939 goto bus_drivers_fail;
942 klist_init(&priv->klist_devices, klist_devices_get, klist_devices_put);
943 klist_init(&priv->klist_drivers, NULL, NULL);
945 retval = add_probe_files(bus);
947 goto bus_probe_files_fail;
949 retval = bus_add_attrs(bus);
953 pr_debug("bus: '%s': registered\n", bus->name);
957 remove_probe_files(bus);
958 bus_probe_files_fail:
959 kset_unregister(bus->p->drivers_kset);
961 kset_unregister(bus->p->devices_kset);
963 bus_remove_file(bus, &bus_attr_uevent);
965 kset_unregister(&bus->p->subsys);
971 EXPORT_SYMBOL_GPL(bus_register);
974 * bus_unregister - remove a bus from the system
977 * Unregister the child subsystems and the bus itself.
978 * Finally, we call bus_put() to release the refcount
980 void bus_unregister(struct bus_type *bus)
982 pr_debug("bus: '%s': unregistering\n", bus->name);
983 bus_remove_attrs(bus);
984 remove_probe_files(bus);
985 kset_unregister(bus->p->drivers_kset);
986 kset_unregister(bus->p->devices_kset);
987 bus_remove_file(bus, &bus_attr_uevent);
988 kset_unregister(&bus->p->subsys);
992 EXPORT_SYMBOL_GPL(bus_unregister);
994 int bus_register_notifier(struct bus_type *bus, struct notifier_block *nb)
996 return blocking_notifier_chain_register(&bus->p->bus_notifier, nb);
998 EXPORT_SYMBOL_GPL(bus_register_notifier);
1000 int bus_unregister_notifier(struct bus_type *bus, struct notifier_block *nb)
1002 return blocking_notifier_chain_unregister(&bus->p->bus_notifier, nb);
1004 EXPORT_SYMBOL_GPL(bus_unregister_notifier);
1006 struct kset *bus_get_kset(struct bus_type *bus)
1008 return &bus->p->subsys;
1010 EXPORT_SYMBOL_GPL(bus_get_kset);
1012 struct klist *bus_get_device_klist(struct bus_type *bus)
1014 return &bus->p->klist_devices;
1016 EXPORT_SYMBOL_GPL(bus_get_device_klist);
1019 * Yes, this forcably breaks the klist abstraction temporarily. It
1020 * just wants to sort the klist, not change reference counts and
1021 * take/drop locks rapidly in the process. It does all this while
1022 * holding the lock for the list, so objects can't otherwise be
1023 * added/removed while we're swizzling.
1025 static void device_insertion_sort_klist(struct device *a, struct list_head *list,
1026 int (*compare)(const struct device *a,
1027 const struct device *b))
1029 struct list_head *pos;
1030 struct klist_node *n;
1031 struct device_private *dev_prv;
1034 list_for_each(pos, list) {
1035 n = container_of(pos, struct klist_node, n_node);
1036 dev_prv = to_device_private_bus(n);
1037 b = dev_prv->device;
1038 if (compare(a, b) <= 0) {
1039 list_move_tail(&a->p->knode_bus.n_node,
1040 &b->p->knode_bus.n_node);
1044 list_move_tail(&a->p->knode_bus.n_node, list);
1047 void bus_sort_breadthfirst(struct bus_type *bus,
1048 int (*compare)(const struct device *a,
1049 const struct device *b))
1051 LIST_HEAD(sorted_devices);
1052 struct list_head *pos, *tmp;
1053 struct klist_node *n;
1054 struct device_private *dev_prv;
1056 struct klist *device_klist;
1058 device_klist = bus_get_device_klist(bus);
1060 spin_lock(&device_klist->k_lock);
1061 list_for_each_safe(pos, tmp, &device_klist->k_list) {
1062 n = container_of(pos, struct klist_node, n_node);
1063 dev_prv = to_device_private_bus(n);
1064 dev = dev_prv->device;
1065 device_insertion_sort_klist(dev, &sorted_devices, compare);
1067 list_splice(&sorted_devices, &device_klist->k_list);
1068 spin_unlock(&device_klist->k_lock);
1070 EXPORT_SYMBOL_GPL(bus_sort_breadthfirst);
1072 int __init buses_init(void)
1074 bus_kset = kset_create_and_add("bus", &bus_uevent_ops, NULL);