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
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/kstrtox.h>
18 #include <linux/module.h>
19 #include <linux/slab.h>
20 #include <linux/string.h>
21 #include <linux/kdev_t.h>
22 #include <linux/notifier.h>
24 #include <linux/of_device.h>
25 #include <linux/blkdev.h>
26 #include <linux/mutex.h>
27 #include <linux/pm_runtime.h>
28 #include <linux/netdevice.h>
29 #include <linux/sched/signal.h>
30 #include <linux/sched/mm.h>
31 #include <linux/swiotlb.h>
32 #include <linux/sysfs.h>
33 #include <linux/dma-map-ops.h> /* for dma_default_coherent */
36 #include "physical_location.h"
37 #include "power/power.h"
39 #ifdef CONFIG_SYSFS_DEPRECATED
40 #ifdef CONFIG_SYSFS_DEPRECATED_V2
41 long sysfs_deprecated = 1;
43 long sysfs_deprecated = 0;
45 static int __init sysfs_deprecated_setup(char *arg)
47 return kstrtol(arg, 10, &sysfs_deprecated);
49 early_param("sysfs.deprecated", sysfs_deprecated_setup);
52 /* Device links support. */
53 static LIST_HEAD(deferred_sync);
54 static unsigned int defer_sync_state_count = 1;
55 static DEFINE_MUTEX(fwnode_link_lock);
56 static bool fw_devlink_is_permissive(void);
57 static bool fw_devlink_drv_reg_done;
58 static bool fw_devlink_best_effort;
61 * fwnode_link_add - Create a link between two fwnode_handles.
62 * @con: Consumer end of the link.
63 * @sup: Supplier end of the link.
65 * Create a fwnode link between fwnode handles @con and @sup. The fwnode link
66 * represents the detail that the firmware lists @sup fwnode as supplying a
69 * The driver core will use the fwnode link to create a device link between the
70 * two device objects corresponding to @con and @sup when they are created. The
71 * driver core will automatically delete the fwnode link between @con and @sup
74 * Attempts to create duplicate links between the same pair of fwnode handles
75 * are ignored and there is no reference counting.
77 int fwnode_link_add(struct fwnode_handle *con, struct fwnode_handle *sup)
79 struct fwnode_link *link;
82 mutex_lock(&fwnode_link_lock);
84 list_for_each_entry(link, &sup->consumers, s_hook)
85 if (link->consumer == con)
88 link = kzalloc(sizeof(*link), GFP_KERNEL);
95 INIT_LIST_HEAD(&link->s_hook);
97 INIT_LIST_HEAD(&link->c_hook);
99 list_add(&link->s_hook, &sup->consumers);
100 list_add(&link->c_hook, &con->suppliers);
101 pr_debug("%pfwP Linked as a fwnode consumer to %pfwP\n",
104 mutex_unlock(&fwnode_link_lock);
110 * __fwnode_link_del - Delete a link between two fwnode_handles.
111 * @link: the fwnode_link to be deleted
113 * The fwnode_link_lock needs to be held when this function is called.
115 static void __fwnode_link_del(struct fwnode_link *link)
117 pr_debug("%pfwP Dropping the fwnode link to %pfwP\n",
118 link->consumer, link->supplier);
119 list_del(&link->s_hook);
120 list_del(&link->c_hook);
125 * fwnode_links_purge_suppliers - Delete all supplier links of fwnode_handle.
126 * @fwnode: fwnode whose supplier links need to be deleted
128 * Deletes all supplier links connecting directly to @fwnode.
130 static void fwnode_links_purge_suppliers(struct fwnode_handle *fwnode)
132 struct fwnode_link *link, *tmp;
134 mutex_lock(&fwnode_link_lock);
135 list_for_each_entry_safe(link, tmp, &fwnode->suppliers, c_hook)
136 __fwnode_link_del(link);
137 mutex_unlock(&fwnode_link_lock);
141 * fwnode_links_purge_consumers - Delete all consumer links of fwnode_handle.
142 * @fwnode: fwnode whose consumer links need to be deleted
144 * Deletes all consumer links connecting directly to @fwnode.
146 static void fwnode_links_purge_consumers(struct fwnode_handle *fwnode)
148 struct fwnode_link *link, *tmp;
150 mutex_lock(&fwnode_link_lock);
151 list_for_each_entry_safe(link, tmp, &fwnode->consumers, s_hook)
152 __fwnode_link_del(link);
153 mutex_unlock(&fwnode_link_lock);
157 * fwnode_links_purge - Delete all links connected to a fwnode_handle.
158 * @fwnode: fwnode whose links needs to be deleted
160 * Deletes all links connecting directly to a fwnode.
162 void fwnode_links_purge(struct fwnode_handle *fwnode)
164 fwnode_links_purge_suppliers(fwnode);
165 fwnode_links_purge_consumers(fwnode);
168 void fw_devlink_purge_absent_suppliers(struct fwnode_handle *fwnode)
170 struct fwnode_handle *child;
172 /* Don't purge consumer links of an added child */
176 fwnode->flags |= FWNODE_FLAG_NOT_DEVICE;
177 fwnode_links_purge_consumers(fwnode);
179 fwnode_for_each_available_child_node(fwnode, child)
180 fw_devlink_purge_absent_suppliers(child);
182 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);
223 static bool device_is_ancestor(struct device *dev, struct device *target)
225 while (target->parent) {
226 target = target->parent;
234 * device_is_dependent - Check if one device depends on another one
235 * @dev: Device to check dependencies for.
236 * @target: Device to check against.
238 * Check if @target depends on @dev or any device dependent on it (its child or
239 * its consumer etc). Return 1 if that is the case or 0 otherwise.
241 int device_is_dependent(struct device *dev, void *target)
243 struct device_link *link;
247 * The "ancestors" check is needed to catch the case when the target
248 * device has not been completely initialized yet and it is still
249 * missing from the list of children of its parent device.
251 if (dev == target || device_is_ancestor(dev, target))
254 ret = device_for_each_child(dev, target, device_is_dependent);
258 list_for_each_entry(link, &dev->links.consumers, s_node) {
259 if ((link->flags & ~DL_FLAG_INFERRED) ==
260 (DL_FLAG_SYNC_STATE_ONLY | DL_FLAG_MANAGED))
263 if (link->consumer == target)
266 ret = device_is_dependent(link->consumer, target);
273 static void device_link_init_status(struct device_link *link,
274 struct device *consumer,
275 struct device *supplier)
277 switch (supplier->links.status) {
279 switch (consumer->links.status) {
282 * A consumer driver can create a link to a supplier
283 * that has not completed its probing yet as long as it
284 * knows that the supplier is already functional (for
285 * example, it has just acquired some resources from the
288 link->status = DL_STATE_CONSUMER_PROBE;
291 link->status = DL_STATE_DORMANT;
295 case DL_DEV_DRIVER_BOUND:
296 switch (consumer->links.status) {
298 link->status = DL_STATE_CONSUMER_PROBE;
300 case DL_DEV_DRIVER_BOUND:
301 link->status = DL_STATE_ACTIVE;
304 link->status = DL_STATE_AVAILABLE;
308 case DL_DEV_UNBINDING:
309 link->status = DL_STATE_SUPPLIER_UNBIND;
312 link->status = DL_STATE_DORMANT;
317 static int device_reorder_to_tail(struct device *dev, void *not_used)
319 struct device_link *link;
322 * Devices that have not been registered yet will be put to the ends
323 * of the lists during the registration, so skip them here.
325 if (device_is_registered(dev))
326 devices_kset_move_last(dev);
328 if (device_pm_initialized(dev))
329 device_pm_move_last(dev);
331 device_for_each_child(dev, NULL, device_reorder_to_tail);
332 list_for_each_entry(link, &dev->links.consumers, s_node) {
333 if ((link->flags & ~DL_FLAG_INFERRED) ==
334 (DL_FLAG_SYNC_STATE_ONLY | DL_FLAG_MANAGED))
336 device_reorder_to_tail(link->consumer, NULL);
343 * device_pm_move_to_tail - Move set of devices to the end of device lists
344 * @dev: Device to move
346 * This is a device_reorder_to_tail() wrapper taking the requisite locks.
348 * It moves the @dev along with all of its children and all of its consumers
349 * to the ends of the device_kset and dpm_list, recursively.
351 void device_pm_move_to_tail(struct device *dev)
355 idx = device_links_read_lock();
357 device_reorder_to_tail(dev, NULL);
359 device_links_read_unlock(idx);
362 #define to_devlink(dev) container_of((dev), struct device_link, link_dev)
364 static ssize_t status_show(struct device *dev,
365 struct device_attribute *attr, char *buf)
369 switch (to_devlink(dev)->status) {
371 output = "not tracked";
373 case DL_STATE_DORMANT:
376 case DL_STATE_AVAILABLE:
377 output = "available";
379 case DL_STATE_CONSUMER_PROBE:
380 output = "consumer probing";
382 case DL_STATE_ACTIVE:
385 case DL_STATE_SUPPLIER_UNBIND:
386 output = "supplier unbinding";
393 return sysfs_emit(buf, "%s\n", output);
395 static DEVICE_ATTR_RO(status);
397 static ssize_t auto_remove_on_show(struct device *dev,
398 struct device_attribute *attr, char *buf)
400 struct device_link *link = to_devlink(dev);
403 if (link->flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
404 output = "supplier unbind";
405 else if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER)
406 output = "consumer unbind";
410 return sysfs_emit(buf, "%s\n", output);
412 static DEVICE_ATTR_RO(auto_remove_on);
414 static ssize_t runtime_pm_show(struct device *dev,
415 struct device_attribute *attr, char *buf)
417 struct device_link *link = to_devlink(dev);
419 return sysfs_emit(buf, "%d\n", !!(link->flags & DL_FLAG_PM_RUNTIME));
421 static DEVICE_ATTR_RO(runtime_pm);
423 static ssize_t sync_state_only_show(struct device *dev,
424 struct device_attribute *attr, char *buf)
426 struct device_link *link = to_devlink(dev);
428 return sysfs_emit(buf, "%d\n",
429 !!(link->flags & DL_FLAG_SYNC_STATE_ONLY));
431 static DEVICE_ATTR_RO(sync_state_only);
433 static struct attribute *devlink_attrs[] = {
434 &dev_attr_status.attr,
435 &dev_attr_auto_remove_on.attr,
436 &dev_attr_runtime_pm.attr,
437 &dev_attr_sync_state_only.attr,
440 ATTRIBUTE_GROUPS(devlink);
442 static void device_link_release_fn(struct work_struct *work)
444 struct device_link *link = container_of(work, struct device_link, rm_work);
446 /* Ensure that all references to the link object have been dropped. */
447 device_link_synchronize_removal();
449 pm_runtime_release_supplier(link);
451 * If supplier_preactivated is set, the link has been dropped between
452 * the pm_runtime_get_suppliers() and pm_runtime_put_suppliers() calls
453 * in __driver_probe_device(). In that case, drop the supplier's
454 * PM-runtime usage counter to remove the reference taken by
455 * pm_runtime_get_suppliers().
457 if (link->supplier_preactivated)
458 pm_runtime_put_noidle(link->supplier);
460 pm_request_idle(link->supplier);
462 put_device(link->consumer);
463 put_device(link->supplier);
467 static void devlink_dev_release(struct device *dev)
469 struct device_link *link = to_devlink(dev);
471 INIT_WORK(&link->rm_work, device_link_release_fn);
473 * It may take a while to complete this work because of the SRCU
474 * synchronization in device_link_release_fn() and if the consumer or
475 * supplier devices get deleted when it runs, so put it into the "long"
478 queue_work(system_long_wq, &link->rm_work);
481 static struct class devlink_class = {
483 .owner = THIS_MODULE,
484 .dev_groups = devlink_groups,
485 .dev_release = devlink_dev_release,
488 static int devlink_add_symlinks(struct device *dev,
489 struct class_interface *class_intf)
493 struct device_link *link = to_devlink(dev);
494 struct device *sup = link->supplier;
495 struct device *con = link->consumer;
498 len = max(strlen(dev_bus_name(sup)) + strlen(dev_name(sup)),
499 strlen(dev_bus_name(con)) + strlen(dev_name(con)));
501 len += strlen("supplier:") + 1;
502 buf = kzalloc(len, GFP_KERNEL);
506 ret = sysfs_create_link(&link->link_dev.kobj, &sup->kobj, "supplier");
510 ret = sysfs_create_link(&link->link_dev.kobj, &con->kobj, "consumer");
514 snprintf(buf, len, "consumer:%s:%s", dev_bus_name(con), dev_name(con));
515 ret = sysfs_create_link(&sup->kobj, &link->link_dev.kobj, buf);
519 snprintf(buf, len, "supplier:%s:%s", dev_bus_name(sup), dev_name(sup));
520 ret = sysfs_create_link(&con->kobj, &link->link_dev.kobj, buf);
527 snprintf(buf, len, "consumer:%s:%s", dev_bus_name(con), dev_name(con));
528 sysfs_remove_link(&sup->kobj, buf);
530 sysfs_remove_link(&link->link_dev.kobj, "consumer");
532 sysfs_remove_link(&link->link_dev.kobj, "supplier");
538 static void devlink_remove_symlinks(struct device *dev,
539 struct class_interface *class_intf)
541 struct device_link *link = to_devlink(dev);
543 struct device *sup = link->supplier;
544 struct device *con = link->consumer;
547 sysfs_remove_link(&link->link_dev.kobj, "consumer");
548 sysfs_remove_link(&link->link_dev.kobj, "supplier");
550 len = max(strlen(dev_bus_name(sup)) + strlen(dev_name(sup)),
551 strlen(dev_bus_name(con)) + strlen(dev_name(con)));
553 len += strlen("supplier:") + 1;
554 buf = kzalloc(len, GFP_KERNEL);
556 WARN(1, "Unable to properly free device link symlinks!\n");
560 if (device_is_registered(con)) {
561 snprintf(buf, len, "supplier:%s:%s", dev_bus_name(sup), dev_name(sup));
562 sysfs_remove_link(&con->kobj, buf);
564 snprintf(buf, len, "consumer:%s:%s", dev_bus_name(con), dev_name(con));
565 sysfs_remove_link(&sup->kobj, buf);
569 static struct class_interface devlink_class_intf = {
570 .class = &devlink_class,
571 .add_dev = devlink_add_symlinks,
572 .remove_dev = devlink_remove_symlinks,
575 static int __init devlink_class_init(void)
579 ret = class_register(&devlink_class);
583 ret = class_interface_register(&devlink_class_intf);
585 class_unregister(&devlink_class);
589 postcore_initcall(devlink_class_init);
591 #define DL_MANAGED_LINK_FLAGS (DL_FLAG_AUTOREMOVE_CONSUMER | \
592 DL_FLAG_AUTOREMOVE_SUPPLIER | \
593 DL_FLAG_AUTOPROBE_CONSUMER | \
594 DL_FLAG_SYNC_STATE_ONLY | \
597 #define DL_ADD_VALID_FLAGS (DL_MANAGED_LINK_FLAGS | DL_FLAG_STATELESS | \
598 DL_FLAG_PM_RUNTIME | DL_FLAG_RPM_ACTIVE)
601 * device_link_add - Create a link between two devices.
602 * @consumer: Consumer end of the link.
603 * @supplier: Supplier end of the link.
604 * @flags: Link flags.
606 * The caller is responsible for the proper synchronization of the link creation
607 * with runtime PM. First, setting the DL_FLAG_PM_RUNTIME flag will cause the
608 * runtime PM framework to take the link into account. Second, if the
609 * DL_FLAG_RPM_ACTIVE flag is set in addition to it, the supplier devices will
610 * be forced into the active meta state and reference-counted upon the creation
611 * of the link. If DL_FLAG_PM_RUNTIME is not set, DL_FLAG_RPM_ACTIVE will be
614 * If DL_FLAG_STATELESS is set in @flags, the caller of this function is
615 * expected to release the link returned by it directly with the help of either
616 * device_link_del() or device_link_remove().
618 * If that flag is not set, however, the caller of this function is handing the
619 * management of the link over to the driver core entirely and its return value
620 * can only be used to check whether or not the link is present. In that case,
621 * the DL_FLAG_AUTOREMOVE_CONSUMER and DL_FLAG_AUTOREMOVE_SUPPLIER device link
622 * flags can be used to indicate to the driver core when the link can be safely
623 * deleted. Namely, setting one of them in @flags indicates to the driver core
624 * that the link is not going to be used (by the given caller of this function)
625 * after unbinding the consumer or supplier driver, respectively, from its
626 * device, so the link can be deleted at that point. If none of them is set,
627 * the link will be maintained until one of the devices pointed to by it (either
628 * the consumer or the supplier) is unregistered.
630 * Also, if DL_FLAG_STATELESS, DL_FLAG_AUTOREMOVE_CONSUMER and
631 * DL_FLAG_AUTOREMOVE_SUPPLIER are not set in @flags (that is, a persistent
632 * managed device link is being added), the DL_FLAG_AUTOPROBE_CONSUMER flag can
633 * be used to request the driver core to automatically probe for a consumer
634 * driver after successfully binding a driver to the supplier device.
636 * The combination of DL_FLAG_STATELESS and one of DL_FLAG_AUTOREMOVE_CONSUMER,
637 * DL_FLAG_AUTOREMOVE_SUPPLIER, or DL_FLAG_AUTOPROBE_CONSUMER set in @flags at
638 * the same time is invalid and will cause NULL to be returned upfront.
639 * However, if a device link between the given @consumer and @supplier pair
640 * exists already when this function is called for them, the existing link will
641 * be returned regardless of its current type and status (the link's flags may
642 * be modified then). The caller of this function is then expected to treat
643 * the link as though it has just been created, so (in particular) if
644 * DL_FLAG_STATELESS was passed in @flags, the link needs to be released
645 * explicitly when not needed any more (as stated above).
647 * A side effect of the link creation is re-ordering of dpm_list and the
648 * devices_kset list by moving the consumer device and all devices depending
649 * on it to the ends of these lists (that does not happen to devices that have
650 * not been registered when this function is called).
652 * The supplier device is required to be registered when this function is called
653 * and NULL will be returned if that is not the case. The consumer device need
654 * not be registered, however.
656 struct device_link *device_link_add(struct device *consumer,
657 struct device *supplier, u32 flags)
659 struct device_link *link;
661 if (!consumer || !supplier || consumer == supplier ||
662 flags & ~DL_ADD_VALID_FLAGS ||
663 (flags & DL_FLAG_STATELESS && flags & DL_MANAGED_LINK_FLAGS) ||
664 (flags & DL_FLAG_SYNC_STATE_ONLY &&
665 (flags & ~DL_FLAG_INFERRED) != DL_FLAG_SYNC_STATE_ONLY) ||
666 (flags & DL_FLAG_AUTOPROBE_CONSUMER &&
667 flags & (DL_FLAG_AUTOREMOVE_CONSUMER |
668 DL_FLAG_AUTOREMOVE_SUPPLIER)))
671 if (flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) {
672 if (pm_runtime_get_sync(supplier) < 0) {
673 pm_runtime_put_noidle(supplier);
678 if (!(flags & DL_FLAG_STATELESS))
679 flags |= DL_FLAG_MANAGED;
681 device_links_write_lock();
685 * If the supplier has not been fully registered yet or there is a
686 * reverse (non-SYNC_STATE_ONLY) dependency between the consumer and
687 * the supplier already in the graph, return NULL. If the link is a
688 * SYNC_STATE_ONLY link, we don't check for reverse dependencies
689 * because it only affects sync_state() callbacks.
691 if (!device_pm_initialized(supplier)
692 || (!(flags & DL_FLAG_SYNC_STATE_ONLY) &&
693 device_is_dependent(consumer, supplier))) {
699 * SYNC_STATE_ONLY links are useless once a consumer device has probed.
700 * So, only create it if the consumer hasn't probed yet.
702 if (flags & DL_FLAG_SYNC_STATE_ONLY &&
703 consumer->links.status != DL_DEV_NO_DRIVER &&
704 consumer->links.status != DL_DEV_PROBING) {
710 * DL_FLAG_AUTOREMOVE_SUPPLIER indicates that the link will be needed
711 * longer than for DL_FLAG_AUTOREMOVE_CONSUMER and setting them both
712 * together doesn't make sense, so prefer DL_FLAG_AUTOREMOVE_SUPPLIER.
714 if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
715 flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER;
717 list_for_each_entry(link, &supplier->links.consumers, s_node) {
718 if (link->consumer != consumer)
721 if (link->flags & DL_FLAG_INFERRED &&
722 !(flags & DL_FLAG_INFERRED))
723 link->flags &= ~DL_FLAG_INFERRED;
725 if (flags & DL_FLAG_PM_RUNTIME) {
726 if (!(link->flags & DL_FLAG_PM_RUNTIME)) {
727 pm_runtime_new_link(consumer);
728 link->flags |= DL_FLAG_PM_RUNTIME;
730 if (flags & DL_FLAG_RPM_ACTIVE)
731 refcount_inc(&link->rpm_active);
734 if (flags & DL_FLAG_STATELESS) {
735 kref_get(&link->kref);
736 if (link->flags & DL_FLAG_SYNC_STATE_ONLY &&
737 !(link->flags & DL_FLAG_STATELESS)) {
738 link->flags |= DL_FLAG_STATELESS;
741 link->flags |= DL_FLAG_STATELESS;
747 * If the life time of the link following from the new flags is
748 * longer than indicated by the flags of the existing link,
749 * update the existing link to stay around longer.
751 if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER) {
752 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER) {
753 link->flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER;
754 link->flags |= DL_FLAG_AUTOREMOVE_SUPPLIER;
756 } else if (!(flags & DL_FLAG_AUTOREMOVE_CONSUMER)) {
757 link->flags &= ~(DL_FLAG_AUTOREMOVE_CONSUMER |
758 DL_FLAG_AUTOREMOVE_SUPPLIER);
760 if (!(link->flags & DL_FLAG_MANAGED)) {
761 kref_get(&link->kref);
762 link->flags |= DL_FLAG_MANAGED;
763 device_link_init_status(link, consumer, supplier);
765 if (link->flags & DL_FLAG_SYNC_STATE_ONLY &&
766 !(flags & DL_FLAG_SYNC_STATE_ONLY)) {
767 link->flags &= ~DL_FLAG_SYNC_STATE_ONLY;
774 link = kzalloc(sizeof(*link), GFP_KERNEL);
778 refcount_set(&link->rpm_active, 1);
780 get_device(supplier);
781 link->supplier = supplier;
782 INIT_LIST_HEAD(&link->s_node);
783 get_device(consumer);
784 link->consumer = consumer;
785 INIT_LIST_HEAD(&link->c_node);
787 kref_init(&link->kref);
789 link->link_dev.class = &devlink_class;
790 device_set_pm_not_required(&link->link_dev);
791 dev_set_name(&link->link_dev, "%s:%s--%s:%s",
792 dev_bus_name(supplier), dev_name(supplier),
793 dev_bus_name(consumer), dev_name(consumer));
794 if (device_register(&link->link_dev)) {
795 put_device(&link->link_dev);
800 if (flags & DL_FLAG_PM_RUNTIME) {
801 if (flags & DL_FLAG_RPM_ACTIVE)
802 refcount_inc(&link->rpm_active);
804 pm_runtime_new_link(consumer);
807 /* Determine the initial link state. */
808 if (flags & DL_FLAG_STATELESS)
809 link->status = DL_STATE_NONE;
811 device_link_init_status(link, consumer, supplier);
814 * Some callers expect the link creation during consumer driver probe to
815 * resume the supplier even without DL_FLAG_RPM_ACTIVE.
817 if (link->status == DL_STATE_CONSUMER_PROBE &&
818 flags & DL_FLAG_PM_RUNTIME)
819 pm_runtime_resume(supplier);
821 list_add_tail_rcu(&link->s_node, &supplier->links.consumers);
822 list_add_tail_rcu(&link->c_node, &consumer->links.suppliers);
824 if (flags & DL_FLAG_SYNC_STATE_ONLY) {
826 "Linked as a sync state only consumer to %s\n",
833 * Move the consumer and all of the devices depending on it to the end
834 * of dpm_list and the devices_kset list.
836 * It is necessary to hold dpm_list locked throughout all that or else
837 * we may end up suspending with a wrong ordering of it.
839 device_reorder_to_tail(consumer, NULL);
841 dev_dbg(consumer, "Linked as a consumer to %s\n", dev_name(supplier));
845 device_links_write_unlock();
847 if ((flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) && !link)
848 pm_runtime_put(supplier);
852 EXPORT_SYMBOL_GPL(device_link_add);
854 static void __device_link_del(struct kref *kref)
856 struct device_link *link = container_of(kref, struct device_link, kref);
858 dev_dbg(link->consumer, "Dropping the link to %s\n",
859 dev_name(link->supplier));
861 pm_runtime_drop_link(link);
863 device_link_remove_from_lists(link);
864 device_unregister(&link->link_dev);
867 static void device_link_put_kref(struct device_link *link)
869 if (link->flags & DL_FLAG_STATELESS)
870 kref_put(&link->kref, __device_link_del);
871 else if (!device_is_registered(link->consumer))
872 __device_link_del(&link->kref);
874 WARN(1, "Unable to drop a managed device link reference\n");
878 * device_link_del - Delete a stateless link between two devices.
879 * @link: Device link to delete.
881 * The caller must ensure proper synchronization of this function with runtime
882 * PM. If the link was added multiple times, it needs to be deleted as often.
883 * Care is required for hotplugged devices: Their links are purged on removal
884 * and calling device_link_del() is then no longer allowed.
886 void device_link_del(struct device_link *link)
888 device_links_write_lock();
889 device_link_put_kref(link);
890 device_links_write_unlock();
892 EXPORT_SYMBOL_GPL(device_link_del);
895 * device_link_remove - Delete a stateless link between two devices.
896 * @consumer: Consumer end of the link.
897 * @supplier: Supplier end of the link.
899 * The caller must ensure proper synchronization of this function with runtime
902 void device_link_remove(void *consumer, struct device *supplier)
904 struct device_link *link;
906 if (WARN_ON(consumer == supplier))
909 device_links_write_lock();
911 list_for_each_entry(link, &supplier->links.consumers, s_node) {
912 if (link->consumer == consumer) {
913 device_link_put_kref(link);
918 device_links_write_unlock();
920 EXPORT_SYMBOL_GPL(device_link_remove);
922 static void device_links_missing_supplier(struct device *dev)
924 struct device_link *link;
926 list_for_each_entry(link, &dev->links.suppliers, c_node) {
927 if (link->status != DL_STATE_CONSUMER_PROBE)
930 if (link->supplier->links.status == DL_DEV_DRIVER_BOUND) {
931 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
933 WARN_ON(!(link->flags & DL_FLAG_SYNC_STATE_ONLY));
934 WRITE_ONCE(link->status, DL_STATE_DORMANT);
939 static bool dev_is_best_effort(struct device *dev)
941 return (fw_devlink_best_effort && dev->can_match) ||
942 (dev->fwnode && (dev->fwnode->flags & FWNODE_FLAG_BEST_EFFORT));
946 * device_links_check_suppliers - Check presence of supplier drivers.
947 * @dev: Consumer device.
949 * Check links from this device to any suppliers. Walk the list of the device's
950 * links to suppliers and see if all of them are available. If not, simply
951 * return -EPROBE_DEFER.
953 * We need to guarantee that the supplier will not go away after the check has
954 * been positive here. It only can go away in __device_release_driver() and
955 * that function checks the device's links to consumers. This means we need to
956 * mark the link as "consumer probe in progress" to make the supplier removal
957 * wait for us to complete (or bad things may happen).
959 * Links without the DL_FLAG_MANAGED flag set are ignored.
961 int device_links_check_suppliers(struct device *dev)
963 struct device_link *link;
964 int ret = 0, fwnode_ret = 0;
965 struct fwnode_handle *sup_fw;
968 * Device waiting for supplier to become available is not allowed to
971 mutex_lock(&fwnode_link_lock);
972 if (dev->fwnode && !list_empty(&dev->fwnode->suppliers) &&
973 !fw_devlink_is_permissive()) {
974 sup_fw = list_first_entry(&dev->fwnode->suppliers,
977 if (!dev_is_best_effort(dev)) {
978 fwnode_ret = -EPROBE_DEFER;
979 dev_err_probe(dev, -EPROBE_DEFER,
980 "wait for supplier %pfwP\n", sup_fw);
982 fwnode_ret = -EAGAIN;
985 mutex_unlock(&fwnode_link_lock);
986 if (fwnode_ret == -EPROBE_DEFER)
989 device_links_write_lock();
991 list_for_each_entry(link, &dev->links.suppliers, c_node) {
992 if (!(link->flags & DL_FLAG_MANAGED))
995 if (link->status != DL_STATE_AVAILABLE &&
996 !(link->flags & DL_FLAG_SYNC_STATE_ONLY)) {
998 if (dev_is_best_effort(dev) &&
999 link->flags & DL_FLAG_INFERRED &&
1000 !link->supplier->can_match) {
1005 device_links_missing_supplier(dev);
1006 dev_err_probe(dev, -EPROBE_DEFER,
1007 "supplier %s not ready\n",
1008 dev_name(link->supplier));
1009 ret = -EPROBE_DEFER;
1012 WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE);
1014 dev->links.status = DL_DEV_PROBING;
1016 device_links_write_unlock();
1018 return ret ? ret : fwnode_ret;
1022 * __device_links_queue_sync_state - Queue a device for sync_state() callback
1023 * @dev: Device to call sync_state() on
1024 * @list: List head to queue the @dev on
1026 * Queues a device for a sync_state() callback when the device links write lock
1027 * isn't held. This allows the sync_state() execution flow to use device links
1028 * APIs. The caller must ensure this function is called with
1029 * device_links_write_lock() held.
1031 * This function does a get_device() to make sure the device is not freed while
1034 * So the caller must also ensure that device_links_flush_sync_list() is called
1035 * as soon as the caller releases device_links_write_lock(). This is necessary
1036 * to make sure the sync_state() is called in a timely fashion and the
1037 * put_device() is called on this device.
1039 static void __device_links_queue_sync_state(struct device *dev,
1040 struct list_head *list)
1042 struct device_link *link;
1044 if (!dev_has_sync_state(dev))
1046 if (dev->state_synced)
1049 list_for_each_entry(link, &dev->links.consumers, s_node) {
1050 if (!(link->flags & DL_FLAG_MANAGED))
1052 if (link->status != DL_STATE_ACTIVE)
1057 * Set the flag here to avoid adding the same device to a list more
1058 * than once. This can happen if new consumers get added to the device
1059 * and probed before the list is flushed.
1061 dev->state_synced = true;
1063 if (WARN_ON(!list_empty(&dev->links.defer_sync)))
1067 list_add_tail(&dev->links.defer_sync, list);
1071 * device_links_flush_sync_list - Call sync_state() on a list of devices
1072 * @list: List of devices to call sync_state() on
1073 * @dont_lock_dev: Device for which lock is already held by the caller
1075 * Calls sync_state() on all the devices that have been queued for it. This
1076 * function is used in conjunction with __device_links_queue_sync_state(). The
1077 * @dont_lock_dev parameter is useful when this function is called from a
1078 * context where a device lock is already held.
1080 static void device_links_flush_sync_list(struct list_head *list,
1081 struct device *dont_lock_dev)
1083 struct device *dev, *tmp;
1085 list_for_each_entry_safe(dev, tmp, list, links.defer_sync) {
1086 list_del_init(&dev->links.defer_sync);
1088 if (dev != dont_lock_dev)
1091 if (dev->bus->sync_state)
1092 dev->bus->sync_state(dev);
1093 else if (dev->driver && dev->driver->sync_state)
1094 dev->driver->sync_state(dev);
1096 if (dev != dont_lock_dev)
1103 void device_links_supplier_sync_state_pause(void)
1105 device_links_write_lock();
1106 defer_sync_state_count++;
1107 device_links_write_unlock();
1110 void device_links_supplier_sync_state_resume(void)
1112 struct device *dev, *tmp;
1113 LIST_HEAD(sync_list);
1115 device_links_write_lock();
1116 if (!defer_sync_state_count) {
1117 WARN(true, "Unmatched sync_state pause/resume!");
1120 defer_sync_state_count--;
1121 if (defer_sync_state_count)
1124 list_for_each_entry_safe(dev, tmp, &deferred_sync, links.defer_sync) {
1126 * Delete from deferred_sync list before queuing it to
1127 * sync_list because defer_sync is used for both lists.
1129 list_del_init(&dev->links.defer_sync);
1130 __device_links_queue_sync_state(dev, &sync_list);
1133 device_links_write_unlock();
1135 device_links_flush_sync_list(&sync_list, NULL);
1138 static int sync_state_resume_initcall(void)
1140 device_links_supplier_sync_state_resume();
1143 late_initcall(sync_state_resume_initcall);
1145 static void __device_links_supplier_defer_sync(struct device *sup)
1147 if (list_empty(&sup->links.defer_sync) && dev_has_sync_state(sup))
1148 list_add_tail(&sup->links.defer_sync, &deferred_sync);
1151 static void device_link_drop_managed(struct device_link *link)
1153 link->flags &= ~DL_FLAG_MANAGED;
1154 WRITE_ONCE(link->status, DL_STATE_NONE);
1155 kref_put(&link->kref, __device_link_del);
1158 static ssize_t waiting_for_supplier_show(struct device *dev,
1159 struct device_attribute *attr,
1165 val = !list_empty(&dev->fwnode->suppliers);
1167 return sysfs_emit(buf, "%u\n", val);
1169 static DEVICE_ATTR_RO(waiting_for_supplier);
1172 * device_links_force_bind - Prepares device to be force bound
1173 * @dev: Consumer device.
1175 * device_bind_driver() force binds a device to a driver without calling any
1176 * driver probe functions. So the consumer really isn't going to wait for any
1177 * supplier before it's bound to the driver. We still want the device link
1178 * states to be sensible when this happens.
1180 * In preparation for device_bind_driver(), this function goes through each
1181 * supplier device links and checks if the supplier is bound. If it is, then
1182 * the device link status is set to CONSUMER_PROBE. Otherwise, the device link
1183 * is dropped. Links without the DL_FLAG_MANAGED flag set are ignored.
1185 void device_links_force_bind(struct device *dev)
1187 struct device_link *link, *ln;
1189 device_links_write_lock();
1191 list_for_each_entry_safe(link, ln, &dev->links.suppliers, c_node) {
1192 if (!(link->flags & DL_FLAG_MANAGED))
1195 if (link->status != DL_STATE_AVAILABLE) {
1196 device_link_drop_managed(link);
1199 WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE);
1201 dev->links.status = DL_DEV_PROBING;
1203 device_links_write_unlock();
1207 * device_links_driver_bound - Update device links after probing its driver.
1208 * @dev: Device to update the links for.
1210 * The probe has been successful, so update links from this device to any
1211 * consumers by changing their status to "available".
1213 * Also change the status of @dev's links to suppliers to "active".
1215 * Links without the DL_FLAG_MANAGED flag set are ignored.
1217 void device_links_driver_bound(struct device *dev)
1219 struct device_link *link, *ln;
1220 LIST_HEAD(sync_list);
1223 * If a device binds successfully, it's expected to have created all
1224 * the device links it needs to or make new device links as it needs
1225 * them. So, fw_devlink no longer needs to create device links to any
1226 * of the device's suppliers.
1228 * Also, if a child firmware node of this bound device is not added as
1229 * a device by now, assume it is never going to be added and make sure
1230 * other devices don't defer probe indefinitely by waiting for such a
1233 if (dev->fwnode && dev->fwnode->dev == dev) {
1234 struct fwnode_handle *child;
1235 fwnode_links_purge_suppliers(dev->fwnode);
1236 fwnode_for_each_available_child_node(dev->fwnode, child)
1237 fw_devlink_purge_absent_suppliers(child);
1239 device_remove_file(dev, &dev_attr_waiting_for_supplier);
1241 device_links_write_lock();
1243 list_for_each_entry(link, &dev->links.consumers, s_node) {
1244 if (!(link->flags & DL_FLAG_MANAGED))
1248 * Links created during consumer probe may be in the "consumer
1249 * probe" state to start with if the supplier is still probing
1250 * when they are created and they may become "active" if the
1251 * consumer probe returns first. Skip them here.
1253 if (link->status == DL_STATE_CONSUMER_PROBE ||
1254 link->status == DL_STATE_ACTIVE)
1257 WARN_ON(link->status != DL_STATE_DORMANT);
1258 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
1260 if (link->flags & DL_FLAG_AUTOPROBE_CONSUMER)
1261 driver_deferred_probe_add(link->consumer);
1264 if (defer_sync_state_count)
1265 __device_links_supplier_defer_sync(dev);
1267 __device_links_queue_sync_state(dev, &sync_list);
1269 list_for_each_entry_safe(link, ln, &dev->links.suppliers, c_node) {
1270 struct device *supplier;
1272 if (!(link->flags & DL_FLAG_MANAGED))
1275 supplier = link->supplier;
1276 if (link->flags & DL_FLAG_SYNC_STATE_ONLY) {
1278 * When DL_FLAG_SYNC_STATE_ONLY is set, it means no
1279 * other DL_MANAGED_LINK_FLAGS have been set. So, it's
1280 * save to drop the managed link completely.
1282 device_link_drop_managed(link);
1283 } else if (dev_is_best_effort(dev) &&
1284 link->flags & DL_FLAG_INFERRED &&
1285 link->status != DL_STATE_CONSUMER_PROBE &&
1286 !link->supplier->can_match) {
1288 * When dev_is_best_effort() is true, we ignore device
1289 * links to suppliers that don't have a driver. If the
1290 * consumer device still managed to probe, there's no
1291 * point in maintaining a device link in a weird state
1292 * (consumer probed before supplier). So delete it.
1294 device_link_drop_managed(link);
1296 WARN_ON(link->status != DL_STATE_CONSUMER_PROBE);
1297 WRITE_ONCE(link->status, DL_STATE_ACTIVE);
1301 * This needs to be done even for the deleted
1302 * DL_FLAG_SYNC_STATE_ONLY device link in case it was the last
1303 * device link that was preventing the supplier from getting a
1304 * sync_state() call.
1306 if (defer_sync_state_count)
1307 __device_links_supplier_defer_sync(supplier);
1309 __device_links_queue_sync_state(supplier, &sync_list);
1312 dev->links.status = DL_DEV_DRIVER_BOUND;
1314 device_links_write_unlock();
1316 device_links_flush_sync_list(&sync_list, dev);
1320 * __device_links_no_driver - Update links of a device without a driver.
1321 * @dev: Device without a drvier.
1323 * Delete all non-persistent links from this device to any suppliers.
1325 * Persistent links stay around, but their status is changed to "available",
1326 * unless they already are in the "supplier unbind in progress" state in which
1327 * case they need not be updated.
1329 * Links without the DL_FLAG_MANAGED flag set are ignored.
1331 static void __device_links_no_driver(struct device *dev)
1333 struct device_link *link, *ln;
1335 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
1336 if (!(link->flags & DL_FLAG_MANAGED))
1339 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER) {
1340 device_link_drop_managed(link);
1344 if (link->status != DL_STATE_CONSUMER_PROBE &&
1345 link->status != DL_STATE_ACTIVE)
1348 if (link->supplier->links.status == DL_DEV_DRIVER_BOUND) {
1349 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
1351 WARN_ON(!(link->flags & DL_FLAG_SYNC_STATE_ONLY));
1352 WRITE_ONCE(link->status, DL_STATE_DORMANT);
1356 dev->links.status = DL_DEV_NO_DRIVER;
1360 * device_links_no_driver - Update links after failing driver probe.
1361 * @dev: Device whose driver has just failed to probe.
1363 * Clean up leftover links to consumers for @dev and invoke
1364 * %__device_links_no_driver() to update links to suppliers for it as
1367 * Links without the DL_FLAG_MANAGED flag set are ignored.
1369 void device_links_no_driver(struct device *dev)
1371 struct device_link *link;
1373 device_links_write_lock();
1375 list_for_each_entry(link, &dev->links.consumers, s_node) {
1376 if (!(link->flags & DL_FLAG_MANAGED))
1380 * The probe has failed, so if the status of the link is
1381 * "consumer probe" or "active", it must have been added by
1382 * a probing consumer while this device was still probing.
1383 * Change its state to "dormant", as it represents a valid
1384 * relationship, but it is not functionally meaningful.
1386 if (link->status == DL_STATE_CONSUMER_PROBE ||
1387 link->status == DL_STATE_ACTIVE)
1388 WRITE_ONCE(link->status, DL_STATE_DORMANT);
1391 __device_links_no_driver(dev);
1393 device_links_write_unlock();
1397 * device_links_driver_cleanup - Update links after driver removal.
1398 * @dev: Device whose driver has just gone away.
1400 * Update links to consumers for @dev by changing their status to "dormant" and
1401 * invoke %__device_links_no_driver() to update links to suppliers for it as
1404 * Links without the DL_FLAG_MANAGED flag set are ignored.
1406 void device_links_driver_cleanup(struct device *dev)
1408 struct device_link *link, *ln;
1410 device_links_write_lock();
1412 list_for_each_entry_safe(link, ln, &dev->links.consumers, s_node) {
1413 if (!(link->flags & DL_FLAG_MANAGED))
1416 WARN_ON(link->flags & DL_FLAG_AUTOREMOVE_CONSUMER);
1417 WARN_ON(link->status != DL_STATE_SUPPLIER_UNBIND);
1420 * autoremove the links between this @dev and its consumer
1421 * devices that are not active, i.e. where the link state
1422 * has moved to DL_STATE_SUPPLIER_UNBIND.
1424 if (link->status == DL_STATE_SUPPLIER_UNBIND &&
1425 link->flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
1426 device_link_drop_managed(link);
1428 WRITE_ONCE(link->status, DL_STATE_DORMANT);
1431 list_del_init(&dev->links.defer_sync);
1432 __device_links_no_driver(dev);
1434 device_links_write_unlock();
1438 * device_links_busy - Check if there are any busy links to consumers.
1439 * @dev: Device to check.
1441 * Check each consumer of the device and return 'true' if its link's status
1442 * is one of "consumer probe" or "active" (meaning that the given consumer is
1443 * probing right now or its driver is present). Otherwise, change the link
1444 * state to "supplier unbind" to prevent the consumer from being probed
1445 * successfully going forward.
1447 * Return 'false' if there are no probing or active consumers.
1449 * Links without the DL_FLAG_MANAGED flag set are ignored.
1451 bool device_links_busy(struct device *dev)
1453 struct device_link *link;
1456 device_links_write_lock();
1458 list_for_each_entry(link, &dev->links.consumers, s_node) {
1459 if (!(link->flags & DL_FLAG_MANAGED))
1462 if (link->status == DL_STATE_CONSUMER_PROBE
1463 || link->status == DL_STATE_ACTIVE) {
1467 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
1470 dev->links.status = DL_DEV_UNBINDING;
1472 device_links_write_unlock();
1477 * device_links_unbind_consumers - Force unbind consumers of the given device.
1478 * @dev: Device to unbind the consumers of.
1480 * Walk the list of links to consumers for @dev and if any of them is in the
1481 * "consumer probe" state, wait for all device probes in progress to complete
1484 * If that's not the case, change the status of the link to "supplier unbind"
1485 * and check if the link was in the "active" state. If so, force the consumer
1486 * driver to unbind and start over (the consumer will not re-probe as we have
1487 * changed the state of the link already).
1489 * Links without the DL_FLAG_MANAGED flag set are ignored.
1491 void device_links_unbind_consumers(struct device *dev)
1493 struct device_link *link;
1496 device_links_write_lock();
1498 list_for_each_entry(link, &dev->links.consumers, s_node) {
1499 enum device_link_state status;
1501 if (!(link->flags & DL_FLAG_MANAGED) ||
1502 link->flags & DL_FLAG_SYNC_STATE_ONLY)
1505 status = link->status;
1506 if (status == DL_STATE_CONSUMER_PROBE) {
1507 device_links_write_unlock();
1509 wait_for_device_probe();
1512 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
1513 if (status == DL_STATE_ACTIVE) {
1514 struct device *consumer = link->consumer;
1516 get_device(consumer);
1518 device_links_write_unlock();
1520 device_release_driver_internal(consumer, NULL,
1522 put_device(consumer);
1527 device_links_write_unlock();
1531 * device_links_purge - Delete existing links to other devices.
1532 * @dev: Target device.
1534 static void device_links_purge(struct device *dev)
1536 struct device_link *link, *ln;
1538 if (dev->class == &devlink_class)
1542 * Delete all of the remaining links from this device to any other
1543 * devices (either consumers or suppliers).
1545 device_links_write_lock();
1547 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
1548 WARN_ON(link->status == DL_STATE_ACTIVE);
1549 __device_link_del(&link->kref);
1552 list_for_each_entry_safe_reverse(link, ln, &dev->links.consumers, s_node) {
1553 WARN_ON(link->status != DL_STATE_DORMANT &&
1554 link->status != DL_STATE_NONE);
1555 __device_link_del(&link->kref);
1558 device_links_write_unlock();
1561 #define FW_DEVLINK_FLAGS_PERMISSIVE (DL_FLAG_INFERRED | \
1562 DL_FLAG_SYNC_STATE_ONLY)
1563 #define FW_DEVLINK_FLAGS_ON (DL_FLAG_INFERRED | \
1564 DL_FLAG_AUTOPROBE_CONSUMER)
1565 #define FW_DEVLINK_FLAGS_RPM (FW_DEVLINK_FLAGS_ON | \
1568 static u32 fw_devlink_flags = FW_DEVLINK_FLAGS_ON;
1569 static int __init fw_devlink_setup(char *arg)
1574 if (strcmp(arg, "off") == 0) {
1575 fw_devlink_flags = 0;
1576 } else if (strcmp(arg, "permissive") == 0) {
1577 fw_devlink_flags = FW_DEVLINK_FLAGS_PERMISSIVE;
1578 } else if (strcmp(arg, "on") == 0) {
1579 fw_devlink_flags = FW_DEVLINK_FLAGS_ON;
1580 } else if (strcmp(arg, "rpm") == 0) {
1581 fw_devlink_flags = FW_DEVLINK_FLAGS_RPM;
1585 early_param("fw_devlink", fw_devlink_setup);
1587 static bool fw_devlink_strict;
1588 static int __init fw_devlink_strict_setup(char *arg)
1590 return kstrtobool(arg, &fw_devlink_strict);
1592 early_param("fw_devlink.strict", fw_devlink_strict_setup);
1594 u32 fw_devlink_get_flags(void)
1596 return fw_devlink_flags;
1599 static bool fw_devlink_is_permissive(void)
1601 return fw_devlink_flags == FW_DEVLINK_FLAGS_PERMISSIVE;
1604 bool fw_devlink_is_strict(void)
1606 return fw_devlink_strict && !fw_devlink_is_permissive();
1609 static void fw_devlink_parse_fwnode(struct fwnode_handle *fwnode)
1611 if (fwnode->flags & FWNODE_FLAG_LINKS_ADDED)
1614 fwnode_call_int_op(fwnode, add_links);
1615 fwnode->flags |= FWNODE_FLAG_LINKS_ADDED;
1618 static void fw_devlink_parse_fwtree(struct fwnode_handle *fwnode)
1620 struct fwnode_handle *child = NULL;
1622 fw_devlink_parse_fwnode(fwnode);
1624 while ((child = fwnode_get_next_available_child_node(fwnode, child)))
1625 fw_devlink_parse_fwtree(child);
1628 static void fw_devlink_relax_link(struct device_link *link)
1630 if (!(link->flags & DL_FLAG_INFERRED))
1633 if (link->flags == (DL_FLAG_MANAGED | FW_DEVLINK_FLAGS_PERMISSIVE))
1636 pm_runtime_drop_link(link);
1637 link->flags = DL_FLAG_MANAGED | FW_DEVLINK_FLAGS_PERMISSIVE;
1638 dev_dbg(link->consumer, "Relaxing link with %s\n",
1639 dev_name(link->supplier));
1642 static int fw_devlink_no_driver(struct device *dev, void *data)
1644 struct device_link *link = to_devlink(dev);
1646 if (!link->supplier->can_match)
1647 fw_devlink_relax_link(link);
1652 void fw_devlink_drivers_done(void)
1654 fw_devlink_drv_reg_done = true;
1655 device_links_write_lock();
1656 class_for_each_device(&devlink_class, NULL, NULL,
1657 fw_devlink_no_driver);
1658 device_links_write_unlock();
1662 * wait_for_init_devices_probe - Try to probe any device needed for init
1664 * Some devices might need to be probed and bound successfully before the kernel
1665 * boot sequence can finish and move on to init/userspace. For example, a
1666 * network interface might need to be bound to be able to mount a NFS rootfs.
1668 * With fw_devlink=on by default, some of these devices might be blocked from
1669 * probing because they are waiting on a optional supplier that doesn't have a
1670 * driver. While fw_devlink will eventually identify such devices and unblock
1671 * the probing automatically, it might be too late by the time it unblocks the
1672 * probing of devices. For example, the IP4 autoconfig might timeout before
1673 * fw_devlink unblocks probing of the network interface.
1675 * This function is available to temporarily try and probe all devices that have
1676 * a driver even if some of their suppliers haven't been added or don't have
1679 * The drivers can then decide which of the suppliers are optional vs mandatory
1680 * and probe the device if possible. By the time this function returns, all such
1681 * "best effort" probes are guaranteed to be completed. If a device successfully
1682 * probes in this mode, we delete all fw_devlink discovered dependencies of that
1683 * device where the supplier hasn't yet probed successfully because they have to
1684 * be optional dependencies.
1686 * Any devices that didn't successfully probe go back to being treated as if
1687 * this function was never called.
1689 * This also means that some devices that aren't needed for init and could have
1690 * waited for their optional supplier to probe (when the supplier's module is
1691 * loaded later on) would end up probing prematurely with limited functionality.
1692 * So call this function only when boot would fail without it.
1694 void __init wait_for_init_devices_probe(void)
1696 if (!fw_devlink_flags || fw_devlink_is_permissive())
1700 * Wait for all ongoing probes to finish so that the "best effort" is
1701 * only applied to devices that can't probe otherwise.
1703 wait_for_device_probe();
1705 pr_info("Trying to probe devices needed for running init ...\n");
1706 fw_devlink_best_effort = true;
1707 driver_deferred_probe_trigger();
1710 * Wait for all "best effort" probes to finish before going back to
1711 * normal enforcement.
1713 wait_for_device_probe();
1714 fw_devlink_best_effort = false;
1717 static void fw_devlink_unblock_consumers(struct device *dev)
1719 struct device_link *link;
1721 if (!fw_devlink_flags || fw_devlink_is_permissive())
1724 device_links_write_lock();
1725 list_for_each_entry(link, &dev->links.consumers, s_node)
1726 fw_devlink_relax_link(link);
1727 device_links_write_unlock();
1731 * fw_devlink_relax_cycle - Convert cyclic links to SYNC_STATE_ONLY links
1732 * @con: Device to check dependencies for.
1733 * @sup: Device to check against.
1735 * Check if @sup depends on @con or any device dependent on it (its child or
1736 * its consumer etc). When such a cyclic dependency is found, convert all
1737 * device links created solely by fw_devlink into SYNC_STATE_ONLY device links.
1738 * This is the equivalent of doing fw_devlink=permissive just between the
1739 * devices in the cycle. We need to do this because, at this point, fw_devlink
1740 * can't tell which of these dependencies is not a real dependency.
1742 * Return 1 if a cycle is found. Otherwise, return 0.
1744 static int fw_devlink_relax_cycle(struct device *con, void *sup)
1746 struct device_link *link;
1752 ret = device_for_each_child(con, sup, fw_devlink_relax_cycle);
1756 list_for_each_entry(link, &con->links.consumers, s_node) {
1757 if ((link->flags & ~DL_FLAG_INFERRED) ==
1758 (DL_FLAG_SYNC_STATE_ONLY | DL_FLAG_MANAGED))
1761 if (!fw_devlink_relax_cycle(link->consumer, sup))
1766 fw_devlink_relax_link(link);
1772 * fw_devlink_create_devlink - Create a device link from a consumer to fwnode
1773 * @con: consumer device for the device link
1774 * @sup_handle: fwnode handle of supplier
1775 * @flags: devlink flags
1777 * This function will try to create a device link between the consumer device
1778 * @con and the supplier device represented by @sup_handle.
1780 * The supplier has to be provided as a fwnode because incorrect cycles in
1781 * fwnode links can sometimes cause the supplier device to never be created.
1782 * This function detects such cases and returns an error if it cannot create a
1783 * device link from the consumer to a missing supplier.
1786 * 0 on successfully creating a device link
1787 * -EINVAL if the device link cannot be created as expected
1788 * -EAGAIN if the device link cannot be created right now, but it may be
1789 * possible to do that in the future
1791 static int fw_devlink_create_devlink(struct device *con,
1792 struct fwnode_handle *sup_handle, u32 flags)
1794 struct device *sup_dev;
1798 * In some cases, a device P might also be a supplier to its child node
1799 * C. However, this would defer the probe of C until the probe of P
1800 * completes successfully. This is perfectly fine in the device driver
1801 * model. device_add() doesn't guarantee probe completion of the device
1802 * by the time it returns.
1804 * However, there are a few drivers that assume C will finish probing
1805 * as soon as it's added and before P finishes probing. So, we provide
1806 * a flag to let fw_devlink know not to delay the probe of C until the
1807 * probe of P completes successfully.
1809 * When such a flag is set, we can't create device links where P is the
1810 * supplier of C as that would delay the probe of C.
1812 if (sup_handle->flags & FWNODE_FLAG_NEEDS_CHILD_BOUND_ON_ADD &&
1813 fwnode_is_ancestor_of(sup_handle, con->fwnode))
1816 sup_dev = get_dev_from_fwnode(sup_handle);
1819 * If it's one of those drivers that don't actually bind to
1820 * their device using driver core, then don't wait on this
1821 * supplier device indefinitely.
1823 if (sup_dev->links.status == DL_DEV_NO_DRIVER &&
1824 sup_handle->flags & FWNODE_FLAG_INITIALIZED) {
1830 * If this fails, it is due to cycles in device links. Just
1831 * give up on this link and treat it as invalid.
1833 if (!device_link_add(con, sup_dev, flags) &&
1834 !(flags & DL_FLAG_SYNC_STATE_ONLY)) {
1835 dev_info(con, "Fixing up cyclic dependency with %s\n",
1837 device_links_write_lock();
1838 fw_devlink_relax_cycle(con, sup_dev);
1839 device_links_write_unlock();
1840 device_link_add(con, sup_dev,
1841 FW_DEVLINK_FLAGS_PERMISSIVE);
1848 /* Supplier that's already initialized without a struct device. */
1849 if (sup_handle->flags & FWNODE_FLAG_INITIALIZED)
1853 * DL_FLAG_SYNC_STATE_ONLY doesn't block probing and supports
1854 * cycles. So cycle detection isn't necessary and shouldn't be
1857 if (flags & DL_FLAG_SYNC_STATE_ONLY)
1861 * If we can't find the supplier device from its fwnode, it might be
1862 * due to a cyclic dependency between fwnodes. Some of these cycles can
1863 * be broken by applying logic. Check for these types of cycles and
1864 * break them so that devices in the cycle probe properly.
1866 * If the supplier's parent is dependent on the consumer, then the
1867 * consumer and supplier have a cyclic dependency. Since fw_devlink
1868 * can't tell which of the inferred dependencies are incorrect, don't
1869 * enforce probe ordering between any of the devices in this cyclic
1870 * dependency. Do this by relaxing all the fw_devlink device links in
1871 * this cycle and by treating the fwnode link between the consumer and
1872 * the supplier as an invalid dependency.
1874 sup_dev = fwnode_get_next_parent_dev(sup_handle);
1875 if (sup_dev && device_is_dependent(con, sup_dev)) {
1876 dev_info(con, "Fixing up cyclic dependency with %pfwP (%s)\n",
1877 sup_handle, dev_name(sup_dev));
1878 device_links_write_lock();
1879 fw_devlink_relax_cycle(con, sup_dev);
1880 device_links_write_unlock();
1884 * Can't check for cycles or no cycles. So let's try
1891 put_device(sup_dev);
1896 * __fw_devlink_link_to_consumers - Create device links to consumers of a device
1897 * @dev: Device that needs to be linked to its consumers
1899 * This function looks at all the consumer fwnodes of @dev and creates device
1900 * links between the consumer device and @dev (supplier).
1902 * If the consumer device has not been added yet, then this function creates a
1903 * SYNC_STATE_ONLY link between @dev (supplier) and the closest ancestor device
1904 * of the consumer fwnode. This is necessary to make sure @dev doesn't get a
1905 * sync_state() callback before the real consumer device gets to be added and
1908 * Once device links are created from the real consumer to @dev (supplier), the
1909 * fwnode links are deleted.
1911 static void __fw_devlink_link_to_consumers(struct device *dev)
1913 struct fwnode_handle *fwnode = dev->fwnode;
1914 struct fwnode_link *link, *tmp;
1916 list_for_each_entry_safe(link, tmp, &fwnode->consumers, s_hook) {
1917 u32 dl_flags = fw_devlink_get_flags();
1918 struct device *con_dev;
1919 bool own_link = true;
1922 con_dev = get_dev_from_fwnode(link->consumer);
1924 * If consumer device is not available yet, make a "proxy"
1925 * SYNC_STATE_ONLY link from the consumer's parent device to
1926 * the supplier device. This is necessary to make sure the
1927 * supplier doesn't get a sync_state() callback before the real
1928 * consumer can create a device link to the supplier.
1930 * This proxy link step is needed to handle the case where the
1931 * consumer's parent device is added before the supplier.
1934 con_dev = fwnode_get_next_parent_dev(link->consumer);
1936 * However, if the consumer's parent device is also the
1937 * parent of the supplier, don't create a
1938 * consumer-supplier link from the parent to its child
1939 * device. Such a dependency is impossible.
1942 fwnode_is_ancestor_of(con_dev->fwnode, fwnode)) {
1943 put_device(con_dev);
1947 dl_flags = FW_DEVLINK_FLAGS_PERMISSIVE;
1954 ret = fw_devlink_create_devlink(con_dev, fwnode, dl_flags);
1955 put_device(con_dev);
1956 if (!own_link || ret == -EAGAIN)
1959 __fwnode_link_del(link);
1964 * __fw_devlink_link_to_suppliers - Create device links to suppliers of a device
1965 * @dev: The consumer device that needs to be linked to its suppliers
1966 * @fwnode: Root of the fwnode tree that is used to create device links
1968 * This function looks at all the supplier fwnodes of fwnode tree rooted at
1969 * @fwnode and creates device links between @dev (consumer) and all the
1970 * supplier devices of the entire fwnode tree at @fwnode.
1972 * The function creates normal (non-SYNC_STATE_ONLY) device links between @dev
1973 * and the real suppliers of @dev. Once these device links are created, the
1974 * fwnode links are deleted. When such device links are successfully created,
1975 * this function is called recursively on those supplier devices. This is
1976 * needed to detect and break some invalid cycles in fwnode links. See
1977 * fw_devlink_create_devlink() for more details.
1979 * In addition, it also looks at all the suppliers of the entire fwnode tree
1980 * because some of the child devices of @dev that have not been added yet
1981 * (because @dev hasn't probed) might already have their suppliers added to
1982 * driver core. So, this function creates SYNC_STATE_ONLY device links between
1983 * @dev (consumer) and these suppliers to make sure they don't execute their
1984 * sync_state() callbacks before these child devices have a chance to create
1985 * their device links. The fwnode links that correspond to the child devices
1986 * aren't delete because they are needed later to create the device links
1987 * between the real consumer and supplier devices.
1989 static void __fw_devlink_link_to_suppliers(struct device *dev,
1990 struct fwnode_handle *fwnode)
1992 bool own_link = (dev->fwnode == fwnode);
1993 struct fwnode_link *link, *tmp;
1994 struct fwnode_handle *child = NULL;
1998 dl_flags = fw_devlink_get_flags();
2000 dl_flags = FW_DEVLINK_FLAGS_PERMISSIVE;
2002 list_for_each_entry_safe(link, tmp, &fwnode->suppliers, c_hook) {
2004 struct device *sup_dev;
2005 struct fwnode_handle *sup = link->supplier;
2007 ret = fw_devlink_create_devlink(dev, sup, dl_flags);
2008 if (!own_link || ret == -EAGAIN)
2011 __fwnode_link_del(link);
2013 /* If no device link was created, nothing more to do. */
2018 * If a device link was successfully created to a supplier, we
2019 * now need to try and link the supplier to all its suppliers.
2021 * This is needed to detect and delete false dependencies in
2022 * fwnode links that haven't been converted to a device link
2023 * yet. See comments in fw_devlink_create_devlink() for more
2024 * details on the false dependency.
2026 * Without deleting these false dependencies, some devices will
2027 * never probe because they'll keep waiting for their false
2028 * dependency fwnode links to be converted to device links.
2030 sup_dev = get_dev_from_fwnode(sup);
2031 __fw_devlink_link_to_suppliers(sup_dev, sup_dev->fwnode);
2032 put_device(sup_dev);
2036 * Make "proxy" SYNC_STATE_ONLY device links to represent the needs of
2037 * all the descendants. This proxy link step is needed to handle the
2038 * case where the supplier is added before the consumer's parent device
2041 while ((child = fwnode_get_next_available_child_node(fwnode, child)))
2042 __fw_devlink_link_to_suppliers(dev, child);
2045 static void fw_devlink_link_device(struct device *dev)
2047 struct fwnode_handle *fwnode = dev->fwnode;
2049 if (!fw_devlink_flags)
2052 fw_devlink_parse_fwtree(fwnode);
2054 mutex_lock(&fwnode_link_lock);
2055 __fw_devlink_link_to_consumers(dev);
2056 __fw_devlink_link_to_suppliers(dev, fwnode);
2057 mutex_unlock(&fwnode_link_lock);
2060 /* Device links support end. */
2062 int (*platform_notify)(struct device *dev) = NULL;
2063 int (*platform_notify_remove)(struct device *dev) = NULL;
2064 static struct kobject *dev_kobj;
2065 struct kobject *sysfs_dev_char_kobj;
2066 struct kobject *sysfs_dev_block_kobj;
2068 static DEFINE_MUTEX(device_hotplug_lock);
2070 void lock_device_hotplug(void)
2072 mutex_lock(&device_hotplug_lock);
2075 void unlock_device_hotplug(void)
2077 mutex_unlock(&device_hotplug_lock);
2080 int lock_device_hotplug_sysfs(void)
2082 if (mutex_trylock(&device_hotplug_lock))
2085 /* Avoid busy looping (5 ms of sleep should do). */
2087 return restart_syscall();
2091 static inline int device_is_not_partition(struct device *dev)
2093 return !(dev->type == &part_type);
2096 static inline int device_is_not_partition(struct device *dev)
2102 static void device_platform_notify(struct device *dev)
2104 acpi_device_notify(dev);
2106 software_node_notify(dev);
2108 if (platform_notify)
2109 platform_notify(dev);
2112 static void device_platform_notify_remove(struct device *dev)
2114 acpi_device_notify_remove(dev);
2116 software_node_notify_remove(dev);
2118 if (platform_notify_remove)
2119 platform_notify_remove(dev);
2123 * dev_driver_string - Return a device's driver name, if at all possible
2124 * @dev: struct device to get the name of
2126 * Will return the device's driver's name if it is bound to a device. If
2127 * the device is not bound to a driver, it will return the name of the bus
2128 * it is attached to. If it is not attached to a bus either, an empty
2129 * string will be returned.
2131 const char *dev_driver_string(const struct device *dev)
2133 struct device_driver *drv;
2135 /* dev->driver can change to NULL underneath us because of unbinding,
2136 * so be careful about accessing it. dev->bus and dev->class should
2137 * never change once they are set, so they don't need special care.
2139 drv = READ_ONCE(dev->driver);
2140 return drv ? drv->name : dev_bus_name(dev);
2142 EXPORT_SYMBOL(dev_driver_string);
2144 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
2146 static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
2149 struct device_attribute *dev_attr = to_dev_attr(attr);
2150 struct device *dev = kobj_to_dev(kobj);
2154 ret = dev_attr->show(dev, dev_attr, buf);
2155 if (ret >= (ssize_t)PAGE_SIZE) {
2156 printk("dev_attr_show: %pS returned bad count\n",
2162 static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
2163 const char *buf, size_t count)
2165 struct device_attribute *dev_attr = to_dev_attr(attr);
2166 struct device *dev = kobj_to_dev(kobj);
2169 if (dev_attr->store)
2170 ret = dev_attr->store(dev, dev_attr, buf, count);
2174 static const struct sysfs_ops dev_sysfs_ops = {
2175 .show = dev_attr_show,
2176 .store = dev_attr_store,
2179 #define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
2181 ssize_t device_store_ulong(struct device *dev,
2182 struct device_attribute *attr,
2183 const char *buf, size_t size)
2185 struct dev_ext_attribute *ea = to_ext_attr(attr);
2189 ret = kstrtoul(buf, 0, &new);
2192 *(unsigned long *)(ea->var) = new;
2193 /* Always return full write size even if we didn't consume all */
2196 EXPORT_SYMBOL_GPL(device_store_ulong);
2198 ssize_t device_show_ulong(struct device *dev,
2199 struct device_attribute *attr,
2202 struct dev_ext_attribute *ea = to_ext_attr(attr);
2203 return sysfs_emit(buf, "%lx\n", *(unsigned long *)(ea->var));
2205 EXPORT_SYMBOL_GPL(device_show_ulong);
2207 ssize_t device_store_int(struct device *dev,
2208 struct device_attribute *attr,
2209 const char *buf, size_t size)
2211 struct dev_ext_attribute *ea = to_ext_attr(attr);
2215 ret = kstrtol(buf, 0, &new);
2219 if (new > INT_MAX || new < INT_MIN)
2221 *(int *)(ea->var) = new;
2222 /* Always return full write size even if we didn't consume all */
2225 EXPORT_SYMBOL_GPL(device_store_int);
2227 ssize_t device_show_int(struct device *dev,
2228 struct device_attribute *attr,
2231 struct dev_ext_attribute *ea = to_ext_attr(attr);
2233 return sysfs_emit(buf, "%d\n", *(int *)(ea->var));
2235 EXPORT_SYMBOL_GPL(device_show_int);
2237 ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
2238 const char *buf, size_t size)
2240 struct dev_ext_attribute *ea = to_ext_attr(attr);
2242 if (kstrtobool(buf, ea->var) < 0)
2247 EXPORT_SYMBOL_GPL(device_store_bool);
2249 ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
2252 struct dev_ext_attribute *ea = to_ext_attr(attr);
2254 return sysfs_emit(buf, "%d\n", *(bool *)(ea->var));
2256 EXPORT_SYMBOL_GPL(device_show_bool);
2259 * device_release - free device structure.
2260 * @kobj: device's kobject.
2262 * This is called once the reference count for the object
2263 * reaches 0. We forward the call to the device's release
2264 * method, which should handle actually freeing the structure.
2266 static void device_release(struct kobject *kobj)
2268 struct device *dev = kobj_to_dev(kobj);
2269 struct device_private *p = dev->p;
2272 * Some platform devices are driven without driver attached
2273 * and managed resources may have been acquired. Make sure
2274 * all resources are released.
2276 * Drivers still can add resources into device after device
2277 * is deleted but alive, so release devres here to avoid
2278 * possible memory leak.
2280 devres_release_all(dev);
2282 kfree(dev->dma_range_map);
2286 else if (dev->type && dev->type->release)
2287 dev->type->release(dev);
2288 else if (dev->class && dev->class->dev_release)
2289 dev->class->dev_release(dev);
2291 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",
2296 static const void *device_namespace(const struct kobject *kobj)
2298 const struct device *dev = kobj_to_dev(kobj);
2299 const void *ns = NULL;
2301 if (dev->class && dev->class->ns_type)
2302 ns = dev->class->namespace(dev);
2307 static void device_get_ownership(const struct kobject *kobj, kuid_t *uid, kgid_t *gid)
2309 const struct device *dev = kobj_to_dev(kobj);
2311 if (dev->class && dev->class->get_ownership)
2312 dev->class->get_ownership(dev, uid, gid);
2315 static struct kobj_type device_ktype = {
2316 .release = device_release,
2317 .sysfs_ops = &dev_sysfs_ops,
2318 .namespace = device_namespace,
2319 .get_ownership = device_get_ownership,
2323 static int dev_uevent_filter(const struct kobject *kobj)
2325 const struct kobj_type *ktype = get_ktype(kobj);
2327 if (ktype == &device_ktype) {
2328 const struct device *dev = kobj_to_dev(kobj);
2337 static const char *dev_uevent_name(const struct kobject *kobj)
2339 const struct device *dev = kobj_to_dev(kobj);
2342 return dev->bus->name;
2344 return dev->class->name;
2348 static int dev_uevent(struct kobject *kobj, struct kobj_uevent_env *env)
2350 struct device *dev = kobj_to_dev(kobj);
2353 /* add device node properties if present */
2354 if (MAJOR(dev->devt)) {
2358 kuid_t uid = GLOBAL_ROOT_UID;
2359 kgid_t gid = GLOBAL_ROOT_GID;
2361 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
2362 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
2363 name = device_get_devnode(dev, &mode, &uid, &gid, &tmp);
2365 add_uevent_var(env, "DEVNAME=%s", name);
2367 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
2368 if (!uid_eq(uid, GLOBAL_ROOT_UID))
2369 add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid));
2370 if (!gid_eq(gid, GLOBAL_ROOT_GID))
2371 add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid));
2376 if (dev->type && dev->type->name)
2377 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
2380 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
2382 /* Add common DT information about the device */
2383 of_device_uevent(dev, env);
2385 /* have the bus specific function add its stuff */
2386 if (dev->bus && dev->bus->uevent) {
2387 retval = dev->bus->uevent(dev, env);
2389 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
2390 dev_name(dev), __func__, retval);
2393 /* have the class specific function add its stuff */
2394 if (dev->class && dev->class->dev_uevent) {
2395 retval = dev->class->dev_uevent(dev, env);
2397 pr_debug("device: '%s': %s: class uevent() "
2398 "returned %d\n", dev_name(dev),
2402 /* have the device type specific function add its stuff */
2403 if (dev->type && dev->type->uevent) {
2404 retval = dev->type->uevent(dev, env);
2406 pr_debug("device: '%s': %s: dev_type uevent() "
2407 "returned %d\n", dev_name(dev),
2414 static const struct kset_uevent_ops device_uevent_ops = {
2415 .filter = dev_uevent_filter,
2416 .name = dev_uevent_name,
2417 .uevent = dev_uevent,
2420 static ssize_t uevent_show(struct device *dev, struct device_attribute *attr,
2423 struct kobject *top_kobj;
2425 struct kobj_uevent_env *env = NULL;
2430 /* search the kset, the device belongs to */
2431 top_kobj = &dev->kobj;
2432 while (!top_kobj->kset && top_kobj->parent)
2433 top_kobj = top_kobj->parent;
2434 if (!top_kobj->kset)
2437 kset = top_kobj->kset;
2438 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
2441 /* respect filter */
2442 if (kset->uevent_ops && kset->uevent_ops->filter)
2443 if (!kset->uevent_ops->filter(&dev->kobj))
2446 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
2450 /* let the kset specific function add its keys */
2451 retval = kset->uevent_ops->uevent(&dev->kobj, env);
2455 /* copy keys to file */
2456 for (i = 0; i < env->envp_idx; i++)
2457 len += sysfs_emit_at(buf, len, "%s\n", env->envp[i]);
2463 static ssize_t uevent_store(struct device *dev, struct device_attribute *attr,
2464 const char *buf, size_t count)
2468 rc = kobject_synth_uevent(&dev->kobj, buf, count);
2471 dev_err(dev, "uevent: failed to send synthetic uevent: %d\n", rc);
2477 static DEVICE_ATTR_RW(uevent);
2479 static ssize_t online_show(struct device *dev, struct device_attribute *attr,
2485 val = !dev->offline;
2487 return sysfs_emit(buf, "%u\n", val);
2490 static ssize_t online_store(struct device *dev, struct device_attribute *attr,
2491 const char *buf, size_t count)
2496 ret = kstrtobool(buf, &val);
2500 ret = lock_device_hotplug_sysfs();
2504 ret = val ? device_online(dev) : device_offline(dev);
2505 unlock_device_hotplug();
2506 return ret < 0 ? ret : count;
2508 static DEVICE_ATTR_RW(online);
2510 static ssize_t removable_show(struct device *dev, struct device_attribute *attr,
2515 switch (dev->removable) {
2516 case DEVICE_REMOVABLE:
2525 return sysfs_emit(buf, "%s\n", loc);
2527 static DEVICE_ATTR_RO(removable);
2529 int device_add_groups(struct device *dev, const struct attribute_group **groups)
2531 return sysfs_create_groups(&dev->kobj, groups);
2533 EXPORT_SYMBOL_GPL(device_add_groups);
2535 void device_remove_groups(struct device *dev,
2536 const struct attribute_group **groups)
2538 sysfs_remove_groups(&dev->kobj, groups);
2540 EXPORT_SYMBOL_GPL(device_remove_groups);
2542 union device_attr_group_devres {
2543 const struct attribute_group *group;
2544 const struct attribute_group **groups;
2547 static void devm_attr_group_remove(struct device *dev, void *res)
2549 union device_attr_group_devres *devres = res;
2550 const struct attribute_group *group = devres->group;
2552 dev_dbg(dev, "%s: removing group %p\n", __func__, group);
2553 sysfs_remove_group(&dev->kobj, group);
2556 static void devm_attr_groups_remove(struct device *dev, void *res)
2558 union device_attr_group_devres *devres = res;
2559 const struct attribute_group **groups = devres->groups;
2561 dev_dbg(dev, "%s: removing groups %p\n", __func__, groups);
2562 sysfs_remove_groups(&dev->kobj, groups);
2566 * devm_device_add_group - given a device, create a managed attribute group
2567 * @dev: The device to create the group for
2568 * @grp: The attribute group to create
2570 * This function creates a group for the first time. It will explicitly
2571 * warn and error if any of the attribute files being created already exist.
2573 * Returns 0 on success or error code on failure.
2575 int devm_device_add_group(struct device *dev, const struct attribute_group *grp)
2577 union device_attr_group_devres *devres;
2580 devres = devres_alloc(devm_attr_group_remove,
2581 sizeof(*devres), GFP_KERNEL);
2585 error = sysfs_create_group(&dev->kobj, grp);
2587 devres_free(devres);
2591 devres->group = grp;
2592 devres_add(dev, devres);
2595 EXPORT_SYMBOL_GPL(devm_device_add_group);
2598 * devm_device_add_groups - create a bunch of managed attribute groups
2599 * @dev: The device to create the group for
2600 * @groups: The attribute groups to create, NULL terminated
2602 * This function creates a bunch of managed attribute groups. If an error
2603 * occurs when creating a group, all previously created groups will be
2604 * removed, unwinding everything back to the original state when this
2605 * function was called. It will explicitly warn and error if any of the
2606 * attribute files being created already exist.
2608 * Returns 0 on success or error code from sysfs_create_group on failure.
2610 int devm_device_add_groups(struct device *dev,
2611 const struct attribute_group **groups)
2613 union device_attr_group_devres *devres;
2616 devres = devres_alloc(devm_attr_groups_remove,
2617 sizeof(*devres), GFP_KERNEL);
2621 error = sysfs_create_groups(&dev->kobj, groups);
2623 devres_free(devres);
2627 devres->groups = groups;
2628 devres_add(dev, devres);
2631 EXPORT_SYMBOL_GPL(devm_device_add_groups);
2633 static int device_add_attrs(struct device *dev)
2635 struct class *class = dev->class;
2636 const struct device_type *type = dev->type;
2640 error = device_add_groups(dev, class->dev_groups);
2646 error = device_add_groups(dev, type->groups);
2648 goto err_remove_class_groups;
2651 error = device_add_groups(dev, dev->groups);
2653 goto err_remove_type_groups;
2655 if (device_supports_offline(dev) && !dev->offline_disabled) {
2656 error = device_create_file(dev, &dev_attr_online);
2658 goto err_remove_dev_groups;
2661 if (fw_devlink_flags && !fw_devlink_is_permissive() && dev->fwnode) {
2662 error = device_create_file(dev, &dev_attr_waiting_for_supplier);
2664 goto err_remove_dev_online;
2667 if (dev_removable_is_valid(dev)) {
2668 error = device_create_file(dev, &dev_attr_removable);
2670 goto err_remove_dev_waiting_for_supplier;
2673 if (dev_add_physical_location(dev)) {
2674 error = device_add_group(dev,
2675 &dev_attr_physical_location_group);
2677 goto err_remove_dev_removable;
2682 err_remove_dev_removable:
2683 device_remove_file(dev, &dev_attr_removable);
2684 err_remove_dev_waiting_for_supplier:
2685 device_remove_file(dev, &dev_attr_waiting_for_supplier);
2686 err_remove_dev_online:
2687 device_remove_file(dev, &dev_attr_online);
2688 err_remove_dev_groups:
2689 device_remove_groups(dev, dev->groups);
2690 err_remove_type_groups:
2692 device_remove_groups(dev, type->groups);
2693 err_remove_class_groups:
2695 device_remove_groups(dev, class->dev_groups);
2700 static void device_remove_attrs(struct device *dev)
2702 struct class *class = dev->class;
2703 const struct device_type *type = dev->type;
2705 if (dev->physical_location) {
2706 device_remove_group(dev, &dev_attr_physical_location_group);
2707 kfree(dev->physical_location);
2710 device_remove_file(dev, &dev_attr_removable);
2711 device_remove_file(dev, &dev_attr_waiting_for_supplier);
2712 device_remove_file(dev, &dev_attr_online);
2713 device_remove_groups(dev, dev->groups);
2716 device_remove_groups(dev, type->groups);
2719 device_remove_groups(dev, class->dev_groups);
2722 static ssize_t dev_show(struct device *dev, struct device_attribute *attr,
2725 return print_dev_t(buf, dev->devt);
2727 static DEVICE_ATTR_RO(dev);
2730 struct kset *devices_kset;
2733 * devices_kset_move_before - Move device in the devices_kset's list.
2734 * @deva: Device to move.
2735 * @devb: Device @deva should come before.
2737 static void devices_kset_move_before(struct device *deva, struct device *devb)
2741 pr_debug("devices_kset: Moving %s before %s\n",
2742 dev_name(deva), dev_name(devb));
2743 spin_lock(&devices_kset->list_lock);
2744 list_move_tail(&deva->kobj.entry, &devb->kobj.entry);
2745 spin_unlock(&devices_kset->list_lock);
2749 * devices_kset_move_after - Move device in the devices_kset's list.
2750 * @deva: Device to move
2751 * @devb: Device @deva should come after.
2753 static void devices_kset_move_after(struct device *deva, struct device *devb)
2757 pr_debug("devices_kset: Moving %s after %s\n",
2758 dev_name(deva), dev_name(devb));
2759 spin_lock(&devices_kset->list_lock);
2760 list_move(&deva->kobj.entry, &devb->kobj.entry);
2761 spin_unlock(&devices_kset->list_lock);
2765 * devices_kset_move_last - move the device to the end of devices_kset's list.
2766 * @dev: device to move
2768 void devices_kset_move_last(struct device *dev)
2772 pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev));
2773 spin_lock(&devices_kset->list_lock);
2774 list_move_tail(&dev->kobj.entry, &devices_kset->list);
2775 spin_unlock(&devices_kset->list_lock);
2779 * device_create_file - create sysfs attribute file for device.
2781 * @attr: device attribute descriptor.
2783 int device_create_file(struct device *dev,
2784 const struct device_attribute *attr)
2789 WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
2790 "Attribute %s: write permission without 'store'\n",
2792 WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
2793 "Attribute %s: read permission without 'show'\n",
2795 error = sysfs_create_file(&dev->kobj, &attr->attr);
2800 EXPORT_SYMBOL_GPL(device_create_file);
2803 * device_remove_file - remove sysfs attribute file.
2805 * @attr: device attribute descriptor.
2807 void device_remove_file(struct device *dev,
2808 const struct device_attribute *attr)
2811 sysfs_remove_file(&dev->kobj, &attr->attr);
2813 EXPORT_SYMBOL_GPL(device_remove_file);
2816 * device_remove_file_self - remove sysfs attribute file from its own method.
2818 * @attr: device attribute descriptor.
2820 * See kernfs_remove_self() for details.
2822 bool device_remove_file_self(struct device *dev,
2823 const struct device_attribute *attr)
2826 return sysfs_remove_file_self(&dev->kobj, &attr->attr);
2830 EXPORT_SYMBOL_GPL(device_remove_file_self);
2833 * device_create_bin_file - create sysfs binary attribute file for device.
2835 * @attr: device binary attribute descriptor.
2837 int device_create_bin_file(struct device *dev,
2838 const struct bin_attribute *attr)
2840 int error = -EINVAL;
2842 error = sysfs_create_bin_file(&dev->kobj, attr);
2845 EXPORT_SYMBOL_GPL(device_create_bin_file);
2848 * device_remove_bin_file - remove sysfs binary attribute file
2850 * @attr: device binary attribute descriptor.
2852 void device_remove_bin_file(struct device *dev,
2853 const struct bin_attribute *attr)
2856 sysfs_remove_bin_file(&dev->kobj, attr);
2858 EXPORT_SYMBOL_GPL(device_remove_bin_file);
2860 static void klist_children_get(struct klist_node *n)
2862 struct device_private *p = to_device_private_parent(n);
2863 struct device *dev = p->device;
2868 static void klist_children_put(struct klist_node *n)
2870 struct device_private *p = to_device_private_parent(n);
2871 struct device *dev = p->device;
2877 * device_initialize - init device structure.
2880 * This prepares the device for use by other layers by initializing
2882 * It is the first half of device_register(), if called by
2883 * that function, though it can also be called separately, so one
2884 * may use @dev's fields. In particular, get_device()/put_device()
2885 * may be used for reference counting of @dev after calling this
2888 * All fields in @dev must be initialized by the caller to 0, except
2889 * for those explicitly set to some other value. The simplest
2890 * approach is to use kzalloc() to allocate the structure containing
2893 * NOTE: Use put_device() to give up your reference instead of freeing
2894 * @dev directly once you have called this function.
2896 void device_initialize(struct device *dev)
2898 dev->kobj.kset = devices_kset;
2899 kobject_init(&dev->kobj, &device_ktype);
2900 INIT_LIST_HEAD(&dev->dma_pools);
2901 mutex_init(&dev->mutex);
2902 lockdep_set_novalidate_class(&dev->mutex);
2903 spin_lock_init(&dev->devres_lock);
2904 INIT_LIST_HEAD(&dev->devres_head);
2905 device_pm_init(dev);
2906 set_dev_node(dev, NUMA_NO_NODE);
2907 INIT_LIST_HEAD(&dev->links.consumers);
2908 INIT_LIST_HEAD(&dev->links.suppliers);
2909 INIT_LIST_HEAD(&dev->links.defer_sync);
2910 dev->links.status = DL_DEV_NO_DRIVER;
2911 #if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE) || \
2912 defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU) || \
2913 defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL)
2914 dev->dma_coherent = dma_default_coherent;
2916 #ifdef CONFIG_SWIOTLB
2917 dev->dma_io_tlb_mem = &io_tlb_default_mem;
2920 EXPORT_SYMBOL_GPL(device_initialize);
2922 struct kobject *virtual_device_parent(struct device *dev)
2924 static struct kobject *virtual_dir = NULL;
2927 virtual_dir = kobject_create_and_add("virtual",
2928 &devices_kset->kobj);
2934 struct kobject kobj;
2935 struct class *class;
2938 #define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
2940 static void class_dir_release(struct kobject *kobj)
2942 struct class_dir *dir = to_class_dir(kobj);
2947 struct kobj_ns_type_operations *class_dir_child_ns_type(const struct kobject *kobj)
2949 const struct class_dir *dir = to_class_dir(kobj);
2950 return dir->class->ns_type;
2953 static struct kobj_type class_dir_ktype = {
2954 .release = class_dir_release,
2955 .sysfs_ops = &kobj_sysfs_ops,
2956 .child_ns_type = class_dir_child_ns_type
2959 static struct kobject *
2960 class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
2962 struct class_dir *dir;
2965 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
2967 return ERR_PTR(-ENOMEM);
2970 kobject_init(&dir->kobj, &class_dir_ktype);
2972 dir->kobj.kset = &class->p->glue_dirs;
2974 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
2976 kobject_put(&dir->kobj);
2977 return ERR_PTR(retval);
2982 static DEFINE_MUTEX(gdp_mutex);
2984 static struct kobject *get_device_parent(struct device *dev,
2985 struct device *parent)
2988 struct kobject *kobj = NULL;
2989 struct kobject *parent_kobj;
2993 /* block disks show up in /sys/block */
2994 if (sysfs_deprecated && dev->class == &block_class) {
2995 if (parent && parent->class == &block_class)
2996 return &parent->kobj;
2997 return &block_class.p->subsys.kobj;
3002 * If we have no parent, we live in "virtual".
3003 * Class-devices with a non class-device as parent, live
3004 * in a "glue" directory to prevent namespace collisions.
3007 parent_kobj = virtual_device_parent(dev);
3008 else if (parent->class && !dev->class->ns_type)
3009 return &parent->kobj;
3011 parent_kobj = &parent->kobj;
3013 mutex_lock(&gdp_mutex);
3015 /* find our class-directory at the parent and reference it */
3016 spin_lock(&dev->class->p->glue_dirs.list_lock);
3017 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
3018 if (k->parent == parent_kobj) {
3019 kobj = kobject_get(k);
3022 spin_unlock(&dev->class->p->glue_dirs.list_lock);
3024 mutex_unlock(&gdp_mutex);
3028 /* or create a new class-directory at the parent device */
3029 k = class_dir_create_and_add(dev->class, parent_kobj);
3030 /* do not emit an uevent for this simple "glue" directory */
3031 mutex_unlock(&gdp_mutex);
3035 /* subsystems can specify a default root directory for their devices */
3036 if (!parent && dev->bus && dev->bus->dev_root)
3037 return &dev->bus->dev_root->kobj;
3040 return &parent->kobj;
3044 static inline bool live_in_glue_dir(struct kobject *kobj,
3047 if (!kobj || !dev->class ||
3048 kobj->kset != &dev->class->p->glue_dirs)
3053 static inline struct kobject *get_glue_dir(struct device *dev)
3055 return dev->kobj.parent;
3059 * kobject_has_children - Returns whether a kobject has children.
3060 * @kobj: the object to test
3062 * This will return whether a kobject has other kobjects as children.
3064 * It does NOT account for the presence of attribute files, only sub
3065 * directories. It also assumes there is no concurrent addition or
3066 * removal of such children, and thus relies on external locking.
3068 static inline bool kobject_has_children(struct kobject *kobj)
3070 WARN_ON_ONCE(kref_read(&kobj->kref) == 0);
3072 return kobj->sd && kobj->sd->dir.subdirs;
3076 * make sure cleaning up dir as the last step, we need to make
3077 * sure .release handler of kobject is run with holding the
3080 static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
3084 /* see if we live in a "glue" directory */
3085 if (!live_in_glue_dir(glue_dir, dev))
3088 mutex_lock(&gdp_mutex);
3090 * There is a race condition between removing glue directory
3091 * and adding a new device under the glue directory.
3096 * get_device_parent()
3097 * class_dir_create_and_add()
3098 * kobject_add_internal()
3099 * create_dir() // create glue_dir
3102 * get_device_parent()
3103 * kobject_get() // get glue_dir
3106 * cleanup_glue_dir()
3107 * kobject_del(glue_dir)
3110 * kobject_add_internal()
3111 * create_dir() // in glue_dir
3112 * sysfs_create_dir_ns()
3113 * kernfs_create_dir_ns(sd)
3115 * sysfs_remove_dir() // glue_dir->sd=NULL
3116 * sysfs_put() // free glue_dir->sd
3119 * kernfs_new_node(sd)
3120 * kernfs_get(glue_dir)
3124 * Before CPU1 remove last child device under glue dir, if CPU2 add
3125 * a new device under glue dir, the glue_dir kobject reference count
3126 * will be increase to 2 in kobject_get(k). And CPU2 has been called
3127 * kernfs_create_dir_ns(). Meanwhile, CPU1 call sysfs_remove_dir()
3128 * and sysfs_put(). This result in glue_dir->sd is freed.
3130 * Then the CPU2 will see a stale "empty" but still potentially used
3131 * glue dir around in kernfs_new_node().
3133 * In order to avoid this happening, we also should make sure that
3134 * kernfs_node for glue_dir is released in CPU1 only when refcount
3135 * for glue_dir kobj is 1.
3137 ref = kref_read(&glue_dir->kref);
3138 if (!kobject_has_children(glue_dir) && !--ref)
3139 kobject_del(glue_dir);
3140 kobject_put(glue_dir);
3141 mutex_unlock(&gdp_mutex);
3144 static int device_add_class_symlinks(struct device *dev)
3146 struct device_node *of_node = dev_of_node(dev);
3150 error = sysfs_create_link(&dev->kobj, of_node_kobj(of_node), "of_node");
3152 dev_warn(dev, "Error %d creating of_node link\n",error);
3153 /* An error here doesn't warrant bringing down the device */
3159 error = sysfs_create_link(&dev->kobj,
3160 &dev->class->p->subsys.kobj,
3165 if (dev->parent && device_is_not_partition(dev)) {
3166 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
3173 /* /sys/block has directories and does not need symlinks */
3174 if (sysfs_deprecated && dev->class == &block_class)
3178 /* link in the class directory pointing to the device */
3179 error = sysfs_create_link(&dev->class->p->subsys.kobj,
3180 &dev->kobj, dev_name(dev));
3187 sysfs_remove_link(&dev->kobj, "device");
3190 sysfs_remove_link(&dev->kobj, "subsystem");
3192 sysfs_remove_link(&dev->kobj, "of_node");
3196 static void device_remove_class_symlinks(struct device *dev)
3198 if (dev_of_node(dev))
3199 sysfs_remove_link(&dev->kobj, "of_node");
3204 if (dev->parent && device_is_not_partition(dev))
3205 sysfs_remove_link(&dev->kobj, "device");
3206 sysfs_remove_link(&dev->kobj, "subsystem");
3208 if (sysfs_deprecated && dev->class == &block_class)
3211 sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
3215 * dev_set_name - set a device name
3217 * @fmt: format string for the device's name
3219 int dev_set_name(struct device *dev, const char *fmt, ...)
3224 va_start(vargs, fmt);
3225 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
3229 EXPORT_SYMBOL_GPL(dev_set_name);
3232 * device_to_dev_kobj - select a /sys/dev/ directory for the device
3235 * By default we select char/ for new entries. Setting class->dev_obj
3236 * to NULL prevents an entry from being created. class->dev_kobj must
3237 * be set (or cleared) before any devices are registered to the class
3238 * otherwise device_create_sys_dev_entry() and
3239 * device_remove_sys_dev_entry() will disagree about the presence of
3242 static struct kobject *device_to_dev_kobj(struct device *dev)
3244 struct kobject *kobj;
3247 kobj = dev->class->dev_kobj;
3249 kobj = sysfs_dev_char_kobj;
3254 static int device_create_sys_dev_entry(struct device *dev)
3256 struct kobject *kobj = device_to_dev_kobj(dev);
3261 format_dev_t(devt_str, dev->devt);
3262 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
3268 static void device_remove_sys_dev_entry(struct device *dev)
3270 struct kobject *kobj = device_to_dev_kobj(dev);
3274 format_dev_t(devt_str, dev->devt);
3275 sysfs_remove_link(kobj, devt_str);
3279 static int device_private_init(struct device *dev)
3281 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
3284 dev->p->device = dev;
3285 klist_init(&dev->p->klist_children, klist_children_get,
3286 klist_children_put);
3287 INIT_LIST_HEAD(&dev->p->deferred_probe);
3292 * device_add - add device to device hierarchy.
3295 * This is part 2 of device_register(), though may be called
3296 * separately _iff_ device_initialize() has been called separately.
3298 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
3299 * to the global and sibling lists for the device, then
3300 * adds it to the other relevant subsystems of the driver model.
3302 * Do not call this routine or device_register() more than once for
3303 * any device structure. The driver model core is not designed to work
3304 * with devices that get unregistered and then spring back to life.
3305 * (Among other things, it's very hard to guarantee that all references
3306 * to the previous incarnation of @dev have been dropped.) Allocate
3307 * and register a fresh new struct device instead.
3309 * NOTE: _Never_ directly free @dev after calling this function, even
3310 * if it returned an error! Always use put_device() to give up your
3311 * reference instead.
3313 * Rule of thumb is: if device_add() succeeds, you should call
3314 * device_del() when you want to get rid of it. If device_add() has
3315 * *not* succeeded, use *only* put_device() to drop the reference
3318 int device_add(struct device *dev)
3320 struct device *parent;
3321 struct kobject *kobj;
3322 struct class_interface *class_intf;
3323 int error = -EINVAL;
3324 struct kobject *glue_dir = NULL;
3326 dev = get_device(dev);
3331 error = device_private_init(dev);
3337 * for statically allocated devices, which should all be converted
3338 * some day, we need to initialize the name. We prevent reading back
3339 * the name, and force the use of dev_name()
3341 if (dev->init_name) {
3342 dev_set_name(dev, "%s", dev->init_name);
3343 dev->init_name = NULL;
3346 /* subsystems can specify simple device enumeration */
3347 if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
3348 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
3350 if (!dev_name(dev)) {
3355 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
3357 parent = get_device(dev->parent);
3358 kobj = get_device_parent(dev, parent);
3360 error = PTR_ERR(kobj);
3364 dev->kobj.parent = kobj;
3366 /* use parent numa_node */
3367 if (parent && (dev_to_node(dev) == NUMA_NO_NODE))
3368 set_dev_node(dev, dev_to_node(parent));
3370 /* first, register with generic layer. */
3371 /* we require the name to be set before, and pass NULL */
3372 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
3374 glue_dir = get_glue_dir(dev);
3378 /* notify platform of device entry */
3379 device_platform_notify(dev);
3381 error = device_create_file(dev, &dev_attr_uevent);
3385 error = device_add_class_symlinks(dev);
3388 error = device_add_attrs(dev);
3391 error = bus_add_device(dev);
3394 error = dpm_sysfs_add(dev);
3399 if (MAJOR(dev->devt)) {
3400 error = device_create_file(dev, &dev_attr_dev);
3404 error = device_create_sys_dev_entry(dev);
3408 devtmpfs_create_node(dev);
3411 /* Notify clients of device addition. This call must come
3412 * after dpm_sysfs_add() and before kobject_uevent().
3415 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
3416 BUS_NOTIFY_ADD_DEVICE, dev);
3418 kobject_uevent(&dev->kobj, KOBJ_ADD);
3421 * Check if any of the other devices (consumers) have been waiting for
3422 * this device (supplier) to be added so that they can create a device
3425 * This needs to happen after device_pm_add() because device_link_add()
3426 * requires the supplier be registered before it's called.
3428 * But this also needs to happen before bus_probe_device() to make sure
3429 * waiting consumers can link to it before the driver is bound to the
3430 * device and the driver sync_state callback is called for this device.
3432 if (dev->fwnode && !dev->fwnode->dev) {
3433 dev->fwnode->dev = dev;
3434 fw_devlink_link_device(dev);
3437 bus_probe_device(dev);
3440 * If all driver registration is done and a newly added device doesn't
3441 * match with any driver, don't block its consumers from probing in
3442 * case the consumer device is able to operate without this supplier.
3444 if (dev->fwnode && fw_devlink_drv_reg_done && !dev->can_match)
3445 fw_devlink_unblock_consumers(dev);
3448 klist_add_tail(&dev->p->knode_parent,
3449 &parent->p->klist_children);
3452 mutex_lock(&dev->class->p->mutex);
3453 /* tie the class to the device */
3454 klist_add_tail(&dev->p->knode_class,
3455 &dev->class->p->klist_devices);
3457 /* notify any interfaces that the device is here */
3458 list_for_each_entry(class_intf,
3459 &dev->class->p->interfaces, node)
3460 if (class_intf->add_dev)
3461 class_intf->add_dev(dev, class_intf);
3462 mutex_unlock(&dev->class->p->mutex);
3468 if (MAJOR(dev->devt))
3469 device_remove_file(dev, &dev_attr_dev);
3471 device_pm_remove(dev);
3472 dpm_sysfs_remove(dev);
3474 bus_remove_device(dev);
3476 device_remove_attrs(dev);
3478 device_remove_class_symlinks(dev);
3480 device_remove_file(dev, &dev_attr_uevent);
3482 device_platform_notify_remove(dev);
3483 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
3484 glue_dir = get_glue_dir(dev);
3485 kobject_del(&dev->kobj);
3487 cleanup_glue_dir(dev, glue_dir);
3495 EXPORT_SYMBOL_GPL(device_add);
3498 * device_register - register a device with the system.
3499 * @dev: pointer to the device structure
3501 * This happens in two clean steps - initialize the device
3502 * and add it to the system. The two steps can be called
3503 * separately, but this is the easiest and most common.
3504 * I.e. you should only call the two helpers separately if
3505 * have a clearly defined need to use and refcount the device
3506 * before it is added to the hierarchy.
3508 * For more information, see the kerneldoc for device_initialize()
3511 * NOTE: _Never_ directly free @dev after calling this function, even
3512 * if it returned an error! Always use put_device() to give up the
3513 * reference initialized in this function instead.
3515 int device_register(struct device *dev)
3517 device_initialize(dev);
3518 return device_add(dev);
3520 EXPORT_SYMBOL_GPL(device_register);
3523 * get_device - increment reference count for device.
3526 * This simply forwards the call to kobject_get(), though
3527 * we do take care to provide for the case that we get a NULL
3528 * pointer passed in.
3530 struct device *get_device(struct device *dev)
3532 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
3534 EXPORT_SYMBOL_GPL(get_device);
3537 * put_device - decrement reference count.
3538 * @dev: device in question.
3540 void put_device(struct device *dev)
3542 /* might_sleep(); */
3544 kobject_put(&dev->kobj);
3546 EXPORT_SYMBOL_GPL(put_device);
3548 bool kill_device(struct device *dev)
3551 * Require the device lock and set the "dead" flag to guarantee that
3552 * the update behavior is consistent with the other bitfields near
3553 * it and that we cannot have an asynchronous probe routine trying
3554 * to run while we are tearing out the bus/class/sysfs from
3555 * underneath the device.
3557 device_lock_assert(dev);
3561 dev->p->dead = true;
3564 EXPORT_SYMBOL_GPL(kill_device);
3567 * device_del - delete device from system.
3570 * This is the first part of the device unregistration
3571 * sequence. This removes the device from the lists we control
3572 * from here, has it removed from the other driver model
3573 * subsystems it was added to in device_add(), and removes it
3574 * from the kobject hierarchy.
3576 * NOTE: this should be called manually _iff_ device_add() was
3577 * also called manually.
3579 void device_del(struct device *dev)
3581 struct device *parent = dev->parent;
3582 struct kobject *glue_dir = NULL;
3583 struct class_interface *class_intf;
3584 unsigned int noio_flag;
3590 if (dev->fwnode && dev->fwnode->dev == dev)
3591 dev->fwnode->dev = NULL;
3593 /* Notify clients of device removal. This call must come
3594 * before dpm_sysfs_remove().
3596 noio_flag = memalloc_noio_save();
3598 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
3599 BUS_NOTIFY_DEL_DEVICE, dev);
3601 dpm_sysfs_remove(dev);
3603 klist_del(&dev->p->knode_parent);
3604 if (MAJOR(dev->devt)) {
3605 devtmpfs_delete_node(dev);
3606 device_remove_sys_dev_entry(dev);
3607 device_remove_file(dev, &dev_attr_dev);
3610 device_remove_class_symlinks(dev);
3612 mutex_lock(&dev->class->p->mutex);
3613 /* notify any interfaces that the device is now gone */
3614 list_for_each_entry(class_intf,
3615 &dev->class->p->interfaces, node)
3616 if (class_intf->remove_dev)
3617 class_intf->remove_dev(dev, class_intf);
3618 /* remove the device from the class list */
3619 klist_del(&dev->p->knode_class);
3620 mutex_unlock(&dev->class->p->mutex);
3622 device_remove_file(dev, &dev_attr_uevent);
3623 device_remove_attrs(dev);
3624 bus_remove_device(dev);
3625 device_pm_remove(dev);
3626 driver_deferred_probe_del(dev);
3627 device_platform_notify_remove(dev);
3628 device_links_purge(dev);
3631 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
3632 BUS_NOTIFY_REMOVED_DEVICE, dev);
3633 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
3634 glue_dir = get_glue_dir(dev);
3635 kobject_del(&dev->kobj);
3636 cleanup_glue_dir(dev, glue_dir);
3637 memalloc_noio_restore(noio_flag);
3640 EXPORT_SYMBOL_GPL(device_del);
3643 * device_unregister - unregister device from system.
3644 * @dev: device going away.
3646 * We do this in two parts, like we do device_register(). First,
3647 * we remove it from all the subsystems with device_del(), then
3648 * we decrement the reference count via put_device(). If that
3649 * is the final reference count, the device will be cleaned up
3650 * via device_release() above. Otherwise, the structure will
3651 * stick around until the final reference to the device is dropped.
3653 void device_unregister(struct device *dev)
3655 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
3659 EXPORT_SYMBOL_GPL(device_unregister);
3661 static struct device *prev_device(struct klist_iter *i)
3663 struct klist_node *n = klist_prev(i);
3664 struct device *dev = NULL;
3665 struct device_private *p;
3668 p = to_device_private_parent(n);
3674 static struct device *next_device(struct klist_iter *i)
3676 struct klist_node *n = klist_next(i);
3677 struct device *dev = NULL;
3678 struct device_private *p;
3681 p = to_device_private_parent(n);
3688 * device_get_devnode - path of device node file
3690 * @mode: returned file access mode
3691 * @uid: returned file owner
3692 * @gid: returned file group
3693 * @tmp: possibly allocated string
3695 * Return the relative path of a possible device node.
3696 * Non-default names may need to allocate a memory to compose
3697 * a name. This memory is returned in tmp and needs to be
3698 * freed by the caller.
3700 const char *device_get_devnode(struct device *dev,
3701 umode_t *mode, kuid_t *uid, kgid_t *gid,
3708 /* the device type may provide a specific name */
3709 if (dev->type && dev->type->devnode)
3710 *tmp = dev->type->devnode(dev, mode, uid, gid);
3714 /* the class may provide a specific name */
3715 if (dev->class && dev->class->devnode)
3716 *tmp = dev->class->devnode(dev, mode);
3720 /* return name without allocation, tmp == NULL */
3721 if (strchr(dev_name(dev), '!') == NULL)
3722 return dev_name(dev);
3724 /* replace '!' in the name with '/' */
3725 s = kstrdup(dev_name(dev), GFP_KERNEL);
3728 strreplace(s, '!', '/');
3733 * device_for_each_child - device child iterator.
3734 * @parent: parent struct device.
3735 * @fn: function to be called for each device.
3736 * @data: data for the callback.
3738 * Iterate over @parent's child devices, and call @fn for each,
3741 * We check the return of @fn each time. If it returns anything
3742 * other than 0, we break out and return that value.
3744 int device_for_each_child(struct device *parent, void *data,
3745 int (*fn)(struct device *dev, void *data))
3747 struct klist_iter i;
3748 struct device *child;
3754 klist_iter_init(&parent->p->klist_children, &i);
3755 while (!error && (child = next_device(&i)))
3756 error = fn(child, data);
3757 klist_iter_exit(&i);
3760 EXPORT_SYMBOL_GPL(device_for_each_child);
3763 * device_for_each_child_reverse - device child iterator in reversed order.
3764 * @parent: parent struct device.
3765 * @fn: function to be called for each device.
3766 * @data: data for the callback.
3768 * Iterate over @parent's child devices, and call @fn for each,
3771 * We check the return of @fn each time. If it returns anything
3772 * other than 0, we break out and return that value.
3774 int device_for_each_child_reverse(struct device *parent, void *data,
3775 int (*fn)(struct device *dev, void *data))
3777 struct klist_iter i;
3778 struct device *child;
3784 klist_iter_init(&parent->p->klist_children, &i);
3785 while ((child = prev_device(&i)) && !error)
3786 error = fn(child, data);
3787 klist_iter_exit(&i);
3790 EXPORT_SYMBOL_GPL(device_for_each_child_reverse);
3793 * device_find_child - device iterator for locating a particular device.
3794 * @parent: parent struct device
3795 * @match: Callback function to check device
3796 * @data: Data to pass to match function
3798 * This is similar to the device_for_each_child() function above, but it
3799 * returns a reference to a device that is 'found' for later use, as
3800 * determined by the @match callback.
3802 * The callback should return 0 if the device doesn't match and non-zero
3803 * if it does. If the callback returns non-zero and a reference to the
3804 * current device can be obtained, this function will return to the caller
3805 * and not iterate over any more devices.
3807 * NOTE: you will need to drop the reference with put_device() after use.
3809 struct device *device_find_child(struct device *parent, void *data,
3810 int (*match)(struct device *dev, void *data))
3812 struct klist_iter i;
3813 struct device *child;
3818 klist_iter_init(&parent->p->klist_children, &i);
3819 while ((child = next_device(&i)))
3820 if (match(child, data) && get_device(child))
3822 klist_iter_exit(&i);
3825 EXPORT_SYMBOL_GPL(device_find_child);
3828 * device_find_child_by_name - device iterator for locating a child device.
3829 * @parent: parent struct device
3830 * @name: name of the child device
3832 * This is similar to the device_find_child() function above, but it
3833 * returns a reference to a device that has the name @name.
3835 * NOTE: you will need to drop the reference with put_device() after use.
3837 struct device *device_find_child_by_name(struct device *parent,
3840 struct klist_iter i;
3841 struct device *child;
3846 klist_iter_init(&parent->p->klist_children, &i);
3847 while ((child = next_device(&i)))
3848 if (sysfs_streq(dev_name(child), name) && get_device(child))
3850 klist_iter_exit(&i);
3853 EXPORT_SYMBOL_GPL(device_find_child_by_name);
3855 static int match_any(struct device *dev, void *unused)
3861 * device_find_any_child - device iterator for locating a child device, if any.
3862 * @parent: parent struct device
3864 * This is similar to the device_find_child() function above, but it
3865 * returns a reference to a child device, if any.
3867 * NOTE: you will need to drop the reference with put_device() after use.
3869 struct device *device_find_any_child(struct device *parent)
3871 return device_find_child(parent, NULL, match_any);
3873 EXPORT_SYMBOL_GPL(device_find_any_child);
3875 int __init devices_init(void)
3877 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
3880 dev_kobj = kobject_create_and_add("dev", NULL);
3883 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
3884 if (!sysfs_dev_block_kobj)
3885 goto block_kobj_err;
3886 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
3887 if (!sysfs_dev_char_kobj)
3893 kobject_put(sysfs_dev_block_kobj);
3895 kobject_put(dev_kobj);
3897 kset_unregister(devices_kset);
3901 static int device_check_offline(struct device *dev, void *not_used)
3905 ret = device_for_each_child(dev, NULL, device_check_offline);
3909 return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
3913 * device_offline - Prepare the device for hot-removal.
3914 * @dev: Device to be put offline.
3916 * Execute the device bus type's .offline() callback, if present, to prepare
3917 * the device for a subsequent hot-removal. If that succeeds, the device must
3918 * not be used until either it is removed or its bus type's .online() callback
3921 * Call under device_hotplug_lock.
3923 int device_offline(struct device *dev)
3927 if (dev->offline_disabled)
3930 ret = device_for_each_child(dev, NULL, device_check_offline);
3935 if (device_supports_offline(dev)) {
3939 ret = dev->bus->offline(dev);
3941 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
3942 dev->offline = true;
3952 * device_online - Put the device back online after successful device_offline().
3953 * @dev: Device to be put back online.
3955 * If device_offline() has been successfully executed for @dev, but the device
3956 * has not been removed subsequently, execute its bus type's .online() callback
3957 * to indicate that the device can be used again.
3959 * Call under device_hotplug_lock.
3961 int device_online(struct device *dev)
3966 if (device_supports_offline(dev)) {
3968 ret = dev->bus->online(dev);
3970 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
3971 dev->offline = false;
3982 struct root_device {
3984 struct module *owner;
3987 static inline struct root_device *to_root_device(struct device *d)
3989 return container_of(d, struct root_device, dev);
3992 static void root_device_release(struct device *dev)
3994 kfree(to_root_device(dev));
3998 * __root_device_register - allocate and register a root device
3999 * @name: root device name
4000 * @owner: owner module of the root device, usually THIS_MODULE
4002 * This function allocates a root device and registers it
4003 * using device_register(). In order to free the returned
4004 * device, use root_device_unregister().
4006 * Root devices are dummy devices which allow other devices
4007 * to be grouped under /sys/devices. Use this function to
4008 * allocate a root device and then use it as the parent of
4009 * any device which should appear under /sys/devices/{name}
4011 * The /sys/devices/{name} directory will also contain a
4012 * 'module' symlink which points to the @owner directory
4015 * Returns &struct device pointer on success, or ERR_PTR() on error.
4017 * Note: You probably want to use root_device_register().
4019 struct device *__root_device_register(const char *name, struct module *owner)
4021 struct root_device *root;
4024 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
4026 return ERR_PTR(err);
4028 err = dev_set_name(&root->dev, "%s", name);
4031 return ERR_PTR(err);
4034 root->dev.release = root_device_release;
4036 err = device_register(&root->dev);
4038 put_device(&root->dev);
4039 return ERR_PTR(err);
4042 #ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
4044 struct module_kobject *mk = &owner->mkobj;
4046 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
4048 device_unregister(&root->dev);
4049 return ERR_PTR(err);
4051 root->owner = owner;
4057 EXPORT_SYMBOL_GPL(__root_device_register);
4060 * root_device_unregister - unregister and free a root device
4061 * @dev: device going away
4063 * This function unregisters and cleans up a device that was created by
4064 * root_device_register().
4066 void root_device_unregister(struct device *dev)
4068 struct root_device *root = to_root_device(dev);
4071 sysfs_remove_link(&root->dev.kobj, "module");
4073 device_unregister(dev);
4075 EXPORT_SYMBOL_GPL(root_device_unregister);
4078 static void device_create_release(struct device *dev)
4080 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
4084 static __printf(6, 0) struct device *
4085 device_create_groups_vargs(struct class *class, struct device *parent,
4086 dev_t devt, void *drvdata,
4087 const struct attribute_group **groups,
4088 const char *fmt, va_list args)
4090 struct device *dev = NULL;
4091 int retval = -ENODEV;
4093 if (IS_ERR_OR_NULL(class))
4096 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
4102 device_initialize(dev);
4105 dev->parent = parent;
4106 dev->groups = groups;
4107 dev->release = device_create_release;
4108 dev_set_drvdata(dev, drvdata);
4110 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
4114 retval = device_add(dev);
4122 return ERR_PTR(retval);
4126 * device_create - creates a device and registers it with sysfs
4127 * @class: pointer to the struct class that this device should be registered to
4128 * @parent: pointer to the parent struct device of this new device, if any
4129 * @devt: the dev_t for the char device to be added
4130 * @drvdata: the data to be added to the device for callbacks
4131 * @fmt: string for the device's name
4133 * This function can be used by char device classes. A struct device
4134 * will be created in sysfs, registered to the specified class.
4136 * A "dev" file will be created, showing the dev_t for the device, if
4137 * the dev_t is not 0,0.
4138 * If a pointer to a parent struct device is passed in, the newly created
4139 * struct device will be a child of that device in sysfs.
4140 * The pointer to the struct device will be returned from the call.
4141 * Any further sysfs files that might be required can be created using this
4144 * Returns &struct device pointer on success, or ERR_PTR() on error.
4146 * Note: the struct class passed to this function must have previously
4147 * been created with a call to class_create().
4149 struct device *device_create(struct class *class, struct device *parent,
4150 dev_t devt, void *drvdata, const char *fmt, ...)
4155 va_start(vargs, fmt);
4156 dev = device_create_groups_vargs(class, parent, devt, drvdata, NULL,
4161 EXPORT_SYMBOL_GPL(device_create);
4164 * device_create_with_groups - creates a device and registers it with sysfs
4165 * @class: pointer to the struct class that this device should be registered to
4166 * @parent: pointer to the parent struct device of this new device, if any
4167 * @devt: the dev_t for the char device to be added
4168 * @drvdata: the data to be added to the device for callbacks
4169 * @groups: NULL-terminated list of attribute groups to be created
4170 * @fmt: string for the device's name
4172 * This function can be used by char device classes. A struct device
4173 * will be created in sysfs, registered to the specified class.
4174 * Additional attributes specified in the groups parameter will also
4175 * be created automatically.
4177 * A "dev" file will be created, showing the dev_t for the device, if
4178 * the dev_t is not 0,0.
4179 * If a pointer to a parent struct device is passed in, the newly created
4180 * struct device will be a child of that device in sysfs.
4181 * The pointer to the struct device will be returned from the call.
4182 * Any further sysfs files that might be required can be created using this
4185 * Returns &struct device pointer on success, or ERR_PTR() on error.
4187 * Note: the struct class passed to this function must have previously
4188 * been created with a call to class_create().
4190 struct device *device_create_with_groups(struct class *class,
4191 struct device *parent, dev_t devt,
4193 const struct attribute_group **groups,
4194 const char *fmt, ...)
4199 va_start(vargs, fmt);
4200 dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
4205 EXPORT_SYMBOL_GPL(device_create_with_groups);
4208 * device_destroy - removes a device that was created with device_create()
4209 * @class: pointer to the struct class that this device was registered with
4210 * @devt: the dev_t of the device that was previously registered
4212 * This call unregisters and cleans up a device that was created with a
4213 * call to device_create().
4215 void device_destroy(struct class *class, dev_t devt)
4219 dev = class_find_device_by_devt(class, devt);
4222 device_unregister(dev);
4225 EXPORT_SYMBOL_GPL(device_destroy);
4228 * device_rename - renames a device
4229 * @dev: the pointer to the struct device to be renamed
4230 * @new_name: the new name of the device
4232 * It is the responsibility of the caller to provide mutual
4233 * exclusion between two different calls of device_rename
4234 * on the same device to ensure that new_name is valid and
4235 * won't conflict with other devices.
4237 * Note: Don't call this function. Currently, the networking layer calls this
4238 * function, but that will change. The following text from Kay Sievers offers
4241 * Renaming devices is racy at many levels, symlinks and other stuff are not
4242 * replaced atomically, and you get a "move" uevent, but it's not easy to
4243 * connect the event to the old and new device. Device nodes are not renamed at
4244 * all, there isn't even support for that in the kernel now.
4246 * In the meantime, during renaming, your target name might be taken by another
4247 * driver, creating conflicts. Or the old name is taken directly after you
4248 * renamed it -- then you get events for the same DEVPATH, before you even see
4249 * the "move" event. It's just a mess, and nothing new should ever rely on
4250 * kernel device renaming. Besides that, it's not even implemented now for
4251 * other things than (driver-core wise very simple) network devices.
4253 * We are currently about to change network renaming in udev to completely
4254 * disallow renaming of devices in the same namespace as the kernel uses,
4255 * because we can't solve the problems properly, that arise with swapping names
4256 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
4257 * be allowed to some other name than eth[0-9]*, for the aforementioned
4260 * Make up a "real" name in the driver before you register anything, or add
4261 * some other attributes for userspace to find the device, or use udev to add
4262 * symlinks -- but never rename kernel devices later, it's a complete mess. We
4263 * don't even want to get into that and try to implement the missing pieces in
4264 * the core. We really have other pieces to fix in the driver core mess. :)
4266 int device_rename(struct device *dev, const char *new_name)
4268 struct kobject *kobj = &dev->kobj;
4269 char *old_device_name = NULL;
4272 dev = get_device(dev);
4276 dev_dbg(dev, "renaming to %s\n", new_name);
4278 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
4279 if (!old_device_name) {
4285 error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj,
4286 kobj, old_device_name,
4287 new_name, kobject_namespace(kobj));
4292 error = kobject_rename(kobj, new_name);
4299 kfree(old_device_name);
4303 EXPORT_SYMBOL_GPL(device_rename);
4305 static int device_move_class_links(struct device *dev,
4306 struct device *old_parent,
4307 struct device *new_parent)
4312 sysfs_remove_link(&dev->kobj, "device");
4314 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
4320 * device_move - moves a device to a new parent
4321 * @dev: the pointer to the struct device to be moved
4322 * @new_parent: the new parent of the device (can be NULL)
4323 * @dpm_order: how to reorder the dpm_list
4325 int device_move(struct device *dev, struct device *new_parent,
4326 enum dpm_order dpm_order)
4329 struct device *old_parent;
4330 struct kobject *new_parent_kobj;
4332 dev = get_device(dev);
4337 new_parent = get_device(new_parent);
4338 new_parent_kobj = get_device_parent(dev, new_parent);
4339 if (IS_ERR(new_parent_kobj)) {
4340 error = PTR_ERR(new_parent_kobj);
4341 put_device(new_parent);
4345 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
4346 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
4347 error = kobject_move(&dev->kobj, new_parent_kobj);
4349 cleanup_glue_dir(dev, new_parent_kobj);
4350 put_device(new_parent);
4353 old_parent = dev->parent;
4354 dev->parent = new_parent;
4356 klist_remove(&dev->p->knode_parent);
4358 klist_add_tail(&dev->p->knode_parent,
4359 &new_parent->p->klist_children);
4360 set_dev_node(dev, dev_to_node(new_parent));
4364 error = device_move_class_links(dev, old_parent, new_parent);
4366 /* We ignore errors on cleanup since we're hosed anyway... */
4367 device_move_class_links(dev, new_parent, old_parent);
4368 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
4370 klist_remove(&dev->p->knode_parent);
4371 dev->parent = old_parent;
4373 klist_add_tail(&dev->p->knode_parent,
4374 &old_parent->p->klist_children);
4375 set_dev_node(dev, dev_to_node(old_parent));
4378 cleanup_glue_dir(dev, new_parent_kobj);
4379 put_device(new_parent);
4383 switch (dpm_order) {
4384 case DPM_ORDER_NONE:
4386 case DPM_ORDER_DEV_AFTER_PARENT:
4387 device_pm_move_after(dev, new_parent);
4388 devices_kset_move_after(dev, new_parent);
4390 case DPM_ORDER_PARENT_BEFORE_DEV:
4391 device_pm_move_before(new_parent, dev);
4392 devices_kset_move_before(new_parent, dev);
4394 case DPM_ORDER_DEV_LAST:
4395 device_pm_move_last(dev);
4396 devices_kset_move_last(dev);
4400 put_device(old_parent);
4406 EXPORT_SYMBOL_GPL(device_move);
4408 static int device_attrs_change_owner(struct device *dev, kuid_t kuid,
4411 struct kobject *kobj = &dev->kobj;
4412 struct class *class = dev->class;
4413 const struct device_type *type = dev->type;
4418 * Change the device groups of the device class for @dev to
4421 error = sysfs_groups_change_owner(kobj, class->dev_groups, kuid,
4429 * Change the device groups of the device type for @dev to
4432 error = sysfs_groups_change_owner(kobj, type->groups, kuid,
4438 /* Change the device groups of @dev to @kuid/@kgid. */
4439 error = sysfs_groups_change_owner(kobj, dev->groups, kuid, kgid);
4443 if (device_supports_offline(dev) && !dev->offline_disabled) {
4444 /* Change online device attributes of @dev to @kuid/@kgid. */
4445 error = sysfs_file_change_owner(kobj, dev_attr_online.attr.name,
4455 * device_change_owner - change the owner of an existing device.
4457 * @kuid: new owner's kuid
4458 * @kgid: new owner's kgid
4460 * This changes the owner of @dev and its corresponding sysfs entries to
4461 * @kuid/@kgid. This function closely mirrors how @dev was added via driver
4464 * Returns 0 on success or error code on failure.
4466 int device_change_owner(struct device *dev, kuid_t kuid, kgid_t kgid)
4469 struct kobject *kobj = &dev->kobj;
4471 dev = get_device(dev);
4476 * Change the kobject and the default attributes and groups of the
4477 * ktype associated with it to @kuid/@kgid.
4479 error = sysfs_change_owner(kobj, kuid, kgid);
4484 * Change the uevent file for @dev to the new owner. The uevent file
4485 * was created in a separate step when @dev got added and we mirror
4488 error = sysfs_file_change_owner(kobj, dev_attr_uevent.attr.name, kuid,
4494 * Change the device groups, the device groups associated with the
4495 * device class, and the groups associated with the device type of @dev
4498 error = device_attrs_change_owner(dev, kuid, kgid);
4502 error = dpm_sysfs_change_owner(dev, kuid, kgid);
4507 if (sysfs_deprecated && dev->class == &block_class)
4512 * Change the owner of the symlink located in the class directory of
4513 * the device class associated with @dev which points to the actual
4514 * directory entry for @dev to @kuid/@kgid. This ensures that the
4515 * symlink shows the same permissions as its target.
4517 error = sysfs_link_change_owner(&dev->class->p->subsys.kobj, &dev->kobj,
4518 dev_name(dev), kuid, kgid);
4526 EXPORT_SYMBOL_GPL(device_change_owner);
4529 * device_shutdown - call ->shutdown() on each device to shutdown.
4531 void device_shutdown(void)
4533 struct device *dev, *parent;
4535 wait_for_device_probe();
4536 device_block_probing();
4540 spin_lock(&devices_kset->list_lock);
4542 * Walk the devices list backward, shutting down each in turn.
4543 * Beware that device unplug events may also start pulling
4544 * devices offline, even as the system is shutting down.
4546 while (!list_empty(&devices_kset->list)) {
4547 dev = list_entry(devices_kset->list.prev, struct device,
4551 * hold reference count of device's parent to
4552 * prevent it from being freed because parent's
4553 * lock is to be held
4555 parent = get_device(dev->parent);
4558 * Make sure the device is off the kset list, in the
4559 * event that dev->*->shutdown() doesn't remove it.
4561 list_del_init(&dev->kobj.entry);
4562 spin_unlock(&devices_kset->list_lock);
4564 /* hold lock to avoid race with probe/release */
4566 device_lock(parent);
4569 /* Don't allow any more runtime suspends */
4570 pm_runtime_get_noresume(dev);
4571 pm_runtime_barrier(dev);
4573 if (dev->class && dev->class->shutdown_pre) {
4575 dev_info(dev, "shutdown_pre\n");
4576 dev->class->shutdown_pre(dev);
4578 if (dev->bus && dev->bus->shutdown) {
4580 dev_info(dev, "shutdown\n");
4581 dev->bus->shutdown(dev);
4582 } else if (dev->driver && dev->driver->shutdown) {
4584 dev_info(dev, "shutdown\n");
4585 dev->driver->shutdown(dev);
4590 device_unlock(parent);
4595 spin_lock(&devices_kset->list_lock);
4597 spin_unlock(&devices_kset->list_lock);
4601 * Device logging functions
4604 #ifdef CONFIG_PRINTK
4606 set_dev_info(const struct device *dev, struct dev_printk_info *dev_info)
4610 memset(dev_info, 0, sizeof(*dev_info));
4613 subsys = dev->class->name;
4615 subsys = dev->bus->name;
4619 strscpy(dev_info->subsystem, subsys, sizeof(dev_info->subsystem));
4622 * Add device identifier DEVICE=:
4626 * +sound:card0 subsystem:devname
4628 if (MAJOR(dev->devt)) {
4631 if (strcmp(subsys, "block") == 0)
4636 snprintf(dev_info->device, sizeof(dev_info->device),
4637 "%c%u:%u", c, MAJOR(dev->devt), MINOR(dev->devt));
4638 } else if (strcmp(subsys, "net") == 0) {
4639 struct net_device *net = to_net_dev(dev);
4641 snprintf(dev_info->device, sizeof(dev_info->device),
4642 "n%u", net->ifindex);
4644 snprintf(dev_info->device, sizeof(dev_info->device),
4645 "+%s:%s", subsys, dev_name(dev));
4649 int dev_vprintk_emit(int level, const struct device *dev,
4650 const char *fmt, va_list args)
4652 struct dev_printk_info dev_info;
4654 set_dev_info(dev, &dev_info);
4656 return vprintk_emit(0, level, &dev_info, fmt, args);
4658 EXPORT_SYMBOL(dev_vprintk_emit);
4660 int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
4665 va_start(args, fmt);
4667 r = dev_vprintk_emit(level, dev, fmt, args);
4673 EXPORT_SYMBOL(dev_printk_emit);
4675 static void __dev_printk(const char *level, const struct device *dev,
4676 struct va_format *vaf)
4679 dev_printk_emit(level[1] - '0', dev, "%s %s: %pV",
4680 dev_driver_string(dev), dev_name(dev), vaf);
4682 printk("%s(NULL device *): %pV", level, vaf);
4685 void _dev_printk(const char *level, const struct device *dev,
4686 const char *fmt, ...)
4688 struct va_format vaf;
4691 va_start(args, fmt);
4696 __dev_printk(level, dev, &vaf);
4700 EXPORT_SYMBOL(_dev_printk);
4702 #define define_dev_printk_level(func, kern_level) \
4703 void func(const struct device *dev, const char *fmt, ...) \
4705 struct va_format vaf; \
4708 va_start(args, fmt); \
4713 __dev_printk(kern_level, dev, &vaf); \
4717 EXPORT_SYMBOL(func);
4719 define_dev_printk_level(_dev_emerg, KERN_EMERG);
4720 define_dev_printk_level(_dev_alert, KERN_ALERT);
4721 define_dev_printk_level(_dev_crit, KERN_CRIT);
4722 define_dev_printk_level(_dev_err, KERN_ERR);
4723 define_dev_printk_level(_dev_warn, KERN_WARNING);
4724 define_dev_printk_level(_dev_notice, KERN_NOTICE);
4725 define_dev_printk_level(_dev_info, KERN_INFO);
4730 * dev_err_probe - probe error check and log helper
4731 * @dev: the pointer to the struct device
4732 * @err: error value to test
4733 * @fmt: printf-style format string
4734 * @...: arguments as specified in the format string
4736 * This helper implements common pattern present in probe functions for error
4737 * checking: print debug or error message depending if the error value is
4738 * -EPROBE_DEFER and propagate error upwards.
4739 * In case of -EPROBE_DEFER it sets also defer probe reason, which can be
4740 * checked later by reading devices_deferred debugfs attribute.
4741 * It replaces code sequence::
4743 * if (err != -EPROBE_DEFER)
4744 * dev_err(dev, ...);
4746 * dev_dbg(dev, ...);
4751 * return dev_err_probe(dev, err, ...);
4753 * Note that it is deemed acceptable to use this function for error
4754 * prints during probe even if the @err is known to never be -EPROBE_DEFER.
4755 * The benefit compared to a normal dev_err() is the standardized format
4756 * of the error code and the fact that the error code is returned.
4761 int dev_err_probe(const struct device *dev, int err, const char *fmt, ...)
4763 struct va_format vaf;
4766 va_start(args, fmt);
4770 if (err != -EPROBE_DEFER) {
4771 dev_err(dev, "error %pe: %pV", ERR_PTR(err), &vaf);
4773 device_set_deferred_probe_reason(dev, &vaf);
4774 dev_dbg(dev, "error %pe: %pV", ERR_PTR(err), &vaf);
4781 EXPORT_SYMBOL_GPL(dev_err_probe);
4783 static inline bool fwnode_is_primary(struct fwnode_handle *fwnode)
4785 return fwnode && !IS_ERR(fwnode->secondary);
4789 * set_primary_fwnode - Change the primary firmware node of a given device.
4790 * @dev: Device to handle.
4791 * @fwnode: New primary firmware node of the device.
4793 * Set the device's firmware node pointer to @fwnode, but if a secondary
4794 * firmware node of the device is present, preserve it.
4796 * Valid fwnode cases are:
4797 * - primary --> secondary --> -ENODEV
4798 * - primary --> NULL
4799 * - secondary --> -ENODEV
4802 void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
4804 struct device *parent = dev->parent;
4805 struct fwnode_handle *fn = dev->fwnode;
4808 if (fwnode_is_primary(fn))
4812 WARN_ON(fwnode->secondary);
4813 fwnode->secondary = fn;
4815 dev->fwnode = fwnode;
4817 if (fwnode_is_primary(fn)) {
4818 dev->fwnode = fn->secondary;
4819 /* Set fn->secondary = NULL, so fn remains the primary fwnode */
4820 if (!(parent && fn == parent->fwnode))
4821 fn->secondary = NULL;
4827 EXPORT_SYMBOL_GPL(set_primary_fwnode);
4830 * set_secondary_fwnode - Change the secondary firmware node of a given device.
4831 * @dev: Device to handle.
4832 * @fwnode: New secondary firmware node of the device.
4834 * If a primary firmware node of the device is present, set its secondary
4835 * pointer to @fwnode. Otherwise, set the device's firmware node pointer to
4838 void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
4841 fwnode->secondary = ERR_PTR(-ENODEV);
4843 if (fwnode_is_primary(dev->fwnode))
4844 dev->fwnode->secondary = fwnode;
4846 dev->fwnode = fwnode;
4848 EXPORT_SYMBOL_GPL(set_secondary_fwnode);
4851 * device_set_of_node_from_dev - reuse device-tree node of another device
4852 * @dev: device whose device-tree node is being set
4853 * @dev2: device whose device-tree node is being reused
4855 * Takes another reference to the new device-tree node after first dropping
4856 * any reference held to the old node.
4858 void device_set_of_node_from_dev(struct device *dev, const struct device *dev2)
4860 of_node_put(dev->of_node);
4861 dev->of_node = of_node_get(dev2->of_node);
4862 dev->of_node_reused = true;
4864 EXPORT_SYMBOL_GPL(device_set_of_node_from_dev);
4866 void device_set_node(struct device *dev, struct fwnode_handle *fwnode)
4868 dev->fwnode = fwnode;
4869 dev->of_node = to_of_node(fwnode);
4871 EXPORT_SYMBOL_GPL(device_set_node);
4873 int device_match_name(struct device *dev, const void *name)
4875 return sysfs_streq(dev_name(dev), name);
4877 EXPORT_SYMBOL_GPL(device_match_name);
4879 int device_match_of_node(struct device *dev, const void *np)
4881 return dev->of_node == np;
4883 EXPORT_SYMBOL_GPL(device_match_of_node);
4885 int device_match_fwnode(struct device *dev, const void *fwnode)
4887 return dev_fwnode(dev) == fwnode;
4889 EXPORT_SYMBOL_GPL(device_match_fwnode);
4891 int device_match_devt(struct device *dev, const void *pdevt)
4893 return dev->devt == *(dev_t *)pdevt;
4895 EXPORT_SYMBOL_GPL(device_match_devt);
4897 int device_match_acpi_dev(struct device *dev, const void *adev)
4899 return ACPI_COMPANION(dev) == adev;
4901 EXPORT_SYMBOL(device_match_acpi_dev);
4903 int device_match_acpi_handle(struct device *dev, const void *handle)
4905 return ACPI_HANDLE(dev) == handle;
4907 EXPORT_SYMBOL(device_match_acpi_handle);
4909 int device_match_any(struct device *dev, const void *unused)
4913 EXPORT_SYMBOL_GPL(device_match_any);