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