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/cpufreq.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. */
49 static DEFINE_MUTEX(device_links_lock);
50 DEFINE_STATIC_SRCU(device_links_srcu);
52 static inline void device_links_write_lock(void)
54 mutex_lock(&device_links_lock);
57 static inline void device_links_write_unlock(void)
59 mutex_unlock(&device_links_lock);
62 int device_links_read_lock(void)
64 return srcu_read_lock(&device_links_srcu);
67 void device_links_read_unlock(int idx)
69 srcu_read_unlock(&device_links_srcu, idx);
71 #else /* !CONFIG_SRCU */
72 static DECLARE_RWSEM(device_links_lock);
74 static inline void device_links_write_lock(void)
76 down_write(&device_links_lock);
79 static inline void device_links_write_unlock(void)
81 up_write(&device_links_lock);
84 int device_links_read_lock(void)
86 down_read(&device_links_lock);
90 void device_links_read_unlock(int not_used)
92 up_read(&device_links_lock);
94 #endif /* !CONFIG_SRCU */
96 static bool device_is_ancestor(struct device *dev, struct device *target)
98 while (target->parent) {
99 target = target->parent;
107 * device_is_dependent - Check if one device depends on another one
108 * @dev: Device to check dependencies for.
109 * @target: Device to check against.
111 * Check if @target depends on @dev or any device dependent on it (its child or
112 * its consumer etc). Return 1 if that is the case or 0 otherwise.
114 static int device_is_dependent(struct device *dev, void *target)
116 struct device_link *link;
120 * The "ancestors" check is needed to catch the case when the target
121 * device has not been completely initialized yet and it is still
122 * missing from the list of children of its parent device.
124 if (dev == target || device_is_ancestor(dev, target))
127 ret = device_for_each_child(dev, target, device_is_dependent);
131 list_for_each_entry(link, &dev->links.consumers, s_node) {
132 if (link->consumer == target)
135 ret = device_is_dependent(link->consumer, target);
142 static void device_link_init_status(struct device_link *link,
143 struct device *consumer,
144 struct device *supplier)
146 switch (supplier->links.status) {
148 switch (consumer->links.status) {
151 * A consumer driver can create a link to a supplier
152 * that has not completed its probing yet as long as it
153 * knows that the supplier is already functional (for
154 * example, it has just acquired some resources from the
157 link->status = DL_STATE_CONSUMER_PROBE;
160 link->status = DL_STATE_DORMANT;
164 case DL_DEV_DRIVER_BOUND:
165 switch (consumer->links.status) {
167 link->status = DL_STATE_CONSUMER_PROBE;
169 case DL_DEV_DRIVER_BOUND:
170 link->status = DL_STATE_ACTIVE;
173 link->status = DL_STATE_AVAILABLE;
177 case DL_DEV_UNBINDING:
178 link->status = DL_STATE_SUPPLIER_UNBIND;
181 link->status = DL_STATE_DORMANT;
186 static int device_reorder_to_tail(struct device *dev, void *not_used)
188 struct device_link *link;
191 * Devices that have not been registered yet will be put to the ends
192 * of the lists during the registration, so skip them here.
194 if (device_is_registered(dev))
195 devices_kset_move_last(dev);
197 if (device_pm_initialized(dev))
198 device_pm_move_last(dev);
200 device_for_each_child(dev, NULL, device_reorder_to_tail);
201 list_for_each_entry(link, &dev->links.consumers, s_node)
202 device_reorder_to_tail(link->consumer, NULL);
208 * device_pm_move_to_tail - Move set of devices to the end of device lists
209 * @dev: Device to move
211 * This is a device_reorder_to_tail() wrapper taking the requisite locks.
213 * It moves the @dev along with all of its children and all of its consumers
214 * to the ends of the device_kset and dpm_list, recursively.
216 void device_pm_move_to_tail(struct device *dev)
220 idx = device_links_read_lock();
222 device_reorder_to_tail(dev, NULL);
224 device_links_read_unlock(idx);
227 #define DL_MANAGED_LINK_FLAGS (DL_FLAG_AUTOREMOVE_CONSUMER | \
228 DL_FLAG_AUTOREMOVE_SUPPLIER | \
229 DL_FLAG_AUTOPROBE_CONSUMER)
231 #define DL_ADD_VALID_FLAGS (DL_MANAGED_LINK_FLAGS | DL_FLAG_STATELESS | \
232 DL_FLAG_PM_RUNTIME | DL_FLAG_RPM_ACTIVE)
235 * device_link_add - Create a link between two devices.
236 * @consumer: Consumer end of the link.
237 * @supplier: Supplier end of the link.
238 * @flags: Link flags.
240 * The caller is responsible for the proper synchronization of the link creation
241 * with runtime PM. First, setting the DL_FLAG_PM_RUNTIME flag will cause the
242 * runtime PM framework to take the link into account. Second, if the
243 * DL_FLAG_RPM_ACTIVE flag is set in addition to it, the supplier devices will
244 * be forced into the active metastate and reference-counted upon the creation
245 * of the link. If DL_FLAG_PM_RUNTIME is not set, DL_FLAG_RPM_ACTIVE will be
248 * If DL_FLAG_STATELESS is set in @flags, the caller of this function is
249 * expected to release the link returned by it directly with the help of either
250 * device_link_del() or device_link_remove().
252 * If that flag is not set, however, the caller of this function is handing the
253 * management of the link over to the driver core entirely and its return value
254 * can only be used to check whether or not the link is present. In that case,
255 * the DL_FLAG_AUTOREMOVE_CONSUMER and DL_FLAG_AUTOREMOVE_SUPPLIER device link
256 * flags can be used to indicate to the driver core when the link can be safely
257 * deleted. Namely, setting one of them in @flags indicates to the driver core
258 * that the link is not going to be used (by the given caller of this function)
259 * after unbinding the consumer or supplier driver, respectively, from its
260 * device, so the link can be deleted at that point. If none of them is set,
261 * the link will be maintained until one of the devices pointed to by it (either
262 * the consumer or the supplier) is unregistered.
264 * Also, if DL_FLAG_STATELESS, DL_FLAG_AUTOREMOVE_CONSUMER and
265 * DL_FLAG_AUTOREMOVE_SUPPLIER are not set in @flags (that is, a persistent
266 * managed device link is being added), the DL_FLAG_AUTOPROBE_CONSUMER flag can
267 * be used to request the driver core to automaticall probe for a consmer
268 * driver after successfully binding a driver to the supplier device.
270 * The combination of DL_FLAG_STATELESS and one of DL_FLAG_AUTOREMOVE_CONSUMER,
271 * DL_FLAG_AUTOREMOVE_SUPPLIER, or DL_FLAG_AUTOPROBE_CONSUMER set in @flags at
272 * the same time is invalid and will cause NULL to be returned upfront.
273 * However, if a device link between the given @consumer and @supplier pair
274 * exists already when this function is called for them, the existing link will
275 * be returned regardless of its current type and status (the link's flags may
276 * be modified then). The caller of this function is then expected to treat
277 * the link as though it has just been created, so (in particular) if
278 * DL_FLAG_STATELESS was passed in @flags, the link needs to be released
279 * explicitly when not needed any more (as stated above).
281 * A side effect of the link creation is re-ordering of dpm_list and the
282 * devices_kset list by moving the consumer device and all devices depending
283 * on it to the ends of these lists (that does not happen to devices that have
284 * not been registered when this function is called).
286 * The supplier device is required to be registered when this function is called
287 * and NULL will be returned if that is not the case. The consumer device need
288 * not be registered, however.
290 struct device_link *device_link_add(struct device *consumer,
291 struct device *supplier, u32 flags)
293 struct device_link *link;
295 if (!consumer || !supplier || flags & ~DL_ADD_VALID_FLAGS ||
296 (flags & DL_FLAG_STATELESS && flags & DL_MANAGED_LINK_FLAGS) ||
297 (flags & DL_FLAG_AUTOPROBE_CONSUMER &&
298 flags & (DL_FLAG_AUTOREMOVE_CONSUMER |
299 DL_FLAG_AUTOREMOVE_SUPPLIER)))
302 if (flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) {
303 if (pm_runtime_get_sync(supplier) < 0) {
304 pm_runtime_put_noidle(supplier);
309 if (!(flags & DL_FLAG_STATELESS))
310 flags |= DL_FLAG_MANAGED;
312 device_links_write_lock();
316 * If the supplier has not been fully registered yet or there is a
317 * reverse dependency between the consumer and the supplier already in
318 * the graph, return NULL.
320 if (!device_pm_initialized(supplier)
321 || device_is_dependent(consumer, supplier)) {
327 * DL_FLAG_AUTOREMOVE_SUPPLIER indicates that the link will be needed
328 * longer than for DL_FLAG_AUTOREMOVE_CONSUMER and setting them both
329 * together doesn't make sense, so prefer DL_FLAG_AUTOREMOVE_SUPPLIER.
331 if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
332 flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER;
334 list_for_each_entry(link, &supplier->links.consumers, s_node) {
335 if (link->consumer != consumer)
338 if (flags & DL_FLAG_PM_RUNTIME) {
339 if (!(link->flags & DL_FLAG_PM_RUNTIME)) {
340 pm_runtime_new_link(consumer);
341 link->flags |= DL_FLAG_PM_RUNTIME;
343 if (flags & DL_FLAG_RPM_ACTIVE)
344 refcount_inc(&link->rpm_active);
347 if (flags & DL_FLAG_STATELESS) {
348 link->flags |= DL_FLAG_STATELESS;
349 kref_get(&link->kref);
354 * If the life time of the link following from the new flags is
355 * longer than indicated by the flags of the existing link,
356 * update the existing link to stay around longer.
358 if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER) {
359 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER) {
360 link->flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER;
361 link->flags |= DL_FLAG_AUTOREMOVE_SUPPLIER;
363 } else if (!(flags & DL_FLAG_AUTOREMOVE_CONSUMER)) {
364 link->flags &= ~(DL_FLAG_AUTOREMOVE_CONSUMER |
365 DL_FLAG_AUTOREMOVE_SUPPLIER);
367 if (!(link->flags & DL_FLAG_MANAGED)) {
368 kref_get(&link->kref);
369 link->flags |= DL_FLAG_MANAGED;
370 device_link_init_status(link, consumer, supplier);
375 link = kzalloc(sizeof(*link), GFP_KERNEL);
379 refcount_set(&link->rpm_active, 1);
381 if (flags & DL_FLAG_PM_RUNTIME) {
382 if (flags & DL_FLAG_RPM_ACTIVE)
383 refcount_inc(&link->rpm_active);
385 pm_runtime_new_link(consumer);
388 get_device(supplier);
389 link->supplier = supplier;
390 INIT_LIST_HEAD(&link->s_node);
391 get_device(consumer);
392 link->consumer = consumer;
393 INIT_LIST_HEAD(&link->c_node);
395 kref_init(&link->kref);
397 /* Determine the initial link state. */
398 if (flags & DL_FLAG_STATELESS)
399 link->status = DL_STATE_NONE;
401 device_link_init_status(link, consumer, supplier);
404 * Some callers expect the link creation during consumer driver probe to
405 * resume the supplier even without DL_FLAG_RPM_ACTIVE.
407 if (link->status == DL_STATE_CONSUMER_PROBE &&
408 flags & DL_FLAG_PM_RUNTIME)
409 pm_runtime_resume(supplier);
412 * Move the consumer and all of the devices depending on it to the end
413 * of dpm_list and the devices_kset list.
415 * It is necessary to hold dpm_list locked throughout all that or else
416 * we may end up suspending with a wrong ordering of it.
418 device_reorder_to_tail(consumer, NULL);
420 list_add_tail_rcu(&link->s_node, &supplier->links.consumers);
421 list_add_tail_rcu(&link->c_node, &consumer->links.suppliers);
423 dev_info(consumer, "Linked as a consumer to %s\n", dev_name(supplier));
427 device_links_write_unlock();
429 if ((flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) && !link)
430 pm_runtime_put(supplier);
434 EXPORT_SYMBOL_GPL(device_link_add);
436 static void device_link_free(struct device_link *link)
438 while (refcount_dec_not_one(&link->rpm_active))
439 pm_runtime_put(link->supplier);
441 put_device(link->consumer);
442 put_device(link->supplier);
447 static void __device_link_free_srcu(struct rcu_head *rhead)
449 device_link_free(container_of(rhead, struct device_link, rcu_head));
452 static void __device_link_del(struct kref *kref)
454 struct device_link *link = container_of(kref, struct device_link, kref);
456 dev_info(link->consumer, "Dropping the link to %s\n",
457 dev_name(link->supplier));
459 if (link->flags & DL_FLAG_PM_RUNTIME)
460 pm_runtime_drop_link(link->consumer);
462 list_del_rcu(&link->s_node);
463 list_del_rcu(&link->c_node);
464 call_srcu(&device_links_srcu, &link->rcu_head, __device_link_free_srcu);
466 #else /* !CONFIG_SRCU */
467 static void __device_link_del(struct kref *kref)
469 struct device_link *link = container_of(kref, struct device_link, kref);
471 dev_info(link->consumer, "Dropping the link to %s\n",
472 dev_name(link->supplier));
474 if (link->flags & DL_FLAG_PM_RUNTIME)
475 pm_runtime_drop_link(link->consumer);
477 list_del(&link->s_node);
478 list_del(&link->c_node);
479 device_link_free(link);
481 #endif /* !CONFIG_SRCU */
483 static void device_link_put_kref(struct device_link *link)
485 if (link->flags & DL_FLAG_STATELESS)
486 kref_put(&link->kref, __device_link_del);
488 WARN(1, "Unable to drop a managed device link reference\n");
492 * device_link_del - Delete a stateless link between two devices.
493 * @link: Device link to delete.
495 * The caller must ensure proper synchronization of this function with runtime
496 * PM. If the link was added multiple times, it needs to be deleted as often.
497 * Care is required for hotplugged devices: Their links are purged on removal
498 * and calling device_link_del() is then no longer allowed.
500 void device_link_del(struct device_link *link)
502 device_links_write_lock();
504 device_link_put_kref(link);
506 device_links_write_unlock();
508 EXPORT_SYMBOL_GPL(device_link_del);
511 * device_link_remove - Delete a stateless link between two devices.
512 * @consumer: Consumer end of the link.
513 * @supplier: Supplier end of the link.
515 * The caller must ensure proper synchronization of this function with runtime
518 void device_link_remove(void *consumer, struct device *supplier)
520 struct device_link *link;
522 if (WARN_ON(consumer == supplier))
525 device_links_write_lock();
528 list_for_each_entry(link, &supplier->links.consumers, s_node) {
529 if (link->consumer == consumer) {
530 device_link_put_kref(link);
536 device_links_write_unlock();
538 EXPORT_SYMBOL_GPL(device_link_remove);
540 static void device_links_missing_supplier(struct device *dev)
542 struct device_link *link;
544 list_for_each_entry(link, &dev->links.suppliers, c_node)
545 if (link->status == DL_STATE_CONSUMER_PROBE)
546 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
550 * device_links_check_suppliers - Check presence of supplier drivers.
551 * @dev: Consumer device.
553 * Check links from this device to any suppliers. Walk the list of the device's
554 * links to suppliers and see if all of them are available. If not, simply
555 * return -EPROBE_DEFER.
557 * We need to guarantee that the supplier will not go away after the check has
558 * been positive here. It only can go away in __device_release_driver() and
559 * that function checks the device's links to consumers. This means we need to
560 * mark the link as "consumer probe in progress" to make the supplier removal
561 * wait for us to complete (or bad things may happen).
563 * Links without the DL_FLAG_MANAGED flag set are ignored.
565 int device_links_check_suppliers(struct device *dev)
567 struct device_link *link;
570 device_links_write_lock();
572 list_for_each_entry(link, &dev->links.suppliers, c_node) {
573 if (!(link->flags & DL_FLAG_MANAGED))
576 if (link->status != DL_STATE_AVAILABLE) {
577 device_links_missing_supplier(dev);
581 WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE);
583 dev->links.status = DL_DEV_PROBING;
585 device_links_write_unlock();
590 * device_links_driver_bound - Update device links after probing its driver.
591 * @dev: Device to update the links for.
593 * The probe has been successful, so update links from this device to any
594 * consumers by changing their status to "available".
596 * Also change the status of @dev's links to suppliers to "active".
598 * Links without the DL_FLAG_MANAGED flag set are ignored.
600 void device_links_driver_bound(struct device *dev)
602 struct device_link *link;
604 device_links_write_lock();
606 list_for_each_entry(link, &dev->links.consumers, s_node) {
607 if (!(link->flags & DL_FLAG_MANAGED))
611 * Links created during consumer probe may be in the "consumer
612 * probe" state to start with if the supplier is still probing
613 * when they are created and they may become "active" if the
614 * consumer probe returns first. Skip them here.
616 if (link->status == DL_STATE_CONSUMER_PROBE ||
617 link->status == DL_STATE_ACTIVE)
620 WARN_ON(link->status != DL_STATE_DORMANT);
621 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
623 if (link->flags & DL_FLAG_AUTOPROBE_CONSUMER)
624 driver_deferred_probe_add(link->consumer);
627 list_for_each_entry(link, &dev->links.suppliers, c_node) {
628 if (!(link->flags & DL_FLAG_MANAGED))
631 WARN_ON(link->status != DL_STATE_CONSUMER_PROBE);
632 WRITE_ONCE(link->status, DL_STATE_ACTIVE);
635 dev->links.status = DL_DEV_DRIVER_BOUND;
637 device_links_write_unlock();
640 static void device_link_drop_managed(struct device_link *link)
642 link->flags &= ~DL_FLAG_MANAGED;
643 WRITE_ONCE(link->status, DL_STATE_NONE);
644 kref_put(&link->kref, __device_link_del);
648 * __device_links_no_driver - Update links of a device without a driver.
649 * @dev: Device without a drvier.
651 * Delete all non-persistent links from this device to any suppliers.
653 * Persistent links stay around, but their status is changed to "available",
654 * unless they already are in the "supplier unbind in progress" state in which
655 * case they need not be updated.
657 * Links without the DL_FLAG_MANAGED flag set are ignored.
659 static void __device_links_no_driver(struct device *dev)
661 struct device_link *link, *ln;
663 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
664 if (!(link->flags & DL_FLAG_MANAGED))
667 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER)
668 device_link_drop_managed(link);
669 else if (link->status == DL_STATE_CONSUMER_PROBE ||
670 link->status == DL_STATE_ACTIVE)
671 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
674 dev->links.status = DL_DEV_NO_DRIVER;
678 * device_links_no_driver - Update links after failing driver probe.
679 * @dev: Device whose driver has just failed to probe.
681 * Clean up leftover links to consumers for @dev and invoke
682 * %__device_links_no_driver() to update links to suppliers for it as
685 * Links without the DL_FLAG_MANAGED flag set are ignored.
687 void device_links_no_driver(struct device *dev)
689 struct device_link *link;
691 device_links_write_lock();
693 list_for_each_entry(link, &dev->links.consumers, s_node) {
694 if (!(link->flags & DL_FLAG_MANAGED))
698 * The probe has failed, so if the status of the link is
699 * "consumer probe" or "active", it must have been added by
700 * a probing consumer while this device was still probing.
701 * Change its state to "dormant", as it represents a valid
702 * relationship, but it is not functionally meaningful.
704 if (link->status == DL_STATE_CONSUMER_PROBE ||
705 link->status == DL_STATE_ACTIVE)
706 WRITE_ONCE(link->status, DL_STATE_DORMANT);
709 __device_links_no_driver(dev);
711 device_links_write_unlock();
715 * device_links_driver_cleanup - Update links after driver removal.
716 * @dev: Device whose driver has just gone away.
718 * Update links to consumers for @dev by changing their status to "dormant" and
719 * invoke %__device_links_no_driver() to update links to suppliers for it as
722 * Links without the DL_FLAG_MANAGED flag set are ignored.
724 void device_links_driver_cleanup(struct device *dev)
726 struct device_link *link, *ln;
728 device_links_write_lock();
730 list_for_each_entry_safe(link, ln, &dev->links.consumers, s_node) {
731 if (!(link->flags & DL_FLAG_MANAGED))
734 WARN_ON(link->flags & DL_FLAG_AUTOREMOVE_CONSUMER);
735 WARN_ON(link->status != DL_STATE_SUPPLIER_UNBIND);
738 * autoremove the links between this @dev and its consumer
739 * devices that are not active, i.e. where the link state
740 * has moved to DL_STATE_SUPPLIER_UNBIND.
742 if (link->status == DL_STATE_SUPPLIER_UNBIND &&
743 link->flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
744 device_link_drop_managed(link);
746 WRITE_ONCE(link->status, DL_STATE_DORMANT);
749 __device_links_no_driver(dev);
751 device_links_write_unlock();
755 * device_links_busy - Check if there are any busy links to consumers.
756 * @dev: Device to check.
758 * Check each consumer of the device and return 'true' if its link's status
759 * is one of "consumer probe" or "active" (meaning that the given consumer is
760 * probing right now or its driver is present). Otherwise, change the link
761 * state to "supplier unbind" to prevent the consumer from being probed
762 * successfully going forward.
764 * Return 'false' if there are no probing or active consumers.
766 * Links without the DL_FLAG_MANAGED flag set are ignored.
768 bool device_links_busy(struct device *dev)
770 struct device_link *link;
773 device_links_write_lock();
775 list_for_each_entry(link, &dev->links.consumers, s_node) {
776 if (!(link->flags & DL_FLAG_MANAGED))
779 if (link->status == DL_STATE_CONSUMER_PROBE
780 || link->status == DL_STATE_ACTIVE) {
784 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
787 dev->links.status = DL_DEV_UNBINDING;
789 device_links_write_unlock();
794 * device_links_unbind_consumers - Force unbind consumers of the given device.
795 * @dev: Device to unbind the consumers of.
797 * Walk the list of links to consumers for @dev and if any of them is in the
798 * "consumer probe" state, wait for all device probes in progress to complete
801 * If that's not the case, change the status of the link to "supplier unbind"
802 * and check if the link was in the "active" state. If so, force the consumer
803 * driver to unbind and start over (the consumer will not re-probe as we have
804 * changed the state of the link already).
806 * Links without the DL_FLAG_MANAGED flag set are ignored.
808 void device_links_unbind_consumers(struct device *dev)
810 struct device_link *link;
813 device_links_write_lock();
815 list_for_each_entry(link, &dev->links.consumers, s_node) {
816 enum device_link_state status;
818 if (!(link->flags & DL_FLAG_MANAGED))
821 status = link->status;
822 if (status == DL_STATE_CONSUMER_PROBE) {
823 device_links_write_unlock();
825 wait_for_device_probe();
828 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
829 if (status == DL_STATE_ACTIVE) {
830 struct device *consumer = link->consumer;
832 get_device(consumer);
834 device_links_write_unlock();
836 device_release_driver_internal(consumer, NULL,
838 put_device(consumer);
843 device_links_write_unlock();
847 * device_links_purge - Delete existing links to other devices.
848 * @dev: Target device.
850 static void device_links_purge(struct device *dev)
852 struct device_link *link, *ln;
855 * Delete all of the remaining links from this device to any other
856 * devices (either consumers or suppliers).
858 device_links_write_lock();
860 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
861 WARN_ON(link->status == DL_STATE_ACTIVE);
862 __device_link_del(&link->kref);
865 list_for_each_entry_safe_reverse(link, ln, &dev->links.consumers, s_node) {
866 WARN_ON(link->status != DL_STATE_DORMANT &&
867 link->status != DL_STATE_NONE);
868 __device_link_del(&link->kref);
871 device_links_write_unlock();
874 /* Device links support end. */
876 int (*platform_notify)(struct device *dev) = NULL;
877 int (*platform_notify_remove)(struct device *dev) = NULL;
878 static struct kobject *dev_kobj;
879 struct kobject *sysfs_dev_char_kobj;
880 struct kobject *sysfs_dev_block_kobj;
882 static DEFINE_MUTEX(device_hotplug_lock);
884 void lock_device_hotplug(void)
886 mutex_lock(&device_hotplug_lock);
889 void unlock_device_hotplug(void)
891 mutex_unlock(&device_hotplug_lock);
894 int lock_device_hotplug_sysfs(void)
896 if (mutex_trylock(&device_hotplug_lock))
899 /* Avoid busy looping (5 ms of sleep should do). */
901 return restart_syscall();
905 static inline int device_is_not_partition(struct device *dev)
907 return !(dev->type == &part_type);
910 static inline int device_is_not_partition(struct device *dev)
917 * dev_driver_string - Return a device's driver name, if at all possible
918 * @dev: struct device to get the name of
920 * Will return the device's driver's name if it is bound to a device. If
921 * the device is not bound to a driver, it will return the name of the bus
922 * it is attached to. If it is not attached to a bus either, an empty
923 * string will be returned.
925 const char *dev_driver_string(const struct device *dev)
927 struct device_driver *drv;
929 /* dev->driver can change to NULL underneath us because of unbinding,
930 * so be careful about accessing it. dev->bus and dev->class should
931 * never change once they are set, so they don't need special care.
933 drv = READ_ONCE(dev->driver);
934 return drv ? drv->name :
935 (dev->bus ? dev->bus->name :
936 (dev->class ? dev->class->name : ""));
938 EXPORT_SYMBOL(dev_driver_string);
940 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
942 static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
945 struct device_attribute *dev_attr = to_dev_attr(attr);
946 struct device *dev = kobj_to_dev(kobj);
950 ret = dev_attr->show(dev, dev_attr, buf);
951 if (ret >= (ssize_t)PAGE_SIZE) {
952 printk("dev_attr_show: %pS returned bad count\n",
958 static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
959 const char *buf, size_t count)
961 struct device_attribute *dev_attr = to_dev_attr(attr);
962 struct device *dev = kobj_to_dev(kobj);
966 ret = dev_attr->store(dev, dev_attr, buf, count);
970 static const struct sysfs_ops dev_sysfs_ops = {
971 .show = dev_attr_show,
972 .store = dev_attr_store,
975 #define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
977 ssize_t device_store_ulong(struct device *dev,
978 struct device_attribute *attr,
979 const char *buf, size_t size)
981 struct dev_ext_attribute *ea = to_ext_attr(attr);
983 unsigned long new = simple_strtoul(buf, &end, 0);
986 *(unsigned long *)(ea->var) = new;
987 /* Always return full write size even if we didn't consume all */
990 EXPORT_SYMBOL_GPL(device_store_ulong);
992 ssize_t device_show_ulong(struct device *dev,
993 struct device_attribute *attr,
996 struct dev_ext_attribute *ea = to_ext_attr(attr);
997 return snprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var));
999 EXPORT_SYMBOL_GPL(device_show_ulong);
1001 ssize_t device_store_int(struct device *dev,
1002 struct device_attribute *attr,
1003 const char *buf, size_t size)
1005 struct dev_ext_attribute *ea = to_ext_attr(attr);
1007 long new = simple_strtol(buf, &end, 0);
1008 if (end == buf || new > INT_MAX || new < INT_MIN)
1010 *(int *)(ea->var) = new;
1011 /* Always return full write size even if we didn't consume all */
1014 EXPORT_SYMBOL_GPL(device_store_int);
1016 ssize_t device_show_int(struct device *dev,
1017 struct device_attribute *attr,
1020 struct dev_ext_attribute *ea = to_ext_attr(attr);
1022 return snprintf(buf, PAGE_SIZE, "%d\n", *(int *)(ea->var));
1024 EXPORT_SYMBOL_GPL(device_show_int);
1026 ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
1027 const char *buf, size_t size)
1029 struct dev_ext_attribute *ea = to_ext_attr(attr);
1031 if (strtobool(buf, ea->var) < 0)
1036 EXPORT_SYMBOL_GPL(device_store_bool);
1038 ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
1041 struct dev_ext_attribute *ea = to_ext_attr(attr);
1043 return snprintf(buf, PAGE_SIZE, "%d\n", *(bool *)(ea->var));
1045 EXPORT_SYMBOL_GPL(device_show_bool);
1048 * device_release - free device structure.
1049 * @kobj: device's kobject.
1051 * This is called once the reference count for the object
1052 * reaches 0. We forward the call to the device's release
1053 * method, which should handle actually freeing the structure.
1055 static void device_release(struct kobject *kobj)
1057 struct device *dev = kobj_to_dev(kobj);
1058 struct device_private *p = dev->p;
1061 * Some platform devices are driven without driver attached
1062 * and managed resources may have been acquired. Make sure
1063 * all resources are released.
1065 * Drivers still can add resources into device after device
1066 * is deleted but alive, so release devres here to avoid
1067 * possible memory leak.
1069 devres_release_all(dev);
1073 else if (dev->type && dev->type->release)
1074 dev->type->release(dev);
1075 else if (dev->class && dev->class->dev_release)
1076 dev->class->dev_release(dev);
1078 WARN(1, KERN_ERR "Device '%s' does not have a release() "
1079 "function, it is broken and must be fixed.\n",
1084 static const void *device_namespace(struct kobject *kobj)
1086 struct device *dev = kobj_to_dev(kobj);
1087 const void *ns = NULL;
1089 if (dev->class && dev->class->ns_type)
1090 ns = dev->class->namespace(dev);
1095 static void device_get_ownership(struct kobject *kobj, kuid_t *uid, kgid_t *gid)
1097 struct device *dev = kobj_to_dev(kobj);
1099 if (dev->class && dev->class->get_ownership)
1100 dev->class->get_ownership(dev, uid, gid);
1103 static struct kobj_type device_ktype = {
1104 .release = device_release,
1105 .sysfs_ops = &dev_sysfs_ops,
1106 .namespace = device_namespace,
1107 .get_ownership = device_get_ownership,
1111 static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
1113 struct kobj_type *ktype = get_ktype(kobj);
1115 if (ktype == &device_ktype) {
1116 struct device *dev = kobj_to_dev(kobj);
1125 static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
1127 struct device *dev = kobj_to_dev(kobj);
1130 return dev->bus->name;
1132 return dev->class->name;
1136 static int dev_uevent(struct kset *kset, struct kobject *kobj,
1137 struct kobj_uevent_env *env)
1139 struct device *dev = kobj_to_dev(kobj);
1142 /* add device node properties if present */
1143 if (MAJOR(dev->devt)) {
1147 kuid_t uid = GLOBAL_ROOT_UID;
1148 kgid_t gid = GLOBAL_ROOT_GID;
1150 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
1151 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
1152 name = device_get_devnode(dev, &mode, &uid, &gid, &tmp);
1154 add_uevent_var(env, "DEVNAME=%s", name);
1156 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
1157 if (!uid_eq(uid, GLOBAL_ROOT_UID))
1158 add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid));
1159 if (!gid_eq(gid, GLOBAL_ROOT_GID))
1160 add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid));
1165 if (dev->type && dev->type->name)
1166 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
1169 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
1171 /* Add common DT information about the device */
1172 of_device_uevent(dev, env);
1174 /* have the bus specific function add its stuff */
1175 if (dev->bus && dev->bus->uevent) {
1176 retval = dev->bus->uevent(dev, env);
1178 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
1179 dev_name(dev), __func__, retval);
1182 /* have the class specific function add its stuff */
1183 if (dev->class && dev->class->dev_uevent) {
1184 retval = dev->class->dev_uevent(dev, env);
1186 pr_debug("device: '%s': %s: class uevent() "
1187 "returned %d\n", dev_name(dev),
1191 /* have the device type specific function add its stuff */
1192 if (dev->type && dev->type->uevent) {
1193 retval = dev->type->uevent(dev, env);
1195 pr_debug("device: '%s': %s: dev_type uevent() "
1196 "returned %d\n", dev_name(dev),
1203 static const struct kset_uevent_ops device_uevent_ops = {
1204 .filter = dev_uevent_filter,
1205 .name = dev_uevent_name,
1206 .uevent = dev_uevent,
1209 static ssize_t uevent_show(struct device *dev, struct device_attribute *attr,
1212 struct kobject *top_kobj;
1214 struct kobj_uevent_env *env = NULL;
1219 /* search the kset, the device belongs to */
1220 top_kobj = &dev->kobj;
1221 while (!top_kobj->kset && top_kobj->parent)
1222 top_kobj = top_kobj->parent;
1223 if (!top_kobj->kset)
1226 kset = top_kobj->kset;
1227 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
1230 /* respect filter */
1231 if (kset->uevent_ops && kset->uevent_ops->filter)
1232 if (!kset->uevent_ops->filter(kset, &dev->kobj))
1235 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
1239 /* let the kset specific function add its keys */
1240 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
1244 /* copy keys to file */
1245 for (i = 0; i < env->envp_idx; i++)
1246 count += sprintf(&buf[count], "%s\n", env->envp[i]);
1252 static ssize_t uevent_store(struct device *dev, struct device_attribute *attr,
1253 const char *buf, size_t count)
1257 rc = kobject_synth_uevent(&dev->kobj, buf, count);
1260 dev_err(dev, "uevent: failed to send synthetic uevent\n");
1266 static DEVICE_ATTR_RW(uevent);
1268 static ssize_t online_show(struct device *dev, struct device_attribute *attr,
1274 val = !dev->offline;
1276 return sprintf(buf, "%u\n", val);
1279 static ssize_t online_store(struct device *dev, struct device_attribute *attr,
1280 const char *buf, size_t count)
1285 ret = strtobool(buf, &val);
1289 ret = lock_device_hotplug_sysfs();
1293 ret = val ? device_online(dev) : device_offline(dev);
1294 unlock_device_hotplug();
1295 return ret < 0 ? ret : count;
1297 static DEVICE_ATTR_RW(online);
1299 int device_add_groups(struct device *dev, const struct attribute_group **groups)
1301 return sysfs_create_groups(&dev->kobj, groups);
1303 EXPORT_SYMBOL_GPL(device_add_groups);
1305 void device_remove_groups(struct device *dev,
1306 const struct attribute_group **groups)
1308 sysfs_remove_groups(&dev->kobj, groups);
1310 EXPORT_SYMBOL_GPL(device_remove_groups);
1312 union device_attr_group_devres {
1313 const struct attribute_group *group;
1314 const struct attribute_group **groups;
1317 static int devm_attr_group_match(struct device *dev, void *res, void *data)
1319 return ((union device_attr_group_devres *)res)->group == data;
1322 static void devm_attr_group_remove(struct device *dev, void *res)
1324 union device_attr_group_devres *devres = res;
1325 const struct attribute_group *group = devres->group;
1327 dev_dbg(dev, "%s: removing group %p\n", __func__, group);
1328 sysfs_remove_group(&dev->kobj, group);
1331 static void devm_attr_groups_remove(struct device *dev, void *res)
1333 union device_attr_group_devres *devres = res;
1334 const struct attribute_group **groups = devres->groups;
1336 dev_dbg(dev, "%s: removing groups %p\n", __func__, groups);
1337 sysfs_remove_groups(&dev->kobj, groups);
1341 * devm_device_add_group - given a device, create a managed attribute group
1342 * @dev: The device to create the group for
1343 * @grp: The attribute group to create
1345 * This function creates a group for the first time. It will explicitly
1346 * warn and error if any of the attribute files being created already exist.
1348 * Returns 0 on success or error code on failure.
1350 int devm_device_add_group(struct device *dev, const struct attribute_group *grp)
1352 union device_attr_group_devres *devres;
1355 devres = devres_alloc(devm_attr_group_remove,
1356 sizeof(*devres), GFP_KERNEL);
1360 error = sysfs_create_group(&dev->kobj, grp);
1362 devres_free(devres);
1366 devres->group = grp;
1367 devres_add(dev, devres);
1370 EXPORT_SYMBOL_GPL(devm_device_add_group);
1373 * devm_device_remove_group: remove a managed group from a device
1374 * @dev: device to remove the group from
1375 * @grp: group to remove
1377 * This function removes a group of attributes from a device. The attributes
1378 * previously have to have been created for this group, otherwise it will fail.
1380 void devm_device_remove_group(struct device *dev,
1381 const struct attribute_group *grp)
1383 WARN_ON(devres_release(dev, devm_attr_group_remove,
1384 devm_attr_group_match,
1385 /* cast away const */ (void *)grp));
1387 EXPORT_SYMBOL_GPL(devm_device_remove_group);
1390 * devm_device_add_groups - create a bunch of managed attribute groups
1391 * @dev: The device to create the group for
1392 * @groups: The attribute groups to create, NULL terminated
1394 * This function creates a bunch of managed attribute groups. If an error
1395 * occurs when creating a group, all previously created groups will be
1396 * removed, unwinding everything back to the original state when this
1397 * function was called. It will explicitly warn and error if any of the
1398 * attribute files being created already exist.
1400 * Returns 0 on success or error code from sysfs_create_group on failure.
1402 int devm_device_add_groups(struct device *dev,
1403 const struct attribute_group **groups)
1405 union device_attr_group_devres *devres;
1408 devres = devres_alloc(devm_attr_groups_remove,
1409 sizeof(*devres), GFP_KERNEL);
1413 error = sysfs_create_groups(&dev->kobj, groups);
1415 devres_free(devres);
1419 devres->groups = groups;
1420 devres_add(dev, devres);
1423 EXPORT_SYMBOL_GPL(devm_device_add_groups);
1426 * devm_device_remove_groups - remove a list of managed groups
1428 * @dev: The device for the groups to be removed from
1429 * @groups: NULL terminated list of groups to be removed
1431 * If groups is not NULL, remove the specified groups from the device.
1433 void devm_device_remove_groups(struct device *dev,
1434 const struct attribute_group **groups)
1436 WARN_ON(devres_release(dev, devm_attr_groups_remove,
1437 devm_attr_group_match,
1438 /* cast away const */ (void *)groups));
1440 EXPORT_SYMBOL_GPL(devm_device_remove_groups);
1442 static int device_add_attrs(struct device *dev)
1444 struct class *class = dev->class;
1445 const struct device_type *type = dev->type;
1449 error = device_add_groups(dev, class->dev_groups);
1455 error = device_add_groups(dev, type->groups);
1457 goto err_remove_class_groups;
1460 error = device_add_groups(dev, dev->groups);
1462 goto err_remove_type_groups;
1464 if (device_supports_offline(dev) && !dev->offline_disabled) {
1465 error = device_create_file(dev, &dev_attr_online);
1467 goto err_remove_dev_groups;
1472 err_remove_dev_groups:
1473 device_remove_groups(dev, dev->groups);
1474 err_remove_type_groups:
1476 device_remove_groups(dev, type->groups);
1477 err_remove_class_groups:
1479 device_remove_groups(dev, class->dev_groups);
1484 static void device_remove_attrs(struct device *dev)
1486 struct class *class = dev->class;
1487 const struct device_type *type = dev->type;
1489 device_remove_file(dev, &dev_attr_online);
1490 device_remove_groups(dev, dev->groups);
1493 device_remove_groups(dev, type->groups);
1496 device_remove_groups(dev, class->dev_groups);
1499 static ssize_t dev_show(struct device *dev, struct device_attribute *attr,
1502 return print_dev_t(buf, dev->devt);
1504 static DEVICE_ATTR_RO(dev);
1507 struct kset *devices_kset;
1510 * devices_kset_move_before - Move device in the devices_kset's list.
1511 * @deva: Device to move.
1512 * @devb: Device @deva should come before.
1514 static void devices_kset_move_before(struct device *deva, struct device *devb)
1518 pr_debug("devices_kset: Moving %s before %s\n",
1519 dev_name(deva), dev_name(devb));
1520 spin_lock(&devices_kset->list_lock);
1521 list_move_tail(&deva->kobj.entry, &devb->kobj.entry);
1522 spin_unlock(&devices_kset->list_lock);
1526 * devices_kset_move_after - Move device in the devices_kset's list.
1527 * @deva: Device to move
1528 * @devb: Device @deva should come after.
1530 static void devices_kset_move_after(struct device *deva, struct device *devb)
1534 pr_debug("devices_kset: Moving %s after %s\n",
1535 dev_name(deva), dev_name(devb));
1536 spin_lock(&devices_kset->list_lock);
1537 list_move(&deva->kobj.entry, &devb->kobj.entry);
1538 spin_unlock(&devices_kset->list_lock);
1542 * devices_kset_move_last - move the device to the end of devices_kset's list.
1543 * @dev: device to move
1545 void devices_kset_move_last(struct device *dev)
1549 pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev));
1550 spin_lock(&devices_kset->list_lock);
1551 list_move_tail(&dev->kobj.entry, &devices_kset->list);
1552 spin_unlock(&devices_kset->list_lock);
1556 * device_create_file - create sysfs attribute file for device.
1558 * @attr: device attribute descriptor.
1560 int device_create_file(struct device *dev,
1561 const struct device_attribute *attr)
1566 WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
1567 "Attribute %s: write permission without 'store'\n",
1569 WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
1570 "Attribute %s: read permission without 'show'\n",
1572 error = sysfs_create_file(&dev->kobj, &attr->attr);
1577 EXPORT_SYMBOL_GPL(device_create_file);
1580 * device_remove_file - remove sysfs attribute file.
1582 * @attr: device attribute descriptor.
1584 void device_remove_file(struct device *dev,
1585 const struct device_attribute *attr)
1588 sysfs_remove_file(&dev->kobj, &attr->attr);
1590 EXPORT_SYMBOL_GPL(device_remove_file);
1593 * device_remove_file_self - remove sysfs attribute file from its own method.
1595 * @attr: device attribute descriptor.
1597 * See kernfs_remove_self() for details.
1599 bool device_remove_file_self(struct device *dev,
1600 const struct device_attribute *attr)
1603 return sysfs_remove_file_self(&dev->kobj, &attr->attr);
1607 EXPORT_SYMBOL_GPL(device_remove_file_self);
1610 * device_create_bin_file - create sysfs binary attribute file for device.
1612 * @attr: device binary attribute descriptor.
1614 int device_create_bin_file(struct device *dev,
1615 const struct bin_attribute *attr)
1617 int error = -EINVAL;
1619 error = sysfs_create_bin_file(&dev->kobj, attr);
1622 EXPORT_SYMBOL_GPL(device_create_bin_file);
1625 * device_remove_bin_file - remove sysfs binary attribute file
1627 * @attr: device binary attribute descriptor.
1629 void device_remove_bin_file(struct device *dev,
1630 const struct bin_attribute *attr)
1633 sysfs_remove_bin_file(&dev->kobj, attr);
1635 EXPORT_SYMBOL_GPL(device_remove_bin_file);
1637 static void klist_children_get(struct klist_node *n)
1639 struct device_private *p = to_device_private_parent(n);
1640 struct device *dev = p->device;
1645 static void klist_children_put(struct klist_node *n)
1647 struct device_private *p = to_device_private_parent(n);
1648 struct device *dev = p->device;
1654 * device_initialize - init device structure.
1657 * This prepares the device for use by other layers by initializing
1659 * It is the first half of device_register(), if called by
1660 * that function, though it can also be called separately, so one
1661 * may use @dev's fields. In particular, get_device()/put_device()
1662 * may be used for reference counting of @dev after calling this
1665 * All fields in @dev must be initialized by the caller to 0, except
1666 * for those explicitly set to some other value. The simplest
1667 * approach is to use kzalloc() to allocate the structure containing
1670 * NOTE: Use put_device() to give up your reference instead of freeing
1671 * @dev directly once you have called this function.
1673 void device_initialize(struct device *dev)
1675 dev->kobj.kset = devices_kset;
1676 kobject_init(&dev->kobj, &device_ktype);
1677 INIT_LIST_HEAD(&dev->dma_pools);
1678 mutex_init(&dev->mutex);
1679 lockdep_set_novalidate_class(&dev->mutex);
1680 spin_lock_init(&dev->devres_lock);
1681 INIT_LIST_HEAD(&dev->devres_head);
1682 device_pm_init(dev);
1683 set_dev_node(dev, -1);
1684 #ifdef CONFIG_GENERIC_MSI_IRQ
1685 raw_spin_lock_init(&dev->msi_lock);
1686 INIT_LIST_HEAD(&dev->msi_list);
1688 INIT_LIST_HEAD(&dev->links.consumers);
1689 INIT_LIST_HEAD(&dev->links.suppliers);
1690 dev->links.status = DL_DEV_NO_DRIVER;
1692 EXPORT_SYMBOL_GPL(device_initialize);
1694 struct kobject *virtual_device_parent(struct device *dev)
1696 static struct kobject *virtual_dir = NULL;
1699 virtual_dir = kobject_create_and_add("virtual",
1700 &devices_kset->kobj);
1706 struct kobject kobj;
1707 struct class *class;
1710 #define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
1712 static void class_dir_release(struct kobject *kobj)
1714 struct class_dir *dir = to_class_dir(kobj);
1719 struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
1721 struct class_dir *dir = to_class_dir(kobj);
1722 return dir->class->ns_type;
1725 static struct kobj_type class_dir_ktype = {
1726 .release = class_dir_release,
1727 .sysfs_ops = &kobj_sysfs_ops,
1728 .child_ns_type = class_dir_child_ns_type
1731 static struct kobject *
1732 class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
1734 struct class_dir *dir;
1737 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1739 return ERR_PTR(-ENOMEM);
1742 kobject_init(&dir->kobj, &class_dir_ktype);
1744 dir->kobj.kset = &class->p->glue_dirs;
1746 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
1748 kobject_put(&dir->kobj);
1749 return ERR_PTR(retval);
1754 static DEFINE_MUTEX(gdp_mutex);
1756 static struct kobject *get_device_parent(struct device *dev,
1757 struct device *parent)
1760 struct kobject *kobj = NULL;
1761 struct kobject *parent_kobj;
1765 /* block disks show up in /sys/block */
1766 if (sysfs_deprecated && dev->class == &block_class) {
1767 if (parent && parent->class == &block_class)
1768 return &parent->kobj;
1769 return &block_class.p->subsys.kobj;
1774 * If we have no parent, we live in "virtual".
1775 * Class-devices with a non class-device as parent, live
1776 * in a "glue" directory to prevent namespace collisions.
1779 parent_kobj = virtual_device_parent(dev);
1780 else if (parent->class && !dev->class->ns_type)
1781 return &parent->kobj;
1783 parent_kobj = &parent->kobj;
1785 mutex_lock(&gdp_mutex);
1787 /* find our class-directory at the parent and reference it */
1788 spin_lock(&dev->class->p->glue_dirs.list_lock);
1789 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
1790 if (k->parent == parent_kobj) {
1791 kobj = kobject_get(k);
1794 spin_unlock(&dev->class->p->glue_dirs.list_lock);
1796 mutex_unlock(&gdp_mutex);
1800 /* or create a new class-directory at the parent device */
1801 k = class_dir_create_and_add(dev->class, parent_kobj);
1802 /* do not emit an uevent for this simple "glue" directory */
1803 mutex_unlock(&gdp_mutex);
1807 /* subsystems can specify a default root directory for their devices */
1808 if (!parent && dev->bus && dev->bus->dev_root)
1809 return &dev->bus->dev_root->kobj;
1812 return &parent->kobj;
1816 static inline bool live_in_glue_dir(struct kobject *kobj,
1819 if (!kobj || !dev->class ||
1820 kobj->kset != &dev->class->p->glue_dirs)
1825 static inline struct kobject *get_glue_dir(struct device *dev)
1827 return dev->kobj.parent;
1831 * make sure cleaning up dir as the last step, we need to make
1832 * sure .release handler of kobject is run with holding the
1835 static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
1839 /* see if we live in a "glue" directory */
1840 if (!live_in_glue_dir(glue_dir, dev))
1843 mutex_lock(&gdp_mutex);
1845 * There is a race condition between removing glue directory
1846 * and adding a new device under the glue directory.
1851 * get_device_parent()
1852 * class_dir_create_and_add()
1853 * kobject_add_internal()
1854 * create_dir() // create glue_dir
1857 * get_device_parent()
1858 * kobject_get() // get glue_dir
1861 * cleanup_glue_dir()
1862 * kobject_del(glue_dir)
1865 * kobject_add_internal()
1866 * create_dir() // in glue_dir
1867 * sysfs_create_dir_ns()
1868 * kernfs_create_dir_ns(sd)
1870 * sysfs_remove_dir() // glue_dir->sd=NULL
1871 * sysfs_put() // free glue_dir->sd
1874 * kernfs_new_node(sd)
1875 * kernfs_get(glue_dir)
1879 * Before CPU1 remove last child device under glue dir, if CPU2 add
1880 * a new device under glue dir, the glue_dir kobject reference count
1881 * will be increase to 2 in kobject_get(k). And CPU2 has been called
1882 * kernfs_create_dir_ns(). Meanwhile, CPU1 call sysfs_remove_dir()
1883 * and sysfs_put(). This result in glue_dir->sd is freed.
1885 * Then the CPU2 will see a stale "empty" but still potentially used
1886 * glue dir around in kernfs_new_node().
1888 * In order to avoid this happening, we also should make sure that
1889 * kernfs_node for glue_dir is released in CPU1 only when refcount
1890 * for glue_dir kobj is 1.
1892 ref = kref_read(&glue_dir->kref);
1893 if (!kobject_has_children(glue_dir) && !--ref)
1894 kobject_del(glue_dir);
1895 kobject_put(glue_dir);
1896 mutex_unlock(&gdp_mutex);
1899 static int device_add_class_symlinks(struct device *dev)
1901 struct device_node *of_node = dev_of_node(dev);
1905 error = sysfs_create_link(&dev->kobj, of_node_kobj(of_node), "of_node");
1907 dev_warn(dev, "Error %d creating of_node link\n",error);
1908 /* An error here doesn't warrant bringing down the device */
1914 error = sysfs_create_link(&dev->kobj,
1915 &dev->class->p->subsys.kobj,
1920 if (dev->parent && device_is_not_partition(dev)) {
1921 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
1928 /* /sys/block has directories and does not need symlinks */
1929 if (sysfs_deprecated && dev->class == &block_class)
1933 /* link in the class directory pointing to the device */
1934 error = sysfs_create_link(&dev->class->p->subsys.kobj,
1935 &dev->kobj, dev_name(dev));
1942 sysfs_remove_link(&dev->kobj, "device");
1945 sysfs_remove_link(&dev->kobj, "subsystem");
1947 sysfs_remove_link(&dev->kobj, "of_node");
1951 static void device_remove_class_symlinks(struct device *dev)
1953 if (dev_of_node(dev))
1954 sysfs_remove_link(&dev->kobj, "of_node");
1959 if (dev->parent && device_is_not_partition(dev))
1960 sysfs_remove_link(&dev->kobj, "device");
1961 sysfs_remove_link(&dev->kobj, "subsystem");
1963 if (sysfs_deprecated && dev->class == &block_class)
1966 sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
1970 * dev_set_name - set a device name
1972 * @fmt: format string for the device's name
1974 int dev_set_name(struct device *dev, const char *fmt, ...)
1979 va_start(vargs, fmt);
1980 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
1984 EXPORT_SYMBOL_GPL(dev_set_name);
1987 * device_to_dev_kobj - select a /sys/dev/ directory for the device
1990 * By default we select char/ for new entries. Setting class->dev_obj
1991 * to NULL prevents an entry from being created. class->dev_kobj must
1992 * be set (or cleared) before any devices are registered to the class
1993 * otherwise device_create_sys_dev_entry() and
1994 * device_remove_sys_dev_entry() will disagree about the presence of
1997 static struct kobject *device_to_dev_kobj(struct device *dev)
1999 struct kobject *kobj;
2002 kobj = dev->class->dev_kobj;
2004 kobj = sysfs_dev_char_kobj;
2009 static int device_create_sys_dev_entry(struct device *dev)
2011 struct kobject *kobj = device_to_dev_kobj(dev);
2016 format_dev_t(devt_str, dev->devt);
2017 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
2023 static void device_remove_sys_dev_entry(struct device *dev)
2025 struct kobject *kobj = device_to_dev_kobj(dev);
2029 format_dev_t(devt_str, dev->devt);
2030 sysfs_remove_link(kobj, devt_str);
2034 static int device_private_init(struct device *dev)
2036 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
2039 dev->p->device = dev;
2040 klist_init(&dev->p->klist_children, klist_children_get,
2041 klist_children_put);
2042 INIT_LIST_HEAD(&dev->p->deferred_probe);
2047 * device_add - add device to device hierarchy.
2050 * This is part 2 of device_register(), though may be called
2051 * separately _iff_ device_initialize() has been called separately.
2053 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
2054 * to the global and sibling lists for the device, then
2055 * adds it to the other relevant subsystems of the driver model.
2057 * Do not call this routine or device_register() more than once for
2058 * any device structure. The driver model core is not designed to work
2059 * with devices that get unregistered and then spring back to life.
2060 * (Among other things, it's very hard to guarantee that all references
2061 * to the previous incarnation of @dev have been dropped.) Allocate
2062 * and register a fresh new struct device instead.
2064 * NOTE: _Never_ directly free @dev after calling this function, even
2065 * if it returned an error! Always use put_device() to give up your
2066 * reference instead.
2068 int device_add(struct device *dev)
2070 struct device *parent;
2071 struct kobject *kobj;
2072 struct class_interface *class_intf;
2073 int error = -EINVAL;
2074 struct kobject *glue_dir = NULL;
2076 dev = get_device(dev);
2081 error = device_private_init(dev);
2087 * for statically allocated devices, which should all be converted
2088 * some day, we need to initialize the name. We prevent reading back
2089 * the name, and force the use of dev_name()
2091 if (dev->init_name) {
2092 dev_set_name(dev, "%s", dev->init_name);
2093 dev->init_name = NULL;
2096 /* subsystems can specify simple device enumeration */
2097 if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
2098 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
2100 if (!dev_name(dev)) {
2105 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
2107 parent = get_device(dev->parent);
2108 kobj = get_device_parent(dev, parent);
2110 error = PTR_ERR(kobj);
2114 dev->kobj.parent = kobj;
2116 /* use parent numa_node */
2117 if (parent && (dev_to_node(dev) == NUMA_NO_NODE))
2118 set_dev_node(dev, dev_to_node(parent));
2120 /* first, register with generic layer. */
2121 /* we require the name to be set before, and pass NULL */
2122 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
2124 glue_dir = get_glue_dir(dev);
2128 /* notify platform of device entry */
2129 if (platform_notify)
2130 platform_notify(dev);
2132 error = device_create_file(dev, &dev_attr_uevent);
2136 error = device_add_class_symlinks(dev);
2139 error = device_add_attrs(dev);
2142 error = bus_add_device(dev);
2145 error = dpm_sysfs_add(dev);
2150 if (MAJOR(dev->devt)) {
2151 error = device_create_file(dev, &dev_attr_dev);
2155 error = device_create_sys_dev_entry(dev);
2159 devtmpfs_create_node(dev);
2162 /* Notify clients of device addition. This call must come
2163 * after dpm_sysfs_add() and before kobject_uevent().
2166 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2167 BUS_NOTIFY_ADD_DEVICE, dev);
2169 kobject_uevent(&dev->kobj, KOBJ_ADD);
2170 bus_probe_device(dev);
2172 klist_add_tail(&dev->p->knode_parent,
2173 &parent->p->klist_children);
2176 mutex_lock(&dev->class->p->mutex);
2177 /* tie the class to the device */
2178 klist_add_tail(&dev->knode_class,
2179 &dev->class->p->klist_devices);
2181 /* notify any interfaces that the device is here */
2182 list_for_each_entry(class_intf,
2183 &dev->class->p->interfaces, node)
2184 if (class_intf->add_dev)
2185 class_intf->add_dev(dev, class_intf);
2186 mutex_unlock(&dev->class->p->mutex);
2192 if (MAJOR(dev->devt))
2193 device_remove_file(dev, &dev_attr_dev);
2195 device_pm_remove(dev);
2196 dpm_sysfs_remove(dev);
2198 bus_remove_device(dev);
2200 device_remove_attrs(dev);
2202 device_remove_class_symlinks(dev);
2204 device_remove_file(dev, &dev_attr_uevent);
2206 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
2207 glue_dir = get_glue_dir(dev);
2208 kobject_del(&dev->kobj);
2210 cleanup_glue_dir(dev, glue_dir);
2218 EXPORT_SYMBOL_GPL(device_add);
2221 * device_register - register a device with the system.
2222 * @dev: pointer to the device structure
2224 * This happens in two clean steps - initialize the device
2225 * and add it to the system. The two steps can be called
2226 * separately, but this is the easiest and most common.
2227 * I.e. you should only call the two helpers separately if
2228 * have a clearly defined need to use and refcount the device
2229 * before it is added to the hierarchy.
2231 * For more information, see the kerneldoc for device_initialize()
2234 * NOTE: _Never_ directly free @dev after calling this function, even
2235 * if it returned an error! Always use put_device() to give up the
2236 * reference initialized in this function instead.
2238 int device_register(struct device *dev)
2240 device_initialize(dev);
2241 return device_add(dev);
2243 EXPORT_SYMBOL_GPL(device_register);
2246 * get_device - increment reference count for device.
2249 * This simply forwards the call to kobject_get(), though
2250 * we do take care to provide for the case that we get a NULL
2251 * pointer passed in.
2253 struct device *get_device(struct device *dev)
2255 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
2257 EXPORT_SYMBOL_GPL(get_device);
2260 * put_device - decrement reference count.
2261 * @dev: device in question.
2263 void put_device(struct device *dev)
2265 /* might_sleep(); */
2267 kobject_put(&dev->kobj);
2269 EXPORT_SYMBOL_GPL(put_device);
2271 bool kill_device(struct device *dev)
2274 * Require the device lock and set the "dead" flag to guarantee that
2275 * the update behavior is consistent with the other bitfields near
2276 * it and that we cannot have an asynchronous probe routine trying
2277 * to run while we are tearing out the bus/class/sysfs from
2278 * underneath the device.
2280 lockdep_assert_held(&dev->mutex);
2284 dev->p->dead = true;
2287 EXPORT_SYMBOL_GPL(kill_device);
2290 * device_del - delete device from system.
2293 * This is the first part of the device unregistration
2294 * sequence. This removes the device from the lists we control
2295 * from here, has it removed from the other driver model
2296 * subsystems it was added to in device_add(), and removes it
2297 * from the kobject hierarchy.
2299 * NOTE: this should be called manually _iff_ device_add() was
2300 * also called manually.
2302 void device_del(struct device *dev)
2304 struct device *parent = dev->parent;
2305 struct kobject *glue_dir = NULL;
2306 struct class_interface *class_intf;
2312 /* Notify clients of device removal. This call must come
2313 * before dpm_sysfs_remove().
2316 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2317 BUS_NOTIFY_DEL_DEVICE, dev);
2319 dpm_sysfs_remove(dev);
2321 klist_del(&dev->p->knode_parent);
2322 if (MAJOR(dev->devt)) {
2323 devtmpfs_delete_node(dev);
2324 device_remove_sys_dev_entry(dev);
2325 device_remove_file(dev, &dev_attr_dev);
2328 device_remove_class_symlinks(dev);
2330 mutex_lock(&dev->class->p->mutex);
2331 /* notify any interfaces that the device is now gone */
2332 list_for_each_entry(class_intf,
2333 &dev->class->p->interfaces, node)
2334 if (class_intf->remove_dev)
2335 class_intf->remove_dev(dev, class_intf);
2336 /* remove the device from the class list */
2337 klist_del(&dev->knode_class);
2338 mutex_unlock(&dev->class->p->mutex);
2340 device_remove_file(dev, &dev_attr_uevent);
2341 device_remove_attrs(dev);
2342 bus_remove_device(dev);
2343 device_pm_remove(dev);
2344 driver_deferred_probe_del(dev);
2345 device_remove_properties(dev);
2346 device_links_purge(dev);
2348 /* Notify the platform of the removal, in case they
2349 * need to do anything...
2351 if (platform_notify_remove)
2352 platform_notify_remove(dev);
2354 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2355 BUS_NOTIFY_REMOVED_DEVICE, dev);
2356 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
2357 glue_dir = get_glue_dir(dev);
2358 kobject_del(&dev->kobj);
2359 cleanup_glue_dir(dev, glue_dir);
2362 EXPORT_SYMBOL_GPL(device_del);
2365 * device_unregister - unregister device from system.
2366 * @dev: device going away.
2368 * We do this in two parts, like we do device_register(). First,
2369 * we remove it from all the subsystems with device_del(), then
2370 * we decrement the reference count via put_device(). If that
2371 * is the final reference count, the device will be cleaned up
2372 * via device_release() above. Otherwise, the structure will
2373 * stick around until the final reference to the device is dropped.
2375 void device_unregister(struct device *dev)
2377 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
2381 EXPORT_SYMBOL_GPL(device_unregister);
2383 static struct device *prev_device(struct klist_iter *i)
2385 struct klist_node *n = klist_prev(i);
2386 struct device *dev = NULL;
2387 struct device_private *p;
2390 p = to_device_private_parent(n);
2396 static struct device *next_device(struct klist_iter *i)
2398 struct klist_node *n = klist_next(i);
2399 struct device *dev = NULL;
2400 struct device_private *p;
2403 p = to_device_private_parent(n);
2410 * device_get_devnode - path of device node file
2412 * @mode: returned file access mode
2413 * @uid: returned file owner
2414 * @gid: returned file group
2415 * @tmp: possibly allocated string
2417 * Return the relative path of a possible device node.
2418 * Non-default names may need to allocate a memory to compose
2419 * a name. This memory is returned in tmp and needs to be
2420 * freed by the caller.
2422 const char *device_get_devnode(struct device *dev,
2423 umode_t *mode, kuid_t *uid, kgid_t *gid,
2430 /* the device type may provide a specific name */
2431 if (dev->type && dev->type->devnode)
2432 *tmp = dev->type->devnode(dev, mode, uid, gid);
2436 /* the class may provide a specific name */
2437 if (dev->class && dev->class->devnode)
2438 *tmp = dev->class->devnode(dev, mode);
2442 /* return name without allocation, tmp == NULL */
2443 if (strchr(dev_name(dev), '!') == NULL)
2444 return dev_name(dev);
2446 /* replace '!' in the name with '/' */
2447 s = kstrdup(dev_name(dev), GFP_KERNEL);
2450 strreplace(s, '!', '/');
2455 * device_for_each_child - device child iterator.
2456 * @parent: parent struct device.
2457 * @fn: function to be called for each device.
2458 * @data: data for the callback.
2460 * Iterate over @parent's child devices, and call @fn for each,
2463 * We check the return of @fn each time. If it returns anything
2464 * other than 0, we break out and return that value.
2466 int device_for_each_child(struct device *parent, void *data,
2467 int (*fn)(struct device *dev, void *data))
2469 struct klist_iter i;
2470 struct device *child;
2476 klist_iter_init(&parent->p->klist_children, &i);
2477 while (!error && (child = next_device(&i)))
2478 error = fn(child, data);
2479 klist_iter_exit(&i);
2482 EXPORT_SYMBOL_GPL(device_for_each_child);
2485 * device_for_each_child_reverse - device child iterator in reversed order.
2486 * @parent: parent struct device.
2487 * @fn: function to be called for each device.
2488 * @data: data for the callback.
2490 * Iterate over @parent's child devices, and call @fn for each,
2493 * We check the return of @fn each time. If it returns anything
2494 * other than 0, we break out and return that value.
2496 int device_for_each_child_reverse(struct device *parent, void *data,
2497 int (*fn)(struct device *dev, void *data))
2499 struct klist_iter i;
2500 struct device *child;
2506 klist_iter_init(&parent->p->klist_children, &i);
2507 while ((child = prev_device(&i)) && !error)
2508 error = fn(child, data);
2509 klist_iter_exit(&i);
2512 EXPORT_SYMBOL_GPL(device_for_each_child_reverse);
2515 * device_find_child - device iterator for locating a particular device.
2516 * @parent: parent struct device
2517 * @match: Callback function to check device
2518 * @data: Data to pass to match function
2520 * This is similar to the device_for_each_child() function above, but it
2521 * returns a reference to a device that is 'found' for later use, as
2522 * determined by the @match callback.
2524 * The callback should return 0 if the device doesn't match and non-zero
2525 * if it does. If the callback returns non-zero and a reference to the
2526 * current device can be obtained, this function will return to the caller
2527 * and not iterate over any more devices.
2529 * NOTE: you will need to drop the reference with put_device() after use.
2531 struct device *device_find_child(struct device *parent, void *data,
2532 int (*match)(struct device *dev, void *data))
2534 struct klist_iter i;
2535 struct device *child;
2540 klist_iter_init(&parent->p->klist_children, &i);
2541 while ((child = next_device(&i)))
2542 if (match(child, data) && get_device(child))
2544 klist_iter_exit(&i);
2547 EXPORT_SYMBOL_GPL(device_find_child);
2549 int __init devices_init(void)
2551 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
2554 dev_kobj = kobject_create_and_add("dev", NULL);
2557 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
2558 if (!sysfs_dev_block_kobj)
2559 goto block_kobj_err;
2560 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
2561 if (!sysfs_dev_char_kobj)
2567 kobject_put(sysfs_dev_block_kobj);
2569 kobject_put(dev_kobj);
2571 kset_unregister(devices_kset);
2575 static int device_check_offline(struct device *dev, void *not_used)
2579 ret = device_for_each_child(dev, NULL, device_check_offline);
2583 return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
2587 * device_offline - Prepare the device for hot-removal.
2588 * @dev: Device to be put offline.
2590 * Execute the device bus type's .offline() callback, if present, to prepare
2591 * the device for a subsequent hot-removal. If that succeeds, the device must
2592 * not be used until either it is removed or its bus type's .online() callback
2595 * Call under device_hotplug_lock.
2597 int device_offline(struct device *dev)
2601 if (dev->offline_disabled)
2604 ret = device_for_each_child(dev, NULL, device_check_offline);
2609 if (device_supports_offline(dev)) {
2613 ret = dev->bus->offline(dev);
2615 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
2616 dev->offline = true;
2626 * device_online - Put the device back online after successful device_offline().
2627 * @dev: Device to be put back online.
2629 * If device_offline() has been successfully executed for @dev, but the device
2630 * has not been removed subsequently, execute its bus type's .online() callback
2631 * to indicate that the device can be used again.
2633 * Call under device_hotplug_lock.
2635 int device_online(struct device *dev)
2640 if (device_supports_offline(dev)) {
2642 ret = dev->bus->online(dev);
2644 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
2645 dev->offline = false;
2656 struct root_device {
2658 struct module *owner;
2661 static inline struct root_device *to_root_device(struct device *d)
2663 return container_of(d, struct root_device, dev);
2666 static void root_device_release(struct device *dev)
2668 kfree(to_root_device(dev));
2672 * __root_device_register - allocate and register a root device
2673 * @name: root device name
2674 * @owner: owner module of the root device, usually THIS_MODULE
2676 * This function allocates a root device and registers it
2677 * using device_register(). In order to free the returned
2678 * device, use root_device_unregister().
2680 * Root devices are dummy devices which allow other devices
2681 * to be grouped under /sys/devices. Use this function to
2682 * allocate a root device and then use it as the parent of
2683 * any device which should appear under /sys/devices/{name}
2685 * The /sys/devices/{name} directory will also contain a
2686 * 'module' symlink which points to the @owner directory
2689 * Returns &struct device pointer on success, or ERR_PTR() on error.
2691 * Note: You probably want to use root_device_register().
2693 struct device *__root_device_register(const char *name, struct module *owner)
2695 struct root_device *root;
2698 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
2700 return ERR_PTR(err);
2702 err = dev_set_name(&root->dev, "%s", name);
2705 return ERR_PTR(err);
2708 root->dev.release = root_device_release;
2710 err = device_register(&root->dev);
2712 put_device(&root->dev);
2713 return ERR_PTR(err);
2716 #ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
2718 struct module_kobject *mk = &owner->mkobj;
2720 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
2722 device_unregister(&root->dev);
2723 return ERR_PTR(err);
2725 root->owner = owner;
2731 EXPORT_SYMBOL_GPL(__root_device_register);
2734 * root_device_unregister - unregister and free a root device
2735 * @dev: device going away
2737 * This function unregisters and cleans up a device that was created by
2738 * root_device_register().
2740 void root_device_unregister(struct device *dev)
2742 struct root_device *root = to_root_device(dev);
2745 sysfs_remove_link(&root->dev.kobj, "module");
2747 device_unregister(dev);
2749 EXPORT_SYMBOL_GPL(root_device_unregister);
2752 static void device_create_release(struct device *dev)
2754 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
2758 static __printf(6, 0) struct device *
2759 device_create_groups_vargs(struct class *class, struct device *parent,
2760 dev_t devt, void *drvdata,
2761 const struct attribute_group **groups,
2762 const char *fmt, va_list args)
2764 struct device *dev = NULL;
2765 int retval = -ENODEV;
2767 if (class == NULL || IS_ERR(class))
2770 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2776 device_initialize(dev);
2779 dev->parent = parent;
2780 dev->groups = groups;
2781 dev->release = device_create_release;
2782 dev_set_drvdata(dev, drvdata);
2784 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
2788 retval = device_add(dev);
2796 return ERR_PTR(retval);
2800 * device_create_vargs - creates a device and registers it with sysfs
2801 * @class: pointer to the struct class that this device should be registered to
2802 * @parent: pointer to the parent struct device of this new device, if any
2803 * @devt: the dev_t for the char device to be added
2804 * @drvdata: the data to be added to the device for callbacks
2805 * @fmt: string for the device's name
2806 * @args: va_list for the device's name
2808 * This function can be used by char device classes. A struct device
2809 * will be created in sysfs, registered to the specified class.
2811 * A "dev" file will be created, showing the dev_t for the device, if
2812 * the dev_t is not 0,0.
2813 * If a pointer to a parent struct device is passed in, the newly created
2814 * struct device will be a child of that device in sysfs.
2815 * The pointer to the struct device will be returned from the call.
2816 * Any further sysfs files that might be required can be created using this
2819 * Returns &struct device pointer on success, or ERR_PTR() on error.
2821 * Note: the struct class passed to this function must have previously
2822 * been created with a call to class_create().
2824 struct device *device_create_vargs(struct class *class, struct device *parent,
2825 dev_t devt, void *drvdata, const char *fmt,
2828 return device_create_groups_vargs(class, parent, devt, drvdata, NULL,
2831 EXPORT_SYMBOL_GPL(device_create_vargs);
2834 * device_create - creates a device and registers it with sysfs
2835 * @class: pointer to the struct class that this device should be registered to
2836 * @parent: pointer to the parent struct device of this new device, if any
2837 * @devt: the dev_t for the char device to be added
2838 * @drvdata: the data to be added to the device for callbacks
2839 * @fmt: string for the device's name
2841 * This function can be used by char device classes. A struct device
2842 * will be created in sysfs, registered to the specified class.
2844 * A "dev" file will be created, showing the dev_t for the device, if
2845 * the dev_t is not 0,0.
2846 * If a pointer to a parent struct device is passed in, the newly created
2847 * struct device will be a child of that device in sysfs.
2848 * The pointer to the struct device will be returned from the call.
2849 * Any further sysfs files that might be required can be created using this
2852 * Returns &struct device pointer on success, or ERR_PTR() on error.
2854 * Note: the struct class passed to this function must have previously
2855 * been created with a call to class_create().
2857 struct device *device_create(struct class *class, struct device *parent,
2858 dev_t devt, void *drvdata, const char *fmt, ...)
2863 va_start(vargs, fmt);
2864 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
2868 EXPORT_SYMBOL_GPL(device_create);
2871 * device_create_with_groups - creates a device and registers it with sysfs
2872 * @class: pointer to the struct class that this device should be registered to
2873 * @parent: pointer to the parent struct device of this new device, if any
2874 * @devt: the dev_t for the char device to be added
2875 * @drvdata: the data to be added to the device for callbacks
2876 * @groups: NULL-terminated list of attribute groups to be created
2877 * @fmt: string for the device's name
2879 * This function can be used by char device classes. A struct device
2880 * will be created in sysfs, registered to the specified class.
2881 * Additional attributes specified in the groups parameter will also
2882 * be created automatically.
2884 * A "dev" file will be created, showing the dev_t for the device, if
2885 * the dev_t is not 0,0.
2886 * If a pointer to a parent struct device is passed in, the newly created
2887 * struct device will be a child of that device in sysfs.
2888 * The pointer to the struct device will be returned from the call.
2889 * Any further sysfs files that might be required can be created using this
2892 * Returns &struct device pointer on success, or ERR_PTR() on error.
2894 * Note: the struct class passed to this function must have previously
2895 * been created with a call to class_create().
2897 struct device *device_create_with_groups(struct class *class,
2898 struct device *parent, dev_t devt,
2900 const struct attribute_group **groups,
2901 const char *fmt, ...)
2906 va_start(vargs, fmt);
2907 dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
2912 EXPORT_SYMBOL_GPL(device_create_with_groups);
2914 static int __match_devt(struct device *dev, const void *data)
2916 const dev_t *devt = data;
2918 return dev->devt == *devt;
2922 * device_destroy - removes a device that was created with device_create()
2923 * @class: pointer to the struct class that this device was registered with
2924 * @devt: the dev_t of the device that was previously registered
2926 * This call unregisters and cleans up a device that was created with a
2927 * call to device_create().
2929 void device_destroy(struct class *class, dev_t devt)
2933 dev = class_find_device(class, NULL, &devt, __match_devt);
2936 device_unregister(dev);
2939 EXPORT_SYMBOL_GPL(device_destroy);
2942 * device_rename - renames a device
2943 * @dev: the pointer to the struct device to be renamed
2944 * @new_name: the new name of the device
2946 * It is the responsibility of the caller to provide mutual
2947 * exclusion between two different calls of device_rename
2948 * on the same device to ensure that new_name is valid and
2949 * won't conflict with other devices.
2951 * Note: Don't call this function. Currently, the networking layer calls this
2952 * function, but that will change. The following text from Kay Sievers offers
2955 * Renaming devices is racy at many levels, symlinks and other stuff are not
2956 * replaced atomically, and you get a "move" uevent, but it's not easy to
2957 * connect the event to the old and new device. Device nodes are not renamed at
2958 * all, there isn't even support for that in the kernel now.
2960 * In the meantime, during renaming, your target name might be taken by another
2961 * driver, creating conflicts. Or the old name is taken directly after you
2962 * renamed it -- then you get events for the same DEVPATH, before you even see
2963 * the "move" event. It's just a mess, and nothing new should ever rely on
2964 * kernel device renaming. Besides that, it's not even implemented now for
2965 * other things than (driver-core wise very simple) network devices.
2967 * We are currently about to change network renaming in udev to completely
2968 * disallow renaming of devices in the same namespace as the kernel uses,
2969 * because we can't solve the problems properly, that arise with swapping names
2970 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
2971 * be allowed to some other name than eth[0-9]*, for the aforementioned
2974 * Make up a "real" name in the driver before you register anything, or add
2975 * some other attributes for userspace to find the device, or use udev to add
2976 * symlinks -- but never rename kernel devices later, it's a complete mess. We
2977 * don't even want to get into that and try to implement the missing pieces in
2978 * the core. We really have other pieces to fix in the driver core mess. :)
2980 int device_rename(struct device *dev, const char *new_name)
2982 struct kobject *kobj = &dev->kobj;
2983 char *old_device_name = NULL;
2986 dev = get_device(dev);
2990 dev_dbg(dev, "renaming to %s\n", new_name);
2992 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
2993 if (!old_device_name) {
2999 error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj,
3000 kobj, old_device_name,
3001 new_name, kobject_namespace(kobj));
3006 error = kobject_rename(kobj, new_name);
3013 kfree(old_device_name);
3017 EXPORT_SYMBOL_GPL(device_rename);
3019 static int device_move_class_links(struct device *dev,
3020 struct device *old_parent,
3021 struct device *new_parent)
3026 sysfs_remove_link(&dev->kobj, "device");
3028 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
3034 * device_move - moves a device to a new parent
3035 * @dev: the pointer to the struct device to be moved
3036 * @new_parent: the new parent of the device (can be NULL)
3037 * @dpm_order: how to reorder the dpm_list
3039 int device_move(struct device *dev, struct device *new_parent,
3040 enum dpm_order dpm_order)
3043 struct device *old_parent;
3044 struct kobject *new_parent_kobj;
3046 dev = get_device(dev);
3051 new_parent = get_device(new_parent);
3052 new_parent_kobj = get_device_parent(dev, new_parent);
3053 if (IS_ERR(new_parent_kobj)) {
3054 error = PTR_ERR(new_parent_kobj);
3055 put_device(new_parent);
3059 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
3060 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
3061 error = kobject_move(&dev->kobj, new_parent_kobj);
3063 cleanup_glue_dir(dev, new_parent_kobj);
3064 put_device(new_parent);
3067 old_parent = dev->parent;
3068 dev->parent = new_parent;
3070 klist_remove(&dev->p->knode_parent);
3072 klist_add_tail(&dev->p->knode_parent,
3073 &new_parent->p->klist_children);
3074 set_dev_node(dev, dev_to_node(new_parent));
3078 error = device_move_class_links(dev, old_parent, new_parent);
3080 /* We ignore errors on cleanup since we're hosed anyway... */
3081 device_move_class_links(dev, new_parent, old_parent);
3082 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
3084 klist_remove(&dev->p->knode_parent);
3085 dev->parent = old_parent;
3087 klist_add_tail(&dev->p->knode_parent,
3088 &old_parent->p->klist_children);
3089 set_dev_node(dev, dev_to_node(old_parent));
3092 cleanup_glue_dir(dev, new_parent_kobj);
3093 put_device(new_parent);
3097 switch (dpm_order) {
3098 case DPM_ORDER_NONE:
3100 case DPM_ORDER_DEV_AFTER_PARENT:
3101 device_pm_move_after(dev, new_parent);
3102 devices_kset_move_after(dev, new_parent);
3104 case DPM_ORDER_PARENT_BEFORE_DEV:
3105 device_pm_move_before(new_parent, dev);
3106 devices_kset_move_before(new_parent, dev);
3108 case DPM_ORDER_DEV_LAST:
3109 device_pm_move_last(dev);
3110 devices_kset_move_last(dev);
3114 put_device(old_parent);
3120 EXPORT_SYMBOL_GPL(device_move);
3123 * device_shutdown - call ->shutdown() on each device to shutdown.
3125 void device_shutdown(void)
3127 struct device *dev, *parent;
3129 wait_for_device_probe();
3130 device_block_probing();
3134 spin_lock(&devices_kset->list_lock);
3136 * Walk the devices list backward, shutting down each in turn.
3137 * Beware that device unplug events may also start pulling
3138 * devices offline, even as the system is shutting down.
3140 while (!list_empty(&devices_kset->list)) {
3141 dev = list_entry(devices_kset->list.prev, struct device,
3145 * hold reference count of device's parent to
3146 * prevent it from being freed because parent's
3147 * lock is to be held
3149 parent = get_device(dev->parent);
3152 * Make sure the device is off the kset list, in the
3153 * event that dev->*->shutdown() doesn't remove it.
3155 list_del_init(&dev->kobj.entry);
3156 spin_unlock(&devices_kset->list_lock);
3158 /* hold lock to avoid race with probe/release */
3160 device_lock(parent);
3163 /* Don't allow any more runtime suspends */
3164 pm_runtime_get_noresume(dev);
3165 pm_runtime_barrier(dev);
3167 if (dev->class && dev->class->shutdown_pre) {
3169 dev_info(dev, "shutdown_pre\n");
3170 dev->class->shutdown_pre(dev);
3172 if (dev->bus && dev->bus->shutdown) {
3174 dev_info(dev, "shutdown\n");
3175 dev->bus->shutdown(dev);
3176 } else if (dev->driver && dev->driver->shutdown) {
3178 dev_info(dev, "shutdown\n");
3179 dev->driver->shutdown(dev);
3184 device_unlock(parent);
3189 spin_lock(&devices_kset->list_lock);
3191 spin_unlock(&devices_kset->list_lock);
3195 * Device logging functions
3198 #ifdef CONFIG_PRINTK
3200 create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
3206 subsys = dev->class->name;
3208 subsys = dev->bus->name;
3212 pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys);
3217 * Add device identifier DEVICE=:
3221 * +sound:card0 subsystem:devname
3223 if (MAJOR(dev->devt)) {
3226 if (strcmp(subsys, "block") == 0)
3231 pos += snprintf(hdr + pos, hdrlen - pos,
3233 c, MAJOR(dev->devt), MINOR(dev->devt));
3234 } else if (strcmp(subsys, "net") == 0) {
3235 struct net_device *net = to_net_dev(dev);
3238 pos += snprintf(hdr + pos, hdrlen - pos,
3239 "DEVICE=n%u", net->ifindex);
3242 pos += snprintf(hdr + pos, hdrlen - pos,
3243 "DEVICE=+%s:%s", subsys, dev_name(dev));
3252 dev_WARN(dev, "device/subsystem name too long");
3256 int dev_vprintk_emit(int level, const struct device *dev,
3257 const char *fmt, va_list args)
3262 hdrlen = create_syslog_header(dev, hdr, sizeof(hdr));
3264 return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args);
3266 EXPORT_SYMBOL(dev_vprintk_emit);
3268 int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
3273 va_start(args, fmt);
3275 r = dev_vprintk_emit(level, dev, fmt, args);
3281 EXPORT_SYMBOL(dev_printk_emit);
3283 static void __dev_printk(const char *level, const struct device *dev,
3284 struct va_format *vaf)
3287 dev_printk_emit(level[1] - '0', dev, "%s %s: %pV",
3288 dev_driver_string(dev), dev_name(dev), vaf);
3290 printk("%s(NULL device *): %pV", level, vaf);
3293 void dev_printk(const char *level, const struct device *dev,
3294 const char *fmt, ...)
3296 struct va_format vaf;
3299 va_start(args, fmt);
3304 __dev_printk(level, dev, &vaf);
3308 EXPORT_SYMBOL(dev_printk);
3310 #define define_dev_printk_level(func, kern_level) \
3311 void func(const struct device *dev, const char *fmt, ...) \
3313 struct va_format vaf; \
3316 va_start(args, fmt); \
3321 __dev_printk(kern_level, dev, &vaf); \
3325 EXPORT_SYMBOL(func);
3327 define_dev_printk_level(_dev_emerg, KERN_EMERG);
3328 define_dev_printk_level(_dev_alert, KERN_ALERT);
3329 define_dev_printk_level(_dev_crit, KERN_CRIT);
3330 define_dev_printk_level(_dev_err, KERN_ERR);
3331 define_dev_printk_level(_dev_warn, KERN_WARNING);
3332 define_dev_printk_level(_dev_notice, KERN_NOTICE);
3333 define_dev_printk_level(_dev_info, KERN_INFO);
3337 static inline bool fwnode_is_primary(struct fwnode_handle *fwnode)
3339 return fwnode && !IS_ERR(fwnode->secondary);
3343 * set_primary_fwnode - Change the primary firmware node of a given device.
3344 * @dev: Device to handle.
3345 * @fwnode: New primary firmware node of the device.
3347 * Set the device's firmware node pointer to @fwnode, but if a secondary
3348 * firmware node of the device is present, preserve it.
3350 void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
3352 struct device *parent = dev->parent;
3353 struct fwnode_handle *fn = dev->fwnode;
3356 if (fwnode_is_primary(fn))
3360 WARN_ON(fwnode->secondary);
3361 fwnode->secondary = fn;
3363 dev->fwnode = fwnode;
3365 if (fwnode_is_primary(fn)) {
3366 dev->fwnode = fn->secondary;
3367 if (!(parent && fn == parent->fwnode))
3368 fn->secondary = NULL;
3374 EXPORT_SYMBOL_GPL(set_primary_fwnode);
3377 * set_secondary_fwnode - Change the secondary firmware node of a given device.
3378 * @dev: Device to handle.
3379 * @fwnode: New secondary firmware node of the device.
3381 * If a primary firmware node of the device is present, set its secondary
3382 * pointer to @fwnode. Otherwise, set the device's firmware node pointer to
3385 void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
3388 fwnode->secondary = ERR_PTR(-ENODEV);
3390 if (fwnode_is_primary(dev->fwnode))
3391 dev->fwnode->secondary = fwnode;
3393 dev->fwnode = fwnode;
3397 * device_set_of_node_from_dev - reuse device-tree node of another device
3398 * @dev: device whose device-tree node is being set
3399 * @dev2: device whose device-tree node is being reused
3401 * Takes another reference to the new device-tree node after first dropping
3402 * any reference held to the old node.
3404 void device_set_of_node_from_dev(struct device *dev, const struct device *dev2)
3406 of_node_put(dev->of_node);
3407 dev->of_node = of_node_get(dev2->of_node);
3408 dev->of_node_reused = true;
3410 EXPORT_SYMBOL_GPL(device_set_of_node_from_dev);