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