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