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