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;
2374 * device_add - add device to device hierarchy.
2377 * This is part 2 of device_register(), though may be called
2378 * separately _iff_ device_initialize() has been called separately.
2380 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
2381 * to the global and sibling lists for the device, then
2382 * adds it to the other relevant subsystems of the driver model.
2384 * Do not call this routine or device_register() more than once for
2385 * any device structure. The driver model core is not designed to work
2386 * with devices that get unregistered and then spring back to life.
2387 * (Among other things, it's very hard to guarantee that all references
2388 * to the previous incarnation of @dev have been dropped.) Allocate
2389 * and register a fresh new struct device instead.
2391 * NOTE: _Never_ directly free @dev after calling this function, even
2392 * if it returned an error! Always use put_device() to give up your
2393 * reference instead.
2395 * Rule of thumb is: if device_add() succeeds, you should call
2396 * device_del() when you want to get rid of it. If device_add() has
2397 * *not* succeeded, use *only* put_device() to drop the reference
2400 int device_add(struct device *dev)
2402 struct device *parent;
2403 struct kobject *kobj;
2404 struct class_interface *class_intf;
2405 int error = -EINVAL, fw_ret;
2406 struct kobject *glue_dir = NULL;
2407 bool is_fwnode_dev = false;
2409 dev = get_device(dev);
2414 error = device_private_init(dev);
2420 * for statically allocated devices, which should all be converted
2421 * some day, we need to initialize the name. We prevent reading back
2422 * the name, and force the use of dev_name()
2424 if (dev->init_name) {
2425 dev_set_name(dev, "%s", dev->init_name);
2426 dev->init_name = NULL;
2429 /* subsystems can specify simple device enumeration */
2430 if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
2431 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
2433 if (!dev_name(dev)) {
2438 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
2440 parent = get_device(dev->parent);
2441 kobj = get_device_parent(dev, parent);
2443 error = PTR_ERR(kobj);
2447 dev->kobj.parent = kobj;
2449 /* use parent numa_node */
2450 if (parent && (dev_to_node(dev) == NUMA_NO_NODE))
2451 set_dev_node(dev, dev_to_node(parent));
2453 /* first, register with generic layer. */
2454 /* we require the name to be set before, and pass NULL */
2455 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
2457 glue_dir = get_glue_dir(dev);
2461 /* notify platform of device entry */
2462 error = device_platform_notify(dev, KOBJ_ADD);
2464 goto platform_error;
2466 error = device_create_file(dev, &dev_attr_uevent);
2470 error = device_add_class_symlinks(dev);
2473 error = device_add_attrs(dev);
2476 error = bus_add_device(dev);
2479 error = dpm_sysfs_add(dev);
2484 if (MAJOR(dev->devt)) {
2485 error = device_create_file(dev, &dev_attr_dev);
2489 error = device_create_sys_dev_entry(dev);
2493 devtmpfs_create_node(dev);
2496 /* Notify clients of device addition. This call must come
2497 * after dpm_sysfs_add() and before kobject_uevent().
2500 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2501 BUS_NOTIFY_ADD_DEVICE, dev);
2503 kobject_uevent(&dev->kobj, KOBJ_ADD);
2505 if (dev->fwnode && !dev->fwnode->dev) {
2506 dev->fwnode->dev = dev;
2507 is_fwnode_dev = true;
2511 * Check if any of the other devices (consumers) have been waiting for
2512 * this device (supplier) to be added so that they can create a device
2515 * This needs to happen after device_pm_add() because device_link_add()
2516 * requires the supplier be registered before it's called.
2518 * But this also needs to happe before bus_probe_device() to make sure
2519 * waiting consumers can link to it before the driver is bound to the
2520 * device and the driver sync_state callback is called for this device.
2522 device_link_add_missing_supplier_links();
2524 if (fw_devlink_flags && is_fwnode_dev &&
2525 fwnode_has_op(dev->fwnode, add_links)) {
2526 fw_ret = fwnode_call_int_op(dev->fwnode, add_links, dev);
2527 if (fw_ret == -ENODEV)
2528 device_link_wait_for_mandatory_supplier(dev);
2530 device_link_wait_for_optional_supplier(dev);
2533 bus_probe_device(dev);
2535 klist_add_tail(&dev->p->knode_parent,
2536 &parent->p->klist_children);
2539 mutex_lock(&dev->class->p->mutex);
2540 /* tie the class to the device */
2541 klist_add_tail(&dev->p->knode_class,
2542 &dev->class->p->klist_devices);
2544 /* notify any interfaces that the device is here */
2545 list_for_each_entry(class_intf,
2546 &dev->class->p->interfaces, node)
2547 if (class_intf->add_dev)
2548 class_intf->add_dev(dev, class_intf);
2549 mutex_unlock(&dev->class->p->mutex);
2555 if (MAJOR(dev->devt))
2556 device_remove_file(dev, &dev_attr_dev);
2558 device_pm_remove(dev);
2559 dpm_sysfs_remove(dev);
2561 bus_remove_device(dev);
2563 device_remove_attrs(dev);
2565 device_remove_class_symlinks(dev);
2567 device_remove_file(dev, &dev_attr_uevent);
2569 device_platform_notify(dev, KOBJ_REMOVE);
2571 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
2572 glue_dir = get_glue_dir(dev);
2573 kobject_del(&dev->kobj);
2575 cleanup_glue_dir(dev, glue_dir);
2583 EXPORT_SYMBOL_GPL(device_add);
2586 * device_register - register a device with the system.
2587 * @dev: pointer to the device structure
2589 * This happens in two clean steps - initialize the device
2590 * and add it to the system. The two steps can be called
2591 * separately, but this is the easiest and most common.
2592 * I.e. you should only call the two helpers separately if
2593 * have a clearly defined need to use and refcount the device
2594 * before it is added to the hierarchy.
2596 * For more information, see the kerneldoc for device_initialize()
2599 * NOTE: _Never_ directly free @dev after calling this function, even
2600 * if it returned an error! Always use put_device() to give up the
2601 * reference initialized in this function instead.
2603 int device_register(struct device *dev)
2605 device_initialize(dev);
2606 return device_add(dev);
2608 EXPORT_SYMBOL_GPL(device_register);
2611 * get_device - increment reference count for device.
2614 * This simply forwards the call to kobject_get(), though
2615 * we do take care to provide for the case that we get a NULL
2616 * pointer passed in.
2618 struct device *get_device(struct device *dev)
2620 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
2622 EXPORT_SYMBOL_GPL(get_device);
2625 * put_device - decrement reference count.
2626 * @dev: device in question.
2628 void put_device(struct device *dev)
2630 /* might_sleep(); */
2632 kobject_put(&dev->kobj);
2634 EXPORT_SYMBOL_GPL(put_device);
2636 bool kill_device(struct device *dev)
2639 * Require the device lock and set the "dead" flag to guarantee that
2640 * the update behavior is consistent with the other bitfields near
2641 * it and that we cannot have an asynchronous probe routine trying
2642 * to run while we are tearing out the bus/class/sysfs from
2643 * underneath the device.
2645 lockdep_assert_held(&dev->mutex);
2649 dev->p->dead = true;
2652 EXPORT_SYMBOL_GPL(kill_device);
2655 * device_del - delete device from system.
2658 * This is the first part of the device unregistration
2659 * sequence. This removes the device from the lists we control
2660 * from here, has it removed from the other driver model
2661 * subsystems it was added to in device_add(), and removes it
2662 * from the kobject hierarchy.
2664 * NOTE: this should be called manually _iff_ device_add() was
2665 * also called manually.
2667 void device_del(struct device *dev)
2669 struct device *parent = dev->parent;
2670 struct kobject *glue_dir = NULL;
2671 struct class_interface *class_intf;
2677 if (dev->fwnode && dev->fwnode->dev == dev)
2678 dev->fwnode->dev = NULL;
2680 /* Notify clients of device removal. This call must come
2681 * before dpm_sysfs_remove().
2684 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2685 BUS_NOTIFY_DEL_DEVICE, dev);
2687 dpm_sysfs_remove(dev);
2689 klist_del(&dev->p->knode_parent);
2690 if (MAJOR(dev->devt)) {
2691 devtmpfs_delete_node(dev);
2692 device_remove_sys_dev_entry(dev);
2693 device_remove_file(dev, &dev_attr_dev);
2696 device_remove_class_symlinks(dev);
2698 mutex_lock(&dev->class->p->mutex);
2699 /* notify any interfaces that the device is now gone */
2700 list_for_each_entry(class_intf,
2701 &dev->class->p->interfaces, node)
2702 if (class_intf->remove_dev)
2703 class_intf->remove_dev(dev, class_intf);
2704 /* remove the device from the class list */
2705 klist_del(&dev->p->knode_class);
2706 mutex_unlock(&dev->class->p->mutex);
2708 device_remove_file(dev, &dev_attr_uevent);
2709 device_remove_attrs(dev);
2710 bus_remove_device(dev);
2711 device_pm_remove(dev);
2712 driver_deferred_probe_del(dev);
2713 device_platform_notify(dev, KOBJ_REMOVE);
2714 device_remove_properties(dev);
2715 device_links_purge(dev);
2718 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2719 BUS_NOTIFY_REMOVED_DEVICE, dev);
2720 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
2721 glue_dir = get_glue_dir(dev);
2722 kobject_del(&dev->kobj);
2723 cleanup_glue_dir(dev, glue_dir);
2726 EXPORT_SYMBOL_GPL(device_del);
2729 * device_unregister - unregister device from system.
2730 * @dev: device going away.
2732 * We do this in two parts, like we do device_register(). First,
2733 * we remove it from all the subsystems with device_del(), then
2734 * we decrement the reference count via put_device(). If that
2735 * is the final reference count, the device will be cleaned up
2736 * via device_release() above. Otherwise, the structure will
2737 * stick around until the final reference to the device is dropped.
2739 void device_unregister(struct device *dev)
2741 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
2745 EXPORT_SYMBOL_GPL(device_unregister);
2747 static struct device *prev_device(struct klist_iter *i)
2749 struct klist_node *n = klist_prev(i);
2750 struct device *dev = NULL;
2751 struct device_private *p;
2754 p = to_device_private_parent(n);
2760 static struct device *next_device(struct klist_iter *i)
2762 struct klist_node *n = klist_next(i);
2763 struct device *dev = NULL;
2764 struct device_private *p;
2767 p = to_device_private_parent(n);
2774 * device_get_devnode - path of device node file
2776 * @mode: returned file access mode
2777 * @uid: returned file owner
2778 * @gid: returned file group
2779 * @tmp: possibly allocated string
2781 * Return the relative path of a possible device node.
2782 * Non-default names may need to allocate a memory to compose
2783 * a name. This memory is returned in tmp and needs to be
2784 * freed by the caller.
2786 const char *device_get_devnode(struct device *dev,
2787 umode_t *mode, kuid_t *uid, kgid_t *gid,
2794 /* the device type may provide a specific name */
2795 if (dev->type && dev->type->devnode)
2796 *tmp = dev->type->devnode(dev, mode, uid, gid);
2800 /* the class may provide a specific name */
2801 if (dev->class && dev->class->devnode)
2802 *tmp = dev->class->devnode(dev, mode);
2806 /* return name without allocation, tmp == NULL */
2807 if (strchr(dev_name(dev), '!') == NULL)
2808 return dev_name(dev);
2810 /* replace '!' in the name with '/' */
2811 s = kstrdup(dev_name(dev), GFP_KERNEL);
2814 strreplace(s, '!', '/');
2819 * device_for_each_child - device child iterator.
2820 * @parent: parent struct device.
2821 * @fn: function to be called for each device.
2822 * @data: data for the callback.
2824 * Iterate over @parent's child devices, and call @fn for each,
2827 * We check the return of @fn each time. If it returns anything
2828 * other than 0, we break out and return that value.
2830 int device_for_each_child(struct device *parent, void *data,
2831 int (*fn)(struct device *dev, void *data))
2833 struct klist_iter i;
2834 struct device *child;
2840 klist_iter_init(&parent->p->klist_children, &i);
2841 while (!error && (child = next_device(&i)))
2842 error = fn(child, data);
2843 klist_iter_exit(&i);
2846 EXPORT_SYMBOL_GPL(device_for_each_child);
2849 * device_for_each_child_reverse - device child iterator in reversed order.
2850 * @parent: parent struct device.
2851 * @fn: function to be called for each device.
2852 * @data: data for the callback.
2854 * Iterate over @parent's child devices, and call @fn for each,
2857 * We check the return of @fn each time. If it returns anything
2858 * other than 0, we break out and return that value.
2860 int device_for_each_child_reverse(struct device *parent, void *data,
2861 int (*fn)(struct device *dev, void *data))
2863 struct klist_iter i;
2864 struct device *child;
2870 klist_iter_init(&parent->p->klist_children, &i);
2871 while ((child = prev_device(&i)) && !error)
2872 error = fn(child, data);
2873 klist_iter_exit(&i);
2876 EXPORT_SYMBOL_GPL(device_for_each_child_reverse);
2879 * device_find_child - device iterator for locating a particular device.
2880 * @parent: parent struct device
2881 * @match: Callback function to check device
2882 * @data: Data to pass to match function
2884 * This is similar to the device_for_each_child() function above, but it
2885 * returns a reference to a device that is 'found' for later use, as
2886 * determined by the @match callback.
2888 * The callback should return 0 if the device doesn't match and non-zero
2889 * if it does. If the callback returns non-zero and a reference to the
2890 * current device can be obtained, this function will return to the caller
2891 * and not iterate over any more devices.
2893 * NOTE: you will need to drop the reference with put_device() after use.
2895 struct device *device_find_child(struct device *parent, void *data,
2896 int (*match)(struct device *dev, void *data))
2898 struct klist_iter i;
2899 struct device *child;
2904 klist_iter_init(&parent->p->klist_children, &i);
2905 while ((child = next_device(&i)))
2906 if (match(child, data) && get_device(child))
2908 klist_iter_exit(&i);
2911 EXPORT_SYMBOL_GPL(device_find_child);
2914 * device_find_child_by_name - device iterator for locating a child device.
2915 * @parent: parent struct device
2916 * @name: name of the child device
2918 * This is similar to the device_find_child() function above, but it
2919 * returns a reference to a device that has the name @name.
2921 * NOTE: you will need to drop the reference with put_device() after use.
2923 struct device *device_find_child_by_name(struct device *parent,
2926 struct klist_iter i;
2927 struct device *child;
2932 klist_iter_init(&parent->p->klist_children, &i);
2933 while ((child = next_device(&i)))
2934 if (!strcmp(dev_name(child), name) && get_device(child))
2936 klist_iter_exit(&i);
2939 EXPORT_SYMBOL_GPL(device_find_child_by_name);
2941 int __init devices_init(void)
2943 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
2946 dev_kobj = kobject_create_and_add("dev", NULL);
2949 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
2950 if (!sysfs_dev_block_kobj)
2951 goto block_kobj_err;
2952 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
2953 if (!sysfs_dev_char_kobj)
2959 kobject_put(sysfs_dev_block_kobj);
2961 kobject_put(dev_kobj);
2963 kset_unregister(devices_kset);
2967 static int device_check_offline(struct device *dev, void *not_used)
2971 ret = device_for_each_child(dev, NULL, device_check_offline);
2975 return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
2979 * device_offline - Prepare the device for hot-removal.
2980 * @dev: Device to be put offline.
2982 * Execute the device bus type's .offline() callback, if present, to prepare
2983 * the device for a subsequent hot-removal. If that succeeds, the device must
2984 * not be used until either it is removed or its bus type's .online() callback
2987 * Call under device_hotplug_lock.
2989 int device_offline(struct device *dev)
2993 if (dev->offline_disabled)
2996 ret = device_for_each_child(dev, NULL, device_check_offline);
3001 if (device_supports_offline(dev)) {
3005 ret = dev->bus->offline(dev);
3007 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
3008 dev->offline = true;
3018 * device_online - Put the device back online after successful device_offline().
3019 * @dev: Device to be put back online.
3021 * If device_offline() has been successfully executed for @dev, but the device
3022 * has not been removed subsequently, execute its bus type's .online() callback
3023 * to indicate that the device can be used again.
3025 * Call under device_hotplug_lock.
3027 int device_online(struct device *dev)
3032 if (device_supports_offline(dev)) {
3034 ret = dev->bus->online(dev);
3036 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
3037 dev->offline = false;
3048 struct root_device {
3050 struct module *owner;
3053 static inline struct root_device *to_root_device(struct device *d)
3055 return container_of(d, struct root_device, dev);
3058 static void root_device_release(struct device *dev)
3060 kfree(to_root_device(dev));
3064 * __root_device_register - allocate and register a root device
3065 * @name: root device name
3066 * @owner: owner module of the root device, usually THIS_MODULE
3068 * This function allocates a root device and registers it
3069 * using device_register(). In order to free the returned
3070 * device, use root_device_unregister().
3072 * Root devices are dummy devices which allow other devices
3073 * to be grouped under /sys/devices. Use this function to
3074 * allocate a root device and then use it as the parent of
3075 * any device which should appear under /sys/devices/{name}
3077 * The /sys/devices/{name} directory will also contain a
3078 * 'module' symlink which points to the @owner directory
3081 * Returns &struct device pointer on success, or ERR_PTR() on error.
3083 * Note: You probably want to use root_device_register().
3085 struct device *__root_device_register(const char *name, struct module *owner)
3087 struct root_device *root;
3090 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
3092 return ERR_PTR(err);
3094 err = dev_set_name(&root->dev, "%s", name);
3097 return ERR_PTR(err);
3100 root->dev.release = root_device_release;
3102 err = device_register(&root->dev);
3104 put_device(&root->dev);
3105 return ERR_PTR(err);
3108 #ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
3110 struct module_kobject *mk = &owner->mkobj;
3112 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
3114 device_unregister(&root->dev);
3115 return ERR_PTR(err);
3117 root->owner = owner;
3123 EXPORT_SYMBOL_GPL(__root_device_register);
3126 * root_device_unregister - unregister and free a root device
3127 * @dev: device going away
3129 * This function unregisters and cleans up a device that was created by
3130 * root_device_register().
3132 void root_device_unregister(struct device *dev)
3134 struct root_device *root = to_root_device(dev);
3137 sysfs_remove_link(&root->dev.kobj, "module");
3139 device_unregister(dev);
3141 EXPORT_SYMBOL_GPL(root_device_unregister);
3144 static void device_create_release(struct device *dev)
3146 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
3150 static __printf(6, 0) struct device *
3151 device_create_groups_vargs(struct class *class, struct device *parent,
3152 dev_t devt, void *drvdata,
3153 const struct attribute_group **groups,
3154 const char *fmt, va_list args)
3156 struct device *dev = NULL;
3157 int retval = -ENODEV;
3159 if (class == NULL || IS_ERR(class))
3162 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
3168 device_initialize(dev);
3171 dev->parent = parent;
3172 dev->groups = groups;
3173 dev->release = device_create_release;
3174 dev_set_drvdata(dev, drvdata);
3176 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
3180 retval = device_add(dev);
3188 return ERR_PTR(retval);
3192 * device_create_vargs - creates a device and registers it with sysfs
3193 * @class: pointer to the struct class that this device should be registered to
3194 * @parent: pointer to the parent struct device of this new device, if any
3195 * @devt: the dev_t for the char device to be added
3196 * @drvdata: the data to be added to the device for callbacks
3197 * @fmt: string for the device's name
3198 * @args: va_list for the device's name
3200 * This function can be used by char device classes. A struct device
3201 * will be created in sysfs, registered to the specified class.
3203 * A "dev" file will be created, showing the dev_t for the device, if
3204 * the dev_t is not 0,0.
3205 * If a pointer to a parent struct device is passed in, the newly created
3206 * struct device will be a child of that device in sysfs.
3207 * The pointer to the struct device will be returned from the call.
3208 * Any further sysfs files that might be required can be created using this
3211 * Returns &struct device pointer on success, or ERR_PTR() on error.
3213 * Note: the struct class passed to this function must have previously
3214 * been created with a call to class_create().
3216 struct device *device_create_vargs(struct class *class, struct device *parent,
3217 dev_t devt, void *drvdata, const char *fmt,
3220 return device_create_groups_vargs(class, parent, devt, drvdata, NULL,
3223 EXPORT_SYMBOL_GPL(device_create_vargs);
3226 * device_create - creates a device and registers it with sysfs
3227 * @class: pointer to the struct class that this device should be registered to
3228 * @parent: pointer to the parent struct device of this new device, if any
3229 * @devt: the dev_t for the char device to be added
3230 * @drvdata: the data to be added to the device for callbacks
3231 * @fmt: string for the device's name
3233 * This function can be used by char device classes. A struct device
3234 * will be created in sysfs, registered to the specified class.
3236 * A "dev" file will be created, showing the dev_t for the device, if
3237 * the dev_t is not 0,0.
3238 * If a pointer to a parent struct device is passed in, the newly created
3239 * struct device will be a child of that device in sysfs.
3240 * The pointer to the struct device will be returned from the call.
3241 * Any further sysfs files that might be required can be created using this
3244 * Returns &struct device pointer on success, or ERR_PTR() on error.
3246 * Note: the struct class passed to this function must have previously
3247 * been created with a call to class_create().
3249 struct device *device_create(struct class *class, struct device *parent,
3250 dev_t devt, void *drvdata, const char *fmt, ...)
3255 va_start(vargs, fmt);
3256 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
3260 EXPORT_SYMBOL_GPL(device_create);
3263 * device_create_with_groups - creates a device and registers it with sysfs
3264 * @class: pointer to the struct class that this device should be registered to
3265 * @parent: pointer to the parent struct device of this new device, if any
3266 * @devt: the dev_t for the char device to be added
3267 * @drvdata: the data to be added to the device for callbacks
3268 * @groups: NULL-terminated list of attribute groups to be created
3269 * @fmt: string for the device's name
3271 * This function can be used by char device classes. A struct device
3272 * will be created in sysfs, registered to the specified class.
3273 * Additional attributes specified in the groups parameter will also
3274 * be created automatically.
3276 * A "dev" file will be created, showing the dev_t for the device, if
3277 * the dev_t is not 0,0.
3278 * If a pointer to a parent struct device is passed in, the newly created
3279 * struct device will be a child of that device in sysfs.
3280 * The pointer to the struct device will be returned from the call.
3281 * Any further sysfs files that might be required can be created using this
3284 * Returns &struct device pointer on success, or ERR_PTR() on error.
3286 * Note: the struct class passed to this function must have previously
3287 * been created with a call to class_create().
3289 struct device *device_create_with_groups(struct class *class,
3290 struct device *parent, dev_t devt,
3292 const struct attribute_group **groups,
3293 const char *fmt, ...)
3298 va_start(vargs, fmt);
3299 dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
3304 EXPORT_SYMBOL_GPL(device_create_with_groups);
3307 * device_destroy - removes a device that was created with device_create()
3308 * @class: pointer to the struct class that this device was registered with
3309 * @devt: the dev_t of the device that was previously registered
3311 * This call unregisters and cleans up a device that was created with a
3312 * call to device_create().
3314 void device_destroy(struct class *class, dev_t devt)
3318 dev = class_find_device_by_devt(class, devt);
3321 device_unregister(dev);
3324 EXPORT_SYMBOL_GPL(device_destroy);
3327 * device_rename - renames a device
3328 * @dev: the pointer to the struct device to be renamed
3329 * @new_name: the new name of the device
3331 * It is the responsibility of the caller to provide mutual
3332 * exclusion between two different calls of device_rename
3333 * on the same device to ensure that new_name is valid and
3334 * won't conflict with other devices.
3336 * Note: Don't call this function. Currently, the networking layer calls this
3337 * function, but that will change. The following text from Kay Sievers offers
3340 * Renaming devices is racy at many levels, symlinks and other stuff are not
3341 * replaced atomically, and you get a "move" uevent, but it's not easy to
3342 * connect the event to the old and new device. Device nodes are not renamed at
3343 * all, there isn't even support for that in the kernel now.
3345 * In the meantime, during renaming, your target name might be taken by another
3346 * driver, creating conflicts. Or the old name is taken directly after you
3347 * renamed it -- then you get events for the same DEVPATH, before you even see
3348 * the "move" event. It's just a mess, and nothing new should ever rely on
3349 * kernel device renaming. Besides that, it's not even implemented now for
3350 * other things than (driver-core wise very simple) network devices.
3352 * We are currently about to change network renaming in udev to completely
3353 * disallow renaming of devices in the same namespace as the kernel uses,
3354 * because we can't solve the problems properly, that arise with swapping names
3355 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
3356 * be allowed to some other name than eth[0-9]*, for the aforementioned
3359 * Make up a "real" name in the driver before you register anything, or add
3360 * some other attributes for userspace to find the device, or use udev to add
3361 * symlinks -- but never rename kernel devices later, it's a complete mess. We
3362 * don't even want to get into that and try to implement the missing pieces in
3363 * the core. We really have other pieces to fix in the driver core mess. :)
3365 int device_rename(struct device *dev, const char *new_name)
3367 struct kobject *kobj = &dev->kobj;
3368 char *old_device_name = NULL;
3371 dev = get_device(dev);
3375 dev_dbg(dev, "renaming to %s\n", new_name);
3377 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
3378 if (!old_device_name) {
3384 error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj,
3385 kobj, old_device_name,
3386 new_name, kobject_namespace(kobj));
3391 error = kobject_rename(kobj, new_name);
3398 kfree(old_device_name);
3402 EXPORT_SYMBOL_GPL(device_rename);
3404 static int device_move_class_links(struct device *dev,
3405 struct device *old_parent,
3406 struct device *new_parent)
3411 sysfs_remove_link(&dev->kobj, "device");
3413 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
3419 * device_move - moves a device to a new parent
3420 * @dev: the pointer to the struct device to be moved
3421 * @new_parent: the new parent of the device (can be NULL)
3422 * @dpm_order: how to reorder the dpm_list
3424 int device_move(struct device *dev, struct device *new_parent,
3425 enum dpm_order dpm_order)
3428 struct device *old_parent;
3429 struct kobject *new_parent_kobj;
3431 dev = get_device(dev);
3436 new_parent = get_device(new_parent);
3437 new_parent_kobj = get_device_parent(dev, new_parent);
3438 if (IS_ERR(new_parent_kobj)) {
3439 error = PTR_ERR(new_parent_kobj);
3440 put_device(new_parent);
3444 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
3445 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
3446 error = kobject_move(&dev->kobj, new_parent_kobj);
3448 cleanup_glue_dir(dev, new_parent_kobj);
3449 put_device(new_parent);
3452 old_parent = dev->parent;
3453 dev->parent = new_parent;
3455 klist_remove(&dev->p->knode_parent);
3457 klist_add_tail(&dev->p->knode_parent,
3458 &new_parent->p->klist_children);
3459 set_dev_node(dev, dev_to_node(new_parent));
3463 error = device_move_class_links(dev, old_parent, new_parent);
3465 /* We ignore errors on cleanup since we're hosed anyway... */
3466 device_move_class_links(dev, new_parent, old_parent);
3467 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
3469 klist_remove(&dev->p->knode_parent);
3470 dev->parent = old_parent;
3472 klist_add_tail(&dev->p->knode_parent,
3473 &old_parent->p->klist_children);
3474 set_dev_node(dev, dev_to_node(old_parent));
3477 cleanup_glue_dir(dev, new_parent_kobj);
3478 put_device(new_parent);
3482 switch (dpm_order) {
3483 case DPM_ORDER_NONE:
3485 case DPM_ORDER_DEV_AFTER_PARENT:
3486 device_pm_move_after(dev, new_parent);
3487 devices_kset_move_after(dev, new_parent);
3489 case DPM_ORDER_PARENT_BEFORE_DEV:
3490 device_pm_move_before(new_parent, dev);
3491 devices_kset_move_before(new_parent, dev);
3493 case DPM_ORDER_DEV_LAST:
3494 device_pm_move_last(dev);
3495 devices_kset_move_last(dev);
3499 put_device(old_parent);
3505 EXPORT_SYMBOL_GPL(device_move);
3507 static int device_attrs_change_owner(struct device *dev, kuid_t kuid,
3510 struct kobject *kobj = &dev->kobj;
3511 struct class *class = dev->class;
3512 const struct device_type *type = dev->type;
3517 * Change the device groups of the device class for @dev to
3520 error = sysfs_groups_change_owner(kobj, class->dev_groups, kuid,
3528 * Change the device groups of the device type for @dev to
3531 error = sysfs_groups_change_owner(kobj, type->groups, kuid,
3537 /* Change the device groups of @dev to @kuid/@kgid. */
3538 error = sysfs_groups_change_owner(kobj, dev->groups, kuid, kgid);
3542 if (device_supports_offline(dev) && !dev->offline_disabled) {
3543 /* Change online device attributes of @dev to @kuid/@kgid. */
3544 error = sysfs_file_change_owner(kobj, dev_attr_online.attr.name,
3554 * device_change_owner - change the owner of an existing device.
3556 * @kuid: new owner's kuid
3557 * @kgid: new owner's kgid
3559 * This changes the owner of @dev and its corresponding sysfs entries to
3560 * @kuid/@kgid. This function closely mirrors how @dev was added via driver
3563 * Returns 0 on success or error code on failure.
3565 int device_change_owner(struct device *dev, kuid_t kuid, kgid_t kgid)
3568 struct kobject *kobj = &dev->kobj;
3570 dev = get_device(dev);
3575 * Change the kobject and the default attributes and groups of the
3576 * ktype associated with it to @kuid/@kgid.
3578 error = sysfs_change_owner(kobj, kuid, kgid);
3583 * Change the uevent file for @dev to the new owner. The uevent file
3584 * was created in a separate step when @dev got added and we mirror
3587 error = sysfs_file_change_owner(kobj, dev_attr_uevent.attr.name, kuid,
3593 * Change the device groups, the device groups associated with the
3594 * device class, and the groups associated with the device type of @dev
3597 error = device_attrs_change_owner(dev, kuid, kgid);
3601 error = dpm_sysfs_change_owner(dev, kuid, kgid);
3606 if (sysfs_deprecated && dev->class == &block_class)
3611 * Change the owner of the symlink located in the class directory of
3612 * the device class associated with @dev which points to the actual
3613 * directory entry for @dev to @kuid/@kgid. This ensures that the
3614 * symlink shows the same permissions as its target.
3616 error = sysfs_link_change_owner(&dev->class->p->subsys.kobj, &dev->kobj,
3617 dev_name(dev), kuid, kgid);
3625 EXPORT_SYMBOL_GPL(device_change_owner);
3628 * device_shutdown - call ->shutdown() on each device to shutdown.
3630 void device_shutdown(void)
3632 struct device *dev, *parent;
3634 wait_for_device_probe();
3635 device_block_probing();
3639 spin_lock(&devices_kset->list_lock);
3641 * Walk the devices list backward, shutting down each in turn.
3642 * Beware that device unplug events may also start pulling
3643 * devices offline, even as the system is shutting down.
3645 while (!list_empty(&devices_kset->list)) {
3646 dev = list_entry(devices_kset->list.prev, struct device,
3650 * hold reference count of device's parent to
3651 * prevent it from being freed because parent's
3652 * lock is to be held
3654 parent = get_device(dev->parent);
3657 * Make sure the device is off the kset list, in the
3658 * event that dev->*->shutdown() doesn't remove it.
3660 list_del_init(&dev->kobj.entry);
3661 spin_unlock(&devices_kset->list_lock);
3663 /* hold lock to avoid race with probe/release */
3665 device_lock(parent);
3668 /* Don't allow any more runtime suspends */
3669 pm_runtime_get_noresume(dev);
3670 pm_runtime_barrier(dev);
3672 if (dev->class && dev->class->shutdown_pre) {
3674 dev_info(dev, "shutdown_pre\n");
3675 dev->class->shutdown_pre(dev);
3677 if (dev->bus && dev->bus->shutdown) {
3679 dev_info(dev, "shutdown\n");
3680 dev->bus->shutdown(dev);
3681 } else if (dev->driver && dev->driver->shutdown) {
3683 dev_info(dev, "shutdown\n");
3684 dev->driver->shutdown(dev);
3689 device_unlock(parent);
3694 spin_lock(&devices_kset->list_lock);
3696 spin_unlock(&devices_kset->list_lock);
3700 * Device logging functions
3703 #ifdef CONFIG_PRINTK
3705 create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
3711 subsys = dev->class->name;
3713 subsys = dev->bus->name;
3717 pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys);
3722 * Add device identifier DEVICE=:
3726 * +sound:card0 subsystem:devname
3728 if (MAJOR(dev->devt)) {
3731 if (strcmp(subsys, "block") == 0)
3736 pos += snprintf(hdr + pos, hdrlen - pos,
3738 c, MAJOR(dev->devt), MINOR(dev->devt));
3739 } else if (strcmp(subsys, "net") == 0) {
3740 struct net_device *net = to_net_dev(dev);
3743 pos += snprintf(hdr + pos, hdrlen - pos,
3744 "DEVICE=n%u", net->ifindex);
3747 pos += snprintf(hdr + pos, hdrlen - pos,
3748 "DEVICE=+%s:%s", subsys, dev_name(dev));
3757 dev_WARN(dev, "device/subsystem name too long");
3761 int dev_vprintk_emit(int level, const struct device *dev,
3762 const char *fmt, va_list args)
3767 hdrlen = create_syslog_header(dev, hdr, sizeof(hdr));
3769 return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args);
3771 EXPORT_SYMBOL(dev_vprintk_emit);
3773 int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
3778 va_start(args, fmt);
3780 r = dev_vprintk_emit(level, dev, fmt, args);
3786 EXPORT_SYMBOL(dev_printk_emit);
3788 static void __dev_printk(const char *level, const struct device *dev,
3789 struct va_format *vaf)
3792 dev_printk_emit(level[1] - '0', dev, "%s %s: %pV",
3793 dev_driver_string(dev), dev_name(dev), vaf);
3795 printk("%s(NULL device *): %pV", level, vaf);
3798 void dev_printk(const char *level, const struct device *dev,
3799 const char *fmt, ...)
3801 struct va_format vaf;
3804 va_start(args, fmt);
3809 __dev_printk(level, dev, &vaf);
3813 EXPORT_SYMBOL(dev_printk);
3815 #define define_dev_printk_level(func, kern_level) \
3816 void func(const struct device *dev, const char *fmt, ...) \
3818 struct va_format vaf; \
3821 va_start(args, fmt); \
3826 __dev_printk(kern_level, dev, &vaf); \
3830 EXPORT_SYMBOL(func);
3832 define_dev_printk_level(_dev_emerg, KERN_EMERG);
3833 define_dev_printk_level(_dev_alert, KERN_ALERT);
3834 define_dev_printk_level(_dev_crit, KERN_CRIT);
3835 define_dev_printk_level(_dev_err, KERN_ERR);
3836 define_dev_printk_level(_dev_warn, KERN_WARNING);
3837 define_dev_printk_level(_dev_notice, KERN_NOTICE);
3838 define_dev_printk_level(_dev_info, KERN_INFO);
3842 static inline bool fwnode_is_primary(struct fwnode_handle *fwnode)
3844 return fwnode && !IS_ERR(fwnode->secondary);
3848 * set_primary_fwnode - Change the primary firmware node of a given device.
3849 * @dev: Device to handle.
3850 * @fwnode: New primary firmware node of the device.
3852 * Set the device's firmware node pointer to @fwnode, but if a secondary
3853 * firmware node of the device is present, preserve it.
3855 void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
3858 struct fwnode_handle *fn = dev->fwnode;
3860 if (fwnode_is_primary(fn))
3864 WARN_ON(fwnode->secondary);
3865 fwnode->secondary = fn;
3867 dev->fwnode = fwnode;
3869 dev->fwnode = fwnode_is_primary(dev->fwnode) ?
3870 dev->fwnode->secondary : NULL;
3873 EXPORT_SYMBOL_GPL(set_primary_fwnode);
3876 * set_secondary_fwnode - Change the secondary firmware node of a given device.
3877 * @dev: Device to handle.
3878 * @fwnode: New secondary firmware node of the device.
3880 * If a primary firmware node of the device is present, set its secondary
3881 * pointer to @fwnode. Otherwise, set the device's firmware node pointer to
3884 void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
3887 fwnode->secondary = ERR_PTR(-ENODEV);
3889 if (fwnode_is_primary(dev->fwnode))
3890 dev->fwnode->secondary = fwnode;
3892 dev->fwnode = fwnode;
3896 * device_set_of_node_from_dev - reuse device-tree node of another device
3897 * @dev: device whose device-tree node is being set
3898 * @dev2: device whose device-tree node is being reused
3900 * Takes another reference to the new device-tree node after first dropping
3901 * any reference held to the old node.
3903 void device_set_of_node_from_dev(struct device *dev, const struct device *dev2)
3905 of_node_put(dev->of_node);
3906 dev->of_node = of_node_get(dev2->of_node);
3907 dev->of_node_reused = true;
3909 EXPORT_SYMBOL_GPL(device_set_of_node_from_dev);
3911 int device_match_name(struct device *dev, const void *name)
3913 return sysfs_streq(dev_name(dev), name);
3915 EXPORT_SYMBOL_GPL(device_match_name);
3917 int device_match_of_node(struct device *dev, const void *np)
3919 return dev->of_node == np;
3921 EXPORT_SYMBOL_GPL(device_match_of_node);
3923 int device_match_fwnode(struct device *dev, const void *fwnode)
3925 return dev_fwnode(dev) == fwnode;
3927 EXPORT_SYMBOL_GPL(device_match_fwnode);
3929 int device_match_devt(struct device *dev, const void *pdevt)
3931 return dev->devt == *(dev_t *)pdevt;
3933 EXPORT_SYMBOL_GPL(device_match_devt);
3935 int device_match_acpi_dev(struct device *dev, const void *adev)
3937 return ACPI_COMPANION(dev) == adev;
3939 EXPORT_SYMBOL(device_match_acpi_dev);
3941 int device_match_any(struct device *dev, const void *unused)
3945 EXPORT_SYMBOL_GPL(device_match_any);