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. */
48 static LIST_HEAD(wait_for_suppliers);
49 static DEFINE_MUTEX(wfs_lock);
50 static LIST_HEAD(deferred_sync);
51 static unsigned int defer_sync_state_count = 1;
52 static unsigned int defer_fw_devlink_count;
53 static DEFINE_MUTEX(defer_fw_devlink_lock);
54 static bool fw_devlink_is_permissive(void);
57 static DEFINE_MUTEX(device_links_lock);
58 DEFINE_STATIC_SRCU(device_links_srcu);
60 static inline void device_links_write_lock(void)
62 mutex_lock(&device_links_lock);
65 static inline void device_links_write_unlock(void)
67 mutex_unlock(&device_links_lock);
70 int device_links_read_lock(void) __acquires(&device_links_srcu)
72 return srcu_read_lock(&device_links_srcu);
75 void device_links_read_unlock(int idx) __releases(&device_links_srcu)
77 srcu_read_unlock(&device_links_srcu, idx);
80 int device_links_read_lock_held(void)
82 return srcu_read_lock_held(&device_links_srcu);
84 #else /* !CONFIG_SRCU */
85 static DECLARE_RWSEM(device_links_lock);
87 static inline void device_links_write_lock(void)
89 down_write(&device_links_lock);
92 static inline void device_links_write_unlock(void)
94 up_write(&device_links_lock);
97 int device_links_read_lock(void)
99 down_read(&device_links_lock);
103 void device_links_read_unlock(int not_used)
105 up_read(&device_links_lock);
108 #ifdef CONFIG_DEBUG_LOCK_ALLOC
109 int device_links_read_lock_held(void)
111 return lockdep_is_held(&device_links_lock);
114 #endif /* !CONFIG_SRCU */
117 * device_is_dependent - Check if one device depends on another one
118 * @dev: Device to check dependencies for.
119 * @target: Device to check against.
121 * Check if @target depends on @dev or any device dependent on it (its child or
122 * its consumer etc). Return 1 if that is the case or 0 otherwise.
124 static int device_is_dependent(struct device *dev, void *target)
126 struct device_link *link;
132 ret = device_for_each_child(dev, target, device_is_dependent);
136 list_for_each_entry(link, &dev->links.consumers, s_node) {
137 if (link->flags == (DL_FLAG_SYNC_STATE_ONLY | DL_FLAG_MANAGED))
140 if (link->consumer == target)
143 ret = device_is_dependent(link->consumer, target);
150 static void device_link_init_status(struct device_link *link,
151 struct device *consumer,
152 struct device *supplier)
154 switch (supplier->links.status) {
156 switch (consumer->links.status) {
159 * A consumer driver can create a link to a supplier
160 * that has not completed its probing yet as long as it
161 * knows that the supplier is already functional (for
162 * example, it has just acquired some resources from the
165 link->status = DL_STATE_CONSUMER_PROBE;
168 link->status = DL_STATE_DORMANT;
172 case DL_DEV_DRIVER_BOUND:
173 switch (consumer->links.status) {
175 link->status = DL_STATE_CONSUMER_PROBE;
177 case DL_DEV_DRIVER_BOUND:
178 link->status = DL_STATE_ACTIVE;
181 link->status = DL_STATE_AVAILABLE;
185 case DL_DEV_UNBINDING:
186 link->status = DL_STATE_SUPPLIER_UNBIND;
189 link->status = DL_STATE_DORMANT;
194 static int device_reorder_to_tail(struct device *dev, void *not_used)
196 struct device_link *link;
199 * Devices that have not been registered yet will be put to the ends
200 * of the lists during the registration, so skip them here.
202 if (device_is_registered(dev))
203 devices_kset_move_last(dev);
205 if (device_pm_initialized(dev))
206 device_pm_move_last(dev);
208 device_for_each_child(dev, NULL, device_reorder_to_tail);
209 list_for_each_entry(link, &dev->links.consumers, s_node) {
210 if (link->flags == (DL_FLAG_SYNC_STATE_ONLY | DL_FLAG_MANAGED))
212 device_reorder_to_tail(link->consumer, NULL);
219 * device_pm_move_to_tail - Move set of devices to the end of device lists
220 * @dev: Device to move
222 * This is a device_reorder_to_tail() wrapper taking the requisite locks.
224 * It moves the @dev along with all of its children and all of its consumers
225 * to the ends of the device_kset and dpm_list, recursively.
227 void device_pm_move_to_tail(struct device *dev)
231 idx = device_links_read_lock();
233 device_reorder_to_tail(dev, NULL);
235 device_links_read_unlock(idx);
238 #define DL_MANAGED_LINK_FLAGS (DL_FLAG_AUTOREMOVE_CONSUMER | \
239 DL_FLAG_AUTOREMOVE_SUPPLIER | \
240 DL_FLAG_AUTOPROBE_CONSUMER | \
241 DL_FLAG_SYNC_STATE_ONLY)
243 #define DL_ADD_VALID_FLAGS (DL_MANAGED_LINK_FLAGS | DL_FLAG_STATELESS | \
244 DL_FLAG_PM_RUNTIME | DL_FLAG_RPM_ACTIVE)
247 * device_link_add - Create a link between two devices.
248 * @consumer: Consumer end of the link.
249 * @supplier: Supplier end of the link.
250 * @flags: Link flags.
252 * The caller is responsible for the proper synchronization of the link creation
253 * with runtime PM. First, setting the DL_FLAG_PM_RUNTIME flag will cause the
254 * runtime PM framework to take the link into account. Second, if the
255 * DL_FLAG_RPM_ACTIVE flag is set in addition to it, the supplier devices will
256 * be forced into the active metastate and reference-counted upon the creation
257 * of the link. If DL_FLAG_PM_RUNTIME is not set, DL_FLAG_RPM_ACTIVE will be
260 * If DL_FLAG_STATELESS is set in @flags, the caller of this function is
261 * expected to release the link returned by it directly with the help of either
262 * device_link_del() or device_link_remove().
264 * If that flag is not set, however, the caller of this function is handing the
265 * management of the link over to the driver core entirely and its return value
266 * can only be used to check whether or not the link is present. In that case,
267 * the DL_FLAG_AUTOREMOVE_CONSUMER and DL_FLAG_AUTOREMOVE_SUPPLIER device link
268 * flags can be used to indicate to the driver core when the link can be safely
269 * deleted. Namely, setting one of them in @flags indicates to the driver core
270 * that the link is not going to be used (by the given caller of this function)
271 * after unbinding the consumer or supplier driver, respectively, from its
272 * device, so the link can be deleted at that point. If none of them is set,
273 * the link will be maintained until one of the devices pointed to by it (either
274 * the consumer or the supplier) is unregistered.
276 * Also, if DL_FLAG_STATELESS, DL_FLAG_AUTOREMOVE_CONSUMER and
277 * DL_FLAG_AUTOREMOVE_SUPPLIER are not set in @flags (that is, a persistent
278 * managed device link is being added), the DL_FLAG_AUTOPROBE_CONSUMER flag can
279 * be used to request the driver core to automaticall probe for a consmer
280 * driver after successfully binding a driver to the supplier device.
282 * The combination of DL_FLAG_STATELESS and one of DL_FLAG_AUTOREMOVE_CONSUMER,
283 * DL_FLAG_AUTOREMOVE_SUPPLIER, or DL_FLAG_AUTOPROBE_CONSUMER set in @flags at
284 * the same time is invalid and will cause NULL to be returned upfront.
285 * However, if a device link between the given @consumer and @supplier pair
286 * exists already when this function is called for them, the existing link will
287 * be returned regardless of its current type and status (the link's flags may
288 * be modified then). The caller of this function is then expected to treat
289 * the link as though it has just been created, so (in particular) if
290 * DL_FLAG_STATELESS was passed in @flags, the link needs to be released
291 * explicitly when not needed any more (as stated above).
293 * A side effect of the link creation is re-ordering of dpm_list and the
294 * devices_kset list by moving the consumer device and all devices depending
295 * on it to the ends of these lists (that does not happen to devices that have
296 * not been registered when this function is called).
298 * The supplier device is required to be registered when this function is called
299 * and NULL will be returned if that is not the case. The consumer device need
300 * not be registered, however.
302 struct device_link *device_link_add(struct device *consumer,
303 struct device *supplier, u32 flags)
305 struct device_link *link;
307 if (!consumer || !supplier || flags & ~DL_ADD_VALID_FLAGS ||
308 (flags & DL_FLAG_STATELESS && flags & DL_MANAGED_LINK_FLAGS) ||
309 (flags & DL_FLAG_SYNC_STATE_ONLY &&
310 flags != DL_FLAG_SYNC_STATE_ONLY) ||
311 (flags & DL_FLAG_AUTOPROBE_CONSUMER &&
312 flags & (DL_FLAG_AUTOREMOVE_CONSUMER |
313 DL_FLAG_AUTOREMOVE_SUPPLIER)))
316 if (flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) {
317 if (pm_runtime_get_sync(supplier) < 0) {
318 pm_runtime_put_noidle(supplier);
323 if (!(flags & DL_FLAG_STATELESS))
324 flags |= DL_FLAG_MANAGED;
326 device_links_write_lock();
330 * If the supplier has not been fully registered yet or there is a
331 * reverse (non-SYNC_STATE_ONLY) dependency between the consumer and
332 * the supplier already in the graph, return NULL. If the link is a
333 * SYNC_STATE_ONLY link, we don't check for reverse dependencies
334 * because it only affects sync_state() callbacks.
336 if (!device_pm_initialized(supplier)
337 || (!(flags & DL_FLAG_SYNC_STATE_ONLY) &&
338 device_is_dependent(consumer, supplier))) {
344 * DL_FLAG_AUTOREMOVE_SUPPLIER indicates that the link will be needed
345 * longer than for DL_FLAG_AUTOREMOVE_CONSUMER and setting them both
346 * together doesn't make sense, so prefer DL_FLAG_AUTOREMOVE_SUPPLIER.
348 if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
349 flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER;
351 list_for_each_entry(link, &supplier->links.consumers, s_node) {
352 if (link->consumer != consumer)
355 if (flags & DL_FLAG_PM_RUNTIME) {
356 if (!(link->flags & DL_FLAG_PM_RUNTIME)) {
357 pm_runtime_new_link(consumer);
358 link->flags |= DL_FLAG_PM_RUNTIME;
360 if (flags & DL_FLAG_RPM_ACTIVE)
361 refcount_inc(&link->rpm_active);
364 if (flags & DL_FLAG_STATELESS) {
365 kref_get(&link->kref);
366 if (link->flags & DL_FLAG_SYNC_STATE_ONLY &&
367 !(link->flags & DL_FLAG_STATELESS)) {
368 link->flags |= DL_FLAG_STATELESS;
371 link->flags |= DL_FLAG_STATELESS;
377 * If the life time of the link following from the new flags is
378 * longer than indicated by the flags of the existing link,
379 * update the existing link to stay around longer.
381 if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER) {
382 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER) {
383 link->flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER;
384 link->flags |= DL_FLAG_AUTOREMOVE_SUPPLIER;
386 } else if (!(flags & DL_FLAG_AUTOREMOVE_CONSUMER)) {
387 link->flags &= ~(DL_FLAG_AUTOREMOVE_CONSUMER |
388 DL_FLAG_AUTOREMOVE_SUPPLIER);
390 if (!(link->flags & DL_FLAG_MANAGED)) {
391 kref_get(&link->kref);
392 link->flags |= DL_FLAG_MANAGED;
393 device_link_init_status(link, consumer, supplier);
395 if (link->flags & DL_FLAG_SYNC_STATE_ONLY &&
396 !(flags & DL_FLAG_SYNC_STATE_ONLY)) {
397 link->flags &= ~DL_FLAG_SYNC_STATE_ONLY;
404 link = kzalloc(sizeof(*link), GFP_KERNEL);
408 refcount_set(&link->rpm_active, 1);
410 if (flags & DL_FLAG_PM_RUNTIME) {
411 if (flags & DL_FLAG_RPM_ACTIVE)
412 refcount_inc(&link->rpm_active);
414 pm_runtime_new_link(consumer);
417 get_device(supplier);
418 link->supplier = supplier;
419 INIT_LIST_HEAD(&link->s_node);
420 get_device(consumer);
421 link->consumer = consumer;
422 INIT_LIST_HEAD(&link->c_node);
424 kref_init(&link->kref);
426 /* Determine the initial link state. */
427 if (flags & DL_FLAG_STATELESS)
428 link->status = DL_STATE_NONE;
430 device_link_init_status(link, consumer, supplier);
433 * Some callers expect the link creation during consumer driver probe to
434 * resume the supplier even without DL_FLAG_RPM_ACTIVE.
436 if (link->status == DL_STATE_CONSUMER_PROBE &&
437 flags & DL_FLAG_PM_RUNTIME)
438 pm_runtime_resume(supplier);
440 list_add_tail_rcu(&link->s_node, &supplier->links.consumers);
441 list_add_tail_rcu(&link->c_node, &consumer->links.suppliers);
443 if (flags & DL_FLAG_SYNC_STATE_ONLY) {
445 "Linked as a sync state only consumer to %s\n",
452 * Move the consumer and all of the devices depending on it to the end
453 * of dpm_list and the devices_kset list.
455 * It is necessary to hold dpm_list locked throughout all that or else
456 * we may end up suspending with a wrong ordering of it.
458 device_reorder_to_tail(consumer, NULL);
460 dev_dbg(consumer, "Linked as a consumer to %s\n", dev_name(supplier));
464 device_links_write_unlock();
466 if ((flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) && !link)
467 pm_runtime_put(supplier);
471 EXPORT_SYMBOL_GPL(device_link_add);
474 * device_link_wait_for_supplier - Add device to wait_for_suppliers list
475 * @consumer: Consumer device
477 * Marks the @consumer device as waiting for suppliers to become available by
478 * adding it to the wait_for_suppliers list. The consumer device will never be
479 * probed until it's removed from the wait_for_suppliers list.
481 * The caller is responsible for adding the links to the supplier devices once
482 * they are available and removing the @consumer device from the
483 * wait_for_suppliers list once links to all the suppliers have been created.
485 * This function is NOT meant to be called from the probe function of the
486 * consumer but rather from code that creates/adds the consumer device.
488 static void device_link_wait_for_supplier(struct device *consumer,
491 mutex_lock(&wfs_lock);
492 list_add_tail(&consumer->links.needs_suppliers, &wait_for_suppliers);
493 consumer->links.need_for_probe = need_for_probe;
494 mutex_unlock(&wfs_lock);
497 static void device_link_wait_for_mandatory_supplier(struct device *consumer)
499 device_link_wait_for_supplier(consumer, true);
502 static void device_link_wait_for_optional_supplier(struct device *consumer)
504 device_link_wait_for_supplier(consumer, false);
508 * device_link_add_missing_supplier_links - Add links from consumer devices to
509 * supplier devices, leaving any
510 * consumer with inactive suppliers on
511 * the wait_for_suppliers list
513 * Loops through all consumers waiting on suppliers and tries to add all their
514 * supplier links. If that succeeds, the consumer device is removed from
515 * wait_for_suppliers list. Otherwise, they are left in the wait_for_suppliers
516 * list. Devices left on the wait_for_suppliers list will not be probed.
518 * The fwnode add_links callback is expected to return 0 if it has found and
519 * added all the supplier links for the consumer device. It should return an
520 * error if it isn't able to do so.
522 * The caller of device_link_wait_for_supplier() is expected to call this once
523 * it's aware of potential suppliers becoming available.
525 static void device_link_add_missing_supplier_links(void)
527 struct device *dev, *tmp;
529 mutex_lock(&wfs_lock);
530 list_for_each_entry_safe(dev, tmp, &wait_for_suppliers,
531 links.needs_suppliers) {
532 int ret = fwnode_call_int_op(dev->fwnode, add_links, dev);
534 list_del_init(&dev->links.needs_suppliers);
535 else if (ret != -ENODEV || fw_devlink_is_permissive())
536 dev->links.need_for_probe = false;
538 mutex_unlock(&wfs_lock);
541 static void device_link_free(struct device_link *link)
543 while (refcount_dec_not_one(&link->rpm_active))
544 pm_runtime_put(link->supplier);
546 put_device(link->consumer);
547 put_device(link->supplier);
552 static void __device_link_free_srcu(struct rcu_head *rhead)
554 device_link_free(container_of(rhead, struct device_link, rcu_head));
557 static void __device_link_del(struct kref *kref)
559 struct device_link *link = container_of(kref, struct device_link, kref);
561 dev_dbg(link->consumer, "Dropping the link to %s\n",
562 dev_name(link->supplier));
564 if (link->flags & DL_FLAG_PM_RUNTIME)
565 pm_runtime_drop_link(link->consumer);
567 list_del_rcu(&link->s_node);
568 list_del_rcu(&link->c_node);
569 call_srcu(&device_links_srcu, &link->rcu_head, __device_link_free_srcu);
571 #else /* !CONFIG_SRCU */
572 static void __device_link_del(struct kref *kref)
574 struct device_link *link = container_of(kref, struct device_link, kref);
576 dev_info(link->consumer, "Dropping the link to %s\n",
577 dev_name(link->supplier));
579 if (link->flags & DL_FLAG_PM_RUNTIME)
580 pm_runtime_drop_link(link->consumer);
582 list_del(&link->s_node);
583 list_del(&link->c_node);
584 device_link_free(link);
586 #endif /* !CONFIG_SRCU */
588 static void device_link_put_kref(struct device_link *link)
590 if (link->flags & DL_FLAG_STATELESS)
591 kref_put(&link->kref, __device_link_del);
593 WARN(1, "Unable to drop a managed device link reference\n");
597 * device_link_del - Delete a stateless link between two devices.
598 * @link: Device link to delete.
600 * The caller must ensure proper synchronization of this function with runtime
601 * PM. If the link was added multiple times, it needs to be deleted as often.
602 * Care is required for hotplugged devices: Their links are purged on removal
603 * and calling device_link_del() is then no longer allowed.
605 void device_link_del(struct device_link *link)
607 device_links_write_lock();
609 device_link_put_kref(link);
611 device_links_write_unlock();
613 EXPORT_SYMBOL_GPL(device_link_del);
616 * device_link_remove - Delete a stateless link between two devices.
617 * @consumer: Consumer end of the link.
618 * @supplier: Supplier end of the link.
620 * The caller must ensure proper synchronization of this function with runtime
623 void device_link_remove(void *consumer, struct device *supplier)
625 struct device_link *link;
627 if (WARN_ON(consumer == supplier))
630 device_links_write_lock();
633 list_for_each_entry(link, &supplier->links.consumers, s_node) {
634 if (link->consumer == consumer) {
635 device_link_put_kref(link);
641 device_links_write_unlock();
643 EXPORT_SYMBOL_GPL(device_link_remove);
645 static void device_links_missing_supplier(struct device *dev)
647 struct device_link *link;
649 list_for_each_entry(link, &dev->links.suppliers, c_node) {
650 if (link->status != DL_STATE_CONSUMER_PROBE)
653 if (link->supplier->links.status == DL_DEV_DRIVER_BOUND) {
654 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
656 WARN_ON(!(link->flags & DL_FLAG_SYNC_STATE_ONLY));
657 WRITE_ONCE(link->status, DL_STATE_DORMANT);
663 * device_links_check_suppliers - Check presence of supplier drivers.
664 * @dev: Consumer device.
666 * Check links from this device to any suppliers. Walk the list of the device's
667 * links to suppliers and see if all of them are available. If not, simply
668 * return -EPROBE_DEFER.
670 * We need to guarantee that the supplier will not go away after the check has
671 * been positive here. It only can go away in __device_release_driver() and
672 * that function checks the device's links to consumers. This means we need to
673 * mark the link as "consumer probe in progress" to make the supplier removal
674 * wait for us to complete (or bad things may happen).
676 * Links without the DL_FLAG_MANAGED flag set are ignored.
678 int device_links_check_suppliers(struct device *dev)
680 struct device_link *link;
684 * Device waiting for supplier to become available is not allowed to
687 mutex_lock(&wfs_lock);
688 if (!list_empty(&dev->links.needs_suppliers) &&
689 dev->links.need_for_probe) {
690 mutex_unlock(&wfs_lock);
691 return -EPROBE_DEFER;
693 mutex_unlock(&wfs_lock);
695 device_links_write_lock();
697 list_for_each_entry(link, &dev->links.suppliers, c_node) {
698 if (!(link->flags & DL_FLAG_MANAGED))
701 if (link->status != DL_STATE_AVAILABLE &&
702 !(link->flags & DL_FLAG_SYNC_STATE_ONLY)) {
703 device_links_missing_supplier(dev);
707 WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE);
709 dev->links.status = DL_DEV_PROBING;
711 device_links_write_unlock();
716 * __device_links_queue_sync_state - Queue a device for sync_state() callback
717 * @dev: Device to call sync_state() on
718 * @list: List head to queue the @dev on
720 * Queues a device for a sync_state() callback when the device links write lock
721 * isn't held. This allows the sync_state() execution flow to use device links
722 * APIs. The caller must ensure this function is called with
723 * device_links_write_lock() held.
725 * This function does a get_device() to make sure the device is not freed while
728 * So the caller must also ensure that device_links_flush_sync_list() is called
729 * as soon as the caller releases device_links_write_lock(). This is necessary
730 * to make sure the sync_state() is called in a timely fashion and the
731 * put_device() is called on this device.
733 static void __device_links_queue_sync_state(struct device *dev,
734 struct list_head *list)
736 struct device_link *link;
738 if (!dev_has_sync_state(dev))
740 if (dev->state_synced)
743 list_for_each_entry(link, &dev->links.consumers, s_node) {
744 if (!(link->flags & DL_FLAG_MANAGED))
746 if (link->status != DL_STATE_ACTIVE)
751 * Set the flag here to avoid adding the same device to a list more
752 * than once. This can happen if new consumers get added to the device
753 * and probed before the list is flushed.
755 dev->state_synced = true;
757 if (WARN_ON(!list_empty(&dev->links.defer_sync)))
761 list_add_tail(&dev->links.defer_sync, list);
765 * device_links_flush_sync_list - Call sync_state() on a list of devices
766 * @list: List of devices to call sync_state() on
767 * @dont_lock_dev: Device for which lock is already held by the caller
769 * Calls sync_state() on all the devices that have been queued for it. This
770 * function is used in conjunction with __device_links_queue_sync_state(). The
771 * @dont_lock_dev parameter is useful when this function is called from a
772 * context where a device lock is already held.
774 static void device_links_flush_sync_list(struct list_head *list,
775 struct device *dont_lock_dev)
777 struct device *dev, *tmp;
779 list_for_each_entry_safe(dev, tmp, list, links.defer_sync) {
780 list_del_init(&dev->links.defer_sync);
782 if (dev != dont_lock_dev)
785 if (dev->bus->sync_state)
786 dev->bus->sync_state(dev);
787 else if (dev->driver && dev->driver->sync_state)
788 dev->driver->sync_state(dev);
790 if (dev != dont_lock_dev)
797 void device_links_supplier_sync_state_pause(void)
799 device_links_write_lock();
800 defer_sync_state_count++;
801 device_links_write_unlock();
804 void device_links_supplier_sync_state_resume(void)
806 struct device *dev, *tmp;
807 LIST_HEAD(sync_list);
809 device_links_write_lock();
810 if (!defer_sync_state_count) {
811 WARN(true, "Unmatched sync_state pause/resume!");
814 defer_sync_state_count--;
815 if (defer_sync_state_count)
818 list_for_each_entry_safe(dev, tmp, &deferred_sync, links.defer_sync) {
820 * Delete from deferred_sync list before queuing it to
821 * sync_list because defer_sync is used for both lists.
823 list_del_init(&dev->links.defer_sync);
824 __device_links_queue_sync_state(dev, &sync_list);
827 device_links_write_unlock();
829 device_links_flush_sync_list(&sync_list, NULL);
832 static int sync_state_resume_initcall(void)
834 device_links_supplier_sync_state_resume();
837 late_initcall(sync_state_resume_initcall);
839 static void __device_links_supplier_defer_sync(struct device *sup)
841 if (list_empty(&sup->links.defer_sync) && dev_has_sync_state(sup))
842 list_add_tail(&sup->links.defer_sync, &deferred_sync);
845 static void device_link_drop_managed(struct device_link *link)
847 link->flags &= ~DL_FLAG_MANAGED;
848 WRITE_ONCE(link->status, DL_STATE_NONE);
849 kref_put(&link->kref, __device_link_del);
853 * device_links_driver_bound - Update device links after probing its driver.
854 * @dev: Device to update the links for.
856 * The probe has been successful, so update links from this device to any
857 * consumers by changing their status to "available".
859 * Also change the status of @dev's links to suppliers to "active".
861 * Links without the DL_FLAG_MANAGED flag set are ignored.
863 void device_links_driver_bound(struct device *dev)
865 struct device_link *link, *ln;
866 LIST_HEAD(sync_list);
869 * If a device probes successfully, it's expected to have created all
870 * the device links it needs to or make new device links as it needs
871 * them. So, it no longer needs to wait on any suppliers.
873 mutex_lock(&wfs_lock);
874 list_del_init(&dev->links.needs_suppliers);
875 mutex_unlock(&wfs_lock);
877 device_links_write_lock();
879 list_for_each_entry(link, &dev->links.consumers, s_node) {
880 if (!(link->flags & DL_FLAG_MANAGED))
884 * Links created during consumer probe may be in the "consumer
885 * probe" state to start with if the supplier is still probing
886 * when they are created and they may become "active" if the
887 * consumer probe returns first. Skip them here.
889 if (link->status == DL_STATE_CONSUMER_PROBE ||
890 link->status == DL_STATE_ACTIVE)
893 WARN_ON(link->status != DL_STATE_DORMANT);
894 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
896 if (link->flags & DL_FLAG_AUTOPROBE_CONSUMER)
897 driver_deferred_probe_add(link->consumer);
900 if (defer_sync_state_count)
901 __device_links_supplier_defer_sync(dev);
903 __device_links_queue_sync_state(dev, &sync_list);
905 list_for_each_entry_safe(link, ln, &dev->links.suppliers, c_node) {
906 struct device *supplier;
908 if (!(link->flags & DL_FLAG_MANAGED))
911 supplier = link->supplier;
912 if (link->flags & DL_FLAG_SYNC_STATE_ONLY) {
914 * When DL_FLAG_SYNC_STATE_ONLY is set, it means no
915 * other DL_MANAGED_LINK_FLAGS have been set. So, it's
916 * save to drop the managed link completely.
918 device_link_drop_managed(link);
920 WARN_ON(link->status != DL_STATE_CONSUMER_PROBE);
921 WRITE_ONCE(link->status, DL_STATE_ACTIVE);
925 * This needs to be done even for the deleted
926 * DL_FLAG_SYNC_STATE_ONLY device link in case it was the last
927 * device link that was preventing the supplier from getting a
930 if (defer_sync_state_count)
931 __device_links_supplier_defer_sync(supplier);
933 __device_links_queue_sync_state(supplier, &sync_list);
936 dev->links.status = DL_DEV_DRIVER_BOUND;
938 device_links_write_unlock();
940 device_links_flush_sync_list(&sync_list, dev);
944 * __device_links_no_driver - Update links of a device without a driver.
945 * @dev: Device without a drvier.
947 * Delete all non-persistent links from this device to any suppliers.
949 * Persistent links stay around, but their status is changed to "available",
950 * unless they already are in the "supplier unbind in progress" state in which
951 * case they need not be updated.
953 * Links without the DL_FLAG_MANAGED flag set are ignored.
955 static void __device_links_no_driver(struct device *dev)
957 struct device_link *link, *ln;
959 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
960 if (!(link->flags & DL_FLAG_MANAGED))
963 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER) {
964 device_link_drop_managed(link);
968 if (link->status != DL_STATE_CONSUMER_PROBE &&
969 link->status != DL_STATE_ACTIVE)
972 if (link->supplier->links.status == DL_DEV_DRIVER_BOUND) {
973 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
975 WARN_ON(!(link->flags & DL_FLAG_SYNC_STATE_ONLY));
976 WRITE_ONCE(link->status, DL_STATE_DORMANT);
980 dev->links.status = DL_DEV_NO_DRIVER;
984 * device_links_no_driver - Update links after failing driver probe.
985 * @dev: Device whose driver has just failed to probe.
987 * Clean up leftover links to consumers for @dev and invoke
988 * %__device_links_no_driver() to update links to suppliers for it as
991 * Links without the DL_FLAG_MANAGED flag set are ignored.
993 void device_links_no_driver(struct device *dev)
995 struct device_link *link;
997 device_links_write_lock();
999 list_for_each_entry(link, &dev->links.consumers, s_node) {
1000 if (!(link->flags & DL_FLAG_MANAGED))
1004 * The probe has failed, so if the status of the link is
1005 * "consumer probe" or "active", it must have been added by
1006 * a probing consumer while this device was still probing.
1007 * Change its state to "dormant", as it represents a valid
1008 * relationship, but it is not functionally meaningful.
1010 if (link->status == DL_STATE_CONSUMER_PROBE ||
1011 link->status == DL_STATE_ACTIVE)
1012 WRITE_ONCE(link->status, DL_STATE_DORMANT);
1015 __device_links_no_driver(dev);
1017 device_links_write_unlock();
1021 * device_links_driver_cleanup - Update links after driver removal.
1022 * @dev: Device whose driver has just gone away.
1024 * Update links to consumers for @dev by changing their status to "dormant" and
1025 * invoke %__device_links_no_driver() to update links to suppliers for it as
1028 * Links without the DL_FLAG_MANAGED flag set are ignored.
1030 void device_links_driver_cleanup(struct device *dev)
1032 struct device_link *link, *ln;
1034 device_links_write_lock();
1036 list_for_each_entry_safe(link, ln, &dev->links.consumers, s_node) {
1037 if (!(link->flags & DL_FLAG_MANAGED))
1040 WARN_ON(link->flags & DL_FLAG_AUTOREMOVE_CONSUMER);
1041 WARN_ON(link->status != DL_STATE_SUPPLIER_UNBIND);
1044 * autoremove the links between this @dev and its consumer
1045 * devices that are not active, i.e. where the link state
1046 * has moved to DL_STATE_SUPPLIER_UNBIND.
1048 if (link->status == DL_STATE_SUPPLIER_UNBIND &&
1049 link->flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
1050 device_link_drop_managed(link);
1052 WRITE_ONCE(link->status, DL_STATE_DORMANT);
1055 list_del_init(&dev->links.defer_sync);
1056 __device_links_no_driver(dev);
1058 device_links_write_unlock();
1062 * device_links_busy - Check if there are any busy links to consumers.
1063 * @dev: Device to check.
1065 * Check each consumer of the device and return 'true' if its link's status
1066 * is one of "consumer probe" or "active" (meaning that the given consumer is
1067 * probing right now or its driver is present). Otherwise, change the link
1068 * state to "supplier unbind" to prevent the consumer from being probed
1069 * successfully going forward.
1071 * Return 'false' if there are no probing or active consumers.
1073 * Links without the DL_FLAG_MANAGED flag set are ignored.
1075 bool device_links_busy(struct device *dev)
1077 struct device_link *link;
1080 device_links_write_lock();
1082 list_for_each_entry(link, &dev->links.consumers, s_node) {
1083 if (!(link->flags & DL_FLAG_MANAGED))
1086 if (link->status == DL_STATE_CONSUMER_PROBE
1087 || link->status == DL_STATE_ACTIVE) {
1091 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
1094 dev->links.status = DL_DEV_UNBINDING;
1096 device_links_write_unlock();
1101 * device_links_unbind_consumers - Force unbind consumers of the given device.
1102 * @dev: Device to unbind the consumers of.
1104 * Walk the list of links to consumers for @dev and if any of them is in the
1105 * "consumer probe" state, wait for all device probes in progress to complete
1108 * If that's not the case, change the status of the link to "supplier unbind"
1109 * and check if the link was in the "active" state. If so, force the consumer
1110 * driver to unbind and start over (the consumer will not re-probe as we have
1111 * changed the state of the link already).
1113 * Links without the DL_FLAG_MANAGED flag set are ignored.
1115 void device_links_unbind_consumers(struct device *dev)
1117 struct device_link *link;
1120 device_links_write_lock();
1122 list_for_each_entry(link, &dev->links.consumers, s_node) {
1123 enum device_link_state status;
1125 if (!(link->flags & DL_FLAG_MANAGED) ||
1126 link->flags & DL_FLAG_SYNC_STATE_ONLY)
1129 status = link->status;
1130 if (status == DL_STATE_CONSUMER_PROBE) {
1131 device_links_write_unlock();
1133 wait_for_device_probe();
1136 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
1137 if (status == DL_STATE_ACTIVE) {
1138 struct device *consumer = link->consumer;
1140 get_device(consumer);
1142 device_links_write_unlock();
1144 device_release_driver_internal(consumer, NULL,
1146 put_device(consumer);
1151 device_links_write_unlock();
1155 * device_links_purge - Delete existing links to other devices.
1156 * @dev: Target device.
1158 static void device_links_purge(struct device *dev)
1160 struct device_link *link, *ln;
1162 mutex_lock(&wfs_lock);
1163 list_del(&dev->links.needs_suppliers);
1164 mutex_unlock(&wfs_lock);
1167 * Delete all of the remaining links from this device to any other
1168 * devices (either consumers or suppliers).
1170 device_links_write_lock();
1172 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
1173 WARN_ON(link->status == DL_STATE_ACTIVE);
1174 __device_link_del(&link->kref);
1177 list_for_each_entry_safe_reverse(link, ln, &dev->links.consumers, s_node) {
1178 WARN_ON(link->status != DL_STATE_DORMANT &&
1179 link->status != DL_STATE_NONE);
1180 __device_link_del(&link->kref);
1183 device_links_write_unlock();
1186 static u32 fw_devlink_flags = DL_FLAG_SYNC_STATE_ONLY;
1187 static int __init fw_devlink_setup(char *arg)
1192 if (strcmp(arg, "off") == 0) {
1193 fw_devlink_flags = 0;
1194 } else if (strcmp(arg, "permissive") == 0) {
1195 fw_devlink_flags = DL_FLAG_SYNC_STATE_ONLY;
1196 } else if (strcmp(arg, "on") == 0) {
1197 fw_devlink_flags = DL_FLAG_AUTOPROBE_CONSUMER;
1198 } else if (strcmp(arg, "rpm") == 0) {
1199 fw_devlink_flags = DL_FLAG_AUTOPROBE_CONSUMER |
1204 early_param("fw_devlink", fw_devlink_setup);
1206 u32 fw_devlink_get_flags(void)
1208 return fw_devlink_flags;
1211 static bool fw_devlink_is_permissive(void)
1213 return fw_devlink_flags == DL_FLAG_SYNC_STATE_ONLY;
1216 static void fw_devlink_link_device(struct device *dev)
1220 if (!fw_devlink_flags)
1223 mutex_lock(&defer_fw_devlink_lock);
1224 if (!defer_fw_devlink_count)
1225 device_link_add_missing_supplier_links();
1228 * The device's fwnode not having add_links() doesn't affect if other
1229 * consumers can find this device as a supplier. So, this check is
1230 * intentionally placed after device_link_add_missing_supplier_links().
1232 if (!fwnode_has_op(dev->fwnode, add_links))
1236 * If fw_devlink is being deferred, assume all devices have mandatory
1237 * suppliers they need to link to later. Then, when the fw_devlink is
1238 * resumed, all these devices will get a chance to try and link to any
1239 * suppliers they have.
1241 if (!defer_fw_devlink_count) {
1242 fw_ret = fwnode_call_int_op(dev->fwnode, add_links, dev);
1243 if (fw_ret == -ENODEV && fw_devlink_is_permissive())
1249 if (fw_ret == -ENODEV)
1250 device_link_wait_for_mandatory_supplier(dev);
1252 device_link_wait_for_optional_supplier(dev);
1255 mutex_unlock(&defer_fw_devlink_lock);
1259 * fw_devlink_pause - Pause parsing of fwnode to create device links
1261 * Calling this function defers any fwnode parsing to create device links until
1262 * fw_devlink_resume() is called. Both these functions are ref counted and the
1263 * caller needs to match the calls.
1265 * While fw_devlink is paused:
1266 * - Any device that is added won't have its fwnode parsed to create device
1268 * - The probe of the device will also be deferred during this period.
1269 * - Any devices that were already added, but waiting for suppliers won't be
1270 * able to link to newly added devices.
1272 * Once fw_devlink_resume():
1273 * - All the fwnodes that was not parsed will be parsed.
1274 * - All the devices that were deferred probing will be reattempted if they
1275 * aren't waiting for any more suppliers.
1277 * This pair of functions, is mainly meant to optimize the parsing of fwnodes
1278 * when a lot of devices that need to link to each other are added in a short
1279 * interval of time. For example, adding all the top level devices in a system.
1281 * For example, if N devices are added and:
1282 * - All the consumers are added before their suppliers
1283 * - All the suppliers of the N devices are part of the N devices
1287 * - With the use of fw_devlink_pause() and fw_devlink_resume(), each device
1288 * will only need one parsing of its fwnode because it is guaranteed to find
1289 * all the supplier devices already registered and ready to link to. It won't
1290 * have to do another pass later to find one or more suppliers it couldn't
1291 * find in the first parse of the fwnode. So, we'll only need O(N) fwnode
1294 * - Without the use of fw_devlink_pause() and fw_devlink_resume(), we would
1295 * end up doing O(N^2) parses of fwnodes because every device that's added is
1296 * guaranteed to trigger a parse of the fwnode of every device added before
1297 * it. This O(N^2) parse is made worse by the fact that when a fwnode of a
1298 * device is parsed, all it descendant devices might need to have their
1299 * fwnodes parsed too (even if the devices themselves aren't added).
1301 void fw_devlink_pause(void)
1303 mutex_lock(&defer_fw_devlink_lock);
1304 defer_fw_devlink_count++;
1305 mutex_unlock(&defer_fw_devlink_lock);
1308 /** fw_devlink_resume - Resume parsing of fwnode to create device links
1310 * This function is used in conjunction with fw_devlink_pause() and is ref
1311 * counted. See documentation for fw_devlink_pause() for more details.
1313 void fw_devlink_resume(void)
1315 mutex_lock(&defer_fw_devlink_lock);
1316 if (!defer_fw_devlink_count) {
1317 WARN(true, "Unmatched fw_devlink pause/resume!");
1321 defer_fw_devlink_count--;
1322 if (defer_fw_devlink_count)
1325 device_link_add_missing_supplier_links();
1327 mutex_unlock(&defer_fw_devlink_lock);
1329 /* Device links support end. */
1331 int (*platform_notify)(struct device *dev) = NULL;
1332 int (*platform_notify_remove)(struct device *dev) = NULL;
1333 static struct kobject *dev_kobj;
1334 struct kobject *sysfs_dev_char_kobj;
1335 struct kobject *sysfs_dev_block_kobj;
1337 static DEFINE_MUTEX(device_hotplug_lock);
1339 void lock_device_hotplug(void)
1341 mutex_lock(&device_hotplug_lock);
1344 void unlock_device_hotplug(void)
1346 mutex_unlock(&device_hotplug_lock);
1349 int lock_device_hotplug_sysfs(void)
1351 if (mutex_trylock(&device_hotplug_lock))
1354 /* Avoid busy looping (5 ms of sleep should do). */
1356 return restart_syscall();
1360 static inline int device_is_not_partition(struct device *dev)
1362 return !(dev->type == &part_type);
1365 static inline int device_is_not_partition(struct device *dev)
1372 device_platform_notify(struct device *dev, enum kobject_action action)
1376 ret = acpi_platform_notify(dev, action);
1380 ret = software_node_notify(dev, action);
1384 if (platform_notify && action == KOBJ_ADD)
1385 platform_notify(dev);
1386 else if (platform_notify_remove && action == KOBJ_REMOVE)
1387 platform_notify_remove(dev);
1392 * dev_driver_string - Return a device's driver name, if at all possible
1393 * @dev: struct device to get the name of
1395 * Will return the device's driver's name if it is bound to a device. If
1396 * the device is not bound to a driver, it will return the name of the bus
1397 * it is attached to. If it is not attached to a bus either, an empty
1398 * string will be returned.
1400 const char *dev_driver_string(const struct device *dev)
1402 struct device_driver *drv;
1404 /* dev->driver can change to NULL underneath us because of unbinding,
1405 * so be careful about accessing it. dev->bus and dev->class should
1406 * never change once they are set, so they don't need special care.
1408 drv = READ_ONCE(dev->driver);
1409 return drv ? drv->name :
1410 (dev->bus ? dev->bus->name :
1411 (dev->class ? dev->class->name : ""));
1413 EXPORT_SYMBOL(dev_driver_string);
1415 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
1417 static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
1420 struct device_attribute *dev_attr = to_dev_attr(attr);
1421 struct device *dev = kobj_to_dev(kobj);
1425 ret = dev_attr->show(dev, dev_attr, buf);
1426 if (ret >= (ssize_t)PAGE_SIZE) {
1427 printk("dev_attr_show: %pS returned bad count\n",
1433 static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
1434 const char *buf, size_t count)
1436 struct device_attribute *dev_attr = to_dev_attr(attr);
1437 struct device *dev = kobj_to_dev(kobj);
1440 if (dev_attr->store)
1441 ret = dev_attr->store(dev, dev_attr, buf, count);
1445 static const struct sysfs_ops dev_sysfs_ops = {
1446 .show = dev_attr_show,
1447 .store = dev_attr_store,
1450 #define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
1452 ssize_t device_store_ulong(struct device *dev,
1453 struct device_attribute *attr,
1454 const char *buf, size_t size)
1456 struct dev_ext_attribute *ea = to_ext_attr(attr);
1460 ret = kstrtoul(buf, 0, &new);
1463 *(unsigned long *)(ea->var) = new;
1464 /* Always return full write size even if we didn't consume all */
1467 EXPORT_SYMBOL_GPL(device_store_ulong);
1469 ssize_t device_show_ulong(struct device *dev,
1470 struct device_attribute *attr,
1473 struct dev_ext_attribute *ea = to_ext_attr(attr);
1474 return snprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var));
1476 EXPORT_SYMBOL_GPL(device_show_ulong);
1478 ssize_t device_store_int(struct device *dev,
1479 struct device_attribute *attr,
1480 const char *buf, size_t size)
1482 struct dev_ext_attribute *ea = to_ext_attr(attr);
1486 ret = kstrtol(buf, 0, &new);
1490 if (new > INT_MAX || new < INT_MIN)
1492 *(int *)(ea->var) = new;
1493 /* Always return full write size even if we didn't consume all */
1496 EXPORT_SYMBOL_GPL(device_store_int);
1498 ssize_t device_show_int(struct device *dev,
1499 struct device_attribute *attr,
1502 struct dev_ext_attribute *ea = to_ext_attr(attr);
1504 return snprintf(buf, PAGE_SIZE, "%d\n", *(int *)(ea->var));
1506 EXPORT_SYMBOL_GPL(device_show_int);
1508 ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
1509 const char *buf, size_t size)
1511 struct dev_ext_attribute *ea = to_ext_attr(attr);
1513 if (strtobool(buf, ea->var) < 0)
1518 EXPORT_SYMBOL_GPL(device_store_bool);
1520 ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
1523 struct dev_ext_attribute *ea = to_ext_attr(attr);
1525 return snprintf(buf, PAGE_SIZE, "%d\n", *(bool *)(ea->var));
1527 EXPORT_SYMBOL_GPL(device_show_bool);
1530 * device_release - free device structure.
1531 * @kobj: device's kobject.
1533 * This is called once the reference count for the object
1534 * reaches 0. We forward the call to the device's release
1535 * method, which should handle actually freeing the structure.
1537 static void device_release(struct kobject *kobj)
1539 struct device *dev = kobj_to_dev(kobj);
1540 struct device_private *p = dev->p;
1543 * Some platform devices are driven without driver attached
1544 * and managed resources may have been acquired. Make sure
1545 * all resources are released.
1547 * Drivers still can add resources into device after device
1548 * is deleted but alive, so release devres here to avoid
1549 * possible memory leak.
1551 devres_release_all(dev);
1555 else if (dev->type && dev->type->release)
1556 dev->type->release(dev);
1557 else if (dev->class && dev->class->dev_release)
1558 dev->class->dev_release(dev);
1560 WARN(1, KERN_ERR "Device '%s' does not have a release() function, it is broken and must be fixed. See Documentation/core-api/kobject.rst.\n",
1565 static const void *device_namespace(struct kobject *kobj)
1567 struct device *dev = kobj_to_dev(kobj);
1568 const void *ns = NULL;
1570 if (dev->class && dev->class->ns_type)
1571 ns = dev->class->namespace(dev);
1576 static void device_get_ownership(struct kobject *kobj, kuid_t *uid, kgid_t *gid)
1578 struct device *dev = kobj_to_dev(kobj);
1580 if (dev->class && dev->class->get_ownership)
1581 dev->class->get_ownership(dev, uid, gid);
1584 static struct kobj_type device_ktype = {
1585 .release = device_release,
1586 .sysfs_ops = &dev_sysfs_ops,
1587 .namespace = device_namespace,
1588 .get_ownership = device_get_ownership,
1592 static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
1594 struct kobj_type *ktype = get_ktype(kobj);
1596 if (ktype == &device_ktype) {
1597 struct device *dev = kobj_to_dev(kobj);
1606 static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
1608 struct device *dev = kobj_to_dev(kobj);
1611 return dev->bus->name;
1613 return dev->class->name;
1617 static int dev_uevent(struct kset *kset, struct kobject *kobj,
1618 struct kobj_uevent_env *env)
1620 struct device *dev = kobj_to_dev(kobj);
1623 /* add device node properties if present */
1624 if (MAJOR(dev->devt)) {
1628 kuid_t uid = GLOBAL_ROOT_UID;
1629 kgid_t gid = GLOBAL_ROOT_GID;
1631 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
1632 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
1633 name = device_get_devnode(dev, &mode, &uid, &gid, &tmp);
1635 add_uevent_var(env, "DEVNAME=%s", name);
1637 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
1638 if (!uid_eq(uid, GLOBAL_ROOT_UID))
1639 add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid));
1640 if (!gid_eq(gid, GLOBAL_ROOT_GID))
1641 add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid));
1646 if (dev->type && dev->type->name)
1647 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
1650 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
1652 /* Add common DT information about the device */
1653 of_device_uevent(dev, env);
1655 /* have the bus specific function add its stuff */
1656 if (dev->bus && dev->bus->uevent) {
1657 retval = dev->bus->uevent(dev, env);
1659 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
1660 dev_name(dev), __func__, retval);
1663 /* have the class specific function add its stuff */
1664 if (dev->class && dev->class->dev_uevent) {
1665 retval = dev->class->dev_uevent(dev, env);
1667 pr_debug("device: '%s': %s: class uevent() "
1668 "returned %d\n", dev_name(dev),
1672 /* have the device type specific function add its stuff */
1673 if (dev->type && dev->type->uevent) {
1674 retval = dev->type->uevent(dev, env);
1676 pr_debug("device: '%s': %s: dev_type uevent() "
1677 "returned %d\n", dev_name(dev),
1684 static const struct kset_uevent_ops device_uevent_ops = {
1685 .filter = dev_uevent_filter,
1686 .name = dev_uevent_name,
1687 .uevent = dev_uevent,
1690 static ssize_t uevent_show(struct device *dev, struct device_attribute *attr,
1693 struct kobject *top_kobj;
1695 struct kobj_uevent_env *env = NULL;
1700 /* search the kset, the device belongs to */
1701 top_kobj = &dev->kobj;
1702 while (!top_kobj->kset && top_kobj->parent)
1703 top_kobj = top_kobj->parent;
1704 if (!top_kobj->kset)
1707 kset = top_kobj->kset;
1708 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
1711 /* respect filter */
1712 if (kset->uevent_ops && kset->uevent_ops->filter)
1713 if (!kset->uevent_ops->filter(kset, &dev->kobj))
1716 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
1720 /* let the kset specific function add its keys */
1721 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
1725 /* copy keys to file */
1726 for (i = 0; i < env->envp_idx; i++)
1727 count += sprintf(&buf[count], "%s\n", env->envp[i]);
1733 static ssize_t uevent_store(struct device *dev, struct device_attribute *attr,
1734 const char *buf, size_t count)
1738 rc = kobject_synth_uevent(&dev->kobj, buf, count);
1741 dev_err(dev, "uevent: failed to send synthetic uevent\n");
1747 static DEVICE_ATTR_RW(uevent);
1749 static ssize_t online_show(struct device *dev, struct device_attribute *attr,
1755 val = !dev->offline;
1757 return sprintf(buf, "%u\n", val);
1760 static ssize_t online_store(struct device *dev, struct device_attribute *attr,
1761 const char *buf, size_t count)
1766 ret = strtobool(buf, &val);
1770 ret = lock_device_hotplug_sysfs();
1774 ret = val ? device_online(dev) : device_offline(dev);
1775 unlock_device_hotplug();
1776 return ret < 0 ? ret : count;
1778 static DEVICE_ATTR_RW(online);
1780 int device_add_groups(struct device *dev, const struct attribute_group **groups)
1782 return sysfs_create_groups(&dev->kobj, groups);
1784 EXPORT_SYMBOL_GPL(device_add_groups);
1786 void device_remove_groups(struct device *dev,
1787 const struct attribute_group **groups)
1789 sysfs_remove_groups(&dev->kobj, groups);
1791 EXPORT_SYMBOL_GPL(device_remove_groups);
1793 union device_attr_group_devres {
1794 const struct attribute_group *group;
1795 const struct attribute_group **groups;
1798 static int devm_attr_group_match(struct device *dev, void *res, void *data)
1800 return ((union device_attr_group_devres *)res)->group == data;
1803 static void devm_attr_group_remove(struct device *dev, void *res)
1805 union device_attr_group_devres *devres = res;
1806 const struct attribute_group *group = devres->group;
1808 dev_dbg(dev, "%s: removing group %p\n", __func__, group);
1809 sysfs_remove_group(&dev->kobj, group);
1812 static void devm_attr_groups_remove(struct device *dev, void *res)
1814 union device_attr_group_devres *devres = res;
1815 const struct attribute_group **groups = devres->groups;
1817 dev_dbg(dev, "%s: removing groups %p\n", __func__, groups);
1818 sysfs_remove_groups(&dev->kobj, groups);
1822 * devm_device_add_group - given a device, create a managed attribute group
1823 * @dev: The device to create the group for
1824 * @grp: The attribute group to create
1826 * This function creates a group for the first time. It will explicitly
1827 * warn and error if any of the attribute files being created already exist.
1829 * Returns 0 on success or error code on failure.
1831 int devm_device_add_group(struct device *dev, const struct attribute_group *grp)
1833 union device_attr_group_devres *devres;
1836 devres = devres_alloc(devm_attr_group_remove,
1837 sizeof(*devres), GFP_KERNEL);
1841 error = sysfs_create_group(&dev->kobj, grp);
1843 devres_free(devres);
1847 devres->group = grp;
1848 devres_add(dev, devres);
1851 EXPORT_SYMBOL_GPL(devm_device_add_group);
1854 * devm_device_remove_group: remove a managed group from a device
1855 * @dev: device to remove the group from
1856 * @grp: group to remove
1858 * This function removes a group of attributes from a device. The attributes
1859 * previously have to have been created for this group, otherwise it will fail.
1861 void devm_device_remove_group(struct device *dev,
1862 const struct attribute_group *grp)
1864 WARN_ON(devres_release(dev, devm_attr_group_remove,
1865 devm_attr_group_match,
1866 /* cast away const */ (void *)grp));
1868 EXPORT_SYMBOL_GPL(devm_device_remove_group);
1871 * devm_device_add_groups - create a bunch of managed attribute groups
1872 * @dev: The device to create the group for
1873 * @groups: The attribute groups to create, NULL terminated
1875 * This function creates a bunch of managed attribute groups. If an error
1876 * occurs when creating a group, all previously created groups will be
1877 * removed, unwinding everything back to the original state when this
1878 * function was called. It will explicitly warn and error if any of the
1879 * attribute files being created already exist.
1881 * Returns 0 on success or error code from sysfs_create_group on failure.
1883 int devm_device_add_groups(struct device *dev,
1884 const struct attribute_group **groups)
1886 union device_attr_group_devres *devres;
1889 devres = devres_alloc(devm_attr_groups_remove,
1890 sizeof(*devres), GFP_KERNEL);
1894 error = sysfs_create_groups(&dev->kobj, groups);
1896 devres_free(devres);
1900 devres->groups = groups;
1901 devres_add(dev, devres);
1904 EXPORT_SYMBOL_GPL(devm_device_add_groups);
1907 * devm_device_remove_groups - remove a list of managed groups
1909 * @dev: The device for the groups to be removed from
1910 * @groups: NULL terminated list of groups to be removed
1912 * If groups is not NULL, remove the specified groups from the device.
1914 void devm_device_remove_groups(struct device *dev,
1915 const struct attribute_group **groups)
1917 WARN_ON(devres_release(dev, devm_attr_groups_remove,
1918 devm_attr_group_match,
1919 /* cast away const */ (void *)groups));
1921 EXPORT_SYMBOL_GPL(devm_device_remove_groups);
1923 static int device_add_attrs(struct device *dev)
1925 struct class *class = dev->class;
1926 const struct device_type *type = dev->type;
1930 error = device_add_groups(dev, class->dev_groups);
1936 error = device_add_groups(dev, type->groups);
1938 goto err_remove_class_groups;
1941 error = device_add_groups(dev, dev->groups);
1943 goto err_remove_type_groups;
1945 if (device_supports_offline(dev) && !dev->offline_disabled) {
1946 error = device_create_file(dev, &dev_attr_online);
1948 goto err_remove_dev_groups;
1953 err_remove_dev_groups:
1954 device_remove_groups(dev, dev->groups);
1955 err_remove_type_groups:
1957 device_remove_groups(dev, type->groups);
1958 err_remove_class_groups:
1960 device_remove_groups(dev, class->dev_groups);
1965 static void device_remove_attrs(struct device *dev)
1967 struct class *class = dev->class;
1968 const struct device_type *type = dev->type;
1970 device_remove_file(dev, &dev_attr_online);
1971 device_remove_groups(dev, dev->groups);
1974 device_remove_groups(dev, type->groups);
1977 device_remove_groups(dev, class->dev_groups);
1980 static ssize_t dev_show(struct device *dev, struct device_attribute *attr,
1983 return print_dev_t(buf, dev->devt);
1985 static DEVICE_ATTR_RO(dev);
1988 struct kset *devices_kset;
1991 * devices_kset_move_before - Move device in the devices_kset's list.
1992 * @deva: Device to move.
1993 * @devb: Device @deva should come before.
1995 static void devices_kset_move_before(struct device *deva, struct device *devb)
1999 pr_debug("devices_kset: Moving %s before %s\n",
2000 dev_name(deva), dev_name(devb));
2001 spin_lock(&devices_kset->list_lock);
2002 list_move_tail(&deva->kobj.entry, &devb->kobj.entry);
2003 spin_unlock(&devices_kset->list_lock);
2007 * devices_kset_move_after - Move device in the devices_kset's list.
2008 * @deva: Device to move
2009 * @devb: Device @deva should come after.
2011 static void devices_kset_move_after(struct device *deva, struct device *devb)
2015 pr_debug("devices_kset: Moving %s after %s\n",
2016 dev_name(deva), dev_name(devb));
2017 spin_lock(&devices_kset->list_lock);
2018 list_move(&deva->kobj.entry, &devb->kobj.entry);
2019 spin_unlock(&devices_kset->list_lock);
2023 * devices_kset_move_last - move the device to the end of devices_kset's list.
2024 * @dev: device to move
2026 void devices_kset_move_last(struct device *dev)
2030 pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev));
2031 spin_lock(&devices_kset->list_lock);
2032 list_move_tail(&dev->kobj.entry, &devices_kset->list);
2033 spin_unlock(&devices_kset->list_lock);
2037 * device_create_file - create sysfs attribute file for device.
2039 * @attr: device attribute descriptor.
2041 int device_create_file(struct device *dev,
2042 const struct device_attribute *attr)
2047 WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
2048 "Attribute %s: write permission without 'store'\n",
2050 WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
2051 "Attribute %s: read permission without 'show'\n",
2053 error = sysfs_create_file(&dev->kobj, &attr->attr);
2058 EXPORT_SYMBOL_GPL(device_create_file);
2061 * device_remove_file - remove sysfs attribute file.
2063 * @attr: device attribute descriptor.
2065 void device_remove_file(struct device *dev,
2066 const struct device_attribute *attr)
2069 sysfs_remove_file(&dev->kobj, &attr->attr);
2071 EXPORT_SYMBOL_GPL(device_remove_file);
2074 * device_remove_file_self - remove sysfs attribute file from its own method.
2076 * @attr: device attribute descriptor.
2078 * See kernfs_remove_self() for details.
2080 bool device_remove_file_self(struct device *dev,
2081 const struct device_attribute *attr)
2084 return sysfs_remove_file_self(&dev->kobj, &attr->attr);
2088 EXPORT_SYMBOL_GPL(device_remove_file_self);
2091 * device_create_bin_file - create sysfs binary attribute file for device.
2093 * @attr: device binary attribute descriptor.
2095 int device_create_bin_file(struct device *dev,
2096 const struct bin_attribute *attr)
2098 int error = -EINVAL;
2100 error = sysfs_create_bin_file(&dev->kobj, attr);
2103 EXPORT_SYMBOL_GPL(device_create_bin_file);
2106 * device_remove_bin_file - remove sysfs binary attribute file
2108 * @attr: device binary attribute descriptor.
2110 void device_remove_bin_file(struct device *dev,
2111 const struct bin_attribute *attr)
2114 sysfs_remove_bin_file(&dev->kobj, attr);
2116 EXPORT_SYMBOL_GPL(device_remove_bin_file);
2118 static void klist_children_get(struct klist_node *n)
2120 struct device_private *p = to_device_private_parent(n);
2121 struct device *dev = p->device;
2126 static void klist_children_put(struct klist_node *n)
2128 struct device_private *p = to_device_private_parent(n);
2129 struct device *dev = p->device;
2135 * device_initialize - init device structure.
2138 * This prepares the device for use by other layers by initializing
2140 * It is the first half of device_register(), if called by
2141 * that function, though it can also be called separately, so one
2142 * may use @dev's fields. In particular, get_device()/put_device()
2143 * may be used for reference counting of @dev after calling this
2146 * All fields in @dev must be initialized by the caller to 0, except
2147 * for those explicitly set to some other value. The simplest
2148 * approach is to use kzalloc() to allocate the structure containing
2151 * NOTE: Use put_device() to give up your reference instead of freeing
2152 * @dev directly once you have called this function.
2154 void device_initialize(struct device *dev)
2156 dev->kobj.kset = devices_kset;
2157 kobject_init(&dev->kobj, &device_ktype);
2158 INIT_LIST_HEAD(&dev->dma_pools);
2159 mutex_init(&dev->mutex);
2160 #ifdef CONFIG_PROVE_LOCKING
2161 mutex_init(&dev->lockdep_mutex);
2163 lockdep_set_novalidate_class(&dev->mutex);
2164 spin_lock_init(&dev->devres_lock);
2165 INIT_LIST_HEAD(&dev->devres_head);
2166 device_pm_init(dev);
2167 set_dev_node(dev, -1);
2168 #ifdef CONFIG_GENERIC_MSI_IRQ
2169 INIT_LIST_HEAD(&dev->msi_list);
2171 INIT_LIST_HEAD(&dev->links.consumers);
2172 INIT_LIST_HEAD(&dev->links.suppliers);
2173 INIT_LIST_HEAD(&dev->links.needs_suppliers);
2174 INIT_LIST_HEAD(&dev->links.defer_sync);
2175 dev->links.status = DL_DEV_NO_DRIVER;
2177 EXPORT_SYMBOL_GPL(device_initialize);
2179 struct kobject *virtual_device_parent(struct device *dev)
2181 static struct kobject *virtual_dir = NULL;
2184 virtual_dir = kobject_create_and_add("virtual",
2185 &devices_kset->kobj);
2191 struct kobject kobj;
2192 struct class *class;
2195 #define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
2197 static void class_dir_release(struct kobject *kobj)
2199 struct class_dir *dir = to_class_dir(kobj);
2204 struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
2206 struct class_dir *dir = to_class_dir(kobj);
2207 return dir->class->ns_type;
2210 static struct kobj_type class_dir_ktype = {
2211 .release = class_dir_release,
2212 .sysfs_ops = &kobj_sysfs_ops,
2213 .child_ns_type = class_dir_child_ns_type
2216 static struct kobject *
2217 class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
2219 struct class_dir *dir;
2222 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
2224 return ERR_PTR(-ENOMEM);
2227 kobject_init(&dir->kobj, &class_dir_ktype);
2229 dir->kobj.kset = &class->p->glue_dirs;
2231 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
2233 kobject_put(&dir->kobj);
2234 return ERR_PTR(retval);
2239 static DEFINE_MUTEX(gdp_mutex);
2241 static struct kobject *get_device_parent(struct device *dev,
2242 struct device *parent)
2245 struct kobject *kobj = NULL;
2246 struct kobject *parent_kobj;
2250 /* block disks show up in /sys/block */
2251 if (sysfs_deprecated && dev->class == &block_class) {
2252 if (parent && parent->class == &block_class)
2253 return &parent->kobj;
2254 return &block_class.p->subsys.kobj;
2259 * If we have no parent, we live in "virtual".
2260 * Class-devices with a non class-device as parent, live
2261 * in a "glue" directory to prevent namespace collisions.
2264 parent_kobj = virtual_device_parent(dev);
2265 else if (parent->class && !dev->class->ns_type)
2266 return &parent->kobj;
2268 parent_kobj = &parent->kobj;
2270 mutex_lock(&gdp_mutex);
2272 /* find our class-directory at the parent and reference it */
2273 spin_lock(&dev->class->p->glue_dirs.list_lock);
2274 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
2275 if (k->parent == parent_kobj) {
2276 kobj = kobject_get(k);
2279 spin_unlock(&dev->class->p->glue_dirs.list_lock);
2281 mutex_unlock(&gdp_mutex);
2285 /* or create a new class-directory at the parent device */
2286 k = class_dir_create_and_add(dev->class, parent_kobj);
2287 /* do not emit an uevent for this simple "glue" directory */
2288 mutex_unlock(&gdp_mutex);
2292 /* subsystems can specify a default root directory for their devices */
2293 if (!parent && dev->bus && dev->bus->dev_root)
2294 return &dev->bus->dev_root->kobj;
2297 return &parent->kobj;
2301 static inline bool live_in_glue_dir(struct kobject *kobj,
2304 if (!kobj || !dev->class ||
2305 kobj->kset != &dev->class->p->glue_dirs)
2310 static inline struct kobject *get_glue_dir(struct device *dev)
2312 return dev->kobj.parent;
2316 * make sure cleaning up dir as the last step, we need to make
2317 * sure .release handler of kobject is run with holding the
2320 static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
2324 /* see if we live in a "glue" directory */
2325 if (!live_in_glue_dir(glue_dir, dev))
2328 mutex_lock(&gdp_mutex);
2330 * There is a race condition between removing glue directory
2331 * and adding a new device under the glue directory.
2336 * get_device_parent()
2337 * class_dir_create_and_add()
2338 * kobject_add_internal()
2339 * create_dir() // create glue_dir
2342 * get_device_parent()
2343 * kobject_get() // get glue_dir
2346 * cleanup_glue_dir()
2347 * kobject_del(glue_dir)
2350 * kobject_add_internal()
2351 * create_dir() // in glue_dir
2352 * sysfs_create_dir_ns()
2353 * kernfs_create_dir_ns(sd)
2355 * sysfs_remove_dir() // glue_dir->sd=NULL
2356 * sysfs_put() // free glue_dir->sd
2359 * kernfs_new_node(sd)
2360 * kernfs_get(glue_dir)
2364 * Before CPU1 remove last child device under glue dir, if CPU2 add
2365 * a new device under glue dir, the glue_dir kobject reference count
2366 * will be increase to 2 in kobject_get(k). And CPU2 has been called
2367 * kernfs_create_dir_ns(). Meanwhile, CPU1 call sysfs_remove_dir()
2368 * and sysfs_put(). This result in glue_dir->sd is freed.
2370 * Then the CPU2 will see a stale "empty" but still potentially used
2371 * glue dir around in kernfs_new_node().
2373 * In order to avoid this happening, we also should make sure that
2374 * kernfs_node for glue_dir is released in CPU1 only when refcount
2375 * for glue_dir kobj is 1.
2377 ref = kref_read(&glue_dir->kref);
2378 if (!kobject_has_children(glue_dir) && !--ref)
2379 kobject_del(glue_dir);
2380 kobject_put(glue_dir);
2381 mutex_unlock(&gdp_mutex);
2384 static int device_add_class_symlinks(struct device *dev)
2386 struct device_node *of_node = dev_of_node(dev);
2390 error = sysfs_create_link(&dev->kobj, of_node_kobj(of_node), "of_node");
2392 dev_warn(dev, "Error %d creating of_node link\n",error);
2393 /* An error here doesn't warrant bringing down the device */
2399 error = sysfs_create_link(&dev->kobj,
2400 &dev->class->p->subsys.kobj,
2405 if (dev->parent && device_is_not_partition(dev)) {
2406 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
2413 /* /sys/block has directories and does not need symlinks */
2414 if (sysfs_deprecated && dev->class == &block_class)
2418 /* link in the class directory pointing to the device */
2419 error = sysfs_create_link(&dev->class->p->subsys.kobj,
2420 &dev->kobj, dev_name(dev));
2427 sysfs_remove_link(&dev->kobj, "device");
2430 sysfs_remove_link(&dev->kobj, "subsystem");
2432 sysfs_remove_link(&dev->kobj, "of_node");
2436 static void device_remove_class_symlinks(struct device *dev)
2438 if (dev_of_node(dev))
2439 sysfs_remove_link(&dev->kobj, "of_node");
2444 if (dev->parent && device_is_not_partition(dev))
2445 sysfs_remove_link(&dev->kobj, "device");
2446 sysfs_remove_link(&dev->kobj, "subsystem");
2448 if (sysfs_deprecated && dev->class == &block_class)
2451 sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
2455 * dev_set_name - set a device name
2457 * @fmt: format string for the device's name
2459 int dev_set_name(struct device *dev, const char *fmt, ...)
2464 va_start(vargs, fmt);
2465 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
2469 EXPORT_SYMBOL_GPL(dev_set_name);
2472 * device_to_dev_kobj - select a /sys/dev/ directory for the device
2475 * By default we select char/ for new entries. Setting class->dev_obj
2476 * to NULL prevents an entry from being created. class->dev_kobj must
2477 * be set (or cleared) before any devices are registered to the class
2478 * otherwise device_create_sys_dev_entry() and
2479 * device_remove_sys_dev_entry() will disagree about the presence of
2482 static struct kobject *device_to_dev_kobj(struct device *dev)
2484 struct kobject *kobj;
2487 kobj = dev->class->dev_kobj;
2489 kobj = sysfs_dev_char_kobj;
2494 static int device_create_sys_dev_entry(struct device *dev)
2496 struct kobject *kobj = device_to_dev_kobj(dev);
2501 format_dev_t(devt_str, dev->devt);
2502 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
2508 static void device_remove_sys_dev_entry(struct device *dev)
2510 struct kobject *kobj = device_to_dev_kobj(dev);
2514 format_dev_t(devt_str, dev->devt);
2515 sysfs_remove_link(kobj, devt_str);
2519 static int device_private_init(struct device *dev)
2521 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
2524 dev->p->device = dev;
2525 klist_init(&dev->p->klist_children, klist_children_get,
2526 klist_children_put);
2527 INIT_LIST_HEAD(&dev->p->deferred_probe);
2532 * device_add - add device to device hierarchy.
2535 * This is part 2 of device_register(), though may be called
2536 * separately _iff_ device_initialize() has been called separately.
2538 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
2539 * to the global and sibling lists for the device, then
2540 * adds it to the other relevant subsystems of the driver model.
2542 * Do not call this routine or device_register() more than once for
2543 * any device structure. The driver model core is not designed to work
2544 * with devices that get unregistered and then spring back to life.
2545 * (Among other things, it's very hard to guarantee that all references
2546 * to the previous incarnation of @dev have been dropped.) Allocate
2547 * and register a fresh new struct device instead.
2549 * NOTE: _Never_ directly free @dev after calling this function, even
2550 * if it returned an error! Always use put_device() to give up your
2551 * reference instead.
2553 * Rule of thumb is: if device_add() succeeds, you should call
2554 * device_del() when you want to get rid of it. If device_add() has
2555 * *not* succeeded, use *only* put_device() to drop the reference
2558 int device_add(struct device *dev)
2560 struct device *parent;
2561 struct kobject *kobj;
2562 struct class_interface *class_intf;
2563 int error = -EINVAL;
2564 struct kobject *glue_dir = NULL;
2566 dev = get_device(dev);
2571 error = device_private_init(dev);
2577 * for statically allocated devices, which should all be converted
2578 * some day, we need to initialize the name. We prevent reading back
2579 * the name, and force the use of dev_name()
2581 if (dev->init_name) {
2582 dev_set_name(dev, "%s", dev->init_name);
2583 dev->init_name = NULL;
2586 /* subsystems can specify simple device enumeration */
2587 if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
2588 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
2590 if (!dev_name(dev)) {
2595 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
2597 parent = get_device(dev->parent);
2598 kobj = get_device_parent(dev, parent);
2600 error = PTR_ERR(kobj);
2604 dev->kobj.parent = kobj;
2606 /* use parent numa_node */
2607 if (parent && (dev_to_node(dev) == NUMA_NO_NODE))
2608 set_dev_node(dev, dev_to_node(parent));
2610 /* first, register with generic layer. */
2611 /* we require the name to be set before, and pass NULL */
2612 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
2614 glue_dir = get_glue_dir(dev);
2618 /* notify platform of device entry */
2619 error = device_platform_notify(dev, KOBJ_ADD);
2621 goto platform_error;
2623 error = device_create_file(dev, &dev_attr_uevent);
2627 error = device_add_class_symlinks(dev);
2630 error = device_add_attrs(dev);
2633 error = bus_add_device(dev);
2636 error = dpm_sysfs_add(dev);
2641 if (MAJOR(dev->devt)) {
2642 error = device_create_file(dev, &dev_attr_dev);
2646 error = device_create_sys_dev_entry(dev);
2650 devtmpfs_create_node(dev);
2653 /* Notify clients of device addition. This call must come
2654 * after dpm_sysfs_add() and before kobject_uevent().
2657 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2658 BUS_NOTIFY_ADD_DEVICE, dev);
2660 kobject_uevent(&dev->kobj, KOBJ_ADD);
2663 * Check if any of the other devices (consumers) have been waiting for
2664 * this device (supplier) to be added so that they can create a device
2667 * This needs to happen after device_pm_add() because device_link_add()
2668 * requires the supplier be registered before it's called.
2670 * But this also needs to happen before bus_probe_device() to make sure
2671 * waiting consumers can link to it before the driver is bound to the
2672 * device and the driver sync_state callback is called for this device.
2674 if (dev->fwnode && !dev->fwnode->dev) {
2675 dev->fwnode->dev = dev;
2676 fw_devlink_link_device(dev);
2679 bus_probe_device(dev);
2681 klist_add_tail(&dev->p->knode_parent,
2682 &parent->p->klist_children);
2685 mutex_lock(&dev->class->p->mutex);
2686 /* tie the class to the device */
2687 klist_add_tail(&dev->p->knode_class,
2688 &dev->class->p->klist_devices);
2690 /* notify any interfaces that the device is here */
2691 list_for_each_entry(class_intf,
2692 &dev->class->p->interfaces, node)
2693 if (class_intf->add_dev)
2694 class_intf->add_dev(dev, class_intf);
2695 mutex_unlock(&dev->class->p->mutex);
2701 if (MAJOR(dev->devt))
2702 device_remove_file(dev, &dev_attr_dev);
2704 device_pm_remove(dev);
2705 dpm_sysfs_remove(dev);
2707 bus_remove_device(dev);
2709 device_remove_attrs(dev);
2711 device_remove_class_symlinks(dev);
2713 device_remove_file(dev, &dev_attr_uevent);
2715 device_platform_notify(dev, KOBJ_REMOVE);
2717 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
2718 glue_dir = get_glue_dir(dev);
2719 kobject_del(&dev->kobj);
2721 cleanup_glue_dir(dev, glue_dir);
2729 EXPORT_SYMBOL_GPL(device_add);
2732 * device_register - register a device with the system.
2733 * @dev: pointer to the device structure
2735 * This happens in two clean steps - initialize the device
2736 * and add it to the system. The two steps can be called
2737 * separately, but this is the easiest and most common.
2738 * I.e. you should only call the two helpers separately if
2739 * have a clearly defined need to use and refcount the device
2740 * before it is added to the hierarchy.
2742 * For more information, see the kerneldoc for device_initialize()
2745 * NOTE: _Never_ directly free @dev after calling this function, even
2746 * if it returned an error! Always use put_device() to give up the
2747 * reference initialized in this function instead.
2749 int device_register(struct device *dev)
2751 device_initialize(dev);
2752 return device_add(dev);
2754 EXPORT_SYMBOL_GPL(device_register);
2757 * get_device - increment reference count for device.
2760 * This simply forwards the call to kobject_get(), though
2761 * we do take care to provide for the case that we get a NULL
2762 * pointer passed in.
2764 struct device *get_device(struct device *dev)
2766 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
2768 EXPORT_SYMBOL_GPL(get_device);
2771 * put_device - decrement reference count.
2772 * @dev: device in question.
2774 void put_device(struct device *dev)
2776 /* might_sleep(); */
2778 kobject_put(&dev->kobj);
2780 EXPORT_SYMBOL_GPL(put_device);
2782 bool kill_device(struct device *dev)
2785 * Require the device lock and set the "dead" flag to guarantee that
2786 * the update behavior is consistent with the other bitfields near
2787 * it and that we cannot have an asynchronous probe routine trying
2788 * to run while we are tearing out the bus/class/sysfs from
2789 * underneath the device.
2791 lockdep_assert_held(&dev->mutex);
2795 dev->p->dead = true;
2798 EXPORT_SYMBOL_GPL(kill_device);
2801 * device_del - delete device from system.
2804 * This is the first part of the device unregistration
2805 * sequence. This removes the device from the lists we control
2806 * from here, has it removed from the other driver model
2807 * subsystems it was added to in device_add(), and removes it
2808 * from the kobject hierarchy.
2810 * NOTE: this should be called manually _iff_ device_add() was
2811 * also called manually.
2813 void device_del(struct device *dev)
2815 struct device *parent = dev->parent;
2816 struct kobject *glue_dir = NULL;
2817 struct class_interface *class_intf;
2823 if (dev->fwnode && dev->fwnode->dev == dev)
2824 dev->fwnode->dev = NULL;
2826 /* Notify clients of device removal. This call must come
2827 * before dpm_sysfs_remove().
2830 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2831 BUS_NOTIFY_DEL_DEVICE, dev);
2833 dpm_sysfs_remove(dev);
2835 klist_del(&dev->p->knode_parent);
2836 if (MAJOR(dev->devt)) {
2837 devtmpfs_delete_node(dev);
2838 device_remove_sys_dev_entry(dev);
2839 device_remove_file(dev, &dev_attr_dev);
2842 device_remove_class_symlinks(dev);
2844 mutex_lock(&dev->class->p->mutex);
2845 /* notify any interfaces that the device is now gone */
2846 list_for_each_entry(class_intf,
2847 &dev->class->p->interfaces, node)
2848 if (class_intf->remove_dev)
2849 class_intf->remove_dev(dev, class_intf);
2850 /* remove the device from the class list */
2851 klist_del(&dev->p->knode_class);
2852 mutex_unlock(&dev->class->p->mutex);
2854 device_remove_file(dev, &dev_attr_uevent);
2855 device_remove_attrs(dev);
2856 bus_remove_device(dev);
2857 device_pm_remove(dev);
2858 driver_deferred_probe_del(dev);
2859 device_platform_notify(dev, KOBJ_REMOVE);
2860 device_remove_properties(dev);
2861 device_links_purge(dev);
2864 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2865 BUS_NOTIFY_REMOVED_DEVICE, dev);
2866 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
2867 glue_dir = get_glue_dir(dev);
2868 kobject_del(&dev->kobj);
2869 cleanup_glue_dir(dev, glue_dir);
2872 EXPORT_SYMBOL_GPL(device_del);
2875 * device_unregister - unregister device from system.
2876 * @dev: device going away.
2878 * We do this in two parts, like we do device_register(). First,
2879 * we remove it from all the subsystems with device_del(), then
2880 * we decrement the reference count via put_device(). If that
2881 * is the final reference count, the device will be cleaned up
2882 * via device_release() above. Otherwise, the structure will
2883 * stick around until the final reference to the device is dropped.
2885 void device_unregister(struct device *dev)
2887 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
2891 EXPORT_SYMBOL_GPL(device_unregister);
2893 static struct device *prev_device(struct klist_iter *i)
2895 struct klist_node *n = klist_prev(i);
2896 struct device *dev = NULL;
2897 struct device_private *p;
2900 p = to_device_private_parent(n);
2906 static struct device *next_device(struct klist_iter *i)
2908 struct klist_node *n = klist_next(i);
2909 struct device *dev = NULL;
2910 struct device_private *p;
2913 p = to_device_private_parent(n);
2920 * device_get_devnode - path of device node file
2922 * @mode: returned file access mode
2923 * @uid: returned file owner
2924 * @gid: returned file group
2925 * @tmp: possibly allocated string
2927 * Return the relative path of a possible device node.
2928 * Non-default names may need to allocate a memory to compose
2929 * a name. This memory is returned in tmp and needs to be
2930 * freed by the caller.
2932 const char *device_get_devnode(struct device *dev,
2933 umode_t *mode, kuid_t *uid, kgid_t *gid,
2940 /* the device type may provide a specific name */
2941 if (dev->type && dev->type->devnode)
2942 *tmp = dev->type->devnode(dev, mode, uid, gid);
2946 /* the class may provide a specific name */
2947 if (dev->class && dev->class->devnode)
2948 *tmp = dev->class->devnode(dev, mode);
2952 /* return name without allocation, tmp == NULL */
2953 if (strchr(dev_name(dev), '!') == NULL)
2954 return dev_name(dev);
2956 /* replace '!' in the name with '/' */
2957 s = kstrdup(dev_name(dev), GFP_KERNEL);
2960 strreplace(s, '!', '/');
2965 * device_for_each_child - device child iterator.
2966 * @parent: parent struct device.
2967 * @fn: function to be called for each device.
2968 * @data: data for the callback.
2970 * Iterate over @parent's child devices, and call @fn for each,
2973 * We check the return of @fn each time. If it returns anything
2974 * other than 0, we break out and return that value.
2976 int device_for_each_child(struct device *parent, void *data,
2977 int (*fn)(struct device *dev, void *data))
2979 struct klist_iter i;
2980 struct device *child;
2986 klist_iter_init(&parent->p->klist_children, &i);
2987 while (!error && (child = next_device(&i)))
2988 error = fn(child, data);
2989 klist_iter_exit(&i);
2992 EXPORT_SYMBOL_GPL(device_for_each_child);
2995 * device_for_each_child_reverse - device child iterator in reversed order.
2996 * @parent: parent struct device.
2997 * @fn: function to be called for each device.
2998 * @data: data for the callback.
3000 * Iterate over @parent's child devices, and call @fn for each,
3003 * We check the return of @fn each time. If it returns anything
3004 * other than 0, we break out and return that value.
3006 int device_for_each_child_reverse(struct device *parent, void *data,
3007 int (*fn)(struct device *dev, void *data))
3009 struct klist_iter i;
3010 struct device *child;
3016 klist_iter_init(&parent->p->klist_children, &i);
3017 while ((child = prev_device(&i)) && !error)
3018 error = fn(child, data);
3019 klist_iter_exit(&i);
3022 EXPORT_SYMBOL_GPL(device_for_each_child_reverse);
3025 * device_find_child - device iterator for locating a particular device.
3026 * @parent: parent struct device
3027 * @match: Callback function to check device
3028 * @data: Data to pass to match function
3030 * This is similar to the device_for_each_child() function above, but it
3031 * returns a reference to a device that is 'found' for later use, as
3032 * determined by the @match callback.
3034 * The callback should return 0 if the device doesn't match and non-zero
3035 * if it does. If the callback returns non-zero and a reference to the
3036 * current device can be obtained, this function will return to the caller
3037 * and not iterate over any more devices.
3039 * NOTE: you will need to drop the reference with put_device() after use.
3041 struct device *device_find_child(struct device *parent, void *data,
3042 int (*match)(struct device *dev, void *data))
3044 struct klist_iter i;
3045 struct device *child;
3050 klist_iter_init(&parent->p->klist_children, &i);
3051 while ((child = next_device(&i)))
3052 if (match(child, data) && get_device(child))
3054 klist_iter_exit(&i);
3057 EXPORT_SYMBOL_GPL(device_find_child);
3060 * device_find_child_by_name - device iterator for locating a child device.
3061 * @parent: parent struct device
3062 * @name: name of the child device
3064 * This is similar to the device_find_child() function above, but it
3065 * returns a reference to a device that has the name @name.
3067 * NOTE: you will need to drop the reference with put_device() after use.
3069 struct device *device_find_child_by_name(struct device *parent,
3072 struct klist_iter i;
3073 struct device *child;
3078 klist_iter_init(&parent->p->klist_children, &i);
3079 while ((child = next_device(&i)))
3080 if (!strcmp(dev_name(child), name) && get_device(child))
3082 klist_iter_exit(&i);
3085 EXPORT_SYMBOL_GPL(device_find_child_by_name);
3087 int __init devices_init(void)
3089 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
3092 dev_kobj = kobject_create_and_add("dev", NULL);
3095 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
3096 if (!sysfs_dev_block_kobj)
3097 goto block_kobj_err;
3098 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
3099 if (!sysfs_dev_char_kobj)
3105 kobject_put(sysfs_dev_block_kobj);
3107 kobject_put(dev_kobj);
3109 kset_unregister(devices_kset);
3113 static int device_check_offline(struct device *dev, void *not_used)
3117 ret = device_for_each_child(dev, NULL, device_check_offline);
3121 return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
3125 * device_offline - Prepare the device for hot-removal.
3126 * @dev: Device to be put offline.
3128 * Execute the device bus type's .offline() callback, if present, to prepare
3129 * the device for a subsequent hot-removal. If that succeeds, the device must
3130 * not be used until either it is removed or its bus type's .online() callback
3133 * Call under device_hotplug_lock.
3135 int device_offline(struct device *dev)
3139 if (dev->offline_disabled)
3142 ret = device_for_each_child(dev, NULL, device_check_offline);
3147 if (device_supports_offline(dev)) {
3151 ret = dev->bus->offline(dev);
3153 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
3154 dev->offline = true;
3164 * device_online - Put the device back online after successful device_offline().
3165 * @dev: Device to be put back online.
3167 * If device_offline() has been successfully executed for @dev, but the device
3168 * has not been removed subsequently, execute its bus type's .online() callback
3169 * to indicate that the device can be used again.
3171 * Call under device_hotplug_lock.
3173 int device_online(struct device *dev)
3178 if (device_supports_offline(dev)) {
3180 ret = dev->bus->online(dev);
3182 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
3183 dev->offline = false;
3194 struct root_device {
3196 struct module *owner;
3199 static inline struct root_device *to_root_device(struct device *d)
3201 return container_of(d, struct root_device, dev);
3204 static void root_device_release(struct device *dev)
3206 kfree(to_root_device(dev));
3210 * __root_device_register - allocate and register a root device
3211 * @name: root device name
3212 * @owner: owner module of the root device, usually THIS_MODULE
3214 * This function allocates a root device and registers it
3215 * using device_register(). In order to free the returned
3216 * device, use root_device_unregister().
3218 * Root devices are dummy devices which allow other devices
3219 * to be grouped under /sys/devices. Use this function to
3220 * allocate a root device and then use it as the parent of
3221 * any device which should appear under /sys/devices/{name}
3223 * The /sys/devices/{name} directory will also contain a
3224 * 'module' symlink which points to the @owner directory
3227 * Returns &struct device pointer on success, or ERR_PTR() on error.
3229 * Note: You probably want to use root_device_register().
3231 struct device *__root_device_register(const char *name, struct module *owner)
3233 struct root_device *root;
3236 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
3238 return ERR_PTR(err);
3240 err = dev_set_name(&root->dev, "%s", name);
3243 return ERR_PTR(err);
3246 root->dev.release = root_device_release;
3248 err = device_register(&root->dev);
3250 put_device(&root->dev);
3251 return ERR_PTR(err);
3254 #ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
3256 struct module_kobject *mk = &owner->mkobj;
3258 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
3260 device_unregister(&root->dev);
3261 return ERR_PTR(err);
3263 root->owner = owner;
3269 EXPORT_SYMBOL_GPL(__root_device_register);
3272 * root_device_unregister - unregister and free a root device
3273 * @dev: device going away
3275 * This function unregisters and cleans up a device that was created by
3276 * root_device_register().
3278 void root_device_unregister(struct device *dev)
3280 struct root_device *root = to_root_device(dev);
3283 sysfs_remove_link(&root->dev.kobj, "module");
3285 device_unregister(dev);
3287 EXPORT_SYMBOL_GPL(root_device_unregister);
3290 static void device_create_release(struct device *dev)
3292 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
3296 static __printf(6, 0) struct device *
3297 device_create_groups_vargs(struct class *class, struct device *parent,
3298 dev_t devt, void *drvdata,
3299 const struct attribute_group **groups,
3300 const char *fmt, va_list args)
3302 struct device *dev = NULL;
3303 int retval = -ENODEV;
3305 if (class == NULL || IS_ERR(class))
3308 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
3314 device_initialize(dev);
3317 dev->parent = parent;
3318 dev->groups = groups;
3319 dev->release = device_create_release;
3320 dev_set_drvdata(dev, drvdata);
3322 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
3326 retval = device_add(dev);
3334 return ERR_PTR(retval);
3338 * device_create - creates a device and registers it with sysfs
3339 * @class: pointer to the struct class that this device should be registered to
3340 * @parent: pointer to the parent struct device of this new device, if any
3341 * @devt: the dev_t for the char device to be added
3342 * @drvdata: the data to be added to the device for callbacks
3343 * @fmt: string for the device's name
3345 * This function can be used by char device classes. A struct device
3346 * will be created in sysfs, registered to the specified class.
3348 * A "dev" file will be created, showing the dev_t for the device, if
3349 * the dev_t is not 0,0.
3350 * If a pointer to a parent struct device is passed in, the newly created
3351 * struct device will be a child of that device in sysfs.
3352 * The pointer to the struct device will be returned from the call.
3353 * Any further sysfs files that might be required can be created using this
3356 * Returns &struct device pointer on success, or ERR_PTR() on error.
3358 * Note: the struct class passed to this function must have previously
3359 * been created with a call to class_create().
3361 struct device *device_create(struct class *class, struct device *parent,
3362 dev_t devt, void *drvdata, const char *fmt, ...)
3367 va_start(vargs, fmt);
3368 dev = device_create_groups_vargs(class, parent, devt, drvdata, NULL,
3373 EXPORT_SYMBOL_GPL(device_create);
3376 * device_create_with_groups - creates a device and registers it with sysfs
3377 * @class: pointer to the struct class that this device should be registered to
3378 * @parent: pointer to the parent struct device of this new device, if any
3379 * @devt: the dev_t for the char device to be added
3380 * @drvdata: the data to be added to the device for callbacks
3381 * @groups: NULL-terminated list of attribute groups to be created
3382 * @fmt: string for the device's name
3384 * This function can be used by char device classes. A struct device
3385 * will be created in sysfs, registered to the specified class.
3386 * Additional attributes specified in the groups parameter will also
3387 * be created automatically.
3389 * A "dev" file will be created, showing the dev_t for the device, if
3390 * the dev_t is not 0,0.
3391 * If a pointer to a parent struct device is passed in, the newly created
3392 * struct device will be a child of that device in sysfs.
3393 * The pointer to the struct device will be returned from the call.
3394 * Any further sysfs files that might be required can be created using this
3397 * Returns &struct device pointer on success, or ERR_PTR() on error.
3399 * Note: the struct class passed to this function must have previously
3400 * been created with a call to class_create().
3402 struct device *device_create_with_groups(struct class *class,
3403 struct device *parent, dev_t devt,
3405 const struct attribute_group **groups,
3406 const char *fmt, ...)
3411 va_start(vargs, fmt);
3412 dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
3417 EXPORT_SYMBOL_GPL(device_create_with_groups);
3420 * device_destroy - removes a device that was created with device_create()
3421 * @class: pointer to the struct class that this device was registered with
3422 * @devt: the dev_t of the device that was previously registered
3424 * This call unregisters and cleans up a device that was created with a
3425 * call to device_create().
3427 void device_destroy(struct class *class, dev_t devt)
3431 dev = class_find_device_by_devt(class, devt);
3434 device_unregister(dev);
3437 EXPORT_SYMBOL_GPL(device_destroy);
3440 * device_rename - renames a device
3441 * @dev: the pointer to the struct device to be renamed
3442 * @new_name: the new name of the device
3444 * It is the responsibility of the caller to provide mutual
3445 * exclusion between two different calls of device_rename
3446 * on the same device to ensure that new_name is valid and
3447 * won't conflict with other devices.
3449 * Note: Don't call this function. Currently, the networking layer calls this
3450 * function, but that will change. The following text from Kay Sievers offers
3453 * Renaming devices is racy at many levels, symlinks and other stuff are not
3454 * replaced atomically, and you get a "move" uevent, but it's not easy to
3455 * connect the event to the old and new device. Device nodes are not renamed at
3456 * all, there isn't even support for that in the kernel now.
3458 * In the meantime, during renaming, your target name might be taken by another
3459 * driver, creating conflicts. Or the old name is taken directly after you
3460 * renamed it -- then you get events for the same DEVPATH, before you even see
3461 * the "move" event. It's just a mess, and nothing new should ever rely on
3462 * kernel device renaming. Besides that, it's not even implemented now for
3463 * other things than (driver-core wise very simple) network devices.
3465 * We are currently about to change network renaming in udev to completely
3466 * disallow renaming of devices in the same namespace as the kernel uses,
3467 * because we can't solve the problems properly, that arise with swapping names
3468 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
3469 * be allowed to some other name than eth[0-9]*, for the aforementioned
3472 * Make up a "real" name in the driver before you register anything, or add
3473 * some other attributes for userspace to find the device, or use udev to add
3474 * symlinks -- but never rename kernel devices later, it's a complete mess. We
3475 * don't even want to get into that and try to implement the missing pieces in
3476 * the core. We really have other pieces to fix in the driver core mess. :)
3478 int device_rename(struct device *dev, const char *new_name)
3480 struct kobject *kobj = &dev->kobj;
3481 char *old_device_name = NULL;
3484 dev = get_device(dev);
3488 dev_dbg(dev, "renaming to %s\n", new_name);
3490 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
3491 if (!old_device_name) {
3497 error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj,
3498 kobj, old_device_name,
3499 new_name, kobject_namespace(kobj));
3504 error = kobject_rename(kobj, new_name);
3511 kfree(old_device_name);
3515 EXPORT_SYMBOL_GPL(device_rename);
3517 static int device_move_class_links(struct device *dev,
3518 struct device *old_parent,
3519 struct device *new_parent)
3524 sysfs_remove_link(&dev->kobj, "device");
3526 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
3532 * device_move - moves a device to a new parent
3533 * @dev: the pointer to the struct device to be moved
3534 * @new_parent: the new parent of the device (can be NULL)
3535 * @dpm_order: how to reorder the dpm_list
3537 int device_move(struct device *dev, struct device *new_parent,
3538 enum dpm_order dpm_order)
3541 struct device *old_parent;
3542 struct kobject *new_parent_kobj;
3544 dev = get_device(dev);
3549 new_parent = get_device(new_parent);
3550 new_parent_kobj = get_device_parent(dev, new_parent);
3551 if (IS_ERR(new_parent_kobj)) {
3552 error = PTR_ERR(new_parent_kobj);
3553 put_device(new_parent);
3557 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
3558 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
3559 error = kobject_move(&dev->kobj, new_parent_kobj);
3561 cleanup_glue_dir(dev, new_parent_kobj);
3562 put_device(new_parent);
3565 old_parent = dev->parent;
3566 dev->parent = new_parent;
3568 klist_remove(&dev->p->knode_parent);
3570 klist_add_tail(&dev->p->knode_parent,
3571 &new_parent->p->klist_children);
3572 set_dev_node(dev, dev_to_node(new_parent));
3576 error = device_move_class_links(dev, old_parent, new_parent);
3578 /* We ignore errors on cleanup since we're hosed anyway... */
3579 device_move_class_links(dev, new_parent, old_parent);
3580 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
3582 klist_remove(&dev->p->knode_parent);
3583 dev->parent = old_parent;
3585 klist_add_tail(&dev->p->knode_parent,
3586 &old_parent->p->klist_children);
3587 set_dev_node(dev, dev_to_node(old_parent));
3590 cleanup_glue_dir(dev, new_parent_kobj);
3591 put_device(new_parent);
3595 switch (dpm_order) {
3596 case DPM_ORDER_NONE:
3598 case DPM_ORDER_DEV_AFTER_PARENT:
3599 device_pm_move_after(dev, new_parent);
3600 devices_kset_move_after(dev, new_parent);
3602 case DPM_ORDER_PARENT_BEFORE_DEV:
3603 device_pm_move_before(new_parent, dev);
3604 devices_kset_move_before(new_parent, dev);
3606 case DPM_ORDER_DEV_LAST:
3607 device_pm_move_last(dev);
3608 devices_kset_move_last(dev);
3612 put_device(old_parent);
3618 EXPORT_SYMBOL_GPL(device_move);
3620 static int device_attrs_change_owner(struct device *dev, kuid_t kuid,
3623 struct kobject *kobj = &dev->kobj;
3624 struct class *class = dev->class;
3625 const struct device_type *type = dev->type;
3630 * Change the device groups of the device class for @dev to
3633 error = sysfs_groups_change_owner(kobj, class->dev_groups, kuid,
3641 * Change the device groups of the device type for @dev to
3644 error = sysfs_groups_change_owner(kobj, type->groups, kuid,
3650 /* Change the device groups of @dev to @kuid/@kgid. */
3651 error = sysfs_groups_change_owner(kobj, dev->groups, kuid, kgid);
3655 if (device_supports_offline(dev) && !dev->offline_disabled) {
3656 /* Change online device attributes of @dev to @kuid/@kgid. */
3657 error = sysfs_file_change_owner(kobj, dev_attr_online.attr.name,
3667 * device_change_owner - change the owner of an existing device.
3669 * @kuid: new owner's kuid
3670 * @kgid: new owner's kgid
3672 * This changes the owner of @dev and its corresponding sysfs entries to
3673 * @kuid/@kgid. This function closely mirrors how @dev was added via driver
3676 * Returns 0 on success or error code on failure.
3678 int device_change_owner(struct device *dev, kuid_t kuid, kgid_t kgid)
3681 struct kobject *kobj = &dev->kobj;
3683 dev = get_device(dev);
3688 * Change the kobject and the default attributes and groups of the
3689 * ktype associated with it to @kuid/@kgid.
3691 error = sysfs_change_owner(kobj, kuid, kgid);
3696 * Change the uevent file for @dev to the new owner. The uevent file
3697 * was created in a separate step when @dev got added and we mirror
3700 error = sysfs_file_change_owner(kobj, dev_attr_uevent.attr.name, kuid,
3706 * Change the device groups, the device groups associated with the
3707 * device class, and the groups associated with the device type of @dev
3710 error = device_attrs_change_owner(dev, kuid, kgid);
3714 error = dpm_sysfs_change_owner(dev, kuid, kgid);
3719 if (sysfs_deprecated && dev->class == &block_class)
3724 * Change the owner of the symlink located in the class directory of
3725 * the device class associated with @dev which points to the actual
3726 * directory entry for @dev to @kuid/@kgid. This ensures that the
3727 * symlink shows the same permissions as its target.
3729 error = sysfs_link_change_owner(&dev->class->p->subsys.kobj, &dev->kobj,
3730 dev_name(dev), kuid, kgid);
3738 EXPORT_SYMBOL_GPL(device_change_owner);
3741 * device_shutdown - call ->shutdown() on each device to shutdown.
3743 void device_shutdown(void)
3745 struct device *dev, *parent;
3747 wait_for_device_probe();
3748 device_block_probing();
3752 spin_lock(&devices_kset->list_lock);
3754 * Walk the devices list backward, shutting down each in turn.
3755 * Beware that device unplug events may also start pulling
3756 * devices offline, even as the system is shutting down.
3758 while (!list_empty(&devices_kset->list)) {
3759 dev = list_entry(devices_kset->list.prev, struct device,
3763 * hold reference count of device's parent to
3764 * prevent it from being freed because parent's
3765 * lock is to be held
3767 parent = get_device(dev->parent);
3770 * Make sure the device is off the kset list, in the
3771 * event that dev->*->shutdown() doesn't remove it.
3773 list_del_init(&dev->kobj.entry);
3774 spin_unlock(&devices_kset->list_lock);
3776 /* hold lock to avoid race with probe/release */
3778 device_lock(parent);
3781 /* Don't allow any more runtime suspends */
3782 pm_runtime_get_noresume(dev);
3783 pm_runtime_barrier(dev);
3785 if (dev->class && dev->class->shutdown_pre) {
3787 dev_info(dev, "shutdown_pre\n");
3788 dev->class->shutdown_pre(dev);
3790 if (dev->bus && dev->bus->shutdown) {
3792 dev_info(dev, "shutdown\n");
3793 dev->bus->shutdown(dev);
3794 } else if (dev->driver && dev->driver->shutdown) {
3796 dev_info(dev, "shutdown\n");
3797 dev->driver->shutdown(dev);
3802 device_unlock(parent);
3807 spin_lock(&devices_kset->list_lock);
3809 spin_unlock(&devices_kset->list_lock);
3813 * Device logging functions
3816 #ifdef CONFIG_PRINTK
3818 create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
3824 subsys = dev->class->name;
3826 subsys = dev->bus->name;
3830 pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys);
3835 * Add device identifier DEVICE=:
3839 * +sound:card0 subsystem:devname
3841 if (MAJOR(dev->devt)) {
3844 if (strcmp(subsys, "block") == 0)
3849 pos += snprintf(hdr + pos, hdrlen - pos,
3851 c, MAJOR(dev->devt), MINOR(dev->devt));
3852 } else if (strcmp(subsys, "net") == 0) {
3853 struct net_device *net = to_net_dev(dev);
3856 pos += snprintf(hdr + pos, hdrlen - pos,
3857 "DEVICE=n%u", net->ifindex);
3860 pos += snprintf(hdr + pos, hdrlen - pos,
3861 "DEVICE=+%s:%s", subsys, dev_name(dev));
3870 dev_WARN(dev, "device/subsystem name too long");
3874 int dev_vprintk_emit(int level, const struct device *dev,
3875 const char *fmt, va_list args)
3880 hdrlen = create_syslog_header(dev, hdr, sizeof(hdr));
3882 return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args);
3884 EXPORT_SYMBOL(dev_vprintk_emit);
3886 int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
3891 va_start(args, fmt);
3893 r = dev_vprintk_emit(level, dev, fmt, args);
3899 EXPORT_SYMBOL(dev_printk_emit);
3901 static void __dev_printk(const char *level, const struct device *dev,
3902 struct va_format *vaf)
3905 dev_printk_emit(level[1] - '0', dev, "%s %s: %pV",
3906 dev_driver_string(dev), dev_name(dev), vaf);
3908 printk("%s(NULL device *): %pV", level, vaf);
3911 void dev_printk(const char *level, const struct device *dev,
3912 const char *fmt, ...)
3914 struct va_format vaf;
3917 va_start(args, fmt);
3922 __dev_printk(level, dev, &vaf);
3926 EXPORT_SYMBOL(dev_printk);
3928 #define define_dev_printk_level(func, kern_level) \
3929 void func(const struct device *dev, const char *fmt, ...) \
3931 struct va_format vaf; \
3934 va_start(args, fmt); \
3939 __dev_printk(kern_level, dev, &vaf); \
3943 EXPORT_SYMBOL(func);
3945 define_dev_printk_level(_dev_emerg, KERN_EMERG);
3946 define_dev_printk_level(_dev_alert, KERN_ALERT);
3947 define_dev_printk_level(_dev_crit, KERN_CRIT);
3948 define_dev_printk_level(_dev_err, KERN_ERR);
3949 define_dev_printk_level(_dev_warn, KERN_WARNING);
3950 define_dev_printk_level(_dev_notice, KERN_NOTICE);
3951 define_dev_printk_level(_dev_info, KERN_INFO);
3955 static inline bool fwnode_is_primary(struct fwnode_handle *fwnode)
3957 return fwnode && !IS_ERR(fwnode->secondary);
3961 * set_primary_fwnode - Change the primary firmware node of a given device.
3962 * @dev: Device to handle.
3963 * @fwnode: New primary firmware node of the device.
3965 * Set the device's firmware node pointer to @fwnode, but if a secondary
3966 * firmware node of the device is present, preserve it.
3968 void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
3971 struct fwnode_handle *fn = dev->fwnode;
3973 if (fwnode_is_primary(fn))
3977 WARN_ON(fwnode->secondary);
3978 fwnode->secondary = fn;
3980 dev->fwnode = fwnode;
3982 dev->fwnode = fwnode_is_primary(dev->fwnode) ?
3983 dev->fwnode->secondary : NULL;
3986 EXPORT_SYMBOL_GPL(set_primary_fwnode);
3989 * set_secondary_fwnode - Change the secondary firmware node of a given device.
3990 * @dev: Device to handle.
3991 * @fwnode: New secondary firmware node of the device.
3993 * If a primary firmware node of the device is present, set its secondary
3994 * pointer to @fwnode. Otherwise, set the device's firmware node pointer to
3997 void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
4000 fwnode->secondary = ERR_PTR(-ENODEV);
4002 if (fwnode_is_primary(dev->fwnode))
4003 dev->fwnode->secondary = fwnode;
4005 dev->fwnode = fwnode;
4007 EXPORT_SYMBOL_GPL(set_secondary_fwnode);
4010 * device_set_of_node_from_dev - reuse device-tree node of another device
4011 * @dev: device whose device-tree node is being set
4012 * @dev2: device whose device-tree node is being reused
4014 * Takes another reference to the new device-tree node after first dropping
4015 * any reference held to the old node.
4017 void device_set_of_node_from_dev(struct device *dev, const struct device *dev2)
4019 of_node_put(dev->of_node);
4020 dev->of_node = of_node_get(dev2->of_node);
4021 dev->of_node_reused = true;
4023 EXPORT_SYMBOL_GPL(device_set_of_node_from_dev);
4025 int device_match_name(struct device *dev, const void *name)
4027 return sysfs_streq(dev_name(dev), name);
4029 EXPORT_SYMBOL_GPL(device_match_name);
4031 int device_match_of_node(struct device *dev, const void *np)
4033 return dev->of_node == np;
4035 EXPORT_SYMBOL_GPL(device_match_of_node);
4037 int device_match_fwnode(struct device *dev, const void *fwnode)
4039 return dev_fwnode(dev) == fwnode;
4041 EXPORT_SYMBOL_GPL(device_match_fwnode);
4043 int device_match_devt(struct device *dev, const void *pdevt)
4045 return dev->devt == *(dev_t *)pdevt;
4047 EXPORT_SYMBOL_GPL(device_match_devt);
4049 int device_match_acpi_dev(struct device *dev, const void *adev)
4051 return ACPI_COMPANION(dev) == adev;
4053 EXPORT_SYMBOL(device_match_acpi_dev);
4055 int device_match_any(struct device *dev, const void *unused)
4059 EXPORT_SYMBOL_GPL(device_match_any);