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