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/kstrtox.h>
18 #include <linux/module.h>
19 #include <linux/slab.h>
20 #include <linux/string.h>
21 #include <linux/kdev_t.h>
22 #include <linux/notifier.h>
24 #include <linux/of_device.h>
25 #include <linux/blkdev.h>
26 #include <linux/mutex.h>
27 #include <linux/pm_runtime.h>
28 #include <linux/netdevice.h>
29 #include <linux/sched/signal.h>
30 #include <linux/sched/mm.h>
31 #include <linux/swiotlb.h>
32 #include <linux/sysfs.h>
33 #include <linux/dma-map-ops.h> /* for dma_default_coherent */
36 #include "physical_location.h"
37 #include "power/power.h"
39 /* Device links support. */
40 static LIST_HEAD(deferred_sync);
41 static unsigned int defer_sync_state_count = 1;
42 static DEFINE_MUTEX(fwnode_link_lock);
43 static bool fw_devlink_is_permissive(void);
44 static void __fw_devlink_link_to_consumers(struct device *dev);
45 static bool fw_devlink_drv_reg_done;
46 static bool fw_devlink_best_effort;
49 * __fwnode_link_add - Create a link between two fwnode_handles.
50 * @con: Consumer end of the link.
51 * @sup: Supplier end of the link.
53 * Create a fwnode link between fwnode handles @con and @sup. The fwnode link
54 * represents the detail that the firmware lists @sup fwnode as supplying a
57 * The driver core will use the fwnode link to create a device link between the
58 * two device objects corresponding to @con and @sup when they are created. The
59 * driver core will automatically delete the fwnode link between @con and @sup
62 * Attempts to create duplicate links between the same pair of fwnode handles
63 * are ignored and there is no reference counting.
65 static int __fwnode_link_add(struct fwnode_handle *con,
66 struct fwnode_handle *sup, u8 flags)
68 struct fwnode_link *link;
70 list_for_each_entry(link, &sup->consumers, s_hook)
71 if (link->consumer == con) {
76 link = kzalloc(sizeof(*link), GFP_KERNEL);
81 INIT_LIST_HEAD(&link->s_hook);
83 INIT_LIST_HEAD(&link->c_hook);
86 list_add(&link->s_hook, &sup->consumers);
87 list_add(&link->c_hook, &con->suppliers);
88 pr_debug("%pfwf Linked as a fwnode consumer to %pfwf\n",
94 int fwnode_link_add(struct fwnode_handle *con, struct fwnode_handle *sup)
98 mutex_lock(&fwnode_link_lock);
99 ret = __fwnode_link_add(con, sup, 0);
100 mutex_unlock(&fwnode_link_lock);
105 * __fwnode_link_del - Delete a link between two fwnode_handles.
106 * @link: the fwnode_link to be deleted
108 * The fwnode_link_lock needs to be held when this function is called.
110 static void __fwnode_link_del(struct fwnode_link *link)
112 pr_debug("%pfwf Dropping the fwnode link to %pfwf\n",
113 link->consumer, link->supplier);
114 list_del(&link->s_hook);
115 list_del(&link->c_hook);
120 * __fwnode_link_cycle - Mark a fwnode link as being part of a cycle.
121 * @link: the fwnode_link to be marked
123 * The fwnode_link_lock needs to be held when this function is called.
125 static void __fwnode_link_cycle(struct fwnode_link *link)
127 pr_debug("%pfwf: Relaxing link with %pfwf\n",
128 link->consumer, link->supplier);
129 link->flags |= FWLINK_FLAG_CYCLE;
133 * fwnode_links_purge_suppliers - Delete all supplier links of fwnode_handle.
134 * @fwnode: fwnode whose supplier links need to be deleted
136 * Deletes all supplier links connecting directly to @fwnode.
138 static void fwnode_links_purge_suppliers(struct fwnode_handle *fwnode)
140 struct fwnode_link *link, *tmp;
142 mutex_lock(&fwnode_link_lock);
143 list_for_each_entry_safe(link, tmp, &fwnode->suppliers, c_hook)
144 __fwnode_link_del(link);
145 mutex_unlock(&fwnode_link_lock);
149 * fwnode_links_purge_consumers - Delete all consumer links of fwnode_handle.
150 * @fwnode: fwnode whose consumer links need to be deleted
152 * Deletes all consumer links connecting directly to @fwnode.
154 static void fwnode_links_purge_consumers(struct fwnode_handle *fwnode)
156 struct fwnode_link *link, *tmp;
158 mutex_lock(&fwnode_link_lock);
159 list_for_each_entry_safe(link, tmp, &fwnode->consumers, s_hook)
160 __fwnode_link_del(link);
161 mutex_unlock(&fwnode_link_lock);
165 * fwnode_links_purge - Delete all links connected to a fwnode_handle.
166 * @fwnode: fwnode whose links needs to be deleted
168 * Deletes all links connecting directly to a fwnode.
170 void fwnode_links_purge(struct fwnode_handle *fwnode)
172 fwnode_links_purge_suppliers(fwnode);
173 fwnode_links_purge_consumers(fwnode);
176 void fw_devlink_purge_absent_suppliers(struct fwnode_handle *fwnode)
178 struct fwnode_handle *child;
180 /* Don't purge consumer links of an added child */
184 fwnode->flags |= FWNODE_FLAG_NOT_DEVICE;
185 fwnode_links_purge_consumers(fwnode);
187 fwnode_for_each_available_child_node(fwnode, child)
188 fw_devlink_purge_absent_suppliers(child);
190 EXPORT_SYMBOL_GPL(fw_devlink_purge_absent_suppliers);
193 * __fwnode_links_move_consumers - Move consumer from @from to @to fwnode_handle
194 * @from: move consumers away from this fwnode
195 * @to: move consumers to this fwnode
197 * Move all consumer links from @from fwnode to @to fwnode.
199 static void __fwnode_links_move_consumers(struct fwnode_handle *from,
200 struct fwnode_handle *to)
202 struct fwnode_link *link, *tmp;
204 list_for_each_entry_safe(link, tmp, &from->consumers, s_hook) {
205 __fwnode_link_add(link->consumer, to, link->flags);
206 __fwnode_link_del(link);
211 * __fw_devlink_pickup_dangling_consumers - Pick up dangling consumers
212 * @fwnode: fwnode from which to pick up dangling consumers
213 * @new_sup: fwnode of new supplier
215 * If the @fwnode has a corresponding struct device and the device supports
216 * probing (that is, added to a bus), then we want to let fw_devlink create
217 * MANAGED device links to this device, so leave @fwnode and its descendant's
218 * fwnode links alone.
220 * Otherwise, move its consumers to the new supplier @new_sup.
222 static void __fw_devlink_pickup_dangling_consumers(struct fwnode_handle *fwnode,
223 struct fwnode_handle *new_sup)
225 struct fwnode_handle *child;
227 if (fwnode->dev && fwnode->dev->bus)
230 fwnode->flags |= FWNODE_FLAG_NOT_DEVICE;
231 __fwnode_links_move_consumers(fwnode, new_sup);
233 fwnode_for_each_available_child_node(fwnode, child)
234 __fw_devlink_pickup_dangling_consumers(child, new_sup);
237 static DEFINE_MUTEX(device_links_lock);
238 DEFINE_STATIC_SRCU(device_links_srcu);
240 static inline void device_links_write_lock(void)
242 mutex_lock(&device_links_lock);
245 static inline void device_links_write_unlock(void)
247 mutex_unlock(&device_links_lock);
250 int device_links_read_lock(void) __acquires(&device_links_srcu)
252 return srcu_read_lock(&device_links_srcu);
255 void device_links_read_unlock(int idx) __releases(&device_links_srcu)
257 srcu_read_unlock(&device_links_srcu, idx);
260 int device_links_read_lock_held(void)
262 return srcu_read_lock_held(&device_links_srcu);
265 static void device_link_synchronize_removal(void)
267 synchronize_srcu(&device_links_srcu);
270 static void device_link_remove_from_lists(struct device_link *link)
272 list_del_rcu(&link->s_node);
273 list_del_rcu(&link->c_node);
276 static bool device_is_ancestor(struct device *dev, struct device *target)
278 while (target->parent) {
279 target = target->parent;
286 static inline bool device_link_flag_is_sync_state_only(u32 flags)
288 return (flags & ~(DL_FLAG_INFERRED | DL_FLAG_CYCLE)) ==
289 (DL_FLAG_SYNC_STATE_ONLY | DL_FLAG_MANAGED);
293 * device_is_dependent - Check if one device depends on another one
294 * @dev: Device to check dependencies for.
295 * @target: Device to check against.
297 * Check if @target depends on @dev or any device dependent on it (its child or
298 * its consumer etc). Return 1 if that is the case or 0 otherwise.
300 int device_is_dependent(struct device *dev, void *target)
302 struct device_link *link;
306 * The "ancestors" check is needed to catch the case when the target
307 * device has not been completely initialized yet and it is still
308 * missing from the list of children of its parent device.
310 if (dev == target || device_is_ancestor(dev, target))
313 ret = device_for_each_child(dev, target, device_is_dependent);
317 list_for_each_entry(link, &dev->links.consumers, s_node) {
318 if (device_link_flag_is_sync_state_only(link->flags))
321 if (link->consumer == target)
324 ret = device_is_dependent(link->consumer, target);
331 static void device_link_init_status(struct device_link *link,
332 struct device *consumer,
333 struct device *supplier)
335 switch (supplier->links.status) {
337 switch (consumer->links.status) {
340 * A consumer driver can create a link to a supplier
341 * that has not completed its probing yet as long as it
342 * knows that the supplier is already functional (for
343 * example, it has just acquired some resources from the
346 link->status = DL_STATE_CONSUMER_PROBE;
349 link->status = DL_STATE_DORMANT;
353 case DL_DEV_DRIVER_BOUND:
354 switch (consumer->links.status) {
356 link->status = DL_STATE_CONSUMER_PROBE;
358 case DL_DEV_DRIVER_BOUND:
359 link->status = DL_STATE_ACTIVE;
362 link->status = DL_STATE_AVAILABLE;
366 case DL_DEV_UNBINDING:
367 link->status = DL_STATE_SUPPLIER_UNBIND;
370 link->status = DL_STATE_DORMANT;
375 static int device_reorder_to_tail(struct device *dev, void *not_used)
377 struct device_link *link;
380 * Devices that have not been registered yet will be put to the ends
381 * of the lists during the registration, so skip them here.
383 if (device_is_registered(dev))
384 devices_kset_move_last(dev);
386 if (device_pm_initialized(dev))
387 device_pm_move_last(dev);
389 device_for_each_child(dev, NULL, device_reorder_to_tail);
390 list_for_each_entry(link, &dev->links.consumers, s_node) {
391 if (device_link_flag_is_sync_state_only(link->flags))
393 device_reorder_to_tail(link->consumer, NULL);
400 * device_pm_move_to_tail - Move set of devices to the end of device lists
401 * @dev: Device to move
403 * This is a device_reorder_to_tail() wrapper taking the requisite locks.
405 * It moves the @dev along with all of its children and all of its consumers
406 * to the ends of the device_kset and dpm_list, recursively.
408 void device_pm_move_to_tail(struct device *dev)
412 idx = device_links_read_lock();
414 device_reorder_to_tail(dev, NULL);
416 device_links_read_unlock(idx);
419 #define to_devlink(dev) container_of((dev), struct device_link, link_dev)
421 static ssize_t status_show(struct device *dev,
422 struct device_attribute *attr, char *buf)
426 switch (to_devlink(dev)->status) {
428 output = "not tracked";
430 case DL_STATE_DORMANT:
433 case DL_STATE_AVAILABLE:
434 output = "available";
436 case DL_STATE_CONSUMER_PROBE:
437 output = "consumer probing";
439 case DL_STATE_ACTIVE:
442 case DL_STATE_SUPPLIER_UNBIND:
443 output = "supplier unbinding";
450 return sysfs_emit(buf, "%s\n", output);
452 static DEVICE_ATTR_RO(status);
454 static ssize_t auto_remove_on_show(struct device *dev,
455 struct device_attribute *attr, char *buf)
457 struct device_link *link = to_devlink(dev);
460 if (link->flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
461 output = "supplier unbind";
462 else if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER)
463 output = "consumer unbind";
467 return sysfs_emit(buf, "%s\n", output);
469 static DEVICE_ATTR_RO(auto_remove_on);
471 static ssize_t runtime_pm_show(struct device *dev,
472 struct device_attribute *attr, char *buf)
474 struct device_link *link = to_devlink(dev);
476 return sysfs_emit(buf, "%d\n", !!(link->flags & DL_FLAG_PM_RUNTIME));
478 static DEVICE_ATTR_RO(runtime_pm);
480 static ssize_t sync_state_only_show(struct device *dev,
481 struct device_attribute *attr, char *buf)
483 struct device_link *link = to_devlink(dev);
485 return sysfs_emit(buf, "%d\n",
486 !!(link->flags & DL_FLAG_SYNC_STATE_ONLY));
488 static DEVICE_ATTR_RO(sync_state_only);
490 static struct attribute *devlink_attrs[] = {
491 &dev_attr_status.attr,
492 &dev_attr_auto_remove_on.attr,
493 &dev_attr_runtime_pm.attr,
494 &dev_attr_sync_state_only.attr,
497 ATTRIBUTE_GROUPS(devlink);
499 static void device_link_release_fn(struct work_struct *work)
501 struct device_link *link = container_of(work, struct device_link, rm_work);
503 /* Ensure that all references to the link object have been dropped. */
504 device_link_synchronize_removal();
506 pm_runtime_release_supplier(link);
508 * If supplier_preactivated is set, the link has been dropped between
509 * the pm_runtime_get_suppliers() and pm_runtime_put_suppliers() calls
510 * in __driver_probe_device(). In that case, drop the supplier's
511 * PM-runtime usage counter to remove the reference taken by
512 * pm_runtime_get_suppliers().
514 if (link->supplier_preactivated)
515 pm_runtime_put_noidle(link->supplier);
517 pm_request_idle(link->supplier);
519 put_device(link->consumer);
520 put_device(link->supplier);
524 static void devlink_dev_release(struct device *dev)
526 struct device_link *link = to_devlink(dev);
528 INIT_WORK(&link->rm_work, device_link_release_fn);
530 * It may take a while to complete this work because of the SRCU
531 * synchronization in device_link_release_fn() and if the consumer or
532 * supplier devices get deleted when it runs, so put it into the "long"
535 queue_work(system_long_wq, &link->rm_work);
538 static struct class devlink_class = {
540 .dev_groups = devlink_groups,
541 .dev_release = devlink_dev_release,
544 static int devlink_add_symlinks(struct device *dev,
545 struct class_interface *class_intf)
549 struct device_link *link = to_devlink(dev);
550 struct device *sup = link->supplier;
551 struct device *con = link->consumer;
554 len = max(strlen(dev_bus_name(sup)) + strlen(dev_name(sup)),
555 strlen(dev_bus_name(con)) + strlen(dev_name(con)));
557 len += strlen("supplier:") + 1;
558 buf = kzalloc(len, GFP_KERNEL);
562 ret = sysfs_create_link(&link->link_dev.kobj, &sup->kobj, "supplier");
566 ret = sysfs_create_link(&link->link_dev.kobj, &con->kobj, "consumer");
570 snprintf(buf, len, "consumer:%s:%s", dev_bus_name(con), dev_name(con));
571 ret = sysfs_create_link(&sup->kobj, &link->link_dev.kobj, buf);
575 snprintf(buf, len, "supplier:%s:%s", dev_bus_name(sup), dev_name(sup));
576 ret = sysfs_create_link(&con->kobj, &link->link_dev.kobj, buf);
583 snprintf(buf, len, "consumer:%s:%s", dev_bus_name(con), dev_name(con));
584 sysfs_remove_link(&sup->kobj, buf);
586 sysfs_remove_link(&link->link_dev.kobj, "consumer");
588 sysfs_remove_link(&link->link_dev.kobj, "supplier");
594 static void devlink_remove_symlinks(struct device *dev,
595 struct class_interface *class_intf)
597 struct device_link *link = to_devlink(dev);
599 struct device *sup = link->supplier;
600 struct device *con = link->consumer;
603 sysfs_remove_link(&link->link_dev.kobj, "consumer");
604 sysfs_remove_link(&link->link_dev.kobj, "supplier");
606 len = max(strlen(dev_bus_name(sup)) + strlen(dev_name(sup)),
607 strlen(dev_bus_name(con)) + strlen(dev_name(con)));
609 len += strlen("supplier:") + 1;
610 buf = kzalloc(len, GFP_KERNEL);
612 WARN(1, "Unable to properly free device link symlinks!\n");
616 if (device_is_registered(con)) {
617 snprintf(buf, len, "supplier:%s:%s", dev_bus_name(sup), dev_name(sup));
618 sysfs_remove_link(&con->kobj, buf);
620 snprintf(buf, len, "consumer:%s:%s", dev_bus_name(con), dev_name(con));
621 sysfs_remove_link(&sup->kobj, buf);
625 static struct class_interface devlink_class_intf = {
626 .class = &devlink_class,
627 .add_dev = devlink_add_symlinks,
628 .remove_dev = devlink_remove_symlinks,
631 static int __init devlink_class_init(void)
635 ret = class_register(&devlink_class);
639 ret = class_interface_register(&devlink_class_intf);
641 class_unregister(&devlink_class);
645 postcore_initcall(devlink_class_init);
647 #define DL_MANAGED_LINK_FLAGS (DL_FLAG_AUTOREMOVE_CONSUMER | \
648 DL_FLAG_AUTOREMOVE_SUPPLIER | \
649 DL_FLAG_AUTOPROBE_CONSUMER | \
650 DL_FLAG_SYNC_STATE_ONLY | \
654 #define DL_ADD_VALID_FLAGS (DL_MANAGED_LINK_FLAGS | DL_FLAG_STATELESS | \
655 DL_FLAG_PM_RUNTIME | DL_FLAG_RPM_ACTIVE)
658 * device_link_add - Create a link between two devices.
659 * @consumer: Consumer end of the link.
660 * @supplier: Supplier end of the link.
661 * @flags: Link flags.
663 * The caller is responsible for the proper synchronization of the link creation
664 * with runtime PM. First, setting the DL_FLAG_PM_RUNTIME flag will cause the
665 * runtime PM framework to take the link into account. Second, if the
666 * DL_FLAG_RPM_ACTIVE flag is set in addition to it, the supplier devices will
667 * be forced into the active meta state and reference-counted upon the creation
668 * of the link. If DL_FLAG_PM_RUNTIME is not set, DL_FLAG_RPM_ACTIVE will be
671 * If DL_FLAG_STATELESS is set in @flags, the caller of this function is
672 * expected to release the link returned by it directly with the help of either
673 * device_link_del() or device_link_remove().
675 * If that flag is not set, however, the caller of this function is handing the
676 * management of the link over to the driver core entirely and its return value
677 * can only be used to check whether or not the link is present. In that case,
678 * the DL_FLAG_AUTOREMOVE_CONSUMER and DL_FLAG_AUTOREMOVE_SUPPLIER device link
679 * flags can be used to indicate to the driver core when the link can be safely
680 * deleted. Namely, setting one of them in @flags indicates to the driver core
681 * that the link is not going to be used (by the given caller of this function)
682 * after unbinding the consumer or supplier driver, respectively, from its
683 * device, so the link can be deleted at that point. If none of them is set,
684 * the link will be maintained until one of the devices pointed to by it (either
685 * the consumer or the supplier) is unregistered.
687 * Also, if DL_FLAG_STATELESS, DL_FLAG_AUTOREMOVE_CONSUMER and
688 * DL_FLAG_AUTOREMOVE_SUPPLIER are not set in @flags (that is, a persistent
689 * managed device link is being added), the DL_FLAG_AUTOPROBE_CONSUMER flag can
690 * be used to request the driver core to automatically probe for a consumer
691 * driver after successfully binding a driver to the supplier device.
693 * The combination of DL_FLAG_STATELESS and one of DL_FLAG_AUTOREMOVE_CONSUMER,
694 * DL_FLAG_AUTOREMOVE_SUPPLIER, or DL_FLAG_AUTOPROBE_CONSUMER set in @flags at
695 * the same time is invalid and will cause NULL to be returned upfront.
696 * However, if a device link between the given @consumer and @supplier pair
697 * exists already when this function is called for them, the existing link will
698 * be returned regardless of its current type and status (the link's flags may
699 * be modified then). The caller of this function is then expected to treat
700 * the link as though it has just been created, so (in particular) if
701 * DL_FLAG_STATELESS was passed in @flags, the link needs to be released
702 * explicitly when not needed any more (as stated above).
704 * A side effect of the link creation is re-ordering of dpm_list and the
705 * devices_kset list by moving the consumer device and all devices depending
706 * on it to the ends of these lists (that does not happen to devices that have
707 * not been registered when this function is called).
709 * The supplier device is required to be registered when this function is called
710 * and NULL will be returned if that is not the case. The consumer device need
711 * not be registered, however.
713 struct device_link *device_link_add(struct device *consumer,
714 struct device *supplier, u32 flags)
716 struct device_link *link;
718 if (!consumer || !supplier || consumer == supplier ||
719 flags & ~DL_ADD_VALID_FLAGS ||
720 (flags & DL_FLAG_STATELESS && flags & DL_MANAGED_LINK_FLAGS) ||
721 (flags & DL_FLAG_AUTOPROBE_CONSUMER &&
722 flags & (DL_FLAG_AUTOREMOVE_CONSUMER |
723 DL_FLAG_AUTOREMOVE_SUPPLIER)))
726 if (flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) {
727 if (pm_runtime_get_sync(supplier) < 0) {
728 pm_runtime_put_noidle(supplier);
733 if (!(flags & DL_FLAG_STATELESS))
734 flags |= DL_FLAG_MANAGED;
736 if (flags & DL_FLAG_SYNC_STATE_ONLY &&
737 !device_link_flag_is_sync_state_only(flags))
740 device_links_write_lock();
744 * If the supplier has not been fully registered yet or there is a
745 * reverse (non-SYNC_STATE_ONLY) dependency between the consumer and
746 * the supplier already in the graph, return NULL. If the link is a
747 * SYNC_STATE_ONLY link, we don't check for reverse dependencies
748 * because it only affects sync_state() callbacks.
750 if (!device_pm_initialized(supplier)
751 || (!(flags & DL_FLAG_SYNC_STATE_ONLY) &&
752 device_is_dependent(consumer, supplier))) {
758 * SYNC_STATE_ONLY links are useless once a consumer device has probed.
759 * So, only create it if the consumer hasn't probed yet.
761 if (flags & DL_FLAG_SYNC_STATE_ONLY &&
762 consumer->links.status != DL_DEV_NO_DRIVER &&
763 consumer->links.status != DL_DEV_PROBING) {
769 * DL_FLAG_AUTOREMOVE_SUPPLIER indicates that the link will be needed
770 * longer than for DL_FLAG_AUTOREMOVE_CONSUMER and setting them both
771 * together doesn't make sense, so prefer DL_FLAG_AUTOREMOVE_SUPPLIER.
773 if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
774 flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER;
776 list_for_each_entry(link, &supplier->links.consumers, s_node) {
777 if (link->consumer != consumer)
780 if (link->flags & DL_FLAG_INFERRED &&
781 !(flags & DL_FLAG_INFERRED))
782 link->flags &= ~DL_FLAG_INFERRED;
784 if (flags & DL_FLAG_PM_RUNTIME) {
785 if (!(link->flags & DL_FLAG_PM_RUNTIME)) {
786 pm_runtime_new_link(consumer);
787 link->flags |= DL_FLAG_PM_RUNTIME;
789 if (flags & DL_FLAG_RPM_ACTIVE)
790 refcount_inc(&link->rpm_active);
793 if (flags & DL_FLAG_STATELESS) {
794 kref_get(&link->kref);
795 if (link->flags & DL_FLAG_SYNC_STATE_ONLY &&
796 !(link->flags & DL_FLAG_STATELESS)) {
797 link->flags |= DL_FLAG_STATELESS;
800 link->flags |= DL_FLAG_STATELESS;
806 * If the life time of the link following from the new flags is
807 * longer than indicated by the flags of the existing link,
808 * update the existing link to stay around longer.
810 if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER) {
811 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER) {
812 link->flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER;
813 link->flags |= DL_FLAG_AUTOREMOVE_SUPPLIER;
815 } else if (!(flags & DL_FLAG_AUTOREMOVE_CONSUMER)) {
816 link->flags &= ~(DL_FLAG_AUTOREMOVE_CONSUMER |
817 DL_FLAG_AUTOREMOVE_SUPPLIER);
819 if (!(link->flags & DL_FLAG_MANAGED)) {
820 kref_get(&link->kref);
821 link->flags |= DL_FLAG_MANAGED;
822 device_link_init_status(link, consumer, supplier);
824 if (link->flags & DL_FLAG_SYNC_STATE_ONLY &&
825 !(flags & DL_FLAG_SYNC_STATE_ONLY)) {
826 link->flags &= ~DL_FLAG_SYNC_STATE_ONLY;
833 link = kzalloc(sizeof(*link), GFP_KERNEL);
837 refcount_set(&link->rpm_active, 1);
839 get_device(supplier);
840 link->supplier = supplier;
841 INIT_LIST_HEAD(&link->s_node);
842 get_device(consumer);
843 link->consumer = consumer;
844 INIT_LIST_HEAD(&link->c_node);
846 kref_init(&link->kref);
848 link->link_dev.class = &devlink_class;
849 device_set_pm_not_required(&link->link_dev);
850 dev_set_name(&link->link_dev, "%s:%s--%s:%s",
851 dev_bus_name(supplier), dev_name(supplier),
852 dev_bus_name(consumer), dev_name(consumer));
853 if (device_register(&link->link_dev)) {
854 put_device(&link->link_dev);
859 if (flags & DL_FLAG_PM_RUNTIME) {
860 if (flags & DL_FLAG_RPM_ACTIVE)
861 refcount_inc(&link->rpm_active);
863 pm_runtime_new_link(consumer);
866 /* Determine the initial link state. */
867 if (flags & DL_FLAG_STATELESS)
868 link->status = DL_STATE_NONE;
870 device_link_init_status(link, consumer, supplier);
873 * Some callers expect the link creation during consumer driver probe to
874 * resume the supplier even without DL_FLAG_RPM_ACTIVE.
876 if (link->status == DL_STATE_CONSUMER_PROBE &&
877 flags & DL_FLAG_PM_RUNTIME)
878 pm_runtime_resume(supplier);
880 list_add_tail_rcu(&link->s_node, &supplier->links.consumers);
881 list_add_tail_rcu(&link->c_node, &consumer->links.suppliers);
883 if (flags & DL_FLAG_SYNC_STATE_ONLY) {
885 "Linked as a sync state only consumer to %s\n",
892 * Move the consumer and all of the devices depending on it to the end
893 * of dpm_list and the devices_kset list.
895 * It is necessary to hold dpm_list locked throughout all that or else
896 * we may end up suspending with a wrong ordering of it.
898 device_reorder_to_tail(consumer, NULL);
900 dev_dbg(consumer, "Linked as a consumer to %s\n", dev_name(supplier));
904 device_links_write_unlock();
906 if ((flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) && !link)
907 pm_runtime_put(supplier);
911 EXPORT_SYMBOL_GPL(device_link_add);
913 static void __device_link_del(struct kref *kref)
915 struct device_link *link = container_of(kref, struct device_link, kref);
917 dev_dbg(link->consumer, "Dropping the link to %s\n",
918 dev_name(link->supplier));
920 pm_runtime_drop_link(link);
922 device_link_remove_from_lists(link);
923 device_unregister(&link->link_dev);
926 static void device_link_put_kref(struct device_link *link)
928 if (link->flags & DL_FLAG_STATELESS)
929 kref_put(&link->kref, __device_link_del);
930 else if (!device_is_registered(link->consumer))
931 __device_link_del(&link->kref);
933 WARN(1, "Unable to drop a managed device link reference\n");
937 * device_link_del - Delete a stateless link between two devices.
938 * @link: Device link to delete.
940 * The caller must ensure proper synchronization of this function with runtime
941 * PM. If the link was added multiple times, it needs to be deleted as often.
942 * Care is required for hotplugged devices: Their links are purged on removal
943 * and calling device_link_del() is then no longer allowed.
945 void device_link_del(struct device_link *link)
947 device_links_write_lock();
948 device_link_put_kref(link);
949 device_links_write_unlock();
951 EXPORT_SYMBOL_GPL(device_link_del);
954 * device_link_remove - Delete a stateless link between two devices.
955 * @consumer: Consumer end of the link.
956 * @supplier: Supplier end of the link.
958 * The caller must ensure proper synchronization of this function with runtime
961 void device_link_remove(void *consumer, struct device *supplier)
963 struct device_link *link;
965 if (WARN_ON(consumer == supplier))
968 device_links_write_lock();
970 list_for_each_entry(link, &supplier->links.consumers, s_node) {
971 if (link->consumer == consumer) {
972 device_link_put_kref(link);
977 device_links_write_unlock();
979 EXPORT_SYMBOL_GPL(device_link_remove);
981 static void device_links_missing_supplier(struct device *dev)
983 struct device_link *link;
985 list_for_each_entry(link, &dev->links.suppliers, c_node) {
986 if (link->status != DL_STATE_CONSUMER_PROBE)
989 if (link->supplier->links.status == DL_DEV_DRIVER_BOUND) {
990 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
992 WARN_ON(!(link->flags & DL_FLAG_SYNC_STATE_ONLY));
993 WRITE_ONCE(link->status, DL_STATE_DORMANT);
998 static bool dev_is_best_effort(struct device *dev)
1000 return (fw_devlink_best_effort && dev->can_match) ||
1001 (dev->fwnode && (dev->fwnode->flags & FWNODE_FLAG_BEST_EFFORT));
1004 static struct fwnode_handle *fwnode_links_check_suppliers(
1005 struct fwnode_handle *fwnode)
1007 struct fwnode_link *link;
1009 if (!fwnode || fw_devlink_is_permissive())
1012 list_for_each_entry(link, &fwnode->suppliers, c_hook)
1013 if (!(link->flags & FWLINK_FLAG_CYCLE))
1014 return link->supplier;
1020 * device_links_check_suppliers - Check presence of supplier drivers.
1021 * @dev: Consumer device.
1023 * Check links from this device to any suppliers. Walk the list of the device's
1024 * links to suppliers and see if all of them are available. If not, simply
1025 * return -EPROBE_DEFER.
1027 * We need to guarantee that the supplier will not go away after the check has
1028 * been positive here. It only can go away in __device_release_driver() and
1029 * that function checks the device's links to consumers. This means we need to
1030 * mark the link as "consumer probe in progress" to make the supplier removal
1031 * wait for us to complete (or bad things may happen).
1033 * Links without the DL_FLAG_MANAGED flag set are ignored.
1035 int device_links_check_suppliers(struct device *dev)
1037 struct device_link *link;
1038 int ret = 0, fwnode_ret = 0;
1039 struct fwnode_handle *sup_fw;
1042 * Device waiting for supplier to become available is not allowed to
1045 mutex_lock(&fwnode_link_lock);
1046 sup_fw = fwnode_links_check_suppliers(dev->fwnode);
1048 if (!dev_is_best_effort(dev)) {
1049 fwnode_ret = -EPROBE_DEFER;
1050 dev_err_probe(dev, -EPROBE_DEFER,
1051 "wait for supplier %pfwf\n", sup_fw);
1053 fwnode_ret = -EAGAIN;
1056 mutex_unlock(&fwnode_link_lock);
1057 if (fwnode_ret == -EPROBE_DEFER)
1060 device_links_write_lock();
1062 list_for_each_entry(link, &dev->links.suppliers, c_node) {
1063 if (!(link->flags & DL_FLAG_MANAGED))
1066 if (link->status != DL_STATE_AVAILABLE &&
1067 !(link->flags & DL_FLAG_SYNC_STATE_ONLY)) {
1069 if (dev_is_best_effort(dev) &&
1070 link->flags & DL_FLAG_INFERRED &&
1071 !link->supplier->can_match) {
1076 device_links_missing_supplier(dev);
1077 dev_err_probe(dev, -EPROBE_DEFER,
1078 "supplier %s not ready\n",
1079 dev_name(link->supplier));
1080 ret = -EPROBE_DEFER;
1083 WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE);
1085 dev->links.status = DL_DEV_PROBING;
1087 device_links_write_unlock();
1089 return ret ? ret : fwnode_ret;
1093 * __device_links_queue_sync_state - Queue a device for sync_state() callback
1094 * @dev: Device to call sync_state() on
1095 * @list: List head to queue the @dev on
1097 * Queues a device for a sync_state() callback when the device links write lock
1098 * isn't held. This allows the sync_state() execution flow to use device links
1099 * APIs. The caller must ensure this function is called with
1100 * device_links_write_lock() held.
1102 * This function does a get_device() to make sure the device is not freed while
1105 * So the caller must also ensure that device_links_flush_sync_list() is called
1106 * as soon as the caller releases device_links_write_lock(). This is necessary
1107 * to make sure the sync_state() is called in a timely fashion and the
1108 * put_device() is called on this device.
1110 static void __device_links_queue_sync_state(struct device *dev,
1111 struct list_head *list)
1113 struct device_link *link;
1115 if (!dev_has_sync_state(dev))
1117 if (dev->state_synced)
1120 list_for_each_entry(link, &dev->links.consumers, s_node) {
1121 if (!(link->flags & DL_FLAG_MANAGED))
1123 if (link->status != DL_STATE_ACTIVE)
1128 * Set the flag here to avoid adding the same device to a list more
1129 * than once. This can happen if new consumers get added to the device
1130 * and probed before the list is flushed.
1132 dev->state_synced = true;
1134 if (WARN_ON(!list_empty(&dev->links.defer_sync)))
1138 list_add_tail(&dev->links.defer_sync, list);
1142 * device_links_flush_sync_list - Call sync_state() on a list of devices
1143 * @list: List of devices to call sync_state() on
1144 * @dont_lock_dev: Device for which lock is already held by the caller
1146 * Calls sync_state() on all the devices that have been queued for it. This
1147 * function is used in conjunction with __device_links_queue_sync_state(). The
1148 * @dont_lock_dev parameter is useful when this function is called from a
1149 * context where a device lock is already held.
1151 static void device_links_flush_sync_list(struct list_head *list,
1152 struct device *dont_lock_dev)
1154 struct device *dev, *tmp;
1156 list_for_each_entry_safe(dev, tmp, list, links.defer_sync) {
1157 list_del_init(&dev->links.defer_sync);
1159 if (dev != dont_lock_dev)
1162 dev_sync_state(dev);
1164 if (dev != dont_lock_dev)
1171 void device_links_supplier_sync_state_pause(void)
1173 device_links_write_lock();
1174 defer_sync_state_count++;
1175 device_links_write_unlock();
1178 void device_links_supplier_sync_state_resume(void)
1180 struct device *dev, *tmp;
1181 LIST_HEAD(sync_list);
1183 device_links_write_lock();
1184 if (!defer_sync_state_count) {
1185 WARN(true, "Unmatched sync_state pause/resume!");
1188 defer_sync_state_count--;
1189 if (defer_sync_state_count)
1192 list_for_each_entry_safe(dev, tmp, &deferred_sync, links.defer_sync) {
1194 * Delete from deferred_sync list before queuing it to
1195 * sync_list because defer_sync is used for both lists.
1197 list_del_init(&dev->links.defer_sync);
1198 __device_links_queue_sync_state(dev, &sync_list);
1201 device_links_write_unlock();
1203 device_links_flush_sync_list(&sync_list, NULL);
1206 static int sync_state_resume_initcall(void)
1208 device_links_supplier_sync_state_resume();
1211 late_initcall(sync_state_resume_initcall);
1213 static void __device_links_supplier_defer_sync(struct device *sup)
1215 if (list_empty(&sup->links.defer_sync) && dev_has_sync_state(sup))
1216 list_add_tail(&sup->links.defer_sync, &deferred_sync);
1219 static void device_link_drop_managed(struct device_link *link)
1221 link->flags &= ~DL_FLAG_MANAGED;
1222 WRITE_ONCE(link->status, DL_STATE_NONE);
1223 kref_put(&link->kref, __device_link_del);
1226 static ssize_t waiting_for_supplier_show(struct device *dev,
1227 struct device_attribute *attr,
1233 mutex_lock(&fwnode_link_lock);
1234 val = !!fwnode_links_check_suppliers(dev->fwnode);
1235 mutex_unlock(&fwnode_link_lock);
1237 return sysfs_emit(buf, "%u\n", val);
1239 static DEVICE_ATTR_RO(waiting_for_supplier);
1242 * device_links_force_bind - Prepares device to be force bound
1243 * @dev: Consumer device.
1245 * device_bind_driver() force binds a device to a driver without calling any
1246 * driver probe functions. So the consumer really isn't going to wait for any
1247 * supplier before it's bound to the driver. We still want the device link
1248 * states to be sensible when this happens.
1250 * In preparation for device_bind_driver(), this function goes through each
1251 * supplier device links and checks if the supplier is bound. If it is, then
1252 * the device link status is set to CONSUMER_PROBE. Otherwise, the device link
1253 * is dropped. Links without the DL_FLAG_MANAGED flag set are ignored.
1255 void device_links_force_bind(struct device *dev)
1257 struct device_link *link, *ln;
1259 device_links_write_lock();
1261 list_for_each_entry_safe(link, ln, &dev->links.suppliers, c_node) {
1262 if (!(link->flags & DL_FLAG_MANAGED))
1265 if (link->status != DL_STATE_AVAILABLE) {
1266 device_link_drop_managed(link);
1269 WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE);
1271 dev->links.status = DL_DEV_PROBING;
1273 device_links_write_unlock();
1277 * device_links_driver_bound - Update device links after probing its driver.
1278 * @dev: Device to update the links for.
1280 * The probe has been successful, so update links from this device to any
1281 * consumers by changing their status to "available".
1283 * Also change the status of @dev's links to suppliers to "active".
1285 * Links without the DL_FLAG_MANAGED flag set are ignored.
1287 void device_links_driver_bound(struct device *dev)
1289 struct device_link *link, *ln;
1290 LIST_HEAD(sync_list);
1293 * If a device binds successfully, it's expected to have created all
1294 * the device links it needs to or make new device links as it needs
1295 * them. So, fw_devlink no longer needs to create device links to any
1296 * of the device's suppliers.
1298 * Also, if a child firmware node of this bound device is not added as a
1299 * device by now, assume it is never going to be added. Make this bound
1300 * device the fallback supplier to the dangling consumers of the child
1301 * firmware node because this bound device is probably implementing the
1302 * child firmware node functionality and we don't want the dangling
1303 * consumers to defer probe indefinitely waiting for a device for the
1304 * child firmware node.
1306 if (dev->fwnode && dev->fwnode->dev == dev) {
1307 struct fwnode_handle *child;
1308 fwnode_links_purge_suppliers(dev->fwnode);
1309 mutex_lock(&fwnode_link_lock);
1310 fwnode_for_each_available_child_node(dev->fwnode, child)
1311 __fw_devlink_pickup_dangling_consumers(child,
1313 __fw_devlink_link_to_consumers(dev);
1314 mutex_unlock(&fwnode_link_lock);
1316 device_remove_file(dev, &dev_attr_waiting_for_supplier);
1318 device_links_write_lock();
1320 list_for_each_entry(link, &dev->links.consumers, s_node) {
1321 if (!(link->flags & DL_FLAG_MANAGED))
1325 * Links created during consumer probe may be in the "consumer
1326 * probe" state to start with if the supplier is still probing
1327 * when they are created and they may become "active" if the
1328 * consumer probe returns first. Skip them here.
1330 if (link->status == DL_STATE_CONSUMER_PROBE ||
1331 link->status == DL_STATE_ACTIVE)
1334 WARN_ON(link->status != DL_STATE_DORMANT);
1335 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
1337 if (link->flags & DL_FLAG_AUTOPROBE_CONSUMER)
1338 driver_deferred_probe_add(link->consumer);
1341 if (defer_sync_state_count)
1342 __device_links_supplier_defer_sync(dev);
1344 __device_links_queue_sync_state(dev, &sync_list);
1346 list_for_each_entry_safe(link, ln, &dev->links.suppliers, c_node) {
1347 struct device *supplier;
1349 if (!(link->flags & DL_FLAG_MANAGED))
1352 supplier = link->supplier;
1353 if (link->flags & DL_FLAG_SYNC_STATE_ONLY) {
1355 * When DL_FLAG_SYNC_STATE_ONLY is set, it means no
1356 * other DL_MANAGED_LINK_FLAGS have been set. So, it's
1357 * save to drop the managed link completely.
1359 device_link_drop_managed(link);
1360 } else if (dev_is_best_effort(dev) &&
1361 link->flags & DL_FLAG_INFERRED &&
1362 link->status != DL_STATE_CONSUMER_PROBE &&
1363 !link->supplier->can_match) {
1365 * When dev_is_best_effort() is true, we ignore device
1366 * links to suppliers that don't have a driver. If the
1367 * consumer device still managed to probe, there's no
1368 * point in maintaining a device link in a weird state
1369 * (consumer probed before supplier). So delete it.
1371 device_link_drop_managed(link);
1373 WARN_ON(link->status != DL_STATE_CONSUMER_PROBE);
1374 WRITE_ONCE(link->status, DL_STATE_ACTIVE);
1378 * This needs to be done even for the deleted
1379 * DL_FLAG_SYNC_STATE_ONLY device link in case it was the last
1380 * device link that was preventing the supplier from getting a
1381 * sync_state() call.
1383 if (defer_sync_state_count)
1384 __device_links_supplier_defer_sync(supplier);
1386 __device_links_queue_sync_state(supplier, &sync_list);
1389 dev->links.status = DL_DEV_DRIVER_BOUND;
1391 device_links_write_unlock();
1393 device_links_flush_sync_list(&sync_list, dev);
1397 * __device_links_no_driver - Update links of a device without a driver.
1398 * @dev: Device without a drvier.
1400 * Delete all non-persistent links from this device to any suppliers.
1402 * Persistent links stay around, but their status is changed to "available",
1403 * unless they already are in the "supplier unbind in progress" state in which
1404 * case they need not be updated.
1406 * Links without the DL_FLAG_MANAGED flag set are ignored.
1408 static void __device_links_no_driver(struct device *dev)
1410 struct device_link *link, *ln;
1412 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
1413 if (!(link->flags & DL_FLAG_MANAGED))
1416 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER) {
1417 device_link_drop_managed(link);
1421 if (link->status != DL_STATE_CONSUMER_PROBE &&
1422 link->status != DL_STATE_ACTIVE)
1425 if (link->supplier->links.status == DL_DEV_DRIVER_BOUND) {
1426 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
1428 WARN_ON(!(link->flags & DL_FLAG_SYNC_STATE_ONLY));
1429 WRITE_ONCE(link->status, DL_STATE_DORMANT);
1433 dev->links.status = DL_DEV_NO_DRIVER;
1437 * device_links_no_driver - Update links after failing driver probe.
1438 * @dev: Device whose driver has just failed to probe.
1440 * Clean up leftover links to consumers for @dev and invoke
1441 * %__device_links_no_driver() to update links to suppliers for it as
1444 * Links without the DL_FLAG_MANAGED flag set are ignored.
1446 void device_links_no_driver(struct device *dev)
1448 struct device_link *link;
1450 device_links_write_lock();
1452 list_for_each_entry(link, &dev->links.consumers, s_node) {
1453 if (!(link->flags & DL_FLAG_MANAGED))
1457 * The probe has failed, so if the status of the link is
1458 * "consumer probe" or "active", it must have been added by
1459 * a probing consumer while this device was still probing.
1460 * Change its state to "dormant", as it represents a valid
1461 * relationship, but it is not functionally meaningful.
1463 if (link->status == DL_STATE_CONSUMER_PROBE ||
1464 link->status == DL_STATE_ACTIVE)
1465 WRITE_ONCE(link->status, DL_STATE_DORMANT);
1468 __device_links_no_driver(dev);
1470 device_links_write_unlock();
1474 * device_links_driver_cleanup - Update links after driver removal.
1475 * @dev: Device whose driver has just gone away.
1477 * Update links to consumers for @dev by changing their status to "dormant" and
1478 * invoke %__device_links_no_driver() to update links to suppliers for it as
1481 * Links without the DL_FLAG_MANAGED flag set are ignored.
1483 void device_links_driver_cleanup(struct device *dev)
1485 struct device_link *link, *ln;
1487 device_links_write_lock();
1489 list_for_each_entry_safe(link, ln, &dev->links.consumers, s_node) {
1490 if (!(link->flags & DL_FLAG_MANAGED))
1493 WARN_ON(link->flags & DL_FLAG_AUTOREMOVE_CONSUMER);
1494 WARN_ON(link->status != DL_STATE_SUPPLIER_UNBIND);
1497 * autoremove the links between this @dev and its consumer
1498 * devices that are not active, i.e. where the link state
1499 * has moved to DL_STATE_SUPPLIER_UNBIND.
1501 if (link->status == DL_STATE_SUPPLIER_UNBIND &&
1502 link->flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
1503 device_link_drop_managed(link);
1505 WRITE_ONCE(link->status, DL_STATE_DORMANT);
1508 list_del_init(&dev->links.defer_sync);
1509 __device_links_no_driver(dev);
1511 device_links_write_unlock();
1515 * device_links_busy - Check if there are any busy links to consumers.
1516 * @dev: Device to check.
1518 * Check each consumer of the device and return 'true' if its link's status
1519 * is one of "consumer probe" or "active" (meaning that the given consumer is
1520 * probing right now or its driver is present). Otherwise, change the link
1521 * state to "supplier unbind" to prevent the consumer from being probed
1522 * successfully going forward.
1524 * Return 'false' if there are no probing or active consumers.
1526 * Links without the DL_FLAG_MANAGED flag set are ignored.
1528 bool device_links_busy(struct device *dev)
1530 struct device_link *link;
1533 device_links_write_lock();
1535 list_for_each_entry(link, &dev->links.consumers, s_node) {
1536 if (!(link->flags & DL_FLAG_MANAGED))
1539 if (link->status == DL_STATE_CONSUMER_PROBE
1540 || link->status == DL_STATE_ACTIVE) {
1544 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
1547 dev->links.status = DL_DEV_UNBINDING;
1549 device_links_write_unlock();
1554 * device_links_unbind_consumers - Force unbind consumers of the given device.
1555 * @dev: Device to unbind the consumers of.
1557 * Walk the list of links to consumers for @dev and if any of them is in the
1558 * "consumer probe" state, wait for all device probes in progress to complete
1561 * If that's not the case, change the status of the link to "supplier unbind"
1562 * and check if the link was in the "active" state. If so, force the consumer
1563 * driver to unbind and start over (the consumer will not re-probe as we have
1564 * changed the state of the link already).
1566 * Links without the DL_FLAG_MANAGED flag set are ignored.
1568 void device_links_unbind_consumers(struct device *dev)
1570 struct device_link *link;
1573 device_links_write_lock();
1575 list_for_each_entry(link, &dev->links.consumers, s_node) {
1576 enum device_link_state status;
1578 if (!(link->flags & DL_FLAG_MANAGED) ||
1579 link->flags & DL_FLAG_SYNC_STATE_ONLY)
1582 status = link->status;
1583 if (status == DL_STATE_CONSUMER_PROBE) {
1584 device_links_write_unlock();
1586 wait_for_device_probe();
1589 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
1590 if (status == DL_STATE_ACTIVE) {
1591 struct device *consumer = link->consumer;
1593 get_device(consumer);
1595 device_links_write_unlock();
1597 device_release_driver_internal(consumer, NULL,
1599 put_device(consumer);
1604 device_links_write_unlock();
1608 * device_links_purge - Delete existing links to other devices.
1609 * @dev: Target device.
1611 static void device_links_purge(struct device *dev)
1613 struct device_link *link, *ln;
1615 if (dev->class == &devlink_class)
1619 * Delete all of the remaining links from this device to any other
1620 * devices (either consumers or suppliers).
1622 device_links_write_lock();
1624 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
1625 WARN_ON(link->status == DL_STATE_ACTIVE);
1626 __device_link_del(&link->kref);
1629 list_for_each_entry_safe_reverse(link, ln, &dev->links.consumers, s_node) {
1630 WARN_ON(link->status != DL_STATE_DORMANT &&
1631 link->status != DL_STATE_NONE);
1632 __device_link_del(&link->kref);
1635 device_links_write_unlock();
1638 #define FW_DEVLINK_FLAGS_PERMISSIVE (DL_FLAG_INFERRED | \
1639 DL_FLAG_SYNC_STATE_ONLY)
1640 #define FW_DEVLINK_FLAGS_ON (DL_FLAG_INFERRED | \
1641 DL_FLAG_AUTOPROBE_CONSUMER)
1642 #define FW_DEVLINK_FLAGS_RPM (FW_DEVLINK_FLAGS_ON | \
1645 static u32 fw_devlink_flags = FW_DEVLINK_FLAGS_ON;
1646 static int __init fw_devlink_setup(char *arg)
1651 if (strcmp(arg, "off") == 0) {
1652 fw_devlink_flags = 0;
1653 } else if (strcmp(arg, "permissive") == 0) {
1654 fw_devlink_flags = FW_DEVLINK_FLAGS_PERMISSIVE;
1655 } else if (strcmp(arg, "on") == 0) {
1656 fw_devlink_flags = FW_DEVLINK_FLAGS_ON;
1657 } else if (strcmp(arg, "rpm") == 0) {
1658 fw_devlink_flags = FW_DEVLINK_FLAGS_RPM;
1662 early_param("fw_devlink", fw_devlink_setup);
1664 static bool fw_devlink_strict;
1665 static int __init fw_devlink_strict_setup(char *arg)
1667 return kstrtobool(arg, &fw_devlink_strict);
1669 early_param("fw_devlink.strict", fw_devlink_strict_setup);
1671 #define FW_DEVLINK_SYNC_STATE_STRICT 0
1672 #define FW_DEVLINK_SYNC_STATE_TIMEOUT 1
1674 #ifndef CONFIG_FW_DEVLINK_SYNC_STATE_TIMEOUT
1675 static int fw_devlink_sync_state;
1677 static int fw_devlink_sync_state = FW_DEVLINK_SYNC_STATE_TIMEOUT;
1680 static int __init fw_devlink_sync_state_setup(char *arg)
1685 if (strcmp(arg, "strict") == 0) {
1686 fw_devlink_sync_state = FW_DEVLINK_SYNC_STATE_STRICT;
1688 } else if (strcmp(arg, "timeout") == 0) {
1689 fw_devlink_sync_state = FW_DEVLINK_SYNC_STATE_TIMEOUT;
1694 early_param("fw_devlink.sync_state", fw_devlink_sync_state_setup);
1696 static inline u32 fw_devlink_get_flags(u8 fwlink_flags)
1698 if (fwlink_flags & FWLINK_FLAG_CYCLE)
1699 return FW_DEVLINK_FLAGS_PERMISSIVE | DL_FLAG_CYCLE;
1701 return fw_devlink_flags;
1704 static bool fw_devlink_is_permissive(void)
1706 return fw_devlink_flags == FW_DEVLINK_FLAGS_PERMISSIVE;
1709 bool fw_devlink_is_strict(void)
1711 return fw_devlink_strict && !fw_devlink_is_permissive();
1714 static void fw_devlink_parse_fwnode(struct fwnode_handle *fwnode)
1716 if (fwnode->flags & FWNODE_FLAG_LINKS_ADDED)
1719 fwnode_call_int_op(fwnode, add_links);
1720 fwnode->flags |= FWNODE_FLAG_LINKS_ADDED;
1723 static void fw_devlink_parse_fwtree(struct fwnode_handle *fwnode)
1725 struct fwnode_handle *child = NULL;
1727 fw_devlink_parse_fwnode(fwnode);
1729 while ((child = fwnode_get_next_available_child_node(fwnode, child)))
1730 fw_devlink_parse_fwtree(child);
1733 static void fw_devlink_relax_link(struct device_link *link)
1735 if (!(link->flags & DL_FLAG_INFERRED))
1738 if (device_link_flag_is_sync_state_only(link->flags))
1741 pm_runtime_drop_link(link);
1742 link->flags = DL_FLAG_MANAGED | FW_DEVLINK_FLAGS_PERMISSIVE;
1743 dev_dbg(link->consumer, "Relaxing link with %s\n",
1744 dev_name(link->supplier));
1747 static int fw_devlink_no_driver(struct device *dev, void *data)
1749 struct device_link *link = to_devlink(dev);
1751 if (!link->supplier->can_match)
1752 fw_devlink_relax_link(link);
1757 void fw_devlink_drivers_done(void)
1759 fw_devlink_drv_reg_done = true;
1760 device_links_write_lock();
1761 class_for_each_device(&devlink_class, NULL, NULL,
1762 fw_devlink_no_driver);
1763 device_links_write_unlock();
1766 static int fw_devlink_dev_sync_state(struct device *dev, void *data)
1768 struct device_link *link = to_devlink(dev);
1769 struct device *sup = link->supplier;
1771 if (!(link->flags & DL_FLAG_MANAGED) ||
1772 link->status == DL_STATE_ACTIVE || sup->state_synced ||
1773 !dev_has_sync_state(sup))
1776 if (fw_devlink_sync_state == FW_DEVLINK_SYNC_STATE_STRICT) {
1777 dev_warn(sup, "sync_state() pending due to %s\n",
1778 dev_name(link->consumer));
1782 if (!list_empty(&sup->links.defer_sync))
1785 dev_warn(sup, "Timed out. Forcing sync_state()\n");
1786 sup->state_synced = true;
1788 list_add_tail(&sup->links.defer_sync, data);
1793 void fw_devlink_probing_done(void)
1795 LIST_HEAD(sync_list);
1797 device_links_write_lock();
1798 class_for_each_device(&devlink_class, NULL, &sync_list,
1799 fw_devlink_dev_sync_state);
1800 device_links_write_unlock();
1801 device_links_flush_sync_list(&sync_list, NULL);
1805 * wait_for_init_devices_probe - Try to probe any device needed for init
1807 * Some devices might need to be probed and bound successfully before the kernel
1808 * boot sequence can finish and move on to init/userspace. For example, a
1809 * network interface might need to be bound to be able to mount a NFS rootfs.
1811 * With fw_devlink=on by default, some of these devices might be blocked from
1812 * probing because they are waiting on a optional supplier that doesn't have a
1813 * driver. While fw_devlink will eventually identify such devices and unblock
1814 * the probing automatically, it might be too late by the time it unblocks the
1815 * probing of devices. For example, the IP4 autoconfig might timeout before
1816 * fw_devlink unblocks probing of the network interface.
1818 * This function is available to temporarily try and probe all devices that have
1819 * a driver even if some of their suppliers haven't been added or don't have
1822 * The drivers can then decide which of the suppliers are optional vs mandatory
1823 * and probe the device if possible. By the time this function returns, all such
1824 * "best effort" probes are guaranteed to be completed. If a device successfully
1825 * probes in this mode, we delete all fw_devlink discovered dependencies of that
1826 * device where the supplier hasn't yet probed successfully because they have to
1827 * be optional dependencies.
1829 * Any devices that didn't successfully probe go back to being treated as if
1830 * this function was never called.
1832 * This also means that some devices that aren't needed for init and could have
1833 * waited for their optional supplier to probe (when the supplier's module is
1834 * loaded later on) would end up probing prematurely with limited functionality.
1835 * So call this function only when boot would fail without it.
1837 void __init wait_for_init_devices_probe(void)
1839 if (!fw_devlink_flags || fw_devlink_is_permissive())
1843 * Wait for all ongoing probes to finish so that the "best effort" is
1844 * only applied to devices that can't probe otherwise.
1846 wait_for_device_probe();
1848 pr_info("Trying to probe devices needed for running init ...\n");
1849 fw_devlink_best_effort = true;
1850 driver_deferred_probe_trigger();
1853 * Wait for all "best effort" probes to finish before going back to
1854 * normal enforcement.
1856 wait_for_device_probe();
1857 fw_devlink_best_effort = false;
1860 static void fw_devlink_unblock_consumers(struct device *dev)
1862 struct device_link *link;
1864 if (!fw_devlink_flags || fw_devlink_is_permissive())
1867 device_links_write_lock();
1868 list_for_each_entry(link, &dev->links.consumers, s_node)
1869 fw_devlink_relax_link(link);
1870 device_links_write_unlock();
1874 static bool fwnode_init_without_drv(struct fwnode_handle *fwnode)
1879 if (!(fwnode->flags & FWNODE_FLAG_INITIALIZED))
1882 dev = get_dev_from_fwnode(fwnode);
1883 ret = !dev || dev->links.status == DL_DEV_NO_DRIVER;
1889 static bool fwnode_ancestor_init_without_drv(struct fwnode_handle *fwnode)
1891 struct fwnode_handle *parent;
1893 fwnode_for_each_parent_node(fwnode, parent) {
1894 if (fwnode_init_without_drv(parent)) {
1895 fwnode_handle_put(parent);
1904 * __fw_devlink_relax_cycles - Relax and mark dependency cycles.
1905 * @con: Potential consumer device.
1906 * @sup_handle: Potential supplier's fwnode.
1908 * Needs to be called with fwnode_lock and device link lock held.
1910 * Check if @sup_handle or any of its ancestors or suppliers direct/indirectly
1911 * depend on @con. This function can detect multiple cyles between @sup_handle
1912 * and @con. When such dependency cycles are found, convert all device links
1913 * created solely by fw_devlink into SYNC_STATE_ONLY device links. Also, mark
1914 * all fwnode links in the cycle with FWLINK_FLAG_CYCLE so that when they are
1915 * converted into a device link in the future, they are created as
1916 * SYNC_STATE_ONLY device links. This is the equivalent of doing
1917 * fw_devlink=permissive just between the devices in the cycle. We need to do
1918 * this because, at this point, fw_devlink can't tell which of these
1919 * dependencies is not a real dependency.
1921 * Return true if one or more cycles were found. Otherwise, return false.
1923 static bool __fw_devlink_relax_cycles(struct device *con,
1924 struct fwnode_handle *sup_handle)
1926 struct device *sup_dev = NULL, *par_dev = NULL;
1927 struct fwnode_link *link;
1928 struct device_link *dev_link;
1935 * We aren't trying to find all cycles. Just a cycle between con and
1938 if (sup_handle->flags & FWNODE_FLAG_VISITED)
1941 sup_handle->flags |= FWNODE_FLAG_VISITED;
1943 sup_dev = get_dev_from_fwnode(sup_handle);
1945 /* Termination condition. */
1946 if (sup_dev == con) {
1952 * If sup_dev is bound to a driver and @con hasn't started binding to a
1953 * driver, sup_dev can't be a consumer of @con. So, no need to check
1956 if (sup_dev && sup_dev->links.status == DL_DEV_DRIVER_BOUND &&
1957 con->links.status == DL_DEV_NO_DRIVER) {
1962 list_for_each_entry(link, &sup_handle->suppliers, c_hook) {
1963 if (__fw_devlink_relax_cycles(con, link->supplier)) {
1964 __fwnode_link_cycle(link);
1970 * Give priority to device parent over fwnode parent to account for any
1971 * quirks in how fwnodes are converted to devices.
1974 par_dev = get_device(sup_dev->parent);
1976 par_dev = fwnode_get_next_parent_dev(sup_handle);
1978 if (par_dev && __fw_devlink_relax_cycles(con, par_dev->fwnode))
1984 list_for_each_entry(dev_link, &sup_dev->links.suppliers, c_node) {
1986 * Ignore a SYNC_STATE_ONLY flag only if it wasn't marked as
1987 * such due to a cycle.
1989 if (device_link_flag_is_sync_state_only(dev_link->flags) &&
1990 !(dev_link->flags & DL_FLAG_CYCLE))
1993 if (__fw_devlink_relax_cycles(con,
1994 dev_link->supplier->fwnode)) {
1995 fw_devlink_relax_link(dev_link);
1996 dev_link->flags |= DL_FLAG_CYCLE;
2002 sup_handle->flags &= ~FWNODE_FLAG_VISITED;
2003 put_device(sup_dev);
2004 put_device(par_dev);
2009 * fw_devlink_create_devlink - Create a device link from a consumer to fwnode
2010 * @con: consumer device for the device link
2011 * @sup_handle: fwnode handle of supplier
2012 * @link: fwnode link that's being converted to a device link
2014 * This function will try to create a device link between the consumer device
2015 * @con and the supplier device represented by @sup_handle.
2017 * The supplier has to be provided as a fwnode because incorrect cycles in
2018 * fwnode links can sometimes cause the supplier device to never be created.
2019 * This function detects such cases and returns an error if it cannot create a
2020 * device link from the consumer to a missing supplier.
2023 * 0 on successfully creating a device link
2024 * -EINVAL if the device link cannot be created as expected
2025 * -EAGAIN if the device link cannot be created right now, but it may be
2026 * possible to do that in the future
2028 static int fw_devlink_create_devlink(struct device *con,
2029 struct fwnode_handle *sup_handle,
2030 struct fwnode_link *link)
2032 struct device *sup_dev;
2036 if (con->fwnode == link->consumer)
2037 flags = fw_devlink_get_flags(link->flags);
2039 flags = FW_DEVLINK_FLAGS_PERMISSIVE;
2042 * In some cases, a device P might also be a supplier to its child node
2043 * C. However, this would defer the probe of C until the probe of P
2044 * completes successfully. This is perfectly fine in the device driver
2045 * model. device_add() doesn't guarantee probe completion of the device
2046 * by the time it returns.
2048 * However, there are a few drivers that assume C will finish probing
2049 * as soon as it's added and before P finishes probing. So, we provide
2050 * a flag to let fw_devlink know not to delay the probe of C until the
2051 * probe of P completes successfully.
2053 * When such a flag is set, we can't create device links where P is the
2054 * supplier of C as that would delay the probe of C.
2056 if (sup_handle->flags & FWNODE_FLAG_NEEDS_CHILD_BOUND_ON_ADD &&
2057 fwnode_is_ancestor_of(sup_handle, con->fwnode))
2061 * SYNC_STATE_ONLY device links don't block probing and supports cycles.
2062 * So cycle detection isn't necessary and shouldn't be done.
2064 if (!(flags & DL_FLAG_SYNC_STATE_ONLY)) {
2065 device_links_write_lock();
2066 if (__fw_devlink_relax_cycles(con, sup_handle)) {
2067 __fwnode_link_cycle(link);
2068 flags = fw_devlink_get_flags(link->flags);
2069 dev_info(con, "Fixed dependency cycle(s) with %pfwf\n",
2072 device_links_write_unlock();
2075 if (sup_handle->flags & FWNODE_FLAG_NOT_DEVICE)
2076 sup_dev = fwnode_get_next_parent_dev(sup_handle);
2078 sup_dev = get_dev_from_fwnode(sup_handle);
2082 * If it's one of those drivers that don't actually bind to
2083 * their device using driver core, then don't wait on this
2084 * supplier device indefinitely.
2086 if (sup_dev->links.status == DL_DEV_NO_DRIVER &&
2087 sup_handle->flags & FWNODE_FLAG_INITIALIZED) {
2089 "Not linking %pfwf - dev might never probe\n",
2095 if (con != sup_dev && !device_link_add(con, sup_dev, flags)) {
2096 dev_err(con, "Failed to create device link (0x%x) with %s\n",
2097 flags, dev_name(sup_dev));
2105 * Supplier or supplier's ancestor already initialized without a struct
2106 * device or being probed by a driver.
2108 if (fwnode_init_without_drv(sup_handle) ||
2109 fwnode_ancestor_init_without_drv(sup_handle)) {
2110 dev_dbg(con, "Not linking %pfwf - might never become dev\n",
2117 put_device(sup_dev);
2122 * __fw_devlink_link_to_consumers - Create device links to consumers of a device
2123 * @dev: Device that needs to be linked to its consumers
2125 * This function looks at all the consumer fwnodes of @dev and creates device
2126 * links between the consumer device and @dev (supplier).
2128 * If the consumer device has not been added yet, then this function creates a
2129 * SYNC_STATE_ONLY link between @dev (supplier) and the closest ancestor device
2130 * of the consumer fwnode. This is necessary to make sure @dev doesn't get a
2131 * sync_state() callback before the real consumer device gets to be added and
2134 * Once device links are created from the real consumer to @dev (supplier), the
2135 * fwnode links are deleted.
2137 static void __fw_devlink_link_to_consumers(struct device *dev)
2139 struct fwnode_handle *fwnode = dev->fwnode;
2140 struct fwnode_link *link, *tmp;
2142 list_for_each_entry_safe(link, tmp, &fwnode->consumers, s_hook) {
2143 struct device *con_dev;
2144 bool own_link = true;
2147 con_dev = get_dev_from_fwnode(link->consumer);
2149 * If consumer device is not available yet, make a "proxy"
2150 * SYNC_STATE_ONLY link from the consumer's parent device to
2151 * the supplier device. This is necessary to make sure the
2152 * supplier doesn't get a sync_state() callback before the real
2153 * consumer can create a device link to the supplier.
2155 * This proxy link step is needed to handle the case where the
2156 * consumer's parent device is added before the supplier.
2159 con_dev = fwnode_get_next_parent_dev(link->consumer);
2161 * However, if the consumer's parent device is also the
2162 * parent of the supplier, don't create a
2163 * consumer-supplier link from the parent to its child
2164 * device. Such a dependency is impossible.
2167 fwnode_is_ancestor_of(con_dev->fwnode, fwnode)) {
2168 put_device(con_dev);
2178 ret = fw_devlink_create_devlink(con_dev, fwnode, link);
2179 put_device(con_dev);
2180 if (!own_link || ret == -EAGAIN)
2183 __fwnode_link_del(link);
2188 * __fw_devlink_link_to_suppliers - Create device links to suppliers of a device
2189 * @dev: The consumer device that needs to be linked to its suppliers
2190 * @fwnode: Root of the fwnode tree that is used to create device links
2192 * This function looks at all the supplier fwnodes of fwnode tree rooted at
2193 * @fwnode and creates device links between @dev (consumer) and all the
2194 * supplier devices of the entire fwnode tree at @fwnode.
2196 * The function creates normal (non-SYNC_STATE_ONLY) device links between @dev
2197 * and the real suppliers of @dev. Once these device links are created, the
2198 * fwnode links are deleted.
2200 * In addition, it also looks at all the suppliers of the entire fwnode tree
2201 * because some of the child devices of @dev that have not been added yet
2202 * (because @dev hasn't probed) might already have their suppliers added to
2203 * driver core. So, this function creates SYNC_STATE_ONLY device links between
2204 * @dev (consumer) and these suppliers to make sure they don't execute their
2205 * sync_state() callbacks before these child devices have a chance to create
2206 * their device links. The fwnode links that correspond to the child devices
2207 * aren't delete because they are needed later to create the device links
2208 * between the real consumer and supplier devices.
2210 static void __fw_devlink_link_to_suppliers(struct device *dev,
2211 struct fwnode_handle *fwnode)
2213 bool own_link = (dev->fwnode == fwnode);
2214 struct fwnode_link *link, *tmp;
2215 struct fwnode_handle *child = NULL;
2217 list_for_each_entry_safe(link, tmp, &fwnode->suppliers, c_hook) {
2219 struct fwnode_handle *sup = link->supplier;
2221 ret = fw_devlink_create_devlink(dev, sup, link);
2222 if (!own_link || ret == -EAGAIN)
2225 __fwnode_link_del(link);
2229 * Make "proxy" SYNC_STATE_ONLY device links to represent the needs of
2230 * all the descendants. This proxy link step is needed to handle the
2231 * case where the supplier is added before the consumer's parent device
2234 while ((child = fwnode_get_next_available_child_node(fwnode, child)))
2235 __fw_devlink_link_to_suppliers(dev, child);
2238 static void fw_devlink_link_device(struct device *dev)
2240 struct fwnode_handle *fwnode = dev->fwnode;
2242 if (!fw_devlink_flags)
2245 fw_devlink_parse_fwtree(fwnode);
2247 mutex_lock(&fwnode_link_lock);
2248 __fw_devlink_link_to_consumers(dev);
2249 __fw_devlink_link_to_suppliers(dev, fwnode);
2250 mutex_unlock(&fwnode_link_lock);
2253 /* Device links support end. */
2255 int (*platform_notify)(struct device *dev) = NULL;
2256 int (*platform_notify_remove)(struct device *dev) = NULL;
2257 static struct kobject *dev_kobj;
2260 static struct kobject *sysfs_dev_char_kobj;
2262 /* /sys/dev/block */
2263 static struct kobject *sysfs_dev_block_kobj;
2265 static DEFINE_MUTEX(device_hotplug_lock);
2267 void lock_device_hotplug(void)
2269 mutex_lock(&device_hotplug_lock);
2272 void unlock_device_hotplug(void)
2274 mutex_unlock(&device_hotplug_lock);
2277 int lock_device_hotplug_sysfs(void)
2279 if (mutex_trylock(&device_hotplug_lock))
2282 /* Avoid busy looping (5 ms of sleep should do). */
2284 return restart_syscall();
2288 static inline int device_is_not_partition(struct device *dev)
2290 return !(dev->type == &part_type);
2293 static inline int device_is_not_partition(struct device *dev)
2299 static void device_platform_notify(struct device *dev)
2301 acpi_device_notify(dev);
2303 software_node_notify(dev);
2305 if (platform_notify)
2306 platform_notify(dev);
2309 static void device_platform_notify_remove(struct device *dev)
2311 acpi_device_notify_remove(dev);
2313 software_node_notify_remove(dev);
2315 if (platform_notify_remove)
2316 platform_notify_remove(dev);
2320 * dev_driver_string - Return a device's driver name, if at all possible
2321 * @dev: struct device to get the name of
2323 * Will return the device's driver's name if it is bound to a device. If
2324 * the device is not bound to a driver, it will return the name of the bus
2325 * it is attached to. If it is not attached to a bus either, an empty
2326 * string will be returned.
2328 const char *dev_driver_string(const struct device *dev)
2330 struct device_driver *drv;
2332 /* dev->driver can change to NULL underneath us because of unbinding,
2333 * so be careful about accessing it. dev->bus and dev->class should
2334 * never change once they are set, so they don't need special care.
2336 drv = READ_ONCE(dev->driver);
2337 return drv ? drv->name : dev_bus_name(dev);
2339 EXPORT_SYMBOL(dev_driver_string);
2341 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
2343 static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
2346 struct device_attribute *dev_attr = to_dev_attr(attr);
2347 struct device *dev = kobj_to_dev(kobj);
2351 ret = dev_attr->show(dev, dev_attr, buf);
2352 if (ret >= (ssize_t)PAGE_SIZE) {
2353 printk("dev_attr_show: %pS returned bad count\n",
2359 static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
2360 const char *buf, size_t count)
2362 struct device_attribute *dev_attr = to_dev_attr(attr);
2363 struct device *dev = kobj_to_dev(kobj);
2366 if (dev_attr->store)
2367 ret = dev_attr->store(dev, dev_attr, buf, count);
2371 static const struct sysfs_ops dev_sysfs_ops = {
2372 .show = dev_attr_show,
2373 .store = dev_attr_store,
2376 #define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
2378 ssize_t device_store_ulong(struct device *dev,
2379 struct device_attribute *attr,
2380 const char *buf, size_t size)
2382 struct dev_ext_attribute *ea = to_ext_attr(attr);
2386 ret = kstrtoul(buf, 0, &new);
2389 *(unsigned long *)(ea->var) = new;
2390 /* Always return full write size even if we didn't consume all */
2393 EXPORT_SYMBOL_GPL(device_store_ulong);
2395 ssize_t device_show_ulong(struct device *dev,
2396 struct device_attribute *attr,
2399 struct dev_ext_attribute *ea = to_ext_attr(attr);
2400 return sysfs_emit(buf, "%lx\n", *(unsigned long *)(ea->var));
2402 EXPORT_SYMBOL_GPL(device_show_ulong);
2404 ssize_t device_store_int(struct device *dev,
2405 struct device_attribute *attr,
2406 const char *buf, size_t size)
2408 struct dev_ext_attribute *ea = to_ext_attr(attr);
2412 ret = kstrtol(buf, 0, &new);
2416 if (new > INT_MAX || new < INT_MIN)
2418 *(int *)(ea->var) = new;
2419 /* Always return full write size even if we didn't consume all */
2422 EXPORT_SYMBOL_GPL(device_store_int);
2424 ssize_t device_show_int(struct device *dev,
2425 struct device_attribute *attr,
2428 struct dev_ext_attribute *ea = to_ext_attr(attr);
2430 return sysfs_emit(buf, "%d\n", *(int *)(ea->var));
2432 EXPORT_SYMBOL_GPL(device_show_int);
2434 ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
2435 const char *buf, size_t size)
2437 struct dev_ext_attribute *ea = to_ext_attr(attr);
2439 if (kstrtobool(buf, ea->var) < 0)
2444 EXPORT_SYMBOL_GPL(device_store_bool);
2446 ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
2449 struct dev_ext_attribute *ea = to_ext_attr(attr);
2451 return sysfs_emit(buf, "%d\n", *(bool *)(ea->var));
2453 EXPORT_SYMBOL_GPL(device_show_bool);
2456 * device_release - free device structure.
2457 * @kobj: device's kobject.
2459 * This is called once the reference count for the object
2460 * reaches 0. We forward the call to the device's release
2461 * method, which should handle actually freeing the structure.
2463 static void device_release(struct kobject *kobj)
2465 struct device *dev = kobj_to_dev(kobj);
2466 struct device_private *p = dev->p;
2469 * Some platform devices are driven without driver attached
2470 * and managed resources may have been acquired. Make sure
2471 * all resources are released.
2473 * Drivers still can add resources into device after device
2474 * is deleted but alive, so release devres here to avoid
2475 * possible memory leak.
2477 devres_release_all(dev);
2479 kfree(dev->dma_range_map);
2483 else if (dev->type && dev->type->release)
2484 dev->type->release(dev);
2485 else if (dev->class && dev->class->dev_release)
2486 dev->class->dev_release(dev);
2488 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",
2493 static const void *device_namespace(const struct kobject *kobj)
2495 const struct device *dev = kobj_to_dev(kobj);
2496 const void *ns = NULL;
2498 if (dev->class && dev->class->ns_type)
2499 ns = dev->class->namespace(dev);
2504 static void device_get_ownership(const struct kobject *kobj, kuid_t *uid, kgid_t *gid)
2506 const struct device *dev = kobj_to_dev(kobj);
2508 if (dev->class && dev->class->get_ownership)
2509 dev->class->get_ownership(dev, uid, gid);
2512 static const struct kobj_type device_ktype = {
2513 .release = device_release,
2514 .sysfs_ops = &dev_sysfs_ops,
2515 .namespace = device_namespace,
2516 .get_ownership = device_get_ownership,
2520 static int dev_uevent_filter(const struct kobject *kobj)
2522 const struct kobj_type *ktype = get_ktype(kobj);
2524 if (ktype == &device_ktype) {
2525 const struct device *dev = kobj_to_dev(kobj);
2534 static const char *dev_uevent_name(const struct kobject *kobj)
2536 const struct device *dev = kobj_to_dev(kobj);
2539 return dev->bus->name;
2541 return dev->class->name;
2545 static int dev_uevent(const struct kobject *kobj, struct kobj_uevent_env *env)
2547 const struct device *dev = kobj_to_dev(kobj);
2550 /* add device node properties if present */
2551 if (MAJOR(dev->devt)) {
2555 kuid_t uid = GLOBAL_ROOT_UID;
2556 kgid_t gid = GLOBAL_ROOT_GID;
2558 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
2559 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
2560 name = device_get_devnode(dev, &mode, &uid, &gid, &tmp);
2562 add_uevent_var(env, "DEVNAME=%s", name);
2564 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
2565 if (!uid_eq(uid, GLOBAL_ROOT_UID))
2566 add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid));
2567 if (!gid_eq(gid, GLOBAL_ROOT_GID))
2568 add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid));
2573 if (dev->type && dev->type->name)
2574 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
2577 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
2579 /* Add common DT information about the device */
2580 of_device_uevent(dev, env);
2582 /* have the bus specific function add its stuff */
2583 if (dev->bus && dev->bus->uevent) {
2584 retval = dev->bus->uevent(dev, env);
2586 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
2587 dev_name(dev), __func__, retval);
2590 /* have the class specific function add its stuff */
2591 if (dev->class && dev->class->dev_uevent) {
2592 retval = dev->class->dev_uevent(dev, env);
2594 pr_debug("device: '%s': %s: class uevent() "
2595 "returned %d\n", dev_name(dev),
2599 /* have the device type specific function add its stuff */
2600 if (dev->type && dev->type->uevent) {
2601 retval = dev->type->uevent(dev, env);
2603 pr_debug("device: '%s': %s: dev_type uevent() "
2604 "returned %d\n", dev_name(dev),
2611 static const struct kset_uevent_ops device_uevent_ops = {
2612 .filter = dev_uevent_filter,
2613 .name = dev_uevent_name,
2614 .uevent = dev_uevent,
2617 static ssize_t uevent_show(struct device *dev, struct device_attribute *attr,
2620 struct kobject *top_kobj;
2622 struct kobj_uevent_env *env = NULL;
2627 /* search the kset, the device belongs to */
2628 top_kobj = &dev->kobj;
2629 while (!top_kobj->kset && top_kobj->parent)
2630 top_kobj = top_kobj->parent;
2631 if (!top_kobj->kset)
2634 kset = top_kobj->kset;
2635 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
2638 /* respect filter */
2639 if (kset->uevent_ops && kset->uevent_ops->filter)
2640 if (!kset->uevent_ops->filter(&dev->kobj))
2643 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
2647 /* let the kset specific function add its keys */
2648 retval = kset->uevent_ops->uevent(&dev->kobj, env);
2652 /* copy keys to file */
2653 for (i = 0; i < env->envp_idx; i++)
2654 len += sysfs_emit_at(buf, len, "%s\n", env->envp[i]);
2660 static ssize_t uevent_store(struct device *dev, struct device_attribute *attr,
2661 const char *buf, size_t count)
2665 rc = kobject_synth_uevent(&dev->kobj, buf, count);
2668 dev_err(dev, "uevent: failed to send synthetic uevent: %d\n", rc);
2674 static DEVICE_ATTR_RW(uevent);
2676 static ssize_t online_show(struct device *dev, struct device_attribute *attr,
2682 val = !dev->offline;
2684 return sysfs_emit(buf, "%u\n", val);
2687 static ssize_t online_store(struct device *dev, struct device_attribute *attr,
2688 const char *buf, size_t count)
2693 ret = kstrtobool(buf, &val);
2697 ret = lock_device_hotplug_sysfs();
2701 ret = val ? device_online(dev) : device_offline(dev);
2702 unlock_device_hotplug();
2703 return ret < 0 ? ret : count;
2705 static DEVICE_ATTR_RW(online);
2707 static ssize_t removable_show(struct device *dev, struct device_attribute *attr,
2712 switch (dev->removable) {
2713 case DEVICE_REMOVABLE:
2722 return sysfs_emit(buf, "%s\n", loc);
2724 static DEVICE_ATTR_RO(removable);
2726 int device_add_groups(struct device *dev, const struct attribute_group **groups)
2728 return sysfs_create_groups(&dev->kobj, groups);
2730 EXPORT_SYMBOL_GPL(device_add_groups);
2732 void device_remove_groups(struct device *dev,
2733 const struct attribute_group **groups)
2735 sysfs_remove_groups(&dev->kobj, groups);
2737 EXPORT_SYMBOL_GPL(device_remove_groups);
2739 union device_attr_group_devres {
2740 const struct attribute_group *group;
2741 const struct attribute_group **groups;
2744 static void devm_attr_group_remove(struct device *dev, void *res)
2746 union device_attr_group_devres *devres = res;
2747 const struct attribute_group *group = devres->group;
2749 dev_dbg(dev, "%s: removing group %p\n", __func__, group);
2750 sysfs_remove_group(&dev->kobj, group);
2753 static void devm_attr_groups_remove(struct device *dev, void *res)
2755 union device_attr_group_devres *devres = res;
2756 const struct attribute_group **groups = devres->groups;
2758 dev_dbg(dev, "%s: removing groups %p\n", __func__, groups);
2759 sysfs_remove_groups(&dev->kobj, groups);
2763 * devm_device_add_group - given a device, create a managed attribute group
2764 * @dev: The device to create the group for
2765 * @grp: The attribute group to create
2767 * This function creates a group for the first time. It will explicitly
2768 * warn and error if any of the attribute files being created already exist.
2770 * Returns 0 on success or error code on failure.
2772 int devm_device_add_group(struct device *dev, const struct attribute_group *grp)
2774 union device_attr_group_devres *devres;
2777 devres = devres_alloc(devm_attr_group_remove,
2778 sizeof(*devres), GFP_KERNEL);
2782 error = sysfs_create_group(&dev->kobj, grp);
2784 devres_free(devres);
2788 devres->group = grp;
2789 devres_add(dev, devres);
2792 EXPORT_SYMBOL_GPL(devm_device_add_group);
2795 * devm_device_add_groups - create a bunch of managed attribute groups
2796 * @dev: The device to create the group for
2797 * @groups: The attribute groups to create, NULL terminated
2799 * This function creates a bunch of managed attribute groups. If an error
2800 * occurs when creating a group, all previously created groups will be
2801 * removed, unwinding everything back to the original state when this
2802 * function was called. It will explicitly warn and error if any of the
2803 * attribute files being created already exist.
2805 * Returns 0 on success or error code from sysfs_create_group on failure.
2807 int devm_device_add_groups(struct device *dev,
2808 const struct attribute_group **groups)
2810 union device_attr_group_devres *devres;
2813 devres = devres_alloc(devm_attr_groups_remove,
2814 sizeof(*devres), GFP_KERNEL);
2818 error = sysfs_create_groups(&dev->kobj, groups);
2820 devres_free(devres);
2824 devres->groups = groups;
2825 devres_add(dev, devres);
2828 EXPORT_SYMBOL_GPL(devm_device_add_groups);
2830 static int device_add_attrs(struct device *dev)
2832 const struct class *class = dev->class;
2833 const struct device_type *type = dev->type;
2837 error = device_add_groups(dev, class->dev_groups);
2843 error = device_add_groups(dev, type->groups);
2845 goto err_remove_class_groups;
2848 error = device_add_groups(dev, dev->groups);
2850 goto err_remove_type_groups;
2852 if (device_supports_offline(dev) && !dev->offline_disabled) {
2853 error = device_create_file(dev, &dev_attr_online);
2855 goto err_remove_dev_groups;
2858 if (fw_devlink_flags && !fw_devlink_is_permissive() && dev->fwnode) {
2859 error = device_create_file(dev, &dev_attr_waiting_for_supplier);
2861 goto err_remove_dev_online;
2864 if (dev_removable_is_valid(dev)) {
2865 error = device_create_file(dev, &dev_attr_removable);
2867 goto err_remove_dev_waiting_for_supplier;
2870 if (dev_add_physical_location(dev)) {
2871 error = device_add_group(dev,
2872 &dev_attr_physical_location_group);
2874 goto err_remove_dev_removable;
2879 err_remove_dev_removable:
2880 device_remove_file(dev, &dev_attr_removable);
2881 err_remove_dev_waiting_for_supplier:
2882 device_remove_file(dev, &dev_attr_waiting_for_supplier);
2883 err_remove_dev_online:
2884 device_remove_file(dev, &dev_attr_online);
2885 err_remove_dev_groups:
2886 device_remove_groups(dev, dev->groups);
2887 err_remove_type_groups:
2889 device_remove_groups(dev, type->groups);
2890 err_remove_class_groups:
2892 device_remove_groups(dev, class->dev_groups);
2897 static void device_remove_attrs(struct device *dev)
2899 const struct class *class = dev->class;
2900 const struct device_type *type = dev->type;
2902 if (dev->physical_location) {
2903 device_remove_group(dev, &dev_attr_physical_location_group);
2904 kfree(dev->physical_location);
2907 device_remove_file(dev, &dev_attr_removable);
2908 device_remove_file(dev, &dev_attr_waiting_for_supplier);
2909 device_remove_file(dev, &dev_attr_online);
2910 device_remove_groups(dev, dev->groups);
2913 device_remove_groups(dev, type->groups);
2916 device_remove_groups(dev, class->dev_groups);
2919 static ssize_t dev_show(struct device *dev, struct device_attribute *attr,
2922 return print_dev_t(buf, dev->devt);
2924 static DEVICE_ATTR_RO(dev);
2927 struct kset *devices_kset;
2930 * devices_kset_move_before - Move device in the devices_kset's list.
2931 * @deva: Device to move.
2932 * @devb: Device @deva should come before.
2934 static void devices_kset_move_before(struct device *deva, struct device *devb)
2938 pr_debug("devices_kset: Moving %s before %s\n",
2939 dev_name(deva), dev_name(devb));
2940 spin_lock(&devices_kset->list_lock);
2941 list_move_tail(&deva->kobj.entry, &devb->kobj.entry);
2942 spin_unlock(&devices_kset->list_lock);
2946 * devices_kset_move_after - Move device in the devices_kset's list.
2947 * @deva: Device to move
2948 * @devb: Device @deva should come after.
2950 static void devices_kset_move_after(struct device *deva, struct device *devb)
2954 pr_debug("devices_kset: Moving %s after %s\n",
2955 dev_name(deva), dev_name(devb));
2956 spin_lock(&devices_kset->list_lock);
2957 list_move(&deva->kobj.entry, &devb->kobj.entry);
2958 spin_unlock(&devices_kset->list_lock);
2962 * devices_kset_move_last - move the device to the end of devices_kset's list.
2963 * @dev: device to move
2965 void devices_kset_move_last(struct device *dev)
2969 pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev));
2970 spin_lock(&devices_kset->list_lock);
2971 list_move_tail(&dev->kobj.entry, &devices_kset->list);
2972 spin_unlock(&devices_kset->list_lock);
2976 * device_create_file - create sysfs attribute file for device.
2978 * @attr: device attribute descriptor.
2980 int device_create_file(struct device *dev,
2981 const struct device_attribute *attr)
2986 WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
2987 "Attribute %s: write permission without 'store'\n",
2989 WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
2990 "Attribute %s: read permission without 'show'\n",
2992 error = sysfs_create_file(&dev->kobj, &attr->attr);
2997 EXPORT_SYMBOL_GPL(device_create_file);
3000 * device_remove_file - remove sysfs attribute file.
3002 * @attr: device attribute descriptor.
3004 void device_remove_file(struct device *dev,
3005 const struct device_attribute *attr)
3008 sysfs_remove_file(&dev->kobj, &attr->attr);
3010 EXPORT_SYMBOL_GPL(device_remove_file);
3013 * device_remove_file_self - remove sysfs attribute file from its own method.
3015 * @attr: device attribute descriptor.
3017 * See kernfs_remove_self() for details.
3019 bool device_remove_file_self(struct device *dev,
3020 const struct device_attribute *attr)
3023 return sysfs_remove_file_self(&dev->kobj, &attr->attr);
3027 EXPORT_SYMBOL_GPL(device_remove_file_self);
3030 * device_create_bin_file - create sysfs binary attribute file for device.
3032 * @attr: device binary attribute descriptor.
3034 int device_create_bin_file(struct device *dev,
3035 const struct bin_attribute *attr)
3037 int error = -EINVAL;
3039 error = sysfs_create_bin_file(&dev->kobj, attr);
3042 EXPORT_SYMBOL_GPL(device_create_bin_file);
3045 * device_remove_bin_file - remove sysfs binary attribute file
3047 * @attr: device binary attribute descriptor.
3049 void device_remove_bin_file(struct device *dev,
3050 const struct bin_attribute *attr)
3053 sysfs_remove_bin_file(&dev->kobj, attr);
3055 EXPORT_SYMBOL_GPL(device_remove_bin_file);
3057 static void klist_children_get(struct klist_node *n)
3059 struct device_private *p = to_device_private_parent(n);
3060 struct device *dev = p->device;
3065 static void klist_children_put(struct klist_node *n)
3067 struct device_private *p = to_device_private_parent(n);
3068 struct device *dev = p->device;
3074 * device_initialize - init device structure.
3077 * This prepares the device for use by other layers by initializing
3079 * It is the first half of device_register(), if called by
3080 * that function, though it can also be called separately, so one
3081 * may use @dev's fields. In particular, get_device()/put_device()
3082 * may be used for reference counting of @dev after calling this
3085 * All fields in @dev must be initialized by the caller to 0, except
3086 * for those explicitly set to some other value. The simplest
3087 * approach is to use kzalloc() to allocate the structure containing
3090 * NOTE: Use put_device() to give up your reference instead of freeing
3091 * @dev directly once you have called this function.
3093 void device_initialize(struct device *dev)
3095 dev->kobj.kset = devices_kset;
3096 kobject_init(&dev->kobj, &device_ktype);
3097 INIT_LIST_HEAD(&dev->dma_pools);
3098 mutex_init(&dev->mutex);
3099 lockdep_set_novalidate_class(&dev->mutex);
3100 spin_lock_init(&dev->devres_lock);
3101 INIT_LIST_HEAD(&dev->devres_head);
3102 device_pm_init(dev);
3103 set_dev_node(dev, NUMA_NO_NODE);
3104 INIT_LIST_HEAD(&dev->links.consumers);
3105 INIT_LIST_HEAD(&dev->links.suppliers);
3106 INIT_LIST_HEAD(&dev->links.defer_sync);
3107 dev->links.status = DL_DEV_NO_DRIVER;
3108 #if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE) || \
3109 defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU) || \
3110 defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL)
3111 dev->dma_coherent = dma_default_coherent;
3113 #ifdef CONFIG_SWIOTLB
3114 dev->dma_io_tlb_mem = &io_tlb_default_mem;
3117 EXPORT_SYMBOL_GPL(device_initialize);
3119 struct kobject *virtual_device_parent(struct device *dev)
3121 static struct kobject *virtual_dir = NULL;
3124 virtual_dir = kobject_create_and_add("virtual",
3125 &devices_kset->kobj);
3131 struct kobject kobj;
3132 const struct class *class;
3135 #define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
3137 static void class_dir_release(struct kobject *kobj)
3139 struct class_dir *dir = to_class_dir(kobj);
3144 struct kobj_ns_type_operations *class_dir_child_ns_type(const struct kobject *kobj)
3146 const struct class_dir *dir = to_class_dir(kobj);
3147 return dir->class->ns_type;
3150 static const struct kobj_type class_dir_ktype = {
3151 .release = class_dir_release,
3152 .sysfs_ops = &kobj_sysfs_ops,
3153 .child_ns_type = class_dir_child_ns_type
3156 static struct kobject *class_dir_create_and_add(struct subsys_private *sp,
3157 struct kobject *parent_kobj)
3159 struct class_dir *dir;
3162 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
3164 return ERR_PTR(-ENOMEM);
3166 dir->class = sp->class;
3167 kobject_init(&dir->kobj, &class_dir_ktype);
3169 dir->kobj.kset = &sp->glue_dirs;
3171 retval = kobject_add(&dir->kobj, parent_kobj, "%s", sp->class->name);
3173 kobject_put(&dir->kobj);
3174 return ERR_PTR(retval);
3179 static DEFINE_MUTEX(gdp_mutex);
3181 static struct kobject *get_device_parent(struct device *dev,
3182 struct device *parent)
3184 struct subsys_private *sp = class_to_subsys(dev->class);
3185 struct kobject *kobj = NULL;
3188 struct kobject *parent_kobj;
3192 * If we have no parent, we live in "virtual".
3193 * Class-devices with a non class-device as parent, live
3194 * in a "glue" directory to prevent namespace collisions.
3197 parent_kobj = virtual_device_parent(dev);
3198 else if (parent->class && !dev->class->ns_type) {
3200 return &parent->kobj;
3202 parent_kobj = &parent->kobj;
3205 mutex_lock(&gdp_mutex);
3207 /* find our class-directory at the parent and reference it */
3208 spin_lock(&sp->glue_dirs.list_lock);
3209 list_for_each_entry(k, &sp->glue_dirs.list, entry)
3210 if (k->parent == parent_kobj) {
3211 kobj = kobject_get(k);
3214 spin_unlock(&sp->glue_dirs.list_lock);
3216 mutex_unlock(&gdp_mutex);
3221 /* or create a new class-directory at the parent device */
3222 k = class_dir_create_and_add(sp, parent_kobj);
3223 /* do not emit an uevent for this simple "glue" directory */
3224 mutex_unlock(&gdp_mutex);
3229 /* subsystems can specify a default root directory for their devices */
3230 if (!parent && dev->bus) {
3231 struct device *dev_root = bus_get_dev_root(dev->bus);
3234 kobj = &dev_root->kobj;
3235 put_device(dev_root);
3241 return &parent->kobj;
3245 static inline bool live_in_glue_dir(struct kobject *kobj,
3248 struct subsys_private *sp;
3251 if (!kobj || !dev->class)
3254 sp = class_to_subsys(dev->class);
3258 if (kobj->kset == &sp->glue_dirs)
3267 static inline struct kobject *get_glue_dir(struct device *dev)
3269 return dev->kobj.parent;
3273 * kobject_has_children - Returns whether a kobject has children.
3274 * @kobj: the object to test
3276 * This will return whether a kobject has other kobjects as children.
3278 * It does NOT account for the presence of attribute files, only sub
3279 * directories. It also assumes there is no concurrent addition or
3280 * removal of such children, and thus relies on external locking.
3282 static inline bool kobject_has_children(struct kobject *kobj)
3284 WARN_ON_ONCE(kref_read(&kobj->kref) == 0);
3286 return kobj->sd && kobj->sd->dir.subdirs;
3290 * make sure cleaning up dir as the last step, we need to make
3291 * sure .release handler of kobject is run with holding the
3294 static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
3298 /* see if we live in a "glue" directory */
3299 if (!live_in_glue_dir(glue_dir, dev))
3302 mutex_lock(&gdp_mutex);
3304 * There is a race condition between removing glue directory
3305 * and adding a new device under the glue directory.
3310 * get_device_parent()
3311 * class_dir_create_and_add()
3312 * kobject_add_internal()
3313 * create_dir() // create glue_dir
3316 * get_device_parent()
3317 * kobject_get() // get glue_dir
3320 * cleanup_glue_dir()
3321 * kobject_del(glue_dir)
3324 * kobject_add_internal()
3325 * create_dir() // in glue_dir
3326 * sysfs_create_dir_ns()
3327 * kernfs_create_dir_ns(sd)
3329 * sysfs_remove_dir() // glue_dir->sd=NULL
3330 * sysfs_put() // free glue_dir->sd
3333 * kernfs_new_node(sd)
3334 * kernfs_get(glue_dir)
3338 * Before CPU1 remove last child device under glue dir, if CPU2 add
3339 * a new device under glue dir, the glue_dir kobject reference count
3340 * will be increase to 2 in kobject_get(k). And CPU2 has been called
3341 * kernfs_create_dir_ns(). Meanwhile, CPU1 call sysfs_remove_dir()
3342 * and sysfs_put(). This result in glue_dir->sd is freed.
3344 * Then the CPU2 will see a stale "empty" but still potentially used
3345 * glue dir around in kernfs_new_node().
3347 * In order to avoid this happening, we also should make sure that
3348 * kernfs_node for glue_dir is released in CPU1 only when refcount
3349 * for glue_dir kobj is 1.
3351 ref = kref_read(&glue_dir->kref);
3352 if (!kobject_has_children(glue_dir) && !--ref)
3353 kobject_del(glue_dir);
3354 kobject_put(glue_dir);
3355 mutex_unlock(&gdp_mutex);
3358 static int device_add_class_symlinks(struct device *dev)
3360 struct device_node *of_node = dev_of_node(dev);
3361 struct subsys_private *sp;
3365 error = sysfs_create_link(&dev->kobj, of_node_kobj(of_node), "of_node");
3367 dev_warn(dev, "Error %d creating of_node link\n",error);
3368 /* An error here doesn't warrant bringing down the device */
3371 sp = class_to_subsys(dev->class);
3375 error = sysfs_create_link(&dev->kobj, &sp->subsys.kobj, "subsystem");
3379 if (dev->parent && device_is_not_partition(dev)) {
3380 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
3386 /* link in the class directory pointing to the device */
3387 error = sysfs_create_link(&sp->subsys.kobj, &dev->kobj, dev_name(dev));
3393 sysfs_remove_link(&dev->kobj, "device");
3395 sysfs_remove_link(&dev->kobj, "subsystem");
3397 sysfs_remove_link(&dev->kobj, "of_node");
3403 static void device_remove_class_symlinks(struct device *dev)
3405 struct subsys_private *sp = class_to_subsys(dev->class);
3407 if (dev_of_node(dev))
3408 sysfs_remove_link(&dev->kobj, "of_node");
3413 if (dev->parent && device_is_not_partition(dev))
3414 sysfs_remove_link(&dev->kobj, "device");
3415 sysfs_remove_link(&dev->kobj, "subsystem");
3416 sysfs_delete_link(&sp->subsys.kobj, &dev->kobj, dev_name(dev));
3421 * dev_set_name - set a device name
3423 * @fmt: format string for the device's name
3425 int dev_set_name(struct device *dev, const char *fmt, ...)
3430 va_start(vargs, fmt);
3431 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
3435 EXPORT_SYMBOL_GPL(dev_set_name);
3437 /* select a /sys/dev/ directory for the device */
3438 static struct kobject *device_to_dev_kobj(struct device *dev)
3440 if (is_blockdev(dev))
3441 return sysfs_dev_block_kobj;
3443 return sysfs_dev_char_kobj;
3446 static int device_create_sys_dev_entry(struct device *dev)
3448 struct kobject *kobj = device_to_dev_kobj(dev);
3453 format_dev_t(devt_str, dev->devt);
3454 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
3460 static void device_remove_sys_dev_entry(struct device *dev)
3462 struct kobject *kobj = device_to_dev_kobj(dev);
3466 format_dev_t(devt_str, dev->devt);
3467 sysfs_remove_link(kobj, devt_str);
3471 static int device_private_init(struct device *dev)
3473 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
3476 dev->p->device = dev;
3477 klist_init(&dev->p->klist_children, klist_children_get,
3478 klist_children_put);
3479 INIT_LIST_HEAD(&dev->p->deferred_probe);
3484 * device_add - add device to device hierarchy.
3487 * This is part 2 of device_register(), though may be called
3488 * separately _iff_ device_initialize() has been called separately.
3490 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
3491 * to the global and sibling lists for the device, then
3492 * adds it to the other relevant subsystems of the driver model.
3494 * Do not call this routine or device_register() more than once for
3495 * any device structure. The driver model core is not designed to work
3496 * with devices that get unregistered and then spring back to life.
3497 * (Among other things, it's very hard to guarantee that all references
3498 * to the previous incarnation of @dev have been dropped.) Allocate
3499 * and register a fresh new struct device instead.
3501 * NOTE: _Never_ directly free @dev after calling this function, even
3502 * if it returned an error! Always use put_device() to give up your
3503 * reference instead.
3505 * Rule of thumb is: if device_add() succeeds, you should call
3506 * device_del() when you want to get rid of it. If device_add() has
3507 * *not* succeeded, use *only* put_device() to drop the reference
3510 int device_add(struct device *dev)
3512 struct subsys_private *sp;
3513 struct device *parent;
3514 struct kobject *kobj;
3515 struct class_interface *class_intf;
3516 int error = -EINVAL;
3517 struct kobject *glue_dir = NULL;
3519 dev = get_device(dev);
3524 error = device_private_init(dev);
3530 * for statically allocated devices, which should all be converted
3531 * some day, we need to initialize the name. We prevent reading back
3532 * the name, and force the use of dev_name()
3534 if (dev->init_name) {
3535 dev_set_name(dev, "%s", dev->init_name);
3536 dev->init_name = NULL;
3539 /* subsystems can specify simple device enumeration */
3540 if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
3541 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
3543 if (!dev_name(dev)) {
3548 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
3550 parent = get_device(dev->parent);
3551 kobj = get_device_parent(dev, parent);
3553 error = PTR_ERR(kobj);
3557 dev->kobj.parent = kobj;
3559 /* use parent numa_node */
3560 if (parent && (dev_to_node(dev) == NUMA_NO_NODE))
3561 set_dev_node(dev, dev_to_node(parent));
3563 /* first, register with generic layer. */
3564 /* we require the name to be set before, and pass NULL */
3565 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
3571 /* notify platform of device entry */
3572 device_platform_notify(dev);
3574 error = device_create_file(dev, &dev_attr_uevent);
3578 error = device_add_class_symlinks(dev);
3581 error = device_add_attrs(dev);
3584 error = bus_add_device(dev);
3587 error = dpm_sysfs_add(dev);
3592 if (MAJOR(dev->devt)) {
3593 error = device_create_file(dev, &dev_attr_dev);
3597 error = device_create_sys_dev_entry(dev);
3601 devtmpfs_create_node(dev);
3604 /* Notify clients of device addition. This call must come
3605 * after dpm_sysfs_add() and before kobject_uevent().
3607 bus_notify(dev, BUS_NOTIFY_ADD_DEVICE);
3608 kobject_uevent(&dev->kobj, KOBJ_ADD);
3611 * Check if any of the other devices (consumers) have been waiting for
3612 * this device (supplier) to be added so that they can create a device
3615 * This needs to happen after device_pm_add() because device_link_add()
3616 * requires the supplier be registered before it's called.
3618 * But this also needs to happen before bus_probe_device() to make sure
3619 * waiting consumers can link to it before the driver is bound to the
3620 * device and the driver sync_state callback is called for this device.
3622 if (dev->fwnode && !dev->fwnode->dev) {
3623 dev->fwnode->dev = dev;
3624 fw_devlink_link_device(dev);
3627 bus_probe_device(dev);
3630 * If all driver registration is done and a newly added device doesn't
3631 * match with any driver, don't block its consumers from probing in
3632 * case the consumer device is able to operate without this supplier.
3634 if (dev->fwnode && fw_devlink_drv_reg_done && !dev->can_match)
3635 fw_devlink_unblock_consumers(dev);
3638 klist_add_tail(&dev->p->knode_parent,
3639 &parent->p->klist_children);
3641 sp = class_to_subsys(dev->class);
3643 mutex_lock(&sp->mutex);
3644 /* tie the class to the device */
3645 klist_add_tail(&dev->p->knode_class, &sp->klist_devices);
3647 /* notify any interfaces that the device is here */
3648 list_for_each_entry(class_intf, &sp->interfaces, node)
3649 if (class_intf->add_dev)
3650 class_intf->add_dev(dev, class_intf);
3651 mutex_unlock(&sp->mutex);
3658 if (MAJOR(dev->devt))
3659 device_remove_file(dev, &dev_attr_dev);
3661 device_pm_remove(dev);
3662 dpm_sysfs_remove(dev);
3665 bus_remove_device(dev);
3667 device_remove_attrs(dev);
3669 device_remove_class_symlinks(dev);
3671 device_remove_file(dev, &dev_attr_uevent);
3673 device_platform_notify_remove(dev);
3674 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
3675 glue_dir = get_glue_dir(dev);
3676 kobject_del(&dev->kobj);
3678 cleanup_glue_dir(dev, glue_dir);
3686 EXPORT_SYMBOL_GPL(device_add);
3689 * device_register - register a device with the system.
3690 * @dev: pointer to the device structure
3692 * This happens in two clean steps - initialize the device
3693 * and add it to the system. The two steps can be called
3694 * separately, but this is the easiest and most common.
3695 * I.e. you should only call the two helpers separately if
3696 * have a clearly defined need to use and refcount the device
3697 * before it is added to the hierarchy.
3699 * For more information, see the kerneldoc for device_initialize()
3702 * NOTE: _Never_ directly free @dev after calling this function, even
3703 * if it returned an error! Always use put_device() to give up the
3704 * reference initialized in this function instead.
3706 int device_register(struct device *dev)
3708 device_initialize(dev);
3709 return device_add(dev);
3711 EXPORT_SYMBOL_GPL(device_register);
3714 * get_device - increment reference count for device.
3717 * This simply forwards the call to kobject_get(), though
3718 * we do take care to provide for the case that we get a NULL
3719 * pointer passed in.
3721 struct device *get_device(struct device *dev)
3723 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
3725 EXPORT_SYMBOL_GPL(get_device);
3728 * put_device - decrement reference count.
3729 * @dev: device in question.
3731 void put_device(struct device *dev)
3733 /* might_sleep(); */
3735 kobject_put(&dev->kobj);
3737 EXPORT_SYMBOL_GPL(put_device);
3739 bool kill_device(struct device *dev)
3742 * Require the device lock and set the "dead" flag to guarantee that
3743 * the update behavior is consistent with the other bitfields near
3744 * it and that we cannot have an asynchronous probe routine trying
3745 * to run while we are tearing out the bus/class/sysfs from
3746 * underneath the device.
3748 device_lock_assert(dev);
3752 dev->p->dead = true;
3755 EXPORT_SYMBOL_GPL(kill_device);
3758 * device_del - delete device from system.
3761 * This is the first part of the device unregistration
3762 * sequence. This removes the device from the lists we control
3763 * from here, has it removed from the other driver model
3764 * subsystems it was added to in device_add(), and removes it
3765 * from the kobject hierarchy.
3767 * NOTE: this should be called manually _iff_ device_add() was
3768 * also called manually.
3770 void device_del(struct device *dev)
3772 struct subsys_private *sp;
3773 struct device *parent = dev->parent;
3774 struct kobject *glue_dir = NULL;
3775 struct class_interface *class_intf;
3776 unsigned int noio_flag;
3782 if (dev->fwnode && dev->fwnode->dev == dev)
3783 dev->fwnode->dev = NULL;
3785 /* Notify clients of device removal. This call must come
3786 * before dpm_sysfs_remove().
3788 noio_flag = memalloc_noio_save();
3789 bus_notify(dev, BUS_NOTIFY_DEL_DEVICE);
3791 dpm_sysfs_remove(dev);
3793 klist_del(&dev->p->knode_parent);
3794 if (MAJOR(dev->devt)) {
3795 devtmpfs_delete_node(dev);
3796 device_remove_sys_dev_entry(dev);
3797 device_remove_file(dev, &dev_attr_dev);
3800 sp = class_to_subsys(dev->class);
3802 device_remove_class_symlinks(dev);
3804 mutex_lock(&sp->mutex);
3805 /* notify any interfaces that the device is now gone */
3806 list_for_each_entry(class_intf, &sp->interfaces, node)
3807 if (class_intf->remove_dev)
3808 class_intf->remove_dev(dev, class_intf);
3809 /* remove the device from the class list */
3810 klist_del(&dev->p->knode_class);
3811 mutex_unlock(&sp->mutex);
3814 device_remove_file(dev, &dev_attr_uevent);
3815 device_remove_attrs(dev);
3816 bus_remove_device(dev);
3817 device_pm_remove(dev);
3818 driver_deferred_probe_del(dev);
3819 device_platform_notify_remove(dev);
3820 device_links_purge(dev);
3822 bus_notify(dev, BUS_NOTIFY_REMOVED_DEVICE);
3823 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
3824 glue_dir = get_glue_dir(dev);
3825 kobject_del(&dev->kobj);
3826 cleanup_glue_dir(dev, glue_dir);
3827 memalloc_noio_restore(noio_flag);
3830 EXPORT_SYMBOL_GPL(device_del);
3833 * device_unregister - unregister device from system.
3834 * @dev: device going away.
3836 * We do this in two parts, like we do device_register(). First,
3837 * we remove it from all the subsystems with device_del(), then
3838 * we decrement the reference count via put_device(). If that
3839 * is the final reference count, the device will be cleaned up
3840 * via device_release() above. Otherwise, the structure will
3841 * stick around until the final reference to the device is dropped.
3843 void device_unregister(struct device *dev)
3845 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
3849 EXPORT_SYMBOL_GPL(device_unregister);
3851 static struct device *prev_device(struct klist_iter *i)
3853 struct klist_node *n = klist_prev(i);
3854 struct device *dev = NULL;
3855 struct device_private *p;
3858 p = to_device_private_parent(n);
3864 static struct device *next_device(struct klist_iter *i)
3866 struct klist_node *n = klist_next(i);
3867 struct device *dev = NULL;
3868 struct device_private *p;
3871 p = to_device_private_parent(n);
3878 * device_get_devnode - path of device node file
3880 * @mode: returned file access mode
3881 * @uid: returned file owner
3882 * @gid: returned file group
3883 * @tmp: possibly allocated string
3885 * Return the relative path of a possible device node.
3886 * Non-default names may need to allocate a memory to compose
3887 * a name. This memory is returned in tmp and needs to be
3888 * freed by the caller.
3890 const char *device_get_devnode(const struct device *dev,
3891 umode_t *mode, kuid_t *uid, kgid_t *gid,
3898 /* the device type may provide a specific name */
3899 if (dev->type && dev->type->devnode)
3900 *tmp = dev->type->devnode(dev, mode, uid, gid);
3904 /* the class may provide a specific name */
3905 if (dev->class && dev->class->devnode)
3906 *tmp = dev->class->devnode(dev, mode);
3910 /* return name without allocation, tmp == NULL */
3911 if (strchr(dev_name(dev), '!') == NULL)
3912 return dev_name(dev);
3914 /* replace '!' in the name with '/' */
3915 s = kstrdup(dev_name(dev), GFP_KERNEL);
3918 strreplace(s, '!', '/');
3923 * device_for_each_child - device child iterator.
3924 * @parent: parent struct device.
3925 * @fn: function to be called for each device.
3926 * @data: data for the callback.
3928 * Iterate over @parent's child devices, and call @fn for each,
3931 * We check the return of @fn each time. If it returns anything
3932 * other than 0, we break out and return that value.
3934 int device_for_each_child(struct device *parent, void *data,
3935 int (*fn)(struct device *dev, void *data))
3937 struct klist_iter i;
3938 struct device *child;
3944 klist_iter_init(&parent->p->klist_children, &i);
3945 while (!error && (child = next_device(&i)))
3946 error = fn(child, data);
3947 klist_iter_exit(&i);
3950 EXPORT_SYMBOL_GPL(device_for_each_child);
3953 * device_for_each_child_reverse - device child iterator in reversed order.
3954 * @parent: parent struct device.
3955 * @fn: function to be called for each device.
3956 * @data: data for the callback.
3958 * Iterate over @parent's child devices, and call @fn for each,
3961 * We check the return of @fn each time. If it returns anything
3962 * other than 0, we break out and return that value.
3964 int device_for_each_child_reverse(struct device *parent, void *data,
3965 int (*fn)(struct device *dev, void *data))
3967 struct klist_iter i;
3968 struct device *child;
3974 klist_iter_init(&parent->p->klist_children, &i);
3975 while ((child = prev_device(&i)) && !error)
3976 error = fn(child, data);
3977 klist_iter_exit(&i);
3980 EXPORT_SYMBOL_GPL(device_for_each_child_reverse);
3983 * device_find_child - device iterator for locating a particular device.
3984 * @parent: parent struct device
3985 * @match: Callback function to check device
3986 * @data: Data to pass to match function
3988 * This is similar to the device_for_each_child() function above, but it
3989 * returns a reference to a device that is 'found' for later use, as
3990 * determined by the @match callback.
3992 * The callback should return 0 if the device doesn't match and non-zero
3993 * if it does. If the callback returns non-zero and a reference to the
3994 * current device can be obtained, this function will return to the caller
3995 * and not iterate over any more devices.
3997 * NOTE: you will need to drop the reference with put_device() after use.
3999 struct device *device_find_child(struct device *parent, void *data,
4000 int (*match)(struct device *dev, void *data))
4002 struct klist_iter i;
4003 struct device *child;
4008 klist_iter_init(&parent->p->klist_children, &i);
4009 while ((child = next_device(&i)))
4010 if (match(child, data) && get_device(child))
4012 klist_iter_exit(&i);
4015 EXPORT_SYMBOL_GPL(device_find_child);
4018 * device_find_child_by_name - device iterator for locating a child device.
4019 * @parent: parent struct device
4020 * @name: name of the child device
4022 * This is similar to the device_find_child() function above, but it
4023 * returns a reference to a device that has the name @name.
4025 * NOTE: you will need to drop the reference with put_device() after use.
4027 struct device *device_find_child_by_name(struct device *parent,
4030 struct klist_iter i;
4031 struct device *child;
4036 klist_iter_init(&parent->p->klist_children, &i);
4037 while ((child = next_device(&i)))
4038 if (sysfs_streq(dev_name(child), name) && get_device(child))
4040 klist_iter_exit(&i);
4043 EXPORT_SYMBOL_GPL(device_find_child_by_name);
4045 static int match_any(struct device *dev, void *unused)
4051 * device_find_any_child - device iterator for locating a child device, if any.
4052 * @parent: parent struct device
4054 * This is similar to the device_find_child() function above, but it
4055 * returns a reference to a child device, if any.
4057 * NOTE: you will need to drop the reference with put_device() after use.
4059 struct device *device_find_any_child(struct device *parent)
4061 return device_find_child(parent, NULL, match_any);
4063 EXPORT_SYMBOL_GPL(device_find_any_child);
4065 int __init devices_init(void)
4067 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
4070 dev_kobj = kobject_create_and_add("dev", NULL);
4073 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
4074 if (!sysfs_dev_block_kobj)
4075 goto block_kobj_err;
4076 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
4077 if (!sysfs_dev_char_kobj)
4083 kobject_put(sysfs_dev_block_kobj);
4085 kobject_put(dev_kobj);
4087 kset_unregister(devices_kset);
4091 static int device_check_offline(struct device *dev, void *not_used)
4095 ret = device_for_each_child(dev, NULL, device_check_offline);
4099 return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
4103 * device_offline - Prepare the device for hot-removal.
4104 * @dev: Device to be put offline.
4106 * Execute the device bus type's .offline() callback, if present, to prepare
4107 * the device for a subsequent hot-removal. If that succeeds, the device must
4108 * not be used until either it is removed or its bus type's .online() callback
4111 * Call under device_hotplug_lock.
4113 int device_offline(struct device *dev)
4117 if (dev->offline_disabled)
4120 ret = device_for_each_child(dev, NULL, device_check_offline);
4125 if (device_supports_offline(dev)) {
4129 ret = dev->bus->offline(dev);
4131 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
4132 dev->offline = true;
4142 * device_online - Put the device back online after successful device_offline().
4143 * @dev: Device to be put back online.
4145 * If device_offline() has been successfully executed for @dev, but the device
4146 * has not been removed subsequently, execute its bus type's .online() callback
4147 * to indicate that the device can be used again.
4149 * Call under device_hotplug_lock.
4151 int device_online(struct device *dev)
4156 if (device_supports_offline(dev)) {
4158 ret = dev->bus->online(dev);
4160 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
4161 dev->offline = false;
4172 struct root_device {
4174 struct module *owner;
4177 static inline struct root_device *to_root_device(struct device *d)
4179 return container_of(d, struct root_device, dev);
4182 static void root_device_release(struct device *dev)
4184 kfree(to_root_device(dev));
4188 * __root_device_register - allocate and register a root device
4189 * @name: root device name
4190 * @owner: owner module of the root device, usually THIS_MODULE
4192 * This function allocates a root device and registers it
4193 * using device_register(). In order to free the returned
4194 * device, use root_device_unregister().
4196 * Root devices are dummy devices which allow other devices
4197 * to be grouped under /sys/devices. Use this function to
4198 * allocate a root device and then use it as the parent of
4199 * any device which should appear under /sys/devices/{name}
4201 * The /sys/devices/{name} directory will also contain a
4202 * 'module' symlink which points to the @owner directory
4205 * Returns &struct device pointer on success, or ERR_PTR() on error.
4207 * Note: You probably want to use root_device_register().
4209 struct device *__root_device_register(const char *name, struct module *owner)
4211 struct root_device *root;
4214 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
4216 return ERR_PTR(err);
4218 err = dev_set_name(&root->dev, "%s", name);
4221 return ERR_PTR(err);
4224 root->dev.release = root_device_release;
4226 err = device_register(&root->dev);
4228 put_device(&root->dev);
4229 return ERR_PTR(err);
4232 #ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
4234 struct module_kobject *mk = &owner->mkobj;
4236 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
4238 device_unregister(&root->dev);
4239 return ERR_PTR(err);
4241 root->owner = owner;
4247 EXPORT_SYMBOL_GPL(__root_device_register);
4250 * root_device_unregister - unregister and free a root device
4251 * @dev: device going away
4253 * This function unregisters and cleans up a device that was created by
4254 * root_device_register().
4256 void root_device_unregister(struct device *dev)
4258 struct root_device *root = to_root_device(dev);
4261 sysfs_remove_link(&root->dev.kobj, "module");
4263 device_unregister(dev);
4265 EXPORT_SYMBOL_GPL(root_device_unregister);
4268 static void device_create_release(struct device *dev)
4270 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
4274 static __printf(6, 0) struct device *
4275 device_create_groups_vargs(const struct class *class, struct device *parent,
4276 dev_t devt, void *drvdata,
4277 const struct attribute_group **groups,
4278 const char *fmt, va_list args)
4280 struct device *dev = NULL;
4281 int retval = -ENODEV;
4283 if (IS_ERR_OR_NULL(class))
4286 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
4292 device_initialize(dev);
4295 dev->parent = parent;
4296 dev->groups = groups;
4297 dev->release = device_create_release;
4298 dev_set_drvdata(dev, drvdata);
4300 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
4304 retval = device_add(dev);
4312 return ERR_PTR(retval);
4316 * device_create - creates a device and registers it with sysfs
4317 * @class: pointer to the struct class that this device should be registered to
4318 * @parent: pointer to the parent struct device of this new device, if any
4319 * @devt: the dev_t for the char device to be added
4320 * @drvdata: the data to be added to the device for callbacks
4321 * @fmt: string for the device's name
4323 * This function can be used by char device classes. A struct device
4324 * will be created in sysfs, registered to the specified class.
4326 * A "dev" file will be created, showing the dev_t for the device, if
4327 * the dev_t is not 0,0.
4328 * If a pointer to a parent struct device is passed in, the newly created
4329 * struct device will be a child of that device in sysfs.
4330 * The pointer to the struct device will be returned from the call.
4331 * Any further sysfs files that might be required can be created using this
4334 * Returns &struct device pointer on success, or ERR_PTR() on error.
4336 * Note: the struct class passed to this function must have previously
4337 * been created with a call to class_create().
4339 struct device *device_create(const struct class *class, struct device *parent,
4340 dev_t devt, void *drvdata, const char *fmt, ...)
4345 va_start(vargs, fmt);
4346 dev = device_create_groups_vargs(class, parent, devt, drvdata, NULL,
4351 EXPORT_SYMBOL_GPL(device_create);
4354 * device_create_with_groups - creates a device and registers it with sysfs
4355 * @class: pointer to the struct class that this device should be registered to
4356 * @parent: pointer to the parent struct device of this new device, if any
4357 * @devt: the dev_t for the char device to be added
4358 * @drvdata: the data to be added to the device for callbacks
4359 * @groups: NULL-terminated list of attribute groups to be created
4360 * @fmt: string for the device's name
4362 * This function can be used by char device classes. A struct device
4363 * will be created in sysfs, registered to the specified class.
4364 * Additional attributes specified in the groups parameter will also
4365 * be created automatically.
4367 * A "dev" file will be created, showing the dev_t for the device, if
4368 * the dev_t is not 0,0.
4369 * If a pointer to a parent struct device is passed in, the newly created
4370 * struct device will be a child of that device in sysfs.
4371 * The pointer to the struct device will be returned from the call.
4372 * Any further sysfs files that might be required can be created using this
4375 * Returns &struct device pointer on success, or ERR_PTR() on error.
4377 * Note: the struct class passed to this function must have previously
4378 * been created with a call to class_create().
4380 struct device *device_create_with_groups(const struct class *class,
4381 struct device *parent, dev_t devt,
4383 const struct attribute_group **groups,
4384 const char *fmt, ...)
4389 va_start(vargs, fmt);
4390 dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
4395 EXPORT_SYMBOL_GPL(device_create_with_groups);
4398 * device_destroy - removes a device that was created with device_create()
4399 * @class: pointer to the struct class that this device was registered with
4400 * @devt: the dev_t of the device that was previously registered
4402 * This call unregisters and cleans up a device that was created with a
4403 * call to device_create().
4405 void device_destroy(const struct class *class, dev_t devt)
4409 dev = class_find_device_by_devt(class, devt);
4412 device_unregister(dev);
4415 EXPORT_SYMBOL_GPL(device_destroy);
4418 * device_rename - renames a device
4419 * @dev: the pointer to the struct device to be renamed
4420 * @new_name: the new name of the device
4422 * It is the responsibility of the caller to provide mutual
4423 * exclusion between two different calls of device_rename
4424 * on the same device to ensure that new_name is valid and
4425 * won't conflict with other devices.
4427 * Note: Don't call this function. Currently, the networking layer calls this
4428 * function, but that will change. The following text from Kay Sievers offers
4431 * Renaming devices is racy at many levels, symlinks and other stuff are not
4432 * replaced atomically, and you get a "move" uevent, but it's not easy to
4433 * connect the event to the old and new device. Device nodes are not renamed at
4434 * all, there isn't even support for that in the kernel now.
4436 * In the meantime, during renaming, your target name might be taken by another
4437 * driver, creating conflicts. Or the old name is taken directly after you
4438 * renamed it -- then you get events for the same DEVPATH, before you even see
4439 * the "move" event. It's just a mess, and nothing new should ever rely on
4440 * kernel device renaming. Besides that, it's not even implemented now for
4441 * other things than (driver-core wise very simple) network devices.
4443 * We are currently about to change network renaming in udev to completely
4444 * disallow renaming of devices in the same namespace as the kernel uses,
4445 * because we can't solve the problems properly, that arise with swapping names
4446 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
4447 * be allowed to some other name than eth[0-9]*, for the aforementioned
4450 * Make up a "real" name in the driver before you register anything, or add
4451 * some other attributes for userspace to find the device, or use udev to add
4452 * symlinks -- but never rename kernel devices later, it's a complete mess. We
4453 * don't even want to get into that and try to implement the missing pieces in
4454 * the core. We really have other pieces to fix in the driver core mess. :)
4456 int device_rename(struct device *dev, const char *new_name)
4458 struct kobject *kobj = &dev->kobj;
4459 char *old_device_name = NULL;
4462 dev = get_device(dev);
4466 dev_dbg(dev, "renaming to %s\n", new_name);
4468 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
4469 if (!old_device_name) {
4475 struct subsys_private *sp = class_to_subsys(dev->class);
4482 error = sysfs_rename_link_ns(&sp->subsys.kobj, kobj, old_device_name,
4483 new_name, kobject_namespace(kobj));
4489 error = kobject_rename(kobj, new_name);
4496 kfree(old_device_name);
4500 EXPORT_SYMBOL_GPL(device_rename);
4502 static int device_move_class_links(struct device *dev,
4503 struct device *old_parent,
4504 struct device *new_parent)
4509 sysfs_remove_link(&dev->kobj, "device");
4511 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
4517 * device_move - moves a device to a new parent
4518 * @dev: the pointer to the struct device to be moved
4519 * @new_parent: the new parent of the device (can be NULL)
4520 * @dpm_order: how to reorder the dpm_list
4522 int device_move(struct device *dev, struct device *new_parent,
4523 enum dpm_order dpm_order)
4526 struct device *old_parent;
4527 struct kobject *new_parent_kobj;
4529 dev = get_device(dev);
4534 new_parent = get_device(new_parent);
4535 new_parent_kobj = get_device_parent(dev, new_parent);
4536 if (IS_ERR(new_parent_kobj)) {
4537 error = PTR_ERR(new_parent_kobj);
4538 put_device(new_parent);
4542 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
4543 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
4544 error = kobject_move(&dev->kobj, new_parent_kobj);
4546 cleanup_glue_dir(dev, new_parent_kobj);
4547 put_device(new_parent);
4550 old_parent = dev->parent;
4551 dev->parent = new_parent;
4553 klist_remove(&dev->p->knode_parent);
4555 klist_add_tail(&dev->p->knode_parent,
4556 &new_parent->p->klist_children);
4557 set_dev_node(dev, dev_to_node(new_parent));
4561 error = device_move_class_links(dev, old_parent, new_parent);
4563 /* We ignore errors on cleanup since we're hosed anyway... */
4564 device_move_class_links(dev, new_parent, old_parent);
4565 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
4567 klist_remove(&dev->p->knode_parent);
4568 dev->parent = old_parent;
4570 klist_add_tail(&dev->p->knode_parent,
4571 &old_parent->p->klist_children);
4572 set_dev_node(dev, dev_to_node(old_parent));
4575 cleanup_glue_dir(dev, new_parent_kobj);
4576 put_device(new_parent);
4580 switch (dpm_order) {
4581 case DPM_ORDER_NONE:
4583 case DPM_ORDER_DEV_AFTER_PARENT:
4584 device_pm_move_after(dev, new_parent);
4585 devices_kset_move_after(dev, new_parent);
4587 case DPM_ORDER_PARENT_BEFORE_DEV:
4588 device_pm_move_before(new_parent, dev);
4589 devices_kset_move_before(new_parent, dev);
4591 case DPM_ORDER_DEV_LAST:
4592 device_pm_move_last(dev);
4593 devices_kset_move_last(dev);
4597 put_device(old_parent);
4603 EXPORT_SYMBOL_GPL(device_move);
4605 static int device_attrs_change_owner(struct device *dev, kuid_t kuid,
4608 struct kobject *kobj = &dev->kobj;
4609 const struct class *class = dev->class;
4610 const struct device_type *type = dev->type;
4615 * Change the device groups of the device class for @dev to
4618 error = sysfs_groups_change_owner(kobj, class->dev_groups, kuid,
4626 * Change the device groups of the device type for @dev to
4629 error = sysfs_groups_change_owner(kobj, type->groups, kuid,
4635 /* Change the device groups of @dev to @kuid/@kgid. */
4636 error = sysfs_groups_change_owner(kobj, dev->groups, kuid, kgid);
4640 if (device_supports_offline(dev) && !dev->offline_disabled) {
4641 /* Change online device attributes of @dev to @kuid/@kgid. */
4642 error = sysfs_file_change_owner(kobj, dev_attr_online.attr.name,
4652 * device_change_owner - change the owner of an existing device.
4654 * @kuid: new owner's kuid
4655 * @kgid: new owner's kgid
4657 * This changes the owner of @dev and its corresponding sysfs entries to
4658 * @kuid/@kgid. This function closely mirrors how @dev was added via driver
4661 * Returns 0 on success or error code on failure.
4663 int device_change_owner(struct device *dev, kuid_t kuid, kgid_t kgid)
4666 struct kobject *kobj = &dev->kobj;
4667 struct subsys_private *sp;
4669 dev = get_device(dev);
4674 * Change the kobject and the default attributes and groups of the
4675 * ktype associated with it to @kuid/@kgid.
4677 error = sysfs_change_owner(kobj, kuid, kgid);
4682 * Change the uevent file for @dev to the new owner. The uevent file
4683 * was created in a separate step when @dev got added and we mirror
4686 error = sysfs_file_change_owner(kobj, dev_attr_uevent.attr.name, kuid,
4692 * Change the device groups, the device groups associated with the
4693 * device class, and the groups associated with the device type of @dev
4696 error = device_attrs_change_owner(dev, kuid, kgid);
4700 error = dpm_sysfs_change_owner(dev, kuid, kgid);
4705 * Change the owner of the symlink located in the class directory of
4706 * the device class associated with @dev which points to the actual
4707 * directory entry for @dev to @kuid/@kgid. This ensures that the
4708 * symlink shows the same permissions as its target.
4710 sp = class_to_subsys(dev->class);
4715 error = sysfs_link_change_owner(&sp->subsys.kobj, &dev->kobj, dev_name(dev), kuid, kgid);
4722 EXPORT_SYMBOL_GPL(device_change_owner);
4725 * device_shutdown - call ->shutdown() on each device to shutdown.
4727 void device_shutdown(void)
4729 struct device *dev, *parent;
4731 wait_for_device_probe();
4732 device_block_probing();
4736 spin_lock(&devices_kset->list_lock);
4738 * Walk the devices list backward, shutting down each in turn.
4739 * Beware that device unplug events may also start pulling
4740 * devices offline, even as the system is shutting down.
4742 while (!list_empty(&devices_kset->list)) {
4743 dev = list_entry(devices_kset->list.prev, struct device,
4747 * hold reference count of device's parent to
4748 * prevent it from being freed because parent's
4749 * lock is to be held
4751 parent = get_device(dev->parent);
4754 * Make sure the device is off the kset list, in the
4755 * event that dev->*->shutdown() doesn't remove it.
4757 list_del_init(&dev->kobj.entry);
4758 spin_unlock(&devices_kset->list_lock);
4760 /* hold lock to avoid race with probe/release */
4762 device_lock(parent);
4765 /* Don't allow any more runtime suspends */
4766 pm_runtime_get_noresume(dev);
4767 pm_runtime_barrier(dev);
4769 if (dev->class && dev->class->shutdown_pre) {
4771 dev_info(dev, "shutdown_pre\n");
4772 dev->class->shutdown_pre(dev);
4774 if (dev->bus && dev->bus->shutdown) {
4776 dev_info(dev, "shutdown\n");
4777 dev->bus->shutdown(dev);
4778 } else if (dev->driver && dev->driver->shutdown) {
4780 dev_info(dev, "shutdown\n");
4781 dev->driver->shutdown(dev);
4786 device_unlock(parent);
4791 spin_lock(&devices_kset->list_lock);
4793 spin_unlock(&devices_kset->list_lock);
4797 * Device logging functions
4800 #ifdef CONFIG_PRINTK
4802 set_dev_info(const struct device *dev, struct dev_printk_info *dev_info)
4806 memset(dev_info, 0, sizeof(*dev_info));
4809 subsys = dev->class->name;
4811 subsys = dev->bus->name;
4815 strscpy(dev_info->subsystem, subsys, sizeof(dev_info->subsystem));
4818 * Add device identifier DEVICE=:
4822 * +sound:card0 subsystem:devname
4824 if (MAJOR(dev->devt)) {
4827 if (strcmp(subsys, "block") == 0)
4832 snprintf(dev_info->device, sizeof(dev_info->device),
4833 "%c%u:%u", c, MAJOR(dev->devt), MINOR(dev->devt));
4834 } else if (strcmp(subsys, "net") == 0) {
4835 struct net_device *net = to_net_dev(dev);
4837 snprintf(dev_info->device, sizeof(dev_info->device),
4838 "n%u", net->ifindex);
4840 snprintf(dev_info->device, sizeof(dev_info->device),
4841 "+%s:%s", subsys, dev_name(dev));
4845 int dev_vprintk_emit(int level, const struct device *dev,
4846 const char *fmt, va_list args)
4848 struct dev_printk_info dev_info;
4850 set_dev_info(dev, &dev_info);
4852 return vprintk_emit(0, level, &dev_info, fmt, args);
4854 EXPORT_SYMBOL(dev_vprintk_emit);
4856 int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
4861 va_start(args, fmt);
4863 r = dev_vprintk_emit(level, dev, fmt, args);
4869 EXPORT_SYMBOL(dev_printk_emit);
4871 static void __dev_printk(const char *level, const struct device *dev,
4872 struct va_format *vaf)
4875 dev_printk_emit(level[1] - '0', dev, "%s %s: %pV",
4876 dev_driver_string(dev), dev_name(dev), vaf);
4878 printk("%s(NULL device *): %pV", level, vaf);
4881 void _dev_printk(const char *level, const struct device *dev,
4882 const char *fmt, ...)
4884 struct va_format vaf;
4887 va_start(args, fmt);
4892 __dev_printk(level, dev, &vaf);
4896 EXPORT_SYMBOL(_dev_printk);
4898 #define define_dev_printk_level(func, kern_level) \
4899 void func(const struct device *dev, const char *fmt, ...) \
4901 struct va_format vaf; \
4904 va_start(args, fmt); \
4909 __dev_printk(kern_level, dev, &vaf); \
4913 EXPORT_SYMBOL(func);
4915 define_dev_printk_level(_dev_emerg, KERN_EMERG);
4916 define_dev_printk_level(_dev_alert, KERN_ALERT);
4917 define_dev_printk_level(_dev_crit, KERN_CRIT);
4918 define_dev_printk_level(_dev_err, KERN_ERR);
4919 define_dev_printk_level(_dev_warn, KERN_WARNING);
4920 define_dev_printk_level(_dev_notice, KERN_NOTICE);
4921 define_dev_printk_level(_dev_info, KERN_INFO);
4926 * dev_err_probe - probe error check and log helper
4927 * @dev: the pointer to the struct device
4928 * @err: error value to test
4929 * @fmt: printf-style format string
4930 * @...: arguments as specified in the format string
4932 * This helper implements common pattern present in probe functions for error
4933 * checking: print debug or error message depending if the error value is
4934 * -EPROBE_DEFER and propagate error upwards.
4935 * In case of -EPROBE_DEFER it sets also defer probe reason, which can be
4936 * checked later by reading devices_deferred debugfs attribute.
4937 * It replaces code sequence::
4939 * if (err != -EPROBE_DEFER)
4940 * dev_err(dev, ...);
4942 * dev_dbg(dev, ...);
4947 * return dev_err_probe(dev, err, ...);
4949 * Note that it is deemed acceptable to use this function for error
4950 * prints during probe even if the @err is known to never be -EPROBE_DEFER.
4951 * The benefit compared to a normal dev_err() is the standardized format
4952 * of the error code and the fact that the error code is returned.
4957 int dev_err_probe(const struct device *dev, int err, const char *fmt, ...)
4959 struct va_format vaf;
4962 va_start(args, fmt);
4966 if (err != -EPROBE_DEFER) {
4967 dev_err(dev, "error %pe: %pV", ERR_PTR(err), &vaf);
4969 device_set_deferred_probe_reason(dev, &vaf);
4970 dev_dbg(dev, "error %pe: %pV", ERR_PTR(err), &vaf);
4977 EXPORT_SYMBOL_GPL(dev_err_probe);
4979 static inline bool fwnode_is_primary(struct fwnode_handle *fwnode)
4981 return fwnode && !IS_ERR(fwnode->secondary);
4985 * set_primary_fwnode - Change the primary firmware node of a given device.
4986 * @dev: Device to handle.
4987 * @fwnode: New primary firmware node of the device.
4989 * Set the device's firmware node pointer to @fwnode, but if a secondary
4990 * firmware node of the device is present, preserve it.
4992 * Valid fwnode cases are:
4993 * - primary --> secondary --> -ENODEV
4994 * - primary --> NULL
4995 * - secondary --> -ENODEV
4998 void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
5000 struct device *parent = dev->parent;
5001 struct fwnode_handle *fn = dev->fwnode;
5004 if (fwnode_is_primary(fn))
5008 WARN_ON(fwnode->secondary);
5009 fwnode->secondary = fn;
5011 dev->fwnode = fwnode;
5013 if (fwnode_is_primary(fn)) {
5014 dev->fwnode = fn->secondary;
5016 /* Skip nullifying fn->secondary if the primary is shared */
5017 if (parent && fn == parent->fwnode)
5020 /* Set fn->secondary = NULL, so fn remains the primary fwnode */
5021 fn->secondary = NULL;
5027 EXPORT_SYMBOL_GPL(set_primary_fwnode);
5030 * set_secondary_fwnode - Change the secondary firmware node of a given device.
5031 * @dev: Device to handle.
5032 * @fwnode: New secondary firmware node of the device.
5034 * If a primary firmware node of the device is present, set its secondary
5035 * pointer to @fwnode. Otherwise, set the device's firmware node pointer to
5038 void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
5041 fwnode->secondary = ERR_PTR(-ENODEV);
5043 if (fwnode_is_primary(dev->fwnode))
5044 dev->fwnode->secondary = fwnode;
5046 dev->fwnode = fwnode;
5048 EXPORT_SYMBOL_GPL(set_secondary_fwnode);
5051 * device_set_of_node_from_dev - reuse device-tree node of another device
5052 * @dev: device whose device-tree node is being set
5053 * @dev2: device whose device-tree node is being reused
5055 * Takes another reference to the new device-tree node after first dropping
5056 * any reference held to the old node.
5058 void device_set_of_node_from_dev(struct device *dev, const struct device *dev2)
5060 of_node_put(dev->of_node);
5061 dev->of_node = of_node_get(dev2->of_node);
5062 dev->of_node_reused = true;
5064 EXPORT_SYMBOL_GPL(device_set_of_node_from_dev);
5066 void device_set_node(struct device *dev, struct fwnode_handle *fwnode)
5068 dev->fwnode = fwnode;
5069 dev->of_node = to_of_node(fwnode);
5071 EXPORT_SYMBOL_GPL(device_set_node);
5073 int device_match_name(struct device *dev, const void *name)
5075 return sysfs_streq(dev_name(dev), name);
5077 EXPORT_SYMBOL_GPL(device_match_name);
5079 int device_match_of_node(struct device *dev, const void *np)
5081 return dev->of_node == np;
5083 EXPORT_SYMBOL_GPL(device_match_of_node);
5085 int device_match_fwnode(struct device *dev, const void *fwnode)
5087 return dev_fwnode(dev) == fwnode;
5089 EXPORT_SYMBOL_GPL(device_match_fwnode);
5091 int device_match_devt(struct device *dev, const void *pdevt)
5093 return dev->devt == *(dev_t *)pdevt;
5095 EXPORT_SYMBOL_GPL(device_match_devt);
5097 int device_match_acpi_dev(struct device *dev, const void *adev)
5099 return ACPI_COMPANION(dev) == adev;
5101 EXPORT_SYMBOL(device_match_acpi_dev);
5103 int device_match_acpi_handle(struct device *dev, const void *handle)
5105 return ACPI_HANDLE(dev) == handle;
5107 EXPORT_SYMBOL(device_match_acpi_handle);
5109 int device_match_any(struct device *dev, const void *unused)
5113 EXPORT_SYMBOL_GPL(device_match_any);