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> |
Oliver Neukum | b853001 | 2020-09-16 21:15:44 +0200 | [diff] [blame] | 29 | #include <linux/sched/mm.h> |
Claire Chang | 69031f5 | 2021-06-19 11:40:34 +0800 | [diff] [blame] | 30 | #include <linux/swiotlb.h> |
Greg Kroah-Hartman | 6396768 | 2013-08-27 10:24:15 -0700 | [diff] [blame] | 31 | #include <linux/sysfs.h> |
Christoph Hellwig | 6d4e9a8 | 2021-02-10 10:56:39 +0100 | [diff] [blame] | 32 | #include <linux/dma-map-ops.h> /* for dma_default_coherent */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
| 34 | #include "base.h" |
| 35 | #include "power/power.h" |
| 36 | |
Andi Kleen | e52eec1 | 2010-09-08 16:54:17 +0200 | [diff] [blame] | 37 | #ifdef CONFIG_SYSFS_DEPRECATED |
| 38 | #ifdef CONFIG_SYSFS_DEPRECATED_V2 |
| 39 | long sysfs_deprecated = 1; |
| 40 | #else |
| 41 | long sysfs_deprecated = 0; |
| 42 | #endif |
Hanjun Guo | 3454bf9 | 2013-08-17 20:42:24 +0800 | [diff] [blame] | 43 | static int __init sysfs_deprecated_setup(char *arg) |
Andi Kleen | e52eec1 | 2010-09-08 16:54:17 +0200 | [diff] [blame] | 44 | { |
Jingoo Han | 34da5e6 | 2013-07-26 13:10:22 +0900 | [diff] [blame] | 45 | return kstrtol(arg, 10, &sysfs_deprecated); |
Andi Kleen | e52eec1 | 2010-09-08 16:54:17 +0200 | [diff] [blame] | 46 | } |
| 47 | early_param("sysfs.deprecated", sysfs_deprecated_setup); |
| 48 | #endif |
| 49 | |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 50 | /* Device links support. */ |
Saravana Kannan | fc5a251 | 2019-09-04 14:11:23 -0700 | [diff] [blame] | 51 | static LIST_HEAD(deferred_sync); |
| 52 | static unsigned int defer_sync_state_count = 1; |
Saravana Kannan | 7b337cb | 2020-11-20 18:02:23 -0800 | [diff] [blame] | 53 | static DEFINE_MUTEX(fwnode_link_lock); |
Saravana Kannan | 25ac86c | 2020-11-20 18:02:28 -0800 | [diff] [blame] | 54 | static bool fw_devlink_is_permissive(void); |
Saravana Kannan | d46f3e3 | 2021-04-01 21:03:41 -0700 | [diff] [blame] | 55 | static bool fw_devlink_drv_reg_done; |
Saravana Kannan | 7b337cb | 2020-11-20 18:02:23 -0800 | [diff] [blame] | 56 | |
| 57 | /** |
| 58 | * fwnode_link_add - Create a link between two fwnode_handles. |
| 59 | * @con: Consumer end of the link. |
| 60 | * @sup: Supplier end of the link. |
| 61 | * |
| 62 | * Create a fwnode link between fwnode handles @con and @sup. The fwnode link |
| 63 | * represents the detail that the firmware lists @sup fwnode as supplying a |
| 64 | * resource to @con. |
| 65 | * |
| 66 | * The driver core will use the fwnode link to create a device link between the |
| 67 | * two device objects corresponding to @con and @sup when they are created. The |
| 68 | * driver core will automatically delete the fwnode link between @con and @sup |
| 69 | * after doing that. |
| 70 | * |
| 71 | * Attempts to create duplicate links between the same pair of fwnode handles |
| 72 | * are ignored and there is no reference counting. |
| 73 | */ |
| 74 | int fwnode_link_add(struct fwnode_handle *con, struct fwnode_handle *sup) |
| 75 | { |
| 76 | struct fwnode_link *link; |
| 77 | int ret = 0; |
| 78 | |
| 79 | mutex_lock(&fwnode_link_lock); |
| 80 | |
| 81 | list_for_each_entry(link, &sup->consumers, s_hook) |
| 82 | if (link->consumer == con) |
| 83 | goto out; |
| 84 | |
| 85 | link = kzalloc(sizeof(*link), GFP_KERNEL); |
| 86 | if (!link) { |
| 87 | ret = -ENOMEM; |
| 88 | goto out; |
| 89 | } |
| 90 | |
| 91 | link->supplier = sup; |
| 92 | INIT_LIST_HEAD(&link->s_hook); |
| 93 | link->consumer = con; |
| 94 | INIT_LIST_HEAD(&link->c_hook); |
| 95 | |
| 96 | list_add(&link->s_hook, &sup->consumers); |
| 97 | list_add(&link->c_hook, &con->suppliers); |
Saravana Kannan | ebd6823 | 2021-09-15 10:28:07 -0700 | [diff] [blame] | 98 | pr_debug("%pfwP Linked as a fwnode consumer to %pfwP\n", |
| 99 | con, sup); |
Saravana Kannan | 7b337cb | 2020-11-20 18:02:23 -0800 | [diff] [blame] | 100 | out: |
| 101 | mutex_unlock(&fwnode_link_lock); |
| 102 | |
| 103 | return ret; |
| 104 | } |
| 105 | |
| 106 | /** |
Saravana Kannan | 76f1308 | 2021-09-15 10:28:06 -0700 | [diff] [blame] | 107 | * __fwnode_link_del - Delete a link between two fwnode_handles. |
| 108 | * @link: the fwnode_link to be deleted |
| 109 | * |
| 110 | * The fwnode_link_lock needs to be held when this function is called. |
| 111 | */ |
| 112 | static void __fwnode_link_del(struct fwnode_link *link) |
| 113 | { |
Saravana Kannan | ebd6823 | 2021-09-15 10:28:07 -0700 | [diff] [blame] | 114 | pr_debug("%pfwP Dropping the fwnode link to %pfwP\n", |
| 115 | link->consumer, link->supplier); |
Saravana Kannan | 76f1308 | 2021-09-15 10:28:06 -0700 | [diff] [blame] | 116 | list_del(&link->s_hook); |
| 117 | list_del(&link->c_hook); |
| 118 | kfree(link); |
| 119 | } |
| 120 | |
| 121 | /** |
Saravana Kannan | 7b337cb | 2020-11-20 18:02:23 -0800 | [diff] [blame] | 122 | * fwnode_links_purge_suppliers - Delete all supplier links of fwnode_handle. |
| 123 | * @fwnode: fwnode whose supplier links need to be deleted |
| 124 | * |
| 125 | * Deletes all supplier links connecting directly to @fwnode. |
| 126 | */ |
| 127 | static void fwnode_links_purge_suppliers(struct fwnode_handle *fwnode) |
| 128 | { |
| 129 | struct fwnode_link *link, *tmp; |
| 130 | |
| 131 | mutex_lock(&fwnode_link_lock); |
Saravana Kannan | 76f1308 | 2021-09-15 10:28:06 -0700 | [diff] [blame] | 132 | list_for_each_entry_safe(link, tmp, &fwnode->suppliers, c_hook) |
| 133 | __fwnode_link_del(link); |
Saravana Kannan | 7b337cb | 2020-11-20 18:02:23 -0800 | [diff] [blame] | 134 | mutex_unlock(&fwnode_link_lock); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * fwnode_links_purge_consumers - Delete all consumer links of fwnode_handle. |
| 139 | * @fwnode: fwnode whose consumer links need to be deleted |
| 140 | * |
| 141 | * Deletes all consumer links connecting directly to @fwnode. |
| 142 | */ |
| 143 | static void fwnode_links_purge_consumers(struct fwnode_handle *fwnode) |
| 144 | { |
| 145 | struct fwnode_link *link, *tmp; |
| 146 | |
| 147 | mutex_lock(&fwnode_link_lock); |
Saravana Kannan | 76f1308 | 2021-09-15 10:28:06 -0700 | [diff] [blame] | 148 | list_for_each_entry_safe(link, tmp, &fwnode->consumers, s_hook) |
| 149 | __fwnode_link_del(link); |
Saravana Kannan | 7b337cb | 2020-11-20 18:02:23 -0800 | [diff] [blame] | 150 | mutex_unlock(&fwnode_link_lock); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * fwnode_links_purge - Delete all links connected to a fwnode_handle. |
| 155 | * @fwnode: fwnode whose links needs to be deleted |
| 156 | * |
| 157 | * Deletes all links connecting directly to a fwnode. |
| 158 | */ |
| 159 | void fwnode_links_purge(struct fwnode_handle *fwnode) |
| 160 | { |
| 161 | fwnode_links_purge_suppliers(fwnode); |
| 162 | fwnode_links_purge_consumers(fwnode); |
| 163 | } |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 164 | |
Saravana Kannan | 28ec344 | 2021-05-05 17:44:22 -0700 | [diff] [blame] | 165 | void fw_devlink_purge_absent_suppliers(struct fwnode_handle *fwnode) |
Saravana Kannan | 9528e0d | 2021-02-05 14:26:37 -0800 | [diff] [blame] | 166 | { |
| 167 | struct fwnode_handle *child; |
| 168 | |
| 169 | /* Don't purge consumer links of an added child */ |
| 170 | if (fwnode->dev) |
| 171 | return; |
| 172 | |
| 173 | fwnode->flags |= FWNODE_FLAG_NOT_DEVICE; |
| 174 | fwnode_links_purge_consumers(fwnode); |
| 175 | |
| 176 | fwnode_for_each_available_child_node(fwnode, child) |
| 177 | fw_devlink_purge_absent_suppliers(child); |
| 178 | } |
Saravana Kannan | 28ec344 | 2021-05-05 17:44:22 -0700 | [diff] [blame] | 179 | EXPORT_SYMBOL_GPL(fw_devlink_purge_absent_suppliers); |
Saravana Kannan | 9528e0d | 2021-02-05 14:26:37 -0800 | [diff] [blame] | 180 | |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 181 | #ifdef CONFIG_SRCU |
| 182 | static DEFINE_MUTEX(device_links_lock); |
| 183 | DEFINE_STATIC_SRCU(device_links_srcu); |
| 184 | |
| 185 | static inline void device_links_write_lock(void) |
| 186 | { |
| 187 | mutex_lock(&device_links_lock); |
| 188 | } |
| 189 | |
| 190 | static inline void device_links_write_unlock(void) |
| 191 | { |
| 192 | mutex_unlock(&device_links_lock); |
| 193 | } |
| 194 | |
Jules Irenge | 68464d7 | 2020-02-14 20:47:29 +0000 | [diff] [blame] | 195 | int device_links_read_lock(void) __acquires(&device_links_srcu) |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 196 | { |
| 197 | return srcu_read_lock(&device_links_srcu); |
| 198 | } |
| 199 | |
Jules Irenge | ab7789c5 | 2020-02-14 20:47:30 +0000 | [diff] [blame] | 200 | void device_links_read_unlock(int idx) __releases(&device_links_srcu) |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 201 | { |
| 202 | srcu_read_unlock(&device_links_srcu, idx); |
| 203 | } |
Joel Fernandes (Google) | c2fa1e1 | 2019-07-16 18:12:25 -0400 | [diff] [blame] | 204 | |
| 205 | int device_links_read_lock_held(void) |
| 206 | { |
| 207 | return srcu_read_lock_held(&device_links_srcu); |
| 208 | } |
Rafael J. Wysocki | 80dd33c | 2021-05-14 14:10:15 +0200 | [diff] [blame] | 209 | |
| 210 | static void device_link_synchronize_removal(void) |
| 211 | { |
| 212 | synchronize_srcu(&device_links_srcu); |
| 213 | } |
Rafael J. Wysocki | 0c87131 | 2021-05-14 14:11:19 +0200 | [diff] [blame] | 214 | |
| 215 | static void device_link_remove_from_lists(struct device_link *link) |
| 216 | { |
| 217 | list_del_rcu(&link->s_node); |
| 218 | list_del_rcu(&link->c_node); |
| 219 | } |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 220 | #else /* !CONFIG_SRCU */ |
| 221 | static DECLARE_RWSEM(device_links_lock); |
| 222 | |
| 223 | static inline void device_links_write_lock(void) |
| 224 | { |
| 225 | down_write(&device_links_lock); |
| 226 | } |
| 227 | |
| 228 | static inline void device_links_write_unlock(void) |
| 229 | { |
| 230 | up_write(&device_links_lock); |
| 231 | } |
| 232 | |
| 233 | int device_links_read_lock(void) |
| 234 | { |
| 235 | down_read(&device_links_lock); |
| 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | void device_links_read_unlock(int not_used) |
| 240 | { |
| 241 | up_read(&device_links_lock); |
| 242 | } |
Joel Fernandes (Google) | c2fa1e1 | 2019-07-16 18:12:25 -0400 | [diff] [blame] | 243 | |
| 244 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 245 | int device_links_read_lock_held(void) |
| 246 | { |
| 247 | return lockdep_is_held(&device_links_lock); |
| 248 | } |
| 249 | #endif |
Rafael J. Wysocki | 80dd33c | 2021-05-14 14:10:15 +0200 | [diff] [blame] | 250 | |
| 251 | static inline void device_link_synchronize_removal(void) |
| 252 | { |
| 253 | } |
Rafael J. Wysocki | 0c87131 | 2021-05-14 14:11:19 +0200 | [diff] [blame] | 254 | |
| 255 | static void device_link_remove_from_lists(struct device_link *link) |
| 256 | { |
| 257 | list_del(&link->s_node); |
| 258 | list_del(&link->c_node); |
| 259 | } |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 260 | #endif /* !CONFIG_SRCU */ |
| 261 | |
Rafael J. Wysocki | 3d1cf43 | 2021-01-15 19:30:51 +0100 | [diff] [blame] | 262 | static bool device_is_ancestor(struct device *dev, struct device *target) |
| 263 | { |
| 264 | while (target->parent) { |
| 265 | target = target->parent; |
| 266 | if (dev == target) |
| 267 | return true; |
| 268 | } |
| 269 | return false; |
| 270 | } |
| 271 | |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 272 | /** |
| 273 | * device_is_dependent - Check if one device depends on another one |
| 274 | * @dev: Device to check dependencies for. |
| 275 | * @target: Device to check against. |
| 276 | * |
| 277 | * Check if @target depends on @dev or any device dependent on it (its child or |
| 278 | * its consumer etc). Return 1 if that is the case or 0 otherwise. |
| 279 | */ |
Saravana Kannan | 7d34ca3 | 2020-06-09 18:19:33 -0700 | [diff] [blame] | 280 | int device_is_dependent(struct device *dev, void *target) |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 281 | { |
| 282 | struct device_link *link; |
| 283 | int ret; |
| 284 | |
Rafael J. Wysocki | 3d1cf43 | 2021-01-15 19:30:51 +0100 | [diff] [blame] | 285 | /* |
| 286 | * The "ancestors" check is needed to catch the case when the target |
| 287 | * device has not been completely initialized yet and it is still |
| 288 | * missing from the list of children of its parent device. |
| 289 | */ |
| 290 | if (dev == target || device_is_ancestor(dev, target)) |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 291 | return 1; |
| 292 | |
| 293 | ret = device_for_each_child(dev, target, device_is_dependent); |
| 294 | if (ret) |
| 295 | return ret; |
| 296 | |
| 297 | list_for_each_entry(link, &dev->links.consumers, s_node) { |
Saravana Kannan | 4b9bbb2 | 2020-12-17 19:17:00 -0800 | [diff] [blame] | 298 | if ((link->flags & ~DL_FLAG_INFERRED) == |
| 299 | (DL_FLAG_SYNC_STATE_ONLY | DL_FLAG_MANAGED)) |
Saravana Kannan | 05ef983 | 2019-10-28 15:00:22 -0700 | [diff] [blame] | 300 | continue; |
| 301 | |
Benjamin Gaignard | e16f4f3 | 2018-07-16 13:37:44 +0200 | [diff] [blame] | 302 | if (link->consumer == target) |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 303 | return 1; |
| 304 | |
| 305 | ret = device_is_dependent(link->consumer, target); |
| 306 | if (ret) |
| 307 | break; |
| 308 | } |
| 309 | return ret; |
| 310 | } |
| 311 | |
Rafael J. Wysocki | 515db26 | 2019-07-16 17:21:06 +0200 | [diff] [blame] | 312 | static void device_link_init_status(struct device_link *link, |
| 313 | struct device *consumer, |
| 314 | struct device *supplier) |
| 315 | { |
| 316 | switch (supplier->links.status) { |
| 317 | case DL_DEV_PROBING: |
| 318 | switch (consumer->links.status) { |
| 319 | case DL_DEV_PROBING: |
| 320 | /* |
| 321 | * A consumer driver can create a link to a supplier |
| 322 | * that has not completed its probing yet as long as it |
| 323 | * knows that the supplier is already functional (for |
| 324 | * example, it has just acquired some resources from the |
| 325 | * supplier). |
| 326 | */ |
| 327 | link->status = DL_STATE_CONSUMER_PROBE; |
| 328 | break; |
| 329 | default: |
| 330 | link->status = DL_STATE_DORMANT; |
| 331 | break; |
| 332 | } |
| 333 | break; |
| 334 | case DL_DEV_DRIVER_BOUND: |
| 335 | switch (consumer->links.status) { |
| 336 | case DL_DEV_PROBING: |
| 337 | link->status = DL_STATE_CONSUMER_PROBE; |
| 338 | break; |
| 339 | case DL_DEV_DRIVER_BOUND: |
| 340 | link->status = DL_STATE_ACTIVE; |
| 341 | break; |
| 342 | default: |
| 343 | link->status = DL_STATE_AVAILABLE; |
| 344 | break; |
| 345 | } |
| 346 | break; |
| 347 | case DL_DEV_UNBINDING: |
| 348 | link->status = DL_STATE_SUPPLIER_UNBIND; |
| 349 | break; |
| 350 | default: |
| 351 | link->status = DL_STATE_DORMANT; |
| 352 | break; |
| 353 | } |
| 354 | } |
| 355 | |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 356 | static int device_reorder_to_tail(struct device *dev, void *not_used) |
| 357 | { |
| 358 | struct device_link *link; |
| 359 | |
| 360 | /* |
| 361 | * Devices that have not been registered yet will be put to the ends |
| 362 | * of the lists during the registration, so skip them here. |
| 363 | */ |
| 364 | if (device_is_registered(dev)) |
| 365 | devices_kset_move_last(dev); |
| 366 | |
| 367 | if (device_pm_initialized(dev)) |
| 368 | device_pm_move_last(dev); |
| 369 | |
| 370 | device_for_each_child(dev, NULL, device_reorder_to_tail); |
Saravana Kannan | 05ef983 | 2019-10-28 15:00:22 -0700 | [diff] [blame] | 371 | list_for_each_entry(link, &dev->links.consumers, s_node) { |
Saravana Kannan | 4b9bbb2 | 2020-12-17 19:17:00 -0800 | [diff] [blame] | 372 | if ((link->flags & ~DL_FLAG_INFERRED) == |
| 373 | (DL_FLAG_SYNC_STATE_ONLY | DL_FLAG_MANAGED)) |
Saravana Kannan | 05ef983 | 2019-10-28 15:00:22 -0700 | [diff] [blame] | 374 | continue; |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 375 | device_reorder_to_tail(link->consumer, NULL); |
Saravana Kannan | 05ef983 | 2019-10-28 15:00:22 -0700 | [diff] [blame] | 376 | } |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 377 | |
| 378 | return 0; |
| 379 | } |
| 380 | |
| 381 | /** |
Feng Kan | 494fd7b | 2018-04-10 16:57:06 -0700 | [diff] [blame] | 382 | * device_pm_move_to_tail - Move set of devices to the end of device lists |
| 383 | * @dev: Device to move |
| 384 | * |
| 385 | * This is a device_reorder_to_tail() wrapper taking the requisite locks. |
| 386 | * |
| 387 | * It moves the @dev along with all of its children and all of its consumers |
| 388 | * to the ends of the device_kset and dpm_list, recursively. |
| 389 | */ |
| 390 | void device_pm_move_to_tail(struct device *dev) |
| 391 | { |
| 392 | int idx; |
| 393 | |
| 394 | idx = device_links_read_lock(); |
| 395 | device_pm_lock(); |
| 396 | device_reorder_to_tail(dev, NULL); |
| 397 | device_pm_unlock(); |
| 398 | device_links_read_unlock(idx); |
| 399 | } |
| 400 | |
Saravana Kannan | 287905e | 2020-05-21 12:17:58 -0700 | [diff] [blame] | 401 | #define to_devlink(dev) container_of((dev), struct device_link, link_dev) |
| 402 | |
| 403 | static ssize_t status_show(struct device *dev, |
Joe Perches | 948b3ed | 2020-09-16 13:40:42 -0700 | [diff] [blame] | 404 | struct device_attribute *attr, char *buf) |
Saravana Kannan | 287905e | 2020-05-21 12:17:58 -0700 | [diff] [blame] | 405 | { |
Joe Perches | 948b3ed | 2020-09-16 13:40:42 -0700 | [diff] [blame] | 406 | const char *output; |
Saravana Kannan | 287905e | 2020-05-21 12:17:58 -0700 | [diff] [blame] | 407 | |
| 408 | switch (to_devlink(dev)->status) { |
| 409 | case DL_STATE_NONE: |
Joe Perches | 948b3ed | 2020-09-16 13:40:42 -0700 | [diff] [blame] | 410 | output = "not tracked"; |
| 411 | break; |
Saravana Kannan | 287905e | 2020-05-21 12:17:58 -0700 | [diff] [blame] | 412 | case DL_STATE_DORMANT: |
Joe Perches | 948b3ed | 2020-09-16 13:40:42 -0700 | [diff] [blame] | 413 | output = "dormant"; |
| 414 | break; |
Saravana Kannan | 287905e | 2020-05-21 12:17:58 -0700 | [diff] [blame] | 415 | case DL_STATE_AVAILABLE: |
Joe Perches | 948b3ed | 2020-09-16 13:40:42 -0700 | [diff] [blame] | 416 | output = "available"; |
| 417 | break; |
Saravana Kannan | 287905e | 2020-05-21 12:17:58 -0700 | [diff] [blame] | 418 | case DL_STATE_CONSUMER_PROBE: |
Joe Perches | 948b3ed | 2020-09-16 13:40:42 -0700 | [diff] [blame] | 419 | output = "consumer probing"; |
| 420 | break; |
Saravana Kannan | 287905e | 2020-05-21 12:17:58 -0700 | [diff] [blame] | 421 | case DL_STATE_ACTIVE: |
Joe Perches | 948b3ed | 2020-09-16 13:40:42 -0700 | [diff] [blame] | 422 | output = "active"; |
| 423 | break; |
Saravana Kannan | 287905e | 2020-05-21 12:17:58 -0700 | [diff] [blame] | 424 | case DL_STATE_SUPPLIER_UNBIND: |
Joe Perches | 948b3ed | 2020-09-16 13:40:42 -0700 | [diff] [blame] | 425 | output = "supplier unbinding"; |
| 426 | break; |
Saravana Kannan | 287905e | 2020-05-21 12:17:58 -0700 | [diff] [blame] | 427 | default: |
Joe Perches | 948b3ed | 2020-09-16 13:40:42 -0700 | [diff] [blame] | 428 | output = "unknown"; |
| 429 | break; |
Saravana Kannan | 287905e | 2020-05-21 12:17:58 -0700 | [diff] [blame] | 430 | } |
Joe Perches | 948b3ed | 2020-09-16 13:40:42 -0700 | [diff] [blame] | 431 | |
| 432 | return sysfs_emit(buf, "%s\n", output); |
Saravana Kannan | 287905e | 2020-05-21 12:17:58 -0700 | [diff] [blame] | 433 | } |
| 434 | static DEVICE_ATTR_RO(status); |
| 435 | |
| 436 | static ssize_t auto_remove_on_show(struct device *dev, |
| 437 | struct device_attribute *attr, char *buf) |
| 438 | { |
| 439 | struct device_link *link = to_devlink(dev); |
Joe Perches | 973c391 | 2020-09-16 13:40:40 -0700 | [diff] [blame] | 440 | const char *output; |
Saravana Kannan | 287905e | 2020-05-21 12:17:58 -0700 | [diff] [blame] | 441 | |
| 442 | if (link->flags & DL_FLAG_AUTOREMOVE_SUPPLIER) |
Joe Perches | 973c391 | 2020-09-16 13:40:40 -0700 | [diff] [blame] | 443 | output = "supplier unbind"; |
Saravana Kannan | 287905e | 2020-05-21 12:17:58 -0700 | [diff] [blame] | 444 | else if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER) |
Joe Perches | 973c391 | 2020-09-16 13:40:40 -0700 | [diff] [blame] | 445 | output = "consumer unbind"; |
Saravana Kannan | 287905e | 2020-05-21 12:17:58 -0700 | [diff] [blame] | 446 | else |
Joe Perches | 973c391 | 2020-09-16 13:40:40 -0700 | [diff] [blame] | 447 | output = "never"; |
Saravana Kannan | 287905e | 2020-05-21 12:17:58 -0700 | [diff] [blame] | 448 | |
Joe Perches | 973c391 | 2020-09-16 13:40:40 -0700 | [diff] [blame] | 449 | return sysfs_emit(buf, "%s\n", output); |
Saravana Kannan | 287905e | 2020-05-21 12:17:58 -0700 | [diff] [blame] | 450 | } |
| 451 | static DEVICE_ATTR_RO(auto_remove_on); |
| 452 | |
| 453 | static ssize_t runtime_pm_show(struct device *dev, |
| 454 | struct device_attribute *attr, char *buf) |
| 455 | { |
| 456 | struct device_link *link = to_devlink(dev); |
| 457 | |
Joe Perches | aa83889 | 2020-09-16 13:40:39 -0700 | [diff] [blame] | 458 | return sysfs_emit(buf, "%d\n", !!(link->flags & DL_FLAG_PM_RUNTIME)); |
Saravana Kannan | 287905e | 2020-05-21 12:17:58 -0700 | [diff] [blame] | 459 | } |
| 460 | static DEVICE_ATTR_RO(runtime_pm); |
| 461 | |
| 462 | static ssize_t sync_state_only_show(struct device *dev, |
| 463 | struct device_attribute *attr, char *buf) |
| 464 | { |
| 465 | struct device_link *link = to_devlink(dev); |
| 466 | |
Joe Perches | aa83889 | 2020-09-16 13:40:39 -0700 | [diff] [blame] | 467 | return sysfs_emit(buf, "%d\n", |
| 468 | !!(link->flags & DL_FLAG_SYNC_STATE_ONLY)); |
Saravana Kannan | 287905e | 2020-05-21 12:17:58 -0700 | [diff] [blame] | 469 | } |
| 470 | static DEVICE_ATTR_RO(sync_state_only); |
| 471 | |
| 472 | static struct attribute *devlink_attrs[] = { |
| 473 | &dev_attr_status.attr, |
| 474 | &dev_attr_auto_remove_on.attr, |
| 475 | &dev_attr_runtime_pm.attr, |
| 476 | &dev_attr_sync_state_only.attr, |
| 477 | NULL, |
| 478 | }; |
| 479 | ATTRIBUTE_GROUPS(devlink); |
| 480 | |
Rafael J. Wysocki | 80dd33c | 2021-05-14 14:10:15 +0200 | [diff] [blame] | 481 | static void device_link_release_fn(struct work_struct *work) |
Saravana Kannan | 843e600 | 2020-07-16 14:45:23 -0700 | [diff] [blame] | 482 | { |
Rafael J. Wysocki | 80dd33c | 2021-05-14 14:10:15 +0200 | [diff] [blame] | 483 | struct device_link *link = container_of(work, struct device_link, rm_work); |
| 484 | |
| 485 | /* Ensure that all references to the link object have been dropped. */ |
| 486 | device_link_synchronize_removal(); |
| 487 | |
Saravana Kannan | 843e600 | 2020-07-16 14:45:23 -0700 | [diff] [blame] | 488 | while (refcount_dec_not_one(&link->rpm_active)) |
| 489 | pm_runtime_put(link->supplier); |
| 490 | |
| 491 | put_device(link->consumer); |
| 492 | put_device(link->supplier); |
| 493 | kfree(link); |
| 494 | } |
| 495 | |
Saravana Kannan | 287905e | 2020-05-21 12:17:58 -0700 | [diff] [blame] | 496 | static void devlink_dev_release(struct device *dev) |
| 497 | { |
Saravana Kannan | 843e600 | 2020-07-16 14:45:23 -0700 | [diff] [blame] | 498 | struct device_link *link = to_devlink(dev); |
| 499 | |
Rafael J. Wysocki | 80dd33c | 2021-05-14 14:10:15 +0200 | [diff] [blame] | 500 | INIT_WORK(&link->rm_work, device_link_release_fn); |
| 501 | /* |
| 502 | * It may take a while to complete this work because of the SRCU |
| 503 | * synchronization in device_link_release_fn() and if the consumer or |
| 504 | * supplier devices get deleted when it runs, so put it into the "long" |
| 505 | * workqueue. |
| 506 | */ |
| 507 | queue_work(system_long_wq, &link->rm_work); |
Saravana Kannan | 287905e | 2020-05-21 12:17:58 -0700 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | static struct class devlink_class = { |
| 511 | .name = "devlink", |
| 512 | .owner = THIS_MODULE, |
| 513 | .dev_groups = devlink_groups, |
| 514 | .dev_release = devlink_dev_release, |
| 515 | }; |
| 516 | |
| 517 | static int devlink_add_symlinks(struct device *dev, |
| 518 | struct class_interface *class_intf) |
| 519 | { |
| 520 | int ret; |
| 521 | size_t len; |
| 522 | struct device_link *link = to_devlink(dev); |
| 523 | struct device *sup = link->supplier; |
| 524 | struct device *con = link->consumer; |
| 525 | char *buf; |
| 526 | |
Saravana Kannan | e020ff6 | 2021-01-10 09:54:07 -0800 | [diff] [blame] | 527 | len = max(strlen(dev_bus_name(sup)) + strlen(dev_name(sup)), |
| 528 | strlen(dev_bus_name(con)) + strlen(dev_name(con))); |
| 529 | len += strlen(":"); |
Saravana Kannan | 287905e | 2020-05-21 12:17:58 -0700 | [diff] [blame] | 530 | len += strlen("supplier:") + 1; |
| 531 | buf = kzalloc(len, GFP_KERNEL); |
| 532 | if (!buf) |
| 533 | return -ENOMEM; |
| 534 | |
| 535 | ret = sysfs_create_link(&link->link_dev.kobj, &sup->kobj, "supplier"); |
| 536 | if (ret) |
| 537 | goto out; |
| 538 | |
| 539 | ret = sysfs_create_link(&link->link_dev.kobj, &con->kobj, "consumer"); |
| 540 | if (ret) |
| 541 | goto err_con; |
| 542 | |
Saravana Kannan | e020ff6 | 2021-01-10 09:54:07 -0800 | [diff] [blame] | 543 | snprintf(buf, len, "consumer:%s:%s", dev_bus_name(con), dev_name(con)); |
Saravana Kannan | 287905e | 2020-05-21 12:17:58 -0700 | [diff] [blame] | 544 | ret = sysfs_create_link(&sup->kobj, &link->link_dev.kobj, buf); |
| 545 | if (ret) |
| 546 | goto err_con_dev; |
| 547 | |
Saravana Kannan | e020ff6 | 2021-01-10 09:54:07 -0800 | [diff] [blame] | 548 | snprintf(buf, len, "supplier:%s:%s", dev_bus_name(sup), dev_name(sup)); |
Saravana Kannan | 287905e | 2020-05-21 12:17:58 -0700 | [diff] [blame] | 549 | ret = sysfs_create_link(&con->kobj, &link->link_dev.kobj, buf); |
| 550 | if (ret) |
| 551 | goto err_sup_dev; |
| 552 | |
| 553 | goto out; |
| 554 | |
| 555 | err_sup_dev: |
Saravana Kannan | e020ff6 | 2021-01-10 09:54:07 -0800 | [diff] [blame] | 556 | snprintf(buf, len, "consumer:%s:%s", dev_bus_name(con), dev_name(con)); |
Saravana Kannan | 287905e | 2020-05-21 12:17:58 -0700 | [diff] [blame] | 557 | sysfs_remove_link(&sup->kobj, buf); |
| 558 | err_con_dev: |
| 559 | sysfs_remove_link(&link->link_dev.kobj, "consumer"); |
| 560 | err_con: |
| 561 | sysfs_remove_link(&link->link_dev.kobj, "supplier"); |
| 562 | out: |
| 563 | kfree(buf); |
| 564 | return ret; |
| 565 | } |
| 566 | |
| 567 | static void devlink_remove_symlinks(struct device *dev, |
| 568 | struct class_interface *class_intf) |
| 569 | { |
| 570 | struct device_link *link = to_devlink(dev); |
| 571 | size_t len; |
| 572 | struct device *sup = link->supplier; |
| 573 | struct device *con = link->consumer; |
| 574 | char *buf; |
| 575 | |
| 576 | sysfs_remove_link(&link->link_dev.kobj, "consumer"); |
| 577 | sysfs_remove_link(&link->link_dev.kobj, "supplier"); |
| 578 | |
Saravana Kannan | e020ff6 | 2021-01-10 09:54:07 -0800 | [diff] [blame] | 579 | len = max(strlen(dev_bus_name(sup)) + strlen(dev_name(sup)), |
| 580 | strlen(dev_bus_name(con)) + strlen(dev_name(con))); |
| 581 | len += strlen(":"); |
Saravana Kannan | 287905e | 2020-05-21 12:17:58 -0700 | [diff] [blame] | 582 | len += strlen("supplier:") + 1; |
| 583 | buf = kzalloc(len, GFP_KERNEL); |
| 584 | if (!buf) { |
| 585 | WARN(1, "Unable to properly free device link symlinks!\n"); |
| 586 | return; |
| 587 | } |
| 588 | |
Adrian Hunter | e64daad | 2021-07-16 14:44:07 +0300 | [diff] [blame] | 589 | if (device_is_registered(con)) { |
| 590 | snprintf(buf, len, "supplier:%s:%s", dev_bus_name(sup), dev_name(sup)); |
| 591 | sysfs_remove_link(&con->kobj, buf); |
| 592 | } |
Saravana Kannan | e020ff6 | 2021-01-10 09:54:07 -0800 | [diff] [blame] | 593 | snprintf(buf, len, "consumer:%s:%s", dev_bus_name(con), dev_name(con)); |
Saravana Kannan | 287905e | 2020-05-21 12:17:58 -0700 | [diff] [blame] | 594 | sysfs_remove_link(&sup->kobj, buf); |
| 595 | kfree(buf); |
| 596 | } |
| 597 | |
| 598 | static struct class_interface devlink_class_intf = { |
| 599 | .class = &devlink_class, |
| 600 | .add_dev = devlink_add_symlinks, |
| 601 | .remove_dev = devlink_remove_symlinks, |
| 602 | }; |
| 603 | |
| 604 | static int __init devlink_class_init(void) |
| 605 | { |
| 606 | int ret; |
| 607 | |
| 608 | ret = class_register(&devlink_class); |
| 609 | if (ret) |
| 610 | return ret; |
| 611 | |
| 612 | ret = class_interface_register(&devlink_class_intf); |
| 613 | if (ret) |
| 614 | class_unregister(&devlink_class); |
| 615 | |
| 616 | return ret; |
| 617 | } |
| 618 | postcore_initcall(devlink_class_init); |
| 619 | |
Rafael J. Wysocki | 515db26 | 2019-07-16 17:21:06 +0200 | [diff] [blame] | 620 | #define DL_MANAGED_LINK_FLAGS (DL_FLAG_AUTOREMOVE_CONSUMER | \ |
| 621 | DL_FLAG_AUTOREMOVE_SUPPLIER | \ |
Saravana Kannan | 05ef983 | 2019-10-28 15:00:22 -0700 | [diff] [blame] | 622 | DL_FLAG_AUTOPROBE_CONSUMER | \ |
Saravana Kannan | 4b9bbb2 | 2020-12-17 19:17:00 -0800 | [diff] [blame] | 623 | DL_FLAG_SYNC_STATE_ONLY | \ |
| 624 | DL_FLAG_INFERRED) |
Rafael J. Wysocki | 515db26 | 2019-07-16 17:21:06 +0200 | [diff] [blame] | 625 | |
Rafael J. Wysocki | fb583c8 | 2019-07-30 11:28:57 +0200 | [diff] [blame] | 626 | #define DL_ADD_VALID_FLAGS (DL_MANAGED_LINK_FLAGS | DL_FLAG_STATELESS | \ |
| 627 | DL_FLAG_PM_RUNTIME | DL_FLAG_RPM_ACTIVE) |
| 628 | |
Feng Kan | 494fd7b | 2018-04-10 16:57:06 -0700 | [diff] [blame] | 629 | /** |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 630 | * device_link_add - Create a link between two devices. |
| 631 | * @consumer: Consumer end of the link. |
| 632 | * @supplier: Supplier end of the link. |
| 633 | * @flags: Link flags. |
| 634 | * |
Rafael J. Wysocki | 21d5c57 | 2016-10-30 17:32:31 +0100 | [diff] [blame] | 635 | * The caller is responsible for the proper synchronization of the link creation |
| 636 | * with runtime PM. First, setting the DL_FLAG_PM_RUNTIME flag will cause the |
| 637 | * runtime PM framework to take the link into account. Second, if the |
| 638 | * DL_FLAG_RPM_ACTIVE flag is set in addition to it, the supplier devices will |
Thierry Reding | d475f8e | 2020-11-27 11:46:30 +0100 | [diff] [blame] | 639 | * be forced into the active meta state and reference-counted upon the creation |
Rafael J. Wysocki | 21d5c57 | 2016-10-30 17:32:31 +0100 | [diff] [blame] | 640 | * of the link. If DL_FLAG_PM_RUNTIME is not set, DL_FLAG_RPM_ACTIVE will be |
| 641 | * ignored. |
| 642 | * |
Rafael J. Wysocki | 515db26 | 2019-07-16 17:21:06 +0200 | [diff] [blame] | 643 | * If DL_FLAG_STATELESS is set in @flags, the caller of this function is |
| 644 | * expected to release the link returned by it directly with the help of either |
| 645 | * device_link_del() or device_link_remove(). |
Rafael J. Wysocki | 72175d4 | 2019-02-01 01:58:33 +0100 | [diff] [blame] | 646 | * |
| 647 | * If that flag is not set, however, the caller of this function is handing the |
| 648 | * management of the link over to the driver core entirely and its return value |
| 649 | * can only be used to check whether or not the link is present. In that case, |
| 650 | * the DL_FLAG_AUTOREMOVE_CONSUMER and DL_FLAG_AUTOREMOVE_SUPPLIER device link |
| 651 | * flags can be used to indicate to the driver core when the link can be safely |
| 652 | * deleted. Namely, setting one of them in @flags indicates to the driver core |
| 653 | * that the link is not going to be used (by the given caller of this function) |
| 654 | * after unbinding the consumer or supplier driver, respectively, from its |
| 655 | * device, so the link can be deleted at that point. If none of them is set, |
| 656 | * the link will be maintained until one of the devices pointed to by it (either |
| 657 | * the consumer or the supplier) is unregistered. |
Rafael J. Wysocki | c8d5098 | 2019-02-01 01:45:55 +0100 | [diff] [blame] | 658 | * |
Rafael J. Wysocki | e7dd401 | 2019-02-01 01:59:42 +0100 | [diff] [blame] | 659 | * Also, if DL_FLAG_STATELESS, DL_FLAG_AUTOREMOVE_CONSUMER and |
| 660 | * DL_FLAG_AUTOREMOVE_SUPPLIER are not set in @flags (that is, a persistent |
| 661 | * managed device link is being added), the DL_FLAG_AUTOPROBE_CONSUMER flag can |
Thierry Reding | d475f8e | 2020-11-27 11:46:30 +0100 | [diff] [blame] | 662 | * be used to request the driver core to automatically probe for a consumer |
Rafael J. Wysocki | e7dd401 | 2019-02-01 01:59:42 +0100 | [diff] [blame] | 663 | * driver after successfully binding a driver to the supplier device. |
| 664 | * |
Rafael J. Wysocki | 515db26 | 2019-07-16 17:21:06 +0200 | [diff] [blame] | 665 | * The combination of DL_FLAG_STATELESS and one of DL_FLAG_AUTOREMOVE_CONSUMER, |
| 666 | * DL_FLAG_AUTOREMOVE_SUPPLIER, or DL_FLAG_AUTOPROBE_CONSUMER set in @flags at |
| 667 | * the same time is invalid and will cause NULL to be returned upfront. |
| 668 | * However, if a device link between the given @consumer and @supplier pair |
| 669 | * exists already when this function is called for them, the existing link will |
| 670 | * be returned regardless of its current type and status (the link's flags may |
| 671 | * be modified then). The caller of this function is then expected to treat |
| 672 | * the link as though it has just been created, so (in particular) if |
| 673 | * DL_FLAG_STATELESS was passed in @flags, the link needs to be released |
| 674 | * explicitly when not needed any more (as stated above). |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 675 | * |
| 676 | * A side effect of the link creation is re-ordering of dpm_list and the |
| 677 | * devices_kset list by moving the consumer device and all devices depending |
| 678 | * on it to the ends of these lists (that does not happen to devices that have |
| 679 | * not been registered when this function is called). |
| 680 | * |
| 681 | * The supplier device is required to be registered when this function is called |
| 682 | * 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] | 683 | * not be registered, however. |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 684 | */ |
| 685 | struct device_link *device_link_add(struct device *consumer, |
| 686 | struct device *supplier, u32 flags) |
| 687 | { |
| 688 | struct device_link *link; |
| 689 | |
Saravana Kannan | f729a59 | 2021-09-29 12:05:49 -0700 | [diff] [blame] | 690 | if (!consumer || !supplier || consumer == supplier || |
| 691 | flags & ~DL_ADD_VALID_FLAGS || |
Rafael J. Wysocki | 515db26 | 2019-07-16 17:21:06 +0200 | [diff] [blame] | 692 | (flags & DL_FLAG_STATELESS && flags & DL_MANAGED_LINK_FLAGS) || |
Saravana Kannan | 05ef983 | 2019-10-28 15:00:22 -0700 | [diff] [blame] | 693 | (flags & DL_FLAG_SYNC_STATE_ONLY && |
Saravana Kannan | 4b9bbb2 | 2020-12-17 19:17:00 -0800 | [diff] [blame] | 694 | (flags & ~DL_FLAG_INFERRED) != DL_FLAG_SYNC_STATE_ONLY) || |
Rafael J. Wysocki | e7dd401 | 2019-02-01 01:59:42 +0100 | [diff] [blame] | 695 | (flags & DL_FLAG_AUTOPROBE_CONSUMER && |
| 696 | flags & (DL_FLAG_AUTOREMOVE_CONSUMER | |
| 697 | DL_FLAG_AUTOREMOVE_SUPPLIER))) |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 698 | return NULL; |
| 699 | |
Rafael J. Wysocki | 5db25c9 | 2019-02-01 01:47:53 +0100 | [diff] [blame] | 700 | if (flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) { |
| 701 | if (pm_runtime_get_sync(supplier) < 0) { |
| 702 | pm_runtime_put_noidle(supplier); |
| 703 | return NULL; |
| 704 | } |
Rafael J. Wysocki | 5db25c9 | 2019-02-01 01:47:53 +0100 | [diff] [blame] | 705 | } |
| 706 | |
Rafael J. Wysocki | 515db26 | 2019-07-16 17:21:06 +0200 | [diff] [blame] | 707 | if (!(flags & DL_FLAG_STATELESS)) |
| 708 | flags |= DL_FLAG_MANAGED; |
| 709 | |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 710 | device_links_write_lock(); |
| 711 | device_pm_lock(); |
| 712 | |
| 713 | /* |
| 714 | * 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] | 715 | * reverse (non-SYNC_STATE_ONLY) dependency between the consumer and |
| 716 | * the supplier already in the graph, return NULL. If the link is a |
| 717 | * SYNC_STATE_ONLY link, we don't check for reverse dependencies |
| 718 | * because it only affects sync_state() callbacks. |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 719 | */ |
| 720 | if (!device_pm_initialized(supplier) |
Saravana Kannan | 05ef983 | 2019-10-28 15:00:22 -0700 | [diff] [blame] | 721 | || (!(flags & DL_FLAG_SYNC_STATE_ONLY) && |
| 722 | device_is_dependent(consumer, supplier))) { |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 723 | link = NULL; |
| 724 | goto out; |
| 725 | } |
| 726 | |
Rafael J. Wysocki | 72175d4 | 2019-02-01 01:58:33 +0100 | [diff] [blame] | 727 | /* |
Saravana Kannan | ac66c5b | 2020-11-20 18:02:24 -0800 | [diff] [blame] | 728 | * SYNC_STATE_ONLY links are useless once a consumer device has probed. |
| 729 | * So, only create it if the consumer hasn't probed yet. |
| 730 | */ |
| 731 | if (flags & DL_FLAG_SYNC_STATE_ONLY && |
| 732 | consumer->links.status != DL_DEV_NO_DRIVER && |
| 733 | consumer->links.status != DL_DEV_PROBING) { |
| 734 | link = NULL; |
| 735 | goto out; |
| 736 | } |
| 737 | |
| 738 | /* |
Rafael J. Wysocki | 72175d4 | 2019-02-01 01:58:33 +0100 | [diff] [blame] | 739 | * DL_FLAG_AUTOREMOVE_SUPPLIER indicates that the link will be needed |
| 740 | * longer than for DL_FLAG_AUTOREMOVE_CONSUMER and setting them both |
| 741 | * together doesn't make sense, so prefer DL_FLAG_AUTOREMOVE_SUPPLIER. |
| 742 | */ |
| 743 | if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER) |
| 744 | flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER; |
| 745 | |
Rafael J. Wysocki | f265df5 | 2019-02-01 01:46:54 +0100 | [diff] [blame] | 746 | list_for_each_entry(link, &supplier->links.consumers, s_node) { |
| 747 | if (link->consumer != consumer) |
| 748 | continue; |
| 749 | |
Saravana Kannan | 4b9bbb2 | 2020-12-17 19:17:00 -0800 | [diff] [blame] | 750 | if (link->flags & DL_FLAG_INFERRED && |
| 751 | !(flags & DL_FLAG_INFERRED)) |
| 752 | link->flags &= ~DL_FLAG_INFERRED; |
| 753 | |
Rafael J. Wysocki | e2f3cd8 | 2019-02-01 01:49:14 +0100 | [diff] [blame] | 754 | if (flags & DL_FLAG_PM_RUNTIME) { |
| 755 | if (!(link->flags & DL_FLAG_PM_RUNTIME)) { |
Rafael J. Wysocki | 4c06c4e | 2019-02-12 13:08:10 +0100 | [diff] [blame] | 756 | pm_runtime_new_link(consumer); |
Rafael J. Wysocki | e2f3cd8 | 2019-02-01 01:49:14 +0100 | [diff] [blame] | 757 | link->flags |= DL_FLAG_PM_RUNTIME; |
| 758 | } |
| 759 | if (flags & DL_FLAG_RPM_ACTIVE) |
Rafael J. Wysocki | 36003d4 | 2019-02-19 17:53:26 +0100 | [diff] [blame] | 760 | refcount_inc(&link->rpm_active); |
Rafael J. Wysocki | e2f3cd8 | 2019-02-01 01:49:14 +0100 | [diff] [blame] | 761 | } |
| 762 | |
Rafael J. Wysocki | 72175d4 | 2019-02-01 01:58:33 +0100 | [diff] [blame] | 763 | if (flags & DL_FLAG_STATELESS) { |
| 764 | kref_get(&link->kref); |
Saravana Kannan | 05ef983 | 2019-10-28 15:00:22 -0700 | [diff] [blame] | 765 | if (link->flags & DL_FLAG_SYNC_STATE_ONLY && |
Saravana Kannan | 44e9604 | 2020-05-19 21:36:26 -0700 | [diff] [blame] | 766 | !(link->flags & DL_FLAG_STATELESS)) { |
| 767 | link->flags |= DL_FLAG_STATELESS; |
Saravana Kannan | 05ef983 | 2019-10-28 15:00:22 -0700 | [diff] [blame] | 768 | goto reorder; |
Saravana Kannan | 44e9604 | 2020-05-19 21:36:26 -0700 | [diff] [blame] | 769 | } else { |
| 770 | link->flags |= DL_FLAG_STATELESS; |
Saravana Kannan | 05ef983 | 2019-10-28 15:00:22 -0700 | [diff] [blame] | 771 | goto out; |
Saravana Kannan | 44e9604 | 2020-05-19 21:36:26 -0700 | [diff] [blame] | 772 | } |
Rafael J. Wysocki | 72175d4 | 2019-02-01 01:58:33 +0100 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | /* |
| 776 | * If the life time of the link following from the new flags is |
| 777 | * longer than indicated by the flags of the existing link, |
| 778 | * update the existing link to stay around longer. |
| 779 | */ |
| 780 | if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER) { |
| 781 | if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER) { |
| 782 | link->flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER; |
| 783 | link->flags |= DL_FLAG_AUTOREMOVE_SUPPLIER; |
| 784 | } |
| 785 | } else if (!(flags & DL_FLAG_AUTOREMOVE_CONSUMER)) { |
| 786 | link->flags &= ~(DL_FLAG_AUTOREMOVE_CONSUMER | |
| 787 | DL_FLAG_AUTOREMOVE_SUPPLIER); |
| 788 | } |
Rafael J. Wysocki | 515db26 | 2019-07-16 17:21:06 +0200 | [diff] [blame] | 789 | if (!(link->flags & DL_FLAG_MANAGED)) { |
| 790 | kref_get(&link->kref); |
| 791 | link->flags |= DL_FLAG_MANAGED; |
| 792 | device_link_init_status(link, consumer, supplier); |
| 793 | } |
Saravana Kannan | 05ef983 | 2019-10-28 15:00:22 -0700 | [diff] [blame] | 794 | if (link->flags & DL_FLAG_SYNC_STATE_ONLY && |
| 795 | !(flags & DL_FLAG_SYNC_STATE_ONLY)) { |
| 796 | link->flags &= ~DL_FLAG_SYNC_STATE_ONLY; |
| 797 | goto reorder; |
| 798 | } |
| 799 | |
Rafael J. Wysocki | f265df5 | 2019-02-01 01:46:54 +0100 | [diff] [blame] | 800 | goto out; |
| 801 | } |
| 802 | |
Rafael J. Wysocki | 21d5c57 | 2016-10-30 17:32:31 +0100 | [diff] [blame] | 803 | link = kzalloc(sizeof(*link), GFP_KERNEL); |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 804 | if (!link) |
| 805 | goto out; |
| 806 | |
Rafael J. Wysocki | e2f3cd8 | 2019-02-01 01:49:14 +0100 | [diff] [blame] | 807 | refcount_set(&link->rpm_active, 1); |
| 808 | |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 809 | get_device(supplier); |
| 810 | link->supplier = supplier; |
| 811 | INIT_LIST_HEAD(&link->s_node); |
| 812 | get_device(consumer); |
| 813 | link->consumer = consumer; |
| 814 | INIT_LIST_HEAD(&link->c_node); |
| 815 | link->flags = flags; |
Lukas Wunner | ead18c2 | 2018-02-10 19:27:12 +0100 | [diff] [blame] | 816 | kref_init(&link->kref); |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 817 | |
Saravana Kannan | 287905e | 2020-05-21 12:17:58 -0700 | [diff] [blame] | 818 | link->link_dev.class = &devlink_class; |
| 819 | device_set_pm_not_required(&link->link_dev); |
Saravana Kannan | e020ff6 | 2021-01-10 09:54:07 -0800 | [diff] [blame] | 820 | dev_set_name(&link->link_dev, "%s:%s--%s:%s", |
| 821 | dev_bus_name(supplier), dev_name(supplier), |
| 822 | dev_bus_name(consumer), dev_name(consumer)); |
Saravana Kannan | 287905e | 2020-05-21 12:17:58 -0700 | [diff] [blame] | 823 | if (device_register(&link->link_dev)) { |
Yang Yingliang | df0a181 | 2021-09-30 16:57:14 +0800 | [diff] [blame] | 824 | put_device(&link->link_dev); |
Saravana Kannan | 287905e | 2020-05-21 12:17:58 -0700 | [diff] [blame] | 825 | link = NULL; |
| 826 | goto out; |
| 827 | } |
| 828 | |
| 829 | if (flags & DL_FLAG_PM_RUNTIME) { |
| 830 | if (flags & DL_FLAG_RPM_ACTIVE) |
| 831 | refcount_inc(&link->rpm_active); |
| 832 | |
| 833 | pm_runtime_new_link(consumer); |
| 834 | } |
| 835 | |
Lukas Wunner | 64df114 | 2016-12-04 13:10:04 +0100 | [diff] [blame] | 836 | /* Determine the initial link state. */ |
Rafael J. Wysocki | 515db26 | 2019-07-16 17:21:06 +0200 | [diff] [blame] | 837 | if (flags & DL_FLAG_STATELESS) |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 838 | link->status = DL_STATE_NONE; |
Rafael J. Wysocki | 515db26 | 2019-07-16 17:21:06 +0200 | [diff] [blame] | 839 | else |
| 840 | device_link_init_status(link, consumer, supplier); |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 841 | |
| 842 | /* |
Rafael J. Wysocki | 15cfb09 | 2019-02-01 01:50:39 +0100 | [diff] [blame] | 843 | * Some callers expect the link creation during consumer driver probe to |
| 844 | * resume the supplier even without DL_FLAG_RPM_ACTIVE. |
| 845 | */ |
| 846 | if (link->status == DL_STATE_CONSUMER_PROBE && |
| 847 | flags & DL_FLAG_PM_RUNTIME) |
| 848 | pm_runtime_resume(supplier); |
| 849 | |
Saravana Kannan | 21c27f0 | 2020-05-18 23:30:00 -0700 | [diff] [blame] | 850 | list_add_tail_rcu(&link->s_node, &supplier->links.consumers); |
| 851 | list_add_tail_rcu(&link->c_node, &consumer->links.suppliers); |
| 852 | |
Saravana Kannan | 05ef983 | 2019-10-28 15:00:22 -0700 | [diff] [blame] | 853 | if (flags & DL_FLAG_SYNC_STATE_ONLY) { |
| 854 | dev_dbg(consumer, |
| 855 | "Linked as a sync state only consumer to %s\n", |
| 856 | dev_name(supplier)); |
| 857 | goto out; |
| 858 | } |
Saravana Kannan | 21c27f0 | 2020-05-18 23:30:00 -0700 | [diff] [blame] | 859 | |
Saravana Kannan | 05ef983 | 2019-10-28 15:00:22 -0700 | [diff] [blame] | 860 | reorder: |
Rafael J. Wysocki | 15cfb09 | 2019-02-01 01:50:39 +0100 | [diff] [blame] | 861 | /* |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 862 | * Move the consumer and all of the devices depending on it to the end |
| 863 | * of dpm_list and the devices_kset list. |
| 864 | * |
| 865 | * It is necessary to hold dpm_list locked throughout all that or else |
| 866 | * we may end up suspending with a wrong ordering of it. |
| 867 | */ |
| 868 | device_reorder_to_tail(consumer, NULL); |
| 869 | |
Jerome Brunet | 8a4b326 | 2018-12-21 17:23:41 +0100 | [diff] [blame] | 870 | 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] | 871 | |
Saravana Kannan | 21c27f0 | 2020-05-18 23:30:00 -0700 | [diff] [blame] | 872 | out: |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 873 | device_pm_unlock(); |
| 874 | device_links_write_unlock(); |
Rafael J. Wysocki | 5db25c9 | 2019-02-01 01:47:53 +0100 | [diff] [blame] | 875 | |
Rafael J. Wysocki | e2f3cd8 | 2019-02-01 01:49:14 +0100 | [diff] [blame] | 876 | 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] | 877 | pm_runtime_put(supplier); |
| 878 | |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 879 | return link; |
| 880 | } |
| 881 | EXPORT_SYMBOL_GPL(device_link_add); |
| 882 | |
Lukas Wunner | ead18c2 | 2018-02-10 19:27:12 +0100 | [diff] [blame] | 883 | static void __device_link_del(struct kref *kref) |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 884 | { |
Lukas Wunner | ead18c2 | 2018-02-10 19:27:12 +0100 | [diff] [blame] | 885 | struct device_link *link = container_of(kref, struct device_link, kref); |
| 886 | |
Jerome Brunet | 8a4b326 | 2018-12-21 17:23:41 +0100 | [diff] [blame] | 887 | dev_dbg(link->consumer, "Dropping the link to %s\n", |
| 888 | dev_name(link->supplier)); |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 889 | |
Rafael J. Wysocki | e0e398e | 2020-10-21 21:12:15 +0200 | [diff] [blame] | 890 | pm_runtime_drop_link(link); |
Rafael J. Wysocki | baa8809 | 2016-10-30 17:32:43 +0100 | [diff] [blame] | 891 | |
Rafael J. Wysocki | 0c87131 | 2021-05-14 14:11:19 +0200 | [diff] [blame] | 892 | device_link_remove_from_lists(link); |
Saravana Kannan | 843e600 | 2020-07-16 14:45:23 -0700 | [diff] [blame] | 893 | device_unregister(&link->link_dev); |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 894 | } |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 895 | |
Rafael J. Wysocki | 72175d4 | 2019-02-01 01:58:33 +0100 | [diff] [blame] | 896 | static void device_link_put_kref(struct device_link *link) |
| 897 | { |
| 898 | if (link->flags & DL_FLAG_STATELESS) |
| 899 | kref_put(&link->kref, __device_link_del); |
Adrian Hunter | bf25967 | 2021-08-06 16:04:41 +0300 | [diff] [blame] | 900 | else if (!device_is_registered(link->consumer)) |
| 901 | __device_link_del(&link->kref); |
Rafael J. Wysocki | 72175d4 | 2019-02-01 01:58:33 +0100 | [diff] [blame] | 902 | else |
| 903 | WARN(1, "Unable to drop a managed device link reference\n"); |
| 904 | } |
| 905 | |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 906 | /** |
Rafael J. Wysocki | 72175d4 | 2019-02-01 01:58:33 +0100 | [diff] [blame] | 907 | * device_link_del - Delete a stateless link between two devices. |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 908 | * @link: Device link to delete. |
| 909 | * |
| 910 | * The caller must ensure proper synchronization of this function with runtime |
Lukas Wunner | ead18c2 | 2018-02-10 19:27:12 +0100 | [diff] [blame] | 911 | * PM. If the link was added multiple times, it needs to be deleted as often. |
| 912 | * Care is required for hotplugged devices: Their links are purged on removal |
| 913 | * and calling device_link_del() is then no longer allowed. |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 914 | */ |
| 915 | void device_link_del(struct device_link *link) |
| 916 | { |
| 917 | device_links_write_lock(); |
Rafael J. Wysocki | 72175d4 | 2019-02-01 01:58:33 +0100 | [diff] [blame] | 918 | device_link_put_kref(link); |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 919 | device_links_write_unlock(); |
| 920 | } |
| 921 | EXPORT_SYMBOL_GPL(device_link_del); |
| 922 | |
pascal paillet | d884221 | 2018-07-05 14:25:56 +0000 | [diff] [blame] | 923 | /** |
Rafael J. Wysocki | 72175d4 | 2019-02-01 01:58:33 +0100 | [diff] [blame] | 924 | * device_link_remove - Delete a stateless link between two devices. |
pascal paillet | d884221 | 2018-07-05 14:25:56 +0000 | [diff] [blame] | 925 | * @consumer: Consumer end of the link. |
| 926 | * @supplier: Supplier end of the link. |
| 927 | * |
| 928 | * The caller must ensure proper synchronization of this function with runtime |
| 929 | * PM. |
| 930 | */ |
| 931 | void device_link_remove(void *consumer, struct device *supplier) |
| 932 | { |
| 933 | struct device_link *link; |
| 934 | |
| 935 | if (WARN_ON(consumer == supplier)) |
| 936 | return; |
| 937 | |
| 938 | device_links_write_lock(); |
pascal paillet | d884221 | 2018-07-05 14:25:56 +0000 | [diff] [blame] | 939 | |
| 940 | list_for_each_entry(link, &supplier->links.consumers, s_node) { |
| 941 | if (link->consumer == consumer) { |
Rafael J. Wysocki | 72175d4 | 2019-02-01 01:58:33 +0100 | [diff] [blame] | 942 | device_link_put_kref(link); |
pascal paillet | d884221 | 2018-07-05 14:25:56 +0000 | [diff] [blame] | 943 | break; |
| 944 | } |
| 945 | } |
| 946 | |
pascal paillet | d884221 | 2018-07-05 14:25:56 +0000 | [diff] [blame] | 947 | device_links_write_unlock(); |
| 948 | } |
| 949 | EXPORT_SYMBOL_GPL(device_link_remove); |
| 950 | |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 951 | static void device_links_missing_supplier(struct device *dev) |
| 952 | { |
| 953 | struct device_link *link; |
| 954 | |
Saravana Kannan | 8c3e315 | 2020-05-26 15:09:27 -0700 | [diff] [blame] | 955 | list_for_each_entry(link, &dev->links.suppliers, c_node) { |
| 956 | if (link->status != DL_STATE_CONSUMER_PROBE) |
| 957 | continue; |
| 958 | |
| 959 | if (link->supplier->links.status == DL_DEV_DRIVER_BOUND) { |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 960 | WRITE_ONCE(link->status, DL_STATE_AVAILABLE); |
Saravana Kannan | 8c3e315 | 2020-05-26 15:09:27 -0700 | [diff] [blame] | 961 | } else { |
| 962 | WARN_ON(!(link->flags & DL_FLAG_SYNC_STATE_ONLY)); |
| 963 | WRITE_ONCE(link->status, DL_STATE_DORMANT); |
| 964 | } |
| 965 | } |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 966 | } |
| 967 | |
| 968 | /** |
| 969 | * device_links_check_suppliers - Check presence of supplier drivers. |
| 970 | * @dev: Consumer device. |
| 971 | * |
| 972 | * Check links from this device to any suppliers. Walk the list of the device's |
| 973 | * links to suppliers and see if all of them are available. If not, simply |
| 974 | * return -EPROBE_DEFER. |
| 975 | * |
| 976 | * We need to guarantee that the supplier will not go away after the check has |
| 977 | * been positive here. It only can go away in __device_release_driver() and |
| 978 | * that function checks the device's links to consumers. This means we need to |
| 979 | * mark the link as "consumer probe in progress" to make the supplier removal |
| 980 | * wait for us to complete (or bad things may happen). |
| 981 | * |
Rafael J. Wysocki | 515db26 | 2019-07-16 17:21:06 +0200 | [diff] [blame] | 982 | * Links without the DL_FLAG_MANAGED flag set are ignored. |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 983 | */ |
| 984 | int device_links_check_suppliers(struct device *dev) |
| 985 | { |
| 986 | struct device_link *link; |
| 987 | int ret = 0; |
Saravana Kannan | 68223ee | 2021-09-15 10:28:05 -0700 | [diff] [blame] | 988 | struct fwnode_handle *sup_fw; |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 989 | |
Saravana Kannan | e2ae9bc | 2019-09-04 14:11:21 -0700 | [diff] [blame] | 990 | /* |
| 991 | * Device waiting for supplier to become available is not allowed to |
| 992 | * probe. |
| 993 | */ |
Saravana Kannan | 25ac86c | 2020-11-20 18:02:28 -0800 | [diff] [blame] | 994 | mutex_lock(&fwnode_link_lock); |
| 995 | if (dev->fwnode && !list_empty(&dev->fwnode->suppliers) && |
| 996 | !fw_devlink_is_permissive()) { |
Saravana Kannan | 68223ee | 2021-09-15 10:28:05 -0700 | [diff] [blame] | 997 | sup_fw = list_first_entry(&dev->fwnode->suppliers, |
| 998 | struct fwnode_link, |
| 999 | c_hook)->supplier; |
| 1000 | dev_err_probe(dev, -EPROBE_DEFER, "wait for supplier %pfwP\n", |
| 1001 | sup_fw); |
Saravana Kannan | 25ac86c | 2020-11-20 18:02:28 -0800 | [diff] [blame] | 1002 | mutex_unlock(&fwnode_link_lock); |
Saravana Kannan | e2ae9bc | 2019-09-04 14:11:21 -0700 | [diff] [blame] | 1003 | return -EPROBE_DEFER; |
| 1004 | } |
Saravana Kannan | 25ac86c | 2020-11-20 18:02:28 -0800 | [diff] [blame] | 1005 | mutex_unlock(&fwnode_link_lock); |
Saravana Kannan | e2ae9bc | 2019-09-04 14:11:21 -0700 | [diff] [blame] | 1006 | |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 1007 | device_links_write_lock(); |
| 1008 | |
| 1009 | list_for_each_entry(link, &dev->links.suppliers, c_node) { |
Saravana Kannan | 8c3e315 | 2020-05-26 15:09:27 -0700 | [diff] [blame] | 1010 | if (!(link->flags & DL_FLAG_MANAGED)) |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 1011 | continue; |
| 1012 | |
Saravana Kannan | 8c3e315 | 2020-05-26 15:09:27 -0700 | [diff] [blame] | 1013 | if (link->status != DL_STATE_AVAILABLE && |
| 1014 | !(link->flags & DL_FLAG_SYNC_STATE_ONLY)) { |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 1015 | device_links_missing_supplier(dev); |
Saravana Kannan | 68223ee | 2021-09-15 10:28:05 -0700 | [diff] [blame] | 1016 | dev_err_probe(dev, -EPROBE_DEFER, |
| 1017 | "supplier %s not ready\n", |
| 1018 | dev_name(link->supplier)); |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 1019 | ret = -EPROBE_DEFER; |
| 1020 | break; |
| 1021 | } |
| 1022 | WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE); |
| 1023 | } |
| 1024 | dev->links.status = DL_DEV_PROBING; |
| 1025 | |
| 1026 | device_links_write_unlock(); |
| 1027 | return ret; |
| 1028 | } |
| 1029 | |
Saravana Kannan | 26e7770 | 2019-11-14 14:56:45 -0800 | [diff] [blame] | 1030 | /** |
| 1031 | * __device_links_queue_sync_state - Queue a device for sync_state() callback |
| 1032 | * @dev: Device to call sync_state() on |
| 1033 | * @list: List head to queue the @dev on |
| 1034 | * |
| 1035 | * Queues a device for a sync_state() callback when the device links write lock |
| 1036 | * isn't held. This allows the sync_state() execution flow to use device links |
| 1037 | * APIs. The caller must ensure this function is called with |
| 1038 | * device_links_write_lock() held. |
| 1039 | * |
| 1040 | * This function does a get_device() to make sure the device is not freed while |
| 1041 | * on this list. |
| 1042 | * |
| 1043 | * So the caller must also ensure that device_links_flush_sync_list() is called |
| 1044 | * as soon as the caller releases device_links_write_lock(). This is necessary |
| 1045 | * to make sure the sync_state() is called in a timely fashion and the |
| 1046 | * put_device() is called on this device. |
| 1047 | */ |
| 1048 | static void __device_links_queue_sync_state(struct device *dev, |
| 1049 | struct list_head *list) |
Saravana Kannan | fc5a251 | 2019-09-04 14:11:23 -0700 | [diff] [blame] | 1050 | { |
| 1051 | struct device_link *link; |
| 1052 | |
Saravana Kannan | 7703616 | 2020-02-21 00:05:10 -0800 | [diff] [blame] | 1053 | if (!dev_has_sync_state(dev)) |
| 1054 | return; |
Saravana Kannan | fc5a251 | 2019-09-04 14:11:23 -0700 | [diff] [blame] | 1055 | if (dev->state_synced) |
| 1056 | return; |
| 1057 | |
| 1058 | list_for_each_entry(link, &dev->links.consumers, s_node) { |
| 1059 | if (!(link->flags & DL_FLAG_MANAGED)) |
| 1060 | continue; |
| 1061 | if (link->status != DL_STATE_ACTIVE) |
| 1062 | return; |
| 1063 | } |
| 1064 | |
Saravana Kannan | 26e7770 | 2019-11-14 14:56:45 -0800 | [diff] [blame] | 1065 | /* |
| 1066 | * Set the flag here to avoid adding the same device to a list more |
| 1067 | * than once. This can happen if new consumers get added to the device |
| 1068 | * and probed before the list is flushed. |
| 1069 | */ |
Saravana Kannan | fc5a251 | 2019-09-04 14:11:23 -0700 | [diff] [blame] | 1070 | dev->state_synced = true; |
Saravana Kannan | 26e7770 | 2019-11-14 14:56:45 -0800 | [diff] [blame] | 1071 | |
Saravana Kannan | 3b052a3 | 2020-11-20 18:02:17 -0800 | [diff] [blame] | 1072 | if (WARN_ON(!list_empty(&dev->links.defer_sync))) |
Saravana Kannan | 26e7770 | 2019-11-14 14:56:45 -0800 | [diff] [blame] | 1073 | return; |
| 1074 | |
| 1075 | get_device(dev); |
Saravana Kannan | 3b052a3 | 2020-11-20 18:02:17 -0800 | [diff] [blame] | 1076 | list_add_tail(&dev->links.defer_sync, list); |
Saravana Kannan | 26e7770 | 2019-11-14 14:56:45 -0800 | [diff] [blame] | 1077 | } |
| 1078 | |
| 1079 | /** |
| 1080 | * device_links_flush_sync_list - Call sync_state() on a list of devices |
| 1081 | * @list: List of devices to call sync_state() on |
Saravana Kannan | 21eb93f | 2020-02-21 00:05:08 -0800 | [diff] [blame] | 1082 | * @dont_lock_dev: Device for which lock is already held by the caller |
Saravana Kannan | 26e7770 | 2019-11-14 14:56:45 -0800 | [diff] [blame] | 1083 | * |
| 1084 | * Calls sync_state() on all the devices that have been queued for it. This |
Saravana Kannan | 21eb93f | 2020-02-21 00:05:08 -0800 | [diff] [blame] | 1085 | * function is used in conjunction with __device_links_queue_sync_state(). The |
| 1086 | * @dont_lock_dev parameter is useful when this function is called from a |
| 1087 | * context where a device lock is already held. |
Saravana Kannan | 26e7770 | 2019-11-14 14:56:45 -0800 | [diff] [blame] | 1088 | */ |
Saravana Kannan | 21eb93f | 2020-02-21 00:05:08 -0800 | [diff] [blame] | 1089 | static void device_links_flush_sync_list(struct list_head *list, |
| 1090 | struct device *dont_lock_dev) |
Saravana Kannan | 26e7770 | 2019-11-14 14:56:45 -0800 | [diff] [blame] | 1091 | { |
| 1092 | struct device *dev, *tmp; |
| 1093 | |
Saravana Kannan | 3b052a3 | 2020-11-20 18:02:17 -0800 | [diff] [blame] | 1094 | list_for_each_entry_safe(dev, tmp, list, links.defer_sync) { |
| 1095 | list_del_init(&dev->links.defer_sync); |
Saravana Kannan | 26e7770 | 2019-11-14 14:56:45 -0800 | [diff] [blame] | 1096 | |
Saravana Kannan | 21eb93f | 2020-02-21 00:05:08 -0800 | [diff] [blame] | 1097 | if (dev != dont_lock_dev) |
| 1098 | device_lock(dev); |
Saravana Kannan | 26e7770 | 2019-11-14 14:56:45 -0800 | [diff] [blame] | 1099 | |
| 1100 | if (dev->bus->sync_state) |
| 1101 | dev->bus->sync_state(dev); |
| 1102 | else if (dev->driver && dev->driver->sync_state) |
| 1103 | dev->driver->sync_state(dev); |
| 1104 | |
Saravana Kannan | 21eb93f | 2020-02-21 00:05:08 -0800 | [diff] [blame] | 1105 | if (dev != dont_lock_dev) |
| 1106 | device_unlock(dev); |
Saravana Kannan | 26e7770 | 2019-11-14 14:56:45 -0800 | [diff] [blame] | 1107 | |
| 1108 | put_device(dev); |
| 1109 | } |
Saravana Kannan | fc5a251 | 2019-09-04 14:11:23 -0700 | [diff] [blame] | 1110 | } |
| 1111 | |
| 1112 | void device_links_supplier_sync_state_pause(void) |
| 1113 | { |
| 1114 | device_links_write_lock(); |
| 1115 | defer_sync_state_count++; |
| 1116 | device_links_write_unlock(); |
| 1117 | } |
| 1118 | |
| 1119 | void device_links_supplier_sync_state_resume(void) |
| 1120 | { |
| 1121 | struct device *dev, *tmp; |
Saravana Kannan | 26e7770 | 2019-11-14 14:56:45 -0800 | [diff] [blame] | 1122 | LIST_HEAD(sync_list); |
Saravana Kannan | fc5a251 | 2019-09-04 14:11:23 -0700 | [diff] [blame] | 1123 | |
| 1124 | device_links_write_lock(); |
| 1125 | if (!defer_sync_state_count) { |
| 1126 | WARN(true, "Unmatched sync_state pause/resume!"); |
| 1127 | goto out; |
| 1128 | } |
| 1129 | defer_sync_state_count--; |
| 1130 | if (defer_sync_state_count) |
| 1131 | goto out; |
| 1132 | |
Saravana Kannan | 3b052a3 | 2020-11-20 18:02:17 -0800 | [diff] [blame] | 1133 | list_for_each_entry_safe(dev, tmp, &deferred_sync, links.defer_sync) { |
Saravana Kannan | 26e7770 | 2019-11-14 14:56:45 -0800 | [diff] [blame] | 1134 | /* |
| 1135 | * Delete from deferred_sync list before queuing it to |
Saravana Kannan | 3b052a3 | 2020-11-20 18:02:17 -0800 | [diff] [blame] | 1136 | * sync_list because defer_sync is used for both lists. |
Saravana Kannan | 26e7770 | 2019-11-14 14:56:45 -0800 | [diff] [blame] | 1137 | */ |
Saravana Kannan | 3b052a3 | 2020-11-20 18:02:17 -0800 | [diff] [blame] | 1138 | list_del_init(&dev->links.defer_sync); |
Saravana Kannan | 26e7770 | 2019-11-14 14:56:45 -0800 | [diff] [blame] | 1139 | __device_links_queue_sync_state(dev, &sync_list); |
Saravana Kannan | fc5a251 | 2019-09-04 14:11:23 -0700 | [diff] [blame] | 1140 | } |
| 1141 | out: |
| 1142 | device_links_write_unlock(); |
Saravana Kannan | 26e7770 | 2019-11-14 14:56:45 -0800 | [diff] [blame] | 1143 | |
Saravana Kannan | 21eb93f | 2020-02-21 00:05:08 -0800 | [diff] [blame] | 1144 | device_links_flush_sync_list(&sync_list, NULL); |
Saravana Kannan | fc5a251 | 2019-09-04 14:11:23 -0700 | [diff] [blame] | 1145 | } |
| 1146 | |
| 1147 | static int sync_state_resume_initcall(void) |
| 1148 | { |
| 1149 | device_links_supplier_sync_state_resume(); |
| 1150 | return 0; |
| 1151 | } |
| 1152 | late_initcall(sync_state_resume_initcall); |
| 1153 | |
| 1154 | static void __device_links_supplier_defer_sync(struct device *sup) |
| 1155 | { |
Saravana Kannan | 3b052a3 | 2020-11-20 18:02:17 -0800 | [diff] [blame] | 1156 | if (list_empty(&sup->links.defer_sync) && dev_has_sync_state(sup)) |
| 1157 | list_add_tail(&sup->links.defer_sync, &deferred_sync); |
Saravana Kannan | fc5a251 | 2019-09-04 14:11:23 -0700 | [diff] [blame] | 1158 | } |
| 1159 | |
Saravana Kannan | 21c27f0 | 2020-05-18 23:30:00 -0700 | [diff] [blame] | 1160 | static void device_link_drop_managed(struct device_link *link) |
| 1161 | { |
| 1162 | link->flags &= ~DL_FLAG_MANAGED; |
| 1163 | WRITE_ONCE(link->status, DL_STATE_NONE); |
| 1164 | kref_put(&link->kref, __device_link_del); |
| 1165 | } |
| 1166 | |
Saravana Kannan | da6d647 | 2020-05-21 12:18:00 -0700 | [diff] [blame] | 1167 | static ssize_t waiting_for_supplier_show(struct device *dev, |
| 1168 | struct device_attribute *attr, |
| 1169 | char *buf) |
| 1170 | { |
| 1171 | bool val; |
| 1172 | |
| 1173 | device_lock(dev); |
Saravana Kannan | 25ac86c | 2020-11-20 18:02:28 -0800 | [diff] [blame] | 1174 | val = !list_empty(&dev->fwnode->suppliers); |
Saravana Kannan | da6d647 | 2020-05-21 12:18:00 -0700 | [diff] [blame] | 1175 | device_unlock(dev); |
Joe Perches | aa83889 | 2020-09-16 13:40:39 -0700 | [diff] [blame] | 1176 | return sysfs_emit(buf, "%u\n", val); |
Saravana Kannan | da6d647 | 2020-05-21 12:18:00 -0700 | [diff] [blame] | 1177 | } |
| 1178 | static DEVICE_ATTR_RO(waiting_for_supplier); |
| 1179 | |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 1180 | /** |
Saravana Kannan | b6f617d | 2021-03-02 13:11:31 -0800 | [diff] [blame] | 1181 | * device_links_force_bind - Prepares device to be force bound |
| 1182 | * @dev: Consumer device. |
| 1183 | * |
| 1184 | * device_bind_driver() force binds a device to a driver without calling any |
| 1185 | * driver probe functions. So the consumer really isn't going to wait for any |
| 1186 | * supplier before it's bound to the driver. We still want the device link |
| 1187 | * states to be sensible when this happens. |
| 1188 | * |
| 1189 | * In preparation for device_bind_driver(), this function goes through each |
| 1190 | * supplier device links and checks if the supplier is bound. If it is, then |
| 1191 | * the device link status is set to CONSUMER_PROBE. Otherwise, the device link |
| 1192 | * is dropped. Links without the DL_FLAG_MANAGED flag set are ignored. |
| 1193 | */ |
| 1194 | void device_links_force_bind(struct device *dev) |
| 1195 | { |
| 1196 | struct device_link *link, *ln; |
| 1197 | |
| 1198 | device_links_write_lock(); |
| 1199 | |
| 1200 | list_for_each_entry_safe(link, ln, &dev->links.suppliers, c_node) { |
| 1201 | if (!(link->flags & DL_FLAG_MANAGED)) |
| 1202 | continue; |
| 1203 | |
| 1204 | if (link->status != DL_STATE_AVAILABLE) { |
| 1205 | device_link_drop_managed(link); |
| 1206 | continue; |
| 1207 | } |
| 1208 | WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE); |
| 1209 | } |
| 1210 | dev->links.status = DL_DEV_PROBING; |
| 1211 | |
| 1212 | device_links_write_unlock(); |
| 1213 | } |
| 1214 | |
| 1215 | /** |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 1216 | * device_links_driver_bound - Update device links after probing its driver. |
| 1217 | * @dev: Device to update the links for. |
| 1218 | * |
| 1219 | * The probe has been successful, so update links from this device to any |
| 1220 | * consumers by changing their status to "available". |
| 1221 | * |
| 1222 | * Also change the status of @dev's links to suppliers to "active". |
| 1223 | * |
Rafael J. Wysocki | 515db26 | 2019-07-16 17:21:06 +0200 | [diff] [blame] | 1224 | * Links without the DL_FLAG_MANAGED flag set are ignored. |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 1225 | */ |
| 1226 | void device_links_driver_bound(struct device *dev) |
| 1227 | { |
Saravana Kannan | 21c27f0 | 2020-05-18 23:30:00 -0700 | [diff] [blame] | 1228 | struct device_link *link, *ln; |
Saravana Kannan | 26e7770 | 2019-11-14 14:56:45 -0800 | [diff] [blame] | 1229 | LIST_HEAD(sync_list); |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 1230 | |
Saravana Kannan | bcbbcfd | 2019-10-28 15:00:23 -0700 | [diff] [blame] | 1231 | /* |
Saravana Kannan | 9528e0d | 2021-02-05 14:26:37 -0800 | [diff] [blame] | 1232 | * If a device binds successfully, it's expected to have created all |
Saravana Kannan | bcbbcfd | 2019-10-28 15:00:23 -0700 | [diff] [blame] | 1233 | * the device links it needs to or make new device links as it needs |
Saravana Kannan | 9528e0d | 2021-02-05 14:26:37 -0800 | [diff] [blame] | 1234 | * them. So, fw_devlink no longer needs to create device links to any |
| 1235 | * of the device's suppliers. |
| 1236 | * |
| 1237 | * Also, if a child firmware node of this bound device is not added as |
| 1238 | * a device by now, assume it is never going to be added and make sure |
| 1239 | * other devices don't defer probe indefinitely by waiting for such a |
| 1240 | * child device. |
Saravana Kannan | bcbbcfd | 2019-10-28 15:00:23 -0700 | [diff] [blame] | 1241 | */ |
Saravana Kannan | 9528e0d | 2021-02-05 14:26:37 -0800 | [diff] [blame] | 1242 | if (dev->fwnode && dev->fwnode->dev == dev) { |
| 1243 | struct fwnode_handle *child; |
Saravana Kannan | f9aa460 | 2020-11-20 18:02:31 -0800 | [diff] [blame] | 1244 | fwnode_links_purge_suppliers(dev->fwnode); |
Saravana Kannan | 9528e0d | 2021-02-05 14:26:37 -0800 | [diff] [blame] | 1245 | fwnode_for_each_available_child_node(dev->fwnode, child) |
| 1246 | fw_devlink_purge_absent_suppliers(child); |
| 1247 | } |
Saravana Kannan | da6d647 | 2020-05-21 12:18:00 -0700 | [diff] [blame] | 1248 | device_remove_file(dev, &dev_attr_waiting_for_supplier); |
Saravana Kannan | bcbbcfd | 2019-10-28 15:00:23 -0700 | [diff] [blame] | 1249 | |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 1250 | device_links_write_lock(); |
| 1251 | |
| 1252 | list_for_each_entry(link, &dev->links.consumers, s_node) { |
Rafael J. Wysocki | 515db26 | 2019-07-16 17:21:06 +0200 | [diff] [blame] | 1253 | if (!(link->flags & DL_FLAG_MANAGED)) |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 1254 | continue; |
| 1255 | |
Rafael J. Wysocki | 15cfb09 | 2019-02-01 01:50:39 +0100 | [diff] [blame] | 1256 | /* |
| 1257 | * Links created during consumer probe may be in the "consumer |
| 1258 | * probe" state to start with if the supplier is still probing |
| 1259 | * when they are created and they may become "active" if the |
| 1260 | * consumer probe returns first. Skip them here. |
| 1261 | */ |
| 1262 | if (link->status == DL_STATE_CONSUMER_PROBE || |
| 1263 | link->status == DL_STATE_ACTIVE) |
| 1264 | continue; |
| 1265 | |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 1266 | WARN_ON(link->status != DL_STATE_DORMANT); |
| 1267 | WRITE_ONCE(link->status, DL_STATE_AVAILABLE); |
Rafael J. Wysocki | e7dd401 | 2019-02-01 01:59:42 +0100 | [diff] [blame] | 1268 | |
| 1269 | if (link->flags & DL_FLAG_AUTOPROBE_CONSUMER) |
| 1270 | driver_deferred_probe_add(link->consumer); |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 1271 | } |
| 1272 | |
Saravana Kannan | 21eb93f | 2020-02-21 00:05:08 -0800 | [diff] [blame] | 1273 | if (defer_sync_state_count) |
| 1274 | __device_links_supplier_defer_sync(dev); |
| 1275 | else |
| 1276 | __device_links_queue_sync_state(dev, &sync_list); |
| 1277 | |
Saravana Kannan | 21c27f0 | 2020-05-18 23:30:00 -0700 | [diff] [blame] | 1278 | list_for_each_entry_safe(link, ln, &dev->links.suppliers, c_node) { |
| 1279 | struct device *supplier; |
| 1280 | |
Rafael J. Wysocki | 515db26 | 2019-07-16 17:21:06 +0200 | [diff] [blame] | 1281 | if (!(link->flags & DL_FLAG_MANAGED)) |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 1282 | continue; |
| 1283 | |
Saravana Kannan | 21c27f0 | 2020-05-18 23:30:00 -0700 | [diff] [blame] | 1284 | supplier = link->supplier; |
| 1285 | if (link->flags & DL_FLAG_SYNC_STATE_ONLY) { |
| 1286 | /* |
| 1287 | * When DL_FLAG_SYNC_STATE_ONLY is set, it means no |
| 1288 | * other DL_MANAGED_LINK_FLAGS have been set. So, it's |
| 1289 | * save to drop the managed link completely. |
| 1290 | */ |
| 1291 | device_link_drop_managed(link); |
| 1292 | } else { |
| 1293 | WARN_ON(link->status != DL_STATE_CONSUMER_PROBE); |
| 1294 | WRITE_ONCE(link->status, DL_STATE_ACTIVE); |
| 1295 | } |
Saravana Kannan | fc5a251 | 2019-09-04 14:11:23 -0700 | [diff] [blame] | 1296 | |
Saravana Kannan | 21c27f0 | 2020-05-18 23:30:00 -0700 | [diff] [blame] | 1297 | /* |
| 1298 | * This needs to be done even for the deleted |
| 1299 | * DL_FLAG_SYNC_STATE_ONLY device link in case it was the last |
| 1300 | * device link that was preventing the supplier from getting a |
| 1301 | * sync_state() call. |
| 1302 | */ |
Saravana Kannan | fc5a251 | 2019-09-04 14:11:23 -0700 | [diff] [blame] | 1303 | if (defer_sync_state_count) |
Saravana Kannan | 21c27f0 | 2020-05-18 23:30:00 -0700 | [diff] [blame] | 1304 | __device_links_supplier_defer_sync(supplier); |
Saravana Kannan | fc5a251 | 2019-09-04 14:11:23 -0700 | [diff] [blame] | 1305 | else |
Saravana Kannan | 21c27f0 | 2020-05-18 23:30:00 -0700 | [diff] [blame] | 1306 | __device_links_queue_sync_state(supplier, &sync_list); |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 1307 | } |
| 1308 | |
| 1309 | dev->links.status = DL_DEV_DRIVER_BOUND; |
| 1310 | |
| 1311 | device_links_write_unlock(); |
Saravana Kannan | 26e7770 | 2019-11-14 14:56:45 -0800 | [diff] [blame] | 1312 | |
Saravana Kannan | 21eb93f | 2020-02-21 00:05:08 -0800 | [diff] [blame] | 1313 | device_links_flush_sync_list(&sync_list, dev); |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 1314 | } |
| 1315 | |
| 1316 | /** |
| 1317 | * __device_links_no_driver - Update links of a device without a driver. |
| 1318 | * @dev: Device without a drvier. |
| 1319 | * |
| 1320 | * Delete all non-persistent links from this device to any suppliers. |
| 1321 | * |
| 1322 | * Persistent links stay around, but their status is changed to "available", |
| 1323 | * unless they already are in the "supplier unbind in progress" state in which |
| 1324 | * case they need not be updated. |
| 1325 | * |
Rafael J. Wysocki | 515db26 | 2019-07-16 17:21:06 +0200 | [diff] [blame] | 1326 | * Links without the DL_FLAG_MANAGED flag set are ignored. |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 1327 | */ |
| 1328 | static void __device_links_no_driver(struct device *dev) |
| 1329 | { |
| 1330 | struct device_link *link, *ln; |
| 1331 | |
| 1332 | 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] | 1333 | if (!(link->flags & DL_FLAG_MANAGED)) |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 1334 | continue; |
| 1335 | |
Saravana Kannan | 8c3e315 | 2020-05-26 15:09:27 -0700 | [diff] [blame] | 1336 | if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER) { |
Rafael J. Wysocki | 515db26 | 2019-07-16 17:21:06 +0200 | [diff] [blame] | 1337 | device_link_drop_managed(link); |
Saravana Kannan | 8c3e315 | 2020-05-26 15:09:27 -0700 | [diff] [blame] | 1338 | continue; |
| 1339 | } |
| 1340 | |
| 1341 | if (link->status != DL_STATE_CONSUMER_PROBE && |
| 1342 | link->status != DL_STATE_ACTIVE) |
| 1343 | continue; |
| 1344 | |
| 1345 | if (link->supplier->links.status == DL_DEV_DRIVER_BOUND) { |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 1346 | WRITE_ONCE(link->status, DL_STATE_AVAILABLE); |
Saravana Kannan | 8c3e315 | 2020-05-26 15:09:27 -0700 | [diff] [blame] | 1347 | } else { |
| 1348 | WARN_ON(!(link->flags & DL_FLAG_SYNC_STATE_ONLY)); |
| 1349 | WRITE_ONCE(link->status, DL_STATE_DORMANT); |
| 1350 | } |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 1351 | } |
| 1352 | |
| 1353 | dev->links.status = DL_DEV_NO_DRIVER; |
| 1354 | } |
| 1355 | |
Rafael J. Wysocki | 15cfb09 | 2019-02-01 01:50:39 +0100 | [diff] [blame] | 1356 | /** |
| 1357 | * device_links_no_driver - Update links after failing driver probe. |
| 1358 | * @dev: Device whose driver has just failed to probe. |
| 1359 | * |
| 1360 | * Clean up leftover links to consumers for @dev and invoke |
| 1361 | * %__device_links_no_driver() to update links to suppliers for it as |
| 1362 | * appropriate. |
| 1363 | * |
Rafael J. Wysocki | 515db26 | 2019-07-16 17:21:06 +0200 | [diff] [blame] | 1364 | * Links without the DL_FLAG_MANAGED flag set are ignored. |
Rafael J. Wysocki | 15cfb09 | 2019-02-01 01:50:39 +0100 | [diff] [blame] | 1365 | */ |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 1366 | void device_links_no_driver(struct device *dev) |
| 1367 | { |
Rafael J. Wysocki | 15cfb09 | 2019-02-01 01:50:39 +0100 | [diff] [blame] | 1368 | struct device_link *link; |
| 1369 | |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 1370 | device_links_write_lock(); |
Rafael J. Wysocki | 15cfb09 | 2019-02-01 01:50:39 +0100 | [diff] [blame] | 1371 | |
| 1372 | list_for_each_entry(link, &dev->links.consumers, s_node) { |
Rafael J. Wysocki | 515db26 | 2019-07-16 17:21:06 +0200 | [diff] [blame] | 1373 | if (!(link->flags & DL_FLAG_MANAGED)) |
Rafael J. Wysocki | 15cfb09 | 2019-02-01 01:50:39 +0100 | [diff] [blame] | 1374 | continue; |
| 1375 | |
| 1376 | /* |
| 1377 | * The probe has failed, so if the status of the link is |
| 1378 | * "consumer probe" or "active", it must have been added by |
| 1379 | * a probing consumer while this device was still probing. |
| 1380 | * Change its state to "dormant", as it represents a valid |
| 1381 | * relationship, but it is not functionally meaningful. |
| 1382 | */ |
| 1383 | if (link->status == DL_STATE_CONSUMER_PROBE || |
| 1384 | link->status == DL_STATE_ACTIVE) |
| 1385 | WRITE_ONCE(link->status, DL_STATE_DORMANT); |
| 1386 | } |
| 1387 | |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 1388 | __device_links_no_driver(dev); |
Rafael J. Wysocki | 15cfb09 | 2019-02-01 01:50:39 +0100 | [diff] [blame] | 1389 | |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 1390 | device_links_write_unlock(); |
| 1391 | } |
| 1392 | |
| 1393 | /** |
| 1394 | * device_links_driver_cleanup - Update links after driver removal. |
| 1395 | * @dev: Device whose driver has just gone away. |
| 1396 | * |
| 1397 | * Update links to consumers for @dev by changing their status to "dormant" and |
| 1398 | * invoke %__device_links_no_driver() to update links to suppliers for it as |
| 1399 | * appropriate. |
| 1400 | * |
Rafael J. Wysocki | 515db26 | 2019-07-16 17:21:06 +0200 | [diff] [blame] | 1401 | * Links without the DL_FLAG_MANAGED flag set are ignored. |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 1402 | */ |
| 1403 | void device_links_driver_cleanup(struct device *dev) |
| 1404 | { |
Rafael J. Wysocki | c8d5098 | 2019-02-01 01:45:55 +0100 | [diff] [blame] | 1405 | struct device_link *link, *ln; |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 1406 | |
| 1407 | device_links_write_lock(); |
| 1408 | |
Rafael J. Wysocki | c8d5098 | 2019-02-01 01:45:55 +0100 | [diff] [blame] | 1409 | 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] | 1410 | if (!(link->flags & DL_FLAG_MANAGED)) |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 1411 | continue; |
| 1412 | |
Vivek Gautam | e88728f | 2018-06-27 18:20:55 +0530 | [diff] [blame] | 1413 | WARN_ON(link->flags & DL_FLAG_AUTOREMOVE_CONSUMER); |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 1414 | WARN_ON(link->status != DL_STATE_SUPPLIER_UNBIND); |
Vivek Gautam | 1689cac | 2018-06-27 18:20:56 +0530 | [diff] [blame] | 1415 | |
| 1416 | /* |
| 1417 | * autoremove the links between this @dev and its consumer |
| 1418 | * devices that are not active, i.e. where the link state |
| 1419 | * has moved to DL_STATE_SUPPLIER_UNBIND. |
| 1420 | */ |
| 1421 | if (link->status == DL_STATE_SUPPLIER_UNBIND && |
| 1422 | link->flags & DL_FLAG_AUTOREMOVE_SUPPLIER) |
Rafael J. Wysocki | 515db26 | 2019-07-16 17:21:06 +0200 | [diff] [blame] | 1423 | device_link_drop_managed(link); |
Vivek Gautam | 1689cac | 2018-06-27 18:20:56 +0530 | [diff] [blame] | 1424 | |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 1425 | WRITE_ONCE(link->status, DL_STATE_DORMANT); |
| 1426 | } |
| 1427 | |
Saravana Kannan | 3b052a3 | 2020-11-20 18:02:17 -0800 | [diff] [blame] | 1428 | list_del_init(&dev->links.defer_sync); |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 1429 | __device_links_no_driver(dev); |
| 1430 | |
| 1431 | device_links_write_unlock(); |
| 1432 | } |
| 1433 | |
| 1434 | /** |
| 1435 | * device_links_busy - Check if there are any busy links to consumers. |
| 1436 | * @dev: Device to check. |
| 1437 | * |
| 1438 | * Check each consumer of the device and return 'true' if its link's status |
| 1439 | * is one of "consumer probe" or "active" (meaning that the given consumer is |
| 1440 | * probing right now or its driver is present). Otherwise, change the link |
| 1441 | * state to "supplier unbind" to prevent the consumer from being probed |
| 1442 | * successfully going forward. |
| 1443 | * |
| 1444 | * Return 'false' if there are no probing or active consumers. |
| 1445 | * |
Rafael J. Wysocki | 515db26 | 2019-07-16 17:21:06 +0200 | [diff] [blame] | 1446 | * Links without the DL_FLAG_MANAGED flag set are ignored. |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 1447 | */ |
| 1448 | bool device_links_busy(struct device *dev) |
| 1449 | { |
| 1450 | struct device_link *link; |
| 1451 | bool ret = false; |
| 1452 | |
| 1453 | device_links_write_lock(); |
| 1454 | |
| 1455 | list_for_each_entry(link, &dev->links.consumers, s_node) { |
Rafael J. Wysocki | 515db26 | 2019-07-16 17:21:06 +0200 | [diff] [blame] | 1456 | if (!(link->flags & DL_FLAG_MANAGED)) |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 1457 | continue; |
| 1458 | |
| 1459 | if (link->status == DL_STATE_CONSUMER_PROBE |
| 1460 | || link->status == DL_STATE_ACTIVE) { |
| 1461 | ret = true; |
| 1462 | break; |
| 1463 | } |
| 1464 | WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND); |
| 1465 | } |
| 1466 | |
| 1467 | dev->links.status = DL_DEV_UNBINDING; |
| 1468 | |
| 1469 | device_links_write_unlock(); |
| 1470 | return ret; |
| 1471 | } |
| 1472 | |
| 1473 | /** |
| 1474 | * device_links_unbind_consumers - Force unbind consumers of the given device. |
| 1475 | * @dev: Device to unbind the consumers of. |
| 1476 | * |
| 1477 | * Walk the list of links to consumers for @dev and if any of them is in the |
| 1478 | * "consumer probe" state, wait for all device probes in progress to complete |
| 1479 | * and start over. |
| 1480 | * |
| 1481 | * If that's not the case, change the status of the link to "supplier unbind" |
| 1482 | * and check if the link was in the "active" state. If so, force the consumer |
| 1483 | * driver to unbind and start over (the consumer will not re-probe as we have |
| 1484 | * changed the state of the link already). |
| 1485 | * |
Rafael J. Wysocki | 515db26 | 2019-07-16 17:21:06 +0200 | [diff] [blame] | 1486 | * Links without the DL_FLAG_MANAGED flag set are ignored. |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 1487 | */ |
| 1488 | void device_links_unbind_consumers(struct device *dev) |
| 1489 | { |
| 1490 | struct device_link *link; |
| 1491 | |
| 1492 | start: |
| 1493 | device_links_write_lock(); |
| 1494 | |
| 1495 | list_for_each_entry(link, &dev->links.consumers, s_node) { |
| 1496 | enum device_link_state status; |
| 1497 | |
Saravana Kannan | 05ef983 | 2019-10-28 15:00:22 -0700 | [diff] [blame] | 1498 | if (!(link->flags & DL_FLAG_MANAGED) || |
| 1499 | link->flags & DL_FLAG_SYNC_STATE_ONLY) |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 1500 | continue; |
| 1501 | |
| 1502 | status = link->status; |
| 1503 | if (status == DL_STATE_CONSUMER_PROBE) { |
| 1504 | device_links_write_unlock(); |
| 1505 | |
| 1506 | wait_for_device_probe(); |
| 1507 | goto start; |
| 1508 | } |
| 1509 | WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND); |
| 1510 | if (status == DL_STATE_ACTIVE) { |
| 1511 | struct device *consumer = link->consumer; |
| 1512 | |
| 1513 | get_device(consumer); |
| 1514 | |
| 1515 | device_links_write_unlock(); |
| 1516 | |
| 1517 | device_release_driver_internal(consumer, NULL, |
| 1518 | consumer->parent); |
| 1519 | put_device(consumer); |
| 1520 | goto start; |
| 1521 | } |
| 1522 | } |
| 1523 | |
| 1524 | device_links_write_unlock(); |
| 1525 | } |
| 1526 | |
| 1527 | /** |
| 1528 | * device_links_purge - Delete existing links to other devices. |
| 1529 | * @dev: Target device. |
| 1530 | */ |
| 1531 | static void device_links_purge(struct device *dev) |
| 1532 | { |
| 1533 | struct device_link *link, *ln; |
| 1534 | |
Saravana Kannan | 287905e | 2020-05-21 12:17:58 -0700 | [diff] [blame] | 1535 | if (dev->class == &devlink_class) |
| 1536 | return; |
| 1537 | |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 1538 | /* |
| 1539 | * Delete all of the remaining links from this device to any other |
| 1540 | * devices (either consumers or suppliers). |
| 1541 | */ |
| 1542 | device_links_write_lock(); |
| 1543 | |
| 1544 | list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) { |
| 1545 | WARN_ON(link->status == DL_STATE_ACTIVE); |
Lukas Wunner | ead18c2 | 2018-02-10 19:27:12 +0100 | [diff] [blame] | 1546 | __device_link_del(&link->kref); |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 1547 | } |
| 1548 | |
| 1549 | list_for_each_entry_safe_reverse(link, ln, &dev->links.consumers, s_node) { |
| 1550 | WARN_ON(link->status != DL_STATE_DORMANT && |
| 1551 | link->status != DL_STATE_NONE); |
Lukas Wunner | ead18c2 | 2018-02-10 19:27:12 +0100 | [diff] [blame] | 1552 | __device_link_del(&link->kref); |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 1553 | } |
| 1554 | |
| 1555 | device_links_write_unlock(); |
| 1556 | } |
| 1557 | |
Saravana Kannan | b90fb8f | 2020-12-17 19:17:01 -0800 | [diff] [blame] | 1558 | #define FW_DEVLINK_FLAGS_PERMISSIVE (DL_FLAG_INFERRED | \ |
| 1559 | DL_FLAG_SYNC_STATE_ONLY) |
| 1560 | #define FW_DEVLINK_FLAGS_ON (DL_FLAG_INFERRED | \ |
| 1561 | DL_FLAG_AUTOPROBE_CONSUMER) |
| 1562 | #define FW_DEVLINK_FLAGS_RPM (FW_DEVLINK_FLAGS_ON | \ |
| 1563 | DL_FLAG_PM_RUNTIME) |
| 1564 | |
Saravana Kannan | ea718c69 | 2021-03-02 13:11:32 -0800 | [diff] [blame] | 1565 | static u32 fw_devlink_flags = FW_DEVLINK_FLAGS_ON; |
Saravana Kannan | 42926ac | 2020-05-14 22:34:57 -0700 | [diff] [blame] | 1566 | static int __init fw_devlink_setup(char *arg) |
| 1567 | { |
| 1568 | if (!arg) |
| 1569 | return -EINVAL; |
| 1570 | |
| 1571 | if (strcmp(arg, "off") == 0) { |
| 1572 | fw_devlink_flags = 0; |
| 1573 | } else if (strcmp(arg, "permissive") == 0) { |
Saravana Kannan | b90fb8f | 2020-12-17 19:17:01 -0800 | [diff] [blame] | 1574 | fw_devlink_flags = FW_DEVLINK_FLAGS_PERMISSIVE; |
Saravana Kannan | 42926ac | 2020-05-14 22:34:57 -0700 | [diff] [blame] | 1575 | } else if (strcmp(arg, "on") == 0) { |
Saravana Kannan | b90fb8f | 2020-12-17 19:17:01 -0800 | [diff] [blame] | 1576 | fw_devlink_flags = FW_DEVLINK_FLAGS_ON; |
Saravana Kannan | 42926ac | 2020-05-14 22:34:57 -0700 | [diff] [blame] | 1577 | } else if (strcmp(arg, "rpm") == 0) { |
Saravana Kannan | b90fb8f | 2020-12-17 19:17:01 -0800 | [diff] [blame] | 1578 | fw_devlink_flags = FW_DEVLINK_FLAGS_RPM; |
Saravana Kannan | 42926ac | 2020-05-14 22:34:57 -0700 | [diff] [blame] | 1579 | } |
| 1580 | return 0; |
| 1581 | } |
| 1582 | early_param("fw_devlink", fw_devlink_setup); |
| 1583 | |
Saravana Kannan | 19d0f5f | 2021-02-05 14:26:39 -0800 | [diff] [blame] | 1584 | static bool fw_devlink_strict; |
| 1585 | static int __init fw_devlink_strict_setup(char *arg) |
| 1586 | { |
| 1587 | return strtobool(arg, &fw_devlink_strict); |
| 1588 | } |
| 1589 | early_param("fw_devlink.strict", fw_devlink_strict_setup); |
| 1590 | |
Saravana Kannan | 42926ac | 2020-05-14 22:34:57 -0700 | [diff] [blame] | 1591 | u32 fw_devlink_get_flags(void) |
| 1592 | { |
| 1593 | return fw_devlink_flags; |
| 1594 | } |
| 1595 | |
| 1596 | static bool fw_devlink_is_permissive(void) |
| 1597 | { |
Saravana Kannan | b90fb8f | 2020-12-17 19:17:01 -0800 | [diff] [blame] | 1598 | return fw_devlink_flags == FW_DEVLINK_FLAGS_PERMISSIVE; |
Saravana Kannan | 42926ac | 2020-05-14 22:34:57 -0700 | [diff] [blame] | 1599 | } |
| 1600 | |
Saravana Kannan | 19d0f5f | 2021-02-05 14:26:39 -0800 | [diff] [blame] | 1601 | bool fw_devlink_is_strict(void) |
| 1602 | { |
| 1603 | return fw_devlink_strict && !fw_devlink_is_permissive(); |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 1604 | } |
| 1605 | |
Saravana Kannan | c2c724c | 2020-11-20 18:02:27 -0800 | [diff] [blame] | 1606 | static void fw_devlink_parse_fwnode(struct fwnode_handle *fwnode) |
| 1607 | { |
| 1608 | if (fwnode->flags & FWNODE_FLAG_LINKS_ADDED) |
| 1609 | return; |
| 1610 | |
Saravana Kannan | 2d09e6e | 2020-11-20 18:02:32 -0800 | [diff] [blame] | 1611 | fwnode_call_int_op(fwnode, add_links); |
Saravana Kannan | c2c724c | 2020-11-20 18:02:27 -0800 | [diff] [blame] | 1612 | fwnode->flags |= FWNODE_FLAG_LINKS_ADDED; |
| 1613 | } |
| 1614 | |
| 1615 | static void fw_devlink_parse_fwtree(struct fwnode_handle *fwnode) |
| 1616 | { |
| 1617 | struct fwnode_handle *child = NULL; |
| 1618 | |
| 1619 | fw_devlink_parse_fwnode(fwnode); |
| 1620 | |
| 1621 | while ((child = fwnode_get_next_available_child_node(fwnode, child))) |
| 1622 | fw_devlink_parse_fwtree(child); |
| 1623 | } |
| 1624 | |
Saravana Kannan | d46f3e3 | 2021-04-01 21:03:41 -0700 | [diff] [blame] | 1625 | static void fw_devlink_relax_link(struct device_link *link) |
| 1626 | { |
| 1627 | if (!(link->flags & DL_FLAG_INFERRED)) |
| 1628 | return; |
| 1629 | |
| 1630 | if (link->flags == (DL_FLAG_MANAGED | FW_DEVLINK_FLAGS_PERMISSIVE)) |
| 1631 | return; |
| 1632 | |
| 1633 | pm_runtime_drop_link(link); |
| 1634 | link->flags = DL_FLAG_MANAGED | FW_DEVLINK_FLAGS_PERMISSIVE; |
| 1635 | dev_dbg(link->consumer, "Relaxing link with %s\n", |
| 1636 | dev_name(link->supplier)); |
| 1637 | } |
| 1638 | |
| 1639 | static int fw_devlink_no_driver(struct device *dev, void *data) |
| 1640 | { |
| 1641 | struct device_link *link = to_devlink(dev); |
| 1642 | |
| 1643 | if (!link->supplier->can_match) |
| 1644 | fw_devlink_relax_link(link); |
| 1645 | |
| 1646 | return 0; |
| 1647 | } |
| 1648 | |
| 1649 | void fw_devlink_drivers_done(void) |
| 1650 | { |
| 1651 | fw_devlink_drv_reg_done = true; |
| 1652 | device_links_write_lock(); |
| 1653 | class_for_each_device(&devlink_class, NULL, NULL, |
| 1654 | fw_devlink_no_driver); |
| 1655 | device_links_write_unlock(); |
| 1656 | } |
| 1657 | |
| 1658 | static void fw_devlink_unblock_consumers(struct device *dev) |
| 1659 | { |
| 1660 | struct device_link *link; |
| 1661 | |
| 1662 | if (!fw_devlink_flags || fw_devlink_is_permissive()) |
| 1663 | return; |
| 1664 | |
| 1665 | device_links_write_lock(); |
| 1666 | list_for_each_entry(link, &dev->links.consumers, s_node) |
| 1667 | fw_devlink_relax_link(link); |
| 1668 | device_links_write_unlock(); |
| 1669 | } |
| 1670 | |
Saravana Kannan | f9aa460 | 2020-11-20 18:02:31 -0800 | [diff] [blame] | 1671 | /** |
Saravana Kannan | b0e2fa4 | 2020-12-17 19:17:02 -0800 | [diff] [blame] | 1672 | * fw_devlink_relax_cycle - Convert cyclic links to SYNC_STATE_ONLY links |
| 1673 | * @con: Device to check dependencies for. |
| 1674 | * @sup: Device to check against. |
| 1675 | * |
| 1676 | * Check if @sup depends on @con or any device dependent on it (its child or |
| 1677 | * its consumer etc). When such a cyclic dependency is found, convert all |
| 1678 | * device links created solely by fw_devlink into SYNC_STATE_ONLY device links. |
| 1679 | * This is the equivalent of doing fw_devlink=permissive just between the |
| 1680 | * devices in the cycle. We need to do this because, at this point, fw_devlink |
| 1681 | * can't tell which of these dependencies is not a real dependency. |
| 1682 | * |
| 1683 | * Return 1 if a cycle is found. Otherwise, return 0. |
| 1684 | */ |
kernel test robot | c13b827 | 2020-12-18 14:39:35 +0800 | [diff] [blame] | 1685 | static int fw_devlink_relax_cycle(struct device *con, void *sup) |
Saravana Kannan | b0e2fa4 | 2020-12-17 19:17:02 -0800 | [diff] [blame] | 1686 | { |
| 1687 | struct device_link *link; |
| 1688 | int ret; |
| 1689 | |
| 1690 | if (con == sup) |
| 1691 | return 1; |
| 1692 | |
| 1693 | ret = device_for_each_child(con, sup, fw_devlink_relax_cycle); |
| 1694 | if (ret) |
| 1695 | return ret; |
| 1696 | |
| 1697 | list_for_each_entry(link, &con->links.consumers, s_node) { |
| 1698 | if ((link->flags & ~DL_FLAG_INFERRED) == |
| 1699 | (DL_FLAG_SYNC_STATE_ONLY | DL_FLAG_MANAGED)) |
| 1700 | continue; |
| 1701 | |
| 1702 | if (!fw_devlink_relax_cycle(link->consumer, sup)) |
| 1703 | continue; |
| 1704 | |
| 1705 | ret = 1; |
| 1706 | |
Saravana Kannan | d46f3e3 | 2021-04-01 21:03:41 -0700 | [diff] [blame] | 1707 | fw_devlink_relax_link(link); |
Saravana Kannan | b0e2fa4 | 2020-12-17 19:17:02 -0800 | [diff] [blame] | 1708 | } |
| 1709 | return ret; |
| 1710 | } |
| 1711 | |
| 1712 | /** |
Saravana Kannan | f9aa460 | 2020-11-20 18:02:31 -0800 | [diff] [blame] | 1713 | * fw_devlink_create_devlink - Create a device link from a consumer to fwnode |
Pierre-Louis Bossart | 37c52f7 | 2021-03-31 18:26:08 -0500 | [diff] [blame] | 1714 | * @con: consumer device for the device link |
| 1715 | * @sup_handle: fwnode handle of supplier |
| 1716 | * @flags: devlink flags |
Saravana Kannan | f9aa460 | 2020-11-20 18:02:31 -0800 | [diff] [blame] | 1717 | * |
| 1718 | * This function will try to create a device link between the consumer device |
| 1719 | * @con and the supplier device represented by @sup_handle. |
| 1720 | * |
| 1721 | * The supplier has to be provided as a fwnode because incorrect cycles in |
| 1722 | * fwnode links can sometimes cause the supplier device to never be created. |
| 1723 | * This function detects such cases and returns an error if it cannot create a |
| 1724 | * device link from the consumer to a missing supplier. |
| 1725 | * |
| 1726 | * Returns, |
| 1727 | * 0 on successfully creating a device link |
| 1728 | * -EINVAL if the device link cannot be created as expected |
| 1729 | * -EAGAIN if the device link cannot be created right now, but it may be |
| 1730 | * possible to do that in the future |
| 1731 | */ |
| 1732 | static int fw_devlink_create_devlink(struct device *con, |
| 1733 | struct fwnode_handle *sup_handle, u32 flags) |
| 1734 | { |
| 1735 | struct device *sup_dev; |
| 1736 | int ret = 0; |
| 1737 | |
Saravana Kannan | 5501765 | 2021-09-15 10:09:38 -0700 | [diff] [blame] | 1738 | /* |
| 1739 | * In some cases, a device P might also be a supplier to its child node |
| 1740 | * C. However, this would defer the probe of C until the probe of P |
| 1741 | * completes successfully. This is perfectly fine in the device driver |
| 1742 | * model. device_add() doesn't guarantee probe completion of the device |
| 1743 | * by the time it returns. |
| 1744 | * |
| 1745 | * However, there are a few drivers that assume C will finish probing |
| 1746 | * as soon as it's added and before P finishes probing. So, we provide |
| 1747 | * a flag to let fw_devlink know not to delay the probe of C until the |
| 1748 | * probe of P completes successfully. |
| 1749 | * |
| 1750 | * When such a flag is set, we can't create device links where P is the |
| 1751 | * supplier of C as that would delay the probe of C. |
| 1752 | */ |
| 1753 | if (sup_handle->flags & FWNODE_FLAG_NEEDS_CHILD_BOUND_ON_ADD && |
| 1754 | fwnode_is_ancestor_of(sup_handle, con->fwnode)) |
| 1755 | return -EINVAL; |
| 1756 | |
Saravana Kannan | f9aa460 | 2020-11-20 18:02:31 -0800 | [diff] [blame] | 1757 | sup_dev = get_dev_from_fwnode(sup_handle); |
| 1758 | if (sup_dev) { |
| 1759 | /* |
Saravana Kannan | 74c782c | 2021-02-05 14:26:41 -0800 | [diff] [blame] | 1760 | * If it's one of those drivers that don't actually bind to |
| 1761 | * their device using driver core, then don't wait on this |
| 1762 | * supplier device indefinitely. |
| 1763 | */ |
| 1764 | if (sup_dev->links.status == DL_DEV_NO_DRIVER && |
| 1765 | sup_handle->flags & FWNODE_FLAG_INITIALIZED) { |
| 1766 | ret = -EINVAL; |
| 1767 | goto out; |
| 1768 | } |
| 1769 | |
| 1770 | /* |
Saravana Kannan | f9aa460 | 2020-11-20 18:02:31 -0800 | [diff] [blame] | 1771 | * If this fails, it is due to cycles in device links. Just |
| 1772 | * give up on this link and treat it as invalid. |
| 1773 | */ |
Saravana Kannan | b0e2fa4 | 2020-12-17 19:17:02 -0800 | [diff] [blame] | 1774 | if (!device_link_add(con, sup_dev, flags) && |
| 1775 | !(flags & DL_FLAG_SYNC_STATE_ONLY)) { |
| 1776 | dev_info(con, "Fixing up cyclic dependency with %s\n", |
| 1777 | dev_name(sup_dev)); |
| 1778 | device_links_write_lock(); |
| 1779 | fw_devlink_relax_cycle(con, sup_dev); |
| 1780 | device_links_write_unlock(); |
| 1781 | device_link_add(con, sup_dev, |
| 1782 | FW_DEVLINK_FLAGS_PERMISSIVE); |
Saravana Kannan | f9aa460 | 2020-11-20 18:02:31 -0800 | [diff] [blame] | 1783 | ret = -EINVAL; |
Saravana Kannan | b0e2fa4 | 2020-12-17 19:17:02 -0800 | [diff] [blame] | 1784 | } |
Saravana Kannan | f9aa460 | 2020-11-20 18:02:31 -0800 | [diff] [blame] | 1785 | |
| 1786 | goto out; |
| 1787 | } |
| 1788 | |
Saravana Kannan | 74c782c | 2021-02-05 14:26:41 -0800 | [diff] [blame] | 1789 | /* Supplier that's already initialized without a struct device. */ |
| 1790 | if (sup_handle->flags & FWNODE_FLAG_INITIALIZED) |
| 1791 | return -EINVAL; |
| 1792 | |
Saravana Kannan | f9aa460 | 2020-11-20 18:02:31 -0800 | [diff] [blame] | 1793 | /* |
| 1794 | * DL_FLAG_SYNC_STATE_ONLY doesn't block probing and supports |
| 1795 | * cycles. So cycle detection isn't necessary and shouldn't be |
| 1796 | * done. |
| 1797 | */ |
| 1798 | if (flags & DL_FLAG_SYNC_STATE_ONLY) |
| 1799 | return -EAGAIN; |
| 1800 | |
| 1801 | /* |
| 1802 | * If we can't find the supplier device from its fwnode, it might be |
| 1803 | * due to a cyclic dependency between fwnodes. Some of these cycles can |
| 1804 | * be broken by applying logic. Check for these types of cycles and |
| 1805 | * break them so that devices in the cycle probe properly. |
| 1806 | * |
Saravana Kannan | 2de9d8e | 2021-09-15 10:09:37 -0700 | [diff] [blame] | 1807 | * If the supplier's parent is dependent on the consumer, then the |
| 1808 | * consumer and supplier have a cyclic dependency. Since fw_devlink |
| 1809 | * can't tell which of the inferred dependencies are incorrect, don't |
| 1810 | * enforce probe ordering between any of the devices in this cyclic |
| 1811 | * dependency. Do this by relaxing all the fw_devlink device links in |
| 1812 | * this cycle and by treating the fwnode link between the consumer and |
| 1813 | * the supplier as an invalid dependency. |
Saravana Kannan | f9aa460 | 2020-11-20 18:02:31 -0800 | [diff] [blame] | 1814 | */ |
| 1815 | sup_dev = fwnode_get_next_parent_dev(sup_handle); |
| 1816 | if (sup_dev && device_is_dependent(con, sup_dev)) { |
Saravana Kannan | 2de9d8e | 2021-09-15 10:09:37 -0700 | [diff] [blame] | 1817 | dev_info(con, "Fixing up cyclic dependency with %pfwP (%s)\n", |
| 1818 | sup_handle, dev_name(sup_dev)); |
| 1819 | device_links_write_lock(); |
| 1820 | fw_devlink_relax_cycle(con, sup_dev); |
| 1821 | device_links_write_unlock(); |
Saravana Kannan | f9aa460 | 2020-11-20 18:02:31 -0800 | [diff] [blame] | 1822 | ret = -EINVAL; |
| 1823 | } else { |
| 1824 | /* |
| 1825 | * Can't check for cycles or no cycles. So let's try |
| 1826 | * again later. |
| 1827 | */ |
| 1828 | ret = -EAGAIN; |
| 1829 | } |
| 1830 | |
| 1831 | out: |
| 1832 | put_device(sup_dev); |
| 1833 | return ret; |
| 1834 | } |
| 1835 | |
| 1836 | /** |
| 1837 | * __fw_devlink_link_to_consumers - Create device links to consumers of a device |
Pierre-Louis Bossart | 37c52f7 | 2021-03-31 18:26:08 -0500 | [diff] [blame] | 1838 | * @dev: Device that needs to be linked to its consumers |
Saravana Kannan | f9aa460 | 2020-11-20 18:02:31 -0800 | [diff] [blame] | 1839 | * |
| 1840 | * This function looks at all the consumer fwnodes of @dev and creates device |
| 1841 | * links between the consumer device and @dev (supplier). |
| 1842 | * |
| 1843 | * If the consumer device has not been added yet, then this function creates a |
| 1844 | * SYNC_STATE_ONLY link between @dev (supplier) and the closest ancestor device |
| 1845 | * of the consumer fwnode. This is necessary to make sure @dev doesn't get a |
| 1846 | * sync_state() callback before the real consumer device gets to be added and |
| 1847 | * then probed. |
| 1848 | * |
| 1849 | * Once device links are created from the real consumer to @dev (supplier), the |
| 1850 | * fwnode links are deleted. |
| 1851 | */ |
| 1852 | static void __fw_devlink_link_to_consumers(struct device *dev) |
| 1853 | { |
| 1854 | struct fwnode_handle *fwnode = dev->fwnode; |
| 1855 | struct fwnode_link *link, *tmp; |
| 1856 | |
| 1857 | list_for_each_entry_safe(link, tmp, &fwnode->consumers, s_hook) { |
| 1858 | u32 dl_flags = fw_devlink_get_flags(); |
| 1859 | struct device *con_dev; |
| 1860 | bool own_link = true; |
| 1861 | int ret; |
| 1862 | |
| 1863 | con_dev = get_dev_from_fwnode(link->consumer); |
| 1864 | /* |
| 1865 | * If consumer device is not available yet, make a "proxy" |
| 1866 | * SYNC_STATE_ONLY link from the consumer's parent device to |
| 1867 | * the supplier device. This is necessary to make sure the |
| 1868 | * supplier doesn't get a sync_state() callback before the real |
| 1869 | * consumer can create a device link to the supplier. |
| 1870 | * |
| 1871 | * This proxy link step is needed to handle the case where the |
| 1872 | * consumer's parent device is added before the supplier. |
| 1873 | */ |
| 1874 | if (!con_dev) { |
| 1875 | con_dev = fwnode_get_next_parent_dev(link->consumer); |
| 1876 | /* |
| 1877 | * However, if the consumer's parent device is also the |
| 1878 | * parent of the supplier, don't create a |
| 1879 | * consumer-supplier link from the parent to its child |
| 1880 | * device. Such a dependency is impossible. |
| 1881 | */ |
| 1882 | if (con_dev && |
| 1883 | fwnode_is_ancestor_of(con_dev->fwnode, fwnode)) { |
| 1884 | put_device(con_dev); |
| 1885 | con_dev = NULL; |
| 1886 | } else { |
| 1887 | own_link = false; |
Saravana Kannan | b90fb8f | 2020-12-17 19:17:01 -0800 | [diff] [blame] | 1888 | dl_flags = FW_DEVLINK_FLAGS_PERMISSIVE; |
Saravana Kannan | f9aa460 | 2020-11-20 18:02:31 -0800 | [diff] [blame] | 1889 | } |
| 1890 | } |
| 1891 | |
| 1892 | if (!con_dev) |
| 1893 | continue; |
| 1894 | |
| 1895 | ret = fw_devlink_create_devlink(con_dev, fwnode, dl_flags); |
| 1896 | put_device(con_dev); |
| 1897 | if (!own_link || ret == -EAGAIN) |
| 1898 | continue; |
| 1899 | |
Saravana Kannan | 76f1308 | 2021-09-15 10:28:06 -0700 | [diff] [blame] | 1900 | __fwnode_link_del(link); |
Saravana Kannan | f9aa460 | 2020-11-20 18:02:31 -0800 | [diff] [blame] | 1901 | } |
| 1902 | } |
| 1903 | |
| 1904 | /** |
| 1905 | * __fw_devlink_link_to_suppliers - Create device links to suppliers of a device |
Pierre-Louis Bossart | 37c52f7 | 2021-03-31 18:26:08 -0500 | [diff] [blame] | 1906 | * @dev: The consumer device that needs to be linked to its suppliers |
| 1907 | * @fwnode: Root of the fwnode tree that is used to create device links |
Saravana Kannan | f9aa460 | 2020-11-20 18:02:31 -0800 | [diff] [blame] | 1908 | * |
| 1909 | * This function looks at all the supplier fwnodes of fwnode tree rooted at |
| 1910 | * @fwnode and creates device links between @dev (consumer) and all the |
| 1911 | * supplier devices of the entire fwnode tree at @fwnode. |
| 1912 | * |
| 1913 | * The function creates normal (non-SYNC_STATE_ONLY) device links between @dev |
| 1914 | * and the real suppliers of @dev. Once these device links are created, the |
| 1915 | * fwnode links are deleted. When such device links are successfully created, |
| 1916 | * this function is called recursively on those supplier devices. This is |
| 1917 | * needed to detect and break some invalid cycles in fwnode links. See |
| 1918 | * fw_devlink_create_devlink() for more details. |
| 1919 | * |
| 1920 | * In addition, it also looks at all the suppliers of the entire fwnode tree |
| 1921 | * because some of the child devices of @dev that have not been added yet |
| 1922 | * (because @dev hasn't probed) might already have their suppliers added to |
| 1923 | * driver core. So, this function creates SYNC_STATE_ONLY device links between |
| 1924 | * @dev (consumer) and these suppliers to make sure they don't execute their |
| 1925 | * sync_state() callbacks before these child devices have a chance to create |
| 1926 | * their device links. The fwnode links that correspond to the child devices |
| 1927 | * aren't delete because they are needed later to create the device links |
| 1928 | * between the real consumer and supplier devices. |
| 1929 | */ |
| 1930 | static void __fw_devlink_link_to_suppliers(struct device *dev, |
| 1931 | struct fwnode_handle *fwnode) |
| 1932 | { |
| 1933 | bool own_link = (dev->fwnode == fwnode); |
| 1934 | struct fwnode_link *link, *tmp; |
| 1935 | struct fwnode_handle *child = NULL; |
| 1936 | u32 dl_flags; |
| 1937 | |
| 1938 | if (own_link) |
| 1939 | dl_flags = fw_devlink_get_flags(); |
| 1940 | else |
Saravana Kannan | b90fb8f | 2020-12-17 19:17:01 -0800 | [diff] [blame] | 1941 | dl_flags = FW_DEVLINK_FLAGS_PERMISSIVE; |
Saravana Kannan | f9aa460 | 2020-11-20 18:02:31 -0800 | [diff] [blame] | 1942 | |
| 1943 | list_for_each_entry_safe(link, tmp, &fwnode->suppliers, c_hook) { |
| 1944 | int ret; |
| 1945 | struct device *sup_dev; |
| 1946 | struct fwnode_handle *sup = link->supplier; |
| 1947 | |
| 1948 | ret = fw_devlink_create_devlink(dev, sup, dl_flags); |
| 1949 | if (!own_link || ret == -EAGAIN) |
| 1950 | continue; |
| 1951 | |
Saravana Kannan | 76f1308 | 2021-09-15 10:28:06 -0700 | [diff] [blame] | 1952 | __fwnode_link_del(link); |
Saravana Kannan | f9aa460 | 2020-11-20 18:02:31 -0800 | [diff] [blame] | 1953 | |
| 1954 | /* If no device link was created, nothing more to do. */ |
| 1955 | if (ret) |
| 1956 | continue; |
| 1957 | |
| 1958 | /* |
| 1959 | * If a device link was successfully created to a supplier, we |
| 1960 | * now need to try and link the supplier to all its suppliers. |
| 1961 | * |
| 1962 | * This is needed to detect and delete false dependencies in |
| 1963 | * fwnode links that haven't been converted to a device link |
| 1964 | * yet. See comments in fw_devlink_create_devlink() for more |
| 1965 | * details on the false dependency. |
| 1966 | * |
| 1967 | * Without deleting these false dependencies, some devices will |
| 1968 | * never probe because they'll keep waiting for their false |
| 1969 | * dependency fwnode links to be converted to device links. |
| 1970 | */ |
| 1971 | sup_dev = get_dev_from_fwnode(sup); |
| 1972 | __fw_devlink_link_to_suppliers(sup_dev, sup_dev->fwnode); |
| 1973 | put_device(sup_dev); |
| 1974 | } |
| 1975 | |
| 1976 | /* |
| 1977 | * Make "proxy" SYNC_STATE_ONLY device links to represent the needs of |
| 1978 | * all the descendants. This proxy link step is needed to handle the |
| 1979 | * case where the supplier is added before the consumer's parent device |
| 1980 | * (@dev). |
| 1981 | */ |
| 1982 | while ((child = fwnode_get_next_available_child_node(fwnode, child))) |
| 1983 | __fw_devlink_link_to_suppliers(dev, child); |
| 1984 | } |
| 1985 | |
Saravana Kannan | 5f5377e | 2020-05-14 22:34:58 -0700 | [diff] [blame] | 1986 | static void fw_devlink_link_device(struct device *dev) |
| 1987 | { |
Saravana Kannan | f9aa460 | 2020-11-20 18:02:31 -0800 | [diff] [blame] | 1988 | struct fwnode_handle *fwnode = dev->fwnode; |
Saravana Kannan | 5f5377e | 2020-05-14 22:34:58 -0700 | [diff] [blame] | 1989 | |
Saravana Kannan | f9aa460 | 2020-11-20 18:02:31 -0800 | [diff] [blame] | 1990 | if (!fw_devlink_flags) |
| 1991 | return; |
Saravana Kannan | c84b909 | 2020-11-20 18:02:21 -0800 | [diff] [blame] | 1992 | |
Saravana Kannan | f9aa460 | 2020-11-20 18:02:31 -0800 | [diff] [blame] | 1993 | fw_devlink_parse_fwtree(fwnode); |
| 1994 | |
| 1995 | mutex_lock(&fwnode_link_lock); |
| 1996 | __fw_devlink_link_to_consumers(dev); |
| 1997 | __fw_devlink_link_to_suppliers(dev, fwnode); |
| 1998 | mutex_unlock(&fwnode_link_lock); |
Saravana Kannan | 716a7a2 | 2020-05-14 22:34:59 -0700 | [diff] [blame] | 1999 | } |
Saravana Kannan | c84b909 | 2020-11-20 18:02:21 -0800 | [diff] [blame] | 2000 | |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 2001 | /* Device links support end. */ |
| 2002 | |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 2003 | int (*platform_notify)(struct device *dev) = NULL; |
| 2004 | int (*platform_notify_remove)(struct device *dev) = NULL; |
Dan Williams | e105b8b | 2008-04-21 10:51:07 -0700 | [diff] [blame] | 2005 | static struct kobject *dev_kobj; |
| 2006 | struct kobject *sysfs_dev_char_kobj; |
| 2007 | struct kobject *sysfs_dev_block_kobj; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2008 | |
Rafael J. Wysocki | 5e33bc4 | 2013-08-28 21:41:01 +0200 | [diff] [blame] | 2009 | static DEFINE_MUTEX(device_hotplug_lock); |
| 2010 | |
| 2011 | void lock_device_hotplug(void) |
| 2012 | { |
| 2013 | mutex_lock(&device_hotplug_lock); |
| 2014 | } |
| 2015 | |
| 2016 | void unlock_device_hotplug(void) |
| 2017 | { |
| 2018 | mutex_unlock(&device_hotplug_lock); |
| 2019 | } |
| 2020 | |
| 2021 | int lock_device_hotplug_sysfs(void) |
| 2022 | { |
| 2023 | if (mutex_trylock(&device_hotplug_lock)) |
| 2024 | return 0; |
| 2025 | |
| 2026 | /* Avoid busy looping (5 ms of sleep should do). */ |
| 2027 | msleep(5); |
| 2028 | return restart_syscall(); |
| 2029 | } |
| 2030 | |
Greg Kroah-Hartman | 4e886c2 | 2008-01-27 12:12:43 -0800 | [diff] [blame] | 2031 | #ifdef CONFIG_BLOCK |
| 2032 | static inline int device_is_not_partition(struct device *dev) |
| 2033 | { |
| 2034 | return !(dev->type == &part_type); |
| 2035 | } |
| 2036 | #else |
| 2037 | static inline int device_is_not_partition(struct device *dev) |
| 2038 | { |
| 2039 | return 1; |
| 2040 | } |
| 2041 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2042 | |
Rafael J. Wysocki | b2ebd9d | 2021-07-12 19:28:16 +0200 | [diff] [blame] | 2043 | static void device_platform_notify(struct device *dev) |
Heikki Krogerus | 07de0e8 | 2018-11-09 17:21:34 +0300 | [diff] [blame] | 2044 | { |
Rafael J. Wysocki | b2ebd9d | 2021-07-12 19:28:16 +0200 | [diff] [blame] | 2045 | acpi_device_notify(dev); |
Heikki Krogerus | 7847a14 | 2018-11-09 17:21:35 +0300 | [diff] [blame] | 2046 | |
Rafael J. Wysocki | b2ebd9d | 2021-07-12 19:28:16 +0200 | [diff] [blame] | 2047 | software_node_notify(dev); |
Heikki Krogerus | 7847a14 | 2018-11-09 17:21:35 +0300 | [diff] [blame] | 2048 | |
Rafael J. Wysocki | b2ebd9d | 2021-07-12 19:28:16 +0200 | [diff] [blame] | 2049 | if (platform_notify) |
Heikki Krogerus | 07de0e8 | 2018-11-09 17:21:34 +0300 | [diff] [blame] | 2050 | platform_notify(dev); |
Rafael J. Wysocki | b2ebd9d | 2021-07-12 19:28:16 +0200 | [diff] [blame] | 2051 | } |
| 2052 | |
| 2053 | static void device_platform_notify_remove(struct device *dev) |
| 2054 | { |
| 2055 | acpi_device_notify_remove(dev); |
| 2056 | |
| 2057 | software_node_notify_remove(dev); |
| 2058 | |
| 2059 | if (platform_notify_remove) |
Heikki Krogerus | 07de0e8 | 2018-11-09 17:21:34 +0300 | [diff] [blame] | 2060 | platform_notify_remove(dev); |
Heikki Krogerus | 07de0e8 | 2018-11-09 17:21:34 +0300 | [diff] [blame] | 2061 | } |
| 2062 | |
Alan Stern | 3e95637 | 2006-06-16 17:10:48 -0400 | [diff] [blame] | 2063 | /** |
| 2064 | * dev_driver_string - Return a device's driver name, if at all possible |
| 2065 | * @dev: struct device to get the name of |
| 2066 | * |
| 2067 | * 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] | 2068 | * 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] | 2069 | * it is attached to. If it is not attached to a bus either, an empty |
| 2070 | * string will be returned. |
| 2071 | */ |
Jean Delvare | bf9ca69 | 2008-07-30 12:29:21 -0700 | [diff] [blame] | 2072 | const char *dev_driver_string(const struct device *dev) |
Alan Stern | 3e95637 | 2006-06-16 17:10:48 -0400 | [diff] [blame] | 2073 | { |
Alan Stern | 3589972 | 2009-12-04 11:06:57 -0500 | [diff] [blame] | 2074 | struct device_driver *drv; |
| 2075 | |
| 2076 | /* dev->driver can change to NULL underneath us because of unbinding, |
| 2077 | * so be careful about accessing it. dev->bus and dev->class should |
| 2078 | * never change once they are set, so they don't need special care. |
| 2079 | */ |
Mark Rutland | 6aa7de0 | 2017-10-23 14:07:29 -0700 | [diff] [blame] | 2080 | drv = READ_ONCE(dev->driver); |
Saravana Kannan | e020ff6 | 2021-01-10 09:54:07 -0800 | [diff] [blame] | 2081 | return drv ? drv->name : dev_bus_name(dev); |
Alan Stern | 3e95637 | 2006-06-16 17:10:48 -0400 | [diff] [blame] | 2082 | } |
Matthew Wilcox | 310a922 | 2006-09-23 23:35:04 -0600 | [diff] [blame] | 2083 | EXPORT_SYMBOL(dev_driver_string); |
Alan Stern | 3e95637 | 2006-06-16 17:10:48 -0400 | [diff] [blame] | 2084 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2085 | #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr) |
| 2086 | |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 2087 | static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr, |
| 2088 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2089 | { |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 2090 | struct device_attribute *dev_attr = to_dev_attr(attr); |
Lars-Peter Clausen | b0d1f80 | 2012-07-03 18:49:36 +0200 | [diff] [blame] | 2091 | struct device *dev = kobj_to_dev(kobj); |
Dmitry Torokhov | 4a0c20b | 2005-04-29 01:23:47 -0500 | [diff] [blame] | 2092 | ssize_t ret = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2093 | |
| 2094 | if (dev_attr->show) |
Yani Ioannou | 54b6f35 | 2005-05-17 06:39:34 -0400 | [diff] [blame] | 2095 | ret = dev_attr->show(dev, dev_attr, buf); |
Andrew Morton | 815d2d5 | 2008-03-04 15:09:07 -0800 | [diff] [blame] | 2096 | if (ret >= (ssize_t)PAGE_SIZE) { |
Sergey Senozhatsky | a52668c | 2017-12-11 21:50:21 +0900 | [diff] [blame] | 2097 | printk("dev_attr_show: %pS returned bad count\n", |
| 2098 | dev_attr->show); |
Andrew Morton | 815d2d5 | 2008-03-04 15:09:07 -0800 | [diff] [blame] | 2099 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2100 | return ret; |
| 2101 | } |
| 2102 | |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 2103 | static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 2104 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2105 | { |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 2106 | struct device_attribute *dev_attr = to_dev_attr(attr); |
Lars-Peter Clausen | b0d1f80 | 2012-07-03 18:49:36 +0200 | [diff] [blame] | 2107 | struct device *dev = kobj_to_dev(kobj); |
Dmitry Torokhov | 4a0c20b | 2005-04-29 01:23:47 -0500 | [diff] [blame] | 2108 | ssize_t ret = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2109 | |
| 2110 | if (dev_attr->store) |
Yani Ioannou | 54b6f35 | 2005-05-17 06:39:34 -0400 | [diff] [blame] | 2111 | ret = dev_attr->store(dev, dev_attr, buf, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2112 | return ret; |
| 2113 | } |
| 2114 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 2115 | static const struct sysfs_ops dev_sysfs_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2116 | .show = dev_attr_show, |
| 2117 | .store = dev_attr_store, |
| 2118 | }; |
| 2119 | |
Kay Sievers | ca22e56 | 2011-12-14 14:29:38 -0800 | [diff] [blame] | 2120 | #define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr) |
| 2121 | |
| 2122 | ssize_t device_store_ulong(struct device *dev, |
| 2123 | struct device_attribute *attr, |
| 2124 | const char *buf, size_t size) |
| 2125 | { |
| 2126 | struct dev_ext_attribute *ea = to_ext_attr(attr); |
Kaitao cheng | f88184b | 2018-11-06 08:34:54 -0800 | [diff] [blame] | 2127 | int ret; |
| 2128 | unsigned long new; |
| 2129 | |
| 2130 | ret = kstrtoul(buf, 0, &new); |
| 2131 | if (ret) |
| 2132 | return ret; |
Kay Sievers | ca22e56 | 2011-12-14 14:29:38 -0800 | [diff] [blame] | 2133 | *(unsigned long *)(ea->var) = new; |
| 2134 | /* Always return full write size even if we didn't consume all */ |
| 2135 | return size; |
| 2136 | } |
| 2137 | EXPORT_SYMBOL_GPL(device_store_ulong); |
| 2138 | |
| 2139 | ssize_t device_show_ulong(struct device *dev, |
| 2140 | struct device_attribute *attr, |
| 2141 | char *buf) |
| 2142 | { |
| 2143 | struct dev_ext_attribute *ea = to_ext_attr(attr); |
Joe Perches | aa83889 | 2020-09-16 13:40:39 -0700 | [diff] [blame] | 2144 | return sysfs_emit(buf, "%lx\n", *(unsigned long *)(ea->var)); |
Kay Sievers | ca22e56 | 2011-12-14 14:29:38 -0800 | [diff] [blame] | 2145 | } |
| 2146 | EXPORT_SYMBOL_GPL(device_show_ulong); |
| 2147 | |
| 2148 | ssize_t device_store_int(struct device *dev, |
| 2149 | struct device_attribute *attr, |
| 2150 | const char *buf, size_t size) |
| 2151 | { |
| 2152 | struct dev_ext_attribute *ea = to_ext_attr(attr); |
Kaitao cheng | f88184b | 2018-11-06 08:34:54 -0800 | [diff] [blame] | 2153 | int ret; |
| 2154 | long new; |
| 2155 | |
| 2156 | ret = kstrtol(buf, 0, &new); |
| 2157 | if (ret) |
| 2158 | return ret; |
| 2159 | |
| 2160 | if (new > INT_MAX || new < INT_MIN) |
Kay Sievers | ca22e56 | 2011-12-14 14:29:38 -0800 | [diff] [blame] | 2161 | return -EINVAL; |
| 2162 | *(int *)(ea->var) = new; |
| 2163 | /* Always return full write size even if we didn't consume all */ |
| 2164 | return size; |
| 2165 | } |
| 2166 | EXPORT_SYMBOL_GPL(device_store_int); |
| 2167 | |
| 2168 | ssize_t device_show_int(struct device *dev, |
| 2169 | struct device_attribute *attr, |
| 2170 | char *buf) |
| 2171 | { |
| 2172 | struct dev_ext_attribute *ea = to_ext_attr(attr); |
| 2173 | |
Joe Perches | aa83889 | 2020-09-16 13:40:39 -0700 | [diff] [blame] | 2174 | return sysfs_emit(buf, "%d\n", *(int *)(ea->var)); |
Kay Sievers | ca22e56 | 2011-12-14 14:29:38 -0800 | [diff] [blame] | 2175 | } |
| 2176 | EXPORT_SYMBOL_GPL(device_show_int); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2177 | |
Borislav Petkov | 9187239 | 2012-10-09 19:52:05 +0200 | [diff] [blame] | 2178 | ssize_t device_store_bool(struct device *dev, struct device_attribute *attr, |
| 2179 | const char *buf, size_t size) |
| 2180 | { |
| 2181 | struct dev_ext_attribute *ea = to_ext_attr(attr); |
| 2182 | |
| 2183 | if (strtobool(buf, ea->var) < 0) |
| 2184 | return -EINVAL; |
| 2185 | |
| 2186 | return size; |
| 2187 | } |
| 2188 | EXPORT_SYMBOL_GPL(device_store_bool); |
| 2189 | |
| 2190 | ssize_t device_show_bool(struct device *dev, struct device_attribute *attr, |
| 2191 | char *buf) |
| 2192 | { |
| 2193 | struct dev_ext_attribute *ea = to_ext_attr(attr); |
| 2194 | |
Joe Perches | aa83889 | 2020-09-16 13:40:39 -0700 | [diff] [blame] | 2195 | return sysfs_emit(buf, "%d\n", *(bool *)(ea->var)); |
Borislav Petkov | 9187239 | 2012-10-09 19:52:05 +0200 | [diff] [blame] | 2196 | } |
| 2197 | EXPORT_SYMBOL_GPL(device_show_bool); |
| 2198 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2199 | /** |
Robert P. J. Day | f8878dc | 2013-06-01 20:17:34 -0400 | [diff] [blame] | 2200 | * device_release - free device structure. |
| 2201 | * @kobj: device's kobject. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2202 | * |
Robert P. J. Day | f8878dc | 2013-06-01 20:17:34 -0400 | [diff] [blame] | 2203 | * This is called once the reference count for the object |
| 2204 | * reaches 0. We forward the call to the device's release |
| 2205 | * method, which should handle actually freeing the structure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2206 | */ |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 2207 | static void device_release(struct kobject *kobj) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2208 | { |
Lars-Peter Clausen | b0d1f80 | 2012-07-03 18:49:36 +0200 | [diff] [blame] | 2209 | struct device *dev = kobj_to_dev(kobj); |
Greg Kroah-Hartman | fb069a5 | 2008-12-16 12:23:36 -0800 | [diff] [blame] | 2210 | struct device_private *p = dev->p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2211 | |
Ming Lei | a525a3d | 2012-07-25 01:42:29 +0800 | [diff] [blame] | 2212 | /* |
| 2213 | * Some platform devices are driven without driver attached |
| 2214 | * and managed resources may have been acquired. Make sure |
| 2215 | * all resources are released. |
| 2216 | * |
| 2217 | * Drivers still can add resources into device after device |
| 2218 | * is deleted but alive, so release devres here to avoid |
| 2219 | * possible memory leak. |
| 2220 | */ |
| 2221 | devres_release_all(dev); |
| 2222 | |
Jim Quinlan | e0d0727 | 2020-09-17 18:43:40 +0200 | [diff] [blame] | 2223 | kfree(dev->dma_range_map); |
| 2224 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2225 | if (dev->release) |
| 2226 | dev->release(dev); |
Kay Sievers | f9f852d | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 2227 | else if (dev->type && dev->type->release) |
| 2228 | dev->type->release(dev); |
Greg Kroah-Hartman | 2620efe | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 2229 | else if (dev->class && dev->class->dev_release) |
| 2230 | dev->class->dev_release(dev); |
Arjan van de Ven | f810a5c | 2008-07-25 19:45:39 -0700 | [diff] [blame] | 2231 | else |
Mauro Carvalho Chehab | 0c1bc6b | 2020-04-14 18:48:37 +0200 | [diff] [blame] | 2232 | WARN(1, KERN_ERR "Device '%s' does not have a release() function, it is broken and must be fixed. See Documentation/core-api/kobject.rst.\n", |
Kay Sievers | 1e0b2cf | 2008-10-30 01:36:48 +0100 | [diff] [blame] | 2233 | dev_name(dev)); |
Greg Kroah-Hartman | fb069a5 | 2008-12-16 12:23:36 -0800 | [diff] [blame] | 2234 | kfree(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2235 | } |
| 2236 | |
Eric W. Biederman | bc451f2 | 2010-03-30 11:31:25 -0700 | [diff] [blame] | 2237 | static const void *device_namespace(struct kobject *kobj) |
| 2238 | { |
Lars-Peter Clausen | b0d1f80 | 2012-07-03 18:49:36 +0200 | [diff] [blame] | 2239 | struct device *dev = kobj_to_dev(kobj); |
Eric W. Biederman | bc451f2 | 2010-03-30 11:31:25 -0700 | [diff] [blame] | 2240 | const void *ns = NULL; |
| 2241 | |
| 2242 | if (dev->class && dev->class->ns_type) |
| 2243 | ns = dev->class->namespace(dev); |
| 2244 | |
| 2245 | return ns; |
| 2246 | } |
| 2247 | |
Dmitry Torokhov | 9944e89 | 2018-07-20 21:56:50 +0000 | [diff] [blame] | 2248 | static void device_get_ownership(struct kobject *kobj, kuid_t *uid, kgid_t *gid) |
| 2249 | { |
| 2250 | struct device *dev = kobj_to_dev(kobj); |
| 2251 | |
| 2252 | if (dev->class && dev->class->get_ownership) |
| 2253 | dev->class->get_ownership(dev, uid, gid); |
| 2254 | } |
| 2255 | |
Greg Kroah-Hartman | 8f4afc4 | 2007-10-11 10:47:49 -0600 | [diff] [blame] | 2256 | static struct kobj_type device_ktype = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2257 | .release = device_release, |
| 2258 | .sysfs_ops = &dev_sysfs_ops, |
Eric W. Biederman | bc451f2 | 2010-03-30 11:31:25 -0700 | [diff] [blame] | 2259 | .namespace = device_namespace, |
Dmitry Torokhov | 9944e89 | 2018-07-20 21:56:50 +0000 | [diff] [blame] | 2260 | .get_ownership = device_get_ownership, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2261 | }; |
| 2262 | |
| 2263 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 2264 | static int dev_uevent_filter(struct kset *kset, struct kobject *kobj) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2265 | { |
| 2266 | struct kobj_type *ktype = get_ktype(kobj); |
| 2267 | |
Greg Kroah-Hartman | 8f4afc4 | 2007-10-11 10:47:49 -0600 | [diff] [blame] | 2268 | if (ktype == &device_ktype) { |
Lars-Peter Clausen | b0d1f80 | 2012-07-03 18:49:36 +0200 | [diff] [blame] | 2269 | struct device *dev = kobj_to_dev(kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2270 | if (dev->bus) |
| 2271 | return 1; |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 2272 | if (dev->class) |
| 2273 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2274 | } |
| 2275 | return 0; |
| 2276 | } |
| 2277 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 2278 | static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2279 | { |
Lars-Peter Clausen | b0d1f80 | 2012-07-03 18:49:36 +0200 | [diff] [blame] | 2280 | struct device *dev = kobj_to_dev(kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2281 | |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 2282 | if (dev->bus) |
| 2283 | return dev->bus->name; |
| 2284 | if (dev->class) |
| 2285 | return dev->class->name; |
| 2286 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2287 | } |
| 2288 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 2289 | static int dev_uevent(struct kset *kset, struct kobject *kobj, |
| 2290 | struct kobj_uevent_env *env) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2291 | { |
Lars-Peter Clausen | b0d1f80 | 2012-07-03 18:49:36 +0200 | [diff] [blame] | 2292 | struct device *dev = kobj_to_dev(kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2293 | int retval = 0; |
| 2294 | |
Kay Sievers | 6fcf53a | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 2295 | /* add device node properties if present */ |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 2296 | if (MAJOR(dev->devt)) { |
Kay Sievers | 6fcf53a | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 2297 | const char *tmp; |
| 2298 | const char *name; |
Al Viro | 2c9ede5 | 2011-07-23 20:24:48 -0400 | [diff] [blame] | 2299 | umode_t mode = 0; |
Greg Kroah-Hartman | 4e4098a | 2013-04-11 11:43:29 -0700 | [diff] [blame] | 2300 | kuid_t uid = GLOBAL_ROOT_UID; |
| 2301 | kgid_t gid = GLOBAL_ROOT_GID; |
Kay Sievers | 6fcf53a | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 2302 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 2303 | add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt)); |
| 2304 | add_uevent_var(env, "MINOR=%u", MINOR(dev->devt)); |
Kay Sievers | 3c2670e | 2013-04-06 09:56:00 -0700 | [diff] [blame] | 2305 | name = device_get_devnode(dev, &mode, &uid, &gid, &tmp); |
Kay Sievers | 6fcf53a | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 2306 | if (name) { |
| 2307 | add_uevent_var(env, "DEVNAME=%s", name); |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 2308 | if (mode) |
| 2309 | add_uevent_var(env, "DEVMODE=%#o", mode & 0777); |
Greg Kroah-Hartman | 4e4098a | 2013-04-11 11:43:29 -0700 | [diff] [blame] | 2310 | if (!uid_eq(uid, GLOBAL_ROOT_UID)) |
| 2311 | add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid)); |
| 2312 | if (!gid_eq(gid, GLOBAL_ROOT_GID)) |
| 2313 | add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid)); |
Kay Sievers | 3c2670e | 2013-04-06 09:56:00 -0700 | [diff] [blame] | 2314 | kfree(tmp); |
Kay Sievers | 6fcf53a | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 2315 | } |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 2316 | } |
| 2317 | |
Kay Sievers | 414264f | 2007-03-12 21:08:57 +0100 | [diff] [blame] | 2318 | if (dev->type && dev->type->name) |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 2319 | add_uevent_var(env, "DEVTYPE=%s", dev->type->name); |
Kay Sievers | 414264f | 2007-03-12 21:08:57 +0100 | [diff] [blame] | 2320 | |
Kay Sievers | 239378f | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 2321 | if (dev->driver) |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 2322 | add_uevent_var(env, "DRIVER=%s", dev->driver->name); |
Kay Sievers | 239378f | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 2323 | |
Grant Likely | 07d57a3 | 2012-02-01 11:22:22 -0700 | [diff] [blame] | 2324 | /* Add common DT information about the device */ |
| 2325 | of_device_uevent(dev, env); |
| 2326 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 2327 | /* have the bus specific function add its stuff */ |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 2328 | if (dev->bus && dev->bus->uevent) { |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 2329 | retval = dev->bus->uevent(dev, env); |
Kay Sievers | f9f852d | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 2330 | if (retval) |
Greg Kroah-Hartman | 7dc72b2 | 2007-11-28 23:49:41 -0800 | [diff] [blame] | 2331 | pr_debug("device: '%s': %s: bus uevent() returned %d\n", |
Kay Sievers | 1e0b2cf | 2008-10-30 01:36:48 +0100 | [diff] [blame] | 2332 | dev_name(dev), __func__, retval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2333 | } |
| 2334 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 2335 | /* have the class specific function add its stuff */ |
Greg Kroah-Hartman | 2620efe | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 2336 | if (dev->class && dev->class->dev_uevent) { |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 2337 | retval = dev->class->dev_uevent(dev, env); |
Kay Sievers | f9f852d | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 2338 | if (retval) |
Greg Kroah-Hartman | 7dc72b2 | 2007-11-28 23:49:41 -0800 | [diff] [blame] | 2339 | pr_debug("device: '%s': %s: class uevent() " |
Kay Sievers | 1e0b2cf | 2008-10-30 01:36:48 +0100 | [diff] [blame] | 2340 | "returned %d\n", dev_name(dev), |
Harvey Harrison | 2b3a302 | 2008-03-04 16:41:05 -0800 | [diff] [blame] | 2341 | __func__, retval); |
Kay Sievers | f9f852d | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 2342 | } |
| 2343 | |
Stefan Weil | eef35c2 | 2010-08-06 21:11:15 +0200 | [diff] [blame] | 2344 | /* have the device type specific function add its stuff */ |
Kay Sievers | f9f852d | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 2345 | if (dev->type && dev->type->uevent) { |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 2346 | retval = dev->type->uevent(dev, env); |
Kay Sievers | f9f852d | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 2347 | if (retval) |
Greg Kroah-Hartman | 7dc72b2 | 2007-11-28 23:49:41 -0800 | [diff] [blame] | 2348 | pr_debug("device: '%s': %s: dev_type uevent() " |
Kay Sievers | 1e0b2cf | 2008-10-30 01:36:48 +0100 | [diff] [blame] | 2349 | "returned %d\n", dev_name(dev), |
Harvey Harrison | 2b3a302 | 2008-03-04 16:41:05 -0800 | [diff] [blame] | 2350 | __func__, retval); |
Greg Kroah-Hartman | 2620efe | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 2351 | } |
| 2352 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2353 | return retval; |
| 2354 | } |
| 2355 | |
Emese Revfy | 9cd4361 | 2009-12-31 14:52:51 +0100 | [diff] [blame] | 2356 | static const struct kset_uevent_ops device_uevent_ops = { |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 2357 | .filter = dev_uevent_filter, |
| 2358 | .name = dev_uevent_name, |
| 2359 | .uevent = dev_uevent, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2360 | }; |
| 2361 | |
Greg Kroah-Hartman | c5e064a | 2013-08-23 17:07:26 -0700 | [diff] [blame] | 2362 | static ssize_t uevent_show(struct device *dev, struct device_attribute *attr, |
Kay Sievers | 16574dc | 2007-04-06 01:40:38 +0200 | [diff] [blame] | 2363 | char *buf) |
| 2364 | { |
| 2365 | struct kobject *top_kobj; |
| 2366 | struct kset *kset; |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 2367 | struct kobj_uevent_env *env = NULL; |
Kay Sievers | 16574dc | 2007-04-06 01:40:38 +0200 | [diff] [blame] | 2368 | int i; |
Joe Perches | 948b3ed | 2020-09-16 13:40:42 -0700 | [diff] [blame] | 2369 | int len = 0; |
Kay Sievers | 16574dc | 2007-04-06 01:40:38 +0200 | [diff] [blame] | 2370 | int retval; |
| 2371 | |
| 2372 | /* search the kset, the device belongs to */ |
| 2373 | top_kobj = &dev->kobj; |
Kay Sievers | 5c5daf6 | 2007-08-12 20:43:55 +0200 | [diff] [blame] | 2374 | while (!top_kobj->kset && top_kobj->parent) |
| 2375 | top_kobj = top_kobj->parent; |
Kay Sievers | 16574dc | 2007-04-06 01:40:38 +0200 | [diff] [blame] | 2376 | if (!top_kobj->kset) |
| 2377 | goto out; |
Kay Sievers | 5c5daf6 | 2007-08-12 20:43:55 +0200 | [diff] [blame] | 2378 | |
Kay Sievers | 16574dc | 2007-04-06 01:40:38 +0200 | [diff] [blame] | 2379 | kset = top_kobj->kset; |
| 2380 | if (!kset->uevent_ops || !kset->uevent_ops->uevent) |
| 2381 | goto out; |
| 2382 | |
| 2383 | /* respect filter */ |
| 2384 | if (kset->uevent_ops && kset->uevent_ops->filter) |
| 2385 | if (!kset->uevent_ops->filter(kset, &dev->kobj)) |
| 2386 | goto out; |
| 2387 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 2388 | env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL); |
| 2389 | if (!env) |
Greg Kroah-Hartman | c7308c8 | 2007-05-02 14:14:11 +0200 | [diff] [blame] | 2390 | return -ENOMEM; |
| 2391 | |
Kay Sievers | 16574dc | 2007-04-06 01:40:38 +0200 | [diff] [blame] | 2392 | /* let the kset specific function add its keys */ |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 2393 | retval = kset->uevent_ops->uevent(kset, &dev->kobj, env); |
Kay Sievers | 16574dc | 2007-04-06 01:40:38 +0200 | [diff] [blame] | 2394 | if (retval) |
| 2395 | goto out; |
| 2396 | |
| 2397 | /* copy keys to file */ |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 2398 | for (i = 0; i < env->envp_idx; i++) |
Joe Perches | 948b3ed | 2020-09-16 13:40:42 -0700 | [diff] [blame] | 2399 | len += sysfs_emit_at(buf, len, "%s\n", env->envp[i]); |
Kay Sievers | 16574dc | 2007-04-06 01:40:38 +0200 | [diff] [blame] | 2400 | out: |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 2401 | kfree(env); |
Joe Perches | 948b3ed | 2020-09-16 13:40:42 -0700 | [diff] [blame] | 2402 | return len; |
Kay Sievers | 16574dc | 2007-04-06 01:40:38 +0200 | [diff] [blame] | 2403 | } |
| 2404 | |
Greg Kroah-Hartman | c5e064a | 2013-08-23 17:07:26 -0700 | [diff] [blame] | 2405 | static ssize_t uevent_store(struct device *dev, struct device_attribute *attr, |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 2406 | const char *buf, size_t count) |
| 2407 | { |
Peter Rajnoha | df44b47 | 2018-12-05 12:27:44 +0100 | [diff] [blame] | 2408 | int rc; |
| 2409 | |
| 2410 | rc = kobject_synth_uevent(&dev->kobj, buf, count); |
| 2411 | |
| 2412 | if (rc) { |
Peter Rajnoha | f36776f | 2017-05-09 15:22:30 +0200 | [diff] [blame] | 2413 | dev_err(dev, "uevent: failed to send synthetic uevent\n"); |
Peter Rajnoha | df44b47 | 2018-12-05 12:27:44 +0100 | [diff] [blame] | 2414 | return rc; |
| 2415 | } |
Kay Sievers | 60a96a5 | 2007-07-08 22:29:26 +0200 | [diff] [blame] | 2416 | |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 2417 | return count; |
| 2418 | } |
Greg Kroah-Hartman | c5e064a | 2013-08-23 17:07:26 -0700 | [diff] [blame] | 2419 | static DEVICE_ATTR_RW(uevent); |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 2420 | |
Greg Kroah-Hartman | c5e064a | 2013-08-23 17:07:26 -0700 | [diff] [blame] | 2421 | 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] | 2422 | char *buf) |
| 2423 | { |
| 2424 | bool val; |
| 2425 | |
Rafael J. Wysocki | 5e33bc4 | 2013-08-28 21:41:01 +0200 | [diff] [blame] | 2426 | device_lock(dev); |
Rafael J. Wysocki | 4f3549d | 2013-05-02 22:15:29 +0200 | [diff] [blame] | 2427 | val = !dev->offline; |
Rafael J. Wysocki | 5e33bc4 | 2013-08-28 21:41:01 +0200 | [diff] [blame] | 2428 | device_unlock(dev); |
Joe Perches | aa83889 | 2020-09-16 13:40:39 -0700 | [diff] [blame] | 2429 | return sysfs_emit(buf, "%u\n", val); |
Rafael J. Wysocki | 4f3549d | 2013-05-02 22:15:29 +0200 | [diff] [blame] | 2430 | } |
| 2431 | |
Greg Kroah-Hartman | c5e064a | 2013-08-23 17:07:26 -0700 | [diff] [blame] | 2432 | 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] | 2433 | const char *buf, size_t count) |
| 2434 | { |
| 2435 | bool val; |
| 2436 | int ret; |
| 2437 | |
| 2438 | ret = strtobool(buf, &val); |
| 2439 | if (ret < 0) |
| 2440 | return ret; |
| 2441 | |
Rafael J. Wysocki | 5e33bc4 | 2013-08-28 21:41:01 +0200 | [diff] [blame] | 2442 | ret = lock_device_hotplug_sysfs(); |
| 2443 | if (ret) |
| 2444 | return ret; |
| 2445 | |
Rafael J. Wysocki | 4f3549d | 2013-05-02 22:15:29 +0200 | [diff] [blame] | 2446 | ret = val ? device_online(dev) : device_offline(dev); |
| 2447 | unlock_device_hotplug(); |
| 2448 | return ret < 0 ? ret : count; |
| 2449 | } |
Greg Kroah-Hartman | c5e064a | 2013-08-23 17:07:26 -0700 | [diff] [blame] | 2450 | static DEVICE_ATTR_RW(online); |
Rafael J. Wysocki | 4f3549d | 2013-05-02 22:15:29 +0200 | [diff] [blame] | 2451 | |
Rajat Jain | 70f400d | 2021-05-24 10:18:11 -0700 | [diff] [blame] | 2452 | static ssize_t removable_show(struct device *dev, struct device_attribute *attr, |
| 2453 | char *buf) |
| 2454 | { |
| 2455 | const char *loc; |
| 2456 | |
| 2457 | switch (dev->removable) { |
| 2458 | case DEVICE_REMOVABLE: |
| 2459 | loc = "removable"; |
| 2460 | break; |
| 2461 | case DEVICE_FIXED: |
| 2462 | loc = "fixed"; |
| 2463 | break; |
| 2464 | default: |
| 2465 | loc = "unknown"; |
| 2466 | } |
| 2467 | return sysfs_emit(buf, "%s\n", loc); |
| 2468 | } |
| 2469 | static DEVICE_ATTR_RO(removable); |
| 2470 | |
Greg Kroah-Hartman | fa6fdb3 | 2013-08-08 15:22:55 -0700 | [diff] [blame] | 2471 | int device_add_groups(struct device *dev, const struct attribute_group **groups) |
Dmitry Torokhov | 621a167 | 2007-03-10 01:37:34 -0500 | [diff] [blame] | 2472 | { |
Greg Kroah-Hartman | 3e9b2ba | 2013-08-21 13:47:50 -0700 | [diff] [blame] | 2473 | return sysfs_create_groups(&dev->kobj, groups); |
Dmitry Torokhov | 621a167 | 2007-03-10 01:37:34 -0500 | [diff] [blame] | 2474 | } |
Dmitry Torokhov | a7670d4 | 2017-07-19 17:24:31 -0700 | [diff] [blame] | 2475 | EXPORT_SYMBOL_GPL(device_add_groups); |
Dmitry Torokhov | 621a167 | 2007-03-10 01:37:34 -0500 | [diff] [blame] | 2476 | |
Greg Kroah-Hartman | fa6fdb3 | 2013-08-08 15:22:55 -0700 | [diff] [blame] | 2477 | void device_remove_groups(struct device *dev, |
| 2478 | const struct attribute_group **groups) |
Dmitry Torokhov | 621a167 | 2007-03-10 01:37:34 -0500 | [diff] [blame] | 2479 | { |
Greg Kroah-Hartman | 3e9b2ba | 2013-08-21 13:47:50 -0700 | [diff] [blame] | 2480 | sysfs_remove_groups(&dev->kobj, groups); |
Greg Kroah-Hartman | de0ff00 | 2006-06-27 00:06:09 -0700 | [diff] [blame] | 2481 | } |
Dmitry Torokhov | a7670d4 | 2017-07-19 17:24:31 -0700 | [diff] [blame] | 2482 | EXPORT_SYMBOL_GPL(device_remove_groups); |
Greg Kroah-Hartman | de0ff00 | 2006-06-27 00:06:09 -0700 | [diff] [blame] | 2483 | |
Dmitry Torokhov | 57b8ff0 | 2017-07-19 17:24:33 -0700 | [diff] [blame] | 2484 | union device_attr_group_devres { |
| 2485 | const struct attribute_group *group; |
| 2486 | const struct attribute_group **groups; |
| 2487 | }; |
| 2488 | |
| 2489 | static int devm_attr_group_match(struct device *dev, void *res, void *data) |
| 2490 | { |
| 2491 | return ((union device_attr_group_devres *)res)->group == data; |
| 2492 | } |
| 2493 | |
| 2494 | static void devm_attr_group_remove(struct device *dev, void *res) |
| 2495 | { |
| 2496 | union device_attr_group_devres *devres = res; |
| 2497 | const struct attribute_group *group = devres->group; |
| 2498 | |
| 2499 | dev_dbg(dev, "%s: removing group %p\n", __func__, group); |
| 2500 | sysfs_remove_group(&dev->kobj, group); |
| 2501 | } |
| 2502 | |
| 2503 | static void devm_attr_groups_remove(struct device *dev, void *res) |
| 2504 | { |
| 2505 | union device_attr_group_devres *devres = res; |
| 2506 | const struct attribute_group **groups = devres->groups; |
| 2507 | |
| 2508 | dev_dbg(dev, "%s: removing groups %p\n", __func__, groups); |
| 2509 | sysfs_remove_groups(&dev->kobj, groups); |
| 2510 | } |
| 2511 | |
| 2512 | /** |
| 2513 | * devm_device_add_group - given a device, create a managed attribute group |
| 2514 | * @dev: The device to create the group for |
| 2515 | * @grp: The attribute group to create |
| 2516 | * |
| 2517 | * This function creates a group for the first time. It will explicitly |
| 2518 | * warn and error if any of the attribute files being created already exist. |
| 2519 | * |
| 2520 | * Returns 0 on success or error code on failure. |
| 2521 | */ |
| 2522 | int devm_device_add_group(struct device *dev, const struct attribute_group *grp) |
| 2523 | { |
| 2524 | union device_attr_group_devres *devres; |
| 2525 | int error; |
| 2526 | |
| 2527 | devres = devres_alloc(devm_attr_group_remove, |
| 2528 | sizeof(*devres), GFP_KERNEL); |
| 2529 | if (!devres) |
| 2530 | return -ENOMEM; |
| 2531 | |
| 2532 | error = sysfs_create_group(&dev->kobj, grp); |
| 2533 | if (error) { |
| 2534 | devres_free(devres); |
| 2535 | return error; |
| 2536 | } |
| 2537 | |
| 2538 | devres->group = grp; |
| 2539 | devres_add(dev, devres); |
| 2540 | return 0; |
| 2541 | } |
| 2542 | EXPORT_SYMBOL_GPL(devm_device_add_group); |
| 2543 | |
| 2544 | /** |
| 2545 | * devm_device_remove_group: remove a managed group from a device |
| 2546 | * @dev: device to remove the group from |
| 2547 | * @grp: group to remove |
| 2548 | * |
| 2549 | * This function removes a group of attributes from a device. The attributes |
| 2550 | * previously have to have been created for this group, otherwise it will fail. |
| 2551 | */ |
| 2552 | void devm_device_remove_group(struct device *dev, |
| 2553 | const struct attribute_group *grp) |
| 2554 | { |
| 2555 | WARN_ON(devres_release(dev, devm_attr_group_remove, |
| 2556 | devm_attr_group_match, |
| 2557 | /* cast away const */ (void *)grp)); |
| 2558 | } |
| 2559 | EXPORT_SYMBOL_GPL(devm_device_remove_group); |
| 2560 | |
| 2561 | /** |
| 2562 | * devm_device_add_groups - create a bunch of managed attribute groups |
| 2563 | * @dev: The device to create the group for |
| 2564 | * @groups: The attribute groups to create, NULL terminated |
| 2565 | * |
| 2566 | * This function creates a bunch of managed attribute groups. If an error |
| 2567 | * occurs when creating a group, all previously created groups will be |
| 2568 | * removed, unwinding everything back to the original state when this |
| 2569 | * function was called. It will explicitly warn and error if any of the |
| 2570 | * attribute files being created already exist. |
| 2571 | * |
| 2572 | * Returns 0 on success or error code from sysfs_create_group on failure. |
| 2573 | */ |
| 2574 | int devm_device_add_groups(struct device *dev, |
| 2575 | const struct attribute_group **groups) |
| 2576 | { |
| 2577 | union device_attr_group_devres *devres; |
| 2578 | int error; |
| 2579 | |
| 2580 | devres = devres_alloc(devm_attr_groups_remove, |
| 2581 | sizeof(*devres), GFP_KERNEL); |
| 2582 | if (!devres) |
| 2583 | return -ENOMEM; |
| 2584 | |
| 2585 | error = sysfs_create_groups(&dev->kobj, groups); |
| 2586 | if (error) { |
| 2587 | devres_free(devres); |
| 2588 | return error; |
| 2589 | } |
| 2590 | |
| 2591 | devres->groups = groups; |
| 2592 | devres_add(dev, devres); |
| 2593 | return 0; |
| 2594 | } |
| 2595 | EXPORT_SYMBOL_GPL(devm_device_add_groups); |
| 2596 | |
| 2597 | /** |
| 2598 | * devm_device_remove_groups - remove a list of managed groups |
| 2599 | * |
| 2600 | * @dev: The device for the groups to be removed from |
| 2601 | * @groups: NULL terminated list of groups to be removed |
| 2602 | * |
| 2603 | * If groups is not NULL, remove the specified groups from the device. |
| 2604 | */ |
| 2605 | void devm_device_remove_groups(struct device *dev, |
| 2606 | const struct attribute_group **groups) |
| 2607 | { |
| 2608 | WARN_ON(devres_release(dev, devm_attr_groups_remove, |
| 2609 | devm_attr_group_match, |
| 2610 | /* cast away const */ (void *)groups)); |
| 2611 | } |
| 2612 | EXPORT_SYMBOL_GPL(devm_device_remove_groups); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2613 | |
Greg Kroah-Hartman | 2620efe | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 2614 | static int device_add_attrs(struct device *dev) |
| 2615 | { |
| 2616 | struct class *class = dev->class; |
Stephen Hemminger | aed65af | 2011-03-28 09:12:52 -0700 | [diff] [blame] | 2617 | const struct device_type *type = dev->type; |
Dmitry Torokhov | 621a167 | 2007-03-10 01:37:34 -0500 | [diff] [blame] | 2618 | int error; |
Greg Kroah-Hartman | 2620efe | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 2619 | |
Dmitry Torokhov | 621a167 | 2007-03-10 01:37:34 -0500 | [diff] [blame] | 2620 | if (class) { |
Greg Kroah-Hartman | d05a6f9 | 2013-07-14 16:05:58 -0700 | [diff] [blame] | 2621 | error = device_add_groups(dev, class->dev_groups); |
Kay Sievers | f9f852d | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 2622 | if (error) |
Dmitry Torokhov | 621a167 | 2007-03-10 01:37:34 -0500 | [diff] [blame] | 2623 | return error; |
Greg Kroah-Hartman | 2620efe | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 2624 | } |
Kay Sievers | f9f852d | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 2625 | |
Dmitry Torokhov | 621a167 | 2007-03-10 01:37:34 -0500 | [diff] [blame] | 2626 | if (type) { |
| 2627 | error = device_add_groups(dev, type->groups); |
Kay Sievers | f9f852d | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 2628 | if (error) |
Greg Kroah-Hartman | a6b01de | 2013-10-05 18:19:30 -0700 | [diff] [blame] | 2629 | goto err_remove_class_groups; |
Kay Sievers | f9f852d | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 2630 | } |
| 2631 | |
Dmitry Torokhov | 621a167 | 2007-03-10 01:37:34 -0500 | [diff] [blame] | 2632 | error = device_add_groups(dev, dev->groups); |
| 2633 | if (error) |
| 2634 | goto err_remove_type_groups; |
| 2635 | |
Rafael J. Wysocki | 4f3549d | 2013-05-02 22:15:29 +0200 | [diff] [blame] | 2636 | if (device_supports_offline(dev) && !dev->offline_disabled) { |
Greg Kroah-Hartman | c5e064a | 2013-08-23 17:07:26 -0700 | [diff] [blame] | 2637 | error = device_create_file(dev, &dev_attr_online); |
Rafael J. Wysocki | 4f3549d | 2013-05-02 22:15:29 +0200 | [diff] [blame] | 2638 | if (error) |
Rafael J. Wysocki | ecfbf6f | 2013-12-12 06:11:02 +0100 | [diff] [blame] | 2639 | goto err_remove_dev_groups; |
Rafael J. Wysocki | 4f3549d | 2013-05-02 22:15:29 +0200 | [diff] [blame] | 2640 | } |
| 2641 | |
Saravana Kannan | 25ac86c | 2020-11-20 18:02:28 -0800 | [diff] [blame] | 2642 | if (fw_devlink_flags && !fw_devlink_is_permissive() && dev->fwnode) { |
Saravana Kannan | da6d647 | 2020-05-21 12:18:00 -0700 | [diff] [blame] | 2643 | error = device_create_file(dev, &dev_attr_waiting_for_supplier); |
| 2644 | if (error) |
| 2645 | goto err_remove_dev_online; |
| 2646 | } |
| 2647 | |
Rajat Jain | 70f400d | 2021-05-24 10:18:11 -0700 | [diff] [blame] | 2648 | if (dev_removable_is_valid(dev)) { |
| 2649 | error = device_create_file(dev, &dev_attr_removable); |
| 2650 | if (error) |
| 2651 | goto err_remove_dev_waiting_for_supplier; |
| 2652 | } |
| 2653 | |
Dmitry Torokhov | 621a167 | 2007-03-10 01:37:34 -0500 | [diff] [blame] | 2654 | return 0; |
| 2655 | |
Rajat Jain | 70f400d | 2021-05-24 10:18:11 -0700 | [diff] [blame] | 2656 | err_remove_dev_waiting_for_supplier: |
| 2657 | device_remove_file(dev, &dev_attr_waiting_for_supplier); |
Saravana Kannan | da6d647 | 2020-05-21 12:18:00 -0700 | [diff] [blame] | 2658 | err_remove_dev_online: |
| 2659 | device_remove_file(dev, &dev_attr_online); |
Rafael J. Wysocki | ecfbf6f | 2013-12-12 06:11:02 +0100 | [diff] [blame] | 2660 | err_remove_dev_groups: |
| 2661 | device_remove_groups(dev, dev->groups); |
Dmitry Torokhov | 621a167 | 2007-03-10 01:37:34 -0500 | [diff] [blame] | 2662 | err_remove_type_groups: |
| 2663 | if (type) |
| 2664 | device_remove_groups(dev, type->groups); |
Greg Kroah-Hartman | d05a6f9 | 2013-07-14 16:05:58 -0700 | [diff] [blame] | 2665 | err_remove_class_groups: |
| 2666 | if (class) |
| 2667 | device_remove_groups(dev, class->dev_groups); |
Dmitry Torokhov | 621a167 | 2007-03-10 01:37:34 -0500 | [diff] [blame] | 2668 | |
Greg Kroah-Hartman | 2620efe | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 2669 | return error; |
| 2670 | } |
| 2671 | |
| 2672 | static void device_remove_attrs(struct device *dev) |
| 2673 | { |
| 2674 | struct class *class = dev->class; |
Stephen Hemminger | aed65af | 2011-03-28 09:12:52 -0700 | [diff] [blame] | 2675 | const struct device_type *type = dev->type; |
Greg Kroah-Hartman | 2620efe | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 2676 | |
Rajat Jain | 70f400d | 2021-05-24 10:18:11 -0700 | [diff] [blame] | 2677 | device_remove_file(dev, &dev_attr_removable); |
Saravana Kannan | da6d647 | 2020-05-21 12:18:00 -0700 | [diff] [blame] | 2678 | device_remove_file(dev, &dev_attr_waiting_for_supplier); |
Greg Kroah-Hartman | c5e064a | 2013-08-23 17:07:26 -0700 | [diff] [blame] | 2679 | device_remove_file(dev, &dev_attr_online); |
Dmitry Torokhov | 621a167 | 2007-03-10 01:37:34 -0500 | [diff] [blame] | 2680 | device_remove_groups(dev, dev->groups); |
Kay Sievers | f9f852d | 2006-10-07 21:54:55 +0200 | [diff] [blame] | 2681 | |
Dmitry Torokhov | 621a167 | 2007-03-10 01:37:34 -0500 | [diff] [blame] | 2682 | if (type) |
| 2683 | device_remove_groups(dev, type->groups); |
| 2684 | |
Greg Kroah-Hartman | a6b01de | 2013-10-05 18:19:30 -0700 | [diff] [blame] | 2685 | if (class) |
Greg Kroah-Hartman | d05a6f9 | 2013-07-14 16:05:58 -0700 | [diff] [blame] | 2686 | device_remove_groups(dev, class->dev_groups); |
Greg Kroah-Hartman | 2620efe | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 2687 | } |
| 2688 | |
Greg Kroah-Hartman | c5e064a | 2013-08-23 17:07:26 -0700 | [diff] [blame] | 2689 | 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] | 2690 | char *buf) |
| 2691 | { |
| 2692 | return print_dev_t(buf, dev->devt); |
| 2693 | } |
Greg Kroah-Hartman | c5e064a | 2013-08-23 17:07:26 -0700 | [diff] [blame] | 2694 | static DEVICE_ATTR_RO(dev); |
Tejun Heo | ad6a1e1 | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 2695 | |
Kay Sievers | ca22e56 | 2011-12-14 14:29:38 -0800 | [diff] [blame] | 2696 | /* /sys/devices/ */ |
Greg Kroah-Hartman | 881c6cfd | 2007-11-01 09:29:06 -0600 | [diff] [blame] | 2697 | struct kset *devices_kset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2698 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2699 | /** |
Grygorii Strashko | 52cdbdd | 2015-07-27 20:43:01 +0300 | [diff] [blame] | 2700 | * devices_kset_move_before - Move device in the devices_kset's list. |
| 2701 | * @deva: Device to move. |
| 2702 | * @devb: Device @deva should come before. |
| 2703 | */ |
| 2704 | static void devices_kset_move_before(struct device *deva, struct device *devb) |
| 2705 | { |
| 2706 | if (!devices_kset) |
| 2707 | return; |
| 2708 | pr_debug("devices_kset: Moving %s before %s\n", |
| 2709 | dev_name(deva), dev_name(devb)); |
| 2710 | spin_lock(&devices_kset->list_lock); |
| 2711 | list_move_tail(&deva->kobj.entry, &devb->kobj.entry); |
| 2712 | spin_unlock(&devices_kset->list_lock); |
| 2713 | } |
| 2714 | |
| 2715 | /** |
| 2716 | * devices_kset_move_after - Move device in the devices_kset's list. |
| 2717 | * @deva: Device to move |
| 2718 | * @devb: Device @deva should come after. |
| 2719 | */ |
| 2720 | static void devices_kset_move_after(struct device *deva, struct device *devb) |
| 2721 | { |
| 2722 | if (!devices_kset) |
| 2723 | return; |
| 2724 | pr_debug("devices_kset: Moving %s after %s\n", |
| 2725 | dev_name(deva), dev_name(devb)); |
| 2726 | spin_lock(&devices_kset->list_lock); |
| 2727 | list_move(&deva->kobj.entry, &devb->kobj.entry); |
| 2728 | spin_unlock(&devices_kset->list_lock); |
| 2729 | } |
| 2730 | |
| 2731 | /** |
| 2732 | * devices_kset_move_last - move the device to the end of devices_kset's list. |
| 2733 | * @dev: device to move |
| 2734 | */ |
| 2735 | void devices_kset_move_last(struct device *dev) |
| 2736 | { |
| 2737 | if (!devices_kset) |
| 2738 | return; |
| 2739 | pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev)); |
| 2740 | spin_lock(&devices_kset->list_lock); |
| 2741 | list_move_tail(&dev->kobj.entry, &devices_kset->list); |
| 2742 | spin_unlock(&devices_kset->list_lock); |
| 2743 | } |
| 2744 | |
| 2745 | /** |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 2746 | * device_create_file - create sysfs attribute file for device. |
| 2747 | * @dev: device. |
| 2748 | * @attr: device attribute descriptor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2749 | */ |
Phil Carmody | 26579ab | 2009-12-18 15:34:19 +0200 | [diff] [blame] | 2750 | int device_create_file(struct device *dev, |
| 2751 | const struct device_attribute *attr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2752 | { |
| 2753 | int error = 0; |
Felipe Balbi | 8f46baa | 2013-02-20 10:31:42 +0200 | [diff] [blame] | 2754 | |
| 2755 | if (dev) { |
| 2756 | WARN(((attr->attr.mode & S_IWUGO) && !attr->store), |
dyoung@redhat.com | 9752197 | 2013-05-16 14:31:30 +0800 | [diff] [blame] | 2757 | "Attribute %s: write permission without 'store'\n", |
| 2758 | attr->attr.name); |
Felipe Balbi | 8f46baa | 2013-02-20 10:31:42 +0200 | [diff] [blame] | 2759 | WARN(((attr->attr.mode & S_IRUGO) && !attr->show), |
dyoung@redhat.com | 9752197 | 2013-05-16 14:31:30 +0800 | [diff] [blame] | 2760 | "Attribute %s: read permission without 'show'\n", |
| 2761 | attr->attr.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2762 | error = sysfs_create_file(&dev->kobj, &attr->attr); |
Felipe Balbi | 8f46baa | 2013-02-20 10:31:42 +0200 | [diff] [blame] | 2763 | } |
| 2764 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2765 | return error; |
| 2766 | } |
David Graham White | 86df268 | 2013-07-21 20:41:14 -0400 | [diff] [blame] | 2767 | EXPORT_SYMBOL_GPL(device_create_file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2768 | |
| 2769 | /** |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 2770 | * device_remove_file - remove sysfs attribute file. |
| 2771 | * @dev: device. |
| 2772 | * @attr: device attribute descriptor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2773 | */ |
Phil Carmody | 26579ab | 2009-12-18 15:34:19 +0200 | [diff] [blame] | 2774 | void device_remove_file(struct device *dev, |
| 2775 | const struct device_attribute *attr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2776 | { |
Cornelia Huck | 0c98b19 | 2008-01-31 10:39:38 +0100 | [diff] [blame] | 2777 | if (dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2778 | sysfs_remove_file(&dev->kobj, &attr->attr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2779 | } |
David Graham White | 86df268 | 2013-07-21 20:41:14 -0400 | [diff] [blame] | 2780 | EXPORT_SYMBOL_GPL(device_remove_file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2781 | |
Greg Kroah-Hartman | 2589f188 | 2006-09-19 09:39:19 -0700 | [diff] [blame] | 2782 | /** |
Tejun Heo | 6b0afc2 | 2014-02-03 14:03:01 -0500 | [diff] [blame] | 2783 | * device_remove_file_self - remove sysfs attribute file from its own method. |
| 2784 | * @dev: device. |
| 2785 | * @attr: device attribute descriptor. |
| 2786 | * |
| 2787 | * See kernfs_remove_self() for details. |
| 2788 | */ |
| 2789 | bool device_remove_file_self(struct device *dev, |
| 2790 | const struct device_attribute *attr) |
| 2791 | { |
| 2792 | if (dev) |
| 2793 | return sysfs_remove_file_self(&dev->kobj, &attr->attr); |
| 2794 | else |
| 2795 | return false; |
| 2796 | } |
| 2797 | EXPORT_SYMBOL_GPL(device_remove_file_self); |
| 2798 | |
| 2799 | /** |
Greg Kroah-Hartman | 2589f188 | 2006-09-19 09:39:19 -0700 | [diff] [blame] | 2800 | * device_create_bin_file - create sysfs binary attribute file for device. |
| 2801 | * @dev: device. |
| 2802 | * @attr: device binary attribute descriptor. |
| 2803 | */ |
Phil Carmody | 66ecb92 | 2009-12-18 15:34:20 +0200 | [diff] [blame] | 2804 | int device_create_bin_file(struct device *dev, |
| 2805 | const struct bin_attribute *attr) |
Greg Kroah-Hartman | 2589f188 | 2006-09-19 09:39:19 -0700 | [diff] [blame] | 2806 | { |
| 2807 | int error = -EINVAL; |
| 2808 | if (dev) |
| 2809 | error = sysfs_create_bin_file(&dev->kobj, attr); |
| 2810 | return error; |
| 2811 | } |
| 2812 | EXPORT_SYMBOL_GPL(device_create_bin_file); |
| 2813 | |
| 2814 | /** |
| 2815 | * device_remove_bin_file - remove sysfs binary attribute file |
| 2816 | * @dev: device. |
| 2817 | * @attr: device binary attribute descriptor. |
| 2818 | */ |
Phil Carmody | 66ecb92 | 2009-12-18 15:34:20 +0200 | [diff] [blame] | 2819 | void device_remove_bin_file(struct device *dev, |
| 2820 | const struct bin_attribute *attr) |
Greg Kroah-Hartman | 2589f188 | 2006-09-19 09:39:19 -0700 | [diff] [blame] | 2821 | { |
| 2822 | if (dev) |
| 2823 | sysfs_remove_bin_file(&dev->kobj, attr); |
| 2824 | } |
| 2825 | EXPORT_SYMBOL_GPL(device_remove_bin_file); |
| 2826 | |
James Bottomley | 34bb61f | 2005-09-06 16:56:51 -0700 | [diff] [blame] | 2827 | static void klist_children_get(struct klist_node *n) |
| 2828 | { |
Greg Kroah-Hartman | f791b8c | 2008-12-16 12:24:56 -0800 | [diff] [blame] | 2829 | struct device_private *p = to_device_private_parent(n); |
| 2830 | struct device *dev = p->device; |
James Bottomley | 34bb61f | 2005-09-06 16:56:51 -0700 | [diff] [blame] | 2831 | |
| 2832 | get_device(dev); |
| 2833 | } |
| 2834 | |
| 2835 | static void klist_children_put(struct klist_node *n) |
| 2836 | { |
Greg Kroah-Hartman | f791b8c | 2008-12-16 12:24:56 -0800 | [diff] [blame] | 2837 | struct device_private *p = to_device_private_parent(n); |
| 2838 | struct device *dev = p->device; |
James Bottomley | 34bb61f | 2005-09-06 16:56:51 -0700 | [diff] [blame] | 2839 | |
| 2840 | put_device(dev); |
| 2841 | } |
| 2842 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2843 | /** |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 2844 | * device_initialize - init device structure. |
| 2845 | * @dev: device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2846 | * |
Cornelia Huck | 5739411 | 2008-09-03 18:26:40 +0200 | [diff] [blame] | 2847 | * This prepares the device for use by other layers by initializing |
| 2848 | * its fields. |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 2849 | * It is the first half of device_register(), if called by |
Cornelia Huck | 5739411 | 2008-09-03 18:26:40 +0200 | [diff] [blame] | 2850 | * that function, though it can also be called separately, so one |
| 2851 | * may use @dev's fields. In particular, get_device()/put_device() |
| 2852 | * may be used for reference counting of @dev after calling this |
| 2853 | * function. |
| 2854 | * |
Alan Stern | b10d5ef | 2012-01-17 11:39:00 -0500 | [diff] [blame] | 2855 | * All fields in @dev must be initialized by the caller to 0, except |
| 2856 | * for those explicitly set to some other value. The simplest |
| 2857 | * approach is to use kzalloc() to allocate the structure containing |
| 2858 | * @dev. |
| 2859 | * |
Cornelia Huck | 5739411 | 2008-09-03 18:26:40 +0200 | [diff] [blame] | 2860 | * NOTE: Use put_device() to give up your reference instead of freeing |
| 2861 | * @dev directly once you have called this function. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2862 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2863 | void device_initialize(struct device *dev) |
| 2864 | { |
Greg Kroah-Hartman | 881c6cfd | 2007-11-01 09:29:06 -0600 | [diff] [blame] | 2865 | dev->kobj.kset = devices_kset; |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2866 | kobject_init(&dev->kobj, &device_ktype); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2867 | INIT_LIST_HEAD(&dev->dma_pools); |
Thomas Gleixner | 3142788 | 2010-01-29 20:39:02 +0000 | [diff] [blame] | 2868 | mutex_init(&dev->mutex); |
Dan Williams | 87a30e1 | 2019-07-17 18:08:26 -0700 | [diff] [blame] | 2869 | #ifdef CONFIG_PROVE_LOCKING |
| 2870 | mutex_init(&dev->lockdep_mutex); |
| 2871 | #endif |
Peter Zijlstra | 1704f47 | 2010-03-19 01:37:42 +0100 | [diff] [blame] | 2872 | lockdep_set_novalidate_class(&dev->mutex); |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 2873 | spin_lock_init(&dev->devres_lock); |
| 2874 | INIT_LIST_HEAD(&dev->devres_head); |
Alan Stern | 3b98aea | 2008-08-07 13:06:12 -0400 | [diff] [blame] | 2875 | device_pm_init(dev); |
Max Gurtovoy | d460d7f | 2021-10-04 16:34:52 +0300 | [diff] [blame] | 2876 | set_dev_node(dev, NUMA_NO_NODE); |
Jiang Liu | 4a7cc83 | 2015-07-09 16:00:44 +0800 | [diff] [blame] | 2877 | #ifdef CONFIG_GENERIC_MSI_IRQ |
Thomas Gleixner | 77e89af | 2021-07-29 23:51:47 +0200 | [diff] [blame] | 2878 | raw_spin_lock_init(&dev->msi_lock); |
Jiang Liu | 4a7cc83 | 2015-07-09 16:00:44 +0800 | [diff] [blame] | 2879 | INIT_LIST_HEAD(&dev->msi_list); |
| 2880 | #endif |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 2881 | INIT_LIST_HEAD(&dev->links.consumers); |
| 2882 | INIT_LIST_HEAD(&dev->links.suppliers); |
Saravana Kannan | 3b052a3 | 2020-11-20 18:02:17 -0800 | [diff] [blame] | 2883 | INIT_LIST_HEAD(&dev->links.defer_sync); |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 2884 | dev->links.status = DL_DEV_NO_DRIVER; |
Christoph Hellwig | 6d4e9a8 | 2021-02-10 10:56:39 +0100 | [diff] [blame] | 2885 | #if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE) || \ |
| 2886 | defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU) || \ |
| 2887 | defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL) |
| 2888 | dev->dma_coherent = dma_default_coherent; |
| 2889 | #endif |
Claire Chang | 69031f5 | 2021-06-19 11:40:34 +0800 | [diff] [blame] | 2890 | #ifdef CONFIG_SWIOTLB |
Will Deacon | 463e862 | 2021-07-20 14:38:24 +0100 | [diff] [blame] | 2891 | dev->dma_io_tlb_mem = &io_tlb_default_mem; |
Claire Chang | 69031f5 | 2021-06-19 11:40:34 +0800 | [diff] [blame] | 2892 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2893 | } |
David Graham White | 86df268 | 2013-07-21 20:41:14 -0400 | [diff] [blame] | 2894 | EXPORT_SYMBOL_GPL(device_initialize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2895 | |
Tejun Heo | d73ce00 | 2013-03-12 11:30:05 -0700 | [diff] [blame] | 2896 | struct kobject *virtual_device_parent(struct device *dev) |
Greg Kroah-Hartman | f0ee61a | 2006-10-23 10:40:54 -0700 | [diff] [blame] | 2897 | { |
Kay Sievers | 8640624 | 2007-03-14 03:25:56 +0100 | [diff] [blame] | 2898 | static struct kobject *virtual_dir = NULL; |
Greg Kroah-Hartman | f0ee61a | 2006-10-23 10:40:54 -0700 | [diff] [blame] | 2899 | |
Kay Sievers | 8640624 | 2007-03-14 03:25:56 +0100 | [diff] [blame] | 2900 | if (!virtual_dir) |
Greg Kroah-Hartman | 4ff6abf | 2007-11-05 22:24:43 -0800 | [diff] [blame] | 2901 | virtual_dir = kobject_create_and_add("virtual", |
Greg Kroah-Hartman | 881c6cfd | 2007-11-01 09:29:06 -0600 | [diff] [blame] | 2902 | &devices_kset->kobj); |
Greg Kroah-Hartman | f0ee61a | 2006-10-23 10:40:54 -0700 | [diff] [blame] | 2903 | |
Kay Sievers | 8640624 | 2007-03-14 03:25:56 +0100 | [diff] [blame] | 2904 | return virtual_dir; |
Greg Kroah-Hartman | f0ee61a | 2006-10-23 10:40:54 -0700 | [diff] [blame] | 2905 | } |
| 2906 | |
Eric W. Biederman | bc451f2 | 2010-03-30 11:31:25 -0700 | [diff] [blame] | 2907 | struct class_dir { |
| 2908 | struct kobject kobj; |
| 2909 | struct class *class; |
| 2910 | }; |
| 2911 | |
| 2912 | #define to_class_dir(obj) container_of(obj, struct class_dir, kobj) |
| 2913 | |
| 2914 | static void class_dir_release(struct kobject *kobj) |
| 2915 | { |
| 2916 | struct class_dir *dir = to_class_dir(kobj); |
| 2917 | kfree(dir); |
| 2918 | } |
| 2919 | |
| 2920 | static const |
| 2921 | struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj) |
| 2922 | { |
| 2923 | struct class_dir *dir = to_class_dir(kobj); |
| 2924 | return dir->class->ns_type; |
| 2925 | } |
| 2926 | |
| 2927 | static struct kobj_type class_dir_ktype = { |
| 2928 | .release = class_dir_release, |
| 2929 | .sysfs_ops = &kobj_sysfs_ops, |
| 2930 | .child_ns_type = class_dir_child_ns_type |
| 2931 | }; |
| 2932 | |
| 2933 | static struct kobject * |
| 2934 | class_dir_create_and_add(struct class *class, struct kobject *parent_kobj) |
| 2935 | { |
| 2936 | struct class_dir *dir; |
| 2937 | int retval; |
| 2938 | |
| 2939 | dir = kzalloc(sizeof(*dir), GFP_KERNEL); |
| 2940 | if (!dir) |
Tetsuo Handa | 84d0c27 | 2018-05-07 19:10:31 +0900 | [diff] [blame] | 2941 | return ERR_PTR(-ENOMEM); |
Eric W. Biederman | bc451f2 | 2010-03-30 11:31:25 -0700 | [diff] [blame] | 2942 | |
| 2943 | dir->class = class; |
| 2944 | kobject_init(&dir->kobj, &class_dir_ktype); |
| 2945 | |
Kay Sievers | 6b6e39a | 2010-11-15 23:13:18 +0100 | [diff] [blame] | 2946 | dir->kobj.kset = &class->p->glue_dirs; |
Eric W. Biederman | bc451f2 | 2010-03-30 11:31:25 -0700 | [diff] [blame] | 2947 | |
| 2948 | retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name); |
| 2949 | if (retval < 0) { |
| 2950 | kobject_put(&dir->kobj); |
Tetsuo Handa | 84d0c27 | 2018-05-07 19:10:31 +0900 | [diff] [blame] | 2951 | return ERR_PTR(retval); |
Eric W. Biederman | bc451f2 | 2010-03-30 11:31:25 -0700 | [diff] [blame] | 2952 | } |
| 2953 | return &dir->kobj; |
| 2954 | } |
| 2955 | |
Yijing Wang | e4a60d1 | 2014-11-07 12:05:49 +0800 | [diff] [blame] | 2956 | static DEFINE_MUTEX(gdp_mutex); |
Eric W. Biederman | bc451f2 | 2010-03-30 11:31:25 -0700 | [diff] [blame] | 2957 | |
Kay Sievers | da231fd | 2007-11-21 17:29:15 +0100 | [diff] [blame] | 2958 | static struct kobject *get_device_parent(struct device *dev, |
| 2959 | struct device *parent) |
Greg Kroah-Hartman | 40fa542 | 2006-10-24 00:37:58 +0100 | [diff] [blame] | 2960 | { |
Kay Sievers | 8640624 | 2007-03-14 03:25:56 +0100 | [diff] [blame] | 2961 | if (dev->class) { |
| 2962 | struct kobject *kobj = NULL; |
| 2963 | struct kobject *parent_kobj; |
| 2964 | struct kobject *k; |
| 2965 | |
Randy Dunlap | ead454f | 2010-09-24 14:36:49 -0700 | [diff] [blame] | 2966 | #ifdef CONFIG_BLOCK |
Kay Sievers | 39aba96 | 2010-09-04 22:33:14 -0700 | [diff] [blame] | 2967 | /* block disks show up in /sys/block */ |
Andi Kleen | e52eec1 | 2010-09-08 16:54:17 +0200 | [diff] [blame] | 2968 | if (sysfs_deprecated && dev->class == &block_class) { |
Kay Sievers | 39aba96 | 2010-09-04 22:33:14 -0700 | [diff] [blame] | 2969 | if (parent && parent->class == &block_class) |
| 2970 | return &parent->kobj; |
Kay Sievers | 6b6e39a | 2010-11-15 23:13:18 +0100 | [diff] [blame] | 2971 | return &block_class.p->subsys.kobj; |
Kay Sievers | 39aba96 | 2010-09-04 22:33:14 -0700 | [diff] [blame] | 2972 | } |
Randy Dunlap | ead454f | 2010-09-24 14:36:49 -0700 | [diff] [blame] | 2973 | #endif |
Andi Kleen | e52eec1 | 2010-09-08 16:54:17 +0200 | [diff] [blame] | 2974 | |
Kay Sievers | 8640624 | 2007-03-14 03:25:56 +0100 | [diff] [blame] | 2975 | /* |
| 2976 | * If we have no parent, we live in "virtual". |
Kay Sievers | 0f4dafc | 2007-12-19 01:40:42 +0100 | [diff] [blame] | 2977 | * Class-devices with a non class-device as parent, live |
| 2978 | * in a "glue" directory to prevent namespace collisions. |
Kay Sievers | 8640624 | 2007-03-14 03:25:56 +0100 | [diff] [blame] | 2979 | */ |
| 2980 | if (parent == NULL) |
| 2981 | parent_kobj = virtual_device_parent(dev); |
Eric W. Biederman | 24b1442 | 2010-07-24 22:43:35 -0700 | [diff] [blame] | 2982 | else if (parent->class && !dev->class->ns_type) |
Kay Sievers | 8640624 | 2007-03-14 03:25:56 +0100 | [diff] [blame] | 2983 | return &parent->kobj; |
| 2984 | else |
| 2985 | parent_kobj = &parent->kobj; |
| 2986 | |
Tejun Heo | 77d3d7c | 2010-02-05 17:57:02 +0900 | [diff] [blame] | 2987 | mutex_lock(&gdp_mutex); |
| 2988 | |
Kay Sievers | 8640624 | 2007-03-14 03:25:56 +0100 | [diff] [blame] | 2989 | /* find our class-directory at the parent and reference it */ |
Kay Sievers | 6b6e39a | 2010-11-15 23:13:18 +0100 | [diff] [blame] | 2990 | spin_lock(&dev->class->p->glue_dirs.list_lock); |
| 2991 | list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry) |
Kay Sievers | 8640624 | 2007-03-14 03:25:56 +0100 | [diff] [blame] | 2992 | if (k->parent == parent_kobj) { |
| 2993 | kobj = kobject_get(k); |
| 2994 | break; |
| 2995 | } |
Kay Sievers | 6b6e39a | 2010-11-15 23:13:18 +0100 | [diff] [blame] | 2996 | spin_unlock(&dev->class->p->glue_dirs.list_lock); |
Tejun Heo | 77d3d7c | 2010-02-05 17:57:02 +0900 | [diff] [blame] | 2997 | if (kobj) { |
| 2998 | mutex_unlock(&gdp_mutex); |
Kay Sievers | 8640624 | 2007-03-14 03:25:56 +0100 | [diff] [blame] | 2999 | return kobj; |
Tejun Heo | 77d3d7c | 2010-02-05 17:57:02 +0900 | [diff] [blame] | 3000 | } |
Kay Sievers | 8640624 | 2007-03-14 03:25:56 +0100 | [diff] [blame] | 3001 | |
| 3002 | /* or create a new class-directory at the parent device */ |
Eric W. Biederman | bc451f2 | 2010-03-30 11:31:25 -0700 | [diff] [blame] | 3003 | k = class_dir_create_and_add(dev->class, parent_kobj); |
Kay Sievers | 0f4dafc | 2007-12-19 01:40:42 +0100 | [diff] [blame] | 3004 | /* do not emit an uevent for this simple "glue" directory */ |
Tejun Heo | 77d3d7c | 2010-02-05 17:57:02 +0900 | [diff] [blame] | 3005 | mutex_unlock(&gdp_mutex); |
Greg Kroah-Hartman | 43968d2 | 2007-11-05 22:24:43 -0800 | [diff] [blame] | 3006 | return k; |
Kay Sievers | 8640624 | 2007-03-14 03:25:56 +0100 | [diff] [blame] | 3007 | } |
| 3008 | |
Kay Sievers | ca22e56 | 2011-12-14 14:29:38 -0800 | [diff] [blame] | 3009 | /* subsystems can specify a default root directory for their devices */ |
| 3010 | if (!parent && dev->bus && dev->bus->dev_root) |
| 3011 | return &dev->bus->dev_root->kobj; |
| 3012 | |
Kay Sievers | 8640624 | 2007-03-14 03:25:56 +0100 | [diff] [blame] | 3013 | if (parent) |
Cornelia Huck | c744aeae | 2007-01-08 20:16:44 +0100 | [diff] [blame] | 3014 | return &parent->kobj; |
| 3015 | return NULL; |
| 3016 | } |
Kay Sievers | da231fd | 2007-11-21 17:29:15 +0100 | [diff] [blame] | 3017 | |
Ming Lei | cebf8fd | 2016-07-10 19:27:36 +0800 | [diff] [blame] | 3018 | static inline bool live_in_glue_dir(struct kobject *kobj, |
| 3019 | struct device *dev) |
| 3020 | { |
| 3021 | if (!kobj || !dev->class || |
| 3022 | kobj->kset != &dev->class->p->glue_dirs) |
| 3023 | return false; |
| 3024 | return true; |
| 3025 | } |
| 3026 | |
| 3027 | static inline struct kobject *get_glue_dir(struct device *dev) |
| 3028 | { |
| 3029 | return dev->kobj.parent; |
| 3030 | } |
| 3031 | |
| 3032 | /* |
| 3033 | * make sure cleaning up dir as the last step, we need to make |
| 3034 | * sure .release handler of kobject is run with holding the |
| 3035 | * global lock |
| 3036 | */ |
Cornelia Huck | 63b6971 | 2008-01-21 16:09:44 +0100 | [diff] [blame] | 3037 | static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir) |
Kay Sievers | da231fd | 2007-11-21 17:29:15 +0100 | [diff] [blame] | 3038 | { |
Muchun Song | ac43432 | 2019-07-27 11:21:22 +0800 | [diff] [blame] | 3039 | unsigned int ref; |
| 3040 | |
Kay Sievers | 0f4dafc | 2007-12-19 01:40:42 +0100 | [diff] [blame] | 3041 | /* see if we live in a "glue" directory */ |
Ming Lei | cebf8fd | 2016-07-10 19:27:36 +0800 | [diff] [blame] | 3042 | if (!live_in_glue_dir(glue_dir, dev)) |
Kay Sievers | da231fd | 2007-11-21 17:29:15 +0100 | [diff] [blame] | 3043 | return; |
| 3044 | |
Yijing Wang | e4a60d1 | 2014-11-07 12:05:49 +0800 | [diff] [blame] | 3045 | mutex_lock(&gdp_mutex); |
Muchun Song | ac43432 | 2019-07-27 11:21:22 +0800 | [diff] [blame] | 3046 | /** |
| 3047 | * There is a race condition between removing glue directory |
| 3048 | * and adding a new device under the glue directory. |
| 3049 | * |
| 3050 | * CPU1: CPU2: |
| 3051 | * |
| 3052 | * device_add() |
| 3053 | * get_device_parent() |
| 3054 | * class_dir_create_and_add() |
| 3055 | * kobject_add_internal() |
| 3056 | * create_dir() // create glue_dir |
| 3057 | * |
| 3058 | * device_add() |
| 3059 | * get_device_parent() |
| 3060 | * kobject_get() // get glue_dir |
| 3061 | * |
| 3062 | * device_del() |
| 3063 | * cleanup_glue_dir() |
| 3064 | * kobject_del(glue_dir) |
| 3065 | * |
| 3066 | * kobject_add() |
| 3067 | * kobject_add_internal() |
| 3068 | * create_dir() // in glue_dir |
| 3069 | * sysfs_create_dir_ns() |
| 3070 | * kernfs_create_dir_ns(sd) |
| 3071 | * |
| 3072 | * sysfs_remove_dir() // glue_dir->sd=NULL |
| 3073 | * sysfs_put() // free glue_dir->sd |
| 3074 | * |
| 3075 | * // sd is freed |
| 3076 | * kernfs_new_node(sd) |
| 3077 | * kernfs_get(glue_dir) |
| 3078 | * kernfs_add_one() |
| 3079 | * kernfs_put() |
| 3080 | * |
| 3081 | * Before CPU1 remove last child device under glue dir, if CPU2 add |
| 3082 | * a new device under glue dir, the glue_dir kobject reference count |
| 3083 | * will be increase to 2 in kobject_get(k). And CPU2 has been called |
| 3084 | * kernfs_create_dir_ns(). Meanwhile, CPU1 call sysfs_remove_dir() |
| 3085 | * and sysfs_put(). This result in glue_dir->sd is freed. |
| 3086 | * |
| 3087 | * Then the CPU2 will see a stale "empty" but still potentially used |
| 3088 | * glue dir around in kernfs_new_node(). |
| 3089 | * |
| 3090 | * In order to avoid this happening, we also should make sure that |
| 3091 | * kernfs_node for glue_dir is released in CPU1 only when refcount |
| 3092 | * for glue_dir kobj is 1. |
| 3093 | */ |
| 3094 | ref = kref_read(&glue_dir->kref); |
| 3095 | if (!kobject_has_children(glue_dir) && !--ref) |
Benjamin Herrenschmidt | 726e410 | 2018-07-10 10:29:10 +1000 | [diff] [blame] | 3096 | kobject_del(glue_dir); |
Kay Sievers | 0f4dafc | 2007-12-19 01:40:42 +0100 | [diff] [blame] | 3097 | kobject_put(glue_dir); |
Yijing Wang | e4a60d1 | 2014-11-07 12:05:49 +0800 | [diff] [blame] | 3098 | mutex_unlock(&gdp_mutex); |
Kay Sievers | da231fd | 2007-11-21 17:29:15 +0100 | [diff] [blame] | 3099 | } |
Cornelia Huck | 63b6971 | 2008-01-21 16:09:44 +0100 | [diff] [blame] | 3100 | |
Cornelia Huck | 2ee97ca | 2007-07-18 01:43:47 -0700 | [diff] [blame] | 3101 | static int device_add_class_symlinks(struct device *dev) |
| 3102 | { |
Benjamin Herrenschmidt | 5590f31 | 2015-02-18 11:25:18 +1100 | [diff] [blame] | 3103 | struct device_node *of_node = dev_of_node(dev); |
Cornelia Huck | 2ee97ca | 2007-07-18 01:43:47 -0700 | [diff] [blame] | 3104 | int error; |
| 3105 | |
Benjamin Herrenschmidt | 5590f31 | 2015-02-18 11:25:18 +1100 | [diff] [blame] | 3106 | if (of_node) { |
Rob Herring | 0c3c234 | 2017-10-04 14:04:01 -0500 | [diff] [blame] | 3107 | 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] | 3108 | if (error) |
| 3109 | dev_warn(dev, "Error %d creating of_node link\n",error); |
| 3110 | /* An error here doesn't warrant bringing down the device */ |
| 3111 | } |
| 3112 | |
Cornelia Huck | 2ee97ca | 2007-07-18 01:43:47 -0700 | [diff] [blame] | 3113 | if (!dev->class) |
| 3114 | return 0; |
Kay Sievers | da231fd | 2007-11-21 17:29:15 +0100 | [diff] [blame] | 3115 | |
Greg Kroah-Hartman | 1fbfee6 | 2008-05-28 09:28:39 -0700 | [diff] [blame] | 3116 | error = sysfs_create_link(&dev->kobj, |
Kay Sievers | 6b6e39a | 2010-11-15 23:13:18 +0100 | [diff] [blame] | 3117 | &dev->class->p->subsys.kobj, |
Cornelia Huck | 2ee97ca | 2007-07-18 01:43:47 -0700 | [diff] [blame] | 3118 | "subsystem"); |
| 3119 | if (error) |
Benjamin Herrenschmidt | 5590f31 | 2015-02-18 11:25:18 +1100 | [diff] [blame] | 3120 | goto out_devnode; |
Kay Sievers | da231fd | 2007-11-21 17:29:15 +0100 | [diff] [blame] | 3121 | |
Greg Kroah-Hartman | 4e886c2 | 2008-01-27 12:12:43 -0800 | [diff] [blame] | 3122 | if (dev->parent && device_is_not_partition(dev)) { |
Dmitry Torokhov | 4f01a75 | 2007-09-18 22:46:50 -0700 | [diff] [blame] | 3123 | error = sysfs_create_link(&dev->kobj, &dev->parent->kobj, |
| 3124 | "device"); |
| 3125 | if (error) |
Kay Sievers | 39aba96 | 2010-09-04 22:33:14 -0700 | [diff] [blame] | 3126 | goto out_subsys; |
Cornelia Huck | 2ee97ca | 2007-07-18 01:43:47 -0700 | [diff] [blame] | 3127 | } |
Kay Sievers | 39aba96 | 2010-09-04 22:33:14 -0700 | [diff] [blame] | 3128 | |
Randy Dunlap | ead454f | 2010-09-24 14:36:49 -0700 | [diff] [blame] | 3129 | #ifdef CONFIG_BLOCK |
Kay Sievers | 39aba96 | 2010-09-04 22:33:14 -0700 | [diff] [blame] | 3130 | /* /sys/block has directories and does not need symlinks */ |
Andi Kleen | e52eec1 | 2010-09-08 16:54:17 +0200 | [diff] [blame] | 3131 | if (sysfs_deprecated && dev->class == &block_class) |
Kay Sievers | 39aba96 | 2010-09-04 22:33:14 -0700 | [diff] [blame] | 3132 | return 0; |
Randy Dunlap | ead454f | 2010-09-24 14:36:49 -0700 | [diff] [blame] | 3133 | #endif |
Kay Sievers | 39aba96 | 2010-09-04 22:33:14 -0700 | [diff] [blame] | 3134 | |
| 3135 | /* link in the class directory pointing to the device */ |
Kay Sievers | 6b6e39a | 2010-11-15 23:13:18 +0100 | [diff] [blame] | 3136 | error = sysfs_create_link(&dev->class->p->subsys.kobj, |
Kay Sievers | 39aba96 | 2010-09-04 22:33:14 -0700 | [diff] [blame] | 3137 | &dev->kobj, dev_name(dev)); |
| 3138 | if (error) |
| 3139 | goto out_device; |
| 3140 | |
Cornelia Huck | 2ee97ca | 2007-07-18 01:43:47 -0700 | [diff] [blame] | 3141 | return 0; |
| 3142 | |
Kay Sievers | 39aba96 | 2010-09-04 22:33:14 -0700 | [diff] [blame] | 3143 | out_device: |
| 3144 | sysfs_remove_link(&dev->kobj, "device"); |
Kay Sievers | da231fd | 2007-11-21 17:29:15 +0100 | [diff] [blame] | 3145 | |
Cornelia Huck | 2ee97ca | 2007-07-18 01:43:47 -0700 | [diff] [blame] | 3146 | out_subsys: |
| 3147 | sysfs_remove_link(&dev->kobj, "subsystem"); |
Benjamin Herrenschmidt | 5590f31 | 2015-02-18 11:25:18 +1100 | [diff] [blame] | 3148 | out_devnode: |
| 3149 | sysfs_remove_link(&dev->kobj, "of_node"); |
Cornelia Huck | 2ee97ca | 2007-07-18 01:43:47 -0700 | [diff] [blame] | 3150 | return error; |
| 3151 | } |
| 3152 | |
| 3153 | static void device_remove_class_symlinks(struct device *dev) |
| 3154 | { |
Benjamin Herrenschmidt | 5590f31 | 2015-02-18 11:25:18 +1100 | [diff] [blame] | 3155 | if (dev_of_node(dev)) |
| 3156 | sysfs_remove_link(&dev->kobj, "of_node"); |
| 3157 | |
Cornelia Huck | 2ee97ca | 2007-07-18 01:43:47 -0700 | [diff] [blame] | 3158 | if (!dev->class) |
| 3159 | return; |
Kay Sievers | da231fd | 2007-11-21 17:29:15 +0100 | [diff] [blame] | 3160 | |
Greg Kroah-Hartman | 4e886c2 | 2008-01-27 12:12:43 -0800 | [diff] [blame] | 3161 | if (dev->parent && device_is_not_partition(dev)) |
Kay Sievers | da231fd | 2007-11-21 17:29:15 +0100 | [diff] [blame] | 3162 | sysfs_remove_link(&dev->kobj, "device"); |
Cornelia Huck | 2ee97ca | 2007-07-18 01:43:47 -0700 | [diff] [blame] | 3163 | sysfs_remove_link(&dev->kobj, "subsystem"); |
Randy Dunlap | ead454f | 2010-09-24 14:36:49 -0700 | [diff] [blame] | 3164 | #ifdef CONFIG_BLOCK |
Andi Kleen | e52eec1 | 2010-09-08 16:54:17 +0200 | [diff] [blame] | 3165 | if (sysfs_deprecated && dev->class == &block_class) |
Kay Sievers | 39aba96 | 2010-09-04 22:33:14 -0700 | [diff] [blame] | 3166 | return; |
Randy Dunlap | ead454f | 2010-09-24 14:36:49 -0700 | [diff] [blame] | 3167 | #endif |
Kay Sievers | 6b6e39a | 2010-11-15 23:13:18 +0100 | [diff] [blame] | 3168 | 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] | 3169 | } |
| 3170 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3171 | /** |
Stephen Rothwell | 413c239 | 2008-05-30 10:16:40 +1000 | [diff] [blame] | 3172 | * dev_set_name - set a device name |
| 3173 | * @dev: device |
Randy Dunlap | 4623236 | 2008-06-04 21:40:43 -0700 | [diff] [blame] | 3174 | * @fmt: format string for the device's name |
Stephen Rothwell | 413c239 | 2008-05-30 10:16:40 +1000 | [diff] [blame] | 3175 | */ |
| 3176 | int dev_set_name(struct device *dev, const char *fmt, ...) |
| 3177 | { |
| 3178 | va_list vargs; |
Kay Sievers | 1fa5ae8 | 2009-01-25 15:17:37 +0100 | [diff] [blame] | 3179 | int err; |
Stephen Rothwell | 413c239 | 2008-05-30 10:16:40 +1000 | [diff] [blame] | 3180 | |
| 3181 | va_start(vargs, fmt); |
Kay Sievers | 1fa5ae8 | 2009-01-25 15:17:37 +0100 | [diff] [blame] | 3182 | err = kobject_set_name_vargs(&dev->kobj, fmt, vargs); |
Stephen Rothwell | 413c239 | 2008-05-30 10:16:40 +1000 | [diff] [blame] | 3183 | va_end(vargs); |
Kay Sievers | 1fa5ae8 | 2009-01-25 15:17:37 +0100 | [diff] [blame] | 3184 | return err; |
Stephen Rothwell | 413c239 | 2008-05-30 10:16:40 +1000 | [diff] [blame] | 3185 | } |
| 3186 | EXPORT_SYMBOL_GPL(dev_set_name); |
| 3187 | |
| 3188 | /** |
Dan Williams | e105b8b | 2008-04-21 10:51:07 -0700 | [diff] [blame] | 3189 | * device_to_dev_kobj - select a /sys/dev/ directory for the device |
| 3190 | * @dev: device |
| 3191 | * |
| 3192 | * By default we select char/ for new entries. Setting class->dev_obj |
| 3193 | * to NULL prevents an entry from being created. class->dev_kobj must |
| 3194 | * be set (or cleared) before any devices are registered to the class |
| 3195 | * otherwise device_create_sys_dev_entry() and |
Peter Korsgaard | 0d4e293c | 2012-04-17 12:12:57 +0200 | [diff] [blame] | 3196 | * device_remove_sys_dev_entry() will disagree about the presence of |
| 3197 | * the link. |
Dan Williams | e105b8b | 2008-04-21 10:51:07 -0700 | [diff] [blame] | 3198 | */ |
| 3199 | static struct kobject *device_to_dev_kobj(struct device *dev) |
| 3200 | { |
| 3201 | struct kobject *kobj; |
| 3202 | |
| 3203 | if (dev->class) |
| 3204 | kobj = dev->class->dev_kobj; |
| 3205 | else |
| 3206 | kobj = sysfs_dev_char_kobj; |
| 3207 | |
| 3208 | return kobj; |
| 3209 | } |
| 3210 | |
| 3211 | static int device_create_sys_dev_entry(struct device *dev) |
| 3212 | { |
| 3213 | struct kobject *kobj = device_to_dev_kobj(dev); |
| 3214 | int error = 0; |
| 3215 | char devt_str[15]; |
| 3216 | |
| 3217 | if (kobj) { |
| 3218 | format_dev_t(devt_str, dev->devt); |
| 3219 | error = sysfs_create_link(kobj, &dev->kobj, devt_str); |
| 3220 | } |
| 3221 | |
| 3222 | return error; |
| 3223 | } |
| 3224 | |
| 3225 | static void device_remove_sys_dev_entry(struct device *dev) |
| 3226 | { |
| 3227 | struct kobject *kobj = device_to_dev_kobj(dev); |
| 3228 | char devt_str[15]; |
| 3229 | |
| 3230 | if (kobj) { |
| 3231 | format_dev_t(devt_str, dev->devt); |
| 3232 | sysfs_remove_link(kobj, devt_str); |
| 3233 | } |
| 3234 | } |
| 3235 | |
Shaokun Zhang | 46d3a03 | 2018-07-15 18:08:56 +0800 | [diff] [blame] | 3236 | static int device_private_init(struct device *dev) |
Greg Kroah-Hartman | b402843 | 2009-05-11 14:16:57 -0700 | [diff] [blame] | 3237 | { |
| 3238 | dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL); |
| 3239 | if (!dev->p) |
| 3240 | return -ENOMEM; |
| 3241 | dev->p->device = dev; |
| 3242 | klist_init(&dev->p->klist_children, klist_children_get, |
| 3243 | klist_children_put); |
Greg Kroah-Hartman | ef8a3fd | 2012-03-08 12:17:22 -0800 | [diff] [blame] | 3244 | INIT_LIST_HEAD(&dev->p->deferred_probe); |
Greg Kroah-Hartman | b402843 | 2009-05-11 14:16:57 -0700 | [diff] [blame] | 3245 | return 0; |
| 3246 | } |
| 3247 | |
Dan Williams | e105b8b | 2008-04-21 10:51:07 -0700 | [diff] [blame] | 3248 | /** |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 3249 | * device_add - add device to device hierarchy. |
| 3250 | * @dev: device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3251 | * |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 3252 | * This is part 2 of device_register(), though may be called |
| 3253 | * separately _iff_ device_initialize() has been called separately. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3254 | * |
Cornelia Huck | 5739411 | 2008-09-03 18:26:40 +0200 | [diff] [blame] | 3255 | * 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] | 3256 | * to the global and sibling lists for the device, then |
| 3257 | * adds it to the other relevant subsystems of the driver model. |
Cornelia Huck | 5739411 | 2008-09-03 18:26:40 +0200 | [diff] [blame] | 3258 | * |
Alan Stern | b10d5ef | 2012-01-17 11:39:00 -0500 | [diff] [blame] | 3259 | * Do not call this routine or device_register() more than once for |
| 3260 | * any device structure. The driver model core is not designed to work |
| 3261 | * with devices that get unregistered and then spring back to life. |
| 3262 | * (Among other things, it's very hard to guarantee that all references |
| 3263 | * to the previous incarnation of @dev have been dropped.) Allocate |
| 3264 | * and register a fresh new struct device instead. |
| 3265 | * |
Cornelia Huck | 5739411 | 2008-09-03 18:26:40 +0200 | [diff] [blame] | 3266 | * NOTE: _Never_ directly free @dev after calling this function, even |
| 3267 | * if it returned an error! Always use put_device() to give up your |
| 3268 | * reference instead. |
Borislav Petkov | affada7 | 2019-04-18 19:41:56 +0200 | [diff] [blame] | 3269 | * |
| 3270 | * Rule of thumb is: if device_add() succeeds, you should call |
| 3271 | * device_del() when you want to get rid of it. If device_add() has |
| 3272 | * *not* succeeded, use *only* put_device() to drop the reference |
| 3273 | * count. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3274 | */ |
| 3275 | int device_add(struct device *dev) |
| 3276 | { |
Viresh Kumar | 35dbf4e | 2017-03-17 12:24:22 +0530 | [diff] [blame] | 3277 | struct device *parent; |
Kay Sievers | ca22e56 | 2011-12-14 14:29:38 -0800 | [diff] [blame] | 3278 | struct kobject *kobj; |
Greg Kroah-Hartman | c47ed21 | 2006-09-13 15:34:05 +0200 | [diff] [blame] | 3279 | struct class_interface *class_intf; |
Saravana Kannan | 5f5377e | 2020-05-14 22:34:58 -0700 | [diff] [blame] | 3280 | int error = -EINVAL; |
Ming Lei | cebf8fd | 2016-07-10 19:27:36 +0800 | [diff] [blame] | 3281 | struct kobject *glue_dir = NULL; |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 3282 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3283 | dev = get_device(dev); |
Greg Kroah-Hartman | c906a48 | 2008-05-30 10:45:12 -0700 | [diff] [blame] | 3284 | if (!dev) |
| 3285 | goto done; |
| 3286 | |
Greg Kroah-Hartman | fb069a5 | 2008-12-16 12:23:36 -0800 | [diff] [blame] | 3287 | if (!dev->p) { |
Greg Kroah-Hartman | b402843 | 2009-05-11 14:16:57 -0700 | [diff] [blame] | 3288 | error = device_private_init(dev); |
| 3289 | if (error) |
| 3290 | goto done; |
Greg Kroah-Hartman | fb069a5 | 2008-12-16 12:23:36 -0800 | [diff] [blame] | 3291 | } |
Greg Kroah-Hartman | fb069a5 | 2008-12-16 12:23:36 -0800 | [diff] [blame] | 3292 | |
Kay Sievers | 1fa5ae8 | 2009-01-25 15:17:37 +0100 | [diff] [blame] | 3293 | /* |
| 3294 | * for statically allocated devices, which should all be converted |
| 3295 | * some day, we need to initialize the name. We prevent reading back |
| 3296 | * the name, and force the use of dev_name() |
| 3297 | */ |
| 3298 | if (dev->init_name) { |
Greg Kroah-Hartman | acc0e90 | 2009-06-02 15:39:55 -0700 | [diff] [blame] | 3299 | dev_set_name(dev, "%s", dev->init_name); |
Kay Sievers | 1fa5ae8 | 2009-01-25 15:17:37 +0100 | [diff] [blame] | 3300 | dev->init_name = NULL; |
| 3301 | } |
Greg Kroah-Hartman | c906a48 | 2008-05-30 10:45:12 -0700 | [diff] [blame] | 3302 | |
Kay Sievers | ca22e56 | 2011-12-14 14:29:38 -0800 | [diff] [blame] | 3303 | /* subsystems can specify simple device enumeration */ |
| 3304 | if (!dev_name(dev) && dev->bus && dev->bus->dev_name) |
| 3305 | dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id); |
| 3306 | |
Thomas Gleixner | e6309e7 | 2009-12-10 19:32:49 +0000 | [diff] [blame] | 3307 | if (!dev_name(dev)) { |
| 3308 | error = -EINVAL; |
Kay Sievers | 5c8563d | 2009-05-28 14:24:07 -0700 | [diff] [blame] | 3309 | goto name_error; |
Thomas Gleixner | e6309e7 | 2009-12-10 19:32:49 +0000 | [diff] [blame] | 3310 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3311 | |
Kay Sievers | 1e0b2cf | 2008-10-30 01:36:48 +0100 | [diff] [blame] | 3312 | pr_debug("device: '%s': %s\n", dev_name(dev), __func__); |
Greg Kroah-Hartman | c205ef4 | 2006-08-07 22:19:37 -0700 | [diff] [blame] | 3313 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3314 | parent = get_device(dev->parent); |
Kay Sievers | ca22e56 | 2011-12-14 14:29:38 -0800 | [diff] [blame] | 3315 | kobj = get_device_parent(dev, parent); |
Tetsuo Handa | 84d0c27 | 2018-05-07 19:10:31 +0900 | [diff] [blame] | 3316 | if (IS_ERR(kobj)) { |
| 3317 | error = PTR_ERR(kobj); |
| 3318 | goto parent_error; |
| 3319 | } |
Kay Sievers | ca22e56 | 2011-12-14 14:29:38 -0800 | [diff] [blame] | 3320 | if (kobj) |
| 3321 | dev->kobj.parent = kobj; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3322 | |
Yinghai Lu | 0d358f2 | 2008-02-19 03:20:41 -0800 | [diff] [blame] | 3323 | /* use parent numa_node */ |
Zhen Lei | 56f2de8 | 2015-08-25 12:08:22 +0800 | [diff] [blame] | 3324 | if (parent && (dev_to_node(dev) == NUMA_NO_NODE)) |
Yinghai Lu | 0d358f2 | 2008-02-19 03:20:41 -0800 | [diff] [blame] | 3325 | set_dev_node(dev, dev_to_node(parent)); |
| 3326 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3327 | /* first, register with generic layer. */ |
Kay Sievers | 8a577ff | 2009-04-18 15:05:45 -0700 | [diff] [blame] | 3328 | /* we require the name to be set before, and pass NULL */ |
| 3329 | error = kobject_add(&dev->kobj, dev->kobj.parent, NULL); |
Ming Lei | cebf8fd | 2016-07-10 19:27:36 +0800 | [diff] [blame] | 3330 | if (error) { |
| 3331 | glue_dir = get_glue_dir(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3332 | goto Error; |
Ming Lei | cebf8fd | 2016-07-10 19:27:36 +0800 | [diff] [blame] | 3333 | } |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 3334 | |
Brian Walsh | 3702264 | 2006-08-14 22:43:19 -0700 | [diff] [blame] | 3335 | /* notify platform of device entry */ |
Rafael J. Wysocki | b2ebd9d | 2021-07-12 19:28:16 +0200 | [diff] [blame] | 3336 | device_platform_notify(dev); |
Brian Walsh | 3702264 | 2006-08-14 22:43:19 -0700 | [diff] [blame] | 3337 | |
Greg Kroah-Hartman | c5e064a | 2013-08-23 17:07:26 -0700 | [diff] [blame] | 3338 | error = device_create_file(dev, &dev_attr_uevent); |
Cornelia Huck | a306eea | 2006-09-22 11:37:13 +0200 | [diff] [blame] | 3339 | if (error) |
| 3340 | goto attrError; |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 3341 | |
Cornelia Huck | 2ee97ca | 2007-07-18 01:43:47 -0700 | [diff] [blame] | 3342 | error = device_add_class_symlinks(dev); |
| 3343 | if (error) |
| 3344 | goto SymlinkError; |
Cornelia Huck | dc0afa8 | 2007-07-09 11:39:18 -0700 | [diff] [blame] | 3345 | error = device_add_attrs(dev); |
| 3346 | if (error) |
Greg Kroah-Hartman | 2620efe | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 3347 | goto AttrsError; |
Cornelia Huck | dc0afa8 | 2007-07-09 11:39:18 -0700 | [diff] [blame] | 3348 | error = bus_add_device(dev); |
| 3349 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3350 | goto BusError; |
Alan Stern | 3b98aea | 2008-08-07 13:06:12 -0400 | [diff] [blame] | 3351 | error = dpm_sysfs_add(dev); |
Rafael J. Wysocki | 57eee3d | 2008-03-12 00:59:38 +0100 | [diff] [blame] | 3352 | if (error) |
Alan Stern | 3b98aea | 2008-08-07 13:06:12 -0400 | [diff] [blame] | 3353 | goto DPMError; |
| 3354 | device_pm_add(dev); |
Alan Stern | ec0676ee | 2008-12-05 14:10:31 -0500 | [diff] [blame] | 3355 | |
Sergey Klyaus | 0cd7504 | 2014-10-08 11:31:54 +0400 | [diff] [blame] | 3356 | if (MAJOR(dev->devt)) { |
| 3357 | error = device_create_file(dev, &dev_attr_dev); |
| 3358 | if (error) |
| 3359 | goto DevAttrError; |
| 3360 | |
| 3361 | error = device_create_sys_dev_entry(dev); |
| 3362 | if (error) |
| 3363 | goto SysEntryError; |
| 3364 | |
| 3365 | devtmpfs_create_node(dev); |
| 3366 | } |
| 3367 | |
Alan Stern | ec0676ee | 2008-12-05 14:10:31 -0500 | [diff] [blame] | 3368 | /* Notify clients of device addition. This call must come |
majianpeng | 268863f | 2012-01-11 15:12:06 +0000 | [diff] [blame] | 3369 | * after dpm_sysfs_add() and before kobject_uevent(). |
Alan Stern | ec0676ee | 2008-12-05 14:10:31 -0500 | [diff] [blame] | 3370 | */ |
| 3371 | if (dev->bus) |
| 3372 | blocking_notifier_call_chain(&dev->bus->p->bus_notifier, |
| 3373 | BUS_NOTIFY_ADD_DEVICE, dev); |
| 3374 | |
Cornelia Huck | 83b5fb4c | 2007-03-29 11:12:11 +0200 | [diff] [blame] | 3375 | kobject_uevent(&dev->kobj, KOBJ_ADD); |
Saravana Kannan | 372a67c | 2019-09-04 14:11:20 -0700 | [diff] [blame] | 3376 | |
Saravana Kannan | e2ae9bc | 2019-09-04 14:11:21 -0700 | [diff] [blame] | 3377 | /* |
| 3378 | * Check if any of the other devices (consumers) have been waiting for |
| 3379 | * this device (supplier) to be added so that they can create a device |
| 3380 | * link to it. |
| 3381 | * |
| 3382 | * This needs to happen after device_pm_add() because device_link_add() |
| 3383 | * requires the supplier be registered before it's called. |
| 3384 | * |
Saravana Kannan | 2cd38fd | 2020-05-19 20:48:21 -0700 | [diff] [blame] | 3385 | * But this also needs to happen before bus_probe_device() to make sure |
Saravana Kannan | e2ae9bc | 2019-09-04 14:11:21 -0700 | [diff] [blame] | 3386 | * waiting consumers can link to it before the driver is bound to the |
| 3387 | * device and the driver sync_state callback is called for this device. |
| 3388 | */ |
Saravana Kannan | 2cd38fd | 2020-05-19 20:48:21 -0700 | [diff] [blame] | 3389 | if (dev->fwnode && !dev->fwnode->dev) { |
| 3390 | dev->fwnode->dev = dev; |
Saravana Kannan | 5f5377e | 2020-05-14 22:34:58 -0700 | [diff] [blame] | 3391 | fw_devlink_link_device(dev); |
Saravana Kannan | 0332450 | 2019-10-28 15:00:24 -0700 | [diff] [blame] | 3392 | } |
Saravana Kannan | e2ae9bc | 2019-09-04 14:11:21 -0700 | [diff] [blame] | 3393 | |
Alan Stern | 2023c61 | 2009-07-30 15:27:18 -0400 | [diff] [blame] | 3394 | bus_probe_device(dev); |
Saravana Kannan | d46f3e3 | 2021-04-01 21:03:41 -0700 | [diff] [blame] | 3395 | |
| 3396 | /* |
| 3397 | * If all driver registration is done and a newly added device doesn't |
| 3398 | * match with any driver, don't block its consumers from probing in |
| 3399 | * case the consumer device is able to operate without this supplier. |
| 3400 | */ |
| 3401 | if (dev->fwnode && fw_devlink_drv_reg_done && !dev->can_match) |
| 3402 | fw_devlink_unblock_consumers(dev); |
| 3403 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3404 | if (parent) |
Greg Kroah-Hartman | f791b8c | 2008-12-16 12:24:56 -0800 | [diff] [blame] | 3405 | klist_add_tail(&dev->p->knode_parent, |
| 3406 | &parent->p->klist_children); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3407 | |
Greg Kroah-Hartman | 5d9fd16 | 2006-06-22 17:17:32 -0700 | [diff] [blame] | 3408 | if (dev->class) { |
Kay Sievers | ca22e56 | 2011-12-14 14:29:38 -0800 | [diff] [blame] | 3409 | mutex_lock(&dev->class->p->mutex); |
Greg Kroah-Hartman | c47ed21 | 2006-09-13 15:34:05 +0200 | [diff] [blame] | 3410 | /* tie the class to the device */ |
Wei Yang | 570d0200 | 2019-01-18 10:34:59 +0800 | [diff] [blame] | 3411 | klist_add_tail(&dev->p->knode_class, |
Kay Sievers | 6b6e39a | 2010-11-15 23:13:18 +0100 | [diff] [blame] | 3412 | &dev->class->p->klist_devices); |
Greg Kroah-Hartman | c47ed21 | 2006-09-13 15:34:05 +0200 | [diff] [blame] | 3413 | |
| 3414 | /* notify any interfaces that the device is here */ |
Greg Kroah-Hartman | 184f1f7 | 2008-05-28 09:28:39 -0700 | [diff] [blame] | 3415 | list_for_each_entry(class_intf, |
Kay Sievers | ca22e56 | 2011-12-14 14:29:38 -0800 | [diff] [blame] | 3416 | &dev->class->p->interfaces, node) |
Greg Kroah-Hartman | c47ed21 | 2006-09-13 15:34:05 +0200 | [diff] [blame] | 3417 | if (class_intf->add_dev) |
| 3418 | class_intf->add_dev(dev, class_intf); |
Kay Sievers | ca22e56 | 2011-12-14 14:29:38 -0800 | [diff] [blame] | 3419 | mutex_unlock(&dev->class->p->mutex); |
Greg Kroah-Hartman | 5d9fd16 | 2006-06-22 17:17:32 -0700 | [diff] [blame] | 3420 | } |
Greg Kroah-Hartman | c906a48 | 2008-05-30 10:45:12 -0700 | [diff] [blame] | 3421 | done: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3422 | put_device(dev); |
| 3423 | return error; |
Sergey Klyaus | 0cd7504 | 2014-10-08 11:31:54 +0400 | [diff] [blame] | 3424 | SysEntryError: |
| 3425 | if (MAJOR(dev->devt)) |
| 3426 | device_remove_file(dev, &dev_attr_dev); |
| 3427 | DevAttrError: |
| 3428 | device_pm_remove(dev); |
| 3429 | dpm_sysfs_remove(dev); |
Alan Stern | 3b98aea | 2008-08-07 13:06:12 -0400 | [diff] [blame] | 3430 | DPMError: |
Rafael J. Wysocki | 57eee3d | 2008-03-12 00:59:38 +0100 | [diff] [blame] | 3431 | bus_remove_device(dev); |
| 3432 | BusError: |
James Simmons | 82f0cf9 | 2007-02-21 17:44:51 +0000 | [diff] [blame] | 3433 | device_remove_attrs(dev); |
Greg Kroah-Hartman | 2620efe | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 3434 | AttrsError: |
Cornelia Huck | 2ee97ca | 2007-07-18 01:43:47 -0700 | [diff] [blame] | 3435 | device_remove_class_symlinks(dev); |
| 3436 | SymlinkError: |
Greg Kroah-Hartman | c5e064a | 2013-08-23 17:07:26 -0700 | [diff] [blame] | 3437 | device_remove_file(dev, &dev_attr_uevent); |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 3438 | attrError: |
Rafael J. Wysocki | b2ebd9d | 2021-07-12 19:28:16 +0200 | [diff] [blame] | 3439 | device_platform_notify_remove(dev); |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 3440 | kobject_uevent(&dev->kobj, KOBJ_REMOVE); |
Ming Lei | cebf8fd | 2016-07-10 19:27:36 +0800 | [diff] [blame] | 3441 | glue_dir = get_glue_dir(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3442 | kobject_del(&dev->kobj); |
| 3443 | Error: |
Ming Lei | cebf8fd | 2016-07-10 19:27:36 +0800 | [diff] [blame] | 3444 | cleanup_glue_dir(dev, glue_dir); |
Tetsuo Handa | 84d0c27 | 2018-05-07 19:10:31 +0900 | [diff] [blame] | 3445 | parent_error: |
Markus Elfring | 5f0163a | 2015-02-05 11:48:26 +0100 | [diff] [blame] | 3446 | put_device(parent); |
Kay Sievers | 5c8563d | 2009-05-28 14:24:07 -0700 | [diff] [blame] | 3447 | name_error: |
| 3448 | kfree(dev->p); |
| 3449 | dev->p = NULL; |
Greg Kroah-Hartman | c906a48 | 2008-05-30 10:45:12 -0700 | [diff] [blame] | 3450 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3451 | } |
David Graham White | 86df268 | 2013-07-21 20:41:14 -0400 | [diff] [blame] | 3452 | EXPORT_SYMBOL_GPL(device_add); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3453 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3454 | /** |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 3455 | * device_register - register a device with the system. |
| 3456 | * @dev: pointer to the device structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3457 | * |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 3458 | * This happens in two clean steps - initialize the device |
| 3459 | * and add it to the system. The two steps can be called |
| 3460 | * separately, but this is the easiest and most common. |
| 3461 | * I.e. you should only call the two helpers separately if |
| 3462 | * have a clearly defined need to use and refcount the device |
| 3463 | * before it is added to the hierarchy. |
Cornelia Huck | 5739411 | 2008-09-03 18:26:40 +0200 | [diff] [blame] | 3464 | * |
Alan Stern | b10d5ef | 2012-01-17 11:39:00 -0500 | [diff] [blame] | 3465 | * For more information, see the kerneldoc for device_initialize() |
| 3466 | * and device_add(). |
| 3467 | * |
Cornelia Huck | 5739411 | 2008-09-03 18:26:40 +0200 | [diff] [blame] | 3468 | * NOTE: _Never_ directly free @dev after calling this function, even |
| 3469 | * if it returned an error! Always use put_device() to give up the |
| 3470 | * reference initialized in this function instead. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3471 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3472 | int device_register(struct device *dev) |
| 3473 | { |
| 3474 | device_initialize(dev); |
| 3475 | return device_add(dev); |
| 3476 | } |
David Graham White | 86df268 | 2013-07-21 20:41:14 -0400 | [diff] [blame] | 3477 | EXPORT_SYMBOL_GPL(device_register); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3478 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3479 | /** |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 3480 | * get_device - increment reference count for device. |
| 3481 | * @dev: device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3482 | * |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 3483 | * This simply forwards the call to kobject_get(), though |
| 3484 | * we do take care to provide for the case that we get a NULL |
| 3485 | * pointer passed in. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3486 | */ |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 3487 | struct device *get_device(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3488 | { |
Lars-Peter Clausen | b0d1f80 | 2012-07-03 18:49:36 +0200 | [diff] [blame] | 3489 | return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3490 | } |
David Graham White | 86df268 | 2013-07-21 20:41:14 -0400 | [diff] [blame] | 3491 | EXPORT_SYMBOL_GPL(get_device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3492 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3493 | /** |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 3494 | * put_device - decrement reference count. |
| 3495 | * @dev: device in question. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3496 | */ |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 3497 | void put_device(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3498 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 3499 | /* might_sleep(); */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3500 | if (dev) |
| 3501 | kobject_put(&dev->kobj); |
| 3502 | } |
David Graham White | 86df268 | 2013-07-21 20:41:14 -0400 | [diff] [blame] | 3503 | EXPORT_SYMBOL_GPL(put_device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3504 | |
Dan Williams | 00289cd | 2019-07-17 18:07:53 -0700 | [diff] [blame] | 3505 | bool kill_device(struct device *dev) |
| 3506 | { |
| 3507 | /* |
| 3508 | * Require the device lock and set the "dead" flag to guarantee that |
| 3509 | * the update behavior is consistent with the other bitfields near |
| 3510 | * it and that we cannot have an asynchronous probe routine trying |
| 3511 | * to run while we are tearing out the bus/class/sysfs from |
| 3512 | * underneath the device. |
| 3513 | */ |
Julian Wiedmann | 8c60a14 | 2021-05-12 16:10:54 +0200 | [diff] [blame] | 3514 | device_lock_assert(dev); |
Dan Williams | 00289cd | 2019-07-17 18:07:53 -0700 | [diff] [blame] | 3515 | |
| 3516 | if (dev->p->dead) |
| 3517 | return false; |
| 3518 | dev->p->dead = true; |
| 3519 | return true; |
| 3520 | } |
| 3521 | EXPORT_SYMBOL_GPL(kill_device); |
| 3522 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3523 | /** |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 3524 | * device_del - delete device from system. |
| 3525 | * @dev: device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3526 | * |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 3527 | * This is the first part of the device unregistration |
| 3528 | * sequence. This removes the device from the lists we control |
| 3529 | * from here, has it removed from the other driver model |
| 3530 | * subsystems it was added to in device_add(), and removes it |
| 3531 | * from the kobject hierarchy. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3532 | * |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 3533 | * NOTE: this should be called manually _iff_ device_add() was |
| 3534 | * also called manually. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3535 | */ |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 3536 | void device_del(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3537 | { |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 3538 | struct device *parent = dev->parent; |
Ming Lei | cebf8fd | 2016-07-10 19:27:36 +0800 | [diff] [blame] | 3539 | struct kobject *glue_dir = NULL; |
Greg Kroah-Hartman | c47ed21 | 2006-09-13 15:34:05 +0200 | [diff] [blame] | 3540 | struct class_interface *class_intf; |
Oliver Neukum | b853001 | 2020-09-16 21:15:44 +0200 | [diff] [blame] | 3541 | unsigned int noio_flag; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3542 | |
Alexander Duyck | 3451a49 | 2019-01-22 10:39:10 -0800 | [diff] [blame] | 3543 | device_lock(dev); |
Dan Williams | 00289cd | 2019-07-17 18:07:53 -0700 | [diff] [blame] | 3544 | kill_device(dev); |
Alexander Duyck | 3451a49 | 2019-01-22 10:39:10 -0800 | [diff] [blame] | 3545 | device_unlock(dev); |
| 3546 | |
Saravana Kannan | 372a67c | 2019-09-04 14:11:20 -0700 | [diff] [blame] | 3547 | if (dev->fwnode && dev->fwnode->dev == dev) |
| 3548 | dev->fwnode->dev = NULL; |
| 3549 | |
Alan Stern | ec0676ee | 2008-12-05 14:10:31 -0500 | [diff] [blame] | 3550 | /* Notify clients of device removal. This call must come |
| 3551 | * before dpm_sysfs_remove(). |
| 3552 | */ |
Oliver Neukum | b853001 | 2020-09-16 21:15:44 +0200 | [diff] [blame] | 3553 | noio_flag = memalloc_noio_save(); |
Alan Stern | ec0676ee | 2008-12-05 14:10:31 -0500 | [diff] [blame] | 3554 | if (dev->bus) |
| 3555 | blocking_notifier_call_chain(&dev->bus->p->bus_notifier, |
| 3556 | BUS_NOTIFY_DEL_DEVICE, dev); |
Rafael J. Wysocki | 9ed9895 | 2016-10-30 17:32:16 +0100 | [diff] [blame] | 3557 | |
Alan Stern | 3b98aea | 2008-08-07 13:06:12 -0400 | [diff] [blame] | 3558 | dpm_sysfs_remove(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3559 | if (parent) |
Greg Kroah-Hartman | f791b8c | 2008-12-16 12:24:56 -0800 | [diff] [blame] | 3560 | klist_del(&dev->p->knode_parent); |
Dan Williams | e105b8b | 2008-04-21 10:51:07 -0700 | [diff] [blame] | 3561 | if (MAJOR(dev->devt)) { |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 3562 | devtmpfs_delete_node(dev); |
Dan Williams | e105b8b | 2008-04-21 10:51:07 -0700 | [diff] [blame] | 3563 | device_remove_sys_dev_entry(dev); |
Greg Kroah-Hartman | c5e064a | 2013-08-23 17:07:26 -0700 | [diff] [blame] | 3564 | device_remove_file(dev, &dev_attr_dev); |
Dan Williams | e105b8b | 2008-04-21 10:51:07 -0700 | [diff] [blame] | 3565 | } |
Kay Sievers | b9d9c82 | 2006-06-15 15:31:56 +0200 | [diff] [blame] | 3566 | if (dev->class) { |
Kay Sievers | da231fd | 2007-11-21 17:29:15 +0100 | [diff] [blame] | 3567 | device_remove_class_symlinks(dev); |
Kay Sievers | 99ef3ef | 2006-09-14 11:23:28 +0200 | [diff] [blame] | 3568 | |
Kay Sievers | ca22e56 | 2011-12-14 14:29:38 -0800 | [diff] [blame] | 3569 | mutex_lock(&dev->class->p->mutex); |
Greg Kroah-Hartman | c47ed21 | 2006-09-13 15:34:05 +0200 | [diff] [blame] | 3570 | /* notify any interfaces that the device is now gone */ |
Greg Kroah-Hartman | 184f1f7 | 2008-05-28 09:28:39 -0700 | [diff] [blame] | 3571 | list_for_each_entry(class_intf, |
Kay Sievers | ca22e56 | 2011-12-14 14:29:38 -0800 | [diff] [blame] | 3572 | &dev->class->p->interfaces, node) |
Greg Kroah-Hartman | c47ed21 | 2006-09-13 15:34:05 +0200 | [diff] [blame] | 3573 | if (class_intf->remove_dev) |
| 3574 | class_intf->remove_dev(dev, class_intf); |
| 3575 | /* remove the device from the class list */ |
Wei Yang | 570d0200 | 2019-01-18 10:34:59 +0800 | [diff] [blame] | 3576 | klist_del(&dev->p->knode_class); |
Kay Sievers | ca22e56 | 2011-12-14 14:29:38 -0800 | [diff] [blame] | 3577 | mutex_unlock(&dev->class->p->mutex); |
Kay Sievers | b9d9c82 | 2006-06-15 15:31:56 +0200 | [diff] [blame] | 3578 | } |
Greg Kroah-Hartman | c5e064a | 2013-08-23 17:07:26 -0700 | [diff] [blame] | 3579 | device_remove_file(dev, &dev_attr_uevent); |
Greg Kroah-Hartman | 2620efe | 2006-06-28 16:19:58 -0700 | [diff] [blame] | 3580 | device_remove_attrs(dev); |
Benjamin Herrenschmidt | 2895353 | 2006-11-08 19:46:14 -0800 | [diff] [blame] | 3581 | bus_remove_device(dev); |
LongX Zhang | 4b6d1f12 | 2012-10-25 00:21:28 +0200 | [diff] [blame] | 3582 | device_pm_remove(dev); |
Grant Likely | d1c3414 | 2012-03-05 08:47:41 -0700 | [diff] [blame] | 3583 | driver_deferred_probe_del(dev); |
Rafael J. Wysocki | b2ebd9d | 2021-07-12 19:28:16 +0200 | [diff] [blame] | 3584 | device_platform_notify_remove(dev); |
Lukas Wunner | 478573c | 2016-07-28 02:25:41 +0200 | [diff] [blame] | 3585 | device_remove_properties(dev); |
Jeffy Chen | 2ec1615 | 2017-10-20 20:01:01 +0800 | [diff] [blame] | 3586 | device_links_purge(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3587 | |
Joerg Roedel | 599bad3 | 2014-09-30 13:02:02 +0200 | [diff] [blame] | 3588 | if (dev->bus) |
| 3589 | blocking_notifier_call_chain(&dev->bus->p->bus_notifier, |
| 3590 | BUS_NOTIFY_REMOVED_DEVICE, dev); |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 3591 | kobject_uevent(&dev->kobj, KOBJ_REMOVE); |
Ming Lei | cebf8fd | 2016-07-10 19:27:36 +0800 | [diff] [blame] | 3592 | glue_dir = get_glue_dir(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3593 | kobject_del(&dev->kobj); |
Ming Lei | cebf8fd | 2016-07-10 19:27:36 +0800 | [diff] [blame] | 3594 | cleanup_glue_dir(dev, glue_dir); |
Oliver Neukum | b853001 | 2020-09-16 21:15:44 +0200 | [diff] [blame] | 3595 | memalloc_noio_restore(noio_flag); |
Kay Sievers | da231fd | 2007-11-21 17:29:15 +0100 | [diff] [blame] | 3596 | put_device(parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3597 | } |
David Graham White | 86df268 | 2013-07-21 20:41:14 -0400 | [diff] [blame] | 3598 | EXPORT_SYMBOL_GPL(device_del); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3599 | |
| 3600 | /** |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 3601 | * device_unregister - unregister device from system. |
| 3602 | * @dev: device going away. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3603 | * |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 3604 | * We do this in two parts, like we do device_register(). First, |
| 3605 | * we remove it from all the subsystems with device_del(), then |
| 3606 | * we decrement the reference count via put_device(). If that |
| 3607 | * is the final reference count, the device will be cleaned up |
| 3608 | * via device_release() above. Otherwise, the structure will |
| 3609 | * stick around until the final reference to the device is dropped. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3610 | */ |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 3611 | void device_unregister(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3612 | { |
Kay Sievers | 1e0b2cf | 2008-10-30 01:36:48 +0100 | [diff] [blame] | 3613 | pr_debug("device: '%s': %s\n", dev_name(dev), __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3614 | device_del(dev); |
| 3615 | put_device(dev); |
| 3616 | } |
David Graham White | 86df268 | 2013-07-21 20:41:14 -0400 | [diff] [blame] | 3617 | EXPORT_SYMBOL_GPL(device_unregister); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3618 | |
Andy Shevchenko | 3d060ae | 2015-07-27 18:04:00 +0300 | [diff] [blame] | 3619 | static struct device *prev_device(struct klist_iter *i) |
| 3620 | { |
| 3621 | struct klist_node *n = klist_prev(i); |
| 3622 | struct device *dev = NULL; |
| 3623 | struct device_private *p; |
| 3624 | |
| 3625 | if (n) { |
| 3626 | p = to_device_private_parent(n); |
| 3627 | dev = p->device; |
| 3628 | } |
| 3629 | return dev; |
| 3630 | } |
| 3631 | |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 3632 | static struct device *next_device(struct klist_iter *i) |
mochel@digitalimplant.org | 3623957 | 2005-03-24 19:08:30 -0800 | [diff] [blame] | 3633 | { |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 3634 | struct klist_node *n = klist_next(i); |
Greg Kroah-Hartman | f791b8c | 2008-12-16 12:24:56 -0800 | [diff] [blame] | 3635 | struct device *dev = NULL; |
| 3636 | struct device_private *p; |
| 3637 | |
| 3638 | if (n) { |
| 3639 | p = to_device_private_parent(n); |
| 3640 | dev = p->device; |
| 3641 | } |
| 3642 | return dev; |
mochel@digitalimplant.org | 3623957 | 2005-03-24 19:08:30 -0800 | [diff] [blame] | 3643 | } |
| 3644 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3645 | /** |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 3646 | * device_get_devnode - path of device node file |
Kay Sievers | 6fcf53a | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 3647 | * @dev: device |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 3648 | * @mode: returned file access mode |
Kay Sievers | 3c2670e | 2013-04-06 09:56:00 -0700 | [diff] [blame] | 3649 | * @uid: returned file owner |
| 3650 | * @gid: returned file group |
Kay Sievers | 6fcf53a | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 3651 | * @tmp: possibly allocated string |
| 3652 | * |
| 3653 | * Return the relative path of a possible device node. |
| 3654 | * Non-default names may need to allocate a memory to compose |
| 3655 | * a name. This memory is returned in tmp and needs to be |
| 3656 | * freed by the caller. |
| 3657 | */ |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 3658 | const char *device_get_devnode(struct device *dev, |
Greg Kroah-Hartman | 4e4098a | 2013-04-11 11:43:29 -0700 | [diff] [blame] | 3659 | umode_t *mode, kuid_t *uid, kgid_t *gid, |
Kay Sievers | 3c2670e | 2013-04-06 09:56:00 -0700 | [diff] [blame] | 3660 | const char **tmp) |
Kay Sievers | 6fcf53a | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 3661 | { |
| 3662 | char *s; |
| 3663 | |
| 3664 | *tmp = NULL; |
| 3665 | |
| 3666 | /* the device type may provide a specific name */ |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 3667 | if (dev->type && dev->type->devnode) |
Kay Sievers | 3c2670e | 2013-04-06 09:56:00 -0700 | [diff] [blame] | 3668 | *tmp = dev->type->devnode(dev, mode, uid, gid); |
Kay Sievers | 6fcf53a | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 3669 | if (*tmp) |
| 3670 | return *tmp; |
| 3671 | |
| 3672 | /* the class may provide a specific name */ |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 3673 | if (dev->class && dev->class->devnode) |
| 3674 | *tmp = dev->class->devnode(dev, mode); |
Kay Sievers | 6fcf53a | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 3675 | if (*tmp) |
| 3676 | return *tmp; |
| 3677 | |
| 3678 | /* return name without allocation, tmp == NULL */ |
| 3679 | if (strchr(dev_name(dev), '!') == NULL) |
| 3680 | return dev_name(dev); |
| 3681 | |
| 3682 | /* replace '!' in the name with '/' */ |
Rasmus Villemoes | a29fd61 | 2015-06-25 15:02:33 -0700 | [diff] [blame] | 3683 | s = kstrdup(dev_name(dev), GFP_KERNEL); |
| 3684 | if (!s) |
Kay Sievers | 6fcf53a | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 3685 | return NULL; |
Rasmus Villemoes | a29fd61 | 2015-06-25 15:02:33 -0700 | [diff] [blame] | 3686 | strreplace(s, '!', '/'); |
| 3687 | return *tmp = s; |
Kay Sievers | 6fcf53a | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 3688 | } |
| 3689 | |
| 3690 | /** |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 3691 | * device_for_each_child - device child iterator. |
| 3692 | * @parent: parent struct device. |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 3693 | * @fn: function to be called for each device. |
Robert P. J. Day | f8878dc | 2013-06-01 20:17:34 -0400 | [diff] [blame] | 3694 | * @data: data for the callback. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3695 | * |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 3696 | * Iterate over @parent's child devices, and call @fn for each, |
| 3697 | * passing it @data. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3698 | * |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 3699 | * We check the return of @fn each time. If it returns anything |
| 3700 | * other than 0, we break out and return that value. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3701 | */ |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 3702 | int device_for_each_child(struct device *parent, void *data, |
| 3703 | int (*fn)(struct device *dev, void *data)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3704 | { |
mochel@digitalimplant.org | 3623957 | 2005-03-24 19:08:30 -0800 | [diff] [blame] | 3705 | struct klist_iter i; |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 3706 | struct device *child; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3707 | int error = 0; |
| 3708 | |
Greg Kroah-Hartman | 014c90db | 2009-04-15 16:00:12 -0700 | [diff] [blame] | 3709 | if (!parent->p) |
| 3710 | return 0; |
| 3711 | |
Greg Kroah-Hartman | f791b8c | 2008-12-16 12:24:56 -0800 | [diff] [blame] | 3712 | klist_iter_init(&parent->p->klist_children, &i); |
Gimcuan Hui | 93ead7c | 2017-11-11 05:52:54 +0000 | [diff] [blame] | 3713 | while (!error && (child = next_device(&i))) |
mochel@digitalimplant.org | 3623957 | 2005-03-24 19:08:30 -0800 | [diff] [blame] | 3714 | error = fn(child, data); |
| 3715 | klist_iter_exit(&i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3716 | return error; |
| 3717 | } |
David Graham White | 86df268 | 2013-07-21 20:41:14 -0400 | [diff] [blame] | 3718 | EXPORT_SYMBOL_GPL(device_for_each_child); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3719 | |
Cornelia Huck | 5ab6998 | 2006-11-16 15:42:07 +0100 | [diff] [blame] | 3720 | /** |
Andy Shevchenko | 3d060ae | 2015-07-27 18:04:00 +0300 | [diff] [blame] | 3721 | * device_for_each_child_reverse - device child iterator in reversed order. |
| 3722 | * @parent: parent struct device. |
| 3723 | * @fn: function to be called for each device. |
| 3724 | * @data: data for the callback. |
| 3725 | * |
| 3726 | * Iterate over @parent's child devices, and call @fn for each, |
| 3727 | * passing it @data. |
| 3728 | * |
| 3729 | * We check the return of @fn each time. If it returns anything |
| 3730 | * other than 0, we break out and return that value. |
| 3731 | */ |
| 3732 | int device_for_each_child_reverse(struct device *parent, void *data, |
| 3733 | int (*fn)(struct device *dev, void *data)) |
| 3734 | { |
| 3735 | struct klist_iter i; |
| 3736 | struct device *child; |
| 3737 | int error = 0; |
| 3738 | |
| 3739 | if (!parent->p) |
| 3740 | return 0; |
| 3741 | |
| 3742 | klist_iter_init(&parent->p->klist_children, &i); |
| 3743 | while ((child = prev_device(&i)) && !error) |
| 3744 | error = fn(child, data); |
| 3745 | klist_iter_exit(&i); |
| 3746 | return error; |
| 3747 | } |
| 3748 | EXPORT_SYMBOL_GPL(device_for_each_child_reverse); |
| 3749 | |
| 3750 | /** |
Cornelia Huck | 5ab6998 | 2006-11-16 15:42:07 +0100 | [diff] [blame] | 3751 | * device_find_child - device iterator for locating a particular device. |
| 3752 | * @parent: parent struct device |
Cornelia Huck | 5ab6998 | 2006-11-16 15:42:07 +0100 | [diff] [blame] | 3753 | * @match: Callback function to check device |
Robert P. J. Day | f8878dc | 2013-06-01 20:17:34 -0400 | [diff] [blame] | 3754 | * @data: Data to pass to match function |
Cornelia Huck | 5ab6998 | 2006-11-16 15:42:07 +0100 | [diff] [blame] | 3755 | * |
| 3756 | * This is similar to the device_for_each_child() function above, but it |
| 3757 | * returns a reference to a device that is 'found' for later use, as |
| 3758 | * determined by the @match callback. |
| 3759 | * |
| 3760 | * The callback should return 0 if the device doesn't match and non-zero |
| 3761 | * if it does. If the callback returns non-zero and a reference to the |
| 3762 | * current device can be obtained, this function will return to the caller |
| 3763 | * and not iterate over any more devices. |
Federico Vaga | a4e2400 | 2013-04-15 11:18:11 +0200 | [diff] [blame] | 3764 | * |
| 3765 | * 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] | 3766 | */ |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 3767 | struct device *device_find_child(struct device *parent, void *data, |
| 3768 | int (*match)(struct device *dev, void *data)) |
Cornelia Huck | 5ab6998 | 2006-11-16 15:42:07 +0100 | [diff] [blame] | 3769 | { |
| 3770 | struct klist_iter i; |
| 3771 | struct device *child; |
| 3772 | |
| 3773 | if (!parent) |
| 3774 | return NULL; |
| 3775 | |
Greg Kroah-Hartman | f791b8c | 2008-12-16 12:24:56 -0800 | [diff] [blame] | 3776 | klist_iter_init(&parent->p->klist_children, &i); |
Cornelia Huck | 5ab6998 | 2006-11-16 15:42:07 +0100 | [diff] [blame] | 3777 | while ((child = next_device(&i))) |
| 3778 | if (match(child, data) && get_device(child)) |
| 3779 | break; |
| 3780 | klist_iter_exit(&i); |
| 3781 | return child; |
| 3782 | } |
David Graham White | 86df268 | 2013-07-21 20:41:14 -0400 | [diff] [blame] | 3783 | EXPORT_SYMBOL_GPL(device_find_child); |
Cornelia Huck | 5ab6998 | 2006-11-16 15:42:07 +0100 | [diff] [blame] | 3784 | |
Heikki Krogerus | dad9bb0 | 2019-05-31 17:15:37 +0300 | [diff] [blame] | 3785 | /** |
| 3786 | * device_find_child_by_name - device iterator for locating a child device. |
| 3787 | * @parent: parent struct device |
| 3788 | * @name: name of the child device |
| 3789 | * |
| 3790 | * This is similar to the device_find_child() function above, but it |
| 3791 | * returns a reference to a device that has the name @name. |
| 3792 | * |
| 3793 | * NOTE: you will need to drop the reference with put_device() after use. |
| 3794 | */ |
| 3795 | struct device *device_find_child_by_name(struct device *parent, |
| 3796 | const char *name) |
| 3797 | { |
| 3798 | struct klist_iter i; |
| 3799 | struct device *child; |
| 3800 | |
| 3801 | if (!parent) |
| 3802 | return NULL; |
| 3803 | |
| 3804 | klist_iter_init(&parent->p->klist_children, &i); |
| 3805 | while ((child = next_device(&i))) |
Dan Williams | c77f520 | 2020-10-13 16:50:18 -0700 | [diff] [blame] | 3806 | if (sysfs_streq(dev_name(child), name) && get_device(child)) |
Heikki Krogerus | dad9bb0 | 2019-05-31 17:15:37 +0300 | [diff] [blame] | 3807 | break; |
| 3808 | klist_iter_exit(&i); |
| 3809 | return child; |
| 3810 | } |
| 3811 | EXPORT_SYMBOL_GPL(device_find_child_by_name); |
| 3812 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3813 | int __init devices_init(void) |
| 3814 | { |
Greg Kroah-Hartman | 881c6cfd | 2007-11-01 09:29:06 -0600 | [diff] [blame] | 3815 | devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL); |
| 3816 | if (!devices_kset) |
| 3817 | return -ENOMEM; |
Dan Williams | e105b8b | 2008-04-21 10:51:07 -0700 | [diff] [blame] | 3818 | dev_kobj = kobject_create_and_add("dev", NULL); |
| 3819 | if (!dev_kobj) |
| 3820 | goto dev_kobj_err; |
| 3821 | sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj); |
| 3822 | if (!sysfs_dev_block_kobj) |
| 3823 | goto block_kobj_err; |
| 3824 | sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj); |
| 3825 | if (!sysfs_dev_char_kobj) |
| 3826 | goto char_kobj_err; |
| 3827 | |
Greg Kroah-Hartman | 881c6cfd | 2007-11-01 09:29:06 -0600 | [diff] [blame] | 3828 | return 0; |
Dan Williams | e105b8b | 2008-04-21 10:51:07 -0700 | [diff] [blame] | 3829 | |
| 3830 | char_kobj_err: |
| 3831 | kobject_put(sysfs_dev_block_kobj); |
| 3832 | block_kobj_err: |
| 3833 | kobject_put(dev_kobj); |
| 3834 | dev_kobj_err: |
| 3835 | kset_unregister(devices_kset); |
| 3836 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3837 | } |
| 3838 | |
Rafael J. Wysocki | 4f3549d | 2013-05-02 22:15:29 +0200 | [diff] [blame] | 3839 | static int device_check_offline(struct device *dev, void *not_used) |
| 3840 | { |
| 3841 | int ret; |
| 3842 | |
| 3843 | ret = device_for_each_child(dev, NULL, device_check_offline); |
| 3844 | if (ret) |
| 3845 | return ret; |
| 3846 | |
| 3847 | return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0; |
| 3848 | } |
| 3849 | |
| 3850 | /** |
| 3851 | * device_offline - Prepare the device for hot-removal. |
| 3852 | * @dev: Device to be put offline. |
| 3853 | * |
| 3854 | * Execute the device bus type's .offline() callback, if present, to prepare |
| 3855 | * the device for a subsequent hot-removal. If that succeeds, the device must |
| 3856 | * not be used until either it is removed or its bus type's .online() callback |
| 3857 | * is executed. |
| 3858 | * |
| 3859 | * Call under device_hotplug_lock. |
| 3860 | */ |
| 3861 | int device_offline(struct device *dev) |
| 3862 | { |
| 3863 | int ret; |
| 3864 | |
| 3865 | if (dev->offline_disabled) |
| 3866 | return -EPERM; |
| 3867 | |
| 3868 | ret = device_for_each_child(dev, NULL, device_check_offline); |
| 3869 | if (ret) |
| 3870 | return ret; |
| 3871 | |
| 3872 | device_lock(dev); |
| 3873 | if (device_supports_offline(dev)) { |
| 3874 | if (dev->offline) { |
| 3875 | ret = 1; |
| 3876 | } else { |
| 3877 | ret = dev->bus->offline(dev); |
| 3878 | if (!ret) { |
| 3879 | kobject_uevent(&dev->kobj, KOBJ_OFFLINE); |
| 3880 | dev->offline = true; |
| 3881 | } |
| 3882 | } |
| 3883 | } |
| 3884 | device_unlock(dev); |
| 3885 | |
| 3886 | return ret; |
| 3887 | } |
| 3888 | |
| 3889 | /** |
| 3890 | * device_online - Put the device back online after successful device_offline(). |
| 3891 | * @dev: Device to be put back online. |
| 3892 | * |
| 3893 | * If device_offline() has been successfully executed for @dev, but the device |
| 3894 | * has not been removed subsequently, execute its bus type's .online() callback |
| 3895 | * to indicate that the device can be used again. |
| 3896 | * |
| 3897 | * Call under device_hotplug_lock. |
| 3898 | */ |
| 3899 | int device_online(struct device *dev) |
| 3900 | { |
| 3901 | int ret = 0; |
| 3902 | |
| 3903 | device_lock(dev); |
| 3904 | if (device_supports_offline(dev)) { |
| 3905 | if (dev->offline) { |
| 3906 | ret = dev->bus->online(dev); |
| 3907 | if (!ret) { |
| 3908 | kobject_uevent(&dev->kobj, KOBJ_ONLINE); |
| 3909 | dev->offline = false; |
| 3910 | } |
| 3911 | } else { |
| 3912 | ret = 1; |
| 3913 | } |
| 3914 | } |
| 3915 | device_unlock(dev); |
| 3916 | |
| 3917 | return ret; |
| 3918 | } |
| 3919 | |
Karthigan Srinivasan | 7f100d1 | 2011-04-18 16:16:52 -0500 | [diff] [blame] | 3920 | struct root_device { |
Mark McLoughlin | 0aa0dc4 | 2008-12-15 12:58:26 +0000 | [diff] [blame] | 3921 | struct device dev; |
| 3922 | struct module *owner; |
| 3923 | }; |
| 3924 | |
Josh Triplett | 9305842 | 2012-11-18 21:27:55 -0800 | [diff] [blame] | 3925 | static inline struct root_device *to_root_device(struct device *d) |
Ferenc Wagner | 481e207 | 2011-01-07 15:17:47 +0100 | [diff] [blame] | 3926 | { |
| 3927 | return container_of(d, struct root_device, dev); |
| 3928 | } |
Mark McLoughlin | 0aa0dc4 | 2008-12-15 12:58:26 +0000 | [diff] [blame] | 3929 | |
| 3930 | static void root_device_release(struct device *dev) |
| 3931 | { |
| 3932 | kfree(to_root_device(dev)); |
| 3933 | } |
| 3934 | |
| 3935 | /** |
| 3936 | * __root_device_register - allocate and register a root device |
| 3937 | * @name: root device name |
| 3938 | * @owner: owner module of the root device, usually THIS_MODULE |
| 3939 | * |
| 3940 | * This function allocates a root device and registers it |
| 3941 | * using device_register(). In order to free the returned |
| 3942 | * device, use root_device_unregister(). |
| 3943 | * |
| 3944 | * Root devices are dummy devices which allow other devices |
| 3945 | * to be grouped under /sys/devices. Use this function to |
| 3946 | * allocate a root device and then use it as the parent of |
| 3947 | * any device which should appear under /sys/devices/{name} |
| 3948 | * |
| 3949 | * The /sys/devices/{name} directory will also contain a |
| 3950 | * 'module' symlink which points to the @owner directory |
| 3951 | * in sysfs. |
| 3952 | * |
Jani Nikula | f0eae0e | 2010-03-11 18:11:45 +0200 | [diff] [blame] | 3953 | * Returns &struct device pointer on success, or ERR_PTR() on error. |
| 3954 | * |
Mark McLoughlin | 0aa0dc4 | 2008-12-15 12:58:26 +0000 | [diff] [blame] | 3955 | * Note: You probably want to use root_device_register(). |
| 3956 | */ |
| 3957 | struct device *__root_device_register(const char *name, struct module *owner) |
| 3958 | { |
| 3959 | struct root_device *root; |
| 3960 | int err = -ENOMEM; |
| 3961 | |
| 3962 | root = kzalloc(sizeof(struct root_device), GFP_KERNEL); |
| 3963 | if (!root) |
| 3964 | return ERR_PTR(err); |
| 3965 | |
Greg Kroah-Hartman | acc0e90 | 2009-06-02 15:39:55 -0700 | [diff] [blame] | 3966 | err = dev_set_name(&root->dev, "%s", name); |
Mark McLoughlin | 0aa0dc4 | 2008-12-15 12:58:26 +0000 | [diff] [blame] | 3967 | if (err) { |
| 3968 | kfree(root); |
| 3969 | return ERR_PTR(err); |
| 3970 | } |
| 3971 | |
| 3972 | root->dev.release = root_device_release; |
| 3973 | |
| 3974 | err = device_register(&root->dev); |
| 3975 | if (err) { |
| 3976 | put_device(&root->dev); |
| 3977 | return ERR_PTR(err); |
| 3978 | } |
| 3979 | |
Christoph Egger | 1d9e882 | 2010-05-17 16:57:58 +0200 | [diff] [blame] | 3980 | #ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */ |
Mark McLoughlin | 0aa0dc4 | 2008-12-15 12:58:26 +0000 | [diff] [blame] | 3981 | if (owner) { |
| 3982 | struct module_kobject *mk = &owner->mkobj; |
| 3983 | |
| 3984 | err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module"); |
| 3985 | if (err) { |
| 3986 | device_unregister(&root->dev); |
| 3987 | return ERR_PTR(err); |
| 3988 | } |
| 3989 | root->owner = owner; |
| 3990 | } |
| 3991 | #endif |
| 3992 | |
| 3993 | return &root->dev; |
| 3994 | } |
| 3995 | EXPORT_SYMBOL_GPL(__root_device_register); |
| 3996 | |
| 3997 | /** |
| 3998 | * root_device_unregister - unregister and free a root device |
Randy Dunlap | 7cbcf22 | 2009-01-20 16:29:13 -0800 | [diff] [blame] | 3999 | * @dev: device going away |
Mark McLoughlin | 0aa0dc4 | 2008-12-15 12:58:26 +0000 | [diff] [blame] | 4000 | * |
| 4001 | * This function unregisters and cleans up a device that was created by |
| 4002 | * root_device_register(). |
| 4003 | */ |
| 4004 | void root_device_unregister(struct device *dev) |
| 4005 | { |
| 4006 | struct root_device *root = to_root_device(dev); |
| 4007 | |
| 4008 | if (root->owner) |
| 4009 | sysfs_remove_link(&root->dev.kobj, "module"); |
| 4010 | |
| 4011 | device_unregister(dev); |
| 4012 | } |
| 4013 | EXPORT_SYMBOL_GPL(root_device_unregister); |
| 4014 | |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 4015 | |
| 4016 | static void device_create_release(struct device *dev) |
| 4017 | { |
Kay Sievers | 1e0b2cf | 2008-10-30 01:36:48 +0100 | [diff] [blame] | 4018 | pr_debug("device: '%s': %s\n", dev_name(dev), __func__); |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 4019 | kfree(dev); |
| 4020 | } |
| 4021 | |
Mathieu Malaterre | 6a8b55d | 2018-05-05 21:57:41 +0200 | [diff] [blame] | 4022 | static __printf(6, 0) struct device * |
Guenter Roeck | 39ef311 | 2013-07-14 16:05:57 -0700 | [diff] [blame] | 4023 | device_create_groups_vargs(struct class *class, struct device *parent, |
| 4024 | dev_t devt, void *drvdata, |
| 4025 | const struct attribute_group **groups, |
| 4026 | const char *fmt, va_list args) |
| 4027 | { |
| 4028 | struct device *dev = NULL; |
| 4029 | int retval = -ENODEV; |
| 4030 | |
| 4031 | if (class == NULL || IS_ERR(class)) |
| 4032 | goto error; |
| 4033 | |
| 4034 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
| 4035 | if (!dev) { |
| 4036 | retval = -ENOMEM; |
| 4037 | goto error; |
| 4038 | } |
| 4039 | |
David Herrmann | bbc780f | 2013-11-21 20:15:48 +0100 | [diff] [blame] | 4040 | device_initialize(dev); |
Guenter Roeck | 39ef311 | 2013-07-14 16:05:57 -0700 | [diff] [blame] | 4041 | dev->devt = devt; |
| 4042 | dev->class = class; |
| 4043 | dev->parent = parent; |
| 4044 | dev->groups = groups; |
| 4045 | dev->release = device_create_release; |
| 4046 | dev_set_drvdata(dev, drvdata); |
| 4047 | |
| 4048 | retval = kobject_set_name_vargs(&dev->kobj, fmt, args); |
| 4049 | if (retval) |
| 4050 | goto error; |
| 4051 | |
David Herrmann | bbc780f | 2013-11-21 20:15:48 +0100 | [diff] [blame] | 4052 | retval = device_add(dev); |
Guenter Roeck | 39ef311 | 2013-07-14 16:05:57 -0700 | [diff] [blame] | 4053 | if (retval) |
| 4054 | goto error; |
| 4055 | |
| 4056 | return dev; |
| 4057 | |
| 4058 | error: |
| 4059 | put_device(dev); |
| 4060 | return ERR_PTR(retval); |
| 4061 | } |
| 4062 | |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 4063 | /** |
Greg Kroah-Hartman | 4e10673 | 2008-07-21 20:03:34 -0700 | [diff] [blame] | 4064 | * device_create - creates a device and registers it with sysfs |
Greg Kroah-Hartman | 8882b394 | 2008-05-15 13:44:08 -0700 | [diff] [blame] | 4065 | * @class: pointer to the struct class that this device should be registered to |
| 4066 | * @parent: pointer to the parent struct device of this new device, if any |
| 4067 | * @devt: the dev_t for the char device to be added |
| 4068 | * @drvdata: the data to be added to the device for callbacks |
| 4069 | * @fmt: string for the device's name |
| 4070 | * |
| 4071 | * This function can be used by char device classes. A struct device |
| 4072 | * will be created in sysfs, registered to the specified class. |
| 4073 | * |
| 4074 | * A "dev" file will be created, showing the dev_t for the device, if |
| 4075 | * the dev_t is not 0,0. |
| 4076 | * If a pointer to a parent struct device is passed in, the newly created |
| 4077 | * struct device will be a child of that device in sysfs. |
| 4078 | * The pointer to the struct device will be returned from the call. |
| 4079 | * Any further sysfs files that might be required can be created using this |
| 4080 | * pointer. |
| 4081 | * |
Jani Nikula | f0eae0e | 2010-03-11 18:11:45 +0200 | [diff] [blame] | 4082 | * Returns &struct device pointer on success, or ERR_PTR() on error. |
| 4083 | * |
Greg Kroah-Hartman | 8882b394 | 2008-05-15 13:44:08 -0700 | [diff] [blame] | 4084 | * Note: the struct class passed to this function must have previously |
| 4085 | * been created with a call to class_create(). |
| 4086 | */ |
Greg Kroah-Hartman | 4e10673 | 2008-07-21 20:03:34 -0700 | [diff] [blame] | 4087 | struct device *device_create(struct class *class, struct device *parent, |
| 4088 | dev_t devt, void *drvdata, const char *fmt, ...) |
Greg Kroah-Hartman | 8882b394 | 2008-05-15 13:44:08 -0700 | [diff] [blame] | 4089 | { |
| 4090 | va_list vargs; |
| 4091 | struct device *dev; |
| 4092 | |
| 4093 | va_start(vargs, fmt); |
Christoph Hellwig | 4c74746 | 2020-05-04 14:47:57 +0200 | [diff] [blame] | 4094 | dev = device_create_groups_vargs(class, parent, devt, drvdata, NULL, |
| 4095 | fmt, vargs); |
Greg Kroah-Hartman | 8882b394 | 2008-05-15 13:44:08 -0700 | [diff] [blame] | 4096 | va_end(vargs); |
| 4097 | return dev; |
| 4098 | } |
Greg Kroah-Hartman | 4e10673 | 2008-07-21 20:03:34 -0700 | [diff] [blame] | 4099 | EXPORT_SYMBOL_GPL(device_create); |
Greg Kroah-Hartman | 8882b394 | 2008-05-15 13:44:08 -0700 | [diff] [blame] | 4100 | |
Guenter Roeck | 39ef311 | 2013-07-14 16:05:57 -0700 | [diff] [blame] | 4101 | /** |
| 4102 | * device_create_with_groups - creates a device and registers it with sysfs |
| 4103 | * @class: pointer to the struct class that this device should be registered to |
| 4104 | * @parent: pointer to the parent struct device of this new device, if any |
| 4105 | * @devt: the dev_t for the char device to be added |
| 4106 | * @drvdata: the data to be added to the device for callbacks |
| 4107 | * @groups: NULL-terminated list of attribute groups to be created |
| 4108 | * @fmt: string for the device's name |
| 4109 | * |
| 4110 | * This function can be used by char device classes. A struct device |
| 4111 | * will be created in sysfs, registered to the specified class. |
| 4112 | * Additional attributes specified in the groups parameter will also |
| 4113 | * be created automatically. |
| 4114 | * |
| 4115 | * A "dev" file will be created, showing the dev_t for the device, if |
| 4116 | * the dev_t is not 0,0. |
| 4117 | * If a pointer to a parent struct device is passed in, the newly created |
| 4118 | * struct device will be a child of that device in sysfs. |
| 4119 | * The pointer to the struct device will be returned from the call. |
| 4120 | * Any further sysfs files that might be required can be created using this |
| 4121 | * pointer. |
| 4122 | * |
| 4123 | * Returns &struct device pointer on success, or ERR_PTR() on error. |
| 4124 | * |
| 4125 | * Note: the struct class passed to this function must have previously |
| 4126 | * been created with a call to class_create(). |
| 4127 | */ |
| 4128 | struct device *device_create_with_groups(struct class *class, |
| 4129 | struct device *parent, dev_t devt, |
| 4130 | void *drvdata, |
| 4131 | const struct attribute_group **groups, |
| 4132 | const char *fmt, ...) |
| 4133 | { |
| 4134 | va_list vargs; |
| 4135 | struct device *dev; |
| 4136 | |
| 4137 | va_start(vargs, fmt); |
| 4138 | dev = device_create_groups_vargs(class, parent, devt, drvdata, groups, |
| 4139 | fmt, vargs); |
| 4140 | va_end(vargs); |
| 4141 | return dev; |
| 4142 | } |
| 4143 | EXPORT_SYMBOL_GPL(device_create_with_groups); |
| 4144 | |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 4145 | /** |
| 4146 | * device_destroy - removes a device that was created with device_create() |
| 4147 | * @class: pointer to the struct class that this device was registered with |
| 4148 | * @devt: the dev_t of the device that was previously registered |
| 4149 | * |
| 4150 | * This call unregisters and cleans up a device that was created with a |
| 4151 | * call to device_create(). |
| 4152 | */ |
| 4153 | void device_destroy(struct class *class, dev_t devt) |
| 4154 | { |
| 4155 | struct device *dev; |
| 4156 | |
Suzuki K Poulose | 4495dfd | 2019-07-23 23:18:35 +0100 | [diff] [blame] | 4157 | dev = class_find_device_by_devt(class, devt); |
Dave Young | cd35449 | 2008-01-28 16:56:11 +0800 | [diff] [blame] | 4158 | if (dev) { |
| 4159 | put_device(dev); |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 4160 | device_unregister(dev); |
Dave Young | cd35449 | 2008-01-28 16:56:11 +0800 | [diff] [blame] | 4161 | } |
Greg Kroah-Hartman | 23681e4 | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 4162 | } |
| 4163 | EXPORT_SYMBOL_GPL(device_destroy); |
Greg Kroah-Hartman | a2de48c | 2006-07-03 14:31:12 -0700 | [diff] [blame] | 4164 | |
| 4165 | /** |
| 4166 | * device_rename - renames a device |
| 4167 | * @dev: the pointer to the struct device to be renamed |
| 4168 | * @new_name: the new name of the device |
Eric W. Biederman | 030c1d2 | 2008-05-08 14:41:00 -0700 | [diff] [blame] | 4169 | * |
| 4170 | * It is the responsibility of the caller to provide mutual |
| 4171 | * exclusion between two different calls of device_rename |
| 4172 | * on the same device to ensure that new_name is valid and |
| 4173 | * won't conflict with other devices. |
Michael Ellerman | c6c0ac6 | 2010-11-25 09:44:07 +1100 | [diff] [blame] | 4174 | * |
Timur Tabi | a546251 | 2010-12-13 14:08:52 -0600 | [diff] [blame] | 4175 | * Note: Don't call this function. Currently, the networking layer calls this |
| 4176 | * function, but that will change. The following text from Kay Sievers offers |
| 4177 | * some insight: |
| 4178 | * |
| 4179 | * Renaming devices is racy at many levels, symlinks and other stuff are not |
| 4180 | * replaced atomically, and you get a "move" uevent, but it's not easy to |
| 4181 | * connect the event to the old and new device. Device nodes are not renamed at |
| 4182 | * all, there isn't even support for that in the kernel now. |
| 4183 | * |
| 4184 | * In the meantime, during renaming, your target name might be taken by another |
| 4185 | * driver, creating conflicts. Or the old name is taken directly after you |
| 4186 | * renamed it -- then you get events for the same DEVPATH, before you even see |
| 4187 | * the "move" event. It's just a mess, and nothing new should ever rely on |
| 4188 | * kernel device renaming. Besides that, it's not even implemented now for |
| 4189 | * other things than (driver-core wise very simple) network devices. |
| 4190 | * |
| 4191 | * We are currently about to change network renaming in udev to completely |
| 4192 | * disallow renaming of devices in the same namespace as the kernel uses, |
| 4193 | * because we can't solve the problems properly, that arise with swapping names |
| 4194 | * of multiple interfaces without races. Means, renaming of eth[0-9]* will only |
| 4195 | * be allowed to some other name than eth[0-9]*, for the aforementioned |
| 4196 | * reasons. |
| 4197 | * |
| 4198 | * Make up a "real" name in the driver before you register anything, or add |
| 4199 | * some other attributes for userspace to find the device, or use udev to add |
| 4200 | * symlinks -- but never rename kernel devices later, it's a complete mess. We |
| 4201 | * don't even want to get into that and try to implement the missing pieces in |
| 4202 | * 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] | 4203 | */ |
Johannes Berg | 6937e8f | 2010-08-05 17:38:18 +0200 | [diff] [blame] | 4204 | int device_rename(struct device *dev, const char *new_name) |
Greg Kroah-Hartman | a2de48c | 2006-07-03 14:31:12 -0700 | [diff] [blame] | 4205 | { |
Tejun Heo | 4b30ee5 | 2013-09-11 22:29:06 -0400 | [diff] [blame] | 4206 | struct kobject *kobj = &dev->kobj; |
Cornelia Huck | 2ee97ca | 2007-07-18 01:43:47 -0700 | [diff] [blame] | 4207 | char *old_device_name = NULL; |
Greg Kroah-Hartman | a2de48c | 2006-07-03 14:31:12 -0700 | [diff] [blame] | 4208 | int error; |
| 4209 | |
| 4210 | dev = get_device(dev); |
| 4211 | if (!dev) |
| 4212 | return -EINVAL; |
| 4213 | |
ethan.zhao | 69df753 | 2013-10-13 22:12:35 +0800 | [diff] [blame] | 4214 | dev_dbg(dev, "renaming to %s\n", new_name); |
Greg Kroah-Hartman | a2de48c | 2006-07-03 14:31:12 -0700 | [diff] [blame] | 4215 | |
Kay Sievers | 1fa5ae8 | 2009-01-25 15:17:37 +0100 | [diff] [blame] | 4216 | old_device_name = kstrdup(dev_name(dev), GFP_KERNEL); |
Cornelia Huck | 2ee97ca | 2007-07-18 01:43:47 -0700 | [diff] [blame] | 4217 | if (!old_device_name) { |
| 4218 | error = -ENOMEM; |
| 4219 | goto out; |
Greg Kroah-Hartman | a2de48c | 2006-07-03 14:31:12 -0700 | [diff] [blame] | 4220 | } |
Greg Kroah-Hartman | a2de48c | 2006-07-03 14:31:12 -0700 | [diff] [blame] | 4221 | |
Eric W. Biederman | f349cf3 | 2010-03-30 11:31:29 -0700 | [diff] [blame] | 4222 | if (dev->class) { |
Tejun Heo | 4b30ee5 | 2013-09-11 22:29:06 -0400 | [diff] [blame] | 4223 | error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj, |
| 4224 | kobj, old_device_name, |
| 4225 | new_name, kobject_namespace(kobj)); |
Eric W. Biederman | f349cf3 | 2010-03-30 11:31:29 -0700 | [diff] [blame] | 4226 | if (error) |
| 4227 | goto out; |
| 4228 | } |
Kay Sievers | 39aba96 | 2010-09-04 22:33:14 -0700 | [diff] [blame] | 4229 | |
Tejun Heo | 4b30ee5 | 2013-09-11 22:29:06 -0400 | [diff] [blame] | 4230 | error = kobject_rename(kobj, new_name); |
Kay Sievers | 1fa5ae8 | 2009-01-25 15:17:37 +0100 | [diff] [blame] | 4231 | if (error) |
Cornelia Huck | 2ee97ca | 2007-07-18 01:43:47 -0700 | [diff] [blame] | 4232 | goto out; |
Greg Kroah-Hartman | a2de48c | 2006-07-03 14:31:12 -0700 | [diff] [blame] | 4233 | |
Cornelia Huck | 2ee97ca | 2007-07-18 01:43:47 -0700 | [diff] [blame] | 4234 | out: |
Greg Kroah-Hartman | a2de48c | 2006-07-03 14:31:12 -0700 | [diff] [blame] | 4235 | put_device(dev); |
| 4236 | |
Cornelia Huck | 2ee97ca | 2007-07-18 01:43:47 -0700 | [diff] [blame] | 4237 | kfree(old_device_name); |
Greg Kroah-Hartman | a2de48c | 2006-07-03 14:31:12 -0700 | [diff] [blame] | 4238 | |
| 4239 | return error; |
| 4240 | } |
Johannes Berg | a2807db | 2007-02-28 12:38:31 +0100 | [diff] [blame] | 4241 | EXPORT_SYMBOL_GPL(device_rename); |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 4242 | |
| 4243 | static int device_move_class_links(struct device *dev, |
| 4244 | struct device *old_parent, |
| 4245 | struct device *new_parent) |
| 4246 | { |
Greg Kroah-Hartman | f7f3461 | 2007-03-06 12:55:53 -0800 | [diff] [blame] | 4247 | int error = 0; |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 4248 | |
Greg Kroah-Hartman | f7f3461 | 2007-03-06 12:55:53 -0800 | [diff] [blame] | 4249 | if (old_parent) |
| 4250 | sysfs_remove_link(&dev->kobj, "device"); |
| 4251 | if (new_parent) |
| 4252 | error = sysfs_create_link(&dev->kobj, &new_parent->kobj, |
| 4253 | "device"); |
| 4254 | return error; |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 4255 | } |
| 4256 | |
| 4257 | /** |
| 4258 | * device_move - moves a device to a new parent |
| 4259 | * @dev: the pointer to the struct device to be moved |
Wolfram Sang | 1350986 | 2018-05-06 13:23:47 +0200 | [diff] [blame] | 4260 | * @new_parent: the new parent of the device (can be NULL) |
Cornelia Huck | ffa6a70 | 2009-03-04 12:44:00 +0100 | [diff] [blame] | 4261 | * @dpm_order: how to reorder the dpm_list |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 4262 | */ |
Cornelia Huck | ffa6a70 | 2009-03-04 12:44:00 +0100 | [diff] [blame] | 4263 | int device_move(struct device *dev, struct device *new_parent, |
| 4264 | enum dpm_order dpm_order) |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 4265 | { |
| 4266 | int error; |
| 4267 | struct device *old_parent; |
Cornelia Huck | c744aeae | 2007-01-08 20:16:44 +0100 | [diff] [blame] | 4268 | struct kobject *new_parent_kobj; |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 4269 | |
| 4270 | dev = get_device(dev); |
| 4271 | if (!dev) |
| 4272 | return -EINVAL; |
| 4273 | |
Cornelia Huck | ffa6a70 | 2009-03-04 12:44:00 +0100 | [diff] [blame] | 4274 | device_pm_lock(); |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 4275 | new_parent = get_device(new_parent); |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 4276 | new_parent_kobj = get_device_parent(dev, new_parent); |
Tetsuo Handa | 84d0c27 | 2018-05-07 19:10:31 +0900 | [diff] [blame] | 4277 | if (IS_ERR(new_parent_kobj)) { |
| 4278 | error = PTR_ERR(new_parent_kobj); |
| 4279 | put_device(new_parent); |
| 4280 | goto out; |
| 4281 | } |
Cornelia Huck | 63b6971 | 2008-01-21 16:09:44 +0100 | [diff] [blame] | 4282 | |
Kay Sievers | 1e0b2cf | 2008-10-30 01:36:48 +0100 | [diff] [blame] | 4283 | pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev), |
| 4284 | __func__, new_parent ? dev_name(new_parent) : "<NULL>"); |
Cornelia Huck | c744aeae | 2007-01-08 20:16:44 +0100 | [diff] [blame] | 4285 | error = kobject_move(&dev->kobj, new_parent_kobj); |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 4286 | if (error) { |
Cornelia Huck | 63b6971 | 2008-01-21 16:09:44 +0100 | [diff] [blame] | 4287 | cleanup_glue_dir(dev, new_parent_kobj); |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 4288 | put_device(new_parent); |
| 4289 | goto out; |
| 4290 | } |
| 4291 | old_parent = dev->parent; |
| 4292 | dev->parent = new_parent; |
| 4293 | if (old_parent) |
Greg Kroah-Hartman | f791b8c | 2008-12-16 12:24:56 -0800 | [diff] [blame] | 4294 | klist_remove(&dev->p->knode_parent); |
Yinghai Lu | 0d358f2 | 2008-02-19 03:20:41 -0800 | [diff] [blame] | 4295 | if (new_parent) { |
Greg Kroah-Hartman | f791b8c | 2008-12-16 12:24:56 -0800 | [diff] [blame] | 4296 | klist_add_tail(&dev->p->knode_parent, |
| 4297 | &new_parent->p->klist_children); |
Yinghai Lu | 0d358f2 | 2008-02-19 03:20:41 -0800 | [diff] [blame] | 4298 | set_dev_node(dev, dev_to_node(new_parent)); |
| 4299 | } |
| 4300 | |
Rabin Vincent | bdd4034 | 2012-04-23 09:16:36 +0200 | [diff] [blame] | 4301 | if (dev->class) { |
| 4302 | error = device_move_class_links(dev, old_parent, new_parent); |
| 4303 | if (error) { |
| 4304 | /* We ignore errors on cleanup since we're hosed anyway... */ |
| 4305 | device_move_class_links(dev, new_parent, old_parent); |
| 4306 | if (!kobject_move(&dev->kobj, &old_parent->kobj)) { |
| 4307 | if (new_parent) |
| 4308 | klist_remove(&dev->p->knode_parent); |
| 4309 | dev->parent = old_parent; |
| 4310 | if (old_parent) { |
| 4311 | klist_add_tail(&dev->p->knode_parent, |
| 4312 | &old_parent->p->klist_children); |
| 4313 | set_dev_node(dev, dev_to_node(old_parent)); |
| 4314 | } |
Yinghai Lu | 0d358f2 | 2008-02-19 03:20:41 -0800 | [diff] [blame] | 4315 | } |
Rabin Vincent | bdd4034 | 2012-04-23 09:16:36 +0200 | [diff] [blame] | 4316 | cleanup_glue_dir(dev, new_parent_kobj); |
| 4317 | put_device(new_parent); |
| 4318 | goto out; |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 4319 | } |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 4320 | } |
Cornelia Huck | ffa6a70 | 2009-03-04 12:44:00 +0100 | [diff] [blame] | 4321 | switch (dpm_order) { |
| 4322 | case DPM_ORDER_NONE: |
| 4323 | break; |
| 4324 | case DPM_ORDER_DEV_AFTER_PARENT: |
| 4325 | device_pm_move_after(dev, new_parent); |
Grygorii Strashko | 52cdbdd | 2015-07-27 20:43:01 +0300 | [diff] [blame] | 4326 | devices_kset_move_after(dev, new_parent); |
Cornelia Huck | ffa6a70 | 2009-03-04 12:44:00 +0100 | [diff] [blame] | 4327 | break; |
| 4328 | case DPM_ORDER_PARENT_BEFORE_DEV: |
| 4329 | device_pm_move_before(new_parent, dev); |
Grygorii Strashko | 52cdbdd | 2015-07-27 20:43:01 +0300 | [diff] [blame] | 4330 | devices_kset_move_before(new_parent, dev); |
Cornelia Huck | ffa6a70 | 2009-03-04 12:44:00 +0100 | [diff] [blame] | 4331 | break; |
| 4332 | case DPM_ORDER_DEV_LAST: |
| 4333 | device_pm_move_last(dev); |
Grygorii Strashko | 52cdbdd | 2015-07-27 20:43:01 +0300 | [diff] [blame] | 4334 | devices_kset_move_last(dev); |
Cornelia Huck | ffa6a70 | 2009-03-04 12:44:00 +0100 | [diff] [blame] | 4335 | break; |
| 4336 | } |
Rabin Vincent | bdd4034 | 2012-04-23 09:16:36 +0200 | [diff] [blame] | 4337 | |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 4338 | put_device(old_parent); |
| 4339 | out: |
Cornelia Huck | ffa6a70 | 2009-03-04 12:44:00 +0100 | [diff] [blame] | 4340 | device_pm_unlock(); |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 4341 | put_device(dev); |
| 4342 | return error; |
| 4343 | } |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 4344 | EXPORT_SYMBOL_GPL(device_move); |
Greg Kroah-Hartman | 37b0c02 | 2007-11-26 22:11:55 -0800 | [diff] [blame] | 4345 | |
Christian Brauner | b8f33e5 | 2020-02-27 04:37:15 +0100 | [diff] [blame] | 4346 | static int device_attrs_change_owner(struct device *dev, kuid_t kuid, |
| 4347 | kgid_t kgid) |
| 4348 | { |
| 4349 | struct kobject *kobj = &dev->kobj; |
| 4350 | struct class *class = dev->class; |
| 4351 | const struct device_type *type = dev->type; |
| 4352 | int error; |
| 4353 | |
| 4354 | if (class) { |
| 4355 | /* |
| 4356 | * Change the device groups of the device class for @dev to |
| 4357 | * @kuid/@kgid. |
| 4358 | */ |
| 4359 | error = sysfs_groups_change_owner(kobj, class->dev_groups, kuid, |
| 4360 | kgid); |
| 4361 | if (error) |
| 4362 | return error; |
| 4363 | } |
| 4364 | |
| 4365 | if (type) { |
| 4366 | /* |
| 4367 | * Change the device groups of the device type for @dev to |
| 4368 | * @kuid/@kgid. |
| 4369 | */ |
| 4370 | error = sysfs_groups_change_owner(kobj, type->groups, kuid, |
| 4371 | kgid); |
| 4372 | if (error) |
| 4373 | return error; |
| 4374 | } |
| 4375 | |
| 4376 | /* Change the device groups of @dev to @kuid/@kgid. */ |
| 4377 | error = sysfs_groups_change_owner(kobj, dev->groups, kuid, kgid); |
| 4378 | if (error) |
| 4379 | return error; |
| 4380 | |
| 4381 | if (device_supports_offline(dev) && !dev->offline_disabled) { |
| 4382 | /* Change online device attributes of @dev to @kuid/@kgid. */ |
| 4383 | error = sysfs_file_change_owner(kobj, dev_attr_online.attr.name, |
| 4384 | kuid, kgid); |
| 4385 | if (error) |
| 4386 | return error; |
| 4387 | } |
| 4388 | |
| 4389 | return 0; |
| 4390 | } |
| 4391 | |
| 4392 | /** |
| 4393 | * device_change_owner - change the owner of an existing device. |
| 4394 | * @dev: device. |
| 4395 | * @kuid: new owner's kuid |
| 4396 | * @kgid: new owner's kgid |
| 4397 | * |
| 4398 | * This changes the owner of @dev and its corresponding sysfs entries to |
| 4399 | * @kuid/@kgid. This function closely mirrors how @dev was added via driver |
| 4400 | * core. |
| 4401 | * |
| 4402 | * Returns 0 on success or error code on failure. |
| 4403 | */ |
| 4404 | int device_change_owner(struct device *dev, kuid_t kuid, kgid_t kgid) |
| 4405 | { |
| 4406 | int error; |
| 4407 | struct kobject *kobj = &dev->kobj; |
| 4408 | |
| 4409 | dev = get_device(dev); |
| 4410 | if (!dev) |
| 4411 | return -EINVAL; |
| 4412 | |
| 4413 | /* |
| 4414 | * Change the kobject and the default attributes and groups of the |
| 4415 | * ktype associated with it to @kuid/@kgid. |
| 4416 | */ |
| 4417 | error = sysfs_change_owner(kobj, kuid, kgid); |
| 4418 | if (error) |
| 4419 | goto out; |
| 4420 | |
| 4421 | /* |
| 4422 | * Change the uevent file for @dev to the new owner. The uevent file |
| 4423 | * was created in a separate step when @dev got added and we mirror |
| 4424 | * that step here. |
| 4425 | */ |
| 4426 | error = sysfs_file_change_owner(kobj, dev_attr_uevent.attr.name, kuid, |
| 4427 | kgid); |
| 4428 | if (error) |
| 4429 | goto out; |
| 4430 | |
| 4431 | /* |
| 4432 | * Change the device groups, the device groups associated with the |
| 4433 | * device class, and the groups associated with the device type of @dev |
| 4434 | * to @kuid/@kgid. |
| 4435 | */ |
| 4436 | error = device_attrs_change_owner(dev, kuid, kgid); |
| 4437 | if (error) |
| 4438 | goto out; |
| 4439 | |
Christian Brauner | 3b52fc5 | 2020-02-27 04:37:16 +0100 | [diff] [blame] | 4440 | error = dpm_sysfs_change_owner(dev, kuid, kgid); |
| 4441 | if (error) |
| 4442 | goto out; |
| 4443 | |
Christian Brauner | b8f33e5 | 2020-02-27 04:37:15 +0100 | [diff] [blame] | 4444 | #ifdef CONFIG_BLOCK |
| 4445 | if (sysfs_deprecated && dev->class == &block_class) |
| 4446 | goto out; |
| 4447 | #endif |
| 4448 | |
| 4449 | /* |
| 4450 | * Change the owner of the symlink located in the class directory of |
| 4451 | * the device class associated with @dev which points to the actual |
| 4452 | * directory entry for @dev to @kuid/@kgid. This ensures that the |
| 4453 | * symlink shows the same permissions as its target. |
| 4454 | */ |
| 4455 | error = sysfs_link_change_owner(&dev->class->p->subsys.kobj, &dev->kobj, |
| 4456 | dev_name(dev), kuid, kgid); |
| 4457 | if (error) |
| 4458 | goto out; |
| 4459 | |
| 4460 | out: |
| 4461 | put_device(dev); |
| 4462 | return error; |
| 4463 | } |
| 4464 | EXPORT_SYMBOL_GPL(device_change_owner); |
| 4465 | |
Greg Kroah-Hartman | 37b0c02 | 2007-11-26 22:11:55 -0800 | [diff] [blame] | 4466 | /** |
| 4467 | * device_shutdown - call ->shutdown() on each device to shutdown. |
| 4468 | */ |
| 4469 | void device_shutdown(void) |
| 4470 | { |
Benson Leung | f123db8 | 2013-09-24 20:05:11 -0700 | [diff] [blame] | 4471 | struct device *dev, *parent; |
Greg Kroah-Hartman | 37b0c02 | 2007-11-26 22:11:55 -0800 | [diff] [blame] | 4472 | |
Pingfan Liu | 3297c8f | 2018-07-19 13:14:58 +0800 | [diff] [blame] | 4473 | wait_for_device_probe(); |
| 4474 | device_block_probing(); |
| 4475 | |
Rafael J. Wysocki | 65650b3 | 2019-10-09 01:29:10 +0200 | [diff] [blame] | 4476 | cpufreq_suspend(); |
| 4477 | |
Hugh Daschbach | 6245838 | 2010-03-22 10:36:37 -0700 | [diff] [blame] | 4478 | spin_lock(&devices_kset->list_lock); |
| 4479 | /* |
| 4480 | * Walk the devices list backward, shutting down each in turn. |
| 4481 | * Beware that device unplug events may also start pulling |
| 4482 | * devices offline, even as the system is shutting down. |
| 4483 | */ |
| 4484 | while (!list_empty(&devices_kset->list)) { |
| 4485 | dev = list_entry(devices_kset->list.prev, struct device, |
| 4486 | kobj.entry); |
Ming Lei | d1c6c03 | 2012-06-22 18:01:40 +0800 | [diff] [blame] | 4487 | |
| 4488 | /* |
| 4489 | * hold reference count of device's parent to |
| 4490 | * prevent it from being freed because parent's |
| 4491 | * lock is to be held |
| 4492 | */ |
Benson Leung | f123db8 | 2013-09-24 20:05:11 -0700 | [diff] [blame] | 4493 | parent = get_device(dev->parent); |
Hugh Daschbach | 6245838 | 2010-03-22 10:36:37 -0700 | [diff] [blame] | 4494 | get_device(dev); |
| 4495 | /* |
| 4496 | * Make sure the device is off the kset list, in the |
| 4497 | * event that dev->*->shutdown() doesn't remove it. |
| 4498 | */ |
| 4499 | list_del_init(&dev->kobj.entry); |
| 4500 | spin_unlock(&devices_kset->list_lock); |
Alan Stern | fe6b91f | 2011-12-06 23:24:52 +0100 | [diff] [blame] | 4501 | |
Ming Lei | d1c6c03 | 2012-06-22 18:01:40 +0800 | [diff] [blame] | 4502 | /* hold lock to avoid race with probe/release */ |
Benson Leung | f123db8 | 2013-09-24 20:05:11 -0700 | [diff] [blame] | 4503 | if (parent) |
| 4504 | device_lock(parent); |
Ming Lei | d1c6c03 | 2012-06-22 18:01:40 +0800 | [diff] [blame] | 4505 | device_lock(dev); |
| 4506 | |
Alan Stern | fe6b91f | 2011-12-06 23:24:52 +0100 | [diff] [blame] | 4507 | /* Don't allow any more runtime suspends */ |
| 4508 | pm_runtime_get_noresume(dev); |
| 4509 | pm_runtime_barrier(dev); |
Hugh Daschbach | 6245838 | 2010-03-22 10:36:37 -0700 | [diff] [blame] | 4510 | |
Michal Suchanek | 7521621 | 2017-08-11 15:44:43 +0200 | [diff] [blame] | 4511 | if (dev->class && dev->class->shutdown_pre) { |
Josh Zimmerman | f77af15 | 2017-06-25 14:53:23 -0700 | [diff] [blame] | 4512 | if (initcall_debug) |
Michal Suchanek | 7521621 | 2017-08-11 15:44:43 +0200 | [diff] [blame] | 4513 | dev_info(dev, "shutdown_pre\n"); |
| 4514 | dev->class->shutdown_pre(dev); |
| 4515 | } |
| 4516 | if (dev->bus && dev->bus->shutdown) { |
ShuoX Liu | 0246c4f | 2012-11-23 15:14:12 +0800 | [diff] [blame] | 4517 | if (initcall_debug) |
| 4518 | dev_info(dev, "shutdown\n"); |
Greg Kroah-Hartman | 37b0c02 | 2007-11-26 22:11:55 -0800 | [diff] [blame] | 4519 | dev->bus->shutdown(dev); |
| 4520 | } else if (dev->driver && dev->driver->shutdown) { |
ShuoX Liu | 0246c4f | 2012-11-23 15:14:12 +0800 | [diff] [blame] | 4521 | if (initcall_debug) |
| 4522 | dev_info(dev, "shutdown\n"); |
Greg Kroah-Hartman | 37b0c02 | 2007-11-26 22:11:55 -0800 | [diff] [blame] | 4523 | dev->driver->shutdown(dev); |
| 4524 | } |
Ming Lei | d1c6c03 | 2012-06-22 18:01:40 +0800 | [diff] [blame] | 4525 | |
| 4526 | device_unlock(dev); |
Benson Leung | f123db8 | 2013-09-24 20:05:11 -0700 | [diff] [blame] | 4527 | if (parent) |
| 4528 | device_unlock(parent); |
Ming Lei | d1c6c03 | 2012-06-22 18:01:40 +0800 | [diff] [blame] | 4529 | |
Hugh Daschbach | 6245838 | 2010-03-22 10:36:37 -0700 | [diff] [blame] | 4530 | put_device(dev); |
Benson Leung | f123db8 | 2013-09-24 20:05:11 -0700 | [diff] [blame] | 4531 | put_device(parent); |
Hugh Daschbach | 6245838 | 2010-03-22 10:36:37 -0700 | [diff] [blame] | 4532 | |
| 4533 | spin_lock(&devices_kset->list_lock); |
Greg Kroah-Hartman | 37b0c02 | 2007-11-26 22:11:55 -0800 | [diff] [blame] | 4534 | } |
Hugh Daschbach | 6245838 | 2010-03-22 10:36:37 -0700 | [diff] [blame] | 4535 | spin_unlock(&devices_kset->list_lock); |
Greg Kroah-Hartman | 37b0c02 | 2007-11-26 22:11:55 -0800 | [diff] [blame] | 4536 | } |
Joe Perches | 99bcf21 | 2010-06-27 01:02:34 +0000 | [diff] [blame] | 4537 | |
| 4538 | /* |
| 4539 | * Device logging functions |
| 4540 | */ |
| 4541 | |
| 4542 | #ifdef CONFIG_PRINTK |
John Ogness | 74caba7 | 2020-09-21 13:24:45 +0206 | [diff] [blame] | 4543 | static void |
| 4544 | set_dev_info(const struct device *dev, struct dev_printk_info *dev_info) |
Joe Perches | 99bcf21 | 2010-06-27 01:02:34 +0000 | [diff] [blame] | 4545 | { |
Kay Sievers | c4e00da | 2012-05-03 02:29:59 +0200 | [diff] [blame] | 4546 | const char *subsys; |
John Ogness | 74caba7 | 2020-09-21 13:24:45 +0206 | [diff] [blame] | 4547 | |
| 4548 | memset(dev_info, 0, sizeof(*dev_info)); |
Joe Perches | 99bcf21 | 2010-06-27 01:02:34 +0000 | [diff] [blame] | 4549 | |
Kay Sievers | c4e00da | 2012-05-03 02:29:59 +0200 | [diff] [blame] | 4550 | if (dev->class) |
| 4551 | subsys = dev->class->name; |
| 4552 | else if (dev->bus) |
| 4553 | subsys = dev->bus->name; |
| 4554 | else |
John Ogness | 74caba7 | 2020-09-21 13:24:45 +0206 | [diff] [blame] | 4555 | return; |
Kay Sievers | c4e00da | 2012-05-03 02:29:59 +0200 | [diff] [blame] | 4556 | |
John Ogness | 74caba7 | 2020-09-21 13:24:45 +0206 | [diff] [blame] | 4557 | strscpy(dev_info->subsystem, subsys, sizeof(dev_info->subsystem)); |
Kay Sievers | c4e00da | 2012-05-03 02:29:59 +0200 | [diff] [blame] | 4558 | |
| 4559 | /* |
| 4560 | * Add device identifier DEVICE=: |
| 4561 | * b12:8 block dev_t |
| 4562 | * c127:3 char dev_t |
| 4563 | * n8 netdev ifindex |
| 4564 | * +sound:card0 subsystem:devname |
| 4565 | */ |
| 4566 | if (MAJOR(dev->devt)) { |
| 4567 | char c; |
| 4568 | |
| 4569 | if (strcmp(subsys, "block") == 0) |
| 4570 | c = 'b'; |
| 4571 | else |
| 4572 | c = 'c'; |
John Ogness | 74caba7 | 2020-09-21 13:24:45 +0206 | [diff] [blame] | 4573 | |
| 4574 | snprintf(dev_info->device, sizeof(dev_info->device), |
| 4575 | "%c%u:%u", c, MAJOR(dev->devt), MINOR(dev->devt)); |
Kay Sievers | c4e00da | 2012-05-03 02:29:59 +0200 | [diff] [blame] | 4576 | } else if (strcmp(subsys, "net") == 0) { |
| 4577 | struct net_device *net = to_net_dev(dev); |
| 4578 | |
John Ogness | 74caba7 | 2020-09-21 13:24:45 +0206 | [diff] [blame] | 4579 | snprintf(dev_info->device, sizeof(dev_info->device), |
| 4580 | "n%u", net->ifindex); |
Kay Sievers | c4e00da | 2012-05-03 02:29:59 +0200 | [diff] [blame] | 4581 | } else { |
John Ogness | 74caba7 | 2020-09-21 13:24:45 +0206 | [diff] [blame] | 4582 | snprintf(dev_info->device, sizeof(dev_info->device), |
| 4583 | "+%s:%s", subsys, dev_name(dev)); |
Kay Sievers | c4e00da | 2012-05-03 02:29:59 +0200 | [diff] [blame] | 4584 | } |
Joe Perches | 99bcf21 | 2010-06-27 01:02:34 +0000 | [diff] [blame] | 4585 | } |
Joe Perches | 798efc6 | 2012-09-12 20:11:29 -0700 | [diff] [blame] | 4586 | |
Joe Perches | 05e4e5b | 2012-09-12 20:13:37 -0700 | [diff] [blame] | 4587 | int dev_vprintk_emit(int level, const struct device *dev, |
| 4588 | const char *fmt, va_list args) |
| 4589 | { |
John Ogness | 74caba7 | 2020-09-21 13:24:45 +0206 | [diff] [blame] | 4590 | struct dev_printk_info dev_info; |
Joe Perches | 05e4e5b | 2012-09-12 20:13:37 -0700 | [diff] [blame] | 4591 | |
John Ogness | 74caba7 | 2020-09-21 13:24:45 +0206 | [diff] [blame] | 4592 | set_dev_info(dev, &dev_info); |
Joe Perches | 05e4e5b | 2012-09-12 20:13:37 -0700 | [diff] [blame] | 4593 | |
John Ogness | 74caba7 | 2020-09-21 13:24:45 +0206 | [diff] [blame] | 4594 | return vprintk_emit(0, level, &dev_info, fmt, args); |
Joe Perches | 05e4e5b | 2012-09-12 20:13:37 -0700 | [diff] [blame] | 4595 | } |
| 4596 | EXPORT_SYMBOL(dev_vprintk_emit); |
| 4597 | |
| 4598 | int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...) |
| 4599 | { |
| 4600 | va_list args; |
| 4601 | int r; |
| 4602 | |
| 4603 | va_start(args, fmt); |
| 4604 | |
| 4605 | r = dev_vprintk_emit(level, dev, fmt, args); |
| 4606 | |
| 4607 | va_end(args); |
| 4608 | |
| 4609 | return r; |
| 4610 | } |
| 4611 | EXPORT_SYMBOL(dev_printk_emit); |
| 4612 | |
Joe Perches | d1f1052 | 2014-12-25 15:07:04 -0800 | [diff] [blame] | 4613 | static void __dev_printk(const char *level, const struct device *dev, |
Joe Perches | 798efc6 | 2012-09-12 20:11:29 -0700 | [diff] [blame] | 4614 | struct va_format *vaf) |
| 4615 | { |
Joe Perches | d1f1052 | 2014-12-25 15:07:04 -0800 | [diff] [blame] | 4616 | if (dev) |
| 4617 | dev_printk_emit(level[1] - '0', dev, "%s %s: %pV", |
| 4618 | dev_driver_string(dev), dev_name(dev), vaf); |
| 4619 | else |
| 4620 | printk("%s(NULL device *): %pV", level, vaf); |
Joe Perches | 798efc6 | 2012-09-12 20:11:29 -0700 | [diff] [blame] | 4621 | } |
Joe Perches | 99bcf21 | 2010-06-27 01:02:34 +0000 | [diff] [blame] | 4622 | |
Chris Down | ad7d61f | 2021-06-15 17:52:56 +0100 | [diff] [blame] | 4623 | void _dev_printk(const char *level, const struct device *dev, |
| 4624 | const char *fmt, ...) |
Joe Perches | 99bcf21 | 2010-06-27 01:02:34 +0000 | [diff] [blame] | 4625 | { |
| 4626 | struct va_format vaf; |
| 4627 | va_list args; |
Joe Perches | 99bcf21 | 2010-06-27 01:02:34 +0000 | [diff] [blame] | 4628 | |
| 4629 | va_start(args, fmt); |
| 4630 | |
| 4631 | vaf.fmt = fmt; |
| 4632 | vaf.va = &args; |
| 4633 | |
Joe Perches | d1f1052 | 2014-12-25 15:07:04 -0800 | [diff] [blame] | 4634 | __dev_printk(level, dev, &vaf); |
Joe Perches | 798efc6 | 2012-09-12 20:11:29 -0700 | [diff] [blame] | 4635 | |
Joe Perches | 99bcf21 | 2010-06-27 01:02:34 +0000 | [diff] [blame] | 4636 | va_end(args); |
Joe Perches | 99bcf21 | 2010-06-27 01:02:34 +0000 | [diff] [blame] | 4637 | } |
Chris Down | ad7d61f | 2021-06-15 17:52:56 +0100 | [diff] [blame] | 4638 | EXPORT_SYMBOL(_dev_printk); |
Joe Perches | 99bcf21 | 2010-06-27 01:02:34 +0000 | [diff] [blame] | 4639 | |
| 4640 | #define define_dev_printk_level(func, kern_level) \ |
Joe Perches | d1f1052 | 2014-12-25 15:07:04 -0800 | [diff] [blame] | 4641 | void func(const struct device *dev, const char *fmt, ...) \ |
Joe Perches | 99bcf21 | 2010-06-27 01:02:34 +0000 | [diff] [blame] | 4642 | { \ |
| 4643 | struct va_format vaf; \ |
| 4644 | va_list args; \ |
Joe Perches | 99bcf21 | 2010-06-27 01:02:34 +0000 | [diff] [blame] | 4645 | \ |
| 4646 | va_start(args, fmt); \ |
| 4647 | \ |
| 4648 | vaf.fmt = fmt; \ |
| 4649 | vaf.va = &args; \ |
| 4650 | \ |
Joe Perches | d1f1052 | 2014-12-25 15:07:04 -0800 | [diff] [blame] | 4651 | __dev_printk(kern_level, dev, &vaf); \ |
Joe Perches | 798efc6 | 2012-09-12 20:11:29 -0700 | [diff] [blame] | 4652 | \ |
Joe Perches | 99bcf21 | 2010-06-27 01:02:34 +0000 | [diff] [blame] | 4653 | va_end(args); \ |
Joe Perches | 99bcf21 | 2010-06-27 01:02:34 +0000 | [diff] [blame] | 4654 | } \ |
| 4655 | EXPORT_SYMBOL(func); |
| 4656 | |
Joe Perches | 663336e | 2018-05-09 08:15:46 -0700 | [diff] [blame] | 4657 | define_dev_printk_level(_dev_emerg, KERN_EMERG); |
| 4658 | define_dev_printk_level(_dev_alert, KERN_ALERT); |
| 4659 | define_dev_printk_level(_dev_crit, KERN_CRIT); |
| 4660 | define_dev_printk_level(_dev_err, KERN_ERR); |
| 4661 | define_dev_printk_level(_dev_warn, KERN_WARNING); |
| 4662 | define_dev_printk_level(_dev_notice, KERN_NOTICE); |
Joe Perches | 99bcf21 | 2010-06-27 01:02:34 +0000 | [diff] [blame] | 4663 | define_dev_printk_level(_dev_info, KERN_INFO); |
| 4664 | |
| 4665 | #endif |
Rafael J. Wysocki | 97badf8 | 2015-04-03 23:23:37 +0200 | [diff] [blame] | 4666 | |
Andrzej Hajda | a787e54 | 2020-07-13 16:43:21 +0200 | [diff] [blame] | 4667 | /** |
| 4668 | * dev_err_probe - probe error check and log helper |
| 4669 | * @dev: the pointer to the struct device |
| 4670 | * @err: error value to test |
| 4671 | * @fmt: printf-style format string |
| 4672 | * @...: arguments as specified in the format string |
| 4673 | * |
| 4674 | * This helper implements common pattern present in probe functions for error |
| 4675 | * checking: print debug or error message depending if the error value is |
| 4676 | * -EPROBE_DEFER and propagate error upwards. |
Andrzej Hajda | d090b70 | 2020-07-13 16:43:22 +0200 | [diff] [blame] | 4677 | * In case of -EPROBE_DEFER it sets also defer probe reason, which can be |
| 4678 | * checked later by reading devices_deferred debugfs attribute. |
Mauro Carvalho Chehab | 074b3aa | 2020-09-09 11:53:43 +0200 | [diff] [blame] | 4679 | * It replaces code sequence:: |
| 4680 | * |
Andrzej Hajda | a787e54 | 2020-07-13 16:43:21 +0200 | [diff] [blame] | 4681 | * if (err != -EPROBE_DEFER) |
| 4682 | * dev_err(dev, ...); |
| 4683 | * else |
| 4684 | * dev_dbg(dev, ...); |
| 4685 | * return err; |
Mauro Carvalho Chehab | 074b3aa | 2020-09-09 11:53:43 +0200 | [diff] [blame] | 4686 | * |
| 4687 | * with:: |
| 4688 | * |
Andrzej Hajda | a787e54 | 2020-07-13 16:43:21 +0200 | [diff] [blame] | 4689 | * return dev_err_probe(dev, err, ...); |
| 4690 | * |
Douglas Anderson | 7065f92 | 2021-09-16 16:19:40 -0700 | [diff] [blame] | 4691 | * Note that it is deemed acceptable to use this function for error |
| 4692 | * prints during probe even if the @err is known to never be -EPROBE_DEFER. |
| 4693 | * The benefit compared to a normal dev_err() is the standardized format |
| 4694 | * of the error code and the fact that the error code is returned. |
| 4695 | * |
Andrzej Hajda | a787e54 | 2020-07-13 16:43:21 +0200 | [diff] [blame] | 4696 | * Returns @err. |
| 4697 | * |
| 4698 | */ |
| 4699 | int dev_err_probe(const struct device *dev, int err, const char *fmt, ...) |
| 4700 | { |
| 4701 | struct va_format vaf; |
| 4702 | va_list args; |
| 4703 | |
| 4704 | va_start(args, fmt); |
| 4705 | vaf.fmt = fmt; |
| 4706 | vaf.va = &args; |
| 4707 | |
Andrzej Hajda | d090b70 | 2020-07-13 16:43:22 +0200 | [diff] [blame] | 4708 | if (err != -EPROBE_DEFER) { |
Michał Mirosław | 693a8e9 | 2020-08-28 18:14:35 +0200 | [diff] [blame] | 4709 | dev_err(dev, "error %pe: %pV", ERR_PTR(err), &vaf); |
Andrzej Hajda | d090b70 | 2020-07-13 16:43:22 +0200 | [diff] [blame] | 4710 | } else { |
| 4711 | device_set_deferred_probe_reason(dev, &vaf); |
Michał Mirosław | 693a8e9 | 2020-08-28 18:14:35 +0200 | [diff] [blame] | 4712 | dev_dbg(dev, "error %pe: %pV", ERR_PTR(err), &vaf); |
Andrzej Hajda | d090b70 | 2020-07-13 16:43:22 +0200 | [diff] [blame] | 4713 | } |
Andrzej Hajda | a787e54 | 2020-07-13 16:43:21 +0200 | [diff] [blame] | 4714 | |
| 4715 | va_end(args); |
| 4716 | |
| 4717 | return err; |
| 4718 | } |
| 4719 | EXPORT_SYMBOL_GPL(dev_err_probe); |
| 4720 | |
Rafael J. Wysocki | 97badf8 | 2015-04-03 23:23:37 +0200 | [diff] [blame] | 4721 | static inline bool fwnode_is_primary(struct fwnode_handle *fwnode) |
| 4722 | { |
| 4723 | return fwnode && !IS_ERR(fwnode->secondary); |
| 4724 | } |
| 4725 | |
| 4726 | /** |
| 4727 | * set_primary_fwnode - Change the primary firmware node of a given device. |
| 4728 | * @dev: Device to handle. |
| 4729 | * @fwnode: New primary firmware node of the device. |
| 4730 | * |
| 4731 | * Set the device's firmware node pointer to @fwnode, but if a secondary |
| 4732 | * firmware node of the device is present, preserve it. |
Bard Liao | 3f7bdda | 2021-01-05 17:11:46 +0800 | [diff] [blame] | 4733 | * |
| 4734 | * Valid fwnode cases are: |
| 4735 | * - primary --> secondary --> -ENODEV |
| 4736 | * - primary --> NULL |
| 4737 | * - secondary --> -ENODEV |
| 4738 | * - NULL |
Rafael J. Wysocki | 97badf8 | 2015-04-03 23:23:37 +0200 | [diff] [blame] | 4739 | */ |
| 4740 | void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode) |
| 4741 | { |
Andy Shevchenko | 99aed92 | 2020-10-22 21:41:00 +0300 | [diff] [blame] | 4742 | struct device *parent = dev->parent; |
Heikki Krogerus | c15e1bd | 2020-08-21 13:53:42 +0300 | [diff] [blame] | 4743 | struct fwnode_handle *fn = dev->fwnode; |
Rafael J. Wysocki | 97badf8 | 2015-04-03 23:23:37 +0200 | [diff] [blame] | 4744 | |
Heikki Krogerus | c15e1bd | 2020-08-21 13:53:42 +0300 | [diff] [blame] | 4745 | if (fwnode) { |
Rafael J. Wysocki | 97badf8 | 2015-04-03 23:23:37 +0200 | [diff] [blame] | 4746 | if (fwnode_is_primary(fn)) |
| 4747 | fn = fn->secondary; |
| 4748 | |
Mika Westerberg | 55f89a8 | 2015-11-30 17:11:39 +0200 | [diff] [blame] | 4749 | if (fn) { |
| 4750 | WARN_ON(fwnode->secondary); |
| 4751 | fwnode->secondary = fn; |
| 4752 | } |
Rafael J. Wysocki | 97badf8 | 2015-04-03 23:23:37 +0200 | [diff] [blame] | 4753 | dev->fwnode = fwnode; |
| 4754 | } else { |
Heikki Krogerus | c15e1bd | 2020-08-21 13:53:42 +0300 | [diff] [blame] | 4755 | if (fwnode_is_primary(fn)) { |
| 4756 | dev->fwnode = fn->secondary; |
Bard Liao | 3f7bdda | 2021-01-05 17:11:46 +0800 | [diff] [blame] | 4757 | /* Set fn->secondary = NULL, so fn remains the primary fwnode */ |
Andy Shevchenko | 99aed92 | 2020-10-22 21:41:00 +0300 | [diff] [blame] | 4758 | if (!(parent && fn == parent->fwnode)) |
Bard Liao | 47f4469 | 2021-01-05 17:11:45 +0800 | [diff] [blame] | 4759 | fn->secondary = NULL; |
Heikki Krogerus | c15e1bd | 2020-08-21 13:53:42 +0300 | [diff] [blame] | 4760 | } else { |
| 4761 | dev->fwnode = NULL; |
| 4762 | } |
Rafael J. Wysocki | 97badf8 | 2015-04-03 23:23:37 +0200 | [diff] [blame] | 4763 | } |
| 4764 | } |
| 4765 | EXPORT_SYMBOL_GPL(set_primary_fwnode); |
| 4766 | |
| 4767 | /** |
| 4768 | * set_secondary_fwnode - Change the secondary firmware node of a given device. |
| 4769 | * @dev: Device to handle. |
| 4770 | * @fwnode: New secondary firmware node of the device. |
| 4771 | * |
| 4772 | * If a primary firmware node of the device is present, set its secondary |
| 4773 | * pointer to @fwnode. Otherwise, set the device's firmware node pointer to |
| 4774 | * @fwnode. |
| 4775 | */ |
| 4776 | void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode) |
| 4777 | { |
| 4778 | if (fwnode) |
| 4779 | fwnode->secondary = ERR_PTR(-ENODEV); |
| 4780 | |
| 4781 | if (fwnode_is_primary(dev->fwnode)) |
| 4782 | dev->fwnode->secondary = fwnode; |
| 4783 | else |
| 4784 | dev->fwnode = fwnode; |
| 4785 | } |
Andy Shevchenko | 96489ae | 2020-04-08 19:09:01 +0300 | [diff] [blame] | 4786 | EXPORT_SYMBOL_GPL(set_secondary_fwnode); |
Johan Hovold | 4e75e1d | 2017-06-06 17:59:00 +0200 | [diff] [blame] | 4787 | |
| 4788 | /** |
| 4789 | * device_set_of_node_from_dev - reuse device-tree node of another device |
| 4790 | * @dev: device whose device-tree node is being set |
| 4791 | * @dev2: device whose device-tree node is being reused |
| 4792 | * |
| 4793 | * Takes another reference to the new device-tree node after first dropping |
| 4794 | * any reference held to the old node. |
| 4795 | */ |
| 4796 | void device_set_of_node_from_dev(struct device *dev, const struct device *dev2) |
| 4797 | { |
| 4798 | of_node_put(dev->of_node); |
| 4799 | dev->of_node = of_node_get(dev2->of_node); |
| 4800 | dev->of_node_reused = true; |
| 4801 | } |
| 4802 | EXPORT_SYMBOL_GPL(device_set_of_node_from_dev); |
Suzuki K Poulose | 65b6668 | 2019-06-14 18:54:01 +0100 | [diff] [blame] | 4803 | |
Ioana Ciornei | 43e76d4 | 2021-06-17 15:29:04 +0300 | [diff] [blame] | 4804 | void device_set_node(struct device *dev, struct fwnode_handle *fwnode) |
| 4805 | { |
| 4806 | dev->fwnode = fwnode; |
| 4807 | dev->of_node = to_of_node(fwnode); |
| 4808 | } |
| 4809 | EXPORT_SYMBOL_GPL(device_set_node); |
| 4810 | |
Suzuki K Poulose | 6cda08a | 2019-07-23 23:18:32 +0100 | [diff] [blame] | 4811 | int device_match_name(struct device *dev, const void *name) |
| 4812 | { |
| 4813 | return sysfs_streq(dev_name(dev), name); |
| 4814 | } |
| 4815 | EXPORT_SYMBOL_GPL(device_match_name); |
| 4816 | |
Suzuki K Poulose | 65b6668 | 2019-06-14 18:54:01 +0100 | [diff] [blame] | 4817 | int device_match_of_node(struct device *dev, const void *np) |
| 4818 | { |
| 4819 | return dev->of_node == np; |
| 4820 | } |
| 4821 | EXPORT_SYMBOL_GPL(device_match_of_node); |
Suzuki K Poulose | 67843bb | 2019-07-23 23:18:34 +0100 | [diff] [blame] | 4822 | |
| 4823 | int device_match_fwnode(struct device *dev, const void *fwnode) |
| 4824 | { |
| 4825 | return dev_fwnode(dev) == fwnode; |
| 4826 | } |
| 4827 | EXPORT_SYMBOL_GPL(device_match_fwnode); |
Suzuki K Poulose | 4495dfd | 2019-07-23 23:18:35 +0100 | [diff] [blame] | 4828 | |
| 4829 | int device_match_devt(struct device *dev, const void *pdevt) |
| 4830 | { |
| 4831 | return dev->devt == *(dev_t *)pdevt; |
| 4832 | } |
| 4833 | EXPORT_SYMBOL_GPL(device_match_devt); |
Suzuki K Poulose | 0050014 | 2019-07-23 23:18:36 +0100 | [diff] [blame] | 4834 | |
| 4835 | int device_match_acpi_dev(struct device *dev, const void *adev) |
| 4836 | { |
| 4837 | return ACPI_COMPANION(dev) == adev; |
| 4838 | } |
| 4839 | EXPORT_SYMBOL(device_match_acpi_dev); |
Suzuki K Poulose | 6bf85ba | 2019-07-23 23:18:37 +0100 | [diff] [blame] | 4840 | |
Andy Shevchenko | a164ff5 | 2021-10-14 16:47:54 +0300 | [diff] [blame] | 4841 | int device_match_acpi_handle(struct device *dev, const void *handle) |
| 4842 | { |
| 4843 | return ACPI_HANDLE(dev) == handle; |
| 4844 | } |
| 4845 | EXPORT_SYMBOL(device_match_acpi_handle); |
| 4846 | |
Suzuki K Poulose | 6bf85ba | 2019-07-23 23:18:37 +0100 | [diff] [blame] | 4847 | int device_match_any(struct device *dev, const void *unused) |
| 4848 | { |
| 4849 | return 1; |
| 4850 | } |
| 4851 | EXPORT_SYMBOL_GPL(device_match_any); |