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) __acquires(&device_links_srcu
)
69 return srcu_read_lock(&device_links_srcu
);
72 void device_links_read_unlock(int idx
) __releases(&device_links_srcu
)
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
;
368 link
->flags
|= DL_FLAG_STATELESS
;
374 * If the life time of the link following from the new flags is
375 * longer than indicated by the flags of the existing link,
376 * update the existing link to stay around longer.
378 if (flags
& DL_FLAG_AUTOREMOVE_SUPPLIER
) {
379 if (link
->flags
& DL_FLAG_AUTOREMOVE_CONSUMER
) {
380 link
->flags
&= ~DL_FLAG_AUTOREMOVE_CONSUMER
;
381 link
->flags
|= DL_FLAG_AUTOREMOVE_SUPPLIER
;
383 } else if (!(flags
& DL_FLAG_AUTOREMOVE_CONSUMER
)) {
384 link
->flags
&= ~(DL_FLAG_AUTOREMOVE_CONSUMER
|
385 DL_FLAG_AUTOREMOVE_SUPPLIER
);
387 if (!(link
->flags
& DL_FLAG_MANAGED
)) {
388 kref_get(&link
->kref
);
389 link
->flags
|= DL_FLAG_MANAGED
;
390 device_link_init_status(link
, consumer
, supplier
);
392 if (link
->flags
& DL_FLAG_SYNC_STATE_ONLY
&&
393 !(flags
& DL_FLAG_SYNC_STATE_ONLY
)) {
394 link
->flags
&= ~DL_FLAG_SYNC_STATE_ONLY
;
401 link
= kzalloc(sizeof(*link
), GFP_KERNEL
);
405 refcount_set(&link
->rpm_active
, 1);
407 if (flags
& DL_FLAG_PM_RUNTIME
) {
408 if (flags
& DL_FLAG_RPM_ACTIVE
)
409 refcount_inc(&link
->rpm_active
);
411 pm_runtime_new_link(consumer
);
414 get_device(supplier
);
415 link
->supplier
= supplier
;
416 INIT_LIST_HEAD(&link
->s_node
);
417 get_device(consumer
);
418 link
->consumer
= consumer
;
419 INIT_LIST_HEAD(&link
->c_node
);
421 kref_init(&link
->kref
);
423 /* Determine the initial link state. */
424 if (flags
& DL_FLAG_STATELESS
)
425 link
->status
= DL_STATE_NONE
;
427 device_link_init_status(link
, consumer
, supplier
);
430 * Some callers expect the link creation during consumer driver probe to
431 * resume the supplier even without DL_FLAG_RPM_ACTIVE.
433 if (link
->status
== DL_STATE_CONSUMER_PROBE
&&
434 flags
& DL_FLAG_PM_RUNTIME
)
435 pm_runtime_resume(supplier
);
437 list_add_tail_rcu(&link
->s_node
, &supplier
->links
.consumers
);
438 list_add_tail_rcu(&link
->c_node
, &consumer
->links
.suppliers
);
440 if (flags
& DL_FLAG_SYNC_STATE_ONLY
) {
442 "Linked as a sync state only consumer to %s\n",
449 * Move the consumer and all of the devices depending on it to the end
450 * of dpm_list and the devices_kset list.
452 * It is necessary to hold dpm_list locked throughout all that or else
453 * we may end up suspending with a wrong ordering of it.
455 device_reorder_to_tail(consumer
, NULL
);
457 dev_dbg(consumer
, "Linked as a consumer to %s\n", dev_name(supplier
));
461 device_links_write_unlock();
463 if ((flags
& DL_FLAG_PM_RUNTIME
&& flags
& DL_FLAG_RPM_ACTIVE
) && !link
)
464 pm_runtime_put(supplier
);
468 EXPORT_SYMBOL_GPL(device_link_add
);
471 * device_link_wait_for_supplier - Add device to wait_for_suppliers list
472 * @consumer: Consumer device
474 * Marks the @consumer device as waiting for suppliers to become available by
475 * adding it to the wait_for_suppliers list. The consumer device will never be
476 * probed until it's removed from the wait_for_suppliers list.
478 * The caller is responsible for adding the links to the supplier devices once
479 * they are available and removing the @consumer device from the
480 * wait_for_suppliers list once links to all the suppliers have been created.
482 * This function is NOT meant to be called from the probe function of the
483 * consumer but rather from code that creates/adds the consumer device.
485 static void device_link_wait_for_supplier(struct device
*consumer
,
488 mutex_lock(&wfs_lock
);
489 list_add_tail(&consumer
->links
.needs_suppliers
, &wait_for_suppliers
);
490 consumer
->links
.need_for_probe
= need_for_probe
;
491 mutex_unlock(&wfs_lock
);
494 static void device_link_wait_for_mandatory_supplier(struct device
*consumer
)
496 device_link_wait_for_supplier(consumer
, true);
499 static void device_link_wait_for_optional_supplier(struct device
*consumer
)
501 device_link_wait_for_supplier(consumer
, false);
505 * device_link_add_missing_supplier_links - Add links from consumer devices to
506 * supplier devices, leaving any
507 * consumer with inactive suppliers on
508 * the wait_for_suppliers list
510 * Loops through all consumers waiting on suppliers and tries to add all their
511 * supplier links. If that succeeds, the consumer device is removed from
512 * wait_for_suppliers list. Otherwise, they are left in the wait_for_suppliers
513 * list. Devices left on the wait_for_suppliers list will not be probed.
515 * The fwnode add_links callback is expected to return 0 if it has found and
516 * added all the supplier links for the consumer device. It should return an
517 * error if it isn't able to do so.
519 * The caller of device_link_wait_for_supplier() is expected to call this once
520 * it's aware of potential suppliers becoming available.
522 static void device_link_add_missing_supplier_links(void)
524 struct device
*dev
, *tmp
;
526 mutex_lock(&wfs_lock
);
527 list_for_each_entry_safe(dev
, tmp
, &wait_for_suppliers
,
528 links
.needs_suppliers
) {
529 int ret
= fwnode_call_int_op(dev
->fwnode
, add_links
, dev
);
531 list_del_init(&dev
->links
.needs_suppliers
);
532 else if (ret
!= -ENODEV
)
533 dev
->links
.need_for_probe
= false;
535 mutex_unlock(&wfs_lock
);
538 static void device_link_free(struct device_link
*link
)
540 while (refcount_dec_not_one(&link
->rpm_active
))
541 pm_runtime_put(link
->supplier
);
543 put_device(link
->consumer
);
544 put_device(link
->supplier
);
549 static void __device_link_free_srcu(struct rcu_head
*rhead
)
551 device_link_free(container_of(rhead
, struct device_link
, rcu_head
));
554 static void __device_link_del(struct kref
*kref
)
556 struct device_link
*link
= container_of(kref
, struct device_link
, kref
);
558 dev_dbg(link
->consumer
, "Dropping the link to %s\n",
559 dev_name(link
->supplier
));
561 if (link
->flags
& DL_FLAG_PM_RUNTIME
)
562 pm_runtime_drop_link(link
->consumer
);
564 list_del_rcu(&link
->s_node
);
565 list_del_rcu(&link
->c_node
);
566 call_srcu(&device_links_srcu
, &link
->rcu_head
, __device_link_free_srcu
);
568 #else /* !CONFIG_SRCU */
569 static void __device_link_del(struct kref
*kref
)
571 struct device_link
*link
= container_of(kref
, struct device_link
, kref
);
573 dev_info(link
->consumer
, "Dropping the link to %s\n",
574 dev_name(link
->supplier
));
576 if (link
->flags
& DL_FLAG_PM_RUNTIME
)
577 pm_runtime_drop_link(link
->consumer
);
579 list_del(&link
->s_node
);
580 list_del(&link
->c_node
);
581 device_link_free(link
);
583 #endif /* !CONFIG_SRCU */
585 static void device_link_put_kref(struct device_link
*link
)
587 if (link
->flags
& DL_FLAG_STATELESS
)
588 kref_put(&link
->kref
, __device_link_del
);
590 WARN(1, "Unable to drop a managed device link reference\n");
594 * device_link_del - Delete a stateless link between two devices.
595 * @link: Device link to delete.
597 * The caller must ensure proper synchronization of this function with runtime
598 * PM. If the link was added multiple times, it needs to be deleted as often.
599 * Care is required for hotplugged devices: Their links are purged on removal
600 * and calling device_link_del() is then no longer allowed.
602 void device_link_del(struct device_link
*link
)
604 device_links_write_lock();
606 device_link_put_kref(link
);
608 device_links_write_unlock();
610 EXPORT_SYMBOL_GPL(device_link_del
);
613 * device_link_remove - Delete a stateless link between two devices.
614 * @consumer: Consumer end of the link.
615 * @supplier: Supplier end of the link.
617 * The caller must ensure proper synchronization of this function with runtime
620 void device_link_remove(void *consumer
, struct device
*supplier
)
622 struct device_link
*link
;
624 if (WARN_ON(consumer
== supplier
))
627 device_links_write_lock();
630 list_for_each_entry(link
, &supplier
->links
.consumers
, s_node
) {
631 if (link
->consumer
== consumer
) {
632 device_link_put_kref(link
);
638 device_links_write_unlock();
640 EXPORT_SYMBOL_GPL(device_link_remove
);
642 static void device_links_missing_supplier(struct device
*dev
)
644 struct device_link
*link
;
646 list_for_each_entry(link
, &dev
->links
.suppliers
, c_node
) {
647 if (link
->status
!= DL_STATE_CONSUMER_PROBE
)
650 if (link
->supplier
->links
.status
== DL_DEV_DRIVER_BOUND
) {
651 WRITE_ONCE(link
->status
, DL_STATE_AVAILABLE
);
653 WARN_ON(!(link
->flags
& DL_FLAG_SYNC_STATE_ONLY
));
654 WRITE_ONCE(link
->status
, DL_STATE_DORMANT
);
660 * device_links_check_suppliers - Check presence of supplier drivers.
661 * @dev: Consumer device.
663 * Check links from this device to any suppliers. Walk the list of the device's
664 * links to suppliers and see if all of them are available. If not, simply
665 * return -EPROBE_DEFER.
667 * We need to guarantee that the supplier will not go away after the check has
668 * been positive here. It only can go away in __device_release_driver() and
669 * that function checks the device's links to consumers. This means we need to
670 * mark the link as "consumer probe in progress" to make the supplier removal
671 * wait for us to complete (or bad things may happen).
673 * Links without the DL_FLAG_MANAGED flag set are ignored.
675 int device_links_check_suppliers(struct device
*dev
)
677 struct device_link
*link
;
681 * Device waiting for supplier to become available is not allowed to
684 mutex_lock(&wfs_lock
);
685 if (!list_empty(&dev
->links
.needs_suppliers
) &&
686 dev
->links
.need_for_probe
) {
687 mutex_unlock(&wfs_lock
);
688 return -EPROBE_DEFER
;
690 mutex_unlock(&wfs_lock
);
692 device_links_write_lock();
694 list_for_each_entry(link
, &dev
->links
.suppliers
, c_node
) {
695 if (!(link
->flags
& DL_FLAG_MANAGED
))
698 if (link
->status
!= DL_STATE_AVAILABLE
&&
699 !(link
->flags
& DL_FLAG_SYNC_STATE_ONLY
)) {
700 device_links_missing_supplier(dev
);
704 WRITE_ONCE(link
->status
, DL_STATE_CONSUMER_PROBE
);
706 dev
->links
.status
= DL_DEV_PROBING
;
708 device_links_write_unlock();
713 * __device_links_queue_sync_state - Queue a device for sync_state() callback
714 * @dev: Device to call sync_state() on
715 * @list: List head to queue the @dev on
717 * Queues a device for a sync_state() callback when the device links write lock
718 * isn't held. This allows the sync_state() execution flow to use device links
719 * APIs. The caller must ensure this function is called with
720 * device_links_write_lock() held.
722 * This function does a get_device() to make sure the device is not freed while
725 * So the caller must also ensure that device_links_flush_sync_list() is called
726 * as soon as the caller releases device_links_write_lock(). This is necessary
727 * to make sure the sync_state() is called in a timely fashion and the
728 * put_device() is called on this device.
730 static void __device_links_queue_sync_state(struct device
*dev
,
731 struct list_head
*list
)
733 struct device_link
*link
;
735 if (!dev_has_sync_state(dev
))
737 if (dev
->state_synced
)
740 list_for_each_entry(link
, &dev
->links
.consumers
, s_node
) {
741 if (!(link
->flags
& DL_FLAG_MANAGED
))
743 if (link
->status
!= DL_STATE_ACTIVE
)
748 * Set the flag here to avoid adding the same device to a list more
749 * than once. This can happen if new consumers get added to the device
750 * and probed before the list is flushed.
752 dev
->state_synced
= true;
754 if (WARN_ON(!list_empty(&dev
->links
.defer_sync
)))
758 list_add_tail(&dev
->links
.defer_sync
, list
);
762 * device_links_flush_sync_list - Call sync_state() on a list of devices
763 * @list: List of devices to call sync_state() on
764 * @dont_lock_dev: Device for which lock is already held by the caller
766 * Calls sync_state() on all the devices that have been queued for it. This
767 * function is used in conjunction with __device_links_queue_sync_state(). The
768 * @dont_lock_dev parameter is useful when this function is called from a
769 * context where a device lock is already held.
771 static void device_links_flush_sync_list(struct list_head
*list
,
772 struct device
*dont_lock_dev
)
774 struct device
*dev
, *tmp
;
776 list_for_each_entry_safe(dev
, tmp
, list
, links
.defer_sync
) {
777 list_del_init(&dev
->links
.defer_sync
);
779 if (dev
!= dont_lock_dev
)
782 if (dev
->bus
->sync_state
)
783 dev
->bus
->sync_state(dev
);
784 else if (dev
->driver
&& dev
->driver
->sync_state
)
785 dev
->driver
->sync_state(dev
);
787 if (dev
!= dont_lock_dev
)
794 void device_links_supplier_sync_state_pause(void)
796 device_links_write_lock();
797 defer_sync_state_count
++;
798 device_links_write_unlock();
801 void device_links_supplier_sync_state_resume(void)
803 struct device
*dev
, *tmp
;
804 LIST_HEAD(sync_list
);
806 device_links_write_lock();
807 if (!defer_sync_state_count
) {
808 WARN(true, "Unmatched sync_state pause/resume!");
811 defer_sync_state_count
--;
812 if (defer_sync_state_count
)
815 list_for_each_entry_safe(dev
, tmp
, &deferred_sync
, links
.defer_sync
) {
817 * Delete from deferred_sync list before queuing it to
818 * sync_list because defer_sync is used for both lists.
820 list_del_init(&dev
->links
.defer_sync
);
821 __device_links_queue_sync_state(dev
, &sync_list
);
824 device_links_write_unlock();
826 device_links_flush_sync_list(&sync_list
, NULL
);
829 static int sync_state_resume_initcall(void)
831 device_links_supplier_sync_state_resume();
834 late_initcall(sync_state_resume_initcall
);
836 static void __device_links_supplier_defer_sync(struct device
*sup
)
838 if (list_empty(&sup
->links
.defer_sync
) && dev_has_sync_state(sup
))
839 list_add_tail(&sup
->links
.defer_sync
, &deferred_sync
);
842 static void device_link_drop_managed(struct device_link
*link
)
844 link
->flags
&= ~DL_FLAG_MANAGED
;
845 WRITE_ONCE(link
->status
, DL_STATE_NONE
);
846 kref_put(&link
->kref
, __device_link_del
);
850 * device_links_driver_bound - Update device links after probing its driver.
851 * @dev: Device to update the links for.
853 * The probe has been successful, so update links from this device to any
854 * consumers by changing their status to "available".
856 * Also change the status of @dev's links to suppliers to "active".
858 * Links without the DL_FLAG_MANAGED flag set are ignored.
860 void device_links_driver_bound(struct device
*dev
)
862 struct device_link
*link
, *ln
;
863 LIST_HEAD(sync_list
);
866 * If a device probes successfully, it's expected to have created all
867 * the device links it needs to or make new device links as it needs
868 * them. So, it no longer needs to wait on any suppliers.
870 mutex_lock(&wfs_lock
);
871 list_del_init(&dev
->links
.needs_suppliers
);
872 mutex_unlock(&wfs_lock
);
874 device_links_write_lock();
876 list_for_each_entry(link
, &dev
->links
.consumers
, s_node
) {
877 if (!(link
->flags
& DL_FLAG_MANAGED
))
881 * Links created during consumer probe may be in the "consumer
882 * probe" state to start with if the supplier is still probing
883 * when they are created and they may become "active" if the
884 * consumer probe returns first. Skip them here.
886 if (link
->status
== DL_STATE_CONSUMER_PROBE
||
887 link
->status
== DL_STATE_ACTIVE
)
890 WARN_ON(link
->status
!= DL_STATE_DORMANT
);
891 WRITE_ONCE(link
->status
, DL_STATE_AVAILABLE
);
893 if (link
->flags
& DL_FLAG_AUTOPROBE_CONSUMER
)
894 driver_deferred_probe_add(link
->consumer
);
897 if (defer_sync_state_count
)
898 __device_links_supplier_defer_sync(dev
);
900 __device_links_queue_sync_state(dev
, &sync_list
);
902 list_for_each_entry_safe(link
, ln
, &dev
->links
.suppliers
, c_node
) {
903 struct device
*supplier
;
905 if (!(link
->flags
& DL_FLAG_MANAGED
))
908 supplier
= link
->supplier
;
909 if (link
->flags
& DL_FLAG_SYNC_STATE_ONLY
) {
911 * When DL_FLAG_SYNC_STATE_ONLY is set, it means no
912 * other DL_MANAGED_LINK_FLAGS have been set. So, it's
913 * save to drop the managed link completely.
915 device_link_drop_managed(link
);
917 WARN_ON(link
->status
!= DL_STATE_CONSUMER_PROBE
);
918 WRITE_ONCE(link
->status
, DL_STATE_ACTIVE
);
922 * This needs to be done even for the deleted
923 * DL_FLAG_SYNC_STATE_ONLY device link in case it was the last
924 * device link that was preventing the supplier from getting a
927 if (defer_sync_state_count
)
928 __device_links_supplier_defer_sync(supplier
);
930 __device_links_queue_sync_state(supplier
, &sync_list
);
933 dev
->links
.status
= DL_DEV_DRIVER_BOUND
;
935 device_links_write_unlock();
937 device_links_flush_sync_list(&sync_list
, dev
);
941 * __device_links_no_driver - Update links of a device without a driver.
942 * @dev: Device without a drvier.
944 * Delete all non-persistent links from this device to any suppliers.
946 * Persistent links stay around, but their status is changed to "available",
947 * unless they already are in the "supplier unbind in progress" state in which
948 * case they need not be updated.
950 * Links without the DL_FLAG_MANAGED flag set are ignored.
952 static void __device_links_no_driver(struct device
*dev
)
954 struct device_link
*link
, *ln
;
956 list_for_each_entry_safe_reverse(link
, ln
, &dev
->links
.suppliers
, c_node
) {
957 if (!(link
->flags
& DL_FLAG_MANAGED
))
960 if (link
->flags
& DL_FLAG_AUTOREMOVE_CONSUMER
) {
961 device_link_drop_managed(link
);
965 if (link
->status
!= DL_STATE_CONSUMER_PROBE
&&
966 link
->status
!= DL_STATE_ACTIVE
)
969 if (link
->supplier
->links
.status
== DL_DEV_DRIVER_BOUND
) {
970 WRITE_ONCE(link
->status
, DL_STATE_AVAILABLE
);
972 WARN_ON(!(link
->flags
& DL_FLAG_SYNC_STATE_ONLY
));
973 WRITE_ONCE(link
->status
, DL_STATE_DORMANT
);
977 dev
->links
.status
= DL_DEV_NO_DRIVER
;
981 * device_links_no_driver - Update links after failing driver probe.
982 * @dev: Device whose driver has just failed to probe.
984 * Clean up leftover links to consumers for @dev and invoke
985 * %__device_links_no_driver() to update links to suppliers for it as
988 * Links without the DL_FLAG_MANAGED flag set are ignored.
990 void device_links_no_driver(struct device
*dev
)
992 struct device_link
*link
;
994 device_links_write_lock();
996 list_for_each_entry(link
, &dev
->links
.consumers
, s_node
) {
997 if (!(link
->flags
& DL_FLAG_MANAGED
))
1001 * The probe has failed, so if the status of the link is
1002 * "consumer probe" or "active", it must have been added by
1003 * a probing consumer while this device was still probing.
1004 * Change its state to "dormant", as it represents a valid
1005 * relationship, but it is not functionally meaningful.
1007 if (link
->status
== DL_STATE_CONSUMER_PROBE
||
1008 link
->status
== DL_STATE_ACTIVE
)
1009 WRITE_ONCE(link
->status
, DL_STATE_DORMANT
);
1012 __device_links_no_driver(dev
);
1014 device_links_write_unlock();
1018 * device_links_driver_cleanup - Update links after driver removal.
1019 * @dev: Device whose driver has just gone away.
1021 * Update links to consumers for @dev by changing their status to "dormant" and
1022 * invoke %__device_links_no_driver() to update links to suppliers for it as
1025 * Links without the DL_FLAG_MANAGED flag set are ignored.
1027 void device_links_driver_cleanup(struct device
*dev
)
1029 struct device_link
*link
, *ln
;
1031 device_links_write_lock();
1033 list_for_each_entry_safe(link
, ln
, &dev
->links
.consumers
, s_node
) {
1034 if (!(link
->flags
& DL_FLAG_MANAGED
))
1037 WARN_ON(link
->flags
& DL_FLAG_AUTOREMOVE_CONSUMER
);
1038 WARN_ON(link
->status
!= DL_STATE_SUPPLIER_UNBIND
);
1041 * autoremove the links between this @dev and its consumer
1042 * devices that are not active, i.e. where the link state
1043 * has moved to DL_STATE_SUPPLIER_UNBIND.
1045 if (link
->status
== DL_STATE_SUPPLIER_UNBIND
&&
1046 link
->flags
& DL_FLAG_AUTOREMOVE_SUPPLIER
)
1047 device_link_drop_managed(link
);
1049 WRITE_ONCE(link
->status
, DL_STATE_DORMANT
);
1052 list_del_init(&dev
->links
.defer_sync
);
1053 __device_links_no_driver(dev
);
1055 device_links_write_unlock();
1059 * device_links_busy - Check if there are any busy links to consumers.
1060 * @dev: Device to check.
1062 * Check each consumer of the device and return 'true' if its link's status
1063 * is one of "consumer probe" or "active" (meaning that the given consumer is
1064 * probing right now or its driver is present). Otherwise, change the link
1065 * state to "supplier unbind" to prevent the consumer from being probed
1066 * successfully going forward.
1068 * Return 'false' if there are no probing or active consumers.
1070 * Links without the DL_FLAG_MANAGED flag set are ignored.
1072 bool device_links_busy(struct device
*dev
)
1074 struct device_link
*link
;
1077 device_links_write_lock();
1079 list_for_each_entry(link
, &dev
->links
.consumers
, s_node
) {
1080 if (!(link
->flags
& DL_FLAG_MANAGED
))
1083 if (link
->status
== DL_STATE_CONSUMER_PROBE
1084 || link
->status
== DL_STATE_ACTIVE
) {
1088 WRITE_ONCE(link
->status
, DL_STATE_SUPPLIER_UNBIND
);
1091 dev
->links
.status
= DL_DEV_UNBINDING
;
1093 device_links_write_unlock();
1098 * device_links_unbind_consumers - Force unbind consumers of the given device.
1099 * @dev: Device to unbind the consumers of.
1101 * Walk the list of links to consumers for @dev and if any of them is in the
1102 * "consumer probe" state, wait for all device probes in progress to complete
1105 * If that's not the case, change the status of the link to "supplier unbind"
1106 * and check if the link was in the "active" state. If so, force the consumer
1107 * driver to unbind and start over (the consumer will not re-probe as we have
1108 * changed the state of the link already).
1110 * Links without the DL_FLAG_MANAGED flag set are ignored.
1112 void device_links_unbind_consumers(struct device
*dev
)
1114 struct device_link
*link
;
1117 device_links_write_lock();
1119 list_for_each_entry(link
, &dev
->links
.consumers
, s_node
) {
1120 enum device_link_state status
;
1122 if (!(link
->flags
& DL_FLAG_MANAGED
) ||
1123 link
->flags
& DL_FLAG_SYNC_STATE_ONLY
)
1126 status
= link
->status
;
1127 if (status
== DL_STATE_CONSUMER_PROBE
) {
1128 device_links_write_unlock();
1130 wait_for_device_probe();
1133 WRITE_ONCE(link
->status
, DL_STATE_SUPPLIER_UNBIND
);
1134 if (status
== DL_STATE_ACTIVE
) {
1135 struct device
*consumer
= link
->consumer
;
1137 get_device(consumer
);
1139 device_links_write_unlock();
1141 device_release_driver_internal(consumer
, NULL
,
1143 put_device(consumer
);
1148 device_links_write_unlock();
1152 * device_links_purge - Delete existing links to other devices.
1153 * @dev: Target device.
1155 static void device_links_purge(struct device
*dev
)
1157 struct device_link
*link
, *ln
;
1159 mutex_lock(&wfs_lock
);
1160 list_del(&dev
->links
.needs_suppliers
);
1161 mutex_unlock(&wfs_lock
);
1164 * Delete all of the remaining links from this device to any other
1165 * devices (either consumers or suppliers).
1167 device_links_write_lock();
1169 list_for_each_entry_safe_reverse(link
, ln
, &dev
->links
.suppliers
, c_node
) {
1170 WARN_ON(link
->status
== DL_STATE_ACTIVE
);
1171 __device_link_del(&link
->kref
);
1174 list_for_each_entry_safe_reverse(link
, ln
, &dev
->links
.consumers
, s_node
) {
1175 WARN_ON(link
->status
!= DL_STATE_DORMANT
&&
1176 link
->status
!= DL_STATE_NONE
);
1177 __device_link_del(&link
->kref
);
1180 device_links_write_unlock();
1183 /* Device links support end. */
1185 int (*platform_notify
)(struct device
*dev
) = NULL
;
1186 int (*platform_notify_remove
)(struct device
*dev
) = NULL
;
1187 static struct kobject
*dev_kobj
;
1188 struct kobject
*sysfs_dev_char_kobj
;
1189 struct kobject
*sysfs_dev_block_kobj
;
1191 static DEFINE_MUTEX(device_hotplug_lock
);
1193 void lock_device_hotplug(void)
1195 mutex_lock(&device_hotplug_lock
);
1198 void unlock_device_hotplug(void)
1200 mutex_unlock(&device_hotplug_lock
);
1203 int lock_device_hotplug_sysfs(void)
1205 if (mutex_trylock(&device_hotplug_lock
))
1208 /* Avoid busy looping (5 ms of sleep should do). */
1210 return restart_syscall();
1214 static inline int device_is_not_partition(struct device
*dev
)
1216 return !(dev
->type
== &part_type
);
1219 static inline int device_is_not_partition(struct device
*dev
)
1226 device_platform_notify(struct device
*dev
, enum kobject_action action
)
1230 ret
= acpi_platform_notify(dev
, action
);
1234 ret
= software_node_notify(dev
, action
);
1238 if (platform_notify
&& action
== KOBJ_ADD
)
1239 platform_notify(dev
);
1240 else if (platform_notify_remove
&& action
== KOBJ_REMOVE
)
1241 platform_notify_remove(dev
);
1246 * dev_driver_string - Return a device's driver name, if at all possible
1247 * @dev: struct device to get the name of
1249 * Will return the device's driver's name if it is bound to a device. If
1250 * the device is not bound to a driver, it will return the name of the bus
1251 * it is attached to. If it is not attached to a bus either, an empty
1252 * string will be returned.
1254 const char *dev_driver_string(const struct device
*dev
)
1256 struct device_driver
*drv
;
1258 /* dev->driver can change to NULL underneath us because of unbinding,
1259 * so be careful about accessing it. dev->bus and dev->class should
1260 * never change once they are set, so they don't need special care.
1262 drv
= READ_ONCE(dev
->driver
);
1263 return drv
? drv
->name
:
1264 (dev
->bus
? dev
->bus
->name
:
1265 (dev
->class ? dev
->class->name
: ""));
1267 EXPORT_SYMBOL(dev_driver_string
);
1269 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
1271 static ssize_t
dev_attr_show(struct kobject
*kobj
, struct attribute
*attr
,
1274 struct device_attribute
*dev_attr
= to_dev_attr(attr
);
1275 struct device
*dev
= kobj_to_dev(kobj
);
1279 ret
= dev_attr
->show(dev
, dev_attr
, buf
);
1280 if (ret
>= (ssize_t
)PAGE_SIZE
) {
1281 printk("dev_attr_show: %pS returned bad count\n",
1287 static ssize_t
dev_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
1288 const char *buf
, size_t count
)
1290 struct device_attribute
*dev_attr
= to_dev_attr(attr
);
1291 struct device
*dev
= kobj_to_dev(kobj
);
1294 if (dev_attr
->store
)
1295 ret
= dev_attr
->store(dev
, dev_attr
, buf
, count
);
1299 static const struct sysfs_ops dev_sysfs_ops
= {
1300 .show
= dev_attr_show
,
1301 .store
= dev_attr_store
,
1304 #define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
1306 ssize_t
device_store_ulong(struct device
*dev
,
1307 struct device_attribute
*attr
,
1308 const char *buf
, size_t size
)
1310 struct dev_ext_attribute
*ea
= to_ext_attr(attr
);
1314 ret
= kstrtoul(buf
, 0, &new);
1317 *(unsigned long *)(ea
->var
) = new;
1318 /* Always return full write size even if we didn't consume all */
1321 EXPORT_SYMBOL_GPL(device_store_ulong
);
1323 ssize_t
device_show_ulong(struct device
*dev
,
1324 struct device_attribute
*attr
,
1327 struct dev_ext_attribute
*ea
= to_ext_attr(attr
);
1328 return snprintf(buf
, PAGE_SIZE
, "%lx\n", *(unsigned long *)(ea
->var
));
1330 EXPORT_SYMBOL_GPL(device_show_ulong
);
1332 ssize_t
device_store_int(struct device
*dev
,
1333 struct device_attribute
*attr
,
1334 const char *buf
, size_t size
)
1336 struct dev_ext_attribute
*ea
= to_ext_attr(attr
);
1340 ret
= kstrtol(buf
, 0, &new);
1344 if (new > INT_MAX
|| new < INT_MIN
)
1346 *(int *)(ea
->var
) = new;
1347 /* Always return full write size even if we didn't consume all */
1350 EXPORT_SYMBOL_GPL(device_store_int
);
1352 ssize_t
device_show_int(struct device
*dev
,
1353 struct device_attribute
*attr
,
1356 struct dev_ext_attribute
*ea
= to_ext_attr(attr
);
1358 return snprintf(buf
, PAGE_SIZE
, "%d\n", *(int *)(ea
->var
));
1360 EXPORT_SYMBOL_GPL(device_show_int
);
1362 ssize_t
device_store_bool(struct device
*dev
, struct device_attribute
*attr
,
1363 const char *buf
, size_t size
)
1365 struct dev_ext_attribute
*ea
= to_ext_attr(attr
);
1367 if (strtobool(buf
, ea
->var
) < 0)
1372 EXPORT_SYMBOL_GPL(device_store_bool
);
1374 ssize_t
device_show_bool(struct device
*dev
, struct device_attribute
*attr
,
1377 struct dev_ext_attribute
*ea
= to_ext_attr(attr
);
1379 return snprintf(buf
, PAGE_SIZE
, "%d\n", *(bool *)(ea
->var
));
1381 EXPORT_SYMBOL_GPL(device_show_bool
);
1384 * device_release - free device structure.
1385 * @kobj: device's kobject.
1387 * This is called once the reference count for the object
1388 * reaches 0. We forward the call to the device's release
1389 * method, which should handle actually freeing the structure.
1391 static void device_release(struct kobject
*kobj
)
1393 struct device
*dev
= kobj_to_dev(kobj
);
1394 struct device_private
*p
= dev
->p
;
1397 * Some platform devices are driven without driver attached
1398 * and managed resources may have been acquired. Make sure
1399 * all resources are released.
1401 * Drivers still can add resources into device after device
1402 * is deleted but alive, so release devres here to avoid
1403 * possible memory leak.
1405 devres_release_all(dev
);
1409 else if (dev
->type
&& dev
->type
->release
)
1410 dev
->type
->release(dev
);
1411 else if (dev
->class && dev
->class->dev_release
)
1412 dev
->class->dev_release(dev
);
1414 WARN(1, KERN_ERR
"Device '%s' does not have a release() function, it is broken and must be fixed. See Documentation/kobject.txt.\n",
1419 static const void *device_namespace(struct kobject
*kobj
)
1421 struct device
*dev
= kobj_to_dev(kobj
);
1422 const void *ns
= NULL
;
1424 if (dev
->class && dev
->class->ns_type
)
1425 ns
= dev
->class->namespace(dev
);
1430 static void device_get_ownership(struct kobject
*kobj
, kuid_t
*uid
, kgid_t
*gid
)
1432 struct device
*dev
= kobj_to_dev(kobj
);
1434 if (dev
->class && dev
->class->get_ownership
)
1435 dev
->class->get_ownership(dev
, uid
, gid
);
1438 static struct kobj_type device_ktype
= {
1439 .release
= device_release
,
1440 .sysfs_ops
= &dev_sysfs_ops
,
1441 .namespace = device_namespace
,
1442 .get_ownership
= device_get_ownership
,
1446 static int dev_uevent_filter(struct kset
*kset
, struct kobject
*kobj
)
1448 struct kobj_type
*ktype
= get_ktype(kobj
);
1450 if (ktype
== &device_ktype
) {
1451 struct device
*dev
= kobj_to_dev(kobj
);
1460 static const char *dev_uevent_name(struct kset
*kset
, struct kobject
*kobj
)
1462 struct device
*dev
= kobj_to_dev(kobj
);
1465 return dev
->bus
->name
;
1467 return dev
->class->name
;
1471 static int dev_uevent(struct kset
*kset
, struct kobject
*kobj
,
1472 struct kobj_uevent_env
*env
)
1474 struct device
*dev
= kobj_to_dev(kobj
);
1477 /* add device node properties if present */
1478 if (MAJOR(dev
->devt
)) {
1482 kuid_t uid
= GLOBAL_ROOT_UID
;
1483 kgid_t gid
= GLOBAL_ROOT_GID
;
1485 add_uevent_var(env
, "MAJOR=%u", MAJOR(dev
->devt
));
1486 add_uevent_var(env
, "MINOR=%u", MINOR(dev
->devt
));
1487 name
= device_get_devnode(dev
, &mode
, &uid
, &gid
, &tmp
);
1489 add_uevent_var(env
, "DEVNAME=%s", name
);
1491 add_uevent_var(env
, "DEVMODE=%#o", mode
& 0777);
1492 if (!uid_eq(uid
, GLOBAL_ROOT_UID
))
1493 add_uevent_var(env
, "DEVUID=%u", from_kuid(&init_user_ns
, uid
));
1494 if (!gid_eq(gid
, GLOBAL_ROOT_GID
))
1495 add_uevent_var(env
, "DEVGID=%u", from_kgid(&init_user_ns
, gid
));
1500 if (dev
->type
&& dev
->type
->name
)
1501 add_uevent_var(env
, "DEVTYPE=%s", dev
->type
->name
);
1504 add_uevent_var(env
, "DRIVER=%s", dev
->driver
->name
);
1506 /* Add common DT information about the device */
1507 of_device_uevent(dev
, env
);
1509 /* have the bus specific function add its stuff */
1510 if (dev
->bus
&& dev
->bus
->uevent
) {
1511 retval
= dev
->bus
->uevent(dev
, env
);
1513 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
1514 dev_name(dev
), __func__
, retval
);
1517 /* have the class specific function add its stuff */
1518 if (dev
->class && dev
->class->dev_uevent
) {
1519 retval
= dev
->class->dev_uevent(dev
, env
);
1521 pr_debug("device: '%s': %s: class uevent() "
1522 "returned %d\n", dev_name(dev
),
1526 /* have the device type specific function add its stuff */
1527 if (dev
->type
&& dev
->type
->uevent
) {
1528 retval
= dev
->type
->uevent(dev
, env
);
1530 pr_debug("device: '%s': %s: dev_type uevent() "
1531 "returned %d\n", dev_name(dev
),
1538 static const struct kset_uevent_ops device_uevent_ops
= {
1539 .filter
= dev_uevent_filter
,
1540 .name
= dev_uevent_name
,
1541 .uevent
= dev_uevent
,
1544 static ssize_t
uevent_show(struct device
*dev
, struct device_attribute
*attr
,
1547 struct kobject
*top_kobj
;
1549 struct kobj_uevent_env
*env
= NULL
;
1554 /* search the kset, the device belongs to */
1555 top_kobj
= &dev
->kobj
;
1556 while (!top_kobj
->kset
&& top_kobj
->parent
)
1557 top_kobj
= top_kobj
->parent
;
1558 if (!top_kobj
->kset
)
1561 kset
= top_kobj
->kset
;
1562 if (!kset
->uevent_ops
|| !kset
->uevent_ops
->uevent
)
1565 /* respect filter */
1566 if (kset
->uevent_ops
&& kset
->uevent_ops
->filter
)
1567 if (!kset
->uevent_ops
->filter(kset
, &dev
->kobj
))
1570 env
= kzalloc(sizeof(struct kobj_uevent_env
), GFP_KERNEL
);
1574 /* let the kset specific function add its keys */
1575 retval
= kset
->uevent_ops
->uevent(kset
, &dev
->kobj
, env
);
1579 /* copy keys to file */
1580 for (i
= 0; i
< env
->envp_idx
; i
++)
1581 count
+= sprintf(&buf
[count
], "%s\n", env
->envp
[i
]);
1587 static ssize_t
uevent_store(struct device
*dev
, struct device_attribute
*attr
,
1588 const char *buf
, size_t count
)
1592 rc
= kobject_synth_uevent(&dev
->kobj
, buf
, count
);
1595 dev_err(dev
, "uevent: failed to send synthetic uevent\n");
1601 static DEVICE_ATTR_RW(uevent
);
1603 static ssize_t
online_show(struct device
*dev
, struct device_attribute
*attr
,
1609 val
= !dev
->offline
;
1611 return sprintf(buf
, "%u\n", val
);
1614 static ssize_t
online_store(struct device
*dev
, struct device_attribute
*attr
,
1615 const char *buf
, size_t count
)
1620 ret
= strtobool(buf
, &val
);
1624 ret
= lock_device_hotplug_sysfs();
1628 ret
= val
? device_online(dev
) : device_offline(dev
);
1629 unlock_device_hotplug();
1630 return ret
< 0 ? ret
: count
;
1632 static DEVICE_ATTR_RW(online
);
1634 int device_add_groups(struct device
*dev
, const struct attribute_group
**groups
)
1636 return sysfs_create_groups(&dev
->kobj
, groups
);
1638 EXPORT_SYMBOL_GPL(device_add_groups
);
1640 void device_remove_groups(struct device
*dev
,
1641 const struct attribute_group
**groups
)
1643 sysfs_remove_groups(&dev
->kobj
, groups
);
1645 EXPORT_SYMBOL_GPL(device_remove_groups
);
1647 union device_attr_group_devres
{
1648 const struct attribute_group
*group
;
1649 const struct attribute_group
**groups
;
1652 static int devm_attr_group_match(struct device
*dev
, void *res
, void *data
)
1654 return ((union device_attr_group_devres
*)res
)->group
== data
;
1657 static void devm_attr_group_remove(struct device
*dev
, void *res
)
1659 union device_attr_group_devres
*devres
= res
;
1660 const struct attribute_group
*group
= devres
->group
;
1662 dev_dbg(dev
, "%s: removing group %p\n", __func__
, group
);
1663 sysfs_remove_group(&dev
->kobj
, group
);
1666 static void devm_attr_groups_remove(struct device
*dev
, void *res
)
1668 union device_attr_group_devres
*devres
= res
;
1669 const struct attribute_group
**groups
= devres
->groups
;
1671 dev_dbg(dev
, "%s: removing groups %p\n", __func__
, groups
);
1672 sysfs_remove_groups(&dev
->kobj
, groups
);
1676 * devm_device_add_group - given a device, create a managed attribute group
1677 * @dev: The device to create the group for
1678 * @grp: The attribute group to create
1680 * This function creates a group for the first time. It will explicitly
1681 * warn and error if any of the attribute files being created already exist.
1683 * Returns 0 on success or error code on failure.
1685 int devm_device_add_group(struct device
*dev
, const struct attribute_group
*grp
)
1687 union device_attr_group_devres
*devres
;
1690 devres
= devres_alloc(devm_attr_group_remove
,
1691 sizeof(*devres
), GFP_KERNEL
);
1695 error
= sysfs_create_group(&dev
->kobj
, grp
);
1697 devres_free(devres
);
1701 devres
->group
= grp
;
1702 devres_add(dev
, devres
);
1705 EXPORT_SYMBOL_GPL(devm_device_add_group
);
1708 * devm_device_remove_group: remove a managed group from a device
1709 * @dev: device to remove the group from
1710 * @grp: group to remove
1712 * This function removes a group of attributes from a device. The attributes
1713 * previously have to have been created for this group, otherwise it will fail.
1715 void devm_device_remove_group(struct device
*dev
,
1716 const struct attribute_group
*grp
)
1718 WARN_ON(devres_release(dev
, devm_attr_group_remove
,
1719 devm_attr_group_match
,
1720 /* cast away const */ (void *)grp
));
1722 EXPORT_SYMBOL_GPL(devm_device_remove_group
);
1725 * devm_device_add_groups - create a bunch of managed attribute groups
1726 * @dev: The device to create the group for
1727 * @groups: The attribute groups to create, NULL terminated
1729 * This function creates a bunch of managed attribute groups. If an error
1730 * occurs when creating a group, all previously created groups will be
1731 * removed, unwinding everything back to the original state when this
1732 * function was called. It will explicitly warn and error if any of the
1733 * attribute files being created already exist.
1735 * Returns 0 on success or error code from sysfs_create_group on failure.
1737 int devm_device_add_groups(struct device
*dev
,
1738 const struct attribute_group
**groups
)
1740 union device_attr_group_devres
*devres
;
1743 devres
= devres_alloc(devm_attr_groups_remove
,
1744 sizeof(*devres
), GFP_KERNEL
);
1748 error
= sysfs_create_groups(&dev
->kobj
, groups
);
1750 devres_free(devres
);
1754 devres
->groups
= groups
;
1755 devres_add(dev
, devres
);
1758 EXPORT_SYMBOL_GPL(devm_device_add_groups
);
1761 * devm_device_remove_groups - remove a list of managed groups
1763 * @dev: The device for the groups to be removed from
1764 * @groups: NULL terminated list of groups to be removed
1766 * If groups is not NULL, remove the specified groups from the device.
1768 void devm_device_remove_groups(struct device
*dev
,
1769 const struct attribute_group
**groups
)
1771 WARN_ON(devres_release(dev
, devm_attr_groups_remove
,
1772 devm_attr_group_match
,
1773 /* cast away const */ (void *)groups
));
1775 EXPORT_SYMBOL_GPL(devm_device_remove_groups
);
1777 static int device_add_attrs(struct device
*dev
)
1779 struct class *class = dev
->class;
1780 const struct device_type
*type
= dev
->type
;
1784 error
= device_add_groups(dev
, class->dev_groups
);
1790 error
= device_add_groups(dev
, type
->groups
);
1792 goto err_remove_class_groups
;
1795 error
= device_add_groups(dev
, dev
->groups
);
1797 goto err_remove_type_groups
;
1799 if (device_supports_offline(dev
) && !dev
->offline_disabled
) {
1800 error
= device_create_file(dev
, &dev_attr_online
);
1802 goto err_remove_dev_groups
;
1807 err_remove_dev_groups
:
1808 device_remove_groups(dev
, dev
->groups
);
1809 err_remove_type_groups
:
1811 device_remove_groups(dev
, type
->groups
);
1812 err_remove_class_groups
:
1814 device_remove_groups(dev
, class->dev_groups
);
1819 static void device_remove_attrs(struct device
*dev
)
1821 struct class *class = dev
->class;
1822 const struct device_type
*type
= dev
->type
;
1824 device_remove_file(dev
, &dev_attr_online
);
1825 device_remove_groups(dev
, dev
->groups
);
1828 device_remove_groups(dev
, type
->groups
);
1831 device_remove_groups(dev
, class->dev_groups
);
1834 static ssize_t
dev_show(struct device
*dev
, struct device_attribute
*attr
,
1837 return print_dev_t(buf
, dev
->devt
);
1839 static DEVICE_ATTR_RO(dev
);
1842 struct kset
*devices_kset
;
1845 * devices_kset_move_before - Move device in the devices_kset's list.
1846 * @deva: Device to move.
1847 * @devb: Device @deva should come before.
1849 static void devices_kset_move_before(struct device
*deva
, struct device
*devb
)
1853 pr_debug("devices_kset: Moving %s before %s\n",
1854 dev_name(deva
), dev_name(devb
));
1855 spin_lock(&devices_kset
->list_lock
);
1856 list_move_tail(&deva
->kobj
.entry
, &devb
->kobj
.entry
);
1857 spin_unlock(&devices_kset
->list_lock
);
1861 * devices_kset_move_after - Move device in the devices_kset's list.
1862 * @deva: Device to move
1863 * @devb: Device @deva should come after.
1865 static void devices_kset_move_after(struct device
*deva
, struct device
*devb
)
1869 pr_debug("devices_kset: Moving %s after %s\n",
1870 dev_name(deva
), dev_name(devb
));
1871 spin_lock(&devices_kset
->list_lock
);
1872 list_move(&deva
->kobj
.entry
, &devb
->kobj
.entry
);
1873 spin_unlock(&devices_kset
->list_lock
);
1877 * devices_kset_move_last - move the device to the end of devices_kset's list.
1878 * @dev: device to move
1880 void devices_kset_move_last(struct device
*dev
)
1884 pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev
));
1885 spin_lock(&devices_kset
->list_lock
);
1886 list_move_tail(&dev
->kobj
.entry
, &devices_kset
->list
);
1887 spin_unlock(&devices_kset
->list_lock
);
1891 * device_create_file - create sysfs attribute file for device.
1893 * @attr: device attribute descriptor.
1895 int device_create_file(struct device
*dev
,
1896 const struct device_attribute
*attr
)
1901 WARN(((attr
->attr
.mode
& S_IWUGO
) && !attr
->store
),
1902 "Attribute %s: write permission without 'store'\n",
1904 WARN(((attr
->attr
.mode
& S_IRUGO
) && !attr
->show
),
1905 "Attribute %s: read permission without 'show'\n",
1907 error
= sysfs_create_file(&dev
->kobj
, &attr
->attr
);
1912 EXPORT_SYMBOL_GPL(device_create_file
);
1915 * device_remove_file - remove sysfs attribute file.
1917 * @attr: device attribute descriptor.
1919 void device_remove_file(struct device
*dev
,
1920 const struct device_attribute
*attr
)
1923 sysfs_remove_file(&dev
->kobj
, &attr
->attr
);
1925 EXPORT_SYMBOL_GPL(device_remove_file
);
1928 * device_remove_file_self - remove sysfs attribute file from its own method.
1930 * @attr: device attribute descriptor.
1932 * See kernfs_remove_self() for details.
1934 bool device_remove_file_self(struct device
*dev
,
1935 const struct device_attribute
*attr
)
1938 return sysfs_remove_file_self(&dev
->kobj
, &attr
->attr
);
1942 EXPORT_SYMBOL_GPL(device_remove_file_self
);
1945 * device_create_bin_file - create sysfs binary attribute file for device.
1947 * @attr: device binary attribute descriptor.
1949 int device_create_bin_file(struct device
*dev
,
1950 const struct bin_attribute
*attr
)
1952 int error
= -EINVAL
;
1954 error
= sysfs_create_bin_file(&dev
->kobj
, attr
);
1957 EXPORT_SYMBOL_GPL(device_create_bin_file
);
1960 * device_remove_bin_file - remove sysfs binary attribute file
1962 * @attr: device binary attribute descriptor.
1964 void device_remove_bin_file(struct device
*dev
,
1965 const struct bin_attribute
*attr
)
1968 sysfs_remove_bin_file(&dev
->kobj
, attr
);
1970 EXPORT_SYMBOL_GPL(device_remove_bin_file
);
1972 static void klist_children_get(struct klist_node
*n
)
1974 struct device_private
*p
= to_device_private_parent(n
);
1975 struct device
*dev
= p
->device
;
1980 static void klist_children_put(struct klist_node
*n
)
1982 struct device_private
*p
= to_device_private_parent(n
);
1983 struct device
*dev
= p
->device
;
1989 * device_initialize - init device structure.
1992 * This prepares the device for use by other layers by initializing
1994 * It is the first half of device_register(), if called by
1995 * that function, though it can also be called separately, so one
1996 * may use @dev's fields. In particular, get_device()/put_device()
1997 * may be used for reference counting of @dev after calling this
2000 * All fields in @dev must be initialized by the caller to 0, except
2001 * for those explicitly set to some other value. The simplest
2002 * approach is to use kzalloc() to allocate the structure containing
2005 * NOTE: Use put_device() to give up your reference instead of freeing
2006 * @dev directly once you have called this function.
2008 void device_initialize(struct device
*dev
)
2010 dev
->kobj
.kset
= devices_kset
;
2011 kobject_init(&dev
->kobj
, &device_ktype
);
2012 INIT_LIST_HEAD(&dev
->dma_pools
);
2013 mutex_init(&dev
->mutex
);
2014 #ifdef CONFIG_PROVE_LOCKING
2015 mutex_init(&dev
->lockdep_mutex
);
2017 lockdep_set_novalidate_class(&dev
->mutex
);
2018 spin_lock_init(&dev
->devres_lock
);
2019 INIT_LIST_HEAD(&dev
->devres_head
);
2020 device_pm_init(dev
);
2021 set_dev_node(dev
, -1);
2022 #ifdef CONFIG_GENERIC_MSI_IRQ
2023 INIT_LIST_HEAD(&dev
->msi_list
);
2025 INIT_LIST_HEAD(&dev
->links
.consumers
);
2026 INIT_LIST_HEAD(&dev
->links
.suppliers
);
2027 INIT_LIST_HEAD(&dev
->links
.needs_suppliers
);
2028 INIT_LIST_HEAD(&dev
->links
.defer_sync
);
2029 dev
->links
.status
= DL_DEV_NO_DRIVER
;
2031 EXPORT_SYMBOL_GPL(device_initialize
);
2033 struct kobject
*virtual_device_parent(struct device
*dev
)
2035 static struct kobject
*virtual_dir
= NULL
;
2038 virtual_dir
= kobject_create_and_add("virtual",
2039 &devices_kset
->kobj
);
2045 struct kobject kobj
;
2046 struct class *class;
2049 #define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
2051 static void class_dir_release(struct kobject
*kobj
)
2053 struct class_dir
*dir
= to_class_dir(kobj
);
2058 struct kobj_ns_type_operations
*class_dir_child_ns_type(struct kobject
*kobj
)
2060 struct class_dir
*dir
= to_class_dir(kobj
);
2061 return dir
->class->ns_type
;
2064 static struct kobj_type class_dir_ktype
= {
2065 .release
= class_dir_release
,
2066 .sysfs_ops
= &kobj_sysfs_ops
,
2067 .child_ns_type
= class_dir_child_ns_type
2070 static struct kobject
*
2071 class_dir_create_and_add(struct class *class, struct kobject
*parent_kobj
)
2073 struct class_dir
*dir
;
2076 dir
= kzalloc(sizeof(*dir
), GFP_KERNEL
);
2078 return ERR_PTR(-ENOMEM
);
2081 kobject_init(&dir
->kobj
, &class_dir_ktype
);
2083 dir
->kobj
.kset
= &class->p
->glue_dirs
;
2085 retval
= kobject_add(&dir
->kobj
, parent_kobj
, "%s", class->name
);
2087 kobject_put(&dir
->kobj
);
2088 return ERR_PTR(retval
);
2093 static DEFINE_MUTEX(gdp_mutex
);
2095 static struct kobject
*get_device_parent(struct device
*dev
,
2096 struct device
*parent
)
2099 struct kobject
*kobj
= NULL
;
2100 struct kobject
*parent_kobj
;
2104 /* block disks show up in /sys/block */
2105 if (sysfs_deprecated
&& dev
->class == &block_class
) {
2106 if (parent
&& parent
->class == &block_class
)
2107 return &parent
->kobj
;
2108 return &block_class
.p
->subsys
.kobj
;
2113 * If we have no parent, we live in "virtual".
2114 * Class-devices with a non class-device as parent, live
2115 * in a "glue" directory to prevent namespace collisions.
2118 parent_kobj
= virtual_device_parent(dev
);
2119 else if (parent
->class && !dev
->class->ns_type
)
2120 return &parent
->kobj
;
2122 parent_kobj
= &parent
->kobj
;
2124 mutex_lock(&gdp_mutex
);
2126 /* find our class-directory at the parent and reference it */
2127 spin_lock(&dev
->class->p
->glue_dirs
.list_lock
);
2128 list_for_each_entry(k
, &dev
->class->p
->glue_dirs
.list
, entry
)
2129 if (k
->parent
== parent_kobj
) {
2130 kobj
= kobject_get(k
);
2133 spin_unlock(&dev
->class->p
->glue_dirs
.list_lock
);
2135 mutex_unlock(&gdp_mutex
);
2139 /* or create a new class-directory at the parent device */
2140 k
= class_dir_create_and_add(dev
->class, parent_kobj
);
2141 /* do not emit an uevent for this simple "glue" directory */
2142 mutex_unlock(&gdp_mutex
);
2146 /* subsystems can specify a default root directory for their devices */
2147 if (!parent
&& dev
->bus
&& dev
->bus
->dev_root
)
2148 return &dev
->bus
->dev_root
->kobj
;
2151 return &parent
->kobj
;
2155 static inline bool live_in_glue_dir(struct kobject
*kobj
,
2158 if (!kobj
|| !dev
->class ||
2159 kobj
->kset
!= &dev
->class->p
->glue_dirs
)
2164 static inline struct kobject
*get_glue_dir(struct device
*dev
)
2166 return dev
->kobj
.parent
;
2170 * make sure cleaning up dir as the last step, we need to make
2171 * sure .release handler of kobject is run with holding the
2174 static void cleanup_glue_dir(struct device
*dev
, struct kobject
*glue_dir
)
2178 /* see if we live in a "glue" directory */
2179 if (!live_in_glue_dir(glue_dir
, dev
))
2182 mutex_lock(&gdp_mutex
);
2184 * There is a race condition between removing glue directory
2185 * and adding a new device under the glue directory.
2190 * get_device_parent()
2191 * class_dir_create_and_add()
2192 * kobject_add_internal()
2193 * create_dir() // create glue_dir
2196 * get_device_parent()
2197 * kobject_get() // get glue_dir
2200 * cleanup_glue_dir()
2201 * kobject_del(glue_dir)
2204 * kobject_add_internal()
2205 * create_dir() // in glue_dir
2206 * sysfs_create_dir_ns()
2207 * kernfs_create_dir_ns(sd)
2209 * sysfs_remove_dir() // glue_dir->sd=NULL
2210 * sysfs_put() // free glue_dir->sd
2213 * kernfs_new_node(sd)
2214 * kernfs_get(glue_dir)
2218 * Before CPU1 remove last child device under glue dir, if CPU2 add
2219 * a new device under glue dir, the glue_dir kobject reference count
2220 * will be increase to 2 in kobject_get(k). And CPU2 has been called
2221 * kernfs_create_dir_ns(). Meanwhile, CPU1 call sysfs_remove_dir()
2222 * and sysfs_put(). This result in glue_dir->sd is freed.
2224 * Then the CPU2 will see a stale "empty" but still potentially used
2225 * glue dir around in kernfs_new_node().
2227 * In order to avoid this happening, we also should make sure that
2228 * kernfs_node for glue_dir is released in CPU1 only when refcount
2229 * for glue_dir kobj is 1.
2231 ref
= kref_read(&glue_dir
->kref
);
2232 if (!kobject_has_children(glue_dir
) && !--ref
)
2233 kobject_del(glue_dir
);
2234 kobject_put(glue_dir
);
2235 mutex_unlock(&gdp_mutex
);
2238 static int device_add_class_symlinks(struct device
*dev
)
2240 struct device_node
*of_node
= dev_of_node(dev
);
2244 error
= sysfs_create_link(&dev
->kobj
, of_node_kobj(of_node
), "of_node");
2246 dev_warn(dev
, "Error %d creating of_node link\n",error
);
2247 /* An error here doesn't warrant bringing down the device */
2253 error
= sysfs_create_link(&dev
->kobj
,
2254 &dev
->class->p
->subsys
.kobj
,
2259 if (dev
->parent
&& device_is_not_partition(dev
)) {
2260 error
= sysfs_create_link(&dev
->kobj
, &dev
->parent
->kobj
,
2267 /* /sys/block has directories and does not need symlinks */
2268 if (sysfs_deprecated
&& dev
->class == &block_class
)
2272 /* link in the class directory pointing to the device */
2273 error
= sysfs_create_link(&dev
->class->p
->subsys
.kobj
,
2274 &dev
->kobj
, dev_name(dev
));
2281 sysfs_remove_link(&dev
->kobj
, "device");
2284 sysfs_remove_link(&dev
->kobj
, "subsystem");
2286 sysfs_remove_link(&dev
->kobj
, "of_node");
2290 static void device_remove_class_symlinks(struct device
*dev
)
2292 if (dev_of_node(dev
))
2293 sysfs_remove_link(&dev
->kobj
, "of_node");
2298 if (dev
->parent
&& device_is_not_partition(dev
))
2299 sysfs_remove_link(&dev
->kobj
, "device");
2300 sysfs_remove_link(&dev
->kobj
, "subsystem");
2302 if (sysfs_deprecated
&& dev
->class == &block_class
)
2305 sysfs_delete_link(&dev
->class->p
->subsys
.kobj
, &dev
->kobj
, dev_name(dev
));
2309 * dev_set_name - set a device name
2311 * @fmt: format string for the device's name
2313 int dev_set_name(struct device
*dev
, const char *fmt
, ...)
2318 va_start(vargs
, fmt
);
2319 err
= kobject_set_name_vargs(&dev
->kobj
, fmt
, vargs
);
2323 EXPORT_SYMBOL_GPL(dev_set_name
);
2326 * device_to_dev_kobj - select a /sys/dev/ directory for the device
2329 * By default we select char/ for new entries. Setting class->dev_obj
2330 * to NULL prevents an entry from being created. class->dev_kobj must
2331 * be set (or cleared) before any devices are registered to the class
2332 * otherwise device_create_sys_dev_entry() and
2333 * device_remove_sys_dev_entry() will disagree about the presence of
2336 static struct kobject
*device_to_dev_kobj(struct device
*dev
)
2338 struct kobject
*kobj
;
2341 kobj
= dev
->class->dev_kobj
;
2343 kobj
= sysfs_dev_char_kobj
;
2348 static int device_create_sys_dev_entry(struct device
*dev
)
2350 struct kobject
*kobj
= device_to_dev_kobj(dev
);
2355 format_dev_t(devt_str
, dev
->devt
);
2356 error
= sysfs_create_link(kobj
, &dev
->kobj
, devt_str
);
2362 static void device_remove_sys_dev_entry(struct device
*dev
)
2364 struct kobject
*kobj
= device_to_dev_kobj(dev
);
2368 format_dev_t(devt_str
, dev
->devt
);
2369 sysfs_remove_link(kobj
, devt_str
);
2373 static int device_private_init(struct device
*dev
)
2375 dev
->p
= kzalloc(sizeof(*dev
->p
), GFP_KERNEL
);
2378 dev
->p
->device
= dev
;
2379 klist_init(&dev
->p
->klist_children
, klist_children_get
,
2380 klist_children_put
);
2381 INIT_LIST_HEAD(&dev
->p
->deferred_probe
);
2385 static u32 fw_devlink_flags
;
2386 static int __init
fw_devlink_setup(char *arg
)
2391 if (strcmp(arg
, "off") == 0) {
2392 fw_devlink_flags
= 0;
2393 } else if (strcmp(arg
, "permissive") == 0) {
2394 fw_devlink_flags
= DL_FLAG_SYNC_STATE_ONLY
;
2395 } else if (strcmp(arg
, "on") == 0) {
2396 fw_devlink_flags
= DL_FLAG_AUTOPROBE_CONSUMER
;
2397 } else if (strcmp(arg
, "rpm") == 0) {
2398 fw_devlink_flags
= DL_FLAG_AUTOPROBE_CONSUMER
|
2403 early_param("fw_devlink", fw_devlink_setup
);
2405 u32
fw_devlink_get_flags(void)
2407 return fw_devlink_flags
;
2410 static bool fw_devlink_is_permissive(void)
2412 return fw_devlink_flags
== DL_FLAG_SYNC_STATE_ONLY
;
2416 * device_add - add device to device hierarchy.
2419 * This is part 2 of device_register(), though may be called
2420 * separately _iff_ device_initialize() has been called separately.
2422 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
2423 * to the global and sibling lists for the device, then
2424 * adds it to the other relevant subsystems of the driver model.
2426 * Do not call this routine or device_register() more than once for
2427 * any device structure. The driver model core is not designed to work
2428 * with devices that get unregistered and then spring back to life.
2429 * (Among other things, it's very hard to guarantee that all references
2430 * to the previous incarnation of @dev have been dropped.) Allocate
2431 * and register a fresh new struct device instead.
2433 * NOTE: _Never_ directly free @dev after calling this function, even
2434 * if it returned an error! Always use put_device() to give up your
2435 * reference instead.
2437 * Rule of thumb is: if device_add() succeeds, you should call
2438 * device_del() when you want to get rid of it. If device_add() has
2439 * *not* succeeded, use *only* put_device() to drop the reference
2442 int device_add(struct device
*dev
)
2444 struct device
*parent
;
2445 struct kobject
*kobj
;
2446 struct class_interface
*class_intf
;
2447 int error
= -EINVAL
, fw_ret
;
2448 struct kobject
*glue_dir
= NULL
;
2449 bool is_fwnode_dev
= false;
2451 dev
= get_device(dev
);
2456 error
= device_private_init(dev
);
2462 * for statically allocated devices, which should all be converted
2463 * some day, we need to initialize the name. We prevent reading back
2464 * the name, and force the use of dev_name()
2466 if (dev
->init_name
) {
2467 dev_set_name(dev
, "%s", dev
->init_name
);
2468 dev
->init_name
= NULL
;
2471 /* subsystems can specify simple device enumeration */
2472 if (!dev_name(dev
) && dev
->bus
&& dev
->bus
->dev_name
)
2473 dev_set_name(dev
, "%s%u", dev
->bus
->dev_name
, dev
->id
);
2475 if (!dev_name(dev
)) {
2480 pr_debug("device: '%s': %s\n", dev_name(dev
), __func__
);
2482 parent
= get_device(dev
->parent
);
2483 kobj
= get_device_parent(dev
, parent
);
2485 error
= PTR_ERR(kobj
);
2489 dev
->kobj
.parent
= kobj
;
2491 /* use parent numa_node */
2492 if (parent
&& (dev_to_node(dev
) == NUMA_NO_NODE
))
2493 set_dev_node(dev
, dev_to_node(parent
));
2495 /* first, register with generic layer. */
2496 /* we require the name to be set before, and pass NULL */
2497 error
= kobject_add(&dev
->kobj
, dev
->kobj
.parent
, NULL
);
2499 glue_dir
= get_glue_dir(dev
);
2503 /* notify platform of device entry */
2504 error
= device_platform_notify(dev
, KOBJ_ADD
);
2506 goto platform_error
;
2508 error
= device_create_file(dev
, &dev_attr_uevent
);
2512 error
= device_add_class_symlinks(dev
);
2515 error
= device_add_attrs(dev
);
2518 error
= bus_add_device(dev
);
2521 error
= dpm_sysfs_add(dev
);
2526 if (MAJOR(dev
->devt
)) {
2527 error
= device_create_file(dev
, &dev_attr_dev
);
2531 error
= device_create_sys_dev_entry(dev
);
2535 devtmpfs_create_node(dev
);
2538 /* Notify clients of device addition. This call must come
2539 * after dpm_sysfs_add() and before kobject_uevent().
2542 blocking_notifier_call_chain(&dev
->bus
->p
->bus_notifier
,
2543 BUS_NOTIFY_ADD_DEVICE
, dev
);
2545 kobject_uevent(&dev
->kobj
, KOBJ_ADD
);
2547 if (dev
->fwnode
&& !dev
->fwnode
->dev
) {
2548 dev
->fwnode
->dev
= dev
;
2549 is_fwnode_dev
= true;
2553 * Check if any of the other devices (consumers) have been waiting for
2554 * this device (supplier) to be added so that they can create a device
2557 * This needs to happen after device_pm_add() because device_link_add()
2558 * requires the supplier be registered before it's called.
2560 * But this also needs to happe before bus_probe_device() to make sure
2561 * waiting consumers can link to it before the driver is bound to the
2562 * device and the driver sync_state callback is called for this device.
2564 device_link_add_missing_supplier_links();
2566 if (fw_devlink_flags
&& is_fwnode_dev
&&
2567 fwnode_has_op(dev
->fwnode
, add_links
)) {
2568 fw_ret
= fwnode_call_int_op(dev
->fwnode
, add_links
, dev
);
2569 if (fw_ret
== -ENODEV
&& !fw_devlink_is_permissive())
2570 device_link_wait_for_mandatory_supplier(dev
);
2572 device_link_wait_for_optional_supplier(dev
);
2575 bus_probe_device(dev
);
2577 klist_add_tail(&dev
->p
->knode_parent
,
2578 &parent
->p
->klist_children
);
2581 mutex_lock(&dev
->class->p
->mutex
);
2582 /* tie the class to the device */
2583 klist_add_tail(&dev
->p
->knode_class
,
2584 &dev
->class->p
->klist_devices
);
2586 /* notify any interfaces that the device is here */
2587 list_for_each_entry(class_intf
,
2588 &dev
->class->p
->interfaces
, node
)
2589 if (class_intf
->add_dev
)
2590 class_intf
->add_dev(dev
, class_intf
);
2591 mutex_unlock(&dev
->class->p
->mutex
);
2597 if (MAJOR(dev
->devt
))
2598 device_remove_file(dev
, &dev_attr_dev
);
2600 device_pm_remove(dev
);
2601 dpm_sysfs_remove(dev
);
2603 bus_remove_device(dev
);
2605 device_remove_attrs(dev
);
2607 device_remove_class_symlinks(dev
);
2609 device_remove_file(dev
, &dev_attr_uevent
);
2611 device_platform_notify(dev
, KOBJ_REMOVE
);
2613 kobject_uevent(&dev
->kobj
, KOBJ_REMOVE
);
2614 glue_dir
= get_glue_dir(dev
);
2615 kobject_del(&dev
->kobj
);
2617 cleanup_glue_dir(dev
, glue_dir
);
2625 EXPORT_SYMBOL_GPL(device_add
);
2628 * device_register - register a device with the system.
2629 * @dev: pointer to the device structure
2631 * This happens in two clean steps - initialize the device
2632 * and add it to the system. The two steps can be called
2633 * separately, but this is the easiest and most common.
2634 * I.e. you should only call the two helpers separately if
2635 * have a clearly defined need to use and refcount the device
2636 * before it is added to the hierarchy.
2638 * For more information, see the kerneldoc for device_initialize()
2641 * NOTE: _Never_ directly free @dev after calling this function, even
2642 * if it returned an error! Always use put_device() to give up the
2643 * reference initialized in this function instead.
2645 int device_register(struct device
*dev
)
2647 device_initialize(dev
);
2648 return device_add(dev
);
2650 EXPORT_SYMBOL_GPL(device_register
);
2653 * get_device - increment reference count for device.
2656 * This simply forwards the call to kobject_get(), though
2657 * we do take care to provide for the case that we get a NULL
2658 * pointer passed in.
2660 struct device
*get_device(struct device
*dev
)
2662 return dev
? kobj_to_dev(kobject_get(&dev
->kobj
)) : NULL
;
2664 EXPORT_SYMBOL_GPL(get_device
);
2667 * put_device - decrement reference count.
2668 * @dev: device in question.
2670 void put_device(struct device
*dev
)
2672 /* might_sleep(); */
2674 kobject_put(&dev
->kobj
);
2676 EXPORT_SYMBOL_GPL(put_device
);
2678 bool kill_device(struct device
*dev
)
2681 * Require the device lock and set the "dead" flag to guarantee that
2682 * the update behavior is consistent with the other bitfields near
2683 * it and that we cannot have an asynchronous probe routine trying
2684 * to run while we are tearing out the bus/class/sysfs from
2685 * underneath the device.
2687 lockdep_assert_held(&dev
->mutex
);
2691 dev
->p
->dead
= true;
2694 EXPORT_SYMBOL_GPL(kill_device
);
2697 * device_del - delete device from system.
2700 * This is the first part of the device unregistration
2701 * sequence. This removes the device from the lists we control
2702 * from here, has it removed from the other driver model
2703 * subsystems it was added to in device_add(), and removes it
2704 * from the kobject hierarchy.
2706 * NOTE: this should be called manually _iff_ device_add() was
2707 * also called manually.
2709 void device_del(struct device
*dev
)
2711 struct device
*parent
= dev
->parent
;
2712 struct kobject
*glue_dir
= NULL
;
2713 struct class_interface
*class_intf
;
2719 if (dev
->fwnode
&& dev
->fwnode
->dev
== dev
)
2720 dev
->fwnode
->dev
= NULL
;
2722 /* Notify clients of device removal. This call must come
2723 * before dpm_sysfs_remove().
2726 blocking_notifier_call_chain(&dev
->bus
->p
->bus_notifier
,
2727 BUS_NOTIFY_DEL_DEVICE
, dev
);
2729 dpm_sysfs_remove(dev
);
2731 klist_del(&dev
->p
->knode_parent
);
2732 if (MAJOR(dev
->devt
)) {
2733 devtmpfs_delete_node(dev
);
2734 device_remove_sys_dev_entry(dev
);
2735 device_remove_file(dev
, &dev_attr_dev
);
2738 device_remove_class_symlinks(dev
);
2740 mutex_lock(&dev
->class->p
->mutex
);
2741 /* notify any interfaces that the device is now gone */
2742 list_for_each_entry(class_intf
,
2743 &dev
->class->p
->interfaces
, node
)
2744 if (class_intf
->remove_dev
)
2745 class_intf
->remove_dev(dev
, class_intf
);
2746 /* remove the device from the class list */
2747 klist_del(&dev
->p
->knode_class
);
2748 mutex_unlock(&dev
->class->p
->mutex
);
2750 device_remove_file(dev
, &dev_attr_uevent
);
2751 device_remove_attrs(dev
);
2752 bus_remove_device(dev
);
2753 device_pm_remove(dev
);
2754 driver_deferred_probe_del(dev
);
2755 device_platform_notify(dev
, KOBJ_REMOVE
);
2756 device_remove_properties(dev
);
2757 device_links_purge(dev
);
2760 blocking_notifier_call_chain(&dev
->bus
->p
->bus_notifier
,
2761 BUS_NOTIFY_REMOVED_DEVICE
, dev
);
2762 kobject_uevent(&dev
->kobj
, KOBJ_REMOVE
);
2763 glue_dir
= get_glue_dir(dev
);
2764 kobject_del(&dev
->kobj
);
2765 cleanup_glue_dir(dev
, glue_dir
);
2768 EXPORT_SYMBOL_GPL(device_del
);
2771 * device_unregister - unregister device from system.
2772 * @dev: device going away.
2774 * We do this in two parts, like we do device_register(). First,
2775 * we remove it from all the subsystems with device_del(), then
2776 * we decrement the reference count via put_device(). If that
2777 * is the final reference count, the device will be cleaned up
2778 * via device_release() above. Otherwise, the structure will
2779 * stick around until the final reference to the device is dropped.
2781 void device_unregister(struct device
*dev
)
2783 pr_debug("device: '%s': %s\n", dev_name(dev
), __func__
);
2787 EXPORT_SYMBOL_GPL(device_unregister
);
2789 static struct device
*prev_device(struct klist_iter
*i
)
2791 struct klist_node
*n
= klist_prev(i
);
2792 struct device
*dev
= NULL
;
2793 struct device_private
*p
;
2796 p
= to_device_private_parent(n
);
2802 static struct device
*next_device(struct klist_iter
*i
)
2804 struct klist_node
*n
= klist_next(i
);
2805 struct device
*dev
= NULL
;
2806 struct device_private
*p
;
2809 p
= to_device_private_parent(n
);
2816 * device_get_devnode - path of device node file
2818 * @mode: returned file access mode
2819 * @uid: returned file owner
2820 * @gid: returned file group
2821 * @tmp: possibly allocated string
2823 * Return the relative path of a possible device node.
2824 * Non-default names may need to allocate a memory to compose
2825 * a name. This memory is returned in tmp and needs to be
2826 * freed by the caller.
2828 const char *device_get_devnode(struct device
*dev
,
2829 umode_t
*mode
, kuid_t
*uid
, kgid_t
*gid
,
2836 /* the device type may provide a specific name */
2837 if (dev
->type
&& dev
->type
->devnode
)
2838 *tmp
= dev
->type
->devnode(dev
, mode
, uid
, gid
);
2842 /* the class may provide a specific name */
2843 if (dev
->class && dev
->class->devnode
)
2844 *tmp
= dev
->class->devnode(dev
, mode
);
2848 /* return name without allocation, tmp == NULL */
2849 if (strchr(dev_name(dev
), '!') == NULL
)
2850 return dev_name(dev
);
2852 /* replace '!' in the name with '/' */
2853 s
= kstrdup(dev_name(dev
), GFP_KERNEL
);
2856 strreplace(s
, '!', '/');
2861 * device_for_each_child - device child iterator.
2862 * @parent: parent struct device.
2863 * @fn: function to be called for each device.
2864 * @data: data for the callback.
2866 * Iterate over @parent's child devices, and call @fn for each,
2869 * We check the return of @fn each time. If it returns anything
2870 * other than 0, we break out and return that value.
2872 int device_for_each_child(struct device
*parent
, void *data
,
2873 int (*fn
)(struct device
*dev
, void *data
))
2875 struct klist_iter i
;
2876 struct device
*child
;
2882 klist_iter_init(&parent
->p
->klist_children
, &i
);
2883 while (!error
&& (child
= next_device(&i
)))
2884 error
= fn(child
, data
);
2885 klist_iter_exit(&i
);
2888 EXPORT_SYMBOL_GPL(device_for_each_child
);
2891 * device_for_each_child_reverse - device child iterator in reversed order.
2892 * @parent: parent struct device.
2893 * @fn: function to be called for each device.
2894 * @data: data for the callback.
2896 * Iterate over @parent's child devices, and call @fn for each,
2899 * We check the return of @fn each time. If it returns anything
2900 * other than 0, we break out and return that value.
2902 int device_for_each_child_reverse(struct device
*parent
, void *data
,
2903 int (*fn
)(struct device
*dev
, void *data
))
2905 struct klist_iter i
;
2906 struct device
*child
;
2912 klist_iter_init(&parent
->p
->klist_children
, &i
);
2913 while ((child
= prev_device(&i
)) && !error
)
2914 error
= fn(child
, data
);
2915 klist_iter_exit(&i
);
2918 EXPORT_SYMBOL_GPL(device_for_each_child_reverse
);
2921 * device_find_child - device iterator for locating a particular device.
2922 * @parent: parent struct device
2923 * @match: Callback function to check device
2924 * @data: Data to pass to match function
2926 * This is similar to the device_for_each_child() function above, but it
2927 * returns a reference to a device that is 'found' for later use, as
2928 * determined by the @match callback.
2930 * The callback should return 0 if the device doesn't match and non-zero
2931 * if it does. If the callback returns non-zero and a reference to the
2932 * current device can be obtained, this function will return to the caller
2933 * and not iterate over any more devices.
2935 * NOTE: you will need to drop the reference with put_device() after use.
2937 struct device
*device_find_child(struct device
*parent
, void *data
,
2938 int (*match
)(struct device
*dev
, void *data
))
2940 struct klist_iter i
;
2941 struct device
*child
;
2946 klist_iter_init(&parent
->p
->klist_children
, &i
);
2947 while ((child
= next_device(&i
)))
2948 if (match(child
, data
) && get_device(child
))
2950 klist_iter_exit(&i
);
2953 EXPORT_SYMBOL_GPL(device_find_child
);
2956 * device_find_child_by_name - device iterator for locating a child device.
2957 * @parent: parent struct device
2958 * @name: name of the child device
2960 * This is similar to the device_find_child() function above, but it
2961 * returns a reference to a device that has the name @name.
2963 * NOTE: you will need to drop the reference with put_device() after use.
2965 struct device
*device_find_child_by_name(struct device
*parent
,
2968 struct klist_iter i
;
2969 struct device
*child
;
2974 klist_iter_init(&parent
->p
->klist_children
, &i
);
2975 while ((child
= next_device(&i
)))
2976 if (!strcmp(dev_name(child
), name
) && get_device(child
))
2978 klist_iter_exit(&i
);
2981 EXPORT_SYMBOL_GPL(device_find_child_by_name
);
2983 int __init
devices_init(void)
2985 devices_kset
= kset_create_and_add("devices", &device_uevent_ops
, NULL
);
2988 dev_kobj
= kobject_create_and_add("dev", NULL
);
2991 sysfs_dev_block_kobj
= kobject_create_and_add("block", dev_kobj
);
2992 if (!sysfs_dev_block_kobj
)
2993 goto block_kobj_err
;
2994 sysfs_dev_char_kobj
= kobject_create_and_add("char", dev_kobj
);
2995 if (!sysfs_dev_char_kobj
)
3001 kobject_put(sysfs_dev_block_kobj
);
3003 kobject_put(dev_kobj
);
3005 kset_unregister(devices_kset
);
3009 static int device_check_offline(struct device
*dev
, void *not_used
)
3013 ret
= device_for_each_child(dev
, NULL
, device_check_offline
);
3017 return device_supports_offline(dev
) && !dev
->offline
? -EBUSY
: 0;
3021 * device_offline - Prepare the device for hot-removal.
3022 * @dev: Device to be put offline.
3024 * Execute the device bus type's .offline() callback, if present, to prepare
3025 * the device for a subsequent hot-removal. If that succeeds, the device must
3026 * not be used until either it is removed or its bus type's .online() callback
3029 * Call under device_hotplug_lock.
3031 int device_offline(struct device
*dev
)
3035 if (dev
->offline_disabled
)
3038 ret
= device_for_each_child(dev
, NULL
, device_check_offline
);
3043 if (device_supports_offline(dev
)) {
3047 ret
= dev
->bus
->offline(dev
);
3049 kobject_uevent(&dev
->kobj
, KOBJ_OFFLINE
);
3050 dev
->offline
= true;
3060 * device_online - Put the device back online after successful device_offline().
3061 * @dev: Device to be put back online.
3063 * If device_offline() has been successfully executed for @dev, but the device
3064 * has not been removed subsequently, execute its bus type's .online() callback
3065 * to indicate that the device can be used again.
3067 * Call under device_hotplug_lock.
3069 int device_online(struct device
*dev
)
3074 if (device_supports_offline(dev
)) {
3076 ret
= dev
->bus
->online(dev
);
3078 kobject_uevent(&dev
->kobj
, KOBJ_ONLINE
);
3079 dev
->offline
= false;
3090 struct root_device
{
3092 struct module
*owner
;
3095 static inline struct root_device
*to_root_device(struct device
*d
)
3097 return container_of(d
, struct root_device
, dev
);
3100 static void root_device_release(struct device
*dev
)
3102 kfree(to_root_device(dev
));
3106 * __root_device_register - allocate and register a root device
3107 * @name: root device name
3108 * @owner: owner module of the root device, usually THIS_MODULE
3110 * This function allocates a root device and registers it
3111 * using device_register(). In order to free the returned
3112 * device, use root_device_unregister().
3114 * Root devices are dummy devices which allow other devices
3115 * to be grouped under /sys/devices. Use this function to
3116 * allocate a root device and then use it as the parent of
3117 * any device which should appear under /sys/devices/{name}
3119 * The /sys/devices/{name} directory will also contain a
3120 * 'module' symlink which points to the @owner directory
3123 * Returns &struct device pointer on success, or ERR_PTR() on error.
3125 * Note: You probably want to use root_device_register().
3127 struct device
*__root_device_register(const char *name
, struct module
*owner
)
3129 struct root_device
*root
;
3132 root
= kzalloc(sizeof(struct root_device
), GFP_KERNEL
);
3134 return ERR_PTR(err
);
3136 err
= dev_set_name(&root
->dev
, "%s", name
);
3139 return ERR_PTR(err
);
3142 root
->dev
.release
= root_device_release
;
3144 err
= device_register(&root
->dev
);
3146 put_device(&root
->dev
);
3147 return ERR_PTR(err
);
3150 #ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
3152 struct module_kobject
*mk
= &owner
->mkobj
;
3154 err
= sysfs_create_link(&root
->dev
.kobj
, &mk
->kobj
, "module");
3156 device_unregister(&root
->dev
);
3157 return ERR_PTR(err
);
3159 root
->owner
= owner
;
3165 EXPORT_SYMBOL_GPL(__root_device_register
);
3168 * root_device_unregister - unregister and free a root device
3169 * @dev: device going away
3171 * This function unregisters and cleans up a device that was created by
3172 * root_device_register().
3174 void root_device_unregister(struct device
*dev
)
3176 struct root_device
*root
= to_root_device(dev
);
3179 sysfs_remove_link(&root
->dev
.kobj
, "module");
3181 device_unregister(dev
);
3183 EXPORT_SYMBOL_GPL(root_device_unregister
);
3186 static void device_create_release(struct device
*dev
)
3188 pr_debug("device: '%s': %s\n", dev_name(dev
), __func__
);
3192 static __printf(6, 0) struct device
*
3193 device_create_groups_vargs(struct class *class, struct device
*parent
,
3194 dev_t devt
, void *drvdata
,
3195 const struct attribute_group
**groups
,
3196 const char *fmt
, va_list args
)
3198 struct device
*dev
= NULL
;
3199 int retval
= -ENODEV
;
3201 if (class == NULL
|| IS_ERR(class))
3204 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
3210 device_initialize(dev
);
3213 dev
->parent
= parent
;
3214 dev
->groups
= groups
;
3215 dev
->release
= device_create_release
;
3216 dev_set_drvdata(dev
, drvdata
);
3218 retval
= kobject_set_name_vargs(&dev
->kobj
, fmt
, args
);
3222 retval
= device_add(dev
);
3230 return ERR_PTR(retval
);
3234 * device_create_vargs - creates a device and registers it with sysfs
3235 * @class: pointer to the struct class that this device should be registered to
3236 * @parent: pointer to the parent struct device of this new device, if any
3237 * @devt: the dev_t for the char device to be added
3238 * @drvdata: the data to be added to the device for callbacks
3239 * @fmt: string for the device's name
3240 * @args: va_list for the device's name
3242 * This function can be used by char device classes. A struct device
3243 * will be created in sysfs, registered to the specified class.
3245 * A "dev" file will be created, showing the dev_t for the device, if
3246 * the dev_t is not 0,0.
3247 * If a pointer to a parent struct device is passed in, the newly created
3248 * struct device will be a child of that device in sysfs.
3249 * The pointer to the struct device will be returned from the call.
3250 * Any further sysfs files that might be required can be created using this
3253 * Returns &struct device pointer on success, or ERR_PTR() on error.
3255 * Note: the struct class passed to this function must have previously
3256 * been created with a call to class_create().
3258 struct device
*device_create_vargs(struct class *class, struct device
*parent
,
3259 dev_t devt
, void *drvdata
, const char *fmt
,
3262 return device_create_groups_vargs(class, parent
, devt
, drvdata
, NULL
,
3265 EXPORT_SYMBOL_GPL(device_create_vargs
);
3268 * device_create - creates a device and registers it with sysfs
3269 * @class: pointer to the struct class that this device should be registered to
3270 * @parent: pointer to the parent struct device of this new device, if any
3271 * @devt: the dev_t for the char device to be added
3272 * @drvdata: the data to be added to the device for callbacks
3273 * @fmt: string for the device's name
3275 * This function can be used by char device classes. A struct device
3276 * will be created in sysfs, registered to the specified class.
3278 * A "dev" file will be created, showing the dev_t for the device, if
3279 * the dev_t is not 0,0.
3280 * If a pointer to a parent struct device is passed in, the newly created
3281 * struct device will be a child of that device in sysfs.
3282 * The pointer to the struct device will be returned from the call.
3283 * Any further sysfs files that might be required can be created using this
3286 * Returns &struct device pointer on success, or ERR_PTR() on error.
3288 * Note: the struct class passed to this function must have previously
3289 * been created with a call to class_create().
3291 struct device
*device_create(struct class *class, struct device
*parent
,
3292 dev_t devt
, void *drvdata
, const char *fmt
, ...)
3297 va_start(vargs
, fmt
);
3298 dev
= device_create_vargs(class, parent
, devt
, drvdata
, fmt
, vargs
);
3302 EXPORT_SYMBOL_GPL(device_create
);
3305 * device_create_with_groups - creates a device and registers it with sysfs
3306 * @class: pointer to the struct class that this device should be registered to
3307 * @parent: pointer to the parent struct device of this new device, if any
3308 * @devt: the dev_t for the char device to be added
3309 * @drvdata: the data to be added to the device for callbacks
3310 * @groups: NULL-terminated list of attribute groups to be created
3311 * @fmt: string for the device's name
3313 * This function can be used by char device classes. A struct device
3314 * will be created in sysfs, registered to the specified class.
3315 * Additional attributes specified in the groups parameter will also
3316 * be created automatically.
3318 * A "dev" file will be created, showing the dev_t for the device, if
3319 * the dev_t is not 0,0.
3320 * If a pointer to a parent struct device is passed in, the newly created
3321 * struct device will be a child of that device in sysfs.
3322 * The pointer to the struct device will be returned from the call.
3323 * Any further sysfs files that might be required can be created using this
3326 * Returns &struct device pointer on success, or ERR_PTR() on error.
3328 * Note: the struct class passed to this function must have previously
3329 * been created with a call to class_create().
3331 struct device
*device_create_with_groups(struct class *class,
3332 struct device
*parent
, dev_t devt
,
3334 const struct attribute_group
**groups
,
3335 const char *fmt
, ...)
3340 va_start(vargs
, fmt
);
3341 dev
= device_create_groups_vargs(class, parent
, devt
, drvdata
, groups
,
3346 EXPORT_SYMBOL_GPL(device_create_with_groups
);
3349 * device_destroy - removes a device that was created with device_create()
3350 * @class: pointer to the struct class that this device was registered with
3351 * @devt: the dev_t of the device that was previously registered
3353 * This call unregisters and cleans up a device that was created with a
3354 * call to device_create().
3356 void device_destroy(struct class *class, dev_t devt
)
3360 dev
= class_find_device_by_devt(class, devt
);
3363 device_unregister(dev
);
3366 EXPORT_SYMBOL_GPL(device_destroy
);
3369 * device_rename - renames a device
3370 * @dev: the pointer to the struct device to be renamed
3371 * @new_name: the new name of the device
3373 * It is the responsibility of the caller to provide mutual
3374 * exclusion between two different calls of device_rename
3375 * on the same device to ensure that new_name is valid and
3376 * won't conflict with other devices.
3378 * Note: Don't call this function. Currently, the networking layer calls this
3379 * function, but that will change. The following text from Kay Sievers offers
3382 * Renaming devices is racy at many levels, symlinks and other stuff are not
3383 * replaced atomically, and you get a "move" uevent, but it's not easy to
3384 * connect the event to the old and new device. Device nodes are not renamed at
3385 * all, there isn't even support for that in the kernel now.
3387 * In the meantime, during renaming, your target name might be taken by another
3388 * driver, creating conflicts. Or the old name is taken directly after you
3389 * renamed it -- then you get events for the same DEVPATH, before you even see
3390 * the "move" event. It's just a mess, and nothing new should ever rely on
3391 * kernel device renaming. Besides that, it's not even implemented now for
3392 * other things than (driver-core wise very simple) network devices.
3394 * We are currently about to change network renaming in udev to completely
3395 * disallow renaming of devices in the same namespace as the kernel uses,
3396 * because we can't solve the problems properly, that arise with swapping names
3397 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
3398 * be allowed to some other name than eth[0-9]*, for the aforementioned
3401 * Make up a "real" name in the driver before you register anything, or add
3402 * some other attributes for userspace to find the device, or use udev to add
3403 * symlinks -- but never rename kernel devices later, it's a complete mess. We
3404 * don't even want to get into that and try to implement the missing pieces in
3405 * the core. We really have other pieces to fix in the driver core mess. :)
3407 int device_rename(struct device
*dev
, const char *new_name
)
3409 struct kobject
*kobj
= &dev
->kobj
;
3410 char *old_device_name
= NULL
;
3413 dev
= get_device(dev
);
3417 dev_dbg(dev
, "renaming to %s\n", new_name
);
3419 old_device_name
= kstrdup(dev_name(dev
), GFP_KERNEL
);
3420 if (!old_device_name
) {
3426 error
= sysfs_rename_link_ns(&dev
->class->p
->subsys
.kobj
,
3427 kobj
, old_device_name
,
3428 new_name
, kobject_namespace(kobj
));
3433 error
= kobject_rename(kobj
, new_name
);
3440 kfree(old_device_name
);
3444 EXPORT_SYMBOL_GPL(device_rename
);
3446 static int device_move_class_links(struct device
*dev
,
3447 struct device
*old_parent
,
3448 struct device
*new_parent
)
3453 sysfs_remove_link(&dev
->kobj
, "device");
3455 error
= sysfs_create_link(&dev
->kobj
, &new_parent
->kobj
,
3461 * device_move - moves a device to a new parent
3462 * @dev: the pointer to the struct device to be moved
3463 * @new_parent: the new parent of the device (can be NULL)
3464 * @dpm_order: how to reorder the dpm_list
3466 int device_move(struct device
*dev
, struct device
*new_parent
,
3467 enum dpm_order dpm_order
)
3470 struct device
*old_parent
;
3471 struct kobject
*new_parent_kobj
;
3473 dev
= get_device(dev
);
3478 new_parent
= get_device(new_parent
);
3479 new_parent_kobj
= get_device_parent(dev
, new_parent
);
3480 if (IS_ERR(new_parent_kobj
)) {
3481 error
= PTR_ERR(new_parent_kobj
);
3482 put_device(new_parent
);
3486 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev
),
3487 __func__
, new_parent
? dev_name(new_parent
) : "<NULL>");
3488 error
= kobject_move(&dev
->kobj
, new_parent_kobj
);
3490 cleanup_glue_dir(dev
, new_parent_kobj
);
3491 put_device(new_parent
);
3494 old_parent
= dev
->parent
;
3495 dev
->parent
= new_parent
;
3497 klist_remove(&dev
->p
->knode_parent
);
3499 klist_add_tail(&dev
->p
->knode_parent
,
3500 &new_parent
->p
->klist_children
);
3501 set_dev_node(dev
, dev_to_node(new_parent
));
3505 error
= device_move_class_links(dev
, old_parent
, new_parent
);
3507 /* We ignore errors on cleanup since we're hosed anyway... */
3508 device_move_class_links(dev
, new_parent
, old_parent
);
3509 if (!kobject_move(&dev
->kobj
, &old_parent
->kobj
)) {
3511 klist_remove(&dev
->p
->knode_parent
);
3512 dev
->parent
= old_parent
;
3514 klist_add_tail(&dev
->p
->knode_parent
,
3515 &old_parent
->p
->klist_children
);
3516 set_dev_node(dev
, dev_to_node(old_parent
));
3519 cleanup_glue_dir(dev
, new_parent_kobj
);
3520 put_device(new_parent
);
3524 switch (dpm_order
) {
3525 case DPM_ORDER_NONE
:
3527 case DPM_ORDER_DEV_AFTER_PARENT
:
3528 device_pm_move_after(dev
, new_parent
);
3529 devices_kset_move_after(dev
, new_parent
);
3531 case DPM_ORDER_PARENT_BEFORE_DEV
:
3532 device_pm_move_before(new_parent
, dev
);
3533 devices_kset_move_before(new_parent
, dev
);
3535 case DPM_ORDER_DEV_LAST
:
3536 device_pm_move_last(dev
);
3537 devices_kset_move_last(dev
);
3541 put_device(old_parent
);
3547 EXPORT_SYMBOL_GPL(device_move
);
3549 static int device_attrs_change_owner(struct device
*dev
, kuid_t kuid
,
3552 struct kobject
*kobj
= &dev
->kobj
;
3553 struct class *class = dev
->class;
3554 const struct device_type
*type
= dev
->type
;
3559 * Change the device groups of the device class for @dev to
3562 error
= sysfs_groups_change_owner(kobj
, class->dev_groups
, kuid
,
3570 * Change the device groups of the device type for @dev to
3573 error
= sysfs_groups_change_owner(kobj
, type
->groups
, kuid
,
3579 /* Change the device groups of @dev to @kuid/@kgid. */
3580 error
= sysfs_groups_change_owner(kobj
, dev
->groups
, kuid
, kgid
);
3584 if (device_supports_offline(dev
) && !dev
->offline_disabled
) {
3585 /* Change online device attributes of @dev to @kuid/@kgid. */
3586 error
= sysfs_file_change_owner(kobj
, dev_attr_online
.attr
.name
,
3596 * device_change_owner - change the owner of an existing device.
3598 * @kuid: new owner's kuid
3599 * @kgid: new owner's kgid
3601 * This changes the owner of @dev and its corresponding sysfs entries to
3602 * @kuid/@kgid. This function closely mirrors how @dev was added via driver
3605 * Returns 0 on success or error code on failure.
3607 int device_change_owner(struct device
*dev
, kuid_t kuid
, kgid_t kgid
)
3610 struct kobject
*kobj
= &dev
->kobj
;
3612 dev
= get_device(dev
);
3617 * Change the kobject and the default attributes and groups of the
3618 * ktype associated with it to @kuid/@kgid.
3620 error
= sysfs_change_owner(kobj
, kuid
, kgid
);
3625 * Change the uevent file for @dev to the new owner. The uevent file
3626 * was created in a separate step when @dev got added and we mirror
3629 error
= sysfs_file_change_owner(kobj
, dev_attr_uevent
.attr
.name
, kuid
,
3635 * Change the device groups, the device groups associated with the
3636 * device class, and the groups associated with the device type of @dev
3639 error
= device_attrs_change_owner(dev
, kuid
, kgid
);
3643 error
= dpm_sysfs_change_owner(dev
, kuid
, kgid
);
3648 if (sysfs_deprecated
&& dev
->class == &block_class
)
3653 * Change the owner of the symlink located in the class directory of
3654 * the device class associated with @dev which points to the actual
3655 * directory entry for @dev to @kuid/@kgid. This ensures that the
3656 * symlink shows the same permissions as its target.
3658 error
= sysfs_link_change_owner(&dev
->class->p
->subsys
.kobj
, &dev
->kobj
,
3659 dev_name(dev
), kuid
, kgid
);
3667 EXPORT_SYMBOL_GPL(device_change_owner
);
3670 * device_shutdown - call ->shutdown() on each device to shutdown.
3672 void device_shutdown(void)
3674 struct device
*dev
, *parent
;
3676 wait_for_device_probe();
3677 device_block_probing();
3681 spin_lock(&devices_kset
->list_lock
);
3683 * Walk the devices list backward, shutting down each in turn.
3684 * Beware that device unplug events may also start pulling
3685 * devices offline, even as the system is shutting down.
3687 while (!list_empty(&devices_kset
->list
)) {
3688 dev
= list_entry(devices_kset
->list
.prev
, struct device
,
3692 * hold reference count of device's parent to
3693 * prevent it from being freed because parent's
3694 * lock is to be held
3696 parent
= get_device(dev
->parent
);
3699 * Make sure the device is off the kset list, in the
3700 * event that dev->*->shutdown() doesn't remove it.
3702 list_del_init(&dev
->kobj
.entry
);
3703 spin_unlock(&devices_kset
->list_lock
);
3705 /* hold lock to avoid race with probe/release */
3707 device_lock(parent
);
3710 /* Don't allow any more runtime suspends */
3711 pm_runtime_get_noresume(dev
);
3712 pm_runtime_barrier(dev
);
3714 if (dev
->class && dev
->class->shutdown_pre
) {
3716 dev_info(dev
, "shutdown_pre\n");
3717 dev
->class->shutdown_pre(dev
);
3719 if (dev
->bus
&& dev
->bus
->shutdown
) {
3721 dev_info(dev
, "shutdown\n");
3722 dev
->bus
->shutdown(dev
);
3723 } else if (dev
->driver
&& dev
->driver
->shutdown
) {
3725 dev_info(dev
, "shutdown\n");
3726 dev
->driver
->shutdown(dev
);
3731 device_unlock(parent
);
3736 spin_lock(&devices_kset
->list_lock
);
3738 spin_unlock(&devices_kset
->list_lock
);
3742 * Device logging functions
3745 #ifdef CONFIG_PRINTK
3747 create_syslog_header(const struct device
*dev
, char *hdr
, size_t hdrlen
)
3753 subsys
= dev
->class->name
;
3755 subsys
= dev
->bus
->name
;
3759 pos
+= snprintf(hdr
+ pos
, hdrlen
- pos
, "SUBSYSTEM=%s", subsys
);
3764 * Add device identifier DEVICE=:
3768 * +sound:card0 subsystem:devname
3770 if (MAJOR(dev
->devt
)) {
3773 if (strcmp(subsys
, "block") == 0)
3778 pos
+= snprintf(hdr
+ pos
, hdrlen
- pos
,
3780 c
, MAJOR(dev
->devt
), MINOR(dev
->devt
));
3781 } else if (strcmp(subsys
, "net") == 0) {
3782 struct net_device
*net
= to_net_dev(dev
);
3785 pos
+= snprintf(hdr
+ pos
, hdrlen
- pos
,
3786 "DEVICE=n%u", net
->ifindex
);
3789 pos
+= snprintf(hdr
+ pos
, hdrlen
- pos
,
3790 "DEVICE=+%s:%s", subsys
, dev_name(dev
));
3799 dev_WARN(dev
, "device/subsystem name too long");
3803 int dev_vprintk_emit(int level
, const struct device
*dev
,
3804 const char *fmt
, va_list args
)
3809 hdrlen
= create_syslog_header(dev
, hdr
, sizeof(hdr
));
3811 return vprintk_emit(0, level
, hdrlen
? hdr
: NULL
, hdrlen
, fmt
, args
);
3813 EXPORT_SYMBOL(dev_vprintk_emit
);
3815 int dev_printk_emit(int level
, const struct device
*dev
, const char *fmt
, ...)
3820 va_start(args
, fmt
);
3822 r
= dev_vprintk_emit(level
, dev
, fmt
, args
);
3828 EXPORT_SYMBOL(dev_printk_emit
);
3830 static void __dev_printk(const char *level
, const struct device
*dev
,
3831 struct va_format
*vaf
)
3834 dev_printk_emit(level
[1] - '0', dev
, "%s %s: %pV",
3835 dev_driver_string(dev
), dev_name(dev
), vaf
);
3837 printk("%s(NULL device *): %pV", level
, vaf
);
3840 void dev_printk(const char *level
, const struct device
*dev
,
3841 const char *fmt
, ...)
3843 struct va_format vaf
;
3846 va_start(args
, fmt
);
3851 __dev_printk(level
, dev
, &vaf
);
3855 EXPORT_SYMBOL(dev_printk
);
3857 #define define_dev_printk_level(func, kern_level) \
3858 void func(const struct device *dev, const char *fmt, ...) \
3860 struct va_format vaf; \
3863 va_start(args, fmt); \
3868 __dev_printk(kern_level, dev, &vaf); \
3872 EXPORT_SYMBOL(func);
3874 define_dev_printk_level(_dev_emerg
, KERN_EMERG
);
3875 define_dev_printk_level(_dev_alert
, KERN_ALERT
);
3876 define_dev_printk_level(_dev_crit
, KERN_CRIT
);
3877 define_dev_printk_level(_dev_err
, KERN_ERR
);
3878 define_dev_printk_level(_dev_warn
, KERN_WARNING
);
3879 define_dev_printk_level(_dev_notice
, KERN_NOTICE
);
3880 define_dev_printk_level(_dev_info
, KERN_INFO
);
3884 static inline bool fwnode_is_primary(struct fwnode_handle
*fwnode
)
3886 return fwnode
&& !IS_ERR(fwnode
->secondary
);
3890 * set_primary_fwnode - Change the primary firmware node of a given device.
3891 * @dev: Device to handle.
3892 * @fwnode: New primary firmware node of the device.
3894 * Set the device's firmware node pointer to @fwnode, but if a secondary
3895 * firmware node of the device is present, preserve it.
3897 void set_primary_fwnode(struct device
*dev
, struct fwnode_handle
*fwnode
)
3900 struct fwnode_handle
*fn
= dev
->fwnode
;
3902 if (fwnode_is_primary(fn
))
3906 WARN_ON(fwnode
->secondary
);
3907 fwnode
->secondary
= fn
;
3909 dev
->fwnode
= fwnode
;
3911 dev
->fwnode
= fwnode_is_primary(dev
->fwnode
) ?
3912 dev
->fwnode
->secondary
: NULL
;
3915 EXPORT_SYMBOL_GPL(set_primary_fwnode
);
3918 * set_secondary_fwnode - Change the secondary firmware node of a given device.
3919 * @dev: Device to handle.
3920 * @fwnode: New secondary firmware node of the device.
3922 * If a primary firmware node of the device is present, set its secondary
3923 * pointer to @fwnode. Otherwise, set the device's firmware node pointer to
3926 void set_secondary_fwnode(struct device
*dev
, struct fwnode_handle
*fwnode
)
3929 fwnode
->secondary
= ERR_PTR(-ENODEV
);
3931 if (fwnode_is_primary(dev
->fwnode
))
3932 dev
->fwnode
->secondary
= fwnode
;
3934 dev
->fwnode
= fwnode
;
3938 * device_set_of_node_from_dev - reuse device-tree node of another device
3939 * @dev: device whose device-tree node is being set
3940 * @dev2: device whose device-tree node is being reused
3942 * Takes another reference to the new device-tree node after first dropping
3943 * any reference held to the old node.
3945 void device_set_of_node_from_dev(struct device
*dev
, const struct device
*dev2
)
3947 of_node_put(dev
->of_node
);
3948 dev
->of_node
= of_node_get(dev2
->of_node
);
3949 dev
->of_node_reused
= true;
3951 EXPORT_SYMBOL_GPL(device_set_of_node_from_dev
);
3953 int device_match_name(struct device
*dev
, const void *name
)
3955 return sysfs_streq(dev_name(dev
), name
);
3957 EXPORT_SYMBOL_GPL(device_match_name
);
3959 int device_match_of_node(struct device
*dev
, const void *np
)
3961 return dev
->of_node
== np
;
3963 EXPORT_SYMBOL_GPL(device_match_of_node
);
3965 int device_match_fwnode(struct device
*dev
, const void *fwnode
)
3967 return dev_fwnode(dev
) == fwnode
;
3969 EXPORT_SYMBOL_GPL(device_match_fwnode
);
3971 int device_match_devt(struct device
*dev
, const void *pdevt
)
3973 return dev
->devt
== *(dev_t
*)pdevt
;
3975 EXPORT_SYMBOL_GPL(device_match_devt
);
3977 int device_match_acpi_dev(struct device
*dev
, const void *adev
)
3979 return ACPI_COMPANION(dev
) == adev
;
3981 EXPORT_SYMBOL(device_match_acpi_dev
);
3983 int device_match_any(struct device
*dev
, const void *unused
)
3987 EXPORT_SYMBOL_GPL(device_match_any
);