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/cpufreq.h>
12 #include <linux/device.h>
13 #include <linux/err.h>
14 #include <linux/fwnode.h>
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/string.h>
19 #include <linux/kdev_t.h>
20 #include <linux/notifier.h>
22 #include <linux/of_device.h>
23 #include <linux/genhd.h>
24 #include <linux/mutex.h>
25 #include <linux/pm_runtime.h>
26 #include <linux/netdevice.h>
27 #include <linux/sched/signal.h>
28 #include <linux/sysfs.h>
31 #include "power/power.h"
33 #ifdef CONFIG_SYSFS_DEPRECATED
34 #ifdef CONFIG_SYSFS_DEPRECATED_V2
35 long sysfs_deprecated
= 1;
37 long sysfs_deprecated
= 0;
39 static int __init
sysfs_deprecated_setup(char *arg
)
41 return kstrtol(arg
, 10, &sysfs_deprecated
);
43 early_param("sysfs.deprecated", sysfs_deprecated_setup
);
46 /* Device links support. */
49 static DEFINE_MUTEX(device_links_lock
);
50 DEFINE_STATIC_SRCU(device_links_srcu
);
52 static inline void device_links_write_lock(void)
54 mutex_lock(&device_links_lock
);
57 static inline void device_links_write_unlock(void)
59 mutex_unlock(&device_links_lock
);
62 int device_links_read_lock(void)
64 return srcu_read_lock(&device_links_srcu
);
67 void device_links_read_unlock(int idx
)
69 srcu_read_unlock(&device_links_srcu
, idx
);
71 #else /* !CONFIG_SRCU */
72 static DECLARE_RWSEM(device_links_lock
);
74 static inline void device_links_write_lock(void)
76 down_write(&device_links_lock
);
79 static inline void device_links_write_unlock(void)
81 up_write(&device_links_lock
);
84 int device_links_read_lock(void)
86 down_read(&device_links_lock
);
90 void device_links_read_unlock(int not_used
)
92 up_read(&device_links_lock
);
94 #endif /* !CONFIG_SRCU */
97 * device_is_dependent - Check if one device depends on another one
98 * @dev: Device to check dependencies for.
99 * @target: Device to check against.
101 * Check if @target depends on @dev or any device dependent on it (its child or
102 * its consumer etc). Return 1 if that is the case or 0 otherwise.
104 static int device_is_dependent(struct device
*dev
, void *target
)
106 struct device_link
*link
;
112 ret
= device_for_each_child(dev
, target
, device_is_dependent
);
116 list_for_each_entry(link
, &dev
->links
.consumers
, s_node
) {
117 if (link
->consumer
== target
)
120 ret
= device_is_dependent(link
->consumer
, target
);
127 static void device_link_init_status(struct device_link
*link
,
128 struct device
*consumer
,
129 struct device
*supplier
)
131 switch (supplier
->links
.status
) {
133 switch (consumer
->links
.status
) {
136 * A consumer driver can create a link to a supplier
137 * that has not completed its probing yet as long as it
138 * knows that the supplier is already functional (for
139 * example, it has just acquired some resources from the
142 link
->status
= DL_STATE_CONSUMER_PROBE
;
145 link
->status
= DL_STATE_DORMANT
;
149 case DL_DEV_DRIVER_BOUND
:
150 switch (consumer
->links
.status
) {
152 link
->status
= DL_STATE_CONSUMER_PROBE
;
154 case DL_DEV_DRIVER_BOUND
:
155 link
->status
= DL_STATE_ACTIVE
;
158 link
->status
= DL_STATE_AVAILABLE
;
162 case DL_DEV_UNBINDING
:
163 link
->status
= DL_STATE_SUPPLIER_UNBIND
;
166 link
->status
= DL_STATE_DORMANT
;
171 static int device_reorder_to_tail(struct device
*dev
, void *not_used
)
173 struct device_link
*link
;
176 * Devices that have not been registered yet will be put to the ends
177 * of the lists during the registration, so skip them here.
179 if (device_is_registered(dev
))
180 devices_kset_move_last(dev
);
182 if (device_pm_initialized(dev
))
183 device_pm_move_last(dev
);
185 device_for_each_child(dev
, NULL
, device_reorder_to_tail
);
186 list_for_each_entry(link
, &dev
->links
.consumers
, s_node
)
187 device_reorder_to_tail(link
->consumer
, NULL
);
193 * device_pm_move_to_tail - Move set of devices to the end of device lists
194 * @dev: Device to move
196 * This is a device_reorder_to_tail() wrapper taking the requisite locks.
198 * It moves the @dev along with all of its children and all of its consumers
199 * to the ends of the device_kset and dpm_list, recursively.
201 void device_pm_move_to_tail(struct device
*dev
)
205 idx
= device_links_read_lock();
207 device_reorder_to_tail(dev
, NULL
);
209 device_links_read_unlock(idx
);
212 #define DL_MANAGED_LINK_FLAGS (DL_FLAG_AUTOREMOVE_CONSUMER | \
213 DL_FLAG_AUTOREMOVE_SUPPLIER | \
214 DL_FLAG_AUTOPROBE_CONSUMER)
216 #define DL_ADD_VALID_FLAGS (DL_MANAGED_LINK_FLAGS | DL_FLAG_STATELESS | \
217 DL_FLAG_PM_RUNTIME | DL_FLAG_RPM_ACTIVE)
220 * device_link_add - Create a link between two devices.
221 * @consumer: Consumer end of the link.
222 * @supplier: Supplier end of the link.
223 * @flags: Link flags.
225 * The caller is responsible for the proper synchronization of the link creation
226 * with runtime PM. First, setting the DL_FLAG_PM_RUNTIME flag will cause the
227 * runtime PM framework to take the link into account. Second, if the
228 * DL_FLAG_RPM_ACTIVE flag is set in addition to it, the supplier devices will
229 * be forced into the active metastate and reference-counted upon the creation
230 * of the link. If DL_FLAG_PM_RUNTIME is not set, DL_FLAG_RPM_ACTIVE will be
233 * If DL_FLAG_STATELESS is set in @flags, the caller of this function is
234 * expected to release the link returned by it directly with the help of either
235 * device_link_del() or device_link_remove().
237 * If that flag is not set, however, the caller of this function is handing the
238 * management of the link over to the driver core entirely and its return value
239 * can only be used to check whether or not the link is present. In that case,
240 * the DL_FLAG_AUTOREMOVE_CONSUMER and DL_FLAG_AUTOREMOVE_SUPPLIER device link
241 * flags can be used to indicate to the driver core when the link can be safely
242 * deleted. Namely, setting one of them in @flags indicates to the driver core
243 * that the link is not going to be used (by the given caller of this function)
244 * after unbinding the consumer or supplier driver, respectively, from its
245 * device, so the link can be deleted at that point. If none of them is set,
246 * the link will be maintained until one of the devices pointed to by it (either
247 * the consumer or the supplier) is unregistered.
249 * Also, if DL_FLAG_STATELESS, DL_FLAG_AUTOREMOVE_CONSUMER and
250 * DL_FLAG_AUTOREMOVE_SUPPLIER are not set in @flags (that is, a persistent
251 * managed device link is being added), the DL_FLAG_AUTOPROBE_CONSUMER flag can
252 * be used to request the driver core to automaticall probe for a consmer
253 * driver after successfully binding a driver to the supplier device.
255 * The combination of DL_FLAG_STATELESS and one of DL_FLAG_AUTOREMOVE_CONSUMER,
256 * DL_FLAG_AUTOREMOVE_SUPPLIER, or DL_FLAG_AUTOPROBE_CONSUMER set in @flags at
257 * the same time is invalid and will cause NULL to be returned upfront.
258 * However, if a device link between the given @consumer and @supplier pair
259 * exists already when this function is called for them, the existing link will
260 * be returned regardless of its current type and status (the link's flags may
261 * be modified then). The caller of this function is then expected to treat
262 * the link as though it has just been created, so (in particular) if
263 * DL_FLAG_STATELESS was passed in @flags, the link needs to be released
264 * explicitly when not needed any more (as stated above).
266 * A side effect of the link creation is re-ordering of dpm_list and the
267 * devices_kset list by moving the consumer device and all devices depending
268 * on it to the ends of these lists (that does not happen to devices that have
269 * not been registered when this function is called).
271 * The supplier device is required to be registered when this function is called
272 * and NULL will be returned if that is not the case. The consumer device need
273 * not be registered, however.
275 struct device_link
*device_link_add(struct device
*consumer
,
276 struct device
*supplier
, u32 flags
)
278 struct device_link
*link
;
280 if (!consumer
|| !supplier
|| flags
& ~DL_ADD_VALID_FLAGS
||
281 (flags
& DL_FLAG_STATELESS
&& flags
& DL_MANAGED_LINK_FLAGS
) ||
282 (flags
& DL_FLAG_AUTOPROBE_CONSUMER
&&
283 flags
& (DL_FLAG_AUTOREMOVE_CONSUMER
|
284 DL_FLAG_AUTOREMOVE_SUPPLIER
)))
287 if (flags
& DL_FLAG_PM_RUNTIME
&& flags
& DL_FLAG_RPM_ACTIVE
) {
288 if (pm_runtime_get_sync(supplier
) < 0) {
289 pm_runtime_put_noidle(supplier
);
294 if (!(flags
& DL_FLAG_STATELESS
))
295 flags
|= DL_FLAG_MANAGED
;
297 device_links_write_lock();
301 * If the supplier has not been fully registered yet or there is a
302 * reverse dependency between the consumer and the supplier already in
303 * the graph, return NULL.
305 if (!device_pm_initialized(supplier
)
306 || device_is_dependent(consumer
, supplier
)) {
312 * DL_FLAG_AUTOREMOVE_SUPPLIER indicates that the link will be needed
313 * longer than for DL_FLAG_AUTOREMOVE_CONSUMER and setting them both
314 * together doesn't make sense, so prefer DL_FLAG_AUTOREMOVE_SUPPLIER.
316 if (flags
& DL_FLAG_AUTOREMOVE_SUPPLIER
)
317 flags
&= ~DL_FLAG_AUTOREMOVE_CONSUMER
;
319 list_for_each_entry(link
, &supplier
->links
.consumers
, s_node
) {
320 if (link
->consumer
!= consumer
)
323 if (flags
& DL_FLAG_PM_RUNTIME
) {
324 if (!(link
->flags
& DL_FLAG_PM_RUNTIME
)) {
325 pm_runtime_new_link(consumer
);
326 link
->flags
|= DL_FLAG_PM_RUNTIME
;
328 if (flags
& DL_FLAG_RPM_ACTIVE
)
329 refcount_inc(&link
->rpm_active
);
332 if (flags
& DL_FLAG_STATELESS
) {
333 link
->flags
|= DL_FLAG_STATELESS
;
334 kref_get(&link
->kref
);
339 * If the life time of the link following from the new flags is
340 * longer than indicated by the flags of the existing link,
341 * update the existing link to stay around longer.
343 if (flags
& DL_FLAG_AUTOREMOVE_SUPPLIER
) {
344 if (link
->flags
& DL_FLAG_AUTOREMOVE_CONSUMER
) {
345 link
->flags
&= ~DL_FLAG_AUTOREMOVE_CONSUMER
;
346 link
->flags
|= DL_FLAG_AUTOREMOVE_SUPPLIER
;
348 } else if (!(flags
& DL_FLAG_AUTOREMOVE_CONSUMER
)) {
349 link
->flags
&= ~(DL_FLAG_AUTOREMOVE_CONSUMER
|
350 DL_FLAG_AUTOREMOVE_SUPPLIER
);
352 if (!(link
->flags
& DL_FLAG_MANAGED
)) {
353 kref_get(&link
->kref
);
354 link
->flags
|= DL_FLAG_MANAGED
;
355 device_link_init_status(link
, consumer
, supplier
);
360 link
= kzalloc(sizeof(*link
), GFP_KERNEL
);
364 refcount_set(&link
->rpm_active
, 1);
366 if (flags
& DL_FLAG_PM_RUNTIME
) {
367 if (flags
& DL_FLAG_RPM_ACTIVE
)
368 refcount_inc(&link
->rpm_active
);
370 pm_runtime_new_link(consumer
);
373 get_device(supplier
);
374 link
->supplier
= supplier
;
375 INIT_LIST_HEAD(&link
->s_node
);
376 get_device(consumer
);
377 link
->consumer
= consumer
;
378 INIT_LIST_HEAD(&link
->c_node
);
380 kref_init(&link
->kref
);
382 /* Determine the initial link state. */
383 if (flags
& DL_FLAG_STATELESS
)
384 link
->status
= DL_STATE_NONE
;
386 device_link_init_status(link
, consumer
, supplier
);
389 * Some callers expect the link creation during consumer driver probe to
390 * resume the supplier even without DL_FLAG_RPM_ACTIVE.
392 if (link
->status
== DL_STATE_CONSUMER_PROBE
&&
393 flags
& DL_FLAG_PM_RUNTIME
)
394 pm_runtime_resume(supplier
);
397 * Move the consumer and all of the devices depending on it to the end
398 * of dpm_list and the devices_kset list.
400 * It is necessary to hold dpm_list locked throughout all that or else
401 * we may end up suspending with a wrong ordering of it.
403 device_reorder_to_tail(consumer
, NULL
);
405 list_add_tail_rcu(&link
->s_node
, &supplier
->links
.consumers
);
406 list_add_tail_rcu(&link
->c_node
, &consumer
->links
.suppliers
);
408 dev_info(consumer
, "Linked as a consumer to %s\n", dev_name(supplier
));
412 device_links_write_unlock();
414 if ((flags
& DL_FLAG_PM_RUNTIME
&& flags
& DL_FLAG_RPM_ACTIVE
) && !link
)
415 pm_runtime_put(supplier
);
419 EXPORT_SYMBOL_GPL(device_link_add
);
421 static void device_link_free(struct device_link
*link
)
423 while (refcount_dec_not_one(&link
->rpm_active
))
424 pm_runtime_put(link
->supplier
);
426 put_device(link
->consumer
);
427 put_device(link
->supplier
);
432 static void __device_link_free_srcu(struct rcu_head
*rhead
)
434 device_link_free(container_of(rhead
, struct device_link
, rcu_head
));
437 static void __device_link_del(struct kref
*kref
)
439 struct device_link
*link
= container_of(kref
, struct device_link
, kref
);
441 dev_info(link
->consumer
, "Dropping the link to %s\n",
442 dev_name(link
->supplier
));
444 if (link
->flags
& DL_FLAG_PM_RUNTIME
)
445 pm_runtime_drop_link(link
->consumer
);
447 list_del_rcu(&link
->s_node
);
448 list_del_rcu(&link
->c_node
);
449 call_srcu(&device_links_srcu
, &link
->rcu_head
, __device_link_free_srcu
);
451 #else /* !CONFIG_SRCU */
452 static void __device_link_del(struct kref
*kref
)
454 struct device_link
*link
= container_of(kref
, struct device_link
, kref
);
456 dev_info(link
->consumer
, "Dropping the link to %s\n",
457 dev_name(link
->supplier
));
459 if (link
->flags
& DL_FLAG_PM_RUNTIME
)
460 pm_runtime_drop_link(link
->consumer
);
462 list_del(&link
->s_node
);
463 list_del(&link
->c_node
);
464 device_link_free(link
);
466 #endif /* !CONFIG_SRCU */
468 static void device_link_put_kref(struct device_link
*link
)
470 if (link
->flags
& DL_FLAG_STATELESS
)
471 kref_put(&link
->kref
, __device_link_del
);
473 WARN(1, "Unable to drop a managed device link reference\n");
477 * device_link_del - Delete a stateless link between two devices.
478 * @link: Device link to delete.
480 * The caller must ensure proper synchronization of this function with runtime
481 * PM. If the link was added multiple times, it needs to be deleted as often.
482 * Care is required for hotplugged devices: Their links are purged on removal
483 * and calling device_link_del() is then no longer allowed.
485 void device_link_del(struct device_link
*link
)
487 device_links_write_lock();
489 device_link_put_kref(link
);
491 device_links_write_unlock();
493 EXPORT_SYMBOL_GPL(device_link_del
);
496 * device_link_remove - Delete a stateless link between two devices.
497 * @consumer: Consumer end of the link.
498 * @supplier: Supplier end of the link.
500 * The caller must ensure proper synchronization of this function with runtime
503 void device_link_remove(void *consumer
, struct device
*supplier
)
505 struct device_link
*link
;
507 if (WARN_ON(consumer
== supplier
))
510 device_links_write_lock();
513 list_for_each_entry(link
, &supplier
->links
.consumers
, s_node
) {
514 if (link
->consumer
== consumer
) {
515 device_link_put_kref(link
);
521 device_links_write_unlock();
523 EXPORT_SYMBOL_GPL(device_link_remove
);
525 static void device_links_missing_supplier(struct device
*dev
)
527 struct device_link
*link
;
529 list_for_each_entry(link
, &dev
->links
.suppliers
, c_node
)
530 if (link
->status
== DL_STATE_CONSUMER_PROBE
)
531 WRITE_ONCE(link
->status
, DL_STATE_AVAILABLE
);
535 * device_links_check_suppliers - Check presence of supplier drivers.
536 * @dev: Consumer device.
538 * Check links from this device to any suppliers. Walk the list of the device's
539 * links to suppliers and see if all of them are available. If not, simply
540 * return -EPROBE_DEFER.
542 * We need to guarantee that the supplier will not go away after the check has
543 * been positive here. It only can go away in __device_release_driver() and
544 * that function checks the device's links to consumers. This means we need to
545 * mark the link as "consumer probe in progress" to make the supplier removal
546 * wait for us to complete (or bad things may happen).
548 * Links without the DL_FLAG_MANAGED flag set are ignored.
550 int device_links_check_suppliers(struct device
*dev
)
552 struct device_link
*link
;
555 device_links_write_lock();
557 list_for_each_entry(link
, &dev
->links
.suppliers
, c_node
) {
558 if (!(link
->flags
& DL_FLAG_MANAGED
))
561 if (link
->status
!= DL_STATE_AVAILABLE
) {
562 device_links_missing_supplier(dev
);
566 WRITE_ONCE(link
->status
, DL_STATE_CONSUMER_PROBE
);
568 dev
->links
.status
= DL_DEV_PROBING
;
570 device_links_write_unlock();
575 * device_links_driver_bound - Update device links after probing its driver.
576 * @dev: Device to update the links for.
578 * The probe has been successful, so update links from this device to any
579 * consumers by changing their status to "available".
581 * Also change the status of @dev's links to suppliers to "active".
583 * Links without the DL_FLAG_MANAGED flag set are ignored.
585 void device_links_driver_bound(struct device
*dev
)
587 struct device_link
*link
;
589 device_links_write_lock();
591 list_for_each_entry(link
, &dev
->links
.consumers
, s_node
) {
592 if (!(link
->flags
& DL_FLAG_MANAGED
))
596 * Links created during consumer probe may be in the "consumer
597 * probe" state to start with if the supplier is still probing
598 * when they are created and they may become "active" if the
599 * consumer probe returns first. Skip them here.
601 if (link
->status
== DL_STATE_CONSUMER_PROBE
||
602 link
->status
== DL_STATE_ACTIVE
)
605 WARN_ON(link
->status
!= DL_STATE_DORMANT
);
606 WRITE_ONCE(link
->status
, DL_STATE_AVAILABLE
);
608 if (link
->flags
& DL_FLAG_AUTOPROBE_CONSUMER
)
609 driver_deferred_probe_add(link
->consumer
);
612 list_for_each_entry(link
, &dev
->links
.suppliers
, c_node
) {
613 if (!(link
->flags
& DL_FLAG_MANAGED
))
616 WARN_ON(link
->status
!= DL_STATE_CONSUMER_PROBE
);
617 WRITE_ONCE(link
->status
, DL_STATE_ACTIVE
);
620 dev
->links
.status
= DL_DEV_DRIVER_BOUND
;
622 device_links_write_unlock();
625 static void device_link_drop_managed(struct device_link
*link
)
627 link
->flags
&= ~DL_FLAG_MANAGED
;
628 WRITE_ONCE(link
->status
, DL_STATE_NONE
);
629 kref_put(&link
->kref
, __device_link_del
);
633 * __device_links_no_driver - Update links of a device without a driver.
634 * @dev: Device without a drvier.
636 * Delete all non-persistent links from this device to any suppliers.
638 * Persistent links stay around, but their status is changed to "available",
639 * unless they already are in the "supplier unbind in progress" state in which
640 * case they need not be updated.
642 * Links without the DL_FLAG_MANAGED flag set are ignored.
644 static void __device_links_no_driver(struct device
*dev
)
646 struct device_link
*link
, *ln
;
648 list_for_each_entry_safe_reverse(link
, ln
, &dev
->links
.suppliers
, c_node
) {
649 if (!(link
->flags
& DL_FLAG_MANAGED
))
652 if (link
->flags
& DL_FLAG_AUTOREMOVE_CONSUMER
)
653 device_link_drop_managed(link
);
654 else if (link
->status
== DL_STATE_CONSUMER_PROBE
||
655 link
->status
== DL_STATE_ACTIVE
)
656 WRITE_ONCE(link
->status
, DL_STATE_AVAILABLE
);
659 dev
->links
.status
= DL_DEV_NO_DRIVER
;
663 * device_links_no_driver - Update links after failing driver probe.
664 * @dev: Device whose driver has just failed to probe.
666 * Clean up leftover links to consumers for @dev and invoke
667 * %__device_links_no_driver() to update links to suppliers for it as
670 * Links without the DL_FLAG_MANAGED flag set are ignored.
672 void device_links_no_driver(struct device
*dev
)
674 struct device_link
*link
;
676 device_links_write_lock();
678 list_for_each_entry(link
, &dev
->links
.consumers
, s_node
) {
679 if (!(link
->flags
& DL_FLAG_MANAGED
))
683 * The probe has failed, so if the status of the link is
684 * "consumer probe" or "active", it must have been added by
685 * a probing consumer while this device was still probing.
686 * Change its state to "dormant", as it represents a valid
687 * relationship, but it is not functionally meaningful.
689 if (link
->status
== DL_STATE_CONSUMER_PROBE
||
690 link
->status
== DL_STATE_ACTIVE
)
691 WRITE_ONCE(link
->status
, DL_STATE_DORMANT
);
694 __device_links_no_driver(dev
);
696 device_links_write_unlock();
700 * device_links_driver_cleanup - Update links after driver removal.
701 * @dev: Device whose driver has just gone away.
703 * Update links to consumers for @dev by changing their status to "dormant" and
704 * invoke %__device_links_no_driver() to update links to suppliers for it as
707 * Links without the DL_FLAG_MANAGED flag set are ignored.
709 void device_links_driver_cleanup(struct device
*dev
)
711 struct device_link
*link
, *ln
;
713 device_links_write_lock();
715 list_for_each_entry_safe(link
, ln
, &dev
->links
.consumers
, s_node
) {
716 if (!(link
->flags
& DL_FLAG_MANAGED
))
719 WARN_ON(link
->flags
& DL_FLAG_AUTOREMOVE_CONSUMER
);
720 WARN_ON(link
->status
!= DL_STATE_SUPPLIER_UNBIND
);
723 * autoremove the links between this @dev and its consumer
724 * devices that are not active, i.e. where the link state
725 * has moved to DL_STATE_SUPPLIER_UNBIND.
727 if (link
->status
== DL_STATE_SUPPLIER_UNBIND
&&
728 link
->flags
& DL_FLAG_AUTOREMOVE_SUPPLIER
)
729 device_link_drop_managed(link
);
731 WRITE_ONCE(link
->status
, DL_STATE_DORMANT
);
734 __device_links_no_driver(dev
);
736 device_links_write_unlock();
740 * device_links_busy - Check if there are any busy links to consumers.
741 * @dev: Device to check.
743 * Check each consumer of the device and return 'true' if its link's status
744 * is one of "consumer probe" or "active" (meaning that the given consumer is
745 * probing right now or its driver is present). Otherwise, change the link
746 * state to "supplier unbind" to prevent the consumer from being probed
747 * successfully going forward.
749 * Return 'false' if there are no probing or active consumers.
751 * Links without the DL_FLAG_MANAGED flag set are ignored.
753 bool device_links_busy(struct device
*dev
)
755 struct device_link
*link
;
758 device_links_write_lock();
760 list_for_each_entry(link
, &dev
->links
.consumers
, s_node
) {
761 if (!(link
->flags
& DL_FLAG_MANAGED
))
764 if (link
->status
== DL_STATE_CONSUMER_PROBE
765 || link
->status
== DL_STATE_ACTIVE
) {
769 WRITE_ONCE(link
->status
, DL_STATE_SUPPLIER_UNBIND
);
772 dev
->links
.status
= DL_DEV_UNBINDING
;
774 device_links_write_unlock();
779 * device_links_unbind_consumers - Force unbind consumers of the given device.
780 * @dev: Device to unbind the consumers of.
782 * Walk the list of links to consumers for @dev and if any of them is in the
783 * "consumer probe" state, wait for all device probes in progress to complete
786 * If that's not the case, change the status of the link to "supplier unbind"
787 * and check if the link was in the "active" state. If so, force the consumer
788 * driver to unbind and start over (the consumer will not re-probe as we have
789 * changed the state of the link already).
791 * Links without the DL_FLAG_MANAGED flag set are ignored.
793 void device_links_unbind_consumers(struct device
*dev
)
795 struct device_link
*link
;
798 device_links_write_lock();
800 list_for_each_entry(link
, &dev
->links
.consumers
, s_node
) {
801 enum device_link_state status
;
803 if (!(link
->flags
& DL_FLAG_MANAGED
))
806 status
= link
->status
;
807 if (status
== DL_STATE_CONSUMER_PROBE
) {
808 device_links_write_unlock();
810 wait_for_device_probe();
813 WRITE_ONCE(link
->status
, DL_STATE_SUPPLIER_UNBIND
);
814 if (status
== DL_STATE_ACTIVE
) {
815 struct device
*consumer
= link
->consumer
;
817 get_device(consumer
);
819 device_links_write_unlock();
821 device_release_driver_internal(consumer
, NULL
,
823 put_device(consumer
);
828 device_links_write_unlock();
832 * device_links_purge - Delete existing links to other devices.
833 * @dev: Target device.
835 static void device_links_purge(struct device
*dev
)
837 struct device_link
*link
, *ln
;
840 * Delete all of the remaining links from this device to any other
841 * devices (either consumers or suppliers).
843 device_links_write_lock();
845 list_for_each_entry_safe_reverse(link
, ln
, &dev
->links
.suppliers
, c_node
) {
846 WARN_ON(link
->status
== DL_STATE_ACTIVE
);
847 __device_link_del(&link
->kref
);
850 list_for_each_entry_safe_reverse(link
, ln
, &dev
->links
.consumers
, s_node
) {
851 WARN_ON(link
->status
!= DL_STATE_DORMANT
&&
852 link
->status
!= DL_STATE_NONE
);
853 __device_link_del(&link
->kref
);
856 device_links_write_unlock();
859 /* Device links support end. */
861 int (*platform_notify
)(struct device
*dev
) = NULL
;
862 int (*platform_notify_remove
)(struct device
*dev
) = NULL
;
863 static struct kobject
*dev_kobj
;
864 struct kobject
*sysfs_dev_char_kobj
;
865 struct kobject
*sysfs_dev_block_kobj
;
867 static DEFINE_MUTEX(device_hotplug_lock
);
869 void lock_device_hotplug(void)
871 mutex_lock(&device_hotplug_lock
);
874 void unlock_device_hotplug(void)
876 mutex_unlock(&device_hotplug_lock
);
879 int lock_device_hotplug_sysfs(void)
881 if (mutex_trylock(&device_hotplug_lock
))
884 /* Avoid busy looping (5 ms of sleep should do). */
886 return restart_syscall();
890 static inline int device_is_not_partition(struct device
*dev
)
892 return !(dev
->type
== &part_type
);
895 static inline int device_is_not_partition(struct device
*dev
)
902 * dev_driver_string - Return a device's driver name, if at all possible
903 * @dev: struct device to get the name of
905 * Will return the device's driver's name if it is bound to a device. If
906 * the device is not bound to a driver, it will return the name of the bus
907 * it is attached to. If it is not attached to a bus either, an empty
908 * string will be returned.
910 const char *dev_driver_string(const struct device
*dev
)
912 struct device_driver
*drv
;
914 /* dev->driver can change to NULL underneath us because of unbinding,
915 * so be careful about accessing it. dev->bus and dev->class should
916 * never change once they are set, so they don't need special care.
918 drv
= READ_ONCE(dev
->driver
);
919 return drv
? drv
->name
:
920 (dev
->bus
? dev
->bus
->name
:
921 (dev
->class ? dev
->class->name
: ""));
923 EXPORT_SYMBOL(dev_driver_string
);
925 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
927 static ssize_t
dev_attr_show(struct kobject
*kobj
, struct attribute
*attr
,
930 struct device_attribute
*dev_attr
= to_dev_attr(attr
);
931 struct device
*dev
= kobj_to_dev(kobj
);
935 ret
= dev_attr
->show(dev
, dev_attr
, buf
);
936 if (ret
>= (ssize_t
)PAGE_SIZE
) {
937 printk("dev_attr_show: %pS returned bad count\n",
943 static ssize_t
dev_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
944 const char *buf
, size_t count
)
946 struct device_attribute
*dev_attr
= to_dev_attr(attr
);
947 struct device
*dev
= kobj_to_dev(kobj
);
951 ret
= dev_attr
->store(dev
, dev_attr
, buf
, count
);
955 static const struct sysfs_ops dev_sysfs_ops
= {
956 .show
= dev_attr_show
,
957 .store
= dev_attr_store
,
960 #define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
962 ssize_t
device_store_ulong(struct device
*dev
,
963 struct device_attribute
*attr
,
964 const char *buf
, size_t size
)
966 struct dev_ext_attribute
*ea
= to_ext_attr(attr
);
968 unsigned long new = simple_strtoul(buf
, &end
, 0);
971 *(unsigned long *)(ea
->var
) = new;
972 /* Always return full write size even if we didn't consume all */
975 EXPORT_SYMBOL_GPL(device_store_ulong
);
977 ssize_t
device_show_ulong(struct device
*dev
,
978 struct device_attribute
*attr
,
981 struct dev_ext_attribute
*ea
= to_ext_attr(attr
);
982 return snprintf(buf
, PAGE_SIZE
, "%lx\n", *(unsigned long *)(ea
->var
));
984 EXPORT_SYMBOL_GPL(device_show_ulong
);
986 ssize_t
device_store_int(struct device
*dev
,
987 struct device_attribute
*attr
,
988 const char *buf
, size_t size
)
990 struct dev_ext_attribute
*ea
= to_ext_attr(attr
);
992 long new = simple_strtol(buf
, &end
, 0);
993 if (end
== buf
|| new > INT_MAX
|| new < INT_MIN
)
995 *(int *)(ea
->var
) = new;
996 /* Always return full write size even if we didn't consume all */
999 EXPORT_SYMBOL_GPL(device_store_int
);
1001 ssize_t
device_show_int(struct device
*dev
,
1002 struct device_attribute
*attr
,
1005 struct dev_ext_attribute
*ea
= to_ext_attr(attr
);
1007 return snprintf(buf
, PAGE_SIZE
, "%d\n", *(int *)(ea
->var
));
1009 EXPORT_SYMBOL_GPL(device_show_int
);
1011 ssize_t
device_store_bool(struct device
*dev
, struct device_attribute
*attr
,
1012 const char *buf
, size_t size
)
1014 struct dev_ext_attribute
*ea
= to_ext_attr(attr
);
1016 if (strtobool(buf
, ea
->var
) < 0)
1021 EXPORT_SYMBOL_GPL(device_store_bool
);
1023 ssize_t
device_show_bool(struct device
*dev
, struct device_attribute
*attr
,
1026 struct dev_ext_attribute
*ea
= to_ext_attr(attr
);
1028 return snprintf(buf
, PAGE_SIZE
, "%d\n", *(bool *)(ea
->var
));
1030 EXPORT_SYMBOL_GPL(device_show_bool
);
1033 * device_release - free device structure.
1034 * @kobj: device's kobject.
1036 * This is called once the reference count for the object
1037 * reaches 0. We forward the call to the device's release
1038 * method, which should handle actually freeing the structure.
1040 static void device_release(struct kobject
*kobj
)
1042 struct device
*dev
= kobj_to_dev(kobj
);
1043 struct device_private
*p
= dev
->p
;
1046 * Some platform devices are driven without driver attached
1047 * and managed resources may have been acquired. Make sure
1048 * all resources are released.
1050 * Drivers still can add resources into device after device
1051 * is deleted but alive, so release devres here to avoid
1052 * possible memory leak.
1054 devres_release_all(dev
);
1058 else if (dev
->type
&& dev
->type
->release
)
1059 dev
->type
->release(dev
);
1060 else if (dev
->class && dev
->class->dev_release
)
1061 dev
->class->dev_release(dev
);
1063 WARN(1, KERN_ERR
"Device '%s' does not have a release() "
1064 "function, it is broken and must be fixed.\n",
1069 static const void *device_namespace(struct kobject
*kobj
)
1071 struct device
*dev
= kobj_to_dev(kobj
);
1072 const void *ns
= NULL
;
1074 if (dev
->class && dev
->class->ns_type
)
1075 ns
= dev
->class->namespace(dev
);
1080 static void device_get_ownership(struct kobject
*kobj
, kuid_t
*uid
, kgid_t
*gid
)
1082 struct device
*dev
= kobj_to_dev(kobj
);
1084 if (dev
->class && dev
->class->get_ownership
)
1085 dev
->class->get_ownership(dev
, uid
, gid
);
1088 static struct kobj_type device_ktype
= {
1089 .release
= device_release
,
1090 .sysfs_ops
= &dev_sysfs_ops
,
1091 .namespace = device_namespace
,
1092 .get_ownership
= device_get_ownership
,
1096 static int dev_uevent_filter(struct kset
*kset
, struct kobject
*kobj
)
1098 struct kobj_type
*ktype
= get_ktype(kobj
);
1100 if (ktype
== &device_ktype
) {
1101 struct device
*dev
= kobj_to_dev(kobj
);
1110 static const char *dev_uevent_name(struct kset
*kset
, struct kobject
*kobj
)
1112 struct device
*dev
= kobj_to_dev(kobj
);
1115 return dev
->bus
->name
;
1117 return dev
->class->name
;
1121 static int dev_uevent(struct kset
*kset
, struct kobject
*kobj
,
1122 struct kobj_uevent_env
*env
)
1124 struct device
*dev
= kobj_to_dev(kobj
);
1127 /* add device node properties if present */
1128 if (MAJOR(dev
->devt
)) {
1132 kuid_t uid
= GLOBAL_ROOT_UID
;
1133 kgid_t gid
= GLOBAL_ROOT_GID
;
1135 add_uevent_var(env
, "MAJOR=%u", MAJOR(dev
->devt
));
1136 add_uevent_var(env
, "MINOR=%u", MINOR(dev
->devt
));
1137 name
= device_get_devnode(dev
, &mode
, &uid
, &gid
, &tmp
);
1139 add_uevent_var(env
, "DEVNAME=%s", name
);
1141 add_uevent_var(env
, "DEVMODE=%#o", mode
& 0777);
1142 if (!uid_eq(uid
, GLOBAL_ROOT_UID
))
1143 add_uevent_var(env
, "DEVUID=%u", from_kuid(&init_user_ns
, uid
));
1144 if (!gid_eq(gid
, GLOBAL_ROOT_GID
))
1145 add_uevent_var(env
, "DEVGID=%u", from_kgid(&init_user_ns
, gid
));
1150 if (dev
->type
&& dev
->type
->name
)
1151 add_uevent_var(env
, "DEVTYPE=%s", dev
->type
->name
);
1154 add_uevent_var(env
, "DRIVER=%s", dev
->driver
->name
);
1156 /* Add common DT information about the device */
1157 of_device_uevent(dev
, env
);
1159 /* have the bus specific function add its stuff */
1160 if (dev
->bus
&& dev
->bus
->uevent
) {
1161 retval
= dev
->bus
->uevent(dev
, env
);
1163 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
1164 dev_name(dev
), __func__
, retval
);
1167 /* have the class specific function add its stuff */
1168 if (dev
->class && dev
->class->dev_uevent
) {
1169 retval
= dev
->class->dev_uevent(dev
, env
);
1171 pr_debug("device: '%s': %s: class uevent() "
1172 "returned %d\n", dev_name(dev
),
1176 /* have the device type specific function add its stuff */
1177 if (dev
->type
&& dev
->type
->uevent
) {
1178 retval
= dev
->type
->uevent(dev
, env
);
1180 pr_debug("device: '%s': %s: dev_type uevent() "
1181 "returned %d\n", dev_name(dev
),
1188 static const struct kset_uevent_ops device_uevent_ops
= {
1189 .filter
= dev_uevent_filter
,
1190 .name
= dev_uevent_name
,
1191 .uevent
= dev_uevent
,
1194 static ssize_t
uevent_show(struct device
*dev
, struct device_attribute
*attr
,
1197 struct kobject
*top_kobj
;
1199 struct kobj_uevent_env
*env
= NULL
;
1204 /* search the kset, the device belongs to */
1205 top_kobj
= &dev
->kobj
;
1206 while (!top_kobj
->kset
&& top_kobj
->parent
)
1207 top_kobj
= top_kobj
->parent
;
1208 if (!top_kobj
->kset
)
1211 kset
= top_kobj
->kset
;
1212 if (!kset
->uevent_ops
|| !kset
->uevent_ops
->uevent
)
1215 /* respect filter */
1216 if (kset
->uevent_ops
&& kset
->uevent_ops
->filter
)
1217 if (!kset
->uevent_ops
->filter(kset
, &dev
->kobj
))
1220 env
= kzalloc(sizeof(struct kobj_uevent_env
), GFP_KERNEL
);
1224 /* let the kset specific function add its keys */
1225 retval
= kset
->uevent_ops
->uevent(kset
, &dev
->kobj
, env
);
1229 /* copy keys to file */
1230 for (i
= 0; i
< env
->envp_idx
; i
++)
1231 count
+= sprintf(&buf
[count
], "%s\n", env
->envp
[i
]);
1237 static ssize_t
uevent_store(struct device
*dev
, struct device_attribute
*attr
,
1238 const char *buf
, size_t count
)
1242 rc
= kobject_synth_uevent(&dev
->kobj
, buf
, count
);
1245 dev_err(dev
, "uevent: failed to send synthetic uevent\n");
1251 static DEVICE_ATTR_RW(uevent
);
1253 static ssize_t
online_show(struct device
*dev
, struct device_attribute
*attr
,
1259 val
= !dev
->offline
;
1261 return sprintf(buf
, "%u\n", val
);
1264 static ssize_t
online_store(struct device
*dev
, struct device_attribute
*attr
,
1265 const char *buf
, size_t count
)
1270 ret
= strtobool(buf
, &val
);
1274 ret
= lock_device_hotplug_sysfs();
1278 ret
= val
? device_online(dev
) : device_offline(dev
);
1279 unlock_device_hotplug();
1280 return ret
< 0 ? ret
: count
;
1282 static DEVICE_ATTR_RW(online
);
1284 int device_add_groups(struct device
*dev
, const struct attribute_group
**groups
)
1286 return sysfs_create_groups(&dev
->kobj
, groups
);
1288 EXPORT_SYMBOL_GPL(device_add_groups
);
1290 void device_remove_groups(struct device
*dev
,
1291 const struct attribute_group
**groups
)
1293 sysfs_remove_groups(&dev
->kobj
, groups
);
1295 EXPORT_SYMBOL_GPL(device_remove_groups
);
1297 union device_attr_group_devres
{
1298 const struct attribute_group
*group
;
1299 const struct attribute_group
**groups
;
1302 static int devm_attr_group_match(struct device
*dev
, void *res
, void *data
)
1304 return ((union device_attr_group_devres
*)res
)->group
== data
;
1307 static void devm_attr_group_remove(struct device
*dev
, void *res
)
1309 union device_attr_group_devres
*devres
= res
;
1310 const struct attribute_group
*group
= devres
->group
;
1312 dev_dbg(dev
, "%s: removing group %p\n", __func__
, group
);
1313 sysfs_remove_group(&dev
->kobj
, group
);
1316 static void devm_attr_groups_remove(struct device
*dev
, void *res
)
1318 union device_attr_group_devres
*devres
= res
;
1319 const struct attribute_group
**groups
= devres
->groups
;
1321 dev_dbg(dev
, "%s: removing groups %p\n", __func__
, groups
);
1322 sysfs_remove_groups(&dev
->kobj
, groups
);
1326 * devm_device_add_group - given a device, create a managed attribute group
1327 * @dev: The device to create the group for
1328 * @grp: The attribute group to create
1330 * This function creates a group for the first time. It will explicitly
1331 * warn and error if any of the attribute files being created already exist.
1333 * Returns 0 on success or error code on failure.
1335 int devm_device_add_group(struct device
*dev
, const struct attribute_group
*grp
)
1337 union device_attr_group_devres
*devres
;
1340 devres
= devres_alloc(devm_attr_group_remove
,
1341 sizeof(*devres
), GFP_KERNEL
);
1345 error
= sysfs_create_group(&dev
->kobj
, grp
);
1347 devres_free(devres
);
1351 devres
->group
= grp
;
1352 devres_add(dev
, devres
);
1355 EXPORT_SYMBOL_GPL(devm_device_add_group
);
1358 * devm_device_remove_group: remove a managed group from a device
1359 * @dev: device to remove the group from
1360 * @grp: group to remove
1362 * This function removes a group of attributes from a device. The attributes
1363 * previously have to have been created for this group, otherwise it will fail.
1365 void devm_device_remove_group(struct device
*dev
,
1366 const struct attribute_group
*grp
)
1368 WARN_ON(devres_release(dev
, devm_attr_group_remove
,
1369 devm_attr_group_match
,
1370 /* cast away const */ (void *)grp
));
1372 EXPORT_SYMBOL_GPL(devm_device_remove_group
);
1375 * devm_device_add_groups - create a bunch of managed attribute groups
1376 * @dev: The device to create the group for
1377 * @groups: The attribute groups to create, NULL terminated
1379 * This function creates a bunch of managed attribute groups. If an error
1380 * occurs when creating a group, all previously created groups will be
1381 * removed, unwinding everything back to the original state when this
1382 * function was called. It will explicitly warn and error if any of the
1383 * attribute files being created already exist.
1385 * Returns 0 on success or error code from sysfs_create_group on failure.
1387 int devm_device_add_groups(struct device
*dev
,
1388 const struct attribute_group
**groups
)
1390 union device_attr_group_devres
*devres
;
1393 devres
= devres_alloc(devm_attr_groups_remove
,
1394 sizeof(*devres
), GFP_KERNEL
);
1398 error
= sysfs_create_groups(&dev
->kobj
, groups
);
1400 devres_free(devres
);
1404 devres
->groups
= groups
;
1405 devres_add(dev
, devres
);
1408 EXPORT_SYMBOL_GPL(devm_device_add_groups
);
1411 * devm_device_remove_groups - remove a list of managed groups
1413 * @dev: The device for the groups to be removed from
1414 * @groups: NULL terminated list of groups to be removed
1416 * If groups is not NULL, remove the specified groups from the device.
1418 void devm_device_remove_groups(struct device
*dev
,
1419 const struct attribute_group
**groups
)
1421 WARN_ON(devres_release(dev
, devm_attr_groups_remove
,
1422 devm_attr_group_match
,
1423 /* cast away const */ (void *)groups
));
1425 EXPORT_SYMBOL_GPL(devm_device_remove_groups
);
1427 static int device_add_attrs(struct device
*dev
)
1429 struct class *class = dev
->class;
1430 const struct device_type
*type
= dev
->type
;
1434 error
= device_add_groups(dev
, class->dev_groups
);
1440 error
= device_add_groups(dev
, type
->groups
);
1442 goto err_remove_class_groups
;
1445 error
= device_add_groups(dev
, dev
->groups
);
1447 goto err_remove_type_groups
;
1449 if (device_supports_offline(dev
) && !dev
->offline_disabled
) {
1450 error
= device_create_file(dev
, &dev_attr_online
);
1452 goto err_remove_dev_groups
;
1457 err_remove_dev_groups
:
1458 device_remove_groups(dev
, dev
->groups
);
1459 err_remove_type_groups
:
1461 device_remove_groups(dev
, type
->groups
);
1462 err_remove_class_groups
:
1464 device_remove_groups(dev
, class->dev_groups
);
1469 static void device_remove_attrs(struct device
*dev
)
1471 struct class *class = dev
->class;
1472 const struct device_type
*type
= dev
->type
;
1474 device_remove_file(dev
, &dev_attr_online
);
1475 device_remove_groups(dev
, dev
->groups
);
1478 device_remove_groups(dev
, type
->groups
);
1481 device_remove_groups(dev
, class->dev_groups
);
1484 static ssize_t
dev_show(struct device
*dev
, struct device_attribute
*attr
,
1487 return print_dev_t(buf
, dev
->devt
);
1489 static DEVICE_ATTR_RO(dev
);
1492 struct kset
*devices_kset
;
1495 * devices_kset_move_before - Move device in the devices_kset's list.
1496 * @deva: Device to move.
1497 * @devb: Device @deva should come before.
1499 static void devices_kset_move_before(struct device
*deva
, struct device
*devb
)
1503 pr_debug("devices_kset: Moving %s before %s\n",
1504 dev_name(deva
), dev_name(devb
));
1505 spin_lock(&devices_kset
->list_lock
);
1506 list_move_tail(&deva
->kobj
.entry
, &devb
->kobj
.entry
);
1507 spin_unlock(&devices_kset
->list_lock
);
1511 * devices_kset_move_after - Move device in the devices_kset's list.
1512 * @deva: Device to move
1513 * @devb: Device @deva should come after.
1515 static void devices_kset_move_after(struct device
*deva
, struct device
*devb
)
1519 pr_debug("devices_kset: Moving %s after %s\n",
1520 dev_name(deva
), dev_name(devb
));
1521 spin_lock(&devices_kset
->list_lock
);
1522 list_move(&deva
->kobj
.entry
, &devb
->kobj
.entry
);
1523 spin_unlock(&devices_kset
->list_lock
);
1527 * devices_kset_move_last - move the device to the end of devices_kset's list.
1528 * @dev: device to move
1530 void devices_kset_move_last(struct device
*dev
)
1534 pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev
));
1535 spin_lock(&devices_kset
->list_lock
);
1536 list_move_tail(&dev
->kobj
.entry
, &devices_kset
->list
);
1537 spin_unlock(&devices_kset
->list_lock
);
1541 * device_create_file - create sysfs attribute file for device.
1543 * @attr: device attribute descriptor.
1545 int device_create_file(struct device
*dev
,
1546 const struct device_attribute
*attr
)
1551 WARN(((attr
->attr
.mode
& S_IWUGO
) && !attr
->store
),
1552 "Attribute %s: write permission without 'store'\n",
1554 WARN(((attr
->attr
.mode
& S_IRUGO
) && !attr
->show
),
1555 "Attribute %s: read permission without 'show'\n",
1557 error
= sysfs_create_file(&dev
->kobj
, &attr
->attr
);
1562 EXPORT_SYMBOL_GPL(device_create_file
);
1565 * device_remove_file - remove sysfs attribute file.
1567 * @attr: device attribute descriptor.
1569 void device_remove_file(struct device
*dev
,
1570 const struct device_attribute
*attr
)
1573 sysfs_remove_file(&dev
->kobj
, &attr
->attr
);
1575 EXPORT_SYMBOL_GPL(device_remove_file
);
1578 * device_remove_file_self - remove sysfs attribute file from its own method.
1580 * @attr: device attribute descriptor.
1582 * See kernfs_remove_self() for details.
1584 bool device_remove_file_self(struct device
*dev
,
1585 const struct device_attribute
*attr
)
1588 return sysfs_remove_file_self(&dev
->kobj
, &attr
->attr
);
1592 EXPORT_SYMBOL_GPL(device_remove_file_self
);
1595 * device_create_bin_file - create sysfs binary attribute file for device.
1597 * @attr: device binary attribute descriptor.
1599 int device_create_bin_file(struct device
*dev
,
1600 const struct bin_attribute
*attr
)
1602 int error
= -EINVAL
;
1604 error
= sysfs_create_bin_file(&dev
->kobj
, attr
);
1607 EXPORT_SYMBOL_GPL(device_create_bin_file
);
1610 * device_remove_bin_file - remove sysfs binary attribute file
1612 * @attr: device binary attribute descriptor.
1614 void device_remove_bin_file(struct device
*dev
,
1615 const struct bin_attribute
*attr
)
1618 sysfs_remove_bin_file(&dev
->kobj
, attr
);
1620 EXPORT_SYMBOL_GPL(device_remove_bin_file
);
1622 static void klist_children_get(struct klist_node
*n
)
1624 struct device_private
*p
= to_device_private_parent(n
);
1625 struct device
*dev
= p
->device
;
1630 static void klist_children_put(struct klist_node
*n
)
1632 struct device_private
*p
= to_device_private_parent(n
);
1633 struct device
*dev
= p
->device
;
1639 * device_initialize - init device structure.
1642 * This prepares the device for use by other layers by initializing
1644 * It is the first half of device_register(), if called by
1645 * that function, though it can also be called separately, so one
1646 * may use @dev's fields. In particular, get_device()/put_device()
1647 * may be used for reference counting of @dev after calling this
1650 * All fields in @dev must be initialized by the caller to 0, except
1651 * for those explicitly set to some other value. The simplest
1652 * approach is to use kzalloc() to allocate the structure containing
1655 * NOTE: Use put_device() to give up your reference instead of freeing
1656 * @dev directly once you have called this function.
1658 void device_initialize(struct device
*dev
)
1660 dev
->kobj
.kset
= devices_kset
;
1661 kobject_init(&dev
->kobj
, &device_ktype
);
1662 INIT_LIST_HEAD(&dev
->dma_pools
);
1663 mutex_init(&dev
->mutex
);
1664 lockdep_set_novalidate_class(&dev
->mutex
);
1665 spin_lock_init(&dev
->devres_lock
);
1666 INIT_LIST_HEAD(&dev
->devres_head
);
1667 device_pm_init(dev
);
1668 set_dev_node(dev
, -1);
1669 #ifdef CONFIG_GENERIC_MSI_IRQ
1670 INIT_LIST_HEAD(&dev
->msi_list
);
1672 INIT_LIST_HEAD(&dev
->links
.consumers
);
1673 INIT_LIST_HEAD(&dev
->links
.suppliers
);
1674 dev
->links
.status
= DL_DEV_NO_DRIVER
;
1676 EXPORT_SYMBOL_GPL(device_initialize
);
1678 struct kobject
*virtual_device_parent(struct device
*dev
)
1680 static struct kobject
*virtual_dir
= NULL
;
1683 virtual_dir
= kobject_create_and_add("virtual",
1684 &devices_kset
->kobj
);
1690 struct kobject kobj
;
1691 struct class *class;
1694 #define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
1696 static void class_dir_release(struct kobject
*kobj
)
1698 struct class_dir
*dir
= to_class_dir(kobj
);
1703 struct kobj_ns_type_operations
*class_dir_child_ns_type(struct kobject
*kobj
)
1705 struct class_dir
*dir
= to_class_dir(kobj
);
1706 return dir
->class->ns_type
;
1709 static struct kobj_type class_dir_ktype
= {
1710 .release
= class_dir_release
,
1711 .sysfs_ops
= &kobj_sysfs_ops
,
1712 .child_ns_type
= class_dir_child_ns_type
1715 static struct kobject
*
1716 class_dir_create_and_add(struct class *class, struct kobject
*parent_kobj
)
1718 struct class_dir
*dir
;
1721 dir
= kzalloc(sizeof(*dir
), GFP_KERNEL
);
1723 return ERR_PTR(-ENOMEM
);
1726 kobject_init(&dir
->kobj
, &class_dir_ktype
);
1728 dir
->kobj
.kset
= &class->p
->glue_dirs
;
1730 retval
= kobject_add(&dir
->kobj
, parent_kobj
, "%s", class->name
);
1732 kobject_put(&dir
->kobj
);
1733 return ERR_PTR(retval
);
1738 static DEFINE_MUTEX(gdp_mutex
);
1740 static struct kobject
*get_device_parent(struct device
*dev
,
1741 struct device
*parent
)
1744 struct kobject
*kobj
= NULL
;
1745 struct kobject
*parent_kobj
;
1749 /* block disks show up in /sys/block */
1750 if (sysfs_deprecated
&& dev
->class == &block_class
) {
1751 if (parent
&& parent
->class == &block_class
)
1752 return &parent
->kobj
;
1753 return &block_class
.p
->subsys
.kobj
;
1758 * If we have no parent, we live in "virtual".
1759 * Class-devices with a non class-device as parent, live
1760 * in a "glue" directory to prevent namespace collisions.
1763 parent_kobj
= virtual_device_parent(dev
);
1764 else if (parent
->class && !dev
->class->ns_type
)
1765 return &parent
->kobj
;
1767 parent_kobj
= &parent
->kobj
;
1769 mutex_lock(&gdp_mutex
);
1771 /* find our class-directory at the parent and reference it */
1772 spin_lock(&dev
->class->p
->glue_dirs
.list_lock
);
1773 list_for_each_entry(k
, &dev
->class->p
->glue_dirs
.list
, entry
)
1774 if (k
->parent
== parent_kobj
) {
1775 kobj
= kobject_get(k
);
1778 spin_unlock(&dev
->class->p
->glue_dirs
.list_lock
);
1780 mutex_unlock(&gdp_mutex
);
1784 /* or create a new class-directory at the parent device */
1785 k
= class_dir_create_and_add(dev
->class, parent_kobj
);
1786 /* do not emit an uevent for this simple "glue" directory */
1787 mutex_unlock(&gdp_mutex
);
1791 /* subsystems can specify a default root directory for their devices */
1792 if (!parent
&& dev
->bus
&& dev
->bus
->dev_root
)
1793 return &dev
->bus
->dev_root
->kobj
;
1796 return &parent
->kobj
;
1800 static inline bool live_in_glue_dir(struct kobject
*kobj
,
1803 if (!kobj
|| !dev
->class ||
1804 kobj
->kset
!= &dev
->class->p
->glue_dirs
)
1809 static inline struct kobject
*get_glue_dir(struct device
*dev
)
1811 return dev
->kobj
.parent
;
1815 * make sure cleaning up dir as the last step, we need to make
1816 * sure .release handler of kobject is run with holding the
1819 static void cleanup_glue_dir(struct device
*dev
, struct kobject
*glue_dir
)
1823 /* see if we live in a "glue" directory */
1824 if (!live_in_glue_dir(glue_dir
, dev
))
1827 mutex_lock(&gdp_mutex
);
1829 * There is a race condition between removing glue directory
1830 * and adding a new device under the glue directory.
1835 * get_device_parent()
1836 * class_dir_create_and_add()
1837 * kobject_add_internal()
1838 * create_dir() // create glue_dir
1841 * get_device_parent()
1842 * kobject_get() // get glue_dir
1845 * cleanup_glue_dir()
1846 * kobject_del(glue_dir)
1849 * kobject_add_internal()
1850 * create_dir() // in glue_dir
1851 * sysfs_create_dir_ns()
1852 * kernfs_create_dir_ns(sd)
1854 * sysfs_remove_dir() // glue_dir->sd=NULL
1855 * sysfs_put() // free glue_dir->sd
1858 * kernfs_new_node(sd)
1859 * kernfs_get(glue_dir)
1863 * Before CPU1 remove last child device under glue dir, if CPU2 add
1864 * a new device under glue dir, the glue_dir kobject reference count
1865 * will be increase to 2 in kobject_get(k). And CPU2 has been called
1866 * kernfs_create_dir_ns(). Meanwhile, CPU1 call sysfs_remove_dir()
1867 * and sysfs_put(). This result in glue_dir->sd is freed.
1869 * Then the CPU2 will see a stale "empty" but still potentially used
1870 * glue dir around in kernfs_new_node().
1872 * In order to avoid this happening, we also should make sure that
1873 * kernfs_node for glue_dir is released in CPU1 only when refcount
1874 * for glue_dir kobj is 1.
1876 ref
= kref_read(&glue_dir
->kref
);
1877 if (!kobject_has_children(glue_dir
) && !--ref
)
1878 kobject_del(glue_dir
);
1879 kobject_put(glue_dir
);
1880 mutex_unlock(&gdp_mutex
);
1883 static int device_add_class_symlinks(struct device
*dev
)
1885 struct device_node
*of_node
= dev_of_node(dev
);
1889 error
= sysfs_create_link(&dev
->kobj
, of_node_kobj(of_node
), "of_node");
1891 dev_warn(dev
, "Error %d creating of_node link\n",error
);
1892 /* An error here doesn't warrant bringing down the device */
1898 error
= sysfs_create_link(&dev
->kobj
,
1899 &dev
->class->p
->subsys
.kobj
,
1904 if (dev
->parent
&& device_is_not_partition(dev
)) {
1905 error
= sysfs_create_link(&dev
->kobj
, &dev
->parent
->kobj
,
1912 /* /sys/block has directories and does not need symlinks */
1913 if (sysfs_deprecated
&& dev
->class == &block_class
)
1917 /* link in the class directory pointing to the device */
1918 error
= sysfs_create_link(&dev
->class->p
->subsys
.kobj
,
1919 &dev
->kobj
, dev_name(dev
));
1926 sysfs_remove_link(&dev
->kobj
, "device");
1929 sysfs_remove_link(&dev
->kobj
, "subsystem");
1931 sysfs_remove_link(&dev
->kobj
, "of_node");
1935 static void device_remove_class_symlinks(struct device
*dev
)
1937 if (dev_of_node(dev
))
1938 sysfs_remove_link(&dev
->kobj
, "of_node");
1943 if (dev
->parent
&& device_is_not_partition(dev
))
1944 sysfs_remove_link(&dev
->kobj
, "device");
1945 sysfs_remove_link(&dev
->kobj
, "subsystem");
1947 if (sysfs_deprecated
&& dev
->class == &block_class
)
1950 sysfs_delete_link(&dev
->class->p
->subsys
.kobj
, &dev
->kobj
, dev_name(dev
));
1954 * dev_set_name - set a device name
1956 * @fmt: format string for the device's name
1958 int dev_set_name(struct device
*dev
, const char *fmt
, ...)
1963 va_start(vargs
, fmt
);
1964 err
= kobject_set_name_vargs(&dev
->kobj
, fmt
, vargs
);
1968 EXPORT_SYMBOL_GPL(dev_set_name
);
1971 * device_to_dev_kobj - select a /sys/dev/ directory for the device
1974 * By default we select char/ for new entries. Setting class->dev_obj
1975 * to NULL prevents an entry from being created. class->dev_kobj must
1976 * be set (or cleared) before any devices are registered to the class
1977 * otherwise device_create_sys_dev_entry() and
1978 * device_remove_sys_dev_entry() will disagree about the presence of
1981 static struct kobject
*device_to_dev_kobj(struct device
*dev
)
1983 struct kobject
*kobj
;
1986 kobj
= dev
->class->dev_kobj
;
1988 kobj
= sysfs_dev_char_kobj
;
1993 static int device_create_sys_dev_entry(struct device
*dev
)
1995 struct kobject
*kobj
= device_to_dev_kobj(dev
);
2000 format_dev_t(devt_str
, dev
->devt
);
2001 error
= sysfs_create_link(kobj
, &dev
->kobj
, devt_str
);
2007 static void device_remove_sys_dev_entry(struct device
*dev
)
2009 struct kobject
*kobj
= device_to_dev_kobj(dev
);
2013 format_dev_t(devt_str
, dev
->devt
);
2014 sysfs_remove_link(kobj
, devt_str
);
2018 static int device_private_init(struct device
*dev
)
2020 dev
->p
= kzalloc(sizeof(*dev
->p
), GFP_KERNEL
);
2023 dev
->p
->device
= dev
;
2024 klist_init(&dev
->p
->klist_children
, klist_children_get
,
2025 klist_children_put
);
2026 INIT_LIST_HEAD(&dev
->p
->deferred_probe
);
2031 * device_add - add device to device hierarchy.
2034 * This is part 2 of device_register(), though may be called
2035 * separately _iff_ device_initialize() has been called separately.
2037 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
2038 * to the global and sibling lists for the device, then
2039 * adds it to the other relevant subsystems of the driver model.
2041 * Do not call this routine or device_register() more than once for
2042 * any device structure. The driver model core is not designed to work
2043 * with devices that get unregistered and then spring back to life.
2044 * (Among other things, it's very hard to guarantee that all references
2045 * to the previous incarnation of @dev have been dropped.) Allocate
2046 * and register a fresh new struct device instead.
2048 * NOTE: _Never_ directly free @dev after calling this function, even
2049 * if it returned an error! Always use put_device() to give up your
2050 * reference instead.
2052 int device_add(struct device
*dev
)
2054 struct device
*parent
;
2055 struct kobject
*kobj
;
2056 struct class_interface
*class_intf
;
2057 int error
= -EINVAL
;
2058 struct kobject
*glue_dir
= NULL
;
2060 dev
= get_device(dev
);
2065 error
= device_private_init(dev
);
2071 * for statically allocated devices, which should all be converted
2072 * some day, we need to initialize the name. We prevent reading back
2073 * the name, and force the use of dev_name()
2075 if (dev
->init_name
) {
2076 dev_set_name(dev
, "%s", dev
->init_name
);
2077 dev
->init_name
= NULL
;
2080 /* subsystems can specify simple device enumeration */
2081 if (!dev_name(dev
) && dev
->bus
&& dev
->bus
->dev_name
)
2082 dev_set_name(dev
, "%s%u", dev
->bus
->dev_name
, dev
->id
);
2084 if (!dev_name(dev
)) {
2089 pr_debug("device: '%s': %s\n", dev_name(dev
), __func__
);
2091 parent
= get_device(dev
->parent
);
2092 kobj
= get_device_parent(dev
, parent
);
2094 error
= PTR_ERR(kobj
);
2098 dev
->kobj
.parent
= kobj
;
2100 /* use parent numa_node */
2101 if (parent
&& (dev_to_node(dev
) == NUMA_NO_NODE
))
2102 set_dev_node(dev
, dev_to_node(parent
));
2104 /* first, register with generic layer. */
2105 /* we require the name to be set before, and pass NULL */
2106 error
= kobject_add(&dev
->kobj
, dev
->kobj
.parent
, NULL
);
2108 glue_dir
= get_glue_dir(dev
);
2112 /* notify platform of device entry */
2113 if (platform_notify
)
2114 platform_notify(dev
);
2116 error
= device_create_file(dev
, &dev_attr_uevent
);
2120 error
= device_add_class_symlinks(dev
);
2123 error
= device_add_attrs(dev
);
2126 error
= bus_add_device(dev
);
2129 error
= dpm_sysfs_add(dev
);
2134 if (MAJOR(dev
->devt
)) {
2135 error
= device_create_file(dev
, &dev_attr_dev
);
2139 error
= device_create_sys_dev_entry(dev
);
2143 devtmpfs_create_node(dev
);
2146 /* Notify clients of device addition. This call must come
2147 * after dpm_sysfs_add() and before kobject_uevent().
2150 blocking_notifier_call_chain(&dev
->bus
->p
->bus_notifier
,
2151 BUS_NOTIFY_ADD_DEVICE
, dev
);
2153 kobject_uevent(&dev
->kobj
, KOBJ_ADD
);
2154 bus_probe_device(dev
);
2156 klist_add_tail(&dev
->p
->knode_parent
,
2157 &parent
->p
->klist_children
);
2160 mutex_lock(&dev
->class->p
->mutex
);
2161 /* tie the class to the device */
2162 klist_add_tail(&dev
->knode_class
,
2163 &dev
->class->p
->klist_devices
);
2165 /* notify any interfaces that the device is here */
2166 list_for_each_entry(class_intf
,
2167 &dev
->class->p
->interfaces
, node
)
2168 if (class_intf
->add_dev
)
2169 class_intf
->add_dev(dev
, class_intf
);
2170 mutex_unlock(&dev
->class->p
->mutex
);
2176 if (MAJOR(dev
->devt
))
2177 device_remove_file(dev
, &dev_attr_dev
);
2179 device_pm_remove(dev
);
2180 dpm_sysfs_remove(dev
);
2182 bus_remove_device(dev
);
2184 device_remove_attrs(dev
);
2186 device_remove_class_symlinks(dev
);
2188 device_remove_file(dev
, &dev_attr_uevent
);
2190 kobject_uevent(&dev
->kobj
, KOBJ_REMOVE
);
2191 glue_dir
= get_glue_dir(dev
);
2192 kobject_del(&dev
->kobj
);
2194 cleanup_glue_dir(dev
, glue_dir
);
2202 EXPORT_SYMBOL_GPL(device_add
);
2205 * device_register - register a device with the system.
2206 * @dev: pointer to the device structure
2208 * This happens in two clean steps - initialize the device
2209 * and add it to the system. The two steps can be called
2210 * separately, but this is the easiest and most common.
2211 * I.e. you should only call the two helpers separately if
2212 * have a clearly defined need to use and refcount the device
2213 * before it is added to the hierarchy.
2215 * For more information, see the kerneldoc for device_initialize()
2218 * NOTE: _Never_ directly free @dev after calling this function, even
2219 * if it returned an error! Always use put_device() to give up the
2220 * reference initialized in this function instead.
2222 int device_register(struct device
*dev
)
2224 device_initialize(dev
);
2225 return device_add(dev
);
2227 EXPORT_SYMBOL_GPL(device_register
);
2230 * get_device - increment reference count for device.
2233 * This simply forwards the call to kobject_get(), though
2234 * we do take care to provide for the case that we get a NULL
2235 * pointer passed in.
2237 struct device
*get_device(struct device
*dev
)
2239 return dev
? kobj_to_dev(kobject_get(&dev
->kobj
)) : NULL
;
2241 EXPORT_SYMBOL_GPL(get_device
);
2244 * put_device - decrement reference count.
2245 * @dev: device in question.
2247 void put_device(struct device
*dev
)
2249 /* might_sleep(); */
2251 kobject_put(&dev
->kobj
);
2253 EXPORT_SYMBOL_GPL(put_device
);
2255 bool kill_device(struct device
*dev
)
2258 * Require the device lock and set the "dead" flag to guarantee that
2259 * the update behavior is consistent with the other bitfields near
2260 * it and that we cannot have an asynchronous probe routine trying
2261 * to run while we are tearing out the bus/class/sysfs from
2262 * underneath the device.
2264 lockdep_assert_held(&dev
->mutex
);
2268 dev
->p
->dead
= true;
2271 EXPORT_SYMBOL_GPL(kill_device
);
2274 * device_del - delete device from system.
2277 * This is the first part of the device unregistration
2278 * sequence. This removes the device from the lists we control
2279 * from here, has it removed from the other driver model
2280 * subsystems it was added to in device_add(), and removes it
2281 * from the kobject hierarchy.
2283 * NOTE: this should be called manually _iff_ device_add() was
2284 * also called manually.
2286 void device_del(struct device
*dev
)
2288 struct device
*parent
= dev
->parent
;
2289 struct kobject
*glue_dir
= NULL
;
2290 struct class_interface
*class_intf
;
2296 /* Notify clients of device removal. This call must come
2297 * before dpm_sysfs_remove().
2300 blocking_notifier_call_chain(&dev
->bus
->p
->bus_notifier
,
2301 BUS_NOTIFY_DEL_DEVICE
, dev
);
2303 dpm_sysfs_remove(dev
);
2305 klist_del(&dev
->p
->knode_parent
);
2306 if (MAJOR(dev
->devt
)) {
2307 devtmpfs_delete_node(dev
);
2308 device_remove_sys_dev_entry(dev
);
2309 device_remove_file(dev
, &dev_attr_dev
);
2312 device_remove_class_symlinks(dev
);
2314 mutex_lock(&dev
->class->p
->mutex
);
2315 /* notify any interfaces that the device is now gone */
2316 list_for_each_entry(class_intf
,
2317 &dev
->class->p
->interfaces
, node
)
2318 if (class_intf
->remove_dev
)
2319 class_intf
->remove_dev(dev
, class_intf
);
2320 /* remove the device from the class list */
2321 klist_del(&dev
->knode_class
);
2322 mutex_unlock(&dev
->class->p
->mutex
);
2324 device_remove_file(dev
, &dev_attr_uevent
);
2325 device_remove_attrs(dev
);
2326 bus_remove_device(dev
);
2327 device_pm_remove(dev
);
2328 driver_deferred_probe_del(dev
);
2329 device_remove_properties(dev
);
2330 device_links_purge(dev
);
2332 /* Notify the platform of the removal, in case they
2333 * need to do anything...
2335 if (platform_notify_remove
)
2336 platform_notify_remove(dev
);
2338 blocking_notifier_call_chain(&dev
->bus
->p
->bus_notifier
,
2339 BUS_NOTIFY_REMOVED_DEVICE
, dev
);
2340 kobject_uevent(&dev
->kobj
, KOBJ_REMOVE
);
2341 glue_dir
= get_glue_dir(dev
);
2342 kobject_del(&dev
->kobj
);
2343 cleanup_glue_dir(dev
, glue_dir
);
2346 EXPORT_SYMBOL_GPL(device_del
);
2349 * device_unregister - unregister device from system.
2350 * @dev: device going away.
2352 * We do this in two parts, like we do device_register(). First,
2353 * we remove it from all the subsystems with device_del(), then
2354 * we decrement the reference count via put_device(). If that
2355 * is the final reference count, the device will be cleaned up
2356 * via device_release() above. Otherwise, the structure will
2357 * stick around until the final reference to the device is dropped.
2359 void device_unregister(struct device
*dev
)
2361 pr_debug("device: '%s': %s\n", dev_name(dev
), __func__
);
2365 EXPORT_SYMBOL_GPL(device_unregister
);
2367 static struct device
*prev_device(struct klist_iter
*i
)
2369 struct klist_node
*n
= klist_prev(i
);
2370 struct device
*dev
= NULL
;
2371 struct device_private
*p
;
2374 p
= to_device_private_parent(n
);
2380 static struct device
*next_device(struct klist_iter
*i
)
2382 struct klist_node
*n
= klist_next(i
);
2383 struct device
*dev
= NULL
;
2384 struct device_private
*p
;
2387 p
= to_device_private_parent(n
);
2394 * device_get_devnode - path of device node file
2396 * @mode: returned file access mode
2397 * @uid: returned file owner
2398 * @gid: returned file group
2399 * @tmp: possibly allocated string
2401 * Return the relative path of a possible device node.
2402 * Non-default names may need to allocate a memory to compose
2403 * a name. This memory is returned in tmp and needs to be
2404 * freed by the caller.
2406 const char *device_get_devnode(struct device
*dev
,
2407 umode_t
*mode
, kuid_t
*uid
, kgid_t
*gid
,
2414 /* the device type may provide a specific name */
2415 if (dev
->type
&& dev
->type
->devnode
)
2416 *tmp
= dev
->type
->devnode(dev
, mode
, uid
, gid
);
2420 /* the class may provide a specific name */
2421 if (dev
->class && dev
->class->devnode
)
2422 *tmp
= dev
->class->devnode(dev
, mode
);
2426 /* return name without allocation, tmp == NULL */
2427 if (strchr(dev_name(dev
), '!') == NULL
)
2428 return dev_name(dev
);
2430 /* replace '!' in the name with '/' */
2431 s
= kstrdup(dev_name(dev
), GFP_KERNEL
);
2434 strreplace(s
, '!', '/');
2439 * device_for_each_child - device child iterator.
2440 * @parent: parent struct device.
2441 * @fn: function to be called for each device.
2442 * @data: data for the callback.
2444 * Iterate over @parent's child devices, and call @fn for each,
2447 * We check the return of @fn each time. If it returns anything
2448 * other than 0, we break out and return that value.
2450 int device_for_each_child(struct device
*parent
, void *data
,
2451 int (*fn
)(struct device
*dev
, void *data
))
2453 struct klist_iter i
;
2454 struct device
*child
;
2460 klist_iter_init(&parent
->p
->klist_children
, &i
);
2461 while (!error
&& (child
= next_device(&i
)))
2462 error
= fn(child
, data
);
2463 klist_iter_exit(&i
);
2466 EXPORT_SYMBOL_GPL(device_for_each_child
);
2469 * device_for_each_child_reverse - device child iterator in reversed order.
2470 * @parent: parent struct device.
2471 * @fn: function to be called for each device.
2472 * @data: data for the callback.
2474 * Iterate over @parent's child devices, and call @fn for each,
2477 * We check the return of @fn each time. If it returns anything
2478 * other than 0, we break out and return that value.
2480 int device_for_each_child_reverse(struct device
*parent
, void *data
,
2481 int (*fn
)(struct device
*dev
, void *data
))
2483 struct klist_iter i
;
2484 struct device
*child
;
2490 klist_iter_init(&parent
->p
->klist_children
, &i
);
2491 while ((child
= prev_device(&i
)) && !error
)
2492 error
= fn(child
, data
);
2493 klist_iter_exit(&i
);
2496 EXPORT_SYMBOL_GPL(device_for_each_child_reverse
);
2499 * device_find_child - device iterator for locating a particular device.
2500 * @parent: parent struct device
2501 * @match: Callback function to check device
2502 * @data: Data to pass to match function
2504 * This is similar to the device_for_each_child() function above, but it
2505 * returns a reference to a device that is 'found' for later use, as
2506 * determined by the @match callback.
2508 * The callback should return 0 if the device doesn't match and non-zero
2509 * if it does. If the callback returns non-zero and a reference to the
2510 * current device can be obtained, this function will return to the caller
2511 * and not iterate over any more devices.
2513 * NOTE: you will need to drop the reference with put_device() after use.
2515 struct device
*device_find_child(struct device
*parent
, void *data
,
2516 int (*match
)(struct device
*dev
, void *data
))
2518 struct klist_iter i
;
2519 struct device
*child
;
2524 klist_iter_init(&parent
->p
->klist_children
, &i
);
2525 while ((child
= next_device(&i
)))
2526 if (match(child
, data
) && get_device(child
))
2528 klist_iter_exit(&i
);
2531 EXPORT_SYMBOL_GPL(device_find_child
);
2533 int __init
devices_init(void)
2535 devices_kset
= kset_create_and_add("devices", &device_uevent_ops
, NULL
);
2538 dev_kobj
= kobject_create_and_add("dev", NULL
);
2541 sysfs_dev_block_kobj
= kobject_create_and_add("block", dev_kobj
);
2542 if (!sysfs_dev_block_kobj
)
2543 goto block_kobj_err
;
2544 sysfs_dev_char_kobj
= kobject_create_and_add("char", dev_kobj
);
2545 if (!sysfs_dev_char_kobj
)
2551 kobject_put(sysfs_dev_block_kobj
);
2553 kobject_put(dev_kobj
);
2555 kset_unregister(devices_kset
);
2559 static int device_check_offline(struct device
*dev
, void *not_used
)
2563 ret
= device_for_each_child(dev
, NULL
, device_check_offline
);
2567 return device_supports_offline(dev
) && !dev
->offline
? -EBUSY
: 0;
2571 * device_offline - Prepare the device for hot-removal.
2572 * @dev: Device to be put offline.
2574 * Execute the device bus type's .offline() callback, if present, to prepare
2575 * the device for a subsequent hot-removal. If that succeeds, the device must
2576 * not be used until either it is removed or its bus type's .online() callback
2579 * Call under device_hotplug_lock.
2581 int device_offline(struct device
*dev
)
2585 if (dev
->offline_disabled
)
2588 ret
= device_for_each_child(dev
, NULL
, device_check_offline
);
2593 if (device_supports_offline(dev
)) {
2597 ret
= dev
->bus
->offline(dev
);
2599 kobject_uevent(&dev
->kobj
, KOBJ_OFFLINE
);
2600 dev
->offline
= true;
2610 * device_online - Put the device back online after successful device_offline().
2611 * @dev: Device to be put back online.
2613 * If device_offline() has been successfully executed for @dev, but the device
2614 * has not been removed subsequently, execute its bus type's .online() callback
2615 * to indicate that the device can be used again.
2617 * Call under device_hotplug_lock.
2619 int device_online(struct device
*dev
)
2624 if (device_supports_offline(dev
)) {
2626 ret
= dev
->bus
->online(dev
);
2628 kobject_uevent(&dev
->kobj
, KOBJ_ONLINE
);
2629 dev
->offline
= false;
2640 struct root_device
{
2642 struct module
*owner
;
2645 static inline struct root_device
*to_root_device(struct device
*d
)
2647 return container_of(d
, struct root_device
, dev
);
2650 static void root_device_release(struct device
*dev
)
2652 kfree(to_root_device(dev
));
2656 * __root_device_register - allocate and register a root device
2657 * @name: root device name
2658 * @owner: owner module of the root device, usually THIS_MODULE
2660 * This function allocates a root device and registers it
2661 * using device_register(). In order to free the returned
2662 * device, use root_device_unregister().
2664 * Root devices are dummy devices which allow other devices
2665 * to be grouped under /sys/devices. Use this function to
2666 * allocate a root device and then use it as the parent of
2667 * any device which should appear under /sys/devices/{name}
2669 * The /sys/devices/{name} directory will also contain a
2670 * 'module' symlink which points to the @owner directory
2673 * Returns &struct device pointer on success, or ERR_PTR() on error.
2675 * Note: You probably want to use root_device_register().
2677 struct device
*__root_device_register(const char *name
, struct module
*owner
)
2679 struct root_device
*root
;
2682 root
= kzalloc(sizeof(struct root_device
), GFP_KERNEL
);
2684 return ERR_PTR(err
);
2686 err
= dev_set_name(&root
->dev
, "%s", name
);
2689 return ERR_PTR(err
);
2692 root
->dev
.release
= root_device_release
;
2694 err
= device_register(&root
->dev
);
2696 put_device(&root
->dev
);
2697 return ERR_PTR(err
);
2700 #ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
2702 struct module_kobject
*mk
= &owner
->mkobj
;
2704 err
= sysfs_create_link(&root
->dev
.kobj
, &mk
->kobj
, "module");
2706 device_unregister(&root
->dev
);
2707 return ERR_PTR(err
);
2709 root
->owner
= owner
;
2715 EXPORT_SYMBOL_GPL(__root_device_register
);
2718 * root_device_unregister - unregister and free a root device
2719 * @dev: device going away
2721 * This function unregisters and cleans up a device that was created by
2722 * root_device_register().
2724 void root_device_unregister(struct device
*dev
)
2726 struct root_device
*root
= to_root_device(dev
);
2729 sysfs_remove_link(&root
->dev
.kobj
, "module");
2731 device_unregister(dev
);
2733 EXPORT_SYMBOL_GPL(root_device_unregister
);
2736 static void device_create_release(struct device
*dev
)
2738 pr_debug("device: '%s': %s\n", dev_name(dev
), __func__
);
2742 static __printf(6, 0) struct device
*
2743 device_create_groups_vargs(struct class *class, struct device
*parent
,
2744 dev_t devt
, void *drvdata
,
2745 const struct attribute_group
**groups
,
2746 const char *fmt
, va_list args
)
2748 struct device
*dev
= NULL
;
2749 int retval
= -ENODEV
;
2751 if (class == NULL
|| IS_ERR(class))
2754 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
2760 device_initialize(dev
);
2763 dev
->parent
= parent
;
2764 dev
->groups
= groups
;
2765 dev
->release
= device_create_release
;
2766 dev_set_drvdata(dev
, drvdata
);
2768 retval
= kobject_set_name_vargs(&dev
->kobj
, fmt
, args
);
2772 retval
= device_add(dev
);
2780 return ERR_PTR(retval
);
2784 * device_create_vargs - creates a device and registers it with sysfs
2785 * @class: pointer to the struct class that this device should be registered to
2786 * @parent: pointer to the parent struct device of this new device, if any
2787 * @devt: the dev_t for the char device to be added
2788 * @drvdata: the data to be added to the device for callbacks
2789 * @fmt: string for the device's name
2790 * @args: va_list for the device's name
2792 * This function can be used by char device classes. A struct device
2793 * will be created in sysfs, registered to the specified class.
2795 * A "dev" file will be created, showing the dev_t for the device, if
2796 * the dev_t is not 0,0.
2797 * If a pointer to a parent struct device is passed in, the newly created
2798 * struct device will be a child of that device in sysfs.
2799 * The pointer to the struct device will be returned from the call.
2800 * Any further sysfs files that might be required can be created using this
2803 * Returns &struct device pointer on success, or ERR_PTR() on error.
2805 * Note: the struct class passed to this function must have previously
2806 * been created with a call to class_create().
2808 struct device
*device_create_vargs(struct class *class, struct device
*parent
,
2809 dev_t devt
, void *drvdata
, const char *fmt
,
2812 return device_create_groups_vargs(class, parent
, devt
, drvdata
, NULL
,
2815 EXPORT_SYMBOL_GPL(device_create_vargs
);
2818 * device_create - creates a device and registers it with sysfs
2819 * @class: pointer to the struct class that this device should be registered to
2820 * @parent: pointer to the parent struct device of this new device, if any
2821 * @devt: the dev_t for the char device to be added
2822 * @drvdata: the data to be added to the device for callbacks
2823 * @fmt: string for the device's name
2825 * This function can be used by char device classes. A struct device
2826 * will be created in sysfs, registered to the specified class.
2828 * A "dev" file will be created, showing the dev_t for the device, if
2829 * the dev_t is not 0,0.
2830 * If a pointer to a parent struct device is passed in, the newly created
2831 * struct device will be a child of that device in sysfs.
2832 * The pointer to the struct device will be returned from the call.
2833 * Any further sysfs files that might be required can be created using this
2836 * Returns &struct device pointer on success, or ERR_PTR() on error.
2838 * Note: the struct class passed to this function must have previously
2839 * been created with a call to class_create().
2841 struct device
*device_create(struct class *class, struct device
*parent
,
2842 dev_t devt
, void *drvdata
, const char *fmt
, ...)
2847 va_start(vargs
, fmt
);
2848 dev
= device_create_vargs(class, parent
, devt
, drvdata
, fmt
, vargs
);
2852 EXPORT_SYMBOL_GPL(device_create
);
2855 * device_create_with_groups - creates a device and registers it with sysfs
2856 * @class: pointer to the struct class that this device should be registered to
2857 * @parent: pointer to the parent struct device of this new device, if any
2858 * @devt: the dev_t for the char device to be added
2859 * @drvdata: the data to be added to the device for callbacks
2860 * @groups: NULL-terminated list of attribute groups to be created
2861 * @fmt: string for the device's name
2863 * This function can be used by char device classes. A struct device
2864 * will be created in sysfs, registered to the specified class.
2865 * Additional attributes specified in the groups parameter will also
2866 * be created automatically.
2868 * A "dev" file will be created, showing the dev_t for the device, if
2869 * the dev_t is not 0,0.
2870 * If a pointer to a parent struct device is passed in, the newly created
2871 * struct device will be a child of that device in sysfs.
2872 * The pointer to the struct device will be returned from the call.
2873 * Any further sysfs files that might be required can be created using this
2876 * Returns &struct device pointer on success, or ERR_PTR() on error.
2878 * Note: the struct class passed to this function must have previously
2879 * been created with a call to class_create().
2881 struct device
*device_create_with_groups(struct class *class,
2882 struct device
*parent
, dev_t devt
,
2884 const struct attribute_group
**groups
,
2885 const char *fmt
, ...)
2890 va_start(vargs
, fmt
);
2891 dev
= device_create_groups_vargs(class, parent
, devt
, drvdata
, groups
,
2896 EXPORT_SYMBOL_GPL(device_create_with_groups
);
2898 static int __match_devt(struct device
*dev
, const void *data
)
2900 const dev_t
*devt
= data
;
2902 return dev
->devt
== *devt
;
2906 * device_destroy - removes a device that was created with device_create()
2907 * @class: pointer to the struct class that this device was registered with
2908 * @devt: the dev_t of the device that was previously registered
2910 * This call unregisters and cleans up a device that was created with a
2911 * call to device_create().
2913 void device_destroy(struct class *class, dev_t devt
)
2917 dev
= class_find_device(class, NULL
, &devt
, __match_devt
);
2920 device_unregister(dev
);
2923 EXPORT_SYMBOL_GPL(device_destroy
);
2926 * device_rename - renames a device
2927 * @dev: the pointer to the struct device to be renamed
2928 * @new_name: the new name of the device
2930 * It is the responsibility of the caller to provide mutual
2931 * exclusion between two different calls of device_rename
2932 * on the same device to ensure that new_name is valid and
2933 * won't conflict with other devices.
2935 * Note: Don't call this function. Currently, the networking layer calls this
2936 * function, but that will change. The following text from Kay Sievers offers
2939 * Renaming devices is racy at many levels, symlinks and other stuff are not
2940 * replaced atomically, and you get a "move" uevent, but it's not easy to
2941 * connect the event to the old and new device. Device nodes are not renamed at
2942 * all, there isn't even support for that in the kernel now.
2944 * In the meantime, during renaming, your target name might be taken by another
2945 * driver, creating conflicts. Or the old name is taken directly after you
2946 * renamed it -- then you get events for the same DEVPATH, before you even see
2947 * the "move" event. It's just a mess, and nothing new should ever rely on
2948 * kernel device renaming. Besides that, it's not even implemented now for
2949 * other things than (driver-core wise very simple) network devices.
2951 * We are currently about to change network renaming in udev to completely
2952 * disallow renaming of devices in the same namespace as the kernel uses,
2953 * because we can't solve the problems properly, that arise with swapping names
2954 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
2955 * be allowed to some other name than eth[0-9]*, for the aforementioned
2958 * Make up a "real" name in the driver before you register anything, or add
2959 * some other attributes for userspace to find the device, or use udev to add
2960 * symlinks -- but never rename kernel devices later, it's a complete mess. We
2961 * don't even want to get into that and try to implement the missing pieces in
2962 * the core. We really have other pieces to fix in the driver core mess. :)
2964 int device_rename(struct device
*dev
, const char *new_name
)
2966 struct kobject
*kobj
= &dev
->kobj
;
2967 char *old_device_name
= NULL
;
2970 dev
= get_device(dev
);
2974 dev_dbg(dev
, "renaming to %s\n", new_name
);
2976 old_device_name
= kstrdup(dev_name(dev
), GFP_KERNEL
);
2977 if (!old_device_name
) {
2983 error
= sysfs_rename_link_ns(&dev
->class->p
->subsys
.kobj
,
2984 kobj
, old_device_name
,
2985 new_name
, kobject_namespace(kobj
));
2990 error
= kobject_rename(kobj
, new_name
);
2997 kfree(old_device_name
);
3001 EXPORT_SYMBOL_GPL(device_rename
);
3003 static int device_move_class_links(struct device
*dev
,
3004 struct device
*old_parent
,
3005 struct device
*new_parent
)
3010 sysfs_remove_link(&dev
->kobj
, "device");
3012 error
= sysfs_create_link(&dev
->kobj
, &new_parent
->kobj
,
3018 * device_move - moves a device to a new parent
3019 * @dev: the pointer to the struct device to be moved
3020 * @new_parent: the new parent of the device (can be NULL)
3021 * @dpm_order: how to reorder the dpm_list
3023 int device_move(struct device
*dev
, struct device
*new_parent
,
3024 enum dpm_order dpm_order
)
3027 struct device
*old_parent
;
3028 struct kobject
*new_parent_kobj
;
3030 dev
= get_device(dev
);
3035 new_parent
= get_device(new_parent
);
3036 new_parent_kobj
= get_device_parent(dev
, new_parent
);
3037 if (IS_ERR(new_parent_kobj
)) {
3038 error
= PTR_ERR(new_parent_kobj
);
3039 put_device(new_parent
);
3043 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev
),
3044 __func__
, new_parent
? dev_name(new_parent
) : "<NULL>");
3045 error
= kobject_move(&dev
->kobj
, new_parent_kobj
);
3047 cleanup_glue_dir(dev
, new_parent_kobj
);
3048 put_device(new_parent
);
3051 old_parent
= dev
->parent
;
3052 dev
->parent
= new_parent
;
3054 klist_remove(&dev
->p
->knode_parent
);
3056 klist_add_tail(&dev
->p
->knode_parent
,
3057 &new_parent
->p
->klist_children
);
3058 set_dev_node(dev
, dev_to_node(new_parent
));
3062 error
= device_move_class_links(dev
, old_parent
, new_parent
);
3064 /* We ignore errors on cleanup since we're hosed anyway... */
3065 device_move_class_links(dev
, new_parent
, old_parent
);
3066 if (!kobject_move(&dev
->kobj
, &old_parent
->kobj
)) {
3068 klist_remove(&dev
->p
->knode_parent
);
3069 dev
->parent
= old_parent
;
3071 klist_add_tail(&dev
->p
->knode_parent
,
3072 &old_parent
->p
->klist_children
);
3073 set_dev_node(dev
, dev_to_node(old_parent
));
3076 cleanup_glue_dir(dev
, new_parent_kobj
);
3077 put_device(new_parent
);
3081 switch (dpm_order
) {
3082 case DPM_ORDER_NONE
:
3084 case DPM_ORDER_DEV_AFTER_PARENT
:
3085 device_pm_move_after(dev
, new_parent
);
3086 devices_kset_move_after(dev
, new_parent
);
3088 case DPM_ORDER_PARENT_BEFORE_DEV
:
3089 device_pm_move_before(new_parent
, dev
);
3090 devices_kset_move_before(new_parent
, dev
);
3092 case DPM_ORDER_DEV_LAST
:
3093 device_pm_move_last(dev
);
3094 devices_kset_move_last(dev
);
3098 put_device(old_parent
);
3104 EXPORT_SYMBOL_GPL(device_move
);
3107 * device_shutdown - call ->shutdown() on each device to shutdown.
3109 void device_shutdown(void)
3111 struct device
*dev
, *parent
;
3113 wait_for_device_probe();
3114 device_block_probing();
3118 spin_lock(&devices_kset
->list_lock
);
3120 * Walk the devices list backward, shutting down each in turn.
3121 * Beware that device unplug events may also start pulling
3122 * devices offline, even as the system is shutting down.
3124 while (!list_empty(&devices_kset
->list
)) {
3125 dev
= list_entry(devices_kset
->list
.prev
, struct device
,
3129 * hold reference count of device's parent to
3130 * prevent it from being freed because parent's
3131 * lock is to be held
3133 parent
= get_device(dev
->parent
);
3136 * Make sure the device is off the kset list, in the
3137 * event that dev->*->shutdown() doesn't remove it.
3139 list_del_init(&dev
->kobj
.entry
);
3140 spin_unlock(&devices_kset
->list_lock
);
3142 /* hold lock to avoid race with probe/release */
3144 device_lock(parent
);
3147 /* Don't allow any more runtime suspends */
3148 pm_runtime_get_noresume(dev
);
3149 pm_runtime_barrier(dev
);
3151 if (dev
->class && dev
->class->shutdown_pre
) {
3153 dev_info(dev
, "shutdown_pre\n");
3154 dev
->class->shutdown_pre(dev
);
3156 if (dev
->bus
&& dev
->bus
->shutdown
) {
3158 dev_info(dev
, "shutdown\n");
3159 dev
->bus
->shutdown(dev
);
3160 } else if (dev
->driver
&& dev
->driver
->shutdown
) {
3162 dev_info(dev
, "shutdown\n");
3163 dev
->driver
->shutdown(dev
);
3168 device_unlock(parent
);
3173 spin_lock(&devices_kset
->list_lock
);
3175 spin_unlock(&devices_kset
->list_lock
);
3179 * Device logging functions
3182 #ifdef CONFIG_PRINTK
3184 create_syslog_header(const struct device
*dev
, char *hdr
, size_t hdrlen
)
3190 subsys
= dev
->class->name
;
3192 subsys
= dev
->bus
->name
;
3196 pos
+= snprintf(hdr
+ pos
, hdrlen
- pos
, "SUBSYSTEM=%s", subsys
);
3201 * Add device identifier DEVICE=:
3205 * +sound:card0 subsystem:devname
3207 if (MAJOR(dev
->devt
)) {
3210 if (strcmp(subsys
, "block") == 0)
3215 pos
+= snprintf(hdr
+ pos
, hdrlen
- pos
,
3217 c
, MAJOR(dev
->devt
), MINOR(dev
->devt
));
3218 } else if (strcmp(subsys
, "net") == 0) {
3219 struct net_device
*net
= to_net_dev(dev
);
3222 pos
+= snprintf(hdr
+ pos
, hdrlen
- pos
,
3223 "DEVICE=n%u", net
->ifindex
);
3226 pos
+= snprintf(hdr
+ pos
, hdrlen
- pos
,
3227 "DEVICE=+%s:%s", subsys
, dev_name(dev
));
3236 dev_WARN(dev
, "device/subsystem name too long");
3240 int dev_vprintk_emit(int level
, const struct device
*dev
,
3241 const char *fmt
, va_list args
)
3246 hdrlen
= create_syslog_header(dev
, hdr
, sizeof(hdr
));
3248 return vprintk_emit(0, level
, hdrlen
? hdr
: NULL
, hdrlen
, fmt
, args
);
3250 EXPORT_SYMBOL(dev_vprintk_emit
);
3252 int dev_printk_emit(int level
, const struct device
*dev
, const char *fmt
, ...)
3257 va_start(args
, fmt
);
3259 r
= dev_vprintk_emit(level
, dev
, fmt
, args
);
3265 EXPORT_SYMBOL(dev_printk_emit
);
3267 static void __dev_printk(const char *level
, const struct device
*dev
,
3268 struct va_format
*vaf
)
3271 dev_printk_emit(level
[1] - '0', dev
, "%s %s: %pV",
3272 dev_driver_string(dev
), dev_name(dev
), vaf
);
3274 printk("%s(NULL device *): %pV", level
, vaf
);
3277 void dev_printk(const char *level
, const struct device
*dev
,
3278 const char *fmt
, ...)
3280 struct va_format vaf
;
3283 va_start(args
, fmt
);
3288 __dev_printk(level
, dev
, &vaf
);
3292 EXPORT_SYMBOL(dev_printk
);
3294 #define define_dev_printk_level(func, kern_level) \
3295 void func(const struct device *dev, const char *fmt, ...) \
3297 struct va_format vaf; \
3300 va_start(args, fmt); \
3305 __dev_printk(kern_level, dev, &vaf); \
3309 EXPORT_SYMBOL(func);
3311 define_dev_printk_level(_dev_emerg
, KERN_EMERG
);
3312 define_dev_printk_level(_dev_alert
, KERN_ALERT
);
3313 define_dev_printk_level(_dev_crit
, KERN_CRIT
);
3314 define_dev_printk_level(_dev_err
, KERN_ERR
);
3315 define_dev_printk_level(_dev_warn
, KERN_WARNING
);
3316 define_dev_printk_level(_dev_notice
, KERN_NOTICE
);
3317 define_dev_printk_level(_dev_info
, KERN_INFO
);
3321 static inline bool fwnode_is_primary(struct fwnode_handle
*fwnode
)
3323 return fwnode
&& !IS_ERR(fwnode
->secondary
);
3327 * set_primary_fwnode - Change the primary firmware node of a given device.
3328 * @dev: Device to handle.
3329 * @fwnode: New primary firmware node of the device.
3331 * Set the device's firmware node pointer to @fwnode, but if a secondary
3332 * firmware node of the device is present, preserve it.
3334 void set_primary_fwnode(struct device
*dev
, struct fwnode_handle
*fwnode
)
3337 struct fwnode_handle
*fn
= dev
->fwnode
;
3339 if (fwnode_is_primary(fn
))
3343 WARN_ON(fwnode
->secondary
);
3344 fwnode
->secondary
= fn
;
3346 dev
->fwnode
= fwnode
;
3348 dev
->fwnode
= fwnode_is_primary(dev
->fwnode
) ?
3349 dev
->fwnode
->secondary
: NULL
;
3352 EXPORT_SYMBOL_GPL(set_primary_fwnode
);
3355 * set_secondary_fwnode - Change the secondary firmware node of a given device.
3356 * @dev: Device to handle.
3357 * @fwnode: New secondary firmware node of the device.
3359 * If a primary firmware node of the device is present, set its secondary
3360 * pointer to @fwnode. Otherwise, set the device's firmware node pointer to
3363 void set_secondary_fwnode(struct device
*dev
, struct fwnode_handle
*fwnode
)
3366 fwnode
->secondary
= ERR_PTR(-ENODEV
);
3368 if (fwnode_is_primary(dev
->fwnode
))
3369 dev
->fwnode
->secondary
= fwnode
;
3371 dev
->fwnode
= fwnode
;
3375 * device_set_of_node_from_dev - reuse device-tree node of another device
3376 * @dev: device whose device-tree node is being set
3377 * @dev2: device whose device-tree node is being reused
3379 * Takes another reference to the new device-tree node after first dropping
3380 * any reference held to the old node.
3382 void device_set_of_node_from_dev(struct device
*dev
, const struct device
*dev2
)
3384 of_node_put(dev
->of_node
);
3385 dev
->of_node
= of_node_get(dev2
->of_node
);
3386 dev
->of_node_reused
= true;
3388 EXPORT_SYMBOL_GPL(device_set_of_node_from_dev
);