1 // SPDX-License-Identifier: GPL-2.0
3 * drivers/base/core.c - core driver model code (device registration, etc)
5 * Copyright (c) 2002-3 Patrick Mochel
6 * Copyright (c) 2002-3 Open Source Development Labs
7 * Copyright (c) 2006 Greg Kroah-Hartman <gregkh@suse.de>
8 * Copyright (c) 2006 Novell, Inc.
11 #include <linux/acpi.h>
12 #include <linux/cpufreq.h>
13 #include <linux/device.h>
14 #include <linux/err.h>
15 #include <linux/fwnode.h>
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/string.h>
20 #include <linux/kdev_t.h>
21 #include <linux/notifier.h>
23 #include <linux/of_device.h>
24 #include <linux/genhd.h>
25 #include <linux/mutex.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/netdevice.h>
28 #include <linux/sched/signal.h>
29 #include <linux/sysfs.h>
32 #include "power/power.h"
34 #ifdef CONFIG_SYSFS_DEPRECATED
35 #ifdef CONFIG_SYSFS_DEPRECATED_V2
36 long sysfs_deprecated = 1;
38 long sysfs_deprecated = 0;
40 static int __init sysfs_deprecated_setup(char *arg)
42 return kstrtol(arg, 10, &sysfs_deprecated);
44 early_param("sysfs.deprecated", sysfs_deprecated_setup);
47 /* Device links support. */
48 static LIST_HEAD(wait_for_suppliers);
49 static DEFINE_MUTEX(wfs_lock);
50 static LIST_HEAD(deferred_sync);
51 static unsigned int defer_sync_state_count = 1;
52 static unsigned int defer_fw_devlink_count;
53 static DEFINE_MUTEX(defer_fw_devlink_lock);
54 static bool fw_devlink_is_permissive(void);
57 static DEFINE_MUTEX(device_links_lock);
58 DEFINE_STATIC_SRCU(device_links_srcu);
60 static inline void device_links_write_lock(void)
62 mutex_lock(&device_links_lock);
65 static inline void device_links_write_unlock(void)
67 mutex_unlock(&device_links_lock);
70 int device_links_read_lock(void) __acquires(&device_links_srcu)
72 return srcu_read_lock(&device_links_srcu);
75 void device_links_read_unlock(int idx) __releases(&device_links_srcu)
77 srcu_read_unlock(&device_links_srcu, idx);
80 int device_links_read_lock_held(void)
82 return srcu_read_lock_held(&device_links_srcu);
84 #else /* !CONFIG_SRCU */
85 static DECLARE_RWSEM(device_links_lock);
87 static inline void device_links_write_lock(void)
89 down_write(&device_links_lock);
92 static inline void device_links_write_unlock(void)
94 up_write(&device_links_lock);
97 int device_links_read_lock(void)
99 down_read(&device_links_lock);
103 void device_links_read_unlock(int not_used)
105 up_read(&device_links_lock);
108 #ifdef CONFIG_DEBUG_LOCK_ALLOC
109 int device_links_read_lock_held(void)
111 return lockdep_is_held(&device_links_lock);
114 #endif /* !CONFIG_SRCU */
117 * device_is_dependent - Check if one device depends on another one
118 * @dev: Device to check dependencies for.
119 * @target: Device to check against.
121 * Check if @target depends on @dev or any device dependent on it (its child or
122 * its consumer etc). Return 1 if that is the case or 0 otherwise.
124 static int device_is_dependent(struct device *dev, void *target)
126 struct device_link *link;
132 ret = device_for_each_child(dev, target, device_is_dependent);
136 list_for_each_entry(link, &dev->links.consumers, s_node) {
137 if (link->flags == (DL_FLAG_SYNC_STATE_ONLY | DL_FLAG_MANAGED))
140 if (link->consumer == target)
143 ret = device_is_dependent(link->consumer, target);
150 static void device_link_init_status(struct device_link *link,
151 struct device *consumer,
152 struct device *supplier)
154 switch (supplier->links.status) {
156 switch (consumer->links.status) {
159 * A consumer driver can create a link to a supplier
160 * that has not completed its probing yet as long as it
161 * knows that the supplier is already functional (for
162 * example, it has just acquired some resources from the
165 link->status = DL_STATE_CONSUMER_PROBE;
168 link->status = DL_STATE_DORMANT;
172 case DL_DEV_DRIVER_BOUND:
173 switch (consumer->links.status) {
175 link->status = DL_STATE_CONSUMER_PROBE;
177 case DL_DEV_DRIVER_BOUND:
178 link->status = DL_STATE_ACTIVE;
181 link->status = DL_STATE_AVAILABLE;
185 case DL_DEV_UNBINDING:
186 link->status = DL_STATE_SUPPLIER_UNBIND;
189 link->status = DL_STATE_DORMANT;
194 static int device_reorder_to_tail(struct device *dev, void *not_used)
196 struct device_link *link;
199 * Devices that have not been registered yet will be put to the ends
200 * of the lists during the registration, so skip them here.
202 if (device_is_registered(dev))
203 devices_kset_move_last(dev);
205 if (device_pm_initialized(dev))
206 device_pm_move_last(dev);
208 device_for_each_child(dev, NULL, device_reorder_to_tail);
209 list_for_each_entry(link, &dev->links.consumers, s_node) {
210 if (link->flags == (DL_FLAG_SYNC_STATE_ONLY | DL_FLAG_MANAGED))
212 device_reorder_to_tail(link->consumer, NULL);
219 * device_pm_move_to_tail - Move set of devices to the end of device lists
220 * @dev: Device to move
222 * This is a device_reorder_to_tail() wrapper taking the requisite locks.
224 * It moves the @dev along with all of its children and all of its consumers
225 * to the ends of the device_kset and dpm_list, recursively.
227 void device_pm_move_to_tail(struct device *dev)
231 idx = device_links_read_lock();
233 device_reorder_to_tail(dev, NULL);
235 device_links_read_unlock(idx);
238 #define DL_MANAGED_LINK_FLAGS (DL_FLAG_AUTOREMOVE_CONSUMER | \
239 DL_FLAG_AUTOREMOVE_SUPPLIER | \
240 DL_FLAG_AUTOPROBE_CONSUMER | \
241 DL_FLAG_SYNC_STATE_ONLY)
243 #define DL_ADD_VALID_FLAGS (DL_MANAGED_LINK_FLAGS | DL_FLAG_STATELESS | \
244 DL_FLAG_PM_RUNTIME | DL_FLAG_RPM_ACTIVE)
247 * device_link_add - Create a link between two devices.
248 * @consumer: Consumer end of the link.
249 * @supplier: Supplier end of the link.
250 * @flags: Link flags.
252 * The caller is responsible for the proper synchronization of the link creation
253 * with runtime PM. First, setting the DL_FLAG_PM_RUNTIME flag will cause the
254 * runtime PM framework to take the link into account. Second, if the
255 * DL_FLAG_RPM_ACTIVE flag is set in addition to it, the supplier devices will
256 * be forced into the active metastate and reference-counted upon the creation
257 * of the link. If DL_FLAG_PM_RUNTIME is not set, DL_FLAG_RPM_ACTIVE will be
260 * If DL_FLAG_STATELESS is set in @flags, the caller of this function is
261 * expected to release the link returned by it directly with the help of either
262 * device_link_del() or device_link_remove().
264 * If that flag is not set, however, the caller of this function is handing the
265 * management of the link over to the driver core entirely and its return value
266 * can only be used to check whether or not the link is present. In that case,
267 * the DL_FLAG_AUTOREMOVE_CONSUMER and DL_FLAG_AUTOREMOVE_SUPPLIER device link
268 * flags can be used to indicate to the driver core when the link can be safely
269 * deleted. Namely, setting one of them in @flags indicates to the driver core
270 * that the link is not going to be used (by the given caller of this function)
271 * after unbinding the consumer or supplier driver, respectively, from its
272 * device, so the link can be deleted at that point. If none of them is set,
273 * the link will be maintained until one of the devices pointed to by it (either
274 * the consumer or the supplier) is unregistered.
276 * Also, if DL_FLAG_STATELESS, DL_FLAG_AUTOREMOVE_CONSUMER and
277 * DL_FLAG_AUTOREMOVE_SUPPLIER are not set in @flags (that is, a persistent
278 * managed device link is being added), the DL_FLAG_AUTOPROBE_CONSUMER flag can
279 * be used to request the driver core to automaticall probe for a consmer
280 * driver after successfully binding a driver to the supplier device.
282 * The combination of DL_FLAG_STATELESS and one of DL_FLAG_AUTOREMOVE_CONSUMER,
283 * DL_FLAG_AUTOREMOVE_SUPPLIER, or DL_FLAG_AUTOPROBE_CONSUMER set in @flags at
284 * the same time is invalid and will cause NULL to be returned upfront.
285 * However, if a device link between the given @consumer and @supplier pair
286 * exists already when this function is called for them, the existing link will
287 * be returned regardless of its current type and status (the link's flags may
288 * be modified then). The caller of this function is then expected to treat
289 * the link as though it has just been created, so (in particular) if
290 * DL_FLAG_STATELESS was passed in @flags, the link needs to be released
291 * explicitly when not needed any more (as stated above).
293 * A side effect of the link creation is re-ordering of dpm_list and the
294 * devices_kset list by moving the consumer device and all devices depending
295 * on it to the ends of these lists (that does not happen to devices that have
296 * not been registered when this function is called).
298 * The supplier device is required to be registered when this function is called
299 * and NULL will be returned if that is not the case. The consumer device need
300 * not be registered, however.
302 struct device_link *device_link_add(struct device *consumer,
303 struct device *supplier, u32 flags)
305 struct device_link *link;
307 if (!consumer || !supplier || flags & ~DL_ADD_VALID_FLAGS ||
308 (flags & DL_FLAG_STATELESS && flags & DL_MANAGED_LINK_FLAGS) ||
309 (flags & DL_FLAG_SYNC_STATE_ONLY &&
310 flags != DL_FLAG_SYNC_STATE_ONLY) ||
311 (flags & DL_FLAG_AUTOPROBE_CONSUMER &&
312 flags & (DL_FLAG_AUTOREMOVE_CONSUMER |
313 DL_FLAG_AUTOREMOVE_SUPPLIER)))
316 if (flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) {
317 if (pm_runtime_get_sync(supplier) < 0) {
318 pm_runtime_put_noidle(supplier);
323 if (!(flags & DL_FLAG_STATELESS))
324 flags |= DL_FLAG_MANAGED;
326 device_links_write_lock();
330 * If the supplier has not been fully registered yet or there is a
331 * reverse (non-SYNC_STATE_ONLY) dependency between the consumer and
332 * the supplier already in the graph, return NULL. If the link is a
333 * SYNC_STATE_ONLY link, we don't check for reverse dependencies
334 * because it only affects sync_state() callbacks.
336 if (!device_pm_initialized(supplier)
337 || (!(flags & DL_FLAG_SYNC_STATE_ONLY) &&
338 device_is_dependent(consumer, supplier))) {
344 * DL_FLAG_AUTOREMOVE_SUPPLIER indicates that the link will be needed
345 * longer than for DL_FLAG_AUTOREMOVE_CONSUMER and setting them both
346 * together doesn't make sense, so prefer DL_FLAG_AUTOREMOVE_SUPPLIER.
348 if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
349 flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER;
351 list_for_each_entry(link, &supplier->links.consumers, s_node) {
352 if (link->consumer != consumer)
355 if (flags & DL_FLAG_PM_RUNTIME) {
356 if (!(link->flags & DL_FLAG_PM_RUNTIME)) {
357 pm_runtime_new_link(consumer);
358 link->flags |= DL_FLAG_PM_RUNTIME;
360 if (flags & DL_FLAG_RPM_ACTIVE)
361 refcount_inc(&link->rpm_active);
364 if (flags & DL_FLAG_STATELESS) {
365 kref_get(&link->kref);
366 if (link->flags & DL_FLAG_SYNC_STATE_ONLY &&
367 !(link->flags & DL_FLAG_STATELESS)) {
368 link->flags |= DL_FLAG_STATELESS;
371 link->flags |= DL_FLAG_STATELESS;
377 * If the life time of the link following from the new flags is
378 * longer than indicated by the flags of the existing link,
379 * update the existing link to stay around longer.
381 if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER) {
382 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER) {
383 link->flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER;
384 link->flags |= DL_FLAG_AUTOREMOVE_SUPPLIER;
386 } else if (!(flags & DL_FLAG_AUTOREMOVE_CONSUMER)) {
387 link->flags &= ~(DL_FLAG_AUTOREMOVE_CONSUMER |
388 DL_FLAG_AUTOREMOVE_SUPPLIER);
390 if (!(link->flags & DL_FLAG_MANAGED)) {
391 kref_get(&link->kref);
392 link->flags |= DL_FLAG_MANAGED;
393 device_link_init_status(link, consumer, supplier);
395 if (link->flags & DL_FLAG_SYNC_STATE_ONLY &&
396 !(flags & DL_FLAG_SYNC_STATE_ONLY)) {
397 link->flags &= ~DL_FLAG_SYNC_STATE_ONLY;
404 link = kzalloc(sizeof(*link), GFP_KERNEL);
408 refcount_set(&link->rpm_active, 1);
410 if (flags & DL_FLAG_PM_RUNTIME) {
411 if (flags & DL_FLAG_RPM_ACTIVE)
412 refcount_inc(&link->rpm_active);
414 pm_runtime_new_link(consumer);
417 get_device(supplier);
418 link->supplier = supplier;
419 INIT_LIST_HEAD(&link->s_node);
420 get_device(consumer);
421 link->consumer = consumer;
422 INIT_LIST_HEAD(&link->c_node);
424 kref_init(&link->kref);
426 /* Determine the initial link state. */
427 if (flags & DL_FLAG_STATELESS)
428 link->status = DL_STATE_NONE;
430 device_link_init_status(link, consumer, supplier);
433 * Some callers expect the link creation during consumer driver probe to
434 * resume the supplier even without DL_FLAG_RPM_ACTIVE.
436 if (link->status == DL_STATE_CONSUMER_PROBE &&
437 flags & DL_FLAG_PM_RUNTIME)
438 pm_runtime_resume(supplier);
440 list_add_tail_rcu(&link->s_node, &supplier->links.consumers);
441 list_add_tail_rcu(&link->c_node, &consumer->links.suppliers);
443 if (flags & DL_FLAG_SYNC_STATE_ONLY) {
445 "Linked as a sync state only consumer to %s\n",
452 * Move the consumer and all of the devices depending on it to the end
453 * of dpm_list and the devices_kset list.
455 * It is necessary to hold dpm_list locked throughout all that or else
456 * we may end up suspending with a wrong ordering of it.
458 device_reorder_to_tail(consumer, NULL);
460 dev_dbg(consumer, "Linked as a consumer to %s\n", dev_name(supplier));
464 device_links_write_unlock();
466 if ((flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) && !link)
467 pm_runtime_put(supplier);
471 EXPORT_SYMBOL_GPL(device_link_add);
474 * device_link_wait_for_supplier - Add device to wait_for_suppliers list
475 * @consumer: Consumer device
477 * Marks the @consumer device as waiting for suppliers to become available by
478 * adding it to the wait_for_suppliers list. The consumer device will never be
479 * probed until it's removed from the wait_for_suppliers list.
481 * The caller is responsible for adding the links to the supplier devices once
482 * they are available and removing the @consumer device from the
483 * wait_for_suppliers list once links to all the suppliers have been created.
485 * This function is NOT meant to be called from the probe function of the
486 * consumer but rather from code that creates/adds the consumer device.
488 static void device_link_wait_for_supplier(struct device *consumer,
491 mutex_lock(&wfs_lock);
492 list_add_tail(&consumer->links.needs_suppliers, &wait_for_suppliers);
493 consumer->links.need_for_probe = need_for_probe;
494 mutex_unlock(&wfs_lock);
497 static void device_link_wait_for_mandatory_supplier(struct device *consumer)
499 device_link_wait_for_supplier(consumer, true);
502 static void device_link_wait_for_optional_supplier(struct device *consumer)
504 device_link_wait_for_supplier(consumer, false);
508 * device_link_add_missing_supplier_links - Add links from consumer devices to
509 * supplier devices, leaving any
510 * consumer with inactive suppliers on
511 * the wait_for_suppliers list
513 * Loops through all consumers waiting on suppliers and tries to add all their
514 * supplier links. If that succeeds, the consumer device is removed from
515 * wait_for_suppliers list. Otherwise, they are left in the wait_for_suppliers
516 * list. Devices left on the wait_for_suppliers list will not be probed.
518 * The fwnode add_links callback is expected to return 0 if it has found and
519 * added all the supplier links for the consumer device. It should return an
520 * error if it isn't able to do so.
522 * The caller of device_link_wait_for_supplier() is expected to call this once
523 * it's aware of potential suppliers becoming available.
525 static void device_link_add_missing_supplier_links(void)
527 struct device *dev, *tmp;
529 mutex_lock(&wfs_lock);
530 list_for_each_entry_safe(dev, tmp, &wait_for_suppliers,
531 links.needs_suppliers) {
532 int ret = fwnode_call_int_op(dev->fwnode, add_links, dev);
534 list_del_init(&dev->links.needs_suppliers);
535 else if (ret != -ENODEV || fw_devlink_is_permissive())
536 dev->links.need_for_probe = false;
538 mutex_unlock(&wfs_lock);
541 static void device_link_free(struct device_link *link)
543 while (refcount_dec_not_one(&link->rpm_active))
544 pm_runtime_put(link->supplier);
546 put_device(link->consumer);
547 put_device(link->supplier);
552 static void __device_link_free_srcu(struct rcu_head *rhead)
554 device_link_free(container_of(rhead, struct device_link, rcu_head));
557 static void __device_link_del(struct kref *kref)
559 struct device_link *link = container_of(kref, struct device_link, kref);
561 dev_dbg(link->consumer, "Dropping the link to %s\n",
562 dev_name(link->supplier));
564 if (link->flags & DL_FLAG_PM_RUNTIME)
565 pm_runtime_drop_link(link->consumer);
567 list_del_rcu(&link->s_node);
568 list_del_rcu(&link->c_node);
569 call_srcu(&device_links_srcu, &link->rcu_head, __device_link_free_srcu);
571 #else /* !CONFIG_SRCU */
572 static void __device_link_del(struct kref *kref)
574 struct device_link *link = container_of(kref, struct device_link, kref);
576 dev_info(link->consumer, "Dropping the link to %s\n",
577 dev_name(link->supplier));
579 if (link->flags & DL_FLAG_PM_RUNTIME)
580 pm_runtime_drop_link(link->consumer);
582 list_del(&link->s_node);
583 list_del(&link->c_node);
584 device_link_free(link);
586 #endif /* !CONFIG_SRCU */
588 static void device_link_put_kref(struct device_link *link)
590 if (link->flags & DL_FLAG_STATELESS)
591 kref_put(&link->kref, __device_link_del);
593 WARN(1, "Unable to drop a managed device link reference\n");
597 * device_link_del - Delete a stateless link between two devices.
598 * @link: Device link to delete.
600 * The caller must ensure proper synchronization of this function with runtime
601 * PM. If the link was added multiple times, it needs to be deleted as often.
602 * Care is required for hotplugged devices: Their links are purged on removal
603 * and calling device_link_del() is then no longer allowed.
605 void device_link_del(struct device_link *link)
607 device_links_write_lock();
609 device_link_put_kref(link);
611 device_links_write_unlock();
613 EXPORT_SYMBOL_GPL(device_link_del);
616 * device_link_remove - Delete a stateless link between two devices.
617 * @consumer: Consumer end of the link.
618 * @supplier: Supplier end of the link.
620 * The caller must ensure proper synchronization of this function with runtime
623 void device_link_remove(void *consumer, struct device *supplier)
625 struct device_link *link;
627 if (WARN_ON(consumer == supplier))
630 device_links_write_lock();
633 list_for_each_entry(link, &supplier->links.consumers, s_node) {
634 if (link->consumer == consumer) {
635 device_link_put_kref(link);
641 device_links_write_unlock();
643 EXPORT_SYMBOL_GPL(device_link_remove);
645 static void device_links_missing_supplier(struct device *dev)
647 struct device_link *link;
649 list_for_each_entry(link, &dev->links.suppliers, c_node) {
650 if (link->status != DL_STATE_CONSUMER_PROBE)
653 if (link->supplier->links.status == DL_DEV_DRIVER_BOUND) {
654 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
656 WARN_ON(!(link->flags & DL_FLAG_SYNC_STATE_ONLY));
657 WRITE_ONCE(link->status, DL_STATE_DORMANT);
663 * device_links_check_suppliers - Check presence of supplier drivers.
664 * @dev: Consumer device.
666 * Check links from this device to any suppliers. Walk the list of the device's
667 * links to suppliers and see if all of them are available. If not, simply
668 * return -EPROBE_DEFER.
670 * We need to guarantee that the supplier will not go away after the check has
671 * been positive here. It only can go away in __device_release_driver() and
672 * that function checks the device's links to consumers. This means we need to
673 * mark the link as "consumer probe in progress" to make the supplier removal
674 * wait for us to complete (or bad things may happen).
676 * Links without the DL_FLAG_MANAGED flag set are ignored.
678 int device_links_check_suppliers(struct device *dev)
680 struct device_link *link;
684 * Device waiting for supplier to become available is not allowed to
687 mutex_lock(&wfs_lock);
688 if (!list_empty(&dev->links.needs_suppliers) &&
689 dev->links.need_for_probe) {
690 mutex_unlock(&wfs_lock);
691 return -EPROBE_DEFER;
693 mutex_unlock(&wfs_lock);
695 device_links_write_lock();
697 list_for_each_entry(link, &dev->links.suppliers, c_node) {
698 if (!(link->flags & DL_FLAG_MANAGED))
701 if (link->status != DL_STATE_AVAILABLE &&
702 !(link->flags & DL_FLAG_SYNC_STATE_ONLY)) {
703 device_links_missing_supplier(dev);
707 WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE);
709 dev->links.status = DL_DEV_PROBING;
711 device_links_write_unlock();
716 * __device_links_queue_sync_state - Queue a device for sync_state() callback
717 * @dev: Device to call sync_state() on
718 * @list: List head to queue the @dev on
720 * Queues a device for a sync_state() callback when the device links write lock
721 * isn't held. This allows the sync_state() execution flow to use device links
722 * APIs. The caller must ensure this function is called with
723 * device_links_write_lock() held.
725 * This function does a get_device() to make sure the device is not freed while
728 * So the caller must also ensure that device_links_flush_sync_list() is called
729 * as soon as the caller releases device_links_write_lock(). This is necessary
730 * to make sure the sync_state() is called in a timely fashion and the
731 * put_device() is called on this device.
733 static void __device_links_queue_sync_state(struct device *dev,
734 struct list_head *list)
736 struct device_link *link;
738 if (!dev_has_sync_state(dev))
740 if (dev->state_synced)
743 list_for_each_entry(link, &dev->links.consumers, s_node) {
744 if (!(link->flags & DL_FLAG_MANAGED))
746 if (link->status != DL_STATE_ACTIVE)
751 * Set the flag here to avoid adding the same device to a list more
752 * than once. This can happen if new consumers get added to the device
753 * and probed before the list is flushed.
755 dev->state_synced = true;
757 if (WARN_ON(!list_empty(&dev->links.defer_sync)))
761 list_add_tail(&dev->links.defer_sync, list);
765 * device_links_flush_sync_list - Call sync_state() on a list of devices
766 * @list: List of devices to call sync_state() on
767 * @dont_lock_dev: Device for which lock is already held by the caller
769 * Calls sync_state() on all the devices that have been queued for it. This
770 * function is used in conjunction with __device_links_queue_sync_state(). The
771 * @dont_lock_dev parameter is useful when this function is called from a
772 * context where a device lock is already held.
774 static void device_links_flush_sync_list(struct list_head *list,
775 struct device *dont_lock_dev)
777 struct device *dev, *tmp;
779 list_for_each_entry_safe(dev, tmp, list, links.defer_sync) {
780 list_del_init(&dev->links.defer_sync);
782 if (dev != dont_lock_dev)
785 if (dev->bus->sync_state)
786 dev->bus->sync_state(dev);
787 else if (dev->driver && dev->driver->sync_state)
788 dev->driver->sync_state(dev);
790 if (dev != dont_lock_dev)
797 void device_links_supplier_sync_state_pause(void)
799 device_links_write_lock();
800 defer_sync_state_count++;
801 device_links_write_unlock();
804 void device_links_supplier_sync_state_resume(void)
806 struct device *dev, *tmp;
807 LIST_HEAD(sync_list);
809 device_links_write_lock();
810 if (!defer_sync_state_count) {
811 WARN(true, "Unmatched sync_state pause/resume!");
814 defer_sync_state_count--;
815 if (defer_sync_state_count)
818 list_for_each_entry_safe(dev, tmp, &deferred_sync, links.defer_sync) {
820 * Delete from deferred_sync list before queuing it to
821 * sync_list because defer_sync is used for both lists.
823 list_del_init(&dev->links.defer_sync);
824 __device_links_queue_sync_state(dev, &sync_list);
827 device_links_write_unlock();
829 device_links_flush_sync_list(&sync_list, NULL);
832 static int sync_state_resume_initcall(void)
834 device_links_supplier_sync_state_resume();
837 late_initcall(sync_state_resume_initcall);
839 static void __device_links_supplier_defer_sync(struct device *sup)
841 if (list_empty(&sup->links.defer_sync) && dev_has_sync_state(sup))
842 list_add_tail(&sup->links.defer_sync, &deferred_sync);
845 static void device_link_drop_managed(struct device_link *link)
847 link->flags &= ~DL_FLAG_MANAGED;
848 WRITE_ONCE(link->status, DL_STATE_NONE);
849 kref_put(&link->kref, __device_link_del);
853 * device_links_driver_bound - Update device links after probing its driver.
854 * @dev: Device to update the links for.
856 * The probe has been successful, so update links from this device to any
857 * consumers by changing their status to "available".
859 * Also change the status of @dev's links to suppliers to "active".
861 * Links without the DL_FLAG_MANAGED flag set are ignored.
863 void device_links_driver_bound(struct device *dev)
865 struct device_link *link, *ln;
866 LIST_HEAD(sync_list);
869 * If a device probes successfully, it's expected to have created all
870 * the device links it needs to or make new device links as it needs
871 * them. So, it no longer needs to wait on any suppliers.
873 mutex_lock(&wfs_lock);
874 list_del_init(&dev->links.needs_suppliers);
875 mutex_unlock(&wfs_lock);
877 device_links_write_lock();
879 list_for_each_entry(link, &dev->links.consumers, s_node) {
880 if (!(link->flags & DL_FLAG_MANAGED))
884 * Links created during consumer probe may be in the "consumer
885 * probe" state to start with if the supplier is still probing
886 * when they are created and they may become "active" if the
887 * consumer probe returns first. Skip them here.
889 if (link->status == DL_STATE_CONSUMER_PROBE ||
890 link->status == DL_STATE_ACTIVE)
893 WARN_ON(link->status != DL_STATE_DORMANT);
894 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
896 if (link->flags & DL_FLAG_AUTOPROBE_CONSUMER)
897 driver_deferred_probe_add(link->consumer);
900 if (defer_sync_state_count)
901 __device_links_supplier_defer_sync(dev);
903 __device_links_queue_sync_state(dev, &sync_list);
905 list_for_each_entry_safe(link, ln, &dev->links.suppliers, c_node) {
906 struct device *supplier;
908 if (!(link->flags & DL_FLAG_MANAGED))
911 supplier = link->supplier;
912 if (link->flags & DL_FLAG_SYNC_STATE_ONLY) {
914 * When DL_FLAG_SYNC_STATE_ONLY is set, it means no
915 * other DL_MANAGED_LINK_FLAGS have been set. So, it's
916 * save to drop the managed link completely.
918 device_link_drop_managed(link);
920 WARN_ON(link->status != DL_STATE_CONSUMER_PROBE);
921 WRITE_ONCE(link->status, DL_STATE_ACTIVE);
925 * This needs to be done even for the deleted
926 * DL_FLAG_SYNC_STATE_ONLY device link in case it was the last
927 * device link that was preventing the supplier from getting a
930 if (defer_sync_state_count)
931 __device_links_supplier_defer_sync(supplier);
933 __device_links_queue_sync_state(supplier, &sync_list);
936 dev->links.status = DL_DEV_DRIVER_BOUND;
938 device_links_write_unlock();
940 device_links_flush_sync_list(&sync_list, dev);
944 * __device_links_no_driver - Update links of a device without a driver.
945 * @dev: Device without a drvier.
947 * Delete all non-persistent links from this device to any suppliers.
949 * Persistent links stay around, but their status is changed to "available",
950 * unless they already are in the "supplier unbind in progress" state in which
951 * case they need not be updated.
953 * Links without the DL_FLAG_MANAGED flag set are ignored.
955 static void __device_links_no_driver(struct device *dev)
957 struct device_link *link, *ln;
959 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
960 if (!(link->flags & DL_FLAG_MANAGED))
963 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER) {
964 device_link_drop_managed(link);
968 if (link->status != DL_STATE_CONSUMER_PROBE &&
969 link->status != DL_STATE_ACTIVE)
972 if (link->supplier->links.status == DL_DEV_DRIVER_BOUND) {
973 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
975 WARN_ON(!(link->flags & DL_FLAG_SYNC_STATE_ONLY));
976 WRITE_ONCE(link->status, DL_STATE_DORMANT);
980 dev->links.status = DL_DEV_NO_DRIVER;
984 * device_links_no_driver - Update links after failing driver probe.
985 * @dev: Device whose driver has just failed to probe.
987 * Clean up leftover links to consumers for @dev and invoke
988 * %__device_links_no_driver() to update links to suppliers for it as
991 * Links without the DL_FLAG_MANAGED flag set are ignored.
993 void device_links_no_driver(struct device *dev)
995 struct device_link *link;
997 device_links_write_lock();
999 list_for_each_entry(link, &dev->links.consumers, s_node) {
1000 if (!(link->flags & DL_FLAG_MANAGED))
1004 * The probe has failed, so if the status of the link is
1005 * "consumer probe" or "active", it must have been added by
1006 * a probing consumer while this device was still probing.
1007 * Change its state to "dormant", as it represents a valid
1008 * relationship, but it is not functionally meaningful.
1010 if (link->status == DL_STATE_CONSUMER_PROBE ||
1011 link->status == DL_STATE_ACTIVE)
1012 WRITE_ONCE(link->status, DL_STATE_DORMANT);
1015 __device_links_no_driver(dev);
1017 device_links_write_unlock();
1021 * device_links_driver_cleanup - Update links after driver removal.
1022 * @dev: Device whose driver has just gone away.
1024 * Update links to consumers for @dev by changing their status to "dormant" and
1025 * invoke %__device_links_no_driver() to update links to suppliers for it as
1028 * Links without the DL_FLAG_MANAGED flag set are ignored.
1030 void device_links_driver_cleanup(struct device *dev)
1032 struct device_link *link, *ln;
1034 device_links_write_lock();
1036 list_for_each_entry_safe(link, ln, &dev->links.consumers, s_node) {
1037 if (!(link->flags & DL_FLAG_MANAGED))
1040 WARN_ON(link->flags & DL_FLAG_AUTOREMOVE_CONSUMER);
1041 WARN_ON(link->status != DL_STATE_SUPPLIER_UNBIND);
1044 * autoremove the links between this @dev and its consumer
1045 * devices that are not active, i.e. where the link state
1046 * has moved to DL_STATE_SUPPLIER_UNBIND.
1048 if (link->status == DL_STATE_SUPPLIER_UNBIND &&
1049 link->flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
1050 device_link_drop_managed(link);
1052 WRITE_ONCE(link->status, DL_STATE_DORMANT);
1055 list_del_init(&dev->links.defer_sync);
1056 __device_links_no_driver(dev);
1058 device_links_write_unlock();
1062 * device_links_busy - Check if there are any busy links to consumers.
1063 * @dev: Device to check.
1065 * Check each consumer of the device and return 'true' if its link's status
1066 * is one of "consumer probe" or "active" (meaning that the given consumer is
1067 * probing right now or its driver is present). Otherwise, change the link
1068 * state to "supplier unbind" to prevent the consumer from being probed
1069 * successfully going forward.
1071 * Return 'false' if there are no probing or active consumers.
1073 * Links without the DL_FLAG_MANAGED flag set are ignored.
1075 bool device_links_busy(struct device *dev)
1077 struct device_link *link;
1080 device_links_write_lock();
1082 list_for_each_entry(link, &dev->links.consumers, s_node) {
1083 if (!(link->flags & DL_FLAG_MANAGED))
1086 if (link->status == DL_STATE_CONSUMER_PROBE
1087 || link->status == DL_STATE_ACTIVE) {
1091 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
1094 dev->links.status = DL_DEV_UNBINDING;
1096 device_links_write_unlock();
1101 * device_links_unbind_consumers - Force unbind consumers of the given device.
1102 * @dev: Device to unbind the consumers of.
1104 * Walk the list of links to consumers for @dev and if any of them is in the
1105 * "consumer probe" state, wait for all device probes in progress to complete
1108 * If that's not the case, change the status of the link to "supplier unbind"
1109 * and check if the link was in the "active" state. If so, force the consumer
1110 * driver to unbind and start over (the consumer will not re-probe as we have
1111 * changed the state of the link already).
1113 * Links without the DL_FLAG_MANAGED flag set are ignored.
1115 void device_links_unbind_consumers(struct device *dev)
1117 struct device_link *link;
1120 device_links_write_lock();
1122 list_for_each_entry(link, &dev->links.consumers, s_node) {
1123 enum device_link_state status;
1125 if (!(link->flags & DL_FLAG_MANAGED) ||
1126 link->flags & DL_FLAG_SYNC_STATE_ONLY)
1129 status = link->status;
1130 if (status == DL_STATE_CONSUMER_PROBE) {
1131 device_links_write_unlock();
1133 wait_for_device_probe();
1136 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
1137 if (status == DL_STATE_ACTIVE) {
1138 struct device *consumer = link->consumer;
1140 get_device(consumer);
1142 device_links_write_unlock();
1144 device_release_driver_internal(consumer, NULL,
1146 put_device(consumer);
1151 device_links_write_unlock();
1155 * device_links_purge - Delete existing links to other devices.
1156 * @dev: Target device.
1158 static void device_links_purge(struct device *dev)
1160 struct device_link *link, *ln;
1162 mutex_lock(&wfs_lock);
1163 list_del(&dev->links.needs_suppliers);
1164 mutex_unlock(&wfs_lock);
1167 * Delete all of the remaining links from this device to any other
1168 * devices (either consumers or suppliers).
1170 device_links_write_lock();
1172 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
1173 WARN_ON(link->status == DL_STATE_ACTIVE);
1174 __device_link_del(&link->kref);
1177 list_for_each_entry_safe_reverse(link, ln, &dev->links.consumers, s_node) {
1178 WARN_ON(link->status != DL_STATE_DORMANT &&
1179 link->status != DL_STATE_NONE);
1180 __device_link_del(&link->kref);
1183 device_links_write_unlock();
1186 static u32 fw_devlink_flags = DL_FLAG_SYNC_STATE_ONLY;
1187 static int __init fw_devlink_setup(char *arg)
1192 if (strcmp(arg, "off") == 0) {
1193 fw_devlink_flags = 0;
1194 } else if (strcmp(arg, "permissive") == 0) {
1195 fw_devlink_flags = DL_FLAG_SYNC_STATE_ONLY;
1196 } else if (strcmp(arg, "on") == 0) {
1197 fw_devlink_flags = DL_FLAG_AUTOPROBE_CONSUMER;
1198 } else if (strcmp(arg, "rpm") == 0) {
1199 fw_devlink_flags = DL_FLAG_AUTOPROBE_CONSUMER |
1204 early_param("fw_devlink", fw_devlink_setup);
1206 u32 fw_devlink_get_flags(void)
1208 return fw_devlink_flags;
1211 static bool fw_devlink_is_permissive(void)
1213 return fw_devlink_flags == DL_FLAG_SYNC_STATE_ONLY;
1216 static void fw_devlink_link_device(struct device *dev)
1220 if (!fw_devlink_flags)
1223 mutex_lock(&defer_fw_devlink_lock);
1224 if (!defer_fw_devlink_count)
1225 device_link_add_missing_supplier_links();
1228 * The device's fwnode not having add_links() doesn't affect if other
1229 * consumers can find this device as a supplier. So, this check is
1230 * intentionally placed after device_link_add_missing_supplier_links().
1232 if (!fwnode_has_op(dev->fwnode, add_links))
1236 * If fw_devlink is being deferred, assume all devices have mandatory
1237 * suppliers they need to link to later. Then, when the fw_devlink is
1238 * resumed, all these devices will get a chance to try and link to any
1239 * suppliers they have.
1241 if (!defer_fw_devlink_count) {
1242 fw_ret = fwnode_call_int_op(dev->fwnode, add_links, dev);
1243 if (fw_ret == -ENODEV && fw_devlink_is_permissive())
1249 if (fw_ret == -ENODEV)
1250 device_link_wait_for_mandatory_supplier(dev);
1252 device_link_wait_for_optional_supplier(dev);
1255 mutex_unlock(&defer_fw_devlink_lock);
1259 * fw_devlink_pause - Pause parsing of fwnode to create device links
1261 * Calling this function defers any fwnode parsing to create device links until
1262 * fw_devlink_resume() is called. Both these functions are ref counted and the
1263 * caller needs to match the calls.
1265 * While fw_devlink is paused:
1266 * - Any device that is added won't have its fwnode parsed to create device
1268 * - The probe of the device will also be deferred during this period.
1269 * - Any devices that were already added, but waiting for suppliers won't be
1270 * able to link to newly added devices.
1272 * Once fw_devlink_resume():
1273 * - All the fwnodes that was not parsed will be parsed.
1274 * - All the devices that were deferred probing will be reattempted if they
1275 * aren't waiting for any more suppliers.
1277 * This pair of functions, is mainly meant to optimize the parsing of fwnodes
1278 * when a lot of devices that need to link to each other are added in a short
1279 * interval of time. For example, adding all the top level devices in a system.
1281 * For example, if N devices are added and:
1282 * - All the consumers are added before their suppliers
1283 * - All the suppliers of the N devices are part of the N devices
1287 * - With the use of fw_devlink_pause() and fw_devlink_resume(), each device
1288 * will only need one parsing of its fwnode because it is guaranteed to find
1289 * all the supplier devices already registered and ready to link to. It won't
1290 * have to do another pass later to find one or more suppliers it couldn't
1291 * find in the first parse of the fwnode. So, we'll only need O(N) fwnode
1294 * - Without the use of fw_devlink_pause() and fw_devlink_resume(), we would
1295 * end up doing O(N^2) parses of fwnodes because every device that's added is
1296 * guaranteed to trigger a parse of the fwnode of every device added before
1297 * it. This O(N^2) parse is made worse by the fact that when a fwnode of a
1298 * device is parsed, all it descendant devices might need to have their
1299 * fwnodes parsed too (even if the devices themselves aren't added).
1301 void fw_devlink_pause(void)
1303 mutex_lock(&defer_fw_devlink_lock);
1304 defer_fw_devlink_count++;
1305 mutex_unlock(&defer_fw_devlink_lock);
1308 /** fw_devlink_resume - Resume parsing of fwnode to create device links
1310 * This function is used in conjunction with fw_devlink_pause() and is ref
1311 * counted. See documentation for fw_devlink_pause() for more details.
1313 void fw_devlink_resume(void)
1315 mutex_lock(&defer_fw_devlink_lock);
1316 if (!defer_fw_devlink_count) {
1317 WARN(true, "Unmatched fw_devlink pause/resume!");
1321 defer_fw_devlink_count--;
1322 if (defer_fw_devlink_count)
1325 device_link_add_missing_supplier_links();
1326 driver_deferred_probe_force_trigger();
1328 mutex_unlock(&defer_fw_devlink_lock);
1330 /* Device links support end. */
1332 int (*platform_notify)(struct device *dev) = NULL;
1333 int (*platform_notify_remove)(struct device *dev) = NULL;
1334 static struct kobject *dev_kobj;
1335 struct kobject *sysfs_dev_char_kobj;
1336 struct kobject *sysfs_dev_block_kobj;
1338 static DEFINE_MUTEX(device_hotplug_lock);
1340 void lock_device_hotplug(void)
1342 mutex_lock(&device_hotplug_lock);
1345 void unlock_device_hotplug(void)
1347 mutex_unlock(&device_hotplug_lock);
1350 int lock_device_hotplug_sysfs(void)
1352 if (mutex_trylock(&device_hotplug_lock))
1355 /* Avoid busy looping (5 ms of sleep should do). */
1357 return restart_syscall();
1361 static inline int device_is_not_partition(struct device *dev)
1363 return !(dev->type == &part_type);
1366 static inline int device_is_not_partition(struct device *dev)
1373 device_platform_notify(struct device *dev, enum kobject_action action)
1377 ret = acpi_platform_notify(dev, action);
1381 ret = software_node_notify(dev, action);
1385 if (platform_notify && action == KOBJ_ADD)
1386 platform_notify(dev);
1387 else if (platform_notify_remove && action == KOBJ_REMOVE)
1388 platform_notify_remove(dev);
1393 * dev_driver_string - Return a device's driver name, if at all possible
1394 * @dev: struct device to get the name of
1396 * Will return the device's driver's name if it is bound to a device. If
1397 * the device is not bound to a driver, it will return the name of the bus
1398 * it is attached to. If it is not attached to a bus either, an empty
1399 * string will be returned.
1401 const char *dev_driver_string(const struct device *dev)
1403 struct device_driver *drv;
1405 /* dev->driver can change to NULL underneath us because of unbinding,
1406 * so be careful about accessing it. dev->bus and dev->class should
1407 * never change once they are set, so they don't need special care.
1409 drv = READ_ONCE(dev->driver);
1410 return drv ? drv->name :
1411 (dev->bus ? dev->bus->name :
1412 (dev->class ? dev->class->name : ""));
1414 EXPORT_SYMBOL(dev_driver_string);
1416 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
1418 static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
1421 struct device_attribute *dev_attr = to_dev_attr(attr);
1422 struct device *dev = kobj_to_dev(kobj);
1426 ret = dev_attr->show(dev, dev_attr, buf);
1427 if (ret >= (ssize_t)PAGE_SIZE) {
1428 printk("dev_attr_show: %pS returned bad count\n",
1434 static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
1435 const char *buf, size_t count)
1437 struct device_attribute *dev_attr = to_dev_attr(attr);
1438 struct device *dev = kobj_to_dev(kobj);
1441 if (dev_attr->store)
1442 ret = dev_attr->store(dev, dev_attr, buf, count);
1446 static const struct sysfs_ops dev_sysfs_ops = {
1447 .show = dev_attr_show,
1448 .store = dev_attr_store,
1451 #define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
1453 ssize_t device_store_ulong(struct device *dev,
1454 struct device_attribute *attr,
1455 const char *buf, size_t size)
1457 struct dev_ext_attribute *ea = to_ext_attr(attr);
1461 ret = kstrtoul(buf, 0, &new);
1464 *(unsigned long *)(ea->var) = new;
1465 /* Always return full write size even if we didn't consume all */
1468 EXPORT_SYMBOL_GPL(device_store_ulong);
1470 ssize_t device_show_ulong(struct device *dev,
1471 struct device_attribute *attr,
1474 struct dev_ext_attribute *ea = to_ext_attr(attr);
1475 return snprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var));
1477 EXPORT_SYMBOL_GPL(device_show_ulong);
1479 ssize_t device_store_int(struct device *dev,
1480 struct device_attribute *attr,
1481 const char *buf, size_t size)
1483 struct dev_ext_attribute *ea = to_ext_attr(attr);
1487 ret = kstrtol(buf, 0, &new);
1491 if (new > INT_MAX || new < INT_MIN)
1493 *(int *)(ea->var) = new;
1494 /* Always return full write size even if we didn't consume all */
1497 EXPORT_SYMBOL_GPL(device_store_int);
1499 ssize_t device_show_int(struct device *dev,
1500 struct device_attribute *attr,
1503 struct dev_ext_attribute *ea = to_ext_attr(attr);
1505 return snprintf(buf, PAGE_SIZE, "%d\n", *(int *)(ea->var));
1507 EXPORT_SYMBOL_GPL(device_show_int);
1509 ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
1510 const char *buf, size_t size)
1512 struct dev_ext_attribute *ea = to_ext_attr(attr);
1514 if (strtobool(buf, ea->var) < 0)
1519 EXPORT_SYMBOL_GPL(device_store_bool);
1521 ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
1524 struct dev_ext_attribute *ea = to_ext_attr(attr);
1526 return snprintf(buf, PAGE_SIZE, "%d\n", *(bool *)(ea->var));
1528 EXPORT_SYMBOL_GPL(device_show_bool);
1531 * device_release - free device structure.
1532 * @kobj: device's kobject.
1534 * This is called once the reference count for the object
1535 * reaches 0. We forward the call to the device's release
1536 * method, which should handle actually freeing the structure.
1538 static void device_release(struct kobject *kobj)
1540 struct device *dev = kobj_to_dev(kobj);
1541 struct device_private *p = dev->p;
1544 * Some platform devices are driven without driver attached
1545 * and managed resources may have been acquired. Make sure
1546 * all resources are released.
1548 * Drivers still can add resources into device after device
1549 * is deleted but alive, so release devres here to avoid
1550 * possible memory leak.
1552 devres_release_all(dev);
1556 else if (dev->type && dev->type->release)
1557 dev->type->release(dev);
1558 else if (dev->class && dev->class->dev_release)
1559 dev->class->dev_release(dev);
1561 WARN(1, KERN_ERR "Device '%s' does not have a release() function, it is broken and must be fixed. See Documentation/core-api/kobject.rst.\n",
1566 static const void *device_namespace(struct kobject *kobj)
1568 struct device *dev = kobj_to_dev(kobj);
1569 const void *ns = NULL;
1571 if (dev->class && dev->class->ns_type)
1572 ns = dev->class->namespace(dev);
1577 static void device_get_ownership(struct kobject *kobj, kuid_t *uid, kgid_t *gid)
1579 struct device *dev = kobj_to_dev(kobj);
1581 if (dev->class && dev->class->get_ownership)
1582 dev->class->get_ownership(dev, uid, gid);
1585 static struct kobj_type device_ktype = {
1586 .release = device_release,
1587 .sysfs_ops = &dev_sysfs_ops,
1588 .namespace = device_namespace,
1589 .get_ownership = device_get_ownership,
1593 static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
1595 struct kobj_type *ktype = get_ktype(kobj);
1597 if (ktype == &device_ktype) {
1598 struct device *dev = kobj_to_dev(kobj);
1607 static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
1609 struct device *dev = kobj_to_dev(kobj);
1612 return dev->bus->name;
1614 return dev->class->name;
1618 static int dev_uevent(struct kset *kset, struct kobject *kobj,
1619 struct kobj_uevent_env *env)
1621 struct device *dev = kobj_to_dev(kobj);
1624 /* add device node properties if present */
1625 if (MAJOR(dev->devt)) {
1629 kuid_t uid = GLOBAL_ROOT_UID;
1630 kgid_t gid = GLOBAL_ROOT_GID;
1632 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
1633 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
1634 name = device_get_devnode(dev, &mode, &uid, &gid, &tmp);
1636 add_uevent_var(env, "DEVNAME=%s", name);
1638 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
1639 if (!uid_eq(uid, GLOBAL_ROOT_UID))
1640 add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid));
1641 if (!gid_eq(gid, GLOBAL_ROOT_GID))
1642 add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid));
1647 if (dev->type && dev->type->name)
1648 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
1651 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
1653 /* Add common DT information about the device */
1654 of_device_uevent(dev, env);
1656 /* have the bus specific function add its stuff */
1657 if (dev->bus && dev->bus->uevent) {
1658 retval = dev->bus->uevent(dev, env);
1660 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
1661 dev_name(dev), __func__, retval);
1664 /* have the class specific function add its stuff */
1665 if (dev->class && dev->class->dev_uevent) {
1666 retval = dev->class->dev_uevent(dev, env);
1668 pr_debug("device: '%s': %s: class uevent() "
1669 "returned %d\n", dev_name(dev),
1673 /* have the device type specific function add its stuff */
1674 if (dev->type && dev->type->uevent) {
1675 retval = dev->type->uevent(dev, env);
1677 pr_debug("device: '%s': %s: dev_type uevent() "
1678 "returned %d\n", dev_name(dev),
1685 static const struct kset_uevent_ops device_uevent_ops = {
1686 .filter = dev_uevent_filter,
1687 .name = dev_uevent_name,
1688 .uevent = dev_uevent,
1691 static ssize_t uevent_show(struct device *dev, struct device_attribute *attr,
1694 struct kobject *top_kobj;
1696 struct kobj_uevent_env *env = NULL;
1701 /* search the kset, the device belongs to */
1702 top_kobj = &dev->kobj;
1703 while (!top_kobj->kset && top_kobj->parent)
1704 top_kobj = top_kobj->parent;
1705 if (!top_kobj->kset)
1708 kset = top_kobj->kset;
1709 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
1712 /* respect filter */
1713 if (kset->uevent_ops && kset->uevent_ops->filter)
1714 if (!kset->uevent_ops->filter(kset, &dev->kobj))
1717 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
1721 /* let the kset specific function add its keys */
1722 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
1726 /* copy keys to file */
1727 for (i = 0; i < env->envp_idx; i++)
1728 count += sprintf(&buf[count], "%s\n", env->envp[i]);
1734 static ssize_t uevent_store(struct device *dev, struct device_attribute *attr,
1735 const char *buf, size_t count)
1739 rc = kobject_synth_uevent(&dev->kobj, buf, count);
1742 dev_err(dev, "uevent: failed to send synthetic uevent\n");
1748 static DEVICE_ATTR_RW(uevent);
1750 static ssize_t online_show(struct device *dev, struct device_attribute *attr,
1756 val = !dev->offline;
1758 return sprintf(buf, "%u\n", val);
1761 static ssize_t online_store(struct device *dev, struct device_attribute *attr,
1762 const char *buf, size_t count)
1767 ret = strtobool(buf, &val);
1771 ret = lock_device_hotplug_sysfs();
1775 ret = val ? device_online(dev) : device_offline(dev);
1776 unlock_device_hotplug();
1777 return ret < 0 ? ret : count;
1779 static DEVICE_ATTR_RW(online);
1781 int device_add_groups(struct device *dev, const struct attribute_group **groups)
1783 return sysfs_create_groups(&dev->kobj, groups);
1785 EXPORT_SYMBOL_GPL(device_add_groups);
1787 void device_remove_groups(struct device *dev,
1788 const struct attribute_group **groups)
1790 sysfs_remove_groups(&dev->kobj, groups);
1792 EXPORT_SYMBOL_GPL(device_remove_groups);
1794 union device_attr_group_devres {
1795 const struct attribute_group *group;
1796 const struct attribute_group **groups;
1799 static int devm_attr_group_match(struct device *dev, void *res, void *data)
1801 return ((union device_attr_group_devres *)res)->group == data;
1804 static void devm_attr_group_remove(struct device *dev, void *res)
1806 union device_attr_group_devres *devres = res;
1807 const struct attribute_group *group = devres->group;
1809 dev_dbg(dev, "%s: removing group %p\n", __func__, group);
1810 sysfs_remove_group(&dev->kobj, group);
1813 static void devm_attr_groups_remove(struct device *dev, void *res)
1815 union device_attr_group_devres *devres = res;
1816 const struct attribute_group **groups = devres->groups;
1818 dev_dbg(dev, "%s: removing groups %p\n", __func__, groups);
1819 sysfs_remove_groups(&dev->kobj, groups);
1823 * devm_device_add_group - given a device, create a managed attribute group
1824 * @dev: The device to create the group for
1825 * @grp: The attribute group to create
1827 * This function creates a group for the first time. It will explicitly
1828 * warn and error if any of the attribute files being created already exist.
1830 * Returns 0 on success or error code on failure.
1832 int devm_device_add_group(struct device *dev, const struct attribute_group *grp)
1834 union device_attr_group_devres *devres;
1837 devres = devres_alloc(devm_attr_group_remove,
1838 sizeof(*devres), GFP_KERNEL);
1842 error = sysfs_create_group(&dev->kobj, grp);
1844 devres_free(devres);
1848 devres->group = grp;
1849 devres_add(dev, devres);
1852 EXPORT_SYMBOL_GPL(devm_device_add_group);
1855 * devm_device_remove_group: remove a managed group from a device
1856 * @dev: device to remove the group from
1857 * @grp: group to remove
1859 * This function removes a group of attributes from a device. The attributes
1860 * previously have to have been created for this group, otherwise it will fail.
1862 void devm_device_remove_group(struct device *dev,
1863 const struct attribute_group *grp)
1865 WARN_ON(devres_release(dev, devm_attr_group_remove,
1866 devm_attr_group_match,
1867 /* cast away const */ (void *)grp));
1869 EXPORT_SYMBOL_GPL(devm_device_remove_group);
1872 * devm_device_add_groups - create a bunch of managed attribute groups
1873 * @dev: The device to create the group for
1874 * @groups: The attribute groups to create, NULL terminated
1876 * This function creates a bunch of managed attribute groups. If an error
1877 * occurs when creating a group, all previously created groups will be
1878 * removed, unwinding everything back to the original state when this
1879 * function was called. It will explicitly warn and error if any of the
1880 * attribute files being created already exist.
1882 * Returns 0 on success or error code from sysfs_create_group on failure.
1884 int devm_device_add_groups(struct device *dev,
1885 const struct attribute_group **groups)
1887 union device_attr_group_devres *devres;
1890 devres = devres_alloc(devm_attr_groups_remove,
1891 sizeof(*devres), GFP_KERNEL);
1895 error = sysfs_create_groups(&dev->kobj, groups);
1897 devres_free(devres);
1901 devres->groups = groups;
1902 devres_add(dev, devres);
1905 EXPORT_SYMBOL_GPL(devm_device_add_groups);
1908 * devm_device_remove_groups - remove a list of managed groups
1910 * @dev: The device for the groups to be removed from
1911 * @groups: NULL terminated list of groups to be removed
1913 * If groups is not NULL, remove the specified groups from the device.
1915 void devm_device_remove_groups(struct device *dev,
1916 const struct attribute_group **groups)
1918 WARN_ON(devres_release(dev, devm_attr_groups_remove,
1919 devm_attr_group_match,
1920 /* cast away const */ (void *)groups));
1922 EXPORT_SYMBOL_GPL(devm_device_remove_groups);
1924 static int device_add_attrs(struct device *dev)
1926 struct class *class = dev->class;
1927 const struct device_type *type = dev->type;
1931 error = device_add_groups(dev, class->dev_groups);
1937 error = device_add_groups(dev, type->groups);
1939 goto err_remove_class_groups;
1942 error = device_add_groups(dev, dev->groups);
1944 goto err_remove_type_groups;
1946 if (device_supports_offline(dev) && !dev->offline_disabled) {
1947 error = device_create_file(dev, &dev_attr_online);
1949 goto err_remove_dev_groups;
1954 err_remove_dev_groups:
1955 device_remove_groups(dev, dev->groups);
1956 err_remove_type_groups:
1958 device_remove_groups(dev, type->groups);
1959 err_remove_class_groups:
1961 device_remove_groups(dev, class->dev_groups);
1966 static void device_remove_attrs(struct device *dev)
1968 struct class *class = dev->class;
1969 const struct device_type *type = dev->type;
1971 device_remove_file(dev, &dev_attr_online);
1972 device_remove_groups(dev, dev->groups);
1975 device_remove_groups(dev, type->groups);
1978 device_remove_groups(dev, class->dev_groups);
1981 static ssize_t dev_show(struct device *dev, struct device_attribute *attr,
1984 return print_dev_t(buf, dev->devt);
1986 static DEVICE_ATTR_RO(dev);
1989 struct kset *devices_kset;
1992 * devices_kset_move_before - Move device in the devices_kset's list.
1993 * @deva: Device to move.
1994 * @devb: Device @deva should come before.
1996 static void devices_kset_move_before(struct device *deva, struct device *devb)
2000 pr_debug("devices_kset: Moving %s before %s\n",
2001 dev_name(deva), dev_name(devb));
2002 spin_lock(&devices_kset->list_lock);
2003 list_move_tail(&deva->kobj.entry, &devb->kobj.entry);
2004 spin_unlock(&devices_kset->list_lock);
2008 * devices_kset_move_after - Move device in the devices_kset's list.
2009 * @deva: Device to move
2010 * @devb: Device @deva should come after.
2012 static void devices_kset_move_after(struct device *deva, struct device *devb)
2016 pr_debug("devices_kset: Moving %s after %s\n",
2017 dev_name(deva), dev_name(devb));
2018 spin_lock(&devices_kset->list_lock);
2019 list_move(&deva->kobj.entry, &devb->kobj.entry);
2020 spin_unlock(&devices_kset->list_lock);
2024 * devices_kset_move_last - move the device to the end of devices_kset's list.
2025 * @dev: device to move
2027 void devices_kset_move_last(struct device *dev)
2031 pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev));
2032 spin_lock(&devices_kset->list_lock);
2033 list_move_tail(&dev->kobj.entry, &devices_kset->list);
2034 spin_unlock(&devices_kset->list_lock);
2038 * device_create_file - create sysfs attribute file for device.
2040 * @attr: device attribute descriptor.
2042 int device_create_file(struct device *dev,
2043 const struct device_attribute *attr)
2048 WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
2049 "Attribute %s: write permission without 'store'\n",
2051 WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
2052 "Attribute %s: read permission without 'show'\n",
2054 error = sysfs_create_file(&dev->kobj, &attr->attr);
2059 EXPORT_SYMBOL_GPL(device_create_file);
2062 * device_remove_file - remove sysfs attribute file.
2064 * @attr: device attribute descriptor.
2066 void device_remove_file(struct device *dev,
2067 const struct device_attribute *attr)
2070 sysfs_remove_file(&dev->kobj, &attr->attr);
2072 EXPORT_SYMBOL_GPL(device_remove_file);
2075 * device_remove_file_self - remove sysfs attribute file from its own method.
2077 * @attr: device attribute descriptor.
2079 * See kernfs_remove_self() for details.
2081 bool device_remove_file_self(struct device *dev,
2082 const struct device_attribute *attr)
2085 return sysfs_remove_file_self(&dev->kobj, &attr->attr);
2089 EXPORT_SYMBOL_GPL(device_remove_file_self);
2092 * device_create_bin_file - create sysfs binary attribute file for device.
2094 * @attr: device binary attribute descriptor.
2096 int device_create_bin_file(struct device *dev,
2097 const struct bin_attribute *attr)
2099 int error = -EINVAL;
2101 error = sysfs_create_bin_file(&dev->kobj, attr);
2104 EXPORT_SYMBOL_GPL(device_create_bin_file);
2107 * device_remove_bin_file - remove sysfs binary attribute file
2109 * @attr: device binary attribute descriptor.
2111 void device_remove_bin_file(struct device *dev,
2112 const struct bin_attribute *attr)
2115 sysfs_remove_bin_file(&dev->kobj, attr);
2117 EXPORT_SYMBOL_GPL(device_remove_bin_file);
2119 static void klist_children_get(struct klist_node *n)
2121 struct device_private *p = to_device_private_parent(n);
2122 struct device *dev = p->device;
2127 static void klist_children_put(struct klist_node *n)
2129 struct device_private *p = to_device_private_parent(n);
2130 struct device *dev = p->device;
2136 * device_initialize - init device structure.
2139 * This prepares the device for use by other layers by initializing
2141 * It is the first half of device_register(), if called by
2142 * that function, though it can also be called separately, so one
2143 * may use @dev's fields. In particular, get_device()/put_device()
2144 * may be used for reference counting of @dev after calling this
2147 * All fields in @dev must be initialized by the caller to 0, except
2148 * for those explicitly set to some other value. The simplest
2149 * approach is to use kzalloc() to allocate the structure containing
2152 * NOTE: Use put_device() to give up your reference instead of freeing
2153 * @dev directly once you have called this function.
2155 void device_initialize(struct device *dev)
2157 dev->kobj.kset = devices_kset;
2158 kobject_init(&dev->kobj, &device_ktype);
2159 INIT_LIST_HEAD(&dev->dma_pools);
2160 mutex_init(&dev->mutex);
2161 #ifdef CONFIG_PROVE_LOCKING
2162 mutex_init(&dev->lockdep_mutex);
2164 lockdep_set_novalidate_class(&dev->mutex);
2165 spin_lock_init(&dev->devres_lock);
2166 INIT_LIST_HEAD(&dev->devres_head);
2167 device_pm_init(dev);
2168 set_dev_node(dev, -1);
2169 #ifdef CONFIG_GENERIC_MSI_IRQ
2170 INIT_LIST_HEAD(&dev->msi_list);
2172 INIT_LIST_HEAD(&dev->links.consumers);
2173 INIT_LIST_HEAD(&dev->links.suppliers);
2174 INIT_LIST_HEAD(&dev->links.needs_suppliers);
2175 INIT_LIST_HEAD(&dev->links.defer_sync);
2176 dev->links.status = DL_DEV_NO_DRIVER;
2178 EXPORT_SYMBOL_GPL(device_initialize);
2180 struct kobject *virtual_device_parent(struct device *dev)
2182 static struct kobject *virtual_dir = NULL;
2185 virtual_dir = kobject_create_and_add("virtual",
2186 &devices_kset->kobj);
2192 struct kobject kobj;
2193 struct class *class;
2196 #define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
2198 static void class_dir_release(struct kobject *kobj)
2200 struct class_dir *dir = to_class_dir(kobj);
2205 struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
2207 struct class_dir *dir = to_class_dir(kobj);
2208 return dir->class->ns_type;
2211 static struct kobj_type class_dir_ktype = {
2212 .release = class_dir_release,
2213 .sysfs_ops = &kobj_sysfs_ops,
2214 .child_ns_type = class_dir_child_ns_type
2217 static struct kobject *
2218 class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
2220 struct class_dir *dir;
2223 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
2225 return ERR_PTR(-ENOMEM);
2228 kobject_init(&dir->kobj, &class_dir_ktype);
2230 dir->kobj.kset = &class->p->glue_dirs;
2232 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
2234 kobject_put(&dir->kobj);
2235 return ERR_PTR(retval);
2240 static DEFINE_MUTEX(gdp_mutex);
2242 static struct kobject *get_device_parent(struct device *dev,
2243 struct device *parent)
2246 struct kobject *kobj = NULL;
2247 struct kobject *parent_kobj;
2251 /* block disks show up in /sys/block */
2252 if (sysfs_deprecated && dev->class == &block_class) {
2253 if (parent && parent->class == &block_class)
2254 return &parent->kobj;
2255 return &block_class.p->subsys.kobj;
2260 * If we have no parent, we live in "virtual".
2261 * Class-devices with a non class-device as parent, live
2262 * in a "glue" directory to prevent namespace collisions.
2265 parent_kobj = virtual_device_parent(dev);
2266 else if (parent->class && !dev->class->ns_type)
2267 return &parent->kobj;
2269 parent_kobj = &parent->kobj;
2271 mutex_lock(&gdp_mutex);
2273 /* find our class-directory at the parent and reference it */
2274 spin_lock(&dev->class->p->glue_dirs.list_lock);
2275 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
2276 if (k->parent == parent_kobj) {
2277 kobj = kobject_get(k);
2280 spin_unlock(&dev->class->p->glue_dirs.list_lock);
2282 mutex_unlock(&gdp_mutex);
2286 /* or create a new class-directory at the parent device */
2287 k = class_dir_create_and_add(dev->class, parent_kobj);
2288 /* do not emit an uevent for this simple "glue" directory */
2289 mutex_unlock(&gdp_mutex);
2293 /* subsystems can specify a default root directory for their devices */
2294 if (!parent && dev->bus && dev->bus->dev_root)
2295 return &dev->bus->dev_root->kobj;
2298 return &parent->kobj;
2302 static inline bool live_in_glue_dir(struct kobject *kobj,
2305 if (!kobj || !dev->class ||
2306 kobj->kset != &dev->class->p->glue_dirs)
2311 static inline struct kobject *get_glue_dir(struct device *dev)
2313 return dev->kobj.parent;
2317 * make sure cleaning up dir as the last step, we need to make
2318 * sure .release handler of kobject is run with holding the
2321 static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
2325 /* see if we live in a "glue" directory */
2326 if (!live_in_glue_dir(glue_dir, dev))
2329 mutex_lock(&gdp_mutex);
2331 * There is a race condition between removing glue directory
2332 * and adding a new device under the glue directory.
2337 * get_device_parent()
2338 * class_dir_create_and_add()
2339 * kobject_add_internal()
2340 * create_dir() // create glue_dir
2343 * get_device_parent()
2344 * kobject_get() // get glue_dir
2347 * cleanup_glue_dir()
2348 * kobject_del(glue_dir)
2351 * kobject_add_internal()
2352 * create_dir() // in glue_dir
2353 * sysfs_create_dir_ns()
2354 * kernfs_create_dir_ns(sd)
2356 * sysfs_remove_dir() // glue_dir->sd=NULL
2357 * sysfs_put() // free glue_dir->sd
2360 * kernfs_new_node(sd)
2361 * kernfs_get(glue_dir)
2365 * Before CPU1 remove last child device under glue dir, if CPU2 add
2366 * a new device under glue dir, the glue_dir kobject reference count
2367 * will be increase to 2 in kobject_get(k). And CPU2 has been called
2368 * kernfs_create_dir_ns(). Meanwhile, CPU1 call sysfs_remove_dir()
2369 * and sysfs_put(). This result in glue_dir->sd is freed.
2371 * Then the CPU2 will see a stale "empty" but still potentially used
2372 * glue dir around in kernfs_new_node().
2374 * In order to avoid this happening, we also should make sure that
2375 * kernfs_node for glue_dir is released in CPU1 only when refcount
2376 * for glue_dir kobj is 1.
2378 ref = kref_read(&glue_dir->kref);
2379 if (!kobject_has_children(glue_dir) && !--ref)
2380 kobject_del(glue_dir);
2381 kobject_put(glue_dir);
2382 mutex_unlock(&gdp_mutex);
2385 static int device_add_class_symlinks(struct device *dev)
2387 struct device_node *of_node = dev_of_node(dev);
2391 error = sysfs_create_link(&dev->kobj, of_node_kobj(of_node), "of_node");
2393 dev_warn(dev, "Error %d creating of_node link\n",error);
2394 /* An error here doesn't warrant bringing down the device */
2400 error = sysfs_create_link(&dev->kobj,
2401 &dev->class->p->subsys.kobj,
2406 if (dev->parent && device_is_not_partition(dev)) {
2407 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
2414 /* /sys/block has directories and does not need symlinks */
2415 if (sysfs_deprecated && dev->class == &block_class)
2419 /* link in the class directory pointing to the device */
2420 error = sysfs_create_link(&dev->class->p->subsys.kobj,
2421 &dev->kobj, dev_name(dev));
2428 sysfs_remove_link(&dev->kobj, "device");
2431 sysfs_remove_link(&dev->kobj, "subsystem");
2433 sysfs_remove_link(&dev->kobj, "of_node");
2437 static void device_remove_class_symlinks(struct device *dev)
2439 if (dev_of_node(dev))
2440 sysfs_remove_link(&dev->kobj, "of_node");
2445 if (dev->parent && device_is_not_partition(dev))
2446 sysfs_remove_link(&dev->kobj, "device");
2447 sysfs_remove_link(&dev->kobj, "subsystem");
2449 if (sysfs_deprecated && dev->class == &block_class)
2452 sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
2456 * dev_set_name - set a device name
2458 * @fmt: format string for the device's name
2460 int dev_set_name(struct device *dev, const char *fmt, ...)
2465 va_start(vargs, fmt);
2466 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
2470 EXPORT_SYMBOL_GPL(dev_set_name);
2473 * device_to_dev_kobj - select a /sys/dev/ directory for the device
2476 * By default we select char/ for new entries. Setting class->dev_obj
2477 * to NULL prevents an entry from being created. class->dev_kobj must
2478 * be set (or cleared) before any devices are registered to the class
2479 * otherwise device_create_sys_dev_entry() and
2480 * device_remove_sys_dev_entry() will disagree about the presence of
2483 static struct kobject *device_to_dev_kobj(struct device *dev)
2485 struct kobject *kobj;
2488 kobj = dev->class->dev_kobj;
2490 kobj = sysfs_dev_char_kobj;
2495 static int device_create_sys_dev_entry(struct device *dev)
2497 struct kobject *kobj = device_to_dev_kobj(dev);
2502 format_dev_t(devt_str, dev->devt);
2503 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
2509 static void device_remove_sys_dev_entry(struct device *dev)
2511 struct kobject *kobj = device_to_dev_kobj(dev);
2515 format_dev_t(devt_str, dev->devt);
2516 sysfs_remove_link(kobj, devt_str);
2520 static int device_private_init(struct device *dev)
2522 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
2525 dev->p->device = dev;
2526 klist_init(&dev->p->klist_children, klist_children_get,
2527 klist_children_put);
2528 INIT_LIST_HEAD(&dev->p->deferred_probe);
2533 * device_add - add device to device hierarchy.
2536 * This is part 2 of device_register(), though may be called
2537 * separately _iff_ device_initialize() has been called separately.
2539 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
2540 * to the global and sibling lists for the device, then
2541 * adds it to the other relevant subsystems of the driver model.
2543 * Do not call this routine or device_register() more than once for
2544 * any device structure. The driver model core is not designed to work
2545 * with devices that get unregistered and then spring back to life.
2546 * (Among other things, it's very hard to guarantee that all references
2547 * to the previous incarnation of @dev have been dropped.) Allocate
2548 * and register a fresh new struct device instead.
2550 * NOTE: _Never_ directly free @dev after calling this function, even
2551 * if it returned an error! Always use put_device() to give up your
2552 * reference instead.
2554 * Rule of thumb is: if device_add() succeeds, you should call
2555 * device_del() when you want to get rid of it. If device_add() has
2556 * *not* succeeded, use *only* put_device() to drop the reference
2559 int device_add(struct device *dev)
2561 struct device *parent;
2562 struct kobject *kobj;
2563 struct class_interface *class_intf;
2564 int error = -EINVAL;
2565 struct kobject *glue_dir = NULL;
2567 dev = get_device(dev);
2572 error = device_private_init(dev);
2578 * for statically allocated devices, which should all be converted
2579 * some day, we need to initialize the name. We prevent reading back
2580 * the name, and force the use of dev_name()
2582 if (dev->init_name) {
2583 dev_set_name(dev, "%s", dev->init_name);
2584 dev->init_name = NULL;
2587 /* subsystems can specify simple device enumeration */
2588 if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
2589 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
2591 if (!dev_name(dev)) {
2596 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
2598 parent = get_device(dev->parent);
2599 kobj = get_device_parent(dev, parent);
2601 error = PTR_ERR(kobj);
2605 dev->kobj.parent = kobj;
2607 /* use parent numa_node */
2608 if (parent && (dev_to_node(dev) == NUMA_NO_NODE))
2609 set_dev_node(dev, dev_to_node(parent));
2611 /* first, register with generic layer. */
2612 /* we require the name to be set before, and pass NULL */
2613 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
2615 glue_dir = get_glue_dir(dev);
2619 /* notify platform of device entry */
2620 error = device_platform_notify(dev, KOBJ_ADD);
2622 goto platform_error;
2624 error = device_create_file(dev, &dev_attr_uevent);
2628 error = device_add_class_symlinks(dev);
2631 error = device_add_attrs(dev);
2634 error = bus_add_device(dev);
2637 error = dpm_sysfs_add(dev);
2642 if (MAJOR(dev->devt)) {
2643 error = device_create_file(dev, &dev_attr_dev);
2647 error = device_create_sys_dev_entry(dev);
2651 devtmpfs_create_node(dev);
2654 /* Notify clients of device addition. This call must come
2655 * after dpm_sysfs_add() and before kobject_uevent().
2658 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2659 BUS_NOTIFY_ADD_DEVICE, dev);
2661 kobject_uevent(&dev->kobj, KOBJ_ADD);
2664 * Check if any of the other devices (consumers) have been waiting for
2665 * this device (supplier) to be added so that they can create a device
2668 * This needs to happen after device_pm_add() because device_link_add()
2669 * requires the supplier be registered before it's called.
2671 * But this also needs to happen before bus_probe_device() to make sure
2672 * waiting consumers can link to it before the driver is bound to the
2673 * device and the driver sync_state callback is called for this device.
2675 if (dev->fwnode && !dev->fwnode->dev) {
2676 dev->fwnode->dev = dev;
2677 fw_devlink_link_device(dev);
2680 bus_probe_device(dev);
2682 klist_add_tail(&dev->p->knode_parent,
2683 &parent->p->klist_children);
2686 mutex_lock(&dev->class->p->mutex);
2687 /* tie the class to the device */
2688 klist_add_tail(&dev->p->knode_class,
2689 &dev->class->p->klist_devices);
2691 /* notify any interfaces that the device is here */
2692 list_for_each_entry(class_intf,
2693 &dev->class->p->interfaces, node)
2694 if (class_intf->add_dev)
2695 class_intf->add_dev(dev, class_intf);
2696 mutex_unlock(&dev->class->p->mutex);
2702 if (MAJOR(dev->devt))
2703 device_remove_file(dev, &dev_attr_dev);
2705 device_pm_remove(dev);
2706 dpm_sysfs_remove(dev);
2708 bus_remove_device(dev);
2710 device_remove_attrs(dev);
2712 device_remove_class_symlinks(dev);
2714 device_remove_file(dev, &dev_attr_uevent);
2716 device_platform_notify(dev, KOBJ_REMOVE);
2718 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
2719 glue_dir = get_glue_dir(dev);
2720 kobject_del(&dev->kobj);
2722 cleanup_glue_dir(dev, glue_dir);
2730 EXPORT_SYMBOL_GPL(device_add);
2733 * device_register - register a device with the system.
2734 * @dev: pointer to the device structure
2736 * This happens in two clean steps - initialize the device
2737 * and add it to the system. The two steps can be called
2738 * separately, but this is the easiest and most common.
2739 * I.e. you should only call the two helpers separately if
2740 * have a clearly defined need to use and refcount the device
2741 * before it is added to the hierarchy.
2743 * For more information, see the kerneldoc for device_initialize()
2746 * NOTE: _Never_ directly free @dev after calling this function, even
2747 * if it returned an error! Always use put_device() to give up the
2748 * reference initialized in this function instead.
2750 int device_register(struct device *dev)
2752 device_initialize(dev);
2753 return device_add(dev);
2755 EXPORT_SYMBOL_GPL(device_register);
2758 * get_device - increment reference count for device.
2761 * This simply forwards the call to kobject_get(), though
2762 * we do take care to provide for the case that we get a NULL
2763 * pointer passed in.
2765 struct device *get_device(struct device *dev)
2767 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
2769 EXPORT_SYMBOL_GPL(get_device);
2772 * put_device - decrement reference count.
2773 * @dev: device in question.
2775 void put_device(struct device *dev)
2777 /* might_sleep(); */
2779 kobject_put(&dev->kobj);
2781 EXPORT_SYMBOL_GPL(put_device);
2783 bool kill_device(struct device *dev)
2786 * Require the device lock and set the "dead" flag to guarantee that
2787 * the update behavior is consistent with the other bitfields near
2788 * it and that we cannot have an asynchronous probe routine trying
2789 * to run while we are tearing out the bus/class/sysfs from
2790 * underneath the device.
2792 lockdep_assert_held(&dev->mutex);
2796 dev->p->dead = true;
2799 EXPORT_SYMBOL_GPL(kill_device);
2802 * device_del - delete device from system.
2805 * This is the first part of the device unregistration
2806 * sequence. This removes the device from the lists we control
2807 * from here, has it removed from the other driver model
2808 * subsystems it was added to in device_add(), and removes it
2809 * from the kobject hierarchy.
2811 * NOTE: this should be called manually _iff_ device_add() was
2812 * also called manually.
2814 void device_del(struct device *dev)
2816 struct device *parent = dev->parent;
2817 struct kobject *glue_dir = NULL;
2818 struct class_interface *class_intf;
2824 if (dev->fwnode && dev->fwnode->dev == dev)
2825 dev->fwnode->dev = NULL;
2827 /* Notify clients of device removal. This call must come
2828 * before dpm_sysfs_remove().
2831 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2832 BUS_NOTIFY_DEL_DEVICE, dev);
2834 dpm_sysfs_remove(dev);
2836 klist_del(&dev->p->knode_parent);
2837 if (MAJOR(dev->devt)) {
2838 devtmpfs_delete_node(dev);
2839 device_remove_sys_dev_entry(dev);
2840 device_remove_file(dev, &dev_attr_dev);
2843 device_remove_class_symlinks(dev);
2845 mutex_lock(&dev->class->p->mutex);
2846 /* notify any interfaces that the device is now gone */
2847 list_for_each_entry(class_intf,
2848 &dev->class->p->interfaces, node)
2849 if (class_intf->remove_dev)
2850 class_intf->remove_dev(dev, class_intf);
2851 /* remove the device from the class list */
2852 klist_del(&dev->p->knode_class);
2853 mutex_unlock(&dev->class->p->mutex);
2855 device_remove_file(dev, &dev_attr_uevent);
2856 device_remove_attrs(dev);
2857 bus_remove_device(dev);
2858 device_pm_remove(dev);
2859 driver_deferred_probe_del(dev);
2860 device_platform_notify(dev, KOBJ_REMOVE);
2861 device_remove_properties(dev);
2862 device_links_purge(dev);
2865 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2866 BUS_NOTIFY_REMOVED_DEVICE, dev);
2867 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
2868 glue_dir = get_glue_dir(dev);
2869 kobject_del(&dev->kobj);
2870 cleanup_glue_dir(dev, glue_dir);
2873 EXPORT_SYMBOL_GPL(device_del);
2876 * device_unregister - unregister device from system.
2877 * @dev: device going away.
2879 * We do this in two parts, like we do device_register(). First,
2880 * we remove it from all the subsystems with device_del(), then
2881 * we decrement the reference count via put_device(). If that
2882 * is the final reference count, the device will be cleaned up
2883 * via device_release() above. Otherwise, the structure will
2884 * stick around until the final reference to the device is dropped.
2886 void device_unregister(struct device *dev)
2888 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
2892 EXPORT_SYMBOL_GPL(device_unregister);
2894 static struct device *prev_device(struct klist_iter *i)
2896 struct klist_node *n = klist_prev(i);
2897 struct device *dev = NULL;
2898 struct device_private *p;
2901 p = to_device_private_parent(n);
2907 static struct device *next_device(struct klist_iter *i)
2909 struct klist_node *n = klist_next(i);
2910 struct device *dev = NULL;
2911 struct device_private *p;
2914 p = to_device_private_parent(n);
2921 * device_get_devnode - path of device node file
2923 * @mode: returned file access mode
2924 * @uid: returned file owner
2925 * @gid: returned file group
2926 * @tmp: possibly allocated string
2928 * Return the relative path of a possible device node.
2929 * Non-default names may need to allocate a memory to compose
2930 * a name. This memory is returned in tmp and needs to be
2931 * freed by the caller.
2933 const char *device_get_devnode(struct device *dev,
2934 umode_t *mode, kuid_t *uid, kgid_t *gid,
2941 /* the device type may provide a specific name */
2942 if (dev->type && dev->type->devnode)
2943 *tmp = dev->type->devnode(dev, mode, uid, gid);
2947 /* the class may provide a specific name */
2948 if (dev->class && dev->class->devnode)
2949 *tmp = dev->class->devnode(dev, mode);
2953 /* return name without allocation, tmp == NULL */
2954 if (strchr(dev_name(dev), '!') == NULL)
2955 return dev_name(dev);
2957 /* replace '!' in the name with '/' */
2958 s = kstrdup(dev_name(dev), GFP_KERNEL);
2961 strreplace(s, '!', '/');
2966 * device_for_each_child - device child iterator.
2967 * @parent: parent struct device.
2968 * @fn: function to be called for each device.
2969 * @data: data for the callback.
2971 * Iterate over @parent's child devices, and call @fn for each,
2974 * We check the return of @fn each time. If it returns anything
2975 * other than 0, we break out and return that value.
2977 int device_for_each_child(struct device *parent, void *data,
2978 int (*fn)(struct device *dev, void *data))
2980 struct klist_iter i;
2981 struct device *child;
2987 klist_iter_init(&parent->p->klist_children, &i);
2988 while (!error && (child = next_device(&i)))
2989 error = fn(child, data);
2990 klist_iter_exit(&i);
2993 EXPORT_SYMBOL_GPL(device_for_each_child);
2996 * device_for_each_child_reverse - device child iterator in reversed order.
2997 * @parent: parent struct device.
2998 * @fn: function to be called for each device.
2999 * @data: data for the callback.
3001 * Iterate over @parent's child devices, and call @fn for each,
3004 * We check the return of @fn each time. If it returns anything
3005 * other than 0, we break out and return that value.
3007 int device_for_each_child_reverse(struct device *parent, void *data,
3008 int (*fn)(struct device *dev, void *data))
3010 struct klist_iter i;
3011 struct device *child;
3017 klist_iter_init(&parent->p->klist_children, &i);
3018 while ((child = prev_device(&i)) && !error)
3019 error = fn(child, data);
3020 klist_iter_exit(&i);
3023 EXPORT_SYMBOL_GPL(device_for_each_child_reverse);
3026 * device_find_child - device iterator for locating a particular device.
3027 * @parent: parent struct device
3028 * @match: Callback function to check device
3029 * @data: Data to pass to match function
3031 * This is similar to the device_for_each_child() function above, but it
3032 * returns a reference to a device that is 'found' for later use, as
3033 * determined by the @match callback.
3035 * The callback should return 0 if the device doesn't match and non-zero
3036 * if it does. If the callback returns non-zero and a reference to the
3037 * current device can be obtained, this function will return to the caller
3038 * and not iterate over any more devices.
3040 * NOTE: you will need to drop the reference with put_device() after use.
3042 struct device *device_find_child(struct device *parent, void *data,
3043 int (*match)(struct device *dev, void *data))
3045 struct klist_iter i;
3046 struct device *child;
3051 klist_iter_init(&parent->p->klist_children, &i);
3052 while ((child = next_device(&i)))
3053 if (match(child, data) && get_device(child))
3055 klist_iter_exit(&i);
3058 EXPORT_SYMBOL_GPL(device_find_child);
3061 * device_find_child_by_name - device iterator for locating a child device.
3062 * @parent: parent struct device
3063 * @name: name of the child device
3065 * This is similar to the device_find_child() function above, but it
3066 * returns a reference to a device that has the name @name.
3068 * NOTE: you will need to drop the reference with put_device() after use.
3070 struct device *device_find_child_by_name(struct device *parent,
3073 struct klist_iter i;
3074 struct device *child;
3079 klist_iter_init(&parent->p->klist_children, &i);
3080 while ((child = next_device(&i)))
3081 if (!strcmp(dev_name(child), name) && get_device(child))
3083 klist_iter_exit(&i);
3086 EXPORT_SYMBOL_GPL(device_find_child_by_name);
3088 int __init devices_init(void)
3090 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
3093 dev_kobj = kobject_create_and_add("dev", NULL);
3096 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
3097 if (!sysfs_dev_block_kobj)
3098 goto block_kobj_err;
3099 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
3100 if (!sysfs_dev_char_kobj)
3106 kobject_put(sysfs_dev_block_kobj);
3108 kobject_put(dev_kobj);
3110 kset_unregister(devices_kset);
3114 static int device_check_offline(struct device *dev, void *not_used)
3118 ret = device_for_each_child(dev, NULL, device_check_offline);
3122 return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
3126 * device_offline - Prepare the device for hot-removal.
3127 * @dev: Device to be put offline.
3129 * Execute the device bus type's .offline() callback, if present, to prepare
3130 * the device for a subsequent hot-removal. If that succeeds, the device must
3131 * not be used until either it is removed or its bus type's .online() callback
3134 * Call under device_hotplug_lock.
3136 int device_offline(struct device *dev)
3140 if (dev->offline_disabled)
3143 ret = device_for_each_child(dev, NULL, device_check_offline);
3148 if (device_supports_offline(dev)) {
3152 ret = dev->bus->offline(dev);
3154 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
3155 dev->offline = true;
3165 * device_online - Put the device back online after successful device_offline().
3166 * @dev: Device to be put back online.
3168 * If device_offline() has been successfully executed for @dev, but the device
3169 * has not been removed subsequently, execute its bus type's .online() callback
3170 * to indicate that the device can be used again.
3172 * Call under device_hotplug_lock.
3174 int device_online(struct device *dev)
3179 if (device_supports_offline(dev)) {
3181 ret = dev->bus->online(dev);
3183 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
3184 dev->offline = false;
3195 struct root_device {
3197 struct module *owner;
3200 static inline struct root_device *to_root_device(struct device *d)
3202 return container_of(d, struct root_device, dev);
3205 static void root_device_release(struct device *dev)
3207 kfree(to_root_device(dev));
3211 * __root_device_register - allocate and register a root device
3212 * @name: root device name
3213 * @owner: owner module of the root device, usually THIS_MODULE
3215 * This function allocates a root device and registers it
3216 * using device_register(). In order to free the returned
3217 * device, use root_device_unregister().
3219 * Root devices are dummy devices which allow other devices
3220 * to be grouped under /sys/devices. Use this function to
3221 * allocate a root device and then use it as the parent of
3222 * any device which should appear under /sys/devices/{name}
3224 * The /sys/devices/{name} directory will also contain a
3225 * 'module' symlink which points to the @owner directory
3228 * Returns &struct device pointer on success, or ERR_PTR() on error.
3230 * Note: You probably want to use root_device_register().
3232 struct device *__root_device_register(const char *name, struct module *owner)
3234 struct root_device *root;
3237 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
3239 return ERR_PTR(err);
3241 err = dev_set_name(&root->dev, "%s", name);
3244 return ERR_PTR(err);
3247 root->dev.release = root_device_release;
3249 err = device_register(&root->dev);
3251 put_device(&root->dev);
3252 return ERR_PTR(err);
3255 #ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
3257 struct module_kobject *mk = &owner->mkobj;
3259 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
3261 device_unregister(&root->dev);
3262 return ERR_PTR(err);
3264 root->owner = owner;
3270 EXPORT_SYMBOL_GPL(__root_device_register);
3273 * root_device_unregister - unregister and free a root device
3274 * @dev: device going away
3276 * This function unregisters and cleans up a device that was created by
3277 * root_device_register().
3279 void root_device_unregister(struct device *dev)
3281 struct root_device *root = to_root_device(dev);
3284 sysfs_remove_link(&root->dev.kobj, "module");
3286 device_unregister(dev);
3288 EXPORT_SYMBOL_GPL(root_device_unregister);
3291 static void device_create_release(struct device *dev)
3293 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
3297 static __printf(6, 0) struct device *
3298 device_create_groups_vargs(struct class *class, struct device *parent,
3299 dev_t devt, void *drvdata,
3300 const struct attribute_group **groups,
3301 const char *fmt, va_list args)
3303 struct device *dev = NULL;
3304 int retval = -ENODEV;
3306 if (class == NULL || IS_ERR(class))
3309 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
3315 device_initialize(dev);
3318 dev->parent = parent;
3319 dev->groups = groups;
3320 dev->release = device_create_release;
3321 dev_set_drvdata(dev, drvdata);
3323 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
3327 retval = device_add(dev);
3335 return ERR_PTR(retval);
3339 * device_create - creates a device and registers it with sysfs
3340 * @class: pointer to the struct class that this device should be registered to
3341 * @parent: pointer to the parent struct device of this new device, if any
3342 * @devt: the dev_t for the char device to be added
3343 * @drvdata: the data to be added to the device for callbacks
3344 * @fmt: string for the device's name
3346 * This function can be used by char device classes. A struct device
3347 * will be created in sysfs, registered to the specified class.
3349 * A "dev" file will be created, showing the dev_t for the device, if
3350 * the dev_t is not 0,0.
3351 * If a pointer to a parent struct device is passed in, the newly created
3352 * struct device will be a child of that device in sysfs.
3353 * The pointer to the struct device will be returned from the call.
3354 * Any further sysfs files that might be required can be created using this
3357 * Returns &struct device pointer on success, or ERR_PTR() on error.
3359 * Note: the struct class passed to this function must have previously
3360 * been created with a call to class_create().
3362 struct device *device_create(struct class *class, struct device *parent,
3363 dev_t devt, void *drvdata, const char *fmt, ...)
3368 va_start(vargs, fmt);
3369 dev = device_create_groups_vargs(class, parent, devt, drvdata, NULL,
3374 EXPORT_SYMBOL_GPL(device_create);
3377 * device_create_with_groups - creates a device and registers it with sysfs
3378 * @class: pointer to the struct class that this device should be registered to
3379 * @parent: pointer to the parent struct device of this new device, if any
3380 * @devt: the dev_t for the char device to be added
3381 * @drvdata: the data to be added to the device for callbacks
3382 * @groups: NULL-terminated list of attribute groups to be created
3383 * @fmt: string for the device's name
3385 * This function can be used by char device classes. A struct device
3386 * will be created in sysfs, registered to the specified class.
3387 * Additional attributes specified in the groups parameter will also
3388 * be created automatically.
3390 * A "dev" file will be created, showing the dev_t for the device, if
3391 * the dev_t is not 0,0.
3392 * If a pointer to a parent struct device is passed in, the newly created
3393 * struct device will be a child of that device in sysfs.
3394 * The pointer to the struct device will be returned from the call.
3395 * Any further sysfs files that might be required can be created using this
3398 * Returns &struct device pointer on success, or ERR_PTR() on error.
3400 * Note: the struct class passed to this function must have previously
3401 * been created with a call to class_create().
3403 struct device *device_create_with_groups(struct class *class,
3404 struct device *parent, dev_t devt,
3406 const struct attribute_group **groups,
3407 const char *fmt, ...)
3412 va_start(vargs, fmt);
3413 dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
3418 EXPORT_SYMBOL_GPL(device_create_with_groups);
3421 * device_destroy - removes a device that was created with device_create()
3422 * @class: pointer to the struct class that this device was registered with
3423 * @devt: the dev_t of the device that was previously registered
3425 * This call unregisters and cleans up a device that was created with a
3426 * call to device_create().
3428 void device_destroy(struct class *class, dev_t devt)
3432 dev = class_find_device_by_devt(class, devt);
3435 device_unregister(dev);
3438 EXPORT_SYMBOL_GPL(device_destroy);
3441 * device_rename - renames a device
3442 * @dev: the pointer to the struct device to be renamed
3443 * @new_name: the new name of the device
3445 * It is the responsibility of the caller to provide mutual
3446 * exclusion between two different calls of device_rename
3447 * on the same device to ensure that new_name is valid and
3448 * won't conflict with other devices.
3450 * Note: Don't call this function. Currently, the networking layer calls this
3451 * function, but that will change. The following text from Kay Sievers offers
3454 * Renaming devices is racy at many levels, symlinks and other stuff are not
3455 * replaced atomically, and you get a "move" uevent, but it's not easy to
3456 * connect the event to the old and new device. Device nodes are not renamed at
3457 * all, there isn't even support for that in the kernel now.
3459 * In the meantime, during renaming, your target name might be taken by another
3460 * driver, creating conflicts. Or the old name is taken directly after you
3461 * renamed it -- then you get events for the same DEVPATH, before you even see
3462 * the "move" event. It's just a mess, and nothing new should ever rely on
3463 * kernel device renaming. Besides that, it's not even implemented now for
3464 * other things than (driver-core wise very simple) network devices.
3466 * We are currently about to change network renaming in udev to completely
3467 * disallow renaming of devices in the same namespace as the kernel uses,
3468 * because we can't solve the problems properly, that arise with swapping names
3469 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
3470 * be allowed to some other name than eth[0-9]*, for the aforementioned
3473 * Make up a "real" name in the driver before you register anything, or add
3474 * some other attributes for userspace to find the device, or use udev to add
3475 * symlinks -- but never rename kernel devices later, it's a complete mess. We
3476 * don't even want to get into that and try to implement the missing pieces in
3477 * the core. We really have other pieces to fix in the driver core mess. :)
3479 int device_rename(struct device *dev, const char *new_name)
3481 struct kobject *kobj = &dev->kobj;
3482 char *old_device_name = NULL;
3485 dev = get_device(dev);
3489 dev_dbg(dev, "renaming to %s\n", new_name);
3491 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
3492 if (!old_device_name) {
3498 error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj,
3499 kobj, old_device_name,
3500 new_name, kobject_namespace(kobj));
3505 error = kobject_rename(kobj, new_name);
3512 kfree(old_device_name);
3516 EXPORT_SYMBOL_GPL(device_rename);
3518 static int device_move_class_links(struct device *dev,
3519 struct device *old_parent,
3520 struct device *new_parent)
3525 sysfs_remove_link(&dev->kobj, "device");
3527 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
3533 * device_move - moves a device to a new parent
3534 * @dev: the pointer to the struct device to be moved
3535 * @new_parent: the new parent of the device (can be NULL)
3536 * @dpm_order: how to reorder the dpm_list
3538 int device_move(struct device *dev, struct device *new_parent,
3539 enum dpm_order dpm_order)
3542 struct device *old_parent;
3543 struct kobject *new_parent_kobj;
3545 dev = get_device(dev);
3550 new_parent = get_device(new_parent);
3551 new_parent_kobj = get_device_parent(dev, new_parent);
3552 if (IS_ERR(new_parent_kobj)) {
3553 error = PTR_ERR(new_parent_kobj);
3554 put_device(new_parent);
3558 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
3559 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
3560 error = kobject_move(&dev->kobj, new_parent_kobj);
3562 cleanup_glue_dir(dev, new_parent_kobj);
3563 put_device(new_parent);
3566 old_parent = dev->parent;
3567 dev->parent = new_parent;
3569 klist_remove(&dev->p->knode_parent);
3571 klist_add_tail(&dev->p->knode_parent,
3572 &new_parent->p->klist_children);
3573 set_dev_node(dev, dev_to_node(new_parent));
3577 error = device_move_class_links(dev, old_parent, new_parent);
3579 /* We ignore errors on cleanup since we're hosed anyway... */
3580 device_move_class_links(dev, new_parent, old_parent);
3581 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
3583 klist_remove(&dev->p->knode_parent);
3584 dev->parent = old_parent;
3586 klist_add_tail(&dev->p->knode_parent,
3587 &old_parent->p->klist_children);
3588 set_dev_node(dev, dev_to_node(old_parent));
3591 cleanup_glue_dir(dev, new_parent_kobj);
3592 put_device(new_parent);
3596 switch (dpm_order) {
3597 case DPM_ORDER_NONE:
3599 case DPM_ORDER_DEV_AFTER_PARENT:
3600 device_pm_move_after(dev, new_parent);
3601 devices_kset_move_after(dev, new_parent);
3603 case DPM_ORDER_PARENT_BEFORE_DEV:
3604 device_pm_move_before(new_parent, dev);
3605 devices_kset_move_before(new_parent, dev);
3607 case DPM_ORDER_DEV_LAST:
3608 device_pm_move_last(dev);
3609 devices_kset_move_last(dev);
3613 put_device(old_parent);
3619 EXPORT_SYMBOL_GPL(device_move);
3621 static int device_attrs_change_owner(struct device *dev, kuid_t kuid,
3624 struct kobject *kobj = &dev->kobj;
3625 struct class *class = dev->class;
3626 const struct device_type *type = dev->type;
3631 * Change the device groups of the device class for @dev to
3634 error = sysfs_groups_change_owner(kobj, class->dev_groups, kuid,
3642 * Change the device groups of the device type for @dev to
3645 error = sysfs_groups_change_owner(kobj, type->groups, kuid,
3651 /* Change the device groups of @dev to @kuid/@kgid. */
3652 error = sysfs_groups_change_owner(kobj, dev->groups, kuid, kgid);
3656 if (device_supports_offline(dev) && !dev->offline_disabled) {
3657 /* Change online device attributes of @dev to @kuid/@kgid. */
3658 error = sysfs_file_change_owner(kobj, dev_attr_online.attr.name,
3668 * device_change_owner - change the owner of an existing device.
3670 * @kuid: new owner's kuid
3671 * @kgid: new owner's kgid
3673 * This changes the owner of @dev and its corresponding sysfs entries to
3674 * @kuid/@kgid. This function closely mirrors how @dev was added via driver
3677 * Returns 0 on success or error code on failure.
3679 int device_change_owner(struct device *dev, kuid_t kuid, kgid_t kgid)
3682 struct kobject *kobj = &dev->kobj;
3684 dev = get_device(dev);
3689 * Change the kobject and the default attributes and groups of the
3690 * ktype associated with it to @kuid/@kgid.
3692 error = sysfs_change_owner(kobj, kuid, kgid);
3697 * Change the uevent file for @dev to the new owner. The uevent file
3698 * was created in a separate step when @dev got added and we mirror
3701 error = sysfs_file_change_owner(kobj, dev_attr_uevent.attr.name, kuid,
3707 * Change the device groups, the device groups associated with the
3708 * device class, and the groups associated with the device type of @dev
3711 error = device_attrs_change_owner(dev, kuid, kgid);
3715 error = dpm_sysfs_change_owner(dev, kuid, kgid);
3720 if (sysfs_deprecated && dev->class == &block_class)
3725 * Change the owner of the symlink located in the class directory of
3726 * the device class associated with @dev which points to the actual
3727 * directory entry for @dev to @kuid/@kgid. This ensures that the
3728 * symlink shows the same permissions as its target.
3730 error = sysfs_link_change_owner(&dev->class->p->subsys.kobj, &dev->kobj,
3731 dev_name(dev), kuid, kgid);
3739 EXPORT_SYMBOL_GPL(device_change_owner);
3742 * device_shutdown - call ->shutdown() on each device to shutdown.
3744 void device_shutdown(void)
3746 struct device *dev, *parent;
3748 wait_for_device_probe();
3749 device_block_probing();
3753 spin_lock(&devices_kset->list_lock);
3755 * Walk the devices list backward, shutting down each in turn.
3756 * Beware that device unplug events may also start pulling
3757 * devices offline, even as the system is shutting down.
3759 while (!list_empty(&devices_kset->list)) {
3760 dev = list_entry(devices_kset->list.prev, struct device,
3764 * hold reference count of device's parent to
3765 * prevent it from being freed because parent's
3766 * lock is to be held
3768 parent = get_device(dev->parent);
3771 * Make sure the device is off the kset list, in the
3772 * event that dev->*->shutdown() doesn't remove it.
3774 list_del_init(&dev->kobj.entry);
3775 spin_unlock(&devices_kset->list_lock);
3777 /* hold lock to avoid race with probe/release */
3779 device_lock(parent);
3782 /* Don't allow any more runtime suspends */
3783 pm_runtime_get_noresume(dev);
3784 pm_runtime_barrier(dev);
3786 if (dev->class && dev->class->shutdown_pre) {
3788 dev_info(dev, "shutdown_pre\n");
3789 dev->class->shutdown_pre(dev);
3791 if (dev->bus && dev->bus->shutdown) {
3793 dev_info(dev, "shutdown\n");
3794 dev->bus->shutdown(dev);
3795 } else if (dev->driver && dev->driver->shutdown) {
3797 dev_info(dev, "shutdown\n");
3798 dev->driver->shutdown(dev);
3803 device_unlock(parent);
3808 spin_lock(&devices_kset->list_lock);
3810 spin_unlock(&devices_kset->list_lock);
3814 * Device logging functions
3817 #ifdef CONFIG_PRINTK
3819 create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
3825 subsys = dev->class->name;
3827 subsys = dev->bus->name;
3831 pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys);
3836 * Add device identifier DEVICE=:
3840 * +sound:card0 subsystem:devname
3842 if (MAJOR(dev->devt)) {
3845 if (strcmp(subsys, "block") == 0)
3850 pos += snprintf(hdr + pos, hdrlen - pos,
3852 c, MAJOR(dev->devt), MINOR(dev->devt));
3853 } else if (strcmp(subsys, "net") == 0) {
3854 struct net_device *net = to_net_dev(dev);
3857 pos += snprintf(hdr + pos, hdrlen - pos,
3858 "DEVICE=n%u", net->ifindex);
3861 pos += snprintf(hdr + pos, hdrlen - pos,
3862 "DEVICE=+%s:%s", subsys, dev_name(dev));
3871 dev_WARN(dev, "device/subsystem name too long");
3875 int dev_vprintk_emit(int level, const struct device *dev,
3876 const char *fmt, va_list args)
3881 hdrlen = create_syslog_header(dev, hdr, sizeof(hdr));
3883 return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args);
3885 EXPORT_SYMBOL(dev_vprintk_emit);
3887 int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
3892 va_start(args, fmt);
3894 r = dev_vprintk_emit(level, dev, fmt, args);
3900 EXPORT_SYMBOL(dev_printk_emit);
3902 static void __dev_printk(const char *level, const struct device *dev,
3903 struct va_format *vaf)
3906 dev_printk_emit(level[1] - '0', dev, "%s %s: %pV",
3907 dev_driver_string(dev), dev_name(dev), vaf);
3909 printk("%s(NULL device *): %pV", level, vaf);
3912 void dev_printk(const char *level, const struct device *dev,
3913 const char *fmt, ...)
3915 struct va_format vaf;
3918 va_start(args, fmt);
3923 __dev_printk(level, dev, &vaf);
3927 EXPORT_SYMBOL(dev_printk);
3929 #define define_dev_printk_level(func, kern_level) \
3930 void func(const struct device *dev, const char *fmt, ...) \
3932 struct va_format vaf; \
3935 va_start(args, fmt); \
3940 __dev_printk(kern_level, dev, &vaf); \
3944 EXPORT_SYMBOL(func);
3946 define_dev_printk_level(_dev_emerg, KERN_EMERG);
3947 define_dev_printk_level(_dev_alert, KERN_ALERT);
3948 define_dev_printk_level(_dev_crit, KERN_CRIT);
3949 define_dev_printk_level(_dev_err, KERN_ERR);
3950 define_dev_printk_level(_dev_warn, KERN_WARNING);
3951 define_dev_printk_level(_dev_notice, KERN_NOTICE);
3952 define_dev_printk_level(_dev_info, KERN_INFO);
3956 static inline bool fwnode_is_primary(struct fwnode_handle *fwnode)
3958 return fwnode && !IS_ERR(fwnode->secondary);
3962 * set_primary_fwnode - Change the primary firmware node of a given device.
3963 * @dev: Device to handle.
3964 * @fwnode: New primary firmware node of the device.
3966 * Set the device's firmware node pointer to @fwnode, but if a secondary
3967 * firmware node of the device is present, preserve it.
3969 void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
3972 struct fwnode_handle *fn = dev->fwnode;
3974 if (fwnode_is_primary(fn))
3978 WARN_ON(fwnode->secondary);
3979 fwnode->secondary = fn;
3981 dev->fwnode = fwnode;
3983 dev->fwnode = fwnode_is_primary(dev->fwnode) ?
3984 dev->fwnode->secondary : NULL;
3987 EXPORT_SYMBOL_GPL(set_primary_fwnode);
3990 * set_secondary_fwnode - Change the secondary firmware node of a given device.
3991 * @dev: Device to handle.
3992 * @fwnode: New secondary firmware node of the device.
3994 * If a primary firmware node of the device is present, set its secondary
3995 * pointer to @fwnode. Otherwise, set the device's firmware node pointer to
3998 void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
4001 fwnode->secondary = ERR_PTR(-ENODEV);
4003 if (fwnode_is_primary(dev->fwnode))
4004 dev->fwnode->secondary = fwnode;
4006 dev->fwnode = fwnode;
4008 EXPORT_SYMBOL_GPL(set_secondary_fwnode);
4011 * device_set_of_node_from_dev - reuse device-tree node of another device
4012 * @dev: device whose device-tree node is being set
4013 * @dev2: device whose device-tree node is being reused
4015 * Takes another reference to the new device-tree node after first dropping
4016 * any reference held to the old node.
4018 void device_set_of_node_from_dev(struct device *dev, const struct device *dev2)
4020 of_node_put(dev->of_node);
4021 dev->of_node = of_node_get(dev2->of_node);
4022 dev->of_node_reused = true;
4024 EXPORT_SYMBOL_GPL(device_set_of_node_from_dev);
4026 int device_match_name(struct device *dev, const void *name)
4028 return sysfs_streq(dev_name(dev), name);
4030 EXPORT_SYMBOL_GPL(device_match_name);
4032 int device_match_of_node(struct device *dev, const void *np)
4034 return dev->of_node == np;
4036 EXPORT_SYMBOL_GPL(device_match_of_node);
4038 int device_match_fwnode(struct device *dev, const void *fwnode)
4040 return dev_fwnode(dev) == fwnode;
4042 EXPORT_SYMBOL_GPL(device_match_fwnode);
4044 int device_match_devt(struct device *dev, const void *pdevt)
4046 return dev->devt == *(dev_t *)pdevt;
4048 EXPORT_SYMBOL_GPL(device_match_devt);
4050 int device_match_acpi_dev(struct device *dev, const void *adev)
4052 return ACPI_COMPANION(dev) == adev;
4054 EXPORT_SYMBOL(device_match_acpi_dev);
4056 int device_match_any(struct device *dev, const void *unused)
4060 EXPORT_SYMBOL_GPL(device_match_any);