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