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