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