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