1 // SPDX-License-Identifier: GPL-2.0
3 * drivers/base/core.c - core driver model code (device registration, etc)
5 * Copyright (c) 2002-3 Patrick Mochel
6 * Copyright (c) 2002-3 Open Source Development Labs
7 * Copyright (c) 2006 Greg Kroah-Hartman <gregkh@suse.de>
8 * Copyright (c) 2006 Novell, Inc.
11 #include <linux/device.h>
12 #include <linux/err.h>
13 #include <linux/fwnode.h>
14 #include <linux/init.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <linux/string.h>
18 #include <linux/kdev_t.h>
19 #include <linux/notifier.h>
21 #include <linux/of_device.h>
22 #include <linux/genhd.h>
23 #include <linux/mutex.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/netdevice.h>
26 #include <linux/sched/signal.h>
27 #include <linux/sysfs.h>
30 #include "power/power.h"
32 #ifdef CONFIG_SYSFS_DEPRECATED
33 #ifdef CONFIG_SYSFS_DEPRECATED_V2
34 long sysfs_deprecated
= 1;
36 long sysfs_deprecated
= 0;
38 static int __init
sysfs_deprecated_setup(char *arg
)
40 return kstrtol(arg
, 10, &sysfs_deprecated
);
42 early_param("sysfs.deprecated", sysfs_deprecated_setup
);
45 /* Device links support. */
48 static DEFINE_MUTEX(device_links_lock
);
49 DEFINE_STATIC_SRCU(device_links_srcu
);
51 static inline void device_links_write_lock(void)
53 mutex_lock(&device_links_lock
);
56 static inline void device_links_write_unlock(void)
58 mutex_unlock(&device_links_lock
);
61 int device_links_read_lock(void)
63 return srcu_read_lock(&device_links_srcu
);
66 void device_links_read_unlock(int idx
)
68 srcu_read_unlock(&device_links_srcu
, idx
);
70 #else /* !CONFIG_SRCU */
71 static DECLARE_RWSEM(device_links_lock
);
73 static inline void device_links_write_lock(void)
75 down_write(&device_links_lock
);
78 static inline void device_links_write_unlock(void)
80 up_write(&device_links_lock
);
83 int device_links_read_lock(void)
85 down_read(&device_links_lock
);
89 void device_links_read_unlock(int not_used
)
91 up_read(&device_links_lock
);
93 #endif /* !CONFIG_SRCU */
96 * device_is_dependent - Check if one device depends on another one
97 * @dev: Device to check dependencies for.
98 * @target: Device to check against.
100 * Check if @target depends on @dev or any device dependent on it (its child or
101 * its consumer etc). Return 1 if that is the case or 0 otherwise.
103 static int device_is_dependent(struct device
*dev
, void *target
)
105 struct device_link
*link
;
108 if (WARN_ON(dev
== target
))
111 ret
= device_for_each_child(dev
, target
, device_is_dependent
);
115 list_for_each_entry(link
, &dev
->links
.consumers
, s_node
) {
116 if (WARN_ON(link
->consumer
== target
))
119 ret
= device_is_dependent(link
->consumer
, target
);
126 static int device_reorder_to_tail(struct device
*dev
, void *not_used
)
128 struct device_link
*link
;
131 * Devices that have not been registered yet will be put to the ends
132 * of the lists during the registration, so skip them here.
134 if (device_is_registered(dev
))
135 devices_kset_move_last(dev
);
137 if (device_pm_initialized(dev
))
138 device_pm_move_last(dev
);
140 device_for_each_child(dev
, NULL
, device_reorder_to_tail
);
141 list_for_each_entry(link
, &dev
->links
.consumers
, s_node
)
142 device_reorder_to_tail(link
->consumer
, NULL
);
148 * device_link_add - Create a link between two devices.
149 * @consumer: Consumer end of the link.
150 * @supplier: Supplier end of the link.
151 * @flags: Link flags.
153 * The caller is responsible for the proper synchronization of the link creation
154 * with runtime PM. First, setting the DL_FLAG_PM_RUNTIME flag will cause the
155 * runtime PM framework to take the link into account. Second, if the
156 * DL_FLAG_RPM_ACTIVE flag is set in addition to it, the supplier devices will
157 * be forced into the active metastate and reference-counted upon the creation
158 * of the link. If DL_FLAG_PM_RUNTIME is not set, DL_FLAG_RPM_ACTIVE will be
161 * If the DL_FLAG_AUTOREMOVE is set, the link will be removed automatically
162 * when the consumer device driver unbinds from it. The combination of both
163 * DL_FLAG_AUTOREMOVE and DL_FLAG_STATELESS set is invalid and will cause NULL
166 * A side effect of the link creation is re-ordering of dpm_list and the
167 * devices_kset list by moving the consumer device and all devices depending
168 * on it to the ends of these lists (that does not happen to devices that have
169 * not been registered when this function is called).
171 * The supplier device is required to be registered when this function is called
172 * and NULL will be returned if that is not the case. The consumer device need
173 * not be registered, however.
175 struct device_link
*device_link_add(struct device
*consumer
,
176 struct device
*supplier
, u32 flags
)
178 struct device_link
*link
;
180 if (!consumer
|| !supplier
||
181 ((flags
& DL_FLAG_STATELESS
) && (flags
& DL_FLAG_AUTOREMOVE
)))
184 device_links_write_lock();
188 * If the supplier has not been fully registered yet or there is a
189 * reverse dependency between the consumer and the supplier already in
190 * the graph, return NULL.
192 if (!device_pm_initialized(supplier
)
193 || device_is_dependent(consumer
, supplier
)) {
198 list_for_each_entry(link
, &supplier
->links
.consumers
, s_node
)
199 if (link
->consumer
== consumer
)
202 link
= kzalloc(sizeof(*link
), GFP_KERNEL
);
206 if (flags
& DL_FLAG_PM_RUNTIME
) {
207 if (flags
& DL_FLAG_RPM_ACTIVE
) {
208 if (pm_runtime_get_sync(supplier
) < 0) {
209 pm_runtime_put_noidle(supplier
);
214 link
->rpm_active
= true;
216 pm_runtime_new_link(consumer
);
218 get_device(supplier
);
219 link
->supplier
= supplier
;
220 INIT_LIST_HEAD(&link
->s_node
);
221 get_device(consumer
);
222 link
->consumer
= consumer
;
223 INIT_LIST_HEAD(&link
->c_node
);
226 /* Determine the initial link state. */
227 if (flags
& DL_FLAG_STATELESS
) {
228 link
->status
= DL_STATE_NONE
;
230 switch (supplier
->links
.status
) {
231 case DL_DEV_DRIVER_BOUND
:
232 switch (consumer
->links
.status
) {
235 * Balance the decrementation of the supplier's
236 * runtime PM usage counter after consumer probe
237 * in driver_probe_device().
239 if (flags
& DL_FLAG_PM_RUNTIME
)
240 pm_runtime_get_sync(supplier
);
242 link
->status
= DL_STATE_CONSUMER_PROBE
;
244 case DL_DEV_DRIVER_BOUND
:
245 link
->status
= DL_STATE_ACTIVE
;
248 link
->status
= DL_STATE_AVAILABLE
;
252 case DL_DEV_UNBINDING
:
253 link
->status
= DL_STATE_SUPPLIER_UNBIND
;
256 link
->status
= DL_STATE_DORMANT
;
262 * Move the consumer and all of the devices depending on it to the end
263 * of dpm_list and the devices_kset list.
265 * It is necessary to hold dpm_list locked throughout all that or else
266 * we may end up suspending with a wrong ordering of it.
268 device_reorder_to_tail(consumer
, NULL
);
270 list_add_tail_rcu(&link
->s_node
, &supplier
->links
.consumers
);
271 list_add_tail_rcu(&link
->c_node
, &consumer
->links
.suppliers
);
273 dev_info(consumer
, "Linked as a consumer to %s\n", dev_name(supplier
));
277 device_links_write_unlock();
280 EXPORT_SYMBOL_GPL(device_link_add
);
282 static void device_link_free(struct device_link
*link
)
284 put_device(link
->consumer
);
285 put_device(link
->supplier
);
290 static void __device_link_free_srcu(struct rcu_head
*rhead
)
292 device_link_free(container_of(rhead
, struct device_link
, rcu_head
));
295 static void __device_link_del(struct device_link
*link
)
297 dev_info(link
->consumer
, "Dropping the link to %s\n",
298 dev_name(link
->supplier
));
300 if (link
->flags
& DL_FLAG_PM_RUNTIME
)
301 pm_runtime_drop_link(link
->consumer
);
303 list_del_rcu(&link
->s_node
);
304 list_del_rcu(&link
->c_node
);
305 call_srcu(&device_links_srcu
, &link
->rcu_head
, __device_link_free_srcu
);
307 #else /* !CONFIG_SRCU */
308 static void __device_link_del(struct device_link
*link
)
310 dev_info(link
->consumer
, "Dropping the link to %s\n",
311 dev_name(link
->supplier
));
313 if (link
->flags
& DL_FLAG_PM_RUNTIME
)
314 pm_runtime_drop_link(link
->consumer
);
316 list_del(&link
->s_node
);
317 list_del(&link
->c_node
);
318 device_link_free(link
);
320 #endif /* !CONFIG_SRCU */
323 * device_link_del - Delete a link between two devices.
324 * @link: Device link to delete.
326 * The caller must ensure proper synchronization of this function with runtime
329 void device_link_del(struct device_link
*link
)
331 device_links_write_lock();
333 __device_link_del(link
);
335 device_links_write_unlock();
337 EXPORT_SYMBOL_GPL(device_link_del
);
339 static void device_links_missing_supplier(struct device
*dev
)
341 struct device_link
*link
;
343 list_for_each_entry(link
, &dev
->links
.suppliers
, c_node
)
344 if (link
->status
== DL_STATE_CONSUMER_PROBE
)
345 WRITE_ONCE(link
->status
, DL_STATE_AVAILABLE
);
349 * device_links_check_suppliers - Check presence of supplier drivers.
350 * @dev: Consumer device.
352 * Check links from this device to any suppliers. Walk the list of the device's
353 * links to suppliers and see if all of them are available. If not, simply
354 * return -EPROBE_DEFER.
356 * We need to guarantee that the supplier will not go away after the check has
357 * been positive here. It only can go away in __device_release_driver() and
358 * that function checks the device's links to consumers. This means we need to
359 * mark the link as "consumer probe in progress" to make the supplier removal
360 * wait for us to complete (or bad things may happen).
362 * Links with the DL_FLAG_STATELESS flag set are ignored.
364 int device_links_check_suppliers(struct device
*dev
)
366 struct device_link
*link
;
369 device_links_write_lock();
371 list_for_each_entry(link
, &dev
->links
.suppliers
, c_node
) {
372 if (link
->flags
& DL_FLAG_STATELESS
)
375 if (link
->status
!= DL_STATE_AVAILABLE
) {
376 device_links_missing_supplier(dev
);
380 WRITE_ONCE(link
->status
, DL_STATE_CONSUMER_PROBE
);
382 dev
->links
.status
= DL_DEV_PROBING
;
384 device_links_write_unlock();
389 * device_links_driver_bound - Update device links after probing its driver.
390 * @dev: Device to update the links for.
392 * The probe has been successful, so update links from this device to any
393 * consumers by changing their status to "available".
395 * Also change the status of @dev's links to suppliers to "active".
397 * Links with the DL_FLAG_STATELESS flag set are ignored.
399 void device_links_driver_bound(struct device
*dev
)
401 struct device_link
*link
;
403 device_links_write_lock();
405 list_for_each_entry(link
, &dev
->links
.consumers
, s_node
) {
406 if (link
->flags
& DL_FLAG_STATELESS
)
409 WARN_ON(link
->status
!= DL_STATE_DORMANT
);
410 WRITE_ONCE(link
->status
, DL_STATE_AVAILABLE
);
413 list_for_each_entry(link
, &dev
->links
.suppliers
, c_node
) {
414 if (link
->flags
& DL_FLAG_STATELESS
)
417 WARN_ON(link
->status
!= DL_STATE_CONSUMER_PROBE
);
418 WRITE_ONCE(link
->status
, DL_STATE_ACTIVE
);
421 dev
->links
.status
= DL_DEV_DRIVER_BOUND
;
423 device_links_write_unlock();
427 * __device_links_no_driver - Update links of a device without a driver.
428 * @dev: Device without a drvier.
430 * Delete all non-persistent links from this device to any suppliers.
432 * Persistent links stay around, but their status is changed to "available",
433 * unless they already are in the "supplier unbind in progress" state in which
434 * case they need not be updated.
436 * Links with the DL_FLAG_STATELESS flag set are ignored.
438 static void __device_links_no_driver(struct device
*dev
)
440 struct device_link
*link
, *ln
;
442 list_for_each_entry_safe_reverse(link
, ln
, &dev
->links
.suppliers
, c_node
) {
443 if (link
->flags
& DL_FLAG_STATELESS
)
446 if (link
->flags
& DL_FLAG_AUTOREMOVE
)
447 __device_link_del(link
);
448 else if (link
->status
!= DL_STATE_SUPPLIER_UNBIND
)
449 WRITE_ONCE(link
->status
, DL_STATE_AVAILABLE
);
452 dev
->links
.status
= DL_DEV_NO_DRIVER
;
455 void device_links_no_driver(struct device
*dev
)
457 device_links_write_lock();
458 __device_links_no_driver(dev
);
459 device_links_write_unlock();
463 * device_links_driver_cleanup - Update links after driver removal.
464 * @dev: Device whose driver has just gone away.
466 * Update links to consumers for @dev by changing their status to "dormant" and
467 * invoke %__device_links_no_driver() to update links to suppliers for it as
470 * Links with the DL_FLAG_STATELESS flag set are ignored.
472 void device_links_driver_cleanup(struct device
*dev
)
474 struct device_link
*link
;
476 device_links_write_lock();
478 list_for_each_entry(link
, &dev
->links
.consumers
, s_node
) {
479 if (link
->flags
& DL_FLAG_STATELESS
)
482 WARN_ON(link
->flags
& DL_FLAG_AUTOREMOVE
);
483 WARN_ON(link
->status
!= DL_STATE_SUPPLIER_UNBIND
);
484 WRITE_ONCE(link
->status
, DL_STATE_DORMANT
);
487 __device_links_no_driver(dev
);
489 device_links_write_unlock();
493 * device_links_busy - Check if there are any busy links to consumers.
494 * @dev: Device to check.
496 * Check each consumer of the device and return 'true' if its link's status
497 * is one of "consumer probe" or "active" (meaning that the given consumer is
498 * probing right now or its driver is present). Otherwise, change the link
499 * state to "supplier unbind" to prevent the consumer from being probed
500 * successfully going forward.
502 * Return 'false' if there are no probing or active consumers.
504 * Links with the DL_FLAG_STATELESS flag set are ignored.
506 bool device_links_busy(struct device
*dev
)
508 struct device_link
*link
;
511 device_links_write_lock();
513 list_for_each_entry(link
, &dev
->links
.consumers
, s_node
) {
514 if (link
->flags
& DL_FLAG_STATELESS
)
517 if (link
->status
== DL_STATE_CONSUMER_PROBE
518 || link
->status
== DL_STATE_ACTIVE
) {
522 WRITE_ONCE(link
->status
, DL_STATE_SUPPLIER_UNBIND
);
525 dev
->links
.status
= DL_DEV_UNBINDING
;
527 device_links_write_unlock();
532 * device_links_unbind_consumers - Force unbind consumers of the given device.
533 * @dev: Device to unbind the consumers of.
535 * Walk the list of links to consumers for @dev and if any of them is in the
536 * "consumer probe" state, wait for all device probes in progress to complete
539 * If that's not the case, change the status of the link to "supplier unbind"
540 * and check if the link was in the "active" state. If so, force the consumer
541 * driver to unbind and start over (the consumer will not re-probe as we have
542 * changed the state of the link already).
544 * Links with the DL_FLAG_STATELESS flag set are ignored.
546 void device_links_unbind_consumers(struct device
*dev
)
548 struct device_link
*link
;
551 device_links_write_lock();
553 list_for_each_entry(link
, &dev
->links
.consumers
, s_node
) {
554 enum device_link_state status
;
556 if (link
->flags
& DL_FLAG_STATELESS
)
559 status
= link
->status
;
560 if (status
== DL_STATE_CONSUMER_PROBE
) {
561 device_links_write_unlock();
563 wait_for_device_probe();
566 WRITE_ONCE(link
->status
, DL_STATE_SUPPLIER_UNBIND
);
567 if (status
== DL_STATE_ACTIVE
) {
568 struct device
*consumer
= link
->consumer
;
570 get_device(consumer
);
572 device_links_write_unlock();
574 device_release_driver_internal(consumer
, NULL
,
576 put_device(consumer
);
581 device_links_write_unlock();
585 * device_links_purge - Delete existing links to other devices.
586 * @dev: Target device.
588 static void device_links_purge(struct device
*dev
)
590 struct device_link
*link
, *ln
;
593 * Delete all of the remaining links from this device to any other
594 * devices (either consumers or suppliers).
596 device_links_write_lock();
598 list_for_each_entry_safe_reverse(link
, ln
, &dev
->links
.suppliers
, c_node
) {
599 WARN_ON(link
->status
== DL_STATE_ACTIVE
);
600 __device_link_del(link
);
603 list_for_each_entry_safe_reverse(link
, ln
, &dev
->links
.consumers
, s_node
) {
604 WARN_ON(link
->status
!= DL_STATE_DORMANT
&&
605 link
->status
!= DL_STATE_NONE
);
606 __device_link_del(link
);
609 device_links_write_unlock();
612 /* Device links support end. */
614 int (*platform_notify
)(struct device
*dev
) = NULL
;
615 int (*platform_notify_remove
)(struct device
*dev
) = NULL
;
616 static struct kobject
*dev_kobj
;
617 struct kobject
*sysfs_dev_char_kobj
;
618 struct kobject
*sysfs_dev_block_kobj
;
620 static DEFINE_MUTEX(device_hotplug_lock
);
622 void lock_device_hotplug(void)
624 mutex_lock(&device_hotplug_lock
);
627 void unlock_device_hotplug(void)
629 mutex_unlock(&device_hotplug_lock
);
632 int lock_device_hotplug_sysfs(void)
634 if (mutex_trylock(&device_hotplug_lock
))
637 /* Avoid busy looping (5 ms of sleep should do). */
639 return restart_syscall();
643 static inline int device_is_not_partition(struct device
*dev
)
645 return !(dev
->type
== &part_type
);
648 static inline int device_is_not_partition(struct device
*dev
)
655 * dev_driver_string - Return a device's driver name, if at all possible
656 * @dev: struct device to get the name of
658 * Will return the device's driver's name if it is bound to a device. If
659 * the device is not bound to a driver, it will return the name of the bus
660 * it is attached to. If it is not attached to a bus either, an empty
661 * string will be returned.
663 const char *dev_driver_string(const struct device
*dev
)
665 struct device_driver
*drv
;
667 /* dev->driver can change to NULL underneath us because of unbinding,
668 * so be careful about accessing it. dev->bus and dev->class should
669 * never change once they are set, so they don't need special care.
671 drv
= READ_ONCE(dev
->driver
);
672 return drv
? drv
->name
:
673 (dev
->bus
? dev
->bus
->name
:
674 (dev
->class ? dev
->class->name
: ""));
676 EXPORT_SYMBOL(dev_driver_string
);
678 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
680 static ssize_t
dev_attr_show(struct kobject
*kobj
, struct attribute
*attr
,
683 struct device_attribute
*dev_attr
= to_dev_attr(attr
);
684 struct device
*dev
= kobj_to_dev(kobj
);
688 ret
= dev_attr
->show(dev
, dev_attr
, buf
);
689 if (ret
>= (ssize_t
)PAGE_SIZE
) {
690 printk("dev_attr_show: %pS returned bad count\n",
696 static ssize_t
dev_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
697 const char *buf
, size_t count
)
699 struct device_attribute
*dev_attr
= to_dev_attr(attr
);
700 struct device
*dev
= kobj_to_dev(kobj
);
704 ret
= dev_attr
->store(dev
, dev_attr
, buf
, count
);
708 static const struct sysfs_ops dev_sysfs_ops
= {
709 .show
= dev_attr_show
,
710 .store
= dev_attr_store
,
713 #define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
715 ssize_t
device_store_ulong(struct device
*dev
,
716 struct device_attribute
*attr
,
717 const char *buf
, size_t size
)
719 struct dev_ext_attribute
*ea
= to_ext_attr(attr
);
721 unsigned long new = simple_strtoul(buf
, &end
, 0);
724 *(unsigned long *)(ea
->var
) = new;
725 /* Always return full write size even if we didn't consume all */
728 EXPORT_SYMBOL_GPL(device_store_ulong
);
730 ssize_t
device_show_ulong(struct device
*dev
,
731 struct device_attribute
*attr
,
734 struct dev_ext_attribute
*ea
= to_ext_attr(attr
);
735 return snprintf(buf
, PAGE_SIZE
, "%lx\n", *(unsigned long *)(ea
->var
));
737 EXPORT_SYMBOL_GPL(device_show_ulong
);
739 ssize_t
device_store_int(struct device
*dev
,
740 struct device_attribute
*attr
,
741 const char *buf
, size_t size
)
743 struct dev_ext_attribute
*ea
= to_ext_attr(attr
);
745 long new = simple_strtol(buf
, &end
, 0);
746 if (end
== buf
|| new > INT_MAX
|| new < INT_MIN
)
748 *(int *)(ea
->var
) = new;
749 /* Always return full write size even if we didn't consume all */
752 EXPORT_SYMBOL_GPL(device_store_int
);
754 ssize_t
device_show_int(struct device
*dev
,
755 struct device_attribute
*attr
,
758 struct dev_ext_attribute
*ea
= to_ext_attr(attr
);
760 return snprintf(buf
, PAGE_SIZE
, "%d\n", *(int *)(ea
->var
));
762 EXPORT_SYMBOL_GPL(device_show_int
);
764 ssize_t
device_store_bool(struct device
*dev
, struct device_attribute
*attr
,
765 const char *buf
, size_t size
)
767 struct dev_ext_attribute
*ea
= to_ext_attr(attr
);
769 if (strtobool(buf
, ea
->var
) < 0)
774 EXPORT_SYMBOL_GPL(device_store_bool
);
776 ssize_t
device_show_bool(struct device
*dev
, struct device_attribute
*attr
,
779 struct dev_ext_attribute
*ea
= to_ext_attr(attr
);
781 return snprintf(buf
, PAGE_SIZE
, "%d\n", *(bool *)(ea
->var
));
783 EXPORT_SYMBOL_GPL(device_show_bool
);
786 * device_release - free device structure.
787 * @kobj: device's kobject.
789 * This is called once the reference count for the object
790 * reaches 0. We forward the call to the device's release
791 * method, which should handle actually freeing the structure.
793 static void device_release(struct kobject
*kobj
)
795 struct device
*dev
= kobj_to_dev(kobj
);
796 struct device_private
*p
= dev
->p
;
799 * Some platform devices are driven without driver attached
800 * and managed resources may have been acquired. Make sure
801 * all resources are released.
803 * Drivers still can add resources into device after device
804 * is deleted but alive, so release devres here to avoid
805 * possible memory leak.
807 devres_release_all(dev
);
811 else if (dev
->type
&& dev
->type
->release
)
812 dev
->type
->release(dev
);
813 else if (dev
->class && dev
->class->dev_release
)
814 dev
->class->dev_release(dev
);
816 WARN(1, KERN_ERR
"Device '%s' does not have a release() "
817 "function, it is broken and must be fixed.\n",
822 static const void *device_namespace(struct kobject
*kobj
)
824 struct device
*dev
= kobj_to_dev(kobj
);
825 const void *ns
= NULL
;
827 if (dev
->class && dev
->class->ns_type
)
828 ns
= dev
->class->namespace(dev
);
833 static struct kobj_type device_ktype
= {
834 .release
= device_release
,
835 .sysfs_ops
= &dev_sysfs_ops
,
836 .namespace = device_namespace
,
840 static int dev_uevent_filter(struct kset
*kset
, struct kobject
*kobj
)
842 struct kobj_type
*ktype
= get_ktype(kobj
);
844 if (ktype
== &device_ktype
) {
845 struct device
*dev
= kobj_to_dev(kobj
);
854 static const char *dev_uevent_name(struct kset
*kset
, struct kobject
*kobj
)
856 struct device
*dev
= kobj_to_dev(kobj
);
859 return dev
->bus
->name
;
861 return dev
->class->name
;
865 static int dev_uevent(struct kset
*kset
, struct kobject
*kobj
,
866 struct kobj_uevent_env
*env
)
868 struct device
*dev
= kobj_to_dev(kobj
);
871 /* add device node properties if present */
872 if (MAJOR(dev
->devt
)) {
876 kuid_t uid
= GLOBAL_ROOT_UID
;
877 kgid_t gid
= GLOBAL_ROOT_GID
;
879 add_uevent_var(env
, "MAJOR=%u", MAJOR(dev
->devt
));
880 add_uevent_var(env
, "MINOR=%u", MINOR(dev
->devt
));
881 name
= device_get_devnode(dev
, &mode
, &uid
, &gid
, &tmp
);
883 add_uevent_var(env
, "DEVNAME=%s", name
);
885 add_uevent_var(env
, "DEVMODE=%#o", mode
& 0777);
886 if (!uid_eq(uid
, GLOBAL_ROOT_UID
))
887 add_uevent_var(env
, "DEVUID=%u", from_kuid(&init_user_ns
, uid
));
888 if (!gid_eq(gid
, GLOBAL_ROOT_GID
))
889 add_uevent_var(env
, "DEVGID=%u", from_kgid(&init_user_ns
, gid
));
894 if (dev
->type
&& dev
->type
->name
)
895 add_uevent_var(env
, "DEVTYPE=%s", dev
->type
->name
);
898 add_uevent_var(env
, "DRIVER=%s", dev
->driver
->name
);
900 /* Add common DT information about the device */
901 of_device_uevent(dev
, env
);
903 /* have the bus specific function add its stuff */
904 if (dev
->bus
&& dev
->bus
->uevent
) {
905 retval
= dev
->bus
->uevent(dev
, env
);
907 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
908 dev_name(dev
), __func__
, retval
);
911 /* have the class specific function add its stuff */
912 if (dev
->class && dev
->class->dev_uevent
) {
913 retval
= dev
->class->dev_uevent(dev
, env
);
915 pr_debug("device: '%s': %s: class uevent() "
916 "returned %d\n", dev_name(dev
),
920 /* have the device type specific function add its stuff */
921 if (dev
->type
&& dev
->type
->uevent
) {
922 retval
= dev
->type
->uevent(dev
, env
);
924 pr_debug("device: '%s': %s: dev_type uevent() "
925 "returned %d\n", dev_name(dev
),
932 static const struct kset_uevent_ops device_uevent_ops
= {
933 .filter
= dev_uevent_filter
,
934 .name
= dev_uevent_name
,
935 .uevent
= dev_uevent
,
938 static ssize_t
uevent_show(struct device
*dev
, struct device_attribute
*attr
,
941 struct kobject
*top_kobj
;
943 struct kobj_uevent_env
*env
= NULL
;
948 /* search the kset, the device belongs to */
949 top_kobj
= &dev
->kobj
;
950 while (!top_kobj
->kset
&& top_kobj
->parent
)
951 top_kobj
= top_kobj
->parent
;
955 kset
= top_kobj
->kset
;
956 if (!kset
->uevent_ops
|| !kset
->uevent_ops
->uevent
)
960 if (kset
->uevent_ops
&& kset
->uevent_ops
->filter
)
961 if (!kset
->uevent_ops
->filter(kset
, &dev
->kobj
))
964 env
= kzalloc(sizeof(struct kobj_uevent_env
), GFP_KERNEL
);
968 /* let the kset specific function add its keys */
969 retval
= kset
->uevent_ops
->uevent(kset
, &dev
->kobj
, env
);
973 /* copy keys to file */
974 for (i
= 0; i
< env
->envp_idx
; i
++)
975 count
+= sprintf(&buf
[count
], "%s\n", env
->envp
[i
]);
981 static ssize_t
uevent_store(struct device
*dev
, struct device_attribute
*attr
,
982 const char *buf
, size_t count
)
984 if (kobject_synth_uevent(&dev
->kobj
, buf
, count
))
985 dev_err(dev
, "uevent: failed to send synthetic uevent\n");
989 static DEVICE_ATTR_RW(uevent
);
991 static ssize_t
online_show(struct device
*dev
, struct device_attribute
*attr
,
999 return sprintf(buf
, "%u\n", val
);
1002 static ssize_t
online_store(struct device
*dev
, struct device_attribute
*attr
,
1003 const char *buf
, size_t count
)
1008 ret
= strtobool(buf
, &val
);
1012 ret
= lock_device_hotplug_sysfs();
1016 ret
= val
? device_online(dev
) : device_offline(dev
);
1017 unlock_device_hotplug();
1018 return ret
< 0 ? ret
: count
;
1020 static DEVICE_ATTR_RW(online
);
1022 int device_add_groups(struct device
*dev
, const struct attribute_group
**groups
)
1024 return sysfs_create_groups(&dev
->kobj
, groups
);
1026 EXPORT_SYMBOL_GPL(device_add_groups
);
1028 void device_remove_groups(struct device
*dev
,
1029 const struct attribute_group
**groups
)
1031 sysfs_remove_groups(&dev
->kobj
, groups
);
1033 EXPORT_SYMBOL_GPL(device_remove_groups
);
1035 union device_attr_group_devres
{
1036 const struct attribute_group
*group
;
1037 const struct attribute_group
**groups
;
1040 static int devm_attr_group_match(struct device
*dev
, void *res
, void *data
)
1042 return ((union device_attr_group_devres
*)res
)->group
== data
;
1045 static void devm_attr_group_remove(struct device
*dev
, void *res
)
1047 union device_attr_group_devres
*devres
= res
;
1048 const struct attribute_group
*group
= devres
->group
;
1050 dev_dbg(dev
, "%s: removing group %p\n", __func__
, group
);
1051 sysfs_remove_group(&dev
->kobj
, group
);
1054 static void devm_attr_groups_remove(struct device
*dev
, void *res
)
1056 union device_attr_group_devres
*devres
= res
;
1057 const struct attribute_group
**groups
= devres
->groups
;
1059 dev_dbg(dev
, "%s: removing groups %p\n", __func__
, groups
);
1060 sysfs_remove_groups(&dev
->kobj
, groups
);
1064 * devm_device_add_group - given a device, create a managed attribute group
1065 * @dev: The device to create the group for
1066 * @grp: The attribute group to create
1068 * This function creates a group for the first time. It will explicitly
1069 * warn and error if any of the attribute files being created already exist.
1071 * Returns 0 on success or error code on failure.
1073 int devm_device_add_group(struct device
*dev
, const struct attribute_group
*grp
)
1075 union device_attr_group_devres
*devres
;
1078 devres
= devres_alloc(devm_attr_group_remove
,
1079 sizeof(*devres
), GFP_KERNEL
);
1083 error
= sysfs_create_group(&dev
->kobj
, grp
);
1085 devres_free(devres
);
1089 devres
->group
= grp
;
1090 devres_add(dev
, devres
);
1093 EXPORT_SYMBOL_GPL(devm_device_add_group
);
1096 * devm_device_remove_group: remove a managed group from a device
1097 * @dev: device to remove the group from
1098 * @grp: group to remove
1100 * This function removes a group of attributes from a device. The attributes
1101 * previously have to have been created for this group, otherwise it will fail.
1103 void devm_device_remove_group(struct device
*dev
,
1104 const struct attribute_group
*grp
)
1106 WARN_ON(devres_release(dev
, devm_attr_group_remove
,
1107 devm_attr_group_match
,
1108 /* cast away const */ (void *)grp
));
1110 EXPORT_SYMBOL_GPL(devm_device_remove_group
);
1113 * devm_device_add_groups - create a bunch of managed attribute groups
1114 * @dev: The device to create the group for
1115 * @groups: The attribute groups to create, NULL terminated
1117 * This function creates a bunch of managed attribute groups. If an error
1118 * occurs when creating a group, all previously created groups will be
1119 * removed, unwinding everything back to the original state when this
1120 * function was called. It will explicitly warn and error if any of the
1121 * attribute files being created already exist.
1123 * Returns 0 on success or error code from sysfs_create_group on failure.
1125 int devm_device_add_groups(struct device
*dev
,
1126 const struct attribute_group
**groups
)
1128 union device_attr_group_devres
*devres
;
1131 devres
= devres_alloc(devm_attr_groups_remove
,
1132 sizeof(*devres
), GFP_KERNEL
);
1136 error
= sysfs_create_groups(&dev
->kobj
, groups
);
1138 devres_free(devres
);
1142 devres
->groups
= groups
;
1143 devres_add(dev
, devres
);
1146 EXPORT_SYMBOL_GPL(devm_device_add_groups
);
1149 * devm_device_remove_groups - remove a list of managed groups
1151 * @dev: The device for the groups to be removed from
1152 * @groups: NULL terminated list of groups to be removed
1154 * If groups is not NULL, remove the specified groups from the device.
1156 void devm_device_remove_groups(struct device
*dev
,
1157 const struct attribute_group
**groups
)
1159 WARN_ON(devres_release(dev
, devm_attr_groups_remove
,
1160 devm_attr_group_match
,
1161 /* cast away const */ (void *)groups
));
1163 EXPORT_SYMBOL_GPL(devm_device_remove_groups
);
1165 static int device_add_attrs(struct device
*dev
)
1167 struct class *class = dev
->class;
1168 const struct device_type
*type
= dev
->type
;
1172 error
= device_add_groups(dev
, class->dev_groups
);
1178 error
= device_add_groups(dev
, type
->groups
);
1180 goto err_remove_class_groups
;
1183 error
= device_add_groups(dev
, dev
->groups
);
1185 goto err_remove_type_groups
;
1187 if (device_supports_offline(dev
) && !dev
->offline_disabled
) {
1188 error
= device_create_file(dev
, &dev_attr_online
);
1190 goto err_remove_dev_groups
;
1195 err_remove_dev_groups
:
1196 device_remove_groups(dev
, dev
->groups
);
1197 err_remove_type_groups
:
1199 device_remove_groups(dev
, type
->groups
);
1200 err_remove_class_groups
:
1202 device_remove_groups(dev
, class->dev_groups
);
1207 static void device_remove_attrs(struct device
*dev
)
1209 struct class *class = dev
->class;
1210 const struct device_type
*type
= dev
->type
;
1212 device_remove_file(dev
, &dev_attr_online
);
1213 device_remove_groups(dev
, dev
->groups
);
1216 device_remove_groups(dev
, type
->groups
);
1219 device_remove_groups(dev
, class->dev_groups
);
1222 static ssize_t
dev_show(struct device
*dev
, struct device_attribute
*attr
,
1225 return print_dev_t(buf
, dev
->devt
);
1227 static DEVICE_ATTR_RO(dev
);
1230 struct kset
*devices_kset
;
1233 * devices_kset_move_before - Move device in the devices_kset's list.
1234 * @deva: Device to move.
1235 * @devb: Device @deva should come before.
1237 static void devices_kset_move_before(struct device
*deva
, struct device
*devb
)
1241 pr_debug("devices_kset: Moving %s before %s\n",
1242 dev_name(deva
), dev_name(devb
));
1243 spin_lock(&devices_kset
->list_lock
);
1244 list_move_tail(&deva
->kobj
.entry
, &devb
->kobj
.entry
);
1245 spin_unlock(&devices_kset
->list_lock
);
1249 * devices_kset_move_after - Move device in the devices_kset's list.
1250 * @deva: Device to move
1251 * @devb: Device @deva should come after.
1253 static void devices_kset_move_after(struct device
*deva
, struct device
*devb
)
1257 pr_debug("devices_kset: Moving %s after %s\n",
1258 dev_name(deva
), dev_name(devb
));
1259 spin_lock(&devices_kset
->list_lock
);
1260 list_move(&deva
->kobj
.entry
, &devb
->kobj
.entry
);
1261 spin_unlock(&devices_kset
->list_lock
);
1265 * devices_kset_move_last - move the device to the end of devices_kset's list.
1266 * @dev: device to move
1268 void devices_kset_move_last(struct device
*dev
)
1272 pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev
));
1273 spin_lock(&devices_kset
->list_lock
);
1274 list_move_tail(&dev
->kobj
.entry
, &devices_kset
->list
);
1275 spin_unlock(&devices_kset
->list_lock
);
1279 * device_create_file - create sysfs attribute file for device.
1281 * @attr: device attribute descriptor.
1283 int device_create_file(struct device
*dev
,
1284 const struct device_attribute
*attr
)
1289 WARN(((attr
->attr
.mode
& S_IWUGO
) && !attr
->store
),
1290 "Attribute %s: write permission without 'store'\n",
1292 WARN(((attr
->attr
.mode
& S_IRUGO
) && !attr
->show
),
1293 "Attribute %s: read permission without 'show'\n",
1295 error
= sysfs_create_file(&dev
->kobj
, &attr
->attr
);
1300 EXPORT_SYMBOL_GPL(device_create_file
);
1303 * device_remove_file - remove sysfs attribute file.
1305 * @attr: device attribute descriptor.
1307 void device_remove_file(struct device
*dev
,
1308 const struct device_attribute
*attr
)
1311 sysfs_remove_file(&dev
->kobj
, &attr
->attr
);
1313 EXPORT_SYMBOL_GPL(device_remove_file
);
1316 * device_remove_file_self - remove sysfs attribute file from its own method.
1318 * @attr: device attribute descriptor.
1320 * See kernfs_remove_self() for details.
1322 bool device_remove_file_self(struct device
*dev
,
1323 const struct device_attribute
*attr
)
1326 return sysfs_remove_file_self(&dev
->kobj
, &attr
->attr
);
1330 EXPORT_SYMBOL_GPL(device_remove_file_self
);
1333 * device_create_bin_file - create sysfs binary attribute file for device.
1335 * @attr: device binary attribute descriptor.
1337 int device_create_bin_file(struct device
*dev
,
1338 const struct bin_attribute
*attr
)
1340 int error
= -EINVAL
;
1342 error
= sysfs_create_bin_file(&dev
->kobj
, attr
);
1345 EXPORT_SYMBOL_GPL(device_create_bin_file
);
1348 * device_remove_bin_file - remove sysfs binary attribute file
1350 * @attr: device binary attribute descriptor.
1352 void device_remove_bin_file(struct device
*dev
,
1353 const struct bin_attribute
*attr
)
1356 sysfs_remove_bin_file(&dev
->kobj
, attr
);
1358 EXPORT_SYMBOL_GPL(device_remove_bin_file
);
1360 static void klist_children_get(struct klist_node
*n
)
1362 struct device_private
*p
= to_device_private_parent(n
);
1363 struct device
*dev
= p
->device
;
1368 static void klist_children_put(struct klist_node
*n
)
1370 struct device_private
*p
= to_device_private_parent(n
);
1371 struct device
*dev
= p
->device
;
1377 * device_initialize - init device structure.
1380 * This prepares the device for use by other layers by initializing
1382 * It is the first half of device_register(), if called by
1383 * that function, though it can also be called separately, so one
1384 * may use @dev's fields. In particular, get_device()/put_device()
1385 * may be used for reference counting of @dev after calling this
1388 * All fields in @dev must be initialized by the caller to 0, except
1389 * for those explicitly set to some other value. The simplest
1390 * approach is to use kzalloc() to allocate the structure containing
1393 * NOTE: Use put_device() to give up your reference instead of freeing
1394 * @dev directly once you have called this function.
1396 void device_initialize(struct device
*dev
)
1398 dev
->kobj
.kset
= devices_kset
;
1399 kobject_init(&dev
->kobj
, &device_ktype
);
1400 INIT_LIST_HEAD(&dev
->dma_pools
);
1401 mutex_init(&dev
->mutex
);
1402 lockdep_set_novalidate_class(&dev
->mutex
);
1403 spin_lock_init(&dev
->devres_lock
);
1404 INIT_LIST_HEAD(&dev
->devres_head
);
1405 device_pm_init(dev
);
1406 set_dev_node(dev
, -1);
1407 #ifdef CONFIG_GENERIC_MSI_IRQ
1408 INIT_LIST_HEAD(&dev
->msi_list
);
1410 INIT_LIST_HEAD(&dev
->links
.consumers
);
1411 INIT_LIST_HEAD(&dev
->links
.suppliers
);
1412 dev
->links
.status
= DL_DEV_NO_DRIVER
;
1414 EXPORT_SYMBOL_GPL(device_initialize
);
1416 struct kobject
*virtual_device_parent(struct device
*dev
)
1418 static struct kobject
*virtual_dir
= NULL
;
1421 virtual_dir
= kobject_create_and_add("virtual",
1422 &devices_kset
->kobj
);
1428 struct kobject kobj
;
1429 struct class *class;
1432 #define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
1434 static void class_dir_release(struct kobject
*kobj
)
1436 struct class_dir
*dir
= to_class_dir(kobj
);
1441 struct kobj_ns_type_operations
*class_dir_child_ns_type(struct kobject
*kobj
)
1443 struct class_dir
*dir
= to_class_dir(kobj
);
1444 return dir
->class->ns_type
;
1447 static struct kobj_type class_dir_ktype
= {
1448 .release
= class_dir_release
,
1449 .sysfs_ops
= &kobj_sysfs_ops
,
1450 .child_ns_type
= class_dir_child_ns_type
1453 static struct kobject
*
1454 class_dir_create_and_add(struct class *class, struct kobject
*parent_kobj
)
1456 struct class_dir
*dir
;
1459 dir
= kzalloc(sizeof(*dir
), GFP_KERNEL
);
1464 kobject_init(&dir
->kobj
, &class_dir_ktype
);
1466 dir
->kobj
.kset
= &class->p
->glue_dirs
;
1468 retval
= kobject_add(&dir
->kobj
, parent_kobj
, "%s", class->name
);
1470 kobject_put(&dir
->kobj
);
1476 static DEFINE_MUTEX(gdp_mutex
);
1478 static struct kobject
*get_device_parent(struct device
*dev
,
1479 struct device
*parent
)
1482 struct kobject
*kobj
= NULL
;
1483 struct kobject
*parent_kobj
;
1487 /* block disks show up in /sys/block */
1488 if (sysfs_deprecated
&& dev
->class == &block_class
) {
1489 if (parent
&& parent
->class == &block_class
)
1490 return &parent
->kobj
;
1491 return &block_class
.p
->subsys
.kobj
;
1496 * If we have no parent, we live in "virtual".
1497 * Class-devices with a non class-device as parent, live
1498 * in a "glue" directory to prevent namespace collisions.
1501 parent_kobj
= virtual_device_parent(dev
);
1502 else if (parent
->class && !dev
->class->ns_type
)
1503 return &parent
->kobj
;
1505 parent_kobj
= &parent
->kobj
;
1507 mutex_lock(&gdp_mutex
);
1509 /* find our class-directory at the parent and reference it */
1510 spin_lock(&dev
->class->p
->glue_dirs
.list_lock
);
1511 list_for_each_entry(k
, &dev
->class->p
->glue_dirs
.list
, entry
)
1512 if (k
->parent
== parent_kobj
) {
1513 kobj
= kobject_get(k
);
1516 spin_unlock(&dev
->class->p
->glue_dirs
.list_lock
);
1518 mutex_unlock(&gdp_mutex
);
1522 /* or create a new class-directory at the parent device */
1523 k
= class_dir_create_and_add(dev
->class, parent_kobj
);
1524 /* do not emit an uevent for this simple "glue" directory */
1525 mutex_unlock(&gdp_mutex
);
1529 /* subsystems can specify a default root directory for their devices */
1530 if (!parent
&& dev
->bus
&& dev
->bus
->dev_root
)
1531 return &dev
->bus
->dev_root
->kobj
;
1534 return &parent
->kobj
;
1538 static inline bool live_in_glue_dir(struct kobject
*kobj
,
1541 if (!kobj
|| !dev
->class ||
1542 kobj
->kset
!= &dev
->class->p
->glue_dirs
)
1547 static inline struct kobject
*get_glue_dir(struct device
*dev
)
1549 return dev
->kobj
.parent
;
1553 * make sure cleaning up dir as the last step, we need to make
1554 * sure .release handler of kobject is run with holding the
1557 static void cleanup_glue_dir(struct device
*dev
, struct kobject
*glue_dir
)
1559 /* see if we live in a "glue" directory */
1560 if (!live_in_glue_dir(glue_dir
, dev
))
1563 mutex_lock(&gdp_mutex
);
1564 kobject_put(glue_dir
);
1565 mutex_unlock(&gdp_mutex
);
1568 static int device_add_class_symlinks(struct device
*dev
)
1570 struct device_node
*of_node
= dev_of_node(dev
);
1574 error
= sysfs_create_link(&dev
->kobj
, of_node_kobj(of_node
), "of_node");
1576 dev_warn(dev
, "Error %d creating of_node link\n",error
);
1577 /* An error here doesn't warrant bringing down the device */
1583 error
= sysfs_create_link(&dev
->kobj
,
1584 &dev
->class->p
->subsys
.kobj
,
1589 if (dev
->parent
&& device_is_not_partition(dev
)) {
1590 error
= sysfs_create_link(&dev
->kobj
, &dev
->parent
->kobj
,
1597 /* /sys/block has directories and does not need symlinks */
1598 if (sysfs_deprecated
&& dev
->class == &block_class
)
1602 /* link in the class directory pointing to the device */
1603 error
= sysfs_create_link(&dev
->class->p
->subsys
.kobj
,
1604 &dev
->kobj
, dev_name(dev
));
1611 sysfs_remove_link(&dev
->kobj
, "device");
1614 sysfs_remove_link(&dev
->kobj
, "subsystem");
1616 sysfs_remove_link(&dev
->kobj
, "of_node");
1620 static void device_remove_class_symlinks(struct device
*dev
)
1622 if (dev_of_node(dev
))
1623 sysfs_remove_link(&dev
->kobj
, "of_node");
1628 if (dev
->parent
&& device_is_not_partition(dev
))
1629 sysfs_remove_link(&dev
->kobj
, "device");
1630 sysfs_remove_link(&dev
->kobj
, "subsystem");
1632 if (sysfs_deprecated
&& dev
->class == &block_class
)
1635 sysfs_delete_link(&dev
->class->p
->subsys
.kobj
, &dev
->kobj
, dev_name(dev
));
1639 * dev_set_name - set a device name
1641 * @fmt: format string for the device's name
1643 int dev_set_name(struct device
*dev
, const char *fmt
, ...)
1648 va_start(vargs
, fmt
);
1649 err
= kobject_set_name_vargs(&dev
->kobj
, fmt
, vargs
);
1653 EXPORT_SYMBOL_GPL(dev_set_name
);
1656 * device_to_dev_kobj - select a /sys/dev/ directory for the device
1659 * By default we select char/ for new entries. Setting class->dev_obj
1660 * to NULL prevents an entry from being created. class->dev_kobj must
1661 * be set (or cleared) before any devices are registered to the class
1662 * otherwise device_create_sys_dev_entry() and
1663 * device_remove_sys_dev_entry() will disagree about the presence of
1666 static struct kobject
*device_to_dev_kobj(struct device
*dev
)
1668 struct kobject
*kobj
;
1671 kobj
= dev
->class->dev_kobj
;
1673 kobj
= sysfs_dev_char_kobj
;
1678 static int device_create_sys_dev_entry(struct device
*dev
)
1680 struct kobject
*kobj
= device_to_dev_kobj(dev
);
1685 format_dev_t(devt_str
, dev
->devt
);
1686 error
= sysfs_create_link(kobj
, &dev
->kobj
, devt_str
);
1692 static void device_remove_sys_dev_entry(struct device
*dev
)
1694 struct kobject
*kobj
= device_to_dev_kobj(dev
);
1698 format_dev_t(devt_str
, dev
->devt
);
1699 sysfs_remove_link(kobj
, devt_str
);
1703 int device_private_init(struct device
*dev
)
1705 dev
->p
= kzalloc(sizeof(*dev
->p
), GFP_KERNEL
);
1708 dev
->p
->device
= dev
;
1709 klist_init(&dev
->p
->klist_children
, klist_children_get
,
1710 klist_children_put
);
1711 INIT_LIST_HEAD(&dev
->p
->deferred_probe
);
1716 * device_add - add device to device hierarchy.
1719 * This is part 2 of device_register(), though may be called
1720 * separately _iff_ device_initialize() has been called separately.
1722 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
1723 * to the global and sibling lists for the device, then
1724 * adds it to the other relevant subsystems of the driver model.
1726 * Do not call this routine or device_register() more than once for
1727 * any device structure. The driver model core is not designed to work
1728 * with devices that get unregistered and then spring back to life.
1729 * (Among other things, it's very hard to guarantee that all references
1730 * to the previous incarnation of @dev have been dropped.) Allocate
1731 * and register a fresh new struct device instead.
1733 * NOTE: _Never_ directly free @dev after calling this function, even
1734 * if it returned an error! Always use put_device() to give up your
1735 * reference instead.
1737 int device_add(struct device
*dev
)
1739 struct device
*parent
;
1740 struct kobject
*kobj
;
1741 struct class_interface
*class_intf
;
1742 int error
= -EINVAL
;
1743 struct kobject
*glue_dir
= NULL
;
1745 dev
= get_device(dev
);
1750 error
= device_private_init(dev
);
1756 * for statically allocated devices, which should all be converted
1757 * some day, we need to initialize the name. We prevent reading back
1758 * the name, and force the use of dev_name()
1760 if (dev
->init_name
) {
1761 dev_set_name(dev
, "%s", dev
->init_name
);
1762 dev
->init_name
= NULL
;
1765 /* subsystems can specify simple device enumeration */
1766 if (!dev_name(dev
) && dev
->bus
&& dev
->bus
->dev_name
)
1767 dev_set_name(dev
, "%s%u", dev
->bus
->dev_name
, dev
->id
);
1769 if (!dev_name(dev
)) {
1774 pr_debug("device: '%s': %s\n", dev_name(dev
), __func__
);
1776 parent
= get_device(dev
->parent
);
1777 kobj
= get_device_parent(dev
, parent
);
1779 dev
->kobj
.parent
= kobj
;
1781 /* use parent numa_node */
1782 if (parent
&& (dev_to_node(dev
) == NUMA_NO_NODE
))
1783 set_dev_node(dev
, dev_to_node(parent
));
1785 /* first, register with generic layer. */
1786 /* we require the name to be set before, and pass NULL */
1787 error
= kobject_add(&dev
->kobj
, dev
->kobj
.parent
, NULL
);
1789 glue_dir
= get_glue_dir(dev
);
1793 /* notify platform of device entry */
1794 if (platform_notify
)
1795 platform_notify(dev
);
1797 error
= device_create_file(dev
, &dev_attr_uevent
);
1801 error
= device_add_class_symlinks(dev
);
1804 error
= device_add_attrs(dev
);
1807 error
= bus_add_device(dev
);
1810 error
= dpm_sysfs_add(dev
);
1815 if (MAJOR(dev
->devt
)) {
1816 error
= device_create_file(dev
, &dev_attr_dev
);
1820 error
= device_create_sys_dev_entry(dev
);
1824 devtmpfs_create_node(dev
);
1827 /* Notify clients of device addition. This call must come
1828 * after dpm_sysfs_add() and before kobject_uevent().
1831 blocking_notifier_call_chain(&dev
->bus
->p
->bus_notifier
,
1832 BUS_NOTIFY_ADD_DEVICE
, dev
);
1834 kobject_uevent(&dev
->kobj
, KOBJ_ADD
);
1835 bus_probe_device(dev
);
1837 klist_add_tail(&dev
->p
->knode_parent
,
1838 &parent
->p
->klist_children
);
1841 mutex_lock(&dev
->class->p
->mutex
);
1842 /* tie the class to the device */
1843 klist_add_tail(&dev
->knode_class
,
1844 &dev
->class->p
->klist_devices
);
1846 /* notify any interfaces that the device is here */
1847 list_for_each_entry(class_intf
,
1848 &dev
->class->p
->interfaces
, node
)
1849 if (class_intf
->add_dev
)
1850 class_intf
->add_dev(dev
, class_intf
);
1851 mutex_unlock(&dev
->class->p
->mutex
);
1857 if (MAJOR(dev
->devt
))
1858 device_remove_file(dev
, &dev_attr_dev
);
1860 device_pm_remove(dev
);
1861 dpm_sysfs_remove(dev
);
1863 bus_remove_device(dev
);
1865 device_remove_attrs(dev
);
1867 device_remove_class_symlinks(dev
);
1869 device_remove_file(dev
, &dev_attr_uevent
);
1871 kobject_uevent(&dev
->kobj
, KOBJ_REMOVE
);
1872 glue_dir
= get_glue_dir(dev
);
1873 kobject_del(&dev
->kobj
);
1875 cleanup_glue_dir(dev
, glue_dir
);
1882 EXPORT_SYMBOL_GPL(device_add
);
1885 * device_register - register a device with the system.
1886 * @dev: pointer to the device structure
1888 * This happens in two clean steps - initialize the device
1889 * and add it to the system. The two steps can be called
1890 * separately, but this is the easiest and most common.
1891 * I.e. you should only call the two helpers separately if
1892 * have a clearly defined need to use and refcount the device
1893 * before it is added to the hierarchy.
1895 * For more information, see the kerneldoc for device_initialize()
1898 * NOTE: _Never_ directly free @dev after calling this function, even
1899 * if it returned an error! Always use put_device() to give up the
1900 * reference initialized in this function instead.
1902 int device_register(struct device
*dev
)
1904 device_initialize(dev
);
1905 return device_add(dev
);
1907 EXPORT_SYMBOL_GPL(device_register
);
1910 * get_device - increment reference count for device.
1913 * This simply forwards the call to kobject_get(), though
1914 * we do take care to provide for the case that we get a NULL
1915 * pointer passed in.
1917 struct device
*get_device(struct device
*dev
)
1919 return dev
? kobj_to_dev(kobject_get(&dev
->kobj
)) : NULL
;
1921 EXPORT_SYMBOL_GPL(get_device
);
1924 * put_device - decrement reference count.
1925 * @dev: device in question.
1927 void put_device(struct device
*dev
)
1929 /* might_sleep(); */
1931 kobject_put(&dev
->kobj
);
1933 EXPORT_SYMBOL_GPL(put_device
);
1936 * device_del - delete device from system.
1939 * This is the first part of the device unregistration
1940 * sequence. This removes the device from the lists we control
1941 * from here, has it removed from the other driver model
1942 * subsystems it was added to in device_add(), and removes it
1943 * from the kobject hierarchy.
1945 * NOTE: this should be called manually _iff_ device_add() was
1946 * also called manually.
1948 void device_del(struct device
*dev
)
1950 struct device
*parent
= dev
->parent
;
1951 struct kobject
*glue_dir
= NULL
;
1952 struct class_interface
*class_intf
;
1954 /* Notify clients of device removal. This call must come
1955 * before dpm_sysfs_remove().
1958 blocking_notifier_call_chain(&dev
->bus
->p
->bus_notifier
,
1959 BUS_NOTIFY_DEL_DEVICE
, dev
);
1961 dpm_sysfs_remove(dev
);
1963 klist_del(&dev
->p
->knode_parent
);
1964 if (MAJOR(dev
->devt
)) {
1965 devtmpfs_delete_node(dev
);
1966 device_remove_sys_dev_entry(dev
);
1967 device_remove_file(dev
, &dev_attr_dev
);
1970 device_remove_class_symlinks(dev
);
1972 mutex_lock(&dev
->class->p
->mutex
);
1973 /* notify any interfaces that the device is now gone */
1974 list_for_each_entry(class_intf
,
1975 &dev
->class->p
->interfaces
, node
)
1976 if (class_intf
->remove_dev
)
1977 class_intf
->remove_dev(dev
, class_intf
);
1978 /* remove the device from the class list */
1979 klist_del(&dev
->knode_class
);
1980 mutex_unlock(&dev
->class->p
->mutex
);
1982 device_remove_file(dev
, &dev_attr_uevent
);
1983 device_remove_attrs(dev
);
1984 bus_remove_device(dev
);
1985 device_pm_remove(dev
);
1986 driver_deferred_probe_del(dev
);
1987 device_remove_properties(dev
);
1988 device_links_purge(dev
);
1990 /* Notify the platform of the removal, in case they
1991 * need to do anything...
1993 if (platform_notify_remove
)
1994 platform_notify_remove(dev
);
1996 blocking_notifier_call_chain(&dev
->bus
->p
->bus_notifier
,
1997 BUS_NOTIFY_REMOVED_DEVICE
, dev
);
1998 kobject_uevent(&dev
->kobj
, KOBJ_REMOVE
);
1999 glue_dir
= get_glue_dir(dev
);
2000 kobject_del(&dev
->kobj
);
2001 cleanup_glue_dir(dev
, glue_dir
);
2004 EXPORT_SYMBOL_GPL(device_del
);
2007 * device_unregister - unregister device from system.
2008 * @dev: device going away.
2010 * We do this in two parts, like we do device_register(). First,
2011 * we remove it from all the subsystems with device_del(), then
2012 * we decrement the reference count via put_device(). If that
2013 * is the final reference count, the device will be cleaned up
2014 * via device_release() above. Otherwise, the structure will
2015 * stick around until the final reference to the device is dropped.
2017 void device_unregister(struct device
*dev
)
2019 pr_debug("device: '%s': %s\n", dev_name(dev
), __func__
);
2023 EXPORT_SYMBOL_GPL(device_unregister
);
2025 static struct device
*prev_device(struct klist_iter
*i
)
2027 struct klist_node
*n
= klist_prev(i
);
2028 struct device
*dev
= NULL
;
2029 struct device_private
*p
;
2032 p
= to_device_private_parent(n
);
2038 static struct device
*next_device(struct klist_iter
*i
)
2040 struct klist_node
*n
= klist_next(i
);
2041 struct device
*dev
= NULL
;
2042 struct device_private
*p
;
2045 p
= to_device_private_parent(n
);
2052 * device_get_devnode - path of device node file
2054 * @mode: returned file access mode
2055 * @uid: returned file owner
2056 * @gid: returned file group
2057 * @tmp: possibly allocated string
2059 * Return the relative path of a possible device node.
2060 * Non-default names may need to allocate a memory to compose
2061 * a name. This memory is returned in tmp and needs to be
2062 * freed by the caller.
2064 const char *device_get_devnode(struct device
*dev
,
2065 umode_t
*mode
, kuid_t
*uid
, kgid_t
*gid
,
2072 /* the device type may provide a specific name */
2073 if (dev
->type
&& dev
->type
->devnode
)
2074 *tmp
= dev
->type
->devnode(dev
, mode
, uid
, gid
);
2078 /* the class may provide a specific name */
2079 if (dev
->class && dev
->class->devnode
)
2080 *tmp
= dev
->class->devnode(dev
, mode
);
2084 /* return name without allocation, tmp == NULL */
2085 if (strchr(dev_name(dev
), '!') == NULL
)
2086 return dev_name(dev
);
2088 /* replace '!' in the name with '/' */
2089 s
= kstrdup(dev_name(dev
), GFP_KERNEL
);
2092 strreplace(s
, '!', '/');
2097 * device_for_each_child - device child iterator.
2098 * @parent: parent struct device.
2099 * @fn: function to be called for each device.
2100 * @data: data for the callback.
2102 * Iterate over @parent's child devices, and call @fn for each,
2105 * We check the return of @fn each time. If it returns anything
2106 * other than 0, we break out and return that value.
2108 int device_for_each_child(struct device
*parent
, void *data
,
2109 int (*fn
)(struct device
*dev
, void *data
))
2111 struct klist_iter i
;
2112 struct device
*child
;
2118 klist_iter_init(&parent
->p
->klist_children
, &i
);
2119 while (!error
&& (child
= next_device(&i
)))
2120 error
= fn(child
, data
);
2121 klist_iter_exit(&i
);
2124 EXPORT_SYMBOL_GPL(device_for_each_child
);
2127 * device_for_each_child_reverse - device child iterator in reversed order.
2128 * @parent: parent struct device.
2129 * @fn: function to be called for each device.
2130 * @data: data for the callback.
2132 * Iterate over @parent's child devices, and call @fn for each,
2135 * We check the return of @fn each time. If it returns anything
2136 * other than 0, we break out and return that value.
2138 int device_for_each_child_reverse(struct device
*parent
, void *data
,
2139 int (*fn
)(struct device
*dev
, void *data
))
2141 struct klist_iter i
;
2142 struct device
*child
;
2148 klist_iter_init(&parent
->p
->klist_children
, &i
);
2149 while ((child
= prev_device(&i
)) && !error
)
2150 error
= fn(child
, data
);
2151 klist_iter_exit(&i
);
2154 EXPORT_SYMBOL_GPL(device_for_each_child_reverse
);
2157 * device_find_child - device iterator for locating a particular device.
2158 * @parent: parent struct device
2159 * @match: Callback function to check device
2160 * @data: Data to pass to match function
2162 * This is similar to the device_for_each_child() function above, but it
2163 * returns a reference to a device that is 'found' for later use, as
2164 * determined by the @match callback.
2166 * The callback should return 0 if the device doesn't match and non-zero
2167 * if it does. If the callback returns non-zero and a reference to the
2168 * current device can be obtained, this function will return to the caller
2169 * and not iterate over any more devices.
2171 * NOTE: you will need to drop the reference with put_device() after use.
2173 struct device
*device_find_child(struct device
*parent
, void *data
,
2174 int (*match
)(struct device
*dev
, void *data
))
2176 struct klist_iter i
;
2177 struct device
*child
;
2182 klist_iter_init(&parent
->p
->klist_children
, &i
);
2183 while ((child
= next_device(&i
)))
2184 if (match(child
, data
) && get_device(child
))
2186 klist_iter_exit(&i
);
2189 EXPORT_SYMBOL_GPL(device_find_child
);
2191 int __init
devices_init(void)
2193 devices_kset
= kset_create_and_add("devices", &device_uevent_ops
, NULL
);
2196 dev_kobj
= kobject_create_and_add("dev", NULL
);
2199 sysfs_dev_block_kobj
= kobject_create_and_add("block", dev_kobj
);
2200 if (!sysfs_dev_block_kobj
)
2201 goto block_kobj_err
;
2202 sysfs_dev_char_kobj
= kobject_create_and_add("char", dev_kobj
);
2203 if (!sysfs_dev_char_kobj
)
2209 kobject_put(sysfs_dev_block_kobj
);
2211 kobject_put(dev_kobj
);
2213 kset_unregister(devices_kset
);
2217 static int device_check_offline(struct device
*dev
, void *not_used
)
2221 ret
= device_for_each_child(dev
, NULL
, device_check_offline
);
2225 return device_supports_offline(dev
) && !dev
->offline
? -EBUSY
: 0;
2229 * device_offline - Prepare the device for hot-removal.
2230 * @dev: Device to be put offline.
2232 * Execute the device bus type's .offline() callback, if present, to prepare
2233 * the device for a subsequent hot-removal. If that succeeds, the device must
2234 * not be used until either it is removed or its bus type's .online() callback
2237 * Call under device_hotplug_lock.
2239 int device_offline(struct device
*dev
)
2243 if (dev
->offline_disabled
)
2246 ret
= device_for_each_child(dev
, NULL
, device_check_offline
);
2251 if (device_supports_offline(dev
)) {
2255 ret
= dev
->bus
->offline(dev
);
2257 kobject_uevent(&dev
->kobj
, KOBJ_OFFLINE
);
2258 dev
->offline
= true;
2268 * device_online - Put the device back online after successful device_offline().
2269 * @dev: Device to be put back online.
2271 * If device_offline() has been successfully executed for @dev, but the device
2272 * has not been removed subsequently, execute its bus type's .online() callback
2273 * to indicate that the device can be used again.
2275 * Call under device_hotplug_lock.
2277 int device_online(struct device
*dev
)
2282 if (device_supports_offline(dev
)) {
2284 ret
= dev
->bus
->online(dev
);
2286 kobject_uevent(&dev
->kobj
, KOBJ_ONLINE
);
2287 dev
->offline
= false;
2298 struct root_device
{
2300 struct module
*owner
;
2303 static inline struct root_device
*to_root_device(struct device
*d
)
2305 return container_of(d
, struct root_device
, dev
);
2308 static void root_device_release(struct device
*dev
)
2310 kfree(to_root_device(dev
));
2314 * __root_device_register - allocate and register a root device
2315 * @name: root device name
2316 * @owner: owner module of the root device, usually THIS_MODULE
2318 * This function allocates a root device and registers it
2319 * using device_register(). In order to free the returned
2320 * device, use root_device_unregister().
2322 * Root devices are dummy devices which allow other devices
2323 * to be grouped under /sys/devices. Use this function to
2324 * allocate a root device and then use it as the parent of
2325 * any device which should appear under /sys/devices/{name}
2327 * The /sys/devices/{name} directory will also contain a
2328 * 'module' symlink which points to the @owner directory
2331 * Returns &struct device pointer on success, or ERR_PTR() on error.
2333 * Note: You probably want to use root_device_register().
2335 struct device
*__root_device_register(const char *name
, struct module
*owner
)
2337 struct root_device
*root
;
2340 root
= kzalloc(sizeof(struct root_device
), GFP_KERNEL
);
2342 return ERR_PTR(err
);
2344 err
= dev_set_name(&root
->dev
, "%s", name
);
2347 return ERR_PTR(err
);
2350 root
->dev
.release
= root_device_release
;
2352 err
= device_register(&root
->dev
);
2354 put_device(&root
->dev
);
2355 return ERR_PTR(err
);
2358 #ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
2360 struct module_kobject
*mk
= &owner
->mkobj
;
2362 err
= sysfs_create_link(&root
->dev
.kobj
, &mk
->kobj
, "module");
2364 device_unregister(&root
->dev
);
2365 return ERR_PTR(err
);
2367 root
->owner
= owner
;
2373 EXPORT_SYMBOL_GPL(__root_device_register
);
2376 * root_device_unregister - unregister and free a root device
2377 * @dev: device going away
2379 * This function unregisters and cleans up a device that was created by
2380 * root_device_register().
2382 void root_device_unregister(struct device
*dev
)
2384 struct root_device
*root
= to_root_device(dev
);
2387 sysfs_remove_link(&root
->dev
.kobj
, "module");
2389 device_unregister(dev
);
2391 EXPORT_SYMBOL_GPL(root_device_unregister
);
2394 static void device_create_release(struct device
*dev
)
2396 pr_debug("device: '%s': %s\n", dev_name(dev
), __func__
);
2400 static struct device
*
2401 device_create_groups_vargs(struct class *class, struct device
*parent
,
2402 dev_t devt
, void *drvdata
,
2403 const struct attribute_group
**groups
,
2404 const char *fmt
, va_list args
)
2406 struct device
*dev
= NULL
;
2407 int retval
= -ENODEV
;
2409 if (class == NULL
|| IS_ERR(class))
2412 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
2418 device_initialize(dev
);
2421 dev
->parent
= parent
;
2422 dev
->groups
= groups
;
2423 dev
->release
= device_create_release
;
2424 dev_set_drvdata(dev
, drvdata
);
2426 retval
= kobject_set_name_vargs(&dev
->kobj
, fmt
, args
);
2430 retval
= device_add(dev
);
2438 return ERR_PTR(retval
);
2442 * device_create_vargs - creates a device and registers it with sysfs
2443 * @class: pointer to the struct class that this device should be registered to
2444 * @parent: pointer to the parent struct device of this new device, if any
2445 * @devt: the dev_t for the char device to be added
2446 * @drvdata: the data to be added to the device for callbacks
2447 * @fmt: string for the device's name
2448 * @args: va_list for the device's name
2450 * This function can be used by char device classes. A struct device
2451 * will be created in sysfs, registered to the specified class.
2453 * A "dev" file will be created, showing the dev_t for the device, if
2454 * the dev_t is not 0,0.
2455 * If a pointer to a parent struct device is passed in, the newly created
2456 * struct device will be a child of that device in sysfs.
2457 * The pointer to the struct device will be returned from the call.
2458 * Any further sysfs files that might be required can be created using this
2461 * Returns &struct device pointer on success, or ERR_PTR() on error.
2463 * Note: the struct class passed to this function must have previously
2464 * been created with a call to class_create().
2466 struct device
*device_create_vargs(struct class *class, struct device
*parent
,
2467 dev_t devt
, void *drvdata
, const char *fmt
,
2470 return device_create_groups_vargs(class, parent
, devt
, drvdata
, NULL
,
2473 EXPORT_SYMBOL_GPL(device_create_vargs
);
2476 * device_create - creates a device and registers it with sysfs
2477 * @class: pointer to the struct class that this device should be registered to
2478 * @parent: pointer to the parent struct device of this new device, if any
2479 * @devt: the dev_t for the char device to be added
2480 * @drvdata: the data to be added to the device for callbacks
2481 * @fmt: string for the device's name
2483 * This function can be used by char device classes. A struct device
2484 * will be created in sysfs, registered to the specified class.
2486 * A "dev" file will be created, showing the dev_t for the device, if
2487 * the dev_t is not 0,0.
2488 * If a pointer to a parent struct device is passed in, the newly created
2489 * struct device will be a child of that device in sysfs.
2490 * The pointer to the struct device will be returned from the call.
2491 * Any further sysfs files that might be required can be created using this
2494 * Returns &struct device pointer on success, or ERR_PTR() on error.
2496 * Note: the struct class passed to this function must have previously
2497 * been created with a call to class_create().
2499 struct device
*device_create(struct class *class, struct device
*parent
,
2500 dev_t devt
, void *drvdata
, const char *fmt
, ...)
2505 va_start(vargs
, fmt
);
2506 dev
= device_create_vargs(class, parent
, devt
, drvdata
, fmt
, vargs
);
2510 EXPORT_SYMBOL_GPL(device_create
);
2513 * device_create_with_groups - creates a device and registers it with sysfs
2514 * @class: pointer to the struct class that this device should be registered to
2515 * @parent: pointer to the parent struct device of this new device, if any
2516 * @devt: the dev_t for the char device to be added
2517 * @drvdata: the data to be added to the device for callbacks
2518 * @groups: NULL-terminated list of attribute groups to be created
2519 * @fmt: string for the device's name
2521 * This function can be used by char device classes. A struct device
2522 * will be created in sysfs, registered to the specified class.
2523 * Additional attributes specified in the groups parameter will also
2524 * be created automatically.
2526 * A "dev" file will be created, showing the dev_t for the device, if
2527 * the dev_t is not 0,0.
2528 * If a pointer to a parent struct device is passed in, the newly created
2529 * struct device will be a child of that device in sysfs.
2530 * The pointer to the struct device will be returned from the call.
2531 * Any further sysfs files that might be required can be created using this
2534 * Returns &struct device pointer on success, or ERR_PTR() on error.
2536 * Note: the struct class passed to this function must have previously
2537 * been created with a call to class_create().
2539 struct device
*device_create_with_groups(struct class *class,
2540 struct device
*parent
, dev_t devt
,
2542 const struct attribute_group
**groups
,
2543 const char *fmt
, ...)
2548 va_start(vargs
, fmt
);
2549 dev
= device_create_groups_vargs(class, parent
, devt
, drvdata
, groups
,
2554 EXPORT_SYMBOL_GPL(device_create_with_groups
);
2556 static int __match_devt(struct device
*dev
, const void *data
)
2558 const dev_t
*devt
= data
;
2560 return dev
->devt
== *devt
;
2564 * device_destroy - removes a device that was created with device_create()
2565 * @class: pointer to the struct class that this device was registered with
2566 * @devt: the dev_t of the device that was previously registered
2568 * This call unregisters and cleans up a device that was created with a
2569 * call to device_create().
2571 void device_destroy(struct class *class, dev_t devt
)
2575 dev
= class_find_device(class, NULL
, &devt
, __match_devt
);
2578 device_unregister(dev
);
2581 EXPORT_SYMBOL_GPL(device_destroy
);
2584 * device_rename - renames a device
2585 * @dev: the pointer to the struct device to be renamed
2586 * @new_name: the new name of the device
2588 * It is the responsibility of the caller to provide mutual
2589 * exclusion between two different calls of device_rename
2590 * on the same device to ensure that new_name is valid and
2591 * won't conflict with other devices.
2593 * Note: Don't call this function. Currently, the networking layer calls this
2594 * function, but that will change. The following text from Kay Sievers offers
2597 * Renaming devices is racy at many levels, symlinks and other stuff are not
2598 * replaced atomically, and you get a "move" uevent, but it's not easy to
2599 * connect the event to the old and new device. Device nodes are not renamed at
2600 * all, there isn't even support for that in the kernel now.
2602 * In the meantime, during renaming, your target name might be taken by another
2603 * driver, creating conflicts. Or the old name is taken directly after you
2604 * renamed it -- then you get events for the same DEVPATH, before you even see
2605 * the "move" event. It's just a mess, and nothing new should ever rely on
2606 * kernel device renaming. Besides that, it's not even implemented now for
2607 * other things than (driver-core wise very simple) network devices.
2609 * We are currently about to change network renaming in udev to completely
2610 * disallow renaming of devices in the same namespace as the kernel uses,
2611 * because we can't solve the problems properly, that arise with swapping names
2612 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
2613 * be allowed to some other name than eth[0-9]*, for the aforementioned
2616 * Make up a "real" name in the driver before you register anything, or add
2617 * some other attributes for userspace to find the device, or use udev to add
2618 * symlinks -- but never rename kernel devices later, it's a complete mess. We
2619 * don't even want to get into that and try to implement the missing pieces in
2620 * the core. We really have other pieces to fix in the driver core mess. :)
2622 int device_rename(struct device
*dev
, const char *new_name
)
2624 struct kobject
*kobj
= &dev
->kobj
;
2625 char *old_device_name
= NULL
;
2628 dev
= get_device(dev
);
2632 dev_dbg(dev
, "renaming to %s\n", new_name
);
2634 old_device_name
= kstrdup(dev_name(dev
), GFP_KERNEL
);
2635 if (!old_device_name
) {
2641 error
= sysfs_rename_link_ns(&dev
->class->p
->subsys
.kobj
,
2642 kobj
, old_device_name
,
2643 new_name
, kobject_namespace(kobj
));
2648 error
= kobject_rename(kobj
, new_name
);
2655 kfree(old_device_name
);
2659 EXPORT_SYMBOL_GPL(device_rename
);
2661 static int device_move_class_links(struct device
*dev
,
2662 struct device
*old_parent
,
2663 struct device
*new_parent
)
2668 sysfs_remove_link(&dev
->kobj
, "device");
2670 error
= sysfs_create_link(&dev
->kobj
, &new_parent
->kobj
,
2676 * device_move - moves a device to a new parent
2677 * @dev: the pointer to the struct device to be moved
2678 * @new_parent: the new parent of the device (can by NULL)
2679 * @dpm_order: how to reorder the dpm_list
2681 int device_move(struct device
*dev
, struct device
*new_parent
,
2682 enum dpm_order dpm_order
)
2685 struct device
*old_parent
;
2686 struct kobject
*new_parent_kobj
;
2688 dev
= get_device(dev
);
2693 new_parent
= get_device(new_parent
);
2694 new_parent_kobj
= get_device_parent(dev
, new_parent
);
2696 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev
),
2697 __func__
, new_parent
? dev_name(new_parent
) : "<NULL>");
2698 error
= kobject_move(&dev
->kobj
, new_parent_kobj
);
2700 cleanup_glue_dir(dev
, new_parent_kobj
);
2701 put_device(new_parent
);
2704 old_parent
= dev
->parent
;
2705 dev
->parent
= new_parent
;
2707 klist_remove(&dev
->p
->knode_parent
);
2709 klist_add_tail(&dev
->p
->knode_parent
,
2710 &new_parent
->p
->klist_children
);
2711 set_dev_node(dev
, dev_to_node(new_parent
));
2715 error
= device_move_class_links(dev
, old_parent
, new_parent
);
2717 /* We ignore errors on cleanup since we're hosed anyway... */
2718 device_move_class_links(dev
, new_parent
, old_parent
);
2719 if (!kobject_move(&dev
->kobj
, &old_parent
->kobj
)) {
2721 klist_remove(&dev
->p
->knode_parent
);
2722 dev
->parent
= old_parent
;
2724 klist_add_tail(&dev
->p
->knode_parent
,
2725 &old_parent
->p
->klist_children
);
2726 set_dev_node(dev
, dev_to_node(old_parent
));
2729 cleanup_glue_dir(dev
, new_parent_kobj
);
2730 put_device(new_parent
);
2734 switch (dpm_order
) {
2735 case DPM_ORDER_NONE
:
2737 case DPM_ORDER_DEV_AFTER_PARENT
:
2738 device_pm_move_after(dev
, new_parent
);
2739 devices_kset_move_after(dev
, new_parent
);
2741 case DPM_ORDER_PARENT_BEFORE_DEV
:
2742 device_pm_move_before(new_parent
, dev
);
2743 devices_kset_move_before(new_parent
, dev
);
2745 case DPM_ORDER_DEV_LAST
:
2746 device_pm_move_last(dev
);
2747 devices_kset_move_last(dev
);
2751 put_device(old_parent
);
2757 EXPORT_SYMBOL_GPL(device_move
);
2760 * device_shutdown - call ->shutdown() on each device to shutdown.
2762 void device_shutdown(void)
2764 struct device
*dev
, *parent
;
2766 spin_lock(&devices_kset
->list_lock
);
2768 * Walk the devices list backward, shutting down each in turn.
2769 * Beware that device unplug events may also start pulling
2770 * devices offline, even as the system is shutting down.
2772 while (!list_empty(&devices_kset
->list
)) {
2773 dev
= list_entry(devices_kset
->list
.prev
, struct device
,
2777 * hold reference count of device's parent to
2778 * prevent it from being freed because parent's
2779 * lock is to be held
2781 parent
= get_device(dev
->parent
);
2784 * Make sure the device is off the kset list, in the
2785 * event that dev->*->shutdown() doesn't remove it.
2787 list_del_init(&dev
->kobj
.entry
);
2788 spin_unlock(&devices_kset
->list_lock
);
2790 /* hold lock to avoid race with probe/release */
2792 device_lock(parent
);
2795 /* Don't allow any more runtime suspends */
2796 pm_runtime_get_noresume(dev
);
2797 pm_runtime_barrier(dev
);
2799 if (dev
->class && dev
->class->shutdown_pre
) {
2801 dev_info(dev
, "shutdown_pre\n");
2802 dev
->class->shutdown_pre(dev
);
2804 if (dev
->bus
&& dev
->bus
->shutdown
) {
2806 dev_info(dev
, "shutdown\n");
2807 dev
->bus
->shutdown(dev
);
2808 } else if (dev
->driver
&& dev
->driver
->shutdown
) {
2810 dev_info(dev
, "shutdown\n");
2811 dev
->driver
->shutdown(dev
);
2816 device_unlock(parent
);
2821 spin_lock(&devices_kset
->list_lock
);
2823 spin_unlock(&devices_kset
->list_lock
);
2827 * Device logging functions
2830 #ifdef CONFIG_PRINTK
2832 create_syslog_header(const struct device
*dev
, char *hdr
, size_t hdrlen
)
2838 subsys
= dev
->class->name
;
2840 subsys
= dev
->bus
->name
;
2844 pos
+= snprintf(hdr
+ pos
, hdrlen
- pos
, "SUBSYSTEM=%s", subsys
);
2849 * Add device identifier DEVICE=:
2853 * +sound:card0 subsystem:devname
2855 if (MAJOR(dev
->devt
)) {
2858 if (strcmp(subsys
, "block") == 0)
2863 pos
+= snprintf(hdr
+ pos
, hdrlen
- pos
,
2865 c
, MAJOR(dev
->devt
), MINOR(dev
->devt
));
2866 } else if (strcmp(subsys
, "net") == 0) {
2867 struct net_device
*net
= to_net_dev(dev
);
2870 pos
+= snprintf(hdr
+ pos
, hdrlen
- pos
,
2871 "DEVICE=n%u", net
->ifindex
);
2874 pos
+= snprintf(hdr
+ pos
, hdrlen
- pos
,
2875 "DEVICE=+%s:%s", subsys
, dev_name(dev
));
2884 dev_WARN(dev
, "device/subsystem name too long");
2888 int dev_vprintk_emit(int level
, const struct device
*dev
,
2889 const char *fmt
, va_list args
)
2894 hdrlen
= create_syslog_header(dev
, hdr
, sizeof(hdr
));
2896 return vprintk_emit(0, level
, hdrlen
? hdr
: NULL
, hdrlen
, fmt
, args
);
2898 EXPORT_SYMBOL(dev_vprintk_emit
);
2900 int dev_printk_emit(int level
, const struct device
*dev
, const char *fmt
, ...)
2905 va_start(args
, fmt
);
2907 r
= dev_vprintk_emit(level
, dev
, fmt
, args
);
2913 EXPORT_SYMBOL(dev_printk_emit
);
2915 static void __dev_printk(const char *level
, const struct device
*dev
,
2916 struct va_format
*vaf
)
2919 dev_printk_emit(level
[1] - '0', dev
, "%s %s: %pV",
2920 dev_driver_string(dev
), dev_name(dev
), vaf
);
2922 printk("%s(NULL device *): %pV", level
, vaf
);
2925 void dev_printk(const char *level
, const struct device
*dev
,
2926 const char *fmt
, ...)
2928 struct va_format vaf
;
2931 va_start(args
, fmt
);
2936 __dev_printk(level
, dev
, &vaf
);
2940 EXPORT_SYMBOL(dev_printk
);
2942 #define define_dev_printk_level(func, kern_level) \
2943 void func(const struct device *dev, const char *fmt, ...) \
2945 struct va_format vaf; \
2948 va_start(args, fmt); \
2953 __dev_printk(kern_level, dev, &vaf); \
2957 EXPORT_SYMBOL(func);
2959 define_dev_printk_level(dev_emerg
, KERN_EMERG
);
2960 define_dev_printk_level(dev_alert
, KERN_ALERT
);
2961 define_dev_printk_level(dev_crit
, KERN_CRIT
);
2962 define_dev_printk_level(dev_err
, KERN_ERR
);
2963 define_dev_printk_level(dev_warn
, KERN_WARNING
);
2964 define_dev_printk_level(dev_notice
, KERN_NOTICE
);
2965 define_dev_printk_level(_dev_info
, KERN_INFO
);
2969 static inline bool fwnode_is_primary(struct fwnode_handle
*fwnode
)
2971 return fwnode
&& !IS_ERR(fwnode
->secondary
);
2975 * set_primary_fwnode - Change the primary firmware node of a given device.
2976 * @dev: Device to handle.
2977 * @fwnode: New primary firmware node of the device.
2979 * Set the device's firmware node pointer to @fwnode, but if a secondary
2980 * firmware node of the device is present, preserve it.
2982 void set_primary_fwnode(struct device
*dev
, struct fwnode_handle
*fwnode
)
2985 struct fwnode_handle
*fn
= dev
->fwnode
;
2987 if (fwnode_is_primary(fn
))
2991 WARN_ON(fwnode
->secondary
);
2992 fwnode
->secondary
= fn
;
2994 dev
->fwnode
= fwnode
;
2996 dev
->fwnode
= fwnode_is_primary(dev
->fwnode
) ?
2997 dev
->fwnode
->secondary
: NULL
;
3000 EXPORT_SYMBOL_GPL(set_primary_fwnode
);
3003 * set_secondary_fwnode - Change the secondary firmware node of a given device.
3004 * @dev: Device to handle.
3005 * @fwnode: New secondary firmware node of the device.
3007 * If a primary firmware node of the device is present, set its secondary
3008 * pointer to @fwnode. Otherwise, set the device's firmware node pointer to
3011 void set_secondary_fwnode(struct device
*dev
, struct fwnode_handle
*fwnode
)
3014 fwnode
->secondary
= ERR_PTR(-ENODEV
);
3016 if (fwnode_is_primary(dev
->fwnode
))
3017 dev
->fwnode
->secondary
= fwnode
;
3019 dev
->fwnode
= fwnode
;
3023 * device_set_of_node_from_dev - reuse device-tree node of another device
3024 * @dev: device whose device-tree node is being set
3025 * @dev2: device whose device-tree node is being reused
3027 * Takes another reference to the new device-tree node after first dropping
3028 * any reference held to the old node.
3030 void device_set_of_node_from_dev(struct device
*dev
, const struct device
*dev2
)
3032 of_node_put(dev
->of_node
);
3033 dev
->of_node
= of_node_get(dev2
->of_node
);
3034 dev
->of_node_reused
= true;
3036 EXPORT_SYMBOL_GPL(device_set_of_node_from_dev
);