2 * drivers/base/core.c - core driver model code (device registration, etc)
4 * Copyright (c) 2002-3 Patrick Mochel
5 * Copyright (c) 2002-3 Open Source Development Labs
6 * Copyright (c) 2006 Greg Kroah-Hartman <gregkh@suse.de>
7 * Copyright (c) 2006 Novell, Inc.
9 * This file is released under the GPLv2
13 #include <linux/device.h>
14 #include <linux/err.h>
15 #include <linux/fwnode.h>
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/string.h>
20 #include <linux/kdev_t.h>
21 #include <linux/notifier.h>
23 #include <linux/of_device.h>
24 #include <linux/genhd.h>
25 #include <linux/kallsyms.h>
26 #include <linux/mutex.h>
27 #include <linux/pm_runtime.h>
28 #include <linux/netdevice.h>
29 #include <linux/sched/signal.h>
30 #include <linux/sysfs.h>
33 #include "power/power.h"
35 #ifdef CONFIG_SYSFS_DEPRECATED
36 #ifdef CONFIG_SYSFS_DEPRECATED_V2
37 long sysfs_deprecated = 1;
39 long sysfs_deprecated = 0;
41 static int __init sysfs_deprecated_setup(char *arg)
43 return kstrtol(arg, 10, &sysfs_deprecated);
45 early_param("sysfs.deprecated", sysfs_deprecated_setup);
48 /* Device links support. */
51 static DEFINE_MUTEX(device_links_lock);
52 DEFINE_STATIC_SRCU(device_links_srcu);
54 static inline void device_links_write_lock(void)
56 mutex_lock(&device_links_lock);
59 static inline void device_links_write_unlock(void)
61 mutex_unlock(&device_links_lock);
64 int device_links_read_lock(void)
66 return srcu_read_lock(&device_links_srcu);
69 void device_links_read_unlock(int idx)
71 srcu_read_unlock(&device_links_srcu, idx);
73 #else /* !CONFIG_SRCU */
74 static DECLARE_RWSEM(device_links_lock);
76 static inline void device_links_write_lock(void)
78 down_write(&device_links_lock);
81 static inline void device_links_write_unlock(void)
83 up_write(&device_links_lock);
86 int device_links_read_lock(void)
88 down_read(&device_links_lock);
92 void device_links_read_unlock(int not_used)
94 up_read(&device_links_lock);
96 #endif /* !CONFIG_SRCU */
99 * device_is_dependent - Check if one device depends on another one
100 * @dev: Device to check dependencies for.
101 * @target: Device to check against.
103 * Check if @target depends on @dev or any device dependent on it (its child or
104 * its consumer etc). Return 1 if that is the case or 0 otherwise.
106 static int device_is_dependent(struct device *dev, void *target)
108 struct device_link *link;
111 if (WARN_ON(dev == target))
114 ret = device_for_each_child(dev, target, device_is_dependent);
118 list_for_each_entry(link, &dev->links.consumers, s_node) {
119 if (WARN_ON(link->consumer == target))
122 ret = device_is_dependent(link->consumer, target);
129 static int device_reorder_to_tail(struct device *dev, void *not_used)
131 struct device_link *link;
134 * Devices that have not been registered yet will be put to the ends
135 * of the lists during the registration, so skip them here.
137 if (device_is_registered(dev))
138 devices_kset_move_last(dev);
140 if (device_pm_initialized(dev))
141 device_pm_move_last(dev);
143 device_for_each_child(dev, NULL, device_reorder_to_tail);
144 list_for_each_entry(link, &dev->links.consumers, s_node)
145 device_reorder_to_tail(link->consumer, NULL);
151 * device_link_add - Create a link between two devices.
152 * @consumer: Consumer end of the link.
153 * @supplier: Supplier end of the link.
154 * @flags: Link flags.
156 * The caller is responsible for the proper synchronization of the link creation
157 * with runtime PM. First, setting the DL_FLAG_PM_RUNTIME flag will cause the
158 * runtime PM framework to take the link into account. Second, if the
159 * DL_FLAG_RPM_ACTIVE flag is set in addition to it, the supplier devices will
160 * be forced into the active metastate and reference-counted upon the creation
161 * of the link. If DL_FLAG_PM_RUNTIME is not set, DL_FLAG_RPM_ACTIVE will be
164 * If the DL_FLAG_AUTOREMOVE is set, the link will be removed automatically
165 * when the consumer device driver unbinds from it. The combination of both
166 * DL_FLAG_AUTOREMOVE and DL_FLAG_STATELESS set is invalid and will cause NULL
169 * A side effect of the link creation is re-ordering of dpm_list and the
170 * devices_kset list by moving the consumer device and all devices depending
171 * on it to the ends of these lists (that does not happen to devices that have
172 * not been registered when this function is called).
174 * The supplier device is required to be registered when this function is called
175 * and NULL will be returned if that is not the case. The consumer device need
176 * not be registered, however.
178 struct device_link *device_link_add(struct device *consumer,
179 struct device *supplier, u32 flags)
181 struct device_link *link;
183 if (!consumer || !supplier ||
184 ((flags & DL_FLAG_STATELESS) && (flags & DL_FLAG_AUTOREMOVE)))
187 device_links_write_lock();
191 * If the supplier has not been fully registered yet or there is a
192 * reverse dependency between the consumer and the supplier already in
193 * the graph, return NULL.
195 if (!device_pm_initialized(supplier)
196 || device_is_dependent(consumer, supplier)) {
201 list_for_each_entry(link, &supplier->links.consumers, s_node)
202 if (link->consumer == consumer)
205 link = kzalloc(sizeof(*link), GFP_KERNEL);
209 if (flags & DL_FLAG_PM_RUNTIME) {
210 if (flags & DL_FLAG_RPM_ACTIVE) {
211 if (pm_runtime_get_sync(supplier) < 0) {
212 pm_runtime_put_noidle(supplier);
217 link->rpm_active = true;
219 pm_runtime_new_link(consumer);
221 get_device(supplier);
222 link->supplier = supplier;
223 INIT_LIST_HEAD(&link->s_node);
224 get_device(consumer);
225 link->consumer = consumer;
226 INIT_LIST_HEAD(&link->c_node);
229 /* Determine the initial link state. */
230 if (flags & DL_FLAG_STATELESS) {
231 link->status = DL_STATE_NONE;
233 switch (supplier->links.status) {
234 case DL_DEV_DRIVER_BOUND:
235 switch (consumer->links.status) {
238 * Balance the decrementation of the supplier's
239 * runtime PM usage counter after consumer probe
240 * in driver_probe_device().
242 if (flags & DL_FLAG_PM_RUNTIME)
243 pm_runtime_get_sync(supplier);
245 link->status = DL_STATE_CONSUMER_PROBE;
247 case DL_DEV_DRIVER_BOUND:
248 link->status = DL_STATE_ACTIVE;
251 link->status = DL_STATE_AVAILABLE;
255 case DL_DEV_UNBINDING:
256 link->status = DL_STATE_SUPPLIER_UNBIND;
259 link->status = DL_STATE_DORMANT;
265 * Move the consumer and all of the devices depending on it to the end
266 * of dpm_list and the devices_kset list.
268 * It is necessary to hold dpm_list locked throughout all that or else
269 * we may end up suspending with a wrong ordering of it.
271 device_reorder_to_tail(consumer, NULL);
273 list_add_tail_rcu(&link->s_node, &supplier->links.consumers);
274 list_add_tail_rcu(&link->c_node, &consumer->links.suppliers);
276 dev_info(consumer, "Linked as a consumer to %s\n", dev_name(supplier));
280 device_links_write_unlock();
283 EXPORT_SYMBOL_GPL(device_link_add);
285 static void device_link_free(struct device_link *link)
287 put_device(link->consumer);
288 put_device(link->supplier);
293 static void __device_link_free_srcu(struct rcu_head *rhead)
295 device_link_free(container_of(rhead, struct device_link, rcu_head));
298 static void __device_link_del(struct device_link *link)
300 dev_info(link->consumer, "Dropping the link to %s\n",
301 dev_name(link->supplier));
303 if (link->flags & DL_FLAG_PM_RUNTIME)
304 pm_runtime_drop_link(link->consumer);
306 list_del_rcu(&link->s_node);
307 list_del_rcu(&link->c_node);
308 call_srcu(&device_links_srcu, &link->rcu_head, __device_link_free_srcu);
310 #else /* !CONFIG_SRCU */
311 static void __device_link_del(struct device_link *link)
313 dev_info(link->consumer, "Dropping the link to %s\n",
314 dev_name(link->supplier));
316 if (link->flags & DL_FLAG_PM_RUNTIME)
317 pm_runtime_drop_link(link->consumer);
319 list_del(&link->s_node);
320 list_del(&link->c_node);
321 device_link_free(link);
323 #endif /* !CONFIG_SRCU */
326 * device_link_del - Delete a link between two devices.
327 * @link: Device link to delete.
329 * The caller must ensure proper synchronization of this function with runtime
332 void device_link_del(struct device_link *link)
334 device_links_write_lock();
336 __device_link_del(link);
338 device_links_write_unlock();
340 EXPORT_SYMBOL_GPL(device_link_del);
342 static void device_links_missing_supplier(struct device *dev)
344 struct device_link *link;
346 list_for_each_entry(link, &dev->links.suppliers, c_node)
347 if (link->status == DL_STATE_CONSUMER_PROBE)
348 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
352 * device_links_check_suppliers - Check presence of supplier drivers.
353 * @dev: Consumer device.
355 * Check links from this device to any suppliers. Walk the list of the device's
356 * links to suppliers and see if all of them are available. If not, simply
357 * return -EPROBE_DEFER.
359 * We need to guarantee that the supplier will not go away after the check has
360 * been positive here. It only can go away in __device_release_driver() and
361 * that function checks the device's links to consumers. This means we need to
362 * mark the link as "consumer probe in progress" to make the supplier removal
363 * wait for us to complete (or bad things may happen).
365 * Links with the DL_FLAG_STATELESS flag set are ignored.
367 int device_links_check_suppliers(struct device *dev)
369 struct device_link *link;
372 device_links_write_lock();
374 list_for_each_entry(link, &dev->links.suppliers, c_node) {
375 if (link->flags & DL_FLAG_STATELESS)
378 if (link->status != DL_STATE_AVAILABLE) {
379 device_links_missing_supplier(dev);
383 WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE);
385 dev->links.status = DL_DEV_PROBING;
387 device_links_write_unlock();
392 * device_links_driver_bound - Update device links after probing its driver.
393 * @dev: Device to update the links for.
395 * The probe has been successful, so update links from this device to any
396 * consumers by changing their status to "available".
398 * Also change the status of @dev's links to suppliers to "active".
400 * Links with the DL_FLAG_STATELESS flag set are ignored.
402 void device_links_driver_bound(struct device *dev)
404 struct device_link *link;
406 device_links_write_lock();
408 list_for_each_entry(link, &dev->links.consumers, s_node) {
409 if (link->flags & DL_FLAG_STATELESS)
412 WARN_ON(link->status != DL_STATE_DORMANT);
413 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
416 list_for_each_entry(link, &dev->links.suppliers, c_node) {
417 if (link->flags & DL_FLAG_STATELESS)
420 WARN_ON(link->status != DL_STATE_CONSUMER_PROBE);
421 WRITE_ONCE(link->status, DL_STATE_ACTIVE);
424 dev->links.status = DL_DEV_DRIVER_BOUND;
426 device_links_write_unlock();
430 * __device_links_no_driver - Update links of a device without a driver.
431 * @dev: Device without a drvier.
433 * Delete all non-persistent links from this device to any suppliers.
435 * Persistent links stay around, but their status is changed to "available",
436 * unless they already are in the "supplier unbind in progress" state in which
437 * case they need not be updated.
439 * Links with the DL_FLAG_STATELESS flag set are ignored.
441 static void __device_links_no_driver(struct device *dev)
443 struct device_link *link, *ln;
445 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
446 if (link->flags & DL_FLAG_STATELESS)
449 if (link->flags & DL_FLAG_AUTOREMOVE)
450 __device_link_del(link);
451 else if (link->status != DL_STATE_SUPPLIER_UNBIND)
452 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
455 dev->links.status = DL_DEV_NO_DRIVER;
458 void device_links_no_driver(struct device *dev)
460 device_links_write_lock();
461 __device_links_no_driver(dev);
462 device_links_write_unlock();
466 * device_links_driver_cleanup - Update links after driver removal.
467 * @dev: Device whose driver has just gone away.
469 * Update links to consumers for @dev by changing their status to "dormant" and
470 * invoke %__device_links_no_driver() to update links to suppliers for it as
473 * Links with the DL_FLAG_STATELESS flag set are ignored.
475 void device_links_driver_cleanup(struct device *dev)
477 struct device_link *link;
479 device_links_write_lock();
481 list_for_each_entry(link, &dev->links.consumers, s_node) {
482 if (link->flags & DL_FLAG_STATELESS)
485 WARN_ON(link->flags & DL_FLAG_AUTOREMOVE);
486 WARN_ON(link->status != DL_STATE_SUPPLIER_UNBIND);
487 WRITE_ONCE(link->status, DL_STATE_DORMANT);
490 __device_links_no_driver(dev);
492 device_links_write_unlock();
496 * device_links_busy - Check if there are any busy links to consumers.
497 * @dev: Device to check.
499 * Check each consumer of the device and return 'true' if its link's status
500 * is one of "consumer probe" or "active" (meaning that the given consumer is
501 * probing right now or its driver is present). Otherwise, change the link
502 * state to "supplier unbind" to prevent the consumer from being probed
503 * successfully going forward.
505 * Return 'false' if there are no probing or active consumers.
507 * Links with the DL_FLAG_STATELESS flag set are ignored.
509 bool device_links_busy(struct device *dev)
511 struct device_link *link;
514 device_links_write_lock();
516 list_for_each_entry(link, &dev->links.consumers, s_node) {
517 if (link->flags & DL_FLAG_STATELESS)
520 if (link->status == DL_STATE_CONSUMER_PROBE
521 || link->status == DL_STATE_ACTIVE) {
525 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
528 dev->links.status = DL_DEV_UNBINDING;
530 device_links_write_unlock();
535 * device_links_unbind_consumers - Force unbind consumers of the given device.
536 * @dev: Device to unbind the consumers of.
538 * Walk the list of links to consumers for @dev and if any of them is in the
539 * "consumer probe" state, wait for all device probes in progress to complete
542 * If that's not the case, change the status of the link to "supplier unbind"
543 * and check if the link was in the "active" state. If so, force the consumer
544 * driver to unbind and start over (the consumer will not re-probe as we have
545 * changed the state of the link already).
547 * Links with the DL_FLAG_STATELESS flag set are ignored.
549 void device_links_unbind_consumers(struct device *dev)
551 struct device_link *link;
554 device_links_write_lock();
556 list_for_each_entry(link, &dev->links.consumers, s_node) {
557 enum device_link_state status;
559 if (link->flags & DL_FLAG_STATELESS)
562 status = link->status;
563 if (status == DL_STATE_CONSUMER_PROBE) {
564 device_links_write_unlock();
566 wait_for_device_probe();
569 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
570 if (status == DL_STATE_ACTIVE) {
571 struct device *consumer = link->consumer;
573 get_device(consumer);
575 device_links_write_unlock();
577 device_release_driver_internal(consumer, NULL,
579 put_device(consumer);
584 device_links_write_unlock();
588 * device_links_purge - Delete existing links to other devices.
589 * @dev: Target device.
591 static void device_links_purge(struct device *dev)
593 struct device_link *link, *ln;
596 * Delete all of the remaining links from this device to any other
597 * devices (either consumers or suppliers).
599 device_links_write_lock();
601 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
602 WARN_ON(link->status == DL_STATE_ACTIVE);
603 __device_link_del(link);
606 list_for_each_entry_safe_reverse(link, ln, &dev->links.consumers, s_node) {
607 WARN_ON(link->status != DL_STATE_DORMANT &&
608 link->status != DL_STATE_NONE);
609 __device_link_del(link);
612 device_links_write_unlock();
615 /* Device links support end. */
617 int (*platform_notify)(struct device *dev) = NULL;
618 int (*platform_notify_remove)(struct device *dev) = NULL;
619 static struct kobject *dev_kobj;
620 struct kobject *sysfs_dev_char_kobj;
621 struct kobject *sysfs_dev_block_kobj;
623 static DEFINE_MUTEX(device_hotplug_lock);
625 void lock_device_hotplug(void)
627 mutex_lock(&device_hotplug_lock);
630 void unlock_device_hotplug(void)
632 mutex_unlock(&device_hotplug_lock);
635 int lock_device_hotplug_sysfs(void)
637 if (mutex_trylock(&device_hotplug_lock))
640 /* Avoid busy looping (5 ms of sleep should do). */
642 return restart_syscall();
646 static inline int device_is_not_partition(struct device *dev)
648 return !(dev->type == &part_type);
651 static inline int device_is_not_partition(struct device *dev)
658 * dev_driver_string - Return a device's driver name, if at all possible
659 * @dev: struct device to get the name of
661 * Will return the device's driver's name if it is bound to a device. If
662 * the device is not bound to a driver, it will return the name of the bus
663 * it is attached to. If it is not attached to a bus either, an empty
664 * string will be returned.
666 const char *dev_driver_string(const struct device *dev)
668 struct device_driver *drv;
670 /* dev->driver can change to NULL underneath us because of unbinding,
671 * so be careful about accessing it. dev->bus and dev->class should
672 * never change once they are set, so they don't need special care.
674 drv = ACCESS_ONCE(dev->driver);
675 return drv ? drv->name :
676 (dev->bus ? dev->bus->name :
677 (dev->class ? dev->class->name : ""));
679 EXPORT_SYMBOL(dev_driver_string);
681 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
683 static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
686 struct device_attribute *dev_attr = to_dev_attr(attr);
687 struct device *dev = kobj_to_dev(kobj);
691 ret = dev_attr->show(dev, dev_attr, buf);
692 if (ret >= (ssize_t)PAGE_SIZE) {
693 print_symbol("dev_attr_show: %s returned bad count\n",
694 (unsigned long)dev_attr->show);
699 static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
700 const char *buf, size_t count)
702 struct device_attribute *dev_attr = to_dev_attr(attr);
703 struct device *dev = kobj_to_dev(kobj);
707 ret = dev_attr->store(dev, dev_attr, buf, count);
711 static const struct sysfs_ops dev_sysfs_ops = {
712 .show = dev_attr_show,
713 .store = dev_attr_store,
716 #define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
718 ssize_t device_store_ulong(struct device *dev,
719 struct device_attribute *attr,
720 const char *buf, size_t size)
722 struct dev_ext_attribute *ea = to_ext_attr(attr);
724 unsigned long new = simple_strtoul(buf, &end, 0);
727 *(unsigned long *)(ea->var) = new;
728 /* Always return full write size even if we didn't consume all */
731 EXPORT_SYMBOL_GPL(device_store_ulong);
733 ssize_t device_show_ulong(struct device *dev,
734 struct device_attribute *attr,
737 struct dev_ext_attribute *ea = to_ext_attr(attr);
738 return snprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var));
740 EXPORT_SYMBOL_GPL(device_show_ulong);
742 ssize_t device_store_int(struct device *dev,
743 struct device_attribute *attr,
744 const char *buf, size_t size)
746 struct dev_ext_attribute *ea = to_ext_attr(attr);
748 long new = simple_strtol(buf, &end, 0);
749 if (end == buf || new > INT_MAX || new < INT_MIN)
751 *(int *)(ea->var) = new;
752 /* Always return full write size even if we didn't consume all */
755 EXPORT_SYMBOL_GPL(device_store_int);
757 ssize_t device_show_int(struct device *dev,
758 struct device_attribute *attr,
761 struct dev_ext_attribute *ea = to_ext_attr(attr);
763 return snprintf(buf, PAGE_SIZE, "%d\n", *(int *)(ea->var));
765 EXPORT_SYMBOL_GPL(device_show_int);
767 ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
768 const char *buf, size_t size)
770 struct dev_ext_attribute *ea = to_ext_attr(attr);
772 if (strtobool(buf, ea->var) < 0)
777 EXPORT_SYMBOL_GPL(device_store_bool);
779 ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
782 struct dev_ext_attribute *ea = to_ext_attr(attr);
784 return snprintf(buf, PAGE_SIZE, "%d\n", *(bool *)(ea->var));
786 EXPORT_SYMBOL_GPL(device_show_bool);
789 * device_release - free device structure.
790 * @kobj: device's kobject.
792 * This is called once the reference count for the object
793 * reaches 0. We forward the call to the device's release
794 * method, which should handle actually freeing the structure.
796 static void device_release(struct kobject *kobj)
798 struct device *dev = kobj_to_dev(kobj);
799 struct device_private *p = dev->p;
802 * Some platform devices are driven without driver attached
803 * and managed resources may have been acquired. Make sure
804 * all resources are released.
806 * Drivers still can add resources into device after device
807 * is deleted but alive, so release devres here to avoid
808 * possible memory leak.
810 devres_release_all(dev);
814 else if (dev->type && dev->type->release)
815 dev->type->release(dev);
816 else if (dev->class && dev->class->dev_release)
817 dev->class->dev_release(dev);
819 WARN(1, KERN_ERR "Device '%s' does not have a release() "
820 "function, it is broken and must be fixed.\n",
825 static const void *device_namespace(struct kobject *kobj)
827 struct device *dev = kobj_to_dev(kobj);
828 const void *ns = NULL;
830 if (dev->class && dev->class->ns_type)
831 ns = dev->class->namespace(dev);
836 static struct kobj_type device_ktype = {
837 .release = device_release,
838 .sysfs_ops = &dev_sysfs_ops,
839 .namespace = device_namespace,
843 static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
845 struct kobj_type *ktype = get_ktype(kobj);
847 if (ktype == &device_ktype) {
848 struct device *dev = kobj_to_dev(kobj);
857 static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
859 struct device *dev = kobj_to_dev(kobj);
862 return dev->bus->name;
864 return dev->class->name;
868 static int dev_uevent(struct kset *kset, struct kobject *kobj,
869 struct kobj_uevent_env *env)
871 struct device *dev = kobj_to_dev(kobj);
874 /* add device node properties if present */
875 if (MAJOR(dev->devt)) {
879 kuid_t uid = GLOBAL_ROOT_UID;
880 kgid_t gid = GLOBAL_ROOT_GID;
882 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
883 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
884 name = device_get_devnode(dev, &mode, &uid, &gid, &tmp);
886 add_uevent_var(env, "DEVNAME=%s", name);
888 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
889 if (!uid_eq(uid, GLOBAL_ROOT_UID))
890 add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid));
891 if (!gid_eq(gid, GLOBAL_ROOT_GID))
892 add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid));
897 if (dev->type && dev->type->name)
898 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
901 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
903 /* Add common DT information about the device */
904 of_device_uevent(dev, env);
906 /* have the bus specific function add its stuff */
907 if (dev->bus && dev->bus->uevent) {
908 retval = dev->bus->uevent(dev, env);
910 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
911 dev_name(dev), __func__, retval);
914 /* have the class specific function add its stuff */
915 if (dev->class && dev->class->dev_uevent) {
916 retval = dev->class->dev_uevent(dev, env);
918 pr_debug("device: '%s': %s: class uevent() "
919 "returned %d\n", dev_name(dev),
923 /* have the device type specific function add its stuff */
924 if (dev->type && dev->type->uevent) {
925 retval = dev->type->uevent(dev, env);
927 pr_debug("device: '%s': %s: dev_type uevent() "
928 "returned %d\n", dev_name(dev),
935 static const struct kset_uevent_ops device_uevent_ops = {
936 .filter = dev_uevent_filter,
937 .name = dev_uevent_name,
938 .uevent = dev_uevent,
941 static ssize_t uevent_show(struct device *dev, struct device_attribute *attr,
944 struct kobject *top_kobj;
946 struct kobj_uevent_env *env = NULL;
951 /* search the kset, the device belongs to */
952 top_kobj = &dev->kobj;
953 while (!top_kobj->kset && top_kobj->parent)
954 top_kobj = top_kobj->parent;
958 kset = top_kobj->kset;
959 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
963 if (kset->uevent_ops && kset->uevent_ops->filter)
964 if (!kset->uevent_ops->filter(kset, &dev->kobj))
967 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
971 /* let the kset specific function add its keys */
972 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
976 /* copy keys to file */
977 for (i = 0; i < env->envp_idx; i++)
978 count += sprintf(&buf[count], "%s\n", env->envp[i]);
984 static ssize_t uevent_store(struct device *dev, struct device_attribute *attr,
985 const char *buf, size_t count)
987 if (kobject_synth_uevent(&dev->kobj, buf, count))
988 dev_err(dev, "uevent: failed to send synthetic uevent\n");
992 static DEVICE_ATTR_RW(uevent);
994 static ssize_t online_show(struct device *dev, struct device_attribute *attr,
1000 val = !dev->offline;
1002 return sprintf(buf, "%u\n", val);
1005 static ssize_t online_store(struct device *dev, struct device_attribute *attr,
1006 const char *buf, size_t count)
1011 ret = strtobool(buf, &val);
1015 ret = lock_device_hotplug_sysfs();
1019 ret = val ? device_online(dev) : device_offline(dev);
1020 unlock_device_hotplug();
1021 return ret < 0 ? ret : count;
1023 static DEVICE_ATTR_RW(online);
1025 int device_add_groups(struct device *dev, const struct attribute_group **groups)
1027 return sysfs_create_groups(&dev->kobj, groups);
1029 EXPORT_SYMBOL_GPL(device_add_groups);
1031 void device_remove_groups(struct device *dev,
1032 const struct attribute_group **groups)
1034 sysfs_remove_groups(&dev->kobj, groups);
1036 EXPORT_SYMBOL_GPL(device_remove_groups);
1038 union device_attr_group_devres {
1039 const struct attribute_group *group;
1040 const struct attribute_group **groups;
1043 static int devm_attr_group_match(struct device *dev, void *res, void *data)
1045 return ((union device_attr_group_devres *)res)->group == data;
1048 static void devm_attr_group_remove(struct device *dev, void *res)
1050 union device_attr_group_devres *devres = res;
1051 const struct attribute_group *group = devres->group;
1053 dev_dbg(dev, "%s: removing group %p\n", __func__, group);
1054 sysfs_remove_group(&dev->kobj, group);
1057 static void devm_attr_groups_remove(struct device *dev, void *res)
1059 union device_attr_group_devres *devres = res;
1060 const struct attribute_group **groups = devres->groups;
1062 dev_dbg(dev, "%s: removing groups %p\n", __func__, groups);
1063 sysfs_remove_groups(&dev->kobj, groups);
1067 * devm_device_add_group - given a device, create a managed attribute group
1068 * @dev: The device to create the group for
1069 * @grp: The attribute group to create
1071 * This function creates a group for the first time. It will explicitly
1072 * warn and error if any of the attribute files being created already exist.
1074 * Returns 0 on success or error code on failure.
1076 int devm_device_add_group(struct device *dev, const struct attribute_group *grp)
1078 union device_attr_group_devres *devres;
1081 devres = devres_alloc(devm_attr_group_remove,
1082 sizeof(*devres), GFP_KERNEL);
1086 error = sysfs_create_group(&dev->kobj, grp);
1088 devres_free(devres);
1092 devres->group = grp;
1093 devres_add(dev, devres);
1096 EXPORT_SYMBOL_GPL(devm_device_add_group);
1099 * devm_device_remove_group: remove a managed group from a device
1100 * @dev: device to remove the group from
1101 * @grp: group to remove
1103 * This function removes a group of attributes from a device. The attributes
1104 * previously have to have been created for this group, otherwise it will fail.
1106 void devm_device_remove_group(struct device *dev,
1107 const struct attribute_group *grp)
1109 WARN_ON(devres_release(dev, devm_attr_group_remove,
1110 devm_attr_group_match,
1111 /* cast away const */ (void *)grp));
1113 EXPORT_SYMBOL_GPL(devm_device_remove_group);
1116 * devm_device_add_groups - create a bunch of managed attribute groups
1117 * @dev: The device to create the group for
1118 * @groups: The attribute groups to create, NULL terminated
1120 * This function creates a bunch of managed attribute groups. If an error
1121 * occurs when creating a group, all previously created groups will be
1122 * removed, unwinding everything back to the original state when this
1123 * function was called. It will explicitly warn and error if any of the
1124 * attribute files being created already exist.
1126 * Returns 0 on success or error code from sysfs_create_group on failure.
1128 int devm_device_add_groups(struct device *dev,
1129 const struct attribute_group **groups)
1131 union device_attr_group_devres *devres;
1134 devres = devres_alloc(devm_attr_groups_remove,
1135 sizeof(*devres), GFP_KERNEL);
1139 error = sysfs_create_groups(&dev->kobj, groups);
1141 devres_free(devres);
1145 devres->groups = groups;
1146 devres_add(dev, devres);
1149 EXPORT_SYMBOL_GPL(devm_device_add_groups);
1152 * devm_device_remove_groups - remove a list of managed groups
1154 * @dev: The device for the groups to be removed from
1155 * @groups: NULL terminated list of groups to be removed
1157 * If groups is not NULL, remove the specified groups from the device.
1159 void devm_device_remove_groups(struct device *dev,
1160 const struct attribute_group **groups)
1162 WARN_ON(devres_release(dev, devm_attr_groups_remove,
1163 devm_attr_group_match,
1164 /* cast away const */ (void *)groups));
1166 EXPORT_SYMBOL_GPL(devm_device_remove_groups);
1168 static int device_add_attrs(struct device *dev)
1170 struct class *class = dev->class;
1171 const struct device_type *type = dev->type;
1175 error = device_add_groups(dev, class->dev_groups);
1181 error = device_add_groups(dev, type->groups);
1183 goto err_remove_class_groups;
1186 error = device_add_groups(dev, dev->groups);
1188 goto err_remove_type_groups;
1190 if (device_supports_offline(dev) && !dev->offline_disabled) {
1191 error = device_create_file(dev, &dev_attr_online);
1193 goto err_remove_dev_groups;
1198 err_remove_dev_groups:
1199 device_remove_groups(dev, dev->groups);
1200 err_remove_type_groups:
1202 device_remove_groups(dev, type->groups);
1203 err_remove_class_groups:
1205 device_remove_groups(dev, class->dev_groups);
1210 static void device_remove_attrs(struct device *dev)
1212 struct class *class = dev->class;
1213 const struct device_type *type = dev->type;
1215 device_remove_file(dev, &dev_attr_online);
1216 device_remove_groups(dev, dev->groups);
1219 device_remove_groups(dev, type->groups);
1222 device_remove_groups(dev, class->dev_groups);
1225 static ssize_t dev_show(struct device *dev, struct device_attribute *attr,
1228 return print_dev_t(buf, dev->devt);
1230 static DEVICE_ATTR_RO(dev);
1233 struct kset *devices_kset;
1236 * devices_kset_move_before - Move device in the devices_kset's list.
1237 * @deva: Device to move.
1238 * @devb: Device @deva should come before.
1240 static void devices_kset_move_before(struct device *deva, struct device *devb)
1244 pr_debug("devices_kset: Moving %s before %s\n",
1245 dev_name(deva), dev_name(devb));
1246 spin_lock(&devices_kset->list_lock);
1247 list_move_tail(&deva->kobj.entry, &devb->kobj.entry);
1248 spin_unlock(&devices_kset->list_lock);
1252 * devices_kset_move_after - Move device in the devices_kset's list.
1253 * @deva: Device to move
1254 * @devb: Device @deva should come after.
1256 static void devices_kset_move_after(struct device *deva, struct device *devb)
1260 pr_debug("devices_kset: Moving %s after %s\n",
1261 dev_name(deva), dev_name(devb));
1262 spin_lock(&devices_kset->list_lock);
1263 list_move(&deva->kobj.entry, &devb->kobj.entry);
1264 spin_unlock(&devices_kset->list_lock);
1268 * devices_kset_move_last - move the device to the end of devices_kset's list.
1269 * @dev: device to move
1271 void devices_kset_move_last(struct device *dev)
1275 pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev));
1276 spin_lock(&devices_kset->list_lock);
1277 list_move_tail(&dev->kobj.entry, &devices_kset->list);
1278 spin_unlock(&devices_kset->list_lock);
1282 * device_create_file - create sysfs attribute file for device.
1284 * @attr: device attribute descriptor.
1286 int device_create_file(struct device *dev,
1287 const struct device_attribute *attr)
1292 WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
1293 "Attribute %s: write permission without 'store'\n",
1295 WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
1296 "Attribute %s: read permission without 'show'\n",
1298 error = sysfs_create_file(&dev->kobj, &attr->attr);
1303 EXPORT_SYMBOL_GPL(device_create_file);
1306 * device_remove_file - remove sysfs attribute file.
1308 * @attr: device attribute descriptor.
1310 void device_remove_file(struct device *dev,
1311 const struct device_attribute *attr)
1314 sysfs_remove_file(&dev->kobj, &attr->attr);
1316 EXPORT_SYMBOL_GPL(device_remove_file);
1319 * device_remove_file_self - remove sysfs attribute file from its own method.
1321 * @attr: device attribute descriptor.
1323 * See kernfs_remove_self() for details.
1325 bool device_remove_file_self(struct device *dev,
1326 const struct device_attribute *attr)
1329 return sysfs_remove_file_self(&dev->kobj, &attr->attr);
1333 EXPORT_SYMBOL_GPL(device_remove_file_self);
1336 * device_create_bin_file - create sysfs binary attribute file for device.
1338 * @attr: device binary attribute descriptor.
1340 int device_create_bin_file(struct device *dev,
1341 const struct bin_attribute *attr)
1343 int error = -EINVAL;
1345 error = sysfs_create_bin_file(&dev->kobj, attr);
1348 EXPORT_SYMBOL_GPL(device_create_bin_file);
1351 * device_remove_bin_file - remove sysfs binary attribute file
1353 * @attr: device binary attribute descriptor.
1355 void device_remove_bin_file(struct device *dev,
1356 const struct bin_attribute *attr)
1359 sysfs_remove_bin_file(&dev->kobj, attr);
1361 EXPORT_SYMBOL_GPL(device_remove_bin_file);
1363 static void klist_children_get(struct klist_node *n)
1365 struct device_private *p = to_device_private_parent(n);
1366 struct device *dev = p->device;
1371 static void klist_children_put(struct klist_node *n)
1373 struct device_private *p = to_device_private_parent(n);
1374 struct device *dev = p->device;
1380 * device_initialize - init device structure.
1383 * This prepares the device for use by other layers by initializing
1385 * It is the first half of device_register(), if called by
1386 * that function, though it can also be called separately, so one
1387 * may use @dev's fields. In particular, get_device()/put_device()
1388 * may be used for reference counting of @dev after calling this
1391 * All fields in @dev must be initialized by the caller to 0, except
1392 * for those explicitly set to some other value. The simplest
1393 * approach is to use kzalloc() to allocate the structure containing
1396 * NOTE: Use put_device() to give up your reference instead of freeing
1397 * @dev directly once you have called this function.
1399 void device_initialize(struct device *dev)
1401 dev->kobj.kset = devices_kset;
1402 kobject_init(&dev->kobj, &device_ktype);
1403 INIT_LIST_HEAD(&dev->dma_pools);
1404 mutex_init(&dev->mutex);
1405 lockdep_set_novalidate_class(&dev->mutex);
1406 spin_lock_init(&dev->devres_lock);
1407 INIT_LIST_HEAD(&dev->devres_head);
1408 device_pm_init(dev);
1409 set_dev_node(dev, -1);
1410 #ifdef CONFIG_GENERIC_MSI_IRQ
1411 INIT_LIST_HEAD(&dev->msi_list);
1413 INIT_LIST_HEAD(&dev->links.consumers);
1414 INIT_LIST_HEAD(&dev->links.suppliers);
1415 dev->links.status = DL_DEV_NO_DRIVER;
1417 EXPORT_SYMBOL_GPL(device_initialize);
1419 struct kobject *virtual_device_parent(struct device *dev)
1421 static struct kobject *virtual_dir = NULL;
1424 virtual_dir = kobject_create_and_add("virtual",
1425 &devices_kset->kobj);
1431 struct kobject kobj;
1432 struct class *class;
1435 #define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
1437 static void class_dir_release(struct kobject *kobj)
1439 struct class_dir *dir = to_class_dir(kobj);
1444 struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
1446 struct class_dir *dir = to_class_dir(kobj);
1447 return dir->class->ns_type;
1450 static struct kobj_type class_dir_ktype = {
1451 .release = class_dir_release,
1452 .sysfs_ops = &kobj_sysfs_ops,
1453 .child_ns_type = class_dir_child_ns_type
1456 static struct kobject *
1457 class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
1459 struct class_dir *dir;
1462 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1464 return ERR_PTR(-ENOMEM);
1467 kobject_init(&dir->kobj, &class_dir_ktype);
1469 dir->kobj.kset = &class->p->glue_dirs;
1471 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
1473 kobject_put(&dir->kobj);
1474 return ERR_PTR(retval);
1479 static DEFINE_MUTEX(gdp_mutex);
1481 static struct kobject *get_device_parent(struct device *dev,
1482 struct device *parent)
1485 struct kobject *kobj = NULL;
1486 struct kobject *parent_kobj;
1490 /* block disks show up in /sys/block */
1491 if (sysfs_deprecated && dev->class == &block_class) {
1492 if (parent && parent->class == &block_class)
1493 return &parent->kobj;
1494 return &block_class.p->subsys.kobj;
1499 * If we have no parent, we live in "virtual".
1500 * Class-devices with a non class-device as parent, live
1501 * in a "glue" directory to prevent namespace collisions.
1504 parent_kobj = virtual_device_parent(dev);
1505 else if (parent->class && !dev->class->ns_type)
1506 return &parent->kobj;
1508 parent_kobj = &parent->kobj;
1510 mutex_lock(&gdp_mutex);
1512 /* find our class-directory at the parent and reference it */
1513 spin_lock(&dev->class->p->glue_dirs.list_lock);
1514 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
1515 if (k->parent == parent_kobj) {
1516 kobj = kobject_get(k);
1519 spin_unlock(&dev->class->p->glue_dirs.list_lock);
1521 mutex_unlock(&gdp_mutex);
1525 /* or create a new class-directory at the parent device */
1526 k = class_dir_create_and_add(dev->class, parent_kobj);
1527 /* do not emit an uevent for this simple "glue" directory */
1528 mutex_unlock(&gdp_mutex);
1532 /* subsystems can specify a default root directory for their devices */
1533 if (!parent && dev->bus && dev->bus->dev_root)
1534 return &dev->bus->dev_root->kobj;
1537 return &parent->kobj;
1541 static inline bool live_in_glue_dir(struct kobject *kobj,
1544 if (!kobj || !dev->class ||
1545 kobj->kset != &dev->class->p->glue_dirs)
1550 static inline struct kobject *get_glue_dir(struct device *dev)
1552 return dev->kobj.parent;
1556 * make sure cleaning up dir as the last step, we need to make
1557 * sure .release handler of kobject is run with holding the
1560 static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
1562 /* see if we live in a "glue" directory */
1563 if (!live_in_glue_dir(glue_dir, dev))
1566 mutex_lock(&gdp_mutex);
1567 kobject_put(glue_dir);
1568 mutex_unlock(&gdp_mutex);
1571 static int device_add_class_symlinks(struct device *dev)
1573 struct device_node *of_node = dev_of_node(dev);
1577 error = sysfs_create_link(&dev->kobj, &of_node->kobj,"of_node");
1579 dev_warn(dev, "Error %d creating of_node link\n",error);
1580 /* An error here doesn't warrant bringing down the device */
1586 error = sysfs_create_link(&dev->kobj,
1587 &dev->class->p->subsys.kobj,
1592 if (dev->parent && device_is_not_partition(dev)) {
1593 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
1600 /* /sys/block has directories and does not need symlinks */
1601 if (sysfs_deprecated && dev->class == &block_class)
1605 /* link in the class directory pointing to the device */
1606 error = sysfs_create_link(&dev->class->p->subsys.kobj,
1607 &dev->kobj, dev_name(dev));
1614 sysfs_remove_link(&dev->kobj, "device");
1617 sysfs_remove_link(&dev->kobj, "subsystem");
1619 sysfs_remove_link(&dev->kobj, "of_node");
1623 static void device_remove_class_symlinks(struct device *dev)
1625 if (dev_of_node(dev))
1626 sysfs_remove_link(&dev->kobj, "of_node");
1631 if (dev->parent && device_is_not_partition(dev))
1632 sysfs_remove_link(&dev->kobj, "device");
1633 sysfs_remove_link(&dev->kobj, "subsystem");
1635 if (sysfs_deprecated && dev->class == &block_class)
1638 sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
1642 * dev_set_name - set a device name
1644 * @fmt: format string for the device's name
1646 int dev_set_name(struct device *dev, const char *fmt, ...)
1651 va_start(vargs, fmt);
1652 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
1656 EXPORT_SYMBOL_GPL(dev_set_name);
1659 * device_to_dev_kobj - select a /sys/dev/ directory for the device
1662 * By default we select char/ for new entries. Setting class->dev_obj
1663 * to NULL prevents an entry from being created. class->dev_kobj must
1664 * be set (or cleared) before any devices are registered to the class
1665 * otherwise device_create_sys_dev_entry() and
1666 * device_remove_sys_dev_entry() will disagree about the presence of
1669 static struct kobject *device_to_dev_kobj(struct device *dev)
1671 struct kobject *kobj;
1674 kobj = dev->class->dev_kobj;
1676 kobj = sysfs_dev_char_kobj;
1681 static int device_create_sys_dev_entry(struct device *dev)
1683 struct kobject *kobj = device_to_dev_kobj(dev);
1688 format_dev_t(devt_str, dev->devt);
1689 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
1695 static void device_remove_sys_dev_entry(struct device *dev)
1697 struct kobject *kobj = device_to_dev_kobj(dev);
1701 format_dev_t(devt_str, dev->devt);
1702 sysfs_remove_link(kobj, devt_str);
1706 int device_private_init(struct device *dev)
1708 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
1711 dev->p->device = dev;
1712 klist_init(&dev->p->klist_children, klist_children_get,
1713 klist_children_put);
1714 INIT_LIST_HEAD(&dev->p->deferred_probe);
1719 * device_add - add device to device hierarchy.
1722 * This is part 2 of device_register(), though may be called
1723 * separately _iff_ device_initialize() has been called separately.
1725 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
1726 * to the global and sibling lists for the device, then
1727 * adds it to the other relevant subsystems of the driver model.
1729 * Do not call this routine or device_register() more than once for
1730 * any device structure. The driver model core is not designed to work
1731 * with devices that get unregistered and then spring back to life.
1732 * (Among other things, it's very hard to guarantee that all references
1733 * to the previous incarnation of @dev have been dropped.) Allocate
1734 * and register a fresh new struct device instead.
1736 * NOTE: _Never_ directly free @dev after calling this function, even
1737 * if it returned an error! Always use put_device() to give up your
1738 * reference instead.
1740 int device_add(struct device *dev)
1742 struct device *parent;
1743 struct kobject *kobj;
1744 struct class_interface *class_intf;
1745 int error = -EINVAL;
1746 struct kobject *glue_dir = NULL;
1748 dev = get_device(dev);
1753 error = device_private_init(dev);
1759 * for statically allocated devices, which should all be converted
1760 * some day, we need to initialize the name. We prevent reading back
1761 * the name, and force the use of dev_name()
1763 if (dev->init_name) {
1764 dev_set_name(dev, "%s", dev->init_name);
1765 dev->init_name = NULL;
1768 /* subsystems can specify simple device enumeration */
1769 if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
1770 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
1772 if (!dev_name(dev)) {
1777 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
1779 parent = get_device(dev->parent);
1780 kobj = get_device_parent(dev, parent);
1782 error = PTR_ERR(kobj);
1786 dev->kobj.parent = kobj;
1788 /* use parent numa_node */
1789 if (parent && (dev_to_node(dev) == NUMA_NO_NODE))
1790 set_dev_node(dev, dev_to_node(parent));
1792 /* first, register with generic layer. */
1793 /* we require the name to be set before, and pass NULL */
1794 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
1796 glue_dir = get_glue_dir(dev);
1800 /* notify platform of device entry */
1801 if (platform_notify)
1802 platform_notify(dev);
1804 error = device_create_file(dev, &dev_attr_uevent);
1808 error = device_add_class_symlinks(dev);
1811 error = device_add_attrs(dev);
1814 error = bus_add_device(dev);
1817 error = dpm_sysfs_add(dev);
1822 if (MAJOR(dev->devt)) {
1823 error = device_create_file(dev, &dev_attr_dev);
1827 error = device_create_sys_dev_entry(dev);
1831 devtmpfs_create_node(dev);
1834 /* Notify clients of device addition. This call must come
1835 * after dpm_sysfs_add() and before kobject_uevent().
1838 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1839 BUS_NOTIFY_ADD_DEVICE, dev);
1841 kobject_uevent(&dev->kobj, KOBJ_ADD);
1842 bus_probe_device(dev);
1844 klist_add_tail(&dev->p->knode_parent,
1845 &parent->p->klist_children);
1848 mutex_lock(&dev->class->p->mutex);
1849 /* tie the class to the device */
1850 klist_add_tail(&dev->knode_class,
1851 &dev->class->p->klist_devices);
1853 /* notify any interfaces that the device is here */
1854 list_for_each_entry(class_intf,
1855 &dev->class->p->interfaces, node)
1856 if (class_intf->add_dev)
1857 class_intf->add_dev(dev, class_intf);
1858 mutex_unlock(&dev->class->p->mutex);
1864 if (MAJOR(dev->devt))
1865 device_remove_file(dev, &dev_attr_dev);
1867 device_pm_remove(dev);
1868 dpm_sysfs_remove(dev);
1870 bus_remove_device(dev);
1872 device_remove_attrs(dev);
1874 device_remove_class_symlinks(dev);
1876 device_remove_file(dev, &dev_attr_uevent);
1878 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
1879 glue_dir = get_glue_dir(dev);
1880 kobject_del(&dev->kobj);
1882 cleanup_glue_dir(dev, glue_dir);
1890 EXPORT_SYMBOL_GPL(device_add);
1893 * device_register - register a device with the system.
1894 * @dev: pointer to the device structure
1896 * This happens in two clean steps - initialize the device
1897 * and add it to the system. The two steps can be called
1898 * separately, but this is the easiest and most common.
1899 * I.e. you should only call the two helpers separately if
1900 * have a clearly defined need to use and refcount the device
1901 * before it is added to the hierarchy.
1903 * For more information, see the kerneldoc for device_initialize()
1906 * NOTE: _Never_ directly free @dev after calling this function, even
1907 * if it returned an error! Always use put_device() to give up the
1908 * reference initialized in this function instead.
1910 int device_register(struct device *dev)
1912 device_initialize(dev);
1913 return device_add(dev);
1915 EXPORT_SYMBOL_GPL(device_register);
1918 * get_device - increment reference count for device.
1921 * This simply forwards the call to kobject_get(), though
1922 * we do take care to provide for the case that we get a NULL
1923 * pointer passed in.
1925 struct device *get_device(struct device *dev)
1927 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
1929 EXPORT_SYMBOL_GPL(get_device);
1932 * put_device - decrement reference count.
1933 * @dev: device in question.
1935 void put_device(struct device *dev)
1937 /* might_sleep(); */
1939 kobject_put(&dev->kobj);
1941 EXPORT_SYMBOL_GPL(put_device);
1944 * device_del - delete device from system.
1947 * This is the first part of the device unregistration
1948 * sequence. This removes the device from the lists we control
1949 * from here, has it removed from the other driver model
1950 * subsystems it was added to in device_add(), and removes it
1951 * from the kobject hierarchy.
1953 * NOTE: this should be called manually _iff_ device_add() was
1954 * also called manually.
1956 void device_del(struct device *dev)
1958 struct device *parent = dev->parent;
1959 struct kobject *glue_dir = NULL;
1960 struct class_interface *class_intf;
1962 /* Notify clients of device removal. This call must come
1963 * before dpm_sysfs_remove().
1966 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1967 BUS_NOTIFY_DEL_DEVICE, dev);
1969 device_links_purge(dev);
1970 dpm_sysfs_remove(dev);
1972 klist_del(&dev->p->knode_parent);
1973 if (MAJOR(dev->devt)) {
1974 devtmpfs_delete_node(dev);
1975 device_remove_sys_dev_entry(dev);
1976 device_remove_file(dev, &dev_attr_dev);
1979 device_remove_class_symlinks(dev);
1981 mutex_lock(&dev->class->p->mutex);
1982 /* notify any interfaces that the device is now gone */
1983 list_for_each_entry(class_intf,
1984 &dev->class->p->interfaces, node)
1985 if (class_intf->remove_dev)
1986 class_intf->remove_dev(dev, class_intf);
1987 /* remove the device from the class list */
1988 klist_del(&dev->knode_class);
1989 mutex_unlock(&dev->class->p->mutex);
1991 device_remove_file(dev, &dev_attr_uevent);
1992 device_remove_attrs(dev);
1993 bus_remove_device(dev);
1994 device_pm_remove(dev);
1995 driver_deferred_probe_del(dev);
1996 device_remove_properties(dev);
1998 /* Notify the platform of the removal, in case they
1999 * need to do anything...
2001 if (platform_notify_remove)
2002 platform_notify_remove(dev);
2004 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2005 BUS_NOTIFY_REMOVED_DEVICE, dev);
2006 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
2007 glue_dir = get_glue_dir(dev);
2008 kobject_del(&dev->kobj);
2009 cleanup_glue_dir(dev, glue_dir);
2012 EXPORT_SYMBOL_GPL(device_del);
2015 * device_unregister - unregister device from system.
2016 * @dev: device going away.
2018 * We do this in two parts, like we do device_register(). First,
2019 * we remove it from all the subsystems with device_del(), then
2020 * we decrement the reference count via put_device(). If that
2021 * is the final reference count, the device will be cleaned up
2022 * via device_release() above. Otherwise, the structure will
2023 * stick around until the final reference to the device is dropped.
2025 void device_unregister(struct device *dev)
2027 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
2031 EXPORT_SYMBOL_GPL(device_unregister);
2033 static struct device *prev_device(struct klist_iter *i)
2035 struct klist_node *n = klist_prev(i);
2036 struct device *dev = NULL;
2037 struct device_private *p;
2040 p = to_device_private_parent(n);
2046 static struct device *next_device(struct klist_iter *i)
2048 struct klist_node *n = klist_next(i);
2049 struct device *dev = NULL;
2050 struct device_private *p;
2053 p = to_device_private_parent(n);
2060 * device_get_devnode - path of device node file
2062 * @mode: returned file access mode
2063 * @uid: returned file owner
2064 * @gid: returned file group
2065 * @tmp: possibly allocated string
2067 * Return the relative path of a possible device node.
2068 * Non-default names may need to allocate a memory to compose
2069 * a name. This memory is returned in tmp and needs to be
2070 * freed by the caller.
2072 const char *device_get_devnode(struct device *dev,
2073 umode_t *mode, kuid_t *uid, kgid_t *gid,
2080 /* the device type may provide a specific name */
2081 if (dev->type && dev->type->devnode)
2082 *tmp = dev->type->devnode(dev, mode, uid, gid);
2086 /* the class may provide a specific name */
2087 if (dev->class && dev->class->devnode)
2088 *tmp = dev->class->devnode(dev, mode);
2092 /* return name without allocation, tmp == NULL */
2093 if (strchr(dev_name(dev), '!') == NULL)
2094 return dev_name(dev);
2096 /* replace '!' in the name with '/' */
2097 s = kstrdup(dev_name(dev), GFP_KERNEL);
2100 strreplace(s, '!', '/');
2105 * device_for_each_child - device child iterator.
2106 * @parent: parent struct device.
2107 * @fn: function to be called for each device.
2108 * @data: data for the callback.
2110 * Iterate over @parent's child devices, and call @fn for each,
2113 * We check the return of @fn each time. If it returns anything
2114 * other than 0, we break out and return that value.
2116 int device_for_each_child(struct device *parent, void *data,
2117 int (*fn)(struct device *dev, void *data))
2119 struct klist_iter i;
2120 struct device *child;
2126 klist_iter_init(&parent->p->klist_children, &i);
2127 while ((child = next_device(&i)) && !error)
2128 error = fn(child, data);
2129 klist_iter_exit(&i);
2132 EXPORT_SYMBOL_GPL(device_for_each_child);
2135 * device_for_each_child_reverse - device child iterator in reversed order.
2136 * @parent: parent struct device.
2137 * @fn: function to be called for each device.
2138 * @data: data for the callback.
2140 * Iterate over @parent's child devices, and call @fn for each,
2143 * We check the return of @fn each time. If it returns anything
2144 * other than 0, we break out and return that value.
2146 int device_for_each_child_reverse(struct device *parent, void *data,
2147 int (*fn)(struct device *dev, void *data))
2149 struct klist_iter i;
2150 struct device *child;
2156 klist_iter_init(&parent->p->klist_children, &i);
2157 while ((child = prev_device(&i)) && !error)
2158 error = fn(child, data);
2159 klist_iter_exit(&i);
2162 EXPORT_SYMBOL_GPL(device_for_each_child_reverse);
2165 * device_find_child - device iterator for locating a particular device.
2166 * @parent: parent struct device
2167 * @match: Callback function to check device
2168 * @data: Data to pass to match function
2170 * This is similar to the device_for_each_child() function above, but it
2171 * returns a reference to a device that is 'found' for later use, as
2172 * determined by the @match callback.
2174 * The callback should return 0 if the device doesn't match and non-zero
2175 * if it does. If the callback returns non-zero and a reference to the
2176 * current device can be obtained, this function will return to the caller
2177 * and not iterate over any more devices.
2179 * NOTE: you will need to drop the reference with put_device() after use.
2181 struct device *device_find_child(struct device *parent, void *data,
2182 int (*match)(struct device *dev, void *data))
2184 struct klist_iter i;
2185 struct device *child;
2190 klist_iter_init(&parent->p->klist_children, &i);
2191 while ((child = next_device(&i)))
2192 if (match(child, data) && get_device(child))
2194 klist_iter_exit(&i);
2197 EXPORT_SYMBOL_GPL(device_find_child);
2199 int __init devices_init(void)
2201 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
2204 dev_kobj = kobject_create_and_add("dev", NULL);
2207 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
2208 if (!sysfs_dev_block_kobj)
2209 goto block_kobj_err;
2210 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
2211 if (!sysfs_dev_char_kobj)
2217 kobject_put(sysfs_dev_block_kobj);
2219 kobject_put(dev_kobj);
2221 kset_unregister(devices_kset);
2225 static int device_check_offline(struct device *dev, void *not_used)
2229 ret = device_for_each_child(dev, NULL, device_check_offline);
2233 return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
2237 * device_offline - Prepare the device for hot-removal.
2238 * @dev: Device to be put offline.
2240 * Execute the device bus type's .offline() callback, if present, to prepare
2241 * the device for a subsequent hot-removal. If that succeeds, the device must
2242 * not be used until either it is removed or its bus type's .online() callback
2245 * Call under device_hotplug_lock.
2247 int device_offline(struct device *dev)
2251 if (dev->offline_disabled)
2254 ret = device_for_each_child(dev, NULL, device_check_offline);
2259 if (device_supports_offline(dev)) {
2263 ret = dev->bus->offline(dev);
2265 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
2266 dev->offline = true;
2276 * device_online - Put the device back online after successful device_offline().
2277 * @dev: Device to be put back online.
2279 * If device_offline() has been successfully executed for @dev, but the device
2280 * has not been removed subsequently, execute its bus type's .online() callback
2281 * to indicate that the device can be used again.
2283 * Call under device_hotplug_lock.
2285 int device_online(struct device *dev)
2290 if (device_supports_offline(dev)) {
2292 ret = dev->bus->online(dev);
2294 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
2295 dev->offline = false;
2306 struct root_device {
2308 struct module *owner;
2311 static inline struct root_device *to_root_device(struct device *d)
2313 return container_of(d, struct root_device, dev);
2316 static void root_device_release(struct device *dev)
2318 kfree(to_root_device(dev));
2322 * __root_device_register - allocate and register a root device
2323 * @name: root device name
2324 * @owner: owner module of the root device, usually THIS_MODULE
2326 * This function allocates a root device and registers it
2327 * using device_register(). In order to free the returned
2328 * device, use root_device_unregister().
2330 * Root devices are dummy devices which allow other devices
2331 * to be grouped under /sys/devices. Use this function to
2332 * allocate a root device and then use it as the parent of
2333 * any device which should appear under /sys/devices/{name}
2335 * The /sys/devices/{name} directory will also contain a
2336 * 'module' symlink which points to the @owner directory
2339 * Returns &struct device pointer on success, or ERR_PTR() on error.
2341 * Note: You probably want to use root_device_register().
2343 struct device *__root_device_register(const char *name, struct module *owner)
2345 struct root_device *root;
2348 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
2350 return ERR_PTR(err);
2352 err = dev_set_name(&root->dev, "%s", name);
2355 return ERR_PTR(err);
2358 root->dev.release = root_device_release;
2360 err = device_register(&root->dev);
2362 put_device(&root->dev);
2363 return ERR_PTR(err);
2366 #ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
2368 struct module_kobject *mk = &owner->mkobj;
2370 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
2372 device_unregister(&root->dev);
2373 return ERR_PTR(err);
2375 root->owner = owner;
2381 EXPORT_SYMBOL_GPL(__root_device_register);
2384 * root_device_unregister - unregister and free a root device
2385 * @dev: device going away
2387 * This function unregisters and cleans up a device that was created by
2388 * root_device_register().
2390 void root_device_unregister(struct device *dev)
2392 struct root_device *root = to_root_device(dev);
2395 sysfs_remove_link(&root->dev.kobj, "module");
2397 device_unregister(dev);
2399 EXPORT_SYMBOL_GPL(root_device_unregister);
2402 static void device_create_release(struct device *dev)
2404 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
2408 static struct device *
2409 device_create_groups_vargs(struct class *class, struct device *parent,
2410 dev_t devt, void *drvdata,
2411 const struct attribute_group **groups,
2412 const char *fmt, va_list args)
2414 struct device *dev = NULL;
2415 int retval = -ENODEV;
2417 if (class == NULL || IS_ERR(class))
2420 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2426 device_initialize(dev);
2429 dev->parent = parent;
2430 dev->groups = groups;
2431 dev->release = device_create_release;
2432 dev_set_drvdata(dev, drvdata);
2434 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
2438 retval = device_add(dev);
2446 return ERR_PTR(retval);
2450 * device_create_vargs - creates a device and registers it with sysfs
2451 * @class: pointer to the struct class that this device should be registered to
2452 * @parent: pointer to the parent struct device of this new device, if any
2453 * @devt: the dev_t for the char device to be added
2454 * @drvdata: the data to be added to the device for callbacks
2455 * @fmt: string for the device's name
2456 * @args: va_list for the device's name
2458 * This function can be used by char device classes. A struct device
2459 * will be created in sysfs, registered to the specified class.
2461 * A "dev" file will be created, showing the dev_t for the device, if
2462 * the dev_t is not 0,0.
2463 * If a pointer to a parent struct device is passed in, the newly created
2464 * struct device will be a child of that device in sysfs.
2465 * The pointer to the struct device will be returned from the call.
2466 * Any further sysfs files that might be required can be created using this
2469 * Returns &struct device pointer on success, or ERR_PTR() on error.
2471 * Note: the struct class passed to this function must have previously
2472 * been created with a call to class_create().
2474 struct device *device_create_vargs(struct class *class, struct device *parent,
2475 dev_t devt, void *drvdata, const char *fmt,
2478 return device_create_groups_vargs(class, parent, devt, drvdata, NULL,
2481 EXPORT_SYMBOL_GPL(device_create_vargs);
2484 * device_create - 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
2491 * This function can be used by char device classes. A struct device
2492 * will be created in sysfs, registered to the specified class.
2494 * A "dev" file will be created, showing the dev_t for the device, if
2495 * the dev_t is not 0,0.
2496 * If a pointer to a parent struct device is passed in, the newly created
2497 * struct device will be a child of that device in sysfs.
2498 * The pointer to the struct device will be returned from the call.
2499 * Any further sysfs files that might be required can be created using this
2502 * Returns &struct device pointer on success, or ERR_PTR() on error.
2504 * Note: the struct class passed to this function must have previously
2505 * been created with a call to class_create().
2507 struct device *device_create(struct class *class, struct device *parent,
2508 dev_t devt, void *drvdata, const char *fmt, ...)
2513 va_start(vargs, fmt);
2514 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
2518 EXPORT_SYMBOL_GPL(device_create);
2521 * device_create_with_groups - creates a device and registers it with sysfs
2522 * @class: pointer to the struct class that this device should be registered to
2523 * @parent: pointer to the parent struct device of this new device, if any
2524 * @devt: the dev_t for the char device to be added
2525 * @drvdata: the data to be added to the device for callbacks
2526 * @groups: NULL-terminated list of attribute groups to be created
2527 * @fmt: string for the device's name
2529 * This function can be used by char device classes. A struct device
2530 * will be created in sysfs, registered to the specified class.
2531 * Additional attributes specified in the groups parameter will also
2532 * be created automatically.
2534 * A "dev" file will be created, showing the dev_t for the device, if
2535 * the dev_t is not 0,0.
2536 * If a pointer to a parent struct device is passed in, the newly created
2537 * struct device will be a child of that device in sysfs.
2538 * The pointer to the struct device will be returned from the call.
2539 * Any further sysfs files that might be required can be created using this
2542 * Returns &struct device pointer on success, or ERR_PTR() on error.
2544 * Note: the struct class passed to this function must have previously
2545 * been created with a call to class_create().
2547 struct device *device_create_with_groups(struct class *class,
2548 struct device *parent, dev_t devt,
2550 const struct attribute_group **groups,
2551 const char *fmt, ...)
2556 va_start(vargs, fmt);
2557 dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
2562 EXPORT_SYMBOL_GPL(device_create_with_groups);
2564 static int __match_devt(struct device *dev, const void *data)
2566 const dev_t *devt = data;
2568 return dev->devt == *devt;
2572 * device_destroy - removes a device that was created with device_create()
2573 * @class: pointer to the struct class that this device was registered with
2574 * @devt: the dev_t of the device that was previously registered
2576 * This call unregisters and cleans up a device that was created with a
2577 * call to device_create().
2579 void device_destroy(struct class *class, dev_t devt)
2583 dev = class_find_device(class, NULL, &devt, __match_devt);
2586 device_unregister(dev);
2589 EXPORT_SYMBOL_GPL(device_destroy);
2592 * device_rename - renames a device
2593 * @dev: the pointer to the struct device to be renamed
2594 * @new_name: the new name of the device
2596 * It is the responsibility of the caller to provide mutual
2597 * exclusion between two different calls of device_rename
2598 * on the same device to ensure that new_name is valid and
2599 * won't conflict with other devices.
2601 * Note: Don't call this function. Currently, the networking layer calls this
2602 * function, but that will change. The following text from Kay Sievers offers
2605 * Renaming devices is racy at many levels, symlinks and other stuff are not
2606 * replaced atomically, and you get a "move" uevent, but it's not easy to
2607 * connect the event to the old and new device. Device nodes are not renamed at
2608 * all, there isn't even support for that in the kernel now.
2610 * In the meantime, during renaming, your target name might be taken by another
2611 * driver, creating conflicts. Or the old name is taken directly after you
2612 * renamed it -- then you get events for the same DEVPATH, before you even see
2613 * the "move" event. It's just a mess, and nothing new should ever rely on
2614 * kernel device renaming. Besides that, it's not even implemented now for
2615 * other things than (driver-core wise very simple) network devices.
2617 * We are currently about to change network renaming in udev to completely
2618 * disallow renaming of devices in the same namespace as the kernel uses,
2619 * because we can't solve the problems properly, that arise with swapping names
2620 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
2621 * be allowed to some other name than eth[0-9]*, for the aforementioned
2624 * Make up a "real" name in the driver before you register anything, or add
2625 * some other attributes for userspace to find the device, or use udev to add
2626 * symlinks -- but never rename kernel devices later, it's a complete mess. We
2627 * don't even want to get into that and try to implement the missing pieces in
2628 * the core. We really have other pieces to fix in the driver core mess. :)
2630 int device_rename(struct device *dev, const char *new_name)
2632 struct kobject *kobj = &dev->kobj;
2633 char *old_device_name = NULL;
2636 dev = get_device(dev);
2640 dev_dbg(dev, "renaming to %s\n", new_name);
2642 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
2643 if (!old_device_name) {
2649 error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj,
2650 kobj, old_device_name,
2651 new_name, kobject_namespace(kobj));
2656 error = kobject_rename(kobj, new_name);
2663 kfree(old_device_name);
2667 EXPORT_SYMBOL_GPL(device_rename);
2669 static int device_move_class_links(struct device *dev,
2670 struct device *old_parent,
2671 struct device *new_parent)
2676 sysfs_remove_link(&dev->kobj, "device");
2678 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
2684 * device_move - moves a device to a new parent
2685 * @dev: the pointer to the struct device to be moved
2686 * @new_parent: the new parent of the device (can by NULL)
2687 * @dpm_order: how to reorder the dpm_list
2689 int device_move(struct device *dev, struct device *new_parent,
2690 enum dpm_order dpm_order)
2693 struct device *old_parent;
2694 struct kobject *new_parent_kobj;
2696 dev = get_device(dev);
2701 new_parent = get_device(new_parent);
2702 new_parent_kobj = get_device_parent(dev, new_parent);
2703 if (IS_ERR(new_parent_kobj)) {
2704 error = PTR_ERR(new_parent_kobj);
2705 put_device(new_parent);
2709 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
2710 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
2711 error = kobject_move(&dev->kobj, new_parent_kobj);
2713 cleanup_glue_dir(dev, new_parent_kobj);
2714 put_device(new_parent);
2717 old_parent = dev->parent;
2718 dev->parent = new_parent;
2720 klist_remove(&dev->p->knode_parent);
2722 klist_add_tail(&dev->p->knode_parent,
2723 &new_parent->p->klist_children);
2724 set_dev_node(dev, dev_to_node(new_parent));
2728 error = device_move_class_links(dev, old_parent, new_parent);
2730 /* We ignore errors on cleanup since we're hosed anyway... */
2731 device_move_class_links(dev, new_parent, old_parent);
2732 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
2734 klist_remove(&dev->p->knode_parent);
2735 dev->parent = old_parent;
2737 klist_add_tail(&dev->p->knode_parent,
2738 &old_parent->p->klist_children);
2739 set_dev_node(dev, dev_to_node(old_parent));
2742 cleanup_glue_dir(dev, new_parent_kobj);
2743 put_device(new_parent);
2747 switch (dpm_order) {
2748 case DPM_ORDER_NONE:
2750 case DPM_ORDER_DEV_AFTER_PARENT:
2751 device_pm_move_after(dev, new_parent);
2752 devices_kset_move_after(dev, new_parent);
2754 case DPM_ORDER_PARENT_BEFORE_DEV:
2755 device_pm_move_before(new_parent, dev);
2756 devices_kset_move_before(new_parent, dev);
2758 case DPM_ORDER_DEV_LAST:
2759 device_pm_move_last(dev);
2760 devices_kset_move_last(dev);
2764 put_device(old_parent);
2770 EXPORT_SYMBOL_GPL(device_move);
2773 * device_shutdown - call ->shutdown() on each device to shutdown.
2775 void device_shutdown(void)
2777 struct device *dev, *parent;
2779 spin_lock(&devices_kset->list_lock);
2781 * Walk the devices list backward, shutting down each in turn.
2782 * Beware that device unplug events may also start pulling
2783 * devices offline, even as the system is shutting down.
2785 while (!list_empty(&devices_kset->list)) {
2786 dev = list_entry(devices_kset->list.prev, struct device,
2790 * hold reference count of device's parent to
2791 * prevent it from being freed because parent's
2792 * lock is to be held
2794 parent = get_device(dev->parent);
2797 * Make sure the device is off the kset list, in the
2798 * event that dev->*->shutdown() doesn't remove it.
2800 list_del_init(&dev->kobj.entry);
2801 spin_unlock(&devices_kset->list_lock);
2803 /* hold lock to avoid race with probe/release */
2805 device_lock(parent);
2808 /* Don't allow any more runtime suspends */
2809 pm_runtime_get_noresume(dev);
2810 pm_runtime_barrier(dev);
2812 if (dev->class && dev->class->shutdown_pre) {
2814 dev_info(dev, "shutdown_pre\n");
2815 dev->class->shutdown_pre(dev);
2817 if (dev->bus && dev->bus->shutdown) {
2819 dev_info(dev, "shutdown\n");
2820 dev->bus->shutdown(dev);
2821 } else if (dev->driver && dev->driver->shutdown) {
2823 dev_info(dev, "shutdown\n");
2824 dev->driver->shutdown(dev);
2829 device_unlock(parent);
2834 spin_lock(&devices_kset->list_lock);
2836 spin_unlock(&devices_kset->list_lock);
2840 * Device logging functions
2843 #ifdef CONFIG_PRINTK
2845 create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
2851 subsys = dev->class->name;
2853 subsys = dev->bus->name;
2857 pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys);
2862 * Add device identifier DEVICE=:
2866 * +sound:card0 subsystem:devname
2868 if (MAJOR(dev->devt)) {
2871 if (strcmp(subsys, "block") == 0)
2876 pos += snprintf(hdr + pos, hdrlen - pos,
2878 c, MAJOR(dev->devt), MINOR(dev->devt));
2879 } else if (strcmp(subsys, "net") == 0) {
2880 struct net_device *net = to_net_dev(dev);
2883 pos += snprintf(hdr + pos, hdrlen - pos,
2884 "DEVICE=n%u", net->ifindex);
2887 pos += snprintf(hdr + pos, hdrlen - pos,
2888 "DEVICE=+%s:%s", subsys, dev_name(dev));
2897 dev_WARN(dev, "device/subsystem name too long");
2901 int dev_vprintk_emit(int level, const struct device *dev,
2902 const char *fmt, va_list args)
2907 hdrlen = create_syslog_header(dev, hdr, sizeof(hdr));
2909 return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args);
2911 EXPORT_SYMBOL(dev_vprintk_emit);
2913 int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
2918 va_start(args, fmt);
2920 r = dev_vprintk_emit(level, dev, fmt, args);
2926 EXPORT_SYMBOL(dev_printk_emit);
2928 static void __dev_printk(const char *level, const struct device *dev,
2929 struct va_format *vaf)
2932 dev_printk_emit(level[1] - '0', dev, "%s %s: %pV",
2933 dev_driver_string(dev), dev_name(dev), vaf);
2935 printk("%s(NULL device *): %pV", level, vaf);
2938 void dev_printk(const char *level, const struct device *dev,
2939 const char *fmt, ...)
2941 struct va_format vaf;
2944 va_start(args, fmt);
2949 __dev_printk(level, dev, &vaf);
2953 EXPORT_SYMBOL(dev_printk);
2955 #define define_dev_printk_level(func, kern_level) \
2956 void func(const struct device *dev, const char *fmt, ...) \
2958 struct va_format vaf; \
2961 va_start(args, fmt); \
2966 __dev_printk(kern_level, dev, &vaf); \
2970 EXPORT_SYMBOL(func);
2972 define_dev_printk_level(dev_emerg, KERN_EMERG);
2973 define_dev_printk_level(dev_alert, KERN_ALERT);
2974 define_dev_printk_level(dev_crit, KERN_CRIT);
2975 define_dev_printk_level(dev_err, KERN_ERR);
2976 define_dev_printk_level(dev_warn, KERN_WARNING);
2977 define_dev_printk_level(dev_notice, KERN_NOTICE);
2978 define_dev_printk_level(_dev_info, KERN_INFO);
2982 static inline bool fwnode_is_primary(struct fwnode_handle *fwnode)
2984 return fwnode && !IS_ERR(fwnode->secondary);
2988 * set_primary_fwnode - Change the primary firmware node of a given device.
2989 * @dev: Device to handle.
2990 * @fwnode: New primary firmware node of the device.
2992 * Set the device's firmware node pointer to @fwnode, but if a secondary
2993 * firmware node of the device is present, preserve it.
2995 void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
2998 struct fwnode_handle *fn = dev->fwnode;
3000 if (fwnode_is_primary(fn))
3004 WARN_ON(fwnode->secondary);
3005 fwnode->secondary = fn;
3007 dev->fwnode = fwnode;
3009 dev->fwnode = fwnode_is_primary(dev->fwnode) ?
3010 dev->fwnode->secondary : NULL;
3013 EXPORT_SYMBOL_GPL(set_primary_fwnode);
3016 * set_secondary_fwnode - Change the secondary firmware node of a given device.
3017 * @dev: Device to handle.
3018 * @fwnode: New secondary firmware node of the device.
3020 * If a primary firmware node of the device is present, set its secondary
3021 * pointer to @fwnode. Otherwise, set the device's firmware node pointer to
3024 void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
3027 fwnode->secondary = ERR_PTR(-ENODEV);
3029 if (fwnode_is_primary(dev->fwnode))
3030 dev->fwnode->secondary = fwnode;
3032 dev->fwnode = fwnode;
3036 * device_set_of_node_from_dev - reuse device-tree node of another device
3037 * @dev: device whose device-tree node is being set
3038 * @dev2: device whose device-tree node is being reused
3040 * Takes another reference to the new device-tree node after first dropping
3041 * any reference held to the old node.
3043 void device_set_of_node_from_dev(struct device *dev, const struct device *dev2)
3045 of_node_put(dev->of_node);
3046 dev->of_node = of_node_get(dev2->of_node);
3047 dev->of_node_reused = true;
3049 EXPORT_SYMBOL_GPL(device_set_of_node_from_dev);