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/kdev_t.h>
21 #include <linux/notifier.h>
23 #include <linux/of_device.h>
24 #include <linux/blkdev.h>
25 #include <linux/mutex.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/netdevice.h>
28 #include <linux/sched/signal.h>
29 #include <linux/sched/mm.h>
30 #include <linux/string_helpers.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)
548 struct device_link *link = to_devlink(dev);
549 struct device *sup = link->supplier;
550 struct device *con = link->consumer;
553 len = max(strlen(dev_bus_name(sup)) + strlen(dev_name(sup)),
554 strlen(dev_bus_name(con)) + strlen(dev_name(con)));
556 len += strlen("supplier:") + 1;
557 buf = kzalloc(len, GFP_KERNEL);
561 ret = sysfs_create_link(&link->link_dev.kobj, &sup->kobj, "supplier");
565 ret = sysfs_create_link(&link->link_dev.kobj, &con->kobj, "consumer");
569 snprintf(buf, len, "consumer:%s:%s", dev_bus_name(con), dev_name(con));
570 ret = sysfs_create_link(&sup->kobj, &link->link_dev.kobj, buf);
574 snprintf(buf, len, "supplier:%s:%s", dev_bus_name(sup), dev_name(sup));
575 ret = sysfs_create_link(&con->kobj, &link->link_dev.kobj, buf);
582 snprintf(buf, len, "consumer:%s:%s", dev_bus_name(con), dev_name(con));
583 sysfs_remove_link(&sup->kobj, buf);
585 sysfs_remove_link(&link->link_dev.kobj, "consumer");
587 sysfs_remove_link(&link->link_dev.kobj, "supplier");
593 static void devlink_remove_symlinks(struct device *dev)
595 struct device_link *link = to_devlink(dev);
597 struct device *sup = link->supplier;
598 struct device *con = link->consumer;
601 sysfs_remove_link(&link->link_dev.kobj, "consumer");
602 sysfs_remove_link(&link->link_dev.kobj, "supplier");
604 len = max(strlen(dev_bus_name(sup)) + strlen(dev_name(sup)),
605 strlen(dev_bus_name(con)) + strlen(dev_name(con)));
607 len += strlen("supplier:") + 1;
608 buf = kzalloc(len, GFP_KERNEL);
610 WARN(1, "Unable to properly free device link symlinks!\n");
614 if (device_is_registered(con)) {
615 snprintf(buf, len, "supplier:%s:%s", dev_bus_name(sup), dev_name(sup));
616 sysfs_remove_link(&con->kobj, buf);
618 snprintf(buf, len, "consumer:%s:%s", dev_bus_name(con), dev_name(con));
619 sysfs_remove_link(&sup->kobj, buf);
623 static struct class_interface devlink_class_intf = {
624 .class = &devlink_class,
625 .add_dev = devlink_add_symlinks,
626 .remove_dev = devlink_remove_symlinks,
629 static int __init devlink_class_init(void)
633 ret = class_register(&devlink_class);
637 ret = class_interface_register(&devlink_class_intf);
639 class_unregister(&devlink_class);
643 postcore_initcall(devlink_class_init);
645 #define DL_MANAGED_LINK_FLAGS (DL_FLAG_AUTOREMOVE_CONSUMER | \
646 DL_FLAG_AUTOREMOVE_SUPPLIER | \
647 DL_FLAG_AUTOPROBE_CONSUMER | \
648 DL_FLAG_SYNC_STATE_ONLY | \
652 #define DL_ADD_VALID_FLAGS (DL_MANAGED_LINK_FLAGS | DL_FLAG_STATELESS | \
653 DL_FLAG_PM_RUNTIME | DL_FLAG_RPM_ACTIVE)
656 * device_link_add - Create a link between two devices.
657 * @consumer: Consumer end of the link.
658 * @supplier: Supplier end of the link.
659 * @flags: Link flags.
661 * The caller is responsible for the proper synchronization of the link creation
662 * with runtime PM. First, setting the DL_FLAG_PM_RUNTIME flag will cause the
663 * runtime PM framework to take the link into account. Second, if the
664 * DL_FLAG_RPM_ACTIVE flag is set in addition to it, the supplier devices will
665 * be forced into the active meta state and reference-counted upon the creation
666 * of the link. If DL_FLAG_PM_RUNTIME is not set, DL_FLAG_RPM_ACTIVE will be
669 * If DL_FLAG_STATELESS is set in @flags, the caller of this function is
670 * expected to release the link returned by it directly with the help of either
671 * device_link_del() or device_link_remove().
673 * If that flag is not set, however, the caller of this function is handing the
674 * management of the link over to the driver core entirely and its return value
675 * can only be used to check whether or not the link is present. In that case,
676 * the DL_FLAG_AUTOREMOVE_CONSUMER and DL_FLAG_AUTOREMOVE_SUPPLIER device link
677 * flags can be used to indicate to the driver core when the link can be safely
678 * deleted. Namely, setting one of them in @flags indicates to the driver core
679 * that the link is not going to be used (by the given caller of this function)
680 * after unbinding the consumer or supplier driver, respectively, from its
681 * device, so the link can be deleted at that point. If none of them is set,
682 * the link will be maintained until one of the devices pointed to by it (either
683 * the consumer or the supplier) is unregistered.
685 * Also, if DL_FLAG_STATELESS, DL_FLAG_AUTOREMOVE_CONSUMER and
686 * DL_FLAG_AUTOREMOVE_SUPPLIER are not set in @flags (that is, a persistent
687 * managed device link is being added), the DL_FLAG_AUTOPROBE_CONSUMER flag can
688 * be used to request the driver core to automatically probe for a consumer
689 * driver after successfully binding a driver to the supplier device.
691 * The combination of DL_FLAG_STATELESS and one of DL_FLAG_AUTOREMOVE_CONSUMER,
692 * DL_FLAG_AUTOREMOVE_SUPPLIER, or DL_FLAG_AUTOPROBE_CONSUMER set in @flags at
693 * the same time is invalid and will cause NULL to be returned upfront.
694 * However, if a device link between the given @consumer and @supplier pair
695 * exists already when this function is called for them, the existing link will
696 * be returned regardless of its current type and status (the link's flags may
697 * be modified then). The caller of this function is then expected to treat
698 * the link as though it has just been created, so (in particular) if
699 * DL_FLAG_STATELESS was passed in @flags, the link needs to be released
700 * explicitly when not needed any more (as stated above).
702 * A side effect of the link creation is re-ordering of dpm_list and the
703 * devices_kset list by moving the consumer device and all devices depending
704 * on it to the ends of these lists (that does not happen to devices that have
705 * not been registered when this function is called).
707 * The supplier device is required to be registered when this function is called
708 * and NULL will be returned if that is not the case. The consumer device need
709 * not be registered, however.
711 struct device_link *device_link_add(struct device *consumer,
712 struct device *supplier, u32 flags)
714 struct device_link *link;
716 if (!consumer || !supplier || consumer == supplier ||
717 flags & ~DL_ADD_VALID_FLAGS ||
718 (flags & DL_FLAG_STATELESS && flags & DL_MANAGED_LINK_FLAGS) ||
719 (flags & DL_FLAG_AUTOPROBE_CONSUMER &&
720 flags & (DL_FLAG_AUTOREMOVE_CONSUMER |
721 DL_FLAG_AUTOREMOVE_SUPPLIER)))
724 if (flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) {
725 if (pm_runtime_get_sync(supplier) < 0) {
726 pm_runtime_put_noidle(supplier);
731 if (!(flags & DL_FLAG_STATELESS))
732 flags |= DL_FLAG_MANAGED;
734 if (flags & DL_FLAG_SYNC_STATE_ONLY &&
735 !device_link_flag_is_sync_state_only(flags))
738 device_links_write_lock();
742 * If the supplier has not been fully registered yet or there is a
743 * reverse (non-SYNC_STATE_ONLY) dependency between the consumer and
744 * the supplier already in the graph, return NULL. If the link is a
745 * SYNC_STATE_ONLY link, we don't check for reverse dependencies
746 * because it only affects sync_state() callbacks.
748 if (!device_pm_initialized(supplier)
749 || (!(flags & DL_FLAG_SYNC_STATE_ONLY) &&
750 device_is_dependent(consumer, supplier))) {
756 * SYNC_STATE_ONLY links are useless once a consumer device has probed.
757 * So, only create it if the consumer hasn't probed yet.
759 if (flags & DL_FLAG_SYNC_STATE_ONLY &&
760 consumer->links.status != DL_DEV_NO_DRIVER &&
761 consumer->links.status != DL_DEV_PROBING) {
767 * DL_FLAG_AUTOREMOVE_SUPPLIER indicates that the link will be needed
768 * longer than for DL_FLAG_AUTOREMOVE_CONSUMER and setting them both
769 * together doesn't make sense, so prefer DL_FLAG_AUTOREMOVE_SUPPLIER.
771 if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
772 flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER;
774 list_for_each_entry(link, &supplier->links.consumers, s_node) {
775 if (link->consumer != consumer)
778 if (link->flags & DL_FLAG_INFERRED &&
779 !(flags & DL_FLAG_INFERRED))
780 link->flags &= ~DL_FLAG_INFERRED;
782 if (flags & DL_FLAG_PM_RUNTIME) {
783 if (!(link->flags & DL_FLAG_PM_RUNTIME)) {
784 pm_runtime_new_link(consumer);
785 link->flags |= DL_FLAG_PM_RUNTIME;
787 if (flags & DL_FLAG_RPM_ACTIVE)
788 refcount_inc(&link->rpm_active);
791 if (flags & DL_FLAG_STATELESS) {
792 kref_get(&link->kref);
793 if (link->flags & DL_FLAG_SYNC_STATE_ONLY &&
794 !(link->flags & DL_FLAG_STATELESS)) {
795 link->flags |= DL_FLAG_STATELESS;
798 link->flags |= DL_FLAG_STATELESS;
804 * If the life time of the link following from the new flags is
805 * longer than indicated by the flags of the existing link,
806 * update the existing link to stay around longer.
808 if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER) {
809 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER) {
810 link->flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER;
811 link->flags |= DL_FLAG_AUTOREMOVE_SUPPLIER;
813 } else if (!(flags & DL_FLAG_AUTOREMOVE_CONSUMER)) {
814 link->flags &= ~(DL_FLAG_AUTOREMOVE_CONSUMER |
815 DL_FLAG_AUTOREMOVE_SUPPLIER);
817 if (!(link->flags & DL_FLAG_MANAGED)) {
818 kref_get(&link->kref);
819 link->flags |= DL_FLAG_MANAGED;
820 device_link_init_status(link, consumer, supplier);
822 if (link->flags & DL_FLAG_SYNC_STATE_ONLY &&
823 !(flags & DL_FLAG_SYNC_STATE_ONLY)) {
824 link->flags &= ~DL_FLAG_SYNC_STATE_ONLY;
831 link = kzalloc(sizeof(*link), GFP_KERNEL);
835 refcount_set(&link->rpm_active, 1);
837 get_device(supplier);
838 link->supplier = supplier;
839 INIT_LIST_HEAD(&link->s_node);
840 get_device(consumer);
841 link->consumer = consumer;
842 INIT_LIST_HEAD(&link->c_node);
844 kref_init(&link->kref);
846 link->link_dev.class = &devlink_class;
847 device_set_pm_not_required(&link->link_dev);
848 dev_set_name(&link->link_dev, "%s:%s--%s:%s",
849 dev_bus_name(supplier), dev_name(supplier),
850 dev_bus_name(consumer), dev_name(consumer));
851 if (device_register(&link->link_dev)) {
852 put_device(&link->link_dev);
857 if (flags & DL_FLAG_PM_RUNTIME) {
858 if (flags & DL_FLAG_RPM_ACTIVE)
859 refcount_inc(&link->rpm_active);
861 pm_runtime_new_link(consumer);
864 /* Determine the initial link state. */
865 if (flags & DL_FLAG_STATELESS)
866 link->status = DL_STATE_NONE;
868 device_link_init_status(link, consumer, supplier);
871 * Some callers expect the link creation during consumer driver probe to
872 * resume the supplier even without DL_FLAG_RPM_ACTIVE.
874 if (link->status == DL_STATE_CONSUMER_PROBE &&
875 flags & DL_FLAG_PM_RUNTIME)
876 pm_runtime_resume(supplier);
878 list_add_tail_rcu(&link->s_node, &supplier->links.consumers);
879 list_add_tail_rcu(&link->c_node, &consumer->links.suppliers);
881 if (flags & DL_FLAG_SYNC_STATE_ONLY) {
883 "Linked as a sync state only consumer to %s\n",
890 * Move the consumer and all of the devices depending on it to the end
891 * of dpm_list and the devices_kset list.
893 * It is necessary to hold dpm_list locked throughout all that or else
894 * we may end up suspending with a wrong ordering of it.
896 device_reorder_to_tail(consumer, NULL);
898 dev_dbg(consumer, "Linked as a consumer to %s\n", dev_name(supplier));
902 device_links_write_unlock();
904 if ((flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) && !link)
905 pm_runtime_put(supplier);
909 EXPORT_SYMBOL_GPL(device_link_add);
911 static void __device_link_del(struct kref *kref)
913 struct device_link *link = container_of(kref, struct device_link, kref);
915 dev_dbg(link->consumer, "Dropping the link to %s\n",
916 dev_name(link->supplier));
918 pm_runtime_drop_link(link);
920 device_link_remove_from_lists(link);
921 device_unregister(&link->link_dev);
924 static void device_link_put_kref(struct device_link *link)
926 if (link->flags & DL_FLAG_STATELESS)
927 kref_put(&link->kref, __device_link_del);
928 else if (!device_is_registered(link->consumer))
929 __device_link_del(&link->kref);
931 WARN(1, "Unable to drop a managed device link reference\n");
935 * device_link_del - Delete a stateless link between two devices.
936 * @link: Device link to delete.
938 * The caller must ensure proper synchronization of this function with runtime
939 * PM. If the link was added multiple times, it needs to be deleted as often.
940 * Care is required for hotplugged devices: Their links are purged on removal
941 * and calling device_link_del() is then no longer allowed.
943 void device_link_del(struct device_link *link)
945 device_links_write_lock();
946 device_link_put_kref(link);
947 device_links_write_unlock();
949 EXPORT_SYMBOL_GPL(device_link_del);
952 * device_link_remove - Delete a stateless link between two devices.
953 * @consumer: Consumer end of the link.
954 * @supplier: Supplier end of the link.
956 * The caller must ensure proper synchronization of this function with runtime
959 void device_link_remove(void *consumer, struct device *supplier)
961 struct device_link *link;
963 if (WARN_ON(consumer == supplier))
966 device_links_write_lock();
968 list_for_each_entry(link, &supplier->links.consumers, s_node) {
969 if (link->consumer == consumer) {
970 device_link_put_kref(link);
975 device_links_write_unlock();
977 EXPORT_SYMBOL_GPL(device_link_remove);
979 static void device_links_missing_supplier(struct device *dev)
981 struct device_link *link;
983 list_for_each_entry(link, &dev->links.suppliers, c_node) {
984 if (link->status != DL_STATE_CONSUMER_PROBE)
987 if (link->supplier->links.status == DL_DEV_DRIVER_BOUND) {
988 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
990 WARN_ON(!(link->flags & DL_FLAG_SYNC_STATE_ONLY));
991 WRITE_ONCE(link->status, DL_STATE_DORMANT);
996 static bool dev_is_best_effort(struct device *dev)
998 return (fw_devlink_best_effort && dev->can_match) ||
999 (dev->fwnode && (dev->fwnode->flags & FWNODE_FLAG_BEST_EFFORT));
1002 static struct fwnode_handle *fwnode_links_check_suppliers(
1003 struct fwnode_handle *fwnode)
1005 struct fwnode_link *link;
1007 if (!fwnode || fw_devlink_is_permissive())
1010 list_for_each_entry(link, &fwnode->suppliers, c_hook)
1011 if (!(link->flags & FWLINK_FLAG_CYCLE))
1012 return link->supplier;
1018 * device_links_check_suppliers - Check presence of supplier drivers.
1019 * @dev: Consumer device.
1021 * Check links from this device to any suppliers. Walk the list of the device's
1022 * links to suppliers and see if all of them are available. If not, simply
1023 * return -EPROBE_DEFER.
1025 * We need to guarantee that the supplier will not go away after the check has
1026 * been positive here. It only can go away in __device_release_driver() and
1027 * that function checks the device's links to consumers. This means we need to
1028 * mark the link as "consumer probe in progress" to make the supplier removal
1029 * wait for us to complete (or bad things may happen).
1031 * Links without the DL_FLAG_MANAGED flag set are ignored.
1033 int device_links_check_suppliers(struct device *dev)
1035 struct device_link *link;
1036 int ret = 0, fwnode_ret = 0;
1037 struct fwnode_handle *sup_fw;
1040 * Device waiting for supplier to become available is not allowed to
1043 mutex_lock(&fwnode_link_lock);
1044 sup_fw = fwnode_links_check_suppliers(dev->fwnode);
1046 if (!dev_is_best_effort(dev)) {
1047 fwnode_ret = -EPROBE_DEFER;
1048 dev_err_probe(dev, -EPROBE_DEFER,
1049 "wait for supplier %pfwf\n", sup_fw);
1051 fwnode_ret = -EAGAIN;
1054 mutex_unlock(&fwnode_link_lock);
1055 if (fwnode_ret == -EPROBE_DEFER)
1058 device_links_write_lock();
1060 list_for_each_entry(link, &dev->links.suppliers, c_node) {
1061 if (!(link->flags & DL_FLAG_MANAGED))
1064 if (link->status != DL_STATE_AVAILABLE &&
1065 !(link->flags & DL_FLAG_SYNC_STATE_ONLY)) {
1067 if (dev_is_best_effort(dev) &&
1068 link->flags & DL_FLAG_INFERRED &&
1069 !link->supplier->can_match) {
1074 device_links_missing_supplier(dev);
1075 dev_err_probe(dev, -EPROBE_DEFER,
1076 "supplier %s not ready\n",
1077 dev_name(link->supplier));
1078 ret = -EPROBE_DEFER;
1081 WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE);
1083 dev->links.status = DL_DEV_PROBING;
1085 device_links_write_unlock();
1087 return ret ? ret : fwnode_ret;
1091 * __device_links_queue_sync_state - Queue a device for sync_state() callback
1092 * @dev: Device to call sync_state() on
1093 * @list: List head to queue the @dev on
1095 * Queues a device for a sync_state() callback when the device links write lock
1096 * isn't held. This allows the sync_state() execution flow to use device links
1097 * APIs. The caller must ensure this function is called with
1098 * device_links_write_lock() held.
1100 * This function does a get_device() to make sure the device is not freed while
1103 * So the caller must also ensure that device_links_flush_sync_list() is called
1104 * as soon as the caller releases device_links_write_lock(). This is necessary
1105 * to make sure the sync_state() is called in a timely fashion and the
1106 * put_device() is called on this device.
1108 static void __device_links_queue_sync_state(struct device *dev,
1109 struct list_head *list)
1111 struct device_link *link;
1113 if (!dev_has_sync_state(dev))
1115 if (dev->state_synced)
1118 list_for_each_entry(link, &dev->links.consumers, s_node) {
1119 if (!(link->flags & DL_FLAG_MANAGED))
1121 if (link->status != DL_STATE_ACTIVE)
1126 * Set the flag here to avoid adding the same device to a list more
1127 * than once. This can happen if new consumers get added to the device
1128 * and probed before the list is flushed.
1130 dev->state_synced = true;
1132 if (WARN_ON(!list_empty(&dev->links.defer_sync)))
1136 list_add_tail(&dev->links.defer_sync, list);
1140 * device_links_flush_sync_list - Call sync_state() on a list of devices
1141 * @list: List of devices to call sync_state() on
1142 * @dont_lock_dev: Device for which lock is already held by the caller
1144 * Calls sync_state() on all the devices that have been queued for it. This
1145 * function is used in conjunction with __device_links_queue_sync_state(). The
1146 * @dont_lock_dev parameter is useful when this function is called from a
1147 * context where a device lock is already held.
1149 static void device_links_flush_sync_list(struct list_head *list,
1150 struct device *dont_lock_dev)
1152 struct device *dev, *tmp;
1154 list_for_each_entry_safe(dev, tmp, list, links.defer_sync) {
1155 list_del_init(&dev->links.defer_sync);
1157 if (dev != dont_lock_dev)
1160 dev_sync_state(dev);
1162 if (dev != dont_lock_dev)
1169 void device_links_supplier_sync_state_pause(void)
1171 device_links_write_lock();
1172 defer_sync_state_count++;
1173 device_links_write_unlock();
1176 void device_links_supplier_sync_state_resume(void)
1178 struct device *dev, *tmp;
1179 LIST_HEAD(sync_list);
1181 device_links_write_lock();
1182 if (!defer_sync_state_count) {
1183 WARN(true, "Unmatched sync_state pause/resume!");
1186 defer_sync_state_count--;
1187 if (defer_sync_state_count)
1190 list_for_each_entry_safe(dev, tmp, &deferred_sync, links.defer_sync) {
1192 * Delete from deferred_sync list before queuing it to
1193 * sync_list because defer_sync is used for both lists.
1195 list_del_init(&dev->links.defer_sync);
1196 __device_links_queue_sync_state(dev, &sync_list);
1199 device_links_write_unlock();
1201 device_links_flush_sync_list(&sync_list, NULL);
1204 static int sync_state_resume_initcall(void)
1206 device_links_supplier_sync_state_resume();
1209 late_initcall(sync_state_resume_initcall);
1211 static void __device_links_supplier_defer_sync(struct device *sup)
1213 if (list_empty(&sup->links.defer_sync) && dev_has_sync_state(sup))
1214 list_add_tail(&sup->links.defer_sync, &deferred_sync);
1217 static void device_link_drop_managed(struct device_link *link)
1219 link->flags &= ~DL_FLAG_MANAGED;
1220 WRITE_ONCE(link->status, DL_STATE_NONE);
1221 kref_put(&link->kref, __device_link_del);
1224 static ssize_t waiting_for_supplier_show(struct device *dev,
1225 struct device_attribute *attr,
1231 mutex_lock(&fwnode_link_lock);
1232 val = !!fwnode_links_check_suppliers(dev->fwnode);
1233 mutex_unlock(&fwnode_link_lock);
1235 return sysfs_emit(buf, "%u\n", val);
1237 static DEVICE_ATTR_RO(waiting_for_supplier);
1240 * device_links_force_bind - Prepares device to be force bound
1241 * @dev: Consumer device.
1243 * device_bind_driver() force binds a device to a driver without calling any
1244 * driver probe functions. So the consumer really isn't going to wait for any
1245 * supplier before it's bound to the driver. We still want the device link
1246 * states to be sensible when this happens.
1248 * In preparation for device_bind_driver(), this function goes through each
1249 * supplier device links and checks if the supplier is bound. If it is, then
1250 * the device link status is set to CONSUMER_PROBE. Otherwise, the device link
1251 * is dropped. Links without the DL_FLAG_MANAGED flag set are ignored.
1253 void device_links_force_bind(struct device *dev)
1255 struct device_link *link, *ln;
1257 device_links_write_lock();
1259 list_for_each_entry_safe(link, ln, &dev->links.suppliers, c_node) {
1260 if (!(link->flags & DL_FLAG_MANAGED))
1263 if (link->status != DL_STATE_AVAILABLE) {
1264 device_link_drop_managed(link);
1267 WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE);
1269 dev->links.status = DL_DEV_PROBING;
1271 device_links_write_unlock();
1275 * device_links_driver_bound - Update device links after probing its driver.
1276 * @dev: Device to update the links for.
1278 * The probe has been successful, so update links from this device to any
1279 * consumers by changing their status to "available".
1281 * Also change the status of @dev's links to suppliers to "active".
1283 * Links without the DL_FLAG_MANAGED flag set are ignored.
1285 void device_links_driver_bound(struct device *dev)
1287 struct device_link *link, *ln;
1288 LIST_HEAD(sync_list);
1291 * If a device binds successfully, it's expected to have created all
1292 * the device links it needs to or make new device links as it needs
1293 * them. So, fw_devlink no longer needs to create device links to any
1294 * of the device's suppliers.
1296 * Also, if a child firmware node of this bound device is not added as a
1297 * device by now, assume it is never going to be added. Make this bound
1298 * device the fallback supplier to the dangling consumers of the child
1299 * firmware node because this bound device is probably implementing the
1300 * child firmware node functionality and we don't want the dangling
1301 * consumers to defer probe indefinitely waiting for a device for the
1302 * child firmware node.
1304 if (dev->fwnode && dev->fwnode->dev == dev) {
1305 struct fwnode_handle *child;
1306 fwnode_links_purge_suppliers(dev->fwnode);
1307 mutex_lock(&fwnode_link_lock);
1308 fwnode_for_each_available_child_node(dev->fwnode, child)
1309 __fw_devlink_pickup_dangling_consumers(child,
1311 __fw_devlink_link_to_consumers(dev);
1312 mutex_unlock(&fwnode_link_lock);
1314 device_remove_file(dev, &dev_attr_waiting_for_supplier);
1316 device_links_write_lock();
1318 list_for_each_entry(link, &dev->links.consumers, s_node) {
1319 if (!(link->flags & DL_FLAG_MANAGED))
1323 * Links created during consumer probe may be in the "consumer
1324 * probe" state to start with if the supplier is still probing
1325 * when they are created and they may become "active" if the
1326 * consumer probe returns first. Skip them here.
1328 if (link->status == DL_STATE_CONSUMER_PROBE ||
1329 link->status == DL_STATE_ACTIVE)
1332 WARN_ON(link->status != DL_STATE_DORMANT);
1333 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
1335 if (link->flags & DL_FLAG_AUTOPROBE_CONSUMER)
1336 driver_deferred_probe_add(link->consumer);
1339 if (defer_sync_state_count)
1340 __device_links_supplier_defer_sync(dev);
1342 __device_links_queue_sync_state(dev, &sync_list);
1344 list_for_each_entry_safe(link, ln, &dev->links.suppliers, c_node) {
1345 struct device *supplier;
1347 if (!(link->flags & DL_FLAG_MANAGED))
1350 supplier = link->supplier;
1351 if (link->flags & DL_FLAG_SYNC_STATE_ONLY) {
1353 * When DL_FLAG_SYNC_STATE_ONLY is set, it means no
1354 * other DL_MANAGED_LINK_FLAGS have been set. So, it's
1355 * save to drop the managed link completely.
1357 device_link_drop_managed(link);
1358 } else if (dev_is_best_effort(dev) &&
1359 link->flags & DL_FLAG_INFERRED &&
1360 link->status != DL_STATE_CONSUMER_PROBE &&
1361 !link->supplier->can_match) {
1363 * When dev_is_best_effort() is true, we ignore device
1364 * links to suppliers that don't have a driver. If the
1365 * consumer device still managed to probe, there's no
1366 * point in maintaining a device link in a weird state
1367 * (consumer probed before supplier). So delete it.
1369 device_link_drop_managed(link);
1371 WARN_ON(link->status != DL_STATE_CONSUMER_PROBE);
1372 WRITE_ONCE(link->status, DL_STATE_ACTIVE);
1376 * This needs to be done even for the deleted
1377 * DL_FLAG_SYNC_STATE_ONLY device link in case it was the last
1378 * device link that was preventing the supplier from getting a
1379 * sync_state() call.
1381 if (defer_sync_state_count)
1382 __device_links_supplier_defer_sync(supplier);
1384 __device_links_queue_sync_state(supplier, &sync_list);
1387 dev->links.status = DL_DEV_DRIVER_BOUND;
1389 device_links_write_unlock();
1391 device_links_flush_sync_list(&sync_list, dev);
1395 * __device_links_no_driver - Update links of a device without a driver.
1396 * @dev: Device without a drvier.
1398 * Delete all non-persistent links from this device to any suppliers.
1400 * Persistent links stay around, but their status is changed to "available",
1401 * unless they already are in the "supplier unbind in progress" state in which
1402 * case they need not be updated.
1404 * Links without the DL_FLAG_MANAGED flag set are ignored.
1406 static void __device_links_no_driver(struct device *dev)
1408 struct device_link *link, *ln;
1410 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
1411 if (!(link->flags & DL_FLAG_MANAGED))
1414 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER) {
1415 device_link_drop_managed(link);
1419 if (link->status != DL_STATE_CONSUMER_PROBE &&
1420 link->status != DL_STATE_ACTIVE)
1423 if (link->supplier->links.status == DL_DEV_DRIVER_BOUND) {
1424 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
1426 WARN_ON(!(link->flags & DL_FLAG_SYNC_STATE_ONLY));
1427 WRITE_ONCE(link->status, DL_STATE_DORMANT);
1431 dev->links.status = DL_DEV_NO_DRIVER;
1435 * device_links_no_driver - Update links after failing driver probe.
1436 * @dev: Device whose driver has just failed to probe.
1438 * Clean up leftover links to consumers for @dev and invoke
1439 * %__device_links_no_driver() to update links to suppliers for it as
1442 * Links without the DL_FLAG_MANAGED flag set are ignored.
1444 void device_links_no_driver(struct device *dev)
1446 struct device_link *link;
1448 device_links_write_lock();
1450 list_for_each_entry(link, &dev->links.consumers, s_node) {
1451 if (!(link->flags & DL_FLAG_MANAGED))
1455 * The probe has failed, so if the status of the link is
1456 * "consumer probe" or "active", it must have been added by
1457 * a probing consumer while this device was still probing.
1458 * Change its state to "dormant", as it represents a valid
1459 * relationship, but it is not functionally meaningful.
1461 if (link->status == DL_STATE_CONSUMER_PROBE ||
1462 link->status == DL_STATE_ACTIVE)
1463 WRITE_ONCE(link->status, DL_STATE_DORMANT);
1466 __device_links_no_driver(dev);
1468 device_links_write_unlock();
1472 * device_links_driver_cleanup - Update links after driver removal.
1473 * @dev: Device whose driver has just gone away.
1475 * Update links to consumers for @dev by changing their status to "dormant" and
1476 * invoke %__device_links_no_driver() to update links to suppliers for it as
1479 * Links without the DL_FLAG_MANAGED flag set are ignored.
1481 void device_links_driver_cleanup(struct device *dev)
1483 struct device_link *link, *ln;
1485 device_links_write_lock();
1487 list_for_each_entry_safe(link, ln, &dev->links.consumers, s_node) {
1488 if (!(link->flags & DL_FLAG_MANAGED))
1491 WARN_ON(link->flags & DL_FLAG_AUTOREMOVE_CONSUMER);
1492 WARN_ON(link->status != DL_STATE_SUPPLIER_UNBIND);
1495 * autoremove the links between this @dev and its consumer
1496 * devices that are not active, i.e. where the link state
1497 * has moved to DL_STATE_SUPPLIER_UNBIND.
1499 if (link->status == DL_STATE_SUPPLIER_UNBIND &&
1500 link->flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
1501 device_link_drop_managed(link);
1503 WRITE_ONCE(link->status, DL_STATE_DORMANT);
1506 list_del_init(&dev->links.defer_sync);
1507 __device_links_no_driver(dev);
1509 device_links_write_unlock();
1513 * device_links_busy - Check if there are any busy links to consumers.
1514 * @dev: Device to check.
1516 * Check each consumer of the device and return 'true' if its link's status
1517 * is one of "consumer probe" or "active" (meaning that the given consumer is
1518 * probing right now or its driver is present). Otherwise, change the link
1519 * state to "supplier unbind" to prevent the consumer from being probed
1520 * successfully going forward.
1522 * Return 'false' if there are no probing or active consumers.
1524 * Links without the DL_FLAG_MANAGED flag set are ignored.
1526 bool device_links_busy(struct device *dev)
1528 struct device_link *link;
1531 device_links_write_lock();
1533 list_for_each_entry(link, &dev->links.consumers, s_node) {
1534 if (!(link->flags & DL_FLAG_MANAGED))
1537 if (link->status == DL_STATE_CONSUMER_PROBE
1538 || link->status == DL_STATE_ACTIVE) {
1542 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
1545 dev->links.status = DL_DEV_UNBINDING;
1547 device_links_write_unlock();
1552 * device_links_unbind_consumers - Force unbind consumers of the given device.
1553 * @dev: Device to unbind the consumers of.
1555 * Walk the list of links to consumers for @dev and if any of them is in the
1556 * "consumer probe" state, wait for all device probes in progress to complete
1559 * If that's not the case, change the status of the link to "supplier unbind"
1560 * and check if the link was in the "active" state. If so, force the consumer
1561 * driver to unbind and start over (the consumer will not re-probe as we have
1562 * changed the state of the link already).
1564 * Links without the DL_FLAG_MANAGED flag set are ignored.
1566 void device_links_unbind_consumers(struct device *dev)
1568 struct device_link *link;
1571 device_links_write_lock();
1573 list_for_each_entry(link, &dev->links.consumers, s_node) {
1574 enum device_link_state status;
1576 if (!(link->flags & DL_FLAG_MANAGED) ||
1577 link->flags & DL_FLAG_SYNC_STATE_ONLY)
1580 status = link->status;
1581 if (status == DL_STATE_CONSUMER_PROBE) {
1582 device_links_write_unlock();
1584 wait_for_device_probe();
1587 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
1588 if (status == DL_STATE_ACTIVE) {
1589 struct device *consumer = link->consumer;
1591 get_device(consumer);
1593 device_links_write_unlock();
1595 device_release_driver_internal(consumer, NULL,
1597 put_device(consumer);
1602 device_links_write_unlock();
1606 * device_links_purge - Delete existing links to other devices.
1607 * @dev: Target device.
1609 static void device_links_purge(struct device *dev)
1611 struct device_link *link, *ln;
1613 if (dev->class == &devlink_class)
1617 * Delete all of the remaining links from this device to any other
1618 * devices (either consumers or suppliers).
1620 device_links_write_lock();
1622 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
1623 WARN_ON(link->status == DL_STATE_ACTIVE);
1624 __device_link_del(&link->kref);
1627 list_for_each_entry_safe_reverse(link, ln, &dev->links.consumers, s_node) {
1628 WARN_ON(link->status != DL_STATE_DORMANT &&
1629 link->status != DL_STATE_NONE);
1630 __device_link_del(&link->kref);
1633 device_links_write_unlock();
1636 #define FW_DEVLINK_FLAGS_PERMISSIVE (DL_FLAG_INFERRED | \
1637 DL_FLAG_SYNC_STATE_ONLY)
1638 #define FW_DEVLINK_FLAGS_ON (DL_FLAG_INFERRED | \
1639 DL_FLAG_AUTOPROBE_CONSUMER)
1640 #define FW_DEVLINK_FLAGS_RPM (FW_DEVLINK_FLAGS_ON | \
1643 static u32 fw_devlink_flags = FW_DEVLINK_FLAGS_ON;
1644 static int __init fw_devlink_setup(char *arg)
1649 if (strcmp(arg, "off") == 0) {
1650 fw_devlink_flags = 0;
1651 } else if (strcmp(arg, "permissive") == 0) {
1652 fw_devlink_flags = FW_DEVLINK_FLAGS_PERMISSIVE;
1653 } else if (strcmp(arg, "on") == 0) {
1654 fw_devlink_flags = FW_DEVLINK_FLAGS_ON;
1655 } else if (strcmp(arg, "rpm") == 0) {
1656 fw_devlink_flags = FW_DEVLINK_FLAGS_RPM;
1660 early_param("fw_devlink", fw_devlink_setup);
1662 static bool fw_devlink_strict;
1663 static int __init fw_devlink_strict_setup(char *arg)
1665 return kstrtobool(arg, &fw_devlink_strict);
1667 early_param("fw_devlink.strict", fw_devlink_strict_setup);
1669 #define FW_DEVLINK_SYNC_STATE_STRICT 0
1670 #define FW_DEVLINK_SYNC_STATE_TIMEOUT 1
1672 #ifndef CONFIG_FW_DEVLINK_SYNC_STATE_TIMEOUT
1673 static int fw_devlink_sync_state;
1675 static int fw_devlink_sync_state = FW_DEVLINK_SYNC_STATE_TIMEOUT;
1678 static int __init fw_devlink_sync_state_setup(char *arg)
1683 if (strcmp(arg, "strict") == 0) {
1684 fw_devlink_sync_state = FW_DEVLINK_SYNC_STATE_STRICT;
1686 } else if (strcmp(arg, "timeout") == 0) {
1687 fw_devlink_sync_state = FW_DEVLINK_SYNC_STATE_TIMEOUT;
1692 early_param("fw_devlink.sync_state", fw_devlink_sync_state_setup);
1694 static inline u32 fw_devlink_get_flags(u8 fwlink_flags)
1696 if (fwlink_flags & FWLINK_FLAG_CYCLE)
1697 return FW_DEVLINK_FLAGS_PERMISSIVE | DL_FLAG_CYCLE;
1699 return fw_devlink_flags;
1702 static bool fw_devlink_is_permissive(void)
1704 return fw_devlink_flags == FW_DEVLINK_FLAGS_PERMISSIVE;
1707 bool fw_devlink_is_strict(void)
1709 return fw_devlink_strict && !fw_devlink_is_permissive();
1712 static void fw_devlink_parse_fwnode(struct fwnode_handle *fwnode)
1714 if (fwnode->flags & FWNODE_FLAG_LINKS_ADDED)
1717 fwnode_call_int_op(fwnode, add_links);
1718 fwnode->flags |= FWNODE_FLAG_LINKS_ADDED;
1721 static void fw_devlink_parse_fwtree(struct fwnode_handle *fwnode)
1723 struct fwnode_handle *child = NULL;
1725 fw_devlink_parse_fwnode(fwnode);
1727 while ((child = fwnode_get_next_available_child_node(fwnode, child)))
1728 fw_devlink_parse_fwtree(child);
1731 static void fw_devlink_relax_link(struct device_link *link)
1733 if (!(link->flags & DL_FLAG_INFERRED))
1736 if (device_link_flag_is_sync_state_only(link->flags))
1739 pm_runtime_drop_link(link);
1740 link->flags = DL_FLAG_MANAGED | FW_DEVLINK_FLAGS_PERMISSIVE;
1741 dev_dbg(link->consumer, "Relaxing link with %s\n",
1742 dev_name(link->supplier));
1745 static int fw_devlink_no_driver(struct device *dev, void *data)
1747 struct device_link *link = to_devlink(dev);
1749 if (!link->supplier->can_match)
1750 fw_devlink_relax_link(link);
1755 void fw_devlink_drivers_done(void)
1757 fw_devlink_drv_reg_done = true;
1758 device_links_write_lock();
1759 class_for_each_device(&devlink_class, NULL, NULL,
1760 fw_devlink_no_driver);
1761 device_links_write_unlock();
1764 static int fw_devlink_dev_sync_state(struct device *dev, void *data)
1766 struct device_link *link = to_devlink(dev);
1767 struct device *sup = link->supplier;
1769 if (!(link->flags & DL_FLAG_MANAGED) ||
1770 link->status == DL_STATE_ACTIVE || sup->state_synced ||
1771 !dev_has_sync_state(sup))
1774 if (fw_devlink_sync_state == FW_DEVLINK_SYNC_STATE_STRICT) {
1775 dev_warn(sup, "sync_state() pending due to %s\n",
1776 dev_name(link->consumer));
1780 if (!list_empty(&sup->links.defer_sync))
1783 dev_warn(sup, "Timed out. Forcing sync_state()\n");
1784 sup->state_synced = true;
1786 list_add_tail(&sup->links.defer_sync, data);
1791 void fw_devlink_probing_done(void)
1793 LIST_HEAD(sync_list);
1795 device_links_write_lock();
1796 class_for_each_device(&devlink_class, NULL, &sync_list,
1797 fw_devlink_dev_sync_state);
1798 device_links_write_unlock();
1799 device_links_flush_sync_list(&sync_list, NULL);
1803 * wait_for_init_devices_probe - Try to probe any device needed for init
1805 * Some devices might need to be probed and bound successfully before the kernel
1806 * boot sequence can finish and move on to init/userspace. For example, a
1807 * network interface might need to be bound to be able to mount a NFS rootfs.
1809 * With fw_devlink=on by default, some of these devices might be blocked from
1810 * probing because they are waiting on a optional supplier that doesn't have a
1811 * driver. While fw_devlink will eventually identify such devices and unblock
1812 * the probing automatically, it might be too late by the time it unblocks the
1813 * probing of devices. For example, the IP4 autoconfig might timeout before
1814 * fw_devlink unblocks probing of the network interface.
1816 * This function is available to temporarily try and probe all devices that have
1817 * a driver even if some of their suppliers haven't been added or don't have
1820 * The drivers can then decide which of the suppliers are optional vs mandatory
1821 * and probe the device if possible. By the time this function returns, all such
1822 * "best effort" probes are guaranteed to be completed. If a device successfully
1823 * probes in this mode, we delete all fw_devlink discovered dependencies of that
1824 * device where the supplier hasn't yet probed successfully because they have to
1825 * be optional dependencies.
1827 * Any devices that didn't successfully probe go back to being treated as if
1828 * this function was never called.
1830 * This also means that some devices that aren't needed for init and could have
1831 * waited for their optional supplier to probe (when the supplier's module is
1832 * loaded later on) would end up probing prematurely with limited functionality.
1833 * So call this function only when boot would fail without it.
1835 void __init wait_for_init_devices_probe(void)
1837 if (!fw_devlink_flags || fw_devlink_is_permissive())
1841 * Wait for all ongoing probes to finish so that the "best effort" is
1842 * only applied to devices that can't probe otherwise.
1844 wait_for_device_probe();
1846 pr_info("Trying to probe devices needed for running init ...\n");
1847 fw_devlink_best_effort = true;
1848 driver_deferred_probe_trigger();
1851 * Wait for all "best effort" probes to finish before going back to
1852 * normal enforcement.
1854 wait_for_device_probe();
1855 fw_devlink_best_effort = false;
1858 static void fw_devlink_unblock_consumers(struct device *dev)
1860 struct device_link *link;
1862 if (!fw_devlink_flags || fw_devlink_is_permissive())
1865 device_links_write_lock();
1866 list_for_each_entry(link, &dev->links.consumers, s_node)
1867 fw_devlink_relax_link(link);
1868 device_links_write_unlock();
1872 static bool fwnode_init_without_drv(struct fwnode_handle *fwnode)
1877 if (!(fwnode->flags & FWNODE_FLAG_INITIALIZED))
1880 dev = get_dev_from_fwnode(fwnode);
1881 ret = !dev || dev->links.status == DL_DEV_NO_DRIVER;
1887 static bool fwnode_ancestor_init_without_drv(struct fwnode_handle *fwnode)
1889 struct fwnode_handle *parent;
1891 fwnode_for_each_parent_node(fwnode, parent) {
1892 if (fwnode_init_without_drv(parent)) {
1893 fwnode_handle_put(parent);
1902 * __fw_devlink_relax_cycles - Relax and mark dependency cycles.
1903 * @con: Potential consumer device.
1904 * @sup_handle: Potential supplier's fwnode.
1906 * Needs to be called with fwnode_lock and device link lock held.
1908 * Check if @sup_handle or any of its ancestors or suppliers direct/indirectly
1909 * depend on @con. This function can detect multiple cyles between @sup_handle
1910 * and @con. When such dependency cycles are found, convert all device links
1911 * created solely by fw_devlink into SYNC_STATE_ONLY device links. Also, mark
1912 * all fwnode links in the cycle with FWLINK_FLAG_CYCLE so that when they are
1913 * converted into a device link in the future, they are created as
1914 * SYNC_STATE_ONLY device links. This is the equivalent of doing
1915 * fw_devlink=permissive just between the devices in the cycle. We need to do
1916 * this because, at this point, fw_devlink can't tell which of these
1917 * dependencies is not a real dependency.
1919 * Return true if one or more cycles were found. Otherwise, return false.
1921 static bool __fw_devlink_relax_cycles(struct device *con,
1922 struct fwnode_handle *sup_handle)
1924 struct device *sup_dev = NULL, *par_dev = NULL;
1925 struct fwnode_link *link;
1926 struct device_link *dev_link;
1933 * We aren't trying to find all cycles. Just a cycle between con and
1936 if (sup_handle->flags & FWNODE_FLAG_VISITED)
1939 sup_handle->flags |= FWNODE_FLAG_VISITED;
1941 sup_dev = get_dev_from_fwnode(sup_handle);
1943 /* Termination condition. */
1944 if (sup_dev == con) {
1950 * If sup_dev is bound to a driver and @con hasn't started binding to a
1951 * driver, sup_dev can't be a consumer of @con. So, no need to check
1954 if (sup_dev && sup_dev->links.status == DL_DEV_DRIVER_BOUND &&
1955 con->links.status == DL_DEV_NO_DRIVER) {
1960 list_for_each_entry(link, &sup_handle->suppliers, c_hook) {
1961 if (__fw_devlink_relax_cycles(con, link->supplier)) {
1962 __fwnode_link_cycle(link);
1968 * Give priority to device parent over fwnode parent to account for any
1969 * quirks in how fwnodes are converted to devices.
1972 par_dev = get_device(sup_dev->parent);
1974 par_dev = fwnode_get_next_parent_dev(sup_handle);
1976 if (par_dev && __fw_devlink_relax_cycles(con, par_dev->fwnode))
1982 list_for_each_entry(dev_link, &sup_dev->links.suppliers, c_node) {
1984 * Ignore a SYNC_STATE_ONLY flag only if it wasn't marked as
1985 * such due to a cycle.
1987 if (device_link_flag_is_sync_state_only(dev_link->flags) &&
1988 !(dev_link->flags & DL_FLAG_CYCLE))
1991 if (__fw_devlink_relax_cycles(con,
1992 dev_link->supplier->fwnode)) {
1993 fw_devlink_relax_link(dev_link);
1994 dev_link->flags |= DL_FLAG_CYCLE;
2000 sup_handle->flags &= ~FWNODE_FLAG_VISITED;
2001 put_device(sup_dev);
2002 put_device(par_dev);
2007 * fw_devlink_create_devlink - Create a device link from a consumer to fwnode
2008 * @con: consumer device for the device link
2009 * @sup_handle: fwnode handle of supplier
2010 * @link: fwnode link that's being converted to a device link
2012 * This function will try to create a device link between the consumer device
2013 * @con and the supplier device represented by @sup_handle.
2015 * The supplier has to be provided as a fwnode because incorrect cycles in
2016 * fwnode links can sometimes cause the supplier device to never be created.
2017 * This function detects such cases and returns an error if it cannot create a
2018 * device link from the consumer to a missing supplier.
2021 * 0 on successfully creating a device link
2022 * -EINVAL if the device link cannot be created as expected
2023 * -EAGAIN if the device link cannot be created right now, but it may be
2024 * possible to do that in the future
2026 static int fw_devlink_create_devlink(struct device *con,
2027 struct fwnode_handle *sup_handle,
2028 struct fwnode_link *link)
2030 struct device *sup_dev;
2034 if (con->fwnode == link->consumer)
2035 flags = fw_devlink_get_flags(link->flags);
2037 flags = FW_DEVLINK_FLAGS_PERMISSIVE;
2040 * In some cases, a device P might also be a supplier to its child node
2041 * C. However, this would defer the probe of C until the probe of P
2042 * completes successfully. This is perfectly fine in the device driver
2043 * model. device_add() doesn't guarantee probe completion of the device
2044 * by the time it returns.
2046 * However, there are a few drivers that assume C will finish probing
2047 * as soon as it's added and before P finishes probing. So, we provide
2048 * a flag to let fw_devlink know not to delay the probe of C until the
2049 * probe of P completes successfully.
2051 * When such a flag is set, we can't create device links where P is the
2052 * supplier of C as that would delay the probe of C.
2054 if (sup_handle->flags & FWNODE_FLAG_NEEDS_CHILD_BOUND_ON_ADD &&
2055 fwnode_is_ancestor_of(sup_handle, con->fwnode))
2059 * SYNC_STATE_ONLY device links don't block probing and supports cycles.
2060 * So cycle detection isn't necessary and shouldn't be done.
2062 if (!(flags & DL_FLAG_SYNC_STATE_ONLY)) {
2063 device_links_write_lock();
2064 if (__fw_devlink_relax_cycles(con, sup_handle)) {
2065 __fwnode_link_cycle(link);
2066 flags = fw_devlink_get_flags(link->flags);
2067 dev_info(con, "Fixed dependency cycle(s) with %pfwf\n",
2070 device_links_write_unlock();
2073 if (sup_handle->flags & FWNODE_FLAG_NOT_DEVICE)
2074 sup_dev = fwnode_get_next_parent_dev(sup_handle);
2076 sup_dev = get_dev_from_fwnode(sup_handle);
2080 * If it's one of those drivers that don't actually bind to
2081 * their device using driver core, then don't wait on this
2082 * supplier device indefinitely.
2084 if (sup_dev->links.status == DL_DEV_NO_DRIVER &&
2085 sup_handle->flags & FWNODE_FLAG_INITIALIZED) {
2087 "Not linking %pfwf - dev might never probe\n",
2093 if (con != sup_dev && !device_link_add(con, sup_dev, flags)) {
2094 dev_err(con, "Failed to create device link (0x%x) with %s\n",
2095 flags, dev_name(sup_dev));
2103 * Supplier or supplier's ancestor already initialized without a struct
2104 * device or being probed by a driver.
2106 if (fwnode_init_without_drv(sup_handle) ||
2107 fwnode_ancestor_init_without_drv(sup_handle)) {
2108 dev_dbg(con, "Not linking %pfwf - might never become dev\n",
2115 put_device(sup_dev);
2120 * __fw_devlink_link_to_consumers - Create device links to consumers of a device
2121 * @dev: Device that needs to be linked to its consumers
2123 * This function looks at all the consumer fwnodes of @dev and creates device
2124 * links between the consumer device and @dev (supplier).
2126 * If the consumer device has not been added yet, then this function creates a
2127 * SYNC_STATE_ONLY link between @dev (supplier) and the closest ancestor device
2128 * of the consumer fwnode. This is necessary to make sure @dev doesn't get a
2129 * sync_state() callback before the real consumer device gets to be added and
2132 * Once device links are created from the real consumer to @dev (supplier), the
2133 * fwnode links are deleted.
2135 static void __fw_devlink_link_to_consumers(struct device *dev)
2137 struct fwnode_handle *fwnode = dev->fwnode;
2138 struct fwnode_link *link, *tmp;
2140 list_for_each_entry_safe(link, tmp, &fwnode->consumers, s_hook) {
2141 struct device *con_dev;
2142 bool own_link = true;
2145 con_dev = get_dev_from_fwnode(link->consumer);
2147 * If consumer device is not available yet, make a "proxy"
2148 * SYNC_STATE_ONLY link from the consumer's parent device to
2149 * the supplier device. This is necessary to make sure the
2150 * supplier doesn't get a sync_state() callback before the real
2151 * consumer can create a device link to the supplier.
2153 * This proxy link step is needed to handle the case where the
2154 * consumer's parent device is added before the supplier.
2157 con_dev = fwnode_get_next_parent_dev(link->consumer);
2159 * However, if the consumer's parent device is also the
2160 * parent of the supplier, don't create a
2161 * consumer-supplier link from the parent to its child
2162 * device. Such a dependency is impossible.
2165 fwnode_is_ancestor_of(con_dev->fwnode, fwnode)) {
2166 put_device(con_dev);
2176 ret = fw_devlink_create_devlink(con_dev, fwnode, link);
2177 put_device(con_dev);
2178 if (!own_link || ret == -EAGAIN)
2181 __fwnode_link_del(link);
2186 * __fw_devlink_link_to_suppliers - Create device links to suppliers of a device
2187 * @dev: The consumer device that needs to be linked to its suppliers
2188 * @fwnode: Root of the fwnode tree that is used to create device links
2190 * This function looks at all the supplier fwnodes of fwnode tree rooted at
2191 * @fwnode and creates device links between @dev (consumer) and all the
2192 * supplier devices of the entire fwnode tree at @fwnode.
2194 * The function creates normal (non-SYNC_STATE_ONLY) device links between @dev
2195 * and the real suppliers of @dev. Once these device links are created, the
2196 * fwnode links are deleted.
2198 * In addition, it also looks at all the suppliers of the entire fwnode tree
2199 * because some of the child devices of @dev that have not been added yet
2200 * (because @dev hasn't probed) might already have their suppliers added to
2201 * driver core. So, this function creates SYNC_STATE_ONLY device links between
2202 * @dev (consumer) and these suppliers to make sure they don't execute their
2203 * sync_state() callbacks before these child devices have a chance to create
2204 * their device links. The fwnode links that correspond to the child devices
2205 * aren't delete because they are needed later to create the device links
2206 * between the real consumer and supplier devices.
2208 static void __fw_devlink_link_to_suppliers(struct device *dev,
2209 struct fwnode_handle *fwnode)
2211 bool own_link = (dev->fwnode == fwnode);
2212 struct fwnode_link *link, *tmp;
2213 struct fwnode_handle *child = NULL;
2215 list_for_each_entry_safe(link, tmp, &fwnode->suppliers, c_hook) {
2217 struct fwnode_handle *sup = link->supplier;
2219 ret = fw_devlink_create_devlink(dev, sup, link);
2220 if (!own_link || ret == -EAGAIN)
2223 __fwnode_link_del(link);
2227 * Make "proxy" SYNC_STATE_ONLY device links to represent the needs of
2228 * all the descendants. This proxy link step is needed to handle the
2229 * case where the supplier is added before the consumer's parent device
2232 while ((child = fwnode_get_next_available_child_node(fwnode, child)))
2233 __fw_devlink_link_to_suppliers(dev, child);
2236 static void fw_devlink_link_device(struct device *dev)
2238 struct fwnode_handle *fwnode = dev->fwnode;
2240 if (!fw_devlink_flags)
2243 fw_devlink_parse_fwtree(fwnode);
2245 mutex_lock(&fwnode_link_lock);
2246 __fw_devlink_link_to_consumers(dev);
2247 __fw_devlink_link_to_suppliers(dev, fwnode);
2248 mutex_unlock(&fwnode_link_lock);
2251 /* Device links support end. */
2253 int (*platform_notify)(struct device *dev) = NULL;
2254 int (*platform_notify_remove)(struct device *dev) = NULL;
2255 static struct kobject *dev_kobj;
2258 static struct kobject *sysfs_dev_char_kobj;
2260 /* /sys/dev/block */
2261 static struct kobject *sysfs_dev_block_kobj;
2263 static DEFINE_MUTEX(device_hotplug_lock);
2265 void lock_device_hotplug(void)
2267 mutex_lock(&device_hotplug_lock);
2270 void unlock_device_hotplug(void)
2272 mutex_unlock(&device_hotplug_lock);
2275 int lock_device_hotplug_sysfs(void)
2277 if (mutex_trylock(&device_hotplug_lock))
2280 /* Avoid busy looping (5 ms of sleep should do). */
2282 return restart_syscall();
2286 static inline int device_is_not_partition(struct device *dev)
2288 return !(dev->type == &part_type);
2291 static inline int device_is_not_partition(struct device *dev)
2297 static void device_platform_notify(struct device *dev)
2299 acpi_device_notify(dev);
2301 software_node_notify(dev);
2303 if (platform_notify)
2304 platform_notify(dev);
2307 static void device_platform_notify_remove(struct device *dev)
2309 if (platform_notify_remove)
2310 platform_notify_remove(dev);
2312 software_node_notify_remove(dev);
2314 acpi_device_notify_remove(dev);
2318 * dev_driver_string - Return a device's driver name, if at all possible
2319 * @dev: struct device to get the name of
2321 * Will return the device's driver's name if it is bound to a device. If
2322 * the device is not bound to a driver, it will return the name of the bus
2323 * it is attached to. If it is not attached to a bus either, an empty
2324 * string will be returned.
2326 const char *dev_driver_string(const struct device *dev)
2328 struct device_driver *drv;
2330 /* dev->driver can change to NULL underneath us because of unbinding,
2331 * so be careful about accessing it. dev->bus and dev->class should
2332 * never change once they are set, so they don't need special care.
2334 drv = READ_ONCE(dev->driver);
2335 return drv ? drv->name : dev_bus_name(dev);
2337 EXPORT_SYMBOL(dev_driver_string);
2339 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
2341 static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
2344 struct device_attribute *dev_attr = to_dev_attr(attr);
2345 struct device *dev = kobj_to_dev(kobj);
2349 ret = dev_attr->show(dev, dev_attr, buf);
2350 if (ret >= (ssize_t)PAGE_SIZE) {
2351 printk("dev_attr_show: %pS returned bad count\n",
2357 static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
2358 const char *buf, size_t count)
2360 struct device_attribute *dev_attr = to_dev_attr(attr);
2361 struct device *dev = kobj_to_dev(kobj);
2364 if (dev_attr->store)
2365 ret = dev_attr->store(dev, dev_attr, buf, count);
2369 static const struct sysfs_ops dev_sysfs_ops = {
2370 .show = dev_attr_show,
2371 .store = dev_attr_store,
2374 #define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
2376 ssize_t device_store_ulong(struct device *dev,
2377 struct device_attribute *attr,
2378 const char *buf, size_t size)
2380 struct dev_ext_attribute *ea = to_ext_attr(attr);
2384 ret = kstrtoul(buf, 0, &new);
2387 *(unsigned long *)(ea->var) = new;
2388 /* Always return full write size even if we didn't consume all */
2391 EXPORT_SYMBOL_GPL(device_store_ulong);
2393 ssize_t device_show_ulong(struct device *dev,
2394 struct device_attribute *attr,
2397 struct dev_ext_attribute *ea = to_ext_attr(attr);
2398 return sysfs_emit(buf, "%lx\n", *(unsigned long *)(ea->var));
2400 EXPORT_SYMBOL_GPL(device_show_ulong);
2402 ssize_t device_store_int(struct device *dev,
2403 struct device_attribute *attr,
2404 const char *buf, size_t size)
2406 struct dev_ext_attribute *ea = to_ext_attr(attr);
2410 ret = kstrtol(buf, 0, &new);
2414 if (new > INT_MAX || new < INT_MIN)
2416 *(int *)(ea->var) = new;
2417 /* Always return full write size even if we didn't consume all */
2420 EXPORT_SYMBOL_GPL(device_store_int);
2422 ssize_t device_show_int(struct device *dev,
2423 struct device_attribute *attr,
2426 struct dev_ext_attribute *ea = to_ext_attr(attr);
2428 return sysfs_emit(buf, "%d\n", *(int *)(ea->var));
2430 EXPORT_SYMBOL_GPL(device_show_int);
2432 ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
2433 const char *buf, size_t size)
2435 struct dev_ext_attribute *ea = to_ext_attr(attr);
2437 if (kstrtobool(buf, ea->var) < 0)
2442 EXPORT_SYMBOL_GPL(device_store_bool);
2444 ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
2447 struct dev_ext_attribute *ea = to_ext_attr(attr);
2449 return sysfs_emit(buf, "%d\n", *(bool *)(ea->var));
2451 EXPORT_SYMBOL_GPL(device_show_bool);
2454 * device_release - free device structure.
2455 * @kobj: device's kobject.
2457 * This is called once the reference count for the object
2458 * reaches 0. We forward the call to the device's release
2459 * method, which should handle actually freeing the structure.
2461 static void device_release(struct kobject *kobj)
2463 struct device *dev = kobj_to_dev(kobj);
2464 struct device_private *p = dev->p;
2467 * Some platform devices are driven without driver attached
2468 * and managed resources may have been acquired. Make sure
2469 * all resources are released.
2471 * Drivers still can add resources into device after device
2472 * is deleted but alive, so release devres here to avoid
2473 * possible memory leak.
2475 devres_release_all(dev);
2477 kfree(dev->dma_range_map);
2481 else if (dev->type && dev->type->release)
2482 dev->type->release(dev);
2483 else if (dev->class && dev->class->dev_release)
2484 dev->class->dev_release(dev);
2486 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",
2491 static const void *device_namespace(const struct kobject *kobj)
2493 const struct device *dev = kobj_to_dev(kobj);
2494 const void *ns = NULL;
2496 if (dev->class && dev->class->ns_type)
2497 ns = dev->class->namespace(dev);
2502 static void device_get_ownership(const struct kobject *kobj, kuid_t *uid, kgid_t *gid)
2504 const struct device *dev = kobj_to_dev(kobj);
2506 if (dev->class && dev->class->get_ownership)
2507 dev->class->get_ownership(dev, uid, gid);
2510 static const struct kobj_type device_ktype = {
2511 .release = device_release,
2512 .sysfs_ops = &dev_sysfs_ops,
2513 .namespace = device_namespace,
2514 .get_ownership = device_get_ownership,
2518 static int dev_uevent_filter(const struct kobject *kobj)
2520 const struct kobj_type *ktype = get_ktype(kobj);
2522 if (ktype == &device_ktype) {
2523 const struct device *dev = kobj_to_dev(kobj);
2532 static const char *dev_uevent_name(const struct kobject *kobj)
2534 const struct device *dev = kobj_to_dev(kobj);
2537 return dev->bus->name;
2539 return dev->class->name;
2543 static int dev_uevent(const struct kobject *kobj, struct kobj_uevent_env *env)
2545 const struct device *dev = kobj_to_dev(kobj);
2548 /* add device node properties if present */
2549 if (MAJOR(dev->devt)) {
2553 kuid_t uid = GLOBAL_ROOT_UID;
2554 kgid_t gid = GLOBAL_ROOT_GID;
2556 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
2557 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
2558 name = device_get_devnode(dev, &mode, &uid, &gid, &tmp);
2560 add_uevent_var(env, "DEVNAME=%s", name);
2562 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
2563 if (!uid_eq(uid, GLOBAL_ROOT_UID))
2564 add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid));
2565 if (!gid_eq(gid, GLOBAL_ROOT_GID))
2566 add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid));
2571 if (dev->type && dev->type->name)
2572 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
2575 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
2577 /* Add common DT information about the device */
2578 of_device_uevent(dev, env);
2580 /* have the bus specific function add its stuff */
2581 if (dev->bus && dev->bus->uevent) {
2582 retval = dev->bus->uevent(dev, env);
2584 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
2585 dev_name(dev), __func__, retval);
2588 /* have the class specific function add its stuff */
2589 if (dev->class && dev->class->dev_uevent) {
2590 retval = dev->class->dev_uevent(dev, env);
2592 pr_debug("device: '%s': %s: class uevent() "
2593 "returned %d\n", dev_name(dev),
2597 /* have the device type specific function add its stuff */
2598 if (dev->type && dev->type->uevent) {
2599 retval = dev->type->uevent(dev, env);
2601 pr_debug("device: '%s': %s: dev_type uevent() "
2602 "returned %d\n", dev_name(dev),
2609 static const struct kset_uevent_ops device_uevent_ops = {
2610 .filter = dev_uevent_filter,
2611 .name = dev_uevent_name,
2612 .uevent = dev_uevent,
2615 static ssize_t uevent_show(struct device *dev, struct device_attribute *attr,
2618 struct kobject *top_kobj;
2620 struct kobj_uevent_env *env = NULL;
2625 /* search the kset, the device belongs to */
2626 top_kobj = &dev->kobj;
2627 while (!top_kobj->kset && top_kobj->parent)
2628 top_kobj = top_kobj->parent;
2629 if (!top_kobj->kset)
2632 kset = top_kobj->kset;
2633 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
2636 /* respect filter */
2637 if (kset->uevent_ops && kset->uevent_ops->filter)
2638 if (!kset->uevent_ops->filter(&dev->kobj))
2641 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
2645 /* let the kset specific function add its keys */
2646 retval = kset->uevent_ops->uevent(&dev->kobj, env);
2650 /* copy keys to file */
2651 for (i = 0; i < env->envp_idx; i++)
2652 len += sysfs_emit_at(buf, len, "%s\n", env->envp[i]);
2658 static ssize_t uevent_store(struct device *dev, struct device_attribute *attr,
2659 const char *buf, size_t count)
2663 rc = kobject_synth_uevent(&dev->kobj, buf, count);
2666 dev_err(dev, "uevent: failed to send synthetic uevent: %d\n", rc);
2672 static DEVICE_ATTR_RW(uevent);
2674 static ssize_t online_show(struct device *dev, struct device_attribute *attr,
2680 val = !dev->offline;
2682 return sysfs_emit(buf, "%u\n", val);
2685 static ssize_t online_store(struct device *dev, struct device_attribute *attr,
2686 const char *buf, size_t count)
2691 ret = kstrtobool(buf, &val);
2695 ret = lock_device_hotplug_sysfs();
2699 ret = val ? device_online(dev) : device_offline(dev);
2700 unlock_device_hotplug();
2701 return ret < 0 ? ret : count;
2703 static DEVICE_ATTR_RW(online);
2705 static ssize_t removable_show(struct device *dev, struct device_attribute *attr,
2710 switch (dev->removable) {
2711 case DEVICE_REMOVABLE:
2720 return sysfs_emit(buf, "%s\n", loc);
2722 static DEVICE_ATTR_RO(removable);
2724 int device_add_groups(struct device *dev, const struct attribute_group **groups)
2726 return sysfs_create_groups(&dev->kobj, groups);
2728 EXPORT_SYMBOL_GPL(device_add_groups);
2730 void device_remove_groups(struct device *dev,
2731 const struct attribute_group **groups)
2733 sysfs_remove_groups(&dev->kobj, groups);
2735 EXPORT_SYMBOL_GPL(device_remove_groups);
2737 union device_attr_group_devres {
2738 const struct attribute_group *group;
2739 const struct attribute_group **groups;
2742 static void devm_attr_group_remove(struct device *dev, void *res)
2744 union device_attr_group_devres *devres = res;
2745 const struct attribute_group *group = devres->group;
2747 dev_dbg(dev, "%s: removing group %p\n", __func__, group);
2748 sysfs_remove_group(&dev->kobj, group);
2751 static void devm_attr_groups_remove(struct device *dev, void *res)
2753 union device_attr_group_devres *devres = res;
2754 const struct attribute_group **groups = devres->groups;
2756 dev_dbg(dev, "%s: removing groups %p\n", __func__, groups);
2757 sysfs_remove_groups(&dev->kobj, groups);
2761 * devm_device_add_group - given a device, create a managed attribute group
2762 * @dev: The device to create the group for
2763 * @grp: The attribute group to create
2765 * This function creates a group for the first time. It will explicitly
2766 * warn and error if any of the attribute files being created already exist.
2768 * Returns 0 on success or error code on failure.
2770 int devm_device_add_group(struct device *dev, const struct attribute_group *grp)
2772 union device_attr_group_devres *devres;
2775 devres = devres_alloc(devm_attr_group_remove,
2776 sizeof(*devres), GFP_KERNEL);
2780 error = sysfs_create_group(&dev->kobj, grp);
2782 devres_free(devres);
2786 devres->group = grp;
2787 devres_add(dev, devres);
2790 EXPORT_SYMBOL_GPL(devm_device_add_group);
2793 * devm_device_add_groups - create a bunch of managed attribute groups
2794 * @dev: The device to create the group for
2795 * @groups: The attribute groups to create, NULL terminated
2797 * This function creates a bunch of managed attribute groups. If an error
2798 * occurs when creating a group, all previously created groups will be
2799 * removed, unwinding everything back to the original state when this
2800 * function was called. It will explicitly warn and error if any of the
2801 * attribute files being created already exist.
2803 * Returns 0 on success or error code from sysfs_create_group on failure.
2805 int devm_device_add_groups(struct device *dev,
2806 const struct attribute_group **groups)
2808 union device_attr_group_devres *devres;
2811 devres = devres_alloc(devm_attr_groups_remove,
2812 sizeof(*devres), GFP_KERNEL);
2816 error = sysfs_create_groups(&dev->kobj, groups);
2818 devres_free(devres);
2822 devres->groups = groups;
2823 devres_add(dev, devres);
2826 EXPORT_SYMBOL_GPL(devm_device_add_groups);
2828 static int device_add_attrs(struct device *dev)
2830 const struct class *class = dev->class;
2831 const struct device_type *type = dev->type;
2835 error = device_add_groups(dev, class->dev_groups);
2841 error = device_add_groups(dev, type->groups);
2843 goto err_remove_class_groups;
2846 error = device_add_groups(dev, dev->groups);
2848 goto err_remove_type_groups;
2850 if (device_supports_offline(dev) && !dev->offline_disabled) {
2851 error = device_create_file(dev, &dev_attr_online);
2853 goto err_remove_dev_groups;
2856 if (fw_devlink_flags && !fw_devlink_is_permissive() && dev->fwnode) {
2857 error = device_create_file(dev, &dev_attr_waiting_for_supplier);
2859 goto err_remove_dev_online;
2862 if (dev_removable_is_valid(dev)) {
2863 error = device_create_file(dev, &dev_attr_removable);
2865 goto err_remove_dev_waiting_for_supplier;
2868 if (dev_add_physical_location(dev)) {
2869 error = device_add_group(dev,
2870 &dev_attr_physical_location_group);
2872 goto err_remove_dev_removable;
2877 err_remove_dev_removable:
2878 device_remove_file(dev, &dev_attr_removable);
2879 err_remove_dev_waiting_for_supplier:
2880 device_remove_file(dev, &dev_attr_waiting_for_supplier);
2881 err_remove_dev_online:
2882 device_remove_file(dev, &dev_attr_online);
2883 err_remove_dev_groups:
2884 device_remove_groups(dev, dev->groups);
2885 err_remove_type_groups:
2887 device_remove_groups(dev, type->groups);
2888 err_remove_class_groups:
2890 device_remove_groups(dev, class->dev_groups);
2895 static void device_remove_attrs(struct device *dev)
2897 const struct class *class = dev->class;
2898 const struct device_type *type = dev->type;
2900 if (dev->physical_location) {
2901 device_remove_group(dev, &dev_attr_physical_location_group);
2902 kfree(dev->physical_location);
2905 device_remove_file(dev, &dev_attr_removable);
2906 device_remove_file(dev, &dev_attr_waiting_for_supplier);
2907 device_remove_file(dev, &dev_attr_online);
2908 device_remove_groups(dev, dev->groups);
2911 device_remove_groups(dev, type->groups);
2914 device_remove_groups(dev, class->dev_groups);
2917 static ssize_t dev_show(struct device *dev, struct device_attribute *attr,
2920 return print_dev_t(buf, dev->devt);
2922 static DEVICE_ATTR_RO(dev);
2925 struct kset *devices_kset;
2928 * devices_kset_move_before - Move device in the devices_kset's list.
2929 * @deva: Device to move.
2930 * @devb: Device @deva should come before.
2932 static void devices_kset_move_before(struct device *deva, struct device *devb)
2936 pr_debug("devices_kset: Moving %s before %s\n",
2937 dev_name(deva), dev_name(devb));
2938 spin_lock(&devices_kset->list_lock);
2939 list_move_tail(&deva->kobj.entry, &devb->kobj.entry);
2940 spin_unlock(&devices_kset->list_lock);
2944 * devices_kset_move_after - Move device in the devices_kset's list.
2945 * @deva: Device to move
2946 * @devb: Device @deva should come after.
2948 static void devices_kset_move_after(struct device *deva, struct device *devb)
2952 pr_debug("devices_kset: Moving %s after %s\n",
2953 dev_name(deva), dev_name(devb));
2954 spin_lock(&devices_kset->list_lock);
2955 list_move(&deva->kobj.entry, &devb->kobj.entry);
2956 spin_unlock(&devices_kset->list_lock);
2960 * devices_kset_move_last - move the device to the end of devices_kset's list.
2961 * @dev: device to move
2963 void devices_kset_move_last(struct device *dev)
2967 pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev));
2968 spin_lock(&devices_kset->list_lock);
2969 list_move_tail(&dev->kobj.entry, &devices_kset->list);
2970 spin_unlock(&devices_kset->list_lock);
2974 * device_create_file - create sysfs attribute file for device.
2976 * @attr: device attribute descriptor.
2978 int device_create_file(struct device *dev,
2979 const struct device_attribute *attr)
2984 WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
2985 "Attribute %s: write permission without 'store'\n",
2987 WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
2988 "Attribute %s: read permission without 'show'\n",
2990 error = sysfs_create_file(&dev->kobj, &attr->attr);
2995 EXPORT_SYMBOL_GPL(device_create_file);
2998 * device_remove_file - remove sysfs attribute file.
3000 * @attr: device attribute descriptor.
3002 void device_remove_file(struct device *dev,
3003 const struct device_attribute *attr)
3006 sysfs_remove_file(&dev->kobj, &attr->attr);
3008 EXPORT_SYMBOL_GPL(device_remove_file);
3011 * device_remove_file_self - remove sysfs attribute file from its own method.
3013 * @attr: device attribute descriptor.
3015 * See kernfs_remove_self() for details.
3017 bool device_remove_file_self(struct device *dev,
3018 const struct device_attribute *attr)
3021 return sysfs_remove_file_self(&dev->kobj, &attr->attr);
3025 EXPORT_SYMBOL_GPL(device_remove_file_self);
3028 * device_create_bin_file - create sysfs binary attribute file for device.
3030 * @attr: device binary attribute descriptor.
3032 int device_create_bin_file(struct device *dev,
3033 const struct bin_attribute *attr)
3035 int error = -EINVAL;
3037 error = sysfs_create_bin_file(&dev->kobj, attr);
3040 EXPORT_SYMBOL_GPL(device_create_bin_file);
3043 * device_remove_bin_file - remove sysfs binary attribute file
3045 * @attr: device binary attribute descriptor.
3047 void device_remove_bin_file(struct device *dev,
3048 const struct bin_attribute *attr)
3051 sysfs_remove_bin_file(&dev->kobj, attr);
3053 EXPORT_SYMBOL_GPL(device_remove_bin_file);
3055 static void klist_children_get(struct klist_node *n)
3057 struct device_private *p = to_device_private_parent(n);
3058 struct device *dev = p->device;
3063 static void klist_children_put(struct klist_node *n)
3065 struct device_private *p = to_device_private_parent(n);
3066 struct device *dev = p->device;
3072 * device_initialize - init device structure.
3075 * This prepares the device for use by other layers by initializing
3077 * It is the first half of device_register(), if called by
3078 * that function, though it can also be called separately, so one
3079 * may use @dev's fields. In particular, get_device()/put_device()
3080 * may be used for reference counting of @dev after calling this
3083 * All fields in @dev must be initialized by the caller to 0, except
3084 * for those explicitly set to some other value. The simplest
3085 * approach is to use kzalloc() to allocate the structure containing
3088 * NOTE: Use put_device() to give up your reference instead of freeing
3089 * @dev directly once you have called this function.
3091 void device_initialize(struct device *dev)
3093 dev->kobj.kset = devices_kset;
3094 kobject_init(&dev->kobj, &device_ktype);
3095 INIT_LIST_HEAD(&dev->dma_pools);
3096 mutex_init(&dev->mutex);
3097 lockdep_set_novalidate_class(&dev->mutex);
3098 spin_lock_init(&dev->devres_lock);
3099 INIT_LIST_HEAD(&dev->devres_head);
3100 device_pm_init(dev);
3101 set_dev_node(dev, NUMA_NO_NODE);
3102 INIT_LIST_HEAD(&dev->links.consumers);
3103 INIT_LIST_HEAD(&dev->links.suppliers);
3104 INIT_LIST_HEAD(&dev->links.defer_sync);
3105 dev->links.status = DL_DEV_NO_DRIVER;
3106 #if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE) || \
3107 defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU) || \
3108 defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL)
3109 dev->dma_coherent = dma_default_coherent;
3111 swiotlb_dev_init(dev);
3113 EXPORT_SYMBOL_GPL(device_initialize);
3115 struct kobject *virtual_device_parent(struct device *dev)
3117 static struct kobject *virtual_dir = NULL;
3120 virtual_dir = kobject_create_and_add("virtual",
3121 &devices_kset->kobj);
3127 struct kobject kobj;
3128 const struct class *class;
3131 #define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
3133 static void class_dir_release(struct kobject *kobj)
3135 struct class_dir *dir = to_class_dir(kobj);
3140 struct kobj_ns_type_operations *class_dir_child_ns_type(const struct kobject *kobj)
3142 const struct class_dir *dir = to_class_dir(kobj);
3143 return dir->class->ns_type;
3146 static const struct kobj_type class_dir_ktype = {
3147 .release = class_dir_release,
3148 .sysfs_ops = &kobj_sysfs_ops,
3149 .child_ns_type = class_dir_child_ns_type
3152 static struct kobject *class_dir_create_and_add(struct subsys_private *sp,
3153 struct kobject *parent_kobj)
3155 struct class_dir *dir;
3158 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
3160 return ERR_PTR(-ENOMEM);
3162 dir->class = sp->class;
3163 kobject_init(&dir->kobj, &class_dir_ktype);
3165 dir->kobj.kset = &sp->glue_dirs;
3167 retval = kobject_add(&dir->kobj, parent_kobj, "%s", sp->class->name);
3169 kobject_put(&dir->kobj);
3170 return ERR_PTR(retval);
3175 static DEFINE_MUTEX(gdp_mutex);
3177 static struct kobject *get_device_parent(struct device *dev,
3178 struct device *parent)
3180 struct subsys_private *sp = class_to_subsys(dev->class);
3181 struct kobject *kobj = NULL;
3184 struct kobject *parent_kobj;
3188 * If we have no parent, we live in "virtual".
3189 * Class-devices with a non class-device as parent, live
3190 * in a "glue" directory to prevent namespace collisions.
3193 parent_kobj = virtual_device_parent(dev);
3194 else if (parent->class && !dev->class->ns_type) {
3196 return &parent->kobj;
3198 parent_kobj = &parent->kobj;
3201 mutex_lock(&gdp_mutex);
3203 /* find our class-directory at the parent and reference it */
3204 spin_lock(&sp->glue_dirs.list_lock);
3205 list_for_each_entry(k, &sp->glue_dirs.list, entry)
3206 if (k->parent == parent_kobj) {
3207 kobj = kobject_get(k);
3210 spin_unlock(&sp->glue_dirs.list_lock);
3212 mutex_unlock(&gdp_mutex);
3217 /* or create a new class-directory at the parent device */
3218 k = class_dir_create_and_add(sp, parent_kobj);
3219 /* do not emit an uevent for this simple "glue" directory */
3220 mutex_unlock(&gdp_mutex);
3225 /* subsystems can specify a default root directory for their devices */
3226 if (!parent && dev->bus) {
3227 struct device *dev_root = bus_get_dev_root(dev->bus);
3230 kobj = &dev_root->kobj;
3231 put_device(dev_root);
3237 return &parent->kobj;
3241 static inline bool live_in_glue_dir(struct kobject *kobj,
3244 struct subsys_private *sp;
3247 if (!kobj || !dev->class)
3250 sp = class_to_subsys(dev->class);
3254 if (kobj->kset == &sp->glue_dirs)
3263 static inline struct kobject *get_glue_dir(struct device *dev)
3265 return dev->kobj.parent;
3269 * kobject_has_children - Returns whether a kobject has children.
3270 * @kobj: the object to test
3272 * This will return whether a kobject has other kobjects as children.
3274 * It does NOT account for the presence of attribute files, only sub
3275 * directories. It also assumes there is no concurrent addition or
3276 * removal of such children, and thus relies on external locking.
3278 static inline bool kobject_has_children(struct kobject *kobj)
3280 WARN_ON_ONCE(kref_read(&kobj->kref) == 0);
3282 return kobj->sd && kobj->sd->dir.subdirs;
3286 * make sure cleaning up dir as the last step, we need to make
3287 * sure .release handler of kobject is run with holding the
3290 static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
3294 /* see if we live in a "glue" directory */
3295 if (!live_in_glue_dir(glue_dir, dev))
3298 mutex_lock(&gdp_mutex);
3300 * There is a race condition between removing glue directory
3301 * and adding a new device under the glue directory.
3306 * get_device_parent()
3307 * class_dir_create_and_add()
3308 * kobject_add_internal()
3309 * create_dir() // create glue_dir
3312 * get_device_parent()
3313 * kobject_get() // get glue_dir
3316 * cleanup_glue_dir()
3317 * kobject_del(glue_dir)
3320 * kobject_add_internal()
3321 * create_dir() // in glue_dir
3322 * sysfs_create_dir_ns()
3323 * kernfs_create_dir_ns(sd)
3325 * sysfs_remove_dir() // glue_dir->sd=NULL
3326 * sysfs_put() // free glue_dir->sd
3329 * kernfs_new_node(sd)
3330 * kernfs_get(glue_dir)
3334 * Before CPU1 remove last child device under glue dir, if CPU2 add
3335 * a new device under glue dir, the glue_dir kobject reference count
3336 * will be increase to 2 in kobject_get(k). And CPU2 has been called
3337 * kernfs_create_dir_ns(). Meanwhile, CPU1 call sysfs_remove_dir()
3338 * and sysfs_put(). This result in glue_dir->sd is freed.
3340 * Then the CPU2 will see a stale "empty" but still potentially used
3341 * glue dir around in kernfs_new_node().
3343 * In order to avoid this happening, we also should make sure that
3344 * kernfs_node for glue_dir is released in CPU1 only when refcount
3345 * for glue_dir kobj is 1.
3347 ref = kref_read(&glue_dir->kref);
3348 if (!kobject_has_children(glue_dir) && !--ref)
3349 kobject_del(glue_dir);
3350 kobject_put(glue_dir);
3351 mutex_unlock(&gdp_mutex);
3354 static int device_add_class_symlinks(struct device *dev)
3356 struct device_node *of_node = dev_of_node(dev);
3357 struct subsys_private *sp;
3361 error = sysfs_create_link(&dev->kobj, of_node_kobj(of_node), "of_node");
3363 dev_warn(dev, "Error %d creating of_node link\n",error);
3364 /* An error here doesn't warrant bringing down the device */
3367 sp = class_to_subsys(dev->class);
3371 error = sysfs_create_link(&dev->kobj, &sp->subsys.kobj, "subsystem");
3375 if (dev->parent && device_is_not_partition(dev)) {
3376 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
3382 /* link in the class directory pointing to the device */
3383 error = sysfs_create_link(&sp->subsys.kobj, &dev->kobj, dev_name(dev));
3389 sysfs_remove_link(&dev->kobj, "device");
3391 sysfs_remove_link(&dev->kobj, "subsystem");
3393 sysfs_remove_link(&dev->kobj, "of_node");
3399 static void device_remove_class_symlinks(struct device *dev)
3401 struct subsys_private *sp = class_to_subsys(dev->class);
3403 if (dev_of_node(dev))
3404 sysfs_remove_link(&dev->kobj, "of_node");
3409 if (dev->parent && device_is_not_partition(dev))
3410 sysfs_remove_link(&dev->kobj, "device");
3411 sysfs_remove_link(&dev->kobj, "subsystem");
3412 sysfs_delete_link(&sp->subsys.kobj, &dev->kobj, dev_name(dev));
3417 * dev_set_name - set a device name
3419 * @fmt: format string for the device's name
3421 int dev_set_name(struct device *dev, const char *fmt, ...)
3426 va_start(vargs, fmt);
3427 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
3431 EXPORT_SYMBOL_GPL(dev_set_name);
3433 /* select a /sys/dev/ directory for the device */
3434 static struct kobject *device_to_dev_kobj(struct device *dev)
3436 if (is_blockdev(dev))
3437 return sysfs_dev_block_kobj;
3439 return sysfs_dev_char_kobj;
3442 static int device_create_sys_dev_entry(struct device *dev)
3444 struct kobject *kobj = device_to_dev_kobj(dev);
3449 format_dev_t(devt_str, dev->devt);
3450 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
3456 static void device_remove_sys_dev_entry(struct device *dev)
3458 struct kobject *kobj = device_to_dev_kobj(dev);
3462 format_dev_t(devt_str, dev->devt);
3463 sysfs_remove_link(kobj, devt_str);
3467 static int device_private_init(struct device *dev)
3469 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
3472 dev->p->device = dev;
3473 klist_init(&dev->p->klist_children, klist_children_get,
3474 klist_children_put);
3475 INIT_LIST_HEAD(&dev->p->deferred_probe);
3480 * device_add - add device to device hierarchy.
3483 * This is part 2 of device_register(), though may be called
3484 * separately _iff_ device_initialize() has been called separately.
3486 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
3487 * to the global and sibling lists for the device, then
3488 * adds it to the other relevant subsystems of the driver model.
3490 * Do not call this routine or device_register() more than once for
3491 * any device structure. The driver model core is not designed to work
3492 * with devices that get unregistered and then spring back to life.
3493 * (Among other things, it's very hard to guarantee that all references
3494 * to the previous incarnation of @dev have been dropped.) Allocate
3495 * and register a fresh new struct device instead.
3497 * NOTE: _Never_ directly free @dev after calling this function, even
3498 * if it returned an error! Always use put_device() to give up your
3499 * reference instead.
3501 * Rule of thumb is: if device_add() succeeds, you should call
3502 * device_del() when you want to get rid of it. If device_add() has
3503 * *not* succeeded, use *only* put_device() to drop the reference
3506 int device_add(struct device *dev)
3508 struct subsys_private *sp;
3509 struct device *parent;
3510 struct kobject *kobj;
3511 struct class_interface *class_intf;
3512 int error = -EINVAL;
3513 struct kobject *glue_dir = NULL;
3515 dev = get_device(dev);
3520 error = device_private_init(dev);
3526 * for statically allocated devices, which should all be converted
3527 * some day, we need to initialize the name. We prevent reading back
3528 * the name, and force the use of dev_name()
3530 if (dev->init_name) {
3531 error = dev_set_name(dev, "%s", dev->init_name);
3532 dev->init_name = NULL;
3537 /* subsystems can specify simple device enumeration */
3538 else if (dev->bus && dev->bus->dev_name)
3539 error = dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
3545 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
3547 parent = get_device(dev->parent);
3548 kobj = get_device_parent(dev, parent);
3550 error = PTR_ERR(kobj);
3554 dev->kobj.parent = kobj;
3556 /* use parent numa_node */
3557 if (parent && (dev_to_node(dev) == NUMA_NO_NODE))
3558 set_dev_node(dev, dev_to_node(parent));
3560 /* first, register with generic layer. */
3561 /* we require the name to be set before, and pass NULL */
3562 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
3568 /* notify platform of device entry */
3569 device_platform_notify(dev);
3571 error = device_create_file(dev, &dev_attr_uevent);
3575 error = device_add_class_symlinks(dev);
3578 error = device_add_attrs(dev);
3581 error = bus_add_device(dev);
3584 error = dpm_sysfs_add(dev);
3589 if (MAJOR(dev->devt)) {
3590 error = device_create_file(dev, &dev_attr_dev);
3594 error = device_create_sys_dev_entry(dev);
3598 devtmpfs_create_node(dev);
3601 /* Notify clients of device addition. This call must come
3602 * after dpm_sysfs_add() and before kobject_uevent().
3604 bus_notify(dev, BUS_NOTIFY_ADD_DEVICE);
3605 kobject_uevent(&dev->kobj, KOBJ_ADD);
3608 * Check if any of the other devices (consumers) have been waiting for
3609 * this device (supplier) to be added so that they can create a device
3612 * This needs to happen after device_pm_add() because device_link_add()
3613 * requires the supplier be registered before it's called.
3615 * But this also needs to happen before bus_probe_device() to make sure
3616 * waiting consumers can link to it before the driver is bound to the
3617 * device and the driver sync_state callback is called for this device.
3619 if (dev->fwnode && !dev->fwnode->dev) {
3620 dev->fwnode->dev = dev;
3621 fw_devlink_link_device(dev);
3624 bus_probe_device(dev);
3627 * If all driver registration is done and a newly added device doesn't
3628 * match with any driver, don't block its consumers from probing in
3629 * case the consumer device is able to operate without this supplier.
3631 if (dev->fwnode && fw_devlink_drv_reg_done && !dev->can_match)
3632 fw_devlink_unblock_consumers(dev);
3635 klist_add_tail(&dev->p->knode_parent,
3636 &parent->p->klist_children);
3638 sp = class_to_subsys(dev->class);
3640 mutex_lock(&sp->mutex);
3641 /* tie the class to the device */
3642 klist_add_tail(&dev->p->knode_class, &sp->klist_devices);
3644 /* notify any interfaces that the device is here */
3645 list_for_each_entry(class_intf, &sp->interfaces, node)
3646 if (class_intf->add_dev)
3647 class_intf->add_dev(dev);
3648 mutex_unlock(&sp->mutex);
3655 if (MAJOR(dev->devt))
3656 device_remove_file(dev, &dev_attr_dev);
3658 device_pm_remove(dev);
3659 dpm_sysfs_remove(dev);
3662 bus_remove_device(dev);
3664 device_remove_attrs(dev);
3666 device_remove_class_symlinks(dev);
3668 device_remove_file(dev, &dev_attr_uevent);
3670 device_platform_notify_remove(dev);
3671 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
3672 glue_dir = get_glue_dir(dev);
3673 kobject_del(&dev->kobj);
3675 cleanup_glue_dir(dev, glue_dir);
3683 EXPORT_SYMBOL_GPL(device_add);
3686 * device_register - register a device with the system.
3687 * @dev: pointer to the device structure
3689 * This happens in two clean steps - initialize the device
3690 * and add it to the system. The two steps can be called
3691 * separately, but this is the easiest and most common.
3692 * I.e. you should only call the two helpers separately if
3693 * have a clearly defined need to use and refcount the device
3694 * before it is added to the hierarchy.
3696 * For more information, see the kerneldoc for device_initialize()
3699 * NOTE: _Never_ directly free @dev after calling this function, even
3700 * if it returned an error! Always use put_device() to give up the
3701 * reference initialized in this function instead.
3703 int device_register(struct device *dev)
3705 device_initialize(dev);
3706 return device_add(dev);
3708 EXPORT_SYMBOL_GPL(device_register);
3711 * get_device - increment reference count for device.
3714 * This simply forwards the call to kobject_get(), though
3715 * we do take care to provide for the case that we get a NULL
3716 * pointer passed in.
3718 struct device *get_device(struct device *dev)
3720 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
3722 EXPORT_SYMBOL_GPL(get_device);
3725 * put_device - decrement reference count.
3726 * @dev: device in question.
3728 void put_device(struct device *dev)
3730 /* might_sleep(); */
3732 kobject_put(&dev->kobj);
3734 EXPORT_SYMBOL_GPL(put_device);
3736 bool kill_device(struct device *dev)
3739 * Require the device lock and set the "dead" flag to guarantee that
3740 * the update behavior is consistent with the other bitfields near
3741 * it and that we cannot have an asynchronous probe routine trying
3742 * to run while we are tearing out the bus/class/sysfs from
3743 * underneath the device.
3745 device_lock_assert(dev);
3749 dev->p->dead = true;
3752 EXPORT_SYMBOL_GPL(kill_device);
3755 * device_del - delete device from system.
3758 * This is the first part of the device unregistration
3759 * sequence. This removes the device from the lists we control
3760 * from here, has it removed from the other driver model
3761 * subsystems it was added to in device_add(), and removes it
3762 * from the kobject hierarchy.
3764 * NOTE: this should be called manually _iff_ device_add() was
3765 * also called manually.
3767 void device_del(struct device *dev)
3769 struct subsys_private *sp;
3770 struct device *parent = dev->parent;
3771 struct kobject *glue_dir = NULL;
3772 struct class_interface *class_intf;
3773 unsigned int noio_flag;
3779 if (dev->fwnode && dev->fwnode->dev == dev)
3780 dev->fwnode->dev = NULL;
3782 /* Notify clients of device removal. This call must come
3783 * before dpm_sysfs_remove().
3785 noio_flag = memalloc_noio_save();
3786 bus_notify(dev, BUS_NOTIFY_DEL_DEVICE);
3788 dpm_sysfs_remove(dev);
3790 klist_del(&dev->p->knode_parent);
3791 if (MAJOR(dev->devt)) {
3792 devtmpfs_delete_node(dev);
3793 device_remove_sys_dev_entry(dev);
3794 device_remove_file(dev, &dev_attr_dev);
3797 sp = class_to_subsys(dev->class);
3799 device_remove_class_symlinks(dev);
3801 mutex_lock(&sp->mutex);
3802 /* notify any interfaces that the device is now gone */
3803 list_for_each_entry(class_intf, &sp->interfaces, node)
3804 if (class_intf->remove_dev)
3805 class_intf->remove_dev(dev);
3806 /* remove the device from the class list */
3807 klist_del(&dev->p->knode_class);
3808 mutex_unlock(&sp->mutex);
3811 device_remove_file(dev, &dev_attr_uevent);
3812 device_remove_attrs(dev);
3813 bus_remove_device(dev);
3814 device_pm_remove(dev);
3815 driver_deferred_probe_del(dev);
3816 device_platform_notify_remove(dev);
3817 device_links_purge(dev);
3820 * If a device does not have a driver attached, we need to clean
3821 * up any managed resources. We do this in device_release(), but
3822 * it's never called (and we leak the device) if a managed
3823 * resource holds a reference to the device. So release all
3824 * managed resources here, like we do in driver_detach(). We
3825 * still need to do so again in device_release() in case someone
3826 * adds a new resource after this point, though.
3828 devres_release_all(dev);
3830 bus_notify(dev, BUS_NOTIFY_REMOVED_DEVICE);
3831 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
3832 glue_dir = get_glue_dir(dev);
3833 kobject_del(&dev->kobj);
3834 cleanup_glue_dir(dev, glue_dir);
3835 memalloc_noio_restore(noio_flag);
3838 EXPORT_SYMBOL_GPL(device_del);
3841 * device_unregister - unregister device from system.
3842 * @dev: device going away.
3844 * We do this in two parts, like we do device_register(). First,
3845 * we remove it from all the subsystems with device_del(), then
3846 * we decrement the reference count via put_device(). If that
3847 * is the final reference count, the device will be cleaned up
3848 * via device_release() above. Otherwise, the structure will
3849 * stick around until the final reference to the device is dropped.
3851 void device_unregister(struct device *dev)
3853 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
3857 EXPORT_SYMBOL_GPL(device_unregister);
3859 static struct device *prev_device(struct klist_iter *i)
3861 struct klist_node *n = klist_prev(i);
3862 struct device *dev = NULL;
3863 struct device_private *p;
3866 p = to_device_private_parent(n);
3872 static struct device *next_device(struct klist_iter *i)
3874 struct klist_node *n = klist_next(i);
3875 struct device *dev = NULL;
3876 struct device_private *p;
3879 p = to_device_private_parent(n);
3886 * device_get_devnode - path of device node file
3888 * @mode: returned file access mode
3889 * @uid: returned file owner
3890 * @gid: returned file group
3891 * @tmp: possibly allocated string
3893 * Return the relative path of a possible device node.
3894 * Non-default names may need to allocate a memory to compose
3895 * a name. This memory is returned in tmp and needs to be
3896 * freed by the caller.
3898 const char *device_get_devnode(const struct device *dev,
3899 umode_t *mode, kuid_t *uid, kgid_t *gid,
3906 /* the device type may provide a specific name */
3907 if (dev->type && dev->type->devnode)
3908 *tmp = dev->type->devnode(dev, mode, uid, gid);
3912 /* the class may provide a specific name */
3913 if (dev->class && dev->class->devnode)
3914 *tmp = dev->class->devnode(dev, mode);
3918 /* return name without allocation, tmp == NULL */
3919 if (strchr(dev_name(dev), '!') == NULL)
3920 return dev_name(dev);
3922 /* replace '!' in the name with '/' */
3923 s = kstrdup_and_replace(dev_name(dev), '!', '/', GFP_KERNEL);
3930 * device_for_each_child - device child iterator.
3931 * @parent: parent struct device.
3932 * @fn: function to be called for each device.
3933 * @data: data for the callback.
3935 * Iterate over @parent's child devices, and call @fn for each,
3938 * We check the return of @fn each time. If it returns anything
3939 * other than 0, we break out and return that value.
3941 int device_for_each_child(struct device *parent, void *data,
3942 int (*fn)(struct device *dev, void *data))
3944 struct klist_iter i;
3945 struct device *child;
3951 klist_iter_init(&parent->p->klist_children, &i);
3952 while (!error && (child = next_device(&i)))
3953 error = fn(child, data);
3954 klist_iter_exit(&i);
3957 EXPORT_SYMBOL_GPL(device_for_each_child);
3960 * device_for_each_child_reverse - device child iterator in reversed order.
3961 * @parent: parent struct device.
3962 * @fn: function to be called for each device.
3963 * @data: data for the callback.
3965 * Iterate over @parent's child devices, and call @fn for each,
3968 * We check the return of @fn each time. If it returns anything
3969 * other than 0, we break out and return that value.
3971 int device_for_each_child_reverse(struct device *parent, void *data,
3972 int (*fn)(struct device *dev, void *data))
3974 struct klist_iter i;
3975 struct device *child;
3981 klist_iter_init(&parent->p->klist_children, &i);
3982 while ((child = prev_device(&i)) && !error)
3983 error = fn(child, data);
3984 klist_iter_exit(&i);
3987 EXPORT_SYMBOL_GPL(device_for_each_child_reverse);
3990 * device_find_child - device iterator for locating a particular device.
3991 * @parent: parent struct device
3992 * @match: Callback function to check device
3993 * @data: Data to pass to match function
3995 * This is similar to the device_for_each_child() function above, but it
3996 * returns a reference to a device that is 'found' for later use, as
3997 * determined by the @match callback.
3999 * The callback should return 0 if the device doesn't match and non-zero
4000 * if it does. If the callback returns non-zero and a reference to the
4001 * current device can be obtained, this function will return to the caller
4002 * and not iterate over any more devices.
4004 * NOTE: you will need to drop the reference with put_device() after use.
4006 struct device *device_find_child(struct device *parent, void *data,
4007 int (*match)(struct device *dev, void *data))
4009 struct klist_iter i;
4010 struct device *child;
4015 klist_iter_init(&parent->p->klist_children, &i);
4016 while ((child = next_device(&i)))
4017 if (match(child, data) && get_device(child))
4019 klist_iter_exit(&i);
4022 EXPORT_SYMBOL_GPL(device_find_child);
4025 * device_find_child_by_name - device iterator for locating a child device.
4026 * @parent: parent struct device
4027 * @name: name of the child device
4029 * This is similar to the device_find_child() function above, but it
4030 * returns a reference to a device that has the name @name.
4032 * NOTE: you will need to drop the reference with put_device() after use.
4034 struct device *device_find_child_by_name(struct device *parent,
4037 struct klist_iter i;
4038 struct device *child;
4043 klist_iter_init(&parent->p->klist_children, &i);
4044 while ((child = next_device(&i)))
4045 if (sysfs_streq(dev_name(child), name) && get_device(child))
4047 klist_iter_exit(&i);
4050 EXPORT_SYMBOL_GPL(device_find_child_by_name);
4052 static int match_any(struct device *dev, void *unused)
4058 * device_find_any_child - device iterator for locating a child device, if any.
4059 * @parent: parent struct device
4061 * This is similar to the device_find_child() function above, but it
4062 * returns a reference to a child device, if any.
4064 * NOTE: you will need to drop the reference with put_device() after use.
4066 struct device *device_find_any_child(struct device *parent)
4068 return device_find_child(parent, NULL, match_any);
4070 EXPORT_SYMBOL_GPL(device_find_any_child);
4072 int __init devices_init(void)
4074 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
4077 dev_kobj = kobject_create_and_add("dev", NULL);
4080 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
4081 if (!sysfs_dev_block_kobj)
4082 goto block_kobj_err;
4083 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
4084 if (!sysfs_dev_char_kobj)
4090 kobject_put(sysfs_dev_block_kobj);
4092 kobject_put(dev_kobj);
4094 kset_unregister(devices_kset);
4098 static int device_check_offline(struct device *dev, void *not_used)
4102 ret = device_for_each_child(dev, NULL, device_check_offline);
4106 return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
4110 * device_offline - Prepare the device for hot-removal.
4111 * @dev: Device to be put offline.
4113 * Execute the device bus type's .offline() callback, if present, to prepare
4114 * the device for a subsequent hot-removal. If that succeeds, the device must
4115 * not be used until either it is removed or its bus type's .online() callback
4118 * Call under device_hotplug_lock.
4120 int device_offline(struct device *dev)
4124 if (dev->offline_disabled)
4127 ret = device_for_each_child(dev, NULL, device_check_offline);
4132 if (device_supports_offline(dev)) {
4136 ret = dev->bus->offline(dev);
4138 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
4139 dev->offline = true;
4149 * device_online - Put the device back online after successful device_offline().
4150 * @dev: Device to be put back online.
4152 * If device_offline() has been successfully executed for @dev, but the device
4153 * has not been removed subsequently, execute its bus type's .online() callback
4154 * to indicate that the device can be used again.
4156 * Call under device_hotplug_lock.
4158 int device_online(struct device *dev)
4163 if (device_supports_offline(dev)) {
4165 ret = dev->bus->online(dev);
4167 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
4168 dev->offline = false;
4179 struct root_device {
4181 struct module *owner;
4184 static inline struct root_device *to_root_device(struct device *d)
4186 return container_of(d, struct root_device, dev);
4189 static void root_device_release(struct device *dev)
4191 kfree(to_root_device(dev));
4195 * __root_device_register - allocate and register a root device
4196 * @name: root device name
4197 * @owner: owner module of the root device, usually THIS_MODULE
4199 * This function allocates a root device and registers it
4200 * using device_register(). In order to free the returned
4201 * device, use root_device_unregister().
4203 * Root devices are dummy devices which allow other devices
4204 * to be grouped under /sys/devices. Use this function to
4205 * allocate a root device and then use it as the parent of
4206 * any device which should appear under /sys/devices/{name}
4208 * The /sys/devices/{name} directory will also contain a
4209 * 'module' symlink which points to the @owner directory
4212 * Returns &struct device pointer on success, or ERR_PTR() on error.
4214 * Note: You probably want to use root_device_register().
4216 struct device *__root_device_register(const char *name, struct module *owner)
4218 struct root_device *root;
4221 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
4223 return ERR_PTR(err);
4225 err = dev_set_name(&root->dev, "%s", name);
4228 return ERR_PTR(err);
4231 root->dev.release = root_device_release;
4233 err = device_register(&root->dev);
4235 put_device(&root->dev);
4236 return ERR_PTR(err);
4239 #ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
4241 struct module_kobject *mk = &owner->mkobj;
4243 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
4245 device_unregister(&root->dev);
4246 return ERR_PTR(err);
4248 root->owner = owner;
4254 EXPORT_SYMBOL_GPL(__root_device_register);
4257 * root_device_unregister - unregister and free a root device
4258 * @dev: device going away
4260 * This function unregisters and cleans up a device that was created by
4261 * root_device_register().
4263 void root_device_unregister(struct device *dev)
4265 struct root_device *root = to_root_device(dev);
4268 sysfs_remove_link(&root->dev.kobj, "module");
4270 device_unregister(dev);
4272 EXPORT_SYMBOL_GPL(root_device_unregister);
4275 static void device_create_release(struct device *dev)
4277 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
4281 static __printf(6, 0) struct device *
4282 device_create_groups_vargs(const struct class *class, struct device *parent,
4283 dev_t devt, void *drvdata,
4284 const struct attribute_group **groups,
4285 const char *fmt, va_list args)
4287 struct device *dev = NULL;
4288 int retval = -ENODEV;
4290 if (IS_ERR_OR_NULL(class))
4293 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
4299 device_initialize(dev);
4302 dev->parent = parent;
4303 dev->groups = groups;
4304 dev->release = device_create_release;
4305 dev_set_drvdata(dev, drvdata);
4307 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
4311 retval = device_add(dev);
4319 return ERR_PTR(retval);
4323 * device_create - creates a device and registers it with sysfs
4324 * @class: pointer to the struct class that this device should be registered to
4325 * @parent: pointer to the parent struct device of this new device, if any
4326 * @devt: the dev_t for the char device to be added
4327 * @drvdata: the data to be added to the device for callbacks
4328 * @fmt: string for the device's name
4330 * This function can be used by char device classes. A struct device
4331 * will be created in sysfs, registered to the specified class.
4333 * A "dev" file will be created, showing the dev_t for the device, if
4334 * the dev_t is not 0,0.
4335 * If a pointer to a parent struct device is passed in, the newly created
4336 * struct device will be a child of that device in sysfs.
4337 * The pointer to the struct device will be returned from the call.
4338 * Any further sysfs files that might be required can be created using this
4341 * Returns &struct device pointer on success, or ERR_PTR() on error.
4343 struct device *device_create(const struct class *class, struct device *parent,
4344 dev_t devt, void *drvdata, const char *fmt, ...)
4349 va_start(vargs, fmt);
4350 dev = device_create_groups_vargs(class, parent, devt, drvdata, NULL,
4355 EXPORT_SYMBOL_GPL(device_create);
4358 * device_create_with_groups - creates a device and registers it with sysfs
4359 * @class: pointer to the struct class that this device should be registered to
4360 * @parent: pointer to the parent struct device of this new device, if any
4361 * @devt: the dev_t for the char device to be added
4362 * @drvdata: the data to be added to the device for callbacks
4363 * @groups: NULL-terminated list of attribute groups to be created
4364 * @fmt: string for the device's name
4366 * This function can be used by char device classes. A struct device
4367 * will be created in sysfs, registered to the specified class.
4368 * Additional attributes specified in the groups parameter will also
4369 * be created automatically.
4371 * A "dev" file will be created, showing the dev_t for the device, if
4372 * the dev_t is not 0,0.
4373 * If a pointer to a parent struct device is passed in, the newly created
4374 * struct device will be a child of that device in sysfs.
4375 * The pointer to the struct device will be returned from the call.
4376 * Any further sysfs files that might be required can be created using this
4379 * Returns &struct device pointer on success, or ERR_PTR() on error.
4381 struct device *device_create_with_groups(const struct class *class,
4382 struct device *parent, dev_t devt,
4384 const struct attribute_group **groups,
4385 const char *fmt, ...)
4390 va_start(vargs, fmt);
4391 dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
4396 EXPORT_SYMBOL_GPL(device_create_with_groups);
4399 * device_destroy - removes a device that was created with device_create()
4400 * @class: pointer to the struct class that this device was registered with
4401 * @devt: the dev_t of the device that was previously registered
4403 * This call unregisters and cleans up a device that was created with a
4404 * call to device_create().
4406 void device_destroy(const struct class *class, dev_t devt)
4410 dev = class_find_device_by_devt(class, devt);
4413 device_unregister(dev);
4416 EXPORT_SYMBOL_GPL(device_destroy);
4419 * device_rename - renames a device
4420 * @dev: the pointer to the struct device to be renamed
4421 * @new_name: the new name of the device
4423 * It is the responsibility of the caller to provide mutual
4424 * exclusion between two different calls of device_rename
4425 * on the same device to ensure that new_name is valid and
4426 * won't conflict with other devices.
4428 * Note: given that some subsystems (networking and infiniband) use this
4429 * function, with no immediate plans for this to change, we cannot assume or
4430 * require that this function not be called at all.
4432 * However, if you're writing new code, do not call this function. The following
4433 * text from Kay Sievers offers some insight:
4435 * Renaming devices is racy at many levels, symlinks and other stuff are not
4436 * replaced atomically, and you get a "move" uevent, but it's not easy to
4437 * connect the event to the old and new device. Device nodes are not renamed at
4438 * all, there isn't even support for that in the kernel now.
4440 * In the meantime, during renaming, your target name might be taken by another
4441 * driver, creating conflicts. Or the old name is taken directly after you
4442 * renamed it -- then you get events for the same DEVPATH, before you even see
4443 * the "move" event. It's just a mess, and nothing new should ever rely on
4444 * kernel device renaming. Besides that, it's not even implemented now for
4445 * other things than (driver-core wise very simple) network devices.
4447 * Make up a "real" name in the driver before you register anything, or add
4448 * some other attributes for userspace to find the device, or use udev to add
4449 * symlinks -- but never rename kernel devices later, it's a complete mess. We
4450 * don't even want to get into that and try to implement the missing pieces in
4451 * the core. We really have other pieces to fix in the driver core mess. :)
4453 int device_rename(struct device *dev, const char *new_name)
4455 struct kobject *kobj = &dev->kobj;
4456 char *old_device_name = NULL;
4459 dev = get_device(dev);
4463 dev_dbg(dev, "renaming to %s\n", new_name);
4465 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
4466 if (!old_device_name) {
4472 struct subsys_private *sp = class_to_subsys(dev->class);
4479 error = sysfs_rename_link_ns(&sp->subsys.kobj, kobj, old_device_name,
4480 new_name, kobject_namespace(kobj));
4486 error = kobject_rename(kobj, new_name);
4493 kfree(old_device_name);
4497 EXPORT_SYMBOL_GPL(device_rename);
4499 static int device_move_class_links(struct device *dev,
4500 struct device *old_parent,
4501 struct device *new_parent)
4506 sysfs_remove_link(&dev->kobj, "device");
4508 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
4514 * device_move - moves a device to a new parent
4515 * @dev: the pointer to the struct device to be moved
4516 * @new_parent: the new parent of the device (can be NULL)
4517 * @dpm_order: how to reorder the dpm_list
4519 int device_move(struct device *dev, struct device *new_parent,
4520 enum dpm_order dpm_order)
4523 struct device *old_parent;
4524 struct kobject *new_parent_kobj;
4526 dev = get_device(dev);
4531 new_parent = get_device(new_parent);
4532 new_parent_kobj = get_device_parent(dev, new_parent);
4533 if (IS_ERR(new_parent_kobj)) {
4534 error = PTR_ERR(new_parent_kobj);
4535 put_device(new_parent);
4539 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
4540 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
4541 error = kobject_move(&dev->kobj, new_parent_kobj);
4543 cleanup_glue_dir(dev, new_parent_kobj);
4544 put_device(new_parent);
4547 old_parent = dev->parent;
4548 dev->parent = new_parent;
4550 klist_remove(&dev->p->knode_parent);
4552 klist_add_tail(&dev->p->knode_parent,
4553 &new_parent->p->klist_children);
4554 set_dev_node(dev, dev_to_node(new_parent));
4558 error = device_move_class_links(dev, old_parent, new_parent);
4560 /* We ignore errors on cleanup since we're hosed anyway... */
4561 device_move_class_links(dev, new_parent, old_parent);
4562 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
4564 klist_remove(&dev->p->knode_parent);
4565 dev->parent = old_parent;
4567 klist_add_tail(&dev->p->knode_parent,
4568 &old_parent->p->klist_children);
4569 set_dev_node(dev, dev_to_node(old_parent));
4572 cleanup_glue_dir(dev, new_parent_kobj);
4573 put_device(new_parent);
4577 switch (dpm_order) {
4578 case DPM_ORDER_NONE:
4580 case DPM_ORDER_DEV_AFTER_PARENT:
4581 device_pm_move_after(dev, new_parent);
4582 devices_kset_move_after(dev, new_parent);
4584 case DPM_ORDER_PARENT_BEFORE_DEV:
4585 device_pm_move_before(new_parent, dev);
4586 devices_kset_move_before(new_parent, dev);
4588 case DPM_ORDER_DEV_LAST:
4589 device_pm_move_last(dev);
4590 devices_kset_move_last(dev);
4594 put_device(old_parent);
4600 EXPORT_SYMBOL_GPL(device_move);
4602 static int device_attrs_change_owner(struct device *dev, kuid_t kuid,
4605 struct kobject *kobj = &dev->kobj;
4606 const struct class *class = dev->class;
4607 const struct device_type *type = dev->type;
4612 * Change the device groups of the device class for @dev to
4615 error = sysfs_groups_change_owner(kobj, class->dev_groups, kuid,
4623 * Change the device groups of the device type for @dev to
4626 error = sysfs_groups_change_owner(kobj, type->groups, kuid,
4632 /* Change the device groups of @dev to @kuid/@kgid. */
4633 error = sysfs_groups_change_owner(kobj, dev->groups, kuid, kgid);
4637 if (device_supports_offline(dev) && !dev->offline_disabled) {
4638 /* Change online device attributes of @dev to @kuid/@kgid. */
4639 error = sysfs_file_change_owner(kobj, dev_attr_online.attr.name,
4649 * device_change_owner - change the owner of an existing device.
4651 * @kuid: new owner's kuid
4652 * @kgid: new owner's kgid
4654 * This changes the owner of @dev and its corresponding sysfs entries to
4655 * @kuid/@kgid. This function closely mirrors how @dev was added via driver
4658 * Returns 0 on success or error code on failure.
4660 int device_change_owner(struct device *dev, kuid_t kuid, kgid_t kgid)
4663 struct kobject *kobj = &dev->kobj;
4664 struct subsys_private *sp;
4666 dev = get_device(dev);
4671 * Change the kobject and the default attributes and groups of the
4672 * ktype associated with it to @kuid/@kgid.
4674 error = sysfs_change_owner(kobj, kuid, kgid);
4679 * Change the uevent file for @dev to the new owner. The uevent file
4680 * was created in a separate step when @dev got added and we mirror
4683 error = sysfs_file_change_owner(kobj, dev_attr_uevent.attr.name, kuid,
4689 * Change the device groups, the device groups associated with the
4690 * device class, and the groups associated with the device type of @dev
4693 error = device_attrs_change_owner(dev, kuid, kgid);
4697 error = dpm_sysfs_change_owner(dev, kuid, kgid);
4702 * Change the owner of the symlink located in the class directory of
4703 * the device class associated with @dev which points to the actual
4704 * directory entry for @dev to @kuid/@kgid. This ensures that the
4705 * symlink shows the same permissions as its target.
4707 sp = class_to_subsys(dev->class);
4712 error = sysfs_link_change_owner(&sp->subsys.kobj, &dev->kobj, dev_name(dev), kuid, kgid);
4719 EXPORT_SYMBOL_GPL(device_change_owner);
4722 * device_shutdown - call ->shutdown() on each device to shutdown.
4724 void device_shutdown(void)
4726 struct device *dev, *parent;
4728 wait_for_device_probe();
4729 device_block_probing();
4733 spin_lock(&devices_kset->list_lock);
4735 * Walk the devices list backward, shutting down each in turn.
4736 * Beware that device unplug events may also start pulling
4737 * devices offline, even as the system is shutting down.
4739 while (!list_empty(&devices_kset->list)) {
4740 dev = list_entry(devices_kset->list.prev, struct device,
4744 * hold reference count of device's parent to
4745 * prevent it from being freed because parent's
4746 * lock is to be held
4748 parent = get_device(dev->parent);
4751 * Make sure the device is off the kset list, in the
4752 * event that dev->*->shutdown() doesn't remove it.
4754 list_del_init(&dev->kobj.entry);
4755 spin_unlock(&devices_kset->list_lock);
4757 /* hold lock to avoid race with probe/release */
4759 device_lock(parent);
4762 /* Don't allow any more runtime suspends */
4763 pm_runtime_get_noresume(dev);
4764 pm_runtime_barrier(dev);
4766 if (dev->class && dev->class->shutdown_pre) {
4768 dev_info(dev, "shutdown_pre\n");
4769 dev->class->shutdown_pre(dev);
4771 if (dev->bus && dev->bus->shutdown) {
4773 dev_info(dev, "shutdown\n");
4774 dev->bus->shutdown(dev);
4775 } else if (dev->driver && dev->driver->shutdown) {
4777 dev_info(dev, "shutdown\n");
4778 dev->driver->shutdown(dev);
4783 device_unlock(parent);
4788 spin_lock(&devices_kset->list_lock);
4790 spin_unlock(&devices_kset->list_lock);
4794 * Device logging functions
4797 #ifdef CONFIG_PRINTK
4799 set_dev_info(const struct device *dev, struct dev_printk_info *dev_info)
4803 memset(dev_info, 0, sizeof(*dev_info));
4806 subsys = dev->class->name;
4808 subsys = dev->bus->name;
4812 strscpy(dev_info->subsystem, subsys, sizeof(dev_info->subsystem));
4815 * Add device identifier DEVICE=:
4819 * +sound:card0 subsystem:devname
4821 if (MAJOR(dev->devt)) {
4824 if (strcmp(subsys, "block") == 0)
4829 snprintf(dev_info->device, sizeof(dev_info->device),
4830 "%c%u:%u", c, MAJOR(dev->devt), MINOR(dev->devt));
4831 } else if (strcmp(subsys, "net") == 0) {
4832 struct net_device *net = to_net_dev(dev);
4834 snprintf(dev_info->device, sizeof(dev_info->device),
4835 "n%u", net->ifindex);
4837 snprintf(dev_info->device, sizeof(dev_info->device),
4838 "+%s:%s", subsys, dev_name(dev));
4842 int dev_vprintk_emit(int level, const struct device *dev,
4843 const char *fmt, va_list args)
4845 struct dev_printk_info dev_info;
4847 set_dev_info(dev, &dev_info);
4849 return vprintk_emit(0, level, &dev_info, fmt, args);
4851 EXPORT_SYMBOL(dev_vprintk_emit);
4853 int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
4858 va_start(args, fmt);
4860 r = dev_vprintk_emit(level, dev, fmt, args);
4866 EXPORT_SYMBOL(dev_printk_emit);
4868 static void __dev_printk(const char *level, const struct device *dev,
4869 struct va_format *vaf)
4872 dev_printk_emit(level[1] - '0', dev, "%s %s: %pV",
4873 dev_driver_string(dev), dev_name(dev), vaf);
4875 printk("%s(NULL device *): %pV", level, vaf);
4878 void _dev_printk(const char *level, const struct device *dev,
4879 const char *fmt, ...)
4881 struct va_format vaf;
4884 va_start(args, fmt);
4889 __dev_printk(level, dev, &vaf);
4893 EXPORT_SYMBOL(_dev_printk);
4895 #define define_dev_printk_level(func, kern_level) \
4896 void func(const struct device *dev, const char *fmt, ...) \
4898 struct va_format vaf; \
4901 va_start(args, fmt); \
4906 __dev_printk(kern_level, dev, &vaf); \
4910 EXPORT_SYMBOL(func);
4912 define_dev_printk_level(_dev_emerg, KERN_EMERG);
4913 define_dev_printk_level(_dev_alert, KERN_ALERT);
4914 define_dev_printk_level(_dev_crit, KERN_CRIT);
4915 define_dev_printk_level(_dev_err, KERN_ERR);
4916 define_dev_printk_level(_dev_warn, KERN_WARNING);
4917 define_dev_printk_level(_dev_notice, KERN_NOTICE);
4918 define_dev_printk_level(_dev_info, KERN_INFO);
4923 * dev_err_probe - probe error check and log helper
4924 * @dev: the pointer to the struct device
4925 * @err: error value to test
4926 * @fmt: printf-style format string
4927 * @...: arguments as specified in the format string
4929 * This helper implements common pattern present in probe functions for error
4930 * checking: print debug or error message depending if the error value is
4931 * -EPROBE_DEFER and propagate error upwards.
4932 * In case of -EPROBE_DEFER it sets also defer probe reason, which can be
4933 * checked later by reading devices_deferred debugfs attribute.
4934 * It replaces code sequence::
4936 * if (err != -EPROBE_DEFER)
4937 * dev_err(dev, ...);
4939 * dev_dbg(dev, ...);
4944 * return dev_err_probe(dev, err, ...);
4946 * Note that it is deemed acceptable to use this function for error
4947 * prints during probe even if the @err is known to never be -EPROBE_DEFER.
4948 * The benefit compared to a normal dev_err() is the standardized format
4949 * of the error code and the fact that the error code is returned.
4954 int dev_err_probe(const struct device *dev, int err, const char *fmt, ...)
4956 struct va_format vaf;
4959 va_start(args, fmt);
4963 if (err != -EPROBE_DEFER) {
4964 dev_err(dev, "error %pe: %pV", ERR_PTR(err), &vaf);
4966 device_set_deferred_probe_reason(dev, &vaf);
4967 dev_dbg(dev, "error %pe: %pV", ERR_PTR(err), &vaf);
4974 EXPORT_SYMBOL_GPL(dev_err_probe);
4976 static inline bool fwnode_is_primary(struct fwnode_handle *fwnode)
4978 return fwnode && !IS_ERR(fwnode->secondary);
4982 * set_primary_fwnode - Change the primary firmware node of a given device.
4983 * @dev: Device to handle.
4984 * @fwnode: New primary firmware node of the device.
4986 * Set the device's firmware node pointer to @fwnode, but if a secondary
4987 * firmware node of the device is present, preserve it.
4989 * Valid fwnode cases are:
4990 * - primary --> secondary --> -ENODEV
4991 * - primary --> NULL
4992 * - secondary --> -ENODEV
4995 void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
4997 struct device *parent = dev->parent;
4998 struct fwnode_handle *fn = dev->fwnode;
5001 if (fwnode_is_primary(fn))
5005 WARN_ON(fwnode->secondary);
5006 fwnode->secondary = fn;
5008 dev->fwnode = fwnode;
5010 if (fwnode_is_primary(fn)) {
5011 dev->fwnode = fn->secondary;
5013 /* Skip nullifying fn->secondary if the primary is shared */
5014 if (parent && fn == parent->fwnode)
5017 /* Set fn->secondary = NULL, so fn remains the primary fwnode */
5018 fn->secondary = NULL;
5024 EXPORT_SYMBOL_GPL(set_primary_fwnode);
5027 * set_secondary_fwnode - Change the secondary firmware node of a given device.
5028 * @dev: Device to handle.
5029 * @fwnode: New secondary firmware node of the device.
5031 * If a primary firmware node of the device is present, set its secondary
5032 * pointer to @fwnode. Otherwise, set the device's firmware node pointer to
5035 void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
5038 fwnode->secondary = ERR_PTR(-ENODEV);
5040 if (fwnode_is_primary(dev->fwnode))
5041 dev->fwnode->secondary = fwnode;
5043 dev->fwnode = fwnode;
5045 EXPORT_SYMBOL_GPL(set_secondary_fwnode);
5048 * device_set_of_node_from_dev - reuse device-tree node of another device
5049 * @dev: device whose device-tree node is being set
5050 * @dev2: device whose device-tree node is being reused
5052 * Takes another reference to the new device-tree node after first dropping
5053 * any reference held to the old node.
5055 void device_set_of_node_from_dev(struct device *dev, const struct device *dev2)
5057 of_node_put(dev->of_node);
5058 dev->of_node = of_node_get(dev2->of_node);
5059 dev->of_node_reused = true;
5061 EXPORT_SYMBOL_GPL(device_set_of_node_from_dev);
5063 void device_set_node(struct device *dev, struct fwnode_handle *fwnode)
5065 dev->fwnode = fwnode;
5066 dev->of_node = to_of_node(fwnode);
5068 EXPORT_SYMBOL_GPL(device_set_node);
5070 int device_match_name(struct device *dev, const void *name)
5072 return sysfs_streq(dev_name(dev), name);
5074 EXPORT_SYMBOL_GPL(device_match_name);
5076 int device_match_of_node(struct device *dev, const void *np)
5078 return dev->of_node == np;
5080 EXPORT_SYMBOL_GPL(device_match_of_node);
5082 int device_match_fwnode(struct device *dev, const void *fwnode)
5084 return dev_fwnode(dev) == fwnode;
5086 EXPORT_SYMBOL_GPL(device_match_fwnode);
5088 int device_match_devt(struct device *dev, const void *pdevt)
5090 return dev->devt == *(dev_t *)pdevt;
5092 EXPORT_SYMBOL_GPL(device_match_devt);
5094 int device_match_acpi_dev(struct device *dev, const void *adev)
5096 return ACPI_COMPANION(dev) == adev;
5098 EXPORT_SYMBOL(device_match_acpi_dev);
5100 int device_match_acpi_handle(struct device *dev, const void *handle)
5102 return ACPI_HANDLE(dev) == handle;
5104 EXPORT_SYMBOL(device_match_acpi_handle);
5106 int device_match_any(struct device *dev, const void *unused)
5110 EXPORT_SYMBOL_GPL(device_match_any);