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/device.h>
13 #include <linux/err.h>
14 #include <linux/fwnode.h>
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/string.h>
19 #include <linux/kdev_t.h>
20 #include <linux/notifier.h>
22 #include <linux/of_device.h>
23 #include <linux/genhd.h>
24 #include <linux/mutex.h>
25 #include <linux/pm_runtime.h>
26 #include <linux/netdevice.h>
27 #include <linux/sched/signal.h>
28 #include <linux/sysfs.h>
31 #include "power/power.h"
33 #ifdef CONFIG_SYSFS_DEPRECATED
34 #ifdef CONFIG_SYSFS_DEPRECATED_V2
35 long sysfs_deprecated = 1;
37 long sysfs_deprecated = 0;
39 static int __init sysfs_deprecated_setup(char *arg)
41 return kstrtol(arg, 10, &sysfs_deprecated);
43 early_param("sysfs.deprecated", sysfs_deprecated_setup);
46 /* Device links support. */
47 static LIST_HEAD(wait_for_suppliers);
48 static DEFINE_MUTEX(wfs_lock);
51 static DEFINE_MUTEX(device_links_lock);
52 DEFINE_STATIC_SRCU(device_links_srcu);
54 static inline void device_links_write_lock(void)
56 mutex_lock(&device_links_lock);
59 static inline void device_links_write_unlock(void)
61 mutex_unlock(&device_links_lock);
64 int device_links_read_lock(void)
66 return srcu_read_lock(&device_links_srcu);
69 void device_links_read_unlock(int idx)
71 srcu_read_unlock(&device_links_srcu, idx);
73 #else /* !CONFIG_SRCU */
74 static DECLARE_RWSEM(device_links_lock);
76 static inline void device_links_write_lock(void)
78 down_write(&device_links_lock);
81 static inline void device_links_write_unlock(void)
83 up_write(&device_links_lock);
86 int device_links_read_lock(void)
88 down_read(&device_links_lock);
92 void device_links_read_unlock(int not_used)
94 up_read(&device_links_lock);
96 #endif /* !CONFIG_SRCU */
99 * device_is_dependent - Check if one device depends on another one
100 * @dev: Device to check dependencies for.
101 * @target: Device to check against.
103 * Check if @target depends on @dev or any device dependent on it (its child or
104 * its consumer etc). Return 1 if that is the case or 0 otherwise.
106 static int device_is_dependent(struct device *dev, void *target)
108 struct device_link *link;
114 ret = device_for_each_child(dev, target, device_is_dependent);
118 list_for_each_entry(link, &dev->links.consumers, s_node) {
119 if (link->consumer == target)
122 ret = device_is_dependent(link->consumer, target);
129 static void device_link_init_status(struct device_link *link,
130 struct device *consumer,
131 struct device *supplier)
133 switch (supplier->links.status) {
135 switch (consumer->links.status) {
138 * A consumer driver can create a link to a supplier
139 * that has not completed its probing yet as long as it
140 * knows that the supplier is already functional (for
141 * example, it has just acquired some resources from the
144 link->status = DL_STATE_CONSUMER_PROBE;
147 link->status = DL_STATE_DORMANT;
151 case DL_DEV_DRIVER_BOUND:
152 switch (consumer->links.status) {
154 link->status = DL_STATE_CONSUMER_PROBE;
156 case DL_DEV_DRIVER_BOUND:
157 link->status = DL_STATE_ACTIVE;
160 link->status = DL_STATE_AVAILABLE;
164 case DL_DEV_UNBINDING:
165 link->status = DL_STATE_SUPPLIER_UNBIND;
168 link->status = DL_STATE_DORMANT;
173 static int device_reorder_to_tail(struct device *dev, void *not_used)
175 struct device_link *link;
178 * Devices that have not been registered yet will be put to the ends
179 * of the lists during the registration, so skip them here.
181 if (device_is_registered(dev))
182 devices_kset_move_last(dev);
184 if (device_pm_initialized(dev))
185 device_pm_move_last(dev);
187 device_for_each_child(dev, NULL, device_reorder_to_tail);
188 list_for_each_entry(link, &dev->links.consumers, s_node)
189 device_reorder_to_tail(link->consumer, NULL);
195 * device_pm_move_to_tail - Move set of devices to the end of device lists
196 * @dev: Device to move
198 * This is a device_reorder_to_tail() wrapper taking the requisite locks.
200 * It moves the @dev along with all of its children and all of its consumers
201 * to the ends of the device_kset and dpm_list, recursively.
203 void device_pm_move_to_tail(struct device *dev)
207 idx = device_links_read_lock();
209 device_reorder_to_tail(dev, NULL);
211 device_links_read_unlock(idx);
214 #define DL_MANAGED_LINK_FLAGS (DL_FLAG_AUTOREMOVE_CONSUMER | \
215 DL_FLAG_AUTOREMOVE_SUPPLIER | \
216 DL_FLAG_AUTOPROBE_CONSUMER)
218 #define DL_ADD_VALID_FLAGS (DL_MANAGED_LINK_FLAGS | DL_FLAG_STATELESS | \
219 DL_FLAG_PM_RUNTIME | DL_FLAG_RPM_ACTIVE)
222 * device_link_add - Create a link between two devices.
223 * @consumer: Consumer end of the link.
224 * @supplier: Supplier end of the link.
225 * @flags: Link flags.
227 * The caller is responsible for the proper synchronization of the link creation
228 * with runtime PM. First, setting the DL_FLAG_PM_RUNTIME flag will cause the
229 * runtime PM framework to take the link into account. Second, if the
230 * DL_FLAG_RPM_ACTIVE flag is set in addition to it, the supplier devices will
231 * be forced into the active metastate and reference-counted upon the creation
232 * of the link. If DL_FLAG_PM_RUNTIME is not set, DL_FLAG_RPM_ACTIVE will be
235 * If DL_FLAG_STATELESS is set in @flags, the caller of this function is
236 * expected to release the link returned by it directly with the help of either
237 * device_link_del() or device_link_remove().
239 * If that flag is not set, however, the caller of this function is handing the
240 * management of the link over to the driver core entirely and its return value
241 * can only be used to check whether or not the link is present. In that case,
242 * the DL_FLAG_AUTOREMOVE_CONSUMER and DL_FLAG_AUTOREMOVE_SUPPLIER device link
243 * flags can be used to indicate to the driver core when the link can be safely
244 * deleted. Namely, setting one of them in @flags indicates to the driver core
245 * that the link is not going to be used (by the given caller of this function)
246 * after unbinding the consumer or supplier driver, respectively, from its
247 * device, so the link can be deleted at that point. If none of them is set,
248 * the link will be maintained until one of the devices pointed to by it (either
249 * the consumer or the supplier) is unregistered.
251 * Also, if DL_FLAG_STATELESS, DL_FLAG_AUTOREMOVE_CONSUMER and
252 * DL_FLAG_AUTOREMOVE_SUPPLIER are not set in @flags (that is, a persistent
253 * managed device link is being added), the DL_FLAG_AUTOPROBE_CONSUMER flag can
254 * be used to request the driver core to automaticall probe for a consmer
255 * driver after successfully binding a driver to the supplier device.
257 * The combination of DL_FLAG_STATELESS and one of DL_FLAG_AUTOREMOVE_CONSUMER,
258 * DL_FLAG_AUTOREMOVE_SUPPLIER, or DL_FLAG_AUTOPROBE_CONSUMER set in @flags at
259 * the same time is invalid and will cause NULL to be returned upfront.
260 * However, if a device link between the given @consumer and @supplier pair
261 * exists already when this function is called for them, the existing link will
262 * be returned regardless of its current type and status (the link's flags may
263 * be modified then). The caller of this function is then expected to treat
264 * the link as though it has just been created, so (in particular) if
265 * DL_FLAG_STATELESS was passed in @flags, the link needs to be released
266 * explicitly when not needed any more (as stated above).
268 * A side effect of the link creation is re-ordering of dpm_list and the
269 * devices_kset list by moving the consumer device and all devices depending
270 * on it to the ends of these lists (that does not happen to devices that have
271 * not been registered when this function is called).
273 * The supplier device is required to be registered when this function is called
274 * and NULL will be returned if that is not the case. The consumer device need
275 * not be registered, however.
277 struct device_link *device_link_add(struct device *consumer,
278 struct device *supplier, u32 flags)
280 struct device_link *link;
282 if (!consumer || !supplier || flags & ~DL_ADD_VALID_FLAGS ||
283 (flags & DL_FLAG_STATELESS && flags & DL_MANAGED_LINK_FLAGS) ||
284 (flags & DL_FLAG_AUTOPROBE_CONSUMER &&
285 flags & (DL_FLAG_AUTOREMOVE_CONSUMER |
286 DL_FLAG_AUTOREMOVE_SUPPLIER)))
289 if (flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) {
290 if (pm_runtime_get_sync(supplier) < 0) {
291 pm_runtime_put_noidle(supplier);
296 if (!(flags & DL_FLAG_STATELESS))
297 flags |= DL_FLAG_MANAGED;
299 device_links_write_lock();
303 * If the supplier has not been fully registered yet or there is a
304 * reverse dependency between the consumer and the supplier already in
305 * the graph, return NULL.
307 if (!device_pm_initialized(supplier)
308 || device_is_dependent(consumer, supplier)) {
314 * DL_FLAG_AUTOREMOVE_SUPPLIER indicates that the link will be needed
315 * longer than for DL_FLAG_AUTOREMOVE_CONSUMER and setting them both
316 * together doesn't make sense, so prefer DL_FLAG_AUTOREMOVE_SUPPLIER.
318 if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
319 flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER;
321 list_for_each_entry(link, &supplier->links.consumers, s_node) {
322 if (link->consumer != consumer)
325 if (flags & DL_FLAG_PM_RUNTIME) {
326 if (!(link->flags & DL_FLAG_PM_RUNTIME)) {
327 pm_runtime_new_link(consumer);
328 link->flags |= DL_FLAG_PM_RUNTIME;
330 if (flags & DL_FLAG_RPM_ACTIVE)
331 refcount_inc(&link->rpm_active);
334 if (flags & DL_FLAG_STATELESS) {
335 link->flags |= DL_FLAG_STATELESS;
336 kref_get(&link->kref);
341 * If the life time of the link following from the new flags is
342 * longer than indicated by the flags of the existing link,
343 * update the existing link to stay around longer.
345 if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER) {
346 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER) {
347 link->flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER;
348 link->flags |= DL_FLAG_AUTOREMOVE_SUPPLIER;
350 } else if (!(flags & DL_FLAG_AUTOREMOVE_CONSUMER)) {
351 link->flags &= ~(DL_FLAG_AUTOREMOVE_CONSUMER |
352 DL_FLAG_AUTOREMOVE_SUPPLIER);
354 if (!(link->flags & DL_FLAG_MANAGED)) {
355 kref_get(&link->kref);
356 link->flags |= DL_FLAG_MANAGED;
357 device_link_init_status(link, consumer, supplier);
362 link = kzalloc(sizeof(*link), GFP_KERNEL);
366 refcount_set(&link->rpm_active, 1);
368 if (flags & DL_FLAG_PM_RUNTIME) {
369 if (flags & DL_FLAG_RPM_ACTIVE)
370 refcount_inc(&link->rpm_active);
372 pm_runtime_new_link(consumer);
375 get_device(supplier);
376 link->supplier = supplier;
377 INIT_LIST_HEAD(&link->s_node);
378 get_device(consumer);
379 link->consumer = consumer;
380 INIT_LIST_HEAD(&link->c_node);
382 kref_init(&link->kref);
384 /* Determine the initial link state. */
385 if (flags & DL_FLAG_STATELESS)
386 link->status = DL_STATE_NONE;
388 device_link_init_status(link, consumer, supplier);
391 * Some callers expect the link creation during consumer driver probe to
392 * resume the supplier even without DL_FLAG_RPM_ACTIVE.
394 if (link->status == DL_STATE_CONSUMER_PROBE &&
395 flags & DL_FLAG_PM_RUNTIME)
396 pm_runtime_resume(supplier);
399 * Move the consumer and all of the devices depending on it to the end
400 * of dpm_list and the devices_kset list.
402 * It is necessary to hold dpm_list locked throughout all that or else
403 * we may end up suspending with a wrong ordering of it.
405 device_reorder_to_tail(consumer, NULL);
407 list_add_tail_rcu(&link->s_node, &supplier->links.consumers);
408 list_add_tail_rcu(&link->c_node, &consumer->links.suppliers);
410 dev_dbg(consumer, "Linked as a consumer to %s\n", dev_name(supplier));
414 device_links_write_unlock();
416 if ((flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) && !link)
417 pm_runtime_put(supplier);
421 EXPORT_SYMBOL_GPL(device_link_add);
424 * device_link_wait_for_supplier - Mark device as waiting for supplier
425 * @consumer: Consumer device
427 * Marks the consumer device as waiting for suppliers to become available. The
428 * consumer device will never be probed until it's unmarked as waiting for
429 * suppliers. The caller is responsible for adding the link to the supplier
430 * once the supplier device is present.
432 * This function is NOT meant to be called from the probe function of the
433 * consumer but rather from code that creates/adds the consumer device.
435 static void device_link_wait_for_supplier(struct device *consumer)
437 mutex_lock(&wfs_lock);
438 list_add_tail(&consumer->links.needs_suppliers, &wait_for_suppliers);
439 mutex_unlock(&wfs_lock);
443 * device_link_remove_from_wfs - Unmark device as waiting for supplier
444 * @consumer: Consumer device
446 * Unmark the consumer device as waiting for suppliers to become available.
448 void device_link_remove_from_wfs(struct device *consumer)
450 mutex_lock(&wfs_lock);
451 list_del_init(&consumer->links.needs_suppliers);
452 mutex_unlock(&wfs_lock);
456 * device_link_check_waiting_consumers - Try to unmark waiting consumers
458 * Loops through all consumers waiting on suppliers and tries to add all their
459 * supplier links. If that succeeds, the consumer device is unmarked as waiting
460 * for suppliers. Otherwise, they are left marked as waiting on suppliers,
462 * The add_links bus callback is expected to return 0 if it has found and added
463 * all the supplier links for the consumer device. It should return an error if
464 * it isn't able to do so.
466 * The caller of device_link_wait_for_supplier() is expected to call this once
467 * it's aware of potential suppliers becoming available.
469 static void device_link_check_waiting_consumers(void)
471 struct device *dev, *tmp;
474 mutex_lock(&wfs_lock);
475 list_for_each_entry_safe(dev, tmp, &wait_for_suppliers,
476 links.needs_suppliers) {
478 if (dev->has_edit_links)
479 ret = driver_edit_links(dev);
480 else if (dev->bus->add_links)
481 ret = dev->bus->add_links(dev);
483 list_del_init(&dev->links.needs_suppliers);
485 mutex_unlock(&wfs_lock);
488 static void device_link_free(struct device_link *link)
490 while (refcount_dec_not_one(&link->rpm_active))
491 pm_runtime_put(link->supplier);
493 put_device(link->consumer);
494 put_device(link->supplier);
499 static void __device_link_free_srcu(struct rcu_head *rhead)
501 device_link_free(container_of(rhead, struct device_link, rcu_head));
504 static void __device_link_del(struct kref *kref)
506 struct device_link *link = container_of(kref, struct device_link, kref);
508 dev_dbg(link->consumer, "Dropping the link to %s\n",
509 dev_name(link->supplier));
511 if (link->flags & DL_FLAG_PM_RUNTIME)
512 pm_runtime_drop_link(link->consumer);
514 list_del_rcu(&link->s_node);
515 list_del_rcu(&link->c_node);
516 call_srcu(&device_links_srcu, &link->rcu_head, __device_link_free_srcu);
518 #else /* !CONFIG_SRCU */
519 static void __device_link_del(struct kref *kref)
521 struct device_link *link = container_of(kref, struct device_link, kref);
523 dev_info(link->consumer, "Dropping the link to %s\n",
524 dev_name(link->supplier));
526 if (link->flags & DL_FLAG_PM_RUNTIME)
527 pm_runtime_drop_link(link->consumer);
529 list_del(&link->s_node);
530 list_del(&link->c_node);
531 device_link_free(link);
533 #endif /* !CONFIG_SRCU */
535 static void device_link_put_kref(struct device_link *link)
537 if (link->flags & DL_FLAG_STATELESS)
538 kref_put(&link->kref, __device_link_del);
540 WARN(1, "Unable to drop a managed device link reference\n");
544 * device_link_del - Delete a stateless link between two devices.
545 * @link: Device link to delete.
547 * The caller must ensure proper synchronization of this function with runtime
548 * PM. If the link was added multiple times, it needs to be deleted as often.
549 * Care is required for hotplugged devices: Their links are purged on removal
550 * and calling device_link_del() is then no longer allowed.
552 void device_link_del(struct device_link *link)
554 device_links_write_lock();
556 device_link_put_kref(link);
558 device_links_write_unlock();
560 EXPORT_SYMBOL_GPL(device_link_del);
563 * device_link_remove - Delete a stateless link between two devices.
564 * @consumer: Consumer end of the link.
565 * @supplier: Supplier end of the link.
567 * The caller must ensure proper synchronization of this function with runtime
570 void device_link_remove(void *consumer, struct device *supplier)
572 struct device_link *link;
574 if (WARN_ON(consumer == supplier))
577 device_links_write_lock();
580 list_for_each_entry(link, &supplier->links.consumers, s_node) {
581 if (link->consumer == consumer) {
582 device_link_put_kref(link);
588 device_links_write_unlock();
590 EXPORT_SYMBOL_GPL(device_link_remove);
592 static void device_links_missing_supplier(struct device *dev)
594 struct device_link *link;
596 list_for_each_entry(link, &dev->links.suppliers, c_node)
597 if (link->status == DL_STATE_CONSUMER_PROBE)
598 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
602 * device_links_check_suppliers - Check presence of supplier drivers.
603 * @dev: Consumer device.
605 * Check links from this device to any suppliers. Walk the list of the device's
606 * links to suppliers and see if all of them are available. If not, simply
607 * return -EPROBE_DEFER.
609 * We need to guarantee that the supplier will not go away after the check has
610 * been positive here. It only can go away in __device_release_driver() and
611 * that function checks the device's links to consumers. This means we need to
612 * mark the link as "consumer probe in progress" to make the supplier removal
613 * wait for us to complete (or bad things may happen).
615 * Links without the DL_FLAG_MANAGED flag set are ignored.
617 int device_links_check_suppliers(struct device *dev)
619 struct device_link *link;
623 * If a device is waiting for one or more suppliers (in
624 * wait_for_suppliers list), it is not ready to probe yet. So just
625 * return -EPROBE_DEFER without having to check the links with existing
628 mutex_lock(&wfs_lock);
629 if (!list_empty(&dev->links.needs_suppliers)) {
630 mutex_unlock(&wfs_lock);
631 return -EPROBE_DEFER;
633 mutex_unlock(&wfs_lock);
635 device_links_write_lock();
637 list_for_each_entry(link, &dev->links.suppliers, c_node) {
638 if (!(link->flags & DL_FLAG_MANAGED))
641 if (link->status != DL_STATE_AVAILABLE) {
642 device_links_missing_supplier(dev);
646 WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE);
648 dev->links.status = DL_DEV_PROBING;
650 device_links_write_unlock();
655 * device_links_driver_bound - Update device links after probing its driver.
656 * @dev: Device to update the links for.
658 * The probe has been successful, so update links from this device to any
659 * consumers by changing their status to "available".
661 * Also change the status of @dev's links to suppliers to "active".
663 * Links without the DL_FLAG_MANAGED flag set are ignored.
665 void device_links_driver_bound(struct device *dev)
667 struct device_link *link;
669 device_links_write_lock();
671 list_for_each_entry(link, &dev->links.consumers, s_node) {
672 if (!(link->flags & DL_FLAG_MANAGED))
676 * Links created during consumer probe may be in the "consumer
677 * probe" state to start with if the supplier is still probing
678 * when they are created and they may become "active" if the
679 * consumer probe returns first. Skip them here.
681 if (link->status == DL_STATE_CONSUMER_PROBE ||
682 link->status == DL_STATE_ACTIVE)
685 WARN_ON(link->status != DL_STATE_DORMANT);
686 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
688 if (link->flags & DL_FLAG_AUTOPROBE_CONSUMER)
689 driver_deferred_probe_add(link->consumer);
692 list_for_each_entry(link, &dev->links.suppliers, c_node) {
693 if (!(link->flags & DL_FLAG_MANAGED))
696 WARN_ON(link->status != DL_STATE_CONSUMER_PROBE);
697 WRITE_ONCE(link->status, DL_STATE_ACTIVE);
700 dev->links.status = DL_DEV_DRIVER_BOUND;
702 device_links_write_unlock();
705 static void device_link_drop_managed(struct device_link *link)
707 link->flags &= ~DL_FLAG_MANAGED;
708 WRITE_ONCE(link->status, DL_STATE_NONE);
709 kref_put(&link->kref, __device_link_del);
713 * __device_links_no_driver - Update links of a device without a driver.
714 * @dev: Device without a drvier.
716 * Delete all non-persistent links from this device to any suppliers.
718 * Persistent links stay around, but their status is changed to "available",
719 * unless they already are in the "supplier unbind in progress" state in which
720 * case they need not be updated.
722 * Links without the DL_FLAG_MANAGED flag set are ignored.
724 static void __device_links_no_driver(struct device *dev)
726 struct device_link *link, *ln;
728 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
729 if (!(link->flags & DL_FLAG_MANAGED))
732 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER)
733 device_link_drop_managed(link);
734 else if (link->status == DL_STATE_CONSUMER_PROBE ||
735 link->status == DL_STATE_ACTIVE)
736 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
739 dev->links.status = DL_DEV_NO_DRIVER;
743 * device_links_no_driver - Update links after failing driver probe.
744 * @dev: Device whose driver has just failed to probe.
746 * Clean up leftover links to consumers for @dev and invoke
747 * %__device_links_no_driver() to update links to suppliers for it as
750 * Links without the DL_FLAG_MANAGED flag set are ignored.
752 void device_links_no_driver(struct device *dev)
754 struct device_link *link;
756 device_links_write_lock();
758 list_for_each_entry(link, &dev->links.consumers, s_node) {
759 if (!(link->flags & DL_FLAG_MANAGED))
763 * The probe has failed, so if the status of the link is
764 * "consumer probe" or "active", it must have been added by
765 * a probing consumer while this device was still probing.
766 * Change its state to "dormant", as it represents a valid
767 * relationship, but it is not functionally meaningful.
769 if (link->status == DL_STATE_CONSUMER_PROBE ||
770 link->status == DL_STATE_ACTIVE)
771 WRITE_ONCE(link->status, DL_STATE_DORMANT);
774 __device_links_no_driver(dev);
776 device_links_write_unlock();
780 * device_links_driver_cleanup - Update links after driver removal.
781 * @dev: Device whose driver has just gone away.
783 * Update links to consumers for @dev by changing their status to "dormant" and
784 * invoke %__device_links_no_driver() to update links to suppliers for it as
787 * Links without the DL_FLAG_MANAGED flag set are ignored.
789 void device_links_driver_cleanup(struct device *dev)
791 struct device_link *link, *ln;
793 device_links_write_lock();
795 list_for_each_entry_safe(link, ln, &dev->links.consumers, s_node) {
796 if (!(link->flags & DL_FLAG_MANAGED))
799 WARN_ON(link->flags & DL_FLAG_AUTOREMOVE_CONSUMER);
800 WARN_ON(link->status != DL_STATE_SUPPLIER_UNBIND);
803 * autoremove the links between this @dev and its consumer
804 * devices that are not active, i.e. where the link state
805 * has moved to DL_STATE_SUPPLIER_UNBIND.
807 if (link->status == DL_STATE_SUPPLIER_UNBIND &&
808 link->flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
809 device_link_drop_managed(link);
811 WRITE_ONCE(link->status, DL_STATE_DORMANT);
814 __device_links_no_driver(dev);
816 device_links_write_unlock();
820 * device_links_busy - Check if there are any busy links to consumers.
821 * @dev: Device to check.
823 * Check each consumer of the device and return 'true' if its link's status
824 * is one of "consumer probe" or "active" (meaning that the given consumer is
825 * probing right now or its driver is present). Otherwise, change the link
826 * state to "supplier unbind" to prevent the consumer from being probed
827 * successfully going forward.
829 * Return 'false' if there are no probing or active consumers.
831 * Links without the DL_FLAG_MANAGED flag set are ignored.
833 bool device_links_busy(struct device *dev)
835 struct device_link *link;
838 device_links_write_lock();
840 list_for_each_entry(link, &dev->links.consumers, s_node) {
841 if (!(link->flags & DL_FLAG_MANAGED))
844 if (link->status == DL_STATE_CONSUMER_PROBE
845 || link->status == DL_STATE_ACTIVE) {
849 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
852 dev->links.status = DL_DEV_UNBINDING;
854 device_links_write_unlock();
859 * device_links_unbind_consumers - Force unbind consumers of the given device.
860 * @dev: Device to unbind the consumers of.
862 * Walk the list of links to consumers for @dev and if any of them is in the
863 * "consumer probe" state, wait for all device probes in progress to complete
866 * If that's not the case, change the status of the link to "supplier unbind"
867 * and check if the link was in the "active" state. If so, force the consumer
868 * driver to unbind and start over (the consumer will not re-probe as we have
869 * changed the state of the link already).
871 * Links without the DL_FLAG_MANAGED flag set are ignored.
873 void device_links_unbind_consumers(struct device *dev)
875 struct device_link *link;
878 device_links_write_lock();
880 list_for_each_entry(link, &dev->links.consumers, s_node) {
881 enum device_link_state status;
883 if (!(link->flags & DL_FLAG_MANAGED))
886 status = link->status;
887 if (status == DL_STATE_CONSUMER_PROBE) {
888 device_links_write_unlock();
890 wait_for_device_probe();
893 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
894 if (status == DL_STATE_ACTIVE) {
895 struct device *consumer = link->consumer;
897 get_device(consumer);
899 device_links_write_unlock();
901 device_release_driver_internal(consumer, NULL,
903 put_device(consumer);
908 device_links_write_unlock();
912 * device_links_purge - Delete existing links to other devices.
913 * @dev: Target device.
915 static void device_links_purge(struct device *dev)
917 struct device_link *link, *ln;
919 mutex_lock(&wfs_lock);
920 list_del(&dev->links.needs_suppliers);
921 mutex_unlock(&wfs_lock);
924 * Delete all of the remaining links from this device to any other
925 * devices (either consumers or suppliers).
927 device_links_write_lock();
929 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
930 WARN_ON(link->status == DL_STATE_ACTIVE);
931 __device_link_del(&link->kref);
934 list_for_each_entry_safe_reverse(link, ln, &dev->links.consumers, s_node) {
935 WARN_ON(link->status != DL_STATE_DORMANT &&
936 link->status != DL_STATE_NONE);
937 __device_link_del(&link->kref);
940 device_links_write_unlock();
943 /* Device links support end. */
945 int (*platform_notify)(struct device *dev) = NULL;
946 int (*platform_notify_remove)(struct device *dev) = NULL;
947 static struct kobject *dev_kobj;
948 struct kobject *sysfs_dev_char_kobj;
949 struct kobject *sysfs_dev_block_kobj;
951 static DEFINE_MUTEX(device_hotplug_lock);
953 void lock_device_hotplug(void)
955 mutex_lock(&device_hotplug_lock);
958 void unlock_device_hotplug(void)
960 mutex_unlock(&device_hotplug_lock);
963 int lock_device_hotplug_sysfs(void)
965 if (mutex_trylock(&device_hotplug_lock))
968 /* Avoid busy looping (5 ms of sleep should do). */
970 return restart_syscall();
974 static inline int device_is_not_partition(struct device *dev)
976 return !(dev->type == &part_type);
979 static inline int device_is_not_partition(struct device *dev)
986 device_platform_notify(struct device *dev, enum kobject_action action)
990 ret = acpi_platform_notify(dev, action);
994 ret = software_node_notify(dev, action);
998 if (platform_notify && action == KOBJ_ADD)
999 platform_notify(dev);
1000 else if (platform_notify_remove && action == KOBJ_REMOVE)
1001 platform_notify_remove(dev);
1006 * dev_driver_string - Return a device's driver name, if at all possible
1007 * @dev: struct device to get the name of
1009 * Will return the device's driver's name if it is bound to a device. If
1010 * the device is not bound to a driver, it will return the name of the bus
1011 * it is attached to. If it is not attached to a bus either, an empty
1012 * string will be returned.
1014 const char *dev_driver_string(const struct device *dev)
1016 struct device_driver *drv;
1018 /* dev->driver can change to NULL underneath us because of unbinding,
1019 * so be careful about accessing it. dev->bus and dev->class should
1020 * never change once they are set, so they don't need special care.
1022 drv = READ_ONCE(dev->driver);
1023 return drv ? drv->name :
1024 (dev->bus ? dev->bus->name :
1025 (dev->class ? dev->class->name : ""));
1027 EXPORT_SYMBOL(dev_driver_string);
1029 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
1031 static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
1034 struct device_attribute *dev_attr = to_dev_attr(attr);
1035 struct device *dev = kobj_to_dev(kobj);
1039 ret = dev_attr->show(dev, dev_attr, buf);
1040 if (ret >= (ssize_t)PAGE_SIZE) {
1041 printk("dev_attr_show: %pS returned bad count\n",
1047 static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
1048 const char *buf, size_t count)
1050 struct device_attribute *dev_attr = to_dev_attr(attr);
1051 struct device *dev = kobj_to_dev(kobj);
1054 if (dev_attr->store)
1055 ret = dev_attr->store(dev, dev_attr, buf, count);
1059 static const struct sysfs_ops dev_sysfs_ops = {
1060 .show = dev_attr_show,
1061 .store = dev_attr_store,
1064 #define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
1066 ssize_t device_store_ulong(struct device *dev,
1067 struct device_attribute *attr,
1068 const char *buf, size_t size)
1070 struct dev_ext_attribute *ea = to_ext_attr(attr);
1074 ret = kstrtoul(buf, 0, &new);
1077 *(unsigned long *)(ea->var) = new;
1078 /* Always return full write size even if we didn't consume all */
1081 EXPORT_SYMBOL_GPL(device_store_ulong);
1083 ssize_t device_show_ulong(struct device *dev,
1084 struct device_attribute *attr,
1087 struct dev_ext_attribute *ea = to_ext_attr(attr);
1088 return snprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var));
1090 EXPORT_SYMBOL_GPL(device_show_ulong);
1092 ssize_t device_store_int(struct device *dev,
1093 struct device_attribute *attr,
1094 const char *buf, size_t size)
1096 struct dev_ext_attribute *ea = to_ext_attr(attr);
1100 ret = kstrtol(buf, 0, &new);
1104 if (new > INT_MAX || new < INT_MIN)
1106 *(int *)(ea->var) = new;
1107 /* Always return full write size even if we didn't consume all */
1110 EXPORT_SYMBOL_GPL(device_store_int);
1112 ssize_t device_show_int(struct device *dev,
1113 struct device_attribute *attr,
1116 struct dev_ext_attribute *ea = to_ext_attr(attr);
1118 return snprintf(buf, PAGE_SIZE, "%d\n", *(int *)(ea->var));
1120 EXPORT_SYMBOL_GPL(device_show_int);
1122 ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
1123 const char *buf, size_t size)
1125 struct dev_ext_attribute *ea = to_ext_attr(attr);
1127 if (strtobool(buf, ea->var) < 0)
1132 EXPORT_SYMBOL_GPL(device_store_bool);
1134 ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
1137 struct dev_ext_attribute *ea = to_ext_attr(attr);
1139 return snprintf(buf, PAGE_SIZE, "%d\n", *(bool *)(ea->var));
1141 EXPORT_SYMBOL_GPL(device_show_bool);
1144 * device_release - free device structure.
1145 * @kobj: device's kobject.
1147 * This is called once the reference count for the object
1148 * reaches 0. We forward the call to the device's release
1149 * method, which should handle actually freeing the structure.
1151 static void device_release(struct kobject *kobj)
1153 struct device *dev = kobj_to_dev(kobj);
1154 struct device_private *p = dev->p;
1157 * Some platform devices are driven without driver attached
1158 * and managed resources may have been acquired. Make sure
1159 * all resources are released.
1161 * Drivers still can add resources into device after device
1162 * is deleted but alive, so release devres here to avoid
1163 * possible memory leak.
1165 devres_release_all(dev);
1169 else if (dev->type && dev->type->release)
1170 dev->type->release(dev);
1171 else if (dev->class && dev->class->dev_release)
1172 dev->class->dev_release(dev);
1174 WARN(1, KERN_ERR "Device '%s' does not have a release() function, it is broken and must be fixed. See Documentation/kobject.txt.\n",
1179 static const void *device_namespace(struct kobject *kobj)
1181 struct device *dev = kobj_to_dev(kobj);
1182 const void *ns = NULL;
1184 if (dev->class && dev->class->ns_type)
1185 ns = dev->class->namespace(dev);
1190 static void device_get_ownership(struct kobject *kobj, kuid_t *uid, kgid_t *gid)
1192 struct device *dev = kobj_to_dev(kobj);
1194 if (dev->class && dev->class->get_ownership)
1195 dev->class->get_ownership(dev, uid, gid);
1198 static struct kobj_type device_ktype = {
1199 .release = device_release,
1200 .sysfs_ops = &dev_sysfs_ops,
1201 .namespace = device_namespace,
1202 .get_ownership = device_get_ownership,
1206 static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
1208 struct kobj_type *ktype = get_ktype(kobj);
1210 if (ktype == &device_ktype) {
1211 struct device *dev = kobj_to_dev(kobj);
1220 static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
1222 struct device *dev = kobj_to_dev(kobj);
1225 return dev->bus->name;
1227 return dev->class->name;
1231 static int dev_uevent(struct kset *kset, struct kobject *kobj,
1232 struct kobj_uevent_env *env)
1234 struct device *dev = kobj_to_dev(kobj);
1237 /* add device node properties if present */
1238 if (MAJOR(dev->devt)) {
1242 kuid_t uid = GLOBAL_ROOT_UID;
1243 kgid_t gid = GLOBAL_ROOT_GID;
1245 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
1246 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
1247 name = device_get_devnode(dev, &mode, &uid, &gid, &tmp);
1249 add_uevent_var(env, "DEVNAME=%s", name);
1251 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
1252 if (!uid_eq(uid, GLOBAL_ROOT_UID))
1253 add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid));
1254 if (!gid_eq(gid, GLOBAL_ROOT_GID))
1255 add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid));
1260 if (dev->type && dev->type->name)
1261 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
1264 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
1266 /* Add common DT information about the device */
1267 of_device_uevent(dev, env);
1269 /* have the bus specific function add its stuff */
1270 if (dev->bus && dev->bus->uevent) {
1271 retval = dev->bus->uevent(dev, env);
1273 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
1274 dev_name(dev), __func__, retval);
1277 /* have the class specific function add its stuff */
1278 if (dev->class && dev->class->dev_uevent) {
1279 retval = dev->class->dev_uevent(dev, env);
1281 pr_debug("device: '%s': %s: class uevent() "
1282 "returned %d\n", dev_name(dev),
1286 /* have the device type specific function add its stuff */
1287 if (dev->type && dev->type->uevent) {
1288 retval = dev->type->uevent(dev, env);
1290 pr_debug("device: '%s': %s: dev_type uevent() "
1291 "returned %d\n", dev_name(dev),
1298 static const struct kset_uevent_ops device_uevent_ops = {
1299 .filter = dev_uevent_filter,
1300 .name = dev_uevent_name,
1301 .uevent = dev_uevent,
1304 static ssize_t uevent_show(struct device *dev, struct device_attribute *attr,
1307 struct kobject *top_kobj;
1309 struct kobj_uevent_env *env = NULL;
1314 /* search the kset, the device belongs to */
1315 top_kobj = &dev->kobj;
1316 while (!top_kobj->kset && top_kobj->parent)
1317 top_kobj = top_kobj->parent;
1318 if (!top_kobj->kset)
1321 kset = top_kobj->kset;
1322 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
1325 /* respect filter */
1326 if (kset->uevent_ops && kset->uevent_ops->filter)
1327 if (!kset->uevent_ops->filter(kset, &dev->kobj))
1330 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
1334 /* let the kset specific function add its keys */
1335 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
1339 /* copy keys to file */
1340 for (i = 0; i < env->envp_idx; i++)
1341 count += sprintf(&buf[count], "%s\n", env->envp[i]);
1347 static ssize_t uevent_store(struct device *dev, struct device_attribute *attr,
1348 const char *buf, size_t count)
1352 rc = kobject_synth_uevent(&dev->kobj, buf, count);
1355 dev_err(dev, "uevent: failed to send synthetic uevent\n");
1361 static DEVICE_ATTR_RW(uevent);
1363 static ssize_t online_show(struct device *dev, struct device_attribute *attr,
1369 val = !dev->offline;
1371 return sprintf(buf, "%u\n", val);
1374 static ssize_t online_store(struct device *dev, struct device_attribute *attr,
1375 const char *buf, size_t count)
1380 ret = strtobool(buf, &val);
1384 ret = lock_device_hotplug_sysfs();
1388 ret = val ? device_online(dev) : device_offline(dev);
1389 unlock_device_hotplug();
1390 return ret < 0 ? ret : count;
1392 static DEVICE_ATTR_RW(online);
1394 int device_add_groups(struct device *dev, const struct attribute_group **groups)
1396 return sysfs_create_groups(&dev->kobj, groups);
1398 EXPORT_SYMBOL_GPL(device_add_groups);
1400 void device_remove_groups(struct device *dev,
1401 const struct attribute_group **groups)
1403 sysfs_remove_groups(&dev->kobj, groups);
1405 EXPORT_SYMBOL_GPL(device_remove_groups);
1407 union device_attr_group_devres {
1408 const struct attribute_group *group;
1409 const struct attribute_group **groups;
1412 static int devm_attr_group_match(struct device *dev, void *res, void *data)
1414 return ((union device_attr_group_devres *)res)->group == data;
1417 static void devm_attr_group_remove(struct device *dev, void *res)
1419 union device_attr_group_devres *devres = res;
1420 const struct attribute_group *group = devres->group;
1422 dev_dbg(dev, "%s: removing group %p\n", __func__, group);
1423 sysfs_remove_group(&dev->kobj, group);
1426 static void devm_attr_groups_remove(struct device *dev, void *res)
1428 union device_attr_group_devres *devres = res;
1429 const struct attribute_group **groups = devres->groups;
1431 dev_dbg(dev, "%s: removing groups %p\n", __func__, groups);
1432 sysfs_remove_groups(&dev->kobj, groups);
1436 * devm_device_add_group - given a device, create a managed attribute group
1437 * @dev: The device to create the group for
1438 * @grp: The attribute group to create
1440 * This function creates a group for the first time. It will explicitly
1441 * warn and error if any of the attribute files being created already exist.
1443 * Returns 0 on success or error code on failure.
1445 int devm_device_add_group(struct device *dev, const struct attribute_group *grp)
1447 union device_attr_group_devres *devres;
1450 devres = devres_alloc(devm_attr_group_remove,
1451 sizeof(*devres), GFP_KERNEL);
1455 error = sysfs_create_group(&dev->kobj, grp);
1457 devres_free(devres);
1461 devres->group = grp;
1462 devres_add(dev, devres);
1465 EXPORT_SYMBOL_GPL(devm_device_add_group);
1468 * devm_device_remove_group: remove a managed group from a device
1469 * @dev: device to remove the group from
1470 * @grp: group to remove
1472 * This function removes a group of attributes from a device. The attributes
1473 * previously have to have been created for this group, otherwise it will fail.
1475 void devm_device_remove_group(struct device *dev,
1476 const struct attribute_group *grp)
1478 WARN_ON(devres_release(dev, devm_attr_group_remove,
1479 devm_attr_group_match,
1480 /* cast away const */ (void *)grp));
1482 EXPORT_SYMBOL_GPL(devm_device_remove_group);
1485 * devm_device_add_groups - create a bunch of managed attribute groups
1486 * @dev: The device to create the group for
1487 * @groups: The attribute groups to create, NULL terminated
1489 * This function creates a bunch of managed attribute groups. If an error
1490 * occurs when creating a group, all previously created groups will be
1491 * removed, unwinding everything back to the original state when this
1492 * function was called. It will explicitly warn and error if any of the
1493 * attribute files being created already exist.
1495 * Returns 0 on success or error code from sysfs_create_group on failure.
1497 int devm_device_add_groups(struct device *dev,
1498 const struct attribute_group **groups)
1500 union device_attr_group_devres *devres;
1503 devres = devres_alloc(devm_attr_groups_remove,
1504 sizeof(*devres), GFP_KERNEL);
1508 error = sysfs_create_groups(&dev->kobj, groups);
1510 devres_free(devres);
1514 devres->groups = groups;
1515 devres_add(dev, devres);
1518 EXPORT_SYMBOL_GPL(devm_device_add_groups);
1521 * devm_device_remove_groups - remove a list of managed groups
1523 * @dev: The device for the groups to be removed from
1524 * @groups: NULL terminated list of groups to be removed
1526 * If groups is not NULL, remove the specified groups from the device.
1528 void devm_device_remove_groups(struct device *dev,
1529 const struct attribute_group **groups)
1531 WARN_ON(devres_release(dev, devm_attr_groups_remove,
1532 devm_attr_group_match,
1533 /* cast away const */ (void *)groups));
1535 EXPORT_SYMBOL_GPL(devm_device_remove_groups);
1537 static int device_add_attrs(struct device *dev)
1539 struct class *class = dev->class;
1540 const struct device_type *type = dev->type;
1544 error = device_add_groups(dev, class->dev_groups);
1550 error = device_add_groups(dev, type->groups);
1552 goto err_remove_class_groups;
1555 error = device_add_groups(dev, dev->groups);
1557 goto err_remove_type_groups;
1559 if (device_supports_offline(dev) && !dev->offline_disabled) {
1560 error = device_create_file(dev, &dev_attr_online);
1562 goto err_remove_dev_groups;
1567 err_remove_dev_groups:
1568 device_remove_groups(dev, dev->groups);
1569 err_remove_type_groups:
1571 device_remove_groups(dev, type->groups);
1572 err_remove_class_groups:
1574 device_remove_groups(dev, class->dev_groups);
1579 static void device_remove_attrs(struct device *dev)
1581 struct class *class = dev->class;
1582 const struct device_type *type = dev->type;
1584 device_remove_file(dev, &dev_attr_online);
1585 device_remove_groups(dev, dev->groups);
1588 device_remove_groups(dev, type->groups);
1591 device_remove_groups(dev, class->dev_groups);
1594 static ssize_t dev_show(struct device *dev, struct device_attribute *attr,
1597 return print_dev_t(buf, dev->devt);
1599 static DEVICE_ATTR_RO(dev);
1602 struct kset *devices_kset;
1605 * devices_kset_move_before - Move device in the devices_kset's list.
1606 * @deva: Device to move.
1607 * @devb: Device @deva should come before.
1609 static void devices_kset_move_before(struct device *deva, struct device *devb)
1613 pr_debug("devices_kset: Moving %s before %s\n",
1614 dev_name(deva), dev_name(devb));
1615 spin_lock(&devices_kset->list_lock);
1616 list_move_tail(&deva->kobj.entry, &devb->kobj.entry);
1617 spin_unlock(&devices_kset->list_lock);
1621 * devices_kset_move_after - Move device in the devices_kset's list.
1622 * @deva: Device to move
1623 * @devb: Device @deva should come after.
1625 static void devices_kset_move_after(struct device *deva, struct device *devb)
1629 pr_debug("devices_kset: Moving %s after %s\n",
1630 dev_name(deva), dev_name(devb));
1631 spin_lock(&devices_kset->list_lock);
1632 list_move(&deva->kobj.entry, &devb->kobj.entry);
1633 spin_unlock(&devices_kset->list_lock);
1637 * devices_kset_move_last - move the device to the end of devices_kset's list.
1638 * @dev: device to move
1640 void devices_kset_move_last(struct device *dev)
1644 pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev));
1645 spin_lock(&devices_kset->list_lock);
1646 list_move_tail(&dev->kobj.entry, &devices_kset->list);
1647 spin_unlock(&devices_kset->list_lock);
1651 * device_create_file - create sysfs attribute file for device.
1653 * @attr: device attribute descriptor.
1655 int device_create_file(struct device *dev,
1656 const struct device_attribute *attr)
1661 WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
1662 "Attribute %s: write permission without 'store'\n",
1664 WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
1665 "Attribute %s: read permission without 'show'\n",
1667 error = sysfs_create_file(&dev->kobj, &attr->attr);
1672 EXPORT_SYMBOL_GPL(device_create_file);
1675 * device_remove_file - remove sysfs attribute file.
1677 * @attr: device attribute descriptor.
1679 void device_remove_file(struct device *dev,
1680 const struct device_attribute *attr)
1683 sysfs_remove_file(&dev->kobj, &attr->attr);
1685 EXPORT_SYMBOL_GPL(device_remove_file);
1688 * device_remove_file_self - remove sysfs attribute file from its own method.
1690 * @attr: device attribute descriptor.
1692 * See kernfs_remove_self() for details.
1694 bool device_remove_file_self(struct device *dev,
1695 const struct device_attribute *attr)
1698 return sysfs_remove_file_self(&dev->kobj, &attr->attr);
1702 EXPORT_SYMBOL_GPL(device_remove_file_self);
1705 * device_create_bin_file - create sysfs binary attribute file for device.
1707 * @attr: device binary attribute descriptor.
1709 int device_create_bin_file(struct device *dev,
1710 const struct bin_attribute *attr)
1712 int error = -EINVAL;
1714 error = sysfs_create_bin_file(&dev->kobj, attr);
1717 EXPORT_SYMBOL_GPL(device_create_bin_file);
1720 * device_remove_bin_file - remove sysfs binary attribute file
1722 * @attr: device binary attribute descriptor.
1724 void device_remove_bin_file(struct device *dev,
1725 const struct bin_attribute *attr)
1728 sysfs_remove_bin_file(&dev->kobj, attr);
1730 EXPORT_SYMBOL_GPL(device_remove_bin_file);
1732 static void klist_children_get(struct klist_node *n)
1734 struct device_private *p = to_device_private_parent(n);
1735 struct device *dev = p->device;
1740 static void klist_children_put(struct klist_node *n)
1742 struct device_private *p = to_device_private_parent(n);
1743 struct device *dev = p->device;
1749 * device_initialize - init device structure.
1752 * This prepares the device for use by other layers by initializing
1754 * It is the first half of device_register(), if called by
1755 * that function, though it can also be called separately, so one
1756 * may use @dev's fields. In particular, get_device()/put_device()
1757 * may be used for reference counting of @dev after calling this
1760 * All fields in @dev must be initialized by the caller to 0, except
1761 * for those explicitly set to some other value. The simplest
1762 * approach is to use kzalloc() to allocate the structure containing
1765 * NOTE: Use put_device() to give up your reference instead of freeing
1766 * @dev directly once you have called this function.
1768 void device_initialize(struct device *dev)
1770 dev->kobj.kset = devices_kset;
1771 kobject_init(&dev->kobj, &device_ktype);
1772 INIT_LIST_HEAD(&dev->dma_pools);
1773 mutex_init(&dev->mutex);
1774 #ifdef CONFIG_PROVE_LOCKING
1775 mutex_init(&dev->lockdep_mutex);
1777 lockdep_set_novalidate_class(&dev->mutex);
1778 spin_lock_init(&dev->devres_lock);
1779 INIT_LIST_HEAD(&dev->devres_head);
1780 device_pm_init(dev);
1781 set_dev_node(dev, -1);
1782 #ifdef CONFIG_GENERIC_MSI_IRQ
1783 INIT_LIST_HEAD(&dev->msi_list);
1785 INIT_LIST_HEAD(&dev->links.consumers);
1786 INIT_LIST_HEAD(&dev->links.suppliers);
1787 INIT_LIST_HEAD(&dev->links.needs_suppliers);
1788 dev->links.status = DL_DEV_NO_DRIVER;
1790 EXPORT_SYMBOL_GPL(device_initialize);
1792 struct kobject *virtual_device_parent(struct device *dev)
1794 static struct kobject *virtual_dir = NULL;
1797 virtual_dir = kobject_create_and_add("virtual",
1798 &devices_kset->kobj);
1804 struct kobject kobj;
1805 struct class *class;
1808 #define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
1810 static void class_dir_release(struct kobject *kobj)
1812 struct class_dir *dir = to_class_dir(kobj);
1817 struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
1819 struct class_dir *dir = to_class_dir(kobj);
1820 return dir->class->ns_type;
1823 static struct kobj_type class_dir_ktype = {
1824 .release = class_dir_release,
1825 .sysfs_ops = &kobj_sysfs_ops,
1826 .child_ns_type = class_dir_child_ns_type
1829 static struct kobject *
1830 class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
1832 struct class_dir *dir;
1835 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1837 return ERR_PTR(-ENOMEM);
1840 kobject_init(&dir->kobj, &class_dir_ktype);
1842 dir->kobj.kset = &class->p->glue_dirs;
1844 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
1846 kobject_put(&dir->kobj);
1847 return ERR_PTR(retval);
1852 static DEFINE_MUTEX(gdp_mutex);
1854 static struct kobject *get_device_parent(struct device *dev,
1855 struct device *parent)
1858 struct kobject *kobj = NULL;
1859 struct kobject *parent_kobj;
1863 /* block disks show up in /sys/block */
1864 if (sysfs_deprecated && dev->class == &block_class) {
1865 if (parent && parent->class == &block_class)
1866 return &parent->kobj;
1867 return &block_class.p->subsys.kobj;
1872 * If we have no parent, we live in "virtual".
1873 * Class-devices with a non class-device as parent, live
1874 * in a "glue" directory to prevent namespace collisions.
1877 parent_kobj = virtual_device_parent(dev);
1878 else if (parent->class && !dev->class->ns_type)
1879 return &parent->kobj;
1881 parent_kobj = &parent->kobj;
1883 mutex_lock(&gdp_mutex);
1885 /* find our class-directory at the parent and reference it */
1886 spin_lock(&dev->class->p->glue_dirs.list_lock);
1887 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
1888 if (k->parent == parent_kobj) {
1889 kobj = kobject_get(k);
1892 spin_unlock(&dev->class->p->glue_dirs.list_lock);
1894 mutex_unlock(&gdp_mutex);
1898 /* or create a new class-directory at the parent device */
1899 k = class_dir_create_and_add(dev->class, parent_kobj);
1900 /* do not emit an uevent for this simple "glue" directory */
1901 mutex_unlock(&gdp_mutex);
1905 /* subsystems can specify a default root directory for their devices */
1906 if (!parent && dev->bus && dev->bus->dev_root)
1907 return &dev->bus->dev_root->kobj;
1910 return &parent->kobj;
1914 static inline bool live_in_glue_dir(struct kobject *kobj,
1917 if (!kobj || !dev->class ||
1918 kobj->kset != &dev->class->p->glue_dirs)
1923 static inline struct kobject *get_glue_dir(struct device *dev)
1925 return dev->kobj.parent;
1929 * make sure cleaning up dir as the last step, we need to make
1930 * sure .release handler of kobject is run with holding the
1933 static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
1937 /* see if we live in a "glue" directory */
1938 if (!live_in_glue_dir(glue_dir, dev))
1941 mutex_lock(&gdp_mutex);
1943 * There is a race condition between removing glue directory
1944 * and adding a new device under the glue directory.
1949 * get_device_parent()
1950 * class_dir_create_and_add()
1951 * kobject_add_internal()
1952 * create_dir() // create glue_dir
1955 * get_device_parent()
1956 * kobject_get() // get glue_dir
1959 * cleanup_glue_dir()
1960 * kobject_del(glue_dir)
1963 * kobject_add_internal()
1964 * create_dir() // in glue_dir
1965 * sysfs_create_dir_ns()
1966 * kernfs_create_dir_ns(sd)
1968 * sysfs_remove_dir() // glue_dir->sd=NULL
1969 * sysfs_put() // free glue_dir->sd
1972 * kernfs_new_node(sd)
1973 * kernfs_get(glue_dir)
1977 * Before CPU1 remove last child device under glue dir, if CPU2 add
1978 * a new device under glue dir, the glue_dir kobject reference count
1979 * will be increase to 2 in kobject_get(k). And CPU2 has been called
1980 * kernfs_create_dir_ns(). Meanwhile, CPU1 call sysfs_remove_dir()
1981 * and sysfs_put(). This result in glue_dir->sd is freed.
1983 * Then the CPU2 will see a stale "empty" but still potentially used
1984 * glue dir around in kernfs_new_node().
1986 * In order to avoid this happening, we also should make sure that
1987 * kernfs_node for glue_dir is released in CPU1 only when refcount
1988 * for glue_dir kobj is 1.
1990 ref = kref_read(&glue_dir->kref);
1991 if (!kobject_has_children(glue_dir) && !--ref)
1992 kobject_del(glue_dir);
1993 kobject_put(glue_dir);
1994 mutex_unlock(&gdp_mutex);
1997 static int device_add_class_symlinks(struct device *dev)
1999 struct device_node *of_node = dev_of_node(dev);
2003 error = sysfs_create_link(&dev->kobj, of_node_kobj(of_node), "of_node");
2005 dev_warn(dev, "Error %d creating of_node link\n",error);
2006 /* An error here doesn't warrant bringing down the device */
2012 error = sysfs_create_link(&dev->kobj,
2013 &dev->class->p->subsys.kobj,
2018 if (dev->parent && device_is_not_partition(dev)) {
2019 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
2026 /* /sys/block has directories and does not need symlinks */
2027 if (sysfs_deprecated && dev->class == &block_class)
2031 /* link in the class directory pointing to the device */
2032 error = sysfs_create_link(&dev->class->p->subsys.kobj,
2033 &dev->kobj, dev_name(dev));
2040 sysfs_remove_link(&dev->kobj, "device");
2043 sysfs_remove_link(&dev->kobj, "subsystem");
2045 sysfs_remove_link(&dev->kobj, "of_node");
2049 static void device_remove_class_symlinks(struct device *dev)
2051 if (dev_of_node(dev))
2052 sysfs_remove_link(&dev->kobj, "of_node");
2057 if (dev->parent && device_is_not_partition(dev))
2058 sysfs_remove_link(&dev->kobj, "device");
2059 sysfs_remove_link(&dev->kobj, "subsystem");
2061 if (sysfs_deprecated && dev->class == &block_class)
2064 sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
2068 * dev_set_name - set a device name
2070 * @fmt: format string for the device's name
2072 int dev_set_name(struct device *dev, const char *fmt, ...)
2077 va_start(vargs, fmt);
2078 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
2082 EXPORT_SYMBOL_GPL(dev_set_name);
2085 * device_to_dev_kobj - select a /sys/dev/ directory for the device
2088 * By default we select char/ for new entries. Setting class->dev_obj
2089 * to NULL prevents an entry from being created. class->dev_kobj must
2090 * be set (or cleared) before any devices are registered to the class
2091 * otherwise device_create_sys_dev_entry() and
2092 * device_remove_sys_dev_entry() will disagree about the presence of
2095 static struct kobject *device_to_dev_kobj(struct device *dev)
2097 struct kobject *kobj;
2100 kobj = dev->class->dev_kobj;
2102 kobj = sysfs_dev_char_kobj;
2107 static int device_create_sys_dev_entry(struct device *dev)
2109 struct kobject *kobj = device_to_dev_kobj(dev);
2114 format_dev_t(devt_str, dev->devt);
2115 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
2121 static void device_remove_sys_dev_entry(struct device *dev)
2123 struct kobject *kobj = device_to_dev_kobj(dev);
2127 format_dev_t(devt_str, dev->devt);
2128 sysfs_remove_link(kobj, devt_str);
2132 static int device_private_init(struct device *dev)
2134 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
2137 dev->p->device = dev;
2138 klist_init(&dev->p->klist_children, klist_children_get,
2139 klist_children_put);
2140 INIT_LIST_HEAD(&dev->p->deferred_probe);
2145 * device_add - add device to device hierarchy.
2148 * This is part 2 of device_register(), though may be called
2149 * separately _iff_ device_initialize() has been called separately.
2151 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
2152 * to the global and sibling lists for the device, then
2153 * adds it to the other relevant subsystems of the driver model.
2155 * Do not call this routine or device_register() more than once for
2156 * any device structure. The driver model core is not designed to work
2157 * with devices that get unregistered and then spring back to life.
2158 * (Among other things, it's very hard to guarantee that all references
2159 * to the previous incarnation of @dev have been dropped.) Allocate
2160 * and register a fresh new struct device instead.
2162 * NOTE: _Never_ directly free @dev after calling this function, even
2163 * if it returned an error! Always use put_device() to give up your
2164 * reference instead.
2166 * Rule of thumb is: if device_add() succeeds, you should call
2167 * device_del() when you want to get rid of it. If device_add() has
2168 * *not* succeeded, use *only* put_device() to drop the reference
2171 int device_add(struct device *dev)
2173 struct device *parent;
2174 struct kobject *kobj;
2175 struct class_interface *class_intf;
2176 int error = -EINVAL;
2177 struct kobject *glue_dir = NULL;
2179 dev = get_device(dev);
2184 error = device_private_init(dev);
2190 * for statically allocated devices, which should all be converted
2191 * some day, we need to initialize the name. We prevent reading back
2192 * the name, and force the use of dev_name()
2194 if (dev->init_name) {
2195 dev_set_name(dev, "%s", dev->init_name);
2196 dev->init_name = NULL;
2199 /* subsystems can specify simple device enumeration */
2200 if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
2201 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
2203 if (!dev_name(dev)) {
2208 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
2210 parent = get_device(dev->parent);
2211 kobj = get_device_parent(dev, parent);
2213 error = PTR_ERR(kobj);
2217 dev->kobj.parent = kobj;
2219 /* use parent numa_node */
2220 if (parent && (dev_to_node(dev) == NUMA_NO_NODE))
2221 set_dev_node(dev, dev_to_node(parent));
2223 /* first, register with generic layer. */
2224 /* we require the name to be set before, and pass NULL */
2225 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
2227 glue_dir = get_glue_dir(dev);
2231 /* notify platform of device entry */
2232 error = device_platform_notify(dev, KOBJ_ADD);
2234 goto platform_error;
2236 error = device_create_file(dev, &dev_attr_uevent);
2240 error = device_add_class_symlinks(dev);
2243 error = device_add_attrs(dev);
2246 error = bus_add_device(dev);
2249 error = dpm_sysfs_add(dev);
2254 if (MAJOR(dev->devt)) {
2255 error = device_create_file(dev, &dev_attr_dev);
2259 error = device_create_sys_dev_entry(dev);
2263 devtmpfs_create_node(dev);
2266 /* Notify clients of device addition. This call must come
2267 * after dpm_sysfs_add() and before kobject_uevent().
2270 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2271 BUS_NOTIFY_ADD_DEVICE, dev);
2273 kobject_uevent(&dev->kobj, KOBJ_ADD);
2276 * Check if any of the other devices (consumers) have been waiting for
2277 * this device (supplier) to be added so that they can create a device
2280 * This needs to happen after device_pm_add() because device_link_add()
2281 * requires the supplier be registered before it's called.
2283 * But this also needs to happe before bus_probe_device() to make sure
2284 * waiting consumers can link to it before the driver is bound to the
2285 * device and the driver sync_state callback is called for this device.
2287 device_link_check_waiting_consumers();
2289 if (dev->bus && dev->bus->add_links && dev->bus->add_links(dev))
2290 device_link_wait_for_supplier(dev);
2292 bus_probe_device(dev);
2294 klist_add_tail(&dev->p->knode_parent,
2295 &parent->p->klist_children);
2298 mutex_lock(&dev->class->p->mutex);
2299 /* tie the class to the device */
2300 klist_add_tail(&dev->p->knode_class,
2301 &dev->class->p->klist_devices);
2303 /* notify any interfaces that the device is here */
2304 list_for_each_entry(class_intf,
2305 &dev->class->p->interfaces, node)
2306 if (class_intf->add_dev)
2307 class_intf->add_dev(dev, class_intf);
2308 mutex_unlock(&dev->class->p->mutex);
2314 if (MAJOR(dev->devt))
2315 device_remove_file(dev, &dev_attr_dev);
2317 device_pm_remove(dev);
2318 dpm_sysfs_remove(dev);
2320 bus_remove_device(dev);
2322 device_remove_attrs(dev);
2324 device_remove_class_symlinks(dev);
2326 device_remove_file(dev, &dev_attr_uevent);
2328 device_platform_notify(dev, KOBJ_REMOVE);
2330 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
2331 glue_dir = get_glue_dir(dev);
2332 kobject_del(&dev->kobj);
2334 cleanup_glue_dir(dev, glue_dir);
2342 EXPORT_SYMBOL_GPL(device_add);
2345 * device_register - register a device with the system.
2346 * @dev: pointer to the device structure
2348 * This happens in two clean steps - initialize the device
2349 * and add it to the system. The two steps can be called
2350 * separately, but this is the easiest and most common.
2351 * I.e. you should only call the two helpers separately if
2352 * have a clearly defined need to use and refcount the device
2353 * before it is added to the hierarchy.
2355 * For more information, see the kerneldoc for device_initialize()
2358 * NOTE: _Never_ directly free @dev after calling this function, even
2359 * if it returned an error! Always use put_device() to give up the
2360 * reference initialized in this function instead.
2362 int device_register(struct device *dev)
2364 device_initialize(dev);
2365 return device_add(dev);
2367 EXPORT_SYMBOL_GPL(device_register);
2370 * get_device - increment reference count for device.
2373 * This simply forwards the call to kobject_get(), though
2374 * we do take care to provide for the case that we get a NULL
2375 * pointer passed in.
2377 struct device *get_device(struct device *dev)
2379 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
2381 EXPORT_SYMBOL_GPL(get_device);
2384 * put_device - decrement reference count.
2385 * @dev: device in question.
2387 void put_device(struct device *dev)
2389 /* might_sleep(); */
2391 kobject_put(&dev->kobj);
2393 EXPORT_SYMBOL_GPL(put_device);
2395 bool kill_device(struct device *dev)
2398 * Require the device lock and set the "dead" flag to guarantee that
2399 * the update behavior is consistent with the other bitfields near
2400 * it and that we cannot have an asynchronous probe routine trying
2401 * to run while we are tearing out the bus/class/sysfs from
2402 * underneath the device.
2404 lockdep_assert_held(&dev->mutex);
2408 dev->p->dead = true;
2411 EXPORT_SYMBOL_GPL(kill_device);
2414 * device_del - delete device from system.
2417 * This is the first part of the device unregistration
2418 * sequence. This removes the device from the lists we control
2419 * from here, has it removed from the other driver model
2420 * subsystems it was added to in device_add(), and removes it
2421 * from the kobject hierarchy.
2423 * NOTE: this should be called manually _iff_ device_add() was
2424 * also called manually.
2426 void device_del(struct device *dev)
2428 struct device *parent = dev->parent;
2429 struct kobject *glue_dir = NULL;
2430 struct class_interface *class_intf;
2436 /* Notify clients of device removal. This call must come
2437 * before dpm_sysfs_remove().
2440 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2441 BUS_NOTIFY_DEL_DEVICE, dev);
2443 dpm_sysfs_remove(dev);
2445 klist_del(&dev->p->knode_parent);
2446 if (MAJOR(dev->devt)) {
2447 devtmpfs_delete_node(dev);
2448 device_remove_sys_dev_entry(dev);
2449 device_remove_file(dev, &dev_attr_dev);
2452 device_remove_class_symlinks(dev);
2454 mutex_lock(&dev->class->p->mutex);
2455 /* notify any interfaces that the device is now gone */
2456 list_for_each_entry(class_intf,
2457 &dev->class->p->interfaces, node)
2458 if (class_intf->remove_dev)
2459 class_intf->remove_dev(dev, class_intf);
2460 /* remove the device from the class list */
2461 klist_del(&dev->p->knode_class);
2462 mutex_unlock(&dev->class->p->mutex);
2464 device_remove_file(dev, &dev_attr_uevent);
2465 device_remove_attrs(dev);
2466 bus_remove_device(dev);
2467 device_pm_remove(dev);
2468 driver_deferred_probe_del(dev);
2469 device_platform_notify(dev, KOBJ_REMOVE);
2470 device_remove_properties(dev);
2471 device_links_purge(dev);
2474 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2475 BUS_NOTIFY_REMOVED_DEVICE, dev);
2476 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
2477 glue_dir = get_glue_dir(dev);
2478 kobject_del(&dev->kobj);
2479 cleanup_glue_dir(dev, glue_dir);
2482 EXPORT_SYMBOL_GPL(device_del);
2485 * device_unregister - unregister device from system.
2486 * @dev: device going away.
2488 * We do this in two parts, like we do device_register(). First,
2489 * we remove it from all the subsystems with device_del(), then
2490 * we decrement the reference count via put_device(). If that
2491 * is the final reference count, the device will be cleaned up
2492 * via device_release() above. Otherwise, the structure will
2493 * stick around until the final reference to the device is dropped.
2495 void device_unregister(struct device *dev)
2497 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
2501 EXPORT_SYMBOL_GPL(device_unregister);
2503 static struct device *prev_device(struct klist_iter *i)
2505 struct klist_node *n = klist_prev(i);
2506 struct device *dev = NULL;
2507 struct device_private *p;
2510 p = to_device_private_parent(n);
2516 static struct device *next_device(struct klist_iter *i)
2518 struct klist_node *n = klist_next(i);
2519 struct device *dev = NULL;
2520 struct device_private *p;
2523 p = to_device_private_parent(n);
2530 * device_get_devnode - path of device node file
2532 * @mode: returned file access mode
2533 * @uid: returned file owner
2534 * @gid: returned file group
2535 * @tmp: possibly allocated string
2537 * Return the relative path of a possible device node.
2538 * Non-default names may need to allocate a memory to compose
2539 * a name. This memory is returned in tmp and needs to be
2540 * freed by the caller.
2542 const char *device_get_devnode(struct device *dev,
2543 umode_t *mode, kuid_t *uid, kgid_t *gid,
2550 /* the device type may provide a specific name */
2551 if (dev->type && dev->type->devnode)
2552 *tmp = dev->type->devnode(dev, mode, uid, gid);
2556 /* the class may provide a specific name */
2557 if (dev->class && dev->class->devnode)
2558 *tmp = dev->class->devnode(dev, mode);
2562 /* return name without allocation, tmp == NULL */
2563 if (strchr(dev_name(dev), '!') == NULL)
2564 return dev_name(dev);
2566 /* replace '!' in the name with '/' */
2567 s = kstrdup(dev_name(dev), GFP_KERNEL);
2570 strreplace(s, '!', '/');
2575 * device_for_each_child - device child iterator.
2576 * @parent: parent struct device.
2577 * @fn: function to be called for each device.
2578 * @data: data for the callback.
2580 * Iterate over @parent's child devices, and call @fn for each,
2583 * We check the return of @fn each time. If it returns anything
2584 * other than 0, we break out and return that value.
2586 int device_for_each_child(struct device *parent, void *data,
2587 int (*fn)(struct device *dev, void *data))
2589 struct klist_iter i;
2590 struct device *child;
2596 klist_iter_init(&parent->p->klist_children, &i);
2597 while (!error && (child = next_device(&i)))
2598 error = fn(child, data);
2599 klist_iter_exit(&i);
2602 EXPORT_SYMBOL_GPL(device_for_each_child);
2605 * device_for_each_child_reverse - device child iterator in reversed order.
2606 * @parent: parent struct device.
2607 * @fn: function to be called for each device.
2608 * @data: data for the callback.
2610 * Iterate over @parent's child devices, and call @fn for each,
2613 * We check the return of @fn each time. If it returns anything
2614 * other than 0, we break out and return that value.
2616 int device_for_each_child_reverse(struct device *parent, void *data,
2617 int (*fn)(struct device *dev, void *data))
2619 struct klist_iter i;
2620 struct device *child;
2626 klist_iter_init(&parent->p->klist_children, &i);
2627 while ((child = prev_device(&i)) && !error)
2628 error = fn(child, data);
2629 klist_iter_exit(&i);
2632 EXPORT_SYMBOL_GPL(device_for_each_child_reverse);
2635 * device_find_child - device iterator for locating a particular device.
2636 * @parent: parent struct device
2637 * @match: Callback function to check device
2638 * @data: Data to pass to match function
2640 * This is similar to the device_for_each_child() function above, but it
2641 * returns a reference to a device that is 'found' for later use, as
2642 * determined by the @match callback.
2644 * The callback should return 0 if the device doesn't match and non-zero
2645 * if it does. If the callback returns non-zero and a reference to the
2646 * current device can be obtained, this function will return to the caller
2647 * and not iterate over any more devices.
2649 * NOTE: you will need to drop the reference with put_device() after use.
2651 struct device *device_find_child(struct device *parent, void *data,
2652 int (*match)(struct device *dev, void *data))
2654 struct klist_iter i;
2655 struct device *child;
2660 klist_iter_init(&parent->p->klist_children, &i);
2661 while ((child = next_device(&i)))
2662 if (match(child, data) && get_device(child))
2664 klist_iter_exit(&i);
2667 EXPORT_SYMBOL_GPL(device_find_child);
2670 * device_find_child_by_name - device iterator for locating a child device.
2671 * @parent: parent struct device
2672 * @name: name of the child device
2674 * This is similar to the device_find_child() function above, but it
2675 * returns a reference to a device that has the name @name.
2677 * NOTE: you will need to drop the reference with put_device() after use.
2679 struct device *device_find_child_by_name(struct device *parent,
2682 struct klist_iter i;
2683 struct device *child;
2688 klist_iter_init(&parent->p->klist_children, &i);
2689 while ((child = next_device(&i)))
2690 if (!strcmp(dev_name(child), name) && get_device(child))
2692 klist_iter_exit(&i);
2695 EXPORT_SYMBOL_GPL(device_find_child_by_name);
2697 int __init devices_init(void)
2699 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
2702 dev_kobj = kobject_create_and_add("dev", NULL);
2705 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
2706 if (!sysfs_dev_block_kobj)
2707 goto block_kobj_err;
2708 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
2709 if (!sysfs_dev_char_kobj)
2715 kobject_put(sysfs_dev_block_kobj);
2717 kobject_put(dev_kobj);
2719 kset_unregister(devices_kset);
2723 static int device_check_offline(struct device *dev, void *not_used)
2727 ret = device_for_each_child(dev, NULL, device_check_offline);
2731 return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
2735 * device_offline - Prepare the device for hot-removal.
2736 * @dev: Device to be put offline.
2738 * Execute the device bus type's .offline() callback, if present, to prepare
2739 * the device for a subsequent hot-removal. If that succeeds, the device must
2740 * not be used until either it is removed or its bus type's .online() callback
2743 * Call under device_hotplug_lock.
2745 int device_offline(struct device *dev)
2749 if (dev->offline_disabled)
2752 ret = device_for_each_child(dev, NULL, device_check_offline);
2757 if (device_supports_offline(dev)) {
2761 ret = dev->bus->offline(dev);
2763 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
2764 dev->offline = true;
2774 * device_online - Put the device back online after successful device_offline().
2775 * @dev: Device to be put back online.
2777 * If device_offline() has been successfully executed for @dev, but the device
2778 * has not been removed subsequently, execute its bus type's .online() callback
2779 * to indicate that the device can be used again.
2781 * Call under device_hotplug_lock.
2783 int device_online(struct device *dev)
2788 if (device_supports_offline(dev)) {
2790 ret = dev->bus->online(dev);
2792 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
2793 dev->offline = false;
2804 struct root_device {
2806 struct module *owner;
2809 static inline struct root_device *to_root_device(struct device *d)
2811 return container_of(d, struct root_device, dev);
2814 static void root_device_release(struct device *dev)
2816 kfree(to_root_device(dev));
2820 * __root_device_register - allocate and register a root device
2821 * @name: root device name
2822 * @owner: owner module of the root device, usually THIS_MODULE
2824 * This function allocates a root device and registers it
2825 * using device_register(). In order to free the returned
2826 * device, use root_device_unregister().
2828 * Root devices are dummy devices which allow other devices
2829 * to be grouped under /sys/devices. Use this function to
2830 * allocate a root device and then use it as the parent of
2831 * any device which should appear under /sys/devices/{name}
2833 * The /sys/devices/{name} directory will also contain a
2834 * 'module' symlink which points to the @owner directory
2837 * Returns &struct device pointer on success, or ERR_PTR() on error.
2839 * Note: You probably want to use root_device_register().
2841 struct device *__root_device_register(const char *name, struct module *owner)
2843 struct root_device *root;
2846 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
2848 return ERR_PTR(err);
2850 err = dev_set_name(&root->dev, "%s", name);
2853 return ERR_PTR(err);
2856 root->dev.release = root_device_release;
2858 err = device_register(&root->dev);
2860 put_device(&root->dev);
2861 return ERR_PTR(err);
2864 #ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
2866 struct module_kobject *mk = &owner->mkobj;
2868 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
2870 device_unregister(&root->dev);
2871 return ERR_PTR(err);
2873 root->owner = owner;
2879 EXPORT_SYMBOL_GPL(__root_device_register);
2882 * root_device_unregister - unregister and free a root device
2883 * @dev: device going away
2885 * This function unregisters and cleans up a device that was created by
2886 * root_device_register().
2888 void root_device_unregister(struct device *dev)
2890 struct root_device *root = to_root_device(dev);
2893 sysfs_remove_link(&root->dev.kobj, "module");
2895 device_unregister(dev);
2897 EXPORT_SYMBOL_GPL(root_device_unregister);
2900 static void device_create_release(struct device *dev)
2902 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
2906 static __printf(6, 0) struct device *
2907 device_create_groups_vargs(struct class *class, struct device *parent,
2908 dev_t devt, void *drvdata,
2909 const struct attribute_group **groups,
2910 const char *fmt, va_list args)
2912 struct device *dev = NULL;
2913 int retval = -ENODEV;
2915 if (class == NULL || IS_ERR(class))
2918 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2924 device_initialize(dev);
2927 dev->parent = parent;
2928 dev->groups = groups;
2929 dev->release = device_create_release;
2930 dev_set_drvdata(dev, drvdata);
2932 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
2936 retval = device_add(dev);
2944 return ERR_PTR(retval);
2948 * device_create_vargs - creates a device and registers it with sysfs
2949 * @class: pointer to the struct class that this device should be registered to
2950 * @parent: pointer to the parent struct device of this new device, if any
2951 * @devt: the dev_t for the char device to be added
2952 * @drvdata: the data to be added to the device for callbacks
2953 * @fmt: string for the device's name
2954 * @args: va_list for the device's name
2956 * This function can be used by char device classes. A struct device
2957 * will be created in sysfs, registered to the specified class.
2959 * A "dev" file will be created, showing the dev_t for the device, if
2960 * the dev_t is not 0,0.
2961 * If a pointer to a parent struct device is passed in, the newly created
2962 * struct device will be a child of that device in sysfs.
2963 * The pointer to the struct device will be returned from the call.
2964 * Any further sysfs files that might be required can be created using this
2967 * Returns &struct device pointer on success, or ERR_PTR() on error.
2969 * Note: the struct class passed to this function must have previously
2970 * been created with a call to class_create().
2972 struct device *device_create_vargs(struct class *class, struct device *parent,
2973 dev_t devt, void *drvdata, const char *fmt,
2976 return device_create_groups_vargs(class, parent, devt, drvdata, NULL,
2979 EXPORT_SYMBOL_GPL(device_create_vargs);
2982 * device_create - creates a device and registers it with sysfs
2983 * @class: pointer to the struct class that this device should be registered to
2984 * @parent: pointer to the parent struct device of this new device, if any
2985 * @devt: the dev_t for the char device to be added
2986 * @drvdata: the data to be added to the device for callbacks
2987 * @fmt: string for the device's name
2989 * This function can be used by char device classes. A struct device
2990 * will be created in sysfs, registered to the specified class.
2992 * A "dev" file will be created, showing the dev_t for the device, if
2993 * the dev_t is not 0,0.
2994 * If a pointer to a parent struct device is passed in, the newly created
2995 * struct device will be a child of that device in sysfs.
2996 * The pointer to the struct device will be returned from the call.
2997 * Any further sysfs files that might be required can be created using this
3000 * Returns &struct device pointer on success, or ERR_PTR() on error.
3002 * Note: the struct class passed to this function must have previously
3003 * been created with a call to class_create().
3005 struct device *device_create(struct class *class, struct device *parent,
3006 dev_t devt, void *drvdata, const char *fmt, ...)
3011 va_start(vargs, fmt);
3012 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
3016 EXPORT_SYMBOL_GPL(device_create);
3019 * device_create_with_groups - creates a device and registers it with sysfs
3020 * @class: pointer to the struct class that this device should be registered to
3021 * @parent: pointer to the parent struct device of this new device, if any
3022 * @devt: the dev_t for the char device to be added
3023 * @drvdata: the data to be added to the device for callbacks
3024 * @groups: NULL-terminated list of attribute groups to be created
3025 * @fmt: string for the device's name
3027 * This function can be used by char device classes. A struct device
3028 * will be created in sysfs, registered to the specified class.
3029 * Additional attributes specified in the groups parameter will also
3030 * be created automatically.
3032 * A "dev" file will be created, showing the dev_t for the device, if
3033 * the dev_t is not 0,0.
3034 * If a pointer to a parent struct device is passed in, the newly created
3035 * struct device will be a child of that device in sysfs.
3036 * The pointer to the struct device will be returned from the call.
3037 * Any further sysfs files that might be required can be created using this
3040 * Returns &struct device pointer on success, or ERR_PTR() on error.
3042 * Note: the struct class passed to this function must have previously
3043 * been created with a call to class_create().
3045 struct device *device_create_with_groups(struct class *class,
3046 struct device *parent, dev_t devt,
3048 const struct attribute_group **groups,
3049 const char *fmt, ...)
3054 va_start(vargs, fmt);
3055 dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
3060 EXPORT_SYMBOL_GPL(device_create_with_groups);
3063 * device_destroy - removes a device that was created with device_create()
3064 * @class: pointer to the struct class that this device was registered with
3065 * @devt: the dev_t of the device that was previously registered
3067 * This call unregisters and cleans up a device that was created with a
3068 * call to device_create().
3070 void device_destroy(struct class *class, dev_t devt)
3074 dev = class_find_device_by_devt(class, devt);
3077 device_unregister(dev);
3080 EXPORT_SYMBOL_GPL(device_destroy);
3083 * device_rename - renames a device
3084 * @dev: the pointer to the struct device to be renamed
3085 * @new_name: the new name of the device
3087 * It is the responsibility of the caller to provide mutual
3088 * exclusion between two different calls of device_rename
3089 * on the same device to ensure that new_name is valid and
3090 * won't conflict with other devices.
3092 * Note: Don't call this function. Currently, the networking layer calls this
3093 * function, but that will change. The following text from Kay Sievers offers
3096 * Renaming devices is racy at many levels, symlinks and other stuff are not
3097 * replaced atomically, and you get a "move" uevent, but it's not easy to
3098 * connect the event to the old and new device. Device nodes are not renamed at
3099 * all, there isn't even support for that in the kernel now.
3101 * In the meantime, during renaming, your target name might be taken by another
3102 * driver, creating conflicts. Or the old name is taken directly after you
3103 * renamed it -- then you get events for the same DEVPATH, before you even see
3104 * the "move" event. It's just a mess, and nothing new should ever rely on
3105 * kernel device renaming. Besides that, it's not even implemented now for
3106 * other things than (driver-core wise very simple) network devices.
3108 * We are currently about to change network renaming in udev to completely
3109 * disallow renaming of devices in the same namespace as the kernel uses,
3110 * because we can't solve the problems properly, that arise with swapping names
3111 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
3112 * be allowed to some other name than eth[0-9]*, for the aforementioned
3115 * Make up a "real" name in the driver before you register anything, or add
3116 * some other attributes for userspace to find the device, or use udev to add
3117 * symlinks -- but never rename kernel devices later, it's a complete mess. We
3118 * don't even want to get into that and try to implement the missing pieces in
3119 * the core. We really have other pieces to fix in the driver core mess. :)
3121 int device_rename(struct device *dev, const char *new_name)
3123 struct kobject *kobj = &dev->kobj;
3124 char *old_device_name = NULL;
3127 dev = get_device(dev);
3131 dev_dbg(dev, "renaming to %s\n", new_name);
3133 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
3134 if (!old_device_name) {
3140 error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj,
3141 kobj, old_device_name,
3142 new_name, kobject_namespace(kobj));
3147 error = kobject_rename(kobj, new_name);
3154 kfree(old_device_name);
3158 EXPORT_SYMBOL_GPL(device_rename);
3160 static int device_move_class_links(struct device *dev,
3161 struct device *old_parent,
3162 struct device *new_parent)
3167 sysfs_remove_link(&dev->kobj, "device");
3169 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
3175 * device_move - moves a device to a new parent
3176 * @dev: the pointer to the struct device to be moved
3177 * @new_parent: the new parent of the device (can be NULL)
3178 * @dpm_order: how to reorder the dpm_list
3180 int device_move(struct device *dev, struct device *new_parent,
3181 enum dpm_order dpm_order)
3184 struct device *old_parent;
3185 struct kobject *new_parent_kobj;
3187 dev = get_device(dev);
3192 new_parent = get_device(new_parent);
3193 new_parent_kobj = get_device_parent(dev, new_parent);
3194 if (IS_ERR(new_parent_kobj)) {
3195 error = PTR_ERR(new_parent_kobj);
3196 put_device(new_parent);
3200 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
3201 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
3202 error = kobject_move(&dev->kobj, new_parent_kobj);
3204 cleanup_glue_dir(dev, new_parent_kobj);
3205 put_device(new_parent);
3208 old_parent = dev->parent;
3209 dev->parent = new_parent;
3211 klist_remove(&dev->p->knode_parent);
3213 klist_add_tail(&dev->p->knode_parent,
3214 &new_parent->p->klist_children);
3215 set_dev_node(dev, dev_to_node(new_parent));
3219 error = device_move_class_links(dev, old_parent, new_parent);
3221 /* We ignore errors on cleanup since we're hosed anyway... */
3222 device_move_class_links(dev, new_parent, old_parent);
3223 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
3225 klist_remove(&dev->p->knode_parent);
3226 dev->parent = old_parent;
3228 klist_add_tail(&dev->p->knode_parent,
3229 &old_parent->p->klist_children);
3230 set_dev_node(dev, dev_to_node(old_parent));
3233 cleanup_glue_dir(dev, new_parent_kobj);
3234 put_device(new_parent);
3238 switch (dpm_order) {
3239 case DPM_ORDER_NONE:
3241 case DPM_ORDER_DEV_AFTER_PARENT:
3242 device_pm_move_after(dev, new_parent);
3243 devices_kset_move_after(dev, new_parent);
3245 case DPM_ORDER_PARENT_BEFORE_DEV:
3246 device_pm_move_before(new_parent, dev);
3247 devices_kset_move_before(new_parent, dev);
3249 case DPM_ORDER_DEV_LAST:
3250 device_pm_move_last(dev);
3251 devices_kset_move_last(dev);
3255 put_device(old_parent);
3261 EXPORT_SYMBOL_GPL(device_move);
3264 * device_shutdown - call ->shutdown() on each device to shutdown.
3266 void device_shutdown(void)
3268 struct device *dev, *parent;
3270 wait_for_device_probe();
3271 device_block_probing();
3273 spin_lock(&devices_kset->list_lock);
3275 * Walk the devices list backward, shutting down each in turn.
3276 * Beware that device unplug events may also start pulling
3277 * devices offline, even as the system is shutting down.
3279 while (!list_empty(&devices_kset->list)) {
3280 dev = list_entry(devices_kset->list.prev, struct device,
3284 * hold reference count of device's parent to
3285 * prevent it from being freed because parent's
3286 * lock is to be held
3288 parent = get_device(dev->parent);
3291 * Make sure the device is off the kset list, in the
3292 * event that dev->*->shutdown() doesn't remove it.
3294 list_del_init(&dev->kobj.entry);
3295 spin_unlock(&devices_kset->list_lock);
3297 /* hold lock to avoid race with probe/release */
3299 device_lock(parent);
3302 /* Don't allow any more runtime suspends */
3303 pm_runtime_get_noresume(dev);
3304 pm_runtime_barrier(dev);
3306 if (dev->class && dev->class->shutdown_pre) {
3308 dev_info(dev, "shutdown_pre\n");
3309 dev->class->shutdown_pre(dev);
3311 if (dev->bus && dev->bus->shutdown) {
3313 dev_info(dev, "shutdown\n");
3314 dev->bus->shutdown(dev);
3315 } else if (dev->driver && dev->driver->shutdown) {
3317 dev_info(dev, "shutdown\n");
3318 dev->driver->shutdown(dev);
3323 device_unlock(parent);
3328 spin_lock(&devices_kset->list_lock);
3330 spin_unlock(&devices_kset->list_lock);
3334 * Device logging functions
3337 #ifdef CONFIG_PRINTK
3339 create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
3345 subsys = dev->class->name;
3347 subsys = dev->bus->name;
3351 pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys);
3356 * Add device identifier DEVICE=:
3360 * +sound:card0 subsystem:devname
3362 if (MAJOR(dev->devt)) {
3365 if (strcmp(subsys, "block") == 0)
3370 pos += snprintf(hdr + pos, hdrlen - pos,
3372 c, MAJOR(dev->devt), MINOR(dev->devt));
3373 } else if (strcmp(subsys, "net") == 0) {
3374 struct net_device *net = to_net_dev(dev);
3377 pos += snprintf(hdr + pos, hdrlen - pos,
3378 "DEVICE=n%u", net->ifindex);
3381 pos += snprintf(hdr + pos, hdrlen - pos,
3382 "DEVICE=+%s:%s", subsys, dev_name(dev));
3391 dev_WARN(dev, "device/subsystem name too long");
3395 int dev_vprintk_emit(int level, const struct device *dev,
3396 const char *fmt, va_list args)
3401 hdrlen = create_syslog_header(dev, hdr, sizeof(hdr));
3403 return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args);
3405 EXPORT_SYMBOL(dev_vprintk_emit);
3407 int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
3412 va_start(args, fmt);
3414 r = dev_vprintk_emit(level, dev, fmt, args);
3420 EXPORT_SYMBOL(dev_printk_emit);
3422 static void __dev_printk(const char *level, const struct device *dev,
3423 struct va_format *vaf)
3426 dev_printk_emit(level[1] - '0', dev, "%s %s: %pV",
3427 dev_driver_string(dev), dev_name(dev), vaf);
3429 printk("%s(NULL device *): %pV", level, vaf);
3432 void dev_printk(const char *level, const struct device *dev,
3433 const char *fmt, ...)
3435 struct va_format vaf;
3438 va_start(args, fmt);
3443 __dev_printk(level, dev, &vaf);
3447 EXPORT_SYMBOL(dev_printk);
3449 #define define_dev_printk_level(func, kern_level) \
3450 void func(const struct device *dev, const char *fmt, ...) \
3452 struct va_format vaf; \
3455 va_start(args, fmt); \
3460 __dev_printk(kern_level, dev, &vaf); \
3464 EXPORT_SYMBOL(func);
3466 define_dev_printk_level(_dev_emerg, KERN_EMERG);
3467 define_dev_printk_level(_dev_alert, KERN_ALERT);
3468 define_dev_printk_level(_dev_crit, KERN_CRIT);
3469 define_dev_printk_level(_dev_err, KERN_ERR);
3470 define_dev_printk_level(_dev_warn, KERN_WARNING);
3471 define_dev_printk_level(_dev_notice, KERN_NOTICE);
3472 define_dev_printk_level(_dev_info, KERN_INFO);
3476 static inline bool fwnode_is_primary(struct fwnode_handle *fwnode)
3478 return fwnode && !IS_ERR(fwnode->secondary);
3482 * set_primary_fwnode - Change the primary firmware node of a given device.
3483 * @dev: Device to handle.
3484 * @fwnode: New primary firmware node of the device.
3486 * Set the device's firmware node pointer to @fwnode, but if a secondary
3487 * firmware node of the device is present, preserve it.
3489 void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
3492 struct fwnode_handle *fn = dev->fwnode;
3494 if (fwnode_is_primary(fn))
3498 WARN_ON(fwnode->secondary);
3499 fwnode->secondary = fn;
3501 dev->fwnode = fwnode;
3503 dev->fwnode = fwnode_is_primary(dev->fwnode) ?
3504 dev->fwnode->secondary : NULL;
3507 EXPORT_SYMBOL_GPL(set_primary_fwnode);
3510 * set_secondary_fwnode - Change the secondary firmware node of a given device.
3511 * @dev: Device to handle.
3512 * @fwnode: New secondary firmware node of the device.
3514 * If a primary firmware node of the device is present, set its secondary
3515 * pointer to @fwnode. Otherwise, set the device's firmware node pointer to
3518 void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
3521 fwnode->secondary = ERR_PTR(-ENODEV);
3523 if (fwnode_is_primary(dev->fwnode))
3524 dev->fwnode->secondary = fwnode;
3526 dev->fwnode = fwnode;
3530 * device_set_of_node_from_dev - reuse device-tree node of another device
3531 * @dev: device whose device-tree node is being set
3532 * @dev2: device whose device-tree node is being reused
3534 * Takes another reference to the new device-tree node after first dropping
3535 * any reference held to the old node.
3537 void device_set_of_node_from_dev(struct device *dev, const struct device *dev2)
3539 of_node_put(dev->of_node);
3540 dev->of_node = of_node_get(dev2->of_node);
3541 dev->of_node_reused = true;
3543 EXPORT_SYMBOL_GPL(device_set_of_node_from_dev);
3545 int device_match_name(struct device *dev, const void *name)
3547 return sysfs_streq(dev_name(dev), name);
3549 EXPORT_SYMBOL_GPL(device_match_name);
3551 int device_match_of_node(struct device *dev, const void *np)
3553 return dev->of_node == np;
3555 EXPORT_SYMBOL_GPL(device_match_of_node);
3557 int device_match_fwnode(struct device *dev, const void *fwnode)
3559 return dev_fwnode(dev) == fwnode;
3561 EXPORT_SYMBOL_GPL(device_match_fwnode);
3563 int device_match_devt(struct device *dev, const void *pdevt)
3565 return dev->devt == *(dev_t *)pdevt;
3567 EXPORT_SYMBOL_GPL(device_match_devt);
3569 int device_match_acpi_dev(struct device *dev, const void *adev)
3571 return ACPI_COMPANION(dev) == adev;
3573 EXPORT_SYMBOL(device_match_acpi_dev);
3575 int device_match_any(struct device *dev, const void *unused)
3579 EXPORT_SYMBOL_GPL(device_match_any);