^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * drivers/base/dd.c - The core device/driver interactions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * This file contains the (sometimes tricky) code that controls the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * interactions between devices and drivers, which primarily includes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * driver binding and unbinding.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * All of this code used to exist in drivers/base/bus.c, but was
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * relocated to here in the name of compartmentalization (since it wasn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * strictly code just for the 'struct bus_type'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * Copyright (c) 2002-5 Patrick Mochel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * Copyright (c) 2002-3 Open Source Development Labs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * Copyright (c) 2007-2009 Greg Kroah-Hartman <gregkh@suse.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * Copyright (c) 2007-2009 Novell Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/debugfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/dma-map-ops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/kthread.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/wait.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/async.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/pm_runtime.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <linux/pinctrl/devinfo.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include "base.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include "power/power.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * Deferred Probe infrastructure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * Sometimes driver probe order matters, but the kernel doesn't always have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * dependency information which means some drivers will get probed before a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * resource it depends on is available. For example, an SDHCI driver may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * first need a GPIO line from an i2c GPIO controller before it can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * initialized. If a required resource is not available yet, a driver can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * request probing to be deferred by returning -EPROBE_DEFER from its probe hook
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * Deferred probe maintains two lists of devices, a pending list and an active
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * list. A driver returning -EPROBE_DEFER causes the device to be added to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * pending list. A successful driver probe will trigger moving all devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * from the pending to the active list so that the workqueue will eventually
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * retry them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * The deferred_probe_mutex must be held any time the deferred_probe_*_list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * of the (struct device*)->p->deferred_probe pointers are manipulated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) static DEFINE_MUTEX(deferred_probe_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) static LIST_HEAD(deferred_probe_pending_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) static LIST_HEAD(deferred_probe_active_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) static atomic_t deferred_trigger_count = ATOMIC_INIT(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) static struct dentry *deferred_devices;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) static bool initcalls_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) /* Save the async probe drivers' name from kernel cmdline */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) #define ASYNC_DRV_NAMES_MAX_LEN 256
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) static char async_probe_drv_names[ASYNC_DRV_NAMES_MAX_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * In some cases, like suspend to RAM or hibernation, It might be reasonable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) * to prohibit probing of devices as it could be unsafe.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) * Once defer_all_probes is true all drivers probes will be forcibly deferred.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) static bool defer_all_probes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) * deferred_probe_work_func() - Retry probing devices in the active list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) static void deferred_probe_work_func(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) struct device_private *private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * This block processes every device in the deferred 'active' list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) * Each device is removed from the active list and passed to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) * bus_probe_device() to re-attempt the probe. The loop continues
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) * until every device in the active list is removed and retried.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) * Note: Once the device is removed from the list and the mutex is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) * released, it is possible for the device get freed by another thread
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) * and cause a illegal pointer dereference. This code uses
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) * get/put_device() to ensure the device structure cannot disappear
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) * from under our feet.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) mutex_lock(&deferred_probe_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) while (!list_empty(&deferred_probe_active_list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) private = list_first_entry(&deferred_probe_active_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) typeof(*dev->p), deferred_probe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) dev = private->device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) list_del_init(&private->deferred_probe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) get_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) kfree(dev->p->deferred_probe_reason);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) dev->p->deferred_probe_reason = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) * Drop the mutex while probing each device; the probe path may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) * manipulate the deferred list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) mutex_unlock(&deferred_probe_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) * Force the device to the end of the dpm_list since
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) * the PM code assumes that the order we add things to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) * the list is a good order for suspend but deferred
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) * probe makes that very unsafe.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) device_pm_move_to_tail(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) dev_dbg(dev, "Retrying from deferred list\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) bus_probe_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) mutex_lock(&deferred_probe_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) put_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) mutex_unlock(&deferred_probe_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) static DECLARE_WORK(deferred_probe_work, deferred_probe_work_func);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) void driver_deferred_probe_add(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) mutex_lock(&deferred_probe_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) if (list_empty(&dev->p->deferred_probe)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) dev_dbg(dev, "Added to deferred list\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) list_add_tail(&dev->p->deferred_probe, &deferred_probe_pending_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) mutex_unlock(&deferred_probe_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) void driver_deferred_probe_del(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) mutex_lock(&deferred_probe_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) if (!list_empty(&dev->p->deferred_probe)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) dev_dbg(dev, "Removed from deferred list\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) list_del_init(&dev->p->deferred_probe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) kfree(dev->p->deferred_probe_reason);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) dev->p->deferred_probe_reason = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) mutex_unlock(&deferred_probe_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) static bool driver_deferred_probe_enable = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) * driver_deferred_probe_trigger() - Kick off re-probing deferred devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) * This functions moves all devices from the pending list to the active
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) * list and schedules the deferred probe workqueue to process them. It
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) * should be called anytime a driver is successfully bound to a device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) * Note, there is a race condition in multi-threaded probe. In the case where
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) * more than one device is probing at the same time, it is possible for one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) * probe to complete successfully while another is about to defer. If the second
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) * depends on the first, then it will get put on the pending list after the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) * trigger event has already occurred and will be stuck there.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) * The atomic 'deferred_trigger_count' is used to determine if a successful
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) * trigger has occurred in the midst of probing a driver. If the trigger count
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) * changes in the midst of a probe, then deferred processing should be triggered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) * again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) static void driver_deferred_probe_trigger(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) if (!driver_deferred_probe_enable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) * A successful probe means that all the devices in the pending list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) * should be triggered to be reprobed. Move all the deferred devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) * into the active list so they can be retried by the workqueue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) mutex_lock(&deferred_probe_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) atomic_inc(&deferred_trigger_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) list_splice_tail_init(&deferred_probe_pending_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) &deferred_probe_active_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) mutex_unlock(&deferred_probe_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) * Kick the re-probe thread. It may already be scheduled, but it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) * safe to kick it again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) queue_work(system_unbound_wq, &deferred_probe_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) * device_block_probing() - Block/defer device's probes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) * It will disable probing of devices and defer their probes instead.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) void device_block_probing(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) defer_all_probes = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) /* sync with probes to avoid races. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) wait_for_device_probe();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) * device_unblock_probing() - Unblock/enable device's probes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) * It will restore normal behavior and trigger re-probing of deferred
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) * devices.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) void device_unblock_probing(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) defer_all_probes = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) driver_deferred_probe_trigger();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) * device_set_deferred_probe_reason() - Set defer probe reason message for device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) * @dev: the pointer to the struct device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) * @vaf: the pointer to va_format structure with message
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) void device_set_deferred_probe_reason(const struct device *dev, struct va_format *vaf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) const char *drv = dev_driver_string(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) mutex_lock(&deferred_probe_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) kfree(dev->p->deferred_probe_reason);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) dev->p->deferred_probe_reason = kasprintf(GFP_KERNEL, "%s: %pV", drv, vaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) mutex_unlock(&deferred_probe_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) * deferred_devs_show() - Show the devices in the deferred probe pending list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) static int deferred_devs_show(struct seq_file *s, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) struct device_private *curr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) mutex_lock(&deferred_probe_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) list_for_each_entry(curr, &deferred_probe_pending_list, deferred_probe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) seq_printf(s, "%s\t%s", dev_name(curr->device),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) curr->device->p->deferred_probe_reason ?: "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) mutex_unlock(&deferred_probe_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) DEFINE_SHOW_ATTRIBUTE(deferred_devs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) int driver_deferred_probe_timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) EXPORT_SYMBOL_GPL(driver_deferred_probe_timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) static DECLARE_WAIT_QUEUE_HEAD(probe_timeout_waitqueue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) static int __init deferred_probe_timeout_setup(char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) int timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) if (!kstrtoint(str, 10, &timeout))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) driver_deferred_probe_timeout = timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) __setup("deferred_probe_timeout=", deferred_probe_timeout_setup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) * driver_deferred_probe_check_state() - Check deferred probe state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) * @dev: device to check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) * Return:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) * -ENODEV if initcalls have completed and modules are disabled.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) * -ETIMEDOUT if the deferred probe timeout was set and has expired
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) * and modules are enabled.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) * -EPROBE_DEFER in other cases.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) * Drivers or subsystems can opt-in to calling this function instead of directly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) * returning -EPROBE_DEFER.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) int driver_deferred_probe_check_state(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) if (!IS_ENABLED(CONFIG_MODULES) && initcalls_done) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) dev_warn(dev, "ignoring dependency for device, assuming no driver\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) if (!driver_deferred_probe_timeout && initcalls_done) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) dev_warn(dev, "deferred probe timeout, ignoring dependency\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) return -ETIMEDOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) return -EPROBE_DEFER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) static void deferred_probe_timeout_work_func(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) struct device_private *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) driver_deferred_probe_timeout = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) driver_deferred_probe_trigger();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) flush_work(&deferred_probe_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) mutex_lock(&deferred_probe_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) list_for_each_entry(p, &deferred_probe_pending_list, deferred_probe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) dev_info(p->device, "deferred probe pending\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) mutex_unlock(&deferred_probe_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) wake_up_all(&probe_timeout_waitqueue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) static DECLARE_DELAYED_WORK(deferred_probe_timeout_work, deferred_probe_timeout_work_func);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) * deferred_probe_initcall() - Enable probing of deferred devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) * We don't want to get in the way when the bulk of drivers are getting probed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) * Instead, this initcall makes sure that deferred probing is delayed until
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) * late_initcall time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) static int deferred_probe_initcall(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) deferred_devices = debugfs_create_file("devices_deferred", 0444, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) NULL, &deferred_devs_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) driver_deferred_probe_enable = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) driver_deferred_probe_trigger();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) /* Sort as many dependencies as possible before exiting initcalls */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) flush_work(&deferred_probe_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) initcalls_done = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) * Trigger deferred probe again, this time we won't defer anything
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) * that is optional
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) driver_deferred_probe_trigger();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) flush_work(&deferred_probe_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) if (driver_deferred_probe_timeout > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) schedule_delayed_work(&deferred_probe_timeout_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) driver_deferred_probe_timeout * HZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) late_initcall(deferred_probe_initcall);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) static void __exit deferred_probe_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) debugfs_remove_recursive(deferred_devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) __exitcall(deferred_probe_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) * device_is_bound() - Check if device is bound to a driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) * @dev: device to check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) * Returns true if passed device has already finished probing successfully
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) * against a driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) * This function must be called with the device lock held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) bool device_is_bound(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) return dev->p && klist_node_attached(&dev->p->knode_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) static void driver_bound(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) if (device_is_bound(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) pr_warn("%s: device %s already bound\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) __func__, kobject_name(&dev->kobj));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) pr_debug("driver: '%s': %s: bound to device '%s'\n", dev->driver->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) __func__, dev_name(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) klist_add_tail(&dev->p->knode_driver, &dev->driver->p->klist_devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) device_links_driver_bound(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) device_pm_check_callbacks(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) * Make sure the device is no longer in one of the deferred lists and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) * kick off retrying all pending devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) driver_deferred_probe_del(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) driver_deferred_probe_trigger();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) if (dev->bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) BUS_NOTIFY_BOUND_DRIVER, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) kobject_uevent(&dev->kobj, KOBJ_BIND);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) static ssize_t coredump_store(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) device_lock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) dev->driver->coredump(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) device_unlock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) static DEVICE_ATTR_WO(coredump);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) static int driver_sysfs_add(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) if (dev->bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) BUS_NOTIFY_BIND_DRIVER, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) ret = sysfs_create_link(&dev->driver->p->kobj, &dev->kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) kobject_name(&dev->kobj));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) ret = sysfs_create_link(&dev->kobj, &dev->driver->p->kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) "driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) goto rm_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) if (!IS_ENABLED(CONFIG_DEV_COREDUMP) || !dev->driver->coredump ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) !device_create_file(dev, &dev_attr_coredump))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) sysfs_remove_link(&dev->kobj, "driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) rm_dev:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) sysfs_remove_link(&dev->driver->p->kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) kobject_name(&dev->kobj));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) static void driver_sysfs_remove(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) struct device_driver *drv = dev->driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) if (drv) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) if (drv->coredump)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) device_remove_file(dev, &dev_attr_coredump);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) sysfs_remove_link(&drv->p->kobj, kobject_name(&dev->kobj));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) sysfs_remove_link(&dev->kobj, "driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) * device_bind_driver - bind a driver to one device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) * @dev: device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) * Allow manual attachment of a driver to a device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) * Caller must have already set @dev->driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) * Note that this does not modify the bus reference count.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) * Please verify that is accounted for before calling this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) * (It is ok to call with no other effort from a driver's probe() method.)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) * This function must be called with the device lock held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) int device_bind_driver(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) ret = driver_sysfs_add(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) driver_bound(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) else if (dev->bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) BUS_NOTIFY_DRIVER_NOT_BOUND, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) EXPORT_SYMBOL_GPL(device_bind_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) static atomic_t probe_count = ATOMIC_INIT(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) static DECLARE_WAIT_QUEUE_HEAD(probe_waitqueue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) static void driver_deferred_probe_add_trigger(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) int local_trigger_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) driver_deferred_probe_add(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) /* Did a trigger occur while probing? Need to re-trigger if yes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) if (local_trigger_count != atomic_read(&deferred_trigger_count))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) driver_deferred_probe_trigger();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) static ssize_t state_synced_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) bool val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) device_lock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) val = dev->state_synced;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) device_unlock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) return sysfs_emit(buf, "%u\n", val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) static DEVICE_ATTR_RO(state_synced);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) static int really_probe(struct device *dev, struct device_driver *drv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) int ret = -EPROBE_DEFER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) int local_trigger_count = atomic_read(&deferred_trigger_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) bool test_remove = IS_ENABLED(CONFIG_DEBUG_TEST_DRIVER_REMOVE) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) !drv->suppress_bind_attrs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) if (defer_all_probes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) * Value of defer_all_probes can be set only by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) * device_block_probing() which, in turn, will call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) * wait_for_device_probe() right after that to avoid any races.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) dev_dbg(dev, "Driver %s force probe deferral\n", drv->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) driver_deferred_probe_add(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) ret = device_links_check_suppliers(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) if (ret == -EPROBE_DEFER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) driver_deferred_probe_add_trigger(dev, local_trigger_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) atomic_inc(&probe_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) pr_debug("bus: '%s': %s: probing driver %s with device %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) drv->bus->name, __func__, drv->name, dev_name(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) if (!list_empty(&dev->devres_head)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) dev_crit(dev, "Resources present before probing\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) ret = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) re_probe:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) dev->driver = drv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) /* If using pinctrl, bind pins now before probing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) ret = pinctrl_bind_pins(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) goto pinctrl_bind_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) if (dev->bus->dma_configure) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) ret = dev->bus->dma_configure(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) goto probe_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) ret = driver_sysfs_add(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) pr_err("%s: driver_sysfs_add(%s) failed\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) __func__, dev_name(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) goto probe_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) if (dev->pm_domain && dev->pm_domain->activate) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) ret = dev->pm_domain->activate(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) goto probe_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) if (dev->bus->probe) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) ret = dev->bus->probe(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) goto probe_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) } else if (drv->probe) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) ret = drv->probe(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) goto probe_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) ret = device_add_groups(dev, drv->dev_groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) dev_err(dev, "device_add_groups() failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) goto dev_groups_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) if (dev_has_sync_state(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) ret = device_create_file(dev, &dev_attr_state_synced);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) dev_err(dev, "state_synced sysfs add failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) goto dev_sysfs_state_synced_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) if (test_remove) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) test_remove = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) device_remove_file(dev, &dev_attr_state_synced);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) device_remove_groups(dev, drv->dev_groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) if (dev->bus->remove)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) dev->bus->remove(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) else if (drv->remove)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) drv->remove(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) devres_release_all(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) arch_teardown_dma_ops(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) kfree(dev->dma_range_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) dev->dma_range_map = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) driver_sysfs_remove(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) dev->driver = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) dev_set_drvdata(dev, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) if (dev->pm_domain && dev->pm_domain->dismiss)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) dev->pm_domain->dismiss(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) pm_runtime_reinit(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) goto re_probe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) pinctrl_init_done(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) if (dev->pm_domain && dev->pm_domain->sync)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) dev->pm_domain->sync(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) driver_bound(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) pr_debug("bus: '%s': %s: bound device %s to driver %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) drv->bus->name, __func__, dev_name(dev), drv->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) dev_sysfs_state_synced_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) device_remove_groups(dev, drv->dev_groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) dev_groups_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) if (dev->bus->remove)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) dev->bus->remove(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) else if (drv->remove)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) drv->remove(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) probe_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) if (dev->bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) BUS_NOTIFY_DRIVER_NOT_BOUND, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) pinctrl_bind_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) device_links_no_driver(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) devres_release_all(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) arch_teardown_dma_ops(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) kfree(dev->dma_range_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) dev->dma_range_map = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) driver_sysfs_remove(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) dev->driver = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) dev_set_drvdata(dev, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) if (dev->pm_domain && dev->pm_domain->dismiss)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) dev->pm_domain->dismiss(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) pm_runtime_reinit(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) dev_pm_set_driver_flags(dev, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) switch (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) case -EPROBE_DEFER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) /* Driver requested deferred probing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) dev_dbg(dev, "Driver %s requests probe deferral\n", drv->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) driver_deferred_probe_add_trigger(dev, local_trigger_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) case -ENODEV:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) case -ENXIO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) pr_debug("%s: probe of %s rejects match %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) drv->name, dev_name(dev), ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) /* driver matched but the probe failed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) pr_warn("%s: probe of %s failed with error %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) drv->name, dev_name(dev), ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) * Ignore errors returned by ->probe so that the next driver can try
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) * its luck.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) atomic_dec(&probe_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) wake_up_all(&probe_waitqueue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) * For initcall_debug, show the driver probe time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) static int really_probe_debug(struct device *dev, struct device_driver *drv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) ktime_t calltime, rettime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) calltime = ktime_get();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) ret = really_probe(dev, drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) rettime = ktime_get();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) pr_debug("probe of %s returned %d after %lld usecs\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) dev_name(dev), ret, ktime_us_delta(rettime, calltime));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) * driver_probe_done
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) * Determine if the probe sequence is finished or not.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) * Should somehow figure out how to use a semaphore, not an atomic variable...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) int driver_probe_done(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) int local_probe_count = atomic_read(&probe_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) pr_debug("%s: probe_count = %d\n", __func__, local_probe_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) if (local_probe_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) * wait_for_device_probe
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) * Wait for device probing to be completed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) void wait_for_device_probe(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) /* wait for probe timeout */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) wait_event(probe_timeout_waitqueue, !driver_deferred_probe_timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) /* wait for the deferred probe workqueue to finish */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) flush_work(&deferred_probe_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) /* wait for the known devices to complete their probing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) wait_event(probe_waitqueue, atomic_read(&probe_count) == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) async_synchronize_full();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) EXPORT_SYMBOL_GPL(wait_for_device_probe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) * driver_probe_device - attempt to bind device & driver together
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) * @drv: driver to bind a device to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) * @dev: device to try to bind to the driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) * This function returns -ENODEV if the device is not registered,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) * 1 if the device is bound successfully and 0 otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) * This function must be called with @dev lock held. When called for a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) * USB interface, @dev->parent lock must be held as well.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) * If the device has a parent, runtime-resume the parent before driver probing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) int driver_probe_device(struct device_driver *drv, struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) if (!device_is_registered(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) pr_debug("bus: '%s': %s: matched device %s with driver %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) drv->bus->name, __func__, dev_name(dev), drv->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) pm_runtime_get_suppliers(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) if (dev->parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) pm_runtime_get_sync(dev->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) pm_runtime_barrier(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) if (initcall_debug)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) ret = really_probe_debug(dev, drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) ret = really_probe(dev, drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) pm_request_idle(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) if (dev->parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) pm_runtime_put(dev->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) pm_runtime_put_suppliers(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) static inline bool cmdline_requested_async_probing(const char *drv_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) return parse_option_str(async_probe_drv_names, drv_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) /* The option format is "driver_async_probe=drv_name1,drv_name2,..." */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) static int __init save_async_options(char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) if (strlen(buf) >= ASYNC_DRV_NAMES_MAX_LEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) pr_warn("Too long list of driver names for 'driver_async_probe'!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) strlcpy(async_probe_drv_names, buf, ASYNC_DRV_NAMES_MAX_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) __setup("driver_async_probe=", save_async_options);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) bool driver_allows_async_probing(struct device_driver *drv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) switch (drv->probe_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) case PROBE_PREFER_ASYNCHRONOUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) case PROBE_FORCE_SYNCHRONOUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) if (cmdline_requested_async_probing(drv->name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) if (module_requested_async_probing(drv->owner))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) struct device_attach_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) * Indicates whether we are are considering asynchronous probing or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) * not. Only initial binding after device or driver registration
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) * (including deferral processing) may be done asynchronously, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) * rest is always synchronous, as we expect it is being done by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) * request from userspace.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) bool check_async;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) * Indicates if we are binding synchronous or asynchronous drivers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) * When asynchronous probing is enabled we'll execute 2 passes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) * over drivers: first pass doing synchronous probing and second
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) * doing asynchronous probing (if synchronous did not succeed -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) * most likely because there was no driver requiring synchronous
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) * probing - and we found asynchronous driver during first pass).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) * The 2 passes are done because we can't shoot asynchronous
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) * probe for given device and driver from bus_for_each_drv() since
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) * driver pointer is not guaranteed to stay valid once
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) * bus_for_each_drv() iterates to the next driver on the bus.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) bool want_async;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) * We'll set have_async to 'true' if, while scanning for matching
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) * driver, we'll encounter one that requests asynchronous probing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) bool have_async;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) static int __device_attach_driver(struct device_driver *drv, void *_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) struct device_attach_data *data = _data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) struct device *dev = data->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) bool async_allowed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) ret = driver_match_device(drv, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) if (ret == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) /* no match */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) } else if (ret == -EPROBE_DEFER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) dev_dbg(dev, "Device match requests probe deferral\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) driver_deferred_probe_add(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) } else if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) dev_dbg(dev, "Bus failed to match device: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) } /* ret > 0 means positive match */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) async_allowed = driver_allows_async_probing(drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) if (async_allowed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) data->have_async = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) if (data->check_async && async_allowed != data->want_async)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) return driver_probe_device(drv, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) static void __device_attach_async_helper(void *_dev, async_cookie_t cookie)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) struct device *dev = _dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) struct device_attach_data data = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) .dev = dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) .check_async = true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) .want_async = true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) device_lock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) * Check if device has already been removed or claimed. This may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) * happen with driver loading, device discovery/registration,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) * and deferred probe processing happens all at once with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) * multiple threads.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) if (dev->p->dead || dev->driver)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) if (dev->parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) pm_runtime_get_sync(dev->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) bus_for_each_drv(dev->bus, NULL, &data, __device_attach_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) dev_dbg(dev, "async probe completed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) pm_request_idle(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) if (dev->parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) pm_runtime_put(dev->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) device_unlock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) put_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) static int __device_attach(struct device *dev, bool allow_async)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) device_lock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) if (dev->p->dead) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) } else if (dev->driver) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) if (device_is_bound(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) ret = device_bind_driver(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) dev->driver = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) struct device_attach_data data = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) .dev = dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) .check_async = allow_async,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) .want_async = false,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) if (dev->parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) pm_runtime_get_sync(dev->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) ret = bus_for_each_drv(dev->bus, NULL, &data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) __device_attach_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) if (!ret && allow_async && data.have_async) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) * If we could not find appropriate driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) * synchronously and we are allowed to do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) * async probes and there are drivers that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) * want to probe asynchronously, we'll
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) * try them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) dev_dbg(dev, "scheduling asynchronous probe\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) get_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) async_schedule_dev(__device_attach_async_helper, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) pm_request_idle(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) if (dev->parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) pm_runtime_put(dev->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) device_unlock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) * device_attach - try to attach device to a driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) * @dev: device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) * Walk the list of drivers that the bus has and call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) * driver_probe_device() for each pair. If a compatible
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) * pair is found, break out and return.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) * Returns 1 if the device was bound to a driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) * 0 if no matching driver was found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) * -ENODEV if the device is not registered.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) * When called for a USB interface, @dev->parent lock must be held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) int device_attach(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) return __device_attach(dev, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) EXPORT_SYMBOL_GPL(device_attach);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) void device_initial_probe(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) __device_attach(dev, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) * __device_driver_lock - acquire locks needed to manipulate dev->drv
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) * @dev: Device we will update driver info for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) * @parent: Parent device. Needed if the bus requires parent lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) * This function will take the required locks for manipulating dev->drv.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) * Normally this will just be the @dev lock, but when called for a USB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) * interface, @parent lock will be held as well.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) static void __device_driver_lock(struct device *dev, struct device *parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) if (parent && dev->bus->need_parent_lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) device_lock(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) device_lock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) * __device_driver_unlock - release locks needed to manipulate dev->drv
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) * @dev: Device we will update driver info for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) * @parent: Parent device. Needed if the bus requires parent lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) * This function will release the required locks for manipulating dev->drv.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) * Normally this will just be the the @dev lock, but when called for a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) * USB interface, @parent lock will be released as well.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) static void __device_driver_unlock(struct device *dev, struct device *parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) device_unlock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) if (parent && dev->bus->need_parent_lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) device_unlock(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) * device_driver_attach - attach a specific driver to a specific device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) * @drv: Driver to attach
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) * @dev: Device to attach it to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) * Manually attach driver to a device. Will acquire both @dev lock and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) * @dev->parent lock if needed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) int device_driver_attach(struct device_driver *drv, struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) __device_driver_lock(dev, dev->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) * If device has been removed or someone has already successfully
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) * bound a driver before us just skip the driver probe call.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) if (!dev->p->dead && !dev->driver)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) ret = driver_probe_device(drv, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) __device_driver_unlock(dev, dev->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) static void __driver_attach_async_helper(void *_dev, async_cookie_t cookie)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) struct device *dev = _dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) struct device_driver *drv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) __device_driver_lock(dev, dev->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) drv = dev->p->async_driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) * If device has been removed or someone has already successfully
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) * bound a driver before us just skip the driver probe call.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) if (!dev->p->dead && !dev->driver)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) ret = driver_probe_device(drv, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) __device_driver_unlock(dev, dev->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) dev_dbg(dev, "driver %s async attach completed: %d\n", drv->name, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) put_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) static int __driver_attach(struct device *dev, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) struct device_driver *drv = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) * Lock device and try to bind to it. We drop the error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) * here and always return 0, because we need to keep trying
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) * to bind to devices and some drivers will return an error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) * simply if it didn't support the device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) * driver_probe_device() will spit a warning if there
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) * is an error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) ret = driver_match_device(drv, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) if (ret == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) /* no match */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) } else if (ret == -EPROBE_DEFER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) dev_dbg(dev, "Device match requests probe deferral\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) driver_deferred_probe_add(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) } else if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) dev_dbg(dev, "Bus failed to match device: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) } /* ret > 0 means positive match */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) if (driver_allows_async_probing(drv)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) * Instead of probing the device synchronously we will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) * probe it asynchronously to allow for more parallelism.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) * We only take the device lock here in order to guarantee
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) * that the dev->driver and async_driver fields are protected
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) dev_dbg(dev, "probing driver %s asynchronously\n", drv->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) device_lock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) if (!dev->driver) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) get_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) dev->p->async_driver = drv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) async_schedule_dev(__driver_attach_async_helper, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) device_unlock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) device_driver_attach(drv, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) * driver_attach - try to bind driver to devices.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) * @drv: driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) * Walk the list of devices that the bus has on it and try to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) * match the driver with each one. If driver_probe_device()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) * returns 0 and the @dev->driver is set, we've found a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) * compatible pair.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) int driver_attach(struct device_driver *drv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) return bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) EXPORT_SYMBOL_GPL(driver_attach);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) * __device_release_driver() must be called with @dev lock held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) * When called for a USB interface, @dev->parent lock must be held as well.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) static void __device_release_driver(struct device *dev, struct device *parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) struct device_driver *drv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) drv = dev->driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) if (drv) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) pm_runtime_get_sync(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) while (device_links_busy(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) __device_driver_unlock(dev, parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) device_links_unbind_consumers(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) __device_driver_lock(dev, parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) * A concurrent invocation of the same function might
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) * have released the driver successfully while this one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) * was waiting, so check for that.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) if (dev->driver != drv) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) pm_runtime_put(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) driver_sysfs_remove(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) if (dev->bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) BUS_NOTIFY_UNBIND_DRIVER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) pm_runtime_put_sync(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) device_remove_file(dev, &dev_attr_state_synced);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) device_remove_groups(dev, drv->dev_groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) if (dev->bus && dev->bus->remove)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) dev->bus->remove(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) else if (drv->remove)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) drv->remove(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) device_links_driver_cleanup(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) devres_release_all(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) arch_teardown_dma_ops(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) kfree(dev->dma_range_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) dev->dma_range_map = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) dev->driver = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) dev_set_drvdata(dev, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) if (dev->pm_domain && dev->pm_domain->dismiss)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) dev->pm_domain->dismiss(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) pm_runtime_reinit(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) dev_pm_set_driver_flags(dev, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) klist_remove(&dev->p->knode_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) device_pm_check_callbacks(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) if (dev->bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) BUS_NOTIFY_UNBOUND_DRIVER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) kobject_uevent(&dev->kobj, KOBJ_UNBIND);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) void device_release_driver_internal(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) struct device_driver *drv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) struct device *parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) __device_driver_lock(dev, parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) if (!drv || drv == dev->driver)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) __device_release_driver(dev, parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) __device_driver_unlock(dev, parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) * device_release_driver - manually detach device from driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) * @dev: device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) * Manually detach device from driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) * When called for a USB interface, @dev->parent lock must be held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) * If this function is to be called with @dev->parent lock held, ensure that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) * the device's consumers are unbound in advance or that their locks can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) * acquired under the @dev->parent lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) void device_release_driver(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) * If anyone calls device_release_driver() recursively from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) * within their ->remove callback for the same device, they
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) * will deadlock right here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) device_release_driver_internal(dev, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) EXPORT_SYMBOL_GPL(device_release_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) * device_driver_detach - detach driver from a specific device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) * @dev: device to detach driver from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) * Detach driver from device. Will acquire both @dev lock and @dev->parent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) * lock if needed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) void device_driver_detach(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) device_release_driver_internal(dev, NULL, dev->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) * driver_detach - detach driver from all devices it controls.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) * @drv: driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) void driver_detach(struct device_driver *drv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) struct device_private *dev_prv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) if (driver_allows_async_probing(drv))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) async_synchronize_full();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) spin_lock(&drv->p->klist_devices.k_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) if (list_empty(&drv->p->klist_devices.k_list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) spin_unlock(&drv->p->klist_devices.k_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) dev_prv = list_last_entry(&drv->p->klist_devices.k_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) struct device_private,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) knode_driver.n_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) dev = dev_prv->device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) get_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) spin_unlock(&drv->p->klist_devices.k_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) device_release_driver_internal(dev, drv, dev->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) put_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) }