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
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;
54 static DEFINE_MUTEX(device_links_lock);
55 DEFINE_STATIC_SRCU(device_links_srcu);
57 static inline void device_links_write_lock(void)
59 mutex_lock(&device_links_lock);
62 static inline void device_links_write_unlock(void)
64 mutex_unlock(&device_links_lock);
67 int device_links_read_lock(void) __acquires(&device_links_srcu)
69 return srcu_read_lock(&device_links_srcu);
72 void device_links_read_unlock(int idx) __releases(&device_links_srcu)
74 srcu_read_unlock(&device_links_srcu, idx);
77 int device_links_read_lock_held(void)
79 return srcu_read_lock_held(&device_links_srcu);
81 #else /* !CONFIG_SRCU */
82 static DECLARE_RWSEM(device_links_lock);
84 static inline void device_links_write_lock(void)
86 down_write(&device_links_lock);
89 static inline void device_links_write_unlock(void)
91 up_write(&device_links_lock);
94 int device_links_read_lock(void)
96 down_read(&device_links_lock);
100 void device_links_read_unlock(int not_used)
102 up_read(&device_links_lock);
105 #ifdef CONFIG_DEBUG_LOCK_ALLOC
106 int device_links_read_lock_held(void)
108 return lockdep_is_held(&device_links_lock);
111 #endif /* !CONFIG_SRCU */
114 * device_is_dependent - Check if one device depends on another one
115 * @dev: Device to check dependencies for.
116 * @target: Device to check against.
118 * Check if @target depends on @dev or any device dependent on it (its child or
119 * its consumer etc). Return 1 if that is the case or 0 otherwise.
121 static int device_is_dependent(struct device *dev, void *target)
123 struct device_link *link;
129 ret = device_for_each_child(dev, target, device_is_dependent);
133 list_for_each_entry(link, &dev->links.consumers, s_node) {
134 if (link->flags == (DL_FLAG_SYNC_STATE_ONLY | DL_FLAG_MANAGED))
137 if (link->consumer == target)
140 ret = device_is_dependent(link->consumer, target);
147 static void device_link_init_status(struct device_link *link,
148 struct device *consumer,
149 struct device *supplier)
151 switch (supplier->links.status) {
153 switch (consumer->links.status) {
156 * A consumer driver can create a link to a supplier
157 * that has not completed its probing yet as long as it
158 * knows that the supplier is already functional (for
159 * example, it has just acquired some resources from the
162 link->status = DL_STATE_CONSUMER_PROBE;
165 link->status = DL_STATE_DORMANT;
169 case DL_DEV_DRIVER_BOUND:
170 switch (consumer->links.status) {
172 link->status = DL_STATE_CONSUMER_PROBE;
174 case DL_DEV_DRIVER_BOUND:
175 link->status = DL_STATE_ACTIVE;
178 link->status = DL_STATE_AVAILABLE;
182 case DL_DEV_UNBINDING:
183 link->status = DL_STATE_SUPPLIER_UNBIND;
186 link->status = DL_STATE_DORMANT;
191 static int device_reorder_to_tail(struct device *dev, void *not_used)
193 struct device_link *link;
196 * Devices that have not been registered yet will be put to the ends
197 * of the lists during the registration, so skip them here.
199 if (device_is_registered(dev))
200 devices_kset_move_last(dev);
202 if (device_pm_initialized(dev))
203 device_pm_move_last(dev);
205 device_for_each_child(dev, NULL, device_reorder_to_tail);
206 list_for_each_entry(link, &dev->links.consumers, s_node) {
207 if (link->flags == (DL_FLAG_SYNC_STATE_ONLY | DL_FLAG_MANAGED))
209 device_reorder_to_tail(link->consumer, NULL);
216 * device_pm_move_to_tail - Move set of devices to the end of device lists
217 * @dev: Device to move
219 * This is a device_reorder_to_tail() wrapper taking the requisite locks.
221 * It moves the @dev along with all of its children and all of its consumers
222 * to the ends of the device_kset and dpm_list, recursively.
224 void device_pm_move_to_tail(struct device *dev)
228 idx = device_links_read_lock();
230 device_reorder_to_tail(dev, NULL);
232 device_links_read_unlock(idx);
235 #define DL_MANAGED_LINK_FLAGS (DL_FLAG_AUTOREMOVE_CONSUMER | \
236 DL_FLAG_AUTOREMOVE_SUPPLIER | \
237 DL_FLAG_AUTOPROBE_CONSUMER | \
238 DL_FLAG_SYNC_STATE_ONLY)
240 #define DL_ADD_VALID_FLAGS (DL_MANAGED_LINK_FLAGS | DL_FLAG_STATELESS | \
241 DL_FLAG_PM_RUNTIME | DL_FLAG_RPM_ACTIVE)
244 * device_link_add - Create a link between two devices.
245 * @consumer: Consumer end of the link.
246 * @supplier: Supplier end of the link.
247 * @flags: Link flags.
249 * The caller is responsible for the proper synchronization of the link creation
250 * with runtime PM. First, setting the DL_FLAG_PM_RUNTIME flag will cause the
251 * runtime PM framework to take the link into account. Second, if the
252 * DL_FLAG_RPM_ACTIVE flag is set in addition to it, the supplier devices will
253 * be forced into the active metastate and reference-counted upon the creation
254 * of the link. If DL_FLAG_PM_RUNTIME is not set, DL_FLAG_RPM_ACTIVE will be
257 * If DL_FLAG_STATELESS is set in @flags, the caller of this function is
258 * expected to release the link returned by it directly with the help of either
259 * device_link_del() or device_link_remove().
261 * If that flag is not set, however, the caller of this function is handing the
262 * management of the link over to the driver core entirely and its return value
263 * can only be used to check whether or not the link is present. In that case,
264 * the DL_FLAG_AUTOREMOVE_CONSUMER and DL_FLAG_AUTOREMOVE_SUPPLIER device link
265 * flags can be used to indicate to the driver core when the link can be safely
266 * deleted. Namely, setting one of them in @flags indicates to the driver core
267 * that the link is not going to be used (by the given caller of this function)
268 * after unbinding the consumer or supplier driver, respectively, from its
269 * device, so the link can be deleted at that point. If none of them is set,
270 * the link will be maintained until one of the devices pointed to by it (either
271 * the consumer or the supplier) is unregistered.
273 * Also, if DL_FLAG_STATELESS, DL_FLAG_AUTOREMOVE_CONSUMER and
274 * DL_FLAG_AUTOREMOVE_SUPPLIER are not set in @flags (that is, a persistent
275 * managed device link is being added), the DL_FLAG_AUTOPROBE_CONSUMER flag can
276 * be used to request the driver core to automaticall probe for a consmer
277 * driver after successfully binding a driver to the supplier device.
279 * The combination of DL_FLAG_STATELESS and one of DL_FLAG_AUTOREMOVE_CONSUMER,
280 * DL_FLAG_AUTOREMOVE_SUPPLIER, or DL_FLAG_AUTOPROBE_CONSUMER set in @flags at
281 * the same time is invalid and will cause NULL to be returned upfront.
282 * However, if a device link between the given @consumer and @supplier pair
283 * exists already when this function is called for them, the existing link will
284 * be returned regardless of its current type and status (the link's flags may
285 * be modified then). The caller of this function is then expected to treat
286 * the link as though it has just been created, so (in particular) if
287 * DL_FLAG_STATELESS was passed in @flags, the link needs to be released
288 * explicitly when not needed any more (as stated above).
290 * A side effect of the link creation is re-ordering of dpm_list and the
291 * devices_kset list by moving the consumer device and all devices depending
292 * on it to the ends of these lists (that does not happen to devices that have
293 * not been registered when this function is called).
295 * The supplier device is required to be registered when this function is called
296 * and NULL will be returned if that is not the case. The consumer device need
297 * not be registered, however.
299 struct device_link *device_link_add(struct device *consumer,
300 struct device *supplier, u32 flags)
302 struct device_link *link;
304 if (!consumer || !supplier || flags & ~DL_ADD_VALID_FLAGS ||
305 (flags & DL_FLAG_STATELESS && flags & DL_MANAGED_LINK_FLAGS) ||
306 (flags & DL_FLAG_SYNC_STATE_ONLY &&
307 flags != DL_FLAG_SYNC_STATE_ONLY) ||
308 (flags & DL_FLAG_AUTOPROBE_CONSUMER &&
309 flags & (DL_FLAG_AUTOREMOVE_CONSUMER |
310 DL_FLAG_AUTOREMOVE_SUPPLIER)))
313 if (flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) {
314 if (pm_runtime_get_sync(supplier) < 0) {
315 pm_runtime_put_noidle(supplier);
320 if (!(flags & DL_FLAG_STATELESS))
321 flags |= DL_FLAG_MANAGED;
323 device_links_write_lock();
327 * If the supplier has not been fully registered yet or there is a
328 * reverse (non-SYNC_STATE_ONLY) dependency between the consumer and
329 * the supplier already in the graph, return NULL. If the link is a
330 * SYNC_STATE_ONLY link, we don't check for reverse dependencies
331 * because it only affects sync_state() callbacks.
333 if (!device_pm_initialized(supplier)
334 || (!(flags & DL_FLAG_SYNC_STATE_ONLY) &&
335 device_is_dependent(consumer, supplier))) {
341 * DL_FLAG_AUTOREMOVE_SUPPLIER indicates that the link will be needed
342 * longer than for DL_FLAG_AUTOREMOVE_CONSUMER and setting them both
343 * together doesn't make sense, so prefer DL_FLAG_AUTOREMOVE_SUPPLIER.
345 if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
346 flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER;
348 list_for_each_entry(link, &supplier->links.consumers, s_node) {
349 if (link->consumer != consumer)
352 if (flags & DL_FLAG_PM_RUNTIME) {
353 if (!(link->flags & DL_FLAG_PM_RUNTIME)) {
354 pm_runtime_new_link(consumer);
355 link->flags |= DL_FLAG_PM_RUNTIME;
357 if (flags & DL_FLAG_RPM_ACTIVE)
358 refcount_inc(&link->rpm_active);
361 if (flags & DL_FLAG_STATELESS) {
362 kref_get(&link->kref);
363 if (link->flags & DL_FLAG_SYNC_STATE_ONLY &&
364 !(link->flags & DL_FLAG_STATELESS)) {
365 link->flags |= DL_FLAG_STATELESS;
373 * If the life time of the link following from the new flags is
374 * longer than indicated by the flags of the existing link,
375 * update the existing link to stay around longer.
377 if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER) {
378 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER) {
379 link->flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER;
380 link->flags |= DL_FLAG_AUTOREMOVE_SUPPLIER;
382 } else if (!(flags & DL_FLAG_AUTOREMOVE_CONSUMER)) {
383 link->flags &= ~(DL_FLAG_AUTOREMOVE_CONSUMER |
384 DL_FLAG_AUTOREMOVE_SUPPLIER);
386 if (!(link->flags & DL_FLAG_MANAGED)) {
387 kref_get(&link->kref);
388 link->flags |= DL_FLAG_MANAGED;
389 device_link_init_status(link, consumer, supplier);
391 if (link->flags & DL_FLAG_SYNC_STATE_ONLY &&
392 !(flags & DL_FLAG_SYNC_STATE_ONLY)) {
393 link->flags &= ~DL_FLAG_SYNC_STATE_ONLY;
400 link = kzalloc(sizeof(*link), GFP_KERNEL);
404 refcount_set(&link->rpm_active, 1);
406 if (flags & DL_FLAG_PM_RUNTIME) {
407 if (flags & DL_FLAG_RPM_ACTIVE)
408 refcount_inc(&link->rpm_active);
410 pm_runtime_new_link(consumer);
413 get_device(supplier);
414 link->supplier = supplier;
415 INIT_LIST_HEAD(&link->s_node);
416 get_device(consumer);
417 link->consumer = consumer;
418 INIT_LIST_HEAD(&link->c_node);
420 kref_init(&link->kref);
422 /* Determine the initial link state. */
423 if (flags & DL_FLAG_STATELESS)
424 link->status = DL_STATE_NONE;
426 device_link_init_status(link, consumer, supplier);
429 * Some callers expect the link creation during consumer driver probe to
430 * resume the supplier even without DL_FLAG_RPM_ACTIVE.
432 if (link->status == DL_STATE_CONSUMER_PROBE &&
433 flags & DL_FLAG_PM_RUNTIME)
434 pm_runtime_resume(supplier);
436 if (flags & DL_FLAG_SYNC_STATE_ONLY) {
438 "Linked as a sync state only consumer to %s\n",
444 * Move the consumer and all of the devices depending on it to the end
445 * of dpm_list and the devices_kset list.
447 * It is necessary to hold dpm_list locked throughout all that or else
448 * we may end up suspending with a wrong ordering of it.
450 device_reorder_to_tail(consumer, NULL);
452 list_add_tail_rcu(&link->s_node, &supplier->links.consumers);
453 list_add_tail_rcu(&link->c_node, &consumer->links.suppliers);
455 dev_dbg(consumer, "Linked as a consumer to %s\n", dev_name(supplier));
459 device_links_write_unlock();
461 if ((flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) && !link)
462 pm_runtime_put(supplier);
466 EXPORT_SYMBOL_GPL(device_link_add);
469 * device_link_wait_for_supplier - Add device to wait_for_suppliers list
470 * @consumer: Consumer device
472 * Marks the @consumer device as waiting for suppliers to become available by
473 * adding it to the wait_for_suppliers list. The consumer device will never be
474 * probed until it's removed from the wait_for_suppliers list.
476 * The caller is responsible for adding the links to the supplier devices once
477 * they are available and removing the @consumer device from the
478 * wait_for_suppliers list once links to all the suppliers have been created.
480 * This function is NOT meant to be called from the probe function of the
481 * consumer but rather from code that creates/adds the consumer device.
483 static void device_link_wait_for_supplier(struct device *consumer,
486 mutex_lock(&wfs_lock);
487 list_add_tail(&consumer->links.needs_suppliers, &wait_for_suppliers);
488 consumer->links.need_for_probe = need_for_probe;
489 mutex_unlock(&wfs_lock);
492 static void device_link_wait_for_mandatory_supplier(struct device *consumer)
494 device_link_wait_for_supplier(consumer, true);
497 static void device_link_wait_for_optional_supplier(struct device *consumer)
499 device_link_wait_for_supplier(consumer, false);
503 * device_link_add_missing_supplier_links - Add links from consumer devices to
504 * supplier devices, leaving any
505 * consumer with inactive suppliers on
506 * the wait_for_suppliers list
508 * Loops through all consumers waiting on suppliers and tries to add all their
509 * supplier links. If that succeeds, the consumer device is removed from
510 * wait_for_suppliers list. Otherwise, they are left in the wait_for_suppliers
511 * list. Devices left on the wait_for_suppliers list will not be probed.
513 * The fwnode add_links callback is expected to return 0 if it has found and
514 * added all the supplier links for the consumer device. It should return an
515 * error if it isn't able to do so.
517 * The caller of device_link_wait_for_supplier() is expected to call this once
518 * it's aware of potential suppliers becoming available.
520 static void device_link_add_missing_supplier_links(void)
522 struct device *dev, *tmp;
524 mutex_lock(&wfs_lock);
525 list_for_each_entry_safe(dev, tmp, &wait_for_suppliers,
526 links.needs_suppliers) {
527 int ret = fwnode_call_int_op(dev->fwnode, add_links, dev);
529 list_del_init(&dev->links.needs_suppliers);
530 else if (ret != -ENODEV)
531 dev->links.need_for_probe = false;
533 mutex_unlock(&wfs_lock);
536 static void device_link_free(struct device_link *link)
538 while (refcount_dec_not_one(&link->rpm_active))
539 pm_runtime_put(link->supplier);
541 put_device(link->consumer);
542 put_device(link->supplier);
547 static void __device_link_free_srcu(struct rcu_head *rhead)
549 device_link_free(container_of(rhead, struct device_link, rcu_head));
552 static void __device_link_del(struct kref *kref)
554 struct device_link *link = container_of(kref, struct device_link, kref);
556 dev_dbg(link->consumer, "Dropping the link to %s\n",
557 dev_name(link->supplier));
559 if (link->flags & DL_FLAG_PM_RUNTIME)
560 pm_runtime_drop_link(link->consumer);
562 list_del_rcu(&link->s_node);
563 list_del_rcu(&link->c_node);
564 call_srcu(&device_links_srcu, &link->rcu_head, __device_link_free_srcu);
566 #else /* !CONFIG_SRCU */
567 static void __device_link_del(struct kref *kref)
569 struct device_link *link = container_of(kref, struct device_link, kref);
571 dev_info(link->consumer, "Dropping the link to %s\n",
572 dev_name(link->supplier));
574 if (link->flags & DL_FLAG_PM_RUNTIME)
575 pm_runtime_drop_link(link->consumer);
577 list_del(&link->s_node);
578 list_del(&link->c_node);
579 device_link_free(link);
581 #endif /* !CONFIG_SRCU */
583 static void device_link_put_kref(struct device_link *link)
585 if (link->flags & DL_FLAG_STATELESS)
586 kref_put(&link->kref, __device_link_del);
588 WARN(1, "Unable to drop a managed device link reference\n");
592 * device_link_del - Delete a stateless link between two devices.
593 * @link: Device link to delete.
595 * The caller must ensure proper synchronization of this function with runtime
596 * PM. If the link was added multiple times, it needs to be deleted as often.
597 * Care is required for hotplugged devices: Their links are purged on removal
598 * and calling device_link_del() is then no longer allowed.
600 void device_link_del(struct device_link *link)
602 device_links_write_lock();
604 device_link_put_kref(link);
606 device_links_write_unlock();
608 EXPORT_SYMBOL_GPL(device_link_del);
611 * device_link_remove - Delete a stateless link between two devices.
612 * @consumer: Consumer end of the link.
613 * @supplier: Supplier end of the link.
615 * The caller must ensure proper synchronization of this function with runtime
618 void device_link_remove(void *consumer, struct device *supplier)
620 struct device_link *link;
622 if (WARN_ON(consumer == supplier))
625 device_links_write_lock();
628 list_for_each_entry(link, &supplier->links.consumers, s_node) {
629 if (link->consumer == consumer) {
630 device_link_put_kref(link);
636 device_links_write_unlock();
638 EXPORT_SYMBOL_GPL(device_link_remove);
640 static void device_links_missing_supplier(struct device *dev)
642 struct device_link *link;
644 list_for_each_entry(link, &dev->links.suppliers, c_node)
645 if (link->status == DL_STATE_CONSUMER_PROBE)
646 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
650 * device_links_check_suppliers - Check presence of supplier drivers.
651 * @dev: Consumer device.
653 * Check links from this device to any suppliers. Walk the list of the device's
654 * links to suppliers and see if all of them are available. If not, simply
655 * return -EPROBE_DEFER.
657 * We need to guarantee that the supplier will not go away after the check has
658 * been positive here. It only can go away in __device_release_driver() and
659 * that function checks the device's links to consumers. This means we need to
660 * mark the link as "consumer probe in progress" to make the supplier removal
661 * wait for us to complete (or bad things may happen).
663 * Links without the DL_FLAG_MANAGED flag set are ignored.
665 int device_links_check_suppliers(struct device *dev)
667 struct device_link *link;
671 * Device waiting for supplier to become available is not allowed to
674 mutex_lock(&wfs_lock);
675 if (!list_empty(&dev->links.needs_suppliers) &&
676 dev->links.need_for_probe) {
677 mutex_unlock(&wfs_lock);
678 return -EPROBE_DEFER;
680 mutex_unlock(&wfs_lock);
682 device_links_write_lock();
684 list_for_each_entry(link, &dev->links.suppliers, c_node) {
685 if (!(link->flags & DL_FLAG_MANAGED) ||
686 link->flags & DL_FLAG_SYNC_STATE_ONLY)
689 if (link->status != DL_STATE_AVAILABLE) {
690 device_links_missing_supplier(dev);
694 WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE);
696 dev->links.status = DL_DEV_PROBING;
698 device_links_write_unlock();
703 * __device_links_queue_sync_state - Queue a device for sync_state() callback
704 * @dev: Device to call sync_state() on
705 * @list: List head to queue the @dev on
707 * Queues a device for a sync_state() callback when the device links write lock
708 * isn't held. This allows the sync_state() execution flow to use device links
709 * APIs. The caller must ensure this function is called with
710 * device_links_write_lock() held.
712 * This function does a get_device() to make sure the device is not freed while
715 * So the caller must also ensure that device_links_flush_sync_list() is called
716 * as soon as the caller releases device_links_write_lock(). This is necessary
717 * to make sure the sync_state() is called in a timely fashion and the
718 * put_device() is called on this device.
720 static void __device_links_queue_sync_state(struct device *dev,
721 struct list_head *list)
723 struct device_link *link;
725 if (!dev_has_sync_state(dev))
727 if (dev->state_synced)
730 list_for_each_entry(link, &dev->links.consumers, s_node) {
731 if (!(link->flags & DL_FLAG_MANAGED))
733 if (link->status != DL_STATE_ACTIVE)
738 * Set the flag here to avoid adding the same device to a list more
739 * than once. This can happen if new consumers get added to the device
740 * and probed before the list is flushed.
742 dev->state_synced = true;
744 if (WARN_ON(!list_empty(&dev->links.defer_sync)))
748 list_add_tail(&dev->links.defer_sync, list);
752 * device_links_flush_sync_list - Call sync_state() on a list of devices
753 * @list: List of devices to call sync_state() on
754 * @dont_lock_dev: Device for which lock is already held by the caller
756 * Calls sync_state() on all the devices that have been queued for it. This
757 * function is used in conjunction with __device_links_queue_sync_state(). The
758 * @dont_lock_dev parameter is useful when this function is called from a
759 * context where a device lock is already held.
761 static void device_links_flush_sync_list(struct list_head *list,
762 struct device *dont_lock_dev)
764 struct device *dev, *tmp;
766 list_for_each_entry_safe(dev, tmp, list, links.defer_sync) {
767 list_del_init(&dev->links.defer_sync);
769 if (dev != dont_lock_dev)
772 if (dev->bus->sync_state)
773 dev->bus->sync_state(dev);
774 else if (dev->driver && dev->driver->sync_state)
775 dev->driver->sync_state(dev);
777 if (dev != dont_lock_dev)
784 void device_links_supplier_sync_state_pause(void)
786 device_links_write_lock();
787 defer_sync_state_count++;
788 device_links_write_unlock();
791 void device_links_supplier_sync_state_resume(void)
793 struct device *dev, *tmp;
794 LIST_HEAD(sync_list);
796 device_links_write_lock();
797 if (!defer_sync_state_count) {
798 WARN(true, "Unmatched sync_state pause/resume!");
801 defer_sync_state_count--;
802 if (defer_sync_state_count)
805 list_for_each_entry_safe(dev, tmp, &deferred_sync, links.defer_sync) {
807 * Delete from deferred_sync list before queuing it to
808 * sync_list because defer_sync is used for both lists.
810 list_del_init(&dev->links.defer_sync);
811 __device_links_queue_sync_state(dev, &sync_list);
814 device_links_write_unlock();
816 device_links_flush_sync_list(&sync_list, NULL);
819 static int sync_state_resume_initcall(void)
821 device_links_supplier_sync_state_resume();
824 late_initcall(sync_state_resume_initcall);
826 static void __device_links_supplier_defer_sync(struct device *sup)
828 if (list_empty(&sup->links.defer_sync) && dev_has_sync_state(sup))
829 list_add_tail(&sup->links.defer_sync, &deferred_sync);
833 * device_links_driver_bound - Update device links after probing its driver.
834 * @dev: Device to update the links for.
836 * The probe has been successful, so update links from this device to any
837 * consumers by changing their status to "available".
839 * Also change the status of @dev's links to suppliers to "active".
841 * Links without the DL_FLAG_MANAGED flag set are ignored.
843 void device_links_driver_bound(struct device *dev)
845 struct device_link *link;
846 LIST_HEAD(sync_list);
849 * If a device probes successfully, it's expected to have created all
850 * the device links it needs to or make new device links as it needs
851 * them. So, it no longer needs to wait on any suppliers.
853 mutex_lock(&wfs_lock);
854 list_del_init(&dev->links.needs_suppliers);
855 mutex_unlock(&wfs_lock);
857 device_links_write_lock();
859 list_for_each_entry(link, &dev->links.consumers, s_node) {
860 if (!(link->flags & DL_FLAG_MANAGED))
864 * Links created during consumer probe may be in the "consumer
865 * probe" state to start with if the supplier is still probing
866 * when they are created and they may become "active" if the
867 * consumer probe returns first. Skip them here.
869 if (link->status == DL_STATE_CONSUMER_PROBE ||
870 link->status == DL_STATE_ACTIVE)
873 WARN_ON(link->status != DL_STATE_DORMANT);
874 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
876 if (link->flags & DL_FLAG_AUTOPROBE_CONSUMER)
877 driver_deferred_probe_add(link->consumer);
880 if (defer_sync_state_count)
881 __device_links_supplier_defer_sync(dev);
883 __device_links_queue_sync_state(dev, &sync_list);
885 list_for_each_entry(link, &dev->links.suppliers, c_node) {
886 if (!(link->flags & DL_FLAG_MANAGED))
889 WARN_ON(link->status != DL_STATE_CONSUMER_PROBE);
890 WRITE_ONCE(link->status, DL_STATE_ACTIVE);
892 if (defer_sync_state_count)
893 __device_links_supplier_defer_sync(link->supplier);
895 __device_links_queue_sync_state(link->supplier,
899 dev->links.status = DL_DEV_DRIVER_BOUND;
901 device_links_write_unlock();
903 device_links_flush_sync_list(&sync_list, dev);
906 static void device_link_drop_managed(struct device_link *link)
908 link->flags &= ~DL_FLAG_MANAGED;
909 WRITE_ONCE(link->status, DL_STATE_NONE);
910 kref_put(&link->kref, __device_link_del);
914 * __device_links_no_driver - Update links of a device without a driver.
915 * @dev: Device without a drvier.
917 * Delete all non-persistent links from this device to any suppliers.
919 * Persistent links stay around, but their status is changed to "available",
920 * unless they already are in the "supplier unbind in progress" state in which
921 * case they need not be updated.
923 * Links without the DL_FLAG_MANAGED flag set are ignored.
925 static void __device_links_no_driver(struct device *dev)
927 struct device_link *link, *ln;
929 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
930 if (!(link->flags & DL_FLAG_MANAGED))
933 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER)
934 device_link_drop_managed(link);
935 else if (link->status == DL_STATE_CONSUMER_PROBE ||
936 link->status == DL_STATE_ACTIVE)
937 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
940 dev->links.status = DL_DEV_NO_DRIVER;
944 * device_links_no_driver - Update links after failing driver probe.
945 * @dev: Device whose driver has just failed to probe.
947 * Clean up leftover links to consumers for @dev and invoke
948 * %__device_links_no_driver() to update links to suppliers for it as
951 * Links without the DL_FLAG_MANAGED flag set are ignored.
953 void device_links_no_driver(struct device *dev)
955 struct device_link *link;
957 device_links_write_lock();
959 list_for_each_entry(link, &dev->links.consumers, s_node) {
960 if (!(link->flags & DL_FLAG_MANAGED))
964 * The probe has failed, so if the status of the link is
965 * "consumer probe" or "active", it must have been added by
966 * a probing consumer while this device was still probing.
967 * Change its state to "dormant", as it represents a valid
968 * relationship, but it is not functionally meaningful.
970 if (link->status == DL_STATE_CONSUMER_PROBE ||
971 link->status == DL_STATE_ACTIVE)
972 WRITE_ONCE(link->status, DL_STATE_DORMANT);
975 __device_links_no_driver(dev);
977 device_links_write_unlock();
981 * device_links_driver_cleanup - Update links after driver removal.
982 * @dev: Device whose driver has just gone away.
984 * Update links to consumers for @dev by changing their status to "dormant" and
985 * invoke %__device_links_no_driver() to update links to suppliers for it as
988 * Links without the DL_FLAG_MANAGED flag set are ignored.
990 void device_links_driver_cleanup(struct device *dev)
992 struct device_link *link, *ln;
994 device_links_write_lock();
996 list_for_each_entry_safe(link, ln, &dev->links.consumers, s_node) {
997 if (!(link->flags & DL_FLAG_MANAGED))
1000 WARN_ON(link->flags & DL_FLAG_AUTOREMOVE_CONSUMER);
1001 WARN_ON(link->status != DL_STATE_SUPPLIER_UNBIND);
1004 * autoremove the links between this @dev and its consumer
1005 * devices that are not active, i.e. where the link state
1006 * has moved to DL_STATE_SUPPLIER_UNBIND.
1008 if (link->status == DL_STATE_SUPPLIER_UNBIND &&
1009 link->flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
1010 device_link_drop_managed(link);
1012 WRITE_ONCE(link->status, DL_STATE_DORMANT);
1015 list_del_init(&dev->links.defer_sync);
1016 __device_links_no_driver(dev);
1018 device_links_write_unlock();
1022 * device_links_busy - Check if there are any busy links to consumers.
1023 * @dev: Device to check.
1025 * Check each consumer of the device and return 'true' if its link's status
1026 * is one of "consumer probe" or "active" (meaning that the given consumer is
1027 * probing right now or its driver is present). Otherwise, change the link
1028 * state to "supplier unbind" to prevent the consumer from being probed
1029 * successfully going forward.
1031 * Return 'false' if there are no probing or active consumers.
1033 * Links without the DL_FLAG_MANAGED flag set are ignored.
1035 bool device_links_busy(struct device *dev)
1037 struct device_link *link;
1040 device_links_write_lock();
1042 list_for_each_entry(link, &dev->links.consumers, s_node) {
1043 if (!(link->flags & DL_FLAG_MANAGED))
1046 if (link->status == DL_STATE_CONSUMER_PROBE
1047 || link->status == DL_STATE_ACTIVE) {
1051 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
1054 dev->links.status = DL_DEV_UNBINDING;
1056 device_links_write_unlock();
1061 * device_links_unbind_consumers - Force unbind consumers of the given device.
1062 * @dev: Device to unbind the consumers of.
1064 * Walk the list of links to consumers for @dev and if any of them is in the
1065 * "consumer probe" state, wait for all device probes in progress to complete
1068 * If that's not the case, change the status of the link to "supplier unbind"
1069 * and check if the link was in the "active" state. If so, force the consumer
1070 * driver to unbind and start over (the consumer will not re-probe as we have
1071 * changed the state of the link already).
1073 * Links without the DL_FLAG_MANAGED flag set are ignored.
1075 void device_links_unbind_consumers(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 enum device_link_state status;
1085 if (!(link->flags & DL_FLAG_MANAGED) ||
1086 link->flags & DL_FLAG_SYNC_STATE_ONLY)
1089 status = link->status;
1090 if (status == DL_STATE_CONSUMER_PROBE) {
1091 device_links_write_unlock();
1093 wait_for_device_probe();
1096 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
1097 if (status == DL_STATE_ACTIVE) {
1098 struct device *consumer = link->consumer;
1100 get_device(consumer);
1102 device_links_write_unlock();
1104 device_release_driver_internal(consumer, NULL,
1106 put_device(consumer);
1111 device_links_write_unlock();
1115 * device_links_purge - Delete existing links to other devices.
1116 * @dev: Target device.
1118 static void device_links_purge(struct device *dev)
1120 struct device_link *link, *ln;
1122 mutex_lock(&wfs_lock);
1123 list_del(&dev->links.needs_suppliers);
1124 mutex_unlock(&wfs_lock);
1127 * Delete all of the remaining links from this device to any other
1128 * devices (either consumers or suppliers).
1130 device_links_write_lock();
1132 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
1133 WARN_ON(link->status == DL_STATE_ACTIVE);
1134 __device_link_del(&link->kref);
1137 list_for_each_entry_safe_reverse(link, ln, &dev->links.consumers, s_node) {
1138 WARN_ON(link->status != DL_STATE_DORMANT &&
1139 link->status != DL_STATE_NONE);
1140 __device_link_del(&link->kref);
1143 device_links_write_unlock();
1146 /* Device links support end. */
1148 int (*platform_notify)(struct device *dev) = NULL;
1149 int (*platform_notify_remove)(struct device *dev) = NULL;
1150 static struct kobject *dev_kobj;
1151 struct kobject *sysfs_dev_char_kobj;
1152 struct kobject *sysfs_dev_block_kobj;
1154 static DEFINE_MUTEX(device_hotplug_lock);
1156 void lock_device_hotplug(void)
1158 mutex_lock(&device_hotplug_lock);
1161 void unlock_device_hotplug(void)
1163 mutex_unlock(&device_hotplug_lock);
1166 int lock_device_hotplug_sysfs(void)
1168 if (mutex_trylock(&device_hotplug_lock))
1171 /* Avoid busy looping (5 ms of sleep should do). */
1173 return restart_syscall();
1177 static inline int device_is_not_partition(struct device *dev)
1179 return !(dev->type == &part_type);
1182 static inline int device_is_not_partition(struct device *dev)
1189 device_platform_notify(struct device *dev, enum kobject_action action)
1193 ret = acpi_platform_notify(dev, action);
1197 ret = software_node_notify(dev, action);
1201 if (platform_notify && action == KOBJ_ADD)
1202 platform_notify(dev);
1203 else if (platform_notify_remove && action == KOBJ_REMOVE)
1204 platform_notify_remove(dev);
1209 * dev_driver_string - Return a device's driver name, if at all possible
1210 * @dev: struct device to get the name of
1212 * Will return the device's driver's name if it is bound to a device. If
1213 * the device is not bound to a driver, it will return the name of the bus
1214 * it is attached to. If it is not attached to a bus either, an empty
1215 * string will be returned.
1217 const char *dev_driver_string(const struct device *dev)
1219 struct device_driver *drv;
1221 /* dev->driver can change to NULL underneath us because of unbinding,
1222 * so be careful about accessing it. dev->bus and dev->class should
1223 * never change once they are set, so they don't need special care.
1225 drv = READ_ONCE(dev->driver);
1226 return drv ? drv->name :
1227 (dev->bus ? dev->bus->name :
1228 (dev->class ? dev->class->name : ""));
1230 EXPORT_SYMBOL(dev_driver_string);
1232 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
1234 static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
1237 struct device_attribute *dev_attr = to_dev_attr(attr);
1238 struct device *dev = kobj_to_dev(kobj);
1242 ret = dev_attr->show(dev, dev_attr, buf);
1243 if (ret >= (ssize_t)PAGE_SIZE) {
1244 printk("dev_attr_show: %pS returned bad count\n",
1250 static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
1251 const char *buf, size_t count)
1253 struct device_attribute *dev_attr = to_dev_attr(attr);
1254 struct device *dev = kobj_to_dev(kobj);
1257 if (dev_attr->store)
1258 ret = dev_attr->store(dev, dev_attr, buf, count);
1262 static const struct sysfs_ops dev_sysfs_ops = {
1263 .show = dev_attr_show,
1264 .store = dev_attr_store,
1267 #define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
1269 ssize_t device_store_ulong(struct device *dev,
1270 struct device_attribute *attr,
1271 const char *buf, size_t size)
1273 struct dev_ext_attribute *ea = to_ext_attr(attr);
1277 ret = kstrtoul(buf, 0, &new);
1280 *(unsigned long *)(ea->var) = new;
1281 /* Always return full write size even if we didn't consume all */
1284 EXPORT_SYMBOL_GPL(device_store_ulong);
1286 ssize_t device_show_ulong(struct device *dev,
1287 struct device_attribute *attr,
1290 struct dev_ext_attribute *ea = to_ext_attr(attr);
1291 return snprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var));
1293 EXPORT_SYMBOL_GPL(device_show_ulong);
1295 ssize_t device_store_int(struct device *dev,
1296 struct device_attribute *attr,
1297 const char *buf, size_t size)
1299 struct dev_ext_attribute *ea = to_ext_attr(attr);
1303 ret = kstrtol(buf, 0, &new);
1307 if (new > INT_MAX || new < INT_MIN)
1309 *(int *)(ea->var) = new;
1310 /* Always return full write size even if we didn't consume all */
1313 EXPORT_SYMBOL_GPL(device_store_int);
1315 ssize_t device_show_int(struct device *dev,
1316 struct device_attribute *attr,
1319 struct dev_ext_attribute *ea = to_ext_attr(attr);
1321 return snprintf(buf, PAGE_SIZE, "%d\n", *(int *)(ea->var));
1323 EXPORT_SYMBOL_GPL(device_show_int);
1325 ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
1326 const char *buf, size_t size)
1328 struct dev_ext_attribute *ea = to_ext_attr(attr);
1330 if (strtobool(buf, ea->var) < 0)
1335 EXPORT_SYMBOL_GPL(device_store_bool);
1337 ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
1340 struct dev_ext_attribute *ea = to_ext_attr(attr);
1342 return snprintf(buf, PAGE_SIZE, "%d\n", *(bool *)(ea->var));
1344 EXPORT_SYMBOL_GPL(device_show_bool);
1347 * device_release - free device structure.
1348 * @kobj: device's kobject.
1350 * This is called once the reference count for the object
1351 * reaches 0. We forward the call to the device's release
1352 * method, which should handle actually freeing the structure.
1354 static void device_release(struct kobject *kobj)
1356 struct device *dev = kobj_to_dev(kobj);
1357 struct device_private *p = dev->p;
1360 * Some platform devices are driven without driver attached
1361 * and managed resources may have been acquired. Make sure
1362 * all resources are released.
1364 * Drivers still can add resources into device after device
1365 * is deleted but alive, so release devres here to avoid
1366 * possible memory leak.
1368 devres_release_all(dev);
1372 else if (dev->type && dev->type->release)
1373 dev->type->release(dev);
1374 else if (dev->class && dev->class->dev_release)
1375 dev->class->dev_release(dev);
1377 WARN(1, KERN_ERR "Device '%s' does not have a release() function, it is broken and must be fixed. See Documentation/kobject.txt.\n",
1382 static const void *device_namespace(struct kobject *kobj)
1384 struct device *dev = kobj_to_dev(kobj);
1385 const void *ns = NULL;
1387 if (dev->class && dev->class->ns_type)
1388 ns = dev->class->namespace(dev);
1393 static void device_get_ownership(struct kobject *kobj, kuid_t *uid, kgid_t *gid)
1395 struct device *dev = kobj_to_dev(kobj);
1397 if (dev->class && dev->class->get_ownership)
1398 dev->class->get_ownership(dev, uid, gid);
1401 static struct kobj_type device_ktype = {
1402 .release = device_release,
1403 .sysfs_ops = &dev_sysfs_ops,
1404 .namespace = device_namespace,
1405 .get_ownership = device_get_ownership,
1409 static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
1411 struct kobj_type *ktype = get_ktype(kobj);
1413 if (ktype == &device_ktype) {
1414 struct device *dev = kobj_to_dev(kobj);
1423 static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
1425 struct device *dev = kobj_to_dev(kobj);
1428 return dev->bus->name;
1430 return dev->class->name;
1434 static int dev_uevent(struct kset *kset, struct kobject *kobj,
1435 struct kobj_uevent_env *env)
1437 struct device *dev = kobj_to_dev(kobj);
1440 /* add device node properties if present */
1441 if (MAJOR(dev->devt)) {
1445 kuid_t uid = GLOBAL_ROOT_UID;
1446 kgid_t gid = GLOBAL_ROOT_GID;
1448 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
1449 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
1450 name = device_get_devnode(dev, &mode, &uid, &gid, &tmp);
1452 add_uevent_var(env, "DEVNAME=%s", name);
1454 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
1455 if (!uid_eq(uid, GLOBAL_ROOT_UID))
1456 add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid));
1457 if (!gid_eq(gid, GLOBAL_ROOT_GID))
1458 add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid));
1463 if (dev->type && dev->type->name)
1464 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
1467 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
1469 /* Add common DT information about the device */
1470 of_device_uevent(dev, env);
1472 /* have the bus specific function add its stuff */
1473 if (dev->bus && dev->bus->uevent) {
1474 retval = dev->bus->uevent(dev, env);
1476 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
1477 dev_name(dev), __func__, retval);
1480 /* have the class specific function add its stuff */
1481 if (dev->class && dev->class->dev_uevent) {
1482 retval = dev->class->dev_uevent(dev, env);
1484 pr_debug("device: '%s': %s: class uevent() "
1485 "returned %d\n", dev_name(dev),
1489 /* have the device type specific function add its stuff */
1490 if (dev->type && dev->type->uevent) {
1491 retval = dev->type->uevent(dev, env);
1493 pr_debug("device: '%s': %s: dev_type uevent() "
1494 "returned %d\n", dev_name(dev),
1501 static const struct kset_uevent_ops device_uevent_ops = {
1502 .filter = dev_uevent_filter,
1503 .name = dev_uevent_name,
1504 .uevent = dev_uevent,
1507 static ssize_t uevent_show(struct device *dev, struct device_attribute *attr,
1510 struct kobject *top_kobj;
1512 struct kobj_uevent_env *env = NULL;
1517 /* search the kset, the device belongs to */
1518 top_kobj = &dev->kobj;
1519 while (!top_kobj->kset && top_kobj->parent)
1520 top_kobj = top_kobj->parent;
1521 if (!top_kobj->kset)
1524 kset = top_kobj->kset;
1525 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
1528 /* respect filter */
1529 if (kset->uevent_ops && kset->uevent_ops->filter)
1530 if (!kset->uevent_ops->filter(kset, &dev->kobj))
1533 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
1537 /* let the kset specific function add its keys */
1538 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
1542 /* copy keys to file */
1543 for (i = 0; i < env->envp_idx; i++)
1544 count += sprintf(&buf[count], "%s\n", env->envp[i]);
1550 static ssize_t uevent_store(struct device *dev, struct device_attribute *attr,
1551 const char *buf, size_t count)
1555 rc = kobject_synth_uevent(&dev->kobj, buf, count);
1558 dev_err(dev, "uevent: failed to send synthetic uevent\n");
1564 static DEVICE_ATTR_RW(uevent);
1566 static ssize_t online_show(struct device *dev, struct device_attribute *attr,
1572 val = !dev->offline;
1574 return sprintf(buf, "%u\n", val);
1577 static ssize_t online_store(struct device *dev, struct device_attribute *attr,
1578 const char *buf, size_t count)
1583 ret = strtobool(buf, &val);
1587 ret = lock_device_hotplug_sysfs();
1591 ret = val ? device_online(dev) : device_offline(dev);
1592 unlock_device_hotplug();
1593 return ret < 0 ? ret : count;
1595 static DEVICE_ATTR_RW(online);
1597 int device_add_groups(struct device *dev, const struct attribute_group **groups)
1599 return sysfs_create_groups(&dev->kobj, groups);
1601 EXPORT_SYMBOL_GPL(device_add_groups);
1603 void device_remove_groups(struct device *dev,
1604 const struct attribute_group **groups)
1606 sysfs_remove_groups(&dev->kobj, groups);
1608 EXPORT_SYMBOL_GPL(device_remove_groups);
1610 union device_attr_group_devres {
1611 const struct attribute_group *group;
1612 const struct attribute_group **groups;
1615 static int devm_attr_group_match(struct device *dev, void *res, void *data)
1617 return ((union device_attr_group_devres *)res)->group == data;
1620 static void devm_attr_group_remove(struct device *dev, void *res)
1622 union device_attr_group_devres *devres = res;
1623 const struct attribute_group *group = devres->group;
1625 dev_dbg(dev, "%s: removing group %p\n", __func__, group);
1626 sysfs_remove_group(&dev->kobj, group);
1629 static void devm_attr_groups_remove(struct device *dev, void *res)
1631 union device_attr_group_devres *devres = res;
1632 const struct attribute_group **groups = devres->groups;
1634 dev_dbg(dev, "%s: removing groups %p\n", __func__, groups);
1635 sysfs_remove_groups(&dev->kobj, groups);
1639 * devm_device_add_group - given a device, create a managed attribute group
1640 * @dev: The device to create the group for
1641 * @grp: The attribute group to create
1643 * This function creates a group for the first time. It will explicitly
1644 * warn and error if any of the attribute files being created already exist.
1646 * Returns 0 on success or error code on failure.
1648 int devm_device_add_group(struct device *dev, const struct attribute_group *grp)
1650 union device_attr_group_devres *devres;
1653 devres = devres_alloc(devm_attr_group_remove,
1654 sizeof(*devres), GFP_KERNEL);
1658 error = sysfs_create_group(&dev->kobj, grp);
1660 devres_free(devres);
1664 devres->group = grp;
1665 devres_add(dev, devres);
1668 EXPORT_SYMBOL_GPL(devm_device_add_group);
1671 * devm_device_remove_group: remove a managed group from a device
1672 * @dev: device to remove the group from
1673 * @grp: group to remove
1675 * This function removes a group of attributes from a device. The attributes
1676 * previously have to have been created for this group, otherwise it will fail.
1678 void devm_device_remove_group(struct device *dev,
1679 const struct attribute_group *grp)
1681 WARN_ON(devres_release(dev, devm_attr_group_remove,
1682 devm_attr_group_match,
1683 /* cast away const */ (void *)grp));
1685 EXPORT_SYMBOL_GPL(devm_device_remove_group);
1688 * devm_device_add_groups - create a bunch of managed attribute groups
1689 * @dev: The device to create the group for
1690 * @groups: The attribute groups to create, NULL terminated
1692 * This function creates a bunch of managed attribute groups. If an error
1693 * occurs when creating a group, all previously created groups will be
1694 * removed, unwinding everything back to the original state when this
1695 * function was called. It will explicitly warn and error if any of the
1696 * attribute files being created already exist.
1698 * Returns 0 on success or error code from sysfs_create_group on failure.
1700 int devm_device_add_groups(struct device *dev,
1701 const struct attribute_group **groups)
1703 union device_attr_group_devres *devres;
1706 devres = devres_alloc(devm_attr_groups_remove,
1707 sizeof(*devres), GFP_KERNEL);
1711 error = sysfs_create_groups(&dev->kobj, groups);
1713 devres_free(devres);
1717 devres->groups = groups;
1718 devres_add(dev, devres);
1721 EXPORT_SYMBOL_GPL(devm_device_add_groups);
1724 * devm_device_remove_groups - remove a list of managed groups
1726 * @dev: The device for the groups to be removed from
1727 * @groups: NULL terminated list of groups to be removed
1729 * If groups is not NULL, remove the specified groups from the device.
1731 void devm_device_remove_groups(struct device *dev,
1732 const struct attribute_group **groups)
1734 WARN_ON(devres_release(dev, devm_attr_groups_remove,
1735 devm_attr_group_match,
1736 /* cast away const */ (void *)groups));
1738 EXPORT_SYMBOL_GPL(devm_device_remove_groups);
1740 static int device_add_attrs(struct device *dev)
1742 struct class *class = dev->class;
1743 const struct device_type *type = dev->type;
1747 error = device_add_groups(dev, class->dev_groups);
1753 error = device_add_groups(dev, type->groups);
1755 goto err_remove_class_groups;
1758 error = device_add_groups(dev, dev->groups);
1760 goto err_remove_type_groups;
1762 if (device_supports_offline(dev) && !dev->offline_disabled) {
1763 error = device_create_file(dev, &dev_attr_online);
1765 goto err_remove_dev_groups;
1770 err_remove_dev_groups:
1771 device_remove_groups(dev, dev->groups);
1772 err_remove_type_groups:
1774 device_remove_groups(dev, type->groups);
1775 err_remove_class_groups:
1777 device_remove_groups(dev, class->dev_groups);
1782 static void device_remove_attrs(struct device *dev)
1784 struct class *class = dev->class;
1785 const struct device_type *type = dev->type;
1787 device_remove_file(dev, &dev_attr_online);
1788 device_remove_groups(dev, dev->groups);
1791 device_remove_groups(dev, type->groups);
1794 device_remove_groups(dev, class->dev_groups);
1797 static ssize_t dev_show(struct device *dev, struct device_attribute *attr,
1800 return print_dev_t(buf, dev->devt);
1802 static DEVICE_ATTR_RO(dev);
1805 struct kset *devices_kset;
1808 * devices_kset_move_before - Move device in the devices_kset's list.
1809 * @deva: Device to move.
1810 * @devb: Device @deva should come before.
1812 static void devices_kset_move_before(struct device *deva, struct device *devb)
1816 pr_debug("devices_kset: Moving %s before %s\n",
1817 dev_name(deva), dev_name(devb));
1818 spin_lock(&devices_kset->list_lock);
1819 list_move_tail(&deva->kobj.entry, &devb->kobj.entry);
1820 spin_unlock(&devices_kset->list_lock);
1824 * devices_kset_move_after - Move device in the devices_kset's list.
1825 * @deva: Device to move
1826 * @devb: Device @deva should come after.
1828 static void devices_kset_move_after(struct device *deva, struct device *devb)
1832 pr_debug("devices_kset: Moving %s after %s\n",
1833 dev_name(deva), dev_name(devb));
1834 spin_lock(&devices_kset->list_lock);
1835 list_move(&deva->kobj.entry, &devb->kobj.entry);
1836 spin_unlock(&devices_kset->list_lock);
1840 * devices_kset_move_last - move the device to the end of devices_kset's list.
1841 * @dev: device to move
1843 void devices_kset_move_last(struct device *dev)
1847 pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev));
1848 spin_lock(&devices_kset->list_lock);
1849 list_move_tail(&dev->kobj.entry, &devices_kset->list);
1850 spin_unlock(&devices_kset->list_lock);
1854 * device_create_file - create sysfs attribute file for device.
1856 * @attr: device attribute descriptor.
1858 int device_create_file(struct device *dev,
1859 const struct device_attribute *attr)
1864 WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
1865 "Attribute %s: write permission without 'store'\n",
1867 WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
1868 "Attribute %s: read permission without 'show'\n",
1870 error = sysfs_create_file(&dev->kobj, &attr->attr);
1875 EXPORT_SYMBOL_GPL(device_create_file);
1878 * device_remove_file - remove sysfs attribute file.
1880 * @attr: device attribute descriptor.
1882 void device_remove_file(struct device *dev,
1883 const struct device_attribute *attr)
1886 sysfs_remove_file(&dev->kobj, &attr->attr);
1888 EXPORT_SYMBOL_GPL(device_remove_file);
1891 * device_remove_file_self - remove sysfs attribute file from its own method.
1893 * @attr: device attribute descriptor.
1895 * See kernfs_remove_self() for details.
1897 bool device_remove_file_self(struct device *dev,
1898 const struct device_attribute *attr)
1901 return sysfs_remove_file_self(&dev->kobj, &attr->attr);
1905 EXPORT_SYMBOL_GPL(device_remove_file_self);
1908 * device_create_bin_file - create sysfs binary attribute file for device.
1910 * @attr: device binary attribute descriptor.
1912 int device_create_bin_file(struct device *dev,
1913 const struct bin_attribute *attr)
1915 int error = -EINVAL;
1917 error = sysfs_create_bin_file(&dev->kobj, attr);
1920 EXPORT_SYMBOL_GPL(device_create_bin_file);
1923 * device_remove_bin_file - remove sysfs binary attribute file
1925 * @attr: device binary attribute descriptor.
1927 void device_remove_bin_file(struct device *dev,
1928 const struct bin_attribute *attr)
1931 sysfs_remove_bin_file(&dev->kobj, attr);
1933 EXPORT_SYMBOL_GPL(device_remove_bin_file);
1935 static void klist_children_get(struct klist_node *n)
1937 struct device_private *p = to_device_private_parent(n);
1938 struct device *dev = p->device;
1943 static void klist_children_put(struct klist_node *n)
1945 struct device_private *p = to_device_private_parent(n);
1946 struct device *dev = p->device;
1952 * device_initialize - init device structure.
1955 * This prepares the device for use by other layers by initializing
1957 * It is the first half of device_register(), if called by
1958 * that function, though it can also be called separately, so one
1959 * may use @dev's fields. In particular, get_device()/put_device()
1960 * may be used for reference counting of @dev after calling this
1963 * All fields in @dev must be initialized by the caller to 0, except
1964 * for those explicitly set to some other value. The simplest
1965 * approach is to use kzalloc() to allocate the structure containing
1968 * NOTE: Use put_device() to give up your reference instead of freeing
1969 * @dev directly once you have called this function.
1971 void device_initialize(struct device *dev)
1973 dev->kobj.kset = devices_kset;
1974 kobject_init(&dev->kobj, &device_ktype);
1975 INIT_LIST_HEAD(&dev->dma_pools);
1976 mutex_init(&dev->mutex);
1977 #ifdef CONFIG_PROVE_LOCKING
1978 mutex_init(&dev->lockdep_mutex);
1980 lockdep_set_novalidate_class(&dev->mutex);
1981 spin_lock_init(&dev->devres_lock);
1982 INIT_LIST_HEAD(&dev->devres_head);
1983 device_pm_init(dev);
1984 set_dev_node(dev, -1);
1985 #ifdef CONFIG_GENERIC_MSI_IRQ
1986 INIT_LIST_HEAD(&dev->msi_list);
1988 INIT_LIST_HEAD(&dev->links.consumers);
1989 INIT_LIST_HEAD(&dev->links.suppliers);
1990 INIT_LIST_HEAD(&dev->links.needs_suppliers);
1991 INIT_LIST_HEAD(&dev->links.defer_sync);
1992 dev->links.status = DL_DEV_NO_DRIVER;
1994 EXPORT_SYMBOL_GPL(device_initialize);
1996 struct kobject *virtual_device_parent(struct device *dev)
1998 static struct kobject *virtual_dir = NULL;
2001 virtual_dir = kobject_create_and_add("virtual",
2002 &devices_kset->kobj);
2008 struct kobject kobj;
2009 struct class *class;
2012 #define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
2014 static void class_dir_release(struct kobject *kobj)
2016 struct class_dir *dir = to_class_dir(kobj);
2021 struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
2023 struct class_dir *dir = to_class_dir(kobj);
2024 return dir->class->ns_type;
2027 static struct kobj_type class_dir_ktype = {
2028 .release = class_dir_release,
2029 .sysfs_ops = &kobj_sysfs_ops,
2030 .child_ns_type = class_dir_child_ns_type
2033 static struct kobject *
2034 class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
2036 struct class_dir *dir;
2039 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
2041 return ERR_PTR(-ENOMEM);
2044 kobject_init(&dir->kobj, &class_dir_ktype);
2046 dir->kobj.kset = &class->p->glue_dirs;
2048 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
2050 kobject_put(&dir->kobj);
2051 return ERR_PTR(retval);
2056 static DEFINE_MUTEX(gdp_mutex);
2058 static struct kobject *get_device_parent(struct device *dev,
2059 struct device *parent)
2062 struct kobject *kobj = NULL;
2063 struct kobject *parent_kobj;
2067 /* block disks show up in /sys/block */
2068 if (sysfs_deprecated && dev->class == &block_class) {
2069 if (parent && parent->class == &block_class)
2070 return &parent->kobj;
2071 return &block_class.p->subsys.kobj;
2076 * If we have no parent, we live in "virtual".
2077 * Class-devices with a non class-device as parent, live
2078 * in a "glue" directory to prevent namespace collisions.
2081 parent_kobj = virtual_device_parent(dev);
2082 else if (parent->class && !dev->class->ns_type)
2083 return &parent->kobj;
2085 parent_kobj = &parent->kobj;
2087 mutex_lock(&gdp_mutex);
2089 /* find our class-directory at the parent and reference it */
2090 spin_lock(&dev->class->p->glue_dirs.list_lock);
2091 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
2092 if (k->parent == parent_kobj) {
2093 kobj = kobject_get(k);
2096 spin_unlock(&dev->class->p->glue_dirs.list_lock);
2098 mutex_unlock(&gdp_mutex);
2102 /* or create a new class-directory at the parent device */
2103 k = class_dir_create_and_add(dev->class, parent_kobj);
2104 /* do not emit an uevent for this simple "glue" directory */
2105 mutex_unlock(&gdp_mutex);
2109 /* subsystems can specify a default root directory for their devices */
2110 if (!parent && dev->bus && dev->bus->dev_root)
2111 return &dev->bus->dev_root->kobj;
2114 return &parent->kobj;
2118 static inline bool live_in_glue_dir(struct kobject *kobj,
2121 if (!kobj || !dev->class ||
2122 kobj->kset != &dev->class->p->glue_dirs)
2127 static inline struct kobject *get_glue_dir(struct device *dev)
2129 return dev->kobj.parent;
2133 * make sure cleaning up dir as the last step, we need to make
2134 * sure .release handler of kobject is run with holding the
2137 static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
2141 /* see if we live in a "glue" directory */
2142 if (!live_in_glue_dir(glue_dir, dev))
2145 mutex_lock(&gdp_mutex);
2147 * There is a race condition between removing glue directory
2148 * and adding a new device under the glue directory.
2153 * get_device_parent()
2154 * class_dir_create_and_add()
2155 * kobject_add_internal()
2156 * create_dir() // create glue_dir
2159 * get_device_parent()
2160 * kobject_get() // get glue_dir
2163 * cleanup_glue_dir()
2164 * kobject_del(glue_dir)
2167 * kobject_add_internal()
2168 * create_dir() // in glue_dir
2169 * sysfs_create_dir_ns()
2170 * kernfs_create_dir_ns(sd)
2172 * sysfs_remove_dir() // glue_dir->sd=NULL
2173 * sysfs_put() // free glue_dir->sd
2176 * kernfs_new_node(sd)
2177 * kernfs_get(glue_dir)
2181 * Before CPU1 remove last child device under glue dir, if CPU2 add
2182 * a new device under glue dir, the glue_dir kobject reference count
2183 * will be increase to 2 in kobject_get(k). And CPU2 has been called
2184 * kernfs_create_dir_ns(). Meanwhile, CPU1 call sysfs_remove_dir()
2185 * and sysfs_put(). This result in glue_dir->sd is freed.
2187 * Then the CPU2 will see a stale "empty" but still potentially used
2188 * glue dir around in kernfs_new_node().
2190 * In order to avoid this happening, we also should make sure that
2191 * kernfs_node for glue_dir is released in CPU1 only when refcount
2192 * for glue_dir kobj is 1.
2194 ref = kref_read(&glue_dir->kref);
2195 if (!kobject_has_children(glue_dir) && !--ref)
2196 kobject_del(glue_dir);
2197 kobject_put(glue_dir);
2198 mutex_unlock(&gdp_mutex);
2201 static int device_add_class_symlinks(struct device *dev)
2203 struct device_node *of_node = dev_of_node(dev);
2207 error = sysfs_create_link(&dev->kobj, of_node_kobj(of_node), "of_node");
2209 dev_warn(dev, "Error %d creating of_node link\n",error);
2210 /* An error here doesn't warrant bringing down the device */
2216 error = sysfs_create_link(&dev->kobj,
2217 &dev->class->p->subsys.kobj,
2222 if (dev->parent && device_is_not_partition(dev)) {
2223 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
2230 /* /sys/block has directories and does not need symlinks */
2231 if (sysfs_deprecated && dev->class == &block_class)
2235 /* link in the class directory pointing to the device */
2236 error = sysfs_create_link(&dev->class->p->subsys.kobj,
2237 &dev->kobj, dev_name(dev));
2244 sysfs_remove_link(&dev->kobj, "device");
2247 sysfs_remove_link(&dev->kobj, "subsystem");
2249 sysfs_remove_link(&dev->kobj, "of_node");
2253 static void device_remove_class_symlinks(struct device *dev)
2255 if (dev_of_node(dev))
2256 sysfs_remove_link(&dev->kobj, "of_node");
2261 if (dev->parent && device_is_not_partition(dev))
2262 sysfs_remove_link(&dev->kobj, "device");
2263 sysfs_remove_link(&dev->kobj, "subsystem");
2265 if (sysfs_deprecated && dev->class == &block_class)
2268 sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
2272 * dev_set_name - set a device name
2274 * @fmt: format string for the device's name
2276 int dev_set_name(struct device *dev, const char *fmt, ...)
2281 va_start(vargs, fmt);
2282 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
2286 EXPORT_SYMBOL_GPL(dev_set_name);
2289 * device_to_dev_kobj - select a /sys/dev/ directory for the device
2292 * By default we select char/ for new entries. Setting class->dev_obj
2293 * to NULL prevents an entry from being created. class->dev_kobj must
2294 * be set (or cleared) before any devices are registered to the class
2295 * otherwise device_create_sys_dev_entry() and
2296 * device_remove_sys_dev_entry() will disagree about the presence of
2299 static struct kobject *device_to_dev_kobj(struct device *dev)
2301 struct kobject *kobj;
2304 kobj = dev->class->dev_kobj;
2306 kobj = sysfs_dev_char_kobj;
2311 static int device_create_sys_dev_entry(struct device *dev)
2313 struct kobject *kobj = device_to_dev_kobj(dev);
2318 format_dev_t(devt_str, dev->devt);
2319 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
2325 static void device_remove_sys_dev_entry(struct device *dev)
2327 struct kobject *kobj = device_to_dev_kobj(dev);
2331 format_dev_t(devt_str, dev->devt);
2332 sysfs_remove_link(kobj, devt_str);
2336 static int device_private_init(struct device *dev)
2338 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
2341 dev->p->device = dev;
2342 klist_init(&dev->p->klist_children, klist_children_get,
2343 klist_children_put);
2344 INIT_LIST_HEAD(&dev->p->deferred_probe);
2348 static u32 fw_devlink_flags;
2349 static int __init fw_devlink_setup(char *arg)
2354 if (strcmp(arg, "off") == 0) {
2355 fw_devlink_flags = 0;
2356 } else if (strcmp(arg, "permissive") == 0) {
2357 fw_devlink_flags = DL_FLAG_SYNC_STATE_ONLY;
2358 } else if (strcmp(arg, "on") == 0) {
2359 fw_devlink_flags = DL_FLAG_AUTOPROBE_CONSUMER;
2360 } else if (strcmp(arg, "rpm") == 0) {
2361 fw_devlink_flags = DL_FLAG_AUTOPROBE_CONSUMER |
2366 early_param("fw_devlink", fw_devlink_setup);
2368 u32 fw_devlink_get_flags(void)
2370 return fw_devlink_flags;
2373 static bool fw_devlink_is_permissive(void)
2375 return fw_devlink_flags == DL_FLAG_SYNC_STATE_ONLY;
2379 * device_add - add device to device hierarchy.
2382 * This is part 2 of device_register(), though may be called
2383 * separately _iff_ device_initialize() has been called separately.
2385 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
2386 * to the global and sibling lists for the device, then
2387 * adds it to the other relevant subsystems of the driver model.
2389 * Do not call this routine or device_register() more than once for
2390 * any device structure. The driver model core is not designed to work
2391 * with devices that get unregistered and then spring back to life.
2392 * (Among other things, it's very hard to guarantee that all references
2393 * to the previous incarnation of @dev have been dropped.) Allocate
2394 * and register a fresh new struct device instead.
2396 * NOTE: _Never_ directly free @dev after calling this function, even
2397 * if it returned an error! Always use put_device() to give up your
2398 * reference instead.
2400 * Rule of thumb is: if device_add() succeeds, you should call
2401 * device_del() when you want to get rid of it. If device_add() has
2402 * *not* succeeded, use *only* put_device() to drop the reference
2405 int device_add(struct device *dev)
2407 struct device *parent;
2408 struct kobject *kobj;
2409 struct class_interface *class_intf;
2410 int error = -EINVAL, fw_ret;
2411 struct kobject *glue_dir = NULL;
2412 bool is_fwnode_dev = false;
2414 dev = get_device(dev);
2419 error = device_private_init(dev);
2425 * for statically allocated devices, which should all be converted
2426 * some day, we need to initialize the name. We prevent reading back
2427 * the name, and force the use of dev_name()
2429 if (dev->init_name) {
2430 dev_set_name(dev, "%s", dev->init_name);
2431 dev->init_name = NULL;
2434 /* subsystems can specify simple device enumeration */
2435 if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
2436 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
2438 if (!dev_name(dev)) {
2443 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
2445 parent = get_device(dev->parent);
2446 kobj = get_device_parent(dev, parent);
2448 error = PTR_ERR(kobj);
2452 dev->kobj.parent = kobj;
2454 /* use parent numa_node */
2455 if (parent && (dev_to_node(dev) == NUMA_NO_NODE))
2456 set_dev_node(dev, dev_to_node(parent));
2458 /* first, register with generic layer. */
2459 /* we require the name to be set before, and pass NULL */
2460 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
2462 glue_dir = get_glue_dir(dev);
2466 /* notify platform of device entry */
2467 error = device_platform_notify(dev, KOBJ_ADD);
2469 goto platform_error;
2471 error = device_create_file(dev, &dev_attr_uevent);
2475 error = device_add_class_symlinks(dev);
2478 error = device_add_attrs(dev);
2481 error = bus_add_device(dev);
2484 error = dpm_sysfs_add(dev);
2489 if (MAJOR(dev->devt)) {
2490 error = device_create_file(dev, &dev_attr_dev);
2494 error = device_create_sys_dev_entry(dev);
2498 devtmpfs_create_node(dev);
2501 /* Notify clients of device addition. This call must come
2502 * after dpm_sysfs_add() and before kobject_uevent().
2505 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2506 BUS_NOTIFY_ADD_DEVICE, dev);
2508 kobject_uevent(&dev->kobj, KOBJ_ADD);
2510 if (dev->fwnode && !dev->fwnode->dev) {
2511 dev->fwnode->dev = dev;
2512 is_fwnode_dev = true;
2516 * Check if any of the other devices (consumers) have been waiting for
2517 * this device (supplier) to be added so that they can create a device
2520 * This needs to happen after device_pm_add() because device_link_add()
2521 * requires the supplier be registered before it's called.
2523 * But this also needs to happe before bus_probe_device() to make sure
2524 * waiting consumers can link to it before the driver is bound to the
2525 * device and the driver sync_state callback is called for this device.
2527 device_link_add_missing_supplier_links();
2529 if (fw_devlink_flags && is_fwnode_dev &&
2530 fwnode_has_op(dev->fwnode, add_links)) {
2531 fw_ret = fwnode_call_int_op(dev->fwnode, add_links, dev);
2532 if (fw_ret == -ENODEV && !fw_devlink_is_permissive())
2533 device_link_wait_for_mandatory_supplier(dev);
2535 device_link_wait_for_optional_supplier(dev);
2538 bus_probe_device(dev);
2540 klist_add_tail(&dev->p->knode_parent,
2541 &parent->p->klist_children);
2544 mutex_lock(&dev->class->p->mutex);
2545 /* tie the class to the device */
2546 klist_add_tail(&dev->p->knode_class,
2547 &dev->class->p->klist_devices);
2549 /* notify any interfaces that the device is here */
2550 list_for_each_entry(class_intf,
2551 &dev->class->p->interfaces, node)
2552 if (class_intf->add_dev)
2553 class_intf->add_dev(dev, class_intf);
2554 mutex_unlock(&dev->class->p->mutex);
2560 if (MAJOR(dev->devt))
2561 device_remove_file(dev, &dev_attr_dev);
2563 device_pm_remove(dev);
2564 dpm_sysfs_remove(dev);
2566 bus_remove_device(dev);
2568 device_remove_attrs(dev);
2570 device_remove_class_symlinks(dev);
2572 device_remove_file(dev, &dev_attr_uevent);
2574 device_platform_notify(dev, KOBJ_REMOVE);
2576 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
2577 glue_dir = get_glue_dir(dev);
2578 kobject_del(&dev->kobj);
2580 cleanup_glue_dir(dev, glue_dir);
2588 EXPORT_SYMBOL_GPL(device_add);
2591 * device_register - register a device with the system.
2592 * @dev: pointer to the device structure
2594 * This happens in two clean steps - initialize the device
2595 * and add it to the system. The two steps can be called
2596 * separately, but this is the easiest and most common.
2597 * I.e. you should only call the two helpers separately if
2598 * have a clearly defined need to use and refcount the device
2599 * before it is added to the hierarchy.
2601 * For more information, see the kerneldoc for device_initialize()
2604 * NOTE: _Never_ directly free @dev after calling this function, even
2605 * if it returned an error! Always use put_device() to give up the
2606 * reference initialized in this function instead.
2608 int device_register(struct device *dev)
2610 device_initialize(dev);
2611 return device_add(dev);
2613 EXPORT_SYMBOL_GPL(device_register);
2616 * get_device - increment reference count for device.
2619 * This simply forwards the call to kobject_get(), though
2620 * we do take care to provide for the case that we get a NULL
2621 * pointer passed in.
2623 struct device *get_device(struct device *dev)
2625 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
2627 EXPORT_SYMBOL_GPL(get_device);
2630 * put_device - decrement reference count.
2631 * @dev: device in question.
2633 void put_device(struct device *dev)
2635 /* might_sleep(); */
2637 kobject_put(&dev->kobj);
2639 EXPORT_SYMBOL_GPL(put_device);
2641 bool kill_device(struct device *dev)
2644 * Require the device lock and set the "dead" flag to guarantee that
2645 * the update behavior is consistent with the other bitfields near
2646 * it and that we cannot have an asynchronous probe routine trying
2647 * to run while we are tearing out the bus/class/sysfs from
2648 * underneath the device.
2650 lockdep_assert_held(&dev->mutex);
2654 dev->p->dead = true;
2657 EXPORT_SYMBOL_GPL(kill_device);
2660 * device_del - delete device from system.
2663 * This is the first part of the device unregistration
2664 * sequence. This removes the device from the lists we control
2665 * from here, has it removed from the other driver model
2666 * subsystems it was added to in device_add(), and removes it
2667 * from the kobject hierarchy.
2669 * NOTE: this should be called manually _iff_ device_add() was
2670 * also called manually.
2672 void device_del(struct device *dev)
2674 struct device *parent = dev->parent;
2675 struct kobject *glue_dir = NULL;
2676 struct class_interface *class_intf;
2682 if (dev->fwnode && dev->fwnode->dev == dev)
2683 dev->fwnode->dev = NULL;
2685 /* Notify clients of device removal. This call must come
2686 * before dpm_sysfs_remove().
2689 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2690 BUS_NOTIFY_DEL_DEVICE, dev);
2692 dpm_sysfs_remove(dev);
2694 klist_del(&dev->p->knode_parent);
2695 if (MAJOR(dev->devt)) {
2696 devtmpfs_delete_node(dev);
2697 device_remove_sys_dev_entry(dev);
2698 device_remove_file(dev, &dev_attr_dev);
2701 device_remove_class_symlinks(dev);
2703 mutex_lock(&dev->class->p->mutex);
2704 /* notify any interfaces that the device is now gone */
2705 list_for_each_entry(class_intf,
2706 &dev->class->p->interfaces, node)
2707 if (class_intf->remove_dev)
2708 class_intf->remove_dev(dev, class_intf);
2709 /* remove the device from the class list */
2710 klist_del(&dev->p->knode_class);
2711 mutex_unlock(&dev->class->p->mutex);
2713 device_remove_file(dev, &dev_attr_uevent);
2714 device_remove_attrs(dev);
2715 bus_remove_device(dev);
2716 device_pm_remove(dev);
2717 driver_deferred_probe_del(dev);
2718 device_platform_notify(dev, KOBJ_REMOVE);
2719 device_remove_properties(dev);
2720 device_links_purge(dev);
2723 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2724 BUS_NOTIFY_REMOVED_DEVICE, dev);
2725 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
2726 glue_dir = get_glue_dir(dev);
2727 kobject_del(&dev->kobj);
2728 cleanup_glue_dir(dev, glue_dir);
2731 EXPORT_SYMBOL_GPL(device_del);
2734 * device_unregister - unregister device from system.
2735 * @dev: device going away.
2737 * We do this in two parts, like we do device_register(). First,
2738 * we remove it from all the subsystems with device_del(), then
2739 * we decrement the reference count via put_device(). If that
2740 * is the final reference count, the device will be cleaned up
2741 * via device_release() above. Otherwise, the structure will
2742 * stick around until the final reference to the device is dropped.
2744 void device_unregister(struct device *dev)
2746 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
2750 EXPORT_SYMBOL_GPL(device_unregister);
2752 static struct device *prev_device(struct klist_iter *i)
2754 struct klist_node *n = klist_prev(i);
2755 struct device *dev = NULL;
2756 struct device_private *p;
2759 p = to_device_private_parent(n);
2765 static struct device *next_device(struct klist_iter *i)
2767 struct klist_node *n = klist_next(i);
2768 struct device *dev = NULL;
2769 struct device_private *p;
2772 p = to_device_private_parent(n);
2779 * device_get_devnode - path of device node file
2781 * @mode: returned file access mode
2782 * @uid: returned file owner
2783 * @gid: returned file group
2784 * @tmp: possibly allocated string
2786 * Return the relative path of a possible device node.
2787 * Non-default names may need to allocate a memory to compose
2788 * a name. This memory is returned in tmp and needs to be
2789 * freed by the caller.
2791 const char *device_get_devnode(struct device *dev,
2792 umode_t *mode, kuid_t *uid, kgid_t *gid,
2799 /* the device type may provide a specific name */
2800 if (dev->type && dev->type->devnode)
2801 *tmp = dev->type->devnode(dev, mode, uid, gid);
2805 /* the class may provide a specific name */
2806 if (dev->class && dev->class->devnode)
2807 *tmp = dev->class->devnode(dev, mode);
2811 /* return name without allocation, tmp == NULL */
2812 if (strchr(dev_name(dev), '!') == NULL)
2813 return dev_name(dev);
2815 /* replace '!' in the name with '/' */
2816 s = kstrdup(dev_name(dev), GFP_KERNEL);
2819 strreplace(s, '!', '/');
2824 * device_for_each_child - device child iterator.
2825 * @parent: parent struct device.
2826 * @fn: function to be called for each device.
2827 * @data: data for the callback.
2829 * Iterate over @parent's child devices, and call @fn for each,
2832 * We check the return of @fn each time. If it returns anything
2833 * other than 0, we break out and return that value.
2835 int device_for_each_child(struct device *parent, void *data,
2836 int (*fn)(struct device *dev, void *data))
2838 struct klist_iter i;
2839 struct device *child;
2845 klist_iter_init(&parent->p->klist_children, &i);
2846 while (!error && (child = next_device(&i)))
2847 error = fn(child, data);
2848 klist_iter_exit(&i);
2851 EXPORT_SYMBOL_GPL(device_for_each_child);
2854 * device_for_each_child_reverse - device child iterator in reversed order.
2855 * @parent: parent struct device.
2856 * @fn: function to be called for each device.
2857 * @data: data for the callback.
2859 * Iterate over @parent's child devices, and call @fn for each,
2862 * We check the return of @fn each time. If it returns anything
2863 * other than 0, we break out and return that value.
2865 int device_for_each_child_reverse(struct device *parent, void *data,
2866 int (*fn)(struct device *dev, void *data))
2868 struct klist_iter i;
2869 struct device *child;
2875 klist_iter_init(&parent->p->klist_children, &i);
2876 while ((child = prev_device(&i)) && !error)
2877 error = fn(child, data);
2878 klist_iter_exit(&i);
2881 EXPORT_SYMBOL_GPL(device_for_each_child_reverse);
2884 * device_find_child - device iterator for locating a particular device.
2885 * @parent: parent struct device
2886 * @match: Callback function to check device
2887 * @data: Data to pass to match function
2889 * This is similar to the device_for_each_child() function above, but it
2890 * returns a reference to a device that is 'found' for later use, as
2891 * determined by the @match callback.
2893 * The callback should return 0 if the device doesn't match and non-zero
2894 * if it does. If the callback returns non-zero and a reference to the
2895 * current device can be obtained, this function will return to the caller
2896 * and not iterate over any more devices.
2898 * NOTE: you will need to drop the reference with put_device() after use.
2900 struct device *device_find_child(struct device *parent, void *data,
2901 int (*match)(struct device *dev, void *data))
2903 struct klist_iter i;
2904 struct device *child;
2909 klist_iter_init(&parent->p->klist_children, &i);
2910 while ((child = next_device(&i)))
2911 if (match(child, data) && get_device(child))
2913 klist_iter_exit(&i);
2916 EXPORT_SYMBOL_GPL(device_find_child);
2919 * device_find_child_by_name - device iterator for locating a child device.
2920 * @parent: parent struct device
2921 * @name: name of the child device
2923 * This is similar to the device_find_child() function above, but it
2924 * returns a reference to a device that has the name @name.
2926 * NOTE: you will need to drop the reference with put_device() after use.
2928 struct device *device_find_child_by_name(struct device *parent,
2931 struct klist_iter i;
2932 struct device *child;
2937 klist_iter_init(&parent->p->klist_children, &i);
2938 while ((child = next_device(&i)))
2939 if (!strcmp(dev_name(child), name) && get_device(child))
2941 klist_iter_exit(&i);
2944 EXPORT_SYMBOL_GPL(device_find_child_by_name);
2946 int __init devices_init(void)
2948 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
2951 dev_kobj = kobject_create_and_add("dev", NULL);
2954 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
2955 if (!sysfs_dev_block_kobj)
2956 goto block_kobj_err;
2957 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
2958 if (!sysfs_dev_char_kobj)
2964 kobject_put(sysfs_dev_block_kobj);
2966 kobject_put(dev_kobj);
2968 kset_unregister(devices_kset);
2972 static int device_check_offline(struct device *dev, void *not_used)
2976 ret = device_for_each_child(dev, NULL, device_check_offline);
2980 return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
2984 * device_offline - Prepare the device for hot-removal.
2985 * @dev: Device to be put offline.
2987 * Execute the device bus type's .offline() callback, if present, to prepare
2988 * the device for a subsequent hot-removal. If that succeeds, the device must
2989 * not be used until either it is removed or its bus type's .online() callback
2992 * Call under device_hotplug_lock.
2994 int device_offline(struct device *dev)
2998 if (dev->offline_disabled)
3001 ret = device_for_each_child(dev, NULL, device_check_offline);
3006 if (device_supports_offline(dev)) {
3010 ret = dev->bus->offline(dev);
3012 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
3013 dev->offline = true;
3023 * device_online - Put the device back online after successful device_offline().
3024 * @dev: Device to be put back online.
3026 * If device_offline() has been successfully executed for @dev, but the device
3027 * has not been removed subsequently, execute its bus type's .online() callback
3028 * to indicate that the device can be used again.
3030 * Call under device_hotplug_lock.
3032 int device_online(struct device *dev)
3037 if (device_supports_offline(dev)) {
3039 ret = dev->bus->online(dev);
3041 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
3042 dev->offline = false;
3053 struct root_device {
3055 struct module *owner;
3058 static inline struct root_device *to_root_device(struct device *d)
3060 return container_of(d, struct root_device, dev);
3063 static void root_device_release(struct device *dev)
3065 kfree(to_root_device(dev));
3069 * __root_device_register - allocate and register a root device
3070 * @name: root device name
3071 * @owner: owner module of the root device, usually THIS_MODULE
3073 * This function allocates a root device and registers it
3074 * using device_register(). In order to free the returned
3075 * device, use root_device_unregister().
3077 * Root devices are dummy devices which allow other devices
3078 * to be grouped under /sys/devices. Use this function to
3079 * allocate a root device and then use it as the parent of
3080 * any device which should appear under /sys/devices/{name}
3082 * The /sys/devices/{name} directory will also contain a
3083 * 'module' symlink which points to the @owner directory
3086 * Returns &struct device pointer on success, or ERR_PTR() on error.
3088 * Note: You probably want to use root_device_register().
3090 struct device *__root_device_register(const char *name, struct module *owner)
3092 struct root_device *root;
3095 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
3097 return ERR_PTR(err);
3099 err = dev_set_name(&root->dev, "%s", name);
3102 return ERR_PTR(err);
3105 root->dev.release = root_device_release;
3107 err = device_register(&root->dev);
3109 put_device(&root->dev);
3110 return ERR_PTR(err);
3113 #ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
3115 struct module_kobject *mk = &owner->mkobj;
3117 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
3119 device_unregister(&root->dev);
3120 return ERR_PTR(err);
3122 root->owner = owner;
3128 EXPORT_SYMBOL_GPL(__root_device_register);
3131 * root_device_unregister - unregister and free a root device
3132 * @dev: device going away
3134 * This function unregisters and cleans up a device that was created by
3135 * root_device_register().
3137 void root_device_unregister(struct device *dev)
3139 struct root_device *root = to_root_device(dev);
3142 sysfs_remove_link(&root->dev.kobj, "module");
3144 device_unregister(dev);
3146 EXPORT_SYMBOL_GPL(root_device_unregister);
3149 static void device_create_release(struct device *dev)
3151 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
3155 static __printf(6, 0) struct device *
3156 device_create_groups_vargs(struct class *class, struct device *parent,
3157 dev_t devt, void *drvdata,
3158 const struct attribute_group **groups,
3159 const char *fmt, va_list args)
3161 struct device *dev = NULL;
3162 int retval = -ENODEV;
3164 if (class == NULL || IS_ERR(class))
3167 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
3173 device_initialize(dev);
3176 dev->parent = parent;
3177 dev->groups = groups;
3178 dev->release = device_create_release;
3179 dev_set_drvdata(dev, drvdata);
3181 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
3185 retval = device_add(dev);
3193 return ERR_PTR(retval);
3197 * device_create_vargs - creates a device and registers it with sysfs
3198 * @class: pointer to the struct class that this device should be registered to
3199 * @parent: pointer to the parent struct device of this new device, if any
3200 * @devt: the dev_t for the char device to be added
3201 * @drvdata: the data to be added to the device for callbacks
3202 * @fmt: string for the device's name
3203 * @args: va_list for the device's name
3205 * This function can be used by char device classes. A struct device
3206 * will be created in sysfs, registered to the specified class.
3208 * A "dev" file will be created, showing the dev_t for the device, if
3209 * the dev_t is not 0,0.
3210 * If a pointer to a parent struct device is passed in, the newly created
3211 * struct device will be a child of that device in sysfs.
3212 * The pointer to the struct device will be returned from the call.
3213 * Any further sysfs files that might be required can be created using this
3216 * Returns &struct device pointer on success, or ERR_PTR() on error.
3218 * Note: the struct class passed to this function must have previously
3219 * been created with a call to class_create().
3221 struct device *device_create_vargs(struct class *class, struct device *parent,
3222 dev_t devt, void *drvdata, const char *fmt,
3225 return device_create_groups_vargs(class, parent, devt, drvdata, NULL,
3228 EXPORT_SYMBOL_GPL(device_create_vargs);
3231 * device_create - creates a device and registers it with sysfs
3232 * @class: pointer to the struct class that this device should be registered to
3233 * @parent: pointer to the parent struct device of this new device, if any
3234 * @devt: the dev_t for the char device to be added
3235 * @drvdata: the data to be added to the device for callbacks
3236 * @fmt: string for the device's name
3238 * This function can be used by char device classes. A struct device
3239 * will be created in sysfs, registered to the specified class.
3241 * A "dev" file will be created, showing the dev_t for the device, if
3242 * the dev_t is not 0,0.
3243 * If a pointer to a parent struct device is passed in, the newly created
3244 * struct device will be a child of that device in sysfs.
3245 * The pointer to the struct device will be returned from the call.
3246 * Any further sysfs files that might be required can be created using this
3249 * Returns &struct device pointer on success, or ERR_PTR() on error.
3251 * Note: the struct class passed to this function must have previously
3252 * been created with a call to class_create().
3254 struct device *device_create(struct class *class, struct device *parent,
3255 dev_t devt, void *drvdata, const char *fmt, ...)
3260 va_start(vargs, fmt);
3261 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
3265 EXPORT_SYMBOL_GPL(device_create);
3268 * device_create_with_groups - creates a device and registers it with sysfs
3269 * @class: pointer to the struct class that this device should be registered to
3270 * @parent: pointer to the parent struct device of this new device, if any
3271 * @devt: the dev_t for the char device to be added
3272 * @drvdata: the data to be added to the device for callbacks
3273 * @groups: NULL-terminated list of attribute groups to be created
3274 * @fmt: string for the device's name
3276 * This function can be used by char device classes. A struct device
3277 * will be created in sysfs, registered to the specified class.
3278 * Additional attributes specified in the groups parameter will also
3279 * be created automatically.
3281 * A "dev" file will be created, showing the dev_t for the device, if
3282 * the dev_t is not 0,0.
3283 * If a pointer to a parent struct device is passed in, the newly created
3284 * struct device will be a child of that device in sysfs.
3285 * The pointer to the struct device will be returned from the call.
3286 * Any further sysfs files that might be required can be created using this
3289 * Returns &struct device pointer on success, or ERR_PTR() on error.
3291 * Note: the struct class passed to this function must have previously
3292 * been created with a call to class_create().
3294 struct device *device_create_with_groups(struct class *class,
3295 struct device *parent, dev_t devt,
3297 const struct attribute_group **groups,
3298 const char *fmt, ...)
3303 va_start(vargs, fmt);
3304 dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
3309 EXPORT_SYMBOL_GPL(device_create_with_groups);
3312 * device_destroy - removes a device that was created with device_create()
3313 * @class: pointer to the struct class that this device was registered with
3314 * @devt: the dev_t of the device that was previously registered
3316 * This call unregisters and cleans up a device that was created with a
3317 * call to device_create().
3319 void device_destroy(struct class *class, dev_t devt)
3323 dev = class_find_device_by_devt(class, devt);
3326 device_unregister(dev);
3329 EXPORT_SYMBOL_GPL(device_destroy);
3332 * device_rename - renames a device
3333 * @dev: the pointer to the struct device to be renamed
3334 * @new_name: the new name of the device
3336 * It is the responsibility of the caller to provide mutual
3337 * exclusion between two different calls of device_rename
3338 * on the same device to ensure that new_name is valid and
3339 * won't conflict with other devices.
3341 * Note: Don't call this function. Currently, the networking layer calls this
3342 * function, but that will change. The following text from Kay Sievers offers
3345 * Renaming devices is racy at many levels, symlinks and other stuff are not
3346 * replaced atomically, and you get a "move" uevent, but it's not easy to
3347 * connect the event to the old and new device. Device nodes are not renamed at
3348 * all, there isn't even support for that in the kernel now.
3350 * In the meantime, during renaming, your target name might be taken by another
3351 * driver, creating conflicts. Or the old name is taken directly after you
3352 * renamed it -- then you get events for the same DEVPATH, before you even see
3353 * the "move" event. It's just a mess, and nothing new should ever rely on
3354 * kernel device renaming. Besides that, it's not even implemented now for
3355 * other things than (driver-core wise very simple) network devices.
3357 * We are currently about to change network renaming in udev to completely
3358 * disallow renaming of devices in the same namespace as the kernel uses,
3359 * because we can't solve the problems properly, that arise with swapping names
3360 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
3361 * be allowed to some other name than eth[0-9]*, for the aforementioned
3364 * Make up a "real" name in the driver before you register anything, or add
3365 * some other attributes for userspace to find the device, or use udev to add
3366 * symlinks -- but never rename kernel devices later, it's a complete mess. We
3367 * don't even want to get into that and try to implement the missing pieces in
3368 * the core. We really have other pieces to fix in the driver core mess. :)
3370 int device_rename(struct device *dev, const char *new_name)
3372 struct kobject *kobj = &dev->kobj;
3373 char *old_device_name = NULL;
3376 dev = get_device(dev);
3380 dev_dbg(dev, "renaming to %s\n", new_name);
3382 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
3383 if (!old_device_name) {
3389 error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj,
3390 kobj, old_device_name,
3391 new_name, kobject_namespace(kobj));
3396 error = kobject_rename(kobj, new_name);
3403 kfree(old_device_name);
3407 EXPORT_SYMBOL_GPL(device_rename);
3409 static int device_move_class_links(struct device *dev,
3410 struct device *old_parent,
3411 struct device *new_parent)
3416 sysfs_remove_link(&dev->kobj, "device");
3418 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
3424 * device_move - moves a device to a new parent
3425 * @dev: the pointer to the struct device to be moved
3426 * @new_parent: the new parent of the device (can be NULL)
3427 * @dpm_order: how to reorder the dpm_list
3429 int device_move(struct device *dev, struct device *new_parent,
3430 enum dpm_order dpm_order)
3433 struct device *old_parent;
3434 struct kobject *new_parent_kobj;
3436 dev = get_device(dev);
3441 new_parent = get_device(new_parent);
3442 new_parent_kobj = get_device_parent(dev, new_parent);
3443 if (IS_ERR(new_parent_kobj)) {
3444 error = PTR_ERR(new_parent_kobj);
3445 put_device(new_parent);
3449 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
3450 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
3451 error = kobject_move(&dev->kobj, new_parent_kobj);
3453 cleanup_glue_dir(dev, new_parent_kobj);
3454 put_device(new_parent);
3457 old_parent = dev->parent;
3458 dev->parent = new_parent;
3460 klist_remove(&dev->p->knode_parent);
3462 klist_add_tail(&dev->p->knode_parent,
3463 &new_parent->p->klist_children);
3464 set_dev_node(dev, dev_to_node(new_parent));
3468 error = device_move_class_links(dev, old_parent, new_parent);
3470 /* We ignore errors on cleanup since we're hosed anyway... */
3471 device_move_class_links(dev, new_parent, old_parent);
3472 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
3474 klist_remove(&dev->p->knode_parent);
3475 dev->parent = old_parent;
3477 klist_add_tail(&dev->p->knode_parent,
3478 &old_parent->p->klist_children);
3479 set_dev_node(dev, dev_to_node(old_parent));
3482 cleanup_glue_dir(dev, new_parent_kobj);
3483 put_device(new_parent);
3487 switch (dpm_order) {
3488 case DPM_ORDER_NONE:
3490 case DPM_ORDER_DEV_AFTER_PARENT:
3491 device_pm_move_after(dev, new_parent);
3492 devices_kset_move_after(dev, new_parent);
3494 case DPM_ORDER_PARENT_BEFORE_DEV:
3495 device_pm_move_before(new_parent, dev);
3496 devices_kset_move_before(new_parent, dev);
3498 case DPM_ORDER_DEV_LAST:
3499 device_pm_move_last(dev);
3500 devices_kset_move_last(dev);
3504 put_device(old_parent);
3510 EXPORT_SYMBOL_GPL(device_move);
3512 static int device_attrs_change_owner(struct device *dev, kuid_t kuid,
3515 struct kobject *kobj = &dev->kobj;
3516 struct class *class = dev->class;
3517 const struct device_type *type = dev->type;
3522 * Change the device groups of the device class for @dev to
3525 error = sysfs_groups_change_owner(kobj, class->dev_groups, kuid,
3533 * Change the device groups of the device type for @dev to
3536 error = sysfs_groups_change_owner(kobj, type->groups, kuid,
3542 /* Change the device groups of @dev to @kuid/@kgid. */
3543 error = sysfs_groups_change_owner(kobj, dev->groups, kuid, kgid);
3547 if (device_supports_offline(dev) && !dev->offline_disabled) {
3548 /* Change online device attributes of @dev to @kuid/@kgid. */
3549 error = sysfs_file_change_owner(kobj, dev_attr_online.attr.name,
3559 * device_change_owner - change the owner of an existing device.
3561 * @kuid: new owner's kuid
3562 * @kgid: new owner's kgid
3564 * This changes the owner of @dev and its corresponding sysfs entries to
3565 * @kuid/@kgid. This function closely mirrors how @dev was added via driver
3568 * Returns 0 on success or error code on failure.
3570 int device_change_owner(struct device *dev, kuid_t kuid, kgid_t kgid)
3573 struct kobject *kobj = &dev->kobj;
3575 dev = get_device(dev);
3580 * Change the kobject and the default attributes and groups of the
3581 * ktype associated with it to @kuid/@kgid.
3583 error = sysfs_change_owner(kobj, kuid, kgid);
3588 * Change the uevent file for @dev to the new owner. The uevent file
3589 * was created in a separate step when @dev got added and we mirror
3592 error = sysfs_file_change_owner(kobj, dev_attr_uevent.attr.name, kuid,
3598 * Change the device groups, the device groups associated with the
3599 * device class, and the groups associated with the device type of @dev
3602 error = device_attrs_change_owner(dev, kuid, kgid);
3606 error = dpm_sysfs_change_owner(dev, kuid, kgid);
3611 if (sysfs_deprecated && dev->class == &block_class)
3616 * Change the owner of the symlink located in the class directory of
3617 * the device class associated with @dev which points to the actual
3618 * directory entry for @dev to @kuid/@kgid. This ensures that the
3619 * symlink shows the same permissions as its target.
3621 error = sysfs_link_change_owner(&dev->class->p->subsys.kobj, &dev->kobj,
3622 dev_name(dev), kuid, kgid);
3630 EXPORT_SYMBOL_GPL(device_change_owner);
3633 * device_shutdown - call ->shutdown() on each device to shutdown.
3635 void device_shutdown(void)
3637 struct device *dev, *parent;
3639 wait_for_device_probe();
3640 device_block_probing();
3644 spin_lock(&devices_kset->list_lock);
3646 * Walk the devices list backward, shutting down each in turn.
3647 * Beware that device unplug events may also start pulling
3648 * devices offline, even as the system is shutting down.
3650 while (!list_empty(&devices_kset->list)) {
3651 dev = list_entry(devices_kset->list.prev, struct device,
3655 * hold reference count of device's parent to
3656 * prevent it from being freed because parent's
3657 * lock is to be held
3659 parent = get_device(dev->parent);
3662 * Make sure the device is off the kset list, in the
3663 * event that dev->*->shutdown() doesn't remove it.
3665 list_del_init(&dev->kobj.entry);
3666 spin_unlock(&devices_kset->list_lock);
3668 /* hold lock to avoid race with probe/release */
3670 device_lock(parent);
3673 /* Don't allow any more runtime suspends */
3674 pm_runtime_get_noresume(dev);
3675 pm_runtime_barrier(dev);
3677 if (dev->class && dev->class->shutdown_pre) {
3679 dev_info(dev, "shutdown_pre\n");
3680 dev->class->shutdown_pre(dev);
3682 if (dev->bus && dev->bus->shutdown) {
3684 dev_info(dev, "shutdown\n");
3685 dev->bus->shutdown(dev);
3686 } else if (dev->driver && dev->driver->shutdown) {
3688 dev_info(dev, "shutdown\n");
3689 dev->driver->shutdown(dev);
3694 device_unlock(parent);
3699 spin_lock(&devices_kset->list_lock);
3701 spin_unlock(&devices_kset->list_lock);
3705 * Device logging functions
3708 #ifdef CONFIG_PRINTK
3710 create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
3716 subsys = dev->class->name;
3718 subsys = dev->bus->name;
3722 pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys);
3727 * Add device identifier DEVICE=:
3731 * +sound:card0 subsystem:devname
3733 if (MAJOR(dev->devt)) {
3736 if (strcmp(subsys, "block") == 0)
3741 pos += snprintf(hdr + pos, hdrlen - pos,
3743 c, MAJOR(dev->devt), MINOR(dev->devt));
3744 } else if (strcmp(subsys, "net") == 0) {
3745 struct net_device *net = to_net_dev(dev);
3748 pos += snprintf(hdr + pos, hdrlen - pos,
3749 "DEVICE=n%u", net->ifindex);
3752 pos += snprintf(hdr + pos, hdrlen - pos,
3753 "DEVICE=+%s:%s", subsys, dev_name(dev));
3762 dev_WARN(dev, "device/subsystem name too long");
3766 int dev_vprintk_emit(int level, const struct device *dev,
3767 const char *fmt, va_list args)
3772 hdrlen = create_syslog_header(dev, hdr, sizeof(hdr));
3774 return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args);
3776 EXPORT_SYMBOL(dev_vprintk_emit);
3778 int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
3783 va_start(args, fmt);
3785 r = dev_vprintk_emit(level, dev, fmt, args);
3791 EXPORT_SYMBOL(dev_printk_emit);
3793 static void __dev_printk(const char *level, const struct device *dev,
3794 struct va_format *vaf)
3797 dev_printk_emit(level[1] - '0', dev, "%s %s: %pV",
3798 dev_driver_string(dev), dev_name(dev), vaf);
3800 printk("%s(NULL device *): %pV", level, vaf);
3803 void dev_printk(const char *level, const struct device *dev,
3804 const char *fmt, ...)
3806 struct va_format vaf;
3809 va_start(args, fmt);
3814 __dev_printk(level, dev, &vaf);
3818 EXPORT_SYMBOL(dev_printk);
3820 #define define_dev_printk_level(func, kern_level) \
3821 void func(const struct device *dev, const char *fmt, ...) \
3823 struct va_format vaf; \
3826 va_start(args, fmt); \
3831 __dev_printk(kern_level, dev, &vaf); \
3835 EXPORT_SYMBOL(func);
3837 define_dev_printk_level(_dev_emerg, KERN_EMERG);
3838 define_dev_printk_level(_dev_alert, KERN_ALERT);
3839 define_dev_printk_level(_dev_crit, KERN_CRIT);
3840 define_dev_printk_level(_dev_err, KERN_ERR);
3841 define_dev_printk_level(_dev_warn, KERN_WARNING);
3842 define_dev_printk_level(_dev_notice, KERN_NOTICE);
3843 define_dev_printk_level(_dev_info, KERN_INFO);
3847 static inline bool fwnode_is_primary(struct fwnode_handle *fwnode)
3849 return fwnode && !IS_ERR(fwnode->secondary);
3853 * set_primary_fwnode - Change the primary firmware node of a given device.
3854 * @dev: Device to handle.
3855 * @fwnode: New primary firmware node of the device.
3857 * Set the device's firmware node pointer to @fwnode, but if a secondary
3858 * firmware node of the device is present, preserve it.
3860 void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
3863 struct fwnode_handle *fn = dev->fwnode;
3865 if (fwnode_is_primary(fn))
3869 WARN_ON(fwnode->secondary);
3870 fwnode->secondary = fn;
3872 dev->fwnode = fwnode;
3874 dev->fwnode = fwnode_is_primary(dev->fwnode) ?
3875 dev->fwnode->secondary : NULL;
3878 EXPORT_SYMBOL_GPL(set_primary_fwnode);
3881 * set_secondary_fwnode - Change the secondary firmware node of a given device.
3882 * @dev: Device to handle.
3883 * @fwnode: New secondary firmware node of the device.
3885 * If a primary firmware node of the device is present, set its secondary
3886 * pointer to @fwnode. Otherwise, set the device's firmware node pointer to
3889 void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
3892 fwnode->secondary = ERR_PTR(-ENODEV);
3894 if (fwnode_is_primary(dev->fwnode))
3895 dev->fwnode->secondary = fwnode;
3897 dev->fwnode = fwnode;
3901 * device_set_of_node_from_dev - reuse device-tree node of another device
3902 * @dev: device whose device-tree node is being set
3903 * @dev2: device whose device-tree node is being reused
3905 * Takes another reference to the new device-tree node after first dropping
3906 * any reference held to the old node.
3908 void device_set_of_node_from_dev(struct device *dev, const struct device *dev2)
3910 of_node_put(dev->of_node);
3911 dev->of_node = of_node_get(dev2->of_node);
3912 dev->of_node_reused = true;
3914 EXPORT_SYMBOL_GPL(device_set_of_node_from_dev);
3916 int device_match_name(struct device *dev, const void *name)
3918 return sysfs_streq(dev_name(dev), name);
3920 EXPORT_SYMBOL_GPL(device_match_name);
3922 int device_match_of_node(struct device *dev, const void *np)
3924 return dev->of_node == np;
3926 EXPORT_SYMBOL_GPL(device_match_of_node);
3928 int device_match_fwnode(struct device *dev, const void *fwnode)
3930 return dev_fwnode(dev) == fwnode;
3932 EXPORT_SYMBOL_GPL(device_match_fwnode);
3934 int device_match_devt(struct device *dev, const void *pdevt)
3936 return dev->devt == *(dev_t *)pdevt;
3938 EXPORT_SYMBOL_GPL(device_match_devt);
3940 int device_match_acpi_dev(struct device *dev, const void *adev)
3942 return ACPI_COMPANION(dev) == adev;
3944 EXPORT_SYMBOL(device_match_acpi_dev);
3946 int device_match_any(struct device *dev, const void *unused)
3950 EXPORT_SYMBOL_GPL(device_match_any);