1 // SPDX-License-Identifier: GPL-2.0
3 * drivers/base/core.c - core driver model code (device registration, etc)
5 * Copyright (c) 2002-3 Patrick Mochel
6 * Copyright (c) 2002-3 Open Source Development Labs
7 * Copyright (c) 2006 Greg Kroah-Hartman <gregkh@suse.de>
8 * Copyright (c) 2006 Novell, Inc.
11 #include <linux/acpi.h>
12 #include <linux/device.h>
13 #include <linux/err.h>
14 #include <linux/fwnode.h>
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/string.h>
19 #include <linux/kdev_t.h>
20 #include <linux/notifier.h>
22 #include <linux/of_device.h>
23 #include <linux/genhd.h>
24 #include <linux/mutex.h>
25 #include <linux/pm_runtime.h>
26 #include <linux/netdevice.h>
27 #include <linux/sched/signal.h>
28 #include <linux/sysfs.h>
31 #include "power/power.h"
33 #ifdef CONFIG_SYSFS_DEPRECATED
34 #ifdef CONFIG_SYSFS_DEPRECATED_V2
35 long sysfs_deprecated = 1;
37 long sysfs_deprecated = 0;
39 static int __init sysfs_deprecated_setup(char *arg)
41 return kstrtol(arg, 10, &sysfs_deprecated);
43 early_param("sysfs.deprecated", sysfs_deprecated_setup);
46 /* Device links support. */
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 */
97 * device_is_dependent - Check if one device depends on another one
98 * @dev: Device to check dependencies for.
99 * @target: Device to check against.
101 * Check if @target depends on @dev or any device dependent on it (its child or
102 * its consumer etc). Return 1 if that is the case or 0 otherwise.
104 static int device_is_dependent(struct device *dev, void *target)
106 struct device_link *link;
112 ret = device_for_each_child(dev, target, device_is_dependent);
116 list_for_each_entry(link, &dev->links.consumers, s_node) {
117 if (link->consumer == target)
120 ret = device_is_dependent(link->consumer, target);
127 static int device_reorder_to_tail(struct device *dev, void *not_used)
129 struct device_link *link;
132 * Devices that have not been registered yet will be put to the ends
133 * of the lists during the registration, so skip them here.
135 if (device_is_registered(dev))
136 devices_kset_move_last(dev);
138 if (device_pm_initialized(dev))
139 device_pm_move_last(dev);
141 device_for_each_child(dev, NULL, device_reorder_to_tail);
142 list_for_each_entry(link, &dev->links.consumers, s_node)
143 device_reorder_to_tail(link->consumer, NULL);
149 * device_pm_move_to_tail - Move set of devices to the end of device lists
150 * @dev: Device to move
152 * This is a device_reorder_to_tail() wrapper taking the requisite locks.
154 * It moves the @dev along with all of its children and all of its consumers
155 * to the ends of the device_kset and dpm_list, recursively.
157 void device_pm_move_to_tail(struct device *dev)
161 idx = device_links_read_lock();
163 device_reorder_to_tail(dev, NULL);
165 device_links_read_unlock(idx);
168 static void device_link_rpm_prepare(struct device *consumer,
169 struct device *supplier)
171 pm_runtime_new_link(consumer);
173 * If the link is being added by the consumer driver at probe time,
174 * balance the decrementation of the supplier's runtime PM usage counter
175 * after consumer probe in driver_probe_device().
177 if (consumer->links.status == DL_DEV_PROBING)
178 pm_runtime_get_noresume(supplier);
182 * device_link_add - Create a link between two devices.
183 * @consumer: Consumer end of the link.
184 * @supplier: Supplier end of the link.
185 * @flags: Link flags.
187 * The caller is responsible for the proper synchronization of the link creation
188 * with runtime PM. First, setting the DL_FLAG_PM_RUNTIME flag will cause the
189 * runtime PM framework to take the link into account. Second, if the
190 * DL_FLAG_RPM_ACTIVE flag is set in addition to it, the supplier devices will
191 * be forced into the active metastate and reference-counted upon the creation
192 * of the link. If DL_FLAG_PM_RUNTIME is not set, DL_FLAG_RPM_ACTIVE will be
195 * If DL_FLAG_STATELESS is set in @flags, the link is not going to be managed by
196 * the driver core and, in particular, the caller of this function is expected
197 * to drop the reference to the link acquired by it directly.
199 * If that flag is not set, however, the caller of this function is handing the
200 * management of the link over to the driver core entirely and its return value
201 * can only be used to check whether or not the link is present. In that case,
202 * the DL_FLAG_AUTOREMOVE_CONSUMER and DL_FLAG_AUTOREMOVE_SUPPLIER device link
203 * flags can be used to indicate to the driver core when the link can be safely
204 * deleted. Namely, setting one of them in @flags indicates to the driver core
205 * that the link is not going to be used (by the given caller of this function)
206 * after unbinding the consumer or supplier driver, respectively, from its
207 * device, so the link can be deleted at that point. If none of them is set,
208 * the link will be maintained until one of the devices pointed to by it (either
209 * the consumer or the supplier) is unregistered.
211 * Also, if DL_FLAG_STATELESS, DL_FLAG_AUTOREMOVE_CONSUMER and
212 * DL_FLAG_AUTOREMOVE_SUPPLIER are not set in @flags (that is, a persistent
213 * managed device link is being added), the DL_FLAG_AUTOPROBE_CONSUMER flag can
214 * be used to request the driver core to automaticall probe for a consmer
215 * driver after successfully binding a driver to the supplier device.
217 * The combination of DL_FLAG_STATELESS and either DL_FLAG_AUTOREMOVE_CONSUMER
218 * or DL_FLAG_AUTOREMOVE_SUPPLIER set in @flags at the same time is invalid and
219 * will cause NULL to be returned upfront.
221 * A side effect of the link creation is re-ordering of dpm_list and the
222 * devices_kset list by moving the consumer device and all devices depending
223 * on it to the ends of these lists (that does not happen to devices that have
224 * not been registered when this function is called).
226 * The supplier device is required to be registered when this function is called
227 * and NULL will be returned if that is not the case. The consumer device need
228 * not be registered, however.
230 struct device_link *device_link_add(struct device *consumer,
231 struct device *supplier, u32 flags)
233 struct device_link *link;
235 if (!consumer || !supplier ||
236 (flags & DL_FLAG_STATELESS &&
237 flags & (DL_FLAG_AUTOREMOVE_CONSUMER |
238 DL_FLAG_AUTOREMOVE_SUPPLIER |
239 DL_FLAG_AUTOPROBE_CONSUMER)) ||
240 (flags & DL_FLAG_AUTOPROBE_CONSUMER &&
241 flags & (DL_FLAG_AUTOREMOVE_CONSUMER |
242 DL_FLAG_AUTOREMOVE_SUPPLIER)))
245 if (flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) {
246 if (pm_runtime_get_sync(supplier) < 0) {
247 pm_runtime_put_noidle(supplier);
252 device_links_write_lock();
256 * If the supplier has not been fully registered yet or there is a
257 * reverse dependency between the consumer and the supplier already in
258 * the graph, return NULL.
260 if (!device_pm_initialized(supplier)
261 || device_is_dependent(consumer, supplier)) {
267 * DL_FLAG_AUTOREMOVE_SUPPLIER indicates that the link will be needed
268 * longer than for DL_FLAG_AUTOREMOVE_CONSUMER and setting them both
269 * together doesn't make sense, so prefer DL_FLAG_AUTOREMOVE_SUPPLIER.
271 if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
272 flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER;
274 list_for_each_entry(link, &supplier->links.consumers, s_node) {
275 if (link->consumer != consumer)
279 * Don't return a stateless link if the caller wants a stateful
280 * one and vice versa.
282 if (WARN_ON((flags & DL_FLAG_STATELESS) != (link->flags & DL_FLAG_STATELESS))) {
287 if (flags & DL_FLAG_PM_RUNTIME) {
288 if (!(link->flags & DL_FLAG_PM_RUNTIME)) {
289 device_link_rpm_prepare(consumer, supplier);
290 link->flags |= DL_FLAG_PM_RUNTIME;
292 if (flags & DL_FLAG_RPM_ACTIVE)
293 refcount_inc(&link->rpm_active);
296 if (flags & DL_FLAG_STATELESS) {
297 kref_get(&link->kref);
302 * If the life time of the link following from the new flags is
303 * longer than indicated by the flags of the existing link,
304 * update the existing link to stay around longer.
306 if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER) {
307 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER) {
308 link->flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER;
309 link->flags |= DL_FLAG_AUTOREMOVE_SUPPLIER;
311 } else if (!(flags & DL_FLAG_AUTOREMOVE_CONSUMER)) {
312 link->flags &= ~(DL_FLAG_AUTOREMOVE_CONSUMER |
313 DL_FLAG_AUTOREMOVE_SUPPLIER);
318 link = kzalloc(sizeof(*link), GFP_KERNEL);
322 refcount_set(&link->rpm_active, 1);
324 if (flags & DL_FLAG_PM_RUNTIME) {
325 if (flags & DL_FLAG_RPM_ACTIVE)
326 refcount_inc(&link->rpm_active);
328 device_link_rpm_prepare(consumer, supplier);
331 get_device(supplier);
332 link->supplier = supplier;
333 INIT_LIST_HEAD(&link->s_node);
334 get_device(consumer);
335 link->consumer = consumer;
336 INIT_LIST_HEAD(&link->c_node);
338 kref_init(&link->kref);
340 /* Determine the initial link state. */
341 if (flags & DL_FLAG_STATELESS) {
342 link->status = DL_STATE_NONE;
344 switch (supplier->links.status) {
346 switch (consumer->links.status) {
349 * A consumer driver can create a link to a
350 * supplier that has not completed its probing
351 * yet as long as it knows that the supplier is
352 * already functional (for example, it has just
353 * acquired some resources from the supplier).
355 link->status = DL_STATE_CONSUMER_PROBE;
358 link->status = DL_STATE_DORMANT;
362 case DL_DEV_DRIVER_BOUND:
363 switch (consumer->links.status) {
365 link->status = DL_STATE_CONSUMER_PROBE;
367 case DL_DEV_DRIVER_BOUND:
368 link->status = DL_STATE_ACTIVE;
371 link->status = DL_STATE_AVAILABLE;
375 case DL_DEV_UNBINDING:
376 link->status = DL_STATE_SUPPLIER_UNBIND;
379 link->status = DL_STATE_DORMANT;
385 * Some callers expect the link creation during consumer driver probe to
386 * resume the supplier even without DL_FLAG_RPM_ACTIVE.
388 if (link->status == DL_STATE_CONSUMER_PROBE &&
389 flags & DL_FLAG_PM_RUNTIME)
390 pm_runtime_resume(supplier);
393 * Move the consumer and all of the devices depending on it to the end
394 * of dpm_list and the devices_kset list.
396 * It is necessary to hold dpm_list locked throughout all that or else
397 * we may end up suspending with a wrong ordering of it.
399 device_reorder_to_tail(consumer, NULL);
401 list_add_tail_rcu(&link->s_node, &supplier->links.consumers);
402 list_add_tail_rcu(&link->c_node, &consumer->links.suppliers);
404 dev_dbg(consumer, "Linked as a consumer to %s\n", dev_name(supplier));
408 device_links_write_unlock();
410 if ((flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) && !link)
411 pm_runtime_put(supplier);
415 EXPORT_SYMBOL_GPL(device_link_add);
417 static void device_link_free(struct device_link *link)
419 while (refcount_dec_not_one(&link->rpm_active))
420 pm_runtime_put(link->supplier);
422 put_device(link->consumer);
423 put_device(link->supplier);
428 static void __device_link_free_srcu(struct rcu_head *rhead)
430 device_link_free(container_of(rhead, struct device_link, rcu_head));
433 static void __device_link_del(struct kref *kref)
435 struct device_link *link = container_of(kref, struct device_link, kref);
437 dev_dbg(link->consumer, "Dropping the link to %s\n",
438 dev_name(link->supplier));
440 if (link->flags & DL_FLAG_PM_RUNTIME)
441 pm_runtime_drop_link(link->consumer);
443 list_del_rcu(&link->s_node);
444 list_del_rcu(&link->c_node);
445 call_srcu(&device_links_srcu, &link->rcu_head, __device_link_free_srcu);
447 #else /* !CONFIG_SRCU */
448 static void __device_link_del(struct kref *kref)
450 struct device_link *link = container_of(kref, struct device_link, kref);
452 dev_info(link->consumer, "Dropping the link to %s\n",
453 dev_name(link->supplier));
455 if (link->flags & DL_FLAG_PM_RUNTIME)
456 pm_runtime_drop_link(link->consumer);
458 list_del(&link->s_node);
459 list_del(&link->c_node);
460 device_link_free(link);
462 #endif /* !CONFIG_SRCU */
464 static void device_link_put_kref(struct device_link *link)
466 if (link->flags & DL_FLAG_STATELESS)
467 kref_put(&link->kref, __device_link_del);
469 WARN(1, "Unable to drop a managed device link reference\n");
473 * device_link_del - Delete a stateless link between two devices.
474 * @link: Device link to delete.
476 * The caller must ensure proper synchronization of this function with runtime
477 * PM. If the link was added multiple times, it needs to be deleted as often.
478 * Care is required for hotplugged devices: Their links are purged on removal
479 * and calling device_link_del() is then no longer allowed.
481 void device_link_del(struct device_link *link)
483 device_links_write_lock();
485 device_link_put_kref(link);
487 device_links_write_unlock();
489 EXPORT_SYMBOL_GPL(device_link_del);
492 * device_link_remove - Delete a stateless link between two devices.
493 * @consumer: Consumer end of the link.
494 * @supplier: Supplier end of the link.
496 * The caller must ensure proper synchronization of this function with runtime
499 void device_link_remove(void *consumer, struct device *supplier)
501 struct device_link *link;
503 if (WARN_ON(consumer == supplier))
506 device_links_write_lock();
509 list_for_each_entry(link, &supplier->links.consumers, s_node) {
510 if (link->consumer == consumer) {
511 device_link_put_kref(link);
517 device_links_write_unlock();
519 EXPORT_SYMBOL_GPL(device_link_remove);
521 static void device_links_missing_supplier(struct device *dev)
523 struct device_link *link;
525 list_for_each_entry(link, &dev->links.suppliers, c_node)
526 if (link->status == DL_STATE_CONSUMER_PROBE)
527 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
531 * device_links_check_suppliers - Check presence of supplier drivers.
532 * @dev: Consumer device.
534 * Check links from this device to any suppliers. Walk the list of the device's
535 * links to suppliers and see if all of them are available. If not, simply
536 * return -EPROBE_DEFER.
538 * We need to guarantee that the supplier will not go away after the check has
539 * been positive here. It only can go away in __device_release_driver() and
540 * that function checks the device's links to consumers. This means we need to
541 * mark the link as "consumer probe in progress" to make the supplier removal
542 * wait for us to complete (or bad things may happen).
544 * Links with the DL_FLAG_STATELESS flag set are ignored.
546 int device_links_check_suppliers(struct device *dev)
548 struct device_link *link;
551 device_links_write_lock();
553 list_for_each_entry(link, &dev->links.suppliers, c_node) {
554 if (link->flags & DL_FLAG_STATELESS)
557 if (link->status != DL_STATE_AVAILABLE) {
558 device_links_missing_supplier(dev);
562 WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE);
564 dev->links.status = DL_DEV_PROBING;
566 device_links_write_unlock();
571 * device_links_driver_bound - Update device links after probing its driver.
572 * @dev: Device to update the links for.
574 * The probe has been successful, so update links from this device to any
575 * consumers by changing their status to "available".
577 * Also change the status of @dev's links to suppliers to "active".
579 * Links with the DL_FLAG_STATELESS flag set are ignored.
581 void device_links_driver_bound(struct device *dev)
583 struct device_link *link;
585 device_links_write_lock();
587 list_for_each_entry(link, &dev->links.consumers, s_node) {
588 if (link->flags & DL_FLAG_STATELESS)
592 * Links created during consumer probe may be in the "consumer
593 * probe" state to start with if the supplier is still probing
594 * when they are created and they may become "active" if the
595 * consumer probe returns first. Skip them here.
597 if (link->status == DL_STATE_CONSUMER_PROBE ||
598 link->status == DL_STATE_ACTIVE)
601 WARN_ON(link->status != DL_STATE_DORMANT);
602 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
604 if (link->flags & DL_FLAG_AUTOPROBE_CONSUMER)
605 driver_deferred_probe_add(link->consumer);
608 list_for_each_entry(link, &dev->links.suppliers, c_node) {
609 if (link->flags & DL_FLAG_STATELESS)
612 WARN_ON(link->status != DL_STATE_CONSUMER_PROBE);
613 WRITE_ONCE(link->status, DL_STATE_ACTIVE);
616 dev->links.status = DL_DEV_DRIVER_BOUND;
618 device_links_write_unlock();
622 * __device_links_no_driver - Update links of a device without a driver.
623 * @dev: Device without a drvier.
625 * Delete all non-persistent links from this device to any suppliers.
627 * Persistent links stay around, but their status is changed to "available",
628 * unless they already are in the "supplier unbind in progress" state in which
629 * case they need not be updated.
631 * Links with the DL_FLAG_STATELESS flag set are ignored.
633 static void __device_links_no_driver(struct device *dev)
635 struct device_link *link, *ln;
637 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
638 if (link->flags & DL_FLAG_STATELESS)
641 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER)
642 __device_link_del(&link->kref);
643 else if (link->status == DL_STATE_CONSUMER_PROBE ||
644 link->status == DL_STATE_ACTIVE)
645 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
648 dev->links.status = DL_DEV_NO_DRIVER;
652 * device_links_no_driver - Update links after failing driver probe.
653 * @dev: Device whose driver has just failed to probe.
655 * Clean up leftover links to consumers for @dev and invoke
656 * %__device_links_no_driver() to update links to suppliers for it as
659 * Links with the DL_FLAG_STATELESS flag set are ignored.
661 void device_links_no_driver(struct device *dev)
663 struct device_link *link;
665 device_links_write_lock();
667 list_for_each_entry(link, &dev->links.consumers, s_node) {
668 if (link->flags & DL_FLAG_STATELESS)
672 * The probe has failed, so if the status of the link is
673 * "consumer probe" or "active", it must have been added by
674 * a probing consumer while this device was still probing.
675 * Change its state to "dormant", as it represents a valid
676 * relationship, but it is not functionally meaningful.
678 if (link->status == DL_STATE_CONSUMER_PROBE ||
679 link->status == DL_STATE_ACTIVE)
680 WRITE_ONCE(link->status, DL_STATE_DORMANT);
683 __device_links_no_driver(dev);
685 device_links_write_unlock();
689 * device_links_driver_cleanup - Update links after driver removal.
690 * @dev: Device whose driver has just gone away.
692 * Update links to consumers for @dev by changing their status to "dormant" and
693 * invoke %__device_links_no_driver() to update links to suppliers for it as
696 * Links with the DL_FLAG_STATELESS flag set are ignored.
698 void device_links_driver_cleanup(struct device *dev)
700 struct device_link *link, *ln;
702 device_links_write_lock();
704 list_for_each_entry_safe(link, ln, &dev->links.consumers, s_node) {
705 if (link->flags & DL_FLAG_STATELESS)
708 WARN_ON(link->flags & DL_FLAG_AUTOREMOVE_CONSUMER);
709 WARN_ON(link->status != DL_STATE_SUPPLIER_UNBIND);
712 * autoremove the links between this @dev and its consumer
713 * devices that are not active, i.e. where the link state
714 * has moved to DL_STATE_SUPPLIER_UNBIND.
716 if (link->status == DL_STATE_SUPPLIER_UNBIND &&
717 link->flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
718 __device_link_del(&link->kref);
720 WRITE_ONCE(link->status, DL_STATE_DORMANT);
723 __device_links_no_driver(dev);
725 device_links_write_unlock();
729 * device_links_busy - Check if there are any busy links to consumers.
730 * @dev: Device to check.
732 * Check each consumer of the device and return 'true' if its link's status
733 * is one of "consumer probe" or "active" (meaning that the given consumer is
734 * probing right now or its driver is present). Otherwise, change the link
735 * state to "supplier unbind" to prevent the consumer from being probed
736 * successfully going forward.
738 * Return 'false' if there are no probing or active consumers.
740 * Links with the DL_FLAG_STATELESS flag set are ignored.
742 bool device_links_busy(struct device *dev)
744 struct device_link *link;
747 device_links_write_lock();
749 list_for_each_entry(link, &dev->links.consumers, s_node) {
750 if (link->flags & DL_FLAG_STATELESS)
753 if (link->status == DL_STATE_CONSUMER_PROBE
754 || link->status == DL_STATE_ACTIVE) {
758 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
761 dev->links.status = DL_DEV_UNBINDING;
763 device_links_write_unlock();
768 * device_links_unbind_consumers - Force unbind consumers of the given device.
769 * @dev: Device to unbind the consumers of.
771 * Walk the list of links to consumers for @dev and if any of them is in the
772 * "consumer probe" state, wait for all device probes in progress to complete
775 * If that's not the case, change the status of the link to "supplier unbind"
776 * and check if the link was in the "active" state. If so, force the consumer
777 * driver to unbind and start over (the consumer will not re-probe as we have
778 * changed the state of the link already).
780 * Links with the DL_FLAG_STATELESS flag set are ignored.
782 void device_links_unbind_consumers(struct device *dev)
784 struct device_link *link;
787 device_links_write_lock();
789 list_for_each_entry(link, &dev->links.consumers, s_node) {
790 enum device_link_state status;
792 if (link->flags & DL_FLAG_STATELESS)
795 status = link->status;
796 if (status == DL_STATE_CONSUMER_PROBE) {
797 device_links_write_unlock();
799 wait_for_device_probe();
802 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
803 if (status == DL_STATE_ACTIVE) {
804 struct device *consumer = link->consumer;
806 get_device(consumer);
808 device_links_write_unlock();
810 device_release_driver_internal(consumer, NULL,
812 put_device(consumer);
817 device_links_write_unlock();
821 * device_links_purge - Delete existing links to other devices.
822 * @dev: Target device.
824 static void device_links_purge(struct device *dev)
826 struct device_link *link, *ln;
829 * Delete all of the remaining links from this device to any other
830 * devices (either consumers or suppliers).
832 device_links_write_lock();
834 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
835 WARN_ON(link->status == DL_STATE_ACTIVE);
836 __device_link_del(&link->kref);
839 list_for_each_entry_safe_reverse(link, ln, &dev->links.consumers, s_node) {
840 WARN_ON(link->status != DL_STATE_DORMANT &&
841 link->status != DL_STATE_NONE);
842 __device_link_del(&link->kref);
845 device_links_write_unlock();
848 /* Device links support end. */
850 int (*platform_notify)(struct device *dev) = NULL;
851 int (*platform_notify_remove)(struct device *dev) = NULL;
852 static struct kobject *dev_kobj;
853 struct kobject *sysfs_dev_char_kobj;
854 struct kobject *sysfs_dev_block_kobj;
856 static DEFINE_MUTEX(device_hotplug_lock);
858 void lock_device_hotplug(void)
860 mutex_lock(&device_hotplug_lock);
863 void unlock_device_hotplug(void)
865 mutex_unlock(&device_hotplug_lock);
868 int lock_device_hotplug_sysfs(void)
870 if (mutex_trylock(&device_hotplug_lock))
873 /* Avoid busy looping (5 ms of sleep should do). */
875 return restart_syscall();
879 static inline int device_is_not_partition(struct device *dev)
881 return !(dev->type == &part_type);
884 static inline int device_is_not_partition(struct device *dev)
891 device_platform_notify(struct device *dev, enum kobject_action action)
895 ret = acpi_platform_notify(dev, action);
899 ret = software_node_notify(dev, action);
903 if (platform_notify && action == KOBJ_ADD)
904 platform_notify(dev);
905 else if (platform_notify_remove && action == KOBJ_REMOVE)
906 platform_notify_remove(dev);
911 * dev_driver_string - Return a device's driver name, if at all possible
912 * @dev: struct device to get the name of
914 * Will return the device's driver's name if it is bound to a device. If
915 * the device is not bound to a driver, it will return the name of the bus
916 * it is attached to. If it is not attached to a bus either, an empty
917 * string will be returned.
919 const char *dev_driver_string(const struct device *dev)
921 struct device_driver *drv;
923 /* dev->driver can change to NULL underneath us because of unbinding,
924 * so be careful about accessing it. dev->bus and dev->class should
925 * never change once they are set, so they don't need special care.
927 drv = READ_ONCE(dev->driver);
928 return drv ? drv->name :
929 (dev->bus ? dev->bus->name :
930 (dev->class ? dev->class->name : ""));
932 EXPORT_SYMBOL(dev_driver_string);
934 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
936 static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
939 struct device_attribute *dev_attr = to_dev_attr(attr);
940 struct device *dev = kobj_to_dev(kobj);
944 ret = dev_attr->show(dev, dev_attr, buf);
945 if (ret >= (ssize_t)PAGE_SIZE) {
946 printk("dev_attr_show: %pS returned bad count\n",
952 static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
953 const char *buf, size_t count)
955 struct device_attribute *dev_attr = to_dev_attr(attr);
956 struct device *dev = kobj_to_dev(kobj);
960 ret = dev_attr->store(dev, dev_attr, buf, count);
964 static const struct sysfs_ops dev_sysfs_ops = {
965 .show = dev_attr_show,
966 .store = dev_attr_store,
969 #define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
971 ssize_t device_store_ulong(struct device *dev,
972 struct device_attribute *attr,
973 const char *buf, size_t size)
975 struct dev_ext_attribute *ea = to_ext_attr(attr);
979 ret = kstrtoul(buf, 0, &new);
982 *(unsigned long *)(ea->var) = new;
983 /* Always return full write size even if we didn't consume all */
986 EXPORT_SYMBOL_GPL(device_store_ulong);
988 ssize_t device_show_ulong(struct device *dev,
989 struct device_attribute *attr,
992 struct dev_ext_attribute *ea = to_ext_attr(attr);
993 return snprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var));
995 EXPORT_SYMBOL_GPL(device_show_ulong);
997 ssize_t device_store_int(struct device *dev,
998 struct device_attribute *attr,
999 const char *buf, size_t size)
1001 struct dev_ext_attribute *ea = to_ext_attr(attr);
1005 ret = kstrtol(buf, 0, &new);
1009 if (new > INT_MAX || new < INT_MIN)
1011 *(int *)(ea->var) = new;
1012 /* Always return full write size even if we didn't consume all */
1015 EXPORT_SYMBOL_GPL(device_store_int);
1017 ssize_t device_show_int(struct device *dev,
1018 struct device_attribute *attr,
1021 struct dev_ext_attribute *ea = to_ext_attr(attr);
1023 return snprintf(buf, PAGE_SIZE, "%d\n", *(int *)(ea->var));
1025 EXPORT_SYMBOL_GPL(device_show_int);
1027 ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
1028 const char *buf, size_t size)
1030 struct dev_ext_attribute *ea = to_ext_attr(attr);
1032 if (strtobool(buf, ea->var) < 0)
1037 EXPORT_SYMBOL_GPL(device_store_bool);
1039 ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
1042 struct dev_ext_attribute *ea = to_ext_attr(attr);
1044 return snprintf(buf, PAGE_SIZE, "%d\n", *(bool *)(ea->var));
1046 EXPORT_SYMBOL_GPL(device_show_bool);
1049 * device_release - free device structure.
1050 * @kobj: device's kobject.
1052 * This is called once the reference count for the object
1053 * reaches 0. We forward the call to the device's release
1054 * method, which should handle actually freeing the structure.
1056 static void device_release(struct kobject *kobj)
1058 struct device *dev = kobj_to_dev(kobj);
1059 struct device_private *p = dev->p;
1062 * Some platform devices are driven without driver attached
1063 * and managed resources may have been acquired. Make sure
1064 * all resources are released.
1066 * Drivers still can add resources into device after device
1067 * is deleted but alive, so release devres here to avoid
1068 * possible memory leak.
1070 devres_release_all(dev);
1074 else if (dev->type && dev->type->release)
1075 dev->type->release(dev);
1076 else if (dev->class && dev->class->dev_release)
1077 dev->class->dev_release(dev);
1079 WARN(1, KERN_ERR "Device '%s' does not have a release() function, it is broken and must be fixed. See Documentation/kobject.txt.\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 INIT_LIST_HEAD(&dev->msi_list);
1687 INIT_LIST_HEAD(&dev->links.consumers);
1688 INIT_LIST_HEAD(&dev->links.suppliers);
1689 dev->links.status = DL_DEV_NO_DRIVER;
1691 EXPORT_SYMBOL_GPL(device_initialize);
1693 struct kobject *virtual_device_parent(struct device *dev)
1695 static struct kobject *virtual_dir = NULL;
1698 virtual_dir = kobject_create_and_add("virtual",
1699 &devices_kset->kobj);
1705 struct kobject kobj;
1706 struct class *class;
1709 #define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
1711 static void class_dir_release(struct kobject *kobj)
1713 struct class_dir *dir = to_class_dir(kobj);
1718 struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
1720 struct class_dir *dir = to_class_dir(kobj);
1721 return dir->class->ns_type;
1724 static struct kobj_type class_dir_ktype = {
1725 .release = class_dir_release,
1726 .sysfs_ops = &kobj_sysfs_ops,
1727 .child_ns_type = class_dir_child_ns_type
1730 static struct kobject *
1731 class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
1733 struct class_dir *dir;
1736 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1738 return ERR_PTR(-ENOMEM);
1741 kobject_init(&dir->kobj, &class_dir_ktype);
1743 dir->kobj.kset = &class->p->glue_dirs;
1745 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
1747 kobject_put(&dir->kobj);
1748 return ERR_PTR(retval);
1753 static DEFINE_MUTEX(gdp_mutex);
1755 static struct kobject *get_device_parent(struct device *dev,
1756 struct device *parent)
1759 struct kobject *kobj = NULL;
1760 struct kobject *parent_kobj;
1764 /* block disks show up in /sys/block */
1765 if (sysfs_deprecated && dev->class == &block_class) {
1766 if (parent && parent->class == &block_class)
1767 return &parent->kobj;
1768 return &block_class.p->subsys.kobj;
1773 * If we have no parent, we live in "virtual".
1774 * Class-devices with a non class-device as parent, live
1775 * in a "glue" directory to prevent namespace collisions.
1778 parent_kobj = virtual_device_parent(dev);
1779 else if (parent->class && !dev->class->ns_type)
1780 return &parent->kobj;
1782 parent_kobj = &parent->kobj;
1784 mutex_lock(&gdp_mutex);
1786 /* find our class-directory at the parent and reference it */
1787 spin_lock(&dev->class->p->glue_dirs.list_lock);
1788 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
1789 if (k->parent == parent_kobj) {
1790 kobj = kobject_get(k);
1793 spin_unlock(&dev->class->p->glue_dirs.list_lock);
1795 mutex_unlock(&gdp_mutex);
1799 /* or create a new class-directory at the parent device */
1800 k = class_dir_create_and_add(dev->class, parent_kobj);
1801 /* do not emit an uevent for this simple "glue" directory */
1802 mutex_unlock(&gdp_mutex);
1806 /* subsystems can specify a default root directory for their devices */
1807 if (!parent && dev->bus && dev->bus->dev_root)
1808 return &dev->bus->dev_root->kobj;
1811 return &parent->kobj;
1815 static inline bool live_in_glue_dir(struct kobject *kobj,
1818 if (!kobj || !dev->class ||
1819 kobj->kset != &dev->class->p->glue_dirs)
1824 static inline struct kobject *get_glue_dir(struct device *dev)
1826 return dev->kobj.parent;
1830 * make sure cleaning up dir as the last step, we need to make
1831 * sure .release handler of kobject is run with holding the
1834 static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
1836 /* see if we live in a "glue" directory */
1837 if (!live_in_glue_dir(glue_dir, dev))
1840 mutex_lock(&gdp_mutex);
1841 if (!kobject_has_children(glue_dir))
1842 kobject_del(glue_dir);
1843 kobject_put(glue_dir);
1844 mutex_unlock(&gdp_mutex);
1847 static int device_add_class_symlinks(struct device *dev)
1849 struct device_node *of_node = dev_of_node(dev);
1853 error = sysfs_create_link(&dev->kobj, of_node_kobj(of_node), "of_node");
1855 dev_warn(dev, "Error %d creating of_node link\n",error);
1856 /* An error here doesn't warrant bringing down the device */
1862 error = sysfs_create_link(&dev->kobj,
1863 &dev->class->p->subsys.kobj,
1868 if (dev->parent && device_is_not_partition(dev)) {
1869 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
1876 /* /sys/block has directories and does not need symlinks */
1877 if (sysfs_deprecated && dev->class == &block_class)
1881 /* link in the class directory pointing to the device */
1882 error = sysfs_create_link(&dev->class->p->subsys.kobj,
1883 &dev->kobj, dev_name(dev));
1890 sysfs_remove_link(&dev->kobj, "device");
1893 sysfs_remove_link(&dev->kobj, "subsystem");
1895 sysfs_remove_link(&dev->kobj, "of_node");
1899 static void device_remove_class_symlinks(struct device *dev)
1901 if (dev_of_node(dev))
1902 sysfs_remove_link(&dev->kobj, "of_node");
1907 if (dev->parent && device_is_not_partition(dev))
1908 sysfs_remove_link(&dev->kobj, "device");
1909 sysfs_remove_link(&dev->kobj, "subsystem");
1911 if (sysfs_deprecated && dev->class == &block_class)
1914 sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
1918 * dev_set_name - set a device name
1920 * @fmt: format string for the device's name
1922 int dev_set_name(struct device *dev, const char *fmt, ...)
1927 va_start(vargs, fmt);
1928 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
1932 EXPORT_SYMBOL_GPL(dev_set_name);
1935 * device_to_dev_kobj - select a /sys/dev/ directory for the device
1938 * By default we select char/ for new entries. Setting class->dev_obj
1939 * to NULL prevents an entry from being created. class->dev_kobj must
1940 * be set (or cleared) before any devices are registered to the class
1941 * otherwise device_create_sys_dev_entry() and
1942 * device_remove_sys_dev_entry() will disagree about the presence of
1945 static struct kobject *device_to_dev_kobj(struct device *dev)
1947 struct kobject *kobj;
1950 kobj = dev->class->dev_kobj;
1952 kobj = sysfs_dev_char_kobj;
1957 static int device_create_sys_dev_entry(struct device *dev)
1959 struct kobject *kobj = device_to_dev_kobj(dev);
1964 format_dev_t(devt_str, dev->devt);
1965 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
1971 static void device_remove_sys_dev_entry(struct device *dev)
1973 struct kobject *kobj = device_to_dev_kobj(dev);
1977 format_dev_t(devt_str, dev->devt);
1978 sysfs_remove_link(kobj, devt_str);
1982 static int device_private_init(struct device *dev)
1984 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
1987 dev->p->device = dev;
1988 klist_init(&dev->p->klist_children, klist_children_get,
1989 klist_children_put);
1990 INIT_LIST_HEAD(&dev->p->deferred_probe);
1995 * device_add - add device to device hierarchy.
1998 * This is part 2 of device_register(), though may be called
1999 * separately _iff_ device_initialize() has been called separately.
2001 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
2002 * to the global and sibling lists for the device, then
2003 * adds it to the other relevant subsystems of the driver model.
2005 * Do not call this routine or device_register() more than once for
2006 * any device structure. The driver model core is not designed to work
2007 * with devices that get unregistered and then spring back to life.
2008 * (Among other things, it's very hard to guarantee that all references
2009 * to the previous incarnation of @dev have been dropped.) Allocate
2010 * and register a fresh new struct device instead.
2012 * NOTE: _Never_ directly free @dev after calling this function, even
2013 * if it returned an error! Always use put_device() to give up your
2014 * reference instead.
2016 int device_add(struct device *dev)
2018 struct device *parent;
2019 struct kobject *kobj;
2020 struct class_interface *class_intf;
2021 int error = -EINVAL;
2022 struct kobject *glue_dir = NULL;
2024 dev = get_device(dev);
2029 error = device_private_init(dev);
2035 * for statically allocated devices, which should all be converted
2036 * some day, we need to initialize the name. We prevent reading back
2037 * the name, and force the use of dev_name()
2039 if (dev->init_name) {
2040 dev_set_name(dev, "%s", dev->init_name);
2041 dev->init_name = NULL;
2044 /* subsystems can specify simple device enumeration */
2045 if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
2046 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
2048 if (!dev_name(dev)) {
2053 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
2055 parent = get_device(dev->parent);
2056 kobj = get_device_parent(dev, parent);
2058 error = PTR_ERR(kobj);
2062 dev->kobj.parent = kobj;
2064 /* use parent numa_node */
2065 if (parent && (dev_to_node(dev) == NUMA_NO_NODE))
2066 set_dev_node(dev, dev_to_node(parent));
2068 /* first, register with generic layer. */
2069 /* we require the name to be set before, and pass NULL */
2070 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
2072 glue_dir = get_glue_dir(dev);
2076 /* notify platform of device entry */
2077 error = device_platform_notify(dev, KOBJ_ADD);
2079 goto platform_error;
2081 error = device_create_file(dev, &dev_attr_uevent);
2085 error = device_add_class_symlinks(dev);
2088 error = device_add_attrs(dev);
2091 error = bus_add_device(dev);
2094 error = dpm_sysfs_add(dev);
2099 if (MAJOR(dev->devt)) {
2100 error = device_create_file(dev, &dev_attr_dev);
2104 error = device_create_sys_dev_entry(dev);
2108 devtmpfs_create_node(dev);
2111 /* Notify clients of device addition. This call must come
2112 * after dpm_sysfs_add() and before kobject_uevent().
2115 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2116 BUS_NOTIFY_ADD_DEVICE, dev);
2118 kobject_uevent(&dev->kobj, KOBJ_ADD);
2119 bus_probe_device(dev);
2121 klist_add_tail(&dev->p->knode_parent,
2122 &parent->p->klist_children);
2125 mutex_lock(&dev->class->p->mutex);
2126 /* tie the class to the device */
2127 klist_add_tail(&dev->p->knode_class,
2128 &dev->class->p->klist_devices);
2130 /* notify any interfaces that the device is here */
2131 list_for_each_entry(class_intf,
2132 &dev->class->p->interfaces, node)
2133 if (class_intf->add_dev)
2134 class_intf->add_dev(dev, class_intf);
2135 mutex_unlock(&dev->class->p->mutex);
2141 if (MAJOR(dev->devt))
2142 device_remove_file(dev, &dev_attr_dev);
2144 device_pm_remove(dev);
2145 dpm_sysfs_remove(dev);
2147 bus_remove_device(dev);
2149 device_remove_attrs(dev);
2151 device_remove_class_symlinks(dev);
2153 device_remove_file(dev, &dev_attr_uevent);
2155 device_platform_notify(dev, KOBJ_REMOVE);
2157 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
2158 glue_dir = get_glue_dir(dev);
2159 kobject_del(&dev->kobj);
2161 cleanup_glue_dir(dev, glue_dir);
2169 EXPORT_SYMBOL_GPL(device_add);
2172 * device_register - register a device with the system.
2173 * @dev: pointer to the device structure
2175 * This happens in two clean steps - initialize the device
2176 * and add it to the system. The two steps can be called
2177 * separately, but this is the easiest and most common.
2178 * I.e. you should only call the two helpers separately if
2179 * have a clearly defined need to use and refcount the device
2180 * before it is added to the hierarchy.
2182 * For more information, see the kerneldoc for device_initialize()
2185 * NOTE: _Never_ directly free @dev after calling this function, even
2186 * if it returned an error! Always use put_device() to give up the
2187 * reference initialized in this function instead.
2189 int device_register(struct device *dev)
2191 device_initialize(dev);
2192 return device_add(dev);
2194 EXPORT_SYMBOL_GPL(device_register);
2197 * get_device - increment reference count for device.
2200 * This simply forwards the call to kobject_get(), though
2201 * we do take care to provide for the case that we get a NULL
2202 * pointer passed in.
2204 struct device *get_device(struct device *dev)
2206 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
2208 EXPORT_SYMBOL_GPL(get_device);
2211 * put_device - decrement reference count.
2212 * @dev: device in question.
2214 void put_device(struct device *dev)
2216 /* might_sleep(); */
2218 kobject_put(&dev->kobj);
2220 EXPORT_SYMBOL_GPL(put_device);
2223 * device_del - delete device from system.
2226 * This is the first part of the device unregistration
2227 * sequence. This removes the device from the lists we control
2228 * from here, has it removed from the other driver model
2229 * subsystems it was added to in device_add(), and removes it
2230 * from the kobject hierarchy.
2232 * NOTE: this should be called manually _iff_ device_add() was
2233 * also called manually.
2235 void device_del(struct device *dev)
2237 struct device *parent = dev->parent;
2238 struct kobject *glue_dir = NULL;
2239 struct class_interface *class_intf;
2242 * Hold the device lock and set the "dead" flag to guarantee that
2243 * the update behavior is consistent with the other bitfields near
2244 * it and that we cannot have an asynchronous probe routine trying
2245 * to run while we are tearing out the bus/class/sysfs from
2246 * underneath the device.
2249 dev->p->dead = true;
2252 /* Notify clients of device removal. This call must come
2253 * before dpm_sysfs_remove().
2256 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2257 BUS_NOTIFY_DEL_DEVICE, dev);
2259 dpm_sysfs_remove(dev);
2261 klist_del(&dev->p->knode_parent);
2262 if (MAJOR(dev->devt)) {
2263 devtmpfs_delete_node(dev);
2264 device_remove_sys_dev_entry(dev);
2265 device_remove_file(dev, &dev_attr_dev);
2268 device_remove_class_symlinks(dev);
2270 mutex_lock(&dev->class->p->mutex);
2271 /* notify any interfaces that the device is now gone */
2272 list_for_each_entry(class_intf,
2273 &dev->class->p->interfaces, node)
2274 if (class_intf->remove_dev)
2275 class_intf->remove_dev(dev, class_intf);
2276 /* remove the device from the class list */
2277 klist_del(&dev->p->knode_class);
2278 mutex_unlock(&dev->class->p->mutex);
2280 device_remove_file(dev, &dev_attr_uevent);
2281 device_remove_attrs(dev);
2282 bus_remove_device(dev);
2283 device_pm_remove(dev);
2284 driver_deferred_probe_del(dev);
2285 device_platform_notify(dev, KOBJ_REMOVE);
2286 device_remove_properties(dev);
2287 device_links_purge(dev);
2290 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2291 BUS_NOTIFY_REMOVED_DEVICE, dev);
2292 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
2293 glue_dir = get_glue_dir(dev);
2294 kobject_del(&dev->kobj);
2295 cleanup_glue_dir(dev, glue_dir);
2298 EXPORT_SYMBOL_GPL(device_del);
2301 * device_unregister - unregister device from system.
2302 * @dev: device going away.
2304 * We do this in two parts, like we do device_register(). First,
2305 * we remove it from all the subsystems with device_del(), then
2306 * we decrement the reference count via put_device(). If that
2307 * is the final reference count, the device will be cleaned up
2308 * via device_release() above. Otherwise, the structure will
2309 * stick around until the final reference to the device is dropped.
2311 void device_unregister(struct device *dev)
2313 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
2317 EXPORT_SYMBOL_GPL(device_unregister);
2319 static struct device *prev_device(struct klist_iter *i)
2321 struct klist_node *n = klist_prev(i);
2322 struct device *dev = NULL;
2323 struct device_private *p;
2326 p = to_device_private_parent(n);
2332 static struct device *next_device(struct klist_iter *i)
2334 struct klist_node *n = klist_next(i);
2335 struct device *dev = NULL;
2336 struct device_private *p;
2339 p = to_device_private_parent(n);
2346 * device_get_devnode - path of device node file
2348 * @mode: returned file access mode
2349 * @uid: returned file owner
2350 * @gid: returned file group
2351 * @tmp: possibly allocated string
2353 * Return the relative path of a possible device node.
2354 * Non-default names may need to allocate a memory to compose
2355 * a name. This memory is returned in tmp and needs to be
2356 * freed by the caller.
2358 const char *device_get_devnode(struct device *dev,
2359 umode_t *mode, kuid_t *uid, kgid_t *gid,
2366 /* the device type may provide a specific name */
2367 if (dev->type && dev->type->devnode)
2368 *tmp = dev->type->devnode(dev, mode, uid, gid);
2372 /* the class may provide a specific name */
2373 if (dev->class && dev->class->devnode)
2374 *tmp = dev->class->devnode(dev, mode);
2378 /* return name without allocation, tmp == NULL */
2379 if (strchr(dev_name(dev), '!') == NULL)
2380 return dev_name(dev);
2382 /* replace '!' in the name with '/' */
2383 s = kstrdup(dev_name(dev), GFP_KERNEL);
2386 strreplace(s, '!', '/');
2391 * device_for_each_child - device child iterator.
2392 * @parent: parent struct device.
2393 * @fn: function to be called for each device.
2394 * @data: data for the callback.
2396 * Iterate over @parent's child devices, and call @fn for each,
2399 * We check the return of @fn each time. If it returns anything
2400 * other than 0, we break out and return that value.
2402 int device_for_each_child(struct device *parent, void *data,
2403 int (*fn)(struct device *dev, void *data))
2405 struct klist_iter i;
2406 struct device *child;
2412 klist_iter_init(&parent->p->klist_children, &i);
2413 while (!error && (child = next_device(&i)))
2414 error = fn(child, data);
2415 klist_iter_exit(&i);
2418 EXPORT_SYMBOL_GPL(device_for_each_child);
2421 * device_for_each_child_reverse - device child iterator in reversed order.
2422 * @parent: parent struct device.
2423 * @fn: function to be called for each device.
2424 * @data: data for the callback.
2426 * Iterate over @parent's child devices, and call @fn for each,
2429 * We check the return of @fn each time. If it returns anything
2430 * other than 0, we break out and return that value.
2432 int device_for_each_child_reverse(struct device *parent, void *data,
2433 int (*fn)(struct device *dev, void *data))
2435 struct klist_iter i;
2436 struct device *child;
2442 klist_iter_init(&parent->p->klist_children, &i);
2443 while ((child = prev_device(&i)) && !error)
2444 error = fn(child, data);
2445 klist_iter_exit(&i);
2448 EXPORT_SYMBOL_GPL(device_for_each_child_reverse);
2451 * device_find_child - device iterator for locating a particular device.
2452 * @parent: parent struct device
2453 * @match: Callback function to check device
2454 * @data: Data to pass to match function
2456 * This is similar to the device_for_each_child() function above, but it
2457 * returns a reference to a device that is 'found' for later use, as
2458 * determined by the @match callback.
2460 * The callback should return 0 if the device doesn't match and non-zero
2461 * if it does. If the callback returns non-zero and a reference to the
2462 * current device can be obtained, this function will return to the caller
2463 * and not iterate over any more devices.
2465 * NOTE: you will need to drop the reference with put_device() after use.
2467 struct device *device_find_child(struct device *parent, void *data,
2468 int (*match)(struct device *dev, void *data))
2470 struct klist_iter i;
2471 struct device *child;
2476 klist_iter_init(&parent->p->klist_children, &i);
2477 while ((child = next_device(&i)))
2478 if (match(child, data) && get_device(child))
2480 klist_iter_exit(&i);
2483 EXPORT_SYMBOL_GPL(device_find_child);
2485 int __init devices_init(void)
2487 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
2490 dev_kobj = kobject_create_and_add("dev", NULL);
2493 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
2494 if (!sysfs_dev_block_kobj)
2495 goto block_kobj_err;
2496 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
2497 if (!sysfs_dev_char_kobj)
2503 kobject_put(sysfs_dev_block_kobj);
2505 kobject_put(dev_kobj);
2507 kset_unregister(devices_kset);
2511 static int device_check_offline(struct device *dev, void *not_used)
2515 ret = device_for_each_child(dev, NULL, device_check_offline);
2519 return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
2523 * device_offline - Prepare the device for hot-removal.
2524 * @dev: Device to be put offline.
2526 * Execute the device bus type's .offline() callback, if present, to prepare
2527 * the device for a subsequent hot-removal. If that succeeds, the device must
2528 * not be used until either it is removed or its bus type's .online() callback
2531 * Call under device_hotplug_lock.
2533 int device_offline(struct device *dev)
2537 if (dev->offline_disabled)
2540 ret = device_for_each_child(dev, NULL, device_check_offline);
2545 if (device_supports_offline(dev)) {
2549 ret = dev->bus->offline(dev);
2551 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
2552 dev->offline = true;
2562 * device_online - Put the device back online after successful device_offline().
2563 * @dev: Device to be put back online.
2565 * If device_offline() has been successfully executed for @dev, but the device
2566 * has not been removed subsequently, execute its bus type's .online() callback
2567 * to indicate that the device can be used again.
2569 * Call under device_hotplug_lock.
2571 int device_online(struct device *dev)
2576 if (device_supports_offline(dev)) {
2578 ret = dev->bus->online(dev);
2580 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
2581 dev->offline = false;
2592 struct root_device {
2594 struct module *owner;
2597 static inline struct root_device *to_root_device(struct device *d)
2599 return container_of(d, struct root_device, dev);
2602 static void root_device_release(struct device *dev)
2604 kfree(to_root_device(dev));
2608 * __root_device_register - allocate and register a root device
2609 * @name: root device name
2610 * @owner: owner module of the root device, usually THIS_MODULE
2612 * This function allocates a root device and registers it
2613 * using device_register(). In order to free the returned
2614 * device, use root_device_unregister().
2616 * Root devices are dummy devices which allow other devices
2617 * to be grouped under /sys/devices. Use this function to
2618 * allocate a root device and then use it as the parent of
2619 * any device which should appear under /sys/devices/{name}
2621 * The /sys/devices/{name} directory will also contain a
2622 * 'module' symlink which points to the @owner directory
2625 * Returns &struct device pointer on success, or ERR_PTR() on error.
2627 * Note: You probably want to use root_device_register().
2629 struct device *__root_device_register(const char *name, struct module *owner)
2631 struct root_device *root;
2634 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
2636 return ERR_PTR(err);
2638 err = dev_set_name(&root->dev, "%s", name);
2641 return ERR_PTR(err);
2644 root->dev.release = root_device_release;
2646 err = device_register(&root->dev);
2648 put_device(&root->dev);
2649 return ERR_PTR(err);
2652 #ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
2654 struct module_kobject *mk = &owner->mkobj;
2656 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
2658 device_unregister(&root->dev);
2659 return ERR_PTR(err);
2661 root->owner = owner;
2667 EXPORT_SYMBOL_GPL(__root_device_register);
2670 * root_device_unregister - unregister and free a root device
2671 * @dev: device going away
2673 * This function unregisters and cleans up a device that was created by
2674 * root_device_register().
2676 void root_device_unregister(struct device *dev)
2678 struct root_device *root = to_root_device(dev);
2681 sysfs_remove_link(&root->dev.kobj, "module");
2683 device_unregister(dev);
2685 EXPORT_SYMBOL_GPL(root_device_unregister);
2688 static void device_create_release(struct device *dev)
2690 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
2694 static __printf(6, 0) struct device *
2695 device_create_groups_vargs(struct class *class, struct device *parent,
2696 dev_t devt, void *drvdata,
2697 const struct attribute_group **groups,
2698 const char *fmt, va_list args)
2700 struct device *dev = NULL;
2701 int retval = -ENODEV;
2703 if (class == NULL || IS_ERR(class))
2706 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2712 device_initialize(dev);
2715 dev->parent = parent;
2716 dev->groups = groups;
2717 dev->release = device_create_release;
2718 dev_set_drvdata(dev, drvdata);
2720 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
2724 retval = device_add(dev);
2732 return ERR_PTR(retval);
2736 * device_create_vargs - creates a device and registers it with sysfs
2737 * @class: pointer to the struct class that this device should be registered to
2738 * @parent: pointer to the parent struct device of this new device, if any
2739 * @devt: the dev_t for the char device to be added
2740 * @drvdata: the data to be added to the device for callbacks
2741 * @fmt: string for the device's name
2742 * @args: va_list for the device's name
2744 * This function can be used by char device classes. A struct device
2745 * will be created in sysfs, registered to the specified class.
2747 * A "dev" file will be created, showing the dev_t for the device, if
2748 * the dev_t is not 0,0.
2749 * If a pointer to a parent struct device is passed in, the newly created
2750 * struct device will be a child of that device in sysfs.
2751 * The pointer to the struct device will be returned from the call.
2752 * Any further sysfs files that might be required can be created using this
2755 * Returns &struct device pointer on success, or ERR_PTR() on error.
2757 * Note: the struct class passed to this function must have previously
2758 * been created with a call to class_create().
2760 struct device *device_create_vargs(struct class *class, struct device *parent,
2761 dev_t devt, void *drvdata, const char *fmt,
2764 return device_create_groups_vargs(class, parent, devt, drvdata, NULL,
2767 EXPORT_SYMBOL_GPL(device_create_vargs);
2770 * device_create - creates a device and registers it with sysfs
2771 * @class: pointer to the struct class that this device should be registered to
2772 * @parent: pointer to the parent struct device of this new device, if any
2773 * @devt: the dev_t for the char device to be added
2774 * @drvdata: the data to be added to the device for callbacks
2775 * @fmt: string for the device's name
2777 * This function can be used by char device classes. A struct device
2778 * will be created in sysfs, registered to the specified class.
2780 * A "dev" file will be created, showing the dev_t for the device, if
2781 * the dev_t is not 0,0.
2782 * If a pointer to a parent struct device is passed in, the newly created
2783 * struct device will be a child of that device in sysfs.
2784 * The pointer to the struct device will be returned from the call.
2785 * Any further sysfs files that might be required can be created using this
2788 * Returns &struct device pointer on success, or ERR_PTR() on error.
2790 * Note: the struct class passed to this function must have previously
2791 * been created with a call to class_create().
2793 struct device *device_create(struct class *class, struct device *parent,
2794 dev_t devt, void *drvdata, const char *fmt, ...)
2799 va_start(vargs, fmt);
2800 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
2804 EXPORT_SYMBOL_GPL(device_create);
2807 * device_create_with_groups - creates a device and registers it with sysfs
2808 * @class: pointer to the struct class that this device should be registered to
2809 * @parent: pointer to the parent struct device of this new device, if any
2810 * @devt: the dev_t for the char device to be added
2811 * @drvdata: the data to be added to the device for callbacks
2812 * @groups: NULL-terminated list of attribute groups to be created
2813 * @fmt: string for the device's name
2815 * This function can be used by char device classes. A struct device
2816 * will be created in sysfs, registered to the specified class.
2817 * Additional attributes specified in the groups parameter will also
2818 * be created automatically.
2820 * A "dev" file will be created, showing the dev_t for the device, if
2821 * the dev_t is not 0,0.
2822 * If a pointer to a parent struct device is passed in, the newly created
2823 * struct device will be a child of that device in sysfs.
2824 * The pointer to the struct device will be returned from the call.
2825 * Any further sysfs files that might be required can be created using this
2828 * Returns &struct device pointer on success, or ERR_PTR() on error.
2830 * Note: the struct class passed to this function must have previously
2831 * been created with a call to class_create().
2833 struct device *device_create_with_groups(struct class *class,
2834 struct device *parent, dev_t devt,
2836 const struct attribute_group **groups,
2837 const char *fmt, ...)
2842 va_start(vargs, fmt);
2843 dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
2848 EXPORT_SYMBOL_GPL(device_create_with_groups);
2850 static int __match_devt(struct device *dev, const void *data)
2852 const dev_t *devt = data;
2854 return dev->devt == *devt;
2858 * device_destroy - removes a device that was created with device_create()
2859 * @class: pointer to the struct class that this device was registered with
2860 * @devt: the dev_t of the device that was previously registered
2862 * This call unregisters and cleans up a device that was created with a
2863 * call to device_create().
2865 void device_destroy(struct class *class, dev_t devt)
2869 dev = class_find_device(class, NULL, &devt, __match_devt);
2872 device_unregister(dev);
2875 EXPORT_SYMBOL_GPL(device_destroy);
2878 * device_rename - renames a device
2879 * @dev: the pointer to the struct device to be renamed
2880 * @new_name: the new name of the device
2882 * It is the responsibility of the caller to provide mutual
2883 * exclusion between two different calls of device_rename
2884 * on the same device to ensure that new_name is valid and
2885 * won't conflict with other devices.
2887 * Note: Don't call this function. Currently, the networking layer calls this
2888 * function, but that will change. The following text from Kay Sievers offers
2891 * Renaming devices is racy at many levels, symlinks and other stuff are not
2892 * replaced atomically, and you get a "move" uevent, but it's not easy to
2893 * connect the event to the old and new device. Device nodes are not renamed at
2894 * all, there isn't even support for that in the kernel now.
2896 * In the meantime, during renaming, your target name might be taken by another
2897 * driver, creating conflicts. Or the old name is taken directly after you
2898 * renamed it -- then you get events for the same DEVPATH, before you even see
2899 * the "move" event. It's just a mess, and nothing new should ever rely on
2900 * kernel device renaming. Besides that, it's not even implemented now for
2901 * other things than (driver-core wise very simple) network devices.
2903 * We are currently about to change network renaming in udev to completely
2904 * disallow renaming of devices in the same namespace as the kernel uses,
2905 * because we can't solve the problems properly, that arise with swapping names
2906 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
2907 * be allowed to some other name than eth[0-9]*, for the aforementioned
2910 * Make up a "real" name in the driver before you register anything, or add
2911 * some other attributes for userspace to find the device, or use udev to add
2912 * symlinks -- but never rename kernel devices later, it's a complete mess. We
2913 * don't even want to get into that and try to implement the missing pieces in
2914 * the core. We really have other pieces to fix in the driver core mess. :)
2916 int device_rename(struct device *dev, const char *new_name)
2918 struct kobject *kobj = &dev->kobj;
2919 char *old_device_name = NULL;
2922 dev = get_device(dev);
2926 dev_dbg(dev, "renaming to %s\n", new_name);
2928 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
2929 if (!old_device_name) {
2935 error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj,
2936 kobj, old_device_name,
2937 new_name, kobject_namespace(kobj));
2942 error = kobject_rename(kobj, new_name);
2949 kfree(old_device_name);
2953 EXPORT_SYMBOL_GPL(device_rename);
2955 static int device_move_class_links(struct device *dev,
2956 struct device *old_parent,
2957 struct device *new_parent)
2962 sysfs_remove_link(&dev->kobj, "device");
2964 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
2970 * device_move - moves a device to a new parent
2971 * @dev: the pointer to the struct device to be moved
2972 * @new_parent: the new parent of the device (can be NULL)
2973 * @dpm_order: how to reorder the dpm_list
2975 int device_move(struct device *dev, struct device *new_parent,
2976 enum dpm_order dpm_order)
2979 struct device *old_parent;
2980 struct kobject *new_parent_kobj;
2982 dev = get_device(dev);
2987 new_parent = get_device(new_parent);
2988 new_parent_kobj = get_device_parent(dev, new_parent);
2989 if (IS_ERR(new_parent_kobj)) {
2990 error = PTR_ERR(new_parent_kobj);
2991 put_device(new_parent);
2995 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
2996 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
2997 error = kobject_move(&dev->kobj, new_parent_kobj);
2999 cleanup_glue_dir(dev, new_parent_kobj);
3000 put_device(new_parent);
3003 old_parent = dev->parent;
3004 dev->parent = new_parent;
3006 klist_remove(&dev->p->knode_parent);
3008 klist_add_tail(&dev->p->knode_parent,
3009 &new_parent->p->klist_children);
3010 set_dev_node(dev, dev_to_node(new_parent));
3014 error = device_move_class_links(dev, old_parent, new_parent);
3016 /* We ignore errors on cleanup since we're hosed anyway... */
3017 device_move_class_links(dev, new_parent, old_parent);
3018 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
3020 klist_remove(&dev->p->knode_parent);
3021 dev->parent = old_parent;
3023 klist_add_tail(&dev->p->knode_parent,
3024 &old_parent->p->klist_children);
3025 set_dev_node(dev, dev_to_node(old_parent));
3028 cleanup_glue_dir(dev, new_parent_kobj);
3029 put_device(new_parent);
3033 switch (dpm_order) {
3034 case DPM_ORDER_NONE:
3036 case DPM_ORDER_DEV_AFTER_PARENT:
3037 device_pm_move_after(dev, new_parent);
3038 devices_kset_move_after(dev, new_parent);
3040 case DPM_ORDER_PARENT_BEFORE_DEV:
3041 device_pm_move_before(new_parent, dev);
3042 devices_kset_move_before(new_parent, dev);
3044 case DPM_ORDER_DEV_LAST:
3045 device_pm_move_last(dev);
3046 devices_kset_move_last(dev);
3050 put_device(old_parent);
3056 EXPORT_SYMBOL_GPL(device_move);
3059 * device_shutdown - call ->shutdown() on each device to shutdown.
3061 void device_shutdown(void)
3063 struct device *dev, *parent;
3065 wait_for_device_probe();
3066 device_block_probing();
3068 spin_lock(&devices_kset->list_lock);
3070 * Walk the devices list backward, shutting down each in turn.
3071 * Beware that device unplug events may also start pulling
3072 * devices offline, even as the system is shutting down.
3074 while (!list_empty(&devices_kset->list)) {
3075 dev = list_entry(devices_kset->list.prev, struct device,
3079 * hold reference count of device's parent to
3080 * prevent it from being freed because parent's
3081 * lock is to be held
3083 parent = get_device(dev->parent);
3086 * Make sure the device is off the kset list, in the
3087 * event that dev->*->shutdown() doesn't remove it.
3089 list_del_init(&dev->kobj.entry);
3090 spin_unlock(&devices_kset->list_lock);
3092 /* hold lock to avoid race with probe/release */
3094 device_lock(parent);
3097 /* Don't allow any more runtime suspends */
3098 pm_runtime_get_noresume(dev);
3099 pm_runtime_barrier(dev);
3101 if (dev->class && dev->class->shutdown_pre) {
3103 dev_info(dev, "shutdown_pre\n");
3104 dev->class->shutdown_pre(dev);
3106 if (dev->bus && dev->bus->shutdown) {
3108 dev_info(dev, "shutdown\n");
3109 dev->bus->shutdown(dev);
3110 } else if (dev->driver && dev->driver->shutdown) {
3112 dev_info(dev, "shutdown\n");
3113 dev->driver->shutdown(dev);
3118 device_unlock(parent);
3123 spin_lock(&devices_kset->list_lock);
3125 spin_unlock(&devices_kset->list_lock);
3129 * Device logging functions
3132 #ifdef CONFIG_PRINTK
3134 create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
3140 subsys = dev->class->name;
3142 subsys = dev->bus->name;
3146 pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys);
3151 * Add device identifier DEVICE=:
3155 * +sound:card0 subsystem:devname
3157 if (MAJOR(dev->devt)) {
3160 if (strcmp(subsys, "block") == 0)
3165 pos += snprintf(hdr + pos, hdrlen - pos,
3167 c, MAJOR(dev->devt), MINOR(dev->devt));
3168 } else if (strcmp(subsys, "net") == 0) {
3169 struct net_device *net = to_net_dev(dev);
3172 pos += snprintf(hdr + pos, hdrlen - pos,
3173 "DEVICE=n%u", net->ifindex);
3176 pos += snprintf(hdr + pos, hdrlen - pos,
3177 "DEVICE=+%s:%s", subsys, dev_name(dev));
3186 dev_WARN(dev, "device/subsystem name too long");
3190 int dev_vprintk_emit(int level, const struct device *dev,
3191 const char *fmt, va_list args)
3196 hdrlen = create_syslog_header(dev, hdr, sizeof(hdr));
3198 return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args);
3200 EXPORT_SYMBOL(dev_vprintk_emit);
3202 int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
3207 va_start(args, fmt);
3209 r = dev_vprintk_emit(level, dev, fmt, args);
3215 EXPORT_SYMBOL(dev_printk_emit);
3217 static void __dev_printk(const char *level, const struct device *dev,
3218 struct va_format *vaf)
3221 dev_printk_emit(level[1] - '0', dev, "%s %s: %pV",
3222 dev_driver_string(dev), dev_name(dev), vaf);
3224 printk("%s(NULL device *): %pV", level, vaf);
3227 void dev_printk(const char *level, const struct device *dev,
3228 const char *fmt, ...)
3230 struct va_format vaf;
3233 va_start(args, fmt);
3238 __dev_printk(level, dev, &vaf);
3242 EXPORT_SYMBOL(dev_printk);
3244 #define define_dev_printk_level(func, kern_level) \
3245 void func(const struct device *dev, const char *fmt, ...) \
3247 struct va_format vaf; \
3250 va_start(args, fmt); \
3255 __dev_printk(kern_level, dev, &vaf); \
3259 EXPORT_SYMBOL(func);
3261 define_dev_printk_level(_dev_emerg, KERN_EMERG);
3262 define_dev_printk_level(_dev_alert, KERN_ALERT);
3263 define_dev_printk_level(_dev_crit, KERN_CRIT);
3264 define_dev_printk_level(_dev_err, KERN_ERR);
3265 define_dev_printk_level(_dev_warn, KERN_WARNING);
3266 define_dev_printk_level(_dev_notice, KERN_NOTICE);
3267 define_dev_printk_level(_dev_info, KERN_INFO);
3271 static inline bool fwnode_is_primary(struct fwnode_handle *fwnode)
3273 return fwnode && !IS_ERR(fwnode->secondary);
3277 * set_primary_fwnode - Change the primary firmware node of a given device.
3278 * @dev: Device to handle.
3279 * @fwnode: New primary firmware node of the device.
3281 * Set the device's firmware node pointer to @fwnode, but if a secondary
3282 * firmware node of the device is present, preserve it.
3284 void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
3287 struct fwnode_handle *fn = dev->fwnode;
3289 if (fwnode_is_primary(fn))
3293 WARN_ON(fwnode->secondary);
3294 fwnode->secondary = fn;
3296 dev->fwnode = fwnode;
3298 dev->fwnode = fwnode_is_primary(dev->fwnode) ?
3299 dev->fwnode->secondary : NULL;
3302 EXPORT_SYMBOL_GPL(set_primary_fwnode);
3305 * set_secondary_fwnode - Change the secondary firmware node of a given device.
3306 * @dev: Device to handle.
3307 * @fwnode: New secondary firmware node of the device.
3309 * If a primary firmware node of the device is present, set its secondary
3310 * pointer to @fwnode. Otherwise, set the device's firmware node pointer to
3313 void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
3316 fwnode->secondary = ERR_PTR(-ENODEV);
3318 if (fwnode_is_primary(dev->fwnode))
3319 dev->fwnode->secondary = fwnode;
3321 dev->fwnode = fwnode;
3325 * device_set_of_node_from_dev - reuse device-tree node of another device
3326 * @dev: device whose device-tree node is being set
3327 * @dev2: device whose device-tree node is being reused
3329 * Takes another reference to the new device-tree node after first dropping
3330 * any reference held to the old node.
3332 void device_set_of_node_from_dev(struct device *dev, const struct device *dev2)
3334 of_node_put(dev->of_node);
3335 dev->of_node = of_node_get(dev2->of_node);
3336 dev->of_node_reused = true;
3338 EXPORT_SYMBOL_GPL(device_set_of_node_from_dev);