1 // SPDX-License-Identifier: GPL-2.0
3 * drivers/base/core.c - core driver model code (device registration, etc)
5 * Copyright (c) 2002-3 Patrick Mochel
6 * Copyright (c) 2002-3 Open Source Development Labs
8 * Copyright (c) 2006 Novell, Inc.
11 #include <linux/acpi.h>
12 #include <linux/device.h>
13 #include <linux/err.h>
14 #include <linux/fwnode.h>
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/string.h>
19 #include <linux/kdev_t.h>
20 #include <linux/notifier.h>
22 #include <linux/of_device.h>
23 #include <linux/genhd.h>
24 #include <linux/mutex.h>
25 #include <linux/pm_runtime.h>
26 #include <linux/netdevice.h>
27 #include <linux/sched/signal.h>
28 #include <linux/sysfs.h>
31 #include "power/power.h"
33 #ifdef CONFIG_SYSFS_DEPRECATED
34 #ifdef CONFIG_SYSFS_DEPRECATED_V2
35 long sysfs_deprecated = 1;
37 long sysfs_deprecated = 0;
39 static int __init sysfs_deprecated_setup(char *arg)
41 return kstrtol(arg, 10, &sysfs_deprecated);
43 early_param("sysfs.deprecated", sysfs_deprecated_setup);
46 /* Device links support. */
49 static DEFINE_MUTEX(device_links_lock);
50 DEFINE_STATIC_SRCU(device_links_srcu);
52 static inline void device_links_write_lock(void)
54 mutex_lock(&device_links_lock);
57 static inline void device_links_write_unlock(void)
59 mutex_unlock(&device_links_lock);
62 int device_links_read_lock(void)
64 return srcu_read_lock(&device_links_srcu);
67 void device_links_read_unlock(int idx)
69 srcu_read_unlock(&device_links_srcu, idx);
71 #else /* !CONFIG_SRCU */
72 static DECLARE_RWSEM(device_links_lock);
74 static inline void device_links_write_lock(void)
76 down_write(&device_links_lock);
79 static inline void device_links_write_unlock(void)
81 up_write(&device_links_lock);
84 int device_links_read_lock(void)
86 down_read(&device_links_lock);
90 void device_links_read_unlock(int not_used)
92 up_read(&device_links_lock);
94 #endif /* !CONFIG_SRCU */
97 * device_is_dependent - Check if one device depends on another one
98 * @dev: Device to check dependencies for.
99 * @target: Device to check against.
101 * Check if @target depends on @dev or any device dependent on it (its child or
102 * its consumer etc). Return 1 if that is the case or 0 otherwise.
104 static int device_is_dependent(struct device *dev, void *target)
106 struct device_link *link;
112 ret = device_for_each_child(dev, target, device_is_dependent);
116 list_for_each_entry(link, &dev->links.consumers, s_node) {
117 if (link->consumer == target)
120 ret = device_is_dependent(link->consumer, target);
127 static int device_reorder_to_tail(struct device *dev, void *not_used)
129 struct device_link *link;
132 * Devices that have not been registered yet will be put to the ends
133 * of the lists during the registration, so skip them here.
135 if (device_is_registered(dev))
136 devices_kset_move_last(dev);
138 if (device_pm_initialized(dev))
139 device_pm_move_last(dev);
141 device_for_each_child(dev, NULL, device_reorder_to_tail);
142 list_for_each_entry(link, &dev->links.consumers, s_node)
143 device_reorder_to_tail(link->consumer, NULL);
149 * device_pm_move_to_tail - Move set of devices to the end of device lists
150 * @dev: Device to move
152 * This is a device_reorder_to_tail() wrapper taking the requisite locks.
154 * It moves the @dev along with all of its children and all of its consumers
155 * to the ends of the device_kset and dpm_list, recursively.
157 void device_pm_move_to_tail(struct device *dev)
161 idx = device_links_read_lock();
163 device_reorder_to_tail(dev, NULL);
165 device_links_read_unlock(idx);
169 * device_link_add - Create a link between two devices.
170 * @consumer: Consumer end of the link.
171 * @supplier: Supplier end of the link.
172 * @flags: Link flags.
174 * The caller is responsible for the proper synchronization of the link creation
175 * with runtime PM. First, setting the DL_FLAG_PM_RUNTIME flag will cause the
176 * runtime PM framework to take the link into account. Second, if the
177 * DL_FLAG_RPM_ACTIVE flag is set in addition to it, the supplier devices will
178 * be forced into the active metastate and reference-counted upon the creation
179 * of the link. If DL_FLAG_PM_RUNTIME is not set, DL_FLAG_RPM_ACTIVE will be
182 * If the DL_FLAG_AUTOREMOVE_CONSUMER is set, the link will be removed
183 * automatically when the consumer device driver unbinds from it.
184 * The combination of both DL_FLAG_AUTOREMOVE_CONSUMER and DL_FLAG_STATELESS
185 * set is invalid and will cause NULL to be returned.
187 * A side effect of the link creation is re-ordering of dpm_list and the
188 * devices_kset list by moving the consumer device and all devices depending
189 * on it to the ends of these lists (that does not happen to devices that have
190 * not been registered when this function is called).
192 * The supplier device is required to be registered when this function is called
193 * and NULL will be returned if that is not the case. The consumer device need
194 * not be registered, however.
196 struct device_link *device_link_add(struct device *consumer,
197 struct device *supplier, u32 flags)
199 struct device_link *link;
201 if (!consumer || !supplier ||
202 ((flags & DL_FLAG_STATELESS) &&
203 (flags & DL_FLAG_AUTOREMOVE_CONSUMER)))
206 device_links_write_lock();
210 * If the supplier has not been fully registered yet or there is a
211 * reverse dependency between the consumer and the supplier already in
212 * the graph, return NULL.
214 if (!device_pm_initialized(supplier)
215 || device_is_dependent(consumer, supplier)) {
220 list_for_each_entry(link, &supplier->links.consumers, s_node)
221 if (link->consumer == consumer) {
222 kref_get(&link->kref);
226 link = kzalloc(sizeof(*link), GFP_KERNEL);
230 if (flags & DL_FLAG_PM_RUNTIME) {
231 if (flags & DL_FLAG_RPM_ACTIVE) {
232 if (pm_runtime_get_sync(supplier) < 0) {
233 pm_runtime_put_noidle(supplier);
238 link->rpm_active = true;
240 pm_runtime_new_link(consumer);
242 * If the link is being added by the consumer driver at probe
243 * time, balance the decrementation of the supplier's runtime PM
244 * usage counter after consumer probe in driver_probe_device().
246 if (consumer->links.status == DL_DEV_PROBING)
247 pm_runtime_get_noresume(supplier);
249 get_device(supplier);
250 link->supplier = supplier;
251 INIT_LIST_HEAD(&link->s_node);
252 get_device(consumer);
253 link->consumer = consumer;
254 INIT_LIST_HEAD(&link->c_node);
256 kref_init(&link->kref);
258 /* Determine the initial link state. */
259 if (flags & DL_FLAG_STATELESS) {
260 link->status = DL_STATE_NONE;
262 switch (supplier->links.status) {
263 case DL_DEV_DRIVER_BOUND:
264 switch (consumer->links.status) {
267 * Some callers expect the link creation during
268 * consumer driver probe to resume the supplier
269 * even without DL_FLAG_RPM_ACTIVE.
271 if (flags & DL_FLAG_PM_RUNTIME)
272 pm_runtime_resume(supplier);
274 link->status = DL_STATE_CONSUMER_PROBE;
276 case DL_DEV_DRIVER_BOUND:
277 link->status = DL_STATE_ACTIVE;
280 link->status = DL_STATE_AVAILABLE;
284 case DL_DEV_UNBINDING:
285 link->status = DL_STATE_SUPPLIER_UNBIND;
288 link->status = DL_STATE_DORMANT;
294 * Move the consumer and all of the devices depending on it to the end
295 * of dpm_list and the devices_kset list.
297 * It is necessary to hold dpm_list locked throughout all that or else
298 * we may end up suspending with a wrong ordering of it.
300 device_reorder_to_tail(consumer, NULL);
302 list_add_tail_rcu(&link->s_node, &supplier->links.consumers);
303 list_add_tail_rcu(&link->c_node, &consumer->links.suppliers);
305 dev_info(consumer, "Linked as a consumer to %s\n", dev_name(supplier));
309 device_links_write_unlock();
312 EXPORT_SYMBOL_GPL(device_link_add);
314 static void device_link_free(struct device_link *link)
316 put_device(link->consumer);
317 put_device(link->supplier);
322 static void __device_link_free_srcu(struct rcu_head *rhead)
324 device_link_free(container_of(rhead, struct device_link, rcu_head));
327 static void __device_link_del(struct kref *kref)
329 struct device_link *link = container_of(kref, struct device_link, kref);
331 dev_info(link->consumer, "Dropping the link to %s\n",
332 dev_name(link->supplier));
334 if (link->flags & DL_FLAG_PM_RUNTIME)
335 pm_runtime_drop_link(link->consumer);
337 list_del_rcu(&link->s_node);
338 list_del_rcu(&link->c_node);
339 call_srcu(&device_links_srcu, &link->rcu_head, __device_link_free_srcu);
341 #else /* !CONFIG_SRCU */
342 static void __device_link_del(struct kref *kref)
344 struct device_link *link = container_of(kref, struct device_link, kref);
346 dev_info(link->consumer, "Dropping the link to %s\n",
347 dev_name(link->supplier));
349 if (link->flags & DL_FLAG_PM_RUNTIME)
350 pm_runtime_drop_link(link->consumer);
352 list_del(&link->s_node);
353 list_del(&link->c_node);
354 device_link_free(link);
356 #endif /* !CONFIG_SRCU */
359 * device_link_del - Delete a link between two devices.
360 * @link: Device link to delete.
362 * The caller must ensure proper synchronization of this function with runtime
363 * PM. If the link was added multiple times, it needs to be deleted as often.
364 * Care is required for hotplugged devices: Their links are purged on removal
365 * and calling device_link_del() is then no longer allowed.
367 void device_link_del(struct device_link *link)
369 device_links_write_lock();
371 kref_put(&link->kref, __device_link_del);
373 device_links_write_unlock();
375 EXPORT_SYMBOL_GPL(device_link_del);
378 * device_link_remove - remove a link between two devices.
379 * @consumer: Consumer end of the link.
380 * @supplier: Supplier end of the link.
382 * The caller must ensure proper synchronization of this function with runtime
385 void device_link_remove(void *consumer, struct device *supplier)
387 struct device_link *link;
389 if (WARN_ON(consumer == supplier))
392 device_links_write_lock();
395 list_for_each_entry(link, &supplier->links.consumers, s_node) {
396 if (link->consumer == consumer) {
397 kref_put(&link->kref, __device_link_del);
403 device_links_write_unlock();
405 EXPORT_SYMBOL_GPL(device_link_remove);
407 static void device_links_missing_supplier(struct device *dev)
409 struct device_link *link;
411 list_for_each_entry(link, &dev->links.suppliers, c_node)
412 if (link->status == DL_STATE_CONSUMER_PROBE)
413 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
417 * device_links_check_suppliers - Check presence of supplier drivers.
418 * @dev: Consumer device.
420 * Check links from this device to any suppliers. Walk the list of the device's
421 * links to suppliers and see if all of them are available. If not, simply
422 * return -EPROBE_DEFER.
424 * We need to guarantee that the supplier will not go away after the check has
425 * been positive here. It only can go away in __device_release_driver() and
426 * that function checks the device's links to consumers. This means we need to
427 * mark the link as "consumer probe in progress" to make the supplier removal
428 * wait for us to complete (or bad things may happen).
430 * Links with the DL_FLAG_STATELESS flag set are ignored.
432 int device_links_check_suppliers(struct device *dev)
434 struct device_link *link;
437 device_links_write_lock();
439 list_for_each_entry(link, &dev->links.suppliers, c_node) {
440 if (link->flags & DL_FLAG_STATELESS)
443 if (link->status != DL_STATE_AVAILABLE) {
444 device_links_missing_supplier(dev);
448 WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE);
450 dev->links.status = DL_DEV_PROBING;
452 device_links_write_unlock();
457 * device_links_driver_bound - Update device links after probing its driver.
458 * @dev: Device to update the links for.
460 * The probe has been successful, so update links from this device to any
461 * consumers by changing their status to "available".
463 * Also change the status of @dev's links to suppliers to "active".
465 * Links with the DL_FLAG_STATELESS flag set are ignored.
467 void device_links_driver_bound(struct device *dev)
469 struct device_link *link;
471 device_links_write_lock();
473 list_for_each_entry(link, &dev->links.consumers, s_node) {
474 if (link->flags & DL_FLAG_STATELESS)
477 WARN_ON(link->status != DL_STATE_DORMANT);
478 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
481 list_for_each_entry(link, &dev->links.suppliers, c_node) {
482 if (link->flags & DL_FLAG_STATELESS)
485 WARN_ON(link->status != DL_STATE_CONSUMER_PROBE);
486 WRITE_ONCE(link->status, DL_STATE_ACTIVE);
489 dev->links.status = DL_DEV_DRIVER_BOUND;
491 device_links_write_unlock();
495 * __device_links_no_driver - Update links of a device without a driver.
496 * @dev: Device without a drvier.
498 * Delete all non-persistent links from this device to any suppliers.
500 * Persistent links stay around, but their status is changed to "available",
501 * unless they already are in the "supplier unbind in progress" state in which
502 * case they need not be updated.
504 * Links with the DL_FLAG_STATELESS flag set are ignored.
506 static void __device_links_no_driver(struct device *dev)
508 struct device_link *link, *ln;
510 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
511 if (link->flags & DL_FLAG_STATELESS)
514 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER)
515 kref_put(&link->kref, __device_link_del);
516 else if (link->status != DL_STATE_SUPPLIER_UNBIND)
517 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
520 dev->links.status = DL_DEV_NO_DRIVER;
523 void device_links_no_driver(struct device *dev)
525 device_links_write_lock();
526 __device_links_no_driver(dev);
527 device_links_write_unlock();
531 * device_links_driver_cleanup - Update links after driver removal.
532 * @dev: Device whose driver has just gone away.
534 * Update links to consumers for @dev by changing their status to "dormant" and
535 * invoke %__device_links_no_driver() to update links to suppliers for it as
538 * Links with the DL_FLAG_STATELESS flag set are ignored.
540 void device_links_driver_cleanup(struct device *dev)
542 struct device_link *link;
544 device_links_write_lock();
546 list_for_each_entry(link, &dev->links.consumers, s_node) {
547 if (link->flags & DL_FLAG_STATELESS)
550 WARN_ON(link->flags & DL_FLAG_AUTOREMOVE_CONSUMER);
551 WARN_ON(link->status != DL_STATE_SUPPLIER_UNBIND);
554 * autoremove the links between this @dev and its consumer
555 * devices that are not active, i.e. where the link state
556 * has moved to DL_STATE_SUPPLIER_UNBIND.
558 if (link->status == DL_STATE_SUPPLIER_UNBIND &&
559 link->flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
560 kref_put(&link->kref, __device_link_del);
562 WRITE_ONCE(link->status, DL_STATE_DORMANT);
565 __device_links_no_driver(dev);
567 device_links_write_unlock();
571 * device_links_busy - Check if there are any busy links to consumers.
572 * @dev: Device to check.
574 * Check each consumer of the device and return 'true' if its link's status
575 * is one of "consumer probe" or "active" (meaning that the given consumer is
576 * probing right now or its driver is present). Otherwise, change the link
577 * state to "supplier unbind" to prevent the consumer from being probed
578 * successfully going forward.
580 * Return 'false' if there are no probing or active consumers.
582 * Links with the DL_FLAG_STATELESS flag set are ignored.
584 bool device_links_busy(struct device *dev)
586 struct device_link *link;
589 device_links_write_lock();
591 list_for_each_entry(link, &dev->links.consumers, s_node) {
592 if (link->flags & DL_FLAG_STATELESS)
595 if (link->status == DL_STATE_CONSUMER_PROBE
596 || link->status == DL_STATE_ACTIVE) {
600 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
603 dev->links.status = DL_DEV_UNBINDING;
605 device_links_write_unlock();
610 * device_links_unbind_consumers - Force unbind consumers of the given device.
611 * @dev: Device to unbind the consumers of.
613 * Walk the list of links to consumers for @dev and if any of them is in the
614 * "consumer probe" state, wait for all device probes in progress to complete
617 * If that's not the case, change the status of the link to "supplier unbind"
618 * and check if the link was in the "active" state. If so, force the consumer
619 * driver to unbind and start over (the consumer will not re-probe as we have
620 * changed the state of the link already).
622 * Links with the DL_FLAG_STATELESS flag set are ignored.
624 void device_links_unbind_consumers(struct device *dev)
626 struct device_link *link;
629 device_links_write_lock();
631 list_for_each_entry(link, &dev->links.consumers, s_node) {
632 enum device_link_state status;
634 if (link->flags & DL_FLAG_STATELESS)
637 status = link->status;
638 if (status == DL_STATE_CONSUMER_PROBE) {
639 device_links_write_unlock();
641 wait_for_device_probe();
644 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
645 if (status == DL_STATE_ACTIVE) {
646 struct device *consumer = link->consumer;
648 get_device(consumer);
650 device_links_write_unlock();
652 device_release_driver_internal(consumer, NULL,
654 put_device(consumer);
659 device_links_write_unlock();
663 * device_links_purge - Delete existing links to other devices.
664 * @dev: Target device.
666 static void device_links_purge(struct device *dev)
668 struct device_link *link, *ln;
671 * Delete all of the remaining links from this device to any other
672 * devices (either consumers or suppliers).
674 device_links_write_lock();
676 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
677 WARN_ON(link->status == DL_STATE_ACTIVE);
678 __device_link_del(&link->kref);
681 list_for_each_entry_safe_reverse(link, ln, &dev->links.consumers, s_node) {
682 WARN_ON(link->status != DL_STATE_DORMANT &&
683 link->status != DL_STATE_NONE);
684 __device_link_del(&link->kref);
687 device_links_write_unlock();
690 /* Device links support end. */
692 int (*platform_notify)(struct device *dev) = NULL;
693 int (*platform_notify_remove)(struct device *dev) = NULL;
694 static struct kobject *dev_kobj;
695 struct kobject *sysfs_dev_char_kobj;
696 struct kobject *sysfs_dev_block_kobj;
698 static DEFINE_MUTEX(device_hotplug_lock);
700 void lock_device_hotplug(void)
702 mutex_lock(&device_hotplug_lock);
705 void unlock_device_hotplug(void)
707 mutex_unlock(&device_hotplug_lock);
710 int lock_device_hotplug_sysfs(void)
712 if (mutex_trylock(&device_hotplug_lock))
715 /* Avoid busy looping (5 ms of sleep should do). */
717 return restart_syscall();
721 static inline int device_is_not_partition(struct device *dev)
723 return !(dev->type == &part_type);
726 static inline int device_is_not_partition(struct device *dev)
733 device_platform_notify(struct device *dev, enum kobject_action action)
737 ret = acpi_platform_notify(dev, action);
741 ret = software_node_notify(dev, action);
745 if (platform_notify && action == KOBJ_ADD)
746 platform_notify(dev);
747 else if (platform_notify_remove && action == KOBJ_REMOVE)
748 platform_notify_remove(dev);
753 * dev_driver_string - Return a device's driver name, if at all possible
754 * @dev: struct device to get the name of
756 * Will return the device's driver's name if it is bound to a device. If
757 * the device is not bound to a driver, it will return the name of the bus
758 * it is attached to. If it is not attached to a bus either, an empty
759 * string will be returned.
761 const char *dev_driver_string(const struct device *dev)
763 struct device_driver *drv;
765 /* dev->driver can change to NULL underneath us because of unbinding,
766 * so be careful about accessing it. dev->bus and dev->class should
767 * never change once they are set, so they don't need special care.
769 drv = READ_ONCE(dev->driver);
770 return drv ? drv->name :
771 (dev->bus ? dev->bus->name :
772 (dev->class ? dev->class->name : ""));
774 EXPORT_SYMBOL(dev_driver_string);
776 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
778 static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
781 struct device_attribute *dev_attr = to_dev_attr(attr);
782 struct device *dev = kobj_to_dev(kobj);
786 ret = dev_attr->show(dev, dev_attr, buf);
787 if (ret >= (ssize_t)PAGE_SIZE) {
788 printk("dev_attr_show: %pS returned bad count\n",
794 static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
795 const char *buf, size_t count)
797 struct device_attribute *dev_attr = to_dev_attr(attr);
798 struct device *dev = kobj_to_dev(kobj);
802 ret = dev_attr->store(dev, dev_attr, buf, count);
806 static const struct sysfs_ops dev_sysfs_ops = {
807 .show = dev_attr_show,
808 .store = dev_attr_store,
811 #define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
813 ssize_t device_store_ulong(struct device *dev,
814 struct device_attribute *attr,
815 const char *buf, size_t size)
817 struct dev_ext_attribute *ea = to_ext_attr(attr);
819 unsigned long new = simple_strtoul(buf, &end, 0);
822 *(unsigned long *)(ea->var) = new;
823 /* Always return full write size even if we didn't consume all */
826 EXPORT_SYMBOL_GPL(device_store_ulong);
828 ssize_t device_show_ulong(struct device *dev,
829 struct device_attribute *attr,
832 struct dev_ext_attribute *ea = to_ext_attr(attr);
833 return snprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var));
835 EXPORT_SYMBOL_GPL(device_show_ulong);
837 ssize_t device_store_int(struct device *dev,
838 struct device_attribute *attr,
839 const char *buf, size_t size)
841 struct dev_ext_attribute *ea = to_ext_attr(attr);
843 long new = simple_strtol(buf, &end, 0);
844 if (end == buf || new > INT_MAX || new < INT_MIN)
846 *(int *)(ea->var) = new;
847 /* Always return full write size even if we didn't consume all */
850 EXPORT_SYMBOL_GPL(device_store_int);
852 ssize_t device_show_int(struct device *dev,
853 struct device_attribute *attr,
856 struct dev_ext_attribute *ea = to_ext_attr(attr);
858 return snprintf(buf, PAGE_SIZE, "%d\n", *(int *)(ea->var));
860 EXPORT_SYMBOL_GPL(device_show_int);
862 ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
863 const char *buf, size_t size)
865 struct dev_ext_attribute *ea = to_ext_attr(attr);
867 if (strtobool(buf, ea->var) < 0)
872 EXPORT_SYMBOL_GPL(device_store_bool);
874 ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
877 struct dev_ext_attribute *ea = to_ext_attr(attr);
879 return snprintf(buf, PAGE_SIZE, "%d\n", *(bool *)(ea->var));
881 EXPORT_SYMBOL_GPL(device_show_bool);
884 * device_release - free device structure.
885 * @kobj: device's kobject.
887 * This is called once the reference count for the object
888 * reaches 0. We forward the call to the device's release
889 * method, which should handle actually freeing the structure.
891 static void device_release(struct kobject *kobj)
893 struct device *dev = kobj_to_dev(kobj);
894 struct device_private *p = dev->p;
897 * Some platform devices are driven without driver attached
898 * and managed resources may have been acquired. Make sure
899 * all resources are released.
901 * Drivers still can add resources into device after device
902 * is deleted but alive, so release devres here to avoid
903 * possible memory leak.
905 devres_release_all(dev);
909 else if (dev->type && dev->type->release)
910 dev->type->release(dev);
911 else if (dev->class && dev->class->dev_release)
912 dev->class->dev_release(dev);
914 WARN(1, KERN_ERR "Device '%s' does not have a release() "
915 "function, it is broken and must be fixed.\n",
920 static const void *device_namespace(struct kobject *kobj)
922 struct device *dev = kobj_to_dev(kobj);
923 const void *ns = NULL;
925 if (dev->class && dev->class->ns_type)
926 ns = dev->class->namespace(dev);
931 static void device_get_ownership(struct kobject *kobj, kuid_t *uid, kgid_t *gid)
933 struct device *dev = kobj_to_dev(kobj);
935 if (dev->class && dev->class->get_ownership)
936 dev->class->get_ownership(dev, uid, gid);
939 static struct kobj_type device_ktype = {
940 .release = device_release,
941 .sysfs_ops = &dev_sysfs_ops,
942 .namespace = device_namespace,
943 .get_ownership = device_get_ownership,
947 static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
949 struct kobj_type *ktype = get_ktype(kobj);
951 if (ktype == &device_ktype) {
952 struct device *dev = kobj_to_dev(kobj);
961 static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
963 struct device *dev = kobj_to_dev(kobj);
966 return dev->bus->name;
968 return dev->class->name;
972 static int dev_uevent(struct kset *kset, struct kobject *kobj,
973 struct kobj_uevent_env *env)
975 struct device *dev = kobj_to_dev(kobj);
978 /* add device node properties if present */
979 if (MAJOR(dev->devt)) {
983 kuid_t uid = GLOBAL_ROOT_UID;
984 kgid_t gid = GLOBAL_ROOT_GID;
986 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
987 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
988 name = device_get_devnode(dev, &mode, &uid, &gid, &tmp);
990 add_uevent_var(env, "DEVNAME=%s", name);
992 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
993 if (!uid_eq(uid, GLOBAL_ROOT_UID))
994 add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid));
995 if (!gid_eq(gid, GLOBAL_ROOT_GID))
996 add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid));
1001 if (dev->type && dev->type->name)
1002 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
1005 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
1007 /* Add common DT information about the device */
1008 of_device_uevent(dev, env);
1010 /* have the bus specific function add its stuff */
1011 if (dev->bus && dev->bus->uevent) {
1012 retval = dev->bus->uevent(dev, env);
1014 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
1015 dev_name(dev), __func__, retval);
1018 /* have the class specific function add its stuff */
1019 if (dev->class && dev->class->dev_uevent) {
1020 retval = dev->class->dev_uevent(dev, env);
1022 pr_debug("device: '%s': %s: class uevent() "
1023 "returned %d\n", dev_name(dev),
1027 /* have the device type specific function add its stuff */
1028 if (dev->type && dev->type->uevent) {
1029 retval = dev->type->uevent(dev, env);
1031 pr_debug("device: '%s': %s: dev_type uevent() "
1032 "returned %d\n", dev_name(dev),
1039 static const struct kset_uevent_ops device_uevent_ops = {
1040 .filter = dev_uevent_filter,
1041 .name = dev_uevent_name,
1042 .uevent = dev_uevent,
1045 static ssize_t uevent_show(struct device *dev, struct device_attribute *attr,
1048 struct kobject *top_kobj;
1050 struct kobj_uevent_env *env = NULL;
1055 /* search the kset, the device belongs to */
1056 top_kobj = &dev->kobj;
1057 while (!top_kobj->kset && top_kobj->parent)
1058 top_kobj = top_kobj->parent;
1059 if (!top_kobj->kset)
1062 kset = top_kobj->kset;
1063 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
1066 /* respect filter */
1067 if (kset->uevent_ops && kset->uevent_ops->filter)
1068 if (!kset->uevent_ops->filter(kset, &dev->kobj))
1071 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
1075 /* let the kset specific function add its keys */
1076 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
1080 /* copy keys to file */
1081 for (i = 0; i < env->envp_idx; i++)
1082 count += sprintf(&buf[count], "%s\n", env->envp[i]);
1088 static ssize_t uevent_store(struct device *dev, struct device_attribute *attr,
1089 const char *buf, size_t count)
1091 if (kobject_synth_uevent(&dev->kobj, buf, count))
1092 dev_err(dev, "uevent: failed to send synthetic uevent\n");
1096 static DEVICE_ATTR_RW(uevent);
1098 static ssize_t online_show(struct device *dev, struct device_attribute *attr,
1104 val = !dev->offline;
1106 return sprintf(buf, "%u\n", val);
1109 static ssize_t online_store(struct device *dev, struct device_attribute *attr,
1110 const char *buf, size_t count)
1115 ret = strtobool(buf, &val);
1119 ret = lock_device_hotplug_sysfs();
1123 ret = val ? device_online(dev) : device_offline(dev);
1124 unlock_device_hotplug();
1125 return ret < 0 ? ret : count;
1127 static DEVICE_ATTR_RW(online);
1129 int device_add_groups(struct device *dev, const struct attribute_group **groups)
1131 return sysfs_create_groups(&dev->kobj, groups);
1133 EXPORT_SYMBOL_GPL(device_add_groups);
1135 void device_remove_groups(struct device *dev,
1136 const struct attribute_group **groups)
1138 sysfs_remove_groups(&dev->kobj, groups);
1140 EXPORT_SYMBOL_GPL(device_remove_groups);
1142 union device_attr_group_devres {
1143 const struct attribute_group *group;
1144 const struct attribute_group **groups;
1147 static int devm_attr_group_match(struct device *dev, void *res, void *data)
1149 return ((union device_attr_group_devres *)res)->group == data;
1152 static void devm_attr_group_remove(struct device *dev, void *res)
1154 union device_attr_group_devres *devres = res;
1155 const struct attribute_group *group = devres->group;
1157 dev_dbg(dev, "%s: removing group %p\n", __func__, group);
1158 sysfs_remove_group(&dev->kobj, group);
1161 static void devm_attr_groups_remove(struct device *dev, void *res)
1163 union device_attr_group_devres *devres = res;
1164 const struct attribute_group **groups = devres->groups;
1166 dev_dbg(dev, "%s: removing groups %p\n", __func__, groups);
1167 sysfs_remove_groups(&dev->kobj, groups);
1171 * devm_device_add_group - given a device, create a managed attribute group
1172 * @dev: The device to create the group for
1173 * @grp: The attribute group to create
1175 * This function creates a group for the first time. It will explicitly
1176 * warn and error if any of the attribute files being created already exist.
1178 * Returns 0 on success or error code on failure.
1180 int devm_device_add_group(struct device *dev, const struct attribute_group *grp)
1182 union device_attr_group_devres *devres;
1185 devres = devres_alloc(devm_attr_group_remove,
1186 sizeof(*devres), GFP_KERNEL);
1190 error = sysfs_create_group(&dev->kobj, grp);
1192 devres_free(devres);
1196 devres->group = grp;
1197 devres_add(dev, devres);
1200 EXPORT_SYMBOL_GPL(devm_device_add_group);
1203 * devm_device_remove_group: remove a managed group from a device
1204 * @dev: device to remove the group from
1205 * @grp: group to remove
1207 * This function removes a group of attributes from a device. The attributes
1208 * previously have to have been created for this group, otherwise it will fail.
1210 void devm_device_remove_group(struct device *dev,
1211 const struct attribute_group *grp)
1213 WARN_ON(devres_release(dev, devm_attr_group_remove,
1214 devm_attr_group_match,
1215 /* cast away const */ (void *)grp));
1217 EXPORT_SYMBOL_GPL(devm_device_remove_group);
1220 * devm_device_add_groups - create a bunch of managed attribute groups
1221 * @dev: The device to create the group for
1222 * @groups: The attribute groups to create, NULL terminated
1224 * This function creates a bunch of managed attribute groups. If an error
1225 * occurs when creating a group, all previously created groups will be
1226 * removed, unwinding everything back to the original state when this
1227 * function was called. It will explicitly warn and error if any of the
1228 * attribute files being created already exist.
1230 * Returns 0 on success or error code from sysfs_create_group on failure.
1232 int devm_device_add_groups(struct device *dev,
1233 const struct attribute_group **groups)
1235 union device_attr_group_devres *devres;
1238 devres = devres_alloc(devm_attr_groups_remove,
1239 sizeof(*devres), GFP_KERNEL);
1243 error = sysfs_create_groups(&dev->kobj, groups);
1245 devres_free(devres);
1249 devres->groups = groups;
1250 devres_add(dev, devres);
1253 EXPORT_SYMBOL_GPL(devm_device_add_groups);
1256 * devm_device_remove_groups - remove a list of managed groups
1258 * @dev: The device for the groups to be removed from
1259 * @groups: NULL terminated list of groups to be removed
1261 * If groups is not NULL, remove the specified groups from the device.
1263 void devm_device_remove_groups(struct device *dev,
1264 const struct attribute_group **groups)
1266 WARN_ON(devres_release(dev, devm_attr_groups_remove,
1267 devm_attr_group_match,
1268 /* cast away const */ (void *)groups));
1270 EXPORT_SYMBOL_GPL(devm_device_remove_groups);
1272 static int device_add_attrs(struct device *dev)
1274 struct class *class = dev->class;
1275 const struct device_type *type = dev->type;
1279 error = device_add_groups(dev, class->dev_groups);
1285 error = device_add_groups(dev, type->groups);
1287 goto err_remove_class_groups;
1290 error = device_add_groups(dev, dev->groups);
1292 goto err_remove_type_groups;
1294 if (device_supports_offline(dev) && !dev->offline_disabled) {
1295 error = device_create_file(dev, &dev_attr_online);
1297 goto err_remove_dev_groups;
1302 err_remove_dev_groups:
1303 device_remove_groups(dev, dev->groups);
1304 err_remove_type_groups:
1306 device_remove_groups(dev, type->groups);
1307 err_remove_class_groups:
1309 device_remove_groups(dev, class->dev_groups);
1314 static void device_remove_attrs(struct device *dev)
1316 struct class *class = dev->class;
1317 const struct device_type *type = dev->type;
1319 device_remove_file(dev, &dev_attr_online);
1320 device_remove_groups(dev, dev->groups);
1323 device_remove_groups(dev, type->groups);
1326 device_remove_groups(dev, class->dev_groups);
1329 static ssize_t dev_show(struct device *dev, struct device_attribute *attr,
1332 return print_dev_t(buf, dev->devt);
1334 static DEVICE_ATTR_RO(dev);
1337 struct kset *devices_kset;
1340 * devices_kset_move_before - Move device in the devices_kset's list.
1341 * @deva: Device to move.
1342 * @devb: Device @deva should come before.
1344 static void devices_kset_move_before(struct device *deva, struct device *devb)
1348 pr_debug("devices_kset: Moving %s before %s\n",
1349 dev_name(deva), dev_name(devb));
1350 spin_lock(&devices_kset->list_lock);
1351 list_move_tail(&deva->kobj.entry, &devb->kobj.entry);
1352 spin_unlock(&devices_kset->list_lock);
1356 * devices_kset_move_after - Move device in the devices_kset's list.
1357 * @deva: Device to move
1358 * @devb: Device @deva should come after.
1360 static void devices_kset_move_after(struct device *deva, struct device *devb)
1364 pr_debug("devices_kset: Moving %s after %s\n",
1365 dev_name(deva), dev_name(devb));
1366 spin_lock(&devices_kset->list_lock);
1367 list_move(&deva->kobj.entry, &devb->kobj.entry);
1368 spin_unlock(&devices_kset->list_lock);
1372 * devices_kset_move_last - move the device to the end of devices_kset's list.
1373 * @dev: device to move
1375 void devices_kset_move_last(struct device *dev)
1379 pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev));
1380 spin_lock(&devices_kset->list_lock);
1381 list_move_tail(&dev->kobj.entry, &devices_kset->list);
1382 spin_unlock(&devices_kset->list_lock);
1386 * device_create_file - create sysfs attribute file for device.
1388 * @attr: device attribute descriptor.
1390 int device_create_file(struct device *dev,
1391 const struct device_attribute *attr)
1396 WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
1397 "Attribute %s: write permission without 'store'\n",
1399 WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
1400 "Attribute %s: read permission without 'show'\n",
1402 error = sysfs_create_file(&dev->kobj, &attr->attr);
1407 EXPORT_SYMBOL_GPL(device_create_file);
1410 * device_remove_file - remove sysfs attribute file.
1412 * @attr: device attribute descriptor.
1414 void device_remove_file(struct device *dev,
1415 const struct device_attribute *attr)
1418 sysfs_remove_file(&dev->kobj, &attr->attr);
1420 EXPORT_SYMBOL_GPL(device_remove_file);
1423 * device_remove_file_self - remove sysfs attribute file from its own method.
1425 * @attr: device attribute descriptor.
1427 * See kernfs_remove_self() for details.
1429 bool device_remove_file_self(struct device *dev,
1430 const struct device_attribute *attr)
1433 return sysfs_remove_file_self(&dev->kobj, &attr->attr);
1437 EXPORT_SYMBOL_GPL(device_remove_file_self);
1440 * device_create_bin_file - create sysfs binary attribute file for device.
1442 * @attr: device binary attribute descriptor.
1444 int device_create_bin_file(struct device *dev,
1445 const struct bin_attribute *attr)
1447 int error = -EINVAL;
1449 error = sysfs_create_bin_file(&dev->kobj, attr);
1452 EXPORT_SYMBOL_GPL(device_create_bin_file);
1455 * device_remove_bin_file - remove sysfs binary attribute file
1457 * @attr: device binary attribute descriptor.
1459 void device_remove_bin_file(struct device *dev,
1460 const struct bin_attribute *attr)
1463 sysfs_remove_bin_file(&dev->kobj, attr);
1465 EXPORT_SYMBOL_GPL(device_remove_bin_file);
1467 static void klist_children_get(struct klist_node *n)
1469 struct device_private *p = to_device_private_parent(n);
1470 struct device *dev = p->device;
1475 static void klist_children_put(struct klist_node *n)
1477 struct device_private *p = to_device_private_parent(n);
1478 struct device *dev = p->device;
1484 * device_initialize - init device structure.
1487 * This prepares the device for use by other layers by initializing
1489 * It is the first half of device_register(), if called by
1490 * that function, though it can also be called separately, so one
1491 * may use @dev's fields. In particular, get_device()/put_device()
1492 * may be used for reference counting of @dev after calling this
1495 * All fields in @dev must be initialized by the caller to 0, except
1496 * for those explicitly set to some other value. The simplest
1497 * approach is to use kzalloc() to allocate the structure containing
1500 * NOTE: Use put_device() to give up your reference instead of freeing
1501 * @dev directly once you have called this function.
1503 void device_initialize(struct device *dev)
1505 dev->kobj.kset = devices_kset;
1506 kobject_init(&dev->kobj, &device_ktype);
1507 INIT_LIST_HEAD(&dev->dma_pools);
1508 mutex_init(&dev->mutex);
1509 lockdep_set_novalidate_class(&dev->mutex);
1510 spin_lock_init(&dev->devres_lock);
1511 INIT_LIST_HEAD(&dev->devres_head);
1512 device_pm_init(dev);
1513 set_dev_node(dev, -1);
1514 #ifdef CONFIG_GENERIC_MSI_IRQ
1515 INIT_LIST_HEAD(&dev->msi_list);
1517 INIT_LIST_HEAD(&dev->links.consumers);
1518 INIT_LIST_HEAD(&dev->links.suppliers);
1519 dev->links.status = DL_DEV_NO_DRIVER;
1521 EXPORT_SYMBOL_GPL(device_initialize);
1523 struct kobject *virtual_device_parent(struct device *dev)
1525 static struct kobject *virtual_dir = NULL;
1528 virtual_dir = kobject_create_and_add("virtual",
1529 &devices_kset->kobj);
1535 struct kobject kobj;
1536 struct class *class;
1539 #define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
1541 static void class_dir_release(struct kobject *kobj)
1543 struct class_dir *dir = to_class_dir(kobj);
1548 struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
1550 struct class_dir *dir = to_class_dir(kobj);
1551 return dir->class->ns_type;
1554 static struct kobj_type class_dir_ktype = {
1555 .release = class_dir_release,
1556 .sysfs_ops = &kobj_sysfs_ops,
1557 .child_ns_type = class_dir_child_ns_type
1560 static struct kobject *
1561 class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
1563 struct class_dir *dir;
1566 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1568 return ERR_PTR(-ENOMEM);
1571 kobject_init(&dir->kobj, &class_dir_ktype);
1573 dir->kobj.kset = &class->p->glue_dirs;
1575 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
1577 kobject_put(&dir->kobj);
1578 return ERR_PTR(retval);
1583 static DEFINE_MUTEX(gdp_mutex);
1585 static struct kobject *get_device_parent(struct device *dev,
1586 struct device *parent)
1589 struct kobject *kobj = NULL;
1590 struct kobject *parent_kobj;
1594 /* block disks show up in /sys/block */
1595 if (sysfs_deprecated && dev->class == &block_class) {
1596 if (parent && parent->class == &block_class)
1597 return &parent->kobj;
1598 return &block_class.p->subsys.kobj;
1603 * If we have no parent, we live in "virtual".
1604 * Class-devices with a non class-device as parent, live
1605 * in a "glue" directory to prevent namespace collisions.
1608 parent_kobj = virtual_device_parent(dev);
1609 else if (parent->class && !dev->class->ns_type)
1610 return &parent->kobj;
1612 parent_kobj = &parent->kobj;
1614 mutex_lock(&gdp_mutex);
1616 /* find our class-directory at the parent and reference it */
1617 spin_lock(&dev->class->p->glue_dirs.list_lock);
1618 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
1619 if (k->parent == parent_kobj) {
1620 kobj = kobject_get(k);
1623 spin_unlock(&dev->class->p->glue_dirs.list_lock);
1625 mutex_unlock(&gdp_mutex);
1629 /* or create a new class-directory at the parent device */
1630 k = class_dir_create_and_add(dev->class, parent_kobj);
1631 /* do not emit an uevent for this simple "glue" directory */
1632 mutex_unlock(&gdp_mutex);
1636 /* subsystems can specify a default root directory for their devices */
1637 if (!parent && dev->bus && dev->bus->dev_root)
1638 return &dev->bus->dev_root->kobj;
1641 return &parent->kobj;
1645 static inline bool live_in_glue_dir(struct kobject *kobj,
1648 if (!kobj || !dev->class ||
1649 kobj->kset != &dev->class->p->glue_dirs)
1654 static inline struct kobject *get_glue_dir(struct device *dev)
1656 return dev->kobj.parent;
1660 * make sure cleaning up dir as the last step, we need to make
1661 * sure .release handler of kobject is run with holding the
1664 static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
1666 /* see if we live in a "glue" directory */
1667 if (!live_in_glue_dir(glue_dir, dev))
1670 mutex_lock(&gdp_mutex);
1671 if (!kobject_has_children(glue_dir))
1672 kobject_del(glue_dir);
1673 kobject_put(glue_dir);
1674 mutex_unlock(&gdp_mutex);
1677 static int device_add_class_symlinks(struct device *dev)
1679 struct device_node *of_node = dev_of_node(dev);
1683 error = sysfs_create_link(&dev->kobj, of_node_kobj(of_node), "of_node");
1685 dev_warn(dev, "Error %d creating of_node link\n",error);
1686 /* An error here doesn't warrant bringing down the device */
1692 error = sysfs_create_link(&dev->kobj,
1693 &dev->class->p->subsys.kobj,
1698 if (dev->parent && device_is_not_partition(dev)) {
1699 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
1706 /* /sys/block has directories and does not need symlinks */
1707 if (sysfs_deprecated && dev->class == &block_class)
1711 /* link in the class directory pointing to the device */
1712 error = sysfs_create_link(&dev->class->p->subsys.kobj,
1713 &dev->kobj, dev_name(dev));
1720 sysfs_remove_link(&dev->kobj, "device");
1723 sysfs_remove_link(&dev->kobj, "subsystem");
1725 sysfs_remove_link(&dev->kobj, "of_node");
1729 static void device_remove_class_symlinks(struct device *dev)
1731 if (dev_of_node(dev))
1732 sysfs_remove_link(&dev->kobj, "of_node");
1737 if (dev->parent && device_is_not_partition(dev))
1738 sysfs_remove_link(&dev->kobj, "device");
1739 sysfs_remove_link(&dev->kobj, "subsystem");
1741 if (sysfs_deprecated && dev->class == &block_class)
1744 sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
1748 * dev_set_name - set a device name
1750 * @fmt: format string for the device's name
1752 int dev_set_name(struct device *dev, const char *fmt, ...)
1757 va_start(vargs, fmt);
1758 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
1762 EXPORT_SYMBOL_GPL(dev_set_name);
1765 * device_to_dev_kobj - select a /sys/dev/ directory for the device
1768 * By default we select char/ for new entries. Setting class->dev_obj
1769 * to NULL prevents an entry from being created. class->dev_kobj must
1770 * be set (or cleared) before any devices are registered to the class
1771 * otherwise device_create_sys_dev_entry() and
1772 * device_remove_sys_dev_entry() will disagree about the presence of
1775 static struct kobject *device_to_dev_kobj(struct device *dev)
1777 struct kobject *kobj;
1780 kobj = dev->class->dev_kobj;
1782 kobj = sysfs_dev_char_kobj;
1787 static int device_create_sys_dev_entry(struct device *dev)
1789 struct kobject *kobj = device_to_dev_kobj(dev);
1794 format_dev_t(devt_str, dev->devt);
1795 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
1801 static void device_remove_sys_dev_entry(struct device *dev)
1803 struct kobject *kobj = device_to_dev_kobj(dev);
1807 format_dev_t(devt_str, dev->devt);
1808 sysfs_remove_link(kobj, devt_str);
1812 static int device_private_init(struct device *dev)
1814 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
1817 dev->p->device = dev;
1818 klist_init(&dev->p->klist_children, klist_children_get,
1819 klist_children_put);
1820 INIT_LIST_HEAD(&dev->p->deferred_probe);
1825 * device_add - add device to device hierarchy.
1828 * This is part 2 of device_register(), though may be called
1829 * separately _iff_ device_initialize() has been called separately.
1831 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
1832 * to the global and sibling lists for the device, then
1833 * adds it to the other relevant subsystems of the driver model.
1835 * Do not call this routine or device_register() more than once for
1836 * any device structure. The driver model core is not designed to work
1837 * with devices that get unregistered and then spring back to life.
1838 * (Among other things, it's very hard to guarantee that all references
1839 * to the previous incarnation of @dev have been dropped.) Allocate
1840 * and register a fresh new struct device instead.
1842 * NOTE: _Never_ directly free @dev after calling this function, even
1843 * if it returned an error! Always use put_device() to give up your
1844 * reference instead.
1846 int device_add(struct device *dev)
1848 struct device *parent;
1849 struct kobject *kobj;
1850 struct class_interface *class_intf;
1851 int error = -EINVAL;
1852 struct kobject *glue_dir = NULL;
1854 dev = get_device(dev);
1859 error = device_private_init(dev);
1865 * for statically allocated devices, which should all be converted
1866 * some day, we need to initialize the name. We prevent reading back
1867 * the name, and force the use of dev_name()
1869 if (dev->init_name) {
1870 dev_set_name(dev, "%s", dev->init_name);
1871 dev->init_name = NULL;
1874 /* subsystems can specify simple device enumeration */
1875 if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
1876 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
1878 if (!dev_name(dev)) {
1883 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
1885 parent = get_device(dev->parent);
1886 kobj = get_device_parent(dev, parent);
1888 error = PTR_ERR(kobj);
1892 dev->kobj.parent = kobj;
1894 /* use parent numa_node */
1895 if (parent && (dev_to_node(dev) == NUMA_NO_NODE))
1896 set_dev_node(dev, dev_to_node(parent));
1898 /* first, register with generic layer. */
1899 /* we require the name to be set before, and pass NULL */
1900 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
1902 glue_dir = get_glue_dir(dev);
1906 /* notify platform of device entry */
1907 error = device_platform_notify(dev, KOBJ_ADD);
1909 goto platform_error;
1911 error = device_create_file(dev, &dev_attr_uevent);
1915 error = device_add_class_symlinks(dev);
1918 error = device_add_attrs(dev);
1921 error = bus_add_device(dev);
1924 error = dpm_sysfs_add(dev);
1929 if (MAJOR(dev->devt)) {
1930 error = device_create_file(dev, &dev_attr_dev);
1934 error = device_create_sys_dev_entry(dev);
1938 devtmpfs_create_node(dev);
1941 /* Notify clients of device addition. This call must come
1942 * after dpm_sysfs_add() and before kobject_uevent().
1945 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1946 BUS_NOTIFY_ADD_DEVICE, dev);
1948 kobject_uevent(&dev->kobj, KOBJ_ADD);
1949 bus_probe_device(dev);
1951 klist_add_tail(&dev->p->knode_parent,
1952 &parent->p->klist_children);
1955 mutex_lock(&dev->class->p->mutex);
1956 /* tie the class to the device */
1957 klist_add_tail(&dev->knode_class,
1958 &dev->class->p->klist_devices);
1960 /* notify any interfaces that the device is here */
1961 list_for_each_entry(class_intf,
1962 &dev->class->p->interfaces, node)
1963 if (class_intf->add_dev)
1964 class_intf->add_dev(dev, class_intf);
1965 mutex_unlock(&dev->class->p->mutex);
1971 if (MAJOR(dev->devt))
1972 device_remove_file(dev, &dev_attr_dev);
1974 device_pm_remove(dev);
1975 dpm_sysfs_remove(dev);
1977 bus_remove_device(dev);
1979 device_remove_attrs(dev);
1981 device_remove_class_symlinks(dev);
1983 device_remove_file(dev, &dev_attr_uevent);
1985 device_platform_notify(dev, KOBJ_REMOVE);
1987 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
1988 glue_dir = get_glue_dir(dev);
1989 kobject_del(&dev->kobj);
1991 cleanup_glue_dir(dev, glue_dir);
1999 EXPORT_SYMBOL_GPL(device_add);
2002 * device_register - register a device with the system.
2003 * @dev: pointer to the device structure
2005 * This happens in two clean steps - initialize the device
2006 * and add it to the system. The two steps can be called
2007 * separately, but this is the easiest and most common.
2008 * I.e. you should only call the two helpers separately if
2009 * have a clearly defined need to use and refcount the device
2010 * before it is added to the hierarchy.
2012 * For more information, see the kerneldoc for device_initialize()
2015 * NOTE: _Never_ directly free @dev after calling this function, even
2016 * if it returned an error! Always use put_device() to give up the
2017 * reference initialized in this function instead.
2019 int device_register(struct device *dev)
2021 device_initialize(dev);
2022 return device_add(dev);
2024 EXPORT_SYMBOL_GPL(device_register);
2027 * get_device - increment reference count for device.
2030 * This simply forwards the call to kobject_get(), though
2031 * we do take care to provide for the case that we get a NULL
2032 * pointer passed in.
2034 struct device *get_device(struct device *dev)
2036 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
2038 EXPORT_SYMBOL_GPL(get_device);
2041 * put_device - decrement reference count.
2042 * @dev: device in question.
2044 void put_device(struct device *dev)
2046 /* might_sleep(); */
2048 kobject_put(&dev->kobj);
2050 EXPORT_SYMBOL_GPL(put_device);
2053 * device_del - delete device from system.
2056 * This is the first part of the device unregistration
2057 * sequence. This removes the device from the lists we control
2058 * from here, has it removed from the other driver model
2059 * subsystems it was added to in device_add(), and removes it
2060 * from the kobject hierarchy.
2062 * NOTE: this should be called manually _iff_ device_add() was
2063 * also called manually.
2065 void device_del(struct device *dev)
2067 struct device *parent = dev->parent;
2068 struct kobject *glue_dir = NULL;
2069 struct class_interface *class_intf;
2071 /* Notify clients of device removal. This call must come
2072 * before dpm_sysfs_remove().
2075 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2076 BUS_NOTIFY_DEL_DEVICE, dev);
2078 dpm_sysfs_remove(dev);
2080 klist_del(&dev->p->knode_parent);
2081 if (MAJOR(dev->devt)) {
2082 devtmpfs_delete_node(dev);
2083 device_remove_sys_dev_entry(dev);
2084 device_remove_file(dev, &dev_attr_dev);
2087 device_remove_class_symlinks(dev);
2089 mutex_lock(&dev->class->p->mutex);
2090 /* notify any interfaces that the device is now gone */
2091 list_for_each_entry(class_intf,
2092 &dev->class->p->interfaces, node)
2093 if (class_intf->remove_dev)
2094 class_intf->remove_dev(dev, class_intf);
2095 /* remove the device from the class list */
2096 klist_del(&dev->knode_class);
2097 mutex_unlock(&dev->class->p->mutex);
2099 device_remove_file(dev, &dev_attr_uevent);
2100 device_remove_attrs(dev);
2101 bus_remove_device(dev);
2102 device_pm_remove(dev);
2103 driver_deferred_probe_del(dev);
2104 device_platform_notify(dev, KOBJ_REMOVE);
2105 device_remove_properties(dev);
2106 device_links_purge(dev);
2109 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2110 BUS_NOTIFY_REMOVED_DEVICE, dev);
2111 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
2112 glue_dir = get_glue_dir(dev);
2113 kobject_del(&dev->kobj);
2114 cleanup_glue_dir(dev, glue_dir);
2117 EXPORT_SYMBOL_GPL(device_del);
2120 * device_unregister - unregister device from system.
2121 * @dev: device going away.
2123 * We do this in two parts, like we do device_register(). First,
2124 * we remove it from all the subsystems with device_del(), then
2125 * we decrement the reference count via put_device(). If that
2126 * is the final reference count, the device will be cleaned up
2127 * via device_release() above. Otherwise, the structure will
2128 * stick around until the final reference to the device is dropped.
2130 void device_unregister(struct device *dev)
2132 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
2136 EXPORT_SYMBOL_GPL(device_unregister);
2138 static struct device *prev_device(struct klist_iter *i)
2140 struct klist_node *n = klist_prev(i);
2141 struct device *dev = NULL;
2142 struct device_private *p;
2145 p = to_device_private_parent(n);
2151 static struct device *next_device(struct klist_iter *i)
2153 struct klist_node *n = klist_next(i);
2154 struct device *dev = NULL;
2155 struct device_private *p;
2158 p = to_device_private_parent(n);
2165 * device_get_devnode - path of device node file
2167 * @mode: returned file access mode
2168 * @uid: returned file owner
2169 * @gid: returned file group
2170 * @tmp: possibly allocated string
2172 * Return the relative path of a possible device node.
2173 * Non-default names may need to allocate a memory to compose
2174 * a name. This memory is returned in tmp and needs to be
2175 * freed by the caller.
2177 const char *device_get_devnode(struct device *dev,
2178 umode_t *mode, kuid_t *uid, kgid_t *gid,
2185 /* the device type may provide a specific name */
2186 if (dev->type && dev->type->devnode)
2187 *tmp = dev->type->devnode(dev, mode, uid, gid);
2191 /* the class may provide a specific name */
2192 if (dev->class && dev->class->devnode)
2193 *tmp = dev->class->devnode(dev, mode);
2197 /* return name without allocation, tmp == NULL */
2198 if (strchr(dev_name(dev), '!') == NULL)
2199 return dev_name(dev);
2201 /* replace '!' in the name with '/' */
2202 s = kstrdup(dev_name(dev), GFP_KERNEL);
2205 strreplace(s, '!', '/');
2210 * device_for_each_child - device child iterator.
2211 * @parent: parent struct device.
2212 * @fn: function to be called for each device.
2213 * @data: data for the callback.
2215 * Iterate over @parent's child devices, and call @fn for each,
2218 * We check the return of @fn each time. If it returns anything
2219 * other than 0, we break out and return that value.
2221 int device_for_each_child(struct device *parent, void *data,
2222 int (*fn)(struct device *dev, void *data))
2224 struct klist_iter i;
2225 struct device *child;
2231 klist_iter_init(&parent->p->klist_children, &i);
2232 while (!error && (child = next_device(&i)))
2233 error = fn(child, data);
2234 klist_iter_exit(&i);
2237 EXPORT_SYMBOL_GPL(device_for_each_child);
2240 * device_for_each_child_reverse - device child iterator in reversed order.
2241 * @parent: parent struct device.
2242 * @fn: function to be called for each device.
2243 * @data: data for the callback.
2245 * Iterate over @parent's child devices, and call @fn for each,
2248 * We check the return of @fn each time. If it returns anything
2249 * other than 0, we break out and return that value.
2251 int device_for_each_child_reverse(struct device *parent, void *data,
2252 int (*fn)(struct device *dev, void *data))
2254 struct klist_iter i;
2255 struct device *child;
2261 klist_iter_init(&parent->p->klist_children, &i);
2262 while ((child = prev_device(&i)) && !error)
2263 error = fn(child, data);
2264 klist_iter_exit(&i);
2267 EXPORT_SYMBOL_GPL(device_for_each_child_reverse);
2270 * device_find_child - device iterator for locating a particular device.
2271 * @parent: parent struct device
2272 * @match: Callback function to check device
2273 * @data: Data to pass to match function
2275 * This is similar to the device_for_each_child() function above, but it
2276 * returns a reference to a device that is 'found' for later use, as
2277 * determined by the @match callback.
2279 * The callback should return 0 if the device doesn't match and non-zero
2280 * if it does. If the callback returns non-zero and a reference to the
2281 * current device can be obtained, this function will return to the caller
2282 * and not iterate over any more devices.
2284 * NOTE: you will need to drop the reference with put_device() after use.
2286 struct device *device_find_child(struct device *parent, void *data,
2287 int (*match)(struct device *dev, void *data))
2289 struct klist_iter i;
2290 struct device *child;
2295 klist_iter_init(&parent->p->klist_children, &i);
2296 while ((child = next_device(&i)))
2297 if (match(child, data) && get_device(child))
2299 klist_iter_exit(&i);
2302 EXPORT_SYMBOL_GPL(device_find_child);
2304 int __init devices_init(void)
2306 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
2309 dev_kobj = kobject_create_and_add("dev", NULL);
2312 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
2313 if (!sysfs_dev_block_kobj)
2314 goto block_kobj_err;
2315 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
2316 if (!sysfs_dev_char_kobj)
2322 kobject_put(sysfs_dev_block_kobj);
2324 kobject_put(dev_kobj);
2326 kset_unregister(devices_kset);
2330 static int device_check_offline(struct device *dev, void *not_used)
2334 ret = device_for_each_child(dev, NULL, device_check_offline);
2338 return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
2342 * device_offline - Prepare the device for hot-removal.
2343 * @dev: Device to be put offline.
2345 * Execute the device bus type's .offline() callback, if present, to prepare
2346 * the device for a subsequent hot-removal. If that succeeds, the device must
2347 * not be used until either it is removed or its bus type's .online() callback
2350 * Call under device_hotplug_lock.
2352 int device_offline(struct device *dev)
2356 if (dev->offline_disabled)
2359 ret = device_for_each_child(dev, NULL, device_check_offline);
2364 if (device_supports_offline(dev)) {
2368 ret = dev->bus->offline(dev);
2370 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
2371 dev->offline = true;
2381 * device_online - Put the device back online after successful device_offline().
2382 * @dev: Device to be put back online.
2384 * If device_offline() has been successfully executed for @dev, but the device
2385 * has not been removed subsequently, execute its bus type's .online() callback
2386 * to indicate that the device can be used again.
2388 * Call under device_hotplug_lock.
2390 int device_online(struct device *dev)
2395 if (device_supports_offline(dev)) {
2397 ret = dev->bus->online(dev);
2399 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
2400 dev->offline = false;
2411 struct root_device {
2413 struct module *owner;
2416 static inline struct root_device *to_root_device(struct device *d)
2418 return container_of(d, struct root_device, dev);
2421 static void root_device_release(struct device *dev)
2423 kfree(to_root_device(dev));
2427 * __root_device_register - allocate and register a root device
2428 * @name: root device name
2429 * @owner: owner module of the root device, usually THIS_MODULE
2431 * This function allocates a root device and registers it
2432 * using device_register(). In order to free the returned
2433 * device, use root_device_unregister().
2435 * Root devices are dummy devices which allow other devices
2436 * to be grouped under /sys/devices. Use this function to
2437 * allocate a root device and then use it as the parent of
2438 * any device which should appear under /sys/devices/{name}
2440 * The /sys/devices/{name} directory will also contain a
2441 * 'module' symlink which points to the @owner directory
2444 * Returns &struct device pointer on success, or ERR_PTR() on error.
2446 * Note: You probably want to use root_device_register().
2448 struct device *__root_device_register(const char *name, struct module *owner)
2450 struct root_device *root;
2453 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
2455 return ERR_PTR(err);
2457 err = dev_set_name(&root->dev, "%s", name);
2460 return ERR_PTR(err);
2463 root->dev.release = root_device_release;
2465 err = device_register(&root->dev);
2467 put_device(&root->dev);
2468 return ERR_PTR(err);
2471 #ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
2473 struct module_kobject *mk = &owner->mkobj;
2475 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
2477 device_unregister(&root->dev);
2478 return ERR_PTR(err);
2480 root->owner = owner;
2486 EXPORT_SYMBOL_GPL(__root_device_register);
2489 * root_device_unregister - unregister and free a root device
2490 * @dev: device going away
2492 * This function unregisters and cleans up a device that was created by
2493 * root_device_register().
2495 void root_device_unregister(struct device *dev)
2497 struct root_device *root = to_root_device(dev);
2500 sysfs_remove_link(&root->dev.kobj, "module");
2502 device_unregister(dev);
2504 EXPORT_SYMBOL_GPL(root_device_unregister);
2507 static void device_create_release(struct device *dev)
2509 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
2513 static __printf(6, 0) struct device *
2514 device_create_groups_vargs(struct class *class, struct device *parent,
2515 dev_t devt, void *drvdata,
2516 const struct attribute_group **groups,
2517 const char *fmt, va_list args)
2519 struct device *dev = NULL;
2520 int retval = -ENODEV;
2522 if (class == NULL || IS_ERR(class))
2525 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2531 device_initialize(dev);
2534 dev->parent = parent;
2535 dev->groups = groups;
2536 dev->release = device_create_release;
2537 dev_set_drvdata(dev, drvdata);
2539 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
2543 retval = device_add(dev);
2551 return ERR_PTR(retval);
2555 * device_create_vargs - creates a device and registers it with sysfs
2556 * @class: pointer to the struct class that this device should be registered to
2557 * @parent: pointer to the parent struct device of this new device, if any
2558 * @devt: the dev_t for the char device to be added
2559 * @drvdata: the data to be added to the device for callbacks
2560 * @fmt: string for the device's name
2561 * @args: va_list for the device's name
2563 * This function can be used by char device classes. A struct device
2564 * will be created in sysfs, registered to the specified class.
2566 * A "dev" file will be created, showing the dev_t for the device, if
2567 * the dev_t is not 0,0.
2568 * If a pointer to a parent struct device is passed in, the newly created
2569 * struct device will be a child of that device in sysfs.
2570 * The pointer to the struct device will be returned from the call.
2571 * Any further sysfs files that might be required can be created using this
2574 * Returns &struct device pointer on success, or ERR_PTR() on error.
2576 * Note: the struct class passed to this function must have previously
2577 * been created with a call to class_create().
2579 struct device *device_create_vargs(struct class *class, struct device *parent,
2580 dev_t devt, void *drvdata, const char *fmt,
2583 return device_create_groups_vargs(class, parent, devt, drvdata, NULL,
2586 EXPORT_SYMBOL_GPL(device_create_vargs);
2589 * device_create - creates a device and registers it with sysfs
2590 * @class: pointer to the struct class that this device should be registered to
2591 * @parent: pointer to the parent struct device of this new device, if any
2592 * @devt: the dev_t for the char device to be added
2593 * @drvdata: the data to be added to the device for callbacks
2594 * @fmt: string for the device's name
2596 * This function can be used by char device classes. A struct device
2597 * will be created in sysfs, registered to the specified class.
2599 * A "dev" file will be created, showing the dev_t for the device, if
2600 * the dev_t is not 0,0.
2601 * If a pointer to a parent struct device is passed in, the newly created
2602 * struct device will be a child of that device in sysfs.
2603 * The pointer to the struct device will be returned from the call.
2604 * Any further sysfs files that might be required can be created using this
2607 * Returns &struct device pointer on success, or ERR_PTR() on error.
2609 * Note: the struct class passed to this function must have previously
2610 * been created with a call to class_create().
2612 struct device *device_create(struct class *class, struct device *parent,
2613 dev_t devt, void *drvdata, const char *fmt, ...)
2618 va_start(vargs, fmt);
2619 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
2623 EXPORT_SYMBOL_GPL(device_create);
2626 * device_create_with_groups - creates a device and registers it with sysfs
2627 * @class: pointer to the struct class that this device should be registered to
2628 * @parent: pointer to the parent struct device of this new device, if any
2629 * @devt: the dev_t for the char device to be added
2630 * @drvdata: the data to be added to the device for callbacks
2631 * @groups: NULL-terminated list of attribute groups to be created
2632 * @fmt: string for the device's name
2634 * This function can be used by char device classes. A struct device
2635 * will be created in sysfs, registered to the specified class.
2636 * Additional attributes specified in the groups parameter will also
2637 * be created automatically.
2639 * A "dev" file will be created, showing the dev_t for the device, if
2640 * the dev_t is not 0,0.
2641 * If a pointer to a parent struct device is passed in, the newly created
2642 * struct device will be a child of that device in sysfs.
2643 * The pointer to the struct device will be returned from the call.
2644 * Any further sysfs files that might be required can be created using this
2647 * Returns &struct device pointer on success, or ERR_PTR() on error.
2649 * Note: the struct class passed to this function must have previously
2650 * been created with a call to class_create().
2652 struct device *device_create_with_groups(struct class *class,
2653 struct device *parent, dev_t devt,
2655 const struct attribute_group **groups,
2656 const char *fmt, ...)
2661 va_start(vargs, fmt);
2662 dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
2667 EXPORT_SYMBOL_GPL(device_create_with_groups);
2669 static int __match_devt(struct device *dev, const void *data)
2671 const dev_t *devt = data;
2673 return dev->devt == *devt;
2677 * device_destroy - removes a device that was created with device_create()
2678 * @class: pointer to the struct class that this device was registered with
2679 * @devt: the dev_t of the device that was previously registered
2681 * This call unregisters and cleans up a device that was created with a
2682 * call to device_create().
2684 void device_destroy(struct class *class, dev_t devt)
2688 dev = class_find_device(class, NULL, &devt, __match_devt);
2691 device_unregister(dev);
2694 EXPORT_SYMBOL_GPL(device_destroy);
2697 * device_rename - renames a device
2698 * @dev: the pointer to the struct device to be renamed
2699 * @new_name: the new name of the device
2701 * It is the responsibility of the caller to provide mutual
2702 * exclusion between two different calls of device_rename
2703 * on the same device to ensure that new_name is valid and
2704 * won't conflict with other devices.
2706 * Note: Don't call this function. Currently, the networking layer calls this
2707 * function, but that will change. The following text from Kay Sievers offers
2710 * Renaming devices is racy at many levels, symlinks and other stuff are not
2711 * replaced atomically, and you get a "move" uevent, but it's not easy to
2712 * connect the event to the old and new device. Device nodes are not renamed at
2713 * all, there isn't even support for that in the kernel now.
2715 * In the meantime, during renaming, your target name might be taken by another
2716 * driver, creating conflicts. Or the old name is taken directly after you
2717 * renamed it -- then you get events for the same DEVPATH, before you even see
2718 * the "move" event. It's just a mess, and nothing new should ever rely on
2719 * kernel device renaming. Besides that, it's not even implemented now for
2720 * other things than (driver-core wise very simple) network devices.
2722 * We are currently about to change network renaming in udev to completely
2723 * disallow renaming of devices in the same namespace as the kernel uses,
2724 * because we can't solve the problems properly, that arise with swapping names
2725 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
2726 * be allowed to some other name than eth[0-9]*, for the aforementioned
2729 * Make up a "real" name in the driver before you register anything, or add
2730 * some other attributes for userspace to find the device, or use udev to add
2731 * symlinks -- but never rename kernel devices later, it's a complete mess. We
2732 * don't even want to get into that and try to implement the missing pieces in
2733 * the core. We really have other pieces to fix in the driver core mess. :)
2735 int device_rename(struct device *dev, const char *new_name)
2737 struct kobject *kobj = &dev->kobj;
2738 char *old_device_name = NULL;
2741 dev = get_device(dev);
2745 dev_dbg(dev, "renaming to %s\n", new_name);
2747 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
2748 if (!old_device_name) {
2754 error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj,
2755 kobj, old_device_name,
2756 new_name, kobject_namespace(kobj));
2761 error = kobject_rename(kobj, new_name);
2768 kfree(old_device_name);
2772 EXPORT_SYMBOL_GPL(device_rename);
2774 static int device_move_class_links(struct device *dev,
2775 struct device *old_parent,
2776 struct device *new_parent)
2781 sysfs_remove_link(&dev->kobj, "device");
2783 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
2789 * device_move - moves a device to a new parent
2790 * @dev: the pointer to the struct device to be moved
2791 * @new_parent: the new parent of the device (can be NULL)
2792 * @dpm_order: how to reorder the dpm_list
2794 int device_move(struct device *dev, struct device *new_parent,
2795 enum dpm_order dpm_order)
2798 struct device *old_parent;
2799 struct kobject *new_parent_kobj;
2801 dev = get_device(dev);
2806 new_parent = get_device(new_parent);
2807 new_parent_kobj = get_device_parent(dev, new_parent);
2808 if (IS_ERR(new_parent_kobj)) {
2809 error = PTR_ERR(new_parent_kobj);
2810 put_device(new_parent);
2814 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
2815 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
2816 error = kobject_move(&dev->kobj, new_parent_kobj);
2818 cleanup_glue_dir(dev, new_parent_kobj);
2819 put_device(new_parent);
2822 old_parent = dev->parent;
2823 dev->parent = new_parent;
2825 klist_remove(&dev->p->knode_parent);
2827 klist_add_tail(&dev->p->knode_parent,
2828 &new_parent->p->klist_children);
2829 set_dev_node(dev, dev_to_node(new_parent));
2833 error = device_move_class_links(dev, old_parent, new_parent);
2835 /* We ignore errors on cleanup since we're hosed anyway... */
2836 device_move_class_links(dev, new_parent, old_parent);
2837 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
2839 klist_remove(&dev->p->knode_parent);
2840 dev->parent = old_parent;
2842 klist_add_tail(&dev->p->knode_parent,
2843 &old_parent->p->klist_children);
2844 set_dev_node(dev, dev_to_node(old_parent));
2847 cleanup_glue_dir(dev, new_parent_kobj);
2848 put_device(new_parent);
2852 switch (dpm_order) {
2853 case DPM_ORDER_NONE:
2855 case DPM_ORDER_DEV_AFTER_PARENT:
2856 device_pm_move_after(dev, new_parent);
2857 devices_kset_move_after(dev, new_parent);
2859 case DPM_ORDER_PARENT_BEFORE_DEV:
2860 device_pm_move_before(new_parent, dev);
2861 devices_kset_move_before(new_parent, dev);
2863 case DPM_ORDER_DEV_LAST:
2864 device_pm_move_last(dev);
2865 devices_kset_move_last(dev);
2869 put_device(old_parent);
2875 EXPORT_SYMBOL_GPL(device_move);
2878 * device_shutdown - call ->shutdown() on each device to shutdown.
2880 void device_shutdown(void)
2882 struct device *dev, *parent;
2884 wait_for_device_probe();
2885 device_block_probing();
2887 spin_lock(&devices_kset->list_lock);
2889 * Walk the devices list backward, shutting down each in turn.
2890 * Beware that device unplug events may also start pulling
2891 * devices offline, even as the system is shutting down.
2893 while (!list_empty(&devices_kset->list)) {
2894 dev = list_entry(devices_kset->list.prev, struct device,
2898 * hold reference count of device's parent to
2899 * prevent it from being freed because parent's
2900 * lock is to be held
2902 parent = get_device(dev->parent);
2905 * Make sure the device is off the kset list, in the
2906 * event that dev->*->shutdown() doesn't remove it.
2908 list_del_init(&dev->kobj.entry);
2909 spin_unlock(&devices_kset->list_lock);
2911 /* hold lock to avoid race with probe/release */
2913 device_lock(parent);
2916 /* Don't allow any more runtime suspends */
2917 pm_runtime_get_noresume(dev);
2918 pm_runtime_barrier(dev);
2920 if (dev->class && dev->class->shutdown_pre) {
2922 dev_info(dev, "shutdown_pre\n");
2923 dev->class->shutdown_pre(dev);
2925 if (dev->bus && dev->bus->shutdown) {
2927 dev_info(dev, "shutdown\n");
2928 dev->bus->shutdown(dev);
2929 } else if (dev->driver && dev->driver->shutdown) {
2931 dev_info(dev, "shutdown\n");
2932 dev->driver->shutdown(dev);
2937 device_unlock(parent);
2942 spin_lock(&devices_kset->list_lock);
2944 spin_unlock(&devices_kset->list_lock);
2948 * Device logging functions
2951 #ifdef CONFIG_PRINTK
2953 create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
2959 subsys = dev->class->name;
2961 subsys = dev->bus->name;
2965 pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys);
2970 * Add device identifier DEVICE=:
2974 * +sound:card0 subsystem:devname
2976 if (MAJOR(dev->devt)) {
2979 if (strcmp(subsys, "block") == 0)
2984 pos += snprintf(hdr + pos, hdrlen - pos,
2986 c, MAJOR(dev->devt), MINOR(dev->devt));
2987 } else if (strcmp(subsys, "net") == 0) {
2988 struct net_device *net = to_net_dev(dev);
2991 pos += snprintf(hdr + pos, hdrlen - pos,
2992 "DEVICE=n%u", net->ifindex);
2995 pos += snprintf(hdr + pos, hdrlen - pos,
2996 "DEVICE=+%s:%s", subsys, dev_name(dev));
3005 dev_WARN(dev, "device/subsystem name too long");
3009 int dev_vprintk_emit(int level, const struct device *dev,
3010 const char *fmt, va_list args)
3015 hdrlen = create_syslog_header(dev, hdr, sizeof(hdr));
3017 return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args);
3019 EXPORT_SYMBOL(dev_vprintk_emit);
3021 int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
3026 va_start(args, fmt);
3028 r = dev_vprintk_emit(level, dev, fmt, args);
3034 EXPORT_SYMBOL(dev_printk_emit);
3036 static void __dev_printk(const char *level, const struct device *dev,
3037 struct va_format *vaf)
3040 dev_printk_emit(level[1] - '0', dev, "%s %s: %pV",
3041 dev_driver_string(dev), dev_name(dev), vaf);
3043 printk("%s(NULL device *): %pV", level, vaf);
3046 void dev_printk(const char *level, const struct device *dev,
3047 const char *fmt, ...)
3049 struct va_format vaf;
3052 va_start(args, fmt);
3057 __dev_printk(level, dev, &vaf);
3061 EXPORT_SYMBOL(dev_printk);
3063 #define define_dev_printk_level(func, kern_level) \
3064 void func(const struct device *dev, const char *fmt, ...) \
3066 struct va_format vaf; \
3069 va_start(args, fmt); \
3074 __dev_printk(kern_level, dev, &vaf); \
3078 EXPORT_SYMBOL(func);
3080 define_dev_printk_level(_dev_emerg, KERN_EMERG);
3081 define_dev_printk_level(_dev_alert, KERN_ALERT);
3082 define_dev_printk_level(_dev_crit, KERN_CRIT);
3083 define_dev_printk_level(_dev_err, KERN_ERR);
3084 define_dev_printk_level(_dev_warn, KERN_WARNING);
3085 define_dev_printk_level(_dev_notice, KERN_NOTICE);
3086 define_dev_printk_level(_dev_info, KERN_INFO);
3090 static inline bool fwnode_is_primary(struct fwnode_handle *fwnode)
3092 return fwnode && !IS_ERR(fwnode->secondary);
3096 * set_primary_fwnode - Change the primary firmware node of a given device.
3097 * @dev: Device to handle.
3098 * @fwnode: New primary firmware node of the device.
3100 * Set the device's firmware node pointer to @fwnode, but if a secondary
3101 * firmware node of the device is present, preserve it.
3103 void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
3106 struct fwnode_handle *fn = dev->fwnode;
3108 if (fwnode_is_primary(fn))
3112 WARN_ON(fwnode->secondary);
3113 fwnode->secondary = fn;
3115 dev->fwnode = fwnode;
3117 dev->fwnode = fwnode_is_primary(dev->fwnode) ?
3118 dev->fwnode->secondary : NULL;
3121 EXPORT_SYMBOL_GPL(set_primary_fwnode);
3124 * set_secondary_fwnode - Change the secondary firmware node of a given device.
3125 * @dev: Device to handle.
3126 * @fwnode: New secondary firmware node of the device.
3128 * If a primary firmware node of the device is present, set its secondary
3129 * pointer to @fwnode. Otherwise, set the device's firmware node pointer to
3132 void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
3135 fwnode->secondary = ERR_PTR(-ENODEV);
3137 if (fwnode_is_primary(dev->fwnode))
3138 dev->fwnode->secondary = fwnode;
3140 dev->fwnode = fwnode;
3144 * device_set_of_node_from_dev - reuse device-tree node of another device
3145 * @dev: device whose device-tree node is being set
3146 * @dev2: device whose device-tree node is being reused
3148 * Takes another reference to the new device-tree node after first dropping
3149 * any reference held to the old node.
3151 void device_set_of_node_from_dev(struct device *dev, const struct device *dev2)
3153 of_node_put(dev->of_node);
3154 dev->of_node = of_node_get(dev2->of_node);
3155 dev->of_node_reused = true;
3157 EXPORT_SYMBOL_GPL(device_set_of_node_from_dev);