Greg Kroah-Hartman | 989d42e | 2017-11-07 17:30:07 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * drivers/base/core.c - core driver model code (device registration, etc) |
| 4 | * |
| 5 | * Copyright (c) 2002-3 Patrick Mochel |
| 6 | * Copyright (c) 2002-3 Open Source Development Labs |
Greg Kroah-Hartman | 64bb5d2 | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 7 | * Copyright (c) 2006 Greg Kroah-Hartman <gregkh@suse.de> |
| 8 | * Copyright (c) 2006 Novell, Inc. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
Heikki Krogerus | 7847a14 | 2018-11-09 17:21:35 +0300 | [diff] [blame] | 11 | #include <linux/acpi.h> |
Rafael J. Wysocki | 65650b3 | 2019-10-09 01:29:10 +0200 | [diff] [blame] | 12 | #include <linux/cpufreq.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/device.h> |
| 14 | #include <linux/err.h> |
Rafael J. Wysocki | 97badf8 | 2015-04-03 23:23:37 +0200 | [diff] [blame] | 15 | #include <linux/fwnode.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/init.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/string.h> |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 20 | #include <linux/kdev_t.h> |
Benjamin Herrenschmidt | 116af37 | 2006-10-25 13:44:59 +1000 | [diff] [blame] | 21 | #include <linux/notifier.h> |
Grant Likely | 07d57a3 | 2012-02-01 11:22:22 -0700 | [diff] [blame] | 22 | #include <linux/of.h> |
| 23 | #include <linux/of_device.h> |
Kay Sievers | da231fd | 2007-11-21 17:29:15 +0100 | [diff] [blame] | 24 | #include <linux/genhd.h> |
Dave Young | f75b1c6 | 2008-05-28 09:28:39 -0700 | [diff] [blame] | 25 | #include <linux/mutex.h> |
Peter Chen | af8db15 | 2011-11-15 21:52:29 +0100 | [diff] [blame] | 26 | #include <linux/pm_runtime.h> |
Kay Sievers | c4e00da | 2012-05-03 02:29:59 +0200 | [diff] [blame] | 27 | #include <linux/netdevice.h> |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 28 | #include <linux/sched/signal.h> |
Greg Kroah-Hartman | 6396768 | 2013-08-27 10:24:15 -0700 | [diff] [blame] | 29 | #include <linux/sysfs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
| 31 | #include "base.h" |
| 32 | #include "power/power.h" |
| 33 | |
Andi Kleen | e52eec1 | 2010-09-08 16:54:17 +0200 | [diff] [blame] | 34 | #ifdef CONFIG_SYSFS_DEPRECATED |
| 35 | #ifdef CONFIG_SYSFS_DEPRECATED_V2 |
| 36 | long sysfs_deprecated = 1; |
| 37 | #else |
| 38 | long sysfs_deprecated = 0; |
| 39 | #endif |
Hanjun Guo | 3454bf9 | 2013-08-17 20:42:24 +0800 | [diff] [blame] | 40 | static int __init sysfs_deprecated_setup(char *arg) |
Andi Kleen | e52eec1 | 2010-09-08 16:54:17 +0200 | [diff] [blame] | 41 | { |
Jingoo Han | 34da5e6 | 2013-07-26 13:10:22 +0900 | [diff] [blame] | 42 | return kstrtol(arg, 10, &sysfs_deprecated); |
Andi Kleen | e52eec1 | 2010-09-08 16:54:17 +0200 | [diff] [blame] | 43 | } |
| 44 | early_param("sysfs.deprecated", sysfs_deprecated_setup); |
| 45 | #endif |
| 46 | |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 47 | /* Device links support. */ |
Saravana Kannan | e2ae9bc | 2019-09-04 14:11:21 -0700 | [diff] [blame] | 48 | static LIST_HEAD(wait_for_suppliers); |
| 49 | static DEFINE_MUTEX(wfs_lock); |
Saravana Kannan | fc5a251 | 2019-09-04 14:11:23 -0700 | [diff] [blame] | 50 | static LIST_HEAD(deferred_sync); |
| 51 | static unsigned int defer_sync_state_count = 1; |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 52 | |
| 53 | #ifdef CONFIG_SRCU |
| 54 | static DEFINE_MUTEX(device_links_lock); |
| 55 | DEFINE_STATIC_SRCU(device_links_srcu); |
| 56 | |
| 57 | static inline void device_links_write_lock(void) |
| 58 | { |
| 59 | mutex_lock(&device_links_lock); |
| 60 | } |
| 61 | |
| 62 | static inline void device_links_write_unlock(void) |
| 63 | { |
| 64 | mutex_unlock(&device_links_lock); |
| 65 | } |
| 66 | |
| 67 | int device_links_read_lock(void) |
| 68 | { |
| 69 | return srcu_read_lock(&device_links_srcu); |
| 70 | } |
| 71 | |
| 72 | void device_links_read_unlock(int idx) |
| 73 | { |
| 74 | srcu_read_unlock(&device_links_srcu, idx); |
| 75 | } |
Joel Fernandes (Google) | c2fa1e1 | 2019-07-16 18:12:25 -0400 | [diff] [blame] | 76 | |
| 77 | int device_links_read_lock_held(void) |
| 78 | { |
| 79 | return srcu_read_lock_held(&device_links_srcu); |
| 80 | } |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 81 | #else /* !CONFIG_SRCU */ |
| 82 | static DECLARE_RWSEM(device_links_lock); |
| 83 | |
| 84 | static inline void device_links_write_lock(void) |
| 85 | { |
| 86 | down_write(&device_links_lock); |
| 87 | } |
| 88 | |
| 89 | static inline void device_links_write_unlock(void) |
| 90 | { |
| 91 | up_write(&device_links_lock); |
| 92 | } |
| 93 | |
| 94 | int device_links_read_lock(void) |
| 95 | { |
| 96 | down_read(&device_links_lock); |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | void device_links_read_unlock(int not_used) |
| 101 | { |
| 102 | up_read(&device_links_lock); |
| 103 | } |
Joel Fernandes (Google) | c2fa1e1 | 2019-07-16 18:12:25 -0400 | [diff] [blame] | 104 | |
| 105 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 106 | int device_links_read_lock_held(void) |
| 107 | { |
| 108 | return lockdep_is_held(&device_links_lock); |
| 109 | } |
| 110 | #endif |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 111 | #endif /* !CONFIG_SRCU */ |
| 112 | |
| 113 | /** |
| 114 | * device_is_dependent - Check if one device depends on another one |
| 115 | * @dev: Device to check dependencies for. |
| 116 | * @target: Device to check against. |
| 117 | * |
| 118 | * Check if @target depends on @dev or any device dependent on it (its child or |
| 119 | * its consumer etc). Return 1 if that is the case or 0 otherwise. |
| 120 | */ |
| 121 | static int device_is_dependent(struct device *dev, void *target) |
| 122 | { |
| 123 | struct device_link *link; |
| 124 | int ret; |
| 125 | |
Benjamin Gaignard | e16f4f3 | 2018-07-16 13:37:44 +0200 | [diff] [blame] | 126 | if (dev == target) |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 127 | return 1; |
| 128 | |
| 129 | ret = device_for_each_child(dev, target, device_is_dependent); |
| 130 | if (ret) |
| 131 | return ret; |
| 132 | |
| 133 | list_for_each_entry(link, &dev->links.consumers, s_node) { |
Saravana Kannan | 05ef983 | 2019-10-28 15:00:22 -0700 | [diff] [blame] | 134 | if (link->flags == (DL_FLAG_SYNC_STATE_ONLY | DL_FLAG_MANAGED)) |
| 135 | continue; |
| 136 | |
Benjamin Gaignard | e16f4f3 | 2018-07-16 13:37:44 +0200 | [diff] [blame] | 137 | if (link->consumer == target) |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 138 | return 1; |
| 139 | |
| 140 | ret = device_is_dependent(link->consumer, target); |
| 141 | if (ret) |
| 142 | break; |
| 143 | } |
| 144 | return ret; |
| 145 | } |
| 146 | |
Rafael J. Wysocki | 515db26 | 2019-07-16 17:21:06 +0200 | [diff] [blame] | 147 | static void device_link_init_status(struct device_link *link, |
| 148 | struct device *consumer, |
| 149 | struct device *supplier) |
| 150 | { |
| 151 | switch (supplier->links.status) { |
| 152 | case DL_DEV_PROBING: |
| 153 | switch (consumer->links.status) { |
| 154 | case DL_DEV_PROBING: |
| 155 | /* |
| 156 | * A consumer driver can create a link to a supplier |
| 157 | * that has not completed its probing yet as long as it |
| 158 | * knows that the supplier is already functional (for |
| 159 | * example, it has just acquired some resources from the |
| 160 | * supplier). |
| 161 | */ |
| 162 | link->status = DL_STATE_CONSUMER_PROBE; |
| 163 | break; |
| 164 | default: |
| 165 | link->status = DL_STATE_DORMANT; |
| 166 | break; |
| 167 | } |
| 168 | break; |
| 169 | case DL_DEV_DRIVER_BOUND: |
| 170 | switch (consumer->links.status) { |
| 171 | case DL_DEV_PROBING: |
| 172 | link->status = DL_STATE_CONSUMER_PROBE; |
| 173 | break; |
| 174 | case DL_DEV_DRIVER_BOUND: |
| 175 | link->status = DL_STATE_ACTIVE; |
| 176 | break; |
| 177 | default: |
| 178 | link->status = DL_STATE_AVAILABLE; |
| 179 | break; |
| 180 | } |
| 181 | break; |
| 182 | case DL_DEV_UNBINDING: |
| 183 | link->status = DL_STATE_SUPPLIER_UNBIND; |
| 184 | break; |
| 185 | default: |
| 186 | link->status = DL_STATE_DORMANT; |
| 187 | break; |
| 188 | } |
| 189 | } |
| 190 | |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 191 | static int device_reorder_to_tail(struct device *dev, void *not_used) |
| 192 | { |
| 193 | struct device_link *link; |
| 194 | |
| 195 | /* |
| 196 | * Devices that have not been registered yet will be put to the ends |
| 197 | * of the lists during the registration, so skip them here. |
| 198 | */ |
| 199 | if (device_is_registered(dev)) |
| 200 | devices_kset_move_last(dev); |
| 201 | |
| 202 | if (device_pm_initialized(dev)) |
| 203 | device_pm_move_last(dev); |
| 204 | |
| 205 | device_for_each_child(dev, NULL, device_reorder_to_tail); |
Saravana Kannan | 05ef983 | 2019-10-28 15:00:22 -0700 | [diff] [blame] | 206 | list_for_each_entry(link, &dev->links.consumers, s_node) { |
| 207 | if (link->flags == (DL_FLAG_SYNC_STATE_ONLY | DL_FLAG_MANAGED)) |
| 208 | continue; |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 209 | device_reorder_to_tail(link->consumer, NULL); |
Saravana Kannan | 05ef983 | 2019-10-28 15:00:22 -0700 | [diff] [blame] | 210 | } |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 211 | |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | /** |
Feng Kan | 494fd7b | 2018-04-10 16:57:06 -0700 | [diff] [blame] | 216 | * device_pm_move_to_tail - Move set of devices to the end of device lists |
| 217 | * @dev: Device to move |
| 218 | * |
| 219 | * This is a device_reorder_to_tail() wrapper taking the requisite locks. |
| 220 | * |
| 221 | * It moves the @dev along with all of its children and all of its consumers |
| 222 | * to the ends of the device_kset and dpm_list, recursively. |
| 223 | */ |
| 224 | void device_pm_move_to_tail(struct device *dev) |
| 225 | { |
| 226 | int idx; |
| 227 | |
| 228 | idx = device_links_read_lock(); |
| 229 | device_pm_lock(); |
| 230 | device_reorder_to_tail(dev, NULL); |
| 231 | device_pm_unlock(); |
| 232 | device_links_read_unlock(idx); |
| 233 | } |
| 234 | |
Rafael J. Wysocki | 515db26 | 2019-07-16 17:21:06 +0200 | [diff] [blame] | 235 | #define DL_MANAGED_LINK_FLAGS (DL_FLAG_AUTOREMOVE_CONSUMER | \ |
| 236 | DL_FLAG_AUTOREMOVE_SUPPLIER | \ |
Saravana Kannan | 05ef983 | 2019-10-28 15:00:22 -0700 | [diff] [blame] | 237 | DL_FLAG_AUTOPROBE_CONSUMER | \ |
| 238 | DL_FLAG_SYNC_STATE_ONLY) |
Rafael J. Wysocki | 515db26 | 2019-07-16 17:21:06 +0200 | [diff] [blame] | 239 | |
Rafael J. Wysocki | fb583c8 | 2019-07-30 11:28:57 +0200 | [diff] [blame] | 240 | #define DL_ADD_VALID_FLAGS (DL_MANAGED_LINK_FLAGS | DL_FLAG_STATELESS | \ |
| 241 | DL_FLAG_PM_RUNTIME | DL_FLAG_RPM_ACTIVE) |
| 242 | |
Feng Kan | 494fd7b | 2018-04-10 16:57:06 -0700 | [diff] [blame] | 243 | /** |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 244 | * device_link_add - Create a link between two devices. |
| 245 | * @consumer: Consumer end of the link. |
| 246 | * @supplier: Supplier end of the link. |
| 247 | * @flags: Link flags. |
| 248 | * |
Rafael J. Wysocki | 21d5c57 | 2016-10-30 17:32:31 +0100 | [diff] [blame] | 249 | * The caller is responsible for the proper synchronization of the link creation |
| 250 | * with runtime PM. First, setting the DL_FLAG_PM_RUNTIME flag will cause the |
| 251 | * runtime PM framework to take the link into account. Second, if the |
| 252 | * DL_FLAG_RPM_ACTIVE flag is set in addition to it, the supplier devices will |
| 253 | * be forced into the active metastate and reference-counted upon the creation |
| 254 | * of the link. If DL_FLAG_PM_RUNTIME is not set, DL_FLAG_RPM_ACTIVE will be |
| 255 | * ignored. |
| 256 | * |
Rafael J. Wysocki | 515db26 | 2019-07-16 17:21:06 +0200 | [diff] [blame] | 257 | * If DL_FLAG_STATELESS is set in @flags, the caller of this function is |
| 258 | * expected to release the link returned by it directly with the help of either |
| 259 | * device_link_del() or device_link_remove(). |
Rafael J. Wysocki | 72175d4 | 2019-02-01 01:58:33 +0100 | [diff] [blame] | 260 | * |
| 261 | * If that flag is not set, however, the caller of this function is handing the |
| 262 | * management of the link over to the driver core entirely and its return value |
| 263 | * can only be used to check whether or not the link is present. In that case, |
| 264 | * the DL_FLAG_AUTOREMOVE_CONSUMER and DL_FLAG_AUTOREMOVE_SUPPLIER device link |
| 265 | * flags can be used to indicate to the driver core when the link can be safely |
| 266 | * deleted. Namely, setting one of them in @flags indicates to the driver core |
| 267 | * that the link is not going to be used (by the given caller of this function) |
| 268 | * after unbinding the consumer or supplier driver, respectively, from its |
| 269 | * device, so the link can be deleted at that point. If none of them is set, |
| 270 | * the link will be maintained until one of the devices pointed to by it (either |
| 271 | * the consumer or the supplier) is unregistered. |
Rafael J. Wysocki | c8d5098 | 2019-02-01 01:45:55 +0100 | [diff] [blame] | 272 | * |
Rafael J. Wysocki | e7dd401 | 2019-02-01 01:59:42 +0100 | [diff] [blame] | 273 | * Also, if DL_FLAG_STATELESS, DL_FLAG_AUTOREMOVE_CONSUMER and |
| 274 | * DL_FLAG_AUTOREMOVE_SUPPLIER are not set in @flags (that is, a persistent |
| 275 | * managed device link is being added), the DL_FLAG_AUTOPROBE_CONSUMER flag can |
| 276 | * be used to request the driver core to automaticall probe for a consmer |
| 277 | * driver after successfully binding a driver to the supplier device. |
| 278 | * |
Rafael J. Wysocki | 515db26 | 2019-07-16 17:21:06 +0200 | [diff] [blame] | 279 | * The combination of DL_FLAG_STATELESS and one of DL_FLAG_AUTOREMOVE_CONSUMER, |
| 280 | * DL_FLAG_AUTOREMOVE_SUPPLIER, or DL_FLAG_AUTOPROBE_CONSUMER set in @flags at |
| 281 | * the same time is invalid and will cause NULL to be returned upfront. |
| 282 | * However, if a device link between the given @consumer and @supplier pair |
| 283 | * exists already when this function is called for them, the existing link will |
| 284 | * be returned regardless of its current type and status (the link's flags may |
| 285 | * be modified then). The caller of this function is then expected to treat |
| 286 | * the link as though it has just been created, so (in particular) if |
| 287 | * DL_FLAG_STATELESS was passed in @flags, the link needs to be released |
| 288 | * explicitly when not needed any more (as stated above). |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 289 | * |
| 290 | * A side effect of the link creation is re-ordering of dpm_list and the |
| 291 | * devices_kset list by moving the consumer device and all devices depending |
| 292 | * on it to the ends of these lists (that does not happen to devices that have |
| 293 | * not been registered when this function is called). |
| 294 | * |
| 295 | * The supplier device is required to be registered when this function is called |
| 296 | * and NULL will be returned if that is not the case. The consumer device need |
Lukas Wunner | 64df114 | 2016-12-04 13:10:04 +0100 | [diff] [blame] | 297 | * not be registered, however. |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 298 | */ |
| 299 | struct device_link *device_link_add(struct device *consumer, |
| 300 | struct device *supplier, u32 flags) |
| 301 | { |
| 302 | struct device_link *link; |
| 303 | |
Rafael J. Wysocki | fb583c8 | 2019-07-30 11:28:57 +0200 | [diff] [blame] | 304 | if (!consumer || !supplier || flags & ~DL_ADD_VALID_FLAGS || |
Rafael J. Wysocki | 515db26 | 2019-07-16 17:21:06 +0200 | [diff] [blame] | 305 | (flags & DL_FLAG_STATELESS && flags & DL_MANAGED_LINK_FLAGS) || |
Saravana Kannan | 05ef983 | 2019-10-28 15:00:22 -0700 | [diff] [blame] | 306 | (flags & DL_FLAG_SYNC_STATE_ONLY && |
| 307 | flags != DL_FLAG_SYNC_STATE_ONLY) || |
Rafael J. Wysocki | e7dd401 | 2019-02-01 01:59:42 +0100 | [diff] [blame] | 308 | (flags & DL_FLAG_AUTOPROBE_CONSUMER && |
| 309 | flags & (DL_FLAG_AUTOREMOVE_CONSUMER | |
| 310 | DL_FLAG_AUTOREMOVE_SUPPLIER))) |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 311 | return NULL; |
| 312 | |
Rafael J. Wysocki | 5db25c9 | 2019-02-01 01:47:53 +0100 | [diff] [blame] | 313 | if (flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) { |
| 314 | if (pm_runtime_get_sync(supplier) < 0) { |
| 315 | pm_runtime_put_noidle(supplier); |
| 316 | return NULL; |
| 317 | } |
Rafael J. Wysocki | 5db25c9 | 2019-02-01 01:47:53 +0100 | [diff] [blame] | 318 | } |
| 319 | |
Rafael J. Wysocki | 515db26 | 2019-07-16 17:21:06 +0200 | [diff] [blame] | 320 | if (!(flags & DL_FLAG_STATELESS)) |
| 321 | flags |= DL_FLAG_MANAGED; |
| 322 | |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 323 | device_links_write_lock(); |
| 324 | device_pm_lock(); |
| 325 | |
| 326 | /* |
| 327 | * If the supplier has not been fully registered yet or there is a |
Saravana Kannan | 05ef983 | 2019-10-28 15:00:22 -0700 | [diff] [blame] | 328 | * reverse (non-SYNC_STATE_ONLY) dependency between the consumer and |
| 329 | * the supplier already in the graph, return NULL. If the link is a |
| 330 | * SYNC_STATE_ONLY link, we don't check for reverse dependencies |
| 331 | * because it only affects sync_state() callbacks. |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 332 | */ |
| 333 | if (!device_pm_initialized(supplier) |
Saravana Kannan | 05ef983 | 2019-10-28 15:00:22 -0700 | [diff] [blame] | 334 | || (!(flags & DL_FLAG_SYNC_STATE_ONLY) && |
| 335 | device_is_dependent(consumer, supplier))) { |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 336 | link = NULL; |
| 337 | goto out; |
| 338 | } |
| 339 | |
Rafael J. Wysocki | 72175d4 | 2019-02-01 01:58:33 +0100 | [diff] [blame] | 340 | /* |
| 341 | * DL_FLAG_AUTOREMOVE_SUPPLIER indicates that the link will be needed |
| 342 | * longer than for DL_FLAG_AUTOREMOVE_CONSUMER and setting them both |
| 343 | * together doesn't make sense, so prefer DL_FLAG_AUTOREMOVE_SUPPLIER. |
| 344 | */ |
| 345 | if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER) |
| 346 | flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER; |
| 347 | |
Rafael J. Wysocki | f265df5 | 2019-02-01 01:46:54 +0100 | [diff] [blame] | 348 | list_for_each_entry(link, &supplier->links.consumers, s_node) { |
| 349 | if (link->consumer != consumer) |
| 350 | continue; |
| 351 | |
Rafael J. Wysocki | e2f3cd8 | 2019-02-01 01:49:14 +0100 | [diff] [blame] | 352 | if (flags & DL_FLAG_PM_RUNTIME) { |
| 353 | if (!(link->flags & DL_FLAG_PM_RUNTIME)) { |
Rafael J. Wysocki | 4c06c4e | 2019-02-12 13:08:10 +0100 | [diff] [blame] | 354 | pm_runtime_new_link(consumer); |
Rafael J. Wysocki | e2f3cd8 | 2019-02-01 01:49:14 +0100 | [diff] [blame] | 355 | link->flags |= DL_FLAG_PM_RUNTIME; |
| 356 | } |
| 357 | if (flags & DL_FLAG_RPM_ACTIVE) |
Rafael J. Wysocki | 36003d4 | 2019-02-19 17:53:26 +0100 | [diff] [blame] | 358 | refcount_inc(&link->rpm_active); |
Rafael J. Wysocki | e2f3cd8 | 2019-02-01 01:49:14 +0100 | [diff] [blame] | 359 | } |
| 360 | |
Rafael J. Wysocki | 72175d4 | 2019-02-01 01:58:33 +0100 | [diff] [blame] | 361 | if (flags & DL_FLAG_STATELESS) { |
| 362 | kref_get(&link->kref); |
Saravana Kannan | 05ef983 | 2019-10-28 15:00:22 -0700 | [diff] [blame] | 363 | if (link->flags & DL_FLAG_SYNC_STATE_ONLY && |
| 364 | !(link->flags & DL_FLAG_STATELESS)) { |
| 365 | link->flags |= DL_FLAG_STATELESS; |
| 366 | goto reorder; |
| 367 | } else { |
| 368 | goto out; |
| 369 | } |
Rafael J. Wysocki | 72175d4 | 2019-02-01 01:58:33 +0100 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | /* |
| 373 | * If the life time of the link following from the new flags is |
| 374 | * longer than indicated by the flags of the existing link, |
| 375 | * update the existing link to stay around longer. |
| 376 | */ |
| 377 | if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER) { |
| 378 | if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER) { |
| 379 | link->flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER; |
| 380 | link->flags |= DL_FLAG_AUTOREMOVE_SUPPLIER; |
| 381 | } |
| 382 | } else if (!(flags & DL_FLAG_AUTOREMOVE_CONSUMER)) { |
| 383 | link->flags &= ~(DL_FLAG_AUTOREMOVE_CONSUMER | |
| 384 | DL_FLAG_AUTOREMOVE_SUPPLIER); |
| 385 | } |
Rafael J. Wysocki | 515db26 | 2019-07-16 17:21:06 +0200 | [diff] [blame] | 386 | if (!(link->flags & DL_FLAG_MANAGED)) { |
| 387 | kref_get(&link->kref); |
| 388 | link->flags |= DL_FLAG_MANAGED; |
| 389 | device_link_init_status(link, consumer, supplier); |
| 390 | } |
Saravana Kannan | 05ef983 | 2019-10-28 15:00:22 -0700 | [diff] [blame] | 391 | if (link->flags & DL_FLAG_SYNC_STATE_ONLY && |
| 392 | !(flags & DL_FLAG_SYNC_STATE_ONLY)) { |
| 393 | link->flags &= ~DL_FLAG_SYNC_STATE_ONLY; |
| 394 | goto reorder; |
| 395 | } |
| 396 | |
Rafael J. Wysocki | f265df5 | 2019-02-01 01:46:54 +0100 | [diff] [blame] | 397 | goto out; |
| 398 | } |
| 399 | |
Rafael J. Wysocki | 21d5c57 | 2016-10-30 17:32:31 +0100 | [diff] [blame] | 400 | link = kzalloc(sizeof(*link), GFP_KERNEL); |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 401 | if (!link) |
| 402 | goto out; |
| 403 | |
Rafael J. Wysocki | e2f3cd8 | 2019-02-01 01:49:14 +0100 | [diff] [blame] | 404 | refcount_set(&link->rpm_active, 1); |
| 405 | |
Rafael J. Wysocki | baa8809 | 2016-10-30 17:32:43 +0100 | [diff] [blame] | 406 | if (flags & DL_FLAG_PM_RUNTIME) { |
Rafael J. Wysocki | e2f3cd8 | 2019-02-01 01:49:14 +0100 | [diff] [blame] | 407 | if (flags & DL_FLAG_RPM_ACTIVE) |
Rafael J. Wysocki | 36003d4 | 2019-02-19 17:53:26 +0100 | [diff] [blame] | 408 | refcount_inc(&link->rpm_active); |
Rafael J. Wysocki | e2f3cd8 | 2019-02-01 01:49:14 +0100 | [diff] [blame] | 409 | |
Rafael J. Wysocki | 4c06c4e | 2019-02-12 13:08:10 +0100 | [diff] [blame] | 410 | pm_runtime_new_link(consumer); |
Rafael J. Wysocki | 21d5c57 | 2016-10-30 17:32:31 +0100 | [diff] [blame] | 411 | } |
Rafael J. Wysocki | e2f3cd8 | 2019-02-01 01:49:14 +0100 | [diff] [blame] | 412 | |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 413 | get_device(supplier); |
| 414 | link->supplier = supplier; |
| 415 | INIT_LIST_HEAD(&link->s_node); |
| 416 | get_device(consumer); |
| 417 | link->consumer = consumer; |
| 418 | INIT_LIST_HEAD(&link->c_node); |
| 419 | link->flags = flags; |
Lukas Wunner | ead18c2 | 2018-02-10 19:27:12 +0100 | [diff] [blame] | 420 | kref_init(&link->kref); |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 421 | |
Lukas Wunner | 64df114 | 2016-12-04 13:10:04 +0100 | [diff] [blame] | 422 | /* Determine the initial link state. */ |
Rafael J. Wysocki | 515db26 | 2019-07-16 17:21:06 +0200 | [diff] [blame] | 423 | if (flags & DL_FLAG_STATELESS) |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 424 | link->status = DL_STATE_NONE; |
Rafael J. Wysocki | 515db26 | 2019-07-16 17:21:06 +0200 | [diff] [blame] | 425 | else |
| 426 | device_link_init_status(link, consumer, supplier); |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 427 | |
| 428 | /* |
Rafael J. Wysocki | 15cfb09 | 2019-02-01 01:50:39 +0100 | [diff] [blame] | 429 | * Some callers expect the link creation during consumer driver probe to |
| 430 | * resume the supplier even without DL_FLAG_RPM_ACTIVE. |
| 431 | */ |
| 432 | if (link->status == DL_STATE_CONSUMER_PROBE && |
| 433 | flags & DL_FLAG_PM_RUNTIME) |
| 434 | pm_runtime_resume(supplier); |
| 435 | |
Saravana Kannan | 05ef983 | 2019-10-28 15:00:22 -0700 | [diff] [blame] | 436 | if (flags & DL_FLAG_SYNC_STATE_ONLY) { |
| 437 | dev_dbg(consumer, |
| 438 | "Linked as a sync state only consumer to %s\n", |
| 439 | dev_name(supplier)); |
| 440 | goto out; |
| 441 | } |
| 442 | reorder: |
Rafael J. Wysocki | 15cfb09 | 2019-02-01 01:50:39 +0100 | [diff] [blame] | 443 | /* |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 444 | * Move the consumer and all of the devices depending on it to the end |
| 445 | * of dpm_list and the devices_kset list. |
| 446 | * |
| 447 | * It is necessary to hold dpm_list locked throughout all that or else |
| 448 | * we may end up suspending with a wrong ordering of it. |
| 449 | */ |
| 450 | device_reorder_to_tail(consumer, NULL); |
| 451 | |
| 452 | list_add_tail_rcu(&link->s_node, &supplier->links.consumers); |
| 453 | list_add_tail_rcu(&link->c_node, &consumer->links.suppliers); |
| 454 | |
Jerome Brunet | 8a4b326 | 2018-12-21 17:23:41 +0100 | [diff] [blame] | 455 | dev_dbg(consumer, "Linked as a consumer to %s\n", dev_name(supplier)); |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 456 | |
| 457 | out: |
| 458 | device_pm_unlock(); |
| 459 | device_links_write_unlock(); |
Rafael J. Wysocki | 5db25c9 | 2019-02-01 01:47:53 +0100 | [diff] [blame] | 460 | |
Rafael J. Wysocki | e2f3cd8 | 2019-02-01 01:49:14 +0100 | [diff] [blame] | 461 | if ((flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) && !link) |
Rafael J. Wysocki | 5db25c9 | 2019-02-01 01:47:53 +0100 | [diff] [blame] | 462 | pm_runtime_put(supplier); |
| 463 | |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 464 | return link; |
| 465 | } |
| 466 | EXPORT_SYMBOL_GPL(device_link_add); |
| 467 | |
Saravana Kannan | e2ae9bc | 2019-09-04 14:11:21 -0700 | [diff] [blame] | 468 | /** |
| 469 | * device_link_wait_for_supplier - Add device to wait_for_suppliers list |
| 470 | * @consumer: Consumer device |
| 471 | * |
| 472 | * Marks the @consumer device as waiting for suppliers to become available by |
| 473 | * adding it to the wait_for_suppliers list. The consumer device will never be |
| 474 | * probed until it's removed from the wait_for_suppliers list. |
| 475 | * |
| 476 | * The caller is responsible for adding the links to the supplier devices once |
| 477 | * they are available and removing the @consumer device from the |
| 478 | * wait_for_suppliers list once links to all the suppliers have been created. |
| 479 | * |
| 480 | * This function is NOT meant to be called from the probe function of the |
| 481 | * consumer but rather from code that creates/adds the consumer device. |
| 482 | */ |
Saravana Kannan | bcbbcfd | 2019-10-28 15:00:23 -0700 | [diff] [blame] | 483 | static void device_link_wait_for_supplier(struct device *consumer, |
| 484 | bool need_for_probe) |
Saravana Kannan | e2ae9bc | 2019-09-04 14:11:21 -0700 | [diff] [blame] | 485 | { |
| 486 | mutex_lock(&wfs_lock); |
| 487 | list_add_tail(&consumer->links.needs_suppliers, &wait_for_suppliers); |
Saravana Kannan | bcbbcfd | 2019-10-28 15:00:23 -0700 | [diff] [blame] | 488 | consumer->links.need_for_probe = need_for_probe; |
Saravana Kannan | e2ae9bc | 2019-09-04 14:11:21 -0700 | [diff] [blame] | 489 | mutex_unlock(&wfs_lock); |
| 490 | } |
| 491 | |
Saravana Kannan | bcbbcfd | 2019-10-28 15:00:23 -0700 | [diff] [blame] | 492 | static void device_link_wait_for_mandatory_supplier(struct device *consumer) |
| 493 | { |
| 494 | device_link_wait_for_supplier(consumer, true); |
| 495 | } |
| 496 | |
| 497 | static void device_link_wait_for_optional_supplier(struct device *consumer) |
| 498 | { |
| 499 | device_link_wait_for_supplier(consumer, false); |
| 500 | } |
| 501 | |
Saravana Kannan | e2ae9bc | 2019-09-04 14:11:21 -0700 | [diff] [blame] | 502 | /** |
| 503 | * device_link_add_missing_supplier_links - Add links from consumer devices to |
| 504 | * supplier devices, leaving any |
| 505 | * consumer with inactive suppliers on |
| 506 | * the wait_for_suppliers list |
| 507 | * |
| 508 | * Loops through all consumers waiting on suppliers and tries to add all their |
| 509 | * supplier links. If that succeeds, the consumer device is removed from |
| 510 | * wait_for_suppliers list. Otherwise, they are left in the wait_for_suppliers |
| 511 | * list. Devices left on the wait_for_suppliers list will not be probed. |
| 512 | * |
| 513 | * The fwnode add_links callback is expected to return 0 if it has found and |
| 514 | * added all the supplier links for the consumer device. It should return an |
| 515 | * error if it isn't able to do so. |
| 516 | * |
| 517 | * The caller of device_link_wait_for_supplier() is expected to call this once |
| 518 | * it's aware of potential suppliers becoming available. |
| 519 | */ |
| 520 | static void device_link_add_missing_supplier_links(void) |
| 521 | { |
| 522 | struct device *dev, *tmp; |
| 523 | |
| 524 | mutex_lock(&wfs_lock); |
| 525 | list_for_each_entry_safe(dev, tmp, &wait_for_suppliers, |
| 526 | links.needs_suppliers) |
| 527 | if (!fwnode_call_int_op(dev->fwnode, add_links, dev)) |
| 528 | list_del_init(&dev->links.needs_suppliers); |
| 529 | mutex_unlock(&wfs_lock); |
| 530 | } |
| 531 | |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 532 | static void device_link_free(struct device_link *link) |
| 533 | { |
Rafael J. Wysocki | a1fdbfb | 2019-02-01 01:52:45 +0100 | [diff] [blame] | 534 | while (refcount_dec_not_one(&link->rpm_active)) |
| 535 | pm_runtime_put(link->supplier); |
| 536 | |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 537 | put_device(link->consumer); |
| 538 | put_device(link->supplier); |
| 539 | kfree(link); |
| 540 | } |
| 541 | |
| 542 | #ifdef CONFIG_SRCU |
| 543 | static void __device_link_free_srcu(struct rcu_head *rhead) |
| 544 | { |
| 545 | device_link_free(container_of(rhead, struct device_link, rcu_head)); |
| 546 | } |
| 547 | |
Lukas Wunner | ead18c2 | 2018-02-10 19:27:12 +0100 | [diff] [blame] | 548 | static void __device_link_del(struct kref *kref) |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 549 | { |
Lukas Wunner | ead18c2 | 2018-02-10 19:27:12 +0100 | [diff] [blame] | 550 | struct device_link *link = container_of(kref, struct device_link, kref); |
| 551 | |
Jerome Brunet | 8a4b326 | 2018-12-21 17:23:41 +0100 | [diff] [blame] | 552 | dev_dbg(link->consumer, "Dropping the link to %s\n", |
| 553 | dev_name(link->supplier)); |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 554 | |
Rafael J. Wysocki | baa8809 | 2016-10-30 17:32:43 +0100 | [diff] [blame] | 555 | if (link->flags & DL_FLAG_PM_RUNTIME) |
| 556 | pm_runtime_drop_link(link->consumer); |
| 557 | |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 558 | list_del_rcu(&link->s_node); |
| 559 | list_del_rcu(&link->c_node); |
| 560 | call_srcu(&device_links_srcu, &link->rcu_head, __device_link_free_srcu); |
| 561 | } |
| 562 | #else /* !CONFIG_SRCU */ |
Lukas Wunner | ead18c2 | 2018-02-10 19:27:12 +0100 | [diff] [blame] | 563 | static void __device_link_del(struct kref *kref) |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 564 | { |
Lukas Wunner | ead18c2 | 2018-02-10 19:27:12 +0100 | [diff] [blame] | 565 | struct device_link *link = container_of(kref, struct device_link, kref); |
| 566 | |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 567 | dev_info(link->consumer, "Dropping the link to %s\n", |
| 568 | dev_name(link->supplier)); |
| 569 | |
Lukas Wunner | 433986c | 2018-02-10 19:13:58 +0100 | [diff] [blame] | 570 | if (link->flags & DL_FLAG_PM_RUNTIME) |
| 571 | pm_runtime_drop_link(link->consumer); |
| 572 | |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 573 | list_del(&link->s_node); |
| 574 | list_del(&link->c_node); |
| 575 | device_link_free(link); |
| 576 | } |
| 577 | #endif /* !CONFIG_SRCU */ |
| 578 | |
Rafael J. Wysocki | 72175d4 | 2019-02-01 01:58:33 +0100 | [diff] [blame] | 579 | static void device_link_put_kref(struct device_link *link) |
| 580 | { |
| 581 | if (link->flags & DL_FLAG_STATELESS) |
| 582 | kref_put(&link->kref, __device_link_del); |
| 583 | else |
| 584 | WARN(1, "Unable to drop a managed device link reference\n"); |
| 585 | } |
| 586 | |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 587 | /** |
Rafael J. Wysocki | 72175d4 | 2019-02-01 01:58:33 +0100 | [diff] [blame] | 588 | * device_link_del - Delete a stateless link between two devices. |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 589 | * @link: Device link to delete. |
| 590 | * |
| 591 | * The caller must ensure proper synchronization of this function with runtime |
Lukas Wunner | ead18c2 | 2018-02-10 19:27:12 +0100 | [diff] [blame] | 592 | * PM. If the link was added multiple times, it needs to be deleted as often. |
| 593 | * Care is required for hotplugged devices: Their links are purged on removal |
| 594 | * and calling device_link_del() is then no longer allowed. |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 595 | */ |
| 596 | void device_link_del(struct device_link *link) |
| 597 | { |
| 598 | device_links_write_lock(); |
| 599 | device_pm_lock(); |
Rafael J. Wysocki | 72175d4 | 2019-02-01 01:58:33 +0100 | [diff] [blame] | 600 | device_link_put_kref(link); |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 601 | device_pm_unlock(); |
| 602 | device_links_write_unlock(); |
| 603 | } |
| 604 | EXPORT_SYMBOL_GPL(device_link_del); |
| 605 | |
pascal paillet | d884221 | 2018-07-05 14:25:56 +0000 | [diff] [blame] | 606 | /** |
Rafael J. Wysocki | 72175d4 | 2019-02-01 01:58:33 +0100 | [diff] [blame] | 607 | * device_link_remove - Delete a stateless link between two devices. |
pascal paillet | d884221 | 2018-07-05 14:25:56 +0000 | [diff] [blame] | 608 | * @consumer: Consumer end of the link. |
| 609 | * @supplier: Supplier end of the link. |
| 610 | * |
| 611 | * The caller must ensure proper synchronization of this function with runtime |
| 612 | * PM. |
| 613 | */ |
| 614 | void device_link_remove(void *consumer, struct device *supplier) |
| 615 | { |
| 616 | struct device_link *link; |
| 617 | |
| 618 | if (WARN_ON(consumer == supplier)) |
| 619 | return; |
| 620 | |
| 621 | device_links_write_lock(); |
| 622 | device_pm_lock(); |
| 623 | |
| 624 | list_for_each_entry(link, &supplier->links.consumers, s_node) { |
| 625 | if (link->consumer == consumer) { |
Rafael J. Wysocki | 72175d4 | 2019-02-01 01:58:33 +0100 | [diff] [blame] | 626 | device_link_put_kref(link); |
pascal paillet | d884221 | 2018-07-05 14:25:56 +0000 | [diff] [blame] | 627 | break; |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | device_pm_unlock(); |
| 632 | device_links_write_unlock(); |
| 633 | } |
| 634 | EXPORT_SYMBOL_GPL(device_link_remove); |
| 635 | |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 636 | static void device_links_missing_supplier(struct device *dev) |
| 637 | { |
| 638 | struct device_link *link; |
| 639 | |
| 640 | list_for_each_entry(link, &dev->links.suppliers, c_node) |
| 641 | if (link->status == DL_STATE_CONSUMER_PROBE) |
| 642 | WRITE_ONCE(link->status, DL_STATE_AVAILABLE); |
| 643 | } |
| 644 | |
| 645 | /** |
| 646 | * device_links_check_suppliers - Check presence of supplier drivers. |
| 647 | * @dev: Consumer device. |
| 648 | * |
| 649 | * Check links from this device to any suppliers. Walk the list of the device's |
| 650 | * links to suppliers and see if all of them are available. If not, simply |
| 651 | * return -EPROBE_DEFER. |
| 652 | * |
| 653 | * We need to guarantee that the supplier will not go away after the check has |
| 654 | * been positive here. It only can go away in __device_release_driver() and |
| 655 | * that function checks the device's links to consumers. This means we need to |
| 656 | * mark the link as "consumer probe in progress" to make the supplier removal |
| 657 | * wait for us to complete (or bad things may happen). |
| 658 | * |
Rafael J. Wysocki | 515db26 | 2019-07-16 17:21:06 +0200 | [diff] [blame] | 659 | * Links without the DL_FLAG_MANAGED flag set are ignored. |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 660 | */ |
| 661 | int device_links_check_suppliers(struct device *dev) |
| 662 | { |
| 663 | struct device_link *link; |
| 664 | int ret = 0; |
| 665 | |
Saravana Kannan | e2ae9bc | 2019-09-04 14:11:21 -0700 | [diff] [blame] | 666 | /* |
| 667 | * Device waiting for supplier to become available is not allowed to |
| 668 | * probe. |
| 669 | */ |
| 670 | mutex_lock(&wfs_lock); |
Saravana Kannan | bcbbcfd | 2019-10-28 15:00:23 -0700 | [diff] [blame] | 671 | if (!list_empty(&dev->links.needs_suppliers) && |
| 672 | dev->links.need_for_probe) { |
Saravana Kannan | e2ae9bc | 2019-09-04 14:11:21 -0700 | [diff] [blame] | 673 | mutex_unlock(&wfs_lock); |
| 674 | return -EPROBE_DEFER; |
| 675 | } |
| 676 | mutex_unlock(&wfs_lock); |
| 677 | |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 678 | device_links_write_lock(); |
| 679 | |
| 680 | list_for_each_entry(link, &dev->links.suppliers, c_node) { |
Saravana Kannan | 05ef983 | 2019-10-28 15:00:22 -0700 | [diff] [blame] | 681 | if (!(link->flags & DL_FLAG_MANAGED) || |
| 682 | link->flags & DL_FLAG_SYNC_STATE_ONLY) |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 683 | continue; |
| 684 | |
| 685 | if (link->status != DL_STATE_AVAILABLE) { |
| 686 | device_links_missing_supplier(dev); |
| 687 | ret = -EPROBE_DEFER; |
| 688 | break; |
| 689 | } |
| 690 | WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE); |
| 691 | } |
| 692 | dev->links.status = DL_DEV_PROBING; |
| 693 | |
| 694 | device_links_write_unlock(); |
| 695 | return ret; |
| 696 | } |
| 697 | |
Saravana Kannan | 26e7770 | 2019-11-14 14:56:45 -0800 | [diff] [blame] | 698 | /** |
| 699 | * __device_links_queue_sync_state - Queue a device for sync_state() callback |
| 700 | * @dev: Device to call sync_state() on |
| 701 | * @list: List head to queue the @dev on |
| 702 | * |
| 703 | * Queues a device for a sync_state() callback when the device links write lock |
| 704 | * isn't held. This allows the sync_state() execution flow to use device links |
| 705 | * APIs. The caller must ensure this function is called with |
| 706 | * device_links_write_lock() held. |
| 707 | * |
| 708 | * This function does a get_device() to make sure the device is not freed while |
| 709 | * on this list. |
| 710 | * |
| 711 | * So the caller must also ensure that device_links_flush_sync_list() is called |
| 712 | * as soon as the caller releases device_links_write_lock(). This is necessary |
| 713 | * to make sure the sync_state() is called in a timely fashion and the |
| 714 | * put_device() is called on this device. |
| 715 | */ |
| 716 | static void __device_links_queue_sync_state(struct device *dev, |
| 717 | struct list_head *list) |
Saravana Kannan | fc5a251 | 2019-09-04 14:11:23 -0700 | [diff] [blame] | 718 | { |
| 719 | struct device_link *link; |
| 720 | |
| 721 | if (dev->state_synced) |
| 722 | return; |
| 723 | |
| 724 | list_for_each_entry(link, &dev->links.consumers, s_node) { |
| 725 | if (!(link->flags & DL_FLAG_MANAGED)) |
| 726 | continue; |
| 727 | if (link->status != DL_STATE_ACTIVE) |
| 728 | return; |
| 729 | } |
| 730 | |
Saravana Kannan | 26e7770 | 2019-11-14 14:56:45 -0800 | [diff] [blame] | 731 | /* |
| 732 | * Set the flag here to avoid adding the same device to a list more |
| 733 | * than once. This can happen if new consumers get added to the device |
| 734 | * and probed before the list is flushed. |
| 735 | */ |
Saravana Kannan | fc5a251 | 2019-09-04 14:11:23 -0700 | [diff] [blame] | 736 | dev->state_synced = true; |
Saravana Kannan | 26e7770 | 2019-11-14 14:56:45 -0800 | [diff] [blame] | 737 | |
| 738 | if (WARN_ON(!list_empty(&dev->links.defer_sync))) |
| 739 | return; |
| 740 | |
| 741 | get_device(dev); |
| 742 | list_add_tail(&dev->links.defer_sync, list); |
| 743 | } |
| 744 | |
| 745 | /** |
| 746 | * device_links_flush_sync_list - Call sync_state() on a list of devices |
| 747 | * @list: List of devices to call sync_state() on |
| 748 | * |
| 749 | * Calls sync_state() on all the devices that have been queued for it. This |
| 750 | * function is used in conjunction with __device_links_queue_sync_state(). |
| 751 | */ |
| 752 | static void device_links_flush_sync_list(struct list_head *list) |
| 753 | { |
| 754 | struct device *dev, *tmp; |
| 755 | |
| 756 | list_for_each_entry_safe(dev, tmp, list, links.defer_sync) { |
| 757 | list_del_init(&dev->links.defer_sync); |
| 758 | |
| 759 | device_lock(dev); |
| 760 | |
| 761 | if (dev->bus->sync_state) |
| 762 | dev->bus->sync_state(dev); |
| 763 | else if (dev->driver && dev->driver->sync_state) |
| 764 | dev->driver->sync_state(dev); |
| 765 | |
| 766 | device_unlock(dev); |
| 767 | |
| 768 | put_device(dev); |
| 769 | } |
Saravana Kannan | fc5a251 | 2019-09-04 14:11:23 -0700 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | void device_links_supplier_sync_state_pause(void) |
| 773 | { |
| 774 | device_links_write_lock(); |
| 775 | defer_sync_state_count++; |
| 776 | device_links_write_unlock(); |
| 777 | } |
| 778 | |
| 779 | void device_links_supplier_sync_state_resume(void) |
| 780 | { |
| 781 | struct device *dev, *tmp; |
Saravana Kannan | 26e7770 | 2019-11-14 14:56:45 -0800 | [diff] [blame] | 782 | LIST_HEAD(sync_list); |
Saravana Kannan | fc5a251 | 2019-09-04 14:11:23 -0700 | [diff] [blame] | 783 | |
| 784 | device_links_write_lock(); |
| 785 | if (!defer_sync_state_count) { |
| 786 | WARN(true, "Unmatched sync_state pause/resume!"); |
| 787 | goto out; |
| 788 | } |
| 789 | defer_sync_state_count--; |
| 790 | if (defer_sync_state_count) |
| 791 | goto out; |
| 792 | |
| 793 | list_for_each_entry_safe(dev, tmp, &deferred_sync, links.defer_sync) { |
Saravana Kannan | 26e7770 | 2019-11-14 14:56:45 -0800 | [diff] [blame] | 794 | /* |
| 795 | * Delete from deferred_sync list before queuing it to |
| 796 | * sync_list because defer_sync is used for both lists. |
| 797 | */ |
Saravana Kannan | fc5a251 | 2019-09-04 14:11:23 -0700 | [diff] [blame] | 798 | list_del_init(&dev->links.defer_sync); |
Saravana Kannan | 26e7770 | 2019-11-14 14:56:45 -0800 | [diff] [blame] | 799 | __device_links_queue_sync_state(dev, &sync_list); |
Saravana Kannan | fc5a251 | 2019-09-04 14:11:23 -0700 | [diff] [blame] | 800 | } |
| 801 | out: |
| 802 | device_links_write_unlock(); |
Saravana Kannan | 26e7770 | 2019-11-14 14:56:45 -0800 | [diff] [blame] | 803 | |
| 804 | device_links_flush_sync_list(&sync_list); |
Saravana Kannan | fc5a251 | 2019-09-04 14:11:23 -0700 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | static int sync_state_resume_initcall(void) |
| 808 | { |
| 809 | device_links_supplier_sync_state_resume(); |
| 810 | return 0; |
| 811 | } |
| 812 | late_initcall(sync_state_resume_initcall); |
| 813 | |
| 814 | static void __device_links_supplier_defer_sync(struct device *sup) |
| 815 | { |
| 816 | if (list_empty(&sup->links.defer_sync)) |
| 817 | list_add_tail(&sup->links.defer_sync, &deferred_sync); |
| 818 | } |
| 819 | |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 820 | /** |
| 821 | * device_links_driver_bound - Update device links after probing its driver. |
| 822 | * @dev: Device to update the links for. |
| 823 | * |
| 824 | * The probe has been successful, so update links from this device to any |
| 825 | * consumers by changing their status to "available". |
| 826 | * |
| 827 | * Also change the status of @dev's links to suppliers to "active". |
| 828 | * |
Rafael J. Wysocki | 515db26 | 2019-07-16 17:21:06 +0200 | [diff] [blame] | 829 | * Links without the DL_FLAG_MANAGED flag set are ignored. |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 830 | */ |
| 831 | void device_links_driver_bound(struct device *dev) |
| 832 | { |
| 833 | struct device_link *link; |
Saravana Kannan | 26e7770 | 2019-11-14 14:56:45 -0800 | [diff] [blame] | 834 | LIST_HEAD(sync_list); |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 835 | |
Saravana Kannan | bcbbcfd | 2019-10-28 15:00:23 -0700 | [diff] [blame] | 836 | /* |
| 837 | * If a device probes successfully, it's expected to have created all |
| 838 | * the device links it needs to or make new device links as it needs |
| 839 | * them. So, it no longer needs to wait on any suppliers. |
| 840 | */ |
| 841 | mutex_lock(&wfs_lock); |
| 842 | list_del_init(&dev->links.needs_suppliers); |
| 843 | mutex_unlock(&wfs_lock); |
| 844 | |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 845 | device_links_write_lock(); |
| 846 | |
| 847 | list_for_each_entry(link, &dev->links.consumers, s_node) { |
Rafael J. Wysocki | 515db26 | 2019-07-16 17:21:06 +0200 | [diff] [blame] | 848 | if (!(link->flags & DL_FLAG_MANAGED)) |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 849 | continue; |
| 850 | |
Rafael J. Wysocki | 15cfb09 | 2019-02-01 01:50:39 +0100 | [diff] [blame] | 851 | /* |
| 852 | * Links created during consumer probe may be in the "consumer |
| 853 | * probe" state to start with if the supplier is still probing |
| 854 | * when they are created and they may become "active" if the |
| 855 | * consumer probe returns first. Skip them here. |
| 856 | */ |
| 857 | if (link->status == DL_STATE_CONSUMER_PROBE || |
| 858 | link->status == DL_STATE_ACTIVE) |
| 859 | continue; |
| 860 | |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 861 | WARN_ON(link->status != DL_STATE_DORMANT); |
| 862 | WRITE_ONCE(link->status, DL_STATE_AVAILABLE); |
Rafael J. Wysocki | e7dd401 | 2019-02-01 01:59:42 +0100 | [diff] [blame] | 863 | |
| 864 | if (link->flags & DL_FLAG_AUTOPROBE_CONSUMER) |
| 865 | driver_deferred_probe_add(link->consumer); |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | list_for_each_entry(link, &dev->links.suppliers, c_node) { |
Rafael J. Wysocki | 515db26 | 2019-07-16 17:21:06 +0200 | [diff] [blame] | 869 | if (!(link->flags & DL_FLAG_MANAGED)) |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 870 | continue; |
| 871 | |
| 872 | WARN_ON(link->status != DL_STATE_CONSUMER_PROBE); |
| 873 | WRITE_ONCE(link->status, DL_STATE_ACTIVE); |
Saravana Kannan | fc5a251 | 2019-09-04 14:11:23 -0700 | [diff] [blame] | 874 | |
| 875 | if (defer_sync_state_count) |
| 876 | __device_links_supplier_defer_sync(link->supplier); |
| 877 | else |
Saravana Kannan | 26e7770 | 2019-11-14 14:56:45 -0800 | [diff] [blame] | 878 | __device_links_queue_sync_state(link->supplier, |
| 879 | &sync_list); |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 880 | } |
| 881 | |
| 882 | dev->links.status = DL_DEV_DRIVER_BOUND; |
| 883 | |
| 884 | device_links_write_unlock(); |
Saravana Kannan | 26e7770 | 2019-11-14 14:56:45 -0800 | [diff] [blame] | 885 | |
| 886 | device_links_flush_sync_list(&sync_list); |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 887 | } |
| 888 | |
Rafael J. Wysocki | 515db26 | 2019-07-16 17:21:06 +0200 | [diff] [blame] | 889 | static void device_link_drop_managed(struct device_link *link) |
| 890 | { |
| 891 | link->flags &= ~DL_FLAG_MANAGED; |
| 892 | WRITE_ONCE(link->status, DL_STATE_NONE); |
| 893 | kref_put(&link->kref, __device_link_del); |
| 894 | } |
| 895 | |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 896 | /** |
| 897 | * __device_links_no_driver - Update links of a device without a driver. |
| 898 | * @dev: Device without a drvier. |
| 899 | * |
| 900 | * Delete all non-persistent links from this device to any suppliers. |
| 901 | * |
| 902 | * Persistent links stay around, but their status is changed to "available", |
| 903 | * unless they already are in the "supplier unbind in progress" state in which |
| 904 | * case they need not be updated. |
| 905 | * |
Rafael J. Wysocki | 515db26 | 2019-07-16 17:21:06 +0200 | [diff] [blame] | 906 | * Links without the DL_FLAG_MANAGED flag set are ignored. |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 907 | */ |
| 908 | static void __device_links_no_driver(struct device *dev) |
| 909 | { |
| 910 | struct device_link *link, *ln; |
| 911 | |
| 912 | list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) { |
Rafael J. Wysocki | 515db26 | 2019-07-16 17:21:06 +0200 | [diff] [blame] | 913 | if (!(link->flags & DL_FLAG_MANAGED)) |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 914 | continue; |
| 915 | |
Vivek Gautam | e88728f | 2018-06-27 18:20:55 +0530 | [diff] [blame] | 916 | if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER) |
Rafael J. Wysocki | 515db26 | 2019-07-16 17:21:06 +0200 | [diff] [blame] | 917 | device_link_drop_managed(link); |
Rafael J. Wysocki | 15cfb09 | 2019-02-01 01:50:39 +0100 | [diff] [blame] | 918 | else if (link->status == DL_STATE_CONSUMER_PROBE || |
| 919 | link->status == DL_STATE_ACTIVE) |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 920 | WRITE_ONCE(link->status, DL_STATE_AVAILABLE); |
| 921 | } |
| 922 | |
| 923 | dev->links.status = DL_DEV_NO_DRIVER; |
| 924 | } |
| 925 | |
Rafael J. Wysocki | 15cfb09 | 2019-02-01 01:50:39 +0100 | [diff] [blame] | 926 | /** |
| 927 | * device_links_no_driver - Update links after failing driver probe. |
| 928 | * @dev: Device whose driver has just failed to probe. |
| 929 | * |
| 930 | * Clean up leftover links to consumers for @dev and invoke |
| 931 | * %__device_links_no_driver() to update links to suppliers for it as |
| 932 | * appropriate. |
| 933 | * |
Rafael J. Wysocki | 515db26 | 2019-07-16 17:21:06 +0200 | [diff] [blame] | 934 | * Links without the DL_FLAG_MANAGED flag set are ignored. |
Rafael J. Wysocki | 15cfb09 | 2019-02-01 01:50:39 +0100 | [diff] [blame] | 935 | */ |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 936 | void device_links_no_driver(struct device *dev) |
| 937 | { |
Rafael J. Wysocki | 15cfb09 | 2019-02-01 01:50:39 +0100 | [diff] [blame] | 938 | struct device_link *link; |
| 939 | |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 940 | device_links_write_lock(); |
Rafael J. Wysocki | 15cfb09 | 2019-02-01 01:50:39 +0100 | [diff] [blame] | 941 | |
| 942 | list_for_each_entry(link, &dev->links.consumers, s_node) { |
Rafael J. Wysocki | 515db26 | 2019-07-16 17:21:06 +0200 | [diff] [blame] | 943 | if (!(link->flags & DL_FLAG_MANAGED)) |
Rafael J. Wysocki | 15cfb09 | 2019-02-01 01:50:39 +0100 | [diff] [blame] | 944 | continue; |
| 945 | |
| 946 | /* |
| 947 | * The probe has failed, so if the status of the link is |
| 948 | * "consumer probe" or "active", it must have been added by |
| 949 | * a probing consumer while this device was still probing. |
| 950 | * Change its state to "dormant", as it represents a valid |
| 951 | * relationship, but it is not functionally meaningful. |
| 952 | */ |
| 953 | if (link->status == DL_STATE_CONSUMER_PROBE || |
| 954 | link->status == DL_STATE_ACTIVE) |
| 955 | WRITE_ONCE(link->status, DL_STATE_DORMANT); |
| 956 | } |
| 957 | |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 958 | __device_links_no_driver(dev); |
Rafael J. Wysocki | 15cfb09 | 2019-02-01 01:50:39 +0100 | [diff] [blame] | 959 | |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 960 | device_links_write_unlock(); |
| 961 | } |
| 962 | |
| 963 | /** |
| 964 | * device_links_driver_cleanup - Update links after driver removal. |
| 965 | * @dev: Device whose driver has just gone away. |
| 966 | * |
| 967 | * Update links to consumers for @dev by changing their status to "dormant" and |
| 968 | * invoke %__device_links_no_driver() to update links to suppliers for it as |
| 969 | * appropriate. |
| 970 | * |
Rafael J. Wysocki | 515db26 | 2019-07-16 17:21:06 +0200 | [diff] [blame] | 971 | * Links without the DL_FLAG_MANAGED flag set are ignored. |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 972 | */ |
| 973 | void device_links_driver_cleanup(struct device *dev) |
| 974 | { |
Rafael J. Wysocki | c8d5098 | 2019-02-01 01:45:55 +0100 | [diff] [blame] | 975 | struct device_link *link, *ln; |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 976 | |
| 977 | device_links_write_lock(); |
| 978 | |
Rafael J. Wysocki | c8d5098 | 2019-02-01 01:45:55 +0100 | [diff] [blame] | 979 | list_for_each_entry_safe(link, ln, &dev->links.consumers, s_node) { |
Rafael J. Wysocki | 515db26 | 2019-07-16 17:21:06 +0200 | [diff] [blame] | 980 | if (!(link->flags & DL_FLAG_MANAGED)) |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 981 | continue; |
| 982 | |
Vivek Gautam | e88728f | 2018-06-27 18:20:55 +0530 | [diff] [blame] | 983 | WARN_ON(link->flags & DL_FLAG_AUTOREMOVE_CONSUMER); |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 984 | WARN_ON(link->status != DL_STATE_SUPPLIER_UNBIND); |
Vivek Gautam | 1689cac | 2018-06-27 18:20:56 +0530 | [diff] [blame] | 985 | |
| 986 | /* |
| 987 | * autoremove the links between this @dev and its consumer |
| 988 | * devices that are not active, i.e. where the link state |
| 989 | * has moved to DL_STATE_SUPPLIER_UNBIND. |
| 990 | */ |
| 991 | if (link->status == DL_STATE_SUPPLIER_UNBIND && |
| 992 | link->flags & DL_FLAG_AUTOREMOVE_SUPPLIER) |
Rafael J. Wysocki | 515db26 | 2019-07-16 17:21:06 +0200 | [diff] [blame] | 993 | device_link_drop_managed(link); |
Vivek Gautam | 1689cac | 2018-06-27 18:20:56 +0530 | [diff] [blame] | 994 | |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 995 | WRITE_ONCE(link->status, DL_STATE_DORMANT); |
| 996 | } |
| 997 | |
Saravana Kannan | fc5a251 | 2019-09-04 14:11:23 -0700 | [diff] [blame] | 998 | list_del_init(&dev->links.defer_sync); |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 999 | __device_links_no_driver(dev); |
| 1000 | |
| 1001 | device_links_write_unlock(); |
| 1002 | } |
| 1003 | |
| 1004 | /** |
| 1005 | * device_links_busy - Check if there are any busy links to consumers. |
| 1006 | * @dev: Device to check. |
| 1007 | * |
| 1008 | * Check each consumer of the device and return 'true' if its link's status |
| 1009 | * is one of "consumer probe" or "active" (meaning that the given consumer is |
| 1010 | * probing right now or its driver is present). Otherwise, change the link |
| 1011 | * state to "supplier unbind" to prevent the consumer from being probed |
| 1012 | * successfully going forward. |
| 1013 | * |
| 1014 | * Return 'false' if there are no probing or active consumers. |
| 1015 | * |
Rafael J. Wysocki | 515db26 | 2019-07-16 17:21:06 +0200 | [diff] [blame] | 1016 | * Links without the DL_FLAG_MANAGED flag set are ignored. |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 1017 | */ |
| 1018 | bool device_links_busy(struct device *dev) |
| 1019 | { |
| 1020 | struct device_link *link; |
| 1021 | bool ret = false; |
| 1022 | |
| 1023 | device_links_write_lock(); |
| 1024 | |
| 1025 | list_for_each_entry(link, &dev->links.consumers, s_node) { |
Rafael J. Wysocki | 515db26 | 2019-07-16 17:21:06 +0200 | [diff] [blame] | 1026 | if (!(link->flags & DL_FLAG_MANAGED)) |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 1027 | continue; |
| 1028 | |
| 1029 | if (link->status == DL_STATE_CONSUMER_PROBE |
| 1030 | || link->status == DL_STATE_ACTIVE) { |
| 1031 | ret = true; |
| 1032 | break; |
| 1033 | } |
| 1034 | WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND); |
| 1035 | } |
| 1036 | |
| 1037 | dev->links.status = DL_DEV_UNBINDING; |
| 1038 | |
| 1039 | device_links_write_unlock(); |
| 1040 | return ret; |
| 1041 | } |
| 1042 | |
| 1043 | /** |
| 1044 | * device_links_unbind_consumers - Force unbind consumers of the given device. |
| 1045 | * @dev: Device to unbind the consumers of. |
| 1046 | * |
| 1047 | * Walk the list of links to consumers for @dev and if any of them is in the |
| 1048 | * "consumer probe" state, wait for all device probes in progress to complete |
| 1049 | * and start over. |
| 1050 | * |
| 1051 | * If that's not the case, change the status of the link to "supplier unbind" |
| 1052 | * and check if the link was in the "active" state. If so, force the consumer |
| 1053 | * driver to unbind and start over (the consumer will not re-probe as we have |
| 1054 | * changed the state of the link already). |
| 1055 | * |
Rafael J. Wysocki | 515db26 | 2019-07-16 17:21:06 +0200 | [diff] [blame] | 1056 | * Links without the DL_FLAG_MANAGED flag set are ignored. |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 1057 | */ |
| 1058 | void device_links_unbind_consumers(struct device *dev) |
| 1059 | { |
| 1060 | struct device_link *link; |
| 1061 | |
| 1062 | start: |
| 1063 | device_links_write_lock(); |
| 1064 | |
| 1065 | list_for_each_entry(link, &dev->links.consumers, s_node) { |
| 1066 | enum device_link_state status; |
| 1067 | |
Saravana Kannan | 05ef983 | 2019-10-28 15:00:22 -0700 | [diff] [blame] | 1068 | if (!(link->flags & DL_FLAG_MANAGED) || |
| 1069 | link->flags & DL_FLAG_SYNC_STATE_ONLY) |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 1070 | continue; |
| 1071 | |
| 1072 | status = link->status; |
| 1073 | if (status == DL_STATE_CONSUMER_PROBE) { |
| 1074 | device_links_write_unlock(); |
| 1075 | |
| 1076 | wait_for_device_probe(); |
| 1077 | goto start; |
| 1078 | } |
| 1079 | WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND); |
| 1080 | if (status == DL_STATE_ACTIVE) { |
| 1081 | struct device *consumer = link->consumer; |
| 1082 | |
| 1083 | get_device(consumer); |
| 1084 | |
| 1085 | device_links_write_unlock(); |
| 1086 | |
| 1087 | device_release_driver_internal(consumer, NULL, |
| 1088 | consumer->parent); |
| 1089 | put_device(consumer); |
| 1090 | goto start; |
| 1091 | } |
| 1092 | } |
| 1093 | |
| 1094 | device_links_write_unlock(); |
| 1095 | } |
| 1096 | |
| 1097 | /** |
| 1098 | * device_links_purge - Delete existing links to other devices. |
| 1099 | * @dev: Target device. |
| 1100 | */ |
| 1101 | static void device_links_purge(struct device *dev) |
| 1102 | { |
| 1103 | struct device_link *link, *ln; |
| 1104 | |
Saravana Kannan | e2ae9bc | 2019-09-04 14:11:21 -0700 | [diff] [blame] | 1105 | mutex_lock(&wfs_lock); |
| 1106 | list_del(&dev->links.needs_suppliers); |
| 1107 | mutex_unlock(&wfs_lock); |
| 1108 | |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 1109 | /* |
| 1110 | * Delete all of the remaining links from this device to any other |
| 1111 | * devices (either consumers or suppliers). |
| 1112 | */ |
| 1113 | device_links_write_lock(); |
| 1114 | |
| 1115 | list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) { |
| 1116 | WARN_ON(link->status == DL_STATE_ACTIVE); |
Lukas Wunner | ead18c2 | 2018-02-10 19:27:12 +0100 | [diff] [blame] | 1117 | __device_link_del(&link->kref); |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 1118 | } |
| 1119 | |
| 1120 | list_for_each_entry_safe_reverse(link, ln, &dev->links.consumers, s_node) { |
| 1121 | WARN_ON(link->status != DL_STATE_DORMANT && |
| 1122 | link->status != DL_STATE_NONE); |
Lukas Wunner | ead18c2 | 2018-02-10 19:27:12 +0100 | [diff] [blame] | 1123 | __device_link_del(&link->kref); |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 1124 | } |
| 1125 | |
| 1126 | device_links_write_unlock(); |
| 1127 | } |
| 1128 | |
| 1129 | /* Device links support end. */ |
| 1130 | |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 1131 | int (*platform_notify)(struct device *dev) = NULL; |
| 1132 | int (*platform_notify_remove)(struct device *dev) = NULL; |
Dan Williams | e105b8b | 2008-04-21 10:51:07 -0700 | [diff] [blame] | 1133 | static struct kobject *dev_kobj; |
| 1134 | struct kobject *sysfs_dev_char_kobj; |
| 1135 | struct kobject *sysfs_dev_block_kobj; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1136 | |
Rafael J. Wysocki | 5e33bc4 | 2013-08-28 21:41:01 +0200 | [diff] [blame] | 1137 | static DEFINE_MUTEX(device_hotplug_lock); |
| 1138 | |
| 1139 | void lock_device_hotplug(void) |
| 1140 | { |
| 1141 | mutex_lock(&device_hotplug_lock); |
| 1142 | } |
| 1143 | |
| 1144 | void unlock_device_hotplug(void) |
| 1145 | { |
| 1146 | mutex_unlock(&device_hotplug_lock); |
| 1147 | } |
| 1148 | |
| 1149 | int lock_device_hotplug_sysfs(void) |
| 1150 | { |
| 1151 | if (mutex_trylock(&device_hotplug_lock)) |
| 1152 | return 0; |
| 1153 | |
| 1154 | /* Avoid busy looping (5 ms of sleep should do). */ |
| 1155 | msleep(5); |
| 1156 | return restart_syscall(); |
| 1157 | } |
| 1158 | |
Greg Kroah-Hartman | 4e886c2 | 2008-01-27 12:12:43 -0800 | [diff] [blame] | 1159 | #ifdef CONFIG_BLOCK |
| 1160 | static inline int device_is_not_partition(struct device *dev) |
| 1161 | { |
| 1162 | return !(dev->type == &part_type); |
| 1163 | } |
| 1164 | #else |
| 1165 | static inline int device_is_not_partition(struct device *dev) |
| 1166 | { |
| 1167 | return 1; |
| 1168 | } |
| 1169 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1170 | |
Heikki Krogerus | 07de0e8 | 2018-11-09 17:21:34 +0300 | [diff] [blame] | 1171 | static int |
| 1172 | device_platform_notify(struct device *dev, enum kobject_action action) |
| 1173 | { |
Heikki Krogerus | 7847a14 | 2018-11-09 17:21:35 +0300 | [diff] [blame] | 1174 | int ret; |
| 1175 | |
| 1176 | ret = acpi_platform_notify(dev, action); |
| 1177 | if (ret) |
| 1178 | return ret; |
| 1179 | |
Heikki Krogerus | 59abd83 | 2018-11-09 17:21:36 +0300 | [diff] [blame] | 1180 | ret = software_node_notify(dev, action); |
| 1181 | if (ret) |
| 1182 | return ret; |
| 1183 | |
Heikki Krogerus | 07de0e8 | 2018-11-09 17:21:34 +0300 | [diff] [blame] | 1184 | if (platform_notify && action == KOBJ_ADD) |
| 1185 | platform_notify(dev); |
| 1186 | else if (platform_notify_remove && action == KOBJ_REMOVE) |
| 1187 | platform_notify_remove(dev); |
| 1188 | return 0; |
| 1189 | } |
| 1190 | |
Alan Stern | 3e95637 | 2006-06-16 17:10:48 -0400 | [diff] [blame] | 1191 | /** |
| 1192 | * dev_driver_string - Return a device's driver name, if at all possible |
| 1193 | * @dev: struct device to get the name of |
| 1194 | * |
| 1195 | * Will return the device's driver's name if it is bound to a device. If |
yan | 9169c01 | 2012-04-20 21:08:45 +0800 | [diff] [blame] | 1196 | * the device is not bound to a driver, it will return the name of the bus |
Alan Stern | 3e95637 | 2006-06-16 17:10:48 -0400 | [diff] [blame] | 1197 | * it is attached to. If it is not attached to a bus either, an empty |
| 1198 | * string will be returned. |
| 1199 | */ |
Jean Delvare | bf9ca69 | 2008-07-30 12:29:21 -0700 | [diff] [blame] | 1200 | const char *dev_driver_string(const struct device *dev) |
Alan Stern | 3e95637 | 2006-06-16 17:10:48 -0400 | [diff] [blame] | 1201 | { |
Alan Stern | 3589972 | 2009-12-04 11:06:57 -0500 | [diff] [blame] | 1202 | struct device_driver *drv; |
| 1203 | |
| 1204 | /* dev->driver can change to NULL underneath us because of unbinding, |
| 1205 | * so be careful about accessing it. dev->bus and dev->class should |
| 1206 | * never change once they are set, so they don't need special care. |
| 1207 | */ |
Mark Rutland | 6aa7de0 | 2017-10-23 14:07:29 -0700 | [diff] [blame] | 1208 | drv = READ_ONCE(dev->driver); |
Alan Stern | 3589972 | 2009-12-04 11:06:57 -0500 | [diff] [blame] | 1209 | return drv ? drv->name : |
Jean Delvare | a456b70 | 2007-03-09 16:33:10 +0100 | [diff] [blame] | 1210 | (dev->bus ? dev->bus->name : |
| 1211 | (dev->class ? dev->class->name : "")); |
Alan Stern | 3e95637 | 2006-06-16 17:10:48 -0400 | [diff] [blame] | 1212 | } |
Matthew Wilcox | 310a922 | 2006-09-23 23:35:04 -0600 | [diff] [blame] | 1213 | EXPORT_SYMBOL(dev_driver_string); |
Alan Stern | 3e95637 | 2006-06-16 17:10:48 -0400 | [diff] [blame] | 1214 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1215 | #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr) |
| 1216 | |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 1217 | static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr, |
| 1218 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 | { |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 1220 | struct device_attribute *dev_attr = to_dev_attr(attr); |
Lars-Peter Clausen | b0d1f80 | 2012-07-03 18:49:36 +0200 | [diff] [blame] | 1221 | struct device *dev = kobj_to_dev(kobj); |
Dmitry Torokhov | 4a0c20b | 2005-04-29 01:23:47 -0500 | [diff] [blame] | 1222 | ssize_t ret = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1223 | |
| 1224 | if (dev_attr->show) |
Yani Ioannou | 54b6f35 | 2005-05-17 06:39:34 -0400 | [diff] [blame] | 1225 | ret = dev_attr->show(dev, dev_attr, buf); |
Andrew Morton | 815d2d5 | 2008-03-04 15:09:07 -0800 | [diff] [blame] | 1226 | if (ret >= (ssize_t)PAGE_SIZE) { |
Sergey Senozhatsky | a52668c | 2017-12-11 21:50:21 +0900 | [diff] [blame] | 1227 | printk("dev_attr_show: %pS returned bad count\n", |
| 1228 | dev_attr->show); |
Andrew Morton | 815d2d5 | 2008-03-04 15:09:07 -0800 | [diff] [blame] | 1229 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1230 | return ret; |
| 1231 | } |
| 1232 | |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 1233 | static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 1234 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1235 | { |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 1236 | struct device_attribute *dev_attr = to_dev_attr(attr); |
Lars-Peter Clausen | b0d1f80 | 2012-07-03 18:49:36 +0200 | [diff] [blame] | 1237 | struct device *dev = kobj_to_dev(kobj); |
Dmitry Torokhov | 4a0c20b | 2005-04-29 01:23:47 -0500 | [diff] [blame] | 1238 | ssize_t ret = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | |
| 1240 | if (dev_attr->store) |
Yani Ioannou | 54b6f35 | 2005-05-17 06:39:34 -0400 | [diff] [blame] | 1241 | ret = dev_attr->store(dev, dev_attr, buf, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1242 | return ret; |
| 1243 | } |
| 1244 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 1245 | static const struct sysfs_ops dev_sysfs_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1246 | .show = dev_attr_show, |
| 1247 | .store = dev_attr_store, |
| 1248 | }; |
| 1249 | |
Kay Sievers | ca22e56 | 2011-12-14 14:29:38 -0800 | [diff] [blame] | 1250 | #define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr) |
| 1251 | |
| 1252 | ssize_t device_store_ulong(struct device *dev, |
| 1253 | struct device_attribute *attr, |
| 1254 | const char *buf, size_t size) |
| 1255 | { |
| 1256 | struct dev_ext_attribute *ea = to_ext_attr(attr); |
Kaitao cheng | f88184b | 2018-11-06 08:34:54 -0800 | [diff] [blame] | 1257 | int ret; |
| 1258 | unsigned long new; |
| 1259 | |
| 1260 | ret = kstrtoul(buf, 0, &new); |
| 1261 | if (ret) |
| 1262 | return ret; |
Kay Sievers | ca22e56 | 2011-12-14 14:29:38 -0800 | [diff] [blame] | 1263 | *(unsigned long *)(ea->var) = new; |
| 1264 | /* Always return full write size even if we didn't consume all */ |
| 1265 | return size; |
| 1266 | } |
| 1267 | EXPORT_SYMBOL_GPL(device_store_ulong); |
| 1268 | |
| 1269 | ssize_t device_show_ulong(struct device *dev, |
| 1270 | struct device_attribute *attr, |
| 1271 | char *buf) |
| 1272 | { |
| 1273 | struct dev_ext_attribute *ea = to_ext_attr(attr); |
| 1274 | return snprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var)); |
| 1275 | } |
| 1276 | EXPORT_SYMBOL_GPL(device_show_ulong); |
| 1277 | |
| 1278 | ssize_t device_store_int(struct device *dev, |
| 1279 | struct device_attribute *attr, |
| 1280 | const char *buf, size_t size) |
| 1281 | { |
| 1282 | struct dev_ext_attribute *ea = to_ext_attr(attr); |
Kaitao cheng | f88184b | 2018-11-06 08:34:54 -0800 | [diff] [blame] | 1283 | int ret; |
| 1284 | long new; |
| 1285 | |
| 1286 | ret = kstrtol(buf, 0, &new); |
| 1287 | if (ret) |
| 1288 | return ret; |
| 1289 | |
| 1290 | if (new > INT_MAX || new < INT_MIN) |
Kay Sievers | ca22e56 | 2011-12-14 14:29:38 -0800 | [diff] [blame] | 1291 | return -EINVAL; |
| 1292 | *(int *)(ea->var) = new; |
| 1293 | /* Always return full write size even if we didn't consume all */ |
| 1294 | return size; |
| 1295 | } |
| 1296 | EXPORT_SYMBOL_GPL(device_store_int); |
| 1297 | |
| 1298 | ssize_t device_show_int(struct device *dev, |
| 1299 | struct device_attribute *attr, |
| 1300 | char *buf) |
| 1301 | { |
| 1302 | struct dev_ext_attribute *ea = to_ext_attr(attr); |
| 1303 | |
| 1304 | return snprintf(buf, PAGE_SIZE, "%d\n", *(int *)(ea->var)); |
| 1305 | } |
| 1306 | EXPORT_SYMBOL_GPL(device_show_int); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1307 | |
Borislav Petkov | 9187239 | 2012-10-09 19:52:05 +0200 | [diff] [blame] | 1308 | ssize_t device_store_bool(struct device *dev, struct device_attribute *attr, |
| 1309 | const char *buf, size_t size) |
| 1310 | { |
| 1311 | struct dev_ext_attribute *ea = to_ext_attr(attr); |
| 1312 | |
| 1313 | if (strtobool(buf, ea->var) < 0) |
| 1314 | return -EINVAL; |
| 1315 | |
| 1316 | return size; |
| 1317 | } |
| 1318 | EXPORT_SYMBOL_GPL(device_store_bool); |
| 1319 | |
| 1320 | ssize_t device_show_bool(struct device *dev, struct device_attribute *attr, |
| 1321 | char *buf) |
| 1322 | { |
| 1323 | struct dev_ext_attribute *ea = to_ext_attr(attr); |
| 1324 | |
| 1325 | return snprintf(buf, PAGE_SIZE, "%d\n", *(bool *)(ea->var)); |
| 1326 | } |
| 1327 | EXPORT_SYMBOL_GPL(device_show_bool); |
| 1328 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1329 | /** |
Robert P. J. Day | f8878dc | 2013-06-01 20:17:34 -0400 | [diff] [blame] | 1330 | * device_release - free device structure. |
| 1331 | * @kobj: device's kobject. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1332 | * |
Robert P. J. Day | f8878dc | 2013-06-01 20:17:34 -0400 | [diff] [blame] | 1333 | * This is called once the reference count for the object |
| 1334 | * reaches 0. We forward the call to the device's release |
| 1335 | * method, which should handle actually freeing the structure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1336 | */ |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 1337 | static void device_release(struct kobject *kobj) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1338 | { |
Lars-Peter Clausen | b0d1f80 | 2012-07-03 18:49:36 +0200 | [diff] [blame] | 1339 | struct device *dev = kobj_to_dev(kobj); |
Greg Kroah-Hartman | fb069a5 | 2008-12-16 12:23:36 -0800 | [diff] [blame] | 1340 | struct device_private *p = dev->p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1341 | |
Ming Lei | a525a3d | 2012-07-25 01:42:29 +0800 | [diff] [blame] | 1342 | /* |
| 1343 | * Some platform devices are driven without driver attached |
| 1344 | * and managed resources may have been acquired. Make sure |
| 1345 | * all resources are released. |
| 1346 | * |
| 1347 | * Drivers still can add resources into device after device |
| 1348 | * is deleted but alive, so release devres here to avoid |
| 1349 | * possible memory leak. |
| 1350 | */ |
| 1351 | devres_release_all(dev); |
| 1352 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1353 | if (dev->release) |
| 1354 | dev->release(dev); |
Kay Sievers | f9f852d | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 1355 | else if (dev->type && dev->type->release) |
| 1356 | dev->type->release(dev); |
Greg Kroah-Hartman | 2620efe | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 1357 | else if (dev->class && dev->class->dev_release) |
| 1358 | dev->class->dev_release(dev); |
Arjan van de Ven | f810a5c | 2008-07-25 19:45:39 -0700 | [diff] [blame] | 1359 | else |
Ezequiel Garcia | 186bddb | 2018-12-03 13:44:35 -0300 | [diff] [blame] | 1360 | WARN(1, KERN_ERR "Device '%s' does not have a release() function, it is broken and must be fixed. See Documentation/kobject.txt.\n", |
Kay Sievers | 1e0b2cf | 2008-10-30 01:36:48 +0100 | [diff] [blame] | 1361 | dev_name(dev)); |
Greg Kroah-Hartman | fb069a5 | 2008-12-16 12:23:36 -0800 | [diff] [blame] | 1362 | kfree(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1363 | } |
| 1364 | |
Eric W. Biederman | bc451f2 | 2010-03-30 11:31:25 -0700 | [diff] [blame] | 1365 | static const void *device_namespace(struct kobject *kobj) |
| 1366 | { |
Lars-Peter Clausen | b0d1f80 | 2012-07-03 18:49:36 +0200 | [diff] [blame] | 1367 | struct device *dev = kobj_to_dev(kobj); |
Eric W. Biederman | bc451f2 | 2010-03-30 11:31:25 -0700 | [diff] [blame] | 1368 | const void *ns = NULL; |
| 1369 | |
| 1370 | if (dev->class && dev->class->ns_type) |
| 1371 | ns = dev->class->namespace(dev); |
| 1372 | |
| 1373 | return ns; |
| 1374 | } |
| 1375 | |
Dmitry Torokhov | 9944e89 | 2018-07-20 21:56:50 +0000 | [diff] [blame] | 1376 | static void device_get_ownership(struct kobject *kobj, kuid_t *uid, kgid_t *gid) |
| 1377 | { |
| 1378 | struct device *dev = kobj_to_dev(kobj); |
| 1379 | |
| 1380 | if (dev->class && dev->class->get_ownership) |
| 1381 | dev->class->get_ownership(dev, uid, gid); |
| 1382 | } |
| 1383 | |
Greg Kroah-Hartman | 8f4afc4 | 2007-10-11 10:47:49 -0600 | [diff] [blame] | 1384 | static struct kobj_type device_ktype = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1385 | .release = device_release, |
| 1386 | .sysfs_ops = &dev_sysfs_ops, |
Eric W. Biederman | bc451f2 | 2010-03-30 11:31:25 -0700 | [diff] [blame] | 1387 | .namespace = device_namespace, |
Dmitry Torokhov | 9944e89 | 2018-07-20 21:56:50 +0000 | [diff] [blame] | 1388 | .get_ownership = device_get_ownership, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1389 | }; |
| 1390 | |
| 1391 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 1392 | static int dev_uevent_filter(struct kset *kset, struct kobject *kobj) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1393 | { |
| 1394 | struct kobj_type *ktype = get_ktype(kobj); |
| 1395 | |
Greg Kroah-Hartman | 8f4afc4 | 2007-10-11 10:47:49 -0600 | [diff] [blame] | 1396 | if (ktype == &device_ktype) { |
Lars-Peter Clausen | b0d1f80 | 2012-07-03 18:49:36 +0200 | [diff] [blame] | 1397 | struct device *dev = kobj_to_dev(kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1398 | if (dev->bus) |
| 1399 | return 1; |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 1400 | if (dev->class) |
| 1401 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | } |
| 1403 | return 0; |
| 1404 | } |
| 1405 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 1406 | static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1407 | { |
Lars-Peter Clausen | b0d1f80 | 2012-07-03 18:49:36 +0200 | [diff] [blame] | 1408 | struct device *dev = kobj_to_dev(kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 1410 | if (dev->bus) |
| 1411 | return dev->bus->name; |
| 1412 | if (dev->class) |
| 1413 | return dev->class->name; |
| 1414 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1415 | } |
| 1416 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 1417 | static int dev_uevent(struct kset *kset, struct kobject *kobj, |
| 1418 | struct kobj_uevent_env *env) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1419 | { |
Lars-Peter Clausen | b0d1f80 | 2012-07-03 18:49:36 +0200 | [diff] [blame] | 1420 | struct device *dev = kobj_to_dev(kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1421 | int retval = 0; |
| 1422 | |
Kay Sievers | 6fcf53a | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 1423 | /* add device node properties if present */ |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 1424 | if (MAJOR(dev->devt)) { |
Kay Sievers | 6fcf53a | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 1425 | const char *tmp; |
| 1426 | const char *name; |
Al Viro | 2c9ede5 | 2011-07-23 20:24:48 -0400 | [diff] [blame] | 1427 | umode_t mode = 0; |
Greg Kroah-Hartman | 4e4098a | 2013-04-11 11:43:29 -0700 | [diff] [blame] | 1428 | kuid_t uid = GLOBAL_ROOT_UID; |
| 1429 | kgid_t gid = GLOBAL_ROOT_GID; |
Kay Sievers | 6fcf53a | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 1430 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 1431 | add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt)); |
| 1432 | add_uevent_var(env, "MINOR=%u", MINOR(dev->devt)); |
Kay Sievers | 3c2670e | 2013-04-06 09:56:00 -0700 | [diff] [blame] | 1433 | name = device_get_devnode(dev, &mode, &uid, &gid, &tmp); |
Kay Sievers | 6fcf53a | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 1434 | if (name) { |
| 1435 | add_uevent_var(env, "DEVNAME=%s", name); |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 1436 | if (mode) |
| 1437 | add_uevent_var(env, "DEVMODE=%#o", mode & 0777); |
Greg Kroah-Hartman | 4e4098a | 2013-04-11 11:43:29 -0700 | [diff] [blame] | 1438 | if (!uid_eq(uid, GLOBAL_ROOT_UID)) |
| 1439 | add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid)); |
| 1440 | if (!gid_eq(gid, GLOBAL_ROOT_GID)) |
| 1441 | add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid)); |
Kay Sievers | 3c2670e | 2013-04-06 09:56:00 -0700 | [diff] [blame] | 1442 | kfree(tmp); |
Kay Sievers | 6fcf53a | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 1443 | } |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 1444 | } |
| 1445 | |
Kay Sievers | 414264f | 2007-03-12 21:08:57 +0100 | [diff] [blame] | 1446 | if (dev->type && dev->type->name) |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 1447 | add_uevent_var(env, "DEVTYPE=%s", dev->type->name); |
Kay Sievers | 414264f | 2007-03-12 21:08:57 +0100 | [diff] [blame] | 1448 | |
Kay Sievers | 239378f | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 1449 | if (dev->driver) |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 1450 | add_uevent_var(env, "DRIVER=%s", dev->driver->name); |
Kay Sievers | 239378f | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 1451 | |
Grant Likely | 07d57a3 | 2012-02-01 11:22:22 -0700 | [diff] [blame] | 1452 | /* Add common DT information about the device */ |
| 1453 | of_device_uevent(dev, env); |
| 1454 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 1455 | /* have the bus specific function add its stuff */ |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 1456 | if (dev->bus && dev->bus->uevent) { |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 1457 | retval = dev->bus->uevent(dev, env); |
Kay Sievers | f9f852d | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 1458 | if (retval) |
Greg Kroah-Hartman | 7dc72b2 | 2007-11-28 23:49:41 -0800 | [diff] [blame] | 1459 | pr_debug("device: '%s': %s: bus uevent() returned %d\n", |
Kay Sievers | 1e0b2cf | 2008-10-30 01:36:48 +0100 | [diff] [blame] | 1460 | dev_name(dev), __func__, retval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1461 | } |
| 1462 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 1463 | /* have the class specific function add its stuff */ |
Greg Kroah-Hartman | 2620efe | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 1464 | if (dev->class && dev->class->dev_uevent) { |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 1465 | retval = dev->class->dev_uevent(dev, env); |
Kay Sievers | f9f852d | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 1466 | if (retval) |
Greg Kroah-Hartman | 7dc72b2 | 2007-11-28 23:49:41 -0800 | [diff] [blame] | 1467 | pr_debug("device: '%s': %s: class uevent() " |
Kay Sievers | 1e0b2cf | 2008-10-30 01:36:48 +0100 | [diff] [blame] | 1468 | "returned %d\n", dev_name(dev), |
Harvey Harrison | 2b3a302 | 2008-03-04 16:41:05 -0800 | [diff] [blame] | 1469 | __func__, retval); |
Kay Sievers | f9f852d | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 1470 | } |
| 1471 | |
Stefan Weil | eef35c2 | 2010-08-06 21:11:15 +0200 | [diff] [blame] | 1472 | /* have the device type specific function add its stuff */ |
Kay Sievers | f9f852d | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 1473 | if (dev->type && dev->type->uevent) { |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 1474 | retval = dev->type->uevent(dev, env); |
Kay Sievers | f9f852d | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 1475 | if (retval) |
Greg Kroah-Hartman | 7dc72b2 | 2007-11-28 23:49:41 -0800 | [diff] [blame] | 1476 | pr_debug("device: '%s': %s: dev_type uevent() " |
Kay Sievers | 1e0b2cf | 2008-10-30 01:36:48 +0100 | [diff] [blame] | 1477 | "returned %d\n", dev_name(dev), |
Harvey Harrison | 2b3a302 | 2008-03-04 16:41:05 -0800 | [diff] [blame] | 1478 | __func__, retval); |
Greg Kroah-Hartman | 2620efe | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 1479 | } |
| 1480 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1481 | return retval; |
| 1482 | } |
| 1483 | |
Emese Revfy | 9cd4361 | 2009-12-31 14:52:51 +0100 | [diff] [blame] | 1484 | static const struct kset_uevent_ops device_uevent_ops = { |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 1485 | .filter = dev_uevent_filter, |
| 1486 | .name = dev_uevent_name, |
| 1487 | .uevent = dev_uevent, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1488 | }; |
| 1489 | |
Greg Kroah-Hartman | c5e064a | 2013-08-23 17:07:26 -0700 | [diff] [blame] | 1490 | static ssize_t uevent_show(struct device *dev, struct device_attribute *attr, |
Kay Sievers | 16574dc | 2007-04-06 01:40:38 +0200 | [diff] [blame] | 1491 | char *buf) |
| 1492 | { |
| 1493 | struct kobject *top_kobj; |
| 1494 | struct kset *kset; |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 1495 | struct kobj_uevent_env *env = NULL; |
Kay Sievers | 16574dc | 2007-04-06 01:40:38 +0200 | [diff] [blame] | 1496 | int i; |
| 1497 | size_t count = 0; |
| 1498 | int retval; |
| 1499 | |
| 1500 | /* search the kset, the device belongs to */ |
| 1501 | top_kobj = &dev->kobj; |
Kay Sievers | 5c5daf6 | 2007-08-12 20:43:55 +0200 | [diff] [blame] | 1502 | while (!top_kobj->kset && top_kobj->parent) |
| 1503 | top_kobj = top_kobj->parent; |
Kay Sievers | 16574dc | 2007-04-06 01:40:38 +0200 | [diff] [blame] | 1504 | if (!top_kobj->kset) |
| 1505 | goto out; |
Kay Sievers | 5c5daf6 | 2007-08-12 20:43:55 +0200 | [diff] [blame] | 1506 | |
Kay Sievers | 16574dc | 2007-04-06 01:40:38 +0200 | [diff] [blame] | 1507 | kset = top_kobj->kset; |
| 1508 | if (!kset->uevent_ops || !kset->uevent_ops->uevent) |
| 1509 | goto out; |
| 1510 | |
| 1511 | /* respect filter */ |
| 1512 | if (kset->uevent_ops && kset->uevent_ops->filter) |
| 1513 | if (!kset->uevent_ops->filter(kset, &dev->kobj)) |
| 1514 | goto out; |
| 1515 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 1516 | env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL); |
| 1517 | if (!env) |
Greg Kroah-Hartman | c7308c8 | 2007-05-02 14:14:11 +0200 | [diff] [blame] | 1518 | return -ENOMEM; |
| 1519 | |
Kay Sievers | 16574dc | 2007-04-06 01:40:38 +0200 | [diff] [blame] | 1520 | /* let the kset specific function add its keys */ |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 1521 | retval = kset->uevent_ops->uevent(kset, &dev->kobj, env); |
Kay Sievers | 16574dc | 2007-04-06 01:40:38 +0200 | [diff] [blame] | 1522 | if (retval) |
| 1523 | goto out; |
| 1524 | |
| 1525 | /* copy keys to file */ |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 1526 | for (i = 0; i < env->envp_idx; i++) |
| 1527 | count += sprintf(&buf[count], "%s\n", env->envp[i]); |
Kay Sievers | 16574dc | 2007-04-06 01:40:38 +0200 | [diff] [blame] | 1528 | out: |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 1529 | kfree(env); |
Kay Sievers | 16574dc | 2007-04-06 01:40:38 +0200 | [diff] [blame] | 1530 | return count; |
| 1531 | } |
| 1532 | |
Greg Kroah-Hartman | c5e064a | 2013-08-23 17:07:26 -0700 | [diff] [blame] | 1533 | static ssize_t uevent_store(struct device *dev, struct device_attribute *attr, |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 1534 | const char *buf, size_t count) |
| 1535 | { |
Peter Rajnoha | df44b47 | 2018-12-05 12:27:44 +0100 | [diff] [blame] | 1536 | int rc; |
| 1537 | |
| 1538 | rc = kobject_synth_uevent(&dev->kobj, buf, count); |
| 1539 | |
| 1540 | if (rc) { |
Peter Rajnoha | f36776f | 2017-05-09 15:22:30 +0200 | [diff] [blame] | 1541 | dev_err(dev, "uevent: failed to send synthetic uevent\n"); |
Peter Rajnoha | df44b47 | 2018-12-05 12:27:44 +0100 | [diff] [blame] | 1542 | return rc; |
| 1543 | } |
Kay Sievers | 60a96a5 | 2007-07-08 22:29:26 +0200 | [diff] [blame] | 1544 | |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 1545 | return count; |
| 1546 | } |
Greg Kroah-Hartman | c5e064a | 2013-08-23 17:07:26 -0700 | [diff] [blame] | 1547 | static DEVICE_ATTR_RW(uevent); |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 1548 | |
Greg Kroah-Hartman | c5e064a | 2013-08-23 17:07:26 -0700 | [diff] [blame] | 1549 | static ssize_t online_show(struct device *dev, struct device_attribute *attr, |
Rafael J. Wysocki | 4f3549d | 2013-05-02 22:15:29 +0200 | [diff] [blame] | 1550 | char *buf) |
| 1551 | { |
| 1552 | bool val; |
| 1553 | |
Rafael J. Wysocki | 5e33bc4 | 2013-08-28 21:41:01 +0200 | [diff] [blame] | 1554 | device_lock(dev); |
Rafael J. Wysocki | 4f3549d | 2013-05-02 22:15:29 +0200 | [diff] [blame] | 1555 | val = !dev->offline; |
Rafael J. Wysocki | 5e33bc4 | 2013-08-28 21:41:01 +0200 | [diff] [blame] | 1556 | device_unlock(dev); |
Rafael J. Wysocki | 4f3549d | 2013-05-02 22:15:29 +0200 | [diff] [blame] | 1557 | return sprintf(buf, "%u\n", val); |
| 1558 | } |
| 1559 | |
Greg Kroah-Hartman | c5e064a | 2013-08-23 17:07:26 -0700 | [diff] [blame] | 1560 | static ssize_t online_store(struct device *dev, struct device_attribute *attr, |
Rafael J. Wysocki | 4f3549d | 2013-05-02 22:15:29 +0200 | [diff] [blame] | 1561 | const char *buf, size_t count) |
| 1562 | { |
| 1563 | bool val; |
| 1564 | int ret; |
| 1565 | |
| 1566 | ret = strtobool(buf, &val); |
| 1567 | if (ret < 0) |
| 1568 | return ret; |
| 1569 | |
Rafael J. Wysocki | 5e33bc4 | 2013-08-28 21:41:01 +0200 | [diff] [blame] | 1570 | ret = lock_device_hotplug_sysfs(); |
| 1571 | if (ret) |
| 1572 | return ret; |
| 1573 | |
Rafael J. Wysocki | 4f3549d | 2013-05-02 22:15:29 +0200 | [diff] [blame] | 1574 | ret = val ? device_online(dev) : device_offline(dev); |
| 1575 | unlock_device_hotplug(); |
| 1576 | return ret < 0 ? ret : count; |
| 1577 | } |
Greg Kroah-Hartman | c5e064a | 2013-08-23 17:07:26 -0700 | [diff] [blame] | 1578 | static DEVICE_ATTR_RW(online); |
Rafael J. Wysocki | 4f3549d | 2013-05-02 22:15:29 +0200 | [diff] [blame] | 1579 | |
Greg Kroah-Hartman | fa6fdb3 | 2013-08-08 15:22:55 -0700 | [diff] [blame] | 1580 | int device_add_groups(struct device *dev, const struct attribute_group **groups) |
Dmitry Torokhov | 621a167 | 2007-03-10 01:37:34 -0500 | [diff] [blame] | 1581 | { |
Greg Kroah-Hartman | 3e9b2ba | 2013-08-21 13:47:50 -0700 | [diff] [blame] | 1582 | return sysfs_create_groups(&dev->kobj, groups); |
Dmitry Torokhov | 621a167 | 2007-03-10 01:37:34 -0500 | [diff] [blame] | 1583 | } |
Dmitry Torokhov | a7670d4 | 2017-07-19 17:24:31 -0700 | [diff] [blame] | 1584 | EXPORT_SYMBOL_GPL(device_add_groups); |
Dmitry Torokhov | 621a167 | 2007-03-10 01:37:34 -0500 | [diff] [blame] | 1585 | |
Greg Kroah-Hartman | fa6fdb3 | 2013-08-08 15:22:55 -0700 | [diff] [blame] | 1586 | void device_remove_groups(struct device *dev, |
| 1587 | const struct attribute_group **groups) |
Dmitry Torokhov | 621a167 | 2007-03-10 01:37:34 -0500 | [diff] [blame] | 1588 | { |
Greg Kroah-Hartman | 3e9b2ba | 2013-08-21 13:47:50 -0700 | [diff] [blame] | 1589 | sysfs_remove_groups(&dev->kobj, groups); |
Greg Kroah-Hartman | de0ff00 | 2006-06-27 00:06:09 -0700 | [diff] [blame] | 1590 | } |
Dmitry Torokhov | a7670d4 | 2017-07-19 17:24:31 -0700 | [diff] [blame] | 1591 | EXPORT_SYMBOL_GPL(device_remove_groups); |
Greg Kroah-Hartman | de0ff00 | 2006-06-27 00:06:09 -0700 | [diff] [blame] | 1592 | |
Dmitry Torokhov | 57b8ff0 | 2017-07-19 17:24:33 -0700 | [diff] [blame] | 1593 | union device_attr_group_devres { |
| 1594 | const struct attribute_group *group; |
| 1595 | const struct attribute_group **groups; |
| 1596 | }; |
| 1597 | |
| 1598 | static int devm_attr_group_match(struct device *dev, void *res, void *data) |
| 1599 | { |
| 1600 | return ((union device_attr_group_devres *)res)->group == data; |
| 1601 | } |
| 1602 | |
| 1603 | static void devm_attr_group_remove(struct device *dev, void *res) |
| 1604 | { |
| 1605 | union device_attr_group_devres *devres = res; |
| 1606 | const struct attribute_group *group = devres->group; |
| 1607 | |
| 1608 | dev_dbg(dev, "%s: removing group %p\n", __func__, group); |
| 1609 | sysfs_remove_group(&dev->kobj, group); |
| 1610 | } |
| 1611 | |
| 1612 | static void devm_attr_groups_remove(struct device *dev, void *res) |
| 1613 | { |
| 1614 | union device_attr_group_devres *devres = res; |
| 1615 | const struct attribute_group **groups = devres->groups; |
| 1616 | |
| 1617 | dev_dbg(dev, "%s: removing groups %p\n", __func__, groups); |
| 1618 | sysfs_remove_groups(&dev->kobj, groups); |
| 1619 | } |
| 1620 | |
| 1621 | /** |
| 1622 | * devm_device_add_group - given a device, create a managed attribute group |
| 1623 | * @dev: The device to create the group for |
| 1624 | * @grp: The attribute group to create |
| 1625 | * |
| 1626 | * This function creates a group for the first time. It will explicitly |
| 1627 | * warn and error if any of the attribute files being created already exist. |
| 1628 | * |
| 1629 | * Returns 0 on success or error code on failure. |
| 1630 | */ |
| 1631 | int devm_device_add_group(struct device *dev, const struct attribute_group *grp) |
| 1632 | { |
| 1633 | union device_attr_group_devres *devres; |
| 1634 | int error; |
| 1635 | |
| 1636 | devres = devres_alloc(devm_attr_group_remove, |
| 1637 | sizeof(*devres), GFP_KERNEL); |
| 1638 | if (!devres) |
| 1639 | return -ENOMEM; |
| 1640 | |
| 1641 | error = sysfs_create_group(&dev->kobj, grp); |
| 1642 | if (error) { |
| 1643 | devres_free(devres); |
| 1644 | return error; |
| 1645 | } |
| 1646 | |
| 1647 | devres->group = grp; |
| 1648 | devres_add(dev, devres); |
| 1649 | return 0; |
| 1650 | } |
| 1651 | EXPORT_SYMBOL_GPL(devm_device_add_group); |
| 1652 | |
| 1653 | /** |
| 1654 | * devm_device_remove_group: remove a managed group from a device |
| 1655 | * @dev: device to remove the group from |
| 1656 | * @grp: group to remove |
| 1657 | * |
| 1658 | * This function removes a group of attributes from a device. The attributes |
| 1659 | * previously have to have been created for this group, otherwise it will fail. |
| 1660 | */ |
| 1661 | void devm_device_remove_group(struct device *dev, |
| 1662 | const struct attribute_group *grp) |
| 1663 | { |
| 1664 | WARN_ON(devres_release(dev, devm_attr_group_remove, |
| 1665 | devm_attr_group_match, |
| 1666 | /* cast away const */ (void *)grp)); |
| 1667 | } |
| 1668 | EXPORT_SYMBOL_GPL(devm_device_remove_group); |
| 1669 | |
| 1670 | /** |
| 1671 | * devm_device_add_groups - create a bunch of managed attribute groups |
| 1672 | * @dev: The device to create the group for |
| 1673 | * @groups: The attribute groups to create, NULL terminated |
| 1674 | * |
| 1675 | * This function creates a bunch of managed attribute groups. If an error |
| 1676 | * occurs when creating a group, all previously created groups will be |
| 1677 | * removed, unwinding everything back to the original state when this |
| 1678 | * function was called. It will explicitly warn and error if any of the |
| 1679 | * attribute files being created already exist. |
| 1680 | * |
| 1681 | * Returns 0 on success or error code from sysfs_create_group on failure. |
| 1682 | */ |
| 1683 | int devm_device_add_groups(struct device *dev, |
| 1684 | const struct attribute_group **groups) |
| 1685 | { |
| 1686 | union device_attr_group_devres *devres; |
| 1687 | int error; |
| 1688 | |
| 1689 | devres = devres_alloc(devm_attr_groups_remove, |
| 1690 | sizeof(*devres), GFP_KERNEL); |
| 1691 | if (!devres) |
| 1692 | return -ENOMEM; |
| 1693 | |
| 1694 | error = sysfs_create_groups(&dev->kobj, groups); |
| 1695 | if (error) { |
| 1696 | devres_free(devres); |
| 1697 | return error; |
| 1698 | } |
| 1699 | |
| 1700 | devres->groups = groups; |
| 1701 | devres_add(dev, devres); |
| 1702 | return 0; |
| 1703 | } |
| 1704 | EXPORT_SYMBOL_GPL(devm_device_add_groups); |
| 1705 | |
| 1706 | /** |
| 1707 | * devm_device_remove_groups - remove a list of managed groups |
| 1708 | * |
| 1709 | * @dev: The device for the groups to be removed from |
| 1710 | * @groups: NULL terminated list of groups to be removed |
| 1711 | * |
| 1712 | * If groups is not NULL, remove the specified groups from the device. |
| 1713 | */ |
| 1714 | void devm_device_remove_groups(struct device *dev, |
| 1715 | const struct attribute_group **groups) |
| 1716 | { |
| 1717 | WARN_ON(devres_release(dev, devm_attr_groups_remove, |
| 1718 | devm_attr_group_match, |
| 1719 | /* cast away const */ (void *)groups)); |
| 1720 | } |
| 1721 | EXPORT_SYMBOL_GPL(devm_device_remove_groups); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1722 | |
Greg Kroah-Hartman | 2620efe | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 1723 | static int device_add_attrs(struct device *dev) |
| 1724 | { |
| 1725 | struct class *class = dev->class; |
Stephen Hemminger | aed65af | 2011-03-28 09:12:52 -0700 | [diff] [blame] | 1726 | const struct device_type *type = dev->type; |
Dmitry Torokhov | 621a167 | 2007-03-10 01:37:34 -0500 | [diff] [blame] | 1727 | int error; |
Greg Kroah-Hartman | 2620efe | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 1728 | |
Dmitry Torokhov | 621a167 | 2007-03-10 01:37:34 -0500 | [diff] [blame] | 1729 | if (class) { |
Greg Kroah-Hartman | d05a6f9 | 2013-07-14 16:05:58 -0700 | [diff] [blame] | 1730 | error = device_add_groups(dev, class->dev_groups); |
Kay Sievers | f9f852d | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 1731 | if (error) |
Dmitry Torokhov | 621a167 | 2007-03-10 01:37:34 -0500 | [diff] [blame] | 1732 | return error; |
Greg Kroah-Hartman | 2620efe | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 1733 | } |
Kay Sievers | f9f852d | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 1734 | |
Dmitry Torokhov | 621a167 | 2007-03-10 01:37:34 -0500 | [diff] [blame] | 1735 | if (type) { |
| 1736 | error = device_add_groups(dev, type->groups); |
Kay Sievers | f9f852d | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 1737 | if (error) |
Greg Kroah-Hartman | a6b01de | 2013-10-05 18:19:30 -0700 | [diff] [blame] | 1738 | goto err_remove_class_groups; |
Kay Sievers | f9f852d | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 1739 | } |
| 1740 | |
Dmitry Torokhov | 621a167 | 2007-03-10 01:37:34 -0500 | [diff] [blame] | 1741 | error = device_add_groups(dev, dev->groups); |
| 1742 | if (error) |
| 1743 | goto err_remove_type_groups; |
| 1744 | |
Rafael J. Wysocki | 4f3549d | 2013-05-02 22:15:29 +0200 | [diff] [blame] | 1745 | if (device_supports_offline(dev) && !dev->offline_disabled) { |
Greg Kroah-Hartman | c5e064a | 2013-08-23 17:07:26 -0700 | [diff] [blame] | 1746 | error = device_create_file(dev, &dev_attr_online); |
Rafael J. Wysocki | 4f3549d | 2013-05-02 22:15:29 +0200 | [diff] [blame] | 1747 | if (error) |
Rafael J. Wysocki | ecfbf6f | 2013-12-12 06:11:02 +0100 | [diff] [blame] | 1748 | goto err_remove_dev_groups; |
Rafael J. Wysocki | 4f3549d | 2013-05-02 22:15:29 +0200 | [diff] [blame] | 1749 | } |
| 1750 | |
Dmitry Torokhov | 621a167 | 2007-03-10 01:37:34 -0500 | [diff] [blame] | 1751 | return 0; |
| 1752 | |
Rafael J. Wysocki | ecfbf6f | 2013-12-12 06:11:02 +0100 | [diff] [blame] | 1753 | err_remove_dev_groups: |
| 1754 | device_remove_groups(dev, dev->groups); |
Dmitry Torokhov | 621a167 | 2007-03-10 01:37:34 -0500 | [diff] [blame] | 1755 | err_remove_type_groups: |
| 1756 | if (type) |
| 1757 | device_remove_groups(dev, type->groups); |
Greg Kroah-Hartman | d05a6f9 | 2013-07-14 16:05:58 -0700 | [diff] [blame] | 1758 | err_remove_class_groups: |
| 1759 | if (class) |
| 1760 | device_remove_groups(dev, class->dev_groups); |
Dmitry Torokhov | 621a167 | 2007-03-10 01:37:34 -0500 | [diff] [blame] | 1761 | |
Greg Kroah-Hartman | 2620efe | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 1762 | return error; |
| 1763 | } |
| 1764 | |
| 1765 | static void device_remove_attrs(struct device *dev) |
| 1766 | { |
| 1767 | struct class *class = dev->class; |
Stephen Hemminger | aed65af | 2011-03-28 09:12:52 -0700 | [diff] [blame] | 1768 | const struct device_type *type = dev->type; |
Greg Kroah-Hartman | 2620efe | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 1769 | |
Greg Kroah-Hartman | c5e064a | 2013-08-23 17:07:26 -0700 | [diff] [blame] | 1770 | device_remove_file(dev, &dev_attr_online); |
Dmitry Torokhov | 621a167 | 2007-03-10 01:37:34 -0500 | [diff] [blame] | 1771 | device_remove_groups(dev, dev->groups); |
Kay Sievers | f9f852d | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 1772 | |
Dmitry Torokhov | 621a167 | 2007-03-10 01:37:34 -0500 | [diff] [blame] | 1773 | if (type) |
| 1774 | device_remove_groups(dev, type->groups); |
| 1775 | |
Greg Kroah-Hartman | a6b01de | 2013-10-05 18:19:30 -0700 | [diff] [blame] | 1776 | if (class) |
Greg Kroah-Hartman | d05a6f9 | 2013-07-14 16:05:58 -0700 | [diff] [blame] | 1777 | device_remove_groups(dev, class->dev_groups); |
Greg Kroah-Hartman | 2620efe | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 1778 | } |
| 1779 | |
Greg Kroah-Hartman | c5e064a | 2013-08-23 17:07:26 -0700 | [diff] [blame] | 1780 | static ssize_t dev_show(struct device *dev, struct device_attribute *attr, |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 1781 | char *buf) |
| 1782 | { |
| 1783 | return print_dev_t(buf, dev->devt); |
| 1784 | } |
Greg Kroah-Hartman | c5e064a | 2013-08-23 17:07:26 -0700 | [diff] [blame] | 1785 | static DEVICE_ATTR_RO(dev); |
Tejun Heo | ad6a1e1 | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 1786 | |
Kay Sievers | ca22e56 | 2011-12-14 14:29:38 -0800 | [diff] [blame] | 1787 | /* /sys/devices/ */ |
Greg Kroah-Hartman | 881c6cfd | 2007-11-01 09:29:06 -0600 | [diff] [blame] | 1788 | struct kset *devices_kset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1789 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1790 | /** |
Grygorii Strashko | 52cdbdd | 2015-07-27 20:43:01 +0300 | [diff] [blame] | 1791 | * devices_kset_move_before - Move device in the devices_kset's list. |
| 1792 | * @deva: Device to move. |
| 1793 | * @devb: Device @deva should come before. |
| 1794 | */ |
| 1795 | static void devices_kset_move_before(struct device *deva, struct device *devb) |
| 1796 | { |
| 1797 | if (!devices_kset) |
| 1798 | return; |
| 1799 | pr_debug("devices_kset: Moving %s before %s\n", |
| 1800 | dev_name(deva), dev_name(devb)); |
| 1801 | spin_lock(&devices_kset->list_lock); |
| 1802 | list_move_tail(&deva->kobj.entry, &devb->kobj.entry); |
| 1803 | spin_unlock(&devices_kset->list_lock); |
| 1804 | } |
| 1805 | |
| 1806 | /** |
| 1807 | * devices_kset_move_after - Move device in the devices_kset's list. |
| 1808 | * @deva: Device to move |
| 1809 | * @devb: Device @deva should come after. |
| 1810 | */ |
| 1811 | static void devices_kset_move_after(struct device *deva, struct device *devb) |
| 1812 | { |
| 1813 | if (!devices_kset) |
| 1814 | return; |
| 1815 | pr_debug("devices_kset: Moving %s after %s\n", |
| 1816 | dev_name(deva), dev_name(devb)); |
| 1817 | spin_lock(&devices_kset->list_lock); |
| 1818 | list_move(&deva->kobj.entry, &devb->kobj.entry); |
| 1819 | spin_unlock(&devices_kset->list_lock); |
| 1820 | } |
| 1821 | |
| 1822 | /** |
| 1823 | * devices_kset_move_last - move the device to the end of devices_kset's list. |
| 1824 | * @dev: device to move |
| 1825 | */ |
| 1826 | void devices_kset_move_last(struct device *dev) |
| 1827 | { |
| 1828 | if (!devices_kset) |
| 1829 | return; |
| 1830 | pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev)); |
| 1831 | spin_lock(&devices_kset->list_lock); |
| 1832 | list_move_tail(&dev->kobj.entry, &devices_kset->list); |
| 1833 | spin_unlock(&devices_kset->list_lock); |
| 1834 | } |
| 1835 | |
| 1836 | /** |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 1837 | * device_create_file - create sysfs attribute file for device. |
| 1838 | * @dev: device. |
| 1839 | * @attr: device attribute descriptor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1840 | */ |
Phil Carmody | 26579ab | 2009-12-18 15:34:19 +0200 | [diff] [blame] | 1841 | int device_create_file(struct device *dev, |
| 1842 | const struct device_attribute *attr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1843 | { |
| 1844 | int error = 0; |
Felipe Balbi | 8f46baa | 2013-02-20 10:31:42 +0200 | [diff] [blame] | 1845 | |
| 1846 | if (dev) { |
| 1847 | WARN(((attr->attr.mode & S_IWUGO) && !attr->store), |
dyoung@redhat.com | 9752197 | 2013-05-16 14:31:30 +0800 | [diff] [blame] | 1848 | "Attribute %s: write permission without 'store'\n", |
| 1849 | attr->attr.name); |
Felipe Balbi | 8f46baa | 2013-02-20 10:31:42 +0200 | [diff] [blame] | 1850 | WARN(((attr->attr.mode & S_IRUGO) && !attr->show), |
dyoung@redhat.com | 9752197 | 2013-05-16 14:31:30 +0800 | [diff] [blame] | 1851 | "Attribute %s: read permission without 'show'\n", |
| 1852 | attr->attr.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1853 | error = sysfs_create_file(&dev->kobj, &attr->attr); |
Felipe Balbi | 8f46baa | 2013-02-20 10:31:42 +0200 | [diff] [blame] | 1854 | } |
| 1855 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1856 | return error; |
| 1857 | } |
David Graham White | 86df268 | 2013-07-21 20:41:14 -0400 | [diff] [blame] | 1858 | EXPORT_SYMBOL_GPL(device_create_file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1859 | |
| 1860 | /** |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 1861 | * device_remove_file - remove sysfs attribute file. |
| 1862 | * @dev: device. |
| 1863 | * @attr: device attribute descriptor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1864 | */ |
Phil Carmody | 26579ab | 2009-12-18 15:34:19 +0200 | [diff] [blame] | 1865 | void device_remove_file(struct device *dev, |
| 1866 | const struct device_attribute *attr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1867 | { |
Cornelia Huck | 0c98b19 | 2008-01-31 10:39:38 +0100 | [diff] [blame] | 1868 | if (dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1869 | sysfs_remove_file(&dev->kobj, &attr->attr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1870 | } |
David Graham White | 86df268 | 2013-07-21 20:41:14 -0400 | [diff] [blame] | 1871 | EXPORT_SYMBOL_GPL(device_remove_file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1872 | |
Greg Kroah-Hartman | 2589f188 | 2006-09-19 09:39:19 -0700 | [diff] [blame] | 1873 | /** |
Tejun Heo | 6b0afc2 | 2014-02-03 14:03:01 -0500 | [diff] [blame] | 1874 | * device_remove_file_self - remove sysfs attribute file from its own method. |
| 1875 | * @dev: device. |
| 1876 | * @attr: device attribute descriptor. |
| 1877 | * |
| 1878 | * See kernfs_remove_self() for details. |
| 1879 | */ |
| 1880 | bool device_remove_file_self(struct device *dev, |
| 1881 | const struct device_attribute *attr) |
| 1882 | { |
| 1883 | if (dev) |
| 1884 | return sysfs_remove_file_self(&dev->kobj, &attr->attr); |
| 1885 | else |
| 1886 | return false; |
| 1887 | } |
| 1888 | EXPORT_SYMBOL_GPL(device_remove_file_self); |
| 1889 | |
| 1890 | /** |
Greg Kroah-Hartman | 2589f188 | 2006-09-19 09:39:19 -0700 | [diff] [blame] | 1891 | * device_create_bin_file - create sysfs binary attribute file for device. |
| 1892 | * @dev: device. |
| 1893 | * @attr: device binary attribute descriptor. |
| 1894 | */ |
Phil Carmody | 66ecb92 | 2009-12-18 15:34:20 +0200 | [diff] [blame] | 1895 | int device_create_bin_file(struct device *dev, |
| 1896 | const struct bin_attribute *attr) |
Greg Kroah-Hartman | 2589f188 | 2006-09-19 09:39:19 -0700 | [diff] [blame] | 1897 | { |
| 1898 | int error = -EINVAL; |
| 1899 | if (dev) |
| 1900 | error = sysfs_create_bin_file(&dev->kobj, attr); |
| 1901 | return error; |
| 1902 | } |
| 1903 | EXPORT_SYMBOL_GPL(device_create_bin_file); |
| 1904 | |
| 1905 | /** |
| 1906 | * device_remove_bin_file - remove sysfs binary attribute file |
| 1907 | * @dev: device. |
| 1908 | * @attr: device binary attribute descriptor. |
| 1909 | */ |
Phil Carmody | 66ecb92 | 2009-12-18 15:34:20 +0200 | [diff] [blame] | 1910 | void device_remove_bin_file(struct device *dev, |
| 1911 | const struct bin_attribute *attr) |
Greg Kroah-Hartman | 2589f188 | 2006-09-19 09:39:19 -0700 | [diff] [blame] | 1912 | { |
| 1913 | if (dev) |
| 1914 | sysfs_remove_bin_file(&dev->kobj, attr); |
| 1915 | } |
| 1916 | EXPORT_SYMBOL_GPL(device_remove_bin_file); |
| 1917 | |
James Bottomley | 34bb61f | 2005-09-06 16:56:51 -0700 | [diff] [blame] | 1918 | static void klist_children_get(struct klist_node *n) |
| 1919 | { |
Greg Kroah-Hartman | f791b8c | 2008-12-16 12:24:56 -0800 | [diff] [blame] | 1920 | struct device_private *p = to_device_private_parent(n); |
| 1921 | struct device *dev = p->device; |
James Bottomley | 34bb61f | 2005-09-06 16:56:51 -0700 | [diff] [blame] | 1922 | |
| 1923 | get_device(dev); |
| 1924 | } |
| 1925 | |
| 1926 | static void klist_children_put(struct klist_node *n) |
| 1927 | { |
Greg Kroah-Hartman | f791b8c | 2008-12-16 12:24:56 -0800 | [diff] [blame] | 1928 | struct device_private *p = to_device_private_parent(n); |
| 1929 | struct device *dev = p->device; |
James Bottomley | 34bb61f | 2005-09-06 16:56:51 -0700 | [diff] [blame] | 1930 | |
| 1931 | put_device(dev); |
| 1932 | } |
| 1933 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1934 | /** |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 1935 | * device_initialize - init device structure. |
| 1936 | * @dev: device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1937 | * |
Cornelia Huck | 5739411 | 2008-09-03 18:26:40 +0200 | [diff] [blame] | 1938 | * This prepares the device for use by other layers by initializing |
| 1939 | * its fields. |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 1940 | * It is the first half of device_register(), if called by |
Cornelia Huck | 5739411 | 2008-09-03 18:26:40 +0200 | [diff] [blame] | 1941 | * that function, though it can also be called separately, so one |
| 1942 | * may use @dev's fields. In particular, get_device()/put_device() |
| 1943 | * may be used for reference counting of @dev after calling this |
| 1944 | * function. |
| 1945 | * |
Alan Stern | b10d5ef | 2012-01-17 11:39:00 -0500 | [diff] [blame] | 1946 | * All fields in @dev must be initialized by the caller to 0, except |
| 1947 | * for those explicitly set to some other value. The simplest |
| 1948 | * approach is to use kzalloc() to allocate the structure containing |
| 1949 | * @dev. |
| 1950 | * |
Cornelia Huck | 5739411 | 2008-09-03 18:26:40 +0200 | [diff] [blame] | 1951 | * NOTE: Use put_device() to give up your reference instead of freeing |
| 1952 | * @dev directly once you have called this function. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1953 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1954 | void device_initialize(struct device *dev) |
| 1955 | { |
Greg Kroah-Hartman | 881c6cfd | 2007-11-01 09:29:06 -0600 | [diff] [blame] | 1956 | dev->kobj.kset = devices_kset; |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1957 | kobject_init(&dev->kobj, &device_ktype); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1958 | INIT_LIST_HEAD(&dev->dma_pools); |
Thomas Gleixner | 3142788 | 2010-01-29 20:39:02 +0000 | [diff] [blame] | 1959 | mutex_init(&dev->mutex); |
Dan Williams | 87a30e1 | 2019-07-17 18:08:26 -0700 | [diff] [blame] | 1960 | #ifdef CONFIG_PROVE_LOCKING |
| 1961 | mutex_init(&dev->lockdep_mutex); |
| 1962 | #endif |
Peter Zijlstra | 1704f47 | 2010-03-19 01:37:42 +0100 | [diff] [blame] | 1963 | lockdep_set_novalidate_class(&dev->mutex); |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 1964 | spin_lock_init(&dev->devres_lock); |
| 1965 | INIT_LIST_HEAD(&dev->devres_head); |
Alan Stern | 3b98aea | 2008-08-07 13:06:12 -0400 | [diff] [blame] | 1966 | device_pm_init(dev); |
Christoph Hellwig | 8734813 | 2006-12-06 20:32:33 -0800 | [diff] [blame] | 1967 | set_dev_node(dev, -1); |
Jiang Liu | 4a7cc83 | 2015-07-09 16:00:44 +0800 | [diff] [blame] | 1968 | #ifdef CONFIG_GENERIC_MSI_IRQ |
| 1969 | INIT_LIST_HEAD(&dev->msi_list); |
| 1970 | #endif |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 1971 | INIT_LIST_HEAD(&dev->links.consumers); |
| 1972 | INIT_LIST_HEAD(&dev->links.suppliers); |
Saravana Kannan | e2ae9bc | 2019-09-04 14:11:21 -0700 | [diff] [blame] | 1973 | INIT_LIST_HEAD(&dev->links.needs_suppliers); |
Saravana Kannan | fc5a251 | 2019-09-04 14:11:23 -0700 | [diff] [blame] | 1974 | INIT_LIST_HEAD(&dev->links.defer_sync); |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 1975 | dev->links.status = DL_DEV_NO_DRIVER; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1976 | } |
David Graham White | 86df268 | 2013-07-21 20:41:14 -0400 | [diff] [blame] | 1977 | EXPORT_SYMBOL_GPL(device_initialize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1978 | |
Tejun Heo | d73ce00 | 2013-03-12 11:30:05 -0700 | [diff] [blame] | 1979 | struct kobject *virtual_device_parent(struct device *dev) |
Greg Kroah-Hartman | f0ee61a | 2006-10-23 10:40:54 -0700 | [diff] [blame] | 1980 | { |
Kay Sievers | 8640624 | 2007-03-14 03:25:56 +0100 | [diff] [blame] | 1981 | static struct kobject *virtual_dir = NULL; |
Greg Kroah-Hartman | f0ee61a | 2006-10-23 10:40:54 -0700 | [diff] [blame] | 1982 | |
Kay Sievers | 8640624 | 2007-03-14 03:25:56 +0100 | [diff] [blame] | 1983 | if (!virtual_dir) |
Greg Kroah-Hartman | 4ff6abf | 2007-11-05 22:24:43 -0800 | [diff] [blame] | 1984 | virtual_dir = kobject_create_and_add("virtual", |
Greg Kroah-Hartman | 881c6cfd | 2007-11-01 09:29:06 -0600 | [diff] [blame] | 1985 | &devices_kset->kobj); |
Greg Kroah-Hartman | f0ee61a | 2006-10-23 10:40:54 -0700 | [diff] [blame] | 1986 | |
Kay Sievers | 8640624 | 2007-03-14 03:25:56 +0100 | [diff] [blame] | 1987 | return virtual_dir; |
Greg Kroah-Hartman | f0ee61a | 2006-10-23 10:40:54 -0700 | [diff] [blame] | 1988 | } |
| 1989 | |
Eric W. Biederman | bc451f2 | 2010-03-30 11:31:25 -0700 | [diff] [blame] | 1990 | struct class_dir { |
| 1991 | struct kobject kobj; |
| 1992 | struct class *class; |
| 1993 | }; |
| 1994 | |
| 1995 | #define to_class_dir(obj) container_of(obj, struct class_dir, kobj) |
| 1996 | |
| 1997 | static void class_dir_release(struct kobject *kobj) |
| 1998 | { |
| 1999 | struct class_dir *dir = to_class_dir(kobj); |
| 2000 | kfree(dir); |
| 2001 | } |
| 2002 | |
| 2003 | static const |
| 2004 | struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj) |
| 2005 | { |
| 2006 | struct class_dir *dir = to_class_dir(kobj); |
| 2007 | return dir->class->ns_type; |
| 2008 | } |
| 2009 | |
| 2010 | static struct kobj_type class_dir_ktype = { |
| 2011 | .release = class_dir_release, |
| 2012 | .sysfs_ops = &kobj_sysfs_ops, |
| 2013 | .child_ns_type = class_dir_child_ns_type |
| 2014 | }; |
| 2015 | |
| 2016 | static struct kobject * |
| 2017 | class_dir_create_and_add(struct class *class, struct kobject *parent_kobj) |
| 2018 | { |
| 2019 | struct class_dir *dir; |
| 2020 | int retval; |
| 2021 | |
| 2022 | dir = kzalloc(sizeof(*dir), GFP_KERNEL); |
| 2023 | if (!dir) |
Tetsuo Handa | 84d0c27 | 2018-05-07 19:10:31 +0900 | [diff] [blame] | 2024 | return ERR_PTR(-ENOMEM); |
Eric W. Biederman | bc451f2 | 2010-03-30 11:31:25 -0700 | [diff] [blame] | 2025 | |
| 2026 | dir->class = class; |
| 2027 | kobject_init(&dir->kobj, &class_dir_ktype); |
| 2028 | |
Kay Sievers | 6b6e39a | 2010-11-15 23:13:18 +0100 | [diff] [blame] | 2029 | dir->kobj.kset = &class->p->glue_dirs; |
Eric W. Biederman | bc451f2 | 2010-03-30 11:31:25 -0700 | [diff] [blame] | 2030 | |
| 2031 | retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name); |
| 2032 | if (retval < 0) { |
| 2033 | kobject_put(&dir->kobj); |
Tetsuo Handa | 84d0c27 | 2018-05-07 19:10:31 +0900 | [diff] [blame] | 2034 | return ERR_PTR(retval); |
Eric W. Biederman | bc451f2 | 2010-03-30 11:31:25 -0700 | [diff] [blame] | 2035 | } |
| 2036 | return &dir->kobj; |
| 2037 | } |
| 2038 | |
Yijing Wang | e4a60d1 | 2014-11-07 12:05:49 +0800 | [diff] [blame] | 2039 | static DEFINE_MUTEX(gdp_mutex); |
Eric W. Biederman | bc451f2 | 2010-03-30 11:31:25 -0700 | [diff] [blame] | 2040 | |
Kay Sievers | da231fd | 2007-11-21 17:29:15 +0100 | [diff] [blame] | 2041 | static struct kobject *get_device_parent(struct device *dev, |
| 2042 | struct device *parent) |
Greg Kroah-Hartman | 40fa542 | 2006-10-24 00:37:58 +0100 | [diff] [blame] | 2043 | { |
Kay Sievers | 8640624 | 2007-03-14 03:25:56 +0100 | [diff] [blame] | 2044 | if (dev->class) { |
| 2045 | struct kobject *kobj = NULL; |
| 2046 | struct kobject *parent_kobj; |
| 2047 | struct kobject *k; |
| 2048 | |
Randy Dunlap | ead454f | 2010-09-24 14:36:49 -0700 | [diff] [blame] | 2049 | #ifdef CONFIG_BLOCK |
Kay Sievers | 39aba96 | 2010-09-04 22:33:14 -0700 | [diff] [blame] | 2050 | /* block disks show up in /sys/block */ |
Andi Kleen | e52eec1 | 2010-09-08 16:54:17 +0200 | [diff] [blame] | 2051 | if (sysfs_deprecated && dev->class == &block_class) { |
Kay Sievers | 39aba96 | 2010-09-04 22:33:14 -0700 | [diff] [blame] | 2052 | if (parent && parent->class == &block_class) |
| 2053 | return &parent->kobj; |
Kay Sievers | 6b6e39a | 2010-11-15 23:13:18 +0100 | [diff] [blame] | 2054 | return &block_class.p->subsys.kobj; |
Kay Sievers | 39aba96 | 2010-09-04 22:33:14 -0700 | [diff] [blame] | 2055 | } |
Randy Dunlap | ead454f | 2010-09-24 14:36:49 -0700 | [diff] [blame] | 2056 | #endif |
Andi Kleen | e52eec1 | 2010-09-08 16:54:17 +0200 | [diff] [blame] | 2057 | |
Kay Sievers | 8640624 | 2007-03-14 03:25:56 +0100 | [diff] [blame] | 2058 | /* |
| 2059 | * If we have no parent, we live in "virtual". |
Kay Sievers | 0f4dafc | 2007-12-19 01:40:42 +0100 | [diff] [blame] | 2060 | * Class-devices with a non class-device as parent, live |
| 2061 | * in a "glue" directory to prevent namespace collisions. |
Kay Sievers | 8640624 | 2007-03-14 03:25:56 +0100 | [diff] [blame] | 2062 | */ |
| 2063 | if (parent == NULL) |
| 2064 | parent_kobj = virtual_device_parent(dev); |
Eric W. Biederman | 24b1442 | 2010-07-24 22:43:35 -0700 | [diff] [blame] | 2065 | else if (parent->class && !dev->class->ns_type) |
Kay Sievers | 8640624 | 2007-03-14 03:25:56 +0100 | [diff] [blame] | 2066 | return &parent->kobj; |
| 2067 | else |
| 2068 | parent_kobj = &parent->kobj; |
| 2069 | |
Tejun Heo | 77d3d7c | 2010-02-05 17:57:02 +0900 | [diff] [blame] | 2070 | mutex_lock(&gdp_mutex); |
| 2071 | |
Kay Sievers | 8640624 | 2007-03-14 03:25:56 +0100 | [diff] [blame] | 2072 | /* find our class-directory at the parent and reference it */ |
Kay Sievers | 6b6e39a | 2010-11-15 23:13:18 +0100 | [diff] [blame] | 2073 | spin_lock(&dev->class->p->glue_dirs.list_lock); |
| 2074 | list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry) |
Kay Sievers | 8640624 | 2007-03-14 03:25:56 +0100 | [diff] [blame] | 2075 | if (k->parent == parent_kobj) { |
| 2076 | kobj = kobject_get(k); |
| 2077 | break; |
| 2078 | } |
Kay Sievers | 6b6e39a | 2010-11-15 23:13:18 +0100 | [diff] [blame] | 2079 | spin_unlock(&dev->class->p->glue_dirs.list_lock); |
Tejun Heo | 77d3d7c | 2010-02-05 17:57:02 +0900 | [diff] [blame] | 2080 | if (kobj) { |
| 2081 | mutex_unlock(&gdp_mutex); |
Kay Sievers | 8640624 | 2007-03-14 03:25:56 +0100 | [diff] [blame] | 2082 | return kobj; |
Tejun Heo | 77d3d7c | 2010-02-05 17:57:02 +0900 | [diff] [blame] | 2083 | } |
Kay Sievers | 8640624 | 2007-03-14 03:25:56 +0100 | [diff] [blame] | 2084 | |
| 2085 | /* or create a new class-directory at the parent device */ |
Eric W. Biederman | bc451f2 | 2010-03-30 11:31:25 -0700 | [diff] [blame] | 2086 | k = class_dir_create_and_add(dev->class, parent_kobj); |
Kay Sievers | 0f4dafc | 2007-12-19 01:40:42 +0100 | [diff] [blame] | 2087 | /* do not emit an uevent for this simple "glue" directory */ |
Tejun Heo | 77d3d7c | 2010-02-05 17:57:02 +0900 | [diff] [blame] | 2088 | mutex_unlock(&gdp_mutex); |
Greg Kroah-Hartman | 43968d2 | 2007-11-05 22:24:43 -0800 | [diff] [blame] | 2089 | return k; |
Kay Sievers | 8640624 | 2007-03-14 03:25:56 +0100 | [diff] [blame] | 2090 | } |
| 2091 | |
Kay Sievers | ca22e56 | 2011-12-14 14:29:38 -0800 | [diff] [blame] | 2092 | /* subsystems can specify a default root directory for their devices */ |
| 2093 | if (!parent && dev->bus && dev->bus->dev_root) |
| 2094 | return &dev->bus->dev_root->kobj; |
| 2095 | |
Kay Sievers | 8640624 | 2007-03-14 03:25:56 +0100 | [diff] [blame] | 2096 | if (parent) |
Cornelia Huck | c744aeae | 2007-01-08 20:16:44 +0100 | [diff] [blame] | 2097 | return &parent->kobj; |
| 2098 | return NULL; |
| 2099 | } |
Kay Sievers | da231fd | 2007-11-21 17:29:15 +0100 | [diff] [blame] | 2100 | |
Ming Lei | cebf8fd | 2016-07-10 19:27:36 +0800 | [diff] [blame] | 2101 | static inline bool live_in_glue_dir(struct kobject *kobj, |
| 2102 | struct device *dev) |
| 2103 | { |
| 2104 | if (!kobj || !dev->class || |
| 2105 | kobj->kset != &dev->class->p->glue_dirs) |
| 2106 | return false; |
| 2107 | return true; |
| 2108 | } |
| 2109 | |
| 2110 | static inline struct kobject *get_glue_dir(struct device *dev) |
| 2111 | { |
| 2112 | return dev->kobj.parent; |
| 2113 | } |
| 2114 | |
| 2115 | /* |
| 2116 | * make sure cleaning up dir as the last step, we need to make |
| 2117 | * sure .release handler of kobject is run with holding the |
| 2118 | * global lock |
| 2119 | */ |
Cornelia Huck | 63b6971 | 2008-01-21 16:09:44 +0100 | [diff] [blame] | 2120 | static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir) |
Kay Sievers | da231fd | 2007-11-21 17:29:15 +0100 | [diff] [blame] | 2121 | { |
Muchun Song | ac43432 | 2019-07-27 11:21:22 +0800 | [diff] [blame] | 2122 | unsigned int ref; |
| 2123 | |
Kay Sievers | 0f4dafc | 2007-12-19 01:40:42 +0100 | [diff] [blame] | 2124 | /* see if we live in a "glue" directory */ |
Ming Lei | cebf8fd | 2016-07-10 19:27:36 +0800 | [diff] [blame] | 2125 | if (!live_in_glue_dir(glue_dir, dev)) |
Kay Sievers | da231fd | 2007-11-21 17:29:15 +0100 | [diff] [blame] | 2126 | return; |
| 2127 | |
Yijing Wang | e4a60d1 | 2014-11-07 12:05:49 +0800 | [diff] [blame] | 2128 | mutex_lock(&gdp_mutex); |
Muchun Song | ac43432 | 2019-07-27 11:21:22 +0800 | [diff] [blame] | 2129 | /** |
| 2130 | * There is a race condition between removing glue directory |
| 2131 | * and adding a new device under the glue directory. |
| 2132 | * |
| 2133 | * CPU1: CPU2: |
| 2134 | * |
| 2135 | * device_add() |
| 2136 | * get_device_parent() |
| 2137 | * class_dir_create_and_add() |
| 2138 | * kobject_add_internal() |
| 2139 | * create_dir() // create glue_dir |
| 2140 | * |
| 2141 | * device_add() |
| 2142 | * get_device_parent() |
| 2143 | * kobject_get() // get glue_dir |
| 2144 | * |
| 2145 | * device_del() |
| 2146 | * cleanup_glue_dir() |
| 2147 | * kobject_del(glue_dir) |
| 2148 | * |
| 2149 | * kobject_add() |
| 2150 | * kobject_add_internal() |
| 2151 | * create_dir() // in glue_dir |
| 2152 | * sysfs_create_dir_ns() |
| 2153 | * kernfs_create_dir_ns(sd) |
| 2154 | * |
| 2155 | * sysfs_remove_dir() // glue_dir->sd=NULL |
| 2156 | * sysfs_put() // free glue_dir->sd |
| 2157 | * |
| 2158 | * // sd is freed |
| 2159 | * kernfs_new_node(sd) |
| 2160 | * kernfs_get(glue_dir) |
| 2161 | * kernfs_add_one() |
| 2162 | * kernfs_put() |
| 2163 | * |
| 2164 | * Before CPU1 remove last child device under glue dir, if CPU2 add |
| 2165 | * a new device under glue dir, the glue_dir kobject reference count |
| 2166 | * will be increase to 2 in kobject_get(k). And CPU2 has been called |
| 2167 | * kernfs_create_dir_ns(). Meanwhile, CPU1 call sysfs_remove_dir() |
| 2168 | * and sysfs_put(). This result in glue_dir->sd is freed. |
| 2169 | * |
| 2170 | * Then the CPU2 will see a stale "empty" but still potentially used |
| 2171 | * glue dir around in kernfs_new_node(). |
| 2172 | * |
| 2173 | * In order to avoid this happening, we also should make sure that |
| 2174 | * kernfs_node for glue_dir is released in CPU1 only when refcount |
| 2175 | * for glue_dir kobj is 1. |
| 2176 | */ |
| 2177 | ref = kref_read(&glue_dir->kref); |
| 2178 | if (!kobject_has_children(glue_dir) && !--ref) |
Benjamin Herrenschmidt | 726e410 | 2018-07-10 10:29:10 +1000 | [diff] [blame] | 2179 | kobject_del(glue_dir); |
Kay Sievers | 0f4dafc | 2007-12-19 01:40:42 +0100 | [diff] [blame] | 2180 | kobject_put(glue_dir); |
Yijing Wang | e4a60d1 | 2014-11-07 12:05:49 +0800 | [diff] [blame] | 2181 | mutex_unlock(&gdp_mutex); |
Kay Sievers | da231fd | 2007-11-21 17:29:15 +0100 | [diff] [blame] | 2182 | } |
Cornelia Huck | 63b6971 | 2008-01-21 16:09:44 +0100 | [diff] [blame] | 2183 | |
Cornelia Huck | 2ee97ca | 2007-07-18 01:43:47 -0700 | [diff] [blame] | 2184 | static int device_add_class_symlinks(struct device *dev) |
| 2185 | { |
Benjamin Herrenschmidt | 5590f31 | 2015-02-18 11:25:18 +1100 | [diff] [blame] | 2186 | struct device_node *of_node = dev_of_node(dev); |
Cornelia Huck | 2ee97ca | 2007-07-18 01:43:47 -0700 | [diff] [blame] | 2187 | int error; |
| 2188 | |
Benjamin Herrenschmidt | 5590f31 | 2015-02-18 11:25:18 +1100 | [diff] [blame] | 2189 | if (of_node) { |
Rob Herring | 0c3c234 | 2017-10-04 14:04:01 -0500 | [diff] [blame] | 2190 | error = sysfs_create_link(&dev->kobj, of_node_kobj(of_node), "of_node"); |
Benjamin Herrenschmidt | 5590f31 | 2015-02-18 11:25:18 +1100 | [diff] [blame] | 2191 | if (error) |
| 2192 | dev_warn(dev, "Error %d creating of_node link\n",error); |
| 2193 | /* An error here doesn't warrant bringing down the device */ |
| 2194 | } |
| 2195 | |
Cornelia Huck | 2ee97ca | 2007-07-18 01:43:47 -0700 | [diff] [blame] | 2196 | if (!dev->class) |
| 2197 | return 0; |
Kay Sievers | da231fd | 2007-11-21 17:29:15 +0100 | [diff] [blame] | 2198 | |
Greg Kroah-Hartman | 1fbfee6 | 2008-05-28 09:28:39 -0700 | [diff] [blame] | 2199 | error = sysfs_create_link(&dev->kobj, |
Kay Sievers | 6b6e39a | 2010-11-15 23:13:18 +0100 | [diff] [blame] | 2200 | &dev->class->p->subsys.kobj, |
Cornelia Huck | 2ee97ca | 2007-07-18 01:43:47 -0700 | [diff] [blame] | 2201 | "subsystem"); |
| 2202 | if (error) |
Benjamin Herrenschmidt | 5590f31 | 2015-02-18 11:25:18 +1100 | [diff] [blame] | 2203 | goto out_devnode; |
Kay Sievers | da231fd | 2007-11-21 17:29:15 +0100 | [diff] [blame] | 2204 | |
Greg Kroah-Hartman | 4e886c2 | 2008-01-27 12:12:43 -0800 | [diff] [blame] | 2205 | if (dev->parent && device_is_not_partition(dev)) { |
Dmitry Torokhov | 4f01a75 | 2007-09-18 22:46:50 -0700 | [diff] [blame] | 2206 | error = sysfs_create_link(&dev->kobj, &dev->parent->kobj, |
| 2207 | "device"); |
| 2208 | if (error) |
Kay Sievers | 39aba96 | 2010-09-04 22:33:14 -0700 | [diff] [blame] | 2209 | goto out_subsys; |
Cornelia Huck | 2ee97ca | 2007-07-18 01:43:47 -0700 | [diff] [blame] | 2210 | } |
Kay Sievers | 39aba96 | 2010-09-04 22:33:14 -0700 | [diff] [blame] | 2211 | |
Randy Dunlap | ead454f | 2010-09-24 14:36:49 -0700 | [diff] [blame] | 2212 | #ifdef CONFIG_BLOCK |
Kay Sievers | 39aba96 | 2010-09-04 22:33:14 -0700 | [diff] [blame] | 2213 | /* /sys/block has directories and does not need symlinks */ |
Andi Kleen | e52eec1 | 2010-09-08 16:54:17 +0200 | [diff] [blame] | 2214 | if (sysfs_deprecated && dev->class == &block_class) |
Kay Sievers | 39aba96 | 2010-09-04 22:33:14 -0700 | [diff] [blame] | 2215 | return 0; |
Randy Dunlap | ead454f | 2010-09-24 14:36:49 -0700 | [diff] [blame] | 2216 | #endif |
Kay Sievers | 39aba96 | 2010-09-04 22:33:14 -0700 | [diff] [blame] | 2217 | |
| 2218 | /* link in the class directory pointing to the device */ |
Kay Sievers | 6b6e39a | 2010-11-15 23:13:18 +0100 | [diff] [blame] | 2219 | error = sysfs_create_link(&dev->class->p->subsys.kobj, |
Kay Sievers | 39aba96 | 2010-09-04 22:33:14 -0700 | [diff] [blame] | 2220 | &dev->kobj, dev_name(dev)); |
| 2221 | if (error) |
| 2222 | goto out_device; |
| 2223 | |
Cornelia Huck | 2ee97ca | 2007-07-18 01:43:47 -0700 | [diff] [blame] | 2224 | return 0; |
| 2225 | |
Kay Sievers | 39aba96 | 2010-09-04 22:33:14 -0700 | [diff] [blame] | 2226 | out_device: |
| 2227 | sysfs_remove_link(&dev->kobj, "device"); |
Kay Sievers | da231fd | 2007-11-21 17:29:15 +0100 | [diff] [blame] | 2228 | |
Cornelia Huck | 2ee97ca | 2007-07-18 01:43:47 -0700 | [diff] [blame] | 2229 | out_subsys: |
| 2230 | sysfs_remove_link(&dev->kobj, "subsystem"); |
Benjamin Herrenschmidt | 5590f31 | 2015-02-18 11:25:18 +1100 | [diff] [blame] | 2231 | out_devnode: |
| 2232 | sysfs_remove_link(&dev->kobj, "of_node"); |
Cornelia Huck | 2ee97ca | 2007-07-18 01:43:47 -0700 | [diff] [blame] | 2233 | return error; |
| 2234 | } |
| 2235 | |
| 2236 | static void device_remove_class_symlinks(struct device *dev) |
| 2237 | { |
Benjamin Herrenschmidt | 5590f31 | 2015-02-18 11:25:18 +1100 | [diff] [blame] | 2238 | if (dev_of_node(dev)) |
| 2239 | sysfs_remove_link(&dev->kobj, "of_node"); |
| 2240 | |
Cornelia Huck | 2ee97ca | 2007-07-18 01:43:47 -0700 | [diff] [blame] | 2241 | if (!dev->class) |
| 2242 | return; |
Kay Sievers | da231fd | 2007-11-21 17:29:15 +0100 | [diff] [blame] | 2243 | |
Greg Kroah-Hartman | 4e886c2 | 2008-01-27 12:12:43 -0800 | [diff] [blame] | 2244 | if (dev->parent && device_is_not_partition(dev)) |
Kay Sievers | da231fd | 2007-11-21 17:29:15 +0100 | [diff] [blame] | 2245 | sysfs_remove_link(&dev->kobj, "device"); |
Cornelia Huck | 2ee97ca | 2007-07-18 01:43:47 -0700 | [diff] [blame] | 2246 | sysfs_remove_link(&dev->kobj, "subsystem"); |
Randy Dunlap | ead454f | 2010-09-24 14:36:49 -0700 | [diff] [blame] | 2247 | #ifdef CONFIG_BLOCK |
Andi Kleen | e52eec1 | 2010-09-08 16:54:17 +0200 | [diff] [blame] | 2248 | if (sysfs_deprecated && dev->class == &block_class) |
Kay Sievers | 39aba96 | 2010-09-04 22:33:14 -0700 | [diff] [blame] | 2249 | return; |
Randy Dunlap | ead454f | 2010-09-24 14:36:49 -0700 | [diff] [blame] | 2250 | #endif |
Kay Sievers | 6b6e39a | 2010-11-15 23:13:18 +0100 | [diff] [blame] | 2251 | sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev)); |
Cornelia Huck | 2ee97ca | 2007-07-18 01:43:47 -0700 | [diff] [blame] | 2252 | } |
| 2253 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2254 | /** |
Stephen Rothwell | 413c239 | 2008-05-30 10:16:40 +1000 | [diff] [blame] | 2255 | * dev_set_name - set a device name |
| 2256 | * @dev: device |
Randy Dunlap | 4623236 | 2008-06-04 21:40:43 -0700 | [diff] [blame] | 2257 | * @fmt: format string for the device's name |
Stephen Rothwell | 413c239 | 2008-05-30 10:16:40 +1000 | [diff] [blame] | 2258 | */ |
| 2259 | int dev_set_name(struct device *dev, const char *fmt, ...) |
| 2260 | { |
| 2261 | va_list vargs; |
Kay Sievers | 1fa5ae8 | 2009-01-25 15:17:37 +0100 | [diff] [blame] | 2262 | int err; |
Stephen Rothwell | 413c239 | 2008-05-30 10:16:40 +1000 | [diff] [blame] | 2263 | |
| 2264 | va_start(vargs, fmt); |
Kay Sievers | 1fa5ae8 | 2009-01-25 15:17:37 +0100 | [diff] [blame] | 2265 | err = kobject_set_name_vargs(&dev->kobj, fmt, vargs); |
Stephen Rothwell | 413c239 | 2008-05-30 10:16:40 +1000 | [diff] [blame] | 2266 | va_end(vargs); |
Kay Sievers | 1fa5ae8 | 2009-01-25 15:17:37 +0100 | [diff] [blame] | 2267 | return err; |
Stephen Rothwell | 413c239 | 2008-05-30 10:16:40 +1000 | [diff] [blame] | 2268 | } |
| 2269 | EXPORT_SYMBOL_GPL(dev_set_name); |
| 2270 | |
| 2271 | /** |
Dan Williams | e105b8b | 2008-04-21 10:51:07 -0700 | [diff] [blame] | 2272 | * device_to_dev_kobj - select a /sys/dev/ directory for the device |
| 2273 | * @dev: device |
| 2274 | * |
| 2275 | * By default we select char/ for new entries. Setting class->dev_obj |
| 2276 | * to NULL prevents an entry from being created. class->dev_kobj must |
| 2277 | * be set (or cleared) before any devices are registered to the class |
| 2278 | * otherwise device_create_sys_dev_entry() and |
Peter Korsgaard | 0d4e293c | 2012-04-17 12:12:57 +0200 | [diff] [blame] | 2279 | * device_remove_sys_dev_entry() will disagree about the presence of |
| 2280 | * the link. |
Dan Williams | e105b8b | 2008-04-21 10:51:07 -0700 | [diff] [blame] | 2281 | */ |
| 2282 | static struct kobject *device_to_dev_kobj(struct device *dev) |
| 2283 | { |
| 2284 | struct kobject *kobj; |
| 2285 | |
| 2286 | if (dev->class) |
| 2287 | kobj = dev->class->dev_kobj; |
| 2288 | else |
| 2289 | kobj = sysfs_dev_char_kobj; |
| 2290 | |
| 2291 | return kobj; |
| 2292 | } |
| 2293 | |
| 2294 | static int device_create_sys_dev_entry(struct device *dev) |
| 2295 | { |
| 2296 | struct kobject *kobj = device_to_dev_kobj(dev); |
| 2297 | int error = 0; |
| 2298 | char devt_str[15]; |
| 2299 | |
| 2300 | if (kobj) { |
| 2301 | format_dev_t(devt_str, dev->devt); |
| 2302 | error = sysfs_create_link(kobj, &dev->kobj, devt_str); |
| 2303 | } |
| 2304 | |
| 2305 | return error; |
| 2306 | } |
| 2307 | |
| 2308 | static void device_remove_sys_dev_entry(struct device *dev) |
| 2309 | { |
| 2310 | struct kobject *kobj = device_to_dev_kobj(dev); |
| 2311 | char devt_str[15]; |
| 2312 | |
| 2313 | if (kobj) { |
| 2314 | format_dev_t(devt_str, dev->devt); |
| 2315 | sysfs_remove_link(kobj, devt_str); |
| 2316 | } |
| 2317 | } |
| 2318 | |
Shaokun Zhang | 46d3a03 | 2018-07-15 18:08:56 +0800 | [diff] [blame] | 2319 | static int device_private_init(struct device *dev) |
Greg Kroah-Hartman | b402843 | 2009-05-11 14:16:57 -0700 | [diff] [blame] | 2320 | { |
| 2321 | dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL); |
| 2322 | if (!dev->p) |
| 2323 | return -ENOMEM; |
| 2324 | dev->p->device = dev; |
| 2325 | klist_init(&dev->p->klist_children, klist_children_get, |
| 2326 | klist_children_put); |
Greg Kroah-Hartman | ef8a3fd | 2012-03-08 12:17:22 -0800 | [diff] [blame] | 2327 | INIT_LIST_HEAD(&dev->p->deferred_probe); |
Greg Kroah-Hartman | b402843 | 2009-05-11 14:16:57 -0700 | [diff] [blame] | 2328 | return 0; |
| 2329 | } |
| 2330 | |
Dan Williams | e105b8b | 2008-04-21 10:51:07 -0700 | [diff] [blame] | 2331 | /** |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 2332 | * device_add - add device to device hierarchy. |
| 2333 | * @dev: device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2334 | * |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 2335 | * This is part 2 of device_register(), though may be called |
| 2336 | * separately _iff_ device_initialize() has been called separately. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2337 | * |
Cornelia Huck | 5739411 | 2008-09-03 18:26:40 +0200 | [diff] [blame] | 2338 | * This adds @dev to the kobject hierarchy via kobject_add(), adds it |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 2339 | * to the global and sibling lists for the device, then |
| 2340 | * adds it to the other relevant subsystems of the driver model. |
Cornelia Huck | 5739411 | 2008-09-03 18:26:40 +0200 | [diff] [blame] | 2341 | * |
Alan Stern | b10d5ef | 2012-01-17 11:39:00 -0500 | [diff] [blame] | 2342 | * Do not call this routine or device_register() more than once for |
| 2343 | * any device structure. The driver model core is not designed to work |
| 2344 | * with devices that get unregistered and then spring back to life. |
| 2345 | * (Among other things, it's very hard to guarantee that all references |
| 2346 | * to the previous incarnation of @dev have been dropped.) Allocate |
| 2347 | * and register a fresh new struct device instead. |
| 2348 | * |
Cornelia Huck | 5739411 | 2008-09-03 18:26:40 +0200 | [diff] [blame] | 2349 | * NOTE: _Never_ directly free @dev after calling this function, even |
| 2350 | * if it returned an error! Always use put_device() to give up your |
| 2351 | * reference instead. |
Borislav Petkov | affada7 | 2019-04-18 19:41:56 +0200 | [diff] [blame] | 2352 | * |
| 2353 | * Rule of thumb is: if device_add() succeeds, you should call |
| 2354 | * device_del() when you want to get rid of it. If device_add() has |
| 2355 | * *not* succeeded, use *only* put_device() to drop the reference |
| 2356 | * count. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2357 | */ |
| 2358 | int device_add(struct device *dev) |
| 2359 | { |
Viresh Kumar | 35dbf4e | 2017-03-17 12:24:22 +0530 | [diff] [blame] | 2360 | struct device *parent; |
Kay Sievers | ca22e56 | 2011-12-14 14:29:38 -0800 | [diff] [blame] | 2361 | struct kobject *kobj; |
Greg Kroah-Hartman | c47ed21 | 2006-09-13 15:34:05 +0200 | [diff] [blame] | 2362 | struct class_interface *class_intf; |
Saravana Kannan | 0332450 | 2019-10-28 15:00:24 -0700 | [diff] [blame] | 2363 | int error = -EINVAL, fw_ret; |
Ming Lei | cebf8fd | 2016-07-10 19:27:36 +0800 | [diff] [blame] | 2364 | struct kobject *glue_dir = NULL; |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 2365 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2366 | dev = get_device(dev); |
Greg Kroah-Hartman | c906a48 | 2008-05-30 10:45:12 -0700 | [diff] [blame] | 2367 | if (!dev) |
| 2368 | goto done; |
| 2369 | |
Greg Kroah-Hartman | fb069a5 | 2008-12-16 12:23:36 -0800 | [diff] [blame] | 2370 | if (!dev->p) { |
Greg Kroah-Hartman | b402843 | 2009-05-11 14:16:57 -0700 | [diff] [blame] | 2371 | error = device_private_init(dev); |
| 2372 | if (error) |
| 2373 | goto done; |
Greg Kroah-Hartman | fb069a5 | 2008-12-16 12:23:36 -0800 | [diff] [blame] | 2374 | } |
Greg Kroah-Hartman | fb069a5 | 2008-12-16 12:23:36 -0800 | [diff] [blame] | 2375 | |
Kay Sievers | 1fa5ae8 | 2009-01-25 15:17:37 +0100 | [diff] [blame] | 2376 | /* |
| 2377 | * for statically allocated devices, which should all be converted |
| 2378 | * some day, we need to initialize the name. We prevent reading back |
| 2379 | * the name, and force the use of dev_name() |
| 2380 | */ |
| 2381 | if (dev->init_name) { |
Greg Kroah-Hartman | acc0e90 | 2009-06-02 15:39:55 -0700 | [diff] [blame] | 2382 | dev_set_name(dev, "%s", dev->init_name); |
Kay Sievers | 1fa5ae8 | 2009-01-25 15:17:37 +0100 | [diff] [blame] | 2383 | dev->init_name = NULL; |
| 2384 | } |
Greg Kroah-Hartman | c906a48 | 2008-05-30 10:45:12 -0700 | [diff] [blame] | 2385 | |
Kay Sievers | ca22e56 | 2011-12-14 14:29:38 -0800 | [diff] [blame] | 2386 | /* subsystems can specify simple device enumeration */ |
| 2387 | if (!dev_name(dev) && dev->bus && dev->bus->dev_name) |
| 2388 | dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id); |
| 2389 | |
Thomas Gleixner | e6309e7 | 2009-12-10 19:32:49 +0000 | [diff] [blame] | 2390 | if (!dev_name(dev)) { |
| 2391 | error = -EINVAL; |
Kay Sievers | 5c8563d | 2009-05-28 14:24:07 -0700 | [diff] [blame] | 2392 | goto name_error; |
Thomas Gleixner | e6309e7 | 2009-12-10 19:32:49 +0000 | [diff] [blame] | 2393 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2394 | |
Kay Sievers | 1e0b2cf | 2008-10-30 01:36:48 +0100 | [diff] [blame] | 2395 | pr_debug("device: '%s': %s\n", dev_name(dev), __func__); |
Greg Kroah-Hartman | c205ef4 | 2006-08-07 22:19:37 -0700 | [diff] [blame] | 2396 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2397 | parent = get_device(dev->parent); |
Kay Sievers | ca22e56 | 2011-12-14 14:29:38 -0800 | [diff] [blame] | 2398 | kobj = get_device_parent(dev, parent); |
Tetsuo Handa | 84d0c27 | 2018-05-07 19:10:31 +0900 | [diff] [blame] | 2399 | if (IS_ERR(kobj)) { |
| 2400 | error = PTR_ERR(kobj); |
| 2401 | goto parent_error; |
| 2402 | } |
Kay Sievers | ca22e56 | 2011-12-14 14:29:38 -0800 | [diff] [blame] | 2403 | if (kobj) |
| 2404 | dev->kobj.parent = kobj; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2405 | |
Yinghai Lu | 0d358f2 | 2008-02-19 03:20:41 -0800 | [diff] [blame] | 2406 | /* use parent numa_node */ |
Zhen Lei | 56f2de8 | 2015-08-25 12:08:22 +0800 | [diff] [blame] | 2407 | if (parent && (dev_to_node(dev) == NUMA_NO_NODE)) |
Yinghai Lu | 0d358f2 | 2008-02-19 03:20:41 -0800 | [diff] [blame] | 2408 | set_dev_node(dev, dev_to_node(parent)); |
| 2409 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2410 | /* first, register with generic layer. */ |
Kay Sievers | 8a577ff | 2009-04-18 15:05:45 -0700 | [diff] [blame] | 2411 | /* we require the name to be set before, and pass NULL */ |
| 2412 | error = kobject_add(&dev->kobj, dev->kobj.parent, NULL); |
Ming Lei | cebf8fd | 2016-07-10 19:27:36 +0800 | [diff] [blame] | 2413 | if (error) { |
| 2414 | glue_dir = get_glue_dir(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2415 | goto Error; |
Ming Lei | cebf8fd | 2016-07-10 19:27:36 +0800 | [diff] [blame] | 2416 | } |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 2417 | |
Brian Walsh | 3702264 | 2006-08-14 22:43:19 -0700 | [diff] [blame] | 2418 | /* notify platform of device entry */ |
Heikki Krogerus | 07de0e8 | 2018-11-09 17:21:34 +0300 | [diff] [blame] | 2419 | error = device_platform_notify(dev, KOBJ_ADD); |
| 2420 | if (error) |
| 2421 | goto platform_error; |
Brian Walsh | 3702264 | 2006-08-14 22:43:19 -0700 | [diff] [blame] | 2422 | |
Greg Kroah-Hartman | c5e064a | 2013-08-23 17:07:26 -0700 | [diff] [blame] | 2423 | error = device_create_file(dev, &dev_attr_uevent); |
Cornelia Huck | a306eea | 2006-09-22 11:37:13 +0200 | [diff] [blame] | 2424 | if (error) |
| 2425 | goto attrError; |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 2426 | |
Cornelia Huck | 2ee97ca | 2007-07-18 01:43:47 -0700 | [diff] [blame] | 2427 | error = device_add_class_symlinks(dev); |
| 2428 | if (error) |
| 2429 | goto SymlinkError; |
Cornelia Huck | dc0afa8 | 2007-07-09 11:39:18 -0700 | [diff] [blame] | 2430 | error = device_add_attrs(dev); |
| 2431 | if (error) |
Greg Kroah-Hartman | 2620efe | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 2432 | goto AttrsError; |
Cornelia Huck | dc0afa8 | 2007-07-09 11:39:18 -0700 | [diff] [blame] | 2433 | error = bus_add_device(dev); |
| 2434 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2435 | goto BusError; |
Alan Stern | 3b98aea | 2008-08-07 13:06:12 -0400 | [diff] [blame] | 2436 | error = dpm_sysfs_add(dev); |
Rafael J. Wysocki | 57eee3d | 2008-03-12 00:59:38 +0100 | [diff] [blame] | 2437 | if (error) |
Alan Stern | 3b98aea | 2008-08-07 13:06:12 -0400 | [diff] [blame] | 2438 | goto DPMError; |
| 2439 | device_pm_add(dev); |
Alan Stern | ec0676ee | 2008-12-05 14:10:31 -0500 | [diff] [blame] | 2440 | |
Sergey Klyaus | 0cd7504 | 2014-10-08 11:31:54 +0400 | [diff] [blame] | 2441 | if (MAJOR(dev->devt)) { |
| 2442 | error = device_create_file(dev, &dev_attr_dev); |
| 2443 | if (error) |
| 2444 | goto DevAttrError; |
| 2445 | |
| 2446 | error = device_create_sys_dev_entry(dev); |
| 2447 | if (error) |
| 2448 | goto SysEntryError; |
| 2449 | |
| 2450 | devtmpfs_create_node(dev); |
| 2451 | } |
| 2452 | |
Alan Stern | ec0676ee | 2008-12-05 14:10:31 -0500 | [diff] [blame] | 2453 | /* Notify clients of device addition. This call must come |
majianpeng | 268863f | 2012-01-11 15:12:06 +0000 | [diff] [blame] | 2454 | * after dpm_sysfs_add() and before kobject_uevent(). |
Alan Stern | ec0676ee | 2008-12-05 14:10:31 -0500 | [diff] [blame] | 2455 | */ |
| 2456 | if (dev->bus) |
| 2457 | blocking_notifier_call_chain(&dev->bus->p->bus_notifier, |
| 2458 | BUS_NOTIFY_ADD_DEVICE, dev); |
| 2459 | |
Cornelia Huck | 83b5fb4c | 2007-03-29 11:12:11 +0200 | [diff] [blame] | 2460 | kobject_uevent(&dev->kobj, KOBJ_ADD); |
Saravana Kannan | 372a67c | 2019-09-04 14:11:20 -0700 | [diff] [blame] | 2461 | |
| 2462 | if (dev->fwnode && !dev->fwnode->dev) |
| 2463 | dev->fwnode->dev = dev; |
| 2464 | |
Saravana Kannan | e2ae9bc | 2019-09-04 14:11:21 -0700 | [diff] [blame] | 2465 | /* |
| 2466 | * Check if any of the other devices (consumers) have been waiting for |
| 2467 | * this device (supplier) to be added so that they can create a device |
| 2468 | * link to it. |
| 2469 | * |
| 2470 | * This needs to happen after device_pm_add() because device_link_add() |
| 2471 | * requires the supplier be registered before it's called. |
| 2472 | * |
| 2473 | * But this also needs to happe before bus_probe_device() to make sure |
| 2474 | * waiting consumers can link to it before the driver is bound to the |
| 2475 | * device and the driver sync_state callback is called for this device. |
| 2476 | */ |
| 2477 | device_link_add_missing_supplier_links(); |
| 2478 | |
Saravana Kannan | 0332450 | 2019-10-28 15:00:24 -0700 | [diff] [blame] | 2479 | if (fwnode_has_op(dev->fwnode, add_links)) { |
| 2480 | fw_ret = fwnode_call_int_op(dev->fwnode, add_links, dev); |
| 2481 | if (fw_ret == -ENODEV) |
| 2482 | device_link_wait_for_mandatory_supplier(dev); |
| 2483 | else if (fw_ret) |
| 2484 | device_link_wait_for_optional_supplier(dev); |
| 2485 | } |
Saravana Kannan | e2ae9bc | 2019-09-04 14:11:21 -0700 | [diff] [blame] | 2486 | |
Alan Stern | 2023c61 | 2009-07-30 15:27:18 -0400 | [diff] [blame] | 2487 | bus_probe_device(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2488 | if (parent) |
Greg Kroah-Hartman | f791b8c | 2008-12-16 12:24:56 -0800 | [diff] [blame] | 2489 | klist_add_tail(&dev->p->knode_parent, |
| 2490 | &parent->p->klist_children); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2491 | |
Greg Kroah-Hartman | 5d9fd16 | 2006-06-22 17:17:32 -0700 | [diff] [blame] | 2492 | if (dev->class) { |
Kay Sievers | ca22e56 | 2011-12-14 14:29:38 -0800 | [diff] [blame] | 2493 | mutex_lock(&dev->class->p->mutex); |
Greg Kroah-Hartman | c47ed21 | 2006-09-13 15:34:05 +0200 | [diff] [blame] | 2494 | /* tie the class to the device */ |
Wei Yang | 570d020 | 2019-01-18 10:34:59 +0800 | [diff] [blame] | 2495 | klist_add_tail(&dev->p->knode_class, |
Kay Sievers | 6b6e39a | 2010-11-15 23:13:18 +0100 | [diff] [blame] | 2496 | &dev->class->p->klist_devices); |
Greg Kroah-Hartman | c47ed21 | 2006-09-13 15:34:05 +0200 | [diff] [blame] | 2497 | |
| 2498 | /* notify any interfaces that the device is here */ |
Greg Kroah-Hartman | 184f1f7 | 2008-05-28 09:28:39 -0700 | [diff] [blame] | 2499 | list_for_each_entry(class_intf, |
Kay Sievers | ca22e56 | 2011-12-14 14:29:38 -0800 | [diff] [blame] | 2500 | &dev->class->p->interfaces, node) |
Greg Kroah-Hartman | c47ed21 | 2006-09-13 15:34:05 +0200 | [diff] [blame] | 2501 | if (class_intf->add_dev) |
| 2502 | class_intf->add_dev(dev, class_intf); |
Kay Sievers | ca22e56 | 2011-12-14 14:29:38 -0800 | [diff] [blame] | 2503 | mutex_unlock(&dev->class->p->mutex); |
Greg Kroah-Hartman | 5d9fd16 | 2006-06-22 17:17:32 -0700 | [diff] [blame] | 2504 | } |
Greg Kroah-Hartman | c906a48 | 2008-05-30 10:45:12 -0700 | [diff] [blame] | 2505 | done: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2506 | put_device(dev); |
| 2507 | return error; |
Sergey Klyaus | 0cd7504 | 2014-10-08 11:31:54 +0400 | [diff] [blame] | 2508 | SysEntryError: |
| 2509 | if (MAJOR(dev->devt)) |
| 2510 | device_remove_file(dev, &dev_attr_dev); |
| 2511 | DevAttrError: |
| 2512 | device_pm_remove(dev); |
| 2513 | dpm_sysfs_remove(dev); |
Alan Stern | 3b98aea | 2008-08-07 13:06:12 -0400 | [diff] [blame] | 2514 | DPMError: |
Rafael J. Wysocki | 57eee3d | 2008-03-12 00:59:38 +0100 | [diff] [blame] | 2515 | bus_remove_device(dev); |
| 2516 | BusError: |
James Simmons | 82f0cf9 | 2007-02-21 17:44:51 +0000 | [diff] [blame] | 2517 | device_remove_attrs(dev); |
Greg Kroah-Hartman | 2620efe | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 2518 | AttrsError: |
Cornelia Huck | 2ee97ca | 2007-07-18 01:43:47 -0700 | [diff] [blame] | 2519 | device_remove_class_symlinks(dev); |
| 2520 | SymlinkError: |
Greg Kroah-Hartman | c5e064a | 2013-08-23 17:07:26 -0700 | [diff] [blame] | 2521 | device_remove_file(dev, &dev_attr_uevent); |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 2522 | attrError: |
Heikki Krogerus | 07de0e8 | 2018-11-09 17:21:34 +0300 | [diff] [blame] | 2523 | device_platform_notify(dev, KOBJ_REMOVE); |
| 2524 | platform_error: |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 2525 | kobject_uevent(&dev->kobj, KOBJ_REMOVE); |
Ming Lei | cebf8fd | 2016-07-10 19:27:36 +0800 | [diff] [blame] | 2526 | glue_dir = get_glue_dir(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2527 | kobject_del(&dev->kobj); |
| 2528 | Error: |
Ming Lei | cebf8fd | 2016-07-10 19:27:36 +0800 | [diff] [blame] | 2529 | cleanup_glue_dir(dev, glue_dir); |
Tetsuo Handa | 84d0c27 | 2018-05-07 19:10:31 +0900 | [diff] [blame] | 2530 | parent_error: |
Markus Elfring | 5f0163a | 2015-02-05 11:48:26 +0100 | [diff] [blame] | 2531 | put_device(parent); |
Kay Sievers | 5c8563d | 2009-05-28 14:24:07 -0700 | [diff] [blame] | 2532 | name_error: |
| 2533 | kfree(dev->p); |
| 2534 | dev->p = NULL; |
Greg Kroah-Hartman | c906a48 | 2008-05-30 10:45:12 -0700 | [diff] [blame] | 2535 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2536 | } |
David Graham White | 86df268 | 2013-07-21 20:41:14 -0400 | [diff] [blame] | 2537 | EXPORT_SYMBOL_GPL(device_add); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2538 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2539 | /** |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 2540 | * device_register - register a device with the system. |
| 2541 | * @dev: pointer to the device structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2542 | * |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 2543 | * This happens in two clean steps - initialize the device |
| 2544 | * and add it to the system. The two steps can be called |
| 2545 | * separately, but this is the easiest and most common. |
| 2546 | * I.e. you should only call the two helpers separately if |
| 2547 | * have a clearly defined need to use and refcount the device |
| 2548 | * before it is added to the hierarchy. |
Cornelia Huck | 5739411 | 2008-09-03 18:26:40 +0200 | [diff] [blame] | 2549 | * |
Alan Stern | b10d5ef | 2012-01-17 11:39:00 -0500 | [diff] [blame] | 2550 | * For more information, see the kerneldoc for device_initialize() |
| 2551 | * and device_add(). |
| 2552 | * |
Cornelia Huck | 5739411 | 2008-09-03 18:26:40 +0200 | [diff] [blame] | 2553 | * NOTE: _Never_ directly free @dev after calling this function, even |
| 2554 | * if it returned an error! Always use put_device() to give up the |
| 2555 | * reference initialized in this function instead. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2556 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2557 | int device_register(struct device *dev) |
| 2558 | { |
| 2559 | device_initialize(dev); |
| 2560 | return device_add(dev); |
| 2561 | } |
David Graham White | 86df268 | 2013-07-21 20:41:14 -0400 | [diff] [blame] | 2562 | EXPORT_SYMBOL_GPL(device_register); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2563 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2564 | /** |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 2565 | * get_device - increment reference count for device. |
| 2566 | * @dev: device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2567 | * |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 2568 | * This simply forwards the call to kobject_get(), though |
| 2569 | * we do take care to provide for the case that we get a NULL |
| 2570 | * pointer passed in. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2571 | */ |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 2572 | struct device *get_device(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2573 | { |
Lars-Peter Clausen | b0d1f80 | 2012-07-03 18:49:36 +0200 | [diff] [blame] | 2574 | return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2575 | } |
David Graham White | 86df268 | 2013-07-21 20:41:14 -0400 | [diff] [blame] | 2576 | EXPORT_SYMBOL_GPL(get_device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2577 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2578 | /** |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 2579 | * put_device - decrement reference count. |
| 2580 | * @dev: device in question. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2581 | */ |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 2582 | void put_device(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2583 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 2584 | /* might_sleep(); */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2585 | if (dev) |
| 2586 | kobject_put(&dev->kobj); |
| 2587 | } |
David Graham White | 86df268 | 2013-07-21 20:41:14 -0400 | [diff] [blame] | 2588 | EXPORT_SYMBOL_GPL(put_device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2589 | |
Dan Williams | 00289cd | 2019-07-17 18:07:53 -0700 | [diff] [blame] | 2590 | bool kill_device(struct device *dev) |
| 2591 | { |
| 2592 | /* |
| 2593 | * Require the device lock and set the "dead" flag to guarantee that |
| 2594 | * the update behavior is consistent with the other bitfields near |
| 2595 | * it and that we cannot have an asynchronous probe routine trying |
| 2596 | * to run while we are tearing out the bus/class/sysfs from |
| 2597 | * underneath the device. |
| 2598 | */ |
| 2599 | lockdep_assert_held(&dev->mutex); |
| 2600 | |
| 2601 | if (dev->p->dead) |
| 2602 | return false; |
| 2603 | dev->p->dead = true; |
| 2604 | return true; |
| 2605 | } |
| 2606 | EXPORT_SYMBOL_GPL(kill_device); |
| 2607 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2608 | /** |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 2609 | * device_del - delete device from system. |
| 2610 | * @dev: device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2611 | * |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 2612 | * This is the first part of the device unregistration |
| 2613 | * sequence. This removes the device from the lists we control |
| 2614 | * from here, has it removed from the other driver model |
| 2615 | * subsystems it was added to in device_add(), and removes it |
| 2616 | * from the kobject hierarchy. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2617 | * |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 2618 | * NOTE: this should be called manually _iff_ device_add() was |
| 2619 | * also called manually. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2620 | */ |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 2621 | void device_del(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2622 | { |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 2623 | struct device *parent = dev->parent; |
Ming Lei | cebf8fd | 2016-07-10 19:27:36 +0800 | [diff] [blame] | 2624 | struct kobject *glue_dir = NULL; |
Greg Kroah-Hartman | c47ed21 | 2006-09-13 15:34:05 +0200 | [diff] [blame] | 2625 | struct class_interface *class_intf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2626 | |
Alexander Duyck | 3451a49 | 2019-01-22 10:39:10 -0800 | [diff] [blame] | 2627 | device_lock(dev); |
Dan Williams | 00289cd | 2019-07-17 18:07:53 -0700 | [diff] [blame] | 2628 | kill_device(dev); |
Alexander Duyck | 3451a49 | 2019-01-22 10:39:10 -0800 | [diff] [blame] | 2629 | device_unlock(dev); |
| 2630 | |
Saravana Kannan | 372a67c | 2019-09-04 14:11:20 -0700 | [diff] [blame] | 2631 | if (dev->fwnode && dev->fwnode->dev == dev) |
| 2632 | dev->fwnode->dev = NULL; |
| 2633 | |
Alan Stern | ec0676ee | 2008-12-05 14:10:31 -0500 | [diff] [blame] | 2634 | /* Notify clients of device removal. This call must come |
| 2635 | * before dpm_sysfs_remove(). |
| 2636 | */ |
| 2637 | if (dev->bus) |
| 2638 | blocking_notifier_call_chain(&dev->bus->p->bus_notifier, |
| 2639 | BUS_NOTIFY_DEL_DEVICE, dev); |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 2640 | |
Alan Stern | 3b98aea | 2008-08-07 13:06:12 -0400 | [diff] [blame] | 2641 | dpm_sysfs_remove(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2642 | if (parent) |
Greg Kroah-Hartman | f791b8c | 2008-12-16 12:24:56 -0800 | [diff] [blame] | 2643 | klist_del(&dev->p->knode_parent); |
Dan Williams | e105b8b | 2008-04-21 10:51:07 -0700 | [diff] [blame] | 2644 | if (MAJOR(dev->devt)) { |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 2645 | devtmpfs_delete_node(dev); |
Dan Williams | e105b8b | 2008-04-21 10:51:07 -0700 | [diff] [blame] | 2646 | device_remove_sys_dev_entry(dev); |
Greg Kroah-Hartman | c5e064a | 2013-08-23 17:07:26 -0700 | [diff] [blame] | 2647 | device_remove_file(dev, &dev_attr_dev); |
Dan Williams | e105b8b | 2008-04-21 10:51:07 -0700 | [diff] [blame] | 2648 | } |
Kay Sievers | b9d9c82 | 2006-06-15 15:31:56 +0200 | [diff] [blame] | 2649 | if (dev->class) { |
Kay Sievers | da231fd | 2007-11-21 17:29:15 +0100 | [diff] [blame] | 2650 | device_remove_class_symlinks(dev); |
Kay Sievers | 99ef3ef | 2006-09-14 11:23:28 +0200 | [diff] [blame] | 2651 | |
Kay Sievers | ca22e56 | 2011-12-14 14:29:38 -0800 | [diff] [blame] | 2652 | mutex_lock(&dev->class->p->mutex); |
Greg Kroah-Hartman | c47ed21 | 2006-09-13 15:34:05 +0200 | [diff] [blame] | 2653 | /* notify any interfaces that the device is now gone */ |
Greg Kroah-Hartman | 184f1f7 | 2008-05-28 09:28:39 -0700 | [diff] [blame] | 2654 | list_for_each_entry(class_intf, |
Kay Sievers | ca22e56 | 2011-12-14 14:29:38 -0800 | [diff] [blame] | 2655 | &dev->class->p->interfaces, node) |
Greg Kroah-Hartman | c47ed21 | 2006-09-13 15:34:05 +0200 | [diff] [blame] | 2656 | if (class_intf->remove_dev) |
| 2657 | class_intf->remove_dev(dev, class_intf); |
| 2658 | /* remove the device from the class list */ |
Wei Yang | 570d020 | 2019-01-18 10:34:59 +0800 | [diff] [blame] | 2659 | klist_del(&dev->p->knode_class); |
Kay Sievers | ca22e56 | 2011-12-14 14:29:38 -0800 | [diff] [blame] | 2660 | mutex_unlock(&dev->class->p->mutex); |
Kay Sievers | b9d9c82 | 2006-06-15 15:31:56 +0200 | [diff] [blame] | 2661 | } |
Greg Kroah-Hartman | c5e064a | 2013-08-23 17:07:26 -0700 | [diff] [blame] | 2662 | device_remove_file(dev, &dev_attr_uevent); |
Greg Kroah-Hartman | 2620efe | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 2663 | device_remove_attrs(dev); |
Benjamin Herrenschmidt | 2895353 | 2006-11-08 19:46:14 -0800 | [diff] [blame] | 2664 | bus_remove_device(dev); |
LongX Zhang | 4b6d1f12 | 2012-10-25 00:21:28 +0200 | [diff] [blame] | 2665 | device_pm_remove(dev); |
Grant Likely | d1c3414 | 2012-03-05 08:47:41 -0700 | [diff] [blame] | 2666 | driver_deferred_probe_del(dev); |
Heikki Krogerus | 07de0e8 | 2018-11-09 17:21:34 +0300 | [diff] [blame] | 2667 | device_platform_notify(dev, KOBJ_REMOVE); |
Lukas Wunner | 478573c | 2016-07-28 02:25:41 +0200 | [diff] [blame] | 2668 | device_remove_properties(dev); |
Jeffy Chen | 2ec1615 | 2017-10-20 20:01:01 +0800 | [diff] [blame] | 2669 | device_links_purge(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2670 | |
Joerg Roedel | 599bad3 | 2014-09-30 13:02:02 +0200 | [diff] [blame] | 2671 | if (dev->bus) |
| 2672 | blocking_notifier_call_chain(&dev->bus->p->bus_notifier, |
| 2673 | BUS_NOTIFY_REMOVED_DEVICE, dev); |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 2674 | kobject_uevent(&dev->kobj, KOBJ_REMOVE); |
Ming Lei | cebf8fd | 2016-07-10 19:27:36 +0800 | [diff] [blame] | 2675 | glue_dir = get_glue_dir(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2676 | kobject_del(&dev->kobj); |
Ming Lei | cebf8fd | 2016-07-10 19:27:36 +0800 | [diff] [blame] | 2677 | cleanup_glue_dir(dev, glue_dir); |
Kay Sievers | da231fd | 2007-11-21 17:29:15 +0100 | [diff] [blame] | 2678 | put_device(parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2679 | } |
David Graham White | 86df268 | 2013-07-21 20:41:14 -0400 | [diff] [blame] | 2680 | EXPORT_SYMBOL_GPL(device_del); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2681 | |
| 2682 | /** |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 2683 | * device_unregister - unregister device from system. |
| 2684 | * @dev: device going away. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2685 | * |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 2686 | * We do this in two parts, like we do device_register(). First, |
| 2687 | * we remove it from all the subsystems with device_del(), then |
| 2688 | * we decrement the reference count via put_device(). If that |
| 2689 | * is the final reference count, the device will be cleaned up |
| 2690 | * via device_release() above. Otherwise, the structure will |
| 2691 | * stick around until the final reference to the device is dropped. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2692 | */ |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 2693 | void device_unregister(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2694 | { |
Kay Sievers | 1e0b2cf | 2008-10-30 01:36:48 +0100 | [diff] [blame] | 2695 | pr_debug("device: '%s': %s\n", dev_name(dev), __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2696 | device_del(dev); |
| 2697 | put_device(dev); |
| 2698 | } |
David Graham White | 86df268 | 2013-07-21 20:41:14 -0400 | [diff] [blame] | 2699 | EXPORT_SYMBOL_GPL(device_unregister); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2700 | |
Andy Shevchenko | 3d060ae | 2015-07-27 18:04:00 +0300 | [diff] [blame] | 2701 | static struct device *prev_device(struct klist_iter *i) |
| 2702 | { |
| 2703 | struct klist_node *n = klist_prev(i); |
| 2704 | struct device *dev = NULL; |
| 2705 | struct device_private *p; |
| 2706 | |
| 2707 | if (n) { |
| 2708 | p = to_device_private_parent(n); |
| 2709 | dev = p->device; |
| 2710 | } |
| 2711 | return dev; |
| 2712 | } |
| 2713 | |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 2714 | static struct device *next_device(struct klist_iter *i) |
mochel@digitalimplant.org | 3623957 | 2005-03-24 19:08:30 -0800 | [diff] [blame] | 2715 | { |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 2716 | struct klist_node *n = klist_next(i); |
Greg Kroah-Hartman | f791b8c | 2008-12-16 12:24:56 -0800 | [diff] [blame] | 2717 | struct device *dev = NULL; |
| 2718 | struct device_private *p; |
| 2719 | |
| 2720 | if (n) { |
| 2721 | p = to_device_private_parent(n); |
| 2722 | dev = p->device; |
| 2723 | } |
| 2724 | return dev; |
mochel@digitalimplant.org | 3623957 | 2005-03-24 19:08:30 -0800 | [diff] [blame] | 2725 | } |
| 2726 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2727 | /** |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 2728 | * device_get_devnode - path of device node file |
Kay Sievers | 6fcf53a | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 2729 | * @dev: device |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 2730 | * @mode: returned file access mode |
Kay Sievers | 3c2670e | 2013-04-06 09:56:00 -0700 | [diff] [blame] | 2731 | * @uid: returned file owner |
| 2732 | * @gid: returned file group |
Kay Sievers | 6fcf53a | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 2733 | * @tmp: possibly allocated string |
| 2734 | * |
| 2735 | * Return the relative path of a possible device node. |
| 2736 | * Non-default names may need to allocate a memory to compose |
| 2737 | * a name. This memory is returned in tmp and needs to be |
| 2738 | * freed by the caller. |
| 2739 | */ |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 2740 | const char *device_get_devnode(struct device *dev, |
Greg Kroah-Hartman | 4e4098a | 2013-04-11 11:43:29 -0700 | [diff] [blame] | 2741 | umode_t *mode, kuid_t *uid, kgid_t *gid, |
Kay Sievers | 3c2670e | 2013-04-06 09:56:00 -0700 | [diff] [blame] | 2742 | const char **tmp) |
Kay Sievers | 6fcf53a | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 2743 | { |
| 2744 | char *s; |
| 2745 | |
| 2746 | *tmp = NULL; |
| 2747 | |
| 2748 | /* the device type may provide a specific name */ |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 2749 | if (dev->type && dev->type->devnode) |
Kay Sievers | 3c2670e | 2013-04-06 09:56:00 -0700 | [diff] [blame] | 2750 | *tmp = dev->type->devnode(dev, mode, uid, gid); |
Kay Sievers | 6fcf53a | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 2751 | if (*tmp) |
| 2752 | return *tmp; |
| 2753 | |
| 2754 | /* the class may provide a specific name */ |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 2755 | if (dev->class && dev->class->devnode) |
| 2756 | *tmp = dev->class->devnode(dev, mode); |
Kay Sievers | 6fcf53a | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 2757 | if (*tmp) |
| 2758 | return *tmp; |
| 2759 | |
| 2760 | /* return name without allocation, tmp == NULL */ |
| 2761 | if (strchr(dev_name(dev), '!') == NULL) |
| 2762 | return dev_name(dev); |
| 2763 | |
| 2764 | /* replace '!' in the name with '/' */ |
Rasmus Villemoes | a29fd61 | 2015-06-25 15:02:33 -0700 | [diff] [blame] | 2765 | s = kstrdup(dev_name(dev), GFP_KERNEL); |
| 2766 | if (!s) |
Kay Sievers | 6fcf53a | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 2767 | return NULL; |
Rasmus Villemoes | a29fd61 | 2015-06-25 15:02:33 -0700 | [diff] [blame] | 2768 | strreplace(s, '!', '/'); |
| 2769 | return *tmp = s; |
Kay Sievers | 6fcf53a | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 2770 | } |
| 2771 | |
| 2772 | /** |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 2773 | * device_for_each_child - device child iterator. |
| 2774 | * @parent: parent struct device. |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 2775 | * @fn: function to be called for each device. |
Robert P. J. Day | f8878dc | 2013-06-01 20:17:34 -0400 | [diff] [blame] | 2776 | * @data: data for the callback. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2777 | * |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 2778 | * Iterate over @parent's child devices, and call @fn for each, |
| 2779 | * passing it @data. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2780 | * |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 2781 | * We check the return of @fn each time. If it returns anything |
| 2782 | * other than 0, we break out and return that value. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2783 | */ |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 2784 | int device_for_each_child(struct device *parent, void *data, |
| 2785 | int (*fn)(struct device *dev, void *data)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2786 | { |
mochel@digitalimplant.org | 3623957 | 2005-03-24 19:08:30 -0800 | [diff] [blame] | 2787 | struct klist_iter i; |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 2788 | struct device *child; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2789 | int error = 0; |
| 2790 | |
Greg Kroah-Hartman | 014c90db | 2009-04-15 16:00:12 -0700 | [diff] [blame] | 2791 | if (!parent->p) |
| 2792 | return 0; |
| 2793 | |
Greg Kroah-Hartman | f791b8c | 2008-12-16 12:24:56 -0800 | [diff] [blame] | 2794 | klist_iter_init(&parent->p->klist_children, &i); |
Gimcuan Hui | 93ead7c | 2017-11-11 05:52:54 +0000 | [diff] [blame] | 2795 | while (!error && (child = next_device(&i))) |
mochel@digitalimplant.org | 3623957 | 2005-03-24 19:08:30 -0800 | [diff] [blame] | 2796 | error = fn(child, data); |
| 2797 | klist_iter_exit(&i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2798 | return error; |
| 2799 | } |
David Graham White | 86df268 | 2013-07-21 20:41:14 -0400 | [diff] [blame] | 2800 | EXPORT_SYMBOL_GPL(device_for_each_child); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2801 | |
Cornelia Huck | 5ab6998 | 2006-11-16 15:42:07 +0100 | [diff] [blame] | 2802 | /** |
Andy Shevchenko | 3d060ae | 2015-07-27 18:04:00 +0300 | [diff] [blame] | 2803 | * device_for_each_child_reverse - device child iterator in reversed order. |
| 2804 | * @parent: parent struct device. |
| 2805 | * @fn: function to be called for each device. |
| 2806 | * @data: data for the callback. |
| 2807 | * |
| 2808 | * Iterate over @parent's child devices, and call @fn for each, |
| 2809 | * passing it @data. |
| 2810 | * |
| 2811 | * We check the return of @fn each time. If it returns anything |
| 2812 | * other than 0, we break out and return that value. |
| 2813 | */ |
| 2814 | int device_for_each_child_reverse(struct device *parent, void *data, |
| 2815 | int (*fn)(struct device *dev, void *data)) |
| 2816 | { |
| 2817 | struct klist_iter i; |
| 2818 | struct device *child; |
| 2819 | int error = 0; |
| 2820 | |
| 2821 | if (!parent->p) |
| 2822 | return 0; |
| 2823 | |
| 2824 | klist_iter_init(&parent->p->klist_children, &i); |
| 2825 | while ((child = prev_device(&i)) && !error) |
| 2826 | error = fn(child, data); |
| 2827 | klist_iter_exit(&i); |
| 2828 | return error; |
| 2829 | } |
| 2830 | EXPORT_SYMBOL_GPL(device_for_each_child_reverse); |
| 2831 | |
| 2832 | /** |
Cornelia Huck | 5ab6998 | 2006-11-16 15:42:07 +0100 | [diff] [blame] | 2833 | * device_find_child - device iterator for locating a particular device. |
| 2834 | * @parent: parent struct device |
Cornelia Huck | 5ab6998 | 2006-11-16 15:42:07 +0100 | [diff] [blame] | 2835 | * @match: Callback function to check device |
Robert P. J. Day | f8878dc | 2013-06-01 20:17:34 -0400 | [diff] [blame] | 2836 | * @data: Data to pass to match function |
Cornelia Huck | 5ab6998 | 2006-11-16 15:42:07 +0100 | [diff] [blame] | 2837 | * |
| 2838 | * This is similar to the device_for_each_child() function above, but it |
| 2839 | * returns a reference to a device that is 'found' for later use, as |
| 2840 | * determined by the @match callback. |
| 2841 | * |
| 2842 | * The callback should return 0 if the device doesn't match and non-zero |
| 2843 | * if it does. If the callback returns non-zero and a reference to the |
| 2844 | * current device can be obtained, this function will return to the caller |
| 2845 | * and not iterate over any more devices. |
Federico Vaga | a4e2400 | 2013-04-15 11:18:11 +0200 | [diff] [blame] | 2846 | * |
| 2847 | * NOTE: you will need to drop the reference with put_device() after use. |
Cornelia Huck | 5ab6998 | 2006-11-16 15:42:07 +0100 | [diff] [blame] | 2848 | */ |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 2849 | struct device *device_find_child(struct device *parent, void *data, |
| 2850 | int (*match)(struct device *dev, void *data)) |
Cornelia Huck | 5ab6998 | 2006-11-16 15:42:07 +0100 | [diff] [blame] | 2851 | { |
| 2852 | struct klist_iter i; |
| 2853 | struct device *child; |
| 2854 | |
| 2855 | if (!parent) |
| 2856 | return NULL; |
| 2857 | |
Greg Kroah-Hartman | f791b8c | 2008-12-16 12:24:56 -0800 | [diff] [blame] | 2858 | klist_iter_init(&parent->p->klist_children, &i); |
Cornelia Huck | 5ab6998 | 2006-11-16 15:42:07 +0100 | [diff] [blame] | 2859 | while ((child = next_device(&i))) |
| 2860 | if (match(child, data) && get_device(child)) |
| 2861 | break; |
| 2862 | klist_iter_exit(&i); |
| 2863 | return child; |
| 2864 | } |
David Graham White | 86df268 | 2013-07-21 20:41:14 -0400 | [diff] [blame] | 2865 | EXPORT_SYMBOL_GPL(device_find_child); |
Cornelia Huck | 5ab6998 | 2006-11-16 15:42:07 +0100 | [diff] [blame] | 2866 | |
Heikki Krogerus | dad9bb0 | 2019-05-31 17:15:37 +0300 | [diff] [blame] | 2867 | /** |
| 2868 | * device_find_child_by_name - device iterator for locating a child device. |
| 2869 | * @parent: parent struct device |
| 2870 | * @name: name of the child device |
| 2871 | * |
| 2872 | * This is similar to the device_find_child() function above, but it |
| 2873 | * returns a reference to a device that has the name @name. |
| 2874 | * |
| 2875 | * NOTE: you will need to drop the reference with put_device() after use. |
| 2876 | */ |
| 2877 | struct device *device_find_child_by_name(struct device *parent, |
| 2878 | const char *name) |
| 2879 | { |
| 2880 | struct klist_iter i; |
| 2881 | struct device *child; |
| 2882 | |
| 2883 | if (!parent) |
| 2884 | return NULL; |
| 2885 | |
| 2886 | klist_iter_init(&parent->p->klist_children, &i); |
| 2887 | while ((child = next_device(&i))) |
| 2888 | if (!strcmp(dev_name(child), name) && get_device(child)) |
| 2889 | break; |
| 2890 | klist_iter_exit(&i); |
| 2891 | return child; |
| 2892 | } |
| 2893 | EXPORT_SYMBOL_GPL(device_find_child_by_name); |
| 2894 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2895 | int __init devices_init(void) |
| 2896 | { |
Greg Kroah-Hartman | 881c6cfd | 2007-11-01 09:29:06 -0600 | [diff] [blame] | 2897 | devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL); |
| 2898 | if (!devices_kset) |
| 2899 | return -ENOMEM; |
Dan Williams | e105b8b | 2008-04-21 10:51:07 -0700 | [diff] [blame] | 2900 | dev_kobj = kobject_create_and_add("dev", NULL); |
| 2901 | if (!dev_kobj) |
| 2902 | goto dev_kobj_err; |
| 2903 | sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj); |
| 2904 | if (!sysfs_dev_block_kobj) |
| 2905 | goto block_kobj_err; |
| 2906 | sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj); |
| 2907 | if (!sysfs_dev_char_kobj) |
| 2908 | goto char_kobj_err; |
| 2909 | |
Greg Kroah-Hartman | 881c6cfd | 2007-11-01 09:29:06 -0600 | [diff] [blame] | 2910 | return 0; |
Dan Williams | e105b8b | 2008-04-21 10:51:07 -0700 | [diff] [blame] | 2911 | |
| 2912 | char_kobj_err: |
| 2913 | kobject_put(sysfs_dev_block_kobj); |
| 2914 | block_kobj_err: |
| 2915 | kobject_put(dev_kobj); |
| 2916 | dev_kobj_err: |
| 2917 | kset_unregister(devices_kset); |
| 2918 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2919 | } |
| 2920 | |
Rafael J. Wysocki | 4f3549d | 2013-05-02 22:15:29 +0200 | [diff] [blame] | 2921 | static int device_check_offline(struct device *dev, void *not_used) |
| 2922 | { |
| 2923 | int ret; |
| 2924 | |
| 2925 | ret = device_for_each_child(dev, NULL, device_check_offline); |
| 2926 | if (ret) |
| 2927 | return ret; |
| 2928 | |
| 2929 | return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0; |
| 2930 | } |
| 2931 | |
| 2932 | /** |
| 2933 | * device_offline - Prepare the device for hot-removal. |
| 2934 | * @dev: Device to be put offline. |
| 2935 | * |
| 2936 | * Execute the device bus type's .offline() callback, if present, to prepare |
| 2937 | * the device for a subsequent hot-removal. If that succeeds, the device must |
| 2938 | * not be used until either it is removed or its bus type's .online() callback |
| 2939 | * is executed. |
| 2940 | * |
| 2941 | * Call under device_hotplug_lock. |
| 2942 | */ |
| 2943 | int device_offline(struct device *dev) |
| 2944 | { |
| 2945 | int ret; |
| 2946 | |
| 2947 | if (dev->offline_disabled) |
| 2948 | return -EPERM; |
| 2949 | |
| 2950 | ret = device_for_each_child(dev, NULL, device_check_offline); |
| 2951 | if (ret) |
| 2952 | return ret; |
| 2953 | |
| 2954 | device_lock(dev); |
| 2955 | if (device_supports_offline(dev)) { |
| 2956 | if (dev->offline) { |
| 2957 | ret = 1; |
| 2958 | } else { |
| 2959 | ret = dev->bus->offline(dev); |
| 2960 | if (!ret) { |
| 2961 | kobject_uevent(&dev->kobj, KOBJ_OFFLINE); |
| 2962 | dev->offline = true; |
| 2963 | } |
| 2964 | } |
| 2965 | } |
| 2966 | device_unlock(dev); |
| 2967 | |
| 2968 | return ret; |
| 2969 | } |
| 2970 | |
| 2971 | /** |
| 2972 | * device_online - Put the device back online after successful device_offline(). |
| 2973 | * @dev: Device to be put back online. |
| 2974 | * |
| 2975 | * If device_offline() has been successfully executed for @dev, but the device |
| 2976 | * has not been removed subsequently, execute its bus type's .online() callback |
| 2977 | * to indicate that the device can be used again. |
| 2978 | * |
| 2979 | * Call under device_hotplug_lock. |
| 2980 | */ |
| 2981 | int device_online(struct device *dev) |
| 2982 | { |
| 2983 | int ret = 0; |
| 2984 | |
| 2985 | device_lock(dev); |
| 2986 | if (device_supports_offline(dev)) { |
| 2987 | if (dev->offline) { |
| 2988 | ret = dev->bus->online(dev); |
| 2989 | if (!ret) { |
| 2990 | kobject_uevent(&dev->kobj, KOBJ_ONLINE); |
| 2991 | dev->offline = false; |
| 2992 | } |
| 2993 | } else { |
| 2994 | ret = 1; |
| 2995 | } |
| 2996 | } |
| 2997 | device_unlock(dev); |
| 2998 | |
| 2999 | return ret; |
| 3000 | } |
| 3001 | |
Karthigan Srinivasan | 7f100d1 | 2011-04-18 16:16:52 -0500 | [diff] [blame] | 3002 | struct root_device { |
Mark McLoughlin | 0aa0dc4 | 2008-12-15 12:58:26 +0000 | [diff] [blame] | 3003 | struct device dev; |
| 3004 | struct module *owner; |
| 3005 | }; |
| 3006 | |
Josh Triplett | 9305842 | 2012-11-18 21:27:55 -0800 | [diff] [blame] | 3007 | static inline struct root_device *to_root_device(struct device *d) |
Ferenc Wagner | 481e207 | 2011-01-07 15:17:47 +0100 | [diff] [blame] | 3008 | { |
| 3009 | return container_of(d, struct root_device, dev); |
| 3010 | } |
Mark McLoughlin | 0aa0dc4 | 2008-12-15 12:58:26 +0000 | [diff] [blame] | 3011 | |
| 3012 | static void root_device_release(struct device *dev) |
| 3013 | { |
| 3014 | kfree(to_root_device(dev)); |
| 3015 | } |
| 3016 | |
| 3017 | /** |
| 3018 | * __root_device_register - allocate and register a root device |
| 3019 | * @name: root device name |
| 3020 | * @owner: owner module of the root device, usually THIS_MODULE |
| 3021 | * |
| 3022 | * This function allocates a root device and registers it |
| 3023 | * using device_register(). In order to free the returned |
| 3024 | * device, use root_device_unregister(). |
| 3025 | * |
| 3026 | * Root devices are dummy devices which allow other devices |
| 3027 | * to be grouped under /sys/devices. Use this function to |
| 3028 | * allocate a root device and then use it as the parent of |
| 3029 | * any device which should appear under /sys/devices/{name} |
| 3030 | * |
| 3031 | * The /sys/devices/{name} directory will also contain a |
| 3032 | * 'module' symlink which points to the @owner directory |
| 3033 | * in sysfs. |
| 3034 | * |
Jani Nikula | f0eae0e | 2010-03-11 18:11:45 +0200 | [diff] [blame] | 3035 | * Returns &struct device pointer on success, or ERR_PTR() on error. |
| 3036 | * |
Mark McLoughlin | 0aa0dc4 | 2008-12-15 12:58:26 +0000 | [diff] [blame] | 3037 | * Note: You probably want to use root_device_register(). |
| 3038 | */ |
| 3039 | struct device *__root_device_register(const char *name, struct module *owner) |
| 3040 | { |
| 3041 | struct root_device *root; |
| 3042 | int err = -ENOMEM; |
| 3043 | |
| 3044 | root = kzalloc(sizeof(struct root_device), GFP_KERNEL); |
| 3045 | if (!root) |
| 3046 | return ERR_PTR(err); |
| 3047 | |
Greg Kroah-Hartman | acc0e90 | 2009-06-02 15:39:55 -0700 | [diff] [blame] | 3048 | err = dev_set_name(&root->dev, "%s", name); |
Mark McLoughlin | 0aa0dc4 | 2008-12-15 12:58:26 +0000 | [diff] [blame] | 3049 | if (err) { |
| 3050 | kfree(root); |
| 3051 | return ERR_PTR(err); |
| 3052 | } |
| 3053 | |
| 3054 | root->dev.release = root_device_release; |
| 3055 | |
| 3056 | err = device_register(&root->dev); |
| 3057 | if (err) { |
| 3058 | put_device(&root->dev); |
| 3059 | return ERR_PTR(err); |
| 3060 | } |
| 3061 | |
Christoph Egger | 1d9e882 | 2010-05-17 16:57:58 +0200 | [diff] [blame] | 3062 | #ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */ |
Mark McLoughlin | 0aa0dc4 | 2008-12-15 12:58:26 +0000 | [diff] [blame] | 3063 | if (owner) { |
| 3064 | struct module_kobject *mk = &owner->mkobj; |
| 3065 | |
| 3066 | err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module"); |
| 3067 | if (err) { |
| 3068 | device_unregister(&root->dev); |
| 3069 | return ERR_PTR(err); |
| 3070 | } |
| 3071 | root->owner = owner; |
| 3072 | } |
| 3073 | #endif |
| 3074 | |
| 3075 | return &root->dev; |
| 3076 | } |
| 3077 | EXPORT_SYMBOL_GPL(__root_device_register); |
| 3078 | |
| 3079 | /** |
| 3080 | * root_device_unregister - unregister and free a root device |
Randy Dunlap | 7cbcf22 | 2009-01-20 16:29:13 -0800 | [diff] [blame] | 3081 | * @dev: device going away |
Mark McLoughlin | 0aa0dc4 | 2008-12-15 12:58:26 +0000 | [diff] [blame] | 3082 | * |
| 3083 | * This function unregisters and cleans up a device that was created by |
| 3084 | * root_device_register(). |
| 3085 | */ |
| 3086 | void root_device_unregister(struct device *dev) |
| 3087 | { |
| 3088 | struct root_device *root = to_root_device(dev); |
| 3089 | |
| 3090 | if (root->owner) |
| 3091 | sysfs_remove_link(&root->dev.kobj, "module"); |
| 3092 | |
| 3093 | device_unregister(dev); |
| 3094 | } |
| 3095 | EXPORT_SYMBOL_GPL(root_device_unregister); |
| 3096 | |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 3097 | |
| 3098 | static void device_create_release(struct device *dev) |
| 3099 | { |
Kay Sievers | 1e0b2cf | 2008-10-30 01:36:48 +0100 | [diff] [blame] | 3100 | pr_debug("device: '%s': %s\n", dev_name(dev), __func__); |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 3101 | kfree(dev); |
| 3102 | } |
| 3103 | |
Mathieu Malaterre | 6a8b55d | 2018-05-05 21:57:41 +0200 | [diff] [blame] | 3104 | static __printf(6, 0) struct device * |
Guenter Roeck | 39ef311 | 2013-07-14 16:05:57 -0700 | [diff] [blame] | 3105 | device_create_groups_vargs(struct class *class, struct device *parent, |
| 3106 | dev_t devt, void *drvdata, |
| 3107 | const struct attribute_group **groups, |
| 3108 | const char *fmt, va_list args) |
| 3109 | { |
| 3110 | struct device *dev = NULL; |
| 3111 | int retval = -ENODEV; |
| 3112 | |
| 3113 | if (class == NULL || IS_ERR(class)) |
| 3114 | goto error; |
| 3115 | |
| 3116 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
| 3117 | if (!dev) { |
| 3118 | retval = -ENOMEM; |
| 3119 | goto error; |
| 3120 | } |
| 3121 | |
David Herrmann | bbc780f | 2013-11-21 20:15:48 +0100 | [diff] [blame] | 3122 | device_initialize(dev); |
Guenter Roeck | 39ef311 | 2013-07-14 16:05:57 -0700 | [diff] [blame] | 3123 | dev->devt = devt; |
| 3124 | dev->class = class; |
| 3125 | dev->parent = parent; |
| 3126 | dev->groups = groups; |
| 3127 | dev->release = device_create_release; |
| 3128 | dev_set_drvdata(dev, drvdata); |
| 3129 | |
| 3130 | retval = kobject_set_name_vargs(&dev->kobj, fmt, args); |
| 3131 | if (retval) |
| 3132 | goto error; |
| 3133 | |
David Herrmann | bbc780f | 2013-11-21 20:15:48 +0100 | [diff] [blame] | 3134 | retval = device_add(dev); |
Guenter Roeck | 39ef311 | 2013-07-14 16:05:57 -0700 | [diff] [blame] | 3135 | if (retval) |
| 3136 | goto error; |
| 3137 | |
| 3138 | return dev; |
| 3139 | |
| 3140 | error: |
| 3141 | put_device(dev); |
| 3142 | return ERR_PTR(retval); |
| 3143 | } |
| 3144 | |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 3145 | /** |
Greg Kroah-Hartman | 8882b39 | 2008-05-15 13:44:08 -0700 | [diff] [blame] | 3146 | * device_create_vargs - creates a device and registers it with sysfs |
| 3147 | * @class: pointer to the struct class that this device should be registered to |
| 3148 | * @parent: pointer to the parent struct device of this new device, if any |
| 3149 | * @devt: the dev_t for the char device to be added |
| 3150 | * @drvdata: the data to be added to the device for callbacks |
| 3151 | * @fmt: string for the device's name |
| 3152 | * @args: va_list for the device's name |
| 3153 | * |
| 3154 | * This function can be used by char device classes. A struct device |
| 3155 | * will be created in sysfs, registered to the specified class. |
| 3156 | * |
| 3157 | * A "dev" file will be created, showing the dev_t for the device, if |
| 3158 | * the dev_t is not 0,0. |
| 3159 | * If a pointer to a parent struct device is passed in, the newly created |
| 3160 | * struct device will be a child of that device in sysfs. |
| 3161 | * The pointer to the struct device will be returned from the call. |
| 3162 | * Any further sysfs files that might be required can be created using this |
| 3163 | * pointer. |
| 3164 | * |
Jani Nikula | f0eae0e | 2010-03-11 18:11:45 +0200 | [diff] [blame] | 3165 | * Returns &struct device pointer on success, or ERR_PTR() on error. |
| 3166 | * |
Greg Kroah-Hartman | 8882b39 | 2008-05-15 13:44:08 -0700 | [diff] [blame] | 3167 | * Note: the struct class passed to this function must have previously |
| 3168 | * been created with a call to class_create(). |
| 3169 | */ |
| 3170 | struct device *device_create_vargs(struct class *class, struct device *parent, |
| 3171 | dev_t devt, void *drvdata, const char *fmt, |
| 3172 | va_list args) |
| 3173 | { |
Guenter Roeck | 39ef311 | 2013-07-14 16:05:57 -0700 | [diff] [blame] | 3174 | return device_create_groups_vargs(class, parent, devt, drvdata, NULL, |
| 3175 | fmt, args); |
Greg Kroah-Hartman | 8882b39 | 2008-05-15 13:44:08 -0700 | [diff] [blame] | 3176 | } |
| 3177 | EXPORT_SYMBOL_GPL(device_create_vargs); |
| 3178 | |
| 3179 | /** |
Greg Kroah-Hartman | 4e10673 | 2008-07-21 20:03:34 -0700 | [diff] [blame] | 3180 | * device_create - creates a device and registers it with sysfs |
Greg Kroah-Hartman | 8882b39 | 2008-05-15 13:44:08 -0700 | [diff] [blame] | 3181 | * @class: pointer to the struct class that this device should be registered to |
| 3182 | * @parent: pointer to the parent struct device of this new device, if any |
| 3183 | * @devt: the dev_t for the char device to be added |
| 3184 | * @drvdata: the data to be added to the device for callbacks |
| 3185 | * @fmt: string for the device's name |
| 3186 | * |
| 3187 | * This function can be used by char device classes. A struct device |
| 3188 | * will be created in sysfs, registered to the specified class. |
| 3189 | * |
| 3190 | * A "dev" file will be created, showing the dev_t for the device, if |
| 3191 | * the dev_t is not 0,0. |
| 3192 | * If a pointer to a parent struct device is passed in, the newly created |
| 3193 | * struct device will be a child of that device in sysfs. |
| 3194 | * The pointer to the struct device will be returned from the call. |
| 3195 | * Any further sysfs files that might be required can be created using this |
| 3196 | * pointer. |
| 3197 | * |
Jani Nikula | f0eae0e | 2010-03-11 18:11:45 +0200 | [diff] [blame] | 3198 | * Returns &struct device pointer on success, or ERR_PTR() on error. |
| 3199 | * |
Greg Kroah-Hartman | 8882b39 | 2008-05-15 13:44:08 -0700 | [diff] [blame] | 3200 | * Note: the struct class passed to this function must have previously |
| 3201 | * been created with a call to class_create(). |
| 3202 | */ |
Greg Kroah-Hartman | 4e10673 | 2008-07-21 20:03:34 -0700 | [diff] [blame] | 3203 | struct device *device_create(struct class *class, struct device *parent, |
| 3204 | dev_t devt, void *drvdata, const char *fmt, ...) |
Greg Kroah-Hartman | 8882b39 | 2008-05-15 13:44:08 -0700 | [diff] [blame] | 3205 | { |
| 3206 | va_list vargs; |
| 3207 | struct device *dev; |
| 3208 | |
| 3209 | va_start(vargs, fmt); |
| 3210 | dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs); |
| 3211 | va_end(vargs); |
| 3212 | return dev; |
| 3213 | } |
Greg Kroah-Hartman | 4e10673 | 2008-07-21 20:03:34 -0700 | [diff] [blame] | 3214 | EXPORT_SYMBOL_GPL(device_create); |
Greg Kroah-Hartman | 8882b39 | 2008-05-15 13:44:08 -0700 | [diff] [blame] | 3215 | |
Guenter Roeck | 39ef311 | 2013-07-14 16:05:57 -0700 | [diff] [blame] | 3216 | /** |
| 3217 | * device_create_with_groups - creates a device and registers it with sysfs |
| 3218 | * @class: pointer to the struct class that this device should be registered to |
| 3219 | * @parent: pointer to the parent struct device of this new device, if any |
| 3220 | * @devt: the dev_t for the char device to be added |
| 3221 | * @drvdata: the data to be added to the device for callbacks |
| 3222 | * @groups: NULL-terminated list of attribute groups to be created |
| 3223 | * @fmt: string for the device's name |
| 3224 | * |
| 3225 | * This function can be used by char device classes. A struct device |
| 3226 | * will be created in sysfs, registered to the specified class. |
| 3227 | * Additional attributes specified in the groups parameter will also |
| 3228 | * be created automatically. |
| 3229 | * |
| 3230 | * A "dev" file will be created, showing the dev_t for the device, if |
| 3231 | * the dev_t is not 0,0. |
| 3232 | * If a pointer to a parent struct device is passed in, the newly created |
| 3233 | * struct device will be a child of that device in sysfs. |
| 3234 | * The pointer to the struct device will be returned from the call. |
| 3235 | * Any further sysfs files that might be required can be created using this |
| 3236 | * pointer. |
| 3237 | * |
| 3238 | * Returns &struct device pointer on success, or ERR_PTR() on error. |
| 3239 | * |
| 3240 | * Note: the struct class passed to this function must have previously |
| 3241 | * been created with a call to class_create(). |
| 3242 | */ |
| 3243 | struct device *device_create_with_groups(struct class *class, |
| 3244 | struct device *parent, dev_t devt, |
| 3245 | void *drvdata, |
| 3246 | const struct attribute_group **groups, |
| 3247 | const char *fmt, ...) |
| 3248 | { |
| 3249 | va_list vargs; |
| 3250 | struct device *dev; |
| 3251 | |
| 3252 | va_start(vargs, fmt); |
| 3253 | dev = device_create_groups_vargs(class, parent, devt, drvdata, groups, |
| 3254 | fmt, vargs); |
| 3255 | va_end(vargs); |
| 3256 | return dev; |
| 3257 | } |
| 3258 | EXPORT_SYMBOL_GPL(device_create_with_groups); |
| 3259 | |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 3260 | /** |
| 3261 | * device_destroy - removes a device that was created with device_create() |
| 3262 | * @class: pointer to the struct class that this device was registered with |
| 3263 | * @devt: the dev_t of the device that was previously registered |
| 3264 | * |
| 3265 | * This call unregisters and cleans up a device that was created with a |
| 3266 | * call to device_create(). |
| 3267 | */ |
| 3268 | void device_destroy(struct class *class, dev_t devt) |
| 3269 | { |
| 3270 | struct device *dev; |
| 3271 | |
Suzuki K Poulose | 4495dfd | 2019-07-23 23:18:35 +0100 | [diff] [blame] | 3272 | dev = class_find_device_by_devt(class, devt); |
Dave Young | cd35449 | 2008-01-28 16:56:11 +0800 | [diff] [blame] | 3273 | if (dev) { |
| 3274 | put_device(dev); |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 3275 | device_unregister(dev); |
Dave Young | cd35449 | 2008-01-28 16:56:11 +0800 | [diff] [blame] | 3276 | } |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 3277 | } |
| 3278 | EXPORT_SYMBOL_GPL(device_destroy); |
Greg Kroah-Hartman | a2de48c | 2006-07-03 14:31:12 -0700 | [diff] [blame] | 3279 | |
| 3280 | /** |
| 3281 | * device_rename - renames a device |
| 3282 | * @dev: the pointer to the struct device to be renamed |
| 3283 | * @new_name: the new name of the device |
Eric W. Biederman | 030c1d2 | 2008-05-08 14:41:00 -0700 | [diff] [blame] | 3284 | * |
| 3285 | * It is the responsibility of the caller to provide mutual |
| 3286 | * exclusion between two different calls of device_rename |
| 3287 | * on the same device to ensure that new_name is valid and |
| 3288 | * won't conflict with other devices. |
Michael Ellerman | c6c0ac6 | 2010-11-25 09:44:07 +1100 | [diff] [blame] | 3289 | * |
Timur Tabi | a546251 | 2010-12-13 14:08:52 -0600 | [diff] [blame] | 3290 | * Note: Don't call this function. Currently, the networking layer calls this |
| 3291 | * function, but that will change. The following text from Kay Sievers offers |
| 3292 | * some insight: |
| 3293 | * |
| 3294 | * Renaming devices is racy at many levels, symlinks and other stuff are not |
| 3295 | * replaced atomically, and you get a "move" uevent, but it's not easy to |
| 3296 | * connect the event to the old and new device. Device nodes are not renamed at |
| 3297 | * all, there isn't even support for that in the kernel now. |
| 3298 | * |
| 3299 | * In the meantime, during renaming, your target name might be taken by another |
| 3300 | * driver, creating conflicts. Or the old name is taken directly after you |
| 3301 | * renamed it -- then you get events for the same DEVPATH, before you even see |
| 3302 | * the "move" event. It's just a mess, and nothing new should ever rely on |
| 3303 | * kernel device renaming. Besides that, it's not even implemented now for |
| 3304 | * other things than (driver-core wise very simple) network devices. |
| 3305 | * |
| 3306 | * We are currently about to change network renaming in udev to completely |
| 3307 | * disallow renaming of devices in the same namespace as the kernel uses, |
| 3308 | * because we can't solve the problems properly, that arise with swapping names |
| 3309 | * of multiple interfaces without races. Means, renaming of eth[0-9]* will only |
| 3310 | * be allowed to some other name than eth[0-9]*, for the aforementioned |
| 3311 | * reasons. |
| 3312 | * |
| 3313 | * Make up a "real" name in the driver before you register anything, or add |
| 3314 | * some other attributes for userspace to find the device, or use udev to add |
| 3315 | * symlinks -- but never rename kernel devices later, it's a complete mess. We |
| 3316 | * don't even want to get into that and try to implement the missing pieces in |
| 3317 | * the core. We really have other pieces to fix in the driver core mess. :) |
Greg Kroah-Hartman | a2de48c | 2006-07-03 14:31:12 -0700 | [diff] [blame] | 3318 | */ |
Johannes Berg | 6937e8f | 2010-08-05 17:38:18 +0200 | [diff] [blame] | 3319 | int device_rename(struct device *dev, const char *new_name) |
Greg Kroah-Hartman | a2de48c | 2006-07-03 14:31:12 -0700 | [diff] [blame] | 3320 | { |
Tejun Heo | 4b30ee5 | 2013-09-11 22:29:06 -0400 | [diff] [blame] | 3321 | struct kobject *kobj = &dev->kobj; |
Cornelia Huck | 2ee97ca | 2007-07-18 01:43:47 -0700 | [diff] [blame] | 3322 | char *old_device_name = NULL; |
Greg Kroah-Hartman | a2de48c | 2006-07-03 14:31:12 -0700 | [diff] [blame] | 3323 | int error; |
| 3324 | |
| 3325 | dev = get_device(dev); |
| 3326 | if (!dev) |
| 3327 | return -EINVAL; |
| 3328 | |
ethan.zhao | 69df753 | 2013-10-13 22:12:35 +0800 | [diff] [blame] | 3329 | dev_dbg(dev, "renaming to %s\n", new_name); |
Greg Kroah-Hartman | a2de48c | 2006-07-03 14:31:12 -0700 | [diff] [blame] | 3330 | |
Kay Sievers | 1fa5ae8 | 2009-01-25 15:17:37 +0100 | [diff] [blame] | 3331 | old_device_name = kstrdup(dev_name(dev), GFP_KERNEL); |
Cornelia Huck | 2ee97ca | 2007-07-18 01:43:47 -0700 | [diff] [blame] | 3332 | if (!old_device_name) { |
| 3333 | error = -ENOMEM; |
| 3334 | goto out; |
Greg Kroah-Hartman | a2de48c | 2006-07-03 14:31:12 -0700 | [diff] [blame] | 3335 | } |
Greg Kroah-Hartman | a2de48c | 2006-07-03 14:31:12 -0700 | [diff] [blame] | 3336 | |
Eric W. Biederman | f349cf3 | 2010-03-30 11:31:29 -0700 | [diff] [blame] | 3337 | if (dev->class) { |
Tejun Heo | 4b30ee5 | 2013-09-11 22:29:06 -0400 | [diff] [blame] | 3338 | error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj, |
| 3339 | kobj, old_device_name, |
| 3340 | new_name, kobject_namespace(kobj)); |
Eric W. Biederman | f349cf3 | 2010-03-30 11:31:29 -0700 | [diff] [blame] | 3341 | if (error) |
| 3342 | goto out; |
| 3343 | } |
Kay Sievers | 39aba96 | 2010-09-04 22:33:14 -0700 | [diff] [blame] | 3344 | |
Tejun Heo | 4b30ee5 | 2013-09-11 22:29:06 -0400 | [diff] [blame] | 3345 | error = kobject_rename(kobj, new_name); |
Kay Sievers | 1fa5ae8 | 2009-01-25 15:17:37 +0100 | [diff] [blame] | 3346 | if (error) |
Cornelia Huck | 2ee97ca | 2007-07-18 01:43:47 -0700 | [diff] [blame] | 3347 | goto out; |
Greg Kroah-Hartman | a2de48c | 2006-07-03 14:31:12 -0700 | [diff] [blame] | 3348 | |
Cornelia Huck | 2ee97ca | 2007-07-18 01:43:47 -0700 | [diff] [blame] | 3349 | out: |
Greg Kroah-Hartman | a2de48c | 2006-07-03 14:31:12 -0700 | [diff] [blame] | 3350 | put_device(dev); |
| 3351 | |
Cornelia Huck | 2ee97ca | 2007-07-18 01:43:47 -0700 | [diff] [blame] | 3352 | kfree(old_device_name); |
Greg Kroah-Hartman | a2de48c | 2006-07-03 14:31:12 -0700 | [diff] [blame] | 3353 | |
| 3354 | return error; |
| 3355 | } |
Johannes Berg | a2807db | 2007-02-28 12:38:31 +0100 | [diff] [blame] | 3356 | EXPORT_SYMBOL_GPL(device_rename); |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 3357 | |
| 3358 | static int device_move_class_links(struct device *dev, |
| 3359 | struct device *old_parent, |
| 3360 | struct device *new_parent) |
| 3361 | { |
Greg Kroah-Hartman | f7f3461 | 2007-03-06 12:55:53 -0800 | [diff] [blame] | 3362 | int error = 0; |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 3363 | |
Greg Kroah-Hartman | f7f3461 | 2007-03-06 12:55:53 -0800 | [diff] [blame] | 3364 | if (old_parent) |
| 3365 | sysfs_remove_link(&dev->kobj, "device"); |
| 3366 | if (new_parent) |
| 3367 | error = sysfs_create_link(&dev->kobj, &new_parent->kobj, |
| 3368 | "device"); |
| 3369 | return error; |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 3370 | } |
| 3371 | |
| 3372 | /** |
| 3373 | * device_move - moves a device to a new parent |
| 3374 | * @dev: the pointer to the struct device to be moved |
Wolfram Sang | 1350986 | 2018-05-06 13:23:47 +0200 | [diff] [blame] | 3375 | * @new_parent: the new parent of the device (can be NULL) |
Cornelia Huck | ffa6a70 | 2009-03-04 12:44:00 +0100 | [diff] [blame] | 3376 | * @dpm_order: how to reorder the dpm_list |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 3377 | */ |
Cornelia Huck | ffa6a70 | 2009-03-04 12:44:00 +0100 | [diff] [blame] | 3378 | int device_move(struct device *dev, struct device *new_parent, |
| 3379 | enum dpm_order dpm_order) |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 3380 | { |
| 3381 | int error; |
| 3382 | struct device *old_parent; |
Cornelia Huck | c744aeae | 2007-01-08 20:16:44 +0100 | [diff] [blame] | 3383 | struct kobject *new_parent_kobj; |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 3384 | |
| 3385 | dev = get_device(dev); |
| 3386 | if (!dev) |
| 3387 | return -EINVAL; |
| 3388 | |
Cornelia Huck | ffa6a70 | 2009-03-04 12:44:00 +0100 | [diff] [blame] | 3389 | device_pm_lock(); |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 3390 | new_parent = get_device(new_parent); |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 3391 | new_parent_kobj = get_device_parent(dev, new_parent); |
Tetsuo Handa | 84d0c27 | 2018-05-07 19:10:31 +0900 | [diff] [blame] | 3392 | if (IS_ERR(new_parent_kobj)) { |
| 3393 | error = PTR_ERR(new_parent_kobj); |
| 3394 | put_device(new_parent); |
| 3395 | goto out; |
| 3396 | } |
Cornelia Huck | 63b6971 | 2008-01-21 16:09:44 +0100 | [diff] [blame] | 3397 | |
Kay Sievers | 1e0b2cf | 2008-10-30 01:36:48 +0100 | [diff] [blame] | 3398 | pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev), |
| 3399 | __func__, new_parent ? dev_name(new_parent) : "<NULL>"); |
Cornelia Huck | c744aeae | 2007-01-08 20:16:44 +0100 | [diff] [blame] | 3400 | error = kobject_move(&dev->kobj, new_parent_kobj); |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 3401 | if (error) { |
Cornelia Huck | 63b6971 | 2008-01-21 16:09:44 +0100 | [diff] [blame] | 3402 | cleanup_glue_dir(dev, new_parent_kobj); |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 3403 | put_device(new_parent); |
| 3404 | goto out; |
| 3405 | } |
| 3406 | old_parent = dev->parent; |
| 3407 | dev->parent = new_parent; |
| 3408 | if (old_parent) |
Greg Kroah-Hartman | f791b8c | 2008-12-16 12:24:56 -0800 | [diff] [blame] | 3409 | klist_remove(&dev->p->knode_parent); |
Yinghai Lu | 0d358f2 | 2008-02-19 03:20:41 -0800 | [diff] [blame] | 3410 | if (new_parent) { |
Greg Kroah-Hartman | f791b8c | 2008-12-16 12:24:56 -0800 | [diff] [blame] | 3411 | klist_add_tail(&dev->p->knode_parent, |
| 3412 | &new_parent->p->klist_children); |
Yinghai Lu | 0d358f2 | 2008-02-19 03:20:41 -0800 | [diff] [blame] | 3413 | set_dev_node(dev, dev_to_node(new_parent)); |
| 3414 | } |
| 3415 | |
Rabin Vincent | bdd4034 | 2012-04-23 09:16:36 +0200 | [diff] [blame] | 3416 | if (dev->class) { |
| 3417 | error = device_move_class_links(dev, old_parent, new_parent); |
| 3418 | if (error) { |
| 3419 | /* We ignore errors on cleanup since we're hosed anyway... */ |
| 3420 | device_move_class_links(dev, new_parent, old_parent); |
| 3421 | if (!kobject_move(&dev->kobj, &old_parent->kobj)) { |
| 3422 | if (new_parent) |
| 3423 | klist_remove(&dev->p->knode_parent); |
| 3424 | dev->parent = old_parent; |
| 3425 | if (old_parent) { |
| 3426 | klist_add_tail(&dev->p->knode_parent, |
| 3427 | &old_parent->p->klist_children); |
| 3428 | set_dev_node(dev, dev_to_node(old_parent)); |
| 3429 | } |
Yinghai Lu | 0d358f2 | 2008-02-19 03:20:41 -0800 | [diff] [blame] | 3430 | } |
Rabin Vincent | bdd4034 | 2012-04-23 09:16:36 +0200 | [diff] [blame] | 3431 | cleanup_glue_dir(dev, new_parent_kobj); |
| 3432 | put_device(new_parent); |
| 3433 | goto out; |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 3434 | } |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 3435 | } |
Cornelia Huck | ffa6a70 | 2009-03-04 12:44:00 +0100 | [diff] [blame] | 3436 | switch (dpm_order) { |
| 3437 | case DPM_ORDER_NONE: |
| 3438 | break; |
| 3439 | case DPM_ORDER_DEV_AFTER_PARENT: |
| 3440 | device_pm_move_after(dev, new_parent); |
Grygorii Strashko | 52cdbdd | 2015-07-27 20:43:01 +0300 | [diff] [blame] | 3441 | devices_kset_move_after(dev, new_parent); |
Cornelia Huck | ffa6a70 | 2009-03-04 12:44:00 +0100 | [diff] [blame] | 3442 | break; |
| 3443 | case DPM_ORDER_PARENT_BEFORE_DEV: |
| 3444 | device_pm_move_before(new_parent, dev); |
Grygorii Strashko | 52cdbdd | 2015-07-27 20:43:01 +0300 | [diff] [blame] | 3445 | devices_kset_move_before(new_parent, dev); |
Cornelia Huck | ffa6a70 | 2009-03-04 12:44:00 +0100 | [diff] [blame] | 3446 | break; |
| 3447 | case DPM_ORDER_DEV_LAST: |
| 3448 | device_pm_move_last(dev); |
Grygorii Strashko | 52cdbdd | 2015-07-27 20:43:01 +0300 | [diff] [blame] | 3449 | devices_kset_move_last(dev); |
Cornelia Huck | ffa6a70 | 2009-03-04 12:44:00 +0100 | [diff] [blame] | 3450 | break; |
| 3451 | } |
Rabin Vincent | bdd4034 | 2012-04-23 09:16:36 +0200 | [diff] [blame] | 3452 | |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 3453 | put_device(old_parent); |
| 3454 | out: |
Cornelia Huck | ffa6a70 | 2009-03-04 12:44:00 +0100 | [diff] [blame] | 3455 | device_pm_unlock(); |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 3456 | put_device(dev); |
| 3457 | return error; |
| 3458 | } |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 3459 | EXPORT_SYMBOL_GPL(device_move); |
Greg Kroah-Hartman | 37b0c02 | 2007-11-26 22:11:55 -0800 | [diff] [blame] | 3460 | |
Christian Brauner | b8f33e5 | 2020-02-27 04:37:15 +0100 | [diff] [blame^] | 3461 | static int device_attrs_change_owner(struct device *dev, kuid_t kuid, |
| 3462 | kgid_t kgid) |
| 3463 | { |
| 3464 | struct kobject *kobj = &dev->kobj; |
| 3465 | struct class *class = dev->class; |
| 3466 | const struct device_type *type = dev->type; |
| 3467 | int error; |
| 3468 | |
| 3469 | if (class) { |
| 3470 | /* |
| 3471 | * Change the device groups of the device class for @dev to |
| 3472 | * @kuid/@kgid. |
| 3473 | */ |
| 3474 | error = sysfs_groups_change_owner(kobj, class->dev_groups, kuid, |
| 3475 | kgid); |
| 3476 | if (error) |
| 3477 | return error; |
| 3478 | } |
| 3479 | |
| 3480 | if (type) { |
| 3481 | /* |
| 3482 | * Change the device groups of the device type for @dev to |
| 3483 | * @kuid/@kgid. |
| 3484 | */ |
| 3485 | error = sysfs_groups_change_owner(kobj, type->groups, kuid, |
| 3486 | kgid); |
| 3487 | if (error) |
| 3488 | return error; |
| 3489 | } |
| 3490 | |
| 3491 | /* Change the device groups of @dev to @kuid/@kgid. */ |
| 3492 | error = sysfs_groups_change_owner(kobj, dev->groups, kuid, kgid); |
| 3493 | if (error) |
| 3494 | return error; |
| 3495 | |
| 3496 | if (device_supports_offline(dev) && !dev->offline_disabled) { |
| 3497 | /* Change online device attributes of @dev to @kuid/@kgid. */ |
| 3498 | error = sysfs_file_change_owner(kobj, dev_attr_online.attr.name, |
| 3499 | kuid, kgid); |
| 3500 | if (error) |
| 3501 | return error; |
| 3502 | } |
| 3503 | |
| 3504 | return 0; |
| 3505 | } |
| 3506 | |
| 3507 | /** |
| 3508 | * device_change_owner - change the owner of an existing device. |
| 3509 | * @dev: device. |
| 3510 | * @kuid: new owner's kuid |
| 3511 | * @kgid: new owner's kgid |
| 3512 | * |
| 3513 | * This changes the owner of @dev and its corresponding sysfs entries to |
| 3514 | * @kuid/@kgid. This function closely mirrors how @dev was added via driver |
| 3515 | * core. |
| 3516 | * |
| 3517 | * Returns 0 on success or error code on failure. |
| 3518 | */ |
| 3519 | int device_change_owner(struct device *dev, kuid_t kuid, kgid_t kgid) |
| 3520 | { |
| 3521 | int error; |
| 3522 | struct kobject *kobj = &dev->kobj; |
| 3523 | |
| 3524 | dev = get_device(dev); |
| 3525 | if (!dev) |
| 3526 | return -EINVAL; |
| 3527 | |
| 3528 | /* |
| 3529 | * Change the kobject and the default attributes and groups of the |
| 3530 | * ktype associated with it to @kuid/@kgid. |
| 3531 | */ |
| 3532 | error = sysfs_change_owner(kobj, kuid, kgid); |
| 3533 | if (error) |
| 3534 | goto out; |
| 3535 | |
| 3536 | /* |
| 3537 | * Change the uevent file for @dev to the new owner. The uevent file |
| 3538 | * was created in a separate step when @dev got added and we mirror |
| 3539 | * that step here. |
| 3540 | */ |
| 3541 | error = sysfs_file_change_owner(kobj, dev_attr_uevent.attr.name, kuid, |
| 3542 | kgid); |
| 3543 | if (error) |
| 3544 | goto out; |
| 3545 | |
| 3546 | /* |
| 3547 | * Change the device groups, the device groups associated with the |
| 3548 | * device class, and the groups associated with the device type of @dev |
| 3549 | * to @kuid/@kgid. |
| 3550 | */ |
| 3551 | error = device_attrs_change_owner(dev, kuid, kgid); |
| 3552 | if (error) |
| 3553 | goto out; |
| 3554 | |
| 3555 | #ifdef CONFIG_BLOCK |
| 3556 | if (sysfs_deprecated && dev->class == &block_class) |
| 3557 | goto out; |
| 3558 | #endif |
| 3559 | |
| 3560 | /* |
| 3561 | * Change the owner of the symlink located in the class directory of |
| 3562 | * the device class associated with @dev which points to the actual |
| 3563 | * directory entry for @dev to @kuid/@kgid. This ensures that the |
| 3564 | * symlink shows the same permissions as its target. |
| 3565 | */ |
| 3566 | error = sysfs_link_change_owner(&dev->class->p->subsys.kobj, &dev->kobj, |
| 3567 | dev_name(dev), kuid, kgid); |
| 3568 | if (error) |
| 3569 | goto out; |
| 3570 | |
| 3571 | out: |
| 3572 | put_device(dev); |
| 3573 | return error; |
| 3574 | } |
| 3575 | EXPORT_SYMBOL_GPL(device_change_owner); |
| 3576 | |
Greg Kroah-Hartman | 37b0c02 | 2007-11-26 22:11:55 -0800 | [diff] [blame] | 3577 | /** |
| 3578 | * device_shutdown - call ->shutdown() on each device to shutdown. |
| 3579 | */ |
| 3580 | void device_shutdown(void) |
| 3581 | { |
Benson Leung | f123db8 | 2013-09-24 20:05:11 -0700 | [diff] [blame] | 3582 | struct device *dev, *parent; |
Greg Kroah-Hartman | 37b0c02 | 2007-11-26 22:11:55 -0800 | [diff] [blame] | 3583 | |
Pingfan Liu | 3297c8f | 2018-07-19 13:14:58 +0800 | [diff] [blame] | 3584 | wait_for_device_probe(); |
| 3585 | device_block_probing(); |
| 3586 | |
Rafael J. Wysocki | 65650b3 | 2019-10-09 01:29:10 +0200 | [diff] [blame] | 3587 | cpufreq_suspend(); |
| 3588 | |
Hugh Daschbach | 6245838 | 2010-03-22 10:36:37 -0700 | [diff] [blame] | 3589 | spin_lock(&devices_kset->list_lock); |
| 3590 | /* |
| 3591 | * Walk the devices list backward, shutting down each in turn. |
| 3592 | * Beware that device unplug events may also start pulling |
| 3593 | * devices offline, even as the system is shutting down. |
| 3594 | */ |
| 3595 | while (!list_empty(&devices_kset->list)) { |
| 3596 | dev = list_entry(devices_kset->list.prev, struct device, |
| 3597 | kobj.entry); |
Ming Lei | d1c6c03 | 2012-06-22 18:01:40 +0800 | [diff] [blame] | 3598 | |
| 3599 | /* |
| 3600 | * hold reference count of device's parent to |
| 3601 | * prevent it from being freed because parent's |
| 3602 | * lock is to be held |
| 3603 | */ |
Benson Leung | f123db8 | 2013-09-24 20:05:11 -0700 | [diff] [blame] | 3604 | parent = get_device(dev->parent); |
Hugh Daschbach | 6245838 | 2010-03-22 10:36:37 -0700 | [diff] [blame] | 3605 | get_device(dev); |
| 3606 | /* |
| 3607 | * Make sure the device is off the kset list, in the |
| 3608 | * event that dev->*->shutdown() doesn't remove it. |
| 3609 | */ |
| 3610 | list_del_init(&dev->kobj.entry); |
| 3611 | spin_unlock(&devices_kset->list_lock); |
Alan Stern | fe6b91f | 2011-12-06 23:24:52 +0100 | [diff] [blame] | 3612 | |
Ming Lei | d1c6c03 | 2012-06-22 18:01:40 +0800 | [diff] [blame] | 3613 | /* hold lock to avoid race with probe/release */ |
Benson Leung | f123db8 | 2013-09-24 20:05:11 -0700 | [diff] [blame] | 3614 | if (parent) |
| 3615 | device_lock(parent); |
Ming Lei | d1c6c03 | 2012-06-22 18:01:40 +0800 | [diff] [blame] | 3616 | device_lock(dev); |
| 3617 | |
Alan Stern | fe6b91f | 2011-12-06 23:24:52 +0100 | [diff] [blame] | 3618 | /* Don't allow any more runtime suspends */ |
| 3619 | pm_runtime_get_noresume(dev); |
| 3620 | pm_runtime_barrier(dev); |
Hugh Daschbach | 6245838 | 2010-03-22 10:36:37 -0700 | [diff] [blame] | 3621 | |
Michal Suchanek | 7521621 | 2017-08-11 15:44:43 +0200 | [diff] [blame] | 3622 | if (dev->class && dev->class->shutdown_pre) { |
Josh Zimmerman | f77af15 | 2017-06-25 14:53:23 -0700 | [diff] [blame] | 3623 | if (initcall_debug) |
Michal Suchanek | 7521621 | 2017-08-11 15:44:43 +0200 | [diff] [blame] | 3624 | dev_info(dev, "shutdown_pre\n"); |
| 3625 | dev->class->shutdown_pre(dev); |
| 3626 | } |
| 3627 | if (dev->bus && dev->bus->shutdown) { |
ShuoX Liu | 0246c4f | 2012-11-23 15:14:12 +0800 | [diff] [blame] | 3628 | if (initcall_debug) |
| 3629 | dev_info(dev, "shutdown\n"); |
Greg Kroah-Hartman | 37b0c02 | 2007-11-26 22:11:55 -0800 | [diff] [blame] | 3630 | dev->bus->shutdown(dev); |
| 3631 | } else if (dev->driver && dev->driver->shutdown) { |
ShuoX Liu | 0246c4f | 2012-11-23 15:14:12 +0800 | [diff] [blame] | 3632 | if (initcall_debug) |
| 3633 | dev_info(dev, "shutdown\n"); |
Greg Kroah-Hartman | 37b0c02 | 2007-11-26 22:11:55 -0800 | [diff] [blame] | 3634 | dev->driver->shutdown(dev); |
| 3635 | } |
Ming Lei | d1c6c03 | 2012-06-22 18:01:40 +0800 | [diff] [blame] | 3636 | |
| 3637 | device_unlock(dev); |
Benson Leung | f123db8 | 2013-09-24 20:05:11 -0700 | [diff] [blame] | 3638 | if (parent) |
| 3639 | device_unlock(parent); |
Ming Lei | d1c6c03 | 2012-06-22 18:01:40 +0800 | [diff] [blame] | 3640 | |
Hugh Daschbach | 6245838 | 2010-03-22 10:36:37 -0700 | [diff] [blame] | 3641 | put_device(dev); |
Benson Leung | f123db8 | 2013-09-24 20:05:11 -0700 | [diff] [blame] | 3642 | put_device(parent); |
Hugh Daschbach | 6245838 | 2010-03-22 10:36:37 -0700 | [diff] [blame] | 3643 | |
| 3644 | spin_lock(&devices_kset->list_lock); |
Greg Kroah-Hartman | 37b0c02 | 2007-11-26 22:11:55 -0800 | [diff] [blame] | 3645 | } |
Hugh Daschbach | 6245838 | 2010-03-22 10:36:37 -0700 | [diff] [blame] | 3646 | spin_unlock(&devices_kset->list_lock); |
Greg Kroah-Hartman | 37b0c02 | 2007-11-26 22:11:55 -0800 | [diff] [blame] | 3647 | } |
Joe Perches | 99bcf21 | 2010-06-27 01:02:34 +0000 | [diff] [blame] | 3648 | |
| 3649 | /* |
| 3650 | * Device logging functions |
| 3651 | */ |
| 3652 | |
| 3653 | #ifdef CONFIG_PRINTK |
Joe Perches | 666f355 | 2012-09-12 20:14:11 -0700 | [diff] [blame] | 3654 | static int |
| 3655 | create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen) |
Joe Perches | 99bcf21 | 2010-06-27 01:02:34 +0000 | [diff] [blame] | 3656 | { |
Kay Sievers | c4e00da | 2012-05-03 02:29:59 +0200 | [diff] [blame] | 3657 | const char *subsys; |
Joe Perches | 798efc6 | 2012-09-12 20:11:29 -0700 | [diff] [blame] | 3658 | size_t pos = 0; |
Joe Perches | 99bcf21 | 2010-06-27 01:02:34 +0000 | [diff] [blame] | 3659 | |
Kay Sievers | c4e00da | 2012-05-03 02:29:59 +0200 | [diff] [blame] | 3660 | if (dev->class) |
| 3661 | subsys = dev->class->name; |
| 3662 | else if (dev->bus) |
| 3663 | subsys = dev->bus->name; |
| 3664 | else |
Joe Perches | 798efc6 | 2012-09-12 20:11:29 -0700 | [diff] [blame] | 3665 | return 0; |
Kay Sievers | c4e00da | 2012-05-03 02:29:59 +0200 | [diff] [blame] | 3666 | |
Joe Perches | 798efc6 | 2012-09-12 20:11:29 -0700 | [diff] [blame] | 3667 | pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys); |
Ben Hutchings | 655e5b7 | 2014-08-26 00:34:44 -0700 | [diff] [blame] | 3668 | if (pos >= hdrlen) |
| 3669 | goto overflow; |
Kay Sievers | c4e00da | 2012-05-03 02:29:59 +0200 | [diff] [blame] | 3670 | |
| 3671 | /* |
| 3672 | * Add device identifier DEVICE=: |
| 3673 | * b12:8 block dev_t |
| 3674 | * c127:3 char dev_t |
| 3675 | * n8 netdev ifindex |
| 3676 | * +sound:card0 subsystem:devname |
| 3677 | */ |
| 3678 | if (MAJOR(dev->devt)) { |
| 3679 | char c; |
| 3680 | |
| 3681 | if (strcmp(subsys, "block") == 0) |
| 3682 | c = 'b'; |
| 3683 | else |
| 3684 | c = 'c'; |
Joe Perches | 798efc6 | 2012-09-12 20:11:29 -0700 | [diff] [blame] | 3685 | pos++; |
| 3686 | pos += snprintf(hdr + pos, hdrlen - pos, |
| 3687 | "DEVICE=%c%u:%u", |
| 3688 | c, MAJOR(dev->devt), MINOR(dev->devt)); |
Kay Sievers | c4e00da | 2012-05-03 02:29:59 +0200 | [diff] [blame] | 3689 | } else if (strcmp(subsys, "net") == 0) { |
| 3690 | struct net_device *net = to_net_dev(dev); |
| 3691 | |
Joe Perches | 798efc6 | 2012-09-12 20:11:29 -0700 | [diff] [blame] | 3692 | pos++; |
| 3693 | pos += snprintf(hdr + pos, hdrlen - pos, |
| 3694 | "DEVICE=n%u", net->ifindex); |
Kay Sievers | c4e00da | 2012-05-03 02:29:59 +0200 | [diff] [blame] | 3695 | } else { |
Joe Perches | 798efc6 | 2012-09-12 20:11:29 -0700 | [diff] [blame] | 3696 | pos++; |
| 3697 | pos += snprintf(hdr + pos, hdrlen - pos, |
| 3698 | "DEVICE=+%s:%s", subsys, dev_name(dev)); |
Kay Sievers | c4e00da | 2012-05-03 02:29:59 +0200 | [diff] [blame] | 3699 | } |
Jim Cromie | af7f215 | 2012-07-19 13:46:21 -0600 | [diff] [blame] | 3700 | |
Ben Hutchings | 655e5b7 | 2014-08-26 00:34:44 -0700 | [diff] [blame] | 3701 | if (pos >= hdrlen) |
| 3702 | goto overflow; |
| 3703 | |
Joe Perches | 798efc6 | 2012-09-12 20:11:29 -0700 | [diff] [blame] | 3704 | return pos; |
Ben Hutchings | 655e5b7 | 2014-08-26 00:34:44 -0700 | [diff] [blame] | 3705 | |
| 3706 | overflow: |
| 3707 | dev_WARN(dev, "device/subsystem name too long"); |
| 3708 | return 0; |
Joe Perches | 99bcf21 | 2010-06-27 01:02:34 +0000 | [diff] [blame] | 3709 | } |
Joe Perches | 798efc6 | 2012-09-12 20:11:29 -0700 | [diff] [blame] | 3710 | |
Joe Perches | 05e4e5b | 2012-09-12 20:13:37 -0700 | [diff] [blame] | 3711 | int dev_vprintk_emit(int level, const struct device *dev, |
| 3712 | const char *fmt, va_list args) |
| 3713 | { |
| 3714 | char hdr[128]; |
| 3715 | size_t hdrlen; |
| 3716 | |
| 3717 | hdrlen = create_syslog_header(dev, hdr, sizeof(hdr)); |
| 3718 | |
| 3719 | return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args); |
| 3720 | } |
| 3721 | EXPORT_SYMBOL(dev_vprintk_emit); |
| 3722 | |
| 3723 | int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...) |
| 3724 | { |
| 3725 | va_list args; |
| 3726 | int r; |
| 3727 | |
| 3728 | va_start(args, fmt); |
| 3729 | |
| 3730 | r = dev_vprintk_emit(level, dev, fmt, args); |
| 3731 | |
| 3732 | va_end(args); |
| 3733 | |
| 3734 | return r; |
| 3735 | } |
| 3736 | EXPORT_SYMBOL(dev_printk_emit); |
| 3737 | |
Joe Perches | d1f1052 | 2014-12-25 15:07:04 -0800 | [diff] [blame] | 3738 | static void __dev_printk(const char *level, const struct device *dev, |
Joe Perches | 798efc6 | 2012-09-12 20:11:29 -0700 | [diff] [blame] | 3739 | struct va_format *vaf) |
| 3740 | { |
Joe Perches | d1f1052 | 2014-12-25 15:07:04 -0800 | [diff] [blame] | 3741 | if (dev) |
| 3742 | dev_printk_emit(level[1] - '0', dev, "%s %s: %pV", |
| 3743 | dev_driver_string(dev), dev_name(dev), vaf); |
| 3744 | else |
| 3745 | printk("%s(NULL device *): %pV", level, vaf); |
Joe Perches | 798efc6 | 2012-09-12 20:11:29 -0700 | [diff] [blame] | 3746 | } |
Joe Perches | 99bcf21 | 2010-06-27 01:02:34 +0000 | [diff] [blame] | 3747 | |
Joe Perches | d1f1052 | 2014-12-25 15:07:04 -0800 | [diff] [blame] | 3748 | void dev_printk(const char *level, const struct device *dev, |
| 3749 | const char *fmt, ...) |
Joe Perches | 99bcf21 | 2010-06-27 01:02:34 +0000 | [diff] [blame] | 3750 | { |
| 3751 | struct va_format vaf; |
| 3752 | va_list args; |
Joe Perches | 99bcf21 | 2010-06-27 01:02:34 +0000 | [diff] [blame] | 3753 | |
| 3754 | va_start(args, fmt); |
| 3755 | |
| 3756 | vaf.fmt = fmt; |
| 3757 | vaf.va = &args; |
| 3758 | |
Joe Perches | d1f1052 | 2014-12-25 15:07:04 -0800 | [diff] [blame] | 3759 | __dev_printk(level, dev, &vaf); |
Joe Perches | 798efc6 | 2012-09-12 20:11:29 -0700 | [diff] [blame] | 3760 | |
Joe Perches | 99bcf21 | 2010-06-27 01:02:34 +0000 | [diff] [blame] | 3761 | va_end(args); |
Joe Perches | 99bcf21 | 2010-06-27 01:02:34 +0000 | [diff] [blame] | 3762 | } |
| 3763 | EXPORT_SYMBOL(dev_printk); |
| 3764 | |
| 3765 | #define define_dev_printk_level(func, kern_level) \ |
Joe Perches | d1f1052 | 2014-12-25 15:07:04 -0800 | [diff] [blame] | 3766 | void func(const struct device *dev, const char *fmt, ...) \ |
Joe Perches | 99bcf21 | 2010-06-27 01:02:34 +0000 | [diff] [blame] | 3767 | { \ |
| 3768 | struct va_format vaf; \ |
| 3769 | va_list args; \ |
Joe Perches | 99bcf21 | 2010-06-27 01:02:34 +0000 | [diff] [blame] | 3770 | \ |
| 3771 | va_start(args, fmt); \ |
| 3772 | \ |
| 3773 | vaf.fmt = fmt; \ |
| 3774 | vaf.va = &args; \ |
| 3775 | \ |
Joe Perches | d1f1052 | 2014-12-25 15:07:04 -0800 | [diff] [blame] | 3776 | __dev_printk(kern_level, dev, &vaf); \ |
Joe Perches | 798efc6 | 2012-09-12 20:11:29 -0700 | [diff] [blame] | 3777 | \ |
Joe Perches | 99bcf21 | 2010-06-27 01:02:34 +0000 | [diff] [blame] | 3778 | va_end(args); \ |
Joe Perches | 99bcf21 | 2010-06-27 01:02:34 +0000 | [diff] [blame] | 3779 | } \ |
| 3780 | EXPORT_SYMBOL(func); |
| 3781 | |
Joe Perches | 663336e | 2018-05-09 08:15:46 -0700 | [diff] [blame] | 3782 | define_dev_printk_level(_dev_emerg, KERN_EMERG); |
| 3783 | define_dev_printk_level(_dev_alert, KERN_ALERT); |
| 3784 | define_dev_printk_level(_dev_crit, KERN_CRIT); |
| 3785 | define_dev_printk_level(_dev_err, KERN_ERR); |
| 3786 | define_dev_printk_level(_dev_warn, KERN_WARNING); |
| 3787 | define_dev_printk_level(_dev_notice, KERN_NOTICE); |
Joe Perches | 99bcf21 | 2010-06-27 01:02:34 +0000 | [diff] [blame] | 3788 | define_dev_printk_level(_dev_info, KERN_INFO); |
| 3789 | |
| 3790 | #endif |
Rafael J. Wysocki | 97badf8 | 2015-04-03 23:23:37 +0200 | [diff] [blame] | 3791 | |
| 3792 | static inline bool fwnode_is_primary(struct fwnode_handle *fwnode) |
| 3793 | { |
| 3794 | return fwnode && !IS_ERR(fwnode->secondary); |
| 3795 | } |
| 3796 | |
| 3797 | /** |
| 3798 | * set_primary_fwnode - Change the primary firmware node of a given device. |
| 3799 | * @dev: Device to handle. |
| 3800 | * @fwnode: New primary firmware node of the device. |
| 3801 | * |
| 3802 | * Set the device's firmware node pointer to @fwnode, but if a secondary |
| 3803 | * firmware node of the device is present, preserve it. |
| 3804 | */ |
| 3805 | void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode) |
| 3806 | { |
| 3807 | if (fwnode) { |
| 3808 | struct fwnode_handle *fn = dev->fwnode; |
| 3809 | |
| 3810 | if (fwnode_is_primary(fn)) |
| 3811 | fn = fn->secondary; |
| 3812 | |
Mika Westerberg | 55f89a8 | 2015-11-30 17:11:39 +0200 | [diff] [blame] | 3813 | if (fn) { |
| 3814 | WARN_ON(fwnode->secondary); |
| 3815 | fwnode->secondary = fn; |
| 3816 | } |
Rafael J. Wysocki | 97badf8 | 2015-04-03 23:23:37 +0200 | [diff] [blame] | 3817 | dev->fwnode = fwnode; |
| 3818 | } else { |
| 3819 | dev->fwnode = fwnode_is_primary(dev->fwnode) ? |
| 3820 | dev->fwnode->secondary : NULL; |
| 3821 | } |
| 3822 | } |
| 3823 | EXPORT_SYMBOL_GPL(set_primary_fwnode); |
| 3824 | |
| 3825 | /** |
| 3826 | * set_secondary_fwnode - Change the secondary firmware node of a given device. |
| 3827 | * @dev: Device to handle. |
| 3828 | * @fwnode: New secondary firmware node of the device. |
| 3829 | * |
| 3830 | * If a primary firmware node of the device is present, set its secondary |
| 3831 | * pointer to @fwnode. Otherwise, set the device's firmware node pointer to |
| 3832 | * @fwnode. |
| 3833 | */ |
| 3834 | void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode) |
| 3835 | { |
| 3836 | if (fwnode) |
| 3837 | fwnode->secondary = ERR_PTR(-ENODEV); |
| 3838 | |
| 3839 | if (fwnode_is_primary(dev->fwnode)) |
| 3840 | dev->fwnode->secondary = fwnode; |
| 3841 | else |
| 3842 | dev->fwnode = fwnode; |
| 3843 | } |
Johan Hovold | 4e75e1d | 2017-06-06 17:59:00 +0200 | [diff] [blame] | 3844 | |
| 3845 | /** |
| 3846 | * device_set_of_node_from_dev - reuse device-tree node of another device |
| 3847 | * @dev: device whose device-tree node is being set |
| 3848 | * @dev2: device whose device-tree node is being reused |
| 3849 | * |
| 3850 | * Takes another reference to the new device-tree node after first dropping |
| 3851 | * any reference held to the old node. |
| 3852 | */ |
| 3853 | void device_set_of_node_from_dev(struct device *dev, const struct device *dev2) |
| 3854 | { |
| 3855 | of_node_put(dev->of_node); |
| 3856 | dev->of_node = of_node_get(dev2->of_node); |
| 3857 | dev->of_node_reused = true; |
| 3858 | } |
| 3859 | EXPORT_SYMBOL_GPL(device_set_of_node_from_dev); |
Suzuki K Poulose | 65b6668 | 2019-06-14 18:54:01 +0100 | [diff] [blame] | 3860 | |
Suzuki K Poulose | 6cda08a | 2019-07-23 23:18:32 +0100 | [diff] [blame] | 3861 | int device_match_name(struct device *dev, const void *name) |
| 3862 | { |
| 3863 | return sysfs_streq(dev_name(dev), name); |
| 3864 | } |
| 3865 | EXPORT_SYMBOL_GPL(device_match_name); |
| 3866 | |
Suzuki K Poulose | 65b6668 | 2019-06-14 18:54:01 +0100 | [diff] [blame] | 3867 | int device_match_of_node(struct device *dev, const void *np) |
| 3868 | { |
| 3869 | return dev->of_node == np; |
| 3870 | } |
| 3871 | EXPORT_SYMBOL_GPL(device_match_of_node); |
Suzuki K Poulose | 67843bb | 2019-07-23 23:18:34 +0100 | [diff] [blame] | 3872 | |
| 3873 | int device_match_fwnode(struct device *dev, const void *fwnode) |
| 3874 | { |
| 3875 | return dev_fwnode(dev) == fwnode; |
| 3876 | } |
| 3877 | EXPORT_SYMBOL_GPL(device_match_fwnode); |
Suzuki K Poulose | 4495dfd | 2019-07-23 23:18:35 +0100 | [diff] [blame] | 3878 | |
| 3879 | int device_match_devt(struct device *dev, const void *pdevt) |
| 3880 | { |
| 3881 | return dev->devt == *(dev_t *)pdevt; |
| 3882 | } |
| 3883 | EXPORT_SYMBOL_GPL(device_match_devt); |
Suzuki K Poulose | 0050014 | 2019-07-23 23:18:36 +0100 | [diff] [blame] | 3884 | |
| 3885 | int device_match_acpi_dev(struct device *dev, const void *adev) |
| 3886 | { |
| 3887 | return ACPI_COMPANION(dev) == adev; |
| 3888 | } |
| 3889 | EXPORT_SYMBOL(device_match_acpi_dev); |
Suzuki K Poulose | 6bf85ba | 2019-07-23 23:18:37 +0100 | [diff] [blame] | 3890 | |
| 3891 | int device_match_any(struct device *dev, const void *unused) |
| 3892 | { |
| 3893 | return 1; |
| 3894 | } |
| 3895 | EXPORT_SYMBOL_GPL(device_match_any); |