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/cpufreq.h>
13 #include <linux/device.h>
14 #include <linux/err.h>
15 #include <linux/fwnode.h>
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/string.h>
20 #include <linux/kdev_t.h>
21 #include <linux/notifier.h>
23 #include <linux/of_device.h>
24 #include <linux/genhd.h>
25 #include <linux/mutex.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/netdevice.h>
28 #include <linux/sched/signal.h>
29 #include <linux/sysfs.h>
32 #include "power/power.h"
34 #ifdef CONFIG_SYSFS_DEPRECATED
35 #ifdef CONFIG_SYSFS_DEPRECATED_V2
36 long sysfs_deprecated
= 1;
38 long sysfs_deprecated
= 0;
40 static int __init
sysfs_deprecated_setup(char *arg
)
42 return kstrtol(arg
, 10, &sysfs_deprecated
);
44 early_param("sysfs.deprecated", sysfs_deprecated_setup
);
47 /* Device links support. */
48 static LIST_HEAD(wait_for_suppliers
);
49 static DEFINE_MUTEX(wfs_lock
);
50 static LIST_HEAD(deferred_sync
);
51 static unsigned int defer_sync_state_count
= 1;
54 static DEFINE_MUTEX(device_links_lock
);
55 DEFINE_STATIC_SRCU(device_links_srcu
);
57 static inline void device_links_write_lock(void)
59 mutex_lock(&device_links_lock
);
62 static inline void device_links_write_unlock(void)
64 mutex_unlock(&device_links_lock
);
67 int device_links_read_lock(void)
69 return srcu_read_lock(&device_links_srcu
);
72 void device_links_read_unlock(int idx
)
74 srcu_read_unlock(&device_links_srcu
, idx
);
77 int device_links_read_lock_held(void)
79 return srcu_read_lock_held(&device_links_srcu
);
81 #else /* !CONFIG_SRCU */
82 static DECLARE_RWSEM(device_links_lock
);
84 static inline void device_links_write_lock(void)
86 down_write(&device_links_lock
);
89 static inline void device_links_write_unlock(void)
91 up_write(&device_links_lock
);
94 int device_links_read_lock(void)
96 down_read(&device_links_lock
);
100 void device_links_read_unlock(int not_used
)
102 up_read(&device_links_lock
);
105 #ifdef CONFIG_DEBUG_LOCK_ALLOC
106 int device_links_read_lock_held(void)
108 return lockdep_is_held(&device_links_lock
);
111 #endif /* !CONFIG_SRCU */
114 * device_is_dependent - Check if one device depends on another one
115 * @dev: Device to check dependencies for.
116 * @target: Device to check against.
118 * Check if @target depends on @dev or any device dependent on it (its child or
119 * its consumer etc). Return 1 if that is the case or 0 otherwise.
121 static int device_is_dependent(struct device
*dev
, void *target
)
123 struct device_link
*link
;
129 ret
= device_for_each_child(dev
, target
, device_is_dependent
);
133 list_for_each_entry(link
, &dev
->links
.consumers
, s_node
) {
134 if (link
->flags
== (DL_FLAG_SYNC_STATE_ONLY
| DL_FLAG_MANAGED
))
137 if (link
->consumer
== target
)
140 ret
= device_is_dependent(link
->consumer
, target
);
147 static void device_link_init_status(struct device_link
*link
,
148 struct device
*consumer
,
149 struct device
*supplier
)
151 switch (supplier
->links
.status
) {
153 switch (consumer
->links
.status
) {
156 * A consumer driver can create a link to a supplier
157 * that has not completed its probing yet as long as it
158 * knows that the supplier is already functional (for
159 * example, it has just acquired some resources from the
162 link
->status
= DL_STATE_CONSUMER_PROBE
;
165 link
->status
= DL_STATE_DORMANT
;
169 case DL_DEV_DRIVER_BOUND
:
170 switch (consumer
->links
.status
) {
172 link
->status
= DL_STATE_CONSUMER_PROBE
;
174 case DL_DEV_DRIVER_BOUND
:
175 link
->status
= DL_STATE_ACTIVE
;
178 link
->status
= DL_STATE_AVAILABLE
;
182 case DL_DEV_UNBINDING
:
183 link
->status
= DL_STATE_SUPPLIER_UNBIND
;
186 link
->status
= DL_STATE_DORMANT
;
191 static int device_reorder_to_tail(struct device
*dev
, void *not_used
)
193 struct device_link
*link
;
196 * Devices that have not been registered yet will be put to the ends
197 * of the lists during the registration, so skip them here.
199 if (device_is_registered(dev
))
200 devices_kset_move_last(dev
);
202 if (device_pm_initialized(dev
))
203 device_pm_move_last(dev
);
205 device_for_each_child(dev
, NULL
, device_reorder_to_tail
);
206 list_for_each_entry(link
, &dev
->links
.consumers
, s_node
) {
207 if (link
->flags
== (DL_FLAG_SYNC_STATE_ONLY
| DL_FLAG_MANAGED
))
209 device_reorder_to_tail(link
->consumer
, NULL
);
216 * device_pm_move_to_tail - Move set of devices to the end of device lists
217 * @dev: Device to move
219 * This is a device_reorder_to_tail() wrapper taking the requisite locks.
221 * It moves the @dev along with all of its children and all of its consumers
222 * to the ends of the device_kset and dpm_list, recursively.
224 void device_pm_move_to_tail(struct device
*dev
)
228 idx
= device_links_read_lock();
230 device_reorder_to_tail(dev
, NULL
);
232 device_links_read_unlock(idx
);
235 #define DL_MANAGED_LINK_FLAGS (DL_FLAG_AUTOREMOVE_CONSUMER | \
236 DL_FLAG_AUTOREMOVE_SUPPLIER | \
237 DL_FLAG_AUTOPROBE_CONSUMER | \
238 DL_FLAG_SYNC_STATE_ONLY)
240 #define DL_ADD_VALID_FLAGS (DL_MANAGED_LINK_FLAGS | DL_FLAG_STATELESS | \
241 DL_FLAG_PM_RUNTIME | DL_FLAG_RPM_ACTIVE)
244 * device_link_add - Create a link between two devices.
245 * @consumer: Consumer end of the link.
246 * @supplier: Supplier end of the link.
247 * @flags: Link flags.
249 * The caller is responsible for the proper synchronization of the link creation
250 * with runtime PM. First, setting the DL_FLAG_PM_RUNTIME flag will cause the
251 * runtime PM framework to take the link into account. Second, if the
252 * DL_FLAG_RPM_ACTIVE flag is set in addition to it, the supplier devices will
253 * be forced into the active metastate and reference-counted upon the creation
254 * of the link. If DL_FLAG_PM_RUNTIME is not set, DL_FLAG_RPM_ACTIVE will be
257 * If DL_FLAG_STATELESS is set in @flags, the caller of this function is
258 * expected to release the link returned by it directly with the help of either
259 * device_link_del() or device_link_remove().
261 * If that flag is not set, however, the caller of this function is handing the
262 * management of the link over to the driver core entirely and its return value
263 * can only be used to check whether or not the link is present. In that case,
264 * the DL_FLAG_AUTOREMOVE_CONSUMER and DL_FLAG_AUTOREMOVE_SUPPLIER device link
265 * flags can be used to indicate to the driver core when the link can be safely
266 * deleted. Namely, setting one of them in @flags indicates to the driver core
267 * that the link is not going to be used (by the given caller of this function)
268 * after unbinding the consumer or supplier driver, respectively, from its
269 * device, so the link can be deleted at that point. If none of them is set,
270 * the link will be maintained until one of the devices pointed to by it (either
271 * the consumer or the supplier) is unregistered.
273 * Also, if DL_FLAG_STATELESS, DL_FLAG_AUTOREMOVE_CONSUMER and
274 * DL_FLAG_AUTOREMOVE_SUPPLIER are not set in @flags (that is, a persistent
275 * managed device link is being added), the DL_FLAG_AUTOPROBE_CONSUMER flag can
276 * be used to request the driver core to automaticall probe for a consmer
277 * driver after successfully binding a driver to the supplier device.
279 * The combination of DL_FLAG_STATELESS and one of DL_FLAG_AUTOREMOVE_CONSUMER,
280 * DL_FLAG_AUTOREMOVE_SUPPLIER, or DL_FLAG_AUTOPROBE_CONSUMER set in @flags at
281 * the same time is invalid and will cause NULL to be returned upfront.
282 * However, if a device link between the given @consumer and @supplier pair
283 * exists already when this function is called for them, the existing link will
284 * be returned regardless of its current type and status (the link's flags may
285 * be modified then). The caller of this function is then expected to treat
286 * the link as though it has just been created, so (in particular) if
287 * DL_FLAG_STATELESS was passed in @flags, the link needs to be released
288 * explicitly when not needed any more (as stated above).
290 * A side effect of the link creation is re-ordering of dpm_list and the
291 * devices_kset list by moving the consumer device and all devices depending
292 * on it to the ends of these lists (that does not happen to devices that have
293 * not been registered when this function is called).
295 * The supplier device is required to be registered when this function is called
296 * and NULL will be returned if that is not the case. The consumer device need
297 * not be registered, however.
299 struct device_link
*device_link_add(struct device
*consumer
,
300 struct device
*supplier
, u32 flags
)
302 struct device_link
*link
;
304 if (!consumer
|| !supplier
|| flags
& ~DL_ADD_VALID_FLAGS
||
305 (flags
& DL_FLAG_STATELESS
&& flags
& DL_MANAGED_LINK_FLAGS
) ||
306 (flags
& DL_FLAG_SYNC_STATE_ONLY
&&
307 flags
!= DL_FLAG_SYNC_STATE_ONLY
) ||
308 (flags
& DL_FLAG_AUTOPROBE_CONSUMER
&&
309 flags
& (DL_FLAG_AUTOREMOVE_CONSUMER
|
310 DL_FLAG_AUTOREMOVE_SUPPLIER
)))
313 if (flags
& DL_FLAG_PM_RUNTIME
&& flags
& DL_FLAG_RPM_ACTIVE
) {
314 if (pm_runtime_get_sync(supplier
) < 0) {
315 pm_runtime_put_noidle(supplier
);
320 if (!(flags
& DL_FLAG_STATELESS
))
321 flags
|= DL_FLAG_MANAGED
;
323 device_links_write_lock();
327 * If the supplier has not been fully registered yet or there is a
328 * reverse (non-SYNC_STATE_ONLY) dependency between the consumer and
329 * the supplier already in the graph, return NULL. If the link is a
330 * SYNC_STATE_ONLY link, we don't check for reverse dependencies
331 * because it only affects sync_state() callbacks.
333 if (!device_pm_initialized(supplier
)
334 || (!(flags
& DL_FLAG_SYNC_STATE_ONLY
) &&
335 device_is_dependent(consumer
, supplier
))) {
341 * DL_FLAG_AUTOREMOVE_SUPPLIER indicates that the link will be needed
342 * longer than for DL_FLAG_AUTOREMOVE_CONSUMER and setting them both
343 * together doesn't make sense, so prefer DL_FLAG_AUTOREMOVE_SUPPLIER.
345 if (flags
& DL_FLAG_AUTOREMOVE_SUPPLIER
)
346 flags
&= ~DL_FLAG_AUTOREMOVE_CONSUMER
;
348 list_for_each_entry(link
, &supplier
->links
.consumers
, s_node
) {
349 if (link
->consumer
!= consumer
)
352 if (flags
& DL_FLAG_PM_RUNTIME
) {
353 if (!(link
->flags
& DL_FLAG_PM_RUNTIME
)) {
354 pm_runtime_new_link(consumer
);
355 link
->flags
|= DL_FLAG_PM_RUNTIME
;
357 if (flags
& DL_FLAG_RPM_ACTIVE
)
358 refcount_inc(&link
->rpm_active
);
361 if (flags
& DL_FLAG_STATELESS
) {
362 kref_get(&link
->kref
);
363 if (link
->flags
& DL_FLAG_SYNC_STATE_ONLY
&&
364 !(link
->flags
& DL_FLAG_STATELESS
)) {
365 link
->flags
|= DL_FLAG_STATELESS
;
373 * If the life time of the link following from the new flags is
374 * longer than indicated by the flags of the existing link,
375 * update the existing link to stay around longer.
377 if (flags
& DL_FLAG_AUTOREMOVE_SUPPLIER
) {
378 if (link
->flags
& DL_FLAG_AUTOREMOVE_CONSUMER
) {
379 link
->flags
&= ~DL_FLAG_AUTOREMOVE_CONSUMER
;
380 link
->flags
|= DL_FLAG_AUTOREMOVE_SUPPLIER
;
382 } else if (!(flags
& DL_FLAG_AUTOREMOVE_CONSUMER
)) {
383 link
->flags
&= ~(DL_FLAG_AUTOREMOVE_CONSUMER
|
384 DL_FLAG_AUTOREMOVE_SUPPLIER
);
386 if (!(link
->flags
& DL_FLAG_MANAGED
)) {
387 kref_get(&link
->kref
);
388 link
->flags
|= DL_FLAG_MANAGED
;
389 device_link_init_status(link
, consumer
, supplier
);
391 if (link
->flags
& DL_FLAG_SYNC_STATE_ONLY
&&
392 !(flags
& DL_FLAG_SYNC_STATE_ONLY
)) {
393 link
->flags
&= ~DL_FLAG_SYNC_STATE_ONLY
;
400 link
= kzalloc(sizeof(*link
), GFP_KERNEL
);
404 refcount_set(&link
->rpm_active
, 1);
406 if (flags
& DL_FLAG_PM_RUNTIME
) {
407 if (flags
& DL_FLAG_RPM_ACTIVE
)
408 refcount_inc(&link
->rpm_active
);
410 pm_runtime_new_link(consumer
);
413 get_device(supplier
);
414 link
->supplier
= supplier
;
415 INIT_LIST_HEAD(&link
->s_node
);
416 get_device(consumer
);
417 link
->consumer
= consumer
;
418 INIT_LIST_HEAD(&link
->c_node
);
420 kref_init(&link
->kref
);
422 /* Determine the initial link state. */
423 if (flags
& DL_FLAG_STATELESS
)
424 link
->status
= DL_STATE_NONE
;
426 device_link_init_status(link
, consumer
, supplier
);
429 * Some callers expect the link creation during consumer driver probe to
430 * resume the supplier even without DL_FLAG_RPM_ACTIVE.
432 if (link
->status
== DL_STATE_CONSUMER_PROBE
&&
433 flags
& DL_FLAG_PM_RUNTIME
)
434 pm_runtime_resume(supplier
);
436 if (flags
& DL_FLAG_SYNC_STATE_ONLY
) {
438 "Linked as a sync state only consumer to %s\n",
444 * Move the consumer and all of the devices depending on it to the end
445 * of dpm_list and the devices_kset list.
447 * It is necessary to hold dpm_list locked throughout all that or else
448 * we may end up suspending with a wrong ordering of it.
450 device_reorder_to_tail(consumer
, NULL
);
452 list_add_tail_rcu(&link
->s_node
, &supplier
->links
.consumers
);
453 list_add_tail_rcu(&link
->c_node
, &consumer
->links
.suppliers
);
455 dev_dbg(consumer
, "Linked as a consumer to %s\n", dev_name(supplier
));
459 device_links_write_unlock();
461 if ((flags
& DL_FLAG_PM_RUNTIME
&& flags
& DL_FLAG_RPM_ACTIVE
) && !link
)
462 pm_runtime_put(supplier
);
466 EXPORT_SYMBOL_GPL(device_link_add
);
469 * device_link_wait_for_supplier - Add device to wait_for_suppliers list
470 * @consumer: Consumer device
472 * Marks the @consumer device as waiting for suppliers to become available by
473 * adding it to the wait_for_suppliers list. The consumer device will never be
474 * probed until it's removed from the wait_for_suppliers list.
476 * The caller is responsible for adding the links to the supplier devices once
477 * they are available and removing the @consumer device from the
478 * wait_for_suppliers list once links to all the suppliers have been created.
480 * This function is NOT meant to be called from the probe function of the
481 * consumer but rather from code that creates/adds the consumer device.
483 static void device_link_wait_for_supplier(struct device
*consumer
,
486 mutex_lock(&wfs_lock
);
487 list_add_tail(&consumer
->links
.needs_suppliers
, &wait_for_suppliers
);
488 consumer
->links
.need_for_probe
= need_for_probe
;
489 mutex_unlock(&wfs_lock
);
492 static void device_link_wait_for_mandatory_supplier(struct device
*consumer
)
494 device_link_wait_for_supplier(consumer
, true);
497 static void device_link_wait_for_optional_supplier(struct device
*consumer
)
499 device_link_wait_for_supplier(consumer
, false);
503 * device_link_add_missing_supplier_links - Add links from consumer devices to
504 * supplier devices, leaving any
505 * consumer with inactive suppliers on
506 * the wait_for_suppliers list
508 * Loops through all consumers waiting on suppliers and tries to add all their
509 * supplier links. If that succeeds, the consumer device is removed from
510 * wait_for_suppliers list. Otherwise, they are left in the wait_for_suppliers
511 * list. Devices left on the wait_for_suppliers list will not be probed.
513 * The fwnode add_links callback is expected to return 0 if it has found and
514 * added all the supplier links for the consumer device. It should return an
515 * error if it isn't able to do so.
517 * The caller of device_link_wait_for_supplier() is expected to call this once
518 * it's aware of potential suppliers becoming available.
520 static void device_link_add_missing_supplier_links(void)
522 struct device
*dev
, *tmp
;
524 mutex_lock(&wfs_lock
);
525 list_for_each_entry_safe(dev
, tmp
, &wait_for_suppliers
,
526 links
.needs_suppliers
)
527 if (!fwnode_call_int_op(dev
->fwnode
, add_links
, dev
))
528 list_del_init(&dev
->links
.needs_suppliers
);
529 mutex_unlock(&wfs_lock
);
532 static void device_link_free(struct device_link
*link
)
534 while (refcount_dec_not_one(&link
->rpm_active
))
535 pm_runtime_put(link
->supplier
);
537 put_device(link
->consumer
);
538 put_device(link
->supplier
);
543 static void __device_link_free_srcu(struct rcu_head
*rhead
)
545 device_link_free(container_of(rhead
, struct device_link
, rcu_head
));
548 static void __device_link_del(struct kref
*kref
)
550 struct device_link
*link
= container_of(kref
, struct device_link
, kref
);
552 dev_dbg(link
->consumer
, "Dropping the link to %s\n",
553 dev_name(link
->supplier
));
555 if (link
->flags
& DL_FLAG_PM_RUNTIME
)
556 pm_runtime_drop_link(link
->consumer
);
558 list_del_rcu(&link
->s_node
);
559 list_del_rcu(&link
->c_node
);
560 call_srcu(&device_links_srcu
, &link
->rcu_head
, __device_link_free_srcu
);
562 #else /* !CONFIG_SRCU */
563 static void __device_link_del(struct kref
*kref
)
565 struct device_link
*link
= container_of(kref
, struct device_link
, kref
);
567 dev_info(link
->consumer
, "Dropping the link to %s\n",
568 dev_name(link
->supplier
));
570 if (link
->flags
& DL_FLAG_PM_RUNTIME
)
571 pm_runtime_drop_link(link
->consumer
);
573 list_del(&link
->s_node
);
574 list_del(&link
->c_node
);
575 device_link_free(link
);
577 #endif /* !CONFIG_SRCU */
579 static void device_link_put_kref(struct device_link
*link
)
581 if (link
->flags
& DL_FLAG_STATELESS
)
582 kref_put(&link
->kref
, __device_link_del
);
584 WARN(1, "Unable to drop a managed device link reference\n");
588 * device_link_del - Delete a stateless link between two devices.
589 * @link: Device link to delete.
591 * The caller must ensure proper synchronization of this function with runtime
592 * PM. If the link was added multiple times, it needs to be deleted as often.
593 * Care is required for hotplugged devices: Their links are purged on removal
594 * and calling device_link_del() is then no longer allowed.
596 void device_link_del(struct device_link
*link
)
598 device_links_write_lock();
600 device_link_put_kref(link
);
602 device_links_write_unlock();
604 EXPORT_SYMBOL_GPL(device_link_del
);
607 * device_link_remove - Delete a stateless link between two devices.
608 * @consumer: Consumer end of the link.
609 * @supplier: Supplier end of the link.
611 * The caller must ensure proper synchronization of this function with runtime
614 void device_link_remove(void *consumer
, struct device
*supplier
)
616 struct device_link
*link
;
618 if (WARN_ON(consumer
== supplier
))
621 device_links_write_lock();
624 list_for_each_entry(link
, &supplier
->links
.consumers
, s_node
) {
625 if (link
->consumer
== consumer
) {
626 device_link_put_kref(link
);
632 device_links_write_unlock();
634 EXPORT_SYMBOL_GPL(device_link_remove
);
636 static void device_links_missing_supplier(struct device
*dev
)
638 struct device_link
*link
;
640 list_for_each_entry(link
, &dev
->links
.suppliers
, c_node
)
641 if (link
->status
== DL_STATE_CONSUMER_PROBE
)
642 WRITE_ONCE(link
->status
, DL_STATE_AVAILABLE
);
646 * device_links_check_suppliers - Check presence of supplier drivers.
647 * @dev: Consumer device.
649 * Check links from this device to any suppliers. Walk the list of the device's
650 * links to suppliers and see if all of them are available. If not, simply
651 * return -EPROBE_DEFER.
653 * We need to guarantee that the supplier will not go away after the check has
654 * been positive here. It only can go away in __device_release_driver() and
655 * that function checks the device's links to consumers. This means we need to
656 * mark the link as "consumer probe in progress" to make the supplier removal
657 * wait for us to complete (or bad things may happen).
659 * Links without the DL_FLAG_MANAGED flag set are ignored.
661 int device_links_check_suppliers(struct device
*dev
)
663 struct device_link
*link
;
667 * Device waiting for supplier to become available is not allowed to
670 mutex_lock(&wfs_lock
);
671 if (!list_empty(&dev
->links
.needs_suppliers
) &&
672 dev
->links
.need_for_probe
) {
673 mutex_unlock(&wfs_lock
);
674 return -EPROBE_DEFER
;
676 mutex_unlock(&wfs_lock
);
678 device_links_write_lock();
680 list_for_each_entry(link
, &dev
->links
.suppliers
, c_node
) {
681 if (!(link
->flags
& DL_FLAG_MANAGED
) ||
682 link
->flags
& DL_FLAG_SYNC_STATE_ONLY
)
685 if (link
->status
!= DL_STATE_AVAILABLE
) {
686 device_links_missing_supplier(dev
);
690 WRITE_ONCE(link
->status
, DL_STATE_CONSUMER_PROBE
);
692 dev
->links
.status
= DL_DEV_PROBING
;
694 device_links_write_unlock();
699 * __device_links_queue_sync_state - Queue a device for sync_state() callback
700 * @dev: Device to call sync_state() on
701 * @list: List head to queue the @dev on
703 * Queues a device for a sync_state() callback when the device links write lock
704 * isn't held. This allows the sync_state() execution flow to use device links
705 * APIs. The caller must ensure this function is called with
706 * device_links_write_lock() held.
708 * This function does a get_device() to make sure the device is not freed while
711 * So the caller must also ensure that device_links_flush_sync_list() is called
712 * as soon as the caller releases device_links_write_lock(). This is necessary
713 * to make sure the sync_state() is called in a timely fashion and the
714 * put_device() is called on this device.
716 static void __device_links_queue_sync_state(struct device
*dev
,
717 struct list_head
*list
)
719 struct device_link
*link
;
721 if (dev
->state_synced
)
724 list_for_each_entry(link
, &dev
->links
.consumers
, s_node
) {
725 if (!(link
->flags
& DL_FLAG_MANAGED
))
727 if (link
->status
!= DL_STATE_ACTIVE
)
732 * Set the flag here to avoid adding the same device to a list more
733 * than once. This can happen if new consumers get added to the device
734 * and probed before the list is flushed.
736 dev
->state_synced
= true;
738 if (WARN_ON(!list_empty(&dev
->links
.defer_sync
)))
742 list_add_tail(&dev
->links
.defer_sync
, list
);
746 * device_links_flush_sync_list - Call sync_state() on a list of devices
747 * @list: List of devices to call sync_state() on
749 * Calls sync_state() on all the devices that have been queued for it. This
750 * function is used in conjunction with __device_links_queue_sync_state().
752 static void device_links_flush_sync_list(struct list_head
*list
)
754 struct device
*dev
, *tmp
;
756 list_for_each_entry_safe(dev
, tmp
, list
, links
.defer_sync
) {
757 list_del_init(&dev
->links
.defer_sync
);
761 if (dev
->bus
->sync_state
)
762 dev
->bus
->sync_state(dev
);
763 else if (dev
->driver
&& dev
->driver
->sync_state
)
764 dev
->driver
->sync_state(dev
);
772 void device_links_supplier_sync_state_pause(void)
774 device_links_write_lock();
775 defer_sync_state_count
++;
776 device_links_write_unlock();
779 void device_links_supplier_sync_state_resume(void)
781 struct device
*dev
, *tmp
;
782 LIST_HEAD(sync_list
);
784 device_links_write_lock();
785 if (!defer_sync_state_count
) {
786 WARN(true, "Unmatched sync_state pause/resume!");
789 defer_sync_state_count
--;
790 if (defer_sync_state_count
)
793 list_for_each_entry_safe(dev
, tmp
, &deferred_sync
, links
.defer_sync
) {
795 * Delete from deferred_sync list before queuing it to
796 * sync_list because defer_sync is used for both lists.
798 list_del_init(&dev
->links
.defer_sync
);
799 __device_links_queue_sync_state(dev
, &sync_list
);
802 device_links_write_unlock();
804 device_links_flush_sync_list(&sync_list
);
807 static int sync_state_resume_initcall(void)
809 device_links_supplier_sync_state_resume();
812 late_initcall(sync_state_resume_initcall
);
814 static void __device_links_supplier_defer_sync(struct device
*sup
)
816 if (list_empty(&sup
->links
.defer_sync
))
817 list_add_tail(&sup
->links
.defer_sync
, &deferred_sync
);
821 * device_links_driver_bound - Update device links after probing its driver.
822 * @dev: Device to update the links for.
824 * The probe has been successful, so update links from this device to any
825 * consumers by changing their status to "available".
827 * Also change the status of @dev's links to suppliers to "active".
829 * Links without the DL_FLAG_MANAGED flag set are ignored.
831 void device_links_driver_bound(struct device
*dev
)
833 struct device_link
*link
;
834 LIST_HEAD(sync_list
);
837 * If a device probes successfully, it's expected to have created all
838 * the device links it needs to or make new device links as it needs
839 * them. So, it no longer needs to wait on any suppliers.
841 mutex_lock(&wfs_lock
);
842 list_del_init(&dev
->links
.needs_suppliers
);
843 mutex_unlock(&wfs_lock
);
845 device_links_write_lock();
847 list_for_each_entry(link
, &dev
->links
.consumers
, s_node
) {
848 if (!(link
->flags
& DL_FLAG_MANAGED
))
852 * Links created during consumer probe may be in the "consumer
853 * probe" state to start with if the supplier is still probing
854 * when they are created and they may become "active" if the
855 * consumer probe returns first. Skip them here.
857 if (link
->status
== DL_STATE_CONSUMER_PROBE
||
858 link
->status
== DL_STATE_ACTIVE
)
861 WARN_ON(link
->status
!= DL_STATE_DORMANT
);
862 WRITE_ONCE(link
->status
, DL_STATE_AVAILABLE
);
864 if (link
->flags
& DL_FLAG_AUTOPROBE_CONSUMER
)
865 driver_deferred_probe_add(link
->consumer
);
868 list_for_each_entry(link
, &dev
->links
.suppliers
, c_node
) {
869 if (!(link
->flags
& DL_FLAG_MANAGED
))
872 WARN_ON(link
->status
!= DL_STATE_CONSUMER_PROBE
);
873 WRITE_ONCE(link
->status
, DL_STATE_ACTIVE
);
875 if (defer_sync_state_count
)
876 __device_links_supplier_defer_sync(link
->supplier
);
878 __device_links_queue_sync_state(link
->supplier
,
882 dev
->links
.status
= DL_DEV_DRIVER_BOUND
;
884 device_links_write_unlock();
886 device_links_flush_sync_list(&sync_list
);
889 static void device_link_drop_managed(struct device_link
*link
)
891 link
->flags
&= ~DL_FLAG_MANAGED
;
892 WRITE_ONCE(link
->status
, DL_STATE_NONE
);
893 kref_put(&link
->kref
, __device_link_del
);
897 * __device_links_no_driver - Update links of a device without a driver.
898 * @dev: Device without a drvier.
900 * Delete all non-persistent links from this device to any suppliers.
902 * Persistent links stay around, but their status is changed to "available",
903 * unless they already are in the "supplier unbind in progress" state in which
904 * case they need not be updated.
906 * Links without the DL_FLAG_MANAGED flag set are ignored.
908 static void __device_links_no_driver(struct device
*dev
)
910 struct device_link
*link
, *ln
;
912 list_for_each_entry_safe_reverse(link
, ln
, &dev
->links
.suppliers
, c_node
) {
913 if (!(link
->flags
& DL_FLAG_MANAGED
))
916 if (link
->flags
& DL_FLAG_AUTOREMOVE_CONSUMER
)
917 device_link_drop_managed(link
);
918 else if (link
->status
== DL_STATE_CONSUMER_PROBE
||
919 link
->status
== DL_STATE_ACTIVE
)
920 WRITE_ONCE(link
->status
, DL_STATE_AVAILABLE
);
923 dev
->links
.status
= DL_DEV_NO_DRIVER
;
927 * device_links_no_driver - Update links after failing driver probe.
928 * @dev: Device whose driver has just failed to probe.
930 * Clean up leftover links to consumers for @dev and invoke
931 * %__device_links_no_driver() to update links to suppliers for it as
934 * Links without the DL_FLAG_MANAGED flag set are ignored.
936 void device_links_no_driver(struct device
*dev
)
938 struct device_link
*link
;
940 device_links_write_lock();
942 list_for_each_entry(link
, &dev
->links
.consumers
, s_node
) {
943 if (!(link
->flags
& DL_FLAG_MANAGED
))
947 * The probe has failed, so if the status of the link is
948 * "consumer probe" or "active", it must have been added by
949 * a probing consumer while this device was still probing.
950 * Change its state to "dormant", as it represents a valid
951 * relationship, but it is not functionally meaningful.
953 if (link
->status
== DL_STATE_CONSUMER_PROBE
||
954 link
->status
== DL_STATE_ACTIVE
)
955 WRITE_ONCE(link
->status
, DL_STATE_DORMANT
);
958 __device_links_no_driver(dev
);
960 device_links_write_unlock();
964 * device_links_driver_cleanup - Update links after driver removal.
965 * @dev: Device whose driver has just gone away.
967 * Update links to consumers for @dev by changing their status to "dormant" and
968 * invoke %__device_links_no_driver() to update links to suppliers for it as
971 * Links without the DL_FLAG_MANAGED flag set are ignored.
973 void device_links_driver_cleanup(struct device
*dev
)
975 struct device_link
*link
, *ln
;
977 device_links_write_lock();
979 list_for_each_entry_safe(link
, ln
, &dev
->links
.consumers
, s_node
) {
980 if (!(link
->flags
& DL_FLAG_MANAGED
))
983 WARN_ON(link
->flags
& DL_FLAG_AUTOREMOVE_CONSUMER
);
984 WARN_ON(link
->status
!= DL_STATE_SUPPLIER_UNBIND
);
987 * autoremove the links between this @dev and its consumer
988 * devices that are not active, i.e. where the link state
989 * has moved to DL_STATE_SUPPLIER_UNBIND.
991 if (link
->status
== DL_STATE_SUPPLIER_UNBIND
&&
992 link
->flags
& DL_FLAG_AUTOREMOVE_SUPPLIER
)
993 device_link_drop_managed(link
);
995 WRITE_ONCE(link
->status
, DL_STATE_DORMANT
);
998 list_del_init(&dev
->links
.defer_sync
);
999 __device_links_no_driver(dev
);
1001 device_links_write_unlock();
1005 * device_links_busy - Check if there are any busy links to consumers.
1006 * @dev: Device to check.
1008 * Check each consumer of the device and return 'true' if its link's status
1009 * is one of "consumer probe" or "active" (meaning that the given consumer is
1010 * probing right now or its driver is present). Otherwise, change the link
1011 * state to "supplier unbind" to prevent the consumer from being probed
1012 * successfully going forward.
1014 * Return 'false' if there are no probing or active consumers.
1016 * Links without the DL_FLAG_MANAGED flag set are ignored.
1018 bool device_links_busy(struct device
*dev
)
1020 struct device_link
*link
;
1023 device_links_write_lock();
1025 list_for_each_entry(link
, &dev
->links
.consumers
, s_node
) {
1026 if (!(link
->flags
& DL_FLAG_MANAGED
))
1029 if (link
->status
== DL_STATE_CONSUMER_PROBE
1030 || link
->status
== DL_STATE_ACTIVE
) {
1034 WRITE_ONCE(link
->status
, DL_STATE_SUPPLIER_UNBIND
);
1037 dev
->links
.status
= DL_DEV_UNBINDING
;
1039 device_links_write_unlock();
1044 * device_links_unbind_consumers - Force unbind consumers of the given device.
1045 * @dev: Device to unbind the consumers of.
1047 * Walk the list of links to consumers for @dev and if any of them is in the
1048 * "consumer probe" state, wait for all device probes in progress to complete
1051 * If that's not the case, change the status of the link to "supplier unbind"
1052 * and check if the link was in the "active" state. If so, force the consumer
1053 * driver to unbind and start over (the consumer will not re-probe as we have
1054 * changed the state of the link already).
1056 * Links without the DL_FLAG_MANAGED flag set are ignored.
1058 void device_links_unbind_consumers(struct device
*dev
)
1060 struct device_link
*link
;
1063 device_links_write_lock();
1065 list_for_each_entry(link
, &dev
->links
.consumers
, s_node
) {
1066 enum device_link_state status
;
1068 if (!(link
->flags
& DL_FLAG_MANAGED
) ||
1069 link
->flags
& DL_FLAG_SYNC_STATE_ONLY
)
1072 status
= link
->status
;
1073 if (status
== DL_STATE_CONSUMER_PROBE
) {
1074 device_links_write_unlock();
1076 wait_for_device_probe();
1079 WRITE_ONCE(link
->status
, DL_STATE_SUPPLIER_UNBIND
);
1080 if (status
== DL_STATE_ACTIVE
) {
1081 struct device
*consumer
= link
->consumer
;
1083 get_device(consumer
);
1085 device_links_write_unlock();
1087 device_release_driver_internal(consumer
, NULL
,
1089 put_device(consumer
);
1094 device_links_write_unlock();
1098 * device_links_purge - Delete existing links to other devices.
1099 * @dev: Target device.
1101 static void device_links_purge(struct device
*dev
)
1103 struct device_link
*link
, *ln
;
1105 mutex_lock(&wfs_lock
);
1106 list_del(&dev
->links
.needs_suppliers
);
1107 mutex_unlock(&wfs_lock
);
1110 * Delete all of the remaining links from this device to any other
1111 * devices (either consumers or suppliers).
1113 device_links_write_lock();
1115 list_for_each_entry_safe_reverse(link
, ln
, &dev
->links
.suppliers
, c_node
) {
1116 WARN_ON(link
->status
== DL_STATE_ACTIVE
);
1117 __device_link_del(&link
->kref
);
1120 list_for_each_entry_safe_reverse(link
, ln
, &dev
->links
.consumers
, s_node
) {
1121 WARN_ON(link
->status
!= DL_STATE_DORMANT
&&
1122 link
->status
!= DL_STATE_NONE
);
1123 __device_link_del(&link
->kref
);
1126 device_links_write_unlock();
1129 /* Device links support end. */
1131 int (*platform_notify
)(struct device
*dev
) = NULL
;
1132 int (*platform_notify_remove
)(struct device
*dev
) = NULL
;
1133 static struct kobject
*dev_kobj
;
1134 struct kobject
*sysfs_dev_char_kobj
;
1135 struct kobject
*sysfs_dev_block_kobj
;
1137 static DEFINE_MUTEX(device_hotplug_lock
);
1139 void lock_device_hotplug(void)
1141 mutex_lock(&device_hotplug_lock
);
1144 void unlock_device_hotplug(void)
1146 mutex_unlock(&device_hotplug_lock
);
1149 int lock_device_hotplug_sysfs(void)
1151 if (mutex_trylock(&device_hotplug_lock
))
1154 /* Avoid busy looping (5 ms of sleep should do). */
1156 return restart_syscall();
1160 static inline int device_is_not_partition(struct device
*dev
)
1162 return !(dev
->type
== &part_type
);
1165 static inline int device_is_not_partition(struct device
*dev
)
1172 device_platform_notify(struct device
*dev
, enum kobject_action action
)
1176 ret
= acpi_platform_notify(dev
, action
);
1180 ret
= software_node_notify(dev
, action
);
1184 if (platform_notify
&& action
== KOBJ_ADD
)
1185 platform_notify(dev
);
1186 else if (platform_notify_remove
&& action
== KOBJ_REMOVE
)
1187 platform_notify_remove(dev
);
1192 * dev_driver_string - Return a device's driver name, if at all possible
1193 * @dev: struct device to get the name of
1195 * Will return the device's driver's name if it is bound to a device. If
1196 * the device is not bound to a driver, it will return the name of the bus
1197 * it is attached to. If it is not attached to a bus either, an empty
1198 * string will be returned.
1200 const char *dev_driver_string(const struct device
*dev
)
1202 struct device_driver
*drv
;
1204 /* dev->driver can change to NULL underneath us because of unbinding,
1205 * so be careful about accessing it. dev->bus and dev->class should
1206 * never change once they are set, so they don't need special care.
1208 drv
= READ_ONCE(dev
->driver
);
1209 return drv
? drv
->name
:
1210 (dev
->bus
? dev
->bus
->name
:
1211 (dev
->class ? dev
->class->name
: ""));
1213 EXPORT_SYMBOL(dev_driver_string
);
1215 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
1217 static ssize_t
dev_attr_show(struct kobject
*kobj
, struct attribute
*attr
,
1220 struct device_attribute
*dev_attr
= to_dev_attr(attr
);
1221 struct device
*dev
= kobj_to_dev(kobj
);
1225 ret
= dev_attr
->show(dev
, dev_attr
, buf
);
1226 if (ret
>= (ssize_t
)PAGE_SIZE
) {
1227 printk("dev_attr_show: %pS returned bad count\n",
1233 static ssize_t
dev_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
1234 const char *buf
, size_t count
)
1236 struct device_attribute
*dev_attr
= to_dev_attr(attr
);
1237 struct device
*dev
= kobj_to_dev(kobj
);
1240 if (dev_attr
->store
)
1241 ret
= dev_attr
->store(dev
, dev_attr
, buf
, count
);
1245 static const struct sysfs_ops dev_sysfs_ops
= {
1246 .show
= dev_attr_show
,
1247 .store
= dev_attr_store
,
1250 #define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
1252 ssize_t
device_store_ulong(struct device
*dev
,
1253 struct device_attribute
*attr
,
1254 const char *buf
, size_t size
)
1256 struct dev_ext_attribute
*ea
= to_ext_attr(attr
);
1260 ret
= kstrtoul(buf
, 0, &new);
1263 *(unsigned long *)(ea
->var
) = new;
1264 /* Always return full write size even if we didn't consume all */
1267 EXPORT_SYMBOL_GPL(device_store_ulong
);
1269 ssize_t
device_show_ulong(struct device
*dev
,
1270 struct device_attribute
*attr
,
1273 struct dev_ext_attribute
*ea
= to_ext_attr(attr
);
1274 return snprintf(buf
, PAGE_SIZE
, "%lx\n", *(unsigned long *)(ea
->var
));
1276 EXPORT_SYMBOL_GPL(device_show_ulong
);
1278 ssize_t
device_store_int(struct device
*dev
,
1279 struct device_attribute
*attr
,
1280 const char *buf
, size_t size
)
1282 struct dev_ext_attribute
*ea
= to_ext_attr(attr
);
1286 ret
= kstrtol(buf
, 0, &new);
1290 if (new > INT_MAX
|| new < INT_MIN
)
1292 *(int *)(ea
->var
) = new;
1293 /* Always return full write size even if we didn't consume all */
1296 EXPORT_SYMBOL_GPL(device_store_int
);
1298 ssize_t
device_show_int(struct device
*dev
,
1299 struct device_attribute
*attr
,
1302 struct dev_ext_attribute
*ea
= to_ext_attr(attr
);
1304 return snprintf(buf
, PAGE_SIZE
, "%d\n", *(int *)(ea
->var
));
1306 EXPORT_SYMBOL_GPL(device_show_int
);
1308 ssize_t
device_store_bool(struct device
*dev
, struct device_attribute
*attr
,
1309 const char *buf
, size_t size
)
1311 struct dev_ext_attribute
*ea
= to_ext_attr(attr
);
1313 if (strtobool(buf
, ea
->var
) < 0)
1318 EXPORT_SYMBOL_GPL(device_store_bool
);
1320 ssize_t
device_show_bool(struct device
*dev
, struct device_attribute
*attr
,
1323 struct dev_ext_attribute
*ea
= to_ext_attr(attr
);
1325 return snprintf(buf
, PAGE_SIZE
, "%d\n", *(bool *)(ea
->var
));
1327 EXPORT_SYMBOL_GPL(device_show_bool
);
1330 * device_release - free device structure.
1331 * @kobj: device's kobject.
1333 * This is called once the reference count for the object
1334 * reaches 0. We forward the call to the device's release
1335 * method, which should handle actually freeing the structure.
1337 static void device_release(struct kobject
*kobj
)
1339 struct device
*dev
= kobj_to_dev(kobj
);
1340 struct device_private
*p
= dev
->p
;
1343 * Some platform devices are driven without driver attached
1344 * and managed resources may have been acquired. Make sure
1345 * all resources are released.
1347 * Drivers still can add resources into device after device
1348 * is deleted but alive, so release devres here to avoid
1349 * possible memory leak.
1351 devres_release_all(dev
);
1355 else if (dev
->type
&& dev
->type
->release
)
1356 dev
->type
->release(dev
);
1357 else if (dev
->class && dev
->class->dev_release
)
1358 dev
->class->dev_release(dev
);
1360 WARN(1, KERN_ERR
"Device '%s' does not have a release() function, it is broken and must be fixed. See Documentation/kobject.txt.\n",
1365 static const void *device_namespace(struct kobject
*kobj
)
1367 struct device
*dev
= kobj_to_dev(kobj
);
1368 const void *ns
= NULL
;
1370 if (dev
->class && dev
->class->ns_type
)
1371 ns
= dev
->class->namespace(dev
);
1376 static void device_get_ownership(struct kobject
*kobj
, kuid_t
*uid
, kgid_t
*gid
)
1378 struct device
*dev
= kobj_to_dev(kobj
);
1380 if (dev
->class && dev
->class->get_ownership
)
1381 dev
->class->get_ownership(dev
, uid
, gid
);
1384 static struct kobj_type device_ktype
= {
1385 .release
= device_release
,
1386 .sysfs_ops
= &dev_sysfs_ops
,
1387 .namespace = device_namespace
,
1388 .get_ownership
= device_get_ownership
,
1392 static int dev_uevent_filter(struct kset
*kset
, struct kobject
*kobj
)
1394 struct kobj_type
*ktype
= get_ktype(kobj
);
1396 if (ktype
== &device_ktype
) {
1397 struct device
*dev
= kobj_to_dev(kobj
);
1406 static const char *dev_uevent_name(struct kset
*kset
, struct kobject
*kobj
)
1408 struct device
*dev
= kobj_to_dev(kobj
);
1411 return dev
->bus
->name
;
1413 return dev
->class->name
;
1417 static int dev_uevent(struct kset
*kset
, struct kobject
*kobj
,
1418 struct kobj_uevent_env
*env
)
1420 struct device
*dev
= kobj_to_dev(kobj
);
1423 /* add device node properties if present */
1424 if (MAJOR(dev
->devt
)) {
1428 kuid_t uid
= GLOBAL_ROOT_UID
;
1429 kgid_t gid
= GLOBAL_ROOT_GID
;
1431 add_uevent_var(env
, "MAJOR=%u", MAJOR(dev
->devt
));
1432 add_uevent_var(env
, "MINOR=%u", MINOR(dev
->devt
));
1433 name
= device_get_devnode(dev
, &mode
, &uid
, &gid
, &tmp
);
1435 add_uevent_var(env
, "DEVNAME=%s", name
);
1437 add_uevent_var(env
, "DEVMODE=%#o", mode
& 0777);
1438 if (!uid_eq(uid
, GLOBAL_ROOT_UID
))
1439 add_uevent_var(env
, "DEVUID=%u", from_kuid(&init_user_ns
, uid
));
1440 if (!gid_eq(gid
, GLOBAL_ROOT_GID
))
1441 add_uevent_var(env
, "DEVGID=%u", from_kgid(&init_user_ns
, gid
));
1446 if (dev
->type
&& dev
->type
->name
)
1447 add_uevent_var(env
, "DEVTYPE=%s", dev
->type
->name
);
1450 add_uevent_var(env
, "DRIVER=%s", dev
->driver
->name
);
1452 /* Add common DT information about the device */
1453 of_device_uevent(dev
, env
);
1455 /* have the bus specific function add its stuff */
1456 if (dev
->bus
&& dev
->bus
->uevent
) {
1457 retval
= dev
->bus
->uevent(dev
, env
);
1459 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
1460 dev_name(dev
), __func__
, retval
);
1463 /* have the class specific function add its stuff */
1464 if (dev
->class && dev
->class->dev_uevent
) {
1465 retval
= dev
->class->dev_uevent(dev
, env
);
1467 pr_debug("device: '%s': %s: class uevent() "
1468 "returned %d\n", dev_name(dev
),
1472 /* have the device type specific function add its stuff */
1473 if (dev
->type
&& dev
->type
->uevent
) {
1474 retval
= dev
->type
->uevent(dev
, env
);
1476 pr_debug("device: '%s': %s: dev_type uevent() "
1477 "returned %d\n", dev_name(dev
),
1484 static const struct kset_uevent_ops device_uevent_ops
= {
1485 .filter
= dev_uevent_filter
,
1486 .name
= dev_uevent_name
,
1487 .uevent
= dev_uevent
,
1490 static ssize_t
uevent_show(struct device
*dev
, struct device_attribute
*attr
,
1493 struct kobject
*top_kobj
;
1495 struct kobj_uevent_env
*env
= NULL
;
1500 /* search the kset, the device belongs to */
1501 top_kobj
= &dev
->kobj
;
1502 while (!top_kobj
->kset
&& top_kobj
->parent
)
1503 top_kobj
= top_kobj
->parent
;
1504 if (!top_kobj
->kset
)
1507 kset
= top_kobj
->kset
;
1508 if (!kset
->uevent_ops
|| !kset
->uevent_ops
->uevent
)
1511 /* respect filter */
1512 if (kset
->uevent_ops
&& kset
->uevent_ops
->filter
)
1513 if (!kset
->uevent_ops
->filter(kset
, &dev
->kobj
))
1516 env
= kzalloc(sizeof(struct kobj_uevent_env
), GFP_KERNEL
);
1520 /* let the kset specific function add its keys */
1521 retval
= kset
->uevent_ops
->uevent(kset
, &dev
->kobj
, env
);
1525 /* copy keys to file */
1526 for (i
= 0; i
< env
->envp_idx
; i
++)
1527 count
+= sprintf(&buf
[count
], "%s\n", env
->envp
[i
]);
1533 static ssize_t
uevent_store(struct device
*dev
, struct device_attribute
*attr
,
1534 const char *buf
, size_t count
)
1538 rc
= kobject_synth_uevent(&dev
->kobj
, buf
, count
);
1541 dev_err(dev
, "uevent: failed to send synthetic uevent\n");
1547 static DEVICE_ATTR_RW(uevent
);
1549 static ssize_t
online_show(struct device
*dev
, struct device_attribute
*attr
,
1555 val
= !dev
->offline
;
1557 return sprintf(buf
, "%u\n", val
);
1560 static ssize_t
online_store(struct device
*dev
, struct device_attribute
*attr
,
1561 const char *buf
, size_t count
)
1566 ret
= strtobool(buf
, &val
);
1570 ret
= lock_device_hotplug_sysfs();
1574 ret
= val
? device_online(dev
) : device_offline(dev
);
1575 unlock_device_hotplug();
1576 return ret
< 0 ? ret
: count
;
1578 static DEVICE_ATTR_RW(online
);
1580 int device_add_groups(struct device
*dev
, const struct attribute_group
**groups
)
1582 return sysfs_create_groups(&dev
->kobj
, groups
);
1584 EXPORT_SYMBOL_GPL(device_add_groups
);
1586 void device_remove_groups(struct device
*dev
,
1587 const struct attribute_group
**groups
)
1589 sysfs_remove_groups(&dev
->kobj
, groups
);
1591 EXPORT_SYMBOL_GPL(device_remove_groups
);
1593 union device_attr_group_devres
{
1594 const struct attribute_group
*group
;
1595 const struct attribute_group
**groups
;
1598 static int devm_attr_group_match(struct device
*dev
, void *res
, void *data
)
1600 return ((union device_attr_group_devres
*)res
)->group
== data
;
1603 static void devm_attr_group_remove(struct device
*dev
, void *res
)
1605 union device_attr_group_devres
*devres
= res
;
1606 const struct attribute_group
*group
= devres
->group
;
1608 dev_dbg(dev
, "%s: removing group %p\n", __func__
, group
);
1609 sysfs_remove_group(&dev
->kobj
, group
);
1612 static void devm_attr_groups_remove(struct device
*dev
, void *res
)
1614 union device_attr_group_devres
*devres
= res
;
1615 const struct attribute_group
**groups
= devres
->groups
;
1617 dev_dbg(dev
, "%s: removing groups %p\n", __func__
, groups
);
1618 sysfs_remove_groups(&dev
->kobj
, groups
);
1622 * devm_device_add_group - given a device, create a managed attribute group
1623 * @dev: The device to create the group for
1624 * @grp: The attribute group to create
1626 * This function creates a group for the first time. It will explicitly
1627 * warn and error if any of the attribute files being created already exist.
1629 * Returns 0 on success or error code on failure.
1631 int devm_device_add_group(struct device
*dev
, const struct attribute_group
*grp
)
1633 union device_attr_group_devres
*devres
;
1636 devres
= devres_alloc(devm_attr_group_remove
,
1637 sizeof(*devres
), GFP_KERNEL
);
1641 error
= sysfs_create_group(&dev
->kobj
, grp
);
1643 devres_free(devres
);
1647 devres
->group
= grp
;
1648 devres_add(dev
, devres
);
1651 EXPORT_SYMBOL_GPL(devm_device_add_group
);
1654 * devm_device_remove_group: remove a managed group from a device
1655 * @dev: device to remove the group from
1656 * @grp: group to remove
1658 * This function removes a group of attributes from a device. The attributes
1659 * previously have to have been created for this group, otherwise it will fail.
1661 void devm_device_remove_group(struct device
*dev
,
1662 const struct attribute_group
*grp
)
1664 WARN_ON(devres_release(dev
, devm_attr_group_remove
,
1665 devm_attr_group_match
,
1666 /* cast away const */ (void *)grp
));
1668 EXPORT_SYMBOL_GPL(devm_device_remove_group
);
1671 * devm_device_add_groups - create a bunch of managed attribute groups
1672 * @dev: The device to create the group for
1673 * @groups: The attribute groups to create, NULL terminated
1675 * This function creates a bunch of managed attribute groups. If an error
1676 * occurs when creating a group, all previously created groups will be
1677 * removed, unwinding everything back to the original state when this
1678 * function was called. It will explicitly warn and error if any of the
1679 * attribute files being created already exist.
1681 * Returns 0 on success or error code from sysfs_create_group on failure.
1683 int devm_device_add_groups(struct device
*dev
,
1684 const struct attribute_group
**groups
)
1686 union device_attr_group_devres
*devres
;
1689 devres
= devres_alloc(devm_attr_groups_remove
,
1690 sizeof(*devres
), GFP_KERNEL
);
1694 error
= sysfs_create_groups(&dev
->kobj
, groups
);
1696 devres_free(devres
);
1700 devres
->groups
= groups
;
1701 devres_add(dev
, devres
);
1704 EXPORT_SYMBOL_GPL(devm_device_add_groups
);
1707 * devm_device_remove_groups - remove a list of managed groups
1709 * @dev: The device for the groups to be removed from
1710 * @groups: NULL terminated list of groups to be removed
1712 * If groups is not NULL, remove the specified groups from the device.
1714 void devm_device_remove_groups(struct device
*dev
,
1715 const struct attribute_group
**groups
)
1717 WARN_ON(devres_release(dev
, devm_attr_groups_remove
,
1718 devm_attr_group_match
,
1719 /* cast away const */ (void *)groups
));
1721 EXPORT_SYMBOL_GPL(devm_device_remove_groups
);
1723 static int device_add_attrs(struct device
*dev
)
1725 struct class *class = dev
->class;
1726 const struct device_type
*type
= dev
->type
;
1730 error
= device_add_groups(dev
, class->dev_groups
);
1736 error
= device_add_groups(dev
, type
->groups
);
1738 goto err_remove_class_groups
;
1741 error
= device_add_groups(dev
, dev
->groups
);
1743 goto err_remove_type_groups
;
1745 if (device_supports_offline(dev
) && !dev
->offline_disabled
) {
1746 error
= device_create_file(dev
, &dev_attr_online
);
1748 goto err_remove_dev_groups
;
1753 err_remove_dev_groups
:
1754 device_remove_groups(dev
, dev
->groups
);
1755 err_remove_type_groups
:
1757 device_remove_groups(dev
, type
->groups
);
1758 err_remove_class_groups
:
1760 device_remove_groups(dev
, class->dev_groups
);
1765 static void device_remove_attrs(struct device
*dev
)
1767 struct class *class = dev
->class;
1768 const struct device_type
*type
= dev
->type
;
1770 device_remove_file(dev
, &dev_attr_online
);
1771 device_remove_groups(dev
, dev
->groups
);
1774 device_remove_groups(dev
, type
->groups
);
1777 device_remove_groups(dev
, class->dev_groups
);
1780 static ssize_t
dev_show(struct device
*dev
, struct device_attribute
*attr
,
1783 return print_dev_t(buf
, dev
->devt
);
1785 static DEVICE_ATTR_RO(dev
);
1788 struct kset
*devices_kset
;
1791 * devices_kset_move_before - Move device in the devices_kset's list.
1792 * @deva: Device to move.
1793 * @devb: Device @deva should come before.
1795 static void devices_kset_move_before(struct device
*deva
, struct device
*devb
)
1799 pr_debug("devices_kset: Moving %s before %s\n",
1800 dev_name(deva
), dev_name(devb
));
1801 spin_lock(&devices_kset
->list_lock
);
1802 list_move_tail(&deva
->kobj
.entry
, &devb
->kobj
.entry
);
1803 spin_unlock(&devices_kset
->list_lock
);
1807 * devices_kset_move_after - Move device in the devices_kset's list.
1808 * @deva: Device to move
1809 * @devb: Device @deva should come after.
1811 static void devices_kset_move_after(struct device
*deva
, struct device
*devb
)
1815 pr_debug("devices_kset: Moving %s after %s\n",
1816 dev_name(deva
), dev_name(devb
));
1817 spin_lock(&devices_kset
->list_lock
);
1818 list_move(&deva
->kobj
.entry
, &devb
->kobj
.entry
);
1819 spin_unlock(&devices_kset
->list_lock
);
1823 * devices_kset_move_last - move the device to the end of devices_kset's list.
1824 * @dev: device to move
1826 void devices_kset_move_last(struct device
*dev
)
1830 pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev
));
1831 spin_lock(&devices_kset
->list_lock
);
1832 list_move_tail(&dev
->kobj
.entry
, &devices_kset
->list
);
1833 spin_unlock(&devices_kset
->list_lock
);
1837 * device_create_file - create sysfs attribute file for device.
1839 * @attr: device attribute descriptor.
1841 int device_create_file(struct device
*dev
,
1842 const struct device_attribute
*attr
)
1847 WARN(((attr
->attr
.mode
& S_IWUGO
) && !attr
->store
),
1848 "Attribute %s: write permission without 'store'\n",
1850 WARN(((attr
->attr
.mode
& S_IRUGO
) && !attr
->show
),
1851 "Attribute %s: read permission without 'show'\n",
1853 error
= sysfs_create_file(&dev
->kobj
, &attr
->attr
);
1858 EXPORT_SYMBOL_GPL(device_create_file
);
1861 * device_remove_file - remove sysfs attribute file.
1863 * @attr: device attribute descriptor.
1865 void device_remove_file(struct device
*dev
,
1866 const struct device_attribute
*attr
)
1869 sysfs_remove_file(&dev
->kobj
, &attr
->attr
);
1871 EXPORT_SYMBOL_GPL(device_remove_file
);
1874 * device_remove_file_self - remove sysfs attribute file from its own method.
1876 * @attr: device attribute descriptor.
1878 * See kernfs_remove_self() for details.
1880 bool device_remove_file_self(struct device
*dev
,
1881 const struct device_attribute
*attr
)
1884 return sysfs_remove_file_self(&dev
->kobj
, &attr
->attr
);
1888 EXPORT_SYMBOL_GPL(device_remove_file_self
);
1891 * device_create_bin_file - create sysfs binary attribute file for device.
1893 * @attr: device binary attribute descriptor.
1895 int device_create_bin_file(struct device
*dev
,
1896 const struct bin_attribute
*attr
)
1898 int error
= -EINVAL
;
1900 error
= sysfs_create_bin_file(&dev
->kobj
, attr
);
1903 EXPORT_SYMBOL_GPL(device_create_bin_file
);
1906 * device_remove_bin_file - remove sysfs binary attribute file
1908 * @attr: device binary attribute descriptor.
1910 void device_remove_bin_file(struct device
*dev
,
1911 const struct bin_attribute
*attr
)
1914 sysfs_remove_bin_file(&dev
->kobj
, attr
);
1916 EXPORT_SYMBOL_GPL(device_remove_bin_file
);
1918 static void klist_children_get(struct klist_node
*n
)
1920 struct device_private
*p
= to_device_private_parent(n
);
1921 struct device
*dev
= p
->device
;
1926 static void klist_children_put(struct klist_node
*n
)
1928 struct device_private
*p
= to_device_private_parent(n
);
1929 struct device
*dev
= p
->device
;
1935 * device_initialize - init device structure.
1938 * This prepares the device for use by other layers by initializing
1940 * It is the first half of device_register(), if called by
1941 * that function, though it can also be called separately, so one
1942 * may use @dev's fields. In particular, get_device()/put_device()
1943 * may be used for reference counting of @dev after calling this
1946 * All fields in @dev must be initialized by the caller to 0, except
1947 * for those explicitly set to some other value. The simplest
1948 * approach is to use kzalloc() to allocate the structure containing
1951 * NOTE: Use put_device() to give up your reference instead of freeing
1952 * @dev directly once you have called this function.
1954 void device_initialize(struct device
*dev
)
1956 dev
->kobj
.kset
= devices_kset
;
1957 kobject_init(&dev
->kobj
, &device_ktype
);
1958 INIT_LIST_HEAD(&dev
->dma_pools
);
1959 mutex_init(&dev
->mutex
);
1960 #ifdef CONFIG_PROVE_LOCKING
1961 mutex_init(&dev
->lockdep_mutex
);
1963 lockdep_set_novalidate_class(&dev
->mutex
);
1964 spin_lock_init(&dev
->devres_lock
);
1965 INIT_LIST_HEAD(&dev
->devres_head
);
1966 device_pm_init(dev
);
1967 set_dev_node(dev
, -1);
1968 #ifdef CONFIG_GENERIC_MSI_IRQ
1969 INIT_LIST_HEAD(&dev
->msi_list
);
1971 INIT_LIST_HEAD(&dev
->links
.consumers
);
1972 INIT_LIST_HEAD(&dev
->links
.suppliers
);
1973 INIT_LIST_HEAD(&dev
->links
.needs_suppliers
);
1974 INIT_LIST_HEAD(&dev
->links
.defer_sync
);
1975 dev
->links
.status
= DL_DEV_NO_DRIVER
;
1977 EXPORT_SYMBOL_GPL(device_initialize
);
1979 struct kobject
*virtual_device_parent(struct device
*dev
)
1981 static struct kobject
*virtual_dir
= NULL
;
1984 virtual_dir
= kobject_create_and_add("virtual",
1985 &devices_kset
->kobj
);
1991 struct kobject kobj
;
1992 struct class *class;
1995 #define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
1997 static void class_dir_release(struct kobject
*kobj
)
1999 struct class_dir
*dir
= to_class_dir(kobj
);
2004 struct kobj_ns_type_operations
*class_dir_child_ns_type(struct kobject
*kobj
)
2006 struct class_dir
*dir
= to_class_dir(kobj
);
2007 return dir
->class->ns_type
;
2010 static struct kobj_type class_dir_ktype
= {
2011 .release
= class_dir_release
,
2012 .sysfs_ops
= &kobj_sysfs_ops
,
2013 .child_ns_type
= class_dir_child_ns_type
2016 static struct kobject
*
2017 class_dir_create_and_add(struct class *class, struct kobject
*parent_kobj
)
2019 struct class_dir
*dir
;
2022 dir
= kzalloc(sizeof(*dir
), GFP_KERNEL
);
2024 return ERR_PTR(-ENOMEM
);
2027 kobject_init(&dir
->kobj
, &class_dir_ktype
);
2029 dir
->kobj
.kset
= &class->p
->glue_dirs
;
2031 retval
= kobject_add(&dir
->kobj
, parent_kobj
, "%s", class->name
);
2033 kobject_put(&dir
->kobj
);
2034 return ERR_PTR(retval
);
2039 static DEFINE_MUTEX(gdp_mutex
);
2041 static struct kobject
*get_device_parent(struct device
*dev
,
2042 struct device
*parent
)
2045 struct kobject
*kobj
= NULL
;
2046 struct kobject
*parent_kobj
;
2050 /* block disks show up in /sys/block */
2051 if (sysfs_deprecated
&& dev
->class == &block_class
) {
2052 if (parent
&& parent
->class == &block_class
)
2053 return &parent
->kobj
;
2054 return &block_class
.p
->subsys
.kobj
;
2059 * If we have no parent, we live in "virtual".
2060 * Class-devices with a non class-device as parent, live
2061 * in a "glue" directory to prevent namespace collisions.
2064 parent_kobj
= virtual_device_parent(dev
);
2065 else if (parent
->class && !dev
->class->ns_type
)
2066 return &parent
->kobj
;
2068 parent_kobj
= &parent
->kobj
;
2070 mutex_lock(&gdp_mutex
);
2072 /* find our class-directory at the parent and reference it */
2073 spin_lock(&dev
->class->p
->glue_dirs
.list_lock
);
2074 list_for_each_entry(k
, &dev
->class->p
->glue_dirs
.list
, entry
)
2075 if (k
->parent
== parent_kobj
) {
2076 kobj
= kobject_get(k
);
2079 spin_unlock(&dev
->class->p
->glue_dirs
.list_lock
);
2081 mutex_unlock(&gdp_mutex
);
2085 /* or create a new class-directory at the parent device */
2086 k
= class_dir_create_and_add(dev
->class, parent_kobj
);
2087 /* do not emit an uevent for this simple "glue" directory */
2088 mutex_unlock(&gdp_mutex
);
2092 /* subsystems can specify a default root directory for their devices */
2093 if (!parent
&& dev
->bus
&& dev
->bus
->dev_root
)
2094 return &dev
->bus
->dev_root
->kobj
;
2097 return &parent
->kobj
;
2101 static inline bool live_in_glue_dir(struct kobject
*kobj
,
2104 if (!kobj
|| !dev
->class ||
2105 kobj
->kset
!= &dev
->class->p
->glue_dirs
)
2110 static inline struct kobject
*get_glue_dir(struct device
*dev
)
2112 return dev
->kobj
.parent
;
2116 * make sure cleaning up dir as the last step, we need to make
2117 * sure .release handler of kobject is run with holding the
2120 static void cleanup_glue_dir(struct device
*dev
, struct kobject
*glue_dir
)
2124 /* see if we live in a "glue" directory */
2125 if (!live_in_glue_dir(glue_dir
, dev
))
2128 mutex_lock(&gdp_mutex
);
2130 * There is a race condition between removing glue directory
2131 * and adding a new device under the glue directory.
2136 * get_device_parent()
2137 * class_dir_create_and_add()
2138 * kobject_add_internal()
2139 * create_dir() // create glue_dir
2142 * get_device_parent()
2143 * kobject_get() // get glue_dir
2146 * cleanup_glue_dir()
2147 * kobject_del(glue_dir)
2150 * kobject_add_internal()
2151 * create_dir() // in glue_dir
2152 * sysfs_create_dir_ns()
2153 * kernfs_create_dir_ns(sd)
2155 * sysfs_remove_dir() // glue_dir->sd=NULL
2156 * sysfs_put() // free glue_dir->sd
2159 * kernfs_new_node(sd)
2160 * kernfs_get(glue_dir)
2164 * Before CPU1 remove last child device under glue dir, if CPU2 add
2165 * a new device under glue dir, the glue_dir kobject reference count
2166 * will be increase to 2 in kobject_get(k). And CPU2 has been called
2167 * kernfs_create_dir_ns(). Meanwhile, CPU1 call sysfs_remove_dir()
2168 * and sysfs_put(). This result in glue_dir->sd is freed.
2170 * Then the CPU2 will see a stale "empty" but still potentially used
2171 * glue dir around in kernfs_new_node().
2173 * In order to avoid this happening, we also should make sure that
2174 * kernfs_node for glue_dir is released in CPU1 only when refcount
2175 * for glue_dir kobj is 1.
2177 ref
= kref_read(&glue_dir
->kref
);
2178 if (!kobject_has_children(glue_dir
) && !--ref
)
2179 kobject_del(glue_dir
);
2180 kobject_put(glue_dir
);
2181 mutex_unlock(&gdp_mutex
);
2184 static int device_add_class_symlinks(struct device
*dev
)
2186 struct device_node
*of_node
= dev_of_node(dev
);
2190 error
= sysfs_create_link(&dev
->kobj
, of_node_kobj(of_node
), "of_node");
2192 dev_warn(dev
, "Error %d creating of_node link\n",error
);
2193 /* An error here doesn't warrant bringing down the device */
2199 error
= sysfs_create_link(&dev
->kobj
,
2200 &dev
->class->p
->subsys
.kobj
,
2205 if (dev
->parent
&& device_is_not_partition(dev
)) {
2206 error
= sysfs_create_link(&dev
->kobj
, &dev
->parent
->kobj
,
2213 /* /sys/block has directories and does not need symlinks */
2214 if (sysfs_deprecated
&& dev
->class == &block_class
)
2218 /* link in the class directory pointing to the device */
2219 error
= sysfs_create_link(&dev
->class->p
->subsys
.kobj
,
2220 &dev
->kobj
, dev_name(dev
));
2227 sysfs_remove_link(&dev
->kobj
, "device");
2230 sysfs_remove_link(&dev
->kobj
, "subsystem");
2232 sysfs_remove_link(&dev
->kobj
, "of_node");
2236 static void device_remove_class_symlinks(struct device
*dev
)
2238 if (dev_of_node(dev
))
2239 sysfs_remove_link(&dev
->kobj
, "of_node");
2244 if (dev
->parent
&& device_is_not_partition(dev
))
2245 sysfs_remove_link(&dev
->kobj
, "device");
2246 sysfs_remove_link(&dev
->kobj
, "subsystem");
2248 if (sysfs_deprecated
&& dev
->class == &block_class
)
2251 sysfs_delete_link(&dev
->class->p
->subsys
.kobj
, &dev
->kobj
, dev_name(dev
));
2255 * dev_set_name - set a device name
2257 * @fmt: format string for the device's name
2259 int dev_set_name(struct device
*dev
, const char *fmt
, ...)
2264 va_start(vargs
, fmt
);
2265 err
= kobject_set_name_vargs(&dev
->kobj
, fmt
, vargs
);
2269 EXPORT_SYMBOL_GPL(dev_set_name
);
2272 * device_to_dev_kobj - select a /sys/dev/ directory for the device
2275 * By default we select char/ for new entries. Setting class->dev_obj
2276 * to NULL prevents an entry from being created. class->dev_kobj must
2277 * be set (or cleared) before any devices are registered to the class
2278 * otherwise device_create_sys_dev_entry() and
2279 * device_remove_sys_dev_entry() will disagree about the presence of
2282 static struct kobject
*device_to_dev_kobj(struct device
*dev
)
2284 struct kobject
*kobj
;
2287 kobj
= dev
->class->dev_kobj
;
2289 kobj
= sysfs_dev_char_kobj
;
2294 static int device_create_sys_dev_entry(struct device
*dev
)
2296 struct kobject
*kobj
= device_to_dev_kobj(dev
);
2301 format_dev_t(devt_str
, dev
->devt
);
2302 error
= sysfs_create_link(kobj
, &dev
->kobj
, devt_str
);
2308 static void device_remove_sys_dev_entry(struct device
*dev
)
2310 struct kobject
*kobj
= device_to_dev_kobj(dev
);
2314 format_dev_t(devt_str
, dev
->devt
);
2315 sysfs_remove_link(kobj
, devt_str
);
2319 static int device_private_init(struct device
*dev
)
2321 dev
->p
= kzalloc(sizeof(*dev
->p
), GFP_KERNEL
);
2324 dev
->p
->device
= dev
;
2325 klist_init(&dev
->p
->klist_children
, klist_children_get
,
2326 klist_children_put
);
2327 INIT_LIST_HEAD(&dev
->p
->deferred_probe
);
2332 * device_add - add device to device hierarchy.
2335 * This is part 2 of device_register(), though may be called
2336 * separately _iff_ device_initialize() has been called separately.
2338 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
2339 * to the global and sibling lists for the device, then
2340 * adds it to the other relevant subsystems of the driver model.
2342 * Do not call this routine or device_register() more than once for
2343 * any device structure. The driver model core is not designed to work
2344 * with devices that get unregistered and then spring back to life.
2345 * (Among other things, it's very hard to guarantee that all references
2346 * to the previous incarnation of @dev have been dropped.) Allocate
2347 * and register a fresh new struct device instead.
2349 * NOTE: _Never_ directly free @dev after calling this function, even
2350 * if it returned an error! Always use put_device() to give up your
2351 * reference instead.
2353 * Rule of thumb is: if device_add() succeeds, you should call
2354 * device_del() when you want to get rid of it. If device_add() has
2355 * *not* succeeded, use *only* put_device() to drop the reference
2358 int device_add(struct device
*dev
)
2360 struct device
*parent
;
2361 struct kobject
*kobj
;
2362 struct class_interface
*class_intf
;
2363 int error
= -EINVAL
, fw_ret
;
2364 struct kobject
*glue_dir
= NULL
;
2366 dev
= get_device(dev
);
2371 error
= device_private_init(dev
);
2377 * for statically allocated devices, which should all be converted
2378 * some day, we need to initialize the name. We prevent reading back
2379 * the name, and force the use of dev_name()
2381 if (dev
->init_name
) {
2382 dev_set_name(dev
, "%s", dev
->init_name
);
2383 dev
->init_name
= NULL
;
2386 /* subsystems can specify simple device enumeration */
2387 if (!dev_name(dev
) && dev
->bus
&& dev
->bus
->dev_name
)
2388 dev_set_name(dev
, "%s%u", dev
->bus
->dev_name
, dev
->id
);
2390 if (!dev_name(dev
)) {
2395 pr_debug("device: '%s': %s\n", dev_name(dev
), __func__
);
2397 parent
= get_device(dev
->parent
);
2398 kobj
= get_device_parent(dev
, parent
);
2400 error
= PTR_ERR(kobj
);
2404 dev
->kobj
.parent
= kobj
;
2406 /* use parent numa_node */
2407 if (parent
&& (dev_to_node(dev
) == NUMA_NO_NODE
))
2408 set_dev_node(dev
, dev_to_node(parent
));
2410 /* first, register with generic layer. */
2411 /* we require the name to be set before, and pass NULL */
2412 error
= kobject_add(&dev
->kobj
, dev
->kobj
.parent
, NULL
);
2414 glue_dir
= get_glue_dir(dev
);
2418 /* notify platform of device entry */
2419 error
= device_platform_notify(dev
, KOBJ_ADD
);
2421 goto platform_error
;
2423 error
= device_create_file(dev
, &dev_attr_uevent
);
2427 error
= device_add_class_symlinks(dev
);
2430 error
= device_add_attrs(dev
);
2433 error
= bus_add_device(dev
);
2436 error
= dpm_sysfs_add(dev
);
2441 if (MAJOR(dev
->devt
)) {
2442 error
= device_create_file(dev
, &dev_attr_dev
);
2446 error
= device_create_sys_dev_entry(dev
);
2450 devtmpfs_create_node(dev
);
2453 /* Notify clients of device addition. This call must come
2454 * after dpm_sysfs_add() and before kobject_uevent().
2457 blocking_notifier_call_chain(&dev
->bus
->p
->bus_notifier
,
2458 BUS_NOTIFY_ADD_DEVICE
, dev
);
2460 kobject_uevent(&dev
->kobj
, KOBJ_ADD
);
2462 if (dev
->fwnode
&& !dev
->fwnode
->dev
)
2463 dev
->fwnode
->dev
= dev
;
2466 * Check if any of the other devices (consumers) have been waiting for
2467 * this device (supplier) to be added so that they can create a device
2470 * This needs to happen after device_pm_add() because device_link_add()
2471 * requires the supplier be registered before it's called.
2473 * But this also needs to happe before bus_probe_device() to make sure
2474 * waiting consumers can link to it before the driver is bound to the
2475 * device and the driver sync_state callback is called for this device.
2477 device_link_add_missing_supplier_links();
2479 if (fwnode_has_op(dev
->fwnode
, add_links
)) {
2480 fw_ret
= fwnode_call_int_op(dev
->fwnode
, add_links
, dev
);
2481 if (fw_ret
== -ENODEV
)
2482 device_link_wait_for_mandatory_supplier(dev
);
2484 device_link_wait_for_optional_supplier(dev
);
2487 bus_probe_device(dev
);
2489 klist_add_tail(&dev
->p
->knode_parent
,
2490 &parent
->p
->klist_children
);
2493 mutex_lock(&dev
->class->p
->mutex
);
2494 /* tie the class to the device */
2495 klist_add_tail(&dev
->p
->knode_class
,
2496 &dev
->class->p
->klist_devices
);
2498 /* notify any interfaces that the device is here */
2499 list_for_each_entry(class_intf
,
2500 &dev
->class->p
->interfaces
, node
)
2501 if (class_intf
->add_dev
)
2502 class_intf
->add_dev(dev
, class_intf
);
2503 mutex_unlock(&dev
->class->p
->mutex
);
2509 if (MAJOR(dev
->devt
))
2510 device_remove_file(dev
, &dev_attr_dev
);
2512 device_pm_remove(dev
);
2513 dpm_sysfs_remove(dev
);
2515 bus_remove_device(dev
);
2517 device_remove_attrs(dev
);
2519 device_remove_class_symlinks(dev
);
2521 device_remove_file(dev
, &dev_attr_uevent
);
2523 device_platform_notify(dev
, KOBJ_REMOVE
);
2525 kobject_uevent(&dev
->kobj
, KOBJ_REMOVE
);
2526 glue_dir
= get_glue_dir(dev
);
2527 kobject_del(&dev
->kobj
);
2529 cleanup_glue_dir(dev
, glue_dir
);
2537 EXPORT_SYMBOL_GPL(device_add
);
2540 * device_register - register a device with the system.
2541 * @dev: pointer to the device structure
2543 * This happens in two clean steps - initialize the device
2544 * and add it to the system. The two steps can be called
2545 * separately, but this is the easiest and most common.
2546 * I.e. you should only call the two helpers separately if
2547 * have a clearly defined need to use and refcount the device
2548 * before it is added to the hierarchy.
2550 * For more information, see the kerneldoc for device_initialize()
2553 * NOTE: _Never_ directly free @dev after calling this function, even
2554 * if it returned an error! Always use put_device() to give up the
2555 * reference initialized in this function instead.
2557 int device_register(struct device
*dev
)
2559 device_initialize(dev
);
2560 return device_add(dev
);
2562 EXPORT_SYMBOL_GPL(device_register
);
2565 * get_device - increment reference count for device.
2568 * This simply forwards the call to kobject_get(), though
2569 * we do take care to provide for the case that we get a NULL
2570 * pointer passed in.
2572 struct device
*get_device(struct device
*dev
)
2574 return dev
? kobj_to_dev(kobject_get(&dev
->kobj
)) : NULL
;
2576 EXPORT_SYMBOL_GPL(get_device
);
2579 * put_device - decrement reference count.
2580 * @dev: device in question.
2582 void put_device(struct device
*dev
)
2584 /* might_sleep(); */
2586 kobject_put(&dev
->kobj
);
2588 EXPORT_SYMBOL_GPL(put_device
);
2590 bool kill_device(struct device
*dev
)
2593 * Require the device lock and set the "dead" flag to guarantee that
2594 * the update behavior is consistent with the other bitfields near
2595 * it and that we cannot have an asynchronous probe routine trying
2596 * to run while we are tearing out the bus/class/sysfs from
2597 * underneath the device.
2599 lockdep_assert_held(&dev
->mutex
);
2603 dev
->p
->dead
= true;
2606 EXPORT_SYMBOL_GPL(kill_device
);
2609 * device_del - delete device from system.
2612 * This is the first part of the device unregistration
2613 * sequence. This removes the device from the lists we control
2614 * from here, has it removed from the other driver model
2615 * subsystems it was added to in device_add(), and removes it
2616 * from the kobject hierarchy.
2618 * NOTE: this should be called manually _iff_ device_add() was
2619 * also called manually.
2621 void device_del(struct device
*dev
)
2623 struct device
*parent
= dev
->parent
;
2624 struct kobject
*glue_dir
= NULL
;
2625 struct class_interface
*class_intf
;
2631 if (dev
->fwnode
&& dev
->fwnode
->dev
== dev
)
2632 dev
->fwnode
->dev
= NULL
;
2634 /* Notify clients of device removal. This call must come
2635 * before dpm_sysfs_remove().
2638 blocking_notifier_call_chain(&dev
->bus
->p
->bus_notifier
,
2639 BUS_NOTIFY_DEL_DEVICE
, dev
);
2641 dpm_sysfs_remove(dev
);
2643 klist_del(&dev
->p
->knode_parent
);
2644 if (MAJOR(dev
->devt
)) {
2645 devtmpfs_delete_node(dev
);
2646 device_remove_sys_dev_entry(dev
);
2647 device_remove_file(dev
, &dev_attr_dev
);
2650 device_remove_class_symlinks(dev
);
2652 mutex_lock(&dev
->class->p
->mutex
);
2653 /* notify any interfaces that the device is now gone */
2654 list_for_each_entry(class_intf
,
2655 &dev
->class->p
->interfaces
, node
)
2656 if (class_intf
->remove_dev
)
2657 class_intf
->remove_dev(dev
, class_intf
);
2658 /* remove the device from the class list */
2659 klist_del(&dev
->p
->knode_class
);
2660 mutex_unlock(&dev
->class->p
->mutex
);
2662 device_remove_file(dev
, &dev_attr_uevent
);
2663 device_remove_attrs(dev
);
2664 bus_remove_device(dev
);
2665 device_pm_remove(dev
);
2666 driver_deferred_probe_del(dev
);
2667 device_platform_notify(dev
, KOBJ_REMOVE
);
2668 device_remove_properties(dev
);
2669 device_links_purge(dev
);
2672 blocking_notifier_call_chain(&dev
->bus
->p
->bus_notifier
,
2673 BUS_NOTIFY_REMOVED_DEVICE
, dev
);
2674 kobject_uevent(&dev
->kobj
, KOBJ_REMOVE
);
2675 glue_dir
= get_glue_dir(dev
);
2676 kobject_del(&dev
->kobj
);
2677 cleanup_glue_dir(dev
, glue_dir
);
2680 EXPORT_SYMBOL_GPL(device_del
);
2683 * device_unregister - unregister device from system.
2684 * @dev: device going away.
2686 * We do this in two parts, like we do device_register(). First,
2687 * we remove it from all the subsystems with device_del(), then
2688 * we decrement the reference count via put_device(). If that
2689 * is the final reference count, the device will be cleaned up
2690 * via device_release() above. Otherwise, the structure will
2691 * stick around until the final reference to the device is dropped.
2693 void device_unregister(struct device
*dev
)
2695 pr_debug("device: '%s': %s\n", dev_name(dev
), __func__
);
2699 EXPORT_SYMBOL_GPL(device_unregister
);
2701 static struct device
*prev_device(struct klist_iter
*i
)
2703 struct klist_node
*n
= klist_prev(i
);
2704 struct device
*dev
= NULL
;
2705 struct device_private
*p
;
2708 p
= to_device_private_parent(n
);
2714 static struct device
*next_device(struct klist_iter
*i
)
2716 struct klist_node
*n
= klist_next(i
);
2717 struct device
*dev
= NULL
;
2718 struct device_private
*p
;
2721 p
= to_device_private_parent(n
);
2728 * device_get_devnode - path of device node file
2730 * @mode: returned file access mode
2731 * @uid: returned file owner
2732 * @gid: returned file group
2733 * @tmp: possibly allocated string
2735 * Return the relative path of a possible device node.
2736 * Non-default names may need to allocate a memory to compose
2737 * a name. This memory is returned in tmp and needs to be
2738 * freed by the caller.
2740 const char *device_get_devnode(struct device
*dev
,
2741 umode_t
*mode
, kuid_t
*uid
, kgid_t
*gid
,
2748 /* the device type may provide a specific name */
2749 if (dev
->type
&& dev
->type
->devnode
)
2750 *tmp
= dev
->type
->devnode(dev
, mode
, uid
, gid
);
2754 /* the class may provide a specific name */
2755 if (dev
->class && dev
->class->devnode
)
2756 *tmp
= dev
->class->devnode(dev
, mode
);
2760 /* return name without allocation, tmp == NULL */
2761 if (strchr(dev_name(dev
), '!') == NULL
)
2762 return dev_name(dev
);
2764 /* replace '!' in the name with '/' */
2765 s
= kstrdup(dev_name(dev
), GFP_KERNEL
);
2768 strreplace(s
, '!', '/');
2773 * device_for_each_child - device child iterator.
2774 * @parent: parent struct device.
2775 * @fn: function to be called for each device.
2776 * @data: data for the callback.
2778 * Iterate over @parent's child devices, and call @fn for each,
2781 * We check the return of @fn each time. If it returns anything
2782 * other than 0, we break out and return that value.
2784 int device_for_each_child(struct device
*parent
, void *data
,
2785 int (*fn
)(struct device
*dev
, void *data
))
2787 struct klist_iter i
;
2788 struct device
*child
;
2794 klist_iter_init(&parent
->p
->klist_children
, &i
);
2795 while (!error
&& (child
= next_device(&i
)))
2796 error
= fn(child
, data
);
2797 klist_iter_exit(&i
);
2800 EXPORT_SYMBOL_GPL(device_for_each_child
);
2803 * device_for_each_child_reverse - device child iterator in reversed order.
2804 * @parent: parent struct device.
2805 * @fn: function to be called for each device.
2806 * @data: data for the callback.
2808 * Iterate over @parent's child devices, and call @fn for each,
2811 * We check the return of @fn each time. If it returns anything
2812 * other than 0, we break out and return that value.
2814 int device_for_each_child_reverse(struct device
*parent
, void *data
,
2815 int (*fn
)(struct device
*dev
, void *data
))
2817 struct klist_iter i
;
2818 struct device
*child
;
2824 klist_iter_init(&parent
->p
->klist_children
, &i
);
2825 while ((child
= prev_device(&i
)) && !error
)
2826 error
= fn(child
, data
);
2827 klist_iter_exit(&i
);
2830 EXPORT_SYMBOL_GPL(device_for_each_child_reverse
);
2833 * device_find_child - device iterator for locating a particular device.
2834 * @parent: parent struct device
2835 * @match: Callback function to check device
2836 * @data: Data to pass to match function
2838 * This is similar to the device_for_each_child() function above, but it
2839 * returns a reference to a device that is 'found' for later use, as
2840 * determined by the @match callback.
2842 * The callback should return 0 if the device doesn't match and non-zero
2843 * if it does. If the callback returns non-zero and a reference to the
2844 * current device can be obtained, this function will return to the caller
2845 * and not iterate over any more devices.
2847 * NOTE: you will need to drop the reference with put_device() after use.
2849 struct device
*device_find_child(struct device
*parent
, void *data
,
2850 int (*match
)(struct device
*dev
, void *data
))
2852 struct klist_iter i
;
2853 struct device
*child
;
2858 klist_iter_init(&parent
->p
->klist_children
, &i
);
2859 while ((child
= next_device(&i
)))
2860 if (match(child
, data
) && get_device(child
))
2862 klist_iter_exit(&i
);
2865 EXPORT_SYMBOL_GPL(device_find_child
);
2868 * device_find_child_by_name - device iterator for locating a child device.
2869 * @parent: parent struct device
2870 * @name: name of the child device
2872 * This is similar to the device_find_child() function above, but it
2873 * returns a reference to a device that has the name @name.
2875 * NOTE: you will need to drop the reference with put_device() after use.
2877 struct device
*device_find_child_by_name(struct device
*parent
,
2880 struct klist_iter i
;
2881 struct device
*child
;
2886 klist_iter_init(&parent
->p
->klist_children
, &i
);
2887 while ((child
= next_device(&i
)))
2888 if (!strcmp(dev_name(child
), name
) && get_device(child
))
2890 klist_iter_exit(&i
);
2893 EXPORT_SYMBOL_GPL(device_find_child_by_name
);
2895 int __init
devices_init(void)
2897 devices_kset
= kset_create_and_add("devices", &device_uevent_ops
, NULL
);
2900 dev_kobj
= kobject_create_and_add("dev", NULL
);
2903 sysfs_dev_block_kobj
= kobject_create_and_add("block", dev_kobj
);
2904 if (!sysfs_dev_block_kobj
)
2905 goto block_kobj_err
;
2906 sysfs_dev_char_kobj
= kobject_create_and_add("char", dev_kobj
);
2907 if (!sysfs_dev_char_kobj
)
2913 kobject_put(sysfs_dev_block_kobj
);
2915 kobject_put(dev_kobj
);
2917 kset_unregister(devices_kset
);
2921 static int device_check_offline(struct device
*dev
, void *not_used
)
2925 ret
= device_for_each_child(dev
, NULL
, device_check_offline
);
2929 return device_supports_offline(dev
) && !dev
->offline
? -EBUSY
: 0;
2933 * device_offline - Prepare the device for hot-removal.
2934 * @dev: Device to be put offline.
2936 * Execute the device bus type's .offline() callback, if present, to prepare
2937 * the device for a subsequent hot-removal. If that succeeds, the device must
2938 * not be used until either it is removed or its bus type's .online() callback
2941 * Call under device_hotplug_lock.
2943 int device_offline(struct device
*dev
)
2947 if (dev
->offline_disabled
)
2950 ret
= device_for_each_child(dev
, NULL
, device_check_offline
);
2955 if (device_supports_offline(dev
)) {
2959 ret
= dev
->bus
->offline(dev
);
2961 kobject_uevent(&dev
->kobj
, KOBJ_OFFLINE
);
2962 dev
->offline
= true;
2972 * device_online - Put the device back online after successful device_offline().
2973 * @dev: Device to be put back online.
2975 * If device_offline() has been successfully executed for @dev, but the device
2976 * has not been removed subsequently, execute its bus type's .online() callback
2977 * to indicate that the device can be used again.
2979 * Call under device_hotplug_lock.
2981 int device_online(struct device
*dev
)
2986 if (device_supports_offline(dev
)) {
2988 ret
= dev
->bus
->online(dev
);
2990 kobject_uevent(&dev
->kobj
, KOBJ_ONLINE
);
2991 dev
->offline
= false;
3002 struct root_device
{
3004 struct module
*owner
;
3007 static inline struct root_device
*to_root_device(struct device
*d
)
3009 return container_of(d
, struct root_device
, dev
);
3012 static void root_device_release(struct device
*dev
)
3014 kfree(to_root_device(dev
));
3018 * __root_device_register - allocate and register a root device
3019 * @name: root device name
3020 * @owner: owner module of the root device, usually THIS_MODULE
3022 * This function allocates a root device and registers it
3023 * using device_register(). In order to free the returned
3024 * device, use root_device_unregister().
3026 * Root devices are dummy devices which allow other devices
3027 * to be grouped under /sys/devices. Use this function to
3028 * allocate a root device and then use it as the parent of
3029 * any device which should appear under /sys/devices/{name}
3031 * The /sys/devices/{name} directory will also contain a
3032 * 'module' symlink which points to the @owner directory
3035 * Returns &struct device pointer on success, or ERR_PTR() on error.
3037 * Note: You probably want to use root_device_register().
3039 struct device
*__root_device_register(const char *name
, struct module
*owner
)
3041 struct root_device
*root
;
3044 root
= kzalloc(sizeof(struct root_device
), GFP_KERNEL
);
3046 return ERR_PTR(err
);
3048 err
= dev_set_name(&root
->dev
, "%s", name
);
3051 return ERR_PTR(err
);
3054 root
->dev
.release
= root_device_release
;
3056 err
= device_register(&root
->dev
);
3058 put_device(&root
->dev
);
3059 return ERR_PTR(err
);
3062 #ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
3064 struct module_kobject
*mk
= &owner
->mkobj
;
3066 err
= sysfs_create_link(&root
->dev
.kobj
, &mk
->kobj
, "module");
3068 device_unregister(&root
->dev
);
3069 return ERR_PTR(err
);
3071 root
->owner
= owner
;
3077 EXPORT_SYMBOL_GPL(__root_device_register
);
3080 * root_device_unregister - unregister and free a root device
3081 * @dev: device going away
3083 * This function unregisters and cleans up a device that was created by
3084 * root_device_register().
3086 void root_device_unregister(struct device
*dev
)
3088 struct root_device
*root
= to_root_device(dev
);
3091 sysfs_remove_link(&root
->dev
.kobj
, "module");
3093 device_unregister(dev
);
3095 EXPORT_SYMBOL_GPL(root_device_unregister
);
3098 static void device_create_release(struct device
*dev
)
3100 pr_debug("device: '%s': %s\n", dev_name(dev
), __func__
);
3104 static __printf(6, 0) struct device
*
3105 device_create_groups_vargs(struct class *class, struct device
*parent
,
3106 dev_t devt
, void *drvdata
,
3107 const struct attribute_group
**groups
,
3108 const char *fmt
, va_list args
)
3110 struct device
*dev
= NULL
;
3111 int retval
= -ENODEV
;
3113 if (class == NULL
|| IS_ERR(class))
3116 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
3122 device_initialize(dev
);
3125 dev
->parent
= parent
;
3126 dev
->groups
= groups
;
3127 dev
->release
= device_create_release
;
3128 dev_set_drvdata(dev
, drvdata
);
3130 retval
= kobject_set_name_vargs(&dev
->kobj
, fmt
, args
);
3134 retval
= device_add(dev
);
3142 return ERR_PTR(retval
);
3146 * device_create_vargs - creates a device and registers it with sysfs
3147 * @class: pointer to the struct class that this device should be registered to
3148 * @parent: pointer to the parent struct device of this new device, if any
3149 * @devt: the dev_t for the char device to be added
3150 * @drvdata: the data to be added to the device for callbacks
3151 * @fmt: string for the device's name
3152 * @args: va_list for the device's name
3154 * This function can be used by char device classes. A struct device
3155 * will be created in sysfs, registered to the specified class.
3157 * A "dev" file will be created, showing the dev_t for the device, if
3158 * the dev_t is not 0,0.
3159 * If a pointer to a parent struct device is passed in, the newly created
3160 * struct device will be a child of that device in sysfs.
3161 * The pointer to the struct device will be returned from the call.
3162 * Any further sysfs files that might be required can be created using this
3165 * Returns &struct device pointer on success, or ERR_PTR() on error.
3167 * Note: the struct class passed to this function must have previously
3168 * been created with a call to class_create().
3170 struct device
*device_create_vargs(struct class *class, struct device
*parent
,
3171 dev_t devt
, void *drvdata
, const char *fmt
,
3174 return device_create_groups_vargs(class, parent
, devt
, drvdata
, NULL
,
3177 EXPORT_SYMBOL_GPL(device_create_vargs
);
3180 * device_create - creates a device and registers it with sysfs
3181 * @class: pointer to the struct class that this device should be registered to
3182 * @parent: pointer to the parent struct device of this new device, if any
3183 * @devt: the dev_t for the char device to be added
3184 * @drvdata: the data to be added to the device for callbacks
3185 * @fmt: string for the device's name
3187 * This function can be used by char device classes. A struct device
3188 * will be created in sysfs, registered to the specified class.
3190 * A "dev" file will be created, showing the dev_t for the device, if
3191 * the dev_t is not 0,0.
3192 * If a pointer to a parent struct device is passed in, the newly created
3193 * struct device will be a child of that device in sysfs.
3194 * The pointer to the struct device will be returned from the call.
3195 * Any further sysfs files that might be required can be created using this
3198 * Returns &struct device pointer on success, or ERR_PTR() on error.
3200 * Note: the struct class passed to this function must have previously
3201 * been created with a call to class_create().
3203 struct device
*device_create(struct class *class, struct device
*parent
,
3204 dev_t devt
, void *drvdata
, const char *fmt
, ...)
3209 va_start(vargs
, fmt
);
3210 dev
= device_create_vargs(class, parent
, devt
, drvdata
, fmt
, vargs
);
3214 EXPORT_SYMBOL_GPL(device_create
);
3217 * device_create_with_groups - creates a device and registers it with sysfs
3218 * @class: pointer to the struct class that this device should be registered to
3219 * @parent: pointer to the parent struct device of this new device, if any
3220 * @devt: the dev_t for the char device to be added
3221 * @drvdata: the data to be added to the device for callbacks
3222 * @groups: NULL-terminated list of attribute groups to be created
3223 * @fmt: string for the device's name
3225 * This function can be used by char device classes. A struct device
3226 * will be created in sysfs, registered to the specified class.
3227 * Additional attributes specified in the groups parameter will also
3228 * be created automatically.
3230 * A "dev" file will be created, showing the dev_t for the device, if
3231 * the dev_t is not 0,0.
3232 * If a pointer to a parent struct device is passed in, the newly created
3233 * struct device will be a child of that device in sysfs.
3234 * The pointer to the struct device will be returned from the call.
3235 * Any further sysfs files that might be required can be created using this
3238 * Returns &struct device pointer on success, or ERR_PTR() on error.
3240 * Note: the struct class passed to this function must have previously
3241 * been created with a call to class_create().
3243 struct device
*device_create_with_groups(struct class *class,
3244 struct device
*parent
, dev_t devt
,
3246 const struct attribute_group
**groups
,
3247 const char *fmt
, ...)
3252 va_start(vargs
, fmt
);
3253 dev
= device_create_groups_vargs(class, parent
, devt
, drvdata
, groups
,
3258 EXPORT_SYMBOL_GPL(device_create_with_groups
);
3261 * device_destroy - removes a device that was created with device_create()
3262 * @class: pointer to the struct class that this device was registered with
3263 * @devt: the dev_t of the device that was previously registered
3265 * This call unregisters and cleans up a device that was created with a
3266 * call to device_create().
3268 void device_destroy(struct class *class, dev_t devt
)
3272 dev
= class_find_device_by_devt(class, devt
);
3275 device_unregister(dev
);
3278 EXPORT_SYMBOL_GPL(device_destroy
);
3281 * device_rename - renames a device
3282 * @dev: the pointer to the struct device to be renamed
3283 * @new_name: the new name of the device
3285 * It is the responsibility of the caller to provide mutual
3286 * exclusion between two different calls of device_rename
3287 * on the same device to ensure that new_name is valid and
3288 * won't conflict with other devices.
3290 * Note: Don't call this function. Currently, the networking layer calls this
3291 * function, but that will change. The following text from Kay Sievers offers
3294 * Renaming devices is racy at many levels, symlinks and other stuff are not
3295 * replaced atomically, and you get a "move" uevent, but it's not easy to
3296 * connect the event to the old and new device. Device nodes are not renamed at
3297 * all, there isn't even support for that in the kernel now.
3299 * In the meantime, during renaming, your target name might be taken by another
3300 * driver, creating conflicts. Or the old name is taken directly after you
3301 * renamed it -- then you get events for the same DEVPATH, before you even see
3302 * the "move" event. It's just a mess, and nothing new should ever rely on
3303 * kernel device renaming. Besides that, it's not even implemented now for
3304 * other things than (driver-core wise very simple) network devices.
3306 * We are currently about to change network renaming in udev to completely
3307 * disallow renaming of devices in the same namespace as the kernel uses,
3308 * because we can't solve the problems properly, that arise with swapping names
3309 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
3310 * be allowed to some other name than eth[0-9]*, for the aforementioned
3313 * Make up a "real" name in the driver before you register anything, or add
3314 * some other attributes for userspace to find the device, or use udev to add
3315 * symlinks -- but never rename kernel devices later, it's a complete mess. We
3316 * don't even want to get into that and try to implement the missing pieces in
3317 * the core. We really have other pieces to fix in the driver core mess. :)
3319 int device_rename(struct device
*dev
, const char *new_name
)
3321 struct kobject
*kobj
= &dev
->kobj
;
3322 char *old_device_name
= NULL
;
3325 dev
= get_device(dev
);
3329 dev_dbg(dev
, "renaming to %s\n", new_name
);
3331 old_device_name
= kstrdup(dev_name(dev
), GFP_KERNEL
);
3332 if (!old_device_name
) {
3338 error
= sysfs_rename_link_ns(&dev
->class->p
->subsys
.kobj
,
3339 kobj
, old_device_name
,
3340 new_name
, kobject_namespace(kobj
));
3345 error
= kobject_rename(kobj
, new_name
);
3352 kfree(old_device_name
);
3356 EXPORT_SYMBOL_GPL(device_rename
);
3358 static int device_move_class_links(struct device
*dev
,
3359 struct device
*old_parent
,
3360 struct device
*new_parent
)
3365 sysfs_remove_link(&dev
->kobj
, "device");
3367 error
= sysfs_create_link(&dev
->kobj
, &new_parent
->kobj
,
3373 * device_move - moves a device to a new parent
3374 * @dev: the pointer to the struct device to be moved
3375 * @new_parent: the new parent of the device (can be NULL)
3376 * @dpm_order: how to reorder the dpm_list
3378 int device_move(struct device
*dev
, struct device
*new_parent
,
3379 enum dpm_order dpm_order
)
3382 struct device
*old_parent
;
3383 struct kobject
*new_parent_kobj
;
3385 dev
= get_device(dev
);
3390 new_parent
= get_device(new_parent
);
3391 new_parent_kobj
= get_device_parent(dev
, new_parent
);
3392 if (IS_ERR(new_parent_kobj
)) {
3393 error
= PTR_ERR(new_parent_kobj
);
3394 put_device(new_parent
);
3398 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev
),
3399 __func__
, new_parent
? dev_name(new_parent
) : "<NULL>");
3400 error
= kobject_move(&dev
->kobj
, new_parent_kobj
);
3402 cleanup_glue_dir(dev
, new_parent_kobj
);
3403 put_device(new_parent
);
3406 old_parent
= dev
->parent
;
3407 dev
->parent
= new_parent
;
3409 klist_remove(&dev
->p
->knode_parent
);
3411 klist_add_tail(&dev
->p
->knode_parent
,
3412 &new_parent
->p
->klist_children
);
3413 set_dev_node(dev
, dev_to_node(new_parent
));
3417 error
= device_move_class_links(dev
, old_parent
, new_parent
);
3419 /* We ignore errors on cleanup since we're hosed anyway... */
3420 device_move_class_links(dev
, new_parent
, old_parent
);
3421 if (!kobject_move(&dev
->kobj
, &old_parent
->kobj
)) {
3423 klist_remove(&dev
->p
->knode_parent
);
3424 dev
->parent
= old_parent
;
3426 klist_add_tail(&dev
->p
->knode_parent
,
3427 &old_parent
->p
->klist_children
);
3428 set_dev_node(dev
, dev_to_node(old_parent
));
3431 cleanup_glue_dir(dev
, new_parent_kobj
);
3432 put_device(new_parent
);
3436 switch (dpm_order
) {
3437 case DPM_ORDER_NONE
:
3439 case DPM_ORDER_DEV_AFTER_PARENT
:
3440 device_pm_move_after(dev
, new_parent
);
3441 devices_kset_move_after(dev
, new_parent
);
3443 case DPM_ORDER_PARENT_BEFORE_DEV
:
3444 device_pm_move_before(new_parent
, dev
);
3445 devices_kset_move_before(new_parent
, dev
);
3447 case DPM_ORDER_DEV_LAST
:
3448 device_pm_move_last(dev
);
3449 devices_kset_move_last(dev
);
3453 put_device(old_parent
);
3459 EXPORT_SYMBOL_GPL(device_move
);
3462 * device_shutdown - call ->shutdown() on each device to shutdown.
3464 void device_shutdown(void)
3466 struct device
*dev
, *parent
;
3468 wait_for_device_probe();
3469 device_block_probing();
3473 spin_lock(&devices_kset
->list_lock
);
3475 * Walk the devices list backward, shutting down each in turn.
3476 * Beware that device unplug events may also start pulling
3477 * devices offline, even as the system is shutting down.
3479 while (!list_empty(&devices_kset
->list
)) {
3480 dev
= list_entry(devices_kset
->list
.prev
, struct device
,
3484 * hold reference count of device's parent to
3485 * prevent it from being freed because parent's
3486 * lock is to be held
3488 parent
= get_device(dev
->parent
);
3491 * Make sure the device is off the kset list, in the
3492 * event that dev->*->shutdown() doesn't remove it.
3494 list_del_init(&dev
->kobj
.entry
);
3495 spin_unlock(&devices_kset
->list_lock
);
3497 /* hold lock to avoid race with probe/release */
3499 device_lock(parent
);
3502 /* Don't allow any more runtime suspends */
3503 pm_runtime_get_noresume(dev
);
3504 pm_runtime_barrier(dev
);
3506 if (dev
->class && dev
->class->shutdown_pre
) {
3508 dev_info(dev
, "shutdown_pre\n");
3509 dev
->class->shutdown_pre(dev
);
3511 if (dev
->bus
&& dev
->bus
->shutdown
) {
3513 dev_info(dev
, "shutdown\n");
3514 dev
->bus
->shutdown(dev
);
3515 } else if (dev
->driver
&& dev
->driver
->shutdown
) {
3517 dev_info(dev
, "shutdown\n");
3518 dev
->driver
->shutdown(dev
);
3523 device_unlock(parent
);
3528 spin_lock(&devices_kset
->list_lock
);
3530 spin_unlock(&devices_kset
->list_lock
);
3534 * Device logging functions
3537 #ifdef CONFIG_PRINTK
3539 create_syslog_header(const struct device
*dev
, char *hdr
, size_t hdrlen
)
3545 subsys
= dev
->class->name
;
3547 subsys
= dev
->bus
->name
;
3551 pos
+= snprintf(hdr
+ pos
, hdrlen
- pos
, "SUBSYSTEM=%s", subsys
);
3556 * Add device identifier DEVICE=:
3560 * +sound:card0 subsystem:devname
3562 if (MAJOR(dev
->devt
)) {
3565 if (strcmp(subsys
, "block") == 0)
3570 pos
+= snprintf(hdr
+ pos
, hdrlen
- pos
,
3572 c
, MAJOR(dev
->devt
), MINOR(dev
->devt
));
3573 } else if (strcmp(subsys
, "net") == 0) {
3574 struct net_device
*net
= to_net_dev(dev
);
3577 pos
+= snprintf(hdr
+ pos
, hdrlen
- pos
,
3578 "DEVICE=n%u", net
->ifindex
);
3581 pos
+= snprintf(hdr
+ pos
, hdrlen
- pos
,
3582 "DEVICE=+%s:%s", subsys
, dev_name(dev
));
3591 dev_WARN(dev
, "device/subsystem name too long");
3595 int dev_vprintk_emit(int level
, const struct device
*dev
,
3596 const char *fmt
, va_list args
)
3601 hdrlen
= create_syslog_header(dev
, hdr
, sizeof(hdr
));
3603 return vprintk_emit(0, level
, hdrlen
? hdr
: NULL
, hdrlen
, fmt
, args
);
3605 EXPORT_SYMBOL(dev_vprintk_emit
);
3607 int dev_printk_emit(int level
, const struct device
*dev
, const char *fmt
, ...)
3612 va_start(args
, fmt
);
3614 r
= dev_vprintk_emit(level
, dev
, fmt
, args
);
3620 EXPORT_SYMBOL(dev_printk_emit
);
3622 static void __dev_printk(const char *level
, const struct device
*dev
,
3623 struct va_format
*vaf
)
3626 dev_printk_emit(level
[1] - '0', dev
, "%s %s: %pV",
3627 dev_driver_string(dev
), dev_name(dev
), vaf
);
3629 printk("%s(NULL device *): %pV", level
, vaf
);
3632 void dev_printk(const char *level
, const struct device
*dev
,
3633 const char *fmt
, ...)
3635 struct va_format vaf
;
3638 va_start(args
, fmt
);
3643 __dev_printk(level
, dev
, &vaf
);
3647 EXPORT_SYMBOL(dev_printk
);
3649 #define define_dev_printk_level(func, kern_level) \
3650 void func(const struct device *dev, const char *fmt, ...) \
3652 struct va_format vaf; \
3655 va_start(args, fmt); \
3660 __dev_printk(kern_level, dev, &vaf); \
3664 EXPORT_SYMBOL(func);
3666 define_dev_printk_level(_dev_emerg
, KERN_EMERG
);
3667 define_dev_printk_level(_dev_alert
, KERN_ALERT
);
3668 define_dev_printk_level(_dev_crit
, KERN_CRIT
);
3669 define_dev_printk_level(_dev_err
, KERN_ERR
);
3670 define_dev_printk_level(_dev_warn
, KERN_WARNING
);
3671 define_dev_printk_level(_dev_notice
, KERN_NOTICE
);
3672 define_dev_printk_level(_dev_info
, KERN_INFO
);
3676 static inline bool fwnode_is_primary(struct fwnode_handle
*fwnode
)
3678 return fwnode
&& !IS_ERR(fwnode
->secondary
);
3682 * set_primary_fwnode - Change the primary firmware node of a given device.
3683 * @dev: Device to handle.
3684 * @fwnode: New primary firmware node of the device.
3686 * Set the device's firmware node pointer to @fwnode, but if a secondary
3687 * firmware node of the device is present, preserve it.
3689 void set_primary_fwnode(struct device
*dev
, struct fwnode_handle
*fwnode
)
3692 struct fwnode_handle
*fn
= dev
->fwnode
;
3694 if (fwnode_is_primary(fn
))
3698 WARN_ON(fwnode
->secondary
);
3699 fwnode
->secondary
= fn
;
3701 dev
->fwnode
= fwnode
;
3703 dev
->fwnode
= fwnode_is_primary(dev
->fwnode
) ?
3704 dev
->fwnode
->secondary
: NULL
;
3707 EXPORT_SYMBOL_GPL(set_primary_fwnode
);
3710 * set_secondary_fwnode - Change the secondary firmware node of a given device.
3711 * @dev: Device to handle.
3712 * @fwnode: New secondary firmware node of the device.
3714 * If a primary firmware node of the device is present, set its secondary
3715 * pointer to @fwnode. Otherwise, set the device's firmware node pointer to
3718 void set_secondary_fwnode(struct device
*dev
, struct fwnode_handle
*fwnode
)
3721 fwnode
->secondary
= ERR_PTR(-ENODEV
);
3723 if (fwnode_is_primary(dev
->fwnode
))
3724 dev
->fwnode
->secondary
= fwnode
;
3726 dev
->fwnode
= fwnode
;
3730 * device_set_of_node_from_dev - reuse device-tree node of another device
3731 * @dev: device whose device-tree node is being set
3732 * @dev2: device whose device-tree node is being reused
3734 * Takes another reference to the new device-tree node after first dropping
3735 * any reference held to the old node.
3737 void device_set_of_node_from_dev(struct device
*dev
, const struct device
*dev2
)
3739 of_node_put(dev
->of_node
);
3740 dev
->of_node
= of_node_get(dev2
->of_node
);
3741 dev
->of_node_reused
= true;
3743 EXPORT_SYMBOL_GPL(device_set_of_node_from_dev
);
3745 int device_match_name(struct device
*dev
, const void *name
)
3747 return sysfs_streq(dev_name(dev
), name
);
3749 EXPORT_SYMBOL_GPL(device_match_name
);
3751 int device_match_of_node(struct device
*dev
, const void *np
)
3753 return dev
->of_node
== np
;
3755 EXPORT_SYMBOL_GPL(device_match_of_node
);
3757 int device_match_fwnode(struct device
*dev
, const void *fwnode
)
3759 return dev_fwnode(dev
) == fwnode
;
3761 EXPORT_SYMBOL_GPL(device_match_fwnode
);
3763 int device_match_devt(struct device
*dev
, const void *pdevt
)
3765 return dev
->devt
== *(dev_t
*)pdevt
;
3767 EXPORT_SYMBOL_GPL(device_match_devt
);
3769 int device_match_acpi_dev(struct device
*dev
, const void *adev
)
3771 return ACPI_COMPANION(dev
) == adev
;
3773 EXPORT_SYMBOL(device_match_acpi_dev
);
3775 int device_match_any(struct device
*dev
, const void *unused
)
3779 EXPORT_SYMBOL_GPL(device_match_any
);