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