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