1 // SPDX-License-Identifier: GPL-2.0
3 * drivers/base/core.c - core driver model code (device registration, etc)
5 * Copyright (c) 2002-3 Patrick Mochel
6 * Copyright (c) 2002-3 Open Source Development Labs
7 * Copyright (c) 2006 Greg Kroah-Hartman <gregkh@suse.de>
8 * Copyright (c) 2006 Novell, Inc.
11 #include <linux/acpi.h>
12 #include <linux/device.h>
13 #include <linux/err.h>
14 #include <linux/fwnode.h>
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/string.h>
19 #include <linux/kdev_t.h>
20 #include <linux/notifier.h>
22 #include <linux/of_device.h>
23 #include <linux/genhd.h>
24 #include <linux/mutex.h>
25 #include <linux/pm_runtime.h>
26 #include <linux/netdevice.h>
27 #include <linux/sched/signal.h>
28 #include <linux/sysfs.h>
31 #include "power/power.h"
33 #ifdef CONFIG_SYSFS_DEPRECATED
34 #ifdef CONFIG_SYSFS_DEPRECATED_V2
35 long sysfs_deprecated = 1;
37 long sysfs_deprecated = 0;
39 static int __init sysfs_deprecated_setup(char *arg)
41 return kstrtol(arg, 10, &sysfs_deprecated);
43 early_param("sysfs.deprecated", sysfs_deprecated_setup);
46 /* Device links support. */
49 static DEFINE_MUTEX(device_links_lock);
50 DEFINE_STATIC_SRCU(device_links_srcu);
52 static inline void device_links_write_lock(void)
54 mutex_lock(&device_links_lock);
57 static inline void device_links_write_unlock(void)
59 mutex_unlock(&device_links_lock);
62 int device_links_read_lock(void)
64 return srcu_read_lock(&device_links_srcu);
67 void device_links_read_unlock(int idx)
69 srcu_read_unlock(&device_links_srcu, idx);
71 #else /* !CONFIG_SRCU */
72 static DECLARE_RWSEM(device_links_lock);
74 static inline void device_links_write_lock(void)
76 down_write(&device_links_lock);
79 static inline void device_links_write_unlock(void)
81 up_write(&device_links_lock);
84 int device_links_read_lock(void)
86 down_read(&device_links_lock);
90 void device_links_read_unlock(int not_used)
92 up_read(&device_links_lock);
94 #endif /* !CONFIG_SRCU */
97 * device_is_dependent - Check if one device depends on another one
98 * @dev: Device to check dependencies for.
99 * @target: Device to check against.
101 * Check if @target depends on @dev or any device dependent on it (its child or
102 * its consumer etc). Return 1 if that is the case or 0 otherwise.
104 static int device_is_dependent(struct device *dev, void *target)
106 struct device_link *link;
112 ret = device_for_each_child(dev, target, device_is_dependent);
116 list_for_each_entry(link, &dev->links.consumers, s_node) {
117 if (link->consumer == target)
120 ret = device_is_dependent(link->consumer, target);
127 static int device_reorder_to_tail(struct device *dev, void *not_used)
129 struct device_link *link;
132 * Devices that have not been registered yet will be put to the ends
133 * of the lists during the registration, so skip them here.
135 if (device_is_registered(dev))
136 devices_kset_move_last(dev);
138 if (device_pm_initialized(dev))
139 device_pm_move_last(dev);
141 device_for_each_child(dev, NULL, device_reorder_to_tail);
142 list_for_each_entry(link, &dev->links.consumers, s_node)
143 device_reorder_to_tail(link->consumer, NULL);
149 * device_pm_move_to_tail - Move set of devices to the end of device lists
150 * @dev: Device to move
152 * This is a device_reorder_to_tail() wrapper taking the requisite locks.
154 * It moves the @dev along with all of its children and all of its consumers
155 * to the ends of the device_kset and dpm_list, recursively.
157 void device_pm_move_to_tail(struct device *dev)
161 idx = device_links_read_lock();
163 device_reorder_to_tail(dev, NULL);
165 device_links_read_unlock(idx);
168 static void device_link_rpm_prepare(struct device *consumer,
169 struct device *supplier)
171 pm_runtime_new_link(consumer);
173 * If the link is being added by the consumer driver at probe time,
174 * balance the decrementation of the supplier's runtime PM usage counter
175 * after consumer probe in driver_probe_device().
177 if (consumer->links.status == DL_DEV_PROBING)
178 pm_runtime_get_noresume(supplier);
182 * device_link_add - Create a link between two devices.
183 * @consumer: Consumer end of the link.
184 * @supplier: Supplier end of the link.
185 * @flags: Link flags.
187 * The caller is responsible for the proper synchronization of the link creation
188 * with runtime PM. First, setting the DL_FLAG_PM_RUNTIME flag will cause the
189 * runtime PM framework to take the link into account. Second, if the
190 * DL_FLAG_RPM_ACTIVE flag is set in addition to it, the supplier devices will
191 * be forced into the active metastate and reference-counted upon the creation
192 * of the link. If DL_FLAG_PM_RUNTIME is not set, DL_FLAG_RPM_ACTIVE will be
195 * If the DL_FLAG_AUTOREMOVE_CONSUMER flag is set, the link will be removed
196 * automatically when the consumer device driver unbinds from it. Analogously,
197 * if DL_FLAG_AUTOREMOVE_SUPPLIER is set in @flags, the link will be removed
198 * automatically when the supplier device driver unbinds from it.
200 * The combination of DL_FLAG_STATELESS and either DL_FLAG_AUTOREMOVE_CONSUMER
201 * or DL_FLAG_AUTOREMOVE_SUPPLIER set in @flags at the same time is invalid and
202 * will cause NULL to be returned upfront.
204 * A side effect of the link creation is re-ordering of dpm_list and the
205 * devices_kset list by moving the consumer device and all devices depending
206 * on it to the ends of these lists (that does not happen to devices that have
207 * not been registered when this function is called).
209 * The supplier device is required to be registered when this function is called
210 * and NULL will be returned if that is not the case. The consumer device need
211 * not be registered, however.
213 struct device_link *device_link_add(struct device *consumer,
214 struct device *supplier, u32 flags)
216 struct device_link *link;
218 if (!consumer || !supplier ||
219 (flags & DL_FLAG_STATELESS &&
220 flags & (DL_FLAG_AUTOREMOVE_CONSUMER | DL_FLAG_AUTOREMOVE_SUPPLIER)))
223 if (flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) {
224 if (pm_runtime_get_sync(supplier) < 0) {
225 pm_runtime_put_noidle(supplier);
230 device_links_write_lock();
234 * If the supplier has not been fully registered yet or there is a
235 * reverse dependency between the consumer and the supplier already in
236 * the graph, return NULL.
238 if (!device_pm_initialized(supplier)
239 || device_is_dependent(consumer, supplier)) {
244 list_for_each_entry(link, &supplier->links.consumers, s_node) {
245 if (link->consumer != consumer)
249 * Don't return a stateless link if the caller wants a stateful
250 * one and vice versa.
252 if (WARN_ON((flags & DL_FLAG_STATELESS) != (link->flags & DL_FLAG_STATELESS))) {
257 if (flags & DL_FLAG_AUTOREMOVE_CONSUMER)
258 link->flags |= DL_FLAG_AUTOREMOVE_CONSUMER;
260 if (flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
261 link->flags |= DL_FLAG_AUTOREMOVE_SUPPLIER;
263 if (flags & DL_FLAG_PM_RUNTIME) {
264 if (!(link->flags & DL_FLAG_PM_RUNTIME)) {
265 device_link_rpm_prepare(consumer, supplier);
266 link->flags |= DL_FLAG_PM_RUNTIME;
268 if (flags & DL_FLAG_RPM_ACTIVE)
269 refcount_inc(&link->rpm_active);
272 kref_get(&link->kref);
276 link = kzalloc(sizeof(*link), GFP_KERNEL);
280 refcount_set(&link->rpm_active, 1);
282 if (flags & DL_FLAG_PM_RUNTIME) {
283 if (flags & DL_FLAG_RPM_ACTIVE)
284 refcount_inc(&link->rpm_active);
286 device_link_rpm_prepare(consumer, supplier);
289 get_device(supplier);
290 link->supplier = supplier;
291 INIT_LIST_HEAD(&link->s_node);
292 get_device(consumer);
293 link->consumer = consumer;
294 INIT_LIST_HEAD(&link->c_node);
296 kref_init(&link->kref);
298 /* Determine the initial link state. */
299 if (flags & DL_FLAG_STATELESS) {
300 link->status = DL_STATE_NONE;
302 switch (supplier->links.status) {
303 case DL_DEV_DRIVER_BOUND:
304 switch (consumer->links.status) {
307 * Some callers expect the link creation during
308 * consumer driver probe to resume the supplier
309 * even without DL_FLAG_RPM_ACTIVE.
311 if (flags & DL_FLAG_PM_RUNTIME)
312 pm_runtime_resume(supplier);
314 link->status = DL_STATE_CONSUMER_PROBE;
316 case DL_DEV_DRIVER_BOUND:
317 link->status = DL_STATE_ACTIVE;
320 link->status = DL_STATE_AVAILABLE;
324 case DL_DEV_UNBINDING:
325 link->status = DL_STATE_SUPPLIER_UNBIND;
328 link->status = DL_STATE_DORMANT;
334 * Move the consumer and all of the devices depending on it to the end
335 * of dpm_list and the devices_kset list.
337 * It is necessary to hold dpm_list locked throughout all that or else
338 * we may end up suspending with a wrong ordering of it.
340 device_reorder_to_tail(consumer, NULL);
342 list_add_tail_rcu(&link->s_node, &supplier->links.consumers);
343 list_add_tail_rcu(&link->c_node, &consumer->links.suppliers);
345 dev_dbg(consumer, "Linked as a consumer to %s\n", dev_name(supplier));
349 device_links_write_unlock();
351 if ((flags & DL_FLAG_PM_RUNTIME && flags & DL_FLAG_RPM_ACTIVE) && !link)
352 pm_runtime_put(supplier);
356 EXPORT_SYMBOL_GPL(device_link_add);
358 static void device_link_free(struct device_link *link)
360 put_device(link->consumer);
361 put_device(link->supplier);
366 static void __device_link_free_srcu(struct rcu_head *rhead)
368 device_link_free(container_of(rhead, struct device_link, rcu_head));
371 static void __device_link_del(struct kref *kref)
373 struct device_link *link = container_of(kref, struct device_link, kref);
375 dev_dbg(link->consumer, "Dropping the link to %s\n",
376 dev_name(link->supplier));
378 if (link->flags & DL_FLAG_PM_RUNTIME)
379 pm_runtime_drop_link(link->consumer);
381 list_del_rcu(&link->s_node);
382 list_del_rcu(&link->c_node);
383 call_srcu(&device_links_srcu, &link->rcu_head, __device_link_free_srcu);
385 #else /* !CONFIG_SRCU */
386 static void __device_link_del(struct kref *kref)
388 struct device_link *link = container_of(kref, struct device_link, kref);
390 dev_info(link->consumer, "Dropping the link to %s\n",
391 dev_name(link->supplier));
393 if (link->flags & DL_FLAG_PM_RUNTIME)
394 pm_runtime_drop_link(link->consumer);
396 list_del(&link->s_node);
397 list_del(&link->c_node);
398 device_link_free(link);
400 #endif /* !CONFIG_SRCU */
403 * device_link_del - Delete a link between two devices.
404 * @link: Device link to delete.
406 * The caller must ensure proper synchronization of this function with runtime
407 * PM. If the link was added multiple times, it needs to be deleted as often.
408 * Care is required for hotplugged devices: Their links are purged on removal
409 * and calling device_link_del() is then no longer allowed.
411 void device_link_del(struct device_link *link)
413 device_links_write_lock();
415 kref_put(&link->kref, __device_link_del);
417 device_links_write_unlock();
419 EXPORT_SYMBOL_GPL(device_link_del);
422 * device_link_remove - remove a link between two devices.
423 * @consumer: Consumer end of the link.
424 * @supplier: Supplier end of the link.
426 * The caller must ensure proper synchronization of this function with runtime
429 void device_link_remove(void *consumer, struct device *supplier)
431 struct device_link *link;
433 if (WARN_ON(consumer == supplier))
436 device_links_write_lock();
439 list_for_each_entry(link, &supplier->links.consumers, s_node) {
440 if (link->consumer == consumer) {
441 kref_put(&link->kref, __device_link_del);
447 device_links_write_unlock();
449 EXPORT_SYMBOL_GPL(device_link_remove);
451 static void device_links_missing_supplier(struct device *dev)
453 struct device_link *link;
455 list_for_each_entry(link, &dev->links.suppliers, c_node)
456 if (link->status == DL_STATE_CONSUMER_PROBE)
457 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
461 * device_links_check_suppliers - Check presence of supplier drivers.
462 * @dev: Consumer device.
464 * Check links from this device to any suppliers. Walk the list of the device's
465 * links to suppliers and see if all of them are available. If not, simply
466 * return -EPROBE_DEFER.
468 * We need to guarantee that the supplier will not go away after the check has
469 * been positive here. It only can go away in __device_release_driver() and
470 * that function checks the device's links to consumers. This means we need to
471 * mark the link as "consumer probe in progress" to make the supplier removal
472 * wait for us to complete (or bad things may happen).
474 * Links with the DL_FLAG_STATELESS flag set are ignored.
476 int device_links_check_suppliers(struct device *dev)
478 struct device_link *link;
481 device_links_write_lock();
483 list_for_each_entry(link, &dev->links.suppliers, c_node) {
484 if (link->flags & DL_FLAG_STATELESS)
487 if (link->status != DL_STATE_AVAILABLE) {
488 device_links_missing_supplier(dev);
492 WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE);
494 dev->links.status = DL_DEV_PROBING;
496 device_links_write_unlock();
501 * device_links_driver_bound - Update device links after probing its driver.
502 * @dev: Device to update the links for.
504 * The probe has been successful, so update links from this device to any
505 * consumers by changing their status to "available".
507 * Also change the status of @dev's links to suppliers to "active".
509 * Links with the DL_FLAG_STATELESS flag set are ignored.
511 void device_links_driver_bound(struct device *dev)
513 struct device_link *link;
515 device_links_write_lock();
517 list_for_each_entry(link, &dev->links.consumers, s_node) {
518 if (link->flags & DL_FLAG_STATELESS)
521 WARN_ON(link->status != DL_STATE_DORMANT);
522 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
525 list_for_each_entry(link, &dev->links.suppliers, c_node) {
526 if (link->flags & DL_FLAG_STATELESS)
529 WARN_ON(link->status != DL_STATE_CONSUMER_PROBE);
530 WRITE_ONCE(link->status, DL_STATE_ACTIVE);
533 dev->links.status = DL_DEV_DRIVER_BOUND;
535 device_links_write_unlock();
539 * __device_links_no_driver - Update links of a device without a driver.
540 * @dev: Device without a drvier.
542 * Delete all non-persistent links from this device to any suppliers.
544 * Persistent links stay around, but their status is changed to "available",
545 * unless they already are in the "supplier unbind in progress" state in which
546 * case they need not be updated.
548 * Links with the DL_FLAG_STATELESS flag set are ignored.
550 static void __device_links_no_driver(struct device *dev)
552 struct device_link *link, *ln;
554 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
555 if (link->flags & DL_FLAG_STATELESS)
558 if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER)
559 __device_link_del(&link->kref);
560 else if (link->status != DL_STATE_SUPPLIER_UNBIND)
561 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
564 dev->links.status = DL_DEV_NO_DRIVER;
567 void device_links_no_driver(struct device *dev)
569 device_links_write_lock();
570 __device_links_no_driver(dev);
571 device_links_write_unlock();
575 * device_links_driver_cleanup - Update links after driver removal.
576 * @dev: Device whose driver has just gone away.
578 * Update links to consumers for @dev by changing their status to "dormant" and
579 * invoke %__device_links_no_driver() to update links to suppliers for it as
582 * Links with the DL_FLAG_STATELESS flag set are ignored.
584 void device_links_driver_cleanup(struct device *dev)
586 struct device_link *link, *ln;
588 device_links_write_lock();
590 list_for_each_entry_safe(link, ln, &dev->links.consumers, s_node) {
591 if (link->flags & DL_FLAG_STATELESS)
594 WARN_ON(link->flags & DL_FLAG_AUTOREMOVE_CONSUMER);
595 WARN_ON(link->status != DL_STATE_SUPPLIER_UNBIND);
598 * autoremove the links between this @dev and its consumer
599 * devices that are not active, i.e. where the link state
600 * has moved to DL_STATE_SUPPLIER_UNBIND.
602 if (link->status == DL_STATE_SUPPLIER_UNBIND &&
603 link->flags & DL_FLAG_AUTOREMOVE_SUPPLIER)
604 __device_link_del(&link->kref);
606 WRITE_ONCE(link->status, DL_STATE_DORMANT);
609 __device_links_no_driver(dev);
611 device_links_write_unlock();
615 * device_links_busy - Check if there are any busy links to consumers.
616 * @dev: Device to check.
618 * Check each consumer of the device and return 'true' if its link's status
619 * is one of "consumer probe" or "active" (meaning that the given consumer is
620 * probing right now or its driver is present). Otherwise, change the link
621 * state to "supplier unbind" to prevent the consumer from being probed
622 * successfully going forward.
624 * Return 'false' if there are no probing or active consumers.
626 * Links with the DL_FLAG_STATELESS flag set are ignored.
628 bool device_links_busy(struct device *dev)
630 struct device_link *link;
633 device_links_write_lock();
635 list_for_each_entry(link, &dev->links.consumers, s_node) {
636 if (link->flags & DL_FLAG_STATELESS)
639 if (link->status == DL_STATE_CONSUMER_PROBE
640 || link->status == DL_STATE_ACTIVE) {
644 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
647 dev->links.status = DL_DEV_UNBINDING;
649 device_links_write_unlock();
654 * device_links_unbind_consumers - Force unbind consumers of the given device.
655 * @dev: Device to unbind the consumers of.
657 * Walk the list of links to consumers for @dev and if any of them is in the
658 * "consumer probe" state, wait for all device probes in progress to complete
661 * If that's not the case, change the status of the link to "supplier unbind"
662 * and check if the link was in the "active" state. If so, force the consumer
663 * driver to unbind and start over (the consumer will not re-probe as we have
664 * changed the state of the link already).
666 * Links with the DL_FLAG_STATELESS flag set are ignored.
668 void device_links_unbind_consumers(struct device *dev)
670 struct device_link *link;
673 device_links_write_lock();
675 list_for_each_entry(link, &dev->links.consumers, s_node) {
676 enum device_link_state status;
678 if (link->flags & DL_FLAG_STATELESS)
681 status = link->status;
682 if (status == DL_STATE_CONSUMER_PROBE) {
683 device_links_write_unlock();
685 wait_for_device_probe();
688 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
689 if (status == DL_STATE_ACTIVE) {
690 struct device *consumer = link->consumer;
692 get_device(consumer);
694 device_links_write_unlock();
696 device_release_driver_internal(consumer, NULL,
698 put_device(consumer);
703 device_links_write_unlock();
707 * device_links_purge - Delete existing links to other devices.
708 * @dev: Target device.
710 static void device_links_purge(struct device *dev)
712 struct device_link *link, *ln;
715 * Delete all of the remaining links from this device to any other
716 * devices (either consumers or suppliers).
718 device_links_write_lock();
720 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
721 WARN_ON(link->status == DL_STATE_ACTIVE);
722 __device_link_del(&link->kref);
725 list_for_each_entry_safe_reverse(link, ln, &dev->links.consumers, s_node) {
726 WARN_ON(link->status != DL_STATE_DORMANT &&
727 link->status != DL_STATE_NONE);
728 __device_link_del(&link->kref);
731 device_links_write_unlock();
734 /* Device links support end. */
736 int (*platform_notify)(struct device *dev) = NULL;
737 int (*platform_notify_remove)(struct device *dev) = NULL;
738 static struct kobject *dev_kobj;
739 struct kobject *sysfs_dev_char_kobj;
740 struct kobject *sysfs_dev_block_kobj;
742 static DEFINE_MUTEX(device_hotplug_lock);
744 void lock_device_hotplug(void)
746 mutex_lock(&device_hotplug_lock);
749 void unlock_device_hotplug(void)
751 mutex_unlock(&device_hotplug_lock);
754 int lock_device_hotplug_sysfs(void)
756 if (mutex_trylock(&device_hotplug_lock))
759 /* Avoid busy looping (5 ms of sleep should do). */
761 return restart_syscall();
765 static inline int device_is_not_partition(struct device *dev)
767 return !(dev->type == &part_type);
770 static inline int device_is_not_partition(struct device *dev)
777 device_platform_notify(struct device *dev, enum kobject_action action)
781 ret = acpi_platform_notify(dev, action);
785 ret = software_node_notify(dev, action);
789 if (platform_notify && action == KOBJ_ADD)
790 platform_notify(dev);
791 else if (platform_notify_remove && action == KOBJ_REMOVE)
792 platform_notify_remove(dev);
797 * dev_driver_string - Return a device's driver name, if at all possible
798 * @dev: struct device to get the name of
800 * Will return the device's driver's name if it is bound to a device. If
801 * the device is not bound to a driver, it will return the name of the bus
802 * it is attached to. If it is not attached to a bus either, an empty
803 * string will be returned.
805 const char *dev_driver_string(const struct device *dev)
807 struct device_driver *drv;
809 /* dev->driver can change to NULL underneath us because of unbinding,
810 * so be careful about accessing it. dev->bus and dev->class should
811 * never change once they are set, so they don't need special care.
813 drv = READ_ONCE(dev->driver);
814 return drv ? drv->name :
815 (dev->bus ? dev->bus->name :
816 (dev->class ? dev->class->name : ""));
818 EXPORT_SYMBOL(dev_driver_string);
820 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
822 static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
825 struct device_attribute *dev_attr = to_dev_attr(attr);
826 struct device *dev = kobj_to_dev(kobj);
830 ret = dev_attr->show(dev, dev_attr, buf);
831 if (ret >= (ssize_t)PAGE_SIZE) {
832 printk("dev_attr_show: %pS returned bad count\n",
838 static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
839 const char *buf, size_t count)
841 struct device_attribute *dev_attr = to_dev_attr(attr);
842 struct device *dev = kobj_to_dev(kobj);
846 ret = dev_attr->store(dev, dev_attr, buf, count);
850 static const struct sysfs_ops dev_sysfs_ops = {
851 .show = dev_attr_show,
852 .store = dev_attr_store,
855 #define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
857 ssize_t device_store_ulong(struct device *dev,
858 struct device_attribute *attr,
859 const char *buf, size_t size)
861 struct dev_ext_attribute *ea = to_ext_attr(attr);
865 ret = kstrtoul(buf, 0, &new);
868 *(unsigned long *)(ea->var) = new;
869 /* Always return full write size even if we didn't consume all */
872 EXPORT_SYMBOL_GPL(device_store_ulong);
874 ssize_t device_show_ulong(struct device *dev,
875 struct device_attribute *attr,
878 struct dev_ext_attribute *ea = to_ext_attr(attr);
879 return snprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var));
881 EXPORT_SYMBOL_GPL(device_show_ulong);
883 ssize_t device_store_int(struct device *dev,
884 struct device_attribute *attr,
885 const char *buf, size_t size)
887 struct dev_ext_attribute *ea = to_ext_attr(attr);
891 ret = kstrtol(buf, 0, &new);
895 if (new > INT_MAX || new < INT_MIN)
897 *(int *)(ea->var) = new;
898 /* Always return full write size even if we didn't consume all */
901 EXPORT_SYMBOL_GPL(device_store_int);
903 ssize_t device_show_int(struct device *dev,
904 struct device_attribute *attr,
907 struct dev_ext_attribute *ea = to_ext_attr(attr);
909 return snprintf(buf, PAGE_SIZE, "%d\n", *(int *)(ea->var));
911 EXPORT_SYMBOL_GPL(device_show_int);
913 ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
914 const char *buf, size_t size)
916 struct dev_ext_attribute *ea = to_ext_attr(attr);
918 if (strtobool(buf, ea->var) < 0)
923 EXPORT_SYMBOL_GPL(device_store_bool);
925 ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
928 struct dev_ext_attribute *ea = to_ext_attr(attr);
930 return snprintf(buf, PAGE_SIZE, "%d\n", *(bool *)(ea->var));
932 EXPORT_SYMBOL_GPL(device_show_bool);
935 * device_release - free device structure.
936 * @kobj: device's kobject.
938 * This is called once the reference count for the object
939 * reaches 0. We forward the call to the device's release
940 * method, which should handle actually freeing the structure.
942 static void device_release(struct kobject *kobj)
944 struct device *dev = kobj_to_dev(kobj);
945 struct device_private *p = dev->p;
948 * Some platform devices are driven without driver attached
949 * and managed resources may have been acquired. Make sure
950 * all resources are released.
952 * Drivers still can add resources into device after device
953 * is deleted but alive, so release devres here to avoid
954 * possible memory leak.
956 devres_release_all(dev);
960 else if (dev->type && dev->type->release)
961 dev->type->release(dev);
962 else if (dev->class && dev->class->dev_release)
963 dev->class->dev_release(dev);
965 WARN(1, KERN_ERR "Device '%s' does not have a release() function, it is broken and must be fixed. See Documentation/kobject.txt.\n",
970 static const void *device_namespace(struct kobject *kobj)
972 struct device *dev = kobj_to_dev(kobj);
973 const void *ns = NULL;
975 if (dev->class && dev->class->ns_type)
976 ns = dev->class->namespace(dev);
981 static void device_get_ownership(struct kobject *kobj, kuid_t *uid, kgid_t *gid)
983 struct device *dev = kobj_to_dev(kobj);
985 if (dev->class && dev->class->get_ownership)
986 dev->class->get_ownership(dev, uid, gid);
989 static struct kobj_type device_ktype = {
990 .release = device_release,
991 .sysfs_ops = &dev_sysfs_ops,
992 .namespace = device_namespace,
993 .get_ownership = device_get_ownership,
997 static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
999 struct kobj_type *ktype = get_ktype(kobj);
1001 if (ktype == &device_ktype) {
1002 struct device *dev = kobj_to_dev(kobj);
1011 static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
1013 struct device *dev = kobj_to_dev(kobj);
1016 return dev->bus->name;
1018 return dev->class->name;
1022 static int dev_uevent(struct kset *kset, struct kobject *kobj,
1023 struct kobj_uevent_env *env)
1025 struct device *dev = kobj_to_dev(kobj);
1028 /* add device node properties if present */
1029 if (MAJOR(dev->devt)) {
1033 kuid_t uid = GLOBAL_ROOT_UID;
1034 kgid_t gid = GLOBAL_ROOT_GID;
1036 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
1037 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
1038 name = device_get_devnode(dev, &mode, &uid, &gid, &tmp);
1040 add_uevent_var(env, "DEVNAME=%s", name);
1042 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
1043 if (!uid_eq(uid, GLOBAL_ROOT_UID))
1044 add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid));
1045 if (!gid_eq(gid, GLOBAL_ROOT_GID))
1046 add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid));
1051 if (dev->type && dev->type->name)
1052 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
1055 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
1057 /* Add common DT information about the device */
1058 of_device_uevent(dev, env);
1060 /* have the bus specific function add its stuff */
1061 if (dev->bus && dev->bus->uevent) {
1062 retval = dev->bus->uevent(dev, env);
1064 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
1065 dev_name(dev), __func__, retval);
1068 /* have the class specific function add its stuff */
1069 if (dev->class && dev->class->dev_uevent) {
1070 retval = dev->class->dev_uevent(dev, env);
1072 pr_debug("device: '%s': %s: class uevent() "
1073 "returned %d\n", dev_name(dev),
1077 /* have the device type specific function add its stuff */
1078 if (dev->type && dev->type->uevent) {
1079 retval = dev->type->uevent(dev, env);
1081 pr_debug("device: '%s': %s: dev_type uevent() "
1082 "returned %d\n", dev_name(dev),
1089 static const struct kset_uevent_ops device_uevent_ops = {
1090 .filter = dev_uevent_filter,
1091 .name = dev_uevent_name,
1092 .uevent = dev_uevent,
1095 static ssize_t uevent_show(struct device *dev, struct device_attribute *attr,
1098 struct kobject *top_kobj;
1100 struct kobj_uevent_env *env = NULL;
1105 /* search the kset, the device belongs to */
1106 top_kobj = &dev->kobj;
1107 while (!top_kobj->kset && top_kobj->parent)
1108 top_kobj = top_kobj->parent;
1109 if (!top_kobj->kset)
1112 kset = top_kobj->kset;
1113 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
1116 /* respect filter */
1117 if (kset->uevent_ops && kset->uevent_ops->filter)
1118 if (!kset->uevent_ops->filter(kset, &dev->kobj))
1121 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
1125 /* let the kset specific function add its keys */
1126 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
1130 /* copy keys to file */
1131 for (i = 0; i < env->envp_idx; i++)
1132 count += sprintf(&buf[count], "%s\n", env->envp[i]);
1138 static ssize_t uevent_store(struct device *dev, struct device_attribute *attr,
1139 const char *buf, size_t count)
1143 rc = kobject_synth_uevent(&dev->kobj, buf, count);
1146 dev_err(dev, "uevent: failed to send synthetic uevent\n");
1152 static DEVICE_ATTR_RW(uevent);
1154 static ssize_t online_show(struct device *dev, struct device_attribute *attr,
1160 val = !dev->offline;
1162 return sprintf(buf, "%u\n", val);
1165 static ssize_t online_store(struct device *dev, struct device_attribute *attr,
1166 const char *buf, size_t count)
1171 ret = strtobool(buf, &val);
1175 ret = lock_device_hotplug_sysfs();
1179 ret = val ? device_online(dev) : device_offline(dev);
1180 unlock_device_hotplug();
1181 return ret < 0 ? ret : count;
1183 static DEVICE_ATTR_RW(online);
1185 int device_add_groups(struct device *dev, const struct attribute_group **groups)
1187 return sysfs_create_groups(&dev->kobj, groups);
1189 EXPORT_SYMBOL_GPL(device_add_groups);
1191 void device_remove_groups(struct device *dev,
1192 const struct attribute_group **groups)
1194 sysfs_remove_groups(&dev->kobj, groups);
1196 EXPORT_SYMBOL_GPL(device_remove_groups);
1198 union device_attr_group_devres {
1199 const struct attribute_group *group;
1200 const struct attribute_group **groups;
1203 static int devm_attr_group_match(struct device *dev, void *res, void *data)
1205 return ((union device_attr_group_devres *)res)->group == data;
1208 static void devm_attr_group_remove(struct device *dev, void *res)
1210 union device_attr_group_devres *devres = res;
1211 const struct attribute_group *group = devres->group;
1213 dev_dbg(dev, "%s: removing group %p\n", __func__, group);
1214 sysfs_remove_group(&dev->kobj, group);
1217 static void devm_attr_groups_remove(struct device *dev, void *res)
1219 union device_attr_group_devres *devres = res;
1220 const struct attribute_group **groups = devres->groups;
1222 dev_dbg(dev, "%s: removing groups %p\n", __func__, groups);
1223 sysfs_remove_groups(&dev->kobj, groups);
1227 * devm_device_add_group - given a device, create a managed attribute group
1228 * @dev: The device to create the group for
1229 * @grp: The attribute group to create
1231 * This function creates a group for the first time. It will explicitly
1232 * warn and error if any of the attribute files being created already exist.
1234 * Returns 0 on success or error code on failure.
1236 int devm_device_add_group(struct device *dev, const struct attribute_group *grp)
1238 union device_attr_group_devres *devres;
1241 devres = devres_alloc(devm_attr_group_remove,
1242 sizeof(*devres), GFP_KERNEL);
1246 error = sysfs_create_group(&dev->kobj, grp);
1248 devres_free(devres);
1252 devres->group = grp;
1253 devres_add(dev, devres);
1256 EXPORT_SYMBOL_GPL(devm_device_add_group);
1259 * devm_device_remove_group: remove a managed group from a device
1260 * @dev: device to remove the group from
1261 * @grp: group to remove
1263 * This function removes a group of attributes from a device. The attributes
1264 * previously have to have been created for this group, otherwise it will fail.
1266 void devm_device_remove_group(struct device *dev,
1267 const struct attribute_group *grp)
1269 WARN_ON(devres_release(dev, devm_attr_group_remove,
1270 devm_attr_group_match,
1271 /* cast away const */ (void *)grp));
1273 EXPORT_SYMBOL_GPL(devm_device_remove_group);
1276 * devm_device_add_groups - create a bunch of managed attribute groups
1277 * @dev: The device to create the group for
1278 * @groups: The attribute groups to create, NULL terminated
1280 * This function creates a bunch of managed attribute groups. If an error
1281 * occurs when creating a group, all previously created groups will be
1282 * removed, unwinding everything back to the original state when this
1283 * function was called. It will explicitly warn and error if any of the
1284 * attribute files being created already exist.
1286 * Returns 0 on success or error code from sysfs_create_group on failure.
1288 int devm_device_add_groups(struct device *dev,
1289 const struct attribute_group **groups)
1291 union device_attr_group_devres *devres;
1294 devres = devres_alloc(devm_attr_groups_remove,
1295 sizeof(*devres), GFP_KERNEL);
1299 error = sysfs_create_groups(&dev->kobj, groups);
1301 devres_free(devres);
1305 devres->groups = groups;
1306 devres_add(dev, devres);
1309 EXPORT_SYMBOL_GPL(devm_device_add_groups);
1312 * devm_device_remove_groups - remove a list of managed groups
1314 * @dev: The device for the groups to be removed from
1315 * @groups: NULL terminated list of groups to be removed
1317 * If groups is not NULL, remove the specified groups from the device.
1319 void devm_device_remove_groups(struct device *dev,
1320 const struct attribute_group **groups)
1322 WARN_ON(devres_release(dev, devm_attr_groups_remove,
1323 devm_attr_group_match,
1324 /* cast away const */ (void *)groups));
1326 EXPORT_SYMBOL_GPL(devm_device_remove_groups);
1328 static int device_add_attrs(struct device *dev)
1330 struct class *class = dev->class;
1331 const struct device_type *type = dev->type;
1335 error = device_add_groups(dev, class->dev_groups);
1341 error = device_add_groups(dev, type->groups);
1343 goto err_remove_class_groups;
1346 error = device_add_groups(dev, dev->groups);
1348 goto err_remove_type_groups;
1350 if (device_supports_offline(dev) && !dev->offline_disabled) {
1351 error = device_create_file(dev, &dev_attr_online);
1353 goto err_remove_dev_groups;
1358 err_remove_dev_groups:
1359 device_remove_groups(dev, dev->groups);
1360 err_remove_type_groups:
1362 device_remove_groups(dev, type->groups);
1363 err_remove_class_groups:
1365 device_remove_groups(dev, class->dev_groups);
1370 static void device_remove_attrs(struct device *dev)
1372 struct class *class = dev->class;
1373 const struct device_type *type = dev->type;
1375 device_remove_file(dev, &dev_attr_online);
1376 device_remove_groups(dev, dev->groups);
1379 device_remove_groups(dev, type->groups);
1382 device_remove_groups(dev, class->dev_groups);
1385 static ssize_t dev_show(struct device *dev, struct device_attribute *attr,
1388 return print_dev_t(buf, dev->devt);
1390 static DEVICE_ATTR_RO(dev);
1393 struct kset *devices_kset;
1396 * devices_kset_move_before - Move device in the devices_kset's list.
1397 * @deva: Device to move.
1398 * @devb: Device @deva should come before.
1400 static void devices_kset_move_before(struct device *deva, struct device *devb)
1404 pr_debug("devices_kset: Moving %s before %s\n",
1405 dev_name(deva), dev_name(devb));
1406 spin_lock(&devices_kset->list_lock);
1407 list_move_tail(&deva->kobj.entry, &devb->kobj.entry);
1408 spin_unlock(&devices_kset->list_lock);
1412 * devices_kset_move_after - Move device in the devices_kset's list.
1413 * @deva: Device to move
1414 * @devb: Device @deva should come after.
1416 static void devices_kset_move_after(struct device *deva, struct device *devb)
1420 pr_debug("devices_kset: Moving %s after %s\n",
1421 dev_name(deva), dev_name(devb));
1422 spin_lock(&devices_kset->list_lock);
1423 list_move(&deva->kobj.entry, &devb->kobj.entry);
1424 spin_unlock(&devices_kset->list_lock);
1428 * devices_kset_move_last - move the device to the end of devices_kset's list.
1429 * @dev: device to move
1431 void devices_kset_move_last(struct device *dev)
1435 pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev));
1436 spin_lock(&devices_kset->list_lock);
1437 list_move_tail(&dev->kobj.entry, &devices_kset->list);
1438 spin_unlock(&devices_kset->list_lock);
1442 * device_create_file - create sysfs attribute file for device.
1444 * @attr: device attribute descriptor.
1446 int device_create_file(struct device *dev,
1447 const struct device_attribute *attr)
1452 WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
1453 "Attribute %s: write permission without 'store'\n",
1455 WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
1456 "Attribute %s: read permission without 'show'\n",
1458 error = sysfs_create_file(&dev->kobj, &attr->attr);
1463 EXPORT_SYMBOL_GPL(device_create_file);
1466 * device_remove_file - remove sysfs attribute file.
1468 * @attr: device attribute descriptor.
1470 void device_remove_file(struct device *dev,
1471 const struct device_attribute *attr)
1474 sysfs_remove_file(&dev->kobj, &attr->attr);
1476 EXPORT_SYMBOL_GPL(device_remove_file);
1479 * device_remove_file_self - remove sysfs attribute file from its own method.
1481 * @attr: device attribute descriptor.
1483 * See kernfs_remove_self() for details.
1485 bool device_remove_file_self(struct device *dev,
1486 const struct device_attribute *attr)
1489 return sysfs_remove_file_self(&dev->kobj, &attr->attr);
1493 EXPORT_SYMBOL_GPL(device_remove_file_self);
1496 * device_create_bin_file - create sysfs binary attribute file for device.
1498 * @attr: device binary attribute descriptor.
1500 int device_create_bin_file(struct device *dev,
1501 const struct bin_attribute *attr)
1503 int error = -EINVAL;
1505 error = sysfs_create_bin_file(&dev->kobj, attr);
1508 EXPORT_SYMBOL_GPL(device_create_bin_file);
1511 * device_remove_bin_file - remove sysfs binary attribute file
1513 * @attr: device binary attribute descriptor.
1515 void device_remove_bin_file(struct device *dev,
1516 const struct bin_attribute *attr)
1519 sysfs_remove_bin_file(&dev->kobj, attr);
1521 EXPORT_SYMBOL_GPL(device_remove_bin_file);
1523 static void klist_children_get(struct klist_node *n)
1525 struct device_private *p = to_device_private_parent(n);
1526 struct device *dev = p->device;
1531 static void klist_children_put(struct klist_node *n)
1533 struct device_private *p = to_device_private_parent(n);
1534 struct device *dev = p->device;
1540 * device_initialize - init device structure.
1543 * This prepares the device for use by other layers by initializing
1545 * It is the first half of device_register(), if called by
1546 * that function, though it can also be called separately, so one
1547 * may use @dev's fields. In particular, get_device()/put_device()
1548 * may be used for reference counting of @dev after calling this
1551 * All fields in @dev must be initialized by the caller to 0, except
1552 * for those explicitly set to some other value. The simplest
1553 * approach is to use kzalloc() to allocate the structure containing
1556 * NOTE: Use put_device() to give up your reference instead of freeing
1557 * @dev directly once you have called this function.
1559 void device_initialize(struct device *dev)
1561 dev->kobj.kset = devices_kset;
1562 kobject_init(&dev->kobj, &device_ktype);
1563 INIT_LIST_HEAD(&dev->dma_pools);
1564 mutex_init(&dev->mutex);
1565 lockdep_set_novalidate_class(&dev->mutex);
1566 spin_lock_init(&dev->devres_lock);
1567 INIT_LIST_HEAD(&dev->devres_head);
1568 device_pm_init(dev);
1569 set_dev_node(dev, -1);
1570 #ifdef CONFIG_GENERIC_MSI_IRQ
1571 INIT_LIST_HEAD(&dev->msi_list);
1573 INIT_LIST_HEAD(&dev->links.consumers);
1574 INIT_LIST_HEAD(&dev->links.suppliers);
1575 dev->links.status = DL_DEV_NO_DRIVER;
1577 EXPORT_SYMBOL_GPL(device_initialize);
1579 struct kobject *virtual_device_parent(struct device *dev)
1581 static struct kobject *virtual_dir = NULL;
1584 virtual_dir = kobject_create_and_add("virtual",
1585 &devices_kset->kobj);
1591 struct kobject kobj;
1592 struct class *class;
1595 #define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
1597 static void class_dir_release(struct kobject *kobj)
1599 struct class_dir *dir = to_class_dir(kobj);
1604 struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
1606 struct class_dir *dir = to_class_dir(kobj);
1607 return dir->class->ns_type;
1610 static struct kobj_type class_dir_ktype = {
1611 .release = class_dir_release,
1612 .sysfs_ops = &kobj_sysfs_ops,
1613 .child_ns_type = class_dir_child_ns_type
1616 static struct kobject *
1617 class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
1619 struct class_dir *dir;
1622 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1624 return ERR_PTR(-ENOMEM);
1627 kobject_init(&dir->kobj, &class_dir_ktype);
1629 dir->kobj.kset = &class->p->glue_dirs;
1631 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
1633 kobject_put(&dir->kobj);
1634 return ERR_PTR(retval);
1639 static DEFINE_MUTEX(gdp_mutex);
1641 static struct kobject *get_device_parent(struct device *dev,
1642 struct device *parent)
1645 struct kobject *kobj = NULL;
1646 struct kobject *parent_kobj;
1650 /* block disks show up in /sys/block */
1651 if (sysfs_deprecated && dev->class == &block_class) {
1652 if (parent && parent->class == &block_class)
1653 return &parent->kobj;
1654 return &block_class.p->subsys.kobj;
1659 * If we have no parent, we live in "virtual".
1660 * Class-devices with a non class-device as parent, live
1661 * in a "glue" directory to prevent namespace collisions.
1664 parent_kobj = virtual_device_parent(dev);
1665 else if (parent->class && !dev->class->ns_type)
1666 return &parent->kobj;
1668 parent_kobj = &parent->kobj;
1670 mutex_lock(&gdp_mutex);
1672 /* find our class-directory at the parent and reference it */
1673 spin_lock(&dev->class->p->glue_dirs.list_lock);
1674 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
1675 if (k->parent == parent_kobj) {
1676 kobj = kobject_get(k);
1679 spin_unlock(&dev->class->p->glue_dirs.list_lock);
1681 mutex_unlock(&gdp_mutex);
1685 /* or create a new class-directory at the parent device */
1686 k = class_dir_create_and_add(dev->class, parent_kobj);
1687 /* do not emit an uevent for this simple "glue" directory */
1688 mutex_unlock(&gdp_mutex);
1692 /* subsystems can specify a default root directory for their devices */
1693 if (!parent && dev->bus && dev->bus->dev_root)
1694 return &dev->bus->dev_root->kobj;
1697 return &parent->kobj;
1701 static inline bool live_in_glue_dir(struct kobject *kobj,
1704 if (!kobj || !dev->class ||
1705 kobj->kset != &dev->class->p->glue_dirs)
1710 static inline struct kobject *get_glue_dir(struct device *dev)
1712 return dev->kobj.parent;
1716 * make sure cleaning up dir as the last step, we need to make
1717 * sure .release handler of kobject is run with holding the
1720 static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
1722 /* see if we live in a "glue" directory */
1723 if (!live_in_glue_dir(glue_dir, dev))
1726 mutex_lock(&gdp_mutex);
1727 if (!kobject_has_children(glue_dir))
1728 kobject_del(glue_dir);
1729 kobject_put(glue_dir);
1730 mutex_unlock(&gdp_mutex);
1733 static int device_add_class_symlinks(struct device *dev)
1735 struct device_node *of_node = dev_of_node(dev);
1739 error = sysfs_create_link(&dev->kobj, of_node_kobj(of_node), "of_node");
1741 dev_warn(dev, "Error %d creating of_node link\n",error);
1742 /* An error here doesn't warrant bringing down the device */
1748 error = sysfs_create_link(&dev->kobj,
1749 &dev->class->p->subsys.kobj,
1754 if (dev->parent && device_is_not_partition(dev)) {
1755 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
1762 /* /sys/block has directories and does not need symlinks */
1763 if (sysfs_deprecated && dev->class == &block_class)
1767 /* link in the class directory pointing to the device */
1768 error = sysfs_create_link(&dev->class->p->subsys.kobj,
1769 &dev->kobj, dev_name(dev));
1776 sysfs_remove_link(&dev->kobj, "device");
1779 sysfs_remove_link(&dev->kobj, "subsystem");
1781 sysfs_remove_link(&dev->kobj, "of_node");
1785 static void device_remove_class_symlinks(struct device *dev)
1787 if (dev_of_node(dev))
1788 sysfs_remove_link(&dev->kobj, "of_node");
1793 if (dev->parent && device_is_not_partition(dev))
1794 sysfs_remove_link(&dev->kobj, "device");
1795 sysfs_remove_link(&dev->kobj, "subsystem");
1797 if (sysfs_deprecated && dev->class == &block_class)
1800 sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
1804 * dev_set_name - set a device name
1806 * @fmt: format string for the device's name
1808 int dev_set_name(struct device *dev, const char *fmt, ...)
1813 va_start(vargs, fmt);
1814 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
1818 EXPORT_SYMBOL_GPL(dev_set_name);
1821 * device_to_dev_kobj - select a /sys/dev/ directory for the device
1824 * By default we select char/ for new entries. Setting class->dev_obj
1825 * to NULL prevents an entry from being created. class->dev_kobj must
1826 * be set (or cleared) before any devices are registered to the class
1827 * otherwise device_create_sys_dev_entry() and
1828 * device_remove_sys_dev_entry() will disagree about the presence of
1831 static struct kobject *device_to_dev_kobj(struct device *dev)
1833 struct kobject *kobj;
1836 kobj = dev->class->dev_kobj;
1838 kobj = sysfs_dev_char_kobj;
1843 static int device_create_sys_dev_entry(struct device *dev)
1845 struct kobject *kobj = device_to_dev_kobj(dev);
1850 format_dev_t(devt_str, dev->devt);
1851 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
1857 static void device_remove_sys_dev_entry(struct device *dev)
1859 struct kobject *kobj = device_to_dev_kobj(dev);
1863 format_dev_t(devt_str, dev->devt);
1864 sysfs_remove_link(kobj, devt_str);
1868 static int device_private_init(struct device *dev)
1870 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
1873 dev->p->device = dev;
1874 klist_init(&dev->p->klist_children, klist_children_get,
1875 klist_children_put);
1876 INIT_LIST_HEAD(&dev->p->deferred_probe);
1881 * device_add - add device to device hierarchy.
1884 * This is part 2 of device_register(), though may be called
1885 * separately _iff_ device_initialize() has been called separately.
1887 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
1888 * to the global and sibling lists for the device, then
1889 * adds it to the other relevant subsystems of the driver model.
1891 * Do not call this routine or device_register() more than once for
1892 * any device structure. The driver model core is not designed to work
1893 * with devices that get unregistered and then spring back to life.
1894 * (Among other things, it's very hard to guarantee that all references
1895 * to the previous incarnation of @dev have been dropped.) Allocate
1896 * and register a fresh new struct device instead.
1898 * NOTE: _Never_ directly free @dev after calling this function, even
1899 * if it returned an error! Always use put_device() to give up your
1900 * reference instead.
1902 int device_add(struct device *dev)
1904 struct device *parent;
1905 struct kobject *kobj;
1906 struct class_interface *class_intf;
1907 int error = -EINVAL;
1908 struct kobject *glue_dir = NULL;
1910 dev = get_device(dev);
1915 error = device_private_init(dev);
1921 * for statically allocated devices, which should all be converted
1922 * some day, we need to initialize the name. We prevent reading back
1923 * the name, and force the use of dev_name()
1925 if (dev->init_name) {
1926 dev_set_name(dev, "%s", dev->init_name);
1927 dev->init_name = NULL;
1930 /* subsystems can specify simple device enumeration */
1931 if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
1932 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
1934 if (!dev_name(dev)) {
1939 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
1941 parent = get_device(dev->parent);
1942 kobj = get_device_parent(dev, parent);
1944 error = PTR_ERR(kobj);
1948 dev->kobj.parent = kobj;
1950 /* use parent numa_node */
1951 if (parent && (dev_to_node(dev) == NUMA_NO_NODE))
1952 set_dev_node(dev, dev_to_node(parent));
1954 /* first, register with generic layer. */
1955 /* we require the name to be set before, and pass NULL */
1956 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
1958 glue_dir = get_glue_dir(dev);
1962 /* notify platform of device entry */
1963 error = device_platform_notify(dev, KOBJ_ADD);
1965 goto platform_error;
1967 error = device_create_file(dev, &dev_attr_uevent);
1971 error = device_add_class_symlinks(dev);
1974 error = device_add_attrs(dev);
1977 error = bus_add_device(dev);
1980 error = dpm_sysfs_add(dev);
1985 if (MAJOR(dev->devt)) {
1986 error = device_create_file(dev, &dev_attr_dev);
1990 error = device_create_sys_dev_entry(dev);
1994 devtmpfs_create_node(dev);
1997 /* Notify clients of device addition. This call must come
1998 * after dpm_sysfs_add() and before kobject_uevent().
2001 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2002 BUS_NOTIFY_ADD_DEVICE, dev);
2004 kobject_uevent(&dev->kobj, KOBJ_ADD);
2005 bus_probe_device(dev);
2007 klist_add_tail(&dev->p->knode_parent,
2008 &parent->p->klist_children);
2011 mutex_lock(&dev->class->p->mutex);
2012 /* tie the class to the device */
2013 klist_add_tail(&dev->p->knode_class,
2014 &dev->class->p->klist_devices);
2016 /* notify any interfaces that the device is here */
2017 list_for_each_entry(class_intf,
2018 &dev->class->p->interfaces, node)
2019 if (class_intf->add_dev)
2020 class_intf->add_dev(dev, class_intf);
2021 mutex_unlock(&dev->class->p->mutex);
2027 if (MAJOR(dev->devt))
2028 device_remove_file(dev, &dev_attr_dev);
2030 device_pm_remove(dev);
2031 dpm_sysfs_remove(dev);
2033 bus_remove_device(dev);
2035 device_remove_attrs(dev);
2037 device_remove_class_symlinks(dev);
2039 device_remove_file(dev, &dev_attr_uevent);
2041 device_platform_notify(dev, KOBJ_REMOVE);
2043 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
2044 glue_dir = get_glue_dir(dev);
2045 kobject_del(&dev->kobj);
2047 cleanup_glue_dir(dev, glue_dir);
2055 EXPORT_SYMBOL_GPL(device_add);
2058 * device_register - register a device with the system.
2059 * @dev: pointer to the device structure
2061 * This happens in two clean steps - initialize the device
2062 * and add it to the system. The two steps can be called
2063 * separately, but this is the easiest and most common.
2064 * I.e. you should only call the two helpers separately if
2065 * have a clearly defined need to use and refcount the device
2066 * before it is added to the hierarchy.
2068 * For more information, see the kerneldoc for device_initialize()
2071 * NOTE: _Never_ directly free @dev after calling this function, even
2072 * if it returned an error! Always use put_device() to give up the
2073 * reference initialized in this function instead.
2075 int device_register(struct device *dev)
2077 device_initialize(dev);
2078 return device_add(dev);
2080 EXPORT_SYMBOL_GPL(device_register);
2083 * get_device - increment reference count for device.
2086 * This simply forwards the call to kobject_get(), though
2087 * we do take care to provide for the case that we get a NULL
2088 * pointer passed in.
2090 struct device *get_device(struct device *dev)
2092 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
2094 EXPORT_SYMBOL_GPL(get_device);
2097 * put_device - decrement reference count.
2098 * @dev: device in question.
2100 void put_device(struct device *dev)
2102 /* might_sleep(); */
2104 kobject_put(&dev->kobj);
2106 EXPORT_SYMBOL_GPL(put_device);
2109 * device_del - delete device from system.
2112 * This is the first part of the device unregistration
2113 * sequence. This removes the device from the lists we control
2114 * from here, has it removed from the other driver model
2115 * subsystems it was added to in device_add(), and removes it
2116 * from the kobject hierarchy.
2118 * NOTE: this should be called manually _iff_ device_add() was
2119 * also called manually.
2121 void device_del(struct device *dev)
2123 struct device *parent = dev->parent;
2124 struct kobject *glue_dir = NULL;
2125 struct class_interface *class_intf;
2128 * Hold the device lock and set the "dead" flag to guarantee that
2129 * the update behavior is consistent with the other bitfields near
2130 * it and that we cannot have an asynchronous probe routine trying
2131 * to run while we are tearing out the bus/class/sysfs from
2132 * underneath the device.
2135 dev->p->dead = true;
2138 /* Notify clients of device removal. This call must come
2139 * before dpm_sysfs_remove().
2142 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2143 BUS_NOTIFY_DEL_DEVICE, dev);
2145 dpm_sysfs_remove(dev);
2147 klist_del(&dev->p->knode_parent);
2148 if (MAJOR(dev->devt)) {
2149 devtmpfs_delete_node(dev);
2150 device_remove_sys_dev_entry(dev);
2151 device_remove_file(dev, &dev_attr_dev);
2154 device_remove_class_symlinks(dev);
2156 mutex_lock(&dev->class->p->mutex);
2157 /* notify any interfaces that the device is now gone */
2158 list_for_each_entry(class_intf,
2159 &dev->class->p->interfaces, node)
2160 if (class_intf->remove_dev)
2161 class_intf->remove_dev(dev, class_intf);
2162 /* remove the device from the class list */
2163 klist_del(&dev->p->knode_class);
2164 mutex_unlock(&dev->class->p->mutex);
2166 device_remove_file(dev, &dev_attr_uevent);
2167 device_remove_attrs(dev);
2168 bus_remove_device(dev);
2169 device_pm_remove(dev);
2170 driver_deferred_probe_del(dev);
2171 device_platform_notify(dev, KOBJ_REMOVE);
2172 device_remove_properties(dev);
2173 device_links_purge(dev);
2176 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
2177 BUS_NOTIFY_REMOVED_DEVICE, dev);
2178 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
2179 glue_dir = get_glue_dir(dev);
2180 kobject_del(&dev->kobj);
2181 cleanup_glue_dir(dev, glue_dir);
2184 EXPORT_SYMBOL_GPL(device_del);
2187 * device_unregister - unregister device from system.
2188 * @dev: device going away.
2190 * We do this in two parts, like we do device_register(). First,
2191 * we remove it from all the subsystems with device_del(), then
2192 * we decrement the reference count via put_device(). If that
2193 * is the final reference count, the device will be cleaned up
2194 * via device_release() above. Otherwise, the structure will
2195 * stick around until the final reference to the device is dropped.
2197 void device_unregister(struct device *dev)
2199 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
2203 EXPORT_SYMBOL_GPL(device_unregister);
2205 static struct device *prev_device(struct klist_iter *i)
2207 struct klist_node *n = klist_prev(i);
2208 struct device *dev = NULL;
2209 struct device_private *p;
2212 p = to_device_private_parent(n);
2218 static struct device *next_device(struct klist_iter *i)
2220 struct klist_node *n = klist_next(i);
2221 struct device *dev = NULL;
2222 struct device_private *p;
2225 p = to_device_private_parent(n);
2232 * device_get_devnode - path of device node file
2234 * @mode: returned file access mode
2235 * @uid: returned file owner
2236 * @gid: returned file group
2237 * @tmp: possibly allocated string
2239 * Return the relative path of a possible device node.
2240 * Non-default names may need to allocate a memory to compose
2241 * a name. This memory is returned in tmp and needs to be
2242 * freed by the caller.
2244 const char *device_get_devnode(struct device *dev,
2245 umode_t *mode, kuid_t *uid, kgid_t *gid,
2252 /* the device type may provide a specific name */
2253 if (dev->type && dev->type->devnode)
2254 *tmp = dev->type->devnode(dev, mode, uid, gid);
2258 /* the class may provide a specific name */
2259 if (dev->class && dev->class->devnode)
2260 *tmp = dev->class->devnode(dev, mode);
2264 /* return name without allocation, tmp == NULL */
2265 if (strchr(dev_name(dev), '!') == NULL)
2266 return dev_name(dev);
2268 /* replace '!' in the name with '/' */
2269 s = kstrdup(dev_name(dev), GFP_KERNEL);
2272 strreplace(s, '!', '/');
2277 * device_for_each_child - device child iterator.
2278 * @parent: parent struct device.
2279 * @fn: function to be called for each device.
2280 * @data: data for the callback.
2282 * Iterate over @parent's child devices, and call @fn for each,
2285 * We check the return of @fn each time. If it returns anything
2286 * other than 0, we break out and return that value.
2288 int device_for_each_child(struct device *parent, void *data,
2289 int (*fn)(struct device *dev, void *data))
2291 struct klist_iter i;
2292 struct device *child;
2298 klist_iter_init(&parent->p->klist_children, &i);
2299 while (!error && (child = next_device(&i)))
2300 error = fn(child, data);
2301 klist_iter_exit(&i);
2304 EXPORT_SYMBOL_GPL(device_for_each_child);
2307 * device_for_each_child_reverse - device child iterator in reversed order.
2308 * @parent: parent struct device.
2309 * @fn: function to be called for each device.
2310 * @data: data for the callback.
2312 * Iterate over @parent's child devices, and call @fn for each,
2315 * We check the return of @fn each time. If it returns anything
2316 * other than 0, we break out and return that value.
2318 int device_for_each_child_reverse(struct device *parent, void *data,
2319 int (*fn)(struct device *dev, void *data))
2321 struct klist_iter i;
2322 struct device *child;
2328 klist_iter_init(&parent->p->klist_children, &i);
2329 while ((child = prev_device(&i)) && !error)
2330 error = fn(child, data);
2331 klist_iter_exit(&i);
2334 EXPORT_SYMBOL_GPL(device_for_each_child_reverse);
2337 * device_find_child - device iterator for locating a particular device.
2338 * @parent: parent struct device
2339 * @match: Callback function to check device
2340 * @data: Data to pass to match function
2342 * This is similar to the device_for_each_child() function above, but it
2343 * returns a reference to a device that is 'found' for later use, as
2344 * determined by the @match callback.
2346 * The callback should return 0 if the device doesn't match and non-zero
2347 * if it does. If the callback returns non-zero and a reference to the
2348 * current device can be obtained, this function will return to the caller
2349 * and not iterate over any more devices.
2351 * NOTE: you will need to drop the reference with put_device() after use.
2353 struct device *device_find_child(struct device *parent, void *data,
2354 int (*match)(struct device *dev, void *data))
2356 struct klist_iter i;
2357 struct device *child;
2362 klist_iter_init(&parent->p->klist_children, &i);
2363 while ((child = next_device(&i)))
2364 if (match(child, data) && get_device(child))
2366 klist_iter_exit(&i);
2369 EXPORT_SYMBOL_GPL(device_find_child);
2371 int __init devices_init(void)
2373 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
2376 dev_kobj = kobject_create_and_add("dev", NULL);
2379 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
2380 if (!sysfs_dev_block_kobj)
2381 goto block_kobj_err;
2382 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
2383 if (!sysfs_dev_char_kobj)
2389 kobject_put(sysfs_dev_block_kobj);
2391 kobject_put(dev_kobj);
2393 kset_unregister(devices_kset);
2397 static int device_check_offline(struct device *dev, void *not_used)
2401 ret = device_for_each_child(dev, NULL, device_check_offline);
2405 return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
2409 * device_offline - Prepare the device for hot-removal.
2410 * @dev: Device to be put offline.
2412 * Execute the device bus type's .offline() callback, if present, to prepare
2413 * the device for a subsequent hot-removal. If that succeeds, the device must
2414 * not be used until either it is removed or its bus type's .online() callback
2417 * Call under device_hotplug_lock.
2419 int device_offline(struct device *dev)
2423 if (dev->offline_disabled)
2426 ret = device_for_each_child(dev, NULL, device_check_offline);
2431 if (device_supports_offline(dev)) {
2435 ret = dev->bus->offline(dev);
2437 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
2438 dev->offline = true;
2448 * device_online - Put the device back online after successful device_offline().
2449 * @dev: Device to be put back online.
2451 * If device_offline() has been successfully executed for @dev, but the device
2452 * has not been removed subsequently, execute its bus type's .online() callback
2453 * to indicate that the device can be used again.
2455 * Call under device_hotplug_lock.
2457 int device_online(struct device *dev)
2462 if (device_supports_offline(dev)) {
2464 ret = dev->bus->online(dev);
2466 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
2467 dev->offline = false;
2478 struct root_device {
2480 struct module *owner;
2483 static inline struct root_device *to_root_device(struct device *d)
2485 return container_of(d, struct root_device, dev);
2488 static void root_device_release(struct device *dev)
2490 kfree(to_root_device(dev));
2494 * __root_device_register - allocate and register a root device
2495 * @name: root device name
2496 * @owner: owner module of the root device, usually THIS_MODULE
2498 * This function allocates a root device and registers it
2499 * using device_register(). In order to free the returned
2500 * device, use root_device_unregister().
2502 * Root devices are dummy devices which allow other devices
2503 * to be grouped under /sys/devices. Use this function to
2504 * allocate a root device and then use it as the parent of
2505 * any device which should appear under /sys/devices/{name}
2507 * The /sys/devices/{name} directory will also contain a
2508 * 'module' symlink which points to the @owner directory
2511 * Returns &struct device pointer on success, or ERR_PTR() on error.
2513 * Note: You probably want to use root_device_register().
2515 struct device *__root_device_register(const char *name, struct module *owner)
2517 struct root_device *root;
2520 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
2522 return ERR_PTR(err);
2524 err = dev_set_name(&root->dev, "%s", name);
2527 return ERR_PTR(err);
2530 root->dev.release = root_device_release;
2532 err = device_register(&root->dev);
2534 put_device(&root->dev);
2535 return ERR_PTR(err);
2538 #ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
2540 struct module_kobject *mk = &owner->mkobj;
2542 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
2544 device_unregister(&root->dev);
2545 return ERR_PTR(err);
2547 root->owner = owner;
2553 EXPORT_SYMBOL_GPL(__root_device_register);
2556 * root_device_unregister - unregister and free a root device
2557 * @dev: device going away
2559 * This function unregisters and cleans up a device that was created by
2560 * root_device_register().
2562 void root_device_unregister(struct device *dev)
2564 struct root_device *root = to_root_device(dev);
2567 sysfs_remove_link(&root->dev.kobj, "module");
2569 device_unregister(dev);
2571 EXPORT_SYMBOL_GPL(root_device_unregister);
2574 static void device_create_release(struct device *dev)
2576 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
2580 static __printf(6, 0) struct device *
2581 device_create_groups_vargs(struct class *class, struct device *parent,
2582 dev_t devt, void *drvdata,
2583 const struct attribute_group **groups,
2584 const char *fmt, va_list args)
2586 struct device *dev = NULL;
2587 int retval = -ENODEV;
2589 if (class == NULL || IS_ERR(class))
2592 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2598 device_initialize(dev);
2601 dev->parent = parent;
2602 dev->groups = groups;
2603 dev->release = device_create_release;
2604 dev_set_drvdata(dev, drvdata);
2606 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
2610 retval = device_add(dev);
2618 return ERR_PTR(retval);
2622 * device_create_vargs - creates a device and registers it with sysfs
2623 * @class: pointer to the struct class that this device should be registered to
2624 * @parent: pointer to the parent struct device of this new device, if any
2625 * @devt: the dev_t for the char device to be added
2626 * @drvdata: the data to be added to the device for callbacks
2627 * @fmt: string for the device's name
2628 * @args: va_list for the device's name
2630 * This function can be used by char device classes. A struct device
2631 * will be created in sysfs, registered to the specified class.
2633 * A "dev" file will be created, showing the dev_t for the device, if
2634 * the dev_t is not 0,0.
2635 * If a pointer to a parent struct device is passed in, the newly created
2636 * struct device will be a child of that device in sysfs.
2637 * The pointer to the struct device will be returned from the call.
2638 * Any further sysfs files that might be required can be created using this
2641 * Returns &struct device pointer on success, or ERR_PTR() on error.
2643 * Note: the struct class passed to this function must have previously
2644 * been created with a call to class_create().
2646 struct device *device_create_vargs(struct class *class, struct device *parent,
2647 dev_t devt, void *drvdata, const char *fmt,
2650 return device_create_groups_vargs(class, parent, devt, drvdata, NULL,
2653 EXPORT_SYMBOL_GPL(device_create_vargs);
2656 * device_create - creates a device and registers it with sysfs
2657 * @class: pointer to the struct class that this device should be registered to
2658 * @parent: pointer to the parent struct device of this new device, if any
2659 * @devt: the dev_t for the char device to be added
2660 * @drvdata: the data to be added to the device for callbacks
2661 * @fmt: string for the device's name
2663 * This function can be used by char device classes. A struct device
2664 * will be created in sysfs, registered to the specified class.
2666 * A "dev" file will be created, showing the dev_t for the device, if
2667 * the dev_t is not 0,0.
2668 * If a pointer to a parent struct device is passed in, the newly created
2669 * struct device will be a child of that device in sysfs.
2670 * The pointer to the struct device will be returned from the call.
2671 * Any further sysfs files that might be required can be created using this
2674 * Returns &struct device pointer on success, or ERR_PTR() on error.
2676 * Note: the struct class passed to this function must have previously
2677 * been created with a call to class_create().
2679 struct device *device_create(struct class *class, struct device *parent,
2680 dev_t devt, void *drvdata, const char *fmt, ...)
2685 va_start(vargs, fmt);
2686 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
2690 EXPORT_SYMBOL_GPL(device_create);
2693 * device_create_with_groups - creates a device and registers it with sysfs
2694 * @class: pointer to the struct class that this device should be registered to
2695 * @parent: pointer to the parent struct device of this new device, if any
2696 * @devt: the dev_t for the char device to be added
2697 * @drvdata: the data to be added to the device for callbacks
2698 * @groups: NULL-terminated list of attribute groups to be created
2699 * @fmt: string for the device's name
2701 * This function can be used by char device classes. A struct device
2702 * will be created in sysfs, registered to the specified class.
2703 * Additional attributes specified in the groups parameter will also
2704 * be created automatically.
2706 * A "dev" file will be created, showing the dev_t for the device, if
2707 * the dev_t is not 0,0.
2708 * If a pointer to a parent struct device is passed in, the newly created
2709 * struct device will be a child of that device in sysfs.
2710 * The pointer to the struct device will be returned from the call.
2711 * Any further sysfs files that might be required can be created using this
2714 * Returns &struct device pointer on success, or ERR_PTR() on error.
2716 * Note: the struct class passed to this function must have previously
2717 * been created with a call to class_create().
2719 struct device *device_create_with_groups(struct class *class,
2720 struct device *parent, dev_t devt,
2722 const struct attribute_group **groups,
2723 const char *fmt, ...)
2728 va_start(vargs, fmt);
2729 dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
2734 EXPORT_SYMBOL_GPL(device_create_with_groups);
2736 static int __match_devt(struct device *dev, const void *data)
2738 const dev_t *devt = data;
2740 return dev->devt == *devt;
2744 * device_destroy - removes a device that was created with device_create()
2745 * @class: pointer to the struct class that this device was registered with
2746 * @devt: the dev_t of the device that was previously registered
2748 * This call unregisters and cleans up a device that was created with a
2749 * call to device_create().
2751 void device_destroy(struct class *class, dev_t devt)
2755 dev = class_find_device(class, NULL, &devt, __match_devt);
2758 device_unregister(dev);
2761 EXPORT_SYMBOL_GPL(device_destroy);
2764 * device_rename - renames a device
2765 * @dev: the pointer to the struct device to be renamed
2766 * @new_name: the new name of the device
2768 * It is the responsibility of the caller to provide mutual
2769 * exclusion between two different calls of device_rename
2770 * on the same device to ensure that new_name is valid and
2771 * won't conflict with other devices.
2773 * Note: Don't call this function. Currently, the networking layer calls this
2774 * function, but that will change. The following text from Kay Sievers offers
2777 * Renaming devices is racy at many levels, symlinks and other stuff are not
2778 * replaced atomically, and you get a "move" uevent, but it's not easy to
2779 * connect the event to the old and new device. Device nodes are not renamed at
2780 * all, there isn't even support for that in the kernel now.
2782 * In the meantime, during renaming, your target name might be taken by another
2783 * driver, creating conflicts. Or the old name is taken directly after you
2784 * renamed it -- then you get events for the same DEVPATH, before you even see
2785 * the "move" event. It's just a mess, and nothing new should ever rely on
2786 * kernel device renaming. Besides that, it's not even implemented now for
2787 * other things than (driver-core wise very simple) network devices.
2789 * We are currently about to change network renaming in udev to completely
2790 * disallow renaming of devices in the same namespace as the kernel uses,
2791 * because we can't solve the problems properly, that arise with swapping names
2792 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
2793 * be allowed to some other name than eth[0-9]*, for the aforementioned
2796 * Make up a "real" name in the driver before you register anything, or add
2797 * some other attributes for userspace to find the device, or use udev to add
2798 * symlinks -- but never rename kernel devices later, it's a complete mess. We
2799 * don't even want to get into that and try to implement the missing pieces in
2800 * the core. We really have other pieces to fix in the driver core mess. :)
2802 int device_rename(struct device *dev, const char *new_name)
2804 struct kobject *kobj = &dev->kobj;
2805 char *old_device_name = NULL;
2808 dev = get_device(dev);
2812 dev_dbg(dev, "renaming to %s\n", new_name);
2814 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
2815 if (!old_device_name) {
2821 error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj,
2822 kobj, old_device_name,
2823 new_name, kobject_namespace(kobj));
2828 error = kobject_rename(kobj, new_name);
2835 kfree(old_device_name);
2839 EXPORT_SYMBOL_GPL(device_rename);
2841 static int device_move_class_links(struct device *dev,
2842 struct device *old_parent,
2843 struct device *new_parent)
2848 sysfs_remove_link(&dev->kobj, "device");
2850 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
2856 * device_move - moves a device to a new parent
2857 * @dev: the pointer to the struct device to be moved
2858 * @new_parent: the new parent of the device (can be NULL)
2859 * @dpm_order: how to reorder the dpm_list
2861 int device_move(struct device *dev, struct device *new_parent,
2862 enum dpm_order dpm_order)
2865 struct device *old_parent;
2866 struct kobject *new_parent_kobj;
2868 dev = get_device(dev);
2873 new_parent = get_device(new_parent);
2874 new_parent_kobj = get_device_parent(dev, new_parent);
2875 if (IS_ERR(new_parent_kobj)) {
2876 error = PTR_ERR(new_parent_kobj);
2877 put_device(new_parent);
2881 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
2882 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
2883 error = kobject_move(&dev->kobj, new_parent_kobj);
2885 cleanup_glue_dir(dev, new_parent_kobj);
2886 put_device(new_parent);
2889 old_parent = dev->parent;
2890 dev->parent = new_parent;
2892 klist_remove(&dev->p->knode_parent);
2894 klist_add_tail(&dev->p->knode_parent,
2895 &new_parent->p->klist_children);
2896 set_dev_node(dev, dev_to_node(new_parent));
2900 error = device_move_class_links(dev, old_parent, new_parent);
2902 /* We ignore errors on cleanup since we're hosed anyway... */
2903 device_move_class_links(dev, new_parent, old_parent);
2904 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
2906 klist_remove(&dev->p->knode_parent);
2907 dev->parent = old_parent;
2909 klist_add_tail(&dev->p->knode_parent,
2910 &old_parent->p->klist_children);
2911 set_dev_node(dev, dev_to_node(old_parent));
2914 cleanup_glue_dir(dev, new_parent_kobj);
2915 put_device(new_parent);
2919 switch (dpm_order) {
2920 case DPM_ORDER_NONE:
2922 case DPM_ORDER_DEV_AFTER_PARENT:
2923 device_pm_move_after(dev, new_parent);
2924 devices_kset_move_after(dev, new_parent);
2926 case DPM_ORDER_PARENT_BEFORE_DEV:
2927 device_pm_move_before(new_parent, dev);
2928 devices_kset_move_before(new_parent, dev);
2930 case DPM_ORDER_DEV_LAST:
2931 device_pm_move_last(dev);
2932 devices_kset_move_last(dev);
2936 put_device(old_parent);
2942 EXPORT_SYMBOL_GPL(device_move);
2945 * device_shutdown - call ->shutdown() on each device to shutdown.
2947 void device_shutdown(void)
2949 struct device *dev, *parent;
2951 wait_for_device_probe();
2952 device_block_probing();
2954 spin_lock(&devices_kset->list_lock);
2956 * Walk the devices list backward, shutting down each in turn.
2957 * Beware that device unplug events may also start pulling
2958 * devices offline, even as the system is shutting down.
2960 while (!list_empty(&devices_kset->list)) {
2961 dev = list_entry(devices_kset->list.prev, struct device,
2965 * hold reference count of device's parent to
2966 * prevent it from being freed because parent's
2967 * lock is to be held
2969 parent = get_device(dev->parent);
2972 * Make sure the device is off the kset list, in the
2973 * event that dev->*->shutdown() doesn't remove it.
2975 list_del_init(&dev->kobj.entry);
2976 spin_unlock(&devices_kset->list_lock);
2978 /* hold lock to avoid race with probe/release */
2980 device_lock(parent);
2983 /* Don't allow any more runtime suspends */
2984 pm_runtime_get_noresume(dev);
2985 pm_runtime_barrier(dev);
2987 if (dev->class && dev->class->shutdown_pre) {
2989 dev_info(dev, "shutdown_pre\n");
2990 dev->class->shutdown_pre(dev);
2992 if (dev->bus && dev->bus->shutdown) {
2994 dev_info(dev, "shutdown\n");
2995 dev->bus->shutdown(dev);
2996 } else if (dev->driver && dev->driver->shutdown) {
2998 dev_info(dev, "shutdown\n");
2999 dev->driver->shutdown(dev);
3004 device_unlock(parent);
3009 spin_lock(&devices_kset->list_lock);
3011 spin_unlock(&devices_kset->list_lock);
3015 * Device logging functions
3018 #ifdef CONFIG_PRINTK
3020 create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
3026 subsys = dev->class->name;
3028 subsys = dev->bus->name;
3032 pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys);
3037 * Add device identifier DEVICE=:
3041 * +sound:card0 subsystem:devname
3043 if (MAJOR(dev->devt)) {
3046 if (strcmp(subsys, "block") == 0)
3051 pos += snprintf(hdr + pos, hdrlen - pos,
3053 c, MAJOR(dev->devt), MINOR(dev->devt));
3054 } else if (strcmp(subsys, "net") == 0) {
3055 struct net_device *net = to_net_dev(dev);
3058 pos += snprintf(hdr + pos, hdrlen - pos,
3059 "DEVICE=n%u", net->ifindex);
3062 pos += snprintf(hdr + pos, hdrlen - pos,
3063 "DEVICE=+%s:%s", subsys, dev_name(dev));
3072 dev_WARN(dev, "device/subsystem name too long");
3076 int dev_vprintk_emit(int level, const struct device *dev,
3077 const char *fmt, va_list args)
3082 hdrlen = create_syslog_header(dev, hdr, sizeof(hdr));
3084 return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args);
3086 EXPORT_SYMBOL(dev_vprintk_emit);
3088 int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
3093 va_start(args, fmt);
3095 r = dev_vprintk_emit(level, dev, fmt, args);
3101 EXPORT_SYMBOL(dev_printk_emit);
3103 static void __dev_printk(const char *level, const struct device *dev,
3104 struct va_format *vaf)
3107 dev_printk_emit(level[1] - '0', dev, "%s %s: %pV",
3108 dev_driver_string(dev), dev_name(dev), vaf);
3110 printk("%s(NULL device *): %pV", level, vaf);
3113 void dev_printk(const char *level, const struct device *dev,
3114 const char *fmt, ...)
3116 struct va_format vaf;
3119 va_start(args, fmt);
3124 __dev_printk(level, dev, &vaf);
3128 EXPORT_SYMBOL(dev_printk);
3130 #define define_dev_printk_level(func, kern_level) \
3131 void func(const struct device *dev, const char *fmt, ...) \
3133 struct va_format vaf; \
3136 va_start(args, fmt); \
3141 __dev_printk(kern_level, dev, &vaf); \
3145 EXPORT_SYMBOL(func);
3147 define_dev_printk_level(_dev_emerg, KERN_EMERG);
3148 define_dev_printk_level(_dev_alert, KERN_ALERT);
3149 define_dev_printk_level(_dev_crit, KERN_CRIT);
3150 define_dev_printk_level(_dev_err, KERN_ERR);
3151 define_dev_printk_level(_dev_warn, KERN_WARNING);
3152 define_dev_printk_level(_dev_notice, KERN_NOTICE);
3153 define_dev_printk_level(_dev_info, KERN_INFO);
3157 static inline bool fwnode_is_primary(struct fwnode_handle *fwnode)
3159 return fwnode && !IS_ERR(fwnode->secondary);
3163 * set_primary_fwnode - Change the primary firmware node of a given device.
3164 * @dev: Device to handle.
3165 * @fwnode: New primary firmware node of the device.
3167 * Set the device's firmware node pointer to @fwnode, but if a secondary
3168 * firmware node of the device is present, preserve it.
3170 void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
3173 struct fwnode_handle *fn = dev->fwnode;
3175 if (fwnode_is_primary(fn))
3179 WARN_ON(fwnode->secondary);
3180 fwnode->secondary = fn;
3182 dev->fwnode = fwnode;
3184 dev->fwnode = fwnode_is_primary(dev->fwnode) ?
3185 dev->fwnode->secondary : NULL;
3188 EXPORT_SYMBOL_GPL(set_primary_fwnode);
3191 * set_secondary_fwnode - Change the secondary firmware node of a given device.
3192 * @dev: Device to handle.
3193 * @fwnode: New secondary firmware node of the device.
3195 * If a primary firmware node of the device is present, set its secondary
3196 * pointer to @fwnode. Otherwise, set the device's firmware node pointer to
3199 void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
3202 fwnode->secondary = ERR_PTR(-ENODEV);
3204 if (fwnode_is_primary(dev->fwnode))
3205 dev->fwnode->secondary = fwnode;
3207 dev->fwnode = fwnode;
3211 * device_set_of_node_from_dev - reuse device-tree node of another device
3212 * @dev: device whose device-tree node is being set
3213 * @dev2: device whose device-tree node is being reused
3215 * Takes another reference to the new device-tree node after first dropping
3216 * any reference held to the old node.
3218 void device_set_of_node_from_dev(struct device *dev, const struct device *dev2)
3220 of_node_put(dev->of_node);
3221 dev->of_node = of_node_get(dev2->of_node);
3222 dev->of_node_reused = true;
3224 EXPORT_SYMBOL_GPL(device_set_of_node_from_dev);