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 lockdep_set_novalidate_class(&dev->mutex);
1775 spin_lock_init(&dev->devres_lock);
1776 INIT_LIST_HEAD(&dev->devres_head);
1777 device_pm_init(dev);
1778 set_dev_node(dev, -1);
1779 #ifdef CONFIG_GENERIC_MSI_IRQ
1780 INIT_LIST_HEAD(&dev->msi_list);
1782 INIT_LIST_HEAD(&dev->links.consumers);
1783 INIT_LIST_HEAD(&dev->links.suppliers);
1784 INIT_LIST_HEAD(&dev->links.needs_suppliers);
1785 dev->links.status = DL_DEV_NO_DRIVER;
1787 EXPORT_SYMBOL_GPL(device_initialize);
1789 struct kobject *virtual_device_parent(struct device *dev)
1791 static struct kobject *virtual_dir = NULL;
1794 virtual_dir = kobject_create_and_add("virtual",
1795 &devices_kset->kobj);
1801 struct kobject kobj;
1802 struct class *class;
1805 #define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
1807 static void class_dir_release(struct kobject *kobj)
1809 struct class_dir *dir = to_class_dir(kobj);
1814 struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
1816 struct class_dir *dir = to_class_dir(kobj);
1817 return dir->class->ns_type;
1820 static struct kobj_type class_dir_ktype = {
1821 .release = class_dir_release,
1822 .sysfs_ops = &kobj_sysfs_ops,
1823 .child_ns_type = class_dir_child_ns_type
1826 static struct kobject *
1827 class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
1829 struct class_dir *dir;
1832 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1834 return ERR_PTR(-ENOMEM);
1837 kobject_init(&dir->kobj, &class_dir_ktype);
1839 dir->kobj.kset = &class->p->glue_dirs;
1841 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
1843 kobject_put(&dir->kobj);
1844 return ERR_PTR(retval);
1849 static DEFINE_MUTEX(gdp_mutex);
1851 static struct kobject *get_device_parent(struct device *dev,
1852 struct device *parent)
1855 struct kobject *kobj = NULL;
1856 struct kobject *parent_kobj;
1860 /* block disks show up in /sys/block */
1861 if (sysfs_deprecated && dev->class == &block_class) {
1862 if (parent && parent->class == &block_class)
1863 return &parent->kobj;
1864 return &block_class.p->subsys.kobj;
1869 * If we have no parent, we live in "virtual".
1870 * Class-devices with a non class-device as parent, live
1871 * in a "glue" directory to prevent namespace collisions.
1874 parent_kobj = virtual_device_parent(dev);
1875 else if (parent->class && !dev->class->ns_type)
1876 return &parent->kobj;
1878 parent_kobj = &parent->kobj;
1880 mutex_lock(&gdp_mutex);
1882 /* find our class-directory at the parent and reference it */
1883 spin_lock(&dev->class->p->glue_dirs.list_lock);
1884 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
1885 if (k->parent == parent_kobj) {
1886 kobj = kobject_get(k);
1889 spin_unlock(&dev->class->p->glue_dirs.list_lock);
1891 mutex_unlock(&gdp_mutex);
1895 /* or create a new class-directory at the parent device */
1896 k = class_dir_create_and_add(dev->class, parent_kobj);
1897 /* do not emit an uevent for this simple "glue" directory */
1898 mutex_unlock(&gdp_mutex);
1902 /* subsystems can specify a default root directory for their devices */
1903 if (!parent && dev->bus && dev->bus->dev_root)
1904 return &dev->bus->dev_root->kobj;
1907 return &parent->kobj;
1911 static inline bool live_in_glue_dir(struct kobject *kobj,
1914 if (!kobj || !dev->class ||
1915 kobj->kset != &dev->class->p->glue_dirs)
1920 static inline struct kobject *get_glue_dir(struct device *dev)
1922 return dev->kobj.parent;
1926 * make sure cleaning up dir as the last step, we need to make
1927 * sure .release handler of kobject is run with holding the
1930 static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
1932 /* see if we live in a "glue" directory */
1933 if (!live_in_glue_dir(glue_dir, dev))
1936 mutex_lock(&gdp_mutex);
1937 if (!kobject_has_children(glue_dir))
1938 kobject_del(glue_dir);
1939 kobject_put(glue_dir);
1940 mutex_unlock(&gdp_mutex);
1943 static int device_add_class_symlinks(struct device *dev)
1945 struct device_node *of_node = dev_of_node(dev);
1949 error = sysfs_create_link(&dev->kobj, of_node_kobj(of_node), "of_node");
1951 dev_warn(dev, "Error %d creating of_node link\n",error);
1952 /* An error here doesn't warrant bringing down the device */
1958 error = sysfs_create_link(&dev->kobj,
1959 &dev->class->p->subsys.kobj,
1964 if (dev->parent && device_is_not_partition(dev)) {
1965 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
1972 /* /sys/block has directories and does not need symlinks */
1973 if (sysfs_deprecated && dev->class == &block_class)
1977 /* link in the class directory pointing to the device */
1978 error = sysfs_create_link(&dev->class->p->subsys.kobj,
1979 &dev->kobj, dev_name(dev));
1986 sysfs_remove_link(&dev->kobj, "device");
1989 sysfs_remove_link(&dev->kobj, "subsystem");
1991 sysfs_remove_link(&dev->kobj, "of_node");
1995 static void device_remove_class_symlinks(struct device *dev)
1997 if (dev_of_node(dev))
1998 sysfs_remove_link(&dev->kobj, "of_node");
2003 if (dev->parent && device_is_not_partition(dev))
2004 sysfs_remove_link(&dev->kobj, "device");
2005 sysfs_remove_link(&dev->kobj, "subsystem");
2007 if (sysfs_deprecated && dev->class == &block_class)
2010 sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
2014 * dev_set_name - set a device name
2016 * @fmt: format string for the device's name
2018 int dev_set_name(struct device *dev, const char *fmt, ...)
2023 va_start(vargs, fmt);
2024 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
2028 EXPORT_SYMBOL_GPL(dev_set_name);
2031 * device_to_dev_kobj - select a /sys/dev/ directory for the device
2034 * By default we select char/ for new entries. Setting class->dev_obj
2035 * to NULL prevents an entry from being created. class->dev_kobj must
2036 * be set (or cleared) before any devices are registered to the class
2037 * otherwise device_create_sys_dev_entry() and
2038 * device_remove_sys_dev_entry() will disagree about the presence of
2041 static struct kobject *device_to_dev_kobj(struct device *dev)
2043 struct kobject *kobj;
2046 kobj = dev->class->dev_kobj;
2048 kobj = sysfs_dev_char_kobj;
2053 static int device_create_sys_dev_entry(struct device *dev)
2055 struct kobject *kobj = device_to_dev_kobj(dev);
2060 format_dev_t(devt_str, dev->devt);
2061 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
2067 static void device_remove_sys_dev_entry(struct device *dev)
2069 struct kobject *kobj = device_to_dev_kobj(dev);
2073 format_dev_t(devt_str, dev->devt);
2074 sysfs_remove_link(kobj, devt_str);
2078 static int device_private_init(struct device *dev)
2080 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
2083 dev->p->device = dev;
2084 klist_init(&dev->p->klist_children, klist_children_get,
2085 klist_children_put);
2086 INIT_LIST_HEAD(&dev->p->deferred_probe);
2091 * device_add - add device to device hierarchy.
2094 * This is part 2 of device_register(), though may be called
2095 * separately _iff_ device_initialize() has been called separately.
2097 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
2098 * to the global and sibling lists for the device, then
2099 * adds it to the other relevant subsystems of the driver model.
2101 * Do not call this routine or device_register() more than once for
2102 * any device structure. The driver model core is not designed to work
2103 * with devices that get unregistered and then spring back to life.
2104 * (Among other things, it's very hard to guarantee that all references
2105 * to the previous incarnation of @dev have been dropped.) Allocate
2106 * and register a fresh new struct device instead.
2108 * NOTE: _Never_ directly free @dev after calling this function, even
2109 * if it returned an error! Always use put_device() to give up your
2110 * reference instead.
2112 * Rule of thumb is: if device_add() succeeds, you should call
2113 * device_del() when you want to get rid of it. If device_add() has
2114 * *not* succeeded, use *only* put_device() to drop the reference
2117 int device_add(struct device *dev)
2119 struct device *parent;
2120 struct kobject *kobj;
2121 struct class_interface *class_intf;
2122 int error = -EINVAL;
2123 struct kobject *glue_dir = NULL;
2125 dev = get_device(dev);
2130 error = device_private_init(dev);
2136 * for statically allocated devices, which should all be converted
2137 * some day, we need to initialize the name. We prevent reading back
2138 * the name, and force the use of dev_name()
2140 if (dev->init_name) {
2141 dev_set_name(dev, "%s", dev->init_name);
2142 dev->init_name = NULL;
2145 /* subsystems can specify simple device enumeration */
2146 if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
2147 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
2149 if (!dev_name(dev)) {
2154 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
2156 parent = get_device(dev->parent);
2157 kobj = get_device_parent(dev, parent);
2159 error = PTR_ERR(kobj);
2163 dev->kobj.parent = kobj;
2165 /* use parent numa_node */
2166 if (parent && (dev_to_node(dev) == NUMA_NO_NODE))
2167 set_dev_node(dev, dev_to_node(parent));
2169 /* first, register with generic layer. */
2170 /* we require the name to be set before, and pass NULL */
2171 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
2173 glue_dir = get_glue_dir(dev);
2177 /* notify platform of device entry */
2178 error = device_platform_notify(dev, KOBJ_ADD);
2180 goto platform_error;
2182 error = device_create_file(dev, &dev_attr_uevent);
2186 error = device_add_class_symlinks(dev);
2189 error = device_add_attrs(dev);
2192 error = bus_add_device(dev);
2195 error = dpm_sysfs_add(dev);
2200 if (MAJOR(dev->devt)) {
2201 error = device_create_file(dev, &dev_attr_dev);
2205 error = device_create_sys_dev_entry(dev);
2209 devtmpfs_create_node(dev);
2212 /* Notify clients of device addition. This call must come
2213 * after dpm_sysfs_add() and before kobject_uevent().
2216 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2217 BUS_NOTIFY_ADD_DEVICE, dev);
2219 kobject_uevent(&dev->kobj, KOBJ_ADD);
2222 * Check if any of the other devices (consumers) have been waiting for
2223 * this device (supplier) to be added so that they can create a device
2226 * This needs to happen after device_pm_add() because device_link_add()
2227 * requires the supplier be registered before it's called.
2229 * But this also needs to happe before bus_probe_device() to make sure
2230 * waiting consumers can link to it before the driver is bound to the
2231 * device and the driver sync_state callback is called for this device.
2233 device_link_check_waiting_consumers();
2235 if (dev->bus && dev->bus->add_links && dev->bus->add_links(dev))
2236 device_link_wait_for_supplier(dev);
2238 bus_probe_device(dev);
2240 klist_add_tail(&dev->p->knode_parent,
2241 &parent->p->klist_children);
2244 mutex_lock(&dev->class->p->mutex);
2245 /* tie the class to the device */
2246 klist_add_tail(&dev->p->knode_class,
2247 &dev->class->p->klist_devices);
2249 /* notify any interfaces that the device is here */
2250 list_for_each_entry(class_intf,
2251 &dev->class->p->interfaces, node)
2252 if (class_intf->add_dev)
2253 class_intf->add_dev(dev, class_intf);
2254 mutex_unlock(&dev->class->p->mutex);
2260 if (MAJOR(dev->devt))
2261 device_remove_file(dev, &dev_attr_dev);
2263 device_pm_remove(dev);
2264 dpm_sysfs_remove(dev);
2266 bus_remove_device(dev);
2268 device_remove_attrs(dev);
2270 device_remove_class_symlinks(dev);
2272 device_remove_file(dev, &dev_attr_uevent);
2274 device_platform_notify(dev, KOBJ_REMOVE);
2276 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
2277 glue_dir = get_glue_dir(dev);
2278 kobject_del(&dev->kobj);
2280 cleanup_glue_dir(dev, glue_dir);
2288 EXPORT_SYMBOL_GPL(device_add);
2291 * device_register - register a device with the system.
2292 * @dev: pointer to the device structure
2294 * This happens in two clean steps - initialize the device
2295 * and add it to the system. The two steps can be called
2296 * separately, but this is the easiest and most common.
2297 * I.e. you should only call the two helpers separately if
2298 * have a clearly defined need to use and refcount the device
2299 * before it is added to the hierarchy.
2301 * For more information, see the kerneldoc for device_initialize()
2304 * NOTE: _Never_ directly free @dev after calling this function, even
2305 * if it returned an error! Always use put_device() to give up the
2306 * reference initialized in this function instead.
2308 int device_register(struct device *dev)
2310 device_initialize(dev);
2311 return device_add(dev);
2313 EXPORT_SYMBOL_GPL(device_register);
2316 * get_device - increment reference count for device.
2319 * This simply forwards the call to kobject_get(), though
2320 * we do take care to provide for the case that we get a NULL
2321 * pointer passed in.
2323 struct device *get_device(struct device *dev)
2325 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
2327 EXPORT_SYMBOL_GPL(get_device);
2330 * put_device - decrement reference count.
2331 * @dev: device in question.
2333 void put_device(struct device *dev)
2335 /* might_sleep(); */
2337 kobject_put(&dev->kobj);
2339 EXPORT_SYMBOL_GPL(put_device);
2342 * device_del - delete device from system.
2345 * This is the first part of the device unregistration
2346 * sequence. This removes the device from the lists we control
2347 * from here, has it removed from the other driver model
2348 * subsystems it was added to in device_add(), and removes it
2349 * from the kobject hierarchy.
2351 * NOTE: this should be called manually _iff_ device_add() was
2352 * also called manually.
2354 void device_del(struct device *dev)
2356 struct device *parent = dev->parent;
2357 struct kobject *glue_dir = NULL;
2358 struct class_interface *class_intf;
2361 * Hold the device lock and set the "dead" flag to guarantee that
2362 * the update behavior is consistent with the other bitfields near
2363 * it and that we cannot have an asynchronous probe routine trying
2364 * to run while we are tearing out the bus/class/sysfs from
2365 * underneath the device.
2368 dev->p->dead = true;
2371 /* Notify clients of device removal. This call must come
2372 * before dpm_sysfs_remove().
2375 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2376 BUS_NOTIFY_DEL_DEVICE, dev);
2378 dpm_sysfs_remove(dev);
2380 klist_del(&dev->p->knode_parent);
2381 if (MAJOR(dev->devt)) {
2382 devtmpfs_delete_node(dev);
2383 device_remove_sys_dev_entry(dev);
2384 device_remove_file(dev, &dev_attr_dev);
2387 device_remove_class_symlinks(dev);
2389 mutex_lock(&dev->class->p->mutex);
2390 /* notify any interfaces that the device is now gone */
2391 list_for_each_entry(class_intf,
2392 &dev->class->p->interfaces, node)
2393 if (class_intf->remove_dev)
2394 class_intf->remove_dev(dev, class_intf);
2395 /* remove the device from the class list */
2396 klist_del(&dev->p->knode_class);
2397 mutex_unlock(&dev->class->p->mutex);
2399 device_remove_file(dev, &dev_attr_uevent);
2400 device_remove_attrs(dev);
2401 bus_remove_device(dev);
2402 device_pm_remove(dev);
2403 driver_deferred_probe_del(dev);
2404 device_platform_notify(dev, KOBJ_REMOVE);
2405 device_remove_properties(dev);
2406 device_links_purge(dev);
2409 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2410 BUS_NOTIFY_REMOVED_DEVICE, dev);
2411 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
2412 glue_dir = get_glue_dir(dev);
2413 kobject_del(&dev->kobj);
2414 cleanup_glue_dir(dev, glue_dir);
2417 EXPORT_SYMBOL_GPL(device_del);
2420 * device_unregister - unregister device from system.
2421 * @dev: device going away.
2423 * We do this in two parts, like we do device_register(). First,
2424 * we remove it from all the subsystems with device_del(), then
2425 * we decrement the reference count via put_device(). If that
2426 * is the final reference count, the device will be cleaned up
2427 * via device_release() above. Otherwise, the structure will
2428 * stick around until the final reference to the device is dropped.
2430 void device_unregister(struct device *dev)
2432 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
2436 EXPORT_SYMBOL_GPL(device_unregister);
2438 static struct device *prev_device(struct klist_iter *i)
2440 struct klist_node *n = klist_prev(i);
2441 struct device *dev = NULL;
2442 struct device_private *p;
2445 p = to_device_private_parent(n);
2451 static struct device *next_device(struct klist_iter *i)
2453 struct klist_node *n = klist_next(i);
2454 struct device *dev = NULL;
2455 struct device_private *p;
2458 p = to_device_private_parent(n);
2465 * device_get_devnode - path of device node file
2467 * @mode: returned file access mode
2468 * @uid: returned file owner
2469 * @gid: returned file group
2470 * @tmp: possibly allocated string
2472 * Return the relative path of a possible device node.
2473 * Non-default names may need to allocate a memory to compose
2474 * a name. This memory is returned in tmp and needs to be
2475 * freed by the caller.
2477 const char *device_get_devnode(struct device *dev,
2478 umode_t *mode, kuid_t *uid, kgid_t *gid,
2485 /* the device type may provide a specific name */
2486 if (dev->type && dev->type->devnode)
2487 *tmp = dev->type->devnode(dev, mode, uid, gid);
2491 /* the class may provide a specific name */
2492 if (dev->class && dev->class->devnode)
2493 *tmp = dev->class->devnode(dev, mode);
2497 /* return name without allocation, tmp == NULL */
2498 if (strchr(dev_name(dev), '!') == NULL)
2499 return dev_name(dev);
2501 /* replace '!' in the name with '/' */
2502 s = kstrdup(dev_name(dev), GFP_KERNEL);
2505 strreplace(s, '!', '/');
2510 * device_for_each_child - device child iterator.
2511 * @parent: parent struct device.
2512 * @fn: function to be called for each device.
2513 * @data: data for the callback.
2515 * Iterate over @parent's child devices, and call @fn for each,
2518 * We check the return of @fn each time. If it returns anything
2519 * other than 0, we break out and return that value.
2521 int device_for_each_child(struct device *parent, void *data,
2522 int (*fn)(struct device *dev, void *data))
2524 struct klist_iter i;
2525 struct device *child;
2531 klist_iter_init(&parent->p->klist_children, &i);
2532 while (!error && (child = next_device(&i)))
2533 error = fn(child, data);
2534 klist_iter_exit(&i);
2537 EXPORT_SYMBOL_GPL(device_for_each_child);
2540 * device_for_each_child_reverse - device child iterator in reversed order.
2541 * @parent: parent struct device.
2542 * @fn: function to be called for each device.
2543 * @data: data for the callback.
2545 * Iterate over @parent's child devices, and call @fn for each,
2548 * We check the return of @fn each time. If it returns anything
2549 * other than 0, we break out and return that value.
2551 int device_for_each_child_reverse(struct device *parent, void *data,
2552 int (*fn)(struct device *dev, void *data))
2554 struct klist_iter i;
2555 struct device *child;
2561 klist_iter_init(&parent->p->klist_children, &i);
2562 while ((child = prev_device(&i)) && !error)
2563 error = fn(child, data);
2564 klist_iter_exit(&i);
2567 EXPORT_SYMBOL_GPL(device_for_each_child_reverse);
2570 * device_find_child - device iterator for locating a particular device.
2571 * @parent: parent struct device
2572 * @match: Callback function to check device
2573 * @data: Data to pass to match function
2575 * This is similar to the device_for_each_child() function above, but it
2576 * returns a reference to a device that is 'found' for later use, as
2577 * determined by the @match callback.
2579 * The callback should return 0 if the device doesn't match and non-zero
2580 * if it does. If the callback returns non-zero and a reference to the
2581 * current device can be obtained, this function will return to the caller
2582 * and not iterate over any more devices.
2584 * NOTE: you will need to drop the reference with put_device() after use.
2586 struct device *device_find_child(struct device *parent, void *data,
2587 int (*match)(struct device *dev, void *data))
2589 struct klist_iter i;
2590 struct device *child;
2595 klist_iter_init(&parent->p->klist_children, &i);
2596 while ((child = next_device(&i)))
2597 if (match(child, data) && get_device(child))
2599 klist_iter_exit(&i);
2602 EXPORT_SYMBOL_GPL(device_find_child);
2605 * device_find_child_by_name - device iterator for locating a child device.
2606 * @parent: parent struct device
2607 * @name: name of the child device
2609 * This is similar to the device_find_child() function above, but it
2610 * returns a reference to a device that has the name @name.
2612 * NOTE: you will need to drop the reference with put_device() after use.
2614 struct device *device_find_child_by_name(struct device *parent,
2617 struct klist_iter i;
2618 struct device *child;
2623 klist_iter_init(&parent->p->klist_children, &i);
2624 while ((child = next_device(&i)))
2625 if (!strcmp(dev_name(child), name) && get_device(child))
2627 klist_iter_exit(&i);
2630 EXPORT_SYMBOL_GPL(device_find_child_by_name);
2632 int __init devices_init(void)
2634 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
2637 dev_kobj = kobject_create_and_add("dev", NULL);
2640 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
2641 if (!sysfs_dev_block_kobj)
2642 goto block_kobj_err;
2643 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
2644 if (!sysfs_dev_char_kobj)
2650 kobject_put(sysfs_dev_block_kobj);
2652 kobject_put(dev_kobj);
2654 kset_unregister(devices_kset);
2658 static int device_check_offline(struct device *dev, void *not_used)
2662 ret = device_for_each_child(dev, NULL, device_check_offline);
2666 return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
2670 * device_offline - Prepare the device for hot-removal.
2671 * @dev: Device to be put offline.
2673 * Execute the device bus type's .offline() callback, if present, to prepare
2674 * the device for a subsequent hot-removal. If that succeeds, the device must
2675 * not be used until either it is removed or its bus type's .online() callback
2678 * Call under device_hotplug_lock.
2680 int device_offline(struct device *dev)
2684 if (dev->offline_disabled)
2687 ret = device_for_each_child(dev, NULL, device_check_offline);
2692 if (device_supports_offline(dev)) {
2696 ret = dev->bus->offline(dev);
2698 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
2699 dev->offline = true;
2709 * device_online - Put the device back online after successful device_offline().
2710 * @dev: Device to be put back online.
2712 * If device_offline() has been successfully executed for @dev, but the device
2713 * has not been removed subsequently, execute its bus type's .online() callback
2714 * to indicate that the device can be used again.
2716 * Call under device_hotplug_lock.
2718 int device_online(struct device *dev)
2723 if (device_supports_offline(dev)) {
2725 ret = dev->bus->online(dev);
2727 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
2728 dev->offline = false;
2739 struct root_device {
2741 struct module *owner;
2744 static inline struct root_device *to_root_device(struct device *d)
2746 return container_of(d, struct root_device, dev);
2749 static void root_device_release(struct device *dev)
2751 kfree(to_root_device(dev));
2755 * __root_device_register - allocate and register a root device
2756 * @name: root device name
2757 * @owner: owner module of the root device, usually THIS_MODULE
2759 * This function allocates a root device and registers it
2760 * using device_register(). In order to free the returned
2761 * device, use root_device_unregister().
2763 * Root devices are dummy devices which allow other devices
2764 * to be grouped under /sys/devices. Use this function to
2765 * allocate a root device and then use it as the parent of
2766 * any device which should appear under /sys/devices/{name}
2768 * The /sys/devices/{name} directory will also contain a
2769 * 'module' symlink which points to the @owner directory
2772 * Returns &struct device pointer on success, or ERR_PTR() on error.
2774 * Note: You probably want to use root_device_register().
2776 struct device *__root_device_register(const char *name, struct module *owner)
2778 struct root_device *root;
2781 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
2783 return ERR_PTR(err);
2785 err = dev_set_name(&root->dev, "%s", name);
2788 return ERR_PTR(err);
2791 root->dev.release = root_device_release;
2793 err = device_register(&root->dev);
2795 put_device(&root->dev);
2796 return ERR_PTR(err);
2799 #ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
2801 struct module_kobject *mk = &owner->mkobj;
2803 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
2805 device_unregister(&root->dev);
2806 return ERR_PTR(err);
2808 root->owner = owner;
2814 EXPORT_SYMBOL_GPL(__root_device_register);
2817 * root_device_unregister - unregister and free a root device
2818 * @dev: device going away
2820 * This function unregisters and cleans up a device that was created by
2821 * root_device_register().
2823 void root_device_unregister(struct device *dev)
2825 struct root_device *root = to_root_device(dev);
2828 sysfs_remove_link(&root->dev.kobj, "module");
2830 device_unregister(dev);
2832 EXPORT_SYMBOL_GPL(root_device_unregister);
2835 static void device_create_release(struct device *dev)
2837 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
2841 static __printf(6, 0) struct device *
2842 device_create_groups_vargs(struct class *class, struct device *parent,
2843 dev_t devt, void *drvdata,
2844 const struct attribute_group **groups,
2845 const char *fmt, va_list args)
2847 struct device *dev = NULL;
2848 int retval = -ENODEV;
2850 if (class == NULL || IS_ERR(class))
2853 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2859 device_initialize(dev);
2862 dev->parent = parent;
2863 dev->groups = groups;
2864 dev->release = device_create_release;
2865 dev_set_drvdata(dev, drvdata);
2867 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
2871 retval = device_add(dev);
2879 return ERR_PTR(retval);
2883 * device_create_vargs - creates a device and registers it with sysfs
2884 * @class: pointer to the struct class that this device should be registered to
2885 * @parent: pointer to the parent struct device of this new device, if any
2886 * @devt: the dev_t for the char device to be added
2887 * @drvdata: the data to be added to the device for callbacks
2888 * @fmt: string for the device's name
2889 * @args: va_list for the device's name
2891 * This function can be used by char device classes. A struct device
2892 * will be created in sysfs, registered to the specified class.
2894 * A "dev" file will be created, showing the dev_t for the device, if
2895 * the dev_t is not 0,0.
2896 * If a pointer to a parent struct device is passed in, the newly created
2897 * struct device will be a child of that device in sysfs.
2898 * The pointer to the struct device will be returned from the call.
2899 * Any further sysfs files that might be required can be created using this
2902 * Returns &struct device pointer on success, or ERR_PTR() on error.
2904 * Note: the struct class passed to this function must have previously
2905 * been created with a call to class_create().
2907 struct device *device_create_vargs(struct class *class, struct device *parent,
2908 dev_t devt, void *drvdata, const char *fmt,
2911 return device_create_groups_vargs(class, parent, devt, drvdata, NULL,
2914 EXPORT_SYMBOL_GPL(device_create_vargs);
2917 * device_create - creates a device and registers it with sysfs
2918 * @class: pointer to the struct class that this device should be registered to
2919 * @parent: pointer to the parent struct device of this new device, if any
2920 * @devt: the dev_t for the char device to be added
2921 * @drvdata: the data to be added to the device for callbacks
2922 * @fmt: string for the device's name
2924 * This function can be used by char device classes. A struct device
2925 * will be created in sysfs, registered to the specified class.
2927 * A "dev" file will be created, showing the dev_t for the device, if
2928 * the dev_t is not 0,0.
2929 * If a pointer to a parent struct device is passed in, the newly created
2930 * struct device will be a child of that device in sysfs.
2931 * The pointer to the struct device will be returned from the call.
2932 * Any further sysfs files that might be required can be created using this
2935 * Returns &struct device pointer on success, or ERR_PTR() on error.
2937 * Note: the struct class passed to this function must have previously
2938 * been created with a call to class_create().
2940 struct device *device_create(struct class *class, struct device *parent,
2941 dev_t devt, void *drvdata, const char *fmt, ...)
2946 va_start(vargs, fmt);
2947 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
2951 EXPORT_SYMBOL_GPL(device_create);
2954 * device_create_with_groups - creates a device and registers it with sysfs
2955 * @class: pointer to the struct class that this device should be registered to
2956 * @parent: pointer to the parent struct device of this new device, if any
2957 * @devt: the dev_t for the char device to be added
2958 * @drvdata: the data to be added to the device for callbacks
2959 * @groups: NULL-terminated list of attribute groups to be created
2960 * @fmt: string for the device's name
2962 * This function can be used by char device classes. A struct device
2963 * will be created in sysfs, registered to the specified class.
2964 * Additional attributes specified in the groups parameter will also
2965 * be created automatically.
2967 * A "dev" file will be created, showing the dev_t for the device, if
2968 * the dev_t is not 0,0.
2969 * If a pointer to a parent struct device is passed in, the newly created
2970 * struct device will be a child of that device in sysfs.
2971 * The pointer to the struct device will be returned from the call.
2972 * Any further sysfs files that might be required can be created using this
2975 * Returns &struct device pointer on success, or ERR_PTR() on error.
2977 * Note: the struct class passed to this function must have previously
2978 * been created with a call to class_create().
2980 struct device *device_create_with_groups(struct class *class,
2981 struct device *parent, dev_t devt,
2983 const struct attribute_group **groups,
2984 const char *fmt, ...)
2989 va_start(vargs, fmt);
2990 dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
2995 EXPORT_SYMBOL_GPL(device_create_with_groups);
2998 * device_destroy - removes a device that was created with device_create()
2999 * @class: pointer to the struct class that this device was registered with
3000 * @devt: the dev_t of the device that was previously registered
3002 * This call unregisters and cleans up a device that was created with a
3003 * call to device_create().
3005 void device_destroy(struct class *class, dev_t devt)
3009 dev = class_find_device_by_devt(class, devt);
3012 device_unregister(dev);
3015 EXPORT_SYMBOL_GPL(device_destroy);
3018 * device_rename - renames a device
3019 * @dev: the pointer to the struct device to be renamed
3020 * @new_name: the new name of the device
3022 * It is the responsibility of the caller to provide mutual
3023 * exclusion between two different calls of device_rename
3024 * on the same device to ensure that new_name is valid and
3025 * won't conflict with other devices.
3027 * Note: Don't call this function. Currently, the networking layer calls this
3028 * function, but that will change. The following text from Kay Sievers offers
3031 * Renaming devices is racy at many levels, symlinks and other stuff are not
3032 * replaced atomically, and you get a "move" uevent, but it's not easy to
3033 * connect the event to the old and new device. Device nodes are not renamed at
3034 * all, there isn't even support for that in the kernel now.
3036 * In the meantime, during renaming, your target name might be taken by another
3037 * driver, creating conflicts. Or the old name is taken directly after you
3038 * renamed it -- then you get events for the same DEVPATH, before you even see
3039 * the "move" event. It's just a mess, and nothing new should ever rely on
3040 * kernel device renaming. Besides that, it's not even implemented now for
3041 * other things than (driver-core wise very simple) network devices.
3043 * We are currently about to change network renaming in udev to completely
3044 * disallow renaming of devices in the same namespace as the kernel uses,
3045 * because we can't solve the problems properly, that arise with swapping names
3046 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
3047 * be allowed to some other name than eth[0-9]*, for the aforementioned
3050 * Make up a "real" name in the driver before you register anything, or add
3051 * some other attributes for userspace to find the device, or use udev to add
3052 * symlinks -- but never rename kernel devices later, it's a complete mess. We
3053 * don't even want to get into that and try to implement the missing pieces in
3054 * the core. We really have other pieces to fix in the driver core mess. :)
3056 int device_rename(struct device *dev, const char *new_name)
3058 struct kobject *kobj = &dev->kobj;
3059 char *old_device_name = NULL;
3062 dev = get_device(dev);
3066 dev_dbg(dev, "renaming to %s\n", new_name);
3068 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
3069 if (!old_device_name) {
3075 error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj,
3076 kobj, old_device_name,
3077 new_name, kobject_namespace(kobj));
3082 error = kobject_rename(kobj, new_name);
3089 kfree(old_device_name);
3093 EXPORT_SYMBOL_GPL(device_rename);
3095 static int device_move_class_links(struct device *dev,
3096 struct device *old_parent,
3097 struct device *new_parent)
3102 sysfs_remove_link(&dev->kobj, "device");
3104 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
3110 * device_move - moves a device to a new parent
3111 * @dev: the pointer to the struct device to be moved
3112 * @new_parent: the new parent of the device (can be NULL)
3113 * @dpm_order: how to reorder the dpm_list
3115 int device_move(struct device *dev, struct device *new_parent,
3116 enum dpm_order dpm_order)
3119 struct device *old_parent;
3120 struct kobject *new_parent_kobj;
3122 dev = get_device(dev);
3127 new_parent = get_device(new_parent);
3128 new_parent_kobj = get_device_parent(dev, new_parent);
3129 if (IS_ERR(new_parent_kobj)) {
3130 error = PTR_ERR(new_parent_kobj);
3131 put_device(new_parent);
3135 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
3136 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
3137 error = kobject_move(&dev->kobj, new_parent_kobj);
3139 cleanup_glue_dir(dev, new_parent_kobj);
3140 put_device(new_parent);
3143 old_parent = dev->parent;
3144 dev->parent = new_parent;
3146 klist_remove(&dev->p->knode_parent);
3148 klist_add_tail(&dev->p->knode_parent,
3149 &new_parent->p->klist_children);
3150 set_dev_node(dev, dev_to_node(new_parent));
3154 error = device_move_class_links(dev, old_parent, new_parent);
3156 /* We ignore errors on cleanup since we're hosed anyway... */
3157 device_move_class_links(dev, new_parent, old_parent);
3158 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
3160 klist_remove(&dev->p->knode_parent);
3161 dev->parent = old_parent;
3163 klist_add_tail(&dev->p->knode_parent,
3164 &old_parent->p->klist_children);
3165 set_dev_node(dev, dev_to_node(old_parent));
3168 cleanup_glue_dir(dev, new_parent_kobj);
3169 put_device(new_parent);
3173 switch (dpm_order) {
3174 case DPM_ORDER_NONE:
3176 case DPM_ORDER_DEV_AFTER_PARENT:
3177 device_pm_move_after(dev, new_parent);
3178 devices_kset_move_after(dev, new_parent);
3180 case DPM_ORDER_PARENT_BEFORE_DEV:
3181 device_pm_move_before(new_parent, dev);
3182 devices_kset_move_before(new_parent, dev);
3184 case DPM_ORDER_DEV_LAST:
3185 device_pm_move_last(dev);
3186 devices_kset_move_last(dev);
3190 put_device(old_parent);
3196 EXPORT_SYMBOL_GPL(device_move);
3199 * device_shutdown - call ->shutdown() on each device to shutdown.
3201 void device_shutdown(void)
3203 struct device *dev, *parent;
3205 wait_for_device_probe();
3206 device_block_probing();
3208 spin_lock(&devices_kset->list_lock);
3210 * Walk the devices list backward, shutting down each in turn.
3211 * Beware that device unplug events may also start pulling
3212 * devices offline, even as the system is shutting down.
3214 while (!list_empty(&devices_kset->list)) {
3215 dev = list_entry(devices_kset->list.prev, struct device,
3219 * hold reference count of device's parent to
3220 * prevent it from being freed because parent's
3221 * lock is to be held
3223 parent = get_device(dev->parent);
3226 * Make sure the device is off the kset list, in the
3227 * event that dev->*->shutdown() doesn't remove it.
3229 list_del_init(&dev->kobj.entry);
3230 spin_unlock(&devices_kset->list_lock);
3232 /* hold lock to avoid race with probe/release */
3234 device_lock(parent);
3237 /* Don't allow any more runtime suspends */
3238 pm_runtime_get_noresume(dev);
3239 pm_runtime_barrier(dev);
3241 if (dev->class && dev->class->shutdown_pre) {
3243 dev_info(dev, "shutdown_pre\n");
3244 dev->class->shutdown_pre(dev);
3246 if (dev->bus && dev->bus->shutdown) {
3248 dev_info(dev, "shutdown\n");
3249 dev->bus->shutdown(dev);
3250 } else if (dev->driver && dev->driver->shutdown) {
3252 dev_info(dev, "shutdown\n");
3253 dev->driver->shutdown(dev);
3258 device_unlock(parent);
3263 spin_lock(&devices_kset->list_lock);
3265 spin_unlock(&devices_kset->list_lock);
3269 * Device logging functions
3272 #ifdef CONFIG_PRINTK
3274 create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
3280 subsys = dev->class->name;
3282 subsys = dev->bus->name;
3286 pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys);
3291 * Add device identifier DEVICE=:
3295 * +sound:card0 subsystem:devname
3297 if (MAJOR(dev->devt)) {
3300 if (strcmp(subsys, "block") == 0)
3305 pos += snprintf(hdr + pos, hdrlen - pos,
3307 c, MAJOR(dev->devt), MINOR(dev->devt));
3308 } else if (strcmp(subsys, "net") == 0) {
3309 struct net_device *net = to_net_dev(dev);
3312 pos += snprintf(hdr + pos, hdrlen - pos,
3313 "DEVICE=n%u", net->ifindex);
3316 pos += snprintf(hdr + pos, hdrlen - pos,
3317 "DEVICE=+%s:%s", subsys, dev_name(dev));
3326 dev_WARN(dev, "device/subsystem name too long");
3330 int dev_vprintk_emit(int level, const struct device *dev,
3331 const char *fmt, va_list args)
3336 hdrlen = create_syslog_header(dev, hdr, sizeof(hdr));
3338 return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args);
3340 EXPORT_SYMBOL(dev_vprintk_emit);
3342 int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
3347 va_start(args, fmt);
3349 r = dev_vprintk_emit(level, dev, fmt, args);
3355 EXPORT_SYMBOL(dev_printk_emit);
3357 static void __dev_printk(const char *level, const struct device *dev,
3358 struct va_format *vaf)
3361 dev_printk_emit(level[1] - '0', dev, "%s %s: %pV",
3362 dev_driver_string(dev), dev_name(dev), vaf);
3364 printk("%s(NULL device *): %pV", level, vaf);
3367 void dev_printk(const char *level, const struct device *dev,
3368 const char *fmt, ...)
3370 struct va_format vaf;
3373 va_start(args, fmt);
3378 __dev_printk(level, dev, &vaf);
3382 EXPORT_SYMBOL(dev_printk);
3384 #define define_dev_printk_level(func, kern_level) \
3385 void func(const struct device *dev, const char *fmt, ...) \
3387 struct va_format vaf; \
3390 va_start(args, fmt); \
3395 __dev_printk(kern_level, dev, &vaf); \
3399 EXPORT_SYMBOL(func);
3401 define_dev_printk_level(_dev_emerg, KERN_EMERG);
3402 define_dev_printk_level(_dev_alert, KERN_ALERT);
3403 define_dev_printk_level(_dev_crit, KERN_CRIT);
3404 define_dev_printk_level(_dev_err, KERN_ERR);
3405 define_dev_printk_level(_dev_warn, KERN_WARNING);
3406 define_dev_printk_level(_dev_notice, KERN_NOTICE);
3407 define_dev_printk_level(_dev_info, KERN_INFO);
3411 static inline bool fwnode_is_primary(struct fwnode_handle *fwnode)
3413 return fwnode && !IS_ERR(fwnode->secondary);
3417 * set_primary_fwnode - Change the primary firmware node of a given device.
3418 * @dev: Device to handle.
3419 * @fwnode: New primary firmware node of the device.
3421 * Set the device's firmware node pointer to @fwnode, but if a secondary
3422 * firmware node of the device is present, preserve it.
3424 void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
3427 struct fwnode_handle *fn = dev->fwnode;
3429 if (fwnode_is_primary(fn))
3433 WARN_ON(fwnode->secondary);
3434 fwnode->secondary = fn;
3436 dev->fwnode = fwnode;
3438 dev->fwnode = fwnode_is_primary(dev->fwnode) ?
3439 dev->fwnode->secondary : NULL;
3442 EXPORT_SYMBOL_GPL(set_primary_fwnode);
3445 * set_secondary_fwnode - Change the secondary firmware node of a given device.
3446 * @dev: Device to handle.
3447 * @fwnode: New secondary firmware node of the device.
3449 * If a primary firmware node of the device is present, set its secondary
3450 * pointer to @fwnode. Otherwise, set the device's firmware node pointer to
3453 void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
3456 fwnode->secondary = ERR_PTR(-ENODEV);
3458 if (fwnode_is_primary(dev->fwnode))
3459 dev->fwnode->secondary = fwnode;
3461 dev->fwnode = fwnode;
3465 * device_set_of_node_from_dev - reuse device-tree node of another device
3466 * @dev: device whose device-tree node is being set
3467 * @dev2: device whose device-tree node is being reused
3469 * Takes another reference to the new device-tree node after first dropping
3470 * any reference held to the old node.
3472 void device_set_of_node_from_dev(struct device *dev, const struct device *dev2)
3474 of_node_put(dev->of_node);
3475 dev->of_node = of_node_get(dev2->of_node);
3476 dev->of_node_reused = true;
3478 EXPORT_SYMBOL_GPL(device_set_of_node_from_dev);
3480 int device_match_name(struct device *dev, const void *name)
3482 return sysfs_streq(dev_name(dev), name);
3484 EXPORT_SYMBOL_GPL(device_match_name);
3486 int device_match_of_node(struct device *dev, const void *np)
3488 return dev->of_node == np;
3490 EXPORT_SYMBOL_GPL(device_match_of_node);
3492 int device_match_fwnode(struct device *dev, const void *fwnode)
3494 return dev_fwnode(dev) == fwnode;
3496 EXPORT_SYMBOL_GPL(device_match_fwnode);
3498 int device_match_devt(struct device *dev, const void *pdevt)
3500 return dev->devt == *(dev_t *)pdevt;
3502 EXPORT_SYMBOL_GPL(device_match_devt);
3504 int device_match_acpi_dev(struct device *dev, const void *adev)
3506 return ACPI_COMPANION(dev) == adev;
3508 EXPORT_SYMBOL(device_match_acpi_dev);
3510 int device_match_any(struct device *dev, const void *unused)
3514 EXPORT_SYMBOL_GPL(device_match_any);