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