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/blkdev.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/swiotlb.h>
31 #include <linux/sysfs.h>
32 #include <linux/dma-map-ops.h> /* for dma_default_coherent */
35 #include "physical_location.h"
36 #include "power/power.h"
38 #ifdef CONFIG_SYSFS_DEPRECATED
39 #ifdef CONFIG_SYSFS_DEPRECATED_V2
40 long sysfs_deprecated = 1;
42 long sysfs_deprecated = 0;
44 static int __init sysfs_deprecated_setup(char *arg)
46 return kstrtol(arg, 10, &sysfs_deprecated);
48 early_param("sysfs.deprecated", sysfs_deprecated_setup);
51 /* Device links support. */
52 static LIST_HEAD(deferred_sync);
53 static unsigned int defer_sync_state_count = 1;
54 static DEFINE_MUTEX(fwnode_link_lock);
55 static bool fw_devlink_is_permissive(void);
56 static bool fw_devlink_drv_reg_done;
57 static bool fw_devlink_best_effort;
60 * fwnode_link_add - Create a link between two fwnode_handles.
61 * @con: Consumer end of the link.
62 * @sup: Supplier end of the link.
64 * Create a fwnode link between fwnode handles @con and @sup. The fwnode link
65 * represents the detail that the firmware lists @sup fwnode as supplying a
68 * The driver core will use the fwnode link to create a device link between the
69 * two device objects corresponding to @con and @sup when they are created. The
70 * driver core will automatically delete the fwnode link between @con and @sup
73 * Attempts to create duplicate links between the same pair of fwnode handles
74 * are ignored and there is no reference counting.
76 int fwnode_link_add(struct fwnode_handle *con, struct fwnode_handle *sup)
78 struct fwnode_link *link;
81 mutex_lock(&fwnode_link_lock);
83 list_for_each_entry(link, &sup->consumers, s_hook)
84 if (link->consumer == con)
87 link = kzalloc(sizeof(*link), GFP_KERNEL);
94 INIT_LIST_HEAD(&link->s_hook);
96 INIT_LIST_HEAD(&link->c_hook);
98 list_add(&link->s_hook, &sup->consumers);
99 list_add(&link->c_hook, &con->suppliers);
100 pr_debug("%pfwP Linked as a fwnode consumer to %pfwP\n",
103 mutex_unlock(&fwnode_link_lock);
109 * __fwnode_link_del - Delete a link between two fwnode_handles.
110 * @link: the fwnode_link to be deleted
112 * The fwnode_link_lock needs to be held when this function is called.
114 static void __fwnode_link_del(struct fwnode_link *link)
116 pr_debug("%pfwP Dropping the fwnode link to %pfwP\n",
117 link->consumer, link->supplier);
118 list_del(&link->s_hook);
119 list_del(&link->c_hook);
124 * fwnode_links_purge_suppliers - Delete all supplier links of fwnode_handle.
125 * @fwnode: fwnode whose supplier links need to be deleted
127 * Deletes all supplier links connecting directly to @fwnode.
129 static void fwnode_links_purge_suppliers(struct fwnode_handle *fwnode)
131 struct fwnode_link *link, *tmp;
133 mutex_lock(&fwnode_link_lock);
134 list_for_each_entry_safe(link, tmp, &fwnode->suppliers, c_hook)
135 __fwnode_link_del(link);
136 mutex_unlock(&fwnode_link_lock);
140 * fwnode_links_purge_consumers - Delete all consumer links of fwnode_handle.
141 * @fwnode: fwnode whose consumer links need to be deleted
143 * Deletes all consumer links connecting directly to @fwnode.
145 static void fwnode_links_purge_consumers(struct fwnode_handle *fwnode)
147 struct fwnode_link *link, *tmp;
149 mutex_lock(&fwnode_link_lock);
150 list_for_each_entry_safe(link, tmp, &fwnode->consumers, s_hook)
151 __fwnode_link_del(link);
152 mutex_unlock(&fwnode_link_lock);
156 * fwnode_links_purge - Delete all links connected to a fwnode_handle.
157 * @fwnode: fwnode whose links needs to be deleted
159 * Deletes all links connecting directly to a fwnode.
161 void fwnode_links_purge(struct fwnode_handle *fwnode)
163 fwnode_links_purge_suppliers(fwnode);
164 fwnode_links_purge_consumers(fwnode);
167 void fw_devlink_purge_absent_suppliers(struct fwnode_handle *fwnode)
169 struct fwnode_handle *child;
171 /* Don't purge consumer links of an added child */
175 fwnode->flags |= FWNODE_FLAG_NOT_DEVICE;
176 fwnode_links_purge_consumers(fwnode);
178 fwnode_for_each_available_child_node(fwnode, child)
179 fw_devlink_purge_absent_suppliers(child);
181 EXPORT_SYMBOL_GPL(fw_devlink_purge_absent_suppliers);
184 static DEFINE_MUTEX(device_links_lock);
185 DEFINE_STATIC_SRCU(device_links_srcu);
187 static inline void device_links_write_lock(void)
189 mutex_lock(&device_links_lock);
192 static inline void device_links_write_unlock(void)
194 mutex_unlock(&device_links_lock);
197 int device_links_read_lock(void) __acquires(&device_links_srcu)
199 return srcu_read_lock(&device_links_srcu);
202 void device_links_read_unlock(int idx) __releases(&device_links_srcu)
204 srcu_read_unlock(&device_links_srcu, idx);
207 int device_links_read_lock_held(void)
209 return srcu_read_lock_held(&device_links_srcu);
212 static void device_link_synchronize_removal(void)
214 synchronize_srcu(&device_links_srcu);
217 static void device_link_remove_from_lists(struct device_link *link)
219 list_del_rcu(&link->s_node);
220 list_del_rcu(&link->c_node);
222 #else /* !CONFIG_SRCU */
223 static DECLARE_RWSEM(device_links_lock);
225 static inline void device_links_write_lock(void)
227 down_write(&device_links_lock);
230 static inline void device_links_write_unlock(void)
232 up_write(&device_links_lock);
235 int device_links_read_lock(void)
237 down_read(&device_links_lock);
241 void device_links_read_unlock(int not_used)
243 up_read(&device_links_lock);
246 #ifdef CONFIG_DEBUG_LOCK_ALLOC
247 int device_links_read_lock_held(void)
249 return lockdep_is_held(&device_links_lock);
253 static inline void device_link_synchronize_removal(void)
257 static void device_link_remove_from_lists(struct device_link *link)
259 list_del(&link->s_node);
260 list_del(&link->c_node);
262 #endif /* !CONFIG_SRCU */
264 static bool device_is_ancestor(struct device *dev, struct device *target)
266 while (target->parent) {
267 target = target->parent;
274 static inline bool device_link_flag_is_sync_state_only(u32 flags)
276 return (flags & ~(DL_FLAG_INFERRED | DL_FLAG_CYCLE)) ==
277 (DL_FLAG_SYNC_STATE_ONLY | DL_FLAG_MANAGED);
281 * device_is_dependent - Check if one device depends on another one
282 * @dev: Device to check dependencies for.
283 * @target: Device to check against.
285 * Check if @target depends on @dev or any device dependent on it (its child or
286 * its consumer etc). Return 1 if that is the case or 0 otherwise.
288 int device_is_dependent(struct device *dev, void *target)
290 struct device_link *link;
294 * The "ancestors" check is needed to catch the case when the target
295 * device has not been completely initialized yet and it is still
296 * missing from the list of children of its parent device.
298 if (dev == target || device_is_ancestor(dev, target))
301 ret = device_for_each_child(dev, target, device_is_dependent);
305 list_for_each_entry(link, &dev->links.consumers, s_node) {
306 if (device_link_flag_is_sync_state_only(link->flags))
309 if (link->consumer == target)
312 ret = device_is_dependent(link->consumer, target);
319 static void device_link_init_status(struct device_link *link,
320 struct device *consumer,
321 struct device *supplier)
323 switch (supplier->links.status) {
325 switch (consumer->links.status) {
328 * A consumer driver can create a link to a supplier
329 * that has not completed its probing yet as long as it
330 * knows that the supplier is already functional (for
331 * example, it has just acquired some resources from the
334 link->status = DL_STATE_CONSUMER_PROBE;
337 link->status = DL_STATE_DORMANT;
341 case DL_DEV_DRIVER_BOUND:
342 switch (consumer->links.status) {
344 link->status = DL_STATE_CONSUMER_PROBE;
346 case DL_DEV_DRIVER_BOUND:
347 link->status = DL_STATE_ACTIVE;
350 link->status = DL_STATE_AVAILABLE;
354 case DL_DEV_UNBINDING:
355 link->status = DL_STATE_SUPPLIER_UNBIND;
358 link->status = DL_STATE_DORMANT;
363 static int device_reorder_to_tail(struct device *dev, void *not_used)
365 struct device_link *link;
368 * Devices that have not been registered yet will be put to the ends
369 * of the lists during the registration, so skip them here.
371 if (device_is_registered(dev))
372 devices_kset_move_last(dev);
374 if (device_pm_initialized(dev))
375 device_pm_move_last(dev);
377 device_for_each_child(dev, NULL, device_reorder_to_tail);
378 list_for_each_entry(link, &dev->links.consumers, s_node) {
379 if (device_link_flag_is_sync_state_only(link->flags))
381 device_reorder_to_tail(link->consumer, NULL);
388 * device_pm_move_to_tail - Move set of devices to the end of device lists
389 * @dev: Device to move
391 * This is a device_reorder_to_tail() wrapper taking the requisite locks.
393 * It moves the @dev along with all of its children and all of its consumers
394 * to the ends of the device_kset and dpm_list, recursively.
396 void device_pm_move_to_tail(struct device *dev)
400 idx = device_links_read_lock();
402 device_reorder_to_tail(dev, NULL);
404 device_links_read_unlock(idx);
407 #define to_devlink(dev) container_of((dev), struct device_link, link_dev)
409 static ssize_t status_show(struct device *dev,
410 struct device_attribute *attr, char *buf)
414 switch (to_devlink(dev)->status) {
416 output = "not tracked";
418 case DL_STATE_DORMANT:
421 case DL_STATE_AVAILABLE:
422 output = "available";
424 case DL_STATE_CONSUMER_PROBE:
425 output = "consumer probing";
427 case DL_STATE_ACTIVE:
430 case DL_STATE_SUPPLIER_UNBIND:
431 output = "supplier unbinding";
438 return sysfs_emit(buf, "%s\n", output);
440 static DEVICE_ATTR_RO(status);
442 static ssize_t auto_remove_on_show(struct device *dev,
443 struct device_attribute *attr, char *buf)
445 struct device_link *link = to_devlink(dev);
448 if (link->flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
449 output = "supplier unbind";
450 else if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER)
451 output = "consumer unbind";
455 return sysfs_emit(buf, "%s\n", output);
457 static DEVICE_ATTR_RO(auto_remove_on);
459 static ssize_t runtime_pm_show(struct device *dev,
460 struct device_attribute *attr, char *buf)
462 struct device_link *link = to_devlink(dev);
464 return sysfs_emit(buf, "%d\n", !!(link->flags & DL_FLAG_PM_RUNTIME));
466 static DEVICE_ATTR_RO(runtime_pm);
468 static ssize_t sync_state_only_show(struct device *dev,
469 struct device_attribute *attr, char *buf)
471 struct device_link *link = to_devlink(dev);
473 return sysfs_emit(buf, "%d\n",
474 !!(link->flags & DL_FLAG_SYNC_STATE_ONLY));
476 static DEVICE_ATTR_RO(sync_state_only);
478 static struct attribute *devlink_attrs[] = {
479 &dev_attr_status.attr,
480 &dev_attr_auto_remove_on.attr,
481 &dev_attr_runtime_pm.attr,
482 &dev_attr_sync_state_only.attr,
485 ATTRIBUTE_GROUPS(devlink);
487 static void device_link_release_fn(struct work_struct *work)
489 struct device_link *link = container_of(work, struct device_link, rm_work);
491 /* Ensure that all references to the link object have been dropped. */
492 device_link_synchronize_removal();
494 pm_runtime_release_supplier(link);
496 * If supplier_preactivated is set, the link has been dropped between
497 * the pm_runtime_get_suppliers() and pm_runtime_put_suppliers() calls
498 * in __driver_probe_device(). In that case, drop the supplier's
499 * PM-runtime usage counter to remove the reference taken by
500 * pm_runtime_get_suppliers().
502 if (link->supplier_preactivated)
503 pm_runtime_put_noidle(link->supplier);
505 pm_request_idle(link->supplier);
507 put_device(link->consumer);
508 put_device(link->supplier);
512 static void devlink_dev_release(struct device *dev)
514 struct device_link *link = to_devlink(dev);
516 INIT_WORK(&link->rm_work, device_link_release_fn);
518 * It may take a while to complete this work because of the SRCU
519 * synchronization in device_link_release_fn() and if the consumer or
520 * supplier devices get deleted when it runs, so put it into the "long"
523 queue_work(system_long_wq, &link->rm_work);
526 static struct class devlink_class = {
528 .owner = THIS_MODULE,
529 .dev_groups = devlink_groups,
530 .dev_release = devlink_dev_release,
533 static int devlink_add_symlinks(struct device *dev,
534 struct class_interface *class_intf)
538 struct device_link *link = to_devlink(dev);
539 struct device *sup = link->supplier;
540 struct device *con = link->consumer;
543 len = max(strlen(dev_bus_name(sup)) + strlen(dev_name(sup)),
544 strlen(dev_bus_name(con)) + strlen(dev_name(con)));
546 len += strlen("supplier:") + 1;
547 buf = kzalloc(len, GFP_KERNEL);
551 ret = sysfs_create_link(&link->link_dev.kobj, &sup->kobj, "supplier");
555 ret = sysfs_create_link(&link->link_dev.kobj, &con->kobj, "consumer");
559 snprintf(buf, len, "consumer:%s:%s", dev_bus_name(con), dev_name(con));
560 ret = sysfs_create_link(&sup->kobj, &link->link_dev.kobj, buf);
564 snprintf(buf, len, "supplier:%s:%s", dev_bus_name(sup), dev_name(sup));
565 ret = sysfs_create_link(&con->kobj, &link->link_dev.kobj, buf);
572 snprintf(buf, len, "consumer:%s:%s", dev_bus_name(con), dev_name(con));
573 sysfs_remove_link(&sup->kobj, buf);
575 sysfs_remove_link(&link->link_dev.kobj, "consumer");
577 sysfs_remove_link(&link->link_dev.kobj, "supplier");
583 static void devlink_remove_symlinks(struct device *dev,
584 struct class_interface *class_intf)
586 struct device_link *link = to_devlink(dev);
588 struct device *sup = link->supplier;
589 struct device *con = link->consumer;
592 sysfs_remove_link(&link->link_dev.kobj, "consumer");
593 sysfs_remove_link(&link->link_dev.kobj, "supplier");
595 len = max(strlen(dev_bus_name(sup)) + strlen(dev_name(sup)),
596 strlen(dev_bus_name(con)) + strlen(dev_name(con)));
598 len += strlen("supplier:") + 1;
599 buf = kzalloc(len, GFP_KERNEL);
601 WARN(1, "Unable to properly free device link symlinks!\n");
605 if (device_is_registered(con)) {
606 snprintf(buf, len, "supplier:%s:%s", dev_bus_name(sup), dev_name(sup));
607 sysfs_remove_link(&con->kobj, buf);
609 snprintf(buf, len, "consumer:%s:%s", dev_bus_name(con), dev_name(con));
610 sysfs_remove_link(&sup->kobj, buf);
614 static struct class_interface devlink_class_intf = {
615 .class = &devlink_class,
616 .add_dev = devlink_add_symlinks,
617 .remove_dev = devlink_remove_symlinks,
620 static int __init devlink_class_init(void)
624 ret = class_register(&devlink_class);
628 ret = class_interface_register(&devlink_class_intf);
630 class_unregister(&devlink_class);
634 postcore_initcall(devlink_class_init);
636 #define DL_MANAGED_LINK_FLAGS (DL_FLAG_AUTOREMOVE_CONSUMER | \
637 DL_FLAG_AUTOREMOVE_SUPPLIER | \
638 DL_FLAG_AUTOPROBE_CONSUMER | \
639 DL_FLAG_SYNC_STATE_ONLY | \
643 #define DL_ADD_VALID_FLAGS (DL_MANAGED_LINK_FLAGS | DL_FLAG_STATELESS | \
644 DL_FLAG_PM_RUNTIME | DL_FLAG_RPM_ACTIVE)
647 * device_link_add - Create a link between two devices.
648 * @consumer: Consumer end of the link.
649 * @supplier: Supplier end of the link.
650 * @flags: Link flags.
652 * The caller is responsible for the proper synchronization of the link creation
653 * with runtime PM. First, setting the DL_FLAG_PM_RUNTIME flag will cause the
654 * runtime PM framework to take the link into account. Second, if the
655 * DL_FLAG_RPM_ACTIVE flag is set in addition to it, the supplier devices will
656 * be forced into the active meta state and reference-counted upon the creation
657 * of the link. If DL_FLAG_PM_RUNTIME is not set, DL_FLAG_RPM_ACTIVE will be
660 * If DL_FLAG_STATELESS is set in @flags, the caller of this function is
661 * expected to release the link returned by it directly with the help of either
662 * device_link_del() or device_link_remove().
664 * If that flag is not set, however, the caller of this function is handing the
665 * management of the link over to the driver core entirely and its return value
666 * can only be used to check whether or not the link is present. In that case,
667 * the DL_FLAG_AUTOREMOVE_CONSUMER and DL_FLAG_AUTOREMOVE_SUPPLIER device link
668 * flags can be used to indicate to the driver core when the link can be safely
669 * deleted. Namely, setting one of them in @flags indicates to the driver core
670 * that the link is not going to be used (by the given caller of this function)
671 * after unbinding the consumer or supplier driver, respectively, from its
672 * device, so the link can be deleted at that point. If none of them is set,
673 * the link will be maintained until one of the devices pointed to by it (either
674 * the consumer or the supplier) is unregistered.
676 * Also, if DL_FLAG_STATELESS, DL_FLAG_AUTOREMOVE_CONSUMER and
677 * DL_FLAG_AUTOREMOVE_SUPPLIER are not set in @flags (that is, a persistent
678 * managed device link is being added), the DL_FLAG_AUTOPROBE_CONSUMER flag can
679 * be used to request the driver core to automatically probe for a consumer
680 * driver after successfully binding a driver to the supplier device.
682 * The combination of DL_FLAG_STATELESS and one of DL_FLAG_AUTOREMOVE_CONSUMER,
683 * DL_FLAG_AUTOREMOVE_SUPPLIER, or DL_FLAG_AUTOPROBE_CONSUMER set in @flags at
684 * the same time is invalid and will cause NULL to be returned upfront.
685 * However, if a device link between the given @consumer and @supplier pair
686 * exists already when this function is called for them, the existing link will
687 * be returned regardless of its current type and status (the link's flags may
688 * be modified then). The caller of this function is then expected to treat
689 * the link as though it has just been created, so (in particular) if
690 * DL_FLAG_STATELESS was passed in @flags, the link needs to be released
691 * explicitly when not needed any more (as stated above).
693 * A side effect of the link creation is re-ordering of dpm_list and the
694 * devices_kset list by moving the consumer device and all devices depending
695 * on it to the ends of these lists (that does not happen to devices that have
696 * not been registered when this function is called).
698 * The supplier device is required to be registered when this function is called
699 * and NULL will be returned if that is not the case. The consumer device need
700 * not be registered, however.
702 struct device_link *device_link_add(struct device *consumer,
703 struct device *supplier, u32 flags)
705 struct device_link *link;
707 if (!consumer || !supplier || consumer == supplier ||
708 flags & ~DL_ADD_VALID_FLAGS ||
709 (flags & DL_FLAG_STATELESS && flags & DL_MANAGED_LINK_FLAGS) ||
710 (flags & DL_FLAG_AUTOPROBE_CONSUMER &&
711 flags & (DL_FLAG_AUTOREMOVE_CONSUMER |
712 DL_FLAG_AUTOREMOVE_SUPPLIER)))
715 if (flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) {
716 if (pm_runtime_get_sync(supplier) < 0) {
717 pm_runtime_put_noidle(supplier);
722 if (!(flags & DL_FLAG_STATELESS))
723 flags |= DL_FLAG_MANAGED;
725 if (flags & DL_FLAG_SYNC_STATE_ONLY &&
726 !device_link_flag_is_sync_state_only(flags))
729 device_links_write_lock();
733 * If the supplier has not been fully registered yet or there is a
734 * reverse (non-SYNC_STATE_ONLY) dependency between the consumer and
735 * the supplier already in the graph, return NULL. If the link is a
736 * SYNC_STATE_ONLY link, we don't check for reverse dependencies
737 * because it only affects sync_state() callbacks.
739 if (!device_pm_initialized(supplier)
740 || (!(flags & DL_FLAG_SYNC_STATE_ONLY) &&
741 device_is_dependent(consumer, supplier))) {
747 * SYNC_STATE_ONLY links are useless once a consumer device has probed.
748 * So, only create it if the consumer hasn't probed yet.
750 if (flags & DL_FLAG_SYNC_STATE_ONLY &&
751 consumer->links.status != DL_DEV_NO_DRIVER &&
752 consumer->links.status != DL_DEV_PROBING) {
758 * DL_FLAG_AUTOREMOVE_SUPPLIER indicates that the link will be needed
759 * longer than for DL_FLAG_AUTOREMOVE_CONSUMER and setting them both
760 * together doesn't make sense, so prefer DL_FLAG_AUTOREMOVE_SUPPLIER.
762 if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
763 flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER;
765 list_for_each_entry(link, &supplier->links.consumers, s_node) {
766 if (link->consumer != consumer)
769 if (link->flags & DL_FLAG_INFERRED &&
770 !(flags & DL_FLAG_INFERRED))
771 link->flags &= ~DL_FLAG_INFERRED;
773 if (flags & DL_FLAG_PM_RUNTIME) {
774 if (!(link->flags & DL_FLAG_PM_RUNTIME)) {
775 pm_runtime_new_link(consumer);
776 link->flags |= DL_FLAG_PM_RUNTIME;
778 if (flags & DL_FLAG_RPM_ACTIVE)
779 refcount_inc(&link->rpm_active);
782 if (flags & DL_FLAG_STATELESS) {
783 kref_get(&link->kref);
784 if (link->flags & DL_FLAG_SYNC_STATE_ONLY &&
785 !(link->flags & DL_FLAG_STATELESS)) {
786 link->flags |= DL_FLAG_STATELESS;
789 link->flags |= DL_FLAG_STATELESS;
795 * If the life time of the link following from the new flags is
796 * longer than indicated by the flags of the existing link,
797 * update the existing link to stay around longer.
799 if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER) {
800 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER) {
801 link->flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER;
802 link->flags |= DL_FLAG_AUTOREMOVE_SUPPLIER;
804 } else if (!(flags & DL_FLAG_AUTOREMOVE_CONSUMER)) {
805 link->flags &= ~(DL_FLAG_AUTOREMOVE_CONSUMER |
806 DL_FLAG_AUTOREMOVE_SUPPLIER);
808 if (!(link->flags & DL_FLAG_MANAGED)) {
809 kref_get(&link->kref);
810 link->flags |= DL_FLAG_MANAGED;
811 device_link_init_status(link, consumer, supplier);
813 if (link->flags & DL_FLAG_SYNC_STATE_ONLY &&
814 !(flags & DL_FLAG_SYNC_STATE_ONLY)) {
815 link->flags &= ~DL_FLAG_SYNC_STATE_ONLY;
822 link = kzalloc(sizeof(*link), GFP_KERNEL);
826 refcount_set(&link->rpm_active, 1);
828 get_device(supplier);
829 link->supplier = supplier;
830 INIT_LIST_HEAD(&link->s_node);
831 get_device(consumer);
832 link->consumer = consumer;
833 INIT_LIST_HEAD(&link->c_node);
835 kref_init(&link->kref);
837 link->link_dev.class = &devlink_class;
838 device_set_pm_not_required(&link->link_dev);
839 dev_set_name(&link->link_dev, "%s:%s--%s:%s",
840 dev_bus_name(supplier), dev_name(supplier),
841 dev_bus_name(consumer), dev_name(consumer));
842 if (device_register(&link->link_dev)) {
843 put_device(&link->link_dev);
848 if (flags & DL_FLAG_PM_RUNTIME) {
849 if (flags & DL_FLAG_RPM_ACTIVE)
850 refcount_inc(&link->rpm_active);
852 pm_runtime_new_link(consumer);
855 /* Determine the initial link state. */
856 if (flags & DL_FLAG_STATELESS)
857 link->status = DL_STATE_NONE;
859 device_link_init_status(link, consumer, supplier);
862 * Some callers expect the link creation during consumer driver probe to
863 * resume the supplier even without DL_FLAG_RPM_ACTIVE.
865 if (link->status == DL_STATE_CONSUMER_PROBE &&
866 flags & DL_FLAG_PM_RUNTIME)
867 pm_runtime_resume(supplier);
869 list_add_tail_rcu(&link->s_node, &supplier->links.consumers);
870 list_add_tail_rcu(&link->c_node, &consumer->links.suppliers);
872 if (flags & DL_FLAG_SYNC_STATE_ONLY) {
874 "Linked as a sync state only consumer to %s\n",
881 * Move the consumer and all of the devices depending on it to the end
882 * of dpm_list and the devices_kset list.
884 * It is necessary to hold dpm_list locked throughout all that or else
885 * we may end up suspending with a wrong ordering of it.
887 device_reorder_to_tail(consumer, NULL);
889 dev_dbg(consumer, "Linked as a consumer to %s\n", dev_name(supplier));
893 device_links_write_unlock();
895 if ((flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) && !link)
896 pm_runtime_put(supplier);
900 EXPORT_SYMBOL_GPL(device_link_add);
902 static void __device_link_del(struct kref *kref)
904 struct device_link *link = container_of(kref, struct device_link, kref);
906 dev_dbg(link->consumer, "Dropping the link to %s\n",
907 dev_name(link->supplier));
909 pm_runtime_drop_link(link);
911 device_link_remove_from_lists(link);
912 device_unregister(&link->link_dev);
915 static void device_link_put_kref(struct device_link *link)
917 if (link->flags & DL_FLAG_STATELESS)
918 kref_put(&link->kref, __device_link_del);
919 else if (!device_is_registered(link->consumer))
920 __device_link_del(&link->kref);
922 WARN(1, "Unable to drop a managed device link reference\n");
926 * device_link_del - Delete a stateless link between two devices.
927 * @link: Device link to delete.
929 * The caller must ensure proper synchronization of this function with runtime
930 * PM. If the link was added multiple times, it needs to be deleted as often.
931 * Care is required for hotplugged devices: Their links are purged on removal
932 * and calling device_link_del() is then no longer allowed.
934 void device_link_del(struct device_link *link)
936 device_links_write_lock();
937 device_link_put_kref(link);
938 device_links_write_unlock();
940 EXPORT_SYMBOL_GPL(device_link_del);
943 * device_link_remove - Delete a stateless link between two devices.
944 * @consumer: Consumer end of the link.
945 * @supplier: Supplier end of the link.
947 * The caller must ensure proper synchronization of this function with runtime
950 void device_link_remove(void *consumer, struct device *supplier)
952 struct device_link *link;
954 if (WARN_ON(consumer == supplier))
957 device_links_write_lock();
959 list_for_each_entry(link, &supplier->links.consumers, s_node) {
960 if (link->consumer == consumer) {
961 device_link_put_kref(link);
966 device_links_write_unlock();
968 EXPORT_SYMBOL_GPL(device_link_remove);
970 static void device_links_missing_supplier(struct device *dev)
972 struct device_link *link;
974 list_for_each_entry(link, &dev->links.suppliers, c_node) {
975 if (link->status != DL_STATE_CONSUMER_PROBE)
978 if (link->supplier->links.status == DL_DEV_DRIVER_BOUND) {
979 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
981 WARN_ON(!(link->flags & DL_FLAG_SYNC_STATE_ONLY));
982 WRITE_ONCE(link->status, DL_STATE_DORMANT);
987 static bool dev_is_best_effort(struct device *dev)
989 return (fw_devlink_best_effort && dev->can_match) ||
990 (dev->fwnode && (dev->fwnode->flags & FWNODE_FLAG_BEST_EFFORT));
994 * device_links_check_suppliers - Check presence of supplier drivers.
995 * @dev: Consumer device.
997 * Check links from this device to any suppliers. Walk the list of the device's
998 * links to suppliers and see if all of them are available. If not, simply
999 * return -EPROBE_DEFER.
1001 * We need to guarantee that the supplier will not go away after the check has
1002 * been positive here. It only can go away in __device_release_driver() and
1003 * that function checks the device's links to consumers. This means we need to
1004 * mark the link as "consumer probe in progress" to make the supplier removal
1005 * wait for us to complete (or bad things may happen).
1007 * Links without the DL_FLAG_MANAGED flag set are ignored.
1009 int device_links_check_suppliers(struct device *dev)
1011 struct device_link *link;
1012 int ret = 0, fwnode_ret = 0;
1013 struct fwnode_handle *sup_fw;
1016 * Device waiting for supplier to become available is not allowed to
1019 mutex_lock(&fwnode_link_lock);
1020 if (dev->fwnode && !list_empty(&dev->fwnode->suppliers) &&
1021 !fw_devlink_is_permissive()) {
1022 sup_fw = list_first_entry(&dev->fwnode->suppliers,
1025 if (!dev_is_best_effort(dev)) {
1026 fwnode_ret = -EPROBE_DEFER;
1027 dev_err_probe(dev, -EPROBE_DEFER,
1028 "wait for supplier %pfwP\n", sup_fw);
1030 fwnode_ret = -EAGAIN;
1033 mutex_unlock(&fwnode_link_lock);
1034 if (fwnode_ret == -EPROBE_DEFER)
1037 device_links_write_lock();
1039 list_for_each_entry(link, &dev->links.suppliers, c_node) {
1040 if (!(link->flags & DL_FLAG_MANAGED))
1043 if (link->status != DL_STATE_AVAILABLE &&
1044 !(link->flags & DL_FLAG_SYNC_STATE_ONLY)) {
1046 if (dev_is_best_effort(dev) &&
1047 link->flags & DL_FLAG_INFERRED &&
1048 !link->supplier->can_match) {
1053 device_links_missing_supplier(dev);
1054 dev_err_probe(dev, -EPROBE_DEFER,
1055 "supplier %s not ready\n",
1056 dev_name(link->supplier));
1057 ret = -EPROBE_DEFER;
1060 WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE);
1062 dev->links.status = DL_DEV_PROBING;
1064 device_links_write_unlock();
1066 return ret ? ret : fwnode_ret;
1070 * __device_links_queue_sync_state - Queue a device for sync_state() callback
1071 * @dev: Device to call sync_state() on
1072 * @list: List head to queue the @dev on
1074 * Queues a device for a sync_state() callback when the device links write lock
1075 * isn't held. This allows the sync_state() execution flow to use device links
1076 * APIs. The caller must ensure this function is called with
1077 * device_links_write_lock() held.
1079 * This function does a get_device() to make sure the device is not freed while
1082 * So the caller must also ensure that device_links_flush_sync_list() is called
1083 * as soon as the caller releases device_links_write_lock(). This is necessary
1084 * to make sure the sync_state() is called in a timely fashion and the
1085 * put_device() is called on this device.
1087 static void __device_links_queue_sync_state(struct device *dev,
1088 struct list_head *list)
1090 struct device_link *link;
1092 if (!dev_has_sync_state(dev))
1094 if (dev->state_synced)
1097 list_for_each_entry(link, &dev->links.consumers, s_node) {
1098 if (!(link->flags & DL_FLAG_MANAGED))
1100 if (link->status != DL_STATE_ACTIVE)
1105 * Set the flag here to avoid adding the same device to a list more
1106 * than once. This can happen if new consumers get added to the device
1107 * and probed before the list is flushed.
1109 dev->state_synced = true;
1111 if (WARN_ON(!list_empty(&dev->links.defer_sync)))
1115 list_add_tail(&dev->links.defer_sync, list);
1119 * device_links_flush_sync_list - Call sync_state() on a list of devices
1120 * @list: List of devices to call sync_state() on
1121 * @dont_lock_dev: Device for which lock is already held by the caller
1123 * Calls sync_state() on all the devices that have been queued for it. This
1124 * function is used in conjunction with __device_links_queue_sync_state(). The
1125 * @dont_lock_dev parameter is useful when this function is called from a
1126 * context where a device lock is already held.
1128 static void device_links_flush_sync_list(struct list_head *list,
1129 struct device *dont_lock_dev)
1131 struct device *dev, *tmp;
1133 list_for_each_entry_safe(dev, tmp, list, links.defer_sync) {
1134 list_del_init(&dev->links.defer_sync);
1136 if (dev != dont_lock_dev)
1139 if (dev->bus->sync_state)
1140 dev->bus->sync_state(dev);
1141 else if (dev->driver && dev->driver->sync_state)
1142 dev->driver->sync_state(dev);
1144 if (dev != dont_lock_dev)
1151 void device_links_supplier_sync_state_pause(void)
1153 device_links_write_lock();
1154 defer_sync_state_count++;
1155 device_links_write_unlock();
1158 void device_links_supplier_sync_state_resume(void)
1160 struct device *dev, *tmp;
1161 LIST_HEAD(sync_list);
1163 device_links_write_lock();
1164 if (!defer_sync_state_count) {
1165 WARN(true, "Unmatched sync_state pause/resume!");
1168 defer_sync_state_count--;
1169 if (defer_sync_state_count)
1172 list_for_each_entry_safe(dev, tmp, &deferred_sync, links.defer_sync) {
1174 * Delete from deferred_sync list before queuing it to
1175 * sync_list because defer_sync is used for both lists.
1177 list_del_init(&dev->links.defer_sync);
1178 __device_links_queue_sync_state(dev, &sync_list);
1181 device_links_write_unlock();
1183 device_links_flush_sync_list(&sync_list, NULL);
1186 static int sync_state_resume_initcall(void)
1188 device_links_supplier_sync_state_resume();
1191 late_initcall(sync_state_resume_initcall);
1193 static void __device_links_supplier_defer_sync(struct device *sup)
1195 if (list_empty(&sup->links.defer_sync) && dev_has_sync_state(sup))
1196 list_add_tail(&sup->links.defer_sync, &deferred_sync);
1199 static void device_link_drop_managed(struct device_link *link)
1201 link->flags &= ~DL_FLAG_MANAGED;
1202 WRITE_ONCE(link->status, DL_STATE_NONE);
1203 kref_put(&link->kref, __device_link_del);
1206 static ssize_t waiting_for_supplier_show(struct device *dev,
1207 struct device_attribute *attr,
1213 val = !list_empty(&dev->fwnode->suppliers);
1215 return sysfs_emit(buf, "%u\n", val);
1217 static DEVICE_ATTR_RO(waiting_for_supplier);
1220 * device_links_force_bind - Prepares device to be force bound
1221 * @dev: Consumer device.
1223 * device_bind_driver() force binds a device to a driver without calling any
1224 * driver probe functions. So the consumer really isn't going to wait for any
1225 * supplier before it's bound to the driver. We still want the device link
1226 * states to be sensible when this happens.
1228 * In preparation for device_bind_driver(), this function goes through each
1229 * supplier device links and checks if the supplier is bound. If it is, then
1230 * the device link status is set to CONSUMER_PROBE. Otherwise, the device link
1231 * is dropped. Links without the DL_FLAG_MANAGED flag set are ignored.
1233 void device_links_force_bind(struct device *dev)
1235 struct device_link *link, *ln;
1237 device_links_write_lock();
1239 list_for_each_entry_safe(link, ln, &dev->links.suppliers, c_node) {
1240 if (!(link->flags & DL_FLAG_MANAGED))
1243 if (link->status != DL_STATE_AVAILABLE) {
1244 device_link_drop_managed(link);
1247 WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE);
1249 dev->links.status = DL_DEV_PROBING;
1251 device_links_write_unlock();
1255 * device_links_driver_bound - Update device links after probing its driver.
1256 * @dev: Device to update the links for.
1258 * The probe has been successful, so update links from this device to any
1259 * consumers by changing their status to "available".
1261 * Also change the status of @dev's links to suppliers to "active".
1263 * Links without the DL_FLAG_MANAGED flag set are ignored.
1265 void device_links_driver_bound(struct device *dev)
1267 struct device_link *link, *ln;
1268 LIST_HEAD(sync_list);
1271 * If a device binds successfully, it's expected to have created all
1272 * the device links it needs to or make new device links as it needs
1273 * them. So, fw_devlink no longer needs to create device links to any
1274 * of the device's suppliers.
1276 * Also, if a child firmware node of this bound device is not added as
1277 * a device by now, assume it is never going to be added and make sure
1278 * other devices don't defer probe indefinitely by waiting for such a
1281 if (dev->fwnode && dev->fwnode->dev == dev) {
1282 struct fwnode_handle *child;
1283 fwnode_links_purge_suppliers(dev->fwnode);
1284 fwnode_for_each_available_child_node(dev->fwnode, child)
1285 fw_devlink_purge_absent_suppliers(child);
1287 device_remove_file(dev, &dev_attr_waiting_for_supplier);
1289 device_links_write_lock();
1291 list_for_each_entry(link, &dev->links.consumers, s_node) {
1292 if (!(link->flags & DL_FLAG_MANAGED))
1296 * Links created during consumer probe may be in the "consumer
1297 * probe" state to start with if the supplier is still probing
1298 * when they are created and they may become "active" if the
1299 * consumer probe returns first. Skip them here.
1301 if (link->status == DL_STATE_CONSUMER_PROBE ||
1302 link->status == DL_STATE_ACTIVE)
1305 WARN_ON(link->status != DL_STATE_DORMANT);
1306 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
1308 if (link->flags & DL_FLAG_AUTOPROBE_CONSUMER)
1309 driver_deferred_probe_add(link->consumer);
1312 if (defer_sync_state_count)
1313 __device_links_supplier_defer_sync(dev);
1315 __device_links_queue_sync_state(dev, &sync_list);
1317 list_for_each_entry_safe(link, ln, &dev->links.suppliers, c_node) {
1318 struct device *supplier;
1320 if (!(link->flags & DL_FLAG_MANAGED))
1323 supplier = link->supplier;
1324 if (link->flags & DL_FLAG_SYNC_STATE_ONLY) {
1326 * When DL_FLAG_SYNC_STATE_ONLY is set, it means no
1327 * other DL_MANAGED_LINK_FLAGS have been set. So, it's
1328 * save to drop the managed link completely.
1330 device_link_drop_managed(link);
1331 } else if (dev_is_best_effort(dev) &&
1332 link->flags & DL_FLAG_INFERRED &&
1333 link->status != DL_STATE_CONSUMER_PROBE &&
1334 !link->supplier->can_match) {
1336 * When dev_is_best_effort() is true, we ignore device
1337 * links to suppliers that don't have a driver. If the
1338 * consumer device still managed to probe, there's no
1339 * point in maintaining a device link in a weird state
1340 * (consumer probed before supplier). So delete it.
1342 device_link_drop_managed(link);
1344 WARN_ON(link->status != DL_STATE_CONSUMER_PROBE);
1345 WRITE_ONCE(link->status, DL_STATE_ACTIVE);
1349 * This needs to be done even for the deleted
1350 * DL_FLAG_SYNC_STATE_ONLY device link in case it was the last
1351 * device link that was preventing the supplier from getting a
1352 * sync_state() call.
1354 if (defer_sync_state_count)
1355 __device_links_supplier_defer_sync(supplier);
1357 __device_links_queue_sync_state(supplier, &sync_list);
1360 dev->links.status = DL_DEV_DRIVER_BOUND;
1362 device_links_write_unlock();
1364 device_links_flush_sync_list(&sync_list, dev);
1368 * __device_links_no_driver - Update links of a device without a driver.
1369 * @dev: Device without a drvier.
1371 * Delete all non-persistent links from this device to any suppliers.
1373 * Persistent links stay around, but their status is changed to "available",
1374 * unless they already are in the "supplier unbind in progress" state in which
1375 * case they need not be updated.
1377 * Links without the DL_FLAG_MANAGED flag set are ignored.
1379 static void __device_links_no_driver(struct device *dev)
1381 struct device_link *link, *ln;
1383 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
1384 if (!(link->flags & DL_FLAG_MANAGED))
1387 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER) {
1388 device_link_drop_managed(link);
1392 if (link->status != DL_STATE_CONSUMER_PROBE &&
1393 link->status != DL_STATE_ACTIVE)
1396 if (link->supplier->links.status == DL_DEV_DRIVER_BOUND) {
1397 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
1399 WARN_ON(!(link->flags & DL_FLAG_SYNC_STATE_ONLY));
1400 WRITE_ONCE(link->status, DL_STATE_DORMANT);
1404 dev->links.status = DL_DEV_NO_DRIVER;
1408 * device_links_no_driver - Update links after failing driver probe.
1409 * @dev: Device whose driver has just failed to probe.
1411 * Clean up leftover links to consumers for @dev and invoke
1412 * %__device_links_no_driver() to update links to suppliers for it as
1415 * Links without the DL_FLAG_MANAGED flag set are ignored.
1417 void device_links_no_driver(struct device *dev)
1419 struct device_link *link;
1421 device_links_write_lock();
1423 list_for_each_entry(link, &dev->links.consumers, s_node) {
1424 if (!(link->flags & DL_FLAG_MANAGED))
1428 * The probe has failed, so if the status of the link is
1429 * "consumer probe" or "active", it must have been added by
1430 * a probing consumer while this device was still probing.
1431 * Change its state to "dormant", as it represents a valid
1432 * relationship, but it is not functionally meaningful.
1434 if (link->status == DL_STATE_CONSUMER_PROBE ||
1435 link->status == DL_STATE_ACTIVE)
1436 WRITE_ONCE(link->status, DL_STATE_DORMANT);
1439 __device_links_no_driver(dev);
1441 device_links_write_unlock();
1445 * device_links_driver_cleanup - Update links after driver removal.
1446 * @dev: Device whose driver has just gone away.
1448 * Update links to consumers for @dev by changing their status to "dormant" and
1449 * invoke %__device_links_no_driver() to update links to suppliers for it as
1452 * Links without the DL_FLAG_MANAGED flag set are ignored.
1454 void device_links_driver_cleanup(struct device *dev)
1456 struct device_link *link, *ln;
1458 device_links_write_lock();
1460 list_for_each_entry_safe(link, ln, &dev->links.consumers, s_node) {
1461 if (!(link->flags & DL_FLAG_MANAGED))
1464 WARN_ON(link->flags & DL_FLAG_AUTOREMOVE_CONSUMER);
1465 WARN_ON(link->status != DL_STATE_SUPPLIER_UNBIND);
1468 * autoremove the links between this @dev and its consumer
1469 * devices that are not active, i.e. where the link state
1470 * has moved to DL_STATE_SUPPLIER_UNBIND.
1472 if (link->status == DL_STATE_SUPPLIER_UNBIND &&
1473 link->flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
1474 device_link_drop_managed(link);
1476 WRITE_ONCE(link->status, DL_STATE_DORMANT);
1479 list_del_init(&dev->links.defer_sync);
1480 __device_links_no_driver(dev);
1482 device_links_write_unlock();
1486 * device_links_busy - Check if there are any busy links to consumers.
1487 * @dev: Device to check.
1489 * Check each consumer of the device and return 'true' if its link's status
1490 * is one of "consumer probe" or "active" (meaning that the given consumer is
1491 * probing right now or its driver is present). Otherwise, change the link
1492 * state to "supplier unbind" to prevent the consumer from being probed
1493 * successfully going forward.
1495 * Return 'false' if there are no probing or active consumers.
1497 * Links without the DL_FLAG_MANAGED flag set are ignored.
1499 bool device_links_busy(struct device *dev)
1501 struct device_link *link;
1504 device_links_write_lock();
1506 list_for_each_entry(link, &dev->links.consumers, s_node) {
1507 if (!(link->flags & DL_FLAG_MANAGED))
1510 if (link->status == DL_STATE_CONSUMER_PROBE
1511 || link->status == DL_STATE_ACTIVE) {
1515 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
1518 dev->links.status = DL_DEV_UNBINDING;
1520 device_links_write_unlock();
1525 * device_links_unbind_consumers - Force unbind consumers of the given device.
1526 * @dev: Device to unbind the consumers of.
1528 * Walk the list of links to consumers for @dev and if any of them is in the
1529 * "consumer probe" state, wait for all device probes in progress to complete
1532 * If that's not the case, change the status of the link to "supplier unbind"
1533 * and check if the link was in the "active" state. If so, force the consumer
1534 * driver to unbind and start over (the consumer will not re-probe as we have
1535 * changed the state of the link already).
1537 * Links without the DL_FLAG_MANAGED flag set are ignored.
1539 void device_links_unbind_consumers(struct device *dev)
1541 struct device_link *link;
1544 device_links_write_lock();
1546 list_for_each_entry(link, &dev->links.consumers, s_node) {
1547 enum device_link_state status;
1549 if (!(link->flags & DL_FLAG_MANAGED) ||
1550 link->flags & DL_FLAG_SYNC_STATE_ONLY)
1553 status = link->status;
1554 if (status == DL_STATE_CONSUMER_PROBE) {
1555 device_links_write_unlock();
1557 wait_for_device_probe();
1560 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
1561 if (status == DL_STATE_ACTIVE) {
1562 struct device *consumer = link->consumer;
1564 get_device(consumer);
1566 device_links_write_unlock();
1568 device_release_driver_internal(consumer, NULL,
1570 put_device(consumer);
1575 device_links_write_unlock();
1579 * device_links_purge - Delete existing links to other devices.
1580 * @dev: Target device.
1582 static void device_links_purge(struct device *dev)
1584 struct device_link *link, *ln;
1586 if (dev->class == &devlink_class)
1590 * Delete all of the remaining links from this device to any other
1591 * devices (either consumers or suppliers).
1593 device_links_write_lock();
1595 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
1596 WARN_ON(link->status == DL_STATE_ACTIVE);
1597 __device_link_del(&link->kref);
1600 list_for_each_entry_safe_reverse(link, ln, &dev->links.consumers, s_node) {
1601 WARN_ON(link->status != DL_STATE_DORMANT &&
1602 link->status != DL_STATE_NONE);
1603 __device_link_del(&link->kref);
1606 device_links_write_unlock();
1609 #define FW_DEVLINK_FLAGS_PERMISSIVE (DL_FLAG_INFERRED | \
1610 DL_FLAG_SYNC_STATE_ONLY)
1611 #define FW_DEVLINK_FLAGS_ON (DL_FLAG_INFERRED | \
1612 DL_FLAG_AUTOPROBE_CONSUMER)
1613 #define FW_DEVLINK_FLAGS_RPM (FW_DEVLINK_FLAGS_ON | \
1616 static u32 fw_devlink_flags = FW_DEVLINK_FLAGS_ON;
1617 static int __init fw_devlink_setup(char *arg)
1622 if (strcmp(arg, "off") == 0) {
1623 fw_devlink_flags = 0;
1624 } else if (strcmp(arg, "permissive") == 0) {
1625 fw_devlink_flags = FW_DEVLINK_FLAGS_PERMISSIVE;
1626 } else if (strcmp(arg, "on") == 0) {
1627 fw_devlink_flags = FW_DEVLINK_FLAGS_ON;
1628 } else if (strcmp(arg, "rpm") == 0) {
1629 fw_devlink_flags = FW_DEVLINK_FLAGS_RPM;
1633 early_param("fw_devlink", fw_devlink_setup);
1635 static bool fw_devlink_strict;
1636 static int __init fw_devlink_strict_setup(char *arg)
1638 return strtobool(arg, &fw_devlink_strict);
1640 early_param("fw_devlink.strict", fw_devlink_strict_setup);
1642 u32 fw_devlink_get_flags(void)
1644 return fw_devlink_flags;
1647 static bool fw_devlink_is_permissive(void)
1649 return fw_devlink_flags == FW_DEVLINK_FLAGS_PERMISSIVE;
1652 bool fw_devlink_is_strict(void)
1654 return fw_devlink_strict && !fw_devlink_is_permissive();
1657 static void fw_devlink_parse_fwnode(struct fwnode_handle *fwnode)
1659 if (fwnode->flags & FWNODE_FLAG_LINKS_ADDED)
1662 fwnode_call_int_op(fwnode, add_links);
1663 fwnode->flags |= FWNODE_FLAG_LINKS_ADDED;
1666 static void fw_devlink_parse_fwtree(struct fwnode_handle *fwnode)
1668 struct fwnode_handle *child = NULL;
1670 fw_devlink_parse_fwnode(fwnode);
1672 while ((child = fwnode_get_next_available_child_node(fwnode, child)))
1673 fw_devlink_parse_fwtree(child);
1676 static void fw_devlink_relax_link(struct device_link *link)
1678 if (!(link->flags & DL_FLAG_INFERRED))
1681 if (device_link_flag_is_sync_state_only(link->flags))
1684 pm_runtime_drop_link(link);
1685 link->flags = DL_FLAG_MANAGED | FW_DEVLINK_FLAGS_PERMISSIVE;
1686 dev_dbg(link->consumer, "Relaxing link with %s\n",
1687 dev_name(link->supplier));
1690 static int fw_devlink_no_driver(struct device *dev, void *data)
1692 struct device_link *link = to_devlink(dev);
1694 if (!link->supplier->can_match)
1695 fw_devlink_relax_link(link);
1700 void fw_devlink_drivers_done(void)
1702 fw_devlink_drv_reg_done = true;
1703 device_links_write_lock();
1704 class_for_each_device(&devlink_class, NULL, NULL,
1705 fw_devlink_no_driver);
1706 device_links_write_unlock();
1710 * wait_for_init_devices_probe - Try to probe any device needed for init
1712 * Some devices might need to be probed and bound successfully before the kernel
1713 * boot sequence can finish and move on to init/userspace. For example, a
1714 * network interface might need to be bound to be able to mount a NFS rootfs.
1716 * With fw_devlink=on by default, some of these devices might be blocked from
1717 * probing because they are waiting on a optional supplier that doesn't have a
1718 * driver. While fw_devlink will eventually identify such devices and unblock
1719 * the probing automatically, it might be too late by the time it unblocks the
1720 * probing of devices. For example, the IP4 autoconfig might timeout before
1721 * fw_devlink unblocks probing of the network interface.
1723 * This function is available to temporarily try and probe all devices that have
1724 * a driver even if some of their suppliers haven't been added or don't have
1727 * The drivers can then decide which of the suppliers are optional vs mandatory
1728 * and probe the device if possible. By the time this function returns, all such
1729 * "best effort" probes are guaranteed to be completed. If a device successfully
1730 * probes in this mode, we delete all fw_devlink discovered dependencies of that
1731 * device where the supplier hasn't yet probed successfully because they have to
1732 * be optional dependencies.
1734 * Any devices that didn't successfully probe go back to being treated as if
1735 * this function was never called.
1737 * This also means that some devices that aren't needed for init and could have
1738 * waited for their optional supplier to probe (when the supplier's module is
1739 * loaded later on) would end up probing prematurely with limited functionality.
1740 * So call this function only when boot would fail without it.
1742 void __init wait_for_init_devices_probe(void)
1744 if (!fw_devlink_flags || fw_devlink_is_permissive())
1748 * Wait for all ongoing probes to finish so that the "best effort" is
1749 * only applied to devices that can't probe otherwise.
1751 wait_for_device_probe();
1753 pr_info("Trying to probe devices needed for running init ...\n");
1754 fw_devlink_best_effort = true;
1755 driver_deferred_probe_trigger();
1758 * Wait for all "best effort" probes to finish before going back to
1759 * normal enforcement.
1761 wait_for_device_probe();
1762 fw_devlink_best_effort = false;
1765 static void fw_devlink_unblock_consumers(struct device *dev)
1767 struct device_link *link;
1769 if (!fw_devlink_flags || fw_devlink_is_permissive())
1772 device_links_write_lock();
1773 list_for_each_entry(link, &dev->links.consumers, s_node)
1774 fw_devlink_relax_link(link);
1775 device_links_write_unlock();
1779 * fw_devlink_relax_cycle - Convert cyclic links to SYNC_STATE_ONLY links
1780 * @con: Device to check dependencies for.
1781 * @sup: Device to check against.
1783 * Check if @sup depends on @con or any device dependent on it (its child or
1784 * its consumer etc). When such a cyclic dependency is found, convert all
1785 * device links created solely by fw_devlink into SYNC_STATE_ONLY device links.
1786 * This is the equivalent of doing fw_devlink=permissive just between the
1787 * devices in the cycle. We need to do this because, at this point, fw_devlink
1788 * can't tell which of these dependencies is not a real dependency.
1790 * Return 1 if a cycle is found. Otherwise, return 0.
1792 static int fw_devlink_relax_cycle(struct device *con, void *sup)
1794 struct device_link *link;
1800 ret = device_for_each_child(con, sup, fw_devlink_relax_cycle);
1804 list_for_each_entry(link, &con->links.consumers, s_node) {
1805 if (!(link->flags & DL_FLAG_CYCLE) &&
1806 device_link_flag_is_sync_state_only(link->flags))
1809 if (!fw_devlink_relax_cycle(link->consumer, sup))
1814 fw_devlink_relax_link(link);
1815 link->flags |= DL_FLAG_CYCLE;
1821 * fw_devlink_create_devlink - Create a device link from a consumer to fwnode
1822 * @con: consumer device for the device link
1823 * @sup_handle: fwnode handle of supplier
1824 * @flags: devlink flags
1826 * This function will try to create a device link between the consumer device
1827 * @con and the supplier device represented by @sup_handle.
1829 * The supplier has to be provided as a fwnode because incorrect cycles in
1830 * fwnode links can sometimes cause the supplier device to never be created.
1831 * This function detects such cases and returns an error if it cannot create a
1832 * device link from the consumer to a missing supplier.
1835 * 0 on successfully creating a device link
1836 * -EINVAL if the device link cannot be created as expected
1837 * -EAGAIN if the device link cannot be created right now, but it may be
1838 * possible to do that in the future
1840 static int fw_devlink_create_devlink(struct device *con,
1841 struct fwnode_handle *sup_handle, u32 flags)
1843 struct device *sup_dev;
1847 * In some cases, a device P might also be a supplier to its child node
1848 * C. However, this would defer the probe of C until the probe of P
1849 * completes successfully. This is perfectly fine in the device driver
1850 * model. device_add() doesn't guarantee probe completion of the device
1851 * by the time it returns.
1853 * However, there are a few drivers that assume C will finish probing
1854 * as soon as it's added and before P finishes probing. So, we provide
1855 * a flag to let fw_devlink know not to delay the probe of C until the
1856 * probe of P completes successfully.
1858 * When such a flag is set, we can't create device links where P is the
1859 * supplier of C as that would delay the probe of C.
1861 if (sup_handle->flags & FWNODE_FLAG_NEEDS_CHILD_BOUND_ON_ADD &&
1862 fwnode_is_ancestor_of(sup_handle, con->fwnode))
1865 sup_dev = get_dev_from_fwnode(sup_handle);
1868 * If it's one of those drivers that don't actually bind to
1869 * their device using driver core, then don't wait on this
1870 * supplier device indefinitely.
1872 if (sup_dev->links.status == DL_DEV_NO_DRIVER &&
1873 sup_handle->flags & FWNODE_FLAG_INITIALIZED) {
1879 * If this fails, it is due to cycles in device links. Just
1880 * give up on this link and treat it as invalid.
1882 if (!device_link_add(con, sup_dev, flags) &&
1883 !(flags & DL_FLAG_SYNC_STATE_ONLY)) {
1884 dev_info(con, "Fixing up cyclic dependency with %s\n",
1886 device_links_write_lock();
1887 fw_devlink_relax_cycle(con, sup_dev);
1888 device_links_write_unlock();
1889 device_link_add(con, sup_dev,
1890 FW_DEVLINK_FLAGS_PERMISSIVE);
1897 /* Supplier that's already initialized without a struct device. */
1898 if (sup_handle->flags & FWNODE_FLAG_INITIALIZED)
1902 * DL_FLAG_SYNC_STATE_ONLY doesn't block probing and supports
1903 * cycles. So cycle detection isn't necessary and shouldn't be
1906 if (flags & DL_FLAG_SYNC_STATE_ONLY)
1910 * If we can't find the supplier device from its fwnode, it might be
1911 * due to a cyclic dependency between fwnodes. Some of these cycles can
1912 * be broken by applying logic. Check for these types of cycles and
1913 * break them so that devices in the cycle probe properly.
1915 * If the supplier's parent is dependent on the consumer, then the
1916 * consumer and supplier have a cyclic dependency. Since fw_devlink
1917 * can't tell which of the inferred dependencies are incorrect, don't
1918 * enforce probe ordering between any of the devices in this cyclic
1919 * dependency. Do this by relaxing all the fw_devlink device links in
1920 * this cycle and by treating the fwnode link between the consumer and
1921 * the supplier as an invalid dependency.
1923 sup_dev = fwnode_get_next_parent_dev(sup_handle);
1924 if (sup_dev && device_is_dependent(con, sup_dev)) {
1925 dev_info(con, "Fixing up cyclic dependency with %pfwP (%s)\n",
1926 sup_handle, dev_name(sup_dev));
1927 device_links_write_lock();
1928 fw_devlink_relax_cycle(con, sup_dev);
1929 device_links_write_unlock();
1933 * Can't check for cycles or no cycles. So let's try
1940 put_device(sup_dev);
1945 * __fw_devlink_link_to_consumers - Create device links to consumers of a device
1946 * @dev: Device that needs to be linked to its consumers
1948 * This function looks at all the consumer fwnodes of @dev and creates device
1949 * links between the consumer device and @dev (supplier).
1951 * If the consumer device has not been added yet, then this function creates a
1952 * SYNC_STATE_ONLY link between @dev (supplier) and the closest ancestor device
1953 * of the consumer fwnode. This is necessary to make sure @dev doesn't get a
1954 * sync_state() callback before the real consumer device gets to be added and
1957 * Once device links are created from the real consumer to @dev (supplier), the
1958 * fwnode links are deleted.
1960 static void __fw_devlink_link_to_consumers(struct device *dev)
1962 struct fwnode_handle *fwnode = dev->fwnode;
1963 struct fwnode_link *link, *tmp;
1965 list_for_each_entry_safe(link, tmp, &fwnode->consumers, s_hook) {
1966 u32 dl_flags = fw_devlink_get_flags();
1967 struct device *con_dev;
1968 bool own_link = true;
1971 con_dev = get_dev_from_fwnode(link->consumer);
1973 * If consumer device is not available yet, make a "proxy"
1974 * SYNC_STATE_ONLY link from the consumer's parent device to
1975 * the supplier device. This is necessary to make sure the
1976 * supplier doesn't get a sync_state() callback before the real
1977 * consumer can create a device link to the supplier.
1979 * This proxy link step is needed to handle the case where the
1980 * consumer's parent device is added before the supplier.
1983 con_dev = fwnode_get_next_parent_dev(link->consumer);
1985 * However, if the consumer's parent device is also the
1986 * parent of the supplier, don't create a
1987 * consumer-supplier link from the parent to its child
1988 * device. Such a dependency is impossible.
1991 fwnode_is_ancestor_of(con_dev->fwnode, fwnode)) {
1992 put_device(con_dev);
1996 dl_flags = FW_DEVLINK_FLAGS_PERMISSIVE;
2003 ret = fw_devlink_create_devlink(con_dev, fwnode, dl_flags);
2004 put_device(con_dev);
2005 if (!own_link || ret == -EAGAIN)
2008 __fwnode_link_del(link);
2013 * __fw_devlink_link_to_suppliers - Create device links to suppliers of a device
2014 * @dev: The consumer device that needs to be linked to its suppliers
2015 * @fwnode: Root of the fwnode tree that is used to create device links
2017 * This function looks at all the supplier fwnodes of fwnode tree rooted at
2018 * @fwnode and creates device links between @dev (consumer) and all the
2019 * supplier devices of the entire fwnode tree at @fwnode.
2021 * The function creates normal (non-SYNC_STATE_ONLY) device links between @dev
2022 * and the real suppliers of @dev. Once these device links are created, the
2023 * fwnode links are deleted. When such device links are successfully created,
2024 * this function is called recursively on those supplier devices. This is
2025 * needed to detect and break some invalid cycles in fwnode links. See
2026 * fw_devlink_create_devlink() for more details.
2028 * In addition, it also looks at all the suppliers of the entire fwnode tree
2029 * because some of the child devices of @dev that have not been added yet
2030 * (because @dev hasn't probed) might already have their suppliers added to
2031 * driver core. So, this function creates SYNC_STATE_ONLY device links between
2032 * @dev (consumer) and these suppliers to make sure they don't execute their
2033 * sync_state() callbacks before these child devices have a chance to create
2034 * their device links. The fwnode links that correspond to the child devices
2035 * aren't delete because they are needed later to create the device links
2036 * between the real consumer and supplier devices.
2038 static void __fw_devlink_link_to_suppliers(struct device *dev,
2039 struct fwnode_handle *fwnode)
2041 bool own_link = (dev->fwnode == fwnode);
2042 struct fwnode_link *link, *tmp;
2043 struct fwnode_handle *child = NULL;
2047 dl_flags = fw_devlink_get_flags();
2049 dl_flags = FW_DEVLINK_FLAGS_PERMISSIVE;
2051 list_for_each_entry_safe(link, tmp, &fwnode->suppliers, c_hook) {
2053 struct device *sup_dev;
2054 struct fwnode_handle *sup = link->supplier;
2056 ret = fw_devlink_create_devlink(dev, sup, dl_flags);
2057 if (!own_link || ret == -EAGAIN)
2060 __fwnode_link_del(link);
2062 /* If no device link was created, nothing more to do. */
2067 * If a device link was successfully created to a supplier, we
2068 * now need to try and link the supplier to all its suppliers.
2070 * This is needed to detect and delete false dependencies in
2071 * fwnode links that haven't been converted to a device link
2072 * yet. See comments in fw_devlink_create_devlink() for more
2073 * details on the false dependency.
2075 * Without deleting these false dependencies, some devices will
2076 * never probe because they'll keep waiting for their false
2077 * dependency fwnode links to be converted to device links.
2079 sup_dev = get_dev_from_fwnode(sup);
2080 __fw_devlink_link_to_suppliers(sup_dev, sup_dev->fwnode);
2081 put_device(sup_dev);
2085 * Make "proxy" SYNC_STATE_ONLY device links to represent the needs of
2086 * all the descendants. This proxy link step is needed to handle the
2087 * case where the supplier is added before the consumer's parent device
2090 while ((child = fwnode_get_next_available_child_node(fwnode, child)))
2091 __fw_devlink_link_to_suppliers(dev, child);
2094 static void fw_devlink_link_device(struct device *dev)
2096 struct fwnode_handle *fwnode = dev->fwnode;
2098 if (!fw_devlink_flags)
2101 fw_devlink_parse_fwtree(fwnode);
2103 mutex_lock(&fwnode_link_lock);
2104 __fw_devlink_link_to_consumers(dev);
2105 __fw_devlink_link_to_suppliers(dev, fwnode);
2106 mutex_unlock(&fwnode_link_lock);
2109 /* Device links support end. */
2111 int (*platform_notify)(struct device *dev) = NULL;
2112 int (*platform_notify_remove)(struct device *dev) = NULL;
2113 static struct kobject *dev_kobj;
2114 struct kobject *sysfs_dev_char_kobj;
2115 struct kobject *sysfs_dev_block_kobj;
2117 static DEFINE_MUTEX(device_hotplug_lock);
2119 void lock_device_hotplug(void)
2121 mutex_lock(&device_hotplug_lock);
2124 void unlock_device_hotplug(void)
2126 mutex_unlock(&device_hotplug_lock);
2129 int lock_device_hotplug_sysfs(void)
2131 if (mutex_trylock(&device_hotplug_lock))
2134 /* Avoid busy looping (5 ms of sleep should do). */
2136 return restart_syscall();
2140 static inline int device_is_not_partition(struct device *dev)
2142 return !(dev->type == &part_type);
2145 static inline int device_is_not_partition(struct device *dev)
2151 static void device_platform_notify(struct device *dev)
2153 acpi_device_notify(dev);
2155 software_node_notify(dev);
2157 if (platform_notify)
2158 platform_notify(dev);
2161 static void device_platform_notify_remove(struct device *dev)
2163 acpi_device_notify_remove(dev);
2165 software_node_notify_remove(dev);
2167 if (platform_notify_remove)
2168 platform_notify_remove(dev);
2172 * dev_driver_string - Return a device's driver name, if at all possible
2173 * @dev: struct device to get the name of
2175 * Will return the device's driver's name if it is bound to a device. If
2176 * the device is not bound to a driver, it will return the name of the bus
2177 * it is attached to. If it is not attached to a bus either, an empty
2178 * string will be returned.
2180 const char *dev_driver_string(const struct device *dev)
2182 struct device_driver *drv;
2184 /* dev->driver can change to NULL underneath us because of unbinding,
2185 * so be careful about accessing it. dev->bus and dev->class should
2186 * never change once they are set, so they don't need special care.
2188 drv = READ_ONCE(dev->driver);
2189 return drv ? drv->name : dev_bus_name(dev);
2191 EXPORT_SYMBOL(dev_driver_string);
2193 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
2195 static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
2198 struct device_attribute *dev_attr = to_dev_attr(attr);
2199 struct device *dev = kobj_to_dev(kobj);
2203 ret = dev_attr->show(dev, dev_attr, buf);
2204 if (ret >= (ssize_t)PAGE_SIZE) {
2205 printk("dev_attr_show: %pS returned bad count\n",
2211 static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
2212 const char *buf, size_t count)
2214 struct device_attribute *dev_attr = to_dev_attr(attr);
2215 struct device *dev = kobj_to_dev(kobj);
2218 if (dev_attr->store)
2219 ret = dev_attr->store(dev, dev_attr, buf, count);
2223 static const struct sysfs_ops dev_sysfs_ops = {
2224 .show = dev_attr_show,
2225 .store = dev_attr_store,
2228 #define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
2230 ssize_t device_store_ulong(struct device *dev,
2231 struct device_attribute *attr,
2232 const char *buf, size_t size)
2234 struct dev_ext_attribute *ea = to_ext_attr(attr);
2238 ret = kstrtoul(buf, 0, &new);
2241 *(unsigned long *)(ea->var) = new;
2242 /* Always return full write size even if we didn't consume all */
2245 EXPORT_SYMBOL_GPL(device_store_ulong);
2247 ssize_t device_show_ulong(struct device *dev,
2248 struct device_attribute *attr,
2251 struct dev_ext_attribute *ea = to_ext_attr(attr);
2252 return sysfs_emit(buf, "%lx\n", *(unsigned long *)(ea->var));
2254 EXPORT_SYMBOL_GPL(device_show_ulong);
2256 ssize_t device_store_int(struct device *dev,
2257 struct device_attribute *attr,
2258 const char *buf, size_t size)
2260 struct dev_ext_attribute *ea = to_ext_attr(attr);
2264 ret = kstrtol(buf, 0, &new);
2268 if (new > INT_MAX || new < INT_MIN)
2270 *(int *)(ea->var) = new;
2271 /* Always return full write size even if we didn't consume all */
2274 EXPORT_SYMBOL_GPL(device_store_int);
2276 ssize_t device_show_int(struct device *dev,
2277 struct device_attribute *attr,
2280 struct dev_ext_attribute *ea = to_ext_attr(attr);
2282 return sysfs_emit(buf, "%d\n", *(int *)(ea->var));
2284 EXPORT_SYMBOL_GPL(device_show_int);
2286 ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
2287 const char *buf, size_t size)
2289 struct dev_ext_attribute *ea = to_ext_attr(attr);
2291 if (strtobool(buf, ea->var) < 0)
2296 EXPORT_SYMBOL_GPL(device_store_bool);
2298 ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
2301 struct dev_ext_attribute *ea = to_ext_attr(attr);
2303 return sysfs_emit(buf, "%d\n", *(bool *)(ea->var));
2305 EXPORT_SYMBOL_GPL(device_show_bool);
2308 * device_release - free device structure.
2309 * @kobj: device's kobject.
2311 * This is called once the reference count for the object
2312 * reaches 0. We forward the call to the device's release
2313 * method, which should handle actually freeing the structure.
2315 static void device_release(struct kobject *kobj)
2317 struct device *dev = kobj_to_dev(kobj);
2318 struct device_private *p = dev->p;
2321 * Some platform devices are driven without driver attached
2322 * and managed resources may have been acquired. Make sure
2323 * all resources are released.
2325 * Drivers still can add resources into device after device
2326 * is deleted but alive, so release devres here to avoid
2327 * possible memory leak.
2329 devres_release_all(dev);
2331 kfree(dev->dma_range_map);
2335 else if (dev->type && dev->type->release)
2336 dev->type->release(dev);
2337 else if (dev->class && dev->class->dev_release)
2338 dev->class->dev_release(dev);
2340 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",
2345 static const void *device_namespace(struct kobject *kobj)
2347 struct device *dev = kobj_to_dev(kobj);
2348 const void *ns = NULL;
2350 if (dev->class && dev->class->ns_type)
2351 ns = dev->class->namespace(dev);
2356 static void device_get_ownership(struct kobject *kobj, kuid_t *uid, kgid_t *gid)
2358 struct device *dev = kobj_to_dev(kobj);
2360 if (dev->class && dev->class->get_ownership)
2361 dev->class->get_ownership(dev, uid, gid);
2364 static struct kobj_type device_ktype = {
2365 .release = device_release,
2366 .sysfs_ops = &dev_sysfs_ops,
2367 .namespace = device_namespace,
2368 .get_ownership = device_get_ownership,
2372 static int dev_uevent_filter(struct kobject *kobj)
2374 const struct kobj_type *ktype = get_ktype(kobj);
2376 if (ktype == &device_ktype) {
2377 struct device *dev = kobj_to_dev(kobj);
2386 static const char *dev_uevent_name(struct kobject *kobj)
2388 struct device *dev = kobj_to_dev(kobj);
2391 return dev->bus->name;
2393 return dev->class->name;
2397 static int dev_uevent(struct kobject *kobj, struct kobj_uevent_env *env)
2399 struct device *dev = kobj_to_dev(kobj);
2402 /* add device node properties if present */
2403 if (MAJOR(dev->devt)) {
2407 kuid_t uid = GLOBAL_ROOT_UID;
2408 kgid_t gid = GLOBAL_ROOT_GID;
2410 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
2411 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
2412 name = device_get_devnode(dev, &mode, &uid, &gid, &tmp);
2414 add_uevent_var(env, "DEVNAME=%s", name);
2416 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
2417 if (!uid_eq(uid, GLOBAL_ROOT_UID))
2418 add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid));
2419 if (!gid_eq(gid, GLOBAL_ROOT_GID))
2420 add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid));
2425 if (dev->type && dev->type->name)
2426 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
2429 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
2431 /* Add common DT information about the device */
2432 of_device_uevent(dev, env);
2434 /* have the bus specific function add its stuff */
2435 if (dev->bus && dev->bus->uevent) {
2436 retval = dev->bus->uevent(dev, env);
2438 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
2439 dev_name(dev), __func__, retval);
2442 /* have the class specific function add its stuff */
2443 if (dev->class && dev->class->dev_uevent) {
2444 retval = dev->class->dev_uevent(dev, env);
2446 pr_debug("device: '%s': %s: class uevent() "
2447 "returned %d\n", dev_name(dev),
2451 /* have the device type specific function add its stuff */
2452 if (dev->type && dev->type->uevent) {
2453 retval = dev->type->uevent(dev, env);
2455 pr_debug("device: '%s': %s: dev_type uevent() "
2456 "returned %d\n", dev_name(dev),
2463 static const struct kset_uevent_ops device_uevent_ops = {
2464 .filter = dev_uevent_filter,
2465 .name = dev_uevent_name,
2466 .uevent = dev_uevent,
2469 static ssize_t uevent_show(struct device *dev, struct device_attribute *attr,
2472 struct kobject *top_kobj;
2474 struct kobj_uevent_env *env = NULL;
2479 /* search the kset, the device belongs to */
2480 top_kobj = &dev->kobj;
2481 while (!top_kobj->kset && top_kobj->parent)
2482 top_kobj = top_kobj->parent;
2483 if (!top_kobj->kset)
2486 kset = top_kobj->kset;
2487 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
2490 /* respect filter */
2491 if (kset->uevent_ops && kset->uevent_ops->filter)
2492 if (!kset->uevent_ops->filter(&dev->kobj))
2495 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
2499 /* let the kset specific function add its keys */
2500 retval = kset->uevent_ops->uevent(&dev->kobj, env);
2504 /* copy keys to file */
2505 for (i = 0; i < env->envp_idx; i++)
2506 len += sysfs_emit_at(buf, len, "%s\n", env->envp[i]);
2512 static ssize_t uevent_store(struct device *dev, struct device_attribute *attr,
2513 const char *buf, size_t count)
2517 rc = kobject_synth_uevent(&dev->kobj, buf, count);
2520 dev_err(dev, "uevent: failed to send synthetic uevent: %d\n", rc);
2526 static DEVICE_ATTR_RW(uevent);
2528 static ssize_t online_show(struct device *dev, struct device_attribute *attr,
2534 val = !dev->offline;
2536 return sysfs_emit(buf, "%u\n", val);
2539 static ssize_t online_store(struct device *dev, struct device_attribute *attr,
2540 const char *buf, size_t count)
2545 ret = strtobool(buf, &val);
2549 ret = lock_device_hotplug_sysfs();
2553 ret = val ? device_online(dev) : device_offline(dev);
2554 unlock_device_hotplug();
2555 return ret < 0 ? ret : count;
2557 static DEVICE_ATTR_RW(online);
2559 static ssize_t removable_show(struct device *dev, struct device_attribute *attr,
2564 switch (dev->removable) {
2565 case DEVICE_REMOVABLE:
2574 return sysfs_emit(buf, "%s\n", loc);
2576 static DEVICE_ATTR_RO(removable);
2578 int device_add_groups(struct device *dev, const struct attribute_group **groups)
2580 return sysfs_create_groups(&dev->kobj, groups);
2582 EXPORT_SYMBOL_GPL(device_add_groups);
2584 void device_remove_groups(struct device *dev,
2585 const struct attribute_group **groups)
2587 sysfs_remove_groups(&dev->kobj, groups);
2589 EXPORT_SYMBOL_GPL(device_remove_groups);
2591 union device_attr_group_devres {
2592 const struct attribute_group *group;
2593 const struct attribute_group **groups;
2596 static int devm_attr_group_match(struct device *dev, void *res, void *data)
2598 return ((union device_attr_group_devres *)res)->group == data;
2601 static void devm_attr_group_remove(struct device *dev, void *res)
2603 union device_attr_group_devres *devres = res;
2604 const struct attribute_group *group = devres->group;
2606 dev_dbg(dev, "%s: removing group %p\n", __func__, group);
2607 sysfs_remove_group(&dev->kobj, group);
2610 static void devm_attr_groups_remove(struct device *dev, void *res)
2612 union device_attr_group_devres *devres = res;
2613 const struct attribute_group **groups = devres->groups;
2615 dev_dbg(dev, "%s: removing groups %p\n", __func__, groups);
2616 sysfs_remove_groups(&dev->kobj, groups);
2620 * devm_device_add_group - given a device, create a managed attribute group
2621 * @dev: The device to create the group for
2622 * @grp: The attribute group to create
2624 * This function creates a group for the first time. It will explicitly
2625 * warn and error if any of the attribute files being created already exist.
2627 * Returns 0 on success or error code on failure.
2629 int devm_device_add_group(struct device *dev, const struct attribute_group *grp)
2631 union device_attr_group_devres *devres;
2634 devres = devres_alloc(devm_attr_group_remove,
2635 sizeof(*devres), GFP_KERNEL);
2639 error = sysfs_create_group(&dev->kobj, grp);
2641 devres_free(devres);
2645 devres->group = grp;
2646 devres_add(dev, devres);
2649 EXPORT_SYMBOL_GPL(devm_device_add_group);
2652 * devm_device_remove_group: remove a managed group from a device
2653 * @dev: device to remove the group from
2654 * @grp: group to remove
2656 * This function removes a group of attributes from a device. The attributes
2657 * previously have to have been created for this group, otherwise it will fail.
2659 void devm_device_remove_group(struct device *dev,
2660 const struct attribute_group *grp)
2662 WARN_ON(devres_release(dev, devm_attr_group_remove,
2663 devm_attr_group_match,
2664 /* cast away const */ (void *)grp));
2666 EXPORT_SYMBOL_GPL(devm_device_remove_group);
2669 * devm_device_add_groups - create a bunch of managed attribute groups
2670 * @dev: The device to create the group for
2671 * @groups: The attribute groups to create, NULL terminated
2673 * This function creates a bunch of managed attribute groups. If an error
2674 * occurs when creating a group, all previously created groups will be
2675 * removed, unwinding everything back to the original state when this
2676 * function was called. It will explicitly warn and error if any of the
2677 * attribute files being created already exist.
2679 * Returns 0 on success or error code from sysfs_create_group on failure.
2681 int devm_device_add_groups(struct device *dev,
2682 const struct attribute_group **groups)
2684 union device_attr_group_devres *devres;
2687 devres = devres_alloc(devm_attr_groups_remove,
2688 sizeof(*devres), GFP_KERNEL);
2692 error = sysfs_create_groups(&dev->kobj, groups);
2694 devres_free(devres);
2698 devres->groups = groups;
2699 devres_add(dev, devres);
2702 EXPORT_SYMBOL_GPL(devm_device_add_groups);
2705 * devm_device_remove_groups - remove a list of managed groups
2707 * @dev: The device for the groups to be removed from
2708 * @groups: NULL terminated list of groups to be removed
2710 * If groups is not NULL, remove the specified groups from the device.
2712 void devm_device_remove_groups(struct device *dev,
2713 const struct attribute_group **groups)
2715 WARN_ON(devres_release(dev, devm_attr_groups_remove,
2716 devm_attr_group_match,
2717 /* cast away const */ (void *)groups));
2719 EXPORT_SYMBOL_GPL(devm_device_remove_groups);
2721 static int device_add_attrs(struct device *dev)
2723 struct class *class = dev->class;
2724 const struct device_type *type = dev->type;
2728 error = device_add_groups(dev, class->dev_groups);
2734 error = device_add_groups(dev, type->groups);
2736 goto err_remove_class_groups;
2739 error = device_add_groups(dev, dev->groups);
2741 goto err_remove_type_groups;
2743 if (device_supports_offline(dev) && !dev->offline_disabled) {
2744 error = device_create_file(dev, &dev_attr_online);
2746 goto err_remove_dev_groups;
2749 if (fw_devlink_flags && !fw_devlink_is_permissive() && dev->fwnode) {
2750 error = device_create_file(dev, &dev_attr_waiting_for_supplier);
2752 goto err_remove_dev_online;
2755 if (dev_removable_is_valid(dev)) {
2756 error = device_create_file(dev, &dev_attr_removable);
2758 goto err_remove_dev_waiting_for_supplier;
2761 if (dev_add_physical_location(dev)) {
2762 error = device_add_group(dev,
2763 &dev_attr_physical_location_group);
2765 goto err_remove_dev_removable;
2770 err_remove_dev_removable:
2771 device_remove_file(dev, &dev_attr_removable);
2772 err_remove_dev_waiting_for_supplier:
2773 device_remove_file(dev, &dev_attr_waiting_for_supplier);
2774 err_remove_dev_online:
2775 device_remove_file(dev, &dev_attr_online);
2776 err_remove_dev_groups:
2777 device_remove_groups(dev, dev->groups);
2778 err_remove_type_groups:
2780 device_remove_groups(dev, type->groups);
2781 err_remove_class_groups:
2783 device_remove_groups(dev, class->dev_groups);
2788 static void device_remove_attrs(struct device *dev)
2790 struct class *class = dev->class;
2791 const struct device_type *type = dev->type;
2793 if (dev->physical_location) {
2794 device_remove_group(dev, &dev_attr_physical_location_group);
2795 kfree(dev->physical_location);
2798 device_remove_file(dev, &dev_attr_removable);
2799 device_remove_file(dev, &dev_attr_waiting_for_supplier);
2800 device_remove_file(dev, &dev_attr_online);
2801 device_remove_groups(dev, dev->groups);
2804 device_remove_groups(dev, type->groups);
2807 device_remove_groups(dev, class->dev_groups);
2810 static ssize_t dev_show(struct device *dev, struct device_attribute *attr,
2813 return print_dev_t(buf, dev->devt);
2815 static DEVICE_ATTR_RO(dev);
2818 struct kset *devices_kset;
2821 * devices_kset_move_before - Move device in the devices_kset's list.
2822 * @deva: Device to move.
2823 * @devb: Device @deva should come before.
2825 static void devices_kset_move_before(struct device *deva, struct device *devb)
2829 pr_debug("devices_kset: Moving %s before %s\n",
2830 dev_name(deva), dev_name(devb));
2831 spin_lock(&devices_kset->list_lock);
2832 list_move_tail(&deva->kobj.entry, &devb->kobj.entry);
2833 spin_unlock(&devices_kset->list_lock);
2837 * devices_kset_move_after - Move device in the devices_kset's list.
2838 * @deva: Device to move
2839 * @devb: Device @deva should come after.
2841 static void devices_kset_move_after(struct device *deva, struct device *devb)
2845 pr_debug("devices_kset: Moving %s after %s\n",
2846 dev_name(deva), dev_name(devb));
2847 spin_lock(&devices_kset->list_lock);
2848 list_move(&deva->kobj.entry, &devb->kobj.entry);
2849 spin_unlock(&devices_kset->list_lock);
2853 * devices_kset_move_last - move the device to the end of devices_kset's list.
2854 * @dev: device to move
2856 void devices_kset_move_last(struct device *dev)
2860 pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev));
2861 spin_lock(&devices_kset->list_lock);
2862 list_move_tail(&dev->kobj.entry, &devices_kset->list);
2863 spin_unlock(&devices_kset->list_lock);
2867 * device_create_file - create sysfs attribute file for device.
2869 * @attr: device attribute descriptor.
2871 int device_create_file(struct device *dev,
2872 const struct device_attribute *attr)
2877 WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
2878 "Attribute %s: write permission without 'store'\n",
2880 WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
2881 "Attribute %s: read permission without 'show'\n",
2883 error = sysfs_create_file(&dev->kobj, &attr->attr);
2888 EXPORT_SYMBOL_GPL(device_create_file);
2891 * device_remove_file - remove sysfs attribute file.
2893 * @attr: device attribute descriptor.
2895 void device_remove_file(struct device *dev,
2896 const struct device_attribute *attr)
2899 sysfs_remove_file(&dev->kobj, &attr->attr);
2901 EXPORT_SYMBOL_GPL(device_remove_file);
2904 * device_remove_file_self - remove sysfs attribute file from its own method.
2906 * @attr: device attribute descriptor.
2908 * See kernfs_remove_self() for details.
2910 bool device_remove_file_self(struct device *dev,
2911 const struct device_attribute *attr)
2914 return sysfs_remove_file_self(&dev->kobj, &attr->attr);
2918 EXPORT_SYMBOL_GPL(device_remove_file_self);
2921 * device_create_bin_file - create sysfs binary attribute file for device.
2923 * @attr: device binary attribute descriptor.
2925 int device_create_bin_file(struct device *dev,
2926 const struct bin_attribute *attr)
2928 int error = -EINVAL;
2930 error = sysfs_create_bin_file(&dev->kobj, attr);
2933 EXPORT_SYMBOL_GPL(device_create_bin_file);
2936 * device_remove_bin_file - remove sysfs binary attribute file
2938 * @attr: device binary attribute descriptor.
2940 void device_remove_bin_file(struct device *dev,
2941 const struct bin_attribute *attr)
2944 sysfs_remove_bin_file(&dev->kobj, attr);
2946 EXPORT_SYMBOL_GPL(device_remove_bin_file);
2948 static void klist_children_get(struct klist_node *n)
2950 struct device_private *p = to_device_private_parent(n);
2951 struct device *dev = p->device;
2956 static void klist_children_put(struct klist_node *n)
2958 struct device_private *p = to_device_private_parent(n);
2959 struct device *dev = p->device;
2965 * device_initialize - init device structure.
2968 * This prepares the device for use by other layers by initializing
2970 * It is the first half of device_register(), if called by
2971 * that function, though it can also be called separately, so one
2972 * may use @dev's fields. In particular, get_device()/put_device()
2973 * may be used for reference counting of @dev after calling this
2976 * All fields in @dev must be initialized by the caller to 0, except
2977 * for those explicitly set to some other value. The simplest
2978 * approach is to use kzalloc() to allocate the structure containing
2981 * NOTE: Use put_device() to give up your reference instead of freeing
2982 * @dev directly once you have called this function.
2984 void device_initialize(struct device *dev)
2986 dev->kobj.kset = devices_kset;
2987 kobject_init(&dev->kobj, &device_ktype);
2988 INIT_LIST_HEAD(&dev->dma_pools);
2989 mutex_init(&dev->mutex);
2990 lockdep_set_novalidate_class(&dev->mutex);
2991 spin_lock_init(&dev->devres_lock);
2992 INIT_LIST_HEAD(&dev->devres_head);
2993 device_pm_init(dev);
2994 set_dev_node(dev, NUMA_NO_NODE);
2995 INIT_LIST_HEAD(&dev->links.consumers);
2996 INIT_LIST_HEAD(&dev->links.suppliers);
2997 INIT_LIST_HEAD(&dev->links.defer_sync);
2998 dev->links.status = DL_DEV_NO_DRIVER;
2999 #if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE) || \
3000 defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU) || \
3001 defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL)
3002 dev->dma_coherent = dma_default_coherent;
3004 #ifdef CONFIG_SWIOTLB
3005 dev->dma_io_tlb_mem = &io_tlb_default_mem;
3008 EXPORT_SYMBOL_GPL(device_initialize);
3010 struct kobject *virtual_device_parent(struct device *dev)
3012 static struct kobject *virtual_dir = NULL;
3015 virtual_dir = kobject_create_and_add("virtual",
3016 &devices_kset->kobj);
3022 struct kobject kobj;
3023 struct class *class;
3026 #define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
3028 static void class_dir_release(struct kobject *kobj)
3030 struct class_dir *dir = to_class_dir(kobj);
3035 struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
3037 struct class_dir *dir = to_class_dir(kobj);
3038 return dir->class->ns_type;
3041 static struct kobj_type class_dir_ktype = {
3042 .release = class_dir_release,
3043 .sysfs_ops = &kobj_sysfs_ops,
3044 .child_ns_type = class_dir_child_ns_type
3047 static struct kobject *
3048 class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
3050 struct class_dir *dir;
3053 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
3055 return ERR_PTR(-ENOMEM);
3058 kobject_init(&dir->kobj, &class_dir_ktype);
3060 dir->kobj.kset = &class->p->glue_dirs;
3062 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
3064 kobject_put(&dir->kobj);
3065 return ERR_PTR(retval);
3070 static DEFINE_MUTEX(gdp_mutex);
3072 static struct kobject *get_device_parent(struct device *dev,
3073 struct device *parent)
3076 struct kobject *kobj = NULL;
3077 struct kobject *parent_kobj;
3081 /* block disks show up in /sys/block */
3082 if (sysfs_deprecated && dev->class == &block_class) {
3083 if (parent && parent->class == &block_class)
3084 return &parent->kobj;
3085 return &block_class.p->subsys.kobj;
3090 * If we have no parent, we live in "virtual".
3091 * Class-devices with a non class-device as parent, live
3092 * in a "glue" directory to prevent namespace collisions.
3095 parent_kobj = virtual_device_parent(dev);
3096 else if (parent->class && !dev->class->ns_type)
3097 return &parent->kobj;
3099 parent_kobj = &parent->kobj;
3101 mutex_lock(&gdp_mutex);
3103 /* find our class-directory at the parent and reference it */
3104 spin_lock(&dev->class->p->glue_dirs.list_lock);
3105 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
3106 if (k->parent == parent_kobj) {
3107 kobj = kobject_get(k);
3110 spin_unlock(&dev->class->p->glue_dirs.list_lock);
3112 mutex_unlock(&gdp_mutex);
3116 /* or create a new class-directory at the parent device */
3117 k = class_dir_create_and_add(dev->class, parent_kobj);
3118 /* do not emit an uevent for this simple "glue" directory */
3119 mutex_unlock(&gdp_mutex);
3123 /* subsystems can specify a default root directory for their devices */
3124 if (!parent && dev->bus && dev->bus->dev_root)
3125 return &dev->bus->dev_root->kobj;
3128 return &parent->kobj;
3132 static inline bool live_in_glue_dir(struct kobject *kobj,
3135 if (!kobj || !dev->class ||
3136 kobj->kset != &dev->class->p->glue_dirs)
3141 static inline struct kobject *get_glue_dir(struct device *dev)
3143 return dev->kobj.parent;
3147 * kobject_has_children - Returns whether a kobject has children.
3148 * @kobj: the object to test
3150 * This will return whether a kobject has other kobjects as children.
3152 * It does NOT account for the presence of attribute files, only sub
3153 * directories. It also assumes there is no concurrent addition or
3154 * removal of such children, and thus relies on external locking.
3156 static inline bool kobject_has_children(struct kobject *kobj)
3158 WARN_ON_ONCE(kref_read(&kobj->kref) == 0);
3160 return kobj->sd && kobj->sd->dir.subdirs;
3164 * make sure cleaning up dir as the last step, we need to make
3165 * sure .release handler of kobject is run with holding the
3168 static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
3172 /* see if we live in a "glue" directory */
3173 if (!live_in_glue_dir(glue_dir, dev))
3176 mutex_lock(&gdp_mutex);
3178 * There is a race condition between removing glue directory
3179 * and adding a new device under the glue directory.
3184 * get_device_parent()
3185 * class_dir_create_and_add()
3186 * kobject_add_internal()
3187 * create_dir() // create glue_dir
3190 * get_device_parent()
3191 * kobject_get() // get glue_dir
3194 * cleanup_glue_dir()
3195 * kobject_del(glue_dir)
3198 * kobject_add_internal()
3199 * create_dir() // in glue_dir
3200 * sysfs_create_dir_ns()
3201 * kernfs_create_dir_ns(sd)
3203 * sysfs_remove_dir() // glue_dir->sd=NULL
3204 * sysfs_put() // free glue_dir->sd
3207 * kernfs_new_node(sd)
3208 * kernfs_get(glue_dir)
3212 * Before CPU1 remove last child device under glue dir, if CPU2 add
3213 * a new device under glue dir, the glue_dir kobject reference count
3214 * will be increase to 2 in kobject_get(k). And CPU2 has been called
3215 * kernfs_create_dir_ns(). Meanwhile, CPU1 call sysfs_remove_dir()
3216 * and sysfs_put(). This result in glue_dir->sd is freed.
3218 * Then the CPU2 will see a stale "empty" but still potentially used
3219 * glue dir around in kernfs_new_node().
3221 * In order to avoid this happening, we also should make sure that
3222 * kernfs_node for glue_dir is released in CPU1 only when refcount
3223 * for glue_dir kobj is 1.
3225 ref = kref_read(&glue_dir->kref);
3226 if (!kobject_has_children(glue_dir) && !--ref)
3227 kobject_del(glue_dir);
3228 kobject_put(glue_dir);
3229 mutex_unlock(&gdp_mutex);
3232 static int device_add_class_symlinks(struct device *dev)
3234 struct device_node *of_node = dev_of_node(dev);
3238 error = sysfs_create_link(&dev->kobj, of_node_kobj(of_node), "of_node");
3240 dev_warn(dev, "Error %d creating of_node link\n",error);
3241 /* An error here doesn't warrant bringing down the device */
3247 error = sysfs_create_link(&dev->kobj,
3248 &dev->class->p->subsys.kobj,
3253 if (dev->parent && device_is_not_partition(dev)) {
3254 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
3261 /* /sys/block has directories and does not need symlinks */
3262 if (sysfs_deprecated && dev->class == &block_class)
3266 /* link in the class directory pointing to the device */
3267 error = sysfs_create_link(&dev->class->p->subsys.kobj,
3268 &dev->kobj, dev_name(dev));
3275 sysfs_remove_link(&dev->kobj, "device");
3278 sysfs_remove_link(&dev->kobj, "subsystem");
3280 sysfs_remove_link(&dev->kobj, "of_node");
3284 static void device_remove_class_symlinks(struct device *dev)
3286 if (dev_of_node(dev))
3287 sysfs_remove_link(&dev->kobj, "of_node");
3292 if (dev->parent && device_is_not_partition(dev))
3293 sysfs_remove_link(&dev->kobj, "device");
3294 sysfs_remove_link(&dev->kobj, "subsystem");
3296 if (sysfs_deprecated && dev->class == &block_class)
3299 sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
3303 * dev_set_name - set a device name
3305 * @fmt: format string for the device's name
3307 int dev_set_name(struct device *dev, const char *fmt, ...)
3312 va_start(vargs, fmt);
3313 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
3317 EXPORT_SYMBOL_GPL(dev_set_name);
3320 * device_to_dev_kobj - select a /sys/dev/ directory for the device
3323 * By default we select char/ for new entries. Setting class->dev_obj
3324 * to NULL prevents an entry from being created. class->dev_kobj must
3325 * be set (or cleared) before any devices are registered to the class
3326 * otherwise device_create_sys_dev_entry() and
3327 * device_remove_sys_dev_entry() will disagree about the presence of
3330 static struct kobject *device_to_dev_kobj(struct device *dev)
3332 struct kobject *kobj;
3335 kobj = dev->class->dev_kobj;
3337 kobj = sysfs_dev_char_kobj;
3342 static int device_create_sys_dev_entry(struct device *dev)
3344 struct kobject *kobj = device_to_dev_kobj(dev);
3349 format_dev_t(devt_str, dev->devt);
3350 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
3356 static void device_remove_sys_dev_entry(struct device *dev)
3358 struct kobject *kobj = device_to_dev_kobj(dev);
3362 format_dev_t(devt_str, dev->devt);
3363 sysfs_remove_link(kobj, devt_str);
3367 static int device_private_init(struct device *dev)
3369 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
3372 dev->p->device = dev;
3373 klist_init(&dev->p->klist_children, klist_children_get,
3374 klist_children_put);
3375 INIT_LIST_HEAD(&dev->p->deferred_probe);
3380 * device_add - add device to device hierarchy.
3383 * This is part 2 of device_register(), though may be called
3384 * separately _iff_ device_initialize() has been called separately.
3386 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
3387 * to the global and sibling lists for the device, then
3388 * adds it to the other relevant subsystems of the driver model.
3390 * Do not call this routine or device_register() more than once for
3391 * any device structure. The driver model core is not designed to work
3392 * with devices that get unregistered and then spring back to life.
3393 * (Among other things, it's very hard to guarantee that all references
3394 * to the previous incarnation of @dev have been dropped.) Allocate
3395 * and register a fresh new struct device instead.
3397 * NOTE: _Never_ directly free @dev after calling this function, even
3398 * if it returned an error! Always use put_device() to give up your
3399 * reference instead.
3401 * Rule of thumb is: if device_add() succeeds, you should call
3402 * device_del() when you want to get rid of it. If device_add() has
3403 * *not* succeeded, use *only* put_device() to drop the reference
3406 int device_add(struct device *dev)
3408 struct device *parent;
3409 struct kobject *kobj;
3410 struct class_interface *class_intf;
3411 int error = -EINVAL;
3412 struct kobject *glue_dir = NULL;
3414 dev = get_device(dev);
3419 error = device_private_init(dev);
3425 * for statically allocated devices, which should all be converted
3426 * some day, we need to initialize the name. We prevent reading back
3427 * the name, and force the use of dev_name()
3429 if (dev->init_name) {
3430 dev_set_name(dev, "%s", dev->init_name);
3431 dev->init_name = NULL;
3434 /* subsystems can specify simple device enumeration */
3435 if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
3436 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
3438 if (!dev_name(dev)) {
3443 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
3445 parent = get_device(dev->parent);
3446 kobj = get_device_parent(dev, parent);
3448 error = PTR_ERR(kobj);
3452 dev->kobj.parent = kobj;
3454 /* use parent numa_node */
3455 if (parent && (dev_to_node(dev) == NUMA_NO_NODE))
3456 set_dev_node(dev, dev_to_node(parent));
3458 /* first, register with generic layer. */
3459 /* we require the name to be set before, and pass NULL */
3460 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
3466 /* notify platform of device entry */
3467 device_platform_notify(dev);
3469 error = device_create_file(dev, &dev_attr_uevent);
3473 error = device_add_class_symlinks(dev);
3476 error = device_add_attrs(dev);
3479 error = bus_add_device(dev);
3482 error = dpm_sysfs_add(dev);
3487 if (MAJOR(dev->devt)) {
3488 error = device_create_file(dev, &dev_attr_dev);
3492 error = device_create_sys_dev_entry(dev);
3496 devtmpfs_create_node(dev);
3499 /* Notify clients of device addition. This call must come
3500 * after dpm_sysfs_add() and before kobject_uevent().
3503 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
3504 BUS_NOTIFY_ADD_DEVICE, dev);
3506 kobject_uevent(&dev->kobj, KOBJ_ADD);
3509 * Check if any of the other devices (consumers) have been waiting for
3510 * this device (supplier) to be added so that they can create a device
3513 * This needs to happen after device_pm_add() because device_link_add()
3514 * requires the supplier be registered before it's called.
3516 * But this also needs to happen before bus_probe_device() to make sure
3517 * waiting consumers can link to it before the driver is bound to the
3518 * device and the driver sync_state callback is called for this device.
3520 if (dev->fwnode && !dev->fwnode->dev) {
3521 dev->fwnode->dev = dev;
3522 fw_devlink_link_device(dev);
3525 bus_probe_device(dev);
3528 * If all driver registration is done and a newly added device doesn't
3529 * match with any driver, don't block its consumers from probing in
3530 * case the consumer device is able to operate without this supplier.
3532 if (dev->fwnode && fw_devlink_drv_reg_done && !dev->can_match)
3533 fw_devlink_unblock_consumers(dev);
3536 klist_add_tail(&dev->p->knode_parent,
3537 &parent->p->klist_children);
3540 mutex_lock(&dev->class->p->mutex);
3541 /* tie the class to the device */
3542 klist_add_tail(&dev->p->knode_class,
3543 &dev->class->p->klist_devices);
3545 /* notify any interfaces that the device is here */
3546 list_for_each_entry(class_intf,
3547 &dev->class->p->interfaces, node)
3548 if (class_intf->add_dev)
3549 class_intf->add_dev(dev, class_intf);
3550 mutex_unlock(&dev->class->p->mutex);
3556 if (MAJOR(dev->devt))
3557 device_remove_file(dev, &dev_attr_dev);
3559 device_pm_remove(dev);
3560 dpm_sysfs_remove(dev);
3563 bus_remove_device(dev);
3565 device_remove_attrs(dev);
3567 device_remove_class_symlinks(dev);
3569 device_remove_file(dev, &dev_attr_uevent);
3571 device_platform_notify_remove(dev);
3572 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
3573 glue_dir = get_glue_dir(dev);
3574 kobject_del(&dev->kobj);
3576 cleanup_glue_dir(dev, glue_dir);
3584 EXPORT_SYMBOL_GPL(device_add);
3587 * device_register - register a device with the system.
3588 * @dev: pointer to the device structure
3590 * This happens in two clean steps - initialize the device
3591 * and add it to the system. The two steps can be called
3592 * separately, but this is the easiest and most common.
3593 * I.e. you should only call the two helpers separately if
3594 * have a clearly defined need to use and refcount the device
3595 * before it is added to the hierarchy.
3597 * For more information, see the kerneldoc for device_initialize()
3600 * NOTE: _Never_ directly free @dev after calling this function, even
3601 * if it returned an error! Always use put_device() to give up the
3602 * reference initialized in this function instead.
3604 int device_register(struct device *dev)
3606 device_initialize(dev);
3607 return device_add(dev);
3609 EXPORT_SYMBOL_GPL(device_register);
3612 * get_device - increment reference count for device.
3615 * This simply forwards the call to kobject_get(), though
3616 * we do take care to provide for the case that we get a NULL
3617 * pointer passed in.
3619 struct device *get_device(struct device *dev)
3621 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
3623 EXPORT_SYMBOL_GPL(get_device);
3626 * put_device - decrement reference count.
3627 * @dev: device in question.
3629 void put_device(struct device *dev)
3631 /* might_sleep(); */
3633 kobject_put(&dev->kobj);
3635 EXPORT_SYMBOL_GPL(put_device);
3637 bool kill_device(struct device *dev)
3640 * Require the device lock and set the "dead" flag to guarantee that
3641 * the update behavior is consistent with the other bitfields near
3642 * it and that we cannot have an asynchronous probe routine trying
3643 * to run while we are tearing out the bus/class/sysfs from
3644 * underneath the device.
3646 device_lock_assert(dev);
3650 dev->p->dead = true;
3653 EXPORT_SYMBOL_GPL(kill_device);
3656 * device_del - delete device from system.
3659 * This is the first part of the device unregistration
3660 * sequence. This removes the device from the lists we control
3661 * from here, has it removed from the other driver model
3662 * subsystems it was added to in device_add(), and removes it
3663 * from the kobject hierarchy.
3665 * NOTE: this should be called manually _iff_ device_add() was
3666 * also called manually.
3668 void device_del(struct device *dev)
3670 struct device *parent = dev->parent;
3671 struct kobject *glue_dir = NULL;
3672 struct class_interface *class_intf;
3673 unsigned int noio_flag;
3679 if (dev->fwnode && dev->fwnode->dev == dev)
3680 dev->fwnode->dev = NULL;
3682 /* Notify clients of device removal. This call must come
3683 * before dpm_sysfs_remove().
3685 noio_flag = memalloc_noio_save();
3687 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
3688 BUS_NOTIFY_DEL_DEVICE, dev);
3690 dpm_sysfs_remove(dev);
3692 klist_del(&dev->p->knode_parent);
3693 if (MAJOR(dev->devt)) {
3694 devtmpfs_delete_node(dev);
3695 device_remove_sys_dev_entry(dev);
3696 device_remove_file(dev, &dev_attr_dev);
3699 device_remove_class_symlinks(dev);
3701 mutex_lock(&dev->class->p->mutex);
3702 /* notify any interfaces that the device is now gone */
3703 list_for_each_entry(class_intf,
3704 &dev->class->p->interfaces, node)
3705 if (class_intf->remove_dev)
3706 class_intf->remove_dev(dev, class_intf);
3707 /* remove the device from the class list */
3708 klist_del(&dev->p->knode_class);
3709 mutex_unlock(&dev->class->p->mutex);
3711 device_remove_file(dev, &dev_attr_uevent);
3712 device_remove_attrs(dev);
3713 bus_remove_device(dev);
3714 device_pm_remove(dev);
3715 driver_deferred_probe_del(dev);
3716 device_platform_notify_remove(dev);
3717 device_links_purge(dev);
3720 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
3721 BUS_NOTIFY_REMOVED_DEVICE, dev);
3722 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
3723 glue_dir = get_glue_dir(dev);
3724 kobject_del(&dev->kobj);
3725 cleanup_glue_dir(dev, glue_dir);
3726 memalloc_noio_restore(noio_flag);
3729 EXPORT_SYMBOL_GPL(device_del);
3732 * device_unregister - unregister device from system.
3733 * @dev: device going away.
3735 * We do this in two parts, like we do device_register(). First,
3736 * we remove it from all the subsystems with device_del(), then
3737 * we decrement the reference count via put_device(). If that
3738 * is the final reference count, the device will be cleaned up
3739 * via device_release() above. Otherwise, the structure will
3740 * stick around until the final reference to the device is dropped.
3742 void device_unregister(struct device *dev)
3744 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
3748 EXPORT_SYMBOL_GPL(device_unregister);
3750 static struct device *prev_device(struct klist_iter *i)
3752 struct klist_node *n = klist_prev(i);
3753 struct device *dev = NULL;
3754 struct device_private *p;
3757 p = to_device_private_parent(n);
3763 static struct device *next_device(struct klist_iter *i)
3765 struct klist_node *n = klist_next(i);
3766 struct device *dev = NULL;
3767 struct device_private *p;
3770 p = to_device_private_parent(n);
3777 * device_get_devnode - path of device node file
3779 * @mode: returned file access mode
3780 * @uid: returned file owner
3781 * @gid: returned file group
3782 * @tmp: possibly allocated string
3784 * Return the relative path of a possible device node.
3785 * Non-default names may need to allocate a memory to compose
3786 * a name. This memory is returned in tmp and needs to be
3787 * freed by the caller.
3789 const char *device_get_devnode(struct device *dev,
3790 umode_t *mode, kuid_t *uid, kgid_t *gid,
3797 /* the device type may provide a specific name */
3798 if (dev->type && dev->type->devnode)
3799 *tmp = dev->type->devnode(dev, mode, uid, gid);
3803 /* the class may provide a specific name */
3804 if (dev->class && dev->class->devnode)
3805 *tmp = dev->class->devnode(dev, mode);
3809 /* return name without allocation, tmp == NULL */
3810 if (strchr(dev_name(dev), '!') == NULL)
3811 return dev_name(dev);
3813 /* replace '!' in the name with '/' */
3814 s = kstrdup(dev_name(dev), GFP_KERNEL);
3817 strreplace(s, '!', '/');
3822 * device_for_each_child - device child iterator.
3823 * @parent: parent struct device.
3824 * @fn: function to be called for each device.
3825 * @data: data for the callback.
3827 * Iterate over @parent's child devices, and call @fn for each,
3830 * We check the return of @fn each time. If it returns anything
3831 * other than 0, we break out and return that value.
3833 int device_for_each_child(struct device *parent, void *data,
3834 int (*fn)(struct device *dev, void *data))
3836 struct klist_iter i;
3837 struct device *child;
3843 klist_iter_init(&parent->p->klist_children, &i);
3844 while (!error && (child = next_device(&i)))
3845 error = fn(child, data);
3846 klist_iter_exit(&i);
3849 EXPORT_SYMBOL_GPL(device_for_each_child);
3852 * device_for_each_child_reverse - device child iterator in reversed order.
3853 * @parent: parent struct device.
3854 * @fn: function to be called for each device.
3855 * @data: data for the callback.
3857 * Iterate over @parent's child devices, and call @fn for each,
3860 * We check the return of @fn each time. If it returns anything
3861 * other than 0, we break out and return that value.
3863 int device_for_each_child_reverse(struct device *parent, void *data,
3864 int (*fn)(struct device *dev, void *data))
3866 struct klist_iter i;
3867 struct device *child;
3873 klist_iter_init(&parent->p->klist_children, &i);
3874 while ((child = prev_device(&i)) && !error)
3875 error = fn(child, data);
3876 klist_iter_exit(&i);
3879 EXPORT_SYMBOL_GPL(device_for_each_child_reverse);
3882 * device_find_child - device iterator for locating a particular device.
3883 * @parent: parent struct device
3884 * @match: Callback function to check device
3885 * @data: Data to pass to match function
3887 * This is similar to the device_for_each_child() function above, but it
3888 * returns a reference to a device that is 'found' for later use, as
3889 * determined by the @match callback.
3891 * The callback should return 0 if the device doesn't match and non-zero
3892 * if it does. If the callback returns non-zero and a reference to the
3893 * current device can be obtained, this function will return to the caller
3894 * and not iterate over any more devices.
3896 * NOTE: you will need to drop the reference with put_device() after use.
3898 struct device *device_find_child(struct device *parent, void *data,
3899 int (*match)(struct device *dev, void *data))
3901 struct klist_iter i;
3902 struct device *child;
3907 klist_iter_init(&parent->p->klist_children, &i);
3908 while ((child = next_device(&i)))
3909 if (match(child, data) && get_device(child))
3911 klist_iter_exit(&i);
3914 EXPORT_SYMBOL_GPL(device_find_child);
3917 * device_find_child_by_name - device iterator for locating a child device.
3918 * @parent: parent struct device
3919 * @name: name of the child device
3921 * This is similar to the device_find_child() function above, but it
3922 * returns a reference to a device that has the name @name.
3924 * NOTE: you will need to drop the reference with put_device() after use.
3926 struct device *device_find_child_by_name(struct device *parent,
3929 struct klist_iter i;
3930 struct device *child;
3935 klist_iter_init(&parent->p->klist_children, &i);
3936 while ((child = next_device(&i)))
3937 if (sysfs_streq(dev_name(child), name) && get_device(child))
3939 klist_iter_exit(&i);
3942 EXPORT_SYMBOL_GPL(device_find_child_by_name);
3944 static int match_any(struct device *dev, void *unused)
3950 * device_find_any_child - device iterator for locating a child device, if any.
3951 * @parent: parent struct device
3953 * This is similar to the device_find_child() function above, but it
3954 * returns a reference to a child device, if any.
3956 * NOTE: you will need to drop the reference with put_device() after use.
3958 struct device *device_find_any_child(struct device *parent)
3960 return device_find_child(parent, NULL, match_any);
3962 EXPORT_SYMBOL_GPL(device_find_any_child);
3964 int __init devices_init(void)
3966 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
3969 dev_kobj = kobject_create_and_add("dev", NULL);
3972 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
3973 if (!sysfs_dev_block_kobj)
3974 goto block_kobj_err;
3975 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
3976 if (!sysfs_dev_char_kobj)
3982 kobject_put(sysfs_dev_block_kobj);
3984 kobject_put(dev_kobj);
3986 kset_unregister(devices_kset);
3990 static int device_check_offline(struct device *dev, void *not_used)
3994 ret = device_for_each_child(dev, NULL, device_check_offline);
3998 return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
4002 * device_offline - Prepare the device for hot-removal.
4003 * @dev: Device to be put offline.
4005 * Execute the device bus type's .offline() callback, if present, to prepare
4006 * the device for a subsequent hot-removal. If that succeeds, the device must
4007 * not be used until either it is removed or its bus type's .online() callback
4010 * Call under device_hotplug_lock.
4012 int device_offline(struct device *dev)
4016 if (dev->offline_disabled)
4019 ret = device_for_each_child(dev, NULL, device_check_offline);
4024 if (device_supports_offline(dev)) {
4028 ret = dev->bus->offline(dev);
4030 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
4031 dev->offline = true;
4041 * device_online - Put the device back online after successful device_offline().
4042 * @dev: Device to be put back online.
4044 * If device_offline() has been successfully executed for @dev, but the device
4045 * has not been removed subsequently, execute its bus type's .online() callback
4046 * to indicate that the device can be used again.
4048 * Call under device_hotplug_lock.
4050 int device_online(struct device *dev)
4055 if (device_supports_offline(dev)) {
4057 ret = dev->bus->online(dev);
4059 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
4060 dev->offline = false;
4071 struct root_device {
4073 struct module *owner;
4076 static inline struct root_device *to_root_device(struct device *d)
4078 return container_of(d, struct root_device, dev);
4081 static void root_device_release(struct device *dev)
4083 kfree(to_root_device(dev));
4087 * __root_device_register - allocate and register a root device
4088 * @name: root device name
4089 * @owner: owner module of the root device, usually THIS_MODULE
4091 * This function allocates a root device and registers it
4092 * using device_register(). In order to free the returned
4093 * device, use root_device_unregister().
4095 * Root devices are dummy devices which allow other devices
4096 * to be grouped under /sys/devices. Use this function to
4097 * allocate a root device and then use it as the parent of
4098 * any device which should appear under /sys/devices/{name}
4100 * The /sys/devices/{name} directory will also contain a
4101 * 'module' symlink which points to the @owner directory
4104 * Returns &struct device pointer on success, or ERR_PTR() on error.
4106 * Note: You probably want to use root_device_register().
4108 struct device *__root_device_register(const char *name, struct module *owner)
4110 struct root_device *root;
4113 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
4115 return ERR_PTR(err);
4117 err = dev_set_name(&root->dev, "%s", name);
4120 return ERR_PTR(err);
4123 root->dev.release = root_device_release;
4125 err = device_register(&root->dev);
4127 put_device(&root->dev);
4128 return ERR_PTR(err);
4131 #ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
4133 struct module_kobject *mk = &owner->mkobj;
4135 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
4137 device_unregister(&root->dev);
4138 return ERR_PTR(err);
4140 root->owner = owner;
4146 EXPORT_SYMBOL_GPL(__root_device_register);
4149 * root_device_unregister - unregister and free a root device
4150 * @dev: device going away
4152 * This function unregisters and cleans up a device that was created by
4153 * root_device_register().
4155 void root_device_unregister(struct device *dev)
4157 struct root_device *root = to_root_device(dev);
4160 sysfs_remove_link(&root->dev.kobj, "module");
4162 device_unregister(dev);
4164 EXPORT_SYMBOL_GPL(root_device_unregister);
4167 static void device_create_release(struct device *dev)
4169 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
4173 static __printf(6, 0) struct device *
4174 device_create_groups_vargs(struct class *class, struct device *parent,
4175 dev_t devt, void *drvdata,
4176 const struct attribute_group **groups,
4177 const char *fmt, va_list args)
4179 struct device *dev = NULL;
4180 int retval = -ENODEV;
4182 if (IS_ERR_OR_NULL(class))
4185 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
4191 device_initialize(dev);
4194 dev->parent = parent;
4195 dev->groups = groups;
4196 dev->release = device_create_release;
4197 dev_set_drvdata(dev, drvdata);
4199 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
4203 retval = device_add(dev);
4211 return ERR_PTR(retval);
4215 * device_create - creates a device and registers it with sysfs
4216 * @class: pointer to the struct class that this device should be registered to
4217 * @parent: pointer to the parent struct device of this new device, if any
4218 * @devt: the dev_t for the char device to be added
4219 * @drvdata: the data to be added to the device for callbacks
4220 * @fmt: string for the device's name
4222 * This function can be used by char device classes. A struct device
4223 * will be created in sysfs, registered to the specified class.
4225 * A "dev" file will be created, showing the dev_t for the device, if
4226 * the dev_t is not 0,0.
4227 * If a pointer to a parent struct device is passed in, the newly created
4228 * struct device will be a child of that device in sysfs.
4229 * The pointer to the struct device will be returned from the call.
4230 * Any further sysfs files that might be required can be created using this
4233 * Returns &struct device pointer on success, or ERR_PTR() on error.
4235 * Note: the struct class passed to this function must have previously
4236 * been created with a call to class_create().
4238 struct device *device_create(struct class *class, struct device *parent,
4239 dev_t devt, void *drvdata, const char *fmt, ...)
4244 va_start(vargs, fmt);
4245 dev = device_create_groups_vargs(class, parent, devt, drvdata, NULL,
4250 EXPORT_SYMBOL_GPL(device_create);
4253 * device_create_with_groups - creates a device and registers it with sysfs
4254 * @class: pointer to the struct class that this device should be registered to
4255 * @parent: pointer to the parent struct device of this new device, if any
4256 * @devt: the dev_t for the char device to be added
4257 * @drvdata: the data to be added to the device for callbacks
4258 * @groups: NULL-terminated list of attribute groups to be created
4259 * @fmt: string for the device's name
4261 * This function can be used by char device classes. A struct device
4262 * will be created in sysfs, registered to the specified class.
4263 * Additional attributes specified in the groups parameter will also
4264 * be created automatically.
4266 * A "dev" file will be created, showing the dev_t for the device, if
4267 * the dev_t is not 0,0.
4268 * If a pointer to a parent struct device is passed in, the newly created
4269 * struct device will be a child of that device in sysfs.
4270 * The pointer to the struct device will be returned from the call.
4271 * Any further sysfs files that might be required can be created using this
4274 * Returns &struct device pointer on success, or ERR_PTR() on error.
4276 * Note: the struct class passed to this function must have previously
4277 * been created with a call to class_create().
4279 struct device *device_create_with_groups(struct class *class,
4280 struct device *parent, dev_t devt,
4282 const struct attribute_group **groups,
4283 const char *fmt, ...)
4288 va_start(vargs, fmt);
4289 dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
4294 EXPORT_SYMBOL_GPL(device_create_with_groups);
4297 * device_destroy - removes a device that was created with device_create()
4298 * @class: pointer to the struct class that this device was registered with
4299 * @devt: the dev_t of the device that was previously registered
4301 * This call unregisters and cleans up a device that was created with a
4302 * call to device_create().
4304 void device_destroy(struct class *class, dev_t devt)
4308 dev = class_find_device_by_devt(class, devt);
4311 device_unregister(dev);
4314 EXPORT_SYMBOL_GPL(device_destroy);
4317 * device_rename - renames a device
4318 * @dev: the pointer to the struct device to be renamed
4319 * @new_name: the new name of the device
4321 * It is the responsibility of the caller to provide mutual
4322 * exclusion between two different calls of device_rename
4323 * on the same device to ensure that new_name is valid and
4324 * won't conflict with other devices.
4326 * Note: Don't call this function. Currently, the networking layer calls this
4327 * function, but that will change. The following text from Kay Sievers offers
4330 * Renaming devices is racy at many levels, symlinks and other stuff are not
4331 * replaced atomically, and you get a "move" uevent, but it's not easy to
4332 * connect the event to the old and new device. Device nodes are not renamed at
4333 * all, there isn't even support for that in the kernel now.
4335 * In the meantime, during renaming, your target name might be taken by another
4336 * driver, creating conflicts. Or the old name is taken directly after you
4337 * renamed it -- then you get events for the same DEVPATH, before you even see
4338 * the "move" event. It's just a mess, and nothing new should ever rely on
4339 * kernel device renaming. Besides that, it's not even implemented now for
4340 * other things than (driver-core wise very simple) network devices.
4342 * We are currently about to change network renaming in udev to completely
4343 * disallow renaming of devices in the same namespace as the kernel uses,
4344 * because we can't solve the problems properly, that arise with swapping names
4345 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
4346 * be allowed to some other name than eth[0-9]*, for the aforementioned
4349 * Make up a "real" name in the driver before you register anything, or add
4350 * some other attributes for userspace to find the device, or use udev to add
4351 * symlinks -- but never rename kernel devices later, it's a complete mess. We
4352 * don't even want to get into that and try to implement the missing pieces in
4353 * the core. We really have other pieces to fix in the driver core mess. :)
4355 int device_rename(struct device *dev, const char *new_name)
4357 struct kobject *kobj = &dev->kobj;
4358 char *old_device_name = NULL;
4361 dev = get_device(dev);
4365 dev_dbg(dev, "renaming to %s\n", new_name);
4367 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
4368 if (!old_device_name) {
4374 error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj,
4375 kobj, old_device_name,
4376 new_name, kobject_namespace(kobj));
4381 error = kobject_rename(kobj, new_name);
4388 kfree(old_device_name);
4392 EXPORT_SYMBOL_GPL(device_rename);
4394 static int device_move_class_links(struct device *dev,
4395 struct device *old_parent,
4396 struct device *new_parent)
4401 sysfs_remove_link(&dev->kobj, "device");
4403 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
4409 * device_move - moves a device to a new parent
4410 * @dev: the pointer to the struct device to be moved
4411 * @new_parent: the new parent of the device (can be NULL)
4412 * @dpm_order: how to reorder the dpm_list
4414 int device_move(struct device *dev, struct device *new_parent,
4415 enum dpm_order dpm_order)
4418 struct device *old_parent;
4419 struct kobject *new_parent_kobj;
4421 dev = get_device(dev);
4426 new_parent = get_device(new_parent);
4427 new_parent_kobj = get_device_parent(dev, new_parent);
4428 if (IS_ERR(new_parent_kobj)) {
4429 error = PTR_ERR(new_parent_kobj);
4430 put_device(new_parent);
4434 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
4435 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
4436 error = kobject_move(&dev->kobj, new_parent_kobj);
4438 cleanup_glue_dir(dev, new_parent_kobj);
4439 put_device(new_parent);
4442 old_parent = dev->parent;
4443 dev->parent = new_parent;
4445 klist_remove(&dev->p->knode_parent);
4447 klist_add_tail(&dev->p->knode_parent,
4448 &new_parent->p->klist_children);
4449 set_dev_node(dev, dev_to_node(new_parent));
4453 error = device_move_class_links(dev, old_parent, new_parent);
4455 /* We ignore errors on cleanup since we're hosed anyway... */
4456 device_move_class_links(dev, new_parent, old_parent);
4457 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
4459 klist_remove(&dev->p->knode_parent);
4460 dev->parent = old_parent;
4462 klist_add_tail(&dev->p->knode_parent,
4463 &old_parent->p->klist_children);
4464 set_dev_node(dev, dev_to_node(old_parent));
4467 cleanup_glue_dir(dev, new_parent_kobj);
4468 put_device(new_parent);
4472 switch (dpm_order) {
4473 case DPM_ORDER_NONE:
4475 case DPM_ORDER_DEV_AFTER_PARENT:
4476 device_pm_move_after(dev, new_parent);
4477 devices_kset_move_after(dev, new_parent);
4479 case DPM_ORDER_PARENT_BEFORE_DEV:
4480 device_pm_move_before(new_parent, dev);
4481 devices_kset_move_before(new_parent, dev);
4483 case DPM_ORDER_DEV_LAST:
4484 device_pm_move_last(dev);
4485 devices_kset_move_last(dev);
4489 put_device(old_parent);
4495 EXPORT_SYMBOL_GPL(device_move);
4497 static int device_attrs_change_owner(struct device *dev, kuid_t kuid,
4500 struct kobject *kobj = &dev->kobj;
4501 struct class *class = dev->class;
4502 const struct device_type *type = dev->type;
4507 * Change the device groups of the device class for @dev to
4510 error = sysfs_groups_change_owner(kobj, class->dev_groups, kuid,
4518 * Change the device groups of the device type for @dev to
4521 error = sysfs_groups_change_owner(kobj, type->groups, kuid,
4527 /* Change the device groups of @dev to @kuid/@kgid. */
4528 error = sysfs_groups_change_owner(kobj, dev->groups, kuid, kgid);
4532 if (device_supports_offline(dev) && !dev->offline_disabled) {
4533 /* Change online device attributes of @dev to @kuid/@kgid. */
4534 error = sysfs_file_change_owner(kobj, dev_attr_online.attr.name,
4544 * device_change_owner - change the owner of an existing device.
4546 * @kuid: new owner's kuid
4547 * @kgid: new owner's kgid
4549 * This changes the owner of @dev and its corresponding sysfs entries to
4550 * @kuid/@kgid. This function closely mirrors how @dev was added via driver
4553 * Returns 0 on success or error code on failure.
4555 int device_change_owner(struct device *dev, kuid_t kuid, kgid_t kgid)
4558 struct kobject *kobj = &dev->kobj;
4560 dev = get_device(dev);
4565 * Change the kobject and the default attributes and groups of the
4566 * ktype associated with it to @kuid/@kgid.
4568 error = sysfs_change_owner(kobj, kuid, kgid);
4573 * Change the uevent file for @dev to the new owner. The uevent file
4574 * was created in a separate step when @dev got added and we mirror
4577 error = sysfs_file_change_owner(kobj, dev_attr_uevent.attr.name, kuid,
4583 * Change the device groups, the device groups associated with the
4584 * device class, and the groups associated with the device type of @dev
4587 error = device_attrs_change_owner(dev, kuid, kgid);
4591 error = dpm_sysfs_change_owner(dev, kuid, kgid);
4596 if (sysfs_deprecated && dev->class == &block_class)
4601 * Change the owner of the symlink located in the class directory of
4602 * the device class associated with @dev which points to the actual
4603 * directory entry for @dev to @kuid/@kgid. This ensures that the
4604 * symlink shows the same permissions as its target.
4606 error = sysfs_link_change_owner(&dev->class->p->subsys.kobj, &dev->kobj,
4607 dev_name(dev), kuid, kgid);
4615 EXPORT_SYMBOL_GPL(device_change_owner);
4618 * device_shutdown - call ->shutdown() on each device to shutdown.
4620 void device_shutdown(void)
4622 struct device *dev, *parent;
4624 wait_for_device_probe();
4625 device_block_probing();
4629 spin_lock(&devices_kset->list_lock);
4631 * Walk the devices list backward, shutting down each in turn.
4632 * Beware that device unplug events may also start pulling
4633 * devices offline, even as the system is shutting down.
4635 while (!list_empty(&devices_kset->list)) {
4636 dev = list_entry(devices_kset->list.prev, struct device,
4640 * hold reference count of device's parent to
4641 * prevent it from being freed because parent's
4642 * lock is to be held
4644 parent = get_device(dev->parent);
4647 * Make sure the device is off the kset list, in the
4648 * event that dev->*->shutdown() doesn't remove it.
4650 list_del_init(&dev->kobj.entry);
4651 spin_unlock(&devices_kset->list_lock);
4653 /* hold lock to avoid race with probe/release */
4655 device_lock(parent);
4658 /* Don't allow any more runtime suspends */
4659 pm_runtime_get_noresume(dev);
4660 pm_runtime_barrier(dev);
4662 if (dev->class && dev->class->shutdown_pre) {
4664 dev_info(dev, "shutdown_pre\n");
4665 dev->class->shutdown_pre(dev);
4667 if (dev->bus && dev->bus->shutdown) {
4669 dev_info(dev, "shutdown\n");
4670 dev->bus->shutdown(dev);
4671 } else if (dev->driver && dev->driver->shutdown) {
4673 dev_info(dev, "shutdown\n");
4674 dev->driver->shutdown(dev);
4679 device_unlock(parent);
4684 spin_lock(&devices_kset->list_lock);
4686 spin_unlock(&devices_kset->list_lock);
4690 * Device logging functions
4693 #ifdef CONFIG_PRINTK
4695 set_dev_info(const struct device *dev, struct dev_printk_info *dev_info)
4699 memset(dev_info, 0, sizeof(*dev_info));
4702 subsys = dev->class->name;
4704 subsys = dev->bus->name;
4708 strscpy(dev_info->subsystem, subsys, sizeof(dev_info->subsystem));
4711 * Add device identifier DEVICE=:
4715 * +sound:card0 subsystem:devname
4717 if (MAJOR(dev->devt)) {
4720 if (strcmp(subsys, "block") == 0)
4725 snprintf(dev_info->device, sizeof(dev_info->device),
4726 "%c%u:%u", c, MAJOR(dev->devt), MINOR(dev->devt));
4727 } else if (strcmp(subsys, "net") == 0) {
4728 struct net_device *net = to_net_dev(dev);
4730 snprintf(dev_info->device, sizeof(dev_info->device),
4731 "n%u", net->ifindex);
4733 snprintf(dev_info->device, sizeof(dev_info->device),
4734 "+%s:%s", subsys, dev_name(dev));
4738 int dev_vprintk_emit(int level, const struct device *dev,
4739 const char *fmt, va_list args)
4741 struct dev_printk_info dev_info;
4743 set_dev_info(dev, &dev_info);
4745 return vprintk_emit(0, level, &dev_info, fmt, args);
4747 EXPORT_SYMBOL(dev_vprintk_emit);
4749 int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
4754 va_start(args, fmt);
4756 r = dev_vprintk_emit(level, dev, fmt, args);
4762 EXPORT_SYMBOL(dev_printk_emit);
4764 static void __dev_printk(const char *level, const struct device *dev,
4765 struct va_format *vaf)
4768 dev_printk_emit(level[1] - '0', dev, "%s %s: %pV",
4769 dev_driver_string(dev), dev_name(dev), vaf);
4771 printk("%s(NULL device *): %pV", level, vaf);
4774 void _dev_printk(const char *level, const struct device *dev,
4775 const char *fmt, ...)
4777 struct va_format vaf;
4780 va_start(args, fmt);
4785 __dev_printk(level, dev, &vaf);
4789 EXPORT_SYMBOL(_dev_printk);
4791 #define define_dev_printk_level(func, kern_level) \
4792 void func(const struct device *dev, const char *fmt, ...) \
4794 struct va_format vaf; \
4797 va_start(args, fmt); \
4802 __dev_printk(kern_level, dev, &vaf); \
4806 EXPORT_SYMBOL(func);
4808 define_dev_printk_level(_dev_emerg, KERN_EMERG);
4809 define_dev_printk_level(_dev_alert, KERN_ALERT);
4810 define_dev_printk_level(_dev_crit, KERN_CRIT);
4811 define_dev_printk_level(_dev_err, KERN_ERR);
4812 define_dev_printk_level(_dev_warn, KERN_WARNING);
4813 define_dev_printk_level(_dev_notice, KERN_NOTICE);
4814 define_dev_printk_level(_dev_info, KERN_INFO);
4819 * dev_err_probe - probe error check and log helper
4820 * @dev: the pointer to the struct device
4821 * @err: error value to test
4822 * @fmt: printf-style format string
4823 * @...: arguments as specified in the format string
4825 * This helper implements common pattern present in probe functions for error
4826 * checking: print debug or error message depending if the error value is
4827 * -EPROBE_DEFER and propagate error upwards.
4828 * In case of -EPROBE_DEFER it sets also defer probe reason, which can be
4829 * checked later by reading devices_deferred debugfs attribute.
4830 * It replaces code sequence::
4832 * if (err != -EPROBE_DEFER)
4833 * dev_err(dev, ...);
4835 * dev_dbg(dev, ...);
4840 * return dev_err_probe(dev, err, ...);
4842 * Note that it is deemed acceptable to use this function for error
4843 * prints during probe even if the @err is known to never be -EPROBE_DEFER.
4844 * The benefit compared to a normal dev_err() is the standardized format
4845 * of the error code and the fact that the error code is returned.
4850 int dev_err_probe(const struct device *dev, int err, const char *fmt, ...)
4852 struct va_format vaf;
4855 va_start(args, fmt);
4859 if (err != -EPROBE_DEFER) {
4860 dev_err(dev, "error %pe: %pV", ERR_PTR(err), &vaf);
4862 device_set_deferred_probe_reason(dev, &vaf);
4863 dev_dbg(dev, "error %pe: %pV", ERR_PTR(err), &vaf);
4870 EXPORT_SYMBOL_GPL(dev_err_probe);
4872 static inline bool fwnode_is_primary(struct fwnode_handle *fwnode)
4874 return fwnode && !IS_ERR(fwnode->secondary);
4878 * set_primary_fwnode - Change the primary firmware node of a given device.
4879 * @dev: Device to handle.
4880 * @fwnode: New primary firmware node of the device.
4882 * Set the device's firmware node pointer to @fwnode, but if a secondary
4883 * firmware node of the device is present, preserve it.
4885 * Valid fwnode cases are:
4886 * - primary --> secondary --> -ENODEV
4887 * - primary --> NULL
4888 * - secondary --> -ENODEV
4891 void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
4893 struct device *parent = dev->parent;
4894 struct fwnode_handle *fn = dev->fwnode;
4897 if (fwnode_is_primary(fn))
4901 WARN_ON(fwnode->secondary);
4902 fwnode->secondary = fn;
4904 dev->fwnode = fwnode;
4906 if (fwnode_is_primary(fn)) {
4907 dev->fwnode = fn->secondary;
4908 /* Set fn->secondary = NULL, so fn remains the primary fwnode */
4909 if (!(parent && fn == parent->fwnode))
4910 fn->secondary = NULL;
4916 EXPORT_SYMBOL_GPL(set_primary_fwnode);
4919 * set_secondary_fwnode - Change the secondary firmware node of a given device.
4920 * @dev: Device to handle.
4921 * @fwnode: New secondary firmware node of the device.
4923 * If a primary firmware node of the device is present, set its secondary
4924 * pointer to @fwnode. Otherwise, set the device's firmware node pointer to
4927 void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
4930 fwnode->secondary = ERR_PTR(-ENODEV);
4932 if (fwnode_is_primary(dev->fwnode))
4933 dev->fwnode->secondary = fwnode;
4935 dev->fwnode = fwnode;
4937 EXPORT_SYMBOL_GPL(set_secondary_fwnode);
4940 * device_set_of_node_from_dev - reuse device-tree node of another device
4941 * @dev: device whose device-tree node is being set
4942 * @dev2: device whose device-tree node is being reused
4944 * Takes another reference to the new device-tree node after first dropping
4945 * any reference held to the old node.
4947 void device_set_of_node_from_dev(struct device *dev, const struct device *dev2)
4949 of_node_put(dev->of_node);
4950 dev->of_node = of_node_get(dev2->of_node);
4951 dev->of_node_reused = true;
4953 EXPORT_SYMBOL_GPL(device_set_of_node_from_dev);
4955 void device_set_node(struct device *dev, struct fwnode_handle *fwnode)
4957 dev->fwnode = fwnode;
4958 dev->of_node = to_of_node(fwnode);
4960 EXPORT_SYMBOL_GPL(device_set_node);
4962 int device_match_name(struct device *dev, const void *name)
4964 return sysfs_streq(dev_name(dev), name);
4966 EXPORT_SYMBOL_GPL(device_match_name);
4968 int device_match_of_node(struct device *dev, const void *np)
4970 return dev->of_node == np;
4972 EXPORT_SYMBOL_GPL(device_match_of_node);
4974 int device_match_fwnode(struct device *dev, const void *fwnode)
4976 return dev_fwnode(dev) == fwnode;
4978 EXPORT_SYMBOL_GPL(device_match_fwnode);
4980 int device_match_devt(struct device *dev, const void *pdevt)
4982 return dev->devt == *(dev_t *)pdevt;
4984 EXPORT_SYMBOL_GPL(device_match_devt);
4986 int device_match_acpi_dev(struct device *dev, const void *adev)
4988 return ACPI_COMPANION(dev) == adev;
4990 EXPORT_SYMBOL(device_match_acpi_dev);
4992 int device_match_acpi_handle(struct device *dev, const void *handle)
4994 return ACPI_HANDLE(dev) == handle;
4996 EXPORT_SYMBOL(device_match_acpi_handle);
4998 int device_match_any(struct device *dev, const void *unused)
5002 EXPORT_SYMBOL_GPL(device_match_any);