1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * bus.c - bus driver management 4 * 5 * Copyright (c) 2002-3 Patrick Mochel 6 * Copyright (c) 2002-3 Open Source Development Labs 7 * Copyright (c) 2007 Greg Kroah-Hartman <gregkh@suse.de> 8 * Copyright (c) 2007 Novell Inc. 9 * Copyright (c) 2023 Greg Kroah-Hartman <gregkh@linuxfoundation.org> 10 */ 11 12 #include <linux/async.h> 13 #include <linux/device/bus.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> 22 #include "base.h" 23 #include "power/power.h" 24 25 /* /sys/devices/system */ 26 static struct kset *system_kset; 27 28 /* /sys/bus */ 29 static struct kset *bus_kset; 30 31 #define to_bus_attr(_attr) container_of(_attr, struct bus_attribute, attr) 32 33 /* 34 * sysfs bindings for drivers 35 */ 36 37 #define to_drv_attr(_attr) container_of(_attr, struct driver_attribute, attr) 38 39 #define DRIVER_ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) \ 40 struct driver_attribute driver_attr_##_name = \ 41 __ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) 42 43 static int __must_check bus_rescan_devices_helper(struct device *dev, 44 void *data); 45 46 /** 47 * bus_to_subsys - Turn a struct bus_type into a struct subsys_private 48 * 49 * @bus: pointer to the struct bus_type to look up 50 * 51 * The driver core internals needs to work on the subsys_private structure, not 52 * the external struct bus_type pointer. This function walks the list of 53 * registered busses in the system and finds the matching one and returns the 54 * internal struct subsys_private that relates to that bus. 55 * 56 * Note, the reference count of the return value is INCREMENTED if it is not 57 * NULL. A call to subsys_put() must be done when finished with the pointer in 58 * order for it to be properly freed. 59 */ 60 struct subsys_private *bus_to_subsys(const struct bus_type *bus) 61 { 62 struct subsys_private *sp = NULL; 63 struct kobject *kobj; 64 65 if (!bus || !bus_kset) 66 return NULL; 67 68 spin_lock(&bus_kset->list_lock); 69 70 if (list_empty(&bus_kset->list)) 71 goto done; 72 73 list_for_each_entry(kobj, &bus_kset->list, entry) { 74 struct kset *kset = container_of(kobj, struct kset, kobj); 75 76 sp = container_of_const(kset, struct subsys_private, subsys); 77 if (sp->bus == bus) 78 goto done; 79 } 80 sp = NULL; 81 done: 82 sp = subsys_get(sp); 83 spin_unlock(&bus_kset->list_lock); 84 return sp; 85 } 86 87 static const struct bus_type *bus_get(const struct bus_type *bus) 88 { 89 struct subsys_private *sp = bus_to_subsys(bus); 90 91 if (sp) 92 return bus; 93 return NULL; 94 } 95 96 static void bus_put(const struct bus_type *bus) 97 { 98 struct subsys_private *sp = bus_to_subsys(bus); 99 100 /* two puts are required as the call to bus_to_subsys incremented it again */ 101 subsys_put(sp); 102 subsys_put(sp); 103 } 104 105 static ssize_t drv_attr_show(struct kobject *kobj, struct attribute *attr, 106 char *buf) 107 { 108 struct driver_attribute *drv_attr = to_drv_attr(attr); 109 struct driver_private *drv_priv = to_driver(kobj); 110 ssize_t ret = -EIO; 111 112 if (drv_attr->show) 113 ret = drv_attr->show(drv_priv->driver, buf); 114 return ret; 115 } 116 117 static ssize_t drv_attr_store(struct kobject *kobj, struct attribute *attr, 118 const char *buf, size_t count) 119 { 120 struct driver_attribute *drv_attr = to_drv_attr(attr); 121 struct driver_private *drv_priv = to_driver(kobj); 122 ssize_t ret = -EIO; 123 124 if (drv_attr->store) 125 ret = drv_attr->store(drv_priv->driver, buf, count); 126 return ret; 127 } 128 129 static const struct sysfs_ops driver_sysfs_ops = { 130 .show = drv_attr_show, 131 .store = drv_attr_store, 132 }; 133 134 static void driver_release(struct kobject *kobj) 135 { 136 struct driver_private *drv_priv = to_driver(kobj); 137 138 pr_debug("driver: '%s': %s\n", kobject_name(kobj), __func__); 139 kfree(drv_priv); 140 } 141 142 static const struct kobj_type driver_ktype = { 143 .sysfs_ops = &driver_sysfs_ops, 144 .release = driver_release, 145 }; 146 147 /* 148 * sysfs bindings for buses 149 */ 150 static ssize_t bus_attr_show(struct kobject *kobj, struct attribute *attr, 151 char *buf) 152 { 153 struct bus_attribute *bus_attr = to_bus_attr(attr); 154 struct subsys_private *subsys_priv = to_subsys_private(kobj); 155 /* return -EIO for reading a bus attribute without show() */ 156 ssize_t ret = -EIO; 157 158 if (bus_attr->show) 159 ret = bus_attr->show(subsys_priv->bus, buf); 160 return ret; 161 } 162 163 static ssize_t bus_attr_store(struct kobject *kobj, struct attribute *attr, 164 const char *buf, size_t count) 165 { 166 struct bus_attribute *bus_attr = to_bus_attr(attr); 167 struct subsys_private *subsys_priv = to_subsys_private(kobj); 168 /* return -EIO for writing a bus attribute without store() */ 169 ssize_t ret = -EIO; 170 171 if (bus_attr->store) 172 ret = bus_attr->store(subsys_priv->bus, buf, count); 173 return ret; 174 } 175 176 static const struct sysfs_ops bus_sysfs_ops = { 177 .show = bus_attr_show, 178 .store = bus_attr_store, 179 }; 180 181 int bus_create_file(const struct bus_type *bus, struct bus_attribute *attr) 182 { 183 struct subsys_private *sp = bus_to_subsys(bus); 184 int error; 185 186 if (!sp) 187 return -EINVAL; 188 189 error = sysfs_create_file(&sp->subsys.kobj, &attr->attr); 190 191 subsys_put(sp); 192 return error; 193 } 194 EXPORT_SYMBOL_GPL(bus_create_file); 195 196 void bus_remove_file(const struct bus_type *bus, struct bus_attribute *attr) 197 { 198 struct subsys_private *sp = bus_to_subsys(bus); 199 200 if (!sp) 201 return; 202 203 sysfs_remove_file(&sp->subsys.kobj, &attr->attr); 204 subsys_put(sp); 205 } 206 EXPORT_SYMBOL_GPL(bus_remove_file); 207 208 static void bus_release(struct kobject *kobj) 209 { 210 struct subsys_private *priv = to_subsys_private(kobj); 211 212 lockdep_unregister_key(&priv->lock_key); 213 kfree(priv); 214 } 215 216 static const struct kobj_type bus_ktype = { 217 .sysfs_ops = &bus_sysfs_ops, 218 .release = bus_release, 219 }; 220 221 static int bus_uevent_filter(const struct kobject *kobj) 222 { 223 const struct kobj_type *ktype = get_ktype(kobj); 224 225 if (ktype == &bus_ktype) 226 return 1; 227 return 0; 228 } 229 230 static const struct kset_uevent_ops bus_uevent_ops = { 231 .filter = bus_uevent_filter, 232 }; 233 234 /* Manually detach a device from its associated driver. */ 235 static ssize_t unbind_store(struct device_driver *drv, const char *buf, 236 size_t count) 237 { 238 const struct bus_type *bus = bus_get(drv->bus); 239 struct device *dev; 240 int err = -ENODEV; 241 242 dev = bus_find_device_by_name(bus, NULL, buf); 243 if (dev && dev->driver == drv) { 244 device_driver_detach(dev); 245 err = count; 246 } 247 put_device(dev); 248 bus_put(bus); 249 return err; 250 } 251 static DRIVER_ATTR_IGNORE_LOCKDEP(unbind, 0200, NULL, unbind_store); 252 253 /* 254 * Manually attach a device to a driver. 255 * Note: the driver must want to bind to the device, 256 * it is not possible to override the driver's id table. 257 */ 258 static ssize_t bind_store(struct device_driver *drv, const char *buf, 259 size_t count) 260 { 261 const struct bus_type *bus = bus_get(drv->bus); 262 struct device *dev; 263 int err = -ENODEV; 264 265 dev = bus_find_device_by_name(bus, NULL, buf); 266 if (dev && driver_match_device(drv, dev)) { 267 err = device_driver_attach(drv, dev); 268 if (!err) { 269 /* success */ 270 err = count; 271 } 272 } 273 put_device(dev); 274 bus_put(bus); 275 return err; 276 } 277 static DRIVER_ATTR_IGNORE_LOCKDEP(bind, 0200, NULL, bind_store); 278 279 static ssize_t drivers_autoprobe_show(const struct bus_type *bus, char *buf) 280 { 281 struct subsys_private *sp = bus_to_subsys(bus); 282 int ret; 283 284 if (!sp) 285 return -EINVAL; 286 287 ret = sysfs_emit(buf, "%d\n", sp->drivers_autoprobe); 288 subsys_put(sp); 289 return ret; 290 } 291 292 static ssize_t drivers_autoprobe_store(const struct bus_type *bus, 293 const char *buf, size_t count) 294 { 295 struct subsys_private *sp = bus_to_subsys(bus); 296 297 if (!sp) 298 return -EINVAL; 299 300 if (buf[0] == '0') 301 sp->drivers_autoprobe = 0; 302 else 303 sp->drivers_autoprobe = 1; 304 305 subsys_put(sp); 306 return count; 307 } 308 309 static ssize_t drivers_probe_store(const struct bus_type *bus, 310 const char *buf, size_t count) 311 { 312 struct device *dev; 313 int err = -EINVAL; 314 315 dev = bus_find_device_by_name(bus, NULL, buf); 316 if (!dev) 317 return -ENODEV; 318 if (bus_rescan_devices_helper(dev, NULL) == 0) 319 err = count; 320 put_device(dev); 321 return err; 322 } 323 324 static struct device *next_device(struct klist_iter *i) 325 { 326 struct klist_node *n = klist_next(i); 327 struct device *dev = NULL; 328 struct device_private *dev_prv; 329 330 if (n) { 331 dev_prv = to_device_private_bus(n); 332 dev = dev_prv->device; 333 } 334 return dev; 335 } 336 337 static struct device *prev_device(struct klist_iter *i) 338 { 339 struct klist_node *n = klist_prev(i); 340 struct device *dev = NULL; 341 struct device_private *dev_prv; 342 343 if (n) { 344 dev_prv = to_device_private_bus(n); 345 dev = dev_prv->device; 346 } 347 return dev; 348 } 349 350 /** 351 * bus_for_each_dev - device iterator. 352 * @bus: bus type. 353 * @start: device to start iterating from. 354 * @data: data for the callback. 355 * @fn: function to be called for each device. 356 * 357 * Iterate over @bus's list of devices, and call @fn for each, 358 * passing it @data. If @start is not NULL, we use that device to 359 * begin iterating from. 360 * 361 * We check the return of @fn each time. If it returns anything 362 * other than 0, we break out and return that value. 363 * 364 * NOTE: The device that returns a non-zero value is not retained 365 * in any way, nor is its refcount incremented. If the caller needs 366 * to retain this data, it should do so, and increment the reference 367 * count in the supplied callback. 368 */ 369 int bus_for_each_dev(const struct bus_type *bus, struct device *start, 370 void *data, device_iter_t fn) 371 { 372 struct subsys_private *sp = bus_to_subsys(bus); 373 struct klist_iter i; 374 struct device *dev; 375 int error = 0; 376 377 if (!sp) 378 return -EINVAL; 379 380 klist_iter_init_node(&sp->klist_devices, &i, 381 (start ? &start->p->knode_bus : NULL)); 382 while (!error && (dev = next_device(&i))) 383 error = fn(dev, data); 384 klist_iter_exit(&i); 385 subsys_put(sp); 386 return error; 387 } 388 EXPORT_SYMBOL_GPL(bus_for_each_dev); 389 390 /** 391 * bus_find_device - device iterator for locating a particular device. 392 * @bus: bus type 393 * @start: Device to begin with 394 * @data: Data to pass to match function 395 * @match: Callback function to check device 396 * 397 * This is similar to the bus_for_each_dev() function above, but it 398 * returns a reference to a device that is 'found' for later use, as 399 * determined by the @match callback. 400 * 401 * The callback should return 0 if the device doesn't match and non-zero 402 * if it does. If the callback returns non-zero, this function will 403 * return to the caller and not iterate over any more devices. 404 */ 405 struct device *bus_find_device(const struct bus_type *bus, 406 struct device *start, const void *data, 407 device_match_t match) 408 { 409 struct subsys_private *sp = bus_to_subsys(bus); 410 struct klist_iter i; 411 struct device *dev; 412 413 if (!sp) 414 return NULL; 415 416 klist_iter_init_node(&sp->klist_devices, &i, 417 (start ? &start->p->knode_bus : NULL)); 418 while ((dev = next_device(&i))) { 419 if (match(dev, data)) { 420 get_device(dev); 421 break; 422 } 423 } 424 klist_iter_exit(&i); 425 subsys_put(sp); 426 return dev; 427 } 428 EXPORT_SYMBOL_GPL(bus_find_device); 429 430 struct device *bus_find_device_reverse(const struct bus_type *bus, 431 struct device *start, const void *data, 432 device_match_t match) 433 { 434 struct subsys_private *sp = bus_to_subsys(bus); 435 struct klist_iter i; 436 struct device *dev; 437 438 if (!sp) 439 return NULL; 440 441 klist_iter_init_node(&sp->klist_devices, &i, 442 (start ? &start->p->knode_bus : NULL)); 443 while ((dev = prev_device(&i))) { 444 if (match(dev, data)) { 445 get_device(dev); 446 break; 447 } 448 } 449 klist_iter_exit(&i); 450 subsys_put(sp); 451 return dev; 452 } 453 EXPORT_SYMBOL_GPL(bus_find_device_reverse); 454 455 static struct device_driver *next_driver(struct klist_iter *i) 456 { 457 struct klist_node *n = klist_next(i); 458 struct driver_private *drv_priv; 459 460 if (n) { 461 drv_priv = container_of(n, struct driver_private, knode_bus); 462 return drv_priv->driver; 463 } 464 return NULL; 465 } 466 467 /** 468 * bus_for_each_drv - driver iterator 469 * @bus: bus we're dealing with. 470 * @start: driver to start iterating on. 471 * @data: data to pass to the callback. 472 * @fn: function to call for each driver. 473 * 474 * This is nearly identical to the device iterator above. 475 * We iterate over each driver that belongs to @bus, and call 476 * @fn for each. If @fn returns anything but 0, we break out 477 * and return it. If @start is not NULL, we use it as the head 478 * of the list. 479 * 480 * NOTE: we don't return the driver that returns a non-zero 481 * value, nor do we leave the reference count incremented for that 482 * driver. If the caller needs to know that info, it must set it 483 * in the callback. It must also be sure to increment the refcount 484 * so it doesn't disappear before returning to the caller. 485 */ 486 int bus_for_each_drv(const struct bus_type *bus, struct device_driver *start, 487 void *data, int (*fn)(struct device_driver *, void *)) 488 { 489 struct subsys_private *sp = bus_to_subsys(bus); 490 struct klist_iter i; 491 struct device_driver *drv; 492 int error = 0; 493 494 if (!sp) 495 return -EINVAL; 496 497 klist_iter_init_node(&sp->klist_drivers, &i, 498 start ? &start->p->knode_bus : NULL); 499 while ((drv = next_driver(&i)) && !error) 500 error = fn(drv, data); 501 klist_iter_exit(&i); 502 subsys_put(sp); 503 return error; 504 } 505 EXPORT_SYMBOL_GPL(bus_for_each_drv); 506 507 static ssize_t driver_override_store(struct device *dev, 508 struct device_attribute *attr, 509 const char *buf, size_t count) 510 { 511 int ret; 512 513 ret = __device_set_driver_override(dev, buf, count); 514 if (ret) 515 return ret; 516 517 return count; 518 } 519 520 static ssize_t driver_override_show(struct device *dev, 521 struct device_attribute *attr, char *buf) 522 { 523 guard(spinlock)(&dev->driver_override.lock); 524 return sysfs_emit(buf, "%s\n", dev->driver_override.name); 525 } 526 static DEVICE_ATTR_RW(driver_override); 527 528 static struct attribute *driver_override_dev_attrs[] = { 529 &dev_attr_driver_override.attr, 530 NULL, 531 }; 532 533 static const struct attribute_group driver_override_dev_group = { 534 .attrs = driver_override_dev_attrs, 535 }; 536 537 /** 538 * bus_add_device - add device to bus 539 * @dev: device being added 540 * 541 * - Add device's bus attributes. 542 * - Create links to device's bus. 543 * - Add the device to its bus's list of devices. 544 */ 545 int bus_add_device(struct device *dev) 546 { 547 struct subsys_private *sp; 548 int error; 549 550 if (!dev->bus) { 551 /* 552 * This is a normal operation for many devices that do not 553 * have a bus assigned to them, just say that all went 554 * well. 555 */ 556 return 0; 557 } 558 559 sp = bus_to_subsys(dev->bus); 560 if (!sp) { 561 pr_err("%s: cannot add device '%s' to unregistered bus '%s'\n", 562 __func__, dev_name(dev), dev->bus->name); 563 return -EINVAL; 564 } 565 566 /* 567 * Reference in sp is now incremented and will be dropped when 568 * the device is removed from the bus 569 */ 570 571 pr_debug("bus: '%s': add device %s\n", sp->bus->name, dev_name(dev)); 572 573 error = device_add_groups(dev, sp->bus->dev_groups); 574 if (error) 575 goto out_put; 576 577 if (dev->bus->driver_override) { 578 error = device_add_group(dev, &driver_override_dev_group); 579 if (error) 580 goto out_groups; 581 } 582 583 error = sysfs_create_link(&sp->devices_kset->kobj, &dev->kobj, dev_name(dev)); 584 if (error) 585 goto out_override; 586 587 error = sysfs_create_link(&dev->kobj, &sp->subsys.kobj, "subsystem"); 588 if (error) 589 goto out_subsys; 590 591 klist_add_tail(&dev->p->knode_bus, &sp->klist_devices); 592 return 0; 593 594 out_subsys: 595 sysfs_remove_link(&sp->devices_kset->kobj, dev_name(dev)); 596 out_override: 597 if (dev->bus->driver_override) 598 device_remove_group(dev, &driver_override_dev_group); 599 out_groups: 600 device_remove_groups(dev, sp->bus->dev_groups); 601 out_put: 602 subsys_put(sp); 603 return error; 604 } 605 606 static void __bus_probe_device(struct device *dev) 607 { 608 struct subsys_private *sp = bus_to_subsys(dev->bus); 609 struct subsys_interface *sif; 610 611 if (!sp) 612 return; 613 614 device_initial_probe(dev); 615 616 mutex_lock(&sp->mutex); 617 list_for_each_entry(sif, &sp->interfaces, node) 618 if (sif->add_dev) 619 sif->add_dev(dev, sif); 620 mutex_unlock(&sp->mutex); 621 subsys_put(sp); 622 } 623 624 /** 625 * bus_probe_device - probe drivers for a new device 626 * @dev: device to probe 627 * 628 * - Automatically probe for a driver if the bus allows it. 629 */ 630 void bus_probe_device(struct device *dev) 631 { 632 __bus_probe_device(dev); 633 } 634 635 void bus_probe_device_deferred(struct device *dev) 636 { 637 if (dev->type && dev->type->need_parent_lock) 638 device_lock(dev->parent); 639 __bus_probe_device(dev); 640 if (dev->type && dev->type->need_parent_lock) 641 device_unlock(dev->parent); 642 } 643 644 /** 645 * bus_remove_device - remove device from bus 646 * @dev: device to be removed 647 * 648 * - Remove device from all interfaces. 649 * - Remove symlink from bus' directory. 650 * - Delete device from bus's list. 651 * - Detach from its driver. 652 * - Drop reference taken in bus_add_device(). 653 */ 654 void bus_remove_device(struct device *dev) 655 { 656 struct subsys_private *sp = bus_to_subsys(dev->bus); 657 struct subsys_interface *sif; 658 659 if (!sp) 660 return; 661 662 mutex_lock(&sp->mutex); 663 list_for_each_entry(sif, &sp->interfaces, node) 664 if (sif->remove_dev) 665 sif->remove_dev(dev, sif); 666 mutex_unlock(&sp->mutex); 667 668 sysfs_remove_link(&dev->kobj, "subsystem"); 669 sysfs_remove_link(&sp->devices_kset->kobj, dev_name(dev)); 670 if (dev->bus->driver_override) 671 device_remove_group(dev, &driver_override_dev_group); 672 device_remove_groups(dev, dev->bus->dev_groups); 673 if (klist_node_attached(&dev->p->knode_bus)) 674 klist_del(&dev->p->knode_bus); 675 676 pr_debug("bus: '%s': remove device %s\n", 677 dev->bus->name, dev_name(dev)); 678 device_release_driver(dev); 679 680 /* 681 * Decrement the reference count twice, once for the bus_to_subsys() 682 * call in the start of this function, and the second one from the 683 * reference increment in bus_add_device() 684 */ 685 subsys_put(sp); 686 subsys_put(sp); 687 } 688 689 static int __must_check add_bind_files(struct device_driver *drv) 690 { 691 int ret; 692 693 ret = driver_create_file(drv, &driver_attr_unbind); 694 if (ret == 0) { 695 ret = driver_create_file(drv, &driver_attr_bind); 696 if (ret) 697 driver_remove_file(drv, &driver_attr_unbind); 698 } 699 return ret; 700 } 701 702 static void remove_bind_files(struct device_driver *drv) 703 { 704 driver_remove_file(drv, &driver_attr_bind); 705 driver_remove_file(drv, &driver_attr_unbind); 706 } 707 708 static BUS_ATTR_WO(drivers_probe); 709 static BUS_ATTR_RW(drivers_autoprobe); 710 711 static int add_probe_files(const struct bus_type *bus) 712 { 713 int retval; 714 715 retval = bus_create_file(bus, &bus_attr_drivers_probe); 716 if (retval) 717 goto out; 718 719 retval = bus_create_file(bus, &bus_attr_drivers_autoprobe); 720 if (retval) 721 bus_remove_file(bus, &bus_attr_drivers_probe); 722 out: 723 return retval; 724 } 725 726 static void remove_probe_files(const struct bus_type *bus) 727 { 728 bus_remove_file(bus, &bus_attr_drivers_autoprobe); 729 bus_remove_file(bus, &bus_attr_drivers_probe); 730 } 731 732 static ssize_t uevent_store(struct device_driver *drv, const char *buf, 733 size_t count) 734 { 735 int rc; 736 737 rc = kobject_synth_uevent(&drv->p->kobj, buf, count); 738 return rc ? rc : count; 739 } 740 static DRIVER_ATTR_WO(uevent); 741 742 /** 743 * bus_add_driver - Add a driver to the bus. 744 * @drv: driver. 745 */ 746 int bus_add_driver(struct device_driver *drv) 747 { 748 struct subsys_private *sp = bus_to_subsys(drv->bus); 749 struct driver_private *priv; 750 int error = 0; 751 752 if (!sp) 753 return -EINVAL; 754 755 if (sp->no_drivers) { 756 error = -ENXIO; 757 goto out_put_bus; 758 } 759 760 /* 761 * Reference in sp is now incremented and will be dropped when 762 * the driver is removed from the bus 763 */ 764 pr_debug("bus: '%s': add driver %s\n", sp->bus->name, drv->name); 765 766 priv = kzalloc_obj(*priv); 767 if (!priv) { 768 error = -ENOMEM; 769 goto out_put_bus; 770 } 771 klist_init(&priv->klist_devices, NULL, NULL); 772 priv->driver = drv; 773 drv->p = priv; 774 priv->kobj.kset = sp->drivers_kset; 775 error = kobject_init_and_add(&priv->kobj, &driver_ktype, NULL, 776 "%s", drv->name); 777 if (error) 778 goto out_unregister; 779 780 klist_add_tail(&priv->knode_bus, &sp->klist_drivers); 781 if (sp->drivers_autoprobe) { 782 error = driver_attach(drv); 783 if (error) 784 goto out_del_list; 785 } 786 error = module_add_driver(drv->owner, drv); 787 if (error) { 788 printk(KERN_ERR "%s: failed to create module links for %s\n", 789 __func__, drv->name); 790 goto out_detach; 791 } 792 793 error = driver_create_file(drv, &driver_attr_uevent); 794 if (error) { 795 printk(KERN_ERR "%s: uevent attr (%s) failed\n", 796 __func__, drv->name); 797 } 798 error = driver_add_groups(drv, sp->bus->drv_groups); 799 if (error) { 800 /* How the hell do we get out of this pickle? Give up */ 801 printk(KERN_ERR "%s: driver_add_groups(%s) failed\n", 802 __func__, drv->name); 803 } 804 805 if (!drv->suppress_bind_attrs) { 806 error = add_bind_files(drv); 807 if (error) { 808 /* Ditto */ 809 printk(KERN_ERR "%s: add_bind_files(%s) failed\n", 810 __func__, drv->name); 811 } 812 } 813 814 return 0; 815 816 out_detach: 817 driver_detach(drv); 818 out_del_list: 819 klist_del(&priv->knode_bus); 820 out_unregister: 821 kobject_put(&priv->kobj); 822 /* drv->p is freed in driver_release() */ 823 drv->p = NULL; 824 out_put_bus: 825 subsys_put(sp); 826 return error; 827 } 828 829 /** 830 * bus_remove_driver - delete driver from bus's knowledge. 831 * @drv: driver. 832 * 833 * Detach the driver from the devices it controls, and remove 834 * it from its bus's list of drivers. Finally, we drop the reference 835 * to the bus we took in bus_add_driver(). 836 */ 837 void bus_remove_driver(struct device_driver *drv) 838 { 839 struct subsys_private *sp = bus_to_subsys(drv->bus); 840 841 if (!sp) 842 return; 843 844 pr_debug("bus: '%s': remove driver %s\n", sp->bus->name, drv->name); 845 846 if (!drv->suppress_bind_attrs) 847 remove_bind_files(drv); 848 driver_remove_groups(drv, sp->bus->drv_groups); 849 driver_remove_file(drv, &driver_attr_uevent); 850 klist_remove(&drv->p->knode_bus); 851 driver_detach(drv); 852 module_remove_driver(drv); 853 kobject_put(&drv->p->kobj); 854 855 /* 856 * Decrement the reference count twice, once for the bus_to_subsys() 857 * call in the start of this function, and the second one from the 858 * reference increment in bus_add_driver() 859 */ 860 subsys_put(sp); 861 subsys_put(sp); 862 } 863 864 /* Helper for bus_rescan_devices's iter */ 865 static int __must_check bus_rescan_devices_helper(struct device *dev, 866 void *data) 867 { 868 int ret = 0; 869 870 if (!dev->driver) { 871 if (dev->type && dev->type->need_parent_lock) 872 device_lock(dev->parent); 873 ret = device_attach(dev); 874 if (dev->type && dev->type->need_parent_lock) 875 device_unlock(dev->parent); 876 } 877 return ret < 0 ? ret : 0; 878 } 879 880 /** 881 * bus_rescan_devices - rescan devices on the bus for possible drivers 882 * @bus: the bus to scan. 883 * 884 * This function will look for devices on the bus with no driver 885 * attached and rescan it against existing drivers to see if it matches 886 * any by calling device_attach() for the unbound devices. 887 */ 888 int bus_rescan_devices(const struct bus_type *bus) 889 { 890 return bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper); 891 } 892 EXPORT_SYMBOL_GPL(bus_rescan_devices); 893 894 /** 895 * device_reprobe - remove driver for a device and probe for a new driver 896 * @dev: the device to reprobe 897 * 898 * This function detaches the attached driver (if any) for the given 899 * device and restarts the driver probing process. It is intended 900 * to use if probing criteria changed during a devices lifetime and 901 * driver attachment should change accordingly. 902 */ 903 int device_reprobe(struct device *dev) 904 { 905 if (dev->driver) 906 device_driver_detach(dev); 907 return bus_rescan_devices_helper(dev, NULL); 908 } 909 EXPORT_SYMBOL_GPL(device_reprobe); 910 911 static void klist_devices_get(struct klist_node *n) 912 { 913 struct device_private *dev_prv = to_device_private_bus(n); 914 struct device *dev = dev_prv->device; 915 916 get_device(dev); 917 } 918 919 static void klist_devices_put(struct klist_node *n) 920 { 921 struct device_private *dev_prv = to_device_private_bus(n); 922 struct device *dev = dev_prv->device; 923 924 put_device(dev); 925 } 926 927 static ssize_t bus_uevent_store(const struct bus_type *bus, 928 const char *buf, size_t count) 929 { 930 struct subsys_private *sp = bus_to_subsys(bus); 931 int ret; 932 933 if (!sp) 934 return -EINVAL; 935 936 ret = kobject_synth_uevent(&sp->subsys.kobj, buf, count); 937 subsys_put(sp); 938 939 if (ret) 940 return ret; 941 return count; 942 } 943 /* 944 * "open code" the old BUS_ATTR() macro here. We want to use BUS_ATTR_WO() 945 * here, but can not use it as earlier in the file we have 946 * DEVICE_ATTR_WO(uevent), which would cause a clash with the with the store 947 * function name. 948 */ 949 static struct bus_attribute bus_attr_uevent = __ATTR(uevent, 0200, NULL, 950 bus_uevent_store); 951 952 static int bus_register_internal(const struct bus_type *bus, bool use_drivers) 953 { 954 int retval; 955 struct subsys_private *priv; 956 struct kobject *bus_kobj; 957 struct lock_class_key *key; 958 959 priv = kzalloc_obj(struct subsys_private); 960 if (!priv) 961 return -ENOMEM; 962 963 priv->bus = bus; 964 965 BLOCKING_INIT_NOTIFIER_HEAD(&priv->bus_notifier); 966 967 bus_kobj = &priv->subsys.kobj; 968 retval = kobject_set_name(bus_kobj, "%s", bus->name); 969 if (retval) 970 goto out; 971 972 bus_kobj->kset = bus_kset; 973 bus_kobj->ktype = &bus_ktype; 974 priv->drivers_autoprobe = use_drivers; 975 priv->no_drivers = !use_drivers; 976 977 retval = kset_register(&priv->subsys); 978 if (retval) 979 goto out; 980 981 retval = bus_create_file(bus, &bus_attr_uevent); 982 if (retval) 983 goto bus_uevent_fail; 984 985 priv->devices_kset = kset_create_and_add("devices", NULL, bus_kobj); 986 if (!priv->devices_kset) { 987 retval = -ENOMEM; 988 goto bus_devices_fail; 989 } 990 991 /* 992 * Create the drivers kset even if use_drivers is unset to prevent some 993 * older versions of systemd from failing. 994 */ 995 priv->drivers_kset = kset_create_and_add("drivers", NULL, bus_kobj); 996 if (!priv->drivers_kset) { 997 retval = -ENOMEM; 998 goto bus_drivers_fail; 999 } 1000 1001 INIT_LIST_HEAD(&priv->interfaces); 1002 key = &priv->lock_key; 1003 lockdep_register_key(key); 1004 __mutex_init(&priv->mutex, "subsys mutex", key); 1005 klist_init(&priv->klist_devices, klist_devices_get, klist_devices_put); 1006 klist_init(&priv->klist_drivers, NULL, NULL); 1007 1008 if (use_drivers) { 1009 retval = add_probe_files(bus); 1010 if (retval) 1011 goto bus_probe_files_fail; 1012 } 1013 1014 retval = sysfs_create_groups(bus_kobj, bus->bus_groups); 1015 if (retval) 1016 goto bus_groups_fail; 1017 1018 pr_debug("bus: '%s': registered\n", bus->name); 1019 return 0; 1020 1021 bus_groups_fail: 1022 remove_probe_files(bus); 1023 bus_probe_files_fail: 1024 kset_unregister(priv->drivers_kset); 1025 bus_drivers_fail: 1026 kset_unregister(priv->devices_kset); 1027 bus_devices_fail: 1028 bus_remove_file(bus, &bus_attr_uevent); 1029 bus_uevent_fail: 1030 kset_unregister(&priv->subsys); 1031 /* Above kset_unregister() will kfree @priv */ 1032 priv = NULL; 1033 out: 1034 kfree(priv); 1035 return retval; 1036 } 1037 1038 /** 1039 * bus_register - register a driver-core subsystem 1040 * @bus: bus to register 1041 * 1042 * Once we have that, we register the bus with the kobject 1043 * infrastructure, then register the children subsystems it has: 1044 * the devices and drivers that belong to the subsystem. 1045 */ 1046 int bus_register(const struct bus_type *bus) 1047 { 1048 return bus_register_internal(bus, true); 1049 } 1050 EXPORT_SYMBOL_GPL(bus_register); 1051 1052 /** 1053 * companion_bus_register - register a companion bus type 1054 * @bus: companion bus to register 1055 * 1056 * A companion bus is a bus without drivers. Devices that belong to it can be 1057 * bound to other devices as their "companions" and represent interfaces that 1058 * can be used by the drivers of those other devices. They may also be used for 1059 * the enumeration of those other devices. 1060 * 1061 * The ACPI bus is a specific example of a companion bus. 1062 * 1063 * Registering a companion bus is like registering a regular bus except that it 1064 * skips the creation of sysfs interfaces related to drivers for @bus. 1065 */ 1066 int companion_bus_register(const struct bus_type *bus) 1067 { 1068 return bus_register_internal(bus, false); 1069 } 1070 1071 /** 1072 * bus_unregister - remove a bus from the system 1073 * @bus: bus. 1074 * 1075 * Unregister the child subsystems and the bus itself. 1076 * Finally, we call bus_put() to release the refcount 1077 */ 1078 void bus_unregister(const struct bus_type *bus) 1079 { 1080 struct subsys_private *sp = bus_to_subsys(bus); 1081 struct kobject *bus_kobj; 1082 1083 if (!sp) 1084 return; 1085 1086 pr_debug("bus: '%s': unregistering\n", bus->name); 1087 if (sp->dev_root) 1088 device_unregister(sp->dev_root); 1089 1090 bus_kobj = &sp->subsys.kobj; 1091 sysfs_remove_groups(bus_kobj, bus->bus_groups); 1092 remove_probe_files(bus); 1093 bus_remove_file(bus, &bus_attr_uevent); 1094 1095 kset_unregister(sp->drivers_kset); 1096 kset_unregister(sp->devices_kset); 1097 kset_unregister(&sp->subsys); 1098 subsys_put(sp); 1099 } 1100 EXPORT_SYMBOL_GPL(bus_unregister); 1101 1102 int bus_register_notifier(const struct bus_type *bus, struct notifier_block *nb) 1103 { 1104 struct subsys_private *sp = bus_to_subsys(bus); 1105 int retval; 1106 1107 if (!sp) 1108 return -EINVAL; 1109 1110 retval = blocking_notifier_chain_register(&sp->bus_notifier, nb); 1111 subsys_put(sp); 1112 return retval; 1113 } 1114 EXPORT_SYMBOL_GPL(bus_register_notifier); 1115 1116 int bus_unregister_notifier(const struct bus_type *bus, struct notifier_block *nb) 1117 { 1118 struct subsys_private *sp = bus_to_subsys(bus); 1119 int retval; 1120 1121 if (!sp) 1122 return -EINVAL; 1123 retval = blocking_notifier_chain_unregister(&sp->bus_notifier, nb); 1124 subsys_put(sp); 1125 return retval; 1126 } 1127 EXPORT_SYMBOL_GPL(bus_unregister_notifier); 1128 1129 void bus_notify(struct device *dev, enum bus_notifier_event value) 1130 { 1131 struct subsys_private *sp = bus_to_subsys(dev->bus); 1132 1133 if (!sp) 1134 return; 1135 1136 blocking_notifier_call_chain(&sp->bus_notifier, value, dev); 1137 subsys_put(sp); 1138 } 1139 1140 struct kset *bus_get_kset(const struct bus_type *bus) 1141 { 1142 struct subsys_private *sp = bus_to_subsys(bus); 1143 struct kset *kset; 1144 1145 if (!sp) 1146 return NULL; 1147 1148 kset = &sp->subsys; 1149 subsys_put(sp); 1150 1151 return kset; 1152 } 1153 EXPORT_SYMBOL_GPL(bus_get_kset); 1154 1155 /* 1156 * Yes, this forcibly breaks the klist abstraction temporarily. It 1157 * just wants to sort the klist, not change reference counts and 1158 * take/drop locks rapidly in the process. It does all this while 1159 * holding the lock for the list, so objects can't otherwise be 1160 * added/removed while we're swizzling. 1161 */ 1162 static void device_insertion_sort_klist(struct device *a, struct list_head *list, 1163 int (*compare)(const struct device *a, 1164 const struct device *b)) 1165 { 1166 struct klist_node *n; 1167 struct device_private *dev_prv; 1168 struct device *b; 1169 1170 list_for_each_entry(n, list, n_node) { 1171 dev_prv = to_device_private_bus(n); 1172 b = dev_prv->device; 1173 if (compare(a, b) <= 0) { 1174 list_move_tail(&a->p->knode_bus.n_node, 1175 &b->p->knode_bus.n_node); 1176 return; 1177 } 1178 } 1179 list_move_tail(&a->p->knode_bus.n_node, list); 1180 } 1181 1182 void bus_sort_breadthfirst(const struct bus_type *bus, 1183 int (*compare)(const struct device *a, 1184 const struct device *b)) 1185 { 1186 struct subsys_private *sp = bus_to_subsys(bus); 1187 LIST_HEAD(sorted_devices); 1188 struct klist_node *n, *tmp; 1189 struct device_private *dev_prv; 1190 struct device *dev; 1191 struct klist *device_klist; 1192 1193 if (!sp) 1194 return; 1195 device_klist = &sp->klist_devices; 1196 1197 spin_lock(&device_klist->k_lock); 1198 list_for_each_entry_safe(n, tmp, &device_klist->k_list, n_node) { 1199 dev_prv = to_device_private_bus(n); 1200 dev = dev_prv->device; 1201 device_insertion_sort_klist(dev, &sorted_devices, compare); 1202 } 1203 list_splice(&sorted_devices, &device_klist->k_list); 1204 spin_unlock(&device_klist->k_lock); 1205 subsys_put(sp); 1206 } 1207 EXPORT_SYMBOL_GPL(bus_sort_breadthfirst); 1208 1209 struct subsys_dev_iter { 1210 struct klist_iter ki; 1211 const struct device_type *type; 1212 }; 1213 1214 /** 1215 * subsys_dev_iter_init - initialize subsys device iterator 1216 * @iter: subsys iterator to initialize 1217 * @sp: the subsys private (i.e. bus) we wanna iterate over 1218 * @start: the device to start iterating from, if any 1219 * @type: device_type of the devices to iterate over, NULL for all 1220 * 1221 * Initialize subsys iterator @iter such that it iterates over devices 1222 * of @subsys. If @start is set, the list iteration will start there, 1223 * otherwise if it is NULL, the iteration starts at the beginning of 1224 * the list. 1225 */ 1226 static void subsys_dev_iter_init(struct subsys_dev_iter *iter, struct subsys_private *sp, 1227 struct device *start, const struct device_type *type) 1228 { 1229 struct klist_node *start_knode = NULL; 1230 1231 if (start) 1232 start_knode = &start->p->knode_bus; 1233 klist_iter_init_node(&sp->klist_devices, &iter->ki, start_knode); 1234 iter->type = type; 1235 } 1236 1237 /** 1238 * subsys_dev_iter_next - iterate to the next device 1239 * @iter: subsys iterator to proceed 1240 * 1241 * Proceed @iter to the next device and return it. Returns NULL if 1242 * iteration is complete. 1243 * 1244 * The returned device is referenced and won't be released till 1245 * iterator is proceed to the next device or exited. The caller is 1246 * free to do whatever it wants to do with the device including 1247 * calling back into subsys code. 1248 */ 1249 static struct device *subsys_dev_iter_next(struct subsys_dev_iter *iter) 1250 { 1251 struct klist_node *knode; 1252 struct device *dev; 1253 1254 for (;;) { 1255 knode = klist_next(&iter->ki); 1256 if (!knode) 1257 return NULL; 1258 dev = to_device_private_bus(knode)->device; 1259 if (!iter->type || iter->type == dev->type) 1260 return dev; 1261 } 1262 } 1263 1264 /** 1265 * subsys_dev_iter_exit - finish iteration 1266 * @iter: subsys iterator to finish 1267 * 1268 * Finish an iteration. Always call this function after iteration is 1269 * complete whether the iteration ran till the end or not. 1270 */ 1271 static void subsys_dev_iter_exit(struct subsys_dev_iter *iter) 1272 { 1273 klist_iter_exit(&iter->ki); 1274 } 1275 1276 int subsys_interface_register(struct subsys_interface *sif) 1277 { 1278 struct subsys_private *sp; 1279 struct subsys_dev_iter iter; 1280 struct device *dev; 1281 1282 if (!sif || !sif->subsys) 1283 return -ENODEV; 1284 1285 sp = bus_to_subsys(sif->subsys); 1286 if (!sp) 1287 return -EINVAL; 1288 1289 /* 1290 * Reference in sp is now incremented and will be dropped when 1291 * the interface is removed from the bus 1292 */ 1293 1294 mutex_lock(&sp->mutex); 1295 list_add_tail(&sif->node, &sp->interfaces); 1296 if (sif->add_dev) { 1297 subsys_dev_iter_init(&iter, sp, NULL, NULL); 1298 while ((dev = subsys_dev_iter_next(&iter))) 1299 sif->add_dev(dev, sif); 1300 subsys_dev_iter_exit(&iter); 1301 } 1302 mutex_unlock(&sp->mutex); 1303 1304 return 0; 1305 } 1306 EXPORT_SYMBOL_GPL(subsys_interface_register); 1307 1308 void subsys_interface_unregister(struct subsys_interface *sif) 1309 { 1310 struct subsys_private *sp; 1311 struct subsys_dev_iter iter; 1312 struct device *dev; 1313 1314 if (!sif || !sif->subsys) 1315 return; 1316 1317 sp = bus_to_subsys(sif->subsys); 1318 if (!sp) 1319 return; 1320 1321 mutex_lock(&sp->mutex); 1322 list_del_init(&sif->node); 1323 if (sif->remove_dev) { 1324 subsys_dev_iter_init(&iter, sp, NULL, NULL); 1325 while ((dev = subsys_dev_iter_next(&iter))) 1326 sif->remove_dev(dev, sif); 1327 subsys_dev_iter_exit(&iter); 1328 } 1329 mutex_unlock(&sp->mutex); 1330 1331 /* 1332 * Decrement the reference count twice, once for the bus_to_subsys() 1333 * call in the start of this function, and the second one from the 1334 * reference increment in subsys_interface_register() 1335 */ 1336 subsys_put(sp); 1337 subsys_put(sp); 1338 } 1339 EXPORT_SYMBOL_GPL(subsys_interface_unregister); 1340 1341 static void system_root_device_release(struct device *dev) 1342 { 1343 kfree(dev); 1344 } 1345 1346 static int subsys_register(const struct bus_type *subsys, 1347 const struct attribute_group **groups, 1348 struct kobject *parent_of_root) 1349 { 1350 struct subsys_private *sp; 1351 struct device *dev; 1352 int err; 1353 1354 err = bus_register(subsys); 1355 if (err < 0) 1356 return err; 1357 1358 sp = bus_to_subsys(subsys); 1359 if (!sp) { 1360 err = -EINVAL; 1361 goto err_sp; 1362 } 1363 1364 dev = kzalloc_obj(struct device); 1365 if (!dev) { 1366 err = -ENOMEM; 1367 goto err_dev; 1368 } 1369 1370 err = dev_set_name(dev, "%s", subsys->name); 1371 if (err < 0) 1372 goto err_name; 1373 1374 dev->kobj.parent = parent_of_root; 1375 dev->groups = groups; 1376 dev->release = system_root_device_release; 1377 1378 err = device_register(dev); 1379 if (err < 0) 1380 goto err_dev_reg; 1381 1382 sp->dev_root = dev; 1383 subsys_put(sp); 1384 return 0; 1385 1386 err_dev_reg: 1387 put_device(dev); 1388 dev = NULL; 1389 err_name: 1390 kfree(dev); 1391 err_dev: 1392 subsys_put(sp); 1393 err_sp: 1394 bus_unregister(subsys); 1395 return err; 1396 } 1397 1398 /** 1399 * subsys_system_register - register a subsystem at /sys/devices/system/ 1400 * @subsys: system subsystem 1401 * @groups: default attributes for the root device 1402 * 1403 * All 'system' subsystems have a /sys/devices/system/<name> root device 1404 * with the name of the subsystem. The root device can carry subsystem- 1405 * wide attributes. All registered devices are below this single root 1406 * device and are named after the subsystem with a simple enumeration 1407 * number appended. The registered devices are not explicitly named; 1408 * only 'id' in the device needs to be set. 1409 * 1410 * Do not use this interface for anything new, it exists for compatibility 1411 * with bad ideas only. New subsystems should use plain subsystems; and 1412 * add the subsystem-wide attributes should be added to the subsystem 1413 * directory itself and not some create fake root-device placed in 1414 * /sys/devices/system/<name>. 1415 */ 1416 int subsys_system_register(const struct bus_type *subsys, 1417 const struct attribute_group **groups) 1418 { 1419 return subsys_register(subsys, groups, &system_kset->kobj); 1420 } 1421 EXPORT_SYMBOL_GPL(subsys_system_register); 1422 1423 /** 1424 * subsys_virtual_register - register a subsystem at /sys/devices/virtual/ 1425 * @subsys: virtual subsystem 1426 * @groups: default attributes for the root device 1427 * 1428 * All 'virtual' subsystems have a /sys/devices/system/<name> root device 1429 * with the name of the subsystem. The root device can carry subsystem-wide 1430 * attributes. All registered devices are below this single root device. 1431 * There's no restriction on device naming. This is for kernel software 1432 * constructs which need sysfs interface. 1433 */ 1434 int subsys_virtual_register(const struct bus_type *subsys, 1435 const struct attribute_group **groups) 1436 { 1437 struct kobject *virtual_dir; 1438 1439 virtual_dir = virtual_device_parent(); 1440 if (!virtual_dir) 1441 return -ENOMEM; 1442 1443 return subsys_register(subsys, groups, virtual_dir); 1444 } 1445 EXPORT_SYMBOL_GPL(subsys_virtual_register); 1446 1447 /** 1448 * driver_find - locate driver on a bus by its name. 1449 * @name: name of the driver. 1450 * @bus: bus to scan for the driver. 1451 * 1452 * Call kset_find_obj() to iterate over list of drivers on 1453 * a bus to find driver by name. Return driver if found. 1454 * 1455 * This routine provides no locking to prevent the driver it returns 1456 * from being unregistered or unloaded while the caller is using it. 1457 * The caller is responsible for preventing this. 1458 */ 1459 struct device_driver *driver_find(const char *name, const struct bus_type *bus) 1460 { 1461 struct subsys_private *sp = bus_to_subsys(bus); 1462 struct kobject *k; 1463 struct driver_private *priv; 1464 1465 if (!sp) 1466 return NULL; 1467 1468 if (sp->no_drivers) { 1469 subsys_put(sp); 1470 return NULL; 1471 } 1472 1473 k = kset_find_obj(sp->drivers_kset, name); 1474 subsys_put(sp); 1475 if (!k) 1476 return NULL; 1477 1478 priv = to_driver(k); 1479 1480 /* Drop reference added by kset_find_obj() */ 1481 kobject_put(k); 1482 return priv->driver; 1483 } 1484 EXPORT_SYMBOL_GPL(driver_find); 1485 1486 /* 1487 * Warning, the value could go to "removed" instantly after calling this function, so be very 1488 * careful when calling it... 1489 */ 1490 bool bus_is_registered(const struct bus_type *bus) 1491 { 1492 struct subsys_private *sp = bus_to_subsys(bus); 1493 bool is_initialized = false; 1494 1495 if (sp) { 1496 is_initialized = true; 1497 subsys_put(sp); 1498 } 1499 return is_initialized; 1500 } 1501 1502 /** 1503 * bus_get_dev_root - return a pointer to the "device root" of a bus 1504 * @bus: bus to return the device root of. 1505 * 1506 * If a bus has a "device root" structure, return it, WITH THE REFERENCE 1507 * COUNT INCREMENTED. 1508 * 1509 * Note, when finished with the device, a call to put_device() is required. 1510 * 1511 * If the device root is not present (or bus is not a valid pointer), NULL 1512 * will be returned. 1513 */ 1514 struct device *bus_get_dev_root(const struct bus_type *bus) 1515 { 1516 struct subsys_private *sp = bus_to_subsys(bus); 1517 struct device *dev_root; 1518 1519 if (!sp) 1520 return NULL; 1521 1522 dev_root = get_device(sp->dev_root); 1523 subsys_put(sp); 1524 return dev_root; 1525 } 1526 EXPORT_SYMBOL_GPL(bus_get_dev_root); 1527 1528 int __init buses_init(void) 1529 { 1530 bus_kset = kset_create_and_add("bus", &bus_uevent_ops, NULL); 1531 if (!bus_kset) 1532 return -ENOMEM; 1533 1534 system_kset = kset_create_and_add("system", NULL, &devices_kset->kobj); 1535 if (!system_kset) { 1536 /* Do error handling here as devices_init() do */ 1537 kset_unregister(bus_kset); 1538 bus_kset = NULL; 1539 pr_err("%s: failed to create and add kset 'bus'\n", __func__); 1540 return -ENOMEM; 1541 } 1542 1543 return 0; 1544 } 1545