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/device.h> 13 #include <linux/err.h> 14 #include <linux/fwnode.h> 15 #include <linux/init.h> 16 #include <linux/module.h> 17 #include <linux/slab.h> 18 #include <linux/string.h> 19 #include <linux/kdev_t.h> 20 #include <linux/notifier.h> 21 #include <linux/of.h> 22 #include <linux/of_device.h> 23 #include <linux/genhd.h> 24 #include <linux/mutex.h> 25 #include <linux/pm_runtime.h> 26 #include <linux/netdevice.h> 27 #include <linux/sched/signal.h> 28 #include <linux/sysfs.h> 29 30 #include "base.h" 31 #include "power/power.h" 32 33 #ifdef CONFIG_SYSFS_DEPRECATED 34 #ifdef CONFIG_SYSFS_DEPRECATED_V2 35 long sysfs_deprecated = 1; 36 #else 37 long sysfs_deprecated = 0; 38 #endif 39 static int __init sysfs_deprecated_setup(char *arg) 40 { 41 return kstrtol(arg, 10, &sysfs_deprecated); 42 } 43 early_param("sysfs.deprecated", sysfs_deprecated_setup); 44 #endif 45 46 /* Device links support. */ 47 48 #ifdef CONFIG_SRCU 49 static DEFINE_MUTEX(device_links_lock); 50 DEFINE_STATIC_SRCU(device_links_srcu); 51 52 static inline void device_links_write_lock(void) 53 { 54 mutex_lock(&device_links_lock); 55 } 56 57 static inline void device_links_write_unlock(void) 58 { 59 mutex_unlock(&device_links_lock); 60 } 61 62 int device_links_read_lock(void) 63 { 64 return srcu_read_lock(&device_links_srcu); 65 } 66 67 void device_links_read_unlock(int idx) 68 { 69 srcu_read_unlock(&device_links_srcu, idx); 70 } 71 #else /* !CONFIG_SRCU */ 72 static DECLARE_RWSEM(device_links_lock); 73 74 static inline void device_links_write_lock(void) 75 { 76 down_write(&device_links_lock); 77 } 78 79 static inline void device_links_write_unlock(void) 80 { 81 up_write(&device_links_lock); 82 } 83 84 int device_links_read_lock(void) 85 { 86 down_read(&device_links_lock); 87 return 0; 88 } 89 90 void device_links_read_unlock(int not_used) 91 { 92 up_read(&device_links_lock); 93 } 94 #endif /* !CONFIG_SRCU */ 95 96 /** 97 * device_is_dependent - Check if one device depends on another one 98 * @dev: Device to check dependencies for. 99 * @target: Device to check against. 100 * 101 * Check if @target depends on @dev or any device dependent on it (its child or 102 * its consumer etc). Return 1 if that is the case or 0 otherwise. 103 */ 104 static int device_is_dependent(struct device *dev, void *target) 105 { 106 struct device_link *link; 107 int ret; 108 109 if (dev == target) 110 return 1; 111 112 ret = device_for_each_child(dev, target, device_is_dependent); 113 if (ret) 114 return ret; 115 116 list_for_each_entry(link, &dev->links.consumers, s_node) { 117 if (link->consumer == target) 118 return 1; 119 120 ret = device_is_dependent(link->consumer, target); 121 if (ret) 122 break; 123 } 124 return ret; 125 } 126 127 static int device_reorder_to_tail(struct device *dev, void *not_used) 128 { 129 struct device_link *link; 130 131 /* 132 * Devices that have not been registered yet will be put to the ends 133 * of the lists during the registration, so skip them here. 134 */ 135 if (device_is_registered(dev)) 136 devices_kset_move_last(dev); 137 138 if (device_pm_initialized(dev)) 139 device_pm_move_last(dev); 140 141 device_for_each_child(dev, NULL, device_reorder_to_tail); 142 list_for_each_entry(link, &dev->links.consumers, s_node) 143 device_reorder_to_tail(link->consumer, NULL); 144 145 return 0; 146 } 147 148 /** 149 * device_pm_move_to_tail - Move set of devices to the end of device lists 150 * @dev: Device to move 151 * 152 * This is a device_reorder_to_tail() wrapper taking the requisite locks. 153 * 154 * It moves the @dev along with all of its children and all of its consumers 155 * to the ends of the device_kset and dpm_list, recursively. 156 */ 157 void device_pm_move_to_tail(struct device *dev) 158 { 159 int idx; 160 161 idx = device_links_read_lock(); 162 device_pm_lock(); 163 device_reorder_to_tail(dev, NULL); 164 device_pm_unlock(); 165 device_links_read_unlock(idx); 166 } 167 168 /** 169 * device_link_add - Create a link between two devices. 170 * @consumer: Consumer end of the link. 171 * @supplier: Supplier end of the link. 172 * @flags: Link flags. 173 * 174 * The caller is responsible for the proper synchronization of the link creation 175 * with runtime PM. First, setting the DL_FLAG_PM_RUNTIME flag will cause the 176 * runtime PM framework to take the link into account. Second, if the 177 * DL_FLAG_RPM_ACTIVE flag is set in addition to it, the supplier devices will 178 * be forced into the active metastate and reference-counted upon the creation 179 * of the link. If DL_FLAG_PM_RUNTIME is not set, DL_FLAG_RPM_ACTIVE will be 180 * ignored. 181 * 182 * If the DL_FLAG_AUTOREMOVE_CONSUMER is set, the link will be removed 183 * automatically when the consumer device driver unbinds from it. 184 * The combination of both DL_FLAG_AUTOREMOVE_CONSUMER and DL_FLAG_STATELESS 185 * set is invalid and will cause NULL to be returned. 186 * 187 * A side effect of the link creation is re-ordering of dpm_list and the 188 * devices_kset list by moving the consumer device and all devices depending 189 * on it to the ends of these lists (that does not happen to devices that have 190 * not been registered when this function is called). 191 * 192 * The supplier device is required to be registered when this function is called 193 * and NULL will be returned if that is not the case. The consumer device need 194 * not be registered, however. 195 */ 196 struct device_link *device_link_add(struct device *consumer, 197 struct device *supplier, u32 flags) 198 { 199 struct device_link *link; 200 201 if (!consumer || !supplier || 202 ((flags & DL_FLAG_STATELESS) && 203 (flags & DL_FLAG_AUTOREMOVE_CONSUMER))) 204 return NULL; 205 206 device_links_write_lock(); 207 device_pm_lock(); 208 209 /* 210 * If the supplier has not been fully registered yet or there is a 211 * reverse dependency between the consumer and the supplier already in 212 * the graph, return NULL. 213 */ 214 if (!device_pm_initialized(supplier) 215 || device_is_dependent(consumer, supplier)) { 216 link = NULL; 217 goto out; 218 } 219 220 list_for_each_entry(link, &supplier->links.consumers, s_node) 221 if (link->consumer == consumer) { 222 kref_get(&link->kref); 223 goto out; 224 } 225 226 link = kzalloc(sizeof(*link), GFP_KERNEL); 227 if (!link) 228 goto out; 229 230 if (flags & DL_FLAG_PM_RUNTIME) { 231 if (flags & DL_FLAG_RPM_ACTIVE) { 232 if (pm_runtime_get_sync(supplier) < 0) { 233 pm_runtime_put_noidle(supplier); 234 kfree(link); 235 link = NULL; 236 goto out; 237 } 238 link->rpm_active = true; 239 } 240 pm_runtime_new_link(consumer); 241 /* 242 * If the link is being added by the consumer driver at probe 243 * time, balance the decrementation of the supplier's runtime PM 244 * usage counter after consumer probe in driver_probe_device(). 245 */ 246 if (consumer->links.status == DL_DEV_PROBING) 247 pm_runtime_get_noresume(supplier); 248 } 249 get_device(supplier); 250 link->supplier = supplier; 251 INIT_LIST_HEAD(&link->s_node); 252 get_device(consumer); 253 link->consumer = consumer; 254 INIT_LIST_HEAD(&link->c_node); 255 link->flags = flags; 256 kref_init(&link->kref); 257 258 /* Determine the initial link state. */ 259 if (flags & DL_FLAG_STATELESS) { 260 link->status = DL_STATE_NONE; 261 } else { 262 switch (supplier->links.status) { 263 case DL_DEV_DRIVER_BOUND: 264 switch (consumer->links.status) { 265 case DL_DEV_PROBING: 266 /* 267 * Some callers expect the link creation during 268 * consumer driver probe to resume the supplier 269 * even without DL_FLAG_RPM_ACTIVE. 270 */ 271 if (flags & DL_FLAG_PM_RUNTIME) 272 pm_runtime_resume(supplier); 273 274 link->status = DL_STATE_CONSUMER_PROBE; 275 break; 276 case DL_DEV_DRIVER_BOUND: 277 link->status = DL_STATE_ACTIVE; 278 break; 279 default: 280 link->status = DL_STATE_AVAILABLE; 281 break; 282 } 283 break; 284 case DL_DEV_UNBINDING: 285 link->status = DL_STATE_SUPPLIER_UNBIND; 286 break; 287 default: 288 link->status = DL_STATE_DORMANT; 289 break; 290 } 291 } 292 293 /* 294 * Move the consumer and all of the devices depending on it to the end 295 * of dpm_list and the devices_kset list. 296 * 297 * It is necessary to hold dpm_list locked throughout all that or else 298 * we may end up suspending with a wrong ordering of it. 299 */ 300 device_reorder_to_tail(consumer, NULL); 301 302 list_add_tail_rcu(&link->s_node, &supplier->links.consumers); 303 list_add_tail_rcu(&link->c_node, &consumer->links.suppliers); 304 305 dev_info(consumer, "Linked as a consumer to %s\n", dev_name(supplier)); 306 307 out: 308 device_pm_unlock(); 309 device_links_write_unlock(); 310 return link; 311 } 312 EXPORT_SYMBOL_GPL(device_link_add); 313 314 static void device_link_free(struct device_link *link) 315 { 316 put_device(link->consumer); 317 put_device(link->supplier); 318 kfree(link); 319 } 320 321 #ifdef CONFIG_SRCU 322 static void __device_link_free_srcu(struct rcu_head *rhead) 323 { 324 device_link_free(container_of(rhead, struct device_link, rcu_head)); 325 } 326 327 static void __device_link_del(struct kref *kref) 328 { 329 struct device_link *link = container_of(kref, struct device_link, kref); 330 331 dev_info(link->consumer, "Dropping the link to %s\n", 332 dev_name(link->supplier)); 333 334 if (link->flags & DL_FLAG_PM_RUNTIME) 335 pm_runtime_drop_link(link->consumer); 336 337 list_del_rcu(&link->s_node); 338 list_del_rcu(&link->c_node); 339 call_srcu(&device_links_srcu, &link->rcu_head, __device_link_free_srcu); 340 } 341 #else /* !CONFIG_SRCU */ 342 static void __device_link_del(struct kref *kref) 343 { 344 struct device_link *link = container_of(kref, struct device_link, kref); 345 346 dev_info(link->consumer, "Dropping the link to %s\n", 347 dev_name(link->supplier)); 348 349 if (link->flags & DL_FLAG_PM_RUNTIME) 350 pm_runtime_drop_link(link->consumer); 351 352 list_del(&link->s_node); 353 list_del(&link->c_node); 354 device_link_free(link); 355 } 356 #endif /* !CONFIG_SRCU */ 357 358 /** 359 * device_link_del - Delete a link between two devices. 360 * @link: Device link to delete. 361 * 362 * The caller must ensure proper synchronization of this function with runtime 363 * PM. If the link was added multiple times, it needs to be deleted as often. 364 * Care is required for hotplugged devices: Their links are purged on removal 365 * and calling device_link_del() is then no longer allowed. 366 */ 367 void device_link_del(struct device_link *link) 368 { 369 device_links_write_lock(); 370 device_pm_lock(); 371 kref_put(&link->kref, __device_link_del); 372 device_pm_unlock(); 373 device_links_write_unlock(); 374 } 375 EXPORT_SYMBOL_GPL(device_link_del); 376 377 /** 378 * device_link_remove - remove a link between two devices. 379 * @consumer: Consumer end of the link. 380 * @supplier: Supplier end of the link. 381 * 382 * The caller must ensure proper synchronization of this function with runtime 383 * PM. 384 */ 385 void device_link_remove(void *consumer, struct device *supplier) 386 { 387 struct device_link *link; 388 389 if (WARN_ON(consumer == supplier)) 390 return; 391 392 device_links_write_lock(); 393 device_pm_lock(); 394 395 list_for_each_entry(link, &supplier->links.consumers, s_node) { 396 if (link->consumer == consumer) { 397 kref_put(&link->kref, __device_link_del); 398 break; 399 } 400 } 401 402 device_pm_unlock(); 403 device_links_write_unlock(); 404 } 405 EXPORT_SYMBOL_GPL(device_link_remove); 406 407 static void device_links_missing_supplier(struct device *dev) 408 { 409 struct device_link *link; 410 411 list_for_each_entry(link, &dev->links.suppliers, c_node) 412 if (link->status == DL_STATE_CONSUMER_PROBE) 413 WRITE_ONCE(link->status, DL_STATE_AVAILABLE); 414 } 415 416 /** 417 * device_links_check_suppliers - Check presence of supplier drivers. 418 * @dev: Consumer device. 419 * 420 * Check links from this device to any suppliers. Walk the list of the device's 421 * links to suppliers and see if all of them are available. If not, simply 422 * return -EPROBE_DEFER. 423 * 424 * We need to guarantee that the supplier will not go away after the check has 425 * been positive here. It only can go away in __device_release_driver() and 426 * that function checks the device's links to consumers. This means we need to 427 * mark the link as "consumer probe in progress" to make the supplier removal 428 * wait for us to complete (or bad things may happen). 429 * 430 * Links with the DL_FLAG_STATELESS flag set are ignored. 431 */ 432 int device_links_check_suppliers(struct device *dev) 433 { 434 struct device_link *link; 435 int ret = 0; 436 437 device_links_write_lock(); 438 439 list_for_each_entry(link, &dev->links.suppliers, c_node) { 440 if (link->flags & DL_FLAG_STATELESS) 441 continue; 442 443 if (link->status != DL_STATE_AVAILABLE) { 444 device_links_missing_supplier(dev); 445 ret = -EPROBE_DEFER; 446 break; 447 } 448 WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE); 449 } 450 dev->links.status = DL_DEV_PROBING; 451 452 device_links_write_unlock(); 453 return ret; 454 } 455 456 /** 457 * device_links_driver_bound - Update device links after probing its driver. 458 * @dev: Device to update the links for. 459 * 460 * The probe has been successful, so update links from this device to any 461 * consumers by changing their status to "available". 462 * 463 * Also change the status of @dev's links to suppliers to "active". 464 * 465 * Links with the DL_FLAG_STATELESS flag set are ignored. 466 */ 467 void device_links_driver_bound(struct device *dev) 468 { 469 struct device_link *link; 470 471 device_links_write_lock(); 472 473 list_for_each_entry(link, &dev->links.consumers, s_node) { 474 if (link->flags & DL_FLAG_STATELESS) 475 continue; 476 477 WARN_ON(link->status != DL_STATE_DORMANT); 478 WRITE_ONCE(link->status, DL_STATE_AVAILABLE); 479 } 480 481 list_for_each_entry(link, &dev->links.suppliers, c_node) { 482 if (link->flags & DL_FLAG_STATELESS) 483 continue; 484 485 WARN_ON(link->status != DL_STATE_CONSUMER_PROBE); 486 WRITE_ONCE(link->status, DL_STATE_ACTIVE); 487 } 488 489 dev->links.status = DL_DEV_DRIVER_BOUND; 490 491 device_links_write_unlock(); 492 } 493 494 /** 495 * __device_links_no_driver - Update links of a device without a driver. 496 * @dev: Device without a drvier. 497 * 498 * Delete all non-persistent links from this device to any suppliers. 499 * 500 * Persistent links stay around, but their status is changed to "available", 501 * unless they already are in the "supplier unbind in progress" state in which 502 * case they need not be updated. 503 * 504 * Links with the DL_FLAG_STATELESS flag set are ignored. 505 */ 506 static void __device_links_no_driver(struct device *dev) 507 { 508 struct device_link *link, *ln; 509 510 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) { 511 if (link->flags & DL_FLAG_STATELESS) 512 continue; 513 514 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER) 515 kref_put(&link->kref, __device_link_del); 516 else if (link->status != DL_STATE_SUPPLIER_UNBIND) 517 WRITE_ONCE(link->status, DL_STATE_AVAILABLE); 518 } 519 520 dev->links.status = DL_DEV_NO_DRIVER; 521 } 522 523 void device_links_no_driver(struct device *dev) 524 { 525 device_links_write_lock(); 526 __device_links_no_driver(dev); 527 device_links_write_unlock(); 528 } 529 530 /** 531 * device_links_driver_cleanup - Update links after driver removal. 532 * @dev: Device whose driver has just gone away. 533 * 534 * Update links to consumers for @dev by changing their status to "dormant" and 535 * invoke %__device_links_no_driver() to update links to suppliers for it as 536 * appropriate. 537 * 538 * Links with the DL_FLAG_STATELESS flag set are ignored. 539 */ 540 void device_links_driver_cleanup(struct device *dev) 541 { 542 struct device_link *link; 543 544 device_links_write_lock(); 545 546 list_for_each_entry(link, &dev->links.consumers, s_node) { 547 if (link->flags & DL_FLAG_STATELESS) 548 continue; 549 550 WARN_ON(link->flags & DL_FLAG_AUTOREMOVE_CONSUMER); 551 WARN_ON(link->status != DL_STATE_SUPPLIER_UNBIND); 552 553 /* 554 * autoremove the links between this @dev and its consumer 555 * devices that are not active, i.e. where the link state 556 * has moved to DL_STATE_SUPPLIER_UNBIND. 557 */ 558 if (link->status == DL_STATE_SUPPLIER_UNBIND && 559 link->flags & DL_FLAG_AUTOREMOVE_SUPPLIER) 560 kref_put(&link->kref, __device_link_del); 561 562 WRITE_ONCE(link->status, DL_STATE_DORMANT); 563 } 564 565 __device_links_no_driver(dev); 566 567 device_links_write_unlock(); 568 } 569 570 /** 571 * device_links_busy - Check if there are any busy links to consumers. 572 * @dev: Device to check. 573 * 574 * Check each consumer of the device and return 'true' if its link's status 575 * is one of "consumer probe" or "active" (meaning that the given consumer is 576 * probing right now or its driver is present). Otherwise, change the link 577 * state to "supplier unbind" to prevent the consumer from being probed 578 * successfully going forward. 579 * 580 * Return 'false' if there are no probing or active consumers. 581 * 582 * Links with the DL_FLAG_STATELESS flag set are ignored. 583 */ 584 bool device_links_busy(struct device *dev) 585 { 586 struct device_link *link; 587 bool ret = false; 588 589 device_links_write_lock(); 590 591 list_for_each_entry(link, &dev->links.consumers, s_node) { 592 if (link->flags & DL_FLAG_STATELESS) 593 continue; 594 595 if (link->status == DL_STATE_CONSUMER_PROBE 596 || link->status == DL_STATE_ACTIVE) { 597 ret = true; 598 break; 599 } 600 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND); 601 } 602 603 dev->links.status = DL_DEV_UNBINDING; 604 605 device_links_write_unlock(); 606 return ret; 607 } 608 609 /** 610 * device_links_unbind_consumers - Force unbind consumers of the given device. 611 * @dev: Device to unbind the consumers of. 612 * 613 * Walk the list of links to consumers for @dev and if any of them is in the 614 * "consumer probe" state, wait for all device probes in progress to complete 615 * and start over. 616 * 617 * If that's not the case, change the status of the link to "supplier unbind" 618 * and check if the link was in the "active" state. If so, force the consumer 619 * driver to unbind and start over (the consumer will not re-probe as we have 620 * changed the state of the link already). 621 * 622 * Links with the DL_FLAG_STATELESS flag set are ignored. 623 */ 624 void device_links_unbind_consumers(struct device *dev) 625 { 626 struct device_link *link; 627 628 start: 629 device_links_write_lock(); 630 631 list_for_each_entry(link, &dev->links.consumers, s_node) { 632 enum device_link_state status; 633 634 if (link->flags & DL_FLAG_STATELESS) 635 continue; 636 637 status = link->status; 638 if (status == DL_STATE_CONSUMER_PROBE) { 639 device_links_write_unlock(); 640 641 wait_for_device_probe(); 642 goto start; 643 } 644 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND); 645 if (status == DL_STATE_ACTIVE) { 646 struct device *consumer = link->consumer; 647 648 get_device(consumer); 649 650 device_links_write_unlock(); 651 652 device_release_driver_internal(consumer, NULL, 653 consumer->parent); 654 put_device(consumer); 655 goto start; 656 } 657 } 658 659 device_links_write_unlock(); 660 } 661 662 /** 663 * device_links_purge - Delete existing links to other devices. 664 * @dev: Target device. 665 */ 666 static void device_links_purge(struct device *dev) 667 { 668 struct device_link *link, *ln; 669 670 /* 671 * Delete all of the remaining links from this device to any other 672 * devices (either consumers or suppliers). 673 */ 674 device_links_write_lock(); 675 676 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) { 677 WARN_ON(link->status == DL_STATE_ACTIVE); 678 __device_link_del(&link->kref); 679 } 680 681 list_for_each_entry_safe_reverse(link, ln, &dev->links.consumers, s_node) { 682 WARN_ON(link->status != DL_STATE_DORMANT && 683 link->status != DL_STATE_NONE); 684 __device_link_del(&link->kref); 685 } 686 687 device_links_write_unlock(); 688 } 689 690 /* Device links support end. */ 691 692 int (*platform_notify)(struct device *dev) = NULL; 693 int (*platform_notify_remove)(struct device *dev) = NULL; 694 static struct kobject *dev_kobj; 695 struct kobject *sysfs_dev_char_kobj; 696 struct kobject *sysfs_dev_block_kobj; 697 698 static DEFINE_MUTEX(device_hotplug_lock); 699 700 void lock_device_hotplug(void) 701 { 702 mutex_lock(&device_hotplug_lock); 703 } 704 705 void unlock_device_hotplug(void) 706 { 707 mutex_unlock(&device_hotplug_lock); 708 } 709 710 int lock_device_hotplug_sysfs(void) 711 { 712 if (mutex_trylock(&device_hotplug_lock)) 713 return 0; 714 715 /* Avoid busy looping (5 ms of sleep should do). */ 716 msleep(5); 717 return restart_syscall(); 718 } 719 720 #ifdef CONFIG_BLOCK 721 static inline int device_is_not_partition(struct device *dev) 722 { 723 return !(dev->type == &part_type); 724 } 725 #else 726 static inline int device_is_not_partition(struct device *dev) 727 { 728 return 1; 729 } 730 #endif 731 732 static int 733 device_platform_notify(struct device *dev, enum kobject_action action) 734 { 735 int ret; 736 737 ret = acpi_platform_notify(dev, action); 738 if (ret) 739 return ret; 740 741 ret = software_node_notify(dev, action); 742 if (ret) 743 return ret; 744 745 if (platform_notify && action == KOBJ_ADD) 746 platform_notify(dev); 747 else if (platform_notify_remove && action == KOBJ_REMOVE) 748 platform_notify_remove(dev); 749 return 0; 750 } 751 752 /** 753 * dev_driver_string - Return a device's driver name, if at all possible 754 * @dev: struct device to get the name of 755 * 756 * Will return the device's driver's name if it is bound to a device. If 757 * the device is not bound to a driver, it will return the name of the bus 758 * it is attached to. If it is not attached to a bus either, an empty 759 * string will be returned. 760 */ 761 const char *dev_driver_string(const struct device *dev) 762 { 763 struct device_driver *drv; 764 765 /* dev->driver can change to NULL underneath us because of unbinding, 766 * so be careful about accessing it. dev->bus and dev->class should 767 * never change once they are set, so they don't need special care. 768 */ 769 drv = READ_ONCE(dev->driver); 770 return drv ? drv->name : 771 (dev->bus ? dev->bus->name : 772 (dev->class ? dev->class->name : "")); 773 } 774 EXPORT_SYMBOL(dev_driver_string); 775 776 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr) 777 778 static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr, 779 char *buf) 780 { 781 struct device_attribute *dev_attr = to_dev_attr(attr); 782 struct device *dev = kobj_to_dev(kobj); 783 ssize_t ret = -EIO; 784 785 if (dev_attr->show) 786 ret = dev_attr->show(dev, dev_attr, buf); 787 if (ret >= (ssize_t)PAGE_SIZE) { 788 printk("dev_attr_show: %pS returned bad count\n", 789 dev_attr->show); 790 } 791 return ret; 792 } 793 794 static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr, 795 const char *buf, size_t count) 796 { 797 struct device_attribute *dev_attr = to_dev_attr(attr); 798 struct device *dev = kobj_to_dev(kobj); 799 ssize_t ret = -EIO; 800 801 if (dev_attr->store) 802 ret = dev_attr->store(dev, dev_attr, buf, count); 803 return ret; 804 } 805 806 static const struct sysfs_ops dev_sysfs_ops = { 807 .show = dev_attr_show, 808 .store = dev_attr_store, 809 }; 810 811 #define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr) 812 813 ssize_t device_store_ulong(struct device *dev, 814 struct device_attribute *attr, 815 const char *buf, size_t size) 816 { 817 struct dev_ext_attribute *ea = to_ext_attr(attr); 818 char *end; 819 unsigned long new = simple_strtoul(buf, &end, 0); 820 if (end == buf) 821 return -EINVAL; 822 *(unsigned long *)(ea->var) = new; 823 /* Always return full write size even if we didn't consume all */ 824 return size; 825 } 826 EXPORT_SYMBOL_GPL(device_store_ulong); 827 828 ssize_t device_show_ulong(struct device *dev, 829 struct device_attribute *attr, 830 char *buf) 831 { 832 struct dev_ext_attribute *ea = to_ext_attr(attr); 833 return snprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var)); 834 } 835 EXPORT_SYMBOL_GPL(device_show_ulong); 836 837 ssize_t device_store_int(struct device *dev, 838 struct device_attribute *attr, 839 const char *buf, size_t size) 840 { 841 struct dev_ext_attribute *ea = to_ext_attr(attr); 842 char *end; 843 long new = simple_strtol(buf, &end, 0); 844 if (end == buf || new > INT_MAX || new < INT_MIN) 845 return -EINVAL; 846 *(int *)(ea->var) = new; 847 /* Always return full write size even if we didn't consume all */ 848 return size; 849 } 850 EXPORT_SYMBOL_GPL(device_store_int); 851 852 ssize_t device_show_int(struct device *dev, 853 struct device_attribute *attr, 854 char *buf) 855 { 856 struct dev_ext_attribute *ea = to_ext_attr(attr); 857 858 return snprintf(buf, PAGE_SIZE, "%d\n", *(int *)(ea->var)); 859 } 860 EXPORT_SYMBOL_GPL(device_show_int); 861 862 ssize_t device_store_bool(struct device *dev, struct device_attribute *attr, 863 const char *buf, size_t size) 864 { 865 struct dev_ext_attribute *ea = to_ext_attr(attr); 866 867 if (strtobool(buf, ea->var) < 0) 868 return -EINVAL; 869 870 return size; 871 } 872 EXPORT_SYMBOL_GPL(device_store_bool); 873 874 ssize_t device_show_bool(struct device *dev, struct device_attribute *attr, 875 char *buf) 876 { 877 struct dev_ext_attribute *ea = to_ext_attr(attr); 878 879 return snprintf(buf, PAGE_SIZE, "%d\n", *(bool *)(ea->var)); 880 } 881 EXPORT_SYMBOL_GPL(device_show_bool); 882 883 /** 884 * device_release - free device structure. 885 * @kobj: device's kobject. 886 * 887 * This is called once the reference count for the object 888 * reaches 0. We forward the call to the device's release 889 * method, which should handle actually freeing the structure. 890 */ 891 static void device_release(struct kobject *kobj) 892 { 893 struct device *dev = kobj_to_dev(kobj); 894 struct device_private *p = dev->p; 895 896 /* 897 * Some platform devices are driven without driver attached 898 * and managed resources may have been acquired. Make sure 899 * all resources are released. 900 * 901 * Drivers still can add resources into device after device 902 * is deleted but alive, so release devres here to avoid 903 * possible memory leak. 904 */ 905 devres_release_all(dev); 906 907 if (dev->release) 908 dev->release(dev); 909 else if (dev->type && dev->type->release) 910 dev->type->release(dev); 911 else if (dev->class && dev->class->dev_release) 912 dev->class->dev_release(dev); 913 else 914 WARN(1, KERN_ERR "Device '%s' does not have a release() " 915 "function, it is broken and must be fixed.\n", 916 dev_name(dev)); 917 kfree(p); 918 } 919 920 static const void *device_namespace(struct kobject *kobj) 921 { 922 struct device *dev = kobj_to_dev(kobj); 923 const void *ns = NULL; 924 925 if (dev->class && dev->class->ns_type) 926 ns = dev->class->namespace(dev); 927 928 return ns; 929 } 930 931 static void device_get_ownership(struct kobject *kobj, kuid_t *uid, kgid_t *gid) 932 { 933 struct device *dev = kobj_to_dev(kobj); 934 935 if (dev->class && dev->class->get_ownership) 936 dev->class->get_ownership(dev, uid, gid); 937 } 938 939 static struct kobj_type device_ktype = { 940 .release = device_release, 941 .sysfs_ops = &dev_sysfs_ops, 942 .namespace = device_namespace, 943 .get_ownership = device_get_ownership, 944 }; 945 946 947 static int dev_uevent_filter(struct kset *kset, struct kobject *kobj) 948 { 949 struct kobj_type *ktype = get_ktype(kobj); 950 951 if (ktype == &device_ktype) { 952 struct device *dev = kobj_to_dev(kobj); 953 if (dev->bus) 954 return 1; 955 if (dev->class) 956 return 1; 957 } 958 return 0; 959 } 960 961 static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj) 962 { 963 struct device *dev = kobj_to_dev(kobj); 964 965 if (dev->bus) 966 return dev->bus->name; 967 if (dev->class) 968 return dev->class->name; 969 return NULL; 970 } 971 972 static int dev_uevent(struct kset *kset, struct kobject *kobj, 973 struct kobj_uevent_env *env) 974 { 975 struct device *dev = kobj_to_dev(kobj); 976 int retval = 0; 977 978 /* add device node properties if present */ 979 if (MAJOR(dev->devt)) { 980 const char *tmp; 981 const char *name; 982 umode_t mode = 0; 983 kuid_t uid = GLOBAL_ROOT_UID; 984 kgid_t gid = GLOBAL_ROOT_GID; 985 986 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt)); 987 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt)); 988 name = device_get_devnode(dev, &mode, &uid, &gid, &tmp); 989 if (name) { 990 add_uevent_var(env, "DEVNAME=%s", name); 991 if (mode) 992 add_uevent_var(env, "DEVMODE=%#o", mode & 0777); 993 if (!uid_eq(uid, GLOBAL_ROOT_UID)) 994 add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid)); 995 if (!gid_eq(gid, GLOBAL_ROOT_GID)) 996 add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid)); 997 kfree(tmp); 998 } 999 } 1000 1001 if (dev->type && dev->type->name) 1002 add_uevent_var(env, "DEVTYPE=%s", dev->type->name); 1003 1004 if (dev->driver) 1005 add_uevent_var(env, "DRIVER=%s", dev->driver->name); 1006 1007 /* Add common DT information about the device */ 1008 of_device_uevent(dev, env); 1009 1010 /* have the bus specific function add its stuff */ 1011 if (dev->bus && dev->bus->uevent) { 1012 retval = dev->bus->uevent(dev, env); 1013 if (retval) 1014 pr_debug("device: '%s': %s: bus uevent() returned %d\n", 1015 dev_name(dev), __func__, retval); 1016 } 1017 1018 /* have the class specific function add its stuff */ 1019 if (dev->class && dev->class->dev_uevent) { 1020 retval = dev->class->dev_uevent(dev, env); 1021 if (retval) 1022 pr_debug("device: '%s': %s: class uevent() " 1023 "returned %d\n", dev_name(dev), 1024 __func__, retval); 1025 } 1026 1027 /* have the device type specific function add its stuff */ 1028 if (dev->type && dev->type->uevent) { 1029 retval = dev->type->uevent(dev, env); 1030 if (retval) 1031 pr_debug("device: '%s': %s: dev_type uevent() " 1032 "returned %d\n", dev_name(dev), 1033 __func__, retval); 1034 } 1035 1036 return retval; 1037 } 1038 1039 static const struct kset_uevent_ops device_uevent_ops = { 1040 .filter = dev_uevent_filter, 1041 .name = dev_uevent_name, 1042 .uevent = dev_uevent, 1043 }; 1044 1045 static ssize_t uevent_show(struct device *dev, struct device_attribute *attr, 1046 char *buf) 1047 { 1048 struct kobject *top_kobj; 1049 struct kset *kset; 1050 struct kobj_uevent_env *env = NULL; 1051 int i; 1052 size_t count = 0; 1053 int retval; 1054 1055 /* search the kset, the device belongs to */ 1056 top_kobj = &dev->kobj; 1057 while (!top_kobj->kset && top_kobj->parent) 1058 top_kobj = top_kobj->parent; 1059 if (!top_kobj->kset) 1060 goto out; 1061 1062 kset = top_kobj->kset; 1063 if (!kset->uevent_ops || !kset->uevent_ops->uevent) 1064 goto out; 1065 1066 /* respect filter */ 1067 if (kset->uevent_ops && kset->uevent_ops->filter) 1068 if (!kset->uevent_ops->filter(kset, &dev->kobj)) 1069 goto out; 1070 1071 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL); 1072 if (!env) 1073 return -ENOMEM; 1074 1075 /* let the kset specific function add its keys */ 1076 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env); 1077 if (retval) 1078 goto out; 1079 1080 /* copy keys to file */ 1081 for (i = 0; i < env->envp_idx; i++) 1082 count += sprintf(&buf[count], "%s\n", env->envp[i]); 1083 out: 1084 kfree(env); 1085 return count; 1086 } 1087 1088 static ssize_t uevent_store(struct device *dev, struct device_attribute *attr, 1089 const char *buf, size_t count) 1090 { 1091 if (kobject_synth_uevent(&dev->kobj, buf, count)) 1092 dev_err(dev, "uevent: failed to send synthetic uevent\n"); 1093 1094 return count; 1095 } 1096 static DEVICE_ATTR_RW(uevent); 1097 1098 static ssize_t online_show(struct device *dev, struct device_attribute *attr, 1099 char *buf) 1100 { 1101 bool val; 1102 1103 device_lock(dev); 1104 val = !dev->offline; 1105 device_unlock(dev); 1106 return sprintf(buf, "%u\n", val); 1107 } 1108 1109 static ssize_t online_store(struct device *dev, struct device_attribute *attr, 1110 const char *buf, size_t count) 1111 { 1112 bool val; 1113 int ret; 1114 1115 ret = strtobool(buf, &val); 1116 if (ret < 0) 1117 return ret; 1118 1119 ret = lock_device_hotplug_sysfs(); 1120 if (ret) 1121 return ret; 1122 1123 ret = val ? device_online(dev) : device_offline(dev); 1124 unlock_device_hotplug(); 1125 return ret < 0 ? ret : count; 1126 } 1127 static DEVICE_ATTR_RW(online); 1128 1129 int device_add_groups(struct device *dev, const struct attribute_group **groups) 1130 { 1131 return sysfs_create_groups(&dev->kobj, groups); 1132 } 1133 EXPORT_SYMBOL_GPL(device_add_groups); 1134 1135 void device_remove_groups(struct device *dev, 1136 const struct attribute_group **groups) 1137 { 1138 sysfs_remove_groups(&dev->kobj, groups); 1139 } 1140 EXPORT_SYMBOL_GPL(device_remove_groups); 1141 1142 union device_attr_group_devres { 1143 const struct attribute_group *group; 1144 const struct attribute_group **groups; 1145 }; 1146 1147 static int devm_attr_group_match(struct device *dev, void *res, void *data) 1148 { 1149 return ((union device_attr_group_devres *)res)->group == data; 1150 } 1151 1152 static void devm_attr_group_remove(struct device *dev, void *res) 1153 { 1154 union device_attr_group_devres *devres = res; 1155 const struct attribute_group *group = devres->group; 1156 1157 dev_dbg(dev, "%s: removing group %p\n", __func__, group); 1158 sysfs_remove_group(&dev->kobj, group); 1159 } 1160 1161 static void devm_attr_groups_remove(struct device *dev, void *res) 1162 { 1163 union device_attr_group_devres *devres = res; 1164 const struct attribute_group **groups = devres->groups; 1165 1166 dev_dbg(dev, "%s: removing groups %p\n", __func__, groups); 1167 sysfs_remove_groups(&dev->kobj, groups); 1168 } 1169 1170 /** 1171 * devm_device_add_group - given a device, create a managed attribute group 1172 * @dev: The device to create the group for 1173 * @grp: The attribute group to create 1174 * 1175 * This function creates a group for the first time. It will explicitly 1176 * warn and error if any of the attribute files being created already exist. 1177 * 1178 * Returns 0 on success or error code on failure. 1179 */ 1180 int devm_device_add_group(struct device *dev, const struct attribute_group *grp) 1181 { 1182 union device_attr_group_devres *devres; 1183 int error; 1184 1185 devres = devres_alloc(devm_attr_group_remove, 1186 sizeof(*devres), GFP_KERNEL); 1187 if (!devres) 1188 return -ENOMEM; 1189 1190 error = sysfs_create_group(&dev->kobj, grp); 1191 if (error) { 1192 devres_free(devres); 1193 return error; 1194 } 1195 1196 devres->group = grp; 1197 devres_add(dev, devres); 1198 return 0; 1199 } 1200 EXPORT_SYMBOL_GPL(devm_device_add_group); 1201 1202 /** 1203 * devm_device_remove_group: remove a managed group from a device 1204 * @dev: device to remove the group from 1205 * @grp: group to remove 1206 * 1207 * This function removes a group of attributes from a device. The attributes 1208 * previously have to have been created for this group, otherwise it will fail. 1209 */ 1210 void devm_device_remove_group(struct device *dev, 1211 const struct attribute_group *grp) 1212 { 1213 WARN_ON(devres_release(dev, devm_attr_group_remove, 1214 devm_attr_group_match, 1215 /* cast away const */ (void *)grp)); 1216 } 1217 EXPORT_SYMBOL_GPL(devm_device_remove_group); 1218 1219 /** 1220 * devm_device_add_groups - create a bunch of managed attribute groups 1221 * @dev: The device to create the group for 1222 * @groups: The attribute groups to create, NULL terminated 1223 * 1224 * This function creates a bunch of managed attribute groups. If an error 1225 * occurs when creating a group, all previously created groups will be 1226 * removed, unwinding everything back to the original state when this 1227 * function was called. It will explicitly warn and error if any of the 1228 * attribute files being created already exist. 1229 * 1230 * Returns 0 on success or error code from sysfs_create_group on failure. 1231 */ 1232 int devm_device_add_groups(struct device *dev, 1233 const struct attribute_group **groups) 1234 { 1235 union device_attr_group_devres *devres; 1236 int error; 1237 1238 devres = devres_alloc(devm_attr_groups_remove, 1239 sizeof(*devres), GFP_KERNEL); 1240 if (!devres) 1241 return -ENOMEM; 1242 1243 error = sysfs_create_groups(&dev->kobj, groups); 1244 if (error) { 1245 devres_free(devres); 1246 return error; 1247 } 1248 1249 devres->groups = groups; 1250 devres_add(dev, devres); 1251 return 0; 1252 } 1253 EXPORT_SYMBOL_GPL(devm_device_add_groups); 1254 1255 /** 1256 * devm_device_remove_groups - remove a list of managed groups 1257 * 1258 * @dev: The device for the groups to be removed from 1259 * @groups: NULL terminated list of groups to be removed 1260 * 1261 * If groups is not NULL, remove the specified groups from the device. 1262 */ 1263 void devm_device_remove_groups(struct device *dev, 1264 const struct attribute_group **groups) 1265 { 1266 WARN_ON(devres_release(dev, devm_attr_groups_remove, 1267 devm_attr_group_match, 1268 /* cast away const */ (void *)groups)); 1269 } 1270 EXPORT_SYMBOL_GPL(devm_device_remove_groups); 1271 1272 static int device_add_attrs(struct device *dev) 1273 { 1274 struct class *class = dev->class; 1275 const struct device_type *type = dev->type; 1276 int error; 1277 1278 if (class) { 1279 error = device_add_groups(dev, class->dev_groups); 1280 if (error) 1281 return error; 1282 } 1283 1284 if (type) { 1285 error = device_add_groups(dev, type->groups); 1286 if (error) 1287 goto err_remove_class_groups; 1288 } 1289 1290 error = device_add_groups(dev, dev->groups); 1291 if (error) 1292 goto err_remove_type_groups; 1293 1294 if (device_supports_offline(dev) && !dev->offline_disabled) { 1295 error = device_create_file(dev, &dev_attr_online); 1296 if (error) 1297 goto err_remove_dev_groups; 1298 } 1299 1300 return 0; 1301 1302 err_remove_dev_groups: 1303 device_remove_groups(dev, dev->groups); 1304 err_remove_type_groups: 1305 if (type) 1306 device_remove_groups(dev, type->groups); 1307 err_remove_class_groups: 1308 if (class) 1309 device_remove_groups(dev, class->dev_groups); 1310 1311 return error; 1312 } 1313 1314 static void device_remove_attrs(struct device *dev) 1315 { 1316 struct class *class = dev->class; 1317 const struct device_type *type = dev->type; 1318 1319 device_remove_file(dev, &dev_attr_online); 1320 device_remove_groups(dev, dev->groups); 1321 1322 if (type) 1323 device_remove_groups(dev, type->groups); 1324 1325 if (class) 1326 device_remove_groups(dev, class->dev_groups); 1327 } 1328 1329 static ssize_t dev_show(struct device *dev, struct device_attribute *attr, 1330 char *buf) 1331 { 1332 return print_dev_t(buf, dev->devt); 1333 } 1334 static DEVICE_ATTR_RO(dev); 1335 1336 /* /sys/devices/ */ 1337 struct kset *devices_kset; 1338 1339 /** 1340 * devices_kset_move_before - Move device in the devices_kset's list. 1341 * @deva: Device to move. 1342 * @devb: Device @deva should come before. 1343 */ 1344 static void devices_kset_move_before(struct device *deva, struct device *devb) 1345 { 1346 if (!devices_kset) 1347 return; 1348 pr_debug("devices_kset: Moving %s before %s\n", 1349 dev_name(deva), dev_name(devb)); 1350 spin_lock(&devices_kset->list_lock); 1351 list_move_tail(&deva->kobj.entry, &devb->kobj.entry); 1352 spin_unlock(&devices_kset->list_lock); 1353 } 1354 1355 /** 1356 * devices_kset_move_after - Move device in the devices_kset's list. 1357 * @deva: Device to move 1358 * @devb: Device @deva should come after. 1359 */ 1360 static void devices_kset_move_after(struct device *deva, struct device *devb) 1361 { 1362 if (!devices_kset) 1363 return; 1364 pr_debug("devices_kset: Moving %s after %s\n", 1365 dev_name(deva), dev_name(devb)); 1366 spin_lock(&devices_kset->list_lock); 1367 list_move(&deva->kobj.entry, &devb->kobj.entry); 1368 spin_unlock(&devices_kset->list_lock); 1369 } 1370 1371 /** 1372 * devices_kset_move_last - move the device to the end of devices_kset's list. 1373 * @dev: device to move 1374 */ 1375 void devices_kset_move_last(struct device *dev) 1376 { 1377 if (!devices_kset) 1378 return; 1379 pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev)); 1380 spin_lock(&devices_kset->list_lock); 1381 list_move_tail(&dev->kobj.entry, &devices_kset->list); 1382 spin_unlock(&devices_kset->list_lock); 1383 } 1384 1385 /** 1386 * device_create_file - create sysfs attribute file for device. 1387 * @dev: device. 1388 * @attr: device attribute descriptor. 1389 */ 1390 int device_create_file(struct device *dev, 1391 const struct device_attribute *attr) 1392 { 1393 int error = 0; 1394 1395 if (dev) { 1396 WARN(((attr->attr.mode & S_IWUGO) && !attr->store), 1397 "Attribute %s: write permission without 'store'\n", 1398 attr->attr.name); 1399 WARN(((attr->attr.mode & S_IRUGO) && !attr->show), 1400 "Attribute %s: read permission without 'show'\n", 1401 attr->attr.name); 1402 error = sysfs_create_file(&dev->kobj, &attr->attr); 1403 } 1404 1405 return error; 1406 } 1407 EXPORT_SYMBOL_GPL(device_create_file); 1408 1409 /** 1410 * device_remove_file - remove sysfs attribute file. 1411 * @dev: device. 1412 * @attr: device attribute descriptor. 1413 */ 1414 void device_remove_file(struct device *dev, 1415 const struct device_attribute *attr) 1416 { 1417 if (dev) 1418 sysfs_remove_file(&dev->kobj, &attr->attr); 1419 } 1420 EXPORT_SYMBOL_GPL(device_remove_file); 1421 1422 /** 1423 * device_remove_file_self - remove sysfs attribute file from its own method. 1424 * @dev: device. 1425 * @attr: device attribute descriptor. 1426 * 1427 * See kernfs_remove_self() for details. 1428 */ 1429 bool device_remove_file_self(struct device *dev, 1430 const struct device_attribute *attr) 1431 { 1432 if (dev) 1433 return sysfs_remove_file_self(&dev->kobj, &attr->attr); 1434 else 1435 return false; 1436 } 1437 EXPORT_SYMBOL_GPL(device_remove_file_self); 1438 1439 /** 1440 * device_create_bin_file - create sysfs binary attribute file for device. 1441 * @dev: device. 1442 * @attr: device binary attribute descriptor. 1443 */ 1444 int device_create_bin_file(struct device *dev, 1445 const struct bin_attribute *attr) 1446 { 1447 int error = -EINVAL; 1448 if (dev) 1449 error = sysfs_create_bin_file(&dev->kobj, attr); 1450 return error; 1451 } 1452 EXPORT_SYMBOL_GPL(device_create_bin_file); 1453 1454 /** 1455 * device_remove_bin_file - remove sysfs binary attribute file 1456 * @dev: device. 1457 * @attr: device binary attribute descriptor. 1458 */ 1459 void device_remove_bin_file(struct device *dev, 1460 const struct bin_attribute *attr) 1461 { 1462 if (dev) 1463 sysfs_remove_bin_file(&dev->kobj, attr); 1464 } 1465 EXPORT_SYMBOL_GPL(device_remove_bin_file); 1466 1467 static void klist_children_get(struct klist_node *n) 1468 { 1469 struct device_private *p = to_device_private_parent(n); 1470 struct device *dev = p->device; 1471 1472 get_device(dev); 1473 } 1474 1475 static void klist_children_put(struct klist_node *n) 1476 { 1477 struct device_private *p = to_device_private_parent(n); 1478 struct device *dev = p->device; 1479 1480 put_device(dev); 1481 } 1482 1483 /** 1484 * device_initialize - init device structure. 1485 * @dev: device. 1486 * 1487 * This prepares the device for use by other layers by initializing 1488 * its fields. 1489 * It is the first half of device_register(), if called by 1490 * that function, though it can also be called separately, so one 1491 * may use @dev's fields. In particular, get_device()/put_device() 1492 * may be used for reference counting of @dev after calling this 1493 * function. 1494 * 1495 * All fields in @dev must be initialized by the caller to 0, except 1496 * for those explicitly set to some other value. The simplest 1497 * approach is to use kzalloc() to allocate the structure containing 1498 * @dev. 1499 * 1500 * NOTE: Use put_device() to give up your reference instead of freeing 1501 * @dev directly once you have called this function. 1502 */ 1503 void device_initialize(struct device *dev) 1504 { 1505 dev->kobj.kset = devices_kset; 1506 kobject_init(&dev->kobj, &device_ktype); 1507 INIT_LIST_HEAD(&dev->dma_pools); 1508 mutex_init(&dev->mutex); 1509 lockdep_set_novalidate_class(&dev->mutex); 1510 spin_lock_init(&dev->devres_lock); 1511 INIT_LIST_HEAD(&dev->devres_head); 1512 device_pm_init(dev); 1513 set_dev_node(dev, -1); 1514 #ifdef CONFIG_GENERIC_MSI_IRQ 1515 INIT_LIST_HEAD(&dev->msi_list); 1516 #endif 1517 INIT_LIST_HEAD(&dev->links.consumers); 1518 INIT_LIST_HEAD(&dev->links.suppliers); 1519 dev->links.status = DL_DEV_NO_DRIVER; 1520 } 1521 EXPORT_SYMBOL_GPL(device_initialize); 1522 1523 struct kobject *virtual_device_parent(struct device *dev) 1524 { 1525 static struct kobject *virtual_dir = NULL; 1526 1527 if (!virtual_dir) 1528 virtual_dir = kobject_create_and_add("virtual", 1529 &devices_kset->kobj); 1530 1531 return virtual_dir; 1532 } 1533 1534 struct class_dir { 1535 struct kobject kobj; 1536 struct class *class; 1537 }; 1538 1539 #define to_class_dir(obj) container_of(obj, struct class_dir, kobj) 1540 1541 static void class_dir_release(struct kobject *kobj) 1542 { 1543 struct class_dir *dir = to_class_dir(kobj); 1544 kfree(dir); 1545 } 1546 1547 static const 1548 struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj) 1549 { 1550 struct class_dir *dir = to_class_dir(kobj); 1551 return dir->class->ns_type; 1552 } 1553 1554 static struct kobj_type class_dir_ktype = { 1555 .release = class_dir_release, 1556 .sysfs_ops = &kobj_sysfs_ops, 1557 .child_ns_type = class_dir_child_ns_type 1558 }; 1559 1560 static struct kobject * 1561 class_dir_create_and_add(struct class *class, struct kobject *parent_kobj) 1562 { 1563 struct class_dir *dir; 1564 int retval; 1565 1566 dir = kzalloc(sizeof(*dir), GFP_KERNEL); 1567 if (!dir) 1568 return ERR_PTR(-ENOMEM); 1569 1570 dir->class = class; 1571 kobject_init(&dir->kobj, &class_dir_ktype); 1572 1573 dir->kobj.kset = &class->p->glue_dirs; 1574 1575 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name); 1576 if (retval < 0) { 1577 kobject_put(&dir->kobj); 1578 return ERR_PTR(retval); 1579 } 1580 return &dir->kobj; 1581 } 1582 1583 static DEFINE_MUTEX(gdp_mutex); 1584 1585 static struct kobject *get_device_parent(struct device *dev, 1586 struct device *parent) 1587 { 1588 if (dev->class) { 1589 struct kobject *kobj = NULL; 1590 struct kobject *parent_kobj; 1591 struct kobject *k; 1592 1593 #ifdef CONFIG_BLOCK 1594 /* block disks show up in /sys/block */ 1595 if (sysfs_deprecated && dev->class == &block_class) { 1596 if (parent && parent->class == &block_class) 1597 return &parent->kobj; 1598 return &block_class.p->subsys.kobj; 1599 } 1600 #endif 1601 1602 /* 1603 * If we have no parent, we live in "virtual". 1604 * Class-devices with a non class-device as parent, live 1605 * in a "glue" directory to prevent namespace collisions. 1606 */ 1607 if (parent == NULL) 1608 parent_kobj = virtual_device_parent(dev); 1609 else if (parent->class && !dev->class->ns_type) 1610 return &parent->kobj; 1611 else 1612 parent_kobj = &parent->kobj; 1613 1614 mutex_lock(&gdp_mutex); 1615 1616 /* find our class-directory at the parent and reference it */ 1617 spin_lock(&dev->class->p->glue_dirs.list_lock); 1618 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry) 1619 if (k->parent == parent_kobj) { 1620 kobj = kobject_get(k); 1621 break; 1622 } 1623 spin_unlock(&dev->class->p->glue_dirs.list_lock); 1624 if (kobj) { 1625 mutex_unlock(&gdp_mutex); 1626 return kobj; 1627 } 1628 1629 /* or create a new class-directory at the parent device */ 1630 k = class_dir_create_and_add(dev->class, parent_kobj); 1631 /* do not emit an uevent for this simple "glue" directory */ 1632 mutex_unlock(&gdp_mutex); 1633 return k; 1634 } 1635 1636 /* subsystems can specify a default root directory for their devices */ 1637 if (!parent && dev->bus && dev->bus->dev_root) 1638 return &dev->bus->dev_root->kobj; 1639 1640 if (parent) 1641 return &parent->kobj; 1642 return NULL; 1643 } 1644 1645 static inline bool live_in_glue_dir(struct kobject *kobj, 1646 struct device *dev) 1647 { 1648 if (!kobj || !dev->class || 1649 kobj->kset != &dev->class->p->glue_dirs) 1650 return false; 1651 return true; 1652 } 1653 1654 static inline struct kobject *get_glue_dir(struct device *dev) 1655 { 1656 return dev->kobj.parent; 1657 } 1658 1659 /* 1660 * make sure cleaning up dir as the last step, we need to make 1661 * sure .release handler of kobject is run with holding the 1662 * global lock 1663 */ 1664 static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir) 1665 { 1666 /* see if we live in a "glue" directory */ 1667 if (!live_in_glue_dir(glue_dir, dev)) 1668 return; 1669 1670 mutex_lock(&gdp_mutex); 1671 if (!kobject_has_children(glue_dir)) 1672 kobject_del(glue_dir); 1673 kobject_put(glue_dir); 1674 mutex_unlock(&gdp_mutex); 1675 } 1676 1677 static int device_add_class_symlinks(struct device *dev) 1678 { 1679 struct device_node *of_node = dev_of_node(dev); 1680 int error; 1681 1682 if (of_node) { 1683 error = sysfs_create_link(&dev->kobj, of_node_kobj(of_node), "of_node"); 1684 if (error) 1685 dev_warn(dev, "Error %d creating of_node link\n",error); 1686 /* An error here doesn't warrant bringing down the device */ 1687 } 1688 1689 if (!dev->class) 1690 return 0; 1691 1692 error = sysfs_create_link(&dev->kobj, 1693 &dev->class->p->subsys.kobj, 1694 "subsystem"); 1695 if (error) 1696 goto out_devnode; 1697 1698 if (dev->parent && device_is_not_partition(dev)) { 1699 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj, 1700 "device"); 1701 if (error) 1702 goto out_subsys; 1703 } 1704 1705 #ifdef CONFIG_BLOCK 1706 /* /sys/block has directories and does not need symlinks */ 1707 if (sysfs_deprecated && dev->class == &block_class) 1708 return 0; 1709 #endif 1710 1711 /* link in the class directory pointing to the device */ 1712 error = sysfs_create_link(&dev->class->p->subsys.kobj, 1713 &dev->kobj, dev_name(dev)); 1714 if (error) 1715 goto out_device; 1716 1717 return 0; 1718 1719 out_device: 1720 sysfs_remove_link(&dev->kobj, "device"); 1721 1722 out_subsys: 1723 sysfs_remove_link(&dev->kobj, "subsystem"); 1724 out_devnode: 1725 sysfs_remove_link(&dev->kobj, "of_node"); 1726 return error; 1727 } 1728 1729 static void device_remove_class_symlinks(struct device *dev) 1730 { 1731 if (dev_of_node(dev)) 1732 sysfs_remove_link(&dev->kobj, "of_node"); 1733 1734 if (!dev->class) 1735 return; 1736 1737 if (dev->parent && device_is_not_partition(dev)) 1738 sysfs_remove_link(&dev->kobj, "device"); 1739 sysfs_remove_link(&dev->kobj, "subsystem"); 1740 #ifdef CONFIG_BLOCK 1741 if (sysfs_deprecated && dev->class == &block_class) 1742 return; 1743 #endif 1744 sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev)); 1745 } 1746 1747 /** 1748 * dev_set_name - set a device name 1749 * @dev: device 1750 * @fmt: format string for the device's name 1751 */ 1752 int dev_set_name(struct device *dev, const char *fmt, ...) 1753 { 1754 va_list vargs; 1755 int err; 1756 1757 va_start(vargs, fmt); 1758 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs); 1759 va_end(vargs); 1760 return err; 1761 } 1762 EXPORT_SYMBOL_GPL(dev_set_name); 1763 1764 /** 1765 * device_to_dev_kobj - select a /sys/dev/ directory for the device 1766 * @dev: device 1767 * 1768 * By default we select char/ for new entries. Setting class->dev_obj 1769 * to NULL prevents an entry from being created. class->dev_kobj must 1770 * be set (or cleared) before any devices are registered to the class 1771 * otherwise device_create_sys_dev_entry() and 1772 * device_remove_sys_dev_entry() will disagree about the presence of 1773 * the link. 1774 */ 1775 static struct kobject *device_to_dev_kobj(struct device *dev) 1776 { 1777 struct kobject *kobj; 1778 1779 if (dev->class) 1780 kobj = dev->class->dev_kobj; 1781 else 1782 kobj = sysfs_dev_char_kobj; 1783 1784 return kobj; 1785 } 1786 1787 static int device_create_sys_dev_entry(struct device *dev) 1788 { 1789 struct kobject *kobj = device_to_dev_kobj(dev); 1790 int error = 0; 1791 char devt_str[15]; 1792 1793 if (kobj) { 1794 format_dev_t(devt_str, dev->devt); 1795 error = sysfs_create_link(kobj, &dev->kobj, devt_str); 1796 } 1797 1798 return error; 1799 } 1800 1801 static void device_remove_sys_dev_entry(struct device *dev) 1802 { 1803 struct kobject *kobj = device_to_dev_kobj(dev); 1804 char devt_str[15]; 1805 1806 if (kobj) { 1807 format_dev_t(devt_str, dev->devt); 1808 sysfs_remove_link(kobj, devt_str); 1809 } 1810 } 1811 1812 static int device_private_init(struct device *dev) 1813 { 1814 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL); 1815 if (!dev->p) 1816 return -ENOMEM; 1817 dev->p->device = dev; 1818 klist_init(&dev->p->klist_children, klist_children_get, 1819 klist_children_put); 1820 INIT_LIST_HEAD(&dev->p->deferred_probe); 1821 return 0; 1822 } 1823 1824 /** 1825 * device_add - add device to device hierarchy. 1826 * @dev: device. 1827 * 1828 * This is part 2 of device_register(), though may be called 1829 * separately _iff_ device_initialize() has been called separately. 1830 * 1831 * This adds @dev to the kobject hierarchy via kobject_add(), adds it 1832 * to the global and sibling lists for the device, then 1833 * adds it to the other relevant subsystems of the driver model. 1834 * 1835 * Do not call this routine or device_register() more than once for 1836 * any device structure. The driver model core is not designed to work 1837 * with devices that get unregistered and then spring back to life. 1838 * (Among other things, it's very hard to guarantee that all references 1839 * to the previous incarnation of @dev have been dropped.) Allocate 1840 * and register a fresh new struct device instead. 1841 * 1842 * NOTE: _Never_ directly free @dev after calling this function, even 1843 * if it returned an error! Always use put_device() to give up your 1844 * reference instead. 1845 */ 1846 int device_add(struct device *dev) 1847 { 1848 struct device *parent; 1849 struct kobject *kobj; 1850 struct class_interface *class_intf; 1851 int error = -EINVAL; 1852 struct kobject *glue_dir = NULL; 1853 1854 dev = get_device(dev); 1855 if (!dev) 1856 goto done; 1857 1858 if (!dev->p) { 1859 error = device_private_init(dev); 1860 if (error) 1861 goto done; 1862 } 1863 1864 /* 1865 * for statically allocated devices, which should all be converted 1866 * some day, we need to initialize the name. We prevent reading back 1867 * the name, and force the use of dev_name() 1868 */ 1869 if (dev->init_name) { 1870 dev_set_name(dev, "%s", dev->init_name); 1871 dev->init_name = NULL; 1872 } 1873 1874 /* subsystems can specify simple device enumeration */ 1875 if (!dev_name(dev) && dev->bus && dev->bus->dev_name) 1876 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id); 1877 1878 if (!dev_name(dev)) { 1879 error = -EINVAL; 1880 goto name_error; 1881 } 1882 1883 pr_debug("device: '%s': %s\n", dev_name(dev), __func__); 1884 1885 parent = get_device(dev->parent); 1886 kobj = get_device_parent(dev, parent); 1887 if (IS_ERR(kobj)) { 1888 error = PTR_ERR(kobj); 1889 goto parent_error; 1890 } 1891 if (kobj) 1892 dev->kobj.parent = kobj; 1893 1894 /* use parent numa_node */ 1895 if (parent && (dev_to_node(dev) == NUMA_NO_NODE)) 1896 set_dev_node(dev, dev_to_node(parent)); 1897 1898 /* first, register with generic layer. */ 1899 /* we require the name to be set before, and pass NULL */ 1900 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL); 1901 if (error) { 1902 glue_dir = get_glue_dir(dev); 1903 goto Error; 1904 } 1905 1906 /* notify platform of device entry */ 1907 error = device_platform_notify(dev, KOBJ_ADD); 1908 if (error) 1909 goto platform_error; 1910 1911 error = device_create_file(dev, &dev_attr_uevent); 1912 if (error) 1913 goto attrError; 1914 1915 error = device_add_class_symlinks(dev); 1916 if (error) 1917 goto SymlinkError; 1918 error = device_add_attrs(dev); 1919 if (error) 1920 goto AttrsError; 1921 error = bus_add_device(dev); 1922 if (error) 1923 goto BusError; 1924 error = dpm_sysfs_add(dev); 1925 if (error) 1926 goto DPMError; 1927 device_pm_add(dev); 1928 1929 if (MAJOR(dev->devt)) { 1930 error = device_create_file(dev, &dev_attr_dev); 1931 if (error) 1932 goto DevAttrError; 1933 1934 error = device_create_sys_dev_entry(dev); 1935 if (error) 1936 goto SysEntryError; 1937 1938 devtmpfs_create_node(dev); 1939 } 1940 1941 /* Notify clients of device addition. This call must come 1942 * after dpm_sysfs_add() and before kobject_uevent(). 1943 */ 1944 if (dev->bus) 1945 blocking_notifier_call_chain(&dev->bus->p->bus_notifier, 1946 BUS_NOTIFY_ADD_DEVICE, dev); 1947 1948 kobject_uevent(&dev->kobj, KOBJ_ADD); 1949 bus_probe_device(dev); 1950 if (parent) 1951 klist_add_tail(&dev->p->knode_parent, 1952 &parent->p->klist_children); 1953 1954 if (dev->class) { 1955 mutex_lock(&dev->class->p->mutex); 1956 /* tie the class to the device */ 1957 klist_add_tail(&dev->knode_class, 1958 &dev->class->p->klist_devices); 1959 1960 /* notify any interfaces that the device is here */ 1961 list_for_each_entry(class_intf, 1962 &dev->class->p->interfaces, node) 1963 if (class_intf->add_dev) 1964 class_intf->add_dev(dev, class_intf); 1965 mutex_unlock(&dev->class->p->mutex); 1966 } 1967 done: 1968 put_device(dev); 1969 return error; 1970 SysEntryError: 1971 if (MAJOR(dev->devt)) 1972 device_remove_file(dev, &dev_attr_dev); 1973 DevAttrError: 1974 device_pm_remove(dev); 1975 dpm_sysfs_remove(dev); 1976 DPMError: 1977 bus_remove_device(dev); 1978 BusError: 1979 device_remove_attrs(dev); 1980 AttrsError: 1981 device_remove_class_symlinks(dev); 1982 SymlinkError: 1983 device_remove_file(dev, &dev_attr_uevent); 1984 attrError: 1985 device_platform_notify(dev, KOBJ_REMOVE); 1986 platform_error: 1987 kobject_uevent(&dev->kobj, KOBJ_REMOVE); 1988 glue_dir = get_glue_dir(dev); 1989 kobject_del(&dev->kobj); 1990 Error: 1991 cleanup_glue_dir(dev, glue_dir); 1992 parent_error: 1993 put_device(parent); 1994 name_error: 1995 kfree(dev->p); 1996 dev->p = NULL; 1997 goto done; 1998 } 1999 EXPORT_SYMBOL_GPL(device_add); 2000 2001 /** 2002 * device_register - register a device with the system. 2003 * @dev: pointer to the device structure 2004 * 2005 * This happens in two clean steps - initialize the device 2006 * and add it to the system. The two steps can be called 2007 * separately, but this is the easiest and most common. 2008 * I.e. you should only call the two helpers separately if 2009 * have a clearly defined need to use and refcount the device 2010 * before it is added to the hierarchy. 2011 * 2012 * For more information, see the kerneldoc for device_initialize() 2013 * and device_add(). 2014 * 2015 * NOTE: _Never_ directly free @dev after calling this function, even 2016 * if it returned an error! Always use put_device() to give up the 2017 * reference initialized in this function instead. 2018 */ 2019 int device_register(struct device *dev) 2020 { 2021 device_initialize(dev); 2022 return device_add(dev); 2023 } 2024 EXPORT_SYMBOL_GPL(device_register); 2025 2026 /** 2027 * get_device - increment reference count for device. 2028 * @dev: device. 2029 * 2030 * This simply forwards the call to kobject_get(), though 2031 * we do take care to provide for the case that we get a NULL 2032 * pointer passed in. 2033 */ 2034 struct device *get_device(struct device *dev) 2035 { 2036 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL; 2037 } 2038 EXPORT_SYMBOL_GPL(get_device); 2039 2040 /** 2041 * put_device - decrement reference count. 2042 * @dev: device in question. 2043 */ 2044 void put_device(struct device *dev) 2045 { 2046 /* might_sleep(); */ 2047 if (dev) 2048 kobject_put(&dev->kobj); 2049 } 2050 EXPORT_SYMBOL_GPL(put_device); 2051 2052 /** 2053 * device_del - delete device from system. 2054 * @dev: device. 2055 * 2056 * This is the first part of the device unregistration 2057 * sequence. This removes the device from the lists we control 2058 * from here, has it removed from the other driver model 2059 * subsystems it was added to in device_add(), and removes it 2060 * from the kobject hierarchy. 2061 * 2062 * NOTE: this should be called manually _iff_ device_add() was 2063 * also called manually. 2064 */ 2065 void device_del(struct device *dev) 2066 { 2067 struct device *parent = dev->parent; 2068 struct kobject *glue_dir = NULL; 2069 struct class_interface *class_intf; 2070 2071 /* Notify clients of device removal. This call must come 2072 * before dpm_sysfs_remove(). 2073 */ 2074 if (dev->bus) 2075 blocking_notifier_call_chain(&dev->bus->p->bus_notifier, 2076 BUS_NOTIFY_DEL_DEVICE, dev); 2077 2078 dpm_sysfs_remove(dev); 2079 if (parent) 2080 klist_del(&dev->p->knode_parent); 2081 if (MAJOR(dev->devt)) { 2082 devtmpfs_delete_node(dev); 2083 device_remove_sys_dev_entry(dev); 2084 device_remove_file(dev, &dev_attr_dev); 2085 } 2086 if (dev->class) { 2087 device_remove_class_symlinks(dev); 2088 2089 mutex_lock(&dev->class->p->mutex); 2090 /* notify any interfaces that the device is now gone */ 2091 list_for_each_entry(class_intf, 2092 &dev->class->p->interfaces, node) 2093 if (class_intf->remove_dev) 2094 class_intf->remove_dev(dev, class_intf); 2095 /* remove the device from the class list */ 2096 klist_del(&dev->knode_class); 2097 mutex_unlock(&dev->class->p->mutex); 2098 } 2099 device_remove_file(dev, &dev_attr_uevent); 2100 device_remove_attrs(dev); 2101 bus_remove_device(dev); 2102 device_pm_remove(dev); 2103 driver_deferred_probe_del(dev); 2104 device_platform_notify(dev, KOBJ_REMOVE); 2105 device_remove_properties(dev); 2106 device_links_purge(dev); 2107 2108 if (dev->bus) 2109 blocking_notifier_call_chain(&dev->bus->p->bus_notifier, 2110 BUS_NOTIFY_REMOVED_DEVICE, dev); 2111 kobject_uevent(&dev->kobj, KOBJ_REMOVE); 2112 glue_dir = get_glue_dir(dev); 2113 kobject_del(&dev->kobj); 2114 cleanup_glue_dir(dev, glue_dir); 2115 put_device(parent); 2116 } 2117 EXPORT_SYMBOL_GPL(device_del); 2118 2119 /** 2120 * device_unregister - unregister device from system. 2121 * @dev: device going away. 2122 * 2123 * We do this in two parts, like we do device_register(). First, 2124 * we remove it from all the subsystems with device_del(), then 2125 * we decrement the reference count via put_device(). If that 2126 * is the final reference count, the device will be cleaned up 2127 * via device_release() above. Otherwise, the structure will 2128 * stick around until the final reference to the device is dropped. 2129 */ 2130 void device_unregister(struct device *dev) 2131 { 2132 pr_debug("device: '%s': %s\n", dev_name(dev), __func__); 2133 device_del(dev); 2134 put_device(dev); 2135 } 2136 EXPORT_SYMBOL_GPL(device_unregister); 2137 2138 static struct device *prev_device(struct klist_iter *i) 2139 { 2140 struct klist_node *n = klist_prev(i); 2141 struct device *dev = NULL; 2142 struct device_private *p; 2143 2144 if (n) { 2145 p = to_device_private_parent(n); 2146 dev = p->device; 2147 } 2148 return dev; 2149 } 2150 2151 static struct device *next_device(struct klist_iter *i) 2152 { 2153 struct klist_node *n = klist_next(i); 2154 struct device *dev = NULL; 2155 struct device_private *p; 2156 2157 if (n) { 2158 p = to_device_private_parent(n); 2159 dev = p->device; 2160 } 2161 return dev; 2162 } 2163 2164 /** 2165 * device_get_devnode - path of device node file 2166 * @dev: device 2167 * @mode: returned file access mode 2168 * @uid: returned file owner 2169 * @gid: returned file group 2170 * @tmp: possibly allocated string 2171 * 2172 * Return the relative path of a possible device node. 2173 * Non-default names may need to allocate a memory to compose 2174 * a name. This memory is returned in tmp and needs to be 2175 * freed by the caller. 2176 */ 2177 const char *device_get_devnode(struct device *dev, 2178 umode_t *mode, kuid_t *uid, kgid_t *gid, 2179 const char **tmp) 2180 { 2181 char *s; 2182 2183 *tmp = NULL; 2184 2185 /* the device type may provide a specific name */ 2186 if (dev->type && dev->type->devnode) 2187 *tmp = dev->type->devnode(dev, mode, uid, gid); 2188 if (*tmp) 2189 return *tmp; 2190 2191 /* the class may provide a specific name */ 2192 if (dev->class && dev->class->devnode) 2193 *tmp = dev->class->devnode(dev, mode); 2194 if (*tmp) 2195 return *tmp; 2196 2197 /* return name without allocation, tmp == NULL */ 2198 if (strchr(dev_name(dev), '!') == NULL) 2199 return dev_name(dev); 2200 2201 /* replace '!' in the name with '/' */ 2202 s = kstrdup(dev_name(dev), GFP_KERNEL); 2203 if (!s) 2204 return NULL; 2205 strreplace(s, '!', '/'); 2206 return *tmp = s; 2207 } 2208 2209 /** 2210 * device_for_each_child - device child iterator. 2211 * @parent: parent struct device. 2212 * @fn: function to be called for each device. 2213 * @data: data for the callback. 2214 * 2215 * Iterate over @parent's child devices, and call @fn for each, 2216 * passing it @data. 2217 * 2218 * We check the return of @fn each time. If it returns anything 2219 * other than 0, we break out and return that value. 2220 */ 2221 int device_for_each_child(struct device *parent, void *data, 2222 int (*fn)(struct device *dev, void *data)) 2223 { 2224 struct klist_iter i; 2225 struct device *child; 2226 int error = 0; 2227 2228 if (!parent->p) 2229 return 0; 2230 2231 klist_iter_init(&parent->p->klist_children, &i); 2232 while (!error && (child = next_device(&i))) 2233 error = fn(child, data); 2234 klist_iter_exit(&i); 2235 return error; 2236 } 2237 EXPORT_SYMBOL_GPL(device_for_each_child); 2238 2239 /** 2240 * device_for_each_child_reverse - device child iterator in reversed order. 2241 * @parent: parent struct device. 2242 * @fn: function to be called for each device. 2243 * @data: data for the callback. 2244 * 2245 * Iterate over @parent's child devices, and call @fn for each, 2246 * passing it @data. 2247 * 2248 * We check the return of @fn each time. If it returns anything 2249 * other than 0, we break out and return that value. 2250 */ 2251 int device_for_each_child_reverse(struct device *parent, void *data, 2252 int (*fn)(struct device *dev, void *data)) 2253 { 2254 struct klist_iter i; 2255 struct device *child; 2256 int error = 0; 2257 2258 if (!parent->p) 2259 return 0; 2260 2261 klist_iter_init(&parent->p->klist_children, &i); 2262 while ((child = prev_device(&i)) && !error) 2263 error = fn(child, data); 2264 klist_iter_exit(&i); 2265 return error; 2266 } 2267 EXPORT_SYMBOL_GPL(device_for_each_child_reverse); 2268 2269 /** 2270 * device_find_child - device iterator for locating a particular device. 2271 * @parent: parent struct device 2272 * @match: Callback function to check device 2273 * @data: Data to pass to match function 2274 * 2275 * This is similar to the device_for_each_child() function above, but it 2276 * returns a reference to a device that is 'found' for later use, as 2277 * determined by the @match callback. 2278 * 2279 * The callback should return 0 if the device doesn't match and non-zero 2280 * if it does. If the callback returns non-zero and a reference to the 2281 * current device can be obtained, this function will return to the caller 2282 * and not iterate over any more devices. 2283 * 2284 * NOTE: you will need to drop the reference with put_device() after use. 2285 */ 2286 struct device *device_find_child(struct device *parent, void *data, 2287 int (*match)(struct device *dev, void *data)) 2288 { 2289 struct klist_iter i; 2290 struct device *child; 2291 2292 if (!parent) 2293 return NULL; 2294 2295 klist_iter_init(&parent->p->klist_children, &i); 2296 while ((child = next_device(&i))) 2297 if (match(child, data) && get_device(child)) 2298 break; 2299 klist_iter_exit(&i); 2300 return child; 2301 } 2302 EXPORT_SYMBOL_GPL(device_find_child); 2303 2304 int __init devices_init(void) 2305 { 2306 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL); 2307 if (!devices_kset) 2308 return -ENOMEM; 2309 dev_kobj = kobject_create_and_add("dev", NULL); 2310 if (!dev_kobj) 2311 goto dev_kobj_err; 2312 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj); 2313 if (!sysfs_dev_block_kobj) 2314 goto block_kobj_err; 2315 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj); 2316 if (!sysfs_dev_char_kobj) 2317 goto char_kobj_err; 2318 2319 return 0; 2320 2321 char_kobj_err: 2322 kobject_put(sysfs_dev_block_kobj); 2323 block_kobj_err: 2324 kobject_put(dev_kobj); 2325 dev_kobj_err: 2326 kset_unregister(devices_kset); 2327 return -ENOMEM; 2328 } 2329 2330 static int device_check_offline(struct device *dev, void *not_used) 2331 { 2332 int ret; 2333 2334 ret = device_for_each_child(dev, NULL, device_check_offline); 2335 if (ret) 2336 return ret; 2337 2338 return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0; 2339 } 2340 2341 /** 2342 * device_offline - Prepare the device for hot-removal. 2343 * @dev: Device to be put offline. 2344 * 2345 * Execute the device bus type's .offline() callback, if present, to prepare 2346 * the device for a subsequent hot-removal. If that succeeds, the device must 2347 * not be used until either it is removed or its bus type's .online() callback 2348 * is executed. 2349 * 2350 * Call under device_hotplug_lock. 2351 */ 2352 int device_offline(struct device *dev) 2353 { 2354 int ret; 2355 2356 if (dev->offline_disabled) 2357 return -EPERM; 2358 2359 ret = device_for_each_child(dev, NULL, device_check_offline); 2360 if (ret) 2361 return ret; 2362 2363 device_lock(dev); 2364 if (device_supports_offline(dev)) { 2365 if (dev->offline) { 2366 ret = 1; 2367 } else { 2368 ret = dev->bus->offline(dev); 2369 if (!ret) { 2370 kobject_uevent(&dev->kobj, KOBJ_OFFLINE); 2371 dev->offline = true; 2372 } 2373 } 2374 } 2375 device_unlock(dev); 2376 2377 return ret; 2378 } 2379 2380 /** 2381 * device_online - Put the device back online after successful device_offline(). 2382 * @dev: Device to be put back online. 2383 * 2384 * If device_offline() has been successfully executed for @dev, but the device 2385 * has not been removed subsequently, execute its bus type's .online() callback 2386 * to indicate that the device can be used again. 2387 * 2388 * Call under device_hotplug_lock. 2389 */ 2390 int device_online(struct device *dev) 2391 { 2392 int ret = 0; 2393 2394 device_lock(dev); 2395 if (device_supports_offline(dev)) { 2396 if (dev->offline) { 2397 ret = dev->bus->online(dev); 2398 if (!ret) { 2399 kobject_uevent(&dev->kobj, KOBJ_ONLINE); 2400 dev->offline = false; 2401 } 2402 } else { 2403 ret = 1; 2404 } 2405 } 2406 device_unlock(dev); 2407 2408 return ret; 2409 } 2410 2411 struct root_device { 2412 struct device dev; 2413 struct module *owner; 2414 }; 2415 2416 static inline struct root_device *to_root_device(struct device *d) 2417 { 2418 return container_of(d, struct root_device, dev); 2419 } 2420 2421 static void root_device_release(struct device *dev) 2422 { 2423 kfree(to_root_device(dev)); 2424 } 2425 2426 /** 2427 * __root_device_register - allocate and register a root device 2428 * @name: root device name 2429 * @owner: owner module of the root device, usually THIS_MODULE 2430 * 2431 * This function allocates a root device and registers it 2432 * using device_register(). In order to free the returned 2433 * device, use root_device_unregister(). 2434 * 2435 * Root devices are dummy devices which allow other devices 2436 * to be grouped under /sys/devices. Use this function to 2437 * allocate a root device and then use it as the parent of 2438 * any device which should appear under /sys/devices/{name} 2439 * 2440 * The /sys/devices/{name} directory will also contain a 2441 * 'module' symlink which points to the @owner directory 2442 * in sysfs. 2443 * 2444 * Returns &struct device pointer on success, or ERR_PTR() on error. 2445 * 2446 * Note: You probably want to use root_device_register(). 2447 */ 2448 struct device *__root_device_register(const char *name, struct module *owner) 2449 { 2450 struct root_device *root; 2451 int err = -ENOMEM; 2452 2453 root = kzalloc(sizeof(struct root_device), GFP_KERNEL); 2454 if (!root) 2455 return ERR_PTR(err); 2456 2457 err = dev_set_name(&root->dev, "%s", name); 2458 if (err) { 2459 kfree(root); 2460 return ERR_PTR(err); 2461 } 2462 2463 root->dev.release = root_device_release; 2464 2465 err = device_register(&root->dev); 2466 if (err) { 2467 put_device(&root->dev); 2468 return ERR_PTR(err); 2469 } 2470 2471 #ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */ 2472 if (owner) { 2473 struct module_kobject *mk = &owner->mkobj; 2474 2475 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module"); 2476 if (err) { 2477 device_unregister(&root->dev); 2478 return ERR_PTR(err); 2479 } 2480 root->owner = owner; 2481 } 2482 #endif 2483 2484 return &root->dev; 2485 } 2486 EXPORT_SYMBOL_GPL(__root_device_register); 2487 2488 /** 2489 * root_device_unregister - unregister and free a root device 2490 * @dev: device going away 2491 * 2492 * This function unregisters and cleans up a device that was created by 2493 * root_device_register(). 2494 */ 2495 void root_device_unregister(struct device *dev) 2496 { 2497 struct root_device *root = to_root_device(dev); 2498 2499 if (root->owner) 2500 sysfs_remove_link(&root->dev.kobj, "module"); 2501 2502 device_unregister(dev); 2503 } 2504 EXPORT_SYMBOL_GPL(root_device_unregister); 2505 2506 2507 static void device_create_release(struct device *dev) 2508 { 2509 pr_debug("device: '%s': %s\n", dev_name(dev), __func__); 2510 kfree(dev); 2511 } 2512 2513 static __printf(6, 0) struct device * 2514 device_create_groups_vargs(struct class *class, struct device *parent, 2515 dev_t devt, void *drvdata, 2516 const struct attribute_group **groups, 2517 const char *fmt, va_list args) 2518 { 2519 struct device *dev = NULL; 2520 int retval = -ENODEV; 2521 2522 if (class == NULL || IS_ERR(class)) 2523 goto error; 2524 2525 dev = kzalloc(sizeof(*dev), GFP_KERNEL); 2526 if (!dev) { 2527 retval = -ENOMEM; 2528 goto error; 2529 } 2530 2531 device_initialize(dev); 2532 dev->devt = devt; 2533 dev->class = class; 2534 dev->parent = parent; 2535 dev->groups = groups; 2536 dev->release = device_create_release; 2537 dev_set_drvdata(dev, drvdata); 2538 2539 retval = kobject_set_name_vargs(&dev->kobj, fmt, args); 2540 if (retval) 2541 goto error; 2542 2543 retval = device_add(dev); 2544 if (retval) 2545 goto error; 2546 2547 return dev; 2548 2549 error: 2550 put_device(dev); 2551 return ERR_PTR(retval); 2552 } 2553 2554 /** 2555 * device_create_vargs - creates a device and registers it with sysfs 2556 * @class: pointer to the struct class that this device should be registered to 2557 * @parent: pointer to the parent struct device of this new device, if any 2558 * @devt: the dev_t for the char device to be added 2559 * @drvdata: the data to be added to the device for callbacks 2560 * @fmt: string for the device's name 2561 * @args: va_list for the device's name 2562 * 2563 * This function can be used by char device classes. A struct device 2564 * will be created in sysfs, registered to the specified class. 2565 * 2566 * A "dev" file will be created, showing the dev_t for the device, if 2567 * the dev_t is not 0,0. 2568 * If a pointer to a parent struct device is passed in, the newly created 2569 * struct device will be a child of that device in sysfs. 2570 * The pointer to the struct device will be returned from the call. 2571 * Any further sysfs files that might be required can be created using this 2572 * pointer. 2573 * 2574 * Returns &struct device pointer on success, or ERR_PTR() on error. 2575 * 2576 * Note: the struct class passed to this function must have previously 2577 * been created with a call to class_create(). 2578 */ 2579 struct device *device_create_vargs(struct class *class, struct device *parent, 2580 dev_t devt, void *drvdata, const char *fmt, 2581 va_list args) 2582 { 2583 return device_create_groups_vargs(class, parent, devt, drvdata, NULL, 2584 fmt, args); 2585 } 2586 EXPORT_SYMBOL_GPL(device_create_vargs); 2587 2588 /** 2589 * device_create - creates a device and registers it with sysfs 2590 * @class: pointer to the struct class that this device should be registered to 2591 * @parent: pointer to the parent struct device of this new device, if any 2592 * @devt: the dev_t for the char device to be added 2593 * @drvdata: the data to be added to the device for callbacks 2594 * @fmt: string for the device's name 2595 * 2596 * This function can be used by char device classes. A struct device 2597 * will be created in sysfs, registered to the specified class. 2598 * 2599 * A "dev" file will be created, showing the dev_t for the device, if 2600 * the dev_t is not 0,0. 2601 * If a pointer to a parent struct device is passed in, the newly created 2602 * struct device will be a child of that device in sysfs. 2603 * The pointer to the struct device will be returned from the call. 2604 * Any further sysfs files that might be required can be created using this 2605 * pointer. 2606 * 2607 * Returns &struct device pointer on success, or ERR_PTR() on error. 2608 * 2609 * Note: the struct class passed to this function must have previously 2610 * been created with a call to class_create(). 2611 */ 2612 struct device *device_create(struct class *class, struct device *parent, 2613 dev_t devt, void *drvdata, const char *fmt, ...) 2614 { 2615 va_list vargs; 2616 struct device *dev; 2617 2618 va_start(vargs, fmt); 2619 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs); 2620 va_end(vargs); 2621 return dev; 2622 } 2623 EXPORT_SYMBOL_GPL(device_create); 2624 2625 /** 2626 * device_create_with_groups - creates a device and registers it with sysfs 2627 * @class: pointer to the struct class that this device should be registered to 2628 * @parent: pointer to the parent struct device of this new device, if any 2629 * @devt: the dev_t for the char device to be added 2630 * @drvdata: the data to be added to the device for callbacks 2631 * @groups: NULL-terminated list of attribute groups to be created 2632 * @fmt: string for the device's name 2633 * 2634 * This function can be used by char device classes. A struct device 2635 * will be created in sysfs, registered to the specified class. 2636 * Additional attributes specified in the groups parameter will also 2637 * be created automatically. 2638 * 2639 * A "dev" file will be created, showing the dev_t for the device, if 2640 * the dev_t is not 0,0. 2641 * If a pointer to a parent struct device is passed in, the newly created 2642 * struct device will be a child of that device in sysfs. 2643 * The pointer to the struct device will be returned from the call. 2644 * Any further sysfs files that might be required can be created using this 2645 * pointer. 2646 * 2647 * Returns &struct device pointer on success, or ERR_PTR() on error. 2648 * 2649 * Note: the struct class passed to this function must have previously 2650 * been created with a call to class_create(). 2651 */ 2652 struct device *device_create_with_groups(struct class *class, 2653 struct device *parent, dev_t devt, 2654 void *drvdata, 2655 const struct attribute_group **groups, 2656 const char *fmt, ...) 2657 { 2658 va_list vargs; 2659 struct device *dev; 2660 2661 va_start(vargs, fmt); 2662 dev = device_create_groups_vargs(class, parent, devt, drvdata, groups, 2663 fmt, vargs); 2664 va_end(vargs); 2665 return dev; 2666 } 2667 EXPORT_SYMBOL_GPL(device_create_with_groups); 2668 2669 static int __match_devt(struct device *dev, const void *data) 2670 { 2671 const dev_t *devt = data; 2672 2673 return dev->devt == *devt; 2674 } 2675 2676 /** 2677 * device_destroy - removes a device that was created with device_create() 2678 * @class: pointer to the struct class that this device was registered with 2679 * @devt: the dev_t of the device that was previously registered 2680 * 2681 * This call unregisters and cleans up a device that was created with a 2682 * call to device_create(). 2683 */ 2684 void device_destroy(struct class *class, dev_t devt) 2685 { 2686 struct device *dev; 2687 2688 dev = class_find_device(class, NULL, &devt, __match_devt); 2689 if (dev) { 2690 put_device(dev); 2691 device_unregister(dev); 2692 } 2693 } 2694 EXPORT_SYMBOL_GPL(device_destroy); 2695 2696 /** 2697 * device_rename - renames a device 2698 * @dev: the pointer to the struct device to be renamed 2699 * @new_name: the new name of the device 2700 * 2701 * It is the responsibility of the caller to provide mutual 2702 * exclusion between two different calls of device_rename 2703 * on the same device to ensure that new_name is valid and 2704 * won't conflict with other devices. 2705 * 2706 * Note: Don't call this function. Currently, the networking layer calls this 2707 * function, but that will change. The following text from Kay Sievers offers 2708 * some insight: 2709 * 2710 * Renaming devices is racy at many levels, symlinks and other stuff are not 2711 * replaced atomically, and you get a "move" uevent, but it's not easy to 2712 * connect the event to the old and new device. Device nodes are not renamed at 2713 * all, there isn't even support for that in the kernel now. 2714 * 2715 * In the meantime, during renaming, your target name might be taken by another 2716 * driver, creating conflicts. Or the old name is taken directly after you 2717 * renamed it -- then you get events for the same DEVPATH, before you even see 2718 * the "move" event. It's just a mess, and nothing new should ever rely on 2719 * kernel device renaming. Besides that, it's not even implemented now for 2720 * other things than (driver-core wise very simple) network devices. 2721 * 2722 * We are currently about to change network renaming in udev to completely 2723 * disallow renaming of devices in the same namespace as the kernel uses, 2724 * because we can't solve the problems properly, that arise with swapping names 2725 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only 2726 * be allowed to some other name than eth[0-9]*, for the aforementioned 2727 * reasons. 2728 * 2729 * Make up a "real" name in the driver before you register anything, or add 2730 * some other attributes for userspace to find the device, or use udev to add 2731 * symlinks -- but never rename kernel devices later, it's a complete mess. We 2732 * don't even want to get into that and try to implement the missing pieces in 2733 * the core. We really have other pieces to fix in the driver core mess. :) 2734 */ 2735 int device_rename(struct device *dev, const char *new_name) 2736 { 2737 struct kobject *kobj = &dev->kobj; 2738 char *old_device_name = NULL; 2739 int error; 2740 2741 dev = get_device(dev); 2742 if (!dev) 2743 return -EINVAL; 2744 2745 dev_dbg(dev, "renaming to %s\n", new_name); 2746 2747 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL); 2748 if (!old_device_name) { 2749 error = -ENOMEM; 2750 goto out; 2751 } 2752 2753 if (dev->class) { 2754 error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj, 2755 kobj, old_device_name, 2756 new_name, kobject_namespace(kobj)); 2757 if (error) 2758 goto out; 2759 } 2760 2761 error = kobject_rename(kobj, new_name); 2762 if (error) 2763 goto out; 2764 2765 out: 2766 put_device(dev); 2767 2768 kfree(old_device_name); 2769 2770 return error; 2771 } 2772 EXPORT_SYMBOL_GPL(device_rename); 2773 2774 static int device_move_class_links(struct device *dev, 2775 struct device *old_parent, 2776 struct device *new_parent) 2777 { 2778 int error = 0; 2779 2780 if (old_parent) 2781 sysfs_remove_link(&dev->kobj, "device"); 2782 if (new_parent) 2783 error = sysfs_create_link(&dev->kobj, &new_parent->kobj, 2784 "device"); 2785 return error; 2786 } 2787 2788 /** 2789 * device_move - moves a device to a new parent 2790 * @dev: the pointer to the struct device to be moved 2791 * @new_parent: the new parent of the device (can be NULL) 2792 * @dpm_order: how to reorder the dpm_list 2793 */ 2794 int device_move(struct device *dev, struct device *new_parent, 2795 enum dpm_order dpm_order) 2796 { 2797 int error; 2798 struct device *old_parent; 2799 struct kobject *new_parent_kobj; 2800 2801 dev = get_device(dev); 2802 if (!dev) 2803 return -EINVAL; 2804 2805 device_pm_lock(); 2806 new_parent = get_device(new_parent); 2807 new_parent_kobj = get_device_parent(dev, new_parent); 2808 if (IS_ERR(new_parent_kobj)) { 2809 error = PTR_ERR(new_parent_kobj); 2810 put_device(new_parent); 2811 goto out; 2812 } 2813 2814 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev), 2815 __func__, new_parent ? dev_name(new_parent) : "<NULL>"); 2816 error = kobject_move(&dev->kobj, new_parent_kobj); 2817 if (error) { 2818 cleanup_glue_dir(dev, new_parent_kobj); 2819 put_device(new_parent); 2820 goto out; 2821 } 2822 old_parent = dev->parent; 2823 dev->parent = new_parent; 2824 if (old_parent) 2825 klist_remove(&dev->p->knode_parent); 2826 if (new_parent) { 2827 klist_add_tail(&dev->p->knode_parent, 2828 &new_parent->p->klist_children); 2829 set_dev_node(dev, dev_to_node(new_parent)); 2830 } 2831 2832 if (dev->class) { 2833 error = device_move_class_links(dev, old_parent, new_parent); 2834 if (error) { 2835 /* We ignore errors on cleanup since we're hosed anyway... */ 2836 device_move_class_links(dev, new_parent, old_parent); 2837 if (!kobject_move(&dev->kobj, &old_parent->kobj)) { 2838 if (new_parent) 2839 klist_remove(&dev->p->knode_parent); 2840 dev->parent = old_parent; 2841 if (old_parent) { 2842 klist_add_tail(&dev->p->knode_parent, 2843 &old_parent->p->klist_children); 2844 set_dev_node(dev, dev_to_node(old_parent)); 2845 } 2846 } 2847 cleanup_glue_dir(dev, new_parent_kobj); 2848 put_device(new_parent); 2849 goto out; 2850 } 2851 } 2852 switch (dpm_order) { 2853 case DPM_ORDER_NONE: 2854 break; 2855 case DPM_ORDER_DEV_AFTER_PARENT: 2856 device_pm_move_after(dev, new_parent); 2857 devices_kset_move_after(dev, new_parent); 2858 break; 2859 case DPM_ORDER_PARENT_BEFORE_DEV: 2860 device_pm_move_before(new_parent, dev); 2861 devices_kset_move_before(new_parent, dev); 2862 break; 2863 case DPM_ORDER_DEV_LAST: 2864 device_pm_move_last(dev); 2865 devices_kset_move_last(dev); 2866 break; 2867 } 2868 2869 put_device(old_parent); 2870 out: 2871 device_pm_unlock(); 2872 put_device(dev); 2873 return error; 2874 } 2875 EXPORT_SYMBOL_GPL(device_move); 2876 2877 /** 2878 * device_shutdown - call ->shutdown() on each device to shutdown. 2879 */ 2880 void device_shutdown(void) 2881 { 2882 struct device *dev, *parent; 2883 2884 wait_for_device_probe(); 2885 device_block_probing(); 2886 2887 spin_lock(&devices_kset->list_lock); 2888 /* 2889 * Walk the devices list backward, shutting down each in turn. 2890 * Beware that device unplug events may also start pulling 2891 * devices offline, even as the system is shutting down. 2892 */ 2893 while (!list_empty(&devices_kset->list)) { 2894 dev = list_entry(devices_kset->list.prev, struct device, 2895 kobj.entry); 2896 2897 /* 2898 * hold reference count of device's parent to 2899 * prevent it from being freed because parent's 2900 * lock is to be held 2901 */ 2902 parent = get_device(dev->parent); 2903 get_device(dev); 2904 /* 2905 * Make sure the device is off the kset list, in the 2906 * event that dev->*->shutdown() doesn't remove it. 2907 */ 2908 list_del_init(&dev->kobj.entry); 2909 spin_unlock(&devices_kset->list_lock); 2910 2911 /* hold lock to avoid race with probe/release */ 2912 if (parent) 2913 device_lock(parent); 2914 device_lock(dev); 2915 2916 /* Don't allow any more runtime suspends */ 2917 pm_runtime_get_noresume(dev); 2918 pm_runtime_barrier(dev); 2919 2920 if (dev->class && dev->class->shutdown_pre) { 2921 if (initcall_debug) 2922 dev_info(dev, "shutdown_pre\n"); 2923 dev->class->shutdown_pre(dev); 2924 } 2925 if (dev->bus && dev->bus->shutdown) { 2926 if (initcall_debug) 2927 dev_info(dev, "shutdown\n"); 2928 dev->bus->shutdown(dev); 2929 } else if (dev->driver && dev->driver->shutdown) { 2930 if (initcall_debug) 2931 dev_info(dev, "shutdown\n"); 2932 dev->driver->shutdown(dev); 2933 } 2934 2935 device_unlock(dev); 2936 if (parent) 2937 device_unlock(parent); 2938 2939 put_device(dev); 2940 put_device(parent); 2941 2942 spin_lock(&devices_kset->list_lock); 2943 } 2944 spin_unlock(&devices_kset->list_lock); 2945 } 2946 2947 /* 2948 * Device logging functions 2949 */ 2950 2951 #ifdef CONFIG_PRINTK 2952 static int 2953 create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen) 2954 { 2955 const char *subsys; 2956 size_t pos = 0; 2957 2958 if (dev->class) 2959 subsys = dev->class->name; 2960 else if (dev->bus) 2961 subsys = dev->bus->name; 2962 else 2963 return 0; 2964 2965 pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys); 2966 if (pos >= hdrlen) 2967 goto overflow; 2968 2969 /* 2970 * Add device identifier DEVICE=: 2971 * b12:8 block dev_t 2972 * c127:3 char dev_t 2973 * n8 netdev ifindex 2974 * +sound:card0 subsystem:devname 2975 */ 2976 if (MAJOR(dev->devt)) { 2977 char c; 2978 2979 if (strcmp(subsys, "block") == 0) 2980 c = 'b'; 2981 else 2982 c = 'c'; 2983 pos++; 2984 pos += snprintf(hdr + pos, hdrlen - pos, 2985 "DEVICE=%c%u:%u", 2986 c, MAJOR(dev->devt), MINOR(dev->devt)); 2987 } else if (strcmp(subsys, "net") == 0) { 2988 struct net_device *net = to_net_dev(dev); 2989 2990 pos++; 2991 pos += snprintf(hdr + pos, hdrlen - pos, 2992 "DEVICE=n%u", net->ifindex); 2993 } else { 2994 pos++; 2995 pos += snprintf(hdr + pos, hdrlen - pos, 2996 "DEVICE=+%s:%s", subsys, dev_name(dev)); 2997 } 2998 2999 if (pos >= hdrlen) 3000 goto overflow; 3001 3002 return pos; 3003 3004 overflow: 3005 dev_WARN(dev, "device/subsystem name too long"); 3006 return 0; 3007 } 3008 3009 int dev_vprintk_emit(int level, const struct device *dev, 3010 const char *fmt, va_list args) 3011 { 3012 char hdr[128]; 3013 size_t hdrlen; 3014 3015 hdrlen = create_syslog_header(dev, hdr, sizeof(hdr)); 3016 3017 return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args); 3018 } 3019 EXPORT_SYMBOL(dev_vprintk_emit); 3020 3021 int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...) 3022 { 3023 va_list args; 3024 int r; 3025 3026 va_start(args, fmt); 3027 3028 r = dev_vprintk_emit(level, dev, fmt, args); 3029 3030 va_end(args); 3031 3032 return r; 3033 } 3034 EXPORT_SYMBOL(dev_printk_emit); 3035 3036 static void __dev_printk(const char *level, const struct device *dev, 3037 struct va_format *vaf) 3038 { 3039 if (dev) 3040 dev_printk_emit(level[1] - '0', dev, "%s %s: %pV", 3041 dev_driver_string(dev), dev_name(dev), vaf); 3042 else 3043 printk("%s(NULL device *): %pV", level, vaf); 3044 } 3045 3046 void dev_printk(const char *level, const struct device *dev, 3047 const char *fmt, ...) 3048 { 3049 struct va_format vaf; 3050 va_list args; 3051 3052 va_start(args, fmt); 3053 3054 vaf.fmt = fmt; 3055 vaf.va = &args; 3056 3057 __dev_printk(level, dev, &vaf); 3058 3059 va_end(args); 3060 } 3061 EXPORT_SYMBOL(dev_printk); 3062 3063 #define define_dev_printk_level(func, kern_level) \ 3064 void func(const struct device *dev, const char *fmt, ...) \ 3065 { \ 3066 struct va_format vaf; \ 3067 va_list args; \ 3068 \ 3069 va_start(args, fmt); \ 3070 \ 3071 vaf.fmt = fmt; \ 3072 vaf.va = &args; \ 3073 \ 3074 __dev_printk(kern_level, dev, &vaf); \ 3075 \ 3076 va_end(args); \ 3077 } \ 3078 EXPORT_SYMBOL(func); 3079 3080 define_dev_printk_level(_dev_emerg, KERN_EMERG); 3081 define_dev_printk_level(_dev_alert, KERN_ALERT); 3082 define_dev_printk_level(_dev_crit, KERN_CRIT); 3083 define_dev_printk_level(_dev_err, KERN_ERR); 3084 define_dev_printk_level(_dev_warn, KERN_WARNING); 3085 define_dev_printk_level(_dev_notice, KERN_NOTICE); 3086 define_dev_printk_level(_dev_info, KERN_INFO); 3087 3088 #endif 3089 3090 static inline bool fwnode_is_primary(struct fwnode_handle *fwnode) 3091 { 3092 return fwnode && !IS_ERR(fwnode->secondary); 3093 } 3094 3095 /** 3096 * set_primary_fwnode - Change the primary firmware node of a given device. 3097 * @dev: Device to handle. 3098 * @fwnode: New primary firmware node of the device. 3099 * 3100 * Set the device's firmware node pointer to @fwnode, but if a secondary 3101 * firmware node of the device is present, preserve it. 3102 */ 3103 void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode) 3104 { 3105 if (fwnode) { 3106 struct fwnode_handle *fn = dev->fwnode; 3107 3108 if (fwnode_is_primary(fn)) 3109 fn = fn->secondary; 3110 3111 if (fn) { 3112 WARN_ON(fwnode->secondary); 3113 fwnode->secondary = fn; 3114 } 3115 dev->fwnode = fwnode; 3116 } else { 3117 dev->fwnode = fwnode_is_primary(dev->fwnode) ? 3118 dev->fwnode->secondary : NULL; 3119 } 3120 } 3121 EXPORT_SYMBOL_GPL(set_primary_fwnode); 3122 3123 /** 3124 * set_secondary_fwnode - Change the secondary firmware node of a given device. 3125 * @dev: Device to handle. 3126 * @fwnode: New secondary firmware node of the device. 3127 * 3128 * If a primary firmware node of the device is present, set its secondary 3129 * pointer to @fwnode. Otherwise, set the device's firmware node pointer to 3130 * @fwnode. 3131 */ 3132 void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode) 3133 { 3134 if (fwnode) 3135 fwnode->secondary = ERR_PTR(-ENODEV); 3136 3137 if (fwnode_is_primary(dev->fwnode)) 3138 dev->fwnode->secondary = fwnode; 3139 else 3140 dev->fwnode = fwnode; 3141 } 3142 3143 /** 3144 * device_set_of_node_from_dev - reuse device-tree node of another device 3145 * @dev: device whose device-tree node is being set 3146 * @dev2: device whose device-tree node is being reused 3147 * 3148 * Takes another reference to the new device-tree node after first dropping 3149 * any reference held to the old node. 3150 */ 3151 void device_set_of_node_from_dev(struct device *dev, const struct device *dev2) 3152 { 3153 of_node_put(dev->of_node); 3154 dev->of_node = of_node_get(dev2->of_node); 3155 dev->of_node_reused = true; 3156 } 3157 EXPORT_SYMBOL_GPL(device_set_of_node_from_dev); 3158