1 // SPDX-License-Identifier: GPL-2.0
3 * drivers/base/core.c - core driver model code (device registration, etc)
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.
11 #include <linux/acpi.h>
12 #include <linux/cpufreq.h>
13 #include <linux/device.h>
14 #include <linux/err.h>
15 #include <linux/fwnode.h>
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/string.h>
20 #include <linux/kdev_t.h>
21 #include <linux/notifier.h>
23 #include <linux/of_device.h>
24 #include <linux/genhd.h>
25 #include <linux/mutex.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/netdevice.h>
28 #include <linux/sched/signal.h>
29 #include <linux/sched/mm.h>
30 #include <linux/sysfs.h>
31 #include <linux/dma-map-ops.h> /* for dma_default_coherent */
34 #include "power/power.h"
36 #ifdef CONFIG_SYSFS_DEPRECATED
37 #ifdef CONFIG_SYSFS_DEPRECATED_V2
38 long sysfs_deprecated = 1;
40 long sysfs_deprecated = 0;
42 static int __init sysfs_deprecated_setup(char *arg)
44 return kstrtol(arg, 10, &sysfs_deprecated);
46 early_param("sysfs.deprecated", sysfs_deprecated_setup);
49 /* Device links support. */
50 static LIST_HEAD(deferred_sync);
51 static unsigned int defer_sync_state_count = 1;
52 static DEFINE_MUTEX(fwnode_link_lock);
53 static bool fw_devlink_is_permissive(void);
56 * fwnode_link_add - Create a link between two fwnode_handles.
57 * @con: Consumer end of the link.
58 * @sup: Supplier end of the link.
60 * Create a fwnode link between fwnode handles @con and @sup. The fwnode link
61 * represents the detail that the firmware lists @sup fwnode as supplying a
64 * The driver core will use the fwnode link to create a device link between the
65 * two device objects corresponding to @con and @sup when they are created. The
66 * driver core will automatically delete the fwnode link between @con and @sup
69 * Attempts to create duplicate links between the same pair of fwnode handles
70 * are ignored and there is no reference counting.
72 int fwnode_link_add(struct fwnode_handle *con, struct fwnode_handle *sup)
74 struct fwnode_link *link;
77 mutex_lock(&fwnode_link_lock);
79 list_for_each_entry(link, &sup->consumers, s_hook)
80 if (link->consumer == con)
83 link = kzalloc(sizeof(*link), GFP_KERNEL);
90 INIT_LIST_HEAD(&link->s_hook);
92 INIT_LIST_HEAD(&link->c_hook);
94 list_add(&link->s_hook, &sup->consumers);
95 list_add(&link->c_hook, &con->suppliers);
97 mutex_unlock(&fwnode_link_lock);
103 * fwnode_links_purge_suppliers - Delete all supplier links of fwnode_handle.
104 * @fwnode: fwnode whose supplier links need to be deleted
106 * Deletes all supplier links connecting directly to @fwnode.
108 static void fwnode_links_purge_suppliers(struct fwnode_handle *fwnode)
110 struct fwnode_link *link, *tmp;
112 mutex_lock(&fwnode_link_lock);
113 list_for_each_entry_safe(link, tmp, &fwnode->suppliers, c_hook) {
114 list_del(&link->s_hook);
115 list_del(&link->c_hook);
118 mutex_unlock(&fwnode_link_lock);
122 * fwnode_links_purge_consumers - Delete all consumer links of fwnode_handle.
123 * @fwnode: fwnode whose consumer links need to be deleted
125 * Deletes all consumer links connecting directly to @fwnode.
127 static void fwnode_links_purge_consumers(struct fwnode_handle *fwnode)
129 struct fwnode_link *link, *tmp;
131 mutex_lock(&fwnode_link_lock);
132 list_for_each_entry_safe(link, tmp, &fwnode->consumers, s_hook) {
133 list_del(&link->s_hook);
134 list_del(&link->c_hook);
137 mutex_unlock(&fwnode_link_lock);
141 * fwnode_links_purge - Delete all links connected to a fwnode_handle.
142 * @fwnode: fwnode whose links needs to be deleted
144 * Deletes all links connecting directly to a fwnode.
146 void fwnode_links_purge(struct fwnode_handle *fwnode)
148 fwnode_links_purge_suppliers(fwnode);
149 fwnode_links_purge_consumers(fwnode);
153 static DEFINE_MUTEX(device_links_lock);
154 DEFINE_STATIC_SRCU(device_links_srcu);
156 static inline void device_links_write_lock(void)
158 mutex_lock(&device_links_lock);
161 static inline void device_links_write_unlock(void)
163 mutex_unlock(&device_links_lock);
166 int device_links_read_lock(void) __acquires(&device_links_srcu)
168 return srcu_read_lock(&device_links_srcu);
171 void device_links_read_unlock(int idx) __releases(&device_links_srcu)
173 srcu_read_unlock(&device_links_srcu, idx);
176 int device_links_read_lock_held(void)
178 return srcu_read_lock_held(&device_links_srcu);
180 #else /* !CONFIG_SRCU */
181 static DECLARE_RWSEM(device_links_lock);
183 static inline void device_links_write_lock(void)
185 down_write(&device_links_lock);
188 static inline void device_links_write_unlock(void)
190 up_write(&device_links_lock);
193 int device_links_read_lock(void)
195 down_read(&device_links_lock);
199 void device_links_read_unlock(int not_used)
201 up_read(&device_links_lock);
204 #ifdef CONFIG_DEBUG_LOCK_ALLOC
205 int device_links_read_lock_held(void)
207 return lockdep_is_held(&device_links_lock);
210 #endif /* !CONFIG_SRCU */
212 static bool device_is_ancestor(struct device *dev, struct device *target)
214 while (target->parent) {
215 target = target->parent;
223 * device_is_dependent - Check if one device depends on another one
224 * @dev: Device to check dependencies for.
225 * @target: Device to check against.
227 * Check if @target depends on @dev or any device dependent on it (its child or
228 * its consumer etc). Return 1 if that is the case or 0 otherwise.
230 int device_is_dependent(struct device *dev, void *target)
232 struct device_link *link;
236 * The "ancestors" check is needed to catch the case when the target
237 * device has not been completely initialized yet and it is still
238 * missing from the list of children of its parent device.
240 if (dev == target || device_is_ancestor(dev, target))
243 ret = device_for_each_child(dev, target, device_is_dependent);
247 list_for_each_entry(link, &dev->links.consumers, s_node) {
248 if (link->flags == (DL_FLAG_SYNC_STATE_ONLY | DL_FLAG_MANAGED))
251 if (link->consumer == target)
254 ret = device_is_dependent(link->consumer, target);
261 static void device_link_init_status(struct device_link *link,
262 struct device *consumer,
263 struct device *supplier)
265 switch (supplier->links.status) {
267 switch (consumer->links.status) {
270 * A consumer driver can create a link to a supplier
271 * that has not completed its probing yet as long as it
272 * knows that the supplier is already functional (for
273 * example, it has just acquired some resources from the
276 link->status = DL_STATE_CONSUMER_PROBE;
279 link->status = DL_STATE_DORMANT;
283 case DL_DEV_DRIVER_BOUND:
284 switch (consumer->links.status) {
286 link->status = DL_STATE_CONSUMER_PROBE;
288 case DL_DEV_DRIVER_BOUND:
289 link->status = DL_STATE_ACTIVE;
292 link->status = DL_STATE_AVAILABLE;
296 case DL_DEV_UNBINDING:
297 link->status = DL_STATE_SUPPLIER_UNBIND;
300 link->status = DL_STATE_DORMANT;
305 static int device_reorder_to_tail(struct device *dev, void *not_used)
307 struct device_link *link;
310 * Devices that have not been registered yet will be put to the ends
311 * of the lists during the registration, so skip them here.
313 if (device_is_registered(dev))
314 devices_kset_move_last(dev);
316 if (device_pm_initialized(dev))
317 device_pm_move_last(dev);
319 device_for_each_child(dev, NULL, device_reorder_to_tail);
320 list_for_each_entry(link, &dev->links.consumers, s_node) {
321 if (link->flags == (DL_FLAG_SYNC_STATE_ONLY | DL_FLAG_MANAGED))
323 device_reorder_to_tail(link->consumer, NULL);
330 * device_pm_move_to_tail - Move set of devices to the end of device lists
331 * @dev: Device to move
333 * This is a device_reorder_to_tail() wrapper taking the requisite locks.
335 * It moves the @dev along with all of its children and all of its consumers
336 * to the ends of the device_kset and dpm_list, recursively.
338 void device_pm_move_to_tail(struct device *dev)
342 idx = device_links_read_lock();
344 device_reorder_to_tail(dev, NULL);
346 device_links_read_unlock(idx);
349 #define to_devlink(dev) container_of((dev), struct device_link, link_dev)
351 static ssize_t status_show(struct device *dev,
352 struct device_attribute *attr, char *buf)
356 switch (to_devlink(dev)->status) {
358 output = "not tracked";
360 case DL_STATE_DORMANT:
363 case DL_STATE_AVAILABLE:
364 output = "available";
366 case DL_STATE_CONSUMER_PROBE:
367 output = "consumer probing";
369 case DL_STATE_ACTIVE:
372 case DL_STATE_SUPPLIER_UNBIND:
373 output = "supplier unbinding";
380 return sysfs_emit(buf, "%s\n", output);
382 static DEVICE_ATTR_RO(status);
384 static ssize_t auto_remove_on_show(struct device *dev,
385 struct device_attribute *attr, char *buf)
387 struct device_link *link = to_devlink(dev);
390 if (link->flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
391 output = "supplier unbind";
392 else if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER)
393 output = "consumer unbind";
397 return sysfs_emit(buf, "%s\n", output);
399 static DEVICE_ATTR_RO(auto_remove_on);
401 static ssize_t runtime_pm_show(struct device *dev,
402 struct device_attribute *attr, char *buf)
404 struct device_link *link = to_devlink(dev);
406 return sysfs_emit(buf, "%d\n", !!(link->flags & DL_FLAG_PM_RUNTIME));
408 static DEVICE_ATTR_RO(runtime_pm);
410 static ssize_t sync_state_only_show(struct device *dev,
411 struct device_attribute *attr, char *buf)
413 struct device_link *link = to_devlink(dev);
415 return sysfs_emit(buf, "%d\n",
416 !!(link->flags & DL_FLAG_SYNC_STATE_ONLY));
418 static DEVICE_ATTR_RO(sync_state_only);
420 static struct attribute *devlink_attrs[] = {
421 &dev_attr_status.attr,
422 &dev_attr_auto_remove_on.attr,
423 &dev_attr_runtime_pm.attr,
424 &dev_attr_sync_state_only.attr,
427 ATTRIBUTE_GROUPS(devlink);
429 static void device_link_free(struct device_link *link)
431 while (refcount_dec_not_one(&link->rpm_active))
432 pm_runtime_put(link->supplier);
434 put_device(link->consumer);
435 put_device(link->supplier);
440 static void __device_link_free_srcu(struct rcu_head *rhead)
442 device_link_free(container_of(rhead, struct device_link, rcu_head));
445 static void devlink_dev_release(struct device *dev)
447 struct device_link *link = to_devlink(dev);
449 call_srcu(&device_links_srcu, &link->rcu_head, __device_link_free_srcu);
452 static void devlink_dev_release(struct device *dev)
454 device_link_free(to_devlink(dev));
458 static struct class devlink_class = {
460 .owner = THIS_MODULE,
461 .dev_groups = devlink_groups,
462 .dev_release = devlink_dev_release,
465 static int devlink_add_symlinks(struct device *dev,
466 struct class_interface *class_intf)
470 struct device_link *link = to_devlink(dev);
471 struct device *sup = link->supplier;
472 struct device *con = link->consumer;
475 len = max(strlen(dev_bus_name(sup)) + strlen(dev_name(sup)),
476 strlen(dev_bus_name(con)) + strlen(dev_name(con)));
478 len += strlen("supplier:") + 1;
479 buf = kzalloc(len, GFP_KERNEL);
483 ret = sysfs_create_link(&link->link_dev.kobj, &sup->kobj, "supplier");
487 ret = sysfs_create_link(&link->link_dev.kobj, &con->kobj, "consumer");
491 snprintf(buf, len, "consumer:%s:%s", dev_bus_name(con), dev_name(con));
492 ret = sysfs_create_link(&sup->kobj, &link->link_dev.kobj, buf);
496 snprintf(buf, len, "supplier:%s:%s", dev_bus_name(sup), dev_name(sup));
497 ret = sysfs_create_link(&con->kobj, &link->link_dev.kobj, buf);
504 snprintf(buf, len, "consumer:%s:%s", dev_bus_name(con), dev_name(con));
505 sysfs_remove_link(&sup->kobj, buf);
507 sysfs_remove_link(&link->link_dev.kobj, "consumer");
509 sysfs_remove_link(&link->link_dev.kobj, "supplier");
515 static void devlink_remove_symlinks(struct device *dev,
516 struct class_interface *class_intf)
518 struct device_link *link = to_devlink(dev);
520 struct device *sup = link->supplier;
521 struct device *con = link->consumer;
524 sysfs_remove_link(&link->link_dev.kobj, "consumer");
525 sysfs_remove_link(&link->link_dev.kobj, "supplier");
527 len = max(strlen(dev_bus_name(sup)) + strlen(dev_name(sup)),
528 strlen(dev_bus_name(con)) + strlen(dev_name(con)));
530 len += strlen("supplier:") + 1;
531 buf = kzalloc(len, GFP_KERNEL);
533 WARN(1, "Unable to properly free device link symlinks!\n");
537 snprintf(buf, len, "supplier:%s:%s", dev_bus_name(sup), dev_name(sup));
538 sysfs_remove_link(&con->kobj, buf);
539 snprintf(buf, len, "consumer:%s:%s", dev_bus_name(con), dev_name(con));
540 sysfs_remove_link(&sup->kobj, buf);
544 static struct class_interface devlink_class_intf = {
545 .class = &devlink_class,
546 .add_dev = devlink_add_symlinks,
547 .remove_dev = devlink_remove_symlinks,
550 static int __init devlink_class_init(void)
554 ret = class_register(&devlink_class);
558 ret = class_interface_register(&devlink_class_intf);
560 class_unregister(&devlink_class);
564 postcore_initcall(devlink_class_init);
566 #define DL_MANAGED_LINK_FLAGS (DL_FLAG_AUTOREMOVE_CONSUMER | \
567 DL_FLAG_AUTOREMOVE_SUPPLIER | \
568 DL_FLAG_AUTOPROBE_CONSUMER | \
569 DL_FLAG_SYNC_STATE_ONLY)
571 #define DL_ADD_VALID_FLAGS (DL_MANAGED_LINK_FLAGS | DL_FLAG_STATELESS | \
572 DL_FLAG_PM_RUNTIME | DL_FLAG_RPM_ACTIVE)
575 * device_link_add - Create a link between two devices.
576 * @consumer: Consumer end of the link.
577 * @supplier: Supplier end of the link.
578 * @flags: Link flags.
580 * The caller is responsible for the proper synchronization of the link creation
581 * with runtime PM. First, setting the DL_FLAG_PM_RUNTIME flag will cause the
582 * runtime PM framework to take the link into account. Second, if the
583 * DL_FLAG_RPM_ACTIVE flag is set in addition to it, the supplier devices will
584 * be forced into the active meta state and reference-counted upon the creation
585 * of the link. If DL_FLAG_PM_RUNTIME is not set, DL_FLAG_RPM_ACTIVE will be
588 * If DL_FLAG_STATELESS is set in @flags, the caller of this function is
589 * expected to release the link returned by it directly with the help of either
590 * device_link_del() or device_link_remove().
592 * If that flag is not set, however, the caller of this function is handing the
593 * management of the link over to the driver core entirely and its return value
594 * can only be used to check whether or not the link is present. In that case,
595 * the DL_FLAG_AUTOREMOVE_CONSUMER and DL_FLAG_AUTOREMOVE_SUPPLIER device link
596 * flags can be used to indicate to the driver core when the link can be safely
597 * deleted. Namely, setting one of them in @flags indicates to the driver core
598 * that the link is not going to be used (by the given caller of this function)
599 * after unbinding the consumer or supplier driver, respectively, from its
600 * device, so the link can be deleted at that point. If none of them is set,
601 * the link will be maintained until one of the devices pointed to by it (either
602 * the consumer or the supplier) is unregistered.
604 * Also, if DL_FLAG_STATELESS, DL_FLAG_AUTOREMOVE_CONSUMER and
605 * DL_FLAG_AUTOREMOVE_SUPPLIER are not set in @flags (that is, a persistent
606 * managed device link is being added), the DL_FLAG_AUTOPROBE_CONSUMER flag can
607 * be used to request the driver core to automatically probe for a consumer
608 * driver after successfully binding a driver to the supplier device.
610 * The combination of DL_FLAG_STATELESS and one of DL_FLAG_AUTOREMOVE_CONSUMER,
611 * DL_FLAG_AUTOREMOVE_SUPPLIER, or DL_FLAG_AUTOPROBE_CONSUMER set in @flags at
612 * the same time is invalid and will cause NULL to be returned upfront.
613 * However, if a device link between the given @consumer and @supplier pair
614 * exists already when this function is called for them, the existing link will
615 * be returned regardless of its current type and status (the link's flags may
616 * be modified then). The caller of this function is then expected to treat
617 * the link as though it has just been created, so (in particular) if
618 * DL_FLAG_STATELESS was passed in @flags, the link needs to be released
619 * explicitly when not needed any more (as stated above).
621 * A side effect of the link creation is re-ordering of dpm_list and the
622 * devices_kset list by moving the consumer device and all devices depending
623 * on it to the ends of these lists (that does not happen to devices that have
624 * not been registered when this function is called).
626 * The supplier device is required to be registered when this function is called
627 * and NULL will be returned if that is not the case. The consumer device need
628 * not be registered, however.
630 struct device_link *device_link_add(struct device *consumer,
631 struct device *supplier, u32 flags)
633 struct device_link *link;
635 if (!consumer || !supplier || flags & ~DL_ADD_VALID_FLAGS ||
636 (flags & DL_FLAG_STATELESS && flags & DL_MANAGED_LINK_FLAGS) ||
637 (flags & DL_FLAG_SYNC_STATE_ONLY &&
638 flags != DL_FLAG_SYNC_STATE_ONLY) ||
639 (flags & DL_FLAG_AUTOPROBE_CONSUMER &&
640 flags & (DL_FLAG_AUTOREMOVE_CONSUMER |
641 DL_FLAG_AUTOREMOVE_SUPPLIER)))
644 if (flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) {
645 if (pm_runtime_get_sync(supplier) < 0) {
646 pm_runtime_put_noidle(supplier);
651 if (!(flags & DL_FLAG_STATELESS))
652 flags |= DL_FLAG_MANAGED;
654 device_links_write_lock();
658 * If the supplier has not been fully registered yet or there is a
659 * reverse (non-SYNC_STATE_ONLY) dependency between the consumer and
660 * the supplier already in the graph, return NULL. If the link is a
661 * SYNC_STATE_ONLY link, we don't check for reverse dependencies
662 * because it only affects sync_state() callbacks.
664 if (!device_pm_initialized(supplier)
665 || (!(flags & DL_FLAG_SYNC_STATE_ONLY) &&
666 device_is_dependent(consumer, supplier))) {
672 * SYNC_STATE_ONLY links are useless once a consumer device has probed.
673 * So, only create it if the consumer hasn't probed yet.
675 if (flags & DL_FLAG_SYNC_STATE_ONLY &&
676 consumer->links.status != DL_DEV_NO_DRIVER &&
677 consumer->links.status != DL_DEV_PROBING) {
683 * DL_FLAG_AUTOREMOVE_SUPPLIER indicates that the link will be needed
684 * longer than for DL_FLAG_AUTOREMOVE_CONSUMER and setting them both
685 * together doesn't make sense, so prefer DL_FLAG_AUTOREMOVE_SUPPLIER.
687 if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
688 flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER;
690 list_for_each_entry(link, &supplier->links.consumers, s_node) {
691 if (link->consumer != consumer)
694 if (flags & DL_FLAG_PM_RUNTIME) {
695 if (!(link->flags & DL_FLAG_PM_RUNTIME)) {
696 pm_runtime_new_link(consumer);
697 link->flags |= DL_FLAG_PM_RUNTIME;
699 if (flags & DL_FLAG_RPM_ACTIVE)
700 refcount_inc(&link->rpm_active);
703 if (flags & DL_FLAG_STATELESS) {
704 kref_get(&link->kref);
705 if (link->flags & DL_FLAG_SYNC_STATE_ONLY &&
706 !(link->flags & DL_FLAG_STATELESS)) {
707 link->flags |= DL_FLAG_STATELESS;
710 link->flags |= DL_FLAG_STATELESS;
716 * If the life time of the link following from the new flags is
717 * longer than indicated by the flags of the existing link,
718 * update the existing link to stay around longer.
720 if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER) {
721 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER) {
722 link->flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER;
723 link->flags |= DL_FLAG_AUTOREMOVE_SUPPLIER;
725 } else if (!(flags & DL_FLAG_AUTOREMOVE_CONSUMER)) {
726 link->flags &= ~(DL_FLAG_AUTOREMOVE_CONSUMER |
727 DL_FLAG_AUTOREMOVE_SUPPLIER);
729 if (!(link->flags & DL_FLAG_MANAGED)) {
730 kref_get(&link->kref);
731 link->flags |= DL_FLAG_MANAGED;
732 device_link_init_status(link, consumer, supplier);
734 if (link->flags & DL_FLAG_SYNC_STATE_ONLY &&
735 !(flags & DL_FLAG_SYNC_STATE_ONLY)) {
736 link->flags &= ~DL_FLAG_SYNC_STATE_ONLY;
743 link = kzalloc(sizeof(*link), GFP_KERNEL);
747 refcount_set(&link->rpm_active, 1);
749 get_device(supplier);
750 link->supplier = supplier;
751 INIT_LIST_HEAD(&link->s_node);
752 get_device(consumer);
753 link->consumer = consumer;
754 INIT_LIST_HEAD(&link->c_node);
756 kref_init(&link->kref);
758 link->link_dev.class = &devlink_class;
759 device_set_pm_not_required(&link->link_dev);
760 dev_set_name(&link->link_dev, "%s:%s--%s:%s",
761 dev_bus_name(supplier), dev_name(supplier),
762 dev_bus_name(consumer), dev_name(consumer));
763 if (device_register(&link->link_dev)) {
764 put_device(consumer);
765 put_device(supplier);
771 if (flags & DL_FLAG_PM_RUNTIME) {
772 if (flags & DL_FLAG_RPM_ACTIVE)
773 refcount_inc(&link->rpm_active);
775 pm_runtime_new_link(consumer);
778 /* Determine the initial link state. */
779 if (flags & DL_FLAG_STATELESS)
780 link->status = DL_STATE_NONE;
782 device_link_init_status(link, consumer, supplier);
785 * Some callers expect the link creation during consumer driver probe to
786 * resume the supplier even without DL_FLAG_RPM_ACTIVE.
788 if (link->status == DL_STATE_CONSUMER_PROBE &&
789 flags & DL_FLAG_PM_RUNTIME)
790 pm_runtime_resume(supplier);
792 list_add_tail_rcu(&link->s_node, &supplier->links.consumers);
793 list_add_tail_rcu(&link->c_node, &consumer->links.suppliers);
795 if (flags & DL_FLAG_SYNC_STATE_ONLY) {
797 "Linked as a sync state only consumer to %s\n",
804 * Move the consumer and all of the devices depending on it to the end
805 * of dpm_list and the devices_kset list.
807 * It is necessary to hold dpm_list locked throughout all that or else
808 * we may end up suspending with a wrong ordering of it.
810 device_reorder_to_tail(consumer, NULL);
812 dev_dbg(consumer, "Linked as a consumer to %s\n", dev_name(supplier));
816 device_links_write_unlock();
818 if ((flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) && !link)
819 pm_runtime_put(supplier);
823 EXPORT_SYMBOL_GPL(device_link_add);
826 static void __device_link_del(struct kref *kref)
828 struct device_link *link = container_of(kref, struct device_link, kref);
830 dev_dbg(link->consumer, "Dropping the link to %s\n",
831 dev_name(link->supplier));
833 pm_runtime_drop_link(link);
835 list_del_rcu(&link->s_node);
836 list_del_rcu(&link->c_node);
837 device_unregister(&link->link_dev);
839 #else /* !CONFIG_SRCU */
840 static void __device_link_del(struct kref *kref)
842 struct device_link *link = container_of(kref, struct device_link, kref);
844 dev_info(link->consumer, "Dropping the link to %s\n",
845 dev_name(link->supplier));
847 pm_runtime_drop_link(link);
849 list_del(&link->s_node);
850 list_del(&link->c_node);
851 device_unregister(&link->link_dev);
853 #endif /* !CONFIG_SRCU */
855 static void device_link_put_kref(struct device_link *link)
857 if (link->flags & DL_FLAG_STATELESS)
858 kref_put(&link->kref, __device_link_del);
860 WARN(1, "Unable to drop a managed device link reference\n");
864 * device_link_del - Delete a stateless link between two devices.
865 * @link: Device link to delete.
867 * The caller must ensure proper synchronization of this function with runtime
868 * PM. If the link was added multiple times, it needs to be deleted as often.
869 * Care is required for hotplugged devices: Their links are purged on removal
870 * and calling device_link_del() is then no longer allowed.
872 void device_link_del(struct device_link *link)
874 device_links_write_lock();
875 device_link_put_kref(link);
876 device_links_write_unlock();
878 EXPORT_SYMBOL_GPL(device_link_del);
881 * device_link_remove - Delete a stateless link between two devices.
882 * @consumer: Consumer end of the link.
883 * @supplier: Supplier end of the link.
885 * The caller must ensure proper synchronization of this function with runtime
888 void device_link_remove(void *consumer, struct device *supplier)
890 struct device_link *link;
892 if (WARN_ON(consumer == supplier))
895 device_links_write_lock();
897 list_for_each_entry(link, &supplier->links.consumers, s_node) {
898 if (link->consumer == consumer) {
899 device_link_put_kref(link);
904 device_links_write_unlock();
906 EXPORT_SYMBOL_GPL(device_link_remove);
908 static void device_links_missing_supplier(struct device *dev)
910 struct device_link *link;
912 list_for_each_entry(link, &dev->links.suppliers, c_node) {
913 if (link->status != DL_STATE_CONSUMER_PROBE)
916 if (link->supplier->links.status == DL_DEV_DRIVER_BOUND) {
917 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
919 WARN_ON(!(link->flags & DL_FLAG_SYNC_STATE_ONLY));
920 WRITE_ONCE(link->status, DL_STATE_DORMANT);
926 * device_links_check_suppliers - Check presence of supplier drivers.
927 * @dev: Consumer device.
929 * Check links from this device to any suppliers. Walk the list of the device's
930 * links to suppliers and see if all of them are available. If not, simply
931 * return -EPROBE_DEFER.
933 * We need to guarantee that the supplier will not go away after the check has
934 * been positive here. It only can go away in __device_release_driver() and
935 * that function checks the device's links to consumers. This means we need to
936 * mark the link as "consumer probe in progress" to make the supplier removal
937 * wait for us to complete (or bad things may happen).
939 * Links without the DL_FLAG_MANAGED flag set are ignored.
941 int device_links_check_suppliers(struct device *dev)
943 struct device_link *link;
947 * Device waiting for supplier to become available is not allowed to
950 mutex_lock(&fwnode_link_lock);
951 if (dev->fwnode && !list_empty(&dev->fwnode->suppliers) &&
952 !fw_devlink_is_permissive()) {
953 mutex_unlock(&fwnode_link_lock);
954 return -EPROBE_DEFER;
956 mutex_unlock(&fwnode_link_lock);
958 device_links_write_lock();
960 list_for_each_entry(link, &dev->links.suppliers, c_node) {
961 if (!(link->flags & DL_FLAG_MANAGED))
964 if (link->status != DL_STATE_AVAILABLE &&
965 !(link->flags & DL_FLAG_SYNC_STATE_ONLY)) {
966 device_links_missing_supplier(dev);
970 WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE);
972 dev->links.status = DL_DEV_PROBING;
974 device_links_write_unlock();
979 * __device_links_queue_sync_state - Queue a device for sync_state() callback
980 * @dev: Device to call sync_state() on
981 * @list: List head to queue the @dev on
983 * Queues a device for a sync_state() callback when the device links write lock
984 * isn't held. This allows the sync_state() execution flow to use device links
985 * APIs. The caller must ensure this function is called with
986 * device_links_write_lock() held.
988 * This function does a get_device() to make sure the device is not freed while
991 * So the caller must also ensure that device_links_flush_sync_list() is called
992 * as soon as the caller releases device_links_write_lock(). This is necessary
993 * to make sure the sync_state() is called in a timely fashion and the
994 * put_device() is called on this device.
996 static void __device_links_queue_sync_state(struct device *dev,
997 struct list_head *list)
999 struct device_link *link;
1001 if (!dev_has_sync_state(dev))
1003 if (dev->state_synced)
1006 list_for_each_entry(link, &dev->links.consumers, s_node) {
1007 if (!(link->flags & DL_FLAG_MANAGED))
1009 if (link->status != DL_STATE_ACTIVE)
1014 * Set the flag here to avoid adding the same device to a list more
1015 * than once. This can happen if new consumers get added to the device
1016 * and probed before the list is flushed.
1018 dev->state_synced = true;
1020 if (WARN_ON(!list_empty(&dev->links.defer_sync)))
1024 list_add_tail(&dev->links.defer_sync, list);
1028 * device_links_flush_sync_list - Call sync_state() on a list of devices
1029 * @list: List of devices to call sync_state() on
1030 * @dont_lock_dev: Device for which lock is already held by the caller
1032 * Calls sync_state() on all the devices that have been queued for it. This
1033 * function is used in conjunction with __device_links_queue_sync_state(). The
1034 * @dont_lock_dev parameter is useful when this function is called from a
1035 * context where a device lock is already held.
1037 static void device_links_flush_sync_list(struct list_head *list,
1038 struct device *dont_lock_dev)
1040 struct device *dev, *tmp;
1042 list_for_each_entry_safe(dev, tmp, list, links.defer_sync) {
1043 list_del_init(&dev->links.defer_sync);
1045 if (dev != dont_lock_dev)
1048 if (dev->bus->sync_state)
1049 dev->bus->sync_state(dev);
1050 else if (dev->driver && dev->driver->sync_state)
1051 dev->driver->sync_state(dev);
1053 if (dev != dont_lock_dev)
1060 void device_links_supplier_sync_state_pause(void)
1062 device_links_write_lock();
1063 defer_sync_state_count++;
1064 device_links_write_unlock();
1067 void device_links_supplier_sync_state_resume(void)
1069 struct device *dev, *tmp;
1070 LIST_HEAD(sync_list);
1072 device_links_write_lock();
1073 if (!defer_sync_state_count) {
1074 WARN(true, "Unmatched sync_state pause/resume!");
1077 defer_sync_state_count--;
1078 if (defer_sync_state_count)
1081 list_for_each_entry_safe(dev, tmp, &deferred_sync, links.defer_sync) {
1083 * Delete from deferred_sync list before queuing it to
1084 * sync_list because defer_sync is used for both lists.
1086 list_del_init(&dev->links.defer_sync);
1087 __device_links_queue_sync_state(dev, &sync_list);
1090 device_links_write_unlock();
1092 device_links_flush_sync_list(&sync_list, NULL);
1095 static int sync_state_resume_initcall(void)
1097 device_links_supplier_sync_state_resume();
1100 late_initcall(sync_state_resume_initcall);
1102 static void __device_links_supplier_defer_sync(struct device *sup)
1104 if (list_empty(&sup->links.defer_sync) && dev_has_sync_state(sup))
1105 list_add_tail(&sup->links.defer_sync, &deferred_sync);
1108 static void device_link_drop_managed(struct device_link *link)
1110 link->flags &= ~DL_FLAG_MANAGED;
1111 WRITE_ONCE(link->status, DL_STATE_NONE);
1112 kref_put(&link->kref, __device_link_del);
1115 static ssize_t waiting_for_supplier_show(struct device *dev,
1116 struct device_attribute *attr,
1122 val = !list_empty(&dev->fwnode->suppliers);
1124 return sysfs_emit(buf, "%u\n", val);
1126 static DEVICE_ATTR_RO(waiting_for_supplier);
1129 * device_links_driver_bound - Update device links after probing its driver.
1130 * @dev: Device to update the links for.
1132 * The probe has been successful, so update links from this device to any
1133 * consumers by changing their status to "available".
1135 * Also change the status of @dev's links to suppliers to "active".
1137 * Links without the DL_FLAG_MANAGED flag set are ignored.
1139 void device_links_driver_bound(struct device *dev)
1141 struct device_link *link, *ln;
1142 LIST_HEAD(sync_list);
1145 * If a device probes successfully, it's expected to have created all
1146 * the device links it needs to or make new device links as it needs
1147 * them. So, it no longer needs to wait on any suppliers.
1149 if (dev->fwnode && dev->fwnode->dev == dev)
1150 fwnode_links_purge_suppliers(dev->fwnode);
1151 device_remove_file(dev, &dev_attr_waiting_for_supplier);
1153 device_links_write_lock();
1155 list_for_each_entry(link, &dev->links.consumers, s_node) {
1156 if (!(link->flags & DL_FLAG_MANAGED))
1160 * Links created during consumer probe may be in the "consumer
1161 * probe" state to start with if the supplier is still probing
1162 * when they are created and they may become "active" if the
1163 * consumer probe returns first. Skip them here.
1165 if (link->status == DL_STATE_CONSUMER_PROBE ||
1166 link->status == DL_STATE_ACTIVE)
1169 WARN_ON(link->status != DL_STATE_DORMANT);
1170 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
1172 if (link->flags & DL_FLAG_AUTOPROBE_CONSUMER)
1173 driver_deferred_probe_add(link->consumer);
1176 if (defer_sync_state_count)
1177 __device_links_supplier_defer_sync(dev);
1179 __device_links_queue_sync_state(dev, &sync_list);
1181 list_for_each_entry_safe(link, ln, &dev->links.suppliers, c_node) {
1182 struct device *supplier;
1184 if (!(link->flags & DL_FLAG_MANAGED))
1187 supplier = link->supplier;
1188 if (link->flags & DL_FLAG_SYNC_STATE_ONLY) {
1190 * When DL_FLAG_SYNC_STATE_ONLY is set, it means no
1191 * other DL_MANAGED_LINK_FLAGS have been set. So, it's
1192 * save to drop the managed link completely.
1194 device_link_drop_managed(link);
1196 WARN_ON(link->status != DL_STATE_CONSUMER_PROBE);
1197 WRITE_ONCE(link->status, DL_STATE_ACTIVE);
1201 * This needs to be done even for the deleted
1202 * DL_FLAG_SYNC_STATE_ONLY device link in case it was the last
1203 * device link that was preventing the supplier from getting a
1204 * sync_state() call.
1206 if (defer_sync_state_count)
1207 __device_links_supplier_defer_sync(supplier);
1209 __device_links_queue_sync_state(supplier, &sync_list);
1212 dev->links.status = DL_DEV_DRIVER_BOUND;
1214 device_links_write_unlock();
1216 device_links_flush_sync_list(&sync_list, dev);
1220 * __device_links_no_driver - Update links of a device without a driver.
1221 * @dev: Device without a drvier.
1223 * Delete all non-persistent links from this device to any suppliers.
1225 * Persistent links stay around, but their status is changed to "available",
1226 * unless they already are in the "supplier unbind in progress" state in which
1227 * case they need not be updated.
1229 * Links without the DL_FLAG_MANAGED flag set are ignored.
1231 static void __device_links_no_driver(struct device *dev)
1233 struct device_link *link, *ln;
1235 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
1236 if (!(link->flags & DL_FLAG_MANAGED))
1239 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER) {
1240 device_link_drop_managed(link);
1244 if (link->status != DL_STATE_CONSUMER_PROBE &&
1245 link->status != DL_STATE_ACTIVE)
1248 if (link->supplier->links.status == DL_DEV_DRIVER_BOUND) {
1249 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
1251 WARN_ON(!(link->flags & DL_FLAG_SYNC_STATE_ONLY));
1252 WRITE_ONCE(link->status, DL_STATE_DORMANT);
1256 dev->links.status = DL_DEV_NO_DRIVER;
1260 * device_links_no_driver - Update links after failing driver probe.
1261 * @dev: Device whose driver has just failed to probe.
1263 * Clean up leftover links to consumers for @dev and invoke
1264 * %__device_links_no_driver() to update links to suppliers for it as
1267 * Links without the DL_FLAG_MANAGED flag set are ignored.
1269 void device_links_no_driver(struct device *dev)
1271 struct device_link *link;
1273 device_links_write_lock();
1275 list_for_each_entry(link, &dev->links.consumers, s_node) {
1276 if (!(link->flags & DL_FLAG_MANAGED))
1280 * The probe has failed, so if the status of the link is
1281 * "consumer probe" or "active", it must have been added by
1282 * a probing consumer while this device was still probing.
1283 * Change its state to "dormant", as it represents a valid
1284 * relationship, but it is not functionally meaningful.
1286 if (link->status == DL_STATE_CONSUMER_PROBE ||
1287 link->status == DL_STATE_ACTIVE)
1288 WRITE_ONCE(link->status, DL_STATE_DORMANT);
1291 __device_links_no_driver(dev);
1293 device_links_write_unlock();
1297 * device_links_driver_cleanup - Update links after driver removal.
1298 * @dev: Device whose driver has just gone away.
1300 * Update links to consumers for @dev by changing their status to "dormant" and
1301 * invoke %__device_links_no_driver() to update links to suppliers for it as
1304 * Links without the DL_FLAG_MANAGED flag set are ignored.
1306 void device_links_driver_cleanup(struct device *dev)
1308 struct device_link *link, *ln;
1310 device_links_write_lock();
1312 list_for_each_entry_safe(link, ln, &dev->links.consumers, s_node) {
1313 if (!(link->flags & DL_FLAG_MANAGED))
1316 WARN_ON(link->flags & DL_FLAG_AUTOREMOVE_CONSUMER);
1317 WARN_ON(link->status != DL_STATE_SUPPLIER_UNBIND);
1320 * autoremove the links between this @dev and its consumer
1321 * devices that are not active, i.e. where the link state
1322 * has moved to DL_STATE_SUPPLIER_UNBIND.
1324 if (link->status == DL_STATE_SUPPLIER_UNBIND &&
1325 link->flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
1326 device_link_drop_managed(link);
1328 WRITE_ONCE(link->status, DL_STATE_DORMANT);
1331 list_del_init(&dev->links.defer_sync);
1332 __device_links_no_driver(dev);
1334 device_links_write_unlock();
1338 * device_links_busy - Check if there are any busy links to consumers.
1339 * @dev: Device to check.
1341 * Check each consumer of the device and return 'true' if its link's status
1342 * is one of "consumer probe" or "active" (meaning that the given consumer is
1343 * probing right now or its driver is present). Otherwise, change the link
1344 * state to "supplier unbind" to prevent the consumer from being probed
1345 * successfully going forward.
1347 * Return 'false' if there are no probing or active consumers.
1349 * Links without the DL_FLAG_MANAGED flag set are ignored.
1351 bool device_links_busy(struct device *dev)
1353 struct device_link *link;
1356 device_links_write_lock();
1358 list_for_each_entry(link, &dev->links.consumers, s_node) {
1359 if (!(link->flags & DL_FLAG_MANAGED))
1362 if (link->status == DL_STATE_CONSUMER_PROBE
1363 || link->status == DL_STATE_ACTIVE) {
1367 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
1370 dev->links.status = DL_DEV_UNBINDING;
1372 device_links_write_unlock();
1377 * device_links_unbind_consumers - Force unbind consumers of the given device.
1378 * @dev: Device to unbind the consumers of.
1380 * Walk the list of links to consumers for @dev and if any of them is in the
1381 * "consumer probe" state, wait for all device probes in progress to complete
1384 * If that's not the case, change the status of the link to "supplier unbind"
1385 * and check if the link was in the "active" state. If so, force the consumer
1386 * driver to unbind and start over (the consumer will not re-probe as we have
1387 * changed the state of the link already).
1389 * Links without the DL_FLAG_MANAGED flag set are ignored.
1391 void device_links_unbind_consumers(struct device *dev)
1393 struct device_link *link;
1396 device_links_write_lock();
1398 list_for_each_entry(link, &dev->links.consumers, s_node) {
1399 enum device_link_state status;
1401 if (!(link->flags & DL_FLAG_MANAGED) ||
1402 link->flags & DL_FLAG_SYNC_STATE_ONLY)
1405 status = link->status;
1406 if (status == DL_STATE_CONSUMER_PROBE) {
1407 device_links_write_unlock();
1409 wait_for_device_probe();
1412 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
1413 if (status == DL_STATE_ACTIVE) {
1414 struct device *consumer = link->consumer;
1416 get_device(consumer);
1418 device_links_write_unlock();
1420 device_release_driver_internal(consumer, NULL,
1422 put_device(consumer);
1427 device_links_write_unlock();
1431 * device_links_purge - Delete existing links to other devices.
1432 * @dev: Target device.
1434 static void device_links_purge(struct device *dev)
1436 struct device_link *link, *ln;
1438 if (dev->class == &devlink_class)
1442 * Delete all of the remaining links from this device to any other
1443 * devices (either consumers or suppliers).
1445 device_links_write_lock();
1447 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
1448 WARN_ON(link->status == DL_STATE_ACTIVE);
1449 __device_link_del(&link->kref);
1452 list_for_each_entry_safe_reverse(link, ln, &dev->links.consumers, s_node) {
1453 WARN_ON(link->status != DL_STATE_DORMANT &&
1454 link->status != DL_STATE_NONE);
1455 __device_link_del(&link->kref);
1458 device_links_write_unlock();
1461 static u32 fw_devlink_flags = DL_FLAG_SYNC_STATE_ONLY;
1462 static int __init fw_devlink_setup(char *arg)
1467 if (strcmp(arg, "off") == 0) {
1468 fw_devlink_flags = 0;
1469 } else if (strcmp(arg, "permissive") == 0) {
1470 fw_devlink_flags = DL_FLAG_SYNC_STATE_ONLY;
1471 } else if (strcmp(arg, "on") == 0) {
1472 fw_devlink_flags = DL_FLAG_AUTOPROBE_CONSUMER;
1473 } else if (strcmp(arg, "rpm") == 0) {
1474 fw_devlink_flags = DL_FLAG_AUTOPROBE_CONSUMER |
1479 early_param("fw_devlink", fw_devlink_setup);
1481 u32 fw_devlink_get_flags(void)
1483 return fw_devlink_flags;
1486 static bool fw_devlink_is_permissive(void)
1488 return fw_devlink_flags == DL_FLAG_SYNC_STATE_ONLY;
1491 static void fw_devlink_parse_fwnode(struct fwnode_handle *fwnode)
1493 if (fwnode->flags & FWNODE_FLAG_LINKS_ADDED)
1496 fwnode_call_int_op(fwnode, add_links);
1497 fwnode->flags |= FWNODE_FLAG_LINKS_ADDED;
1500 static void fw_devlink_parse_fwtree(struct fwnode_handle *fwnode)
1502 struct fwnode_handle *child = NULL;
1504 fw_devlink_parse_fwnode(fwnode);
1506 while ((child = fwnode_get_next_available_child_node(fwnode, child)))
1507 fw_devlink_parse_fwtree(child);
1511 * fw_devlink_create_devlink - Create a device link from a consumer to fwnode
1512 * @con - Consumer device for the device link
1513 * @sup_handle - fwnode handle of supplier
1515 * This function will try to create a device link between the consumer device
1516 * @con and the supplier device represented by @sup_handle.
1518 * The supplier has to be provided as a fwnode because incorrect cycles in
1519 * fwnode links can sometimes cause the supplier device to never be created.
1520 * This function detects such cases and returns an error if it cannot create a
1521 * device link from the consumer to a missing supplier.
1524 * 0 on successfully creating a device link
1525 * -EINVAL if the device link cannot be created as expected
1526 * -EAGAIN if the device link cannot be created right now, but it may be
1527 * possible to do that in the future
1529 static int fw_devlink_create_devlink(struct device *con,
1530 struct fwnode_handle *sup_handle, u32 flags)
1532 struct device *sup_dev;
1535 sup_dev = get_dev_from_fwnode(sup_handle);
1538 * If this fails, it is due to cycles in device links. Just
1539 * give up on this link and treat it as invalid.
1541 if (!device_link_add(con, sup_dev, flags))
1548 * DL_FLAG_SYNC_STATE_ONLY doesn't block probing and supports
1549 * cycles. So cycle detection isn't necessary and shouldn't be
1552 if (flags & DL_FLAG_SYNC_STATE_ONLY)
1556 * If we can't find the supplier device from its fwnode, it might be
1557 * due to a cyclic dependency between fwnodes. Some of these cycles can
1558 * be broken by applying logic. Check for these types of cycles and
1559 * break them so that devices in the cycle probe properly.
1561 * If the supplier's parent is dependent on the consumer, then
1562 * the consumer-supplier dependency is a false dependency. So,
1563 * treat it as an invalid link.
1565 sup_dev = fwnode_get_next_parent_dev(sup_handle);
1566 if (sup_dev && device_is_dependent(con, sup_dev)) {
1567 dev_dbg(con, "Not linking to %pfwP - False link\n",
1572 * Can't check for cycles or no cycles. So let's try
1579 put_device(sup_dev);
1584 * __fw_devlink_link_to_consumers - Create device links to consumers of a device
1585 * @dev - Device that needs to be linked to its consumers
1587 * This function looks at all the consumer fwnodes of @dev and creates device
1588 * links between the consumer device and @dev (supplier).
1590 * If the consumer device has not been added yet, then this function creates a
1591 * SYNC_STATE_ONLY link between @dev (supplier) and the closest ancestor device
1592 * of the consumer fwnode. This is necessary to make sure @dev doesn't get a
1593 * sync_state() callback before the real consumer device gets to be added and
1596 * Once device links are created from the real consumer to @dev (supplier), the
1597 * fwnode links are deleted.
1599 static void __fw_devlink_link_to_consumers(struct device *dev)
1601 struct fwnode_handle *fwnode = dev->fwnode;
1602 struct fwnode_link *link, *tmp;
1604 list_for_each_entry_safe(link, tmp, &fwnode->consumers, s_hook) {
1605 u32 dl_flags = fw_devlink_get_flags();
1606 struct device *con_dev;
1607 bool own_link = true;
1610 con_dev = get_dev_from_fwnode(link->consumer);
1612 * If consumer device is not available yet, make a "proxy"
1613 * SYNC_STATE_ONLY link from the consumer's parent device to
1614 * the supplier device. This is necessary to make sure the
1615 * supplier doesn't get a sync_state() callback before the real
1616 * consumer can create a device link to the supplier.
1618 * This proxy link step is needed to handle the case where the
1619 * consumer's parent device is added before the supplier.
1622 con_dev = fwnode_get_next_parent_dev(link->consumer);
1624 * However, if the consumer's parent device is also the
1625 * parent of the supplier, don't create a
1626 * consumer-supplier link from the parent to its child
1627 * device. Such a dependency is impossible.
1630 fwnode_is_ancestor_of(con_dev->fwnode, fwnode)) {
1631 put_device(con_dev);
1635 dl_flags = DL_FLAG_SYNC_STATE_ONLY;
1642 ret = fw_devlink_create_devlink(con_dev, fwnode, dl_flags);
1643 put_device(con_dev);
1644 if (!own_link || ret == -EAGAIN)
1647 list_del(&link->s_hook);
1648 list_del(&link->c_hook);
1654 * __fw_devlink_link_to_suppliers - Create device links to suppliers of a device
1655 * @dev - The consumer device that needs to be linked to its suppliers
1656 * @fwnode - Root of the fwnode tree that is used to create device links
1658 * This function looks at all the supplier fwnodes of fwnode tree rooted at
1659 * @fwnode and creates device links between @dev (consumer) and all the
1660 * supplier devices of the entire fwnode tree at @fwnode.
1662 * The function creates normal (non-SYNC_STATE_ONLY) device links between @dev
1663 * and the real suppliers of @dev. Once these device links are created, the
1664 * fwnode links are deleted. When such device links are successfully created,
1665 * this function is called recursively on those supplier devices. This is
1666 * needed to detect and break some invalid cycles in fwnode links. See
1667 * fw_devlink_create_devlink() for more details.
1669 * In addition, it also looks at all the suppliers of the entire fwnode tree
1670 * because some of the child devices of @dev that have not been added yet
1671 * (because @dev hasn't probed) might already have their suppliers added to
1672 * driver core. So, this function creates SYNC_STATE_ONLY device links between
1673 * @dev (consumer) and these suppliers to make sure they don't execute their
1674 * sync_state() callbacks before these child devices have a chance to create
1675 * their device links. The fwnode links that correspond to the child devices
1676 * aren't delete because they are needed later to create the device links
1677 * between the real consumer and supplier devices.
1679 static void __fw_devlink_link_to_suppliers(struct device *dev,
1680 struct fwnode_handle *fwnode)
1682 bool own_link = (dev->fwnode == fwnode);
1683 struct fwnode_link *link, *tmp;
1684 struct fwnode_handle *child = NULL;
1688 dl_flags = fw_devlink_get_flags();
1690 dl_flags = DL_FLAG_SYNC_STATE_ONLY;
1692 list_for_each_entry_safe(link, tmp, &fwnode->suppliers, c_hook) {
1694 struct device *sup_dev;
1695 struct fwnode_handle *sup = link->supplier;
1697 ret = fw_devlink_create_devlink(dev, sup, dl_flags);
1698 if (!own_link || ret == -EAGAIN)
1701 list_del(&link->s_hook);
1702 list_del(&link->c_hook);
1705 /* If no device link was created, nothing more to do. */
1710 * If a device link was successfully created to a supplier, we
1711 * now need to try and link the supplier to all its suppliers.
1713 * This is needed to detect and delete false dependencies in
1714 * fwnode links that haven't been converted to a device link
1715 * yet. See comments in fw_devlink_create_devlink() for more
1716 * details on the false dependency.
1718 * Without deleting these false dependencies, some devices will
1719 * never probe because they'll keep waiting for their false
1720 * dependency fwnode links to be converted to device links.
1722 sup_dev = get_dev_from_fwnode(sup);
1723 __fw_devlink_link_to_suppliers(sup_dev, sup_dev->fwnode);
1724 put_device(sup_dev);
1728 * Make "proxy" SYNC_STATE_ONLY device links to represent the needs of
1729 * all the descendants. This proxy link step is needed to handle the
1730 * case where the supplier is added before the consumer's parent device
1733 while ((child = fwnode_get_next_available_child_node(fwnode, child)))
1734 __fw_devlink_link_to_suppliers(dev, child);
1737 static void fw_devlink_link_device(struct device *dev)
1739 struct fwnode_handle *fwnode = dev->fwnode;
1741 if (!fw_devlink_flags)
1744 fw_devlink_parse_fwtree(fwnode);
1746 mutex_lock(&fwnode_link_lock);
1747 __fw_devlink_link_to_consumers(dev);
1748 __fw_devlink_link_to_suppliers(dev, fwnode);
1749 mutex_unlock(&fwnode_link_lock);
1752 /* Device links support end. */
1754 int (*platform_notify)(struct device *dev) = NULL;
1755 int (*platform_notify_remove)(struct device *dev) = NULL;
1756 static struct kobject *dev_kobj;
1757 struct kobject *sysfs_dev_char_kobj;
1758 struct kobject *sysfs_dev_block_kobj;
1760 static DEFINE_MUTEX(device_hotplug_lock);
1762 void lock_device_hotplug(void)
1764 mutex_lock(&device_hotplug_lock);
1767 void unlock_device_hotplug(void)
1769 mutex_unlock(&device_hotplug_lock);
1772 int lock_device_hotplug_sysfs(void)
1774 if (mutex_trylock(&device_hotplug_lock))
1777 /* Avoid busy looping (5 ms of sleep should do). */
1779 return restart_syscall();
1783 static inline int device_is_not_partition(struct device *dev)
1785 return !(dev->type == &part_type);
1788 static inline int device_is_not_partition(struct device *dev)
1795 device_platform_notify(struct device *dev, enum kobject_action action)
1799 ret = acpi_platform_notify(dev, action);
1803 ret = software_node_notify(dev, action);
1807 if (platform_notify && action == KOBJ_ADD)
1808 platform_notify(dev);
1809 else if (platform_notify_remove && action == KOBJ_REMOVE)
1810 platform_notify_remove(dev);
1815 * dev_driver_string - Return a device's driver name, if at all possible
1816 * @dev: struct device to get the name of
1818 * Will return the device's driver's name if it is bound to a device. If
1819 * the device is not bound to a driver, it will return the name of the bus
1820 * it is attached to. If it is not attached to a bus either, an empty
1821 * string will be returned.
1823 const char *dev_driver_string(const struct device *dev)
1825 struct device_driver *drv;
1827 /* dev->driver can change to NULL underneath us because of unbinding,
1828 * so be careful about accessing it. dev->bus and dev->class should
1829 * never change once they are set, so they don't need special care.
1831 drv = READ_ONCE(dev->driver);
1832 return drv ? drv->name : dev_bus_name(dev);
1834 EXPORT_SYMBOL(dev_driver_string);
1836 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
1838 static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
1841 struct device_attribute *dev_attr = to_dev_attr(attr);
1842 struct device *dev = kobj_to_dev(kobj);
1846 ret = dev_attr->show(dev, dev_attr, buf);
1847 if (ret >= (ssize_t)PAGE_SIZE) {
1848 printk("dev_attr_show: %pS returned bad count\n",
1854 static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
1855 const char *buf, size_t count)
1857 struct device_attribute *dev_attr = to_dev_attr(attr);
1858 struct device *dev = kobj_to_dev(kobj);
1861 if (dev_attr->store)
1862 ret = dev_attr->store(dev, dev_attr, buf, count);
1866 static const struct sysfs_ops dev_sysfs_ops = {
1867 .show = dev_attr_show,
1868 .store = dev_attr_store,
1871 #define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
1873 ssize_t device_store_ulong(struct device *dev,
1874 struct device_attribute *attr,
1875 const char *buf, size_t size)
1877 struct dev_ext_attribute *ea = to_ext_attr(attr);
1881 ret = kstrtoul(buf, 0, &new);
1884 *(unsigned long *)(ea->var) = new;
1885 /* Always return full write size even if we didn't consume all */
1888 EXPORT_SYMBOL_GPL(device_store_ulong);
1890 ssize_t device_show_ulong(struct device *dev,
1891 struct device_attribute *attr,
1894 struct dev_ext_attribute *ea = to_ext_attr(attr);
1895 return sysfs_emit(buf, "%lx\n", *(unsigned long *)(ea->var));
1897 EXPORT_SYMBOL_GPL(device_show_ulong);
1899 ssize_t device_store_int(struct device *dev,
1900 struct device_attribute *attr,
1901 const char *buf, size_t size)
1903 struct dev_ext_attribute *ea = to_ext_attr(attr);
1907 ret = kstrtol(buf, 0, &new);
1911 if (new > INT_MAX || new < INT_MIN)
1913 *(int *)(ea->var) = new;
1914 /* Always return full write size even if we didn't consume all */
1917 EXPORT_SYMBOL_GPL(device_store_int);
1919 ssize_t device_show_int(struct device *dev,
1920 struct device_attribute *attr,
1923 struct dev_ext_attribute *ea = to_ext_attr(attr);
1925 return sysfs_emit(buf, "%d\n", *(int *)(ea->var));
1927 EXPORT_SYMBOL_GPL(device_show_int);
1929 ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
1930 const char *buf, size_t size)
1932 struct dev_ext_attribute *ea = to_ext_attr(attr);
1934 if (strtobool(buf, ea->var) < 0)
1939 EXPORT_SYMBOL_GPL(device_store_bool);
1941 ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
1944 struct dev_ext_attribute *ea = to_ext_attr(attr);
1946 return sysfs_emit(buf, "%d\n", *(bool *)(ea->var));
1948 EXPORT_SYMBOL_GPL(device_show_bool);
1951 * device_release - free device structure.
1952 * @kobj: device's kobject.
1954 * This is called once the reference count for the object
1955 * reaches 0. We forward the call to the device's release
1956 * method, which should handle actually freeing the structure.
1958 static void device_release(struct kobject *kobj)
1960 struct device *dev = kobj_to_dev(kobj);
1961 struct device_private *p = dev->p;
1964 * Some platform devices are driven without driver attached
1965 * and managed resources may have been acquired. Make sure
1966 * all resources are released.
1968 * Drivers still can add resources into device after device
1969 * is deleted but alive, so release devres here to avoid
1970 * possible memory leak.
1972 devres_release_all(dev);
1974 kfree(dev->dma_range_map);
1978 else if (dev->type && dev->type->release)
1979 dev->type->release(dev);
1980 else if (dev->class && dev->class->dev_release)
1981 dev->class->dev_release(dev);
1983 WARN(1, KERN_ERR "Device '%s' does not have a release() function, it is broken and must be fixed. See Documentation/core-api/kobject.rst.\n",
1988 static const void *device_namespace(struct kobject *kobj)
1990 struct device *dev = kobj_to_dev(kobj);
1991 const void *ns = NULL;
1993 if (dev->class && dev->class->ns_type)
1994 ns = dev->class->namespace(dev);
1999 static void device_get_ownership(struct kobject *kobj, kuid_t *uid, kgid_t *gid)
2001 struct device *dev = kobj_to_dev(kobj);
2003 if (dev->class && dev->class->get_ownership)
2004 dev->class->get_ownership(dev, uid, gid);
2007 static struct kobj_type device_ktype = {
2008 .release = device_release,
2009 .sysfs_ops = &dev_sysfs_ops,
2010 .namespace = device_namespace,
2011 .get_ownership = device_get_ownership,
2015 static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
2017 struct kobj_type *ktype = get_ktype(kobj);
2019 if (ktype == &device_ktype) {
2020 struct device *dev = kobj_to_dev(kobj);
2029 static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
2031 struct device *dev = kobj_to_dev(kobj);
2034 return dev->bus->name;
2036 return dev->class->name;
2040 static int dev_uevent(struct kset *kset, struct kobject *kobj,
2041 struct kobj_uevent_env *env)
2043 struct device *dev = kobj_to_dev(kobj);
2046 /* add device node properties if present */
2047 if (MAJOR(dev->devt)) {
2051 kuid_t uid = GLOBAL_ROOT_UID;
2052 kgid_t gid = GLOBAL_ROOT_GID;
2054 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
2055 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
2056 name = device_get_devnode(dev, &mode, &uid, &gid, &tmp);
2058 add_uevent_var(env, "DEVNAME=%s", name);
2060 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
2061 if (!uid_eq(uid, GLOBAL_ROOT_UID))
2062 add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid));
2063 if (!gid_eq(gid, GLOBAL_ROOT_GID))
2064 add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid));
2069 if (dev->type && dev->type->name)
2070 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
2073 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
2075 /* Add common DT information about the device */
2076 of_device_uevent(dev, env);
2078 /* have the bus specific function add its stuff */
2079 if (dev->bus && dev->bus->uevent) {
2080 retval = dev->bus->uevent(dev, env);
2082 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
2083 dev_name(dev), __func__, retval);
2086 /* have the class specific function add its stuff */
2087 if (dev->class && dev->class->dev_uevent) {
2088 retval = dev->class->dev_uevent(dev, env);
2090 pr_debug("device: '%s': %s: class uevent() "
2091 "returned %d\n", dev_name(dev),
2095 /* have the device type specific function add its stuff */
2096 if (dev->type && dev->type->uevent) {
2097 retval = dev->type->uevent(dev, env);
2099 pr_debug("device: '%s': %s: dev_type uevent() "
2100 "returned %d\n", dev_name(dev),
2107 static const struct kset_uevent_ops device_uevent_ops = {
2108 .filter = dev_uevent_filter,
2109 .name = dev_uevent_name,
2110 .uevent = dev_uevent,
2113 static ssize_t uevent_show(struct device *dev, struct device_attribute *attr,
2116 struct kobject *top_kobj;
2118 struct kobj_uevent_env *env = NULL;
2123 /* search the kset, the device belongs to */
2124 top_kobj = &dev->kobj;
2125 while (!top_kobj->kset && top_kobj->parent)
2126 top_kobj = top_kobj->parent;
2127 if (!top_kobj->kset)
2130 kset = top_kobj->kset;
2131 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
2134 /* respect filter */
2135 if (kset->uevent_ops && kset->uevent_ops->filter)
2136 if (!kset->uevent_ops->filter(kset, &dev->kobj))
2139 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
2143 /* let the kset specific function add its keys */
2144 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
2148 /* copy keys to file */
2149 for (i = 0; i < env->envp_idx; i++)
2150 len += sysfs_emit_at(buf, len, "%s\n", env->envp[i]);
2156 static ssize_t uevent_store(struct device *dev, struct device_attribute *attr,
2157 const char *buf, size_t count)
2161 rc = kobject_synth_uevent(&dev->kobj, buf, count);
2164 dev_err(dev, "uevent: failed to send synthetic uevent\n");
2170 static DEVICE_ATTR_RW(uevent);
2172 static ssize_t online_show(struct device *dev, struct device_attribute *attr,
2178 val = !dev->offline;
2180 return sysfs_emit(buf, "%u\n", val);
2183 static ssize_t online_store(struct device *dev, struct device_attribute *attr,
2184 const char *buf, size_t count)
2189 ret = strtobool(buf, &val);
2193 ret = lock_device_hotplug_sysfs();
2197 ret = val ? device_online(dev) : device_offline(dev);
2198 unlock_device_hotplug();
2199 return ret < 0 ? ret : count;
2201 static DEVICE_ATTR_RW(online);
2203 int device_add_groups(struct device *dev, const struct attribute_group **groups)
2205 return sysfs_create_groups(&dev->kobj, groups);
2207 EXPORT_SYMBOL_GPL(device_add_groups);
2209 void device_remove_groups(struct device *dev,
2210 const struct attribute_group **groups)
2212 sysfs_remove_groups(&dev->kobj, groups);
2214 EXPORT_SYMBOL_GPL(device_remove_groups);
2216 union device_attr_group_devres {
2217 const struct attribute_group *group;
2218 const struct attribute_group **groups;
2221 static int devm_attr_group_match(struct device *dev, void *res, void *data)
2223 return ((union device_attr_group_devres *)res)->group == data;
2226 static void devm_attr_group_remove(struct device *dev, void *res)
2228 union device_attr_group_devres *devres = res;
2229 const struct attribute_group *group = devres->group;
2231 dev_dbg(dev, "%s: removing group %p\n", __func__, group);
2232 sysfs_remove_group(&dev->kobj, group);
2235 static void devm_attr_groups_remove(struct device *dev, void *res)
2237 union device_attr_group_devres *devres = res;
2238 const struct attribute_group **groups = devres->groups;
2240 dev_dbg(dev, "%s: removing groups %p\n", __func__, groups);
2241 sysfs_remove_groups(&dev->kobj, groups);
2245 * devm_device_add_group - given a device, create a managed attribute group
2246 * @dev: The device to create the group for
2247 * @grp: The attribute group to create
2249 * This function creates a group for the first time. It will explicitly
2250 * warn and error if any of the attribute files being created already exist.
2252 * Returns 0 on success or error code on failure.
2254 int devm_device_add_group(struct device *dev, const struct attribute_group *grp)
2256 union device_attr_group_devres *devres;
2259 devres = devres_alloc(devm_attr_group_remove,
2260 sizeof(*devres), GFP_KERNEL);
2264 error = sysfs_create_group(&dev->kobj, grp);
2266 devres_free(devres);
2270 devres->group = grp;
2271 devres_add(dev, devres);
2274 EXPORT_SYMBOL_GPL(devm_device_add_group);
2277 * devm_device_remove_group: remove a managed group from a device
2278 * @dev: device to remove the group from
2279 * @grp: group to remove
2281 * This function removes a group of attributes from a device. The attributes
2282 * previously have to have been created for this group, otherwise it will fail.
2284 void devm_device_remove_group(struct device *dev,
2285 const struct attribute_group *grp)
2287 WARN_ON(devres_release(dev, devm_attr_group_remove,
2288 devm_attr_group_match,
2289 /* cast away const */ (void *)grp));
2291 EXPORT_SYMBOL_GPL(devm_device_remove_group);
2294 * devm_device_add_groups - create a bunch of managed attribute groups
2295 * @dev: The device to create the group for
2296 * @groups: The attribute groups to create, NULL terminated
2298 * This function creates a bunch of managed attribute groups. If an error
2299 * occurs when creating a group, all previously created groups will be
2300 * removed, unwinding everything back to the original state when this
2301 * function was called. It will explicitly warn and error if any of the
2302 * attribute files being created already exist.
2304 * Returns 0 on success or error code from sysfs_create_group on failure.
2306 int devm_device_add_groups(struct device *dev,
2307 const struct attribute_group **groups)
2309 union device_attr_group_devres *devres;
2312 devres = devres_alloc(devm_attr_groups_remove,
2313 sizeof(*devres), GFP_KERNEL);
2317 error = sysfs_create_groups(&dev->kobj, groups);
2319 devres_free(devres);
2323 devres->groups = groups;
2324 devres_add(dev, devres);
2327 EXPORT_SYMBOL_GPL(devm_device_add_groups);
2330 * devm_device_remove_groups - remove a list of managed groups
2332 * @dev: The device for the groups to be removed from
2333 * @groups: NULL terminated list of groups to be removed
2335 * If groups is not NULL, remove the specified groups from the device.
2337 void devm_device_remove_groups(struct device *dev,
2338 const struct attribute_group **groups)
2340 WARN_ON(devres_release(dev, devm_attr_groups_remove,
2341 devm_attr_group_match,
2342 /* cast away const */ (void *)groups));
2344 EXPORT_SYMBOL_GPL(devm_device_remove_groups);
2346 static int device_add_attrs(struct device *dev)
2348 struct class *class = dev->class;
2349 const struct device_type *type = dev->type;
2353 error = device_add_groups(dev, class->dev_groups);
2359 error = device_add_groups(dev, type->groups);
2361 goto err_remove_class_groups;
2364 error = device_add_groups(dev, dev->groups);
2366 goto err_remove_type_groups;
2368 if (device_supports_offline(dev) && !dev->offline_disabled) {
2369 error = device_create_file(dev, &dev_attr_online);
2371 goto err_remove_dev_groups;
2374 if (fw_devlink_flags && !fw_devlink_is_permissive() && dev->fwnode) {
2375 error = device_create_file(dev, &dev_attr_waiting_for_supplier);
2377 goto err_remove_dev_online;
2382 err_remove_dev_online:
2383 device_remove_file(dev, &dev_attr_online);
2384 err_remove_dev_groups:
2385 device_remove_groups(dev, dev->groups);
2386 err_remove_type_groups:
2388 device_remove_groups(dev, type->groups);
2389 err_remove_class_groups:
2391 device_remove_groups(dev, class->dev_groups);
2396 static void device_remove_attrs(struct device *dev)
2398 struct class *class = dev->class;
2399 const struct device_type *type = dev->type;
2401 device_remove_file(dev, &dev_attr_waiting_for_supplier);
2402 device_remove_file(dev, &dev_attr_online);
2403 device_remove_groups(dev, dev->groups);
2406 device_remove_groups(dev, type->groups);
2409 device_remove_groups(dev, class->dev_groups);
2412 static ssize_t dev_show(struct device *dev, struct device_attribute *attr,
2415 return print_dev_t(buf, dev->devt);
2417 static DEVICE_ATTR_RO(dev);
2420 struct kset *devices_kset;
2423 * devices_kset_move_before - Move device in the devices_kset's list.
2424 * @deva: Device to move.
2425 * @devb: Device @deva should come before.
2427 static void devices_kset_move_before(struct device *deva, struct device *devb)
2431 pr_debug("devices_kset: Moving %s before %s\n",
2432 dev_name(deva), dev_name(devb));
2433 spin_lock(&devices_kset->list_lock);
2434 list_move_tail(&deva->kobj.entry, &devb->kobj.entry);
2435 spin_unlock(&devices_kset->list_lock);
2439 * devices_kset_move_after - Move device in the devices_kset's list.
2440 * @deva: Device to move
2441 * @devb: Device @deva should come after.
2443 static void devices_kset_move_after(struct device *deva, struct device *devb)
2447 pr_debug("devices_kset: Moving %s after %s\n",
2448 dev_name(deva), dev_name(devb));
2449 spin_lock(&devices_kset->list_lock);
2450 list_move(&deva->kobj.entry, &devb->kobj.entry);
2451 spin_unlock(&devices_kset->list_lock);
2455 * devices_kset_move_last - move the device to the end of devices_kset's list.
2456 * @dev: device to move
2458 void devices_kset_move_last(struct device *dev)
2462 pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev));
2463 spin_lock(&devices_kset->list_lock);
2464 list_move_tail(&dev->kobj.entry, &devices_kset->list);
2465 spin_unlock(&devices_kset->list_lock);
2469 * device_create_file - create sysfs attribute file for device.
2471 * @attr: device attribute descriptor.
2473 int device_create_file(struct device *dev,
2474 const struct device_attribute *attr)
2479 WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
2480 "Attribute %s: write permission without 'store'\n",
2482 WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
2483 "Attribute %s: read permission without 'show'\n",
2485 error = sysfs_create_file(&dev->kobj, &attr->attr);
2490 EXPORT_SYMBOL_GPL(device_create_file);
2493 * device_remove_file - remove sysfs attribute file.
2495 * @attr: device attribute descriptor.
2497 void device_remove_file(struct device *dev,
2498 const struct device_attribute *attr)
2501 sysfs_remove_file(&dev->kobj, &attr->attr);
2503 EXPORT_SYMBOL_GPL(device_remove_file);
2506 * device_remove_file_self - remove sysfs attribute file from its own method.
2508 * @attr: device attribute descriptor.
2510 * See kernfs_remove_self() for details.
2512 bool device_remove_file_self(struct device *dev,
2513 const struct device_attribute *attr)
2516 return sysfs_remove_file_self(&dev->kobj, &attr->attr);
2520 EXPORT_SYMBOL_GPL(device_remove_file_self);
2523 * device_create_bin_file - create sysfs binary attribute file for device.
2525 * @attr: device binary attribute descriptor.
2527 int device_create_bin_file(struct device *dev,
2528 const struct bin_attribute *attr)
2530 int error = -EINVAL;
2532 error = sysfs_create_bin_file(&dev->kobj, attr);
2535 EXPORT_SYMBOL_GPL(device_create_bin_file);
2538 * device_remove_bin_file - remove sysfs binary attribute file
2540 * @attr: device binary attribute descriptor.
2542 void device_remove_bin_file(struct device *dev,
2543 const struct bin_attribute *attr)
2546 sysfs_remove_bin_file(&dev->kobj, attr);
2548 EXPORT_SYMBOL_GPL(device_remove_bin_file);
2550 static void klist_children_get(struct klist_node *n)
2552 struct device_private *p = to_device_private_parent(n);
2553 struct device *dev = p->device;
2558 static void klist_children_put(struct klist_node *n)
2560 struct device_private *p = to_device_private_parent(n);
2561 struct device *dev = p->device;
2567 * device_initialize - init device structure.
2570 * This prepares the device for use by other layers by initializing
2572 * It is the first half of device_register(), if called by
2573 * that function, though it can also be called separately, so one
2574 * may use @dev's fields. In particular, get_device()/put_device()
2575 * may be used for reference counting of @dev after calling this
2578 * All fields in @dev must be initialized by the caller to 0, except
2579 * for those explicitly set to some other value. The simplest
2580 * approach is to use kzalloc() to allocate the structure containing
2583 * NOTE: Use put_device() to give up your reference instead of freeing
2584 * @dev directly once you have called this function.
2586 void device_initialize(struct device *dev)
2588 dev->kobj.kset = devices_kset;
2589 kobject_init(&dev->kobj, &device_ktype);
2590 INIT_LIST_HEAD(&dev->dma_pools);
2591 mutex_init(&dev->mutex);
2592 #ifdef CONFIG_PROVE_LOCKING
2593 mutex_init(&dev->lockdep_mutex);
2595 lockdep_set_novalidate_class(&dev->mutex);
2596 spin_lock_init(&dev->devres_lock);
2597 INIT_LIST_HEAD(&dev->devres_head);
2598 device_pm_init(dev);
2599 set_dev_node(dev, -1);
2600 #ifdef CONFIG_GENERIC_MSI_IRQ
2601 INIT_LIST_HEAD(&dev->msi_list);
2603 INIT_LIST_HEAD(&dev->links.consumers);
2604 INIT_LIST_HEAD(&dev->links.suppliers);
2605 INIT_LIST_HEAD(&dev->links.defer_sync);
2606 dev->links.status = DL_DEV_NO_DRIVER;
2607 #if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE) || \
2608 defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU) || \
2609 defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL)
2610 dev->dma_coherent = dma_default_coherent;
2613 EXPORT_SYMBOL_GPL(device_initialize);
2615 struct kobject *virtual_device_parent(struct device *dev)
2617 static struct kobject *virtual_dir = NULL;
2620 virtual_dir = kobject_create_and_add("virtual",
2621 &devices_kset->kobj);
2627 struct kobject kobj;
2628 struct class *class;
2631 #define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
2633 static void class_dir_release(struct kobject *kobj)
2635 struct class_dir *dir = to_class_dir(kobj);
2640 struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
2642 struct class_dir *dir = to_class_dir(kobj);
2643 return dir->class->ns_type;
2646 static struct kobj_type class_dir_ktype = {
2647 .release = class_dir_release,
2648 .sysfs_ops = &kobj_sysfs_ops,
2649 .child_ns_type = class_dir_child_ns_type
2652 static struct kobject *
2653 class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
2655 struct class_dir *dir;
2658 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
2660 return ERR_PTR(-ENOMEM);
2663 kobject_init(&dir->kobj, &class_dir_ktype);
2665 dir->kobj.kset = &class->p->glue_dirs;
2667 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
2669 kobject_put(&dir->kobj);
2670 return ERR_PTR(retval);
2675 static DEFINE_MUTEX(gdp_mutex);
2677 static struct kobject *get_device_parent(struct device *dev,
2678 struct device *parent)
2681 struct kobject *kobj = NULL;
2682 struct kobject *parent_kobj;
2686 /* block disks show up in /sys/block */
2687 if (sysfs_deprecated && dev->class == &block_class) {
2688 if (parent && parent->class == &block_class)
2689 return &parent->kobj;
2690 return &block_class.p->subsys.kobj;
2695 * If we have no parent, we live in "virtual".
2696 * Class-devices with a non class-device as parent, live
2697 * in a "glue" directory to prevent namespace collisions.
2700 parent_kobj = virtual_device_parent(dev);
2701 else if (parent->class && !dev->class->ns_type)
2702 return &parent->kobj;
2704 parent_kobj = &parent->kobj;
2706 mutex_lock(&gdp_mutex);
2708 /* find our class-directory at the parent and reference it */
2709 spin_lock(&dev->class->p->glue_dirs.list_lock);
2710 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
2711 if (k->parent == parent_kobj) {
2712 kobj = kobject_get(k);
2715 spin_unlock(&dev->class->p->glue_dirs.list_lock);
2717 mutex_unlock(&gdp_mutex);
2721 /* or create a new class-directory at the parent device */
2722 k = class_dir_create_and_add(dev->class, parent_kobj);
2723 /* do not emit an uevent for this simple "glue" directory */
2724 mutex_unlock(&gdp_mutex);
2728 /* subsystems can specify a default root directory for their devices */
2729 if (!parent && dev->bus && dev->bus->dev_root)
2730 return &dev->bus->dev_root->kobj;
2733 return &parent->kobj;
2737 static inline bool live_in_glue_dir(struct kobject *kobj,
2740 if (!kobj || !dev->class ||
2741 kobj->kset != &dev->class->p->glue_dirs)
2746 static inline struct kobject *get_glue_dir(struct device *dev)
2748 return dev->kobj.parent;
2752 * make sure cleaning up dir as the last step, we need to make
2753 * sure .release handler of kobject is run with holding the
2756 static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
2760 /* see if we live in a "glue" directory */
2761 if (!live_in_glue_dir(glue_dir, dev))
2764 mutex_lock(&gdp_mutex);
2766 * There is a race condition between removing glue directory
2767 * and adding a new device under the glue directory.
2772 * get_device_parent()
2773 * class_dir_create_and_add()
2774 * kobject_add_internal()
2775 * create_dir() // create glue_dir
2778 * get_device_parent()
2779 * kobject_get() // get glue_dir
2782 * cleanup_glue_dir()
2783 * kobject_del(glue_dir)
2786 * kobject_add_internal()
2787 * create_dir() // in glue_dir
2788 * sysfs_create_dir_ns()
2789 * kernfs_create_dir_ns(sd)
2791 * sysfs_remove_dir() // glue_dir->sd=NULL
2792 * sysfs_put() // free glue_dir->sd
2795 * kernfs_new_node(sd)
2796 * kernfs_get(glue_dir)
2800 * Before CPU1 remove last child device under glue dir, if CPU2 add
2801 * a new device under glue dir, the glue_dir kobject reference count
2802 * will be increase to 2 in kobject_get(k). And CPU2 has been called
2803 * kernfs_create_dir_ns(). Meanwhile, CPU1 call sysfs_remove_dir()
2804 * and sysfs_put(). This result in glue_dir->sd is freed.
2806 * Then the CPU2 will see a stale "empty" but still potentially used
2807 * glue dir around in kernfs_new_node().
2809 * In order to avoid this happening, we also should make sure that
2810 * kernfs_node for glue_dir is released in CPU1 only when refcount
2811 * for glue_dir kobj is 1.
2813 ref = kref_read(&glue_dir->kref);
2814 if (!kobject_has_children(glue_dir) && !--ref)
2815 kobject_del(glue_dir);
2816 kobject_put(glue_dir);
2817 mutex_unlock(&gdp_mutex);
2820 static int device_add_class_symlinks(struct device *dev)
2822 struct device_node *of_node = dev_of_node(dev);
2826 error = sysfs_create_link(&dev->kobj, of_node_kobj(of_node), "of_node");
2828 dev_warn(dev, "Error %d creating of_node link\n",error);
2829 /* An error here doesn't warrant bringing down the device */
2835 error = sysfs_create_link(&dev->kobj,
2836 &dev->class->p->subsys.kobj,
2841 if (dev->parent && device_is_not_partition(dev)) {
2842 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
2849 /* /sys/block has directories and does not need symlinks */
2850 if (sysfs_deprecated && dev->class == &block_class)
2854 /* link in the class directory pointing to the device */
2855 error = sysfs_create_link(&dev->class->p->subsys.kobj,
2856 &dev->kobj, dev_name(dev));
2863 sysfs_remove_link(&dev->kobj, "device");
2866 sysfs_remove_link(&dev->kobj, "subsystem");
2868 sysfs_remove_link(&dev->kobj, "of_node");
2872 static void device_remove_class_symlinks(struct device *dev)
2874 if (dev_of_node(dev))
2875 sysfs_remove_link(&dev->kobj, "of_node");
2880 if (dev->parent && device_is_not_partition(dev))
2881 sysfs_remove_link(&dev->kobj, "device");
2882 sysfs_remove_link(&dev->kobj, "subsystem");
2884 if (sysfs_deprecated && dev->class == &block_class)
2887 sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
2891 * dev_set_name - set a device name
2893 * @fmt: format string for the device's name
2895 int dev_set_name(struct device *dev, const char *fmt, ...)
2900 va_start(vargs, fmt);
2901 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
2905 EXPORT_SYMBOL_GPL(dev_set_name);
2908 * device_to_dev_kobj - select a /sys/dev/ directory for the device
2911 * By default we select char/ for new entries. Setting class->dev_obj
2912 * to NULL prevents an entry from being created. class->dev_kobj must
2913 * be set (or cleared) before any devices are registered to the class
2914 * otherwise device_create_sys_dev_entry() and
2915 * device_remove_sys_dev_entry() will disagree about the presence of
2918 static struct kobject *device_to_dev_kobj(struct device *dev)
2920 struct kobject *kobj;
2923 kobj = dev->class->dev_kobj;
2925 kobj = sysfs_dev_char_kobj;
2930 static int device_create_sys_dev_entry(struct device *dev)
2932 struct kobject *kobj = device_to_dev_kobj(dev);
2937 format_dev_t(devt_str, dev->devt);
2938 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
2944 static void device_remove_sys_dev_entry(struct device *dev)
2946 struct kobject *kobj = device_to_dev_kobj(dev);
2950 format_dev_t(devt_str, dev->devt);
2951 sysfs_remove_link(kobj, devt_str);
2955 static int device_private_init(struct device *dev)
2957 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
2960 dev->p->device = dev;
2961 klist_init(&dev->p->klist_children, klist_children_get,
2962 klist_children_put);
2963 INIT_LIST_HEAD(&dev->p->deferred_probe);
2968 * device_add - add device to device hierarchy.
2971 * This is part 2 of device_register(), though may be called
2972 * separately _iff_ device_initialize() has been called separately.
2974 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
2975 * to the global and sibling lists for the device, then
2976 * adds it to the other relevant subsystems of the driver model.
2978 * Do not call this routine or device_register() more than once for
2979 * any device structure. The driver model core is not designed to work
2980 * with devices that get unregistered and then spring back to life.
2981 * (Among other things, it's very hard to guarantee that all references
2982 * to the previous incarnation of @dev have been dropped.) Allocate
2983 * and register a fresh new struct device instead.
2985 * NOTE: _Never_ directly free @dev after calling this function, even
2986 * if it returned an error! Always use put_device() to give up your
2987 * reference instead.
2989 * Rule of thumb is: if device_add() succeeds, you should call
2990 * device_del() when you want to get rid of it. If device_add() has
2991 * *not* succeeded, use *only* put_device() to drop the reference
2994 int device_add(struct device *dev)
2996 struct device *parent;
2997 struct kobject *kobj;
2998 struct class_interface *class_intf;
2999 int error = -EINVAL;
3000 struct kobject *glue_dir = NULL;
3002 dev = get_device(dev);
3007 error = device_private_init(dev);
3013 * for statically allocated devices, which should all be converted
3014 * some day, we need to initialize the name. We prevent reading back
3015 * the name, and force the use of dev_name()
3017 if (dev->init_name) {
3018 dev_set_name(dev, "%s", dev->init_name);
3019 dev->init_name = NULL;
3022 /* subsystems can specify simple device enumeration */
3023 if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
3024 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
3026 if (!dev_name(dev)) {
3031 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
3033 parent = get_device(dev->parent);
3034 kobj = get_device_parent(dev, parent);
3036 error = PTR_ERR(kobj);
3040 dev->kobj.parent = kobj;
3042 /* use parent numa_node */
3043 if (parent && (dev_to_node(dev) == NUMA_NO_NODE))
3044 set_dev_node(dev, dev_to_node(parent));
3046 /* first, register with generic layer. */
3047 /* we require the name to be set before, and pass NULL */
3048 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
3050 glue_dir = get_glue_dir(dev);
3054 /* notify platform of device entry */
3055 error = device_platform_notify(dev, KOBJ_ADD);
3057 goto platform_error;
3059 error = device_create_file(dev, &dev_attr_uevent);
3063 error = device_add_class_symlinks(dev);
3066 error = device_add_attrs(dev);
3069 error = bus_add_device(dev);
3072 error = dpm_sysfs_add(dev);
3077 if (MAJOR(dev->devt)) {
3078 error = device_create_file(dev, &dev_attr_dev);
3082 error = device_create_sys_dev_entry(dev);
3086 devtmpfs_create_node(dev);
3089 /* Notify clients of device addition. This call must come
3090 * after dpm_sysfs_add() and before kobject_uevent().
3093 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
3094 BUS_NOTIFY_ADD_DEVICE, dev);
3096 kobject_uevent(&dev->kobj, KOBJ_ADD);
3099 * Check if any of the other devices (consumers) have been waiting for
3100 * this device (supplier) to be added so that they can create a device
3103 * This needs to happen after device_pm_add() because device_link_add()
3104 * requires the supplier be registered before it's called.
3106 * But this also needs to happen before bus_probe_device() to make sure
3107 * waiting consumers can link to it before the driver is bound to the
3108 * device and the driver sync_state callback is called for this device.
3110 if (dev->fwnode && !dev->fwnode->dev) {
3111 dev->fwnode->dev = dev;
3112 fw_devlink_link_device(dev);
3115 bus_probe_device(dev);
3117 klist_add_tail(&dev->p->knode_parent,
3118 &parent->p->klist_children);
3121 mutex_lock(&dev->class->p->mutex);
3122 /* tie the class to the device */
3123 klist_add_tail(&dev->p->knode_class,
3124 &dev->class->p->klist_devices);
3126 /* notify any interfaces that the device is here */
3127 list_for_each_entry(class_intf,
3128 &dev->class->p->interfaces, node)
3129 if (class_intf->add_dev)
3130 class_intf->add_dev(dev, class_intf);
3131 mutex_unlock(&dev->class->p->mutex);
3137 if (MAJOR(dev->devt))
3138 device_remove_file(dev, &dev_attr_dev);
3140 device_pm_remove(dev);
3141 dpm_sysfs_remove(dev);
3143 bus_remove_device(dev);
3145 device_remove_attrs(dev);
3147 device_remove_class_symlinks(dev);
3149 device_remove_file(dev, &dev_attr_uevent);
3151 device_platform_notify(dev, KOBJ_REMOVE);
3153 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
3154 glue_dir = get_glue_dir(dev);
3155 kobject_del(&dev->kobj);
3157 cleanup_glue_dir(dev, glue_dir);
3165 EXPORT_SYMBOL_GPL(device_add);
3168 * device_register - register a device with the system.
3169 * @dev: pointer to the device structure
3171 * This happens in two clean steps - initialize the device
3172 * and add it to the system. The two steps can be called
3173 * separately, but this is the easiest and most common.
3174 * I.e. you should only call the two helpers separately if
3175 * have a clearly defined need to use and refcount the device
3176 * before it is added to the hierarchy.
3178 * For more information, see the kerneldoc for device_initialize()
3181 * NOTE: _Never_ directly free @dev after calling this function, even
3182 * if it returned an error! Always use put_device() to give up the
3183 * reference initialized in this function instead.
3185 int device_register(struct device *dev)
3187 device_initialize(dev);
3188 return device_add(dev);
3190 EXPORT_SYMBOL_GPL(device_register);
3193 * get_device - increment reference count for device.
3196 * This simply forwards the call to kobject_get(), though
3197 * we do take care to provide for the case that we get a NULL
3198 * pointer passed in.
3200 struct device *get_device(struct device *dev)
3202 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
3204 EXPORT_SYMBOL_GPL(get_device);
3207 * put_device - decrement reference count.
3208 * @dev: device in question.
3210 void put_device(struct device *dev)
3212 /* might_sleep(); */
3214 kobject_put(&dev->kobj);
3216 EXPORT_SYMBOL_GPL(put_device);
3218 bool kill_device(struct device *dev)
3221 * Require the device lock and set the "dead" flag to guarantee that
3222 * the update behavior is consistent with the other bitfields near
3223 * it and that we cannot have an asynchronous probe routine trying
3224 * to run while we are tearing out the bus/class/sysfs from
3225 * underneath the device.
3227 lockdep_assert_held(&dev->mutex);
3231 dev->p->dead = true;
3234 EXPORT_SYMBOL_GPL(kill_device);
3237 * device_del - delete device from system.
3240 * This is the first part of the device unregistration
3241 * sequence. This removes the device from the lists we control
3242 * from here, has it removed from the other driver model
3243 * subsystems it was added to in device_add(), and removes it
3244 * from the kobject hierarchy.
3246 * NOTE: this should be called manually _iff_ device_add() was
3247 * also called manually.
3249 void device_del(struct device *dev)
3251 struct device *parent = dev->parent;
3252 struct kobject *glue_dir = NULL;
3253 struct class_interface *class_intf;
3254 unsigned int noio_flag;
3260 if (dev->fwnode && dev->fwnode->dev == dev)
3261 dev->fwnode->dev = NULL;
3263 /* Notify clients of device removal. This call must come
3264 * before dpm_sysfs_remove().
3266 noio_flag = memalloc_noio_save();
3268 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
3269 BUS_NOTIFY_DEL_DEVICE, dev);
3271 dpm_sysfs_remove(dev);
3273 klist_del(&dev->p->knode_parent);
3274 if (MAJOR(dev->devt)) {
3275 devtmpfs_delete_node(dev);
3276 device_remove_sys_dev_entry(dev);
3277 device_remove_file(dev, &dev_attr_dev);
3280 device_remove_class_symlinks(dev);
3282 mutex_lock(&dev->class->p->mutex);
3283 /* notify any interfaces that the device is now gone */
3284 list_for_each_entry(class_intf,
3285 &dev->class->p->interfaces, node)
3286 if (class_intf->remove_dev)
3287 class_intf->remove_dev(dev, class_intf);
3288 /* remove the device from the class list */
3289 klist_del(&dev->p->knode_class);
3290 mutex_unlock(&dev->class->p->mutex);
3292 device_remove_file(dev, &dev_attr_uevent);
3293 device_remove_attrs(dev);
3294 bus_remove_device(dev);
3295 device_pm_remove(dev);
3296 driver_deferred_probe_del(dev);
3297 device_platform_notify(dev, KOBJ_REMOVE);
3298 device_remove_properties(dev);
3299 device_links_purge(dev);
3302 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
3303 BUS_NOTIFY_REMOVED_DEVICE, dev);
3304 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
3305 glue_dir = get_glue_dir(dev);
3306 kobject_del(&dev->kobj);
3307 cleanup_glue_dir(dev, glue_dir);
3308 memalloc_noio_restore(noio_flag);
3311 EXPORT_SYMBOL_GPL(device_del);
3314 * device_unregister - unregister device from system.
3315 * @dev: device going away.
3317 * We do this in two parts, like we do device_register(). First,
3318 * we remove it from all the subsystems with device_del(), then
3319 * we decrement the reference count via put_device(). If that
3320 * is the final reference count, the device will be cleaned up
3321 * via device_release() above. Otherwise, the structure will
3322 * stick around until the final reference to the device is dropped.
3324 void device_unregister(struct device *dev)
3326 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
3330 EXPORT_SYMBOL_GPL(device_unregister);
3332 static struct device *prev_device(struct klist_iter *i)
3334 struct klist_node *n = klist_prev(i);
3335 struct device *dev = NULL;
3336 struct device_private *p;
3339 p = to_device_private_parent(n);
3345 static struct device *next_device(struct klist_iter *i)
3347 struct klist_node *n = klist_next(i);
3348 struct device *dev = NULL;
3349 struct device_private *p;
3352 p = to_device_private_parent(n);
3359 * device_get_devnode - path of device node file
3361 * @mode: returned file access mode
3362 * @uid: returned file owner
3363 * @gid: returned file group
3364 * @tmp: possibly allocated string
3366 * Return the relative path of a possible device node.
3367 * Non-default names may need to allocate a memory to compose
3368 * a name. This memory is returned in tmp and needs to be
3369 * freed by the caller.
3371 const char *device_get_devnode(struct device *dev,
3372 umode_t *mode, kuid_t *uid, kgid_t *gid,
3379 /* the device type may provide a specific name */
3380 if (dev->type && dev->type->devnode)
3381 *tmp = dev->type->devnode(dev, mode, uid, gid);
3385 /* the class may provide a specific name */
3386 if (dev->class && dev->class->devnode)
3387 *tmp = dev->class->devnode(dev, mode);
3391 /* return name without allocation, tmp == NULL */
3392 if (strchr(dev_name(dev), '!') == NULL)
3393 return dev_name(dev);
3395 /* replace '!' in the name with '/' */
3396 s = kstrdup(dev_name(dev), GFP_KERNEL);
3399 strreplace(s, '!', '/');
3404 * device_for_each_child - device child iterator.
3405 * @parent: parent struct device.
3406 * @fn: function to be called for each device.
3407 * @data: data for the callback.
3409 * Iterate over @parent's child devices, and call @fn for each,
3412 * We check the return of @fn each time. If it returns anything
3413 * other than 0, we break out and return that value.
3415 int device_for_each_child(struct device *parent, void *data,
3416 int (*fn)(struct device *dev, void *data))
3418 struct klist_iter i;
3419 struct device *child;
3425 klist_iter_init(&parent->p->klist_children, &i);
3426 while (!error && (child = next_device(&i)))
3427 error = fn(child, data);
3428 klist_iter_exit(&i);
3431 EXPORT_SYMBOL_GPL(device_for_each_child);
3434 * device_for_each_child_reverse - device child iterator in reversed order.
3435 * @parent: parent struct device.
3436 * @fn: function to be called for each device.
3437 * @data: data for the callback.
3439 * Iterate over @parent's child devices, and call @fn for each,
3442 * We check the return of @fn each time. If it returns anything
3443 * other than 0, we break out and return that value.
3445 int device_for_each_child_reverse(struct device *parent, void *data,
3446 int (*fn)(struct device *dev, void *data))
3448 struct klist_iter i;
3449 struct device *child;
3455 klist_iter_init(&parent->p->klist_children, &i);
3456 while ((child = prev_device(&i)) && !error)
3457 error = fn(child, data);
3458 klist_iter_exit(&i);
3461 EXPORT_SYMBOL_GPL(device_for_each_child_reverse);
3464 * device_find_child - device iterator for locating a particular device.
3465 * @parent: parent struct device
3466 * @match: Callback function to check device
3467 * @data: Data to pass to match function
3469 * This is similar to the device_for_each_child() function above, but it
3470 * returns a reference to a device that is 'found' for later use, as
3471 * determined by the @match callback.
3473 * The callback should return 0 if the device doesn't match and non-zero
3474 * if it does. If the callback returns non-zero and a reference to the
3475 * current device can be obtained, this function will return to the caller
3476 * and not iterate over any more devices.
3478 * NOTE: you will need to drop the reference with put_device() after use.
3480 struct device *device_find_child(struct device *parent, void *data,
3481 int (*match)(struct device *dev, void *data))
3483 struct klist_iter i;
3484 struct device *child;
3489 klist_iter_init(&parent->p->klist_children, &i);
3490 while ((child = next_device(&i)))
3491 if (match(child, data) && get_device(child))
3493 klist_iter_exit(&i);
3496 EXPORT_SYMBOL_GPL(device_find_child);
3499 * device_find_child_by_name - device iterator for locating a child device.
3500 * @parent: parent struct device
3501 * @name: name of the child device
3503 * This is similar to the device_find_child() function above, but it
3504 * returns a reference to a device that has the name @name.
3506 * NOTE: you will need to drop the reference with put_device() after use.
3508 struct device *device_find_child_by_name(struct device *parent,
3511 struct klist_iter i;
3512 struct device *child;
3517 klist_iter_init(&parent->p->klist_children, &i);
3518 while ((child = next_device(&i)))
3519 if (sysfs_streq(dev_name(child), name) && get_device(child))
3521 klist_iter_exit(&i);
3524 EXPORT_SYMBOL_GPL(device_find_child_by_name);
3526 int __init devices_init(void)
3528 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
3531 dev_kobj = kobject_create_and_add("dev", NULL);
3534 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
3535 if (!sysfs_dev_block_kobj)
3536 goto block_kobj_err;
3537 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
3538 if (!sysfs_dev_char_kobj)
3544 kobject_put(sysfs_dev_block_kobj);
3546 kobject_put(dev_kobj);
3548 kset_unregister(devices_kset);
3552 static int device_check_offline(struct device *dev, void *not_used)
3556 ret = device_for_each_child(dev, NULL, device_check_offline);
3560 return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
3564 * device_offline - Prepare the device for hot-removal.
3565 * @dev: Device to be put offline.
3567 * Execute the device bus type's .offline() callback, if present, to prepare
3568 * the device for a subsequent hot-removal. If that succeeds, the device must
3569 * not be used until either it is removed or its bus type's .online() callback
3572 * Call under device_hotplug_lock.
3574 int device_offline(struct device *dev)
3578 if (dev->offline_disabled)
3581 ret = device_for_each_child(dev, NULL, device_check_offline);
3586 if (device_supports_offline(dev)) {
3590 ret = dev->bus->offline(dev);
3592 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
3593 dev->offline = true;
3603 * device_online - Put the device back online after successful device_offline().
3604 * @dev: Device to be put back online.
3606 * If device_offline() has been successfully executed for @dev, but the device
3607 * has not been removed subsequently, execute its bus type's .online() callback
3608 * to indicate that the device can be used again.
3610 * Call under device_hotplug_lock.
3612 int device_online(struct device *dev)
3617 if (device_supports_offline(dev)) {
3619 ret = dev->bus->online(dev);
3621 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
3622 dev->offline = false;
3633 struct root_device {
3635 struct module *owner;
3638 static inline struct root_device *to_root_device(struct device *d)
3640 return container_of(d, struct root_device, dev);
3643 static void root_device_release(struct device *dev)
3645 kfree(to_root_device(dev));
3649 * __root_device_register - allocate and register a root device
3650 * @name: root device name
3651 * @owner: owner module of the root device, usually THIS_MODULE
3653 * This function allocates a root device and registers it
3654 * using device_register(). In order to free the returned
3655 * device, use root_device_unregister().
3657 * Root devices are dummy devices which allow other devices
3658 * to be grouped under /sys/devices. Use this function to
3659 * allocate a root device and then use it as the parent of
3660 * any device which should appear under /sys/devices/{name}
3662 * The /sys/devices/{name} directory will also contain a
3663 * 'module' symlink which points to the @owner directory
3666 * Returns &struct device pointer on success, or ERR_PTR() on error.
3668 * Note: You probably want to use root_device_register().
3670 struct device *__root_device_register(const char *name, struct module *owner)
3672 struct root_device *root;
3675 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
3677 return ERR_PTR(err);
3679 err = dev_set_name(&root->dev, "%s", name);
3682 return ERR_PTR(err);
3685 root->dev.release = root_device_release;
3687 err = device_register(&root->dev);
3689 put_device(&root->dev);
3690 return ERR_PTR(err);
3693 #ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
3695 struct module_kobject *mk = &owner->mkobj;
3697 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
3699 device_unregister(&root->dev);
3700 return ERR_PTR(err);
3702 root->owner = owner;
3708 EXPORT_SYMBOL_GPL(__root_device_register);
3711 * root_device_unregister - unregister and free a root device
3712 * @dev: device going away
3714 * This function unregisters and cleans up a device that was created by
3715 * root_device_register().
3717 void root_device_unregister(struct device *dev)
3719 struct root_device *root = to_root_device(dev);
3722 sysfs_remove_link(&root->dev.kobj, "module");
3724 device_unregister(dev);
3726 EXPORT_SYMBOL_GPL(root_device_unregister);
3729 static void device_create_release(struct device *dev)
3731 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
3735 static __printf(6, 0) struct device *
3736 device_create_groups_vargs(struct class *class, struct device *parent,
3737 dev_t devt, void *drvdata,
3738 const struct attribute_group **groups,
3739 const char *fmt, va_list args)
3741 struct device *dev = NULL;
3742 int retval = -ENODEV;
3744 if (class == NULL || IS_ERR(class))
3747 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
3753 device_initialize(dev);
3756 dev->parent = parent;
3757 dev->groups = groups;
3758 dev->release = device_create_release;
3759 dev_set_drvdata(dev, drvdata);
3761 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
3765 retval = device_add(dev);
3773 return ERR_PTR(retval);
3777 * device_create - creates a device and registers it with sysfs
3778 * @class: pointer to the struct class that this device should be registered to
3779 * @parent: pointer to the parent struct device of this new device, if any
3780 * @devt: the dev_t for the char device to be added
3781 * @drvdata: the data to be added to the device for callbacks
3782 * @fmt: string for the device's name
3784 * This function can be used by char device classes. A struct device
3785 * will be created in sysfs, registered to the specified class.
3787 * A "dev" file will be created, showing the dev_t for the device, if
3788 * the dev_t is not 0,0.
3789 * If a pointer to a parent struct device is passed in, the newly created
3790 * struct device will be a child of that device in sysfs.
3791 * The pointer to the struct device will be returned from the call.
3792 * Any further sysfs files that might be required can be created using this
3795 * Returns &struct device pointer on success, or ERR_PTR() on error.
3797 * Note: the struct class passed to this function must have previously
3798 * been created with a call to class_create().
3800 struct device *device_create(struct class *class, struct device *parent,
3801 dev_t devt, void *drvdata, const char *fmt, ...)
3806 va_start(vargs, fmt);
3807 dev = device_create_groups_vargs(class, parent, devt, drvdata, NULL,
3812 EXPORT_SYMBOL_GPL(device_create);
3815 * device_create_with_groups - creates a device and registers it with sysfs
3816 * @class: pointer to the struct class that this device should be registered to
3817 * @parent: pointer to the parent struct device of this new device, if any
3818 * @devt: the dev_t for the char device to be added
3819 * @drvdata: the data to be added to the device for callbacks
3820 * @groups: NULL-terminated list of attribute groups to be created
3821 * @fmt: string for the device's name
3823 * This function can be used by char device classes. A struct device
3824 * will be created in sysfs, registered to the specified class.
3825 * Additional attributes specified in the groups parameter will also
3826 * be created automatically.
3828 * A "dev" file will be created, showing the dev_t for the device, if
3829 * the dev_t is not 0,0.
3830 * If a pointer to a parent struct device is passed in, the newly created
3831 * struct device will be a child of that device in sysfs.
3832 * The pointer to the struct device will be returned from the call.
3833 * Any further sysfs files that might be required can be created using this
3836 * Returns &struct device pointer on success, or ERR_PTR() on error.
3838 * Note: the struct class passed to this function must have previously
3839 * been created with a call to class_create().
3841 struct device *device_create_with_groups(struct class *class,
3842 struct device *parent, dev_t devt,
3844 const struct attribute_group **groups,
3845 const char *fmt, ...)
3850 va_start(vargs, fmt);
3851 dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
3856 EXPORT_SYMBOL_GPL(device_create_with_groups);
3859 * device_destroy - removes a device that was created with device_create()
3860 * @class: pointer to the struct class that this device was registered with
3861 * @devt: the dev_t of the device that was previously registered
3863 * This call unregisters and cleans up a device that was created with a
3864 * call to device_create().
3866 void device_destroy(struct class *class, dev_t devt)
3870 dev = class_find_device_by_devt(class, devt);
3873 device_unregister(dev);
3876 EXPORT_SYMBOL_GPL(device_destroy);
3879 * device_rename - renames a device
3880 * @dev: the pointer to the struct device to be renamed
3881 * @new_name: the new name of the device
3883 * It is the responsibility of the caller to provide mutual
3884 * exclusion between two different calls of device_rename
3885 * on the same device to ensure that new_name is valid and
3886 * won't conflict with other devices.
3888 * Note: Don't call this function. Currently, the networking layer calls this
3889 * function, but that will change. The following text from Kay Sievers offers
3892 * Renaming devices is racy at many levels, symlinks and other stuff are not
3893 * replaced atomically, and you get a "move" uevent, but it's not easy to
3894 * connect the event to the old and new device. Device nodes are not renamed at
3895 * all, there isn't even support for that in the kernel now.
3897 * In the meantime, during renaming, your target name might be taken by another
3898 * driver, creating conflicts. Or the old name is taken directly after you
3899 * renamed it -- then you get events for the same DEVPATH, before you even see
3900 * the "move" event. It's just a mess, and nothing new should ever rely on
3901 * kernel device renaming. Besides that, it's not even implemented now for
3902 * other things than (driver-core wise very simple) network devices.
3904 * We are currently about to change network renaming in udev to completely
3905 * disallow renaming of devices in the same namespace as the kernel uses,
3906 * because we can't solve the problems properly, that arise with swapping names
3907 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
3908 * be allowed to some other name than eth[0-9]*, for the aforementioned
3911 * Make up a "real" name in the driver before you register anything, or add
3912 * some other attributes for userspace to find the device, or use udev to add
3913 * symlinks -- but never rename kernel devices later, it's a complete mess. We
3914 * don't even want to get into that and try to implement the missing pieces in
3915 * the core. We really have other pieces to fix in the driver core mess. :)
3917 int device_rename(struct device *dev, const char *new_name)
3919 struct kobject *kobj = &dev->kobj;
3920 char *old_device_name = NULL;
3923 dev = get_device(dev);
3927 dev_dbg(dev, "renaming to %s\n", new_name);
3929 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
3930 if (!old_device_name) {
3936 error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj,
3937 kobj, old_device_name,
3938 new_name, kobject_namespace(kobj));
3943 error = kobject_rename(kobj, new_name);
3950 kfree(old_device_name);
3954 EXPORT_SYMBOL_GPL(device_rename);
3956 static int device_move_class_links(struct device *dev,
3957 struct device *old_parent,
3958 struct device *new_parent)
3963 sysfs_remove_link(&dev->kobj, "device");
3965 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
3971 * device_move - moves a device to a new parent
3972 * @dev: the pointer to the struct device to be moved
3973 * @new_parent: the new parent of the device (can be NULL)
3974 * @dpm_order: how to reorder the dpm_list
3976 int device_move(struct device *dev, struct device *new_parent,
3977 enum dpm_order dpm_order)
3980 struct device *old_parent;
3981 struct kobject *new_parent_kobj;
3983 dev = get_device(dev);
3988 new_parent = get_device(new_parent);
3989 new_parent_kobj = get_device_parent(dev, new_parent);
3990 if (IS_ERR(new_parent_kobj)) {
3991 error = PTR_ERR(new_parent_kobj);
3992 put_device(new_parent);
3996 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
3997 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
3998 error = kobject_move(&dev->kobj, new_parent_kobj);
4000 cleanup_glue_dir(dev, new_parent_kobj);
4001 put_device(new_parent);
4004 old_parent = dev->parent;
4005 dev->parent = new_parent;
4007 klist_remove(&dev->p->knode_parent);
4009 klist_add_tail(&dev->p->knode_parent,
4010 &new_parent->p->klist_children);
4011 set_dev_node(dev, dev_to_node(new_parent));
4015 error = device_move_class_links(dev, old_parent, new_parent);
4017 /* We ignore errors on cleanup since we're hosed anyway... */
4018 device_move_class_links(dev, new_parent, old_parent);
4019 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
4021 klist_remove(&dev->p->knode_parent);
4022 dev->parent = old_parent;
4024 klist_add_tail(&dev->p->knode_parent,
4025 &old_parent->p->klist_children);
4026 set_dev_node(dev, dev_to_node(old_parent));
4029 cleanup_glue_dir(dev, new_parent_kobj);
4030 put_device(new_parent);
4034 switch (dpm_order) {
4035 case DPM_ORDER_NONE:
4037 case DPM_ORDER_DEV_AFTER_PARENT:
4038 device_pm_move_after(dev, new_parent);
4039 devices_kset_move_after(dev, new_parent);
4041 case DPM_ORDER_PARENT_BEFORE_DEV:
4042 device_pm_move_before(new_parent, dev);
4043 devices_kset_move_before(new_parent, dev);
4045 case DPM_ORDER_DEV_LAST:
4046 device_pm_move_last(dev);
4047 devices_kset_move_last(dev);
4051 put_device(old_parent);
4057 EXPORT_SYMBOL_GPL(device_move);
4059 static int device_attrs_change_owner(struct device *dev, kuid_t kuid,
4062 struct kobject *kobj = &dev->kobj;
4063 struct class *class = dev->class;
4064 const struct device_type *type = dev->type;
4069 * Change the device groups of the device class for @dev to
4072 error = sysfs_groups_change_owner(kobj, class->dev_groups, kuid,
4080 * Change the device groups of the device type for @dev to
4083 error = sysfs_groups_change_owner(kobj, type->groups, kuid,
4089 /* Change the device groups of @dev to @kuid/@kgid. */
4090 error = sysfs_groups_change_owner(kobj, dev->groups, kuid, kgid);
4094 if (device_supports_offline(dev) && !dev->offline_disabled) {
4095 /* Change online device attributes of @dev to @kuid/@kgid. */
4096 error = sysfs_file_change_owner(kobj, dev_attr_online.attr.name,
4106 * device_change_owner - change the owner of an existing device.
4108 * @kuid: new owner's kuid
4109 * @kgid: new owner's kgid
4111 * This changes the owner of @dev and its corresponding sysfs entries to
4112 * @kuid/@kgid. This function closely mirrors how @dev was added via driver
4115 * Returns 0 on success or error code on failure.
4117 int device_change_owner(struct device *dev, kuid_t kuid, kgid_t kgid)
4120 struct kobject *kobj = &dev->kobj;
4122 dev = get_device(dev);
4127 * Change the kobject and the default attributes and groups of the
4128 * ktype associated with it to @kuid/@kgid.
4130 error = sysfs_change_owner(kobj, kuid, kgid);
4135 * Change the uevent file for @dev to the new owner. The uevent file
4136 * was created in a separate step when @dev got added and we mirror
4139 error = sysfs_file_change_owner(kobj, dev_attr_uevent.attr.name, kuid,
4145 * Change the device groups, the device groups associated with the
4146 * device class, and the groups associated with the device type of @dev
4149 error = device_attrs_change_owner(dev, kuid, kgid);
4153 error = dpm_sysfs_change_owner(dev, kuid, kgid);
4158 if (sysfs_deprecated && dev->class == &block_class)
4163 * Change the owner of the symlink located in the class directory of
4164 * the device class associated with @dev which points to the actual
4165 * directory entry for @dev to @kuid/@kgid. This ensures that the
4166 * symlink shows the same permissions as its target.
4168 error = sysfs_link_change_owner(&dev->class->p->subsys.kobj, &dev->kobj,
4169 dev_name(dev), kuid, kgid);
4177 EXPORT_SYMBOL_GPL(device_change_owner);
4180 * device_shutdown - call ->shutdown() on each device to shutdown.
4182 void device_shutdown(void)
4184 struct device *dev, *parent;
4186 wait_for_device_probe();
4187 device_block_probing();
4191 spin_lock(&devices_kset->list_lock);
4193 * Walk the devices list backward, shutting down each in turn.
4194 * Beware that device unplug events may also start pulling
4195 * devices offline, even as the system is shutting down.
4197 while (!list_empty(&devices_kset->list)) {
4198 dev = list_entry(devices_kset->list.prev, struct device,
4202 * hold reference count of device's parent to
4203 * prevent it from being freed because parent's
4204 * lock is to be held
4206 parent = get_device(dev->parent);
4209 * Make sure the device is off the kset list, in the
4210 * event that dev->*->shutdown() doesn't remove it.
4212 list_del_init(&dev->kobj.entry);
4213 spin_unlock(&devices_kset->list_lock);
4215 /* hold lock to avoid race with probe/release */
4217 device_lock(parent);
4220 /* Don't allow any more runtime suspends */
4221 pm_runtime_get_noresume(dev);
4222 pm_runtime_barrier(dev);
4224 if (dev->class && dev->class->shutdown_pre) {
4226 dev_info(dev, "shutdown_pre\n");
4227 dev->class->shutdown_pre(dev);
4229 if (dev->bus && dev->bus->shutdown) {
4231 dev_info(dev, "shutdown\n");
4232 dev->bus->shutdown(dev);
4233 } else if (dev->driver && dev->driver->shutdown) {
4235 dev_info(dev, "shutdown\n");
4236 dev->driver->shutdown(dev);
4241 device_unlock(parent);
4246 spin_lock(&devices_kset->list_lock);
4248 spin_unlock(&devices_kset->list_lock);
4252 * Device logging functions
4255 #ifdef CONFIG_PRINTK
4257 set_dev_info(const struct device *dev, struct dev_printk_info *dev_info)
4261 memset(dev_info, 0, sizeof(*dev_info));
4264 subsys = dev->class->name;
4266 subsys = dev->bus->name;
4270 strscpy(dev_info->subsystem, subsys, sizeof(dev_info->subsystem));
4273 * Add device identifier DEVICE=:
4277 * +sound:card0 subsystem:devname
4279 if (MAJOR(dev->devt)) {
4282 if (strcmp(subsys, "block") == 0)
4287 snprintf(dev_info->device, sizeof(dev_info->device),
4288 "%c%u:%u", c, MAJOR(dev->devt), MINOR(dev->devt));
4289 } else if (strcmp(subsys, "net") == 0) {
4290 struct net_device *net = to_net_dev(dev);
4292 snprintf(dev_info->device, sizeof(dev_info->device),
4293 "n%u", net->ifindex);
4295 snprintf(dev_info->device, sizeof(dev_info->device),
4296 "+%s:%s", subsys, dev_name(dev));
4300 int dev_vprintk_emit(int level, const struct device *dev,
4301 const char *fmt, va_list args)
4303 struct dev_printk_info dev_info;
4305 set_dev_info(dev, &dev_info);
4307 return vprintk_emit(0, level, &dev_info, fmt, args);
4309 EXPORT_SYMBOL(dev_vprintk_emit);
4311 int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
4316 va_start(args, fmt);
4318 r = dev_vprintk_emit(level, dev, fmt, args);
4324 EXPORT_SYMBOL(dev_printk_emit);
4326 static void __dev_printk(const char *level, const struct device *dev,
4327 struct va_format *vaf)
4330 dev_printk_emit(level[1] - '0', dev, "%s %s: %pV",
4331 dev_driver_string(dev), dev_name(dev), vaf);
4333 printk("%s(NULL device *): %pV", level, vaf);
4336 void dev_printk(const char *level, const struct device *dev,
4337 const char *fmt, ...)
4339 struct va_format vaf;
4342 va_start(args, fmt);
4347 __dev_printk(level, dev, &vaf);
4351 EXPORT_SYMBOL(dev_printk);
4353 #define define_dev_printk_level(func, kern_level) \
4354 void func(const struct device *dev, const char *fmt, ...) \
4356 struct va_format vaf; \
4359 va_start(args, fmt); \
4364 __dev_printk(kern_level, dev, &vaf); \
4368 EXPORT_SYMBOL(func);
4370 define_dev_printk_level(_dev_emerg, KERN_EMERG);
4371 define_dev_printk_level(_dev_alert, KERN_ALERT);
4372 define_dev_printk_level(_dev_crit, KERN_CRIT);
4373 define_dev_printk_level(_dev_err, KERN_ERR);
4374 define_dev_printk_level(_dev_warn, KERN_WARNING);
4375 define_dev_printk_level(_dev_notice, KERN_NOTICE);
4376 define_dev_printk_level(_dev_info, KERN_INFO);
4381 * dev_err_probe - probe error check and log helper
4382 * @dev: the pointer to the struct device
4383 * @err: error value to test
4384 * @fmt: printf-style format string
4385 * @...: arguments as specified in the format string
4387 * This helper implements common pattern present in probe functions for error
4388 * checking: print debug or error message depending if the error value is
4389 * -EPROBE_DEFER and propagate error upwards.
4390 * In case of -EPROBE_DEFER it sets also defer probe reason, which can be
4391 * checked later by reading devices_deferred debugfs attribute.
4392 * It replaces code sequence::
4394 * if (err != -EPROBE_DEFER)
4395 * dev_err(dev, ...);
4397 * dev_dbg(dev, ...);
4402 * return dev_err_probe(dev, err, ...);
4407 int dev_err_probe(const struct device *dev, int err, const char *fmt, ...)
4409 struct va_format vaf;
4412 va_start(args, fmt);
4416 if (err != -EPROBE_DEFER) {
4417 dev_err(dev, "error %pe: %pV", ERR_PTR(err), &vaf);
4419 device_set_deferred_probe_reason(dev, &vaf);
4420 dev_dbg(dev, "error %pe: %pV", ERR_PTR(err), &vaf);
4427 EXPORT_SYMBOL_GPL(dev_err_probe);
4429 static inline bool fwnode_is_primary(struct fwnode_handle *fwnode)
4431 return fwnode && !IS_ERR(fwnode->secondary);
4435 * set_primary_fwnode - Change the primary firmware node of a given device.
4436 * @dev: Device to handle.
4437 * @fwnode: New primary firmware node of the device.
4439 * Set the device's firmware node pointer to @fwnode, but if a secondary
4440 * firmware node of the device is present, preserve it.
4442 * Valid fwnode cases are:
4443 * - primary --> secondary --> -ENODEV
4444 * - primary --> NULL
4445 * - secondary --> -ENODEV
4448 void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
4450 struct device *parent = dev->parent;
4451 struct fwnode_handle *fn = dev->fwnode;
4454 if (fwnode_is_primary(fn))
4458 WARN_ON(fwnode->secondary);
4459 fwnode->secondary = fn;
4461 dev->fwnode = fwnode;
4463 if (fwnode_is_primary(fn)) {
4464 dev->fwnode = fn->secondary;
4465 /* Set fn->secondary = NULL, so fn remains the primary fwnode */
4466 if (!(parent && fn == parent->fwnode))
4467 fn->secondary = NULL;
4473 EXPORT_SYMBOL_GPL(set_primary_fwnode);
4476 * set_secondary_fwnode - Change the secondary firmware node of a given device.
4477 * @dev: Device to handle.
4478 * @fwnode: New secondary firmware node of the device.
4480 * If a primary firmware node of the device is present, set its secondary
4481 * pointer to @fwnode. Otherwise, set the device's firmware node pointer to
4484 void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
4487 fwnode->secondary = ERR_PTR(-ENODEV);
4489 if (fwnode_is_primary(dev->fwnode))
4490 dev->fwnode->secondary = fwnode;
4492 dev->fwnode = fwnode;
4494 EXPORT_SYMBOL_GPL(set_secondary_fwnode);
4497 * device_set_of_node_from_dev - reuse device-tree node of another device
4498 * @dev: device whose device-tree node is being set
4499 * @dev2: device whose device-tree node is being reused
4501 * Takes another reference to the new device-tree node after first dropping
4502 * any reference held to the old node.
4504 void device_set_of_node_from_dev(struct device *dev, const struct device *dev2)
4506 of_node_put(dev->of_node);
4507 dev->of_node = of_node_get(dev2->of_node);
4508 dev->of_node_reused = true;
4510 EXPORT_SYMBOL_GPL(device_set_of_node_from_dev);
4512 int device_match_name(struct device *dev, const void *name)
4514 return sysfs_streq(dev_name(dev), name);
4516 EXPORT_SYMBOL_GPL(device_match_name);
4518 int device_match_of_node(struct device *dev, const void *np)
4520 return dev->of_node == np;
4522 EXPORT_SYMBOL_GPL(device_match_of_node);
4524 int device_match_fwnode(struct device *dev, const void *fwnode)
4526 return dev_fwnode(dev) == fwnode;
4528 EXPORT_SYMBOL_GPL(device_match_fwnode);
4530 int device_match_devt(struct device *dev, const void *pdevt)
4532 return dev->devt == *(dev_t *)pdevt;
4534 EXPORT_SYMBOL_GPL(device_match_devt);
4536 int device_match_acpi_dev(struct device *dev, const void *adev)
4538 return ACPI_COMPANION(dev) == adev;
4540 EXPORT_SYMBOL(device_match_acpi_dev);
4542 int device_match_any(struct device *dev, const void *unused)
4546 EXPORT_SYMBOL_GPL(device_match_any);