1 // SPDX-License-Identifier: GPL-2.0
3 * drivers/base/core.c - core driver model code (device registration, etc)
5 * Copyright (c) 2002-3 Patrick Mochel
6 * Copyright (c) 2002-3 Open Source Development Labs
7 * Copyright (c) 2006 Greg Kroah-Hartman <gregkh@suse.de>
8 * Copyright (c) 2006 Novell, Inc.
11 #include <linux/acpi.h>
12 #include <linux/cpufreq.h>
13 #include <linux/device.h>
14 #include <linux/err.h>
15 #include <linux/fwnode.h>
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/string.h>
20 #include <linux/kdev_t.h>
21 #include <linux/notifier.h>
23 #include <linux/of_device.h>
24 #include <linux/genhd.h>
25 #include <linux/mutex.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/netdevice.h>
28 #include <linux/sched/signal.h>
29 #include <linux/sysfs.h>
32 #include "power/power.h"
34 #ifdef CONFIG_SYSFS_DEPRECATED
35 #ifdef CONFIG_SYSFS_DEPRECATED_V2
36 long sysfs_deprecated = 1;
38 long sysfs_deprecated = 0;
40 static int __init sysfs_deprecated_setup(char *arg)
42 return kstrtol(arg, 10, &sysfs_deprecated);
44 early_param("sysfs.deprecated", sysfs_deprecated_setup);
47 /* Device links support. */
48 static LIST_HEAD(wait_for_suppliers);
49 static DEFINE_MUTEX(wfs_lock);
50 static LIST_HEAD(deferred_sync);
51 static unsigned int defer_sync_state_count = 1;
54 static DEFINE_MUTEX(device_links_lock);
55 DEFINE_STATIC_SRCU(device_links_srcu);
57 static inline void device_links_write_lock(void)
59 mutex_lock(&device_links_lock);
62 static inline void device_links_write_unlock(void)
64 mutex_unlock(&device_links_lock);
67 int device_links_read_lock(void)
69 return srcu_read_lock(&device_links_srcu);
72 void device_links_read_unlock(int idx)
74 srcu_read_unlock(&device_links_srcu, idx);
77 int device_links_read_lock_held(void)
79 return srcu_read_lock_held(&device_links_srcu);
81 #else /* !CONFIG_SRCU */
82 static DECLARE_RWSEM(device_links_lock);
84 static inline void device_links_write_lock(void)
86 down_write(&device_links_lock);
89 static inline void device_links_write_unlock(void)
91 up_write(&device_links_lock);
94 int device_links_read_lock(void)
96 down_read(&device_links_lock);
100 void device_links_read_unlock(int not_used)
102 up_read(&device_links_lock);
105 #ifdef CONFIG_DEBUG_LOCK_ALLOC
106 int device_links_read_lock_held(void)
108 return lockdep_is_held(&device_links_lock);
111 #endif /* !CONFIG_SRCU */
114 * device_is_dependent - Check if one device depends on another one
115 * @dev: Device to check dependencies for.
116 * @target: Device to check against.
118 * Check if @target depends on @dev or any device dependent on it (its child or
119 * its consumer etc). Return 1 if that is the case or 0 otherwise.
121 static int device_is_dependent(struct device *dev, void *target)
123 struct device_link *link;
129 ret = device_for_each_child(dev, target, device_is_dependent);
133 list_for_each_entry(link, &dev->links.consumers, s_node) {
134 if (link->flags == (DL_FLAG_SYNC_STATE_ONLY | DL_FLAG_MANAGED))
137 if (link->consumer == target)
140 ret = device_is_dependent(link->consumer, target);
147 static void device_link_init_status(struct device_link *link,
148 struct device *consumer,
149 struct device *supplier)
151 switch (supplier->links.status) {
153 switch (consumer->links.status) {
156 * A consumer driver can create a link to a supplier
157 * that has not completed its probing yet as long as it
158 * knows that the supplier is already functional (for
159 * example, it has just acquired some resources from the
162 link->status = DL_STATE_CONSUMER_PROBE;
165 link->status = DL_STATE_DORMANT;
169 case DL_DEV_DRIVER_BOUND:
170 switch (consumer->links.status) {
172 link->status = DL_STATE_CONSUMER_PROBE;
174 case DL_DEV_DRIVER_BOUND:
175 link->status = DL_STATE_ACTIVE;
178 link->status = DL_STATE_AVAILABLE;
182 case DL_DEV_UNBINDING:
183 link->status = DL_STATE_SUPPLIER_UNBIND;
186 link->status = DL_STATE_DORMANT;
191 static int device_reorder_to_tail(struct device *dev, void *not_used)
193 struct device_link *link;
196 * Devices that have not been registered yet will be put to the ends
197 * of the lists during the registration, so skip them here.
199 if (device_is_registered(dev))
200 devices_kset_move_last(dev);
202 if (device_pm_initialized(dev))
203 device_pm_move_last(dev);
205 device_for_each_child(dev, NULL, device_reorder_to_tail);
206 list_for_each_entry(link, &dev->links.consumers, s_node) {
207 if (link->flags == (DL_FLAG_SYNC_STATE_ONLY | DL_FLAG_MANAGED))
209 device_reorder_to_tail(link->consumer, NULL);
216 * device_pm_move_to_tail - Move set of devices to the end of device lists
217 * @dev: Device to move
219 * This is a device_reorder_to_tail() wrapper taking the requisite locks.
221 * It moves the @dev along with all of its children and all of its consumers
222 * to the ends of the device_kset and dpm_list, recursively.
224 void device_pm_move_to_tail(struct device *dev)
228 idx = device_links_read_lock();
230 device_reorder_to_tail(dev, NULL);
232 device_links_read_unlock(idx);
235 #define DL_MANAGED_LINK_FLAGS (DL_FLAG_AUTOREMOVE_CONSUMER | \
236 DL_FLAG_AUTOREMOVE_SUPPLIER | \
237 DL_FLAG_AUTOPROBE_CONSUMER | \
238 DL_FLAG_SYNC_STATE_ONLY)
240 #define DL_ADD_VALID_FLAGS (DL_MANAGED_LINK_FLAGS | DL_FLAG_STATELESS | \
241 DL_FLAG_PM_RUNTIME | DL_FLAG_RPM_ACTIVE)
244 * device_link_add - Create a link between two devices.
245 * @consumer: Consumer end of the link.
246 * @supplier: Supplier end of the link.
247 * @flags: Link flags.
249 * The caller is responsible for the proper synchronization of the link creation
250 * with runtime PM. First, setting the DL_FLAG_PM_RUNTIME flag will cause the
251 * runtime PM framework to take the link into account. Second, if the
252 * DL_FLAG_RPM_ACTIVE flag is set in addition to it, the supplier devices will
253 * be forced into the active metastate and reference-counted upon the creation
254 * of the link. If DL_FLAG_PM_RUNTIME is not set, DL_FLAG_RPM_ACTIVE will be
257 * If DL_FLAG_STATELESS is set in @flags, the caller of this function is
258 * expected to release the link returned by it directly with the help of either
259 * device_link_del() or device_link_remove().
261 * If that flag is not set, however, the caller of this function is handing the
262 * management of the link over to the driver core entirely and its return value
263 * can only be used to check whether or not the link is present. In that case,
264 * the DL_FLAG_AUTOREMOVE_CONSUMER and DL_FLAG_AUTOREMOVE_SUPPLIER device link
265 * flags can be used to indicate to the driver core when the link can be safely
266 * deleted. Namely, setting one of them in @flags indicates to the driver core
267 * that the link is not going to be used (by the given caller of this function)
268 * after unbinding the consumer or supplier driver, respectively, from its
269 * device, so the link can be deleted at that point. If none of them is set,
270 * the link will be maintained until one of the devices pointed to by it (either
271 * the consumer or the supplier) is unregistered.
273 * Also, if DL_FLAG_STATELESS, DL_FLAG_AUTOREMOVE_CONSUMER and
274 * DL_FLAG_AUTOREMOVE_SUPPLIER are not set in @flags (that is, a persistent
275 * managed device link is being added), the DL_FLAG_AUTOPROBE_CONSUMER flag can
276 * be used to request the driver core to automaticall probe for a consmer
277 * driver after successfully binding a driver to the supplier device.
279 * The combination of DL_FLAG_STATELESS and one of DL_FLAG_AUTOREMOVE_CONSUMER,
280 * DL_FLAG_AUTOREMOVE_SUPPLIER, or DL_FLAG_AUTOPROBE_CONSUMER set in @flags at
281 * the same time is invalid and will cause NULL to be returned upfront.
282 * However, if a device link between the given @consumer and @supplier pair
283 * exists already when this function is called for them, the existing link will
284 * be returned regardless of its current type and status (the link's flags may
285 * be modified then). The caller of this function is then expected to treat
286 * the link as though it has just been created, so (in particular) if
287 * DL_FLAG_STATELESS was passed in @flags, the link needs to be released
288 * explicitly when not needed any more (as stated above).
290 * A side effect of the link creation is re-ordering of dpm_list and the
291 * devices_kset list by moving the consumer device and all devices depending
292 * on it to the ends of these lists (that does not happen to devices that have
293 * not been registered when this function is called).
295 * The supplier device is required to be registered when this function is called
296 * and NULL will be returned if that is not the case. The consumer device need
297 * not be registered, however.
299 struct device_link *device_link_add(struct device *consumer,
300 struct device *supplier, u32 flags)
302 struct device_link *link;
304 if (!consumer || !supplier || flags & ~DL_ADD_VALID_FLAGS ||
305 (flags & DL_FLAG_STATELESS && flags & DL_MANAGED_LINK_FLAGS) ||
306 (flags & DL_FLAG_SYNC_STATE_ONLY &&
307 flags != DL_FLAG_SYNC_STATE_ONLY) ||
308 (flags & DL_FLAG_AUTOPROBE_CONSUMER &&
309 flags & (DL_FLAG_AUTOREMOVE_CONSUMER |
310 DL_FLAG_AUTOREMOVE_SUPPLIER)))
313 if (flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) {
314 if (pm_runtime_get_sync(supplier) < 0) {
315 pm_runtime_put_noidle(supplier);
320 if (!(flags & DL_FLAG_STATELESS))
321 flags |= DL_FLAG_MANAGED;
323 device_links_write_lock();
327 * If the supplier has not been fully registered yet or there is a
328 * reverse (non-SYNC_STATE_ONLY) dependency between the consumer and
329 * the supplier already in the graph, return NULL. If the link is a
330 * SYNC_STATE_ONLY link, we don't check for reverse dependencies
331 * because it only affects sync_state() callbacks.
333 if (!device_pm_initialized(supplier)
334 || (!(flags & DL_FLAG_SYNC_STATE_ONLY) &&
335 device_is_dependent(consumer, supplier))) {
341 * DL_FLAG_AUTOREMOVE_SUPPLIER indicates that the link will be needed
342 * longer than for DL_FLAG_AUTOREMOVE_CONSUMER and setting them both
343 * together doesn't make sense, so prefer DL_FLAG_AUTOREMOVE_SUPPLIER.
345 if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
346 flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER;
348 list_for_each_entry(link, &supplier->links.consumers, s_node) {
349 if (link->consumer != consumer)
352 if (flags & DL_FLAG_PM_RUNTIME) {
353 if (!(link->flags & DL_FLAG_PM_RUNTIME)) {
354 pm_runtime_new_link(consumer);
355 link->flags |= DL_FLAG_PM_RUNTIME;
357 if (flags & DL_FLAG_RPM_ACTIVE)
358 refcount_inc(&link->rpm_active);
361 if (flags & DL_FLAG_STATELESS) {
362 kref_get(&link->kref);
363 if (link->flags & DL_FLAG_SYNC_STATE_ONLY &&
364 !(link->flags & DL_FLAG_STATELESS)) {
365 link->flags |= DL_FLAG_STATELESS;
373 * If the life time of the link following from the new flags is
374 * longer than indicated by the flags of the existing link,
375 * update the existing link to stay around longer.
377 if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER) {
378 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER) {
379 link->flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER;
380 link->flags |= DL_FLAG_AUTOREMOVE_SUPPLIER;
382 } else if (!(flags & DL_FLAG_AUTOREMOVE_CONSUMER)) {
383 link->flags &= ~(DL_FLAG_AUTOREMOVE_CONSUMER |
384 DL_FLAG_AUTOREMOVE_SUPPLIER);
386 if (!(link->flags & DL_FLAG_MANAGED)) {
387 kref_get(&link->kref);
388 link->flags |= DL_FLAG_MANAGED;
389 device_link_init_status(link, consumer, supplier);
391 if (link->flags & DL_FLAG_SYNC_STATE_ONLY &&
392 !(flags & DL_FLAG_SYNC_STATE_ONLY)) {
393 link->flags &= ~DL_FLAG_SYNC_STATE_ONLY;
400 link = kzalloc(sizeof(*link), GFP_KERNEL);
404 refcount_set(&link->rpm_active, 1);
406 if (flags & DL_FLAG_PM_RUNTIME) {
407 if (flags & DL_FLAG_RPM_ACTIVE)
408 refcount_inc(&link->rpm_active);
410 pm_runtime_new_link(consumer);
413 get_device(supplier);
414 link->supplier = supplier;
415 INIT_LIST_HEAD(&link->s_node);
416 get_device(consumer);
417 link->consumer = consumer;
418 INIT_LIST_HEAD(&link->c_node);
420 kref_init(&link->kref);
422 /* Determine the initial link state. */
423 if (flags & DL_FLAG_STATELESS)
424 link->status = DL_STATE_NONE;
426 device_link_init_status(link, consumer, supplier);
429 * Some callers expect the link creation during consumer driver probe to
430 * resume the supplier even without DL_FLAG_RPM_ACTIVE.
432 if (link->status == DL_STATE_CONSUMER_PROBE &&
433 flags & DL_FLAG_PM_RUNTIME)
434 pm_runtime_resume(supplier);
436 if (flags & DL_FLAG_SYNC_STATE_ONLY) {
438 "Linked as a sync state only consumer to %s\n",
444 * Move the consumer and all of the devices depending on it to the end
445 * of dpm_list and the devices_kset list.
447 * It is necessary to hold dpm_list locked throughout all that or else
448 * we may end up suspending with a wrong ordering of it.
450 device_reorder_to_tail(consumer, NULL);
452 list_add_tail_rcu(&link->s_node, &supplier->links.consumers);
453 list_add_tail_rcu(&link->c_node, &consumer->links.suppliers);
455 dev_dbg(consumer, "Linked as a consumer to %s\n", dev_name(supplier));
459 device_links_write_unlock();
461 if ((flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) && !link)
462 pm_runtime_put(supplier);
466 EXPORT_SYMBOL_GPL(device_link_add);
469 * device_link_wait_for_supplier - Add device to wait_for_suppliers list
470 * @consumer: Consumer device
472 * Marks the @consumer device as waiting for suppliers to become available by
473 * adding it to the wait_for_suppliers list. The consumer device will never be
474 * probed until it's removed from the wait_for_suppliers list.
476 * The caller is responsible for adding the links to the supplier devices once
477 * they are available and removing the @consumer device from the
478 * wait_for_suppliers list once links to all the suppliers have been created.
480 * This function is NOT meant to be called from the probe function of the
481 * consumer but rather from code that creates/adds the consumer device.
483 static void device_link_wait_for_supplier(struct device *consumer,
486 mutex_lock(&wfs_lock);
487 list_add_tail(&consumer->links.needs_suppliers, &wait_for_suppliers);
488 consumer->links.need_for_probe = need_for_probe;
489 mutex_unlock(&wfs_lock);
492 static void device_link_wait_for_mandatory_supplier(struct device *consumer)
494 device_link_wait_for_supplier(consumer, true);
497 static void device_link_wait_for_optional_supplier(struct device *consumer)
499 device_link_wait_for_supplier(consumer, false);
503 * device_link_add_missing_supplier_links - Add links from consumer devices to
504 * supplier devices, leaving any
505 * consumer with inactive suppliers on
506 * the wait_for_suppliers list
508 * Loops through all consumers waiting on suppliers and tries to add all their
509 * supplier links. If that succeeds, the consumer device is removed from
510 * wait_for_suppliers list. Otherwise, they are left in the wait_for_suppliers
511 * list. Devices left on the wait_for_suppliers list will not be probed.
513 * The fwnode add_links callback is expected to return 0 if it has found and
514 * added all the supplier links for the consumer device. It should return an
515 * error if it isn't able to do so.
517 * The caller of device_link_wait_for_supplier() is expected to call this once
518 * it's aware of potential suppliers becoming available.
520 static void device_link_add_missing_supplier_links(void)
522 struct device *dev, *tmp;
524 mutex_lock(&wfs_lock);
525 list_for_each_entry_safe(dev, tmp, &wait_for_suppliers,
526 links.needs_suppliers)
527 if (!fwnode_call_int_op(dev->fwnode, add_links, dev))
528 list_del_init(&dev->links.needs_suppliers);
529 mutex_unlock(&wfs_lock);
532 static void device_link_free(struct device_link *link)
534 while (refcount_dec_not_one(&link->rpm_active))
535 pm_runtime_put(link->supplier);
537 put_device(link->consumer);
538 put_device(link->supplier);
543 static void __device_link_free_srcu(struct rcu_head *rhead)
545 device_link_free(container_of(rhead, struct device_link, rcu_head));
548 static void __device_link_del(struct kref *kref)
550 struct device_link *link = container_of(kref, struct device_link, kref);
552 dev_dbg(link->consumer, "Dropping the link to %s\n",
553 dev_name(link->supplier));
555 if (link->flags & DL_FLAG_PM_RUNTIME)
556 pm_runtime_drop_link(link->consumer);
558 list_del_rcu(&link->s_node);
559 list_del_rcu(&link->c_node);
560 call_srcu(&device_links_srcu, &link->rcu_head, __device_link_free_srcu);
562 #else /* !CONFIG_SRCU */
563 static void __device_link_del(struct kref *kref)
565 struct device_link *link = container_of(kref, struct device_link, kref);
567 dev_info(link->consumer, "Dropping the link to %s\n",
568 dev_name(link->supplier));
570 if (link->flags & DL_FLAG_PM_RUNTIME)
571 pm_runtime_drop_link(link->consumer);
573 list_del(&link->s_node);
574 list_del(&link->c_node);
575 device_link_free(link);
577 #endif /* !CONFIG_SRCU */
579 static void device_link_put_kref(struct device_link *link)
581 if (link->flags & DL_FLAG_STATELESS)
582 kref_put(&link->kref, __device_link_del);
584 WARN(1, "Unable to drop a managed device link reference\n");
588 * device_link_del - Delete a stateless link between two devices.
589 * @link: Device link to delete.
591 * The caller must ensure proper synchronization of this function with runtime
592 * PM. If the link was added multiple times, it needs to be deleted as often.
593 * Care is required for hotplugged devices: Their links are purged on removal
594 * and calling device_link_del() is then no longer allowed.
596 void device_link_del(struct device_link *link)
598 device_links_write_lock();
600 device_link_put_kref(link);
602 device_links_write_unlock();
604 EXPORT_SYMBOL_GPL(device_link_del);
607 * device_link_remove - Delete a stateless link between two devices.
608 * @consumer: Consumer end of the link.
609 * @supplier: Supplier end of the link.
611 * The caller must ensure proper synchronization of this function with runtime
614 void device_link_remove(void *consumer, struct device *supplier)
616 struct device_link *link;
618 if (WARN_ON(consumer == supplier))
621 device_links_write_lock();
624 list_for_each_entry(link, &supplier->links.consumers, s_node) {
625 if (link->consumer == consumer) {
626 device_link_put_kref(link);
632 device_links_write_unlock();
634 EXPORT_SYMBOL_GPL(device_link_remove);
636 static void device_links_missing_supplier(struct device *dev)
638 struct device_link *link;
640 list_for_each_entry(link, &dev->links.suppliers, c_node)
641 if (link->status == DL_STATE_CONSUMER_PROBE)
642 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
646 * device_links_check_suppliers - Check presence of supplier drivers.
647 * @dev: Consumer device.
649 * Check links from this device to any suppliers. Walk the list of the device's
650 * links to suppliers and see if all of them are available. If not, simply
651 * return -EPROBE_DEFER.
653 * We need to guarantee that the supplier will not go away after the check has
654 * been positive here. It only can go away in __device_release_driver() and
655 * that function checks the device's links to consumers. This means we need to
656 * mark the link as "consumer probe in progress" to make the supplier removal
657 * wait for us to complete (or bad things may happen).
659 * Links without the DL_FLAG_MANAGED flag set are ignored.
661 int device_links_check_suppliers(struct device *dev)
663 struct device_link *link;
667 * Device waiting for supplier to become available is not allowed to
670 mutex_lock(&wfs_lock);
671 if (!list_empty(&dev->links.needs_suppliers) &&
672 dev->links.need_for_probe) {
673 mutex_unlock(&wfs_lock);
674 return -EPROBE_DEFER;
676 mutex_unlock(&wfs_lock);
678 device_links_write_lock();
680 list_for_each_entry(link, &dev->links.suppliers, c_node) {
681 if (!(link->flags & DL_FLAG_MANAGED) ||
682 link->flags & DL_FLAG_SYNC_STATE_ONLY)
685 if (link->status != DL_STATE_AVAILABLE) {
686 device_links_missing_supplier(dev);
690 WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE);
692 dev->links.status = DL_DEV_PROBING;
694 device_links_write_unlock();
699 * __device_links_queue_sync_state - Queue a device for sync_state() callback
700 * @dev: Device to call sync_state() on
701 * @list: List head to queue the @dev on
703 * Queues a device for a sync_state() callback when the device links write lock
704 * isn't held. This allows the sync_state() execution flow to use device links
705 * APIs. The caller must ensure this function is called with
706 * device_links_write_lock() held.
708 * This function does a get_device() to make sure the device is not freed while
711 * So the caller must also ensure that device_links_flush_sync_list() is called
712 * as soon as the caller releases device_links_write_lock(). This is necessary
713 * to make sure the sync_state() is called in a timely fashion and the
714 * put_device() is called on this device.
716 static void __device_links_queue_sync_state(struct device *dev,
717 struct list_head *list)
719 struct device_link *link;
721 if (!dev_has_sync_state(dev))
723 if (dev->state_synced)
726 list_for_each_entry(link, &dev->links.consumers, s_node) {
727 if (!(link->flags & DL_FLAG_MANAGED))
729 if (link->status != DL_STATE_ACTIVE)
734 * Set the flag here to avoid adding the same device to a list more
735 * than once. This can happen if new consumers get added to the device
736 * and probed before the list is flushed.
738 dev->state_synced = true;
740 if (WARN_ON(!list_empty(&dev->links.defer_sync)))
744 list_add_tail(&dev->links.defer_sync, list);
748 * device_links_flush_sync_list - Call sync_state() on a list of devices
749 * @list: List of devices to call sync_state() on
750 * @dont_lock_dev: Device for which lock is already held by the caller
752 * Calls sync_state() on all the devices that have been queued for it. This
753 * function is used in conjunction with __device_links_queue_sync_state(). The
754 * @dont_lock_dev parameter is useful when this function is called from a
755 * context where a device lock is already held.
757 static void device_links_flush_sync_list(struct list_head *list,
758 struct device *dont_lock_dev)
760 struct device *dev, *tmp;
762 list_for_each_entry_safe(dev, tmp, list, links.defer_sync) {
763 list_del_init(&dev->links.defer_sync);
765 if (dev != dont_lock_dev)
768 if (dev->bus->sync_state)
769 dev->bus->sync_state(dev);
770 else if (dev->driver && dev->driver->sync_state)
771 dev->driver->sync_state(dev);
773 if (dev != dont_lock_dev)
780 void device_links_supplier_sync_state_pause(void)
782 device_links_write_lock();
783 defer_sync_state_count++;
784 device_links_write_unlock();
787 void device_links_supplier_sync_state_resume(void)
789 struct device *dev, *tmp;
790 LIST_HEAD(sync_list);
792 device_links_write_lock();
793 if (!defer_sync_state_count) {
794 WARN(true, "Unmatched sync_state pause/resume!");
797 defer_sync_state_count--;
798 if (defer_sync_state_count)
801 list_for_each_entry_safe(dev, tmp, &deferred_sync, links.defer_sync) {
803 * Delete from deferred_sync list before queuing it to
804 * sync_list because defer_sync is used for both lists.
806 list_del_init(&dev->links.defer_sync);
807 __device_links_queue_sync_state(dev, &sync_list);
810 device_links_write_unlock();
812 device_links_flush_sync_list(&sync_list, NULL);
815 static int sync_state_resume_initcall(void)
817 device_links_supplier_sync_state_resume();
820 late_initcall(sync_state_resume_initcall);
822 static void __device_links_supplier_defer_sync(struct device *sup)
824 if (list_empty(&sup->links.defer_sync) && dev_has_sync_state(sup))
825 list_add_tail(&sup->links.defer_sync, &deferred_sync);
829 * device_links_driver_bound - Update device links after probing its driver.
830 * @dev: Device to update the links for.
832 * The probe has been successful, so update links from this device to any
833 * consumers by changing their status to "available".
835 * Also change the status of @dev's links to suppliers to "active".
837 * Links without the DL_FLAG_MANAGED flag set are ignored.
839 void device_links_driver_bound(struct device *dev)
841 struct device_link *link;
842 LIST_HEAD(sync_list);
845 * If a device probes successfully, it's expected to have created all
846 * the device links it needs to or make new device links as it needs
847 * them. So, it no longer needs to wait on any suppliers.
849 mutex_lock(&wfs_lock);
850 list_del_init(&dev->links.needs_suppliers);
851 mutex_unlock(&wfs_lock);
853 device_links_write_lock();
855 list_for_each_entry(link, &dev->links.consumers, s_node) {
856 if (!(link->flags & DL_FLAG_MANAGED))
860 * Links created during consumer probe may be in the "consumer
861 * probe" state to start with if the supplier is still probing
862 * when they are created and they may become "active" if the
863 * consumer probe returns first. Skip them here.
865 if (link->status == DL_STATE_CONSUMER_PROBE ||
866 link->status == DL_STATE_ACTIVE)
869 WARN_ON(link->status != DL_STATE_DORMANT);
870 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
872 if (link->flags & DL_FLAG_AUTOPROBE_CONSUMER)
873 driver_deferred_probe_add(link->consumer);
876 if (defer_sync_state_count)
877 __device_links_supplier_defer_sync(dev);
879 __device_links_queue_sync_state(dev, &sync_list);
881 list_for_each_entry(link, &dev->links.suppliers, c_node) {
882 if (!(link->flags & DL_FLAG_MANAGED))
885 WARN_ON(link->status != DL_STATE_CONSUMER_PROBE);
886 WRITE_ONCE(link->status, DL_STATE_ACTIVE);
888 if (defer_sync_state_count)
889 __device_links_supplier_defer_sync(link->supplier);
891 __device_links_queue_sync_state(link->supplier,
895 dev->links.status = DL_DEV_DRIVER_BOUND;
897 device_links_write_unlock();
899 device_links_flush_sync_list(&sync_list, dev);
902 static void device_link_drop_managed(struct device_link *link)
904 link->flags &= ~DL_FLAG_MANAGED;
905 WRITE_ONCE(link->status, DL_STATE_NONE);
906 kref_put(&link->kref, __device_link_del);
910 * __device_links_no_driver - Update links of a device without a driver.
911 * @dev: Device without a drvier.
913 * Delete all non-persistent links from this device to any suppliers.
915 * Persistent links stay around, but their status is changed to "available",
916 * unless they already are in the "supplier unbind in progress" state in which
917 * case they need not be updated.
919 * Links without the DL_FLAG_MANAGED flag set are ignored.
921 static void __device_links_no_driver(struct device *dev)
923 struct device_link *link, *ln;
925 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
926 if (!(link->flags & DL_FLAG_MANAGED))
929 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER)
930 device_link_drop_managed(link);
931 else if (link->status == DL_STATE_CONSUMER_PROBE ||
932 link->status == DL_STATE_ACTIVE)
933 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
936 dev->links.status = DL_DEV_NO_DRIVER;
940 * device_links_no_driver - Update links after failing driver probe.
941 * @dev: Device whose driver has just failed to probe.
943 * Clean up leftover links to consumers for @dev and invoke
944 * %__device_links_no_driver() to update links to suppliers for it as
947 * Links without the DL_FLAG_MANAGED flag set are ignored.
949 void device_links_no_driver(struct device *dev)
951 struct device_link *link;
953 device_links_write_lock();
955 list_for_each_entry(link, &dev->links.consumers, s_node) {
956 if (!(link->flags & DL_FLAG_MANAGED))
960 * The probe has failed, so if the status of the link is
961 * "consumer probe" or "active", it must have been added by
962 * a probing consumer while this device was still probing.
963 * Change its state to "dormant", as it represents a valid
964 * relationship, but it is not functionally meaningful.
966 if (link->status == DL_STATE_CONSUMER_PROBE ||
967 link->status == DL_STATE_ACTIVE)
968 WRITE_ONCE(link->status, DL_STATE_DORMANT);
971 __device_links_no_driver(dev);
973 device_links_write_unlock();
977 * device_links_driver_cleanup - Update links after driver removal.
978 * @dev: Device whose driver has just gone away.
980 * Update links to consumers for @dev by changing their status to "dormant" and
981 * invoke %__device_links_no_driver() to update links to suppliers for it as
984 * Links without the DL_FLAG_MANAGED flag set are ignored.
986 void device_links_driver_cleanup(struct device *dev)
988 struct device_link *link, *ln;
990 device_links_write_lock();
992 list_for_each_entry_safe(link, ln, &dev->links.consumers, s_node) {
993 if (!(link->flags & DL_FLAG_MANAGED))
996 WARN_ON(link->flags & DL_FLAG_AUTOREMOVE_CONSUMER);
997 WARN_ON(link->status != DL_STATE_SUPPLIER_UNBIND);
1000 * autoremove the links between this @dev and its consumer
1001 * devices that are not active, i.e. where the link state
1002 * has moved to DL_STATE_SUPPLIER_UNBIND.
1004 if (link->status == DL_STATE_SUPPLIER_UNBIND &&
1005 link->flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
1006 device_link_drop_managed(link);
1008 WRITE_ONCE(link->status, DL_STATE_DORMANT);
1011 list_del_init(&dev->links.defer_sync);
1012 __device_links_no_driver(dev);
1014 device_links_write_unlock();
1018 * device_links_busy - Check if there are any busy links to consumers.
1019 * @dev: Device to check.
1021 * Check each consumer of the device and return 'true' if its link's status
1022 * is one of "consumer probe" or "active" (meaning that the given consumer is
1023 * probing right now or its driver is present). Otherwise, change the link
1024 * state to "supplier unbind" to prevent the consumer from being probed
1025 * successfully going forward.
1027 * Return 'false' if there are no probing or active consumers.
1029 * Links without the DL_FLAG_MANAGED flag set are ignored.
1031 bool device_links_busy(struct device *dev)
1033 struct device_link *link;
1036 device_links_write_lock();
1038 list_for_each_entry(link, &dev->links.consumers, s_node) {
1039 if (!(link->flags & DL_FLAG_MANAGED))
1042 if (link->status == DL_STATE_CONSUMER_PROBE
1043 || link->status == DL_STATE_ACTIVE) {
1047 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
1050 dev->links.status = DL_DEV_UNBINDING;
1052 device_links_write_unlock();
1057 * device_links_unbind_consumers - Force unbind consumers of the given device.
1058 * @dev: Device to unbind the consumers of.
1060 * Walk the list of links to consumers for @dev and if any of them is in the
1061 * "consumer probe" state, wait for all device probes in progress to complete
1064 * If that's not the case, change the status of the link to "supplier unbind"
1065 * and check if the link was in the "active" state. If so, force the consumer
1066 * driver to unbind and start over (the consumer will not re-probe as we have
1067 * changed the state of the link already).
1069 * Links without the DL_FLAG_MANAGED flag set are ignored.
1071 void device_links_unbind_consumers(struct device *dev)
1073 struct device_link *link;
1076 device_links_write_lock();
1078 list_for_each_entry(link, &dev->links.consumers, s_node) {
1079 enum device_link_state status;
1081 if (!(link->flags & DL_FLAG_MANAGED) ||
1082 link->flags & DL_FLAG_SYNC_STATE_ONLY)
1085 status = link->status;
1086 if (status == DL_STATE_CONSUMER_PROBE) {
1087 device_links_write_unlock();
1089 wait_for_device_probe();
1092 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
1093 if (status == DL_STATE_ACTIVE) {
1094 struct device *consumer = link->consumer;
1096 get_device(consumer);
1098 device_links_write_unlock();
1100 device_release_driver_internal(consumer, NULL,
1102 put_device(consumer);
1107 device_links_write_unlock();
1111 * device_links_purge - Delete existing links to other devices.
1112 * @dev: Target device.
1114 static void device_links_purge(struct device *dev)
1116 struct device_link *link, *ln;
1118 mutex_lock(&wfs_lock);
1119 list_del(&dev->links.needs_suppliers);
1120 mutex_unlock(&wfs_lock);
1123 * Delete all of the remaining links from this device to any other
1124 * devices (either consumers or suppliers).
1126 device_links_write_lock();
1128 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
1129 WARN_ON(link->status == DL_STATE_ACTIVE);
1130 __device_link_del(&link->kref);
1133 list_for_each_entry_safe_reverse(link, ln, &dev->links.consumers, s_node) {
1134 WARN_ON(link->status != DL_STATE_DORMANT &&
1135 link->status != DL_STATE_NONE);
1136 __device_link_del(&link->kref);
1139 device_links_write_unlock();
1142 /* Device links support end. */
1144 int (*platform_notify)(struct device *dev) = NULL;
1145 int (*platform_notify_remove)(struct device *dev) = NULL;
1146 static struct kobject *dev_kobj;
1147 struct kobject *sysfs_dev_char_kobj;
1148 struct kobject *sysfs_dev_block_kobj;
1150 static DEFINE_MUTEX(device_hotplug_lock);
1152 void lock_device_hotplug(void)
1154 mutex_lock(&device_hotplug_lock);
1157 void unlock_device_hotplug(void)
1159 mutex_unlock(&device_hotplug_lock);
1162 int lock_device_hotplug_sysfs(void)
1164 if (mutex_trylock(&device_hotplug_lock))
1167 /* Avoid busy looping (5 ms of sleep should do). */
1169 return restart_syscall();
1173 static inline int device_is_not_partition(struct device *dev)
1175 return !(dev->type == &part_type);
1178 static inline int device_is_not_partition(struct device *dev)
1185 device_platform_notify(struct device *dev, enum kobject_action action)
1189 ret = acpi_platform_notify(dev, action);
1193 ret = software_node_notify(dev, action);
1197 if (platform_notify && action == KOBJ_ADD)
1198 platform_notify(dev);
1199 else if (platform_notify_remove && action == KOBJ_REMOVE)
1200 platform_notify_remove(dev);
1205 * dev_driver_string - Return a device's driver name, if at all possible
1206 * @dev: struct device to get the name of
1208 * Will return the device's driver's name if it is bound to a device. If
1209 * the device is not bound to a driver, it will return the name of the bus
1210 * it is attached to. If it is not attached to a bus either, an empty
1211 * string will be returned.
1213 const char *dev_driver_string(const struct device *dev)
1215 struct device_driver *drv;
1217 /* dev->driver can change to NULL underneath us because of unbinding,
1218 * so be careful about accessing it. dev->bus and dev->class should
1219 * never change once they are set, so they don't need special care.
1221 drv = READ_ONCE(dev->driver);
1222 return drv ? drv->name :
1223 (dev->bus ? dev->bus->name :
1224 (dev->class ? dev->class->name : ""));
1226 EXPORT_SYMBOL(dev_driver_string);
1228 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
1230 static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
1233 struct device_attribute *dev_attr = to_dev_attr(attr);
1234 struct device *dev = kobj_to_dev(kobj);
1238 ret = dev_attr->show(dev, dev_attr, buf);
1239 if (ret >= (ssize_t)PAGE_SIZE) {
1240 printk("dev_attr_show: %pS returned bad count\n",
1246 static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
1247 const char *buf, size_t count)
1249 struct device_attribute *dev_attr = to_dev_attr(attr);
1250 struct device *dev = kobj_to_dev(kobj);
1253 if (dev_attr->store)
1254 ret = dev_attr->store(dev, dev_attr, buf, count);
1258 static const struct sysfs_ops dev_sysfs_ops = {
1259 .show = dev_attr_show,
1260 .store = dev_attr_store,
1263 #define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
1265 ssize_t device_store_ulong(struct device *dev,
1266 struct device_attribute *attr,
1267 const char *buf, size_t size)
1269 struct dev_ext_attribute *ea = to_ext_attr(attr);
1273 ret = kstrtoul(buf, 0, &new);
1276 *(unsigned long *)(ea->var) = new;
1277 /* Always return full write size even if we didn't consume all */
1280 EXPORT_SYMBOL_GPL(device_store_ulong);
1282 ssize_t device_show_ulong(struct device *dev,
1283 struct device_attribute *attr,
1286 struct dev_ext_attribute *ea = to_ext_attr(attr);
1287 return snprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var));
1289 EXPORT_SYMBOL_GPL(device_show_ulong);
1291 ssize_t device_store_int(struct device *dev,
1292 struct device_attribute *attr,
1293 const char *buf, size_t size)
1295 struct dev_ext_attribute *ea = to_ext_attr(attr);
1299 ret = kstrtol(buf, 0, &new);
1303 if (new > INT_MAX || new < INT_MIN)
1305 *(int *)(ea->var) = new;
1306 /* Always return full write size even if we didn't consume all */
1309 EXPORT_SYMBOL_GPL(device_store_int);
1311 ssize_t device_show_int(struct device *dev,
1312 struct device_attribute *attr,
1315 struct dev_ext_attribute *ea = to_ext_attr(attr);
1317 return snprintf(buf, PAGE_SIZE, "%d\n", *(int *)(ea->var));
1319 EXPORT_SYMBOL_GPL(device_show_int);
1321 ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
1322 const char *buf, size_t size)
1324 struct dev_ext_attribute *ea = to_ext_attr(attr);
1326 if (strtobool(buf, ea->var) < 0)
1331 EXPORT_SYMBOL_GPL(device_store_bool);
1333 ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
1336 struct dev_ext_attribute *ea = to_ext_attr(attr);
1338 return snprintf(buf, PAGE_SIZE, "%d\n", *(bool *)(ea->var));
1340 EXPORT_SYMBOL_GPL(device_show_bool);
1343 * device_release - free device structure.
1344 * @kobj: device's kobject.
1346 * This is called once the reference count for the object
1347 * reaches 0. We forward the call to the device's release
1348 * method, which should handle actually freeing the structure.
1350 static void device_release(struct kobject *kobj)
1352 struct device *dev = kobj_to_dev(kobj);
1353 struct device_private *p = dev->p;
1356 * Some platform devices are driven without driver attached
1357 * and managed resources may have been acquired. Make sure
1358 * all resources are released.
1360 * Drivers still can add resources into device after device
1361 * is deleted but alive, so release devres here to avoid
1362 * possible memory leak.
1364 devres_release_all(dev);
1368 else if (dev->type && dev->type->release)
1369 dev->type->release(dev);
1370 else if (dev->class && dev->class->dev_release)
1371 dev->class->dev_release(dev);
1373 WARN(1, KERN_ERR "Device '%s' does not have a release() function, it is broken and must be fixed. See Documentation/kobject.txt.\n",
1378 static const void *device_namespace(struct kobject *kobj)
1380 struct device *dev = kobj_to_dev(kobj);
1381 const void *ns = NULL;
1383 if (dev->class && dev->class->ns_type)
1384 ns = dev->class->namespace(dev);
1389 static void device_get_ownership(struct kobject *kobj, kuid_t *uid, kgid_t *gid)
1391 struct device *dev = kobj_to_dev(kobj);
1393 if (dev->class && dev->class->get_ownership)
1394 dev->class->get_ownership(dev, uid, gid);
1397 static struct kobj_type device_ktype = {
1398 .release = device_release,
1399 .sysfs_ops = &dev_sysfs_ops,
1400 .namespace = device_namespace,
1401 .get_ownership = device_get_ownership,
1405 static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
1407 struct kobj_type *ktype = get_ktype(kobj);
1409 if (ktype == &device_ktype) {
1410 struct device *dev = kobj_to_dev(kobj);
1419 static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
1421 struct device *dev = kobj_to_dev(kobj);
1424 return dev->bus->name;
1426 return dev->class->name;
1430 static int dev_uevent(struct kset *kset, struct kobject *kobj,
1431 struct kobj_uevent_env *env)
1433 struct device *dev = kobj_to_dev(kobj);
1436 /* add device node properties if present */
1437 if (MAJOR(dev->devt)) {
1441 kuid_t uid = GLOBAL_ROOT_UID;
1442 kgid_t gid = GLOBAL_ROOT_GID;
1444 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
1445 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
1446 name = device_get_devnode(dev, &mode, &uid, &gid, &tmp);
1448 add_uevent_var(env, "DEVNAME=%s", name);
1450 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
1451 if (!uid_eq(uid, GLOBAL_ROOT_UID))
1452 add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid));
1453 if (!gid_eq(gid, GLOBAL_ROOT_GID))
1454 add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid));
1459 if (dev->type && dev->type->name)
1460 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
1463 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
1465 /* Add common DT information about the device */
1466 of_device_uevent(dev, env);
1468 /* have the bus specific function add its stuff */
1469 if (dev->bus && dev->bus->uevent) {
1470 retval = dev->bus->uevent(dev, env);
1472 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
1473 dev_name(dev), __func__, retval);
1476 /* have the class specific function add its stuff */
1477 if (dev->class && dev->class->dev_uevent) {
1478 retval = dev->class->dev_uevent(dev, env);
1480 pr_debug("device: '%s': %s: class uevent() "
1481 "returned %d\n", dev_name(dev),
1485 /* have the device type specific function add its stuff */
1486 if (dev->type && dev->type->uevent) {
1487 retval = dev->type->uevent(dev, env);
1489 pr_debug("device: '%s': %s: dev_type uevent() "
1490 "returned %d\n", dev_name(dev),
1497 static const struct kset_uevent_ops device_uevent_ops = {
1498 .filter = dev_uevent_filter,
1499 .name = dev_uevent_name,
1500 .uevent = dev_uevent,
1503 static ssize_t uevent_show(struct device *dev, struct device_attribute *attr,
1506 struct kobject *top_kobj;
1508 struct kobj_uevent_env *env = NULL;
1513 /* search the kset, the device belongs to */
1514 top_kobj = &dev->kobj;
1515 while (!top_kobj->kset && top_kobj->parent)
1516 top_kobj = top_kobj->parent;
1517 if (!top_kobj->kset)
1520 kset = top_kobj->kset;
1521 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
1524 /* respect filter */
1525 if (kset->uevent_ops && kset->uevent_ops->filter)
1526 if (!kset->uevent_ops->filter(kset, &dev->kobj))
1529 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
1533 /* let the kset specific function add its keys */
1534 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
1538 /* copy keys to file */
1539 for (i = 0; i < env->envp_idx; i++)
1540 count += sprintf(&buf[count], "%s\n", env->envp[i]);
1546 static ssize_t uevent_store(struct device *dev, struct device_attribute *attr,
1547 const char *buf, size_t count)
1551 rc = kobject_synth_uevent(&dev->kobj, buf, count);
1554 dev_err(dev, "uevent: failed to send synthetic uevent\n");
1560 static DEVICE_ATTR_RW(uevent);
1562 static ssize_t online_show(struct device *dev, struct device_attribute *attr,
1568 val = !dev->offline;
1570 return sprintf(buf, "%u\n", val);
1573 static ssize_t online_store(struct device *dev, struct device_attribute *attr,
1574 const char *buf, size_t count)
1579 ret = strtobool(buf, &val);
1583 ret = lock_device_hotplug_sysfs();
1587 ret = val ? device_online(dev) : device_offline(dev);
1588 unlock_device_hotplug();
1589 return ret < 0 ? ret : count;
1591 static DEVICE_ATTR_RW(online);
1593 int device_add_groups(struct device *dev, const struct attribute_group **groups)
1595 return sysfs_create_groups(&dev->kobj, groups);
1597 EXPORT_SYMBOL_GPL(device_add_groups);
1599 void device_remove_groups(struct device *dev,
1600 const struct attribute_group **groups)
1602 sysfs_remove_groups(&dev->kobj, groups);
1604 EXPORT_SYMBOL_GPL(device_remove_groups);
1606 union device_attr_group_devres {
1607 const struct attribute_group *group;
1608 const struct attribute_group **groups;
1611 static int devm_attr_group_match(struct device *dev, void *res, void *data)
1613 return ((union device_attr_group_devres *)res)->group == data;
1616 static void devm_attr_group_remove(struct device *dev, void *res)
1618 union device_attr_group_devres *devres = res;
1619 const struct attribute_group *group = devres->group;
1621 dev_dbg(dev, "%s: removing group %p\n", __func__, group);
1622 sysfs_remove_group(&dev->kobj, group);
1625 static void devm_attr_groups_remove(struct device *dev, void *res)
1627 union device_attr_group_devres *devres = res;
1628 const struct attribute_group **groups = devres->groups;
1630 dev_dbg(dev, "%s: removing groups %p\n", __func__, groups);
1631 sysfs_remove_groups(&dev->kobj, groups);
1635 * devm_device_add_group - given a device, create a managed attribute group
1636 * @dev: The device to create the group for
1637 * @grp: The attribute group to create
1639 * This function creates a group for the first time. It will explicitly
1640 * warn and error if any of the attribute files being created already exist.
1642 * Returns 0 on success or error code on failure.
1644 int devm_device_add_group(struct device *dev, const struct attribute_group *grp)
1646 union device_attr_group_devres *devres;
1649 devres = devres_alloc(devm_attr_group_remove,
1650 sizeof(*devres), GFP_KERNEL);
1654 error = sysfs_create_group(&dev->kobj, grp);
1656 devres_free(devres);
1660 devres->group = grp;
1661 devres_add(dev, devres);
1664 EXPORT_SYMBOL_GPL(devm_device_add_group);
1667 * devm_device_remove_group: remove a managed group from a device
1668 * @dev: device to remove the group from
1669 * @grp: group to remove
1671 * This function removes a group of attributes from a device. The attributes
1672 * previously have to have been created for this group, otherwise it will fail.
1674 void devm_device_remove_group(struct device *dev,
1675 const struct attribute_group *grp)
1677 WARN_ON(devres_release(dev, devm_attr_group_remove,
1678 devm_attr_group_match,
1679 /* cast away const */ (void *)grp));
1681 EXPORT_SYMBOL_GPL(devm_device_remove_group);
1684 * devm_device_add_groups - create a bunch of managed attribute groups
1685 * @dev: The device to create the group for
1686 * @groups: The attribute groups to create, NULL terminated
1688 * This function creates a bunch of managed attribute groups. If an error
1689 * occurs when creating a group, all previously created groups will be
1690 * removed, unwinding everything back to the original state when this
1691 * function was called. It will explicitly warn and error if any of the
1692 * attribute files being created already exist.
1694 * Returns 0 on success or error code from sysfs_create_group on failure.
1696 int devm_device_add_groups(struct device *dev,
1697 const struct attribute_group **groups)
1699 union device_attr_group_devres *devres;
1702 devres = devres_alloc(devm_attr_groups_remove,
1703 sizeof(*devres), GFP_KERNEL);
1707 error = sysfs_create_groups(&dev->kobj, groups);
1709 devres_free(devres);
1713 devres->groups = groups;
1714 devres_add(dev, devres);
1717 EXPORT_SYMBOL_GPL(devm_device_add_groups);
1720 * devm_device_remove_groups - remove a list of managed groups
1722 * @dev: The device for the groups to be removed from
1723 * @groups: NULL terminated list of groups to be removed
1725 * If groups is not NULL, remove the specified groups from the device.
1727 void devm_device_remove_groups(struct device *dev,
1728 const struct attribute_group **groups)
1730 WARN_ON(devres_release(dev, devm_attr_groups_remove,
1731 devm_attr_group_match,
1732 /* cast away const */ (void *)groups));
1734 EXPORT_SYMBOL_GPL(devm_device_remove_groups);
1736 static int device_add_attrs(struct device *dev)
1738 struct class *class = dev->class;
1739 const struct device_type *type = dev->type;
1743 error = device_add_groups(dev, class->dev_groups);
1749 error = device_add_groups(dev, type->groups);
1751 goto err_remove_class_groups;
1754 error = device_add_groups(dev, dev->groups);
1756 goto err_remove_type_groups;
1758 if (device_supports_offline(dev) && !dev->offline_disabled) {
1759 error = device_create_file(dev, &dev_attr_online);
1761 goto err_remove_dev_groups;
1766 err_remove_dev_groups:
1767 device_remove_groups(dev, dev->groups);
1768 err_remove_type_groups:
1770 device_remove_groups(dev, type->groups);
1771 err_remove_class_groups:
1773 device_remove_groups(dev, class->dev_groups);
1778 static void device_remove_attrs(struct device *dev)
1780 struct class *class = dev->class;
1781 const struct device_type *type = dev->type;
1783 device_remove_file(dev, &dev_attr_online);
1784 device_remove_groups(dev, dev->groups);
1787 device_remove_groups(dev, type->groups);
1790 device_remove_groups(dev, class->dev_groups);
1793 static ssize_t dev_show(struct device *dev, struct device_attribute *attr,
1796 return print_dev_t(buf, dev->devt);
1798 static DEVICE_ATTR_RO(dev);
1801 struct kset *devices_kset;
1804 * devices_kset_move_before - Move device in the devices_kset's list.
1805 * @deva: Device to move.
1806 * @devb: Device @deva should come before.
1808 static void devices_kset_move_before(struct device *deva, struct device *devb)
1812 pr_debug("devices_kset: Moving %s before %s\n",
1813 dev_name(deva), dev_name(devb));
1814 spin_lock(&devices_kset->list_lock);
1815 list_move_tail(&deva->kobj.entry, &devb->kobj.entry);
1816 spin_unlock(&devices_kset->list_lock);
1820 * devices_kset_move_after - Move device in the devices_kset's list.
1821 * @deva: Device to move
1822 * @devb: Device @deva should come after.
1824 static void devices_kset_move_after(struct device *deva, struct device *devb)
1828 pr_debug("devices_kset: Moving %s after %s\n",
1829 dev_name(deva), dev_name(devb));
1830 spin_lock(&devices_kset->list_lock);
1831 list_move(&deva->kobj.entry, &devb->kobj.entry);
1832 spin_unlock(&devices_kset->list_lock);
1836 * devices_kset_move_last - move the device to the end of devices_kset's list.
1837 * @dev: device to move
1839 void devices_kset_move_last(struct device *dev)
1843 pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev));
1844 spin_lock(&devices_kset->list_lock);
1845 list_move_tail(&dev->kobj.entry, &devices_kset->list);
1846 spin_unlock(&devices_kset->list_lock);
1850 * device_create_file - create sysfs attribute file for device.
1852 * @attr: device attribute descriptor.
1854 int device_create_file(struct device *dev,
1855 const struct device_attribute *attr)
1860 WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
1861 "Attribute %s: write permission without 'store'\n",
1863 WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
1864 "Attribute %s: read permission without 'show'\n",
1866 error = sysfs_create_file(&dev->kobj, &attr->attr);
1871 EXPORT_SYMBOL_GPL(device_create_file);
1874 * device_remove_file - remove sysfs attribute file.
1876 * @attr: device attribute descriptor.
1878 void device_remove_file(struct device *dev,
1879 const struct device_attribute *attr)
1882 sysfs_remove_file(&dev->kobj, &attr->attr);
1884 EXPORT_SYMBOL_GPL(device_remove_file);
1887 * device_remove_file_self - remove sysfs attribute file from its own method.
1889 * @attr: device attribute descriptor.
1891 * See kernfs_remove_self() for details.
1893 bool device_remove_file_self(struct device *dev,
1894 const struct device_attribute *attr)
1897 return sysfs_remove_file_self(&dev->kobj, &attr->attr);
1901 EXPORT_SYMBOL_GPL(device_remove_file_self);
1904 * device_create_bin_file - create sysfs binary attribute file for device.
1906 * @attr: device binary attribute descriptor.
1908 int device_create_bin_file(struct device *dev,
1909 const struct bin_attribute *attr)
1911 int error = -EINVAL;
1913 error = sysfs_create_bin_file(&dev->kobj, attr);
1916 EXPORT_SYMBOL_GPL(device_create_bin_file);
1919 * device_remove_bin_file - remove sysfs binary attribute file
1921 * @attr: device binary attribute descriptor.
1923 void device_remove_bin_file(struct device *dev,
1924 const struct bin_attribute *attr)
1927 sysfs_remove_bin_file(&dev->kobj, attr);
1929 EXPORT_SYMBOL_GPL(device_remove_bin_file);
1931 static void klist_children_get(struct klist_node *n)
1933 struct device_private *p = to_device_private_parent(n);
1934 struct device *dev = p->device;
1939 static void klist_children_put(struct klist_node *n)
1941 struct device_private *p = to_device_private_parent(n);
1942 struct device *dev = p->device;
1948 * device_initialize - init device structure.
1951 * This prepares the device for use by other layers by initializing
1953 * It is the first half of device_register(), if called by
1954 * that function, though it can also be called separately, so one
1955 * may use @dev's fields. In particular, get_device()/put_device()
1956 * may be used for reference counting of @dev after calling this
1959 * All fields in @dev must be initialized by the caller to 0, except
1960 * for those explicitly set to some other value. The simplest
1961 * approach is to use kzalloc() to allocate the structure containing
1964 * NOTE: Use put_device() to give up your reference instead of freeing
1965 * @dev directly once you have called this function.
1967 void device_initialize(struct device *dev)
1969 dev->kobj.kset = devices_kset;
1970 kobject_init(&dev->kobj, &device_ktype);
1971 INIT_LIST_HEAD(&dev->dma_pools);
1972 mutex_init(&dev->mutex);
1973 #ifdef CONFIG_PROVE_LOCKING
1974 mutex_init(&dev->lockdep_mutex);
1976 lockdep_set_novalidate_class(&dev->mutex);
1977 spin_lock_init(&dev->devres_lock);
1978 INIT_LIST_HEAD(&dev->devres_head);
1979 device_pm_init(dev);
1980 set_dev_node(dev, -1);
1981 #ifdef CONFIG_GENERIC_MSI_IRQ
1982 INIT_LIST_HEAD(&dev->msi_list);
1984 INIT_LIST_HEAD(&dev->links.consumers);
1985 INIT_LIST_HEAD(&dev->links.suppliers);
1986 INIT_LIST_HEAD(&dev->links.needs_suppliers);
1987 INIT_LIST_HEAD(&dev->links.defer_sync);
1988 dev->links.status = DL_DEV_NO_DRIVER;
1990 EXPORT_SYMBOL_GPL(device_initialize);
1992 struct kobject *virtual_device_parent(struct device *dev)
1994 static struct kobject *virtual_dir = NULL;
1997 virtual_dir = kobject_create_and_add("virtual",
1998 &devices_kset->kobj);
2004 struct kobject kobj;
2005 struct class *class;
2008 #define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
2010 static void class_dir_release(struct kobject *kobj)
2012 struct class_dir *dir = to_class_dir(kobj);
2017 struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
2019 struct class_dir *dir = to_class_dir(kobj);
2020 return dir->class->ns_type;
2023 static struct kobj_type class_dir_ktype = {
2024 .release = class_dir_release,
2025 .sysfs_ops = &kobj_sysfs_ops,
2026 .child_ns_type = class_dir_child_ns_type
2029 static struct kobject *
2030 class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
2032 struct class_dir *dir;
2035 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
2037 return ERR_PTR(-ENOMEM);
2040 kobject_init(&dir->kobj, &class_dir_ktype);
2042 dir->kobj.kset = &class->p->glue_dirs;
2044 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
2046 kobject_put(&dir->kobj);
2047 return ERR_PTR(retval);
2052 static DEFINE_MUTEX(gdp_mutex);
2054 static struct kobject *get_device_parent(struct device *dev,
2055 struct device *parent)
2058 struct kobject *kobj = NULL;
2059 struct kobject *parent_kobj;
2063 /* block disks show up in /sys/block */
2064 if (sysfs_deprecated && dev->class == &block_class) {
2065 if (parent && parent->class == &block_class)
2066 return &parent->kobj;
2067 return &block_class.p->subsys.kobj;
2072 * If we have no parent, we live in "virtual".
2073 * Class-devices with a non class-device as parent, live
2074 * in a "glue" directory to prevent namespace collisions.
2077 parent_kobj = virtual_device_parent(dev);
2078 else if (parent->class && !dev->class->ns_type)
2079 return &parent->kobj;
2081 parent_kobj = &parent->kobj;
2083 mutex_lock(&gdp_mutex);
2085 /* find our class-directory at the parent and reference it */
2086 spin_lock(&dev->class->p->glue_dirs.list_lock);
2087 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
2088 if (k->parent == parent_kobj) {
2089 kobj = kobject_get(k);
2092 spin_unlock(&dev->class->p->glue_dirs.list_lock);
2094 mutex_unlock(&gdp_mutex);
2098 /* or create a new class-directory at the parent device */
2099 k = class_dir_create_and_add(dev->class, parent_kobj);
2100 /* do not emit an uevent for this simple "glue" directory */
2101 mutex_unlock(&gdp_mutex);
2105 /* subsystems can specify a default root directory for their devices */
2106 if (!parent && dev->bus && dev->bus->dev_root)
2107 return &dev->bus->dev_root->kobj;
2110 return &parent->kobj;
2114 static inline bool live_in_glue_dir(struct kobject *kobj,
2117 if (!kobj || !dev->class ||
2118 kobj->kset != &dev->class->p->glue_dirs)
2123 static inline struct kobject *get_glue_dir(struct device *dev)
2125 return dev->kobj.parent;
2129 * make sure cleaning up dir as the last step, we need to make
2130 * sure .release handler of kobject is run with holding the
2133 static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
2137 /* see if we live in a "glue" directory */
2138 if (!live_in_glue_dir(glue_dir, dev))
2141 mutex_lock(&gdp_mutex);
2143 * There is a race condition between removing glue directory
2144 * and adding a new device under the glue directory.
2149 * get_device_parent()
2150 * class_dir_create_and_add()
2151 * kobject_add_internal()
2152 * create_dir() // create glue_dir
2155 * get_device_parent()
2156 * kobject_get() // get glue_dir
2159 * cleanup_glue_dir()
2160 * kobject_del(glue_dir)
2163 * kobject_add_internal()
2164 * create_dir() // in glue_dir
2165 * sysfs_create_dir_ns()
2166 * kernfs_create_dir_ns(sd)
2168 * sysfs_remove_dir() // glue_dir->sd=NULL
2169 * sysfs_put() // free glue_dir->sd
2172 * kernfs_new_node(sd)
2173 * kernfs_get(glue_dir)
2177 * Before CPU1 remove last child device under glue dir, if CPU2 add
2178 * a new device under glue dir, the glue_dir kobject reference count
2179 * will be increase to 2 in kobject_get(k). And CPU2 has been called
2180 * kernfs_create_dir_ns(). Meanwhile, CPU1 call sysfs_remove_dir()
2181 * and sysfs_put(). This result in glue_dir->sd is freed.
2183 * Then the CPU2 will see a stale "empty" but still potentially used
2184 * glue dir around in kernfs_new_node().
2186 * In order to avoid this happening, we also should make sure that
2187 * kernfs_node for glue_dir is released in CPU1 only when refcount
2188 * for glue_dir kobj is 1.
2190 ref = kref_read(&glue_dir->kref);
2191 if (!kobject_has_children(glue_dir) && !--ref)
2192 kobject_del(glue_dir);
2193 kobject_put(glue_dir);
2194 mutex_unlock(&gdp_mutex);
2197 static int device_add_class_symlinks(struct device *dev)
2199 struct device_node *of_node = dev_of_node(dev);
2203 error = sysfs_create_link(&dev->kobj, of_node_kobj(of_node), "of_node");
2205 dev_warn(dev, "Error %d creating of_node link\n",error);
2206 /* An error here doesn't warrant bringing down the device */
2212 error = sysfs_create_link(&dev->kobj,
2213 &dev->class->p->subsys.kobj,
2218 if (dev->parent && device_is_not_partition(dev)) {
2219 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
2226 /* /sys/block has directories and does not need symlinks */
2227 if (sysfs_deprecated && dev->class == &block_class)
2231 /* link in the class directory pointing to the device */
2232 error = sysfs_create_link(&dev->class->p->subsys.kobj,
2233 &dev->kobj, dev_name(dev));
2240 sysfs_remove_link(&dev->kobj, "device");
2243 sysfs_remove_link(&dev->kobj, "subsystem");
2245 sysfs_remove_link(&dev->kobj, "of_node");
2249 static void device_remove_class_symlinks(struct device *dev)
2251 if (dev_of_node(dev))
2252 sysfs_remove_link(&dev->kobj, "of_node");
2257 if (dev->parent && device_is_not_partition(dev))
2258 sysfs_remove_link(&dev->kobj, "device");
2259 sysfs_remove_link(&dev->kobj, "subsystem");
2261 if (sysfs_deprecated && dev->class == &block_class)
2264 sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
2268 * dev_set_name - set a device name
2270 * @fmt: format string for the device's name
2272 int dev_set_name(struct device *dev, const char *fmt, ...)
2277 va_start(vargs, fmt);
2278 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
2282 EXPORT_SYMBOL_GPL(dev_set_name);
2285 * device_to_dev_kobj - select a /sys/dev/ directory for the device
2288 * By default we select char/ for new entries. Setting class->dev_obj
2289 * to NULL prevents an entry from being created. class->dev_kobj must
2290 * be set (or cleared) before any devices are registered to the class
2291 * otherwise device_create_sys_dev_entry() and
2292 * device_remove_sys_dev_entry() will disagree about the presence of
2295 static struct kobject *device_to_dev_kobj(struct device *dev)
2297 struct kobject *kobj;
2300 kobj = dev->class->dev_kobj;
2302 kobj = sysfs_dev_char_kobj;
2307 static int device_create_sys_dev_entry(struct device *dev)
2309 struct kobject *kobj = device_to_dev_kobj(dev);
2314 format_dev_t(devt_str, dev->devt);
2315 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
2321 static void device_remove_sys_dev_entry(struct device *dev)
2323 struct kobject *kobj = device_to_dev_kobj(dev);
2327 format_dev_t(devt_str, dev->devt);
2328 sysfs_remove_link(kobj, devt_str);
2332 static int device_private_init(struct device *dev)
2334 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
2337 dev->p->device = dev;
2338 klist_init(&dev->p->klist_children, klist_children_get,
2339 klist_children_put);
2340 INIT_LIST_HEAD(&dev->p->deferred_probe);
2345 * device_add - add device to device hierarchy.
2348 * This is part 2 of device_register(), though may be called
2349 * separately _iff_ device_initialize() has been called separately.
2351 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
2352 * to the global and sibling lists for the device, then
2353 * adds it to the other relevant subsystems of the driver model.
2355 * Do not call this routine or device_register() more than once for
2356 * any device structure. The driver model core is not designed to work
2357 * with devices that get unregistered and then spring back to life.
2358 * (Among other things, it's very hard to guarantee that all references
2359 * to the previous incarnation of @dev have been dropped.) Allocate
2360 * and register a fresh new struct device instead.
2362 * NOTE: _Never_ directly free @dev after calling this function, even
2363 * if it returned an error! Always use put_device() to give up your
2364 * reference instead.
2366 * Rule of thumb is: if device_add() succeeds, you should call
2367 * device_del() when you want to get rid of it. If device_add() has
2368 * *not* succeeded, use *only* put_device() to drop the reference
2371 int device_add(struct device *dev)
2373 struct device *parent;
2374 struct kobject *kobj;
2375 struct class_interface *class_intf;
2376 int error = -EINVAL, fw_ret;
2377 struct kobject *glue_dir = NULL;
2379 dev = get_device(dev);
2384 error = device_private_init(dev);
2390 * for statically allocated devices, which should all be converted
2391 * some day, we need to initialize the name. We prevent reading back
2392 * the name, and force the use of dev_name()
2394 if (dev->init_name) {
2395 dev_set_name(dev, "%s", dev->init_name);
2396 dev->init_name = NULL;
2399 /* subsystems can specify simple device enumeration */
2400 if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
2401 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
2403 if (!dev_name(dev)) {
2408 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
2410 parent = get_device(dev->parent);
2411 kobj = get_device_parent(dev, parent);
2413 error = PTR_ERR(kobj);
2417 dev->kobj.parent = kobj;
2419 /* use parent numa_node */
2420 if (parent && (dev_to_node(dev) == NUMA_NO_NODE))
2421 set_dev_node(dev, dev_to_node(parent));
2423 /* first, register with generic layer. */
2424 /* we require the name to be set before, and pass NULL */
2425 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
2427 glue_dir = get_glue_dir(dev);
2431 /* notify platform of device entry */
2432 error = device_platform_notify(dev, KOBJ_ADD);
2434 goto platform_error;
2436 error = device_create_file(dev, &dev_attr_uevent);
2440 error = device_add_class_symlinks(dev);
2443 error = device_add_attrs(dev);
2446 error = bus_add_device(dev);
2449 error = dpm_sysfs_add(dev);
2454 if (MAJOR(dev->devt)) {
2455 error = device_create_file(dev, &dev_attr_dev);
2459 error = device_create_sys_dev_entry(dev);
2463 devtmpfs_create_node(dev);
2466 /* Notify clients of device addition. This call must come
2467 * after dpm_sysfs_add() and before kobject_uevent().
2470 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2471 BUS_NOTIFY_ADD_DEVICE, dev);
2473 kobject_uevent(&dev->kobj, KOBJ_ADD);
2475 if (dev->fwnode && !dev->fwnode->dev)
2476 dev->fwnode->dev = dev;
2479 * Check if any of the other devices (consumers) have been waiting for
2480 * this device (supplier) to be added so that they can create a device
2483 * This needs to happen after device_pm_add() because device_link_add()
2484 * requires the supplier be registered before it's called.
2486 * But this also needs to happe before bus_probe_device() to make sure
2487 * waiting consumers can link to it before the driver is bound to the
2488 * device and the driver sync_state callback is called for this device.
2490 device_link_add_missing_supplier_links();
2492 if (fwnode_has_op(dev->fwnode, add_links)) {
2493 fw_ret = fwnode_call_int_op(dev->fwnode, add_links, dev);
2494 if (fw_ret == -ENODEV)
2495 device_link_wait_for_mandatory_supplier(dev);
2497 device_link_wait_for_optional_supplier(dev);
2500 bus_probe_device(dev);
2502 klist_add_tail(&dev->p->knode_parent,
2503 &parent->p->klist_children);
2506 mutex_lock(&dev->class->p->mutex);
2507 /* tie the class to the device */
2508 klist_add_tail(&dev->p->knode_class,
2509 &dev->class->p->klist_devices);
2511 /* notify any interfaces that the device is here */
2512 list_for_each_entry(class_intf,
2513 &dev->class->p->interfaces, node)
2514 if (class_intf->add_dev)
2515 class_intf->add_dev(dev, class_intf);
2516 mutex_unlock(&dev->class->p->mutex);
2522 if (MAJOR(dev->devt))
2523 device_remove_file(dev, &dev_attr_dev);
2525 device_pm_remove(dev);
2526 dpm_sysfs_remove(dev);
2528 bus_remove_device(dev);
2530 device_remove_attrs(dev);
2532 device_remove_class_symlinks(dev);
2534 device_remove_file(dev, &dev_attr_uevent);
2536 device_platform_notify(dev, KOBJ_REMOVE);
2538 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
2539 glue_dir = get_glue_dir(dev);
2540 kobject_del(&dev->kobj);
2542 cleanup_glue_dir(dev, glue_dir);
2550 EXPORT_SYMBOL_GPL(device_add);
2553 * device_register - register a device with the system.
2554 * @dev: pointer to the device structure
2556 * This happens in two clean steps - initialize the device
2557 * and add it to the system. The two steps can be called
2558 * separately, but this is the easiest and most common.
2559 * I.e. you should only call the two helpers separately if
2560 * have a clearly defined need to use and refcount the device
2561 * before it is added to the hierarchy.
2563 * For more information, see the kerneldoc for device_initialize()
2566 * NOTE: _Never_ directly free @dev after calling this function, even
2567 * if it returned an error! Always use put_device() to give up the
2568 * reference initialized in this function instead.
2570 int device_register(struct device *dev)
2572 device_initialize(dev);
2573 return device_add(dev);
2575 EXPORT_SYMBOL_GPL(device_register);
2578 * get_device - increment reference count for device.
2581 * This simply forwards the call to kobject_get(), though
2582 * we do take care to provide for the case that we get a NULL
2583 * pointer passed in.
2585 struct device *get_device(struct device *dev)
2587 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
2589 EXPORT_SYMBOL_GPL(get_device);
2592 * put_device - decrement reference count.
2593 * @dev: device in question.
2595 void put_device(struct device *dev)
2597 /* might_sleep(); */
2599 kobject_put(&dev->kobj);
2601 EXPORT_SYMBOL_GPL(put_device);
2603 bool kill_device(struct device *dev)
2606 * Require the device lock and set the "dead" flag to guarantee that
2607 * the update behavior is consistent with the other bitfields near
2608 * it and that we cannot have an asynchronous probe routine trying
2609 * to run while we are tearing out the bus/class/sysfs from
2610 * underneath the device.
2612 lockdep_assert_held(&dev->mutex);
2616 dev->p->dead = true;
2619 EXPORT_SYMBOL_GPL(kill_device);
2622 * device_del - delete device from system.
2625 * This is the first part of the device unregistration
2626 * sequence. This removes the device from the lists we control
2627 * from here, has it removed from the other driver model
2628 * subsystems it was added to in device_add(), and removes it
2629 * from the kobject hierarchy.
2631 * NOTE: this should be called manually _iff_ device_add() was
2632 * also called manually.
2634 void device_del(struct device *dev)
2636 struct device *parent = dev->parent;
2637 struct kobject *glue_dir = NULL;
2638 struct class_interface *class_intf;
2644 if (dev->fwnode && dev->fwnode->dev == dev)
2645 dev->fwnode->dev = NULL;
2647 /* Notify clients of device removal. This call must come
2648 * before dpm_sysfs_remove().
2651 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2652 BUS_NOTIFY_DEL_DEVICE, dev);
2654 dpm_sysfs_remove(dev);
2656 klist_del(&dev->p->knode_parent);
2657 if (MAJOR(dev->devt)) {
2658 devtmpfs_delete_node(dev);
2659 device_remove_sys_dev_entry(dev);
2660 device_remove_file(dev, &dev_attr_dev);
2663 device_remove_class_symlinks(dev);
2665 mutex_lock(&dev->class->p->mutex);
2666 /* notify any interfaces that the device is now gone */
2667 list_for_each_entry(class_intf,
2668 &dev->class->p->interfaces, node)
2669 if (class_intf->remove_dev)
2670 class_intf->remove_dev(dev, class_intf);
2671 /* remove the device from the class list */
2672 klist_del(&dev->p->knode_class);
2673 mutex_unlock(&dev->class->p->mutex);
2675 device_remove_file(dev, &dev_attr_uevent);
2676 device_remove_attrs(dev);
2677 bus_remove_device(dev);
2678 device_pm_remove(dev);
2679 driver_deferred_probe_del(dev);
2680 device_platform_notify(dev, KOBJ_REMOVE);
2681 device_remove_properties(dev);
2682 device_links_purge(dev);
2685 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2686 BUS_NOTIFY_REMOVED_DEVICE, dev);
2687 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
2688 glue_dir = get_glue_dir(dev);
2689 kobject_del(&dev->kobj);
2690 cleanup_glue_dir(dev, glue_dir);
2693 EXPORT_SYMBOL_GPL(device_del);
2696 * device_unregister - unregister device from system.
2697 * @dev: device going away.
2699 * We do this in two parts, like we do device_register(). First,
2700 * we remove it from all the subsystems with device_del(), then
2701 * we decrement the reference count via put_device(). If that
2702 * is the final reference count, the device will be cleaned up
2703 * via device_release() above. Otherwise, the structure will
2704 * stick around until the final reference to the device is dropped.
2706 void device_unregister(struct device *dev)
2708 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
2712 EXPORT_SYMBOL_GPL(device_unregister);
2714 static struct device *prev_device(struct klist_iter *i)
2716 struct klist_node *n = klist_prev(i);
2717 struct device *dev = NULL;
2718 struct device_private *p;
2721 p = to_device_private_parent(n);
2727 static struct device *next_device(struct klist_iter *i)
2729 struct klist_node *n = klist_next(i);
2730 struct device *dev = NULL;
2731 struct device_private *p;
2734 p = to_device_private_parent(n);
2741 * device_get_devnode - path of device node file
2743 * @mode: returned file access mode
2744 * @uid: returned file owner
2745 * @gid: returned file group
2746 * @tmp: possibly allocated string
2748 * Return the relative path of a possible device node.
2749 * Non-default names may need to allocate a memory to compose
2750 * a name. This memory is returned in tmp and needs to be
2751 * freed by the caller.
2753 const char *device_get_devnode(struct device *dev,
2754 umode_t *mode, kuid_t *uid, kgid_t *gid,
2761 /* the device type may provide a specific name */
2762 if (dev->type && dev->type->devnode)
2763 *tmp = dev->type->devnode(dev, mode, uid, gid);
2767 /* the class may provide a specific name */
2768 if (dev->class && dev->class->devnode)
2769 *tmp = dev->class->devnode(dev, mode);
2773 /* return name without allocation, tmp == NULL */
2774 if (strchr(dev_name(dev), '!') == NULL)
2775 return dev_name(dev);
2777 /* replace '!' in the name with '/' */
2778 s = kstrdup(dev_name(dev), GFP_KERNEL);
2781 strreplace(s, '!', '/');
2786 * device_for_each_child - device child iterator.
2787 * @parent: parent struct device.
2788 * @fn: function to be called for each device.
2789 * @data: data for the callback.
2791 * Iterate over @parent's child devices, and call @fn for each,
2794 * We check the return of @fn each time. If it returns anything
2795 * other than 0, we break out and return that value.
2797 int device_for_each_child(struct device *parent, void *data,
2798 int (*fn)(struct device *dev, void *data))
2800 struct klist_iter i;
2801 struct device *child;
2807 klist_iter_init(&parent->p->klist_children, &i);
2808 while (!error && (child = next_device(&i)))
2809 error = fn(child, data);
2810 klist_iter_exit(&i);
2813 EXPORT_SYMBOL_GPL(device_for_each_child);
2816 * device_for_each_child_reverse - device child iterator in reversed order.
2817 * @parent: parent struct device.
2818 * @fn: function to be called for each device.
2819 * @data: data for the callback.
2821 * Iterate over @parent's child devices, and call @fn for each,
2824 * We check the return of @fn each time. If it returns anything
2825 * other than 0, we break out and return that value.
2827 int device_for_each_child_reverse(struct device *parent, void *data,
2828 int (*fn)(struct device *dev, void *data))
2830 struct klist_iter i;
2831 struct device *child;
2837 klist_iter_init(&parent->p->klist_children, &i);
2838 while ((child = prev_device(&i)) && !error)
2839 error = fn(child, data);
2840 klist_iter_exit(&i);
2843 EXPORT_SYMBOL_GPL(device_for_each_child_reverse);
2846 * device_find_child - device iterator for locating a particular device.
2847 * @parent: parent struct device
2848 * @match: Callback function to check device
2849 * @data: Data to pass to match function
2851 * This is similar to the device_for_each_child() function above, but it
2852 * returns a reference to a device that is 'found' for later use, as
2853 * determined by the @match callback.
2855 * The callback should return 0 if the device doesn't match and non-zero
2856 * if it does. If the callback returns non-zero and a reference to the
2857 * current device can be obtained, this function will return to the caller
2858 * and not iterate over any more devices.
2860 * NOTE: you will need to drop the reference with put_device() after use.
2862 struct device *device_find_child(struct device *parent, void *data,
2863 int (*match)(struct device *dev, void *data))
2865 struct klist_iter i;
2866 struct device *child;
2871 klist_iter_init(&parent->p->klist_children, &i);
2872 while ((child = next_device(&i)))
2873 if (match(child, data) && get_device(child))
2875 klist_iter_exit(&i);
2878 EXPORT_SYMBOL_GPL(device_find_child);
2881 * device_find_child_by_name - device iterator for locating a child device.
2882 * @parent: parent struct device
2883 * @name: name of the child device
2885 * This is similar to the device_find_child() function above, but it
2886 * returns a reference to a device that has the name @name.
2888 * NOTE: you will need to drop the reference with put_device() after use.
2890 struct device *device_find_child_by_name(struct device *parent,
2893 struct klist_iter i;
2894 struct device *child;
2899 klist_iter_init(&parent->p->klist_children, &i);
2900 while ((child = next_device(&i)))
2901 if (!strcmp(dev_name(child), name) && get_device(child))
2903 klist_iter_exit(&i);
2906 EXPORT_SYMBOL_GPL(device_find_child_by_name);
2908 int __init devices_init(void)
2910 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
2913 dev_kobj = kobject_create_and_add("dev", NULL);
2916 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
2917 if (!sysfs_dev_block_kobj)
2918 goto block_kobj_err;
2919 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
2920 if (!sysfs_dev_char_kobj)
2926 kobject_put(sysfs_dev_block_kobj);
2928 kobject_put(dev_kobj);
2930 kset_unregister(devices_kset);
2934 static int device_check_offline(struct device *dev, void *not_used)
2938 ret = device_for_each_child(dev, NULL, device_check_offline);
2942 return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
2946 * device_offline - Prepare the device for hot-removal.
2947 * @dev: Device to be put offline.
2949 * Execute the device bus type's .offline() callback, if present, to prepare
2950 * the device for a subsequent hot-removal. If that succeeds, the device must
2951 * not be used until either it is removed or its bus type's .online() callback
2954 * Call under device_hotplug_lock.
2956 int device_offline(struct device *dev)
2960 if (dev->offline_disabled)
2963 ret = device_for_each_child(dev, NULL, device_check_offline);
2968 if (device_supports_offline(dev)) {
2972 ret = dev->bus->offline(dev);
2974 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
2975 dev->offline = true;
2985 * device_online - Put the device back online after successful device_offline().
2986 * @dev: Device to be put back online.
2988 * If device_offline() has been successfully executed for @dev, but the device
2989 * has not been removed subsequently, execute its bus type's .online() callback
2990 * to indicate that the device can be used again.
2992 * Call under device_hotplug_lock.
2994 int device_online(struct device *dev)
2999 if (device_supports_offline(dev)) {
3001 ret = dev->bus->online(dev);
3003 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
3004 dev->offline = false;
3015 struct root_device {
3017 struct module *owner;
3020 static inline struct root_device *to_root_device(struct device *d)
3022 return container_of(d, struct root_device, dev);
3025 static void root_device_release(struct device *dev)
3027 kfree(to_root_device(dev));
3031 * __root_device_register - allocate and register a root device
3032 * @name: root device name
3033 * @owner: owner module of the root device, usually THIS_MODULE
3035 * This function allocates a root device and registers it
3036 * using device_register(). In order to free the returned
3037 * device, use root_device_unregister().
3039 * Root devices are dummy devices which allow other devices
3040 * to be grouped under /sys/devices. Use this function to
3041 * allocate a root device and then use it as the parent of
3042 * any device which should appear under /sys/devices/{name}
3044 * The /sys/devices/{name} directory will also contain a
3045 * 'module' symlink which points to the @owner directory
3048 * Returns &struct device pointer on success, or ERR_PTR() on error.
3050 * Note: You probably want to use root_device_register().
3052 struct device *__root_device_register(const char *name, struct module *owner)
3054 struct root_device *root;
3057 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
3059 return ERR_PTR(err);
3061 err = dev_set_name(&root->dev, "%s", name);
3064 return ERR_PTR(err);
3067 root->dev.release = root_device_release;
3069 err = device_register(&root->dev);
3071 put_device(&root->dev);
3072 return ERR_PTR(err);
3075 #ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
3077 struct module_kobject *mk = &owner->mkobj;
3079 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
3081 device_unregister(&root->dev);
3082 return ERR_PTR(err);
3084 root->owner = owner;
3090 EXPORT_SYMBOL_GPL(__root_device_register);
3093 * root_device_unregister - unregister and free a root device
3094 * @dev: device going away
3096 * This function unregisters and cleans up a device that was created by
3097 * root_device_register().
3099 void root_device_unregister(struct device *dev)
3101 struct root_device *root = to_root_device(dev);
3104 sysfs_remove_link(&root->dev.kobj, "module");
3106 device_unregister(dev);
3108 EXPORT_SYMBOL_GPL(root_device_unregister);
3111 static void device_create_release(struct device *dev)
3113 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
3117 static __printf(6, 0) struct device *
3118 device_create_groups_vargs(struct class *class, struct device *parent,
3119 dev_t devt, void *drvdata,
3120 const struct attribute_group **groups,
3121 const char *fmt, va_list args)
3123 struct device *dev = NULL;
3124 int retval = -ENODEV;
3126 if (class == NULL || IS_ERR(class))
3129 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
3135 device_initialize(dev);
3138 dev->parent = parent;
3139 dev->groups = groups;
3140 dev->release = device_create_release;
3141 dev_set_drvdata(dev, drvdata);
3143 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
3147 retval = device_add(dev);
3155 return ERR_PTR(retval);
3159 * device_create_vargs - creates a device and registers it with sysfs
3160 * @class: pointer to the struct class that this device should be registered to
3161 * @parent: pointer to the parent struct device of this new device, if any
3162 * @devt: the dev_t for the char device to be added
3163 * @drvdata: the data to be added to the device for callbacks
3164 * @fmt: string for the device's name
3165 * @args: va_list for the device's name
3167 * This function can be used by char device classes. A struct device
3168 * will be created in sysfs, registered to the specified class.
3170 * A "dev" file will be created, showing the dev_t for the device, if
3171 * the dev_t is not 0,0.
3172 * If a pointer to a parent struct device is passed in, the newly created
3173 * struct device will be a child of that device in sysfs.
3174 * The pointer to the struct device will be returned from the call.
3175 * Any further sysfs files that might be required can be created using this
3178 * Returns &struct device pointer on success, or ERR_PTR() on error.
3180 * Note: the struct class passed to this function must have previously
3181 * been created with a call to class_create().
3183 struct device *device_create_vargs(struct class *class, struct device *parent,
3184 dev_t devt, void *drvdata, const char *fmt,
3187 return device_create_groups_vargs(class, parent, devt, drvdata, NULL,
3190 EXPORT_SYMBOL_GPL(device_create_vargs);
3193 * device_create - creates a device and registers it with sysfs
3194 * @class: pointer to the struct class that this device should be registered to
3195 * @parent: pointer to the parent struct device of this new device, if any
3196 * @devt: the dev_t for the char device to be added
3197 * @drvdata: the data to be added to the device for callbacks
3198 * @fmt: string for the device's name
3200 * This function can be used by char device classes. A struct device
3201 * will be created in sysfs, registered to the specified class.
3203 * A "dev" file will be created, showing the dev_t for the device, if
3204 * the dev_t is not 0,0.
3205 * If a pointer to a parent struct device is passed in, the newly created
3206 * struct device will be a child of that device in sysfs.
3207 * The pointer to the struct device will be returned from the call.
3208 * Any further sysfs files that might be required can be created using this
3211 * Returns &struct device pointer on success, or ERR_PTR() on error.
3213 * Note: the struct class passed to this function must have previously
3214 * been created with a call to class_create().
3216 struct device *device_create(struct class *class, struct device *parent,
3217 dev_t devt, void *drvdata, const char *fmt, ...)
3222 va_start(vargs, fmt);
3223 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
3227 EXPORT_SYMBOL_GPL(device_create);
3230 * device_create_with_groups - creates a device and registers it with sysfs
3231 * @class: pointer to the struct class that this device should be registered to
3232 * @parent: pointer to the parent struct device of this new device, if any
3233 * @devt: the dev_t for the char device to be added
3234 * @drvdata: the data to be added to the device for callbacks
3235 * @groups: NULL-terminated list of attribute groups to be created
3236 * @fmt: string for the device's name
3238 * This function can be used by char device classes. A struct device
3239 * will be created in sysfs, registered to the specified class.
3240 * Additional attributes specified in the groups parameter will also
3241 * be created automatically.
3243 * A "dev" file will be created, showing the dev_t for the device, if
3244 * the dev_t is not 0,0.
3245 * If a pointer to a parent struct device is passed in, the newly created
3246 * struct device will be a child of that device in sysfs.
3247 * The pointer to the struct device will be returned from the call.
3248 * Any further sysfs files that might be required can be created using this
3251 * Returns &struct device pointer on success, or ERR_PTR() on error.
3253 * Note: the struct class passed to this function must have previously
3254 * been created with a call to class_create().
3256 struct device *device_create_with_groups(struct class *class,
3257 struct device *parent, dev_t devt,
3259 const struct attribute_group **groups,
3260 const char *fmt, ...)
3265 va_start(vargs, fmt);
3266 dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
3271 EXPORT_SYMBOL_GPL(device_create_with_groups);
3274 * device_destroy - removes a device that was created with device_create()
3275 * @class: pointer to the struct class that this device was registered with
3276 * @devt: the dev_t of the device that was previously registered
3278 * This call unregisters and cleans up a device that was created with a
3279 * call to device_create().
3281 void device_destroy(struct class *class, dev_t devt)
3285 dev = class_find_device_by_devt(class, devt);
3288 device_unregister(dev);
3291 EXPORT_SYMBOL_GPL(device_destroy);
3294 * device_rename - renames a device
3295 * @dev: the pointer to the struct device to be renamed
3296 * @new_name: the new name of the device
3298 * It is the responsibility of the caller to provide mutual
3299 * exclusion between two different calls of device_rename
3300 * on the same device to ensure that new_name is valid and
3301 * won't conflict with other devices.
3303 * Note: Don't call this function. Currently, the networking layer calls this
3304 * function, but that will change. The following text from Kay Sievers offers
3307 * Renaming devices is racy at many levels, symlinks and other stuff are not
3308 * replaced atomically, and you get a "move" uevent, but it's not easy to
3309 * connect the event to the old and new device. Device nodes are not renamed at
3310 * all, there isn't even support for that in the kernel now.
3312 * In the meantime, during renaming, your target name might be taken by another
3313 * driver, creating conflicts. Or the old name is taken directly after you
3314 * renamed it -- then you get events for the same DEVPATH, before you even see
3315 * the "move" event. It's just a mess, and nothing new should ever rely on
3316 * kernel device renaming. Besides that, it's not even implemented now for
3317 * other things than (driver-core wise very simple) network devices.
3319 * We are currently about to change network renaming in udev to completely
3320 * disallow renaming of devices in the same namespace as the kernel uses,
3321 * because we can't solve the problems properly, that arise with swapping names
3322 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
3323 * be allowed to some other name than eth[0-9]*, for the aforementioned
3326 * Make up a "real" name in the driver before you register anything, or add
3327 * some other attributes for userspace to find the device, or use udev to add
3328 * symlinks -- but never rename kernel devices later, it's a complete mess. We
3329 * don't even want to get into that and try to implement the missing pieces in
3330 * the core. We really have other pieces to fix in the driver core mess. :)
3332 int device_rename(struct device *dev, const char *new_name)
3334 struct kobject *kobj = &dev->kobj;
3335 char *old_device_name = NULL;
3338 dev = get_device(dev);
3342 dev_dbg(dev, "renaming to %s\n", new_name);
3344 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
3345 if (!old_device_name) {
3351 error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj,
3352 kobj, old_device_name,
3353 new_name, kobject_namespace(kobj));
3358 error = kobject_rename(kobj, new_name);
3365 kfree(old_device_name);
3369 EXPORT_SYMBOL_GPL(device_rename);
3371 static int device_move_class_links(struct device *dev,
3372 struct device *old_parent,
3373 struct device *new_parent)
3378 sysfs_remove_link(&dev->kobj, "device");
3380 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
3386 * device_move - moves a device to a new parent
3387 * @dev: the pointer to the struct device to be moved
3388 * @new_parent: the new parent of the device (can be NULL)
3389 * @dpm_order: how to reorder the dpm_list
3391 int device_move(struct device *dev, struct device *new_parent,
3392 enum dpm_order dpm_order)
3395 struct device *old_parent;
3396 struct kobject *new_parent_kobj;
3398 dev = get_device(dev);
3403 new_parent = get_device(new_parent);
3404 new_parent_kobj = get_device_parent(dev, new_parent);
3405 if (IS_ERR(new_parent_kobj)) {
3406 error = PTR_ERR(new_parent_kobj);
3407 put_device(new_parent);
3411 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
3412 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
3413 error = kobject_move(&dev->kobj, new_parent_kobj);
3415 cleanup_glue_dir(dev, new_parent_kobj);
3416 put_device(new_parent);
3419 old_parent = dev->parent;
3420 dev->parent = new_parent;
3422 klist_remove(&dev->p->knode_parent);
3424 klist_add_tail(&dev->p->knode_parent,
3425 &new_parent->p->klist_children);
3426 set_dev_node(dev, dev_to_node(new_parent));
3430 error = device_move_class_links(dev, old_parent, new_parent);
3432 /* We ignore errors on cleanup since we're hosed anyway... */
3433 device_move_class_links(dev, new_parent, old_parent);
3434 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
3436 klist_remove(&dev->p->knode_parent);
3437 dev->parent = old_parent;
3439 klist_add_tail(&dev->p->knode_parent,
3440 &old_parent->p->klist_children);
3441 set_dev_node(dev, dev_to_node(old_parent));
3444 cleanup_glue_dir(dev, new_parent_kobj);
3445 put_device(new_parent);
3449 switch (dpm_order) {
3450 case DPM_ORDER_NONE:
3452 case DPM_ORDER_DEV_AFTER_PARENT:
3453 device_pm_move_after(dev, new_parent);
3454 devices_kset_move_after(dev, new_parent);
3456 case DPM_ORDER_PARENT_BEFORE_DEV:
3457 device_pm_move_before(new_parent, dev);
3458 devices_kset_move_before(new_parent, dev);
3460 case DPM_ORDER_DEV_LAST:
3461 device_pm_move_last(dev);
3462 devices_kset_move_last(dev);
3466 put_device(old_parent);
3472 EXPORT_SYMBOL_GPL(device_move);
3475 * device_shutdown - call ->shutdown() on each device to shutdown.
3477 void device_shutdown(void)
3479 struct device *dev, *parent;
3481 wait_for_device_probe();
3482 device_block_probing();
3486 spin_lock(&devices_kset->list_lock);
3488 * Walk the devices list backward, shutting down each in turn.
3489 * Beware that device unplug events may also start pulling
3490 * devices offline, even as the system is shutting down.
3492 while (!list_empty(&devices_kset->list)) {
3493 dev = list_entry(devices_kset->list.prev, struct device,
3497 * hold reference count of device's parent to
3498 * prevent it from being freed because parent's
3499 * lock is to be held
3501 parent = get_device(dev->parent);
3504 * Make sure the device is off the kset list, in the
3505 * event that dev->*->shutdown() doesn't remove it.
3507 list_del_init(&dev->kobj.entry);
3508 spin_unlock(&devices_kset->list_lock);
3510 /* hold lock to avoid race with probe/release */
3512 device_lock(parent);
3515 /* Don't allow any more runtime suspends */
3516 pm_runtime_get_noresume(dev);
3517 pm_runtime_barrier(dev);
3519 if (dev->class && dev->class->shutdown_pre) {
3521 dev_info(dev, "shutdown_pre\n");
3522 dev->class->shutdown_pre(dev);
3524 if (dev->bus && dev->bus->shutdown) {
3526 dev_info(dev, "shutdown\n");
3527 dev->bus->shutdown(dev);
3528 } else if (dev->driver && dev->driver->shutdown) {
3530 dev_info(dev, "shutdown\n");
3531 dev->driver->shutdown(dev);
3536 device_unlock(parent);
3541 spin_lock(&devices_kset->list_lock);
3543 spin_unlock(&devices_kset->list_lock);
3547 * Device logging functions
3550 #ifdef CONFIG_PRINTK
3552 create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
3558 subsys = dev->class->name;
3560 subsys = dev->bus->name;
3564 pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys);
3569 * Add device identifier DEVICE=:
3573 * +sound:card0 subsystem:devname
3575 if (MAJOR(dev->devt)) {
3578 if (strcmp(subsys, "block") == 0)
3583 pos += snprintf(hdr + pos, hdrlen - pos,
3585 c, MAJOR(dev->devt), MINOR(dev->devt));
3586 } else if (strcmp(subsys, "net") == 0) {
3587 struct net_device *net = to_net_dev(dev);
3590 pos += snprintf(hdr + pos, hdrlen - pos,
3591 "DEVICE=n%u", net->ifindex);
3594 pos += snprintf(hdr + pos, hdrlen - pos,
3595 "DEVICE=+%s:%s", subsys, dev_name(dev));
3604 dev_WARN(dev, "device/subsystem name too long");
3608 int dev_vprintk_emit(int level, const struct device *dev,
3609 const char *fmt, va_list args)
3614 hdrlen = create_syslog_header(dev, hdr, sizeof(hdr));
3616 return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args);
3618 EXPORT_SYMBOL(dev_vprintk_emit);
3620 int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
3625 va_start(args, fmt);
3627 r = dev_vprintk_emit(level, dev, fmt, args);
3633 EXPORT_SYMBOL(dev_printk_emit);
3635 static void __dev_printk(const char *level, const struct device *dev,
3636 struct va_format *vaf)
3639 dev_printk_emit(level[1] - '0', dev, "%s %s: %pV",
3640 dev_driver_string(dev), dev_name(dev), vaf);
3642 printk("%s(NULL device *): %pV", level, vaf);
3645 void dev_printk(const char *level, const struct device *dev,
3646 const char *fmt, ...)
3648 struct va_format vaf;
3651 va_start(args, fmt);
3656 __dev_printk(level, dev, &vaf);
3660 EXPORT_SYMBOL(dev_printk);
3662 #define define_dev_printk_level(func, kern_level) \
3663 void func(const struct device *dev, const char *fmt, ...) \
3665 struct va_format vaf; \
3668 va_start(args, fmt); \
3673 __dev_printk(kern_level, dev, &vaf); \
3677 EXPORT_SYMBOL(func);
3679 define_dev_printk_level(_dev_emerg, KERN_EMERG);
3680 define_dev_printk_level(_dev_alert, KERN_ALERT);
3681 define_dev_printk_level(_dev_crit, KERN_CRIT);
3682 define_dev_printk_level(_dev_err, KERN_ERR);
3683 define_dev_printk_level(_dev_warn, KERN_WARNING);
3684 define_dev_printk_level(_dev_notice, KERN_NOTICE);
3685 define_dev_printk_level(_dev_info, KERN_INFO);
3689 static inline bool fwnode_is_primary(struct fwnode_handle *fwnode)
3691 return fwnode && !IS_ERR(fwnode->secondary);
3695 * set_primary_fwnode - Change the primary firmware node of a given device.
3696 * @dev: Device to handle.
3697 * @fwnode: New primary firmware node of the device.
3699 * Set the device's firmware node pointer to @fwnode, but if a secondary
3700 * firmware node of the device is present, preserve it.
3702 void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
3705 struct fwnode_handle *fn = dev->fwnode;
3707 if (fwnode_is_primary(fn))
3711 WARN_ON(fwnode->secondary);
3712 fwnode->secondary = fn;
3714 dev->fwnode = fwnode;
3716 dev->fwnode = fwnode_is_primary(dev->fwnode) ?
3717 dev->fwnode->secondary : NULL;
3720 EXPORT_SYMBOL_GPL(set_primary_fwnode);
3723 * set_secondary_fwnode - Change the secondary firmware node of a given device.
3724 * @dev: Device to handle.
3725 * @fwnode: New secondary firmware node of the device.
3727 * If a primary firmware node of the device is present, set its secondary
3728 * pointer to @fwnode. Otherwise, set the device's firmware node pointer to
3731 void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
3734 fwnode->secondary = ERR_PTR(-ENODEV);
3736 if (fwnode_is_primary(dev->fwnode))
3737 dev->fwnode->secondary = fwnode;
3739 dev->fwnode = fwnode;
3743 * device_set_of_node_from_dev - reuse device-tree node of another device
3744 * @dev: device whose device-tree node is being set
3745 * @dev2: device whose device-tree node is being reused
3747 * Takes another reference to the new device-tree node after first dropping
3748 * any reference held to the old node.
3750 void device_set_of_node_from_dev(struct device *dev, const struct device *dev2)
3752 of_node_put(dev->of_node);
3753 dev->of_node = of_node_get(dev2->of_node);
3754 dev->of_node_reused = true;
3756 EXPORT_SYMBOL_GPL(device_set_of_node_from_dev);
3758 int device_match_name(struct device *dev, const void *name)
3760 return sysfs_streq(dev_name(dev), name);
3762 EXPORT_SYMBOL_GPL(device_match_name);
3764 int device_match_of_node(struct device *dev, const void *np)
3766 return dev->of_node == np;
3768 EXPORT_SYMBOL_GPL(device_match_of_node);
3770 int device_match_fwnode(struct device *dev, const void *fwnode)
3772 return dev_fwnode(dev) == fwnode;
3774 EXPORT_SYMBOL_GPL(device_match_fwnode);
3776 int device_match_devt(struct device *dev, const void *pdevt)
3778 return dev->devt == *(dev_t *)pdevt;
3780 EXPORT_SYMBOL_GPL(device_match_devt);
3782 int device_match_acpi_dev(struct device *dev, const void *adev)
3784 return ACPI_COMPANION(dev) == adev;
3786 EXPORT_SYMBOL(device_match_acpi_dev);
3788 int device_match_any(struct device *dev, const void *unused)
3792 EXPORT_SYMBOL_GPL(device_match_any);