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