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