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 * If the link is being added by the consumer driver at probe
222 * time, balance the decrementation of the supplier's runtime PM
223 * usage counter after consumer probe in driver_probe_device().
225 if (consumer->links.status == DL_DEV_PROBING)
226 pm_runtime_get_noresume(supplier);
228 get_device(supplier);
229 link->supplier = supplier;
230 INIT_LIST_HEAD(&link->s_node);
231 get_device(consumer);
232 link->consumer = consumer;
233 INIT_LIST_HEAD(&link->c_node);
236 /* Determine the initial link state. */
237 if (flags & DL_FLAG_STATELESS) {
238 link->status = DL_STATE_NONE;
240 switch (supplier->links.status) {
241 case DL_DEV_DRIVER_BOUND:
242 switch (consumer->links.status) {
245 * Some callers expect the link creation during
246 * consumer driver probe to resume the supplier
247 * even without DL_FLAG_RPM_ACTIVE.
249 if (flags & DL_FLAG_PM_RUNTIME)
250 pm_runtime_resume(supplier);
252 link->status = DL_STATE_CONSUMER_PROBE;
254 case DL_DEV_DRIVER_BOUND:
255 link->status = DL_STATE_ACTIVE;
258 link->status = DL_STATE_AVAILABLE;
262 case DL_DEV_UNBINDING:
263 link->status = DL_STATE_SUPPLIER_UNBIND;
266 link->status = DL_STATE_DORMANT;
272 * Move the consumer and all of the devices depending on it to the end
273 * of dpm_list and the devices_kset list.
275 * It is necessary to hold dpm_list locked throughout all that or else
276 * we may end up suspending with a wrong ordering of it.
278 device_reorder_to_tail(consumer, NULL);
280 list_add_tail_rcu(&link->s_node, &supplier->links.consumers);
281 list_add_tail_rcu(&link->c_node, &consumer->links.suppliers);
283 dev_info(consumer, "Linked as a consumer to %s\n", dev_name(supplier));
287 device_links_write_unlock();
290 EXPORT_SYMBOL_GPL(device_link_add);
292 static void device_link_free(struct device_link *link)
294 put_device(link->consumer);
295 put_device(link->supplier);
300 static void __device_link_free_srcu(struct rcu_head *rhead)
302 device_link_free(container_of(rhead, struct device_link, rcu_head));
305 static void __device_link_del(struct device_link *link)
307 dev_info(link->consumer, "Dropping the link to %s\n",
308 dev_name(link->supplier));
310 if (link->flags & DL_FLAG_PM_RUNTIME)
311 pm_runtime_drop_link(link->consumer);
313 list_del_rcu(&link->s_node);
314 list_del_rcu(&link->c_node);
315 call_srcu(&device_links_srcu, &link->rcu_head, __device_link_free_srcu);
317 #else /* !CONFIG_SRCU */
318 static void __device_link_del(struct device_link *link)
320 dev_info(link->consumer, "Dropping the link to %s\n",
321 dev_name(link->supplier));
323 if (link->flags & DL_FLAG_PM_RUNTIME)
324 pm_runtime_drop_link(link->consumer);
326 list_del(&link->s_node);
327 list_del(&link->c_node);
328 device_link_free(link);
330 #endif /* !CONFIG_SRCU */
333 * device_link_del - Delete a link between two devices.
334 * @link: Device link to delete.
336 * The caller must ensure proper synchronization of this function with runtime
339 void device_link_del(struct device_link *link)
341 device_links_write_lock();
343 __device_link_del(link);
345 device_links_write_unlock();
347 EXPORT_SYMBOL_GPL(device_link_del);
349 static void device_links_missing_supplier(struct device *dev)
351 struct device_link *link;
353 list_for_each_entry(link, &dev->links.suppliers, c_node)
354 if (link->status == DL_STATE_CONSUMER_PROBE)
355 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
359 * device_links_check_suppliers - Check presence of supplier drivers.
360 * @dev: Consumer device.
362 * Check links from this device to any suppliers. Walk the list of the device's
363 * links to suppliers and see if all of them are available. If not, simply
364 * return -EPROBE_DEFER.
366 * We need to guarantee that the supplier will not go away after the check has
367 * been positive here. It only can go away in __device_release_driver() and
368 * that function checks the device's links to consumers. This means we need to
369 * mark the link as "consumer probe in progress" to make the supplier removal
370 * wait for us to complete (or bad things may happen).
372 * Links with the DL_FLAG_STATELESS flag set are ignored.
374 int device_links_check_suppliers(struct device *dev)
376 struct device_link *link;
379 device_links_write_lock();
381 list_for_each_entry(link, &dev->links.suppliers, c_node) {
382 if (link->flags & DL_FLAG_STATELESS)
385 if (link->status != DL_STATE_AVAILABLE) {
386 device_links_missing_supplier(dev);
390 WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE);
392 dev->links.status = DL_DEV_PROBING;
394 device_links_write_unlock();
399 * device_links_driver_bound - Update device links after probing its driver.
400 * @dev: Device to update the links for.
402 * The probe has been successful, so update links from this device to any
403 * consumers by changing their status to "available".
405 * Also change the status of @dev's links to suppliers to "active".
407 * Links with the DL_FLAG_STATELESS flag set are ignored.
409 void device_links_driver_bound(struct device *dev)
411 struct device_link *link;
413 device_links_write_lock();
415 list_for_each_entry(link, &dev->links.consumers, s_node) {
416 if (link->flags & DL_FLAG_STATELESS)
419 WARN_ON(link->status != DL_STATE_DORMANT);
420 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
423 list_for_each_entry(link, &dev->links.suppliers, c_node) {
424 if (link->flags & DL_FLAG_STATELESS)
427 WARN_ON(link->status != DL_STATE_CONSUMER_PROBE);
428 WRITE_ONCE(link->status, DL_STATE_ACTIVE);
431 dev->links.status = DL_DEV_DRIVER_BOUND;
433 device_links_write_unlock();
437 * __device_links_no_driver - Update links of a device without a driver.
438 * @dev: Device without a drvier.
440 * Delete all non-persistent links from this device to any suppliers.
442 * Persistent links stay around, but their status is changed to "available",
443 * unless they already are in the "supplier unbind in progress" state in which
444 * case they need not be updated.
446 * Links with the DL_FLAG_STATELESS flag set are ignored.
448 static void __device_links_no_driver(struct device *dev)
450 struct device_link *link, *ln;
452 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
453 if (link->flags & DL_FLAG_STATELESS)
456 if (link->flags & DL_FLAG_AUTOREMOVE)
457 __device_link_del(link);
458 else if (link->status != DL_STATE_SUPPLIER_UNBIND)
459 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
462 dev->links.status = DL_DEV_NO_DRIVER;
465 void device_links_no_driver(struct device *dev)
467 device_links_write_lock();
468 __device_links_no_driver(dev);
469 device_links_write_unlock();
473 * device_links_driver_cleanup - Update links after driver removal.
474 * @dev: Device whose driver has just gone away.
476 * Update links to consumers for @dev by changing their status to "dormant" and
477 * invoke %__device_links_no_driver() to update links to suppliers for it as
480 * Links with the DL_FLAG_STATELESS flag set are ignored.
482 void device_links_driver_cleanup(struct device *dev)
484 struct device_link *link;
486 device_links_write_lock();
488 list_for_each_entry(link, &dev->links.consumers, s_node) {
489 if (link->flags & DL_FLAG_STATELESS)
492 WARN_ON(link->flags & DL_FLAG_AUTOREMOVE);
493 WARN_ON(link->status != DL_STATE_SUPPLIER_UNBIND);
494 WRITE_ONCE(link->status, DL_STATE_DORMANT);
497 __device_links_no_driver(dev);
499 device_links_write_unlock();
503 * device_links_busy - Check if there are any busy links to consumers.
504 * @dev: Device to check.
506 * Check each consumer of the device and return 'true' if its link's status
507 * is one of "consumer probe" or "active" (meaning that the given consumer is
508 * probing right now or its driver is present). Otherwise, change the link
509 * state to "supplier unbind" to prevent the consumer from being probed
510 * successfully going forward.
512 * Return 'false' if there are no probing or active consumers.
514 * Links with the DL_FLAG_STATELESS flag set are ignored.
516 bool device_links_busy(struct device *dev)
518 struct device_link *link;
521 device_links_write_lock();
523 list_for_each_entry(link, &dev->links.consumers, s_node) {
524 if (link->flags & DL_FLAG_STATELESS)
527 if (link->status == DL_STATE_CONSUMER_PROBE
528 || link->status == DL_STATE_ACTIVE) {
532 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
535 dev->links.status = DL_DEV_UNBINDING;
537 device_links_write_unlock();
542 * device_links_unbind_consumers - Force unbind consumers of the given device.
543 * @dev: Device to unbind the consumers of.
545 * Walk the list of links to consumers for @dev and if any of them is in the
546 * "consumer probe" state, wait for all device probes in progress to complete
549 * If that's not the case, change the status of the link to "supplier unbind"
550 * and check if the link was in the "active" state. If so, force the consumer
551 * driver to unbind and start over (the consumer will not re-probe as we have
552 * changed the state of the link already).
554 * Links with the DL_FLAG_STATELESS flag set are ignored.
556 void device_links_unbind_consumers(struct device *dev)
558 struct device_link *link;
561 device_links_write_lock();
563 list_for_each_entry(link, &dev->links.consumers, s_node) {
564 enum device_link_state status;
566 if (link->flags & DL_FLAG_STATELESS)
569 status = link->status;
570 if (status == DL_STATE_CONSUMER_PROBE) {
571 device_links_write_unlock();
573 wait_for_device_probe();
576 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
577 if (status == DL_STATE_ACTIVE) {
578 struct device *consumer = link->consumer;
580 get_device(consumer);
582 device_links_write_unlock();
584 device_release_driver_internal(consumer, NULL,
586 put_device(consumer);
591 device_links_write_unlock();
595 * device_links_purge - Delete existing links to other devices.
596 * @dev: Target device.
598 static void device_links_purge(struct device *dev)
600 struct device_link *link, *ln;
603 * Delete all of the remaining links from this device to any other
604 * devices (either consumers or suppliers).
606 device_links_write_lock();
608 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
609 WARN_ON(link->status == DL_STATE_ACTIVE);
610 __device_link_del(link);
613 list_for_each_entry_safe_reverse(link, ln, &dev->links.consumers, s_node) {
614 WARN_ON(link->status != DL_STATE_DORMANT &&
615 link->status != DL_STATE_NONE);
616 __device_link_del(link);
619 device_links_write_unlock();
622 /* Device links support end. */
624 int (*platform_notify)(struct device *dev) = NULL;
625 int (*platform_notify_remove)(struct device *dev) = NULL;
626 static struct kobject *dev_kobj;
627 struct kobject *sysfs_dev_char_kobj;
628 struct kobject *sysfs_dev_block_kobj;
630 static DEFINE_MUTEX(device_hotplug_lock);
632 void lock_device_hotplug(void)
634 mutex_lock(&device_hotplug_lock);
637 void unlock_device_hotplug(void)
639 mutex_unlock(&device_hotplug_lock);
642 int lock_device_hotplug_sysfs(void)
644 if (mutex_trylock(&device_hotplug_lock))
647 /* Avoid busy looping (5 ms of sleep should do). */
649 return restart_syscall();
653 static inline int device_is_not_partition(struct device *dev)
655 return !(dev->type == &part_type);
658 static inline int device_is_not_partition(struct device *dev)
665 * dev_driver_string - Return a device's driver name, if at all possible
666 * @dev: struct device to get the name of
668 * Will return the device's driver's name if it is bound to a device. If
669 * the device is not bound to a driver, it will return the name of the bus
670 * it is attached to. If it is not attached to a bus either, an empty
671 * string will be returned.
673 const char *dev_driver_string(const struct device *dev)
675 struct device_driver *drv;
677 /* dev->driver can change to NULL underneath us because of unbinding,
678 * so be careful about accessing it. dev->bus and dev->class should
679 * never change once they are set, so they don't need special care.
681 drv = ACCESS_ONCE(dev->driver);
682 return drv ? drv->name :
683 (dev->bus ? dev->bus->name :
684 (dev->class ? dev->class->name : ""));
686 EXPORT_SYMBOL(dev_driver_string);
688 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
690 static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
693 struct device_attribute *dev_attr = to_dev_attr(attr);
694 struct device *dev = kobj_to_dev(kobj);
698 ret = dev_attr->show(dev, dev_attr, buf);
699 if (ret >= (ssize_t)PAGE_SIZE) {
700 print_symbol("dev_attr_show: %s returned bad count\n",
701 (unsigned long)dev_attr->show);
706 static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
707 const char *buf, size_t count)
709 struct device_attribute *dev_attr = to_dev_attr(attr);
710 struct device *dev = kobj_to_dev(kobj);
714 ret = dev_attr->store(dev, dev_attr, buf, count);
718 static const struct sysfs_ops dev_sysfs_ops = {
719 .show = dev_attr_show,
720 .store = dev_attr_store,
723 #define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
725 ssize_t device_store_ulong(struct device *dev,
726 struct device_attribute *attr,
727 const char *buf, size_t size)
729 struct dev_ext_attribute *ea = to_ext_attr(attr);
731 unsigned long new = simple_strtoul(buf, &end, 0);
734 *(unsigned long *)(ea->var) = new;
735 /* Always return full write size even if we didn't consume all */
738 EXPORT_SYMBOL_GPL(device_store_ulong);
740 ssize_t device_show_ulong(struct device *dev,
741 struct device_attribute *attr,
744 struct dev_ext_attribute *ea = to_ext_attr(attr);
745 return snprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var));
747 EXPORT_SYMBOL_GPL(device_show_ulong);
749 ssize_t device_store_int(struct device *dev,
750 struct device_attribute *attr,
751 const char *buf, size_t size)
753 struct dev_ext_attribute *ea = to_ext_attr(attr);
755 long new = simple_strtol(buf, &end, 0);
756 if (end == buf || new > INT_MAX || new < INT_MIN)
758 *(int *)(ea->var) = new;
759 /* Always return full write size even if we didn't consume all */
762 EXPORT_SYMBOL_GPL(device_store_int);
764 ssize_t device_show_int(struct device *dev,
765 struct device_attribute *attr,
768 struct dev_ext_attribute *ea = to_ext_attr(attr);
770 return snprintf(buf, PAGE_SIZE, "%d\n", *(int *)(ea->var));
772 EXPORT_SYMBOL_GPL(device_show_int);
774 ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
775 const char *buf, size_t size)
777 struct dev_ext_attribute *ea = to_ext_attr(attr);
779 if (strtobool(buf, ea->var) < 0)
784 EXPORT_SYMBOL_GPL(device_store_bool);
786 ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
789 struct dev_ext_attribute *ea = to_ext_attr(attr);
791 return snprintf(buf, PAGE_SIZE, "%d\n", *(bool *)(ea->var));
793 EXPORT_SYMBOL_GPL(device_show_bool);
796 * device_release - free device structure.
797 * @kobj: device's kobject.
799 * This is called once the reference count for the object
800 * reaches 0. We forward the call to the device's release
801 * method, which should handle actually freeing the structure.
803 static void device_release(struct kobject *kobj)
805 struct device *dev = kobj_to_dev(kobj);
806 struct device_private *p = dev->p;
809 * Some platform devices are driven without driver attached
810 * and managed resources may have been acquired. Make sure
811 * all resources are released.
813 * Drivers still can add resources into device after device
814 * is deleted but alive, so release devres here to avoid
815 * possible memory leak.
817 devres_release_all(dev);
821 else if (dev->type && dev->type->release)
822 dev->type->release(dev);
823 else if (dev->class && dev->class->dev_release)
824 dev->class->dev_release(dev);
826 WARN(1, KERN_ERR "Device '%s' does not have a release() "
827 "function, it is broken and must be fixed.\n",
832 static const void *device_namespace(struct kobject *kobj)
834 struct device *dev = kobj_to_dev(kobj);
835 const void *ns = NULL;
837 if (dev->class && dev->class->ns_type)
838 ns = dev->class->namespace(dev);
843 static struct kobj_type device_ktype = {
844 .release = device_release,
845 .sysfs_ops = &dev_sysfs_ops,
846 .namespace = device_namespace,
850 static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
852 struct kobj_type *ktype = get_ktype(kobj);
854 if (ktype == &device_ktype) {
855 struct device *dev = kobj_to_dev(kobj);
864 static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
866 struct device *dev = kobj_to_dev(kobj);
869 return dev->bus->name;
871 return dev->class->name;
875 static int dev_uevent(struct kset *kset, struct kobject *kobj,
876 struct kobj_uevent_env *env)
878 struct device *dev = kobj_to_dev(kobj);
881 /* add device node properties if present */
882 if (MAJOR(dev->devt)) {
886 kuid_t uid = GLOBAL_ROOT_UID;
887 kgid_t gid = GLOBAL_ROOT_GID;
889 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
890 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
891 name = device_get_devnode(dev, &mode, &uid, &gid, &tmp);
893 add_uevent_var(env, "DEVNAME=%s", name);
895 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
896 if (!uid_eq(uid, GLOBAL_ROOT_UID))
897 add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid));
898 if (!gid_eq(gid, GLOBAL_ROOT_GID))
899 add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid));
904 if (dev->type && dev->type->name)
905 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
908 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
910 /* Add common DT information about the device */
911 of_device_uevent(dev, env);
913 /* have the bus specific function add its stuff */
914 if (dev->bus && dev->bus->uevent) {
915 retval = dev->bus->uevent(dev, env);
917 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
918 dev_name(dev), __func__, retval);
921 /* have the class specific function add its stuff */
922 if (dev->class && dev->class->dev_uevent) {
923 retval = dev->class->dev_uevent(dev, env);
925 pr_debug("device: '%s': %s: class uevent() "
926 "returned %d\n", dev_name(dev),
930 /* have the device type specific function add its stuff */
931 if (dev->type && dev->type->uevent) {
932 retval = dev->type->uevent(dev, env);
934 pr_debug("device: '%s': %s: dev_type uevent() "
935 "returned %d\n", dev_name(dev),
942 static const struct kset_uevent_ops device_uevent_ops = {
943 .filter = dev_uevent_filter,
944 .name = dev_uevent_name,
945 .uevent = dev_uevent,
948 static ssize_t uevent_show(struct device *dev, struct device_attribute *attr,
951 struct kobject *top_kobj;
953 struct kobj_uevent_env *env = NULL;
958 /* search the kset, the device belongs to */
959 top_kobj = &dev->kobj;
960 while (!top_kobj->kset && top_kobj->parent)
961 top_kobj = top_kobj->parent;
965 kset = top_kobj->kset;
966 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
970 if (kset->uevent_ops && kset->uevent_ops->filter)
971 if (!kset->uevent_ops->filter(kset, &dev->kobj))
974 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
978 /* let the kset specific function add its keys */
979 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
983 /* copy keys to file */
984 for (i = 0; i < env->envp_idx; i++)
985 count += sprintf(&buf[count], "%s\n", env->envp[i]);
991 static ssize_t uevent_store(struct device *dev, struct device_attribute *attr,
992 const char *buf, size_t count)
994 if (kobject_synth_uevent(&dev->kobj, buf, count))
995 dev_err(dev, "uevent: failed to send synthetic uevent\n");
999 static DEVICE_ATTR_RW(uevent);
1001 static ssize_t online_show(struct device *dev, struct device_attribute *attr,
1007 val = !dev->offline;
1009 return sprintf(buf, "%u\n", val);
1012 static ssize_t online_store(struct device *dev, struct device_attribute *attr,
1013 const char *buf, size_t count)
1018 ret = strtobool(buf, &val);
1022 ret = lock_device_hotplug_sysfs();
1026 ret = val ? device_online(dev) : device_offline(dev);
1027 unlock_device_hotplug();
1028 return ret < 0 ? ret : count;
1030 static DEVICE_ATTR_RW(online);
1032 int device_add_groups(struct device *dev, const struct attribute_group **groups)
1034 return sysfs_create_groups(&dev->kobj, groups);
1036 EXPORT_SYMBOL_GPL(device_add_groups);
1038 void device_remove_groups(struct device *dev,
1039 const struct attribute_group **groups)
1041 sysfs_remove_groups(&dev->kobj, groups);
1043 EXPORT_SYMBOL_GPL(device_remove_groups);
1045 union device_attr_group_devres {
1046 const struct attribute_group *group;
1047 const struct attribute_group **groups;
1050 static int devm_attr_group_match(struct device *dev, void *res, void *data)
1052 return ((union device_attr_group_devres *)res)->group == data;
1055 static void devm_attr_group_remove(struct device *dev, void *res)
1057 union device_attr_group_devres *devres = res;
1058 const struct attribute_group *group = devres->group;
1060 dev_dbg(dev, "%s: removing group %p\n", __func__, group);
1061 sysfs_remove_group(&dev->kobj, group);
1064 static void devm_attr_groups_remove(struct device *dev, void *res)
1066 union device_attr_group_devres *devres = res;
1067 const struct attribute_group **groups = devres->groups;
1069 dev_dbg(dev, "%s: removing groups %p\n", __func__, groups);
1070 sysfs_remove_groups(&dev->kobj, groups);
1074 * devm_device_add_group - given a device, create a managed attribute group
1075 * @dev: The device to create the group for
1076 * @grp: The attribute group to create
1078 * This function creates a group for the first time. It will explicitly
1079 * warn and error if any of the attribute files being created already exist.
1081 * Returns 0 on success or error code on failure.
1083 int devm_device_add_group(struct device *dev, const struct attribute_group *grp)
1085 union device_attr_group_devres *devres;
1088 devres = devres_alloc(devm_attr_group_remove,
1089 sizeof(*devres), GFP_KERNEL);
1093 error = sysfs_create_group(&dev->kobj, grp);
1095 devres_free(devres);
1099 devres->group = grp;
1100 devres_add(dev, devres);
1103 EXPORT_SYMBOL_GPL(devm_device_add_group);
1106 * devm_device_remove_group: remove a managed group from a device
1107 * @dev: device to remove the group from
1108 * @grp: group to remove
1110 * This function removes a group of attributes from a device. The attributes
1111 * previously have to have been created for this group, otherwise it will fail.
1113 void devm_device_remove_group(struct device *dev,
1114 const struct attribute_group *grp)
1116 WARN_ON(devres_release(dev, devm_attr_group_remove,
1117 devm_attr_group_match,
1118 /* cast away const */ (void *)grp));
1120 EXPORT_SYMBOL_GPL(devm_device_remove_group);
1123 * devm_device_add_groups - create a bunch of managed attribute groups
1124 * @dev: The device to create the group for
1125 * @groups: The attribute groups to create, NULL terminated
1127 * This function creates a bunch of managed attribute groups. If an error
1128 * occurs when creating a group, all previously created groups will be
1129 * removed, unwinding everything back to the original state when this
1130 * function was called. It will explicitly warn and error if any of the
1131 * attribute files being created already exist.
1133 * Returns 0 on success or error code from sysfs_create_group on failure.
1135 int devm_device_add_groups(struct device *dev,
1136 const struct attribute_group **groups)
1138 union device_attr_group_devres *devres;
1141 devres = devres_alloc(devm_attr_groups_remove,
1142 sizeof(*devres), GFP_KERNEL);
1146 error = sysfs_create_groups(&dev->kobj, groups);
1148 devres_free(devres);
1152 devres->groups = groups;
1153 devres_add(dev, devres);
1156 EXPORT_SYMBOL_GPL(devm_device_add_groups);
1159 * devm_device_remove_groups - remove a list of managed groups
1161 * @dev: The device for the groups to be removed from
1162 * @groups: NULL terminated list of groups to be removed
1164 * If groups is not NULL, remove the specified groups from the device.
1166 void devm_device_remove_groups(struct device *dev,
1167 const struct attribute_group **groups)
1169 WARN_ON(devres_release(dev, devm_attr_groups_remove,
1170 devm_attr_group_match,
1171 /* cast away const */ (void *)groups));
1173 EXPORT_SYMBOL_GPL(devm_device_remove_groups);
1175 static int device_add_attrs(struct device *dev)
1177 struct class *class = dev->class;
1178 const struct device_type *type = dev->type;
1182 error = device_add_groups(dev, class->dev_groups);
1188 error = device_add_groups(dev, type->groups);
1190 goto err_remove_class_groups;
1193 error = device_add_groups(dev, dev->groups);
1195 goto err_remove_type_groups;
1197 if (device_supports_offline(dev) && !dev->offline_disabled) {
1198 error = device_create_file(dev, &dev_attr_online);
1200 goto err_remove_dev_groups;
1205 err_remove_dev_groups:
1206 device_remove_groups(dev, dev->groups);
1207 err_remove_type_groups:
1209 device_remove_groups(dev, type->groups);
1210 err_remove_class_groups:
1212 device_remove_groups(dev, class->dev_groups);
1217 static void device_remove_attrs(struct device *dev)
1219 struct class *class = dev->class;
1220 const struct device_type *type = dev->type;
1222 device_remove_file(dev, &dev_attr_online);
1223 device_remove_groups(dev, dev->groups);
1226 device_remove_groups(dev, type->groups);
1229 device_remove_groups(dev, class->dev_groups);
1232 static ssize_t dev_show(struct device *dev, struct device_attribute *attr,
1235 return print_dev_t(buf, dev->devt);
1237 static DEVICE_ATTR_RO(dev);
1240 struct kset *devices_kset;
1243 * devices_kset_move_before - Move device in the devices_kset's list.
1244 * @deva: Device to move.
1245 * @devb: Device @deva should come before.
1247 static void devices_kset_move_before(struct device *deva, struct device *devb)
1251 pr_debug("devices_kset: Moving %s before %s\n",
1252 dev_name(deva), dev_name(devb));
1253 spin_lock(&devices_kset->list_lock);
1254 list_move_tail(&deva->kobj.entry, &devb->kobj.entry);
1255 spin_unlock(&devices_kset->list_lock);
1259 * devices_kset_move_after - Move device in the devices_kset's list.
1260 * @deva: Device to move
1261 * @devb: Device @deva should come after.
1263 static void devices_kset_move_after(struct device *deva, struct device *devb)
1267 pr_debug("devices_kset: Moving %s after %s\n",
1268 dev_name(deva), dev_name(devb));
1269 spin_lock(&devices_kset->list_lock);
1270 list_move(&deva->kobj.entry, &devb->kobj.entry);
1271 spin_unlock(&devices_kset->list_lock);
1275 * devices_kset_move_last - move the device to the end of devices_kset's list.
1276 * @dev: device to move
1278 void devices_kset_move_last(struct device *dev)
1282 pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev));
1283 spin_lock(&devices_kset->list_lock);
1284 list_move_tail(&dev->kobj.entry, &devices_kset->list);
1285 spin_unlock(&devices_kset->list_lock);
1289 * device_create_file - create sysfs attribute file for device.
1291 * @attr: device attribute descriptor.
1293 int device_create_file(struct device *dev,
1294 const struct device_attribute *attr)
1299 WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
1300 "Attribute %s: write permission without 'store'\n",
1302 WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
1303 "Attribute %s: read permission without 'show'\n",
1305 error = sysfs_create_file(&dev->kobj, &attr->attr);
1310 EXPORT_SYMBOL_GPL(device_create_file);
1313 * device_remove_file - remove sysfs attribute file.
1315 * @attr: device attribute descriptor.
1317 void device_remove_file(struct device *dev,
1318 const struct device_attribute *attr)
1321 sysfs_remove_file(&dev->kobj, &attr->attr);
1323 EXPORT_SYMBOL_GPL(device_remove_file);
1326 * device_remove_file_self - remove sysfs attribute file from its own method.
1328 * @attr: device attribute descriptor.
1330 * See kernfs_remove_self() for details.
1332 bool device_remove_file_self(struct device *dev,
1333 const struct device_attribute *attr)
1336 return sysfs_remove_file_self(&dev->kobj, &attr->attr);
1340 EXPORT_SYMBOL_GPL(device_remove_file_self);
1343 * device_create_bin_file - create sysfs binary attribute file for device.
1345 * @attr: device binary attribute descriptor.
1347 int device_create_bin_file(struct device *dev,
1348 const struct bin_attribute *attr)
1350 int error = -EINVAL;
1352 error = sysfs_create_bin_file(&dev->kobj, attr);
1355 EXPORT_SYMBOL_GPL(device_create_bin_file);
1358 * device_remove_bin_file - remove sysfs binary attribute file
1360 * @attr: device binary attribute descriptor.
1362 void device_remove_bin_file(struct device *dev,
1363 const struct bin_attribute *attr)
1366 sysfs_remove_bin_file(&dev->kobj, attr);
1368 EXPORT_SYMBOL_GPL(device_remove_bin_file);
1370 static void klist_children_get(struct klist_node *n)
1372 struct device_private *p = to_device_private_parent(n);
1373 struct device *dev = p->device;
1378 static void klist_children_put(struct klist_node *n)
1380 struct device_private *p = to_device_private_parent(n);
1381 struct device *dev = p->device;
1387 * device_initialize - init device structure.
1390 * This prepares the device for use by other layers by initializing
1392 * It is the first half of device_register(), if called by
1393 * that function, though it can also be called separately, so one
1394 * may use @dev's fields. In particular, get_device()/put_device()
1395 * may be used for reference counting of @dev after calling this
1398 * All fields in @dev must be initialized by the caller to 0, except
1399 * for those explicitly set to some other value. The simplest
1400 * approach is to use kzalloc() to allocate the structure containing
1403 * NOTE: Use put_device() to give up your reference instead of freeing
1404 * @dev directly once you have called this function.
1406 void device_initialize(struct device *dev)
1408 dev->kobj.kset = devices_kset;
1409 kobject_init(&dev->kobj, &device_ktype);
1410 INIT_LIST_HEAD(&dev->dma_pools);
1411 mutex_init(&dev->mutex);
1412 lockdep_set_novalidate_class(&dev->mutex);
1413 spin_lock_init(&dev->devres_lock);
1414 INIT_LIST_HEAD(&dev->devres_head);
1415 device_pm_init(dev);
1416 set_dev_node(dev, -1);
1417 #ifdef CONFIG_GENERIC_MSI_IRQ
1418 INIT_LIST_HEAD(&dev->msi_list);
1420 INIT_LIST_HEAD(&dev->links.consumers);
1421 INIT_LIST_HEAD(&dev->links.suppliers);
1422 dev->links.status = DL_DEV_NO_DRIVER;
1424 EXPORT_SYMBOL_GPL(device_initialize);
1426 struct kobject *virtual_device_parent(struct device *dev)
1428 static struct kobject *virtual_dir = NULL;
1431 virtual_dir = kobject_create_and_add("virtual",
1432 &devices_kset->kobj);
1438 struct kobject kobj;
1439 struct class *class;
1442 #define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
1444 static void class_dir_release(struct kobject *kobj)
1446 struct class_dir *dir = to_class_dir(kobj);
1451 struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
1453 struct class_dir *dir = to_class_dir(kobj);
1454 return dir->class->ns_type;
1457 static struct kobj_type class_dir_ktype = {
1458 .release = class_dir_release,
1459 .sysfs_ops = &kobj_sysfs_ops,
1460 .child_ns_type = class_dir_child_ns_type
1463 static struct kobject *
1464 class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
1466 struct class_dir *dir;
1469 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1471 return ERR_PTR(-ENOMEM);
1474 kobject_init(&dir->kobj, &class_dir_ktype);
1476 dir->kobj.kset = &class->p->glue_dirs;
1478 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
1480 kobject_put(&dir->kobj);
1481 return ERR_PTR(retval);
1486 static DEFINE_MUTEX(gdp_mutex);
1488 static struct kobject *get_device_parent(struct device *dev,
1489 struct device *parent)
1492 struct kobject *kobj = NULL;
1493 struct kobject *parent_kobj;
1497 /* block disks show up in /sys/block */
1498 if (sysfs_deprecated && dev->class == &block_class) {
1499 if (parent && parent->class == &block_class)
1500 return &parent->kobj;
1501 return &block_class.p->subsys.kobj;
1506 * If we have no parent, we live in "virtual".
1507 * Class-devices with a non class-device as parent, live
1508 * in a "glue" directory to prevent namespace collisions.
1511 parent_kobj = virtual_device_parent(dev);
1512 else if (parent->class && !dev->class->ns_type)
1513 return &parent->kobj;
1515 parent_kobj = &parent->kobj;
1517 mutex_lock(&gdp_mutex);
1519 /* find our class-directory at the parent and reference it */
1520 spin_lock(&dev->class->p->glue_dirs.list_lock);
1521 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
1522 if (k->parent == parent_kobj) {
1523 kobj = kobject_get(k);
1526 spin_unlock(&dev->class->p->glue_dirs.list_lock);
1528 mutex_unlock(&gdp_mutex);
1532 /* or create a new class-directory at the parent device */
1533 k = class_dir_create_and_add(dev->class, parent_kobj);
1534 /* do not emit an uevent for this simple "glue" directory */
1535 mutex_unlock(&gdp_mutex);
1539 /* subsystems can specify a default root directory for their devices */
1540 if (!parent && dev->bus && dev->bus->dev_root)
1541 return &dev->bus->dev_root->kobj;
1544 return &parent->kobj;
1548 static inline bool live_in_glue_dir(struct kobject *kobj,
1551 if (!kobj || !dev->class ||
1552 kobj->kset != &dev->class->p->glue_dirs)
1557 static inline struct kobject *get_glue_dir(struct device *dev)
1559 return dev->kobj.parent;
1563 * make sure cleaning up dir as the last step, we need to make
1564 * sure .release handler of kobject is run with holding the
1567 static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
1569 /* see if we live in a "glue" directory */
1570 if (!live_in_glue_dir(glue_dir, dev))
1573 mutex_lock(&gdp_mutex);
1574 if (!kobject_has_children(glue_dir))
1575 kobject_del(glue_dir);
1576 kobject_put(glue_dir);
1577 mutex_unlock(&gdp_mutex);
1580 static int device_add_class_symlinks(struct device *dev)
1582 struct device_node *of_node = dev_of_node(dev);
1586 error = sysfs_create_link(&dev->kobj, &of_node->kobj,"of_node");
1588 dev_warn(dev, "Error %d creating of_node link\n",error);
1589 /* An error here doesn't warrant bringing down the device */
1595 error = sysfs_create_link(&dev->kobj,
1596 &dev->class->p->subsys.kobj,
1601 if (dev->parent && device_is_not_partition(dev)) {
1602 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
1609 /* /sys/block has directories and does not need symlinks */
1610 if (sysfs_deprecated && dev->class == &block_class)
1614 /* link in the class directory pointing to the device */
1615 error = sysfs_create_link(&dev->class->p->subsys.kobj,
1616 &dev->kobj, dev_name(dev));
1623 sysfs_remove_link(&dev->kobj, "device");
1626 sysfs_remove_link(&dev->kobj, "subsystem");
1628 sysfs_remove_link(&dev->kobj, "of_node");
1632 static void device_remove_class_symlinks(struct device *dev)
1634 if (dev_of_node(dev))
1635 sysfs_remove_link(&dev->kobj, "of_node");
1640 if (dev->parent && device_is_not_partition(dev))
1641 sysfs_remove_link(&dev->kobj, "device");
1642 sysfs_remove_link(&dev->kobj, "subsystem");
1644 if (sysfs_deprecated && dev->class == &block_class)
1647 sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
1651 * dev_set_name - set a device name
1653 * @fmt: format string for the device's name
1655 int dev_set_name(struct device *dev, const char *fmt, ...)
1660 va_start(vargs, fmt);
1661 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
1665 EXPORT_SYMBOL_GPL(dev_set_name);
1668 * device_to_dev_kobj - select a /sys/dev/ directory for the device
1671 * By default we select char/ for new entries. Setting class->dev_obj
1672 * to NULL prevents an entry from being created. class->dev_kobj must
1673 * be set (or cleared) before any devices are registered to the class
1674 * otherwise device_create_sys_dev_entry() and
1675 * device_remove_sys_dev_entry() will disagree about the presence of
1678 static struct kobject *device_to_dev_kobj(struct device *dev)
1680 struct kobject *kobj;
1683 kobj = dev->class->dev_kobj;
1685 kobj = sysfs_dev_char_kobj;
1690 static int device_create_sys_dev_entry(struct device *dev)
1692 struct kobject *kobj = device_to_dev_kobj(dev);
1697 format_dev_t(devt_str, dev->devt);
1698 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
1704 static void device_remove_sys_dev_entry(struct device *dev)
1706 struct kobject *kobj = device_to_dev_kobj(dev);
1710 format_dev_t(devt_str, dev->devt);
1711 sysfs_remove_link(kobj, devt_str);
1715 int device_private_init(struct device *dev)
1717 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
1720 dev->p->device = dev;
1721 klist_init(&dev->p->klist_children, klist_children_get,
1722 klist_children_put);
1723 INIT_LIST_HEAD(&dev->p->deferred_probe);
1728 * device_add - add device to device hierarchy.
1731 * This is part 2 of device_register(), though may be called
1732 * separately _iff_ device_initialize() has been called separately.
1734 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
1735 * to the global and sibling lists for the device, then
1736 * adds it to the other relevant subsystems of the driver model.
1738 * Do not call this routine or device_register() more than once for
1739 * any device structure. The driver model core is not designed to work
1740 * with devices that get unregistered and then spring back to life.
1741 * (Among other things, it's very hard to guarantee that all references
1742 * to the previous incarnation of @dev have been dropped.) Allocate
1743 * and register a fresh new struct device instead.
1745 * NOTE: _Never_ directly free @dev after calling this function, even
1746 * if it returned an error! Always use put_device() to give up your
1747 * reference instead.
1749 int device_add(struct device *dev)
1751 struct device *parent;
1752 struct kobject *kobj;
1753 struct class_interface *class_intf;
1754 int error = -EINVAL;
1755 struct kobject *glue_dir = NULL;
1757 dev = get_device(dev);
1762 error = device_private_init(dev);
1768 * for statically allocated devices, which should all be converted
1769 * some day, we need to initialize the name. We prevent reading back
1770 * the name, and force the use of dev_name()
1772 if (dev->init_name) {
1773 dev_set_name(dev, "%s", dev->init_name);
1774 dev->init_name = NULL;
1777 /* subsystems can specify simple device enumeration */
1778 if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
1779 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
1781 if (!dev_name(dev)) {
1786 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
1788 parent = get_device(dev->parent);
1789 kobj = get_device_parent(dev, parent);
1791 error = PTR_ERR(kobj);
1795 dev->kobj.parent = kobj;
1797 /* use parent numa_node */
1798 if (parent && (dev_to_node(dev) == NUMA_NO_NODE))
1799 set_dev_node(dev, dev_to_node(parent));
1801 /* first, register with generic layer. */
1802 /* we require the name to be set before, and pass NULL */
1803 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
1805 glue_dir = get_glue_dir(dev);
1809 /* notify platform of device entry */
1810 if (platform_notify)
1811 platform_notify(dev);
1813 error = device_create_file(dev, &dev_attr_uevent);
1817 error = device_add_class_symlinks(dev);
1820 error = device_add_attrs(dev);
1823 error = bus_add_device(dev);
1826 error = dpm_sysfs_add(dev);
1831 if (MAJOR(dev->devt)) {
1832 error = device_create_file(dev, &dev_attr_dev);
1836 error = device_create_sys_dev_entry(dev);
1840 devtmpfs_create_node(dev);
1843 /* Notify clients of device addition. This call must come
1844 * after dpm_sysfs_add() and before kobject_uevent().
1847 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1848 BUS_NOTIFY_ADD_DEVICE, dev);
1850 kobject_uevent(&dev->kobj, KOBJ_ADD);
1851 bus_probe_device(dev);
1853 klist_add_tail(&dev->p->knode_parent,
1854 &parent->p->klist_children);
1857 mutex_lock(&dev->class->p->mutex);
1858 /* tie the class to the device */
1859 klist_add_tail(&dev->knode_class,
1860 &dev->class->p->klist_devices);
1862 /* notify any interfaces that the device is here */
1863 list_for_each_entry(class_intf,
1864 &dev->class->p->interfaces, node)
1865 if (class_intf->add_dev)
1866 class_intf->add_dev(dev, class_intf);
1867 mutex_unlock(&dev->class->p->mutex);
1873 if (MAJOR(dev->devt))
1874 device_remove_file(dev, &dev_attr_dev);
1876 device_pm_remove(dev);
1877 dpm_sysfs_remove(dev);
1879 bus_remove_device(dev);
1881 device_remove_attrs(dev);
1883 device_remove_class_symlinks(dev);
1885 device_remove_file(dev, &dev_attr_uevent);
1887 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
1888 glue_dir = get_glue_dir(dev);
1889 kobject_del(&dev->kobj);
1891 cleanup_glue_dir(dev, glue_dir);
1899 EXPORT_SYMBOL_GPL(device_add);
1902 * device_register - register a device with the system.
1903 * @dev: pointer to the device structure
1905 * This happens in two clean steps - initialize the device
1906 * and add it to the system. The two steps can be called
1907 * separately, but this is the easiest and most common.
1908 * I.e. you should only call the two helpers separately if
1909 * have a clearly defined need to use and refcount the device
1910 * before it is added to the hierarchy.
1912 * For more information, see the kerneldoc for device_initialize()
1915 * NOTE: _Never_ directly free @dev after calling this function, even
1916 * if it returned an error! Always use put_device() to give up the
1917 * reference initialized in this function instead.
1919 int device_register(struct device *dev)
1921 device_initialize(dev);
1922 return device_add(dev);
1924 EXPORT_SYMBOL_GPL(device_register);
1927 * get_device - increment reference count for device.
1930 * This simply forwards the call to kobject_get(), though
1931 * we do take care to provide for the case that we get a NULL
1932 * pointer passed in.
1934 struct device *get_device(struct device *dev)
1936 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
1938 EXPORT_SYMBOL_GPL(get_device);
1941 * put_device - decrement reference count.
1942 * @dev: device in question.
1944 void put_device(struct device *dev)
1946 /* might_sleep(); */
1948 kobject_put(&dev->kobj);
1950 EXPORT_SYMBOL_GPL(put_device);
1953 * device_del - delete device from system.
1956 * This is the first part of the device unregistration
1957 * sequence. This removes the device from the lists we control
1958 * from here, has it removed from the other driver model
1959 * subsystems it was added to in device_add(), and removes it
1960 * from the kobject hierarchy.
1962 * NOTE: this should be called manually _iff_ device_add() was
1963 * also called manually.
1965 void device_del(struct device *dev)
1967 struct device *parent = dev->parent;
1968 struct kobject *glue_dir = NULL;
1969 struct class_interface *class_intf;
1971 /* Notify clients of device removal. This call must come
1972 * before dpm_sysfs_remove().
1975 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1976 BUS_NOTIFY_DEL_DEVICE, dev);
1978 dpm_sysfs_remove(dev);
1980 klist_del(&dev->p->knode_parent);
1981 if (MAJOR(dev->devt)) {
1982 devtmpfs_delete_node(dev);
1983 device_remove_sys_dev_entry(dev);
1984 device_remove_file(dev, &dev_attr_dev);
1987 device_remove_class_symlinks(dev);
1989 mutex_lock(&dev->class->p->mutex);
1990 /* notify any interfaces that the device is now gone */
1991 list_for_each_entry(class_intf,
1992 &dev->class->p->interfaces, node)
1993 if (class_intf->remove_dev)
1994 class_intf->remove_dev(dev, class_intf);
1995 /* remove the device from the class list */
1996 klist_del(&dev->knode_class);
1997 mutex_unlock(&dev->class->p->mutex);
1999 device_remove_file(dev, &dev_attr_uevent);
2000 device_remove_attrs(dev);
2001 bus_remove_device(dev);
2002 device_pm_remove(dev);
2003 driver_deferred_probe_del(dev);
2004 device_remove_properties(dev);
2005 device_links_purge(dev);
2007 /* Notify the platform of the removal, in case they
2008 * need to do anything...
2010 if (platform_notify_remove)
2011 platform_notify_remove(dev);
2013 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2014 BUS_NOTIFY_REMOVED_DEVICE, dev);
2015 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
2016 glue_dir = get_glue_dir(dev);
2017 kobject_del(&dev->kobj);
2018 cleanup_glue_dir(dev, glue_dir);
2021 EXPORT_SYMBOL_GPL(device_del);
2024 * device_unregister - unregister device from system.
2025 * @dev: device going away.
2027 * We do this in two parts, like we do device_register(). First,
2028 * we remove it from all the subsystems with device_del(), then
2029 * we decrement the reference count via put_device(). If that
2030 * is the final reference count, the device will be cleaned up
2031 * via device_release() above. Otherwise, the structure will
2032 * stick around until the final reference to the device is dropped.
2034 void device_unregister(struct device *dev)
2036 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
2040 EXPORT_SYMBOL_GPL(device_unregister);
2042 static struct device *prev_device(struct klist_iter *i)
2044 struct klist_node *n = klist_prev(i);
2045 struct device *dev = NULL;
2046 struct device_private *p;
2049 p = to_device_private_parent(n);
2055 static struct device *next_device(struct klist_iter *i)
2057 struct klist_node *n = klist_next(i);
2058 struct device *dev = NULL;
2059 struct device_private *p;
2062 p = to_device_private_parent(n);
2069 * device_get_devnode - path of device node file
2071 * @mode: returned file access mode
2072 * @uid: returned file owner
2073 * @gid: returned file group
2074 * @tmp: possibly allocated string
2076 * Return the relative path of a possible device node.
2077 * Non-default names may need to allocate a memory to compose
2078 * a name. This memory is returned in tmp and needs to be
2079 * freed by the caller.
2081 const char *device_get_devnode(struct device *dev,
2082 umode_t *mode, kuid_t *uid, kgid_t *gid,
2089 /* the device type may provide a specific name */
2090 if (dev->type && dev->type->devnode)
2091 *tmp = dev->type->devnode(dev, mode, uid, gid);
2095 /* the class may provide a specific name */
2096 if (dev->class && dev->class->devnode)
2097 *tmp = dev->class->devnode(dev, mode);
2101 /* return name without allocation, tmp == NULL */
2102 if (strchr(dev_name(dev), '!') == NULL)
2103 return dev_name(dev);
2105 /* replace '!' in the name with '/' */
2106 s = kstrdup(dev_name(dev), GFP_KERNEL);
2109 strreplace(s, '!', '/');
2114 * device_for_each_child - device child iterator.
2115 * @parent: parent struct device.
2116 * @fn: function to be called for each device.
2117 * @data: data for the callback.
2119 * Iterate over @parent's child devices, and call @fn for each,
2122 * We check the return of @fn each time. If it returns anything
2123 * other than 0, we break out and return that value.
2125 int device_for_each_child(struct device *parent, void *data,
2126 int (*fn)(struct device *dev, void *data))
2128 struct klist_iter i;
2129 struct device *child;
2135 klist_iter_init(&parent->p->klist_children, &i);
2136 while ((child = next_device(&i)) && !error)
2137 error = fn(child, data);
2138 klist_iter_exit(&i);
2141 EXPORT_SYMBOL_GPL(device_for_each_child);
2144 * device_for_each_child_reverse - device child iterator in reversed order.
2145 * @parent: parent struct device.
2146 * @fn: function to be called for each device.
2147 * @data: data for the callback.
2149 * Iterate over @parent's child devices, and call @fn for each,
2152 * We check the return of @fn each time. If it returns anything
2153 * other than 0, we break out and return that value.
2155 int device_for_each_child_reverse(struct device *parent, void *data,
2156 int (*fn)(struct device *dev, void *data))
2158 struct klist_iter i;
2159 struct device *child;
2165 klist_iter_init(&parent->p->klist_children, &i);
2166 while ((child = prev_device(&i)) && !error)
2167 error = fn(child, data);
2168 klist_iter_exit(&i);
2171 EXPORT_SYMBOL_GPL(device_for_each_child_reverse);
2174 * device_find_child - device iterator for locating a particular device.
2175 * @parent: parent struct device
2176 * @match: Callback function to check device
2177 * @data: Data to pass to match function
2179 * This is similar to the device_for_each_child() function above, but it
2180 * returns a reference to a device that is 'found' for later use, as
2181 * determined by the @match callback.
2183 * The callback should return 0 if the device doesn't match and non-zero
2184 * if it does. If the callback returns non-zero and a reference to the
2185 * current device can be obtained, this function will return to the caller
2186 * and not iterate over any more devices.
2188 * NOTE: you will need to drop the reference with put_device() after use.
2190 struct device *device_find_child(struct device *parent, void *data,
2191 int (*match)(struct device *dev, void *data))
2193 struct klist_iter i;
2194 struct device *child;
2199 klist_iter_init(&parent->p->klist_children, &i);
2200 while ((child = next_device(&i)))
2201 if (match(child, data) && get_device(child))
2203 klist_iter_exit(&i);
2206 EXPORT_SYMBOL_GPL(device_find_child);
2208 int __init devices_init(void)
2210 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
2213 dev_kobj = kobject_create_and_add("dev", NULL);
2216 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
2217 if (!sysfs_dev_block_kobj)
2218 goto block_kobj_err;
2219 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
2220 if (!sysfs_dev_char_kobj)
2226 kobject_put(sysfs_dev_block_kobj);
2228 kobject_put(dev_kobj);
2230 kset_unregister(devices_kset);
2234 static int device_check_offline(struct device *dev, void *not_used)
2238 ret = device_for_each_child(dev, NULL, device_check_offline);
2242 return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
2246 * device_offline - Prepare the device for hot-removal.
2247 * @dev: Device to be put offline.
2249 * Execute the device bus type's .offline() callback, if present, to prepare
2250 * the device for a subsequent hot-removal. If that succeeds, the device must
2251 * not be used until either it is removed or its bus type's .online() callback
2254 * Call under device_hotplug_lock.
2256 int device_offline(struct device *dev)
2260 if (dev->offline_disabled)
2263 ret = device_for_each_child(dev, NULL, device_check_offline);
2268 if (device_supports_offline(dev)) {
2272 ret = dev->bus->offline(dev);
2274 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
2275 dev->offline = true;
2285 * device_online - Put the device back online after successful device_offline().
2286 * @dev: Device to be put back online.
2288 * If device_offline() has been successfully executed for @dev, but the device
2289 * has not been removed subsequently, execute its bus type's .online() callback
2290 * to indicate that the device can be used again.
2292 * Call under device_hotplug_lock.
2294 int device_online(struct device *dev)
2299 if (device_supports_offline(dev)) {
2301 ret = dev->bus->online(dev);
2303 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
2304 dev->offline = false;
2315 struct root_device {
2317 struct module *owner;
2320 static inline struct root_device *to_root_device(struct device *d)
2322 return container_of(d, struct root_device, dev);
2325 static void root_device_release(struct device *dev)
2327 kfree(to_root_device(dev));
2331 * __root_device_register - allocate and register a root device
2332 * @name: root device name
2333 * @owner: owner module of the root device, usually THIS_MODULE
2335 * This function allocates a root device and registers it
2336 * using device_register(). In order to free the returned
2337 * device, use root_device_unregister().
2339 * Root devices are dummy devices which allow other devices
2340 * to be grouped under /sys/devices. Use this function to
2341 * allocate a root device and then use it as the parent of
2342 * any device which should appear under /sys/devices/{name}
2344 * The /sys/devices/{name} directory will also contain a
2345 * 'module' symlink which points to the @owner directory
2348 * Returns &struct device pointer on success, or ERR_PTR() on error.
2350 * Note: You probably want to use root_device_register().
2352 struct device *__root_device_register(const char *name, struct module *owner)
2354 struct root_device *root;
2357 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
2359 return ERR_PTR(err);
2361 err = dev_set_name(&root->dev, "%s", name);
2364 return ERR_PTR(err);
2367 root->dev.release = root_device_release;
2369 err = device_register(&root->dev);
2371 put_device(&root->dev);
2372 return ERR_PTR(err);
2375 #ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
2377 struct module_kobject *mk = &owner->mkobj;
2379 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
2381 device_unregister(&root->dev);
2382 return ERR_PTR(err);
2384 root->owner = owner;
2390 EXPORT_SYMBOL_GPL(__root_device_register);
2393 * root_device_unregister - unregister and free a root device
2394 * @dev: device going away
2396 * This function unregisters and cleans up a device that was created by
2397 * root_device_register().
2399 void root_device_unregister(struct device *dev)
2401 struct root_device *root = to_root_device(dev);
2404 sysfs_remove_link(&root->dev.kobj, "module");
2406 device_unregister(dev);
2408 EXPORT_SYMBOL_GPL(root_device_unregister);
2411 static void device_create_release(struct device *dev)
2413 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
2417 static struct device *
2418 device_create_groups_vargs(struct class *class, struct device *parent,
2419 dev_t devt, void *drvdata,
2420 const struct attribute_group **groups,
2421 const char *fmt, va_list args)
2423 struct device *dev = NULL;
2424 int retval = -ENODEV;
2426 if (class == NULL || IS_ERR(class))
2429 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2435 device_initialize(dev);
2438 dev->parent = parent;
2439 dev->groups = groups;
2440 dev->release = device_create_release;
2441 dev_set_drvdata(dev, drvdata);
2443 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
2447 retval = device_add(dev);
2455 return ERR_PTR(retval);
2459 * device_create_vargs - creates a device and registers it with sysfs
2460 * @class: pointer to the struct class that this device should be registered to
2461 * @parent: pointer to the parent struct device of this new device, if any
2462 * @devt: the dev_t for the char device to be added
2463 * @drvdata: the data to be added to the device for callbacks
2464 * @fmt: string for the device's name
2465 * @args: va_list for the device's name
2467 * This function can be used by char device classes. A struct device
2468 * will be created in sysfs, registered to the specified class.
2470 * A "dev" file will be created, showing the dev_t for the device, if
2471 * the dev_t is not 0,0.
2472 * If a pointer to a parent struct device is passed in, the newly created
2473 * struct device will be a child of that device in sysfs.
2474 * The pointer to the struct device will be returned from the call.
2475 * Any further sysfs files that might be required can be created using this
2478 * Returns &struct device pointer on success, or ERR_PTR() on error.
2480 * Note: the struct class passed to this function must have previously
2481 * been created with a call to class_create().
2483 struct device *device_create_vargs(struct class *class, struct device *parent,
2484 dev_t devt, void *drvdata, const char *fmt,
2487 return device_create_groups_vargs(class, parent, devt, drvdata, NULL,
2490 EXPORT_SYMBOL_GPL(device_create_vargs);
2493 * device_create - creates a device and registers it with sysfs
2494 * @class: pointer to the struct class that this device should be registered to
2495 * @parent: pointer to the parent struct device of this new device, if any
2496 * @devt: the dev_t for the char device to be added
2497 * @drvdata: the data to be added to the device for callbacks
2498 * @fmt: string for the device's name
2500 * This function can be used by char device classes. A struct device
2501 * will be created in sysfs, registered to the specified class.
2503 * A "dev" file will be created, showing the dev_t for the device, if
2504 * the dev_t is not 0,0.
2505 * If a pointer to a parent struct device is passed in, the newly created
2506 * struct device will be a child of that device in sysfs.
2507 * The pointer to the struct device will be returned from the call.
2508 * Any further sysfs files that might be required can be created using this
2511 * Returns &struct device pointer on success, or ERR_PTR() on error.
2513 * Note: the struct class passed to this function must have previously
2514 * been created with a call to class_create().
2516 struct device *device_create(struct class *class, struct device *parent,
2517 dev_t devt, void *drvdata, const char *fmt, ...)
2522 va_start(vargs, fmt);
2523 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
2527 EXPORT_SYMBOL_GPL(device_create);
2530 * device_create_with_groups - creates a device and registers it with sysfs
2531 * @class: pointer to the struct class that this device should be registered to
2532 * @parent: pointer to the parent struct device of this new device, if any
2533 * @devt: the dev_t for the char device to be added
2534 * @drvdata: the data to be added to the device for callbacks
2535 * @groups: NULL-terminated list of attribute groups to be created
2536 * @fmt: string for the device's name
2538 * This function can be used by char device classes. A struct device
2539 * will be created in sysfs, registered to the specified class.
2540 * Additional attributes specified in the groups parameter will also
2541 * be created automatically.
2543 * A "dev" file will be created, showing the dev_t for the device, if
2544 * the dev_t is not 0,0.
2545 * If a pointer to a parent struct device is passed in, the newly created
2546 * struct device will be a child of that device in sysfs.
2547 * The pointer to the struct device will be returned from the call.
2548 * Any further sysfs files that might be required can be created using this
2551 * Returns &struct device pointer on success, or ERR_PTR() on error.
2553 * Note: the struct class passed to this function must have previously
2554 * been created with a call to class_create().
2556 struct device *device_create_with_groups(struct class *class,
2557 struct device *parent, dev_t devt,
2559 const struct attribute_group **groups,
2560 const char *fmt, ...)
2565 va_start(vargs, fmt);
2566 dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
2571 EXPORT_SYMBOL_GPL(device_create_with_groups);
2573 static int __match_devt(struct device *dev, const void *data)
2575 const dev_t *devt = data;
2577 return dev->devt == *devt;
2581 * device_destroy - removes a device that was created with device_create()
2582 * @class: pointer to the struct class that this device was registered with
2583 * @devt: the dev_t of the device that was previously registered
2585 * This call unregisters and cleans up a device that was created with a
2586 * call to device_create().
2588 void device_destroy(struct class *class, dev_t devt)
2592 dev = class_find_device(class, NULL, &devt, __match_devt);
2595 device_unregister(dev);
2598 EXPORT_SYMBOL_GPL(device_destroy);
2601 * device_rename - renames a device
2602 * @dev: the pointer to the struct device to be renamed
2603 * @new_name: the new name of the device
2605 * It is the responsibility of the caller to provide mutual
2606 * exclusion between two different calls of device_rename
2607 * on the same device to ensure that new_name is valid and
2608 * won't conflict with other devices.
2610 * Note: Don't call this function. Currently, the networking layer calls this
2611 * function, but that will change. The following text from Kay Sievers offers
2614 * Renaming devices is racy at many levels, symlinks and other stuff are not
2615 * replaced atomically, and you get a "move" uevent, but it's not easy to
2616 * connect the event to the old and new device. Device nodes are not renamed at
2617 * all, there isn't even support for that in the kernel now.
2619 * In the meantime, during renaming, your target name might be taken by another
2620 * driver, creating conflicts. Or the old name is taken directly after you
2621 * renamed it -- then you get events for the same DEVPATH, before you even see
2622 * the "move" event. It's just a mess, and nothing new should ever rely on
2623 * kernel device renaming. Besides that, it's not even implemented now for
2624 * other things than (driver-core wise very simple) network devices.
2626 * We are currently about to change network renaming in udev to completely
2627 * disallow renaming of devices in the same namespace as the kernel uses,
2628 * because we can't solve the problems properly, that arise with swapping names
2629 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
2630 * be allowed to some other name than eth[0-9]*, for the aforementioned
2633 * Make up a "real" name in the driver before you register anything, or add
2634 * some other attributes for userspace to find the device, or use udev to add
2635 * symlinks -- but never rename kernel devices later, it's a complete mess. We
2636 * don't even want to get into that and try to implement the missing pieces in
2637 * the core. We really have other pieces to fix in the driver core mess. :)
2639 int device_rename(struct device *dev, const char *new_name)
2641 struct kobject *kobj = &dev->kobj;
2642 char *old_device_name = NULL;
2645 dev = get_device(dev);
2649 dev_dbg(dev, "renaming to %s\n", new_name);
2651 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
2652 if (!old_device_name) {
2658 error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj,
2659 kobj, old_device_name,
2660 new_name, kobject_namespace(kobj));
2665 error = kobject_rename(kobj, new_name);
2672 kfree(old_device_name);
2676 EXPORT_SYMBOL_GPL(device_rename);
2678 static int device_move_class_links(struct device *dev,
2679 struct device *old_parent,
2680 struct device *new_parent)
2685 sysfs_remove_link(&dev->kobj, "device");
2687 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
2693 * device_move - moves a device to a new parent
2694 * @dev: the pointer to the struct device to be moved
2695 * @new_parent: the new parent of the device (can by NULL)
2696 * @dpm_order: how to reorder the dpm_list
2698 int device_move(struct device *dev, struct device *new_parent,
2699 enum dpm_order dpm_order)
2702 struct device *old_parent;
2703 struct kobject *new_parent_kobj;
2705 dev = get_device(dev);
2710 new_parent = get_device(new_parent);
2711 new_parent_kobj = get_device_parent(dev, new_parent);
2712 if (IS_ERR(new_parent_kobj)) {
2713 error = PTR_ERR(new_parent_kobj);
2714 put_device(new_parent);
2718 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
2719 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
2720 error = kobject_move(&dev->kobj, new_parent_kobj);
2722 cleanup_glue_dir(dev, new_parent_kobj);
2723 put_device(new_parent);
2726 old_parent = dev->parent;
2727 dev->parent = new_parent;
2729 klist_remove(&dev->p->knode_parent);
2731 klist_add_tail(&dev->p->knode_parent,
2732 &new_parent->p->klist_children);
2733 set_dev_node(dev, dev_to_node(new_parent));
2737 error = device_move_class_links(dev, old_parent, new_parent);
2739 /* We ignore errors on cleanup since we're hosed anyway... */
2740 device_move_class_links(dev, new_parent, old_parent);
2741 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
2743 klist_remove(&dev->p->knode_parent);
2744 dev->parent = old_parent;
2746 klist_add_tail(&dev->p->knode_parent,
2747 &old_parent->p->klist_children);
2748 set_dev_node(dev, dev_to_node(old_parent));
2751 cleanup_glue_dir(dev, new_parent_kobj);
2752 put_device(new_parent);
2756 switch (dpm_order) {
2757 case DPM_ORDER_NONE:
2759 case DPM_ORDER_DEV_AFTER_PARENT:
2760 device_pm_move_after(dev, new_parent);
2761 devices_kset_move_after(dev, new_parent);
2763 case DPM_ORDER_PARENT_BEFORE_DEV:
2764 device_pm_move_before(new_parent, dev);
2765 devices_kset_move_before(new_parent, dev);
2767 case DPM_ORDER_DEV_LAST:
2768 device_pm_move_last(dev);
2769 devices_kset_move_last(dev);
2773 put_device(old_parent);
2779 EXPORT_SYMBOL_GPL(device_move);
2782 * device_shutdown - call ->shutdown() on each device to shutdown.
2784 void device_shutdown(void)
2786 struct device *dev, *parent;
2788 wait_for_device_probe();
2789 device_block_probing();
2791 spin_lock(&devices_kset->list_lock);
2793 * Walk the devices list backward, shutting down each in turn.
2794 * Beware that device unplug events may also start pulling
2795 * devices offline, even as the system is shutting down.
2797 while (!list_empty(&devices_kset->list)) {
2798 dev = list_entry(devices_kset->list.prev, struct device,
2802 * hold reference count of device's parent to
2803 * prevent it from being freed because parent's
2804 * lock is to be held
2806 parent = get_device(dev->parent);
2809 * Make sure the device is off the kset list, in the
2810 * event that dev->*->shutdown() doesn't remove it.
2812 list_del_init(&dev->kobj.entry);
2813 spin_unlock(&devices_kset->list_lock);
2815 /* hold lock to avoid race with probe/release */
2817 device_lock(parent);
2820 /* Don't allow any more runtime suspends */
2821 pm_runtime_get_noresume(dev);
2822 pm_runtime_barrier(dev);
2824 if (dev->class && dev->class->shutdown_pre) {
2826 dev_info(dev, "shutdown_pre\n");
2827 dev->class->shutdown_pre(dev);
2829 if (dev->bus && dev->bus->shutdown) {
2831 dev_info(dev, "shutdown\n");
2832 dev->bus->shutdown(dev);
2833 } else if (dev->driver && dev->driver->shutdown) {
2835 dev_info(dev, "shutdown\n");
2836 dev->driver->shutdown(dev);
2841 device_unlock(parent);
2846 spin_lock(&devices_kset->list_lock);
2848 spin_unlock(&devices_kset->list_lock);
2852 * Device logging functions
2855 #ifdef CONFIG_PRINTK
2857 create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
2863 subsys = dev->class->name;
2865 subsys = dev->bus->name;
2869 pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys);
2874 * Add device identifier DEVICE=:
2878 * +sound:card0 subsystem:devname
2880 if (MAJOR(dev->devt)) {
2883 if (strcmp(subsys, "block") == 0)
2888 pos += snprintf(hdr + pos, hdrlen - pos,
2890 c, MAJOR(dev->devt), MINOR(dev->devt));
2891 } else if (strcmp(subsys, "net") == 0) {
2892 struct net_device *net = to_net_dev(dev);
2895 pos += snprintf(hdr + pos, hdrlen - pos,
2896 "DEVICE=n%u", net->ifindex);
2899 pos += snprintf(hdr + pos, hdrlen - pos,
2900 "DEVICE=+%s:%s", subsys, dev_name(dev));
2909 dev_WARN(dev, "device/subsystem name too long");
2913 int dev_vprintk_emit(int level, const struct device *dev,
2914 const char *fmt, va_list args)
2919 hdrlen = create_syslog_header(dev, hdr, sizeof(hdr));
2921 return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args);
2923 EXPORT_SYMBOL(dev_vprintk_emit);
2925 int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
2930 va_start(args, fmt);
2932 r = dev_vprintk_emit(level, dev, fmt, args);
2938 EXPORT_SYMBOL(dev_printk_emit);
2940 static void __dev_printk(const char *level, const struct device *dev,
2941 struct va_format *vaf)
2944 dev_printk_emit(level[1] - '0', dev, "%s %s: %pV",
2945 dev_driver_string(dev), dev_name(dev), vaf);
2947 printk("%s(NULL device *): %pV", level, vaf);
2950 void dev_printk(const char *level, const struct device *dev,
2951 const char *fmt, ...)
2953 struct va_format vaf;
2956 va_start(args, fmt);
2961 __dev_printk(level, dev, &vaf);
2965 EXPORT_SYMBOL(dev_printk);
2967 #define define_dev_printk_level(func, kern_level) \
2968 void func(const struct device *dev, const char *fmt, ...) \
2970 struct va_format vaf; \
2973 va_start(args, fmt); \
2978 __dev_printk(kern_level, dev, &vaf); \
2982 EXPORT_SYMBOL(func);
2984 define_dev_printk_level(dev_emerg, KERN_EMERG);
2985 define_dev_printk_level(dev_alert, KERN_ALERT);
2986 define_dev_printk_level(dev_crit, KERN_CRIT);
2987 define_dev_printk_level(dev_err, KERN_ERR);
2988 define_dev_printk_level(dev_warn, KERN_WARNING);
2989 define_dev_printk_level(dev_notice, KERN_NOTICE);
2990 define_dev_printk_level(_dev_info, KERN_INFO);
2994 static inline bool fwnode_is_primary(struct fwnode_handle *fwnode)
2996 return fwnode && !IS_ERR(fwnode->secondary);
3000 * set_primary_fwnode - Change the primary firmware node of a given device.
3001 * @dev: Device to handle.
3002 * @fwnode: New primary firmware node of the device.
3004 * Set the device's firmware node pointer to @fwnode, but if a secondary
3005 * firmware node of the device is present, preserve it.
3007 void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
3010 struct fwnode_handle *fn = dev->fwnode;
3012 if (fwnode_is_primary(fn))
3016 WARN_ON(fwnode->secondary);
3017 fwnode->secondary = fn;
3019 dev->fwnode = fwnode;
3021 dev->fwnode = fwnode_is_primary(dev->fwnode) ?
3022 dev->fwnode->secondary : NULL;
3025 EXPORT_SYMBOL_GPL(set_primary_fwnode);
3028 * set_secondary_fwnode - Change the secondary firmware node of a given device.
3029 * @dev: Device to handle.
3030 * @fwnode: New secondary firmware node of the device.
3032 * If a primary firmware node of the device is present, set its secondary
3033 * pointer to @fwnode. Otherwise, set the device's firmware node pointer to
3036 void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
3039 fwnode->secondary = ERR_PTR(-ENODEV);
3041 if (fwnode_is_primary(dev->fwnode))
3042 dev->fwnode->secondary = fwnode;
3044 dev->fwnode = fwnode;
3048 * device_set_of_node_from_dev - reuse device-tree node of another device
3049 * @dev: device whose device-tree node is being set
3050 * @dev2: device whose device-tree node is being reused
3052 * Takes another reference to the new device-tree node after first dropping
3053 * any reference held to the old node.
3055 void device_set_of_node_from_dev(struct device *dev, const struct device *dev2)
3057 of_node_put(dev->of_node);
3058 dev->of_node = of_node_get(dev2->of_node);
3059 dev->of_node_reused = true;
3061 EXPORT_SYMBOL_GPL(device_set_of_node_from_dev);