Lines Matching +full:fixed +full:- +full:links
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>
32 #include <linux/dma-map-ops.h> /* for dma_default_coherent */
51 /* Device links support. */
60 * fwnode_link_add - Create a link between two fwnode_handles.
73 * Attempts to create duplicate links between the same pair of fwnode handles
83 list_for_each_entry(link, &sup->consumers, s_hook) in fwnode_link_add()
84 if (link->consumer == con) in fwnode_link_add()
89 ret = -ENOMEM; in fwnode_link_add()
93 link->supplier = sup; in fwnode_link_add()
94 INIT_LIST_HEAD(&link->s_hook); in fwnode_link_add()
95 link->consumer = con; in fwnode_link_add()
96 INIT_LIST_HEAD(&link->c_hook); in fwnode_link_add()
98 list_add(&link->s_hook, &sup->consumers); in fwnode_link_add()
99 list_add(&link->c_hook, &con->suppliers); in fwnode_link_add()
109 * __fwnode_link_del - Delete a link between two fwnode_handles.
117 link->consumer, link->supplier); in __fwnode_link_del()
118 list_del(&link->s_hook); in __fwnode_link_del()
119 list_del(&link->c_hook); in __fwnode_link_del()
124 * fwnode_links_purge_suppliers - Delete all supplier links of fwnode_handle.
125 * @fwnode: fwnode whose supplier links need to be deleted
127 * Deletes all supplier links connecting directly to @fwnode.
134 list_for_each_entry_safe(link, tmp, &fwnode->suppliers, c_hook) in fwnode_links_purge_suppliers()
140 * fwnode_links_purge_consumers - Delete all consumer links of fwnode_handle.
141 * @fwnode: fwnode whose consumer links need to be deleted
143 * Deletes all consumer links connecting directly to @fwnode.
150 list_for_each_entry_safe(link, tmp, &fwnode->consumers, s_hook) in fwnode_links_purge_consumers()
156 * fwnode_links_purge - Delete all links connected to a fwnode_handle.
157 * @fwnode: fwnode whose links needs to be deleted
159 * Deletes all links connecting directly to a fwnode.
171 /* Don't purge consumer links of an added child */ in fw_devlink_purge_absent_suppliers()
172 if (fwnode->dev) in fw_devlink_purge_absent_suppliers()
175 fwnode->flags |= FWNODE_FLAG_NOT_DEVICE; in fw_devlink_purge_absent_suppliers()
219 list_del_rcu(&link->s_node); in device_link_remove_from_lists()
220 list_del_rcu(&link->c_node); in device_link_remove_from_lists()
259 list_del(&link->s_node); in device_link_remove_from_lists()
260 list_del(&link->c_node); in device_link_remove_from_lists()
266 while (target->parent) { in device_is_ancestor()
267 target = target->parent; in device_is_ancestor()
275 * device_is_dependent - Check if one device depends on another one
299 list_for_each_entry(link, &dev->links.consumers, s_node) { in device_is_dependent()
300 if ((link->flags & ~DL_FLAG_INFERRED) == in device_is_dependent()
304 if (link->consumer == target) in device_is_dependent()
307 ret = device_is_dependent(link->consumer, target); in device_is_dependent()
318 switch (supplier->links.status) { in device_link_init_status()
320 switch (consumer->links.status) { in device_link_init_status()
329 link->status = DL_STATE_CONSUMER_PROBE; in device_link_init_status()
332 link->status = DL_STATE_DORMANT; in device_link_init_status()
337 switch (consumer->links.status) { in device_link_init_status()
339 link->status = DL_STATE_CONSUMER_PROBE; in device_link_init_status()
342 link->status = DL_STATE_ACTIVE; in device_link_init_status()
345 link->status = DL_STATE_AVAILABLE; in device_link_init_status()
350 link->status = DL_STATE_SUPPLIER_UNBIND; in device_link_init_status()
353 link->status = DL_STATE_DORMANT; in device_link_init_status()
373 list_for_each_entry(link, &dev->links.consumers, s_node) { in device_reorder_to_tail()
374 if ((link->flags & ~DL_FLAG_INFERRED) == in device_reorder_to_tail()
377 device_reorder_to_tail(link->consumer, NULL); in device_reorder_to_tail()
384 * device_pm_move_to_tail - Move set of devices to the end of device lists
410 switch (to_devlink(dev)->status) { in status_show()
444 if (link->flags & DL_FLAG_AUTOREMOVE_SUPPLIER) in auto_remove_on_show()
446 else if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER) in auto_remove_on_show()
460 return sysfs_emit(buf, "%d\n", !!(link->flags & DL_FLAG_PM_RUNTIME)); in runtime_pm_show()
470 !!(link->flags & DL_FLAG_SYNC_STATE_ONLY)); in sync_state_only_show()
495 * PM-runtime usage counter to remove the reference taken by in device_link_release_fn()
498 if (link->supplier_preactivated) in device_link_release_fn()
499 pm_runtime_put_noidle(link->supplier); in device_link_release_fn()
501 pm_request_idle(link->supplier); in device_link_release_fn()
503 put_device(link->consumer); in device_link_release_fn()
504 put_device(link->supplier); in device_link_release_fn()
512 INIT_WORK(&link->rm_work, device_link_release_fn); in devlink_dev_release()
519 queue_work(system_long_wq, &link->rm_work); in devlink_dev_release()
535 struct device *sup = link->supplier; in devlink_add_symlinks()
536 struct device *con = link->consumer; in devlink_add_symlinks()
545 return -ENOMEM; in devlink_add_symlinks()
547 ret = sysfs_create_link(&link->link_dev.kobj, &sup->kobj, "supplier"); in devlink_add_symlinks()
551 ret = sysfs_create_link(&link->link_dev.kobj, &con->kobj, "consumer"); in devlink_add_symlinks()
556 ret = sysfs_create_link(&sup->kobj, &link->link_dev.kobj, buf); in devlink_add_symlinks()
561 ret = sysfs_create_link(&con->kobj, &link->link_dev.kobj, buf); in devlink_add_symlinks()
569 sysfs_remove_link(&sup->kobj, buf); in devlink_add_symlinks()
571 sysfs_remove_link(&link->link_dev.kobj, "consumer"); in devlink_add_symlinks()
573 sysfs_remove_link(&link->link_dev.kobj, "supplier"); in devlink_add_symlinks()
584 struct device *sup = link->supplier; in devlink_remove_symlinks()
585 struct device *con = link->consumer; in devlink_remove_symlinks()
588 sysfs_remove_link(&link->link_dev.kobj, "consumer"); in devlink_remove_symlinks()
589 sysfs_remove_link(&link->link_dev.kobj, "supplier"); in devlink_remove_symlinks()
603 sysfs_remove_link(&con->kobj, buf); in devlink_remove_symlinks()
606 sysfs_remove_link(&sup->kobj, buf); in devlink_remove_symlinks()
642 * device_link_add - Create a link between two devices.
651 * be forced into the active meta state and reference-counted upon the creation
688 * A side effect of the link creation is re-ordering of dpm_list and the
727 * reverse (non-SYNC_STATE_ONLY) dependency between the consumer and in device_link_add()
740 * SYNC_STATE_ONLY links are useless once a consumer device has probed. in device_link_add()
744 consumer->links.status != DL_DEV_NO_DRIVER && in device_link_add()
745 consumer->links.status != DL_DEV_PROBING) { in device_link_add()
758 list_for_each_entry(link, &supplier->links.consumers, s_node) { in device_link_add()
759 if (link->consumer != consumer) in device_link_add()
762 if (link->flags & DL_FLAG_INFERRED && in device_link_add()
764 link->flags &= ~DL_FLAG_INFERRED; in device_link_add()
767 if (!(link->flags & DL_FLAG_PM_RUNTIME)) { in device_link_add()
769 link->flags |= DL_FLAG_PM_RUNTIME; in device_link_add()
772 refcount_inc(&link->rpm_active); in device_link_add()
776 kref_get(&link->kref); in device_link_add()
777 if (link->flags & DL_FLAG_SYNC_STATE_ONLY && in device_link_add()
778 !(link->flags & DL_FLAG_STATELESS)) { in device_link_add()
779 link->flags |= DL_FLAG_STATELESS; in device_link_add()
782 link->flags |= DL_FLAG_STATELESS; in device_link_add()
793 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER) { in device_link_add()
794 link->flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER; in device_link_add()
795 link->flags |= DL_FLAG_AUTOREMOVE_SUPPLIER; in device_link_add()
798 link->flags &= ~(DL_FLAG_AUTOREMOVE_CONSUMER | in device_link_add()
801 if (!(link->flags & DL_FLAG_MANAGED)) { in device_link_add()
802 kref_get(&link->kref); in device_link_add()
803 link->flags |= DL_FLAG_MANAGED; in device_link_add()
806 if (link->flags & DL_FLAG_SYNC_STATE_ONLY && in device_link_add()
808 link->flags &= ~DL_FLAG_SYNC_STATE_ONLY; in device_link_add()
819 refcount_set(&link->rpm_active, 1); in device_link_add()
822 link->supplier = supplier; in device_link_add()
823 INIT_LIST_HEAD(&link->s_node); in device_link_add()
825 link->consumer = consumer; in device_link_add()
826 INIT_LIST_HEAD(&link->c_node); in device_link_add()
827 link->flags = flags; in device_link_add()
828 kref_init(&link->kref); in device_link_add()
830 link->link_dev.class = &devlink_class; in device_link_add()
831 device_set_pm_not_required(&link->link_dev); in device_link_add()
832 dev_set_name(&link->link_dev, "%s:%s--%s:%s", in device_link_add()
835 if (device_register(&link->link_dev)) { in device_link_add()
836 put_device(&link->link_dev); in device_link_add()
843 refcount_inc(&link->rpm_active); in device_link_add()
850 link->status = DL_STATE_NONE; in device_link_add()
858 if (link->status == DL_STATE_CONSUMER_PROBE && in device_link_add()
862 list_add_tail_rcu(&link->s_node, &supplier->links.consumers); in device_link_add()
863 list_add_tail_rcu(&link->c_node, &consumer->links.suppliers); in device_link_add()
899 dev_dbg(link->consumer, "Dropping the link to %s\n", in __device_link_del()
900 dev_name(link->supplier)); in __device_link_del()
905 device_unregister(&link->link_dev); in __device_link_del()
910 if (link->flags & DL_FLAG_STATELESS) in device_link_put_kref()
911 kref_put(&link->kref, __device_link_del); in device_link_put_kref()
912 else if (!device_is_registered(link->consumer)) in device_link_put_kref()
913 __device_link_del(&link->kref); in device_link_put_kref()
919 * device_link_del - Delete a stateless link between two devices.
924 * Care is required for hotplugged devices: Their links are purged on removal
936 * device_link_remove - Delete a stateless link between two devices.
952 list_for_each_entry(link, &supplier->links.consumers, s_node) { in device_link_remove()
953 if (link->consumer == consumer) { in device_link_remove()
967 list_for_each_entry(link, &dev->links.suppliers, c_node) { in device_links_missing_supplier()
968 if (link->status != DL_STATE_CONSUMER_PROBE) in device_links_missing_supplier()
971 if (link->supplier->links.status == DL_DEV_DRIVER_BOUND) { in device_links_missing_supplier()
972 WRITE_ONCE(link->status, DL_STATE_AVAILABLE); in device_links_missing_supplier()
974 WARN_ON(!(link->flags & DL_FLAG_SYNC_STATE_ONLY)); in device_links_missing_supplier()
975 WRITE_ONCE(link->status, DL_STATE_DORMANT); in device_links_missing_supplier()
982 return (fw_devlink_best_effort && dev->can_match) || in dev_is_best_effort()
983 (dev->fwnode && (dev->fwnode->flags & FWNODE_FLAG_BEST_EFFORT)); in dev_is_best_effort()
987 * device_links_check_suppliers - Check presence of supplier drivers.
990 * Check links from this device to any suppliers. Walk the list of the device's
991 * links to suppliers and see if all of them are available. If not, simply
992 * return -EPROBE_DEFER.
996 * that function checks the device's links to consumers. This means we need to
1000 * Links without the DL_FLAG_MANAGED flag set are ignored.
1013 if (dev->fwnode && !list_empty(&dev->fwnode->suppliers) && in device_links_check_suppliers()
1015 sup_fw = list_first_entry(&dev->fwnode->suppliers, in device_links_check_suppliers()
1017 c_hook)->supplier; in device_links_check_suppliers()
1019 fwnode_ret = -EPROBE_DEFER; in device_links_check_suppliers()
1020 dev_err_probe(dev, -EPROBE_DEFER, in device_links_check_suppliers()
1023 fwnode_ret = -EAGAIN; in device_links_check_suppliers()
1027 if (fwnode_ret == -EPROBE_DEFER) in device_links_check_suppliers()
1032 list_for_each_entry(link, &dev->links.suppliers, c_node) { in device_links_check_suppliers()
1033 if (!(link->flags & DL_FLAG_MANAGED)) in device_links_check_suppliers()
1036 if (link->status != DL_STATE_AVAILABLE && in device_links_check_suppliers()
1037 !(link->flags & DL_FLAG_SYNC_STATE_ONLY)) { in device_links_check_suppliers()
1040 link->flags & DL_FLAG_INFERRED && in device_links_check_suppliers()
1041 !link->supplier->can_match) { in device_links_check_suppliers()
1042 ret = -EAGAIN; in device_links_check_suppliers()
1047 dev_err_probe(dev, -EPROBE_DEFER, in device_links_check_suppliers()
1049 dev_name(link->supplier)); in device_links_check_suppliers()
1050 ret = -EPROBE_DEFER; in device_links_check_suppliers()
1053 WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE); in device_links_check_suppliers()
1055 dev->links.status = DL_DEV_PROBING; in device_links_check_suppliers()
1063 * __device_links_queue_sync_state - Queue a device for sync_state() callback
1067 * Queues a device for a sync_state() callback when the device links write lock
1068 * isn't held. This allows the sync_state() execution flow to use device links
1087 if (dev->state_synced) in __device_links_queue_sync_state()
1090 list_for_each_entry(link, &dev->links.consumers, s_node) { in __device_links_queue_sync_state()
1091 if (!(link->flags & DL_FLAG_MANAGED)) in __device_links_queue_sync_state()
1093 if (link->status != DL_STATE_ACTIVE) in __device_links_queue_sync_state()
1102 dev->state_synced = true; in __device_links_queue_sync_state()
1104 if (WARN_ON(!list_empty(&dev->links.defer_sync))) in __device_links_queue_sync_state()
1108 list_add_tail(&dev->links.defer_sync, list); in __device_links_queue_sync_state()
1112 * device_links_flush_sync_list - Call sync_state() on a list of devices
1126 list_for_each_entry_safe(dev, tmp, list, links.defer_sync) { in device_links_flush_sync_list()
1127 list_del_init(&dev->links.defer_sync); in device_links_flush_sync_list()
1132 if (dev->bus->sync_state) in device_links_flush_sync_list()
1133 dev->bus->sync_state(dev); in device_links_flush_sync_list()
1134 else if (dev->driver && dev->driver->sync_state) in device_links_flush_sync_list()
1135 dev->driver->sync_state(dev); in device_links_flush_sync_list()
1161 defer_sync_state_count--; in device_links_supplier_sync_state_resume()
1165 list_for_each_entry_safe(dev, tmp, &deferred_sync, links.defer_sync) { in device_links_supplier_sync_state_resume()
1170 list_del_init(&dev->links.defer_sync); in device_links_supplier_sync_state_resume()
1188 if (list_empty(&sup->links.defer_sync) && dev_has_sync_state(sup)) in __device_links_supplier_defer_sync()
1189 list_add_tail(&sup->links.defer_sync, &deferred_sync); in __device_links_supplier_defer_sync()
1194 link->flags &= ~DL_FLAG_MANAGED; in device_link_drop_managed()
1195 WRITE_ONCE(link->status, DL_STATE_NONE); in device_link_drop_managed()
1196 kref_put(&link->kref, __device_link_del); in device_link_drop_managed()
1206 val = !list_empty(&dev->fwnode->suppliers); in waiting_for_supplier_show()
1213 * device_links_force_bind - Prepares device to be force bound
1222 * supplier device links and checks if the supplier is bound. If it is, then
1224 * is dropped. Links without the DL_FLAG_MANAGED flag set are ignored.
1232 list_for_each_entry_safe(link, ln, &dev->links.suppliers, c_node) { in device_links_force_bind()
1233 if (!(link->flags & DL_FLAG_MANAGED)) in device_links_force_bind()
1236 if (link->status != DL_STATE_AVAILABLE) { in device_links_force_bind()
1240 WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE); in device_links_force_bind()
1242 dev->links.status = DL_DEV_PROBING; in device_links_force_bind()
1248 * device_links_driver_bound - Update device links after probing its driver.
1249 * @dev: Device to update the links for.
1251 * The probe has been successful, so update links from this device to any
1254 * Also change the status of @dev's links to suppliers to "active".
1256 * Links without the DL_FLAG_MANAGED flag set are ignored.
1265 * the device links it needs to or make new device links as it needs in device_links_driver_bound()
1266 * them. So, fw_devlink no longer needs to create device links to any in device_links_driver_bound()
1274 if (dev->fwnode && dev->fwnode->dev == dev) { in device_links_driver_bound()
1276 fwnode_links_purge_suppliers(dev->fwnode); in device_links_driver_bound()
1277 fwnode_for_each_available_child_node(dev->fwnode, child) in device_links_driver_bound()
1284 list_for_each_entry(link, &dev->links.consumers, s_node) { in device_links_driver_bound()
1285 if (!(link->flags & DL_FLAG_MANAGED)) in device_links_driver_bound()
1289 * Links created during consumer probe may be in the "consumer in device_links_driver_bound()
1294 if (link->status == DL_STATE_CONSUMER_PROBE || in device_links_driver_bound()
1295 link->status == DL_STATE_ACTIVE) in device_links_driver_bound()
1298 WARN_ON(link->status != DL_STATE_DORMANT); in device_links_driver_bound()
1299 WRITE_ONCE(link->status, DL_STATE_AVAILABLE); in device_links_driver_bound()
1301 if (link->flags & DL_FLAG_AUTOPROBE_CONSUMER) in device_links_driver_bound()
1302 driver_deferred_probe_add(link->consumer); in device_links_driver_bound()
1310 list_for_each_entry_safe(link, ln, &dev->links.suppliers, c_node) { in device_links_driver_bound()
1313 if (!(link->flags & DL_FLAG_MANAGED)) in device_links_driver_bound()
1316 supplier = link->supplier; in device_links_driver_bound()
1317 if (link->flags & DL_FLAG_SYNC_STATE_ONLY) { in device_links_driver_bound()
1325 link->flags & DL_FLAG_INFERRED && in device_links_driver_bound()
1326 link->status != DL_STATE_CONSUMER_PROBE && in device_links_driver_bound()
1327 !link->supplier->can_match) { in device_links_driver_bound()
1330 * links to suppliers that don't have a driver. If the in device_links_driver_bound()
1337 WARN_ON(link->status != DL_STATE_CONSUMER_PROBE); in device_links_driver_bound()
1338 WRITE_ONCE(link->status, DL_STATE_ACTIVE); in device_links_driver_bound()
1353 dev->links.status = DL_DEV_DRIVER_BOUND; in device_links_driver_bound()
1361 * __device_links_no_driver - Update links of a device without a driver.
1364 * Delete all non-persistent links from this device to any suppliers.
1366 * Persistent links stay around, but their status is changed to "available",
1370 * Links without the DL_FLAG_MANAGED flag set are ignored.
1376 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) { in __device_links_no_driver()
1377 if (!(link->flags & DL_FLAG_MANAGED)) in __device_links_no_driver()
1380 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER) { in __device_links_no_driver()
1385 if (link->status != DL_STATE_CONSUMER_PROBE && in __device_links_no_driver()
1386 link->status != DL_STATE_ACTIVE) in __device_links_no_driver()
1389 if (link->supplier->links.status == DL_DEV_DRIVER_BOUND) { in __device_links_no_driver()
1390 WRITE_ONCE(link->status, DL_STATE_AVAILABLE); in __device_links_no_driver()
1392 WARN_ON(!(link->flags & DL_FLAG_SYNC_STATE_ONLY)); in __device_links_no_driver()
1393 WRITE_ONCE(link->status, DL_STATE_DORMANT); in __device_links_no_driver()
1397 dev->links.status = DL_DEV_NO_DRIVER; in __device_links_no_driver()
1401 * device_links_no_driver - Update links after failing driver probe.
1404 * Clean up leftover links to consumers for @dev and invoke
1405 * %__device_links_no_driver() to update links to suppliers for it as
1408 * Links without the DL_FLAG_MANAGED flag set are ignored.
1416 list_for_each_entry(link, &dev->links.consumers, s_node) { in device_links_no_driver()
1417 if (!(link->flags & DL_FLAG_MANAGED)) in device_links_no_driver()
1427 if (link->status == DL_STATE_CONSUMER_PROBE || in device_links_no_driver()
1428 link->status == DL_STATE_ACTIVE) in device_links_no_driver()
1429 WRITE_ONCE(link->status, DL_STATE_DORMANT); in device_links_no_driver()
1438 * device_links_driver_cleanup - Update links after driver removal.
1441 * Update links to consumers for @dev by changing their status to "dormant" and
1442 * invoke %__device_links_no_driver() to update links to suppliers for it as
1445 * Links without the DL_FLAG_MANAGED flag set are ignored.
1453 list_for_each_entry_safe(link, ln, &dev->links.consumers, s_node) { in device_links_driver_cleanup()
1454 if (!(link->flags & DL_FLAG_MANAGED)) in device_links_driver_cleanup()
1457 WARN_ON(link->flags & DL_FLAG_AUTOREMOVE_CONSUMER); in device_links_driver_cleanup()
1458 WARN_ON(link->status != DL_STATE_SUPPLIER_UNBIND); in device_links_driver_cleanup()
1461 * autoremove the links between this @dev and its consumer in device_links_driver_cleanup()
1465 if (link->status == DL_STATE_SUPPLIER_UNBIND && in device_links_driver_cleanup()
1466 link->flags & DL_FLAG_AUTOREMOVE_SUPPLIER) in device_links_driver_cleanup()
1469 WRITE_ONCE(link->status, DL_STATE_DORMANT); in device_links_driver_cleanup()
1472 list_del_init(&dev->links.defer_sync); in device_links_driver_cleanup()
1479 * device_links_busy - Check if there are any busy links to consumers.
1490 * Links without the DL_FLAG_MANAGED flag set are ignored.
1499 list_for_each_entry(link, &dev->links.consumers, s_node) { in device_links_busy()
1500 if (!(link->flags & DL_FLAG_MANAGED)) in device_links_busy()
1503 if (link->status == DL_STATE_CONSUMER_PROBE in device_links_busy()
1504 || link->status == DL_STATE_ACTIVE) { in device_links_busy()
1508 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND); in device_links_busy()
1511 dev->links.status = DL_DEV_UNBINDING; in device_links_busy()
1518 * device_links_unbind_consumers - Force unbind consumers of the given device.
1521 * Walk the list of links to consumers for @dev and if any of them is in the
1527 * driver to unbind and start over (the consumer will not re-probe as we have
1530 * Links without the DL_FLAG_MANAGED flag set are ignored.
1539 list_for_each_entry(link, &dev->links.consumers, s_node) { in device_links_unbind_consumers()
1542 if (!(link->flags & DL_FLAG_MANAGED) || in device_links_unbind_consumers()
1543 link->flags & DL_FLAG_SYNC_STATE_ONLY) in device_links_unbind_consumers()
1546 status = link->status; in device_links_unbind_consumers()
1553 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND); in device_links_unbind_consumers()
1555 struct device *consumer = link->consumer; in device_links_unbind_consumers()
1562 consumer->parent); in device_links_unbind_consumers()
1572 * device_links_purge - Delete existing links to other devices.
1579 if (dev->class == &devlink_class) in device_links_purge()
1583 * Delete all of the remaining links from this device to any other in device_links_purge()
1588 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) { in device_links_purge()
1589 WARN_ON(link->status == DL_STATE_ACTIVE); in device_links_purge()
1590 __device_link_del(&link->kref); in device_links_purge()
1593 list_for_each_entry_safe_reverse(link, ln, &dev->links.consumers, s_node) { in device_links_purge()
1594 WARN_ON(link->status != DL_STATE_DORMANT && in device_links_purge()
1595 link->status != DL_STATE_NONE); in device_links_purge()
1596 __device_link_del(&link->kref); in device_links_purge()
1613 return -EINVAL; in fw_devlink_setup()
1652 if (fwnode->flags & FWNODE_FLAG_LINKS_ADDED) in fw_devlink_parse_fwnode()
1656 fwnode->flags |= FWNODE_FLAG_LINKS_ADDED; in fw_devlink_parse_fwnode()
1671 if (!(link->flags & DL_FLAG_INFERRED)) in fw_devlink_relax_link()
1674 if (link->flags == (DL_FLAG_MANAGED | FW_DEVLINK_FLAGS_PERMISSIVE)) in fw_devlink_relax_link()
1678 link->flags = DL_FLAG_MANAGED | FW_DEVLINK_FLAGS_PERMISSIVE; in fw_devlink_relax_link()
1679 dev_dbg(link->consumer, "Relaxing link with %s\n", in fw_devlink_relax_link()
1680 dev_name(link->supplier)); in fw_devlink_relax_link()
1687 if (!link->supplier->can_match) in fw_devlink_no_driver()
1703 * wait_for_init_devices_probe - Try to probe any device needed for init
1766 list_for_each_entry(link, &dev->links.consumers, s_node) in fw_devlink_unblock_consumers()
1772 * fw_devlink_relax_cycle - Convert cyclic links to SYNC_STATE_ONLY links
1778 * device links created solely by fw_devlink into SYNC_STATE_ONLY device links.
1797 list_for_each_entry(link, &con->links.consumers, s_node) { in fw_devlink_relax_cycle()
1798 if ((link->flags & ~DL_FLAG_INFERRED) == in fw_devlink_relax_cycle()
1802 if (!fw_devlink_relax_cycle(link->consumer, sup)) in fw_devlink_relax_cycle()
1813 * fw_devlink_create_devlink - Create a device link from a consumer to fwnode
1822 * fwnode links can sometimes cause the supplier device to never be created.
1828 * -EINVAL if the device link cannot be created as expected
1829 * -EAGAIN if the device link cannot be created right now, but it may be
1850 * When such a flag is set, we can't create device links where P is the in fw_devlink_create_devlink()
1853 if (sup_handle->flags & FWNODE_FLAG_NEEDS_CHILD_BOUND_ON_ADD && in fw_devlink_create_devlink()
1854 fwnode_is_ancestor_of(sup_handle, con->fwnode)) in fw_devlink_create_devlink()
1855 return -EINVAL; in fw_devlink_create_devlink()
1864 if (sup_dev->links.status == DL_DEV_NO_DRIVER && in fw_devlink_create_devlink()
1865 sup_handle->flags & FWNODE_FLAG_INITIALIZED) { in fw_devlink_create_devlink()
1866 ret = -EINVAL; in fw_devlink_create_devlink()
1871 * If this fails, it is due to cycles in device links. Just in fw_devlink_create_devlink()
1883 ret = -EINVAL; in fw_devlink_create_devlink()
1890 if (sup_handle->flags & FWNODE_FLAG_INITIALIZED) in fw_devlink_create_devlink()
1891 return -EINVAL; in fw_devlink_create_devlink()
1899 return -EAGAIN; in fw_devlink_create_devlink()
1911 * dependency. Do this by relaxing all the fw_devlink device links in in fw_devlink_create_devlink()
1922 ret = -EINVAL; in fw_devlink_create_devlink()
1928 ret = -EAGAIN; in fw_devlink_create_devlink()
1937 * __fw_devlink_link_to_consumers - Create device links to consumers of a device
1941 * links between the consumer device and @dev (supplier).
1949 * Once device links are created from the real consumer to @dev (supplier), the
1950 * fwnode links are deleted.
1954 struct fwnode_handle *fwnode = dev->fwnode; in __fw_devlink_link_to_consumers()
1957 list_for_each_entry_safe(link, tmp, &fwnode->consumers, s_hook) { in __fw_devlink_link_to_consumers()
1963 con_dev = get_dev_from_fwnode(link->consumer); in __fw_devlink_link_to_consumers()
1975 con_dev = fwnode_get_next_parent_dev(link->consumer); in __fw_devlink_link_to_consumers()
1979 * consumer-supplier link from the parent to its child in __fw_devlink_link_to_consumers()
1983 fwnode_is_ancestor_of(con_dev->fwnode, fwnode)) { in __fw_devlink_link_to_consumers()
1997 if (!own_link || ret == -EAGAIN) in __fw_devlink_link_to_consumers()
2005 * __fw_devlink_link_to_suppliers - Create device links to suppliers of a device
2007 * @fwnode: Root of the fwnode tree that is used to create device links
2010 * @fwnode and creates device links between @dev (consumer) and all the
2013 * The function creates normal (non-SYNC_STATE_ONLY) device links between @dev
2014 * and the real suppliers of @dev. Once these device links are created, the
2015 * fwnode links are deleted. When such device links are successfully created,
2017 * needed to detect and break some invalid cycles in fwnode links. See
2023 * driver core. So, this function creates SYNC_STATE_ONLY device links between
2026 * their device links. The fwnode links that correspond to the child devices
2027 * aren't delete because they are needed later to create the device links
2033 bool own_link = (dev->fwnode == fwnode); in __fw_devlink_link_to_suppliers()
2043 list_for_each_entry_safe(link, tmp, &fwnode->suppliers, c_hook) { in __fw_devlink_link_to_suppliers()
2046 struct fwnode_handle *sup = link->supplier; in __fw_devlink_link_to_suppliers()
2049 if (!own_link || ret == -EAGAIN) in __fw_devlink_link_to_suppliers()
2063 * fwnode links that haven't been converted to a device link in __fw_devlink_link_to_suppliers()
2069 * dependency fwnode links to be converted to device links. in __fw_devlink_link_to_suppliers()
2072 __fw_devlink_link_to_suppliers(sup_dev, sup_dev->fwnode); in __fw_devlink_link_to_suppliers()
2077 * Make "proxy" SYNC_STATE_ONLY device links to represent the needs of in __fw_devlink_link_to_suppliers()
2088 struct fwnode_handle *fwnode = dev->fwnode; in fw_devlink_link_device()
2101 /* Device links support end. */
2134 return !(dev->type == &part_type); in device_is_not_partition()
2164 * dev_driver_string - Return a device's driver name, if at all possible
2176 /* dev->driver can change to NULL underneath us because of unbinding, in dev_driver_string()
2177 * so be careful about accessing it. dev->bus and dev->class should in dev_driver_string()
2180 drv = READ_ONCE(dev->driver); in dev_driver_string()
2181 return drv ? drv->name : dev_bus_name(dev); in dev_driver_string()
2192 ssize_t ret = -EIO; in dev_attr_show()
2194 if (dev_attr->show) in dev_attr_show()
2195 ret = dev_attr->show(dev, dev_attr, buf); in dev_attr_show()
2198 dev_attr->show); in dev_attr_show()
2208 ssize_t ret = -EIO; in dev_attr_store()
2210 if (dev_attr->store) in dev_attr_store()
2211 ret = dev_attr->store(dev, dev_attr, buf, count); in dev_attr_store()
2233 *(unsigned long *)(ea->var) = new; in device_store_ulong()
2244 return sysfs_emit(buf, "%lx\n", *(unsigned long *)(ea->var)); in device_show_ulong()
2261 return -EINVAL; in device_store_int()
2262 *(int *)(ea->var) = new; in device_store_int()
2274 return sysfs_emit(buf, "%d\n", *(int *)(ea->var)); in device_show_int()
2283 if (strtobool(buf, ea->var) < 0) in device_store_bool()
2284 return -EINVAL; in device_store_bool()
2295 return sysfs_emit(buf, "%d\n", *(bool *)(ea->var)); in device_show_bool()
2300 * device_release - free device structure.
2310 struct device_private *p = dev->p; in device_release()
2323 kfree(dev->dma_range_map); in device_release()
2325 if (dev->release) in device_release()
2326 dev->release(dev); in device_release()
2327 else if (dev->type && dev->type->release) in device_release()
2328 dev->type->release(dev); in device_release()
2329 else if (dev->class && dev->class->dev_release) in device_release()
2330 dev->class->dev_release(dev); in device_release()
2332 …does not have a release() function, it is broken and must be fixed. See Documentation/core-api/kob… in device_release()
2342 if (dev->class && dev->class->ns_type) in device_namespace()
2343 ns = dev->class->namespace(dev); in device_namespace()
2352 if (dev->class && dev->class->get_ownership) in device_get_ownership()
2353 dev->class->get_ownership(dev, uid, gid); in device_get_ownership()
2370 if (dev->bus) in dev_uevent_filter()
2372 if (dev->class) in dev_uevent_filter()
2382 if (dev->bus) in dev_uevent_name()
2383 return dev->bus->name; in dev_uevent_name()
2384 if (dev->class) in dev_uevent_name()
2385 return dev->class->name; in dev_uevent_name()
2395 if (MAJOR(dev->devt)) { in dev_uevent()
2402 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt)); in dev_uevent()
2403 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt)); in dev_uevent()
2417 if (dev->type && dev->type->name) in dev_uevent()
2418 add_uevent_var(env, "DEVTYPE=%s", dev->type->name); in dev_uevent()
2420 if (dev->driver) in dev_uevent()
2421 add_uevent_var(env, "DRIVER=%s", dev->driver->name); in dev_uevent()
2427 if (dev->bus && dev->bus->uevent) { in dev_uevent()
2428 retval = dev->bus->uevent(dev, env); in dev_uevent()
2435 if (dev->class && dev->class->dev_uevent) { in dev_uevent()
2436 retval = dev->class->dev_uevent(dev, env); in dev_uevent()
2444 if (dev->type && dev->type->uevent) { in dev_uevent()
2445 retval = dev->type->uevent(dev, env); in dev_uevent()
2472 top_kobj = &dev->kobj; in uevent_show()
2473 while (!top_kobj->kset && top_kobj->parent) in uevent_show()
2474 top_kobj = top_kobj->parent; in uevent_show()
2475 if (!top_kobj->kset) in uevent_show()
2478 kset = top_kobj->kset; in uevent_show()
2479 if (!kset->uevent_ops || !kset->uevent_ops->uevent) in uevent_show()
2483 if (kset->uevent_ops && kset->uevent_ops->filter) in uevent_show()
2484 if (!kset->uevent_ops->filter(&dev->kobj)) in uevent_show()
2489 return -ENOMEM; in uevent_show()
2492 retval = kset->uevent_ops->uevent(&dev->kobj, env); in uevent_show()
2497 for (i = 0; i < env->envp_idx; i++) in uevent_show()
2498 len += sysfs_emit_at(buf, len, "%s\n", env->envp[i]); in uevent_show()
2509 rc = kobject_synth_uevent(&dev->kobj, buf, count); in uevent_store()
2526 val = !dev->offline; in online_show()
2556 switch (dev->removable) { in removable_show()
2561 loc = "fixed"; in removable_show()
2572 return sysfs_create_groups(&dev->kobj, groups); in device_add_groups()
2579 sysfs_remove_groups(&dev->kobj, groups); in device_remove_groups()
2590 return ((union device_attr_group_devres *)res)->group == data; in devm_attr_group_match()
2596 const struct attribute_group *group = devres->group; in devm_attr_group_remove()
2599 sysfs_remove_group(&dev->kobj, group); in devm_attr_group_remove()
2605 const struct attribute_group **groups = devres->groups; in devm_attr_groups_remove()
2608 sysfs_remove_groups(&dev->kobj, groups); in devm_attr_groups_remove()
2612 * devm_device_add_group - given a device, create a managed attribute group
2629 return -ENOMEM; in devm_device_add_group()
2631 error = sysfs_create_group(&dev->kobj, grp); in devm_device_add_group()
2637 devres->group = grp; in devm_device_add_group()
2661 * devm_device_add_groups - create a bunch of managed attribute groups
2682 return -ENOMEM; in devm_device_add_groups()
2684 error = sysfs_create_groups(&dev->kobj, groups); in devm_device_add_groups()
2690 devres->groups = groups; in devm_device_add_groups()
2697 * devm_device_remove_groups - remove a list of managed groups
2715 struct class *class = dev->class; in device_add_attrs()
2716 const struct device_type *type = dev->type; in device_add_attrs()
2720 error = device_add_groups(dev, class->dev_groups); in device_add_attrs()
2726 error = device_add_groups(dev, type->groups); in device_add_attrs()
2731 error = device_add_groups(dev, dev->groups); in device_add_attrs()
2735 if (device_supports_offline(dev) && !dev->offline_disabled) { in device_add_attrs()
2741 if (fw_devlink_flags && !fw_devlink_is_permissive() && dev->fwnode) { in device_add_attrs()
2769 device_remove_groups(dev, dev->groups); in device_add_attrs()
2772 device_remove_groups(dev, type->groups); in device_add_attrs()
2775 device_remove_groups(dev, class->dev_groups); in device_add_attrs()
2782 struct class *class = dev->class; in device_remove_attrs()
2783 const struct device_type *type = dev->type; in device_remove_attrs()
2785 if (dev->physical_location) { in device_remove_attrs()
2787 kfree(dev->physical_location); in device_remove_attrs()
2793 device_remove_groups(dev, dev->groups); in device_remove_attrs()
2796 device_remove_groups(dev, type->groups); in device_remove_attrs()
2799 device_remove_groups(dev, class->dev_groups); in device_remove_attrs()
2805 return print_dev_t(buf, dev->devt); in dev_show()
2813 * devices_kset_move_before - Move device in the devices_kset's list.
2823 spin_lock(&devices_kset->list_lock); in devices_kset_move_before()
2824 list_move_tail(&deva->kobj.entry, &devb->kobj.entry); in devices_kset_move_before()
2825 spin_unlock(&devices_kset->list_lock); in devices_kset_move_before()
2829 * devices_kset_move_after - Move device in the devices_kset's list.
2839 spin_lock(&devices_kset->list_lock); in devices_kset_move_after()
2840 list_move(&deva->kobj.entry, &devb->kobj.entry); in devices_kset_move_after()
2841 spin_unlock(&devices_kset->list_lock); in devices_kset_move_after()
2845 * devices_kset_move_last - move the device to the end of devices_kset's list.
2853 spin_lock(&devices_kset->list_lock); in devices_kset_move_last()
2854 list_move_tail(&dev->kobj.entry, &devices_kset->list); in devices_kset_move_last()
2855 spin_unlock(&devices_kset->list_lock); in devices_kset_move_last()
2859 * device_create_file - create sysfs attribute file for device.
2869 WARN(((attr->attr.mode & S_IWUGO) && !attr->store), in device_create_file()
2871 attr->attr.name); in device_create_file()
2872 WARN(((attr->attr.mode & S_IRUGO) && !attr->show), in device_create_file()
2874 attr->attr.name); in device_create_file()
2875 error = sysfs_create_file(&dev->kobj, &attr->attr); in device_create_file()
2883 * device_remove_file - remove sysfs attribute file.
2891 sysfs_remove_file(&dev->kobj, &attr->attr); in device_remove_file()
2896 * device_remove_file_self - remove sysfs attribute file from its own method.
2906 return sysfs_remove_file_self(&dev->kobj, &attr->attr); in device_remove_file_self()
2913 * device_create_bin_file - create sysfs binary attribute file for device.
2920 int error = -EINVAL; in device_create_bin_file()
2922 error = sysfs_create_bin_file(&dev->kobj, attr); in device_create_bin_file()
2928 * device_remove_bin_file - remove sysfs binary attribute file
2936 sysfs_remove_bin_file(&dev->kobj, attr); in device_remove_bin_file()
2943 struct device *dev = p->device; in klist_children_get()
2951 struct device *dev = p->device; in klist_children_put()
2957 * device_initialize - init device structure.
2978 dev->kobj.kset = devices_kset; in device_initialize()
2979 kobject_init(&dev->kobj, &device_ktype); in device_initialize()
2980 INIT_LIST_HEAD(&dev->dma_pools); in device_initialize()
2981 mutex_init(&dev->mutex); in device_initialize()
2982 lockdep_set_novalidate_class(&dev->mutex); in device_initialize()
2983 spin_lock_init(&dev->devres_lock); in device_initialize()
2984 INIT_LIST_HEAD(&dev->devres_head); in device_initialize()
2987 INIT_LIST_HEAD(&dev->links.consumers); in device_initialize()
2988 INIT_LIST_HEAD(&dev->links.suppliers); in device_initialize()
2989 INIT_LIST_HEAD(&dev->links.defer_sync); in device_initialize()
2990 dev->links.status = DL_DEV_NO_DRIVER; in device_initialize()
2994 dev->dma_coherent = dma_default_coherent; in device_initialize()
2997 dev->dma_io_tlb_mem = &io_tlb_default_mem; in device_initialize()
3008 &devices_kset->kobj); in virtual_device_parent()
3030 return dir->class->ns_type; in class_dir_child_ns_type()
3047 return ERR_PTR(-ENOMEM); in class_dir_create_and_add()
3049 dir->class = class; in class_dir_create_and_add()
3050 kobject_init(&dir->kobj, &class_dir_ktype); in class_dir_create_and_add()
3052 dir->kobj.kset = &class->p->glue_dirs; in class_dir_create_and_add()
3054 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name); in class_dir_create_and_add()
3056 kobject_put(&dir->kobj); in class_dir_create_and_add()
3059 return &dir->kobj; in class_dir_create_and_add()
3067 if (dev->class) { in get_device_parent()
3074 if (sysfs_deprecated && dev->class == &block_class) { in get_device_parent()
3075 if (parent && parent->class == &block_class) in get_device_parent()
3076 return &parent->kobj; in get_device_parent()
3077 return &block_class.p->subsys.kobj; in get_device_parent()
3083 * Class-devices with a non class-device as parent, live in get_device_parent()
3088 else if (parent->class && !dev->class->ns_type) in get_device_parent()
3089 return &parent->kobj; in get_device_parent()
3091 parent_kobj = &parent->kobj; in get_device_parent()
3095 /* find our class-directory at the parent and reference it */ in get_device_parent()
3096 spin_lock(&dev->class->p->glue_dirs.list_lock); in get_device_parent()
3097 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry) in get_device_parent()
3098 if (k->parent == parent_kobj) { in get_device_parent()
3102 spin_unlock(&dev->class->p->glue_dirs.list_lock); in get_device_parent()
3108 /* or create a new class-directory at the parent device */ in get_device_parent()
3109 k = class_dir_create_and_add(dev->class, parent_kobj); in get_device_parent()
3116 if (!parent && dev->bus && dev->bus->dev_root) in get_device_parent()
3117 return &dev->bus->dev_root->kobj; in get_device_parent()
3120 return &parent->kobj; in get_device_parent()
3127 if (!kobj || !dev->class || in live_in_glue_dir()
3128 kobj->kset != &dev->class->p->glue_dirs) in live_in_glue_dir()
3135 return dev->kobj.parent; in get_glue_dir()
3139 * kobject_has_children - Returns whether a kobject has children.
3150 WARN_ON_ONCE(kref_read(&kobj->kref) == 0); in kobject_has_children()
3152 return kobj->sd && kobj->sd->dir.subdirs; in kobject_has_children()
3195 * sysfs_remove_dir() // glue_dir->sd=NULL in cleanup_glue_dir()
3196 * sysfs_put() // free glue_dir->sd in cleanup_glue_dir()
3208 * and sysfs_put(). This result in glue_dir->sd is freed. in cleanup_glue_dir()
3217 ref = kref_read(&glue_dir->kref); in cleanup_glue_dir()
3218 if (!kobject_has_children(glue_dir) && !--ref) in cleanup_glue_dir()
3230 error = sysfs_create_link(&dev->kobj, of_node_kobj(of_node), "of_node"); in device_add_class_symlinks()
3236 if (!dev->class) in device_add_class_symlinks()
3239 error = sysfs_create_link(&dev->kobj, in device_add_class_symlinks()
3240 &dev->class->p->subsys.kobj, in device_add_class_symlinks()
3245 if (dev->parent && device_is_not_partition(dev)) { in device_add_class_symlinks()
3246 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj, in device_add_class_symlinks()
3254 if (sysfs_deprecated && dev->class == &block_class) in device_add_class_symlinks()
3259 error = sysfs_create_link(&dev->class->p->subsys.kobj, in device_add_class_symlinks()
3260 &dev->kobj, dev_name(dev)); in device_add_class_symlinks()
3267 sysfs_remove_link(&dev->kobj, "device"); in device_add_class_symlinks()
3270 sysfs_remove_link(&dev->kobj, "subsystem"); in device_add_class_symlinks()
3272 sysfs_remove_link(&dev->kobj, "of_node"); in device_add_class_symlinks()
3279 sysfs_remove_link(&dev->kobj, "of_node"); in device_remove_class_symlinks()
3281 if (!dev->class) in device_remove_class_symlinks()
3284 if (dev->parent && device_is_not_partition(dev)) in device_remove_class_symlinks()
3285 sysfs_remove_link(&dev->kobj, "device"); in device_remove_class_symlinks()
3286 sysfs_remove_link(&dev->kobj, "subsystem"); in device_remove_class_symlinks()
3288 if (sysfs_deprecated && dev->class == &block_class) in device_remove_class_symlinks()
3291 sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev)); in device_remove_class_symlinks()
3295 * dev_set_name - set a device name
3305 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs); in dev_set_name()
3312 * device_to_dev_kobj - select a /sys/dev/ directory for the device
3315 * By default we select char/ for new entries. Setting class->dev_obj
3316 * to NULL prevents an entry from being created. class->dev_kobj must
3326 if (dev->class) in device_to_dev_kobj()
3327 kobj = dev->class->dev_kobj; in device_to_dev_kobj()
3341 format_dev_t(devt_str, dev->devt); in device_create_sys_dev_entry()
3342 error = sysfs_create_link(kobj, &dev->kobj, devt_str); in device_create_sys_dev_entry()
3354 format_dev_t(devt_str, dev->devt); in device_remove_sys_dev_entry()
3361 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL); in device_private_init()
3362 if (!dev->p) in device_private_init()
3363 return -ENOMEM; in device_private_init()
3364 dev->p->device = dev; in device_private_init()
3365 klist_init(&dev->p->klist_children, klist_children_get, in device_private_init()
3367 INIT_LIST_HEAD(&dev->p->deferred_probe); in device_private_init()
3372 * device_add - add device to device hierarchy.
3403 int error = -EINVAL; in device_add()
3410 if (!dev->p) { in device_add()
3421 if (dev->init_name) { in device_add()
3422 dev_set_name(dev, "%s", dev->init_name); in device_add()
3423 dev->init_name = NULL; in device_add()
3427 if (!dev_name(dev) && dev->bus && dev->bus->dev_name) in device_add()
3428 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id); in device_add()
3431 error = -EINVAL; in device_add()
3437 parent = get_device(dev->parent); in device_add()
3444 dev->kobj.parent = kobj; in device_add()
3452 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL); in device_add()
3479 if (MAJOR(dev->devt)) { in device_add()
3494 if (dev->bus) in device_add()
3495 blocking_notifier_call_chain(&dev->bus->p->bus_notifier, in device_add()
3498 kobject_uevent(&dev->kobj, KOBJ_ADD); in device_add()
3512 if (dev->fwnode && !dev->fwnode->dev) { in device_add()
3513 dev->fwnode->dev = dev; in device_add()
3524 if (dev->fwnode && fw_devlink_drv_reg_done && !dev->can_match) in device_add()
3528 klist_add_tail(&dev->p->knode_parent, in device_add()
3529 &parent->p->klist_children); in device_add()
3531 if (dev->class) { in device_add()
3532 mutex_lock(&dev->class->p->mutex); in device_add()
3534 klist_add_tail(&dev->p->knode_class, in device_add()
3535 &dev->class->p->klist_devices); in device_add()
3539 &dev->class->p->interfaces, node) in device_add()
3540 if (class_intf->add_dev) in device_add()
3541 class_intf->add_dev(dev, class_intf); in device_add()
3542 mutex_unlock(&dev->class->p->mutex); in device_add()
3548 if (MAJOR(dev->devt)) in device_add()
3563 kobject_uevent(&dev->kobj, KOBJ_REMOVE); in device_add()
3565 kobject_del(&dev->kobj); in device_add()
3571 kfree(dev->p); in device_add()
3572 dev->p = NULL; in device_add()
3578 * device_register - register a device with the system.
3581 * This happens in two clean steps - initialize the device
3603 * get_device - increment reference count for device.
3612 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL; in get_device()
3617 * put_device - decrement reference count.
3624 kobject_put(&dev->kobj); in put_device()
3639 if (dev->p->dead) in kill_device()
3641 dev->p->dead = true; in kill_device()
3647 * device_del - delete device from system.
3661 struct device *parent = dev->parent; in device_del()
3670 if (dev->fwnode && dev->fwnode->dev == dev) in device_del()
3671 dev->fwnode->dev = NULL; in device_del()
3677 if (dev->bus) in device_del()
3678 blocking_notifier_call_chain(&dev->bus->p->bus_notifier, in device_del()
3683 klist_del(&dev->p->knode_parent); in device_del()
3684 if (MAJOR(dev->devt)) { in device_del()
3689 if (dev->class) { in device_del()
3692 mutex_lock(&dev->class->p->mutex); in device_del()
3695 &dev->class->p->interfaces, node) in device_del()
3696 if (class_intf->remove_dev) in device_del()
3697 class_intf->remove_dev(dev, class_intf); in device_del()
3699 klist_del(&dev->p->knode_class); in device_del()
3700 mutex_unlock(&dev->class->p->mutex); in device_del()
3710 if (dev->bus) in device_del()
3711 blocking_notifier_call_chain(&dev->bus->p->bus_notifier, in device_del()
3713 kobject_uevent(&dev->kobj, KOBJ_REMOVE); in device_del()
3715 kobject_del(&dev->kobj); in device_del()
3723 * device_unregister - unregister device from system.
3749 dev = p->device; in prev_device()
3762 dev = p->device; in next_device()
3768 * device_get_devnode - path of device node file
3776 * Non-default names may need to allocate a memory to compose
3789 if (dev->type && dev->type->devnode) in device_get_devnode()
3790 *tmp = dev->type->devnode(dev, mode, uid, gid); in device_get_devnode()
3795 if (dev->class && dev->class->devnode) in device_get_devnode()
3796 *tmp = dev->class->devnode(dev, mode); in device_get_devnode()
3813 * device_for_each_child - device child iterator.
3831 if (!parent->p) in device_for_each_child()
3834 klist_iter_init(&parent->p->klist_children, &i); in device_for_each_child()
3843 * device_for_each_child_reverse - device child iterator in reversed order.
3861 if (!parent->p) in device_for_each_child_reverse()
3864 klist_iter_init(&parent->p->klist_children, &i); in device_for_each_child_reverse()
3873 * device_find_child - device iterator for locating a particular device.
3882 * The callback should return 0 if the device doesn't match and non-zero
3883 * if it does. If the callback returns non-zero and a reference to the
3898 klist_iter_init(&parent->p->klist_children, &i); in device_find_child()
3908 * device_find_child_by_name - device iterator for locating a child device.
3926 klist_iter_init(&parent->p->klist_children, &i); in device_find_child_by_name()
3941 * device_find_any_child - device iterator for locating a child device, if any.
3959 return -ENOMEM; in devices_init()
3978 return -ENOMEM; in devices_init()
3989 return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0; in device_check_offline()
3993 * device_offline - Prepare the device for hot-removal.
3997 * the device for a subsequent hot-removal. If that succeeds, the device must
4007 if (dev->offline_disabled) in device_offline()
4008 return -EPERM; in device_offline()
4016 if (dev->offline) { in device_offline()
4019 ret = dev->bus->offline(dev); in device_offline()
4021 kobject_uevent(&dev->kobj, KOBJ_OFFLINE); in device_offline()
4022 dev->offline = true; in device_offline()
4032 * device_online - Put the device back online after successful device_offline().
4047 if (dev->offline) { in device_online()
4048 ret = dev->bus->online(dev); in device_online()
4050 kobject_uevent(&dev->kobj, KOBJ_ONLINE); in device_online()
4051 dev->offline = false; in device_online()
4078 * __root_device_register - allocate and register a root device
4102 int err = -ENOMEM; in __root_device_register()
4108 err = dev_set_name(&root->dev, "%s", name); in __root_device_register()
4114 root->dev.release = root_device_release; in __root_device_register()
4116 err = device_register(&root->dev); in __root_device_register()
4118 put_device(&root->dev); in __root_device_register()
4124 struct module_kobject *mk = &owner->mkobj; in __root_device_register()
4126 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module"); in __root_device_register()
4128 device_unregister(&root->dev); in __root_device_register()
4131 root->owner = owner; in __root_device_register()
4135 return &root->dev; in __root_device_register()
4140 * root_device_unregister - unregister and free a root device
4150 if (root->owner) in root_device_unregister()
4151 sysfs_remove_link(&root->dev.kobj, "module"); in root_device_unregister()
4171 int retval = -ENODEV; in device_create_groups_vargs()
4178 retval = -ENOMEM; in device_create_groups_vargs()
4183 dev->devt = devt; in device_create_groups_vargs()
4184 dev->class = class; in device_create_groups_vargs()
4185 dev->parent = parent; in device_create_groups_vargs()
4186 dev->groups = groups; in device_create_groups_vargs()
4187 dev->release = device_create_release; in device_create_groups_vargs()
4190 retval = kobject_set_name_vargs(&dev->kobj, fmt, args); in device_create_groups_vargs()
4206 * device_create - creates a device and registers it with sysfs
4244 * device_create_with_groups - creates a device and registers it with sysfs
4249 * @groups: NULL-terminated list of attribute groups to be created
4288 * device_destroy - removes a device that was created with device_create()
4308 * device_rename - renames a device
4328 * renamed it -- then you get events for the same DEVPATH, before you even see
4331 * other things than (driver-core wise very simple) network devices.
4336 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
4337 * be allowed to some other name than eth[0-9]*, for the aforementioned
4342 * symlinks -- but never rename kernel devices later, it's a complete mess. We
4348 struct kobject *kobj = &dev->kobj; in device_rename()
4354 return -EINVAL; in device_rename()
4360 error = -ENOMEM; in device_rename()
4364 if (dev->class) { in device_rename()
4365 error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj, in device_rename()
4392 sysfs_remove_link(&dev->kobj, "device"); in device_move_class_links()
4394 error = sysfs_create_link(&dev->kobj, &new_parent->kobj, in device_move_class_links()
4400 * device_move - moves a device to a new parent
4414 return -EINVAL; in device_move()
4427 error = kobject_move(&dev->kobj, new_parent_kobj); in device_move()
4433 old_parent = dev->parent; in device_move()
4434 dev->parent = new_parent; in device_move()
4436 klist_remove(&dev->p->knode_parent); in device_move()
4438 klist_add_tail(&dev->p->knode_parent, in device_move()
4439 &new_parent->p->klist_children); in device_move()
4443 if (dev->class) { in device_move()
4448 if (!kobject_move(&dev->kobj, &old_parent->kobj)) { in device_move()
4450 klist_remove(&dev->p->knode_parent); in device_move()
4451 dev->parent = old_parent; in device_move()
4453 klist_add_tail(&dev->p->knode_parent, in device_move()
4454 &old_parent->p->klist_children); in device_move()
4491 struct kobject *kobj = &dev->kobj; in device_attrs_change_owner()
4492 struct class *class = dev->class; in device_attrs_change_owner()
4493 const struct device_type *type = dev->type; in device_attrs_change_owner()
4501 error = sysfs_groups_change_owner(kobj, class->dev_groups, kuid, in device_attrs_change_owner()
4512 error = sysfs_groups_change_owner(kobj, type->groups, kuid, in device_attrs_change_owner()
4519 error = sysfs_groups_change_owner(kobj, dev->groups, kuid, kgid); in device_attrs_change_owner()
4523 if (device_supports_offline(dev) && !dev->offline_disabled) { in device_attrs_change_owner()
4535 * device_change_owner - change the owner of an existing device.
4549 struct kobject *kobj = &dev->kobj; in device_change_owner()
4553 return -EINVAL; in device_change_owner()
4587 if (sysfs_deprecated && dev->class == &block_class) in device_change_owner()
4597 error = sysfs_link_change_owner(&dev->class->p->subsys.kobj, &dev->kobj, in device_change_owner()
4609 * device_shutdown - call ->shutdown() on each device to shutdown.
4620 spin_lock(&devices_kset->list_lock); in device_shutdown()
4626 while (!list_empty(&devices_kset->list)) { in device_shutdown()
4627 dev = list_entry(devices_kset->list.prev, struct device, in device_shutdown()
4635 parent = get_device(dev->parent); in device_shutdown()
4639 * event that dev->*->shutdown() doesn't remove it. in device_shutdown()
4641 list_del_init(&dev->kobj.entry); in device_shutdown()
4642 spin_unlock(&devices_kset->list_lock); in device_shutdown()
4653 if (dev->class && dev->class->shutdown_pre) { in device_shutdown()
4656 dev->class->shutdown_pre(dev); in device_shutdown()
4658 if (dev->bus && dev->bus->shutdown) { in device_shutdown()
4661 dev->bus->shutdown(dev); in device_shutdown()
4662 } else if (dev->driver && dev->driver->shutdown) { in device_shutdown()
4665 dev->driver->shutdown(dev); in device_shutdown()
4675 spin_lock(&devices_kset->list_lock); in device_shutdown()
4677 spin_unlock(&devices_kset->list_lock); in device_shutdown()
4692 if (dev->class) in set_dev_info()
4693 subsys = dev->class->name; in set_dev_info()
4694 else if (dev->bus) in set_dev_info()
4695 subsys = dev->bus->name; in set_dev_info()
4699 strscpy(dev_info->subsystem, subsys, sizeof(dev_info->subsystem)); in set_dev_info()
4708 if (MAJOR(dev->devt)) { in set_dev_info()
4716 snprintf(dev_info->device, sizeof(dev_info->device), in set_dev_info()
4717 "%c%u:%u", c, MAJOR(dev->devt), MINOR(dev->devt)); in set_dev_info()
4721 snprintf(dev_info->device, sizeof(dev_info->device), in set_dev_info()
4722 "n%u", net->ifindex); in set_dev_info()
4724 snprintf(dev_info->device, sizeof(dev_info->device), in set_dev_info()
4759 dev_printk_emit(level[1] - '0', dev, "%s %s: %pV", in __dev_printk()
4810 * dev_err_probe - probe error check and log helper
4813 * @fmt: printf-style format string
4818 * -EPROBE_DEFER and propagate error upwards.
4819 * In case of -EPROBE_DEFER it sets also defer probe reason, which can be
4823 * if (err != -EPROBE_DEFER)
4834 * prints during probe even if the @err is known to never be -EPROBE_DEFER.
4850 if (err != -EPROBE_DEFER) { in dev_err_probe()
4865 return fwnode && !IS_ERR(fwnode->secondary); in fwnode_is_primary()
4869 * set_primary_fwnode - Change the primary firmware node of a given device.
4877 * - primary --> secondary --> -ENODEV
4878 * - primary --> NULL
4879 * - secondary --> -ENODEV
4880 * - NULL
4884 struct device *parent = dev->parent; in set_primary_fwnode()
4885 struct fwnode_handle *fn = dev->fwnode; in set_primary_fwnode()
4889 fn = fn->secondary; in set_primary_fwnode()
4892 WARN_ON(fwnode->secondary); in set_primary_fwnode()
4893 fwnode->secondary = fn; in set_primary_fwnode()
4895 dev->fwnode = fwnode; in set_primary_fwnode()
4898 dev->fwnode = fn->secondary; in set_primary_fwnode()
4899 /* Set fn->secondary = NULL, so fn remains the primary fwnode */ in set_primary_fwnode()
4900 if (!(parent && fn == parent->fwnode)) in set_primary_fwnode()
4901 fn->secondary = NULL; in set_primary_fwnode()
4903 dev->fwnode = NULL; in set_primary_fwnode()
4910 * set_secondary_fwnode - Change the secondary firmware node of a given device.
4921 fwnode->secondary = ERR_PTR(-ENODEV); in set_secondary_fwnode()
4923 if (fwnode_is_primary(dev->fwnode)) in set_secondary_fwnode()
4924 dev->fwnode->secondary = fwnode; in set_secondary_fwnode()
4926 dev->fwnode = fwnode; in set_secondary_fwnode()
4931 * device_set_of_node_from_dev - reuse device-tree node of another device
4932 * @dev: device whose device-tree node is being set
4933 * @dev2: device whose device-tree node is being reused
4935 * Takes another reference to the new device-tree node after first dropping
4940 of_node_put(dev->of_node); in device_set_of_node_from_dev()
4941 dev->of_node = of_node_get(dev2->of_node); in device_set_of_node_from_dev()
4942 dev->of_node_reused = true; in device_set_of_node_from_dev()
4948 dev->fwnode = fwnode; in device_set_node()
4949 dev->of_node = to_of_node(fwnode); in device_set_node()
4961 return dev->of_node == np; in device_match_of_node()
4973 return dev->devt == *(dev_t *)pdevt; in device_match_devt()