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>
33 #include "power/power.h"
35 #ifdef CONFIG_SYSFS_DEPRECATED
36 #ifdef CONFIG_SYSFS_DEPRECATED_V2
37 long sysfs_deprecated = 1;
39 long sysfs_deprecated = 0;
41 static int __init sysfs_deprecated_setup(char *arg)
43 return kstrtol(arg, 10, &sysfs_deprecated);
45 early_param("sysfs.deprecated", sysfs_deprecated_setup);
48 /* Device links support. */
49 static LIST_HEAD(deferred_sync);
50 static unsigned int defer_sync_state_count = 1;
51 static DEFINE_MUTEX(fwnode_link_lock);
52 static bool fw_devlink_is_permissive(void);
55 * fwnode_link_add - Create a link between two fwnode_handles.
56 * @con: Consumer end of the link.
57 * @sup: Supplier end of the link.
59 * Create a fwnode link between fwnode handles @con and @sup. The fwnode link
60 * represents the detail that the firmware lists @sup fwnode as supplying a
63 * The driver core will use the fwnode link to create a device link between the
64 * two device objects corresponding to @con and @sup when they are created. The
65 * driver core will automatically delete the fwnode link between @con and @sup
68 * Attempts to create duplicate links between the same pair of fwnode handles
69 * are ignored and there is no reference counting.
71 int fwnode_link_add(struct fwnode_handle *con, struct fwnode_handle *sup)
73 struct fwnode_link *link;
76 mutex_lock(&fwnode_link_lock);
78 list_for_each_entry(link, &sup->consumers, s_hook)
79 if (link->consumer == con)
82 link = kzalloc(sizeof(*link), GFP_KERNEL);
89 INIT_LIST_HEAD(&link->s_hook);
91 INIT_LIST_HEAD(&link->c_hook);
93 list_add(&link->s_hook, &sup->consumers);
94 list_add(&link->c_hook, &con->suppliers);
96 mutex_unlock(&fwnode_link_lock);
102 * fwnode_links_purge_suppliers - Delete all supplier links of fwnode_handle.
103 * @fwnode: fwnode whose supplier links need to be deleted
105 * Deletes all supplier links connecting directly to @fwnode.
107 static void fwnode_links_purge_suppliers(struct fwnode_handle *fwnode)
109 struct fwnode_link *link, *tmp;
111 mutex_lock(&fwnode_link_lock);
112 list_for_each_entry_safe(link, tmp, &fwnode->suppliers, c_hook) {
113 list_del(&link->s_hook);
114 list_del(&link->c_hook);
117 mutex_unlock(&fwnode_link_lock);
121 * fwnode_links_purge_consumers - Delete all consumer links of fwnode_handle.
122 * @fwnode: fwnode whose consumer links need to be deleted
124 * Deletes all consumer links connecting directly to @fwnode.
126 static void fwnode_links_purge_consumers(struct fwnode_handle *fwnode)
128 struct fwnode_link *link, *tmp;
130 mutex_lock(&fwnode_link_lock);
131 list_for_each_entry_safe(link, tmp, &fwnode->consumers, s_hook) {
132 list_del(&link->s_hook);
133 list_del(&link->c_hook);
136 mutex_unlock(&fwnode_link_lock);
140 * fwnode_links_purge - Delete all links connected to a fwnode_handle.
141 * @fwnode: fwnode whose links needs to be deleted
143 * Deletes all links connecting directly to a fwnode.
145 void fwnode_links_purge(struct fwnode_handle *fwnode)
147 fwnode_links_purge_suppliers(fwnode);
148 fwnode_links_purge_consumers(fwnode);
152 static DEFINE_MUTEX(device_links_lock);
153 DEFINE_STATIC_SRCU(device_links_srcu);
155 static inline void device_links_write_lock(void)
157 mutex_lock(&device_links_lock);
160 static inline void device_links_write_unlock(void)
162 mutex_unlock(&device_links_lock);
165 int device_links_read_lock(void) __acquires(&device_links_srcu)
167 return srcu_read_lock(&device_links_srcu);
170 void device_links_read_unlock(int idx) __releases(&device_links_srcu)
172 srcu_read_unlock(&device_links_srcu, idx);
175 int device_links_read_lock_held(void)
177 return srcu_read_lock_held(&device_links_srcu);
179 #else /* !CONFIG_SRCU */
180 static DECLARE_RWSEM(device_links_lock);
182 static inline void device_links_write_lock(void)
184 down_write(&device_links_lock);
187 static inline void device_links_write_unlock(void)
189 up_write(&device_links_lock);
192 int device_links_read_lock(void)
194 down_read(&device_links_lock);
198 void device_links_read_unlock(int not_used)
200 up_read(&device_links_lock);
203 #ifdef CONFIG_DEBUG_LOCK_ALLOC
204 int device_links_read_lock_held(void)
206 return lockdep_is_held(&device_links_lock);
209 #endif /* !CONFIG_SRCU */
212 * device_is_dependent - Check if one device depends on another one
213 * @dev: Device to check dependencies for.
214 * @target: Device to check against.
216 * Check if @target depends on @dev or any device dependent on it (its child or
217 * its consumer etc). Return 1 if that is the case or 0 otherwise.
219 int device_is_dependent(struct device *dev, void *target)
221 struct device_link *link;
227 ret = device_for_each_child(dev, target, device_is_dependent);
231 list_for_each_entry(link, &dev->links.consumers, s_node) {
232 if ((link->flags & ~DL_FLAG_INFERRED) ==
233 (DL_FLAG_SYNC_STATE_ONLY | DL_FLAG_MANAGED))
236 if (link->consumer == target)
239 ret = device_is_dependent(link->consumer, target);
246 static void device_link_init_status(struct device_link *link,
247 struct device *consumer,
248 struct device *supplier)
250 switch (supplier->links.status) {
252 switch (consumer->links.status) {
255 * A consumer driver can create a link to a supplier
256 * that has not completed its probing yet as long as it
257 * knows that the supplier is already functional (for
258 * example, it has just acquired some resources from the
261 link->status = DL_STATE_CONSUMER_PROBE;
264 link->status = DL_STATE_DORMANT;
268 case DL_DEV_DRIVER_BOUND:
269 switch (consumer->links.status) {
271 link->status = DL_STATE_CONSUMER_PROBE;
273 case DL_DEV_DRIVER_BOUND:
274 link->status = DL_STATE_ACTIVE;
277 link->status = DL_STATE_AVAILABLE;
281 case DL_DEV_UNBINDING:
282 link->status = DL_STATE_SUPPLIER_UNBIND;
285 link->status = DL_STATE_DORMANT;
290 static int device_reorder_to_tail(struct device *dev, void *not_used)
292 struct device_link *link;
295 * Devices that have not been registered yet will be put to the ends
296 * of the lists during the registration, so skip them here.
298 if (device_is_registered(dev))
299 devices_kset_move_last(dev);
301 if (device_pm_initialized(dev))
302 device_pm_move_last(dev);
304 device_for_each_child(dev, NULL, device_reorder_to_tail);
305 list_for_each_entry(link, &dev->links.consumers, s_node) {
306 if ((link->flags & ~DL_FLAG_INFERRED) ==
307 (DL_FLAG_SYNC_STATE_ONLY | DL_FLAG_MANAGED))
309 device_reorder_to_tail(link->consumer, NULL);
316 * device_pm_move_to_tail - Move set of devices to the end of device lists
317 * @dev: Device to move
319 * This is a device_reorder_to_tail() wrapper taking the requisite locks.
321 * It moves the @dev along with all of its children and all of its consumers
322 * to the ends of the device_kset and dpm_list, recursively.
324 void device_pm_move_to_tail(struct device *dev)
328 idx = device_links_read_lock();
330 device_reorder_to_tail(dev, NULL);
332 device_links_read_unlock(idx);
335 #define to_devlink(dev) container_of((dev), struct device_link, link_dev)
337 static ssize_t status_show(struct device *dev,
338 struct device_attribute *attr, char *buf)
342 switch (to_devlink(dev)->status) {
344 output = "not tracked";
346 case DL_STATE_DORMANT:
349 case DL_STATE_AVAILABLE:
350 output = "available";
352 case DL_STATE_CONSUMER_PROBE:
353 output = "consumer probing";
355 case DL_STATE_ACTIVE:
358 case DL_STATE_SUPPLIER_UNBIND:
359 output = "supplier unbinding";
366 return sysfs_emit(buf, "%s\n", output);
368 static DEVICE_ATTR_RO(status);
370 static ssize_t auto_remove_on_show(struct device *dev,
371 struct device_attribute *attr, char *buf)
373 struct device_link *link = to_devlink(dev);
376 if (link->flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
377 output = "supplier unbind";
378 else if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER)
379 output = "consumer unbind";
383 return sysfs_emit(buf, "%s\n", output);
385 static DEVICE_ATTR_RO(auto_remove_on);
387 static ssize_t runtime_pm_show(struct device *dev,
388 struct device_attribute *attr, char *buf)
390 struct device_link *link = to_devlink(dev);
392 return sysfs_emit(buf, "%d\n", !!(link->flags & DL_FLAG_PM_RUNTIME));
394 static DEVICE_ATTR_RO(runtime_pm);
396 static ssize_t sync_state_only_show(struct device *dev,
397 struct device_attribute *attr, char *buf)
399 struct device_link *link = to_devlink(dev);
401 return sysfs_emit(buf, "%d\n",
402 !!(link->flags & DL_FLAG_SYNC_STATE_ONLY));
404 static DEVICE_ATTR_RO(sync_state_only);
406 static struct attribute *devlink_attrs[] = {
407 &dev_attr_status.attr,
408 &dev_attr_auto_remove_on.attr,
409 &dev_attr_runtime_pm.attr,
410 &dev_attr_sync_state_only.attr,
413 ATTRIBUTE_GROUPS(devlink);
415 static void device_link_free(struct device_link *link)
417 while (refcount_dec_not_one(&link->rpm_active))
418 pm_runtime_put(link->supplier);
420 put_device(link->consumer);
421 put_device(link->supplier);
426 static void __device_link_free_srcu(struct rcu_head *rhead)
428 device_link_free(container_of(rhead, struct device_link, rcu_head));
431 static void devlink_dev_release(struct device *dev)
433 struct device_link *link = to_devlink(dev);
435 call_srcu(&device_links_srcu, &link->rcu_head, __device_link_free_srcu);
438 static void devlink_dev_release(struct device *dev)
440 device_link_free(to_devlink(dev));
444 static struct class devlink_class = {
446 .owner = THIS_MODULE,
447 .dev_groups = devlink_groups,
448 .dev_release = devlink_dev_release,
451 static int devlink_add_symlinks(struct device *dev,
452 struct class_interface *class_intf)
456 struct device_link *link = to_devlink(dev);
457 struct device *sup = link->supplier;
458 struct device *con = link->consumer;
461 len = max(strlen(dev_name(sup)), strlen(dev_name(con)));
462 len += strlen("supplier:") + 1;
463 buf = kzalloc(len, GFP_KERNEL);
467 ret = sysfs_create_link(&link->link_dev.kobj, &sup->kobj, "supplier");
471 ret = sysfs_create_link(&link->link_dev.kobj, &con->kobj, "consumer");
475 snprintf(buf, len, "consumer:%s", dev_name(con));
476 ret = sysfs_create_link(&sup->kobj, &link->link_dev.kobj, buf);
480 snprintf(buf, len, "supplier:%s", dev_name(sup));
481 ret = sysfs_create_link(&con->kobj, &link->link_dev.kobj, buf);
488 snprintf(buf, len, "consumer:%s", dev_name(con));
489 sysfs_remove_link(&sup->kobj, buf);
491 sysfs_remove_link(&link->link_dev.kobj, "consumer");
493 sysfs_remove_link(&link->link_dev.kobj, "supplier");
499 static void devlink_remove_symlinks(struct device *dev,
500 struct class_interface *class_intf)
502 struct device_link *link = to_devlink(dev);
504 struct device *sup = link->supplier;
505 struct device *con = link->consumer;
508 sysfs_remove_link(&link->link_dev.kobj, "consumer");
509 sysfs_remove_link(&link->link_dev.kobj, "supplier");
511 len = max(strlen(dev_name(sup)), strlen(dev_name(con)));
512 len += strlen("supplier:") + 1;
513 buf = kzalloc(len, GFP_KERNEL);
515 WARN(1, "Unable to properly free device link symlinks!\n");
519 snprintf(buf, len, "supplier:%s", dev_name(sup));
520 sysfs_remove_link(&con->kobj, buf);
521 snprintf(buf, len, "consumer:%s", dev_name(con));
522 sysfs_remove_link(&sup->kobj, buf);
526 static struct class_interface devlink_class_intf = {
527 .class = &devlink_class,
528 .add_dev = devlink_add_symlinks,
529 .remove_dev = devlink_remove_symlinks,
532 static int __init devlink_class_init(void)
536 ret = class_register(&devlink_class);
540 ret = class_interface_register(&devlink_class_intf);
542 class_unregister(&devlink_class);
546 postcore_initcall(devlink_class_init);
548 #define DL_MANAGED_LINK_FLAGS (DL_FLAG_AUTOREMOVE_CONSUMER | \
549 DL_FLAG_AUTOREMOVE_SUPPLIER | \
550 DL_FLAG_AUTOPROBE_CONSUMER | \
551 DL_FLAG_SYNC_STATE_ONLY | \
554 #define DL_ADD_VALID_FLAGS (DL_MANAGED_LINK_FLAGS | DL_FLAG_STATELESS | \
555 DL_FLAG_PM_RUNTIME | DL_FLAG_RPM_ACTIVE)
558 * device_link_add - Create a link between two devices.
559 * @consumer: Consumer end of the link.
560 * @supplier: Supplier end of the link.
561 * @flags: Link flags.
563 * The caller is responsible for the proper synchronization of the link creation
564 * with runtime PM. First, setting the DL_FLAG_PM_RUNTIME flag will cause the
565 * runtime PM framework to take the link into account. Second, if the
566 * DL_FLAG_RPM_ACTIVE flag is set in addition to it, the supplier devices will
567 * be forced into the active meta state and reference-counted upon the creation
568 * of the link. If DL_FLAG_PM_RUNTIME is not set, DL_FLAG_RPM_ACTIVE will be
571 * If DL_FLAG_STATELESS is set in @flags, the caller of this function is
572 * expected to release the link returned by it directly with the help of either
573 * device_link_del() or device_link_remove().
575 * If that flag is not set, however, the caller of this function is handing the
576 * management of the link over to the driver core entirely and its return value
577 * can only be used to check whether or not the link is present. In that case,
578 * the DL_FLAG_AUTOREMOVE_CONSUMER and DL_FLAG_AUTOREMOVE_SUPPLIER device link
579 * flags can be used to indicate to the driver core when the link can be safely
580 * deleted. Namely, setting one of them in @flags indicates to the driver core
581 * that the link is not going to be used (by the given caller of this function)
582 * after unbinding the consumer or supplier driver, respectively, from its
583 * device, so the link can be deleted at that point. If none of them is set,
584 * the link will be maintained until one of the devices pointed to by it (either
585 * the consumer or the supplier) is unregistered.
587 * Also, if DL_FLAG_STATELESS, DL_FLAG_AUTOREMOVE_CONSUMER and
588 * DL_FLAG_AUTOREMOVE_SUPPLIER are not set in @flags (that is, a persistent
589 * managed device link is being added), the DL_FLAG_AUTOPROBE_CONSUMER flag can
590 * be used to request the driver core to automatically probe for a consumer
591 * driver after successfully binding a driver to the supplier device.
593 * The combination of DL_FLAG_STATELESS and one of DL_FLAG_AUTOREMOVE_CONSUMER,
594 * DL_FLAG_AUTOREMOVE_SUPPLIER, or DL_FLAG_AUTOPROBE_CONSUMER set in @flags at
595 * the same time is invalid and will cause NULL to be returned upfront.
596 * However, if a device link between the given @consumer and @supplier pair
597 * exists already when this function is called for them, the existing link will
598 * be returned regardless of its current type and status (the link's flags may
599 * be modified then). The caller of this function is then expected to treat
600 * the link as though it has just been created, so (in particular) if
601 * DL_FLAG_STATELESS was passed in @flags, the link needs to be released
602 * explicitly when not needed any more (as stated above).
604 * A side effect of the link creation is re-ordering of dpm_list and the
605 * devices_kset list by moving the consumer device and all devices depending
606 * on it to the ends of these lists (that does not happen to devices that have
607 * not been registered when this function is called).
609 * The supplier device is required to be registered when this function is called
610 * and NULL will be returned if that is not the case. The consumer device need
611 * not be registered, however.
613 struct device_link *device_link_add(struct device *consumer,
614 struct device *supplier, u32 flags)
616 struct device_link *link;
618 if (!consumer || !supplier || flags & ~DL_ADD_VALID_FLAGS ||
619 (flags & DL_FLAG_STATELESS && flags & DL_MANAGED_LINK_FLAGS) ||
620 (flags & DL_FLAG_SYNC_STATE_ONLY &&
621 (flags & ~DL_FLAG_INFERRED) != DL_FLAG_SYNC_STATE_ONLY) ||
622 (flags & DL_FLAG_AUTOPROBE_CONSUMER &&
623 flags & (DL_FLAG_AUTOREMOVE_CONSUMER |
624 DL_FLAG_AUTOREMOVE_SUPPLIER)))
627 if (flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) {
628 if (pm_runtime_get_sync(supplier) < 0) {
629 pm_runtime_put_noidle(supplier);
634 if (!(flags & DL_FLAG_STATELESS))
635 flags |= DL_FLAG_MANAGED;
637 device_links_write_lock();
641 * If the supplier has not been fully registered yet or there is a
642 * reverse (non-SYNC_STATE_ONLY) dependency between the consumer and
643 * the supplier already in the graph, return NULL. If the link is a
644 * SYNC_STATE_ONLY link, we don't check for reverse dependencies
645 * because it only affects sync_state() callbacks.
647 if (!device_pm_initialized(supplier)
648 || (!(flags & DL_FLAG_SYNC_STATE_ONLY) &&
649 device_is_dependent(consumer, supplier))) {
655 * SYNC_STATE_ONLY links are useless once a consumer device has probed.
656 * So, only create it if the consumer hasn't probed yet.
658 if (flags & DL_FLAG_SYNC_STATE_ONLY &&
659 consumer->links.status != DL_DEV_NO_DRIVER &&
660 consumer->links.status != DL_DEV_PROBING) {
666 * DL_FLAG_AUTOREMOVE_SUPPLIER indicates that the link will be needed
667 * longer than for DL_FLAG_AUTOREMOVE_CONSUMER and setting them both
668 * together doesn't make sense, so prefer DL_FLAG_AUTOREMOVE_SUPPLIER.
670 if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
671 flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER;
673 list_for_each_entry(link, &supplier->links.consumers, s_node) {
674 if (link->consumer != consumer)
677 if (link->flags & DL_FLAG_INFERRED &&
678 !(flags & DL_FLAG_INFERRED))
679 link->flags &= ~DL_FLAG_INFERRED;
681 if (flags & DL_FLAG_PM_RUNTIME) {
682 if (!(link->flags & DL_FLAG_PM_RUNTIME)) {
683 pm_runtime_new_link(consumer);
684 link->flags |= DL_FLAG_PM_RUNTIME;
686 if (flags & DL_FLAG_RPM_ACTIVE)
687 refcount_inc(&link->rpm_active);
690 if (flags & DL_FLAG_STATELESS) {
691 kref_get(&link->kref);
692 if (link->flags & DL_FLAG_SYNC_STATE_ONLY &&
693 !(link->flags & DL_FLAG_STATELESS)) {
694 link->flags |= DL_FLAG_STATELESS;
697 link->flags |= DL_FLAG_STATELESS;
703 * If the life time of the link following from the new flags is
704 * longer than indicated by the flags of the existing link,
705 * update the existing link to stay around longer.
707 if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER) {
708 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER) {
709 link->flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER;
710 link->flags |= DL_FLAG_AUTOREMOVE_SUPPLIER;
712 } else if (!(flags & DL_FLAG_AUTOREMOVE_CONSUMER)) {
713 link->flags &= ~(DL_FLAG_AUTOREMOVE_CONSUMER |
714 DL_FLAG_AUTOREMOVE_SUPPLIER);
716 if (!(link->flags & DL_FLAG_MANAGED)) {
717 kref_get(&link->kref);
718 link->flags |= DL_FLAG_MANAGED;
719 device_link_init_status(link, consumer, supplier);
721 if (link->flags & DL_FLAG_SYNC_STATE_ONLY &&
722 !(flags & DL_FLAG_SYNC_STATE_ONLY)) {
723 link->flags &= ~DL_FLAG_SYNC_STATE_ONLY;
730 link = kzalloc(sizeof(*link), GFP_KERNEL);
734 refcount_set(&link->rpm_active, 1);
736 get_device(supplier);
737 link->supplier = supplier;
738 INIT_LIST_HEAD(&link->s_node);
739 get_device(consumer);
740 link->consumer = consumer;
741 INIT_LIST_HEAD(&link->c_node);
743 kref_init(&link->kref);
745 link->link_dev.class = &devlink_class;
746 device_set_pm_not_required(&link->link_dev);
747 dev_set_name(&link->link_dev, "%s--%s",
748 dev_name(supplier), dev_name(consumer));
749 if (device_register(&link->link_dev)) {
750 put_device(consumer);
751 put_device(supplier);
757 if (flags & DL_FLAG_PM_RUNTIME) {
758 if (flags & DL_FLAG_RPM_ACTIVE)
759 refcount_inc(&link->rpm_active);
761 pm_runtime_new_link(consumer);
764 /* Determine the initial link state. */
765 if (flags & DL_FLAG_STATELESS)
766 link->status = DL_STATE_NONE;
768 device_link_init_status(link, consumer, supplier);
771 * Some callers expect the link creation during consumer driver probe to
772 * resume the supplier even without DL_FLAG_RPM_ACTIVE.
774 if (link->status == DL_STATE_CONSUMER_PROBE &&
775 flags & DL_FLAG_PM_RUNTIME)
776 pm_runtime_resume(supplier);
778 list_add_tail_rcu(&link->s_node, &supplier->links.consumers);
779 list_add_tail_rcu(&link->c_node, &consumer->links.suppliers);
781 if (flags & DL_FLAG_SYNC_STATE_ONLY) {
783 "Linked as a sync state only consumer to %s\n",
790 * Move the consumer and all of the devices depending on it to the end
791 * of dpm_list and the devices_kset list.
793 * It is necessary to hold dpm_list locked throughout all that or else
794 * we may end up suspending with a wrong ordering of it.
796 device_reorder_to_tail(consumer, NULL);
798 dev_dbg(consumer, "Linked as a consumer to %s\n", dev_name(supplier));
802 device_links_write_unlock();
804 if ((flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) && !link)
805 pm_runtime_put(supplier);
809 EXPORT_SYMBOL_GPL(device_link_add);
812 static void __device_link_del(struct kref *kref)
814 struct device_link *link = container_of(kref, struct device_link, kref);
816 dev_dbg(link->consumer, "Dropping the link to %s\n",
817 dev_name(link->supplier));
819 pm_runtime_drop_link(link);
821 list_del_rcu(&link->s_node);
822 list_del_rcu(&link->c_node);
823 device_unregister(&link->link_dev);
825 #else /* !CONFIG_SRCU */
826 static void __device_link_del(struct kref *kref)
828 struct device_link *link = container_of(kref, struct device_link, kref);
830 dev_info(link->consumer, "Dropping the link to %s\n",
831 dev_name(link->supplier));
833 pm_runtime_drop_link(link);
835 list_del(&link->s_node);
836 list_del(&link->c_node);
837 device_unregister(&link->link_dev);
839 #endif /* !CONFIG_SRCU */
841 static void device_link_put_kref(struct device_link *link)
843 if (link->flags & DL_FLAG_STATELESS)
844 kref_put(&link->kref, __device_link_del);
846 WARN(1, "Unable to drop a managed device link reference\n");
850 * device_link_del - Delete a stateless link between two devices.
851 * @link: Device link to delete.
853 * The caller must ensure proper synchronization of this function with runtime
854 * PM. If the link was added multiple times, it needs to be deleted as often.
855 * Care is required for hotplugged devices: Their links are purged on removal
856 * and calling device_link_del() is then no longer allowed.
858 void device_link_del(struct device_link *link)
860 device_links_write_lock();
861 device_link_put_kref(link);
862 device_links_write_unlock();
864 EXPORT_SYMBOL_GPL(device_link_del);
867 * device_link_remove - Delete a stateless link between two devices.
868 * @consumer: Consumer end of the link.
869 * @supplier: Supplier end of the link.
871 * The caller must ensure proper synchronization of this function with runtime
874 void device_link_remove(void *consumer, struct device *supplier)
876 struct device_link *link;
878 if (WARN_ON(consumer == supplier))
881 device_links_write_lock();
883 list_for_each_entry(link, &supplier->links.consumers, s_node) {
884 if (link->consumer == consumer) {
885 device_link_put_kref(link);
890 device_links_write_unlock();
892 EXPORT_SYMBOL_GPL(device_link_remove);
894 static void device_links_missing_supplier(struct device *dev)
896 struct device_link *link;
898 list_for_each_entry(link, &dev->links.suppliers, c_node) {
899 if (link->status != DL_STATE_CONSUMER_PROBE)
902 if (link->supplier->links.status == DL_DEV_DRIVER_BOUND) {
903 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
905 WARN_ON(!(link->flags & DL_FLAG_SYNC_STATE_ONLY));
906 WRITE_ONCE(link->status, DL_STATE_DORMANT);
912 * device_links_check_suppliers - Check presence of supplier drivers.
913 * @dev: Consumer device.
915 * Check links from this device to any suppliers. Walk the list of the device's
916 * links to suppliers and see if all of them are available. If not, simply
917 * return -EPROBE_DEFER.
919 * We need to guarantee that the supplier will not go away after the check has
920 * been positive here. It only can go away in __device_release_driver() and
921 * that function checks the device's links to consumers. This means we need to
922 * mark the link as "consumer probe in progress" to make the supplier removal
923 * wait for us to complete (or bad things may happen).
925 * Links without the DL_FLAG_MANAGED flag set are ignored.
927 int device_links_check_suppliers(struct device *dev)
929 struct device_link *link;
933 * Device waiting for supplier to become available is not allowed to
936 mutex_lock(&fwnode_link_lock);
937 if (dev->fwnode && !list_empty(&dev->fwnode->suppliers) &&
938 !fw_devlink_is_permissive()) {
939 dev_dbg(dev, "probe deferral - wait for supplier %pfwP\n",
940 list_first_entry(&dev->fwnode->suppliers,
943 mutex_unlock(&fwnode_link_lock);
944 return -EPROBE_DEFER;
946 mutex_unlock(&fwnode_link_lock);
948 device_links_write_lock();
950 list_for_each_entry(link, &dev->links.suppliers, c_node) {
951 if (!(link->flags & DL_FLAG_MANAGED))
954 if (link->status != DL_STATE_AVAILABLE &&
955 !(link->flags & DL_FLAG_SYNC_STATE_ONLY)) {
956 device_links_missing_supplier(dev);
957 dev_dbg(dev, "probe deferral - supplier %s not ready\n",
958 dev_name(link->supplier));
962 WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE);
964 dev->links.status = DL_DEV_PROBING;
966 device_links_write_unlock();
971 * __device_links_queue_sync_state - Queue a device for sync_state() callback
972 * @dev: Device to call sync_state() on
973 * @list: List head to queue the @dev on
975 * Queues a device for a sync_state() callback when the device links write lock
976 * isn't held. This allows the sync_state() execution flow to use device links
977 * APIs. The caller must ensure this function is called with
978 * device_links_write_lock() held.
980 * This function does a get_device() to make sure the device is not freed while
983 * So the caller must also ensure that device_links_flush_sync_list() is called
984 * as soon as the caller releases device_links_write_lock(). This is necessary
985 * to make sure the sync_state() is called in a timely fashion and the
986 * put_device() is called on this device.
988 static void __device_links_queue_sync_state(struct device *dev,
989 struct list_head *list)
991 struct device_link *link;
993 if (!dev_has_sync_state(dev))
995 if (dev->state_synced)
998 list_for_each_entry(link, &dev->links.consumers, s_node) {
999 if (!(link->flags & DL_FLAG_MANAGED))
1001 if (link->status != DL_STATE_ACTIVE)
1006 * Set the flag here to avoid adding the same device to a list more
1007 * than once. This can happen if new consumers get added to the device
1008 * and probed before the list is flushed.
1010 dev->state_synced = true;
1012 if (WARN_ON(!list_empty(&dev->links.defer_sync)))
1016 list_add_tail(&dev->links.defer_sync, list);
1020 * device_links_flush_sync_list - Call sync_state() on a list of devices
1021 * @list: List of devices to call sync_state() on
1022 * @dont_lock_dev: Device for which lock is already held by the caller
1024 * Calls sync_state() on all the devices that have been queued for it. This
1025 * function is used in conjunction with __device_links_queue_sync_state(). The
1026 * @dont_lock_dev parameter is useful when this function is called from a
1027 * context where a device lock is already held.
1029 static void device_links_flush_sync_list(struct list_head *list,
1030 struct device *dont_lock_dev)
1032 struct device *dev, *tmp;
1034 list_for_each_entry_safe(dev, tmp, list, links.defer_sync) {
1035 list_del_init(&dev->links.defer_sync);
1037 if (dev != dont_lock_dev)
1040 if (dev->bus->sync_state)
1041 dev->bus->sync_state(dev);
1042 else if (dev->driver && dev->driver->sync_state)
1043 dev->driver->sync_state(dev);
1045 if (dev != dont_lock_dev)
1052 void device_links_supplier_sync_state_pause(void)
1054 device_links_write_lock();
1055 defer_sync_state_count++;
1056 device_links_write_unlock();
1059 void device_links_supplier_sync_state_resume(void)
1061 struct device *dev, *tmp;
1062 LIST_HEAD(sync_list);
1064 device_links_write_lock();
1065 if (!defer_sync_state_count) {
1066 WARN(true, "Unmatched sync_state pause/resume!");
1069 defer_sync_state_count--;
1070 if (defer_sync_state_count)
1073 list_for_each_entry_safe(dev, tmp, &deferred_sync, links.defer_sync) {
1075 * Delete from deferred_sync list before queuing it to
1076 * sync_list because defer_sync is used for both lists.
1078 list_del_init(&dev->links.defer_sync);
1079 __device_links_queue_sync_state(dev, &sync_list);
1082 device_links_write_unlock();
1084 device_links_flush_sync_list(&sync_list, NULL);
1087 static int sync_state_resume_initcall(void)
1089 device_links_supplier_sync_state_resume();
1092 late_initcall(sync_state_resume_initcall);
1094 static void __device_links_supplier_defer_sync(struct device *sup)
1096 if (list_empty(&sup->links.defer_sync) && dev_has_sync_state(sup))
1097 list_add_tail(&sup->links.defer_sync, &deferred_sync);
1100 static void device_link_drop_managed(struct device_link *link)
1102 link->flags &= ~DL_FLAG_MANAGED;
1103 WRITE_ONCE(link->status, DL_STATE_NONE);
1104 kref_put(&link->kref, __device_link_del);
1107 static ssize_t waiting_for_supplier_show(struct device *dev,
1108 struct device_attribute *attr,
1114 val = !list_empty(&dev->fwnode->suppliers);
1116 return sysfs_emit(buf, "%u\n", val);
1118 static DEVICE_ATTR_RO(waiting_for_supplier);
1121 * device_links_driver_bound - Update device links after probing its driver.
1122 * @dev: Device to update the links for.
1124 * The probe has been successful, so update links from this device to any
1125 * consumers by changing their status to "available".
1127 * Also change the status of @dev's links to suppliers to "active".
1129 * Links without the DL_FLAG_MANAGED flag set are ignored.
1131 void device_links_driver_bound(struct device *dev)
1133 struct device_link *link, *ln;
1134 LIST_HEAD(sync_list);
1137 * If a device probes successfully, it's expected to have created all
1138 * the device links it needs to or make new device links as it needs
1139 * them. So, it no longer needs to wait on any suppliers.
1141 if (dev->fwnode && dev->fwnode->dev == dev)
1142 fwnode_links_purge_suppliers(dev->fwnode);
1143 device_remove_file(dev, &dev_attr_waiting_for_supplier);
1145 device_links_write_lock();
1147 list_for_each_entry(link, &dev->links.consumers, s_node) {
1148 if (!(link->flags & DL_FLAG_MANAGED))
1152 * Links created during consumer probe may be in the "consumer
1153 * probe" state to start with if the supplier is still probing
1154 * when they are created and they may become "active" if the
1155 * consumer probe returns first. Skip them here.
1157 if (link->status == DL_STATE_CONSUMER_PROBE ||
1158 link->status == DL_STATE_ACTIVE)
1161 WARN_ON(link->status != DL_STATE_DORMANT);
1162 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
1164 if (link->flags & DL_FLAG_AUTOPROBE_CONSUMER)
1165 driver_deferred_probe_add(link->consumer);
1168 if (defer_sync_state_count)
1169 __device_links_supplier_defer_sync(dev);
1171 __device_links_queue_sync_state(dev, &sync_list);
1173 list_for_each_entry_safe(link, ln, &dev->links.suppliers, c_node) {
1174 struct device *supplier;
1176 if (!(link->flags & DL_FLAG_MANAGED))
1179 supplier = link->supplier;
1180 if (link->flags & DL_FLAG_SYNC_STATE_ONLY) {
1182 * When DL_FLAG_SYNC_STATE_ONLY is set, it means no
1183 * other DL_MANAGED_LINK_FLAGS have been set. So, it's
1184 * save to drop the managed link completely.
1186 device_link_drop_managed(link);
1188 WARN_ON(link->status != DL_STATE_CONSUMER_PROBE);
1189 WRITE_ONCE(link->status, DL_STATE_ACTIVE);
1193 * This needs to be done even for the deleted
1194 * DL_FLAG_SYNC_STATE_ONLY device link in case it was the last
1195 * device link that was preventing the supplier from getting a
1196 * sync_state() call.
1198 if (defer_sync_state_count)
1199 __device_links_supplier_defer_sync(supplier);
1201 __device_links_queue_sync_state(supplier, &sync_list);
1204 dev->links.status = DL_DEV_DRIVER_BOUND;
1206 device_links_write_unlock();
1208 device_links_flush_sync_list(&sync_list, dev);
1212 * __device_links_no_driver - Update links of a device without a driver.
1213 * @dev: Device without a drvier.
1215 * Delete all non-persistent links from this device to any suppliers.
1217 * Persistent links stay around, but their status is changed to "available",
1218 * unless they already are in the "supplier unbind in progress" state in which
1219 * case they need not be updated.
1221 * Links without the DL_FLAG_MANAGED flag set are ignored.
1223 static void __device_links_no_driver(struct device *dev)
1225 struct device_link *link, *ln;
1227 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
1228 if (!(link->flags & DL_FLAG_MANAGED))
1231 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER) {
1232 device_link_drop_managed(link);
1236 if (link->status != DL_STATE_CONSUMER_PROBE &&
1237 link->status != DL_STATE_ACTIVE)
1240 if (link->supplier->links.status == DL_DEV_DRIVER_BOUND) {
1241 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
1243 WARN_ON(!(link->flags & DL_FLAG_SYNC_STATE_ONLY));
1244 WRITE_ONCE(link->status, DL_STATE_DORMANT);
1248 dev->links.status = DL_DEV_NO_DRIVER;
1252 * device_links_no_driver - Update links after failing driver probe.
1253 * @dev: Device whose driver has just failed to probe.
1255 * Clean up leftover links to consumers for @dev and invoke
1256 * %__device_links_no_driver() to update links to suppliers for it as
1259 * Links without the DL_FLAG_MANAGED flag set are ignored.
1261 void device_links_no_driver(struct device *dev)
1263 struct device_link *link;
1265 device_links_write_lock();
1267 list_for_each_entry(link, &dev->links.consumers, s_node) {
1268 if (!(link->flags & DL_FLAG_MANAGED))
1272 * The probe has failed, so if the status of the link is
1273 * "consumer probe" or "active", it must have been added by
1274 * a probing consumer while this device was still probing.
1275 * Change its state to "dormant", as it represents a valid
1276 * relationship, but it is not functionally meaningful.
1278 if (link->status == DL_STATE_CONSUMER_PROBE ||
1279 link->status == DL_STATE_ACTIVE)
1280 WRITE_ONCE(link->status, DL_STATE_DORMANT);
1283 __device_links_no_driver(dev);
1285 device_links_write_unlock();
1289 * device_links_driver_cleanup - Update links after driver removal.
1290 * @dev: Device whose driver has just gone away.
1292 * Update links to consumers for @dev by changing their status to "dormant" and
1293 * invoke %__device_links_no_driver() to update links to suppliers for it as
1296 * Links without the DL_FLAG_MANAGED flag set are ignored.
1298 void device_links_driver_cleanup(struct device *dev)
1300 struct device_link *link, *ln;
1302 device_links_write_lock();
1304 list_for_each_entry_safe(link, ln, &dev->links.consumers, s_node) {
1305 if (!(link->flags & DL_FLAG_MANAGED))
1308 WARN_ON(link->flags & DL_FLAG_AUTOREMOVE_CONSUMER);
1309 WARN_ON(link->status != DL_STATE_SUPPLIER_UNBIND);
1312 * autoremove the links between this @dev and its consumer
1313 * devices that are not active, i.e. where the link state
1314 * has moved to DL_STATE_SUPPLIER_UNBIND.
1316 if (link->status == DL_STATE_SUPPLIER_UNBIND &&
1317 link->flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
1318 device_link_drop_managed(link);
1320 WRITE_ONCE(link->status, DL_STATE_DORMANT);
1323 list_del_init(&dev->links.defer_sync);
1324 __device_links_no_driver(dev);
1326 device_links_write_unlock();
1330 * device_links_busy - Check if there are any busy links to consumers.
1331 * @dev: Device to check.
1333 * Check each consumer of the device and return 'true' if its link's status
1334 * is one of "consumer probe" or "active" (meaning that the given consumer is
1335 * probing right now or its driver is present). Otherwise, change the link
1336 * state to "supplier unbind" to prevent the consumer from being probed
1337 * successfully going forward.
1339 * Return 'false' if there are no probing or active consumers.
1341 * Links without the DL_FLAG_MANAGED flag set are ignored.
1343 bool device_links_busy(struct device *dev)
1345 struct device_link *link;
1348 device_links_write_lock();
1350 list_for_each_entry(link, &dev->links.consumers, s_node) {
1351 if (!(link->flags & DL_FLAG_MANAGED))
1354 if (link->status == DL_STATE_CONSUMER_PROBE
1355 || link->status == DL_STATE_ACTIVE) {
1359 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
1362 dev->links.status = DL_DEV_UNBINDING;
1364 device_links_write_unlock();
1369 * device_links_unbind_consumers - Force unbind consumers of the given device.
1370 * @dev: Device to unbind the consumers of.
1372 * Walk the list of links to consumers for @dev and if any of them is in the
1373 * "consumer probe" state, wait for all device probes in progress to complete
1376 * If that's not the case, change the status of the link to "supplier unbind"
1377 * and check if the link was in the "active" state. If so, force the consumer
1378 * driver to unbind and start over (the consumer will not re-probe as we have
1379 * changed the state of the link already).
1381 * Links without the DL_FLAG_MANAGED flag set are ignored.
1383 void device_links_unbind_consumers(struct device *dev)
1385 struct device_link *link;
1388 device_links_write_lock();
1390 list_for_each_entry(link, &dev->links.consumers, s_node) {
1391 enum device_link_state status;
1393 if (!(link->flags & DL_FLAG_MANAGED) ||
1394 link->flags & DL_FLAG_SYNC_STATE_ONLY)
1397 status = link->status;
1398 if (status == DL_STATE_CONSUMER_PROBE) {
1399 device_links_write_unlock();
1401 wait_for_device_probe();
1404 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
1405 if (status == DL_STATE_ACTIVE) {
1406 struct device *consumer = link->consumer;
1408 get_device(consumer);
1410 device_links_write_unlock();
1412 device_release_driver_internal(consumer, NULL,
1414 put_device(consumer);
1419 device_links_write_unlock();
1423 * device_links_purge - Delete existing links to other devices.
1424 * @dev: Target device.
1426 static void device_links_purge(struct device *dev)
1428 struct device_link *link, *ln;
1430 if (dev->class == &devlink_class)
1434 * Delete all of the remaining links from this device to any other
1435 * devices (either consumers or suppliers).
1437 device_links_write_lock();
1439 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
1440 WARN_ON(link->status == DL_STATE_ACTIVE);
1441 __device_link_del(&link->kref);
1444 list_for_each_entry_safe_reverse(link, ln, &dev->links.consumers, s_node) {
1445 WARN_ON(link->status != DL_STATE_DORMANT &&
1446 link->status != DL_STATE_NONE);
1447 __device_link_del(&link->kref);
1450 device_links_write_unlock();
1453 #define FW_DEVLINK_FLAGS_PERMISSIVE (DL_FLAG_INFERRED | \
1454 DL_FLAG_SYNC_STATE_ONLY)
1455 #define FW_DEVLINK_FLAGS_ON (DL_FLAG_INFERRED | \
1456 DL_FLAG_AUTOPROBE_CONSUMER)
1457 #define FW_DEVLINK_FLAGS_RPM (FW_DEVLINK_FLAGS_ON | \
1460 static u32 fw_devlink_flags = FW_DEVLINK_FLAGS_PERMISSIVE;
1461 static int __init fw_devlink_setup(char *arg)
1466 if (strcmp(arg, "off") == 0) {
1467 fw_devlink_flags = 0;
1468 } else if (strcmp(arg, "permissive") == 0) {
1469 fw_devlink_flags = FW_DEVLINK_FLAGS_PERMISSIVE;
1470 } else if (strcmp(arg, "on") == 0) {
1471 fw_devlink_flags = FW_DEVLINK_FLAGS_ON;
1472 } else if (strcmp(arg, "rpm") == 0) {
1473 fw_devlink_flags = FW_DEVLINK_FLAGS_RPM;
1477 early_param("fw_devlink", fw_devlink_setup);
1479 u32 fw_devlink_get_flags(void)
1481 return fw_devlink_flags;
1484 static bool fw_devlink_is_permissive(void)
1486 return fw_devlink_flags == FW_DEVLINK_FLAGS_PERMISSIVE;
1489 static void fw_devlink_parse_fwnode(struct fwnode_handle *fwnode)
1491 if (fwnode->flags & FWNODE_FLAG_LINKS_ADDED)
1494 fwnode_call_int_op(fwnode, add_links);
1495 fwnode->flags |= FWNODE_FLAG_LINKS_ADDED;
1498 static void fw_devlink_parse_fwtree(struct fwnode_handle *fwnode)
1500 struct fwnode_handle *child = NULL;
1502 fw_devlink_parse_fwnode(fwnode);
1504 while ((child = fwnode_get_next_available_child_node(fwnode, child)))
1505 fw_devlink_parse_fwtree(child);
1509 * fw_devlink_create_devlink - Create a device link from a consumer to fwnode
1510 * @con - Consumer device for the device link
1511 * @sup_handle - fwnode handle of supplier
1513 * This function will try to create a device link between the consumer device
1514 * @con and the supplier device represented by @sup_handle.
1516 * The supplier has to be provided as a fwnode because incorrect cycles in
1517 * fwnode links can sometimes cause the supplier device to never be created.
1518 * This function detects such cases and returns an error if it cannot create a
1519 * device link from the consumer to a missing supplier.
1522 * 0 on successfully creating a device link
1523 * -EINVAL if the device link cannot be created as expected
1524 * -EAGAIN if the device link cannot be created right now, but it may be
1525 * possible to do that in the future
1527 static int fw_devlink_create_devlink(struct device *con,
1528 struct fwnode_handle *sup_handle, u32 flags)
1530 struct device *sup_dev;
1533 sup_dev = get_dev_from_fwnode(sup_handle);
1536 * If this fails, it is due to cycles in device links. Just
1537 * give up on this link and treat it as invalid.
1539 if (!device_link_add(con, sup_dev, flags))
1546 * DL_FLAG_SYNC_STATE_ONLY doesn't block probing and supports
1547 * cycles. So cycle detection isn't necessary and shouldn't be
1550 if (flags & DL_FLAG_SYNC_STATE_ONLY)
1554 * If we can't find the supplier device from its fwnode, it might be
1555 * due to a cyclic dependency between fwnodes. Some of these cycles can
1556 * be broken by applying logic. Check for these types of cycles and
1557 * break them so that devices in the cycle probe properly.
1559 * If the supplier's parent is dependent on the consumer, then
1560 * the consumer-supplier dependency is a false dependency. So,
1561 * treat it as an invalid link.
1563 sup_dev = fwnode_get_next_parent_dev(sup_handle);
1564 if (sup_dev && device_is_dependent(con, sup_dev)) {
1565 dev_dbg(con, "Not linking to %pfwP - False link\n",
1570 * Can't check for cycles or no cycles. So let's try
1577 put_device(sup_dev);
1582 * __fw_devlink_link_to_consumers - Create device links to consumers of a device
1583 * @dev - Device that needs to be linked to its consumers
1585 * This function looks at all the consumer fwnodes of @dev and creates device
1586 * links between the consumer device and @dev (supplier).
1588 * If the consumer device has not been added yet, then this function creates a
1589 * SYNC_STATE_ONLY link between @dev (supplier) and the closest ancestor device
1590 * of the consumer fwnode. This is necessary to make sure @dev doesn't get a
1591 * sync_state() callback before the real consumer device gets to be added and
1594 * Once device links are created from the real consumer to @dev (supplier), the
1595 * fwnode links are deleted.
1597 static void __fw_devlink_link_to_consumers(struct device *dev)
1599 struct fwnode_handle *fwnode = dev->fwnode;
1600 struct fwnode_link *link, *tmp;
1602 list_for_each_entry_safe(link, tmp, &fwnode->consumers, s_hook) {
1603 u32 dl_flags = fw_devlink_get_flags();
1604 struct device *con_dev;
1605 bool own_link = true;
1608 con_dev = get_dev_from_fwnode(link->consumer);
1610 * If consumer device is not available yet, make a "proxy"
1611 * SYNC_STATE_ONLY link from the consumer's parent device to
1612 * the supplier device. This is necessary to make sure the
1613 * supplier doesn't get a sync_state() callback before the real
1614 * consumer can create a device link to the supplier.
1616 * This proxy link step is needed to handle the case where the
1617 * consumer's parent device is added before the supplier.
1620 con_dev = fwnode_get_next_parent_dev(link->consumer);
1622 * However, if the consumer's parent device is also the
1623 * parent of the supplier, don't create a
1624 * consumer-supplier link from the parent to its child
1625 * device. Such a dependency is impossible.
1628 fwnode_is_ancestor_of(con_dev->fwnode, fwnode)) {
1629 put_device(con_dev);
1633 dl_flags = FW_DEVLINK_FLAGS_PERMISSIVE;
1640 ret = fw_devlink_create_devlink(con_dev, fwnode, dl_flags);
1641 put_device(con_dev);
1642 if (!own_link || ret == -EAGAIN)
1645 list_del(&link->s_hook);
1646 list_del(&link->c_hook);
1652 * __fw_devlink_link_to_suppliers - Create device links to suppliers of a device
1653 * @dev - The consumer device that needs to be linked to its suppliers
1654 * @fwnode - Root of the fwnode tree that is used to create device links
1656 * This function looks at all the supplier fwnodes of fwnode tree rooted at
1657 * @fwnode and creates device links between @dev (consumer) and all the
1658 * supplier devices of the entire fwnode tree at @fwnode.
1660 * The function creates normal (non-SYNC_STATE_ONLY) device links between @dev
1661 * and the real suppliers of @dev. Once these device links are created, the
1662 * fwnode links are deleted. When such device links are successfully created,
1663 * this function is called recursively on those supplier devices. This is
1664 * needed to detect and break some invalid cycles in fwnode links. See
1665 * fw_devlink_create_devlink() for more details.
1667 * In addition, it also looks at all the suppliers of the entire fwnode tree
1668 * because some of the child devices of @dev that have not been added yet
1669 * (because @dev hasn't probed) might already have their suppliers added to
1670 * driver core. So, this function creates SYNC_STATE_ONLY device links between
1671 * @dev (consumer) and these suppliers to make sure they don't execute their
1672 * sync_state() callbacks before these child devices have a chance to create
1673 * their device links. The fwnode links that correspond to the child devices
1674 * aren't delete because they are needed later to create the device links
1675 * between the real consumer and supplier devices.
1677 static void __fw_devlink_link_to_suppliers(struct device *dev,
1678 struct fwnode_handle *fwnode)
1680 bool own_link = (dev->fwnode == fwnode);
1681 struct fwnode_link *link, *tmp;
1682 struct fwnode_handle *child = NULL;
1686 dl_flags = fw_devlink_get_flags();
1688 dl_flags = FW_DEVLINK_FLAGS_PERMISSIVE;
1690 list_for_each_entry_safe(link, tmp, &fwnode->suppliers, c_hook) {
1692 struct device *sup_dev;
1693 struct fwnode_handle *sup = link->supplier;
1695 ret = fw_devlink_create_devlink(dev, sup, dl_flags);
1696 if (!own_link || ret == -EAGAIN)
1699 list_del(&link->s_hook);
1700 list_del(&link->c_hook);
1703 /* If no device link was created, nothing more to do. */
1708 * If a device link was successfully created to a supplier, we
1709 * now need to try and link the supplier to all its suppliers.
1711 * This is needed to detect and delete false dependencies in
1712 * fwnode links that haven't been converted to a device link
1713 * yet. See comments in fw_devlink_create_devlink() for more
1714 * details on the false dependency.
1716 * Without deleting these false dependencies, some devices will
1717 * never probe because they'll keep waiting for their false
1718 * dependency fwnode links to be converted to device links.
1720 sup_dev = get_dev_from_fwnode(sup);
1721 __fw_devlink_link_to_suppliers(sup_dev, sup_dev->fwnode);
1722 put_device(sup_dev);
1726 * Make "proxy" SYNC_STATE_ONLY device links to represent the needs of
1727 * all the descendants. This proxy link step is needed to handle the
1728 * case where the supplier is added before the consumer's parent device
1731 while ((child = fwnode_get_next_available_child_node(fwnode, child)))
1732 __fw_devlink_link_to_suppliers(dev, child);
1735 static void fw_devlink_link_device(struct device *dev)
1737 struct fwnode_handle *fwnode = dev->fwnode;
1739 if (!fw_devlink_flags)
1742 fw_devlink_parse_fwtree(fwnode);
1744 mutex_lock(&fwnode_link_lock);
1745 __fw_devlink_link_to_consumers(dev);
1746 __fw_devlink_link_to_suppliers(dev, fwnode);
1747 mutex_unlock(&fwnode_link_lock);
1750 /* Device links support end. */
1752 int (*platform_notify)(struct device *dev) = NULL;
1753 int (*platform_notify_remove)(struct device *dev) = NULL;
1754 static struct kobject *dev_kobj;
1755 struct kobject *sysfs_dev_char_kobj;
1756 struct kobject *sysfs_dev_block_kobj;
1758 static DEFINE_MUTEX(device_hotplug_lock);
1760 void lock_device_hotplug(void)
1762 mutex_lock(&device_hotplug_lock);
1765 void unlock_device_hotplug(void)
1767 mutex_unlock(&device_hotplug_lock);
1770 int lock_device_hotplug_sysfs(void)
1772 if (mutex_trylock(&device_hotplug_lock))
1775 /* Avoid busy looping (5 ms of sleep should do). */
1777 return restart_syscall();
1781 static inline int device_is_not_partition(struct device *dev)
1783 return !(dev->type == &part_type);
1786 static inline int device_is_not_partition(struct device *dev)
1793 device_platform_notify(struct device *dev, enum kobject_action action)
1797 ret = acpi_platform_notify(dev, action);
1801 ret = software_node_notify(dev, action);
1805 if (platform_notify && action == KOBJ_ADD)
1806 platform_notify(dev);
1807 else if (platform_notify_remove && action == KOBJ_REMOVE)
1808 platform_notify_remove(dev);
1813 * dev_driver_string - Return a device's driver name, if at all possible
1814 * @dev: struct device to get the name of
1816 * Will return the device's driver's name if it is bound to a device. If
1817 * the device is not bound to a driver, it will return the name of the bus
1818 * it is attached to. If it is not attached to a bus either, an empty
1819 * string will be returned.
1821 const char *dev_driver_string(const struct device *dev)
1823 struct device_driver *drv;
1825 /* dev->driver can change to NULL underneath us because of unbinding,
1826 * so be careful about accessing it. dev->bus and dev->class should
1827 * never change once they are set, so they don't need special care.
1829 drv = READ_ONCE(dev->driver);
1830 return drv ? drv->name :
1831 (dev->bus ? dev->bus->name :
1832 (dev->class ? dev->class->name : ""));
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;
2608 EXPORT_SYMBOL_GPL(device_initialize);
2610 struct kobject *virtual_device_parent(struct device *dev)
2612 static struct kobject *virtual_dir = NULL;
2615 virtual_dir = kobject_create_and_add("virtual",
2616 &devices_kset->kobj);
2622 struct kobject kobj;
2623 struct class *class;
2626 #define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
2628 static void class_dir_release(struct kobject *kobj)
2630 struct class_dir *dir = to_class_dir(kobj);
2635 struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
2637 struct class_dir *dir = to_class_dir(kobj);
2638 return dir->class->ns_type;
2641 static struct kobj_type class_dir_ktype = {
2642 .release = class_dir_release,
2643 .sysfs_ops = &kobj_sysfs_ops,
2644 .child_ns_type = class_dir_child_ns_type
2647 static struct kobject *
2648 class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
2650 struct class_dir *dir;
2653 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
2655 return ERR_PTR(-ENOMEM);
2658 kobject_init(&dir->kobj, &class_dir_ktype);
2660 dir->kobj.kset = &class->p->glue_dirs;
2662 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
2664 kobject_put(&dir->kobj);
2665 return ERR_PTR(retval);
2670 static DEFINE_MUTEX(gdp_mutex);
2672 static struct kobject *get_device_parent(struct device *dev,
2673 struct device *parent)
2676 struct kobject *kobj = NULL;
2677 struct kobject *parent_kobj;
2681 /* block disks show up in /sys/block */
2682 if (sysfs_deprecated && dev->class == &block_class) {
2683 if (parent && parent->class == &block_class)
2684 return &parent->kobj;
2685 return &block_class.p->subsys.kobj;
2690 * If we have no parent, we live in "virtual".
2691 * Class-devices with a non class-device as parent, live
2692 * in a "glue" directory to prevent namespace collisions.
2695 parent_kobj = virtual_device_parent(dev);
2696 else if (parent->class && !dev->class->ns_type)
2697 return &parent->kobj;
2699 parent_kobj = &parent->kobj;
2701 mutex_lock(&gdp_mutex);
2703 /* find our class-directory at the parent and reference it */
2704 spin_lock(&dev->class->p->glue_dirs.list_lock);
2705 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
2706 if (k->parent == parent_kobj) {
2707 kobj = kobject_get(k);
2710 spin_unlock(&dev->class->p->glue_dirs.list_lock);
2712 mutex_unlock(&gdp_mutex);
2716 /* or create a new class-directory at the parent device */
2717 k = class_dir_create_and_add(dev->class, parent_kobj);
2718 /* do not emit an uevent for this simple "glue" directory */
2719 mutex_unlock(&gdp_mutex);
2723 /* subsystems can specify a default root directory for their devices */
2724 if (!parent && dev->bus && dev->bus->dev_root)
2725 return &dev->bus->dev_root->kobj;
2728 return &parent->kobj;
2732 static inline bool live_in_glue_dir(struct kobject *kobj,
2735 if (!kobj || !dev->class ||
2736 kobj->kset != &dev->class->p->glue_dirs)
2741 static inline struct kobject *get_glue_dir(struct device *dev)
2743 return dev->kobj.parent;
2747 * make sure cleaning up dir as the last step, we need to make
2748 * sure .release handler of kobject is run with holding the
2751 static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
2755 /* see if we live in a "glue" directory */
2756 if (!live_in_glue_dir(glue_dir, dev))
2759 mutex_lock(&gdp_mutex);
2761 * There is a race condition between removing glue directory
2762 * and adding a new device under the glue directory.
2767 * get_device_parent()
2768 * class_dir_create_and_add()
2769 * kobject_add_internal()
2770 * create_dir() // create glue_dir
2773 * get_device_parent()
2774 * kobject_get() // get glue_dir
2777 * cleanup_glue_dir()
2778 * kobject_del(glue_dir)
2781 * kobject_add_internal()
2782 * create_dir() // in glue_dir
2783 * sysfs_create_dir_ns()
2784 * kernfs_create_dir_ns(sd)
2786 * sysfs_remove_dir() // glue_dir->sd=NULL
2787 * sysfs_put() // free glue_dir->sd
2790 * kernfs_new_node(sd)
2791 * kernfs_get(glue_dir)
2795 * Before CPU1 remove last child device under glue dir, if CPU2 add
2796 * a new device under glue dir, the glue_dir kobject reference count
2797 * will be increase to 2 in kobject_get(k). And CPU2 has been called
2798 * kernfs_create_dir_ns(). Meanwhile, CPU1 call sysfs_remove_dir()
2799 * and sysfs_put(). This result in glue_dir->sd is freed.
2801 * Then the CPU2 will see a stale "empty" but still potentially used
2802 * glue dir around in kernfs_new_node().
2804 * In order to avoid this happening, we also should make sure that
2805 * kernfs_node for glue_dir is released in CPU1 only when refcount
2806 * for glue_dir kobj is 1.
2808 ref = kref_read(&glue_dir->kref);
2809 if (!kobject_has_children(glue_dir) && !--ref)
2810 kobject_del(glue_dir);
2811 kobject_put(glue_dir);
2812 mutex_unlock(&gdp_mutex);
2815 static int device_add_class_symlinks(struct device *dev)
2817 struct device_node *of_node = dev_of_node(dev);
2821 error = sysfs_create_link(&dev->kobj, of_node_kobj(of_node), "of_node");
2823 dev_warn(dev, "Error %d creating of_node link\n",error);
2824 /* An error here doesn't warrant bringing down the device */
2830 error = sysfs_create_link(&dev->kobj,
2831 &dev->class->p->subsys.kobj,
2836 if (dev->parent && device_is_not_partition(dev)) {
2837 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
2844 /* /sys/block has directories and does not need symlinks */
2845 if (sysfs_deprecated && dev->class == &block_class)
2849 /* link in the class directory pointing to the device */
2850 error = sysfs_create_link(&dev->class->p->subsys.kobj,
2851 &dev->kobj, dev_name(dev));
2858 sysfs_remove_link(&dev->kobj, "device");
2861 sysfs_remove_link(&dev->kobj, "subsystem");
2863 sysfs_remove_link(&dev->kobj, "of_node");
2867 static void device_remove_class_symlinks(struct device *dev)
2869 if (dev_of_node(dev))
2870 sysfs_remove_link(&dev->kobj, "of_node");
2875 if (dev->parent && device_is_not_partition(dev))
2876 sysfs_remove_link(&dev->kobj, "device");
2877 sysfs_remove_link(&dev->kobj, "subsystem");
2879 if (sysfs_deprecated && dev->class == &block_class)
2882 sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
2886 * dev_set_name - set a device name
2888 * @fmt: format string for the device's name
2890 int dev_set_name(struct device *dev, const char *fmt, ...)
2895 va_start(vargs, fmt);
2896 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
2900 EXPORT_SYMBOL_GPL(dev_set_name);
2903 * device_to_dev_kobj - select a /sys/dev/ directory for the device
2906 * By default we select char/ for new entries. Setting class->dev_obj
2907 * to NULL prevents an entry from being created. class->dev_kobj must
2908 * be set (or cleared) before any devices are registered to the class
2909 * otherwise device_create_sys_dev_entry() and
2910 * device_remove_sys_dev_entry() will disagree about the presence of
2913 static struct kobject *device_to_dev_kobj(struct device *dev)
2915 struct kobject *kobj;
2918 kobj = dev->class->dev_kobj;
2920 kobj = sysfs_dev_char_kobj;
2925 static int device_create_sys_dev_entry(struct device *dev)
2927 struct kobject *kobj = device_to_dev_kobj(dev);
2932 format_dev_t(devt_str, dev->devt);
2933 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
2939 static void device_remove_sys_dev_entry(struct device *dev)
2941 struct kobject *kobj = device_to_dev_kobj(dev);
2945 format_dev_t(devt_str, dev->devt);
2946 sysfs_remove_link(kobj, devt_str);
2950 static int device_private_init(struct device *dev)
2952 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
2955 dev->p->device = dev;
2956 klist_init(&dev->p->klist_children, klist_children_get,
2957 klist_children_put);
2958 INIT_LIST_HEAD(&dev->p->deferred_probe);
2963 * device_add - add device to device hierarchy.
2966 * This is part 2 of device_register(), though may be called
2967 * separately _iff_ device_initialize() has been called separately.
2969 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
2970 * to the global and sibling lists for the device, then
2971 * adds it to the other relevant subsystems of the driver model.
2973 * Do not call this routine or device_register() more than once for
2974 * any device structure. The driver model core is not designed to work
2975 * with devices that get unregistered and then spring back to life.
2976 * (Among other things, it's very hard to guarantee that all references
2977 * to the previous incarnation of @dev have been dropped.) Allocate
2978 * and register a fresh new struct device instead.
2980 * NOTE: _Never_ directly free @dev after calling this function, even
2981 * if it returned an error! Always use put_device() to give up your
2982 * reference instead.
2984 * Rule of thumb is: if device_add() succeeds, you should call
2985 * device_del() when you want to get rid of it. If device_add() has
2986 * *not* succeeded, use *only* put_device() to drop the reference
2989 int device_add(struct device *dev)
2991 struct device *parent;
2992 struct kobject *kobj;
2993 struct class_interface *class_intf;
2994 int error = -EINVAL;
2995 struct kobject *glue_dir = NULL;
2997 dev = get_device(dev);
3002 error = device_private_init(dev);
3008 * for statically allocated devices, which should all be converted
3009 * some day, we need to initialize the name. We prevent reading back
3010 * the name, and force the use of dev_name()
3012 if (dev->init_name) {
3013 dev_set_name(dev, "%s", dev->init_name);
3014 dev->init_name = NULL;
3017 /* subsystems can specify simple device enumeration */
3018 if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
3019 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
3021 if (!dev_name(dev)) {
3026 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
3028 parent = get_device(dev->parent);
3029 kobj = get_device_parent(dev, parent);
3031 error = PTR_ERR(kobj);
3035 dev->kobj.parent = kobj;
3037 /* use parent numa_node */
3038 if (parent && (dev_to_node(dev) == NUMA_NO_NODE))
3039 set_dev_node(dev, dev_to_node(parent));
3041 /* first, register with generic layer. */
3042 /* we require the name to be set before, and pass NULL */
3043 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
3045 glue_dir = get_glue_dir(dev);
3049 /* notify platform of device entry */
3050 error = device_platform_notify(dev, KOBJ_ADD);
3052 goto platform_error;
3054 error = device_create_file(dev, &dev_attr_uevent);
3058 error = device_add_class_symlinks(dev);
3061 error = device_add_attrs(dev);
3064 error = bus_add_device(dev);
3067 error = dpm_sysfs_add(dev);
3072 if (MAJOR(dev->devt)) {
3073 error = device_create_file(dev, &dev_attr_dev);
3077 error = device_create_sys_dev_entry(dev);
3081 devtmpfs_create_node(dev);
3084 /* Notify clients of device addition. This call must come
3085 * after dpm_sysfs_add() and before kobject_uevent().
3088 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
3089 BUS_NOTIFY_ADD_DEVICE, dev);
3091 kobject_uevent(&dev->kobj, KOBJ_ADD);
3094 * Check if any of the other devices (consumers) have been waiting for
3095 * this device (supplier) to be added so that they can create a device
3098 * This needs to happen after device_pm_add() because device_link_add()
3099 * requires the supplier be registered before it's called.
3101 * But this also needs to happen before bus_probe_device() to make sure
3102 * waiting consumers can link to it before the driver is bound to the
3103 * device and the driver sync_state callback is called for this device.
3105 if (dev->fwnode && !dev->fwnode->dev) {
3106 dev->fwnode->dev = dev;
3107 fw_devlink_link_device(dev);
3110 bus_probe_device(dev);
3112 klist_add_tail(&dev->p->knode_parent,
3113 &parent->p->klist_children);
3116 mutex_lock(&dev->class->p->mutex);
3117 /* tie the class to the device */
3118 klist_add_tail(&dev->p->knode_class,
3119 &dev->class->p->klist_devices);
3121 /* notify any interfaces that the device is here */
3122 list_for_each_entry(class_intf,
3123 &dev->class->p->interfaces, node)
3124 if (class_intf->add_dev)
3125 class_intf->add_dev(dev, class_intf);
3126 mutex_unlock(&dev->class->p->mutex);
3132 if (MAJOR(dev->devt))
3133 device_remove_file(dev, &dev_attr_dev);
3135 device_pm_remove(dev);
3136 dpm_sysfs_remove(dev);
3138 bus_remove_device(dev);
3140 device_remove_attrs(dev);
3142 device_remove_class_symlinks(dev);
3144 device_remove_file(dev, &dev_attr_uevent);
3146 device_platform_notify(dev, KOBJ_REMOVE);
3148 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
3149 glue_dir = get_glue_dir(dev);
3150 kobject_del(&dev->kobj);
3152 cleanup_glue_dir(dev, glue_dir);
3160 EXPORT_SYMBOL_GPL(device_add);
3163 * device_register - register a device with the system.
3164 * @dev: pointer to the device structure
3166 * This happens in two clean steps - initialize the device
3167 * and add it to the system. The two steps can be called
3168 * separately, but this is the easiest and most common.
3169 * I.e. you should only call the two helpers separately if
3170 * have a clearly defined need to use and refcount the device
3171 * before it is added to the hierarchy.
3173 * For more information, see the kerneldoc for device_initialize()
3176 * NOTE: _Never_ directly free @dev after calling this function, even
3177 * if it returned an error! Always use put_device() to give up the
3178 * reference initialized in this function instead.
3180 int device_register(struct device *dev)
3182 device_initialize(dev);
3183 return device_add(dev);
3185 EXPORT_SYMBOL_GPL(device_register);
3188 * get_device - increment reference count for device.
3191 * This simply forwards the call to kobject_get(), though
3192 * we do take care to provide for the case that we get a NULL
3193 * pointer passed in.
3195 struct device *get_device(struct device *dev)
3197 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
3199 EXPORT_SYMBOL_GPL(get_device);
3202 * put_device - decrement reference count.
3203 * @dev: device in question.
3205 void put_device(struct device *dev)
3207 /* might_sleep(); */
3209 kobject_put(&dev->kobj);
3211 EXPORT_SYMBOL_GPL(put_device);
3213 bool kill_device(struct device *dev)
3216 * Require the device lock and set the "dead" flag to guarantee that
3217 * the update behavior is consistent with the other bitfields near
3218 * it and that we cannot have an asynchronous probe routine trying
3219 * to run while we are tearing out the bus/class/sysfs from
3220 * underneath the device.
3222 lockdep_assert_held(&dev->mutex);
3226 dev->p->dead = true;
3229 EXPORT_SYMBOL_GPL(kill_device);
3232 * device_del - delete device from system.
3235 * This is the first part of the device unregistration
3236 * sequence. This removes the device from the lists we control
3237 * from here, has it removed from the other driver model
3238 * subsystems it was added to in device_add(), and removes it
3239 * from the kobject hierarchy.
3241 * NOTE: this should be called manually _iff_ device_add() was
3242 * also called manually.
3244 void device_del(struct device *dev)
3246 struct device *parent = dev->parent;
3247 struct kobject *glue_dir = NULL;
3248 struct class_interface *class_intf;
3249 unsigned int noio_flag;
3255 if (dev->fwnode && dev->fwnode->dev == dev)
3256 dev->fwnode->dev = NULL;
3258 /* Notify clients of device removal. This call must come
3259 * before dpm_sysfs_remove().
3261 noio_flag = memalloc_noio_save();
3263 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
3264 BUS_NOTIFY_DEL_DEVICE, dev);
3266 dpm_sysfs_remove(dev);
3268 klist_del(&dev->p->knode_parent);
3269 if (MAJOR(dev->devt)) {
3270 devtmpfs_delete_node(dev);
3271 device_remove_sys_dev_entry(dev);
3272 device_remove_file(dev, &dev_attr_dev);
3275 device_remove_class_symlinks(dev);
3277 mutex_lock(&dev->class->p->mutex);
3278 /* notify any interfaces that the device is now gone */
3279 list_for_each_entry(class_intf,
3280 &dev->class->p->interfaces, node)
3281 if (class_intf->remove_dev)
3282 class_intf->remove_dev(dev, class_intf);
3283 /* remove the device from the class list */
3284 klist_del(&dev->p->knode_class);
3285 mutex_unlock(&dev->class->p->mutex);
3287 device_remove_file(dev, &dev_attr_uevent);
3288 device_remove_attrs(dev);
3289 bus_remove_device(dev);
3290 device_pm_remove(dev);
3291 driver_deferred_probe_del(dev);
3292 device_platform_notify(dev, KOBJ_REMOVE);
3293 device_remove_properties(dev);
3294 device_links_purge(dev);
3297 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
3298 BUS_NOTIFY_REMOVED_DEVICE, dev);
3299 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
3300 glue_dir = get_glue_dir(dev);
3301 kobject_del(&dev->kobj);
3302 cleanup_glue_dir(dev, glue_dir);
3303 memalloc_noio_restore(noio_flag);
3306 EXPORT_SYMBOL_GPL(device_del);
3309 * device_unregister - unregister device from system.
3310 * @dev: device going away.
3312 * We do this in two parts, like we do device_register(). First,
3313 * we remove it from all the subsystems with device_del(), then
3314 * we decrement the reference count via put_device(). If that
3315 * is the final reference count, the device will be cleaned up
3316 * via device_release() above. Otherwise, the structure will
3317 * stick around until the final reference to the device is dropped.
3319 void device_unregister(struct device *dev)
3321 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
3325 EXPORT_SYMBOL_GPL(device_unregister);
3327 static struct device *prev_device(struct klist_iter *i)
3329 struct klist_node *n = klist_prev(i);
3330 struct device *dev = NULL;
3331 struct device_private *p;
3334 p = to_device_private_parent(n);
3340 static struct device *next_device(struct klist_iter *i)
3342 struct klist_node *n = klist_next(i);
3343 struct device *dev = NULL;
3344 struct device_private *p;
3347 p = to_device_private_parent(n);
3354 * device_get_devnode - path of device node file
3356 * @mode: returned file access mode
3357 * @uid: returned file owner
3358 * @gid: returned file group
3359 * @tmp: possibly allocated string
3361 * Return the relative path of a possible device node.
3362 * Non-default names may need to allocate a memory to compose
3363 * a name. This memory is returned in tmp and needs to be
3364 * freed by the caller.
3366 const char *device_get_devnode(struct device *dev,
3367 umode_t *mode, kuid_t *uid, kgid_t *gid,
3374 /* the device type may provide a specific name */
3375 if (dev->type && dev->type->devnode)
3376 *tmp = dev->type->devnode(dev, mode, uid, gid);
3380 /* the class may provide a specific name */
3381 if (dev->class && dev->class->devnode)
3382 *tmp = dev->class->devnode(dev, mode);
3386 /* return name without allocation, tmp == NULL */
3387 if (strchr(dev_name(dev), '!') == NULL)
3388 return dev_name(dev);
3390 /* replace '!' in the name with '/' */
3391 s = kstrdup(dev_name(dev), GFP_KERNEL);
3394 strreplace(s, '!', '/');
3399 * device_for_each_child - device child iterator.
3400 * @parent: parent struct device.
3401 * @fn: function to be called for each device.
3402 * @data: data for the callback.
3404 * Iterate over @parent's child devices, and call @fn for each,
3407 * We check the return of @fn each time. If it returns anything
3408 * other than 0, we break out and return that value.
3410 int device_for_each_child(struct device *parent, void *data,
3411 int (*fn)(struct device *dev, void *data))
3413 struct klist_iter i;
3414 struct device *child;
3420 klist_iter_init(&parent->p->klist_children, &i);
3421 while (!error && (child = next_device(&i)))
3422 error = fn(child, data);
3423 klist_iter_exit(&i);
3426 EXPORT_SYMBOL_GPL(device_for_each_child);
3429 * device_for_each_child_reverse - device child iterator in reversed order.
3430 * @parent: parent struct device.
3431 * @fn: function to be called for each device.
3432 * @data: data for the callback.
3434 * Iterate over @parent's child devices, and call @fn for each,
3437 * We check the return of @fn each time. If it returns anything
3438 * other than 0, we break out and return that value.
3440 int device_for_each_child_reverse(struct device *parent, void *data,
3441 int (*fn)(struct device *dev, void *data))
3443 struct klist_iter i;
3444 struct device *child;
3450 klist_iter_init(&parent->p->klist_children, &i);
3451 while ((child = prev_device(&i)) && !error)
3452 error = fn(child, data);
3453 klist_iter_exit(&i);
3456 EXPORT_SYMBOL_GPL(device_for_each_child_reverse);
3459 * device_find_child - device iterator for locating a particular device.
3460 * @parent: parent struct device
3461 * @match: Callback function to check device
3462 * @data: Data to pass to match function
3464 * This is similar to the device_for_each_child() function above, but it
3465 * returns a reference to a device that is 'found' for later use, as
3466 * determined by the @match callback.
3468 * The callback should return 0 if the device doesn't match and non-zero
3469 * if it does. If the callback returns non-zero and a reference to the
3470 * current device can be obtained, this function will return to the caller
3471 * and not iterate over any more devices.
3473 * NOTE: you will need to drop the reference with put_device() after use.
3475 struct device *device_find_child(struct device *parent, void *data,
3476 int (*match)(struct device *dev, void *data))
3478 struct klist_iter i;
3479 struct device *child;
3484 klist_iter_init(&parent->p->klist_children, &i);
3485 while ((child = next_device(&i)))
3486 if (match(child, data) && get_device(child))
3488 klist_iter_exit(&i);
3491 EXPORT_SYMBOL_GPL(device_find_child);
3494 * device_find_child_by_name - device iterator for locating a child device.
3495 * @parent: parent struct device
3496 * @name: name of the child device
3498 * This is similar to the device_find_child() function above, but it
3499 * returns a reference to a device that has the name @name.
3501 * NOTE: you will need to drop the reference with put_device() after use.
3503 struct device *device_find_child_by_name(struct device *parent,
3506 struct klist_iter i;
3507 struct device *child;
3512 klist_iter_init(&parent->p->klist_children, &i);
3513 while ((child = next_device(&i)))
3514 if (sysfs_streq(dev_name(child), name) && get_device(child))
3516 klist_iter_exit(&i);
3519 EXPORT_SYMBOL_GPL(device_find_child_by_name);
3521 int __init devices_init(void)
3523 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
3526 dev_kobj = kobject_create_and_add("dev", NULL);
3529 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
3530 if (!sysfs_dev_block_kobj)
3531 goto block_kobj_err;
3532 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
3533 if (!sysfs_dev_char_kobj)
3539 kobject_put(sysfs_dev_block_kobj);
3541 kobject_put(dev_kobj);
3543 kset_unregister(devices_kset);
3547 static int device_check_offline(struct device *dev, void *not_used)
3551 ret = device_for_each_child(dev, NULL, device_check_offline);
3555 return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
3559 * device_offline - Prepare the device for hot-removal.
3560 * @dev: Device to be put offline.
3562 * Execute the device bus type's .offline() callback, if present, to prepare
3563 * the device for a subsequent hot-removal. If that succeeds, the device must
3564 * not be used until either it is removed or its bus type's .online() callback
3567 * Call under device_hotplug_lock.
3569 int device_offline(struct device *dev)
3573 if (dev->offline_disabled)
3576 ret = device_for_each_child(dev, NULL, device_check_offline);
3581 if (device_supports_offline(dev)) {
3585 ret = dev->bus->offline(dev);
3587 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
3588 dev->offline = true;
3598 * device_online - Put the device back online after successful device_offline().
3599 * @dev: Device to be put back online.
3601 * If device_offline() has been successfully executed for @dev, but the device
3602 * has not been removed subsequently, execute its bus type's .online() callback
3603 * to indicate that the device can be used again.
3605 * Call under device_hotplug_lock.
3607 int device_online(struct device *dev)
3612 if (device_supports_offline(dev)) {
3614 ret = dev->bus->online(dev);
3616 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
3617 dev->offline = false;
3628 struct root_device {
3630 struct module *owner;
3633 static inline struct root_device *to_root_device(struct device *d)
3635 return container_of(d, struct root_device, dev);
3638 static void root_device_release(struct device *dev)
3640 kfree(to_root_device(dev));
3644 * __root_device_register - allocate and register a root device
3645 * @name: root device name
3646 * @owner: owner module of the root device, usually THIS_MODULE
3648 * This function allocates a root device and registers it
3649 * using device_register(). In order to free the returned
3650 * device, use root_device_unregister().
3652 * Root devices are dummy devices which allow other devices
3653 * to be grouped under /sys/devices. Use this function to
3654 * allocate a root device and then use it as the parent of
3655 * any device which should appear under /sys/devices/{name}
3657 * The /sys/devices/{name} directory will also contain a
3658 * 'module' symlink which points to the @owner directory
3661 * Returns &struct device pointer on success, or ERR_PTR() on error.
3663 * Note: You probably want to use root_device_register().
3665 struct device *__root_device_register(const char *name, struct module *owner)
3667 struct root_device *root;
3670 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
3672 return ERR_PTR(err);
3674 err = dev_set_name(&root->dev, "%s", name);
3677 return ERR_PTR(err);
3680 root->dev.release = root_device_release;
3682 err = device_register(&root->dev);
3684 put_device(&root->dev);
3685 return ERR_PTR(err);
3688 #ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
3690 struct module_kobject *mk = &owner->mkobj;
3692 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
3694 device_unregister(&root->dev);
3695 return ERR_PTR(err);
3697 root->owner = owner;
3703 EXPORT_SYMBOL_GPL(__root_device_register);
3706 * root_device_unregister - unregister and free a root device
3707 * @dev: device going away
3709 * This function unregisters and cleans up a device that was created by
3710 * root_device_register().
3712 void root_device_unregister(struct device *dev)
3714 struct root_device *root = to_root_device(dev);
3717 sysfs_remove_link(&root->dev.kobj, "module");
3719 device_unregister(dev);
3721 EXPORT_SYMBOL_GPL(root_device_unregister);
3724 static void device_create_release(struct device *dev)
3726 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
3730 static __printf(6, 0) struct device *
3731 device_create_groups_vargs(struct class *class, struct device *parent,
3732 dev_t devt, void *drvdata,
3733 const struct attribute_group **groups,
3734 const char *fmt, va_list args)
3736 struct device *dev = NULL;
3737 int retval = -ENODEV;
3739 if (class == NULL || IS_ERR(class))
3742 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
3748 device_initialize(dev);
3751 dev->parent = parent;
3752 dev->groups = groups;
3753 dev->release = device_create_release;
3754 dev_set_drvdata(dev, drvdata);
3756 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
3760 retval = device_add(dev);
3768 return ERR_PTR(retval);
3772 * device_create - creates a device and registers it with sysfs
3773 * @class: pointer to the struct class that this device should be registered to
3774 * @parent: pointer to the parent struct device of this new device, if any
3775 * @devt: the dev_t for the char device to be added
3776 * @drvdata: the data to be added to the device for callbacks
3777 * @fmt: string for the device's name
3779 * This function can be used by char device classes. A struct device
3780 * will be created in sysfs, registered to the specified class.
3782 * A "dev" file will be created, showing the dev_t for the device, if
3783 * the dev_t is not 0,0.
3784 * If a pointer to a parent struct device is passed in, the newly created
3785 * struct device will be a child of that device in sysfs.
3786 * The pointer to the struct device will be returned from the call.
3787 * Any further sysfs files that might be required can be created using this
3790 * Returns &struct device pointer on success, or ERR_PTR() on error.
3792 * Note: the struct class passed to this function must have previously
3793 * been created with a call to class_create().
3795 struct device *device_create(struct class *class, struct device *parent,
3796 dev_t devt, void *drvdata, const char *fmt, ...)
3801 va_start(vargs, fmt);
3802 dev = device_create_groups_vargs(class, parent, devt, drvdata, NULL,
3807 EXPORT_SYMBOL_GPL(device_create);
3810 * device_create_with_groups - creates a device and registers it with sysfs
3811 * @class: pointer to the struct class that this device should be registered to
3812 * @parent: pointer to the parent struct device of this new device, if any
3813 * @devt: the dev_t for the char device to be added
3814 * @drvdata: the data to be added to the device for callbacks
3815 * @groups: NULL-terminated list of attribute groups to be created
3816 * @fmt: string for the device's name
3818 * This function can be used by char device classes. A struct device
3819 * will be created in sysfs, registered to the specified class.
3820 * Additional attributes specified in the groups parameter will also
3821 * be created automatically.
3823 * A "dev" file will be created, showing the dev_t for the device, if
3824 * the dev_t is not 0,0.
3825 * If a pointer to a parent struct device is passed in, the newly created
3826 * struct device will be a child of that device in sysfs.
3827 * The pointer to the struct device will be returned from the call.
3828 * Any further sysfs files that might be required can be created using this
3831 * Returns &struct device pointer on success, or ERR_PTR() on error.
3833 * Note: the struct class passed to this function must have previously
3834 * been created with a call to class_create().
3836 struct device *device_create_with_groups(struct class *class,
3837 struct device *parent, dev_t devt,
3839 const struct attribute_group **groups,
3840 const char *fmt, ...)
3845 va_start(vargs, fmt);
3846 dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
3851 EXPORT_SYMBOL_GPL(device_create_with_groups);
3854 * device_destroy - removes a device that was created with device_create()
3855 * @class: pointer to the struct class that this device was registered with
3856 * @devt: the dev_t of the device that was previously registered
3858 * This call unregisters and cleans up a device that was created with a
3859 * call to device_create().
3861 void device_destroy(struct class *class, dev_t devt)
3865 dev = class_find_device_by_devt(class, devt);
3868 device_unregister(dev);
3871 EXPORT_SYMBOL_GPL(device_destroy);
3874 * device_rename - renames a device
3875 * @dev: the pointer to the struct device to be renamed
3876 * @new_name: the new name of the device
3878 * It is the responsibility of the caller to provide mutual
3879 * exclusion between two different calls of device_rename
3880 * on the same device to ensure that new_name is valid and
3881 * won't conflict with other devices.
3883 * Note: Don't call this function. Currently, the networking layer calls this
3884 * function, but that will change. The following text from Kay Sievers offers
3887 * Renaming devices is racy at many levels, symlinks and other stuff are not
3888 * replaced atomically, and you get a "move" uevent, but it's not easy to
3889 * connect the event to the old and new device. Device nodes are not renamed at
3890 * all, there isn't even support for that in the kernel now.
3892 * In the meantime, during renaming, your target name might be taken by another
3893 * driver, creating conflicts. Or the old name is taken directly after you
3894 * renamed it -- then you get events for the same DEVPATH, before you even see
3895 * the "move" event. It's just a mess, and nothing new should ever rely on
3896 * kernel device renaming. Besides that, it's not even implemented now for
3897 * other things than (driver-core wise very simple) network devices.
3899 * We are currently about to change network renaming in udev to completely
3900 * disallow renaming of devices in the same namespace as the kernel uses,
3901 * because we can't solve the problems properly, that arise with swapping names
3902 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
3903 * be allowed to some other name than eth[0-9]*, for the aforementioned
3906 * Make up a "real" name in the driver before you register anything, or add
3907 * some other attributes for userspace to find the device, or use udev to add
3908 * symlinks -- but never rename kernel devices later, it's a complete mess. We
3909 * don't even want to get into that and try to implement the missing pieces in
3910 * the core. We really have other pieces to fix in the driver core mess. :)
3912 int device_rename(struct device *dev, const char *new_name)
3914 struct kobject *kobj = &dev->kobj;
3915 char *old_device_name = NULL;
3918 dev = get_device(dev);
3922 dev_dbg(dev, "renaming to %s\n", new_name);
3924 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
3925 if (!old_device_name) {
3931 error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj,
3932 kobj, old_device_name,
3933 new_name, kobject_namespace(kobj));
3938 error = kobject_rename(kobj, new_name);
3945 kfree(old_device_name);
3949 EXPORT_SYMBOL_GPL(device_rename);
3951 static int device_move_class_links(struct device *dev,
3952 struct device *old_parent,
3953 struct device *new_parent)
3958 sysfs_remove_link(&dev->kobj, "device");
3960 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
3966 * device_move - moves a device to a new parent
3967 * @dev: the pointer to the struct device to be moved
3968 * @new_parent: the new parent of the device (can be NULL)
3969 * @dpm_order: how to reorder the dpm_list
3971 int device_move(struct device *dev, struct device *new_parent,
3972 enum dpm_order dpm_order)
3975 struct device *old_parent;
3976 struct kobject *new_parent_kobj;
3978 dev = get_device(dev);
3983 new_parent = get_device(new_parent);
3984 new_parent_kobj = get_device_parent(dev, new_parent);
3985 if (IS_ERR(new_parent_kobj)) {
3986 error = PTR_ERR(new_parent_kobj);
3987 put_device(new_parent);
3991 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
3992 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
3993 error = kobject_move(&dev->kobj, new_parent_kobj);
3995 cleanup_glue_dir(dev, new_parent_kobj);
3996 put_device(new_parent);
3999 old_parent = dev->parent;
4000 dev->parent = new_parent;
4002 klist_remove(&dev->p->knode_parent);
4004 klist_add_tail(&dev->p->knode_parent,
4005 &new_parent->p->klist_children);
4006 set_dev_node(dev, dev_to_node(new_parent));
4010 error = device_move_class_links(dev, old_parent, new_parent);
4012 /* We ignore errors on cleanup since we're hosed anyway... */
4013 device_move_class_links(dev, new_parent, old_parent);
4014 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
4016 klist_remove(&dev->p->knode_parent);
4017 dev->parent = old_parent;
4019 klist_add_tail(&dev->p->knode_parent,
4020 &old_parent->p->klist_children);
4021 set_dev_node(dev, dev_to_node(old_parent));
4024 cleanup_glue_dir(dev, new_parent_kobj);
4025 put_device(new_parent);
4029 switch (dpm_order) {
4030 case DPM_ORDER_NONE:
4032 case DPM_ORDER_DEV_AFTER_PARENT:
4033 device_pm_move_after(dev, new_parent);
4034 devices_kset_move_after(dev, new_parent);
4036 case DPM_ORDER_PARENT_BEFORE_DEV:
4037 device_pm_move_before(new_parent, dev);
4038 devices_kset_move_before(new_parent, dev);
4040 case DPM_ORDER_DEV_LAST:
4041 device_pm_move_last(dev);
4042 devices_kset_move_last(dev);
4046 put_device(old_parent);
4052 EXPORT_SYMBOL_GPL(device_move);
4054 static int device_attrs_change_owner(struct device *dev, kuid_t kuid,
4057 struct kobject *kobj = &dev->kobj;
4058 struct class *class = dev->class;
4059 const struct device_type *type = dev->type;
4064 * Change the device groups of the device class for @dev to
4067 error = sysfs_groups_change_owner(kobj, class->dev_groups, kuid,
4075 * Change the device groups of the device type for @dev to
4078 error = sysfs_groups_change_owner(kobj, type->groups, kuid,
4084 /* Change the device groups of @dev to @kuid/@kgid. */
4085 error = sysfs_groups_change_owner(kobj, dev->groups, kuid, kgid);
4089 if (device_supports_offline(dev) && !dev->offline_disabled) {
4090 /* Change online device attributes of @dev to @kuid/@kgid. */
4091 error = sysfs_file_change_owner(kobj, dev_attr_online.attr.name,
4101 * device_change_owner - change the owner of an existing device.
4103 * @kuid: new owner's kuid
4104 * @kgid: new owner's kgid
4106 * This changes the owner of @dev and its corresponding sysfs entries to
4107 * @kuid/@kgid. This function closely mirrors how @dev was added via driver
4110 * Returns 0 on success or error code on failure.
4112 int device_change_owner(struct device *dev, kuid_t kuid, kgid_t kgid)
4115 struct kobject *kobj = &dev->kobj;
4117 dev = get_device(dev);
4122 * Change the kobject and the default attributes and groups of the
4123 * ktype associated with it to @kuid/@kgid.
4125 error = sysfs_change_owner(kobj, kuid, kgid);
4130 * Change the uevent file for @dev to the new owner. The uevent file
4131 * was created in a separate step when @dev got added and we mirror
4134 error = sysfs_file_change_owner(kobj, dev_attr_uevent.attr.name, kuid,
4140 * Change the device groups, the device groups associated with the
4141 * device class, and the groups associated with the device type of @dev
4144 error = device_attrs_change_owner(dev, kuid, kgid);
4148 error = dpm_sysfs_change_owner(dev, kuid, kgid);
4153 if (sysfs_deprecated && dev->class == &block_class)
4158 * Change the owner of the symlink located in the class directory of
4159 * the device class associated with @dev which points to the actual
4160 * directory entry for @dev to @kuid/@kgid. This ensures that the
4161 * symlink shows the same permissions as its target.
4163 error = sysfs_link_change_owner(&dev->class->p->subsys.kobj, &dev->kobj,
4164 dev_name(dev), kuid, kgid);
4172 EXPORT_SYMBOL_GPL(device_change_owner);
4175 * device_shutdown - call ->shutdown() on each device to shutdown.
4177 void device_shutdown(void)
4179 struct device *dev, *parent;
4181 wait_for_device_probe();
4182 device_block_probing();
4186 spin_lock(&devices_kset->list_lock);
4188 * Walk the devices list backward, shutting down each in turn.
4189 * Beware that device unplug events may also start pulling
4190 * devices offline, even as the system is shutting down.
4192 while (!list_empty(&devices_kset->list)) {
4193 dev = list_entry(devices_kset->list.prev, struct device,
4197 * hold reference count of device's parent to
4198 * prevent it from being freed because parent's
4199 * lock is to be held
4201 parent = get_device(dev->parent);
4204 * Make sure the device is off the kset list, in the
4205 * event that dev->*->shutdown() doesn't remove it.
4207 list_del_init(&dev->kobj.entry);
4208 spin_unlock(&devices_kset->list_lock);
4210 /* hold lock to avoid race with probe/release */
4212 device_lock(parent);
4215 /* Don't allow any more runtime suspends */
4216 pm_runtime_get_noresume(dev);
4217 pm_runtime_barrier(dev);
4219 if (dev->class && dev->class->shutdown_pre) {
4221 dev_info(dev, "shutdown_pre\n");
4222 dev->class->shutdown_pre(dev);
4224 if (dev->bus && dev->bus->shutdown) {
4226 dev_info(dev, "shutdown\n");
4227 dev->bus->shutdown(dev);
4228 } else if (dev->driver && dev->driver->shutdown) {
4230 dev_info(dev, "shutdown\n");
4231 dev->driver->shutdown(dev);
4236 device_unlock(parent);
4241 spin_lock(&devices_kset->list_lock);
4243 spin_unlock(&devices_kset->list_lock);
4247 * Device logging functions
4250 #ifdef CONFIG_PRINTK
4252 set_dev_info(const struct device *dev, struct dev_printk_info *dev_info)
4256 memset(dev_info, 0, sizeof(*dev_info));
4259 subsys = dev->class->name;
4261 subsys = dev->bus->name;
4265 strscpy(dev_info->subsystem, subsys, sizeof(dev_info->subsystem));
4268 * Add device identifier DEVICE=:
4272 * +sound:card0 subsystem:devname
4274 if (MAJOR(dev->devt)) {
4277 if (strcmp(subsys, "block") == 0)
4282 snprintf(dev_info->device, sizeof(dev_info->device),
4283 "%c%u:%u", c, MAJOR(dev->devt), MINOR(dev->devt));
4284 } else if (strcmp(subsys, "net") == 0) {
4285 struct net_device *net = to_net_dev(dev);
4287 snprintf(dev_info->device, sizeof(dev_info->device),
4288 "n%u", net->ifindex);
4290 snprintf(dev_info->device, sizeof(dev_info->device),
4291 "+%s:%s", subsys, dev_name(dev));
4295 int dev_vprintk_emit(int level, const struct device *dev,
4296 const char *fmt, va_list args)
4298 struct dev_printk_info dev_info;
4300 set_dev_info(dev, &dev_info);
4302 return vprintk_emit(0, level, &dev_info, fmt, args);
4304 EXPORT_SYMBOL(dev_vprintk_emit);
4306 int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
4311 va_start(args, fmt);
4313 r = dev_vprintk_emit(level, dev, fmt, args);
4319 EXPORT_SYMBOL(dev_printk_emit);
4321 static void __dev_printk(const char *level, const struct device *dev,
4322 struct va_format *vaf)
4325 dev_printk_emit(level[1] - '0', dev, "%s %s: %pV",
4326 dev_driver_string(dev), dev_name(dev), vaf);
4328 printk("%s(NULL device *): %pV", level, vaf);
4331 void dev_printk(const char *level, const struct device *dev,
4332 const char *fmt, ...)
4334 struct va_format vaf;
4337 va_start(args, fmt);
4342 __dev_printk(level, dev, &vaf);
4346 EXPORT_SYMBOL(dev_printk);
4348 #define define_dev_printk_level(func, kern_level) \
4349 void func(const struct device *dev, const char *fmt, ...) \
4351 struct va_format vaf; \
4354 va_start(args, fmt); \
4359 __dev_printk(kern_level, dev, &vaf); \
4363 EXPORT_SYMBOL(func);
4365 define_dev_printk_level(_dev_emerg, KERN_EMERG);
4366 define_dev_printk_level(_dev_alert, KERN_ALERT);
4367 define_dev_printk_level(_dev_crit, KERN_CRIT);
4368 define_dev_printk_level(_dev_err, KERN_ERR);
4369 define_dev_printk_level(_dev_warn, KERN_WARNING);
4370 define_dev_printk_level(_dev_notice, KERN_NOTICE);
4371 define_dev_printk_level(_dev_info, KERN_INFO);
4376 * dev_err_probe - probe error check and log helper
4377 * @dev: the pointer to the struct device
4378 * @err: error value to test
4379 * @fmt: printf-style format string
4380 * @...: arguments as specified in the format string
4382 * This helper implements common pattern present in probe functions for error
4383 * checking: print debug or error message depending if the error value is
4384 * -EPROBE_DEFER and propagate error upwards.
4385 * In case of -EPROBE_DEFER it sets also defer probe reason, which can be
4386 * checked later by reading devices_deferred debugfs attribute.
4387 * It replaces code sequence::
4389 * if (err != -EPROBE_DEFER)
4390 * dev_err(dev, ...);
4392 * dev_dbg(dev, ...);
4397 * return dev_err_probe(dev, err, ...);
4402 int dev_err_probe(const struct device *dev, int err, const char *fmt, ...)
4404 struct va_format vaf;
4407 va_start(args, fmt);
4411 if (err != -EPROBE_DEFER) {
4412 dev_err(dev, "error %pe: %pV", ERR_PTR(err), &vaf);
4414 device_set_deferred_probe_reason(dev, &vaf);
4415 dev_dbg(dev, "error %pe: %pV", ERR_PTR(err), &vaf);
4422 EXPORT_SYMBOL_GPL(dev_err_probe);
4424 static inline bool fwnode_is_primary(struct fwnode_handle *fwnode)
4426 return fwnode && !IS_ERR(fwnode->secondary);
4430 * set_primary_fwnode - Change the primary firmware node of a given device.
4431 * @dev: Device to handle.
4432 * @fwnode: New primary firmware node of the device.
4434 * Set the device's firmware node pointer to @fwnode, but if a secondary
4435 * firmware node of the device is present, preserve it.
4437 void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
4439 struct device *parent = dev->parent;
4440 struct fwnode_handle *fn = dev->fwnode;
4443 if (fwnode_is_primary(fn))
4447 WARN_ON(fwnode->secondary);
4448 fwnode->secondary = fn;
4450 dev->fwnode = fwnode;
4452 if (fwnode_is_primary(fn)) {
4453 dev->fwnode = fn->secondary;
4454 if (!(parent && fn == parent->fwnode))
4455 fn->secondary = ERR_PTR(-ENODEV);
4461 EXPORT_SYMBOL_GPL(set_primary_fwnode);
4464 * set_secondary_fwnode - Change the secondary firmware node of a given device.
4465 * @dev: Device to handle.
4466 * @fwnode: New secondary firmware node of the device.
4468 * If a primary firmware node of the device is present, set its secondary
4469 * pointer to @fwnode. Otherwise, set the device's firmware node pointer to
4472 void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
4475 fwnode->secondary = ERR_PTR(-ENODEV);
4477 if (fwnode_is_primary(dev->fwnode))
4478 dev->fwnode->secondary = fwnode;
4480 dev->fwnode = fwnode;
4482 EXPORT_SYMBOL_GPL(set_secondary_fwnode);
4485 * device_set_of_node_from_dev - reuse device-tree node of another device
4486 * @dev: device whose device-tree node is being set
4487 * @dev2: device whose device-tree node is being reused
4489 * Takes another reference to the new device-tree node after first dropping
4490 * any reference held to the old node.
4492 void device_set_of_node_from_dev(struct device *dev, const struct device *dev2)
4494 of_node_put(dev->of_node);
4495 dev->of_node = of_node_get(dev2->of_node);
4496 dev->of_node_reused = true;
4498 EXPORT_SYMBOL_GPL(device_set_of_node_from_dev);
4500 int device_match_name(struct device *dev, const void *name)
4502 return sysfs_streq(dev_name(dev), name);
4504 EXPORT_SYMBOL_GPL(device_match_name);
4506 int device_match_of_node(struct device *dev, const void *np)
4508 return dev->of_node == np;
4510 EXPORT_SYMBOL_GPL(device_match_of_node);
4512 int device_match_fwnode(struct device *dev, const void *fwnode)
4514 return dev_fwnode(dev) == fwnode;
4516 EXPORT_SYMBOL_GPL(device_match_fwnode);
4518 int device_match_devt(struct device *dev, const void *pdevt)
4520 return dev->devt == *(dev_t *)pdevt;
4522 EXPORT_SYMBOL_GPL(device_match_devt);
4524 int device_match_acpi_dev(struct device *dev, const void *adev)
4526 return ACPI_COMPANION(dev) == adev;
4528 EXPORT_SYMBOL(device_match_acpi_dev);
4530 int device_match_any(struct device *dev, const void *unused)
4534 EXPORT_SYMBOL_GPL(device_match_any);