1 // SPDX-License-Identifier: GPL-2.0
3 * drivers/base/core.c - core driver model code (device registration, etc)
5 * Copyright (c) 2002-3 Patrick Mochel
6 * Copyright (c) 2002-3 Open Source Development Labs
7 * Copyright (c) 2006 Greg Kroah-Hartman <gregkh@suse.de>
8 * Copyright (c) 2006 Novell, Inc.
11 #include <linux/acpi.h>
12 #include <linux/cpufreq.h>
13 #include <linux/device.h>
14 #include <linux/err.h>
15 #include <linux/fwnode.h>
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/string.h>
20 #include <linux/kdev_t.h>
21 #include <linux/notifier.h>
23 #include <linux/of_device.h>
24 #include <linux/genhd.h>
25 #include <linux/mutex.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/netdevice.h>
28 #include <linux/sched/signal.h>
29 #include <linux/sched/mm.h>
30 #include <linux/sysfs.h>
31 #include <linux/dma-map-ops.h> /* for dma_default_coherent */
34 #include "power/power.h"
36 #ifdef CONFIG_SYSFS_DEPRECATED
37 #ifdef CONFIG_SYSFS_DEPRECATED_V2
38 long sysfs_deprecated = 1;
40 long sysfs_deprecated = 0;
42 static int __init sysfs_deprecated_setup(char *arg)
44 return kstrtol(arg, 10, &sysfs_deprecated);
46 early_param("sysfs.deprecated", sysfs_deprecated_setup);
49 /* Device links support. */
50 static LIST_HEAD(deferred_sync);
51 static unsigned int defer_sync_state_count = 1;
52 static DEFINE_MUTEX(fwnode_link_lock);
53 static bool fw_devlink_is_permissive(void);
54 static bool fw_devlink_drv_reg_done;
57 * fwnode_link_add - Create a link between two fwnode_handles.
58 * @con: Consumer end of the link.
59 * @sup: Supplier end of the link.
61 * Create a fwnode link between fwnode handles @con and @sup. The fwnode link
62 * represents the detail that the firmware lists @sup fwnode as supplying a
65 * The driver core will use the fwnode link to create a device link between the
66 * two device objects corresponding to @con and @sup when they are created. The
67 * driver core will automatically delete the fwnode link between @con and @sup
70 * Attempts to create duplicate links between the same pair of fwnode handles
71 * are ignored and there is no reference counting.
73 int fwnode_link_add(struct fwnode_handle *con, struct fwnode_handle *sup)
75 struct fwnode_link *link;
78 mutex_lock(&fwnode_link_lock);
80 list_for_each_entry(link, &sup->consumers, s_hook)
81 if (link->consumer == con)
84 link = kzalloc(sizeof(*link), GFP_KERNEL);
91 INIT_LIST_HEAD(&link->s_hook);
93 INIT_LIST_HEAD(&link->c_hook);
95 list_add(&link->s_hook, &sup->consumers);
96 list_add(&link->c_hook, &con->suppliers);
98 mutex_unlock(&fwnode_link_lock);
104 * fwnode_links_purge_suppliers - Delete all supplier links of fwnode_handle.
105 * @fwnode: fwnode whose supplier links need to be deleted
107 * Deletes all supplier links connecting directly to @fwnode.
109 static void fwnode_links_purge_suppliers(struct fwnode_handle *fwnode)
111 struct fwnode_link *link, *tmp;
113 mutex_lock(&fwnode_link_lock);
114 list_for_each_entry_safe(link, tmp, &fwnode->suppliers, c_hook) {
115 list_del(&link->s_hook);
116 list_del(&link->c_hook);
119 mutex_unlock(&fwnode_link_lock);
123 * fwnode_links_purge_consumers - Delete all consumer links of fwnode_handle.
124 * @fwnode: fwnode whose consumer links need to be deleted
126 * Deletes all consumer links connecting directly to @fwnode.
128 static void fwnode_links_purge_consumers(struct fwnode_handle *fwnode)
130 struct fwnode_link *link, *tmp;
132 mutex_lock(&fwnode_link_lock);
133 list_for_each_entry_safe(link, tmp, &fwnode->consumers, s_hook) {
134 list_del(&link->s_hook);
135 list_del(&link->c_hook);
138 mutex_unlock(&fwnode_link_lock);
142 * fwnode_links_purge - Delete all links connected to a fwnode_handle.
143 * @fwnode: fwnode whose links needs to be deleted
145 * Deletes all links connecting directly to a fwnode.
147 void fwnode_links_purge(struct fwnode_handle *fwnode)
149 fwnode_links_purge_suppliers(fwnode);
150 fwnode_links_purge_consumers(fwnode);
153 void fw_devlink_purge_absent_suppliers(struct fwnode_handle *fwnode)
155 struct fwnode_handle *child;
157 /* Don't purge consumer links of an added child */
161 fwnode->flags |= FWNODE_FLAG_NOT_DEVICE;
162 fwnode_links_purge_consumers(fwnode);
164 fwnode_for_each_available_child_node(fwnode, child)
165 fw_devlink_purge_absent_suppliers(child);
167 EXPORT_SYMBOL_GPL(fw_devlink_purge_absent_suppliers);
170 static DEFINE_MUTEX(device_links_lock);
171 DEFINE_STATIC_SRCU(device_links_srcu);
173 static inline void device_links_write_lock(void)
175 mutex_lock(&device_links_lock);
178 static inline void device_links_write_unlock(void)
180 mutex_unlock(&device_links_lock);
183 int device_links_read_lock(void) __acquires(&device_links_srcu)
185 return srcu_read_lock(&device_links_srcu);
188 void device_links_read_unlock(int idx) __releases(&device_links_srcu)
190 srcu_read_unlock(&device_links_srcu, idx);
193 int device_links_read_lock_held(void)
195 return srcu_read_lock_held(&device_links_srcu);
198 static void device_link_synchronize_removal(void)
200 synchronize_srcu(&device_links_srcu);
203 static void device_link_remove_from_lists(struct device_link *link)
205 list_del_rcu(&link->s_node);
206 list_del_rcu(&link->c_node);
208 #else /* !CONFIG_SRCU */
209 static DECLARE_RWSEM(device_links_lock);
211 static inline void device_links_write_lock(void)
213 down_write(&device_links_lock);
216 static inline void device_links_write_unlock(void)
218 up_write(&device_links_lock);
221 int device_links_read_lock(void)
223 down_read(&device_links_lock);
227 void device_links_read_unlock(int not_used)
229 up_read(&device_links_lock);
232 #ifdef CONFIG_DEBUG_LOCK_ALLOC
233 int device_links_read_lock_held(void)
235 return lockdep_is_held(&device_links_lock);
239 static inline void device_link_synchronize_removal(void)
243 static void device_link_remove_from_lists(struct device_link *link)
245 list_del(&link->s_node);
246 list_del(&link->c_node);
248 #endif /* !CONFIG_SRCU */
250 static bool device_is_ancestor(struct device *dev, struct device *target)
252 while (target->parent) {
253 target = target->parent;
261 * device_is_dependent - Check if one device depends on another one
262 * @dev: Device to check dependencies for.
263 * @target: Device to check against.
265 * Check if @target depends on @dev or any device dependent on it (its child or
266 * its consumer etc). Return 1 if that is the case or 0 otherwise.
268 int device_is_dependent(struct device *dev, void *target)
270 struct device_link *link;
274 * The "ancestors" check is needed to catch the case when the target
275 * device has not been completely initialized yet and it is still
276 * missing from the list of children of its parent device.
278 if (dev == target || device_is_ancestor(dev, target))
281 ret = device_for_each_child(dev, target, device_is_dependent);
285 list_for_each_entry(link, &dev->links.consumers, s_node) {
286 if ((link->flags & ~DL_FLAG_INFERRED) ==
287 (DL_FLAG_SYNC_STATE_ONLY | DL_FLAG_MANAGED))
290 if (link->consumer == target)
293 ret = device_is_dependent(link->consumer, target);
300 static void device_link_init_status(struct device_link *link,
301 struct device *consumer,
302 struct device *supplier)
304 switch (supplier->links.status) {
306 switch (consumer->links.status) {
309 * A consumer driver can create a link to a supplier
310 * that has not completed its probing yet as long as it
311 * knows that the supplier is already functional (for
312 * example, it has just acquired some resources from the
315 link->status = DL_STATE_CONSUMER_PROBE;
318 link->status = DL_STATE_DORMANT;
322 case DL_DEV_DRIVER_BOUND:
323 switch (consumer->links.status) {
325 link->status = DL_STATE_CONSUMER_PROBE;
327 case DL_DEV_DRIVER_BOUND:
328 link->status = DL_STATE_ACTIVE;
331 link->status = DL_STATE_AVAILABLE;
335 case DL_DEV_UNBINDING:
336 link->status = DL_STATE_SUPPLIER_UNBIND;
339 link->status = DL_STATE_DORMANT;
344 static int device_reorder_to_tail(struct device *dev, void *not_used)
346 struct device_link *link;
349 * Devices that have not been registered yet will be put to the ends
350 * of the lists during the registration, so skip them here.
352 if (device_is_registered(dev))
353 devices_kset_move_last(dev);
355 if (device_pm_initialized(dev))
356 device_pm_move_last(dev);
358 device_for_each_child(dev, NULL, device_reorder_to_tail);
359 list_for_each_entry(link, &dev->links.consumers, s_node) {
360 if ((link->flags & ~DL_FLAG_INFERRED) ==
361 (DL_FLAG_SYNC_STATE_ONLY | DL_FLAG_MANAGED))
363 device_reorder_to_tail(link->consumer, NULL);
370 * device_pm_move_to_tail - Move set of devices to the end of device lists
371 * @dev: Device to move
373 * This is a device_reorder_to_tail() wrapper taking the requisite locks.
375 * It moves the @dev along with all of its children and all of its consumers
376 * to the ends of the device_kset and dpm_list, recursively.
378 void device_pm_move_to_tail(struct device *dev)
382 idx = device_links_read_lock();
384 device_reorder_to_tail(dev, NULL);
386 device_links_read_unlock(idx);
389 #define to_devlink(dev) container_of((dev), struct device_link, link_dev)
391 static ssize_t status_show(struct device *dev,
392 struct device_attribute *attr, char *buf)
396 switch (to_devlink(dev)->status) {
398 output = "not tracked";
400 case DL_STATE_DORMANT:
403 case DL_STATE_AVAILABLE:
404 output = "available";
406 case DL_STATE_CONSUMER_PROBE:
407 output = "consumer probing";
409 case DL_STATE_ACTIVE:
412 case DL_STATE_SUPPLIER_UNBIND:
413 output = "supplier unbinding";
420 return sysfs_emit(buf, "%s\n", output);
422 static DEVICE_ATTR_RO(status);
424 static ssize_t auto_remove_on_show(struct device *dev,
425 struct device_attribute *attr, char *buf)
427 struct device_link *link = to_devlink(dev);
430 if (link->flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
431 output = "supplier unbind";
432 else if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER)
433 output = "consumer unbind";
437 return sysfs_emit(buf, "%s\n", output);
439 static DEVICE_ATTR_RO(auto_remove_on);
441 static ssize_t runtime_pm_show(struct device *dev,
442 struct device_attribute *attr, char *buf)
444 struct device_link *link = to_devlink(dev);
446 return sysfs_emit(buf, "%d\n", !!(link->flags & DL_FLAG_PM_RUNTIME));
448 static DEVICE_ATTR_RO(runtime_pm);
450 static ssize_t sync_state_only_show(struct device *dev,
451 struct device_attribute *attr, char *buf)
453 struct device_link *link = to_devlink(dev);
455 return sysfs_emit(buf, "%d\n",
456 !!(link->flags & DL_FLAG_SYNC_STATE_ONLY));
458 static DEVICE_ATTR_RO(sync_state_only);
460 static struct attribute *devlink_attrs[] = {
461 &dev_attr_status.attr,
462 &dev_attr_auto_remove_on.attr,
463 &dev_attr_runtime_pm.attr,
464 &dev_attr_sync_state_only.attr,
467 ATTRIBUTE_GROUPS(devlink);
469 static void device_link_release_fn(struct work_struct *work)
471 struct device_link *link = container_of(work, struct device_link, rm_work);
473 /* Ensure that all references to the link object have been dropped. */
474 device_link_synchronize_removal();
476 while (refcount_dec_not_one(&link->rpm_active))
477 pm_runtime_put(link->supplier);
479 put_device(link->consumer);
480 put_device(link->supplier);
484 static void devlink_dev_release(struct device *dev)
486 struct device_link *link = to_devlink(dev);
488 INIT_WORK(&link->rm_work, device_link_release_fn);
490 * It may take a while to complete this work because of the SRCU
491 * synchronization in device_link_release_fn() and if the consumer or
492 * supplier devices get deleted when it runs, so put it into the "long"
495 queue_work(system_long_wq, &link->rm_work);
498 static struct class devlink_class = {
500 .owner = THIS_MODULE,
501 .dev_groups = devlink_groups,
502 .dev_release = devlink_dev_release,
505 static int devlink_add_symlinks(struct device *dev,
506 struct class_interface *class_intf)
510 struct device_link *link = to_devlink(dev);
511 struct device *sup = link->supplier;
512 struct device *con = link->consumer;
515 len = max(strlen(dev_bus_name(sup)) + strlen(dev_name(sup)),
516 strlen(dev_bus_name(con)) + strlen(dev_name(con)));
518 len += strlen("supplier:") + 1;
519 buf = kzalloc(len, GFP_KERNEL);
523 ret = sysfs_create_link(&link->link_dev.kobj, &sup->kobj, "supplier");
527 ret = sysfs_create_link(&link->link_dev.kobj, &con->kobj, "consumer");
531 snprintf(buf, len, "consumer:%s:%s", dev_bus_name(con), dev_name(con));
532 ret = sysfs_create_link(&sup->kobj, &link->link_dev.kobj, buf);
536 snprintf(buf, len, "supplier:%s:%s", dev_bus_name(sup), dev_name(sup));
537 ret = sysfs_create_link(&con->kobj, &link->link_dev.kobj, buf);
544 snprintf(buf, len, "consumer:%s:%s", dev_bus_name(con), dev_name(con));
545 sysfs_remove_link(&sup->kobj, buf);
547 sysfs_remove_link(&link->link_dev.kobj, "consumer");
549 sysfs_remove_link(&link->link_dev.kobj, "supplier");
555 static void devlink_remove_symlinks(struct device *dev,
556 struct class_interface *class_intf)
558 struct device_link *link = to_devlink(dev);
560 struct device *sup = link->supplier;
561 struct device *con = link->consumer;
564 sysfs_remove_link(&link->link_dev.kobj, "consumer");
565 sysfs_remove_link(&link->link_dev.kobj, "supplier");
567 len = max(strlen(dev_bus_name(sup)) + strlen(dev_name(sup)),
568 strlen(dev_bus_name(con)) + strlen(dev_name(con)));
570 len += strlen("supplier:") + 1;
571 buf = kzalloc(len, GFP_KERNEL);
573 WARN(1, "Unable to properly free device link symlinks!\n");
577 if (device_is_registered(con)) {
578 snprintf(buf, len, "supplier:%s:%s", dev_bus_name(sup), dev_name(sup));
579 sysfs_remove_link(&con->kobj, buf);
581 snprintf(buf, len, "consumer:%s:%s", dev_bus_name(con), dev_name(con));
582 sysfs_remove_link(&sup->kobj, buf);
586 static struct class_interface devlink_class_intf = {
587 .class = &devlink_class,
588 .add_dev = devlink_add_symlinks,
589 .remove_dev = devlink_remove_symlinks,
592 static int __init devlink_class_init(void)
596 ret = class_register(&devlink_class);
600 ret = class_interface_register(&devlink_class_intf);
602 class_unregister(&devlink_class);
606 postcore_initcall(devlink_class_init);
608 #define DL_MANAGED_LINK_FLAGS (DL_FLAG_AUTOREMOVE_CONSUMER | \
609 DL_FLAG_AUTOREMOVE_SUPPLIER | \
610 DL_FLAG_AUTOPROBE_CONSUMER | \
611 DL_FLAG_SYNC_STATE_ONLY | \
614 #define DL_ADD_VALID_FLAGS (DL_MANAGED_LINK_FLAGS | DL_FLAG_STATELESS | \
615 DL_FLAG_PM_RUNTIME | DL_FLAG_RPM_ACTIVE)
618 * device_link_add - Create a link between two devices.
619 * @consumer: Consumer end of the link.
620 * @supplier: Supplier end of the link.
621 * @flags: Link flags.
623 * The caller is responsible for the proper synchronization of the link creation
624 * with runtime PM. First, setting the DL_FLAG_PM_RUNTIME flag will cause the
625 * runtime PM framework to take the link into account. Second, if the
626 * DL_FLAG_RPM_ACTIVE flag is set in addition to it, the supplier devices will
627 * be forced into the active meta state and reference-counted upon the creation
628 * of the link. If DL_FLAG_PM_RUNTIME is not set, DL_FLAG_RPM_ACTIVE will be
631 * If DL_FLAG_STATELESS is set in @flags, the caller of this function is
632 * expected to release the link returned by it directly with the help of either
633 * device_link_del() or device_link_remove().
635 * If that flag is not set, however, the caller of this function is handing the
636 * management of the link over to the driver core entirely and its return value
637 * can only be used to check whether or not the link is present. In that case,
638 * the DL_FLAG_AUTOREMOVE_CONSUMER and DL_FLAG_AUTOREMOVE_SUPPLIER device link
639 * flags can be used to indicate to the driver core when the link can be safely
640 * deleted. Namely, setting one of them in @flags indicates to the driver core
641 * that the link is not going to be used (by the given caller of this function)
642 * after unbinding the consumer or supplier driver, respectively, from its
643 * device, so the link can be deleted at that point. If none of them is set,
644 * the link will be maintained until one of the devices pointed to by it (either
645 * the consumer or the supplier) is unregistered.
647 * Also, if DL_FLAG_STATELESS, DL_FLAG_AUTOREMOVE_CONSUMER and
648 * DL_FLAG_AUTOREMOVE_SUPPLIER are not set in @flags (that is, a persistent
649 * managed device link is being added), the DL_FLAG_AUTOPROBE_CONSUMER flag can
650 * be used to request the driver core to automatically probe for a consumer
651 * driver after successfully binding a driver to the supplier device.
653 * The combination of DL_FLAG_STATELESS and one of DL_FLAG_AUTOREMOVE_CONSUMER,
654 * DL_FLAG_AUTOREMOVE_SUPPLIER, or DL_FLAG_AUTOPROBE_CONSUMER set in @flags at
655 * the same time is invalid and will cause NULL to be returned upfront.
656 * However, if a device link between the given @consumer and @supplier pair
657 * exists already when this function is called for them, the existing link will
658 * be returned regardless of its current type and status (the link's flags may
659 * be modified then). The caller of this function is then expected to treat
660 * the link as though it has just been created, so (in particular) if
661 * DL_FLAG_STATELESS was passed in @flags, the link needs to be released
662 * explicitly when not needed any more (as stated above).
664 * A side effect of the link creation is re-ordering of dpm_list and the
665 * devices_kset list by moving the consumer device and all devices depending
666 * on it to the ends of these lists (that does not happen to devices that have
667 * not been registered when this function is called).
669 * The supplier device is required to be registered when this function is called
670 * and NULL will be returned if that is not the case. The consumer device need
671 * not be registered, however.
673 struct device_link *device_link_add(struct device *consumer,
674 struct device *supplier, u32 flags)
676 struct device_link *link;
678 if (!consumer || !supplier || flags & ~DL_ADD_VALID_FLAGS ||
679 (flags & DL_FLAG_STATELESS && flags & DL_MANAGED_LINK_FLAGS) ||
680 (flags & DL_FLAG_SYNC_STATE_ONLY &&
681 (flags & ~DL_FLAG_INFERRED) != DL_FLAG_SYNC_STATE_ONLY) ||
682 (flags & DL_FLAG_AUTOPROBE_CONSUMER &&
683 flags & (DL_FLAG_AUTOREMOVE_CONSUMER |
684 DL_FLAG_AUTOREMOVE_SUPPLIER)))
687 if (flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) {
688 if (pm_runtime_get_sync(supplier) < 0) {
689 pm_runtime_put_noidle(supplier);
694 if (!(flags & DL_FLAG_STATELESS))
695 flags |= DL_FLAG_MANAGED;
697 device_links_write_lock();
701 * If the supplier has not been fully registered yet or there is a
702 * reverse (non-SYNC_STATE_ONLY) dependency between the consumer and
703 * the supplier already in the graph, return NULL. If the link is a
704 * SYNC_STATE_ONLY link, we don't check for reverse dependencies
705 * because it only affects sync_state() callbacks.
707 if (!device_pm_initialized(supplier)
708 || (!(flags & DL_FLAG_SYNC_STATE_ONLY) &&
709 device_is_dependent(consumer, supplier))) {
715 * SYNC_STATE_ONLY links are useless once a consumer device has probed.
716 * So, only create it if the consumer hasn't probed yet.
718 if (flags & DL_FLAG_SYNC_STATE_ONLY &&
719 consumer->links.status != DL_DEV_NO_DRIVER &&
720 consumer->links.status != DL_DEV_PROBING) {
726 * DL_FLAG_AUTOREMOVE_SUPPLIER indicates that the link will be needed
727 * longer than for DL_FLAG_AUTOREMOVE_CONSUMER and setting them both
728 * together doesn't make sense, so prefer DL_FLAG_AUTOREMOVE_SUPPLIER.
730 if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
731 flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER;
733 list_for_each_entry(link, &supplier->links.consumers, s_node) {
734 if (link->consumer != consumer)
737 if (link->flags & DL_FLAG_INFERRED &&
738 !(flags & DL_FLAG_INFERRED))
739 link->flags &= ~DL_FLAG_INFERRED;
741 if (flags & DL_FLAG_PM_RUNTIME) {
742 if (!(link->flags & DL_FLAG_PM_RUNTIME)) {
743 pm_runtime_new_link(consumer);
744 link->flags |= DL_FLAG_PM_RUNTIME;
746 if (flags & DL_FLAG_RPM_ACTIVE)
747 refcount_inc(&link->rpm_active);
750 if (flags & DL_FLAG_STATELESS) {
751 kref_get(&link->kref);
752 if (link->flags & DL_FLAG_SYNC_STATE_ONLY &&
753 !(link->flags & DL_FLAG_STATELESS)) {
754 link->flags |= DL_FLAG_STATELESS;
757 link->flags |= DL_FLAG_STATELESS;
763 * If the life time of the link following from the new flags is
764 * longer than indicated by the flags of the existing link,
765 * update the existing link to stay around longer.
767 if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER) {
768 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER) {
769 link->flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER;
770 link->flags |= DL_FLAG_AUTOREMOVE_SUPPLIER;
772 } else if (!(flags & DL_FLAG_AUTOREMOVE_CONSUMER)) {
773 link->flags &= ~(DL_FLAG_AUTOREMOVE_CONSUMER |
774 DL_FLAG_AUTOREMOVE_SUPPLIER);
776 if (!(link->flags & DL_FLAG_MANAGED)) {
777 kref_get(&link->kref);
778 link->flags |= DL_FLAG_MANAGED;
779 device_link_init_status(link, consumer, supplier);
781 if (link->flags & DL_FLAG_SYNC_STATE_ONLY &&
782 !(flags & DL_FLAG_SYNC_STATE_ONLY)) {
783 link->flags &= ~DL_FLAG_SYNC_STATE_ONLY;
790 link = kzalloc(sizeof(*link), GFP_KERNEL);
794 refcount_set(&link->rpm_active, 1);
796 get_device(supplier);
797 link->supplier = supplier;
798 INIT_LIST_HEAD(&link->s_node);
799 get_device(consumer);
800 link->consumer = consumer;
801 INIT_LIST_HEAD(&link->c_node);
803 kref_init(&link->kref);
805 link->link_dev.class = &devlink_class;
806 device_set_pm_not_required(&link->link_dev);
807 dev_set_name(&link->link_dev, "%s:%s--%s:%s",
808 dev_bus_name(supplier), dev_name(supplier),
809 dev_bus_name(consumer), dev_name(consumer));
810 if (device_register(&link->link_dev)) {
811 put_device(consumer);
812 put_device(supplier);
818 if (flags & DL_FLAG_PM_RUNTIME) {
819 if (flags & DL_FLAG_RPM_ACTIVE)
820 refcount_inc(&link->rpm_active);
822 pm_runtime_new_link(consumer);
825 /* Determine the initial link state. */
826 if (flags & DL_FLAG_STATELESS)
827 link->status = DL_STATE_NONE;
829 device_link_init_status(link, consumer, supplier);
832 * Some callers expect the link creation during consumer driver probe to
833 * resume the supplier even without DL_FLAG_RPM_ACTIVE.
835 if (link->status == DL_STATE_CONSUMER_PROBE &&
836 flags & DL_FLAG_PM_RUNTIME)
837 pm_runtime_resume(supplier);
839 list_add_tail_rcu(&link->s_node, &supplier->links.consumers);
840 list_add_tail_rcu(&link->c_node, &consumer->links.suppliers);
842 if (flags & DL_FLAG_SYNC_STATE_ONLY) {
844 "Linked as a sync state only consumer to %s\n",
851 * Move the consumer and all of the devices depending on it to the end
852 * of dpm_list and the devices_kset list.
854 * It is necessary to hold dpm_list locked throughout all that or else
855 * we may end up suspending with a wrong ordering of it.
857 device_reorder_to_tail(consumer, NULL);
859 dev_dbg(consumer, "Linked as a consumer to %s\n", dev_name(supplier));
863 device_links_write_unlock();
865 if ((flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) && !link)
866 pm_runtime_put(supplier);
870 EXPORT_SYMBOL_GPL(device_link_add);
872 static void __device_link_del(struct kref *kref)
874 struct device_link *link = container_of(kref, struct device_link, kref);
876 dev_dbg(link->consumer, "Dropping the link to %s\n",
877 dev_name(link->supplier));
879 pm_runtime_drop_link(link);
881 device_link_remove_from_lists(link);
882 device_unregister(&link->link_dev);
885 static void device_link_put_kref(struct device_link *link)
887 if (link->flags & DL_FLAG_STATELESS)
888 kref_put(&link->kref, __device_link_del);
890 WARN(1, "Unable to drop a managed device link reference\n");
894 * device_link_del - Delete a stateless link between two devices.
895 * @link: Device link to delete.
897 * The caller must ensure proper synchronization of this function with runtime
898 * PM. If the link was added multiple times, it needs to be deleted as often.
899 * Care is required for hotplugged devices: Their links are purged on removal
900 * and calling device_link_del() is then no longer allowed.
902 void device_link_del(struct device_link *link)
904 device_links_write_lock();
905 device_link_put_kref(link);
906 device_links_write_unlock();
908 EXPORT_SYMBOL_GPL(device_link_del);
911 * device_link_remove - Delete a stateless link between two devices.
912 * @consumer: Consumer end of the link.
913 * @supplier: Supplier end of the link.
915 * The caller must ensure proper synchronization of this function with runtime
918 void device_link_remove(void *consumer, struct device *supplier)
920 struct device_link *link;
922 if (WARN_ON(consumer == supplier))
925 device_links_write_lock();
927 list_for_each_entry(link, &supplier->links.consumers, s_node) {
928 if (link->consumer == consumer) {
929 device_link_put_kref(link);
934 device_links_write_unlock();
936 EXPORT_SYMBOL_GPL(device_link_remove);
938 static void device_links_missing_supplier(struct device *dev)
940 struct device_link *link;
942 list_for_each_entry(link, &dev->links.suppliers, c_node) {
943 if (link->status != DL_STATE_CONSUMER_PROBE)
946 if (link->supplier->links.status == DL_DEV_DRIVER_BOUND) {
947 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
949 WARN_ON(!(link->flags & DL_FLAG_SYNC_STATE_ONLY));
950 WRITE_ONCE(link->status, DL_STATE_DORMANT);
956 * device_links_check_suppliers - Check presence of supplier drivers.
957 * @dev: Consumer device.
959 * Check links from this device to any suppliers. Walk the list of the device's
960 * links to suppliers and see if all of them are available. If not, simply
961 * return -EPROBE_DEFER.
963 * We need to guarantee that the supplier will not go away after the check has
964 * been positive here. It only can go away in __device_release_driver() and
965 * that function checks the device's links to consumers. This means we need to
966 * mark the link as "consumer probe in progress" to make the supplier removal
967 * wait for us to complete (or bad things may happen).
969 * Links without the DL_FLAG_MANAGED flag set are ignored.
971 int device_links_check_suppliers(struct device *dev)
973 struct device_link *link;
977 * Device waiting for supplier to become available is not allowed to
980 mutex_lock(&fwnode_link_lock);
981 if (dev->fwnode && !list_empty(&dev->fwnode->suppliers) &&
982 !fw_devlink_is_permissive()) {
983 dev_dbg(dev, "probe deferral - wait for supplier %pfwP\n",
984 list_first_entry(&dev->fwnode->suppliers,
987 mutex_unlock(&fwnode_link_lock);
988 return -EPROBE_DEFER;
990 mutex_unlock(&fwnode_link_lock);
992 device_links_write_lock();
994 list_for_each_entry(link, &dev->links.suppliers, c_node) {
995 if (!(link->flags & DL_FLAG_MANAGED))
998 if (link->status != DL_STATE_AVAILABLE &&
999 !(link->flags & DL_FLAG_SYNC_STATE_ONLY)) {
1000 device_links_missing_supplier(dev);
1001 dev_dbg(dev, "probe deferral - supplier %s not ready\n",
1002 dev_name(link->supplier));
1003 ret = -EPROBE_DEFER;
1006 WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE);
1008 dev->links.status = DL_DEV_PROBING;
1010 device_links_write_unlock();
1015 * __device_links_queue_sync_state - Queue a device for sync_state() callback
1016 * @dev: Device to call sync_state() on
1017 * @list: List head to queue the @dev on
1019 * Queues a device for a sync_state() callback when the device links write lock
1020 * isn't held. This allows the sync_state() execution flow to use device links
1021 * APIs. The caller must ensure this function is called with
1022 * device_links_write_lock() held.
1024 * This function does a get_device() to make sure the device is not freed while
1027 * So the caller must also ensure that device_links_flush_sync_list() is called
1028 * as soon as the caller releases device_links_write_lock(). This is necessary
1029 * to make sure the sync_state() is called in a timely fashion and the
1030 * put_device() is called on this device.
1032 static void __device_links_queue_sync_state(struct device *dev,
1033 struct list_head *list)
1035 struct device_link *link;
1037 if (!dev_has_sync_state(dev))
1039 if (dev->state_synced)
1042 list_for_each_entry(link, &dev->links.consumers, s_node) {
1043 if (!(link->flags & DL_FLAG_MANAGED))
1045 if (link->status != DL_STATE_ACTIVE)
1050 * Set the flag here to avoid adding the same device to a list more
1051 * than once. This can happen if new consumers get added to the device
1052 * and probed before the list is flushed.
1054 dev->state_synced = true;
1056 if (WARN_ON(!list_empty(&dev->links.defer_sync)))
1060 list_add_tail(&dev->links.defer_sync, list);
1064 * device_links_flush_sync_list - Call sync_state() on a list of devices
1065 * @list: List of devices to call sync_state() on
1066 * @dont_lock_dev: Device for which lock is already held by the caller
1068 * Calls sync_state() on all the devices that have been queued for it. This
1069 * function is used in conjunction with __device_links_queue_sync_state(). The
1070 * @dont_lock_dev parameter is useful when this function is called from a
1071 * context where a device lock is already held.
1073 static void device_links_flush_sync_list(struct list_head *list,
1074 struct device *dont_lock_dev)
1076 struct device *dev, *tmp;
1078 list_for_each_entry_safe(dev, tmp, list, links.defer_sync) {
1079 list_del_init(&dev->links.defer_sync);
1081 if (dev != dont_lock_dev)
1084 if (dev->bus->sync_state)
1085 dev->bus->sync_state(dev);
1086 else if (dev->driver && dev->driver->sync_state)
1087 dev->driver->sync_state(dev);
1089 if (dev != dont_lock_dev)
1096 void device_links_supplier_sync_state_pause(void)
1098 device_links_write_lock();
1099 defer_sync_state_count++;
1100 device_links_write_unlock();
1103 void device_links_supplier_sync_state_resume(void)
1105 struct device *dev, *tmp;
1106 LIST_HEAD(sync_list);
1108 device_links_write_lock();
1109 if (!defer_sync_state_count) {
1110 WARN(true, "Unmatched sync_state pause/resume!");
1113 defer_sync_state_count--;
1114 if (defer_sync_state_count)
1117 list_for_each_entry_safe(dev, tmp, &deferred_sync, links.defer_sync) {
1119 * Delete from deferred_sync list before queuing it to
1120 * sync_list because defer_sync is used for both lists.
1122 list_del_init(&dev->links.defer_sync);
1123 __device_links_queue_sync_state(dev, &sync_list);
1126 device_links_write_unlock();
1128 device_links_flush_sync_list(&sync_list, NULL);
1131 static int sync_state_resume_initcall(void)
1133 device_links_supplier_sync_state_resume();
1136 late_initcall(sync_state_resume_initcall);
1138 static void __device_links_supplier_defer_sync(struct device *sup)
1140 if (list_empty(&sup->links.defer_sync) && dev_has_sync_state(sup))
1141 list_add_tail(&sup->links.defer_sync, &deferred_sync);
1144 static void device_link_drop_managed(struct device_link *link)
1146 link->flags &= ~DL_FLAG_MANAGED;
1147 WRITE_ONCE(link->status, DL_STATE_NONE);
1148 kref_put(&link->kref, __device_link_del);
1151 static ssize_t waiting_for_supplier_show(struct device *dev,
1152 struct device_attribute *attr,
1158 val = !list_empty(&dev->fwnode->suppliers);
1160 return sysfs_emit(buf, "%u\n", val);
1162 static DEVICE_ATTR_RO(waiting_for_supplier);
1165 * device_links_force_bind - Prepares device to be force bound
1166 * @dev: Consumer device.
1168 * device_bind_driver() force binds a device to a driver without calling any
1169 * driver probe functions. So the consumer really isn't going to wait for any
1170 * supplier before it's bound to the driver. We still want the device link
1171 * states to be sensible when this happens.
1173 * In preparation for device_bind_driver(), this function goes through each
1174 * supplier device links and checks if the supplier is bound. If it is, then
1175 * the device link status is set to CONSUMER_PROBE. Otherwise, the device link
1176 * is dropped. Links without the DL_FLAG_MANAGED flag set are ignored.
1178 void device_links_force_bind(struct device *dev)
1180 struct device_link *link, *ln;
1182 device_links_write_lock();
1184 list_for_each_entry_safe(link, ln, &dev->links.suppliers, c_node) {
1185 if (!(link->flags & DL_FLAG_MANAGED))
1188 if (link->status != DL_STATE_AVAILABLE) {
1189 device_link_drop_managed(link);
1192 WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE);
1194 dev->links.status = DL_DEV_PROBING;
1196 device_links_write_unlock();
1200 * device_links_driver_bound - Update device links after probing its driver.
1201 * @dev: Device to update the links for.
1203 * The probe has been successful, so update links from this device to any
1204 * consumers by changing their status to "available".
1206 * Also change the status of @dev's links to suppliers to "active".
1208 * Links without the DL_FLAG_MANAGED flag set are ignored.
1210 void device_links_driver_bound(struct device *dev)
1212 struct device_link *link, *ln;
1213 LIST_HEAD(sync_list);
1216 * If a device binds successfully, it's expected to have created all
1217 * the device links it needs to or make new device links as it needs
1218 * them. So, fw_devlink no longer needs to create device links to any
1219 * of the device's suppliers.
1221 * Also, if a child firmware node of this bound device is not added as
1222 * a device by now, assume it is never going to be added and make sure
1223 * other devices don't defer probe indefinitely by waiting for such a
1226 if (dev->fwnode && dev->fwnode->dev == dev) {
1227 struct fwnode_handle *child;
1228 fwnode_links_purge_suppliers(dev->fwnode);
1229 fwnode_for_each_available_child_node(dev->fwnode, child)
1230 fw_devlink_purge_absent_suppliers(child);
1232 device_remove_file(dev, &dev_attr_waiting_for_supplier);
1234 device_links_write_lock();
1236 list_for_each_entry(link, &dev->links.consumers, s_node) {
1237 if (!(link->flags & DL_FLAG_MANAGED))
1241 * Links created during consumer probe may be in the "consumer
1242 * probe" state to start with if the supplier is still probing
1243 * when they are created and they may become "active" if the
1244 * consumer probe returns first. Skip them here.
1246 if (link->status == DL_STATE_CONSUMER_PROBE ||
1247 link->status == DL_STATE_ACTIVE)
1250 WARN_ON(link->status != DL_STATE_DORMANT);
1251 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
1253 if (link->flags & DL_FLAG_AUTOPROBE_CONSUMER)
1254 driver_deferred_probe_add(link->consumer);
1257 if (defer_sync_state_count)
1258 __device_links_supplier_defer_sync(dev);
1260 __device_links_queue_sync_state(dev, &sync_list);
1262 list_for_each_entry_safe(link, ln, &dev->links.suppliers, c_node) {
1263 struct device *supplier;
1265 if (!(link->flags & DL_FLAG_MANAGED))
1268 supplier = link->supplier;
1269 if (link->flags & DL_FLAG_SYNC_STATE_ONLY) {
1271 * When DL_FLAG_SYNC_STATE_ONLY is set, it means no
1272 * other DL_MANAGED_LINK_FLAGS have been set. So, it's
1273 * save to drop the managed link completely.
1275 device_link_drop_managed(link);
1277 WARN_ON(link->status != DL_STATE_CONSUMER_PROBE);
1278 WRITE_ONCE(link->status, DL_STATE_ACTIVE);
1282 * This needs to be done even for the deleted
1283 * DL_FLAG_SYNC_STATE_ONLY device link in case it was the last
1284 * device link that was preventing the supplier from getting a
1285 * sync_state() call.
1287 if (defer_sync_state_count)
1288 __device_links_supplier_defer_sync(supplier);
1290 __device_links_queue_sync_state(supplier, &sync_list);
1293 dev->links.status = DL_DEV_DRIVER_BOUND;
1295 device_links_write_unlock();
1297 device_links_flush_sync_list(&sync_list, dev);
1301 * __device_links_no_driver - Update links of a device without a driver.
1302 * @dev: Device without a drvier.
1304 * Delete all non-persistent links from this device to any suppliers.
1306 * Persistent links stay around, but their status is changed to "available",
1307 * unless they already are in the "supplier unbind in progress" state in which
1308 * case they need not be updated.
1310 * Links without the DL_FLAG_MANAGED flag set are ignored.
1312 static void __device_links_no_driver(struct device *dev)
1314 struct device_link *link, *ln;
1316 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
1317 if (!(link->flags & DL_FLAG_MANAGED))
1320 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER) {
1321 device_link_drop_managed(link);
1325 if (link->status != DL_STATE_CONSUMER_PROBE &&
1326 link->status != DL_STATE_ACTIVE)
1329 if (link->supplier->links.status == DL_DEV_DRIVER_BOUND) {
1330 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
1332 WARN_ON(!(link->flags & DL_FLAG_SYNC_STATE_ONLY));
1333 WRITE_ONCE(link->status, DL_STATE_DORMANT);
1337 dev->links.status = DL_DEV_NO_DRIVER;
1341 * device_links_no_driver - Update links after failing driver probe.
1342 * @dev: Device whose driver has just failed to probe.
1344 * Clean up leftover links to consumers for @dev and invoke
1345 * %__device_links_no_driver() to update links to suppliers for it as
1348 * Links without the DL_FLAG_MANAGED flag set are ignored.
1350 void device_links_no_driver(struct device *dev)
1352 struct device_link *link;
1354 device_links_write_lock();
1356 list_for_each_entry(link, &dev->links.consumers, s_node) {
1357 if (!(link->flags & DL_FLAG_MANAGED))
1361 * The probe has failed, so if the status of the link is
1362 * "consumer probe" or "active", it must have been added by
1363 * a probing consumer while this device was still probing.
1364 * Change its state to "dormant", as it represents a valid
1365 * relationship, but it is not functionally meaningful.
1367 if (link->status == DL_STATE_CONSUMER_PROBE ||
1368 link->status == DL_STATE_ACTIVE)
1369 WRITE_ONCE(link->status, DL_STATE_DORMANT);
1372 __device_links_no_driver(dev);
1374 device_links_write_unlock();
1378 * device_links_driver_cleanup - Update links after driver removal.
1379 * @dev: Device whose driver has just gone away.
1381 * Update links to consumers for @dev by changing their status to "dormant" and
1382 * invoke %__device_links_no_driver() to update links to suppliers for it as
1385 * Links without the DL_FLAG_MANAGED flag set are ignored.
1387 void device_links_driver_cleanup(struct device *dev)
1389 struct device_link *link, *ln;
1391 device_links_write_lock();
1393 list_for_each_entry_safe(link, ln, &dev->links.consumers, s_node) {
1394 if (!(link->flags & DL_FLAG_MANAGED))
1397 WARN_ON(link->flags & DL_FLAG_AUTOREMOVE_CONSUMER);
1398 WARN_ON(link->status != DL_STATE_SUPPLIER_UNBIND);
1401 * autoremove the links between this @dev and its consumer
1402 * devices that are not active, i.e. where the link state
1403 * has moved to DL_STATE_SUPPLIER_UNBIND.
1405 if (link->status == DL_STATE_SUPPLIER_UNBIND &&
1406 link->flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
1407 device_link_drop_managed(link);
1409 WRITE_ONCE(link->status, DL_STATE_DORMANT);
1412 list_del_init(&dev->links.defer_sync);
1413 __device_links_no_driver(dev);
1415 device_links_write_unlock();
1419 * device_links_busy - Check if there are any busy links to consumers.
1420 * @dev: Device to check.
1422 * Check each consumer of the device and return 'true' if its link's status
1423 * is one of "consumer probe" or "active" (meaning that the given consumer is
1424 * probing right now or its driver is present). Otherwise, change the link
1425 * state to "supplier unbind" to prevent the consumer from being probed
1426 * successfully going forward.
1428 * Return 'false' if there are no probing or active consumers.
1430 * Links without the DL_FLAG_MANAGED flag set are ignored.
1432 bool device_links_busy(struct device *dev)
1434 struct device_link *link;
1437 device_links_write_lock();
1439 list_for_each_entry(link, &dev->links.consumers, s_node) {
1440 if (!(link->flags & DL_FLAG_MANAGED))
1443 if (link->status == DL_STATE_CONSUMER_PROBE
1444 || link->status == DL_STATE_ACTIVE) {
1448 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
1451 dev->links.status = DL_DEV_UNBINDING;
1453 device_links_write_unlock();
1458 * device_links_unbind_consumers - Force unbind consumers of the given device.
1459 * @dev: Device to unbind the consumers of.
1461 * Walk the list of links to consumers for @dev and if any of them is in the
1462 * "consumer probe" state, wait for all device probes in progress to complete
1465 * If that's not the case, change the status of the link to "supplier unbind"
1466 * and check if the link was in the "active" state. If so, force the consumer
1467 * driver to unbind and start over (the consumer will not re-probe as we have
1468 * changed the state of the link already).
1470 * Links without the DL_FLAG_MANAGED flag set are ignored.
1472 void device_links_unbind_consumers(struct device *dev)
1474 struct device_link *link;
1477 device_links_write_lock();
1479 list_for_each_entry(link, &dev->links.consumers, s_node) {
1480 enum device_link_state status;
1482 if (!(link->flags & DL_FLAG_MANAGED) ||
1483 link->flags & DL_FLAG_SYNC_STATE_ONLY)
1486 status = link->status;
1487 if (status == DL_STATE_CONSUMER_PROBE) {
1488 device_links_write_unlock();
1490 wait_for_device_probe();
1493 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
1494 if (status == DL_STATE_ACTIVE) {
1495 struct device *consumer = link->consumer;
1497 get_device(consumer);
1499 device_links_write_unlock();
1501 device_release_driver_internal(consumer, NULL,
1503 put_device(consumer);
1508 device_links_write_unlock();
1512 * device_links_purge - Delete existing links to other devices.
1513 * @dev: Target device.
1515 static void device_links_purge(struct device *dev)
1517 struct device_link *link, *ln;
1519 if (dev->class == &devlink_class)
1523 * Delete all of the remaining links from this device to any other
1524 * devices (either consumers or suppliers).
1526 device_links_write_lock();
1528 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
1529 WARN_ON(link->status == DL_STATE_ACTIVE);
1530 __device_link_del(&link->kref);
1533 list_for_each_entry_safe_reverse(link, ln, &dev->links.consumers, s_node) {
1534 WARN_ON(link->status != DL_STATE_DORMANT &&
1535 link->status != DL_STATE_NONE);
1536 __device_link_del(&link->kref);
1539 device_links_write_unlock();
1542 #define FW_DEVLINK_FLAGS_PERMISSIVE (DL_FLAG_INFERRED | \
1543 DL_FLAG_SYNC_STATE_ONLY)
1544 #define FW_DEVLINK_FLAGS_ON (DL_FLAG_INFERRED | \
1545 DL_FLAG_AUTOPROBE_CONSUMER)
1546 #define FW_DEVLINK_FLAGS_RPM (FW_DEVLINK_FLAGS_ON | \
1549 static u32 fw_devlink_flags = FW_DEVLINK_FLAGS_ON;
1550 static int __init fw_devlink_setup(char *arg)
1555 if (strcmp(arg, "off") == 0) {
1556 fw_devlink_flags = 0;
1557 } else if (strcmp(arg, "permissive") == 0) {
1558 fw_devlink_flags = FW_DEVLINK_FLAGS_PERMISSIVE;
1559 } else if (strcmp(arg, "on") == 0) {
1560 fw_devlink_flags = FW_DEVLINK_FLAGS_ON;
1561 } else if (strcmp(arg, "rpm") == 0) {
1562 fw_devlink_flags = FW_DEVLINK_FLAGS_RPM;
1566 early_param("fw_devlink", fw_devlink_setup);
1568 static bool fw_devlink_strict;
1569 static int __init fw_devlink_strict_setup(char *arg)
1571 return strtobool(arg, &fw_devlink_strict);
1573 early_param("fw_devlink.strict", fw_devlink_strict_setup);
1575 u32 fw_devlink_get_flags(void)
1577 return fw_devlink_flags;
1580 static bool fw_devlink_is_permissive(void)
1582 return fw_devlink_flags == FW_DEVLINK_FLAGS_PERMISSIVE;
1585 bool fw_devlink_is_strict(void)
1587 return fw_devlink_strict && !fw_devlink_is_permissive();
1590 static void fw_devlink_parse_fwnode(struct fwnode_handle *fwnode)
1592 if (fwnode->flags & FWNODE_FLAG_LINKS_ADDED)
1595 fwnode_call_int_op(fwnode, add_links);
1596 fwnode->flags |= FWNODE_FLAG_LINKS_ADDED;
1599 static void fw_devlink_parse_fwtree(struct fwnode_handle *fwnode)
1601 struct fwnode_handle *child = NULL;
1603 fw_devlink_parse_fwnode(fwnode);
1605 while ((child = fwnode_get_next_available_child_node(fwnode, child)))
1606 fw_devlink_parse_fwtree(child);
1609 static void fw_devlink_relax_link(struct device_link *link)
1611 if (!(link->flags & DL_FLAG_INFERRED))
1614 if (link->flags == (DL_FLAG_MANAGED | FW_DEVLINK_FLAGS_PERMISSIVE))
1617 pm_runtime_drop_link(link);
1618 link->flags = DL_FLAG_MANAGED | FW_DEVLINK_FLAGS_PERMISSIVE;
1619 dev_dbg(link->consumer, "Relaxing link with %s\n",
1620 dev_name(link->supplier));
1623 static int fw_devlink_no_driver(struct device *dev, void *data)
1625 struct device_link *link = to_devlink(dev);
1627 if (!link->supplier->can_match)
1628 fw_devlink_relax_link(link);
1633 void fw_devlink_drivers_done(void)
1635 fw_devlink_drv_reg_done = true;
1636 device_links_write_lock();
1637 class_for_each_device(&devlink_class, NULL, NULL,
1638 fw_devlink_no_driver);
1639 device_links_write_unlock();
1642 static void fw_devlink_unblock_consumers(struct device *dev)
1644 struct device_link *link;
1646 if (!fw_devlink_flags || fw_devlink_is_permissive())
1649 device_links_write_lock();
1650 list_for_each_entry(link, &dev->links.consumers, s_node)
1651 fw_devlink_relax_link(link);
1652 device_links_write_unlock();
1656 * fw_devlink_relax_cycle - Convert cyclic links to SYNC_STATE_ONLY links
1657 * @con: Device to check dependencies for.
1658 * @sup: Device to check against.
1660 * Check if @sup depends on @con or any device dependent on it (its child or
1661 * its consumer etc). When such a cyclic dependency is found, convert all
1662 * device links created solely by fw_devlink into SYNC_STATE_ONLY device links.
1663 * This is the equivalent of doing fw_devlink=permissive just between the
1664 * devices in the cycle. We need to do this because, at this point, fw_devlink
1665 * can't tell which of these dependencies is not a real dependency.
1667 * Return 1 if a cycle is found. Otherwise, return 0.
1669 static int fw_devlink_relax_cycle(struct device *con, void *sup)
1671 struct device_link *link;
1677 ret = device_for_each_child(con, sup, fw_devlink_relax_cycle);
1681 list_for_each_entry(link, &con->links.consumers, s_node) {
1682 if ((link->flags & ~DL_FLAG_INFERRED) ==
1683 (DL_FLAG_SYNC_STATE_ONLY | DL_FLAG_MANAGED))
1686 if (!fw_devlink_relax_cycle(link->consumer, sup))
1691 fw_devlink_relax_link(link);
1697 * fw_devlink_create_devlink - Create a device link from a consumer to fwnode
1698 * @con: consumer device for the device link
1699 * @sup_handle: fwnode handle of supplier
1700 * @flags: devlink flags
1702 * This function will try to create a device link between the consumer device
1703 * @con and the supplier device represented by @sup_handle.
1705 * The supplier has to be provided as a fwnode because incorrect cycles in
1706 * fwnode links can sometimes cause the supplier device to never be created.
1707 * This function detects such cases and returns an error if it cannot create a
1708 * device link from the consumer to a missing supplier.
1711 * 0 on successfully creating a device link
1712 * -EINVAL if the device link cannot be created as expected
1713 * -EAGAIN if the device link cannot be created right now, but it may be
1714 * possible to do that in the future
1716 static int fw_devlink_create_devlink(struct device *con,
1717 struct fwnode_handle *sup_handle, u32 flags)
1719 struct device *sup_dev;
1722 sup_dev = get_dev_from_fwnode(sup_handle);
1725 * If it's one of those drivers that don't actually bind to
1726 * their device using driver core, then don't wait on this
1727 * supplier device indefinitely.
1729 if (sup_dev->links.status == DL_DEV_NO_DRIVER &&
1730 sup_handle->flags & FWNODE_FLAG_INITIALIZED) {
1736 * If this fails, it is due to cycles in device links. Just
1737 * give up on this link and treat it as invalid.
1739 if (!device_link_add(con, sup_dev, flags) &&
1740 !(flags & DL_FLAG_SYNC_STATE_ONLY)) {
1741 dev_info(con, "Fixing up cyclic dependency with %s\n",
1743 device_links_write_lock();
1744 fw_devlink_relax_cycle(con, sup_dev);
1745 device_links_write_unlock();
1746 device_link_add(con, sup_dev,
1747 FW_DEVLINK_FLAGS_PERMISSIVE);
1754 /* Supplier that's already initialized without a struct device. */
1755 if (sup_handle->flags & FWNODE_FLAG_INITIALIZED)
1759 * DL_FLAG_SYNC_STATE_ONLY doesn't block probing and supports
1760 * cycles. So cycle detection isn't necessary and shouldn't be
1763 if (flags & DL_FLAG_SYNC_STATE_ONLY)
1767 * If we can't find the supplier device from its fwnode, it might be
1768 * due to a cyclic dependency between fwnodes. Some of these cycles can
1769 * be broken by applying logic. Check for these types of cycles and
1770 * break them so that devices in the cycle probe properly.
1772 * If the supplier's parent is dependent on the consumer, then
1773 * the consumer-supplier dependency is a false dependency. So,
1774 * treat it as an invalid link.
1776 sup_dev = fwnode_get_next_parent_dev(sup_handle);
1777 if (sup_dev && device_is_dependent(con, sup_dev)) {
1778 dev_dbg(con, "Not linking to %pfwP - False link\n",
1783 * Can't check for cycles or no cycles. So let's try
1790 put_device(sup_dev);
1795 * __fw_devlink_link_to_consumers - Create device links to consumers of a device
1796 * @dev: Device that needs to be linked to its consumers
1798 * This function looks at all the consumer fwnodes of @dev and creates device
1799 * links between the consumer device and @dev (supplier).
1801 * If the consumer device has not been added yet, then this function creates a
1802 * SYNC_STATE_ONLY link between @dev (supplier) and the closest ancestor device
1803 * of the consumer fwnode. This is necessary to make sure @dev doesn't get a
1804 * sync_state() callback before the real consumer device gets to be added and
1807 * Once device links are created from the real consumer to @dev (supplier), the
1808 * fwnode links are deleted.
1810 static void __fw_devlink_link_to_consumers(struct device *dev)
1812 struct fwnode_handle *fwnode = dev->fwnode;
1813 struct fwnode_link *link, *tmp;
1815 list_for_each_entry_safe(link, tmp, &fwnode->consumers, s_hook) {
1816 u32 dl_flags = fw_devlink_get_flags();
1817 struct device *con_dev;
1818 bool own_link = true;
1821 con_dev = get_dev_from_fwnode(link->consumer);
1823 * If consumer device is not available yet, make a "proxy"
1824 * SYNC_STATE_ONLY link from the consumer's parent device to
1825 * the supplier device. This is necessary to make sure the
1826 * supplier doesn't get a sync_state() callback before the real
1827 * consumer can create a device link to the supplier.
1829 * This proxy link step is needed to handle the case where the
1830 * consumer's parent device is added before the supplier.
1833 con_dev = fwnode_get_next_parent_dev(link->consumer);
1835 * However, if the consumer's parent device is also the
1836 * parent of the supplier, don't create a
1837 * consumer-supplier link from the parent to its child
1838 * device. Such a dependency is impossible.
1841 fwnode_is_ancestor_of(con_dev->fwnode, fwnode)) {
1842 put_device(con_dev);
1846 dl_flags = FW_DEVLINK_FLAGS_PERMISSIVE;
1853 ret = fw_devlink_create_devlink(con_dev, fwnode, dl_flags);
1854 put_device(con_dev);
1855 if (!own_link || ret == -EAGAIN)
1858 list_del(&link->s_hook);
1859 list_del(&link->c_hook);
1865 * __fw_devlink_link_to_suppliers - Create device links to suppliers of a device
1866 * @dev: The consumer device that needs to be linked to its suppliers
1867 * @fwnode: Root of the fwnode tree that is used to create device links
1869 * This function looks at all the supplier fwnodes of fwnode tree rooted at
1870 * @fwnode and creates device links between @dev (consumer) and all the
1871 * supplier devices of the entire fwnode tree at @fwnode.
1873 * The function creates normal (non-SYNC_STATE_ONLY) device links between @dev
1874 * and the real suppliers of @dev. Once these device links are created, the
1875 * fwnode links are deleted. When such device links are successfully created,
1876 * this function is called recursively on those supplier devices. This is
1877 * needed to detect and break some invalid cycles in fwnode links. See
1878 * fw_devlink_create_devlink() for more details.
1880 * In addition, it also looks at all the suppliers of the entire fwnode tree
1881 * because some of the child devices of @dev that have not been added yet
1882 * (because @dev hasn't probed) might already have their suppliers added to
1883 * driver core. So, this function creates SYNC_STATE_ONLY device links between
1884 * @dev (consumer) and these suppliers to make sure they don't execute their
1885 * sync_state() callbacks before these child devices have a chance to create
1886 * their device links. The fwnode links that correspond to the child devices
1887 * aren't delete because they are needed later to create the device links
1888 * between the real consumer and supplier devices.
1890 static void __fw_devlink_link_to_suppliers(struct device *dev,
1891 struct fwnode_handle *fwnode)
1893 bool own_link = (dev->fwnode == fwnode);
1894 struct fwnode_link *link, *tmp;
1895 struct fwnode_handle *child = NULL;
1899 dl_flags = fw_devlink_get_flags();
1901 dl_flags = FW_DEVLINK_FLAGS_PERMISSIVE;
1903 list_for_each_entry_safe(link, tmp, &fwnode->suppliers, c_hook) {
1905 struct device *sup_dev;
1906 struct fwnode_handle *sup = link->supplier;
1908 ret = fw_devlink_create_devlink(dev, sup, dl_flags);
1909 if (!own_link || ret == -EAGAIN)
1912 list_del(&link->s_hook);
1913 list_del(&link->c_hook);
1916 /* If no device link was created, nothing more to do. */
1921 * If a device link was successfully created to a supplier, we
1922 * now need to try and link the supplier to all its suppliers.
1924 * This is needed to detect and delete false dependencies in
1925 * fwnode links that haven't been converted to a device link
1926 * yet. See comments in fw_devlink_create_devlink() for more
1927 * details on the false dependency.
1929 * Without deleting these false dependencies, some devices will
1930 * never probe because they'll keep waiting for their false
1931 * dependency fwnode links to be converted to device links.
1933 sup_dev = get_dev_from_fwnode(sup);
1934 __fw_devlink_link_to_suppliers(sup_dev, sup_dev->fwnode);
1935 put_device(sup_dev);
1939 * Make "proxy" SYNC_STATE_ONLY device links to represent the needs of
1940 * all the descendants. This proxy link step is needed to handle the
1941 * case where the supplier is added before the consumer's parent device
1944 while ((child = fwnode_get_next_available_child_node(fwnode, child)))
1945 __fw_devlink_link_to_suppliers(dev, child);
1948 static void fw_devlink_link_device(struct device *dev)
1950 struct fwnode_handle *fwnode = dev->fwnode;
1952 if (!fw_devlink_flags)
1955 fw_devlink_parse_fwtree(fwnode);
1957 mutex_lock(&fwnode_link_lock);
1958 __fw_devlink_link_to_consumers(dev);
1959 __fw_devlink_link_to_suppliers(dev, fwnode);
1960 mutex_unlock(&fwnode_link_lock);
1963 /* Device links support end. */
1965 int (*platform_notify)(struct device *dev) = NULL;
1966 int (*platform_notify_remove)(struct device *dev) = NULL;
1967 static struct kobject *dev_kobj;
1968 struct kobject *sysfs_dev_char_kobj;
1969 struct kobject *sysfs_dev_block_kobj;
1971 static DEFINE_MUTEX(device_hotplug_lock);
1973 void lock_device_hotplug(void)
1975 mutex_lock(&device_hotplug_lock);
1978 void unlock_device_hotplug(void)
1980 mutex_unlock(&device_hotplug_lock);
1983 int lock_device_hotplug_sysfs(void)
1985 if (mutex_trylock(&device_hotplug_lock))
1988 /* Avoid busy looping (5 ms of sleep should do). */
1990 return restart_syscall();
1994 static inline int device_is_not_partition(struct device *dev)
1996 return !(dev->type == &part_type);
1999 static inline int device_is_not_partition(struct device *dev)
2006 device_platform_notify(struct device *dev, enum kobject_action action)
2010 ret = acpi_platform_notify(dev, action);
2014 ret = software_node_notify(dev, action);
2018 if (platform_notify && action == KOBJ_ADD)
2019 platform_notify(dev);
2020 else if (platform_notify_remove && action == KOBJ_REMOVE)
2021 platform_notify_remove(dev);
2026 * dev_driver_string - Return a device's driver name, if at all possible
2027 * @dev: struct device to get the name of
2029 * Will return the device's driver's name if it is bound to a device. If
2030 * the device is not bound to a driver, it will return the name of the bus
2031 * it is attached to. If it is not attached to a bus either, an empty
2032 * string will be returned.
2034 const char *dev_driver_string(const struct device *dev)
2036 struct device_driver *drv;
2038 /* dev->driver can change to NULL underneath us because of unbinding,
2039 * so be careful about accessing it. dev->bus and dev->class should
2040 * never change once they are set, so they don't need special care.
2042 drv = READ_ONCE(dev->driver);
2043 return drv ? drv->name : dev_bus_name(dev);
2045 EXPORT_SYMBOL(dev_driver_string);
2047 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
2049 static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
2052 struct device_attribute *dev_attr = to_dev_attr(attr);
2053 struct device *dev = kobj_to_dev(kobj);
2057 ret = dev_attr->show(dev, dev_attr, buf);
2058 if (ret >= (ssize_t)PAGE_SIZE) {
2059 printk("dev_attr_show: %pS returned bad count\n",
2065 static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
2066 const char *buf, size_t count)
2068 struct device_attribute *dev_attr = to_dev_attr(attr);
2069 struct device *dev = kobj_to_dev(kobj);
2072 if (dev_attr->store)
2073 ret = dev_attr->store(dev, dev_attr, buf, count);
2077 static const struct sysfs_ops dev_sysfs_ops = {
2078 .show = dev_attr_show,
2079 .store = dev_attr_store,
2082 #define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
2084 ssize_t device_store_ulong(struct device *dev,
2085 struct device_attribute *attr,
2086 const char *buf, size_t size)
2088 struct dev_ext_attribute *ea = to_ext_attr(attr);
2092 ret = kstrtoul(buf, 0, &new);
2095 *(unsigned long *)(ea->var) = new;
2096 /* Always return full write size even if we didn't consume all */
2099 EXPORT_SYMBOL_GPL(device_store_ulong);
2101 ssize_t device_show_ulong(struct device *dev,
2102 struct device_attribute *attr,
2105 struct dev_ext_attribute *ea = to_ext_attr(attr);
2106 return sysfs_emit(buf, "%lx\n", *(unsigned long *)(ea->var));
2108 EXPORT_SYMBOL_GPL(device_show_ulong);
2110 ssize_t device_store_int(struct device *dev,
2111 struct device_attribute *attr,
2112 const char *buf, size_t size)
2114 struct dev_ext_attribute *ea = to_ext_attr(attr);
2118 ret = kstrtol(buf, 0, &new);
2122 if (new > INT_MAX || new < INT_MIN)
2124 *(int *)(ea->var) = new;
2125 /* Always return full write size even if we didn't consume all */
2128 EXPORT_SYMBOL_GPL(device_store_int);
2130 ssize_t device_show_int(struct device *dev,
2131 struct device_attribute *attr,
2134 struct dev_ext_attribute *ea = to_ext_attr(attr);
2136 return sysfs_emit(buf, "%d\n", *(int *)(ea->var));
2138 EXPORT_SYMBOL_GPL(device_show_int);
2140 ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
2141 const char *buf, size_t size)
2143 struct dev_ext_attribute *ea = to_ext_attr(attr);
2145 if (strtobool(buf, ea->var) < 0)
2150 EXPORT_SYMBOL_GPL(device_store_bool);
2152 ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
2155 struct dev_ext_attribute *ea = to_ext_attr(attr);
2157 return sysfs_emit(buf, "%d\n", *(bool *)(ea->var));
2159 EXPORT_SYMBOL_GPL(device_show_bool);
2162 * device_release - free device structure.
2163 * @kobj: device's kobject.
2165 * This is called once the reference count for the object
2166 * reaches 0. We forward the call to the device's release
2167 * method, which should handle actually freeing the structure.
2169 static void device_release(struct kobject *kobj)
2171 struct device *dev = kobj_to_dev(kobj);
2172 struct device_private *p = dev->p;
2175 * Some platform devices are driven without driver attached
2176 * and managed resources may have been acquired. Make sure
2177 * all resources are released.
2179 * Drivers still can add resources into device after device
2180 * is deleted but alive, so release devres here to avoid
2181 * possible memory leak.
2183 devres_release_all(dev);
2185 kfree(dev->dma_range_map);
2189 else if (dev->type && dev->type->release)
2190 dev->type->release(dev);
2191 else if (dev->class && dev->class->dev_release)
2192 dev->class->dev_release(dev);
2194 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",
2199 static const void *device_namespace(struct kobject *kobj)
2201 struct device *dev = kobj_to_dev(kobj);
2202 const void *ns = NULL;
2204 if (dev->class && dev->class->ns_type)
2205 ns = dev->class->namespace(dev);
2210 static void device_get_ownership(struct kobject *kobj, kuid_t *uid, kgid_t *gid)
2212 struct device *dev = kobj_to_dev(kobj);
2214 if (dev->class && dev->class->get_ownership)
2215 dev->class->get_ownership(dev, uid, gid);
2218 static struct kobj_type device_ktype = {
2219 .release = device_release,
2220 .sysfs_ops = &dev_sysfs_ops,
2221 .namespace = device_namespace,
2222 .get_ownership = device_get_ownership,
2226 static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
2228 struct kobj_type *ktype = get_ktype(kobj);
2230 if (ktype == &device_ktype) {
2231 struct device *dev = kobj_to_dev(kobj);
2240 static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
2242 struct device *dev = kobj_to_dev(kobj);
2245 return dev->bus->name;
2247 return dev->class->name;
2251 static int dev_uevent(struct kset *kset, struct kobject *kobj,
2252 struct kobj_uevent_env *env)
2254 struct device *dev = kobj_to_dev(kobj);
2257 /* add device node properties if present */
2258 if (MAJOR(dev->devt)) {
2262 kuid_t uid = GLOBAL_ROOT_UID;
2263 kgid_t gid = GLOBAL_ROOT_GID;
2265 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
2266 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
2267 name = device_get_devnode(dev, &mode, &uid, &gid, &tmp);
2269 add_uevent_var(env, "DEVNAME=%s", name);
2271 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
2272 if (!uid_eq(uid, GLOBAL_ROOT_UID))
2273 add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid));
2274 if (!gid_eq(gid, GLOBAL_ROOT_GID))
2275 add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid));
2280 if (dev->type && dev->type->name)
2281 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
2284 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
2286 /* Add common DT information about the device */
2287 of_device_uevent(dev, env);
2289 /* have the bus specific function add its stuff */
2290 if (dev->bus && dev->bus->uevent) {
2291 retval = dev->bus->uevent(dev, env);
2293 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
2294 dev_name(dev), __func__, retval);
2297 /* have the class specific function add its stuff */
2298 if (dev->class && dev->class->dev_uevent) {
2299 retval = dev->class->dev_uevent(dev, env);
2301 pr_debug("device: '%s': %s: class uevent() "
2302 "returned %d\n", dev_name(dev),
2306 /* have the device type specific function add its stuff */
2307 if (dev->type && dev->type->uevent) {
2308 retval = dev->type->uevent(dev, env);
2310 pr_debug("device: '%s': %s: dev_type uevent() "
2311 "returned %d\n", dev_name(dev),
2318 static const struct kset_uevent_ops device_uevent_ops = {
2319 .filter = dev_uevent_filter,
2320 .name = dev_uevent_name,
2321 .uevent = dev_uevent,
2324 static ssize_t uevent_show(struct device *dev, struct device_attribute *attr,
2327 struct kobject *top_kobj;
2329 struct kobj_uevent_env *env = NULL;
2334 /* search the kset, the device belongs to */
2335 top_kobj = &dev->kobj;
2336 while (!top_kobj->kset && top_kobj->parent)
2337 top_kobj = top_kobj->parent;
2338 if (!top_kobj->kset)
2341 kset = top_kobj->kset;
2342 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
2345 /* respect filter */
2346 if (kset->uevent_ops && kset->uevent_ops->filter)
2347 if (!kset->uevent_ops->filter(kset, &dev->kobj))
2350 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
2354 /* let the kset specific function add its keys */
2355 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
2359 /* copy keys to file */
2360 for (i = 0; i < env->envp_idx; i++)
2361 len += sysfs_emit_at(buf, len, "%s\n", env->envp[i]);
2367 static ssize_t uevent_store(struct device *dev, struct device_attribute *attr,
2368 const char *buf, size_t count)
2372 rc = kobject_synth_uevent(&dev->kobj, buf, count);
2375 dev_err(dev, "uevent: failed to send synthetic uevent\n");
2381 static DEVICE_ATTR_RW(uevent);
2383 static ssize_t online_show(struct device *dev, struct device_attribute *attr,
2389 val = !dev->offline;
2391 return sysfs_emit(buf, "%u\n", val);
2394 static ssize_t online_store(struct device *dev, struct device_attribute *attr,
2395 const char *buf, size_t count)
2400 ret = strtobool(buf, &val);
2404 ret = lock_device_hotplug_sysfs();
2408 ret = val ? device_online(dev) : device_offline(dev);
2409 unlock_device_hotplug();
2410 return ret < 0 ? ret : count;
2412 static DEVICE_ATTR_RW(online);
2414 static ssize_t removable_show(struct device *dev, struct device_attribute *attr,
2419 switch (dev->removable) {
2420 case DEVICE_REMOVABLE:
2429 return sysfs_emit(buf, "%s\n", loc);
2431 static DEVICE_ATTR_RO(removable);
2433 int device_add_groups(struct device *dev, const struct attribute_group **groups)
2435 return sysfs_create_groups(&dev->kobj, groups);
2437 EXPORT_SYMBOL_GPL(device_add_groups);
2439 void device_remove_groups(struct device *dev,
2440 const struct attribute_group **groups)
2442 sysfs_remove_groups(&dev->kobj, groups);
2444 EXPORT_SYMBOL_GPL(device_remove_groups);
2446 union device_attr_group_devres {
2447 const struct attribute_group *group;
2448 const struct attribute_group **groups;
2451 static int devm_attr_group_match(struct device *dev, void *res, void *data)
2453 return ((union device_attr_group_devres *)res)->group == data;
2456 static void devm_attr_group_remove(struct device *dev, void *res)
2458 union device_attr_group_devres *devres = res;
2459 const struct attribute_group *group = devres->group;
2461 dev_dbg(dev, "%s: removing group %p\n", __func__, group);
2462 sysfs_remove_group(&dev->kobj, group);
2465 static void devm_attr_groups_remove(struct device *dev, void *res)
2467 union device_attr_group_devres *devres = res;
2468 const struct attribute_group **groups = devres->groups;
2470 dev_dbg(dev, "%s: removing groups %p\n", __func__, groups);
2471 sysfs_remove_groups(&dev->kobj, groups);
2475 * devm_device_add_group - given a device, create a managed attribute group
2476 * @dev: The device to create the group for
2477 * @grp: The attribute group to create
2479 * This function creates a group for the first time. It will explicitly
2480 * warn and error if any of the attribute files being created already exist.
2482 * Returns 0 on success or error code on failure.
2484 int devm_device_add_group(struct device *dev, const struct attribute_group *grp)
2486 union device_attr_group_devres *devres;
2489 devres = devres_alloc(devm_attr_group_remove,
2490 sizeof(*devres), GFP_KERNEL);
2494 error = sysfs_create_group(&dev->kobj, grp);
2496 devres_free(devres);
2500 devres->group = grp;
2501 devres_add(dev, devres);
2504 EXPORT_SYMBOL_GPL(devm_device_add_group);
2507 * devm_device_remove_group: remove a managed group from a device
2508 * @dev: device to remove the group from
2509 * @grp: group to remove
2511 * This function removes a group of attributes from a device. The attributes
2512 * previously have to have been created for this group, otherwise it will fail.
2514 void devm_device_remove_group(struct device *dev,
2515 const struct attribute_group *grp)
2517 WARN_ON(devres_release(dev, devm_attr_group_remove,
2518 devm_attr_group_match,
2519 /* cast away const */ (void *)grp));
2521 EXPORT_SYMBOL_GPL(devm_device_remove_group);
2524 * devm_device_add_groups - create a bunch of managed attribute groups
2525 * @dev: The device to create the group for
2526 * @groups: The attribute groups to create, NULL terminated
2528 * This function creates a bunch of managed attribute groups. If an error
2529 * occurs when creating a group, all previously created groups will be
2530 * removed, unwinding everything back to the original state when this
2531 * function was called. It will explicitly warn and error if any of the
2532 * attribute files being created already exist.
2534 * Returns 0 on success or error code from sysfs_create_group on failure.
2536 int devm_device_add_groups(struct device *dev,
2537 const struct attribute_group **groups)
2539 union device_attr_group_devres *devres;
2542 devres = devres_alloc(devm_attr_groups_remove,
2543 sizeof(*devres), GFP_KERNEL);
2547 error = sysfs_create_groups(&dev->kobj, groups);
2549 devres_free(devres);
2553 devres->groups = groups;
2554 devres_add(dev, devres);
2557 EXPORT_SYMBOL_GPL(devm_device_add_groups);
2560 * devm_device_remove_groups - remove a list of managed groups
2562 * @dev: The device for the groups to be removed from
2563 * @groups: NULL terminated list of groups to be removed
2565 * If groups is not NULL, remove the specified groups from the device.
2567 void devm_device_remove_groups(struct device *dev,
2568 const struct attribute_group **groups)
2570 WARN_ON(devres_release(dev, devm_attr_groups_remove,
2571 devm_attr_group_match,
2572 /* cast away const */ (void *)groups));
2574 EXPORT_SYMBOL_GPL(devm_device_remove_groups);
2576 static int device_add_attrs(struct device *dev)
2578 struct class *class = dev->class;
2579 const struct device_type *type = dev->type;
2583 error = device_add_groups(dev, class->dev_groups);
2589 error = device_add_groups(dev, type->groups);
2591 goto err_remove_class_groups;
2594 error = device_add_groups(dev, dev->groups);
2596 goto err_remove_type_groups;
2598 if (device_supports_offline(dev) && !dev->offline_disabled) {
2599 error = device_create_file(dev, &dev_attr_online);
2601 goto err_remove_dev_groups;
2604 if (fw_devlink_flags && !fw_devlink_is_permissive() && dev->fwnode) {
2605 error = device_create_file(dev, &dev_attr_waiting_for_supplier);
2607 goto err_remove_dev_online;
2610 if (dev_removable_is_valid(dev)) {
2611 error = device_create_file(dev, &dev_attr_removable);
2613 goto err_remove_dev_waiting_for_supplier;
2618 err_remove_dev_waiting_for_supplier:
2619 device_remove_file(dev, &dev_attr_waiting_for_supplier);
2620 err_remove_dev_online:
2621 device_remove_file(dev, &dev_attr_online);
2622 err_remove_dev_groups:
2623 device_remove_groups(dev, dev->groups);
2624 err_remove_type_groups:
2626 device_remove_groups(dev, type->groups);
2627 err_remove_class_groups:
2629 device_remove_groups(dev, class->dev_groups);
2634 static void device_remove_attrs(struct device *dev)
2636 struct class *class = dev->class;
2637 const struct device_type *type = dev->type;
2639 device_remove_file(dev, &dev_attr_removable);
2640 device_remove_file(dev, &dev_attr_waiting_for_supplier);
2641 device_remove_file(dev, &dev_attr_online);
2642 device_remove_groups(dev, dev->groups);
2645 device_remove_groups(dev, type->groups);
2648 device_remove_groups(dev, class->dev_groups);
2651 static ssize_t dev_show(struct device *dev, struct device_attribute *attr,
2654 return print_dev_t(buf, dev->devt);
2656 static DEVICE_ATTR_RO(dev);
2659 struct kset *devices_kset;
2662 * devices_kset_move_before - Move device in the devices_kset's list.
2663 * @deva: Device to move.
2664 * @devb: Device @deva should come before.
2666 static void devices_kset_move_before(struct device *deva, struct device *devb)
2670 pr_debug("devices_kset: Moving %s before %s\n",
2671 dev_name(deva), dev_name(devb));
2672 spin_lock(&devices_kset->list_lock);
2673 list_move_tail(&deva->kobj.entry, &devb->kobj.entry);
2674 spin_unlock(&devices_kset->list_lock);
2678 * devices_kset_move_after - Move device in the devices_kset's list.
2679 * @deva: Device to move
2680 * @devb: Device @deva should come after.
2682 static void devices_kset_move_after(struct device *deva, struct device *devb)
2686 pr_debug("devices_kset: Moving %s after %s\n",
2687 dev_name(deva), dev_name(devb));
2688 spin_lock(&devices_kset->list_lock);
2689 list_move(&deva->kobj.entry, &devb->kobj.entry);
2690 spin_unlock(&devices_kset->list_lock);
2694 * devices_kset_move_last - move the device to the end of devices_kset's list.
2695 * @dev: device to move
2697 void devices_kset_move_last(struct device *dev)
2701 pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev));
2702 spin_lock(&devices_kset->list_lock);
2703 list_move_tail(&dev->kobj.entry, &devices_kset->list);
2704 spin_unlock(&devices_kset->list_lock);
2708 * device_create_file - create sysfs attribute file for device.
2710 * @attr: device attribute descriptor.
2712 int device_create_file(struct device *dev,
2713 const struct device_attribute *attr)
2718 WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
2719 "Attribute %s: write permission without 'store'\n",
2721 WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
2722 "Attribute %s: read permission without 'show'\n",
2724 error = sysfs_create_file(&dev->kobj, &attr->attr);
2729 EXPORT_SYMBOL_GPL(device_create_file);
2732 * device_remove_file - remove sysfs attribute file.
2734 * @attr: device attribute descriptor.
2736 void device_remove_file(struct device *dev,
2737 const struct device_attribute *attr)
2740 sysfs_remove_file(&dev->kobj, &attr->attr);
2742 EXPORT_SYMBOL_GPL(device_remove_file);
2745 * device_remove_file_self - remove sysfs attribute file from its own method.
2747 * @attr: device attribute descriptor.
2749 * See kernfs_remove_self() for details.
2751 bool device_remove_file_self(struct device *dev,
2752 const struct device_attribute *attr)
2755 return sysfs_remove_file_self(&dev->kobj, &attr->attr);
2759 EXPORT_SYMBOL_GPL(device_remove_file_self);
2762 * device_create_bin_file - create sysfs binary attribute file for device.
2764 * @attr: device binary attribute descriptor.
2766 int device_create_bin_file(struct device *dev,
2767 const struct bin_attribute *attr)
2769 int error = -EINVAL;
2771 error = sysfs_create_bin_file(&dev->kobj, attr);
2774 EXPORT_SYMBOL_GPL(device_create_bin_file);
2777 * device_remove_bin_file - remove sysfs binary attribute file
2779 * @attr: device binary attribute descriptor.
2781 void device_remove_bin_file(struct device *dev,
2782 const struct bin_attribute *attr)
2785 sysfs_remove_bin_file(&dev->kobj, attr);
2787 EXPORT_SYMBOL_GPL(device_remove_bin_file);
2789 static void klist_children_get(struct klist_node *n)
2791 struct device_private *p = to_device_private_parent(n);
2792 struct device *dev = p->device;
2797 static void klist_children_put(struct klist_node *n)
2799 struct device_private *p = to_device_private_parent(n);
2800 struct device *dev = p->device;
2806 * device_initialize - init device structure.
2809 * This prepares the device for use by other layers by initializing
2811 * It is the first half of device_register(), if called by
2812 * that function, though it can also be called separately, so one
2813 * may use @dev's fields. In particular, get_device()/put_device()
2814 * may be used for reference counting of @dev after calling this
2817 * All fields in @dev must be initialized by the caller to 0, except
2818 * for those explicitly set to some other value. The simplest
2819 * approach is to use kzalloc() to allocate the structure containing
2822 * NOTE: Use put_device() to give up your reference instead of freeing
2823 * @dev directly once you have called this function.
2825 void device_initialize(struct device *dev)
2827 dev->kobj.kset = devices_kset;
2828 kobject_init(&dev->kobj, &device_ktype);
2829 INIT_LIST_HEAD(&dev->dma_pools);
2830 mutex_init(&dev->mutex);
2831 #ifdef CONFIG_PROVE_LOCKING
2832 mutex_init(&dev->lockdep_mutex);
2834 lockdep_set_novalidate_class(&dev->mutex);
2835 spin_lock_init(&dev->devres_lock);
2836 INIT_LIST_HEAD(&dev->devres_head);
2837 device_pm_init(dev);
2838 set_dev_node(dev, -1);
2839 #ifdef CONFIG_GENERIC_MSI_IRQ
2840 INIT_LIST_HEAD(&dev->msi_list);
2842 INIT_LIST_HEAD(&dev->links.consumers);
2843 INIT_LIST_HEAD(&dev->links.suppliers);
2844 INIT_LIST_HEAD(&dev->links.defer_sync);
2845 dev->links.status = DL_DEV_NO_DRIVER;
2846 #if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE) || \
2847 defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU) || \
2848 defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL)
2849 dev->dma_coherent = dma_default_coherent;
2852 EXPORT_SYMBOL_GPL(device_initialize);
2854 struct kobject *virtual_device_parent(struct device *dev)
2856 static struct kobject *virtual_dir = NULL;
2859 virtual_dir = kobject_create_and_add("virtual",
2860 &devices_kset->kobj);
2866 struct kobject kobj;
2867 struct class *class;
2870 #define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
2872 static void class_dir_release(struct kobject *kobj)
2874 struct class_dir *dir = to_class_dir(kobj);
2879 struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
2881 struct class_dir *dir = to_class_dir(kobj);
2882 return dir->class->ns_type;
2885 static struct kobj_type class_dir_ktype = {
2886 .release = class_dir_release,
2887 .sysfs_ops = &kobj_sysfs_ops,
2888 .child_ns_type = class_dir_child_ns_type
2891 static struct kobject *
2892 class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
2894 struct class_dir *dir;
2897 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
2899 return ERR_PTR(-ENOMEM);
2902 kobject_init(&dir->kobj, &class_dir_ktype);
2904 dir->kobj.kset = &class->p->glue_dirs;
2906 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
2908 kobject_put(&dir->kobj);
2909 return ERR_PTR(retval);
2914 static DEFINE_MUTEX(gdp_mutex);
2916 static struct kobject *get_device_parent(struct device *dev,
2917 struct device *parent)
2920 struct kobject *kobj = NULL;
2921 struct kobject *parent_kobj;
2925 /* block disks show up in /sys/block */
2926 if (sysfs_deprecated && dev->class == &block_class) {
2927 if (parent && parent->class == &block_class)
2928 return &parent->kobj;
2929 return &block_class.p->subsys.kobj;
2934 * If we have no parent, we live in "virtual".
2935 * Class-devices with a non class-device as parent, live
2936 * in a "glue" directory to prevent namespace collisions.
2939 parent_kobj = virtual_device_parent(dev);
2940 else if (parent->class && !dev->class->ns_type)
2941 return &parent->kobj;
2943 parent_kobj = &parent->kobj;
2945 mutex_lock(&gdp_mutex);
2947 /* find our class-directory at the parent and reference it */
2948 spin_lock(&dev->class->p->glue_dirs.list_lock);
2949 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
2950 if (k->parent == parent_kobj) {
2951 kobj = kobject_get(k);
2954 spin_unlock(&dev->class->p->glue_dirs.list_lock);
2956 mutex_unlock(&gdp_mutex);
2960 /* or create a new class-directory at the parent device */
2961 k = class_dir_create_and_add(dev->class, parent_kobj);
2962 /* do not emit an uevent for this simple "glue" directory */
2963 mutex_unlock(&gdp_mutex);
2967 /* subsystems can specify a default root directory for their devices */
2968 if (!parent && dev->bus && dev->bus->dev_root)
2969 return &dev->bus->dev_root->kobj;
2972 return &parent->kobj;
2976 static inline bool live_in_glue_dir(struct kobject *kobj,
2979 if (!kobj || !dev->class ||
2980 kobj->kset != &dev->class->p->glue_dirs)
2985 static inline struct kobject *get_glue_dir(struct device *dev)
2987 return dev->kobj.parent;
2991 * make sure cleaning up dir as the last step, we need to make
2992 * sure .release handler of kobject is run with holding the
2995 static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
2999 /* see if we live in a "glue" directory */
3000 if (!live_in_glue_dir(glue_dir, dev))
3003 mutex_lock(&gdp_mutex);
3005 * There is a race condition between removing glue directory
3006 * and adding a new device under the glue directory.
3011 * get_device_parent()
3012 * class_dir_create_and_add()
3013 * kobject_add_internal()
3014 * create_dir() // create glue_dir
3017 * get_device_parent()
3018 * kobject_get() // get glue_dir
3021 * cleanup_glue_dir()
3022 * kobject_del(glue_dir)
3025 * kobject_add_internal()
3026 * create_dir() // in glue_dir
3027 * sysfs_create_dir_ns()
3028 * kernfs_create_dir_ns(sd)
3030 * sysfs_remove_dir() // glue_dir->sd=NULL
3031 * sysfs_put() // free glue_dir->sd
3034 * kernfs_new_node(sd)
3035 * kernfs_get(glue_dir)
3039 * Before CPU1 remove last child device under glue dir, if CPU2 add
3040 * a new device under glue dir, the glue_dir kobject reference count
3041 * will be increase to 2 in kobject_get(k). And CPU2 has been called
3042 * kernfs_create_dir_ns(). Meanwhile, CPU1 call sysfs_remove_dir()
3043 * and sysfs_put(). This result in glue_dir->sd is freed.
3045 * Then the CPU2 will see a stale "empty" but still potentially used
3046 * glue dir around in kernfs_new_node().
3048 * In order to avoid this happening, we also should make sure that
3049 * kernfs_node for glue_dir is released in CPU1 only when refcount
3050 * for glue_dir kobj is 1.
3052 ref = kref_read(&glue_dir->kref);
3053 if (!kobject_has_children(glue_dir) && !--ref)
3054 kobject_del(glue_dir);
3055 kobject_put(glue_dir);
3056 mutex_unlock(&gdp_mutex);
3059 static int device_add_class_symlinks(struct device *dev)
3061 struct device_node *of_node = dev_of_node(dev);
3065 error = sysfs_create_link(&dev->kobj, of_node_kobj(of_node), "of_node");
3067 dev_warn(dev, "Error %d creating of_node link\n",error);
3068 /* An error here doesn't warrant bringing down the device */
3074 error = sysfs_create_link(&dev->kobj,
3075 &dev->class->p->subsys.kobj,
3080 if (dev->parent && device_is_not_partition(dev)) {
3081 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
3088 /* /sys/block has directories and does not need symlinks */
3089 if (sysfs_deprecated && dev->class == &block_class)
3093 /* link in the class directory pointing to the device */
3094 error = sysfs_create_link(&dev->class->p->subsys.kobj,
3095 &dev->kobj, dev_name(dev));
3102 sysfs_remove_link(&dev->kobj, "device");
3105 sysfs_remove_link(&dev->kobj, "subsystem");
3107 sysfs_remove_link(&dev->kobj, "of_node");
3111 static void device_remove_class_symlinks(struct device *dev)
3113 if (dev_of_node(dev))
3114 sysfs_remove_link(&dev->kobj, "of_node");
3119 if (dev->parent && device_is_not_partition(dev))
3120 sysfs_remove_link(&dev->kobj, "device");
3121 sysfs_remove_link(&dev->kobj, "subsystem");
3123 if (sysfs_deprecated && dev->class == &block_class)
3126 sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
3130 * dev_set_name - set a device name
3132 * @fmt: format string for the device's name
3134 int dev_set_name(struct device *dev, const char *fmt, ...)
3139 va_start(vargs, fmt);
3140 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
3144 EXPORT_SYMBOL_GPL(dev_set_name);
3147 * device_to_dev_kobj - select a /sys/dev/ directory for the device
3150 * By default we select char/ for new entries. Setting class->dev_obj
3151 * to NULL prevents an entry from being created. class->dev_kobj must
3152 * be set (or cleared) before any devices are registered to the class
3153 * otherwise device_create_sys_dev_entry() and
3154 * device_remove_sys_dev_entry() will disagree about the presence of
3157 static struct kobject *device_to_dev_kobj(struct device *dev)
3159 struct kobject *kobj;
3162 kobj = dev->class->dev_kobj;
3164 kobj = sysfs_dev_char_kobj;
3169 static int device_create_sys_dev_entry(struct device *dev)
3171 struct kobject *kobj = device_to_dev_kobj(dev);
3176 format_dev_t(devt_str, dev->devt);
3177 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
3183 static void device_remove_sys_dev_entry(struct device *dev)
3185 struct kobject *kobj = device_to_dev_kobj(dev);
3189 format_dev_t(devt_str, dev->devt);
3190 sysfs_remove_link(kobj, devt_str);
3194 static int device_private_init(struct device *dev)
3196 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
3199 dev->p->device = dev;
3200 klist_init(&dev->p->klist_children, klist_children_get,
3201 klist_children_put);
3202 INIT_LIST_HEAD(&dev->p->deferred_probe);
3207 * device_add - add device to device hierarchy.
3210 * This is part 2 of device_register(), though may be called
3211 * separately _iff_ device_initialize() has been called separately.
3213 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
3214 * to the global and sibling lists for the device, then
3215 * adds it to the other relevant subsystems of the driver model.
3217 * Do not call this routine or device_register() more than once for
3218 * any device structure. The driver model core is not designed to work
3219 * with devices that get unregistered and then spring back to life.
3220 * (Among other things, it's very hard to guarantee that all references
3221 * to the previous incarnation of @dev have been dropped.) Allocate
3222 * and register a fresh new struct device instead.
3224 * NOTE: _Never_ directly free @dev after calling this function, even
3225 * if it returned an error! Always use put_device() to give up your
3226 * reference instead.
3228 * Rule of thumb is: if device_add() succeeds, you should call
3229 * device_del() when you want to get rid of it. If device_add() has
3230 * *not* succeeded, use *only* put_device() to drop the reference
3233 int device_add(struct device *dev)
3235 struct device *parent;
3236 struct kobject *kobj;
3237 struct class_interface *class_intf;
3238 int error = -EINVAL;
3239 struct kobject *glue_dir = NULL;
3241 dev = get_device(dev);
3246 error = device_private_init(dev);
3252 * for statically allocated devices, which should all be converted
3253 * some day, we need to initialize the name. We prevent reading back
3254 * the name, and force the use of dev_name()
3256 if (dev->init_name) {
3257 dev_set_name(dev, "%s", dev->init_name);
3258 dev->init_name = NULL;
3261 /* subsystems can specify simple device enumeration */
3262 if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
3263 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
3265 if (!dev_name(dev)) {
3270 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
3272 parent = get_device(dev->parent);
3273 kobj = get_device_parent(dev, parent);
3275 error = PTR_ERR(kobj);
3279 dev->kobj.parent = kobj;
3281 /* use parent numa_node */
3282 if (parent && (dev_to_node(dev) == NUMA_NO_NODE))
3283 set_dev_node(dev, dev_to_node(parent));
3285 /* first, register with generic layer. */
3286 /* we require the name to be set before, and pass NULL */
3287 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
3289 glue_dir = get_glue_dir(dev);
3293 /* notify platform of device entry */
3294 error = device_platform_notify(dev, KOBJ_ADD);
3296 goto platform_error;
3298 error = device_create_file(dev, &dev_attr_uevent);
3302 error = device_add_class_symlinks(dev);
3305 error = device_add_attrs(dev);
3308 error = bus_add_device(dev);
3311 error = dpm_sysfs_add(dev);
3316 if (MAJOR(dev->devt)) {
3317 error = device_create_file(dev, &dev_attr_dev);
3321 error = device_create_sys_dev_entry(dev);
3325 devtmpfs_create_node(dev);
3328 /* Notify clients of device addition. This call must come
3329 * after dpm_sysfs_add() and before kobject_uevent().
3332 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
3333 BUS_NOTIFY_ADD_DEVICE, dev);
3335 kobject_uevent(&dev->kobj, KOBJ_ADD);
3338 * Check if any of the other devices (consumers) have been waiting for
3339 * this device (supplier) to be added so that they can create a device
3342 * This needs to happen after device_pm_add() because device_link_add()
3343 * requires the supplier be registered before it's called.
3345 * But this also needs to happen before bus_probe_device() to make sure
3346 * waiting consumers can link to it before the driver is bound to the
3347 * device and the driver sync_state callback is called for this device.
3349 if (dev->fwnode && !dev->fwnode->dev) {
3350 dev->fwnode->dev = dev;
3351 fw_devlink_link_device(dev);
3354 bus_probe_device(dev);
3357 * If all driver registration is done and a newly added device doesn't
3358 * match with any driver, don't block its consumers from probing in
3359 * case the consumer device is able to operate without this supplier.
3361 if (dev->fwnode && fw_devlink_drv_reg_done && !dev->can_match)
3362 fw_devlink_unblock_consumers(dev);
3365 klist_add_tail(&dev->p->knode_parent,
3366 &parent->p->klist_children);
3369 mutex_lock(&dev->class->p->mutex);
3370 /* tie the class to the device */
3371 klist_add_tail(&dev->p->knode_class,
3372 &dev->class->p->klist_devices);
3374 /* notify any interfaces that the device is here */
3375 list_for_each_entry(class_intf,
3376 &dev->class->p->interfaces, node)
3377 if (class_intf->add_dev)
3378 class_intf->add_dev(dev, class_intf);
3379 mutex_unlock(&dev->class->p->mutex);
3385 if (MAJOR(dev->devt))
3386 device_remove_file(dev, &dev_attr_dev);
3388 device_pm_remove(dev);
3389 dpm_sysfs_remove(dev);
3391 bus_remove_device(dev);
3393 device_remove_attrs(dev);
3395 device_remove_class_symlinks(dev);
3397 device_remove_file(dev, &dev_attr_uevent);
3399 device_platform_notify(dev, KOBJ_REMOVE);
3401 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
3402 glue_dir = get_glue_dir(dev);
3403 kobject_del(&dev->kobj);
3405 cleanup_glue_dir(dev, glue_dir);
3413 EXPORT_SYMBOL_GPL(device_add);
3416 * device_register - register a device with the system.
3417 * @dev: pointer to the device structure
3419 * This happens in two clean steps - initialize the device
3420 * and add it to the system. The two steps can be called
3421 * separately, but this is the easiest and most common.
3422 * I.e. you should only call the two helpers separately if
3423 * have a clearly defined need to use and refcount the device
3424 * before it is added to the hierarchy.
3426 * For more information, see the kerneldoc for device_initialize()
3429 * NOTE: _Never_ directly free @dev after calling this function, even
3430 * if it returned an error! Always use put_device() to give up the
3431 * reference initialized in this function instead.
3433 int device_register(struct device *dev)
3435 device_initialize(dev);
3436 return device_add(dev);
3438 EXPORT_SYMBOL_GPL(device_register);
3441 * get_device - increment reference count for device.
3444 * This simply forwards the call to kobject_get(), though
3445 * we do take care to provide for the case that we get a NULL
3446 * pointer passed in.
3448 struct device *get_device(struct device *dev)
3450 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
3452 EXPORT_SYMBOL_GPL(get_device);
3455 * put_device - decrement reference count.
3456 * @dev: device in question.
3458 void put_device(struct device *dev)
3460 /* might_sleep(); */
3462 kobject_put(&dev->kobj);
3464 EXPORT_SYMBOL_GPL(put_device);
3466 bool kill_device(struct device *dev)
3469 * Require the device lock and set the "dead" flag to guarantee that
3470 * the update behavior is consistent with the other bitfields near
3471 * it and that we cannot have an asynchronous probe routine trying
3472 * to run while we are tearing out the bus/class/sysfs from
3473 * underneath the device.
3475 device_lock_assert(dev);
3479 dev->p->dead = true;
3482 EXPORT_SYMBOL_GPL(kill_device);
3485 * device_del - delete device from system.
3488 * This is the first part of the device unregistration
3489 * sequence. This removes the device from the lists we control
3490 * from here, has it removed from the other driver model
3491 * subsystems it was added to in device_add(), and removes it
3492 * from the kobject hierarchy.
3494 * NOTE: this should be called manually _iff_ device_add() was
3495 * also called manually.
3497 void device_del(struct device *dev)
3499 struct device *parent = dev->parent;
3500 struct kobject *glue_dir = NULL;
3501 struct class_interface *class_intf;
3502 unsigned int noio_flag;
3508 if (dev->fwnode && dev->fwnode->dev == dev)
3509 dev->fwnode->dev = NULL;
3511 /* Notify clients of device removal. This call must come
3512 * before dpm_sysfs_remove().
3514 noio_flag = memalloc_noio_save();
3516 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
3517 BUS_NOTIFY_DEL_DEVICE, dev);
3519 dpm_sysfs_remove(dev);
3521 klist_del(&dev->p->knode_parent);
3522 if (MAJOR(dev->devt)) {
3523 devtmpfs_delete_node(dev);
3524 device_remove_sys_dev_entry(dev);
3525 device_remove_file(dev, &dev_attr_dev);
3528 device_remove_class_symlinks(dev);
3530 mutex_lock(&dev->class->p->mutex);
3531 /* notify any interfaces that the device is now gone */
3532 list_for_each_entry(class_intf,
3533 &dev->class->p->interfaces, node)
3534 if (class_intf->remove_dev)
3535 class_intf->remove_dev(dev, class_intf);
3536 /* remove the device from the class list */
3537 klist_del(&dev->p->knode_class);
3538 mutex_unlock(&dev->class->p->mutex);
3540 device_remove_file(dev, &dev_attr_uevent);
3541 device_remove_attrs(dev);
3542 bus_remove_device(dev);
3543 device_pm_remove(dev);
3544 driver_deferred_probe_del(dev);
3545 device_platform_notify(dev, KOBJ_REMOVE);
3546 device_remove_properties(dev);
3547 device_links_purge(dev);
3550 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
3551 BUS_NOTIFY_REMOVED_DEVICE, dev);
3552 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
3553 glue_dir = get_glue_dir(dev);
3554 kobject_del(&dev->kobj);
3555 cleanup_glue_dir(dev, glue_dir);
3556 memalloc_noio_restore(noio_flag);
3559 EXPORT_SYMBOL_GPL(device_del);
3562 * device_unregister - unregister device from system.
3563 * @dev: device going away.
3565 * We do this in two parts, like we do device_register(). First,
3566 * we remove it from all the subsystems with device_del(), then
3567 * we decrement the reference count via put_device(). If that
3568 * is the final reference count, the device will be cleaned up
3569 * via device_release() above. Otherwise, the structure will
3570 * stick around until the final reference to the device is dropped.
3572 void device_unregister(struct device *dev)
3574 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
3578 EXPORT_SYMBOL_GPL(device_unregister);
3580 static struct device *prev_device(struct klist_iter *i)
3582 struct klist_node *n = klist_prev(i);
3583 struct device *dev = NULL;
3584 struct device_private *p;
3587 p = to_device_private_parent(n);
3593 static struct device *next_device(struct klist_iter *i)
3595 struct klist_node *n = klist_next(i);
3596 struct device *dev = NULL;
3597 struct device_private *p;
3600 p = to_device_private_parent(n);
3607 * device_get_devnode - path of device node file
3609 * @mode: returned file access mode
3610 * @uid: returned file owner
3611 * @gid: returned file group
3612 * @tmp: possibly allocated string
3614 * Return the relative path of a possible device node.
3615 * Non-default names may need to allocate a memory to compose
3616 * a name. This memory is returned in tmp and needs to be
3617 * freed by the caller.
3619 const char *device_get_devnode(struct device *dev,
3620 umode_t *mode, kuid_t *uid, kgid_t *gid,
3627 /* the device type may provide a specific name */
3628 if (dev->type && dev->type->devnode)
3629 *tmp = dev->type->devnode(dev, mode, uid, gid);
3633 /* the class may provide a specific name */
3634 if (dev->class && dev->class->devnode)
3635 *tmp = dev->class->devnode(dev, mode);
3639 /* return name without allocation, tmp == NULL */
3640 if (strchr(dev_name(dev), '!') == NULL)
3641 return dev_name(dev);
3643 /* replace '!' in the name with '/' */
3644 s = kstrdup(dev_name(dev), GFP_KERNEL);
3647 strreplace(s, '!', '/');
3652 * device_for_each_child - device child iterator.
3653 * @parent: parent struct device.
3654 * @fn: function to be called for each device.
3655 * @data: data for the callback.
3657 * Iterate over @parent's child devices, and call @fn for each,
3660 * We check the return of @fn each time. If it returns anything
3661 * other than 0, we break out and return that value.
3663 int device_for_each_child(struct device *parent, void *data,
3664 int (*fn)(struct device *dev, void *data))
3666 struct klist_iter i;
3667 struct device *child;
3673 klist_iter_init(&parent->p->klist_children, &i);
3674 while (!error && (child = next_device(&i)))
3675 error = fn(child, data);
3676 klist_iter_exit(&i);
3679 EXPORT_SYMBOL_GPL(device_for_each_child);
3682 * device_for_each_child_reverse - device child iterator in reversed order.
3683 * @parent: parent struct device.
3684 * @fn: function to be called for each device.
3685 * @data: data for the callback.
3687 * Iterate over @parent's child devices, and call @fn for each,
3690 * We check the return of @fn each time. If it returns anything
3691 * other than 0, we break out and return that value.
3693 int device_for_each_child_reverse(struct device *parent, void *data,
3694 int (*fn)(struct device *dev, void *data))
3696 struct klist_iter i;
3697 struct device *child;
3703 klist_iter_init(&parent->p->klist_children, &i);
3704 while ((child = prev_device(&i)) && !error)
3705 error = fn(child, data);
3706 klist_iter_exit(&i);
3709 EXPORT_SYMBOL_GPL(device_for_each_child_reverse);
3712 * device_find_child - device iterator for locating a particular device.
3713 * @parent: parent struct device
3714 * @match: Callback function to check device
3715 * @data: Data to pass to match function
3717 * This is similar to the device_for_each_child() function above, but it
3718 * returns a reference to a device that is 'found' for later use, as
3719 * determined by the @match callback.
3721 * The callback should return 0 if the device doesn't match and non-zero
3722 * if it does. If the callback returns non-zero and a reference to the
3723 * current device can be obtained, this function will return to the caller
3724 * and not iterate over any more devices.
3726 * NOTE: you will need to drop the reference with put_device() after use.
3728 struct device *device_find_child(struct device *parent, void *data,
3729 int (*match)(struct device *dev, void *data))
3731 struct klist_iter i;
3732 struct device *child;
3737 klist_iter_init(&parent->p->klist_children, &i);
3738 while ((child = next_device(&i)))
3739 if (match(child, data) && get_device(child))
3741 klist_iter_exit(&i);
3744 EXPORT_SYMBOL_GPL(device_find_child);
3747 * device_find_child_by_name - device iterator for locating a child device.
3748 * @parent: parent struct device
3749 * @name: name of the child device
3751 * This is similar to the device_find_child() function above, but it
3752 * returns a reference to a device that has the name @name.
3754 * NOTE: you will need to drop the reference with put_device() after use.
3756 struct device *device_find_child_by_name(struct device *parent,
3759 struct klist_iter i;
3760 struct device *child;
3765 klist_iter_init(&parent->p->klist_children, &i);
3766 while ((child = next_device(&i)))
3767 if (sysfs_streq(dev_name(child), name) && get_device(child))
3769 klist_iter_exit(&i);
3772 EXPORT_SYMBOL_GPL(device_find_child_by_name);
3774 int __init devices_init(void)
3776 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
3779 dev_kobj = kobject_create_and_add("dev", NULL);
3782 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
3783 if (!sysfs_dev_block_kobj)
3784 goto block_kobj_err;
3785 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
3786 if (!sysfs_dev_char_kobj)
3792 kobject_put(sysfs_dev_block_kobj);
3794 kobject_put(dev_kobj);
3796 kset_unregister(devices_kset);
3800 static int device_check_offline(struct device *dev, void *not_used)
3804 ret = device_for_each_child(dev, NULL, device_check_offline);
3808 return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
3812 * device_offline - Prepare the device for hot-removal.
3813 * @dev: Device to be put offline.
3815 * Execute the device bus type's .offline() callback, if present, to prepare
3816 * the device for a subsequent hot-removal. If that succeeds, the device must
3817 * not be used until either it is removed or its bus type's .online() callback
3820 * Call under device_hotplug_lock.
3822 int device_offline(struct device *dev)
3826 if (dev->offline_disabled)
3829 ret = device_for_each_child(dev, NULL, device_check_offline);
3834 if (device_supports_offline(dev)) {
3838 ret = dev->bus->offline(dev);
3840 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
3841 dev->offline = true;
3851 * device_online - Put the device back online after successful device_offline().
3852 * @dev: Device to be put back online.
3854 * If device_offline() has been successfully executed for @dev, but the device
3855 * has not been removed subsequently, execute its bus type's .online() callback
3856 * to indicate that the device can be used again.
3858 * Call under device_hotplug_lock.
3860 int device_online(struct device *dev)
3865 if (device_supports_offline(dev)) {
3867 ret = dev->bus->online(dev);
3869 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
3870 dev->offline = false;
3881 struct root_device {
3883 struct module *owner;
3886 static inline struct root_device *to_root_device(struct device *d)
3888 return container_of(d, struct root_device, dev);
3891 static void root_device_release(struct device *dev)
3893 kfree(to_root_device(dev));
3897 * __root_device_register - allocate and register a root device
3898 * @name: root device name
3899 * @owner: owner module of the root device, usually THIS_MODULE
3901 * This function allocates a root device and registers it
3902 * using device_register(). In order to free the returned
3903 * device, use root_device_unregister().
3905 * Root devices are dummy devices which allow other devices
3906 * to be grouped under /sys/devices. Use this function to
3907 * allocate a root device and then use it as the parent of
3908 * any device which should appear under /sys/devices/{name}
3910 * The /sys/devices/{name} directory will also contain a
3911 * 'module' symlink which points to the @owner directory
3914 * Returns &struct device pointer on success, or ERR_PTR() on error.
3916 * Note: You probably want to use root_device_register().
3918 struct device *__root_device_register(const char *name, struct module *owner)
3920 struct root_device *root;
3923 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
3925 return ERR_PTR(err);
3927 err = dev_set_name(&root->dev, "%s", name);
3930 return ERR_PTR(err);
3933 root->dev.release = root_device_release;
3935 err = device_register(&root->dev);
3937 put_device(&root->dev);
3938 return ERR_PTR(err);
3941 #ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
3943 struct module_kobject *mk = &owner->mkobj;
3945 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
3947 device_unregister(&root->dev);
3948 return ERR_PTR(err);
3950 root->owner = owner;
3956 EXPORT_SYMBOL_GPL(__root_device_register);
3959 * root_device_unregister - unregister and free a root device
3960 * @dev: device going away
3962 * This function unregisters and cleans up a device that was created by
3963 * root_device_register().
3965 void root_device_unregister(struct device *dev)
3967 struct root_device *root = to_root_device(dev);
3970 sysfs_remove_link(&root->dev.kobj, "module");
3972 device_unregister(dev);
3974 EXPORT_SYMBOL_GPL(root_device_unregister);
3977 static void device_create_release(struct device *dev)
3979 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
3983 static __printf(6, 0) struct device *
3984 device_create_groups_vargs(struct class *class, struct device *parent,
3985 dev_t devt, void *drvdata,
3986 const struct attribute_group **groups,
3987 const char *fmt, va_list args)
3989 struct device *dev = NULL;
3990 int retval = -ENODEV;
3992 if (class == NULL || IS_ERR(class))
3995 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
4001 device_initialize(dev);
4004 dev->parent = parent;
4005 dev->groups = groups;
4006 dev->release = device_create_release;
4007 dev_set_drvdata(dev, drvdata);
4009 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
4013 retval = device_add(dev);
4021 return ERR_PTR(retval);
4025 * device_create - creates a device and registers it with sysfs
4026 * @class: pointer to the struct class that this device should be registered to
4027 * @parent: pointer to the parent struct device of this new device, if any
4028 * @devt: the dev_t for the char device to be added
4029 * @drvdata: the data to be added to the device for callbacks
4030 * @fmt: string for the device's name
4032 * This function can be used by char device classes. A struct device
4033 * will be created in sysfs, registered to the specified class.
4035 * A "dev" file will be created, showing the dev_t for the device, if
4036 * the dev_t is not 0,0.
4037 * If a pointer to a parent struct device is passed in, the newly created
4038 * struct device will be a child of that device in sysfs.
4039 * The pointer to the struct device will be returned from the call.
4040 * Any further sysfs files that might be required can be created using this
4043 * Returns &struct device pointer on success, or ERR_PTR() on error.
4045 * Note: the struct class passed to this function must have previously
4046 * been created with a call to class_create().
4048 struct device *device_create(struct class *class, struct device *parent,
4049 dev_t devt, void *drvdata, const char *fmt, ...)
4054 va_start(vargs, fmt);
4055 dev = device_create_groups_vargs(class, parent, devt, drvdata, NULL,
4060 EXPORT_SYMBOL_GPL(device_create);
4063 * device_create_with_groups - creates a device and registers it with sysfs
4064 * @class: pointer to the struct class that this device should be registered to
4065 * @parent: pointer to the parent struct device of this new device, if any
4066 * @devt: the dev_t for the char device to be added
4067 * @drvdata: the data to be added to the device for callbacks
4068 * @groups: NULL-terminated list of attribute groups to be created
4069 * @fmt: string for the device's name
4071 * This function can be used by char device classes. A struct device
4072 * will be created in sysfs, registered to the specified class.
4073 * Additional attributes specified in the groups parameter will also
4074 * be created automatically.
4076 * A "dev" file will be created, showing the dev_t for the device, if
4077 * the dev_t is not 0,0.
4078 * If a pointer to a parent struct device is passed in, the newly created
4079 * struct device will be a child of that device in sysfs.
4080 * The pointer to the struct device will be returned from the call.
4081 * Any further sysfs files that might be required can be created using this
4084 * Returns &struct device pointer on success, or ERR_PTR() on error.
4086 * Note: the struct class passed to this function must have previously
4087 * been created with a call to class_create().
4089 struct device *device_create_with_groups(struct class *class,
4090 struct device *parent, dev_t devt,
4092 const struct attribute_group **groups,
4093 const char *fmt, ...)
4098 va_start(vargs, fmt);
4099 dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
4104 EXPORT_SYMBOL_GPL(device_create_with_groups);
4107 * device_destroy - removes a device that was created with device_create()
4108 * @class: pointer to the struct class that this device was registered with
4109 * @devt: the dev_t of the device that was previously registered
4111 * This call unregisters and cleans up a device that was created with a
4112 * call to device_create().
4114 void device_destroy(struct class *class, dev_t devt)
4118 dev = class_find_device_by_devt(class, devt);
4121 device_unregister(dev);
4124 EXPORT_SYMBOL_GPL(device_destroy);
4127 * device_rename - renames a device
4128 * @dev: the pointer to the struct device to be renamed
4129 * @new_name: the new name of the device
4131 * It is the responsibility of the caller to provide mutual
4132 * exclusion between two different calls of device_rename
4133 * on the same device to ensure that new_name is valid and
4134 * won't conflict with other devices.
4136 * Note: Don't call this function. Currently, the networking layer calls this
4137 * function, but that will change. The following text from Kay Sievers offers
4140 * Renaming devices is racy at many levels, symlinks and other stuff are not
4141 * replaced atomically, and you get a "move" uevent, but it's not easy to
4142 * connect the event to the old and new device. Device nodes are not renamed at
4143 * all, there isn't even support for that in the kernel now.
4145 * In the meantime, during renaming, your target name might be taken by another
4146 * driver, creating conflicts. Or the old name is taken directly after you
4147 * renamed it -- then you get events for the same DEVPATH, before you even see
4148 * the "move" event. It's just a mess, and nothing new should ever rely on
4149 * kernel device renaming. Besides that, it's not even implemented now for
4150 * other things than (driver-core wise very simple) network devices.
4152 * We are currently about to change network renaming in udev to completely
4153 * disallow renaming of devices in the same namespace as the kernel uses,
4154 * because we can't solve the problems properly, that arise with swapping names
4155 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
4156 * be allowed to some other name than eth[0-9]*, for the aforementioned
4159 * Make up a "real" name in the driver before you register anything, or add
4160 * some other attributes for userspace to find the device, or use udev to add
4161 * symlinks -- but never rename kernel devices later, it's a complete mess. We
4162 * don't even want to get into that and try to implement the missing pieces in
4163 * the core. We really have other pieces to fix in the driver core mess. :)
4165 int device_rename(struct device *dev, const char *new_name)
4167 struct kobject *kobj = &dev->kobj;
4168 char *old_device_name = NULL;
4171 dev = get_device(dev);
4175 dev_dbg(dev, "renaming to %s\n", new_name);
4177 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
4178 if (!old_device_name) {
4184 error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj,
4185 kobj, old_device_name,
4186 new_name, kobject_namespace(kobj));
4191 error = kobject_rename(kobj, new_name);
4198 kfree(old_device_name);
4202 EXPORT_SYMBOL_GPL(device_rename);
4204 static int device_move_class_links(struct device *dev,
4205 struct device *old_parent,
4206 struct device *new_parent)
4211 sysfs_remove_link(&dev->kobj, "device");
4213 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
4219 * device_move - moves a device to a new parent
4220 * @dev: the pointer to the struct device to be moved
4221 * @new_parent: the new parent of the device (can be NULL)
4222 * @dpm_order: how to reorder the dpm_list
4224 int device_move(struct device *dev, struct device *new_parent,
4225 enum dpm_order dpm_order)
4228 struct device *old_parent;
4229 struct kobject *new_parent_kobj;
4231 dev = get_device(dev);
4236 new_parent = get_device(new_parent);
4237 new_parent_kobj = get_device_parent(dev, new_parent);
4238 if (IS_ERR(new_parent_kobj)) {
4239 error = PTR_ERR(new_parent_kobj);
4240 put_device(new_parent);
4244 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
4245 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
4246 error = kobject_move(&dev->kobj, new_parent_kobj);
4248 cleanup_glue_dir(dev, new_parent_kobj);
4249 put_device(new_parent);
4252 old_parent = dev->parent;
4253 dev->parent = new_parent;
4255 klist_remove(&dev->p->knode_parent);
4257 klist_add_tail(&dev->p->knode_parent,
4258 &new_parent->p->klist_children);
4259 set_dev_node(dev, dev_to_node(new_parent));
4263 error = device_move_class_links(dev, old_parent, new_parent);
4265 /* We ignore errors on cleanup since we're hosed anyway... */
4266 device_move_class_links(dev, new_parent, old_parent);
4267 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
4269 klist_remove(&dev->p->knode_parent);
4270 dev->parent = old_parent;
4272 klist_add_tail(&dev->p->knode_parent,
4273 &old_parent->p->klist_children);
4274 set_dev_node(dev, dev_to_node(old_parent));
4277 cleanup_glue_dir(dev, new_parent_kobj);
4278 put_device(new_parent);
4282 switch (dpm_order) {
4283 case DPM_ORDER_NONE:
4285 case DPM_ORDER_DEV_AFTER_PARENT:
4286 device_pm_move_after(dev, new_parent);
4287 devices_kset_move_after(dev, new_parent);
4289 case DPM_ORDER_PARENT_BEFORE_DEV:
4290 device_pm_move_before(new_parent, dev);
4291 devices_kset_move_before(new_parent, dev);
4293 case DPM_ORDER_DEV_LAST:
4294 device_pm_move_last(dev);
4295 devices_kset_move_last(dev);
4299 put_device(old_parent);
4305 EXPORT_SYMBOL_GPL(device_move);
4307 static int device_attrs_change_owner(struct device *dev, kuid_t kuid,
4310 struct kobject *kobj = &dev->kobj;
4311 struct class *class = dev->class;
4312 const struct device_type *type = dev->type;
4317 * Change the device groups of the device class for @dev to
4320 error = sysfs_groups_change_owner(kobj, class->dev_groups, kuid,
4328 * Change the device groups of the device type for @dev to
4331 error = sysfs_groups_change_owner(kobj, type->groups, kuid,
4337 /* Change the device groups of @dev to @kuid/@kgid. */
4338 error = sysfs_groups_change_owner(kobj, dev->groups, kuid, kgid);
4342 if (device_supports_offline(dev) && !dev->offline_disabled) {
4343 /* Change online device attributes of @dev to @kuid/@kgid. */
4344 error = sysfs_file_change_owner(kobj, dev_attr_online.attr.name,
4354 * device_change_owner - change the owner of an existing device.
4356 * @kuid: new owner's kuid
4357 * @kgid: new owner's kgid
4359 * This changes the owner of @dev and its corresponding sysfs entries to
4360 * @kuid/@kgid. This function closely mirrors how @dev was added via driver
4363 * Returns 0 on success or error code on failure.
4365 int device_change_owner(struct device *dev, kuid_t kuid, kgid_t kgid)
4368 struct kobject *kobj = &dev->kobj;
4370 dev = get_device(dev);
4375 * Change the kobject and the default attributes and groups of the
4376 * ktype associated with it to @kuid/@kgid.
4378 error = sysfs_change_owner(kobj, kuid, kgid);
4383 * Change the uevent file for @dev to the new owner. The uevent file
4384 * was created in a separate step when @dev got added and we mirror
4387 error = sysfs_file_change_owner(kobj, dev_attr_uevent.attr.name, kuid,
4393 * Change the device groups, the device groups associated with the
4394 * device class, and the groups associated with the device type of @dev
4397 error = device_attrs_change_owner(dev, kuid, kgid);
4401 error = dpm_sysfs_change_owner(dev, kuid, kgid);
4406 if (sysfs_deprecated && dev->class == &block_class)
4411 * Change the owner of the symlink located in the class directory of
4412 * the device class associated with @dev which points to the actual
4413 * directory entry for @dev to @kuid/@kgid. This ensures that the
4414 * symlink shows the same permissions as its target.
4416 error = sysfs_link_change_owner(&dev->class->p->subsys.kobj, &dev->kobj,
4417 dev_name(dev), kuid, kgid);
4425 EXPORT_SYMBOL_GPL(device_change_owner);
4428 * device_shutdown - call ->shutdown() on each device to shutdown.
4430 void device_shutdown(void)
4432 struct device *dev, *parent;
4434 wait_for_device_probe();
4435 device_block_probing();
4439 spin_lock(&devices_kset->list_lock);
4441 * Walk the devices list backward, shutting down each in turn.
4442 * Beware that device unplug events may also start pulling
4443 * devices offline, even as the system is shutting down.
4445 while (!list_empty(&devices_kset->list)) {
4446 dev = list_entry(devices_kset->list.prev, struct device,
4450 * hold reference count of device's parent to
4451 * prevent it from being freed because parent's
4452 * lock is to be held
4454 parent = get_device(dev->parent);
4457 * Make sure the device is off the kset list, in the
4458 * event that dev->*->shutdown() doesn't remove it.
4460 list_del_init(&dev->kobj.entry);
4461 spin_unlock(&devices_kset->list_lock);
4463 /* hold lock to avoid race with probe/release */
4465 device_lock(parent);
4468 /* Don't allow any more runtime suspends */
4469 pm_runtime_get_noresume(dev);
4470 pm_runtime_barrier(dev);
4472 if (dev->class && dev->class->shutdown_pre) {
4474 dev_info(dev, "shutdown_pre\n");
4475 dev->class->shutdown_pre(dev);
4477 if (dev->bus && dev->bus->shutdown) {
4479 dev_info(dev, "shutdown\n");
4480 dev->bus->shutdown(dev);
4481 } else if (dev->driver && dev->driver->shutdown) {
4483 dev_info(dev, "shutdown\n");
4484 dev->driver->shutdown(dev);
4489 device_unlock(parent);
4494 spin_lock(&devices_kset->list_lock);
4496 spin_unlock(&devices_kset->list_lock);
4500 * Device logging functions
4503 #ifdef CONFIG_PRINTK
4505 set_dev_info(const struct device *dev, struct dev_printk_info *dev_info)
4509 memset(dev_info, 0, sizeof(*dev_info));
4512 subsys = dev->class->name;
4514 subsys = dev->bus->name;
4518 strscpy(dev_info->subsystem, subsys, sizeof(dev_info->subsystem));
4521 * Add device identifier DEVICE=:
4525 * +sound:card0 subsystem:devname
4527 if (MAJOR(dev->devt)) {
4530 if (strcmp(subsys, "block") == 0)
4535 snprintf(dev_info->device, sizeof(dev_info->device),
4536 "%c%u:%u", c, MAJOR(dev->devt), MINOR(dev->devt));
4537 } else if (strcmp(subsys, "net") == 0) {
4538 struct net_device *net = to_net_dev(dev);
4540 snprintf(dev_info->device, sizeof(dev_info->device),
4541 "n%u", net->ifindex);
4543 snprintf(dev_info->device, sizeof(dev_info->device),
4544 "+%s:%s", subsys, dev_name(dev));
4548 int dev_vprintk_emit(int level, const struct device *dev,
4549 const char *fmt, va_list args)
4551 struct dev_printk_info dev_info;
4553 set_dev_info(dev, &dev_info);
4555 return vprintk_emit(0, level, &dev_info, fmt, args);
4557 EXPORT_SYMBOL(dev_vprintk_emit);
4559 int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
4564 va_start(args, fmt);
4566 r = dev_vprintk_emit(level, dev, fmt, args);
4572 EXPORT_SYMBOL(dev_printk_emit);
4574 static void __dev_printk(const char *level, const struct device *dev,
4575 struct va_format *vaf)
4578 dev_printk_emit(level[1] - '0', dev, "%s %s: %pV",
4579 dev_driver_string(dev), dev_name(dev), vaf);
4581 printk("%s(NULL device *): %pV", level, vaf);
4584 void dev_printk(const char *level, const struct device *dev,
4585 const char *fmt, ...)
4587 struct va_format vaf;
4590 va_start(args, fmt);
4595 __dev_printk(level, dev, &vaf);
4599 EXPORT_SYMBOL(dev_printk);
4601 #define define_dev_printk_level(func, kern_level) \
4602 void func(const struct device *dev, const char *fmt, ...) \
4604 struct va_format vaf; \
4607 va_start(args, fmt); \
4612 __dev_printk(kern_level, dev, &vaf); \
4616 EXPORT_SYMBOL(func);
4618 define_dev_printk_level(_dev_emerg, KERN_EMERG);
4619 define_dev_printk_level(_dev_alert, KERN_ALERT);
4620 define_dev_printk_level(_dev_crit, KERN_CRIT);
4621 define_dev_printk_level(_dev_err, KERN_ERR);
4622 define_dev_printk_level(_dev_warn, KERN_WARNING);
4623 define_dev_printk_level(_dev_notice, KERN_NOTICE);
4624 define_dev_printk_level(_dev_info, KERN_INFO);
4629 * dev_err_probe - probe error check and log helper
4630 * @dev: the pointer to the struct device
4631 * @err: error value to test
4632 * @fmt: printf-style format string
4633 * @...: arguments as specified in the format string
4635 * This helper implements common pattern present in probe functions for error
4636 * checking: print debug or error message depending if the error value is
4637 * -EPROBE_DEFER and propagate error upwards.
4638 * In case of -EPROBE_DEFER it sets also defer probe reason, which can be
4639 * checked later by reading devices_deferred debugfs attribute.
4640 * It replaces code sequence::
4642 * if (err != -EPROBE_DEFER)
4643 * dev_err(dev, ...);
4645 * dev_dbg(dev, ...);
4650 * return dev_err_probe(dev, err, ...);
4655 int dev_err_probe(const struct device *dev, int err, const char *fmt, ...)
4657 struct va_format vaf;
4660 va_start(args, fmt);
4664 if (err != -EPROBE_DEFER) {
4665 dev_err(dev, "error %pe: %pV", ERR_PTR(err), &vaf);
4667 device_set_deferred_probe_reason(dev, &vaf);
4668 dev_dbg(dev, "error %pe: %pV", ERR_PTR(err), &vaf);
4675 EXPORT_SYMBOL_GPL(dev_err_probe);
4677 static inline bool fwnode_is_primary(struct fwnode_handle *fwnode)
4679 return fwnode && !IS_ERR(fwnode->secondary);
4683 * set_primary_fwnode - Change the primary firmware node of a given device.
4684 * @dev: Device to handle.
4685 * @fwnode: New primary firmware node of the device.
4687 * Set the device's firmware node pointer to @fwnode, but if a secondary
4688 * firmware node of the device is present, preserve it.
4690 * Valid fwnode cases are:
4691 * - primary --> secondary --> -ENODEV
4692 * - primary --> NULL
4693 * - secondary --> -ENODEV
4696 void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
4698 struct device *parent = dev->parent;
4699 struct fwnode_handle *fn = dev->fwnode;
4702 if (fwnode_is_primary(fn))
4706 WARN_ON(fwnode->secondary);
4707 fwnode->secondary = fn;
4709 dev->fwnode = fwnode;
4711 if (fwnode_is_primary(fn)) {
4712 dev->fwnode = fn->secondary;
4713 /* Set fn->secondary = NULL, so fn remains the primary fwnode */
4714 if (!(parent && fn == parent->fwnode))
4715 fn->secondary = NULL;
4721 EXPORT_SYMBOL_GPL(set_primary_fwnode);
4724 * set_secondary_fwnode - Change the secondary firmware node of a given device.
4725 * @dev: Device to handle.
4726 * @fwnode: New secondary firmware node of the device.
4728 * If a primary firmware node of the device is present, set its secondary
4729 * pointer to @fwnode. Otherwise, set the device's firmware node pointer to
4732 void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
4735 fwnode->secondary = ERR_PTR(-ENODEV);
4737 if (fwnode_is_primary(dev->fwnode))
4738 dev->fwnode->secondary = fwnode;
4740 dev->fwnode = fwnode;
4742 EXPORT_SYMBOL_GPL(set_secondary_fwnode);
4745 * device_set_of_node_from_dev - reuse device-tree node of another device
4746 * @dev: device whose device-tree node is being set
4747 * @dev2: device whose device-tree node is being reused
4749 * Takes another reference to the new device-tree node after first dropping
4750 * any reference held to the old node.
4752 void device_set_of_node_from_dev(struct device *dev, const struct device *dev2)
4754 of_node_put(dev->of_node);
4755 dev->of_node = of_node_get(dev2->of_node);
4756 dev->of_node_reused = true;
4758 EXPORT_SYMBOL_GPL(device_set_of_node_from_dev);
4760 void device_set_node(struct device *dev, struct fwnode_handle *fwnode)
4762 dev->fwnode = fwnode;
4763 dev->of_node = to_of_node(fwnode);
4765 EXPORT_SYMBOL_GPL(device_set_node);
4767 int device_match_name(struct device *dev, const void *name)
4769 return sysfs_streq(dev_name(dev), name);
4771 EXPORT_SYMBOL_GPL(device_match_name);
4773 int device_match_of_node(struct device *dev, const void *np)
4775 return dev->of_node == np;
4777 EXPORT_SYMBOL_GPL(device_match_of_node);
4779 int device_match_fwnode(struct device *dev, const void *fwnode)
4781 return dev_fwnode(dev) == fwnode;
4783 EXPORT_SYMBOL_GPL(device_match_fwnode);
4785 int device_match_devt(struct device *dev, const void *pdevt)
4787 return dev->devt == *(dev_t *)pdevt;
4789 EXPORT_SYMBOL_GPL(device_match_devt);
4791 int device_match_acpi_dev(struct device *dev, const void *adev)
4793 return ACPI_COMPANION(dev) == adev;
4795 EXPORT_SYMBOL(device_match_acpi_dev);
4797 int device_match_any(struct device *dev, const void *unused)
4801 EXPORT_SYMBOL_GPL(device_match_any);