1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * drivers/base/core.c - core driver model code (device registration, etc) 4 * 5 * Copyright (c) 2002-3 Patrick Mochel 6 * Copyright (c) 2002-3 Open Source Development Labs 7 * Copyright (c) 2006 Greg Kroah-Hartman <gregkh@suse.de> 8 * Copyright (c) 2006 Novell, Inc. 9 */ 10 11 #include <linux/acpi.h> 12 #include <linux/cpufreq.h> 13 #include <linux/device.h> 14 #include <linux/err.h> 15 #include <linux/fwnode.h> 16 #include <linux/init.h> 17 #include <linux/module.h> 18 #include <linux/slab.h> 19 #include <linux/string.h> 20 #include <linux/kdev_t.h> 21 #include <linux/notifier.h> 22 #include <linux/of.h> 23 #include <linux/of_device.h> 24 #include <linux/genhd.h> 25 #include <linux/mutex.h> 26 #include <linux/pm_runtime.h> 27 #include <linux/netdevice.h> 28 #include <linux/sched/signal.h> 29 #include <linux/sched/mm.h> 30 #include <linux/sysfs.h> 31 32 #include "base.h" 33 #include "power/power.h" 34 35 #ifdef CONFIG_SYSFS_DEPRECATED 36 #ifdef CONFIG_SYSFS_DEPRECATED_V2 37 long sysfs_deprecated = 1; 38 #else 39 long sysfs_deprecated = 0; 40 #endif 41 static int __init sysfs_deprecated_setup(char *arg) 42 { 43 return kstrtol(arg, 10, &sysfs_deprecated); 44 } 45 early_param("sysfs.deprecated", sysfs_deprecated_setup); 46 #endif 47 48 /* Device links support. */ 49 static LIST_HEAD(wait_for_suppliers); 50 static DEFINE_MUTEX(wfs_lock); 51 static LIST_HEAD(deferred_sync); 52 static unsigned int defer_sync_state_count = 1; 53 static unsigned int defer_fw_devlink_count; 54 static LIST_HEAD(deferred_fw_devlink); 55 static DEFINE_MUTEX(defer_fw_devlink_lock); 56 static bool fw_devlink_is_permissive(void); 57 58 #ifdef CONFIG_SRCU 59 static DEFINE_MUTEX(device_links_lock); 60 DEFINE_STATIC_SRCU(device_links_srcu); 61 62 static inline void device_links_write_lock(void) 63 { 64 mutex_lock(&device_links_lock); 65 } 66 67 static inline void device_links_write_unlock(void) 68 { 69 mutex_unlock(&device_links_lock); 70 } 71 72 int device_links_read_lock(void) __acquires(&device_links_srcu) 73 { 74 return srcu_read_lock(&device_links_srcu); 75 } 76 77 void device_links_read_unlock(int idx) __releases(&device_links_srcu) 78 { 79 srcu_read_unlock(&device_links_srcu, idx); 80 } 81 82 int device_links_read_lock_held(void) 83 { 84 return srcu_read_lock_held(&device_links_srcu); 85 } 86 #else /* !CONFIG_SRCU */ 87 static DECLARE_RWSEM(device_links_lock); 88 89 static inline void device_links_write_lock(void) 90 { 91 down_write(&device_links_lock); 92 } 93 94 static inline void device_links_write_unlock(void) 95 { 96 up_write(&device_links_lock); 97 } 98 99 int device_links_read_lock(void) 100 { 101 down_read(&device_links_lock); 102 return 0; 103 } 104 105 void device_links_read_unlock(int not_used) 106 { 107 up_read(&device_links_lock); 108 } 109 110 #ifdef CONFIG_DEBUG_LOCK_ALLOC 111 int device_links_read_lock_held(void) 112 { 113 return lockdep_is_held(&device_links_lock); 114 } 115 #endif 116 #endif /* !CONFIG_SRCU */ 117 118 /** 119 * device_is_dependent - Check if one device depends on another one 120 * @dev: Device to check dependencies for. 121 * @target: Device to check against. 122 * 123 * Check if @target depends on @dev or any device dependent on it (its child or 124 * its consumer etc). Return 1 if that is the case or 0 otherwise. 125 */ 126 int device_is_dependent(struct device *dev, void *target) 127 { 128 struct device_link *link; 129 int ret; 130 131 if (dev == target) 132 return 1; 133 134 ret = device_for_each_child(dev, target, device_is_dependent); 135 if (ret) 136 return ret; 137 138 list_for_each_entry(link, &dev->links.consumers, s_node) { 139 if (link->flags == (DL_FLAG_SYNC_STATE_ONLY | DL_FLAG_MANAGED)) 140 continue; 141 142 if (link->consumer == target) 143 return 1; 144 145 ret = device_is_dependent(link->consumer, target); 146 if (ret) 147 break; 148 } 149 return ret; 150 } 151 152 static void device_link_init_status(struct device_link *link, 153 struct device *consumer, 154 struct device *supplier) 155 { 156 switch (supplier->links.status) { 157 case DL_DEV_PROBING: 158 switch (consumer->links.status) { 159 case DL_DEV_PROBING: 160 /* 161 * A consumer driver can create a link to a supplier 162 * that has not completed its probing yet as long as it 163 * knows that the supplier is already functional (for 164 * example, it has just acquired some resources from the 165 * supplier). 166 */ 167 link->status = DL_STATE_CONSUMER_PROBE; 168 break; 169 default: 170 link->status = DL_STATE_DORMANT; 171 break; 172 } 173 break; 174 case DL_DEV_DRIVER_BOUND: 175 switch (consumer->links.status) { 176 case DL_DEV_PROBING: 177 link->status = DL_STATE_CONSUMER_PROBE; 178 break; 179 case DL_DEV_DRIVER_BOUND: 180 link->status = DL_STATE_ACTIVE; 181 break; 182 default: 183 link->status = DL_STATE_AVAILABLE; 184 break; 185 } 186 break; 187 case DL_DEV_UNBINDING: 188 link->status = DL_STATE_SUPPLIER_UNBIND; 189 break; 190 default: 191 link->status = DL_STATE_DORMANT; 192 break; 193 } 194 } 195 196 static int device_reorder_to_tail(struct device *dev, void *not_used) 197 { 198 struct device_link *link; 199 200 /* 201 * Devices that have not been registered yet will be put to the ends 202 * of the lists during the registration, so skip them here. 203 */ 204 if (device_is_registered(dev)) 205 devices_kset_move_last(dev); 206 207 if (device_pm_initialized(dev)) 208 device_pm_move_last(dev); 209 210 device_for_each_child(dev, NULL, device_reorder_to_tail); 211 list_for_each_entry(link, &dev->links.consumers, s_node) { 212 if (link->flags == (DL_FLAG_SYNC_STATE_ONLY | DL_FLAG_MANAGED)) 213 continue; 214 device_reorder_to_tail(link->consumer, NULL); 215 } 216 217 return 0; 218 } 219 220 /** 221 * device_pm_move_to_tail - Move set of devices to the end of device lists 222 * @dev: Device to move 223 * 224 * This is a device_reorder_to_tail() wrapper taking the requisite locks. 225 * 226 * It moves the @dev along with all of its children and all of its consumers 227 * to the ends of the device_kset and dpm_list, recursively. 228 */ 229 void device_pm_move_to_tail(struct device *dev) 230 { 231 int idx; 232 233 idx = device_links_read_lock(); 234 device_pm_lock(); 235 device_reorder_to_tail(dev, NULL); 236 device_pm_unlock(); 237 device_links_read_unlock(idx); 238 } 239 240 #define to_devlink(dev) container_of((dev), struct device_link, link_dev) 241 242 static ssize_t status_show(struct device *dev, 243 struct device_attribute *attr, char *buf) 244 { 245 const char *output; 246 247 switch (to_devlink(dev)->status) { 248 case DL_STATE_NONE: 249 output = "not tracked"; 250 break; 251 case DL_STATE_DORMANT: 252 output = "dormant"; 253 break; 254 case DL_STATE_AVAILABLE: 255 output = "available"; 256 break; 257 case DL_STATE_CONSUMER_PROBE: 258 output = "consumer probing"; 259 break; 260 case DL_STATE_ACTIVE: 261 output = "active"; 262 break; 263 case DL_STATE_SUPPLIER_UNBIND: 264 output = "supplier unbinding"; 265 break; 266 default: 267 output = "unknown"; 268 break; 269 } 270 271 return sysfs_emit(buf, "%s\n", output); 272 } 273 static DEVICE_ATTR_RO(status); 274 275 static ssize_t auto_remove_on_show(struct device *dev, 276 struct device_attribute *attr, char *buf) 277 { 278 struct device_link *link = to_devlink(dev); 279 const char *output; 280 281 if (link->flags & DL_FLAG_AUTOREMOVE_SUPPLIER) 282 output = "supplier unbind"; 283 else if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER) 284 output = "consumer unbind"; 285 else 286 output = "never"; 287 288 return sysfs_emit(buf, "%s\n", output); 289 } 290 static DEVICE_ATTR_RO(auto_remove_on); 291 292 static ssize_t runtime_pm_show(struct device *dev, 293 struct device_attribute *attr, char *buf) 294 { 295 struct device_link *link = to_devlink(dev); 296 297 return sysfs_emit(buf, "%d\n", !!(link->flags & DL_FLAG_PM_RUNTIME)); 298 } 299 static DEVICE_ATTR_RO(runtime_pm); 300 301 static ssize_t sync_state_only_show(struct device *dev, 302 struct device_attribute *attr, char *buf) 303 { 304 struct device_link *link = to_devlink(dev); 305 306 return sysfs_emit(buf, "%d\n", 307 !!(link->flags & DL_FLAG_SYNC_STATE_ONLY)); 308 } 309 static DEVICE_ATTR_RO(sync_state_only); 310 311 static struct attribute *devlink_attrs[] = { 312 &dev_attr_status.attr, 313 &dev_attr_auto_remove_on.attr, 314 &dev_attr_runtime_pm.attr, 315 &dev_attr_sync_state_only.attr, 316 NULL, 317 }; 318 ATTRIBUTE_GROUPS(devlink); 319 320 static void device_link_free(struct device_link *link) 321 { 322 while (refcount_dec_not_one(&link->rpm_active)) 323 pm_runtime_put(link->supplier); 324 325 put_device(link->consumer); 326 put_device(link->supplier); 327 kfree(link); 328 } 329 330 #ifdef CONFIG_SRCU 331 static void __device_link_free_srcu(struct rcu_head *rhead) 332 { 333 device_link_free(container_of(rhead, struct device_link, rcu_head)); 334 } 335 336 static void devlink_dev_release(struct device *dev) 337 { 338 struct device_link *link = to_devlink(dev); 339 340 call_srcu(&device_links_srcu, &link->rcu_head, __device_link_free_srcu); 341 } 342 #else 343 static void devlink_dev_release(struct device *dev) 344 { 345 device_link_free(to_devlink(dev)); 346 } 347 #endif 348 349 static struct class devlink_class = { 350 .name = "devlink", 351 .owner = THIS_MODULE, 352 .dev_groups = devlink_groups, 353 .dev_release = devlink_dev_release, 354 }; 355 356 static int devlink_add_symlinks(struct device *dev, 357 struct class_interface *class_intf) 358 { 359 int ret; 360 size_t len; 361 struct device_link *link = to_devlink(dev); 362 struct device *sup = link->supplier; 363 struct device *con = link->consumer; 364 char *buf; 365 366 len = max(strlen(dev_name(sup)), strlen(dev_name(con))); 367 len += strlen("supplier:") + 1; 368 buf = kzalloc(len, GFP_KERNEL); 369 if (!buf) 370 return -ENOMEM; 371 372 ret = sysfs_create_link(&link->link_dev.kobj, &sup->kobj, "supplier"); 373 if (ret) 374 goto out; 375 376 ret = sysfs_create_link(&link->link_dev.kobj, &con->kobj, "consumer"); 377 if (ret) 378 goto err_con; 379 380 snprintf(buf, len, "consumer:%s", dev_name(con)); 381 ret = sysfs_create_link(&sup->kobj, &link->link_dev.kobj, buf); 382 if (ret) 383 goto err_con_dev; 384 385 snprintf(buf, len, "supplier:%s", dev_name(sup)); 386 ret = sysfs_create_link(&con->kobj, &link->link_dev.kobj, buf); 387 if (ret) 388 goto err_sup_dev; 389 390 goto out; 391 392 err_sup_dev: 393 snprintf(buf, len, "consumer:%s", dev_name(con)); 394 sysfs_remove_link(&sup->kobj, buf); 395 err_con_dev: 396 sysfs_remove_link(&link->link_dev.kobj, "consumer"); 397 err_con: 398 sysfs_remove_link(&link->link_dev.kobj, "supplier"); 399 out: 400 kfree(buf); 401 return ret; 402 } 403 404 static void devlink_remove_symlinks(struct device *dev, 405 struct class_interface *class_intf) 406 { 407 struct device_link *link = to_devlink(dev); 408 size_t len; 409 struct device *sup = link->supplier; 410 struct device *con = link->consumer; 411 char *buf; 412 413 sysfs_remove_link(&link->link_dev.kobj, "consumer"); 414 sysfs_remove_link(&link->link_dev.kobj, "supplier"); 415 416 len = max(strlen(dev_name(sup)), strlen(dev_name(con))); 417 len += strlen("supplier:") + 1; 418 buf = kzalloc(len, GFP_KERNEL); 419 if (!buf) { 420 WARN(1, "Unable to properly free device link symlinks!\n"); 421 return; 422 } 423 424 snprintf(buf, len, "supplier:%s", dev_name(sup)); 425 sysfs_remove_link(&con->kobj, buf); 426 snprintf(buf, len, "consumer:%s", dev_name(con)); 427 sysfs_remove_link(&sup->kobj, buf); 428 kfree(buf); 429 } 430 431 static struct class_interface devlink_class_intf = { 432 .class = &devlink_class, 433 .add_dev = devlink_add_symlinks, 434 .remove_dev = devlink_remove_symlinks, 435 }; 436 437 static int __init devlink_class_init(void) 438 { 439 int ret; 440 441 ret = class_register(&devlink_class); 442 if (ret) 443 return ret; 444 445 ret = class_interface_register(&devlink_class_intf); 446 if (ret) 447 class_unregister(&devlink_class); 448 449 return ret; 450 } 451 postcore_initcall(devlink_class_init); 452 453 #define DL_MANAGED_LINK_FLAGS (DL_FLAG_AUTOREMOVE_CONSUMER | \ 454 DL_FLAG_AUTOREMOVE_SUPPLIER | \ 455 DL_FLAG_AUTOPROBE_CONSUMER | \ 456 DL_FLAG_SYNC_STATE_ONLY) 457 458 #define DL_ADD_VALID_FLAGS (DL_MANAGED_LINK_FLAGS | DL_FLAG_STATELESS | \ 459 DL_FLAG_PM_RUNTIME | DL_FLAG_RPM_ACTIVE) 460 461 /** 462 * device_link_add - Create a link between two devices. 463 * @consumer: Consumer end of the link. 464 * @supplier: Supplier end of the link. 465 * @flags: Link flags. 466 * 467 * The caller is responsible for the proper synchronization of the link creation 468 * with runtime PM. First, setting the DL_FLAG_PM_RUNTIME flag will cause the 469 * runtime PM framework to take the link into account. Second, if the 470 * DL_FLAG_RPM_ACTIVE flag is set in addition to it, the supplier devices will 471 * be forced into the active metastate and reference-counted upon the creation 472 * of the link. If DL_FLAG_PM_RUNTIME is not set, DL_FLAG_RPM_ACTIVE will be 473 * ignored. 474 * 475 * If DL_FLAG_STATELESS is set in @flags, the caller of this function is 476 * expected to release the link returned by it directly with the help of either 477 * device_link_del() or device_link_remove(). 478 * 479 * If that flag is not set, however, the caller of this function is handing the 480 * management of the link over to the driver core entirely and its return value 481 * can only be used to check whether or not the link is present. In that case, 482 * the DL_FLAG_AUTOREMOVE_CONSUMER and DL_FLAG_AUTOREMOVE_SUPPLIER device link 483 * flags can be used to indicate to the driver core when the link can be safely 484 * deleted. Namely, setting one of them in @flags indicates to the driver core 485 * that the link is not going to be used (by the given caller of this function) 486 * after unbinding the consumer or supplier driver, respectively, from its 487 * device, so the link can be deleted at that point. If none of them is set, 488 * the link will be maintained until one of the devices pointed to by it (either 489 * the consumer or the supplier) is unregistered. 490 * 491 * Also, if DL_FLAG_STATELESS, DL_FLAG_AUTOREMOVE_CONSUMER and 492 * DL_FLAG_AUTOREMOVE_SUPPLIER are not set in @flags (that is, a persistent 493 * managed device link is being added), the DL_FLAG_AUTOPROBE_CONSUMER flag can 494 * be used to request the driver core to automaticall probe for a consmer 495 * driver after successfully binding a driver to the supplier device. 496 * 497 * The combination of DL_FLAG_STATELESS and one of DL_FLAG_AUTOREMOVE_CONSUMER, 498 * DL_FLAG_AUTOREMOVE_SUPPLIER, or DL_FLAG_AUTOPROBE_CONSUMER set in @flags at 499 * the same time is invalid and will cause NULL to be returned upfront. 500 * However, if a device link between the given @consumer and @supplier pair 501 * exists already when this function is called for them, the existing link will 502 * be returned regardless of its current type and status (the link's flags may 503 * be modified then). The caller of this function is then expected to treat 504 * the link as though it has just been created, so (in particular) if 505 * DL_FLAG_STATELESS was passed in @flags, the link needs to be released 506 * explicitly when not needed any more (as stated above). 507 * 508 * A side effect of the link creation is re-ordering of dpm_list and the 509 * devices_kset list by moving the consumer device and all devices depending 510 * on it to the ends of these lists (that does not happen to devices that have 511 * not been registered when this function is called). 512 * 513 * The supplier device is required to be registered when this function is called 514 * and NULL will be returned if that is not the case. The consumer device need 515 * not be registered, however. 516 */ 517 struct device_link *device_link_add(struct device *consumer, 518 struct device *supplier, u32 flags) 519 { 520 struct device_link *link; 521 522 if (!consumer || !supplier || flags & ~DL_ADD_VALID_FLAGS || 523 (flags & DL_FLAG_STATELESS && flags & DL_MANAGED_LINK_FLAGS) || 524 (flags & DL_FLAG_SYNC_STATE_ONLY && 525 flags != DL_FLAG_SYNC_STATE_ONLY) || 526 (flags & DL_FLAG_AUTOPROBE_CONSUMER && 527 flags & (DL_FLAG_AUTOREMOVE_CONSUMER | 528 DL_FLAG_AUTOREMOVE_SUPPLIER))) 529 return NULL; 530 531 if (flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) { 532 if (pm_runtime_get_sync(supplier) < 0) { 533 pm_runtime_put_noidle(supplier); 534 return NULL; 535 } 536 } 537 538 if (!(flags & DL_FLAG_STATELESS)) 539 flags |= DL_FLAG_MANAGED; 540 541 device_links_write_lock(); 542 device_pm_lock(); 543 544 /* 545 * If the supplier has not been fully registered yet or there is a 546 * reverse (non-SYNC_STATE_ONLY) dependency between the consumer and 547 * the supplier already in the graph, return NULL. If the link is a 548 * SYNC_STATE_ONLY link, we don't check for reverse dependencies 549 * because it only affects sync_state() callbacks. 550 */ 551 if (!device_pm_initialized(supplier) 552 || (!(flags & DL_FLAG_SYNC_STATE_ONLY) && 553 device_is_dependent(consumer, supplier))) { 554 link = NULL; 555 goto out; 556 } 557 558 /* 559 * DL_FLAG_AUTOREMOVE_SUPPLIER indicates that the link will be needed 560 * longer than for DL_FLAG_AUTOREMOVE_CONSUMER and setting them both 561 * together doesn't make sense, so prefer DL_FLAG_AUTOREMOVE_SUPPLIER. 562 */ 563 if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER) 564 flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER; 565 566 list_for_each_entry(link, &supplier->links.consumers, s_node) { 567 if (link->consumer != consumer) 568 continue; 569 570 if (flags & DL_FLAG_PM_RUNTIME) { 571 if (!(link->flags & DL_FLAG_PM_RUNTIME)) { 572 pm_runtime_new_link(consumer); 573 link->flags |= DL_FLAG_PM_RUNTIME; 574 } 575 if (flags & DL_FLAG_RPM_ACTIVE) 576 refcount_inc(&link->rpm_active); 577 } 578 579 if (flags & DL_FLAG_STATELESS) { 580 kref_get(&link->kref); 581 if (link->flags & DL_FLAG_SYNC_STATE_ONLY && 582 !(link->flags & DL_FLAG_STATELESS)) { 583 link->flags |= DL_FLAG_STATELESS; 584 goto reorder; 585 } else { 586 link->flags |= DL_FLAG_STATELESS; 587 goto out; 588 } 589 } 590 591 /* 592 * If the life time of the link following from the new flags is 593 * longer than indicated by the flags of the existing link, 594 * update the existing link to stay around longer. 595 */ 596 if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER) { 597 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER) { 598 link->flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER; 599 link->flags |= DL_FLAG_AUTOREMOVE_SUPPLIER; 600 } 601 } else if (!(flags & DL_FLAG_AUTOREMOVE_CONSUMER)) { 602 link->flags &= ~(DL_FLAG_AUTOREMOVE_CONSUMER | 603 DL_FLAG_AUTOREMOVE_SUPPLIER); 604 } 605 if (!(link->flags & DL_FLAG_MANAGED)) { 606 kref_get(&link->kref); 607 link->flags |= DL_FLAG_MANAGED; 608 device_link_init_status(link, consumer, supplier); 609 } 610 if (link->flags & DL_FLAG_SYNC_STATE_ONLY && 611 !(flags & DL_FLAG_SYNC_STATE_ONLY)) { 612 link->flags &= ~DL_FLAG_SYNC_STATE_ONLY; 613 goto reorder; 614 } 615 616 goto out; 617 } 618 619 link = kzalloc(sizeof(*link), GFP_KERNEL); 620 if (!link) 621 goto out; 622 623 refcount_set(&link->rpm_active, 1); 624 625 get_device(supplier); 626 link->supplier = supplier; 627 INIT_LIST_HEAD(&link->s_node); 628 get_device(consumer); 629 link->consumer = consumer; 630 INIT_LIST_HEAD(&link->c_node); 631 link->flags = flags; 632 kref_init(&link->kref); 633 634 link->link_dev.class = &devlink_class; 635 device_set_pm_not_required(&link->link_dev); 636 dev_set_name(&link->link_dev, "%s--%s", 637 dev_name(supplier), dev_name(consumer)); 638 if (device_register(&link->link_dev)) { 639 put_device(consumer); 640 put_device(supplier); 641 kfree(link); 642 link = NULL; 643 goto out; 644 } 645 646 if (flags & DL_FLAG_PM_RUNTIME) { 647 if (flags & DL_FLAG_RPM_ACTIVE) 648 refcount_inc(&link->rpm_active); 649 650 pm_runtime_new_link(consumer); 651 } 652 653 /* Determine the initial link state. */ 654 if (flags & DL_FLAG_STATELESS) 655 link->status = DL_STATE_NONE; 656 else 657 device_link_init_status(link, consumer, supplier); 658 659 /* 660 * Some callers expect the link creation during consumer driver probe to 661 * resume the supplier even without DL_FLAG_RPM_ACTIVE. 662 */ 663 if (link->status == DL_STATE_CONSUMER_PROBE && 664 flags & DL_FLAG_PM_RUNTIME) 665 pm_runtime_resume(supplier); 666 667 list_add_tail_rcu(&link->s_node, &supplier->links.consumers); 668 list_add_tail_rcu(&link->c_node, &consumer->links.suppliers); 669 670 if (flags & DL_FLAG_SYNC_STATE_ONLY) { 671 dev_dbg(consumer, 672 "Linked as a sync state only consumer to %s\n", 673 dev_name(supplier)); 674 goto out; 675 } 676 677 reorder: 678 /* 679 * Move the consumer and all of the devices depending on it to the end 680 * of dpm_list and the devices_kset list. 681 * 682 * It is necessary to hold dpm_list locked throughout all that or else 683 * we may end up suspending with a wrong ordering of it. 684 */ 685 device_reorder_to_tail(consumer, NULL); 686 687 dev_dbg(consumer, "Linked as a consumer to %s\n", dev_name(supplier)); 688 689 out: 690 device_pm_unlock(); 691 device_links_write_unlock(); 692 693 if ((flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) && !link) 694 pm_runtime_put(supplier); 695 696 return link; 697 } 698 EXPORT_SYMBOL_GPL(device_link_add); 699 700 /** 701 * device_link_wait_for_supplier - Add device to wait_for_suppliers list 702 * @consumer: Consumer device 703 * 704 * Marks the @consumer device as waiting for suppliers to become available by 705 * adding it to the wait_for_suppliers list. The consumer device will never be 706 * probed until it's removed from the wait_for_suppliers list. 707 * 708 * The caller is responsible for adding the links to the supplier devices once 709 * they are available and removing the @consumer device from the 710 * wait_for_suppliers list once links to all the suppliers have been created. 711 * 712 * This function is NOT meant to be called from the probe function of the 713 * consumer but rather from code that creates/adds the consumer device. 714 */ 715 static void device_link_wait_for_supplier(struct device *consumer, 716 bool need_for_probe) 717 { 718 mutex_lock(&wfs_lock); 719 list_add_tail(&consumer->links.needs_suppliers, &wait_for_suppliers); 720 consumer->links.need_for_probe = need_for_probe; 721 mutex_unlock(&wfs_lock); 722 } 723 724 static void device_link_wait_for_mandatory_supplier(struct device *consumer) 725 { 726 device_link_wait_for_supplier(consumer, true); 727 } 728 729 static void device_link_wait_for_optional_supplier(struct device *consumer) 730 { 731 device_link_wait_for_supplier(consumer, false); 732 } 733 734 /** 735 * device_link_add_missing_supplier_links - Add links from consumer devices to 736 * supplier devices, leaving any 737 * consumer with inactive suppliers on 738 * the wait_for_suppliers list 739 * 740 * Loops through all consumers waiting on suppliers and tries to add all their 741 * supplier links. If that succeeds, the consumer device is removed from 742 * wait_for_suppliers list. Otherwise, they are left in the wait_for_suppliers 743 * list. Devices left on the wait_for_suppliers list will not be probed. 744 * 745 * The fwnode add_links callback is expected to return 0 if it has found and 746 * added all the supplier links for the consumer device. It should return an 747 * error if it isn't able to do so. 748 * 749 * The caller of device_link_wait_for_supplier() is expected to call this once 750 * it's aware of potential suppliers becoming available. 751 */ 752 static void device_link_add_missing_supplier_links(void) 753 { 754 struct device *dev, *tmp; 755 756 mutex_lock(&wfs_lock); 757 list_for_each_entry_safe(dev, tmp, &wait_for_suppliers, 758 links.needs_suppliers) { 759 int ret = fwnode_call_int_op(dev->fwnode, add_links, dev); 760 if (!ret) 761 list_del_init(&dev->links.needs_suppliers); 762 else if (ret != -ENODEV || fw_devlink_is_permissive()) 763 dev->links.need_for_probe = false; 764 } 765 mutex_unlock(&wfs_lock); 766 } 767 768 #ifdef CONFIG_SRCU 769 static void __device_link_del(struct kref *kref) 770 { 771 struct device_link *link = container_of(kref, struct device_link, kref); 772 773 dev_dbg(link->consumer, "Dropping the link to %s\n", 774 dev_name(link->supplier)); 775 776 if (link->flags & DL_FLAG_PM_RUNTIME) 777 pm_runtime_drop_link(link->consumer); 778 779 list_del_rcu(&link->s_node); 780 list_del_rcu(&link->c_node); 781 device_unregister(&link->link_dev); 782 } 783 #else /* !CONFIG_SRCU */ 784 static void __device_link_del(struct kref *kref) 785 { 786 struct device_link *link = container_of(kref, struct device_link, kref); 787 788 dev_info(link->consumer, "Dropping the link to %s\n", 789 dev_name(link->supplier)); 790 791 if (link->flags & DL_FLAG_PM_RUNTIME) 792 pm_runtime_drop_link(link->consumer); 793 794 list_del(&link->s_node); 795 list_del(&link->c_node); 796 device_unregister(&link->link_dev); 797 } 798 #endif /* !CONFIG_SRCU */ 799 800 static void device_link_put_kref(struct device_link *link) 801 { 802 if (link->flags & DL_FLAG_STATELESS) 803 kref_put(&link->kref, __device_link_del); 804 else 805 WARN(1, "Unable to drop a managed device link reference\n"); 806 } 807 808 /** 809 * device_link_del - Delete a stateless link between two devices. 810 * @link: Device link to delete. 811 * 812 * The caller must ensure proper synchronization of this function with runtime 813 * PM. If the link was added multiple times, it needs to be deleted as often. 814 * Care is required for hotplugged devices: Their links are purged on removal 815 * and calling device_link_del() is then no longer allowed. 816 */ 817 void device_link_del(struct device_link *link) 818 { 819 device_links_write_lock(); 820 device_link_put_kref(link); 821 device_links_write_unlock(); 822 } 823 EXPORT_SYMBOL_GPL(device_link_del); 824 825 /** 826 * device_link_remove - Delete a stateless link between two devices. 827 * @consumer: Consumer end of the link. 828 * @supplier: Supplier end of the link. 829 * 830 * The caller must ensure proper synchronization of this function with runtime 831 * PM. 832 */ 833 void device_link_remove(void *consumer, struct device *supplier) 834 { 835 struct device_link *link; 836 837 if (WARN_ON(consumer == supplier)) 838 return; 839 840 device_links_write_lock(); 841 842 list_for_each_entry(link, &supplier->links.consumers, s_node) { 843 if (link->consumer == consumer) { 844 device_link_put_kref(link); 845 break; 846 } 847 } 848 849 device_links_write_unlock(); 850 } 851 EXPORT_SYMBOL_GPL(device_link_remove); 852 853 static void device_links_missing_supplier(struct device *dev) 854 { 855 struct device_link *link; 856 857 list_for_each_entry(link, &dev->links.suppliers, c_node) { 858 if (link->status != DL_STATE_CONSUMER_PROBE) 859 continue; 860 861 if (link->supplier->links.status == DL_DEV_DRIVER_BOUND) { 862 WRITE_ONCE(link->status, DL_STATE_AVAILABLE); 863 } else { 864 WARN_ON(!(link->flags & DL_FLAG_SYNC_STATE_ONLY)); 865 WRITE_ONCE(link->status, DL_STATE_DORMANT); 866 } 867 } 868 } 869 870 /** 871 * device_links_check_suppliers - Check presence of supplier drivers. 872 * @dev: Consumer device. 873 * 874 * Check links from this device to any suppliers. Walk the list of the device's 875 * links to suppliers and see if all of them are available. If not, simply 876 * return -EPROBE_DEFER. 877 * 878 * We need to guarantee that the supplier will not go away after the check has 879 * been positive here. It only can go away in __device_release_driver() and 880 * that function checks the device's links to consumers. This means we need to 881 * mark the link as "consumer probe in progress" to make the supplier removal 882 * wait for us to complete (or bad things may happen). 883 * 884 * Links without the DL_FLAG_MANAGED flag set are ignored. 885 */ 886 int device_links_check_suppliers(struct device *dev) 887 { 888 struct device_link *link; 889 int ret = 0; 890 891 /* 892 * Device waiting for supplier to become available is not allowed to 893 * probe. 894 */ 895 mutex_lock(&wfs_lock); 896 if (!list_empty(&dev->links.needs_suppliers) && 897 dev->links.need_for_probe) { 898 mutex_unlock(&wfs_lock); 899 return -EPROBE_DEFER; 900 } 901 mutex_unlock(&wfs_lock); 902 903 device_links_write_lock(); 904 905 list_for_each_entry(link, &dev->links.suppliers, c_node) { 906 if (!(link->flags & DL_FLAG_MANAGED)) 907 continue; 908 909 if (link->status != DL_STATE_AVAILABLE && 910 !(link->flags & DL_FLAG_SYNC_STATE_ONLY)) { 911 device_links_missing_supplier(dev); 912 ret = -EPROBE_DEFER; 913 break; 914 } 915 WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE); 916 } 917 dev->links.status = DL_DEV_PROBING; 918 919 device_links_write_unlock(); 920 return ret; 921 } 922 923 /** 924 * __device_links_queue_sync_state - Queue a device for sync_state() callback 925 * @dev: Device to call sync_state() on 926 * @list: List head to queue the @dev on 927 * 928 * Queues a device for a sync_state() callback when the device links write lock 929 * isn't held. This allows the sync_state() execution flow to use device links 930 * APIs. The caller must ensure this function is called with 931 * device_links_write_lock() held. 932 * 933 * This function does a get_device() to make sure the device is not freed while 934 * on this list. 935 * 936 * So the caller must also ensure that device_links_flush_sync_list() is called 937 * as soon as the caller releases device_links_write_lock(). This is necessary 938 * to make sure the sync_state() is called in a timely fashion and the 939 * put_device() is called on this device. 940 */ 941 static void __device_links_queue_sync_state(struct device *dev, 942 struct list_head *list) 943 { 944 struct device_link *link; 945 946 if (!dev_has_sync_state(dev)) 947 return; 948 if (dev->state_synced) 949 return; 950 951 list_for_each_entry(link, &dev->links.consumers, s_node) { 952 if (!(link->flags & DL_FLAG_MANAGED)) 953 continue; 954 if (link->status != DL_STATE_ACTIVE) 955 return; 956 } 957 958 /* 959 * Set the flag here to avoid adding the same device to a list more 960 * than once. This can happen if new consumers get added to the device 961 * and probed before the list is flushed. 962 */ 963 dev->state_synced = true; 964 965 if (WARN_ON(!list_empty(&dev->links.defer_hook))) 966 return; 967 968 get_device(dev); 969 list_add_tail(&dev->links.defer_hook, list); 970 } 971 972 /** 973 * device_links_flush_sync_list - Call sync_state() on a list of devices 974 * @list: List of devices to call sync_state() on 975 * @dont_lock_dev: Device for which lock is already held by the caller 976 * 977 * Calls sync_state() on all the devices that have been queued for it. This 978 * function is used in conjunction with __device_links_queue_sync_state(). The 979 * @dont_lock_dev parameter is useful when this function is called from a 980 * context where a device lock is already held. 981 */ 982 static void device_links_flush_sync_list(struct list_head *list, 983 struct device *dont_lock_dev) 984 { 985 struct device *dev, *tmp; 986 987 list_for_each_entry_safe(dev, tmp, list, links.defer_hook) { 988 list_del_init(&dev->links.defer_hook); 989 990 if (dev != dont_lock_dev) 991 device_lock(dev); 992 993 if (dev->bus->sync_state) 994 dev->bus->sync_state(dev); 995 else if (dev->driver && dev->driver->sync_state) 996 dev->driver->sync_state(dev); 997 998 if (dev != dont_lock_dev) 999 device_unlock(dev); 1000 1001 put_device(dev); 1002 } 1003 } 1004 1005 void device_links_supplier_sync_state_pause(void) 1006 { 1007 device_links_write_lock(); 1008 defer_sync_state_count++; 1009 device_links_write_unlock(); 1010 } 1011 1012 void device_links_supplier_sync_state_resume(void) 1013 { 1014 struct device *dev, *tmp; 1015 LIST_HEAD(sync_list); 1016 1017 device_links_write_lock(); 1018 if (!defer_sync_state_count) { 1019 WARN(true, "Unmatched sync_state pause/resume!"); 1020 goto out; 1021 } 1022 defer_sync_state_count--; 1023 if (defer_sync_state_count) 1024 goto out; 1025 1026 list_for_each_entry_safe(dev, tmp, &deferred_sync, links.defer_hook) { 1027 /* 1028 * Delete from deferred_sync list before queuing it to 1029 * sync_list because defer_hook is used for both lists. 1030 */ 1031 list_del_init(&dev->links.defer_hook); 1032 __device_links_queue_sync_state(dev, &sync_list); 1033 } 1034 out: 1035 device_links_write_unlock(); 1036 1037 device_links_flush_sync_list(&sync_list, NULL); 1038 } 1039 1040 static int sync_state_resume_initcall(void) 1041 { 1042 device_links_supplier_sync_state_resume(); 1043 return 0; 1044 } 1045 late_initcall(sync_state_resume_initcall); 1046 1047 static void __device_links_supplier_defer_sync(struct device *sup) 1048 { 1049 if (list_empty(&sup->links.defer_hook) && dev_has_sync_state(sup)) 1050 list_add_tail(&sup->links.defer_hook, &deferred_sync); 1051 } 1052 1053 static void device_link_drop_managed(struct device_link *link) 1054 { 1055 link->flags &= ~DL_FLAG_MANAGED; 1056 WRITE_ONCE(link->status, DL_STATE_NONE); 1057 kref_put(&link->kref, __device_link_del); 1058 } 1059 1060 static ssize_t waiting_for_supplier_show(struct device *dev, 1061 struct device_attribute *attr, 1062 char *buf) 1063 { 1064 bool val; 1065 1066 device_lock(dev); 1067 mutex_lock(&wfs_lock); 1068 val = !list_empty(&dev->links.needs_suppliers) 1069 && dev->links.need_for_probe; 1070 mutex_unlock(&wfs_lock); 1071 device_unlock(dev); 1072 return sysfs_emit(buf, "%u\n", val); 1073 } 1074 static DEVICE_ATTR_RO(waiting_for_supplier); 1075 1076 /** 1077 * device_links_driver_bound - Update device links after probing its driver. 1078 * @dev: Device to update the links for. 1079 * 1080 * The probe has been successful, so update links from this device to any 1081 * consumers by changing their status to "available". 1082 * 1083 * Also change the status of @dev's links to suppliers to "active". 1084 * 1085 * Links without the DL_FLAG_MANAGED flag set are ignored. 1086 */ 1087 void device_links_driver_bound(struct device *dev) 1088 { 1089 struct device_link *link, *ln; 1090 LIST_HEAD(sync_list); 1091 1092 /* 1093 * If a device probes successfully, it's expected to have created all 1094 * the device links it needs to or make new device links as it needs 1095 * them. So, it no longer needs to wait on any suppliers. 1096 */ 1097 mutex_lock(&wfs_lock); 1098 list_del_init(&dev->links.needs_suppliers); 1099 mutex_unlock(&wfs_lock); 1100 device_remove_file(dev, &dev_attr_waiting_for_supplier); 1101 1102 device_links_write_lock(); 1103 1104 list_for_each_entry(link, &dev->links.consumers, s_node) { 1105 if (!(link->flags & DL_FLAG_MANAGED)) 1106 continue; 1107 1108 /* 1109 * Links created during consumer probe may be in the "consumer 1110 * probe" state to start with if the supplier is still probing 1111 * when they are created and they may become "active" if the 1112 * consumer probe returns first. Skip them here. 1113 */ 1114 if (link->status == DL_STATE_CONSUMER_PROBE || 1115 link->status == DL_STATE_ACTIVE) 1116 continue; 1117 1118 WARN_ON(link->status != DL_STATE_DORMANT); 1119 WRITE_ONCE(link->status, DL_STATE_AVAILABLE); 1120 1121 if (link->flags & DL_FLAG_AUTOPROBE_CONSUMER) 1122 driver_deferred_probe_add(link->consumer); 1123 } 1124 1125 if (defer_sync_state_count) 1126 __device_links_supplier_defer_sync(dev); 1127 else 1128 __device_links_queue_sync_state(dev, &sync_list); 1129 1130 list_for_each_entry_safe(link, ln, &dev->links.suppliers, c_node) { 1131 struct device *supplier; 1132 1133 if (!(link->flags & DL_FLAG_MANAGED)) 1134 continue; 1135 1136 supplier = link->supplier; 1137 if (link->flags & DL_FLAG_SYNC_STATE_ONLY) { 1138 /* 1139 * When DL_FLAG_SYNC_STATE_ONLY is set, it means no 1140 * other DL_MANAGED_LINK_FLAGS have been set. So, it's 1141 * save to drop the managed link completely. 1142 */ 1143 device_link_drop_managed(link); 1144 } else { 1145 WARN_ON(link->status != DL_STATE_CONSUMER_PROBE); 1146 WRITE_ONCE(link->status, DL_STATE_ACTIVE); 1147 } 1148 1149 /* 1150 * This needs to be done even for the deleted 1151 * DL_FLAG_SYNC_STATE_ONLY device link in case it was the last 1152 * device link that was preventing the supplier from getting a 1153 * sync_state() call. 1154 */ 1155 if (defer_sync_state_count) 1156 __device_links_supplier_defer_sync(supplier); 1157 else 1158 __device_links_queue_sync_state(supplier, &sync_list); 1159 } 1160 1161 dev->links.status = DL_DEV_DRIVER_BOUND; 1162 1163 device_links_write_unlock(); 1164 1165 device_links_flush_sync_list(&sync_list, dev); 1166 } 1167 1168 /** 1169 * __device_links_no_driver - Update links of a device without a driver. 1170 * @dev: Device without a drvier. 1171 * 1172 * Delete all non-persistent links from this device to any suppliers. 1173 * 1174 * Persistent links stay around, but their status is changed to "available", 1175 * unless they already are in the "supplier unbind in progress" state in which 1176 * case they need not be updated. 1177 * 1178 * Links without the DL_FLAG_MANAGED flag set are ignored. 1179 */ 1180 static void __device_links_no_driver(struct device *dev) 1181 { 1182 struct device_link *link, *ln; 1183 1184 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) { 1185 if (!(link->flags & DL_FLAG_MANAGED)) 1186 continue; 1187 1188 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER) { 1189 device_link_drop_managed(link); 1190 continue; 1191 } 1192 1193 if (link->status != DL_STATE_CONSUMER_PROBE && 1194 link->status != DL_STATE_ACTIVE) 1195 continue; 1196 1197 if (link->supplier->links.status == DL_DEV_DRIVER_BOUND) { 1198 WRITE_ONCE(link->status, DL_STATE_AVAILABLE); 1199 } else { 1200 WARN_ON(!(link->flags & DL_FLAG_SYNC_STATE_ONLY)); 1201 WRITE_ONCE(link->status, DL_STATE_DORMANT); 1202 } 1203 } 1204 1205 dev->links.status = DL_DEV_NO_DRIVER; 1206 } 1207 1208 /** 1209 * device_links_no_driver - Update links after failing driver probe. 1210 * @dev: Device whose driver has just failed to probe. 1211 * 1212 * Clean up leftover links to consumers for @dev and invoke 1213 * %__device_links_no_driver() to update links to suppliers for it as 1214 * appropriate. 1215 * 1216 * Links without the DL_FLAG_MANAGED flag set are ignored. 1217 */ 1218 void device_links_no_driver(struct device *dev) 1219 { 1220 struct device_link *link; 1221 1222 device_links_write_lock(); 1223 1224 list_for_each_entry(link, &dev->links.consumers, s_node) { 1225 if (!(link->flags & DL_FLAG_MANAGED)) 1226 continue; 1227 1228 /* 1229 * The probe has failed, so if the status of the link is 1230 * "consumer probe" or "active", it must have been added by 1231 * a probing consumer while this device was still probing. 1232 * Change its state to "dormant", as it represents a valid 1233 * relationship, but it is not functionally meaningful. 1234 */ 1235 if (link->status == DL_STATE_CONSUMER_PROBE || 1236 link->status == DL_STATE_ACTIVE) 1237 WRITE_ONCE(link->status, DL_STATE_DORMANT); 1238 } 1239 1240 __device_links_no_driver(dev); 1241 1242 device_links_write_unlock(); 1243 } 1244 1245 /** 1246 * device_links_driver_cleanup - Update links after driver removal. 1247 * @dev: Device whose driver has just gone away. 1248 * 1249 * Update links to consumers for @dev by changing their status to "dormant" and 1250 * invoke %__device_links_no_driver() to update links to suppliers for it as 1251 * appropriate. 1252 * 1253 * Links without the DL_FLAG_MANAGED flag set are ignored. 1254 */ 1255 void device_links_driver_cleanup(struct device *dev) 1256 { 1257 struct device_link *link, *ln; 1258 1259 device_links_write_lock(); 1260 1261 list_for_each_entry_safe(link, ln, &dev->links.consumers, s_node) { 1262 if (!(link->flags & DL_FLAG_MANAGED)) 1263 continue; 1264 1265 WARN_ON(link->flags & DL_FLAG_AUTOREMOVE_CONSUMER); 1266 WARN_ON(link->status != DL_STATE_SUPPLIER_UNBIND); 1267 1268 /* 1269 * autoremove the links between this @dev and its consumer 1270 * devices that are not active, i.e. where the link state 1271 * has moved to DL_STATE_SUPPLIER_UNBIND. 1272 */ 1273 if (link->status == DL_STATE_SUPPLIER_UNBIND && 1274 link->flags & DL_FLAG_AUTOREMOVE_SUPPLIER) 1275 device_link_drop_managed(link); 1276 1277 WRITE_ONCE(link->status, DL_STATE_DORMANT); 1278 } 1279 1280 list_del_init(&dev->links.defer_hook); 1281 __device_links_no_driver(dev); 1282 1283 device_links_write_unlock(); 1284 } 1285 1286 /** 1287 * device_links_busy - Check if there are any busy links to consumers. 1288 * @dev: Device to check. 1289 * 1290 * Check each consumer of the device and return 'true' if its link's status 1291 * is one of "consumer probe" or "active" (meaning that the given consumer is 1292 * probing right now or its driver is present). Otherwise, change the link 1293 * state to "supplier unbind" to prevent the consumer from being probed 1294 * successfully going forward. 1295 * 1296 * Return 'false' if there are no probing or active consumers. 1297 * 1298 * Links without the DL_FLAG_MANAGED flag set are ignored. 1299 */ 1300 bool device_links_busy(struct device *dev) 1301 { 1302 struct device_link *link; 1303 bool ret = false; 1304 1305 device_links_write_lock(); 1306 1307 list_for_each_entry(link, &dev->links.consumers, s_node) { 1308 if (!(link->flags & DL_FLAG_MANAGED)) 1309 continue; 1310 1311 if (link->status == DL_STATE_CONSUMER_PROBE 1312 || link->status == DL_STATE_ACTIVE) { 1313 ret = true; 1314 break; 1315 } 1316 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND); 1317 } 1318 1319 dev->links.status = DL_DEV_UNBINDING; 1320 1321 device_links_write_unlock(); 1322 return ret; 1323 } 1324 1325 /** 1326 * device_links_unbind_consumers - Force unbind consumers of the given device. 1327 * @dev: Device to unbind the consumers of. 1328 * 1329 * Walk the list of links to consumers for @dev and if any of them is in the 1330 * "consumer probe" state, wait for all device probes in progress to complete 1331 * and start over. 1332 * 1333 * If that's not the case, change the status of the link to "supplier unbind" 1334 * and check if the link was in the "active" state. If so, force the consumer 1335 * driver to unbind and start over (the consumer will not re-probe as we have 1336 * changed the state of the link already). 1337 * 1338 * Links without the DL_FLAG_MANAGED flag set are ignored. 1339 */ 1340 void device_links_unbind_consumers(struct device *dev) 1341 { 1342 struct device_link *link; 1343 1344 start: 1345 device_links_write_lock(); 1346 1347 list_for_each_entry(link, &dev->links.consumers, s_node) { 1348 enum device_link_state status; 1349 1350 if (!(link->flags & DL_FLAG_MANAGED) || 1351 link->flags & DL_FLAG_SYNC_STATE_ONLY) 1352 continue; 1353 1354 status = link->status; 1355 if (status == DL_STATE_CONSUMER_PROBE) { 1356 device_links_write_unlock(); 1357 1358 wait_for_device_probe(); 1359 goto start; 1360 } 1361 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND); 1362 if (status == DL_STATE_ACTIVE) { 1363 struct device *consumer = link->consumer; 1364 1365 get_device(consumer); 1366 1367 device_links_write_unlock(); 1368 1369 device_release_driver_internal(consumer, NULL, 1370 consumer->parent); 1371 put_device(consumer); 1372 goto start; 1373 } 1374 } 1375 1376 device_links_write_unlock(); 1377 } 1378 1379 /** 1380 * device_links_purge - Delete existing links to other devices. 1381 * @dev: Target device. 1382 */ 1383 static void device_links_purge(struct device *dev) 1384 { 1385 struct device_link *link, *ln; 1386 1387 if (dev->class == &devlink_class) 1388 return; 1389 1390 mutex_lock(&wfs_lock); 1391 list_del(&dev->links.needs_suppliers); 1392 mutex_unlock(&wfs_lock); 1393 1394 /* 1395 * Delete all of the remaining links from this device to any other 1396 * devices (either consumers or suppliers). 1397 */ 1398 device_links_write_lock(); 1399 1400 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) { 1401 WARN_ON(link->status == DL_STATE_ACTIVE); 1402 __device_link_del(&link->kref); 1403 } 1404 1405 list_for_each_entry_safe_reverse(link, ln, &dev->links.consumers, s_node) { 1406 WARN_ON(link->status != DL_STATE_DORMANT && 1407 link->status != DL_STATE_NONE); 1408 __device_link_del(&link->kref); 1409 } 1410 1411 device_links_write_unlock(); 1412 } 1413 1414 static u32 fw_devlink_flags = DL_FLAG_SYNC_STATE_ONLY; 1415 static int __init fw_devlink_setup(char *arg) 1416 { 1417 if (!arg) 1418 return -EINVAL; 1419 1420 if (strcmp(arg, "off") == 0) { 1421 fw_devlink_flags = 0; 1422 } else if (strcmp(arg, "permissive") == 0) { 1423 fw_devlink_flags = DL_FLAG_SYNC_STATE_ONLY; 1424 } else if (strcmp(arg, "on") == 0) { 1425 fw_devlink_flags = DL_FLAG_AUTOPROBE_CONSUMER; 1426 } else if (strcmp(arg, "rpm") == 0) { 1427 fw_devlink_flags = DL_FLAG_AUTOPROBE_CONSUMER | 1428 DL_FLAG_PM_RUNTIME; 1429 } 1430 return 0; 1431 } 1432 early_param("fw_devlink", fw_devlink_setup); 1433 1434 u32 fw_devlink_get_flags(void) 1435 { 1436 return fw_devlink_flags; 1437 } 1438 1439 static bool fw_devlink_is_permissive(void) 1440 { 1441 return fw_devlink_flags == DL_FLAG_SYNC_STATE_ONLY; 1442 } 1443 1444 static void fw_devlink_link_device(struct device *dev) 1445 { 1446 int fw_ret; 1447 1448 if (!fw_devlink_flags) 1449 return; 1450 1451 mutex_lock(&defer_fw_devlink_lock); 1452 if (!defer_fw_devlink_count) 1453 device_link_add_missing_supplier_links(); 1454 1455 /* 1456 * The device's fwnode not having add_links() doesn't affect if other 1457 * consumers can find this device as a supplier. So, this check is 1458 * intentionally placed after device_link_add_missing_supplier_links(). 1459 */ 1460 if (!fwnode_has_op(dev->fwnode, add_links)) 1461 goto out; 1462 1463 /* 1464 * If fw_devlink is being deferred, assume all devices have mandatory 1465 * suppliers they need to link to later. Then, when the fw_devlink is 1466 * resumed, all these devices will get a chance to try and link to any 1467 * suppliers they have. 1468 */ 1469 if (!defer_fw_devlink_count) { 1470 fw_ret = fwnode_call_int_op(dev->fwnode, add_links, dev); 1471 if (fw_ret == -ENODEV && fw_devlink_is_permissive()) 1472 fw_ret = -EAGAIN; 1473 } else { 1474 fw_ret = -ENODEV; 1475 /* 1476 * defer_hook is not used to add device to deferred_sync list 1477 * until device is bound. Since deferred fw devlink also blocks 1478 * probing, same list hook can be used for deferred_fw_devlink. 1479 */ 1480 list_add_tail(&dev->links.defer_hook, &deferred_fw_devlink); 1481 } 1482 1483 if (fw_ret == -ENODEV) 1484 device_link_wait_for_mandatory_supplier(dev); 1485 else if (fw_ret) 1486 device_link_wait_for_optional_supplier(dev); 1487 1488 out: 1489 mutex_unlock(&defer_fw_devlink_lock); 1490 } 1491 1492 /** 1493 * fw_devlink_pause - Pause parsing of fwnode to create device links 1494 * 1495 * Calling this function defers any fwnode parsing to create device links until 1496 * fw_devlink_resume() is called. Both these functions are ref counted and the 1497 * caller needs to match the calls. 1498 * 1499 * While fw_devlink is paused: 1500 * - Any device that is added won't have its fwnode parsed to create device 1501 * links. 1502 * - The probe of the device will also be deferred during this period. 1503 * - Any devices that were already added, but waiting for suppliers won't be 1504 * able to link to newly added devices. 1505 * 1506 * Once fw_devlink_resume(): 1507 * - All the fwnodes that was not parsed will be parsed. 1508 * - All the devices that were deferred probing will be reattempted if they 1509 * aren't waiting for any more suppliers. 1510 * 1511 * This pair of functions, is mainly meant to optimize the parsing of fwnodes 1512 * when a lot of devices that need to link to each other are added in a short 1513 * interval of time. For example, adding all the top level devices in a system. 1514 * 1515 * For example, if N devices are added and: 1516 * - All the consumers are added before their suppliers 1517 * - All the suppliers of the N devices are part of the N devices 1518 * 1519 * Then: 1520 * 1521 * - With the use of fw_devlink_pause() and fw_devlink_resume(), each device 1522 * will only need one parsing of its fwnode because it is guaranteed to find 1523 * all the supplier devices already registered and ready to link to. It won't 1524 * have to do another pass later to find one or more suppliers it couldn't 1525 * find in the first parse of the fwnode. So, we'll only need O(N) fwnode 1526 * parses. 1527 * 1528 * - Without the use of fw_devlink_pause() and fw_devlink_resume(), we would 1529 * end up doing O(N^2) parses of fwnodes because every device that's added is 1530 * guaranteed to trigger a parse of the fwnode of every device added before 1531 * it. This O(N^2) parse is made worse by the fact that when a fwnode of a 1532 * device is parsed, all it descendant devices might need to have their 1533 * fwnodes parsed too (even if the devices themselves aren't added). 1534 */ 1535 void fw_devlink_pause(void) 1536 { 1537 mutex_lock(&defer_fw_devlink_lock); 1538 defer_fw_devlink_count++; 1539 mutex_unlock(&defer_fw_devlink_lock); 1540 } 1541 1542 /** fw_devlink_resume - Resume parsing of fwnode to create device links 1543 * 1544 * This function is used in conjunction with fw_devlink_pause() and is ref 1545 * counted. See documentation for fw_devlink_pause() for more details. 1546 */ 1547 void fw_devlink_resume(void) 1548 { 1549 struct device *dev, *tmp; 1550 LIST_HEAD(probe_list); 1551 1552 mutex_lock(&defer_fw_devlink_lock); 1553 if (!defer_fw_devlink_count) { 1554 WARN(true, "Unmatched fw_devlink pause/resume!"); 1555 goto out; 1556 } 1557 1558 defer_fw_devlink_count--; 1559 if (defer_fw_devlink_count) 1560 goto out; 1561 1562 device_link_add_missing_supplier_links(); 1563 list_splice_tail_init(&deferred_fw_devlink, &probe_list); 1564 out: 1565 mutex_unlock(&defer_fw_devlink_lock); 1566 1567 /* 1568 * bus_probe_device() can cause new devices to get added and they'll 1569 * try to grab defer_fw_devlink_lock. So, this needs to be done outside 1570 * the defer_fw_devlink_lock. 1571 */ 1572 list_for_each_entry_safe(dev, tmp, &probe_list, links.defer_hook) { 1573 list_del_init(&dev->links.defer_hook); 1574 bus_probe_device(dev); 1575 } 1576 } 1577 /* Device links support end. */ 1578 1579 int (*platform_notify)(struct device *dev) = NULL; 1580 int (*platform_notify_remove)(struct device *dev) = NULL; 1581 static struct kobject *dev_kobj; 1582 struct kobject *sysfs_dev_char_kobj; 1583 struct kobject *sysfs_dev_block_kobj; 1584 1585 static DEFINE_MUTEX(device_hotplug_lock); 1586 1587 void lock_device_hotplug(void) 1588 { 1589 mutex_lock(&device_hotplug_lock); 1590 } 1591 1592 void unlock_device_hotplug(void) 1593 { 1594 mutex_unlock(&device_hotplug_lock); 1595 } 1596 1597 int lock_device_hotplug_sysfs(void) 1598 { 1599 if (mutex_trylock(&device_hotplug_lock)) 1600 return 0; 1601 1602 /* Avoid busy looping (5 ms of sleep should do). */ 1603 msleep(5); 1604 return restart_syscall(); 1605 } 1606 1607 #ifdef CONFIG_BLOCK 1608 static inline int device_is_not_partition(struct device *dev) 1609 { 1610 return !(dev->type == &part_type); 1611 } 1612 #else 1613 static inline int device_is_not_partition(struct device *dev) 1614 { 1615 return 1; 1616 } 1617 #endif 1618 1619 static int 1620 device_platform_notify(struct device *dev, enum kobject_action action) 1621 { 1622 int ret; 1623 1624 ret = acpi_platform_notify(dev, action); 1625 if (ret) 1626 return ret; 1627 1628 ret = software_node_notify(dev, action); 1629 if (ret) 1630 return ret; 1631 1632 if (platform_notify && action == KOBJ_ADD) 1633 platform_notify(dev); 1634 else if (platform_notify_remove && action == KOBJ_REMOVE) 1635 platform_notify_remove(dev); 1636 return 0; 1637 } 1638 1639 /** 1640 * dev_driver_string - Return a device's driver name, if at all possible 1641 * @dev: struct device to get the name of 1642 * 1643 * Will return the device's driver's name if it is bound to a device. If 1644 * the device is not bound to a driver, it will return the name of the bus 1645 * it is attached to. If it is not attached to a bus either, an empty 1646 * string will be returned. 1647 */ 1648 const char *dev_driver_string(const struct device *dev) 1649 { 1650 struct device_driver *drv; 1651 1652 /* dev->driver can change to NULL underneath us because of unbinding, 1653 * so be careful about accessing it. dev->bus and dev->class should 1654 * never change once they are set, so they don't need special care. 1655 */ 1656 drv = READ_ONCE(dev->driver); 1657 return drv ? drv->name : 1658 (dev->bus ? dev->bus->name : 1659 (dev->class ? dev->class->name : "")); 1660 } 1661 EXPORT_SYMBOL(dev_driver_string); 1662 1663 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr) 1664 1665 static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr, 1666 char *buf) 1667 { 1668 struct device_attribute *dev_attr = to_dev_attr(attr); 1669 struct device *dev = kobj_to_dev(kobj); 1670 ssize_t ret = -EIO; 1671 1672 if (dev_attr->show) 1673 ret = dev_attr->show(dev, dev_attr, buf); 1674 if (ret >= (ssize_t)PAGE_SIZE) { 1675 printk("dev_attr_show: %pS returned bad count\n", 1676 dev_attr->show); 1677 } 1678 return ret; 1679 } 1680 1681 static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr, 1682 const char *buf, size_t count) 1683 { 1684 struct device_attribute *dev_attr = to_dev_attr(attr); 1685 struct device *dev = kobj_to_dev(kobj); 1686 ssize_t ret = -EIO; 1687 1688 if (dev_attr->store) 1689 ret = dev_attr->store(dev, dev_attr, buf, count); 1690 return ret; 1691 } 1692 1693 static const struct sysfs_ops dev_sysfs_ops = { 1694 .show = dev_attr_show, 1695 .store = dev_attr_store, 1696 }; 1697 1698 #define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr) 1699 1700 ssize_t device_store_ulong(struct device *dev, 1701 struct device_attribute *attr, 1702 const char *buf, size_t size) 1703 { 1704 struct dev_ext_attribute *ea = to_ext_attr(attr); 1705 int ret; 1706 unsigned long new; 1707 1708 ret = kstrtoul(buf, 0, &new); 1709 if (ret) 1710 return ret; 1711 *(unsigned long *)(ea->var) = new; 1712 /* Always return full write size even if we didn't consume all */ 1713 return size; 1714 } 1715 EXPORT_SYMBOL_GPL(device_store_ulong); 1716 1717 ssize_t device_show_ulong(struct device *dev, 1718 struct device_attribute *attr, 1719 char *buf) 1720 { 1721 struct dev_ext_attribute *ea = to_ext_attr(attr); 1722 return sysfs_emit(buf, "%lx\n", *(unsigned long *)(ea->var)); 1723 } 1724 EXPORT_SYMBOL_GPL(device_show_ulong); 1725 1726 ssize_t device_store_int(struct device *dev, 1727 struct device_attribute *attr, 1728 const char *buf, size_t size) 1729 { 1730 struct dev_ext_attribute *ea = to_ext_attr(attr); 1731 int ret; 1732 long new; 1733 1734 ret = kstrtol(buf, 0, &new); 1735 if (ret) 1736 return ret; 1737 1738 if (new > INT_MAX || new < INT_MIN) 1739 return -EINVAL; 1740 *(int *)(ea->var) = new; 1741 /* Always return full write size even if we didn't consume all */ 1742 return size; 1743 } 1744 EXPORT_SYMBOL_GPL(device_store_int); 1745 1746 ssize_t device_show_int(struct device *dev, 1747 struct device_attribute *attr, 1748 char *buf) 1749 { 1750 struct dev_ext_attribute *ea = to_ext_attr(attr); 1751 1752 return sysfs_emit(buf, "%d\n", *(int *)(ea->var)); 1753 } 1754 EXPORT_SYMBOL_GPL(device_show_int); 1755 1756 ssize_t device_store_bool(struct device *dev, struct device_attribute *attr, 1757 const char *buf, size_t size) 1758 { 1759 struct dev_ext_attribute *ea = to_ext_attr(attr); 1760 1761 if (strtobool(buf, ea->var) < 0) 1762 return -EINVAL; 1763 1764 return size; 1765 } 1766 EXPORT_SYMBOL_GPL(device_store_bool); 1767 1768 ssize_t device_show_bool(struct device *dev, struct device_attribute *attr, 1769 char *buf) 1770 { 1771 struct dev_ext_attribute *ea = to_ext_attr(attr); 1772 1773 return sysfs_emit(buf, "%d\n", *(bool *)(ea->var)); 1774 } 1775 EXPORT_SYMBOL_GPL(device_show_bool); 1776 1777 /** 1778 * device_release - free device structure. 1779 * @kobj: device's kobject. 1780 * 1781 * This is called once the reference count for the object 1782 * reaches 0. We forward the call to the device's release 1783 * method, which should handle actually freeing the structure. 1784 */ 1785 static void device_release(struct kobject *kobj) 1786 { 1787 struct device *dev = kobj_to_dev(kobj); 1788 struct device_private *p = dev->p; 1789 1790 /* 1791 * Some platform devices are driven without driver attached 1792 * and managed resources may have been acquired. Make sure 1793 * all resources are released. 1794 * 1795 * Drivers still can add resources into device after device 1796 * is deleted but alive, so release devres here to avoid 1797 * possible memory leak. 1798 */ 1799 devres_release_all(dev); 1800 1801 kfree(dev->dma_range_map); 1802 1803 if (dev->release) 1804 dev->release(dev); 1805 else if (dev->type && dev->type->release) 1806 dev->type->release(dev); 1807 else if (dev->class && dev->class->dev_release) 1808 dev->class->dev_release(dev); 1809 else 1810 WARN(1, KERN_ERR "Device '%s' does not have a release() function, it is broken and must be fixed. See Documentation/core-api/kobject.rst.\n", 1811 dev_name(dev)); 1812 kfree(p); 1813 } 1814 1815 static const void *device_namespace(struct kobject *kobj) 1816 { 1817 struct device *dev = kobj_to_dev(kobj); 1818 const void *ns = NULL; 1819 1820 if (dev->class && dev->class->ns_type) 1821 ns = dev->class->namespace(dev); 1822 1823 return ns; 1824 } 1825 1826 static void device_get_ownership(struct kobject *kobj, kuid_t *uid, kgid_t *gid) 1827 { 1828 struct device *dev = kobj_to_dev(kobj); 1829 1830 if (dev->class && dev->class->get_ownership) 1831 dev->class->get_ownership(dev, uid, gid); 1832 } 1833 1834 static struct kobj_type device_ktype = { 1835 .release = device_release, 1836 .sysfs_ops = &dev_sysfs_ops, 1837 .namespace = device_namespace, 1838 .get_ownership = device_get_ownership, 1839 }; 1840 1841 1842 static int dev_uevent_filter(struct kset *kset, struct kobject *kobj) 1843 { 1844 struct kobj_type *ktype = get_ktype(kobj); 1845 1846 if (ktype == &device_ktype) { 1847 struct device *dev = kobj_to_dev(kobj); 1848 if (dev->bus) 1849 return 1; 1850 if (dev->class) 1851 return 1; 1852 } 1853 return 0; 1854 } 1855 1856 static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj) 1857 { 1858 struct device *dev = kobj_to_dev(kobj); 1859 1860 if (dev->bus) 1861 return dev->bus->name; 1862 if (dev->class) 1863 return dev->class->name; 1864 return NULL; 1865 } 1866 1867 static int dev_uevent(struct kset *kset, struct kobject *kobj, 1868 struct kobj_uevent_env *env) 1869 { 1870 struct device *dev = kobj_to_dev(kobj); 1871 int retval = 0; 1872 1873 /* add device node properties if present */ 1874 if (MAJOR(dev->devt)) { 1875 const char *tmp; 1876 const char *name; 1877 umode_t mode = 0; 1878 kuid_t uid = GLOBAL_ROOT_UID; 1879 kgid_t gid = GLOBAL_ROOT_GID; 1880 1881 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt)); 1882 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt)); 1883 name = device_get_devnode(dev, &mode, &uid, &gid, &tmp); 1884 if (name) { 1885 add_uevent_var(env, "DEVNAME=%s", name); 1886 if (mode) 1887 add_uevent_var(env, "DEVMODE=%#o", mode & 0777); 1888 if (!uid_eq(uid, GLOBAL_ROOT_UID)) 1889 add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid)); 1890 if (!gid_eq(gid, GLOBAL_ROOT_GID)) 1891 add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid)); 1892 kfree(tmp); 1893 } 1894 } 1895 1896 if (dev->type && dev->type->name) 1897 add_uevent_var(env, "DEVTYPE=%s", dev->type->name); 1898 1899 if (dev->driver) 1900 add_uevent_var(env, "DRIVER=%s", dev->driver->name); 1901 1902 /* Add common DT information about the device */ 1903 of_device_uevent(dev, env); 1904 1905 /* have the bus specific function add its stuff */ 1906 if (dev->bus && dev->bus->uevent) { 1907 retval = dev->bus->uevent(dev, env); 1908 if (retval) 1909 pr_debug("device: '%s': %s: bus uevent() returned %d\n", 1910 dev_name(dev), __func__, retval); 1911 } 1912 1913 /* have the class specific function add its stuff */ 1914 if (dev->class && dev->class->dev_uevent) { 1915 retval = dev->class->dev_uevent(dev, env); 1916 if (retval) 1917 pr_debug("device: '%s': %s: class uevent() " 1918 "returned %d\n", dev_name(dev), 1919 __func__, retval); 1920 } 1921 1922 /* have the device type specific function add its stuff */ 1923 if (dev->type && dev->type->uevent) { 1924 retval = dev->type->uevent(dev, env); 1925 if (retval) 1926 pr_debug("device: '%s': %s: dev_type uevent() " 1927 "returned %d\n", dev_name(dev), 1928 __func__, retval); 1929 } 1930 1931 return retval; 1932 } 1933 1934 static const struct kset_uevent_ops device_uevent_ops = { 1935 .filter = dev_uevent_filter, 1936 .name = dev_uevent_name, 1937 .uevent = dev_uevent, 1938 }; 1939 1940 static ssize_t uevent_show(struct device *dev, struct device_attribute *attr, 1941 char *buf) 1942 { 1943 struct kobject *top_kobj; 1944 struct kset *kset; 1945 struct kobj_uevent_env *env = NULL; 1946 int i; 1947 int len = 0; 1948 int retval; 1949 1950 /* search the kset, the device belongs to */ 1951 top_kobj = &dev->kobj; 1952 while (!top_kobj->kset && top_kobj->parent) 1953 top_kobj = top_kobj->parent; 1954 if (!top_kobj->kset) 1955 goto out; 1956 1957 kset = top_kobj->kset; 1958 if (!kset->uevent_ops || !kset->uevent_ops->uevent) 1959 goto out; 1960 1961 /* respect filter */ 1962 if (kset->uevent_ops && kset->uevent_ops->filter) 1963 if (!kset->uevent_ops->filter(kset, &dev->kobj)) 1964 goto out; 1965 1966 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL); 1967 if (!env) 1968 return -ENOMEM; 1969 1970 /* let the kset specific function add its keys */ 1971 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env); 1972 if (retval) 1973 goto out; 1974 1975 /* copy keys to file */ 1976 for (i = 0; i < env->envp_idx; i++) 1977 len += sysfs_emit_at(buf, len, "%s\n", env->envp[i]); 1978 out: 1979 kfree(env); 1980 return len; 1981 } 1982 1983 static ssize_t uevent_store(struct device *dev, struct device_attribute *attr, 1984 const char *buf, size_t count) 1985 { 1986 int rc; 1987 1988 rc = kobject_synth_uevent(&dev->kobj, buf, count); 1989 1990 if (rc) { 1991 dev_err(dev, "uevent: failed to send synthetic uevent\n"); 1992 return rc; 1993 } 1994 1995 return count; 1996 } 1997 static DEVICE_ATTR_RW(uevent); 1998 1999 static ssize_t online_show(struct device *dev, struct device_attribute *attr, 2000 char *buf) 2001 { 2002 bool val; 2003 2004 device_lock(dev); 2005 val = !dev->offline; 2006 device_unlock(dev); 2007 return sysfs_emit(buf, "%u\n", val); 2008 } 2009 2010 static ssize_t online_store(struct device *dev, struct device_attribute *attr, 2011 const char *buf, size_t count) 2012 { 2013 bool val; 2014 int ret; 2015 2016 ret = strtobool(buf, &val); 2017 if (ret < 0) 2018 return ret; 2019 2020 ret = lock_device_hotplug_sysfs(); 2021 if (ret) 2022 return ret; 2023 2024 ret = val ? device_online(dev) : device_offline(dev); 2025 unlock_device_hotplug(); 2026 return ret < 0 ? ret : count; 2027 } 2028 static DEVICE_ATTR_RW(online); 2029 2030 int device_add_groups(struct device *dev, const struct attribute_group **groups) 2031 { 2032 return sysfs_create_groups(&dev->kobj, groups); 2033 } 2034 EXPORT_SYMBOL_GPL(device_add_groups); 2035 2036 void device_remove_groups(struct device *dev, 2037 const struct attribute_group **groups) 2038 { 2039 sysfs_remove_groups(&dev->kobj, groups); 2040 } 2041 EXPORT_SYMBOL_GPL(device_remove_groups); 2042 2043 union device_attr_group_devres { 2044 const struct attribute_group *group; 2045 const struct attribute_group **groups; 2046 }; 2047 2048 static int devm_attr_group_match(struct device *dev, void *res, void *data) 2049 { 2050 return ((union device_attr_group_devres *)res)->group == data; 2051 } 2052 2053 static void devm_attr_group_remove(struct device *dev, void *res) 2054 { 2055 union device_attr_group_devres *devres = res; 2056 const struct attribute_group *group = devres->group; 2057 2058 dev_dbg(dev, "%s: removing group %p\n", __func__, group); 2059 sysfs_remove_group(&dev->kobj, group); 2060 } 2061 2062 static void devm_attr_groups_remove(struct device *dev, void *res) 2063 { 2064 union device_attr_group_devres *devres = res; 2065 const struct attribute_group **groups = devres->groups; 2066 2067 dev_dbg(dev, "%s: removing groups %p\n", __func__, groups); 2068 sysfs_remove_groups(&dev->kobj, groups); 2069 } 2070 2071 /** 2072 * devm_device_add_group - given a device, create a managed attribute group 2073 * @dev: The device to create the group for 2074 * @grp: The attribute group to create 2075 * 2076 * This function creates a group for the first time. It will explicitly 2077 * warn and error if any of the attribute files being created already exist. 2078 * 2079 * Returns 0 on success or error code on failure. 2080 */ 2081 int devm_device_add_group(struct device *dev, const struct attribute_group *grp) 2082 { 2083 union device_attr_group_devres *devres; 2084 int error; 2085 2086 devres = devres_alloc(devm_attr_group_remove, 2087 sizeof(*devres), GFP_KERNEL); 2088 if (!devres) 2089 return -ENOMEM; 2090 2091 error = sysfs_create_group(&dev->kobj, grp); 2092 if (error) { 2093 devres_free(devres); 2094 return error; 2095 } 2096 2097 devres->group = grp; 2098 devres_add(dev, devres); 2099 return 0; 2100 } 2101 EXPORT_SYMBOL_GPL(devm_device_add_group); 2102 2103 /** 2104 * devm_device_remove_group: remove a managed group from a device 2105 * @dev: device to remove the group from 2106 * @grp: group to remove 2107 * 2108 * This function removes a group of attributes from a device. The attributes 2109 * previously have to have been created for this group, otherwise it will fail. 2110 */ 2111 void devm_device_remove_group(struct device *dev, 2112 const struct attribute_group *grp) 2113 { 2114 WARN_ON(devres_release(dev, devm_attr_group_remove, 2115 devm_attr_group_match, 2116 /* cast away const */ (void *)grp)); 2117 } 2118 EXPORT_SYMBOL_GPL(devm_device_remove_group); 2119 2120 /** 2121 * devm_device_add_groups - create a bunch of managed attribute groups 2122 * @dev: The device to create the group for 2123 * @groups: The attribute groups to create, NULL terminated 2124 * 2125 * This function creates a bunch of managed attribute groups. If an error 2126 * occurs when creating a group, all previously created groups will be 2127 * removed, unwinding everything back to the original state when this 2128 * function was called. It will explicitly warn and error if any of the 2129 * attribute files being created already exist. 2130 * 2131 * Returns 0 on success or error code from sysfs_create_group on failure. 2132 */ 2133 int devm_device_add_groups(struct device *dev, 2134 const struct attribute_group **groups) 2135 { 2136 union device_attr_group_devres *devres; 2137 int error; 2138 2139 devres = devres_alloc(devm_attr_groups_remove, 2140 sizeof(*devres), GFP_KERNEL); 2141 if (!devres) 2142 return -ENOMEM; 2143 2144 error = sysfs_create_groups(&dev->kobj, groups); 2145 if (error) { 2146 devres_free(devres); 2147 return error; 2148 } 2149 2150 devres->groups = groups; 2151 devres_add(dev, devres); 2152 return 0; 2153 } 2154 EXPORT_SYMBOL_GPL(devm_device_add_groups); 2155 2156 /** 2157 * devm_device_remove_groups - remove a list of managed groups 2158 * 2159 * @dev: The device for the groups to be removed from 2160 * @groups: NULL terminated list of groups to be removed 2161 * 2162 * If groups is not NULL, remove the specified groups from the device. 2163 */ 2164 void devm_device_remove_groups(struct device *dev, 2165 const struct attribute_group **groups) 2166 { 2167 WARN_ON(devres_release(dev, devm_attr_groups_remove, 2168 devm_attr_group_match, 2169 /* cast away const */ (void *)groups)); 2170 } 2171 EXPORT_SYMBOL_GPL(devm_device_remove_groups); 2172 2173 static int device_add_attrs(struct device *dev) 2174 { 2175 struct class *class = dev->class; 2176 const struct device_type *type = dev->type; 2177 int error; 2178 2179 if (class) { 2180 error = device_add_groups(dev, class->dev_groups); 2181 if (error) 2182 return error; 2183 } 2184 2185 if (type) { 2186 error = device_add_groups(dev, type->groups); 2187 if (error) 2188 goto err_remove_class_groups; 2189 } 2190 2191 error = device_add_groups(dev, dev->groups); 2192 if (error) 2193 goto err_remove_type_groups; 2194 2195 if (device_supports_offline(dev) && !dev->offline_disabled) { 2196 error = device_create_file(dev, &dev_attr_online); 2197 if (error) 2198 goto err_remove_dev_groups; 2199 } 2200 2201 if (fw_devlink_flags && !fw_devlink_is_permissive()) { 2202 error = device_create_file(dev, &dev_attr_waiting_for_supplier); 2203 if (error) 2204 goto err_remove_dev_online; 2205 } 2206 2207 return 0; 2208 2209 err_remove_dev_online: 2210 device_remove_file(dev, &dev_attr_online); 2211 err_remove_dev_groups: 2212 device_remove_groups(dev, dev->groups); 2213 err_remove_type_groups: 2214 if (type) 2215 device_remove_groups(dev, type->groups); 2216 err_remove_class_groups: 2217 if (class) 2218 device_remove_groups(dev, class->dev_groups); 2219 2220 return error; 2221 } 2222 2223 static void device_remove_attrs(struct device *dev) 2224 { 2225 struct class *class = dev->class; 2226 const struct device_type *type = dev->type; 2227 2228 device_remove_file(dev, &dev_attr_waiting_for_supplier); 2229 device_remove_file(dev, &dev_attr_online); 2230 device_remove_groups(dev, dev->groups); 2231 2232 if (type) 2233 device_remove_groups(dev, type->groups); 2234 2235 if (class) 2236 device_remove_groups(dev, class->dev_groups); 2237 } 2238 2239 static ssize_t dev_show(struct device *dev, struct device_attribute *attr, 2240 char *buf) 2241 { 2242 return print_dev_t(buf, dev->devt); 2243 } 2244 static DEVICE_ATTR_RO(dev); 2245 2246 /* /sys/devices/ */ 2247 struct kset *devices_kset; 2248 2249 /** 2250 * devices_kset_move_before - Move device in the devices_kset's list. 2251 * @deva: Device to move. 2252 * @devb: Device @deva should come before. 2253 */ 2254 static void devices_kset_move_before(struct device *deva, struct device *devb) 2255 { 2256 if (!devices_kset) 2257 return; 2258 pr_debug("devices_kset: Moving %s before %s\n", 2259 dev_name(deva), dev_name(devb)); 2260 spin_lock(&devices_kset->list_lock); 2261 list_move_tail(&deva->kobj.entry, &devb->kobj.entry); 2262 spin_unlock(&devices_kset->list_lock); 2263 } 2264 2265 /** 2266 * devices_kset_move_after - Move device in the devices_kset's list. 2267 * @deva: Device to move 2268 * @devb: Device @deva should come after. 2269 */ 2270 static void devices_kset_move_after(struct device *deva, struct device *devb) 2271 { 2272 if (!devices_kset) 2273 return; 2274 pr_debug("devices_kset: Moving %s after %s\n", 2275 dev_name(deva), dev_name(devb)); 2276 spin_lock(&devices_kset->list_lock); 2277 list_move(&deva->kobj.entry, &devb->kobj.entry); 2278 spin_unlock(&devices_kset->list_lock); 2279 } 2280 2281 /** 2282 * devices_kset_move_last - move the device to the end of devices_kset's list. 2283 * @dev: device to move 2284 */ 2285 void devices_kset_move_last(struct device *dev) 2286 { 2287 if (!devices_kset) 2288 return; 2289 pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev)); 2290 spin_lock(&devices_kset->list_lock); 2291 list_move_tail(&dev->kobj.entry, &devices_kset->list); 2292 spin_unlock(&devices_kset->list_lock); 2293 } 2294 2295 /** 2296 * device_create_file - create sysfs attribute file for device. 2297 * @dev: device. 2298 * @attr: device attribute descriptor. 2299 */ 2300 int device_create_file(struct device *dev, 2301 const struct device_attribute *attr) 2302 { 2303 int error = 0; 2304 2305 if (dev) { 2306 WARN(((attr->attr.mode & S_IWUGO) && !attr->store), 2307 "Attribute %s: write permission without 'store'\n", 2308 attr->attr.name); 2309 WARN(((attr->attr.mode & S_IRUGO) && !attr->show), 2310 "Attribute %s: read permission without 'show'\n", 2311 attr->attr.name); 2312 error = sysfs_create_file(&dev->kobj, &attr->attr); 2313 } 2314 2315 return error; 2316 } 2317 EXPORT_SYMBOL_GPL(device_create_file); 2318 2319 /** 2320 * device_remove_file - remove sysfs attribute file. 2321 * @dev: device. 2322 * @attr: device attribute descriptor. 2323 */ 2324 void device_remove_file(struct device *dev, 2325 const struct device_attribute *attr) 2326 { 2327 if (dev) 2328 sysfs_remove_file(&dev->kobj, &attr->attr); 2329 } 2330 EXPORT_SYMBOL_GPL(device_remove_file); 2331 2332 /** 2333 * device_remove_file_self - remove sysfs attribute file from its own method. 2334 * @dev: device. 2335 * @attr: device attribute descriptor. 2336 * 2337 * See kernfs_remove_self() for details. 2338 */ 2339 bool device_remove_file_self(struct device *dev, 2340 const struct device_attribute *attr) 2341 { 2342 if (dev) 2343 return sysfs_remove_file_self(&dev->kobj, &attr->attr); 2344 else 2345 return false; 2346 } 2347 EXPORT_SYMBOL_GPL(device_remove_file_self); 2348 2349 /** 2350 * device_create_bin_file - create sysfs binary attribute file for device. 2351 * @dev: device. 2352 * @attr: device binary attribute descriptor. 2353 */ 2354 int device_create_bin_file(struct device *dev, 2355 const struct bin_attribute *attr) 2356 { 2357 int error = -EINVAL; 2358 if (dev) 2359 error = sysfs_create_bin_file(&dev->kobj, attr); 2360 return error; 2361 } 2362 EXPORT_SYMBOL_GPL(device_create_bin_file); 2363 2364 /** 2365 * device_remove_bin_file - remove sysfs binary attribute file 2366 * @dev: device. 2367 * @attr: device binary attribute descriptor. 2368 */ 2369 void device_remove_bin_file(struct device *dev, 2370 const struct bin_attribute *attr) 2371 { 2372 if (dev) 2373 sysfs_remove_bin_file(&dev->kobj, attr); 2374 } 2375 EXPORT_SYMBOL_GPL(device_remove_bin_file); 2376 2377 static void klist_children_get(struct klist_node *n) 2378 { 2379 struct device_private *p = to_device_private_parent(n); 2380 struct device *dev = p->device; 2381 2382 get_device(dev); 2383 } 2384 2385 static void klist_children_put(struct klist_node *n) 2386 { 2387 struct device_private *p = to_device_private_parent(n); 2388 struct device *dev = p->device; 2389 2390 put_device(dev); 2391 } 2392 2393 /** 2394 * device_initialize - init device structure. 2395 * @dev: device. 2396 * 2397 * This prepares the device for use by other layers by initializing 2398 * its fields. 2399 * It is the first half of device_register(), if called by 2400 * that function, though it can also be called separately, so one 2401 * may use @dev's fields. In particular, get_device()/put_device() 2402 * may be used for reference counting of @dev after calling this 2403 * function. 2404 * 2405 * All fields in @dev must be initialized by the caller to 0, except 2406 * for those explicitly set to some other value. The simplest 2407 * approach is to use kzalloc() to allocate the structure containing 2408 * @dev. 2409 * 2410 * NOTE: Use put_device() to give up your reference instead of freeing 2411 * @dev directly once you have called this function. 2412 */ 2413 void device_initialize(struct device *dev) 2414 { 2415 dev->kobj.kset = devices_kset; 2416 kobject_init(&dev->kobj, &device_ktype); 2417 INIT_LIST_HEAD(&dev->dma_pools); 2418 mutex_init(&dev->mutex); 2419 #ifdef CONFIG_PROVE_LOCKING 2420 mutex_init(&dev->lockdep_mutex); 2421 #endif 2422 lockdep_set_novalidate_class(&dev->mutex); 2423 spin_lock_init(&dev->devres_lock); 2424 INIT_LIST_HEAD(&dev->devres_head); 2425 device_pm_init(dev); 2426 set_dev_node(dev, -1); 2427 #ifdef CONFIG_GENERIC_MSI_IRQ 2428 INIT_LIST_HEAD(&dev->msi_list); 2429 #endif 2430 INIT_LIST_HEAD(&dev->links.consumers); 2431 INIT_LIST_HEAD(&dev->links.suppliers); 2432 INIT_LIST_HEAD(&dev->links.needs_suppliers); 2433 INIT_LIST_HEAD(&dev->links.defer_hook); 2434 dev->links.status = DL_DEV_NO_DRIVER; 2435 } 2436 EXPORT_SYMBOL_GPL(device_initialize); 2437 2438 struct kobject *virtual_device_parent(struct device *dev) 2439 { 2440 static struct kobject *virtual_dir = NULL; 2441 2442 if (!virtual_dir) 2443 virtual_dir = kobject_create_and_add("virtual", 2444 &devices_kset->kobj); 2445 2446 return virtual_dir; 2447 } 2448 2449 struct class_dir { 2450 struct kobject kobj; 2451 struct class *class; 2452 }; 2453 2454 #define to_class_dir(obj) container_of(obj, struct class_dir, kobj) 2455 2456 static void class_dir_release(struct kobject *kobj) 2457 { 2458 struct class_dir *dir = to_class_dir(kobj); 2459 kfree(dir); 2460 } 2461 2462 static const 2463 struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj) 2464 { 2465 struct class_dir *dir = to_class_dir(kobj); 2466 return dir->class->ns_type; 2467 } 2468 2469 static struct kobj_type class_dir_ktype = { 2470 .release = class_dir_release, 2471 .sysfs_ops = &kobj_sysfs_ops, 2472 .child_ns_type = class_dir_child_ns_type 2473 }; 2474 2475 static struct kobject * 2476 class_dir_create_and_add(struct class *class, struct kobject *parent_kobj) 2477 { 2478 struct class_dir *dir; 2479 int retval; 2480 2481 dir = kzalloc(sizeof(*dir), GFP_KERNEL); 2482 if (!dir) 2483 return ERR_PTR(-ENOMEM); 2484 2485 dir->class = class; 2486 kobject_init(&dir->kobj, &class_dir_ktype); 2487 2488 dir->kobj.kset = &class->p->glue_dirs; 2489 2490 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name); 2491 if (retval < 0) { 2492 kobject_put(&dir->kobj); 2493 return ERR_PTR(retval); 2494 } 2495 return &dir->kobj; 2496 } 2497 2498 static DEFINE_MUTEX(gdp_mutex); 2499 2500 static struct kobject *get_device_parent(struct device *dev, 2501 struct device *parent) 2502 { 2503 if (dev->class) { 2504 struct kobject *kobj = NULL; 2505 struct kobject *parent_kobj; 2506 struct kobject *k; 2507 2508 #ifdef CONFIG_BLOCK 2509 /* block disks show up in /sys/block */ 2510 if (sysfs_deprecated && dev->class == &block_class) { 2511 if (parent && parent->class == &block_class) 2512 return &parent->kobj; 2513 return &block_class.p->subsys.kobj; 2514 } 2515 #endif 2516 2517 /* 2518 * If we have no parent, we live in "virtual". 2519 * Class-devices with a non class-device as parent, live 2520 * in a "glue" directory to prevent namespace collisions. 2521 */ 2522 if (parent == NULL) 2523 parent_kobj = virtual_device_parent(dev); 2524 else if (parent->class && !dev->class->ns_type) 2525 return &parent->kobj; 2526 else 2527 parent_kobj = &parent->kobj; 2528 2529 mutex_lock(&gdp_mutex); 2530 2531 /* find our class-directory at the parent and reference it */ 2532 spin_lock(&dev->class->p->glue_dirs.list_lock); 2533 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry) 2534 if (k->parent == parent_kobj) { 2535 kobj = kobject_get(k); 2536 break; 2537 } 2538 spin_unlock(&dev->class->p->glue_dirs.list_lock); 2539 if (kobj) { 2540 mutex_unlock(&gdp_mutex); 2541 return kobj; 2542 } 2543 2544 /* or create a new class-directory at the parent device */ 2545 k = class_dir_create_and_add(dev->class, parent_kobj); 2546 /* do not emit an uevent for this simple "glue" directory */ 2547 mutex_unlock(&gdp_mutex); 2548 return k; 2549 } 2550 2551 /* subsystems can specify a default root directory for their devices */ 2552 if (!parent && dev->bus && dev->bus->dev_root) 2553 return &dev->bus->dev_root->kobj; 2554 2555 if (parent) 2556 return &parent->kobj; 2557 return NULL; 2558 } 2559 2560 static inline bool live_in_glue_dir(struct kobject *kobj, 2561 struct device *dev) 2562 { 2563 if (!kobj || !dev->class || 2564 kobj->kset != &dev->class->p->glue_dirs) 2565 return false; 2566 return true; 2567 } 2568 2569 static inline struct kobject *get_glue_dir(struct device *dev) 2570 { 2571 return dev->kobj.parent; 2572 } 2573 2574 /* 2575 * make sure cleaning up dir as the last step, we need to make 2576 * sure .release handler of kobject is run with holding the 2577 * global lock 2578 */ 2579 static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir) 2580 { 2581 unsigned int ref; 2582 2583 /* see if we live in a "glue" directory */ 2584 if (!live_in_glue_dir(glue_dir, dev)) 2585 return; 2586 2587 mutex_lock(&gdp_mutex); 2588 /** 2589 * There is a race condition between removing glue directory 2590 * and adding a new device under the glue directory. 2591 * 2592 * CPU1: CPU2: 2593 * 2594 * device_add() 2595 * get_device_parent() 2596 * class_dir_create_and_add() 2597 * kobject_add_internal() 2598 * create_dir() // create glue_dir 2599 * 2600 * device_add() 2601 * get_device_parent() 2602 * kobject_get() // get glue_dir 2603 * 2604 * device_del() 2605 * cleanup_glue_dir() 2606 * kobject_del(glue_dir) 2607 * 2608 * kobject_add() 2609 * kobject_add_internal() 2610 * create_dir() // in glue_dir 2611 * sysfs_create_dir_ns() 2612 * kernfs_create_dir_ns(sd) 2613 * 2614 * sysfs_remove_dir() // glue_dir->sd=NULL 2615 * sysfs_put() // free glue_dir->sd 2616 * 2617 * // sd is freed 2618 * kernfs_new_node(sd) 2619 * kernfs_get(glue_dir) 2620 * kernfs_add_one() 2621 * kernfs_put() 2622 * 2623 * Before CPU1 remove last child device under glue dir, if CPU2 add 2624 * a new device under glue dir, the glue_dir kobject reference count 2625 * will be increase to 2 in kobject_get(k). And CPU2 has been called 2626 * kernfs_create_dir_ns(). Meanwhile, CPU1 call sysfs_remove_dir() 2627 * and sysfs_put(). This result in glue_dir->sd is freed. 2628 * 2629 * Then the CPU2 will see a stale "empty" but still potentially used 2630 * glue dir around in kernfs_new_node(). 2631 * 2632 * In order to avoid this happening, we also should make sure that 2633 * kernfs_node for glue_dir is released in CPU1 only when refcount 2634 * for glue_dir kobj is 1. 2635 */ 2636 ref = kref_read(&glue_dir->kref); 2637 if (!kobject_has_children(glue_dir) && !--ref) 2638 kobject_del(glue_dir); 2639 kobject_put(glue_dir); 2640 mutex_unlock(&gdp_mutex); 2641 } 2642 2643 static int device_add_class_symlinks(struct device *dev) 2644 { 2645 struct device_node *of_node = dev_of_node(dev); 2646 int error; 2647 2648 if (of_node) { 2649 error = sysfs_create_link(&dev->kobj, of_node_kobj(of_node), "of_node"); 2650 if (error) 2651 dev_warn(dev, "Error %d creating of_node link\n",error); 2652 /* An error here doesn't warrant bringing down the device */ 2653 } 2654 2655 if (!dev->class) 2656 return 0; 2657 2658 error = sysfs_create_link(&dev->kobj, 2659 &dev->class->p->subsys.kobj, 2660 "subsystem"); 2661 if (error) 2662 goto out_devnode; 2663 2664 if (dev->parent && device_is_not_partition(dev)) { 2665 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj, 2666 "device"); 2667 if (error) 2668 goto out_subsys; 2669 } 2670 2671 #ifdef CONFIG_BLOCK 2672 /* /sys/block has directories and does not need symlinks */ 2673 if (sysfs_deprecated && dev->class == &block_class) 2674 return 0; 2675 #endif 2676 2677 /* link in the class directory pointing to the device */ 2678 error = sysfs_create_link(&dev->class->p->subsys.kobj, 2679 &dev->kobj, dev_name(dev)); 2680 if (error) 2681 goto out_device; 2682 2683 return 0; 2684 2685 out_device: 2686 sysfs_remove_link(&dev->kobj, "device"); 2687 2688 out_subsys: 2689 sysfs_remove_link(&dev->kobj, "subsystem"); 2690 out_devnode: 2691 sysfs_remove_link(&dev->kobj, "of_node"); 2692 return error; 2693 } 2694 2695 static void device_remove_class_symlinks(struct device *dev) 2696 { 2697 if (dev_of_node(dev)) 2698 sysfs_remove_link(&dev->kobj, "of_node"); 2699 2700 if (!dev->class) 2701 return; 2702 2703 if (dev->parent && device_is_not_partition(dev)) 2704 sysfs_remove_link(&dev->kobj, "device"); 2705 sysfs_remove_link(&dev->kobj, "subsystem"); 2706 #ifdef CONFIG_BLOCK 2707 if (sysfs_deprecated && dev->class == &block_class) 2708 return; 2709 #endif 2710 sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev)); 2711 } 2712 2713 /** 2714 * dev_set_name - set a device name 2715 * @dev: device 2716 * @fmt: format string for the device's name 2717 */ 2718 int dev_set_name(struct device *dev, const char *fmt, ...) 2719 { 2720 va_list vargs; 2721 int err; 2722 2723 va_start(vargs, fmt); 2724 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs); 2725 va_end(vargs); 2726 return err; 2727 } 2728 EXPORT_SYMBOL_GPL(dev_set_name); 2729 2730 /** 2731 * device_to_dev_kobj - select a /sys/dev/ directory for the device 2732 * @dev: device 2733 * 2734 * By default we select char/ for new entries. Setting class->dev_obj 2735 * to NULL prevents an entry from being created. class->dev_kobj must 2736 * be set (or cleared) before any devices are registered to the class 2737 * otherwise device_create_sys_dev_entry() and 2738 * device_remove_sys_dev_entry() will disagree about the presence of 2739 * the link. 2740 */ 2741 static struct kobject *device_to_dev_kobj(struct device *dev) 2742 { 2743 struct kobject *kobj; 2744 2745 if (dev->class) 2746 kobj = dev->class->dev_kobj; 2747 else 2748 kobj = sysfs_dev_char_kobj; 2749 2750 return kobj; 2751 } 2752 2753 static int device_create_sys_dev_entry(struct device *dev) 2754 { 2755 struct kobject *kobj = device_to_dev_kobj(dev); 2756 int error = 0; 2757 char devt_str[15]; 2758 2759 if (kobj) { 2760 format_dev_t(devt_str, dev->devt); 2761 error = sysfs_create_link(kobj, &dev->kobj, devt_str); 2762 } 2763 2764 return error; 2765 } 2766 2767 static void device_remove_sys_dev_entry(struct device *dev) 2768 { 2769 struct kobject *kobj = device_to_dev_kobj(dev); 2770 char devt_str[15]; 2771 2772 if (kobj) { 2773 format_dev_t(devt_str, dev->devt); 2774 sysfs_remove_link(kobj, devt_str); 2775 } 2776 } 2777 2778 static int device_private_init(struct device *dev) 2779 { 2780 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL); 2781 if (!dev->p) 2782 return -ENOMEM; 2783 dev->p->device = dev; 2784 klist_init(&dev->p->klist_children, klist_children_get, 2785 klist_children_put); 2786 INIT_LIST_HEAD(&dev->p->deferred_probe); 2787 return 0; 2788 } 2789 2790 /** 2791 * device_add - add device to device hierarchy. 2792 * @dev: device. 2793 * 2794 * This is part 2 of device_register(), though may be called 2795 * separately _iff_ device_initialize() has been called separately. 2796 * 2797 * This adds @dev to the kobject hierarchy via kobject_add(), adds it 2798 * to the global and sibling lists for the device, then 2799 * adds it to the other relevant subsystems of the driver model. 2800 * 2801 * Do not call this routine or device_register() more than once for 2802 * any device structure. The driver model core is not designed to work 2803 * with devices that get unregistered and then spring back to life. 2804 * (Among other things, it's very hard to guarantee that all references 2805 * to the previous incarnation of @dev have been dropped.) Allocate 2806 * and register a fresh new struct device instead. 2807 * 2808 * NOTE: _Never_ directly free @dev after calling this function, even 2809 * if it returned an error! Always use put_device() to give up your 2810 * reference instead. 2811 * 2812 * Rule of thumb is: if device_add() succeeds, you should call 2813 * device_del() when you want to get rid of it. If device_add() has 2814 * *not* succeeded, use *only* put_device() to drop the reference 2815 * count. 2816 */ 2817 int device_add(struct device *dev) 2818 { 2819 struct device *parent; 2820 struct kobject *kobj; 2821 struct class_interface *class_intf; 2822 int error = -EINVAL; 2823 struct kobject *glue_dir = NULL; 2824 2825 dev = get_device(dev); 2826 if (!dev) 2827 goto done; 2828 2829 if (!dev->p) { 2830 error = device_private_init(dev); 2831 if (error) 2832 goto done; 2833 } 2834 2835 /* 2836 * for statically allocated devices, which should all be converted 2837 * some day, we need to initialize the name. We prevent reading back 2838 * the name, and force the use of dev_name() 2839 */ 2840 if (dev->init_name) { 2841 dev_set_name(dev, "%s", dev->init_name); 2842 dev->init_name = NULL; 2843 } 2844 2845 /* subsystems can specify simple device enumeration */ 2846 if (!dev_name(dev) && dev->bus && dev->bus->dev_name) 2847 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id); 2848 2849 if (!dev_name(dev)) { 2850 error = -EINVAL; 2851 goto name_error; 2852 } 2853 2854 pr_debug("device: '%s': %s\n", dev_name(dev), __func__); 2855 2856 parent = get_device(dev->parent); 2857 kobj = get_device_parent(dev, parent); 2858 if (IS_ERR(kobj)) { 2859 error = PTR_ERR(kobj); 2860 goto parent_error; 2861 } 2862 if (kobj) 2863 dev->kobj.parent = kobj; 2864 2865 /* use parent numa_node */ 2866 if (parent && (dev_to_node(dev) == NUMA_NO_NODE)) 2867 set_dev_node(dev, dev_to_node(parent)); 2868 2869 /* first, register with generic layer. */ 2870 /* we require the name to be set before, and pass NULL */ 2871 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL); 2872 if (error) { 2873 glue_dir = get_glue_dir(dev); 2874 goto Error; 2875 } 2876 2877 /* notify platform of device entry */ 2878 error = device_platform_notify(dev, KOBJ_ADD); 2879 if (error) 2880 goto platform_error; 2881 2882 error = device_create_file(dev, &dev_attr_uevent); 2883 if (error) 2884 goto attrError; 2885 2886 error = device_add_class_symlinks(dev); 2887 if (error) 2888 goto SymlinkError; 2889 error = device_add_attrs(dev); 2890 if (error) 2891 goto AttrsError; 2892 error = bus_add_device(dev); 2893 if (error) 2894 goto BusError; 2895 error = dpm_sysfs_add(dev); 2896 if (error) 2897 goto DPMError; 2898 device_pm_add(dev); 2899 2900 if (MAJOR(dev->devt)) { 2901 error = device_create_file(dev, &dev_attr_dev); 2902 if (error) 2903 goto DevAttrError; 2904 2905 error = device_create_sys_dev_entry(dev); 2906 if (error) 2907 goto SysEntryError; 2908 2909 devtmpfs_create_node(dev); 2910 } 2911 2912 /* Notify clients of device addition. This call must come 2913 * after dpm_sysfs_add() and before kobject_uevent(). 2914 */ 2915 if (dev->bus) 2916 blocking_notifier_call_chain(&dev->bus->p->bus_notifier, 2917 BUS_NOTIFY_ADD_DEVICE, dev); 2918 2919 kobject_uevent(&dev->kobj, KOBJ_ADD); 2920 2921 /* 2922 * Check if any of the other devices (consumers) have been waiting for 2923 * this device (supplier) to be added so that they can create a device 2924 * link to it. 2925 * 2926 * This needs to happen after device_pm_add() because device_link_add() 2927 * requires the supplier be registered before it's called. 2928 * 2929 * But this also needs to happen before bus_probe_device() to make sure 2930 * waiting consumers can link to it before the driver is bound to the 2931 * device and the driver sync_state callback is called for this device. 2932 */ 2933 if (dev->fwnode && !dev->fwnode->dev) { 2934 dev->fwnode->dev = dev; 2935 fw_devlink_link_device(dev); 2936 } 2937 2938 bus_probe_device(dev); 2939 if (parent) 2940 klist_add_tail(&dev->p->knode_parent, 2941 &parent->p->klist_children); 2942 2943 if (dev->class) { 2944 mutex_lock(&dev->class->p->mutex); 2945 /* tie the class to the device */ 2946 klist_add_tail(&dev->p->knode_class, 2947 &dev->class->p->klist_devices); 2948 2949 /* notify any interfaces that the device is here */ 2950 list_for_each_entry(class_intf, 2951 &dev->class->p->interfaces, node) 2952 if (class_intf->add_dev) 2953 class_intf->add_dev(dev, class_intf); 2954 mutex_unlock(&dev->class->p->mutex); 2955 } 2956 done: 2957 put_device(dev); 2958 return error; 2959 SysEntryError: 2960 if (MAJOR(dev->devt)) 2961 device_remove_file(dev, &dev_attr_dev); 2962 DevAttrError: 2963 device_pm_remove(dev); 2964 dpm_sysfs_remove(dev); 2965 DPMError: 2966 bus_remove_device(dev); 2967 BusError: 2968 device_remove_attrs(dev); 2969 AttrsError: 2970 device_remove_class_symlinks(dev); 2971 SymlinkError: 2972 device_remove_file(dev, &dev_attr_uevent); 2973 attrError: 2974 device_platform_notify(dev, KOBJ_REMOVE); 2975 platform_error: 2976 kobject_uevent(&dev->kobj, KOBJ_REMOVE); 2977 glue_dir = get_glue_dir(dev); 2978 kobject_del(&dev->kobj); 2979 Error: 2980 cleanup_glue_dir(dev, glue_dir); 2981 parent_error: 2982 put_device(parent); 2983 name_error: 2984 kfree(dev->p); 2985 dev->p = NULL; 2986 goto done; 2987 } 2988 EXPORT_SYMBOL_GPL(device_add); 2989 2990 /** 2991 * device_register - register a device with the system. 2992 * @dev: pointer to the device structure 2993 * 2994 * This happens in two clean steps - initialize the device 2995 * and add it to the system. The two steps can be called 2996 * separately, but this is the easiest and most common. 2997 * I.e. you should only call the two helpers separately if 2998 * have a clearly defined need to use and refcount the device 2999 * before it is added to the hierarchy. 3000 * 3001 * For more information, see the kerneldoc for device_initialize() 3002 * and device_add(). 3003 * 3004 * NOTE: _Never_ directly free @dev after calling this function, even 3005 * if it returned an error! Always use put_device() to give up the 3006 * reference initialized in this function instead. 3007 */ 3008 int device_register(struct device *dev) 3009 { 3010 device_initialize(dev); 3011 return device_add(dev); 3012 } 3013 EXPORT_SYMBOL_GPL(device_register); 3014 3015 /** 3016 * get_device - increment reference count for device. 3017 * @dev: device. 3018 * 3019 * This simply forwards the call to kobject_get(), though 3020 * we do take care to provide for the case that we get a NULL 3021 * pointer passed in. 3022 */ 3023 struct device *get_device(struct device *dev) 3024 { 3025 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL; 3026 } 3027 EXPORT_SYMBOL_GPL(get_device); 3028 3029 /** 3030 * put_device - decrement reference count. 3031 * @dev: device in question. 3032 */ 3033 void put_device(struct device *dev) 3034 { 3035 /* might_sleep(); */ 3036 if (dev) 3037 kobject_put(&dev->kobj); 3038 } 3039 EXPORT_SYMBOL_GPL(put_device); 3040 3041 bool kill_device(struct device *dev) 3042 { 3043 /* 3044 * Require the device lock and set the "dead" flag to guarantee that 3045 * the update behavior is consistent with the other bitfields near 3046 * it and that we cannot have an asynchronous probe routine trying 3047 * to run while we are tearing out the bus/class/sysfs from 3048 * underneath the device. 3049 */ 3050 lockdep_assert_held(&dev->mutex); 3051 3052 if (dev->p->dead) 3053 return false; 3054 dev->p->dead = true; 3055 return true; 3056 } 3057 EXPORT_SYMBOL_GPL(kill_device); 3058 3059 /** 3060 * device_del - delete device from system. 3061 * @dev: device. 3062 * 3063 * This is the first part of the device unregistration 3064 * sequence. This removes the device from the lists we control 3065 * from here, has it removed from the other driver model 3066 * subsystems it was added to in device_add(), and removes it 3067 * from the kobject hierarchy. 3068 * 3069 * NOTE: this should be called manually _iff_ device_add() was 3070 * also called manually. 3071 */ 3072 void device_del(struct device *dev) 3073 { 3074 struct device *parent = dev->parent; 3075 struct kobject *glue_dir = NULL; 3076 struct class_interface *class_intf; 3077 unsigned int noio_flag; 3078 3079 device_lock(dev); 3080 kill_device(dev); 3081 device_unlock(dev); 3082 3083 if (dev->fwnode && dev->fwnode->dev == dev) 3084 dev->fwnode->dev = NULL; 3085 3086 /* Notify clients of device removal. This call must come 3087 * before dpm_sysfs_remove(). 3088 */ 3089 noio_flag = memalloc_noio_save(); 3090 if (dev->bus) 3091 blocking_notifier_call_chain(&dev->bus->p->bus_notifier, 3092 BUS_NOTIFY_DEL_DEVICE, dev); 3093 3094 dpm_sysfs_remove(dev); 3095 if (parent) 3096 klist_del(&dev->p->knode_parent); 3097 if (MAJOR(dev->devt)) { 3098 devtmpfs_delete_node(dev); 3099 device_remove_sys_dev_entry(dev); 3100 device_remove_file(dev, &dev_attr_dev); 3101 } 3102 if (dev->class) { 3103 device_remove_class_symlinks(dev); 3104 3105 mutex_lock(&dev->class->p->mutex); 3106 /* notify any interfaces that the device is now gone */ 3107 list_for_each_entry(class_intf, 3108 &dev->class->p->interfaces, node) 3109 if (class_intf->remove_dev) 3110 class_intf->remove_dev(dev, class_intf); 3111 /* remove the device from the class list */ 3112 klist_del(&dev->p->knode_class); 3113 mutex_unlock(&dev->class->p->mutex); 3114 } 3115 device_remove_file(dev, &dev_attr_uevent); 3116 device_remove_attrs(dev); 3117 bus_remove_device(dev); 3118 device_pm_remove(dev); 3119 driver_deferred_probe_del(dev); 3120 device_platform_notify(dev, KOBJ_REMOVE); 3121 device_remove_properties(dev); 3122 device_links_purge(dev); 3123 3124 if (dev->bus) 3125 blocking_notifier_call_chain(&dev->bus->p->bus_notifier, 3126 BUS_NOTIFY_REMOVED_DEVICE, dev); 3127 kobject_uevent(&dev->kobj, KOBJ_REMOVE); 3128 glue_dir = get_glue_dir(dev); 3129 kobject_del(&dev->kobj); 3130 cleanup_glue_dir(dev, glue_dir); 3131 memalloc_noio_restore(noio_flag); 3132 put_device(parent); 3133 } 3134 EXPORT_SYMBOL_GPL(device_del); 3135 3136 /** 3137 * device_unregister - unregister device from system. 3138 * @dev: device going away. 3139 * 3140 * We do this in two parts, like we do device_register(). First, 3141 * we remove it from all the subsystems with device_del(), then 3142 * we decrement the reference count via put_device(). If that 3143 * is the final reference count, the device will be cleaned up 3144 * via device_release() above. Otherwise, the structure will 3145 * stick around until the final reference to the device is dropped. 3146 */ 3147 void device_unregister(struct device *dev) 3148 { 3149 pr_debug("device: '%s': %s\n", dev_name(dev), __func__); 3150 device_del(dev); 3151 put_device(dev); 3152 } 3153 EXPORT_SYMBOL_GPL(device_unregister); 3154 3155 static struct device *prev_device(struct klist_iter *i) 3156 { 3157 struct klist_node *n = klist_prev(i); 3158 struct device *dev = NULL; 3159 struct device_private *p; 3160 3161 if (n) { 3162 p = to_device_private_parent(n); 3163 dev = p->device; 3164 } 3165 return dev; 3166 } 3167 3168 static struct device *next_device(struct klist_iter *i) 3169 { 3170 struct klist_node *n = klist_next(i); 3171 struct device *dev = NULL; 3172 struct device_private *p; 3173 3174 if (n) { 3175 p = to_device_private_parent(n); 3176 dev = p->device; 3177 } 3178 return dev; 3179 } 3180 3181 /** 3182 * device_get_devnode - path of device node file 3183 * @dev: device 3184 * @mode: returned file access mode 3185 * @uid: returned file owner 3186 * @gid: returned file group 3187 * @tmp: possibly allocated string 3188 * 3189 * Return the relative path of a possible device node. 3190 * Non-default names may need to allocate a memory to compose 3191 * a name. This memory is returned in tmp and needs to be 3192 * freed by the caller. 3193 */ 3194 const char *device_get_devnode(struct device *dev, 3195 umode_t *mode, kuid_t *uid, kgid_t *gid, 3196 const char **tmp) 3197 { 3198 char *s; 3199 3200 *tmp = NULL; 3201 3202 /* the device type may provide a specific name */ 3203 if (dev->type && dev->type->devnode) 3204 *tmp = dev->type->devnode(dev, mode, uid, gid); 3205 if (*tmp) 3206 return *tmp; 3207 3208 /* the class may provide a specific name */ 3209 if (dev->class && dev->class->devnode) 3210 *tmp = dev->class->devnode(dev, mode); 3211 if (*tmp) 3212 return *tmp; 3213 3214 /* return name without allocation, tmp == NULL */ 3215 if (strchr(dev_name(dev), '!') == NULL) 3216 return dev_name(dev); 3217 3218 /* replace '!' in the name with '/' */ 3219 s = kstrdup(dev_name(dev), GFP_KERNEL); 3220 if (!s) 3221 return NULL; 3222 strreplace(s, '!', '/'); 3223 return *tmp = s; 3224 } 3225 3226 /** 3227 * device_for_each_child - device child iterator. 3228 * @parent: parent struct device. 3229 * @fn: function to be called for each device. 3230 * @data: data for the callback. 3231 * 3232 * Iterate over @parent's child devices, and call @fn for each, 3233 * passing it @data. 3234 * 3235 * We check the return of @fn each time. If it returns anything 3236 * other than 0, we break out and return that value. 3237 */ 3238 int device_for_each_child(struct device *parent, void *data, 3239 int (*fn)(struct device *dev, void *data)) 3240 { 3241 struct klist_iter i; 3242 struct device *child; 3243 int error = 0; 3244 3245 if (!parent->p) 3246 return 0; 3247 3248 klist_iter_init(&parent->p->klist_children, &i); 3249 while (!error && (child = next_device(&i))) 3250 error = fn(child, data); 3251 klist_iter_exit(&i); 3252 return error; 3253 } 3254 EXPORT_SYMBOL_GPL(device_for_each_child); 3255 3256 /** 3257 * device_for_each_child_reverse - device child iterator in reversed order. 3258 * @parent: parent struct device. 3259 * @fn: function to be called for each device. 3260 * @data: data for the callback. 3261 * 3262 * Iterate over @parent's child devices, and call @fn for each, 3263 * passing it @data. 3264 * 3265 * We check the return of @fn each time. If it returns anything 3266 * other than 0, we break out and return that value. 3267 */ 3268 int device_for_each_child_reverse(struct device *parent, void *data, 3269 int (*fn)(struct device *dev, void *data)) 3270 { 3271 struct klist_iter i; 3272 struct device *child; 3273 int error = 0; 3274 3275 if (!parent->p) 3276 return 0; 3277 3278 klist_iter_init(&parent->p->klist_children, &i); 3279 while ((child = prev_device(&i)) && !error) 3280 error = fn(child, data); 3281 klist_iter_exit(&i); 3282 return error; 3283 } 3284 EXPORT_SYMBOL_GPL(device_for_each_child_reverse); 3285 3286 /** 3287 * device_find_child - device iterator for locating a particular device. 3288 * @parent: parent struct device 3289 * @match: Callback function to check device 3290 * @data: Data to pass to match function 3291 * 3292 * This is similar to the device_for_each_child() function above, but it 3293 * returns a reference to a device that is 'found' for later use, as 3294 * determined by the @match callback. 3295 * 3296 * The callback should return 0 if the device doesn't match and non-zero 3297 * if it does. If the callback returns non-zero and a reference to the 3298 * current device can be obtained, this function will return to the caller 3299 * and not iterate over any more devices. 3300 * 3301 * NOTE: you will need to drop the reference with put_device() after use. 3302 */ 3303 struct device *device_find_child(struct device *parent, void *data, 3304 int (*match)(struct device *dev, void *data)) 3305 { 3306 struct klist_iter i; 3307 struct device *child; 3308 3309 if (!parent) 3310 return NULL; 3311 3312 klist_iter_init(&parent->p->klist_children, &i); 3313 while ((child = next_device(&i))) 3314 if (match(child, data) && get_device(child)) 3315 break; 3316 klist_iter_exit(&i); 3317 return child; 3318 } 3319 EXPORT_SYMBOL_GPL(device_find_child); 3320 3321 /** 3322 * device_find_child_by_name - device iterator for locating a child device. 3323 * @parent: parent struct device 3324 * @name: name of the child device 3325 * 3326 * This is similar to the device_find_child() function above, but it 3327 * returns a reference to a device that has the name @name. 3328 * 3329 * NOTE: you will need to drop the reference with put_device() after use. 3330 */ 3331 struct device *device_find_child_by_name(struct device *parent, 3332 const char *name) 3333 { 3334 struct klist_iter i; 3335 struct device *child; 3336 3337 if (!parent) 3338 return NULL; 3339 3340 klist_iter_init(&parent->p->klist_children, &i); 3341 while ((child = next_device(&i))) 3342 if (sysfs_streq(dev_name(child), name) && get_device(child)) 3343 break; 3344 klist_iter_exit(&i); 3345 return child; 3346 } 3347 EXPORT_SYMBOL_GPL(device_find_child_by_name); 3348 3349 int __init devices_init(void) 3350 { 3351 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL); 3352 if (!devices_kset) 3353 return -ENOMEM; 3354 dev_kobj = kobject_create_and_add("dev", NULL); 3355 if (!dev_kobj) 3356 goto dev_kobj_err; 3357 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj); 3358 if (!sysfs_dev_block_kobj) 3359 goto block_kobj_err; 3360 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj); 3361 if (!sysfs_dev_char_kobj) 3362 goto char_kobj_err; 3363 3364 return 0; 3365 3366 char_kobj_err: 3367 kobject_put(sysfs_dev_block_kobj); 3368 block_kobj_err: 3369 kobject_put(dev_kobj); 3370 dev_kobj_err: 3371 kset_unregister(devices_kset); 3372 return -ENOMEM; 3373 } 3374 3375 static int device_check_offline(struct device *dev, void *not_used) 3376 { 3377 int ret; 3378 3379 ret = device_for_each_child(dev, NULL, device_check_offline); 3380 if (ret) 3381 return ret; 3382 3383 return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0; 3384 } 3385 3386 /** 3387 * device_offline - Prepare the device for hot-removal. 3388 * @dev: Device to be put offline. 3389 * 3390 * Execute the device bus type's .offline() callback, if present, to prepare 3391 * the device for a subsequent hot-removal. If that succeeds, the device must 3392 * not be used until either it is removed or its bus type's .online() callback 3393 * is executed. 3394 * 3395 * Call under device_hotplug_lock. 3396 */ 3397 int device_offline(struct device *dev) 3398 { 3399 int ret; 3400 3401 if (dev->offline_disabled) 3402 return -EPERM; 3403 3404 ret = device_for_each_child(dev, NULL, device_check_offline); 3405 if (ret) 3406 return ret; 3407 3408 device_lock(dev); 3409 if (device_supports_offline(dev)) { 3410 if (dev->offline) { 3411 ret = 1; 3412 } else { 3413 ret = dev->bus->offline(dev); 3414 if (!ret) { 3415 kobject_uevent(&dev->kobj, KOBJ_OFFLINE); 3416 dev->offline = true; 3417 } 3418 } 3419 } 3420 device_unlock(dev); 3421 3422 return ret; 3423 } 3424 3425 /** 3426 * device_online - Put the device back online after successful device_offline(). 3427 * @dev: Device to be put back online. 3428 * 3429 * If device_offline() has been successfully executed for @dev, but the device 3430 * has not been removed subsequently, execute its bus type's .online() callback 3431 * to indicate that the device can be used again. 3432 * 3433 * Call under device_hotplug_lock. 3434 */ 3435 int device_online(struct device *dev) 3436 { 3437 int ret = 0; 3438 3439 device_lock(dev); 3440 if (device_supports_offline(dev)) { 3441 if (dev->offline) { 3442 ret = dev->bus->online(dev); 3443 if (!ret) { 3444 kobject_uevent(&dev->kobj, KOBJ_ONLINE); 3445 dev->offline = false; 3446 } 3447 } else { 3448 ret = 1; 3449 } 3450 } 3451 device_unlock(dev); 3452 3453 return ret; 3454 } 3455 3456 struct root_device { 3457 struct device dev; 3458 struct module *owner; 3459 }; 3460 3461 static inline struct root_device *to_root_device(struct device *d) 3462 { 3463 return container_of(d, struct root_device, dev); 3464 } 3465 3466 static void root_device_release(struct device *dev) 3467 { 3468 kfree(to_root_device(dev)); 3469 } 3470 3471 /** 3472 * __root_device_register - allocate and register a root device 3473 * @name: root device name 3474 * @owner: owner module of the root device, usually THIS_MODULE 3475 * 3476 * This function allocates a root device and registers it 3477 * using device_register(). In order to free the returned 3478 * device, use root_device_unregister(). 3479 * 3480 * Root devices are dummy devices which allow other devices 3481 * to be grouped under /sys/devices. Use this function to 3482 * allocate a root device and then use it as the parent of 3483 * any device which should appear under /sys/devices/{name} 3484 * 3485 * The /sys/devices/{name} directory will also contain a 3486 * 'module' symlink which points to the @owner directory 3487 * in sysfs. 3488 * 3489 * Returns &struct device pointer on success, or ERR_PTR() on error. 3490 * 3491 * Note: You probably want to use root_device_register(). 3492 */ 3493 struct device *__root_device_register(const char *name, struct module *owner) 3494 { 3495 struct root_device *root; 3496 int err = -ENOMEM; 3497 3498 root = kzalloc(sizeof(struct root_device), GFP_KERNEL); 3499 if (!root) 3500 return ERR_PTR(err); 3501 3502 err = dev_set_name(&root->dev, "%s", name); 3503 if (err) { 3504 kfree(root); 3505 return ERR_PTR(err); 3506 } 3507 3508 root->dev.release = root_device_release; 3509 3510 err = device_register(&root->dev); 3511 if (err) { 3512 put_device(&root->dev); 3513 return ERR_PTR(err); 3514 } 3515 3516 #ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */ 3517 if (owner) { 3518 struct module_kobject *mk = &owner->mkobj; 3519 3520 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module"); 3521 if (err) { 3522 device_unregister(&root->dev); 3523 return ERR_PTR(err); 3524 } 3525 root->owner = owner; 3526 } 3527 #endif 3528 3529 return &root->dev; 3530 } 3531 EXPORT_SYMBOL_GPL(__root_device_register); 3532 3533 /** 3534 * root_device_unregister - unregister and free a root device 3535 * @dev: device going away 3536 * 3537 * This function unregisters and cleans up a device that was created by 3538 * root_device_register(). 3539 */ 3540 void root_device_unregister(struct device *dev) 3541 { 3542 struct root_device *root = to_root_device(dev); 3543 3544 if (root->owner) 3545 sysfs_remove_link(&root->dev.kobj, "module"); 3546 3547 device_unregister(dev); 3548 } 3549 EXPORT_SYMBOL_GPL(root_device_unregister); 3550 3551 3552 static void device_create_release(struct device *dev) 3553 { 3554 pr_debug("device: '%s': %s\n", dev_name(dev), __func__); 3555 kfree(dev); 3556 } 3557 3558 static __printf(6, 0) struct device * 3559 device_create_groups_vargs(struct class *class, struct device *parent, 3560 dev_t devt, void *drvdata, 3561 const struct attribute_group **groups, 3562 const char *fmt, va_list args) 3563 { 3564 struct device *dev = NULL; 3565 int retval = -ENODEV; 3566 3567 if (class == NULL || IS_ERR(class)) 3568 goto error; 3569 3570 dev = kzalloc(sizeof(*dev), GFP_KERNEL); 3571 if (!dev) { 3572 retval = -ENOMEM; 3573 goto error; 3574 } 3575 3576 device_initialize(dev); 3577 dev->devt = devt; 3578 dev->class = class; 3579 dev->parent = parent; 3580 dev->groups = groups; 3581 dev->release = device_create_release; 3582 dev_set_drvdata(dev, drvdata); 3583 3584 retval = kobject_set_name_vargs(&dev->kobj, fmt, args); 3585 if (retval) 3586 goto error; 3587 3588 retval = device_add(dev); 3589 if (retval) 3590 goto error; 3591 3592 return dev; 3593 3594 error: 3595 put_device(dev); 3596 return ERR_PTR(retval); 3597 } 3598 3599 /** 3600 * device_create - creates a device and registers it with sysfs 3601 * @class: pointer to the struct class that this device should be registered to 3602 * @parent: pointer to the parent struct device of this new device, if any 3603 * @devt: the dev_t for the char device to be added 3604 * @drvdata: the data to be added to the device for callbacks 3605 * @fmt: string for the device's name 3606 * 3607 * This function can be used by char device classes. A struct device 3608 * will be created in sysfs, registered to the specified class. 3609 * 3610 * A "dev" file will be created, showing the dev_t for the device, if 3611 * the dev_t is not 0,0. 3612 * If a pointer to a parent struct device is passed in, the newly created 3613 * struct device will be a child of that device in sysfs. 3614 * The pointer to the struct device will be returned from the call. 3615 * Any further sysfs files that might be required can be created using this 3616 * pointer. 3617 * 3618 * Returns &struct device pointer on success, or ERR_PTR() on error. 3619 * 3620 * Note: the struct class passed to this function must have previously 3621 * been created with a call to class_create(). 3622 */ 3623 struct device *device_create(struct class *class, struct device *parent, 3624 dev_t devt, void *drvdata, const char *fmt, ...) 3625 { 3626 va_list vargs; 3627 struct device *dev; 3628 3629 va_start(vargs, fmt); 3630 dev = device_create_groups_vargs(class, parent, devt, drvdata, NULL, 3631 fmt, vargs); 3632 va_end(vargs); 3633 return dev; 3634 } 3635 EXPORT_SYMBOL_GPL(device_create); 3636 3637 /** 3638 * device_create_with_groups - creates a device and registers it with sysfs 3639 * @class: pointer to the struct class that this device should be registered to 3640 * @parent: pointer to the parent struct device of this new device, if any 3641 * @devt: the dev_t for the char device to be added 3642 * @drvdata: the data to be added to the device for callbacks 3643 * @groups: NULL-terminated list of attribute groups to be created 3644 * @fmt: string for the device's name 3645 * 3646 * This function can be used by char device classes. A struct device 3647 * will be created in sysfs, registered to the specified class. 3648 * Additional attributes specified in the groups parameter will also 3649 * be created automatically. 3650 * 3651 * A "dev" file will be created, showing the dev_t for the device, if 3652 * the dev_t is not 0,0. 3653 * If a pointer to a parent struct device is passed in, the newly created 3654 * struct device will be a child of that device in sysfs. 3655 * The pointer to the struct device will be returned from the call. 3656 * Any further sysfs files that might be required can be created using this 3657 * pointer. 3658 * 3659 * Returns &struct device pointer on success, or ERR_PTR() on error. 3660 * 3661 * Note: the struct class passed to this function must have previously 3662 * been created with a call to class_create(). 3663 */ 3664 struct device *device_create_with_groups(struct class *class, 3665 struct device *parent, dev_t devt, 3666 void *drvdata, 3667 const struct attribute_group **groups, 3668 const char *fmt, ...) 3669 { 3670 va_list vargs; 3671 struct device *dev; 3672 3673 va_start(vargs, fmt); 3674 dev = device_create_groups_vargs(class, parent, devt, drvdata, groups, 3675 fmt, vargs); 3676 va_end(vargs); 3677 return dev; 3678 } 3679 EXPORT_SYMBOL_GPL(device_create_with_groups); 3680 3681 /** 3682 * device_destroy - removes a device that was created with device_create() 3683 * @class: pointer to the struct class that this device was registered with 3684 * @devt: the dev_t of the device that was previously registered 3685 * 3686 * This call unregisters and cleans up a device that was created with a 3687 * call to device_create(). 3688 */ 3689 void device_destroy(struct class *class, dev_t devt) 3690 { 3691 struct device *dev; 3692 3693 dev = class_find_device_by_devt(class, devt); 3694 if (dev) { 3695 put_device(dev); 3696 device_unregister(dev); 3697 } 3698 } 3699 EXPORT_SYMBOL_GPL(device_destroy); 3700 3701 /** 3702 * device_rename - renames a device 3703 * @dev: the pointer to the struct device to be renamed 3704 * @new_name: the new name of the device 3705 * 3706 * It is the responsibility of the caller to provide mutual 3707 * exclusion between two different calls of device_rename 3708 * on the same device to ensure that new_name is valid and 3709 * won't conflict with other devices. 3710 * 3711 * Note: Don't call this function. Currently, the networking layer calls this 3712 * function, but that will change. The following text from Kay Sievers offers 3713 * some insight: 3714 * 3715 * Renaming devices is racy at many levels, symlinks and other stuff are not 3716 * replaced atomically, and you get a "move" uevent, but it's not easy to 3717 * connect the event to the old and new device. Device nodes are not renamed at 3718 * all, there isn't even support for that in the kernel now. 3719 * 3720 * In the meantime, during renaming, your target name might be taken by another 3721 * driver, creating conflicts. Or the old name is taken directly after you 3722 * renamed it -- then you get events for the same DEVPATH, before you even see 3723 * the "move" event. It's just a mess, and nothing new should ever rely on 3724 * kernel device renaming. Besides that, it's not even implemented now for 3725 * other things than (driver-core wise very simple) network devices. 3726 * 3727 * We are currently about to change network renaming in udev to completely 3728 * disallow renaming of devices in the same namespace as the kernel uses, 3729 * because we can't solve the problems properly, that arise with swapping names 3730 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only 3731 * be allowed to some other name than eth[0-9]*, for the aforementioned 3732 * reasons. 3733 * 3734 * Make up a "real" name in the driver before you register anything, or add 3735 * some other attributes for userspace to find the device, or use udev to add 3736 * symlinks -- but never rename kernel devices later, it's a complete mess. We 3737 * don't even want to get into that and try to implement the missing pieces in 3738 * the core. We really have other pieces to fix in the driver core mess. :) 3739 */ 3740 int device_rename(struct device *dev, const char *new_name) 3741 { 3742 struct kobject *kobj = &dev->kobj; 3743 char *old_device_name = NULL; 3744 int error; 3745 3746 dev = get_device(dev); 3747 if (!dev) 3748 return -EINVAL; 3749 3750 dev_dbg(dev, "renaming to %s\n", new_name); 3751 3752 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL); 3753 if (!old_device_name) { 3754 error = -ENOMEM; 3755 goto out; 3756 } 3757 3758 if (dev->class) { 3759 error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj, 3760 kobj, old_device_name, 3761 new_name, kobject_namespace(kobj)); 3762 if (error) 3763 goto out; 3764 } 3765 3766 error = kobject_rename(kobj, new_name); 3767 if (error) 3768 goto out; 3769 3770 out: 3771 put_device(dev); 3772 3773 kfree(old_device_name); 3774 3775 return error; 3776 } 3777 EXPORT_SYMBOL_GPL(device_rename); 3778 3779 static int device_move_class_links(struct device *dev, 3780 struct device *old_parent, 3781 struct device *new_parent) 3782 { 3783 int error = 0; 3784 3785 if (old_parent) 3786 sysfs_remove_link(&dev->kobj, "device"); 3787 if (new_parent) 3788 error = sysfs_create_link(&dev->kobj, &new_parent->kobj, 3789 "device"); 3790 return error; 3791 } 3792 3793 /** 3794 * device_move - moves a device to a new parent 3795 * @dev: the pointer to the struct device to be moved 3796 * @new_parent: the new parent of the device (can be NULL) 3797 * @dpm_order: how to reorder the dpm_list 3798 */ 3799 int device_move(struct device *dev, struct device *new_parent, 3800 enum dpm_order dpm_order) 3801 { 3802 int error; 3803 struct device *old_parent; 3804 struct kobject *new_parent_kobj; 3805 3806 dev = get_device(dev); 3807 if (!dev) 3808 return -EINVAL; 3809 3810 device_pm_lock(); 3811 new_parent = get_device(new_parent); 3812 new_parent_kobj = get_device_parent(dev, new_parent); 3813 if (IS_ERR(new_parent_kobj)) { 3814 error = PTR_ERR(new_parent_kobj); 3815 put_device(new_parent); 3816 goto out; 3817 } 3818 3819 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev), 3820 __func__, new_parent ? dev_name(new_parent) : "<NULL>"); 3821 error = kobject_move(&dev->kobj, new_parent_kobj); 3822 if (error) { 3823 cleanup_glue_dir(dev, new_parent_kobj); 3824 put_device(new_parent); 3825 goto out; 3826 } 3827 old_parent = dev->parent; 3828 dev->parent = new_parent; 3829 if (old_parent) 3830 klist_remove(&dev->p->knode_parent); 3831 if (new_parent) { 3832 klist_add_tail(&dev->p->knode_parent, 3833 &new_parent->p->klist_children); 3834 set_dev_node(dev, dev_to_node(new_parent)); 3835 } 3836 3837 if (dev->class) { 3838 error = device_move_class_links(dev, old_parent, new_parent); 3839 if (error) { 3840 /* We ignore errors on cleanup since we're hosed anyway... */ 3841 device_move_class_links(dev, new_parent, old_parent); 3842 if (!kobject_move(&dev->kobj, &old_parent->kobj)) { 3843 if (new_parent) 3844 klist_remove(&dev->p->knode_parent); 3845 dev->parent = old_parent; 3846 if (old_parent) { 3847 klist_add_tail(&dev->p->knode_parent, 3848 &old_parent->p->klist_children); 3849 set_dev_node(dev, dev_to_node(old_parent)); 3850 } 3851 } 3852 cleanup_glue_dir(dev, new_parent_kobj); 3853 put_device(new_parent); 3854 goto out; 3855 } 3856 } 3857 switch (dpm_order) { 3858 case DPM_ORDER_NONE: 3859 break; 3860 case DPM_ORDER_DEV_AFTER_PARENT: 3861 device_pm_move_after(dev, new_parent); 3862 devices_kset_move_after(dev, new_parent); 3863 break; 3864 case DPM_ORDER_PARENT_BEFORE_DEV: 3865 device_pm_move_before(new_parent, dev); 3866 devices_kset_move_before(new_parent, dev); 3867 break; 3868 case DPM_ORDER_DEV_LAST: 3869 device_pm_move_last(dev); 3870 devices_kset_move_last(dev); 3871 break; 3872 } 3873 3874 put_device(old_parent); 3875 out: 3876 device_pm_unlock(); 3877 put_device(dev); 3878 return error; 3879 } 3880 EXPORT_SYMBOL_GPL(device_move); 3881 3882 static int device_attrs_change_owner(struct device *dev, kuid_t kuid, 3883 kgid_t kgid) 3884 { 3885 struct kobject *kobj = &dev->kobj; 3886 struct class *class = dev->class; 3887 const struct device_type *type = dev->type; 3888 int error; 3889 3890 if (class) { 3891 /* 3892 * Change the device groups of the device class for @dev to 3893 * @kuid/@kgid. 3894 */ 3895 error = sysfs_groups_change_owner(kobj, class->dev_groups, kuid, 3896 kgid); 3897 if (error) 3898 return error; 3899 } 3900 3901 if (type) { 3902 /* 3903 * Change the device groups of the device type for @dev to 3904 * @kuid/@kgid. 3905 */ 3906 error = sysfs_groups_change_owner(kobj, type->groups, kuid, 3907 kgid); 3908 if (error) 3909 return error; 3910 } 3911 3912 /* Change the device groups of @dev to @kuid/@kgid. */ 3913 error = sysfs_groups_change_owner(kobj, dev->groups, kuid, kgid); 3914 if (error) 3915 return error; 3916 3917 if (device_supports_offline(dev) && !dev->offline_disabled) { 3918 /* Change online device attributes of @dev to @kuid/@kgid. */ 3919 error = sysfs_file_change_owner(kobj, dev_attr_online.attr.name, 3920 kuid, kgid); 3921 if (error) 3922 return error; 3923 } 3924 3925 return 0; 3926 } 3927 3928 /** 3929 * device_change_owner - change the owner of an existing device. 3930 * @dev: device. 3931 * @kuid: new owner's kuid 3932 * @kgid: new owner's kgid 3933 * 3934 * This changes the owner of @dev and its corresponding sysfs entries to 3935 * @kuid/@kgid. This function closely mirrors how @dev was added via driver 3936 * core. 3937 * 3938 * Returns 0 on success or error code on failure. 3939 */ 3940 int device_change_owner(struct device *dev, kuid_t kuid, kgid_t kgid) 3941 { 3942 int error; 3943 struct kobject *kobj = &dev->kobj; 3944 3945 dev = get_device(dev); 3946 if (!dev) 3947 return -EINVAL; 3948 3949 /* 3950 * Change the kobject and the default attributes and groups of the 3951 * ktype associated with it to @kuid/@kgid. 3952 */ 3953 error = sysfs_change_owner(kobj, kuid, kgid); 3954 if (error) 3955 goto out; 3956 3957 /* 3958 * Change the uevent file for @dev to the new owner. The uevent file 3959 * was created in a separate step when @dev got added and we mirror 3960 * that step here. 3961 */ 3962 error = sysfs_file_change_owner(kobj, dev_attr_uevent.attr.name, kuid, 3963 kgid); 3964 if (error) 3965 goto out; 3966 3967 /* 3968 * Change the device groups, the device groups associated with the 3969 * device class, and the groups associated with the device type of @dev 3970 * to @kuid/@kgid. 3971 */ 3972 error = device_attrs_change_owner(dev, kuid, kgid); 3973 if (error) 3974 goto out; 3975 3976 error = dpm_sysfs_change_owner(dev, kuid, kgid); 3977 if (error) 3978 goto out; 3979 3980 #ifdef CONFIG_BLOCK 3981 if (sysfs_deprecated && dev->class == &block_class) 3982 goto out; 3983 #endif 3984 3985 /* 3986 * Change the owner of the symlink located in the class directory of 3987 * the device class associated with @dev which points to the actual 3988 * directory entry for @dev to @kuid/@kgid. This ensures that the 3989 * symlink shows the same permissions as its target. 3990 */ 3991 error = sysfs_link_change_owner(&dev->class->p->subsys.kobj, &dev->kobj, 3992 dev_name(dev), kuid, kgid); 3993 if (error) 3994 goto out; 3995 3996 out: 3997 put_device(dev); 3998 return error; 3999 } 4000 EXPORT_SYMBOL_GPL(device_change_owner); 4001 4002 /** 4003 * device_shutdown - call ->shutdown() on each device to shutdown. 4004 */ 4005 void device_shutdown(void) 4006 { 4007 struct device *dev, *parent; 4008 4009 wait_for_device_probe(); 4010 device_block_probing(); 4011 4012 cpufreq_suspend(); 4013 4014 spin_lock(&devices_kset->list_lock); 4015 /* 4016 * Walk the devices list backward, shutting down each in turn. 4017 * Beware that device unplug events may also start pulling 4018 * devices offline, even as the system is shutting down. 4019 */ 4020 while (!list_empty(&devices_kset->list)) { 4021 dev = list_entry(devices_kset->list.prev, struct device, 4022 kobj.entry); 4023 4024 /* 4025 * hold reference count of device's parent to 4026 * prevent it from being freed because parent's 4027 * lock is to be held 4028 */ 4029 parent = get_device(dev->parent); 4030 get_device(dev); 4031 /* 4032 * Make sure the device is off the kset list, in the 4033 * event that dev->*->shutdown() doesn't remove it. 4034 */ 4035 list_del_init(&dev->kobj.entry); 4036 spin_unlock(&devices_kset->list_lock); 4037 4038 /* hold lock to avoid race with probe/release */ 4039 if (parent) 4040 device_lock(parent); 4041 device_lock(dev); 4042 4043 /* Don't allow any more runtime suspends */ 4044 pm_runtime_get_noresume(dev); 4045 pm_runtime_barrier(dev); 4046 4047 if (dev->class && dev->class->shutdown_pre) { 4048 if (initcall_debug) 4049 dev_info(dev, "shutdown_pre\n"); 4050 dev->class->shutdown_pre(dev); 4051 } 4052 if (dev->bus && dev->bus->shutdown) { 4053 if (initcall_debug) 4054 dev_info(dev, "shutdown\n"); 4055 dev->bus->shutdown(dev); 4056 } else if (dev->driver && dev->driver->shutdown) { 4057 if (initcall_debug) 4058 dev_info(dev, "shutdown\n"); 4059 dev->driver->shutdown(dev); 4060 } 4061 4062 device_unlock(dev); 4063 if (parent) 4064 device_unlock(parent); 4065 4066 put_device(dev); 4067 put_device(parent); 4068 4069 spin_lock(&devices_kset->list_lock); 4070 } 4071 spin_unlock(&devices_kset->list_lock); 4072 } 4073 4074 /* 4075 * Device logging functions 4076 */ 4077 4078 #ifdef CONFIG_PRINTK 4079 static void 4080 set_dev_info(const struct device *dev, struct dev_printk_info *dev_info) 4081 { 4082 const char *subsys; 4083 4084 memset(dev_info, 0, sizeof(*dev_info)); 4085 4086 if (dev->class) 4087 subsys = dev->class->name; 4088 else if (dev->bus) 4089 subsys = dev->bus->name; 4090 else 4091 return; 4092 4093 strscpy(dev_info->subsystem, subsys, sizeof(dev_info->subsystem)); 4094 4095 /* 4096 * Add device identifier DEVICE=: 4097 * b12:8 block dev_t 4098 * c127:3 char dev_t 4099 * n8 netdev ifindex 4100 * +sound:card0 subsystem:devname 4101 */ 4102 if (MAJOR(dev->devt)) { 4103 char c; 4104 4105 if (strcmp(subsys, "block") == 0) 4106 c = 'b'; 4107 else 4108 c = 'c'; 4109 4110 snprintf(dev_info->device, sizeof(dev_info->device), 4111 "%c%u:%u", c, MAJOR(dev->devt), MINOR(dev->devt)); 4112 } else if (strcmp(subsys, "net") == 0) { 4113 struct net_device *net = to_net_dev(dev); 4114 4115 snprintf(dev_info->device, sizeof(dev_info->device), 4116 "n%u", net->ifindex); 4117 } else { 4118 snprintf(dev_info->device, sizeof(dev_info->device), 4119 "+%s:%s", subsys, dev_name(dev)); 4120 } 4121 } 4122 4123 int dev_vprintk_emit(int level, const struct device *dev, 4124 const char *fmt, va_list args) 4125 { 4126 struct dev_printk_info dev_info; 4127 4128 set_dev_info(dev, &dev_info); 4129 4130 return vprintk_emit(0, level, &dev_info, fmt, args); 4131 } 4132 EXPORT_SYMBOL(dev_vprintk_emit); 4133 4134 int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...) 4135 { 4136 va_list args; 4137 int r; 4138 4139 va_start(args, fmt); 4140 4141 r = dev_vprintk_emit(level, dev, fmt, args); 4142 4143 va_end(args); 4144 4145 return r; 4146 } 4147 EXPORT_SYMBOL(dev_printk_emit); 4148 4149 static void __dev_printk(const char *level, const struct device *dev, 4150 struct va_format *vaf) 4151 { 4152 if (dev) 4153 dev_printk_emit(level[1] - '0', dev, "%s %s: %pV", 4154 dev_driver_string(dev), dev_name(dev), vaf); 4155 else 4156 printk("%s(NULL device *): %pV", level, vaf); 4157 } 4158 4159 void dev_printk(const char *level, const struct device *dev, 4160 const char *fmt, ...) 4161 { 4162 struct va_format vaf; 4163 va_list args; 4164 4165 va_start(args, fmt); 4166 4167 vaf.fmt = fmt; 4168 vaf.va = &args; 4169 4170 __dev_printk(level, dev, &vaf); 4171 4172 va_end(args); 4173 } 4174 EXPORT_SYMBOL(dev_printk); 4175 4176 #define define_dev_printk_level(func, kern_level) \ 4177 void func(const struct device *dev, const char *fmt, ...) \ 4178 { \ 4179 struct va_format vaf; \ 4180 va_list args; \ 4181 \ 4182 va_start(args, fmt); \ 4183 \ 4184 vaf.fmt = fmt; \ 4185 vaf.va = &args; \ 4186 \ 4187 __dev_printk(kern_level, dev, &vaf); \ 4188 \ 4189 va_end(args); \ 4190 } \ 4191 EXPORT_SYMBOL(func); 4192 4193 define_dev_printk_level(_dev_emerg, KERN_EMERG); 4194 define_dev_printk_level(_dev_alert, KERN_ALERT); 4195 define_dev_printk_level(_dev_crit, KERN_CRIT); 4196 define_dev_printk_level(_dev_err, KERN_ERR); 4197 define_dev_printk_level(_dev_warn, KERN_WARNING); 4198 define_dev_printk_level(_dev_notice, KERN_NOTICE); 4199 define_dev_printk_level(_dev_info, KERN_INFO); 4200 4201 #endif 4202 4203 /** 4204 * dev_err_probe - probe error check and log helper 4205 * @dev: the pointer to the struct device 4206 * @err: error value to test 4207 * @fmt: printf-style format string 4208 * @...: arguments as specified in the format string 4209 * 4210 * This helper implements common pattern present in probe functions for error 4211 * checking: print debug or error message depending if the error value is 4212 * -EPROBE_DEFER and propagate error upwards. 4213 * In case of -EPROBE_DEFER it sets also defer probe reason, which can be 4214 * checked later by reading devices_deferred debugfs attribute. 4215 * It replaces code sequence:: 4216 * 4217 * if (err != -EPROBE_DEFER) 4218 * dev_err(dev, ...); 4219 * else 4220 * dev_dbg(dev, ...); 4221 * return err; 4222 * 4223 * with:: 4224 * 4225 * return dev_err_probe(dev, err, ...); 4226 * 4227 * Returns @err. 4228 * 4229 */ 4230 int dev_err_probe(const struct device *dev, int err, const char *fmt, ...) 4231 { 4232 struct va_format vaf; 4233 va_list args; 4234 4235 va_start(args, fmt); 4236 vaf.fmt = fmt; 4237 vaf.va = &args; 4238 4239 if (err != -EPROBE_DEFER) { 4240 dev_err(dev, "error %pe: %pV", ERR_PTR(err), &vaf); 4241 } else { 4242 device_set_deferred_probe_reason(dev, &vaf); 4243 dev_dbg(dev, "error %pe: %pV", ERR_PTR(err), &vaf); 4244 } 4245 4246 va_end(args); 4247 4248 return err; 4249 } 4250 EXPORT_SYMBOL_GPL(dev_err_probe); 4251 4252 static inline bool fwnode_is_primary(struct fwnode_handle *fwnode) 4253 { 4254 return fwnode && !IS_ERR(fwnode->secondary); 4255 } 4256 4257 /** 4258 * set_primary_fwnode - Change the primary firmware node of a given device. 4259 * @dev: Device to handle. 4260 * @fwnode: New primary firmware node of the device. 4261 * 4262 * Set the device's firmware node pointer to @fwnode, but if a secondary 4263 * firmware node of the device is present, preserve it. 4264 */ 4265 void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode) 4266 { 4267 struct fwnode_handle *fn = dev->fwnode; 4268 4269 if (fwnode) { 4270 if (fwnode_is_primary(fn)) 4271 fn = fn->secondary; 4272 4273 if (fn) { 4274 WARN_ON(fwnode->secondary); 4275 fwnode->secondary = fn; 4276 } 4277 dev->fwnode = fwnode; 4278 } else { 4279 if (fwnode_is_primary(fn)) { 4280 dev->fwnode = fn->secondary; 4281 fn->secondary = NULL; 4282 } else { 4283 dev->fwnode = NULL; 4284 } 4285 } 4286 } 4287 EXPORT_SYMBOL_GPL(set_primary_fwnode); 4288 4289 /** 4290 * set_secondary_fwnode - Change the secondary firmware node of a given device. 4291 * @dev: Device to handle. 4292 * @fwnode: New secondary firmware node of the device. 4293 * 4294 * If a primary firmware node of the device is present, set its secondary 4295 * pointer to @fwnode. Otherwise, set the device's firmware node pointer to 4296 * @fwnode. 4297 */ 4298 void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode) 4299 { 4300 if (fwnode) 4301 fwnode->secondary = ERR_PTR(-ENODEV); 4302 4303 if (fwnode_is_primary(dev->fwnode)) 4304 dev->fwnode->secondary = fwnode; 4305 else 4306 dev->fwnode = fwnode; 4307 } 4308 EXPORT_SYMBOL_GPL(set_secondary_fwnode); 4309 4310 /** 4311 * device_set_of_node_from_dev - reuse device-tree node of another device 4312 * @dev: device whose device-tree node is being set 4313 * @dev2: device whose device-tree node is being reused 4314 * 4315 * Takes another reference to the new device-tree node after first dropping 4316 * any reference held to the old node. 4317 */ 4318 void device_set_of_node_from_dev(struct device *dev, const struct device *dev2) 4319 { 4320 of_node_put(dev->of_node); 4321 dev->of_node = of_node_get(dev2->of_node); 4322 dev->of_node_reused = true; 4323 } 4324 EXPORT_SYMBOL_GPL(device_set_of_node_from_dev); 4325 4326 int device_match_name(struct device *dev, const void *name) 4327 { 4328 return sysfs_streq(dev_name(dev), name); 4329 } 4330 EXPORT_SYMBOL_GPL(device_match_name); 4331 4332 int device_match_of_node(struct device *dev, const void *np) 4333 { 4334 return dev->of_node == np; 4335 } 4336 EXPORT_SYMBOL_GPL(device_match_of_node); 4337 4338 int device_match_fwnode(struct device *dev, const void *fwnode) 4339 { 4340 return dev_fwnode(dev) == fwnode; 4341 } 4342 EXPORT_SYMBOL_GPL(device_match_fwnode); 4343 4344 int device_match_devt(struct device *dev, const void *pdevt) 4345 { 4346 return dev->devt == *(dev_t *)pdevt; 4347 } 4348 EXPORT_SYMBOL_GPL(device_match_devt); 4349 4350 int device_match_acpi_dev(struct device *dev, const void *adev) 4351 { 4352 return ACPI_COMPANION(dev) == adev; 4353 } 4354 EXPORT_SYMBOL(device_match_acpi_dev); 4355 4356 int device_match_any(struct device *dev, const void *unused) 4357 { 4358 return 1; 4359 } 4360 EXPORT_SYMBOL_GPL(device_match_any); 4361