1 // SPDX-License-Identifier: GPL-2.0
3 * drivers/base/core.c - core driver model code (device registration, etc)
5 * Copyright (c) 2002-3 Patrick Mochel
6 * Copyright (c) 2002-3 Open Source Development Labs
7 * Copyright (c) 2006 Greg Kroah-Hartman <gregkh@suse.de>
8 * Copyright (c) 2006 Novell, Inc.
11 #include <linux/acpi.h>
12 #include <linux/cpufreq.h>
13 #include <linux/device.h>
14 #include <linux/err.h>
15 #include <linux/fwnode.h>
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/string.h>
20 #include <linux/kdev_t.h>
21 #include <linux/notifier.h>
23 #include <linux/of_device.h>
24 #include <linux/genhd.h>
25 #include <linux/mutex.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/netdevice.h>
28 #include <linux/sched/signal.h>
29 #include <linux/sysfs.h>
32 #include "power/power.h"
34 #ifdef CONFIG_SYSFS_DEPRECATED
35 #ifdef CONFIG_SYSFS_DEPRECATED_V2
36 long sysfs_deprecated = 1;
38 long sysfs_deprecated = 0;
40 static int __init sysfs_deprecated_setup(char *arg)
42 return kstrtol(arg, 10, &sysfs_deprecated);
44 early_param("sysfs.deprecated", sysfs_deprecated_setup);
47 /* Device links support. */
50 static DEFINE_MUTEX(device_links_lock);
51 DEFINE_STATIC_SRCU(device_links_srcu);
53 static inline void device_links_write_lock(void)
55 mutex_lock(&device_links_lock);
58 static inline void device_links_write_unlock(void)
60 mutex_unlock(&device_links_lock);
63 int device_links_read_lock(void)
65 return srcu_read_lock(&device_links_srcu);
68 void device_links_read_unlock(int idx)
70 srcu_read_unlock(&device_links_srcu, idx);
73 int device_links_read_lock_held(void)
75 return srcu_read_lock_held(&device_links_srcu);
77 #else /* !CONFIG_SRCU */
78 static DECLARE_RWSEM(device_links_lock);
80 static inline void device_links_write_lock(void)
82 down_write(&device_links_lock);
85 static inline void device_links_write_unlock(void)
87 up_write(&device_links_lock);
90 int device_links_read_lock(void)
92 down_read(&device_links_lock);
96 void device_links_read_unlock(int not_used)
98 up_read(&device_links_lock);
101 #ifdef CONFIG_DEBUG_LOCK_ALLOC
102 int device_links_read_lock_held(void)
104 return lockdep_is_held(&device_links_lock);
107 #endif /* !CONFIG_SRCU */
109 static bool device_is_ancestor(struct device *dev, struct device *target)
111 while (target->parent) {
112 target = target->parent;
120 * device_is_dependent - Check if one device depends on another one
121 * @dev: Device to check dependencies for.
122 * @target: Device to check against.
124 * Check if @target depends on @dev or any device dependent on it (its child or
125 * its consumer etc). Return 1 if that is the case or 0 otherwise.
127 static int device_is_dependent(struct device *dev, void *target)
129 struct device_link *link;
133 * The "ancestors" check is needed to catch the case when the target
134 * device has not been completely initialized yet and it is still
135 * missing from the list of children of its parent device.
137 if (dev == target || device_is_ancestor(dev, target))
140 ret = device_for_each_child(dev, target, device_is_dependent);
144 list_for_each_entry(link, &dev->links.consumers, s_node) {
145 if (link->consumer == target)
148 ret = device_is_dependent(link->consumer, target);
155 static void device_link_init_status(struct device_link *link,
156 struct device *consumer,
157 struct device *supplier)
159 switch (supplier->links.status) {
161 switch (consumer->links.status) {
164 * A consumer driver can create a link to a supplier
165 * that has not completed its probing yet as long as it
166 * knows that the supplier is already functional (for
167 * example, it has just acquired some resources from the
170 link->status = DL_STATE_CONSUMER_PROBE;
173 link->status = DL_STATE_DORMANT;
177 case DL_DEV_DRIVER_BOUND:
178 switch (consumer->links.status) {
180 link->status = DL_STATE_CONSUMER_PROBE;
182 case DL_DEV_DRIVER_BOUND:
183 link->status = DL_STATE_ACTIVE;
186 link->status = DL_STATE_AVAILABLE;
190 case DL_DEV_UNBINDING:
191 link->status = DL_STATE_SUPPLIER_UNBIND;
194 link->status = DL_STATE_DORMANT;
199 static int device_reorder_to_tail(struct device *dev, void *not_used)
201 struct device_link *link;
204 * Devices that have not been registered yet will be put to the ends
205 * of the lists during the registration, so skip them here.
207 if (device_is_registered(dev))
208 devices_kset_move_last(dev);
210 if (device_pm_initialized(dev))
211 device_pm_move_last(dev);
213 device_for_each_child(dev, NULL, device_reorder_to_tail);
214 list_for_each_entry(link, &dev->links.consumers, s_node)
215 device_reorder_to_tail(link->consumer, NULL);
221 * device_pm_move_to_tail - Move set of devices to the end of device lists
222 * @dev: Device to move
224 * This is a device_reorder_to_tail() wrapper taking the requisite locks.
226 * It moves the @dev along with all of its children and all of its consumers
227 * to the ends of the device_kset and dpm_list, recursively.
229 void device_pm_move_to_tail(struct device *dev)
233 idx = device_links_read_lock();
235 device_reorder_to_tail(dev, NULL);
237 device_links_read_unlock(idx);
240 #define DL_MANAGED_LINK_FLAGS (DL_FLAG_AUTOREMOVE_CONSUMER | \
241 DL_FLAG_AUTOREMOVE_SUPPLIER | \
242 DL_FLAG_AUTOPROBE_CONSUMER)
244 #define DL_ADD_VALID_FLAGS (DL_MANAGED_LINK_FLAGS | DL_FLAG_STATELESS | \
245 DL_FLAG_PM_RUNTIME | DL_FLAG_RPM_ACTIVE)
248 * device_link_add - Create a link between two devices.
249 * @consumer: Consumer end of the link.
250 * @supplier: Supplier end of the link.
251 * @flags: Link flags.
253 * The caller is responsible for the proper synchronization of the link creation
254 * with runtime PM. First, setting the DL_FLAG_PM_RUNTIME flag will cause the
255 * runtime PM framework to take the link into account. Second, if the
256 * DL_FLAG_RPM_ACTIVE flag is set in addition to it, the supplier devices will
257 * be forced into the active metastate and reference-counted upon the creation
258 * of the link. If DL_FLAG_PM_RUNTIME is not set, DL_FLAG_RPM_ACTIVE will be
261 * If DL_FLAG_STATELESS is set in @flags, the caller of this function is
262 * expected to release the link returned by it directly with the help of either
263 * device_link_del() or device_link_remove().
265 * If that flag is not set, however, the caller of this function is handing the
266 * management of the link over to the driver core entirely and its return value
267 * can only be used to check whether or not the link is present. In that case,
268 * the DL_FLAG_AUTOREMOVE_CONSUMER and DL_FLAG_AUTOREMOVE_SUPPLIER device link
269 * flags can be used to indicate to the driver core when the link can be safely
270 * deleted. Namely, setting one of them in @flags indicates to the driver core
271 * that the link is not going to be used (by the given caller of this function)
272 * after unbinding the consumer or supplier driver, respectively, from its
273 * device, so the link can be deleted at that point. If none of them is set,
274 * the link will be maintained until one of the devices pointed to by it (either
275 * the consumer or the supplier) is unregistered.
277 * Also, if DL_FLAG_STATELESS, DL_FLAG_AUTOREMOVE_CONSUMER and
278 * DL_FLAG_AUTOREMOVE_SUPPLIER are not set in @flags (that is, a persistent
279 * managed device link is being added), the DL_FLAG_AUTOPROBE_CONSUMER flag can
280 * be used to request the driver core to automaticall probe for a consmer
281 * driver after successfully binding a driver to the supplier device.
283 * The combination of DL_FLAG_STATELESS and one of DL_FLAG_AUTOREMOVE_CONSUMER,
284 * DL_FLAG_AUTOREMOVE_SUPPLIER, or DL_FLAG_AUTOPROBE_CONSUMER set in @flags at
285 * the same time is invalid and will cause NULL to be returned upfront.
286 * However, if a device link between the given @consumer and @supplier pair
287 * exists already when this function is called for them, the existing link will
288 * be returned regardless of its current type and status (the link's flags may
289 * be modified then). The caller of this function is then expected to treat
290 * the link as though it has just been created, so (in particular) if
291 * DL_FLAG_STATELESS was passed in @flags, the link needs to be released
292 * explicitly when not needed any more (as stated above).
294 * A side effect of the link creation is re-ordering of dpm_list and the
295 * devices_kset list by moving the consumer device and all devices depending
296 * on it to the ends of these lists (that does not happen to devices that have
297 * not been registered when this function is called).
299 * The supplier device is required to be registered when this function is called
300 * and NULL will be returned if that is not the case. The consumer device need
301 * not be registered, however.
303 struct device_link *device_link_add(struct device *consumer,
304 struct device *supplier, u32 flags)
306 struct device_link *link;
308 if (!consumer || !supplier || flags & ~DL_ADD_VALID_FLAGS ||
309 (flags & DL_FLAG_STATELESS && flags & DL_MANAGED_LINK_FLAGS) ||
310 (flags & DL_FLAG_AUTOPROBE_CONSUMER &&
311 flags & (DL_FLAG_AUTOREMOVE_CONSUMER |
312 DL_FLAG_AUTOREMOVE_SUPPLIER)))
315 if (flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) {
316 if (pm_runtime_get_sync(supplier) < 0) {
317 pm_runtime_put_noidle(supplier);
322 if (!(flags & DL_FLAG_STATELESS))
323 flags |= DL_FLAG_MANAGED;
325 device_links_write_lock();
329 * If the supplier has not been fully registered yet or there is a
330 * reverse dependency between the consumer and the supplier already in
331 * the graph, return NULL.
333 if (!device_pm_initialized(supplier)
334 || device_is_dependent(consumer, supplier)) {
340 * DL_FLAG_AUTOREMOVE_SUPPLIER indicates that the link will be needed
341 * longer than for DL_FLAG_AUTOREMOVE_CONSUMER and setting them both
342 * together doesn't make sense, so prefer DL_FLAG_AUTOREMOVE_SUPPLIER.
344 if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
345 flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER;
347 list_for_each_entry(link, &supplier->links.consumers, s_node) {
348 if (link->consumer != consumer)
351 if (flags & DL_FLAG_PM_RUNTIME) {
352 if (!(link->flags & DL_FLAG_PM_RUNTIME)) {
353 pm_runtime_new_link(consumer);
354 link->flags |= DL_FLAG_PM_RUNTIME;
356 if (flags & DL_FLAG_RPM_ACTIVE)
357 refcount_inc(&link->rpm_active);
360 if (flags & DL_FLAG_STATELESS) {
361 link->flags |= DL_FLAG_STATELESS;
362 kref_get(&link->kref);
367 * If the life time of the link following from the new flags is
368 * longer than indicated by the flags of the existing link,
369 * update the existing link to stay around longer.
371 if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER) {
372 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER) {
373 link->flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER;
374 link->flags |= DL_FLAG_AUTOREMOVE_SUPPLIER;
376 } else if (!(flags & DL_FLAG_AUTOREMOVE_CONSUMER)) {
377 link->flags &= ~(DL_FLAG_AUTOREMOVE_CONSUMER |
378 DL_FLAG_AUTOREMOVE_SUPPLIER);
380 if (!(link->flags & DL_FLAG_MANAGED)) {
381 kref_get(&link->kref);
382 link->flags |= DL_FLAG_MANAGED;
383 device_link_init_status(link, consumer, supplier);
388 link = kzalloc(sizeof(*link), GFP_KERNEL);
392 refcount_set(&link->rpm_active, 1);
394 if (flags & DL_FLAG_PM_RUNTIME) {
395 if (flags & DL_FLAG_RPM_ACTIVE)
396 refcount_inc(&link->rpm_active);
398 pm_runtime_new_link(consumer);
401 get_device(supplier);
402 link->supplier = supplier;
403 INIT_LIST_HEAD(&link->s_node);
404 get_device(consumer);
405 link->consumer = consumer;
406 INIT_LIST_HEAD(&link->c_node);
408 kref_init(&link->kref);
410 /* Determine the initial link state. */
411 if (flags & DL_FLAG_STATELESS)
412 link->status = DL_STATE_NONE;
414 device_link_init_status(link, consumer, supplier);
417 * Some callers expect the link creation during consumer driver probe to
418 * resume the supplier even without DL_FLAG_RPM_ACTIVE.
420 if (link->status == DL_STATE_CONSUMER_PROBE &&
421 flags & DL_FLAG_PM_RUNTIME)
422 pm_runtime_resume(supplier);
425 * Move the consumer and all of the devices depending on it to the end
426 * of dpm_list and the devices_kset list.
428 * It is necessary to hold dpm_list locked throughout all that or else
429 * we may end up suspending with a wrong ordering of it.
431 device_reorder_to_tail(consumer, NULL);
433 list_add_tail_rcu(&link->s_node, &supplier->links.consumers);
434 list_add_tail_rcu(&link->c_node, &consumer->links.suppliers);
436 dev_dbg(consumer, "Linked as a consumer to %s\n", dev_name(supplier));
440 device_links_write_unlock();
442 if ((flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) && !link)
443 pm_runtime_put(supplier);
447 EXPORT_SYMBOL_GPL(device_link_add);
449 static void device_link_free(struct device_link *link)
451 while (refcount_dec_not_one(&link->rpm_active))
452 pm_runtime_put(link->supplier);
454 put_device(link->consumer);
455 put_device(link->supplier);
460 static void __device_link_free_srcu(struct rcu_head *rhead)
462 device_link_free(container_of(rhead, struct device_link, rcu_head));
465 static void __device_link_del(struct kref *kref)
467 struct device_link *link = container_of(kref, struct device_link, kref);
469 dev_dbg(link->consumer, "Dropping the link to %s\n",
470 dev_name(link->supplier));
472 pm_runtime_drop_link(link);
474 list_del_rcu(&link->s_node);
475 list_del_rcu(&link->c_node);
476 call_srcu(&device_links_srcu, &link->rcu_head, __device_link_free_srcu);
478 #else /* !CONFIG_SRCU */
479 static void __device_link_del(struct kref *kref)
481 struct device_link *link = container_of(kref, struct device_link, kref);
483 dev_info(link->consumer, "Dropping the link to %s\n",
484 dev_name(link->supplier));
486 pm_runtime_drop_link(link);
488 list_del(&link->s_node);
489 list_del(&link->c_node);
490 device_link_free(link);
492 #endif /* !CONFIG_SRCU */
494 static void device_link_put_kref(struct device_link *link)
496 if (link->flags & DL_FLAG_STATELESS)
497 kref_put(&link->kref, __device_link_del);
499 WARN(1, "Unable to drop a managed device link reference\n");
503 * device_link_del - Delete a stateless link between two devices.
504 * @link: Device link to delete.
506 * The caller must ensure proper synchronization of this function with runtime
507 * PM. If the link was added multiple times, it needs to be deleted as often.
508 * Care is required for hotplugged devices: Their links are purged on removal
509 * and calling device_link_del() is then no longer allowed.
511 void device_link_del(struct device_link *link)
513 device_links_write_lock();
515 device_link_put_kref(link);
517 device_links_write_unlock();
519 EXPORT_SYMBOL_GPL(device_link_del);
522 * device_link_remove - Delete a stateless link between two devices.
523 * @consumer: Consumer end of the link.
524 * @supplier: Supplier end of the link.
526 * The caller must ensure proper synchronization of this function with runtime
529 void device_link_remove(void *consumer, struct device *supplier)
531 struct device_link *link;
533 if (WARN_ON(consumer == supplier))
536 device_links_write_lock();
539 list_for_each_entry(link, &supplier->links.consumers, s_node) {
540 if (link->consumer == consumer) {
541 device_link_put_kref(link);
547 device_links_write_unlock();
549 EXPORT_SYMBOL_GPL(device_link_remove);
551 static void device_links_missing_supplier(struct device *dev)
553 struct device_link *link;
555 list_for_each_entry(link, &dev->links.suppliers, c_node)
556 if (link->status == DL_STATE_CONSUMER_PROBE)
557 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
561 * device_links_check_suppliers - Check presence of supplier drivers.
562 * @dev: Consumer device.
564 * Check links from this device to any suppliers. Walk the list of the device's
565 * links to suppliers and see if all of them are available. If not, simply
566 * return -EPROBE_DEFER.
568 * We need to guarantee that the supplier will not go away after the check has
569 * been positive here. It only can go away in __device_release_driver() and
570 * that function checks the device's links to consumers. This means we need to
571 * mark the link as "consumer probe in progress" to make the supplier removal
572 * wait for us to complete (or bad things may happen).
574 * Links without the DL_FLAG_MANAGED flag set are ignored.
576 int device_links_check_suppliers(struct device *dev)
578 struct device_link *link;
581 device_links_write_lock();
583 list_for_each_entry(link, &dev->links.suppliers, c_node) {
584 if (!(link->flags & DL_FLAG_MANAGED))
587 if (link->status != DL_STATE_AVAILABLE) {
588 device_links_missing_supplier(dev);
592 WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE);
594 dev->links.status = DL_DEV_PROBING;
596 device_links_write_unlock();
601 * device_links_driver_bound - Update device links after probing its driver.
602 * @dev: Device to update the links for.
604 * The probe has been successful, so update links from this device to any
605 * consumers by changing their status to "available".
607 * Also change the status of @dev's links to suppliers to "active".
609 * Links without the DL_FLAG_MANAGED flag set are ignored.
611 void device_links_driver_bound(struct device *dev)
613 struct device_link *link;
615 device_links_write_lock();
617 list_for_each_entry(link, &dev->links.consumers, s_node) {
618 if (!(link->flags & DL_FLAG_MANAGED))
622 * Links created during consumer probe may be in the "consumer
623 * probe" state to start with if the supplier is still probing
624 * when they are created and they may become "active" if the
625 * consumer probe returns first. Skip them here.
627 if (link->status == DL_STATE_CONSUMER_PROBE ||
628 link->status == DL_STATE_ACTIVE)
631 WARN_ON(link->status != DL_STATE_DORMANT);
632 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
634 if (link->flags & DL_FLAG_AUTOPROBE_CONSUMER)
635 driver_deferred_probe_add(link->consumer);
638 list_for_each_entry(link, &dev->links.suppliers, c_node) {
639 if (!(link->flags & DL_FLAG_MANAGED))
642 WARN_ON(link->status != DL_STATE_CONSUMER_PROBE);
643 WRITE_ONCE(link->status, DL_STATE_ACTIVE);
646 dev->links.status = DL_DEV_DRIVER_BOUND;
648 device_links_write_unlock();
651 static void device_link_drop_managed(struct device_link *link)
653 link->flags &= ~DL_FLAG_MANAGED;
654 WRITE_ONCE(link->status, DL_STATE_NONE);
655 kref_put(&link->kref, __device_link_del);
659 * __device_links_no_driver - Update links of a device without a driver.
660 * @dev: Device without a drvier.
662 * Delete all non-persistent links from this device to any suppliers.
664 * Persistent links stay around, but their status is changed to "available",
665 * unless they already are in the "supplier unbind in progress" state in which
666 * case they need not be updated.
668 * Links without the DL_FLAG_MANAGED flag set are ignored.
670 static void __device_links_no_driver(struct device *dev)
672 struct device_link *link, *ln;
674 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
675 if (!(link->flags & DL_FLAG_MANAGED))
678 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER)
679 device_link_drop_managed(link);
680 else if (link->status == DL_STATE_CONSUMER_PROBE ||
681 link->status == DL_STATE_ACTIVE)
682 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
685 dev->links.status = DL_DEV_NO_DRIVER;
689 * device_links_no_driver - Update links after failing driver probe.
690 * @dev: Device whose driver has just failed to probe.
692 * Clean up leftover links to consumers for @dev and invoke
693 * %__device_links_no_driver() to update links to suppliers for it as
696 * Links without the DL_FLAG_MANAGED flag set are ignored.
698 void device_links_no_driver(struct device *dev)
700 struct device_link *link;
702 device_links_write_lock();
704 list_for_each_entry(link, &dev->links.consumers, s_node) {
705 if (!(link->flags & DL_FLAG_MANAGED))
709 * The probe has failed, so if the status of the link is
710 * "consumer probe" or "active", it must have been added by
711 * a probing consumer while this device was still probing.
712 * Change its state to "dormant", as it represents a valid
713 * relationship, but it is not functionally meaningful.
715 if (link->status == DL_STATE_CONSUMER_PROBE ||
716 link->status == DL_STATE_ACTIVE)
717 WRITE_ONCE(link->status, DL_STATE_DORMANT);
720 __device_links_no_driver(dev);
722 device_links_write_unlock();
726 * device_links_driver_cleanup - Update links after driver removal.
727 * @dev: Device whose driver has just gone away.
729 * Update links to consumers for @dev by changing their status to "dormant" and
730 * invoke %__device_links_no_driver() to update links to suppliers for it as
733 * Links without the DL_FLAG_MANAGED flag set are ignored.
735 void device_links_driver_cleanup(struct device *dev)
737 struct device_link *link, *ln;
739 device_links_write_lock();
741 list_for_each_entry_safe(link, ln, &dev->links.consumers, s_node) {
742 if (!(link->flags & DL_FLAG_MANAGED))
745 WARN_ON(link->flags & DL_FLAG_AUTOREMOVE_CONSUMER);
746 WARN_ON(link->status != DL_STATE_SUPPLIER_UNBIND);
749 * autoremove the links between this @dev and its consumer
750 * devices that are not active, i.e. where the link state
751 * has moved to DL_STATE_SUPPLIER_UNBIND.
753 if (link->status == DL_STATE_SUPPLIER_UNBIND &&
754 link->flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
755 device_link_drop_managed(link);
757 WRITE_ONCE(link->status, DL_STATE_DORMANT);
760 __device_links_no_driver(dev);
762 device_links_write_unlock();
766 * device_links_busy - Check if there are any busy links to consumers.
767 * @dev: Device to check.
769 * Check each consumer of the device and return 'true' if its link's status
770 * is one of "consumer probe" or "active" (meaning that the given consumer is
771 * probing right now or its driver is present). Otherwise, change the link
772 * state to "supplier unbind" to prevent the consumer from being probed
773 * successfully going forward.
775 * Return 'false' if there are no probing or active consumers.
777 * Links without the DL_FLAG_MANAGED flag set are ignored.
779 bool device_links_busy(struct device *dev)
781 struct device_link *link;
784 device_links_write_lock();
786 list_for_each_entry(link, &dev->links.consumers, s_node) {
787 if (!(link->flags & DL_FLAG_MANAGED))
790 if (link->status == DL_STATE_CONSUMER_PROBE
791 || link->status == DL_STATE_ACTIVE) {
795 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
798 dev->links.status = DL_DEV_UNBINDING;
800 device_links_write_unlock();
805 * device_links_unbind_consumers - Force unbind consumers of the given device.
806 * @dev: Device to unbind the consumers of.
808 * Walk the list of links to consumers for @dev and if any of them is in the
809 * "consumer probe" state, wait for all device probes in progress to complete
812 * If that's not the case, change the status of the link to "supplier unbind"
813 * and check if the link was in the "active" state. If so, force the consumer
814 * driver to unbind and start over (the consumer will not re-probe as we have
815 * changed the state of the link already).
817 * Links without the DL_FLAG_MANAGED flag set are ignored.
819 void device_links_unbind_consumers(struct device *dev)
821 struct device_link *link;
824 device_links_write_lock();
826 list_for_each_entry(link, &dev->links.consumers, s_node) {
827 enum device_link_state status;
829 if (!(link->flags & DL_FLAG_MANAGED))
832 status = link->status;
833 if (status == DL_STATE_CONSUMER_PROBE) {
834 device_links_write_unlock();
836 wait_for_device_probe();
839 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
840 if (status == DL_STATE_ACTIVE) {
841 struct device *consumer = link->consumer;
843 get_device(consumer);
845 device_links_write_unlock();
847 device_release_driver_internal(consumer, NULL,
849 put_device(consumer);
854 device_links_write_unlock();
858 * device_links_purge - Delete existing links to other devices.
859 * @dev: Target device.
861 static void device_links_purge(struct device *dev)
863 struct device_link *link, *ln;
866 * Delete all of the remaining links from this device to any other
867 * devices (either consumers or suppliers).
869 device_links_write_lock();
871 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
872 WARN_ON(link->status == DL_STATE_ACTIVE);
873 __device_link_del(&link->kref);
876 list_for_each_entry_safe_reverse(link, ln, &dev->links.consumers, s_node) {
877 WARN_ON(link->status != DL_STATE_DORMANT &&
878 link->status != DL_STATE_NONE);
879 __device_link_del(&link->kref);
882 device_links_write_unlock();
885 /* Device links support end. */
887 int (*platform_notify)(struct device *dev) = NULL;
888 int (*platform_notify_remove)(struct device *dev) = NULL;
889 static struct kobject *dev_kobj;
890 struct kobject *sysfs_dev_char_kobj;
891 struct kobject *sysfs_dev_block_kobj;
893 static DEFINE_MUTEX(device_hotplug_lock);
895 void lock_device_hotplug(void)
897 mutex_lock(&device_hotplug_lock);
900 void unlock_device_hotplug(void)
902 mutex_unlock(&device_hotplug_lock);
905 int lock_device_hotplug_sysfs(void)
907 if (mutex_trylock(&device_hotplug_lock))
910 /* Avoid busy looping (5 ms of sleep should do). */
912 return restart_syscall();
916 static inline int device_is_not_partition(struct device *dev)
918 return !(dev->type == &part_type);
921 static inline int device_is_not_partition(struct device *dev)
928 device_platform_notify(struct device *dev, enum kobject_action action)
932 ret = acpi_platform_notify(dev, action);
936 ret = software_node_notify(dev, action);
940 if (platform_notify && action == KOBJ_ADD)
941 platform_notify(dev);
942 else if (platform_notify_remove && action == KOBJ_REMOVE)
943 platform_notify_remove(dev);
948 * dev_driver_string - Return a device's driver name, if at all possible
949 * @dev: struct device to get the name of
951 * Will return the device's driver's name if it is bound to a device. If
952 * the device is not bound to a driver, it will return the name of the bus
953 * it is attached to. If it is not attached to a bus either, an empty
954 * string will be returned.
956 const char *dev_driver_string(const struct device *dev)
958 struct device_driver *drv;
960 /* dev->driver can change to NULL underneath us because of unbinding,
961 * so be careful about accessing it. dev->bus and dev->class should
962 * never change once they are set, so they don't need special care.
964 drv = READ_ONCE(dev->driver);
965 return drv ? drv->name :
966 (dev->bus ? dev->bus->name :
967 (dev->class ? dev->class->name : ""));
969 EXPORT_SYMBOL(dev_driver_string);
971 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
973 static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
976 struct device_attribute *dev_attr = to_dev_attr(attr);
977 struct device *dev = kobj_to_dev(kobj);
981 ret = dev_attr->show(dev, dev_attr, buf);
982 if (ret >= (ssize_t)PAGE_SIZE) {
983 printk("dev_attr_show: %pS returned bad count\n",
989 static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
990 const char *buf, size_t count)
992 struct device_attribute *dev_attr = to_dev_attr(attr);
993 struct device *dev = kobj_to_dev(kobj);
997 ret = dev_attr->store(dev, dev_attr, buf, count);
1001 static const struct sysfs_ops dev_sysfs_ops = {
1002 .show = dev_attr_show,
1003 .store = dev_attr_store,
1006 #define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
1008 ssize_t device_store_ulong(struct device *dev,
1009 struct device_attribute *attr,
1010 const char *buf, size_t size)
1012 struct dev_ext_attribute *ea = to_ext_attr(attr);
1016 ret = kstrtoul(buf, 0, &new);
1019 *(unsigned long *)(ea->var) = new;
1020 /* Always return full write size even if we didn't consume all */
1023 EXPORT_SYMBOL_GPL(device_store_ulong);
1025 ssize_t device_show_ulong(struct device *dev,
1026 struct device_attribute *attr,
1029 struct dev_ext_attribute *ea = to_ext_attr(attr);
1030 return sysfs_emit(buf, "%lx\n", *(unsigned long *)(ea->var));
1032 EXPORT_SYMBOL_GPL(device_show_ulong);
1034 ssize_t device_store_int(struct device *dev,
1035 struct device_attribute *attr,
1036 const char *buf, size_t size)
1038 struct dev_ext_attribute *ea = to_ext_attr(attr);
1042 ret = kstrtol(buf, 0, &new);
1046 if (new > INT_MAX || new < INT_MIN)
1048 *(int *)(ea->var) = new;
1049 /* Always return full write size even if we didn't consume all */
1052 EXPORT_SYMBOL_GPL(device_store_int);
1054 ssize_t device_show_int(struct device *dev,
1055 struct device_attribute *attr,
1058 struct dev_ext_attribute *ea = to_ext_attr(attr);
1060 return sysfs_emit(buf, "%d\n", *(int *)(ea->var));
1062 EXPORT_SYMBOL_GPL(device_show_int);
1064 ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
1065 const char *buf, size_t size)
1067 struct dev_ext_attribute *ea = to_ext_attr(attr);
1069 if (strtobool(buf, ea->var) < 0)
1074 EXPORT_SYMBOL_GPL(device_store_bool);
1076 ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
1079 struct dev_ext_attribute *ea = to_ext_attr(attr);
1081 return sysfs_emit(buf, "%d\n", *(bool *)(ea->var));
1083 EXPORT_SYMBOL_GPL(device_show_bool);
1086 * device_release - free device structure.
1087 * @kobj: device's kobject.
1089 * This is called once the reference count for the object
1090 * reaches 0. We forward the call to the device's release
1091 * method, which should handle actually freeing the structure.
1093 static void device_release(struct kobject *kobj)
1095 struct device *dev = kobj_to_dev(kobj);
1096 struct device_private *p = dev->p;
1099 * Some platform devices are driven without driver attached
1100 * and managed resources may have been acquired. Make sure
1101 * all resources are released.
1103 * Drivers still can add resources into device after device
1104 * is deleted but alive, so release devres here to avoid
1105 * possible memory leak.
1107 devres_release_all(dev);
1111 else if (dev->type && dev->type->release)
1112 dev->type->release(dev);
1113 else if (dev->class && dev->class->dev_release)
1114 dev->class->dev_release(dev);
1116 WARN(1, KERN_ERR "Device '%s' does not have a release() function, it is broken and must be fixed. See Documentation/kobject.txt.\n",
1121 static const void *device_namespace(struct kobject *kobj)
1123 struct device *dev = kobj_to_dev(kobj);
1124 const void *ns = NULL;
1126 if (dev->class && dev->class->ns_type)
1127 ns = dev->class->namespace(dev);
1132 static void device_get_ownership(struct kobject *kobj, kuid_t *uid, kgid_t *gid)
1134 struct device *dev = kobj_to_dev(kobj);
1136 if (dev->class && dev->class->get_ownership)
1137 dev->class->get_ownership(dev, uid, gid);
1140 static struct kobj_type device_ktype = {
1141 .release = device_release,
1142 .sysfs_ops = &dev_sysfs_ops,
1143 .namespace = device_namespace,
1144 .get_ownership = device_get_ownership,
1148 static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
1150 struct kobj_type *ktype = get_ktype(kobj);
1152 if (ktype == &device_ktype) {
1153 struct device *dev = kobj_to_dev(kobj);
1162 static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
1164 struct device *dev = kobj_to_dev(kobj);
1167 return dev->bus->name;
1169 return dev->class->name;
1173 static int dev_uevent(struct kset *kset, struct kobject *kobj,
1174 struct kobj_uevent_env *env)
1176 struct device *dev = kobj_to_dev(kobj);
1179 /* add device node properties if present */
1180 if (MAJOR(dev->devt)) {
1184 kuid_t uid = GLOBAL_ROOT_UID;
1185 kgid_t gid = GLOBAL_ROOT_GID;
1187 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
1188 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
1189 name = device_get_devnode(dev, &mode, &uid, &gid, &tmp);
1191 add_uevent_var(env, "DEVNAME=%s", name);
1193 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
1194 if (!uid_eq(uid, GLOBAL_ROOT_UID))
1195 add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid));
1196 if (!gid_eq(gid, GLOBAL_ROOT_GID))
1197 add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid));
1202 if (dev->type && dev->type->name)
1203 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
1206 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
1208 /* Add common DT information about the device */
1209 of_device_uevent(dev, env);
1211 /* have the bus specific function add its stuff */
1212 if (dev->bus && dev->bus->uevent) {
1213 retval = dev->bus->uevent(dev, env);
1215 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
1216 dev_name(dev), __func__, retval);
1219 /* have the class specific function add its stuff */
1220 if (dev->class && dev->class->dev_uevent) {
1221 retval = dev->class->dev_uevent(dev, env);
1223 pr_debug("device: '%s': %s: class uevent() "
1224 "returned %d\n", dev_name(dev),
1228 /* have the device type specific function add its stuff */
1229 if (dev->type && dev->type->uevent) {
1230 retval = dev->type->uevent(dev, env);
1232 pr_debug("device: '%s': %s: dev_type uevent() "
1233 "returned %d\n", dev_name(dev),
1240 static const struct kset_uevent_ops device_uevent_ops = {
1241 .filter = dev_uevent_filter,
1242 .name = dev_uevent_name,
1243 .uevent = dev_uevent,
1246 static ssize_t uevent_show(struct device *dev, struct device_attribute *attr,
1249 struct kobject *top_kobj;
1251 struct kobj_uevent_env *env = NULL;
1256 /* search the kset, the device belongs to */
1257 top_kobj = &dev->kobj;
1258 while (!top_kobj->kset && top_kobj->parent)
1259 top_kobj = top_kobj->parent;
1260 if (!top_kobj->kset)
1263 kset = top_kobj->kset;
1264 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
1267 /* respect filter */
1268 if (kset->uevent_ops && kset->uevent_ops->filter)
1269 if (!kset->uevent_ops->filter(kset, &dev->kobj))
1272 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
1276 /* let the kset specific function add its keys */
1277 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
1281 /* copy keys to file */
1282 for (i = 0; i < env->envp_idx; i++)
1283 count += sprintf(&buf[count], "%s\n", env->envp[i]);
1289 static ssize_t uevent_store(struct device *dev, struct device_attribute *attr,
1290 const char *buf, size_t count)
1294 rc = kobject_synth_uevent(&dev->kobj, buf, count);
1297 dev_err(dev, "uevent: failed to send synthetic uevent\n");
1303 static DEVICE_ATTR_RW(uevent);
1305 static ssize_t online_show(struct device *dev, struct device_attribute *attr,
1311 val = !dev->offline;
1313 return sysfs_emit(buf, "%u\n", val);
1316 static ssize_t online_store(struct device *dev, struct device_attribute *attr,
1317 const char *buf, size_t count)
1322 ret = strtobool(buf, &val);
1326 ret = lock_device_hotplug_sysfs();
1330 ret = val ? device_online(dev) : device_offline(dev);
1331 unlock_device_hotplug();
1332 return ret < 0 ? ret : count;
1334 static DEVICE_ATTR_RW(online);
1336 int device_add_groups(struct device *dev, const struct attribute_group **groups)
1338 return sysfs_create_groups(&dev->kobj, groups);
1340 EXPORT_SYMBOL_GPL(device_add_groups);
1342 void device_remove_groups(struct device *dev,
1343 const struct attribute_group **groups)
1345 sysfs_remove_groups(&dev->kobj, groups);
1347 EXPORT_SYMBOL_GPL(device_remove_groups);
1349 union device_attr_group_devres {
1350 const struct attribute_group *group;
1351 const struct attribute_group **groups;
1354 static int devm_attr_group_match(struct device *dev, void *res, void *data)
1356 return ((union device_attr_group_devres *)res)->group == data;
1359 static void devm_attr_group_remove(struct device *dev, void *res)
1361 union device_attr_group_devres *devres = res;
1362 const struct attribute_group *group = devres->group;
1364 dev_dbg(dev, "%s: removing group %p\n", __func__, group);
1365 sysfs_remove_group(&dev->kobj, group);
1368 static void devm_attr_groups_remove(struct device *dev, void *res)
1370 union device_attr_group_devres *devres = res;
1371 const struct attribute_group **groups = devres->groups;
1373 dev_dbg(dev, "%s: removing groups %p\n", __func__, groups);
1374 sysfs_remove_groups(&dev->kobj, groups);
1378 * devm_device_add_group - given a device, create a managed attribute group
1379 * @dev: The device to create the group for
1380 * @grp: The attribute group to create
1382 * This function creates a group for the first time. It will explicitly
1383 * warn and error if any of the attribute files being created already exist.
1385 * Returns 0 on success or error code on failure.
1387 int devm_device_add_group(struct device *dev, const struct attribute_group *grp)
1389 union device_attr_group_devres *devres;
1392 devres = devres_alloc(devm_attr_group_remove,
1393 sizeof(*devres), GFP_KERNEL);
1397 error = sysfs_create_group(&dev->kobj, grp);
1399 devres_free(devres);
1403 devres->group = grp;
1404 devres_add(dev, devres);
1407 EXPORT_SYMBOL_GPL(devm_device_add_group);
1410 * devm_device_remove_group: remove a managed group from a device
1411 * @dev: device to remove the group from
1412 * @grp: group to remove
1414 * This function removes a group of attributes from a device. The attributes
1415 * previously have to have been created for this group, otherwise it will fail.
1417 void devm_device_remove_group(struct device *dev,
1418 const struct attribute_group *grp)
1420 WARN_ON(devres_release(dev, devm_attr_group_remove,
1421 devm_attr_group_match,
1422 /* cast away const */ (void *)grp));
1424 EXPORT_SYMBOL_GPL(devm_device_remove_group);
1427 * devm_device_add_groups - create a bunch of managed attribute groups
1428 * @dev: The device to create the group for
1429 * @groups: The attribute groups to create, NULL terminated
1431 * This function creates a bunch of managed attribute groups. If an error
1432 * occurs when creating a group, all previously created groups will be
1433 * removed, unwinding everything back to the original state when this
1434 * function was called. It will explicitly warn and error if any of the
1435 * attribute files being created already exist.
1437 * Returns 0 on success or error code from sysfs_create_group on failure.
1439 int devm_device_add_groups(struct device *dev,
1440 const struct attribute_group **groups)
1442 union device_attr_group_devres *devres;
1445 devres = devres_alloc(devm_attr_groups_remove,
1446 sizeof(*devres), GFP_KERNEL);
1450 error = sysfs_create_groups(&dev->kobj, groups);
1452 devres_free(devres);
1456 devres->groups = groups;
1457 devres_add(dev, devres);
1460 EXPORT_SYMBOL_GPL(devm_device_add_groups);
1463 * devm_device_remove_groups - remove a list of managed groups
1465 * @dev: The device for the groups to be removed from
1466 * @groups: NULL terminated list of groups to be removed
1468 * If groups is not NULL, remove the specified groups from the device.
1470 void devm_device_remove_groups(struct device *dev,
1471 const struct attribute_group **groups)
1473 WARN_ON(devres_release(dev, devm_attr_groups_remove,
1474 devm_attr_group_match,
1475 /* cast away const */ (void *)groups));
1477 EXPORT_SYMBOL_GPL(devm_device_remove_groups);
1479 static int device_add_attrs(struct device *dev)
1481 struct class *class = dev->class;
1482 const struct device_type *type = dev->type;
1486 error = device_add_groups(dev, class->dev_groups);
1492 error = device_add_groups(dev, type->groups);
1494 goto err_remove_class_groups;
1497 error = device_add_groups(dev, dev->groups);
1499 goto err_remove_type_groups;
1501 if (device_supports_offline(dev) && !dev->offline_disabled) {
1502 error = device_create_file(dev, &dev_attr_online);
1504 goto err_remove_dev_groups;
1509 err_remove_dev_groups:
1510 device_remove_groups(dev, dev->groups);
1511 err_remove_type_groups:
1513 device_remove_groups(dev, type->groups);
1514 err_remove_class_groups:
1516 device_remove_groups(dev, class->dev_groups);
1521 static void device_remove_attrs(struct device *dev)
1523 struct class *class = dev->class;
1524 const struct device_type *type = dev->type;
1526 device_remove_file(dev, &dev_attr_online);
1527 device_remove_groups(dev, dev->groups);
1530 device_remove_groups(dev, type->groups);
1533 device_remove_groups(dev, class->dev_groups);
1536 static ssize_t dev_show(struct device *dev, struct device_attribute *attr,
1539 return print_dev_t(buf, dev->devt);
1541 static DEVICE_ATTR_RO(dev);
1544 struct kset *devices_kset;
1547 * devices_kset_move_before - Move device in the devices_kset's list.
1548 * @deva: Device to move.
1549 * @devb: Device @deva should come before.
1551 static void devices_kset_move_before(struct device *deva, struct device *devb)
1555 pr_debug("devices_kset: Moving %s before %s\n",
1556 dev_name(deva), dev_name(devb));
1557 spin_lock(&devices_kset->list_lock);
1558 list_move_tail(&deva->kobj.entry, &devb->kobj.entry);
1559 spin_unlock(&devices_kset->list_lock);
1563 * devices_kset_move_after - Move device in the devices_kset's list.
1564 * @deva: Device to move
1565 * @devb: Device @deva should come after.
1567 static void devices_kset_move_after(struct device *deva, struct device *devb)
1571 pr_debug("devices_kset: Moving %s after %s\n",
1572 dev_name(deva), dev_name(devb));
1573 spin_lock(&devices_kset->list_lock);
1574 list_move(&deva->kobj.entry, &devb->kobj.entry);
1575 spin_unlock(&devices_kset->list_lock);
1579 * devices_kset_move_last - move the device to the end of devices_kset's list.
1580 * @dev: device to move
1582 void devices_kset_move_last(struct device *dev)
1586 pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev));
1587 spin_lock(&devices_kset->list_lock);
1588 list_move_tail(&dev->kobj.entry, &devices_kset->list);
1589 spin_unlock(&devices_kset->list_lock);
1593 * device_create_file - create sysfs attribute file for device.
1595 * @attr: device attribute descriptor.
1597 int device_create_file(struct device *dev,
1598 const struct device_attribute *attr)
1603 WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
1604 "Attribute %s: write permission without 'store'\n",
1606 WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
1607 "Attribute %s: read permission without 'show'\n",
1609 error = sysfs_create_file(&dev->kobj, &attr->attr);
1614 EXPORT_SYMBOL_GPL(device_create_file);
1617 * device_remove_file - remove sysfs attribute file.
1619 * @attr: device attribute descriptor.
1621 void device_remove_file(struct device *dev,
1622 const struct device_attribute *attr)
1625 sysfs_remove_file(&dev->kobj, &attr->attr);
1627 EXPORT_SYMBOL_GPL(device_remove_file);
1630 * device_remove_file_self - remove sysfs attribute file from its own method.
1632 * @attr: device attribute descriptor.
1634 * See kernfs_remove_self() for details.
1636 bool device_remove_file_self(struct device *dev,
1637 const struct device_attribute *attr)
1640 return sysfs_remove_file_self(&dev->kobj, &attr->attr);
1644 EXPORT_SYMBOL_GPL(device_remove_file_self);
1647 * device_create_bin_file - create sysfs binary attribute file for device.
1649 * @attr: device binary attribute descriptor.
1651 int device_create_bin_file(struct device *dev,
1652 const struct bin_attribute *attr)
1654 int error = -EINVAL;
1656 error = sysfs_create_bin_file(&dev->kobj, attr);
1659 EXPORT_SYMBOL_GPL(device_create_bin_file);
1662 * device_remove_bin_file - remove sysfs binary attribute file
1664 * @attr: device binary attribute descriptor.
1666 void device_remove_bin_file(struct device *dev,
1667 const struct bin_attribute *attr)
1670 sysfs_remove_bin_file(&dev->kobj, attr);
1672 EXPORT_SYMBOL_GPL(device_remove_bin_file);
1674 static void klist_children_get(struct klist_node *n)
1676 struct device_private *p = to_device_private_parent(n);
1677 struct device *dev = p->device;
1682 static void klist_children_put(struct klist_node *n)
1684 struct device_private *p = to_device_private_parent(n);
1685 struct device *dev = p->device;
1691 * device_initialize - init device structure.
1694 * This prepares the device for use by other layers by initializing
1696 * It is the first half of device_register(), if called by
1697 * that function, though it can also be called separately, so one
1698 * may use @dev's fields. In particular, get_device()/put_device()
1699 * may be used for reference counting of @dev after calling this
1702 * All fields in @dev must be initialized by the caller to 0, except
1703 * for those explicitly set to some other value. The simplest
1704 * approach is to use kzalloc() to allocate the structure containing
1707 * NOTE: Use put_device() to give up your reference instead of freeing
1708 * @dev directly once you have called this function.
1710 void device_initialize(struct device *dev)
1712 dev->kobj.kset = devices_kset;
1713 kobject_init(&dev->kobj, &device_ktype);
1714 INIT_LIST_HEAD(&dev->dma_pools);
1715 mutex_init(&dev->mutex);
1716 #ifdef CONFIG_PROVE_LOCKING
1717 mutex_init(&dev->lockdep_mutex);
1719 lockdep_set_novalidate_class(&dev->mutex);
1720 spin_lock_init(&dev->devres_lock);
1721 INIT_LIST_HEAD(&dev->devres_head);
1722 device_pm_init(dev);
1723 set_dev_node(dev, -1);
1724 #ifdef CONFIG_GENERIC_MSI_IRQ
1725 raw_spin_lock_init(&dev->msi_lock);
1726 INIT_LIST_HEAD(&dev->msi_list);
1728 INIT_LIST_HEAD(&dev->links.consumers);
1729 INIT_LIST_HEAD(&dev->links.suppliers);
1730 dev->links.status = DL_DEV_NO_DRIVER;
1732 EXPORT_SYMBOL_GPL(device_initialize);
1734 struct kobject *virtual_device_parent(struct device *dev)
1736 static struct kobject *virtual_dir = NULL;
1739 virtual_dir = kobject_create_and_add("virtual",
1740 &devices_kset->kobj);
1746 struct kobject kobj;
1747 struct class *class;
1750 #define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
1752 static void class_dir_release(struct kobject *kobj)
1754 struct class_dir *dir = to_class_dir(kobj);
1759 struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
1761 struct class_dir *dir = to_class_dir(kobj);
1762 return dir->class->ns_type;
1765 static struct kobj_type class_dir_ktype = {
1766 .release = class_dir_release,
1767 .sysfs_ops = &kobj_sysfs_ops,
1768 .child_ns_type = class_dir_child_ns_type
1771 static struct kobject *
1772 class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
1774 struct class_dir *dir;
1777 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1779 return ERR_PTR(-ENOMEM);
1782 kobject_init(&dir->kobj, &class_dir_ktype);
1784 dir->kobj.kset = &class->p->glue_dirs;
1786 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
1788 kobject_put(&dir->kobj);
1789 return ERR_PTR(retval);
1794 static DEFINE_MUTEX(gdp_mutex);
1796 static struct kobject *get_device_parent(struct device *dev,
1797 struct device *parent)
1800 struct kobject *kobj = NULL;
1801 struct kobject *parent_kobj;
1805 /* block disks show up in /sys/block */
1806 if (sysfs_deprecated && dev->class == &block_class) {
1807 if (parent && parent->class == &block_class)
1808 return &parent->kobj;
1809 return &block_class.p->subsys.kobj;
1814 * If we have no parent, we live in "virtual".
1815 * Class-devices with a non class-device as parent, live
1816 * in a "glue" directory to prevent namespace collisions.
1819 parent_kobj = virtual_device_parent(dev);
1820 else if (parent->class && !dev->class->ns_type)
1821 return &parent->kobj;
1823 parent_kobj = &parent->kobj;
1825 mutex_lock(&gdp_mutex);
1827 /* find our class-directory at the parent and reference it */
1828 spin_lock(&dev->class->p->glue_dirs.list_lock);
1829 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
1830 if (k->parent == parent_kobj) {
1831 kobj = kobject_get(k);
1834 spin_unlock(&dev->class->p->glue_dirs.list_lock);
1836 mutex_unlock(&gdp_mutex);
1840 /* or create a new class-directory at the parent device */
1841 k = class_dir_create_and_add(dev->class, parent_kobj);
1842 /* do not emit an uevent for this simple "glue" directory */
1843 mutex_unlock(&gdp_mutex);
1847 /* subsystems can specify a default root directory for their devices */
1848 if (!parent && dev->bus && dev->bus->dev_root)
1849 return &dev->bus->dev_root->kobj;
1852 return &parent->kobj;
1856 static inline bool live_in_glue_dir(struct kobject *kobj,
1859 if (!kobj || !dev->class ||
1860 kobj->kset != &dev->class->p->glue_dirs)
1865 static inline struct kobject *get_glue_dir(struct device *dev)
1867 return dev->kobj.parent;
1871 * make sure cleaning up dir as the last step, we need to make
1872 * sure .release handler of kobject is run with holding the
1875 static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
1879 /* see if we live in a "glue" directory */
1880 if (!live_in_glue_dir(glue_dir, dev))
1883 mutex_lock(&gdp_mutex);
1885 * There is a race condition between removing glue directory
1886 * and adding a new device under the glue directory.
1891 * get_device_parent()
1892 * class_dir_create_and_add()
1893 * kobject_add_internal()
1894 * create_dir() // create glue_dir
1897 * get_device_parent()
1898 * kobject_get() // get glue_dir
1901 * cleanup_glue_dir()
1902 * kobject_del(glue_dir)
1905 * kobject_add_internal()
1906 * create_dir() // in glue_dir
1907 * sysfs_create_dir_ns()
1908 * kernfs_create_dir_ns(sd)
1910 * sysfs_remove_dir() // glue_dir->sd=NULL
1911 * sysfs_put() // free glue_dir->sd
1914 * kernfs_new_node(sd)
1915 * kernfs_get(glue_dir)
1919 * Before CPU1 remove last child device under glue dir, if CPU2 add
1920 * a new device under glue dir, the glue_dir kobject reference count
1921 * will be increase to 2 in kobject_get(k). And CPU2 has been called
1922 * kernfs_create_dir_ns(). Meanwhile, CPU1 call sysfs_remove_dir()
1923 * and sysfs_put(). This result in glue_dir->sd is freed.
1925 * Then the CPU2 will see a stale "empty" but still potentially used
1926 * glue dir around in kernfs_new_node().
1928 * In order to avoid this happening, we also should make sure that
1929 * kernfs_node for glue_dir is released in CPU1 only when refcount
1930 * for glue_dir kobj is 1.
1932 ref = kref_read(&glue_dir->kref);
1933 if (!kobject_has_children(glue_dir) && !--ref)
1934 kobject_del(glue_dir);
1935 kobject_put(glue_dir);
1936 mutex_unlock(&gdp_mutex);
1939 static int device_add_class_symlinks(struct device *dev)
1941 struct device_node *of_node = dev_of_node(dev);
1945 error = sysfs_create_link(&dev->kobj, of_node_kobj(of_node), "of_node");
1947 dev_warn(dev, "Error %d creating of_node link\n",error);
1948 /* An error here doesn't warrant bringing down the device */
1954 error = sysfs_create_link(&dev->kobj,
1955 &dev->class->p->subsys.kobj,
1960 if (dev->parent && device_is_not_partition(dev)) {
1961 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
1968 /* /sys/block has directories and does not need symlinks */
1969 if (sysfs_deprecated && dev->class == &block_class)
1973 /* link in the class directory pointing to the device */
1974 error = sysfs_create_link(&dev->class->p->subsys.kobj,
1975 &dev->kobj, dev_name(dev));
1982 sysfs_remove_link(&dev->kobj, "device");
1985 sysfs_remove_link(&dev->kobj, "subsystem");
1987 sysfs_remove_link(&dev->kobj, "of_node");
1991 static void device_remove_class_symlinks(struct device *dev)
1993 if (dev_of_node(dev))
1994 sysfs_remove_link(&dev->kobj, "of_node");
1999 if (dev->parent && device_is_not_partition(dev))
2000 sysfs_remove_link(&dev->kobj, "device");
2001 sysfs_remove_link(&dev->kobj, "subsystem");
2003 if (sysfs_deprecated && dev->class == &block_class)
2006 sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
2010 * dev_set_name - set a device name
2012 * @fmt: format string for the device's name
2014 int dev_set_name(struct device *dev, const char *fmt, ...)
2019 va_start(vargs, fmt);
2020 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
2024 EXPORT_SYMBOL_GPL(dev_set_name);
2027 * device_to_dev_kobj - select a /sys/dev/ directory for the device
2030 * By default we select char/ for new entries. Setting class->dev_obj
2031 * to NULL prevents an entry from being created. class->dev_kobj must
2032 * be set (or cleared) before any devices are registered to the class
2033 * otherwise device_create_sys_dev_entry() and
2034 * device_remove_sys_dev_entry() will disagree about the presence of
2037 static struct kobject *device_to_dev_kobj(struct device *dev)
2039 struct kobject *kobj;
2042 kobj = dev->class->dev_kobj;
2044 kobj = sysfs_dev_char_kobj;
2049 static int device_create_sys_dev_entry(struct device *dev)
2051 struct kobject *kobj = device_to_dev_kobj(dev);
2056 format_dev_t(devt_str, dev->devt);
2057 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
2063 static void device_remove_sys_dev_entry(struct device *dev)
2065 struct kobject *kobj = device_to_dev_kobj(dev);
2069 format_dev_t(devt_str, dev->devt);
2070 sysfs_remove_link(kobj, devt_str);
2074 static int device_private_init(struct device *dev)
2076 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
2079 dev->p->device = dev;
2080 klist_init(&dev->p->klist_children, klist_children_get,
2081 klist_children_put);
2082 INIT_LIST_HEAD(&dev->p->deferred_probe);
2087 * device_add - add device to device hierarchy.
2090 * This is part 2 of device_register(), though may be called
2091 * separately _iff_ device_initialize() has been called separately.
2093 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
2094 * to the global and sibling lists for the device, then
2095 * adds it to the other relevant subsystems of the driver model.
2097 * Do not call this routine or device_register() more than once for
2098 * any device structure. The driver model core is not designed to work
2099 * with devices that get unregistered and then spring back to life.
2100 * (Among other things, it's very hard to guarantee that all references
2101 * to the previous incarnation of @dev have been dropped.) Allocate
2102 * and register a fresh new struct device instead.
2104 * NOTE: _Never_ directly free @dev after calling this function, even
2105 * if it returned an error! Always use put_device() to give up your
2106 * reference instead.
2108 * Rule of thumb is: if device_add() succeeds, you should call
2109 * device_del() when you want to get rid of it. If device_add() has
2110 * *not* succeeded, use *only* put_device() to drop the reference
2113 int device_add(struct device *dev)
2115 struct device *parent;
2116 struct kobject *kobj;
2117 struct class_interface *class_intf;
2118 int error = -EINVAL;
2119 struct kobject *glue_dir = NULL;
2121 dev = get_device(dev);
2126 error = device_private_init(dev);
2132 * for statically allocated devices, which should all be converted
2133 * some day, we need to initialize the name. We prevent reading back
2134 * the name, and force the use of dev_name()
2136 if (dev->init_name) {
2137 dev_set_name(dev, "%s", dev->init_name);
2138 dev->init_name = NULL;
2141 /* subsystems can specify simple device enumeration */
2142 if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
2143 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
2145 if (!dev_name(dev)) {
2150 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
2152 parent = get_device(dev->parent);
2153 kobj = get_device_parent(dev, parent);
2155 error = PTR_ERR(kobj);
2159 dev->kobj.parent = kobj;
2161 /* use parent numa_node */
2162 if (parent && (dev_to_node(dev) == NUMA_NO_NODE))
2163 set_dev_node(dev, dev_to_node(parent));
2165 /* first, register with generic layer. */
2166 /* we require the name to be set before, and pass NULL */
2167 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
2169 glue_dir = get_glue_dir(dev);
2173 /* notify platform of device entry */
2174 error = device_platform_notify(dev, KOBJ_ADD);
2176 goto platform_error;
2178 error = device_create_file(dev, &dev_attr_uevent);
2182 error = device_add_class_symlinks(dev);
2185 error = device_add_attrs(dev);
2188 error = bus_add_device(dev);
2191 error = dpm_sysfs_add(dev);
2196 if (MAJOR(dev->devt)) {
2197 error = device_create_file(dev, &dev_attr_dev);
2201 error = device_create_sys_dev_entry(dev);
2205 devtmpfs_create_node(dev);
2208 /* Notify clients of device addition. This call must come
2209 * after dpm_sysfs_add() and before kobject_uevent().
2212 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2213 BUS_NOTIFY_ADD_DEVICE, dev);
2215 kobject_uevent(&dev->kobj, KOBJ_ADD);
2216 bus_probe_device(dev);
2218 klist_add_tail(&dev->p->knode_parent,
2219 &parent->p->klist_children);
2222 mutex_lock(&dev->class->p->mutex);
2223 /* tie the class to the device */
2224 klist_add_tail(&dev->p->knode_class,
2225 &dev->class->p->klist_devices);
2227 /* notify any interfaces that the device is here */
2228 list_for_each_entry(class_intf,
2229 &dev->class->p->interfaces, node)
2230 if (class_intf->add_dev)
2231 class_intf->add_dev(dev, class_intf);
2232 mutex_unlock(&dev->class->p->mutex);
2238 if (MAJOR(dev->devt))
2239 device_remove_file(dev, &dev_attr_dev);
2241 device_pm_remove(dev);
2242 dpm_sysfs_remove(dev);
2244 bus_remove_device(dev);
2246 device_remove_attrs(dev);
2248 device_remove_class_symlinks(dev);
2250 device_remove_file(dev, &dev_attr_uevent);
2252 device_platform_notify(dev, KOBJ_REMOVE);
2254 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
2255 glue_dir = get_glue_dir(dev);
2256 kobject_del(&dev->kobj);
2258 cleanup_glue_dir(dev, glue_dir);
2266 EXPORT_SYMBOL_GPL(device_add);
2269 * device_register - register a device with the system.
2270 * @dev: pointer to the device structure
2272 * This happens in two clean steps - initialize the device
2273 * and add it to the system. The two steps can be called
2274 * separately, but this is the easiest and most common.
2275 * I.e. you should only call the two helpers separately if
2276 * have a clearly defined need to use and refcount the device
2277 * before it is added to the hierarchy.
2279 * For more information, see the kerneldoc for device_initialize()
2282 * NOTE: _Never_ directly free @dev after calling this function, even
2283 * if it returned an error! Always use put_device() to give up the
2284 * reference initialized in this function instead.
2286 int device_register(struct device *dev)
2288 device_initialize(dev);
2289 return device_add(dev);
2291 EXPORT_SYMBOL_GPL(device_register);
2294 * get_device - increment reference count for device.
2297 * This simply forwards the call to kobject_get(), though
2298 * we do take care to provide for the case that we get a NULL
2299 * pointer passed in.
2301 struct device *get_device(struct device *dev)
2303 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
2305 EXPORT_SYMBOL_GPL(get_device);
2308 * put_device - decrement reference count.
2309 * @dev: device in question.
2311 void put_device(struct device *dev)
2313 /* might_sleep(); */
2315 kobject_put(&dev->kobj);
2317 EXPORT_SYMBOL_GPL(put_device);
2319 bool kill_device(struct device *dev)
2322 * Require the device lock and set the "dead" flag to guarantee that
2323 * the update behavior is consistent with the other bitfields near
2324 * it and that we cannot have an asynchronous probe routine trying
2325 * to run while we are tearing out the bus/class/sysfs from
2326 * underneath the device.
2328 lockdep_assert_held(&dev->mutex);
2332 dev->p->dead = true;
2335 EXPORT_SYMBOL_GPL(kill_device);
2338 * device_del - delete device from system.
2341 * This is the first part of the device unregistration
2342 * sequence. This removes the device from the lists we control
2343 * from here, has it removed from the other driver model
2344 * subsystems it was added to in device_add(), and removes it
2345 * from the kobject hierarchy.
2347 * NOTE: this should be called manually _iff_ device_add() was
2348 * also called manually.
2350 void device_del(struct device *dev)
2352 struct device *parent = dev->parent;
2353 struct kobject *glue_dir = NULL;
2354 struct class_interface *class_intf;
2360 /* Notify clients of device removal. This call must come
2361 * before dpm_sysfs_remove().
2364 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2365 BUS_NOTIFY_DEL_DEVICE, dev);
2367 dpm_sysfs_remove(dev);
2369 klist_del(&dev->p->knode_parent);
2370 if (MAJOR(dev->devt)) {
2371 devtmpfs_delete_node(dev);
2372 device_remove_sys_dev_entry(dev);
2373 device_remove_file(dev, &dev_attr_dev);
2376 device_remove_class_symlinks(dev);
2378 mutex_lock(&dev->class->p->mutex);
2379 /* notify any interfaces that the device is now gone */
2380 list_for_each_entry(class_intf,
2381 &dev->class->p->interfaces, node)
2382 if (class_intf->remove_dev)
2383 class_intf->remove_dev(dev, class_intf);
2384 /* remove the device from the class list */
2385 klist_del(&dev->p->knode_class);
2386 mutex_unlock(&dev->class->p->mutex);
2388 device_remove_file(dev, &dev_attr_uevent);
2389 device_remove_attrs(dev);
2390 bus_remove_device(dev);
2391 device_pm_remove(dev);
2392 driver_deferred_probe_del(dev);
2393 device_platform_notify(dev, KOBJ_REMOVE);
2394 device_remove_properties(dev);
2395 device_links_purge(dev);
2398 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2399 BUS_NOTIFY_REMOVED_DEVICE, dev);
2400 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
2401 glue_dir = get_glue_dir(dev);
2402 kobject_del(&dev->kobj);
2403 cleanup_glue_dir(dev, glue_dir);
2406 EXPORT_SYMBOL_GPL(device_del);
2409 * device_unregister - unregister device from system.
2410 * @dev: device going away.
2412 * We do this in two parts, like we do device_register(). First,
2413 * we remove it from all the subsystems with device_del(), then
2414 * we decrement the reference count via put_device(). If that
2415 * is the final reference count, the device will be cleaned up
2416 * via device_release() above. Otherwise, the structure will
2417 * stick around until the final reference to the device is dropped.
2419 void device_unregister(struct device *dev)
2421 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
2425 EXPORT_SYMBOL_GPL(device_unregister);
2427 static struct device *prev_device(struct klist_iter *i)
2429 struct klist_node *n = klist_prev(i);
2430 struct device *dev = NULL;
2431 struct device_private *p;
2434 p = to_device_private_parent(n);
2440 static struct device *next_device(struct klist_iter *i)
2442 struct klist_node *n = klist_next(i);
2443 struct device *dev = NULL;
2444 struct device_private *p;
2447 p = to_device_private_parent(n);
2454 * device_get_devnode - path of device node file
2456 * @mode: returned file access mode
2457 * @uid: returned file owner
2458 * @gid: returned file group
2459 * @tmp: possibly allocated string
2461 * Return the relative path of a possible device node.
2462 * Non-default names may need to allocate a memory to compose
2463 * a name. This memory is returned in tmp and needs to be
2464 * freed by the caller.
2466 const char *device_get_devnode(struct device *dev,
2467 umode_t *mode, kuid_t *uid, kgid_t *gid,
2474 /* the device type may provide a specific name */
2475 if (dev->type && dev->type->devnode)
2476 *tmp = dev->type->devnode(dev, mode, uid, gid);
2480 /* the class may provide a specific name */
2481 if (dev->class && dev->class->devnode)
2482 *tmp = dev->class->devnode(dev, mode);
2486 /* return name without allocation, tmp == NULL */
2487 if (strchr(dev_name(dev), '!') == NULL)
2488 return dev_name(dev);
2490 /* replace '!' in the name with '/' */
2491 s = kstrdup(dev_name(dev), GFP_KERNEL);
2494 strreplace(s, '!', '/');
2499 * device_for_each_child - device child iterator.
2500 * @parent: parent struct device.
2501 * @fn: function to be called for each device.
2502 * @data: data for the callback.
2504 * Iterate over @parent's child devices, and call @fn for each,
2507 * We check the return of @fn each time. If it returns anything
2508 * other than 0, we break out and return that value.
2510 int device_for_each_child(struct device *parent, void *data,
2511 int (*fn)(struct device *dev, void *data))
2513 struct klist_iter i;
2514 struct device *child;
2520 klist_iter_init(&parent->p->klist_children, &i);
2521 while (!error && (child = next_device(&i)))
2522 error = fn(child, data);
2523 klist_iter_exit(&i);
2526 EXPORT_SYMBOL_GPL(device_for_each_child);
2529 * device_for_each_child_reverse - device child iterator in reversed order.
2530 * @parent: parent struct device.
2531 * @fn: function to be called for each device.
2532 * @data: data for the callback.
2534 * Iterate over @parent's child devices, and call @fn for each,
2537 * We check the return of @fn each time. If it returns anything
2538 * other than 0, we break out and return that value.
2540 int device_for_each_child_reverse(struct device *parent, void *data,
2541 int (*fn)(struct device *dev, void *data))
2543 struct klist_iter i;
2544 struct device *child;
2550 klist_iter_init(&parent->p->klist_children, &i);
2551 while ((child = prev_device(&i)) && !error)
2552 error = fn(child, data);
2553 klist_iter_exit(&i);
2556 EXPORT_SYMBOL_GPL(device_for_each_child_reverse);
2559 * device_find_child - device iterator for locating a particular device.
2560 * @parent: parent struct device
2561 * @match: Callback function to check device
2562 * @data: Data to pass to match function
2564 * This is similar to the device_for_each_child() function above, but it
2565 * returns a reference to a device that is 'found' for later use, as
2566 * determined by the @match callback.
2568 * The callback should return 0 if the device doesn't match and non-zero
2569 * if it does. If the callback returns non-zero and a reference to the
2570 * current device can be obtained, this function will return to the caller
2571 * and not iterate over any more devices.
2573 * NOTE: you will need to drop the reference with put_device() after use.
2575 struct device *device_find_child(struct device *parent, void *data,
2576 int (*match)(struct device *dev, void *data))
2578 struct klist_iter i;
2579 struct device *child;
2584 klist_iter_init(&parent->p->klist_children, &i);
2585 while ((child = next_device(&i)))
2586 if (match(child, data) && get_device(child))
2588 klist_iter_exit(&i);
2591 EXPORT_SYMBOL_GPL(device_find_child);
2594 * device_find_child_by_name - device iterator for locating a child device.
2595 * @parent: parent struct device
2596 * @name: name of the child device
2598 * This is similar to the device_find_child() function above, but it
2599 * returns a reference to a device that has the name @name.
2601 * NOTE: you will need to drop the reference with put_device() after use.
2603 struct device *device_find_child_by_name(struct device *parent,
2606 struct klist_iter i;
2607 struct device *child;
2612 klist_iter_init(&parent->p->klist_children, &i);
2613 while ((child = next_device(&i)))
2614 if (!strcmp(dev_name(child), name) && get_device(child))
2616 klist_iter_exit(&i);
2619 EXPORT_SYMBOL_GPL(device_find_child_by_name);
2621 int __init devices_init(void)
2623 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
2626 dev_kobj = kobject_create_and_add("dev", NULL);
2629 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
2630 if (!sysfs_dev_block_kobj)
2631 goto block_kobj_err;
2632 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
2633 if (!sysfs_dev_char_kobj)
2639 kobject_put(sysfs_dev_block_kobj);
2641 kobject_put(dev_kobj);
2643 kset_unregister(devices_kset);
2647 static int device_check_offline(struct device *dev, void *not_used)
2651 ret = device_for_each_child(dev, NULL, device_check_offline);
2655 return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
2659 * device_offline - Prepare the device for hot-removal.
2660 * @dev: Device to be put offline.
2662 * Execute the device bus type's .offline() callback, if present, to prepare
2663 * the device for a subsequent hot-removal. If that succeeds, the device must
2664 * not be used until either it is removed or its bus type's .online() callback
2667 * Call under device_hotplug_lock.
2669 int device_offline(struct device *dev)
2673 if (dev->offline_disabled)
2676 ret = device_for_each_child(dev, NULL, device_check_offline);
2681 if (device_supports_offline(dev)) {
2685 ret = dev->bus->offline(dev);
2687 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
2688 dev->offline = true;
2698 * device_online - Put the device back online after successful device_offline().
2699 * @dev: Device to be put back online.
2701 * If device_offline() has been successfully executed for @dev, but the device
2702 * has not been removed subsequently, execute its bus type's .online() callback
2703 * to indicate that the device can be used again.
2705 * Call under device_hotplug_lock.
2707 int device_online(struct device *dev)
2712 if (device_supports_offline(dev)) {
2714 ret = dev->bus->online(dev);
2716 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
2717 dev->offline = false;
2728 struct root_device {
2730 struct module *owner;
2733 static inline struct root_device *to_root_device(struct device *d)
2735 return container_of(d, struct root_device, dev);
2738 static void root_device_release(struct device *dev)
2740 kfree(to_root_device(dev));
2744 * __root_device_register - allocate and register a root device
2745 * @name: root device name
2746 * @owner: owner module of the root device, usually THIS_MODULE
2748 * This function allocates a root device and registers it
2749 * using device_register(). In order to free the returned
2750 * device, use root_device_unregister().
2752 * Root devices are dummy devices which allow other devices
2753 * to be grouped under /sys/devices. Use this function to
2754 * allocate a root device and then use it as the parent of
2755 * any device which should appear under /sys/devices/{name}
2757 * The /sys/devices/{name} directory will also contain a
2758 * 'module' symlink which points to the @owner directory
2761 * Returns &struct device pointer on success, or ERR_PTR() on error.
2763 * Note: You probably want to use root_device_register().
2765 struct device *__root_device_register(const char *name, struct module *owner)
2767 struct root_device *root;
2770 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
2772 return ERR_PTR(err);
2774 err = dev_set_name(&root->dev, "%s", name);
2777 return ERR_PTR(err);
2780 root->dev.release = root_device_release;
2782 err = device_register(&root->dev);
2784 put_device(&root->dev);
2785 return ERR_PTR(err);
2788 #ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
2790 struct module_kobject *mk = &owner->mkobj;
2792 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
2794 device_unregister(&root->dev);
2795 return ERR_PTR(err);
2797 root->owner = owner;
2803 EXPORT_SYMBOL_GPL(__root_device_register);
2806 * root_device_unregister - unregister and free a root device
2807 * @dev: device going away
2809 * This function unregisters and cleans up a device that was created by
2810 * root_device_register().
2812 void root_device_unregister(struct device *dev)
2814 struct root_device *root = to_root_device(dev);
2817 sysfs_remove_link(&root->dev.kobj, "module");
2819 device_unregister(dev);
2821 EXPORT_SYMBOL_GPL(root_device_unregister);
2824 static void device_create_release(struct device *dev)
2826 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
2830 static __printf(6, 0) struct device *
2831 device_create_groups_vargs(struct class *class, struct device *parent,
2832 dev_t devt, void *drvdata,
2833 const struct attribute_group **groups,
2834 const char *fmt, va_list args)
2836 struct device *dev = NULL;
2837 int retval = -ENODEV;
2839 if (class == NULL || IS_ERR(class))
2842 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2848 device_initialize(dev);
2851 dev->parent = parent;
2852 dev->groups = groups;
2853 dev->release = device_create_release;
2854 dev_set_drvdata(dev, drvdata);
2856 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
2860 retval = device_add(dev);
2868 return ERR_PTR(retval);
2872 * device_create_vargs - creates a device and registers it with sysfs
2873 * @class: pointer to the struct class that this device should be registered to
2874 * @parent: pointer to the parent struct device of this new device, if any
2875 * @devt: the dev_t for the char device to be added
2876 * @drvdata: the data to be added to the device for callbacks
2877 * @fmt: string for the device's name
2878 * @args: va_list for the device's name
2880 * This function can be used by char device classes. A struct device
2881 * will be created in sysfs, registered to the specified class.
2883 * A "dev" file will be created, showing the dev_t for the device, if
2884 * the dev_t is not 0,0.
2885 * If a pointer to a parent struct device is passed in, the newly created
2886 * struct device will be a child of that device in sysfs.
2887 * The pointer to the struct device will be returned from the call.
2888 * Any further sysfs files that might be required can be created using this
2891 * Returns &struct device pointer on success, or ERR_PTR() on error.
2893 * Note: the struct class passed to this function must have previously
2894 * been created with a call to class_create().
2896 struct device *device_create_vargs(struct class *class, struct device *parent,
2897 dev_t devt, void *drvdata, const char *fmt,
2900 return device_create_groups_vargs(class, parent, devt, drvdata, NULL,
2903 EXPORT_SYMBOL_GPL(device_create_vargs);
2906 * device_create - creates a device and registers it with sysfs
2907 * @class: pointer to the struct class that this device should be registered to
2908 * @parent: pointer to the parent struct device of this new device, if any
2909 * @devt: the dev_t for the char device to be added
2910 * @drvdata: the data to be added to the device for callbacks
2911 * @fmt: string for the device's name
2913 * This function can be used by char device classes. A struct device
2914 * will be created in sysfs, registered to the specified class.
2916 * A "dev" file will be created, showing the dev_t for the device, if
2917 * the dev_t is not 0,0.
2918 * If a pointer to a parent struct device is passed in, the newly created
2919 * struct device will be a child of that device in sysfs.
2920 * The pointer to the struct device will be returned from the call.
2921 * Any further sysfs files that might be required can be created using this
2924 * Returns &struct device pointer on success, or ERR_PTR() on error.
2926 * Note: the struct class passed to this function must have previously
2927 * been created with a call to class_create().
2929 struct device *device_create(struct class *class, struct device *parent,
2930 dev_t devt, void *drvdata, const char *fmt, ...)
2935 va_start(vargs, fmt);
2936 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
2940 EXPORT_SYMBOL_GPL(device_create);
2943 * device_create_with_groups - creates a device and registers it with sysfs
2944 * @class: pointer to the struct class that this device should be registered to
2945 * @parent: pointer to the parent struct device of this new device, if any
2946 * @devt: the dev_t for the char device to be added
2947 * @drvdata: the data to be added to the device for callbacks
2948 * @groups: NULL-terminated list of attribute groups to be created
2949 * @fmt: string for the device's name
2951 * This function can be used by char device classes. A struct device
2952 * will be created in sysfs, registered to the specified class.
2953 * Additional attributes specified in the groups parameter will also
2954 * be created automatically.
2956 * A "dev" file will be created, showing the dev_t for the device, if
2957 * the dev_t is not 0,0.
2958 * If a pointer to a parent struct device is passed in, the newly created
2959 * struct device will be a child of that device in sysfs.
2960 * The pointer to the struct device will be returned from the call.
2961 * Any further sysfs files that might be required can be created using this
2964 * Returns &struct device pointer on success, or ERR_PTR() on error.
2966 * Note: the struct class passed to this function must have previously
2967 * been created with a call to class_create().
2969 struct device *device_create_with_groups(struct class *class,
2970 struct device *parent, dev_t devt,
2972 const struct attribute_group **groups,
2973 const char *fmt, ...)
2978 va_start(vargs, fmt);
2979 dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
2984 EXPORT_SYMBOL_GPL(device_create_with_groups);
2987 * device_destroy - removes a device that was created with device_create()
2988 * @class: pointer to the struct class that this device was registered with
2989 * @devt: the dev_t of the device that was previously registered
2991 * This call unregisters and cleans up a device that was created with a
2992 * call to device_create().
2994 void device_destroy(struct class *class, dev_t devt)
2998 dev = class_find_device_by_devt(class, devt);
3001 device_unregister(dev);
3004 EXPORT_SYMBOL_GPL(device_destroy);
3007 * device_rename - renames a device
3008 * @dev: the pointer to the struct device to be renamed
3009 * @new_name: the new name of the device
3011 * It is the responsibility of the caller to provide mutual
3012 * exclusion between two different calls of device_rename
3013 * on the same device to ensure that new_name is valid and
3014 * won't conflict with other devices.
3016 * Note: Don't call this function. Currently, the networking layer calls this
3017 * function, but that will change. The following text from Kay Sievers offers
3020 * Renaming devices is racy at many levels, symlinks and other stuff are not
3021 * replaced atomically, and you get a "move" uevent, but it's not easy to
3022 * connect the event to the old and new device. Device nodes are not renamed at
3023 * all, there isn't even support for that in the kernel now.
3025 * In the meantime, during renaming, your target name might be taken by another
3026 * driver, creating conflicts. Or the old name is taken directly after you
3027 * renamed it -- then you get events for the same DEVPATH, before you even see
3028 * the "move" event. It's just a mess, and nothing new should ever rely on
3029 * kernel device renaming. Besides that, it's not even implemented now for
3030 * other things than (driver-core wise very simple) network devices.
3032 * We are currently about to change network renaming in udev to completely
3033 * disallow renaming of devices in the same namespace as the kernel uses,
3034 * because we can't solve the problems properly, that arise with swapping names
3035 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
3036 * be allowed to some other name than eth[0-9]*, for the aforementioned
3039 * Make up a "real" name in the driver before you register anything, or add
3040 * some other attributes for userspace to find the device, or use udev to add
3041 * symlinks -- but never rename kernel devices later, it's a complete mess. We
3042 * don't even want to get into that and try to implement the missing pieces in
3043 * the core. We really have other pieces to fix in the driver core mess. :)
3045 int device_rename(struct device *dev, const char *new_name)
3047 struct kobject *kobj = &dev->kobj;
3048 char *old_device_name = NULL;
3051 dev = get_device(dev);
3055 dev_dbg(dev, "renaming to %s\n", new_name);
3057 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
3058 if (!old_device_name) {
3064 error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj,
3065 kobj, old_device_name,
3066 new_name, kobject_namespace(kobj));
3071 error = kobject_rename(kobj, new_name);
3078 kfree(old_device_name);
3082 EXPORT_SYMBOL_GPL(device_rename);
3084 static int device_move_class_links(struct device *dev,
3085 struct device *old_parent,
3086 struct device *new_parent)
3091 sysfs_remove_link(&dev->kobj, "device");
3093 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
3099 * device_move - moves a device to a new parent
3100 * @dev: the pointer to the struct device to be moved
3101 * @new_parent: the new parent of the device (can be NULL)
3102 * @dpm_order: how to reorder the dpm_list
3104 int device_move(struct device *dev, struct device *new_parent,
3105 enum dpm_order dpm_order)
3108 struct device *old_parent;
3109 struct kobject *new_parent_kobj;
3111 dev = get_device(dev);
3116 new_parent = get_device(new_parent);
3117 new_parent_kobj = get_device_parent(dev, new_parent);
3118 if (IS_ERR(new_parent_kobj)) {
3119 error = PTR_ERR(new_parent_kobj);
3120 put_device(new_parent);
3124 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
3125 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
3126 error = kobject_move(&dev->kobj, new_parent_kobj);
3128 cleanup_glue_dir(dev, new_parent_kobj);
3129 put_device(new_parent);
3132 old_parent = dev->parent;
3133 dev->parent = new_parent;
3135 klist_remove(&dev->p->knode_parent);
3137 klist_add_tail(&dev->p->knode_parent,
3138 &new_parent->p->klist_children);
3139 set_dev_node(dev, dev_to_node(new_parent));
3143 error = device_move_class_links(dev, old_parent, new_parent);
3145 /* We ignore errors on cleanup since we're hosed anyway... */
3146 device_move_class_links(dev, new_parent, old_parent);
3147 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
3149 klist_remove(&dev->p->knode_parent);
3150 dev->parent = old_parent;
3152 klist_add_tail(&dev->p->knode_parent,
3153 &old_parent->p->klist_children);
3154 set_dev_node(dev, dev_to_node(old_parent));
3157 cleanup_glue_dir(dev, new_parent_kobj);
3158 put_device(new_parent);
3162 switch (dpm_order) {
3163 case DPM_ORDER_NONE:
3165 case DPM_ORDER_DEV_AFTER_PARENT:
3166 device_pm_move_after(dev, new_parent);
3167 devices_kset_move_after(dev, new_parent);
3169 case DPM_ORDER_PARENT_BEFORE_DEV:
3170 device_pm_move_before(new_parent, dev);
3171 devices_kset_move_before(new_parent, dev);
3173 case DPM_ORDER_DEV_LAST:
3174 device_pm_move_last(dev);
3175 devices_kset_move_last(dev);
3179 put_device(old_parent);
3185 EXPORT_SYMBOL_GPL(device_move);
3188 * device_shutdown - call ->shutdown() on each device to shutdown.
3190 void device_shutdown(void)
3192 struct device *dev, *parent;
3194 wait_for_device_probe();
3195 device_block_probing();
3199 spin_lock(&devices_kset->list_lock);
3201 * Walk the devices list backward, shutting down each in turn.
3202 * Beware that device unplug events may also start pulling
3203 * devices offline, even as the system is shutting down.
3205 while (!list_empty(&devices_kset->list)) {
3206 dev = list_entry(devices_kset->list.prev, struct device,
3210 * hold reference count of device's parent to
3211 * prevent it from being freed because parent's
3212 * lock is to be held
3214 parent = get_device(dev->parent);
3217 * Make sure the device is off the kset list, in the
3218 * event that dev->*->shutdown() doesn't remove it.
3220 list_del_init(&dev->kobj.entry);
3221 spin_unlock(&devices_kset->list_lock);
3223 /* hold lock to avoid race with probe/release */
3225 device_lock(parent);
3228 /* Don't allow any more runtime suspends */
3229 pm_runtime_get_noresume(dev);
3230 pm_runtime_barrier(dev);
3232 if (dev->class && dev->class->shutdown_pre) {
3234 dev_info(dev, "shutdown_pre\n");
3235 dev->class->shutdown_pre(dev);
3237 if (dev->bus && dev->bus->shutdown) {
3239 dev_info(dev, "shutdown\n");
3240 dev->bus->shutdown(dev);
3241 } else if (dev->driver && dev->driver->shutdown) {
3243 dev_info(dev, "shutdown\n");
3244 dev->driver->shutdown(dev);
3249 device_unlock(parent);
3254 spin_lock(&devices_kset->list_lock);
3256 spin_unlock(&devices_kset->list_lock);
3260 * Device logging functions
3263 #ifdef CONFIG_PRINTK
3265 create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
3271 subsys = dev->class->name;
3273 subsys = dev->bus->name;
3277 pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys);
3282 * Add device identifier DEVICE=:
3286 * +sound:card0 subsystem:devname
3288 if (MAJOR(dev->devt)) {
3291 if (strcmp(subsys, "block") == 0)
3296 pos += snprintf(hdr + pos, hdrlen - pos,
3298 c, MAJOR(dev->devt), MINOR(dev->devt));
3299 } else if (strcmp(subsys, "net") == 0) {
3300 struct net_device *net = to_net_dev(dev);
3303 pos += snprintf(hdr + pos, hdrlen - pos,
3304 "DEVICE=n%u", net->ifindex);
3307 pos += snprintf(hdr + pos, hdrlen - pos,
3308 "DEVICE=+%s:%s", subsys, dev_name(dev));
3317 dev_WARN(dev, "device/subsystem name too long");
3321 int dev_vprintk_emit(int level, const struct device *dev,
3322 const char *fmt, va_list args)
3327 hdrlen = create_syslog_header(dev, hdr, sizeof(hdr));
3329 return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args);
3331 EXPORT_SYMBOL(dev_vprintk_emit);
3333 int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
3338 va_start(args, fmt);
3340 r = dev_vprintk_emit(level, dev, fmt, args);
3346 EXPORT_SYMBOL(dev_printk_emit);
3348 static void __dev_printk(const char *level, const struct device *dev,
3349 struct va_format *vaf)
3352 dev_printk_emit(level[1] - '0', dev, "%s %s: %pV",
3353 dev_driver_string(dev), dev_name(dev), vaf);
3355 printk("%s(NULL device *): %pV", level, vaf);
3358 void dev_printk(const char *level, const struct device *dev,
3359 const char *fmt, ...)
3361 struct va_format vaf;
3364 va_start(args, fmt);
3369 __dev_printk(level, dev, &vaf);
3373 EXPORT_SYMBOL(dev_printk);
3375 #define define_dev_printk_level(func, kern_level) \
3376 void func(const struct device *dev, const char *fmt, ...) \
3378 struct va_format vaf; \
3381 va_start(args, fmt); \
3386 __dev_printk(kern_level, dev, &vaf); \
3390 EXPORT_SYMBOL(func);
3392 define_dev_printk_level(_dev_emerg, KERN_EMERG);
3393 define_dev_printk_level(_dev_alert, KERN_ALERT);
3394 define_dev_printk_level(_dev_crit, KERN_CRIT);
3395 define_dev_printk_level(_dev_err, KERN_ERR);
3396 define_dev_printk_level(_dev_warn, KERN_WARNING);
3397 define_dev_printk_level(_dev_notice, KERN_NOTICE);
3398 define_dev_printk_level(_dev_info, KERN_INFO);
3402 static inline bool fwnode_is_primary(struct fwnode_handle *fwnode)
3404 return fwnode && !IS_ERR(fwnode->secondary);
3408 * set_primary_fwnode - Change the primary firmware node of a given device.
3409 * @dev: Device to handle.
3410 * @fwnode: New primary firmware node of the device.
3412 * Set the device's firmware node pointer to @fwnode, but if a secondary
3413 * firmware node of the device is present, preserve it.
3415 void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
3417 struct device *parent = dev->parent;
3418 struct fwnode_handle *fn = dev->fwnode;
3421 if (fwnode_is_primary(fn))
3425 WARN_ON(fwnode->secondary);
3426 fwnode->secondary = fn;
3428 dev->fwnode = fwnode;
3430 if (fwnode_is_primary(fn)) {
3431 dev->fwnode = fn->secondary;
3432 if (!(parent && fn == parent->fwnode))
3433 fn->secondary = NULL;
3439 EXPORT_SYMBOL_GPL(set_primary_fwnode);
3442 * set_secondary_fwnode - Change the secondary firmware node of a given device.
3443 * @dev: Device to handle.
3444 * @fwnode: New secondary firmware node of the device.
3446 * If a primary firmware node of the device is present, set its secondary
3447 * pointer to @fwnode. Otherwise, set the device's firmware node pointer to
3450 void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
3453 fwnode->secondary = ERR_PTR(-ENODEV);
3455 if (fwnode_is_primary(dev->fwnode))
3456 dev->fwnode->secondary = fwnode;
3458 dev->fwnode = fwnode;
3462 * device_set_of_node_from_dev - reuse device-tree node of another device
3463 * @dev: device whose device-tree node is being set
3464 * @dev2: device whose device-tree node is being reused
3466 * Takes another reference to the new device-tree node after first dropping
3467 * any reference held to the old node.
3469 void device_set_of_node_from_dev(struct device *dev, const struct device *dev2)
3471 of_node_put(dev->of_node);
3472 dev->of_node = of_node_get(dev2->of_node);
3473 dev->of_node_reused = true;
3475 EXPORT_SYMBOL_GPL(device_set_of_node_from_dev);
3477 int device_match_name(struct device *dev, const void *name)
3479 return sysfs_streq(dev_name(dev), name);
3481 EXPORT_SYMBOL_GPL(device_match_name);
3483 int device_match_of_node(struct device *dev, const void *np)
3485 return dev->of_node == np;
3487 EXPORT_SYMBOL_GPL(device_match_of_node);
3489 int device_match_fwnode(struct device *dev, const void *fwnode)
3491 return dev_fwnode(dev) == fwnode;
3493 EXPORT_SYMBOL_GPL(device_match_fwnode);
3495 int device_match_devt(struct device *dev, const void *pdevt)
3497 return dev->devt == *(dev_t *)pdevt;
3499 EXPORT_SYMBOL_GPL(device_match_devt);
3501 int device_match_acpi_dev(struct device *dev, const void *adev)
3503 return ACPI_COMPANION(dev) == adev;
3505 EXPORT_SYMBOL(device_match_acpi_dev);
3507 int device_match_any(struct device *dev, const void *unused)
3511 EXPORT_SYMBOL_GPL(device_match_any);