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 to_devlink(dev) container_of((dev), struct device_link, link_dev)
240 static ssize_t status_show(struct device *dev,
241 struct device_attribute *attr, char *buf)
245 switch (to_devlink(dev)->status) {
247 status = "not tracked"; break;
248 case DL_STATE_DORMANT:
249 status = "dormant"; break;
250 case DL_STATE_AVAILABLE:
251 status = "available"; break;
252 case DL_STATE_CONSUMER_PROBE:
253 status = "consumer probing"; break;
254 case DL_STATE_ACTIVE:
255 status = "active"; break;
256 case DL_STATE_SUPPLIER_UNBIND:
257 status = "supplier unbinding"; break;
259 status = "unknown"; break;
261 return sprintf(buf, "%s\n", status);
263 static DEVICE_ATTR_RO(status);
265 static ssize_t auto_remove_on_show(struct device *dev,
266 struct device_attribute *attr, char *buf)
268 struct device_link *link = to_devlink(dev);
271 if (link->flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
272 str = "supplier unbind";
273 else if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER)
274 str = "consumer unbind";
278 return sprintf(buf, "%s\n", str);
280 static DEVICE_ATTR_RO(auto_remove_on);
282 static ssize_t runtime_pm_show(struct device *dev,
283 struct device_attribute *attr, char *buf)
285 struct device_link *link = to_devlink(dev);
287 return sprintf(buf, "%d\n", !!(link->flags & DL_FLAG_PM_RUNTIME));
289 static DEVICE_ATTR_RO(runtime_pm);
291 static ssize_t sync_state_only_show(struct device *dev,
292 struct device_attribute *attr, char *buf)
294 struct device_link *link = to_devlink(dev);
296 return sprintf(buf, "%d\n", !!(link->flags & DL_FLAG_SYNC_STATE_ONLY));
298 static DEVICE_ATTR_RO(sync_state_only);
300 static struct attribute *devlink_attrs[] = {
301 &dev_attr_status.attr,
302 &dev_attr_auto_remove_on.attr,
303 &dev_attr_runtime_pm.attr,
304 &dev_attr_sync_state_only.attr,
307 ATTRIBUTE_GROUPS(devlink);
309 static void devlink_dev_release(struct device *dev)
311 kfree(to_devlink(dev));
314 static struct class devlink_class = {
316 .owner = THIS_MODULE,
317 .dev_groups = devlink_groups,
318 .dev_release = devlink_dev_release,
321 static int devlink_add_symlinks(struct device *dev,
322 struct class_interface *class_intf)
326 struct device_link *link = to_devlink(dev);
327 struct device *sup = link->supplier;
328 struct device *con = link->consumer;
331 len = max(strlen(dev_name(sup)), strlen(dev_name(con)));
332 len += strlen("supplier:") + 1;
333 buf = kzalloc(len, GFP_KERNEL);
337 ret = sysfs_create_link(&link->link_dev.kobj, &sup->kobj, "supplier");
341 ret = sysfs_create_link(&link->link_dev.kobj, &con->kobj, "consumer");
345 snprintf(buf, len, "consumer:%s", dev_name(con));
346 ret = sysfs_create_link(&sup->kobj, &link->link_dev.kobj, buf);
350 snprintf(buf, len, "supplier:%s", dev_name(sup));
351 ret = sysfs_create_link(&con->kobj, &link->link_dev.kobj, buf);
358 snprintf(buf, len, "consumer:%s", dev_name(con));
359 sysfs_remove_link(&sup->kobj, buf);
361 sysfs_remove_link(&link->link_dev.kobj, "consumer");
363 sysfs_remove_link(&link->link_dev.kobj, "supplier");
369 static void devlink_remove_symlinks(struct device *dev,
370 struct class_interface *class_intf)
372 struct device_link *link = to_devlink(dev);
374 struct device *sup = link->supplier;
375 struct device *con = link->consumer;
378 sysfs_remove_link(&link->link_dev.kobj, "consumer");
379 sysfs_remove_link(&link->link_dev.kobj, "supplier");
381 len = max(strlen(dev_name(sup)), strlen(dev_name(con)));
382 len += strlen("supplier:") + 1;
383 buf = kzalloc(len, GFP_KERNEL);
385 WARN(1, "Unable to properly free device link symlinks!\n");
389 snprintf(buf, len, "supplier:%s", dev_name(sup));
390 sysfs_remove_link(&con->kobj, buf);
391 snprintf(buf, len, "consumer:%s", dev_name(con));
392 sysfs_remove_link(&sup->kobj, buf);
396 static struct class_interface devlink_class_intf = {
397 .class = &devlink_class,
398 .add_dev = devlink_add_symlinks,
399 .remove_dev = devlink_remove_symlinks,
402 static int __init devlink_class_init(void)
406 ret = class_register(&devlink_class);
410 ret = class_interface_register(&devlink_class_intf);
412 class_unregister(&devlink_class);
416 postcore_initcall(devlink_class_init);
418 #define DL_MANAGED_LINK_FLAGS (DL_FLAG_AUTOREMOVE_CONSUMER | \
419 DL_FLAG_AUTOREMOVE_SUPPLIER | \
420 DL_FLAG_AUTOPROBE_CONSUMER | \
421 DL_FLAG_SYNC_STATE_ONLY)
423 #define DL_ADD_VALID_FLAGS (DL_MANAGED_LINK_FLAGS | DL_FLAG_STATELESS | \
424 DL_FLAG_PM_RUNTIME | DL_FLAG_RPM_ACTIVE)
427 * device_link_add - Create a link between two devices.
428 * @consumer: Consumer end of the link.
429 * @supplier: Supplier end of the link.
430 * @flags: Link flags.
432 * The caller is responsible for the proper synchronization of the link creation
433 * with runtime PM. First, setting the DL_FLAG_PM_RUNTIME flag will cause the
434 * runtime PM framework to take the link into account. Second, if the
435 * DL_FLAG_RPM_ACTIVE flag is set in addition to it, the supplier devices will
436 * be forced into the active metastate and reference-counted upon the creation
437 * of the link. If DL_FLAG_PM_RUNTIME is not set, DL_FLAG_RPM_ACTIVE will be
440 * If DL_FLAG_STATELESS is set in @flags, the caller of this function is
441 * expected to release the link returned by it directly with the help of either
442 * device_link_del() or device_link_remove().
444 * If that flag is not set, however, the caller of this function is handing the
445 * management of the link over to the driver core entirely and its return value
446 * can only be used to check whether or not the link is present. In that case,
447 * the DL_FLAG_AUTOREMOVE_CONSUMER and DL_FLAG_AUTOREMOVE_SUPPLIER device link
448 * flags can be used to indicate to the driver core when the link can be safely
449 * deleted. Namely, setting one of them in @flags indicates to the driver core
450 * that the link is not going to be used (by the given caller of this function)
451 * after unbinding the consumer or supplier driver, respectively, from its
452 * device, so the link can be deleted at that point. If none of them is set,
453 * the link will be maintained until one of the devices pointed to by it (either
454 * the consumer or the supplier) is unregistered.
456 * Also, if DL_FLAG_STATELESS, DL_FLAG_AUTOREMOVE_CONSUMER and
457 * DL_FLAG_AUTOREMOVE_SUPPLIER are not set in @flags (that is, a persistent
458 * managed device link is being added), the DL_FLAG_AUTOPROBE_CONSUMER flag can
459 * be used to request the driver core to automaticall probe for a consmer
460 * driver after successfully binding a driver to the supplier device.
462 * The combination of DL_FLAG_STATELESS and one of DL_FLAG_AUTOREMOVE_CONSUMER,
463 * DL_FLAG_AUTOREMOVE_SUPPLIER, or DL_FLAG_AUTOPROBE_CONSUMER set in @flags at
464 * the same time is invalid and will cause NULL to be returned upfront.
465 * However, if a device link between the given @consumer and @supplier pair
466 * exists already when this function is called for them, the existing link will
467 * be returned regardless of its current type and status (the link's flags may
468 * be modified then). The caller of this function is then expected to treat
469 * the link as though it has just been created, so (in particular) if
470 * DL_FLAG_STATELESS was passed in @flags, the link needs to be released
471 * explicitly when not needed any more (as stated above).
473 * A side effect of the link creation is re-ordering of dpm_list and the
474 * devices_kset list by moving the consumer device and all devices depending
475 * on it to the ends of these lists (that does not happen to devices that have
476 * not been registered when this function is called).
478 * The supplier device is required to be registered when this function is called
479 * and NULL will be returned if that is not the case. The consumer device need
480 * not be registered, however.
482 struct device_link *device_link_add(struct device *consumer,
483 struct device *supplier, u32 flags)
485 struct device_link *link;
487 if (!consumer || !supplier || flags & ~DL_ADD_VALID_FLAGS ||
488 (flags & DL_FLAG_STATELESS && flags & DL_MANAGED_LINK_FLAGS) ||
489 (flags & DL_FLAG_SYNC_STATE_ONLY &&
490 flags != DL_FLAG_SYNC_STATE_ONLY) ||
491 (flags & DL_FLAG_AUTOPROBE_CONSUMER &&
492 flags & (DL_FLAG_AUTOREMOVE_CONSUMER |
493 DL_FLAG_AUTOREMOVE_SUPPLIER)))
496 if (flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) {
497 if (pm_runtime_get_sync(supplier) < 0) {
498 pm_runtime_put_noidle(supplier);
503 if (!(flags & DL_FLAG_STATELESS))
504 flags |= DL_FLAG_MANAGED;
506 device_links_write_lock();
510 * If the supplier has not been fully registered yet or there is a
511 * reverse (non-SYNC_STATE_ONLY) dependency between the consumer and
512 * the supplier already in the graph, return NULL. If the link is a
513 * SYNC_STATE_ONLY link, we don't check for reverse dependencies
514 * because it only affects sync_state() callbacks.
516 if (!device_pm_initialized(supplier)
517 || (!(flags & DL_FLAG_SYNC_STATE_ONLY) &&
518 device_is_dependent(consumer, supplier))) {
524 * DL_FLAG_AUTOREMOVE_SUPPLIER indicates that the link will be needed
525 * longer than for DL_FLAG_AUTOREMOVE_CONSUMER and setting them both
526 * together doesn't make sense, so prefer DL_FLAG_AUTOREMOVE_SUPPLIER.
528 if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
529 flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER;
531 list_for_each_entry(link, &supplier->links.consumers, s_node) {
532 if (link->consumer != consumer)
535 if (flags & DL_FLAG_PM_RUNTIME) {
536 if (!(link->flags & DL_FLAG_PM_RUNTIME)) {
537 pm_runtime_new_link(consumer);
538 link->flags |= DL_FLAG_PM_RUNTIME;
540 if (flags & DL_FLAG_RPM_ACTIVE)
541 refcount_inc(&link->rpm_active);
544 if (flags & DL_FLAG_STATELESS) {
545 kref_get(&link->kref);
546 if (link->flags & DL_FLAG_SYNC_STATE_ONLY &&
547 !(link->flags & DL_FLAG_STATELESS)) {
548 link->flags |= DL_FLAG_STATELESS;
551 link->flags |= DL_FLAG_STATELESS;
557 * If the life time of the link following from the new flags is
558 * longer than indicated by the flags of the existing link,
559 * update the existing link to stay around longer.
561 if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER) {
562 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER) {
563 link->flags &= ~DL_FLAG_AUTOREMOVE_CONSUMER;
564 link->flags |= DL_FLAG_AUTOREMOVE_SUPPLIER;
566 } else if (!(flags & DL_FLAG_AUTOREMOVE_CONSUMER)) {
567 link->flags &= ~(DL_FLAG_AUTOREMOVE_CONSUMER |
568 DL_FLAG_AUTOREMOVE_SUPPLIER);
570 if (!(link->flags & DL_FLAG_MANAGED)) {
571 kref_get(&link->kref);
572 link->flags |= DL_FLAG_MANAGED;
573 device_link_init_status(link, consumer, supplier);
575 if (link->flags & DL_FLAG_SYNC_STATE_ONLY &&
576 !(flags & DL_FLAG_SYNC_STATE_ONLY)) {
577 link->flags &= ~DL_FLAG_SYNC_STATE_ONLY;
584 link = kzalloc(sizeof(*link), GFP_KERNEL);
588 refcount_set(&link->rpm_active, 1);
590 get_device(supplier);
591 link->supplier = supplier;
592 INIT_LIST_HEAD(&link->s_node);
593 get_device(consumer);
594 link->consumer = consumer;
595 INIT_LIST_HEAD(&link->c_node);
597 kref_init(&link->kref);
599 link->link_dev.class = &devlink_class;
600 device_set_pm_not_required(&link->link_dev);
601 dev_set_name(&link->link_dev, "%s:%s",
602 dev_name(supplier), dev_name(consumer));
603 if (device_register(&link->link_dev)) {
604 put_device(consumer);
605 put_device(supplier);
611 if (flags & DL_FLAG_PM_RUNTIME) {
612 if (flags & DL_FLAG_RPM_ACTIVE)
613 refcount_inc(&link->rpm_active);
615 pm_runtime_new_link(consumer);
618 /* Determine the initial link state. */
619 if (flags & DL_FLAG_STATELESS)
620 link->status = DL_STATE_NONE;
622 device_link_init_status(link, consumer, supplier);
625 * Some callers expect the link creation during consumer driver probe to
626 * resume the supplier even without DL_FLAG_RPM_ACTIVE.
628 if (link->status == DL_STATE_CONSUMER_PROBE &&
629 flags & DL_FLAG_PM_RUNTIME)
630 pm_runtime_resume(supplier);
632 list_add_tail_rcu(&link->s_node, &supplier->links.consumers);
633 list_add_tail_rcu(&link->c_node, &consumer->links.suppliers);
635 if (flags & DL_FLAG_SYNC_STATE_ONLY) {
637 "Linked as a sync state only consumer to %s\n",
644 * Move the consumer and all of the devices depending on it to the end
645 * of dpm_list and the devices_kset list.
647 * It is necessary to hold dpm_list locked throughout all that or else
648 * we may end up suspending with a wrong ordering of it.
650 device_reorder_to_tail(consumer, NULL);
652 dev_dbg(consumer, "Linked as a consumer to %s\n", dev_name(supplier));
656 device_links_write_unlock();
658 if ((flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) && !link)
659 pm_runtime_put(supplier);
663 EXPORT_SYMBOL_GPL(device_link_add);
666 * device_link_wait_for_supplier - Add device to wait_for_suppliers list
667 * @consumer: Consumer device
669 * Marks the @consumer device as waiting for suppliers to become available by
670 * adding it to the wait_for_suppliers list. The consumer device will never be
671 * probed until it's removed from the wait_for_suppliers list.
673 * The caller is responsible for adding the links to the supplier devices once
674 * they are available and removing the @consumer device from the
675 * wait_for_suppliers list once links to all the suppliers have been created.
677 * This function is NOT meant to be called from the probe function of the
678 * consumer but rather from code that creates/adds the consumer device.
680 static void device_link_wait_for_supplier(struct device *consumer,
683 mutex_lock(&wfs_lock);
684 list_add_tail(&consumer->links.needs_suppliers, &wait_for_suppliers);
685 consumer->links.need_for_probe = need_for_probe;
686 mutex_unlock(&wfs_lock);
689 static void device_link_wait_for_mandatory_supplier(struct device *consumer)
691 device_link_wait_for_supplier(consumer, true);
694 static void device_link_wait_for_optional_supplier(struct device *consumer)
696 device_link_wait_for_supplier(consumer, false);
700 * device_link_add_missing_supplier_links - Add links from consumer devices to
701 * supplier devices, leaving any
702 * consumer with inactive suppliers on
703 * the wait_for_suppliers list
705 * Loops through all consumers waiting on suppliers and tries to add all their
706 * supplier links. If that succeeds, the consumer device is removed from
707 * wait_for_suppliers list. Otherwise, they are left in the wait_for_suppliers
708 * list. Devices left on the wait_for_suppliers list will not be probed.
710 * The fwnode add_links callback is expected to return 0 if it has found and
711 * added all the supplier links for the consumer device. It should return an
712 * error if it isn't able to do so.
714 * The caller of device_link_wait_for_supplier() is expected to call this once
715 * it's aware of potential suppliers becoming available.
717 static void device_link_add_missing_supplier_links(void)
719 struct device *dev, *tmp;
721 mutex_lock(&wfs_lock);
722 list_for_each_entry_safe(dev, tmp, &wait_for_suppliers,
723 links.needs_suppliers) {
724 int ret = fwnode_call_int_op(dev->fwnode, add_links, dev);
726 list_del_init(&dev->links.needs_suppliers);
727 else if (ret != -ENODEV || fw_devlink_is_permissive())
728 dev->links.need_for_probe = false;
730 mutex_unlock(&wfs_lock);
733 static void device_link_free(struct device_link *link)
735 while (refcount_dec_not_one(&link->rpm_active))
736 pm_runtime_put(link->supplier);
738 put_device(link->consumer);
739 put_device(link->supplier);
740 device_unregister(&link->link_dev);
744 static void __device_link_free_srcu(struct rcu_head *rhead)
746 device_link_free(container_of(rhead, struct device_link, rcu_head));
749 static void __device_link_del(struct kref *kref)
751 struct device_link *link = container_of(kref, struct device_link, kref);
753 dev_dbg(link->consumer, "Dropping the link to %s\n",
754 dev_name(link->supplier));
756 if (link->flags & DL_FLAG_PM_RUNTIME)
757 pm_runtime_drop_link(link->consumer);
759 list_del_rcu(&link->s_node);
760 list_del_rcu(&link->c_node);
761 call_srcu(&device_links_srcu, &link->rcu_head, __device_link_free_srcu);
763 #else /* !CONFIG_SRCU */
764 static void __device_link_del(struct kref *kref)
766 struct device_link *link = container_of(kref, struct device_link, kref);
768 dev_info(link->consumer, "Dropping the link to %s\n",
769 dev_name(link->supplier));
771 if (link->flags & DL_FLAG_PM_RUNTIME)
772 pm_runtime_drop_link(link->consumer);
774 list_del(&link->s_node);
775 list_del(&link->c_node);
776 device_link_free(link);
778 #endif /* !CONFIG_SRCU */
780 static void device_link_put_kref(struct device_link *link)
782 if (link->flags & DL_FLAG_STATELESS)
783 kref_put(&link->kref, __device_link_del);
785 WARN(1, "Unable to drop a managed device link reference\n");
789 * device_link_del - Delete a stateless link between two devices.
790 * @link: Device link to delete.
792 * The caller must ensure proper synchronization of this function with runtime
793 * PM. If the link was added multiple times, it needs to be deleted as often.
794 * Care is required for hotplugged devices: Their links are purged on removal
795 * and calling device_link_del() is then no longer allowed.
797 void device_link_del(struct device_link *link)
799 device_links_write_lock();
801 device_link_put_kref(link);
803 device_links_write_unlock();
805 EXPORT_SYMBOL_GPL(device_link_del);
808 * device_link_remove - Delete a stateless link between two devices.
809 * @consumer: Consumer end of the link.
810 * @supplier: Supplier end of the link.
812 * The caller must ensure proper synchronization of this function with runtime
815 void device_link_remove(void *consumer, struct device *supplier)
817 struct device_link *link;
819 if (WARN_ON(consumer == supplier))
822 device_links_write_lock();
825 list_for_each_entry(link, &supplier->links.consumers, s_node) {
826 if (link->consumer == consumer) {
827 device_link_put_kref(link);
833 device_links_write_unlock();
835 EXPORT_SYMBOL_GPL(device_link_remove);
837 static void device_links_missing_supplier(struct device *dev)
839 struct device_link *link;
841 list_for_each_entry(link, &dev->links.suppliers, c_node) {
842 if (link->status != DL_STATE_CONSUMER_PROBE)
845 if (link->supplier->links.status == DL_DEV_DRIVER_BOUND) {
846 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
848 WARN_ON(!(link->flags & DL_FLAG_SYNC_STATE_ONLY));
849 WRITE_ONCE(link->status, DL_STATE_DORMANT);
855 * device_links_check_suppliers - Check presence of supplier drivers.
856 * @dev: Consumer device.
858 * Check links from this device to any suppliers. Walk the list of the device's
859 * links to suppliers and see if all of them are available. If not, simply
860 * return -EPROBE_DEFER.
862 * We need to guarantee that the supplier will not go away after the check has
863 * been positive here. It only can go away in __device_release_driver() and
864 * that function checks the device's links to consumers. This means we need to
865 * mark the link as "consumer probe in progress" to make the supplier removal
866 * wait for us to complete (or bad things may happen).
868 * Links without the DL_FLAG_MANAGED flag set are ignored.
870 int device_links_check_suppliers(struct device *dev)
872 struct device_link *link;
876 * Device waiting for supplier to become available is not allowed to
879 mutex_lock(&wfs_lock);
880 if (!list_empty(&dev->links.needs_suppliers) &&
881 dev->links.need_for_probe) {
882 mutex_unlock(&wfs_lock);
883 return -EPROBE_DEFER;
885 mutex_unlock(&wfs_lock);
887 device_links_write_lock();
889 list_for_each_entry(link, &dev->links.suppliers, c_node) {
890 if (!(link->flags & DL_FLAG_MANAGED))
893 if (link->status != DL_STATE_AVAILABLE &&
894 !(link->flags & DL_FLAG_SYNC_STATE_ONLY)) {
895 device_links_missing_supplier(dev);
899 WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE);
901 dev->links.status = DL_DEV_PROBING;
903 device_links_write_unlock();
908 * __device_links_queue_sync_state - Queue a device for sync_state() callback
909 * @dev: Device to call sync_state() on
910 * @list: List head to queue the @dev on
912 * Queues a device for a sync_state() callback when the device links write lock
913 * isn't held. This allows the sync_state() execution flow to use device links
914 * APIs. The caller must ensure this function is called with
915 * device_links_write_lock() held.
917 * This function does a get_device() to make sure the device is not freed while
920 * So the caller must also ensure that device_links_flush_sync_list() is called
921 * as soon as the caller releases device_links_write_lock(). This is necessary
922 * to make sure the sync_state() is called in a timely fashion and the
923 * put_device() is called on this device.
925 static void __device_links_queue_sync_state(struct device *dev,
926 struct list_head *list)
928 struct device_link *link;
930 if (!dev_has_sync_state(dev))
932 if (dev->state_synced)
935 list_for_each_entry(link, &dev->links.consumers, s_node) {
936 if (!(link->flags & DL_FLAG_MANAGED))
938 if (link->status != DL_STATE_ACTIVE)
943 * Set the flag here to avoid adding the same device to a list more
944 * than once. This can happen if new consumers get added to the device
945 * and probed before the list is flushed.
947 dev->state_synced = true;
949 if (WARN_ON(!list_empty(&dev->links.defer_sync)))
953 list_add_tail(&dev->links.defer_sync, list);
957 * device_links_flush_sync_list - Call sync_state() on a list of devices
958 * @list: List of devices to call sync_state() on
959 * @dont_lock_dev: Device for which lock is already held by the caller
961 * Calls sync_state() on all the devices that have been queued for it. This
962 * function is used in conjunction with __device_links_queue_sync_state(). The
963 * @dont_lock_dev parameter is useful when this function is called from a
964 * context where a device lock is already held.
966 static void device_links_flush_sync_list(struct list_head *list,
967 struct device *dont_lock_dev)
969 struct device *dev, *tmp;
971 list_for_each_entry_safe(dev, tmp, list, links.defer_sync) {
972 list_del_init(&dev->links.defer_sync);
974 if (dev != dont_lock_dev)
977 if (dev->bus->sync_state)
978 dev->bus->sync_state(dev);
979 else if (dev->driver && dev->driver->sync_state)
980 dev->driver->sync_state(dev);
982 if (dev != dont_lock_dev)
989 void device_links_supplier_sync_state_pause(void)
991 device_links_write_lock();
992 defer_sync_state_count++;
993 device_links_write_unlock();
996 void device_links_supplier_sync_state_resume(void)
998 struct device *dev, *tmp;
999 LIST_HEAD(sync_list);
1001 device_links_write_lock();
1002 if (!defer_sync_state_count) {
1003 WARN(true, "Unmatched sync_state pause/resume!");
1006 defer_sync_state_count--;
1007 if (defer_sync_state_count)
1010 list_for_each_entry_safe(dev, tmp, &deferred_sync, links.defer_sync) {
1012 * Delete from deferred_sync list before queuing it to
1013 * sync_list because defer_sync is used for both lists.
1015 list_del_init(&dev->links.defer_sync);
1016 __device_links_queue_sync_state(dev, &sync_list);
1019 device_links_write_unlock();
1021 device_links_flush_sync_list(&sync_list, NULL);
1024 static int sync_state_resume_initcall(void)
1026 device_links_supplier_sync_state_resume();
1029 late_initcall(sync_state_resume_initcall);
1031 static void __device_links_supplier_defer_sync(struct device *sup)
1033 if (list_empty(&sup->links.defer_sync) && dev_has_sync_state(sup))
1034 list_add_tail(&sup->links.defer_sync, &deferred_sync);
1037 static void device_link_drop_managed(struct device_link *link)
1039 link->flags &= ~DL_FLAG_MANAGED;
1040 WRITE_ONCE(link->status, DL_STATE_NONE);
1041 kref_put(&link->kref, __device_link_del);
1045 * device_links_driver_bound - Update device links after probing its driver.
1046 * @dev: Device to update the links for.
1048 * The probe has been successful, so update links from this device to any
1049 * consumers by changing their status to "available".
1051 * Also change the status of @dev's links to suppliers to "active".
1053 * Links without the DL_FLAG_MANAGED flag set are ignored.
1055 void device_links_driver_bound(struct device *dev)
1057 struct device_link *link, *ln;
1058 LIST_HEAD(sync_list);
1061 * If a device probes successfully, it's expected to have created all
1062 * the device links it needs to or make new device links as it needs
1063 * them. So, it no longer needs to wait on any suppliers.
1065 mutex_lock(&wfs_lock);
1066 list_del_init(&dev->links.needs_suppliers);
1067 mutex_unlock(&wfs_lock);
1069 device_links_write_lock();
1071 list_for_each_entry(link, &dev->links.consumers, s_node) {
1072 if (!(link->flags & DL_FLAG_MANAGED))
1076 * Links created during consumer probe may be in the "consumer
1077 * probe" state to start with if the supplier is still probing
1078 * when they are created and they may become "active" if the
1079 * consumer probe returns first. Skip them here.
1081 if (link->status == DL_STATE_CONSUMER_PROBE ||
1082 link->status == DL_STATE_ACTIVE)
1085 WARN_ON(link->status != DL_STATE_DORMANT);
1086 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
1088 if (link->flags & DL_FLAG_AUTOPROBE_CONSUMER)
1089 driver_deferred_probe_add(link->consumer);
1092 if (defer_sync_state_count)
1093 __device_links_supplier_defer_sync(dev);
1095 __device_links_queue_sync_state(dev, &sync_list);
1097 list_for_each_entry_safe(link, ln, &dev->links.suppliers, c_node) {
1098 struct device *supplier;
1100 if (!(link->flags & DL_FLAG_MANAGED))
1103 supplier = link->supplier;
1104 if (link->flags & DL_FLAG_SYNC_STATE_ONLY) {
1106 * When DL_FLAG_SYNC_STATE_ONLY is set, it means no
1107 * other DL_MANAGED_LINK_FLAGS have been set. So, it's
1108 * save to drop the managed link completely.
1110 device_link_drop_managed(link);
1112 WARN_ON(link->status != DL_STATE_CONSUMER_PROBE);
1113 WRITE_ONCE(link->status, DL_STATE_ACTIVE);
1117 * This needs to be done even for the deleted
1118 * DL_FLAG_SYNC_STATE_ONLY device link in case it was the last
1119 * device link that was preventing the supplier from getting a
1120 * sync_state() call.
1122 if (defer_sync_state_count)
1123 __device_links_supplier_defer_sync(supplier);
1125 __device_links_queue_sync_state(supplier, &sync_list);
1128 dev->links.status = DL_DEV_DRIVER_BOUND;
1130 device_links_write_unlock();
1132 device_links_flush_sync_list(&sync_list, dev);
1136 * __device_links_no_driver - Update links of a device without a driver.
1137 * @dev: Device without a drvier.
1139 * Delete all non-persistent links from this device to any suppliers.
1141 * Persistent links stay around, but their status is changed to "available",
1142 * unless they already are in the "supplier unbind in progress" state in which
1143 * case they need not be updated.
1145 * Links without the DL_FLAG_MANAGED flag set are ignored.
1147 static void __device_links_no_driver(struct device *dev)
1149 struct device_link *link, *ln;
1151 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
1152 if (!(link->flags & DL_FLAG_MANAGED))
1155 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER) {
1156 device_link_drop_managed(link);
1160 if (link->status != DL_STATE_CONSUMER_PROBE &&
1161 link->status != DL_STATE_ACTIVE)
1164 if (link->supplier->links.status == DL_DEV_DRIVER_BOUND) {
1165 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
1167 WARN_ON(!(link->flags & DL_FLAG_SYNC_STATE_ONLY));
1168 WRITE_ONCE(link->status, DL_STATE_DORMANT);
1172 dev->links.status = DL_DEV_NO_DRIVER;
1176 * device_links_no_driver - Update links after failing driver probe.
1177 * @dev: Device whose driver has just failed to probe.
1179 * Clean up leftover links to consumers for @dev and invoke
1180 * %__device_links_no_driver() to update links to suppliers for it as
1183 * Links without the DL_FLAG_MANAGED flag set are ignored.
1185 void device_links_no_driver(struct device *dev)
1187 struct device_link *link;
1189 device_links_write_lock();
1191 list_for_each_entry(link, &dev->links.consumers, s_node) {
1192 if (!(link->flags & DL_FLAG_MANAGED))
1196 * The probe has failed, so if the status of the link is
1197 * "consumer probe" or "active", it must have been added by
1198 * a probing consumer while this device was still probing.
1199 * Change its state to "dormant", as it represents a valid
1200 * relationship, but it is not functionally meaningful.
1202 if (link->status == DL_STATE_CONSUMER_PROBE ||
1203 link->status == DL_STATE_ACTIVE)
1204 WRITE_ONCE(link->status, DL_STATE_DORMANT);
1207 __device_links_no_driver(dev);
1209 device_links_write_unlock();
1213 * device_links_driver_cleanup - Update links after driver removal.
1214 * @dev: Device whose driver has just gone away.
1216 * Update links to consumers for @dev by changing their status to "dormant" and
1217 * invoke %__device_links_no_driver() to update links to suppliers for it as
1220 * Links without the DL_FLAG_MANAGED flag set are ignored.
1222 void device_links_driver_cleanup(struct device *dev)
1224 struct device_link *link, *ln;
1226 device_links_write_lock();
1228 list_for_each_entry_safe(link, ln, &dev->links.consumers, s_node) {
1229 if (!(link->flags & DL_FLAG_MANAGED))
1232 WARN_ON(link->flags & DL_FLAG_AUTOREMOVE_CONSUMER);
1233 WARN_ON(link->status != DL_STATE_SUPPLIER_UNBIND);
1236 * autoremove the links between this @dev and its consumer
1237 * devices that are not active, i.e. where the link state
1238 * has moved to DL_STATE_SUPPLIER_UNBIND.
1240 if (link->status == DL_STATE_SUPPLIER_UNBIND &&
1241 link->flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
1242 device_link_drop_managed(link);
1244 WRITE_ONCE(link->status, DL_STATE_DORMANT);
1247 list_del_init(&dev->links.defer_sync);
1248 __device_links_no_driver(dev);
1250 device_links_write_unlock();
1254 * device_links_busy - Check if there are any busy links to consumers.
1255 * @dev: Device to check.
1257 * Check each consumer of the device and return 'true' if its link's status
1258 * is one of "consumer probe" or "active" (meaning that the given consumer is
1259 * probing right now or its driver is present). Otherwise, change the link
1260 * state to "supplier unbind" to prevent the consumer from being probed
1261 * successfully going forward.
1263 * Return 'false' if there are no probing or active consumers.
1265 * Links without the DL_FLAG_MANAGED flag set are ignored.
1267 bool device_links_busy(struct device *dev)
1269 struct device_link *link;
1272 device_links_write_lock();
1274 list_for_each_entry(link, &dev->links.consumers, s_node) {
1275 if (!(link->flags & DL_FLAG_MANAGED))
1278 if (link->status == DL_STATE_CONSUMER_PROBE
1279 || link->status == DL_STATE_ACTIVE) {
1283 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
1286 dev->links.status = DL_DEV_UNBINDING;
1288 device_links_write_unlock();
1293 * device_links_unbind_consumers - Force unbind consumers of the given device.
1294 * @dev: Device to unbind the consumers of.
1296 * Walk the list of links to consumers for @dev and if any of them is in the
1297 * "consumer probe" state, wait for all device probes in progress to complete
1300 * If that's not the case, change the status of the link to "supplier unbind"
1301 * and check if the link was in the "active" state. If so, force the consumer
1302 * driver to unbind and start over (the consumer will not re-probe as we have
1303 * changed the state of the link already).
1305 * Links without the DL_FLAG_MANAGED flag set are ignored.
1307 void device_links_unbind_consumers(struct device *dev)
1309 struct device_link *link;
1312 device_links_write_lock();
1314 list_for_each_entry(link, &dev->links.consumers, s_node) {
1315 enum device_link_state status;
1317 if (!(link->flags & DL_FLAG_MANAGED) ||
1318 link->flags & DL_FLAG_SYNC_STATE_ONLY)
1321 status = link->status;
1322 if (status == DL_STATE_CONSUMER_PROBE) {
1323 device_links_write_unlock();
1325 wait_for_device_probe();
1328 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
1329 if (status == DL_STATE_ACTIVE) {
1330 struct device *consumer = link->consumer;
1332 get_device(consumer);
1334 device_links_write_unlock();
1336 device_release_driver_internal(consumer, NULL,
1338 put_device(consumer);
1343 device_links_write_unlock();
1347 * device_links_purge - Delete existing links to other devices.
1348 * @dev: Target device.
1350 static void device_links_purge(struct device *dev)
1352 struct device_link *link, *ln;
1354 if (dev->class == &devlink_class)
1357 mutex_lock(&wfs_lock);
1358 list_del(&dev->links.needs_suppliers);
1359 mutex_unlock(&wfs_lock);
1362 * Delete all of the remaining links from this device to any other
1363 * devices (either consumers or suppliers).
1365 device_links_write_lock();
1367 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
1368 WARN_ON(link->status == DL_STATE_ACTIVE);
1369 __device_link_del(&link->kref);
1372 list_for_each_entry_safe_reverse(link, ln, &dev->links.consumers, s_node) {
1373 WARN_ON(link->status != DL_STATE_DORMANT &&
1374 link->status != DL_STATE_NONE);
1375 __device_link_del(&link->kref);
1378 device_links_write_unlock();
1381 static u32 fw_devlink_flags = DL_FLAG_SYNC_STATE_ONLY;
1382 static int __init fw_devlink_setup(char *arg)
1387 if (strcmp(arg, "off") == 0) {
1388 fw_devlink_flags = 0;
1389 } else if (strcmp(arg, "permissive") == 0) {
1390 fw_devlink_flags = DL_FLAG_SYNC_STATE_ONLY;
1391 } else if (strcmp(arg, "on") == 0) {
1392 fw_devlink_flags = DL_FLAG_AUTOPROBE_CONSUMER;
1393 } else if (strcmp(arg, "rpm") == 0) {
1394 fw_devlink_flags = DL_FLAG_AUTOPROBE_CONSUMER |
1399 early_param("fw_devlink", fw_devlink_setup);
1401 u32 fw_devlink_get_flags(void)
1403 return fw_devlink_flags;
1406 static bool fw_devlink_is_permissive(void)
1408 return fw_devlink_flags == DL_FLAG_SYNC_STATE_ONLY;
1411 static void fw_devlink_link_device(struct device *dev)
1415 if (!fw_devlink_flags)
1418 mutex_lock(&defer_fw_devlink_lock);
1419 if (!defer_fw_devlink_count)
1420 device_link_add_missing_supplier_links();
1423 * The device's fwnode not having add_links() doesn't affect if other
1424 * consumers can find this device as a supplier. So, this check is
1425 * intentionally placed after device_link_add_missing_supplier_links().
1427 if (!fwnode_has_op(dev->fwnode, add_links))
1431 * If fw_devlink is being deferred, assume all devices have mandatory
1432 * suppliers they need to link to later. Then, when the fw_devlink is
1433 * resumed, all these devices will get a chance to try and link to any
1434 * suppliers they have.
1436 if (!defer_fw_devlink_count) {
1437 fw_ret = fwnode_call_int_op(dev->fwnode, add_links, dev);
1438 if (fw_ret == -ENODEV && fw_devlink_is_permissive())
1444 if (fw_ret == -ENODEV)
1445 device_link_wait_for_mandatory_supplier(dev);
1447 device_link_wait_for_optional_supplier(dev);
1450 mutex_unlock(&defer_fw_devlink_lock);
1454 * fw_devlink_pause - Pause parsing of fwnode to create device links
1456 * Calling this function defers any fwnode parsing to create device links until
1457 * fw_devlink_resume() is called. Both these functions are ref counted and the
1458 * caller needs to match the calls.
1460 * While fw_devlink is paused:
1461 * - Any device that is added won't have its fwnode parsed to create device
1463 * - The probe of the device will also be deferred during this period.
1464 * - Any devices that were already added, but waiting for suppliers won't be
1465 * able to link to newly added devices.
1467 * Once fw_devlink_resume():
1468 * - All the fwnodes that was not parsed will be parsed.
1469 * - All the devices that were deferred probing will be reattempted if they
1470 * aren't waiting for any more suppliers.
1472 * This pair of functions, is mainly meant to optimize the parsing of fwnodes
1473 * when a lot of devices that need to link to each other are added in a short
1474 * interval of time. For example, adding all the top level devices in a system.
1476 * For example, if N devices are added and:
1477 * - All the consumers are added before their suppliers
1478 * - All the suppliers of the N devices are part of the N devices
1482 * - With the use of fw_devlink_pause() and fw_devlink_resume(), each device
1483 * will only need one parsing of its fwnode because it is guaranteed to find
1484 * all the supplier devices already registered and ready to link to. It won't
1485 * have to do another pass later to find one or more suppliers it couldn't
1486 * find in the first parse of the fwnode. So, we'll only need O(N) fwnode
1489 * - Without the use of fw_devlink_pause() and fw_devlink_resume(), we would
1490 * end up doing O(N^2) parses of fwnodes because every device that's added is
1491 * guaranteed to trigger a parse of the fwnode of every device added before
1492 * it. This O(N^2) parse is made worse by the fact that when a fwnode of a
1493 * device is parsed, all it descendant devices might need to have their
1494 * fwnodes parsed too (even if the devices themselves aren't added).
1496 void fw_devlink_pause(void)
1498 mutex_lock(&defer_fw_devlink_lock);
1499 defer_fw_devlink_count++;
1500 mutex_unlock(&defer_fw_devlink_lock);
1503 /** fw_devlink_resume - Resume parsing of fwnode to create device links
1505 * This function is used in conjunction with fw_devlink_pause() and is ref
1506 * counted. See documentation for fw_devlink_pause() for more details.
1508 void fw_devlink_resume(void)
1510 mutex_lock(&defer_fw_devlink_lock);
1511 if (!defer_fw_devlink_count) {
1512 WARN(true, "Unmatched fw_devlink pause/resume!");
1516 defer_fw_devlink_count--;
1517 if (defer_fw_devlink_count)
1520 device_link_add_missing_supplier_links();
1521 driver_deferred_probe_force_trigger();
1523 mutex_unlock(&defer_fw_devlink_lock);
1525 /* Device links support end. */
1527 int (*platform_notify)(struct device *dev) = NULL;
1528 int (*platform_notify_remove)(struct device *dev) = NULL;
1529 static struct kobject *dev_kobj;
1530 struct kobject *sysfs_dev_char_kobj;
1531 struct kobject *sysfs_dev_block_kobj;
1533 static DEFINE_MUTEX(device_hotplug_lock);
1535 void lock_device_hotplug(void)
1537 mutex_lock(&device_hotplug_lock);
1540 void unlock_device_hotplug(void)
1542 mutex_unlock(&device_hotplug_lock);
1545 int lock_device_hotplug_sysfs(void)
1547 if (mutex_trylock(&device_hotplug_lock))
1550 /* Avoid busy looping (5 ms of sleep should do). */
1552 return restart_syscall();
1556 static inline int device_is_not_partition(struct device *dev)
1558 return !(dev->type == &part_type);
1561 static inline int device_is_not_partition(struct device *dev)
1568 device_platform_notify(struct device *dev, enum kobject_action action)
1572 ret = acpi_platform_notify(dev, action);
1576 ret = software_node_notify(dev, action);
1580 if (platform_notify && action == KOBJ_ADD)
1581 platform_notify(dev);
1582 else if (platform_notify_remove && action == KOBJ_REMOVE)
1583 platform_notify_remove(dev);
1588 * dev_driver_string - Return a device's driver name, if at all possible
1589 * @dev: struct device to get the name of
1591 * Will return the device's driver's name if it is bound to a device. If
1592 * the device is not bound to a driver, it will return the name of the bus
1593 * it is attached to. If it is not attached to a bus either, an empty
1594 * string will be returned.
1596 const char *dev_driver_string(const struct device *dev)
1598 struct device_driver *drv;
1600 /* dev->driver can change to NULL underneath us because of unbinding,
1601 * so be careful about accessing it. dev->bus and dev->class should
1602 * never change once they are set, so they don't need special care.
1604 drv = READ_ONCE(dev->driver);
1605 return drv ? drv->name :
1606 (dev->bus ? dev->bus->name :
1607 (dev->class ? dev->class->name : ""));
1609 EXPORT_SYMBOL(dev_driver_string);
1611 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
1613 static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
1616 struct device_attribute *dev_attr = to_dev_attr(attr);
1617 struct device *dev = kobj_to_dev(kobj);
1621 ret = dev_attr->show(dev, dev_attr, buf);
1622 if (ret >= (ssize_t)PAGE_SIZE) {
1623 printk("dev_attr_show: %pS returned bad count\n",
1629 static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
1630 const char *buf, size_t count)
1632 struct device_attribute *dev_attr = to_dev_attr(attr);
1633 struct device *dev = kobj_to_dev(kobj);
1636 if (dev_attr->store)
1637 ret = dev_attr->store(dev, dev_attr, buf, count);
1641 static const struct sysfs_ops dev_sysfs_ops = {
1642 .show = dev_attr_show,
1643 .store = dev_attr_store,
1646 #define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
1648 ssize_t device_store_ulong(struct device *dev,
1649 struct device_attribute *attr,
1650 const char *buf, size_t size)
1652 struct dev_ext_attribute *ea = to_ext_attr(attr);
1656 ret = kstrtoul(buf, 0, &new);
1659 *(unsigned long *)(ea->var) = new;
1660 /* Always return full write size even if we didn't consume all */
1663 EXPORT_SYMBOL_GPL(device_store_ulong);
1665 ssize_t device_show_ulong(struct device *dev,
1666 struct device_attribute *attr,
1669 struct dev_ext_attribute *ea = to_ext_attr(attr);
1670 return snprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var));
1672 EXPORT_SYMBOL_GPL(device_show_ulong);
1674 ssize_t device_store_int(struct device *dev,
1675 struct device_attribute *attr,
1676 const char *buf, size_t size)
1678 struct dev_ext_attribute *ea = to_ext_attr(attr);
1682 ret = kstrtol(buf, 0, &new);
1686 if (new > INT_MAX || new < INT_MIN)
1688 *(int *)(ea->var) = new;
1689 /* Always return full write size even if we didn't consume all */
1692 EXPORT_SYMBOL_GPL(device_store_int);
1694 ssize_t device_show_int(struct device *dev,
1695 struct device_attribute *attr,
1698 struct dev_ext_attribute *ea = to_ext_attr(attr);
1700 return snprintf(buf, PAGE_SIZE, "%d\n", *(int *)(ea->var));
1702 EXPORT_SYMBOL_GPL(device_show_int);
1704 ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
1705 const char *buf, size_t size)
1707 struct dev_ext_attribute *ea = to_ext_attr(attr);
1709 if (strtobool(buf, ea->var) < 0)
1714 EXPORT_SYMBOL_GPL(device_store_bool);
1716 ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
1719 struct dev_ext_attribute *ea = to_ext_attr(attr);
1721 return snprintf(buf, PAGE_SIZE, "%d\n", *(bool *)(ea->var));
1723 EXPORT_SYMBOL_GPL(device_show_bool);
1726 * device_release - free device structure.
1727 * @kobj: device's kobject.
1729 * This is called once the reference count for the object
1730 * reaches 0. We forward the call to the device's release
1731 * method, which should handle actually freeing the structure.
1733 static void device_release(struct kobject *kobj)
1735 struct device *dev = kobj_to_dev(kobj);
1736 struct device_private *p = dev->p;
1739 * Some platform devices are driven without driver attached
1740 * and managed resources may have been acquired. Make sure
1741 * all resources are released.
1743 * Drivers still can add resources into device after device
1744 * is deleted but alive, so release devres here to avoid
1745 * possible memory leak.
1747 devres_release_all(dev);
1751 else if (dev->type && dev->type->release)
1752 dev->type->release(dev);
1753 else if (dev->class && dev->class->dev_release)
1754 dev->class->dev_release(dev);
1756 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",
1761 static const void *device_namespace(struct kobject *kobj)
1763 struct device *dev = kobj_to_dev(kobj);
1764 const void *ns = NULL;
1766 if (dev->class && dev->class->ns_type)
1767 ns = dev->class->namespace(dev);
1772 static void device_get_ownership(struct kobject *kobj, kuid_t *uid, kgid_t *gid)
1774 struct device *dev = kobj_to_dev(kobj);
1776 if (dev->class && dev->class->get_ownership)
1777 dev->class->get_ownership(dev, uid, gid);
1780 static struct kobj_type device_ktype = {
1781 .release = device_release,
1782 .sysfs_ops = &dev_sysfs_ops,
1783 .namespace = device_namespace,
1784 .get_ownership = device_get_ownership,
1788 static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
1790 struct kobj_type *ktype = get_ktype(kobj);
1792 if (ktype == &device_ktype) {
1793 struct device *dev = kobj_to_dev(kobj);
1802 static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
1804 struct device *dev = kobj_to_dev(kobj);
1807 return dev->bus->name;
1809 return dev->class->name;
1813 static int dev_uevent(struct kset *kset, struct kobject *kobj,
1814 struct kobj_uevent_env *env)
1816 struct device *dev = kobj_to_dev(kobj);
1819 /* add device node properties if present */
1820 if (MAJOR(dev->devt)) {
1824 kuid_t uid = GLOBAL_ROOT_UID;
1825 kgid_t gid = GLOBAL_ROOT_GID;
1827 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
1828 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
1829 name = device_get_devnode(dev, &mode, &uid, &gid, &tmp);
1831 add_uevent_var(env, "DEVNAME=%s", name);
1833 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
1834 if (!uid_eq(uid, GLOBAL_ROOT_UID))
1835 add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid));
1836 if (!gid_eq(gid, GLOBAL_ROOT_GID))
1837 add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid));
1842 if (dev->type && dev->type->name)
1843 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
1846 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
1848 /* Add common DT information about the device */
1849 of_device_uevent(dev, env);
1851 /* have the bus specific function add its stuff */
1852 if (dev->bus && dev->bus->uevent) {
1853 retval = dev->bus->uevent(dev, env);
1855 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
1856 dev_name(dev), __func__, retval);
1859 /* have the class specific function add its stuff */
1860 if (dev->class && dev->class->dev_uevent) {
1861 retval = dev->class->dev_uevent(dev, env);
1863 pr_debug("device: '%s': %s: class uevent() "
1864 "returned %d\n", dev_name(dev),
1868 /* have the device type specific function add its stuff */
1869 if (dev->type && dev->type->uevent) {
1870 retval = dev->type->uevent(dev, env);
1872 pr_debug("device: '%s': %s: dev_type uevent() "
1873 "returned %d\n", dev_name(dev),
1880 static const struct kset_uevent_ops device_uevent_ops = {
1881 .filter = dev_uevent_filter,
1882 .name = dev_uevent_name,
1883 .uevent = dev_uevent,
1886 static ssize_t uevent_show(struct device *dev, struct device_attribute *attr,
1889 struct kobject *top_kobj;
1891 struct kobj_uevent_env *env = NULL;
1896 /* search the kset, the device belongs to */
1897 top_kobj = &dev->kobj;
1898 while (!top_kobj->kset && top_kobj->parent)
1899 top_kobj = top_kobj->parent;
1900 if (!top_kobj->kset)
1903 kset = top_kobj->kset;
1904 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
1907 /* respect filter */
1908 if (kset->uevent_ops && kset->uevent_ops->filter)
1909 if (!kset->uevent_ops->filter(kset, &dev->kobj))
1912 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
1916 /* let the kset specific function add its keys */
1917 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
1921 /* copy keys to file */
1922 for (i = 0; i < env->envp_idx; i++)
1923 count += sprintf(&buf[count], "%s\n", env->envp[i]);
1929 static ssize_t uevent_store(struct device *dev, struct device_attribute *attr,
1930 const char *buf, size_t count)
1934 rc = kobject_synth_uevent(&dev->kobj, buf, count);
1937 dev_err(dev, "uevent: failed to send synthetic uevent\n");
1943 static DEVICE_ATTR_RW(uevent);
1945 static ssize_t online_show(struct device *dev, struct device_attribute *attr,
1951 val = !dev->offline;
1953 return sprintf(buf, "%u\n", val);
1956 static ssize_t online_store(struct device *dev, struct device_attribute *attr,
1957 const char *buf, size_t count)
1962 ret = strtobool(buf, &val);
1966 ret = lock_device_hotplug_sysfs();
1970 ret = val ? device_online(dev) : device_offline(dev);
1971 unlock_device_hotplug();
1972 return ret < 0 ? ret : count;
1974 static DEVICE_ATTR_RW(online);
1976 int device_add_groups(struct device *dev, const struct attribute_group **groups)
1978 return sysfs_create_groups(&dev->kobj, groups);
1980 EXPORT_SYMBOL_GPL(device_add_groups);
1982 void device_remove_groups(struct device *dev,
1983 const struct attribute_group **groups)
1985 sysfs_remove_groups(&dev->kobj, groups);
1987 EXPORT_SYMBOL_GPL(device_remove_groups);
1989 union device_attr_group_devres {
1990 const struct attribute_group *group;
1991 const struct attribute_group **groups;
1994 static int devm_attr_group_match(struct device *dev, void *res, void *data)
1996 return ((union device_attr_group_devres *)res)->group == data;
1999 static void devm_attr_group_remove(struct device *dev, void *res)
2001 union device_attr_group_devres *devres = res;
2002 const struct attribute_group *group = devres->group;
2004 dev_dbg(dev, "%s: removing group %p\n", __func__, group);
2005 sysfs_remove_group(&dev->kobj, group);
2008 static void devm_attr_groups_remove(struct device *dev, void *res)
2010 union device_attr_group_devres *devres = res;
2011 const struct attribute_group **groups = devres->groups;
2013 dev_dbg(dev, "%s: removing groups %p\n", __func__, groups);
2014 sysfs_remove_groups(&dev->kobj, groups);
2018 * devm_device_add_group - given a device, create a managed attribute group
2019 * @dev: The device to create the group for
2020 * @grp: The attribute group to create
2022 * This function creates a group for the first time. It will explicitly
2023 * warn and error if any of the attribute files being created already exist.
2025 * Returns 0 on success or error code on failure.
2027 int devm_device_add_group(struct device *dev, const struct attribute_group *grp)
2029 union device_attr_group_devres *devres;
2032 devres = devres_alloc(devm_attr_group_remove,
2033 sizeof(*devres), GFP_KERNEL);
2037 error = sysfs_create_group(&dev->kobj, grp);
2039 devres_free(devres);
2043 devres->group = grp;
2044 devres_add(dev, devres);
2047 EXPORT_SYMBOL_GPL(devm_device_add_group);
2050 * devm_device_remove_group: remove a managed group from a device
2051 * @dev: device to remove the group from
2052 * @grp: group to remove
2054 * This function removes a group of attributes from a device. The attributes
2055 * previously have to have been created for this group, otherwise it will fail.
2057 void devm_device_remove_group(struct device *dev,
2058 const struct attribute_group *grp)
2060 WARN_ON(devres_release(dev, devm_attr_group_remove,
2061 devm_attr_group_match,
2062 /* cast away const */ (void *)grp));
2064 EXPORT_SYMBOL_GPL(devm_device_remove_group);
2067 * devm_device_add_groups - create a bunch of managed attribute groups
2068 * @dev: The device to create the group for
2069 * @groups: The attribute groups to create, NULL terminated
2071 * This function creates a bunch of managed attribute groups. If an error
2072 * occurs when creating a group, all previously created groups will be
2073 * removed, unwinding everything back to the original state when this
2074 * function was called. It will explicitly warn and error if any of the
2075 * attribute files being created already exist.
2077 * Returns 0 on success or error code from sysfs_create_group on failure.
2079 int devm_device_add_groups(struct device *dev,
2080 const struct attribute_group **groups)
2082 union device_attr_group_devres *devres;
2085 devres = devres_alloc(devm_attr_groups_remove,
2086 sizeof(*devres), GFP_KERNEL);
2090 error = sysfs_create_groups(&dev->kobj, groups);
2092 devres_free(devres);
2096 devres->groups = groups;
2097 devres_add(dev, devres);
2100 EXPORT_SYMBOL_GPL(devm_device_add_groups);
2103 * devm_device_remove_groups - remove a list of managed groups
2105 * @dev: The device for the groups to be removed from
2106 * @groups: NULL terminated list of groups to be removed
2108 * If groups is not NULL, remove the specified groups from the device.
2110 void devm_device_remove_groups(struct device *dev,
2111 const struct attribute_group **groups)
2113 WARN_ON(devres_release(dev, devm_attr_groups_remove,
2114 devm_attr_group_match,
2115 /* cast away const */ (void *)groups));
2117 EXPORT_SYMBOL_GPL(devm_device_remove_groups);
2119 static int device_add_attrs(struct device *dev)
2121 struct class *class = dev->class;
2122 const struct device_type *type = dev->type;
2126 error = device_add_groups(dev, class->dev_groups);
2132 error = device_add_groups(dev, type->groups);
2134 goto err_remove_class_groups;
2137 error = device_add_groups(dev, dev->groups);
2139 goto err_remove_type_groups;
2141 if (device_supports_offline(dev) && !dev->offline_disabled) {
2142 error = device_create_file(dev, &dev_attr_online);
2144 goto err_remove_dev_groups;
2149 err_remove_dev_groups:
2150 device_remove_groups(dev, dev->groups);
2151 err_remove_type_groups:
2153 device_remove_groups(dev, type->groups);
2154 err_remove_class_groups:
2156 device_remove_groups(dev, class->dev_groups);
2161 static void device_remove_attrs(struct device *dev)
2163 struct class *class = dev->class;
2164 const struct device_type *type = dev->type;
2166 device_remove_file(dev, &dev_attr_online);
2167 device_remove_groups(dev, dev->groups);
2170 device_remove_groups(dev, type->groups);
2173 device_remove_groups(dev, class->dev_groups);
2176 static ssize_t dev_show(struct device *dev, struct device_attribute *attr,
2179 return print_dev_t(buf, dev->devt);
2181 static DEVICE_ATTR_RO(dev);
2184 struct kset *devices_kset;
2187 * devices_kset_move_before - Move device in the devices_kset's list.
2188 * @deva: Device to move.
2189 * @devb: Device @deva should come before.
2191 static void devices_kset_move_before(struct device *deva, struct device *devb)
2195 pr_debug("devices_kset: Moving %s before %s\n",
2196 dev_name(deva), dev_name(devb));
2197 spin_lock(&devices_kset->list_lock);
2198 list_move_tail(&deva->kobj.entry, &devb->kobj.entry);
2199 spin_unlock(&devices_kset->list_lock);
2203 * devices_kset_move_after - Move device in the devices_kset's list.
2204 * @deva: Device to move
2205 * @devb: Device @deva should come after.
2207 static void devices_kset_move_after(struct device *deva, struct device *devb)
2211 pr_debug("devices_kset: Moving %s after %s\n",
2212 dev_name(deva), dev_name(devb));
2213 spin_lock(&devices_kset->list_lock);
2214 list_move(&deva->kobj.entry, &devb->kobj.entry);
2215 spin_unlock(&devices_kset->list_lock);
2219 * devices_kset_move_last - move the device to the end of devices_kset's list.
2220 * @dev: device to move
2222 void devices_kset_move_last(struct device *dev)
2226 pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev));
2227 spin_lock(&devices_kset->list_lock);
2228 list_move_tail(&dev->kobj.entry, &devices_kset->list);
2229 spin_unlock(&devices_kset->list_lock);
2233 * device_create_file - create sysfs attribute file for device.
2235 * @attr: device attribute descriptor.
2237 int device_create_file(struct device *dev,
2238 const struct device_attribute *attr)
2243 WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
2244 "Attribute %s: write permission without 'store'\n",
2246 WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
2247 "Attribute %s: read permission without 'show'\n",
2249 error = sysfs_create_file(&dev->kobj, &attr->attr);
2254 EXPORT_SYMBOL_GPL(device_create_file);
2257 * device_remove_file - remove sysfs attribute file.
2259 * @attr: device attribute descriptor.
2261 void device_remove_file(struct device *dev,
2262 const struct device_attribute *attr)
2265 sysfs_remove_file(&dev->kobj, &attr->attr);
2267 EXPORT_SYMBOL_GPL(device_remove_file);
2270 * device_remove_file_self - remove sysfs attribute file from its own method.
2272 * @attr: device attribute descriptor.
2274 * See kernfs_remove_self() for details.
2276 bool device_remove_file_self(struct device *dev,
2277 const struct device_attribute *attr)
2280 return sysfs_remove_file_self(&dev->kobj, &attr->attr);
2284 EXPORT_SYMBOL_GPL(device_remove_file_self);
2287 * device_create_bin_file - create sysfs binary attribute file for device.
2289 * @attr: device binary attribute descriptor.
2291 int device_create_bin_file(struct device *dev,
2292 const struct bin_attribute *attr)
2294 int error = -EINVAL;
2296 error = sysfs_create_bin_file(&dev->kobj, attr);
2299 EXPORT_SYMBOL_GPL(device_create_bin_file);
2302 * device_remove_bin_file - remove sysfs binary attribute file
2304 * @attr: device binary attribute descriptor.
2306 void device_remove_bin_file(struct device *dev,
2307 const struct bin_attribute *attr)
2310 sysfs_remove_bin_file(&dev->kobj, attr);
2312 EXPORT_SYMBOL_GPL(device_remove_bin_file);
2314 static void klist_children_get(struct klist_node *n)
2316 struct device_private *p = to_device_private_parent(n);
2317 struct device *dev = p->device;
2322 static void klist_children_put(struct klist_node *n)
2324 struct device_private *p = to_device_private_parent(n);
2325 struct device *dev = p->device;
2331 * device_initialize - init device structure.
2334 * This prepares the device for use by other layers by initializing
2336 * It is the first half of device_register(), if called by
2337 * that function, though it can also be called separately, so one
2338 * may use @dev's fields. In particular, get_device()/put_device()
2339 * may be used for reference counting of @dev after calling this
2342 * All fields in @dev must be initialized by the caller to 0, except
2343 * for those explicitly set to some other value. The simplest
2344 * approach is to use kzalloc() to allocate the structure containing
2347 * NOTE: Use put_device() to give up your reference instead of freeing
2348 * @dev directly once you have called this function.
2350 void device_initialize(struct device *dev)
2352 dev->kobj.kset = devices_kset;
2353 kobject_init(&dev->kobj, &device_ktype);
2354 INIT_LIST_HEAD(&dev->dma_pools);
2355 mutex_init(&dev->mutex);
2356 #ifdef CONFIG_PROVE_LOCKING
2357 mutex_init(&dev->lockdep_mutex);
2359 lockdep_set_novalidate_class(&dev->mutex);
2360 spin_lock_init(&dev->devres_lock);
2361 INIT_LIST_HEAD(&dev->devres_head);
2362 device_pm_init(dev);
2363 set_dev_node(dev, -1);
2364 #ifdef CONFIG_GENERIC_MSI_IRQ
2365 INIT_LIST_HEAD(&dev->msi_list);
2367 INIT_LIST_HEAD(&dev->links.consumers);
2368 INIT_LIST_HEAD(&dev->links.suppliers);
2369 INIT_LIST_HEAD(&dev->links.needs_suppliers);
2370 INIT_LIST_HEAD(&dev->links.defer_sync);
2371 dev->links.status = DL_DEV_NO_DRIVER;
2373 EXPORT_SYMBOL_GPL(device_initialize);
2375 struct kobject *virtual_device_parent(struct device *dev)
2377 static struct kobject *virtual_dir = NULL;
2380 virtual_dir = kobject_create_and_add("virtual",
2381 &devices_kset->kobj);
2387 struct kobject kobj;
2388 struct class *class;
2391 #define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
2393 static void class_dir_release(struct kobject *kobj)
2395 struct class_dir *dir = to_class_dir(kobj);
2400 struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
2402 struct class_dir *dir = to_class_dir(kobj);
2403 return dir->class->ns_type;
2406 static struct kobj_type class_dir_ktype = {
2407 .release = class_dir_release,
2408 .sysfs_ops = &kobj_sysfs_ops,
2409 .child_ns_type = class_dir_child_ns_type
2412 static struct kobject *
2413 class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
2415 struct class_dir *dir;
2418 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
2420 return ERR_PTR(-ENOMEM);
2423 kobject_init(&dir->kobj, &class_dir_ktype);
2425 dir->kobj.kset = &class->p->glue_dirs;
2427 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
2429 kobject_put(&dir->kobj);
2430 return ERR_PTR(retval);
2435 static DEFINE_MUTEX(gdp_mutex);
2437 static struct kobject *get_device_parent(struct device *dev,
2438 struct device *parent)
2441 struct kobject *kobj = NULL;
2442 struct kobject *parent_kobj;
2446 /* block disks show up in /sys/block */
2447 if (sysfs_deprecated && dev->class == &block_class) {
2448 if (parent && parent->class == &block_class)
2449 return &parent->kobj;
2450 return &block_class.p->subsys.kobj;
2455 * If we have no parent, we live in "virtual".
2456 * Class-devices with a non class-device as parent, live
2457 * in a "glue" directory to prevent namespace collisions.
2460 parent_kobj = virtual_device_parent(dev);
2461 else if (parent->class && !dev->class->ns_type)
2462 return &parent->kobj;
2464 parent_kobj = &parent->kobj;
2466 mutex_lock(&gdp_mutex);
2468 /* find our class-directory at the parent and reference it */
2469 spin_lock(&dev->class->p->glue_dirs.list_lock);
2470 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
2471 if (k->parent == parent_kobj) {
2472 kobj = kobject_get(k);
2475 spin_unlock(&dev->class->p->glue_dirs.list_lock);
2477 mutex_unlock(&gdp_mutex);
2481 /* or create a new class-directory at the parent device */
2482 k = class_dir_create_and_add(dev->class, parent_kobj);
2483 /* do not emit an uevent for this simple "glue" directory */
2484 mutex_unlock(&gdp_mutex);
2488 /* subsystems can specify a default root directory for their devices */
2489 if (!parent && dev->bus && dev->bus->dev_root)
2490 return &dev->bus->dev_root->kobj;
2493 return &parent->kobj;
2497 static inline bool live_in_glue_dir(struct kobject *kobj,
2500 if (!kobj || !dev->class ||
2501 kobj->kset != &dev->class->p->glue_dirs)
2506 static inline struct kobject *get_glue_dir(struct device *dev)
2508 return dev->kobj.parent;
2512 * make sure cleaning up dir as the last step, we need to make
2513 * sure .release handler of kobject is run with holding the
2516 static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
2520 /* see if we live in a "glue" directory */
2521 if (!live_in_glue_dir(glue_dir, dev))
2524 mutex_lock(&gdp_mutex);
2526 * There is a race condition between removing glue directory
2527 * and adding a new device under the glue directory.
2532 * get_device_parent()
2533 * class_dir_create_and_add()
2534 * kobject_add_internal()
2535 * create_dir() // create glue_dir
2538 * get_device_parent()
2539 * kobject_get() // get glue_dir
2542 * cleanup_glue_dir()
2543 * kobject_del(glue_dir)
2546 * kobject_add_internal()
2547 * create_dir() // in glue_dir
2548 * sysfs_create_dir_ns()
2549 * kernfs_create_dir_ns(sd)
2551 * sysfs_remove_dir() // glue_dir->sd=NULL
2552 * sysfs_put() // free glue_dir->sd
2555 * kernfs_new_node(sd)
2556 * kernfs_get(glue_dir)
2560 * Before CPU1 remove last child device under glue dir, if CPU2 add
2561 * a new device under glue dir, the glue_dir kobject reference count
2562 * will be increase to 2 in kobject_get(k). And CPU2 has been called
2563 * kernfs_create_dir_ns(). Meanwhile, CPU1 call sysfs_remove_dir()
2564 * and sysfs_put(). This result in glue_dir->sd is freed.
2566 * Then the CPU2 will see a stale "empty" but still potentially used
2567 * glue dir around in kernfs_new_node().
2569 * In order to avoid this happening, we also should make sure that
2570 * kernfs_node for glue_dir is released in CPU1 only when refcount
2571 * for glue_dir kobj is 1.
2573 ref = kref_read(&glue_dir->kref);
2574 if (!kobject_has_children(glue_dir) && !--ref)
2575 kobject_del(glue_dir);
2576 kobject_put(glue_dir);
2577 mutex_unlock(&gdp_mutex);
2580 static int device_add_class_symlinks(struct device *dev)
2582 struct device_node *of_node = dev_of_node(dev);
2586 error = sysfs_create_link(&dev->kobj, of_node_kobj(of_node), "of_node");
2588 dev_warn(dev, "Error %d creating of_node link\n",error);
2589 /* An error here doesn't warrant bringing down the device */
2595 error = sysfs_create_link(&dev->kobj,
2596 &dev->class->p->subsys.kobj,
2601 if (dev->parent && device_is_not_partition(dev)) {
2602 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
2609 /* /sys/block has directories and does not need symlinks */
2610 if (sysfs_deprecated && dev->class == &block_class)
2614 /* link in the class directory pointing to the device */
2615 error = sysfs_create_link(&dev->class->p->subsys.kobj,
2616 &dev->kobj, dev_name(dev));
2623 sysfs_remove_link(&dev->kobj, "device");
2626 sysfs_remove_link(&dev->kobj, "subsystem");
2628 sysfs_remove_link(&dev->kobj, "of_node");
2632 static void device_remove_class_symlinks(struct device *dev)
2634 if (dev_of_node(dev))
2635 sysfs_remove_link(&dev->kobj, "of_node");
2640 if (dev->parent && device_is_not_partition(dev))
2641 sysfs_remove_link(&dev->kobj, "device");
2642 sysfs_remove_link(&dev->kobj, "subsystem");
2644 if (sysfs_deprecated && dev->class == &block_class)
2647 sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
2651 * dev_set_name - set a device name
2653 * @fmt: format string for the device's name
2655 int dev_set_name(struct device *dev, const char *fmt, ...)
2660 va_start(vargs, fmt);
2661 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
2665 EXPORT_SYMBOL_GPL(dev_set_name);
2668 * device_to_dev_kobj - select a /sys/dev/ directory for the device
2671 * By default we select char/ for new entries. Setting class->dev_obj
2672 * to NULL prevents an entry from being created. class->dev_kobj must
2673 * be set (or cleared) before any devices are registered to the class
2674 * otherwise device_create_sys_dev_entry() and
2675 * device_remove_sys_dev_entry() will disagree about the presence of
2678 static struct kobject *device_to_dev_kobj(struct device *dev)
2680 struct kobject *kobj;
2683 kobj = dev->class->dev_kobj;
2685 kobj = sysfs_dev_char_kobj;
2690 static int device_create_sys_dev_entry(struct device *dev)
2692 struct kobject *kobj = device_to_dev_kobj(dev);
2697 format_dev_t(devt_str, dev->devt);
2698 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
2704 static void device_remove_sys_dev_entry(struct device *dev)
2706 struct kobject *kobj = device_to_dev_kobj(dev);
2710 format_dev_t(devt_str, dev->devt);
2711 sysfs_remove_link(kobj, devt_str);
2715 static int device_private_init(struct device *dev)
2717 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
2720 dev->p->device = dev;
2721 klist_init(&dev->p->klist_children, klist_children_get,
2722 klist_children_put);
2723 INIT_LIST_HEAD(&dev->p->deferred_probe);
2728 * device_add - add device to device hierarchy.
2731 * This is part 2 of device_register(), though may be called
2732 * separately _iff_ device_initialize() has been called separately.
2734 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
2735 * to the global and sibling lists for the device, then
2736 * adds it to the other relevant subsystems of the driver model.
2738 * Do not call this routine or device_register() more than once for
2739 * any device structure. The driver model core is not designed to work
2740 * with devices that get unregistered and then spring back to life.
2741 * (Among other things, it's very hard to guarantee that all references
2742 * to the previous incarnation of @dev have been dropped.) Allocate
2743 * and register a fresh new struct device instead.
2745 * NOTE: _Never_ directly free @dev after calling this function, even
2746 * if it returned an error! Always use put_device() to give up your
2747 * reference instead.
2749 * Rule of thumb is: if device_add() succeeds, you should call
2750 * device_del() when you want to get rid of it. If device_add() has
2751 * *not* succeeded, use *only* put_device() to drop the reference
2754 int device_add(struct device *dev)
2756 struct device *parent;
2757 struct kobject *kobj;
2758 struct class_interface *class_intf;
2759 int error = -EINVAL;
2760 struct kobject *glue_dir = NULL;
2762 dev = get_device(dev);
2767 error = device_private_init(dev);
2773 * for statically allocated devices, which should all be converted
2774 * some day, we need to initialize the name. We prevent reading back
2775 * the name, and force the use of dev_name()
2777 if (dev->init_name) {
2778 dev_set_name(dev, "%s", dev->init_name);
2779 dev->init_name = NULL;
2782 /* subsystems can specify simple device enumeration */
2783 if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
2784 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
2786 if (!dev_name(dev)) {
2791 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
2793 parent = get_device(dev->parent);
2794 kobj = get_device_parent(dev, parent);
2796 error = PTR_ERR(kobj);
2800 dev->kobj.parent = kobj;
2802 /* use parent numa_node */
2803 if (parent && (dev_to_node(dev) == NUMA_NO_NODE))
2804 set_dev_node(dev, dev_to_node(parent));
2806 /* first, register with generic layer. */
2807 /* we require the name to be set before, and pass NULL */
2808 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
2810 glue_dir = get_glue_dir(dev);
2814 /* notify platform of device entry */
2815 error = device_platform_notify(dev, KOBJ_ADD);
2817 goto platform_error;
2819 error = device_create_file(dev, &dev_attr_uevent);
2823 error = device_add_class_symlinks(dev);
2826 error = device_add_attrs(dev);
2829 error = bus_add_device(dev);
2832 error = dpm_sysfs_add(dev);
2837 if (MAJOR(dev->devt)) {
2838 error = device_create_file(dev, &dev_attr_dev);
2842 error = device_create_sys_dev_entry(dev);
2846 devtmpfs_create_node(dev);
2849 /* Notify clients of device addition. This call must come
2850 * after dpm_sysfs_add() and before kobject_uevent().
2853 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2854 BUS_NOTIFY_ADD_DEVICE, dev);
2856 kobject_uevent(&dev->kobj, KOBJ_ADD);
2859 * Check if any of the other devices (consumers) have been waiting for
2860 * this device (supplier) to be added so that they can create a device
2863 * This needs to happen after device_pm_add() because device_link_add()
2864 * requires the supplier be registered before it's called.
2866 * But this also needs to happen before bus_probe_device() to make sure
2867 * waiting consumers can link to it before the driver is bound to the
2868 * device and the driver sync_state callback is called for this device.
2870 if (dev->fwnode && !dev->fwnode->dev) {
2871 dev->fwnode->dev = dev;
2872 fw_devlink_link_device(dev);
2875 bus_probe_device(dev);
2877 klist_add_tail(&dev->p->knode_parent,
2878 &parent->p->klist_children);
2881 mutex_lock(&dev->class->p->mutex);
2882 /* tie the class to the device */
2883 klist_add_tail(&dev->p->knode_class,
2884 &dev->class->p->klist_devices);
2886 /* notify any interfaces that the device is here */
2887 list_for_each_entry(class_intf,
2888 &dev->class->p->interfaces, node)
2889 if (class_intf->add_dev)
2890 class_intf->add_dev(dev, class_intf);
2891 mutex_unlock(&dev->class->p->mutex);
2897 if (MAJOR(dev->devt))
2898 device_remove_file(dev, &dev_attr_dev);
2900 device_pm_remove(dev);
2901 dpm_sysfs_remove(dev);
2903 bus_remove_device(dev);
2905 device_remove_attrs(dev);
2907 device_remove_class_symlinks(dev);
2909 device_remove_file(dev, &dev_attr_uevent);
2911 device_platform_notify(dev, KOBJ_REMOVE);
2913 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
2914 glue_dir = get_glue_dir(dev);
2915 kobject_del(&dev->kobj);
2917 cleanup_glue_dir(dev, glue_dir);
2925 EXPORT_SYMBOL_GPL(device_add);
2928 * device_register - register a device with the system.
2929 * @dev: pointer to the device structure
2931 * This happens in two clean steps - initialize the device
2932 * and add it to the system. The two steps can be called
2933 * separately, but this is the easiest and most common.
2934 * I.e. you should only call the two helpers separately if
2935 * have a clearly defined need to use and refcount the device
2936 * before it is added to the hierarchy.
2938 * For more information, see the kerneldoc for device_initialize()
2941 * NOTE: _Never_ directly free @dev after calling this function, even
2942 * if it returned an error! Always use put_device() to give up the
2943 * reference initialized in this function instead.
2945 int device_register(struct device *dev)
2947 device_initialize(dev);
2948 return device_add(dev);
2950 EXPORT_SYMBOL_GPL(device_register);
2953 * get_device - increment reference count for device.
2956 * This simply forwards the call to kobject_get(), though
2957 * we do take care to provide for the case that we get a NULL
2958 * pointer passed in.
2960 struct device *get_device(struct device *dev)
2962 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
2964 EXPORT_SYMBOL_GPL(get_device);
2967 * put_device - decrement reference count.
2968 * @dev: device in question.
2970 void put_device(struct device *dev)
2972 /* might_sleep(); */
2974 kobject_put(&dev->kobj);
2976 EXPORT_SYMBOL_GPL(put_device);
2978 bool kill_device(struct device *dev)
2981 * Require the device lock and set the "dead" flag to guarantee that
2982 * the update behavior is consistent with the other bitfields near
2983 * it and that we cannot have an asynchronous probe routine trying
2984 * to run while we are tearing out the bus/class/sysfs from
2985 * underneath the device.
2987 lockdep_assert_held(&dev->mutex);
2991 dev->p->dead = true;
2994 EXPORT_SYMBOL_GPL(kill_device);
2997 * device_del - delete device from system.
3000 * This is the first part of the device unregistration
3001 * sequence. This removes the device from the lists we control
3002 * from here, has it removed from the other driver model
3003 * subsystems it was added to in device_add(), and removes it
3004 * from the kobject hierarchy.
3006 * NOTE: this should be called manually _iff_ device_add() was
3007 * also called manually.
3009 void device_del(struct device *dev)
3011 struct device *parent = dev->parent;
3012 struct kobject *glue_dir = NULL;
3013 struct class_interface *class_intf;
3019 if (dev->fwnode && dev->fwnode->dev == dev)
3020 dev->fwnode->dev = NULL;
3022 /* Notify clients of device removal. This call must come
3023 * before dpm_sysfs_remove().
3026 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
3027 BUS_NOTIFY_DEL_DEVICE, dev);
3029 dpm_sysfs_remove(dev);
3031 klist_del(&dev->p->knode_parent);
3032 if (MAJOR(dev->devt)) {
3033 devtmpfs_delete_node(dev);
3034 device_remove_sys_dev_entry(dev);
3035 device_remove_file(dev, &dev_attr_dev);
3038 device_remove_class_symlinks(dev);
3040 mutex_lock(&dev->class->p->mutex);
3041 /* notify any interfaces that the device is now gone */
3042 list_for_each_entry(class_intf,
3043 &dev->class->p->interfaces, node)
3044 if (class_intf->remove_dev)
3045 class_intf->remove_dev(dev, class_intf);
3046 /* remove the device from the class list */
3047 klist_del(&dev->p->knode_class);
3048 mutex_unlock(&dev->class->p->mutex);
3050 device_remove_file(dev, &dev_attr_uevent);
3051 device_remove_attrs(dev);
3052 bus_remove_device(dev);
3053 device_pm_remove(dev);
3054 driver_deferred_probe_del(dev);
3055 device_platform_notify(dev, KOBJ_REMOVE);
3056 device_remove_properties(dev);
3057 device_links_purge(dev);
3060 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
3061 BUS_NOTIFY_REMOVED_DEVICE, dev);
3062 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
3063 glue_dir = get_glue_dir(dev);
3064 kobject_del(&dev->kobj);
3065 cleanup_glue_dir(dev, glue_dir);
3068 EXPORT_SYMBOL_GPL(device_del);
3071 * device_unregister - unregister device from system.
3072 * @dev: device going away.
3074 * We do this in two parts, like we do device_register(). First,
3075 * we remove it from all the subsystems with device_del(), then
3076 * we decrement the reference count via put_device(). If that
3077 * is the final reference count, the device will be cleaned up
3078 * via device_release() above. Otherwise, the structure will
3079 * stick around until the final reference to the device is dropped.
3081 void device_unregister(struct device *dev)
3083 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
3087 EXPORT_SYMBOL_GPL(device_unregister);
3089 static struct device *prev_device(struct klist_iter *i)
3091 struct klist_node *n = klist_prev(i);
3092 struct device *dev = NULL;
3093 struct device_private *p;
3096 p = to_device_private_parent(n);
3102 static struct device *next_device(struct klist_iter *i)
3104 struct klist_node *n = klist_next(i);
3105 struct device *dev = NULL;
3106 struct device_private *p;
3109 p = to_device_private_parent(n);
3116 * device_get_devnode - path of device node file
3118 * @mode: returned file access mode
3119 * @uid: returned file owner
3120 * @gid: returned file group
3121 * @tmp: possibly allocated string
3123 * Return the relative path of a possible device node.
3124 * Non-default names may need to allocate a memory to compose
3125 * a name. This memory is returned in tmp and needs to be
3126 * freed by the caller.
3128 const char *device_get_devnode(struct device *dev,
3129 umode_t *mode, kuid_t *uid, kgid_t *gid,
3136 /* the device type may provide a specific name */
3137 if (dev->type && dev->type->devnode)
3138 *tmp = dev->type->devnode(dev, mode, uid, gid);
3142 /* the class may provide a specific name */
3143 if (dev->class && dev->class->devnode)
3144 *tmp = dev->class->devnode(dev, mode);
3148 /* return name without allocation, tmp == NULL */
3149 if (strchr(dev_name(dev), '!') == NULL)
3150 return dev_name(dev);
3152 /* replace '!' in the name with '/' */
3153 s = kstrdup(dev_name(dev), GFP_KERNEL);
3156 strreplace(s, '!', '/');
3161 * device_for_each_child - device child iterator.
3162 * @parent: parent struct device.
3163 * @fn: function to be called for each device.
3164 * @data: data for the callback.
3166 * Iterate over @parent's child devices, and call @fn for each,
3169 * We check the return of @fn each time. If it returns anything
3170 * other than 0, we break out and return that value.
3172 int device_for_each_child(struct device *parent, void *data,
3173 int (*fn)(struct device *dev, void *data))
3175 struct klist_iter i;
3176 struct device *child;
3182 klist_iter_init(&parent->p->klist_children, &i);
3183 while (!error && (child = next_device(&i)))
3184 error = fn(child, data);
3185 klist_iter_exit(&i);
3188 EXPORT_SYMBOL_GPL(device_for_each_child);
3191 * device_for_each_child_reverse - device child iterator in reversed order.
3192 * @parent: parent struct device.
3193 * @fn: function to be called for each device.
3194 * @data: data for the callback.
3196 * Iterate over @parent's child devices, and call @fn for each,
3199 * We check the return of @fn each time. If it returns anything
3200 * other than 0, we break out and return that value.
3202 int device_for_each_child_reverse(struct device *parent, void *data,
3203 int (*fn)(struct device *dev, void *data))
3205 struct klist_iter i;
3206 struct device *child;
3212 klist_iter_init(&parent->p->klist_children, &i);
3213 while ((child = prev_device(&i)) && !error)
3214 error = fn(child, data);
3215 klist_iter_exit(&i);
3218 EXPORT_SYMBOL_GPL(device_for_each_child_reverse);
3221 * device_find_child - device iterator for locating a particular device.
3222 * @parent: parent struct device
3223 * @match: Callback function to check device
3224 * @data: Data to pass to match function
3226 * This is similar to the device_for_each_child() function above, but it
3227 * returns a reference to a device that is 'found' for later use, as
3228 * determined by the @match callback.
3230 * The callback should return 0 if the device doesn't match and non-zero
3231 * if it does. If the callback returns non-zero and a reference to the
3232 * current device can be obtained, this function will return to the caller
3233 * and not iterate over any more devices.
3235 * NOTE: you will need to drop the reference with put_device() after use.
3237 struct device *device_find_child(struct device *parent, void *data,
3238 int (*match)(struct device *dev, void *data))
3240 struct klist_iter i;
3241 struct device *child;
3246 klist_iter_init(&parent->p->klist_children, &i);
3247 while ((child = next_device(&i)))
3248 if (match(child, data) && get_device(child))
3250 klist_iter_exit(&i);
3253 EXPORT_SYMBOL_GPL(device_find_child);
3256 * device_find_child_by_name - device iterator for locating a child device.
3257 * @parent: parent struct device
3258 * @name: name of the child device
3260 * This is similar to the device_find_child() function above, but it
3261 * returns a reference to a device that has the name @name.
3263 * NOTE: you will need to drop the reference with put_device() after use.
3265 struct device *device_find_child_by_name(struct device *parent,
3268 struct klist_iter i;
3269 struct device *child;
3274 klist_iter_init(&parent->p->klist_children, &i);
3275 while ((child = next_device(&i)))
3276 if (!strcmp(dev_name(child), name) && get_device(child))
3278 klist_iter_exit(&i);
3281 EXPORT_SYMBOL_GPL(device_find_child_by_name);
3283 int __init devices_init(void)
3285 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
3288 dev_kobj = kobject_create_and_add("dev", NULL);
3291 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
3292 if (!sysfs_dev_block_kobj)
3293 goto block_kobj_err;
3294 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
3295 if (!sysfs_dev_char_kobj)
3301 kobject_put(sysfs_dev_block_kobj);
3303 kobject_put(dev_kobj);
3305 kset_unregister(devices_kset);
3309 static int device_check_offline(struct device *dev, void *not_used)
3313 ret = device_for_each_child(dev, NULL, device_check_offline);
3317 return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
3321 * device_offline - Prepare the device for hot-removal.
3322 * @dev: Device to be put offline.
3324 * Execute the device bus type's .offline() callback, if present, to prepare
3325 * the device for a subsequent hot-removal. If that succeeds, the device must
3326 * not be used until either it is removed or its bus type's .online() callback
3329 * Call under device_hotplug_lock.
3331 int device_offline(struct device *dev)
3335 if (dev->offline_disabled)
3338 ret = device_for_each_child(dev, NULL, device_check_offline);
3343 if (device_supports_offline(dev)) {
3347 ret = dev->bus->offline(dev);
3349 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
3350 dev->offline = true;
3360 * device_online - Put the device back online after successful device_offline().
3361 * @dev: Device to be put back online.
3363 * If device_offline() has been successfully executed for @dev, but the device
3364 * has not been removed subsequently, execute its bus type's .online() callback
3365 * to indicate that the device can be used again.
3367 * Call under device_hotplug_lock.
3369 int device_online(struct device *dev)
3374 if (device_supports_offline(dev)) {
3376 ret = dev->bus->online(dev);
3378 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
3379 dev->offline = false;
3390 struct root_device {
3392 struct module *owner;
3395 static inline struct root_device *to_root_device(struct device *d)
3397 return container_of(d, struct root_device, dev);
3400 static void root_device_release(struct device *dev)
3402 kfree(to_root_device(dev));
3406 * __root_device_register - allocate and register a root device
3407 * @name: root device name
3408 * @owner: owner module of the root device, usually THIS_MODULE
3410 * This function allocates a root device and registers it
3411 * using device_register(). In order to free the returned
3412 * device, use root_device_unregister().
3414 * Root devices are dummy devices which allow other devices
3415 * to be grouped under /sys/devices. Use this function to
3416 * allocate a root device and then use it as the parent of
3417 * any device which should appear under /sys/devices/{name}
3419 * The /sys/devices/{name} directory will also contain a
3420 * 'module' symlink which points to the @owner directory
3423 * Returns &struct device pointer on success, or ERR_PTR() on error.
3425 * Note: You probably want to use root_device_register().
3427 struct device *__root_device_register(const char *name, struct module *owner)
3429 struct root_device *root;
3432 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
3434 return ERR_PTR(err);
3436 err = dev_set_name(&root->dev, "%s", name);
3439 return ERR_PTR(err);
3442 root->dev.release = root_device_release;
3444 err = device_register(&root->dev);
3446 put_device(&root->dev);
3447 return ERR_PTR(err);
3450 #ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
3452 struct module_kobject *mk = &owner->mkobj;
3454 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
3456 device_unregister(&root->dev);
3457 return ERR_PTR(err);
3459 root->owner = owner;
3465 EXPORT_SYMBOL_GPL(__root_device_register);
3468 * root_device_unregister - unregister and free a root device
3469 * @dev: device going away
3471 * This function unregisters and cleans up a device that was created by
3472 * root_device_register().
3474 void root_device_unregister(struct device *dev)
3476 struct root_device *root = to_root_device(dev);
3479 sysfs_remove_link(&root->dev.kobj, "module");
3481 device_unregister(dev);
3483 EXPORT_SYMBOL_GPL(root_device_unregister);
3486 static void device_create_release(struct device *dev)
3488 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
3492 static __printf(6, 0) struct device *
3493 device_create_groups_vargs(struct class *class, struct device *parent,
3494 dev_t devt, void *drvdata,
3495 const struct attribute_group **groups,
3496 const char *fmt, va_list args)
3498 struct device *dev = NULL;
3499 int retval = -ENODEV;
3501 if (class == NULL || IS_ERR(class))
3504 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
3510 device_initialize(dev);
3513 dev->parent = parent;
3514 dev->groups = groups;
3515 dev->release = device_create_release;
3516 dev_set_drvdata(dev, drvdata);
3518 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
3522 retval = device_add(dev);
3530 return ERR_PTR(retval);
3534 * device_create - creates a device and registers it with sysfs
3535 * @class: pointer to the struct class that this device should be registered to
3536 * @parent: pointer to the parent struct device of this new device, if any
3537 * @devt: the dev_t for the char device to be added
3538 * @drvdata: the data to be added to the device for callbacks
3539 * @fmt: string for the device's name
3541 * This function can be used by char device classes. A struct device
3542 * will be created in sysfs, registered to the specified class.
3544 * A "dev" file will be created, showing the dev_t for the device, if
3545 * the dev_t is not 0,0.
3546 * If a pointer to a parent struct device is passed in, the newly created
3547 * struct device will be a child of that device in sysfs.
3548 * The pointer to the struct device will be returned from the call.
3549 * Any further sysfs files that might be required can be created using this
3552 * Returns &struct device pointer on success, or ERR_PTR() on error.
3554 * Note: the struct class passed to this function must have previously
3555 * been created with a call to class_create().
3557 struct device *device_create(struct class *class, struct device *parent,
3558 dev_t devt, void *drvdata, const char *fmt, ...)
3563 va_start(vargs, fmt);
3564 dev = device_create_groups_vargs(class, parent, devt, drvdata, NULL,
3569 EXPORT_SYMBOL_GPL(device_create);
3572 * device_create_with_groups - creates a device and registers it with sysfs
3573 * @class: pointer to the struct class that this device should be registered to
3574 * @parent: pointer to the parent struct device of this new device, if any
3575 * @devt: the dev_t for the char device to be added
3576 * @drvdata: the data to be added to the device for callbacks
3577 * @groups: NULL-terminated list of attribute groups to be created
3578 * @fmt: string for the device's name
3580 * This function can be used by char device classes. A struct device
3581 * will be created in sysfs, registered to the specified class.
3582 * Additional attributes specified in the groups parameter will also
3583 * be created automatically.
3585 * A "dev" file will be created, showing the dev_t for the device, if
3586 * the dev_t is not 0,0.
3587 * If a pointer to a parent struct device is passed in, the newly created
3588 * struct device will be a child of that device in sysfs.
3589 * The pointer to the struct device will be returned from the call.
3590 * Any further sysfs files that might be required can be created using this
3593 * Returns &struct device pointer on success, or ERR_PTR() on error.
3595 * Note: the struct class passed to this function must have previously
3596 * been created with a call to class_create().
3598 struct device *device_create_with_groups(struct class *class,
3599 struct device *parent, dev_t devt,
3601 const struct attribute_group **groups,
3602 const char *fmt, ...)
3607 va_start(vargs, fmt);
3608 dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
3613 EXPORT_SYMBOL_GPL(device_create_with_groups);
3616 * device_destroy - removes a device that was created with device_create()
3617 * @class: pointer to the struct class that this device was registered with
3618 * @devt: the dev_t of the device that was previously registered
3620 * This call unregisters and cleans up a device that was created with a
3621 * call to device_create().
3623 void device_destroy(struct class *class, dev_t devt)
3627 dev = class_find_device_by_devt(class, devt);
3630 device_unregister(dev);
3633 EXPORT_SYMBOL_GPL(device_destroy);
3636 * device_rename - renames a device
3637 * @dev: the pointer to the struct device to be renamed
3638 * @new_name: the new name of the device
3640 * It is the responsibility of the caller to provide mutual
3641 * exclusion between two different calls of device_rename
3642 * on the same device to ensure that new_name is valid and
3643 * won't conflict with other devices.
3645 * Note: Don't call this function. Currently, the networking layer calls this
3646 * function, but that will change. The following text from Kay Sievers offers
3649 * Renaming devices is racy at many levels, symlinks and other stuff are not
3650 * replaced atomically, and you get a "move" uevent, but it's not easy to
3651 * connect the event to the old and new device. Device nodes are not renamed at
3652 * all, there isn't even support for that in the kernel now.
3654 * In the meantime, during renaming, your target name might be taken by another
3655 * driver, creating conflicts. Or the old name is taken directly after you
3656 * renamed it -- then you get events for the same DEVPATH, before you even see
3657 * the "move" event. It's just a mess, and nothing new should ever rely on
3658 * kernel device renaming. Besides that, it's not even implemented now for
3659 * other things than (driver-core wise very simple) network devices.
3661 * We are currently about to change network renaming in udev to completely
3662 * disallow renaming of devices in the same namespace as the kernel uses,
3663 * because we can't solve the problems properly, that arise with swapping names
3664 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
3665 * be allowed to some other name than eth[0-9]*, for the aforementioned
3668 * Make up a "real" name in the driver before you register anything, or add
3669 * some other attributes for userspace to find the device, or use udev to add
3670 * symlinks -- but never rename kernel devices later, it's a complete mess. We
3671 * don't even want to get into that and try to implement the missing pieces in
3672 * the core. We really have other pieces to fix in the driver core mess. :)
3674 int device_rename(struct device *dev, const char *new_name)
3676 struct kobject *kobj = &dev->kobj;
3677 char *old_device_name = NULL;
3680 dev = get_device(dev);
3684 dev_dbg(dev, "renaming to %s\n", new_name);
3686 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
3687 if (!old_device_name) {
3693 error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj,
3694 kobj, old_device_name,
3695 new_name, kobject_namespace(kobj));
3700 error = kobject_rename(kobj, new_name);
3707 kfree(old_device_name);
3711 EXPORT_SYMBOL_GPL(device_rename);
3713 static int device_move_class_links(struct device *dev,
3714 struct device *old_parent,
3715 struct device *new_parent)
3720 sysfs_remove_link(&dev->kobj, "device");
3722 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
3728 * device_move - moves a device to a new parent
3729 * @dev: the pointer to the struct device to be moved
3730 * @new_parent: the new parent of the device (can be NULL)
3731 * @dpm_order: how to reorder the dpm_list
3733 int device_move(struct device *dev, struct device *new_parent,
3734 enum dpm_order dpm_order)
3737 struct device *old_parent;
3738 struct kobject *new_parent_kobj;
3740 dev = get_device(dev);
3745 new_parent = get_device(new_parent);
3746 new_parent_kobj = get_device_parent(dev, new_parent);
3747 if (IS_ERR(new_parent_kobj)) {
3748 error = PTR_ERR(new_parent_kobj);
3749 put_device(new_parent);
3753 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
3754 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
3755 error = kobject_move(&dev->kobj, new_parent_kobj);
3757 cleanup_glue_dir(dev, new_parent_kobj);
3758 put_device(new_parent);
3761 old_parent = dev->parent;
3762 dev->parent = new_parent;
3764 klist_remove(&dev->p->knode_parent);
3766 klist_add_tail(&dev->p->knode_parent,
3767 &new_parent->p->klist_children);
3768 set_dev_node(dev, dev_to_node(new_parent));
3772 error = device_move_class_links(dev, old_parent, new_parent);
3774 /* We ignore errors on cleanup since we're hosed anyway... */
3775 device_move_class_links(dev, new_parent, old_parent);
3776 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
3778 klist_remove(&dev->p->knode_parent);
3779 dev->parent = old_parent;
3781 klist_add_tail(&dev->p->knode_parent,
3782 &old_parent->p->klist_children);
3783 set_dev_node(dev, dev_to_node(old_parent));
3786 cleanup_glue_dir(dev, new_parent_kobj);
3787 put_device(new_parent);
3791 switch (dpm_order) {
3792 case DPM_ORDER_NONE:
3794 case DPM_ORDER_DEV_AFTER_PARENT:
3795 device_pm_move_after(dev, new_parent);
3796 devices_kset_move_after(dev, new_parent);
3798 case DPM_ORDER_PARENT_BEFORE_DEV:
3799 device_pm_move_before(new_parent, dev);
3800 devices_kset_move_before(new_parent, dev);
3802 case DPM_ORDER_DEV_LAST:
3803 device_pm_move_last(dev);
3804 devices_kset_move_last(dev);
3808 put_device(old_parent);
3814 EXPORT_SYMBOL_GPL(device_move);
3816 static int device_attrs_change_owner(struct device *dev, kuid_t kuid,
3819 struct kobject *kobj = &dev->kobj;
3820 struct class *class = dev->class;
3821 const struct device_type *type = dev->type;
3826 * Change the device groups of the device class for @dev to
3829 error = sysfs_groups_change_owner(kobj, class->dev_groups, kuid,
3837 * Change the device groups of the device type for @dev to
3840 error = sysfs_groups_change_owner(kobj, type->groups, kuid,
3846 /* Change the device groups of @dev to @kuid/@kgid. */
3847 error = sysfs_groups_change_owner(kobj, dev->groups, kuid, kgid);
3851 if (device_supports_offline(dev) && !dev->offline_disabled) {
3852 /* Change online device attributes of @dev to @kuid/@kgid. */
3853 error = sysfs_file_change_owner(kobj, dev_attr_online.attr.name,
3863 * device_change_owner - change the owner of an existing device.
3865 * @kuid: new owner's kuid
3866 * @kgid: new owner's kgid
3868 * This changes the owner of @dev and its corresponding sysfs entries to
3869 * @kuid/@kgid. This function closely mirrors how @dev was added via driver
3872 * Returns 0 on success or error code on failure.
3874 int device_change_owner(struct device *dev, kuid_t kuid, kgid_t kgid)
3877 struct kobject *kobj = &dev->kobj;
3879 dev = get_device(dev);
3884 * Change the kobject and the default attributes and groups of the
3885 * ktype associated with it to @kuid/@kgid.
3887 error = sysfs_change_owner(kobj, kuid, kgid);
3892 * Change the uevent file for @dev to the new owner. The uevent file
3893 * was created in a separate step when @dev got added and we mirror
3896 error = sysfs_file_change_owner(kobj, dev_attr_uevent.attr.name, kuid,
3902 * Change the device groups, the device groups associated with the
3903 * device class, and the groups associated with the device type of @dev
3906 error = device_attrs_change_owner(dev, kuid, kgid);
3910 error = dpm_sysfs_change_owner(dev, kuid, kgid);
3915 if (sysfs_deprecated && dev->class == &block_class)
3920 * Change the owner of the symlink located in the class directory of
3921 * the device class associated with @dev which points to the actual
3922 * directory entry for @dev to @kuid/@kgid. This ensures that the
3923 * symlink shows the same permissions as its target.
3925 error = sysfs_link_change_owner(&dev->class->p->subsys.kobj, &dev->kobj,
3926 dev_name(dev), kuid, kgid);
3934 EXPORT_SYMBOL_GPL(device_change_owner);
3937 * device_shutdown - call ->shutdown() on each device to shutdown.
3939 void device_shutdown(void)
3941 struct device *dev, *parent;
3943 wait_for_device_probe();
3944 device_block_probing();
3948 spin_lock(&devices_kset->list_lock);
3950 * Walk the devices list backward, shutting down each in turn.
3951 * Beware that device unplug events may also start pulling
3952 * devices offline, even as the system is shutting down.
3954 while (!list_empty(&devices_kset->list)) {
3955 dev = list_entry(devices_kset->list.prev, struct device,
3959 * hold reference count of device's parent to
3960 * prevent it from being freed because parent's
3961 * lock is to be held
3963 parent = get_device(dev->parent);
3966 * Make sure the device is off the kset list, in the
3967 * event that dev->*->shutdown() doesn't remove it.
3969 list_del_init(&dev->kobj.entry);
3970 spin_unlock(&devices_kset->list_lock);
3972 /* hold lock to avoid race with probe/release */
3974 device_lock(parent);
3977 /* Don't allow any more runtime suspends */
3978 pm_runtime_get_noresume(dev);
3979 pm_runtime_barrier(dev);
3981 if (dev->class && dev->class->shutdown_pre) {
3983 dev_info(dev, "shutdown_pre\n");
3984 dev->class->shutdown_pre(dev);
3986 if (dev->bus && dev->bus->shutdown) {
3988 dev_info(dev, "shutdown\n");
3989 dev->bus->shutdown(dev);
3990 } else if (dev->driver && dev->driver->shutdown) {
3992 dev_info(dev, "shutdown\n");
3993 dev->driver->shutdown(dev);
3998 device_unlock(parent);
4003 spin_lock(&devices_kset->list_lock);
4005 spin_unlock(&devices_kset->list_lock);
4009 * Device logging functions
4012 #ifdef CONFIG_PRINTK
4014 create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
4020 subsys = dev->class->name;
4022 subsys = dev->bus->name;
4026 pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys);
4031 * Add device identifier DEVICE=:
4035 * +sound:card0 subsystem:devname
4037 if (MAJOR(dev->devt)) {
4040 if (strcmp(subsys, "block") == 0)
4045 pos += snprintf(hdr + pos, hdrlen - pos,
4047 c, MAJOR(dev->devt), MINOR(dev->devt));
4048 } else if (strcmp(subsys, "net") == 0) {
4049 struct net_device *net = to_net_dev(dev);
4052 pos += snprintf(hdr + pos, hdrlen - pos,
4053 "DEVICE=n%u", net->ifindex);
4056 pos += snprintf(hdr + pos, hdrlen - pos,
4057 "DEVICE=+%s:%s", subsys, dev_name(dev));
4066 dev_WARN(dev, "device/subsystem name too long");
4070 int dev_vprintk_emit(int level, const struct device *dev,
4071 const char *fmt, va_list args)
4076 hdrlen = create_syslog_header(dev, hdr, sizeof(hdr));
4078 return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args);
4080 EXPORT_SYMBOL(dev_vprintk_emit);
4082 int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
4087 va_start(args, fmt);
4089 r = dev_vprintk_emit(level, dev, fmt, args);
4095 EXPORT_SYMBOL(dev_printk_emit);
4097 static void __dev_printk(const char *level, const struct device *dev,
4098 struct va_format *vaf)
4101 dev_printk_emit(level[1] - '0', dev, "%s %s: %pV",
4102 dev_driver_string(dev), dev_name(dev), vaf);
4104 printk("%s(NULL device *): %pV", level, vaf);
4107 void dev_printk(const char *level, const struct device *dev,
4108 const char *fmt, ...)
4110 struct va_format vaf;
4113 va_start(args, fmt);
4118 __dev_printk(level, dev, &vaf);
4122 EXPORT_SYMBOL(dev_printk);
4124 #define define_dev_printk_level(func, kern_level) \
4125 void func(const struct device *dev, const char *fmt, ...) \
4127 struct va_format vaf; \
4130 va_start(args, fmt); \
4135 __dev_printk(kern_level, dev, &vaf); \
4139 EXPORT_SYMBOL(func);
4141 define_dev_printk_level(_dev_emerg, KERN_EMERG);
4142 define_dev_printk_level(_dev_alert, KERN_ALERT);
4143 define_dev_printk_level(_dev_crit, KERN_CRIT);
4144 define_dev_printk_level(_dev_err, KERN_ERR);
4145 define_dev_printk_level(_dev_warn, KERN_WARNING);
4146 define_dev_printk_level(_dev_notice, KERN_NOTICE);
4147 define_dev_printk_level(_dev_info, KERN_INFO);
4151 static inline bool fwnode_is_primary(struct fwnode_handle *fwnode)
4153 return fwnode && !IS_ERR(fwnode->secondary);
4157 * set_primary_fwnode - Change the primary firmware node of a given device.
4158 * @dev: Device to handle.
4159 * @fwnode: New primary firmware node of the device.
4161 * Set the device's firmware node pointer to @fwnode, but if a secondary
4162 * firmware node of the device is present, preserve it.
4164 void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
4167 struct fwnode_handle *fn = dev->fwnode;
4169 if (fwnode_is_primary(fn))
4173 WARN_ON(fwnode->secondary);
4174 fwnode->secondary = fn;
4176 dev->fwnode = fwnode;
4178 dev->fwnode = fwnode_is_primary(dev->fwnode) ?
4179 dev->fwnode->secondary : NULL;
4182 EXPORT_SYMBOL_GPL(set_primary_fwnode);
4185 * set_secondary_fwnode - Change the secondary firmware node of a given device.
4186 * @dev: Device to handle.
4187 * @fwnode: New secondary firmware node of the device.
4189 * If a primary firmware node of the device is present, set its secondary
4190 * pointer to @fwnode. Otherwise, set the device's firmware node pointer to
4193 void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
4196 fwnode->secondary = ERR_PTR(-ENODEV);
4198 if (fwnode_is_primary(dev->fwnode))
4199 dev->fwnode->secondary = fwnode;
4201 dev->fwnode = fwnode;
4203 EXPORT_SYMBOL_GPL(set_secondary_fwnode);
4206 * device_set_of_node_from_dev - reuse device-tree node of another device
4207 * @dev: device whose device-tree node is being set
4208 * @dev2: device whose device-tree node is being reused
4210 * Takes another reference to the new device-tree node after first dropping
4211 * any reference held to the old node.
4213 void device_set_of_node_from_dev(struct device *dev, const struct device *dev2)
4215 of_node_put(dev->of_node);
4216 dev->of_node = of_node_get(dev2->of_node);
4217 dev->of_node_reused = true;
4219 EXPORT_SYMBOL_GPL(device_set_of_node_from_dev);
4221 int device_match_name(struct device *dev, const void *name)
4223 return sysfs_streq(dev_name(dev), name);
4225 EXPORT_SYMBOL_GPL(device_match_name);
4227 int device_match_of_node(struct device *dev, const void *np)
4229 return dev->of_node == np;
4231 EXPORT_SYMBOL_GPL(device_match_of_node);
4233 int device_match_fwnode(struct device *dev, const void *fwnode)
4235 return dev_fwnode(dev) == fwnode;
4237 EXPORT_SYMBOL_GPL(device_match_fwnode);
4239 int device_match_devt(struct device *dev, const void *pdevt)
4241 return dev->devt == *(dev_t *)pdevt;
4243 EXPORT_SYMBOL_GPL(device_match_devt);
4245 int device_match_acpi_dev(struct device *dev, const void *adev)
4247 return ACPI_COMPANION(dev) == adev;
4249 EXPORT_SYMBOL(device_match_acpi_dev);
4251 int device_match_any(struct device *dev, const void *unused)
4255 EXPORT_SYMBOL_GPL(device_match_any);