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/device.h>
12 #include <linux/err.h>
13 #include <linux/fwnode.h>
14 #include <linux/init.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <linux/string.h>
18 #include <linux/kdev_t.h>
19 #include <linux/notifier.h>
21 #include <linux/of_device.h>
22 #include <linux/genhd.h>
23 #include <linux/mutex.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/netdevice.h>
26 #include <linux/sched/signal.h>
27 #include <linux/sysfs.h>
30 #include "power/power.h"
32 #ifdef CONFIG_SYSFS_DEPRECATED
33 #ifdef CONFIG_SYSFS_DEPRECATED_V2
34 long sysfs_deprecated = 1;
36 long sysfs_deprecated = 0;
38 static int __init sysfs_deprecated_setup(char *arg)
40 return kstrtol(arg, 10, &sysfs_deprecated);
42 early_param("sysfs.deprecated", sysfs_deprecated_setup);
45 /* Device links support. */
48 static DEFINE_MUTEX(device_links_lock);
49 DEFINE_STATIC_SRCU(device_links_srcu);
51 static inline void device_links_write_lock(void)
53 mutex_lock(&device_links_lock);
56 static inline void device_links_write_unlock(void)
58 mutex_unlock(&device_links_lock);
61 int device_links_read_lock(void)
63 return srcu_read_lock(&device_links_srcu);
66 void device_links_read_unlock(int idx)
68 srcu_read_unlock(&device_links_srcu, idx);
70 #else /* !CONFIG_SRCU */
71 static DECLARE_RWSEM(device_links_lock);
73 static inline void device_links_write_lock(void)
75 down_write(&device_links_lock);
78 static inline void device_links_write_unlock(void)
80 up_write(&device_links_lock);
83 int device_links_read_lock(void)
85 down_read(&device_links_lock);
89 void device_links_read_unlock(int not_used)
91 up_read(&device_links_lock);
93 #endif /* !CONFIG_SRCU */
96 * device_is_dependent - Check if one device depends on another one
97 * @dev: Device to check dependencies for.
98 * @target: Device to check against.
100 * Check if @target depends on @dev or any device dependent on it (its child or
101 * its consumer etc). Return 1 if that is the case or 0 otherwise.
103 static int device_is_dependent(struct device *dev, void *target)
105 struct device_link *link;
108 if (WARN_ON(dev == target))
111 ret = device_for_each_child(dev, target, device_is_dependent);
115 list_for_each_entry(link, &dev->links.consumers, s_node) {
116 if (WARN_ON(link->consumer == target))
119 ret = device_is_dependent(link->consumer, target);
126 static int device_reorder_to_tail(struct device *dev, void *not_used)
128 struct device_link *link;
131 * Devices that have not been registered yet will be put to the ends
132 * of the lists during the registration, so skip them here.
134 if (device_is_registered(dev))
135 devices_kset_move_last(dev);
137 if (device_pm_initialized(dev))
138 device_pm_move_last(dev);
140 device_for_each_child(dev, NULL, device_reorder_to_tail);
141 list_for_each_entry(link, &dev->links.consumers, s_node)
142 device_reorder_to_tail(link->consumer, NULL);
148 * device_pm_move_to_tail - Move set of devices to the end of device lists
149 * @dev: Device to move
151 * This is a device_reorder_to_tail() wrapper taking the requisite locks.
153 * It moves the @dev along with all of its children and all of its consumers
154 * to the ends of the device_kset and dpm_list, recursively.
156 void device_pm_move_to_tail(struct device *dev)
160 idx = device_links_read_lock();
162 device_reorder_to_tail(dev, NULL);
164 device_links_read_unlock(idx);
168 * device_link_add - Create a link between two devices.
169 * @consumer: Consumer end of the link.
170 * @supplier: Supplier end of the link.
171 * @flags: Link flags.
173 * The caller is responsible for the proper synchronization of the link creation
174 * with runtime PM. First, setting the DL_FLAG_PM_RUNTIME flag will cause the
175 * runtime PM framework to take the link into account. Second, if the
176 * DL_FLAG_RPM_ACTIVE flag is set in addition to it, the supplier devices will
177 * be forced into the active metastate and reference-counted upon the creation
178 * of the link. If DL_FLAG_PM_RUNTIME is not set, DL_FLAG_RPM_ACTIVE will be
181 * If the DL_FLAG_AUTOREMOVE_CONSUMER is set, the link will be removed
182 * automatically when the consumer device driver unbinds from it.
183 * The combination of both DL_FLAG_AUTOREMOVE_CONSUMER and DL_FLAG_STATELESS
184 * set is invalid and will cause NULL to be returned.
186 * A side effect of the link creation is re-ordering of dpm_list and the
187 * devices_kset list by moving the consumer device and all devices depending
188 * on it to the ends of these lists (that does not happen to devices that have
189 * not been registered when this function is called).
191 * The supplier device is required to be registered when this function is called
192 * and NULL will be returned if that is not the case. The consumer device need
193 * not be registered, however.
195 struct device_link *device_link_add(struct device *consumer,
196 struct device *supplier, u32 flags)
198 struct device_link *link;
200 if (!consumer || !supplier ||
201 ((flags & DL_FLAG_STATELESS) &&
202 (flags & DL_FLAG_AUTOREMOVE_CONSUMER)))
205 device_links_write_lock();
209 * If the supplier has not been fully registered yet or there is a
210 * reverse dependency between the consumer and the supplier already in
211 * the graph, return NULL.
213 if (!device_pm_initialized(supplier)
214 || device_is_dependent(consumer, supplier)) {
219 list_for_each_entry(link, &supplier->links.consumers, s_node)
220 if (link->consumer == consumer) {
221 kref_get(&link->kref);
225 link = kzalloc(sizeof(*link), GFP_KERNEL);
229 if (flags & DL_FLAG_PM_RUNTIME) {
230 if (flags & DL_FLAG_RPM_ACTIVE) {
231 if (pm_runtime_get_sync(supplier) < 0) {
232 pm_runtime_put_noidle(supplier);
237 link->rpm_active = true;
239 pm_runtime_new_link(consumer);
241 * If the link is being added by the consumer driver at probe
242 * time, balance the decrementation of the supplier's runtime PM
243 * usage counter after consumer probe in driver_probe_device().
245 if (consumer->links.status == DL_DEV_PROBING)
246 pm_runtime_get_noresume(supplier);
248 get_device(supplier);
249 link->supplier = supplier;
250 INIT_LIST_HEAD(&link->s_node);
251 get_device(consumer);
252 link->consumer = consumer;
253 INIT_LIST_HEAD(&link->c_node);
255 kref_init(&link->kref);
257 /* Determine the initial link state. */
258 if (flags & DL_FLAG_STATELESS) {
259 link->status = DL_STATE_NONE;
261 switch (supplier->links.status) {
262 case DL_DEV_DRIVER_BOUND:
263 switch (consumer->links.status) {
266 * Some callers expect the link creation during
267 * consumer driver probe to resume the supplier
268 * even without DL_FLAG_RPM_ACTIVE.
270 if (flags & DL_FLAG_PM_RUNTIME)
271 pm_runtime_resume(supplier);
273 link->status = DL_STATE_CONSUMER_PROBE;
275 case DL_DEV_DRIVER_BOUND:
276 link->status = DL_STATE_ACTIVE;
279 link->status = DL_STATE_AVAILABLE;
283 case DL_DEV_UNBINDING:
284 link->status = DL_STATE_SUPPLIER_UNBIND;
287 link->status = DL_STATE_DORMANT;
293 * Move the consumer and all of the devices depending on it to the end
294 * of dpm_list and the devices_kset list.
296 * It is necessary to hold dpm_list locked throughout all that or else
297 * we may end up suspending with a wrong ordering of it.
299 device_reorder_to_tail(consumer, NULL);
301 list_add_tail_rcu(&link->s_node, &supplier->links.consumers);
302 list_add_tail_rcu(&link->c_node, &consumer->links.suppliers);
304 dev_info(consumer, "Linked as a consumer to %s\n", dev_name(supplier));
308 device_links_write_unlock();
311 EXPORT_SYMBOL_GPL(device_link_add);
313 static void device_link_free(struct device_link *link)
315 put_device(link->consumer);
316 put_device(link->supplier);
321 static void __device_link_free_srcu(struct rcu_head *rhead)
323 device_link_free(container_of(rhead, struct device_link, rcu_head));
326 static void __device_link_del(struct kref *kref)
328 struct device_link *link = container_of(kref, struct device_link, kref);
330 dev_info(link->consumer, "Dropping the link to %s\n",
331 dev_name(link->supplier));
333 if (link->flags & DL_FLAG_PM_RUNTIME)
334 pm_runtime_drop_link(link->consumer);
336 list_del_rcu(&link->s_node);
337 list_del_rcu(&link->c_node);
338 call_srcu(&device_links_srcu, &link->rcu_head, __device_link_free_srcu);
340 #else /* !CONFIG_SRCU */
341 static void __device_link_del(struct kref *kref)
343 struct device_link *link = container_of(kref, struct device_link, kref);
345 dev_info(link->consumer, "Dropping the link to %s\n",
346 dev_name(link->supplier));
348 if (link->flags & DL_FLAG_PM_RUNTIME)
349 pm_runtime_drop_link(link->consumer);
351 list_del(&link->s_node);
352 list_del(&link->c_node);
353 device_link_free(link);
355 #endif /* !CONFIG_SRCU */
358 * device_link_del - Delete a link between two devices.
359 * @link: Device link to delete.
361 * The caller must ensure proper synchronization of this function with runtime
362 * PM. If the link was added multiple times, it needs to be deleted as often.
363 * Care is required for hotplugged devices: Their links are purged on removal
364 * and calling device_link_del() is then no longer allowed.
366 void device_link_del(struct device_link *link)
368 device_links_write_lock();
370 kref_put(&link->kref, __device_link_del);
372 device_links_write_unlock();
374 EXPORT_SYMBOL_GPL(device_link_del);
376 static void device_links_missing_supplier(struct device *dev)
378 struct device_link *link;
380 list_for_each_entry(link, &dev->links.suppliers, c_node)
381 if (link->status == DL_STATE_CONSUMER_PROBE)
382 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
386 * device_links_check_suppliers - Check presence of supplier drivers.
387 * @dev: Consumer device.
389 * Check links from this device to any suppliers. Walk the list of the device's
390 * links to suppliers and see if all of them are available. If not, simply
391 * return -EPROBE_DEFER.
393 * We need to guarantee that the supplier will not go away after the check has
394 * been positive here. It only can go away in __device_release_driver() and
395 * that function checks the device's links to consumers. This means we need to
396 * mark the link as "consumer probe in progress" to make the supplier removal
397 * wait for us to complete (or bad things may happen).
399 * Links with the DL_FLAG_STATELESS flag set are ignored.
401 int device_links_check_suppliers(struct device *dev)
403 struct device_link *link;
406 device_links_write_lock();
408 list_for_each_entry(link, &dev->links.suppliers, c_node) {
409 if (link->flags & DL_FLAG_STATELESS)
412 if (link->status != DL_STATE_AVAILABLE) {
413 device_links_missing_supplier(dev);
417 WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE);
419 dev->links.status = DL_DEV_PROBING;
421 device_links_write_unlock();
426 * device_links_driver_bound - Update device links after probing its driver.
427 * @dev: Device to update the links for.
429 * The probe has been successful, so update links from this device to any
430 * consumers by changing their status to "available".
432 * Also change the status of @dev's links to suppliers to "active".
434 * Links with the DL_FLAG_STATELESS flag set are ignored.
436 void device_links_driver_bound(struct device *dev)
438 struct device_link *link;
440 device_links_write_lock();
442 list_for_each_entry(link, &dev->links.consumers, s_node) {
443 if (link->flags & DL_FLAG_STATELESS)
446 WARN_ON(link->status != DL_STATE_DORMANT);
447 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
450 list_for_each_entry(link, &dev->links.suppliers, c_node) {
451 if (link->flags & DL_FLAG_STATELESS)
454 WARN_ON(link->status != DL_STATE_CONSUMER_PROBE);
455 WRITE_ONCE(link->status, DL_STATE_ACTIVE);
458 dev->links.status = DL_DEV_DRIVER_BOUND;
460 device_links_write_unlock();
464 * __device_links_no_driver - Update links of a device without a driver.
465 * @dev: Device without a drvier.
467 * Delete all non-persistent links from this device to any suppliers.
469 * Persistent links stay around, but their status is changed to "available",
470 * unless they already are in the "supplier unbind in progress" state in which
471 * case they need not be updated.
473 * Links with the DL_FLAG_STATELESS flag set are ignored.
475 static void __device_links_no_driver(struct device *dev)
477 struct device_link *link, *ln;
479 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
480 if (link->flags & DL_FLAG_STATELESS)
483 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER)
484 kref_put(&link->kref, __device_link_del);
485 else if (link->status != DL_STATE_SUPPLIER_UNBIND)
486 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
489 dev->links.status = DL_DEV_NO_DRIVER;
492 void device_links_no_driver(struct device *dev)
494 device_links_write_lock();
495 __device_links_no_driver(dev);
496 device_links_write_unlock();
500 * device_links_driver_cleanup - Update links after driver removal.
501 * @dev: Device whose driver has just gone away.
503 * Update links to consumers for @dev by changing their status to "dormant" and
504 * invoke %__device_links_no_driver() to update links to suppliers for it as
507 * Links with the DL_FLAG_STATELESS flag set are ignored.
509 void device_links_driver_cleanup(struct device *dev)
511 struct device_link *link;
513 device_links_write_lock();
515 list_for_each_entry(link, &dev->links.consumers, s_node) {
516 if (link->flags & DL_FLAG_STATELESS)
519 WARN_ON(link->flags & DL_FLAG_AUTOREMOVE_CONSUMER);
520 WARN_ON(link->status != DL_STATE_SUPPLIER_UNBIND);
521 WRITE_ONCE(link->status, DL_STATE_DORMANT);
524 __device_links_no_driver(dev);
526 device_links_write_unlock();
530 * device_links_busy - Check if there are any busy links to consumers.
531 * @dev: Device to check.
533 * Check each consumer of the device and return 'true' if its link's status
534 * is one of "consumer probe" or "active" (meaning that the given consumer is
535 * probing right now or its driver is present). Otherwise, change the link
536 * state to "supplier unbind" to prevent the consumer from being probed
537 * successfully going forward.
539 * Return 'false' if there are no probing or active consumers.
541 * Links with the DL_FLAG_STATELESS flag set are ignored.
543 bool device_links_busy(struct device *dev)
545 struct device_link *link;
548 device_links_write_lock();
550 list_for_each_entry(link, &dev->links.consumers, s_node) {
551 if (link->flags & DL_FLAG_STATELESS)
554 if (link->status == DL_STATE_CONSUMER_PROBE
555 || link->status == DL_STATE_ACTIVE) {
559 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
562 dev->links.status = DL_DEV_UNBINDING;
564 device_links_write_unlock();
569 * device_links_unbind_consumers - Force unbind consumers of the given device.
570 * @dev: Device to unbind the consumers of.
572 * Walk the list of links to consumers for @dev and if any of them is in the
573 * "consumer probe" state, wait for all device probes in progress to complete
576 * If that's not the case, change the status of the link to "supplier unbind"
577 * and check if the link was in the "active" state. If so, force the consumer
578 * driver to unbind and start over (the consumer will not re-probe as we have
579 * changed the state of the link already).
581 * Links with the DL_FLAG_STATELESS flag set are ignored.
583 void device_links_unbind_consumers(struct device *dev)
585 struct device_link *link;
588 device_links_write_lock();
590 list_for_each_entry(link, &dev->links.consumers, s_node) {
591 enum device_link_state status;
593 if (link->flags & DL_FLAG_STATELESS)
596 status = link->status;
597 if (status == DL_STATE_CONSUMER_PROBE) {
598 device_links_write_unlock();
600 wait_for_device_probe();
603 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
604 if (status == DL_STATE_ACTIVE) {
605 struct device *consumer = link->consumer;
607 get_device(consumer);
609 device_links_write_unlock();
611 device_release_driver_internal(consumer, NULL,
613 put_device(consumer);
618 device_links_write_unlock();
622 * device_links_purge - Delete existing links to other devices.
623 * @dev: Target device.
625 static void device_links_purge(struct device *dev)
627 struct device_link *link, *ln;
630 * Delete all of the remaining links from this device to any other
631 * devices (either consumers or suppliers).
633 device_links_write_lock();
635 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
636 WARN_ON(link->status == DL_STATE_ACTIVE);
637 __device_link_del(&link->kref);
640 list_for_each_entry_safe_reverse(link, ln, &dev->links.consumers, s_node) {
641 WARN_ON(link->status != DL_STATE_DORMANT &&
642 link->status != DL_STATE_NONE);
643 __device_link_del(&link->kref);
646 device_links_write_unlock();
649 /* Device links support end. */
651 int (*platform_notify)(struct device *dev) = NULL;
652 int (*platform_notify_remove)(struct device *dev) = NULL;
653 static struct kobject *dev_kobj;
654 struct kobject *sysfs_dev_char_kobj;
655 struct kobject *sysfs_dev_block_kobj;
657 static DEFINE_MUTEX(device_hotplug_lock);
659 void lock_device_hotplug(void)
661 mutex_lock(&device_hotplug_lock);
664 void unlock_device_hotplug(void)
666 mutex_unlock(&device_hotplug_lock);
669 int lock_device_hotplug_sysfs(void)
671 if (mutex_trylock(&device_hotplug_lock))
674 /* Avoid busy looping (5 ms of sleep should do). */
676 return restart_syscall();
680 static inline int device_is_not_partition(struct device *dev)
682 return !(dev->type == &part_type);
685 static inline int device_is_not_partition(struct device *dev)
692 * dev_driver_string - Return a device's driver name, if at all possible
693 * @dev: struct device to get the name of
695 * Will return the device's driver's name if it is bound to a device. If
696 * the device is not bound to a driver, it will return the name of the bus
697 * it is attached to. If it is not attached to a bus either, an empty
698 * string will be returned.
700 const char *dev_driver_string(const struct device *dev)
702 struct device_driver *drv;
704 /* dev->driver can change to NULL underneath us because of unbinding,
705 * so be careful about accessing it. dev->bus and dev->class should
706 * never change once they are set, so they don't need special care.
708 drv = READ_ONCE(dev->driver);
709 return drv ? drv->name :
710 (dev->bus ? dev->bus->name :
711 (dev->class ? dev->class->name : ""));
713 EXPORT_SYMBOL(dev_driver_string);
715 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
717 static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
720 struct device_attribute *dev_attr = to_dev_attr(attr);
721 struct device *dev = kobj_to_dev(kobj);
725 ret = dev_attr->show(dev, dev_attr, buf);
726 if (ret >= (ssize_t)PAGE_SIZE) {
727 printk("dev_attr_show: %pS returned bad count\n",
733 static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
734 const char *buf, size_t count)
736 struct device_attribute *dev_attr = to_dev_attr(attr);
737 struct device *dev = kobj_to_dev(kobj);
741 ret = dev_attr->store(dev, dev_attr, buf, count);
745 static const struct sysfs_ops dev_sysfs_ops = {
746 .show = dev_attr_show,
747 .store = dev_attr_store,
750 #define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
752 ssize_t device_store_ulong(struct device *dev,
753 struct device_attribute *attr,
754 const char *buf, size_t size)
756 struct dev_ext_attribute *ea = to_ext_attr(attr);
758 unsigned long new = simple_strtoul(buf, &end, 0);
761 *(unsigned long *)(ea->var) = new;
762 /* Always return full write size even if we didn't consume all */
765 EXPORT_SYMBOL_GPL(device_store_ulong);
767 ssize_t device_show_ulong(struct device *dev,
768 struct device_attribute *attr,
771 struct dev_ext_attribute *ea = to_ext_attr(attr);
772 return snprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var));
774 EXPORT_SYMBOL_GPL(device_show_ulong);
776 ssize_t device_store_int(struct device *dev,
777 struct device_attribute *attr,
778 const char *buf, size_t size)
780 struct dev_ext_attribute *ea = to_ext_attr(attr);
782 long new = simple_strtol(buf, &end, 0);
783 if (end == buf || new > INT_MAX || new < INT_MIN)
785 *(int *)(ea->var) = new;
786 /* Always return full write size even if we didn't consume all */
789 EXPORT_SYMBOL_GPL(device_store_int);
791 ssize_t device_show_int(struct device *dev,
792 struct device_attribute *attr,
795 struct dev_ext_attribute *ea = to_ext_attr(attr);
797 return snprintf(buf, PAGE_SIZE, "%d\n", *(int *)(ea->var));
799 EXPORT_SYMBOL_GPL(device_show_int);
801 ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
802 const char *buf, size_t size)
804 struct dev_ext_attribute *ea = to_ext_attr(attr);
806 if (strtobool(buf, ea->var) < 0)
811 EXPORT_SYMBOL_GPL(device_store_bool);
813 ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
816 struct dev_ext_attribute *ea = to_ext_attr(attr);
818 return snprintf(buf, PAGE_SIZE, "%d\n", *(bool *)(ea->var));
820 EXPORT_SYMBOL_GPL(device_show_bool);
823 * device_release - free device structure.
824 * @kobj: device's kobject.
826 * This is called once the reference count for the object
827 * reaches 0. We forward the call to the device's release
828 * method, which should handle actually freeing the structure.
830 static void device_release(struct kobject *kobj)
832 struct device *dev = kobj_to_dev(kobj);
833 struct device_private *p = dev->p;
836 * Some platform devices are driven without driver attached
837 * and managed resources may have been acquired. Make sure
838 * all resources are released.
840 * Drivers still can add resources into device after device
841 * is deleted but alive, so release devres here to avoid
842 * possible memory leak.
844 devres_release_all(dev);
848 else if (dev->type && dev->type->release)
849 dev->type->release(dev);
850 else if (dev->class && dev->class->dev_release)
851 dev->class->dev_release(dev);
853 WARN(1, KERN_ERR "Device '%s' does not have a release() "
854 "function, it is broken and must be fixed.\n",
859 static const void *device_namespace(struct kobject *kobj)
861 struct device *dev = kobj_to_dev(kobj);
862 const void *ns = NULL;
864 if (dev->class && dev->class->ns_type)
865 ns = dev->class->namespace(dev);
870 static struct kobj_type device_ktype = {
871 .release = device_release,
872 .sysfs_ops = &dev_sysfs_ops,
873 .namespace = device_namespace,
877 static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
879 struct kobj_type *ktype = get_ktype(kobj);
881 if (ktype == &device_ktype) {
882 struct device *dev = kobj_to_dev(kobj);
891 static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
893 struct device *dev = kobj_to_dev(kobj);
896 return dev->bus->name;
898 return dev->class->name;
902 static int dev_uevent(struct kset *kset, struct kobject *kobj,
903 struct kobj_uevent_env *env)
905 struct device *dev = kobj_to_dev(kobj);
908 /* add device node properties if present */
909 if (MAJOR(dev->devt)) {
913 kuid_t uid = GLOBAL_ROOT_UID;
914 kgid_t gid = GLOBAL_ROOT_GID;
916 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
917 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
918 name = device_get_devnode(dev, &mode, &uid, &gid, &tmp);
920 add_uevent_var(env, "DEVNAME=%s", name);
922 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
923 if (!uid_eq(uid, GLOBAL_ROOT_UID))
924 add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid));
925 if (!gid_eq(gid, GLOBAL_ROOT_GID))
926 add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid));
931 if (dev->type && dev->type->name)
932 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
935 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
937 /* Add common DT information about the device */
938 of_device_uevent(dev, env);
940 /* have the bus specific function add its stuff */
941 if (dev->bus && dev->bus->uevent) {
942 retval = dev->bus->uevent(dev, env);
944 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
945 dev_name(dev), __func__, retval);
948 /* have the class specific function add its stuff */
949 if (dev->class && dev->class->dev_uevent) {
950 retval = dev->class->dev_uevent(dev, env);
952 pr_debug("device: '%s': %s: class uevent() "
953 "returned %d\n", dev_name(dev),
957 /* have the device type specific function add its stuff */
958 if (dev->type && dev->type->uevent) {
959 retval = dev->type->uevent(dev, env);
961 pr_debug("device: '%s': %s: dev_type uevent() "
962 "returned %d\n", dev_name(dev),
969 static const struct kset_uevent_ops device_uevent_ops = {
970 .filter = dev_uevent_filter,
971 .name = dev_uevent_name,
972 .uevent = dev_uevent,
975 static ssize_t uevent_show(struct device *dev, struct device_attribute *attr,
978 struct kobject *top_kobj;
980 struct kobj_uevent_env *env = NULL;
985 /* search the kset, the device belongs to */
986 top_kobj = &dev->kobj;
987 while (!top_kobj->kset && top_kobj->parent)
988 top_kobj = top_kobj->parent;
992 kset = top_kobj->kset;
993 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
997 if (kset->uevent_ops && kset->uevent_ops->filter)
998 if (!kset->uevent_ops->filter(kset, &dev->kobj))
1001 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
1005 /* let the kset specific function add its keys */
1006 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
1010 /* copy keys to file */
1011 for (i = 0; i < env->envp_idx; i++)
1012 count += sprintf(&buf[count], "%s\n", env->envp[i]);
1018 static ssize_t uevent_store(struct device *dev, struct device_attribute *attr,
1019 const char *buf, size_t count)
1021 if (kobject_synth_uevent(&dev->kobj, buf, count))
1022 dev_err(dev, "uevent: failed to send synthetic uevent\n");
1026 static DEVICE_ATTR_RW(uevent);
1028 static ssize_t online_show(struct device *dev, struct device_attribute *attr,
1034 val = !dev->offline;
1036 return sprintf(buf, "%u\n", val);
1039 static ssize_t online_store(struct device *dev, struct device_attribute *attr,
1040 const char *buf, size_t count)
1045 ret = strtobool(buf, &val);
1049 ret = lock_device_hotplug_sysfs();
1053 ret = val ? device_online(dev) : device_offline(dev);
1054 unlock_device_hotplug();
1055 return ret < 0 ? ret : count;
1057 static DEVICE_ATTR_RW(online);
1059 int device_add_groups(struct device *dev, const struct attribute_group **groups)
1061 return sysfs_create_groups(&dev->kobj, groups);
1063 EXPORT_SYMBOL_GPL(device_add_groups);
1065 void device_remove_groups(struct device *dev,
1066 const struct attribute_group **groups)
1068 sysfs_remove_groups(&dev->kobj, groups);
1070 EXPORT_SYMBOL_GPL(device_remove_groups);
1072 union device_attr_group_devres {
1073 const struct attribute_group *group;
1074 const struct attribute_group **groups;
1077 static int devm_attr_group_match(struct device *dev, void *res, void *data)
1079 return ((union device_attr_group_devres *)res)->group == data;
1082 static void devm_attr_group_remove(struct device *dev, void *res)
1084 union device_attr_group_devres *devres = res;
1085 const struct attribute_group *group = devres->group;
1087 dev_dbg(dev, "%s: removing group %p\n", __func__, group);
1088 sysfs_remove_group(&dev->kobj, group);
1091 static void devm_attr_groups_remove(struct device *dev, void *res)
1093 union device_attr_group_devres *devres = res;
1094 const struct attribute_group **groups = devres->groups;
1096 dev_dbg(dev, "%s: removing groups %p\n", __func__, groups);
1097 sysfs_remove_groups(&dev->kobj, groups);
1101 * devm_device_add_group - given a device, create a managed attribute group
1102 * @dev: The device to create the group for
1103 * @grp: The attribute group to create
1105 * This function creates a group for the first time. It will explicitly
1106 * warn and error if any of the attribute files being created already exist.
1108 * Returns 0 on success or error code on failure.
1110 int devm_device_add_group(struct device *dev, const struct attribute_group *grp)
1112 union device_attr_group_devres *devres;
1115 devres = devres_alloc(devm_attr_group_remove,
1116 sizeof(*devres), GFP_KERNEL);
1120 error = sysfs_create_group(&dev->kobj, grp);
1122 devres_free(devres);
1126 devres->group = grp;
1127 devres_add(dev, devres);
1130 EXPORT_SYMBOL_GPL(devm_device_add_group);
1133 * devm_device_remove_group: remove a managed group from a device
1134 * @dev: device to remove the group from
1135 * @grp: group to remove
1137 * This function removes a group of attributes from a device. The attributes
1138 * previously have to have been created for this group, otherwise it will fail.
1140 void devm_device_remove_group(struct device *dev,
1141 const struct attribute_group *grp)
1143 WARN_ON(devres_release(dev, devm_attr_group_remove,
1144 devm_attr_group_match,
1145 /* cast away const */ (void *)grp));
1147 EXPORT_SYMBOL_GPL(devm_device_remove_group);
1150 * devm_device_add_groups - create a bunch of managed attribute groups
1151 * @dev: The device to create the group for
1152 * @groups: The attribute groups to create, NULL terminated
1154 * This function creates a bunch of managed attribute groups. If an error
1155 * occurs when creating a group, all previously created groups will be
1156 * removed, unwinding everything back to the original state when this
1157 * function was called. It will explicitly warn and error if any of the
1158 * attribute files being created already exist.
1160 * Returns 0 on success or error code from sysfs_create_group on failure.
1162 int devm_device_add_groups(struct device *dev,
1163 const struct attribute_group **groups)
1165 union device_attr_group_devres *devres;
1168 devres = devres_alloc(devm_attr_groups_remove,
1169 sizeof(*devres), GFP_KERNEL);
1173 error = sysfs_create_groups(&dev->kobj, groups);
1175 devres_free(devres);
1179 devres->groups = groups;
1180 devres_add(dev, devres);
1183 EXPORT_SYMBOL_GPL(devm_device_add_groups);
1186 * devm_device_remove_groups - remove a list of managed groups
1188 * @dev: The device for the groups to be removed from
1189 * @groups: NULL terminated list of groups to be removed
1191 * If groups is not NULL, remove the specified groups from the device.
1193 void devm_device_remove_groups(struct device *dev,
1194 const struct attribute_group **groups)
1196 WARN_ON(devres_release(dev, devm_attr_groups_remove,
1197 devm_attr_group_match,
1198 /* cast away const */ (void *)groups));
1200 EXPORT_SYMBOL_GPL(devm_device_remove_groups);
1202 static int device_add_attrs(struct device *dev)
1204 struct class *class = dev->class;
1205 const struct device_type *type = dev->type;
1209 error = device_add_groups(dev, class->dev_groups);
1215 error = device_add_groups(dev, type->groups);
1217 goto err_remove_class_groups;
1220 error = device_add_groups(dev, dev->groups);
1222 goto err_remove_type_groups;
1224 if (device_supports_offline(dev) && !dev->offline_disabled) {
1225 error = device_create_file(dev, &dev_attr_online);
1227 goto err_remove_dev_groups;
1232 err_remove_dev_groups:
1233 device_remove_groups(dev, dev->groups);
1234 err_remove_type_groups:
1236 device_remove_groups(dev, type->groups);
1237 err_remove_class_groups:
1239 device_remove_groups(dev, class->dev_groups);
1244 static void device_remove_attrs(struct device *dev)
1246 struct class *class = dev->class;
1247 const struct device_type *type = dev->type;
1249 device_remove_file(dev, &dev_attr_online);
1250 device_remove_groups(dev, dev->groups);
1253 device_remove_groups(dev, type->groups);
1256 device_remove_groups(dev, class->dev_groups);
1259 static ssize_t dev_show(struct device *dev, struct device_attribute *attr,
1262 return print_dev_t(buf, dev->devt);
1264 static DEVICE_ATTR_RO(dev);
1267 struct kset *devices_kset;
1270 * devices_kset_move_before - Move device in the devices_kset's list.
1271 * @deva: Device to move.
1272 * @devb: Device @deva should come before.
1274 static void devices_kset_move_before(struct device *deva, struct device *devb)
1278 pr_debug("devices_kset: Moving %s before %s\n",
1279 dev_name(deva), dev_name(devb));
1280 spin_lock(&devices_kset->list_lock);
1281 list_move_tail(&deva->kobj.entry, &devb->kobj.entry);
1282 spin_unlock(&devices_kset->list_lock);
1286 * devices_kset_move_after - Move device in the devices_kset's list.
1287 * @deva: Device to move
1288 * @devb: Device @deva should come after.
1290 static void devices_kset_move_after(struct device *deva, struct device *devb)
1294 pr_debug("devices_kset: Moving %s after %s\n",
1295 dev_name(deva), dev_name(devb));
1296 spin_lock(&devices_kset->list_lock);
1297 list_move(&deva->kobj.entry, &devb->kobj.entry);
1298 spin_unlock(&devices_kset->list_lock);
1302 * devices_kset_move_last - move the device to the end of devices_kset's list.
1303 * @dev: device to move
1305 void devices_kset_move_last(struct device *dev)
1309 pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev));
1310 spin_lock(&devices_kset->list_lock);
1311 list_move_tail(&dev->kobj.entry, &devices_kset->list);
1312 spin_unlock(&devices_kset->list_lock);
1316 * device_create_file - create sysfs attribute file for device.
1318 * @attr: device attribute descriptor.
1320 int device_create_file(struct device *dev,
1321 const struct device_attribute *attr)
1326 WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
1327 "Attribute %s: write permission without 'store'\n",
1329 WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
1330 "Attribute %s: read permission without 'show'\n",
1332 error = sysfs_create_file(&dev->kobj, &attr->attr);
1337 EXPORT_SYMBOL_GPL(device_create_file);
1340 * device_remove_file - remove sysfs attribute file.
1342 * @attr: device attribute descriptor.
1344 void device_remove_file(struct device *dev,
1345 const struct device_attribute *attr)
1348 sysfs_remove_file(&dev->kobj, &attr->attr);
1350 EXPORT_SYMBOL_GPL(device_remove_file);
1353 * device_remove_file_self - remove sysfs attribute file from its own method.
1355 * @attr: device attribute descriptor.
1357 * See kernfs_remove_self() for details.
1359 bool device_remove_file_self(struct device *dev,
1360 const struct device_attribute *attr)
1363 return sysfs_remove_file_self(&dev->kobj, &attr->attr);
1367 EXPORT_SYMBOL_GPL(device_remove_file_self);
1370 * device_create_bin_file - create sysfs binary attribute file for device.
1372 * @attr: device binary attribute descriptor.
1374 int device_create_bin_file(struct device *dev,
1375 const struct bin_attribute *attr)
1377 int error = -EINVAL;
1379 error = sysfs_create_bin_file(&dev->kobj, attr);
1382 EXPORT_SYMBOL_GPL(device_create_bin_file);
1385 * device_remove_bin_file - remove sysfs binary attribute file
1387 * @attr: device binary attribute descriptor.
1389 void device_remove_bin_file(struct device *dev,
1390 const struct bin_attribute *attr)
1393 sysfs_remove_bin_file(&dev->kobj, attr);
1395 EXPORT_SYMBOL_GPL(device_remove_bin_file);
1397 static void klist_children_get(struct klist_node *n)
1399 struct device_private *p = to_device_private_parent(n);
1400 struct device *dev = p->device;
1405 static void klist_children_put(struct klist_node *n)
1407 struct device_private *p = to_device_private_parent(n);
1408 struct device *dev = p->device;
1414 * device_initialize - init device structure.
1417 * This prepares the device for use by other layers by initializing
1419 * It is the first half of device_register(), if called by
1420 * that function, though it can also be called separately, so one
1421 * may use @dev's fields. In particular, get_device()/put_device()
1422 * may be used for reference counting of @dev after calling this
1425 * All fields in @dev must be initialized by the caller to 0, except
1426 * for those explicitly set to some other value. The simplest
1427 * approach is to use kzalloc() to allocate the structure containing
1430 * NOTE: Use put_device() to give up your reference instead of freeing
1431 * @dev directly once you have called this function.
1433 void device_initialize(struct device *dev)
1435 dev->kobj.kset = devices_kset;
1436 kobject_init(&dev->kobj, &device_ktype);
1437 INIT_LIST_HEAD(&dev->dma_pools);
1438 mutex_init(&dev->mutex);
1439 lockdep_set_novalidate_class(&dev->mutex);
1440 spin_lock_init(&dev->devres_lock);
1441 INIT_LIST_HEAD(&dev->devres_head);
1442 device_pm_init(dev);
1443 set_dev_node(dev, -1);
1444 #ifdef CONFIG_GENERIC_MSI_IRQ
1445 INIT_LIST_HEAD(&dev->msi_list);
1447 INIT_LIST_HEAD(&dev->links.consumers);
1448 INIT_LIST_HEAD(&dev->links.suppliers);
1449 dev->links.status = DL_DEV_NO_DRIVER;
1451 EXPORT_SYMBOL_GPL(device_initialize);
1453 struct kobject *virtual_device_parent(struct device *dev)
1455 static struct kobject *virtual_dir = NULL;
1458 virtual_dir = kobject_create_and_add("virtual",
1459 &devices_kset->kobj);
1465 struct kobject kobj;
1466 struct class *class;
1469 #define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
1471 static void class_dir_release(struct kobject *kobj)
1473 struct class_dir *dir = to_class_dir(kobj);
1478 struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
1480 struct class_dir *dir = to_class_dir(kobj);
1481 return dir->class->ns_type;
1484 static struct kobj_type class_dir_ktype = {
1485 .release = class_dir_release,
1486 .sysfs_ops = &kobj_sysfs_ops,
1487 .child_ns_type = class_dir_child_ns_type
1490 static struct kobject *
1491 class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
1493 struct class_dir *dir;
1496 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1498 return ERR_PTR(-ENOMEM);
1501 kobject_init(&dir->kobj, &class_dir_ktype);
1503 dir->kobj.kset = &class->p->glue_dirs;
1505 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
1507 kobject_put(&dir->kobj);
1508 return ERR_PTR(retval);
1513 static DEFINE_MUTEX(gdp_mutex);
1515 static struct kobject *get_device_parent(struct device *dev,
1516 struct device *parent)
1519 struct kobject *kobj = NULL;
1520 struct kobject *parent_kobj;
1524 /* block disks show up in /sys/block */
1525 if (sysfs_deprecated && dev->class == &block_class) {
1526 if (parent && parent->class == &block_class)
1527 return &parent->kobj;
1528 return &block_class.p->subsys.kobj;
1533 * If we have no parent, we live in "virtual".
1534 * Class-devices with a non class-device as parent, live
1535 * in a "glue" directory to prevent namespace collisions.
1538 parent_kobj = virtual_device_parent(dev);
1539 else if (parent->class && !dev->class->ns_type)
1540 return &parent->kobj;
1542 parent_kobj = &parent->kobj;
1544 mutex_lock(&gdp_mutex);
1546 /* find our class-directory at the parent and reference it */
1547 spin_lock(&dev->class->p->glue_dirs.list_lock);
1548 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
1549 if (k->parent == parent_kobj) {
1550 kobj = kobject_get(k);
1553 spin_unlock(&dev->class->p->glue_dirs.list_lock);
1555 mutex_unlock(&gdp_mutex);
1559 /* or create a new class-directory at the parent device */
1560 k = class_dir_create_and_add(dev->class, parent_kobj);
1561 /* do not emit an uevent for this simple "glue" directory */
1562 mutex_unlock(&gdp_mutex);
1566 /* subsystems can specify a default root directory for their devices */
1567 if (!parent && dev->bus && dev->bus->dev_root)
1568 return &dev->bus->dev_root->kobj;
1571 return &parent->kobj;
1575 static inline bool live_in_glue_dir(struct kobject *kobj,
1578 if (!kobj || !dev->class ||
1579 kobj->kset != &dev->class->p->glue_dirs)
1584 static inline struct kobject *get_glue_dir(struct device *dev)
1586 return dev->kobj.parent;
1590 * make sure cleaning up dir as the last step, we need to make
1591 * sure .release handler of kobject is run with holding the
1594 static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
1596 /* see if we live in a "glue" directory */
1597 if (!live_in_glue_dir(glue_dir, dev))
1600 mutex_lock(&gdp_mutex);
1601 kobject_put(glue_dir);
1602 mutex_unlock(&gdp_mutex);
1605 static int device_add_class_symlinks(struct device *dev)
1607 struct device_node *of_node = dev_of_node(dev);
1611 error = sysfs_create_link(&dev->kobj, of_node_kobj(of_node), "of_node");
1613 dev_warn(dev, "Error %d creating of_node link\n",error);
1614 /* An error here doesn't warrant bringing down the device */
1620 error = sysfs_create_link(&dev->kobj,
1621 &dev->class->p->subsys.kobj,
1626 if (dev->parent && device_is_not_partition(dev)) {
1627 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
1634 /* /sys/block has directories and does not need symlinks */
1635 if (sysfs_deprecated && dev->class == &block_class)
1639 /* link in the class directory pointing to the device */
1640 error = sysfs_create_link(&dev->class->p->subsys.kobj,
1641 &dev->kobj, dev_name(dev));
1648 sysfs_remove_link(&dev->kobj, "device");
1651 sysfs_remove_link(&dev->kobj, "subsystem");
1653 sysfs_remove_link(&dev->kobj, "of_node");
1657 static void device_remove_class_symlinks(struct device *dev)
1659 if (dev_of_node(dev))
1660 sysfs_remove_link(&dev->kobj, "of_node");
1665 if (dev->parent && device_is_not_partition(dev))
1666 sysfs_remove_link(&dev->kobj, "device");
1667 sysfs_remove_link(&dev->kobj, "subsystem");
1669 if (sysfs_deprecated && dev->class == &block_class)
1672 sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
1676 * dev_set_name - set a device name
1678 * @fmt: format string for the device's name
1680 int dev_set_name(struct device *dev, const char *fmt, ...)
1685 va_start(vargs, fmt);
1686 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
1690 EXPORT_SYMBOL_GPL(dev_set_name);
1693 * device_to_dev_kobj - select a /sys/dev/ directory for the device
1696 * By default we select char/ for new entries. Setting class->dev_obj
1697 * to NULL prevents an entry from being created. class->dev_kobj must
1698 * be set (or cleared) before any devices are registered to the class
1699 * otherwise device_create_sys_dev_entry() and
1700 * device_remove_sys_dev_entry() will disagree about the presence of
1703 static struct kobject *device_to_dev_kobj(struct device *dev)
1705 struct kobject *kobj;
1708 kobj = dev->class->dev_kobj;
1710 kobj = sysfs_dev_char_kobj;
1715 static int device_create_sys_dev_entry(struct device *dev)
1717 struct kobject *kobj = device_to_dev_kobj(dev);
1722 format_dev_t(devt_str, dev->devt);
1723 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
1729 static void device_remove_sys_dev_entry(struct device *dev)
1731 struct kobject *kobj = device_to_dev_kobj(dev);
1735 format_dev_t(devt_str, dev->devt);
1736 sysfs_remove_link(kobj, devt_str);
1740 int device_private_init(struct device *dev)
1742 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
1745 dev->p->device = dev;
1746 klist_init(&dev->p->klist_children, klist_children_get,
1747 klist_children_put);
1748 INIT_LIST_HEAD(&dev->p->deferred_probe);
1753 * device_add - add device to device hierarchy.
1756 * This is part 2 of device_register(), though may be called
1757 * separately _iff_ device_initialize() has been called separately.
1759 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
1760 * to the global and sibling lists for the device, then
1761 * adds it to the other relevant subsystems of the driver model.
1763 * Do not call this routine or device_register() more than once for
1764 * any device structure. The driver model core is not designed to work
1765 * with devices that get unregistered and then spring back to life.
1766 * (Among other things, it's very hard to guarantee that all references
1767 * to the previous incarnation of @dev have been dropped.) Allocate
1768 * and register a fresh new struct device instead.
1770 * NOTE: _Never_ directly free @dev after calling this function, even
1771 * if it returned an error! Always use put_device() to give up your
1772 * reference instead.
1774 int device_add(struct device *dev)
1776 struct device *parent;
1777 struct kobject *kobj;
1778 struct class_interface *class_intf;
1779 int error = -EINVAL;
1780 struct kobject *glue_dir = NULL;
1782 dev = get_device(dev);
1787 error = device_private_init(dev);
1793 * for statically allocated devices, which should all be converted
1794 * some day, we need to initialize the name. We prevent reading back
1795 * the name, and force the use of dev_name()
1797 if (dev->init_name) {
1798 dev_set_name(dev, "%s", dev->init_name);
1799 dev->init_name = NULL;
1802 /* subsystems can specify simple device enumeration */
1803 if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
1804 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
1806 if (!dev_name(dev)) {
1811 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
1813 parent = get_device(dev->parent);
1814 kobj = get_device_parent(dev, parent);
1816 error = PTR_ERR(kobj);
1820 dev->kobj.parent = kobj;
1822 /* use parent numa_node */
1823 if (parent && (dev_to_node(dev) == NUMA_NO_NODE))
1824 set_dev_node(dev, dev_to_node(parent));
1826 /* first, register with generic layer. */
1827 /* we require the name to be set before, and pass NULL */
1828 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
1830 glue_dir = get_glue_dir(dev);
1834 /* notify platform of device entry */
1835 if (platform_notify)
1836 platform_notify(dev);
1838 error = device_create_file(dev, &dev_attr_uevent);
1842 error = device_add_class_symlinks(dev);
1845 error = device_add_attrs(dev);
1848 error = bus_add_device(dev);
1851 error = dpm_sysfs_add(dev);
1856 if (MAJOR(dev->devt)) {
1857 error = device_create_file(dev, &dev_attr_dev);
1861 error = device_create_sys_dev_entry(dev);
1865 devtmpfs_create_node(dev);
1868 /* Notify clients of device addition. This call must come
1869 * after dpm_sysfs_add() and before kobject_uevent().
1872 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1873 BUS_NOTIFY_ADD_DEVICE, dev);
1875 kobject_uevent(&dev->kobj, KOBJ_ADD);
1876 bus_probe_device(dev);
1878 klist_add_tail(&dev->p->knode_parent,
1879 &parent->p->klist_children);
1882 mutex_lock(&dev->class->p->mutex);
1883 /* tie the class to the device */
1884 klist_add_tail(&dev->knode_class,
1885 &dev->class->p->klist_devices);
1887 /* notify any interfaces that the device is here */
1888 list_for_each_entry(class_intf,
1889 &dev->class->p->interfaces, node)
1890 if (class_intf->add_dev)
1891 class_intf->add_dev(dev, class_intf);
1892 mutex_unlock(&dev->class->p->mutex);
1898 if (MAJOR(dev->devt))
1899 device_remove_file(dev, &dev_attr_dev);
1901 device_pm_remove(dev);
1902 dpm_sysfs_remove(dev);
1904 bus_remove_device(dev);
1906 device_remove_attrs(dev);
1908 device_remove_class_symlinks(dev);
1910 device_remove_file(dev, &dev_attr_uevent);
1912 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
1913 glue_dir = get_glue_dir(dev);
1914 kobject_del(&dev->kobj);
1916 cleanup_glue_dir(dev, glue_dir);
1924 EXPORT_SYMBOL_GPL(device_add);
1927 * device_register - register a device with the system.
1928 * @dev: pointer to the device structure
1930 * This happens in two clean steps - initialize the device
1931 * and add it to the system. The two steps can be called
1932 * separately, but this is the easiest and most common.
1933 * I.e. you should only call the two helpers separately if
1934 * have a clearly defined need to use and refcount the device
1935 * before it is added to the hierarchy.
1937 * For more information, see the kerneldoc for device_initialize()
1940 * NOTE: _Never_ directly free @dev after calling this function, even
1941 * if it returned an error! Always use put_device() to give up the
1942 * reference initialized in this function instead.
1944 int device_register(struct device *dev)
1946 device_initialize(dev);
1947 return device_add(dev);
1949 EXPORT_SYMBOL_GPL(device_register);
1952 * get_device - increment reference count for device.
1955 * This simply forwards the call to kobject_get(), though
1956 * we do take care to provide for the case that we get a NULL
1957 * pointer passed in.
1959 struct device *get_device(struct device *dev)
1961 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
1963 EXPORT_SYMBOL_GPL(get_device);
1966 * put_device - decrement reference count.
1967 * @dev: device in question.
1969 void put_device(struct device *dev)
1971 /* might_sleep(); */
1973 kobject_put(&dev->kobj);
1975 EXPORT_SYMBOL_GPL(put_device);
1978 * device_del - delete device from system.
1981 * This is the first part of the device unregistration
1982 * sequence. This removes the device from the lists we control
1983 * from here, has it removed from the other driver model
1984 * subsystems it was added to in device_add(), and removes it
1985 * from the kobject hierarchy.
1987 * NOTE: this should be called manually _iff_ device_add() was
1988 * also called manually.
1990 void device_del(struct device *dev)
1992 struct device *parent = dev->parent;
1993 struct kobject *glue_dir = NULL;
1994 struct class_interface *class_intf;
1996 /* Notify clients of device removal. This call must come
1997 * before dpm_sysfs_remove().
2000 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2001 BUS_NOTIFY_DEL_DEVICE, dev);
2003 dpm_sysfs_remove(dev);
2005 klist_del(&dev->p->knode_parent);
2006 if (MAJOR(dev->devt)) {
2007 devtmpfs_delete_node(dev);
2008 device_remove_sys_dev_entry(dev);
2009 device_remove_file(dev, &dev_attr_dev);
2012 device_remove_class_symlinks(dev);
2014 mutex_lock(&dev->class->p->mutex);
2015 /* notify any interfaces that the device is now gone */
2016 list_for_each_entry(class_intf,
2017 &dev->class->p->interfaces, node)
2018 if (class_intf->remove_dev)
2019 class_intf->remove_dev(dev, class_intf);
2020 /* remove the device from the class list */
2021 klist_del(&dev->knode_class);
2022 mutex_unlock(&dev->class->p->mutex);
2024 device_remove_file(dev, &dev_attr_uevent);
2025 device_remove_attrs(dev);
2026 bus_remove_device(dev);
2027 device_pm_remove(dev);
2028 driver_deferred_probe_del(dev);
2029 device_remove_properties(dev);
2030 device_links_purge(dev);
2032 /* Notify the platform of the removal, in case they
2033 * need to do anything...
2035 if (platform_notify_remove)
2036 platform_notify_remove(dev);
2038 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2039 BUS_NOTIFY_REMOVED_DEVICE, dev);
2040 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
2041 glue_dir = get_glue_dir(dev);
2042 kobject_del(&dev->kobj);
2043 cleanup_glue_dir(dev, glue_dir);
2046 EXPORT_SYMBOL_GPL(device_del);
2049 * device_unregister - unregister device from system.
2050 * @dev: device going away.
2052 * We do this in two parts, like we do device_register(). First,
2053 * we remove it from all the subsystems with device_del(), then
2054 * we decrement the reference count via put_device(). If that
2055 * is the final reference count, the device will be cleaned up
2056 * via device_release() above. Otherwise, the structure will
2057 * stick around until the final reference to the device is dropped.
2059 void device_unregister(struct device *dev)
2061 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
2065 EXPORT_SYMBOL_GPL(device_unregister);
2067 static struct device *prev_device(struct klist_iter *i)
2069 struct klist_node *n = klist_prev(i);
2070 struct device *dev = NULL;
2071 struct device_private *p;
2074 p = to_device_private_parent(n);
2080 static struct device *next_device(struct klist_iter *i)
2082 struct klist_node *n = klist_next(i);
2083 struct device *dev = NULL;
2084 struct device_private *p;
2087 p = to_device_private_parent(n);
2094 * device_get_devnode - path of device node file
2096 * @mode: returned file access mode
2097 * @uid: returned file owner
2098 * @gid: returned file group
2099 * @tmp: possibly allocated string
2101 * Return the relative path of a possible device node.
2102 * Non-default names may need to allocate a memory to compose
2103 * a name. This memory is returned in tmp and needs to be
2104 * freed by the caller.
2106 const char *device_get_devnode(struct device *dev,
2107 umode_t *mode, kuid_t *uid, kgid_t *gid,
2114 /* the device type may provide a specific name */
2115 if (dev->type && dev->type->devnode)
2116 *tmp = dev->type->devnode(dev, mode, uid, gid);
2120 /* the class may provide a specific name */
2121 if (dev->class && dev->class->devnode)
2122 *tmp = dev->class->devnode(dev, mode);
2126 /* return name without allocation, tmp == NULL */
2127 if (strchr(dev_name(dev), '!') == NULL)
2128 return dev_name(dev);
2130 /* replace '!' in the name with '/' */
2131 s = kstrdup(dev_name(dev), GFP_KERNEL);
2134 strreplace(s, '!', '/');
2139 * device_for_each_child - device child iterator.
2140 * @parent: parent struct device.
2141 * @fn: function to be called for each device.
2142 * @data: data for the callback.
2144 * Iterate over @parent's child devices, and call @fn for each,
2147 * We check the return of @fn each time. If it returns anything
2148 * other than 0, we break out and return that value.
2150 int device_for_each_child(struct device *parent, void *data,
2151 int (*fn)(struct device *dev, void *data))
2153 struct klist_iter i;
2154 struct device *child;
2160 klist_iter_init(&parent->p->klist_children, &i);
2161 while (!error && (child = next_device(&i)))
2162 error = fn(child, data);
2163 klist_iter_exit(&i);
2166 EXPORT_SYMBOL_GPL(device_for_each_child);
2169 * device_for_each_child_reverse - device child iterator in reversed order.
2170 * @parent: parent struct device.
2171 * @fn: function to be called for each device.
2172 * @data: data for the callback.
2174 * Iterate over @parent's child devices, and call @fn for each,
2177 * We check the return of @fn each time. If it returns anything
2178 * other than 0, we break out and return that value.
2180 int device_for_each_child_reverse(struct device *parent, void *data,
2181 int (*fn)(struct device *dev, void *data))
2183 struct klist_iter i;
2184 struct device *child;
2190 klist_iter_init(&parent->p->klist_children, &i);
2191 while ((child = prev_device(&i)) && !error)
2192 error = fn(child, data);
2193 klist_iter_exit(&i);
2196 EXPORT_SYMBOL_GPL(device_for_each_child_reverse);
2199 * device_find_child - device iterator for locating a particular device.
2200 * @parent: parent struct device
2201 * @match: Callback function to check device
2202 * @data: Data to pass to match function
2204 * This is similar to the device_for_each_child() function above, but it
2205 * returns a reference to a device that is 'found' for later use, as
2206 * determined by the @match callback.
2208 * The callback should return 0 if the device doesn't match and non-zero
2209 * if it does. If the callback returns non-zero and a reference to the
2210 * current device can be obtained, this function will return to the caller
2211 * and not iterate over any more devices.
2213 * NOTE: you will need to drop the reference with put_device() after use.
2215 struct device *device_find_child(struct device *parent, void *data,
2216 int (*match)(struct device *dev, void *data))
2218 struct klist_iter i;
2219 struct device *child;
2224 klist_iter_init(&parent->p->klist_children, &i);
2225 while ((child = next_device(&i)))
2226 if (match(child, data) && get_device(child))
2228 klist_iter_exit(&i);
2231 EXPORT_SYMBOL_GPL(device_find_child);
2233 int __init devices_init(void)
2235 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
2238 dev_kobj = kobject_create_and_add("dev", NULL);
2241 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
2242 if (!sysfs_dev_block_kobj)
2243 goto block_kobj_err;
2244 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
2245 if (!sysfs_dev_char_kobj)
2251 kobject_put(sysfs_dev_block_kobj);
2253 kobject_put(dev_kobj);
2255 kset_unregister(devices_kset);
2259 static int device_check_offline(struct device *dev, void *not_used)
2263 ret = device_for_each_child(dev, NULL, device_check_offline);
2267 return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
2271 * device_offline - Prepare the device for hot-removal.
2272 * @dev: Device to be put offline.
2274 * Execute the device bus type's .offline() callback, if present, to prepare
2275 * the device for a subsequent hot-removal. If that succeeds, the device must
2276 * not be used until either it is removed or its bus type's .online() callback
2279 * Call under device_hotplug_lock.
2281 int device_offline(struct device *dev)
2285 if (dev->offline_disabled)
2288 ret = device_for_each_child(dev, NULL, device_check_offline);
2293 if (device_supports_offline(dev)) {
2297 ret = dev->bus->offline(dev);
2299 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
2300 dev->offline = true;
2310 * device_online - Put the device back online after successful device_offline().
2311 * @dev: Device to be put back online.
2313 * If device_offline() has been successfully executed for @dev, but the device
2314 * has not been removed subsequently, execute its bus type's .online() callback
2315 * to indicate that the device can be used again.
2317 * Call under device_hotplug_lock.
2319 int device_online(struct device *dev)
2324 if (device_supports_offline(dev)) {
2326 ret = dev->bus->online(dev);
2328 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
2329 dev->offline = false;
2340 struct root_device {
2342 struct module *owner;
2345 static inline struct root_device *to_root_device(struct device *d)
2347 return container_of(d, struct root_device, dev);
2350 static void root_device_release(struct device *dev)
2352 kfree(to_root_device(dev));
2356 * __root_device_register - allocate and register a root device
2357 * @name: root device name
2358 * @owner: owner module of the root device, usually THIS_MODULE
2360 * This function allocates a root device and registers it
2361 * using device_register(). In order to free the returned
2362 * device, use root_device_unregister().
2364 * Root devices are dummy devices which allow other devices
2365 * to be grouped under /sys/devices. Use this function to
2366 * allocate a root device and then use it as the parent of
2367 * any device which should appear under /sys/devices/{name}
2369 * The /sys/devices/{name} directory will also contain a
2370 * 'module' symlink which points to the @owner directory
2373 * Returns &struct device pointer on success, or ERR_PTR() on error.
2375 * Note: You probably want to use root_device_register().
2377 struct device *__root_device_register(const char *name, struct module *owner)
2379 struct root_device *root;
2382 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
2384 return ERR_PTR(err);
2386 err = dev_set_name(&root->dev, "%s", name);
2389 return ERR_PTR(err);
2392 root->dev.release = root_device_release;
2394 err = device_register(&root->dev);
2396 put_device(&root->dev);
2397 return ERR_PTR(err);
2400 #ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
2402 struct module_kobject *mk = &owner->mkobj;
2404 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
2406 device_unregister(&root->dev);
2407 return ERR_PTR(err);
2409 root->owner = owner;
2415 EXPORT_SYMBOL_GPL(__root_device_register);
2418 * root_device_unregister - unregister and free a root device
2419 * @dev: device going away
2421 * This function unregisters and cleans up a device that was created by
2422 * root_device_register().
2424 void root_device_unregister(struct device *dev)
2426 struct root_device *root = to_root_device(dev);
2429 sysfs_remove_link(&root->dev.kobj, "module");
2431 device_unregister(dev);
2433 EXPORT_SYMBOL_GPL(root_device_unregister);
2436 static void device_create_release(struct device *dev)
2438 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
2442 static __printf(6, 0) struct device *
2443 device_create_groups_vargs(struct class *class, struct device *parent,
2444 dev_t devt, void *drvdata,
2445 const struct attribute_group **groups,
2446 const char *fmt, va_list args)
2448 struct device *dev = NULL;
2449 int retval = -ENODEV;
2451 if (class == NULL || IS_ERR(class))
2454 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2460 device_initialize(dev);
2463 dev->parent = parent;
2464 dev->groups = groups;
2465 dev->release = device_create_release;
2466 dev_set_drvdata(dev, drvdata);
2468 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
2472 retval = device_add(dev);
2480 return ERR_PTR(retval);
2484 * device_create_vargs - creates a device and registers it with sysfs
2485 * @class: pointer to the struct class that this device should be registered to
2486 * @parent: pointer to the parent struct device of this new device, if any
2487 * @devt: the dev_t for the char device to be added
2488 * @drvdata: the data to be added to the device for callbacks
2489 * @fmt: string for the device's name
2490 * @args: va_list for the device's name
2492 * This function can be used by char device classes. A struct device
2493 * will be created in sysfs, registered to the specified class.
2495 * A "dev" file will be created, showing the dev_t for the device, if
2496 * the dev_t is not 0,0.
2497 * If a pointer to a parent struct device is passed in, the newly created
2498 * struct device will be a child of that device in sysfs.
2499 * The pointer to the struct device will be returned from the call.
2500 * Any further sysfs files that might be required can be created using this
2503 * Returns &struct device pointer on success, or ERR_PTR() on error.
2505 * Note: the struct class passed to this function must have previously
2506 * been created with a call to class_create().
2508 struct device *device_create_vargs(struct class *class, struct device *parent,
2509 dev_t devt, void *drvdata, const char *fmt,
2512 return device_create_groups_vargs(class, parent, devt, drvdata, NULL,
2515 EXPORT_SYMBOL_GPL(device_create_vargs);
2518 * device_create - creates a device and registers it with sysfs
2519 * @class: pointer to the struct class that this device should be registered to
2520 * @parent: pointer to the parent struct device of this new device, if any
2521 * @devt: the dev_t for the char device to be added
2522 * @drvdata: the data to be added to the device for callbacks
2523 * @fmt: string for the device's name
2525 * This function can be used by char device classes. A struct device
2526 * will be created in sysfs, registered to the specified class.
2528 * A "dev" file will be created, showing the dev_t for the device, if
2529 * the dev_t is not 0,0.
2530 * If a pointer to a parent struct device is passed in, the newly created
2531 * struct device will be a child of that device in sysfs.
2532 * The pointer to the struct device will be returned from the call.
2533 * Any further sysfs files that might be required can be created using this
2536 * Returns &struct device pointer on success, or ERR_PTR() on error.
2538 * Note: the struct class passed to this function must have previously
2539 * been created with a call to class_create().
2541 struct device *device_create(struct class *class, struct device *parent,
2542 dev_t devt, void *drvdata, const char *fmt, ...)
2547 va_start(vargs, fmt);
2548 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
2552 EXPORT_SYMBOL_GPL(device_create);
2555 * device_create_with_groups - 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 * @groups: NULL-terminated list of attribute groups to be created
2561 * @fmt: string 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.
2565 * Additional attributes specified in the groups parameter will also
2566 * be created automatically.
2568 * A "dev" file will be created, showing the dev_t for the device, if
2569 * the dev_t is not 0,0.
2570 * If a pointer to a parent struct device is passed in, the newly created
2571 * struct device will be a child of that device in sysfs.
2572 * The pointer to the struct device will be returned from the call.
2573 * Any further sysfs files that might be required can be created using this
2576 * Returns &struct device pointer on success, or ERR_PTR() on error.
2578 * Note: the struct class passed to this function must have previously
2579 * been created with a call to class_create().
2581 struct device *device_create_with_groups(struct class *class,
2582 struct device *parent, dev_t devt,
2584 const struct attribute_group **groups,
2585 const char *fmt, ...)
2590 va_start(vargs, fmt);
2591 dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
2596 EXPORT_SYMBOL_GPL(device_create_with_groups);
2598 static int __match_devt(struct device *dev, const void *data)
2600 const dev_t *devt = data;
2602 return dev->devt == *devt;
2606 * device_destroy - removes a device that was created with device_create()
2607 * @class: pointer to the struct class that this device was registered with
2608 * @devt: the dev_t of the device that was previously registered
2610 * This call unregisters and cleans up a device that was created with a
2611 * call to device_create().
2613 void device_destroy(struct class *class, dev_t devt)
2617 dev = class_find_device(class, NULL, &devt, __match_devt);
2620 device_unregister(dev);
2623 EXPORT_SYMBOL_GPL(device_destroy);
2626 * device_rename - renames a device
2627 * @dev: the pointer to the struct device to be renamed
2628 * @new_name: the new name of the device
2630 * It is the responsibility of the caller to provide mutual
2631 * exclusion between two different calls of device_rename
2632 * on the same device to ensure that new_name is valid and
2633 * won't conflict with other devices.
2635 * Note: Don't call this function. Currently, the networking layer calls this
2636 * function, but that will change. The following text from Kay Sievers offers
2639 * Renaming devices is racy at many levels, symlinks and other stuff are not
2640 * replaced atomically, and you get a "move" uevent, but it's not easy to
2641 * connect the event to the old and new device. Device nodes are not renamed at
2642 * all, there isn't even support for that in the kernel now.
2644 * In the meantime, during renaming, your target name might be taken by another
2645 * driver, creating conflicts. Or the old name is taken directly after you
2646 * renamed it -- then you get events for the same DEVPATH, before you even see
2647 * the "move" event. It's just a mess, and nothing new should ever rely on
2648 * kernel device renaming. Besides that, it's not even implemented now for
2649 * other things than (driver-core wise very simple) network devices.
2651 * We are currently about to change network renaming in udev to completely
2652 * disallow renaming of devices in the same namespace as the kernel uses,
2653 * because we can't solve the problems properly, that arise with swapping names
2654 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
2655 * be allowed to some other name than eth[0-9]*, for the aforementioned
2658 * Make up a "real" name in the driver before you register anything, or add
2659 * some other attributes for userspace to find the device, or use udev to add
2660 * symlinks -- but never rename kernel devices later, it's a complete mess. We
2661 * don't even want to get into that and try to implement the missing pieces in
2662 * the core. We really have other pieces to fix in the driver core mess. :)
2664 int device_rename(struct device *dev, const char *new_name)
2666 struct kobject *kobj = &dev->kobj;
2667 char *old_device_name = NULL;
2670 dev = get_device(dev);
2674 dev_dbg(dev, "renaming to %s\n", new_name);
2676 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
2677 if (!old_device_name) {
2683 error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj,
2684 kobj, old_device_name,
2685 new_name, kobject_namespace(kobj));
2690 error = kobject_rename(kobj, new_name);
2697 kfree(old_device_name);
2701 EXPORT_SYMBOL_GPL(device_rename);
2703 static int device_move_class_links(struct device *dev,
2704 struct device *old_parent,
2705 struct device *new_parent)
2710 sysfs_remove_link(&dev->kobj, "device");
2712 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
2718 * device_move - moves a device to a new parent
2719 * @dev: the pointer to the struct device to be moved
2720 * @new_parent: the new parent of the device (can be NULL)
2721 * @dpm_order: how to reorder the dpm_list
2723 int device_move(struct device *dev, struct device *new_parent,
2724 enum dpm_order dpm_order)
2727 struct device *old_parent;
2728 struct kobject *new_parent_kobj;
2730 dev = get_device(dev);
2735 new_parent = get_device(new_parent);
2736 new_parent_kobj = get_device_parent(dev, new_parent);
2737 if (IS_ERR(new_parent_kobj)) {
2738 error = PTR_ERR(new_parent_kobj);
2739 put_device(new_parent);
2743 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
2744 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
2745 error = kobject_move(&dev->kobj, new_parent_kobj);
2747 cleanup_glue_dir(dev, new_parent_kobj);
2748 put_device(new_parent);
2751 old_parent = dev->parent;
2752 dev->parent = new_parent;
2754 klist_remove(&dev->p->knode_parent);
2756 klist_add_tail(&dev->p->knode_parent,
2757 &new_parent->p->klist_children);
2758 set_dev_node(dev, dev_to_node(new_parent));
2762 error = device_move_class_links(dev, old_parent, new_parent);
2764 /* We ignore errors on cleanup since we're hosed anyway... */
2765 device_move_class_links(dev, new_parent, old_parent);
2766 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
2768 klist_remove(&dev->p->knode_parent);
2769 dev->parent = old_parent;
2771 klist_add_tail(&dev->p->knode_parent,
2772 &old_parent->p->klist_children);
2773 set_dev_node(dev, dev_to_node(old_parent));
2776 cleanup_glue_dir(dev, new_parent_kobj);
2777 put_device(new_parent);
2781 switch (dpm_order) {
2782 case DPM_ORDER_NONE:
2784 case DPM_ORDER_DEV_AFTER_PARENT:
2785 device_pm_move_after(dev, new_parent);
2786 devices_kset_move_after(dev, new_parent);
2788 case DPM_ORDER_PARENT_BEFORE_DEV:
2789 device_pm_move_before(new_parent, dev);
2790 devices_kset_move_before(new_parent, dev);
2792 case DPM_ORDER_DEV_LAST:
2793 device_pm_move_last(dev);
2794 devices_kset_move_last(dev);
2798 put_device(old_parent);
2804 EXPORT_SYMBOL_GPL(device_move);
2807 * device_shutdown - call ->shutdown() on each device to shutdown.
2809 void device_shutdown(void)
2811 struct device *dev, *parent;
2813 spin_lock(&devices_kset->list_lock);
2815 * Walk the devices list backward, shutting down each in turn.
2816 * Beware that device unplug events may also start pulling
2817 * devices offline, even as the system is shutting down.
2819 while (!list_empty(&devices_kset->list)) {
2820 dev = list_entry(devices_kset->list.prev, struct device,
2824 * hold reference count of device's parent to
2825 * prevent it from being freed because parent's
2826 * lock is to be held
2828 parent = get_device(dev->parent);
2831 * Make sure the device is off the kset list, in the
2832 * event that dev->*->shutdown() doesn't remove it.
2834 list_del_init(&dev->kobj.entry);
2835 spin_unlock(&devices_kset->list_lock);
2837 /* hold lock to avoid race with probe/release */
2839 device_lock(parent);
2842 /* Don't allow any more runtime suspends */
2843 pm_runtime_get_noresume(dev);
2844 pm_runtime_barrier(dev);
2846 if (dev->class && dev->class->shutdown_pre) {
2848 dev_info(dev, "shutdown_pre\n");
2849 dev->class->shutdown_pre(dev);
2851 if (dev->bus && dev->bus->shutdown) {
2853 dev_info(dev, "shutdown\n");
2854 dev->bus->shutdown(dev);
2855 } else if (dev->driver && dev->driver->shutdown) {
2857 dev_info(dev, "shutdown\n");
2858 dev->driver->shutdown(dev);
2863 device_unlock(parent);
2868 spin_lock(&devices_kset->list_lock);
2870 spin_unlock(&devices_kset->list_lock);
2874 * Device logging functions
2877 #ifdef CONFIG_PRINTK
2879 create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
2885 subsys = dev->class->name;
2887 subsys = dev->bus->name;
2891 pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys);
2896 * Add device identifier DEVICE=:
2900 * +sound:card0 subsystem:devname
2902 if (MAJOR(dev->devt)) {
2905 if (strcmp(subsys, "block") == 0)
2910 pos += snprintf(hdr + pos, hdrlen - pos,
2912 c, MAJOR(dev->devt), MINOR(dev->devt));
2913 } else if (strcmp(subsys, "net") == 0) {
2914 struct net_device *net = to_net_dev(dev);
2917 pos += snprintf(hdr + pos, hdrlen - pos,
2918 "DEVICE=n%u", net->ifindex);
2921 pos += snprintf(hdr + pos, hdrlen - pos,
2922 "DEVICE=+%s:%s", subsys, dev_name(dev));
2931 dev_WARN(dev, "device/subsystem name too long");
2935 int dev_vprintk_emit(int level, const struct device *dev,
2936 const char *fmt, va_list args)
2941 hdrlen = create_syslog_header(dev, hdr, sizeof(hdr));
2943 return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args);
2945 EXPORT_SYMBOL(dev_vprintk_emit);
2947 int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
2952 va_start(args, fmt);
2954 r = dev_vprintk_emit(level, dev, fmt, args);
2960 EXPORT_SYMBOL(dev_printk_emit);
2962 static void __dev_printk(const char *level, const struct device *dev,
2963 struct va_format *vaf)
2966 dev_printk_emit(level[1] - '0', dev, "%s %s: %pV",
2967 dev_driver_string(dev), dev_name(dev), vaf);
2969 printk("%s(NULL device *): %pV", level, vaf);
2972 void dev_printk(const char *level, const struct device *dev,
2973 const char *fmt, ...)
2975 struct va_format vaf;
2978 va_start(args, fmt);
2983 __dev_printk(level, dev, &vaf);
2987 EXPORT_SYMBOL(dev_printk);
2989 #define define_dev_printk_level(func, kern_level) \
2990 void func(const struct device *dev, const char *fmt, ...) \
2992 struct va_format vaf; \
2995 va_start(args, fmt); \
3000 __dev_printk(kern_level, dev, &vaf); \
3004 EXPORT_SYMBOL(func);
3006 define_dev_printk_level(dev_emerg, KERN_EMERG);
3007 define_dev_printk_level(dev_alert, KERN_ALERT);
3008 define_dev_printk_level(dev_crit, KERN_CRIT);
3009 define_dev_printk_level(dev_err, KERN_ERR);
3010 define_dev_printk_level(dev_warn, KERN_WARNING);
3011 define_dev_printk_level(dev_notice, KERN_NOTICE);
3012 define_dev_printk_level(_dev_info, KERN_INFO);
3016 static inline bool fwnode_is_primary(struct fwnode_handle *fwnode)
3018 return fwnode && !IS_ERR(fwnode->secondary);
3022 * set_primary_fwnode - Change the primary firmware node of a given device.
3023 * @dev: Device to handle.
3024 * @fwnode: New primary firmware node of the device.
3026 * Set the device's firmware node pointer to @fwnode, but if a secondary
3027 * firmware node of the device is present, preserve it.
3029 void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
3032 struct fwnode_handle *fn = dev->fwnode;
3034 if (fwnode_is_primary(fn))
3038 WARN_ON(fwnode->secondary);
3039 fwnode->secondary = fn;
3041 dev->fwnode = fwnode;
3043 dev->fwnode = fwnode_is_primary(dev->fwnode) ?
3044 dev->fwnode->secondary : NULL;
3047 EXPORT_SYMBOL_GPL(set_primary_fwnode);
3050 * set_secondary_fwnode - Change the secondary firmware node of a given device.
3051 * @dev: Device to handle.
3052 * @fwnode: New secondary firmware node of the device.
3054 * If a primary firmware node of the device is present, set its secondary
3055 * pointer to @fwnode. Otherwise, set the device's firmware node pointer to
3058 void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
3061 fwnode->secondary = ERR_PTR(-ENODEV);
3063 if (fwnode_is_primary(dev->fwnode))
3064 dev->fwnode->secondary = fwnode;
3066 dev->fwnode = fwnode;
3070 * device_set_of_node_from_dev - reuse device-tree node of another device
3071 * @dev: device whose device-tree node is being set
3072 * @dev2: device whose device-tree node is being reused
3074 * Takes another reference to the new device-tree node after first dropping
3075 * any reference held to the old node.
3077 void device_set_of_node_from_dev(struct device *dev, const struct device *dev2)
3079 of_node_put(dev->of_node);
3080 dev->of_node = of_node_get(dev2->of_node);
3081 dev->of_node_reused = true;
3083 EXPORT_SYMBOL_GPL(device_set_of_node_from_dev);